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