16
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
17
18
18
#include < shlwapi.h>
19
+ #include < wrl/client.h>
19
20
20
21
#include " Common/Thread/ThreadUtil.h"
21
22
#include " CaptureDevice.h"
25
26
#include " Core/HLE/sceUsbCam.h"
26
27
#include " Core/Config.h"
27
28
29
+ using Microsoft::WRL::ComPtr;
30
+
28
31
namespace MFAPI {
29
32
HINSTANCE Mflib;
30
33
HINSTANCE Mfplatlib;
@@ -142,7 +145,7 @@ HRESULT ReaderCallback::OnReadSample(
142
145
LONGLONG llTimestamp,
143
146
IMFSample *pSample) {
144
147
HRESULT hr = S_OK;
145
- IMFMediaBuffer * pBuffer = nullptr ;
148
+ ComPtr< IMFMediaBuffer> pBuffer;
146
149
std::lock_guard<std::mutex> lock (device->sdMutex );
147
150
if (device->isShutDown ())
148
151
return hr;
@@ -173,7 +176,7 @@ HRESULT ReaderCallback::OnReadSample(
173
176
174
177
// pSample can be null, in this case ReadSample still should be called to request next frame.
175
178
if (pSample) {
176
- videoBuffer = new VideoBufferLock (pBuffer);
179
+ videoBuffer = new VideoBufferLock (pBuffer. Get () );
177
180
hr = videoBuffer->LockBuffer (device->deviceParam .default_stride , device->deviceParam .height , &pbScanline0, &lStride);
178
181
179
182
if (lStride > 0 )
@@ -278,7 +281,6 @@ HRESULT ReaderCallback::OnReadSample(
278
281
}
279
282
}
280
283
281
- SafeRelease (&pBuffer);
282
284
return hr;
283
285
}
284
286
@@ -522,18 +524,15 @@ bool WindowsCaptureDevice::init() {
522
524
523
525
bool WindowsCaptureDevice::start (void *startParam) {
524
526
HRESULT hr = S_OK;
525
- IMFAttributes * pAttributes = nullptr ;
526
- IMFMediaType * pType = nullptr ;
527
+ ComPtr< IMFAttributes> pAttributes;
528
+ ComPtr< IMFMediaType> pType;
527
529
UINT32 selection = 0 ;
528
530
UINT32 count = 0 ;
529
531
530
532
// Release old sources first(if any).
531
- SafeRelease (&m_pSource);
532
- SafeRelease (&m_pReader);
533
- if (m_pCallback) {
534
- delete m_pCallback;
535
- m_pCallback = nullptr ;
536
- }
533
+ m_pSource = nullptr ;
534
+ m_pReader = nullptr ;
535
+ m_pCallback = nullptr ;
537
536
// Need to re-enumerate the list,because old sources were released.
538
537
std::vector<std::string> deviceList = getDeviceList (true );
539
538
@@ -555,25 +554,24 @@ bool WindowsCaptureDevice::start(void *startParam) {
555
554
}
556
555
++count;
557
556
}
558
- setSelction (selection);
557
+ setSelection (selection);
559
558
hr = param.ppDevices [param.selection ]->ActivateObject (
560
- __uuidof (IMFMediaSource),
561
- (void **)&m_pSource);
559
+ IID_PPV_ARGS (&m_pSource));
562
560
563
561
if (SUCCEEDED (hr))
564
562
hr = MFCreateAttributes (&pAttributes, 2 );
565
563
566
564
// Use async mode
567
565
if (SUCCEEDED (hr))
568
- hr = pAttributes->SetUnknown (MF_SOURCE_READER_ASYNC_CALLBACK, m_pCallback);
566
+ hr = pAttributes->SetUnknown (MF_SOURCE_READER_ASYNC_CALLBACK, m_pCallback. Get () );
569
567
570
568
if (SUCCEEDED (hr))
571
569
hr = pAttributes->SetUINT32 (MF_READWRITE_DISABLE_CONVERTERS, TRUE );
572
570
573
571
if (SUCCEEDED (hr)) {
574
572
hr = CreateSourceReaderFromMediaSource (
575
- m_pSource,
576
- pAttributes,
573
+ m_pSource. Get () ,
574
+ pAttributes. Get () ,
577
575
&m_pReader
578
576
);
579
577
}
@@ -609,7 +607,7 @@ bool WindowsCaptureDevice::start(void *startParam) {
609
607
610
608
if (FAILED (hr)) { break ; }
611
609
612
- hr = setDeviceParam (pType);
610
+ hr = setDeviceParam (pType. Get () );
613
611
614
612
if (SUCCEEDED (hr))
615
613
break ;
@@ -654,7 +652,7 @@ bool WindowsCaptureDevice::start(void *startParam) {
654
652
655
653
if (FAILED (hr)) { break ; }
656
654
657
- hr = setDeviceParam (pType);
655
+ hr = setDeviceParam (pType. Get () );
658
656
659
657
if (SUCCEEDED (hr))
660
658
break ;
@@ -679,15 +677,11 @@ bool WindowsCaptureDevice::start(void *startParam) {
679
677
setError (CAPTUREDEVIDE_ERROR_START_FAILED, " Cannot start" );
680
678
if (m_pSource)
681
679
m_pSource->Shutdown ();
682
- SafeRelease (&m_pSource);
683
- SafeRelease (&pAttributes);
684
- SafeRelease (&pType);
685
- SafeRelease (&m_pReader);
680
+ m_pSource = nullptr ;
681
+ m_pReader = nullptr ;
686
682
return false ;
687
683
}
688
684
689
- SafeRelease (&pAttributes);
690
- SafeRelease (&pType);
691
685
updateState (CAPTUREDEVIDE_STATE::STARTED);
692
686
break ;
693
687
case CAPTUREDEVIDE_STATE::LOST:
@@ -751,13 +745,13 @@ std::vector<std::string> WindowsCaptureDevice::getDeviceList(bool forceEnum, int
751
745
752
746
if (SUCCEEDED (hr)) {
753
747
// Get the size needed first
754
- dwMinSize = WideCharToMultiByte (CP_UTF8, NULL , pwstrName, -1 , nullptr , 0 , nullptr , FALSE );
748
+ dwMinSize = WideCharToMultiByte (CP_UTF8, 0 , pwstrName, -1 , nullptr , 0 , nullptr , FALSE );
755
749
if (dwMinSize == 0 )
756
750
hr = E_FAIL;
757
751
}
758
752
if (SUCCEEDED (hr)) {
759
753
cstrName = new char [dwMinSize];
760
- WideCharToMultiByte (CP_UTF8, NULL , pwstrName, -1 , cstrName, dwMinSize, NULL , FALSE );
754
+ WideCharToMultiByte (CP_UTF8, 0 , pwstrName, -1 , cstrName, dwMinSize, NULL , FALSE );
761
755
strName = cstrName;
762
756
delete[] cstrName;
763
757
@@ -937,9 +931,9 @@ void WindowsCaptureDevice::messageHandler() {
937
931
stop ();
938
932
939
933
std::lock_guard<std::mutex> lock (sdMutex);
940
- SafeRelease (& m_pSource) ;
941
- SafeRelease (& m_pReader) ;
942
- delete m_pCallback;
934
+ m_pSource = nullptr ;
935
+ m_pReader = nullptr ;
936
+ m_pCallback = nullptr ;
943
937
unRegisterCMPTMFApis ();
944
938
945
939
std::unique_lock<std::mutex> lock2 (paramMutex);
@@ -957,7 +951,7 @@ void WindowsCaptureDevice::messageHandler() {
957
951
958
952
HRESULT WindowsCaptureDevice::enumDevices () {
959
953
HRESULT hr = S_OK;
960
- IMFAttributes * pAttributes = nullptr ;
954
+ ComPtr< IMFAttributes> pAttributes;
961
955
962
956
hr = MFCreateAttributes (&pAttributes, 1 );
963
957
if (SUCCEEDED (hr)) {
@@ -982,10 +976,9 @@ HRESULT WindowsCaptureDevice::enumDevices() {
982
976
}
983
977
}
984
978
if (SUCCEEDED (hr)) {
985
- hr = EnumDeviceSources (pAttributes, ¶m.ppDevices , ¶m.count );
979
+ hr = EnumDeviceSources (pAttributes. Get () , ¶m.ppDevices , ¶m.count );
986
980
}
987
981
988
- SafeRelease (&pAttributes);
989
982
return hr;
990
983
}
991
984
0 commit comments