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(rdpsndDevicePlugin* device, const AUDIO_FORMAT* format)
540{
541 WINPR_ASSERT(device);
542 WINPR_ASSERT(format);
543
544 switch (format->wFormatTag)
545 {
546 case WAVE_FORMAT_PCM:
547 if (format->cbSize == 0 && (format->nSamplesPerSec <= PA_RATE_MAX) &&
548 (format->wBitsPerSample == 8 || format->wBitsPerSample == 16) &&
549 (format->nChannels >= 1 && format->nChannels <= PA_CHANNELS_MAX))
550 {
551 return TRUE;
552 }
553
554 break;
555
556 default:
557 break;
558 }
559
560 return FALSE;
561}
562
563static UINT32 rdpsnd_pulse_get_volume(rdpsndDevicePlugin* device)
564{
565 pa_operation* o = nullptr;
566 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
567
568 if (!pulse || !pulse->mainloop)
569 return 0;
570
571 pa_threaded_mainloop_lock(pulse->mainloop);
572 /* re-check under the lock, the context is released if the connection dies */
573 if (rdpsnd_check_pulse(pulse, FALSE))
574 {
575 o = pa_context_get_sink_info_by_index(pulse->context, 0, rdpsnd_pulse_get_sink_info, pulse);
576 if (o)
577 pa_operation_unref(o);
578 }
579 pa_threaded_mainloop_unlock(pulse->mainloop);
580 return pulse->volume;
581}
582
583static void rdpsnd_set_volume_success_cb(WINPR_ATTR_UNUSED pa_context* c, int success,
584 void* userdata)
585{
586 rdpsndPulsePlugin* pulse = userdata;
587
588 if (!rdpsnd_check_pulse(pulse, TRUE))
589 return;
590 WINPR_ASSERT(c);
591
592 WLog_INFO(TAG, "%d", success);
593}
594
595static BOOL rdpsnd_pulse_set_volume(rdpsndDevicePlugin* device, UINT32 value)
596{
597 pa_cvolume cv = WINPR_C_ARRAY_INIT;
598 pa_volume_t left = 0;
599 pa_volume_t right = 0;
600 pa_operation* operation = nullptr;
601 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
602
603 if (!pulse || !pulse->mainloop)
604 {
605 WLog_WARN(TAG, "called before pulse backend was initialized");
606 return FALSE;
607 }
608
609 left = (pa_volume_t)(value & 0xFFFF);
610 right = (pa_volume_t)((value >> 16) & 0xFFFF);
611 pa_cvolume_init(&cv);
612 cv.channels = 2;
613 cv.values[0] = PA_VOLUME_MUTED + (left * (PA_VOLUME_NORM - PA_VOLUME_MUTED)) / PA_VOLUME_NORM;
614 cv.values[1] = PA_VOLUME_MUTED + (right * (PA_VOLUME_NORM - PA_VOLUME_MUTED)) / PA_VOLUME_NORM;
615 pa_threaded_mainloop_lock(pulse->mainloop);
616
617 /* the stream state must be inspected under the mainloop lock - it may be
618 * cleared by the state callback when the stream dies */
619 if (!rdpsnd_check_pulse(pulse, TRUE))
620 {
621 pa_threaded_mainloop_unlock(pulse->mainloop);
622 WLog_WARN(TAG, "no pulse stream, not setting volume");
623 return FALSE;
624 }
625
626 operation = pa_context_set_sink_input_volume(pulse->context, pa_stream_get_index(pulse->stream),
627 &cv, rdpsnd_set_volume_success_cb, pulse);
628
629 if (operation)
630 pa_operation_unref(operation);
631
632 pa_threaded_mainloop_unlock(pulse->mainloop);
633 return TRUE;
634}
635
636static UINT rdpsnd_pulse_play(rdpsndDevicePlugin* device, const BYTE* data, size_t size)
637{
638 size_t length = 0;
639 void* pa_data = nullptr;
640 int status = 0;
641 pa_usec_t latency = 0;
642 int negative = 0;
643 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
644
645 if (!data)
646 return 0;
647
648 pa_threaded_mainloop_lock(pulse->mainloop);
649
650 if (!rdpsnd_check_pulse(pulse, TRUE))
651 {
652 pa_threaded_mainloop_unlock(pulse->mainloop);
653 // Discard this playback request and just attempt to reconnect the stream
654 WLog_DBG(TAG, "reconnecting playback stream");
655 rdpsnd_pulse_open_stream(device);
656 return 0;
657 }
658
659 while (size > 0)
660 {
661 length = size;
662
663 status = pa_stream_begin_write(pulse->stream, &pa_data, &length);
664
665 if (status < 0)
666 break;
667
668 /* A suspended or migrating stream (e.g. the default device just
669 * changed) can hand out an empty buffer; treating it as progress
670 * busy-loops forever with the mainloop lock held, which also stalls
671 * the channel thread calling Play. Drop the rest of the sample
672 * instead. */
673 if (!pa_data || (length == 0))
674 {
675 if (pa_data)
676 pa_stream_cancel_write(pulse->stream);
677 WLog_DBG(TAG, "dropping %" PRIuz " bytes, no buffer space", size);
678 break;
679 }
680
681 memcpy(pa_data, data, length);
682
683 status = pa_stream_write(pulse->stream, pa_data, length, nullptr, 0LL, PA_SEEK_RELATIVE);
684
685 if (status < 0)
686 {
687 break;
688 }
689
690 data += length;
691 size -= length;
692 }
693
694 if (pa_stream_get_latency(pulse->stream, &latency, &negative) != 0)
695 latency = 0;
696
697 pa_threaded_mainloop_unlock(pulse->mainloop);
698
699 const pa_usec_t val = latency / 1000;
700 if (val > UINT32_MAX)
701 return UINT32_MAX;
702 return (UINT32)val;
703}
704
705static UINT rdpsnd_pulse_parse_addin_args(rdpsndPulsePlugin* pulse, const ADDIN_ARGV* args)
706{
707 COMMAND_LINE_ARGUMENT_A rdpsnd_pulse_args[] = {
708 { "dev", COMMAND_LINE_VALUE_REQUIRED, "<device>", nullptr, nullptr, -1, nullptr, "device" },
709 { "reconnect_delay_seconds", COMMAND_LINE_VALUE_REQUIRED, "<reconnect_delay_seconds>",
710 nullptr, nullptr, -1, nullptr, "reconnect_delay_seconds" },
711 { "client_name", COMMAND_LINE_VALUE_REQUIRED, "<client_name>", nullptr, nullptr, -1,
712 nullptr, "name of pulse client" },
713 { "stream_name", COMMAND_LINE_VALUE_REQUIRED, "<stream_name>", nullptr, nullptr, -1,
714 nullptr, "name of pulse stream" },
715 { nullptr, 0, nullptr, nullptr, nullptr, -1, nullptr, nullptr }
716 };
717 const DWORD flags =
718 COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
719
720 WINPR_ASSERT(pulse);
721 WINPR_ASSERT(args);
722
723 const int status = CommandLineParseArgumentsA(args->argc, args->argv, rdpsnd_pulse_args, flags,
724 pulse, nullptr, nullptr);
725
726 if (status < 0)
727 return ERROR_INVALID_DATA;
728
729 const COMMAND_LINE_ARGUMENT_A* arg = rdpsnd_pulse_args;
730
731 const char* client_name = nullptr;
732 const char* stream_name = nullptr;
733 do
734 {
735 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
736 continue;
737
738 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg, "dev")
739 {
740 pulse->device_name = _strdup(arg->Value);
741
742 if (!pulse->device_name)
743 return ERROR_OUTOFMEMORY;
744 }
745 CommandLineSwitchCase(arg, "reconnect_delay_seconds")
746 {
747 unsigned long val = strtoul(arg->Value, nullptr, 0);
748
749 if ((errno != 0) || (val > INT32_MAX))
750 return ERROR_INVALID_DATA;
751
752 pulse->reconnect_delay_seconds = (time_t)val;
753 }
754 CommandLineSwitchCase(arg, "client_name")
755 {
756 client_name = arg->Value;
757 }
758 CommandLineSwitchCase(arg, "stream_name")
759 {
760 stream_name = arg->Value;
761 }
762 CommandLineSwitchEnd(arg)
763 } while ((arg = CommandLineFindNextArgumentA(arg)) != nullptr);
764
765 if (!client_name)
766 client_name = freerdp_getApplicationDetailsString();
767 if (!stream_name)
768 stream_name = freerdp_getApplicationDetailsString();
769
770 pulse->client_name = _strdup(client_name);
771 pulse->stream_name = _strdup(stream_name);
772 if (!pulse->client_name || !pulse->stream_name)
773 return ERROR_OUTOFMEMORY;
774 return CHANNEL_RC_OK;
775}
776
777FREERDP_ENTRY_POINT(UINT VCAPITYPE pulse_freerdp_rdpsnd_client_subsystem_entry(
779{
780 WINPR_ASSERT(pEntryPoints);
781
782 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)calloc(1, sizeof(rdpsndPulsePlugin));
783
784 if (!pulse)
785 return CHANNEL_RC_NO_MEMORY;
786
787 pulse->device.Open = rdpsnd_pulse_open;
788 pulse->device.FormatSupported = rdpsnd_pulse_format_supported;
789 pulse->device.GetVolume = rdpsnd_pulse_get_volume;
790 pulse->device.SetVolume = rdpsnd_pulse_set_volume;
791 pulse->device.Play = rdpsnd_pulse_play;
792 pulse->device.Close = rdpsnd_pulse_close;
793 pulse->device.Free = rdpsnd_pulse_free;
794 pulse->device.DefaultFormat = rdpsnd_pulse_default_format;
795
796 const ADDIN_ARGV* args = pEntryPoints->args;
797 UINT ret = rdpsnd_pulse_parse_addin_args(pulse, args);
798
799 if (ret != CHANNEL_RC_OK)
800 {
801 WLog_ERR(TAG, "error parsing arguments");
802 goto error;
803 }
804
805 pulse->reconnect_delay_seconds = 5;
806 pulse->reconnect_time = time(nullptr);
807
808 ret = CHANNEL_RC_NO_MEMORY;
809 pulse->mainloop = pa_threaded_mainloop_new();
810
811 if (!pulse->mainloop)
812 goto error;
813
814 pa_threaded_mainloop_lock(pulse->mainloop);
815
816 if (pa_threaded_mainloop_start(pulse->mainloop) < 0)
817 {
818 pa_threaded_mainloop_unlock(pulse->mainloop);
819 goto error;
820 }
821
822 pa_threaded_mainloop_unlock(pulse->mainloop);
823
824 if (!rdpsnd_pulse_context_connect((rdpsndDevicePlugin*)pulse))
825 goto error;
826
827 pEntryPoints->pRegisterRdpsndDevice(pEntryPoints->rdpsnd, (rdpsndDevicePlugin*)pulse);
828 return CHANNEL_RC_OK;
829error:
830 rdpsnd_pulse_free((rdpsndDevicePlugin*)pulse);
831 return ret;
832}