Blame server/Windows/wf_directsound.c

Packit Service fa4841
#include "wf_directsound.h"
Packit Service fa4841
#include "wf_interface.h"
Packit Service fa4841
#include "wf_info.h"
Packit Service fa4841
#include "wf_rdpsnd.h"
Packit Service fa4841
Packit Service fa4841
#include <winpr/windows.h>
Packit Service fa4841
Packit Service fa4841
#define INITGUID
Packit Service fa4841
#include <initguid.h>
Packit Service fa4841
#include <objbase.h>
Packit Service fa4841
Packit Service fa4841
#define CINTERFACE 1
Packit Service fa4841
#include <mmsystem.h>
Packit Service fa4841
#include <dsound.h>
Packit Service fa4841
Packit Service fa4841
#include <freerdp/log.h>
Packit Service fa4841
#define TAG SERVER_TAG("windows")
Packit Service fa4841
Packit Service fa4841
IDirectSoundCapture8* cap;
Packit Service fa4841
IDirectSoundCaptureBuffer8* capBuf;
Packit Service fa4841
DSCBUFFERDESC dscbd;
Packit Service fa4841
DWORD lastPos;
Packit Service fa4841
wfPeerContext* latestPeer;
Packit Service fa4841
Packit Service fa4841
int wf_rdpsnd_set_latest_peer(wfPeerContext* peer)
Packit Service fa4841
{
Packit Service fa4841
	latestPeer = peer;
Packit Service fa4841
	return 0;
Packit Service fa4841
}
Packit Service fa4841
Packit Service fa4841
int wf_directsound_activate(RdpsndServerContext* context)
Packit Service fa4841
{
Packit Service fa4841
	HRESULT hr;
Packit Service fa4841
	wfInfo* wfi;
Packit Service fa4841
	HANDLE hThread;
Packit Service b1ea74
Packit Service b1ea74
	LPDIRECTSOUNDCAPTUREBUFFER pDSCB;
Packit Service fa4841
Packit Service fa4841
	wfi = wf_info_get_instance();
Packit Service fa4841
	if (!wfi)
Packit Service fa4841
	{
Packit Service fa4841
		WLog_ERR(TAG, "Failed to wfi instance");
Packit Service fa4841
		return 1;
Packit Service fa4841
	}
Packit Service fa4841
	WLog_DBG(TAG, "RDPSND (direct sound) Activated");
Packit Service fa4841
	hr = DirectSoundCaptureCreate8(NULL, &cap, NULL);
Packit Service fa4841
Packit Service fa4841
	if (FAILED(hr))
Packit Service fa4841
	{
Packit Service fa4841
		WLog_ERR(TAG, "Failed to create sound capture device");
Packit Service fa4841
		return 1;
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	WLog_INFO(TAG, "Created sound capture device");
Packit Service fa4841
	dscbd.dwSize = sizeof(DSCBUFFERDESC);
Packit Service fa4841
	dscbd.dwFlags = 0;
Packit Service fa4841
	dscbd.dwBufferBytes = wfi->agreed_format->nAvgBytesPerSec;
Packit Service fa4841
	dscbd.dwReserved = 0;
Packit Service fa4841
	dscbd.lpwfxFormat = wfi->agreed_format;
Packit Service fa4841
	dscbd.dwFXCount = 0;
Packit Service fa4841
	dscbd.lpDSCFXDesc = NULL;
Packit Service fa4841
Packit Service fa4841
	hr = cap->lpVtbl->CreateCaptureBuffer(cap, &dscbd, &pDSCB, NULL);
Packit Service fa4841
Packit Service fa4841
	if (FAILED(hr))
Packit Service fa4841
	{
Packit Service fa4841
		WLog_ERR(TAG, "Failed to create capture buffer");
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	WLog_INFO(TAG, "Created capture buffer");
Packit Service fa4841
	hr = pDSCB->lpVtbl->QueryInterface(pDSCB, &IID_IDirectSoundCaptureBuffer8, (LPVOID*)&capBuf);
Packit Service fa4841
	if (FAILED(hr))
Packit Service fa4841
	{
Packit Service fa4841
		WLog_ERR(TAG, "Failed to QI capture buffer");
Packit Service fa4841
	}
Packit Service fa4841
	WLog_INFO(TAG, "Created IDirectSoundCaptureBuffer8");
Packit Service fa4841
	pDSCB->lpVtbl->Release(pDSCB);
Packit Service fa4841
	lastPos = 0;
Packit Service fa4841
Packit Service fa4841
	if (!(hThread = CreateThread(NULL, 0, wf_rdpsnd_directsound_thread, latestPeer, 0, NULL)))
Packit Service fa4841
	{
Packit Service fa4841
		WLog_ERR(TAG, "Failed to create direct sound thread");
Packit Service fa4841
		return 1;
Packit Service fa4841
	}
Packit Service fa4841
	CloseHandle(hThread);
Packit Service fa4841
Packit Service fa4841
	return 0;
Packit Service fa4841
}
Packit Service fa4841
Packit Service fa4841
static DWORD WINAPI wf_rdpsnd_directsound_thread(LPVOID lpParam)
Packit Service fa4841
{
Packit Service fa4841
	HRESULT hr;
Packit Service fa4841
	DWORD beg = 0;
Packit Service fa4841
	DWORD end = 0;
Packit Service fa4841
	DWORD diff, rate;
Packit Service fa4841
	wfPeerContext* context;
Packit Service fa4841
	wfInfo* wfi;
Packit Service fa4841
Packit Service b1ea74
	VOID* pbCaptureData = NULL;
Packit Service fa4841
	DWORD dwCaptureLength = 0;
Packit Service fa4841
	VOID* pbCaptureData2 = NULL;
Packit Service fa4841
	DWORD dwCaptureLength2 = 0;
Packit Service b1ea74
	VOID* pbPlayData = NULL;
Packit Service fa4841
	DWORD dwReadPos = 0;
Packit Service fa4841
	LONG lLockSize = 0;
Packit Service fa4841
Packit Service fa4841
	wfi = wf_info_get_instance();
Packit Service fa4841
	if (!wfi)
Packit Service fa4841
	{
Packit Service fa4841
		WLog_ERR(TAG, "Failed get instance");
Packit Service fa4841
		return 1;
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	context = (wfPeerContext*)lpParam;
Packit Service fa4841
	rate = 1000 / 24;
Packit Service fa4841
	WLog_INFO(TAG, "Trying to start capture");
Packit Service fa4841
	hr = capBuf->lpVtbl->Start(capBuf, DSCBSTART_LOOPING);
Packit Service fa4841
	if (FAILED(hr))
Packit Service fa4841
	{
Packit Service fa4841
		WLog_ERR(TAG, "Failed to start capture");
Packit Service fa4841
	}
Packit Service fa4841
	WLog_INFO(TAG, "Capture started");
Packit Service fa4841
Packit Service fa4841
	while (1)
Packit Service fa4841
	{
Packit Service fa4841
Packit Service fa4841
		end = GetTickCount();
Packit Service fa4841
		diff = end - beg;
Packit Service fa4841
Packit Service fa4841
		if (diff < rate)
Packit Service fa4841
		{
Packit Service fa4841
			Sleep(rate - diff);
Packit Service fa4841
		}
Packit Service fa4841
Packit Service fa4841
		beg = GetTickCount();
Packit Service fa4841
Packit Service fa4841
		if (wf_rdpsnd_lock() > 0)
Packit Service fa4841
		{
Packit Service b1ea74
			// check for main exit condition
Packit Service fa4841
			if (wfi->snd_stop == TRUE)
Packit Service fa4841
			{
Packit Service fa4841
				wf_rdpsnd_unlock();
Packit Service fa4841
				break;
Packit Service fa4841
			}
Packit Service fa4841
Packit Service fa4841
			hr = capBuf->lpVtbl->GetCurrentPosition(capBuf, NULL, &dwReadPos);
Packit Service fa4841
			if (FAILED(hr))
Packit Service fa4841
			{
Packit Service fa4841
				WLog_ERR(TAG, "Failed to get read pos");
Packit Service fa4841
				wf_rdpsnd_unlock();
Packit Service fa4841
				break;
Packit Service fa4841
			}
Packit Service fa4841
Packit Service b1ea74
			lLockSize = dwReadPos - lastPos; // dscbd.dwBufferBytes;
Packit Service b1ea74
			if (lLockSize < 0)
Packit Service b1ea74
				lLockSize += dscbd.dwBufferBytes;
Packit Service fa4841
Packit Service b1ea74
			// WLog_DBG(TAG, "Last, read, lock = [%"PRIu32", %"PRIu32", %"PRId32"]\n", lastPos,
Packit Service b1ea74
			// dwReadPos, lLockSize);
Packit Service fa4841
Packit Service fa4841
			if (lLockSize == 0)
Packit Service fa4841
			{
Packit Service fa4841
				wf_rdpsnd_unlock();
Packit Service fa4841
				continue;
Packit Service fa4841
			}
Packit Service fa4841
Packit Service b1ea74
			hr = capBuf->lpVtbl->Lock(capBuf, lastPos, lLockSize, &pbCaptureData, &dwCaptureLength,
Packit Service b1ea74
			                          &pbCaptureData2, &dwCaptureLength2, 0L);
Packit Service fa4841
			if (FAILED(hr))
Packit Service fa4841
			{
Packit Service fa4841
				WLog_ERR(TAG, "Failed to lock sound capture buffer");
Packit Service fa4841
				wf_rdpsnd_unlock();
Packit Service fa4841
				break;
Packit Service fa4841
			}
Packit Service fa4841
Packit Service b1ea74
			// fwrite(pbCaptureData, 1, dwCaptureLength, pFile);
Packit Service b1ea74
			// fwrite(pbCaptureData2, 1, dwCaptureLength2, pFile);
Packit Service fa4841
Packit Service b1ea74
			// FIXME: frames = bytes/(bytespersample * channels)
Packit Service fa4841
Packit Service b1ea74
			context->rdpsnd->SendSamples(context->rdpsnd, pbCaptureData, dwCaptureLength / 4,
Packit Service b1ea74
			                             (UINT16)(beg & 0xffff));
Packit Service b1ea74
			context->rdpsnd->SendSamples(context->rdpsnd, pbCaptureData2, dwCaptureLength2 / 4,
Packit Service b1ea74
			                             (UINT16)(beg & 0xffff));
Packit Service fa4841
Packit Service b1ea74
			hr = capBuf->lpVtbl->Unlock(capBuf, pbCaptureData, dwCaptureLength, pbCaptureData2,
Packit Service b1ea74
			                            dwCaptureLength2);
Packit Service fa4841
			if (FAILED(hr))
Packit Service fa4841
			{
Packit Service fa4841
				WLog_ERR(TAG, "Failed to unlock sound capture buffer");
Packit Service fa4841
				wf_rdpsnd_unlock();
Packit Service fa4841
				return 0;
Packit Service fa4841
			}
Packit Service fa4841
Packit Service b1ea74
			// TODO keep track of location in buffer
Packit Service fa4841
			lastPos += dwCaptureLength;
Packit Service fa4841
			lastPos %= dscbd.dwBufferBytes;
Packit Service fa4841
			lastPos += dwCaptureLength2;
Packit Service fa4841
			lastPos %= dscbd.dwBufferBytes;
Packit Service fa4841
Packit Service fa4841
			wf_rdpsnd_unlock();
Packit Service fa4841
		}
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	WLog_INFO(TAG, "Trying to stop sound capture");
Packit Service fa4841
	hr = capBuf->lpVtbl->Stop(capBuf);
Packit Service fa4841
	if (FAILED(hr))
Packit Service fa4841
	{
Packit Service fa4841
		WLog_ERR(TAG, "Failed to stop capture");
Packit Service fa4841
	}
Packit Service fa4841
Packit Service fa4841
	WLog_INFO(TAG, "Capture stopped");
Packit Service fa4841
	capBuf->lpVtbl->Release(capBuf);
Packit Service fa4841
	cap->lpVtbl->Release(cap);
Packit Service fa4841
Packit Service fa4841
	lastPos = 0;
Packit Service fa4841
Packit Service fa4841
	return 0;
Packit Service fa4841
}