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=NULL");
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(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* format);
93static void rdpsnd_pulse_get_sink_info(pa_context* c,
const pa_sink_info* i,
94 WINPR_ATTR_UNUSED
int eol,
void* userdata)
96 UINT16 dwVolumeLeft = ((50 * 0xFFFF) / 100);
97 UINT16 dwVolumeRight = ((50 * 0xFFFF) / 100);
98 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
101 if (!rdpsnd_check_pulse(pulse, FALSE) || !i)
104 for (uint8_t x = 0; x < i->volume.channels; x++)
106 pa_volume_t volume = i->volume.values[x];
108 if (volume >= PA_VOLUME_NORM)
109 volume = PA_VOLUME_NORM - 1;
114 dwVolumeLeft = (UINT16)volume;
118 dwVolumeRight = (UINT16)volume;
126 pulse->volume = ((UINT32)dwVolumeLeft << 16U) | dwVolumeRight;
129static void rdpsnd_pulse_context_state_callback(pa_context* context,
void* userdata)
131 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
133 WINPR_ASSERT(context);
136 pa_context_state_t state = pa_context_get_state(context);
140 case PA_CONTEXT_READY:
141 pa_threaded_mainloop_signal(pulse->mainloop, 0);
144 case PA_CONTEXT_FAILED:
146 pa_context_unref(pulse->context);
147 pulse->context = NULL;
148 if (pulse->reconnect_delay_seconds >= 0)
149 pulse->reconnect_time = time(NULL) + pulse->reconnect_delay_seconds;
150 pa_threaded_mainloop_signal(pulse->mainloop, 0);
153 case PA_CONTEXT_TERMINATED:
154 pa_threaded_mainloop_signal(pulse->mainloop, 0);
162static BOOL rdpsnd_pulse_connect(rdpsndDevicePlugin* device)
165 pa_operation* o = NULL;
166 pa_context_state_t state = PA_CONTEXT_FAILED;
167 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
169 if (!rdpsnd_check_pulse(pulse, FALSE))
172 pa_threaded_mainloop_lock(pulse->mainloop);
174 if (pa_context_connect(pulse->context, NULL, PA_CONTEXT_NOFLAGS, NULL) < 0)
176 pa_threaded_mainloop_unlock(pulse->mainloop);
182 state = pa_context_get_state(pulse->context);
184 if (state == PA_CONTEXT_READY)
187 if (!PA_CONTEXT_IS_GOOD(state))
192 pa_threaded_mainloop_wait(pulse->mainloop);
195 o = pa_context_get_sink_info_by_index(pulse->context, 0, rdpsnd_pulse_get_sink_info, pulse);
198 pa_operation_unref(o);
200 if (state == PA_CONTEXT_READY)
206 pa_context_disconnect(pulse->context);
210 pa_threaded_mainloop_unlock(pulse->mainloop);
214static void rdpsnd_pulse_stream_success_callback(WINPR_ATTR_UNUSED pa_stream* stream,
215 WINPR_ATTR_UNUSED
int success,
void* userdata)
217 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
219 if (!rdpsnd_check_pulse(pulse, TRUE))
222 pa_threaded_mainloop_signal(pulse->mainloop, 0);
225static void rdpsnd_pulse_wait_for_operation(rdpsndPulsePlugin* pulse, pa_operation* operation)
227 if (!rdpsnd_check_pulse(pulse, TRUE))
233 while (pa_operation_get_state(operation) == PA_OPERATION_RUNNING)
235 pa_threaded_mainloop_wait(pulse->mainloop);
238 pa_operation_unref(operation);
241static void rdpsnd_pulse_stream_state_callback(pa_stream* stream,
void* userdata)
243 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
245 WINPR_ASSERT(stream);
246 if (!rdpsnd_check_pulse(pulse, TRUE))
249 pa_stream_state_t state = pa_stream_get_state(stream);
253 case PA_STREAM_READY:
254 pa_threaded_mainloop_signal(pulse->mainloop, 0);
257 case PA_STREAM_FAILED:
258 case PA_STREAM_TERMINATED:
260 pulse->stream = NULL;
261 pa_threaded_mainloop_signal(pulse->mainloop, 0);
269static void rdpsnd_pulse_stream_request_callback(pa_stream* stream, WINPR_ATTR_UNUSED
size_t length,
272 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
274 WINPR_ASSERT(stream);
275 if (!rdpsnd_check_pulse(pulse, TRUE))
278 pa_threaded_mainloop_signal(pulse->mainloop, 0);
281static void rdpsnd_pulse_close(rdpsndDevicePlugin* device)
283 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
287 if (!rdpsnd_check_pulse(pulse, FALSE))
290 pa_threaded_mainloop_lock(pulse->mainloop);
293 rdpsnd_pulse_wait_for_operation(
294 pulse, pa_stream_drain(pulse->stream, rdpsnd_pulse_stream_success_callback, pulse));
295 pa_stream_disconnect(pulse->stream);
296 pa_stream_unref(pulse->stream);
297 pulse->stream = NULL;
299 pa_threaded_mainloop_unlock(pulse->mainloop);
302static BOOL rdpsnd_pulse_set_format_spec(rdpsndPulsePlugin* pulse,
const AUDIO_FORMAT* format)
304 WINPR_ASSERT(format);
306 if (!rdpsnd_check_pulse(pulse, FALSE))
309 if (!rdpsnd_pulse_format_supported(&pulse->device, format))
312 pa_sample_format_t sformat = PA_SAMPLE_INVALID;
313 switch (format->wFormatTag)
315 case WAVE_FORMAT_PCM:
316 switch (format->wBitsPerSample)
319 sformat = PA_SAMPLE_U8;
323 sformat = PA_SAMPLE_S16LE;
336 const pa_sample_spec sample_spec = { .format = sformat,
337 .rate = format->nSamplesPerSec,
339 WINPR_ASSERTING_INT_CAST(uint8_t, format->nChannels) };
340 pulse->sample_spec = sample_spec;
344static BOOL rdpsnd_pulse_context_connect(rdpsndDevicePlugin* device)
346 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
350 pa_context_new(pa_threaded_mainloop_get_api(pulse->mainloop), pulse->client_name);
355 pa_context_set_state_callback(pulse->context, rdpsnd_pulse_context_state_callback, pulse);
357 if (!rdpsnd_pulse_connect((rdpsndDevicePlugin*)pulse))
363static BOOL rdpsnd_pulse_open_stream(rdpsndDevicePlugin* device)
365 pa_stream_state_t state = PA_STREAM_FAILED;
366 int flags = PA_STREAM_NOFLAGS;
367 pa_buffer_attr buffer_attr = { 0 };
368 char ss[PA_SAMPLE_SPEC_SNPRINT_MAX] = { 0 };
369 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
372 if (pa_sample_spec_valid(&pulse->sample_spec) == 0)
374 pa_sample_spec_snprint(ss,
sizeof(ss), &pulse->sample_spec);
378 pa_threaded_mainloop_lock(pulse->mainloop);
381 pa_threaded_mainloop_unlock(pulse->mainloop);
382 if (pulse->reconnect_delay_seconds >= 0 && time(NULL) - pulse->reconnect_time >= 0)
383 rdpsnd_pulse_context_connect(device);
384 pa_threaded_mainloop_lock(pulse->mainloop);
387 if (!rdpsnd_check_pulse(pulse, FALSE))
389 pa_threaded_mainloop_unlock(pulse->mainloop);
393 pulse->stream = pa_stream_new(pulse->context, pulse->stream_name, &pulse->sample_spec, NULL);
397 pa_threaded_mainloop_unlock(pulse->mainloop);
402 pa_stream_set_state_callback(pulse->stream, rdpsnd_pulse_stream_state_callback, pulse);
403 pa_stream_set_write_callback(pulse->stream, rdpsnd_pulse_stream_request_callback, pulse);
404 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE;
406 if (pulse->latency > 0)
408 const size_t val = pa_usec_to_bytes(1000ULL * pulse->latency, &pulse->sample_spec);
409 buffer_attr.maxlength = UINT32_MAX;
410 buffer_attr.tlength = (val > UINT32_MAX) ? UINT32_MAX : (UINT32)val;
411 buffer_attr.prebuf = UINT32_MAX;
412 buffer_attr.minreq = UINT32_MAX;
413 buffer_attr.fragsize = UINT32_MAX;
414 flags |= PA_STREAM_ADJUST_LATENCY;
418 pa_stream_flags_t eflags = (pa_stream_flags_t)flags;
419 if (pa_stream_connect_playback(pulse->stream, pulse->device_name,
420 pulse->latency > 0 ? &buffer_attr : NULL, eflags, NULL,
423 WLog_ERR(TAG,
"error connecting playback stream");
424 pa_stream_unref(pulse->stream);
425 pulse->stream = NULL;
426 pa_threaded_mainloop_unlock(pulse->mainloop);
430 while (pulse->stream)
432 state = pa_stream_get_state(pulse->stream);
434 if (state == PA_STREAM_READY)
437 if (!PA_STREAM_IS_GOOD(state))
442 pa_threaded_mainloop_wait(pulse->mainloop);
445 pa_threaded_mainloop_unlock(pulse->mainloop);
447 if (state == PA_STREAM_READY)
450 rdpsnd_pulse_close(device);
454static BOOL rdpsnd_pulse_open(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* format,
457 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
459 WINPR_ASSERT(format);
461 if (!rdpsnd_check_pulse(pulse, FALSE))
464 if (!rdpsnd_pulse_set_format_spec(pulse, format))
467 pulse->latency = latency;
469 return rdpsnd_pulse_open_stream(device);
472static void rdpsnd_pulse_free(rdpsndDevicePlugin* device)
474 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
479 rdpsnd_pulse_close(device);
482 pa_threaded_mainloop_stop(pulse->mainloop);
486 pa_context_disconnect(pulse->context);
487 pa_context_unref(pulse->context);
488 pulse->context = NULL;
493 pa_threaded_mainloop_free(pulse->mainloop);
494 pulse->mainloop = NULL;
497 free(pulse->device_name);
498 free(pulse->client_name);
499 free(pulse->stream_name);
503static BOOL rdpsnd_pulse_default_format(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* desired,
506 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
508 if (!pulse || !defaultFormat)
511 *defaultFormat = *desired;
512 defaultFormat->data = NULL;
513 defaultFormat->cbSize = 0;
514 defaultFormat->wFormatTag = WAVE_FORMAT_PCM;
515 if ((defaultFormat->nChannels < 1) || (defaultFormat->nChannels > PA_CHANNELS_MAX))
516 defaultFormat->nChannels = 2;
517 if ((defaultFormat->nSamplesPerSec < 1) || (defaultFormat->nSamplesPerSec > PA_RATE_MAX))
518 defaultFormat->nSamplesPerSec = 44100;
519 if ((defaultFormat->wBitsPerSample != 8) && (defaultFormat->wBitsPerSample != 16))
520 defaultFormat->wBitsPerSample = 16;
522 defaultFormat->nBlockAlign = defaultFormat->nChannels * defaultFormat->wBitsPerSample / 8;
523 defaultFormat->nAvgBytesPerSec = defaultFormat->nBlockAlign * defaultFormat->nSamplesPerSec;
527BOOL rdpsnd_pulse_format_supported(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* format)
529 WINPR_ASSERT(device);
530 WINPR_ASSERT(format);
532 switch (format->wFormatTag)
534 case WAVE_FORMAT_PCM:
535 if (format->cbSize == 0 && (format->nSamplesPerSec <= PA_RATE_MAX) &&
536 (format->wBitsPerSample == 8 || format->wBitsPerSample == 16) &&
537 (format->nChannels >= 1 && format->nChannels <= PA_CHANNELS_MAX))
551static UINT32 rdpsnd_pulse_get_volume(rdpsndDevicePlugin* device)
553 pa_operation* o = NULL;
554 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
556 if (!rdpsnd_check_pulse(pulse, FALSE))
559 pa_threaded_mainloop_lock(pulse->mainloop);
560 o = pa_context_get_sink_info_by_index(pulse->context, 0, rdpsnd_pulse_get_sink_info, pulse);
562 pa_operation_unref(o);
563 pa_threaded_mainloop_unlock(pulse->mainloop);
564 return pulse->volume;
567static void rdpsnd_set_volume_success_cb(pa_context* c,
int success,
void* userdata)
569 rdpsndPulsePlugin* pulse = userdata;
571 if (!rdpsnd_check_pulse(pulse, TRUE))
575 WLog_INFO(TAG,
"%d", success);
578static BOOL rdpsnd_pulse_set_volume(rdpsndDevicePlugin* device, UINT32 value)
580 pa_cvolume cv = { 0 };
581 pa_volume_t left = 0;
582 pa_volume_t right = 0;
583 pa_operation* operation = NULL;
584 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
586 if (!rdpsnd_check_pulse(pulse, TRUE))
588 WLog_WARN(TAG,
"called before pulse backend was initialized");
592 left = (pa_volume_t)(value & 0xFFFF);
593 right = (pa_volume_t)((value >> 16) & 0xFFFF);
594 pa_cvolume_init(&cv);
596 cv.values[0] = PA_VOLUME_MUTED + (left * (PA_VOLUME_NORM - PA_VOLUME_MUTED)) / PA_VOLUME_NORM;
597 cv.values[1] = PA_VOLUME_MUTED + (right * (PA_VOLUME_NORM - PA_VOLUME_MUTED)) / PA_VOLUME_NORM;
598 pa_threaded_mainloop_lock(pulse->mainloop);
599 operation = pa_context_set_sink_input_volume(pulse->context, pa_stream_get_index(pulse->stream),
600 &cv, rdpsnd_set_volume_success_cb, pulse);
603 pa_operation_unref(operation);
605 pa_threaded_mainloop_unlock(pulse->mainloop);
609static UINT rdpsnd_pulse_play(rdpsndDevicePlugin* device,
const BYTE* data,
size_t size)
612 void* pa_data = NULL;
614 pa_usec_t latency = 0;
616 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
621 pa_threaded_mainloop_lock(pulse->mainloop);
623 if (!rdpsnd_check_pulse(pulse, TRUE))
625 pa_threaded_mainloop_unlock(pulse->mainloop);
627 WLog_DBG(TAG,
"reconnecting playback stream");
628 rdpsnd_pulse_open_stream(device);
636 status = pa_stream_begin_write(pulse->stream, &pa_data, &length);
641 memcpy(pa_data, data, length);
643 status = pa_stream_write(pulse->stream, pa_data, length, NULL, 0LL, PA_SEEK_RELATIVE);
654 if (pa_stream_get_latency(pulse->stream, &latency, &negative) != 0)
657 pa_threaded_mainloop_unlock(pulse->mainloop);
659 const pa_usec_t val = latency / 1000;
660 if (val > UINT32_MAX)
665static UINT rdpsnd_pulse_parse_addin_args(rdpsndPulsePlugin* pulse,
const ADDIN_ARGV* args)
668 {
"dev", COMMAND_LINE_VALUE_REQUIRED,
"<device>", NULL, NULL, -1, NULL,
"device" },
669 {
"reconnect_delay_seconds", COMMAND_LINE_VALUE_REQUIRED,
"<reconnect_delay_seconds>", NULL,
670 NULL, -1, NULL,
"reconnect_delay_seconds" },
671 {
"client_name", COMMAND_LINE_VALUE_REQUIRED,
"<client_name>", NULL, NULL, -1, NULL,
672 "name of pulse client" },
673 {
"stream_name", COMMAND_LINE_VALUE_REQUIRED,
"<stream_name>", NULL, NULL, -1, NULL,
674 "name of pulse stream" },
675 { NULL, 0, NULL, NULL, NULL, -1, NULL, NULL }
678 COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
683 const int status = CommandLineParseArgumentsA(args->argc, args->argv, rdpsnd_pulse_args, flags,
687 return ERROR_INVALID_DATA;
691 const char* client_name = NULL;
692 const char* stream_name = NULL;
695 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
698 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg,
"dev")
700 pulse->device_name = _strdup(arg->Value);
702 if (!pulse->device_name)
703 return ERROR_OUTOFMEMORY;
705 CommandLineSwitchCase(arg,
"reconnect_delay_seconds")
707 unsigned long val = strtoul(arg->Value, NULL, 0);
709 if ((errno != 0) || (val > INT32_MAX))
710 return ERROR_INVALID_DATA;
712 pulse->reconnect_delay_seconds = (time_t)val;
714 CommandLineSwitchCase(arg,
"client_name")
716 client_name = arg->Value;
718 CommandLineSwitchCase(arg,
"stream_name")
720 stream_name = arg->Value;
722 CommandLineSwitchEnd(arg)
723 }
while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
726 client_name = freerdp_getApplicationDetailsString();
728 stream_name = freerdp_getApplicationDetailsString();
730 pulse->client_name = _strdup(client_name);
731 pulse->stream_name = _strdup(stream_name);
732 if (!pulse->client_name || !pulse->stream_name)
733 return ERROR_OUTOFMEMORY;
734 return CHANNEL_RC_OK;
737FREERDP_ENTRY_POINT(UINT VCAPITYPE pulse_freerdp_rdpsnd_client_subsystem_entry(
740 WINPR_ASSERT(pEntryPoints);
742 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)calloc(1,
sizeof(rdpsndPulsePlugin));
745 return CHANNEL_RC_NO_MEMORY;
747 pulse->device.Open = rdpsnd_pulse_open;
748 pulse->device.FormatSupported = rdpsnd_pulse_format_supported;
749 pulse->device.GetVolume = rdpsnd_pulse_get_volume;
750 pulse->device.SetVolume = rdpsnd_pulse_set_volume;
751 pulse->device.Play = rdpsnd_pulse_play;
752 pulse->device.Close = rdpsnd_pulse_close;
753 pulse->device.Free = rdpsnd_pulse_free;
754 pulse->device.DefaultFormat = rdpsnd_pulse_default_format;
757 UINT ret = rdpsnd_pulse_parse_addin_args(pulse, args);
759 if (ret != CHANNEL_RC_OK)
761 WLog_ERR(TAG,
"error parsing arguments");
765 pulse->reconnect_delay_seconds = 5;
766 pulse->reconnect_time = time(NULL);
768 ret = CHANNEL_RC_NO_MEMORY;
769 pulse->mainloop = pa_threaded_mainloop_new();
771 if (!pulse->mainloop)
774 pa_threaded_mainloop_lock(pulse->mainloop);
776 if (pa_threaded_mainloop_start(pulse->mainloop) < 0)
778 pa_threaded_mainloop_unlock(pulse->mainloop);
782 pa_threaded_mainloop_unlock(pulse->mainloop);
784 if (!rdpsnd_pulse_context_connect((rdpsndDevicePlugin*)pulse))
787 pEntryPoints->pRegisterRdpsndDevice(pEntryPoints->rdpsnd, (rdpsndDevicePlugin*)pulse);
788 return CHANNEL_RC_OK;
790 rdpsnd_pulse_free((rdpsndDevicePlugin*)pulse);