Coverage Report

Created: 2026-09-14 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/channels/audin/client/pulse/audin_pulse.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Audio Input Redirection Virtual Channel - PulseAudio implementation
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
 *
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 *     http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 */
21
22
#include <freerdp/config.h>
23
24
#include <stdio.h>
25
#include <stdlib.h>
26
#include <string.h>
27
28
#include <winpr/crt.h>
29
#include <winpr/cmdline.h>
30
#include <winpr/wlog.h>
31
#include <winpr/cast.h>
32
33
#include <pulse/pulseaudio.h>
34
35
#include <freerdp/types.h>
36
#include <freerdp/addin.h>
37
#include <freerdp/freerdp.h>
38
#include <freerdp/codec/audio.h>
39
#include <freerdp/client/audin.h>
40
#include <freerdp/utils/helpers.h>
41
42
#include "audin_main.h"
43
44
typedef struct
45
{
46
  IAudinDevice iface;
47
48
  char* device_name;
49
  char* client_name;
50
  char* stream_name;
51
  UINT32 frames_per_packet;
52
  pa_threaded_mainloop* mainloop;
53
  pa_context* context;
54
  pa_sample_spec sample_spec;
55
  pa_stream* stream;
56
  AUDIO_FORMAT format;
57
58
  size_t bytes_per_frame;
59
  size_t buffer_frames;
60
61
  AudinReceive receive;
62
  void* user_data;
63
64
  rdpContext* rdpcontext;
65
  wLog* log;
66
} AudinPulseDevice;
67
68
static const char* pulse_context_state_string(pa_context_state_t state)
69
0
{
70
0
  switch (state)
71
0
  {
72
0
    case PA_CONTEXT_UNCONNECTED:
73
0
      return "PA_CONTEXT_UNCONNECTED";
74
0
    case PA_CONTEXT_CONNECTING:
75
0
      return "PA_CONTEXT_CONNECTING";
76
0
    case PA_CONTEXT_AUTHORIZING:
77
0
      return "PA_CONTEXT_AUTHORIZING";
78
0
    case PA_CONTEXT_SETTING_NAME:
79
0
      return "PA_CONTEXT_SETTING_NAME";
80
0
    case PA_CONTEXT_READY:
81
0
      return "PA_CONTEXT_READY";
82
0
    case PA_CONTEXT_FAILED:
83
0
      return "PA_CONTEXT_FAILED";
84
0
    case PA_CONTEXT_TERMINATED:
85
0
      return "PA_CONTEXT_TERMINATED";
86
0
    default:
87
0
      return "UNKNOWN";
88
0
  }
89
0
}
90
91
static const char* pulse_stream_state_string(pa_stream_state_t state)
92
0
{
93
0
  switch (state)
94
0
  {
95
0
    case PA_STREAM_UNCONNECTED:
96
0
      return "PA_STREAM_UNCONNECTED";
97
0
    case PA_STREAM_CREATING:
98
0
      return "PA_STREAM_CREATING";
99
0
    case PA_STREAM_READY:
100
0
      return "PA_STREAM_READY";
101
0
    case PA_STREAM_FAILED:
102
0
      return "PA_STREAM_FAILED";
103
0
    case PA_STREAM_TERMINATED:
104
0
      return "PA_STREAM_TERMINATED";
105
0
    default:
106
0
      return "UNKNOWN";
107
0
  }
108
0
}
109
110
static void audin_pulse_context_state_callback(pa_context* context, void* userdata)
111
0
{
112
0
  AudinPulseDevice* pulse = (AudinPulseDevice*)userdata;
113
0
  pa_context_state_t state = pa_context_get_state(context);
114
115
0
  WLog_Print(pulse->log, WLOG_DEBUG, "context state %s", pulse_context_state_string(state));
116
0
  switch (state)
117
0
  {
118
0
    case PA_CONTEXT_READY:
119
0
    case PA_CONTEXT_FAILED:
120
0
    case PA_CONTEXT_TERMINATED:
121
0
      pa_threaded_mainloop_signal(pulse->mainloop, 0);
122
0
      break;
123
124
0
    default:
125
0
      break;
126
0
  }
127
0
}
128
129
/**
130
 * Function description
131
 *
132
 * @return 0 on success, otherwise a Win32 error code
133
 */
