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