22 #include <freerdp/config.h>
31 #include <winpr/crt.h>
32 #include <winpr/assert.h>
33 #include <winpr/stream.h>
34 #include <winpr/cmdline.h>
36 #include <pulse/pulseaudio.h>
38 #include <freerdp/types.h>
39 #include <freerdp/codec/dsp.h>
41 #include "rdpsnd_main.h"
45 rdpsndDevicePlugin device;
48 pa_threaded_mainloop* mainloop;
50 pa_sample_spec sample_spec;
54 time_t reconnect_delay_seconds;
55 time_t reconnect_time;
58 static BOOL rdpsnd_check_pulse(rdpsndPulsePlugin* pulse, BOOL haveStream)
65 WLog_WARN(TAG,
"pulse->context=%p", pulse->context);
73 WLog_WARN(TAG,
"pulse->stream=%p", pulse->stream);
80 WLog_WARN(TAG,
"pulse->mainloop=%p", pulse->mainloop);
87 static BOOL rdpsnd_pulse_format_supported(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* format);
89 static void rdpsnd_pulse_get_sink_info(pa_context* c,
const pa_sink_info* i,
int eol,
92 UINT16 dwVolumeLeft = ((50 * 0xFFFF) / 100);
93 UINT16 dwVolumeRight = ((50 * 0xFFFF) / 100);
94 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
97 if (!rdpsnd_check_pulse(pulse, FALSE) || !i)
100 for (uint8_t x = 0; x < i->volume.channels; x++)
102 pa_volume_t volume = i->volume.values[x];
104 if (volume >= PA_VOLUME_NORM)
105 volume = PA_VOLUME_NORM - 1;
110 dwVolumeLeft = (UINT16)volume;
114 dwVolumeRight = (UINT16)volume;
122 pulse->volume = ((UINT32)dwVolumeLeft << 16U) | dwVolumeRight;
125 static void rdpsnd_pulse_context_state_callback(pa_context* context,
void* userdata)
127 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
129 WINPR_ASSERT(context);
132 pa_context_state_t state = pa_context_get_state(context);
136 case PA_CONTEXT_READY:
137 pa_threaded_mainloop_signal(pulse->mainloop, 0);
140 case PA_CONTEXT_FAILED:
142 pa_context_unref(pulse->context);
143 pulse->context = NULL;
144 if (pulse->reconnect_delay_seconds >= 0)
145 pulse->reconnect_time = time(NULL) + pulse->reconnect_delay_seconds;
146 pa_threaded_mainloop_signal(pulse->mainloop, 0);
149 case PA_CONTEXT_TERMINATED:
150 pa_threaded_mainloop_signal(pulse->mainloop, 0);
158 static BOOL rdpsnd_pulse_connect(rdpsndDevicePlugin* device)
161 pa_operation* o = NULL;
162 pa_context_state_t state = PA_CONTEXT_FAILED;
163 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
165 if (!rdpsnd_check_pulse(pulse, FALSE))
168 pa_threaded_mainloop_lock(pulse->mainloop);
170 if (pa_context_connect(pulse->context, NULL, 0, NULL) < 0)
172 pa_threaded_mainloop_unlock(pulse->mainloop);
178 state = pa_context_get_state(pulse->context);
180 if (state == PA_CONTEXT_READY)
183 if (!PA_CONTEXT_IS_GOOD(state))
188 pa_threaded_mainloop_wait(pulse->mainloop);
191 o = pa_context_get_sink_info_by_index(pulse->context, 0, rdpsnd_pulse_get_sink_info, pulse);
194 pa_operation_unref(o);
196 if (state == PA_CONTEXT_READY)
202 pa_context_disconnect(pulse->context);
206 pa_threaded_mainloop_unlock(pulse->mainloop);
210 static void rdpsnd_pulse_stream_success_callback(pa_stream* stream,
int success,
void* userdata)
212 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
214 if (!rdpsnd_check_pulse(pulse, TRUE))
217 pa_threaded_mainloop_signal(pulse->mainloop, 0);
220 static void rdpsnd_pulse_wait_for_operation(rdpsndPulsePlugin* pulse, pa_operation* operation)
222 if (!rdpsnd_check_pulse(pulse, TRUE))
228 while (pa_operation_get_state(operation) == PA_OPERATION_RUNNING)
230 pa_threaded_mainloop_wait(pulse->mainloop);
233 pa_operation_unref(operation);
236 static void rdpsnd_pulse_stream_state_callback(pa_stream* stream,
void* userdata)
238 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
240 WINPR_ASSERT(stream);
241 if (!rdpsnd_check_pulse(pulse, TRUE))
244 pa_stream_state_t state = pa_stream_get_state(stream);
248 case PA_STREAM_READY:
249 pa_threaded_mainloop_signal(pulse->mainloop, 0);
252 case PA_STREAM_FAILED:
253 case PA_STREAM_TERMINATED:
255 pulse->stream = NULL;
256 pa_threaded_mainloop_signal(pulse->mainloop, 0);
264 static void rdpsnd_pulse_stream_request_callback(pa_stream* stream,
size_t length,
void* userdata)
266 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
268 WINPR_ASSERT(stream);
269 if (!rdpsnd_check_pulse(pulse, TRUE))
272 pa_threaded_mainloop_signal(pulse->mainloop, 0);
275 static void rdpsnd_pulse_close(rdpsndDevicePlugin* device)
277 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
281 if (!rdpsnd_check_pulse(pulse, FALSE))
284 pa_threaded_mainloop_lock(pulse->mainloop);
287 rdpsnd_pulse_wait_for_operation(
288 pulse, pa_stream_drain(pulse->stream, rdpsnd_pulse_stream_success_callback, pulse));
289 pa_stream_disconnect(pulse->stream);
290 pa_stream_unref(pulse->stream);
291 pulse->stream = NULL;
293 pa_threaded_mainloop_unlock(pulse->mainloop);
296 static BOOL rdpsnd_pulse_set_format_spec(rdpsndPulsePlugin* pulse,
const AUDIO_FORMAT* format)
298 pa_sample_spec sample_spec = { 0 };
300 WINPR_ASSERT(format);
302 if (!rdpsnd_check_pulse(pulse, FALSE))
305 if (!rdpsnd_pulse_format_supported(&pulse->device, format))
308 sample_spec.rate = format->nSamplesPerSec;
309 sample_spec.channels = format->nChannels;
311 switch (format->wFormatTag)
313 case WAVE_FORMAT_PCM:
314 switch (format->wBitsPerSample)
317 sample_spec.format = PA_SAMPLE_U8;
321 sample_spec.format = PA_SAMPLE_S16LE;
334 pulse->sample_spec = sample_spec;
338 static BOOL rdpsnd_pulse_context_connect(rdpsndDevicePlugin* device)
340 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
342 pulse->context = pa_context_new(pa_threaded_mainloop_get_api(pulse->mainloop),
"freerdp");
347 pa_context_set_state_callback(pulse->context, rdpsnd_pulse_context_state_callback, pulse);
349 if (!rdpsnd_pulse_connect((rdpsndDevicePlugin*)pulse))
355 static BOOL rdpsnd_pulse_open_stream(rdpsndDevicePlugin* device)
357 pa_stream_state_t state = PA_STREAM_FAILED;
358 int flags = PA_STREAM_NOFLAGS;
359 pa_buffer_attr buffer_attr = { 0 };
360 char ss[PA_SAMPLE_SPEC_SNPRINT_MAX] = { 0 };
361 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
363 if (pa_sample_spec_valid(&pulse->sample_spec) == 0)
365 pa_sample_spec_snprint(ss,
sizeof(ss), &pulse->sample_spec);
369 pa_threaded_mainloop_lock(pulse->mainloop);
372 pa_threaded_mainloop_unlock(pulse->mainloop);
373 if (pulse->reconnect_delay_seconds >= 0 && time(NULL) - pulse->reconnect_time >= 0)
374 rdpsnd_pulse_context_connect(device);
375 pa_threaded_mainloop_lock(pulse->mainloop);
378 if (!rdpsnd_check_pulse(pulse, FALSE))
380 pa_threaded_mainloop_unlock(pulse->mainloop);
384 pulse->stream = pa_stream_new(pulse->context,
"freerdp", &pulse->sample_spec, NULL);
388 pa_threaded_mainloop_unlock(pulse->mainloop);
393 pa_stream_set_state_callback(pulse->stream, rdpsnd_pulse_stream_state_callback, pulse);
394 pa_stream_set_write_callback(pulse->stream, rdpsnd_pulse_stream_request_callback, pulse);
395 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE;
397 if (pulse->latency > 0)
399 const size_t val = pa_usec_to_bytes(1000ULL * pulse->latency, &pulse->sample_spec);
400 buffer_attr.maxlength = UINT32_MAX;
401 buffer_attr.tlength = (val > UINT32_MAX) ? UINT32_MAX : (UINT32)val;
402 buffer_attr.prebuf = UINT32_MAX;
403 buffer_attr.minreq = UINT32_MAX;
404 buffer_attr.fragsize = UINT32_MAX;
405 flags |= PA_STREAM_ADJUST_LATENCY;
409 pa_stream_flags_t eflags = (pa_stream_flags_t)flags;
410 if (pa_stream_connect_playback(pulse->stream, pulse->device_name,
411 pulse->latency > 0 ? &buffer_attr : NULL, eflags, NULL,
414 WLog_ERR(TAG,
"error connecting playback stream");
415 pa_stream_unref(pulse->stream);
416 pulse->stream = NULL;
417 pa_threaded_mainloop_unlock(pulse->mainloop);
421 while (pulse->stream)
423 state = pa_stream_get_state(pulse->stream);
425 if (state == PA_STREAM_READY)
428 if (!PA_STREAM_IS_GOOD(state))
433 pa_threaded_mainloop_wait(pulse->mainloop);
436 pa_threaded_mainloop_unlock(pulse->mainloop);
438 if (state == PA_STREAM_READY)
441 rdpsnd_pulse_close(device);
445 static BOOL rdpsnd_pulse_open(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* format,
448 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
450 WINPR_ASSERT(format);
452 if (!rdpsnd_check_pulse(pulse, FALSE))
455 if (!rdpsnd_pulse_set_format_spec(pulse, format))
458 pulse->latency = latency;
460 return rdpsnd_pulse_open_stream(device);
463 static void rdpsnd_pulse_free(rdpsndDevicePlugin* device)
465 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
470 rdpsnd_pulse_close(device);
473 pa_threaded_mainloop_stop(pulse->mainloop);
477 pa_context_disconnect(pulse->context);
478 pa_context_unref(pulse->context);
479 pulse->context = NULL;
484 pa_threaded_mainloop_free(pulse->mainloop);
485 pulse->mainloop = NULL;
488 free(pulse->device_name);
492 static BOOL rdpsnd_pulse_default_format(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* desired,
495 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
497 if (!pulse || !defaultFormat)
500 *defaultFormat = *desired;
501 defaultFormat->data = NULL;
502 defaultFormat->cbSize = 0;
503 defaultFormat->wFormatTag = WAVE_FORMAT_PCM;
504 if ((defaultFormat->nChannels < 1) || (defaultFormat->nChannels > PA_CHANNELS_MAX))
505 defaultFormat->nChannels = 2;
506 if ((defaultFormat->nSamplesPerSec < 1) || (defaultFormat->nSamplesPerSec > PA_RATE_MAX))
507 defaultFormat->nSamplesPerSec = 44100;
508 if ((defaultFormat->wBitsPerSample != 8) && (defaultFormat->wBitsPerSample != 16))
509 defaultFormat->wBitsPerSample = 16;
511 defaultFormat->nBlockAlign = defaultFormat->nChannels * defaultFormat->wBitsPerSample / 8;
512 defaultFormat->nAvgBytesPerSec = defaultFormat->nBlockAlign * defaultFormat->nSamplesPerSec;
516 BOOL rdpsnd_pulse_format_supported(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* format)
518 WINPR_ASSERT(device);
519 WINPR_ASSERT(format);
521 switch (format->wFormatTag)
523 case WAVE_FORMAT_PCM:
524 if (format->cbSize == 0 && (format->nSamplesPerSec <= PA_RATE_MAX) &&
525 (format->wBitsPerSample == 8 || format->wBitsPerSample == 16) &&
526 (format->nChannels >= 1 && format->nChannels <= PA_CHANNELS_MAX))
540 static UINT32 rdpsnd_pulse_get_volume(rdpsndDevicePlugin* device)
542 pa_operation* o = NULL;
543 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
545 if (!rdpsnd_check_pulse(pulse, FALSE))
548 pa_threaded_mainloop_lock(pulse->mainloop);
549 o = pa_context_get_sink_info_by_index(pulse->context, 0, rdpsnd_pulse_get_sink_info, pulse);
551 pa_operation_unref(o);
552 pa_threaded_mainloop_unlock(pulse->mainloop);
553 return pulse->volume;
556 static void rdpsnd_set_volume_success_cb(pa_context* c,
int success,
void* userdata)
558 rdpsndPulsePlugin* pulse = userdata;
560 if (!rdpsnd_check_pulse(pulse, TRUE))
564 WLog_INFO(TAG,
"%d", success);
567 static BOOL rdpsnd_pulse_set_volume(rdpsndDevicePlugin* device, UINT32 value)
569 pa_cvolume cv = { 0 };
570 pa_volume_t left = 0;
571 pa_volume_t right = 0;
572 pa_operation* operation = NULL;
573 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
575 if (!rdpsnd_check_pulse(pulse, TRUE))
577 WLog_WARN(TAG,
"%s called before pulse backend was initialized");
581 left = (pa_volume_t)(value & 0xFFFF);
582 right = (pa_volume_t)((value >> 16) & 0xFFFF);
583 pa_cvolume_init(&cv);
585 cv.values[0] = PA_VOLUME_MUTED + (left * (PA_VOLUME_NORM - PA_VOLUME_MUTED)) / PA_VOLUME_NORM;
586 cv.values[1] = PA_VOLUME_MUTED + (right * (PA_VOLUME_NORM - PA_VOLUME_MUTED)) / PA_VOLUME_NORM;
587 pa_threaded_mainloop_lock(pulse->mainloop);
588 operation = pa_context_set_sink_input_volume(pulse->context, pa_stream_get_index(pulse->stream),
589 &cv, rdpsnd_set_volume_success_cb, pulse);
592 pa_operation_unref(operation);
594 pa_threaded_mainloop_unlock(pulse->mainloop);
598 static UINT rdpsnd_pulse_play(rdpsndDevicePlugin* device,
const BYTE* data,
size_t size)
601 void* pa_data = NULL;
603 pa_usec_t latency = 0;
605 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
610 pa_threaded_mainloop_lock(pulse->mainloop);
612 if (!rdpsnd_check_pulse(pulse, TRUE))
614 pa_threaded_mainloop_unlock(pulse->mainloop);
616 WLog_DBG(TAG,
"reconnecting playback stream");
617 rdpsnd_pulse_open_stream(device);
625 status = pa_stream_begin_write(pulse->stream, &pa_data, &length);
630 memcpy(pa_data, data, length);
632 status = pa_stream_write(pulse->stream, pa_data, length, NULL, 0LL, PA_SEEK_RELATIVE);
643 if (pa_stream_get_latency(pulse->stream, &latency, &negative) != 0)
646 pa_threaded_mainloop_unlock(pulse->mainloop);
648 const pa_usec_t val = latency / 1000;
649 if (val > UINT32_MAX)
654 static UINT rdpsnd_pulse_parse_addin_args(rdpsndDevicePlugin* device,
const ADDIN_ARGV* args)
659 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
661 {
"dev", COMMAND_LINE_VALUE_REQUIRED,
"<device>", NULL, NULL, -1, NULL,
"device" },
662 {
"reconnect_delay_seconds", COMMAND_LINE_VALUE_REQUIRED,
"<reconnect_delay_seconds>", NULL,
663 NULL, -1, NULL,
"reconnect_delay_seconds" },
664 { NULL, 0, NULL, NULL, NULL, -1, NULL, NULL }
667 COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
672 status = CommandLineParseArgumentsA(args->argc, args->argv, rdpsnd_pulse_args, flags, pulse,
676 return ERROR_INVALID_DATA;
678 arg = rdpsnd_pulse_args;
682 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
685 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg,
"dev")
687 pulse->device_name = _strdup(arg->Value);
689 if (!pulse->device_name)
690 return ERROR_OUTOFMEMORY;
692 CommandLineSwitchCase(arg,
"reconnect_delay_seconds")
694 unsigned long val = strtoul(arg->Value, NULL, 0);
696 if ((errno != 0) || (val > INT32_MAX))
697 return ERROR_INVALID_DATA;
699 pulse->reconnect_delay_seconds = (time_t)val;
701 CommandLineSwitchEnd(arg)
702 }
while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
704 return CHANNEL_RC_OK;
707 FREERDP_ENTRY_POINT(UINT VCAPITYPE pulse_freerdp_rdpsnd_client_subsystem_entry(
711 rdpsndPulsePlugin* pulse = NULL;
714 WINPR_ASSERT(pEntryPoints);
716 pulse = (rdpsndPulsePlugin*)calloc(1,
sizeof(rdpsndPulsePlugin));
719 return CHANNEL_RC_NO_MEMORY;
721 pulse->device.Open = rdpsnd_pulse_open;
722 pulse->device.FormatSupported = rdpsnd_pulse_format_supported;
723 pulse->device.GetVolume = rdpsnd_pulse_get_volume;
724 pulse->device.SetVolume = rdpsnd_pulse_set_volume;
725 pulse->device.Play = rdpsnd_pulse_play;
726 pulse->device.Close = rdpsnd_pulse_close;
727 pulse->device.Free = rdpsnd_pulse_free;
728 pulse->device.DefaultFormat = rdpsnd_pulse_default_format;
729 args = pEntryPoints->args;
733 ret = rdpsnd_pulse_parse_addin_args(&pulse->device, args);
735 if (ret != CHANNEL_RC_OK)
737 WLog_ERR(TAG,
"error parsing arguments");
741 pulse->reconnect_delay_seconds = 5;
742 pulse->reconnect_time = time(NULL);
744 ret = CHANNEL_RC_NO_MEMORY;
745 pulse->mainloop = pa_threaded_mainloop_new();
747 if (!pulse->mainloop)
750 pa_threaded_mainloop_lock(pulse->mainloop);
752 if (pa_threaded_mainloop_start(pulse->mainloop) < 0)
754 pa_threaded_mainloop_unlock(pulse->mainloop);
758 pa_threaded_mainloop_unlock(pulse->mainloop);
760 if (!rdpsnd_pulse_context_connect((rdpsndDevicePlugin*)pulse))
763 pEntryPoints->pRegisterRdpsndDevice(pEntryPoints->rdpsnd, (rdpsndDevicePlugin*)pulse);
764 return CHANNEL_RC_OK;
766 rdpsnd_pulse_free((rdpsndDevicePlugin*)pulse);