2011-09-20 10:27:10 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-09-20 10:27:10 +04:00
|
|
|
* Video Redirection Virtual Channel - ALSA Audio Device
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2011-09-20 10:27:10 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2012-11-20 07:31:15 +04:00
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
|
2011-09-20 10:27:10 +04:00
|
|
|
#include <alsa/asoundlib.h>
|
2012-11-20 07:31:15 +04:00
|
|
|
|
2011-09-20 10:27:10 +04:00
|
|
|
#include <freerdp/types.h>
|
2013-02-20 02:47:55 +04:00
|
|
|
#include <freerdp/codec/dsp.h>
|
2011-09-20 10:27:10 +04:00
|
|
|
|
|
|
|
#include "tsmf_audio.h"
|
|
|
|
|
|
|
|
typedef struct _TSMFALSAAudioDevice
|
|
|
|
{
|
|
|
|
ITSMFAudioDevice iface;
|
|
|
|
|
|
|
|
char device[32];
|
2014-05-23 15:12:34 +04:00
|
|
|
snd_pcm_t *out_handle;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 source_rate;
|
|
|
|
UINT32 actual_rate;
|
|
|
|
UINT32 source_channels;
|
|
|
|
UINT32 actual_channels;
|
|
|
|
UINT32 bytes_per_sample;
|
2012-05-09 12:15:23 +04:00
|
|
|
|
2014-05-23 15:12:34 +04:00
|
|
|
FREERDP_DSP_CONTEXT *dsp_context;
|
2012-11-26 10:34:40 +04:00
|
|
|
} TSMFAlsaAudioDevice;
|
2011-09-20 10:27:10 +04:00
|
|
|
|
2014-05-23 15:12:34 +04:00
|
|
|
static BOOL tsmf_alsa_open_device(TSMFAlsaAudioDevice *alsa)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
error = snd_pcm_open(&alsa->out_handle, alsa->device, SND_PCM_STREAM_PLAYBACK, 0);
|
2014-05-23 15:12:34 +04:00
|
|
|
if(error < 0)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "failed to open device %s", alsa->device);
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
2014-05-23 15:12:34 +04:00
|
|
|
DEBUG_TSMF("open device %s", alsa->device);
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
|
|
|
|
2014-05-23 15:12:34 +04:00
|
|
|
static BOOL tsmf_alsa_open(ITSMFAudioDevice *audio, const char *device)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2014-05-23 15:12:34 +04:00
|
|
|
TSMFAlsaAudioDevice *alsa = (TSMFAlsaAudioDevice *) audio;
|
|
|
|
if(!device)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2015-03-17 05:10:58 +03:00
|
|
|
strncpy(alsa->device, "default", sizeof(alsa->device));
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-29 13:07:19 +04:00
|
|
|
strncpy(alsa->device, device, sizeof(alsa->device));
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
|
|
|
return tsmf_alsa_open_device(alsa);
|
|
|
|
}
|
|
|
|
|
2014-05-23 15:12:34 +04:00
|
|
|
static BOOL tsmf_alsa_set_format(ITSMFAudioDevice *audio,
|
|
|
|
UINT32 sample_rate, UINT32 channels, UINT32 bits_per_sample)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
snd_pcm_uframes_t frames;
|
2014-05-23 15:12:34 +04:00
|
|
|
snd_pcm_hw_params_t *hw_params;
|
|
|
|
snd_pcm_sw_params_t *sw_params;
|
|
|
|
TSMFAlsaAudioDevice *alsa = (TSMFAlsaAudioDevice *) audio;
|
|
|
|
if(!alsa->out_handle)
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-09-20 10:27:10 +04:00
|
|
|
snd_pcm_drop(alsa->out_handle);
|
|
|
|
alsa->actual_rate = alsa->source_rate = sample_rate;
|
|
|
|
alsa->actual_channels = alsa->source_channels = channels;
|
|
|
|
alsa->bytes_per_sample = bits_per_sample / 8;
|
|
|
|
error = snd_pcm_hw_params_malloc(&hw_params);
|
2014-05-23 15:12:34 +04:00
|
|
|
if(error < 0)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "snd_pcm_hw_params_malloc failed");
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
|
|
|
snd_pcm_hw_params_any(alsa->out_handle, hw_params);
|
|
|
|
snd_pcm_hw_params_set_access(alsa->out_handle, hw_params,
|
2014-05-23 15:12:34 +04:00
|
|
|
SND_PCM_ACCESS_RW_INTERLEAVED);
|
2011-09-20 10:27:10 +04:00
|
|
|
snd_pcm_hw_params_set_format(alsa->out_handle, hw_params,
|
2014-05-23 15:12:34 +04:00
|
|
|
SND_PCM_FORMAT_S16_LE);
|
2011-09-20 10:27:10 +04:00
|
|
|
snd_pcm_hw_params_set_rate_near(alsa->out_handle, hw_params,
|
2014-05-23 15:12:34 +04:00
|
|
|
&alsa->actual_rate, NULL);
|
2011-09-20 10:27:10 +04:00
|
|
|
snd_pcm_hw_params_set_channels_near(alsa->out_handle, hw_params,
|
2014-05-23 15:12:34 +04:00
|
|
|
&alsa->actual_channels);
|
2011-09-20 10:27:10 +04:00
|
|
|
frames = sample_rate;
|
|
|
|
snd_pcm_hw_params_set_buffer_size_near(alsa->out_handle, hw_params,
|
2014-05-23 15:12:34 +04:00
|
|
|
&frames);
|
2011-09-20 10:27:10 +04:00
|
|
|
snd_pcm_hw_params(alsa->out_handle, hw_params);
|
|
|
|
snd_pcm_hw_params_free(hw_params);
|
|
|
|
error = snd_pcm_sw_params_malloc(&sw_params);
|
2014-05-23 15:12:34 +04:00
|
|
|
if(error < 0)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "snd_pcm_sw_params_malloc");
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
|
|
|
snd_pcm_sw_params_current(alsa->out_handle, sw_params);
|
|
|
|
snd_pcm_sw_params_set_start_threshold(alsa->out_handle, sw_params,
|
2014-05-23 15:12:34 +04:00
|
|
|
frames / 2);
|
2011-09-20 10:27:10 +04:00
|
|
|
snd_pcm_sw_params(alsa->out_handle, sw_params);
|
|
|
|
snd_pcm_sw_params_free(sw_params);
|
|
|
|
snd_pcm_prepare(alsa->out_handle);
|
2014-05-23 15:12:34 +04:00
|
|
|
DEBUG_TSMF("sample_rate %d channels %d bits_per_sample %d",
|
|
|
|
sample_rate, channels, bits_per_sample);
|
|
|
|
DEBUG_TSMF("hardware buffer %d frames", (int)frames);
|
|
|
|
if((alsa->actual_rate != alsa->source_rate) ||
|
|
|
|
(alsa->actual_channels != alsa->source_channels))
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2014-05-23 15:12:34 +04:00
|
|
|
DEBUG_TSMF("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);
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
|
|
|
|
2014-05-23 15:12:34 +04:00
|
|
|
static BOOL tsmf_alsa_play(ITSMFAudioDevice *audio, BYTE *data, UINT32 data_size)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
|
|
|
int len;
|
|
|
|
int error;
|
|
|
|
int frames;
|
2014-05-23 15:12:34 +04:00
|
|
|
BYTE *end;
|
|
|
|
BYTE *src;
|
|
|
|
BYTE *pindex;
|
2011-09-20 10:27:10 +04:00
|
|
|
int rbytes_per_frame;
|
|
|
|
int sbytes_per_frame;
|
2014-05-23 15:12:34 +04:00
|
|
|
TSMFAlsaAudioDevice *alsa = (TSMFAlsaAudioDevice *) audio;
|
|
|
|
DEBUG_TSMF("data_size %d", data_size);
|
|
|
|
if(alsa->out_handle)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
|
|
|
sbytes_per_frame = alsa->source_channels * alsa->bytes_per_sample;
|
|
|
|
rbytes_per_frame = alsa->actual_channels * alsa->bytes_per_sample;
|
2014-05-23 15:12:34 +04:00
|
|
|
if((alsa->source_rate == alsa->actual_rate) &&
|
|
|
|
(alsa->source_channels == alsa->actual_channels))
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
|
|
|
src = data;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-05-09 12:15:23 +04:00
|
|
|
alsa->dsp_context->resample(alsa->dsp_context, data, alsa->bytes_per_sample,
|
2014-05-23 15:12:34 +04:00
|
|
|
alsa->source_channels, alsa->source_rate, data_size / sbytes_per_frame,
|
|
|
|
alsa->actual_channels, alsa->actual_rate);
|
2012-05-09 12:15:23 +04:00
|
|
|
frames = alsa->dsp_context->resampled_frames;
|
2014-05-23 15:12:34 +04:00
|
|
|
DEBUG_TSMF("resampled %d frames at %d to %d frames at %d",
|
|
|
|
data_size / sbytes_per_frame, alsa->source_rate, frames, alsa->actual_rate);
|
2011-09-20 10:27:10 +04:00
|
|
|
data_size = frames * rbytes_per_frame;
|
2012-05-09 12:15:23 +04:00
|
|
|
src = alsa->dsp_context->resampled_buffer;
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
|
|
|
pindex = src;
|
|
|
|
end = pindex + data_size;
|
2014-05-23 15:12:34 +04:00
|
|
|
while(pindex < end)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
|
|
|
len = end - pindex;
|
|
|
|
frames = len / rbytes_per_frame;
|
|
|
|
error = snd_pcm_writei(alsa->out_handle, pindex, frames);
|
2014-05-23 15:12:34 +04:00
|
|
|
if(error == -EPIPE)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
|
|
|
snd_pcm_recover(alsa->out_handle, error, 0);
|
|
|
|
error = 0;
|
|
|
|
}
|
2015-06-29 16:21:53 +03:00
|
|
|
else if(error < 0)
|
|
|
|
{
|
|
|
|
DEBUG_TSMF("error len %d", error);
|
|
|
|
snd_pcm_close(alsa->out_handle);
|
|
|
|
alsa->out_handle = 0;
|
|
|
|
tsmf_alsa_open_device(alsa);
|
|
|
|
break;
|
|
|
|
}
|
2014-05-23 15:12:34 +04:00
|
|
|
DEBUG_TSMF("%d frames played.", error);
|
|
|
|
if(error == 0)
|
2011-09-20 10:27:10 +04:00
|
|
|
break;
|
|
|
|
pindex += error * rbytes_per_frame;
|
|
|
|
}
|
|
|
|
}
|
2012-10-09 07:21:26 +04:00
|
|
|
free(data);
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
|
|
|
|
2014-05-23 15:12:34 +04:00
|
|
|
static UINT64 tsmf_alsa_get_latency(ITSMFAudioDevice *audio)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT64 latency = 0;
|
2011-09-20 10:27:10 +04:00
|
|
|
snd_pcm_sframes_t frames = 0;
|
2014-05-23 15:12:34 +04:00
|
|
|
TSMFAlsaAudioDevice *alsa = (TSMFAlsaAudioDevice *) audio;
|
|
|
|
if(alsa->out_handle && alsa->actual_rate > 0 &&
|
|
|
|
snd_pcm_delay(alsa->out_handle, &frames) == 0 &&
|
|
|
|
frames > 0)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2012-11-26 10:34:40 +04:00
|
|
|
latency = ((UINT64)frames) * 10000000LL / (UINT64) alsa->actual_rate;
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
|
|
|
return latency;
|
|
|
|
}
|
|
|
|
|
2015-06-29 16:21:53 +03:00
|
|
|
static BOOL tsmf_alsa_flush(ITSMFAudioDevice *audio)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2015-06-29 16:21:53 +03:00
|
|
|
return TRUE;
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
|
|
|
|
2014-05-23 15:12:34 +04:00
|
|
|
static void tsmf_alsa_free(ITSMFAudioDevice *audio)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2014-05-23 15:12:34 +04:00
|
|
|
TSMFAlsaAudioDevice *alsa = (TSMFAlsaAudioDevice *) audio;
|
|
|
|
DEBUG_TSMF("");
|
|
|
|
if(alsa->out_handle)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
|
|
|
snd_pcm_drain(alsa->out_handle);
|
|
|
|
snd_pcm_close(alsa->out_handle);
|
|
|
|
}
|
2012-05-09 12:15:23 +04:00
|
|
|
freerdp_dsp_context_free(alsa->dsp_context);
|
2012-10-09 07:21:26 +04:00
|
|
|
free(alsa);
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|
|
|
|
|
2012-11-20 07:31:15 +04:00
|
|
|
#ifdef STATIC_CHANNELS
|
|
|
|
#define freerdp_tsmf_client_audio_subsystem_entry alsa_freerdp_tsmf_client_audio_subsystem_entry
|
|
|
|
#endif
|
|
|
|
|
2014-05-23 15:12:34 +04:00
|
|
|
ITSMFAudioDevice *freerdp_tsmf_client_audio_subsystem_entry(void)
|
2011-09-20 10:27:10 +04:00
|
|
|
{
|
2014-05-23 15:12:34 +04:00
|
|
|
TSMFAlsaAudioDevice *alsa;
|
|
|
|
alsa = (TSMFAlsaAudioDevice *) malloc(sizeof(TSMFAlsaAudioDevice));
|
2012-11-26 10:34:40 +04:00
|
|
|
ZeroMemory(alsa, sizeof(TSMFAlsaAudioDevice));
|
2011-09-20 10:27:10 +04:00
|
|
|
alsa->iface.Open = tsmf_alsa_open;
|
|
|
|
alsa->iface.SetFormat = tsmf_alsa_set_format;
|
|
|
|
alsa->iface.Play = tsmf_alsa_play;
|
|
|
|
alsa->iface.GetLatency = tsmf_alsa_get_latency;
|
|
|
|
alsa->iface.Flush = tsmf_alsa_flush;
|
|
|
|
alsa->iface.Free = tsmf_alsa_free;
|
2012-05-09 12:15:23 +04:00
|
|
|
alsa->dsp_context = freerdp_dsp_context_new();
|
2014-05-23 15:12:34 +04:00
|
|
|
return (ITSMFAudioDevice *) alsa;
|
2011-09-20 10:27:10 +04:00
|
|
|
}
|