FreeRDP
Loading...
Searching...
No Matches
audin_pulse.c
1
22#include <freerdp/config.h>
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27
28#include <winpr/crt.h>
29#include <winpr/cmdline.h>
30#include <winpr/wlog.h>
31#include <winpr/cast.h>
32
33#include <pulse/pulseaudio.h>
34
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>
41
42#include "audin_main.h"
43
44typedef struct
45{
46 IAudinDevice iface;
47
48 char* device_name;
49 char* client_name;
50 char* stream_name;
51 UINT32 frames_per_packet;
52 pa_threaded_mainloop* mainloop;
53 pa_context* context;
54 pa_sample_spec sample_spec;
55 pa_stream* stream;
56 AUDIO_FORMAT format;
57
58 size_t bytes_per_frame;
59 size_t buffer_frames;
60
61 AudinReceive receive;
62 void* user_data;
63
64 rdpContext* rdpcontext;
65 wLog* log;
66} AudinPulseDevice;
67
68static const char* pulse_context_state_string(pa_context_state_t state)
69{
70 switch (state)
71 {
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";
86 default:
87 return "UNKNOWN";
88 }
89}
90
91static const char* pulse_stream_state_string(pa_stream_state_t state)
92{
93 switch (state)
94 {
95 case PA_STREAM_UNCONNECTED:
96 return "PA_STREAM_UNCONNECTED";
97 case PA_STREAM_CREATING:
98 return "PA_STREAM_CREATING";
99 case PA_STREAM_READY:
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";
105 default:
106 return "UNKNOWN";
107 }
108}
109
110static void audin_pulse_context_state_callback(pa_context* context, void* userdata)
111{
112 AudinPulseDevice* pulse = (AudinPulseDevice*)userdata;
113 pa_context_state_t state = pa_context_get_state(context);
114
115 WLog_Print(pulse->log, WLOG_DEBUG, "context state %s", pulse_context_state_string(state));
116 switch (state)
117 {
118 case PA_CONTEXT_READY:
119 case PA_CONTEXT_FAILED:
120 case PA_CONTEXT_TERMINATED:
121 pa_threaded_mainloop_signal(pulse->mainloop, 0);
122 break;
123
124 default:
125 break;
126 }
127}
128
134static UINT audin_pulse_connect(IAudinDevice* device)
135{
136 pa_context_state_t state = PA_CONTEXT_FAILED;
137 AudinPulseDevice* pulse = (AudinPulseDevice*)device;
138
139 if (!pulse->context)
140 return ERROR_INVALID_PARAMETER;
141
142 if (pa_context_connect(pulse->context, NULL, PA_CONTEXT_NOFLAGS, NULL))
143 {
144 WLog_Print(pulse->log, WLOG_ERROR, "pa_context_connect failed (%d)",
145 pa_context_errno(pulse->context));
146 return ERROR_INTERNAL_ERROR;
147 }
148
149 pa_threaded_mainloop_lock(pulse->mainloop);
150
151 if (pa_threaded_mainloop_start(pulse->mainloop) < 0)
152 {
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;
157 }
158
159 for (;;)
160 {
161 state = pa_context_get_state(pulse->context);
162
163 if (state == PA_CONTEXT_READY)
164 break;
165
166 if (!PA_CONTEXT_IS_GOOD(state))
167 {
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;
172 }
173
174 pa_threaded_mainloop_wait(pulse->mainloop);
175 }
176
177 pa_threaded_mainloop_unlock(pulse->mainloop);
178 WLog_Print(pulse->log, WLOG_DEBUG, "connected");
179 return CHANNEL_RC_OK;
180}
181
187static UINT audin_pulse_free(IAudinDevice* device)
188{
189 AudinPulseDevice* pulse = (AudinPulseDevice*)device;
190
191 if (!pulse)
192 return ERROR_INVALID_PARAMETER;
193
194 if (pulse->mainloop)
195 {
196 pa_threaded_mainloop_stop(pulse->mainloop);
197 }
198
199 if (pulse->context)
200 {
201 pa_context_disconnect(pulse->context);
202 pa_context_unref(pulse->context);
203 pulse->context = NULL;
204 }
205
206 if (pulse->mainloop)
207 {
208 pa_threaded_mainloop_free(pulse->mainloop);
209 pulse->mainloop = NULL;
210 }
211
212 free(pulse->device_name);
213 free(pulse->client_name);
214 free(pulse->stream_name);
215 free(pulse);
216 return CHANNEL_RC_OK;
217}
218
219static BOOL audin_pulse_format_supported(IAudinDevice* device, const AUDIO_FORMAT* format)
220{
221 AudinPulseDevice* pulse = (AudinPulseDevice*)device;
222
223 if (!pulse || !format)
224 return FALSE;
225
226 if (!pulse->context)
227 return 0;
228
229 switch (format->wFormatTag)
230 {
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))
235 {
236 return TRUE;
237 }
238
239 break;
240
241 default:
242 return FALSE;
243 }
244
245 return FALSE;
246}
247
253static UINT audin_pulse_set_format(IAudinDevice* device, const AUDIO_FORMAT* format,
254 UINT32 FramesPerPacket)
255{
256 AudinPulseDevice* pulse = (AudinPulseDevice*)device;
257
258 if (!pulse || !format)
259 return ERROR_INVALID_PARAMETER;
260
261 if (!pulse->context)
262 return ERROR_INVALID_PARAMETER;
263
264 if (FramesPerPacket > 0)
265 pulse->frames_per_packet = FramesPerPacket;
266
267 pa_sample_format_t sformat = PA_SAMPLE_INVALID;
268 switch (format->wFormatTag)
269 {
270 case WAVE_FORMAT_PCM: /* PCM */
271 switch (format->wBitsPerSample)
272 {
273 case 8:
274 sformat = PA_SAMPLE_U8;
275 break;
276
277 case 16:
278 sformat = PA_SAMPLE_S16LE;
279 break;
280
281 default:
282 return ERROR_INTERNAL_ERROR;
283 }
284
285 break;
286
287 default:
288 return ERROR_INTERNAL_ERROR;
289 }
290
291 const pa_sample_spec sample_spec = {
292 .format = sformat,
293 .rate = format->nSamplesPerSec,
294 .channels = WINPR_ASSERTING_INT_CAST(uint8_t, format->nChannels),
295 };
296
297 pulse->sample_spec = sample_spec;
298 pulse->format = *format;
299 return CHANNEL_RC_OK;
300}
301
302static void audin_pulse_stream_state_callback(pa_stream* stream, void* userdata)
303{
304 AudinPulseDevice* pulse = (AudinPulseDevice*)userdata;
305 WINPR_ASSERT(pulse);
306
307 pa_stream_state_t state = pa_stream_get_state(stream);
308
309 WLog_Print(pulse->log, WLOG_DEBUG, "stream state %s", pulse_stream_state_string(state));
310 switch (state)
311 {
312 case PA_STREAM_READY:
313 case PA_STREAM_FAILED:
314 case PA_STREAM_TERMINATED:
315 pa_threaded_mainloop_signal(pulse->mainloop, 0);
316 break;
317
318 case PA_STREAM_UNCONNECTED:
319 case PA_STREAM_CREATING:
320 default:
321 break;
322 }
323}
324
325static void audin_pulse_stream_request_callback(pa_stream* stream, size_t length, void* userdata)
326{
327 const void* data = NULL;
328 AudinPulseDevice* pulse = (AudinPulseDevice*)userdata;
329 UINT error = CHANNEL_RC_OK;
330 pa_stream_peek(stream, &data, &length);
331 error =
332 IFCALLRESULT(CHANNEL_RC_OK, pulse->receive, &pulse->format, data, length, pulse->user_data);
333 pa_stream_drop(stream);
334
335 if (error && pulse->rdpcontext)
336 setChannelError(pulse->rdpcontext, error, "audin_pulse_thread_func reported an error");
337}
338
344static UINT audin_pulse_close(IAudinDevice* device)
345{
346 AudinPulseDevice* pulse = (AudinPulseDevice*)device;
347
348 if (!pulse)
349 return ERROR_INVALID_PARAMETER;
350
351 if (pulse->stream)
352 {
353 pa_threaded_mainloop_lock(pulse->mainloop);
354 pa_stream_disconnect(pulse->stream);
355 pa_stream_unref(pulse->stream);
356 pulse->stream = NULL;
357 pa_threaded_mainloop_unlock(pulse->mainloop);
358 }
359
360 pulse->receive = NULL;
361 pulse->user_data = NULL;
362 return CHANNEL_RC_OK;
363}
364
370static UINT audin_pulse_open(IAudinDevice* device, AudinReceive receive, void* user_data)
371{
372 pa_stream_state_t state = PA_STREAM_FAILED;
373 pa_buffer_attr buffer_attr = { 0 };
374 AudinPulseDevice* pulse = (AudinPulseDevice*)device;
375
376 if (!pulse || !receive || !user_data)
377 return ERROR_INVALID_PARAMETER;
378
379 if (!pulse->context)
380 return ERROR_INVALID_PARAMETER;
381
382 if (!pulse->sample_spec.rate || pulse->stream)
383 return ERROR_INVALID_PARAMETER;
384
385 pulse->receive = receive;
386 pulse->user_data = user_data;
387 pa_threaded_mainloop_lock(pulse->mainloop);
388 pulse->stream = pa_stream_new(pulse->context, pulse->stream_name, &pulse->sample_spec, NULL);
389
390 if (!pulse->stream)
391 {
392 pa_threaded_mainloop_unlock(pulse->mainloop);
393 WLog_Print(pulse->log, WLOG_DEBUG, "pa_stream_new failed (%d)",
394 pa_context_errno(pulse->context));
395 const int rc = pa_context_errno(pulse->context);
396 return (UINT)rc;
397 }
398
399 pulse->bytes_per_frame = pa_frame_size(&pulse->sample_spec);
400 pa_stream_set_state_callback(pulse->stream, audin_pulse_stream_state_callback, pulse);
401 pa_stream_set_read_callback(pulse->stream, audin_pulse_stream_request_callback, pulse);
402 buffer_attr.maxlength = (UINT32)-1;
403 buffer_attr.tlength = (UINT32)-1;
404 buffer_attr.prebuf = (UINT32)-1;
405 buffer_attr.minreq = (UINT32)-1;
406 /* 500ms latency */
407 const size_t frag = pulse->bytes_per_frame * pulse->frames_per_packet;
408 WINPR_ASSERT(frag <= UINT32_MAX);
409 buffer_attr.fragsize = (uint32_t)frag;
410
411 if (buffer_attr.fragsize % pulse->format.nBlockAlign)
412 buffer_attr.fragsize +=
413 pulse->format.nBlockAlign - buffer_attr.fragsize % pulse->format.nBlockAlign;
414
415 if (pa_stream_connect_record(pulse->stream, pulse->device_name, &buffer_attr,
416 PA_STREAM_ADJUST_LATENCY) < 0)
417 {
418 pa_threaded_mainloop_unlock(pulse->mainloop);
419 WLog_Print(pulse->log, WLOG_ERROR, "pa_stream_connect_playback failed (%d)",
420 pa_context_errno(pulse->context));
421 const int rc = pa_context_errno(pulse->context);
422 return (UINT)rc;
423 }
424
425 while (pulse->stream)
426 {
427 state = pa_stream_get_state(pulse->stream);
428
429 if (state == PA_STREAM_READY)
430 break;
431
432 if (!PA_STREAM_IS_GOOD(state))
433 {
434 audin_pulse_close(device);
435 WLog_Print(pulse->log, WLOG_ERROR, "bad stream state (%s: %d)",
436 pulse_stream_state_string(state), pa_context_errno(pulse->context));
437 pa_threaded_mainloop_unlock(pulse->mainloop);
438 const int rc = pa_context_errno(pulse->context);
439 return (UINT)rc;
440 }
441
442 pa_threaded_mainloop_wait(pulse->mainloop);
443 }
444
445 pa_threaded_mainloop_unlock(pulse->mainloop);
446 pulse->buffer_frames = 0;
447 WLog_Print(pulse->log, WLOG_DEBUG, "connected");
448 return CHANNEL_RC_OK;
449}
450
456static UINT audin_pulse_parse_addin_args(AudinPulseDevice* pulse, const ADDIN_ARGV* args)
457{
458 COMMAND_LINE_ARGUMENT_A audin_pulse_args[] = {
459 { "dev", COMMAND_LINE_VALUE_REQUIRED, "<device>", NULL, NULL, -1, NULL,
460 "audio device name" },
461 { "client_name", COMMAND_LINE_VALUE_REQUIRED, "<client_name>", NULL, NULL, -1, NULL,
462 "name of pulse client" },
463 { "stream_name", COMMAND_LINE_VALUE_REQUIRED, "<stream_name>", NULL, NULL, -1, NULL,
464 "name of pulse stream" },
465 { NULL, 0, NULL, NULL, NULL, -1, NULL, NULL }
466 };
467
468 const DWORD flags =
469 COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
470 const int status = CommandLineParseArgumentsA(args->argc, args->argv, audin_pulse_args, flags,
471 pulse, NULL, NULL);
472
473 if (status < 0)
474 return ERROR_INVALID_PARAMETER;
475
476 const COMMAND_LINE_ARGUMENT_A* arg = audin_pulse_args;
477
478 const char* client_name = NULL;
479 const char* stream_name = NULL;
480 do
481 {
482 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
483 continue;
484
485 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg, "dev")
486 {
487 pulse->device_name = _strdup(arg->Value);
488
489 if (!pulse->device_name)
490 {
491 WLog_Print(pulse->log, WLOG_ERROR, "_strdup failed!");
492 return CHANNEL_RC_NO_MEMORY;
493 }
494 }
495 CommandLineSwitchEnd(arg)
496 } while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
497
498 if (!client_name)
499 client_name = freerdp_getApplicationDetailsString();
500 if (!stream_name)
501 stream_name = freerdp_getApplicationDetailsString();
502
503 pulse->client_name = _strdup(client_name);
504 pulse->stream_name = _strdup(stream_name);
505 if (!pulse->client_name || !pulse->stream_name)
506 return ERROR_OUTOFMEMORY;
507
508 return CHANNEL_RC_OK;
509}
510
516FREERDP_ENTRY_POINT(UINT VCAPITYPE pulse_freerdp_audin_client_subsystem_entry(
518{
519 const ADDIN_ARGV* args = NULL;
520 AudinPulseDevice* pulse = NULL;
521 UINT error = 0;
522 pulse = (AudinPulseDevice*)calloc(1, sizeof(AudinPulseDevice));
523
524 if (!pulse)
525 {
526 WLog_ERR(TAG, "calloc failed!");
527 return CHANNEL_RC_NO_MEMORY;
528 }
529
530 pulse->log = WLog_Get(TAG);
531 pulse->iface.Open = audin_pulse_open;
532 pulse->iface.FormatSupported = audin_pulse_format_supported;
533 pulse->iface.SetFormat = audin_pulse_set_format;
534 pulse->iface.Close = audin_pulse_close;
535 pulse->iface.Free = audin_pulse_free;
536 pulse->rdpcontext = pEntryPoints->rdpcontext;
537 args = pEntryPoints->args;
538
539 if ((error = audin_pulse_parse_addin_args(pulse, args)))
540 {
541 WLog_Print(pulse->log, WLOG_ERROR,
542 "audin_pulse_parse_addin_args failed with error %" PRIu32 "!", error);
543 goto error_out;
544 }
545
546 pulse->mainloop = pa_threaded_mainloop_new();
547
548 if (!pulse->mainloop)
549 {
550 WLog_Print(pulse->log, WLOG_ERROR, "pa_threaded_mainloop_new failed");
551 error = CHANNEL_RC_NO_MEMORY;
552 goto error_out;
553 }
554
555 pulse->context =
556 pa_context_new(pa_threaded_mainloop_get_api(pulse->mainloop), pulse->client_name);
557
558 if (!pulse->context)
559 {
560 WLog_Print(pulse->log, WLOG_ERROR, "pa_context_new failed");
561 error = CHANNEL_RC_NO_MEMORY;
562 goto error_out;
563 }
564
565 pa_context_set_state_callback(pulse->context, audin_pulse_context_state_callback, pulse);
566
567 if ((error = audin_pulse_connect(&pulse->iface)))
568 {
569 WLog_Print(pulse->log, WLOG_ERROR, "audin_pulse_connect failed");
570 goto error_out;
571 }
572
573 if ((error = pEntryPoints->pRegisterAudinDevice(pEntryPoints->plugin, &pulse->iface)))
574 {
575 WLog_Print(pulse->log, WLOG_ERROR, "RegisterAudinDevice failed with error %" PRIu32 "!",
576 error);
577 goto error_out;
578 }
579
580 return CHANNEL_RC_OK;
581error_out:
582 audin_pulse_free(&pulse->iface);
583 return error;
584}