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;
49 pa_threaded_mainloop* mainloop;
51 pa_sample_spec sample_spec;
55 time_t reconnect_delay_seconds;
56 time_t reconnect_time;
59static BOOL rdpsnd_check_pulse(rdpsndPulsePlugin* pulse, BOOL haveStream)
66 WLog_WARN(TAG,
"pulse->context=%p", pulse->context);
74 WLog_WARN(TAG,
"pulse->stream=%p", pulse->stream);
81 WLog_WARN(TAG,
"pulse->mainloop=%p", pulse->mainloop);
88static BOOL rdpsnd_pulse_format_supported(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* format);
90static void rdpsnd_pulse_get_sink_info(pa_context* c,
const pa_sink_info* i,
91 WINPR_ATTR_UNUSED
int eol,
void* userdata)
93 UINT16 dwVolumeLeft = ((50 * 0xFFFF) / 100);
94 UINT16 dwVolumeRight = ((50 * 0xFFFF) / 100);
95 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
98 if (!rdpsnd_check_pulse(pulse, FALSE) || !i)
101 for (uint8_t x = 0; x < i->volume.channels; x++)
103 pa_volume_t volume = i->volume.values[x];
105 if (volume >= PA_VOLUME_NORM)
106 volume = PA_VOLUME_NORM - 1;
111 dwVolumeLeft = (UINT16)volume;
115 dwVolumeRight = (UINT16)volume;
123 pulse->volume = ((UINT32)dwVolumeLeft << 16U) | dwVolumeRight;
126static void rdpsnd_pulse_context_state_callback(pa_context* context,
void* userdata)
128 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
130 WINPR_ASSERT(context);
133 pa_context_state_t state = pa_context_get_state(context);
137 case PA_CONTEXT_READY:
138 pa_threaded_mainloop_signal(pulse->mainloop, 0);
141 case PA_CONTEXT_FAILED:
143 pa_context_unref(pulse->context);
144 pulse->context = NULL;
145 if (pulse->reconnect_delay_seconds >= 0)
146 pulse->reconnect_time = time(NULL) + pulse->reconnect_delay_seconds;
147 pa_threaded_mainloop_signal(pulse->mainloop, 0);
150 case PA_CONTEXT_TERMINATED:
151 pa_threaded_mainloop_signal(pulse->mainloop, 0);
159static BOOL rdpsnd_pulse_connect(rdpsndDevicePlugin* device)
162 pa_operation* o = NULL;
163 pa_context_state_t state = PA_CONTEXT_FAILED;
164 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
166 if (!rdpsnd_check_pulse(pulse, FALSE))
169 pa_threaded_mainloop_lock(pulse->mainloop);
171 if (pa_context_connect(pulse->context, NULL, 0, NULL) < 0)
173 pa_threaded_mainloop_unlock(pulse->mainloop);
179 state = pa_context_get_state(pulse->context);
181 if (state == PA_CONTEXT_READY)
184 if (!PA_CONTEXT_IS_GOOD(state))
189 pa_threaded_mainloop_wait(pulse->mainloop);
192 o = pa_context_get_sink_info_by_index(pulse->context, 0, rdpsnd_pulse_get_sink_info, pulse);
195 pa_operation_unref(o);
197 if (state == PA_CONTEXT_READY)
203 pa_context_disconnect(pulse->context);
207 pa_threaded_mainloop_unlock(pulse->mainloop);
211static void rdpsnd_pulse_stream_success_callback(WINPR_ATTR_UNUSED pa_stream* stream,
212 WINPR_ATTR_UNUSED
int success,
void* userdata)
214 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
216 if (!rdpsnd_check_pulse(pulse, TRUE))
219 pa_threaded_mainloop_signal(pulse->mainloop, 0);
222static void rdpsnd_pulse_wait_for_operation(rdpsndPulsePlugin* pulse, pa_operation* operation)
224 if (!rdpsnd_check_pulse(pulse, TRUE))
230 while (pa_operation_get_state(operation) == PA_OPERATION_RUNNING)
232 pa_threaded_mainloop_wait(pulse->mainloop);
235 pa_operation_unref(operation);
238static void rdpsnd_pulse_stream_state_callback(pa_stream* stream,
void* userdata)
240 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
242 WINPR_ASSERT(stream);
243 if (!rdpsnd_check_pulse(pulse, TRUE))
246 pa_stream_state_t state = pa_stream_get_state(stream);
250 case PA_STREAM_READY:
251 pa_threaded_mainloop_signal(pulse->mainloop, 0);
254 case PA_STREAM_FAILED:
255 case PA_STREAM_TERMINATED:
257 pulse->stream = NULL;
258 pa_threaded_mainloop_signal(pulse->mainloop, 0);
266static void rdpsnd_pulse_stream_request_callback(pa_stream* stream, WINPR_ATTR_UNUSED
size_t length,
269 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
271 WINPR_ASSERT(stream);
272 if (!rdpsnd_check_pulse(pulse, TRUE))
275 pa_threaded_mainloop_signal(pulse->mainloop, 0);
278static void rdpsnd_pulse_close(rdpsndDevicePlugin* device)
280 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
284 if (!rdpsnd_check_pulse(pulse, FALSE))
287 pa_threaded_mainloop_lock(pulse->mainloop);
290 rdpsnd_pulse_wait_for_operation(
291 pulse, pa_stream_drain(pulse->stream, rdpsnd_pulse_stream_success_callback, pulse));
292 pa_stream_disconnect(pulse->stream);
293 pa_stream_unref(pulse->stream);
294 pulse->stream = NULL;
296 pa_threaded_mainloop_unlock(pulse->mainloop);
299static BOOL rdpsnd_pulse_set_format_spec(rdpsndPulsePlugin* pulse,
const AUDIO_FORMAT* format)
301 pa_sample_spec sample_spec = { 0 };
303 WINPR_ASSERT(format);
305 if (!rdpsnd_check_pulse(pulse, FALSE))
308 if (!rdpsnd_pulse_format_supported(&pulse->device, format))
311 sample_spec.rate = format->nSamplesPerSec;
312 sample_spec.channels = WINPR_ASSERTING_INT_CAST(uint8_t, format->nChannels);
314 switch (format->wFormatTag)
316 case WAVE_FORMAT_PCM:
317 switch (format->wBitsPerSample)
320 sample_spec.format = PA_SAMPLE_U8;
324 sample_spec.format = PA_SAMPLE_S16LE;
337 pulse->sample_spec = sample_spec;
341static BOOL rdpsnd_pulse_context_connect(rdpsndDevicePlugin* device)
343 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
345 pulse->context = pa_context_new(pa_threaded_mainloop_get_api(pulse->mainloop),
"freerdp");
350 pa_context_set_state_callback(pulse->context, rdpsnd_pulse_context_state_callback, pulse);
352 if (!rdpsnd_pulse_connect((rdpsndDevicePlugin*)pulse))
358static BOOL rdpsnd_pulse_open_stream(rdpsndDevicePlugin* device)
360 pa_stream_state_t state = PA_STREAM_FAILED;
361 int flags = PA_STREAM_NOFLAGS;
362 pa_buffer_attr buffer_attr = { 0 };
363 char ss[PA_SAMPLE_SPEC_SNPRINT_MAX] = { 0 };
364 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
366 if (pa_sample_spec_valid(&pulse->sample_spec) == 0)
368 pa_sample_spec_snprint(ss,
sizeof(ss), &pulse->sample_spec);
372 pa_threaded_mainloop_lock(pulse->mainloop);
375 pa_threaded_mainloop_unlock(pulse->mainloop);
376 if (pulse->reconnect_delay_seconds >= 0 && time(NULL) - pulse->reconnect_time >= 0)
377 rdpsnd_pulse_context_connect(device);
378 pa_threaded_mainloop_lock(pulse->mainloop);
381 if (!rdpsnd_check_pulse(pulse, FALSE))
383 pa_threaded_mainloop_unlock(pulse->mainloop);
387 pulse->stream = pa_stream_new(pulse->context,
"freerdp", &pulse->sample_spec, NULL);
391 pa_threaded_mainloop_unlock(pulse->mainloop);
396 pa_stream_set_state_callback(pulse->stream, rdpsnd_pulse_stream_state_callback, pulse);
397 pa_stream_set_write_callback(pulse->stream, rdpsnd_pulse_stream_request_callback, pulse);
398 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE;
400 if (pulse->latency > 0)
402 const size_t val = pa_usec_to_bytes(1000ULL * pulse->latency, &pulse->sample_spec);
403 buffer_attr.maxlength = UINT32_MAX;
404 buffer_attr.tlength = (val > UINT32_MAX) ? UINT32_MAX : (UINT32)val;
405 buffer_attr.prebuf = UINT32_MAX;
406 buffer_attr.minreq = UINT32_MAX;
407 buffer_attr.fragsize = UINT32_MAX;
408 flags |= PA_STREAM_ADJUST_LATENCY;
412 pa_stream_flags_t eflags = (pa_stream_flags_t)flags;
413 if (pa_stream_connect_playback(pulse->stream, pulse->device_name,
414 pulse->latency > 0 ? &buffer_attr : NULL, eflags, NULL,
417 WLog_ERR(TAG,
"error connecting playback stream");
418 pa_stream_unref(pulse->stream);
419 pulse->stream = NULL;
420 pa_threaded_mainloop_unlock(pulse->mainloop);
424 while (pulse->stream)
426 state = pa_stream_get_state(pulse->stream);
428 if (state == PA_STREAM_READY)
431 if (!PA_STREAM_IS_GOOD(state))
436 pa_threaded_mainloop_wait(pulse->mainloop);
439 pa_threaded_mainloop_unlock(pulse->mainloop);
441 if (state == PA_STREAM_READY)
444 rdpsnd_pulse_close(device);
448static BOOL rdpsnd_pulse_open(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* format,
451 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
453 WINPR_ASSERT(format);
455 if (!rdpsnd_check_pulse(pulse, FALSE))
458 if (!rdpsnd_pulse_set_format_spec(pulse, format))
461 pulse->latency = latency;
463 return rdpsnd_pulse_open_stream(device);
466static void rdpsnd_pulse_free(rdpsndDevicePlugin* device)
468 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
473 rdpsnd_pulse_close(device);
476 pa_threaded_mainloop_stop(pulse->mainloop);
480 pa_context_disconnect(pulse->context);
481 pa_context_unref(pulse->context);
482 pulse->context = NULL;
487 pa_threaded_mainloop_free(pulse->mainloop);
488 pulse->mainloop = NULL;
491 free(pulse->device_name);
495static BOOL rdpsnd_pulse_default_format(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* desired,
498 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
500 if (!pulse || !defaultFormat)
503 *defaultFormat = *desired;
504 defaultFormat->data = NULL;
505 defaultFormat->cbSize = 0;
506 defaultFormat->wFormatTag = WAVE_FORMAT_PCM;
507 if ((defaultFormat->nChannels < 1) || (defaultFormat->nChannels > PA_CHANNELS_MAX))
508 defaultFormat->nChannels = 2;
509 if ((defaultFormat->nSamplesPerSec < 1) || (defaultFormat->nSamplesPerSec > PA_RATE_MAX))
510 defaultFormat->nSamplesPerSec = 44100;
511 if ((defaultFormat->wBitsPerSample != 8) && (defaultFormat->wBitsPerSample != 16))
512 defaultFormat->wBitsPerSample = 16;
514 defaultFormat->nBlockAlign = defaultFormat->nChannels * defaultFormat->wBitsPerSample / 8;
515 defaultFormat->nAvgBytesPerSec = defaultFormat->nBlockAlign * defaultFormat->nSamplesPerSec;
519BOOL rdpsnd_pulse_format_supported(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* format)
521 WINPR_ASSERT(device);
522 WINPR_ASSERT(format);
524 switch (format->wFormatTag)
526 case WAVE_FORMAT_PCM:
527 if (format->cbSize == 0 && (format->nSamplesPerSec <= PA_RATE_MAX) &&
528 (format->wBitsPerSample == 8 || format->wBitsPerSample == 16) &&
529 (format->nChannels >= 1 && format->nChannels <= PA_CHANNELS_MAX))
543static UINT32 rdpsnd_pulse_get_volume(rdpsndDevicePlugin* device)
545 pa_operation* o = NULL;
546 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
548 if (!rdpsnd_check_pulse(pulse, FALSE))
551 pa_threaded_mainloop_lock(pulse->mainloop);
552 o = pa_context_get_sink_info_by_index(pulse->context, 0, rdpsnd_pulse_get_sink_info, pulse);
554 pa_operation_unref(o);
555 pa_threaded_mainloop_unlock(pulse->mainloop);
556 return pulse->volume;
559static void rdpsnd_set_volume_success_cb(pa_context* c,
int success,
void* userdata)
561 rdpsndPulsePlugin* pulse = userdata;
563 if (!rdpsnd_check_pulse(pulse, TRUE))
567 WLog_INFO(TAG,
"%d", success);
570static BOOL rdpsnd_pulse_set_volume(rdpsndDevicePlugin* device, UINT32 value)
572 pa_cvolume cv = { 0 };
573 pa_volume_t left = 0;
574 pa_volume_t right = 0;
575 pa_operation* operation = NULL;
576 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
578 if (!rdpsnd_check_pulse(pulse, TRUE))
580 WLog_WARN(TAG,
"%s called before pulse backend was initialized");
584 left = (pa_volume_t)(value & 0xFFFF);
585 right = (pa_volume_t)((value >> 16) & 0xFFFF);
586 pa_cvolume_init(&cv);
588 cv.values[0] = PA_VOLUME_MUTED + (left * (PA_VOLUME_NORM - PA_VOLUME_MUTED)) / PA_VOLUME_NORM;
589 cv.values[1] = PA_VOLUME_MUTED + (right * (PA_VOLUME_NORM - PA_VOLUME_MUTED)) / PA_VOLUME_NORM;
590 pa_threaded_mainloop_lock(pulse->mainloop);
591 operation = pa_context_set_sink_input_volume(pulse->context, pa_stream_get_index(pulse->stream),
592 &cv, rdpsnd_set_volume_success_cb, pulse);
595 pa_operation_unref(operation);
597 pa_threaded_mainloop_unlock(pulse->mainloop);
601static UINT rdpsnd_pulse_play(rdpsndDevicePlugin* device,
const BYTE* data,
size_t size)
604 void* pa_data = NULL;
606 pa_usec_t latency = 0;
608 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
613 pa_threaded_mainloop_lock(pulse->mainloop);
615 if (!rdpsnd_check_pulse(pulse, TRUE))
617 pa_threaded_mainloop_unlock(pulse->mainloop);
619 WLog_DBG(TAG,
"reconnecting playback stream");
620 rdpsnd_pulse_open_stream(device);
628 status = pa_stream_begin_write(pulse->stream, &pa_data, &length);
633 memcpy(pa_data, data, length);
635 status = pa_stream_write(pulse->stream, pa_data, length, NULL, 0LL, PA_SEEK_RELATIVE);
646 if (pa_stream_get_latency(pulse->stream, &latency, &negative) != 0)
649 pa_threaded_mainloop_unlock(pulse->mainloop);
651 const pa_usec_t val = latency / 1000;
652 if (val > UINT32_MAX)
657static UINT rdpsnd_pulse_parse_addin_args(rdpsndDevicePlugin* device,
const ADDIN_ARGV* args)
662 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
664 {
"dev", COMMAND_LINE_VALUE_REQUIRED,
"<device>", NULL, NULL, -1, NULL,
"device" },
665 {
"reconnect_delay_seconds", COMMAND_LINE_VALUE_REQUIRED,
"<reconnect_delay_seconds>", NULL,
666 NULL, -1, NULL,
"reconnect_delay_seconds" },
667 { NULL, 0, NULL, NULL, NULL, -1, NULL, NULL }
670 COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
675 status = CommandLineParseArgumentsA(args->argc, args->argv, rdpsnd_pulse_args, flags, pulse,
679 return ERROR_INVALID_DATA;
681 arg = rdpsnd_pulse_args;
685 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
688 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg,
"dev")
690 pulse->device_name = _strdup(arg->Value);
692 if (!pulse->device_name)
693 return ERROR_OUTOFMEMORY;
695 CommandLineSwitchCase(arg,
"reconnect_delay_seconds")
697 unsigned long val = strtoul(arg->Value, NULL, 0);
699 if ((errno != 0) || (val > INT32_MAX))
700 return ERROR_INVALID_DATA;
702 pulse->reconnect_delay_seconds = (time_t)val;
704 CommandLineSwitchEnd(arg)
705 }
while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
707 return CHANNEL_RC_OK;
710FREERDP_ENTRY_POINT(UINT VCAPITYPE pulse_freerdp_rdpsnd_client_subsystem_entry(
714 rdpsndPulsePlugin* pulse = NULL;
717 WINPR_ASSERT(pEntryPoints);
719 pulse = (rdpsndPulsePlugin*)calloc(1,
sizeof(rdpsndPulsePlugin));
722 return CHANNEL_RC_NO_MEMORY;
724 pulse->device.Open = rdpsnd_pulse_open;
725 pulse->device.FormatSupported = rdpsnd_pulse_format_supported;
726 pulse->device.GetVolume = rdpsnd_pulse_get_volume;
727 pulse->device.SetVolume = rdpsnd_pulse_set_volume;
728 pulse->device.Play = rdpsnd_pulse_play;
729 pulse->device.Close = rdpsnd_pulse_close;
730 pulse->device.Free = rdpsnd_pulse_free;
731 pulse->device.DefaultFormat = rdpsnd_pulse_default_format;
732 args = pEntryPoints->args;
736 ret = rdpsnd_pulse_parse_addin_args(&pulse->device, args);
738 if (ret != CHANNEL_RC_OK)
740 WLog_ERR(TAG,
"error parsing arguments");
744 pulse->reconnect_delay_seconds = 5;
745 pulse->reconnect_time = time(NULL);
747 ret = CHANNEL_RC_NO_MEMORY;
748 pulse->mainloop = pa_threaded_mainloop_new();
750 if (!pulse->mainloop)
753 pa_threaded_mainloop_lock(pulse->mainloop);
755 if (pa_threaded_mainloop_start(pulse->mainloop) < 0)
757 pa_threaded_mainloop_unlock(pulse->mainloop);
761 pa_threaded_mainloop_unlock(pulse->mainloop);
763 if (!rdpsnd_pulse_context_connect((rdpsndDevicePlugin*)pulse))
766 pEntryPoints->pRegisterRdpsndDevice(pEntryPoints->rdpsnd, (rdpsndDevicePlugin*)pulse);
767 return CHANNEL_RC_OK;
769 rdpsnd_pulse_free((rdpsndDevicePlugin*)pulse);