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