Blame channels/rdpsnd/client/alsa/rdpsnd_alsa.c

Packit 1fb8d4
/**
Packit 1fb8d4
 * FreeRDP: A Remote Desktop Protocol Implementation
Packit 1fb8d4
 * Audio Output Virtual Channel
Packit 1fb8d4
 *
Packit 1fb8d4
 * Copyright 2009-2011 Jay Sorg
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/sysinfo.h>
Packit 1fb8d4
#include <winpr/collections.h>
Packit 1fb8d4
Packit 1fb8d4
#include <alsa/asoundlib.h>
Packit 1fb8d4
Packit 1fb8d4
#include <freerdp/types.h>
Packit 1fb8d4
#include <freerdp/codec/dsp.h>
Packit 1fb8d4
#include <freerdp/channels/log.h>
Packit 1fb8d4
Packit 1fb8d4
#include "rdpsnd_main.h"
Packit 1fb8d4
Packit 1fb8d4
typedef struct rdpsnd_alsa_plugin rdpsndAlsaPlugin;
Packit 1fb8d4
Packit 1fb8d4
struct rdpsnd_alsa_plugin
Packit 1fb8d4
{
Packit 1fb8d4
	rdpsndDevicePlugin device;
Packit 1fb8d4
Packit 1fb8d4
	UINT32 latency;
Packit 1fb8d4
	AUDIO_FORMAT aformat;
Packit 1fb8d4
	char* device_name;
Packit 1fb8d4
	snd_pcm_t* pcm_handle;
Packit 1fb8d4
	snd_mixer_t* mixer_handle;
Packit 1fb8d4
Packit 1fb8d4
	UINT32 actual_rate;
Packit 1fb8d4
	snd_pcm_format_t format;
Packit 1fb8d4
	UINT32 actual_channels;
Packit 1fb8d4
Packit 1fb8d4
	snd_pcm_uframes_t buffer_size;
Packit 1fb8d4
	snd_pcm_uframes_t period_size;
Packit 1fb8d4
};
Packit 1fb8d4
Packit Service 5a9772
#define SND_PCM_CHECK(_func, _status)              \
Packit Service 5a9772
	if (_status < 0)                               \
Packit Service 5a9772
	{                                              \
Packit Service 5a9772
		WLog_ERR(TAG, "%s: %d\n", _func, _status); \
Packit Service 5a9772
		return -1;                                 \
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
static int rdpsnd_alsa_set_hw_params(rdpsndAlsaPlugin* alsa)
Packit 1fb8d4
{
Packit 1fb8d4
	int status;
Packit 1fb8d4
	snd_pcm_hw_params_t* hw_params;
Packit 1fb8d4
	snd_pcm_uframes_t buffer_size_max;
Packit 1fb8d4
	status = snd_pcm_hw_params_malloc(&hw_params);
Packit 1fb8d4
	SND_PCM_CHECK("snd_pcm_hw_params_malloc", status);
Packit 1fb8d4
	status = snd_pcm_hw_params_any(alsa->pcm_handle, hw_params);
Packit 1fb8d4
	SND_PCM_CHECK("snd_pcm_hw_params_any", status);
Packit 1fb8d4
	/* Set interleaved read/write access */
Packit Service 5a9772
	status =
Packit Service 5a9772
	    snd_pcm_hw_params_set_access(alsa->pcm_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
Packit 1fb8d4
	SND_PCM_CHECK("snd_pcm_hw_params_set_access", status);
Packit 1fb8d4
	/* Set sample format */
Packit 1fb8d4
	status = snd_pcm_hw_params_set_format(alsa->pcm_handle, hw_params, alsa->format);
Packit 1fb8d4
	SND_PCM_CHECK("snd_pcm_hw_params_set_format", status);
Packit 1fb8d4
	/* Set sample rate */
Packit 1fb8d4
	status = snd_pcm_hw_params_set_rate_near(alsa->pcm_handle, hw_params, &alsa->actual_rate, NULL);
Packit 1fb8d4
	SND_PCM_CHECK("snd_pcm_hw_params_set_rate_near", status);
Packit 1fb8d4
	/* Set number of channels */
Packit 1fb8d4
	status = snd_pcm_hw_params_set_channels(alsa->pcm_handle, hw_params, alsa->actual_channels);
Packit 1fb8d4
	SND_PCM_CHECK("snd_pcm_hw_params_set_channels", status);
Packit 1fb8d4
	/* Get maximum buffer size */
Packit 1fb8d4
	status = snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size_max);