134
static UINT audin_pulse_connect(IAudinDevice* device)
135
0
{
136
0
  pa_context_state_t state = PA_CONTEXT_FAILED;
137
0
  AudinPulseDevice* pulse = (AudinPulseDevice*)device;
138
139
0
  if (!pulse->context)
140
0
    return ERROR_INVALID_PARAMETER;
141
142
0
  if (pa_context_connect(pulse->context, nullptr, PA_CONTEXT_NOFLAGS, nullptr))
143
0
  {
144
0
    WLog_Print(pulse->log, WLOG_ERROR, "pa_context_connect failed (%d)",
145
0
               pa_context_errno(pulse->context));
146
0
    return ERROR_INTERNAL_ERROR;
147
0
  }
148
149
0
  pa_threaded_mainloop_lock(pulse->mainloop);
150
151
0
  if (pa_threaded_mainloop_start(pulse->mainloop) < 0)
152
0
  {
153
0
    pa_threaded_mainloop_unlock(pulse->mainloop);
154
0
    WLog_Print(pulse->log, WLOG_ERROR, "pa_threaded_mainloop_start failed (%d)",
155
0
               pa_context_errno(pulse->context));
156
0
    return ERROR_INTERNAL_ERROR;
157
0
  }
158
159
0
  for (;;)
160
0
  {
161
0
    state = pa_context_get_state(pulse->context);
162
163
0
    if (state == PA_CONTEXT_READY)
164
0
      break;
165
166
0
    if (!PA_CONTEXT_IS_GOOD(state))
167
0
    {
168
0
      WLog_Print(pulse->log, WLOG_ERROR, "bad context state (%s: %d)",
169
0
                 pulse_context_state_string(state), pa_context_errno(pulse->context));
170
0
      pa_context_disconnect(pulse->context);
171
0
      return ERROR_INVALID_STATE;
172
0
    }
173
174
0
    pa_threaded_mainloop_wait(pulse->mainloop);
175
0
  }
176
177
0
  pa_threaded_mainloop_unlock(pulse->mainloop);
178
0
  WLog_Print(pulse->log, WLOG_DEBUG, "connected");
179
0
  return CHANNEL_RC_OK;
180
0
}
181
182
/**
183
 * Function description
184
 *
185
 * @return 0 on success, otherwise a Win32 error code
186
 */
187
static UINT audin_pulse_free(IAudinDevice* device)
188
0
{
189
0
  AudinPulseDevice* pulse = (AudinPulseDevice*)device;
190
191
0
  if (!pulse)
192
0
    return ERROR_INVALID_PARAMETER;
193
194
0
  if (pulse->mainloop)
195
0
  {
196
0
    pa_threaded_mainloop_stop(pulse->mainloop);
197
0
  }
198
199
0
  if (pulse->context)
200
0
  {
201
0
    pa_context_disconnect(pulse->context);
202
0
    pa_context_unref(pulse->context);
203
0
    pulse->context = nullptr;
204
0
  }
205
206
0
  if (pulse->mainloop)
207
0
  {
208
0
    pa_threaded_mainloop_free(pulse->mainloop);
209
0
    pulse->mainloop = nullptr;
210
0
  }
211
212
0
  free(pulse->device_name);
213
0
  free(pulse->client_name);
214
0
  free(pulse->stream_name);
215
0
  free(pulse);
216
0
  return CHANNEL_RC_OK;
217
0
}
218
219
static BOOL audin_pulse_format_supported(IAudinDevice* device, const AUDIO_FORMAT* format)
220
0
{
221
0
  AudinPulseDevice* pulse = (AudinPulseDevice*)device;
222
223
0
  if (!pulse || !format)
224
0
    return FALSE;
225
226
0
  if (!pulse->context)
227
0
    return 0;
228
229
0
  switch (format->wFormatTag)
230
0
  {
231
0
    case WAVE_FORMAT_PCM:
232
0
      if (format->cbSize == 0 && (format->nSamplesPerSec <= PA_RATE_MAX) &&
233
0
          (format->wBitsPerSample == 8 || format->wBitsPerSample == 16) &&
234
0
          (format->nChannels >= 1 && format->nChannels <= PA_CHANNELS_MAX))
235
0
      {
236
0
        return TRUE;
237
0
      }
238
239
0
      break;
240
241
0
    default:
242
0
      return FALSE;
243
0
  }
244
245
0
  return FALSE;
246
0
}
247
248
/**
249
 * Function description
250
 *
251
 * @return 0 on success, otherwise a Win32 error code
252
 */
