-
-
Notifications
You must be signed in to change notification settings - Fork 492
/
Copy pathLoopbackCapture.h
142 lines (121 loc) · 4.17 KB
/
LoopbackCapture.h
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
#pragma once
#include <AudioClient.h>
#include <mmdeviceapi.h>
#include <initguid.h>
#include <guiddef.h>
#include <mfapi.h>
#include <wrl\implements.h>
#include <wil\com.h>
#include <wil\result.h>
#include <mfidl.h>
#include <mfapi.h>
#include <mfobjects.h>
#include <functional>
#ifndef METHODASYNCCALLBACK
#define METHODASYNCCALLBACK(Parent, AsyncCallback, pfnCallback) \
class Callback##AsyncCallback :\
public IMFAsyncCallback \
{ \
public: \
Callback##AsyncCallback() : \
_parent(((Parent*)((BYTE*)this - offsetof(Parent, m_x##AsyncCallback)))), \
_dwQueueID( MFASYNC_CALLBACK_QUEUE_MULTITHREADED ) \
{ \
} \
\
STDMETHOD_( ULONG, AddRef )() \
{ \
return _parent->AddRef(); \
} \
STDMETHOD_( ULONG, Release )() \
{ \
return _parent->Release(); \
} \
STDMETHOD( QueryInterface )( REFIID riid, void **ppvObject ) \
{ \
if (riid == IID_IMFAsyncCallback || riid == IID_IUnknown) \
{ \
(*ppvObject) = this; \
AddRef(); \
return S_OK; \
} \
*ppvObject = NULL; \
return E_NOINTERFACE; \
} \
STDMETHOD( GetParameters )( \
/* [out] */ __RPC__out DWORD *pdwFlags, \
/* [out] */ __RPC__out DWORD *pdwQueue) \
{ \
*pdwFlags = 0; \
*pdwQueue = _dwQueueID; \
return S_OK; \
} \
STDMETHOD( Invoke )( /* [out] */ __RPC__out IMFAsyncResult * pResult ) \
{ \
_parent->pfnCallback( pResult ); \
return S_OK; \
} \
void SetQueueID( DWORD dwQueueID ) { _dwQueueID = dwQueueID; } \
\
protected: \
Parent* _parent; \
DWORD _dwQueueID; \
\
} m_x##AsyncCallback;
#endif
using namespace Microsoft::WRL;
// Courtesy of Microsoft: https://learn.microsoft.com/en-us/samples/microsoft/windows-classic-samples/applicationloopbackaudio-sample/
class CLoopbackCapture :
public RuntimeClass< RuntimeClassFlags< ClassicCom >, FtmBase, IActivateAudioInterfaceCompletionHandler >
{
public:
CLoopbackCapture() = default;
~CLoopbackCapture();
void SetOutputSink(std::function<void(std::vector<uint8_t>&&)> samples);
HRESULT StartCaptureAsync(DWORD processId, bool includeProcessTree);
HRESULT StopCaptureAsync();
METHODASYNCCALLBACK(CLoopbackCapture, StartCapture, OnStartCapture);
METHODASYNCCALLBACK(CLoopbackCapture, StopCapture, OnStopCapture);
METHODASYNCCALLBACK(CLoopbackCapture, SampleReady, OnSampleReady);
METHODASYNCCALLBACK(CLoopbackCapture, FinishCapture, OnFinishCapture);
// IActivateAudioInterfaceCompletionHandler
STDMETHOD(ActivateCompleted)(IActivateAudioInterfaceAsyncOperation* operation);
private:
// NB: All states >= Initialized will allow some methods
// to be called successfully on the Audio Client
enum class DeviceState
{
Uninitialized,
Error,
Initialized,
Starting,
Capturing,
Stopping,
Stopped,
};
HRESULT OnStartCapture(IMFAsyncResult* pResult);
HRESULT OnStopCapture(IMFAsyncResult* pResult);
HRESULT OnFinishCapture(IMFAsyncResult* pResult);
HRESULT OnSampleReady(IMFAsyncResult* pResult);
HRESULT InitializeLoopbackCapture();
HRESULT OnAudioSampleRequested();
HRESULT ActivateAudioInterface(DWORD processId, bool includeProcessTree);
HRESULT FinishCaptureAsync();
HRESULT SetDeviceStateErrorIfFailed(HRESULT hr);
wil::com_ptr_nothrow<IAudioClient> m_AudioClient;
WAVEFORMATEX m_CaptureFormat{};
UINT32 m_BufferFrames = 0;
wil::com_ptr_nothrow<IAudioCaptureClient> m_AudioCaptureClient;
wil::com_ptr_nothrow<IMFAsyncResult> m_SampleReadyAsyncResult;
wil::unique_event_nothrow m_SampleReadyEvent;
MFWORKITEM_KEY m_SampleReadyKey = 0;
wil::critical_section m_CritSec;
DWORD m_dwQueueID = 0;
// These two members are used to communicate between the main thread
// and the ActivateCompleted callback.
HRESULT m_activateResult = E_UNEXPECTED;
DeviceState m_DeviceState{ DeviceState::Uninitialized };
wil::unique_event_nothrow m_hActivateCompleted;
wil::unique_event_nothrow m_hCaptureStopped;
std::function<void(std::vector<uint8_t>&&)> m_samples = nullptr;
};