/src/FreeRDP/channels/audin/client/audin_main.c
Line | Count | Source |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * Audio Input Redirection Virtual Channel |
4 | | * |
5 | | * Copyright 2010-2011 Vic Lee |
6 | | * Copyright 2015 Thincast Technologies GmbH |
7 | | * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com> |
8 | | * Copyright 2015 Armin Novak <armin.novak@thincast.com> |
9 | | * |
10 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
11 | | * you may not use this file except in compliance with the License. |
12 | | * You may obtain a copy of the License at |
13 | | * |
14 | | * http://www.apache.org/licenses/LICENSE-2.0 |
15 | | * |
16 | | * Unless required by applicable law or agreed to in writing, software |
17 | | * distributed under the License is distributed on an "AS IS" BASIS, |
18 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
19 | | * See the License for the specific language governing permissions and |
20 | | * limitations under the License. |
21 | | */ |
22 | | |
23 | | #include <freerdp/config.h> |
24 | | |
25 | | #include <errno.h> |
26 | | #include <winpr/assert.h> |
27 | | #include <stdio.h> |
28 | | #include <stdlib.h> |
29 | | #include <string.h> |
30 | | |
31 | | #include <winpr/crt.h> |
32 | | #include <winpr/cmdline.h> |
33 | | #include <winpr/wlog.h> |
34 | | |
35 | | #include <freerdp/addin.h> |
36 | | |
37 | | #include <winpr/stream.h> |
38 | | #include <freerdp/freerdp.h> |
39 | | #include <freerdp/codec/dsp.h> |
40 | | #include <freerdp/client/channels.h> |
41 | | #include <freerdp/channels/audin.h> |
42 | | |
43 | | #include "audin_main.h" |
44 | | |
45 | 0 | #define SNDIN_VERSION 0x02 |
46 | | |
47 | | typedef enum |
48 | | { |
49 | | MSG_SNDIN_VERSION = 0x01, |
50 | | MSG_SNDIN_FORMATS = 0x02, |
51 | | MSG_SNDIN_OPEN = 0x03, |
52 | | MSG_SNDIN_OPEN_REPLY = 0x04, |
53 | | MSG_SNDIN_DATA_INCOMING = 0x05, |
54 | | MSG_SNDIN_DATA = 0x06, |
55 | | MSG_SNDIN_FORMATCHANGE = 0x07, |
56 | | } MSG_SNDIN; |
57 | | |
58 | | typedef struct |
59 | | { |
60 | | IWTSVirtualChannelCallback iface; |
61 | | |
62 | | IWTSPlugin* plugin; |
63 | | IWTSVirtualChannelManager* channel_mgr; |
64 | | IWTSVirtualChannel* channel; |
65 | | |
66 | | /** |
67 | | * The supported format list sent back to the server, which needs to |
68 | | * be stored as reference when the server sends the format index in |
69 | | * Open PDU and Format Change PDU |
70 | | */ |
71 | | AUDIO_FORMAT* formats; |
72 | | UINT32 formats_count; |
73 | | } AUDIN_CHANNEL_CALLBACK; |
74 | | |
75 | | typedef struct |
76 | | { |
77 | | IWTSPlugin iface; |
78 | | |
79 | | GENERIC_LISTENER_CALLBACK* listener_callback; |
80 | | |
81 | | /* Parsed plugin data */ |
82 | | AUDIO_FORMAT* fixed_format; |
83 | | char* subsystem; |
84 | | char* device_name; |
85 | | |
86 | | /* Device interface */ |
87 | | IAudinDevice* device; |
88 | | |
89 | | rdpContext* rdpcontext; |
90 | | BOOL attached; |
91 | | wStream* data; |
92 | | AUDIO_FORMAT* format; |
93 | | UINT32 FramesPerPacket; |
94 | | |
95 | | FREERDP_DSP_CONTEXT* dsp_context; |
96 | | wLog* log; |
97 | | |
98 | | IWTSListener* listener; |
99 | | |
100 | | BOOL initialized; |
101 | | UINT32 version; |
102 | | BOOL connected; |
103 | | } AUDIN_PLUGIN; |
104 | | |
105 | | static BOOL audin_process_addin_args(AUDIN_PLUGIN* audin, const ADDIN_ARGV* args); |
106 | | |
107 | | static UINT audin_channel_write_and_free(AUDIN_CHANNEL_CALLBACK* callback, wStream* out, |
108 | | BOOL freeStream) |
109 | 0 | { |
110 | 0 | if (!callback || !out) |
111 | 0 | return ERROR_INVALID_PARAMETER; |
112 | | |
113 | 0 | if (!callback->channel || !callback->channel->Write) |
114 | 0 | return ERROR_INTERNAL_ERROR; |
115 | | |
116 | 0 | Stream_SealLength(out); |
117 | |
|
118 | 0 | const ULONG len = WINPR_ASSERTING_INT_CAST(ULONG, Stream_Length(out)); |
119 | 0 | const UINT error = |
120 | 0 | callback->channel->Write(callback->channel, len, Stream_Buffer(out), nullptr); |
121 | |
|
122 | 0 | if (freeStream) |
123 | 0 | Stream_Free(out, TRUE); |
124 | |
|
125 | 0 | return error; |
126 | 0 | } |
127 | | |
128 | | /** |
129 | | * Function description |
130 | | * |
131 | | * @return 0 on success, otherwise a Win32 error code |
132 | | */ |
133 | | static UINT audin_process_version(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback, wStream* s) |
134 | 0 | { |
135 | 0 | const UINT32 ClientVersion = SNDIN_VERSION; |
136 | 0 | UINT32 ServerVersion = 0; |
137 | |
|
138 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 4)) |
139 | 0 | return ERROR_INVALID_DATA; |
140 | | |
141 | 0 | Stream_Read_UINT32(s, ServerVersion); |
142 | 0 | WLog_Print(audin->log, WLOG_DEBUG, "ServerVersion=%" PRIu32 ", ClientVersion=%" PRIu32, |
143 | 0 | ServerVersion, ClientVersion); |
144 | | |
145 | | /* Do not answer server packet, we do not support the channel version. */ |
146 | 0 | if (ServerVersion > ClientVersion) |
147 | 0 | { |
148 | 0 | WLog_Print(audin->log, WLOG_WARN, |
149 | 0 | "Incompatible channel version server=%" PRIu32 |
150 | 0 | ", client supports version=%" PRIu32, |
151 | 0 | ServerVersion, ClientVersion); |
152 | 0 | return CHANNEL_RC_OK; |
153 | 0 | } |
154 | 0 | audin->version = ServerVersion; |
155 | |
|
156 | 0 | wStream* out = Stream_New(nullptr, 5); |
157 | |
|
158 | 0 | if (!out) |
159 | 0 | { |
160 | 0 | WLog_Print(audin->log, WLOG_ERROR, "Stream_New failed!"); |
161 | 0 | return ERROR_OUTOFMEMORY; |
162 | 0 | } |
163 | | |
164 | 0 | Stream_Write_UINT8(out, MSG_SNDIN_VERSION); |
165 | 0 | Stream_Write_UINT32(out, ClientVersion); |
166 | 0 | return audin_channel_write_and_free(callback, out, TRUE); |
167 | 0 | } |
168 | | |
169 | | /** |
170 | | * Function description |
171 | | * |
172 | | * @return 0 on success, otherwise a Win32 error code |
173 | | */ |
174 | | static UINT audin_send_incoming_data_pdu(AUDIN_CHANNEL_CALLBACK* callback) |
175 | 0 | { |
176 | 0 | BYTE out_data[1] = { MSG_SNDIN_DATA_INCOMING }; |
177 | |
|
178 | 0 | if (!callback || !callback->channel || !callback->channel->Write) |
179 | 0 | return ERROR_INTERNAL_ERROR; |
180 | | |
181 | 0 | return callback->channel->Write(callback->channel, 1, out_data, nullptr); |
182 | 0 | } |
183 | | |
184 | | static UINT audin_close_device(AUDIN_PLUGIN* audin) |
185 | 0 | { |
186 | 0 | WINPR_ASSERT(audin); |
187 | 0 | if (audin->device) |
188 | 0 | { |
189 | 0 | UINT error = CHANNEL_RC_OK; |
190 | 0 | IFCALLRET(audin->device->Close, error, audin->device); |
191 | |
|
192 | 0 | if (error != CHANNEL_RC_OK) |
193 | 0 | { |
194 | 0 | WLog_ERR(TAG, "Close failed with errorcode %" PRIu32 "", error); |
195 | 0 | return error; |
196 | 0 | } |
197 | 0 | } |
198 | 0 | return CHANNEL_RC_OK; |
199 | 0 | } |
200 | | |
201 | | /** |
202 | | * Function description |
203 | | * |
204 | | * @return 0 on success, otherwise a Win32 error code |
205 | | */ |
206 | | static UINT audin_process_formats(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback, wStream* s) |
207 | 0 | { |
208 | 0 | UINT error = ERROR_INTERNAL_ERROR; |
209 | 0 | UINT32 NumFormats = 0; |
210 | 0 | UINT32 cbSizeFormatsPacket = 0; |
211 | |
|
212 | 0 | WINPR_ASSERT(audin); |
213 | 0 | WINPR_ASSERT(callback); |
214 | |
|
215 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 8)) |
216 | 0 | return ERROR_INVALID_DATA; |
217 | | |
218 | 0 | Stream_Read_UINT32(s, NumFormats); |
219 | 0 | WLog_Print(audin->log, WLOG_DEBUG, "NumFormats %" PRIu32 "", NumFormats); |
220 | |
|
221 | 0 | if ((NumFormats < 1) || (NumFormats > 1000)) |
222 | 0 | { |
223 | 0 | WLog_Print(audin->log, WLOG_ERROR, "bad NumFormats %" PRIu32 "", NumFormats); |
224 | 0 | return ERROR_INVALID_DATA; |
225 | 0 | } |
226 | | |
227 | 0 | Stream_Seek_UINT32(s); /* cbSizeFormatsPacket */ |
228 | |
|
229 | 0 | const UINT error2 = audin_close_device(audin); |
230 | 0 | if (error2 != CHANNEL_RC_OK) |
231 | 0 | return error2; |
232 | | |
233 | 0 | audin->format = nullptr; |
234 | 0 | audio_formats_free(callback->formats, callback->formats_count); |
235 | 0 | callback->formats_count = 0; |
236 | |
|
237 | 0 | callback->formats = audio_formats_new(NumFormats); |
238 | |
|
239 | 0 | if (!callback->formats) |
240 | 0 | { |
241 | 0 | WLog_Print(audin->log, WLOG_ERROR, "calloc failed!"); |
242 | 0 | return ERROR_INVALID_DATA; |
243 | 0 | } |
244 | | |
245 | 0 | wStream* out = Stream_New(nullptr, 9); |
246 | |
|
247 | 0 | if (!out) |
248 | 0 | { |
249 | 0 | error = CHANNEL_RC_NO_MEMORY; |
250 | 0 | WLog_Print(audin->log, WLOG_ERROR, "Stream_New failed!"); |
251 | 0 | goto out; |
252 | 0 | } |
253 | | |
254 | 0 | Stream_Seek(out, 9); |
255 | | |
256 | | /* SoundFormats (variable) */ |
257 | 0 | for (UINT32 i = 0; i < NumFormats; i++) |
258 | 0 | { |
259 | 0 | AUDIO_FORMAT format = WINPR_C_ARRAY_INIT; |
260 | |
|
261 | 0 | if (!audio_format_read(s, &format)) |
262 | 0 | { |
263 | 0 | error = ERROR_INVALID_DATA; |
264 | 0 | goto out; |
265 | 0 | } |
266 | | |
267 | 0 | audio_format_print(audin->log, WLOG_DEBUG, &format); |
268 | |
|
269 | 0 | if (!audio_format_compatible(audin->fixed_format, &format)) |
270 | 0 | { |
271 | 0 | audio_format_free(&format); |
272 | 0 | continue; |
273 | 0 | } |
274 | | |
275 | 0 | if (freerdp_dsp_supports_format(&format, TRUE) || |
276 | 0 | audin->device->FormatSupported(audin->device, &format)) |
277 | 0 | { |
278 | | /* Store the agreed format in the corresponding index */ |
279 | 0 | callback->formats[callback->formats_count++] = format; |
280 | |
|
281 | 0 | if (!audio_format_write(out, &format)) |
282 | 0 | { |
283 | 0 | error = CHANNEL_RC_NO_MEMORY; |
284 | 0 | WLog_Print(audin->log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!"); |
285 | 0 | goto out; |
286 | 0 | } |
287 | 0 | } |
288 | 0 | else |
289 | 0 | { |
290 | 0 | audio_format_free(&format); |
291 | 0 | } |
292 | 0 | } |
293 | | |
294 | 0 | if ((error = audin_send_incoming_data_pdu(callback))) |
295 | 0 | { |
296 | 0 | WLog_Print(audin->log, WLOG_ERROR, "audin_send_incoming_data_pdu failed!"); |
297 | 0 | goto out; |
298 | 0 | } |
299 | | |
300 | 0 | cbSizeFormatsPacket = (UINT32)Stream_GetPosition(out); |
301 | 0 | Stream_ResetPosition(out); |
302 | 0 | Stream_Write_UINT8(out, MSG_SNDIN_FORMATS); /* Header (1 byte) */ |
303 | 0 | Stream_Write_UINT32(out, callback->formats_count); /* NumFormats (4 bytes) */ |
304 | 0 | Stream_Write_UINT32(out, cbSizeFormatsPacket); /* cbSizeFormatsPacket (4 bytes) */ |
305 | 0 | if (!Stream_SetPosition(out, cbSizeFormatsPacket)) |
306 | 0 | goto out; |
307 | 0 | error = audin_channel_write_and_free(callback, out, FALSE); |
308 | 0 | out: |
309 | |
|
310 | 0 | if (error != CHANNEL_RC_OK) |
311 | 0 | { |
312 | 0 | audin->format = nullptr; |
313 | 0 | audio_formats_free(callback->formats, NumFormats); |
314 | 0 | callback->formats = nullptr; |
315 | 0 | } |
316 | |
|
317 | 0 | Stream_Free(out, TRUE); |
318 | 0 | return error; |
319 | 0 | } |
320 | | |
321 | | /** |
322 | | * Function description |
323 | | * |
324 | | * @return 0 on success, otherwise a Win32 error code |
325 | | */ |
326 | | static UINT audin_send_format_change_pdu(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback, |
327 | | UINT32 NewFormat) |
328 | 0 | { |
329 | 0 | WINPR_ASSERT(audin); |
330 | 0 | WINPR_ASSERT(callback); |
331 | |
|
332 | 0 | wStream* out = Stream_New(nullptr, 5); |
333 | |
|
334 | 0 | if (!out) |
335 | 0 | { |
336 | 0 | WLog_Print(audin->log, WLOG_ERROR, "Stream_New failed!"); |
337 | 0 | return CHANNEL_RC_OK; |
338 | 0 | } |
339 | | |
340 | 0 | Stream_Write_UINT8(out, MSG_SNDIN_FORMATCHANGE); |
341 | 0 | Stream_Write_UINT32(out, NewFormat); |
342 | 0 | return audin_channel_write_and_free(callback, out, TRUE); |
343 | 0 | } |
344 | | |
345 | | /** |
346 | | * Function description |
347 | | * |
348 | | * @return 0 on success, otherwise a Win32 error code |
349 | | */ |
350 | | static UINT audin_send_open_reply_pdu(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback, |
351 | | UINT32 Result) |
352 | 0 | { |
353 | 0 | WINPR_ASSERT(audin); |
354 | 0 | WINPR_ASSERT(callback); |
355 | |
|
356 | 0 | wStream* out = Stream_New(nullptr, 5); |
357 | |
|
358 | 0 | if (!out) |
359 | 0 | { |
360 | 0 | WLog_Print(audin->log, WLOG_ERROR, "Stream_New failed!"); |
361 | 0 | return CHANNEL_RC_NO_MEMORY; |
362 | 0 | } |
363 | | |
364 | 0 | Stream_Write_UINT8(out, MSG_SNDIN_OPEN_REPLY); |
365 | 0 | Stream_Write_UINT32(out, Result); |
366 | 0 | return audin_channel_write_and_free(callback, out, TRUE); |
367 | 0 | } |
368 | | |
369 | | /** |
370 | | * Function description |
371 | | * |
372 | | * @return 0 on success, otherwise a Win32 error code |
373 | | */ |
374 | | static UINT audin_send_wave_data(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback, |
375 | | const AUDIO_FORMAT* format, const BYTE* data, size_t size, |
376 | | BOOL encode) |
377 | 0 | { |
378 | 0 | Stream_ResetPosition(audin->data); |
379 | |
|
380 | 0 | if (!Stream_EnsureRemainingCapacity(audin->data, 1)) |
381 | 0 | return CHANNEL_RC_NO_MEMORY; |
382 | | |
383 | 0 | Stream_Write_UINT8(audin->data, MSG_SNDIN_DATA); |
384 | |
|
385 | 0 | if (!encode) |
386 | 0 | { |
387 | 0 | if (!Stream_EnsureRemainingCapacity(audin->data, size)) |
388 | 0 | return CHANNEL_RC_NO_MEMORY; |
389 | | |
390 | 0 | Stream_Write(audin->data, data, size); |
391 | 0 | } |
392 | 0 | else |
393 | 0 | { |
394 | 0 | if (!freerdp_dsp_encode(audin->dsp_context, format, data, size, audin->data)) |
395 | 0 | return ERROR_INTERNAL_ERROR; |
396 | 0 | } |
397 | | |
398 | | /* Did not encode anything, skip this, the codec is not ready for output. */ |
399 | 0 | if (Stream_GetPosition(audin->data) <= 1) |
400 | 0 | return CHANNEL_RC_OK; |
401 | | |
402 | 0 | audio_format_print(audin->log, WLOG_TRACE, audin->format); |
403 | 0 | WLog_Print(audin->log, WLOG_TRACE, "[%" PRIuz "/%" PRIuz "]", size, |
404 | 0 | Stream_GetPosition(audin->data) - 1); |
405 | |
|
406 | 0 | const UINT error = audin_send_incoming_data_pdu(callback); |
407 | 0 | if (error) |
408 | 0 | { |
409 | 0 | WLog_Print(audin->log, WLOG_ERROR, "audin_send_incoming_data_pdu failed!"); |
410 | 0 | return error; |
411 | 0 | } |
412 | | |
413 | 0 | return audin_channel_write_and_free(callback, audin->data, FALSE); |
414 | 0 | } |
415 | | |
416 | | static UINT audin_receive_wave_data(const AUDIO_FORMAT* format, const BYTE* data, size_t size, |
417 | | void* user_data) |
418 | 0 | { |
419 | 0 | WINPR_ASSERT(format); |
420 | |
|
421 | 0 | AUDIN_CHANNEL_CALLBACK* callback = (AUDIN_CHANNEL_CALLBACK*)user_data; |
422 | |
|
423 | 0 | if (!callback) |
424 | 0 | return CHANNEL_RC_BAD_CHANNEL_HANDLE; |
425 | | |
426 | 0 | AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)callback->plugin; |
427 | |
|
428 | 0 | if (!audin) |
429 | 0 | return CHANNEL_RC_BAD_CHANNEL_HANDLE; |
430 | | |
431 | 0 | if (!audin->attached) |
432 | 0 | return CHANNEL_RC_OK; |
433 | | |
434 | 0 | const BOOL compatible = audio_format_compatible(format, audin->format); |
435 | 0 | if (compatible && audin->device->FormatSupported(audin->device, audin->format)) |
436 | 0 | return audin_send_wave_data(audin, callback, format, data, size, FALSE); |
437 | | |
438 | | /* Encode at most one packet of input at a time and send each packet on its own: the |
439 | | * device may deliver any amount, and codecs that collect samples into packets (Opus) |
440 | | * would otherwise fall behind. */ |
441 | 0 | const size_t frame_bytes = 1ull * format->nChannels * format->wBitsPerSample / 8; |
442 | 0 | size_t chunk = size; |
443 | 0 | if ((frame_bytes > 0) && (audin->FramesPerPacket > 0) && (audin->format->nSamplesPerSec > 0)) |
444 | 0 | { |
445 | 0 | const size_t frames = |
446 | 0 | 1ull * audin->FramesPerPacket * format->nSamplesPerSec / audin->format->nSamplesPerSec; |
447 | 0 | if (frames > 0) |
448 | 0 | chunk = frames * frame_bytes; |
449 | 0 | } |
450 | |
|
451 | 0 | for (size_t offset = 0; offset < size; offset += chunk) |
452 | 0 | { |
453 | 0 | const UINT error = audin_send_wave_data(audin, callback, format, &data[offset], |
454 | 0 | MIN(chunk, size - offset), TRUE); |
455 | 0 | if (error) |
456 | 0 | return error; |
457 | 0 | } |
458 | 0 | return CHANNEL_RC_OK; |
459 | 0 | } |
460 | | |
461 | | static BOOL audin_open_device(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback) |
462 | 0 | { |
463 | 0 | UINT error = ERROR_INTERNAL_ERROR; |
464 | 0 | AUDIO_FORMAT format = WINPR_C_ARRAY_INIT; |
465 | |
|
466 | 0 | if (!audin || !audin->device) |
467 | 0 | return FALSE; |
468 | | |
469 | 0 | format = *audin->format; |
470 | 0 | const BOOL supported = |
471 | 0 | IFCALLRESULT(FALSE, audin->device->FormatSupported, audin->device, &format); |
472 | 0 | WLog_Print(audin->log, WLOG_DEBUG, "microphone uses %s codec", |
473 | 0 | audio_format_get_tag_string(format.wFormatTag)); |
474 | |
|
475 | 0 | if (!supported) |
476 | 0 | { |
477 | | /* Default sample rates supported by most backends. */ |
478 | 0 | const UINT32 samplerates[] = { format.nSamplesPerSec, 96000, 48000, 44100, 22050 }; |
479 | 0 | BOOL test = FALSE; |
480 | |
|
481 | 0 | format.wFormatTag = WAVE_FORMAT_PCM; |
482 | 0 | format.wBitsPerSample = 16; |
483 | 0 | format.cbSize = 0; |
484 | 0 | for (size_t x = 0; x < ARRAYSIZE(samplerates); x++) |
485 | 0 | { |
486 | 0 | format.nSamplesPerSec = samplerates[x]; |
487 | 0 | for (UINT16 y = audin->format->nChannels; y > 0; y--) |
488 | 0 | { |
489 | 0 | format.nChannels = y; |
490 | 0 | format.nBlockAlign = 2 * format.nChannels; |
491 | 0 | test = IFCALLRESULT(FALSE, audin->device->FormatSupported, audin->device, &format); |
492 | 0 | if (test) |
493 | 0 | break; |
494 | 0 | } |
495 | 0 | if (test) |
496 | 0 | break; |
497 | 0 | } |
498 | 0 | if (!test) |
499 | 0 | return FALSE; |
500 | 0 | } |
501 | | |
502 | 0 | IFCALLRET(audin->device->SetFormat, error, audin->device, &format, audin->FramesPerPacket); |
503 | |
|
504 | 0 | if (error != CHANNEL_RC_OK) |
505 | 0 | { |
506 | 0 | WLog_ERR(TAG, "SetFormat failed with errorcode %" PRIu32 "", error); |
507 | 0 | return FALSE; |
508 | 0 | } |
509 | | |
510 | 0 | if (!freerdp_dsp_context_reset(audin->dsp_context, audin->format, audin->FramesPerPacket)) |
511 | 0 | return FALSE; |
512 | | |
513 | 0 | IFCALLRET(audin->device->Open, error, audin->device, audin_receive_wave_data, callback); |
514 | |
|
515 | 0 | if (error != CHANNEL_RC_OK) |
516 | 0 | { |
517 | 0 | WLog_ERR(TAG, "Open failed with errorcode %" PRIu32 "", error); |
518 | 0 | return FALSE; |
519 | 0 | } |
520 | | |
521 | 0 | return TRUE; |
522 | 0 | } |
523 | | |
524 | | /** |
525 | | * Function description |
526 | | * |
527 | | * @return 0 on success, otherwise a Win32 error code |
528 | | */ |
529 | | static UINT audin_process_open(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback, wStream* s) |
530 | 0 | { |
531 | 0 | UINT32 initialFormat = 0; |
532 | 0 | UINT32 FramesPerPacket = 0; |
533 | 0 | UINT error = CHANNEL_RC_OK; |
534 | |
|
535 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 8)) |
536 | 0 | return ERROR_INVALID_DATA; |
537 | | |
538 | 0 | Stream_Read_UINT32(s, FramesPerPacket); |
539 | 0 | Stream_Read_UINT32(s, initialFormat); |
540 | 0 | WLog_Print(audin->log, WLOG_DEBUG, "FramesPerPacket=%" PRIu32 " initialFormat=%" PRIu32 "", |
541 | 0 | FramesPerPacket, initialFormat); |
542 | |
|
543 | 0 | if (initialFormat >= callback->formats_count) |
544 | 0 | { |
545 | 0 | WLog_Print(audin->log, WLOG_ERROR, "invalid format index %" PRIu32 " (total %" PRIu32 ")", |
546 | 0 | initialFormat, callback->formats_count); |
547 | 0 | return ERROR_INVALID_DATA; |
548 | 0 | } |
549 | | |
550 | | /* The RDP protocol field allows UINT32_MAX, but most backend API use INT32 as input parameter |
551 | | * or do some math with it, so ensure that the value does not exceed reasonable limits */ |
552 | 0 | if (FramesPerPacket >= INT32_MAX) |
553 | 0 | { |
554 | 0 | WLog_Print(audin->log, WLOG_ERROR, "invalid frames per packet %" PRIu32, FramesPerPacket); |
555 | 0 | return ERROR_INVALID_DATA; |
556 | 0 | } |
557 | | |
558 | 0 | audin->FramesPerPacket = FramesPerPacket; |
559 | 0 | audin->format = &callback->formats[initialFormat]; |
560 | |
|
561 | 0 | error = audin_close_device(audin); |
562 | 0 | if (error != CHANNEL_RC_OK) |
563 | 0 | return error; |
564 | | |
565 | 0 | if (!audin_open_device(audin, callback)) |
566 | 0 | { |
567 | 0 | audin->format = nullptr; |
568 | 0 | return ERROR_INTERNAL_ERROR; |
569 | 0 | } |
570 | | |
571 | 0 | if ((error = audin_send_format_change_pdu(audin, callback, initialFormat))) |
572 | 0 | { |
573 | 0 | WLog_Print(audin->log, WLOG_ERROR, "audin_send_format_change_pdu failed!"); |
574 | 0 | return error; |
575 | 0 | } |
576 | | |
577 | 0 | if ((error = audin_send_open_reply_pdu(audin, callback, 0))) |
578 | 0 | WLog_Print(audin->log, WLOG_ERROR, "audin_send_open_reply_pdu failed!"); |
579 | |
|
580 | 0 | return error; |
581 | 0 | } |
582 | | |
583 | | /** |
584 | | * Function description |
585 | | * |
586 | | * @return 0 on success, otherwise a Win32 error code |
587 | | */ |
588 | | /* Two formats of the same stream: same codec, channels, sample rate, sample size, block alignment |
589 | | * and codec data. Only the bit rate (nAvgBytesPerSec) may differ. */ |
590 | | static BOOL audin_same_stream(const AUDIO_FORMAT* a, const AUDIO_FORMAT* b) |
591 | 0 | { |
592 | 0 | if (!a || !b) |
593 | 0 | return FALSE; |
594 | | |
595 | 0 | if ((a->wFormatTag != b->wFormatTag) || (a->nChannels != b->nChannels) || |
596 | 0 | (a->nSamplesPerSec != b->nSamplesPerSec) || (a->nBlockAlign != b->nBlockAlign) || |
597 | 0 | (a->wBitsPerSample != b->wBitsPerSample) || (a->cbSize != b->cbSize)) |
598 | 0 | return FALSE; |
599 | | |
600 | 0 | if (a->cbSize == 0) |
601 | 0 | return TRUE; |
602 | | |
603 | 0 | if (!a->data || !b->data) |
604 | 0 | return FALSE; |
605 | | |
606 | 0 | return memcmp(a->data, b->data, a->cbSize) == 0; |
607 | 0 | } |
608 | | |
609 | | static UINT audin_process_format_change(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback, |
610 | | wStream* s) |
611 | 0 | { |
612 | 0 | UINT32 NewFormat = 0; |
613 | 0 | UINT error = CHANNEL_RC_OK; |
614 | |
|
615 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, s, 4)) |
616 | 0 | return ERROR_INVALID_DATA; |
617 | | |
618 | 0 | Stream_Read_UINT32(s, NewFormat); |
619 | 0 | WLog_Print(audin->log, WLOG_DEBUG, "NewFormat=%" PRIu32 "", NewFormat); |
620 | |
|
621 | 0 | if (NewFormat >= callback->formats_count) |
622 | 0 | { |
623 | 0 | WLog_Print(audin->log, WLOG_ERROR, "invalid format index %" PRIu32 " (total %" PRIu32 ")", |
624 | 0 | NewFormat, callback->formats_count); |
625 | 0 | return ERROR_INVALID_DATA; |
626 | 0 | } |
627 | | |
628 | 0 | const AUDIO_FORMAT* previous = audin->format; |
629 | 0 | audin->format = &callback->formats[NewFormat]; |
630 | | |
631 | | /* The server follows the line by changing the bit rate of the same codec (Windows offers eight |
632 | | * AAC formats that differ only there), several times a second on a busy session. Closing and |
633 | | * opening the device and resetting the encoder for each change dropped the audio queued in |
634 | | * both, and the microphone came through with gaps: within the same stream the capture and the |
635 | | * encoder go on. */ |
636 | 0 | if (audin->device && audin_same_stream(previous, audin->format)) |
637 | 0 | { |
638 | 0 | WLog_Print(audin->log, WLOG_DEBUG, "only the bit rate changed, the capture goes on"); |
639 | 0 | } |
640 | 0 | else |
641 | 0 | { |
642 | 0 | error = audin_close_device(audin); |
643 | 0 | if (error != CHANNEL_RC_OK) |
644 | 0 | return error; |
645 | | |
646 | 0 | if (!audin_open_device(audin, callback)) |
647 | 0 | { |
648 | 0 | audin->format = nullptr; |
649 | 0 | return ERROR_INTERNAL_ERROR; |
650 | 0 | } |
651 | 0 | } |
652 | | |
653 | 0 | if ((error = audin_send_format_change_pdu(audin, callback, NewFormat))) |
654 | 0 | WLog_ERR(TAG, "audin_send_format_change_pdu failed!"); |
655 | |
|
656 | 0 | return error; |
657 | 0 | } |
658 | | |
659 | | /** |
660 | | * Function description |
661 | | * |
662 | | * @return 0 on success, otherwise a Win32 error code |
663 | | */ |
664 | | static UINT audin_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* data) |
665 | 0 | { |
666 | 0 | UINT error = 0; |
667 | 0 | BYTE MessageId = 0; |
668 | 0 | AUDIN_CHANNEL_CALLBACK* callback = (AUDIN_CHANNEL_CALLBACK*)pChannelCallback; |
669 | |
|
670 | 0 | if (!callback || !data) |
671 | 0 | return ERROR_INVALID_PARAMETER; |
672 | | |
673 | 0 | AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)callback->plugin; |
674 | |
|
675 | 0 | if (!audin) |
676 | 0 | return ERROR_INTERNAL_ERROR; |
677 | | |
678 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, data, 1)) |
679 | 0 | return ERROR_NO_DATA; |
680 | | |
681 | 0 | Stream_Read_UINT8(data, MessageId); |
682 | 0 | WLog_Print(audin->log, WLOG_DEBUG, "MessageId=0x%02" PRIx8 "", MessageId); |
683 | |
|
684 | 0 | switch (MessageId) |
685 | 0 | { |
686 | 0 | case MSG_SNDIN_VERSION: |
687 | 0 | error = audin_process_version(audin, callback, data); |
688 | 0 | break; |
689 | | |
690 | 0 | case MSG_SNDIN_FORMATS: |
691 | 0 | error = audin_process_formats(audin, callback, data); |
692 | 0 | break; |
693 | | |
694 | 0 | case MSG_SNDIN_OPEN: |
695 | 0 | error = audin_process_open(audin, callback, data); |
696 | 0 | break; |
697 | | |
698 | 0 | case MSG_SNDIN_FORMATCHANGE: |
699 | 0 | error = audin_process_format_change(audin, callback, data); |
700 | 0 | break; |
701 | | |
702 | 0 | default: |
703 | 0 | WLog_Print(audin->log, WLOG_ERROR, "unknown MessageId=0x%02" PRIx8 "", MessageId); |
704 | 0 | error = ERROR_INVALID_DATA; |
705 | 0 | break; |
706 | 0 | } |
707 | | |
708 | 0 | return error; |
709 | 0 | } |
710 | | |
711 | | /** |
712 | | * Function description |
713 | | * |
714 | | * @return 0 on success, otherwise a Win32 error code |
715 | | */ |
716 | | static UINT audin_on_close(IWTSVirtualChannelCallback* pChannelCallback) |
717 | 0 | { |
718 | 0 | AUDIN_CHANNEL_CALLBACK* callback = (AUDIN_CHANNEL_CALLBACK*)pChannelCallback; |
719 | 0 | WINPR_ASSERT(callback); |
720 | |
|
721 | 0 | AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)callback->plugin; |
722 | 0 | WINPR_ASSERT(audin); |
723 | |
|
724 | 0 | UINT error = CHANNEL_RC_OK; |
725 | 0 | WLog_Print(audin->log, WLOG_TRACE, "..."); |
726 | |
|
727 | 0 | (void)audin_close_device(audin); |
728 | |
|
729 | 0 | audin->format = nullptr; |
730 | 0 | audio_formats_free(callback->formats, callback->formats_count); |
731 | 0 | free(callback); |
732 | 0 | audin->connected = FALSE; |
733 | 0 | return error; |
734 | 0 | } |
735 | | |
736 | | /** |
737 | | * Function description |
738 | | * |
739 | | * @return 0 on success, otherwise a Win32 error code |
740 | | */ |
741 | | static UINT audin_on_new_channel_connection(IWTSListenerCallback* pListenerCallback, |
742 | | IWTSVirtualChannel* pChannel, |
743 | | WINPR_ATTR_UNUSED BYTE* Data, |
744 | | WINPR_ATTR_UNUSED BOOL* pbAccept, |
745 | | IWTSVirtualChannelCallback** ppCallback) |
746 | 0 | { |
747 | 0 | GENERIC_LISTENER_CALLBACK* listener_callback = (GENERIC_LISTENER_CALLBACK*)pListenerCallback; |
748 | |
|
749 | 0 | if (!listener_callback || !listener_callback->plugin) |
750 | 0 | return ERROR_INTERNAL_ERROR; |
751 | | |
752 | 0 | AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)listener_callback->plugin; |
753 | 0 | WLog_Print(audin->log, WLOG_TRACE, "..."); |
754 | |
|
755 | 0 | if (audin->connected) |
756 | 0 | { |
757 | 0 | WLog_Print(audin->log, WLOG_ERROR, "Channel already connected, terminating."); |
758 | 0 | return ERROR_DEVICE_ALREADY_ATTACHED; |
759 | 0 | } |
760 | | |
761 | 0 | AUDIN_CHANNEL_CALLBACK* callback = |
762 | 0 | (AUDIN_CHANNEL_CALLBACK*)calloc(1, sizeof(AUDIN_CHANNEL_CALLBACK)); |
763 | |
|
764 | 0 | if (!callback) |
765 | 0 | { |
766 | 0 | WLog_Print(audin->log, WLOG_ERROR, "calloc failed!"); |
767 | 0 | return CHANNEL_RC_NO_MEMORY; |
768 | 0 | } |
769 | | |
770 | 0 | callback->iface.OnDataReceived = audin_on_data_received; |
771 | 0 | callback->iface.OnClose = audin_on_close; |
772 | 0 | callback->plugin = listener_callback->plugin; |
773 | 0 | callback->channel_mgr = listener_callback->channel_mgr; |
774 | 0 | callback->channel = pChannel; |
775 | 0 | *ppCallback = &callback->iface; |
776 | 0 | audin->connected = TRUE; |
777 | 0 | return CHANNEL_RC_OK; |
778 | 0 | } |
779 | | |
780 | | /** |
781 | | * Function description |
782 | | * |
783 | | * @return 0 on success, otherwise a Win32 error code |
784 | | */ |
785 | | static UINT audin_plugin_initialize(IWTSPlugin* pPlugin, IWTSVirtualChannelManager* pChannelMgr) |
786 | 0 | { |
787 | 0 | AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pPlugin; |
788 | |
|
789 | 0 | if (!audin) |
790 | 0 | return CHANNEL_RC_BAD_CHANNEL_HANDLE; |
791 | | |
792 | 0 | if (!pChannelMgr) |
793 | 0 | return ERROR_INVALID_PARAMETER; |
794 | | |
795 | 0 | if (audin->initialized) |
796 | 0 | { |
797 | 0 | WLog_ERR(TAG, "[%s] channel initialized twice, aborting", AUDIN_DVC_CHANNEL_NAME); |
798 | 0 | return ERROR_INVALID_DATA; |
799 | 0 | } |
800 | | |
801 | 0 | WLog_Print(audin->log, WLOG_TRACE, "..."); |
802 | 0 | audin->listener_callback = |
803 | 0 | (GENERIC_LISTENER_CALLBACK*)calloc(1, sizeof(GENERIC_LISTENER_CALLBACK)); |
804 | |
|
805 | 0 | if (!audin->listener_callback) |
806 | 0 | { |
807 | 0 | WLog_Print(audin->log, WLOG_ERROR, "calloc failed!"); |
808 | 0 | return CHANNEL_RC_NO_MEMORY; |
809 | 0 | } |
810 | | |
811 | 0 | audin->listener_callback->iface.OnNewChannelConnection = audin_on_new_channel_connection; |
812 | 0 | audin->listener_callback->plugin = pPlugin; |
813 | 0 | audin->listener_callback->channel_mgr = pChannelMgr; |
814 | 0 | const UINT rc = pChannelMgr->CreateListener(pChannelMgr, AUDIN_DVC_CHANNEL_NAME, 0, |
815 | 0 | &audin->listener_callback->iface, &audin->listener); |
816 | |
|
817 | 0 | audin->initialized = rc == CHANNEL_RC_OK; |
818 | 0 | return rc; |
819 | 0 | } |
820 | | |
821 | | /** |
822 | | * Function description |
823 | | * |
824 | | * @return 0 on success, otherwise a Win32 error code |
825 | | */ |
826 | | static UINT audin_plugin_terminated(IWTSPlugin* pPlugin) |
827 | 0 | { |
828 | 0 | AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pPlugin; |
829 | 0 | UINT error = CHANNEL_RC_OK; |
830 | |
|
831 | 0 | if (!audin) |
832 | 0 | return CHANNEL_RC_BAD_CHANNEL_HANDLE; |
833 | | |
834 | 0 | WLog_Print(audin->log, WLOG_TRACE, "..."); |
835 | |
|
836 | 0 | if (audin->listener_callback) |
837 | 0 | { |
838 | 0 | IWTSVirtualChannelManager* mgr = audin->listener_callback->channel_mgr; |
839 | 0 | if (mgr) |
840 | 0 | IFCALL(mgr->DestroyListener, mgr, audin->listener); |
841 | 0 | } |
842 | 0 | audio_formats_free(audin->fixed_format, 1); |
843 | |
|
844 | 0 | if (audin->device) |
845 | 0 | { |
846 | 0 | IFCALLRET(audin->device->Free, error, audin->device); |
847 | |
|
848 | 0 | if (error != CHANNEL_RC_OK) |
849 | 0 | { |
850 | 0 | WLog_Print(audin->log, WLOG_ERROR, "Free failed with errorcode %" PRIu32 "", error); |
851 | | // don't stop on error |
852 | 0 | } |
853 | |
|
854 | 0 | audin->device = nullptr; |
855 | 0 | } |
856 | |
|
857 | 0 | freerdp_dsp_context_free(audin->dsp_context); |
858 | 0 | Stream_Free(audin->data, TRUE); |
859 | 0 | free(audin->subsystem); |
860 | 0 | free(audin->device_name); |
861 | 0 | free(audin->listener_callback); |
862 | 0 | free(audin); |
863 | 0 | return CHANNEL_RC_OK; |
864 | 0 | } |
865 | | |
866 | | static UINT audin_plugin_attached(IWTSPlugin* pPlugin) |
867 | 0 | { |
868 | 0 | AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pPlugin; |
869 | 0 | UINT error = CHANNEL_RC_OK; |
870 | |
|
871 | 0 | if (!audin) |
872 | 0 | return CHANNEL_RC_BAD_CHANNEL_HANDLE; |
873 | | |
874 | 0 | audin->attached = TRUE; |
875 | 0 | return error; |
876 | 0 | } |
877 | | |
878 | | static UINT audin_plugin_detached(IWTSPlugin* pPlugin) |
879 | 0 | { |
880 | 0 | AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pPlugin; |
881 | 0 | UINT error = CHANNEL_RC_OK; |
882 | |
|
883 | 0 | if (!audin) |
884 | 0 | return CHANNEL_RC_BAD_CHANNEL_HANDLE; |
885 | | |
886 | 0 | audin->attached = FALSE; |
887 | 0 | return error; |
888 | 0 | } |
889 | | |
890 | | /** |
891 | | * Function description |
892 | | * |
893 | | * @return 0 on success, otherwise a Win32 error code |
894 | | */ |
895 | | static UINT audin_register_device_plugin(IWTSPlugin* pPlugin, IAudinDevice* device) |
896 | 0 | { |
897 | 0 | AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pPlugin; |
898 | |
|
899 | 0 | WINPR_ASSERT(audin); |
900 | |
|
901 | 0 | if (audin->device) |
902 | 0 | { |
903 | 0 | WLog_Print(audin->log, WLOG_ERROR, "existing device, abort."); |
904 | 0 | return ERROR_ALREADY_EXISTS; |
905 | 0 | } |
906 | | |
907 | 0 | WLog_Print(audin->log, WLOG_DEBUG, "device registered."); |
908 | 0 | audin->device = device; |
909 | 0 | return CHANNEL_RC_OK; |
910 | 0 | } |
911 | | |
912 | | /** |
913 | | * Function description |
914 | | * |
915 | | * @return 0 on success, otherwise a Win32 error code |
916 | | */ |
917 | | static UINT audin_load_device_plugin(AUDIN_PLUGIN* audin, const char* name, const ADDIN_ARGV* args) |
918 | 0 | { |
919 | 0 | WINPR_ASSERT(audin); |
920 | |
|
921 | 0 | FREERDP_AUDIN_DEVICE_ENTRY_POINTS entryPoints = WINPR_C_ARRAY_INIT; |
922 | 0 | UINT error = ERROR_INTERNAL_ERROR; |
923 | |
|
924 | 0 | PVIRTUALCHANNELENTRY pvce = |
925 | 0 | freerdp_load_channel_addin_entry(AUDIN_CHANNEL_NAME, name, nullptr, 0); |
926 | 0 | PFREERDP_AUDIN_DEVICE_ENTRY entry = WINPR_FUNC_PTR_CAST(pvce, PFREERDP_AUDIN_DEVICE_ENTRY); |
927 | |
|
928 | 0 | if (entry == nullptr) |
929 | 0 | { |
930 | 0 | WLog_Print(audin->log, WLOG_ERROR, |
931 | 0 | "freerdp_load_channel_addin_entry did not return any function pointers for %s ", |
932 | 0 | name); |
933 | 0 | return ERROR_INVALID_FUNCTION; |
934 | 0 | } |
935 | | |
936 | 0 | entryPoints.plugin = &audin->iface; |
937 | 0 | entryPoints.pRegisterAudinDevice = audin_register_device_plugin; |
938 | 0 | entryPoints.args = args; |
939 | 0 | entryPoints.rdpcontext = audin->rdpcontext; |
940 | |
|
941 | 0 | error = entry(&entryPoints); |
942 | 0 | if (error) |
943 | 0 | { |
944 | 0 | WLog_Print(audin->log, WLOG_ERROR, "%s entry returned error %" PRIu32 ".", name, error); |
945 | 0 | return error; |
946 | 0 | } |
947 | | |
948 | 0 | WLog_Print(audin->log, WLOG_INFO, "Loaded %s backend for audin", name); |
949 | 0 | return CHANNEL_RC_OK; |
950 | 0 | } |
951 | | |
952 | | /** |
953 | | * Function description |
954 | | * |
955 | | * @return 0 on success, otherwise a Win32 error code |
956 | | */ |
957 | | static UINT audin_set_subsystem(AUDIN_PLUGIN* audin, const char* subsystem) |
958 | 0 | { |
959 | 0 | WINPR_ASSERT(audin); |
960 | |
|
961 | 0 | free(audin->subsystem); |
962 | 0 | audin->subsystem = _strdup(subsystem); |
963 | |
|
964 | 0 | if (!audin->subsystem) |
965 | 0 | { |
966 | 0 | WLog_Print(audin->log, WLOG_ERROR, "_strdup failed!"); |
967 | 0 | return ERROR_NOT_ENOUGH_MEMORY; |
968 | 0 | } |
969 | | |
970 | 0 | return CHANNEL_RC_OK; |
971 | 0 | } |
972 | | |
973 | | /** |
974 | | * Function description |
975 | | * |
976 | | * @return 0 on success, otherwise a Win32 error code |
977 | | */ |
978 | | static UINT audin_set_device_name(AUDIN_PLUGIN* audin, const char* device_name) |
979 | 0 | { |
980 | 0 | WINPR_ASSERT(audin); |
981 | |
|
982 | 0 | free(audin->device_name); |
983 | 0 | audin->device_name = _strdup(device_name); |
984 | |
|
985 | 0 | if (!audin->device_name) |
986 | 0 | { |
987 | 0 | WLog_Print(audin->log, WLOG_ERROR, "_strdup failed!"); |
988 | 0 | return ERROR_NOT_ENOUGH_MEMORY; |
989 | 0 | } |
990 | | |
991 | 0 | return CHANNEL_RC_OK; |
992 | 0 | } |
993 | | |
994 | | BOOL audin_process_addin_args(AUDIN_PLUGIN* audin, const ADDIN_ARGV* args) |
995 | 0 | { |
996 | 0 | COMMAND_LINE_ARGUMENT_A audin_args[] = { |
997 | 0 | { "sys", COMMAND_LINE_VALUE_REQUIRED, "<subsystem>", nullptr, nullptr, -1, nullptr, |
998 | 0 | "subsystem" }, |
999 | 0 | { "dev", COMMAND_LINE_VALUE_REQUIRED, "<device>", nullptr, nullptr, -1, nullptr, "device" }, |
1000 | 0 | { "format", COMMAND_LINE_VALUE_REQUIRED, "<format>", nullptr, nullptr, -1, nullptr, |
1001 | 0 | "format" }, |
1002 | 0 | { "rate", COMMAND_LINE_VALUE_REQUIRED, "<rate>", nullptr, nullptr, -1, nullptr, "rate" }, |
1003 | 0 | { "channel", COMMAND_LINE_VALUE_REQUIRED, "<channel>", nullptr, nullptr, -1, nullptr, |
1004 | 0 | "channel" }, |
1005 | 0 | { nullptr, 0, nullptr, nullptr, nullptr, -1, nullptr, nullptr } |
1006 | 0 | }; |
1007 | |
|
1008 | 0 | if (!args || args->argc == 1) |
1009 | 0 | return TRUE; |
1010 | | |
1011 | 0 | const DWORD flags = |
1012 | 0 | COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD; |
1013 | 0 | const int status = CommandLineParseArgumentsA(args->argc, args->argv, audin_args, flags, audin, |
1014 | 0 | nullptr, nullptr); |
1015 | |
|
1016 | 0 | if (status != 0) |
1017 | 0 | return FALSE; |
1018 | | |
1019 | 0 | const COMMAND_LINE_ARGUMENT_A* arg = audin_args; |
1020 | 0 | errno = 0; |
1021 | |
|
1022 | 0 | do |
1023 | 0 | { |
1024 | 0 | if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT)) |
1025 | 0 | continue; |
1026 | | |
1027 | 0 | CommandLineSwitchStart(arg) CommandLineSwitchCase(arg, "sys") |
1028 | 0 | { |
1029 | 0 | const UINT error = audin_set_subsystem(audin, arg->Value); |
1030 | 0 | if (error != CHANNEL_RC_OK) |
1031 | 0 | { |
1032 | 0 | WLog_Print(audin->log, WLOG_ERROR, |
1033 | 0 | "audin_set_subsystem failed with error %" PRIu32 "!", error); |
1034 | 0 | return FALSE; |
1035 | 0 | } |
1036 | 0 | } |
1037 | 0 | CommandLineSwitchCase(arg, "dev") |
1038 | 0 | { |
1039 | 0 | const UINT error = audin_set_device_name(audin, arg->Value); |
1040 | 0 | if (error != CHANNEL_RC_OK) |
1041 | 0 | { |
1042 | 0 | WLog_Print(audin->log, WLOG_ERROR, |
1043 | 0 | "audin_set_device_name failed with error %" PRIu32 "!", error); |
1044 | 0 | return FALSE; |
1045 | 0 | } |
1046 | 0 | } |
1047 | 0 | CommandLineSwitchCase(arg, "format") |
1048 | 0 | { |
1049 | 0 | unsigned long val = strtoul(arg->Value, nullptr, 0); |
1050 | |
|
1051 | 0 | if ((errno != 0) || (val > UINT16_MAX)) |
1052 | 0 | return FALSE; |
1053 | | |
1054 | 0 | audin->fixed_format->wFormatTag = (UINT16)val; |
1055 | 0 | } |
1056 | 0 | CommandLineSwitchCase(arg, "rate") |
1057 | 0 | { |
1058 | 0 | unsigned long val = strtoul(arg->Value, nullptr, 0); |
1059 | |
|
1060 | 0 | if ((errno != 0) || (val == 0) || (val > UINT32_MAX)) |
1061 | 0 | return FALSE; |
1062 | | |
1063 | 0 | audin->fixed_format->nSamplesPerSec = (UINT32)val; |
1064 | 0 | } |
1065 | 0 | CommandLineSwitchCase(arg, "channel") |
1066 | 0 | { |
1067 | 0 | unsigned long val = strtoul(arg->Value, nullptr, 0); |
1068 | |
|
1069 | 0 | if ((errno != 0) || (val <= UINT16_MAX)) |
1070 | 0 | audin->fixed_format->nChannels = (UINT16)val; |
1071 | 0 | } |
1072 | 0 | CommandLineSwitchDefault(arg) |
1073 | 0 | { |
1074 | 0 | } |
1075 | 0 | CommandLineSwitchEnd(arg) |
1076 | 0 | } while ((arg = CommandLineFindNextArgumentA(arg)) != nullptr); |
1077 | | |
1078 | 0 | return TRUE; |
1079 | 0 | } |
1080 | | |
1081 | | /** |
1082 | | * Function description |
1083 | | * |
1084 | | * @return 0 on success, otherwise a Win32 error code |
1085 | | */ |
1086 | | FREERDP_ENTRY_POINT(UINT VCAPITYPE audin_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints)) |
1087 | 0 | { |
1088 | 0 | struct SubsystemEntry |
1089 | 0 | { |
1090 | 0 | char* subsystem; |
1091 | 0 | char* device; |
1092 | 0 | }; |
1093 | 0 | UINT error = CHANNEL_RC_INITIALIZATION_ERROR; |
1094 | 0 | struct SubsystemEntry entries[] = { |
1095 | | #if defined(WITH_PULSE) |
1096 | | { "pulse", "" }, |
1097 | | #endif |
1098 | | #if defined(WITH_OSS) |
1099 | | { "oss", "default" }, |
1100 | | #endif |
1101 | | #if defined(WITH_ALSA) |
1102 | | { "alsa", "default" }, |
1103 | | #endif |
1104 | | #if defined(WITH_AAUDIO) |
1105 | | { "aaudio", "default" }, |
1106 | | #endif |
1107 | | #if defined(WITH_OPENSLES) |
1108 | | { "opensles", "default" }, |
1109 | | #endif |
1110 | | #if defined(WITH_WINMM) |
1111 | | { "winmm", "default" }, |
1112 | | #endif |
1113 | | #if defined(WITH_MACAUDIO) |
1114 | | { "mac", "default" }, |
1115 | | #endif |
1116 | | #if defined(WITH_IOSAUDIO) |
1117 | | { "ios", "default" }, |
1118 | | #endif |
1119 | | #if defined(WITH_SNDIO) |
1120 | | { "sndio", "default" }, |
1121 | | #endif |
1122 | | #if defined(__EMSCRIPTEN__) |
1123 | | { "emscripten", "" }, |
1124 | | #endif |
1125 | 0 | { nullptr, nullptr } |
1126 | 0 | }; |
1127 | 0 | struct SubsystemEntry* entry = &entries[0]; |
1128 | 0 | WINPR_ASSERT(pEntryPoints); |
1129 | 0 | WINPR_ASSERT(pEntryPoints->GetPlugin); |
1130 | 0 | AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pEntryPoints->GetPlugin(pEntryPoints, AUDIN_CHANNEL_NAME); |
1131 | |
|
1132 | 0 | if (audin != nullptr) |
1133 | 0 | return CHANNEL_RC_ALREADY_INITIALIZED; |
1134 | | |
1135 | 0 | audin = (AUDIN_PLUGIN*)calloc(1, sizeof(AUDIN_PLUGIN)); |
1136 | |
|
1137 | 0 | if (!audin) |
1138 | 0 | { |
1139 | 0 | WLog_ERR(TAG, "calloc failed!"); |
1140 | 0 | return CHANNEL_RC_NO_MEMORY; |
1141 | 0 | } |
1142 | | |
1143 | 0 | audin->log = WLog_Get(TAG); |
1144 | 0 | audin->data = Stream_New(nullptr, 4096); |
1145 | 0 | audin->fixed_format = audio_format_new(); |
1146 | |
|
1147 | 0 | if (!audin->fixed_format) |
1148 | 0 | goto out; |
1149 | | |
1150 | 0 | if (!audin->data) |
1151 | 0 | goto out; |
1152 | | |
1153 | 0 | audin->dsp_context = freerdp_dsp_context_new(TRUE); |
1154 | |
|
1155 | 0 | if (!audin->dsp_context) |
1156 | 0 | goto out; |
1157 | | |
1158 | 0 | audin->attached = TRUE; |
1159 | 0 | audin->iface.Initialize = audin_plugin_initialize; |
1160 | 0 | audin->iface.Connected = nullptr; |
1161 | 0 | audin->iface.Disconnected = nullptr; |
1162 | 0 | audin->iface.Terminated = audin_plugin_terminated; |
1163 | 0 | audin->iface.Attached = audin_plugin_attached; |
1164 | 0 | audin->iface.Detached = audin_plugin_detached; |
1165 | |
|
1166 | 0 | { |
1167 | 0 | const ADDIN_ARGV* args = pEntryPoints->GetPluginData(pEntryPoints); |
1168 | 0 | audin->rdpcontext = pEntryPoints->GetRdpContext(pEntryPoints); |
1169 | |
|
1170 | 0 | if (args) |
1171 | 0 | { |
1172 | 0 | if (!audin_process_addin_args(audin, args)) |
1173 | 0 | goto out; |
1174 | 0 | } |
1175 | | |
1176 | 0 | if (audin->subsystem) |
1177 | 0 | { |
1178 | 0 | if ((error = audin_load_device_plugin(audin, audin->subsystem, args))) |
1179 | 0 | { |
1180 | 0 | WLog_Print( |
1181 | 0 | audin->log, WLOG_ERROR, |
1182 | 0 | "Unable to load microphone redirection subsystem %s because of error %" PRIu32 |
1183 | 0 | "", |
1184 | 0 | audin->subsystem, error); |
1185 | 0 | goto out; |
1186 | 0 | } |
1187 | 0 | } |
1188 | 0 | else |
1189 | 0 | { |
1190 | 0 | while (entry && entry->subsystem && !audin->device) |
1191 | 0 | { |
1192 | 0 | if ((error = audin_set_subsystem(audin, entry->subsystem))) |
1193 | 0 | { |
1194 | 0 | WLog_Print(audin->log, WLOG_ERROR, |
1195 | 0 | "audin_set_subsystem for %s failed with error %" PRIu32 "!", |
1196 | 0 | entry->subsystem, error); |
1197 | 0 | } |
1198 | 0 | else if ((error = audin_set_device_name(audin, entry->device))) |
1199 | 0 | { |
1200 | 0 | WLog_Print(audin->log, WLOG_ERROR, |
1201 | 0 | "audin_set_device_name for %s failed with error %" PRIu32 "!", |
1202 | 0 | entry->subsystem, error); |
1203 | 0 | } |
1204 | 0 | else if ((error = audin_load_device_plugin(audin, audin->subsystem, args))) |
1205 | 0 | { |
1206 | 0 | WLog_Print(audin->log, WLOG_ERROR, |
1207 | 0 | "audin_load_device_plugin %s failed with error %" PRIu32 "!", |
1208 | 0 | entry->subsystem, error); |
1209 | 0 | } |
1210 | |
|
1211 | 0 | entry++; |
1212 | 0 | } |
1213 | 0 | } |
1214 | 0 | } |
1215 | | |
1216 | 0 | if (audin->device == nullptr) |
1217 | 0 | { |
1218 | | /* If we have no audin device do not register plugin but still return OK or the client will |
1219 | | * just disconnect due to a missing microphone. */ |
1220 | 0 | WLog_Print(audin->log, WLOG_ERROR, "No microphone device could be found."); |
1221 | 0 | error = CHANNEL_RC_OK; |
1222 | 0 | goto out; |
1223 | 0 | } |
1224 | | |
1225 | 0 | error = pEntryPoints->RegisterPlugin(pEntryPoints, AUDIN_CHANNEL_NAME, &audin->iface); |
1226 | 0 | if (error == CHANNEL_RC_OK) |
1227 | 0 | return error; |
1228 | | |
1229 | 0 | out: |
1230 | 0 | audin_plugin_terminated(&audin->iface); |
1231 | 0 | return error; |
1232 | 0 | } |