summaryrefslogtreecommitdiffstats
path: root/server/Windows/wf_directsound.c
blob: 441397a0c486270f25bf0d255e233293c3d32fce (plain)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include "wf_directsound.h"
#include "wf_interface.h"
#include "wf_info.h"
#include "wf_rdpsnd.h"

#include <winpr/windows.h>

#define INITGUID
#include <initguid.h>
#include <objbase.h>

#define CINTERFACE 1
#include <mmsystem.h>
#include <dsound.h>

#include <freerdp/log.h>
#define TAG SERVER_TAG("windows")

IDirectSoundCapture8* cap;
IDirectSoundCaptureBuffer8* capBuf;
DSCBUFFERDESC dscbd;
DWORD lastPos;
wfPeerContext* latestPeer;

int wf_rdpsnd_set_latest_peer(wfPeerContext* peer)
{
	latestPeer = peer;
	return 0;
}

int wf_directsound_activate(RdpsndServerContext* context)
{
	HRESULT hr;
	wfInfo* wfi;
	HANDLE hThread;

	LPDIRECTSOUNDCAPTUREBUFFER pDSCB;

	wfi = wf_info_get_instance();
	if (!wfi)
	{
		WLog_ERR(TAG, "Failed to wfi instance");
		return 1;
	}
	WLog_DBG(TAG, "RDPSND (direct sound) Activated");
	hr = DirectSoundCaptureCreate8(NULL, &cap, NULL);

	if (FAILED(hr))
	{
		WLog_ERR(TAG, "Failed to create sound capture device");
		return 1;
	}

	WLog_INFO(TAG, "Created sound capture device");
	dscbd.dwSize = sizeof(DSCBUFFERDESC);
	dscbd.dwFlags = 0;
	dscbd.dwBufferBytes = wfi->agreed_format->nAvgBytesPerSec;
	dscbd.dwReserved = 0;
	dscbd.lpwfxFormat = wfi->agreed_format;
	dscbd.dwFXCount = 0;
	dscbd.lpDSCFXDesc = NULL;

	hr = cap->lpVtbl->CreateCaptureBuffer(cap, &dscbd, &pDSCB, NULL);

	if (FAILED(hr))
	{
		WLog_ERR(TAG, "Failed to create capture buffer");
	}

	WLog_INFO(TAG, "Created capture buffer");
	hr = pDSCB->lpVtbl->QueryInterface(pDSCB, &IID_IDirectSoundCaptureBuffer8, (LPVOID*)&capBuf);
	if (FAILED(hr))
	{
		WLog_ERR(TAG, "Failed to QI capture buffer");
	}
	WLog_INFO(TAG, "Created IDirectSoundCaptureBuffer8");
	pDSCB->lpVtbl->Release(pDSCB);
	lastPos = 0;

	if (!(hThread = CreateThread(NULL, 0, wf_rdpsnd_directsound_thread, latestPeer, 0, NULL)))
	{
		WLog_ERR(TAG, "Failed to create direct sound thread");
		return 1;
	}
	CloseHandle(hThread);

	return 0;
}

static DWORD WINAPI wf_rdpsnd_directsound_thread(LPVOID lpParam)
{
	HRESULT hr;
	DWORD beg = 0;
	DWORD end = 0;
	DWORD diff, rate;
	wfPeerContext* context;
	wfInfo* wfi;

	VOID* pbCaptureData = NULL;
	DWORD dwCaptureLength = 0;
	VOID* pbCaptureData2 = NULL;
	DWORD dwCaptureLength2 = 0;
	VOID* pbPlayData = NULL;
	DWORD dwReadPos = 0;
	LONG lLockSize = 0;

	wfi = wf_info_get_instance();
	if (!wfi)
	{
		WLog_ERR(TAG, "Failed get instance");
		return 1;
	}

	context = (wfPeerContext*)lpParam;
	rate = 1000 / 24;
	WLog_INFO(TAG, "Trying to start capture");
	hr = capBuf->lpVtbl->Start(capBuf, DSCBSTART_LOOPING);
	if (FAILED(hr))
	{
		WLog_ERR(TAG, "Failed to start capture");
	}
	WLog_INFO(TAG, "Capture started");

	while (1)
	{

		end = GetTickCount();
		diff = end - beg;

		if (diff < rate)
		{
			Sleep(rate - diff);
		}

		beg = GetTickCount();

		if (wf_rdpsnd_lock() > 0)
		{
			// check for main exit condition
			if (wfi->snd_stop == TRUE)
			{
				wf_rdpsnd_unlock();
				break;
			}

			hr = capBuf->lpVtbl->GetCurrentPosition(capBuf, NULL, &dwReadPos);
			if (FAILED(hr))
			{
				WLog_ERR(TAG, "Failed to get read pos");
				wf_rdpsnd_unlock();
				break;
			}

			lLockSize = dwReadPos - lastPos; // dscbd.dwBufferBytes;
			if (lLockSize < 0)
				lLockSize += dscbd.dwBufferBytes;

			// WLog_DBG(TAG, "Last, read, lock = [%"PRIu32", %"PRIu32", %"PRId32"]\n", lastPos,
			// dwReadPos, lLockSize);

			if (lLockSize == 0)
			{
				wf_rdpsnd_unlock();
				continue;
			}

			hr = capBuf->lpVtbl->Lock(capBuf, lastPos, lLockSize, &pbCaptureData, &dwCaptureLength,
			                          &pbCaptureData2, &dwCaptureLength2, 0L);
			if (FAILED(hr))
			{
				WLog_ERR(TAG, "Failed to lock sound capture buffer");
				wf_rdpsnd_unlock();
				break;
			}

			// fwrite(pbCaptureData, 1, dwCaptureLength, pFile);
			// fwrite(pbCaptureData2, 1, dwCaptureLength2, pFile);

			// FIXME: frames = bytes/(bytespersample * channels)

			context->rdpsnd->SendSamples(context->rdpsnd, pbCaptureData, dwCaptureLength / 4,
			                             (UINT16)(beg & 0xffff));
			context->rdpsnd->SendSamples(context->rdpsnd, pbCaptureData2, dwCaptureLength2 / 4,
			                             (UINT16)(beg & 0xffff));

			hr = capBuf->lpVtbl->Unlock(capBuf, pbCaptureData, dwCaptureLength, pbCaptureData2,
			                            dwCaptureLength2);
			if (FAILED(hr))
			{
				WLog_ERR(TAG, "Failed to unlock sound capture buffer");
				wf_rdpsnd_unlock();
				return 0;
			}

			// TODO keep track of location in buffer
			lastPos += dwCaptureLength;
			lastPos %= dscbd.dwBufferBytes;
			lastPos += dwCaptureLength2;
			lastPos %= dscbd.dwBufferBytes;

			wf_rdpsnd_unlock();
		}
	}

	WLog_INFO(TAG, "Trying to stop sound capture");
	hr = capBuf->lpVtbl->Stop(capBuf);
	if (FAILED(hr))
	{
		WLog_ERR(TAG, "Failed to stop capture");
	}

	WLog_INFO(TAG, "Capture stopped");
	capBuf->lpVtbl->Release(capBuf);
	cap->lpVtbl->Release(cap);

	lastPos = 0;

	return 0;
}