Blame channels/audin/client/pulse/audin_pulse.c

Packit 1fb8d4
/**
Packit 1fb8d4
 * FreeRDP: A Remote Desktop Protocol Implementation
Packit 1fb8d4
 * Audio Input Redirection Virtual Channel - PulseAudio implementation
Packit 1fb8d4
 *
Packit 1fb8d4
 * Copyright 2010-2011 Vic Lee
Packit 1fb8d4
 * Copyright 2015 Thincast Technologies GmbH
Packit 1fb8d4
 * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
Packit 1fb8d4
 *
Packit 1fb8d4
 * Licensed under the Apache License, Version 2.0 (the "License");
Packit 1fb8d4
 * you may not use this file except in compliance with the License.
Packit 1fb8d4
 * You may obtain a copy of the License at
Packit 1fb8d4
 *
Packit 1fb8d4
 *     http://www.apache.org/licenses/LICENSE-2.0
Packit 1fb8d4
 *
Packit 1fb8d4
 * Unless required by applicable law or agreed to in writing, software
Packit 1fb8d4
 * distributed under the License is distributed on an "AS IS" BASIS,
Packit 1fb8d4
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 1fb8d4
 * See the License for the specific language governing permissions and
Packit 1fb8d4
 * limitations under the License.
Packit 1fb8d4
 */
