1 /**
2  * FreeRDP: A Remote Desktop Protocol Implementation
3  * Audio Input Redirection Virtual Channel - PulseAudio implementation
4  *
5  * Copyright 2010-2011 Vic Lee
6  * Copyright 2015 Thincast Technologies GmbH
7  * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *     http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #include <winpr/crt.h>
31 #include <winpr/cmdline.h>
32 #include <winpr/wlog.h>
33 
34 #include <pulse/pulseaudio.h>
35 
36 #include <freerdp/types.h>
37 #include <freerdp/addin.h>
38 #include <freerdp/codec/audio.h>
39 #include <freerdp/client/audin.h>
40 
41 #include "audin_main.h"
42 
43 typedef struct _AudinPulseDevice
44 {
45 	IAudinDevice iface;
46 
47 	char* device_name;
48 	UINT32 frames_per_packet;
49 	pa_threaded_mainloop* mainloop;
50 	pa_context* context;
51 	pa_sample_spec sample_spec;
52 	pa_stream* stream;
53 	AUDIO_FORMAT format;
54 
55 	size_t bytes_per_frame;
56 	size_t buffer_frames;
57 
58 	AudinReceive receive;
59 	void* user_data;
60 
61 	rdpContext* rdpcontext;
62 	wLog* log;
63 } AudinPulseDevice;
64 
pulse_context_state_string(pa_context_state_t state)65 static const char* pulse_context_state_string(pa_context_state_t state)
66 {
67 	switch (state)
68 	{
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";
83 		default:
84 			return "UNKNOWN";
85 	}
86 }
87 
pulse_stream_state_string(pa_stream_state_t state)88 static const char* pulse_stream_state_string(pa_stream_state_t state)
89 {
90 	switch (state)
91 	{
92 		case PA_STREAM_UNCONNECTED:
93 			return "PA_STREAM_UNCONNECTED";
94 		case PA_STREAM_CREATING:
95 			return "PA_STREAM_CREATING";
96 		case PA_STREAM_READY:
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";
102 		default:
103 			return "UNKNOWN";
104 	}
105 }
106 
audin_pulse_context_state_callback(pa_context * context,void * userdata)107 static void audin_pulse_context_state_callback(pa_context* context, void* userdata)
108 {
109 	pa_context_state_t state;
110 	AudinPulseDevice* pulse = (AudinPulseDevice*)userdata;
111 	state = pa_context_get_state(context);
112 
113 	WLog_Print(pulse->log, WLOG_DEBUG, "context state %s", pulse_context_state_string(state));
114 	switch (state)
115 	{
116 		case PA_CONTEXT_READY:
117 			pa_threaded_mainloop_signal(pulse->mainloop, 0);
118 			break;
119 
120 		case PA_CONTEXT_FAILED:
121 		case PA_CONTEXT_TERMINATED:
122 			pa_threaded_mainloop_signal(pulse->mainloop, 0);
123 			break;
124 
125 		default:
126 			break;
127 	}
128 }
129 
130 /**
131  * Function description
132  *
133  * @return 0 on success, otherwise a Win32 error code
134  */
audin_pulse_connect(IAudinDevice * device)135 static UINT audin_pulse_connect(IAudinDevice* device)
136 {
137 	pa_context_state_t state;
138 	AudinPulseDevice* pulse = (AudinPulseDevice*)device;
139 
140 	if (!pulse->context)
141 		return ERROR_INVALID_PARAMETER;
142 
143 	if (pa_context_connect(pulse->context, NULL, 0, NULL))
144 	{
145 		WLog_Print(pulse->log, WLOG_ERROR, "pa_context_connect failed (%d)",
146 		           pa_context_errno(pulse->context));
147 		return ERROR_INTERNAL_ERROR;
148 	}
149 
150 	pa_threaded_mainloop_lock(pulse->mainloop);
151 
152 	if (pa_threaded_mainloop_start(pulse->mainloop) < 0)
153 	{
154 		pa_threaded_mainloop_unlock(pulse->mainloop);
155 		WLog_Print(pulse->log, WLOG_ERROR, "pa_threaded_mainloop_start failed (%d)",
156 		           pa_context_errno(pulse->context));
157 		return ERROR_INTERNAL_ERROR;
158 	}
159 
160 	for (;;)
161 	{
162 		state = pa_context_get_state(pulse->context);
163 
164 		if (state == PA_CONTEXT_READY)
165 			break;
166 
167 		if (!PA_CONTEXT_IS_GOOD(state))
168 		{
169 			WLog_Print(pulse->log, WLOG_ERROR, "bad context state (%s: %d)",
170 			           pulse_context_state_string(state), pa_context_errno(pulse->context));
171 			pa_context_disconnect(pulse->context);
172 			return ERROR_INVALID_STATE;
173 		}
174 
175 		pa_threaded_mainloop_wait(pulse->mainloop);
176 	}
177 
178 	pa_threaded_mainloop_unlock(pulse->mainloop);
179 	WLog_Print(pulse->log, WLOG_DEBUG, "connected");
180 	return CHANNEL_RC_OK;
181 }
182 
183 /**
184  * Function description
185  *
186  * @return 0 on success, otherwise a Win32 error code
187  */
audin_pulse_free(IAudinDevice * device)188 static UINT audin_pulse_free(IAudinDevice* device)
189 {
190 	AudinPulseDevice* pulse = (AudinPulseDevice*)device;
191 
192 	if (!pulse)
193 		return ERROR_INVALID_PARAMETER;
194 
195 	if (pulse->mainloop)
196 	{
197 		pa_threaded_mainloop_stop(pulse->mainloop);
198 	}
199 
200 	if (pulse->context)
201 	{
202 		pa_context_disconnect(pulse->context);
203 		pa_context_unref(pulse->context);
204 		pulse->context = NULL;
205 	}
206 
207 	if (pulse->mainloop)
208 	{
209 		pa_threaded_mainloop_free(pulse->mainloop);
210 		pulse->mainloop = NULL;
211 	}
212 
213 	free(pulse);
214 	return CHANNEL_RC_OK;
215 }
216 
audin_pulse_format_supported(IAudinDevice * device,const AUDIO_FORMAT * format)217 static BOOL audin_pulse_format_supported(IAudinDevice* device, const AUDIO_FORMAT* format)
218 {
219 	AudinPulseDevice* pulse = (AudinPulseDevice*)device;
220 
221 	if (!pulse || !format)
222 		return FALSE;
223 
224 	if (!pulse->context)
225 		return 0;
226 
227 	switch (format->wFormatTag)
228 	{
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))
233 			{
234 				return TRUE;
235 			}
236 
237 			break;
238 
239 		case WAVE_FORMAT_ALAW:  /* A-LAW */
240 		case WAVE_FORMAT_MULAW: /* U-LAW */
241 			if (format->cbSize == 0 && (format->nSamplesPerSec <= PA_RATE_MAX) &&
242 			    (format->wBitsPerSample == 8) &&
243 			    (format->nChannels >= 1 && format->nChannels <= PA_CHANNELS_MAX))
244 			{
245 				return TRUE;
246 			}
247 
248 			break;
249 
250 		default:
251 			return FALSE;
252 	}
253 
254 	return FALSE;
255 }
256 
257 /**
258  * Function description
259  *
260  * @return 0 on success, otherwise a Win32 error code
261  */
audin_pulse_set_format(IAudinDevice * device,const AUDIO_FORMAT * format,UINT32 FramesPerPacket)262 static UINT audin_pulse_set_format(IAudinDevice* device, const AUDIO_FORMAT* format,
263                                    UINT32 FramesPerPacket)
264 {
265 	pa_sample_spec sample_spec = { 0 };
266 	AudinPulseDevice* pulse = (AudinPulseDevice*)device;
267 
268 	if (!pulse || !format)
269 		return ERROR_INVALID_PARAMETER;
270 
271 	if (!pulse->context)
272 		return ERROR_INVALID_PARAMETER;
273 
274 	if (FramesPerPacket > 0)
275 		pulse->frames_per_packet = FramesPerPacket;
276 
277 	sample_spec.rate = format->nSamplesPerSec;
278 	sample_spec.channels = format->nChannels;
279 
280 	switch (format->wFormatTag)
281 	{
282 		case WAVE_FORMAT_PCM: /* PCM */
283 			switch (format->wBitsPerSample)
284 			{
285 				case 8:
286 					sample_spec.format = PA_SAMPLE_U8;
287 					break;
288 
289 				case 16:
290 					sample_spec.format = PA_SAMPLE_S16LE;
291 					break;
292 
293 				default:
294 					return ERROR_INTERNAL_ERROR;
295 			}
296 
297 			break;
298 
299 		case WAVE_FORMAT_ALAW: /* A-LAW */
300 			sample_spec.format = PA_SAMPLE_ALAW;
301 			break;
302 
303 		case WAVE_FORMAT_MULAW: /* U-LAW */
304 			sample_spec.format = PA_SAMPLE_ULAW;
305 			break;
306 
307 		default:
308 			return ERROR_INTERNAL_ERROR;
309 	}
310 
311 	pulse->sample_spec = sample_spec;
312 	pulse->format = *format;
313 	return CHANNEL_RC_OK;
314 }
315 
audin_pulse_stream_state_callback(pa_stream * stream,void * userdata)316 static void audin_pulse_stream_state_callback(pa_stream* stream, void* userdata)
317 {
318 	pa_stream_state_t state;
319 	AudinPulseDevice* pulse = (AudinPulseDevice*)userdata;
320 	state = pa_stream_get_state(stream);
321 
322 	WLog_Print(pulse->log, WLOG_DEBUG, "stream state %s", pulse_stream_state_string(state));
323 	switch (state)
324 	{
325 		case PA_STREAM_READY:
326 			pa_threaded_mainloop_signal(pulse->mainloop, 0);
327 			break;
328 
329 		case PA_STREAM_FAILED:
330 		case PA_STREAM_TERMINATED:
331 			pa_threaded_mainloop_signal(pulse->mainloop, 0);
332 			break;
333 
334 		default:
335 			break;
336 	}
337 }
338 
audin_pulse_stream_request_callback(pa_stream * stream,size_t length,void * userdata)339 static void audin_pulse_stream_request_callback(pa_stream* stream, size_t length, void* userdata)
340 {
341 	const void* data;
342 	AudinPulseDevice* pulse = (AudinPulseDevice*)userdata;
343 	UINT error = CHANNEL_RC_OK;
344 	pa_stream_peek(stream, &data, &length);
345 	error =
346 	    IFCALLRESULT(CHANNEL_RC_OK, pulse->receive, &pulse->format, data, length, pulse->user_data);
347 	pa_stream_drop(stream);
348 
349 	if (error && pulse->rdpcontext)
350 		setChannelError(pulse->rdpcontext, error, "audin_pulse_thread_func reported an error");
351 }
352 
353 /**
354  * Function description
355  *
356  * @return 0 on success, otherwise a Win32 error code
357  */
audin_pulse_close(IAudinDevice * device)358 static UINT audin_pulse_close(IAudinDevice* device)
359 {
360 	AudinPulseDevice* pulse = (AudinPulseDevice*)device;
361 
362 	if (!pulse)
363 		return ERROR_INVALID_PARAMETER;
364 
365 	if (pulse->stream)
366 	{
367 		pa_threaded_mainloop_lock(pulse->mainloop);
368 		pa_stream_disconnect(pulse->stream);
369 		pa_stream_unref(pulse->stream);
370 		pulse->stream = NULL;
371 		pa_threaded_mainloop_unlock(pulse->mainloop);
372 	}
373 
374 	pulse->receive = NULL;
375 	pulse->user_data = NULL;
376 	return CHANNEL_RC_OK;
377 }
378 
379 /**
380  * Function description
381  *
382  * @return 0 on success, otherwise a Win32 error code
383  */
audin_pulse_open(IAudinDevice * device,AudinReceive receive,void * user_data)384 static UINT audin_pulse_open(IAudinDevice* device, AudinReceive receive, void* user_data)
385 {
386 	pa_stream_state_t state;
387 	pa_buffer_attr buffer_attr = { 0 };
388 	AudinPulseDevice* pulse = (AudinPulseDevice*)device;
389 
390 	if (!pulse || !receive || !user_data)
391 		return ERROR_INVALID_PARAMETER;
392 
393 	if (!pulse->context)
394 		return ERROR_INVALID_PARAMETER;
395 
396 	if (!pulse->sample_spec.rate || pulse->stream)
397 		return ERROR_INVALID_PARAMETER;
398 
399 	pulse->receive = receive;
400 	pulse->user_data = user_data;
401 	pa_threaded_mainloop_lock(pulse->mainloop);
402 	pulse->stream = pa_stream_new(pulse->context, "freerdp_audin", &pulse->sample_spec, NULL);
403 
404 	if (!pulse->stream)
405 	{
406 		pa_threaded_mainloop_unlock(pulse->mainloop);
407 		WLog_Print(pulse->log, WLOG_DEBUG, "pa_stream_new failed (%d)",
408 		           pa_context_errno(pulse->context));
409 		return pa_context_errno(pulse->context);
410 	}
411 
412 	pulse->bytes_per_frame = pa_frame_size(&pulse->sample_spec);
413 	pa_stream_set_state_callback(pulse->stream, audin_pulse_stream_state_callback, pulse);
414 	pa_stream_set_read_callback(pulse->stream, audin_pulse_stream_request_callback, pulse);
415 	buffer_attr.maxlength = (UINT32)-1;
416 	buffer_attr.tlength = (UINT32)-1;
417 	buffer_attr.prebuf = (UINT32)-1;
418 	buffer_attr.minreq = (UINT32)-1;
419 	/* 500ms latency */
420 	buffer_attr.fragsize = pulse->bytes_per_frame * pulse->frames_per_packet;
421 
422 	if (buffer_attr.fragsize % pulse->format.nBlockAlign)
423 		buffer_attr.fragsize +=
424 		    pulse->format.nBlockAlign - buffer_attr.fragsize % pulse->format.nBlockAlign;
425 
426 	if (pa_stream_connect_record(pulse->stream, pulse->device_name, &buffer_attr,
427 	                             PA_STREAM_ADJUST_LATENCY) < 0)
428 	{
429 		pa_threaded_mainloop_unlock(pulse->mainloop);
430 		WLog_Print(pulse->log, WLOG_ERROR, "pa_stream_connect_playback failed (%d)",
431 		           pa_context_errno(pulse->context));
432 		return pa_context_errno(pulse->context);
433 	}
434 
435 	for (;;)
436 	{
437 		state = pa_stream_get_state(pulse->stream);
438 
439 		if (state == PA_STREAM_READY)
440 			break;
441 
442 		if (!PA_STREAM_IS_GOOD(state))
443 		{
444 			audin_pulse_close(device);
445 			WLog_Print(pulse->log, WLOG_ERROR, "bad stream state (%s: %d)",
446 			           pulse_stream_state_string(state), pa_context_errno(pulse->context));
447 			pa_threaded_mainloop_unlock(pulse->mainloop);
448 			return pa_context_errno(pulse->context);
449 		}
450 
451 		pa_threaded_mainloop_wait(pulse->mainloop);
452 	}
453 
454 	pa_threaded_mainloop_unlock(pulse->mainloop);
455 	pulse->buffer_frames = 0;
456 	WLog_Print(pulse->log, WLOG_DEBUG, "connected");
457 	return CHANNEL_RC_OK;
458 }
459 
460 /**
461  * Function description
462  *
463  * @return 0 on success, otherwise a Win32 error code
464  */
audin_pulse_parse_addin_args(AudinPulseDevice * device,ADDIN_ARGV * args)465 static UINT audin_pulse_parse_addin_args(AudinPulseDevice* device, ADDIN_ARGV* args)
466 {
467 	int status;
468 	DWORD flags;
469 	COMMAND_LINE_ARGUMENT_A* arg;
470 	AudinPulseDevice* pulse = (AudinPulseDevice*)device;
471 	COMMAND_LINE_ARGUMENT_A audin_pulse_args[] = { { "dev", COMMAND_LINE_VALUE_REQUIRED, "<device>",
472 		                                             NULL, NULL, -1, NULL, "audio device name" },
473 		                                           { NULL, 0, NULL, NULL, NULL, -1, NULL, NULL } };
474 
475 	flags =
476 	    COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
477 	status = CommandLineParseArgumentsA(args->argc, args->argv, audin_pulse_args, flags, pulse,
478 	                                    NULL, NULL);
479 
480 	if (status < 0)
481 		return ERROR_INVALID_PARAMETER;
482 
483 	arg = audin_pulse_args;
484 
485 	do
486 	{
487 		if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
488 			continue;
489 
490 		CommandLineSwitchStart(arg) CommandLineSwitchCase(arg, "dev")
491 		{
492 			pulse->device_name = _strdup(arg->Value);
493 
494 			if (!pulse->device_name)
495 			{
496 				WLog_Print(pulse->log, WLOG_ERROR, "_strdup failed!");
497 				return CHANNEL_RC_NO_MEMORY;
498 			}
499 		}
500 		CommandLineSwitchEnd(arg)
501 	} while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
502 
503 	return CHANNEL_RC_OK;
504 }
505 
506 #ifdef BUILTIN_CHANNELS
507 #define freerdp_audin_client_subsystem_entry pulse_freerdp_audin_client_subsystem_entry
508 #else
509 #define freerdp_audin_client_subsystem_entry FREERDP_API freerdp_audin_client_subsystem_entry
510 #endif
511 
512 /**
513  * Function description
514  *
515  * @return 0 on success, otherwise a Win32 error code
516  */
freerdp_audin_client_subsystem_entry(PFREERDP_AUDIN_DEVICE_ENTRY_POINTS pEntryPoints)517 UINT freerdp_audin_client_subsystem_entry(PFREERDP_AUDIN_DEVICE_ENTRY_POINTS pEntryPoints)
518 {
519 	ADDIN_ARGV* args;
520 	AudinPulseDevice* pulse;
521 	UINT error;
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 = pa_context_new(pa_threaded_mainloop_get_api(pulse->mainloop), "freerdp");
556 
557 	if (!pulse->context)
558 	{
559 		WLog_Print(pulse->log, WLOG_ERROR, "pa_context_new failed");
560 		error = CHANNEL_RC_NO_MEMORY;
561 		goto error_out;
562 	}
563 
564 	pa_context_set_state_callback(pulse->context, audin_pulse_context_state_callback, pulse);
565 
566 	if ((error = audin_pulse_connect(&pulse->iface)))
567 	{
568 		WLog_Print(pulse->log, WLOG_ERROR, "audin_pulse_connect failed");
569 		goto error_out;
570 	}
571 
572 	if ((error = pEntryPoints->pRegisterAudinDevice(pEntryPoints->plugin, &pulse->iface)))
573 	{
574 		WLog_Print(pulse->log, WLOG_ERROR, "RegisterAudinDevice failed with error %" PRIu32 "!",
575 		           error);
576 		goto error_out;
577 	}
578 
579 	return CHANNEL_RC_OK;
580 error_out:
581 	audin_pulse_free(&pulse->iface);
582 	return error;
583 }
584