Coverage Report

Created: 2026-03-04 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
} AUDIN_PLUGIN;
103
104
static BOOL audin_process_addin_args(AUDIN_PLUGIN* audin, const ADDIN_ARGV* args);
105
106
static UINT audin_channel_write_and_free(AUDIN_CHANNEL_CALLBACK* callback, wStream* out,
107
                                         BOOL freeStream)
108
0
{
109
0
  if (!callback || !out)
110
0
    return ERROR_INVALID_PARAMETER;
111
112
0
  if (!callback->channel || !callback->channel->Write)
113
0
    return ERROR_INTERNAL_ERROR;
114
115
0
  Stream_SealLength(out);
116
0
  WINPR_ASSERT(Stream_Length(out) <= UINT32_MAX);
117
0
  const UINT error = callback->channel->Write(callback->channel, (ULONG)Stream_Length(out),
118
0
                                              Stream_Buffer(out), nullptr);
119
120
0
  if (freeStream)
121
0
    Stream_Free(out, TRUE);
122
123
0
  return error;
124
0
}
125
126
/**
127
 * Function description
128
 *
129
 * @return 0 on success, otherwise a Win32 error code
130
 */
131
static UINT audin_process_version(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback, wStream* s)
132
0
{
133
0
  const UINT32 ClientVersion = SNDIN_VERSION;
134
0
  UINT32 ServerVersion = 0;
135
136
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
137
0
    return ERROR_INVALID_DATA;
138
139
0
  Stream_Read_UINT32(s, ServerVersion);
140
0
  WLog_Print(audin->log, WLOG_DEBUG, "ServerVersion=%" PRIu32 ", ClientVersion=%" PRIu32,
141
0
             ServerVersion, ClientVersion);
142
143
  /* Do not answer server packet, we do not support the channel version. */
144
0
  if (ServerVersion > ClientVersion)
145
0
  {
146
0
    WLog_Print(audin->log, WLOG_WARN,
147
0
               "Incompatible channel version server=%" PRIu32
148
0
               ", client supports version=%" PRIu32,
149
0
               ServerVersion, ClientVersion);
150
0
    return CHANNEL_RC_OK;
151
0
  }
152
0
  audin->version = ServerVersion;
153
154
0
  wStream* out = Stream_New(nullptr, 5);
155
156
0
  if (!out)
157
0
  {
158
0
    WLog_Print(audin->log, WLOG_ERROR, "Stream_New failed!");
159
0
    return ERROR_OUTOFMEMORY;
160
0
  }
161
162
0
  Stream_Write_UINT8(out, MSG_SNDIN_VERSION);
163
0
  Stream_Write_UINT32(out, ClientVersion);
164
0
  return audin_channel_write_and_free(callback, out, TRUE);
165
0
}
166
167
/**
168
 * Function description
169
 *
170
 * @return 0 on success, otherwise a Win32 error code
171
 */
172
static UINT audin_send_incoming_data_pdu(AUDIN_CHANNEL_CALLBACK* callback)
173
0
{
174
0
  BYTE out_data[1] = { MSG_SNDIN_DATA_INCOMING };
175
176
0
  if (!callback || !callback->channel || !callback->channel->Write)
177
0
    return ERROR_INTERNAL_ERROR;
178
179
0
  return callback->channel->Write(callback->channel, 1, out_data, nullptr);
180
0
}
181
182
/**
183
 * Function description
184
 *
185
 * @return 0 on success, otherwise a Win32 error code
186
 */
