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