253
static UINT audin_pulse_set_format(IAudinDevice* device, const AUDIO_FORMAT* format,
254
                                   UINT32 FramesPerPacket)
255
0
{
256
0
  AudinPulseDevice* pulse = (AudinPulseDevice*)device;
257
258
0
  if (!pulse || !format)
259
0
    return ERROR_INVALID_PARAMETER;
260
261
0
  if (!pulse->context)
262
0
    return ERROR_INVALID_PARAMETER;
263
264
0
  if (FramesPerPacket > 0)
265
0
    pulse->frames_per_packet = FramesPerPacket;
266
267
0
  pa_sample_format_t sformat = PA_SAMPLE_INVALID;
268
0
  switch (format->wFormatTag)
269
0
  {
270
0
    case WAVE_FORMAT_PCM: /* PCM */
271
0
      switch (format->wBitsPerSample)
272
0
      {
273
0
        case 8:
274
0
          sformat = PA_SAMPLE_U8;
275
0
          break;
276
277
0
        case 16:
278
0
          sformat = PA_SAMPLE_S16LE;
279
0
          break;
280
281
0
        default:
282
0
          return ERROR_INTERNAL_ERROR;
283
0
      }
284
285
0
      break;
286
287
0
    default:
288
0
      return ERROR_INTERNAL_ERROR;
289
0
  }
290
291
0
  const pa_sample_spec sample_spec = {
292
0
    .format = sformat,
293
0
    .rate = format->nSamplesPerSec,
294
0
    .channels = WINPR_ASSERTING_INT_CAST(uint8_t, format->nChannels),
295
0
  };
296
297
0
  pulse->sample_spec = sample_spec;
298
0
  pulse->format = *format;
299
0
  return CHANNEL_RC_OK;
300
0
}
301
302
static void audin_pulse_stream_state_callback(pa_stream* stream, void* userdata)
303
0
{
304
0
  AudinPulseDevice* pulse = (AudinPulseDevice*)userdata;
305
0
  WINPR_ASSERT(pulse);
306
307
0
  pa_stream_state_t state = pa_stream_get_state(stream);
308
309
0
  WLog_Print(pulse->log, WLOG_DEBUG, "stream state %s", pulse_stream_state_string(state));
310
0
  switch (state)
311
0
  {
312
0
    case PA_STREAM_READY:
313
0
    case PA_STREAM_FAILED:
314
0
    case PA_STREAM_TERMINATED:
315
0
      pa_threaded_mainloop_signal(pulse->mainloop, 0);
316
0
      break;
317
318
0
    case PA_STREAM_UNCONNECTED:
319
0
    case PA_STREAM_CREATING:
320
0
    default:
321
0
      break;
322
0
  }
323
0
}
324
325
static void audin_pulse_stream_request_callback(pa_stream* stream, size_t length, void* userdata)
326
0
{
327
0
  const void* data = nullptr;
328
0
  AudinPulseDevice* pulse = (AudinPulseDevice*)userdata;
329
0
  UINT error = CHANNEL_RC_OK;
330
331
0
  if (pa_stream_peek(stream, &data, &length) < 0)
332
0
  {
333
0
    WLog_Print(pulse->log, WLOG_WARN, "pa_stream_peek failed (%d)",
334
0
               pa_context_errno(pulse->context));
335
0
    return;
336
0
  }
337
338
  /* length == 0: buffer empty, no fragment to drop.
339
   * data == nullptr with length > 0: a hole in the record stream (e.g. the
340
   * capture device vanished); it carries no samples, only drop it. */
341
0
  if (length == 0)
342
0
    return;
343
344
0
  if (data)
345
0
    error = IFCALLRESULT(CHANNEL_RC_OK, pulse->receive, &pulse->format, data, length,
346
0
                         pulse->user_data);
347
0
  pa_stream_drop(stream);
348
349
0
  if (error && pulse->rdpcontext)
350
0
    setChannelError(pulse->rdpcontext, error, "audin_pulse_thread_func reported an error");
351
0
}
352
353
/**
354
 * Function description
355
 *
356
 * @return 0 on success, otherwise a Win32 error code
357
 */
358
static UINT audin_pulse_close(IAudinDevice* device)
359
0
{
360
0
  AudinPulseDevice* pulse = (AudinPulseDevice*)device;
361
362
0
  if (!pulse)
363
0
    return ERROR_INVALID_PARAMETER;
364
365
0
  if (pulse->stream)
366
0
  {
367
0
    pa_threaded_mainloop_lock(pulse->mainloop);
368
0
    pa_stream_disconnect(pulse->stream);
369
0
    pa_stream_unref(pulse->stream);
370
0
    pulse->stream = nullptr;
371
0
    pa_threaded_mainloop_unlock(pulse->mainloop);
372
0
  }
373
374
0
  pulse->receive = nullptr;
375
0
  pulse->user_data = nullptr;
376
0
  return CHANNEL_RC_OK;
377
0
}
378
379
/**
380
 * Function description
381
 *
382
 * @return 0 on success, otherwise a Win32 error code
383
 */
384
static UINT audin_pulse_open(IAudinDevice* device, AudinReceive receive, void* user_data)
385
0
{
386
0
  pa_stream_state_t state = PA_STREAM_FAILED;
387
0
  pa_buffer_attr buffer_attr = WINPR_C_ARRAY_INIT;
388
0
  AudinPulseDevice* pulse = (AudinPulseDevice*)device;
389
390
0
  if (!pulse || !receive || !user_data)
391
0
    return ERROR_INVALID_PARAMETER;
392
393
0
  if (!pulse->context)
394
0
    return ERROR_INVALID_PARAMETER;
395
396
0
  if (!pulse->sample_spec.rate || pulse->stream)
397
0
    return ERROR_INVALID_PARAMETER;
398
399
0
  pulse->receive = receive;
400
0
  pulse->user_data = user_data;
401
0
  pa_threaded_mainloop_lock(pulse->mainloop);
402
0
  pulse->stream = pa_stream_new(pulse->context, pulse->stream_name, &pulse->sample_spec, nullptr);
403
404
0
  if (!pulse->stream)
405
0
  {
406
0
    pa_threaded_mainloop_unlock(pulse->mainloop);
407
0
    WLog_Print(pulse->log, WLOG_DEBUG, "pa_stream_new failed (%d)",
408
0
               pa_context_errno(pulse->context));
409
0
    const int rc = pa_context_errno(pulse->context);
410
0
    return (UINT)rc;
411
0
  }
412
413
0
  pulse->bytes_per_frame = pa_frame_size(&pulse->sample_spec);
414
0
  pa_stream_set_state_callback(pulse->stream, audin_pulse_stream_state_callback, pulse);
415
0
  pa_stream_set_read_callback(pulse->stream, audin_pulse_stream_request_callback, pulse);
416
0
  buffer_attr.maxlength = (UINT32)-1;
417
0
  buffer_attr.tlength = (UINT32)-1;
418
0
  buffer_attr.prebuf = (UINT32)-1;
419
0
  buffer_attr.minreq = (UINT32)-1;
420
  /* 500ms latency */
421
0
  const size_t frag = pulse->bytes_per_frame * pulse->frames_per_packet;
422
0
  WINPR_ASSERT(frag <= UINT32_MAX);
423
0
  buffer_attr.fragsize = (uint32_t)frag;
424
425
0
  if (buffer_attr.fragsize % pulse->format.nBlockAlign)
426
0
    buffer_attr.fragsize +=
427
0
        pulse->format.nBlockAlign - buffer_attr.fragsize % pulse->format.nBlockAlign;
428
429
0
  if (pa_stream_connect_record(pulse->stream, pulse->device_name, &buffer_attr,
430
0
                               PA_STREAM_ADJUST_LATENCY) < 0)
431
0
  {
432
0
    pa_threaded_mainloop_unlock(pulse->mainloop);
433
0
    WLog_Print(pulse->log, WLOG_ERROR, "pa_stream_connect_playback failed (%d)",
434
0
               pa_context_errno(pulse->context));
435
0
    const int rc = pa_context_errno(pulse->context);
436
0
    return (UINT)rc;
437
0
  }
438
439
0
  while (pulse->stream)
440
0
  {
441
0
    state = pa_stream_get_state(pulse->stream);
442
443
0
    if (state == PA_STREAM_READY)
444
0
      break;
445
446
0
    if (!PA_STREAM_IS_GOOD(state))
447
0
    {
448
0
      audin_pulse_close(device);
449
0
      WLog_Print(pulse->log, WLOG_ERROR, "bad stream state (%s: %d)",
450
0
                 pulse_stream_state_string(state), pa_context_errno(pulse->context));
451
0
      pa_threaded_mainloop_unlock(pulse->mainloop);
452
0
      const int rc = pa_context_errno(pulse->context);
453
0
      return (UINT)rc;
454
0
    }
455
456
0
    pa_threaded_mainloop_wait(pulse->mainloop);
457
0
  }
458
459
0
  pa_threaded_mainloop_unlock(pulse->mainloop);
460
0
  pulse->buffer_frames = 0;
461
0
  WLog_Print(pulse->log, WLOG_DEBUG, "connected");
462
0
  return CHANNEL_RC_OK;
463
0
}
464
465
/**
466
 * Function description
467
 *
468
 * @return 0 on success, otherwise a Win32 error code
469
 */
