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

526 lines
13 KiB
C
Raw Normal View History

2011-08-15 12:28:52 +04:00
/**
2012-10-09 07:02:04 +04:00
* FreeRDP: A Remote Desktop Protocol Implementation
2011-08-15 12:28:52 +04:00
* Audio Output Virtual Channel
*
* Copyright 2009-2011 Jay Sorg
* Copyright 2010-2011 Vic Lee
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
2011-08-15 12:28:52 +04:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winpr/crt.h>
#include <winpr/cmdline.h>
2011-08-15 12:28:52 +04:00
#include <alsa/asoundlib.h>
2011-08-15 12:28:52 +04:00
#include <freerdp/types.h>
#include <freerdp/utils/dsp.h>
#include <freerdp/utils/svc_plugin.h>
#include "rdpsnd_main.h"
typedef struct rdpsnd_alsa_plugin rdpsndAlsaPlugin;
2011-08-15 12:28:52 +04:00
struct rdpsnd_alsa_plugin
{
rdpsndDevicePlugin device;
char* device_name;
snd_pcm_t* out_handle;
2012-05-29 19:36:47 +04:00
snd_mixer_t* mixer_handle;
2012-10-09 11:26:39 +04:00
UINT32 source_rate;
UINT32 actual_rate;
2011-08-15 12:28:52 +04:00
snd_pcm_format_t format;
2012-10-09 11:26:39 +04:00
UINT32 source_channels;
UINT32 actual_channels;
2011-08-15 12:28:52 +04:00
int bytes_per_channel;
int wformat;
int block_size;
int latency;
BYTE* audio_data;
UINT32 audio_data_size;
UINT32 audio_data_left;
snd_pcm_uframes_t chunk_size;
FREERDP_DSP_CONTEXT* dsp_context;
2011-08-15 12:28:52 +04:00
};
static void rdpsnd_alsa_set_params(rdpsndAlsaPlugin* alsa)
{
2012-10-03 07:01:16 +04:00
int status;
2011-08-15 12:28:52 +04:00
snd_pcm_hw_params_t* hw_params;
snd_pcm_sw_params_t* sw_params;
snd_pcm_uframes_t frames;
snd_pcm_uframes_t start_threshold;
2011-08-15 12:28:52 +04:00
snd_pcm_drop(alsa->out_handle);
2012-10-03 07:01:16 +04:00
status = snd_pcm_hw_params_malloc(&hw_params);
if (status < 0)
2011-08-15 12:28:52 +04:00
{
DEBUG_WARN("snd_pcm_hw_params_malloc failed");
return;
}
2012-10-03 07:01:16 +04:00
2011-08-15 12:28:52 +04:00
snd_pcm_hw_params_any(alsa->out_handle, hw_params);
2012-10-03 07:01:16 +04:00
snd_pcm_hw_params_set_access(alsa->out_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
snd_pcm_hw_params_set_format(alsa->out_handle, hw_params, alsa->format);
snd_pcm_hw_params_set_rate_near(alsa->out_handle, hw_params, &alsa->actual_rate, NULL);
snd_pcm_hw_params_set_channels_near(alsa->out_handle, hw_params, &alsa->actual_channels);
snd_pcm_hw_params_get_period_size(hw_params, &alsa->chunk_size, 0);
alsa->audio_data_left = 0;
2012-10-03 07:01:16 +04:00
if (alsa->latency < 0)
frames = alsa->actual_rate * 4 / 10; /* Default to 400ms buffer */
else
frames = alsa->latency * alsa->actual_rate * 2 / 1000; /* Double of the latency */
2012-10-03 07:01:16 +04:00
if (frames < alsa->actual_rate / 2)
frames = alsa->actual_rate / 2; /* Minimum 0.5-second buffer */
2012-10-03 07:01:16 +04:00
snd_pcm_hw_params_set_buffer_size_near(alsa->out_handle, hw_params, &frames);
2011-08-15 12:28:52 +04:00
snd_pcm_hw_params(alsa->out_handle, hw_params);
snd_pcm_hw_params_free(hw_params);
2012-10-03 07:01:16 +04:00
status = snd_pcm_sw_params_malloc(&sw_params);
if (status < 0)
2011-08-15 12:28:52 +04:00
{
DEBUG_WARN("snd_pcm_sw_params_malloc failed");
return;
}
2012-10-03 07:01:16 +04:00
2011-08-15 12:28:52 +04:00
snd_pcm_sw_params_current(alsa->out_handle, sw_params);
2012-10-03 07:01:16 +04:00
if (alsa->latency == 0)
start_threshold = 0;
else
start_threshold = frames / 2;
2012-10-03 07:01:16 +04:00
snd_pcm_sw_params_set_start_threshold(alsa->out_handle, sw_params, start_threshold);
2011-08-15 12:28:52 +04:00
snd_pcm_sw_params(alsa->out_handle, sw_params);
snd_pcm_sw_params_free(sw_params);
snd_pcm_prepare(alsa->out_handle);
DEBUG_SVC("hardware buffer %d frames, playback buffer %.2g seconds",
2012-10-03 07:01:16 +04:00
(int) frames, (double) frames / 2.0 / (double) alsa->actual_rate);
if ((alsa->actual_rate != alsa->source_rate) || (alsa->actual_channels != alsa->source_channels))
2011-08-15 12:28:52 +04:00
{
DEBUG_SVC("actual rate %d / channel %d is different from source rate %d / channel %d, resampling required.",
alsa->actual_rate, alsa->actual_channels, alsa->source_rate, alsa->source_channels);
}
}
static void rdpsnd_alsa_set_format(rdpsndDevicePlugin* device, rdpsndFormat* format, int latency)
2011-08-15 12:28:52 +04:00
{
rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
if (format != NULL)
{
alsa->source_rate = format->nSamplesPerSec;
alsa->actual_rate = format->nSamplesPerSec;
alsa->source_channels = format->nChannels;
alsa->actual_channels = format->nChannels;
2012-10-03 07:01:16 +04:00
2011-08-15 12:28:52 +04:00
switch (format->wFormatTag)
{
case 1: /* PCM */
switch (format->wBitsPerSample)
{
case 8:
alsa->format = SND_PCM_FORMAT_S8;
alsa->bytes_per_channel = 1;
break;
case 16:
alsa->format = SND_PCM_FORMAT_S16_LE;
alsa->bytes_per_channel = 2;
break;
}
break;
2012-05-24 17:26:17 +04:00
case 2: /* MS ADPCM */
2011-08-15 12:28:52 +04:00
case 0x11: /* IMA ADPCM */
alsa->format = SND_PCM_FORMAT_S16_LE;
alsa->bytes_per_channel = 2;
break;
}
2012-10-03 07:01:16 +04:00
2011-08-15 12:28:52 +04:00
alsa->wformat = format->wFormatTag;
alsa->block_size = format->nBlockAlign;
}
alsa->latency = latency;
2011-08-15 12:28:52 +04:00
rdpsnd_alsa_set_params(alsa);
}
2012-05-29 19:36:47 +04:00
static void rdpsnd_alsa_open_mixer(rdpsndAlsaPlugin* alsa)
{
2012-10-03 07:01:16 +04:00
int status;
2012-05-29 19:36:47 +04:00
snd_mixer_t* handle;
2012-10-03 07:01:16 +04:00
status = snd_mixer_open(&handle, 0);
if (status < 0)
2012-05-29 19:36:47 +04:00
{
DEBUG_WARN("snd_mixer_open failed");
return;
}
2012-10-03 07:01:16 +04:00
status = snd_mixer_attach(handle, alsa->device_name);
if (status < 0)
2012-05-29 19:36:47 +04:00
{
DEBUG_WARN("snd_mixer_attach failed");
snd_mixer_close(handle);
return;
}
2012-10-03 07:01:16 +04:00
status = snd_mixer_selem_register(handle, NULL, NULL);
if (status < 0)
2012-05-29 19:36:47 +04:00
{
DEBUG_WARN("snd_mixer_selem_register failed");
snd_mixer_close(handle);
return;
}
2012-10-03 07:01:16 +04:00
status = snd_mixer_load(handle);
if (status < 0)
2012-05-29 19:36:47 +04:00
{
DEBUG_WARN("snd_mixer_load failed");
snd_mixer_close(handle);
return;
}
alsa->mixer_handle = handle;
}
static void rdpsnd_alsa_open(rdpsndDevicePlugin* device, rdpsndFormat* format, int latency)
2011-08-15 12:28:52 +04:00
{
2012-10-03 07:01:16 +04:00
int status;
rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*) device;
2011-08-15 12:28:52 +04:00
if (alsa->out_handle != 0)
return;
DEBUG_SVC("opening");
2012-10-03 07:01:16 +04:00
status = snd_pcm_open(&alsa->out_handle, alsa->device_name, SND_PCM_STREAM_PLAYBACK, 0);
if (status < 0)
2011-08-15 12:28:52 +04:00
{
DEBUG_WARN("snd_pcm_open failed");
}
else
{
freerdp_dsp_context_reset_adpcm(alsa->dsp_context);
rdpsnd_alsa_set_format(device, format, latency);
2012-05-29 19:36:47 +04:00
rdpsnd_alsa_open_mixer(alsa);
2011-08-15 12:28:52 +04:00
}
}
static void rdpsnd_alsa_close(rdpsndDevicePlugin* device)
{
rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
if (alsa->out_handle != 0)
{
DEBUG_SVC("close");
snd_pcm_drain(alsa->out_handle);
snd_pcm_close(alsa->out_handle);
alsa->out_handle = 0;
}
2012-10-03 07:01:16 +04:00
2012-05-29 19:36:47 +04:00
if (alsa->mixer_handle)
{
snd_mixer_close(alsa->mixer_handle);
alsa->mixer_handle = NULL;
}
2011-08-15 12:28:52 +04:00
}
static void rdpsnd_alsa_free(rdpsndDevicePlugin* device)
{
rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*) device;
2011-08-15 12:28:52 +04:00
rdpsnd_alsa_close(device);
free(alsa->device_name);
if (alsa->audio_data)
free(alsa->audio_data);
freerdp_dsp_context_free(alsa->dsp_context);
free(alsa);
2011-08-15 12:28:52 +04:00
}
static BOOL rdpsnd_alsa_format_supported(rdpsndDevicePlugin* device, rdpsndFormat* format)
2011-08-15 12:28:52 +04:00
{
switch (format->wFormatTag)
{
case 1: /* PCM */
if (format->cbSize == 0 &&
format->nSamplesPerSec <= 48000 &&
(format->wBitsPerSample == 8 || format->wBitsPerSample == 16) &&
(format->nChannels == 1 || format->nChannels == 2))
{
return TRUE;
2011-08-15 12:28:52 +04:00
}
break;
2012-05-24 17:26:17 +04:00
case 2: /* MS ADPCM */
2011-08-15 12:28:52 +04:00
case 0x11: /* IMA ADPCM */
if (format->nSamplesPerSec <= 48000 &&
format->wBitsPerSample == 4 &&
(format->nChannels == 1 || format->nChannels == 2))
{
return TRUE;
2011-08-15 12:28:52 +04:00
}
break;
}
return FALSE;
2011-08-15 12:28:52 +04:00
}
2012-10-09 11:26:39 +04:00
static void rdpsnd_alsa_set_volume(rdpsndDevicePlugin* device, UINT32 value)
2011-08-15 12:28:52 +04:00
{
2012-05-29 19:36:47 +04:00
long left;
long right;
long volume_min;
long volume_max;
long volume_left;
long volume_right;
snd_mixer_elem_t* elem;
2012-10-03 07:01:16 +04:00
rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*) device;
2012-05-29 19:36:47 +04:00
if (!alsa->mixer_handle)
return;
left = (value & 0xFFFF);
right = ((value >> 16) & 0xFFFF);
for (elem = snd_mixer_first_elem(alsa->mixer_handle); elem; elem = snd_mixer_elem_next(elem))
{
if (snd_mixer_selem_has_playback_volume(elem))
{
snd_mixer_selem_get_playback_volume_range(elem, &volume_min, &volume_max);
volume_left = volume_min + (left * (volume_max - volume_min)) / 0xFFFF;
volume_right = volume_min + (right * (volume_max - volume_min)) / 0xFFFF;
snd_mixer_selem_set_playback_volume(elem, SND_MIXER_SCHN_FRONT_LEFT, volume_left);
snd_mixer_selem_set_playback_volume(elem, SND_MIXER_SCHN_FRONT_RIGHT, volume_right);
}
}
2011-08-15 12:28:52 +04:00
}
static void rdpsnd_alsa_play(rdpsndDevicePlugin* device, BYTE* data, int size)
2011-08-15 12:28:52 +04:00
{
BYTE* src;
2011-08-15 12:28:52 +04:00
int len;
2012-10-03 07:01:16 +04:00
int status;
2011-08-15 12:28:52 +04:00
int frames;
int rbytes_per_frame;
int sbytes_per_frame;
BYTE* pindex;
BYTE* end;
2012-10-03 07:01:16 +04:00
rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*) device;
2011-08-15 12:28:52 +04:00
if (alsa->out_handle == 0)
return;
2012-05-24 17:26:17 +04:00
if (alsa->wformat == 2)
{
alsa->dsp_context->decode_ms_adpcm(alsa->dsp_context,
data, size, alsa->source_channels, alsa->block_size);
size = alsa->dsp_context->adpcm_size;
src = alsa->dsp_context->adpcm_buffer;
}
else if (alsa->wformat == 0x11)
2011-08-15 12:28:52 +04:00
{
alsa->dsp_context->decode_ima_adpcm(alsa->dsp_context,
data, size, alsa->source_channels, alsa->block_size);
size = alsa->dsp_context->adpcm_size;
src = alsa->dsp_context->adpcm_buffer;
2011-08-15 12:28:52 +04:00
}
else
{
src = data;
}
sbytes_per_frame = alsa->source_channels * alsa->bytes_per_channel;
rbytes_per_frame = alsa->actual_channels * alsa->bytes_per_channel;
2012-10-03 07:01:16 +04:00
2011-08-15 12:28:52 +04:00
if ((size % sbytes_per_frame) != 0)
{
DEBUG_WARN("error len mod");
return;
}
2012-10-03 07:01:16 +04:00
if ((alsa->source_rate == alsa->actual_rate) && (alsa->source_channels == alsa->actual_channels))
2011-08-15 12:28:52 +04:00
{
2011-08-15 12:28:52 +04:00
}
else
{
alsa->dsp_context->resample(alsa->dsp_context, src, alsa->bytes_per_channel,
2011-08-15 12:28:52 +04:00
alsa->source_channels, alsa->source_rate, size / sbytes_per_frame,
alsa->actual_channels, alsa->actual_rate);
frames = alsa->dsp_context->resampled_frames;
2011-08-15 12:28:52 +04:00
DEBUG_SVC("resampled %d frames at %d to %d frames at %d",
size / sbytes_per_frame, alsa->source_rate, frames, alsa->actual_rate);
2011-08-15 12:28:52 +04:00
size = frames * rbytes_per_frame;
src = alsa->dsp_context->resampled_buffer;
2011-08-15 12:28:52 +04:00
}
if (alsa->audio_data_left + size > alsa->audio_data_size)
{
alsa->audio_data = realloc(alsa->audio_data, alsa->audio_data_left + size);
alsa->audio_data_size = alsa->audio_data_left + size;
}
memcpy(alsa->audio_data + alsa->audio_data_left, src, size);
alsa->audio_data_left += size;
2012-10-03 07:01:16 +04:00
pindex = alsa->audio_data;
end = pindex + alsa->audio_data_left;
while (pindex + alsa->chunk_size * rbytes_per_frame <= end)
2011-08-15 12:28:52 +04:00
{
len = end - pindex;
status = snd_pcm_writei(alsa->out_handle, pindex, alsa->chunk_size);
2012-10-03 07:01:16 +04:00
if (status == -EPIPE)
2011-08-15 12:28:52 +04:00
{
2012-10-03 07:01:16 +04:00
snd_pcm_recover(alsa->out_handle, status, 0);
status = 0;
2011-08-15 12:28:52 +04:00
}
2012-10-03 07:01:16 +04:00
else if (status < 0)
2011-08-15 12:28:52 +04:00
{
2012-10-03 07:01:16 +04:00
DEBUG_WARN("status %d", status);
2011-08-15 12:28:52 +04:00
snd_pcm_close(alsa->out_handle);
alsa->out_handle = 0;
rdpsnd_alsa_open(device, NULL, alsa->latency);
2011-08-15 12:28:52 +04:00
break;
}
2012-10-03 07:01:16 +04:00
pindex += status * rbytes_per_frame;
2011-08-15 12:28:52 +04:00
}
if ((pindex <= end) && (pindex != alsa->audio_data))
{
memcpy(alsa->audio_data, pindex, end - pindex);
alsa->audio_data_left = end - pindex;
}
2011-08-15 12:28:52 +04:00
}
static void rdpsnd_alsa_start(rdpsndDevicePlugin* device)
{
2012-10-03 07:01:16 +04:00
rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*) device;
if (alsa->out_handle == 0)
return;
snd_pcm_start(alsa->out_handle);
}
COMMAND_LINE_ARGUMENT_A rdpsnd_alsa_args[] =
{
{ "dev", COMMAND_LINE_VALUE_REQUIRED, "<device>", NULL, NULL, -1, NULL, "device" },
{ NULL, 0, NULL, NULL, NULL, -1, NULL, NULL }
};
static void rdpsnd_alsa_parse_addin_args(rdpsndDevicePlugin* device, ADDIN_ARGV* args)
{
int status;
DWORD flags;
COMMAND_LINE_ARGUMENT_A* arg;
rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*) device;
flags = COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON;
status = CommandLineParseArgumentsA(args->argc, (const char**) args->argv, rdpsnd_alsa_args, flags, alsa, NULL, NULL);
arg = rdpsnd_alsa_args;
do
{
if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
continue;
CommandLineSwitchStart(arg)
CommandLineSwitchCase(arg, "dev")
{
alsa->device_name = _strdup(arg->Value);
}
CommandLineSwitchEnd(arg)
}
while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
}
#ifdef STATIC_CHANNELS
#define freerdp_rdpsnd_client_subsystem_entry alsa_freerdp_rdpsnd_client_subsystem_entry
#endif
int freerdp_rdpsnd_client_subsystem_entry(PFREERDP_RDPSND_DEVICE_ENTRY_POINTS pEntryPoints)
2011-08-15 12:28:52 +04:00
{
ADDIN_ARGV* args;
2011-08-15 12:28:52 +04:00
rdpsndAlsaPlugin* alsa;
alsa = (rdpsndAlsaPlugin*) malloc(sizeof(rdpsndAlsaPlugin));
ZeroMemory(alsa, sizeof(rdpsndAlsaPlugin));
2011-08-15 12:28:52 +04:00
alsa->device.Open = rdpsnd_alsa_open;
alsa->device.FormatSupported = rdpsnd_alsa_format_supported;
alsa->device.SetFormat = rdpsnd_alsa_set_format;
alsa->device.SetVolume = rdpsnd_alsa_set_volume;
alsa->device.Play = rdpsnd_alsa_play;
alsa->device.Start = rdpsnd_alsa_start;
2011-08-15 12:28:52 +04:00
alsa->device.Close = rdpsnd_alsa_close;
alsa->device.Free = rdpsnd_alsa_free;
args = pEntryPoints->args;
rdpsnd_alsa_parse_addin_args((rdpsndDevicePlugin*) alsa, args);
2012-10-03 07:01:16 +04:00
if (!alsa->device_name)
alsa->device_name = _strdup("default");
2011-08-15 12:28:52 +04:00
alsa->out_handle = 0;
alsa->source_rate = 22050;
alsa->actual_rate = 22050;
alsa->format = SND_PCM_FORMAT_S16_LE;
alsa->source_channels = 2;
alsa->actual_channels = 2;
alsa->bytes_per_channel = 2;
alsa->dsp_context = freerdp_dsp_context_new();
2012-10-03 07:01:16 +04:00
pEntryPoints->pRegisterRdpsndDevice(pEntryPoints->rdpsnd, (rdpsndDevicePlugin*) alsa);
2011-08-15 12:28:52 +04:00
return 0;
}