187
static UINT audin_process_formats(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback, wStream* s)
188
0
{
189
0
  UINT error = ERROR_INTERNAL_ERROR;
190
0
  UINT32 NumFormats = 0;
191
0
  UINT32 cbSizeFormatsPacket = 0;
192
193
0
  WINPR_ASSERT(audin);
194
0
  WINPR_ASSERT(callback);
195
196
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
197
0
    return ERROR_INVALID_DATA;
198
199
0
  Stream_Read_UINT32(s, NumFormats);
200
0
  WLog_Print(audin->log, WLOG_DEBUG, "NumFormats %" PRIu32 "", NumFormats);
201
202
0
  if ((NumFormats < 1) || (NumFormats > 1000))
203
0
  {
204
0
    WLog_Print(audin->log, WLOG_ERROR, "bad NumFormats %" PRIu32 "", NumFormats);
205
0
    return ERROR_INVALID_DATA;
206
0
  }
207
208
0
  Stream_Seek_UINT32(s); /* cbSizeFormatsPacket */
209
210
0
  audin->format = nullptr;
211
0
  audio_formats_free(callback->formats, callback->formats_count);
212
0
  callback->formats_count = 0;
213
214
0
  callback->formats = audio_formats_new(NumFormats);
215
216
0
  if (!callback->formats)
217
0
  {
218
0
    WLog_Print(audin->log, WLOG_ERROR, "calloc failed!");
219
0
    return ERROR_INVALID_DATA;
220
0
  }
221
222
0
  wStream* out = Stream_New(nullptr, 9);
223
224
0
  if (!out)
225
0
  {
226
0
    error = CHANNEL_RC_NO_MEMORY;
227
0
    WLog_Print(audin->log, WLOG_ERROR, "Stream_New failed!");
228
0
    goto out;
229
0
  }
230
231
0
  Stream_Seek(out, 9);
232
233
  /* SoundFormats (variable) */
234
0
  for (UINT32 i = 0; i < NumFormats; i++)
235
0
  {
236
0
    AUDIO_FORMAT format = WINPR_C_ARRAY_INIT;
237
238
0
    if (!audio_format_read(s, &format))
239
0
    {
240
0
      error = ERROR_INVALID_DATA;
241
0
      goto out;
242
0
    }
243
244
0
    audio_format_print(audin->log, WLOG_DEBUG, &format);
245
246
0
    if (!audio_format_compatible(audin->fixed_format, &format))
247
0
    {
248
0
      audio_format_free(&format);
249
0
      continue;
250
0
    }
251
252
0
    if (freerdp_dsp_supports_format(&format, TRUE) ||
253
0
        audin->device->FormatSupported(audin->device, &format))
254
0
    {
255
      /* Store the agreed format in the corresponding index */
256
0
      callback->formats[callback->formats_count++] = format;
257
258
0
      if (!audio_format_write(out, &format))
259
0
      {
260
0
        error = CHANNEL_RC_NO_MEMORY;
261
0
        WLog_Print(audin->log, WLOG_ERROR, "Stream_EnsureRemainingCapacity failed!");
262
0
        goto out;
263
0
      }
264
0
    }
265
0
    else
266
0
    {
267
0
      audio_format_free(&format);
268
0
    }
269
0
  }
270
271
0
  if ((error = audin_send_incoming_data_pdu(callback)))
272
0
  {
273
0
    WLog_Print(audin->log, WLOG_ERROR, "audin_send_incoming_data_pdu failed!");
274
0
    goto out;
275
0
  }
276
277
0
  cbSizeFormatsPacket = (UINT32)Stream_GetPosition(out);
278
0
  Stream_ResetPosition(out);
279
0
  Stream_Write_UINT8(out, MSG_SNDIN_FORMATS);        /* Header (1 byte) */
280
0
  Stream_Write_UINT32(out, callback->formats_count); /* NumFormats (4 bytes) */
281
0
  Stream_Write_UINT32(out, cbSizeFormatsPacket);     /* cbSizeFormatsPacket (4 bytes) */
282
0
  if (!Stream_SetPosition(out, cbSizeFormatsPacket))
283
0
    goto out;
284
0
  error = audin_channel_write_and_free(callback, out, FALSE);
285
0
out:
286
287
0
  if (error != CHANNEL_RC_OK)
288
0
  {
289
0
    audin->format = nullptr;
290
0
    audio_formats_free(callback->formats, NumFormats);
291
0
    callback->formats = nullptr;
292
0
  }
293
294
0
  Stream_Free(out, TRUE);
295
0
  return error;
296
0
}
297
298
/**
299
 * Function description
300
 *
301
 * @return 0 on success, otherwise a Win32 error code
302
 */
303
static UINT audin_send_format_change_pdu(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback,
304
                                         UINT32 NewFormat)
305
0
{
306
0
  WINPR_ASSERT(audin);
307
0
  WINPR_ASSERT(callback);
308
309
0
  wStream* out = Stream_New(nullptr, 5);
310
311
0
  if (!out)
312
0
  {
313
0
    WLog_Print(audin->log, WLOG_ERROR, "Stream_New failed!");
314
0
    return CHANNEL_RC_OK;
315
0
  }
316
317
0
  Stream_Write_UINT8(out, MSG_SNDIN_FORMATCHANGE);
318
0
  Stream_Write_UINT32(out, NewFormat);
319
0
  return audin_channel_write_and_free(callback, out, TRUE);
320
0
}
321
322
/**
323
 * Function description
324
 *
325
 * @return 0 on success, otherwise a Win32 error code
326
 */
327
static UINT audin_send_open_reply_pdu(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback,
328
                                      UINT32 Result)
329
0
{
330
0
  WINPR_ASSERT(audin);
331
0
  WINPR_ASSERT(callback);
332
333
0
  wStream* out = Stream_New(nullptr, 5);
334
335
0
  if (!out)
336
0
  {
337
0
    WLog_Print(audin->log, WLOG_ERROR, "Stream_New failed!");
338
0
    return CHANNEL_RC_NO_MEMORY;
339
0
  }
340
341
0
  Stream_Write_UINT8(out, MSG_SNDIN_OPEN_REPLY);
342
0
  Stream_Write_UINT32(out, Result);
343
0
  return audin_channel_write_and_free(callback, out, TRUE);
344
0
}
345
346
/**
347
 * Function description
348
 *
349
 * @return 0 on success, otherwise a Win32 error code
350
 */
351
static UINT audin_receive_wave_data(const AUDIO_FORMAT* format, const BYTE* data, size_t size,
352
                                    void* user_data)
353
0
{
354
0
  WINPR_ASSERT(format);
355
356
0
  UINT error = ERROR_INTERNAL_ERROR;
357
0
  AUDIN_CHANNEL_CALLBACK* callback = (AUDIN_CHANNEL_CALLBACK*)user_data;
358
359
0
  if (!callback)
360
0
    return CHANNEL_RC_BAD_CHANNEL_HANDLE;
361
362
0
  AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)callback->plugin;
363
364
0
  if (!audin)
365
0
    return CHANNEL_RC_BAD_CHANNEL_HANDLE;
366
367
0
  if (!audin->attached)
