22#include <freerdp/config.h>
23#include <freerdp/utils/helpers.h>
33#include <winpr/cast.h>
34#include <winpr/assert.h>
35#include <winpr/stream.h>
36#include <winpr/cmdline.h>
38#include <pulse/pulseaudio.h>
40#include <freerdp/types.h>
41#include <freerdp/codec/dsp.h>
43#include "rdpsnd_main.h"
47 rdpsndDevicePlugin device;
52 pa_threaded_mainloop* mainloop;
54 pa_sample_spec sample_spec;
58 time_t reconnect_delay_seconds;
59 time_t reconnect_time;
62static BOOL rdpsnd_check_pulse(rdpsndPulsePlugin* pulse, BOOL haveStream)
69 WLog_WARN(TAG,
"pulse->context=nullptr");
77 WLog_WARN(TAG,
"pulse->stream=%p", WINPR_CXX_COMPAT_CAST(
const void*, pulse->stream));
84 WLog_WARN(TAG,
"pulse->mainloop=%p", WINPR_CXX_COMPAT_CAST(
const void*, pulse->mainloop));
91static BOOL rdpsnd_pulse_format_supported(WINPR_ATTR_UNUSED rdpsndDevicePlugin* device,
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)
97 UINT16 dwVolumeLeft = ((50 * 0xFFFF) / 100);
98 UINT16 dwVolumeRight = ((50 * 0xFFFF) / 100);
99 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
102 if (!rdpsnd_check_pulse(pulse, FALSE) || !i)
105 for (uint8_t x = 0; x < i->volume.channels; x++)
107 pa_volume_t volume = i->volume.values[x];
109 if (volume >= PA_VOLUME_NORM)
110 volume = PA_VOLUME_NORM - 1;
115 dwVolumeLeft = (UINT16)volume;
119 dwVolumeRight = (UINT16)volume;
127 pulse->volume = ((UINT32)dwVolumeLeft << 16U) | dwVolumeRight;
130static void rdpsnd_pulse_context_state_callback(pa_context* context,
void* userdata)
132 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
134 WINPR_ASSERT(context);
137 pa_context_state_t state = pa_context_get_state(context);
141 case PA_CONTEXT_READY:
142 pa_threaded_mainloop_signal(pulse->mainloop, 0);
145 case PA_CONTEXT_FAILED:
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);
154 case PA_CONTEXT_TERMINATED:
155 pa_threaded_mainloop_signal(pulse->mainloop, 0);
163static BOOL rdpsnd_pulse_connect(rdpsndDevicePlugin* device)
166 pa_operation* o =
nullptr;
167 pa_context_state_t state = PA_CONTEXT_FAILED;
168 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
170 if (!rdpsnd_check_pulse(pulse, FALSE))
173 pa_threaded_mainloop_lock(pulse->mainloop);
175 if (pa_context_connect(pulse->context,
nullptr, PA_CONTEXT_NOFLAGS,
nullptr) < 0)
177 pa_threaded_mainloop_unlock(pulse->mainloop);
183 while (pulse->context)
185 state = pa_context_get_state(pulse->context);
187 if (state == PA_CONTEXT_READY)
190 if (!PA_CONTEXT_IS_GOOD(state))
195 pa_threaded_mainloop_wait(pulse->mainloop);
200 o = pa_context_get_sink_info_by_index(pulse->context, 0, rdpsnd_pulse_get_sink_info, pulse);
203 pa_operation_unref(o);
206 if (pulse->context && (state == PA_CONTEXT_READY))
213 pa_context_disconnect(pulse->context);
217 pa_threaded_mainloop_unlock(pulse->mainloop);
221static void rdpsnd_pulse_stream_success_callback(WINPR_ATTR_UNUSED pa_stream* stream,
222 WINPR_ATTR_UNUSED
int success,
void* userdata)
224 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
226 if (!rdpsnd_check_pulse(pulse, TRUE))
229 pa_threaded_mainloop_signal(pulse->mainloop, 0);
232static void rdpsnd_pulse_wait_for_operation(rdpsndPulsePlugin* pulse, pa_operation* operation)
234 if (!rdpsnd_check_pulse(pulse, TRUE))
240 while (pa_operation_get_state(operation) == PA_OPERATION_RUNNING)
242 pa_threaded_mainloop_wait(pulse->mainloop);
245 pa_operation_unref(operation);
248static void rdpsnd_pulse_stream_state_callback(pa_stream* stream,
void* userdata)
250 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
252 WINPR_ASSERT(stream);
253 if (!rdpsnd_check_pulse(pulse, TRUE))
256 pa_stream_state_t state = pa_stream_get_state(stream);
260 case PA_STREAM_READY:
261 pa_threaded_mainloop_signal(pulse->mainloop, 0);
264 case PA_STREAM_FAILED:
265 case PA_STREAM_TERMINATED:
269 pa_stream_unref(pulse->stream);
270 pulse->stream =
nullptr;
271 pa_threaded_mainloop_signal(pulse->mainloop, 0);
279static void rdpsnd_pulse_stream_request_callback(WINPR_ATTR_UNUSED pa_stream* stream,
280 WINPR_ATTR_UNUSED
size_t length,
void* userdata)
282 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
284 WINPR_ASSERT(stream);
285 if (!rdpsnd_check_pulse(pulse, TRUE))
288 pa_threaded_mainloop_signal(pulse->mainloop, 0);
291static void rdpsnd_pulse_close(rdpsndDevicePlugin* device)
293 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
297 if (!rdpsnd_check_pulse(pulse, FALSE))
300 pa_threaded_mainloop_lock(pulse->mainloop);
303 rdpsnd_pulse_wait_for_operation(
304 pulse, pa_stream_drain(pulse->stream, rdpsnd_pulse_stream_success_callback, pulse));
309 pa_stream_disconnect(pulse->stream);
310 pa_stream_unref(pulse->stream);
311 pulse->stream =
nullptr;
314 pa_threaded_mainloop_unlock(pulse->mainloop);
317static BOOL rdpsnd_pulse_set_format_spec(rdpsndPulsePlugin* pulse,
const AUDIO_FORMAT* format)
319 WINPR_ASSERT(format);
321 if (!rdpsnd_check_pulse(pulse, FALSE))
324 if (!rdpsnd_pulse_format_supported(&pulse->device, format))
327 pa_sample_format_t sformat = PA_SAMPLE_INVALID;
328 switch (format->wFormatTag)
330 case WAVE_FORMAT_PCM:
331 switch (format->wBitsPerSample)
334 sformat = PA_SAMPLE_U8;
338 sformat = PA_SAMPLE_S16LE;
351 const pa_sample_spec sample_spec = { .format = sformat,
352 .rate = format->nSamplesPerSec,
354 WINPR_ASSERTING_INT_CAST(uint8_t, format->nChannels) };
355 pulse->sample_spec = sample_spec;
359static BOOL rdpsnd_pulse_context_connect(rdpsndDevicePlugin* device)
361 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
365 pa_context_new(pa_threaded_mainloop_get_api(pulse->mainloop), pulse->client_name);
370 pa_context_set_state_callback(pulse->context, rdpsnd_pulse_context_state_callback, pulse);
372 return rdpsnd_pulse_connect(&pulse->device);
375static BOOL rdpsnd_pulse_open_stream(rdpsndDevicePlugin* device)
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;
384 if (pa_sample_spec_valid(&pulse->sample_spec) == 0)
386 pa_sample_spec_snprint(ss,
sizeof(ss), &pulse->sample_spec);
390 pa_threaded_mainloop_lock(pulse->mainloop);
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);
399 if (!rdpsnd_check_pulse(pulse, FALSE))
401 pa_threaded_mainloop_unlock(pulse->mainloop);
405 pulse->stream = pa_stream_new(pulse->context, pulse->stream_name, &pulse->sample_spec,
nullptr);
409 pa_threaded_mainloop_unlock(pulse->mainloop);
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;
418 if (pulse->latency > 0)
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;
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,
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);
442 while (pulse->stream)
444 state = pa_stream_get_state(pulse->stream);
446 if (state == PA_STREAM_READY)
449 if (!PA_STREAM_IS_GOOD(state))
454 pa_threaded_mainloop_wait(pulse->mainloop);
457 pa_threaded_mainloop_unlock(pulse->mainloop);
459 if (state == PA_STREAM_READY)
462 rdpsnd_pulse_close(device);
466static BOOL rdpsnd_pulse_open(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* format,
469 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
471 WINPR_ASSERT(format);
473 if (!rdpsnd_check_pulse(pulse, FALSE))
476 if (!rdpsnd_pulse_set_format_spec(pulse, format))
479 pulse->latency = latency;
481 return rdpsnd_pulse_open_stream(device);
484static void rdpsnd_pulse_free(rdpsndDevicePlugin* device)
486 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
491 rdpsnd_pulse_close(device);
494 pa_threaded_mainloop_stop(pulse->mainloop);
498 pa_context_disconnect(pulse->context);
499 pa_context_unref(pulse->context);
500 pulse->context =
nullptr;
505 pa_threaded_mainloop_free(pulse->mainloop);
506 pulse->mainloop =
nullptr;
509 free(pulse->device_name);
510 free(pulse->client_name);
511 free(pulse->stream_name);
515static BOOL rdpsnd_pulse_default_format(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* desired,
518 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
520 if (!pulse || !defaultFormat)
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;
534 defaultFormat->nBlockAlign = defaultFormat->nChannels * defaultFormat->wBitsPerSample / 8;
535 defaultFormat->nAvgBytesPerSec = defaultFormat->nBlockAlign * defaultFormat->nSamplesPerSec;
539BOOL rdpsnd_pulse_format_supported(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* format)
541 WINPR_ASSERT(device);
542 WINPR_ASSERT(format);
544 switch (format->wFormatTag)
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))
563static UINT32 rdpsnd_pulse_get_volume(rdpsndDevicePlugin* device)
565 pa_operation* o =
nullptr;
566 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
568 if (!pulse || !pulse->mainloop)
571 pa_threaded_mainloop_lock(pulse->mainloop);
573 if (rdpsnd_check_pulse(pulse, FALSE))
575 o = pa_context_get_sink_info_by_index(pulse->context, 0, rdpsnd_pulse_get_sink_info, pulse);
577 pa_operation_unref(o);
579 pa_threaded_mainloop_unlock(pulse->mainloop);
580 return pulse->volume;
583static void rdpsnd_set_volume_success_cb(WINPR_ATTR_UNUSED pa_context* c,
int success,
586 rdpsndPulsePlugin* pulse = userdata;
588 if (!rdpsnd_check_pulse(pulse, TRUE))
592 WLog_INFO(TAG,
"%d", success);
595static BOOL rdpsnd_pulse_set_volume(rdpsndDevicePlugin* device, UINT32 value)
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;
603 if (!pulse || !pulse->mainloop)
605 WLog_WARN(TAG,
"called before pulse backend was initialized");
609 left = (pa_volume_t)(value & 0xFFFF);
610 right = (pa_volume_t)((value >> 16) & 0xFFFF);
611 pa_cvolume_init(&cv);
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);
619 if (!rdpsnd_check_pulse(pulse, TRUE))
621 pa_threaded_mainloop_unlock(pulse->mainloop);
622 WLog_WARN(TAG,
"no pulse stream, not setting volume");
626 operation = pa_context_set_sink_input_volume(pulse->context, pa_stream_get_index(pulse->stream),
627 &cv, rdpsnd_set_volume_success_cb, pulse);
630 pa_operation_unref(operation);
632 pa_threaded_mainloop_unlock(pulse->mainloop);
636static UINT rdpsnd_pulse_play(rdpsndDevicePlugin* device,
const BYTE* data,
size_t size)
639 void* pa_data =
nullptr;
641 pa_usec_t latency = 0;
643 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
648 pa_threaded_mainloop_lock(pulse->mainloop);
650 if (!rdpsnd_check_pulse(pulse, TRUE))
652 pa_threaded_mainloop_unlock(pulse->mainloop);
654 WLog_DBG(TAG,
"reconnecting playback stream");
655 rdpsnd_pulse_open_stream(device);
663 status = pa_stream_begin_write(pulse->stream, &pa_data, &length);
673 if (!pa_data || (length == 0))
676 pa_stream_cancel_write(pulse->stream);
677 WLog_DBG(TAG,
"dropping %" PRIuz
" bytes, no buffer space", size);
681 memcpy(pa_data, data, length);
683 status = pa_stream_write(pulse->stream, pa_data, length,
nullptr, 0LL, PA_SEEK_RELATIVE);
694 if (pa_stream_get_latency(pulse->stream, &latency, &negative) != 0)
697 pa_threaded_mainloop_unlock(pulse->mainloop);
699 const pa_usec_t val = latency / 1000;
700 if (val > UINT32_MAX)
705static UINT rdpsnd_pulse_parse_addin_args(rdpsndPulsePlugin* pulse,
const ADDIN_ARGV* 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 }
718 COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
723 const int status = CommandLineParseArgumentsA(args->argc, args->argv, rdpsnd_pulse_args, flags,
724 pulse,
nullptr,
nullptr);
727 return ERROR_INVALID_DATA;
731 const char* client_name =
nullptr;
732 const char* stream_name =
nullptr;
735 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
738 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg,
"dev")
740 pulse->device_name = _strdup(arg->Value);
742 if (!pulse->device_name)
743 return ERROR_OUTOFMEMORY;
745 CommandLineSwitchCase(arg,
"reconnect_delay_seconds")
747 unsigned long val = strtoul(arg->Value,
nullptr, 0);
749 if ((errno != 0) || (val > INT32_MAX))
750 return ERROR_INVALID_DATA;
752 pulse->reconnect_delay_seconds = (time_t)val;
754 CommandLineSwitchCase(arg,
"client_name")
756 client_name = arg->Value;
758 CommandLineSwitchCase(arg,
"stream_name")
760 stream_name = arg->Value;
762 CommandLineSwitchEnd(arg)
763 }
while ((arg = CommandLineFindNextArgumentA(arg)) !=
nullptr);
766 client_name = freerdp_getApplicationDetailsString();
768 stream_name = freerdp_getApplicationDetailsString();
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;
777FREERDP_ENTRY_POINT(UINT VCAPITYPE pulse_freerdp_rdpsnd_client_subsystem_entry(
780 WINPR_ASSERT(pEntryPoints);
782 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)calloc(1,
sizeof(rdpsndPulsePlugin));
785 return CHANNEL_RC_NO_MEMORY;
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;
797 UINT ret = rdpsnd_pulse_parse_addin_args(pulse, args);
799 if (ret != CHANNEL_RC_OK)
801 WLog_ERR(TAG,
"error parsing arguments");
805 pulse->reconnect_delay_seconds = 5;
806 pulse->reconnect_time = time(
nullptr);
808 ret = CHANNEL_RC_NO_MEMORY;
809 pulse->mainloop = pa_threaded_mainloop_new();
811 if (!pulse->mainloop)
814 pa_threaded_mainloop_lock(pulse->mainloop);
816 if (pa_threaded_mainloop_start(pulse->mainloop) < 0)
818 pa_threaded_mainloop_unlock(pulse->mainloop);
822 pa_threaded_mainloop_unlock(pulse->mainloop);
824 if (!rdpsnd_pulse_context_connect((rdpsndDevicePlugin*)pulse))
827 pEntryPoints->pRegisterRdpsndDevice(pEntryPoints->rdpsnd, (rdpsndDevicePlugin*)pulse);
828 return CHANNEL_RC_OK;
830 rdpsnd_pulse_free((rdpsndDevicePlugin*)pulse);