Coverage Report

Created: 2026-10-15 06:31

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