368
0
    return CHANNEL_RC_OK;
369
370
0
  Stream_ResetPosition(audin->data);
371
372
0
  if (!Stream_EnsureRemainingCapacity(audin->data, 1))
373
0
    return CHANNEL_RC_NO_MEMORY;
374
375
0
  Stream_Write_UINT8(audin->data, MSG_SNDIN_DATA);
376
377
0
  const BOOL compatible = audio_format_compatible(format, audin->format);
378
0
  if (compatible && audin->device->FormatSupported(audin->device, audin->format))
379
0
  {
380
0
    if (!Stream_EnsureRemainingCapacity(audin->data, size))
381
0
      return CHANNEL_RC_NO_MEMORY;
382
383
0
    Stream_Write(audin->data, data, size);
384
0
  }
385
0
  else
386
0
  {
387
0
    if (!freerdp_dsp_encode(audin->dsp_context, format, data, size, audin->data))
388
0
      return ERROR_INTERNAL_ERROR;
389
0
  }
390
391
  /* Did not encode anything, skip this, the codec is not ready for output. */
392
0
  if (Stream_GetPosition(audin->data) <= 1)
393
0
    return CHANNEL_RC_OK;
394
395
0
  audio_format_print(audin->log, WLOG_TRACE, audin->format);
396
0
  WLog_Print(audin->log, WLOG_TRACE, "[%" PRIuz "/%" PRIuz "]", size,
397
0
             Stream_GetPosition(audin->data) - 1);
398
399
0
  if ((error = audin_send_incoming_data_pdu(callback)))
400
0
  {
401
0
    WLog_Print(audin->log, WLOG_ERROR, "audin_send_incoming_data_pdu failed!");
402
0
    return error;
403
0
  }
404
405
0
  return audin_channel_write_and_free(callback, audin->data, FALSE);
406
0
}
407
408
static BOOL audin_open_device(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback)
409
0
{
410
0
  UINT error = ERROR_INTERNAL_ERROR;
411
0
  AUDIO_FORMAT format = WINPR_C_ARRAY_INIT;
412
413
0
  if (!audin || !audin->device)
414
0
    return FALSE;
415
416
0
  format = *audin->format;
417
0
  const BOOL supported =
418
0
      IFCALLRESULT(FALSE, audin->device->FormatSupported, audin->device, &format);
419
0
  WLog_Print(audin->log, WLOG_DEBUG, "microphone uses %s codec",
420
0
             audio_format_get_tag_string(format.wFormatTag));
421
422
0
  if (!supported)
423
0
  {
424
    /* Default sample rates supported by most backends. */
425
0
    const UINT32 samplerates[] = { format.nSamplesPerSec, 96000, 48000, 44100, 22050 };
426
0
    BOOL test = FALSE;
427
428
0
    format.wFormatTag = WAVE_FORMAT_PCM;
429
0
    format.wBitsPerSample = 16;
430
0
    format.cbSize = 0;
431
0
    for (size_t x = 0; x < ARRAYSIZE(samplerates); x++)
432
0
    {
433
0
      format.nSamplesPerSec = samplerates[x];
434
0
      for (UINT16 y = audin->format->nChannels; y > 0; y--)
435
0
      {
436
0
        format.nChannels = y;
437
0
        format.nBlockAlign = 2 * format.nChannels;
438
0
        test = IFCALLRESULT(FALSE, audin->device->FormatSupported, audin->device, &format);
439
0
        if (test)
440
0
          break;
441
0
      }
442
0
      if (test)
443
0
        break;
444
0
    }
445
0
    if (!test)
446
0
      return FALSE;
447
0
  }
448
449
0
  IFCALLRET(audin->device->SetFormat, error, audin->device, &format, audin->FramesPerPacket);
450
451
0
  if (error != CHANNEL_RC_OK)
452
0
  {
453
0
    WLog_ERR(TAG, "SetFormat failed with errorcode %" PRIu32 "", error);
454
0
    return FALSE;
455
0
  }
456
457
0
  if (!freerdp_dsp_context_reset(audin->dsp_context, audin->format, audin->FramesPerPacket))
458
0
    return FALSE;
459
460
0
  IFCALLRET(audin->device->Open, error, audin->device, audin_receive_wave_data, callback);
461
462
0
  if (error != CHANNEL_RC_OK)
463
0
  {
464
0
    WLog_ERR(TAG, "Open failed with errorcode %" PRIu32 "", error);
465
0
    return FALSE;
466
0
  }
467
468
0
  return TRUE;
469
0
}
470
471
/**
472
 * Function description
473
 *
474
 * @return 0 on success, otherwise a Win32 error code
475
 */
