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