22 #include <freerdp/config.h>
28 #include <winpr/crt.h>
29 #include <winpr/cmdline.h>
30 #include <winpr/wlog.h>
32 #include <pulse/pulseaudio.h>
34 #include <freerdp/types.h>
35 #include <freerdp/addin.h>
36 #include <freerdp/freerdp.h>
37 #include <freerdp/codec/audio.h>
38 #include <freerdp/client/audin.h>
40 #include "audin_main.h"
47 UINT32 frames_per_packet;
48 pa_threaded_mainloop* mainloop;
50 pa_sample_spec sample_spec;
54 size_t bytes_per_frame;
60 rdpContext* rdpcontext;
64 static const char* pulse_context_state_string(pa_context_state_t state)
68 case PA_CONTEXT_UNCONNECTED:
69 return "PA_CONTEXT_UNCONNECTED";
70 case PA_CONTEXT_CONNECTING:
71 return "PA_CONTEXT_CONNECTING";
72 case PA_CONTEXT_AUTHORIZING:
73 return "PA_CONTEXT_AUTHORIZING";
74 case PA_CONTEXT_SETTING_NAME:
75 return "PA_CONTEXT_SETTING_NAME";
76 case PA_CONTEXT_READY:
77 return "PA_CONTEXT_READY";
78 case PA_CONTEXT_FAILED:
79 return "PA_CONTEXT_FAILED";
80 case PA_CONTEXT_TERMINATED:
81 return "PA_CONTEXT_TERMINATED";
87 static const char* pulse_stream_state_string(pa_stream_state_t state)
91 case PA_STREAM_UNCONNECTED:
92 return "PA_STREAM_UNCONNECTED";
93 case PA_STREAM_CREATING:
94 return "PA_STREAM_CREATING";
96 return "PA_STREAM_READY";
97 case PA_STREAM_FAILED:
98 return "PA_STREAM_FAILED";
99 case PA_STREAM_TERMINATED:
100 return "PA_STREAM_TERMINATED";
106 static void audin_pulse_context_state_callback(pa_context* context,
void* userdata)
108 AudinPulseDevice* pulse = (AudinPulseDevice*)userdata;
109 pa_context_state_t state = pa_context_get_state(context);
111 WLog_Print(pulse->log, WLOG_DEBUG,
"context state %s", pulse_context_state_string(state));
114 case PA_CONTEXT_READY:
115 case PA_CONTEXT_FAILED:
116 case PA_CONTEXT_TERMINATED:
117 pa_threaded_mainloop_signal(pulse->mainloop, 0);
130 static UINT audin_pulse_connect(IAudinDevice* device)
132 pa_context_state_t state = PA_CONTEXT_FAILED;
133 AudinPulseDevice* pulse = (AudinPulseDevice*)device;
136 return ERROR_INVALID_PARAMETER;
138 if (pa_context_connect(pulse->context, NULL, 0, NULL))
140 WLog_Print(pulse->log, WLOG_ERROR,
"pa_context_connect failed (%d)",
141 pa_context_errno(pulse->context));
142 return ERROR_INTERNAL_ERROR;
145 pa_threaded_mainloop_lock(pulse->mainloop);
147 if (pa_threaded_mainloop_start(pulse->mainloop) < 0)
149 pa_threaded_mainloop_unlock(pulse->mainloop);
150 WLog_Print(pulse->log, WLOG_ERROR,
"pa_threaded_mainloop_start failed (%d)",
151 pa_context_errno(pulse->context));
152 return ERROR_INTERNAL_ERROR;
157 state = pa_context_get_state(pulse->context);
159 if (state == PA_CONTEXT_READY)
162 if (!PA_CONTEXT_IS_GOOD(state))
164 WLog_Print(pulse->log, WLOG_ERROR,
"bad context state (%s: %d)",
165 pulse_context_state_string(state), pa_context_errno(pulse->context));
166 pa_context_disconnect(pulse->context);
167 return ERROR_INVALID_STATE;
170 pa_threaded_mainloop_wait(pulse->mainloop);
173 pa_threaded_mainloop_unlock(pulse->mainloop);
174 WLog_Print(pulse->log, WLOG_DEBUG,
"connected");
175 return CHANNEL_RC_OK;
183 static UINT audin_pulse_free(IAudinDevice* device)
185 AudinPulseDevice* pulse = (AudinPulseDevice*)device;
188 return ERROR_INVALID_PARAMETER;
192 pa_threaded_mainloop_stop(pulse->mainloop);
197 pa_context_disconnect(pulse->context);
198 pa_context_unref(pulse->context);
199 pulse->context = NULL;
204 pa_threaded_mainloop_free(pulse->mainloop);
205 pulse->mainloop = NULL;
209 return CHANNEL_RC_OK;
212 static BOOL audin_pulse_format_supported(IAudinDevice* device,
const AUDIO_FORMAT* format)
214 AudinPulseDevice* pulse = (AudinPulseDevice*)device;
216 if (!pulse || !format)
222 switch (format->wFormatTag)
224 case WAVE_FORMAT_PCM:
225 if (format->cbSize == 0 && (format->nSamplesPerSec <= PA_RATE_MAX) &&
226 (format->wBitsPerSample == 8 || format->wBitsPerSample == 16) &&
227 (format->nChannels >= 1 && format->nChannels <= PA_CHANNELS_MAX))
246 static UINT audin_pulse_set_format(IAudinDevice* device,
const AUDIO_FORMAT* format,
247 UINT32 FramesPerPacket)
249 pa_sample_spec sample_spec = { 0 };
250 AudinPulseDevice* pulse = (AudinPulseDevice*)device;
252 if (!pulse || !format)
253 return ERROR_INVALID_PARAMETER;
256 return ERROR_INVALID_PARAMETER;
258 if (FramesPerPacket > 0)
259 pulse->frames_per_packet = FramesPerPacket;
261 sample_spec.rate = format->nSamplesPerSec;
262 sample_spec.channels = format->nChannels;
264 switch (format->wFormatTag)
266 case WAVE_FORMAT_PCM:
267 switch (format->wBitsPerSample)
270 sample_spec.format = PA_SAMPLE_U8;
274 sample_spec.format = PA_SAMPLE_S16LE;
278 return ERROR_INTERNAL_ERROR;
284 return ERROR_INTERNAL_ERROR;
287 pulse->sample_spec = sample_spec;
288 pulse->format = *format;
289 return CHANNEL_RC_OK;
292 static void audin_pulse_stream_state_callback(pa_stream* stream,
void* userdata)
294 AudinPulseDevice* pulse = (AudinPulseDevice*)userdata;
297 pa_stream_state_t state = pa_stream_get_state(stream);
299 WLog_Print(pulse->log, WLOG_DEBUG,
"stream state %s", pulse_stream_state_string(state));
302 case PA_STREAM_READY:
303 case PA_STREAM_FAILED:
304 case PA_STREAM_TERMINATED:
305 pa_threaded_mainloop_signal(pulse->mainloop, 0);
308 case PA_STREAM_UNCONNECTED:
309 case PA_STREAM_CREATING:
315 static void audin_pulse_stream_request_callback(pa_stream* stream,
size_t length,
void* userdata)
317 const void* data = NULL;
318 AudinPulseDevice* pulse = (AudinPulseDevice*)userdata;
319 UINT error = CHANNEL_RC_OK;
320 pa_stream_peek(stream, &data, &length);
322 IFCALLRESULT(CHANNEL_RC_OK, pulse->receive, &pulse->format, data, length, pulse->user_data);
323 pa_stream_drop(stream);
325 if (error && pulse->rdpcontext)
326 setChannelError(pulse->rdpcontext, error,
"audin_pulse_thread_func reported an error");
334 static UINT audin_pulse_close(IAudinDevice* device)
336 AudinPulseDevice* pulse = (AudinPulseDevice*)device;
339 return ERROR_INVALID_PARAMETER;
343 pa_threaded_mainloop_lock(pulse->mainloop);
344 pa_stream_disconnect(pulse->stream);
345 pa_stream_unref(pulse->stream);
346 pulse->stream = NULL;
347 pa_threaded_mainloop_unlock(pulse->mainloop);
350 pulse->receive = NULL;
351 pulse->user_data = NULL;
352 return CHANNEL_RC_OK;
360 static UINT audin_pulse_open(IAudinDevice* device, AudinReceive receive,
void* user_data)
362 pa_stream_state_t state = PA_STREAM_FAILED;
363 pa_buffer_attr buffer_attr = { 0 };
364 AudinPulseDevice* pulse = (AudinPulseDevice*)device;
366 if (!pulse || !receive || !user_data)
367 return ERROR_INVALID_PARAMETER;
370 return ERROR_INVALID_PARAMETER;
372 if (!pulse->sample_spec.rate || pulse->stream)
373 return ERROR_INVALID_PARAMETER;
375 pulse->receive = receive;
376 pulse->user_data = user_data;
377 pa_threaded_mainloop_lock(pulse->mainloop);
378 pulse->stream = pa_stream_new(pulse->context,
"freerdp_audin", &pulse->sample_spec, NULL);
382 pa_threaded_mainloop_unlock(pulse->mainloop);
383 WLog_Print(pulse->log, WLOG_DEBUG,
"pa_stream_new failed (%d)",
384 pa_context_errno(pulse->context));
385 return pa_context_errno(pulse->context);
388 pulse->bytes_per_frame = pa_frame_size(&pulse->sample_spec);
389 pa_stream_set_state_callback(pulse->stream, audin_pulse_stream_state_callback, pulse);
390 pa_stream_set_read_callback(pulse->stream, audin_pulse_stream_request_callback, pulse);
391 buffer_attr.maxlength = (UINT32)-1;
392 buffer_attr.tlength = (UINT32)-1;
393 buffer_attr.prebuf = (UINT32)-1;
394 buffer_attr.minreq = (UINT32)-1;
396 const size_t frag = pulse->bytes_per_frame * pulse->frames_per_packet;
397 WINPR_ASSERT(frag <= UINT32_MAX);
398 buffer_attr.fragsize = (uint32_t)frag;
400 if (buffer_attr.fragsize % pulse->format.nBlockAlign)
401 buffer_attr.fragsize +=
402 pulse->format.nBlockAlign - buffer_attr.fragsize % pulse->format.nBlockAlign;
404 if (pa_stream_connect_record(pulse->stream, pulse->device_name, &buffer_attr,
405 PA_STREAM_ADJUST_LATENCY) < 0)
407 pa_threaded_mainloop_unlock(pulse->mainloop);
408 WLog_Print(pulse->log, WLOG_ERROR,
"pa_stream_connect_playback failed (%d)",
409 pa_context_errno(pulse->context));
410 return pa_context_errno(pulse->context);
413 while (pulse->stream)
415 state = pa_stream_get_state(pulse->stream);
417 if (state == PA_STREAM_READY)
420 if (!PA_STREAM_IS_GOOD(state))
422 audin_pulse_close(device);
423 WLog_Print(pulse->log, WLOG_ERROR,
"bad stream state (%s: %d)",
424 pulse_stream_state_string(state), pa_context_errno(pulse->context));
425 pa_threaded_mainloop_unlock(pulse->mainloop);
426 return pa_context_errno(pulse->context);
429 pa_threaded_mainloop_wait(pulse->mainloop);
432 pa_threaded_mainloop_unlock(pulse->mainloop);
433 pulse->buffer_frames = 0;
434 WLog_Print(pulse->log, WLOG_DEBUG,
"connected");
435 return CHANNEL_RC_OK;
443 static UINT audin_pulse_parse_addin_args(AudinPulseDevice* device,
const ADDIN_ARGV* args)
448 AudinPulseDevice* pulse = device;
450 NULL, NULL, -1, NULL,
"audio device name" },
451 { NULL, 0, NULL, NULL, NULL, -1, NULL, NULL } };
454 COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
455 status = CommandLineParseArgumentsA(args->argc, args->argv, audin_pulse_args, flags, pulse,
459 return ERROR_INVALID_PARAMETER;
461 arg = audin_pulse_args;
465 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
468 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg,
"dev")
470 pulse->device_name = _strdup(arg->Value);
472 if (!pulse->device_name)
474 WLog_Print(pulse->log, WLOG_ERROR,
"_strdup failed!");
475 return CHANNEL_RC_NO_MEMORY;
478 CommandLineSwitchEnd(arg)
479 }
while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
481 return CHANNEL_RC_OK;
489 FREERDP_ENTRY_POINT(UINT VCAPITYPE pulse_freerdp_audin_client_subsystem_entry(
493 AudinPulseDevice* pulse = NULL;
495 pulse = (AudinPulseDevice*)calloc(1,
sizeof(AudinPulseDevice));
499 WLog_ERR(TAG,
"calloc failed!");
500 return CHANNEL_RC_NO_MEMORY;
503 pulse->log = WLog_Get(TAG);
504 pulse->iface.Open = audin_pulse_open;
505 pulse->iface.FormatSupported = audin_pulse_format_supported;
506 pulse->iface.SetFormat = audin_pulse_set_format;
507 pulse->iface.Close = audin_pulse_close;
508 pulse->iface.Free = audin_pulse_free;
509 pulse->rdpcontext = pEntryPoints->rdpcontext;
510 args = pEntryPoints->args;
512 if ((error = audin_pulse_parse_addin_args(pulse, args)))
514 WLog_Print(pulse->log, WLOG_ERROR,
515 "audin_pulse_parse_addin_args failed with error %" PRIu32
"!", error);
519 pulse->mainloop = pa_threaded_mainloop_new();
521 if (!pulse->mainloop)
523 WLog_Print(pulse->log, WLOG_ERROR,
"pa_threaded_mainloop_new failed");
524 error = CHANNEL_RC_NO_MEMORY;
528 pulse->context = pa_context_new(pa_threaded_mainloop_get_api(pulse->mainloop),
"freerdp");
532 WLog_Print(pulse->log, WLOG_ERROR,
"pa_context_new failed");
533 error = CHANNEL_RC_NO_MEMORY;
537 pa_context_set_state_callback(pulse->context, audin_pulse_context_state_callback, pulse);
539 if ((error = audin_pulse_connect(&pulse->iface)))
541 WLog_Print(pulse->log, WLOG_ERROR,
"audin_pulse_connect failed");
545 if ((error = pEntryPoints->pRegisterAudinDevice(pEntryPoints->plugin, &pulse->iface)))
547 WLog_Print(pulse->log, WLOG_ERROR,
"RegisterAudinDevice failed with error %" PRIu32
"!",
552 return CHANNEL_RC_OK;
554 audin_pulse_free(&pulse->iface);