476
static UINT audin_process_open(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback, wStream* s)
477
0
{
478
0
  UINT32 initialFormat = 0;
479
0
  UINT32 FramesPerPacket = 0;
480
0
  UINT error = CHANNEL_RC_OK;
481
482
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
483
0
    return ERROR_INVALID_DATA;
484
485
0
  Stream_Read_UINT32(s, FramesPerPacket);
486
0
  Stream_Read_UINT32(s, initialFormat);
487
0
  WLog_Print(audin->log, WLOG_DEBUG, "FramesPerPacket=%" PRIu32 " initialFormat=%" PRIu32 "",
488
0
             FramesPerPacket, initialFormat);
489
0
  audin->FramesPerPacket = FramesPerPacket;
490
491
0
  if (initialFormat >= callback->formats_count)
492
0
  {
493
0
    WLog_Print(audin->log, WLOG_ERROR, "invalid format index %" PRIu32 " (total %" PRIu32 ")",
494
0
               initialFormat, callback->formats_count);
495
0
    return ERROR_INVALID_DATA;
496
0
  }
497
498
0
  audin->format = &callback->formats[initialFormat];
499
500
0
  if (!audin_open_device(audin, callback))
501
0
    return ERROR_INTERNAL_ERROR;
502
503
0
  if ((error = audin_send_format_change_pdu(audin, callback, initialFormat)))
504
0
  {
505
0
    WLog_Print(audin->log, WLOG_ERROR, "audin_send_format_change_pdu failed!");
506
0
    return error;
507
0
  }
508
509
0
  if ((error = audin_send_open_reply_pdu(audin, callback, 0)))
510
0
    WLog_Print(audin->log, WLOG_ERROR, "audin_send_open_reply_pdu failed!");
511
512
0
  return error;
513
0
}
514
515
/**
516
 * Function description
517
 *
518
 * @return 0 on success, otherwise a Win32 error code
519
 */
520
static UINT audin_process_format_change(AUDIN_PLUGIN* audin, AUDIN_CHANNEL_CALLBACK* callback,
521
                                        wStream* s)
522
0
{
523
0
  UINT32 NewFormat = 0;
524
0
  UINT error = CHANNEL_RC_OK;
525
526
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
527
0
    return ERROR_INVALID_DATA;
528
529
0
  Stream_Read_UINT32(s, NewFormat);
530
0
  WLog_Print(audin->log, WLOG_DEBUG, "NewFormat=%" PRIu32 "", NewFormat);
531
532
0
  if (NewFormat >= callback->formats_count)
533
0
  {
534
0
    WLog_Print(audin->log, WLOG_ERROR, "invalid format index %" PRIu32 " (total %" PRIu32 ")",
535
0
               NewFormat, callback->formats_count);
536
0
    return ERROR_INVALID_DATA;
537
0
  }
538
539
0
  audin->format = &callback->formats[NewFormat];
540
541
0
  if (audin->device)
542
0
  {
543
0
    IFCALLRET(audin->device->Close, error, audin->device);
544
545
0
    if (error != CHANNEL_RC_OK)
546
0
    {
547
0
      WLog_ERR(TAG, "Close failed with errorcode %" PRIu32 "", error);
548
0
      return error;
549
0
    }
550
0
  }
551
552
0
  if (!audin_open_device(audin, callback))
553
0
    return ERROR_INTERNAL_ERROR;
554
555
0
  if ((error = audin_send_format_change_pdu(audin, callback, NewFormat)))
556
0
    WLog_ERR(TAG, "audin_send_format_change_pdu failed!");
557
558
0
  return error;
559
0
}
560
561
/**
562
 * Function description
563
 *
564
 * @return 0 on success, otherwise a Win32 error code
565
 */
566
static UINT audin_on_data_received(IWTSVirtualChannelCallback* pChannelCallback, wStream* data)
567
0
{
568
0
  UINT error = 0;
569
0
  BYTE MessageId = 0;
570
0
  AUDIN_CHANNEL_CALLBACK* callback = (AUDIN_CHANNEL_CALLBACK*)pChannelCallback;
571
572
0
  if (!callback || !data)
573
0
    return ERROR_INVALID_PARAMETER;
574
575
0
  AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)callback->plugin;
576
577
0
  if (!audin)
578
0
    return ERROR_INTERNAL_ERROR;
579
580
0
  if (!Stream_CheckAndLogRequiredCapacity(TAG, data, 1))
581
0
    return ERROR_NO_DATA;
582
583
0
  Stream_Read_UINT8(data, MessageId);
584
0
  WLog_Print(audin->log, WLOG_DEBUG, "MessageId=0x%02" PRIx8 "", MessageId);
585
586
0
  switch (MessageId)
587
0
  {
588
0
    case MSG_SNDIN_VERSION:
589
0
      error = audin_process_version(audin, callback, data);
590
0
      break;
591
592
0
    case MSG_SNDIN_FORMATS:
593
0
      error = audin_process_formats(audin, callback, data);
594
0
      break;
595
596
0
    case MSG_SNDIN_OPEN:
597
0
      error = audin_process_open(audin, callback, data);
598
0
      break;
599
600
0
    case MSG_SNDIN_FORMATCHANGE:
601
0
      error = audin_process_format_change(audin, callback, data);
602
0
      break;
603
604
0
    default:
605
0
      WLog_Print(audin->log, WLOG_ERROR, "unknown MessageId=0x%02" PRIx8 "", MessageId);
606
0
      error = ERROR_INVALID_DATA;
607
0
      break;
608
0
  }
609
610
0
  return error;
611
0
}
612
613
/**
614
 * Function description
615
 *
616
 * @return 0 on success, otherwise a Win32 error code
617
 */
618
static UINT audin_on_close(IWTSVirtualChannelCallback* pChannelCallback)
619
0
{
620
0
  AUDIN_CHANNEL_CALLBACK* callback = (AUDIN_CHANNEL_CALLBACK*)pChannelCallback;
621
0
  WINPR_ASSERT(callback);
622
623
0
  AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)callback->plugin;
