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"
50 UINT32 frames_per_packet;
51 pa_threaded_mainloop* mainloop;
53 pa_sample_spec sample_spec;
57 size_t bytes_per_frame;
63 rdpContext* rdpcontext;
67static const char* pulse_context_state_string(pa_context_state_t state)
71 case PA_CONTEXT_UNCONNECTED:
72 return "PA_CONTEXT_UNCONNECTED";
73 case PA_CONTEXT_CONNECTING:
74 return "PA_CONTEXT_CONNECTING";
75 case PA_CONTEXT_AUTHORIZING:
76 return "PA_CONTEXT_AUTHORIZING";
77 case PA_CONTEXT_SETTING_NAME:
78 return "PA_CONTEXT_SETTING_NAME";
79 case PA_CONTEXT_READY:
80 return "PA_CONTEXT_READY";
81 case PA_CONTEXT_FAILED:
82 return "PA_CONTEXT_FAILED";
83 case PA_CONTEXT_TERMINATED:
84 return "PA_CONTEXT_TERMINATED";
90static const char* pulse_stream_state_string(pa_stream_state_t state)
94 case PA_STREAM_UNCONNECTED:
95 return "PA_STREAM_UNCONNECTED";
96 case PA_STREAM_CREATING:
97 return "PA_STREAM_CREATING";
99 return "PA_STREAM_READY";
100 case PA_STREAM_FAILED:
101 return "PA_STREAM_FAILED";
102 case PA_STREAM_TERMINATED:
103 return "PA_STREAM_TERMINATED";
109static void audin_pulse_context_state_callback(pa_context* context,
void* userdata)
111 AudinPulseDevice* pulse = (AudinPulseDevice*)userdata;
112 pa_context_state_t state = pa_context_get_state(context);
114 WLog_Print(pulse->log, WLOG_DEBUG,
"context state %s", pulse_context_state_string(state));
117 case PA_CONTEXT_READY:
118 case PA_CONTEXT_FAILED:
119 case PA_CONTEXT_TERMINATED:
120 pa_threaded_mainloop_signal(pulse->mainloop, 0);
133static UINT audin_pulse_connect(IAudinDevice* device)
135 pa_context_state_t state = PA_CONTEXT_FAILED;
136 AudinPulseDevice* pulse = (AudinPulseDevice*)device;
139 return ERROR_INVALID_PARAMETER;
141 if (pa_context_connect(pulse->context, NULL, 0, NULL))
143 WLog_Print(pulse->log, WLOG_ERROR,
"pa_context_connect failed (%d)",
144 pa_context_errno(pulse->context));
145 return ERROR_INTERNAL_ERROR;
148 pa_threaded_mainloop_lock(pulse->mainloop);
150 if (pa_threaded_mainloop_start(pulse->mainloop) < 0)
152 pa_threaded_mainloop_unlock(pulse->mainloop);
153 WLog_Print(pulse->log, WLOG_ERROR,
"pa_threaded_mainloop_start failed (%d)",
154 pa_context_errno(pulse->context));
155 return ERROR_INTERNAL_ERROR;
160 state = pa_context_get_state(pulse->context);
162 if (state == PA_CONTEXT_READY)
165 if (!PA_CONTEXT_IS_GOOD(state))
167 WLog_Print(pulse->log, WLOG_ERROR,
"bad context state (%s: %d)",
168 pulse_context_state_string(state), pa_context_errno(pulse->context));
169 pa_context_disconnect(pulse->context);
170 return ERROR_INVALID_STATE;
173 pa_threaded_mainloop_wait(pulse->mainloop);
176 pa_threaded_mainloop_unlock(pulse->mainloop);
177 WLog_Print(pulse->log, WLOG_DEBUG,
"connected");
178 return CHANNEL_RC_OK;
186static UINT audin_pulse_free(IAudinDevice* device)
188 AudinPulseDevice* pulse = (AudinPulseDevice*)device;
191 return ERROR_INVALID_PARAMETER;
195 pa_threaded_mainloop_stop(pulse->mainloop);
200 pa_context_disconnect(pulse->context);
201 pa_context_unref(pulse->context);
202 pulse->context = NULL;
207 pa_threaded_mainloop_free(pulse->mainloop);
208 pulse->mainloop = NULL;
211 free(pulse->client_name);
212 free(pulse->stream_name);
214 return CHANNEL_RC_OK;
217static BOOL audin_pulse_format_supported(IAudinDevice* device,
const AUDIO_FORMAT* format)
219 AudinPulseDevice* pulse = (AudinPulseDevice*)device;
221 if (!pulse || !format)
227 switch (format->wFormatTag)
229 case WAVE_FORMAT_PCM:
230 if (format->cbSize == 0 && (format->nSamplesPerSec <= PA_RATE_MAX) &&
231 (format->wBitsPerSample == 8 || format->wBitsPerSample == 16) &&
232 (format->nChannels >= 1 && format->nChannels <= PA_CHANNELS_MAX))
251static UINT audin_pulse_set_format(IAudinDevice* device,
const AUDIO_FORMAT* format,
252 UINT32 FramesPerPacket)
254 pa_sample_spec sample_spec = { 0 };
255 AudinPulseDevice* pulse = (AudinPulseDevice*)device;
257 if (!pulse || !format)
258 return ERROR_INVALID_PARAMETER;
261 return ERROR_INVALID_PARAMETER;
263 if (FramesPerPacket > 0)
264 pulse->frames_per_packet = FramesPerPacket;
266 sample_spec.rate = format->nSamplesPerSec;
268 sample_spec.channels = WINPR_ASSERTING_INT_CAST(uint8_t, format->nChannels);
270 switch (format->wFormatTag)
272 case WAVE_FORMAT_PCM:
273 switch (format->wBitsPerSample)
276 sample_spec.format = PA_SAMPLE_U8;
280 sample_spec.format = PA_SAMPLE_S16LE;
284 return ERROR_INTERNAL_ERROR;
290 return ERROR_INTERNAL_ERROR;
293 pulse->sample_spec = sample_spec;
294 pulse->format = *format;
295 return CHANNEL_RC_OK;
298static void audin_pulse_stream_state_callback(pa_stream* stream,
void* userdata)
300 AudinPulseDevice* pulse = (AudinPulseDevice*)userdata;
303 pa_stream_state_t state = pa_stream_get_state(stream);
305 WLog_Print(pulse->log, WLOG_DEBUG,
"stream state %s", pulse_stream_state_string(state));
308 case PA_STREAM_READY:
309 case PA_STREAM_FAILED:
310 case PA_STREAM_TERMINATED:
311 pa_threaded_mainloop_signal(pulse->mainloop, 0);
314 case PA_STREAM_UNCONNECTED:
315 case PA_STREAM_CREATING:
321static void audin_pulse_stream_request_callback(pa_stream* stream,
size_t length,
void* userdata)
323 const void* data = NULL;
324 AudinPulseDevice* pulse = (AudinPulseDevice*)userdata;
325 UINT error = CHANNEL_RC_OK;
326 pa_stream_peek(stream, &data, &length);
328 IFCALLRESULT(CHANNEL_RC_OK, pulse->receive, &pulse->format, data, length, pulse->user_data);
329 pa_stream_drop(stream);
331 if (error && pulse->rdpcontext)
332 setChannelError(pulse->rdpcontext, error,
"audin_pulse_thread_func reported an error");
340static UINT audin_pulse_close(IAudinDevice* device)
342 AudinPulseDevice* pulse = (AudinPulseDevice*)device;
345 return ERROR_INVALID_PARAMETER;
349 pa_threaded_mainloop_lock(pulse->mainloop);
350 pa_stream_disconnect(pulse->stream);
351 pa_stream_unref(pulse->stream);
352 pulse->stream = NULL;
353 pa_threaded_mainloop_unlock(pulse->mainloop);
356 pulse->receive = NULL;
357 pulse->user_data = NULL;
358 return CHANNEL_RC_OK;
366static UINT audin_pulse_open(IAudinDevice* device, AudinReceive receive,
void* user_data)
368 pa_stream_state_t state = PA_STREAM_FAILED;
369 pa_buffer_attr buffer_attr = { 0 };
370 AudinPulseDevice* pulse = (AudinPulseDevice*)device;
372 if (!pulse || !receive || !user_data)
373 return ERROR_INVALID_PARAMETER;
376 return ERROR_INVALID_PARAMETER;
378 if (!pulse->sample_spec.rate || pulse->stream)
379 return ERROR_INVALID_PARAMETER;
381 pulse->receive = receive;
382 pulse->user_data = user_data;
383 pa_threaded_mainloop_lock(pulse->mainloop);
384 pulse->stream = pa_stream_new(pulse->context, pulse->stream_name, &pulse->sample_spec, NULL);
388 pa_threaded_mainloop_unlock(pulse->mainloop);
389 WLog_Print(pulse->log, WLOG_DEBUG,
"pa_stream_new failed (%d)",
390 pa_context_errno(pulse->context));
391 const int rc = pa_context_errno(pulse->context);
395 pulse->bytes_per_frame = pa_frame_size(&pulse->sample_spec);
396 pa_stream_set_state_callback(pulse->stream, audin_pulse_stream_state_callback, pulse);
397 pa_stream_set_read_callback(pulse->stream, audin_pulse_stream_request_callback, pulse);
398 buffer_attr.maxlength = (UINT32)-1;
399 buffer_attr.tlength = (UINT32)-1;
400 buffer_attr.prebuf = (UINT32)-1;
401 buffer_attr.minreq = (UINT32)-1;
403 const size_t frag = pulse->bytes_per_frame * pulse->frames_per_packet;
404 WINPR_ASSERT(frag <= UINT32_MAX);
405 buffer_attr.fragsize = (uint32_t)frag;
407 if (buffer_attr.fragsize % pulse->format.nBlockAlign)
408 buffer_attr.fragsize +=
409 pulse->format.nBlockAlign - buffer_attr.fragsize % pulse->format.nBlockAlign;
411 if (pa_stream_connect_record(pulse->stream, pulse->device_name, &buffer_attr,
412 PA_STREAM_ADJUST_LATENCY) < 0)
414 pa_threaded_mainloop_unlock(pulse->mainloop);
415 WLog_Print(pulse->log, WLOG_ERROR,
"pa_stream_connect_playback failed (%d)",
416 pa_context_errno(pulse->context));
417 const int rc = pa_context_errno(pulse->context);
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))
430 audin_pulse_close(device);
431 WLog_Print(pulse->log, WLOG_ERROR,
"bad stream state (%s: %d)",
432 pulse_stream_state_string(state), pa_context_errno(pulse->context));
433 pa_threaded_mainloop_unlock(pulse->mainloop);
434 const int rc = pa_context_errno(pulse->context);
438 pa_threaded_mainloop_wait(pulse->mainloop);
441 pa_threaded_mainloop_unlock(pulse->mainloop);
442 pulse->buffer_frames = 0;
443 WLog_Print(pulse->log, WLOG_DEBUG,
"connected");
444 return CHANNEL_RC_OK;
452static UINT audin_pulse_parse_addin_args(AudinPulseDevice* pulse,
const ADDIN_ARGV* args)
455 {
"dev", COMMAND_LINE_VALUE_REQUIRED,
"<device>", NULL, NULL, -1, NULL,
456 "audio device name" },
457 {
"client_name", COMMAND_LINE_VALUE_REQUIRED,
"<client_name>", NULL, NULL, -1, NULL,
458 "name of pulse client" },
459 {
"stream_name", COMMAND_LINE_VALUE_REQUIRED,
"<stream_name>", NULL, NULL, -1, NULL,
460 "name of pulse stream" },
461 { NULL, 0, NULL, NULL, NULL, -1, NULL, NULL }
465 COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
466 const int status = CommandLineParseArgumentsA(args->argc, args->argv, audin_pulse_args, flags,
470 return ERROR_INVALID_PARAMETER;
474 const char* client_name = NULL;
475 const char* stream_name = NULL;
478 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
481 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg,
"dev")
483 pulse->device_name = _strdup(arg->Value);
485 if (!pulse->device_name)
487 WLog_Print(pulse->log, WLOG_ERROR,
"_strdup failed!");
488 return CHANNEL_RC_NO_MEMORY;
491 CommandLineSwitchEnd(arg)
492 }
while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
495 client_name =
"freerdp";
497 stream_name =
"freerdp_audin";
499 pulse->client_name = _strdup(client_name);
500 pulse->stream_name = _strdup(stream_name);
501 if (!pulse->client_name || !pulse->stream_name)
502 return ERROR_OUTOFMEMORY;
504 return CHANNEL_RC_OK;
512FREERDP_ENTRY_POINT(UINT VCAPITYPE pulse_freerdp_audin_client_subsystem_entry(
516 AudinPulseDevice* pulse = NULL;
518 pulse = (AudinPulseDevice*)calloc(1,
sizeof(AudinPulseDevice));
522 WLog_ERR(TAG,
"calloc failed!");
523 return CHANNEL_RC_NO_MEMORY;
526 pulse->log = WLog_Get(TAG);
527 pulse->iface.Open = audin_pulse_open;
528 pulse->iface.FormatSupported = audin_pulse_format_supported;
529 pulse->iface.SetFormat = audin_pulse_set_format;
530 pulse->iface.Close = audin_pulse_close;
531 pulse->iface.Free = audin_pulse_free;
532 pulse->rdpcontext = pEntryPoints->rdpcontext;
533 args = pEntryPoints->args;
535 if ((error = audin_pulse_parse_addin_args(pulse, args)))
537 WLog_Print(pulse->log, WLOG_ERROR,
538 "audin_pulse_parse_addin_args failed with error %" PRIu32
"!", error);
542 pulse->mainloop = pa_threaded_mainloop_new();
544 if (!pulse->mainloop)
546 WLog_Print(pulse->log, WLOG_ERROR,
"pa_threaded_mainloop_new failed");
547 error = CHANNEL_RC_NO_MEMORY;
552 pa_context_new(pa_threaded_mainloop_get_api(pulse->mainloop), pulse->client_name);
556 WLog_Print(pulse->log, WLOG_ERROR,
"pa_context_new failed");
557 error = CHANNEL_RC_NO_MEMORY;
561 pa_context_set_state_callback(pulse->context, audin_pulse_context_state_callback, pulse);
563 if ((error = audin_pulse_connect(&pulse->iface)))
565 WLog_Print(pulse->log, WLOG_ERROR,
"audin_pulse_connect failed");
569 if ((error = pEntryPoints->pRegisterAudinDevice(pEntryPoints->plugin, &pulse->iface)))
571 WLog_Print(pulse->log, WLOG_ERROR,
"RegisterAudinDevice failed with error %" PRIu32
"!",
576 return CHANNEL_RC_OK;
578 audin_pulse_free(&pulse->iface);