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(WINPR_ATTR_UNUSED rdpsndDevicePlugin* device,
542 WINPR_ASSERT(device);
543 WINPR_ASSERT(format);
545 switch (format->wFormatTag)
547 case WAVE_FORMAT_PCM:
548 if (format->cbSize == 0 && (format->nSamplesPerSec <= PA_RATE_MAX) &&
549 (format->wBitsPerSample == 8 || format->wBitsPerSample == 16) &&
550 (format->nChannels >= 1 && format->nChannels <= PA_CHANNELS_MAX))
564static UINT32 rdpsnd_pulse_get_volume(rdpsndDevicePlugin* device)
566 pa_operation* o =
nullptr;
567 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
569 if (!pulse || !pulse->mainloop)
572 pa_threaded_mainloop_lock(pulse->mainloop);
574 if (rdpsnd_check_pulse(pulse, FALSE))
576 o = pa_context_get_sink_info_by_index(pulse->context, 0, rdpsnd_pulse_get_sink_info, pulse);
578 pa_operation_unref(o);
580 pa_threaded_mainloop_unlock(pulse->mainloop);
581 return pulse->volume;
584static void rdpsnd_set_volume_success_cb(WINPR_ATTR_UNUSED pa_context* c,
int success,
587 rdpsndPulsePlugin* pulse = userdata;
589 if (!rdpsnd_check_pulse(pulse, TRUE))
593 WLog_INFO(TAG,
"%d", success);
596static BOOL rdpsnd_pulse_set_volume(rdpsndDevicePlugin* device, UINT32 value)
598 pa_cvolume cv = WINPR_C_ARRAY_INIT;
599 pa_volume_t left = 0;
600 pa_volume_t right = 0;
601 pa_operation* operation =
nullptr;
602 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
604 if (!pulse || !pulse->mainloop)
606 WLog_WARN(TAG,
"called before pulse backend was initialized");
610 left = (pa_volume_t)(value & 0xFFFF);
611 right = (pa_volume_t)((value >> 16) & 0xFFFF);
612 pa_cvolume_init(&cv);
614 cv.values[0] = PA_VOLUME_MUTED + (left * (PA_VOLUME_NORM - PA_VOLUME_MUTED)) / PA_VOLUME_NORM;
615 cv.values[1] = PA_VOLUME_MUTED + (right * (PA_VOLUME_NORM - PA_VOLUME_MUTED)) / PA_VOLUME_NORM;
616 pa_threaded_mainloop_lock(pulse->mainloop);
620 if (!rdpsnd_check_pulse(pulse, TRUE))
622 pa_threaded_mainloop_unlock(pulse->mainloop);
623 WLog_WARN(TAG,
"no pulse stream, not setting volume");
627 operation = pa_context_set_sink_input_volume(pulse->context, pa_stream_get_index(pulse->stream),
628 &cv, rdpsnd_set_volume_success_cb, pulse);
631 pa_operation_unref(operation);
633 pa_threaded_mainloop_unlock(pulse->mainloop);
637static UINT rdpsnd_pulse_play(rdpsndDevicePlugin* device,
const BYTE* data,
size_t size)
640 void* pa_data =
nullptr;
642 pa_usec_t latency = 0;
644 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
649 pa_threaded_mainloop_lock(pulse->mainloop);
651 if (!rdpsnd_check_pulse(pulse, TRUE))
653 pa_threaded_mainloop_unlock(pulse->mainloop);
655 WLog_DBG(TAG,
"reconnecting playback stream");
656 rdpsnd_pulse_open_stream(device);
664 status = pa_stream_begin_write(pulse->stream, &pa_data, &length);
674 if (!pa_data || (length == 0))
677 pa_stream_cancel_write(pulse->stream);
678 WLog_DBG(TAG,
"dropping %" PRIuz
" bytes, no buffer space", size);
682 memcpy(pa_data, data, length);
684 status = pa_stream_write(pulse->stream, pa_data, length,
nullptr, 0LL, PA_SEEK_RELATIVE);
695 if (pa_stream_get_latency(pulse->stream, &latency, &negative) != 0)
698 pa_threaded_mainloop_unlock(pulse->mainloop);
700 const pa_usec_t val = latency / 1000;
701 if (val > UINT32_MAX)
706static UINT rdpsnd_pulse_parse_addin_args(rdpsndPulsePlugin* pulse,
const ADDIN_ARGV* args)
709 {
"dev", COMMAND_LINE_VALUE_REQUIRED,
"<device>",
nullptr,
nullptr, -1,
nullptr,
"device" },
710 {
"reconnect_delay_seconds", COMMAND_LINE_VALUE_REQUIRED,
"<reconnect_delay_seconds>",
711 nullptr,
nullptr, -1,
nullptr,
"reconnect_delay_seconds" },
712 {
"client_name", COMMAND_LINE_VALUE_REQUIRED,
"<client_name>",
nullptr,
nullptr, -1,
713 nullptr,
"name of pulse client" },
714 {
"stream_name", COMMAND_LINE_VALUE_REQUIRED,
"<stream_name>",
nullptr,
nullptr, -1,
715 nullptr,
"name of pulse stream" },
716 {
nullptr, 0,
nullptr,
nullptr,
nullptr, -1,
nullptr,
nullptr }
719 COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
724 const int status = CommandLineParseArgumentsA(args->argc, args->argv, rdpsnd_pulse_args, flags,
725 pulse,
nullptr,
nullptr);
728 return ERROR_INVALID_DATA;
732 const char* client_name =
nullptr;
733 const char* stream_name =
nullptr;
736 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
739 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg,
"dev")
741 pulse->device_name = _strdup(arg->Value);
743 if (!pulse->device_name)
744 return ERROR_OUTOFMEMORY;
746 CommandLineSwitchCase(arg,
"reconnect_delay_seconds")
748 unsigned long val = strtoul(arg->Value,
nullptr, 0);
750 if ((errno != 0) || (val > INT32_MAX))
751 return ERROR_INVALID_DATA;
753 pulse->reconnect_delay_seconds = (time_t)val;
755 CommandLineSwitchCase(arg,
"client_name")
757 client_name = arg->Value;
759 CommandLineSwitchCase(arg,
"stream_name")
761 stream_name = arg->Value;
763 CommandLineSwitchEnd(arg)
764 }
while ((arg = CommandLineFindNextArgumentA(arg)) !=
nullptr);
767 client_name = freerdp_getApplicationDetailsString();
769 stream_name = freerdp_getApplicationDetailsString();
771 pulse->client_name = _strdup(client_name);
772 pulse->stream_name = _strdup(stream_name);
773 if (!pulse->client_name || !pulse->stream_name)
774 return ERROR_OUTOFMEMORY;
775 return CHANNEL_RC_OK;
778FREERDP_ENTRY_POINT(UINT VCAPITYPE pulse_freerdp_rdpsnd_client_subsystem_entry(
781 WINPR_ASSERT(pEntryPoints);
783 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)calloc(1,
sizeof(rdpsndPulsePlugin));
786 return CHANNEL_RC_NO_MEMORY;
788 pulse->device.Open = rdpsnd_pulse_open;
789 pulse->device.FormatSupported = rdpsnd_pulse_format_supported;
790 pulse->device.GetVolume = rdpsnd_pulse_get_volume;
791 pulse->device.SetVolume = rdpsnd_pulse_set_volume;
792 pulse->device.Play = rdpsnd_pulse_play;
793 pulse->device.Close = rdpsnd_pulse_close;
794 pulse->device.Free = rdpsnd_pulse_free;
795 pulse->device.DefaultFormat = rdpsnd_pulse_default_format;
798 UINT ret = rdpsnd_pulse_parse_addin_args(pulse, args);
800 if (ret != CHANNEL_RC_OK)
802 WLog_ERR(TAG,
"error parsing arguments");
806 pulse->reconnect_delay_seconds = 5;
807 pulse->reconnect_time = time(
nullptr);
809 ret = CHANNEL_RC_NO_MEMORY;
810 pulse->mainloop = pa_threaded_mainloop_new();
812 if (!pulse->mainloop)
815 pa_threaded_mainloop_lock(pulse->mainloop);
817 if (pa_threaded_mainloop_start(pulse->mainloop) < 0)
819 pa_threaded_mainloop_unlock(pulse->mainloop);
823 pa_threaded_mainloop_unlock(pulse->mainloop);
825 if (!rdpsnd_pulse_context_connect((rdpsndDevicePlugin*)pulse))
828 pEntryPoints->pRegisterRdpsndDevice(pEntryPoints->rdpsnd, (rdpsndDevicePlugin*)pulse);
829 return CHANNEL_RC_OK;
831 rdpsnd_pulse_free((rdpsndDevicePlugin*)pulse);