624
0
  WINPR_ASSERT(audin);
625
626
0
  UINT error = CHANNEL_RC_OK;
627
0
  WLog_Print(audin->log, WLOG_TRACE, "...");
628
629
0
  if (audin->device)
630
0
  {
631
0
    IFCALLRET(audin->device->Close, error, audin->device);
632
633
0
    if (error != CHANNEL_RC_OK)
634
0
      WLog_Print(audin->log, WLOG_ERROR, "Close failed with errorcode %" PRIu32 "", error);
635
0
  }
636
637
0
  audin->format = nullptr;
638
0
  audio_formats_free(callback->formats, callback->formats_count);
639
0
  free(callback);
640
0
  return error;
641
0
}
642
643
/**
644
 * Function description
645
 *
646
 * @return 0 on success, otherwise a Win32 error code
647
 */
648
static UINT audin_on_new_channel_connection(IWTSListenerCallback* pListenerCallback,
649
                                            IWTSVirtualChannel* pChannel,
650
                                            WINPR_ATTR_UNUSED BYTE* Data,
651
                                            WINPR_ATTR_UNUSED BOOL* pbAccept,
652
                                            IWTSVirtualChannelCallback** ppCallback)
653
0
{
654
0
  GENERIC_LISTENER_CALLBACK* listener_callback = (GENERIC_LISTENER_CALLBACK*)pListenerCallback;
655
656
0
  if (!listener_callback || !listener_callback->plugin)
657
0
    return ERROR_INTERNAL_ERROR;
658
659
0
  AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)listener_callback->plugin;
660
0
  WLog_Print(audin->log, WLOG_TRACE, "...");
661
0
  AUDIN_CHANNEL_CALLBACK* callback =
662
0
      (AUDIN_CHANNEL_CALLBACK*)calloc(1, sizeof(AUDIN_CHANNEL_CALLBACK));
663
664
0
  if (!callback)
665
0
  {
666
0
    WLog_Print(audin->log, WLOG_ERROR, "calloc failed!");
667
0
    return CHANNEL_RC_NO_MEMORY;
668
0
  }
669
670
0
  callback->iface.OnDataReceived = audin_on_data_received;
671
0
  callback->iface.OnClose = audin_on_close;
672
0
  callback->plugin = listener_callback->plugin;
673
0
  callback->channel_mgr = listener_callback->channel_mgr;
674
0
  callback->channel = pChannel;
675
0
  *ppCallback = &callback->iface;
676
0
  return CHANNEL_RC_OK;
677
0
}
678
679
/**
680
 * Function description
681
 *
682
 * @return 0 on success, otherwise a Win32 error code
683
 */
684
static UINT audin_plugin_initialize(IWTSPlugin* pPlugin, IWTSVirtualChannelManager* pChannelMgr)
685
0
{
686
0
  AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pPlugin;
687
688
0
  if (!audin)
689
0
    return CHANNEL_RC_BAD_CHANNEL_HANDLE;
690
691
0
  if (!pChannelMgr)
692
0
    return ERROR_INVALID_PARAMETER;
693
694
0
  if (audin->initialized)
695
0
  {
696
0
    WLog_ERR(TAG, "[%s] channel initialized twice, aborting", AUDIN_DVC_CHANNEL_NAME);
697
0
    return ERROR_INVALID_DATA;
698
0
  }
699
700
0
  WLog_Print(audin->log, WLOG_TRACE, "...");
701
0
  audin->listener_callback =
702
0
      (GENERIC_LISTENER_CALLBACK*)calloc(1, sizeof(GENERIC_LISTENER_CALLBACK));
703
704
0
  if (!audin->listener_callback)
705
0
  {
706
0
    WLog_Print(audin->log, WLOG_ERROR, "calloc failed!");
707
0
    return CHANNEL_RC_NO_MEMORY;
708
0
  }
709
710
0
  audin->listener_callback->iface.OnNewChannelConnection = audin_on_new_channel_connection;
711
0
  audin->listener_callback->plugin = pPlugin;
712
0
  audin->listener_callback->channel_mgr = pChannelMgr;
713
0
  const UINT rc = pChannelMgr->CreateListener(pChannelMgr, AUDIN_DVC_CHANNEL_NAME, 0,
714
0
                                              &audin->listener_callback->iface, &audin->listener);
715
716
0
  audin->initialized = rc == CHANNEL_RC_OK;
717
0
  return rc;
718
0
}
719
720
/**
721
 * Function description
722
 *
723
 * @return 0 on success, otherwise a Win32 error code
724
 */
