-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnap.cpp
More file actions
553 lines (486 loc) · 19.4 KB
/
snap.cpp
File metadata and controls
553 lines (486 loc) · 19.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
#define _CRT_SECURE_NO_WARNINGS
#define _WIN32_WINNT 0x0A00
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <mfapi.h>
#include <mfidl.h>
#include <mfreadwrite.h>
#include <mftransform.h>
#include <shlwapi.h>
#include <string>
#include <vector>
#include <fstream>
#include <webp/encode.h>
#include <winhttp.h>
#include <cstdlib> // for atoi
#pragma comment(lib, "mfplat.lib")
#pragma comment(lib, "mfreadwrite.lib")
#pragma comment(lib, "mf.lib")
#pragma comment(lib, "shlwapi.lib")
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "libwebp.lib")
#pragma comment(lib, "libsharpyuv.lib")
#pragma comment(lib, "ws2_32.lib")
// Service-related globals
SERVICE_STATUS g_ServiceStatus = { 0 };
SERVICE_STATUS_HANDLE g_StatusHandle = NULL;
HANDLE g_ServiceStopEvent = INVALID_HANDLE_VALUE;
HANDLE g_HttpStopEvent = INVALID_HANDLE_VALUE;
DWORD g_WaitPeriod = 5000; // Common wait period in ms, changeable via HTTP
#define SERVICE_NAME L"snap"
#define HTTP_PORT 46768
// Helper function to get the directory of the executable
std::wstring GetExeDirectory() {
wchar_t exePath[MAX_PATH];
GetModuleFileNameW(NULL, exePath, MAX_PATH);
PathRemoveFileSpecW(exePath); // Remove the executable name to get the directory
return std::wstring(exePath);
}
// Helper function to ensure Temp directory exists
std::wstring EnsureTempDirectory() {
std::wstring tempDir = GetExeDirectory() + L"\\temp";
CreateDirectoryW(tempDir.c_str(), NULL); // Create if it doesn't exist
return tempDir;
}
// Simple file logging for service debugging
void LogToFile(const std::wstring& message) {
std::wstring logPath = EnsureTempDirectory() + L"\\service.log"; // Log in Temp folder
std::wofstream file(logPath, std::ios::app);
if (file.is_open()) {
SYSTEMTIME st;
GetLocalTime(&st);
wchar_t timeStr[64];
swprintf_s(timeStr, L"%04d-%02d-%02d %02d:%02d:%02d: ",
st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
file << timeStr << message << L"\n";
file.close();
}
}
// HTTP Server Thread
DWORD WINAPI HttpServerThread(LPVOID lpParam) {
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
LogToFile(L"WSAStartup failed");
return 1;
}
SOCKET listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (listenSocket == INVALID_SOCKET) {
LogToFile(L"socket failed");
WSACleanup();
return 1;
}
sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons(HTTP_PORT);
if (bind(listenSocket, (SOCKADDR*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
LogToFile(L"bind failed: " + std::to_wstring(WSAGetLastError()));
closesocket(listenSocket);
WSACleanup();
return 1;
}
if (listen(listenSocket, SOMAXCONN) == SOCKET_ERROR) {
LogToFile(L"listen failed: " + std::to_wstring(WSAGetLastError()));
closesocket(listenSocket);
WSACleanup();
return 1;
}
LogToFile(L"HTTP server listening on port " + std::to_wstring(HTTP_PORT));
char recvBuf[1024];
const int recvBufLen = static_cast<int>(sizeof(recvBuf) - 1);
while (WaitForSingleObject(g_HttpStopEvent, 100) == WAIT_TIMEOUT) {
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(listenSocket, &readfds);
timeval tv = { 0, 100000 }; // 100ms timeout for polling
if (select(0, &readfds, NULL, NULL, &tv) > 0 && FD_ISSET(listenSocket, &readfds)) {
SOCKET clientSocket = accept(listenSocket, NULL, NULL);
if (clientSocket != INVALID_SOCKET) {
int recvLen = recv(clientSocket, recvBuf, recvBufLen, 0);
if (recvLen > 0) {
recvBuf[recvLen] = '\0';
std::string request(recvBuf);
// Simple parsing for GET /setwait?period=XXXX HTTP/1.1
if (request.find("GET /setwait?period=") == 0) {
size_t queryStart = request.find("period=") + 7;
size_t spacePos = request.find(' ', queryStart);
if (queryStart != std::string::npos && spacePos != std::string::npos) {
std::string periodStr = request.substr(queryStart, spacePos - queryStart);
DWORD newPeriod = static_cast<DWORD>(std::atoi(periodStr.c_str()));
if (newPeriod >= 100 && newPeriod <= 60000) { // Reasonable range: 0.1s to 60s
g_WaitPeriod = newPeriod;
LogToFile(L"Wait period updated to " + std::to_wstring(g_WaitPeriod) + L" ms via HTTP");
std::string body = "{\"period\": " + std::to_string(g_WaitPeriod) + "}";
const int bodyLen = static_cast<int>(body.length());
std::string response = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: " +
std::to_string(bodyLen) + "\r\n\r\n" + body;
send(clientSocket, response.c_str(), static_cast<int>(response.length()), 0);
}
else {
LogToFile(L"Invalid wait period: " + std::to_wstring(newPeriod));
std::string body = "{\"error\": \"Invalid period\"}";
const int bodyLen = static_cast<int>(body.length());
std::string response = "HTTP/1.1 400 Bad Request\r\nContent-Type: application/json\r\nContent-Length: " +
std::to_string(bodyLen) + "\r\n\r\n" + body;
send(clientSocket, response.c_str(), static_cast<int>(response.length()), 0);
}
}
else {
std::string body = "{\"error\": \"Missing period parameter\"}";
const int bodyLen = static_cast<int>(body.length());
std::string response = "HTTP/1.1 400 Bad Request\r\nContent-Type: application/json\r\nContent-Length: " +
std::to_string(bodyLen) + "\r\n\r\n" + body;
send(clientSocket, response.c_str(), static_cast<int>(response.length()), 0);
}
}
else if (request.find("GET /getwait") == 0) {
std::string body = "{\"period\": " + std::to_string(g_WaitPeriod) + "}";
const int bodyLen = static_cast<int>(body.length());
std::string response = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: " +
std::to_string(bodyLen) + "\r\n\r\n" + body;
send(clientSocket, response.c_str(), static_cast<int>(response.length()), 0);
}
else {
// 404 for other requests
const char* response = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\n\r\n";
send(clientSocket, response, static_cast<int>(strlen(response)), 0);
}
}
closesocket(clientSocket);
}
}
}
closesocket(listenSocket);
WSACleanup();
LogToFile(L"HTTP server stopped");
return 0;
}
// Save BGRA bitmap to WebP using libwebp
void SaveBitmapToWebP(BYTE* pData, UINT32 width, UINT32 height, const std::wstring& filePath) {
// pData is BGRA; convert to RGBA for WebP
std::vector<BYTE> rgbaData(width * height * 4);
BYTE* rgba = rgbaData.data();
for (UINT32 y = 0; y < height; ++y) {
BYTE* srcRow = pData + y * width * 4;
BYTE* dstRow = rgba + y * width * 4;
for (UINT32 x = 0; x < width; ++x) {
BYTE b = srcRow[x * 4 + 0];
BYTE g = srcRow[x * 4 + 1];
BYTE r = srcRow[x * 4 + 2];
BYTE a = srcRow[x * 4 + 3];
dstRow[x * 4 + 0] = r;
dstRow[x * 4 + 1] = g;
dstRow[x * 4 + 2] = b;
dstRow[x * 4 + 3] = a;
}
}
uint8_t* webpData = nullptr;
int webpSize = WebPEncodeRGBA(rgba, width, height, width * 4, 80.0f, &webpData);
if (webpSize == 0) {
LogToFile(L"Failed to encode WebP");
WebPFree(webpData);
return;
}
std::ofstream file(filePath, std::ios::binary);
if (!file.is_open()) {
LogToFile(L"Failed to open WebP file for writing: " + filePath);
WebPFree(webpData);
return;
}
file.write(reinterpret_cast<char*>(webpData), webpSize);
file.close();
WebPFree(webpData);
LogToFile(L"Saved frame to: " + filePath);
}
// Utility: Converts YUY2 buffer to BGRA buffer (Windows RGB32 convention)
void YUY2ToRGB32(const BYTE* yuy2, BYTE* bgra32, UINT32 width, UINT32 height) {
for (UINT32 i = 0; i < width * height; i += 2) {
int y0 = yuy2[i * 2 + 0];
int u = yuy2[i * 2 + 1] - 128;
int y1 = yuy2[i * 2 + 2];
int v = yuy2[i * 2 + 3] - 128;
auto clamp = [](int val) {
if (val < 0) return 0;
if (val > 255) return 255;
return val;
};
int c0 = y0 - 16;
int c1 = y1 - 16;
int d = u;
int e = v;
int r0 = clamp((298 * c0 + 409 * e + 128) >> 8);
int g0 = clamp((298 * c0 - 100 * d - 208 * e + 128) >> 8);
int b0 = clamp((298 * c0 + 516 * d + 128) >> 8);
int r1 = clamp((298 * c1 + 409 * e + 128) >> 8);
int g1 = clamp((298 * c1 - 100 * d - 208 * e + 128) >> 8);
int b1 = clamp((298 * c1 + 516 * d + 128) >> 8);
bgra32[i * 4 + 0] = b0;
bgra32[i * 4 + 1] = g0;
bgra32[i * 4 + 2] = r0;
bgra32[i * 4 + 3] = 255;
bgra32[(i + 1) * 4 + 0] = b1;
bgra32[(i + 1) * 4 + 1] = g1;
bgra32[(i + 1) * 4 + 2] = r1;
bgra32[(i + 1) * 4 + 3] = 255;
}
}
// Function to capture a single frame
DWORD CaptureFrame() {
HRESULT hr;
IMFAttributes* pAttributes = nullptr;
hr = MFCreateAttributes(&pAttributes, 1);
if (FAILED(hr)) {
LogToFile(L"MFCreateAttributes failed: 0x" + std::to_wstring(hr));
return hr;
}
hr = pAttributes->SetGUID(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE, MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID);
if (FAILED(hr)) {
LogToFile(L"SetGUID failed: 0x" + std::to_wstring(hr));
pAttributes->Release();
return hr;
}
IMFActivate** ppDevices = nullptr;
UINT32 count = 0;
hr = MFEnumDeviceSources(pAttributes, &ppDevices, &count);
pAttributes->Release();
if (FAILED(hr) || count == 0) {
LogToFile(L"No camera devices found or error: 0x" + std::to_wstring(hr));
return hr;
}
IMFMediaSource* pSource = nullptr;
hr = ppDevices[0]->ActivateObject(IID_PPV_ARGS(&pSource));
for (UINT32 i = 0; i < count; i++) {
ppDevices[i]->Release();
}
CoTaskMemFree(ppDevices);
if (FAILED(hr)) {
LogToFile(L"ActivateObject failed: 0x" + std::to_wstring(hr));
return hr;
}
IMFSourceReader* pReader = nullptr;
hr = MFCreateSourceReaderFromMediaSource(pSource, NULL, &pReader);
if (FAILED(hr)) {
LogToFile(L"MFCreateSourceReaderFromMediaSource failed: 0x" + std::to_wstring(hr));
pSource->Release();
return hr;
}
bool foundType = false;
DWORD i = 0;
GUID subtype = { 0 };
while (true) {
IMFMediaType* pType = nullptr;
hr = pReader->GetNativeMediaType(MF_SOURCE_READER_FIRST_VIDEO_STREAM, i, &pType);
if (FAILED(hr)) break;
hr = pType->GetGUID(MF_MT_SUBTYPE, &subtype);
if (SUCCEEDED(hr)) {
if (subtype == MFVideoFormat_RGB32 || subtype == MFVideoFormat_YUY2) {
hr = pReader->SetCurrentMediaType(MF_SOURCE_READER_FIRST_VIDEO_STREAM, NULL, pType);
if (SUCCEEDED(hr)) {
foundType = true;
pType->Release();
break;
}
}
}
pType->Release();
i++;
}
if (!foundType) {
LogToFile(L"Failed to set a supported video format (RGB32 or YUY2)");
pReader->Release();
pSource->Release();
return ERROR_UNSUPPORTED_TYPE;
}
IMFMediaType* pCurrentType = nullptr;
UINT32 width = 0, height = 0;
hr = pReader->GetCurrentMediaType(MF_SOURCE_READER_FIRST_VIDEO_STREAM, &pCurrentType);
if (FAILED(hr) || !pCurrentType) {
LogToFile(L"GetCurrentMediaType failed: 0x" + std::to_wstring(hr));
pReader->Release();
pSource->Release();
return hr;
}
MFGetAttributeSize(pCurrentType, MF_MT_FRAME_SIZE, &width, &height);
pCurrentType->Release();
bool captured = false;
while (!captured) {
DWORD streamIndex = 0, flags = 0;
LONGLONG timestamp = 0;
IMFSample* pSample = nullptr;
hr = pReader->ReadSample(MF_SOURCE_READER_FIRST_VIDEO_STREAM, 0, &streamIndex, &flags, ×tamp, &pSample);
if (FAILED(hr)) {
LogToFile(L"ReadSample failed: 0x" + std::to_wstring(hr));
break;
}
if (!pSample) {
LogToFile(L"Empty sample, retrying...");
Sleep(100);
continue;
}
IMFMediaBuffer* pBuffer = nullptr;
hr = pSample->ConvertToContiguousBuffer(&pBuffer);
if (FAILED(hr) || !pBuffer) {
LogToFile(L"ConvertToContiguousBuffer failed: 0x" + std::to_wstring(hr));
pSample->Release();
break;
}
BYTE* pData = nullptr;
DWORD maxLen = 0, currentLen = 0;
hr = pBuffer->Lock(&pData, &maxLen, ¤tLen);
if (FAILED(hr) || !pData) {
LogToFile(L"Buffer lock failed: 0x" + std::to_wstring(hr));
pBuffer->Release();
pSample->Release();
break;
}
std::vector<BYTE> bgraData;
if (subtype == MFVideoFormat_YUY2) {
bgraData.resize(width * height * 4);
YUY2ToRGB32(pData, bgraData.data(), width, height);
}
else if (subtype == MFVideoFormat_RGB32) {
bgraData.assign(pData, pData + width * height * 4);
}
wchar_t filePath[MAX_PATH];
SYSTEMTIME st;
GetLocalTime(&st);
std::wstring tempDir = EnsureTempDirectory(); // Use Temp folder in current directory
swprintf_s(filePath, MAX_PATH, L"%s\\capture_%04d%02d%02d_%02d%02d%02d.webp",
tempDir.c_str(), st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
SaveBitmapToWebP(bgraData.data(), width, height, filePath);
pBuffer->Unlock();
pBuffer->Release();
pSample->Release();
captured = true;
}
pSource->Stop();
pReader->Release();
pSource->Release();
return S_OK;
}
// Main loop for capturing frames
DWORD CaptureFrameLoop(BOOL isService) {
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (FAILED(hr)) {
LogToFile(L"CoInitializeEx failed: 0x" + std::to_wstring(hr));
return hr;
}
hr = MFStartup(MF_VERSION);
if (FAILED(hr)) {
LogToFile(L"MFStartup failed: 0x" + std::to_wstring(hr));
CoUninitialize();
return hr;
}
// Start HTTP server thread
g_HttpStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (g_HttpStopEvent == NULL) {
LogToFile(L"CreateEvent for HTTP failed");
MFShutdown();
CoUninitialize();
return E_FAIL;
}
HANDLE hHttpThread = CreateThread(NULL, 0, HttpServerThread, NULL, 0, NULL);
if (hHttpThread == NULL) {
LogToFile(L"Failed to create HTTP thread");
CloseHandle(g_HttpStopEvent);
MFShutdown();
CoUninitialize();
return E_FAIL;
}
if (isService) {
g_ServiceStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (g_ServiceStopEvent == NULL) {
LogToFile(L"CreateEvent for service failed");
SetEvent(g_HttpStopEvent);
WaitForSingleObject(hHttpThread, 5000);
CloseHandle(hHttpThread);
CloseHandle(g_HttpStopEvent);
MFShutdown();
CoUninitialize();
return E_FAIL;
}
LogToFile(L"Service mode: Capturing every " + std::to_wstring(g_WaitPeriod) + L" ms (change via http://localhost:" + std::to_wstring(HTTP_PORT) + L"/setwait?period=XXXX)");
while (WaitForSingleObject(g_ServiceStopEvent, g_WaitPeriod) == WAIT_TIMEOUT) {
CaptureFrame();
}
CloseHandle(g_ServiceStopEvent);
}
else {
LogToFile(L"Console mode: Capturing every " + std::to_wstring(g_WaitPeriod) + L" ms (press ESC to exit; change via http://localhost:" + std::to_wstring(HTTP_PORT) + L"/setwait?period=XXXX)");
while (true) {
CaptureFrame();
if (GetAsyncKeyState(VK_ESCAPE) & 0x8000) {
LogToFile(L"Escape key pressed, exiting...");
break;
}
Sleep(g_WaitPeriod);
}
}
// Stop HTTP thread
SetEvent(g_HttpStopEvent);
WaitForSingleObject(hHttpThread, 5000);
CloseHandle(hHttpThread);
CloseHandle(g_HttpStopEvent);
MFShutdown();
CoUninitialize();
return S_OK;
}
// Service control handler
VOID WINAPI ServiceCtrlHandler(DWORD CtrlCode) {
switch (CtrlCode) {
case SERVICE_CONTROL_STOP:
if (g_ServiceStatus.dwCurrentState != SERVICE_RUNNING)
break;
g_ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
SetServiceStatus(g_StatusHandle, &g_ServiceStatus);
SetEvent(g_ServiceStopEvent);
break;
default:
break;
}
}
// Service main function
VOID WINAPI ServiceMain(DWORD argc, LPTSTR* argv) {
g_StatusHandle = RegisterServiceCtrlHandler(SERVICE_NAME, ServiceCtrlHandler);
if (g_StatusHandle == NULL) {
LogToFile(L"RegisterServiceCtrlHandler failed");
return;
}
g_ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
g_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
g_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
SetServiceStatus(g_StatusHandle, &g_ServiceStatus);
g_ServiceStopEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (g_ServiceStopEvent == NULL) {
LogToFile(L"CreateEvent failed");
g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(g_StatusHandle, &g_ServiceStatus);
return;
}
g_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus(g_StatusHandle, &g_ServiceStatus);
CaptureFrameLoop(TRUE);
g_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus(g_StatusHandle, &g_ServiceStatus);
CloseHandle(g_ServiceStopEvent);
}
// Main entry point
int wmain() {
SERVICE_TABLE_ENTRY ServiceTable[] = {
{ (LPWSTR)SERVICE_NAME, (LPSERVICE_MAIN_FUNCTION)ServiceMain },
{ NULL, NULL }
};
if (StartServiceCtrlDispatcher(ServiceTable)) {
return 0;
}
else {
DWORD error = GetLastError();
if (error == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) {
LogToFile(L"Running in console mode (press ESC to exit)...");
return static_cast<int>(CaptureFrameLoop(FALSE));
}
LogToFile(L"StartServiceCtrlDispatcher failed: " + std::to_wstring(error));
return static_cast<int>(error);
}
}