mirror of
https://github.com/quantum5/winscap.git
synced 2025-04-24 05:31:58 -04:00
Handle default output device change by capturing from new device (#2)
This commit is contained in:
parent
df1299f5de
commit
837d713edf
74
winscap.cpp
74
winscap.cpp
|
@ -32,6 +32,44 @@ _COM_SMARTPTR_TYPEDEF(IMMDevice, __uuidof(IMMDevice));
|
||||||
_COM_SMARTPTR_TYPEDEF(IAudioClient, __uuidof(IAudioClient));
|
_COM_SMARTPTR_TYPEDEF(IAudioClient, __uuidof(IAudioClient));
|
||||||
_COM_SMARTPTR_TYPEDEF(IAudioCaptureClient, __uuidof(IAudioCaptureClient));
|
_COM_SMARTPTR_TYPEDEF(IAudioCaptureClient, __uuidof(IAudioCaptureClient));
|
||||||
|
|
||||||
|
class DeviceChangeNotification : public IMMNotificationClient {
|
||||||
|
volatile ULONG ref;
|
||||||
|
volatile bool &changed;
|
||||||
|
|
||||||
|
public:
|
||||||
|
DeviceChangeNotification(volatile bool &changed) : changed(changed) {}
|
||||||
|
|
||||||
|
// This is meant to be allocated on stack, so we don't actually free.
|
||||||
|
STDMETHODIMP_(ULONG) AddRef() { return InterlockedIncrement(&ref); }
|
||||||
|
STDMETHODIMP_(ULONG) Release() { return InterlockedDecrement(&ref); }
|
||||||
|
|
||||||
|
STDMETHODIMP QueryInterface(REFIID iid, void **ppv) {
|
||||||
|
if (iid == IID_IUnknown) {
|
||||||
|
AddRef();
|
||||||
|
*ppv = (IUnknown *)this;
|
||||||
|
} else if (iid == __uuidof(IMMNotificationClient)) {
|
||||||
|
AddRef();
|
||||||
|
*ppv = (IMMNotificationClient *)this;
|
||||||
|
} else {
|
||||||
|
*ppv = nullptr;
|
||||||
|
return E_NOINTERFACE;
|
||||||
|
}
|
||||||
|
return S_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
STDMETHODIMP OnDefaultDeviceChanged(EDataFlow flow, ERole role, LPCWSTR) {
|
||||||
|
if (flow == eRender && role == eConsole) {
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
return S_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
STDMETHODIMP OnDeviceAdded(LPCWSTR) { return S_OK; }
|
||||||
|
STDMETHODIMP OnDeviceRemoved(LPCWSTR) { return S_OK; }
|
||||||
|
STDMETHODIMP OnDeviceStateChanged(LPCWSTR, DWORD) { return S_OK; }
|
||||||
|
STDMETHODIMP OnPropertyValueChanged(LPCWSTR, const PROPERTYKEY) { return S_OK; }
|
||||||
|
};
|
||||||
|
|
||||||
class EnsureCaptureStop {
|
class EnsureCaptureStop {
|
||||||
IAudioClientPtr m_client;
|
IAudioClientPtr m_client;
|
||||||
|
|
||||||
|
@ -99,15 +137,6 @@ int main(int argc, char *argv[]) {
|
||||||
CCoInitialize comInit;
|
CCoInitialize comInit;
|
||||||
ensure(comInit);
|
ensure(comInit);
|
||||||
|
|
||||||
IMMDeviceEnumeratorPtr pEnumerator;
|
|
||||||
ensure(pEnumerator.CreateInstance(__uuidof(MMDeviceEnumerator)));
|
|
||||||
|
|
||||||
IMMDevicePtr pDevice;
|
|
||||||
ensure(pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pDevice));
|
|
||||||
|
|
||||||
IAudioClientPtr pClient;
|
|
||||||
ensure(pDevice->Activate(__uuidof(IAudioClient), CLSCTX_ALL, nullptr, (void **)&pClient));
|
|
||||||
|
|
||||||
WAVEFORMATEX wfx;
|
WAVEFORMATEX wfx;
|
||||||
wfx.wFormatTag = WAVE_FORMAT_PCM;
|
wfx.wFormatTag = WAVE_FORMAT_PCM;
|
||||||
wfx.nChannels = (WORD)channels;
|
wfx.nChannels = (WORD)channels;
|
||||||
|
@ -116,6 +145,20 @@ int main(int argc, char *argv[]) {
|
||||||
wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;
|
wfx.nBlockAlign = wfx.nChannels * wfx.wBitsPerSample / 8;
|
||||||
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
|
wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
|
||||||
|
|
||||||
|
IMMDeviceEnumeratorPtr pEnumerator;
|
||||||
|
ensure(pEnumerator.CreateInstance(__uuidof(MMDeviceEnumerator)));
|
||||||
|
|
||||||
|
volatile bool deviceChanged = false;
|
||||||
|
DeviceChangeNotification deviceChangeNotification(deviceChanged);
|
||||||
|
pEnumerator->RegisterEndpointNotificationCallback(&deviceChangeNotification);
|
||||||
|
|
||||||
|
for (;;) {
|
||||||
|
IMMDevicePtr pDevice;
|
||||||
|
ensure(pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pDevice));
|
||||||
|
|
||||||
|
IAudioClientPtr pClient;
|
||||||
|
ensure(pDevice->Activate(__uuidof(IAudioClient), CLSCTX_ALL, nullptr, (void **)&pClient));
|
||||||
|
|
||||||
ensure(pClient->Initialize(AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_LOOPBACK,
|
ensure(pClient->Initialize(AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_LOOPBACK,
|
||||||
16 * REFTIMES_PER_MILLISEC, 0, &wfx, nullptr));
|
16 * REFTIMES_PER_MILLISEC, 0, &wfx, nullptr));
|
||||||
|
|
||||||
|
@ -135,11 +178,18 @@ int main(int argc, char *argv[]) {
|
||||||
ensure(pClient->Start());
|
ensure(pClient->Start());
|
||||||
EnsureCaptureStop autoStop(pClient);
|
EnsureCaptureStop autoStop(pClient);
|
||||||
|
|
||||||
for (;;) {
|
while (!deviceChanged) {
|
||||||
Sleep(dwDelay);
|
Sleep(dwDelay);
|
||||||
|
|
||||||
UINT32 packetLength;
|
UINT32 packetLength;
|
||||||
ensure(pCapture->GetNextPacketSize(&packetLength));
|
HRESULT hrNext = pCapture->GetNextPacketSize(&packetLength);
|
||||||
|
if (hrNext == AUDCLNT_E_DEVICE_INVALIDATED) {
|
||||||
|
while (!deviceChanged)
|
||||||
|
;
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
ensure(hrNext);
|
||||||
|
}
|
||||||
|
|
||||||
while (packetLength) {
|
while (packetLength) {
|
||||||
LPBYTE pData;
|
LPBYTE pData;
|
||||||
|
@ -156,4 +206,6 @@ int main(int argc, char *argv[]) {
|
||||||
ensure(pCapture->GetNextPacketSize(&packetLength));
|
ensure(pCapture->GetNextPacketSize(&packetLength));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
deviceChanged = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue