Coverage Report

Created: 2026-09-14 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/channels/rdpsnd/client/pulse/rdpsnd_pulse.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Audio Output Virtual Channel
4
 *
5
 * Copyright 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
#include <freerdp/utils/helpers.h>
24
25
#include <errno.h>
26
27
#include <stdio.h>
28
#include <stdlib.h>
29
#include <string.h>
30
#include <time.h>
31
32
#include <winpr/crt.h>
33
#include <winpr/cast.h>
34
#include <winpr/assert.h>
35
#include <winpr/stream.h>
36
#include <winpr/cmdline.h>
37
38
#include <pulse/pulseaudio.h>
39
40
#include <freerdp/types.h>
41
#include <freerdp/codec/dsp.h>
42
43
#include "rdpsnd_main.h"
44
45
typedef struct
46
{
47
  rdpsndDevicePlugin device;
48
49
  char* device_name;
50
  char* client_name;
51
  char* stream_name;
52
  pa_threaded_mainloop* mainloop;
53
  pa_context* context;
54
  pa_sample_spec sample_spec;
55
  pa_stream* stream;
56
  UINT32 latency;
57
  UINT32 volume;
58
  time_t reconnect_delay_seconds;
59
  time_t reconnect_time;
60
} rdpsndPulsePlugin;
61
62
static BOOL rdpsnd_check_pulse(rdpsndPulsePlugin* pulse, BOOL haveStream)
63
0
{
64
0
  BOOL rc = TRUE;
65
0
  WINPR_ASSERT(pulse);
66
67
0
  if (!pulse->context)
68
0
  {
69
0
    WLog_WARN(TAG, "pulse->context=nullptr");
70
0
    rc = FALSE;
71
0
  }
72
73
0
  if (haveStream)
74
0
  {
75
0
    if (!pulse->stream)
76
0
    {
77
0
      WLog_WARN(TAG, "pulse->stream=%p", WINPR_CXX_COMPAT_CAST(const void*, pulse->stream));
78
0
      rc = FALSE;
79
0
    }
80
0
  }
81
82
0
  if (!pulse->mainloop)
83
0
  {
84
0
    WLog_WARN(TAG, "pulse->mainloop=%p", WINPR_CXX_COMPAT_CAST(const void*, pulse->mainloop));
85
0
    rc = FALSE;
86
0
  }
87
88
0
  return rc;
89
0
}
90
91
static BOOL rdpsnd_pulse_format_supported(WINPR_ATTR_UNUSED rdpsndDevicePlugin* device,
92
                                          const AUDIO_FORMAT* format);
93
94
static void rdpsnd_pulse_get_sink_info(WINPR_ATTR_UNUSED pa_context* c, const pa_sink_info* i,
95
                                       WINPR_ATTR_UNUSED int eol, void* userdata)
96
0
{
97
0
  UINT16 dwVolumeLeft = ((50 * 0xFFFF) / 100);  /* 50% */
98
0
  UINT16 dwVolumeRight = ((50 * 0xFFFF) / 100); /* 50% */
99
0
  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
100
101
0
  WINPR_ASSERT(c);
102
0
  if (!rdpsnd_check_pulse(pulse, FALSE) || !i)
103
0
    return;
104
105
0
  for (uint8_t x = 0; x < i->volume.channels; x++)
106
0
  {
107
0
    pa_volume_t volume = i->volume.values[x];
108
109
0
    if (volume >= PA_VOLUME_NORM)
110
0
      volume = PA_VOLUME_NORM - 1;
111
112
0
    switch (x)
113
0
    {
114
0
      case 0:
115
0
        dwVolumeLeft = (UINT16)volume;
116
0
        break;
117
118
0
      case 1:
119
0
        dwVolumeRight = (UINT16)volume;
120
0
        break;
121
122
0
      default:
123
0
        break;
124
0
    }
125
0
  }
126
127
0
  pulse->volume = ((UINT32)dwVolumeLeft << 16U) | dwVolumeRight;
128
0
}
129
130
static void rdpsnd_pulse_context_state_callback(pa_context* context, void* userdata)
131
0
{
132
0
  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
133
134
0
  WINPR_ASSERT(context);
135
0
  WINPR_ASSERT(pulse);
136
137
0
  pa_context_state_t state = pa_context_get_state(context);
138
139
0
  switch (state)
140
0
  {
141
0
    case PA_CONTEXT_READY:
142
0
      pa_threaded_mainloop_signal(pulse->mainloop, 0);
143
0
      break;
144
145
0
    case PA_CONTEXT_FAILED:
146
      // Destroy context now, create new one for next connection attempt
147
0
      pa_context_unref(pulse->context);
148
0
      pulse->context = nullptr;
149
0
      if (pulse->reconnect_delay_seconds >= 0)
150
0
        pulse->reconnect_time = time(nullptr) + pulse->reconnect_delay_seconds;
151
0
      pa_threaded_mainloop_signal(pulse->mainloop, 0);
152
0
      break;
153
154
0
    case PA_CONTEXT_TERMINATED:
155
0
      pa_threaded_mainloop_signal(pulse->mainloop, 0);
156
0
      break;
157
158
0
    default:
159
0
      break;
160
0
  }
161
0
}
162
163
static BOOL rdpsnd_pulse_connect(rdpsndDevicePlugin* device)
164
0
{
165
0
  BOOL rc = 0;
166
0
  pa_operation* o = nullptr;
167
0
  pa_context_state_t state = PA_CONTEXT_FAILED;
168
0
  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
169
170
0
  if (!rdpsnd_check_pulse(pulse, FALSE))
171
0
    return FALSE;
172
173
0
  pa_threaded_mainloop_lock(pulse->mainloop);
174
175
0
  if (pa_context_connect(pulse->context, nullptr, PA_CONTEXT_NOFLAGS, nullptr) < 0)
176
0
  {
177
0
    pa_threaded_mainloop_unlock(pulse->mainloop);
178
0
    return FALSE;
179
0
  }
180
181
  /* the context state callback releases pulse->context on PA_CONTEXT_FAILED,
182
   * so it must be re-checked after every wait */
183
0
  while (pulse->context)
184
0
  {
185
0
    state = pa_context_get_state(pulse->context);
186
187
0
    if (state == PA_CONTEXT_READY)
188
0
      break;
189
190
0
    if (!PA_CONTEXT_IS_GOOD(state))
191
0
    {
192
0
      break;
193
0
    }
194
195
0
    pa_threaded_mainloop_wait(pulse->mainloop);
196
0
  }
197
198
0
  if (pulse->context)
199
0
  {
200
0
    o = pa_context_get_sink_info_by_index(pulse->context, 0, rdpsnd_pulse_get_sink_info, pulse);
201
202
0
    if (o)
203
0
      pa_operation_unref(o);
204
0
  }
205
206
0
  if (pulse->context && (state == PA_CONTEXT_READY))
207
0
  {
208
0
    rc = TRUE;
209
0
  }
210
0
  else
211
0
  {
212
0
    if (pulse->context)
213
0
      pa_context_disconnect(pulse->context);
214
0
    rc = FALSE;
215
0
  }
216
217
0
  pa_threaded_mainloop_unlock(pulse->mainloop);
218
0
  return rc;
219
0
}
220
221
static void rdpsnd_pulse_stream_success_callback(WINPR_ATTR_UNUSED pa_stream* stream,
222
                                                 WINPR_ATTR_UNUSED int success, void* userdata)
223
0
{
224
0
  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
225
226
0
  if (!rdpsnd_check_pulse(pulse, TRUE))
227
0
    return;
228
229
0
  pa_threaded_mainloop_signal(pulse->mainloop, 0);
230
0
}
231
232
static void rdpsnd_pulse_wait_for_operation(rdpsndPulsePlugin* pulse, pa_operation* operation)
233
0
{
234
0
  if (!rdpsnd_check_pulse(pulse, TRUE))
235
0
    return;
236
237
0
  if (!operation)
238
0
    return;
239
240
0
  while (pa_operation_get_state(operation) == PA_OPERATION_RUNNING)
241
0
  {
242
0
    pa_threaded_mainloop_wait(pulse->mainloop);
243
0
  }
244
245
0
  pa_operation_unref(operation);
246
0
}
247
248
static void rdpsnd_pulse_stream_state_callback(pa_stream* stream, void* userdata)
249
0
{
250
0
  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
251
252
0
  WINPR_ASSERT(stream);
253
0
  if (!rdpsnd_check_pulse(pulse, TRUE))
254
0
    return;
255
256
0
  pa_stream_state_t state = pa_stream_get_state(stream);
257
258
0
  switch (state)
259
0
  {
260
0
    case PA_STREAM_READY:
261
0
      pa_threaded_mainloop_signal(pulse->mainloop, 0);
262
0
      break;
263
264
0
    case PA_STREAM_FAILED:
265
0
    case PA_STREAM_TERMINATED:
266
      /* The stream is dead (e.g. its device vanished). Drop our
267
       * reference; pa_stream dispatches state callbacks under its own
268
       * ref/unref guard, so unref here is safe. */
269
0
      pa_stream_unref(pulse->stream);
270
0
      pulse->stream = nullptr;
271
0
      pa_threaded_mainloop_signal(pulse->mainloop, 0);
272
0
      break;
273
274
0
    default:
275
0
      break;
276
0
  }
277
0
}
278
279
static void rdpsnd_pulse_stream_request_callback(WINPR_ATTR_UNUSED pa_stream* stream,
280
                                                 WINPR_ATTR_UNUSED size_t length, void* userdata)
281
0
{
282
0
  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
283
284
0
  WINPR_ASSERT(stream);
285
0
  if (!rdpsnd_check_pulse(pulse, TRUE))
286
0
    return;
287
288
0
  pa_threaded_mainloop_signal(pulse->mainloop, 0);
289
0
}
290
291
static void rdpsnd_pulse_close(rdpsndDevicePlugin* device)
292
0
{
293
0
  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
294
295
0
  WINPR_ASSERT(pulse);
296
297
0
  if (!rdpsnd_check_pulse(pulse, FALSE))
298
0
    return;
299
300
0
  pa_threaded_mainloop_lock(pulse->mainloop);
301
0
  if (pulse->stream)
302
0
  {
303
0
    rdpsnd_pulse_wait_for_operation(
304
0
        pulse, pa_stream_drain(pulse->stream, rdpsnd_pulse_stream_success_callback, pulse));
305
    /* The stream may have died while draining; the state callback then
306
     * released it and cleared pulse->stream. */
307
0
    if (pulse->stream)
308
0
    {
309
0
      pa_stream_disconnect(pulse->stream);
310
0
      pa_stream_unref(pulse->stream);
311
0
      pulse->stream = nullptr;
312
0
    }
313
0
  }
314
0
  pa_threaded_mainloop_unlock(pulse->mainloop);
315
0
}
316
317
static BOOL rdpsnd_pulse_set_format_spec(rdpsndPulsePlugin* pulse, const AUDIO_FORMAT* format)
318
0
{
319
0
  WINPR_ASSERT(format);
320
321
0
  if (!rdpsnd_check_pulse(pulse, FALSE))
322
0
    return FALSE;
323
324
0
  if (!rdpsnd_pulse_format_supported(&pulse->device, format))
325
0
    return FALSE;
326
327
0
  pa_sample_format_t sformat = PA_SAMPLE_INVALID;
328
0
  switch (format->wFormatTag)
329
0
  {
330
0
    case WAVE_FORMAT_PCM:
331
0
      switch (format->wBitsPerSample)
332
0
      {
333
0
        case 8:
334
0
          sformat = PA_SAMPLE_U8;
335
0
          break;
336
337
0
        case 16:
338
0
          sformat = PA_SAMPLE_S16LE;
339
0
          break;
340
341
0
        default:
342
0
          return FALSE;
343
0
      }
344
345
0
      break;
346
347
0
    default:
348
0
      return FALSE;
349
0
  }
350
351
0
  const pa_sample_spec sample_spec = { .format = sformat,
352
0
                                     .rate = format->nSamplesPerSec,
353
0
                                     .channels =
354
0
                                         WINPR_ASSERTING_INT_CAST(uint8_t, format->nChannels) };
355
0
  pulse->sample_spec = sample_spec;
356
0
  return TRUE;
357
0
}
358
359
static BOOL rdpsnd_pulse_context_connect(rdpsndDevicePlugin* device)
360
0
{
361
0
  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
362
0
  WINPR_ASSERT(pulse);
363
364
0
  pulse->context =
365
0
      pa_context_new(pa_threaded_mainloop_get_api(pulse->mainloop), pulse->client_name);
366
367
0
  if (!pulse->context)
368
0
    return FALSE;
369
370
0
  pa_context_set_state_callback(pulse->context, rdpsnd_pulse_context_state_callback, pulse);
371
372
0
  return rdpsnd_pulse_connect(&pulse->device);
373
0
}
374
375
static BOOL rdpsnd_pulse_open_stream(rdpsndDevicePlugin* device)
376
0
{
377
0
  pa_stream_state_t state = PA_STREAM_FAILED;
378
0
  int flags = PA_STREAM_NOFLAGS;
379
0
  pa_buffer_attr buffer_attr = WINPR_C_ARRAY_INIT;
380
0
  char ss[PA_SAMPLE_SPEC_SNPRINT_MAX] = WINPR_C_ARRAY_INIT;
381
0
  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
382
0
  WINPR_ASSERT(pulse);
383
384
0
  if (pa_sample_spec_valid(&pulse->sample_spec) == 0)
385
0
  {
386
0
    pa_sample_spec_snprint(ss, sizeof(ss), &pulse->sample_spec);
387
0
    return FALSE;
388
0
  }
389
390
0
  pa_threaded_mainloop_lock(pulse->mainloop);
391
0
  if (!pulse->context)
392
0
  {
393
0
    pa_threaded_mainloop_unlock(pulse->mainloop);
394
0
    if (pulse->reconnect_delay_seconds >= 0 && time(nullptr) - pulse->reconnect_time >= 0)
395
0
      rdpsnd_pulse_context_connect(device);
396
0
    pa_threaded_mainloop_lock(pulse->mainloop);
397
0
  }
398
399
0
  if (!rdpsnd_check_pulse(pulse, FALSE))
400
0
  {
401
0
    pa_threaded_mainloop_unlock(pulse->mainloop);
402
0
    return FALSE;
403
0
  }
404
405
0
  pulse->stream = pa_stream_new(pulse->context, pulse->stream_name, &pulse->sample_spec, nullptr);
406
407
0
  if (!pulse->stream)
408
0
  {
409
0
    pa_threaded_mainloop_unlock(pulse->mainloop);
410
0
    return FALSE;
411
0
  }
412
413
  /* register essential callbacks */
414
0
  pa_stream_set_state_callback(pulse->stream, rdpsnd_pulse_stream_state_callback, pulse);
415
0
  pa_stream_set_write_callback(pulse->stream, rdpsnd_pulse_stream_request_callback, pulse);
416
0
  flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE;
417
418
0
  if (pulse->latency > 0)
419
0
  {
420
0
    const size_t val = pa_usec_to_bytes(1000ULL * pulse->latency, &pulse->sample_spec);
421
0
    buffer_attr.maxlength = UINT32_MAX;
422
0
    buffer_attr.tlength = (val > UINT32_MAX) ? UINT32_MAX : (UINT32)val;
423
0
    buffer_attr.prebuf = UINT32_MAX;
424
0
    buffer_attr.minreq = UINT32_MAX;
425
0
    buffer_attr.fragsize = UINT32_MAX;
426
0
    flags |= PA_STREAM_ADJUST_LATENCY;
427
0
  }
428
429
  // NOLINTNEXTLINE(clang-analyzer-optin.core.EnumCastOutOfRange)
430
0
  pa_stream_flags_t eflags = (pa_stream_flags_t)flags;
431
0
  if (pa_stream_connect_playback(pulse->stream, pulse->device_name,
432
0
                                 pulse->latency > 0 ? &buffer_attr : nullptr, eflags, nullptr,
433
0
                                 nullptr) < 0)
434
0
  {
435
0
    WLog_ERR(TAG, "error connecting playback stream");
436
0
    pa_stream_unref(pulse->stream);
437
0
    pulse->stream = nullptr;
438
0
    pa_threaded_mainloop_unlock(pulse->mainloop);
439
0
    return FALSE;
440
0
  }
441
442
0
  while (pulse->stream)
443
0
  {
444
0
    state = pa_stream_get_state(pulse->stream);
445
446
0
    if (state == PA_STREAM_READY)
447
0
      break;
448
449
0
    if (!PA_STREAM_IS_GOOD(state))
450
0
    {
451
0
      break;
452
0
    }
453
454
0
    pa_threaded_mainloop_wait(pulse->mainloop);
455
0
  }
456
457
0
  pa_threaded_mainloop_unlock(pulse->mainloop);
458
459
0
  if (state == PA_STREAM_READY)
460
0
    return TRUE;
461
462
0
  rdpsnd_pulse_close(device);
463
0
  return FALSE;
464
0
}
465
466
static BOOL rdpsnd_pulse_open(rdpsndDevicePlugin* device, const AUDIO_FORMAT* format,
467
                              UINT32 latency)
468
0
{
469
0
  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
470
471
0
  WINPR_ASSERT(format);
472
473
0
  if (!rdpsnd_check_pulse(pulse, FALSE))
474
0
    return TRUE;
475
476
0
  if (!rdpsnd_pulse_set_format_spec(pulse, format))
477
0
    return FALSE;
478
479
0
  pulse->latency = latency;
480
481
0
  return rdpsnd_pulse_open_stream(device);
482
0
}
483
484
static void rdpsnd_pulse_free(rdpsndDevicePlugin* device)
485
0
{
486
0
  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
487
488
0
  if (!pulse)
489
0
    return;
490
491
0
  rdpsnd_pulse_close(device);
492
493
0
  if (pulse->mainloop)
494
0
    pa_threaded_mainloop_stop(pulse->mainloop);
495
496
0
  if (pulse->context)
497
0
  {
498
0
    pa_context_disconnect(pulse->context);
499
0
    pa_context_unref(pulse->context);
500
0
    pulse->context = nullptr;
501
0
  }
502
503
0
  if (pulse->mainloop)
504
0
  {
505
0
    pa_threaded_mainloop_free(pulse->mainloop);
506
0
    pulse->mainloop = nullptr;
507
0
  }
508
509
0
  free(pulse->device_name);
510
0
  free(pulse->client_name);
511
0
  free(pulse->stream_name);
512
0
  free(pulse);
513
0
}
514
515
static BOOL rdpsnd_pulse_default_format(rdpsndDevicePlugin* device, const AUDIO_FORMAT* desired,
516
                                        AUDIO_FORMAT* defaultFormat)
517
0
{
518
0
  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
519
520
0
  if (!pulse || !defaultFormat)
521
0
    return FALSE;
522
523
0
  *defaultFormat = *desired;
524
0
  defaultFormat->data = nullptr;
525
0
  defaultFormat->cbSize = 0;
526
0
  defaultFormat->wFormatTag = WAVE_FORMAT_PCM;
527
0
  if ((defaultFormat->nChannels < 1) || (defaultFormat->nChannels > PA_CHANNELS_MAX))
528
0
    defaultFormat->nChannels = 2;
529
0
  if ((defaultFormat->nSamplesPerSec < 1) || (defaultFormat->nSamplesPerSec > PA_RATE_MAX))
530
0
    defaultFormat->nSamplesPerSec = 44100;
531
0
  if ((defaultFormat->wBitsPerSample != 8) && (defaultFormat->wBitsPerSample != 16))
532
0
    defaultFormat->wBitsPerSample = 16;
533
534
0
  defaultFormat->nBlockAlign = defaultFormat->nChannels * defaultFormat->wBitsPerSample / 8;
535
0
  defaultFormat->nAvgBytesPerSec = defaultFormat->nBlockAlign * defaultFormat->nSamplesPerSec;
536
0
  return TRUE;
537
0
}
538
539
BOOL rdpsnd_pulse_format_supported(WINPR_ATTR_UNUSED rdpsndDevicePlugin* device,
540
                                   const AUDIO_FORMAT* format)
541
0
{
542
0
  WINPR_ASSERT(device);
543
0
  WINPR_ASSERT(format);
544
545
0
  switch (format->wFormatTag)
546
0
  {
547
0
    case WAVE_FORMAT_PCM:
548
0
      if (format->cbSize == 0 && (format->nSamplesPerSec <= PA_RATE_MAX) &&
549
0
          (format->wBitsPerSample == 8 || format->wBitsPerSample == 16) &&
550
0
          (format->nChannels >= 1 && format->nChannels <= PA_CHANNELS_MAX))
551
0
      {
552
0
        return TRUE;
553
0
      }
554
555
0
      break;
556
557
0
    default:
558
0
      break;
559
0
  }
560
561
0
  return FALSE;
562
0
}
563
564
static UINT32 rdpsnd_pulse_get_volume(rdpsndDevicePlugin* device)
565
0
{
566
0
  pa_operation* o = nullptr;
567
0
  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
568
569
0
  if (!pulse || !pulse->mainloop)
570
0
    return 0;
571
572
0
  pa_threaded_mainloop_lock(pulse->mainloop);
573
  /* re-check under the lock, the context is released if the connection dies */
574
0
  if (rdpsnd_check_pulse(pulse, FALSE))
575
0
  {
576
0
    o = pa_context_get_sink_info_by_index(pulse->context, 0, rdpsnd_pulse_get_sink_info, pulse);
577
0
    if (o)
578
0
      pa_operation_unref(o);
579
0
  }
580
0
  pa_threaded_mainloop_unlock(pulse->mainloop);
581
0
  return pulse->volume;
582
0
}
583
584
static void rdpsnd_set_volume_success_cb(WINPR_ATTR_UNUSED pa_context* c, int success,
585
                                         void* userdata)
586
0
{
587
0
  rdpsndPulsePlugin* pulse = userdata;
588
589
0
  if (!rdpsnd_check_pulse(pulse, TRUE))
590
0
    return;
591
0
  WINPR_ASSERT(c);
592
593
0
  WLog_INFO(TAG, "%d", success);
594
0
}
595
596
static BOOL rdpsnd_pulse_set_volume(rdpsndDevicePlugin* device, UINT32 value)
597
0
{
598
0
  pa_cvolume cv = WINPR_C_ARRAY_INIT;
599
0
  pa_volume_t left = 0;
600
0
  pa_volume_t right = 0;
601
0
  pa_operation* operation = nullptr;
602
0
  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
603
604
0
  if (!pulse || !pulse->mainloop)
605
0
  {
606
0
    WLog_WARN(TAG, "called before pulse backend was initialized");
607
0
    return FALSE;
608
0
  }
609
610
0
  left = (pa_volume_t)(value & 0xFFFF);
611
0
  right = (pa_volume_t)((value >> 16) & 0xFFFF);
612
0
  pa_cvolume_init(&cv);
613
0
  cv.channels = 2;
614
0
  cv.values[0] = PA_VOLUME_MUTED + (left * (PA_VOLUME_NORM - PA_VOLUME_MUTED)) / PA_VOLUME_NORM;
615
0
  cv.values[1] = PA_VOLUME_MUTED + (right * (PA_VOLUME_NORM - PA_VOLUME_MUTED)) / PA_VOLUME_NORM;
616
0
  pa_threaded_mainloop_lock(pulse->mainloop);
617
618
  /* the stream state must be inspected under the mainloop lock - it may be
619
   * cleared by the state callback when the stream dies */
620
0
  if (!rdpsnd_check_pulse(pulse, TRUE))
621
0
  {
622
0
    pa_threaded_mainloop_unlock(pulse->mainloop);
623
0
    WLog_WARN(TAG, "no pulse stream, not setting volume");
624
0
    return FALSE;
625
0
  }
626
627
0
  operation = pa_context_set_sink_input_volume(pulse->context, pa_stream_get_index(pulse->stream),
628
0
                                               &cv, rdpsnd_set_volume_success_cb, pulse);
629
630
0
  if (operation)
631
0
    pa_operation_unref(operation);
632
633
0
  pa_threaded_mainloop_unlock(pulse->mainloop);
634
0
  return TRUE;
635
0
}
636
637
static UINT rdpsnd_pulse_play(rdpsndDevicePlugin* device, const BYTE* data, size_t size)
638
0
{
639
0
  size_t length = 0;
640
0
  void* pa_data = nullptr;
641
0
  int status = 0;
642
0
  pa_usec_t latency = 0;
643
0
  int negative = 0;
644
0
  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
645
646
0
  if (!data)
647
0
    return 0;
648
649
0
  pa_threaded_mainloop_lock(pulse->mainloop);
650
651
0
  if (!rdpsnd_check_pulse(pulse, TRUE))
652
0
  {
653
0
    pa_threaded_mainloop_unlock(pulse->mainloop);
654
    // Discard this playback request and just attempt to reconnect the stream
655
0
    WLog_DBG(TAG, "reconnecting playback stream");
656
0
    rdpsnd_pulse_open_stream(device);
657
0
    return 0;
658
0
  }
659
660
0
  while (size > 0)
661
0
  {
662
0
    length = size;
663
664
0
    status = pa_stream_begin_write(pulse->stream, &pa_data, &length);
665
666
0
    if (status < 0)
667
0
      break;
668
669
    /* A suspended or migrating stream (e.g. the default device just
670
     * changed) can hand out an empty buffer; treating it as progress
671
     * busy-loops forever with the mainloop lock held, which also stalls
672
     * the channel thread calling Play. Drop the rest of the sample
673
     * instead. */
674
0
    if (!pa_data || (length == 0))
675
0
    {
676
0
      if (pa_data)
677
0
        pa_stream_cancel_write(pulse->stream);
678
0
      WLog_DBG(TAG, "dropping %" PRIuz " bytes, no buffer space", size);
679
0
      break;
680
0
    }
681
682
0
    memcpy(pa_data, data, length);
683
684
0
    status = pa_stream_write(pulse->stream, pa_data, length, nullptr, 0LL, PA_SEEK_RELATIVE);
685
686
0
    if (status < 0)
687
0
    {
688
0
      break;
689
0
    }
690
691
0
    data += length;
692
0
    size -= length;
693
0
  }
694
695
0
  if (pa_stream_get_latency(pulse->stream, &latency, &negative) != 0)
696
0
    latency = 0;
697
698
0
  pa_threaded_mainloop_unlock(pulse->mainloop);
699
700
0
  const pa_usec_t val = latency / 1000;
701
0
  if (val > UINT32_MAX)
702
0
    return UINT32_MAX;
703
0
  return (UINT32)val;
704
0
}
705
706
static UINT rdpsnd_pulse_parse_addin_args(rdpsndPulsePlugin* pulse, const ADDIN_ARGV* args)
707
0
{
708
0
  COMMAND_LINE_ARGUMENT_A rdpsnd_pulse_args[] = {
709
0
    { "dev", COMMAND_LINE_VALUE_REQUIRED, "<device>", nullptr, nullptr, -1, nullptr, "device" },
710
0
    { "reconnect_delay_seconds", COMMAND_LINE_VALUE_REQUIRED, "<reconnect_delay_seconds>",
711
0
      nullptr, nullptr, -1, nullptr, "reconnect_delay_seconds" },
712
0
    { "client_name", COMMAND_LINE_VALUE_REQUIRED, "<client_name>", nullptr, nullptr, -1,
713
0
      nullptr, "name of pulse client" },
714
0
    { "stream_name", COMMAND_LINE_VALUE_REQUIRED, "<stream_name>", nullptr, nullptr, -1,
715
0
      nullptr, "name of pulse stream" },
716
0
    { nullptr, 0, nullptr, nullptr, nullptr, -1, nullptr, nullptr }
717
0
  };
718
0
  const DWORD flags =
719
0
      COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
720
721
0
  WINPR_ASSERT(pulse);
722
0
  WINPR_ASSERT(args);
723
724
0
  const int status = CommandLineParseArgumentsA(args->argc, args->argv, rdpsnd_pulse_args, flags,
725
0
                                                pulse, nullptr, nullptr);
726
727
0
  if (status < 0)
728
0
    return ERROR_INVALID_DATA;
729
730
0
  const COMMAND_LINE_ARGUMENT_A* arg = rdpsnd_pulse_args;
731
732
0
  const char* client_name = nullptr;
733
0
  const char* stream_name = nullptr;
734
0
  do
735
0
  {
736
0
    if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
737
0
      continue;
738
739
0
    CommandLineSwitchStart(arg) CommandLineSwitchCase(arg, "dev")
740
0
    {
741
0
      pulse->device_name = _strdup(arg->Value);
742
743
0
      if (!pulse->device_name)
744
0
        return ERROR_OUTOFMEMORY;
745
0
    }
746
0
    CommandLineSwitchCase(arg, "reconnect_delay_seconds")
747
0
    {
748
0
      unsigned long val = strtoul(arg->Value, nullptr, 0);
749
750
0
      if ((errno != 0) || (val > INT32_MAX))
751
0
        return ERROR_INVALID_DATA;
752
753
0
      pulse->reconnect_delay_seconds = (time_t)val;
754
0
    }
755
0
    CommandLineSwitchCase(arg, "client_name")
756
0
    {
757
0
      client_name = arg->Value;
758
0
    }
759
0
    CommandLineSwitchCase(arg, "stream_name")
760
0
    {
761
0
      stream_name = arg->Value;
762
0
    }
763
0
    CommandLineSwitchEnd(arg)
764
0
  } while ((arg = CommandLineFindNextArgumentA(arg)) != nullptr);
765
766
0
  if (!client_name)
767
0
    client_name = freerdp_getApplicationDetailsString();
768
0
  if (!stream_name)
769
0
    stream_name = freerdp_getApplicationDetailsString();
770
771
0
  pulse->client_name = _strdup(client_name);
772
0
  pulse->stream_name = _strdup(stream_name);
773
0
  if (!pulse->client_name || !pulse->stream_name)
774
0
    return ERROR_OUTOFMEMORY;
775
0
  return CHANNEL_RC_OK;
776
0
}
777
778
FREERDP_ENTRY_POINT(UINT VCAPITYPE pulse_freerdp_rdpsnd_client_subsystem_entry(
779
    PFREERDP_RDPSND_DEVICE_ENTRY_POINTS pEntryPoints))
780
0
{
781
0
  WINPR_ASSERT(pEntryPoints);
782
783
0
  rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)calloc(1, sizeof(rdpsndPulsePlugin));
784
785
0
  if (!pulse)
786
0
    return CHANNEL_RC_NO_MEMORY;
787
788
0
  pulse->device.Open = rdpsnd_pulse_open;
789
0
  pulse->device.FormatSupported = rdpsnd_pulse_format_supported;
790
0
  pulse->device.GetVolume = rdpsnd_pulse_get_volume;
791
0
  pulse->device.SetVolume = rdpsnd_pulse_set_volume;
792
0
  pulse->device.Play = rdpsnd_pulse_play;
793
0
  pulse->device.Close = rdpsnd_pulse_close;
794
0
  pulse->device.Free = rdpsnd_pulse_free;
795
0
  pulse->device.DefaultFormat = rdpsnd_pulse_default_format;
796
797
0
  const ADDIN_ARGV* args = pEntryPoints->args;
798
0
  UINT ret = rdpsnd_pulse_parse_addin_args(pulse, args);
799
800
0
  if (ret != CHANNEL_RC_OK)
801
0
  {
802
0
    WLog_ERR(TAG, "error parsing arguments");
803
0
    goto error;
804
0
  }
805
806
0
  pulse->reconnect_delay_seconds = 5;
807
0
  pulse->reconnect_time = time(nullptr);
808
809
0
  ret = CHANNEL_RC_NO_MEMORY;
810
0
  pulse->mainloop = pa_threaded_mainloop_new();
811
812
0
  if (!pulse->mainloop)
813
0
    goto error;
814
815
0
  pa_threaded_mainloop_lock(pulse->mainloop);
816
817
0
  if (pa_threaded_mainloop_start(pulse->mainloop) < 0)
818
0
  {
819
0
    pa_threaded_mainloop_unlock(pulse->mainloop);
820
0
    goto error;
821
0
  }
822
823
0
  pa_threaded_mainloop_unlock(pulse->mainloop);
824
825
0
  if (!rdpsnd_pulse_context_connect((rdpsndDevicePlugin*)pulse))
826
0
    goto error;
827
828
0
  pEntryPoints->pRegisterRdpsndDevice(pEntryPoints->rdpsnd, (rdpsndDevicePlugin*)pulse);
829
0
  return CHANNEL_RC_OK;
830
0
error:
831
0
  rdpsnd_pulse_free((rdpsndDevicePlugin*)pulse);
832
0
  return ret;
833
0
}