-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcapture.cc
More file actions
1051 lines (864 loc) · 32.5 KB
/
capture.cc
File metadata and controls
1051 lines (864 loc) · 32.5 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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//////////////////////////////////////////////////////////////////////////
//
// capture.cpp: Manages video capture.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <Dbt.h>
#include <Wmcodecdsp.h>
#include <assert.h>
#include <mfapi.h>
#include <mfidl.h>
#include <mfreadwrite.h>
#include <shlwapi.h>
#include <string>
#include <algorithm>
#include <cmath>
#include <new>
#include <cstring>
#include "capture.h"
#include "convert.h"
// Use SDK QISearch implementation (link to SDK libs via binding.gyp)
HRESULT CopyAttribute(IMFAttributes* pSrc, IMFAttributes* pDest, const GUID& key);
// Forward declaration for ConfigureSourceReader so StartCapture can call it.
HRESULT ConfigureSourceReader(IMFSourceReader* pReader);
// Forward declaration for helper used to deliver samples to frame callback
static HRESULT DeliverSampleToCallback(IMFSample* pSample, std::function<void(std::vector<uint8_t>&&)>& callback);
void DeviceList::Clear() {
for (UINT32 i = 0; i < m_cDevices; i++) {
SafeRelease(&m_ppDevices[i]);
}
CoTaskMemFree(m_ppDevices);
m_ppDevices = NULL;
m_cDevices = 0;
}
// Note: EnumerateDevices was removed from the public API. GetAllDevices
// performs enumeration internally to simplify the DeviceList interface.
// ... index-based GetDevice removed; use GetDevice(identifier, ppActivate) instead.
HRESULT DeviceList::GetDevice(const WCHAR* identifier, IMFActivate** ppActivate) {
if (!identifier || !ppActivate) return E_POINTER;
*ppActivate = nullptr;
// Enumerate devices on demand
if (m_cDevices == 0) {
IMFAttributes* pAttributes = nullptr;
HRESULT hr = MFCreateAttributes(&pAttributes, 1);
if (SUCCEEDED(hr)) hr = pAttributes->SetGUID(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE, MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID);
if (SUCCEEDED(hr)) hr = MFEnumDeviceSources(pAttributes, &m_ppDevices, &m_cDevices);
SafeRelease(&pAttributes);
if (FAILED(hr)) {
m_cDevices = 0;
m_ppDevices = nullptr;
return hr;
}
}
if (!m_ppDevices || m_cDevices == 0) return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
const UINT32 MAX_DEVICES = 256;
UINT32 limit = (m_cDevices < MAX_DEVICES) ? m_cDevices : MAX_DEVICES;
for (UINT32 i = 0; i < limit; ++i) {
WCHAR* pFriendly = nullptr;
WCHAR* pSymbolic = nullptr;
HRESULT hr1 = m_ppDevices[i]->GetAllocatedString(MF_DEVSOURCE_ATTRIBUTE_FRIENDLY_NAME, &pFriendly, nullptr);
HRESULT hr2 = m_ppDevices[i]->GetAllocatedString(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK, &pSymbolic, nullptr);
bool match = (SUCCEEDED(hr1) && pFriendly && _wcsicmp(pFriendly, identifier) == 0) || (SUCCEEDED(hr2) && pSymbolic && _wcsicmp(pSymbolic, identifier) == 0);
if (pFriendly) {
CoTaskMemFree(pFriendly);
pFriendly = nullptr;
}
if (pSymbolic) {
CoTaskMemFree(pSymbolic);
pSymbolic = nullptr;
}
if (match) {
*ppActivate = m_ppDevices[i];
(*ppActivate)->AddRef();
return S_OK;
}
}
return HRESULT_FROM_WIN32(ERROR_NOT_FOUND);
}
HRESULT DeviceList::GetAllDevices(std::vector<std::pair<std::wstring, std::wstring>>& outDevices) {
outDevices.clear();
// Fresh enumeration each call keeps caller code simple.
Clear();
IMFAttributes* pAttributes = nullptr;
HRESULT hr = MFCreateAttributes(&pAttributes, 1);
if (SUCCEEDED(hr)) {
hr = pAttributes->SetGUID(
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE,
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID);
}
if (SUCCEEDED(hr)) {
hr = MFEnumDeviceSources(pAttributes, &m_ppDevices, &m_cDevices);
}
SafeRelease(&pAttributes);
if (FAILED(hr)) {
// keep internal state consistent on failure
m_cDevices = 0;
m_ppDevices = NULL;
return hr;
}
if (m_cDevices == 0 || m_ppDevices == nullptr) {
return S_OK; // no devices
}
const UINT32 MAX_DEVICES = 256; // safety cap
UINT32 toProcess = (m_cDevices < MAX_DEVICES) ? m_cDevices : MAX_DEVICES;
// Minimal loop: get strings, copy to std::wstring, free, push result.
for (UINT32 i = 0; i < toProcess; ++i) {
WCHAR* pFriendly = nullptr;
WCHAR* pSymbolic = nullptr;
HRESULT hr1 = m_ppDevices[i]->GetAllocatedString(
MF_DEVSOURCE_ATTRIBUTE_FRIENDLY_NAME, &pFriendly, nullptr);
HRESULT hr2 = m_ppDevices[i]->GetAllocatedString(
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK, &pSymbolic, nullptr);
std::wstring friendly;
std::wstring symbolic;
if (SUCCEEDED(hr1) && pFriendly) {
friendly.assign(pFriendly);
}
if (pFriendly) CoTaskMemFree(pFriendly);
if (SUCCEEDED(hr2) && pSymbolic) {
symbolic.assign(pSymbolic);
}
if (pSymbolic) CoTaskMemFree(pSymbolic);
outDevices.emplace_back(std::move(friendly), std::move(symbolic));
}
return S_OK;
}
//-------------------------------------------------------------------
// CreateInstance
//
// Static class method to create the CCapture object.
//-------------------------------------------------------------------
HRESULT CCapture::CreateInstance(
HWND hwnd, // Handle to the window to receive events
CCapture** ppCapture // Receives a pointer to the CCapture object.
) {
if (ppCapture == NULL) {
return E_POINTER;
}
CCapture* pCapture = new (std::nothrow) CCapture(hwnd);
if (pCapture == NULL) {
return E_OUTOFMEMORY;
}
// The CCapture constructor sets the ref count to 1.
*ppCapture = pCapture;
return S_OK;
}
//-------------------------------------------------------------------
// constructor
//-------------------------------------------------------------------
CCapture::CCapture(HWND hwnd) : m_pReader(NULL),
m_hwndEvent(hwnd),
m_nRefCount(1),
m_bFirstSample(FALSE),
m_llBaseTime(0),
m_pwszSymbolicLink(NULL),
m_outputFormat(GUID_NULL),
m_pWicFactory(NULL) {
InitializeCriticalSection(&m_critsec);
}
//-------------------------------------------------------------------
// destructor
//-------------------------------------------------------------------
CCapture::~CCapture() {
assert(m_pReader == NULL);
SafeRelease(&m_pWicFactory);
DeleteCriticalSection(&m_critsec);
}
/////////////// IUnknown methods ///////////////
//-------------------------------------------------------------------
// AddRef
//-------------------------------------------------------------------
ULONG CCapture::AddRef() {
return InterlockedIncrement(&m_nRefCount);
}
//-------------------------------------------------------------------
// Release
//-------------------------------------------------------------------
ULONG CCapture::Release() {
ULONG uCount = InterlockedDecrement(&m_nRefCount);
if (uCount == 0) {
delete this;
}
return uCount;
}
//-------------------------------------------------------------------
// QueryInterface
//-------------------------------------------------------------------
HRESULT CCapture::QueryInterface(REFIID riid, void** ppv) {
static const QITAB qit[] =
{
QITABENT(CCapture, IMFSourceReaderCallback),
{0},
};
return QISearch(this, qit, riid, ppv);
}
/////////////// IMFSourceReaderCallback methods ///////////////
//-------------------------------------------------------------------
// OnReadSample
//
// Called when the IMFMediaSource::ReadSample method completes.
//-------------------------------------------------------------------
HRESULT CCapture::OnReadSample(
HRESULT hrStatus,
DWORD /*dwStreamIndex*/,
DWORD /*dwStreamFlags*/,
LONGLONG llTimeStamp,
IMFSample* pSample // Can be NULL
) {
EnterCriticalSection(&m_critsec);
if (!IsCapturing()) {
LeaveCriticalSection(&m_critsec);
return S_OK;
}
HRESULT hr = S_OK;
if (FAILED(hrStatus)) {
hr = hrStatus;
goto done;
}
if (pSample) {
if (m_bFirstSample) {
m_llBaseTime = llTimeStamp;
m_bFirstSample = FALSE;
}
// rebase the time stamp
llTimeStamp -= m_llBaseTime;
hr = pSample->SetSampleTime(llTimeStamp);
if (FAILED(hr)) {
goto done;
}
if (m_frameCallback) {
// Get current input format
IMFMediaType* pType = NULL;
GUID subtype = GUID_NULL;
UINT32 width = 0, height = 0;
if (SUCCEEDED(m_pReader->GetCurrentMediaType(MF_SOURCE_READER_FIRST_VIDEO_STREAM, &pType)) && pType) {
pType->GetGUID(MF_MT_SUBTYPE, &subtype);
MFGetAttributeSize(pType, MF_MT_FRAME_SIZE, &width, &height);
SafeRelease(&pType);
}
// Check if conversion needed
bool needsConversion = !IsEqualGUID(m_outputFormat, GUID_NULL) && !IsEqualGUID(subtype, m_outputFormat);
if (needsConversion && width > 0 && height > 0) {
std::vector<uint8_t> converted;
if (SUCCEEDED(ConvertFrame(pSample, subtype, width, height, converted)) && !converted.empty()) {
try { m_frameCallback(std::move(converted)); } catch (...) {}
}
} else {
// No conversion; deliver raw buffer
IMFMediaBuffer* pBuffer = NULL;
if (SUCCEEDED(pSample->ConvertToContiguousBuffer(&pBuffer)) && pBuffer) {
BYTE* pData = NULL;
DWORD curLen = 0;
if (SUCCEEDED(pBuffer->Lock(&pData, NULL, &curLen)) && pData && curLen > 0) {
try {
std::vector<uint8_t> out(pData, pData + curLen);
m_frameCallback(std::move(out));
} catch (...) {}
pBuffer->Unlock();
}
SafeRelease(&pBuffer);
}
}
}
}
// Read another sample.
hr = m_pReader->ReadSample(
(DWORD)MF_SOURCE_READER_FIRST_VIDEO_STREAM,
0,
NULL, // actual
NULL, // flags
NULL, // timestamp
NULL // sample
);
done:
if (FAILED(hr)) {
NotifyError(hr);
}
LeaveCriticalSection(&m_critsec);
return hr;
}
//-------------------------------------------------------------------
// OpenMediaSource
//
// Set up preview for a specified media source.
//-------------------------------------------------------------------
HRESULT CCapture::OpenMediaSource(IMFMediaSource* pSource) {
HRESULT hr = S_OK;
IMFAttributes* pAttributes = NULL;
hr = MFCreateAttributes(&pAttributes, 2);
if (SUCCEEDED(hr)) {
hr = pAttributes->SetUnknown(MF_SOURCE_READER_ASYNC_CALLBACK, this);
}
if (SUCCEEDED(hr)) {
hr = MFCreateSourceReaderFromMediaSource(
pSource,
pAttributes,
&m_pReader);
}
SafeRelease(&pAttributes);
return hr;
}
//-------------------------------------------------------------------
// StartCapture
//
// Start capturing.
//-------------------------------------------------------------------
HRESULT CCapture::StartCapture(
IMFActivate* pActivate,
const EncodingParameters& param) {
HRESULT hr = S_OK;
IMFMediaSource* pSource = NULL;
EnterCriticalSection(&m_critsec);
// Entry: start capture
// If we don't already have a source reader, create the media source
// and open it. InitFromActivate may have already created m_pReader
// so avoid re-activating/opening which can cause the media source to
// report that an event generator already has a listener.
if (m_pReader == NULL) {
// Create the media source for the device.
hr = pActivate->ActivateObject(
__uuidof(IMFMediaSource),
(void**)&pSource);
// ActivateObject result in hr
// Get the symbolic link. This is needed to handle device-
// loss notifications. (See CheckDeviceLost.)
if (SUCCEEDED(hr)) {
hr = pActivate->GetAllocatedString(
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK,
&m_pwszSymbolicLink,
NULL);
// GetAllocatedString result in hr
}
if (SUCCEEDED(hr)) {
hr = OpenMediaSource(pSource);
}
} else {
// We already initialized the source reader in InitFromActivate.
// Ensure we have a symbolic link recorded for device loss handling.
if (m_pwszSymbolicLink == NULL) {
HRESULT hrSym = pActivate->GetAllocatedString(
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK,
&m_pwszSymbolicLink,
NULL);
if (FAILED(hrSym)) {
// Non-fatal: continue with existing reader if symbolic link cannot be obtained.
}
}
}
// We don't write to files in this build; operate in callback-only mode.
if (SUCCEEDED(hr)) {
// operate in callback-only mode; no sink writer
}
// Set up the encoding parameters. Only configure the sink writer when
// we actually have a writer. When no writer is present (frame-callback
// mode), request RGB32 output from the source reader so the sample
// buffers are uncompressed and usable by the embedding (JS).
if (SUCCEEDED(hr)) {
// Operate in callback-only mode; prefer RGB32 output from source reader
HRESULT hrCfg = ConfigureSourceReader(m_pReader);
if (FAILED(hrCfg)) {
// Non-fatal: continue with whatever format the reader provides.
hr = S_OK;
}
}
if (SUCCEEDED(hr)) {
m_bFirstSample = TRUE;
m_llBaseTime = 0;
// Request the first video frame.
// Ensure the video stream is selected (in case Stop deselected it).
if (m_pReader) {
HRESULT hrSel = m_pReader->SetStreamSelection(MF_SOURCE_READER_FIRST_VIDEO_STREAM, TRUE);
// ignore hrSel if it fails; ReadSample will surface errors.
}
hr = m_pReader->ReadSample(
(DWORD)MF_SOURCE_READER_FIRST_VIDEO_STREAM,
0,
NULL,
NULL,
NULL,
NULL);
}
SafeRelease(&pSource);
LeaveCriticalSection(&m_critsec);
return hr;
}
// Helper to extract contiguous buffer from an IMFSample and call the frame callback
static HRESULT DeliverSampleToCallback(IMFSample* pSample, std::function<void(std::vector<uint8_t>&&)>& callback) {
if (!pSample || !callback) return E_POINTER;
IMFMediaBuffer* pBuffer = NULL;
HRESULT hr = pSample->ConvertToContiguousBuffer(&pBuffer);
if (FAILED(hr)) return hr;
BYTE* pData = NULL;
DWORD maxLen = 0, curLen = 0;
hr = pBuffer->Lock(&pData, &maxLen, &curLen);
if (SUCCEEDED(hr)) {
std::vector<uint8_t> vec(pData, pData + curLen);
callback(std::move(vec));
pBuffer->Unlock();
}
SafeRelease(&pBuffer);
return hr;
}
// Initialize the CCapture instance from an IMFActivate without starting capture.
// This creates the media source and source reader so GetSupportedFormats can
// enumerate native types without beginning an actual capture session.
HRESULT CCapture::InitFromActivate(IMFActivate* pActivate) {
if (!pActivate) return E_POINTER;
IMFMediaSource* pSource = NULL;
HRESULT hr = pActivate->ActivateObject(__uuidof(IMFMediaSource), (void**)&pSource);
if (SUCCEEDED(hr)) {
hr = OpenMediaSource(pSource);
}
if (pSource) SafeRelease(&pSource);
return hr;
}
//-------------------------------------------------------------------
// EndCaptureSession
//
// Stop the capture session.
//
// NOTE: This method resets the object's state to State_NotReady.
// To start another capture session, call SetCaptureFile.
//-------------------------------------------------------------------
HRESULT CCapture::EndCaptureSession() {
EnterCriticalSection(&m_critsec);
HRESULT hr = S_OK;
// Instead of releasing the source reader outright, flush and deselect the
// video stream so the reader stops delivering frames. This allows the
// reader to be reused by a subsequent StartCapture without re-creating
// the media source/reader, avoiding "already listening" event-generator
// errors and improving start/stop reliability.
if (m_pReader) {
// Flush queued samples for the video stream.
HRESULT hrFlush = m_pReader->Flush(MF_SOURCE_READER_FIRST_VIDEO_STREAM);
(void)hrFlush; // non-fatal
// Deselect the stream so ReadSample will not deliver further frames.
HRESULT hrSel = m_pReader->SetStreamSelection(MF_SOURCE_READER_FIRST_VIDEO_STREAM, FALSE);
(void)hrSel; // non-fatal
}
// Reset internal timing state so next start will rebase timestamps.
m_bFirstSample = TRUE;
m_llBaseTime = 0;
LeaveCriticalSection(&m_critsec);
return hr;
}
BOOL CCapture::IsCapturing() {
EnterCriticalSection(&m_critsec);
// Consider us capturing if we have a writer OR a registered frame callback.
BOOL bIsCapturing = (m_frameCallback != nullptr);
LeaveCriticalSection(&m_critsec);
return bIsCapturing;
}
//-------------------------------------------------------------------
// CheckDeviceLost
// Checks whether the video capture device was removed.
//
// The application calls this method when is receives a
// WM_DEVICECHANGE message.
//-------------------------------------------------------------------
HRESULT CCapture::CheckDeviceLost(DEV_BROADCAST_HDR* pHdr, BOOL* pbDeviceLost) {
if (pbDeviceLost == NULL) {
return E_POINTER;
}
EnterCriticalSection(&m_critsec);
DEV_BROADCAST_DEVICEINTERFACE* pDi = NULL;
*pbDeviceLost = FALSE;
if (!IsCapturing()) {
goto done;
}
if (pHdr == NULL) {
goto done;
}
if (pHdr->dbch_devicetype != DBT_DEVTYP_DEVICEINTERFACE) {
goto done;
}
// Compare the device name with the symbolic link.
pDi = (DEV_BROADCAST_DEVICEINTERFACE*)pHdr;
if (m_pwszSymbolicLink) {
#ifdef UNICODE
if (_wcsicmp(m_pwszSymbolicLink, pDi->dbcc_name) == 0) {
*pbDeviceLost = TRUE;
}
#else
// When UNICODE is not defined, pDi->dbcc_name is ANSI (char[]).
// Convert to wide string before comparing to m_pwszSymbolicLink.
WCHAR wszName[MAX_PATH];
if (MultiByteToWideChar(CP_ACP, 0, pDi->dbcc_name, -1, wszName, MAX_PATH) > 0) {
if (_wcsicmp(m_pwszSymbolicLink, wszName) == 0) {
*pbDeviceLost = TRUE;
}
}
#endif
}
done:
LeaveCriticalSection(&m_critsec);
return S_OK;
}
/////////////// Private/protected class methods ///////////////
//-------------------------------------------------------------------
// ConfigureSourceReader
//
// Sets the media type on the source reader.
//-------------------------------------------------------------------
HRESULT ConfigureSourceReader(IMFSourceReader* pReader) {
// Simplified behavior: respect any current media type previously set
// (for example via CCapture::SetFormat). Do not force-convert to RGB32
// or register color-converter MFTs. If no current media type is present,
// attempt to set the first native media type (index 0) as a sensible default.
if (pReader == NULL) return E_POINTER;
IMFMediaType* pType = NULL;
HRESULT hr = pReader->GetCurrentMediaType((DWORD)MF_SOURCE_READER_FIRST_VIDEO_STREAM, &pType);
if (SUCCEEDED(hr) && pType) {
// A current media type is already set (for example SetFormat was used).
SafeRelease(&pType);
return S_OK;
}
// No current media type set; try to use the first native media type.
hr = pReader->GetNativeMediaType((DWORD)MF_SOURCE_READER_FIRST_VIDEO_STREAM, 0, &pType);
if (SUCCEEDED(hr) && pType) {
hr = pReader->SetCurrentMediaType((DWORD)MF_SOURCE_READER_FIRST_VIDEO_STREAM, NULL, pType);
SafeRelease(&pType);
}
return hr;
}
// ConfigureEncoder removed: package operates in callback-only mode (no file writer)
//-------------------------------------------------------------------
// ConfigureCapture
//
// Configures the capture session.
//
//-------------------------------------------------------------------
HRESULT CCapture::ConfigureCapture(const EncodingParameters& param) {
// In callback-only mode we only need to prefer RGB32 via ConfigureSourceReader
// and leave the source reader configured appropriately.
HRESULT hr = ConfigureSourceReader(m_pReader);
return hr;
}
//-------------------------------------------------------------------
// EndCaptureInternal
//
// Stops capture.
//-------------------------------------------------------------------
HRESULT CCapture::EndCaptureInternal() {
HRESULT hr = S_OK;
SafeRelease(&m_pReader);
CoTaskMemFree(m_pwszSymbolicLink);
m_pwszSymbolicLink = NULL;
return hr;
}
// Preference: callers should use GetSupportedFormats which returns
// (subtype GUID, width, height, frameRate). If callers only need the
// distinct (width,height,frameRate) triplets, they can derive them from
// GetSupportedFormats in the wrapper layer. The dedicated
// GetSupportedFormats helper was removed to avoid duplicated enumeration
// logic and to keep a single authoritative enumeration path.
// Enumerate native media types including the subtype GUID so callers can
// inspect whether the device supports RGB32, YUV, MJPEG, etc.
HRESULT CCapture::GetSupportedFormats(std::vector<std::tuple<GUID, UINT32, UINT32, double>>& outTypes) {
outTypes.clear();
if (m_pReader == NULL) return E_FAIL;
DWORD index = 0;
while (true) {
IMFMediaType* pType = NULL;
HRESULT hr = m_pReader->GetNativeMediaType(MF_SOURCE_READER_FIRST_VIDEO_STREAM, index, &pType);
if (FAILED(hr)) break;
GUID subtype = {0};
UINT32 width = 0, height = 0;
UINT32 num = 0, denom = 0;
pType->GetGUID(MF_MT_SUBTYPE, &subtype);
MFGetAttributeSize(pType, MF_MT_FRAME_SIZE, &width, &height);
MFGetAttributeRatio(pType, MF_MT_FRAME_RATE, &num, &denom);
double frameRate = 0.0;
if (denom != 0) frameRate = static_cast<double>(num) / static_cast<double>(denom);
outTypes.emplace_back(subtype, width, height, frameRate);
SafeRelease(&pType);
++index;
}
// Sort + unique similar to GetSupportedFormats
std::sort(outTypes.begin(), outTypes.end(), [](const auto& a, const auto& b) {
if (std::get<1>(a) != std::get<1>(b)) return std::get<1>(a) < std::get<1>(b);
if (std::get<2>(a) != std::get<2>(b)) return std::get<2>(a) < std::get<2>(b);
if (std::get<3>(a) != std::get<3>(b)) return std::get<3>(a) < std::get<3>(b);
// fallback: compare GUID bytes
const GUID& ga = std::get<0>(a);
const GUID& gb = std::get<0>(b);
return memcmp(&ga, &gb, sizeof(GUID)) < 0;
});
const double eps = 1e-6;
auto last = std::unique(outTypes.begin(), outTypes.end(), [eps](const auto& a, const auto& b) {
return std::get<1>(a) == std::get<1>(b) && std::get<2>(a) == std::get<2>(b) && std::fabs(std::get<3>(a) - std::get<3>(b)) < eps && memcmp(&std::get<0>(a), &std::get<0>(b), sizeof(GUID)) == 0;
});
outTypes.erase(last, outTypes.end());
// No internal cache is maintained for supported formats; callers should
// use the returned outTypes directly if they need to validate.
return S_OK;
}
// Legacy desired-format helper removed; callers should use SetFormat with an explicit subtype
// Set desired format by explicit native subtype GUID (e.g., MFVideoFormat_MJPG)
HRESULT CCapture::SetFormat(const GUID& subtypeReq, UINT32 width, UINT32 height, double frameRate) {
if (m_pReader == NULL) return E_FAIL;
DWORD index = 0;
HRESULT hr = E_FAIL;
while (true) {
IMFMediaType* pType = NULL;
HRESULT hrType = m_pReader->GetNativeMediaType(MF_SOURCE_READER_FIRST_VIDEO_STREAM, index, &pType);
if (FAILED(hrType)) break;
GUID subtype = {0};
UINT32 w = 0, h = 0;
UINT32 num = 0, denom = 0;
pType->GetGUID(MF_MT_SUBTYPE, &subtype);
MFGetAttributeSize(pType, MF_MT_FRAME_SIZE, &w, &h);
MFGetAttributeRatio(pType, MF_MT_FRAME_RATE, &num, &denom);
double fr = 0.0;
if (denom != 0) fr = static_cast<double>(num) / static_cast<double>(denom);
if (IsEqualGUID(subtype, subtypeReq) && w == width && h == height && std::abs(fr - frameRate) < 1e-6) {
hr = m_pReader->SetCurrentMediaType(MF_SOURCE_READER_FIRST_VIDEO_STREAM, NULL, pType);
SafeRelease(&pType);
break;
}
SafeRelease(&pType);
++index;
}
return hr;
}
HRESULT CCapture::GetCurrentDimensions(UINT32* pWidth, UINT32* pHeight, double* pFrameRate) {
if (pWidth) *pWidth = 0;
if (pHeight) *pHeight = 0;
if (pFrameRate) *pFrameRate = 0.0;
if (m_pReader == NULL) return E_FAIL;
IMFMediaType* pType = NULL;
HRESULT hr = m_pReader->GetCurrentMediaType(MF_SOURCE_READER_FIRST_VIDEO_STREAM, &pType);
if (FAILED(hr) || pType == NULL) {
SafeRelease(&pType);
return hr;
}
UINT32 w = 0, h = 0;
UINT32 num = 0, denom = 0;
MFGetAttributeSize(pType, MF_MT_FRAME_SIZE, &w, &h);
MFGetAttributeRatio(pType, MF_MT_FRAME_RATE, &num, &denom);
double fr = 0.0;
if (denom != 0) fr = static_cast<double>(num) / static_cast<double>(denom);
if (pWidth) *pWidth = w;
if (pHeight) *pHeight = h;
if (pFrameRate) *pFrameRate = fr;
SafeRelease(&pType);
return S_OK;
}
// ...existing code...
// Add after last CCapture method (e.g., after GetCurrentDimensions)
HRESULT CCapture::ReleaseDevice() {
EndCaptureSession();
if (m_pReader) {
m_pReader->Release();
m_pReader = nullptr;
}
if (m_pwszSymbolicLink) {
CoTaskMemFree(m_pwszSymbolicLink);
m_pwszSymbolicLink = nullptr;
}
m_rgbaBuffer.clear();
m_frameCallback = nullptr;
m_bFirstSample = TRUE;
m_llBaseTime = 0;
m_outputFormat = GUID_NULL;
return S_OK;
}
// static
// EnumerateFormatsFromActivate removed; CCapture now exposes InitFromActivate + GetSupportedFormats
//-------------------------------------------------------------------
// SetOutputFormat
//-------------------------------------------------------------------
HRESULT CCapture::SetOutputFormat(const GUID& outputSubtype) {
EnterCriticalSection(&m_critsec);
m_outputFormat = outputSubtype;
LeaveCriticalSection(&m_critsec);
return S_OK;
}
//-------------------------------------------------------------------
// ClearOutputFormat
//-------------------------------------------------------------------
void CCapture::ClearOutputFormat() {
EnterCriticalSection(&m_critsec);
m_outputFormat = GUID_NULL;
LeaveCriticalSection(&m_critsec);
}
//-------------------------------------------------------------------
// EncodeToJpeg - Encode RGB/BGRA data to JPEG using WIC
//-------------------------------------------------------------------
HRESULT CCapture::EncodeToJpeg(const uint8_t* rgbData, UINT32 width, UINT32 height, bool isBGRA, std::vector<uint8_t>& outBuffer) {
HRESULT hr = S_OK;
// Create WIC factory if needed
if (!m_pWicFactory) {
hr = CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&m_pWicFactory));
if (FAILED(hr)) return hr;
}
IWICStream* pStream = NULL;
IWICBitmapEncoder* pEncoder = NULL;
IWICBitmapFrameEncode* pFrame = NULL;
IPropertyBag2* pPropertyBag = NULL;
// Create memory stream
hr = m_pWicFactory->CreateStream(&pStream);
if (FAILED(hr)) goto cleanup;
hr = pStream->InitializeFromMemory(NULL, 0);
if (FAILED(hr)) {
// Use IStream instead
IStream* pMemStream = NULL;
hr = CreateStreamOnHGlobal(NULL, TRUE, &pMemStream);
if (FAILED(hr)) goto cleanup;
hr = pStream->InitializeFromIStream(pMemStream);
pMemStream->Release();
if (FAILED(hr)) goto cleanup;
}
// Create JPEG encoder
hr = m_pWicFactory->CreateEncoder(GUID_ContainerFormatJpeg, NULL, &pEncoder);
if (FAILED(hr)) goto cleanup;
hr = pEncoder->Initialize(pStream, WICBitmapEncoderNoCache);
if (FAILED(hr)) goto cleanup;
hr = pEncoder->CreateNewFrame(&pFrame, &pPropertyBag);
if (FAILED(hr)) goto cleanup;
// Set quality
if (pPropertyBag) {
PROPBAG2 option = {0};
option.pstrName = const_cast<LPOLESTR>(L"ImageQuality");
VARIANT value;
VariantInit(&value);
value.vt = VT_R4;
value.fltVal = 0.85f;
pPropertyBag->Write(1, &option, &value);
}
hr = pFrame->Initialize(pPropertyBag);
if (FAILED(hr)) goto cleanup;
hr = pFrame->SetSize(width, height);
if (FAILED(hr)) goto cleanup;
{
WICPixelFormatGUID pixelFormat = isBGRA ? GUID_WICPixelFormat32bppBGRA : GUID_WICPixelFormat24bppBGR;
hr = pFrame->SetPixelFormat(&pixelFormat);
if (FAILED(hr)) goto cleanup;
UINT stride = width * (isBGRA ? 4 : 3);
UINT bufferSize = stride * height;
hr = pFrame->WritePixels(height, stride, bufferSize, const_cast<BYTE*>(rgbData));
if (FAILED(hr)) goto cleanup;
}
hr = pFrame->Commit();
if (FAILED(hr)) goto cleanup;
hr = pEncoder->Commit();
if (FAILED(hr)) goto cleanup;
// Read back from stream
{
LARGE_INTEGER zero = {0};
ULARGE_INTEGER size;
pStream->Seek(zero, STREAM_SEEK_END, &size);
pStream->Seek(zero, STREAM_SEEK_SET, NULL);
outBuffer.resize(static_cast<size_t>(size.QuadPart));
ULONG bytesRead = 0;
hr = pStream->Read(outBuffer.data(), static_cast<ULONG>(size.QuadPart), &bytesRead);
if (SUCCEEDED(hr)) outBuffer.resize(bytesRead);
}
cleanup:
SafeRelease(&pFrame);
SafeRelease(&pPropertyBag);
SafeRelease(&pEncoder);
SafeRelease(&pStream);
return hr;
}
//-------------------------------------------------------------------
// ConvertFrame - Convert sample to output format
//-------------------------------------------------------------------
HRESULT CCapture::ConvertFrame(IMFSample* pSample, const GUID& inputSubtype, UINT32 width, UINT32 height, std::vector<uint8_t>& outBuffer) {
bool isMjpegOutput = IsEqualGUID(m_outputFormat, MFVideoFormat_MJPG);
IMFMediaBuffer* pBuffer = NULL;
HRESULT hr = pSample->ConvertToContiguousBuffer(&pBuffer);
if (FAILED(hr) || !pBuffer) return hr;
BYTE* pData = NULL;
DWORD curLen = 0;
hr = pBuffer->Lock(&pData, NULL, &curLen);
if (FAILED(hr) || !pData) {
SafeRelease(&pBuffer);
return hr;
}
if (isMjpegOutput) {
// Convert to JPEG
// First need to get to RGB format
if (IsEqualGUID(inputSubtype, MFVideoFormat_RGB32)) {
// BGRA -> JPEG
hr = EncodeToJpeg(pData, width, height, true, outBuffer);
} else if (IsEqualGUID(inputSubtype, MFVideoFormat_RGB24)) {
// BGR24 -> JPEG
hr = EncodeToJpeg(pData, width, height, false, outBuffer);
} else if (IsEqualGUID(inputSubtype, MFVideoFormat_YUY2)) {
// YUY2 -> RGB -> JPEG
size_t pixelCount = width * height;
m_rgbaBuffer.resize(pixelCount * 3);
// Simple YUY2 to BGR conversion
for (UINT32 i = 0; i < pixelCount / 2; i++) {
int y0 = pData[i * 4 + 0];
int u = pData[i * 4 + 1];
int y1 = pData[i * 4 + 2];
int v = pData[i * 4 + 3];
int c0 = y0 - 16, c1 = y1 - 16;
int d = u - 128, e = v - 128;
auto clamp = [](int x) { return x < 0 ? 0 : (x > 255 ? 255 : x); };
m_rgbaBuffer[i * 6 + 0] = clamp((298 * c0 + 516 * d + 128) >> 8); // B
m_rgbaBuffer[i * 6 + 1] = clamp((298 * c0 - 100 * d - 208 * e + 128) >> 8); // G
m_rgbaBuffer[i * 6 + 2] = clamp((298 * c0 + 409 * e + 128) >> 8); // R
m_rgbaBuffer[i * 6 + 3] = clamp((298 * c1 + 516 * d + 128) >> 8); // B
m_rgbaBuffer[i * 6 + 4] = clamp((298 * c1 - 100 * d - 208 * e + 128) >> 8); // G
m_rgbaBuffer[i * 6 + 5] = clamp((298 * c1 + 409 * e + 128) >> 8); // R
}
hr = EncodeToJpeg(m_rgbaBuffer.data(), width, height, false, outBuffer);
} else if (IsEqualGUID(inputSubtype, MFVideoFormat_NV12)) {
// NV12 -> RGB -> JPEG
size_t pixelCount = width * height;