Coverage Report

Created: 2026-02-26 06:54

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