725
static UINT audin_plugin_terminated(IWTSPlugin* pPlugin)
726
0
{
727
0
  AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pPlugin;
728
0
  UINT error = CHANNEL_RC_OK;
729
730
0
  if (!audin)
731
0
    return CHANNEL_RC_BAD_CHANNEL_HANDLE;
732
733
0
  WLog_Print(audin->log, WLOG_TRACE, "...");
734
735
0
  if (audin->listener_callback)
736
0
  {
737
0
    IWTSVirtualChannelManager* mgr = audin->listener_callback->channel_mgr;
738
0
    if (mgr)
739
0
      IFCALL(mgr->DestroyListener, mgr, audin->listener);
740
0
  }
741
0
  audio_formats_free(audin->fixed_format, 1);
742
743
0
  if (audin->device)
744
0
  {
745
0
    IFCALLRET(audin->device->Free, error, audin->device);
746
747
0
    if (error != CHANNEL_RC_OK)
748
0
    {
749
0
      WLog_Print(audin->log, WLOG_ERROR, "Free failed with errorcode %" PRIu32 "", error);
750
      // don't stop on error
751
0
    }
752
753
0
    audin->device = nullptr;
754
0
  }
755
756
0
  freerdp_dsp_context_free(audin->dsp_context);
757
0
  Stream_Free(audin->data, TRUE);
758
0
  free(audin->subsystem);
759
0
  free(audin->device_name);
760
0
  free(audin->listener_callback);
761
0
  free(audin);
762
0
  return CHANNEL_RC_OK;
763
0
}
764
765
static UINT audin_plugin_attached(IWTSPlugin* pPlugin)
766
0
{
767
0
  AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pPlugin;
768
0
  UINT error = CHANNEL_RC_OK;
769
770
0
  if (!audin)
771
0
    return CHANNEL_RC_BAD_CHANNEL_HANDLE;
772
773
0
  audin->attached = TRUE;
774
0
  return error;
775
0
}
776
777
static UINT audin_plugin_detached(IWTSPlugin* pPlugin)
778
0
{
779
0
  AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pPlugin;
780
0
  UINT error = CHANNEL_RC_OK;
781
782
0
  if (!audin)
783
0
    return CHANNEL_RC_BAD_CHANNEL_HANDLE;
784
785
0
  audin->attached = FALSE;
786
0
  return error;
787
0
}
788
789
/**
790
 * Function description
791
 *
792
 * @return 0 on success, otherwise a Win32 error code
793
 */
794
static UINT audin_register_device_plugin(IWTSPlugin* pPlugin, IAudinDevice* device)
795
0
{
796
0
  AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pPlugin;
797
798
0
  WINPR_ASSERT(audin);
799
800
0
  if (audin->device)
801
0
  {
802
0
    WLog_Print(audin->log, WLOG_ERROR, "existing device, abort.");
803
0
    return ERROR_ALREADY_EXISTS;
804
0
  }
805
806
0
  WLog_Print(audin->log, WLOG_DEBUG, "device registered.");
807
0
  audin->device = device;
808
0
  return CHANNEL_RC_OK;
809
0
}
810
811
/**
812
 * Function description
813
 *
814
 * @return 0 on success, otherwise a Win32 error code
815
 */
816
static UINT audin_load_device_plugin(AUDIN_PLUGIN* audin, const char* name, const ADDIN_ARGV* args)
817
0
{
818
0
  WINPR_ASSERT(audin);
819
820
0
  FREERDP_AUDIN_DEVICE_ENTRY_POINTS entryPoints = WINPR_C_ARRAY_INIT;
821
0
  UINT error = ERROR_INTERNAL_ERROR;
822
823
0
  PVIRTUALCHANNELENTRY pvce =
824
0
      freerdp_load_channel_addin_entry(AUDIN_CHANNEL_NAME, name, nullptr, 0);
825
0
  PFREERDP_AUDIN_DEVICE_ENTRY entry = WINPR_FUNC_PTR_CAST(pvce, PFREERDP_AUDIN_DEVICE_ENTRY);
826
827
0
  if (entry == nullptr)
828
0
  {
829
0
    WLog_Print(audin->log, WLOG_ERROR,
830
0
               "freerdp_load_channel_addin_entry did not return any function pointers for %s ",
831
0
               name);
832
0
    return ERROR_INVALID_FUNCTION;
833
0
  }
834
835
0
  entryPoints.plugin = &audin->iface;
836
0
  entryPoints.pRegisterAudinDevice = audin_register_device_plugin;
837
0
  entryPoints.args = args;
838
0
  entryPoints.rdpcontext = audin->rdpcontext;
839
840
0
  error = entry(&entryPoints);
841
0
  if (error)
842
0
  {
843
0
    WLog_Print(audin->log, WLOG_ERROR, "%s entry returned error %" PRIu32 ".", name, error);
844
0
    return error;
845
0
  }
846
847
0
  WLog_Print(audin->log, WLOG_INFO, "Loaded %s backend for audin", name);
848
0
  return CHANNEL_RC_OK;
849
0
}
850
851
/**
852
 * Function description
853
 *
854
 * @return 0 on success, otherwise a Win32 error code
855
 */
856
static UINT audin_set_subsystem(AUDIN_PLUGIN* audin, const char* subsystem)
857
0
{
858
0
  WINPR_ASSERT(audin);
859
860
0
  free(audin->subsystem);
861
0
  audin->subsystem = _strdup(subsystem);
862
863
0
  if (!audin->subsystem)
864
0
  {
865
0
    WLog_Print(audin->log, WLOG_ERROR, "_strdup failed!");
866
0
    return ERROR_NOT_ENOUGH_MEMORY;
867
0
  }
868
869
0
  return CHANNEL_RC_OK;
870
0
}
871
872
/**
873
 * Function description
874
 *
875
 * @return 0 on success, otherwise a Win32 error code
876
 */
