Coverage Report

Created: 2026-04-12 07:03

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