22#include <freerdp/config.h>
32#include <winpr/cast.h>
33#include <winpr/assert.h>
34#include <winpr/stream.h>
35#include <winpr/cmdline.h>
37#include <pulse/pulseaudio.h>
39#include <freerdp/types.h>
40#include <freerdp/codec/dsp.h>
42#include "rdpsnd_main.h"
46 rdpsndDevicePlugin device;
51 pa_threaded_mainloop* mainloop;
53 pa_sample_spec sample_spec;
57 time_t reconnect_delay_seconds;
58 time_t reconnect_time;
61static BOOL rdpsnd_check_pulse(rdpsndPulsePlugin* pulse, BOOL haveStream)
68 WLog_WARN(TAG,
"pulse->context=%p", pulse->context);
76 WLog_WARN(TAG,
"pulse->stream=%p", pulse->stream);
83 WLog_WARN(TAG,
"pulse->mainloop=%p", pulse->mainloop);
90static BOOL rdpsnd_pulse_format_supported(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* format);
92static void rdpsnd_pulse_get_sink_info(pa_context* c,
const pa_sink_info* i,
93 WINPR_ATTR_UNUSED
int eol,
void* userdata)
95 UINT16 dwVolumeLeft = ((50 * 0xFFFF) / 100);
96 UINT16 dwVolumeRight = ((50 * 0xFFFF) / 100);
97 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
100 if (!rdpsnd_check_pulse(pulse, FALSE) || !i)
103 for (uint8_t x = 0; x < i->volume.channels; x++)
105 pa_volume_t volume = i->volume.values[x];
107 if (volume >= PA_VOLUME_NORM)
108 volume = PA_VOLUME_NORM - 1;
113 dwVolumeLeft = (UINT16)volume;
117 dwVolumeRight = (UINT16)volume;
125 pulse->volume = ((UINT32)dwVolumeLeft << 16U) | dwVolumeRight;
128static void rdpsnd_pulse_context_state_callback(pa_context* context,
void* userdata)
130 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
132 WINPR_ASSERT(context);
135 pa_context_state_t state = pa_context_get_state(context);
139 case PA_CONTEXT_READY:
140 pa_threaded_mainloop_signal(pulse->mainloop, 0);
143 case PA_CONTEXT_FAILED:
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);
152 case PA_CONTEXT_TERMINATED:
153 pa_threaded_mainloop_signal(pulse->mainloop, 0);
161static BOOL rdpsnd_pulse_connect(rdpsndDevicePlugin* device)
164 pa_operation* o = NULL;
165 pa_context_state_t state = PA_CONTEXT_FAILED;
166 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
168 if (!rdpsnd_check_pulse(pulse, FALSE))
171 pa_threaded_mainloop_lock(pulse->mainloop);
173 if (pa_context_connect(pulse->context, NULL, 0, NULL) < 0)
175 pa_threaded_mainloop_unlock(pulse->mainloop);
181 state = pa_context_get_state(pulse->context);
183 if (state == PA_CONTEXT_READY)
186 if (!PA_CONTEXT_IS_GOOD(state))
191 pa_threaded_mainloop_wait(pulse->mainloop);
194 o = pa_context_get_sink_info_by_index(pulse->context, 0, rdpsnd_pulse_get_sink_info, pulse);
197 pa_operation_unref(o);
199 if (state == PA_CONTEXT_READY)
205 pa_context_disconnect(pulse->context);
209 pa_threaded_mainloop_unlock(pulse->mainloop);
213static void rdpsnd_pulse_stream_success_callback(WINPR_ATTR_UNUSED pa_stream* stream,
214 WINPR_ATTR_UNUSED
int success,
void* userdata)
216 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
218 if (!rdpsnd_check_pulse(pulse, TRUE))
221 pa_threaded_mainloop_signal(pulse->mainloop, 0);
224static void rdpsnd_pulse_wait_for_operation(rdpsndPulsePlugin* pulse, pa_operation* operation)
226 if (!rdpsnd_check_pulse(pulse, TRUE))
232 while (pa_operation_get_state(operation) == PA_OPERATION_RUNNING)
234 pa_threaded_mainloop_wait(pulse->mainloop);
237 pa_operation_unref(operation);
240static void rdpsnd_pulse_stream_state_callback(pa_stream* stream,
void* userdata)
242 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
244 WINPR_ASSERT(stream);
245 if (!rdpsnd_check_pulse(pulse, TRUE))
248 pa_stream_state_t state = pa_stream_get_state(stream);
252 case PA_STREAM_READY:
253 pa_threaded_mainloop_signal(pulse->mainloop, 0);
256 case PA_STREAM_FAILED:
257 case PA_STREAM_TERMINATED:
259 pulse->stream = NULL;
260 pa_threaded_mainloop_signal(pulse->mainloop, 0);
268static void rdpsnd_pulse_stream_request_callback(pa_stream* stream, WINPR_ATTR_UNUSED
size_t length,
271 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
273 WINPR_ASSERT(stream);
274 if (!rdpsnd_check_pulse(pulse, TRUE))
277 pa_threaded_mainloop_signal(pulse->mainloop, 0);
280static void rdpsnd_pulse_close(rdpsndDevicePlugin* device)
282 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
286 if (!rdpsnd_check_pulse(pulse, FALSE))
289 pa_threaded_mainloop_lock(pulse->mainloop);
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;
298 pa_threaded_mainloop_unlock(pulse->mainloop);
301static BOOL rdpsnd_pulse_set_format_spec(rdpsndPulsePlugin* pulse,
const AUDIO_FORMAT* format)
303 pa_sample_spec sample_spec = { 0 };
305 WINPR_ASSERT(format);
307 if (!rdpsnd_check_pulse(pulse, FALSE))
310 if (!rdpsnd_pulse_format_supported(&pulse->device, format))
313 sample_spec.rate = format->nSamplesPerSec;
314 sample_spec.channels = WINPR_ASSERTING_INT_CAST(uint8_t, format->nChannels);
316 switch (format->wFormatTag)
318 case WAVE_FORMAT_PCM:
319 switch (format->wBitsPerSample)
322 sample_spec.format = PA_SAMPLE_U8;
326 sample_spec.format = PA_SAMPLE_S16LE;
339 pulse->sample_spec = sample_spec;
343static BOOL rdpsnd_pulse_context_connect(rdpsndDevicePlugin* device)
345 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
349 pa_context_new(pa_threaded_mainloop_get_api(pulse->mainloop), pulse->client_name);
354 pa_context_set_state_callback(pulse->context, rdpsnd_pulse_context_state_callback, pulse);
356 if (!rdpsnd_pulse_connect((rdpsndDevicePlugin*)pulse))
362static BOOL rdpsnd_pulse_open_stream(rdpsndDevicePlugin* device)
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;
371 if (pa_sample_spec_valid(&pulse->sample_spec) == 0)
373 pa_sample_spec_snprint(ss,
sizeof(ss), &pulse->sample_spec);
377 pa_threaded_mainloop_lock(pulse->mainloop);
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);
386 if (!rdpsnd_check_pulse(pulse, FALSE))
388 pa_threaded_mainloop_unlock(pulse->mainloop);
392 pulse->stream = pa_stream_new(pulse->context, pulse->stream_name, &pulse->sample_spec, NULL);
396 pa_threaded_mainloop_unlock(pulse->mainloop);
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;
405 if (pulse->latency > 0)
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;
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,
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);
429 while (pulse->stream)
431 state = pa_stream_get_state(pulse->stream);
433 if (state == PA_STREAM_READY)
436 if (!PA_STREAM_IS_GOOD(state))
441 pa_threaded_mainloop_wait(pulse->mainloop);
444 pa_threaded_mainloop_unlock(pulse->mainloop);
446 if (state == PA_STREAM_READY)
449 rdpsnd_pulse_close(device);
453static BOOL rdpsnd_pulse_open(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* format,
456 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
458 WINPR_ASSERT(format);
460 if (!rdpsnd_check_pulse(pulse, FALSE))
463 if (!rdpsnd_pulse_set_format_spec(pulse, format))
466 pulse->latency = latency;
468 return rdpsnd_pulse_open_stream(device);
471static void rdpsnd_pulse_free(rdpsndDevicePlugin* device)
473 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
478 rdpsnd_pulse_close(device);
481 pa_threaded_mainloop_stop(pulse->mainloop);
485 pa_context_disconnect(pulse->context);
486 pa_context_unref(pulse->context);
487 pulse->context = NULL;
492 pa_threaded_mainloop_free(pulse->mainloop);
493 pulse->mainloop = NULL;
496 free(pulse->device_name);
497 free(pulse->client_name);
498 free(pulse->stream_name);
502static BOOL rdpsnd_pulse_default_format(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* desired,
505 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
507 if (!pulse || !defaultFormat)
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;
521 defaultFormat->nBlockAlign = defaultFormat->nChannels * defaultFormat->wBitsPerSample / 8;
522 defaultFormat->nAvgBytesPerSec = defaultFormat->nBlockAlign * defaultFormat->nSamplesPerSec;
526BOOL rdpsnd_pulse_format_supported(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* format)
528 WINPR_ASSERT(device);
529 WINPR_ASSERT(format);
531 switch (format->wFormatTag)
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))
550static UINT32 rdpsnd_pulse_get_volume(rdpsndDevicePlugin* device)
552 pa_operation* o = NULL;
553 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
555 if (!rdpsnd_check_pulse(pulse, FALSE))
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);
561 pa_operation_unref(o);
562 pa_threaded_mainloop_unlock(pulse->mainloop);
563 return pulse->volume;
566static void rdpsnd_set_volume_success_cb(pa_context* c,
int success,
void* userdata)
568 rdpsndPulsePlugin* pulse = userdata;
570 if (!rdpsnd_check_pulse(pulse, TRUE))
574 WLog_INFO(TAG,
"%d", success);
577static BOOL rdpsnd_pulse_set_volume(rdpsndDevicePlugin* device, UINT32 value)
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;
585 if (!rdpsnd_check_pulse(pulse, TRUE))
587 WLog_WARN(TAG,
"%s called before pulse backend was initialized");
591 left = (pa_volume_t)(value & 0xFFFF);
592 right = (pa_volume_t)((value >> 16) & 0xFFFF);
593 pa_cvolume_init(&cv);
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);
602 pa_operation_unref(operation);
604 pa_threaded_mainloop_unlock(pulse->mainloop);
608static UINT rdpsnd_pulse_play(rdpsndDevicePlugin* device,
const BYTE* data,
size_t size)
611 void* pa_data = NULL;
613 pa_usec_t latency = 0;
615 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
620 pa_threaded_mainloop_lock(pulse->mainloop);
622 if (!rdpsnd_check_pulse(pulse, TRUE))
624 pa_threaded_mainloop_unlock(pulse->mainloop);
626 WLog_DBG(TAG,
"reconnecting playback stream");
627 rdpsnd_pulse_open_stream(device);
635 status = pa_stream_begin_write(pulse->stream, &pa_data, &length);
640 memcpy(pa_data, data, length);
642 status = pa_stream_write(pulse->stream, pa_data, length, NULL, 0LL, PA_SEEK_RELATIVE);
653 if (pa_stream_get_latency(pulse->stream, &latency, &negative) != 0)
656 pa_threaded_mainloop_unlock(pulse->mainloop);
658 const pa_usec_t val = latency / 1000;
659 if (val > UINT32_MAX)
664static UINT rdpsnd_pulse_parse_addin_args(rdpsndPulsePlugin* pulse,
const ADDIN_ARGV* 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 }
677 COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
682 const int status = CommandLineParseArgumentsA(args->argc, args->argv, rdpsnd_pulse_args, flags,
686 return ERROR_INVALID_DATA;
690 const char* client_name = NULL;
691 const char* stream_name = NULL;
694 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
697 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg,
"dev")
699 pulse->device_name = _strdup(arg->Value);
701 if (!pulse->device_name)
702 return ERROR_OUTOFMEMORY;
704 CommandLineSwitchCase(arg,
"reconnect_delay_seconds")
706 unsigned long val = strtoul(arg->Value, NULL, 0);
708 if ((errno != 0) || (val > INT32_MAX))
709 return ERROR_INVALID_DATA;
711 pulse->reconnect_delay_seconds = (time_t)val;
713 CommandLineSwitchCase(arg,
"client_name")
715 client_name = arg->Value;
717 CommandLineSwitchCase(arg,
"stream_name")
719 stream_name = arg->Value;
721 CommandLineSwitchEnd(arg)
722 }
while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
725 client_name =
"freerdp";
727 stream_name =
"freerdp";
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;
736FREERDP_ENTRY_POINT(UINT VCAPITYPE pulse_freerdp_rdpsnd_client_subsystem_entry(
739 WINPR_ASSERT(pEntryPoints);
741 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)calloc(1,
sizeof(rdpsndPulsePlugin));
744 return CHANNEL_RC_NO_MEMORY;
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;
756 UINT ret = rdpsnd_pulse_parse_addin_args(pulse, args);
758 if (ret != CHANNEL_RC_OK)
760 WLog_ERR(TAG,
"error parsing arguments");
764 pulse->reconnect_delay_seconds = 5;
765 pulse->reconnect_time = time(NULL);
767 ret = CHANNEL_RC_NO_MEMORY;
768 pulse->mainloop = pa_threaded_mainloop_new();
770 if (!pulse->mainloop)
773 pa_threaded_mainloop_lock(pulse->mainloop);
775 if (pa_threaded_mainloop_start(pulse->mainloop) < 0)
777 pa_threaded_mainloop_unlock(pulse->mainloop);
781 pa_threaded_mainloop_unlock(pulse->mainloop);
783 if (!rdpsnd_pulse_context_connect((rdpsndDevicePlugin*)pulse))
786 pEntryPoints->pRegisterRdpsndDevice(pEntryPoints->rdpsnd, (rdpsndDevicePlugin*)pulse);
787 return CHANNEL_RC_OK;
789 rdpsnd_pulse_free((rdpsndDevicePlugin*)pulse);