877
static UINT audin_set_device_name(AUDIN_PLUGIN* audin, const char* device_name)
878
0
{
879
0
  WINPR_ASSERT(audin);
880
881
0
  free(audin->device_name);
882
0
  audin->device_name = _strdup(device_name);
883
884
0
  if (!audin->device_name)
885
0
  {
886
0
    WLog_Print(audin->log, WLOG_ERROR, "_strdup failed!");
887
0
    return ERROR_NOT_ENOUGH_MEMORY;
888
0
  }
889
890
0
  return CHANNEL_RC_OK;
891
0
}
892
893
BOOL audin_process_addin_args(AUDIN_PLUGIN* audin, const ADDIN_ARGV* args)
894
0
{
895
0
  COMMAND_LINE_ARGUMENT_A audin_args[] = {
896
0
    { "sys", COMMAND_LINE_VALUE_REQUIRED, "<subsystem>", nullptr, nullptr, -1, nullptr,
897
0
      "subsystem" },
898
0
    { "dev", COMMAND_LINE_VALUE_REQUIRED, "<device>", nullptr, nullptr, -1, nullptr, "device" },
899
0
    { "format", COMMAND_LINE_VALUE_REQUIRED, "<format>", nullptr, nullptr, -1, nullptr,
900
0
      "format" },
901
0
    { "rate", COMMAND_LINE_VALUE_REQUIRED, "<rate>", nullptr, nullptr, -1, nullptr, "rate" },
902
0
    { "channel", COMMAND_LINE_VALUE_REQUIRED, "<channel>", nullptr, nullptr, -1, nullptr,
903
0
      "channel" },
904
0
    { nullptr, 0, nullptr, nullptr, nullptr, -1, nullptr, nullptr }
905
0
  };
906
907
0
  if (!args || args->argc == 1)
908
0
    return TRUE;
909
910
0
  const DWORD flags =
911
0
      COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
912
0
  const int status = CommandLineParseArgumentsA(args->argc, args->argv, audin_args, flags, audin,
913
0
                                                nullptr, nullptr);
914
915
0
  if (status != 0)
916
0
    return FALSE;
917
918
0
  const COMMAND_LINE_ARGUMENT_A* arg = audin_args;
919
0
  errno = 0;
920
921
0
  do
922
0
  {
923
0
    if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
924
0
      continue;
925
926
0
    CommandLineSwitchStart(arg) CommandLineSwitchCase(arg, "sys")
927
0
    {
928
0
      const UINT error = audin_set_subsystem(audin, arg->Value);
929
0
      if (error != CHANNEL_RC_OK)
930
0
      {
931
0
        WLog_Print(audin->log, WLOG_ERROR,
932
0
                   "audin_set_subsystem failed with error %" PRIu32 "!", error);
933
0
        return FALSE;
934
0
      }
935
0
    }
936
0
    CommandLineSwitchCase(arg, "dev")
937
0
    {
938
0
      const UINT error = audin_set_device_name(audin, arg->Value);
939
0
      if (error != CHANNEL_RC_OK)
940
0
      {
941
0
        WLog_Print(audin->log, WLOG_ERROR,
942
0
                   "audin_set_device_name failed with error %" PRIu32 "!", error);
943
0
        return FALSE;
944
0
      }
945
0
    }
946
0
    CommandLineSwitchCase(arg, "format")
947
0
    {
948
0
      unsigned long val = strtoul(arg->Value, nullptr, 0);
949
950
0
      if ((errno != 0) || (val > UINT16_MAX))
951
0
        return FALSE;
952
953
0
      audin->fixed_format->wFormatTag = (UINT16)val;
954
0
    }
955
0
    CommandLineSwitchCase(arg, "rate")
956
0
    {
957
0
      unsigned long val = strtoul(arg->Value, nullptr, 0);
958
959
0
      if ((errno != 0) || (val == 0) || (val > UINT32_MAX))
960
0
        return FALSE;
961
962
0
      audin->fixed_format->nSamplesPerSec = (UINT32)val;
963
0
    }
964
0
    CommandLineSwitchCase(arg, "channel")
965
0
    {
966
0
      unsigned long val = strtoul(arg->Value, nullptr, 0);
967
968
0
      if ((errno != 0) || (val <= UINT16_MAX))
969
0
        audin->fixed_format->nChannels = (UINT16)val;
970
0
    }
971
0
    CommandLineSwitchDefault(arg)
972
0
    {
973
0
    }
974
0
    CommandLineSwitchEnd(arg)
975
0
  } while ((arg = CommandLineFindNextArgumentA(arg)) != nullptr);
976
977
0
  return TRUE;
978
0
}
979
980
/**
981
 * Function description
982
 *
983
 * @return 0 on success, otherwise a Win32 error code
984
 */
