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>
37#include <winpr/sysinfo.h>
39#include <pulse/pulseaudio.h>
41#include <freerdp/types.h>
42#include <freerdp/codec/dsp.h>
44#include "rdpsnd_main.h"
47#define PULSE_DEFAULT_LATENCY_MS 100
51 rdpsndDevicePlugin device;
56 pa_threaded_mainloop* mainloop;
58 pa_sample_spec sample_spec;
62 time_t reconnect_delay_seconds;
63 time_t reconnect_time;
67static BOOL rdpsnd_check_pulse(rdpsndPulsePlugin* pulse, BOOL haveStream)
74 WLog_WARN(TAG,
"pulse->context=nullptr");
82 WLog_WARN(TAG,
"pulse->stream=%p", WINPR_CXX_COMPAT_CAST(
const void*, pulse->stream));
89 WLog_WARN(TAG,
"pulse->mainloop=%p", WINPR_CXX_COMPAT_CAST(
const void*, pulse->mainloop));
96static BOOL rdpsnd_pulse_format_supported(WINPR_ATTR_UNUSED rdpsndDevicePlugin* device,
99static void rdpsnd_pulse_get_sink_info(WINPR_ATTR_UNUSED pa_context* c,
const pa_sink_info* i,
100 WINPR_ATTR_UNUSED
int eol,
void* userdata)
102 UINT16 dwVolumeLeft = ((50 * 0xFFFF) / 100);
103 UINT16 dwVolumeRight = ((50 * 0xFFFF) / 100);
104 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
107 if (!rdpsnd_check_pulse(pulse, FALSE) || !i)
110 for (uint8_t x = 0; x < i->volume.channels; x++)
112 pa_volume_t volume = i->volume.values[x];
114 if (volume >= PA_VOLUME_NORM)
115 volume = PA_VOLUME_NORM - 1;
120 dwVolumeLeft = (UINT16)volume;
124 dwVolumeRight = (UINT16)volume;
132 pulse->volume = ((UINT32)dwVolumeLeft << 16U) | dwVolumeRight;
135static void rdpsnd_pulse_context_state_callback(pa_context* context,
void* userdata)
137 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
139 WINPR_ASSERT(context);
142 pa_context_state_t state = pa_context_get_state(context);
146 case PA_CONTEXT_READY:
147 pa_threaded_mainloop_signal(pulse->mainloop, 0);
150 case PA_CONTEXT_FAILED:
152 pa_context_unref(pulse->context);
153 pulse->context =
nullptr;
154 if (pulse->reconnect_delay_seconds >= 0)
155 pulse->reconnect_time = time(
nullptr) + pulse->reconnect_delay_seconds;
156 pa_threaded_mainloop_signal(pulse->mainloop, 0);
159 case PA_CONTEXT_TERMINATED:
160 pa_threaded_mainloop_signal(pulse->mainloop, 0);
168static BOOL rdpsnd_pulse_connect(rdpsndDevicePlugin* device)
171 pa_operation* o =
nullptr;
172 pa_context_state_t state = PA_CONTEXT_FAILED;
173 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
175 if (!rdpsnd_check_pulse(pulse, FALSE))
178 pa_threaded_mainloop_lock(pulse->mainloop);
180 if (pa_context_connect(pulse->context,
nullptr, PA_CONTEXT_NOFLAGS,
nullptr) < 0)
182 pa_threaded_mainloop_unlock(pulse->mainloop);
188 while (pulse->context)
190 state = pa_context_get_state(pulse->context);
192 if (state == PA_CONTEXT_READY)
195 if (!PA_CONTEXT_IS_GOOD(state))
200 pa_threaded_mainloop_wait(pulse->mainloop);
205 o = pa_context_get_sink_info_by_index(pulse->context, 0, rdpsnd_pulse_get_sink_info, pulse);
208 pa_operation_unref(o);
211 if (pulse->context && (state == PA_CONTEXT_READY))
218 pa_context_disconnect(pulse->context);
222 pa_threaded_mainloop_unlock(pulse->mainloop);
226static void rdpsnd_pulse_stream_success_callback(WINPR_ATTR_UNUSED pa_stream* stream,
227 WINPR_ATTR_UNUSED
int success,
void* userdata)
229 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
231 if (!rdpsnd_check_pulse(pulse, TRUE))
234 pa_threaded_mainloop_signal(pulse->mainloop, 0);
237static void rdpsnd_pulse_wait_for_operation(rdpsndPulsePlugin* pulse, pa_operation* operation)
239 if (!rdpsnd_check_pulse(pulse, TRUE))
245 while (pa_operation_get_state(operation) == PA_OPERATION_RUNNING)
247 pa_threaded_mainloop_wait(pulse->mainloop);
250 pa_operation_unref(operation);
253static void rdpsnd_pulse_stream_state_callback(pa_stream* stream,
void* userdata)
255 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
257 WINPR_ASSERT(stream);
258 if (!rdpsnd_check_pulse(pulse, TRUE))
261 pa_stream_state_t state = pa_stream_get_state(stream);
265 case PA_STREAM_READY:
266 pa_threaded_mainloop_signal(pulse->mainloop, 0);
269 case PA_STREAM_FAILED:
270 case PA_STREAM_TERMINATED:
274 pa_stream_unref(pulse->stream);
275 pulse->stream =
nullptr;
276 pa_threaded_mainloop_signal(pulse->mainloop, 0);
284static void rdpsnd_pulse_stream_request_callback(WINPR_ATTR_UNUSED pa_stream* stream,
285 WINPR_ATTR_UNUSED
size_t length,
void* userdata)
287 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
289 WINPR_ASSERT(stream);
290 if (!rdpsnd_check_pulse(pulse, TRUE))
293 pa_threaded_mainloop_signal(pulse->mainloop, 0);
296static void rdpsnd_pulse_close(rdpsndDevicePlugin* device)
298 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
302 if (!rdpsnd_check_pulse(pulse, FALSE))
305 pa_threaded_mainloop_lock(pulse->mainloop);
308 rdpsnd_pulse_wait_for_operation(
309 pulse, pa_stream_drain(pulse->stream, rdpsnd_pulse_stream_success_callback, pulse));
314 pa_stream_disconnect(pulse->stream);
315 pa_stream_unref(pulse->stream);
316 pulse->stream =
nullptr;
319 pa_threaded_mainloop_unlock(pulse->mainloop);
322static BOOL rdpsnd_pulse_set_format_spec(rdpsndPulsePlugin* pulse,
const AUDIO_FORMAT* format)
324 WINPR_ASSERT(format);
326 if (!rdpsnd_check_pulse(pulse, FALSE))
329 if (!rdpsnd_pulse_format_supported(&pulse->device, format))
332 pa_sample_format_t sformat = PA_SAMPLE_INVALID;
333 switch (format->wFormatTag)
335 case WAVE_FORMAT_PCM:
336 switch (format->wBitsPerSample)
339 sformat = PA_SAMPLE_U8;
343 sformat = PA_SAMPLE_S16LE;
356 const pa_sample_spec sample_spec = { .format = sformat,
357 .rate = format->nSamplesPerSec,
359 WINPR_ASSERTING_INT_CAST(uint8_t, format->nChannels) };
360 pulse->sample_spec = sample_spec;
364static BOOL rdpsnd_pulse_context_connect(rdpsndDevicePlugin* device)
366 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
370 pa_context_new(pa_threaded_mainloop_get_api(pulse->mainloop), pulse->client_name);
375 pa_context_set_state_callback(pulse->context, rdpsnd_pulse_context_state_callback, pulse);
377 return rdpsnd_pulse_connect(&pulse->device);
380static BOOL rdpsnd_pulse_open_stream(rdpsndDevicePlugin* device)
382 pa_stream_state_t state = PA_STREAM_FAILED;
383 int flags = PA_STREAM_NOFLAGS;
384 pa_buffer_attr buffer_attr = WINPR_C_ARRAY_INIT;
385 char ss[PA_SAMPLE_SPEC_SNPRINT_MAX] = WINPR_C_ARRAY_INIT;
386 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
390 if (pa_sample_spec_valid(&pulse->sample_spec) == 0)
392 pa_sample_spec_snprint(ss,
sizeof(ss), &pulse->sample_spec);
396 pa_threaded_mainloop_lock(pulse->mainloop);
399 pa_threaded_mainloop_unlock(pulse->mainloop);
400 if (pulse->reconnect_delay_seconds >= 0 && time(
nullptr) - pulse->reconnect_time >= 0)
401 rdpsnd_pulse_context_connect(device);
402 pa_threaded_mainloop_lock(pulse->mainloop);
405 if (!rdpsnd_check_pulse(pulse, FALSE))
407 pa_threaded_mainloop_unlock(pulse->mainloop);
411 pulse->stream = pa_stream_new(pulse->context, pulse->stream_name, &pulse->sample_spec,
nullptr);
415 pa_threaded_mainloop_unlock(pulse->mainloop);
420 pa_stream_set_state_callback(pulse->stream, rdpsnd_pulse_stream_state_callback, pulse);
421 pa_stream_set_write_callback(pulse->stream, rdpsnd_pulse_stream_request_callback, pulse);
422 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE | PA_STREAM_ADJUST_LATENCY;
428 const UINT32 ms = (pulse->latency > 0) ? pulse->latency : PULSE_DEFAULT_LATENCY_MS;
429 const size_t val = pa_usec_to_bytes(1000ULL * ms, &pulse->sample_spec);
430 buffer_attr.maxlength = UINT32_MAX;
431 buffer_attr.tlength = (val > UINT32_MAX) ? UINT32_MAX : (UINT32)val;
432 buffer_attr.prebuf = UINT32_MAX;
433 buffer_attr.minreq = UINT32_MAX;
434 buffer_attr.fragsize = UINT32_MAX;
437 pa_stream_flags_t eflags = (pa_stream_flags_t)flags;
438 if (pa_stream_connect_playback(pulse->stream, pulse->device_name, &buffer_attr, eflags,
nullptr,
441 WLog_ERR(TAG,
"error connecting playback stream");
442 pa_stream_unref(pulse->stream);
443 pulse->stream =
nullptr;
444 pa_threaded_mainloop_unlock(pulse->mainloop);
448 while (pulse->stream)
450 state = pa_stream_get_state(pulse->stream);
452 if (state == PA_STREAM_READY)
455 if (!PA_STREAM_IS_GOOD(state))
460 pa_threaded_mainloop_wait(pulse->mainloop);
463 pa_threaded_mainloop_unlock(pulse->mainloop);
465 if (state == PA_STREAM_READY)
468 rdpsnd_pulse_close(device);
472static BOOL rdpsnd_pulse_open(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* format,
475 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
477 WINPR_ASSERT(format);
479 if (!rdpsnd_check_pulse(pulse, FALSE))
482 if (!rdpsnd_pulse_set_format_spec(pulse, format))
485 pulse->latency = latency;
487 return rdpsnd_pulse_open_stream(device);
490static void rdpsnd_pulse_free(rdpsndDevicePlugin* device)
492 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
497 rdpsnd_pulse_close(device);
500 pa_threaded_mainloop_stop(pulse->mainloop);
504 pa_context_disconnect(pulse->context);
505 pa_context_unref(pulse->context);
506 pulse->context =
nullptr;
511 pa_threaded_mainloop_free(pulse->mainloop);
512 pulse->mainloop =
nullptr;
515 free(pulse->device_name);
516 free(pulse->client_name);
517 free(pulse->stream_name);
521static BOOL rdpsnd_pulse_default_format(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* desired,
524 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
526 if (!pulse || !defaultFormat)
529 *defaultFormat = *desired;
530 defaultFormat->data =
nullptr;
531 defaultFormat->cbSize = 0;
532 defaultFormat->wFormatTag = WAVE_FORMAT_PCM;
533 if ((defaultFormat->nChannels < 1) || (defaultFormat->nChannels > PA_CHANNELS_MAX))
534 defaultFormat->nChannels = 2;
535 if ((defaultFormat->nSamplesPerSec < 1) || (defaultFormat->nSamplesPerSec > PA_RATE_MAX))
536 defaultFormat->nSamplesPerSec = 44100;
537 if ((defaultFormat->wBitsPerSample != 8) && (defaultFormat->wBitsPerSample != 16))
538 defaultFormat->wBitsPerSample = 16;
540 defaultFormat->nBlockAlign = defaultFormat->nChannels * defaultFormat->wBitsPerSample / 8;
541 defaultFormat->nAvgBytesPerSec = defaultFormat->nBlockAlign * defaultFormat->nSamplesPerSec;
545BOOL rdpsnd_pulse_format_supported(WINPR_ATTR_UNUSED rdpsndDevicePlugin* device,
548 WINPR_ASSERT(device);
549 WINPR_ASSERT(format);
551 switch (format->wFormatTag)
553 case WAVE_FORMAT_PCM:
554 if (format->cbSize == 0 && (format->nSamplesPerSec <= PA_RATE_MAX) &&
555 (format->wBitsPerSample == 8 || format->wBitsPerSample == 16) &&
556 (format->nChannels >= 1 && format->nChannels <= PA_CHANNELS_MAX))
570static UINT32 rdpsnd_pulse_get_volume(rdpsndDevicePlugin* device)
572 pa_operation* o =
nullptr;
573 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
575 if (!pulse || !pulse->mainloop)
578 pa_threaded_mainloop_lock(pulse->mainloop);
580 if (rdpsnd_check_pulse(pulse, FALSE))
582 o = pa_context_get_sink_info_by_index(pulse->context, 0, rdpsnd_pulse_get_sink_info, pulse);
584 pa_operation_unref(o);
586 pa_threaded_mainloop_unlock(pulse->mainloop);
587 return pulse->volume;
590static void rdpsnd_set_volume_success_cb(WINPR_ATTR_UNUSED pa_context* c,
int success,
593 rdpsndPulsePlugin* pulse = userdata;
595 if (!rdpsnd_check_pulse(pulse, TRUE))
599 WLog_INFO(TAG,
"%d", success);
602static BOOL rdpsnd_pulse_set_volume(rdpsndDevicePlugin* device, UINT32 value)
604 pa_cvolume cv = WINPR_C_ARRAY_INIT;
605 pa_volume_t left = 0;
606 pa_volume_t right = 0;
607 pa_operation* operation =
nullptr;
608 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
610 if (!pulse || !pulse->mainloop)
612 WLog_WARN(TAG,
"called before pulse backend was initialized");
616 left = (pa_volume_t)(value & 0xFFFF);
617 right = (pa_volume_t)((value >> 16) & 0xFFFF);
618 pa_cvolume_init(&cv);
620 cv.values[0] = PA_VOLUME_MUTED + (left * (PA_VOLUME_NORM - PA_VOLUME_MUTED)) / PA_VOLUME_NORM;
621 cv.values[1] = PA_VOLUME_MUTED + (right * (PA_VOLUME_NORM - PA_VOLUME_MUTED)) / PA_VOLUME_NORM;
622 pa_threaded_mainloop_lock(pulse->mainloop);
626 if (!rdpsnd_check_pulse(pulse, TRUE))
628 pa_threaded_mainloop_unlock(pulse->mainloop);
629 WLog_WARN(TAG,
"no pulse stream, not setting volume");
633 operation = pa_context_set_sink_input_volume(pulse->context, pa_stream_get_index(pulse->stream),
634 &cv, rdpsnd_set_volume_success_cb, pulse);
637 pa_operation_unref(operation);
639 pa_threaded_mainloop_unlock(pulse->mainloop);
644static BOOL rdpsnd_pulse_write(rdpsndPulsePlugin* pulse,
const BYTE* data,
size_t size)
648 size_t length = size;
649 void* pa_data =
nullptr;
651 if (pa_stream_begin_write(pulse->stream, &pa_data, &length) < 0)
659 if (!pa_data || (length == 0))
662 pa_stream_cancel_write(pulse->stream);
663 WLog_DBG(TAG,
"dropping %" PRIuz
" bytes, no buffer space", size);
668 memcpy(pa_data, data, length);
670 memset(pa_data, (pulse->sample_spec.format == PA_SAMPLE_U8) ? 0x80 : 0, length);
672 if (pa_stream_write(pulse->stream, pa_data, length,
nullptr, 0LL, PA_SEEK_RELATIVE) < 0)
682static UINT rdpsnd_pulse_play(rdpsndDevicePlugin* device,
const BYTE* data,
size_t size)
684 pa_usec_t latency = 0;
686 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
691 pa_threaded_mainloop_lock(pulse->mainloop);
693 if (!rdpsnd_check_pulse(pulse, TRUE))
695 pa_threaded_mainloop_unlock(pulse->mainloop);
697 WLog_DBG(TAG,
"reconnecting playback stream");
698 rdpsnd_pulse_open_stream(device);
707 const UINT64 now = GetTickCount64();
708 if (now >= pulse->play_end)
710 const UINT32 ms = (pulse->latency > 0) ? pulse->latency : PULSE_DEFAULT_LATENCY_MS;
711 (void)rdpsnd_pulse_write(pulse,
nullptr,
712 pa_usec_to_bytes(1000ULL * ms, &pulse->sample_spec));
713 pulse->play_end = now + ms;
716 if (rdpsnd_pulse_write(pulse, data, size))
717 pulse->play_end += pa_bytes_to_usec(size, &pulse->sample_spec) / 1000;
719 if (pa_stream_get_latency(pulse->stream, &latency, &negative) != 0)
722 pa_threaded_mainloop_unlock(pulse->mainloop);
724 const pa_usec_t val = latency / 1000;
725 if (val > UINT32_MAX)
730static UINT rdpsnd_pulse_parse_addin_args(rdpsndPulsePlugin* pulse,
const ADDIN_ARGV* args)
733 {
"dev", COMMAND_LINE_VALUE_REQUIRED,
"<device>",
nullptr,
nullptr, -1,
nullptr,
"device" },
734 {
"reconnect_delay_seconds", COMMAND_LINE_VALUE_REQUIRED,
"<reconnect_delay_seconds>",
735 nullptr,
nullptr, -1,
nullptr,
"reconnect_delay_seconds" },
736 {
"client_name", COMMAND_LINE_VALUE_REQUIRED,
"<client_name>",
nullptr,
nullptr, -1,
737 nullptr,
"name of pulse client" },
738 {
"stream_name", COMMAND_LINE_VALUE_REQUIRED,
"<stream_name>",
nullptr,
nullptr, -1,
739 nullptr,
"name of pulse stream" },
740 {
nullptr, 0,
nullptr,
nullptr,
nullptr, -1,
nullptr,
nullptr }
743 COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
748 const int status = CommandLineParseArgumentsA(args->argc, args->argv, rdpsnd_pulse_args, flags,
749 pulse,
nullptr,
nullptr);
752 return ERROR_INVALID_DATA;
756 const char* client_name =
nullptr;
757 const char* stream_name =
nullptr;
760 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
763 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg,
"dev")
765 pulse->device_name = _strdup(arg->Value);
767 if (!pulse->device_name)
768 return ERROR_OUTOFMEMORY;
770 CommandLineSwitchCase(arg,
"reconnect_delay_seconds")
772 unsigned long val = strtoul(arg->Value,
nullptr, 0);
774 if ((errno != 0) || (val > INT32_MAX))
775 return ERROR_INVALID_DATA;
777 pulse->reconnect_delay_seconds = (time_t)val;
779 CommandLineSwitchCase(arg,
"client_name")
781 client_name = arg->Value;
783 CommandLineSwitchCase(arg,
"stream_name")
785 stream_name = arg->Value;
787 CommandLineSwitchEnd(arg)
788 }
while ((arg = CommandLineFindNextArgumentA(arg)) !=
nullptr);
791 client_name = freerdp_getApplicationDetailsString();
793 stream_name = freerdp_getApplicationDetailsString();
795 pulse->client_name = _strdup(client_name);
796 pulse->stream_name = _strdup(stream_name);
797 if (!pulse->client_name || !pulse->stream_name)
798 return ERROR_OUTOFMEMORY;
799 return CHANNEL_RC_OK;
802FREERDP_ENTRY_POINT(UINT VCAPITYPE pulse_freerdp_rdpsnd_client_subsystem_entry(
805 WINPR_ASSERT(pEntryPoints);
807 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)calloc(1,
sizeof(rdpsndPulsePlugin));
810 return CHANNEL_RC_NO_MEMORY;
812 pulse->device.Open = rdpsnd_pulse_open;
813 pulse->device.FormatSupported = rdpsnd_pulse_format_supported;
814 pulse->device.GetVolume = rdpsnd_pulse_get_volume;
815 pulse->device.SetVolume = rdpsnd_pulse_set_volume;
816 pulse->device.Play = rdpsnd_pulse_play;
817 pulse->device.Close = rdpsnd_pulse_close;
818 pulse->device.Free = rdpsnd_pulse_free;
819 pulse->device.DefaultFormat = rdpsnd_pulse_default_format;
822 UINT ret = rdpsnd_pulse_parse_addin_args(pulse, args);
824 if (ret != CHANNEL_RC_OK)
826 WLog_ERR(TAG,
"error parsing arguments");
830 pulse->reconnect_delay_seconds = 5;
831 pulse->reconnect_time = time(
nullptr);
833 ret = CHANNEL_RC_NO_MEMORY;
834 pulse->mainloop = pa_threaded_mainloop_new();
836 if (!pulse->mainloop)
839 pa_threaded_mainloop_lock(pulse->mainloop);
841 if (pa_threaded_mainloop_start(pulse->mainloop) < 0)
843 pa_threaded_mainloop_unlock(pulse->mainloop);
847 pa_threaded_mainloop_unlock(pulse->mainloop);
849 if (!rdpsnd_pulse_context_connect((rdpsndDevicePlugin*)pulse))
852 pEntryPoints->pRegisterRdpsndDevice(pEntryPoints->rdpsnd, (rdpsndDevicePlugin*)pulse);
853 return CHANNEL_RC_OK;
855 rdpsnd_pulse_free((rdpsndDevicePlugin*)pulse);