22 #include <freerdp/config.h>
31 #include <winpr/crt.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;
59 static 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);
88 static BOOL rdpsnd_pulse_format_supported(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* format);
90 static void rdpsnd_pulse_get_sink_info(pa_context* c,
const pa_sink_info* i,
int eol,
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;
126 static 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);
159 static 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);
211 static void rdpsnd_pulse_stream_success_callback(pa_stream* stream,
int success,
void* userdata)
213 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
215 if (!rdpsnd_check_pulse(pulse, TRUE))
218 pa_threaded_mainloop_signal(pulse->mainloop, 0);
221 static void rdpsnd_pulse_wait_for_operation(rdpsndPulsePlugin* pulse, pa_operation* operation)
223 if (!rdpsnd_check_pulse(pulse, TRUE))
229 while (pa_operation_get_state(operation) == PA_OPERATION_RUNNING)
231 pa_threaded_mainloop_wait(pulse->mainloop);
234 pa_operation_unref(operation);
237 static void rdpsnd_pulse_stream_state_callback(pa_stream* stream,
void* userdata)
239 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
241 WINPR_ASSERT(stream);
242 if (!rdpsnd_check_pulse(pulse, TRUE))
245 pa_stream_state_t state = pa_stream_get_state(stream);
249 case PA_STREAM_READY:
250 pa_threaded_mainloop_signal(pulse->mainloop, 0);
253 case PA_STREAM_FAILED:
254 case PA_STREAM_TERMINATED:
256 pulse->stream = NULL;
257 pa_threaded_mainloop_signal(pulse->mainloop, 0);
265 static void rdpsnd_pulse_stream_request_callback(pa_stream* stream,
size_t length,
void* userdata)
267 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)userdata;
269 WINPR_ASSERT(stream);
270 if (!rdpsnd_check_pulse(pulse, TRUE))
273 pa_threaded_mainloop_signal(pulse->mainloop, 0);
276 static void rdpsnd_pulse_close(rdpsndDevicePlugin* device)
278 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
282 if (!rdpsnd_check_pulse(pulse, FALSE))
285 pa_threaded_mainloop_lock(pulse->mainloop);
288 rdpsnd_pulse_wait_for_operation(
289 pulse, pa_stream_drain(pulse->stream, rdpsnd_pulse_stream_success_callback, pulse));
290 pa_stream_disconnect(pulse->stream);
291 pa_stream_unref(pulse->stream);
292 pulse->stream = NULL;
294 pa_threaded_mainloop_unlock(pulse->mainloop);
297 static BOOL rdpsnd_pulse_set_format_spec(rdpsndPulsePlugin* pulse,
const AUDIO_FORMAT* format)
299 pa_sample_spec sample_spec = { 0 };
301 WINPR_ASSERT(format);
303 if (!rdpsnd_check_pulse(pulse, FALSE))
306 if (!rdpsnd_pulse_format_supported(&pulse->device, format))
309 sample_spec.rate = format->nSamplesPerSec;
310 sample_spec.channels = WINPR_ASSERTING_INT_CAST(uint8_t, format->nChannels);
312 switch (format->wFormatTag)
314 case WAVE_FORMAT_PCM:
315 switch (format->wBitsPerSample)
318 sample_spec.format = PA_SAMPLE_U8;
322 sample_spec.format = PA_SAMPLE_S16LE;
335 pulse->sample_spec = sample_spec;
339 static BOOL rdpsnd_pulse_context_connect(rdpsndDevicePlugin* device)
341 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
343 pulse->context = pa_context_new(pa_threaded_mainloop_get_api(pulse->mainloop),
"freerdp");
348 pa_context_set_state_callback(pulse->context, rdpsnd_pulse_context_state_callback, pulse);
350 if (!rdpsnd_pulse_connect((rdpsndDevicePlugin*)pulse))
356 static BOOL rdpsnd_pulse_open_stream(rdpsndDevicePlugin* device)
358 pa_stream_state_t state = PA_STREAM_FAILED;
359 int flags = PA_STREAM_NOFLAGS;
360 pa_buffer_attr buffer_attr = { 0 };
361 char ss[PA_SAMPLE_SPEC_SNPRINT_MAX] = { 0 };
362 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
364 if (pa_sample_spec_valid(&pulse->sample_spec) == 0)
366 pa_sample_spec_snprint(ss,
sizeof(ss), &pulse->sample_spec);
370 pa_threaded_mainloop_lock(pulse->mainloop);
373 pa_threaded_mainloop_unlock(pulse->mainloop);
374 if (pulse->reconnect_delay_seconds >= 0 && time(NULL) - pulse->reconnect_time >= 0)
375 rdpsnd_pulse_context_connect(device);
376 pa_threaded_mainloop_lock(pulse->mainloop);
379 if (!rdpsnd_check_pulse(pulse, FALSE))
381 pa_threaded_mainloop_unlock(pulse->mainloop);
385 pulse->stream = pa_stream_new(pulse->context,
"freerdp", &pulse->sample_spec, NULL);
389 pa_threaded_mainloop_unlock(pulse->mainloop);
394 pa_stream_set_state_callback(pulse->stream, rdpsnd_pulse_stream_state_callback, pulse);
395 pa_stream_set_write_callback(pulse->stream, rdpsnd_pulse_stream_request_callback, pulse);
396 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE;
398 if (pulse->latency > 0)
400 const size_t val = pa_usec_to_bytes(1000ULL * pulse->latency, &pulse->sample_spec);
401 buffer_attr.maxlength = UINT32_MAX;
402 buffer_attr.tlength = (val > UINT32_MAX) ? UINT32_MAX : (UINT32)val;
403 buffer_attr.prebuf = UINT32_MAX;
404 buffer_attr.minreq = UINT32_MAX;
405 buffer_attr.fragsize = UINT32_MAX;
406 flags |= PA_STREAM_ADJUST_LATENCY;
410 pa_stream_flags_t eflags = (pa_stream_flags_t)flags;
411 if (pa_stream_connect_playback(pulse->stream, pulse->device_name,
412 pulse->latency > 0 ? &buffer_attr : NULL, eflags, NULL,
415 WLog_ERR(TAG,
"error connecting playback stream");
416 pa_stream_unref(pulse->stream);
417 pulse->stream = NULL;
418 pa_threaded_mainloop_unlock(pulse->mainloop);
422 while (pulse->stream)
424 state = pa_stream_get_state(pulse->stream);
426 if (state == PA_STREAM_READY)
429 if (!PA_STREAM_IS_GOOD(state))
434 pa_threaded_mainloop_wait(pulse->mainloop);
437 pa_threaded_mainloop_unlock(pulse->mainloop);
439 if (state == PA_STREAM_READY)
442 rdpsnd_pulse_close(device);
446 static BOOL rdpsnd_pulse_open(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* format,
449 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
451 WINPR_ASSERT(format);
453 if (!rdpsnd_check_pulse(pulse, FALSE))
456 if (!rdpsnd_pulse_set_format_spec(pulse, format))
459 pulse->latency = latency;
461 return rdpsnd_pulse_open_stream(device);
464 static void rdpsnd_pulse_free(rdpsndDevicePlugin* device)
466 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
471 rdpsnd_pulse_close(device);
474 pa_threaded_mainloop_stop(pulse->mainloop);
478 pa_context_disconnect(pulse->context);
479 pa_context_unref(pulse->context);
480 pulse->context = NULL;
485 pa_threaded_mainloop_free(pulse->mainloop);
486 pulse->mainloop = NULL;
489 free(pulse->device_name);
493 static BOOL rdpsnd_pulse_default_format(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* desired,
496 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
498 if (!pulse || !defaultFormat)
501 *defaultFormat = *desired;
502 defaultFormat->data = NULL;
503 defaultFormat->cbSize = 0;
504 defaultFormat->wFormatTag = WAVE_FORMAT_PCM;
505 if ((defaultFormat->nChannels < 1) || (defaultFormat->nChannels > PA_CHANNELS_MAX))
506 defaultFormat->nChannels = 2;
507 if ((defaultFormat->nSamplesPerSec < 1) || (defaultFormat->nSamplesPerSec > PA_RATE_MAX))
508 defaultFormat->nSamplesPerSec = 44100;
509 if ((defaultFormat->wBitsPerSample != 8) && (defaultFormat->wBitsPerSample != 16))
510 defaultFormat->wBitsPerSample = 16;
512 defaultFormat->nBlockAlign = defaultFormat->nChannels * defaultFormat->wBitsPerSample / 8;
513 defaultFormat->nAvgBytesPerSec = defaultFormat->nBlockAlign * defaultFormat->nSamplesPerSec;
517 BOOL rdpsnd_pulse_format_supported(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* format)
519 WINPR_ASSERT(device);
520 WINPR_ASSERT(format);
522 switch (format->wFormatTag)
524 case WAVE_FORMAT_PCM:
525 if (format->cbSize == 0 && (format->nSamplesPerSec <= PA_RATE_MAX) &&
526 (format->wBitsPerSample == 8 || format->wBitsPerSample == 16) &&
527 (format->nChannels >= 1 && format->nChannels <= PA_CHANNELS_MAX))
541 static UINT32 rdpsnd_pulse_get_volume(rdpsndDevicePlugin* device)
543 pa_operation* o = NULL;
544 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
546 if (!rdpsnd_check_pulse(pulse, FALSE))
549 pa_threaded_mainloop_lock(pulse->mainloop);
550 o = pa_context_get_sink_info_by_index(pulse->context, 0, rdpsnd_pulse_get_sink_info, pulse);
552 pa_operation_unref(o);
553 pa_threaded_mainloop_unlock(pulse->mainloop);
554 return pulse->volume;
557 static void rdpsnd_set_volume_success_cb(pa_context* c,
int success,
void* userdata)
559 rdpsndPulsePlugin* pulse = userdata;
561 if (!rdpsnd_check_pulse(pulse, TRUE))
565 WLog_INFO(TAG,
"%d", success);
568 static BOOL rdpsnd_pulse_set_volume(rdpsndDevicePlugin* device, UINT32 value)
570 pa_cvolume cv = { 0 };
571 pa_volume_t left = 0;
572 pa_volume_t right = 0;
573 pa_operation* operation = NULL;
574 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
576 if (!rdpsnd_check_pulse(pulse, TRUE))
578 WLog_WARN(TAG,
"%s called before pulse backend was initialized");
582 left = (pa_volume_t)(value & 0xFFFF);
583 right = (pa_volume_t)((value >> 16) & 0xFFFF);
584 pa_cvolume_init(&cv);
586 cv.values[0] = PA_VOLUME_MUTED + (left * (PA_VOLUME_NORM - PA_VOLUME_MUTED)) / PA_VOLUME_NORM;
587 cv.values[1] = PA_VOLUME_MUTED + (right * (PA_VOLUME_NORM - PA_VOLUME_MUTED)) / PA_VOLUME_NORM;
588 pa_threaded_mainloop_lock(pulse->mainloop);
589 operation = pa_context_set_sink_input_volume(pulse->context, pa_stream_get_index(pulse->stream),
590 &cv, rdpsnd_set_volume_success_cb, pulse);
593 pa_operation_unref(operation);
595 pa_threaded_mainloop_unlock(pulse->mainloop);
599 static UINT rdpsnd_pulse_play(rdpsndDevicePlugin* device,
const BYTE* data,
size_t size)
602 void* pa_data = NULL;
604 pa_usec_t latency = 0;
606 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
611 pa_threaded_mainloop_lock(pulse->mainloop);
613 if (!rdpsnd_check_pulse(pulse, TRUE))
615 pa_threaded_mainloop_unlock(pulse->mainloop);
617 WLog_DBG(TAG,
"reconnecting playback stream");
618 rdpsnd_pulse_open_stream(device);
626 status = pa_stream_begin_write(pulse->stream, &pa_data, &length);
631 memcpy(pa_data, data, length);
633 status = pa_stream_write(pulse->stream, pa_data, length, NULL, 0LL, PA_SEEK_RELATIVE);
644 if (pa_stream_get_latency(pulse->stream, &latency, &negative) != 0)
647 pa_threaded_mainloop_unlock(pulse->mainloop);
649 const pa_usec_t val = latency / 1000;
650 if (val > UINT32_MAX)
655 static UINT rdpsnd_pulse_parse_addin_args(rdpsndDevicePlugin* device,
const ADDIN_ARGV* args)
660 rdpsndPulsePlugin* pulse = (rdpsndPulsePlugin*)device;
662 {
"dev", COMMAND_LINE_VALUE_REQUIRED,
"<device>", NULL, NULL, -1, NULL,
"device" },
663 {
"reconnect_delay_seconds", COMMAND_LINE_VALUE_REQUIRED,
"<reconnect_delay_seconds>", NULL,
664 NULL, -1, NULL,
"reconnect_delay_seconds" },
665 { NULL, 0, NULL, NULL, NULL, -1, NULL, NULL }
668 COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
673 status = CommandLineParseArgumentsA(args->argc, args->argv, rdpsnd_pulse_args, flags, pulse,
677 return ERROR_INVALID_DATA;
679 arg = rdpsnd_pulse_args;
683 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
686 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg,
"dev")
688 pulse->device_name = _strdup(arg->Value);
690 if (!pulse->device_name)
691 return ERROR_OUTOFMEMORY;
693 CommandLineSwitchCase(arg,
"reconnect_delay_seconds")
695 unsigned long val = strtoul(arg->Value, NULL, 0);
697 if ((errno != 0) || (val > INT32_MAX))
698 return ERROR_INVALID_DATA;
700 pulse->reconnect_delay_seconds = (time_t)val;
702 CommandLineSwitchEnd(arg)
703 }
while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
705 return CHANNEL_RC_OK;
708 FREERDP_ENTRY_POINT(UINT VCAPITYPE pulse_freerdp_rdpsnd_client_subsystem_entry(
712 rdpsndPulsePlugin* pulse = NULL;
715 WINPR_ASSERT(pEntryPoints);
717 pulse = (rdpsndPulsePlugin*)calloc(1,
sizeof(rdpsndPulsePlugin));
720 return CHANNEL_RC_NO_MEMORY;
722 pulse->device.Open = rdpsnd_pulse_open;
723 pulse->device.FormatSupported = rdpsnd_pulse_format_supported;
724 pulse->device.GetVolume = rdpsnd_pulse_get_volume;
725 pulse->device.SetVolume = rdpsnd_pulse_set_volume;
726 pulse->device.Play = rdpsnd_pulse_play;
727 pulse->device.Close = rdpsnd_pulse_close;
728 pulse->device.Free = rdpsnd_pulse_free;
729 pulse->device.DefaultFormat = rdpsnd_pulse_default_format;
730 args = pEntryPoints->args;
734 ret = rdpsnd_pulse_parse_addin_args(&pulse->device, args);
736 if (ret != CHANNEL_RC_OK)
738 WLog_ERR(TAG,
"error parsing arguments");
742 pulse->reconnect_delay_seconds = 5;
743 pulse->reconnect_time = time(NULL);
745 ret = CHANNEL_RC_NO_MEMORY;
746 pulse->mainloop = pa_threaded_mainloop_new();
748 if (!pulse->mainloop)
751 pa_threaded_mainloop_lock(pulse->mainloop);
753 if (pa_threaded_mainloop_start(pulse->mainloop) < 0)
755 pa_threaded_mainloop_unlock(pulse->mainloop);
759 pa_threaded_mainloop_unlock(pulse->mainloop);
761 if (!rdpsnd_pulse_context_connect((rdpsndDevicePlugin*)pulse))
764 pEntryPoints->pRegisterRdpsndDevice(pEntryPoints->rdpsnd, (rdpsndDevicePlugin*)pulse);
765 return CHANNEL_RC_OK;
767 rdpsnd_pulse_free((rdpsndDevicePlugin*)pulse);