FreeRDP/server/Windows/wf_wasapi.c

334 lines
8.1 KiB
C
Raw Normal View History

2013-02-16 03:50:00 +04:00
#include "wf_wasapi.h"
#include "wf_info.h"
#include <initguid.h>
#include <mmdeviceapi.h>
2022-02-17 17:37:03 +03:00
#include <functiondiscoverykeys_devpkey.h>
#include <audioclient.h>
2013-02-16 03:50:00 +04:00
#include <freerdp/log.h>
#define TAG SERVER_TAG("windows")
2013-02-16 05:39:31 +04:00
//#define REFTIMES_PER_SEC 10000000
//#define REFTIMES_PER_MILLISEC 10000
2019-11-06 17:24:51 +03:00
#define REFTIMES_PER_SEC 100000
#define REFTIMES_PER_MILLISEC 100
2013-02-16 05:39:31 +04:00
//#define REFTIMES_PER_SEC 50000
//#define REFTIMES_PER_MILLISEC 50
2013-02-16 05:39:31 +04:00
#ifndef __MINGW32__
2019-11-06 17:24:51 +03:00
DEFINE_GUID(CLSID_MMDeviceEnumerator, 0xBCDE0395, 0xE52F, 0x467C, 0x8E, 0x3D, 0xC4, 0x57, 0x92,
0x91, 0x69, 0x2E);
DEFINE_GUID(IID_IMMDeviceEnumerator, 0xA95664D2, 0x9614, 0x4F35, 0xA7, 0x46, 0xDE, 0x8D, 0xB6, 0x36,
0x17, 0xE6);
DEFINE_GUID(IID_IAudioClient, 0x1cb9ad4c, 0xdbfa, 0x4c32, 0xb1, 0x78, 0xc2, 0xf5, 0x68, 0xa7, 0x03,
0xb2);
DEFINE_GUID(IID_IAudioCaptureClient, 0xc8adbd64, 0xe71e, 0x48a0, 0xa4, 0xde, 0x18, 0x5c, 0x39, 0x5c,
0xd3, 0x17);
#endif
2013-02-16 03:50:00 +04:00
LPWSTR devStr = NULL;
wfPeerContext* latestPeer = NULL;
int wf_rdpsnd_set_latest_peer(wfPeerContext* peer)
{
latestPeer = peer;
return 0;
}
int wf_wasapi_activate(RdpsndServerContext* context)
2013-02-16 03:50:00 +04:00
{
2019-11-06 17:24:51 +03:00
wchar_t* pattern = L"Stereo Mix";
HANDLE hThread;
2013-02-16 03:50:00 +04:00
wf_wasapi_get_device_string(pattern, &devStr);
if (devStr == NULL)
2013-02-16 03:50:00 +04:00
{
2014-09-12 19:38:12 +04:00
WLog_ERR(TAG, "Failed to match for output device! Disabling rdpsnd.");
2013-02-16 03:50:00 +04:00
return 1;
}
2014-09-12 19:38:12 +04:00
WLog_DBG(TAG, "RDPSND (WASAPI) Activated");
if (!(hThread = CreateThread(NULL, 0, wf_rdpsnd_wasapi_thread, latestPeer, 0, NULL)))
{
WLog_ERR(TAG, "CreateThread failed");
return 1;
}
CloseHandle(hThread);
2013-02-16 03:50:00 +04:00
return 0;
}
2019-11-06 17:24:51 +03:00
int wf_wasapi_get_device_string(LPWSTR pattern, LPWSTR* deviceStr)
2013-02-16 03:50:00 +04:00
{
HRESULT hr;
2019-11-06 17:24:51 +03:00
IMMDeviceEnumerator* pEnumerator = NULL;
IMMDeviceCollection* pCollection = NULL;
IMMDevice* pEndpoint = NULL;
IPropertyStore* pProps = NULL;
2013-02-16 03:50:00 +04:00
LPWSTR pwszID = NULL;
unsigned int count;
2013-02-16 03:50:00 +04:00
CoInitialize(NULL);
2019-11-06 17:24:51 +03:00
hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, &IID_IMMDeviceEnumerator,
(void**)&pEnumerator);
2013-02-16 03:50:00 +04:00
if (FAILED(hr))
{
2014-09-12 19:38:12 +04:00
WLog_ERR(TAG, "Failed to cocreate device enumerator");
2013-02-16 03:50:00 +04:00
exit(1);
}
2019-11-06 17:24:51 +03:00
hr = pEnumerator->lpVtbl->EnumAudioEndpoints(pEnumerator, eCapture, DEVICE_STATE_ACTIVE,
&pCollection);
if (FAILED(hr))
2013-02-16 03:50:00 +04:00
{
2014-09-12 19:38:12 +04:00
WLog_ERR(TAG, "Failed to create endpoint collection");
2013-02-16 03:50:00 +04:00
exit(1);
}
pCollection->lpVtbl->GetCount(pCollection, &count);
WLog_INFO(TAG, "Num endpoints: %u", count);
2013-02-16 03:50:00 +04:00
if (count == 0)
{
2014-09-12 19:38:12 +04:00
WLog_ERR(TAG, "No endpoints!");
2013-02-16 03:50:00 +04:00
exit(1);
}
for (unsigned int i = 0; i < count; ++i)
2013-02-16 03:50:00 +04:00
{
PROPVARIANT nameVar;
PropVariantInit(&nameVar);
hr = pCollection->lpVtbl->Item(pCollection, i, &pEndpoint);
2019-11-06 17:24:51 +03:00
if (FAILED(hr))
2013-02-16 03:50:00 +04:00
{
WLog_ERR(TAG, "Failed to get endpoint %u", i);
2013-02-16 03:50:00 +04:00
exit(1);
}
hr = pEndpoint->lpVtbl->GetId(pEndpoint, &pwszID);
2019-11-06 17:24:51 +03:00
if (FAILED(hr))
2013-02-16 03:50:00 +04:00
{
2014-09-12 19:38:12 +04:00
WLog_ERR(TAG, "Failed to get endpoint ID");
2013-02-16 03:50:00 +04:00
exit(1);
}
hr = pEndpoint->lpVtbl->OpenPropertyStore(pEndpoint, STGM_READ, &pProps);
2019-11-06 17:24:51 +03:00
if (FAILED(hr))
2013-02-16 03:50:00 +04:00
{
2014-09-12 19:38:12 +04:00
WLog_ERR(TAG, "Failed to open property store");
2013-02-16 03:50:00 +04:00
exit(1);
}
hr = pProps->lpVtbl->GetValue(pProps, &PKEY_Device_FriendlyName, &nameVar);
2019-11-06 17:24:51 +03:00
if (FAILED(hr))
2013-02-16 03:50:00 +04:00
{
2014-09-12 19:38:12 +04:00
WLog_ERR(TAG, "Failed to get device friendly name");
2013-02-16 03:50:00 +04:00
exit(1);
}
2019-11-06 17:24:51 +03:00
// do this a more reliable way
2013-02-16 03:50:00 +04:00
if (wcscmp(pattern, nameVar.pwszVal) < 0)
{
unsigned int devStrLen;
2014-09-12 19:38:12 +04:00
WLog_INFO(TAG, "Using sound ouput endpoint: [%s] (%s)", nameVar.pwszVal, pwszID);
2019-11-06 17:24:51 +03:00
// WLog_INFO(TAG, "matched %d characters", wcscmp(pattern, nameVar.pwszVal);
2013-02-16 03:50:00 +04:00
devStrLen = wcslen(pwszID);
2019-11-06 17:24:51 +03:00
*deviceStr = (LPWSTR)calloc(devStrLen + 1, 2);
if (!deviceStr)
return -1;
2019-11-06 17:24:51 +03:00
wcscpy_s(*deviceStr, devStrLen + 1, pwszID);
2013-02-16 03:50:00 +04:00
}
CoTaskMemFree(pwszID);
pwszID = NULL;
PropVariantClear(&nameVar);
pProps->lpVtbl->Release(pProps);
pProps = NULL;
pEndpoint->lpVtbl->Release(pEndpoint);
pEndpoint = NULL;
}
pCollection->lpVtbl->Release(pCollection);
pCollection = NULL;
pEnumerator->lpVtbl->Release(pEnumerator);
pEnumerator = NULL;
CoUninitialize();
return 0;
}
DWORD WINAPI wf_rdpsnd_wasapi_thread(LPVOID lpParam)
2013-02-16 03:50:00 +04:00
{
2019-11-06 17:24:51 +03:00
IMMDeviceEnumerator* pEnumerator = NULL;
IMMDevice* pDevice = NULL;
IAudioClient* pAudioClient = NULL;
IAudioCaptureClient* pCaptureClient = NULL;
WAVEFORMATEX* pwfx = NULL;
2013-02-16 03:50:00 +04:00
HRESULT hr;
REFERENCE_TIME hnsRequestedDuration = REFTIMES_PER_SEC;
REFERENCE_TIME hnsActualDuration;
UINT32 bufferFrameCount;
UINT32 numFramesAvailable;
UINT32 packetLength = 0;
UINT32 dCount = 0;
2019-11-06 17:24:51 +03:00
BYTE* pData;
2013-02-16 03:50:00 +04:00
wfPeerContext* context;
wfInfo* wfi;
wfi = wf_info_get_instance();
context = (wfPeerContext*)lpParam;
CoInitialize(NULL);
2019-11-06 17:24:51 +03:00
hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, &IID_IMMDeviceEnumerator,
(void**)&pEnumerator);
2013-02-16 03:50:00 +04:00
if (FAILED(hr))
{
2014-09-12 19:38:12 +04:00
WLog_ERR(TAG, "Failed to cocreate device enumerator");
2013-02-16 03:50:00 +04:00
exit(1);
}
hr = pEnumerator->lpVtbl->GetDevice(pEnumerator, devStr, &pDevice);
if (FAILED(hr))
{
2014-09-12 19:38:12 +04:00
WLog_ERR(TAG, "Failed to cocreate get device");
2013-02-16 03:50:00 +04:00
exit(1);
}
2019-11-06 17:24:51 +03:00
hr = pDevice->lpVtbl->Activate(pDevice, &IID_IAudioClient, CLSCTX_ALL, NULL,
(void**)&pAudioClient);
2013-02-16 03:50:00 +04:00
if (FAILED(hr))
{
2014-09-12 19:38:12 +04:00
WLog_ERR(TAG, "Failed to activate audio client");
2013-02-16 03:50:00 +04:00
exit(1);
}
hr = pAudioClient->lpVtbl->GetMixFormat(pAudioClient, &pwfx);
if (FAILED(hr))
{
2014-09-12 19:38:12 +04:00
WLog_ERR(TAG, "Failed to get mix format");
2013-02-16 03:50:00 +04:00
exit(1);
}
pwfx->wFormatTag = wfi->agreed_format->wFormatTag;
pwfx->nChannels = wfi->agreed_format->nChannels;
pwfx->nSamplesPerSec = wfi->agreed_format->nSamplesPerSec;
pwfx->nAvgBytesPerSec = wfi->agreed_format->nAvgBytesPerSec;
pwfx->nBlockAlign = wfi->agreed_format->nBlockAlign;
pwfx->wBitsPerSample = wfi->agreed_format->wBitsPerSample;
pwfx->cbSize = wfi->agreed_format->cbSize;
2013-02-16 03:50:00 +04:00
2019-11-06 17:24:51 +03:00
hr = pAudioClient->lpVtbl->Initialize(pAudioClient, AUDCLNT_SHAREMODE_SHARED, 0,
hnsRequestedDuration, 0, pwfx, NULL);
2013-02-16 03:50:00 +04:00
if (FAILED(hr))
{
2014-09-12 19:38:12 +04:00
WLog_ERR(TAG, "Failed to initialize the audio client");
2013-02-16 03:50:00 +04:00
exit(1);
}
hr = pAudioClient->lpVtbl->GetBufferSize(pAudioClient, &bufferFrameCount);
if (FAILED(hr))
{
2014-09-12 19:38:12 +04:00
WLog_ERR(TAG, "Failed to get buffer size");
2013-02-16 03:50:00 +04:00
exit(1);
}
2019-11-06 17:24:51 +03:00
hr = pAudioClient->lpVtbl->GetService(pAudioClient, &IID_IAudioCaptureClient,
(void**)&pCaptureClient);
2013-02-16 03:50:00 +04:00
if (FAILED(hr))
{
2014-09-12 19:38:12 +04:00
WLog_ERR(TAG, "Failed to get the capture client");
2013-02-16 03:50:00 +04:00
exit(1);
}
hnsActualDuration = (UINT32)REFTIMES_PER_SEC * bufferFrameCount / pwfx->nSamplesPerSec;
2013-02-16 03:50:00 +04:00
hr = pAudioClient->lpVtbl->Start(pAudioClient);
if (FAILED(hr))
{
2014-09-12 19:38:12 +04:00
WLog_ERR(TAG, "Failed to start capture");
2013-02-16 03:50:00 +04:00
exit(1);
}
dCount = 0;
2014-09-12 19:38:12 +04:00
while (wfi->snd_stop == FALSE)
2013-02-16 03:50:00 +04:00
{
DWORD flags;
2019-11-06 17:24:51 +03:00
Sleep(hnsActualDuration / REFTIMES_PER_MILLISEC / 2);
2013-02-20 02:17:31 +04:00
2013-02-16 03:50:00 +04:00
hr = pCaptureClient->lpVtbl->GetNextPacketSize(pCaptureClient, &packetLength);
if (FAILED(hr))
{
2014-09-12 19:38:12 +04:00
WLog_ERR(TAG, "Failed to get packet length");
2013-02-16 03:50:00 +04:00
exit(1);
}
while (packetLength != 0)
2013-02-16 03:50:00 +04:00
{
2019-11-06 17:24:51 +03:00
hr = pCaptureClient->lpVtbl->GetBuffer(pCaptureClient, &pData, &numFramesAvailable,
&flags, NULL, NULL);
2013-02-16 03:50:00 +04:00
if (FAILED(hr))
{
2014-09-12 19:38:12 +04:00
WLog_ERR(TAG, "Failed to get buffer");
2013-02-16 03:50:00 +04:00
exit(1);
}
2019-11-06 17:24:51 +03:00
// Here we are writing the audio data
// not sure if this flag is ever set by the system; msdn is not clear about it
2013-02-16 05:39:31 +04:00
if (!(flags & AUDCLNT_BUFFERFLAGS_SILENT))
2019-11-06 17:24:51 +03:00
context->rdpsnd->SendSamples(context->rdpsnd, pData, packetLength,
(UINT16)(GetTickCount() & 0xffff));
2013-02-16 03:50:00 +04:00
hr = pCaptureClient->lpVtbl->ReleaseBuffer(pCaptureClient, numFramesAvailable);
if (FAILED(hr))
{
2014-09-12 19:38:12 +04:00
WLog_ERR(TAG, "Failed to release buffer");
2013-02-16 03:50:00 +04:00
exit(1);
}
hr = pCaptureClient->lpVtbl->GetNextPacketSize(pCaptureClient, &packetLength);
if (FAILED(hr))
{
2014-09-12 19:38:12 +04:00
WLog_ERR(TAG, "Failed to get packet length");
2013-02-16 03:50:00 +04:00
exit(1);
}
}
}
pAudioClient->lpVtbl->Stop(pAudioClient);
if (FAILED(hr))
{
2014-09-12 19:38:12 +04:00
WLog_ERR(TAG, "Failed to stop audio client");
2013-02-16 03:50:00 +04:00
exit(1);
}
CoTaskMemFree(pwfx);
if (pEnumerator != NULL)
pEnumerator->lpVtbl->Release(pEnumerator);
if (pDevice != NULL)
pDevice->lpVtbl->Release(pDevice);
if (pAudioClient != NULL)
pAudioClient->lpVtbl->Release(pAudioClient);
if (pCaptureClient != NULL)
pCaptureClient->lpVtbl->Release(pCaptureClient);
CoUninitialize();
return 0;
}