470
static UINT audin_pulse_parse_addin_args(AudinPulseDevice* pulse, const ADDIN_ARGV* args)
471
0
{
472
0
  COMMAND_LINE_ARGUMENT_A audin_pulse_args[] = {
473
0
    { "dev", COMMAND_LINE_VALUE_REQUIRED, "<device>", nullptr, nullptr, -1, nullptr,
474
0
      "audio device name" },
475
0
    { "client_name", COMMAND_LINE_VALUE_REQUIRED, "<client_name>", nullptr, nullptr, -1,
476
0
      nullptr, "name of pulse client" },
477
0
    { "stream_name", COMMAND_LINE_VALUE_REQUIRED, "<stream_name>", nullptr, nullptr, -1,
478
0
      nullptr, "name of pulse stream" },
479
0
    { nullptr, 0, nullptr, nullptr, nullptr, -1, nullptr, nullptr }
480
0
  };
481
482
0
  const DWORD flags =
483
0
      COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
484
0
  const int status = CommandLineParseArgumentsA(args->argc, args->argv, audin_pulse_args, flags,
485
0
                                                pulse, nullptr, nullptr);
486
487
0
  if (status < 0)
488
0
    return ERROR_INVALID_PARAMETER;
489
490
0
  const COMMAND_LINE_ARGUMENT_A* arg = audin_pulse_args;
491
492
0
  const char* client_name = nullptr;
493
0
  const char* stream_name = nullptr;
494
0
  do
495
0
  {
496
0
    if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
497
0
      continue;
498
499
0
    CommandLineSwitchStart(arg) CommandLineSwitchCase(arg, "dev")
500
0
    {
501
0
      pulse->device_name = _strdup(arg->Value);
502
503
0
      if (!pulse->device_name)
504
0
      {
505
0
        WLog_Print(pulse->log, WLOG_ERROR, "_strdup failed!");
506
0
        return CHANNEL_RC_NO_MEMORY;
507
0
      }
508
0
    }
509
0
    CommandLineSwitchEnd(arg)
510
0
  } while ((arg = CommandLineFindNextArgumentA(arg)) != nullptr);
511
512
0
  if (!client_name)
513
0
    client_name = freerdp_getApplicationDetailsString();
514
0
  if (!stream_name)
515
0
    stream_name = freerdp_getApplicationDetailsString();
516
517
0
  pulse->client_name = _strdup(client_name);
518
0
  pulse->stream_name = _strdup(stream_name);
519
0
  if (!pulse->client_name || !pulse->stream_name)
520
0
    return ERROR_OUTOFMEMORY;
521
522
0
  return CHANNEL_RC_OK;
523
0
}
524
525
/**
526
 * Function description
527
 *
528
 * @return 0 on success, otherwise a Win32 error code
529
 */