Packit 1fb8d4
	SND_PCM_CHECK("snd_pcm_hw_params_get_buffer_size_max", status);
Packit 1fb8d4
	/**
Packit 1fb8d4
	 * ALSA Parameters
Packit 1fb8d4
	 *
Packit 1fb8d4
	 * http://www.alsa-project.org/main/index.php/FramesPeriods
Packit 1fb8d4
	 *
Packit 1fb8d4
	 * buffer_size = period_size * periods
Packit 1fb8d4
	 * period_bytes = period_size * bytes_per_frame
Packit 1fb8d4
	 * bytes_per_frame = channels * bytes_per_sample
Packit 1fb8d4
	 *
Packit 1fb8d4
	 * A frame is equivalent of one sample being played,
Packit 1fb8d4
	 * irrespective of the number of channels or the number of bits
Packit 1fb8d4
	 *
Packit 1fb8d4
	 * A period is the number of frames in between each hardware interrupt.
Packit 1fb8d4
	 *
Packit 1fb8d4
	 * The buffer size always has to be greater than one period size.
Packit 1fb8d4
	 * Commonly this is (2 * period_size), but some hardware can do 8 periods per buffer.
Packit 1fb8d4
	 * It is also possible for the buffer size to not be an integer multiple of the period size.
Packit 1fb8d4
	 */
Packit 1fb8d4
	int interrupts_per_sec_near = 50;
Packit Service 5a9772
	int bytes_per_sec =
Packit Service 5a9772
	    (alsa->actual_rate * alsa->aformat.wBitsPerSample / 8 * alsa->actual_channels);
Packit 1fb8d4
	alsa->buffer_size = buffer_size_max;
Packit 1fb8d4
	alsa->period_size = (bytes_per_sec / interrupts_per_sec_near);
Packit 1fb8d4
Packit 1fb8d4
	if (alsa->period_size > buffer_size_max)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "Warning: requested sound buffer size %lu, got %lu instead\n",
Packit 1fb8d4
		         alsa->buffer_size, buffer_size_max);
Packit 1fb8d4
		alsa->period_size = (buffer_size_max / 8);
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	/* Set buffer size */
Packit Service 5a9772
	status =
Packit Service 5a9772
	    snd_pcm_hw_params_set_buffer_size_near(alsa->pcm_handle, hw_params, &alsa->buffer_size);
Packit 1fb8d4
	SND_PCM_CHECK("snd_pcm_hw_params_set_buffer_size_near", status);
Packit 1fb8d4
	/* Set period size */
Packit 1fb8d4
	status = snd_pcm_hw_params_set_period_size_near(alsa->pcm_handle, hw_params, &alsa->period_size,
Packit Service 5a9772
	                                                NULL);
Packit 1fb8d4
	SND_PCM_CHECK("snd_pcm_hw_params_set_period_size_near", status);
Packit 1fb8d4
	status = snd_pcm_hw_params(alsa->pcm_handle, hw_params);
Packit 1fb8d4
	SND_PCM_CHECK("snd_pcm_hw_params", status);
Packit 1fb8d4
	snd_pcm_hw_params_free(hw_params);
Packit 1fb8d4
	return 0;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static int rdpsnd_alsa_set_sw_params(rdpsndAlsaPlugin* alsa)