Packit 1fb8d4
Packit 1fb8d4
#ifdef HAVE_CONFIG_H
Packit 1fb8d4
#include "config.h"
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
#include <stdio.h>
Packit 1fb8d4
#include <stdlib.h>
Packit 1fb8d4
#include <string.h>
Packit 1fb8d4
Packit 1fb8d4
#include <winpr/crt.h>
Packit 1fb8d4
#include <winpr/cmdline.h>
Packit 1fb8d4
#include <winpr/wlog.h>
Packit 1fb8d4
Packit 1fb8d4
#include <pulse/pulseaudio.h>
Packit 1fb8d4
Packit 1fb8d4
#include <freerdp/types.h>
Packit 1fb8d4
#include <freerdp/addin.h>
Packit 1fb8d4
#include <freerdp/codec/audio.h>
Packit 1fb8d4
#include <freerdp/client/audin.h>
Packit 1fb8d4
Packit 1fb8d4
#include "audin_main.h"
Packit 1fb8d4
Packit 1fb8d4
typedef struct _AudinPulseDevice
Packit 1fb8d4
{
Packit 1fb8d4
	IAudinDevice iface;
Packit 1fb8d4
Packit 1fb8d4
	char* device_name;
Packit 1fb8d4
	UINT32 frames_per_packet;
Packit 1fb8d4
	pa_threaded_mainloop* mainloop;
Packit 1fb8d4
	pa_context* context;
Packit 1fb8d4
	pa_sample_spec sample_spec;
Packit 1fb8d4
	pa_stream* stream;
Packit 1fb8d4
	AUDIO_FORMAT format;
Packit 1fb8d4
Packit 1fb8d4
	size_t bytes_per_frame;
Packit 1fb8d4
	size_t buffer_frames;
Packit 1fb8d4
Packit 1fb8d4
	AudinReceive receive;
Packit 1fb8d4
	void* user_data;
Packit 1fb8d4
Packit 1fb8d4
	rdpContext* rdpcontext;
Packit 1fb8d4
	wLog* log;
Packit 1fb8d4
} AudinPulseDevice;
Packit 1fb8d4
Packit Service 5a9772
static const char* pulse_context_state_string(pa_context_state_t state)
Packit Service 5a9772
{
Packit Service 5a9772
	switch (state)
Packit Service 5a9772
	{
Packit Service 5a9772
		case PA_CONTEXT_UNCONNECTED:
Packit Service 5a9772
			return "PA_CONTEXT_UNCONNECTED";
Packit Service 5a9772
		case PA_CONTEXT_CONNECTING:
Packit Service 5a9772
			return "PA_CONTEXT_CONNECTING";
Packit Service 5a9772
		case PA_CONTEXT_AUTHORIZING:
Packit Service 5a9772
			return "PA_CONTEXT_AUTHORIZING";
Packit Service 5a9772
		case PA_CONTEXT_SETTING_NAME:
Packit Service 5a9772
			return "PA_CONTEXT_SETTING_NAME";
Packit Service 5a9772
		case PA_CONTEXT_READY:
Packit Service 5a9772
			return "PA_CONTEXT_READY";
Packit Service 5a9772
		case PA_CONTEXT_FAILED:
Packit Service 5a9772
			return "PA_CONTEXT_FAILED";
Packit Service 5a9772
		case PA_CONTEXT_TERMINATED:
Packit Service 5a9772
			return "PA_CONTEXT_TERMINATED";
Packit Service 5a9772
		default:
Packit Service 5a9772
			return "UNKNOWN";
Packit Service 5a9772
	}
Packit Service 5a9772
}
Packit Service 5a9772
Packit Service 5a9772
static const char* pulse_stream_state_string(pa_stream_state_t state)
Packit Service 5a9772
{
Packit Service 5a9772
	switch (state)
Packit Service 5a9772
	{
Packit Service 5a9772
		case PA_STREAM_UNCONNECTED:
Packit Service 5a9772
			return "PA_STREAM_UNCONNECTED";
Packit Service 5a9772
		case PA_STREAM_CREATING:
Packit Service 5a9772
			return "PA_STREAM_CREATING";
Packit Service 5a9772
		case PA_STREAM_READY:
Packit Service 5a9772
			return "PA_STREAM_READY";
Packit Service 5a9772
		case PA_STREAM_FAILED:
Packit Service 5a9772
			return "PA_STREAM_FAILED";
Packit Service 5a9772
		case PA_STREAM_TERMINATED:
Packit Service 5a9772
			return "PA_STREAM_TERMINATED";
Packit Service 5a9772
		default:
Packit Service 5a9772
			return "UNKNOWN";
Packit Service 5a9772
	}
Packit Service 5a9772
}
Packit Service 5a9772
Packit 1fb8d4
static void audin_pulse_context_state_callback(pa_context* context, void* userdata)
Packit 1fb8d4
{
Packit 1fb8d4
	pa_context_state_t state;
Packit Service 5a9772
	AudinPulseDevice* pulse = (AudinPulseDevice*)userdata;
Packit 1fb8d4
	state = pa_context_get_state(context);
Packit 1fb8d4
Packit Service 5a9772
	WLog_Print(pulse->log, WLOG_DEBUG, "context state %s", pulse_context_state_string(state));
Packit 1fb8d4
	switch (state)
Packit 1fb8d4
	{
Packit 1fb8d4
		case PA_CONTEXT_READY:
Packit 1fb8d4
			pa_threaded_mainloop_signal(pulse->mainloop, 0);
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case PA_CONTEXT_FAILED:
Packit 1fb8d4
		case PA_CONTEXT_TERMINATED:
Packit 1fb8d4
			pa_threaded_mainloop_signal(pulse->mainloop, 0);
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		default:
Packit 1fb8d4
			break;
Packit 1fb8d4
	}
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT audin_pulse_connect(IAudinDevice* device)
Packit 1fb8d4
{
Packit 1fb8d4
	pa_context_state_t state;
Packit Service 5a9772
	AudinPulseDevice* pulse = (AudinPulseDevice*)device;
Packit 1fb8d4
Packit 1fb8d4
	if (!pulse->context)
Packit 1fb8d4
		return ERROR_INVALID_PARAMETER;
Packit 1fb8d4
Packit 1fb8d4
	if (pa_context_connect(pulse->context, NULL, 0, NULL))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_Print(pulse->log, WLOG_ERROR, "pa_context_connect failed (%d)",
Packit 1fb8d4
		           pa_context_errno(pulse->context));
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	pa_threaded_mainloop_lock(pulse->mainloop);
Packit 1fb8d4
Packit 1fb8d4
	if (pa_threaded_mainloop_start(pulse->mainloop) < 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		pa_threaded_mainloop_unlock(pulse->mainloop);
Packit 1fb8d4
		WLog_Print(pulse->log, WLOG_ERROR, "pa_threaded_mainloop_start failed (%d)",
Packit 1fb8d4
		           pa_context_errno(pulse->context));
Packit 1fb8d4
		return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	for (;;)
Packit 1fb8d4
	{
Packit 1fb8d4
		state = pa_context_get_state(pulse->context);
Packit 1fb8d4
Packit 1fb8d4
		if (state == PA_CONTEXT_READY)
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		if (!PA_CONTEXT_IS_GOOD(state))
Packit 1fb8d4
		{
Packit Service 5a9772
			WLog_Print(pulse->log, WLOG_ERROR, "bad context state (%s: %d)",
Packit Service 5a9772
			           pulse_context_state_string(state), pa_context_errno(pulse->context));
Packit 1fb8d4
			pa_context_disconnect(pulse->context);
Packit 1fb8d4
			return ERROR_INVALID_STATE;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		pa_threaded_mainloop_wait(pulse->mainloop);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	pa_threaded_mainloop_unlock(pulse->mainloop);
Packit Service 5a9772
	WLog_Print(pulse->log, WLOG_DEBUG, "connected");
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT audin_pulse_free(IAudinDevice* device)
Packit 1fb8d4
{
Packit Service 5a9772
	AudinPulseDevice* pulse = (AudinPulseDevice*)device;
Packit 1fb8d4
Packit 1fb8d4
	if (!pulse)
Packit 1fb8d4
		return ERROR_INVALID_PARAMETER;
Packit 1fb8d4
Packit 1fb8d4
	if (pulse->mainloop)
Packit 1fb8d4
	{
Packit 1fb8d4
		pa_threaded_mainloop_stop(pulse->mainloop);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (pulse->context)
Packit 1fb8d4
	{
Packit 1fb8d4
		pa_context_disconnect(pulse->context);
Packit 1fb8d4
		pa_context_unref(pulse->context);
Packit 1fb8d4
		pulse->context = NULL;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (pulse->mainloop)
Packit 1fb8d4
	{
Packit 1fb8d4
		pa_threaded_mainloop_free(pulse->mainloop);
Packit 1fb8d4
		pulse->mainloop = NULL;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	free(pulse);
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static BOOL audin_pulse_format_supported(IAudinDevice* device, const AUDIO_FORMAT* format)
Packit 1fb8d4
{
Packit Service 5a9772
	AudinPulseDevice* pulse = (AudinPulseDevice*)device;
Packit 1fb8d4
Packit 1fb8d4
	if (!pulse || !format)
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	if (!pulse->context)
Packit 1fb8d4
		return 0;
Packit 1fb8d4
Packit 1fb8d4
	switch (format->wFormatTag)
Packit 1fb8d4
	{
Packit 1fb8d4
		case WAVE_FORMAT_PCM:
Packit Service 5a9772
			if (format->cbSize == 0 && (format->nSamplesPerSec <= PA_RATE_MAX) &&
Packit 1fb8d4
			    (format->wBitsPerSample == 8 || format->wBitsPerSample == 16) &&
Packit 1fb8d4
			    (format->nChannels >= 1 && format->nChannels <= PA_CHANNELS_MAX))
Packit 1fb8d4
			{
Packit 1fb8d4
				return TRUE;
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit Service 5a9772
		case WAVE_FORMAT_ALAW:  /* A-LAW */
Packit 1fb8d4
		case WAVE_FORMAT_MULAW: /* U-LAW */
Packit Service 5a9772
			if (format->cbSize == 0 && (format->nSamplesPerSec <= PA_RATE_MAX) &&
Packit 1fb8d4
			    (format->wBitsPerSample == 8) &&
Packit 1fb8d4
			    (format->nChannels >= 1 && format->nChannels <= PA_CHANNELS_MAX))
Packit 1fb8d4
			{
Packit 1fb8d4
				return TRUE;
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		default:
Packit 1fb8d4
			return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return FALSE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT audin_pulse_set_format(IAudinDevice* device, const AUDIO_FORMAT* format,
Packit 1fb8d4
                                   UINT32 FramesPerPacket)
Packit 1fb8d4
{
Packit 1fb8d4
	pa_sample_spec sample_spec = { 0 };
Packit Service 5a9772
	AudinPulseDevice* pulse = (AudinPulseDevice*)device;
Packit 1fb8d4
Packit 1fb8d4
	if (!pulse || !format)
Packit 1fb8d4
		return ERROR_INVALID_PARAMETER;
Packit 1fb8d4
Packit 1fb8d4
	if (!pulse->context)
Packit 1fb8d4
		return ERROR_INVALID_PARAMETER;
Packit 1fb8d4
Packit 1fb8d4
	if (FramesPerPacket > 0)
Packit 1fb8d4
		pulse->frames_per_packet = FramesPerPacket;
Packit 1fb8d4
Packit 1fb8d4
	sample_spec.rate = format->nSamplesPerSec;
Packit 1fb8d4
	sample_spec.channels = format->nChannels;
Packit 1fb8d4
Packit 1fb8d4
	switch (format->wFormatTag)
Packit 1fb8d4
	{
Packit 1fb8d4
		case WAVE_FORMAT_PCM: /* PCM */
Packit 1fb8d4
			switch (format->wBitsPerSample)
Packit 1fb8d4
			{
Packit 1fb8d4
				case 8:
Packit 1fb8d4
					sample_spec.format = PA_SAMPLE_U8;
Packit 1fb8d4
					break;
Packit 1fb8d4
Packit 1fb8d4
				case 16:
Packit 1fb8d4
					sample_spec.format = PA_SAMPLE_S16LE;
Packit 1fb8d4
					break;
Packit 1fb8d4
Packit 1fb8d4
				default:
Packit 1fb8d4
					return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case WAVE_FORMAT_ALAW: /* A-LAW */
Packit 1fb8d4
			sample_spec.format = PA_SAMPLE_ALAW;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case WAVE_FORMAT_MULAW: /* U-LAW */
Packit 1fb8d4
			sample_spec.format = PA_SAMPLE_ULAW;
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		default:
Packit 1fb8d4
			return ERROR_INTERNAL_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	pulse->sample_spec = sample_spec;
Packit 1fb8d4
	pulse->format = *format;
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static void audin_pulse_stream_state_callback(pa_stream* stream, void* userdata)
Packit 1fb8d4
{
Packit 1fb8d4
	pa_stream_state_t state;
Packit Service 5a9772
	AudinPulseDevice* pulse = (AudinPulseDevice*)userdata;
Packit 1fb8d4
	state = pa_stream_get_state(stream);
Packit 1fb8d4
Packit Service 5a9772
	WLog_Print(pulse->log, WLOG_DEBUG, "stream state %s", pulse_stream_state_string(state));
Packit 1fb8d4
	switch (state)
Packit 1fb8d4
	{
Packit 1fb8d4
		case PA_STREAM_READY:
Packit 1fb8d4
			pa_threaded_mainloop_signal(pulse->mainloop, 0);
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		case PA_STREAM_FAILED:
Packit 1fb8d4
		case PA_STREAM_TERMINATED:
Packit 1fb8d4
			pa_threaded_mainloop_signal(pulse->mainloop, 0);
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		default:
Packit 1fb8d4
			break;
Packit 1fb8d4
	}
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static void audin_pulse_stream_request_callback(pa_stream* stream, size_t length, void* userdata)
Packit 1fb8d4
{
Packit 1fb8d4
	const void* data;
Packit Service 5a9772
	AudinPulseDevice* pulse = (AudinPulseDevice*)userdata;
Packit 1fb8d4
	UINT error = CHANNEL_RC_OK;
Packit 1fb8d4
	pa_stream_peek(stream, &data, &length);
Packit Service 5a9772
	error =
Packit Service 5a9772
	    IFCALLRESULT(CHANNEL_RC_OK, pulse->receive, &pulse->format, data, length, pulse->user_data);
Packit 1fb8d4
	pa_stream_drop(stream);
Packit 1fb8d4
Packit 1fb8d4
	if (error && pulse->rdpcontext)
Packit 1fb8d4
		setChannelError(pulse->rdpcontext, error, "audin_pulse_thread_func reported an error");
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT audin_pulse_close(IAudinDevice* device)
Packit 1fb8d4
{
Packit Service 5a9772
	AudinPulseDevice* pulse = (AudinPulseDevice*)device;
Packit 1fb8d4
Packit 1fb8d4
	if (!pulse)
Packit 1fb8d4
		return ERROR_INVALID_PARAMETER;
Packit 1fb8d4
Packit 1fb8d4
	if (pulse->stream)
Packit 1fb8d4
	{
Packit 1fb8d4
		pa_threaded_mainloop_lock(pulse->mainloop);
Packit 1fb8d4
		pa_stream_disconnect(pulse->stream);
Packit 1fb8d4
		pa_stream_unref(pulse->stream);
Packit 1fb8d4
		pulse->stream = NULL;
Packit 1fb8d4
		pa_threaded_mainloop_unlock(pulse->mainloop);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	pulse->receive = NULL;
Packit 1fb8d4
	pulse->user_data = NULL;
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT audin_pulse_open(IAudinDevice* device, AudinReceive receive, void* user_data)
Packit 1fb8d4
{
Packit 1fb8d4
	pa_stream_state_t state;
Packit 1fb8d4
	pa_buffer_attr buffer_attr = { 0 };
Packit Service 5a9772
	AudinPulseDevice* pulse = (AudinPulseDevice*)device;
Packit 1fb8d4
Packit 1fb8d4
	if (!pulse || !receive || !user_data)
Packit 1fb8d4
		return ERROR_INVALID_PARAMETER;
Packit 1fb8d4
Packit 1fb8d4
	if (!pulse->context)
Packit 1fb8d4
		return ERROR_INVALID_PARAMETER;
Packit 1fb8d4
Packit 1fb8d4
	if (!pulse->sample_spec.rate || pulse->stream)
Packit 1fb8d4
		return ERROR_INVALID_PARAMETER;
Packit 1fb8d4
Packit 1fb8d4
	pulse->receive = receive;
Packit 1fb8d4
	pulse->user_data = user_data;
Packit 1fb8d4
	pa_threaded_mainloop_lock(pulse->mainloop);
Packit Service 5a9772
	pulse->stream = pa_stream_new(pulse->context, "freerdp_audin", &pulse->sample_spec, NULL);
Packit 1fb8d4
Packit 1fb8d4
	if (!pulse->stream)
Packit 1fb8d4
	{
Packit 1fb8d4
		pa_threaded_mainloop_unlock(pulse->mainloop);
Packit 1fb8d4
		WLog_Print(pulse->log, WLOG_DEBUG, "pa_stream_new failed (%d)",
Packit 1fb8d4
		           pa_context_errno(pulse->context));
Packit 1fb8d4
		return pa_context_errno(pulse->context);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	pulse->bytes_per_frame = pa_frame_size(&pulse->sample_spec);
Packit Service 5a9772
	pa_stream_set_state_callback(pulse->stream, audin_pulse_stream_state_callback, pulse);
Packit Service 5a9772
	pa_stream_set_read_callback(pulse->stream, audin_pulse_stream_request_callback, pulse);
Packit Service 5a9772
	buffer_attr.maxlength = (UINT32)-1;
Packit Service 5a9772
	buffer_attr.tlength = (UINT32)-1;
Packit Service 5a9772
	buffer_attr.prebuf = (UINT32)-1;
Packit Service 5a9772
	buffer_attr.minreq = (UINT32)-1;
Packit 1fb8d4
	/* 500ms latency */
Packit 1fb8d4
	buffer_attr.fragsize = pulse->bytes_per_frame * pulse->frames_per_packet;
Packit 1fb8d4
Packit 1fb8d4
	if (buffer_attr.fragsize % pulse->format.nBlockAlign)
Packit Service 5a9772
		buffer_attr.fragsize +=
Packit Service 5a9772
		    pulse->format.nBlockAlign - buffer_attr.fragsize % pulse->format.nBlockAlign;
Packit 1fb8d4
Packit Service 5a9772
	if (pa_stream_connect_record(pulse->stream, pulse->device_name, &buffer_attr,
Packit Service 5a9772
	                             PA_STREAM_ADJUST_LATENCY) < 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		pa_threaded_mainloop_unlock(pulse->mainloop);
Packit 1fb8d4
		WLog_Print(pulse->log, WLOG_ERROR, "pa_stream_connect_playback failed (%d)",
Packit 1fb8d4
		           pa_context_errno(pulse->context));
Packit 1fb8d4
		return pa_context_errno(pulse->context);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	for (;;)
Packit 1fb8d4
	{
Packit 1fb8d4
		state = pa_stream_get_state(pulse->stream);
Packit 1fb8d4
Packit 1fb8d4
		if (state == PA_STREAM_READY)
Packit 1fb8d4
			break;
Packit 1fb8d4
Packit 1fb8d4
		if (!PA_STREAM_IS_GOOD(state))
Packit 1fb8d4
		{
Packit 1fb8d4
			audin_pulse_close(device);
Packit Service 5a9772
			WLog_Print(pulse->log, WLOG_ERROR, "bad stream state (%s: %d)",
Packit Service 5a9772
			           pulse_stream_state_string(state), pa_context_errno(pulse->context));
Packit 1fb8d4
			pa_threaded_mainloop_unlock(pulse->mainloop);
Packit 1fb8d4
			return pa_context_errno(pulse->context);
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		pa_threaded_mainloop_wait(pulse->mainloop);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	pa_threaded_mainloop_unlock(pulse->mainloop);
Packit 1fb8d4
	pulse->buffer_frames = 0;
Packit 1fb8d4
	WLog_Print(pulse->log, WLOG_DEBUG, "connected");
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
static UINT audin_pulse_parse_addin_args(AudinPulseDevice* device, ADDIN_ARGV* args)
Packit 1fb8d4
{
Packit 1fb8d4
	int status;
Packit 1fb8d4
	DWORD flags;
Packit 1fb8d4
	COMMAND_LINE_ARGUMENT_A* arg;
Packit Service 5a9772
	AudinPulseDevice* pulse = (AudinPulseDevice*)device;
Packit Service 5a9772
	COMMAND_LINE_ARGUMENT_A audin_pulse_args[] = { { "dev", COMMAND_LINE_VALUE_REQUIRED, "<device>",
Packit Service 5a9772
		                                             NULL, NULL, -1, NULL, "audio device name" },
Packit Service 5a9772
		                                           { NULL, 0, NULL, NULL, NULL, -1, NULL, NULL } };
Packit Service 5a9772
Packit Service 5a9772
	flags =
Packit Service 5a9772
	    COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
Packit Service 5a9772
	status = CommandLineParseArgumentsA(args->argc, args->argv, audin_pulse_args, flags, pulse,
Packit Service 5a9772
	                                    NULL, NULL);
Packit 1fb8d4
Packit 1fb8d4
	if (status < 0)
Packit 1fb8d4
		return ERROR_INVALID_PARAMETER;
Packit 1fb8d4
Packit 1fb8d4
	arg = audin_pulse_args;
Packit 1fb8d4
Packit 1fb8d4
	do
Packit 1fb8d4
	{
Packit 1fb8d4
		if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
Packit 1fb8d4
			continue;
Packit 1fb8d4
Packit Service 5a9772
		CommandLineSwitchStart(arg) CommandLineSwitchCase(arg, "dev")
Packit 1fb8d4
		{
Packit 1fb8d4
			pulse->device_name = _strdup(arg->Value);
Packit 1fb8d4
Packit 1fb8d4
			if (!pulse->device_name)
Packit 1fb8d4
			{
Packit 1fb8d4
				WLog_Print(pulse->log, WLOG_ERROR, "_strdup failed!");
Packit 1fb8d4
				return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
			}
Packit 1fb8d4
		}
Packit 1fb8d4
		CommandLineSwitchEnd(arg)
Packit Service 5a9772
	} while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
Packit 1fb8d4
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
#ifdef BUILTIN_CHANNELS
Packit Service 5a9772
#define freerdp_audin_client_subsystem_entry pulse_freerdp_audin_client_subsystem_entry
Packit 1fb8d4
#else
Packit Service 5a9772
#define freerdp_audin_client_subsystem_entry FREERDP_API freerdp_audin_client_subsystem_entry
Packit 1fb8d4
#endif
Packit 1fb8d4
Packit 1fb8d4
/**
Packit 1fb8d4
 * Function description
Packit 1fb8d4
 *
Packit 1fb8d4
 * @return 0 on success, otherwise a Win32 error code
Packit 1fb8d4
 */
Packit 1fb8d4
UINT freerdp_audin_client_subsystem_entry(PFREERDP_AUDIN_DEVICE_ENTRY_POINTS pEntryPoints)
Packit 1fb8d4
{
Packit 1fb8d4
	ADDIN_ARGV* args;
Packit 1fb8d4
	AudinPulseDevice* pulse;
Packit 1fb8d4
	UINT error;
Packit Service 5a9772
	pulse = (AudinPulseDevice*)calloc(1, sizeof(AudinPulseDevice));
Packit 1fb8d4
Packit 1fb8d4
	if (!pulse)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "calloc failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	pulse->log = WLog_Get(TAG);
Packit 1fb8d4
	pulse->iface.Open = audin_pulse_open;
Packit 1fb8d4
	pulse->iface.FormatSupported = audin_pulse_format_supported;
Packit 1fb8d4
	pulse->iface.SetFormat = audin_pulse_set_format;
Packit 1fb8d4
	pulse->iface.Close = audin_pulse_close;
Packit 1fb8d4
	pulse->iface.Free = audin_pulse_free;
Packit 1fb8d4
	pulse->rdpcontext = pEntryPoints->rdpcontext;
Packit 1fb8d4
	args = pEntryPoints->args;
Packit 1fb8d4
Packit 1fb8d4
	if ((error = audin_pulse_parse_addin_args(pulse, args)))
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_Print(pulse->log, WLOG_ERROR,
Packit Service 5a9772
		           "audin_pulse_parse_addin_args failed with error %" PRIu32 "!", error);
Packit 1fb8d4
		goto error_out;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	pulse->mainloop = pa_threaded_mainloop_new();
Packit 1fb8d4
Packit 1fb8d4
	if (!pulse->mainloop)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_Print(pulse->log, WLOG_ERROR, "pa_threaded_mainloop_new failed");
Packit 1fb8d4
		error = CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
		goto error_out;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	pulse->context = pa_context_new(pa_threaded_mainloop_get_api(pulse->mainloop), "freerdp");
Packit 1fb8d4
Packit 1fb8d4
	if (!pulse->context)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_Print(pulse->log, WLOG_ERROR, "pa_context_new failed");
Packit 1fb8d4
		error = CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
		goto error_out;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	pa_context_set_state_callback(pulse->context, audin_pulse_context_state_callback, pulse);
Packit 1fb8d4
Packit Service 5a9772
	if ((error = audin_pulse_connect(&pulse->iface)))
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_Print(pulse->log, WLOG_ERROR, "audin_pulse_connect failed");
Packit 1fb8d4
		goto error_out;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	if ((error = pEntryPoints->pRegisterAudinDevice(pEntryPoints->plugin, &pulse->iface)))
Packit 1fb8d4
	{
Packit Service 5a9772
		WLog_Print(pulse->log, WLOG_ERROR, "RegisterAudinDevice failed with error %" PRIu32 "!",
Packit Service 5a9772
		           error);
Packit 1fb8d4
		goto error_out;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
error_out:
Packit Service 5a9772
	audin_pulse_free(&pulse->iface);
Packit 1fb8d4
	return error;
Packit 1fb8d4
}