/src/FreeRDP/channels/rdpsnd/client/rdpsnd_main.c
Line | Count | Source |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * Audio Output Virtual Channel |
4 | | * |
5 | | * Copyright 2009-2011 Jay Sorg |
6 | | * Copyright 2010-2011 Vic Lee |
7 | | * Copyright 2012-2013 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
8 | | * Copyright 2015 Thincast Technologies GmbH |
9 | | * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com> |
10 | | * Copyright 2016 David PHAM-VAN <d.phamvan@inuvika.com> |
11 | | * |
12 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
13 | | * you may not use this file except in compliance with the License. |
14 | | * You may obtain a copy of the License at |
15 | | * |
16 | | * http://www.apache.org/licenses/LICENSE-2.0 |
17 | | * |
18 | | * Unless required by applicable law or agreed to in writing, software |
19 | | * distributed under the License is distributed on an "AS IS" BASIS, |
20 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
21 | | * See the License for the specific language governing permissions and |
22 | | * limitations under the License. |
23 | | */ |
24 | | |
25 | | #include <freerdp/config.h> |
26 | | |
27 | | #ifndef _WIN32 |
28 | | #include <sys/time.h> |
29 | | #include <signal.h> |
30 | | #endif |
31 | | |
32 | | #include <stdio.h> |
33 | | #include <stdlib.h> |
34 | | #include <string.h> |
35 | | #include <errno.h> |
36 | | |
37 | | #include <winpr/crt.h> |
38 | | #include <winpr/assert.h> |
39 | | #include <winpr/wlog.h> |
40 | | #include <winpr/stream.h> |
41 | | #include <winpr/cmdline.h> |
42 | | #include <winpr/sysinfo.h> |
43 | | #include <winpr/collections.h> |
44 | | |
45 | | #include <freerdp/types.h> |
46 | | #include <freerdp/addin.h> |
47 | | #include <freerdp/freerdp.h> |
48 | | #include <freerdp/codec/dsp.h> |
49 | | #include <freerdp/client/channels.h> |
50 | | |
51 | | #include "rdpsnd_common.h" |
52 | | #include "rdpsnd_main.h" |
53 | | |
54 | | struct rdpsnd_plugin |
55 | | { |
56 | | IWTSPlugin iface; |
57 | | IWTSListener* listener; |
58 | | GENERIC_LISTENER_CALLBACK* listener_callback; |
59 | | |
60 | | CHANNEL_DEF channelDef; |
61 | | CHANNEL_ENTRY_POINTS_FREERDP_EX channelEntryPoints; |
62 | | |
63 | | wStreamPool* pool; |
64 | | wStream* data_in; |
65 | | |
66 | | void* InitHandle; |
67 | | DWORD OpenHandle; |
68 | | |
69 | | wLog* log; |
70 | | |
71 | | BYTE cBlockNo; |
72 | | UINT16 wQualityMode; |
73 | | UINT16 wCurrentFormatNo; |
74 | | |
75 | | AUDIO_FORMAT* ServerFormats; |
76 | | UINT16 NumberOfServerFormats; |
77 | | |
78 | | AUDIO_FORMAT* ClientFormats; |
79 | | UINT16 NumberOfClientFormats; |
80 | | |
81 | | BOOL attached; |
82 | | BOOL dynamic; |
83 | | |
84 | | BOOL expectingWave; |
85 | | BYTE waveData[4]; |
86 | | UINT16 waveDataSize; |
87 | | UINT16 wTimeStamp; |
88 | | UINT64 wArrivalTime; |
89 | | |
90 | | UINT32 latency; |
91 | | BOOL isOpen; |
92 | | AUDIO_FORMAT* fixed_format; |
93 | | |
94 | | UINT32 startPlayTime; |
95 | | size_t totalPlaySize; |
96 | | |
97 | | char* subsystem; |
98 | | char* device_name; |
99 | | |
100 | | /* Device plugin */ |
101 | | rdpsndDevicePlugin* device; |
102 | | rdpContext* rdpcontext; |
103 | | |
104 | | FREERDP_DSP_CONTEXT* dsp_context; |
105 | | |
106 | | HANDLE thread; |
107 | | wMessageQueue* queue; |
108 | | CRITICAL_SECTION asyncLock; |
109 | | BOOL initialized; |
110 | | |
111 | | UINT16 wVersion; |
112 | | UINT32 volume; |
113 | | BOOL applyVolume; |
114 | | |
115 | | size_t references; |
116 | | BOOL OnOpenCalled; |
117 | | BOOL async; |
118 | | BOOL firstFlagReceived; |
119 | | UINT32 totalLength; |
120 | | BOOL connected; |
121 | | UINT64 badWaves; /* waves dropped because they did not decode */ |
122 | | }; |
123 | | |
124 | | WINPR_ATTR_NODISCARD |
125 | | static DWORD WINAPI play_thread(LPVOID arg); |
126 | | |
127 | | WINPR_ATTR_NODISCARD |
128 | | static const char* rdpsnd_is_dyn_str(BOOL dynamic) |
129 | 0 | { |
130 | 0 | if (dynamic) |
131 | 0 | return "[dynamic]"; |
132 | 0 | return "[static]"; |
133 | 0 | } |
134 | | |
135 | | static void rdpsnd_virtual_channel_event_terminated(rdpsndPlugin* rdpsnd); |
136 | | |
137 | | /** |
138 | | * Function description |
139 | | * |
140 | | * @return 0 on success, otherwise a Win32 error code |
141 | | */ |
142 | | WINPR_ATTR_NODISCARD |
143 | | static UINT rdpsnd_virtual_channel_write(rdpsndPlugin* rdpsnd, wStream* s); |
144 | | |
145 | | /** |
146 | | * Function description |
147 | | * |
148 | | * @return 0 on success, otherwise a Win32 error code |
149 | | */ |
150 | | WINPR_ATTR_NODISCARD |
151 | | static UINT rdpsnd_send_quality_mode_pdu(rdpsndPlugin* rdpsnd) |
152 | 0 | { |
153 | 0 | wStream* pdu = nullptr; |
154 | 0 | WINPR_ASSERT(rdpsnd); |
155 | 0 | pdu = Stream_New(nullptr, 8); |
156 | |
|
157 | 0 | if (!pdu) |
158 | 0 | { |
159 | 0 | WLog_ERR(TAG, "%s Stream_New failed!", rdpsnd_is_dyn_str(rdpsnd->dynamic)); |
160 | 0 | return CHANNEL_RC_NO_MEMORY; |
161 | 0 | } |
162 | | |
163 | 0 | Stream_Write_UINT8(pdu, SNDC_QUALITYMODE); /* msgType */ |
164 | 0 | Stream_Write_UINT8(pdu, 0); /* bPad */ |
165 | 0 | Stream_Write_UINT16(pdu, 4); /* BodySize */ |
166 | 0 | Stream_Write_UINT16(pdu, rdpsnd->wQualityMode); /* wQualityMode */ |
167 | 0 | Stream_Write_UINT16(pdu, 0); /* Reserved */ |
168 | 0 | WLog_Print(rdpsnd->log, WLOG_DEBUG, "%s QualityMode: %" PRIu16 "", |
169 | 0 | rdpsnd_is_dyn_str(rdpsnd->dynamic), rdpsnd->wQualityMode); |
170 | 0 | return rdpsnd_virtual_channel_write(rdpsnd, pdu); |
171 | 0 | } |
172 | | |
173 | | static void rdpsnd_select_supported_audio_formats(rdpsndPlugin* rdpsnd) |
174 | 0 | { |
175 | 0 | WINPR_ASSERT(rdpsnd); |
176 | 0 | audio_formats_free(rdpsnd->ClientFormats, rdpsnd->NumberOfClientFormats); |
177 | 0 | rdpsnd->NumberOfClientFormats = 0; |
178 | 0 | rdpsnd->ClientFormats = nullptr; |
179 | |
|
180 | 0 | if (!rdpsnd->NumberOfServerFormats) |
181 | 0 | return; |
182 | | |
183 | 0 | rdpsnd->ClientFormats = audio_formats_new(rdpsnd->NumberOfServerFormats); |
184 | |
|
185 | 0 | if (!rdpsnd->ClientFormats || !rdpsnd->device) |
186 | 0 | return; |
187 | | |
188 | 0 | for (UINT16 index = 0; index < rdpsnd->NumberOfServerFormats; index++) |
189 | 0 | { |
190 | 0 | const AUDIO_FORMAT* serverFormat = &rdpsnd->ServerFormats[index]; |
191 | |
|
192 | 0 | if (!audio_format_compatible(rdpsnd->fixed_format, serverFormat)) |
193 | 0 | continue; |
194 | | |
195 | 0 | WINPR_ASSERT(rdpsnd->device->FormatSupported); |
196 | 0 | if (freerdp_dsp_supports_format(serverFormat, FALSE) || |
197 | 0 | rdpsnd->device->FormatSupported(rdpsnd->device, serverFormat)) |
198 | 0 | { |
199 | 0 | AUDIO_FORMAT* clientFormat = &rdpsnd->ClientFormats[rdpsnd->NumberOfClientFormats++]; |
200 | 0 | audio_format_copy(serverFormat, clientFormat); |
201 | 0 | } |
202 | 0 | } |
203 | 0 | } |
204 | | |
205 | | /** |
206 | | * Function description |
207 | | * |
208 | | * @return 0 on success, otherwise a Win32 error code |
209 | | */ |
210 | | WINPR_ATTR_NODISCARD |
211 | | static UINT rdpsnd_send_client_audio_formats(rdpsndPlugin* rdpsnd) |
212 | 0 | { |
213 | 0 | wStream* pdu = nullptr; |
214 | 0 | size_t length = 0; |
215 | 0 | UINT32 dwVolume = 0; |
216 | 0 | UINT16 wNumberOfFormats = 0; |
217 | 0 | WINPR_ASSERT(rdpsnd); |
218 | |
|
219 | 0 | if (!rdpsnd->device || (!rdpsnd->dynamic && (rdpsnd->OpenHandle == 0))) |
220 | 0 | return CHANNEL_RC_INITIALIZATION_ERROR; |
221 | | |
222 | 0 | dwVolume = IFCALLRESULT(0, rdpsnd->device->GetVolume, rdpsnd->device); |
223 | 0 | wNumberOfFormats = rdpsnd->NumberOfClientFormats; |
224 | 0 | length = 4 + 20; |
225 | |
|
226 | 0 | for (UINT16 index = 0; index < wNumberOfFormats; index++) |
227 | 0 | length += (18 + rdpsnd->ClientFormats[index].cbSize); |
228 | | |
229 | | /* BodySize is a UINT16 field, so the whole PDU must fit in UINT16_MAX. A server can inflate |
230 | | * the client format list through large per-format cbSize values; without this bound the |
231 | | * accumulator wrapped and Stream_New() below was undersized. Mirrors the server side check in |
232 | | * rdpsnd_server_send_formats(). */ |
233 | 0 | if (length > UINT16_MAX) |
234 | 0 | return ERROR_INVALID_DATA; |
235 | | |
236 | 0 | pdu = Stream_New(nullptr, length); |
237 | |
|
238 | 0 | if (!pdu) |
239 | 0 | { |
240 | 0 | WLog_ERR(TAG, "%s Stream_New failed!", rdpsnd_is_dyn_str(rdpsnd->dynamic)); |
241 | 0 | return CHANNEL_RC_NO_MEMORY; |
242 | 0 | } |
243 | | |
244 | 0 | Stream_Write_UINT8(pdu, SNDC_FORMATS); /* msgType */ |
245 | 0 | Stream_Write_UINT8(pdu, 0); /* bPad */ |
246 | 0 | Stream_Write_UINT16(pdu, (UINT16)(length - 4)); /* BodySize */ |
247 | 0 | Stream_Write_UINT32(pdu, TSSNDCAPS_ALIVE | TSSNDCAPS_VOLUME); /* dwFlags */ |
248 | 0 | Stream_Write_UINT32(pdu, dwVolume); /* dwVolume */ |
249 | 0 | Stream_Write_UINT32(pdu, 0); /* dwPitch */ |
250 | 0 | Stream_Write_UINT16(pdu, 0); /* wDGramPort */ |
251 | 0 | Stream_Write_UINT16(pdu, wNumberOfFormats); /* wNumberOfFormats */ |
252 | 0 | Stream_Write_UINT8(pdu, 0); /* cLastBlockConfirmed */ |
253 | 0 | Stream_Write_UINT16(pdu, CHANNEL_VERSION_WIN_MAX); /* wVersion */ |
254 | 0 | Stream_Write_UINT8(pdu, 0); /* bPad */ |
255 | |
|
256 | 0 | for (UINT16 index = 0; index < wNumberOfFormats; index++) |
257 | 0 | { |
258 | 0 | const AUDIO_FORMAT* clientFormat = &rdpsnd->ClientFormats[index]; |
259 | |
|
260 | 0 | if (!audio_format_write(pdu, clientFormat)) |
261 | 0 | { |
262 | 0 | Stream_Free(pdu, TRUE); |
263 | 0 | return ERROR_INTERNAL_ERROR; |
264 | 0 | } |
265 | 0 | } |
266 | | |
267 | 0 | WLog_Print(rdpsnd->log, WLOG_DEBUG, "%s Client Audio Formats", |
268 | 0 | rdpsnd_is_dyn_str(rdpsnd->dynamic)); |
269 | 0 | return rdpsnd_virtual_channel_write(rdpsnd, pdu); |
270 | 0 | } |
271 | | |
272 | | /** |
273 | | * Function description |
274 | | * |
275 | | * @return 0 on success, otherwise a Win32 error code |
276 | | */ |
277 | | WINPR_ATTR_NODISCARD |
278 | | static UINT rdpsnd_recv_server_audio_formats_pdu(rdpsndPlugin* rdpsnd, wStream* s) |
279 | 0 | { |
280 | 0 | UINT16 wNumberOfFormats = 0; |
281 | 0 | UINT ret = ERROR_BAD_LENGTH; |
282 | |
|
283 | 0 | WINPR_ASSERT(rdpsnd); |
284 | 0 | audio_formats_free(rdpsnd->ServerFormats, rdpsnd->NumberOfServerFormats); |
285 | 0 | rdpsnd->NumberOfServerFormats = 0; |
286 | 0 | rdpsnd->ServerFormats = nullptr; |
287 | |
|
288 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 30)) |
289 | 0 | return ERROR_BAD_LENGTH; |
290 | | |
291 | | /* http://msdn.microsoft.com/en-us/library/cc240956.aspx */ |
292 | 0 | Stream_Seek_UINT32(s); /* dwFlags */ |
293 | 0 | Stream_Seek_UINT32(s); /* dwVolume */ |
294 | 0 | Stream_Seek_UINT32(s); /* dwPitch */ |
295 | 0 | Stream_Seek_UINT16(s); /* wDGramPort */ |
296 | 0 | Stream_Read_UINT16(s, wNumberOfFormats); |
297 | 0 | Stream_Read_UINT8(s, rdpsnd->cBlockNo); /* cLastBlockConfirmed */ |
298 | 0 | Stream_Read_UINT16(s, rdpsnd->wVersion); /* wVersion */ |
299 | 0 | Stream_Seek_UINT8(s); /* bPad */ |
300 | |
|
301 | 0 | if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, wNumberOfFormats, 14ull)) |
302 | 0 | return ERROR_BAD_LENGTH; |
303 | | |
304 | 0 | if (wNumberOfFormats > 0) |
305 | 0 | { |
306 | 0 | rdpsnd->ServerFormats = audio_formats_new(wNumberOfFormats); |
307 | |
|
308 | 0 | if (!rdpsnd->ServerFormats) |
309 | 0 | return CHANNEL_RC_NO_MEMORY; |
310 | | |
311 | 0 | rdpsnd->NumberOfServerFormats = wNumberOfFormats; |
312 | |
|
313 | 0 | for (UINT16 index = 0; index < wNumberOfFormats; index++) |
314 | 0 | { |
315 | 0 | AUDIO_FORMAT* format = &rdpsnd->ServerFormats[index]; |
316 | |
|
317 | 0 | if (!audio_format_read(s, format)) |
318 | 0 | goto out_fail; |
319 | 0 | } |
320 | 0 | } |
321 | | |
322 | 0 | WINPR_ASSERT(rdpsnd->device); |
323 | 0 | ret = IFCALLRESULT(CHANNEL_RC_OK, rdpsnd->device->ServerFormatAnnounce, rdpsnd->device, |
324 | 0 | rdpsnd->ServerFormats, rdpsnd->NumberOfServerFormats); |
325 | 0 | if (ret != CHANNEL_RC_OK) |
326 | 0 | goto out_fail; |
327 | | |
328 | 0 | rdpsnd_select_supported_audio_formats(rdpsnd); |
329 | 0 | WLog_Print(rdpsnd->log, WLOG_DEBUG, "%s Server Audio Formats", |
330 | 0 | rdpsnd_is_dyn_str(rdpsnd->dynamic)); |
331 | 0 | ret = rdpsnd_send_client_audio_formats(rdpsnd); |
332 | |
|
333 | 0 | if (ret == CHANNEL_RC_OK) |
334 | 0 | { |
335 | 0 | if (rdpsnd->wVersion >= CHANNEL_VERSION_WIN_7) |
336 | 0 | ret = rdpsnd_send_quality_mode_pdu(rdpsnd); |
337 | 0 | } |
338 | |
|
339 | 0 | return ret; |
340 | 0 | out_fail: |
341 | 0 | audio_formats_free(rdpsnd->ServerFormats, rdpsnd->NumberOfServerFormats); |
342 | 0 | rdpsnd->ServerFormats = nullptr; |
343 | 0 | rdpsnd->NumberOfServerFormats = 0; |
344 | 0 | return ret; |
345 | 0 | } |
346 | | |
347 | | /** |
348 | | * Function description |
349 | | * |
350 | | * @return 0 on success, otherwise a Win32 error code |
351 | | */ |
352 | | WINPR_ATTR_NODISCARD |
353 | | static UINT rdpsnd_send_training_confirm_pdu(rdpsndPlugin* rdpsnd, UINT16 wTimeStamp, |
354 | | UINT16 wPackSize) |
355 | 0 | { |
356 | 0 | wStream* pdu = nullptr; |
357 | 0 | WINPR_ASSERT(rdpsnd); |
358 | 0 | pdu = Stream_New(nullptr, 8); |
359 | |
|
360 | 0 | if (!pdu) |
361 | 0 | { |
362 | 0 | WLog_ERR(TAG, "%s Stream_New failed!", rdpsnd_is_dyn_str(rdpsnd->dynamic)); |
363 | 0 | return CHANNEL_RC_NO_MEMORY; |
364 | 0 | } |
365 | | |
366 | 0 | Stream_Write_UINT8(pdu, SNDC_TRAINING); /* msgType */ |
367 | 0 | Stream_Write_UINT8(pdu, 0); /* bPad */ |
368 | 0 | Stream_Write_UINT16(pdu, 4); /* BodySize */ |
369 | 0 | Stream_Write_UINT16(pdu, wTimeStamp); |
370 | 0 | Stream_Write_UINT16(pdu, wPackSize); |
371 | 0 | WLog_Print(rdpsnd->log, WLOG_DEBUG, |
372 | 0 | "%s Training Response: wTimeStamp: %" PRIu16 " wPackSize: %" PRIu16 "", |
373 | 0 | rdpsnd_is_dyn_str(rdpsnd->dynamic), wTimeStamp, wPackSize); |
374 | 0 | return rdpsnd_virtual_channel_write(rdpsnd, pdu); |
375 | 0 | } |
376 | | |
377 | | /** |
378 | | * Function description |
379 | | * |
380 | | * @return 0 on success, otherwise a Win32 error code |
381 | | */ |
382 | | WINPR_ATTR_NODISCARD |
383 | | static UINT rdpsnd_recv_training_pdu(rdpsndPlugin* rdpsnd, wStream* s) |
384 | 0 | { |
385 | 0 | UINT16 wTimeStamp = 0; |
386 | 0 | UINT16 wPackSize = 0; |
387 | 0 | WINPR_ASSERT(rdpsnd); |
388 | 0 | WINPR_ASSERT(s); |
389 | |
|
390 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 4)) |
391 | 0 | return ERROR_BAD_LENGTH; |
392 | | |
393 | 0 | Stream_Read_UINT16(s, wTimeStamp); |
394 | 0 | Stream_Read_UINT16(s, wPackSize); |
395 | 0 | WLog_Print(rdpsnd->log, WLOG_DEBUG, |
396 | 0 | "%s Training Request: wTimeStamp: %" PRIu16 " wPackSize: %" PRIu16 "", |
397 | 0 | rdpsnd_is_dyn_str(rdpsnd->dynamic), wTimeStamp, wPackSize); |
398 | 0 | return rdpsnd_send_training_confirm_pdu(rdpsnd, wTimeStamp, wPackSize); |
399 | 0 | } |
400 | | |
401 | | WINPR_ATTR_NODISCARD |
402 | | static BOOL rdpsnd_apply_volume(rdpsndPlugin* rdpsnd) |
403 | 0 | { |
404 | 0 | WINPR_ASSERT(rdpsnd); |
405 | |
|
406 | 0 | if (rdpsnd->isOpen && rdpsnd->applyVolume && rdpsnd->device) |
407 | 0 | { |
408 | 0 | BOOL rc = IFCALLRESULT(TRUE, rdpsnd->device->SetVolume, rdpsnd->device, rdpsnd->volume); |
409 | 0 | if (!rc) |
410 | 0 | return FALSE; |
411 | 0 | rdpsnd->applyVolume = FALSE; |
412 | 0 | } |
413 | 0 | return TRUE; |
414 | 0 | } |
415 | | |
416 | | WINPR_ATTR_NODISCARD |
417 | | static BOOL rdpsnd_ensure_device_is_open(rdpsndPlugin* rdpsnd, UINT16 wFormatNo, |
418 | | const AUDIO_FORMAT* format) |
419 | 0 | { |
420 | 0 | if (!rdpsnd) |
421 | 0 | return FALSE; |
422 | 0 | WINPR_ASSERT(format); |
423 | |
|
424 | 0 | if (!rdpsnd->isOpen || (wFormatNo != rdpsnd->wCurrentFormatNo)) |
425 | 0 | { |
426 | 0 | BOOL rc = 0; |
427 | 0 | BOOL supported = 0; |
428 | 0 | AUDIO_FORMAT deviceFormat = *format; |
429 | |
|
430 | 0 | IFCALL(rdpsnd->device->Close, rdpsnd->device); |
431 | 0 | supported = IFCALLRESULT(FALSE, rdpsnd->device->FormatSupported, rdpsnd->device, format); |
432 | |
|
433 | 0 | if (!supported) |
434 | 0 | { |
435 | 0 | if (!IFCALLRESULT(FALSE, rdpsnd->device->DefaultFormat, rdpsnd->device, format, |
436 | 0 | &deviceFormat)) |
437 | 0 | { |
438 | 0 | deviceFormat.wFormatTag = WAVE_FORMAT_PCM; |
439 | 0 | deviceFormat.wBitsPerSample = 16; |
440 | 0 | deviceFormat.cbSize = 0; |
441 | 0 | } |
442 | 0 | } |
443 | |
|
444 | 0 | WLog_Print(rdpsnd->log, WLOG_DEBUG, "%s Opening device with format %s [backend %s]", |
445 | 0 | rdpsnd_is_dyn_str(rdpsnd->dynamic), |
446 | 0 | audio_format_get_tag_string(format->wFormatTag), |
447 | 0 | audio_format_get_tag_string(deviceFormat.wFormatTag)); |
448 | 0 | rc = IFCALLRESULT(FALSE, rdpsnd->device->Open, rdpsnd->device, &deviceFormat, |
449 | 0 | rdpsnd->latency); |
450 | |
|
451 | 0 | if (!rc) |
452 | 0 | return FALSE; |
453 | | |
454 | 0 | if (!supported) |
455 | 0 | { |
456 | 0 | if (!freerdp_dsp_context_reset(rdpsnd->dsp_context, format, 0u)) |
457 | 0 | return FALSE; |
458 | 0 | } |
459 | | |
460 | 0 | rdpsnd->isOpen = TRUE; |
461 | 0 | rdpsnd->wCurrentFormatNo = wFormatNo; |
462 | 0 | rdpsnd->startPlayTime = 0; |
463 | 0 | rdpsnd->totalPlaySize = 0; |
464 | 0 | } |
465 | | |
466 | 0 | return rdpsnd_apply_volume(rdpsnd); |
467 | 0 | } |
468 | | |
469 | | /** |
470 | | * Function description |
471 | | * |
472 | | * @return 0 on success, otherwise a Win32 error code |
473 | | */ |
474 | | WINPR_ATTR_NODISCARD |
475 | | static UINT rdpsnd_recv_wave_info_pdu(rdpsndPlugin* rdpsnd, wStream* s, UINT16 BodySize) |
476 | 0 | { |
477 | 0 | UINT16 wFormatNo = 0; |
478 | 0 | const AUDIO_FORMAT* format = nullptr; |
479 | 0 | WINPR_ASSERT(rdpsnd); |
480 | 0 | WINPR_ASSERT(s); |
481 | |
|
482 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 12) || (BodySize < 8)) |
483 | 0 | return ERROR_BAD_LENGTH; |
484 | | |
485 | 0 | rdpsnd->wArrivalTime = GetTickCount64(); |
486 | 0 | Stream_Read_UINT16(s, rdpsnd->wTimeStamp); |
487 | 0 | Stream_Read_UINT16(s, wFormatNo); |
488 | |
|
489 | 0 | if (wFormatNo >= rdpsnd->NumberOfClientFormats) |
490 | 0 | return ERROR_INVALID_DATA; |
491 | | |
492 | 0 | Stream_Read_UINT8(s, rdpsnd->cBlockNo); |
493 | 0 | Stream_Seek(s, 3); /* bPad */ |
494 | 0 | Stream_Read(s, rdpsnd->waveData, 4); |
495 | 0 | rdpsnd->waveDataSize = BodySize - 8; |
496 | 0 | format = &rdpsnd->ClientFormats[wFormatNo]; |
497 | 0 | WLog_Print(rdpsnd->log, WLOG_DEBUG, |
498 | 0 | "%s WaveInfo: cBlockNo: %" PRIu8 " wFormatNo: %" PRIu16 " [%s]", |
499 | 0 | rdpsnd_is_dyn_str(rdpsnd->dynamic), rdpsnd->cBlockNo, wFormatNo, |
500 | 0 | audio_format_get_tag_string(format->wFormatTag)); |
501 | |
|
502 | 0 | if (!rdpsnd_ensure_device_is_open(rdpsnd, wFormatNo, format)) |
503 | 0 | return ERROR_INTERNAL_ERROR; |
504 | | |
505 | 0 | rdpsnd->expectingWave = TRUE; |
506 | 0 | return CHANNEL_RC_OK; |
507 | 0 | } |
508 | | |
509 | | /** |
510 | | * Function description |
511 | | * |
512 | | * @return 0 on success, otherwise a Win32 error code |
513 | | */ |
514 | | WINPR_ATTR_NODISCARD |
515 | | static UINT rdpsnd_send_wave_confirm_pdu(rdpsndPlugin* rdpsnd, UINT16 wTimeStamp, |
516 | | BYTE cConfirmedBlockNo) |
517 | 0 | { |
518 | 0 | wStream* pdu = nullptr; |
519 | 0 | WINPR_ASSERT(rdpsnd); |
520 | 0 | pdu = Stream_New(nullptr, 8); |
521 | |
|
522 | 0 | if (!pdu) |
523 | 0 | { |
524 | 0 | WLog_ERR(TAG, "%s Stream_New failed!", rdpsnd_is_dyn_str(rdpsnd->dynamic)); |
525 | 0 | return CHANNEL_RC_NO_MEMORY; |
526 | 0 | } |
527 | | |
528 | 0 | Stream_Write_UINT8(pdu, SNDC_WAVECONFIRM); |
529 | 0 | Stream_Write_UINT8(pdu, 0); |
530 | 0 | Stream_Write_UINT16(pdu, 4); |
531 | 0 | Stream_Write_UINT16(pdu, wTimeStamp); |
532 | 0 | Stream_Write_UINT8(pdu, cConfirmedBlockNo); /* cConfirmedBlockNo */ |
533 | 0 | Stream_Write_UINT8(pdu, 0); /* bPad */ |
534 | 0 | return rdpsnd_virtual_channel_write(rdpsnd, pdu); |
535 | 0 | } |
536 | | |
537 | | /* Audio the client may queue beyond the current sample before it drops |
538 | | * samples as an overrun. The check is meant to stop old servers from |
539 | | * queueing seconds of audio; jitter and bursts are tens of milliseconds. */ |
540 | | #define RDPSND_OVERRUN_SLACK_MS 200 |
541 | | |
542 | | WINPR_ATTR_NODISCARD |
543 | | static BOOL rdpsnd_detect_overrun(rdpsndPlugin* rdpsnd, const AUDIO_FORMAT* format, size_t size) |
544 | 0 | { |
545 | 0 | UINT32 bpf = 0; |
546 | 0 | UINT32 now = 0; |
547 | 0 | UINT32 duration = 0; |
548 | 0 | UINT32 totalDuration = 0; |
549 | 0 | UINT32 remainingDuration = 0; |
550 | 0 | UINT32 maxDuration = 0; |
551 | |
|
552 | 0 | if (!rdpsnd || !format) |
553 | 0 | return FALSE; |
554 | | |
555 | | /* Older windows RDP servers do not limit the send buffer, which can |
556 | | * cause quite a large amount of sound data buffered client side. |
557 | | * If e.g. sound is paused server side the client will keep playing |
558 | | * for a long time instead of pausing playback. |
559 | | * |
560 | | * To avoid this we check: |
561 | | * |
562 | | * 1. Is the sound sample received from a known format these servers |
563 | | * support |
564 | | * 2. If it is calculate the size of the client side sound buffer |
565 | | * 3. If the buffer is too large silently drop the sample which will |
566 | | * trigger a retransmit later on. |
567 | | * |
568 | | * This check must only be applied to these known formats, because |
569 | | * with newer and other formats the sample size can not be calculated |
570 | | * without decompressing the sample first. |
571 | | */ |
572 | 0 | switch (format->wFormatTag) |
573 | 0 | { |
574 | 0 | case WAVE_FORMAT_PCM: |
575 | 0 | case WAVE_FORMAT_DVI_ADPCM: |
576 | 0 | case WAVE_FORMAT_ADPCM: |
577 | 0 | case WAVE_FORMAT_ALAW: |
578 | 0 | case WAVE_FORMAT_MULAW: |
579 | 0 | break; |
580 | 0 | case WAVE_FORMAT_MSG723: |
581 | 0 | case WAVE_FORMAT_GSM610: |
582 | 0 | case WAVE_FORMAT_AAC_MS: |
583 | 0 | default: |
584 | 0 | return FALSE; |
585 | 0 | } |
586 | | |
587 | 0 | audio_format_print(WLog_Get(TAG), WLOG_DEBUG, format); |
588 | 0 | bpf = format->nChannels * format->wBitsPerSample * format->nSamplesPerSec / 8; |
589 | 0 | if (bpf == 0) |
590 | 0 | return FALSE; |
591 | | |
592 | 0 | duration = (UINT32)(1000 * size / bpf); |
593 | 0 | totalDuration = (UINT32)(1000 * rdpsnd->totalPlaySize / bpf); |
594 | 0 | now = GetTickCountPrecise(); |
595 | 0 | if (rdpsnd->startPlayTime == 0) |
596 | 0 | { |
597 | 0 | rdpsnd->startPlayTime = now; |
598 | 0 | rdpsnd->totalPlaySize = size; |
599 | 0 | return FALSE; |
600 | 0 | } |
601 | 0 | else if (now - rdpsnd->startPlayTime > totalDuration + 10) |
602 | 0 | { |
603 | | /* Buffer underrun */ |
604 | 0 | WLog_Print(rdpsnd->log, WLOG_DEBUG, "%s Buffer underrun by %u ms", |
605 | 0 | rdpsnd_is_dyn_str(rdpsnd->dynamic), |
606 | 0 | (UINT)(now - rdpsnd->startPlayTime - totalDuration)); |
607 | 0 | rdpsnd->startPlayTime = now; |
608 | 0 | rdpsnd->totalPlaySize = size; |
609 | 0 | return FALSE; |
610 | 0 | } |
611 | 0 | else |
612 | 0 | { |
613 | | /* Calculate remaining duration to be played */ |
614 | 0 | remainingDuration = totalDuration - (now - rdpsnd->startPlayTime); |
615 | | |
616 | | /* Maximum allow duration calculation: at least RDPSND_OVERRUN_SLACK_MS |
617 | | * on top, so samples that arrive in a burst (a late one followed by |
618 | | * an on-time one, or a server sending several at once) are kept */ |
619 | 0 | maxDuration = duration * 2 + MAX(rdpsnd->latency, RDPSND_OVERRUN_SLACK_MS); |
620 | |
|
621 | 0 | if (remainingDuration + duration > maxDuration) |
622 | 0 | { |
623 | 0 | WLog_Print(rdpsnd->log, WLOG_DEBUG, "%s Buffer overrun pending %u ms dropping %u ms", |
624 | 0 | rdpsnd_is_dyn_str(rdpsnd->dynamic), remainingDuration, duration); |
625 | 0 | return TRUE; |
626 | 0 | } |
627 | | |
628 | 0 | rdpsnd->totalPlaySize += size; |
629 | 0 | return FALSE; |
630 | 0 | } |
631 | 0 | } |
632 | | |
633 | | WINPR_ATTR_NODISCARD |
634 | | static UINT rdpsnd_treat_wave(rdpsndPlugin* rdpsnd, wStream* s, size_t size) |
635 | 0 | { |
636 | 0 | AUDIO_FORMAT* format = nullptr; |
637 | 0 | UINT64 end = 0; |
638 | 0 | UINT64 diffMS = 0; |
639 | 0 | UINT64 ts = 0; |
640 | 0 | UINT latency = 0; |
641 | 0 | UINT error = 0; |
642 | |
|
643 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, size)) |
644 | 0 | return ERROR_BAD_LENGTH; |
645 | | |
646 | 0 | if (rdpsnd->wCurrentFormatNo >= rdpsnd->NumberOfClientFormats) |
647 | 0 | return ERROR_INTERNAL_ERROR; |
648 | | |
649 | | /* |
650 | | * Send the first WaveConfirm PDU. The server side uses this to determine the |
651 | | * network latency. |
652 | | * See also [MS-RDPEA] 2.2.3.8 Wave Confirm PDU |
653 | | */ |
654 | 0 | error = rdpsnd_send_wave_confirm_pdu(rdpsnd, rdpsnd->wTimeStamp, rdpsnd->cBlockNo); |
655 | 0 | if (error) |
656 | 0 | return error; |
657 | | |
658 | 0 | const BYTE* data = Stream_ConstPointer(s); |
659 | 0 | format = &rdpsnd->ClientFormats[rdpsnd->wCurrentFormatNo]; |
660 | 0 | WLog_Print(rdpsnd->log, WLOG_DEBUG, |
661 | 0 | "%s Wave: cBlockNo: %" PRIu8 " wTimeStamp: %" PRIu16 ", size: %" PRIuz, |
662 | 0 | rdpsnd_is_dyn_str(rdpsnd->dynamic), rdpsnd->cBlockNo, rdpsnd->wTimeStamp, size); |
663 | |
|
664 | 0 | if (rdpsnd->device && rdpsnd->attached && !rdpsnd_detect_overrun(rdpsnd, format, size)) |
665 | 0 | { |
666 | 0 | wStream* pcmData = StreamPool_Take(rdpsnd->pool, 4096); |
667 | |
|
668 | 0 | if (rdpsnd->device->FormatSupported(rdpsnd->device, format)) |
669 | 0 | { |
670 | 0 | if (rdpsnd->device->PlayEx) |
671 | 0 | latency = rdpsnd->device->PlayEx(rdpsnd->device, format, data, size); |
672 | 0 | else |
673 | 0 | latency = IFCALLRESULT(0, rdpsnd->device->Play, rdpsnd->device, data, size); |
674 | 0 | } |
675 | 0 | else if (freerdp_dsp_decode(rdpsnd->dsp_context, format, data, size, pcmData)) |
676 | 0 | { |
677 | 0 | Stream_SealLength(pcmData); |
678 | | |
679 | | /* Decoders with priming delay (e.g. AAC) may legitimately produce no PCM yet */ |
680 | 0 | if (Stream_Length(pcmData) == 0) |
681 | 0 | latency = 0; |
682 | 0 | else if (rdpsnd->device->PlayEx) |
683 | 0 | latency = rdpsnd->device->PlayEx(rdpsnd->device, format, Stream_Buffer(pcmData), |
684 | 0 | Stream_Length(pcmData)); |
685 | 0 | else |
686 | 0 | latency = IFCALLRESULT(0, rdpsnd->device->Play, rdpsnd->device, |
687 | 0 | Stream_Buffer(pcmData), Stream_Length(pcmData)); |
688 | 0 | } |
689 | 0 | else |
690 | 0 | { |
691 | | /* Dropped, and confirmed like the others: an error here would end the |
692 | | * thread that plays the sound, and with it the sound of the session */ |
693 | 0 | rdpsnd->badWaves++; |
694 | 0 | WLog_Print(rdpsnd->log, (rdpsnd->badWaves == 1) ? WLOG_WARN : WLOG_DEBUG, |
695 | 0 | "%s dropped a %s wave of %" PRIuz " bytes that does not decode (%" PRIu64 |
696 | 0 | " so far)", |
697 | 0 | rdpsnd_is_dyn_str(rdpsnd->dynamic), |
698 | 0 | audio_format_get_tag_string(format->wFormatTag), size, rdpsnd->badWaves); |
699 | 0 | } |
700 | |
|
701 | 0 | Stream_Release(pcmData); |
702 | 0 | } |
703 | |
|
704 | 0 | end = GetTickCount64(); |
705 | 0 | diffMS = end - rdpsnd->wArrivalTime + latency; |
706 | 0 | ts = (rdpsnd->wTimeStamp + diffMS) % UINT16_MAX; |
707 | | |
708 | | /* |
709 | | * Send the second WaveConfirm PDU. With the first WaveConfirm PDU, |
710 | | * the server side uses this second WaveConfirm PDU to determine the actual |
711 | | * render latency. |
712 | | */ |
713 | 0 | return rdpsnd_send_wave_confirm_pdu(rdpsnd, (UINT16)ts, rdpsnd->cBlockNo); |
714 | 0 | } |
715 | | |
716 | | /** |
717 | | * Function description |
718 | | * |
719 | | * @return 0 on success, otherwise a Win32 error code |
720 | | */ |
721 | | WINPR_ATTR_NODISCARD |
722 | | static UINT rdpsnd_recv_wave_pdu(rdpsndPlugin* rdpsnd, wStream* s) |
723 | 0 | { |
724 | 0 | rdpsnd->expectingWave = FALSE; |
725 | | |
726 | | /** |
727 | | * The Wave PDU is a special case: it is always sent after a Wave Info PDU, |
728 | | * and we do not process its header. Instead, the header is pad that needs |
729 | | * to be filled with the first four bytes of the audio sample data sent as |
730 | | * part of the preceding Wave Info PDU. |
731 | | */ |
732 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 4)) |
733 | 0 | return ERROR_INVALID_DATA; |
734 | | |
735 | 0 | CopyMemory(Stream_Buffer(s), rdpsnd->waveData, 4); |
736 | 0 | return rdpsnd_treat_wave(rdpsnd, s, rdpsnd->waveDataSize); |
737 | 0 | } |
738 | | |
739 | | WINPR_ATTR_NODISCARD |
740 | | static UINT rdpsnd_recv_wave2_pdu(rdpsndPlugin* rdpsnd, wStream* s, UINT16 BodySize) |
741 | 0 | { |
742 | 0 | UINT16 wFormatNo = 0; |
743 | 0 | AUDIO_FORMAT* format = nullptr; |
744 | 0 | UINT32 dwAudioTimeStamp = 0; |
745 | |
|
746 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 12) || (BodySize < 12)) |
747 | 0 | return ERROR_BAD_LENGTH; |
748 | | |
749 | 0 | Stream_Read_UINT16(s, rdpsnd->wTimeStamp); |
750 | 0 | Stream_Read_UINT16(s, wFormatNo); |
751 | 0 | Stream_Read_UINT8(s, rdpsnd->cBlockNo); |
752 | 0 | Stream_Seek(s, 3); /* bPad */ |
753 | 0 | Stream_Read_UINT32(s, dwAudioTimeStamp); |
754 | 0 | if (wFormatNo >= rdpsnd->NumberOfClientFormats) |
755 | 0 | return ERROR_INVALID_DATA; |
756 | 0 | format = &rdpsnd->ClientFormats[wFormatNo]; |
757 | 0 | rdpsnd->waveDataSize = BodySize - 12; |
758 | 0 | rdpsnd->wArrivalTime = GetTickCount64(); |
759 | 0 | WLog_Print(rdpsnd->log, WLOG_DEBUG, |
760 | 0 | "%s Wave2PDU: cBlockNo: %" PRIu8 " wFormatNo: %" PRIu16 |
761 | 0 | " [%s] , align=%hu wTimeStamp=0x%04" PRIx16 ", dwAudioTimeStamp=0x%08" PRIx32, |
762 | 0 | rdpsnd_is_dyn_str(rdpsnd->dynamic), rdpsnd->cBlockNo, wFormatNo, |
763 | 0 | audio_format_get_tag_string(format->wFormatTag), format->nBlockAlign, |
764 | 0 | rdpsnd->wTimeStamp, dwAudioTimeStamp); |
765 | |
|
766 | 0 | if (!rdpsnd_ensure_device_is_open(rdpsnd, wFormatNo, format)) |
767 | 0 | return ERROR_INTERNAL_ERROR; |
768 | | |
769 | 0 | return rdpsnd_treat_wave(rdpsnd, s, rdpsnd->waveDataSize); |
770 | 0 | } |
771 | | |
772 | | static void rdpsnd_recv_close_pdu(rdpsndPlugin* rdpsnd) |
773 | 0 | { |
774 | 0 | if (rdpsnd->isOpen) |
775 | 0 | { |
776 | 0 | WLog_Print(rdpsnd->log, WLOG_DEBUG, "%s Closing device", |
777 | 0 | rdpsnd_is_dyn_str(rdpsnd->dynamic)); |
778 | 0 | } |
779 | 0 | else |
780 | 0 | WLog_Print(rdpsnd->log, WLOG_DEBUG, "%s Device already closed", |
781 | 0 | rdpsnd_is_dyn_str(rdpsnd->dynamic)); |
782 | 0 | } |
783 | | |
784 | | /** |
785 | | * Function description |
786 | | * |
787 | | * @return 0 on success, otherwise a Win32 error code |
788 | | */ |
789 | | WINPR_ATTR_NODISCARD |
790 | | static UINT rdpsnd_recv_volume_pdu(rdpsndPlugin* rdpsnd, wStream* s) |
791 | 0 | { |
792 | 0 | BOOL rc = TRUE; |
793 | 0 | UINT32 dwVolume = 0; |
794 | |
|
795 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 4)) |
796 | 0 | return ERROR_BAD_LENGTH; |
797 | | |
798 | 0 | Stream_Read_UINT32(s, dwVolume); |
799 | 0 | WLog_Print(rdpsnd->log, WLOG_DEBUG, "%s Volume: 0x%08" PRIX32 "", |
800 | 0 | rdpsnd_is_dyn_str(rdpsnd->dynamic), dwVolume); |
801 | |
|
802 | 0 | rdpsnd->volume = dwVolume; |
803 | 0 | rdpsnd->applyVolume = TRUE; |
804 | 0 | rc = rdpsnd_apply_volume(rdpsnd); |
805 | |
|
806 | 0 | if (!rc) |
807 | 0 | { |
808 | 0 | WLog_ERR(TAG, "%s error setting volume", rdpsnd_is_dyn_str(rdpsnd->dynamic)); |
809 | 0 | return CHANNEL_RC_INITIALIZATION_ERROR; |
810 | 0 | } |
811 | | |
812 | 0 | return CHANNEL_RC_OK; |
813 | 0 | } |
814 | | |
815 | | /** |
816 | | * Function description |
817 | | * |
818 | | * @return 0 on success, otherwise a Win32 error code |
819 | | */ |
820 | | WINPR_ATTR_NODISCARD |
821 | | static UINT rdpsnd_recv_pdu(rdpsndPlugin* rdpsnd, wStream* s) |
822 | 0 | { |
823 | 0 | BYTE msgType = 0; |
824 | 0 | UINT16 BodySize = 0; |
825 | 0 | UINT status = CHANNEL_RC_OK; |
826 | |
|
827 | 0 | if (rdpsnd->expectingWave) |
828 | 0 | { |
829 | 0 | status = rdpsnd_recv_wave_pdu(rdpsnd, s); |
830 | 0 | goto out; |
831 | 0 | } |
832 | | |
833 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 4)) |
834 | 0 | { |
835 | 0 | status = ERROR_BAD_LENGTH; |
836 | 0 | goto out; |
837 | 0 | } |
838 | | |
839 | 0 | Stream_Read_UINT8(s, msgType); /* msgType */ |
840 | 0 | Stream_Seek_UINT8(s); /* bPad */ |
841 | 0 | Stream_Read_UINT16(s, BodySize); |
842 | |
|
843 | 0 | switch (msgType) |
844 | 0 | { |
845 | 0 | case SNDC_FORMATS: |
846 | 0 | status = rdpsnd_recv_server_audio_formats_pdu(rdpsnd, s); |
847 | 0 | break; |
848 | | |
849 | 0 | case SNDC_TRAINING: |
850 | 0 | status = rdpsnd_recv_training_pdu(rdpsnd, s); |
851 | 0 | break; |
852 | | |
853 | 0 | case SNDC_WAVE: |
854 | 0 | status = rdpsnd_recv_wave_info_pdu(rdpsnd, s, BodySize); |
855 | 0 | break; |
856 | | |
857 | 0 | case SNDC_CLOSE: |
858 | 0 | rdpsnd_recv_close_pdu(rdpsnd); |
859 | 0 | break; |
860 | | |
861 | 0 | case SNDC_SETVOLUME: |
862 | 0 | status = rdpsnd_recv_volume_pdu(rdpsnd, s); |
863 | 0 | break; |
864 | | |
865 | 0 | case SNDC_WAVE2: |
866 | 0 | status = rdpsnd_recv_wave2_pdu(rdpsnd, s, BodySize); |
867 | 0 | break; |
868 | | |
869 | 0 | default: |
870 | 0 | WLog_ERR(TAG, "%s unknown msgType %" PRIu8 "", rdpsnd_is_dyn_str(rdpsnd->dynamic), |
871 | 0 | msgType); |
872 | 0 | break; |
873 | 0 | } |
874 | | |
875 | 0 | out: |
876 | 0 | Stream_Release(s); |
877 | 0 | return status; |
878 | 0 | } |
879 | | |
880 | | static void rdpsnd_register_device_plugin(rdpsndPlugin* rdpsnd, rdpsndDevicePlugin* device) |
881 | 0 | { |
882 | 0 | if (rdpsnd->device) |
883 | 0 | { |
884 | 0 | WLog_ERR(TAG, "%s existing device, abort.", rdpsnd_is_dyn_str(FALSE)); |
885 | 0 | return; |
886 | 0 | } |
887 | | |
888 | 0 | rdpsnd->device = device; |
889 | 0 | device->rdpsnd = rdpsnd; |
890 | 0 | } |
891 | | |
892 | | /** |
893 | | * Function description |
894 | | * |
895 | | * @return 0 on success, otherwise a Win32 error code |
896 | | */ |
897 | | WINPR_ATTR_NODISCARD |
898 | | static UINT rdpsnd_load_device_plugin(rdpsndPlugin* rdpsnd, const char* name, |
899 | | const ADDIN_ARGV* args) |
900 | 0 | { |
901 | 0 | FREERDP_RDPSND_DEVICE_ENTRY_POINTS entryPoints = WINPR_C_ARRAY_INIT; |
902 | 0 | UINT error = 0; |
903 | 0 | DWORD flags = FREERDP_ADDIN_CHANNEL_STATIC | FREERDP_ADDIN_CHANNEL_ENTRYEX; |
904 | 0 | if (rdpsnd->dynamic) |
905 | 0 | flags = FREERDP_ADDIN_CHANNEL_DYNAMIC; |
906 | 0 | PVIRTUALCHANNELENTRY pvce = |
907 | 0 | freerdp_load_channel_addin_entry(RDPSND_CHANNEL_NAME, name, nullptr, flags); |
908 | 0 | PFREERDP_RDPSND_DEVICE_ENTRY entry = WINPR_FUNC_PTR_CAST(pvce, PFREERDP_RDPSND_DEVICE_ENTRY); |
909 | |
|
910 | 0 | if (!entry) |
911 | 0 | return ERROR_INTERNAL_ERROR; |
912 | | |
913 | 0 | entryPoints.rdpsnd = rdpsnd; |
914 | 0 | entryPoints.pRegisterRdpsndDevice = rdpsnd_register_device_plugin; |
915 | 0 | entryPoints.args = args; |
916 | |
|
917 | 0 | error = entry(&entryPoints); |
918 | 0 | if (error) |
919 | 0 | WLog_WARN(TAG, "%s %s entry returns error %" PRIu32 "", rdpsnd_is_dyn_str(rdpsnd->dynamic), |
920 | 0 | name, error); |
921 | 0 | else |
922 | 0 | WLog_INFO(TAG, "%s Loaded %s backend for rdpsnd", rdpsnd_is_dyn_str(rdpsnd->dynamic), name); |
923 | |
|
924 | 0 | return error; |
925 | 0 | } |
926 | | |
927 | | WINPR_ATTR_NODISCARD |
928 | | static BOOL rdpsnd_set_subsystem(rdpsndPlugin* rdpsnd, const char* subsystem) |
929 | 0 | { |
930 | 0 | free(rdpsnd->subsystem); |
931 | 0 | rdpsnd->subsystem = _strdup(subsystem); |
932 | 0 | return (rdpsnd->subsystem != nullptr); |
933 | 0 | } |
934 | | |
935 | | WINPR_ATTR_NODISCARD |
936 | | static BOOL rdpsnd_set_device_name(rdpsndPlugin* rdpsnd, const char* device_name) |
937 | 0 | { |
938 | 0 | free(rdpsnd->device_name); |
939 | 0 | rdpsnd->device_name = _strdup(device_name); |
940 | 0 | return (rdpsnd->device_name != nullptr); |
941 | 0 | } |
942 | | |
943 | | /* format:<format> accepts a numeric wFormatTag (e.g. 0x704F) or the name |
944 | | * audio_format_get_tag_string() returns for it, with or without the |
945 | | * WAVE_FORMAT_ prefix (e.g. opus, alaw, aac_ms). |
946 | | * Anything else is rejected instead of silently becoming WAVE_FORMAT_UNKNOWN, |
947 | | * which would allow any format. */ |
948 | | WINPR_ATTR_NODISCARD |
949 | | static BOOL rdpsnd_parse_format_tag(const char* value, UINT16* tag) |
950 | 0 | { |
951 | 0 | static const char prefix[] = "WAVE_FORMAT_"; |
952 | 0 | const size_t prefixlen = strnlen(prefix, sizeof(prefix)); |
953 | |
|
954 | 0 | WINPR_ASSERT(value); |
955 | 0 | WINPR_ASSERT(tag); |
956 | |
|
957 | 0 | char* end = nullptr; |
958 | 0 | errno = 0; |
959 | 0 | const unsigned long val = strtoul(value, &end, 0); |
960 | 0 | if ((errno == 0) && (end != value) && (*end == '\0')) |
961 | 0 | { |
962 | 0 | if (val > UINT16_MAX) |
963 | 0 | return FALSE; |
964 | 0 | *tag = (UINT16)val; |
965 | 0 | return TRUE; |
966 | 0 | } |
967 | | |
968 | 0 | const char* name = value; |
969 | 0 | if (_strnicmp(name, prefix, prefixlen) == 0) |
970 | 0 | name += prefixlen; |
971 | |
|
972 | 0 | for (UINT32 x = 0; x <= UINT16_MAX; x++) |
973 | 0 | { |
974 | 0 | const char* str = audio_format_get_tag_string((UINT16)x); |
975 | 0 | if (strncmp(str, prefix, prefixlen) == 0) |
976 | 0 | str += prefixlen; |
977 | 0 | if (_stricmp(name, str) == 0) |
978 | 0 | { |
979 | 0 | *tag = (UINT16)x; |
980 | 0 | return TRUE; |
981 | 0 | } |
982 | 0 | } |
983 | | |
984 | 0 | return FALSE; |
985 | 0 | } |
986 | | |
987 | | /** |
988 | | * Function description |
989 | | * |
990 | | * @return 0 on success, otherwise a Win32 error code |
991 | | */ |
992 | | WINPR_ATTR_NODISCARD |
993 | | static UINT rdpsnd_process_addin_args(rdpsndPlugin* rdpsnd, const ADDIN_ARGV* args) |
994 | 0 | { |
995 | 0 | int status = 0; |
996 | 0 | DWORD flags = 0; |
997 | 0 | const COMMAND_LINE_ARGUMENT_A* arg = nullptr; |
998 | 0 | COMMAND_LINE_ARGUMENT_A rdpsnd_args[] = { |
999 | 0 | { "sys", COMMAND_LINE_VALUE_REQUIRED, "<subsystem>", nullptr, nullptr, -1, nullptr, |
1000 | 0 | "subsystem" }, |
1001 | 0 | { "dev", COMMAND_LINE_VALUE_REQUIRED, "<device>", nullptr, nullptr, -1, nullptr, "device" }, |
1002 | 0 | { "format", COMMAND_LINE_VALUE_REQUIRED, "<format>", nullptr, nullptr, -1, nullptr, |
1003 | 0 | "format" }, |
1004 | 0 | { "rate", COMMAND_LINE_VALUE_REQUIRED, "<rate>", nullptr, nullptr, -1, nullptr, "rate" }, |
1005 | 0 | { "channel", COMMAND_LINE_VALUE_REQUIRED, "<channel>", nullptr, nullptr, -1, nullptr, |
1006 | 0 | "channel" }, |
1007 | 0 | { "latency", COMMAND_LINE_VALUE_REQUIRED, "<latency>", nullptr, nullptr, -1, nullptr, |
1008 | 0 | "latency" }, |
1009 | 0 | { "quality", COMMAND_LINE_VALUE_REQUIRED, "<quality mode>", nullptr, nullptr, -1, nullptr, |
1010 | 0 | "quality mode" }, |
1011 | 0 | { nullptr, 0, nullptr, nullptr, nullptr, -1, nullptr, nullptr } |
1012 | 0 | }; |
1013 | 0 | rdpsnd->wQualityMode = HIGH_QUALITY; /* default quality mode */ |
1014 | |
|
1015 | 0 | if (args->argc > 1) |
1016 | 0 | { |
1017 | 0 | flags = COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON; |
1018 | 0 | status = CommandLineParseArgumentsA(args->argc, args->argv, rdpsnd_args, flags, rdpsnd, |
1019 | 0 | nullptr, nullptr); |
1020 | |
|
1021 | 0 | if (status < 0) |
1022 | 0 | return CHANNEL_RC_INITIALIZATION_ERROR; |
1023 | | |
1024 | 0 | arg = rdpsnd_args; |
1025 | 0 | errno = 0; |
1026 | |
|
1027 | 0 | do |
1028 | 0 | { |
1029 | 0 | if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT)) |
1030 | 0 | continue; |
1031 | | |
1032 | 0 | CommandLineSwitchStart(arg) CommandLineSwitchCase(arg, "sys") |
1033 | 0 | { |
1034 | 0 | if (!rdpsnd_set_subsystem(rdpsnd, arg->Value)) |
1035 | 0 | return CHANNEL_RC_NO_MEMORY; |
1036 | 0 | } |
1037 | 0 | CommandLineSwitchCase(arg, "dev") |
1038 | 0 | { |
1039 | 0 | if (!rdpsnd_set_device_name(rdpsnd, arg->Value)) |
1040 | 0 | return CHANNEL_RC_NO_MEMORY; |
1041 | 0 | } |
1042 | 0 | CommandLineSwitchCase(arg, "format") |
1043 | 0 | { |
1044 | 0 | UINT16 tag = WAVE_FORMAT_UNKNOWN; |
1045 | |
|
1046 | 0 | if (!rdpsnd_parse_format_tag(arg->Value, &tag)) |
1047 | 0 | { |
1048 | 0 | WLog_Print(rdpsnd->log, WLOG_ERROR, |
1049 | 0 | "invalid format '%s', expected a format tag (e.g. 0x704F) or " |
1050 | 0 | "a WAVE_FORMAT_ name (e.g. opus, pcm, alaw, aac_ms)", |
1051 | 0 | arg->Value); |
1052 | 0 | return CHANNEL_RC_INITIALIZATION_ERROR; |
1053 | 0 | } |
1054 | | |
1055 | 0 | rdpsnd->fixed_format->wFormatTag = tag; |
1056 | 0 | } |
1057 | 0 | CommandLineSwitchCase(arg, "rate") |
1058 | 0 | { |
1059 | 0 | unsigned long val = strtoul(arg->Value, nullptr, 0); |
1060 | |
|
1061 | 0 | if ((errno != 0) || (val > UINT32_MAX)) |
1062 | 0 | return CHANNEL_RC_INITIALIZATION_ERROR; |
1063 | | |
1064 | 0 | rdpsnd->fixed_format->nSamplesPerSec = (UINT32)val; |
1065 | 0 | } |
1066 | 0 | CommandLineSwitchCase(arg, "channel") |
1067 | 0 | { |
1068 | 0 | unsigned long val = strtoul(arg->Value, nullptr, 0); |
1069 | |
|
1070 | 0 | if ((errno != 0) || (val > UINT16_MAX)) |
1071 | 0 | return CHANNEL_RC_INITIALIZATION_ERROR; |
1072 | | |
1073 | 0 | rdpsnd->fixed_format->nChannels = (UINT16)val; |
1074 | 0 | } |
1075 | 0 | CommandLineSwitchCase(arg, "latency") |
1076 | 0 | { |
1077 | 0 | unsigned long val = strtoul(arg->Value, nullptr, 0); |
1078 | |
|
1079 | 0 | if ((errno != 0) || (val > UINT32_MAX)) |
1080 | 0 | return CHANNEL_RC_INITIALIZATION_ERROR; |
1081 | | |
1082 | 0 | rdpsnd->latency = (UINT32)val; |
1083 | 0 | } |
1084 | 0 | CommandLineSwitchCase(arg, "quality") |
1085 | 0 | { |
1086 | 0 | long wQualityMode = DYNAMIC_QUALITY; |
1087 | |
|
1088 | 0 | if (_stricmp(arg->Value, "dynamic") == 0) |
1089 | 0 | wQualityMode = DYNAMIC_QUALITY; |
1090 | 0 | else if (_stricmp(arg->Value, "medium") == 0) |
1091 | 0 | wQualityMode = MEDIUM_QUALITY; |
1092 | 0 | else if (_stricmp(arg->Value, "high") == 0) |
1093 | 0 | wQualityMode = HIGH_QUALITY; |
1094 | 0 | else |
1095 | 0 | { |
1096 | 0 | wQualityMode = strtol(arg->Value, nullptr, 0); |
1097 | |
|
1098 | 0 | if (errno != 0) |
1099 | 0 | return CHANNEL_RC_INITIALIZATION_ERROR; |
1100 | 0 | } |
1101 | | |
1102 | 0 | if ((wQualityMode < 0) || (wQualityMode > 2)) |
1103 | 0 | wQualityMode = DYNAMIC_QUALITY; |
1104 | |
|
1105 | 0 | rdpsnd->wQualityMode = (UINT16)wQualityMode; |
1106 | 0 | } |
1107 | 0 | CommandLineSwitchDefault(arg) |
1108 | 0 | { |
1109 | 0 | } |
1110 | 0 | CommandLineSwitchEnd(arg) |
1111 | 0 | } while ((arg = CommandLineFindNextArgumentA(arg)) != nullptr); |
1112 | 0 | } |
1113 | | |
1114 | 0 | return CHANNEL_RC_OK; |
1115 | 0 | } |
1116 | | |
1117 | | /** |
1118 | | * Function description |
1119 | | * |
1120 | | * @return 0 on success, otherwise a Win32 error code |
1121 | | */ |
1122 | | WINPR_ATTR_NODISCARD |
1123 | | static UINT rdpsnd_process_connect(rdpsndPlugin* rdpsnd) |
1124 | 0 | { |
1125 | 0 | const struct |
1126 | 0 | { |
1127 | 0 | const char* subsystem; |
1128 | 0 | const char* device; |
1129 | 0 | } backends[] = { |
1130 | | #if defined(WITH_IOSAUDIO) |
1131 | | { "ios", "" }, |
1132 | | #endif |
1133 | | #if defined(WITH_AAUDIO) |
1134 | | { "aaudio", "" }, |
1135 | | #endif |
1136 | | #if defined(WITH_OPENSLES) |
1137 | | { "opensles", "" }, |
1138 | | #endif |
1139 | | #if defined(WITH_PULSE) |
1140 | | { "pulse", "" }, |
1141 | | #endif |
1142 | | #if defined(WITH_ALSA) |
1143 | | { "alsa", "default" }, |
1144 | | #endif |
1145 | | #if defined(WITH_OSS) |
1146 | | { "oss", "" }, |
1147 | | #endif |
1148 | | #if defined(WITH_MACAUDIO) |
1149 | | { "mac", "default" }, |
1150 | | #endif |
1151 | | #if defined(WITH_WINMM) |
1152 | | { "winmm", "" }, |
1153 | | #endif |
1154 | | #if defined(WITH_SNDIO) |
1155 | | { "sndio", "" }, |
1156 | | #endif |
1157 | | #if defined(__EMSCRIPTEN__) |
1158 | | { "emscripten", "" }, |
1159 | | #endif |
1160 | 0 | { "fake", "" } |
1161 | 0 | }; |
1162 | 0 | const ADDIN_ARGV* args = nullptr; |
1163 | 0 | UINT status = ERROR_INTERNAL_ERROR; |
1164 | 0 | WINPR_ASSERT(rdpsnd); |
1165 | 0 | rdpsnd->latency = 0; |
1166 | 0 | args = (const ADDIN_ARGV*)rdpsnd->channelEntryPoints.pExtendedData; |
1167 | |
|
1168 | 0 | if (args) |
1169 | 0 | { |
1170 | 0 | status = rdpsnd_process_addin_args(rdpsnd, args); |
1171 | |
|
1172 | 0 | if (status != CHANNEL_RC_OK) |
1173 | 0 | return status; |
1174 | 0 | } |
1175 | | |
1176 | 0 | if (rdpsnd->subsystem) |
1177 | 0 | { |
1178 | 0 | if ((status = rdpsnd_load_device_plugin(rdpsnd, rdpsnd->subsystem, args))) |
1179 | 0 | { |
1180 | 0 | WLog_ERR(TAG, |
1181 | 0 | "%s Unable to load sound playback subsystem %s because of error %" PRIu32 "", |
1182 | 0 | rdpsnd_is_dyn_str(rdpsnd->dynamic), rdpsnd->subsystem, status); |
1183 | 0 | return status; |
1184 | 0 | } |
1185 | 0 | } |
1186 | 0 | else |
1187 | 0 | { |
1188 | 0 | for (size_t x = 0; x < ARRAYSIZE(backends); x++) |
1189 | 0 | { |
1190 | 0 | const char* subsystem_name = backends[x].subsystem; |
1191 | 0 | const char* device_name = backends[x].device; |
1192 | |
|
1193 | 0 | if ((status = rdpsnd_load_device_plugin(rdpsnd, subsystem_name, args))) |
1194 | 0 | WLog_WARN(TAG, |
1195 | 0 | "%s Unable to load sound playback subsystem %s because of error %" PRIu32 |
1196 | 0 | "", |
1197 | 0 | rdpsnd_is_dyn_str(rdpsnd->dynamic), subsystem_name, status); |
1198 | |
|
1199 | 0 | if (!rdpsnd->device) |
1200 | 0 | continue; |
1201 | | |
1202 | 0 | if (!rdpsnd_set_subsystem(rdpsnd, subsystem_name) || |
1203 | 0 | !rdpsnd_set_device_name(rdpsnd, device_name)) |
1204 | 0 | return CHANNEL_RC_NO_MEMORY; |
1205 | | |
1206 | 0 | break; |
1207 | 0 | } |
1208 | | |
1209 | 0 | if (!rdpsnd->device || status) |
1210 | 0 | return CHANNEL_RC_INITIALIZATION_ERROR; |
1211 | 0 | } |
1212 | | |
1213 | 0 | return CHANNEL_RC_OK; |
1214 | 0 | } |
1215 | | |
1216 | | /** |
1217 | | * Function description |
1218 | | * |
1219 | | * @return 0 on success, otherwise a Win32 error code |
1220 | | */ |
1221 | | UINT rdpsnd_virtual_channel_write(rdpsndPlugin* rdpsnd, wStream* s) |
1222 | 0 | { |
1223 | 0 | UINT status = CHANNEL_RC_BAD_INIT_HANDLE; |
1224 | |
|
1225 | 0 | if (rdpsnd) |
1226 | 0 | { |
1227 | 0 | if (rdpsnd->dynamic) |
1228 | 0 | { |
1229 | 0 | if (rdpsnd->listener_callback && rdpsnd->connected) |
1230 | 0 | { |
1231 | 0 | IWTSVirtualChannel* channel = rdpsnd->listener_callback->channel_callback->channel; |
1232 | 0 | status = |
1233 | 0 | channel->Write(channel, (UINT32)Stream_Length(s), Stream_Buffer(s), nullptr); |
1234 | 0 | } |
1235 | 0 | Stream_Free(s, TRUE); |
1236 | 0 | } |
1237 | 0 | else |
1238 | 0 | { |
1239 | 0 | status = rdpsnd->channelEntryPoints.pVirtualChannelWriteEx( |
1240 | 0 | rdpsnd->InitHandle, rdpsnd->OpenHandle, Stream_Buffer(s), |
1241 | 0 | (UINT32)Stream_GetPosition(s), s); |
1242 | |
|
1243 | 0 | if (status != CHANNEL_RC_OK) |
1244 | 0 | { |
1245 | 0 | Stream_Free(s, TRUE); |
1246 | 0 | WLog_ERR(TAG, "%s pVirtualChannelWriteEx failed with %s [%08" PRIX32 "]", |
1247 | 0 | rdpsnd_is_dyn_str(FALSE), WTSErrorToString(status), status); |
1248 | 0 | } |
1249 | 0 | } |
1250 | 0 | } |
1251 | |
|
1252 | 0 | return status; |
1253 | 0 | } |
1254 | | |
1255 | | /** |
1256 | | * Function description |
1257 | | * |
1258 | | * @return 0 on success, otherwise a Win32 error code |
1259 | | */ |
1260 | | WINPR_ATTR_NODISCARD |
1261 | | static UINT rdpsnd_virtual_channel_event_data_received(rdpsndPlugin* plugin, void* pData, |
1262 | | UINT32 dataLength, UINT32 totalLength, |
1263 | | UINT32 dataFlags) |
1264 | 0 | { |
1265 | 0 | if ((dataFlags & CHANNEL_FLAG_SUSPEND) || (dataFlags & CHANNEL_FLAG_RESUME)) |
1266 | 0 | return CHANNEL_RC_OK; |
1267 | | |
1268 | 0 | if (dataFlags & CHANNEL_FLAG_FIRST) |
1269 | 0 | { |
1270 | 0 | if (plugin->firstFlagReceived) |
1271 | 0 | return ERROR_INVALID_DATA; |
1272 | 0 | plugin->firstFlagReceived = TRUE; |
1273 | |
|
1274 | 0 | if (!plugin->data_in) |
1275 | 0 | plugin->data_in = StreamPool_Take(plugin->pool, dataLength); |
1276 | |
|
1277 | 0 | Stream_ResetPosition(plugin->data_in); |
1278 | 0 | plugin->totalLength = totalLength; |
1279 | 0 | } |
1280 | | |
1281 | 0 | if (!plugin->data_in) |
1282 | 0 | return ERROR_INVALID_DATA; |
1283 | | |
1284 | 0 | if (!Stream_EnsureRemainingCapacity(plugin->data_in, dataLength)) |
1285 | 0 | return CHANNEL_RC_NO_MEMORY; |
1286 | | |
1287 | 0 | Stream_Write(plugin->data_in, pData, dataLength); |
1288 | |
|
1289 | 0 | if ((Stream_GetPosition(plugin->data_in) > totalLength) || (totalLength != plugin->totalLength)) |
1290 | 0 | return ERROR_INVALID_DATA; |
1291 | | |
1292 | 0 | if (dataFlags & CHANNEL_FLAG_LAST) |
1293 | 0 | { |
1294 | 0 | if (!plugin->firstFlagReceived) |
1295 | 0 | return ERROR_INVALID_DATA; |
1296 | 0 | if (Stream_GetPosition(plugin->data_in) != totalLength) |
1297 | 0 | return ERROR_INVALID_DATA; |
1298 | 0 | plugin->firstFlagReceived = FALSE; |
1299 | |
|
1300 | 0 | Stream_SealLength(plugin->data_in); |
1301 | 0 | Stream_ResetPosition(plugin->data_in); |
1302 | 0 | plugin->totalLength = 0; |
1303 | |
|
1304 | 0 | if (plugin->async) |
1305 | 0 | { |
1306 | 0 | if (!MessageQueue_Post(plugin->queue, nullptr, 0, plugin->data_in, nullptr)) |
1307 | 0 | return ERROR_INTERNAL_ERROR; |
1308 | 0 | plugin->data_in = nullptr; |
1309 | 0 | } |
1310 | 0 | else |
1311 | 0 | { |
1312 | 0 | UINT error = rdpsnd_recv_pdu(plugin, plugin->data_in); |
1313 | 0 | plugin->data_in = nullptr; |
1314 | 0 | if (error) |
1315 | 0 | return error; |
1316 | 0 | } |
1317 | 0 | } |
1318 | | |
1319 | 0 | return CHANNEL_RC_OK; |
1320 | 0 | } |
1321 | | |
1322 | | static VOID VCAPITYPE rdpsnd_virtual_channel_open_event_ex(LPVOID lpUserParam, DWORD openHandle, |
1323 | | UINT event, LPVOID pData, |
1324 | | UINT32 dataLength, UINT32 totalLength, |
1325 | | UINT32 dataFlags) |
1326 | 0 | { |
1327 | 0 | UINT error = CHANNEL_RC_OK; |
1328 | 0 | rdpsndPlugin* rdpsnd = (rdpsndPlugin*)lpUserParam; |
1329 | 0 | WINPR_ASSERT(rdpsnd); |
1330 | 0 | WINPR_ASSERT(!rdpsnd->dynamic); |
1331 | |
|
1332 | 0 | switch (event) |
1333 | 0 | { |
1334 | 0 | case CHANNEL_EVENT_DATA_RECEIVED: |
1335 | 0 | if (!rdpsnd) |
1336 | 0 | return; |
1337 | | |
1338 | 0 | if (rdpsnd->OpenHandle != openHandle) |
1339 | 0 | { |
1340 | 0 | WLog_ERR(TAG, "%s error no match", rdpsnd_is_dyn_str(rdpsnd->dynamic)); |
1341 | 0 | return; |
1342 | 0 | } |
1343 | 0 | if ((error = rdpsnd_virtual_channel_event_data_received(rdpsnd, pData, dataLength, |
1344 | 0 | totalLength, dataFlags))) |
1345 | 0 | WLog_ERR(TAG, |
1346 | 0 | "%s rdpsnd_virtual_channel_event_data_received failed with error %" PRIu32 |
1347 | 0 | "", |
1348 | 0 | rdpsnd_is_dyn_str(rdpsnd->dynamic), error); |
1349 | |
|
1350 | 0 | break; |
1351 | | |
1352 | 0 | case CHANNEL_EVENT_WRITE_CANCELLED: |
1353 | 0 | case CHANNEL_EVENT_WRITE_COMPLETE: |
1354 | 0 | { |
1355 | 0 | wStream* s = (wStream*)pData; |
1356 | 0 | Stream_Free(s, TRUE); |
1357 | 0 | } |
1358 | 0 | break; |
1359 | | |
1360 | 0 | case CHANNEL_EVENT_USER: |
1361 | 0 | break; |
1362 | 0 | default: |
1363 | 0 | break; |
1364 | 0 | } |
1365 | | |
1366 | 0 | if (error && rdpsnd && rdpsnd->rdpcontext) |
1367 | 0 | { |
1368 | 0 | char buffer[8192]; |
1369 | 0 | (void)_snprintf(buffer, sizeof(buffer), |
1370 | 0 | "%s rdpsnd_virtual_channel_open_event_ex reported an error", |
1371 | 0 | rdpsnd_is_dyn_str(rdpsnd->dynamic)); |
1372 | 0 | setChannelError(rdpsnd->rdpcontext, error, buffer); |
1373 | 0 | } |
1374 | 0 | } |
1375 | | |
1376 | | /** |
1377 | | * Function description |
1378 | | * |
1379 | | * @return 0 on success, otherwise a Win32 error code |
1380 | | */ |
1381 | | WINPR_ATTR_NODISCARD |
1382 | | static UINT rdpsnd_virtual_channel_event_connected(rdpsndPlugin* rdpsnd, LPVOID pData, |
1383 | | UINT32 dataLength) |
1384 | 0 | { |
1385 | 0 | UINT32 status = 0; |
1386 | 0 | DWORD opened = 0; |
1387 | 0 | WINPR_UNUSED(pData); |
1388 | 0 | WINPR_UNUSED(dataLength); |
1389 | |
|
1390 | 0 | WINPR_ASSERT(rdpsnd); |
1391 | 0 | WINPR_ASSERT(!rdpsnd->dynamic); |
1392 | |
|
1393 | 0 | status = rdpsnd->channelEntryPoints.pVirtualChannelOpenEx( |
1394 | 0 | rdpsnd->InitHandle, &opened, rdpsnd->channelDef.name, rdpsnd_virtual_channel_open_event_ex); |
1395 | |
|
1396 | 0 | if (status != CHANNEL_RC_OK) |
1397 | 0 | { |
1398 | 0 | WLog_ERR(TAG, "%s pVirtualChannelOpenEx failed with %s [%08" PRIX32 "]", |
1399 | 0 | rdpsnd_is_dyn_str(rdpsnd->dynamic), WTSErrorToString(status), status); |
1400 | 0 | goto fail; |
1401 | 0 | } |
1402 | | |
1403 | 0 | if (rdpsnd_process_connect(rdpsnd) != CHANNEL_RC_OK) |
1404 | 0 | goto fail; |
1405 | | |
1406 | 0 | rdpsnd->OpenHandle = opened; |
1407 | 0 | return CHANNEL_RC_OK; |
1408 | 0 | fail: |
1409 | 0 | if (opened != 0) |
1410 | 0 | rdpsnd->channelEntryPoints.pVirtualChannelCloseEx(rdpsnd->InitHandle, opened); |
1411 | 0 | return CHANNEL_RC_NO_MEMORY; |
1412 | 0 | } |
1413 | | |
1414 | | static void rdpsnd_terminate_thread(rdpsndPlugin* rdpsnd) |
1415 | 0 | { |
1416 | 0 | WINPR_ASSERT(rdpsnd); |
1417 | 0 | if (rdpsnd->async) |
1418 | 0 | { |
1419 | 0 | if (rdpsnd->queue) |
1420 | 0 | MessageQueue_PostQuit(rdpsnd->queue, 0); |
1421 | |
|
1422 | 0 | if (rdpsnd->thread) |
1423 | 0 | { |
1424 | 0 | (void)WaitForSingleObject(rdpsnd->thread, INFINITE); |
1425 | 0 | (void)CloseHandle(rdpsnd->thread); |
1426 | 0 | } |
1427 | 0 | DeleteCriticalSection(&rdpsnd->asyncLock); |
1428 | 0 | MessageQueue_Free(rdpsnd->queue); |
1429 | 0 | } |
1430 | 0 | rdpsnd->thread = nullptr; |
1431 | 0 | rdpsnd->queue = nullptr; |
1432 | 0 | } |
1433 | | |
1434 | | static void cleanup_internals(rdpsndPlugin* rdpsnd) |
1435 | 0 | { |
1436 | 0 | if (!rdpsnd) |
1437 | 0 | return; |
1438 | | |
1439 | 0 | if (rdpsnd->async) |
1440 | 0 | { |
1441 | 0 | EnterCriticalSection(&rdpsnd->asyncLock); |
1442 | 0 | if (rdpsnd->queue && (MessageQueue_Clear(rdpsnd->queue) < 0)) |
1443 | 0 | WLog_WARN(TAG, "%s failed to clear the message queue", |
1444 | 0 | rdpsnd_is_dyn_str(rdpsnd->dynamic)); |
1445 | 0 | } |
1446 | |
|
1447 | 0 | if (rdpsnd->device) |
1448 | 0 | { |
1449 | 0 | IFCALL(rdpsnd->device->Close, rdpsnd->device); |
1450 | 0 | IFCALL(rdpsnd->device->Free, rdpsnd->device); |
1451 | 0 | rdpsnd->device = nullptr; |
1452 | 0 | } |
1453 | 0 | rdpsnd->isOpen = FALSE; |
1454 | 0 | rdpsnd->expectingWave = FALSE; |
1455 | 0 | rdpsnd->firstFlagReceived = FALSE; |
1456 | 0 | rdpsnd->totalLength = 0; |
1457 | |
|
1458 | 0 | if (rdpsnd->pool) |
1459 | 0 | StreamPool_Return(rdpsnd->pool, rdpsnd->data_in); |
1460 | |
|
1461 | 0 | audio_formats_free(rdpsnd->ClientFormats, rdpsnd->NumberOfClientFormats); |
1462 | 0 | audio_formats_free(rdpsnd->ServerFormats, rdpsnd->NumberOfServerFormats); |
1463 | |
|
1464 | 0 | rdpsnd->NumberOfClientFormats = 0; |
1465 | 0 | rdpsnd->ClientFormats = nullptr; |
1466 | 0 | rdpsnd->NumberOfServerFormats = 0; |
1467 | 0 | rdpsnd->ServerFormats = nullptr; |
1468 | |
|
1469 | 0 | rdpsnd->data_in = nullptr; |
1470 | |
|
1471 | 0 | if (rdpsnd->async) |
1472 | 0 | LeaveCriticalSection(&rdpsnd->asyncLock); |
1473 | 0 | } |
1474 | | |
1475 | | /** |
1476 | | * Function description |
1477 | | * |
1478 | | * @return 0 on success, otherwise a Win32 error code |
1479 | | */ |
1480 | | WINPR_ATTR_NODISCARD |
1481 | | static UINT rdpsnd_virtual_channel_event_disconnected(rdpsndPlugin* rdpsnd) |
1482 | 0 | { |
1483 | 0 | UINT error = 0; |
1484 | |
|
1485 | 0 | WINPR_ASSERT(rdpsnd); |
1486 | 0 | WINPR_ASSERT(!rdpsnd->dynamic); |
1487 | 0 | if (rdpsnd->OpenHandle != 0) |
1488 | 0 | { |
1489 | 0 | DWORD opened = rdpsnd->OpenHandle; |
1490 | 0 | rdpsnd->OpenHandle = 0; |
1491 | 0 | error = rdpsnd->channelEntryPoints.pVirtualChannelCloseEx(rdpsnd->InitHandle, opened); |
1492 | |
|
1493 | 0 | if (CHANNEL_RC_OK != error) |
1494 | 0 | { |
1495 | 0 | WLog_ERR(TAG, "%s pVirtualChannelCloseEx failed with %s [%08" PRIX32 "]", |
1496 | 0 | rdpsnd_is_dyn_str(rdpsnd->dynamic), WTSErrorToString(error), error); |
1497 | 0 | } |
1498 | 0 | } |
1499 | |
|
1500 | 0 | cleanup_internals(rdpsnd); |
1501 | 0 | return error; |
1502 | 0 | } |
1503 | | |
1504 | | static void queue_free(void* obj) |
1505 | 0 | { |
1506 | 0 | wMessage* msg = obj; |
1507 | 0 | if (!msg) |
1508 | 0 | return; |
1509 | 0 | if (msg->id != 0) |
1510 | 0 | return; |
1511 | 0 | wStream* s = msg->wParam; |
1512 | 0 | Stream_Release(s); |
1513 | 0 | } |
1514 | | |
1515 | | static void free_internals(rdpsndPlugin* rdpsnd) |
1516 | 0 | { |
1517 | 0 | if (!rdpsnd) |
1518 | 0 | return; |
1519 | | |
1520 | 0 | if (rdpsnd->references > 0) |
1521 | 0 | rdpsnd->references--; |
1522 | |
|
1523 | 0 | if (rdpsnd->references > 0) |
1524 | 0 | return; |
1525 | | |
1526 | 0 | rdpsnd_terminate_thread(rdpsnd); |
1527 | 0 | freerdp_dsp_context_free(rdpsnd->dsp_context); |
1528 | 0 | StreamPool_Free(rdpsnd->pool); |
1529 | 0 | rdpsnd->pool = nullptr; |
1530 | 0 | rdpsnd->dsp_context = nullptr; |
1531 | 0 | } |
1532 | | |
1533 | | WINPR_ATTR_NODISCARD |
1534 | | static BOOL allocate_internals(rdpsndPlugin* rdpsnd) |
1535 | 0 | { |
1536 | 0 | WINPR_ASSERT(rdpsnd); |
1537 | |
|
1538 | 0 | if (!rdpsnd->pool) |
1539 | 0 | { |
1540 | 0 | rdpsnd->pool = StreamPool_New(TRUE, 4096); |
1541 | 0 | if (!rdpsnd->pool) |
1542 | 0 | return FALSE; |
1543 | 0 | } |
1544 | | |
1545 | 0 | if (!rdpsnd->dsp_context) |
1546 | 0 | { |
1547 | 0 | rdpsnd->dsp_context = freerdp_dsp_context_new(FALSE); |
1548 | 0 | if (!rdpsnd->dsp_context) |
1549 | 0 | return FALSE; |
1550 | 0 | } |
1551 | | |
1552 | 0 | if (rdpsnd->async) |
1553 | 0 | { |
1554 | 0 | if (!rdpsnd->queue) |
1555 | 0 | { |
1556 | 0 | wObject obj = WINPR_C_ARRAY_INIT; |
1557 | |
|
1558 | 0 | obj.fnObjectFree = queue_free; |
1559 | 0 | rdpsnd->queue = MessageQueue_New(&obj); |
1560 | 0 | if (!rdpsnd->queue) |
1561 | 0 | return CHANNEL_RC_NO_MEMORY; |
1562 | 0 | InitializeCriticalSection(&rdpsnd->asyncLock); |
1563 | 0 | } |
1564 | | |
1565 | 0 | if (!rdpsnd->thread) |
1566 | 0 | { |
1567 | 0 | rdpsnd->thread = CreateThread(nullptr, 0, play_thread, rdpsnd, 0, nullptr); |
1568 | 0 | if (!rdpsnd->thread) |
1569 | 0 | return CHANNEL_RC_INITIALIZATION_ERROR; |
1570 | 0 | } |
1571 | 0 | } |
1572 | | |
1573 | 0 | rdpsnd->references++; |
1574 | |
|
1575 | 0 | return TRUE; |
1576 | 0 | } |
1577 | | |
1578 | | WINPR_ATTR_NODISCARD |
1579 | | static DWORD WINAPI play_thread(LPVOID arg) |
1580 | 0 | { |
1581 | 0 | UINT error = CHANNEL_RC_OK; |
1582 | 0 | rdpsndPlugin* rdpsnd = arg; |
1583 | |
|
1584 | 0 | if (!rdpsnd || !rdpsnd->queue) |
1585 | 0 | return ERROR_INVALID_PARAMETER; |
1586 | | |
1587 | 0 | while (TRUE) |
1588 | 0 | { |
1589 | 0 | int rc = -1; |
1590 | 0 | wMessage message = WINPR_C_ARRAY_INIT; |
1591 | 0 | wStream* s = nullptr; |
1592 | 0 | DWORD status = 0; |
1593 | 0 | DWORD nCount = 0; |
1594 | 0 | HANDLE handles[MAXIMUM_WAIT_OBJECTS] = WINPR_C_ARRAY_INIT; |
1595 | |
|
1596 | 0 | handles[nCount++] = MessageQueue_Event(rdpsnd->queue); |
1597 | 0 | status = WaitForMultipleObjects(nCount, handles, FALSE, INFINITE); |
1598 | 0 | switch (status) |
1599 | 0 | { |
1600 | 0 | case WAIT_OBJECT_0: |
1601 | 0 | break; |
1602 | 0 | default: |
1603 | 0 | return ERROR_TIMEOUT; |
1604 | 0 | } |
1605 | | |
1606 | | /* cleanup_internals clears the queue under this lock. */ |
1607 | 0 | if (rdpsnd->async) |
1608 | 0 | EnterCriticalSection(&rdpsnd->asyncLock); |
1609 | |
|
1610 | 0 | rc = MessageQueue_Peek(rdpsnd->queue, &message, TRUE); |
1611 | 0 | if ((rc > 0) && (message.id != WMQ_QUIT)) |
1612 | 0 | { |
1613 | 0 | s = message.wParam; |
1614 | 0 | error = rdpsnd_recv_pdu(rdpsnd, s); |
1615 | 0 | } |
1616 | |
|
1617 | 0 | if (rdpsnd->async) |
1618 | 0 | LeaveCriticalSection(&rdpsnd->asyncLock); |
1619 | |
|
1620 | 0 | if (rc < 1) |
1621 | 0 | continue; |
1622 | | |
1623 | 0 | if (message.id == WMQ_QUIT) |
1624 | 0 | break; |
1625 | | |
1626 | 0 | if (error) |
1627 | 0 | WLog_Print(rdpsnd->log, WLOG_WARN, "%s PDU dropped [%08" PRIX32 "]", |
1628 | 0 | rdpsnd_is_dyn_str(rdpsnd->dynamic), error); |
1629 | 0 | error = CHANNEL_RC_OK; |
1630 | 0 | } |
1631 | | |
1632 | 0 | return CHANNEL_RC_OK; |
1633 | 0 | } |
1634 | | |
1635 | | WINPR_ATTR_NODISCARD |
1636 | | static UINT rdpsnd_virtual_channel_event_initialized(rdpsndPlugin* rdpsnd) |
1637 | 0 | { |
1638 | 0 | if (!rdpsnd) |
1639 | 0 | return ERROR_INVALID_PARAMETER; |
1640 | | |
1641 | 0 | if (!allocate_internals(rdpsnd)) |
1642 | 0 | return CHANNEL_RC_NO_MEMORY; |
1643 | | |
1644 | 0 | return CHANNEL_RC_OK; |
1645 | 0 | } |
1646 | | |
1647 | | void rdpsnd_virtual_channel_event_terminated(rdpsndPlugin* rdpsnd) |
1648 | 0 | { |
1649 | 0 | if (rdpsnd) |
1650 | 0 | { |
1651 | 0 | free_internals(rdpsnd); |
1652 | 0 | audio_formats_free(rdpsnd->fixed_format, 1); |
1653 | 0 | free(rdpsnd->subsystem); |
1654 | 0 | free(rdpsnd->device_name); |
1655 | 0 | rdpsnd->InitHandle = nullptr; |
1656 | 0 | } |
1657 | |
|
1658 | 0 | free(rdpsnd); |
1659 | 0 | } |
1660 | | |
1661 | | static VOID VCAPITYPE rdpsnd_virtual_channel_init_event_ex(LPVOID lpUserParam, LPVOID pInitHandle, |
1662 | | UINT event, LPVOID pData, |
1663 | | UINT dataLength) |
1664 | 0 | { |
1665 | 0 | UINT error = CHANNEL_RC_OK; |
1666 | 0 | rdpsndPlugin* plugin = (rdpsndPlugin*)lpUserParam; |
1667 | |
|
1668 | 0 | if (!plugin) |
1669 | 0 | return; |
1670 | | |
1671 | 0 | if (plugin->InitHandle != pInitHandle) |
1672 | 0 | { |
1673 | 0 | WLog_ERR(TAG, "%s error no match", rdpsnd_is_dyn_str(plugin->dynamic)); |
1674 | 0 | return; |
1675 | 0 | } |
1676 | | |
1677 | 0 | switch (event) |
1678 | 0 | { |
1679 | 0 | case CHANNEL_EVENT_INITIALIZED: |
1680 | 0 | error = rdpsnd_virtual_channel_event_initialized(plugin); |
1681 | 0 | break; |
1682 | | |
1683 | 0 | case CHANNEL_EVENT_CONNECTED: |
1684 | 0 | error = rdpsnd_virtual_channel_event_connected(plugin, pData, dataLength); |
1685 | 0 | break; |
1686 | | |
1687 | 0 | case CHANNEL_EVENT_DISCONNECTED: |
1688 | 0 | error = rdpsnd_virtual_channel_event_disconnected(plugin); |
1689 | 0 | break; |
1690 | | |
1691 | 0 | case CHANNEL_EVENT_TERMINATED: |
1692 | 0 | rdpsnd_virtual_channel_event_terminated(plugin); |
1693 | 0 | plugin = nullptr; |
1694 | 0 | break; |
1695 | | |
1696 | 0 | case CHANNEL_EVENT_ATTACHED: |
1697 | 0 | plugin->attached = TRUE; |
1698 | 0 | break; |
1699 | | |
1700 | 0 | case CHANNEL_EVENT_DETACHED: |
1701 | 0 | plugin->attached = FALSE; |
1702 | 0 | break; |
1703 | | |
1704 | 0 | default: |
1705 | 0 | break; |
1706 | 0 | } |
1707 | | |
1708 | 0 | if (error && plugin && plugin->rdpcontext) |
1709 | 0 | { |
1710 | 0 | char buffer[8192]; |
1711 | 0 | (void)_snprintf(buffer, sizeof(buffer), "%s reported an error", |
1712 | 0 | rdpsnd_is_dyn_str(plugin->dynamic)); |
1713 | 0 | setChannelError(plugin->rdpcontext, error, buffer); |
1714 | 0 | } |
1715 | 0 | } |
1716 | | |
1717 | | rdpContext* freerdp_rdpsnd_get_context(rdpsndPlugin* plugin) |
1718 | 0 | { |
1719 | 0 | if (!plugin) |
1720 | 0 | return nullptr; |
1721 | | |
1722 | 0 | return plugin->rdpcontext; |
1723 | 0 | } |
1724 | | |
1725 | | WINPR_ATTR_NODISCARD |
1726 | | static rdpsndPlugin* allocatePlugin(void) |
1727 | 0 | { |
1728 | 0 | rdpsndPlugin* rdpsnd = (rdpsndPlugin*)calloc(1, sizeof(rdpsndPlugin)); |
1729 | 0 | if (!rdpsnd) |
1730 | 0 | goto fail; |
1731 | | |
1732 | 0 | rdpsnd->fixed_format = audio_format_new(); |
1733 | 0 | if (!rdpsnd->fixed_format) |
1734 | 0 | goto fail; |
1735 | 0 | rdpsnd->log = WLog_Get("com.freerdp.channels.rdpsnd.client"); |
1736 | 0 | if (!rdpsnd->log) |
1737 | 0 | goto fail; |
1738 | | |
1739 | 0 | rdpsnd->attached = TRUE; |
1740 | 0 | return rdpsnd; |
1741 | | |
1742 | 0 | fail: |
1743 | 0 | if (rdpsnd) |
1744 | 0 | audio_formats_free(rdpsnd->fixed_format, 1); |
1745 | 0 | free(rdpsnd); |
1746 | 0 | return nullptr; |
1747 | 0 | } |
1748 | | /* rdpsnd is always built-in */ |
1749 | | FREERDP_ENTRY_POINT(BOOL VCAPITYPE rdpsnd_VirtualChannelEntryEx( |
1750 | | PCHANNEL_ENTRY_POINTS_EX pEntryPoints, PVOID pInitHandle)) |
1751 | 0 | { |
1752 | 0 | UINT rc = 0; |
1753 | 0 | CHANNEL_ENTRY_POINTS_FREERDP_EX* pEntryPointsEx = nullptr; |
1754 | |
|
1755 | 0 | if (!pEntryPoints) |
1756 | 0 | return FALSE; |
1757 | | |
1758 | 0 | rdpsndPlugin* rdpsnd = allocatePlugin(); |
1759 | |
|
1760 | 0 | if (!rdpsnd) |
1761 | 0 | return FALSE; |
1762 | | |
1763 | 0 | rdpsnd->channelDef.options = CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP; |
1764 | 0 | (void)sprintf_s(rdpsnd->channelDef.name, ARRAYSIZE(rdpsnd->channelDef.name), |
1765 | 0 | RDPSND_CHANNEL_NAME); |
1766 | 0 | pEntryPointsEx = (CHANNEL_ENTRY_POINTS_FREERDP_EX*)pEntryPoints; |
1767 | |
|
1768 | 0 | if ((pEntryPointsEx->cbSize >= sizeof(CHANNEL_ENTRY_POINTS_FREERDP_EX)) && |
1769 | 0 | (pEntryPointsEx->MagicNumber == FREERDP_CHANNEL_MAGIC_NUMBER)) |
1770 | 0 | { |
1771 | 0 | rdpsnd->rdpcontext = pEntryPointsEx->context; |
1772 | 0 | if (!freerdp_settings_get_bool(rdpsnd->rdpcontext->settings, |
1773 | 0 | FreeRDP_SynchronousStaticChannels)) |
1774 | 0 | rdpsnd->async = TRUE; |
1775 | 0 | } |
1776 | |
|
1777 | 0 | CopyMemory(&(rdpsnd->channelEntryPoints), pEntryPoints, |
1778 | 0 | sizeof(CHANNEL_ENTRY_POINTS_FREERDP_EX)); |
1779 | 0 | rdpsnd->InitHandle = pInitHandle; |
1780 | |
|
1781 | 0 | WINPR_ASSERT(rdpsnd->channelEntryPoints.pVirtualChannelInitEx); |
1782 | 0 | rc = rdpsnd->channelEntryPoints.pVirtualChannelInitEx( |
1783 | 0 | rdpsnd, nullptr, pInitHandle, &rdpsnd->channelDef, 1, VIRTUAL_CHANNEL_VERSION_WIN2000, |
1784 | 0 | rdpsnd_virtual_channel_init_event_ex); |
1785 | |
|
1786 | 0 | if (CHANNEL_RC_OK != rc) |
1787 | 0 | { |
1788 | 0 | WLog_ERR(TAG, "%s pVirtualChannelInitEx failed with %s [%08" PRIX32 "]", |
1789 | 0 | rdpsnd_is_dyn_str(FALSE), WTSErrorToString(rc), rc); |
1790 | 0 | rdpsnd_virtual_channel_event_terminated(rdpsnd); |
1791 | 0 | return FALSE; |
1792 | 0 | } |
1793 | | |
1794 | 0 | return TRUE; |
1795 | 0 | } |
1796 | | |
1797 | | WINPR_ATTR_NODISCARD |
1798 | | static UINT rdpsnd_on_open(IWTSVirtualChannelCallback* pChannelCallback) |
1799 | 0 | { |
1800 | 0 | GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback; |
1801 | 0 | rdpsndPlugin* rdpsnd = nullptr; |
1802 | |
|
1803 | 0 | WINPR_ASSERT(callback); |
1804 | |
|
1805 | 0 | rdpsnd = (rdpsndPlugin*)callback->plugin; |
1806 | 0 | WINPR_ASSERT(rdpsnd); |
1807 | |
|
1808 | 0 | if (rdpsnd->OnOpenCalled) |
1809 | 0 | return CHANNEL_RC_OK; |
1810 | 0 | rdpsnd->OnOpenCalled = TRUE; |
1811 | |
|
1812 | 0 | if (!allocate_internals(rdpsnd)) |
1813 | 0 | return ERROR_OUTOFMEMORY; |
1814 | | |
1815 | 0 | return rdpsnd_process_connect(rdpsnd); |
1816 | 0 | } |
1817 | | |
1818 | | WINPR_ATTR_NODISCARD |
1819 | | static UINT rdpsnd_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* data) |
1820 | 0 | { |
1821 | 0 | GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback; |
1822 | 0 | rdpsndPlugin* plugin = nullptr; |
1823 | 0 | wStream* copy = nullptr; |
1824 | 0 | size_t len = 0; |
1825 | |
|
1826 | 0 | len = Stream_GetRemainingLength(data); |
1827 | |
|
1828 | 0 | if (!callback || !callback->plugin) |
1829 | 0 | return ERROR_INVALID_PARAMETER; |
1830 | 0 | plugin = (rdpsndPlugin*)callback->plugin; |
1831 | 0 | WINPR_ASSERT(plugin); |
1832 | |
|
1833 | 0 | copy = StreamPool_Take(plugin->pool, len); |
1834 | 0 | if (!copy) |
1835 | 0 | return ERROR_OUTOFMEMORY; |
1836 | 0 | Stream_Copy(data, copy, len); |
1837 | 0 | Stream_SealLength(copy); |
1838 | 0 | Stream_ResetPosition(copy); |
1839 | |
|
1840 | 0 | if (plugin->async) |
1841 | 0 | { |
1842 | 0 | if (!MessageQueue_Post(plugin->queue, nullptr, 0, copy, nullptr)) |
1843 | 0 | { |
1844 | 0 | Stream_Release(copy); |
1845 | 0 | return ERROR_INTERNAL_ERROR; |
1846 | 0 | } |
1847 | 0 | } |
1848 | 0 | else |
1849 | 0 | { |
1850 | 0 | UINT error = rdpsnd_recv_pdu(plugin, copy); |
1851 | 0 | if (error) |
1852 | 0 | return error; |
1853 | 0 | } |
1854 | | |
1855 | 0 | return CHANNEL_RC_OK; |
1856 | 0 | } |
1857 | | |
1858 | | WINPR_ATTR_NODISCARD |
1859 | | static UINT rdpsnd_on_close(IWTSVirtualChannelCallback* pChannelCallback) |
1860 | 0 | { |
1861 | 0 | GENERIC_CHANNEL_CALLBACK* callback = (GENERIC_CHANNEL_CALLBACK*)pChannelCallback; |
1862 | |
|
1863 | 0 | WINPR_ASSERT(callback); |
1864 | |
|
1865 | 0 | rdpsndPlugin* rdpsnd = (rdpsndPlugin*)callback->plugin; |
1866 | 0 | WINPR_ASSERT(rdpsnd); |
1867 | |
|
1868 | 0 | rdpsnd->OnOpenCalled = FALSE; |
1869 | 0 | cleanup_internals(rdpsnd); |
1870 | 0 | free_internals(rdpsnd); |
1871 | |
|
1872 | 0 | free(pChannelCallback); |
1873 | 0 | rdpsnd->connected = FALSE; |
1874 | 0 | return CHANNEL_RC_OK; |
1875 | 0 | } |
1876 | | |
1877 | | // NOLINTBEGIN(readability-non-const-parameter) |
1878 | | WINPR_ATTR_NODISCARD |
1879 | | static UINT rdpsnd_on_new_channel_connection(IWTSListenerCallback* pListenerCallback, |
1880 | | IWTSVirtualChannel* pChannel, BYTE* Data, |
1881 | | BOOL* pbAccept, |
1882 | | IWTSVirtualChannelCallback** ppCallback) |
1883 | | // NOLINTEND(readability-non-const-parameter) |
1884 | 0 | { |
1885 | 0 | GENERIC_LISTENER_CALLBACK* listener_callback = (GENERIC_LISTENER_CALLBACK*)pListenerCallback; |
1886 | 0 | WINPR_ASSERT(listener_callback); |
1887 | 0 | WINPR_ASSERT(pChannel); |
1888 | 0 | WINPR_ASSERT(ppCallback); |
1889 | |
|
1890 | 0 | rdpsndPlugin* rdpsnd = (rdpsndPlugin*)listener_callback->plugin; |
1891 | 0 | WINPR_ASSERT(rdpsnd); |
1892 | |
|
1893 | 0 | if (rdpsnd->connected) |
1894 | 0 | { |
1895 | 0 | WLog_Print(rdpsnd->log, WLOG_ERROR, "Channel already connected, terminating."); |
1896 | 0 | return ERROR_DEVICE_ALREADY_ATTACHED; |
1897 | 0 | } |
1898 | | |
1899 | 0 | GENERIC_CHANNEL_CALLBACK* callback = |
1900 | 0 | (GENERIC_CHANNEL_CALLBACK*)calloc(1, sizeof(GENERIC_CHANNEL_CALLBACK)); |
1901 | |
|
1902 | 0 | WINPR_UNUSED(Data); |
1903 | 0 | WINPR_UNUSED(pbAccept); |
1904 | |
|
1905 | 0 | if (!callback) |
1906 | 0 | { |
1907 | 0 | WLog_ERR(TAG, "%s calloc failed!", rdpsnd_is_dyn_str(TRUE)); |
1908 | 0 | return CHANNEL_RC_NO_MEMORY; |
1909 | 0 | } |
1910 | | |
1911 | 0 | callback->iface.OnOpen = rdpsnd_on_open; |
1912 | 0 | callback->iface.OnDataReceived = rdpsnd_on_data_received; |
1913 | 0 | callback->iface.OnClose = rdpsnd_on_close; |
1914 | 0 | callback->plugin = listener_callback->plugin; |
1915 | 0 | callback->channel_mgr = listener_callback->channel_mgr; |
1916 | 0 | callback->channel = pChannel; |
1917 | 0 | listener_callback->channel_callback = callback; |
1918 | 0 | *ppCallback = &callback->iface; |
1919 | 0 | rdpsnd->connected = TRUE; |
1920 | 0 | return CHANNEL_RC_OK; |
1921 | 0 | } |
1922 | | |
1923 | | WINPR_ATTR_NODISCARD |
1924 | | static UINT rdpsnd_plugin_initialize(IWTSPlugin* pPlugin, IWTSVirtualChannelManager* pChannelMgr) |
1925 | 0 | { |
1926 | 0 | UINT status = 0; |
1927 | 0 | rdpsndPlugin* rdpsnd = (rdpsndPlugin*)pPlugin; |
1928 | 0 | WINPR_ASSERT(rdpsnd); |
1929 | 0 | WINPR_ASSERT(pChannelMgr); |
1930 | 0 | if (rdpsnd->initialized) |
1931 | 0 | { |
1932 | 0 | WLog_ERR(TAG, "[%s] channel initialized twice, aborting", RDPSND_DVC_CHANNEL_NAME); |
1933 | 0 | return ERROR_INVALID_DATA; |
1934 | 0 | } |
1935 | 0 | rdpsnd->listener_callback = |
1936 | 0 | (GENERIC_LISTENER_CALLBACK*)calloc(1, sizeof(GENERIC_LISTENER_CALLBACK)); |
1937 | |
|
1938 | 0 | if (!rdpsnd->listener_callback) |
1939 | 0 | { |
1940 | 0 | WLog_ERR(TAG, "%s calloc failed!", rdpsnd_is_dyn_str(TRUE)); |
1941 | 0 | return CHANNEL_RC_NO_MEMORY; |
1942 | 0 | } |
1943 | | |
1944 | 0 | rdpsnd->listener_callback->iface.OnNewChannelConnection = rdpsnd_on_new_channel_connection; |
1945 | 0 | rdpsnd->listener_callback->plugin = pPlugin; |
1946 | 0 | rdpsnd->listener_callback->channel_mgr = pChannelMgr; |
1947 | 0 | status = pChannelMgr->CreateListener(pChannelMgr, RDPSND_DVC_CHANNEL_NAME, 0, |
1948 | 0 | &rdpsnd->listener_callback->iface, &(rdpsnd->listener)); |
1949 | 0 | if (status != CHANNEL_RC_OK) |
1950 | 0 | { |
1951 | 0 | WLog_ERR(TAG, "%s CreateListener failed!", rdpsnd_is_dyn_str(TRUE)); |
1952 | 0 | return status; |
1953 | 0 | } |
1954 | | |
1955 | 0 | rdpsnd->listener->pInterface = rdpsnd->iface.pInterface; |
1956 | 0 | status = rdpsnd_virtual_channel_event_initialized(rdpsnd); |
1957 | |
|
1958 | 0 | rdpsnd->initialized = status == CHANNEL_RC_OK; |
1959 | 0 | return status; |
1960 | 0 | } |
1961 | | |
1962 | | /** |
1963 | | * Function description |
1964 | | * |
1965 | | * @return 0 on success, otherwise a Win32 error code |
1966 | | */ |
1967 | | WINPR_ATTR_NODISCARD |
1968 | | static UINT rdpsnd_plugin_terminated(IWTSPlugin* pPlugin) |
1969 | 0 | { |
1970 | 0 | rdpsndPlugin* rdpsnd = (rdpsndPlugin*)pPlugin; |
1971 | 0 | if (rdpsnd) |
1972 | 0 | { |
1973 | 0 | if (rdpsnd->listener_callback) |
1974 | 0 | { |
1975 | 0 | IWTSVirtualChannelManager* mgr = rdpsnd->listener_callback->channel_mgr; |
1976 | 0 | if (mgr) |
1977 | 0 | IFCALL(mgr->DestroyListener, mgr, rdpsnd->listener); |
1978 | 0 | } |
1979 | 0 | free(rdpsnd->listener_callback); |
1980 | 0 | free(rdpsnd->iface.pInterface); |
1981 | 0 | } |
1982 | 0 | rdpsnd_virtual_channel_event_terminated(rdpsnd); |
1983 | 0 | return CHANNEL_RC_OK; |
1984 | 0 | } |
1985 | | |
1986 | | /** |
1987 | | * Function description |
1988 | | * |
1989 | | * @return 0 on success, otherwise a Win32 error code |
1990 | | */ |
1991 | | FREERDP_ENTRY_POINT(UINT VCAPITYPE rdpsnd_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints)) |
1992 | 0 | { |
1993 | 0 | UINT error = CHANNEL_RC_OK; |
1994 | |
|
1995 | 0 | WINPR_ASSERT(pEntryPoints); |
1996 | 0 | WINPR_ASSERT(pEntryPoints->GetPlugin); |
1997 | |
|
1998 | 0 | rdpsndPlugin* rdpsnd = |
1999 | 0 | (rdpsndPlugin*)pEntryPoints->GetPlugin(pEntryPoints, RDPSND_CHANNEL_NAME); |
2000 | |
|
2001 | 0 | if (!rdpsnd) |
2002 | 0 | { |
2003 | 0 | IWTSPlugin* iface = nullptr; |
2004 | 0 | union |
2005 | 0 | { |
2006 | 0 | const void* cev; |
2007 | 0 | void* ev; |
2008 | 0 | } cnv; |
2009 | |
|
2010 | 0 | rdpsnd = allocatePlugin(); |
2011 | 0 | if (!rdpsnd) |
2012 | 0 | { |
2013 | 0 | WLog_ERR(TAG, "%s calloc failed!", rdpsnd_is_dyn_str(TRUE)); |
2014 | 0 | return CHANNEL_RC_NO_MEMORY; |
2015 | 0 | } |
2016 | | |
2017 | 0 | iface = &rdpsnd->iface; |
2018 | 0 | iface->Initialize = rdpsnd_plugin_initialize; |
2019 | 0 | iface->Connected = nullptr; |
2020 | 0 | iface->Disconnected = nullptr; |
2021 | 0 | iface->Terminated = rdpsnd_plugin_terminated; |
2022 | |
|
2023 | 0 | rdpsnd->dynamic = TRUE; |
2024 | |
|
2025 | 0 | WINPR_ASSERT(pEntryPoints->GetRdpContext); |
2026 | 0 | rdpsnd->rdpcontext = pEntryPoints->GetRdpContext(pEntryPoints); |
2027 | |
|
2028 | 0 | if (!freerdp_settings_get_bool(rdpsnd->rdpcontext->settings, |
2029 | 0 | FreeRDP_SynchronousDynamicChannels)) |
2030 | 0 | rdpsnd->async = TRUE; |
2031 | | |
2032 | | /* user data pointer is not const, cast to avoid warning. */ |
2033 | 0 | cnv.cev = pEntryPoints->GetPluginData(pEntryPoints); |
2034 | 0 | WINPR_ASSERT(pEntryPoints->GetPluginData); |
2035 | 0 | rdpsnd->channelEntryPoints.pExtendedData = cnv.ev; |
2036 | |
|
2037 | 0 | error = pEntryPoints->RegisterPlugin(pEntryPoints, RDPSND_CHANNEL_NAME, iface); |
2038 | 0 | } |
2039 | 0 | else |
2040 | 0 | { |
2041 | 0 | WLog_ERR(TAG, "%s could not get rdpsnd Plugin.", rdpsnd_is_dyn_str(TRUE)); |
2042 | 0 | return CHANNEL_RC_BAD_CHANNEL; |
2043 | 0 | } |
2044 | | |
2045 | 0 | return error; |
2046 | 0 | } |