22#include <freerdp/config.h>
29#include <winpr/cmdline.h>
30#include <winpr/wlog.h>
31#include <winpr/cast.h>
33#include <pulse/pulseaudio.h>
35#include <freerdp/types.h>
36#include <freerdp/addin.h>
37#include <freerdp/freerdp.h>
38#include <freerdp/codec/audio.h>
39#include <freerdp/client/audin.h>
40#include <freerdp/utils/helpers.h>
42#include "audin_main.h"
51 UINT32 frames_per_packet;
52 pa_threaded_mainloop* mainloop;
54 pa_sample_spec sample_spec;
58 size_t bytes_per_frame;
64 rdpContext* rdpcontext;
68static const char* pulse_context_state_string(pa_context_state_t state)
72 case PA_CONTEXT_UNCONNECTED:
73 return "PA_CONTEXT_UNCONNECTED";
74 case PA_CONTEXT_CONNECTING:
75 return "PA_CONTEXT_CONNECTING";
76 case PA_CONTEXT_AUTHORIZING:
77 return "PA_CONTEXT_AUTHORIZING";
78 case PA_CONTEXT_SETTING_NAME:
79 return "PA_CONTEXT_SETTING_NAME";
80 case PA_CONTEXT_READY:
81 return "PA_CONTEXT_READY";
82 case PA_CONTEXT_FAILED:
83 return "PA_CONTEXT_FAILED";
84 case PA_CONTEXT_TERMINATED:
85 return "PA_CONTEXT_TERMINATED";
91static const char* pulse_stream_state_string(pa_stream_state_t state)
95 case PA_STREAM_UNCONNECTED:
96 return "PA_STREAM_UNCONNECTED";
97 case PA_STREAM_CREATING:
98 return "PA_STREAM_CREATING";
100 return "PA_STREAM_READY";
101 case PA_STREAM_FAILED:
102 return "PA_STREAM_FAILED";
103 case PA_STREAM_TERMINATED:
104 return "PA_STREAM_TERMINATED";
110static void audin_pulse_context_state_callback(pa_context* context,
void* userdata)
112 AudinPulseDevice* pulse = (AudinPulseDevice*)userdata;
113 pa_context_state_t state = pa_context_get_state(context);
115 WLog_Print(pulse->log, WLOG_DEBUG,
"context state %s", pulse_context_state_string(state));
118 case PA_CONTEXT_READY:
119 case PA_CONTEXT_FAILED:
120 case PA_CONTEXT_TERMINATED:
121 pa_threaded_mainloop_signal(pulse->mainloop, 0);
134static UINT audin_pulse_connect(IAudinDevice* device)
136 pa_context_state_t state = PA_CONTEXT_FAILED;
137 AudinPulseDevice* pulse = (AudinPulseDevice*)device;
140 return ERROR_INVALID_PARAMETER;
142 if (pa_context_connect(pulse->context,
nullptr, PA_CONTEXT_NOFLAGS,
nullptr))
144 WLog_Print(pulse->log, WLOG_ERROR,
"pa_context_connect failed (%d)",
145 pa_context_errno(pulse->context));
146 return ERROR_INTERNAL_ERROR;
149 pa_threaded_mainloop_lock(pulse->mainloop);
151 if (pa_threaded_mainloop_start(pulse->mainloop) < 0)
153 pa_threaded_mainloop_unlock(pulse->mainloop);
154 WLog_Print(pulse->log, WLOG_ERROR,
"pa_threaded_mainloop_start failed (%d)",
155 pa_context_errno(pulse->context));
156 return ERROR_INTERNAL_ERROR;
161 state = pa_context_get_state(pulse->context);
163 if (state == PA_CONTEXT_READY)
166 if (!PA_CONTEXT_IS_GOOD(state))
168 WLog_Print(pulse->log, WLOG_ERROR,
"bad context state (%s: %d)",
169 pulse_context_state_string(state), pa_context_errno(pulse->context));
170 pa_context_disconnect(pulse->context);
171 return ERROR_INVALID_STATE;
174 pa_threaded_mainloop_wait(pulse->mainloop);
177 pa_threaded_mainloop_unlock(pulse->mainloop);
178 WLog_Print(pulse->log, WLOG_DEBUG,
"connected");
179 return CHANNEL_RC_OK;
187static UINT audin_pulse_free(IAudinDevice* device)
189 AudinPulseDevice* pulse = (AudinPulseDevice*)device;
192 return ERROR_INVALID_PARAMETER;
196 pa_threaded_mainloop_stop(pulse->mainloop);
201 pa_context_disconnect(pulse->context);
202 pa_context_unref(pulse->context);
203 pulse->context =
nullptr;
208 pa_threaded_mainloop_free(pulse->mainloop);
209 pulse->mainloop =
nullptr;
212 free(pulse->device_name);
213 free(pulse->client_name);
214 free(pulse->stream_name);
216 return CHANNEL_RC_OK;
219static BOOL audin_pulse_format_supported(IAudinDevice* device,
const AUDIO_FORMAT* format)
221 AudinPulseDevice* pulse = (AudinPulseDevice*)device;
223 if (!pulse || !format)
229 switch (format->wFormatTag)
231 case WAVE_FORMAT_PCM:
232 if (format->cbSize == 0 && (format->nSamplesPerSec <= PA_RATE_MAX) &&
233 (format->wBitsPerSample == 8 || format->wBitsPerSample == 16) &&
234 (format->nChannels >= 1 && format->nChannels <= PA_CHANNELS_MAX))
253static UINT audin_pulse_set_format(IAudinDevice* device,
const AUDIO_FORMAT* format,
254 UINT32 FramesPerPacket)
256 AudinPulseDevice* pulse = (AudinPulseDevice*)device;
258 if (!pulse || !format)
259 return ERROR_INVALID_PARAMETER;
262 return ERROR_INVALID_PARAMETER;
264 if (FramesPerPacket > 0)
265 pulse->frames_per_packet = FramesPerPacket;
267 pa_sample_format_t sformat = PA_SAMPLE_INVALID;
268 switch (format->wFormatTag)
270 case WAVE_FORMAT_PCM:
271 switch (format->wBitsPerSample)
274 sformat = PA_SAMPLE_U8;
278 sformat = PA_SAMPLE_S16LE;
282 return ERROR_INTERNAL_ERROR;
288 return ERROR_INTERNAL_ERROR;
291 const pa_sample_spec sample_spec = {
293 .rate = format->nSamplesPerSec,
294 .channels = WINPR_ASSERTING_INT_CAST(uint8_t, format->nChannels),
297 pulse->sample_spec = sample_spec;
298 pulse->format = *format;
299 return CHANNEL_RC_OK;
302static void audin_pulse_stream_state_callback(pa_stream* stream,
void* userdata)
304 AudinPulseDevice* pulse = (AudinPulseDevice*)userdata;
307 pa_stream_state_t state = pa_stream_get_state(stream);
309 WLog_Print(pulse->log, WLOG_DEBUG,
"stream state %s", pulse_stream_state_string(state));
312 case PA_STREAM_READY:
313 case PA_STREAM_FAILED:
314 case PA_STREAM_TERMINATED:
315 pa_threaded_mainloop_signal(pulse->mainloop, 0);
318 case PA_STREAM_UNCONNECTED:
319 case PA_STREAM_CREATING:
325static void audin_pulse_stream_request_callback(pa_stream* stream,
size_t length,
void* userdata)
327 const void* data =
nullptr;
328 AudinPulseDevice* pulse = (AudinPulseDevice*)userdata;
329 UINT error = CHANNEL_RC_OK;
331 if (pa_stream_peek(stream, &data, &length) < 0)
333 WLog_Print(pulse->log, WLOG_WARN,
"pa_stream_peek failed (%d)",
334 pa_context_errno(pulse->context));
345 error = IFCALLRESULT(CHANNEL_RC_OK, pulse->receive, &pulse->format, data, length,
347 pa_stream_drop(stream);
349 if (error && pulse->rdpcontext)
350 setChannelError(pulse->rdpcontext, error,
"audin_pulse_thread_func reported an error");
358static UINT audin_pulse_close(IAudinDevice* device)
360 AudinPulseDevice* pulse = (AudinPulseDevice*)device;
363 return ERROR_INVALID_PARAMETER;
367 pa_threaded_mainloop_lock(pulse->mainloop);
372 pa_stream_set_read_callback(pulse->stream,
nullptr,
nullptr);
373 pa_stream_set_state_callback(pulse->stream,
nullptr,
nullptr);
374 pa_stream_disconnect(pulse->stream);
375 pa_stream_unref(pulse->stream);
376 pulse->stream =
nullptr;
379 pulse->receive =
nullptr;
380 pulse->user_data =
nullptr;
381 pa_threaded_mainloop_unlock(pulse->mainloop);
382 return CHANNEL_RC_OK;
390static UINT audin_pulse_open(IAudinDevice* device, AudinReceive receive,
void* user_data)
392 pa_stream_state_t state = PA_STREAM_FAILED;
393 pa_buffer_attr buffer_attr = WINPR_C_ARRAY_INIT;
394 AudinPulseDevice* pulse = (AudinPulseDevice*)device;
396 if (!pulse || !receive || !user_data)
397 return ERROR_INVALID_PARAMETER;
400 return ERROR_INVALID_PARAMETER;
402 if (!pulse->sample_spec.rate || pulse->stream)
403 return ERROR_INVALID_PARAMETER;
405 pa_threaded_mainloop_lock(pulse->mainloop);
406 pulse->stream = pa_stream_new(pulse->context, pulse->stream_name, &pulse->sample_spec,
nullptr);
410 pa_threaded_mainloop_unlock(pulse->mainloop);
411 WLog_Print(pulse->log, WLOG_DEBUG,
"pa_stream_new failed (%d)",
412 pa_context_errno(pulse->context));
413 const int rc = pa_context_errno(pulse->context);
417 pulse->bytes_per_frame = pa_frame_size(&pulse->sample_spec);
418 pa_stream_set_state_callback(pulse->stream, audin_pulse_stream_state_callback, pulse);
419 pa_stream_set_read_callback(pulse->stream, audin_pulse_stream_request_callback, pulse);
420 buffer_attr.maxlength = UINT32_MAX;
421 buffer_attr.tlength = UINT32_MAX;
422 buffer_attr.prebuf = UINT32_MAX;
423 buffer_attr.minreq = UINT32_MAX;
425 if ((pulse->bytes_per_frame == 0) || (pulse->frames_per_packet == 0) ||
426 (pulse->bytes_per_frame > UINT32_MAX / pulse->frames_per_packet))
428 pa_threaded_mainloop_unlock(pulse->mainloop);
429 return CHANNEL_RC_BAD_INIT_HANDLE;
432 const size_t frag = pulse->bytes_per_frame * pulse->frames_per_packet;
433 WINPR_ASSERT(frag <= UINT32_MAX);
434 buffer_attr.fragsize = (uint32_t)frag;
436 if (buffer_attr.fragsize % pulse->format.nBlockAlign)
437 buffer_attr.fragsize +=
438 pulse->format.nBlockAlign - buffer_attr.fragsize % pulse->format.nBlockAlign;
440 if (pa_stream_connect_record(pulse->stream, pulse->device_name, &buffer_attr,
441 PA_STREAM_ADJUST_LATENCY) < 0)
443 pa_threaded_mainloop_unlock(pulse->mainloop);
444 WLog_Print(pulse->log, WLOG_ERROR,
"pa_stream_connect_playback failed (%d)",
445 pa_context_errno(pulse->context));
446 const int rc = pa_context_errno(pulse->context);
451 pulse->receive = receive;
452 pulse->user_data = user_data;
454 while (pulse->stream)
456 state = pa_stream_get_state(pulse->stream);
458 if (state == PA_STREAM_READY)
461 if (!PA_STREAM_IS_GOOD(state))
463 audin_pulse_close(device);
464 WLog_Print(pulse->log, WLOG_ERROR,
"bad stream state (%s: %d)",
465 pulse_stream_state_string(state), pa_context_errno(pulse->context));
466 pa_threaded_mainloop_unlock(pulse->mainloop);
467 const int rc = pa_context_errno(pulse->context);
471 pa_threaded_mainloop_wait(pulse->mainloop);
474 pa_threaded_mainloop_unlock(pulse->mainloop);
475 pulse->buffer_frames = 0;
476 WLog_Print(pulse->log, WLOG_DEBUG,
"connected");
477 return CHANNEL_RC_OK;
485static UINT audin_pulse_parse_addin_args(AudinPulseDevice* pulse,
const ADDIN_ARGV* args)
488 {
"dev", COMMAND_LINE_VALUE_REQUIRED,
"<device>",
nullptr,
nullptr, -1,
nullptr,
489 "audio device name" },
490 {
"client_name", COMMAND_LINE_VALUE_REQUIRED,
"<client_name>",
nullptr,
nullptr, -1,
491 nullptr,
"name of pulse client" },
492 {
"stream_name", COMMAND_LINE_VALUE_REQUIRED,
"<stream_name>",
nullptr,
nullptr, -1,
493 nullptr,
"name of pulse stream" },
494 {
nullptr, 0,
nullptr,
nullptr,
nullptr, -1,
nullptr,
nullptr }
498 COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
499 const int status = CommandLineParseArgumentsA(args->argc, args->argv, audin_pulse_args, flags,
500 pulse,
nullptr,
nullptr);
503 return ERROR_INVALID_PARAMETER;
507 const char* client_name =
nullptr;
508 const char* stream_name =
nullptr;
511 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
514 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg,
"dev")
516 pulse->device_name = _strdup(arg->Value);
518 if (!pulse->device_name)
520 WLog_Print(pulse->log, WLOG_ERROR,
"_strdup failed!");
521 return CHANNEL_RC_NO_MEMORY;
524 CommandLineSwitchEnd(arg)
525 }
while ((arg = CommandLineFindNextArgumentA(arg)) !=
nullptr);
528 client_name = freerdp_getApplicationDetailsString();
530 stream_name = freerdp_getApplicationDetailsString();
532 pulse->client_name = _strdup(client_name);
533 pulse->stream_name = _strdup(stream_name);
534 if (!pulse->client_name || !pulse->stream_name)
535 return ERROR_OUTOFMEMORY;
537 return CHANNEL_RC_OK;
545FREERDP_ENTRY_POINT(UINT VCAPITYPE pulse_freerdp_audin_client_subsystem_entry(
546 PFREERDP_AUDIN_DEVICE_ENTRY_POINTS pEntryPoints))
549 AudinPulseDevice* pulse =
nullptr;
551 pulse = (AudinPulseDevice*)calloc(1,
sizeof(AudinPulseDevice));
555 WLog_ERR(TAG,
"calloc failed!");
556 return CHANNEL_RC_NO_MEMORY;
559 pulse->log = WLog_Get(TAG);
560 pulse->iface.Open = audin_pulse_open;
561 pulse->iface.FormatSupported = audin_pulse_format_supported;
562 pulse->iface.SetFormat = audin_pulse_set_format;
563 pulse->iface.Close = audin_pulse_close;
564 pulse->iface.Free = audin_pulse_free;
565 pulse->rdpcontext = pEntryPoints->rdpcontext;
566 args = pEntryPoints->args;
568 if ((error = audin_pulse_parse_addin_args(pulse, args)))
570 WLog_Print(pulse->log, WLOG_ERROR,
571 "audin_pulse_parse_addin_args failed with error %" PRIu32
"!", error);
575 pulse->mainloop = pa_threaded_mainloop_new();
577 if (!pulse->mainloop)
579 WLog_Print(pulse->log, WLOG_ERROR,
"pa_threaded_mainloop_new failed");
580 error = CHANNEL_RC_NO_MEMORY;
585 pa_context_new(pa_threaded_mainloop_get_api(pulse->mainloop), pulse->client_name);
589 WLog_Print(pulse->log, WLOG_ERROR,
"pa_context_new failed");
590 error = CHANNEL_RC_NO_MEMORY;
594 pa_context_set_state_callback(pulse->context, audin_pulse_context_state_callback, pulse);
596 if ((error = audin_pulse_connect(&pulse->iface)))
598 WLog_Print(pulse->log, WLOG_ERROR,
"audin_pulse_connect failed");
602 if ((error = pEntryPoints->pRegisterAudinDevice(pEntryPoints->plugin, &pulse->iface)))
604 WLog_Print(pulse->log, WLOG_ERROR,
"RegisterAudinDevice failed with error %" PRIu32
"!",
609 return CHANNEL_RC_OK;
611 audin_pulse_free(&pulse->iface);