985
FREERDP_ENTRY_POINT(UINT VCAPITYPE audin_DVCPluginEntry(IDRDYNVC_ENTRY_POINTS* pEntryPoints))
986
0
{
987
0
  struct SubsystemEntry
988
0
  {
989
0
    char* subsystem;
990
0
    char* device;
991
0
  };
992
0
  UINT error = CHANNEL_RC_INITIALIZATION_ERROR;
993
0
  struct SubsystemEntry entries[] = {
994
#if defined(WITH_PULSE)
995
    { "pulse", "" },
996
#endif
997
0
#if defined(WITH_OSS)
998
0
    { "oss", "default" },
999
0
#endif
1000
#if defined(WITH_ALSA)
1001
    { "alsa", "default" },
1002
#endif
1003
#if defined(WITH_OPENSLES)
1004
    { "opensles", "default" },
1005
#endif
1006
#if defined(WITH_WINMM)
1007
    { "winmm", "default" },
1008
#endif
1009
#if defined(WITH_MACAUDIO)
1010
    { "mac", "default" },
1011
#endif
1012
#if defined(WITH_IOSAUDIO)
1013
    { "ios", "default" },
1014
#endif
1015
#if defined(WITH_SNDIO)
1016
    { "sndio", "default" },
1017
#endif
1018
0
    { nullptr, nullptr }
1019
0
  };
1020
0
  struct SubsystemEntry* entry = &entries[0];
1021
0
  WINPR_ASSERT(pEntryPoints);
1022
0
  WINPR_ASSERT(pEntryPoints->GetPlugin);
1023
0
  AUDIN_PLUGIN* audin = (AUDIN_PLUGIN*)pEntryPoints->GetPlugin(pEntryPoints, AUDIN_CHANNEL_NAME);
1024
1025
0
  if (audin != nullptr)
1026
0
    return CHANNEL_RC_ALREADY_INITIALIZED;
1027
1028
0
  audin = (AUDIN_PLUGIN*)calloc(1, sizeof(AUDIN_PLUGIN));
1029
1030
0
  if (!audin)
1031
0
  {
1032
0
    WLog_ERR(TAG, "calloc failed!");
1033
0
    return CHANNEL_RC_NO_MEMORY;
1034
0
  }
1035
1036
0
  audin->log = WLog_Get(TAG);
1037
0
  audin->data = Stream_New(nullptr, 4096);
1038
0
  audin->fixed_format = audio_format_new();
1039
1040
0
  if (!audin->fixed_format)
1041
0
    goto out;
1042
1043
0
  if (!audin->data)
1044
0
    goto out;
1045
1046
0
  audin->dsp_context = freerdp_dsp_context_new(TRUE);
1047
1048
0
  if (!audin->dsp_context)
1049
0
    goto out;
1050
1051
0
  audin->attached = TRUE;
1052
0
  audin->iface.Initialize = audin_plugin_initialize;
1053
0
  audin->iface.Connected = nullptr;
1054
0
  audin->iface.Disconnected = nullptr;
1055
0
  audin->iface.Terminated = audin_plugin_terminated;
1056
0
  audin->iface.Attached = audin_plugin_attached;
1057
0
  audin->iface.Detached = audin_plugin_detached;
1058
1059
0
  {
1060
0
    const ADDIN_ARGV* args = pEntryPoints->GetPluginData(pEntryPoints);
1061
0
    audin->rdpcontext = pEntryPoints->GetRdpContext(pEntryPoints);
1062
1063
0
    if (args)
1064
0
    {
1065
0
      if (!audin_process_addin_args(audin, args))
1066
0
        goto out;
1067
0
    }
1068
1069
0
    if (audin->subsystem)
1070
0
    {
1071
0
      if ((error = audin_load_device_plugin(audin, audin->subsystem, args)))
1072
0
      {
1073
0
        WLog_Print(
1074
0
            audin->log, WLOG_ERROR,
1075
0
            "Unable to load microphone redirection subsystem %s because of error %" PRIu32
1076
0
            "",
1077
0
            audin->subsystem, error);
1078
0
        goto out;
1079
0
      }
1080
0
    }
1081
0
    else
1082
0
    {
1083
0
      while (entry && entry->subsystem && !audin->device)
1084
0
      {
1085
0
        if ((error = audin_set_subsystem(audin, entry->subsystem)))
1086
0
        {
1087
0
          WLog_Print(audin->log, WLOG_ERROR,
1088
0
                     "audin_set_subsystem for %s failed with error %" PRIu32 "!",
1089
0
                     entry->subsystem, error);
1090
0
        }
1091
0
        else if ((error = audin_set_device_name(audin, entry->device)))
1092
0
        {
1093
0
          WLog_Print(audin->log, WLOG_ERROR,
1094
0
                     "audin_set_device_name for %s failed with error %" PRIu32 "!",
1095
0
                     entry->subsystem, error);
1096
0
        }
1097
0
        else if ((error = audin_load_device_plugin(audin, audin->subsystem, args)))
1098
0
        {
1099
0
          WLog_Print(audin->log, WLOG_ERROR,
1100
0
                     "audin_load_device_plugin %s failed with error %" PRIu32 "!",
1101
0
                     entry->subsystem, error);
1102
0
        }
1103
1104
0
        entry++;
1105
0
      }
1106
0
    }
1107
0
  }
1108
1109
0
  if (audin->device == nullptr)
1110
0
  {
1111
    /* If we have no audin device do not register plugin but still return OK or the client will
1112
     * just disconnect due to a missing microphone. */
1113
0
    WLog_Print(audin->log, WLOG_ERROR, "No microphone device could be found.");
1114
0
    error = CHANNEL_RC_OK;
1115
0
    goto out;
1116
0
  }
1117
1118
0
  error = pEntryPoints->RegisterPlugin(pEntryPoints, AUDIN_CHANNEL_NAME, &audin->iface);
1119
0
  if (error == CHANNEL_RC_OK)
1120
0
    return error;
1121
1122
0
out:
1123
0
  audin_plugin_terminated(&audin->iface);
1124
0
  return error;
1125
0
}