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>
41#include "audin_main.h"
48 UINT32 frames_per_packet;
49 pa_threaded_mainloop* mainloop;
51 pa_sample_spec sample_spec;
55 size_t bytes_per_frame;
61 rdpContext* rdpcontext;
65static const char* pulse_context_state_string(pa_context_state_t state)
69 case PA_CONTEXT_UNCONNECTED:
70 return "PA_CONTEXT_UNCONNECTED";
71 case PA_CONTEXT_CONNECTING:
72 return "PA_CONTEXT_CONNECTING";
73 case PA_CONTEXT_AUTHORIZING:
74 return "PA_CONTEXT_AUTHORIZING";
75 case PA_CONTEXT_SETTING_NAME:
76 return "PA_CONTEXT_SETTING_NAME";
77 case PA_CONTEXT_READY:
78 return "PA_CONTEXT_READY";
79 case PA_CONTEXT_FAILED:
80 return "PA_CONTEXT_FAILED";
81 case PA_CONTEXT_TERMINATED:
82 return "PA_CONTEXT_TERMINATED";
88static const char* pulse_stream_state_string(pa_stream_state_t state)
92 case PA_STREAM_UNCONNECTED:
93 return "PA_STREAM_UNCONNECTED";
94 case PA_STREAM_CREATING:
95 return "PA_STREAM_CREATING";
97 return "PA_STREAM_READY";
98 case PA_STREAM_FAILED:
99 return "PA_STREAM_FAILED";
100 case PA_STREAM_TERMINATED:
101 return "PA_STREAM_TERMINATED";
107static void audin_pulse_context_state_callback(pa_context* context,
void* userdata)
109 AudinPulseDevice* pulse = (AudinPulseDevice*)userdata;
110 pa_context_state_t state = pa_context_get_state(context);
112 WLog_Print(pulse->log, WLOG_DEBUG,
"context state %s", pulse_context_state_string(state));
115 case PA_CONTEXT_READY:
116 case PA_CONTEXT_FAILED:
117 case PA_CONTEXT_TERMINATED:
118 pa_threaded_mainloop_signal(pulse->mainloop, 0);
131static UINT audin_pulse_connect(IAudinDevice* device)
133 pa_context_state_t state = PA_CONTEXT_FAILED;
134 AudinPulseDevice* pulse = (AudinPulseDevice*)device;
137 return ERROR_INVALID_PARAMETER;
139 if (pa_context_connect(pulse->context, NULL, 0, NULL))
141 WLog_Print(pulse->log, WLOG_ERROR,
"pa_context_connect failed (%d)",
142 pa_context_errno(pulse->context));
143 return ERROR_INTERNAL_ERROR;
146 pa_threaded_mainloop_lock(pulse->mainloop);
148 if (pa_threaded_mainloop_start(pulse->mainloop) < 0)
150 pa_threaded_mainloop_unlock(pulse->mainloop);
151 WLog_Print(pulse->log, WLOG_ERROR,
"pa_threaded_mainloop_start failed (%d)",
152 pa_context_errno(pulse->context));
153 return ERROR_INTERNAL_ERROR;
158 state = pa_context_get_state(pulse->context);
160 if (state == PA_CONTEXT_READY)
163 if (!PA_CONTEXT_IS_GOOD(state))
165 WLog_Print(pulse->log, WLOG_ERROR,
"bad context state (%s: %d)",
166 pulse_context_state_string(state), pa_context_errno(pulse->context));
167 pa_context_disconnect(pulse->context);
168 return ERROR_INVALID_STATE;
171 pa_threaded_mainloop_wait(pulse->mainloop);
174 pa_threaded_mainloop_unlock(pulse->mainloop);
175 WLog_Print(pulse->log, WLOG_DEBUG,
"connected");
176 return CHANNEL_RC_OK;
184static UINT audin_pulse_free(IAudinDevice* device)
186 AudinPulseDevice* pulse = (AudinPulseDevice*)device;
189 return ERROR_INVALID_PARAMETER;
193 pa_threaded_mainloop_stop(pulse->mainloop);
198 pa_context_disconnect(pulse->context);
199 pa_context_unref(pulse->context);
200 pulse->context = NULL;
205 pa_threaded_mainloop_free(pulse->mainloop);
206 pulse->mainloop = NULL;
210 return CHANNEL_RC_OK;
213static BOOL audin_pulse_format_supported(IAudinDevice* device,
const AUDIO_FORMAT* format)
215 AudinPulseDevice* pulse = (AudinPulseDevice*)device;
217 if (!pulse || !format)
223 switch (format->wFormatTag)
225 case WAVE_FORMAT_PCM:
226 if (format->cbSize == 0 && (format->nSamplesPerSec <= PA_RATE_MAX) &&
227 (format->wBitsPerSample == 8 || format->wBitsPerSample == 16) &&
228 (format->nChannels >= 1 && format->nChannels <= PA_CHANNELS_MAX))
247static UINT audin_pulse_set_format(IAudinDevice* device,
const AUDIO_FORMAT* format,
248 UINT32 FramesPerPacket)
250 pa_sample_spec sample_spec = { 0 };
251 AudinPulseDevice* pulse = (AudinPulseDevice*)device;
253 if (!pulse || !format)
254 return ERROR_INVALID_PARAMETER;
257 return ERROR_INVALID_PARAMETER;
259 if (FramesPerPacket > 0)
260 pulse->frames_per_packet = FramesPerPacket;
262 sample_spec.rate = format->nSamplesPerSec;
264 sample_spec.channels = WINPR_ASSERTING_INT_CAST(uint8_t, format->nChannels);
266 switch (format->wFormatTag)
268 case WAVE_FORMAT_PCM:
269 switch (format->wBitsPerSample)
272 sample_spec.format = PA_SAMPLE_U8;
276 sample_spec.format = PA_SAMPLE_S16LE;
280 return ERROR_INTERNAL_ERROR;
286 return ERROR_INTERNAL_ERROR;
289 pulse->sample_spec = sample_spec;
290 pulse->format = *format;
291 return CHANNEL_RC_OK;
294static void audin_pulse_stream_state_callback(pa_stream* stream,
void* userdata)
296 AudinPulseDevice* pulse = (AudinPulseDevice*)userdata;
299 pa_stream_state_t state = pa_stream_get_state(stream);
301 WLog_Print(pulse->log, WLOG_DEBUG,
"stream state %s", pulse_stream_state_string(state));
304 case PA_STREAM_READY:
305 case PA_STREAM_FAILED:
306 case PA_STREAM_TERMINATED:
307 pa_threaded_mainloop_signal(pulse->mainloop, 0);
310 case PA_STREAM_UNCONNECTED:
311 case PA_STREAM_CREATING:
317static void audin_pulse_stream_request_callback(pa_stream* stream,
size_t length,
void* userdata)
319 const void* data = NULL;
320 AudinPulseDevice* pulse = (AudinPulseDevice*)userdata;
321 UINT error = CHANNEL_RC_OK;
322 pa_stream_peek(stream, &data, &length);
324 IFCALLRESULT(CHANNEL_RC_OK, pulse->receive, &pulse->format, data, length, pulse->user_data);
325 pa_stream_drop(stream);
327 if (error && pulse->rdpcontext)
328 setChannelError(pulse->rdpcontext, error,
"audin_pulse_thread_func reported an error");
336static UINT audin_pulse_close(IAudinDevice* device)
338 AudinPulseDevice* pulse = (AudinPulseDevice*)device;
341 return ERROR_INVALID_PARAMETER;
345 pa_threaded_mainloop_lock(pulse->mainloop);
346 pa_stream_disconnect(pulse->stream);
347 pa_stream_unref(pulse->stream);
348 pulse->stream = NULL;
349 pa_threaded_mainloop_unlock(pulse->mainloop);
352 pulse->receive = NULL;
353 pulse->user_data = NULL;
354 return CHANNEL_RC_OK;
362static UINT audin_pulse_open(IAudinDevice* device, AudinReceive receive,
void* user_data)
364 pa_stream_state_t state = PA_STREAM_FAILED;
365 pa_buffer_attr buffer_attr = { 0 };
366 AudinPulseDevice* pulse = (AudinPulseDevice*)device;
368 if (!pulse || !receive || !user_data)
369 return ERROR_INVALID_PARAMETER;
372 return ERROR_INVALID_PARAMETER;
374 if (!pulse->sample_spec.rate || pulse->stream)
375 return ERROR_INVALID_PARAMETER;
377 pulse->receive = receive;
378 pulse->user_data = user_data;
379 pa_threaded_mainloop_lock(pulse->mainloop);
380 pulse->stream = pa_stream_new(pulse->context,
"freerdp_audin", &pulse->sample_spec, NULL);
384 pa_threaded_mainloop_unlock(pulse->mainloop);
385 WLog_Print(pulse->log, WLOG_DEBUG,
"pa_stream_new failed (%d)",
386 pa_context_errno(pulse->context));
387 const int rc = pa_context_errno(pulse->context);
391 pulse->bytes_per_frame = pa_frame_size(&pulse->sample_spec);
392 pa_stream_set_state_callback(pulse->stream, audin_pulse_stream_state_callback, pulse);
393 pa_stream_set_read_callback(pulse->stream, audin_pulse_stream_request_callback, pulse);
394 buffer_attr.maxlength = (UINT32)-1;
395 buffer_attr.tlength = (UINT32)-1;
396 buffer_attr.prebuf = (UINT32)-1;
397 buffer_attr.minreq = (UINT32)-1;
399 const size_t frag = pulse->bytes_per_frame * pulse->frames_per_packet;
400 WINPR_ASSERT(frag <= UINT32_MAX);
401 buffer_attr.fragsize = (uint32_t)frag;
403 if (buffer_attr.fragsize % pulse->format.nBlockAlign)
404 buffer_attr.fragsize +=
405 pulse->format.nBlockAlign - buffer_attr.fragsize % pulse->format.nBlockAlign;
407 if (pa_stream_connect_record(pulse->stream, pulse->device_name, &buffer_attr,
408 PA_STREAM_ADJUST_LATENCY) < 0)
410 pa_threaded_mainloop_unlock(pulse->mainloop);
411 WLog_Print(pulse->log, WLOG_ERROR,
"pa_stream_connect_playback failed (%d)",
412 pa_context_errno(pulse->context));
413 const int rc = pa_context_errno(pulse->context);
417 while (pulse->stream)
419 state = pa_stream_get_state(pulse->stream);
421 if (state == PA_STREAM_READY)
424 if (!PA_STREAM_IS_GOOD(state))
426 audin_pulse_close(device);
427 WLog_Print(pulse->log, WLOG_ERROR,
"bad stream state (%s: %d)",
428 pulse_stream_state_string(state), pa_context_errno(pulse->context));
429 pa_threaded_mainloop_unlock(pulse->mainloop);
430 const int rc = pa_context_errno(pulse->context);
434 pa_threaded_mainloop_wait(pulse->mainloop);
437 pa_threaded_mainloop_unlock(pulse->mainloop);
438 pulse->buffer_frames = 0;
439 WLog_Print(pulse->log, WLOG_DEBUG,
"connected");
440 return CHANNEL_RC_OK;
448static UINT audin_pulse_parse_addin_args(AudinPulseDevice* device,
const ADDIN_ARGV* args)
453 AudinPulseDevice* pulse = device;
455 NULL, NULL, -1, NULL,
"audio device name" },
456 { NULL, 0, NULL, NULL, NULL, -1, NULL, NULL } };
459 COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
460 status = CommandLineParseArgumentsA(args->argc, args->argv, audin_pulse_args, flags, pulse,
464 return ERROR_INVALID_PARAMETER;
466 arg = audin_pulse_args;
470 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
473 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg,
"dev")
475 pulse->device_name = _strdup(arg->Value);
477 if (!pulse->device_name)
479 WLog_Print(pulse->log, WLOG_ERROR,
"_strdup failed!");
480 return CHANNEL_RC_NO_MEMORY;
483 CommandLineSwitchEnd(arg)
484 }
while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
486 return CHANNEL_RC_OK;
494FREERDP_ENTRY_POINT(UINT VCAPITYPE pulse_freerdp_audin_client_subsystem_entry(
498 AudinPulseDevice* pulse = NULL;
500 pulse = (AudinPulseDevice*)calloc(1,
sizeof(AudinPulseDevice));
504 WLog_ERR(TAG,
"calloc failed!");
505 return CHANNEL_RC_NO_MEMORY;
508 pulse->log = WLog_Get(TAG);
509 pulse->iface.Open = audin_pulse_open;
510 pulse->iface.FormatSupported = audin_pulse_format_supported;
511 pulse->iface.SetFormat = audin_pulse_set_format;
512 pulse->iface.Close = audin_pulse_close;
513 pulse->iface.Free = audin_pulse_free;
514 pulse->rdpcontext = pEntryPoints->rdpcontext;
515 args = pEntryPoints->args;
517 if ((error = audin_pulse_parse_addin_args(pulse, args)))
519 WLog_Print(pulse->log, WLOG_ERROR,
520 "audin_pulse_parse_addin_args failed with error %" PRIu32
"!", error);
524 pulse->mainloop = pa_threaded_mainloop_new();
526 if (!pulse->mainloop)
528 WLog_Print(pulse->log, WLOG_ERROR,
"pa_threaded_mainloop_new failed");
529 error = CHANNEL_RC_NO_MEMORY;
533 pulse->context = pa_context_new(pa_threaded_mainloop_get_api(pulse->mainloop),
"freerdp");
537 WLog_Print(pulse->log, WLOG_ERROR,
"pa_context_new failed");
538 error = CHANNEL_RC_NO_MEMORY;
542 pa_context_set_state_callback(pulse->context, audin_pulse_context_state_callback, pulse);
544 if ((error = audin_pulse_connect(&pulse->iface)))
546 WLog_Print(pulse->log, WLOG_ERROR,
"audin_pulse_connect failed");
550 if ((error = pEntryPoints->pRegisterAudinDevice(pEntryPoints->plugin, &pulse->iface)))
552 WLog_Print(pulse->log, WLOG_ERROR,
"RegisterAudinDevice failed with error %" PRIu32
"!",
557 return CHANNEL_RC_OK;
559 audin_pulse_free(&pulse->iface);