530
FREERDP_ENTRY_POINT(UINT VCAPITYPE pulse_freerdp_audin_client_subsystem_entry(
531
    PFREERDP_AUDIN_DEVICE_ENTRY_POINTS pEntryPoints))
532
0
{
533
0
  const ADDIN_ARGV* args = nullptr;
534
0
  AudinPulseDevice* pulse = nullptr;
535
0
  UINT error = 0;
536
0
  pulse = (AudinPulseDevice*)calloc(1, sizeof(AudinPulseDevice));
537
538
0
  if (!pulse)
539
0
  {
540
0
    WLog_ERR(TAG, "calloc failed!");
541
0
    return CHANNEL_RC_NO_MEMORY;
542
0
  }
543
544
0
  pulse->log = WLog_Get(TAG);
545
0
  pulse->iface.Open = audin_pulse_open;
546
0
  pulse->iface.FormatSupported = audin_pulse_format_supported;
547
0
  pulse->iface.SetFormat = audin_pulse_set_format;
548
0
  pulse->iface.Close = audin_pulse_close;
549
0
  pulse->iface.Free = audin_pulse_free;
550
0
  pulse->rdpcontext = pEntryPoints->rdpcontext;
551
0
  args = pEntryPoints->args;
552
553
0
  if ((error = audin_pulse_parse_addin_args(pulse, args)))
554
0
  {
555
0
    WLog_Print(pulse->log, WLOG_ERROR,
556
0
               "audin_pulse_parse_addin_args failed with error %" PRIu32 "!", error);
557
0
    goto error_out;
558
0
  }
559
560
0
  pulse->mainloop = pa_threaded_mainloop_new();
561
562
0
  if (!pulse->mainloop)
563
0
  {
564
0
    WLog_Print(pulse->log, WLOG_ERROR, "pa_threaded_mainloop_new failed");
565
0
    error = CHANNEL_RC_NO_MEMORY;
566
0
    goto error_out;
567
0
  }
568
569
0
  pulse->context =
570
0
      pa_context_new(pa_threaded_mainloop_get_api(pulse->mainloop), pulse->client_name);
571
572
0
  if (!pulse->context)
573
0
  {
574
0
    WLog_Print(pulse->log, WLOG_ERROR, "pa_context_new failed");
575
0
    error = CHANNEL_RC_NO_MEMORY;
576
0
    goto error_out;
577
0
  }
578
579
0
  pa_context_set_state_callback(pulse->context, audin_pulse_context_state_callback, pulse);
580
581
0
  if ((error = audin_pulse_connect(&pulse->iface)))
582
0
  {
583
0
    WLog_Print(pulse->log, WLOG_ERROR, "audin_pulse_connect failed");
584
0
    goto error_out;
585
0
  }
586
587
0
  if ((error = pEntryPoints->pRegisterAudinDevice(pEntryPoints->plugin, &pulse->iface)))
588
0
  {
589
0
    WLog_Print(pulse->log, WLOG_ERROR, "RegisterAudinDevice failed with error %" PRIu32 "!",
590
0
               error);
591
0
    goto error_out;
592
0
  }
593
594
0
  return CHANNEL_RC_OK;
595
0
error_out:
596
0
  audin_pulse_free(&pulse->iface);
597
0
  return error;
598
0
}