Packit 1fb8d4
{
Packit 1fb8d4
	int status;
Packit 1fb8d4
	snd_pcm_sw_params_t* sw_params;
Packit 1fb8d4
	status = snd_pcm_sw_params_malloc(&sw_params);
Packit 1fb8d4
	SND_PCM_CHECK("snd_pcm_sw_params_malloc", status);
Packit 1fb8d4
	status = snd_pcm_sw_params_current(alsa->pcm_handle, sw_params);
Packit 1fb8d4
	SND_PCM_CHECK("snd_pcm_sw_params_current", status);
Packit 1fb8d4
	status = snd_pcm_sw_params_set_avail_min(alsa->pcm_handle, sw_params,
Packit Service 5a9772
	                                         (alsa->aformat.nChannels * alsa->actual_channels));
Packit 1fb8d4
	SND_PCM_CHECK("snd_pcm_sw_params_set_avail_min", status);
Packit 1fb8d4
	status = snd_pcm_sw_params_set_start_threshold(alsa->pcm_handle, sw_params,
Packit Service 5a9772
	                                               alsa->aformat.nBlockAlign);
Packit 1fb8d4
	SND_PCM_CHECK("snd_pcm_sw_params_set_start_threshold", status);
Packit 1fb8d4
	status = snd_pcm_sw_params(alsa->pcm_handle, sw_params);
Packit 1fb8d4
	SND_PCM_CHECK("snd_pcm_sw_params", status);
Packit 1fb8d4
	snd_pcm_sw_params_free(sw_params);
Packit 1fb8d4
	status = snd_pcm_prepare(alsa->pcm_handle);
Packit 1fb8d4
	SND_PCM_CHECK("snd_pcm_prepare", status);
Packit 1fb8d4
	return 0;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static int rdpsnd_alsa_validate_params(rdpsndAlsaPlugin* alsa)
Packit 1fb8d4
{
Packit 1fb8d4
	int status;
Packit 1fb8d4
	snd_pcm_uframes_t buffer_size;
Packit 1fb8d4
	snd_pcm_uframes_t period_size;
Packit 1fb8d4
	status = snd_pcm_get_params(alsa->pcm_handle, &buffer_size, &period_size);
Packit 1fb8d4
	SND_PCM_CHECK("snd_pcm_get_params", status);
Packit 1fb8d4
	return 0;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static int rdpsnd_alsa_set_params(rdpsndAlsaPlugin* alsa)
Packit 1fb8d4
{
Packit 1fb8d4
	snd_pcm_drop(alsa->pcm_handle);
Packit 1fb8d4
Packit 1fb8d4
	if (rdpsnd_alsa_set_hw_params(alsa) < 0)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	if (rdpsnd_alsa_set_sw_params(alsa) < 0)
Packit 1fb8d4
		return -1;
Packit 1fb8d4
Packit 1fb8d4
	return rdpsnd_alsa_validate_params(alsa);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static BOOL rdpsnd_alsa_set_format(rdpsndDevicePlugin* device, const AUDIO_FORMAT* format,
Packit 1fb8d4
                                   UINT32 latency)
Packit 1fb8d4
{
Packit Service 5a9772
	rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
Packit 1fb8d4
Packit 1fb8d4
	if (format)
Packit 1fb8d4
	{
Packit 1fb8d4
		alsa->aformat = *format;
Packit 1fb8d4
		alsa->actual_rate = format->nSamplesPerSec;
Packit 1fb8d4
		alsa->actual_channels = format->nChannels;
Packit 1fb8d4
Packit 1fb8d4
		switch (format->wFormatTag)
Packit 1fb8d4
		{
Packit 1fb8d4
			case WAVE_FORMAT_PCM:
Packit 1fb8d4
				switch (format->wBitsPerSample)
Packit 1fb8d4
				{
Packit 1fb8d4
					case 8:
Packit 1fb8d4
						alsa->format = SND_PCM_FORMAT_S8;
Packit 1fb8d4
						break;
Packit 1fb8d4
Packit 1fb8d4
					case 16:
Packit 1fb8d4
						alsa->format = SND_PCM_FORMAT_S16_LE;
Packit 1fb8d4
						break;
Packit 1fb8d4
Packit 1fb8d4
					default:
Packit 1fb8d4
						return FALSE;
Packit 1fb8d4
				}
Packit 1fb8d4
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			case WAVE_FORMAT_ALAW:
Packit 1fb8d4
			case WAVE_FORMAT_MULAW:
Packit 1fb8d4
				break;
Packit 1fb8d4
Packit 1fb8d4
			default:
Packit 1fb8d4
				return FALSE;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	alsa->latency = latency;
Packit 1fb8d4
	return (rdpsnd_alsa_set_params(alsa) == 0);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static void rdpsnd_alsa_close_mixer(rdpsndAlsaPlugin* alsa)
Packit 1fb8d4
{
Packit 1fb8d4
	if (alsa && alsa->mixer_handle)
Packit 1fb8d4
	{
Packit 1fb8d4
		snd_mixer_close(alsa->mixer_handle);
Packit 1fb8d4
		alsa->mixer_handle = NULL;
Packit 1fb8d4
	}
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static BOOL rdpsnd_alsa_open_mixer(rdpsndAlsaPlugin* alsa)
Packit 1fb8d4
{
Packit 1fb8d4
	int status;
Packit 1fb8d4
Packit 1fb8d4
	if (alsa->mixer_handle)
Packit 1fb8d4
		return TRUE;
Packit 1fb8d4
Packit 1fb8d4
	status = snd_mixer_open(&alsa->mixer_handle, 0);
Packit 1fb8d4
Packit 1fb8d4
	if (status < 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "snd_mixer_open failed");
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	status = snd_mixer_attach(alsa->mixer_handle, alsa->device_name);
Packit 1fb8d4
Packit 1fb8d4
	if (status < 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "snd_mixer_attach failed");
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	status = snd_mixer_selem_register(alsa->mixer_handle, NULL, NULL);
Packit 1fb8d4
Packit 1fb8d4
	if (status < 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "snd_mixer_selem_register failed");
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	status = snd_mixer_load(alsa->mixer_handle);
Packit 1fb8d4
Packit 1fb8d4
	if (status < 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "snd_mixer_load failed");
Packit 1fb8d4
		goto fail;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
fail:
Packit 1fb8d4
	rdpsnd_alsa_close_mixer(alsa);
Packit 1fb8d4
	return FALSE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static void rdpsnd_alsa_pcm_close(rdpsndAlsaPlugin* alsa)
Packit 1fb8d4
{
Packit 1fb8d4
	if (alsa && alsa->pcm_handle)
Packit 1fb8d4
	{
Packit 1fb8d4
		snd_pcm_drain(alsa->pcm_handle);
Packit 1fb8d4
		snd_pcm_close(alsa->pcm_handle);
Packit 1fb8d4
		alsa->pcm_handle = 0;
Packit 1fb8d4
	}
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static BOOL rdpsnd_alsa_open(rdpsndDevicePlugin* device, const AUDIO_FORMAT* format, UINT32 latency)
Packit 1fb8d4
{
Packit 1fb8d4
	int mode;
Packit 1fb8d4
	int status;
Packit Service 5a9772
	rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
Packit 1fb8d4
Packit 1fb8d4
	if (alsa->pcm_handle)
Packit 1fb8d4
		return TRUE;
Packit 1fb8d4
Packit 1fb8d4
	mode = 0;
Packit 1fb8d4
	/*mode |= SND_PCM_NONBLOCK;*/
Packit 1fb8d4
	status = snd_pcm_open(&alsa->pcm_handle, alsa->device_name, SND_PCM_STREAM_PLAYBACK, mode);
Packit 1fb8d4
Packit 1fb8d4
	if (status < 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "snd_pcm_open failed");
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit Service 5a9772
	return rdpsnd_alsa_set_format(device, format, latency) && rdpsnd_alsa_open_mixer(alsa);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static void rdpsnd_alsa_close(rdpsndDevicePlugin* device)
Packit 1fb8d4
{
Packit Service 5a9772
	rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
Packit 1fb8d4
Packit 1fb8d4
	if (!alsa)
Packit 1fb8d4
		return;
Packit 1fb8d4
Packit 1fb8d4
	rdpsnd_alsa_close_mixer(alsa);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static void rdpsnd_alsa_free(rdpsndDevicePlugin* device)
Packit 1fb8d4
{
Packit Service 5a9772
	rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
Packit 1fb8d4
	rdpsnd_alsa_pcm_close(alsa);
Packit 1fb8d4
	rdpsnd_alsa_close_mixer(alsa);
Packit 1fb8d4
	free(alsa->device_name);
Packit 1fb8d4
	free(alsa);
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static BOOL rdpsnd_alsa_format_supported(rdpsndDevicePlugin* device, const AUDIO_FORMAT* format)
Packit 1fb8d4
{
Packit 1fb8d4
	switch (format->wFormatTag)
Packit 1fb8d4
	{
Packit 1fb8d4
		case WAVE_FORMAT_PCM:
Packit Service 5a9772
			if (format->cbSize == 0 && format->nSamplesPerSec <= 48000 &&
Packit 1fb8d4
			    (format->wBitsPerSample == 8 || format->wBitsPerSample == 16) &&
Packit 1fb8d4
			    (format->nChannels == 1 || format->nChannels == 2))
Packit 1fb8d4
			{
Packit 1fb8d4
				return TRUE;
Packit 1fb8d4
			}
Packit 1fb8d4
Packit 1fb8d4
			break;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return FALSE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static UINT32 rdpsnd_alsa_get_volume(rdpsndDevicePlugin* device)
Packit 1fb8d4
{
Packit 1fb8d4
	long volume_min;
Packit 1fb8d4
	long volume_max;
Packit 1fb8d4
	long volume_left;
Packit 1fb8d4
	long volume_right;
Packit 1fb8d4
	UINT32 dwVolume;
Packit 1fb8d4
	UINT16 dwVolumeLeft;
Packit 1fb8d4
	UINT16 dwVolumeRight;
Packit 1fb8d4
	snd_mixer_elem_t* elem;
Packit Service 5a9772
	rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
Packit Service 5a9772
	dwVolumeLeft = ((50 * 0xFFFF) / 100);  /* 50% */
Packit 1fb8d4
	dwVolumeRight = ((50 * 0xFFFF) / 100); /* 50% */
Packit 1fb8d4
Packit 1fb8d4
	if (!rdpsnd_alsa_open_mixer(alsa))
Packit 1fb8d4
		return 0;
Packit 1fb8d4
Packit 1fb8d4
	for (elem = snd_mixer_first_elem(alsa->mixer_handle); elem; elem = snd_mixer_elem_next(elem))
Packit 1fb8d4
	{
Packit 1fb8d4
		if (snd_mixer_selem_has_playback_volume(elem))
Packit 1fb8d4
		{
Packit 1fb8d4
			snd_mixer_selem_get_playback_volume_range(elem, &volume_min, &volume_max);
Packit 1fb8d4
			snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_FRONT_LEFT, &volume_left);
Packit 1fb8d4
			snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_FRONT_RIGHT, &volume_right);
Packit Service 5a9772
			dwVolumeLeft =
Packit Service 5a9772
			    (UINT16)(((volume_left * 0xFFFF) - volume_min) / (volume_max - volume_min));
Packit Service 5a9772
			dwVolumeRight =
Packit Service 5a9772
			    (UINT16)(((volume_right * 0xFFFF) - volume_min) / (volume_max - volume_min));
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	dwVolume = (dwVolumeLeft << 16) | dwVolumeRight;
Packit 1fb8d4
	return dwVolume;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static BOOL rdpsnd_alsa_set_volume(rdpsndDevicePlugin* device, UINT32 value)
Packit 1fb8d4
{
Packit 1fb8d4
	long left;
Packit 1fb8d4
	long right;
Packit 1fb8d4
	long volume_min;
Packit 1fb8d4
	long volume_max;
Packit 1fb8d4
	long volume_left;
Packit 1fb8d4
	long volume_right;
Packit 1fb8d4
	snd_mixer_elem_t* elem;
Packit Service 5a9772
	rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
Packit 1fb8d4
Packit 1fb8d4
	if (!rdpsnd_alsa_open_mixer(alsa))
Packit 1fb8d4
		return FALSE;
Packit 1fb8d4
Packit 1fb8d4
	left = (value & 0xFFFF);
Packit 1fb8d4
	right = ((value >> 16) & 0xFFFF);
Packit 1fb8d4
Packit 1fb8d4
	for (elem = snd_mixer_first_elem(alsa->mixer_handle); elem; elem = snd_mixer_elem_next(elem))
Packit 1fb8d4
	{
Packit 1fb8d4
		if (snd_mixer_selem_has_playback_volume(elem))
Packit 1fb8d4
		{
Packit 1fb8d4
			snd_mixer_selem_get_playback_volume_range(elem, &volume_min, &volume_max);
Packit 1fb8d4
			volume_left = volume_min + (left * (volume_max - volume_min)) / 0xFFFF;
Packit 1fb8d4
			volume_right = volume_min + (right * (volume_max - volume_min)) / 0xFFFF;
Packit 1fb8d4
Packit Service 5a9772
			if ((snd_mixer_selem_set_playback_volume(elem, SND_MIXER_SCHN_FRONT_LEFT, volume_left) <
Packit Service 5a9772
			     0) ||
Packit Service 5a9772
			    (snd_mixer_selem_set_playback_volume(elem, SND_MIXER_SCHN_FRONT_RIGHT,
Packit Service 5a9772
			                                         volume_right) < 0))
Packit 1fb8d4
			{
Packit 1fb8d4
				WLog_ERR(TAG, "error setting the volume\n");
Packit 1fb8d4
				return FALSE;
Packit 1fb8d4
			}
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return TRUE;
Packit 1fb8d4
}
Packit 1fb8d4
Packit 1fb8d4
static UINT rdpsnd_alsa_play(rdpsndDevicePlugin* device, const BYTE* data, size_t size)
Packit 1fb8d4
{
Packit 1fb8d4
	UINT latency;
Packit 1fb8d4
	size_t offset;
Packit 1fb8d4
	int frame_size;
Packit Service 5a9772
	rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
Packit 1fb8d4
	offset = 0;
Packit 1fb8d4
	frame_size = alsa->actual_channels * alsa->aformat.wBitsPerSample / 8;
Packit 1fb8d4
Packit 1fb8d4
	while (offset < size)
Packit 1fb8d4
	{
Packit Service 5a9772
		snd_pcm_sframes_t status =
Packit Service 5a9772
		    snd_pcm_writei(alsa->pcm_handle, &data[offset], (size - offset) / frame_size);
Packit 1fb8d4
Packit 1fb8d4
		if (status < 0)
Packit 1fb8d4
			status = snd_pcm_recover(alsa->pcm_handle, status, 0);
Packit 1fb8d4
Packit 1fb8d4
		if (status < 0)
Packit 1fb8d4
		{
Packit Service 5a9772
			WLog_ERR(TAG, "status: %d\n", status);
Packit 1fb8d4
			rdpsnd_alsa_close(device);
Packit 1fb8d4
			rdpsnd_alsa_open(device, NULL, alsa->latency);
Packit 1fb8d4
			break;
Packit 1fb8d4
		}
Packit 1fb8d4
Packit 1fb8d4
		offset += status * frame_size;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	{
Packit 1fb8d4
		snd_pcm_sframes_t available, delay;
Packit 1fb8d4
		int rc = snd_pcm_avail_delay(alsa->pcm_handle, &available, &delay);
Packit 1fb8d4
Packit 1fb8d4
		if (rc != 0)
Packit 1fb8d4
			latency = 0;
Packit 1fb8d4
		else if (available == 0) /* Get [ms] from number of samples */
Packit 1fb8d4
			latency = delay * 1000 / alsa->actual_rate;
Packit 1fb8d4
		else
Packit 1fb8d4
			latency = 0;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	return latency + alsa->latency;
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 rdpsnd_alsa_parse_addin_args(rdpsndDevicePlugin* device, ADDIN_ARGV* args)
Packit 1fb8d4
{
Packit 1fb8d4
	int status;
Packit 1fb8d4
	DWORD flags;
Packit 1fb8d4
	COMMAND_LINE_ARGUMENT_A* arg;
Packit Service 5a9772
	rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
Packit Service 5a9772
	COMMAND_LINE_ARGUMENT_A rdpsnd_alsa_args[] = { { "dev", COMMAND_LINE_VALUE_REQUIRED, "<device>",
Packit Service 5a9772
		                                             NULL, NULL, -1, NULL, "device" },
Packit Service 5a9772
		                                           { NULL, 0, NULL, NULL, NULL, -1, NULL, NULL } };
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, rdpsnd_alsa_args, flags, alsa, NULL,
Packit Service 5a9772
	                                    NULL);
Packit 1fb8d4
Packit 1fb8d4
	if (status < 0)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "CommandLineParseArgumentsA failed!");
Packit 1fb8d4
		return CHANNEL_RC_INITIALIZATION_ERROR;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	arg = rdpsnd_alsa_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
			alsa->device_name = _strdup(arg->Value);
Packit 1fb8d4
Packit 1fb8d4
			if (!alsa->device_name)
Packit 1fb8d4
				return CHANNEL_RC_NO_MEMORY;
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_rdpsnd_client_subsystem_entry alsa_freerdp_rdpsnd_client_subsystem_entry
Packit 1fb8d4
#else
Packit Service 5a9772
#define freerdp_rdpsnd_client_subsystem_entry FREERDP_API freerdp_rdpsnd_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_rdpsnd_client_subsystem_entry(PFREERDP_RDPSND_DEVICE_ENTRY_POINTS pEntryPoints)
Packit 1fb8d4
{
Packit 1fb8d4
	ADDIN_ARGV* args;
Packit 1fb8d4
	rdpsndAlsaPlugin* alsa;
Packit 1fb8d4
	UINT error;
Packit Service 5a9772
	alsa = (rdpsndAlsaPlugin*)calloc(1, sizeof(rdpsndAlsaPlugin));
Packit 1fb8d4
Packit 1fb8d4
	if (!alsa)
Packit 1fb8d4
	{
Packit 1fb8d4
		WLog_ERR(TAG, "calloc failed!");
Packit 1fb8d4
		return CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	alsa->device.Open = rdpsnd_alsa_open;
Packit 1fb8d4
	alsa->device.FormatSupported = rdpsnd_alsa_format_supported;
Packit 1fb8d4
	alsa->device.GetVolume = rdpsnd_alsa_get_volume;
Packit 1fb8d4
	alsa->device.SetVolume = rdpsnd_alsa_set_volume;
Packit 1fb8d4
	alsa->device.Play = rdpsnd_alsa_play;
Packit 1fb8d4
	alsa->device.Close = rdpsnd_alsa_close;
Packit 1fb8d4
	alsa->device.Free = rdpsnd_alsa_free;
Packit 1fb8d4
	args = pEntryPoints->args;
Packit 1fb8d4
Packit 1fb8d4
	if (args->argc > 1)
Packit 1fb8d4
	{
Packit Service 5a9772
		if ((error = rdpsnd_alsa_parse_addin_args((rdpsndDevicePlugin*)alsa, args)))
Packit 1fb8d4
		{
Packit Service 5a9772
			WLog_ERR(TAG, "rdpsnd_alsa_parse_addin_args failed with error %" PRIu32 "", error);
Packit 1fb8d4
			goto error_parse_args;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	if (!alsa->device_name)
Packit 1fb8d4
	{
Packit 1fb8d4
		alsa->device_name = _strdup("default");
Packit 1fb8d4
Packit 1fb8d4
		if (!alsa->device_name)
Packit 1fb8d4
		{
Packit 1fb8d4
			WLog_ERR(TAG, "_strdup failed!");
Packit 1fb8d4
			error = CHANNEL_RC_NO_MEMORY;
Packit 1fb8d4
			goto error_strdup;
Packit 1fb8d4
		}
Packit 1fb8d4
	}
Packit 1fb8d4
Packit 1fb8d4
	alsa->pcm_handle = 0;
Packit 1fb8d4
	alsa->actual_rate = 22050;
Packit 1fb8d4
	alsa->format = SND_PCM_FORMAT_S16_LE;
Packit 1fb8d4
	alsa->actual_channels = 2;
Packit Service 5a9772
	pEntryPoints->pRegisterRdpsndDevice(pEntryPoints->rdpsnd, (rdpsndDevicePlugin*)alsa);
Packit 1fb8d4
	return CHANNEL_RC_OK;
Packit 1fb8d4
error_strdup:
Packit 1fb8d4
	free(alsa->device_name);
Packit 1fb8d4
error_parse_args:
Packit 1fb8d4
	free(alsa);
Packit 1fb8d4
	return error;
Packit 1fb8d4
}