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