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.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#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>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
2012-10-09 07:42:01 +04:00
|
|
|
#include <winpr/crt.h>
|
2012-11-19 02:32:18 +04:00
|
|
|
#include <winpr/cmdline.h>
|
2013-02-21 23:58:39 +04:00
|
|
|
#include <winpr/sysinfo.h>
|
2013-02-21 20:58:41 +04:00
|
|
|
#include <winpr/collections.h>
|
2012-10-09 07:42:01 +04:00
|
|
|
|
2011-08-15 12:28:52 +04:00
|
|
|
#include <alsa/asoundlib.h>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
2011-08-15 12:28:52 +04:00
|
|
|
#include <freerdp/types.h>
|
2013-02-20 02:47:55 +04:00
|
|
|
#include <freerdp/codec/dsp.h>
|
2013-02-27 19:26:11 +04:00
|
|
|
#include <freerdp/utils/debug.h>
|
2011-08-15 12:28:52 +04:00
|
|
|
|
|
|
|
#include "rdpsnd_main.h"
|
|
|
|
|
|
|
|
typedef struct rdpsnd_alsa_plugin rdpsndAlsaPlugin;
|
2012-11-22 06:22:06 +04:00
|
|
|
|
2011-08-15 12:28:52 +04:00
|
|
|
struct rdpsnd_alsa_plugin
|
|
|
|
{
|
|
|
|
rdpsndDevicePlugin device;
|
|
|
|
|
2013-02-28 00:10:35 +04:00
|
|
|
int latency;
|
|
|
|
int wformat;
|
|
|
|
int block_size;
|
2011-08-15 12:28:52 +04:00
|
|
|
char* device_name;
|
2013-02-20 02:47:55 +04:00
|
|
|
snd_pcm_t* pcm_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;
|
2013-02-28 00:10:35 +04:00
|
|
|
UINT32 wLocalTimeClose;
|
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;
|
2013-02-22 04:20:13 +04:00
|
|
|
snd_pcm_uframes_t buffer_size;
|
2013-02-20 02:47:55 +04:00
|
|
|
snd_pcm_uframes_t period_size;
|
2013-02-22 04:20:13 +04:00
|
|
|
snd_pcm_uframes_t start_threshold;
|
2013-02-20 02:47:55 +04:00
|
|
|
snd_async_handler_t* pcm_callback;
|
2012-05-09 12:15:23 +04:00
|
|
|
FREERDP_DSP_CONTEXT* dsp_context;
|
2011-08-15 12:28:52 +04:00
|
|
|
};
|
|
|
|
|
2013-02-22 04:20:13 +04:00
|
|
|
#define SND_PCM_CHECK(_func, _status) \
|
|
|
|
if (_status < 0) \
|
|
|
|
{ \
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "%s: %d\n", _func, _status); \
|
2013-02-22 04:20:13 +04:00
|
|
|
return -1; \
|
|
|
|
}
|
|
|
|
|
|
|
|
int rdpsnd_alsa_set_hw_params(rdpsndAlsaPlugin* alsa)
|
2011-08-15 12:28:52 +04:00
|
|
|
{
|
2013-02-22 04:20:13 +04:00
|
|
|
int status;
|
2011-08-15 12:28:52 +04:00
|
|
|
snd_pcm_hw_params_t* hw_params;
|
2013-02-26 06:46:48 +04:00
|
|
|
snd_pcm_uframes_t buffer_size_max;
|
2013-02-22 04:20:13 +04:00
|
|
|
|
|
|
|
status = snd_pcm_hw_params_malloc(&hw_params);
|
|
|
|
SND_PCM_CHECK("snd_pcm_hw_params_malloc", status);
|
|
|
|
|
|
|
|
status = snd_pcm_hw_params_any(alsa->pcm_handle, hw_params);
|
|
|
|
SND_PCM_CHECK("snd_pcm_hw_params_any", status);
|
|
|
|
|
|
|
|
/* Set interleaved read/write access */
|
|
|
|
status = snd_pcm_hw_params_set_access(alsa->pcm_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
|
|
|
|
SND_PCM_CHECK("snd_pcm_hw_params_set_access", status);
|
|
|
|
|
|
|
|
/* Set sample format */
|
|
|
|
status = snd_pcm_hw_params_set_format(alsa->pcm_handle, hw_params, alsa->format);
|
|
|
|
SND_PCM_CHECK("snd_pcm_hw_params_set_format", status);
|
|
|
|
|
2013-02-25 07:32:48 +04:00
|
|
|
/* Set sample rate */
|
|
|
|
status = snd_pcm_hw_params_set_rate_near(alsa->pcm_handle, hw_params, &alsa->actual_rate, NULL);
|
|
|
|
SND_PCM_CHECK("snd_pcm_hw_params_set_rate_near", status);
|
|
|
|
|
2013-02-22 04:20:13 +04:00
|
|
|
/* Set number of channels */
|
|
|
|
status = snd_pcm_hw_params_set_channels(alsa->pcm_handle, hw_params, alsa->actual_channels);
|
|
|
|
SND_PCM_CHECK("snd_pcm_hw_params_set_channels", status);
|
|
|
|
|
2013-02-25 07:32:48 +04:00
|
|
|
/* Get maximum buffer size */
|
2013-02-26 06:46:48 +04:00
|
|
|
status = snd_pcm_hw_params_get_buffer_size_max(hw_params, &buffer_size_max);
|
2013-02-25 07:32:48 +04:00
|
|
|
SND_PCM_CHECK("snd_pcm_hw_params_get_buffer_size_max", status);
|
2013-02-22 04:20:13 +04:00
|
|
|
|
2013-02-26 06:46:48 +04:00
|
|
|
if (alsa->buffer_size > buffer_size_max)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "Warning: requested sound buffer size %d, got %d instead\n",
|
2013-02-26 06:46:48 +04:00
|
|
|
(int) alsa->buffer_size, (int) buffer_size_max);
|
|
|
|
alsa->buffer_size = buffer_size_max;
|
|
|
|
}
|
|
|
|
|
2013-02-22 04:20:13 +04:00
|
|
|
/* Set buffer size */
|
|
|
|
status = snd_pcm_hw_params_set_buffer_size_near(alsa->pcm_handle, hw_params, &alsa->buffer_size);
|
|
|
|
SND_PCM_CHECK("snd_pcm_hw_params_set_buffer_size_near", status);
|
|
|
|
|
2013-02-25 07:32:48 +04:00
|
|
|
/* Get period size */
|
|
|
|
status = snd_pcm_hw_params_get_period_size_min(hw_params, &alsa->period_size, NULL);
|
|
|
|
SND_PCM_CHECK("snd_pcm_hw_params_get_period_size_min", status);
|
|
|
|
|
|
|
|
/* Set period size */
|
2013-02-22 04:20:13 +04:00
|
|
|
status = snd_pcm_hw_params_set_period_size_near(alsa->pcm_handle, hw_params, &alsa->period_size, NULL);
|
|
|
|
SND_PCM_CHECK("snd_pcm_hw_params_set_period_size_near", status);
|
|
|
|
|
|
|
|
status = snd_pcm_hw_params(alsa->pcm_handle, hw_params);
|
|
|
|
SND_PCM_CHECK("snd_pcm_hw_params", status);
|
|
|
|
|
|
|
|
snd_pcm_hw_params_free(hw_params);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rdpsnd_alsa_set_sw_params(rdpsndAlsaPlugin* alsa)
|
|
|
|
{
|
|
|
|
int status;
|
2011-08-15 12:28:52 +04:00
|
|
|
snd_pcm_sw_params_t* sw_params;
|
2013-02-20 02:47:55 +04:00
|
|
|
|
2013-02-26 06:46:48 +04:00
|
|
|
alsa->start_threshold = alsa->buffer_size;
|
2013-02-22 04:20:13 +04:00
|
|
|
|
|
|
|
status = snd_pcm_sw_params_malloc(&sw_params);
|
|
|
|
SND_PCM_CHECK("snd_pcm_sw_params_malloc", status);
|
|
|
|
|
|
|
|
status = snd_pcm_sw_params_current(alsa->pcm_handle, sw_params);
|
|
|
|
SND_PCM_CHECK("snd_pcm_sw_params_current", status);
|
2011-08-15 12:28:52 +04:00
|
|
|
|
2013-02-22 04:20:13 +04:00
|
|
|
status = snd_pcm_sw_params_set_start_threshold(alsa->pcm_handle, sw_params, alsa->start_threshold);
|
|
|
|
SND_PCM_CHECK("snd_pcm_sw_params_set_start_threshold", status);
|
|
|
|
|
|
|
|
status = snd_pcm_sw_params(alsa->pcm_handle, sw_params);
|
|
|
|
SND_PCM_CHECK("snd_pcm_sw_params", status);
|
|
|
|
|
|
|
|
snd_pcm_sw_params_free(sw_params);
|
|
|
|
|
|
|
|
status = snd_pcm_prepare(alsa->pcm_handle);
|
|
|
|
SND_PCM_CHECK("snd_pcm_prepare", status);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-02-25 07:32:48 +04:00
|
|
|
int rdpsnd_alsa_validate_params(rdpsndAlsaPlugin* alsa)
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
snd_pcm_uframes_t buffer_size;
|
|
|
|
snd_pcm_uframes_t period_size;
|
|
|
|
|
|
|
|
status = snd_pcm_get_params(alsa->pcm_handle, &buffer_size, &period_size);
|
|
|
|
SND_PCM_CHECK("snd_pcm_get_params", status);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-02-22 04:20:13 +04:00
|
|
|
static int rdpsnd_alsa_set_params(rdpsndAlsaPlugin* alsa)
|
|
|
|
{
|
2013-02-22 03:41:41 +04:00
|
|
|
/**
|
2013-02-22 04:20:13 +04:00
|
|
|
* ALSA Parameters
|
2013-02-22 03:41:41 +04:00
|
|
|
*
|
|
|
|
* http://www.alsa-project.org/main/index.php/FramesPeriods
|
|
|
|
*
|
|
|
|
* buffer_size = period_size * periods
|
|
|
|
* period_bytes = period_size * bytes_per_frame
|
|
|
|
* bytes_per_frame = channels * bytes_per_sample
|
|
|
|
*
|
|
|
|
* A frame is equivalent of one sample being played,
|
|
|
|
* irrespective of the number of channels or the number of bits
|
|
|
|
*
|
|
|
|
* A period is the number of frames in between each hardware interrupt.
|
|
|
|
*
|
|
|
|
* The buffer size always has to be greater than one period size.
|
|
|
|
* Commonly this is (2 * period_size), but some hardware can do 8 periods per buffer.
|
|
|
|
* It is also possible for the buffer size to not be an integer multiple of the period size.
|
|
|
|
*/
|
|
|
|
|
2013-02-22 04:20:13 +04:00
|
|
|
snd_pcm_drop(alsa->pcm_handle);
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2013-02-26 06:46:48 +04:00
|
|
|
if (alsa->latency < 0)
|
2013-02-28 00:10:35 +04:00
|
|
|
alsa->latency = 400;
|
2013-02-26 06:46:48 +04:00
|
|
|
|
|
|
|
alsa->buffer_size = alsa->latency * (alsa->actual_rate / 1000);
|
|
|
|
|
2013-02-22 04:20:13 +04:00
|
|
|
if (rdpsnd_alsa_set_hw_params(alsa) < 0)
|
|
|
|
return -1;
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2013-02-22 04:20:13 +04:00
|
|
|
if (rdpsnd_alsa_set_sw_params(alsa) < 0)
|
|
|
|
return -1;
|
2011-08-15 12:28:52 +04:00
|
|
|
|
2013-02-25 07:32:48 +04:00
|
|
|
rdpsnd_alsa_validate_params(alsa);
|
|
|
|
|
2013-02-22 04:20:13 +04:00
|
|
|
return 0;
|
2011-08-15 12:28:52 +04:00
|
|
|
}
|
|
|
|
|
2013-02-26 06:46:48 +04:00
|
|
|
static void rdpsnd_alsa_set_format(rdpsndDevicePlugin* device, AUDIO_FORMAT* format, int latency)
|
2011-08-15 12:28:52 +04:00
|
|
|
{
|
2013-02-20 02:47:55 +04:00
|
|
|
rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*) device;
|
2011-08-15 12:28:52 +04:00
|
|
|
|
2013-02-22 03:41:41 +04:00
|
|
|
if (format)
|
2011-08-15 12:28:52 +04:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2013-02-19 20:02:45 +04:00
|
|
|
case WAVE_FORMAT_PCM:
|
2011-08-15 12:28:52 +04:00
|
|
|
switch (format->wBitsPerSample)
|
|
|
|
{
|
2013-02-20 02:47:55 +04:00
|
|
|
case 4:
|
2013-02-22 03:41:41 +04:00
|
|
|
alsa->format = SND_PCM_FORMAT_S16_LE;
|
|
|
|
alsa->bytes_per_channel = 2;
|
2013-02-20 02:47:55 +04:00
|
|
|
break;
|
|
|
|
|
2011-08-15 12:28:52 +04:00
|
|
|
case 8:
|
|
|
|
alsa->format = SND_PCM_FORMAT_S8;
|
|
|
|
alsa->bytes_per_channel = 1;
|
|
|
|
break;
|
2013-02-20 02:47:55 +04:00
|
|
|
|
2011-08-15 12:28:52 +04:00
|
|
|
case 16:
|
|
|
|
alsa->format = SND_PCM_FORMAT_S16_LE;
|
|
|
|
alsa->bytes_per_channel = 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-02-19 20:02:45 +04:00
|
|
|
case WAVE_FORMAT_ADPCM:
|
|
|
|
case WAVE_FORMAT_DVI_ADPCM:
|
2011-08-15 12:28:52 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-09-25 07:41:37 +04:00
|
|
|
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
|
|
|
|
2013-02-28 01:57:18 +04:00
|
|
|
if (alsa->mixer_handle)
|
|
|
|
return;
|
|
|
|
|
|
|
|
status = snd_mixer_open(&alsa->mixer_handle, 0);
|
2012-10-03 07:01:16 +04:00
|
|
|
|
|
|
|
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
|
|
|
|
2013-02-28 01:57:18 +04:00
|
|
|
status = snd_mixer_attach(alsa->mixer_handle, alsa->device_name);
|
2012-10-03 07:01:16 +04:00
|
|
|
|
|
|
|
if (status < 0)
|
2012-05-29 19:36:47 +04:00
|
|
|
{
|
|
|
|
DEBUG_WARN("snd_mixer_attach failed");
|
2013-02-28 01:57:18 +04:00
|
|
|
snd_mixer_close(alsa->mixer_handle);
|
2012-05-29 19:36:47 +04:00
|
|
|
return;
|
|
|
|
}
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2013-02-28 01:57:18 +04:00
|
|
|
status = snd_mixer_selem_register(alsa->mixer_handle, NULL, NULL);
|
2012-10-03 07:01:16 +04:00
|
|
|
|
|
|
|
if (status < 0)
|
2012-05-29 19:36:47 +04:00
|
|
|
{
|
|
|
|
DEBUG_WARN("snd_mixer_selem_register failed");
|
2013-02-28 01:57:18 +04:00
|
|
|
snd_mixer_close(alsa->mixer_handle);
|
2012-05-29 19:36:47 +04:00
|
|
|
return;
|
|
|
|
}
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2013-02-28 01:57:18 +04:00
|
|
|
status = snd_mixer_load(alsa->mixer_handle);
|
2012-10-03 07:01:16 +04:00
|
|
|
|
|
|
|
if (status < 0)
|
2012-05-29 19:36:47 +04:00
|
|
|
{
|
|
|
|
DEBUG_WARN("snd_mixer_load failed");
|
2013-02-28 01:57:18 +04:00
|
|
|
snd_mixer_close(alsa->mixer_handle);
|
2012-05-29 19:36:47 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-26 06:46:48 +04:00
|
|
|
static void rdpsnd_alsa_open(rdpsndDevicePlugin* device, AUDIO_FORMAT* format, int latency)
|
2011-08-15 12:28:52 +04:00
|
|
|
{
|
2013-02-20 02:47:55 +04:00
|
|
|
int mode;
|
2012-10-03 07:01:16 +04:00
|
|
|
int status;
|
|
|
|
rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*) device;
|
2011-08-15 12:28:52 +04:00
|
|
|
|
2013-02-21 12:38:36 +04:00
|
|
|
if (alsa->pcm_handle)
|
2011-08-15 12:28:52 +04:00
|
|
|
return;
|
|
|
|
|
2013-02-20 02:47:55 +04:00
|
|
|
mode = 0;
|
2013-02-21 20:58:41 +04:00
|
|
|
//mode |= SND_PCM_NONBLOCK;
|
2013-02-20 02:47:55 +04:00
|
|
|
|
|
|
|
status = snd_pcm_open(&alsa->pcm_handle, alsa->device_name, SND_PCM_STREAM_PLAYBACK, mode);
|
2012-10-03 07:01:16 +04:00
|
|
|
|
|
|
|
if (status < 0)
|
2011-08-15 12:28:52 +04:00
|
|
|
{
|
|
|
|
DEBUG_WARN("snd_pcm_open failed");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-05-09 12:15:23 +04:00
|
|
|
freerdp_dsp_context_reset_adpcm(alsa->dsp_context);
|
2011-09-25 07:41:37 +04:00
|
|
|
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)
|
|
|
|
{
|
2013-02-28 00:10:35 +04:00
|
|
|
int status;
|
|
|
|
snd_htimestamp_t tstamp;
|
|
|
|
snd_pcm_uframes_t frames;
|
|
|
|
rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*) device;
|
|
|
|
|
|
|
|
if (!alsa->pcm_handle)
|
|
|
|
return;
|
|
|
|
|
|
|
|
status = snd_pcm_htimestamp(alsa->pcm_handle, &frames, &tstamp);
|
|
|
|
|
|
|
|
if (status != 0)
|
|
|
|
frames = 0;
|
|
|
|
|
|
|
|
alsa->wLocalTimeClose = GetTickCount();
|
|
|
|
alsa->wLocalTimeClose += (((frames * 1000) / alsa->actual_rate) / alsa->actual_channels);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rdpsnd_alsa_free(rdpsndDevicePlugin* device)
|
|
|
|
{
|
|
|
|
rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*) device;
|
2011-08-15 12:28:52 +04:00
|
|
|
|
2013-02-21 12:38:36 +04:00
|
|
|
if (alsa->pcm_handle)
|
2011-08-15 12:28:52 +04:00
|
|
|
{
|
2013-02-20 02:47:55 +04:00
|
|
|
snd_pcm_drain(alsa->pcm_handle);
|
|
|
|
snd_pcm_close(alsa->pcm_handle);
|
|
|
|
alsa->pcm_handle = 0;
|
2011-08-15 12:28:52 +04:00
|
|
|
}
|
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;
|
|
|
|
}
|
2013-02-19 19:19:08 +04:00
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
free(alsa->device_name);
|
2013-02-19 19:19:08 +04:00
|
|
|
|
2012-05-09 12:15:23 +04:00
|
|
|
freerdp_dsp_context_free(alsa->dsp_context);
|
2013-02-19 19:19:08 +04:00
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
free(alsa);
|
2011-08-15 12:28:52 +04:00
|
|
|
}
|
|
|
|
|
2013-02-26 06:46:48 +04:00
|
|
|
static BOOL rdpsnd_alsa_format_supported(rdpsndDevicePlugin* device, AUDIO_FORMAT* format)
|
2011-08-15 12:28:52 +04:00
|
|
|
{
|
|
|
|
switch (format->wFormatTag)
|
|
|
|
{
|
2013-02-19 20:02:45 +04:00
|
|
|
case WAVE_FORMAT_PCM:
|
2011-08-15 12:28:52 +04:00
|
|
|
if (format->cbSize == 0 &&
|
|
|
|
format->nSamplesPerSec <= 48000 &&
|
|
|
|
(format->wBitsPerSample == 8 || format->wBitsPerSample == 16) &&
|
|
|
|
(format->nChannels == 1 || format->nChannels == 2))
|
|
|
|
{
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-08-15 12:28:52 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-02-19 20:02:45 +04:00
|
|
|
case WAVE_FORMAT_ADPCM:
|
|
|
|
case WAVE_FORMAT_DVI_ADPCM:
|
2011-08-15 12:28:52 +04:00
|
|
|
if (format->nSamplesPerSec <= 48000 &&
|
|
|
|
format->wBitsPerSample == 4 &&
|
|
|
|
(format->nChannels == 1 || format->nChannels == 2))
|
|
|
|
{
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-08-15 12:28:52 +04:00
|
|
|
}
|
|
|
|
break;
|
2013-02-19 20:02:45 +04:00
|
|
|
|
|
|
|
case WAVE_FORMAT_ALAW:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WAVE_FORMAT_MULAW:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WAVE_FORMAT_GSM610:
|
|
|
|
break;
|
2011-08-15 12:28:52 +04:00
|
|
|
}
|
2013-02-19 20:02:45 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-08-15 12:28:52 +04:00
|
|
|
}
|
|
|
|
|
2013-02-28 01:57:18 +04:00
|
|
|
static UINT32 rdpsnd_alsa_get_volume(rdpsndDevicePlugin* device)
|
|
|
|
{
|
|
|
|
long volume_min;
|
|
|
|
long volume_max;
|
|
|
|
long volume_left;
|
|
|
|
long volume_right;
|
|
|
|
int percent_left;
|
|
|
|
int percent_right;
|
|
|
|
UINT32 dwVolume;
|
|
|
|
UINT16 dwVolumeLeft;
|
|
|
|
UINT16 dwVolumeRight;
|
|
|
|
snd_mixer_elem_t* elem;
|
|
|
|
rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*) device;
|
|
|
|
|
|
|
|
dwVolumeLeft = ((50 * 0xFFFF) / 100); /* 50% */
|
|
|
|
dwVolumeRight = ((50 * 0xFFFF) / 100); /* 50% */
|
|
|
|
|
|
|
|
if (!alsa->mixer_handle)
|
|
|
|
rdpsnd_alsa_open_mixer(alsa);
|
|
|
|
|
|
|
|
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);
|
|
|
|
snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_FRONT_LEFT, &volume_left);
|
|
|
|
snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_FRONT_RIGHT, &volume_right);
|
|
|
|
|
|
|
|
dwVolumeLeft = (UINT16) (((volume_left * 0xFFFF) - volume_min) / (volume_max - volume_min));
|
|
|
|
dwVolumeRight = (UINT16) (((volume_right * 0xFFFF) - volume_min) / (volume_max - volume_min));
|
|
|
|
|
|
|
|
percent_left = (dwVolumeLeft * 100) / 0xFFFF;
|
|
|
|
percent_right = (dwVolumeRight * 100) / 0xFFFF;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dwVolume = (dwVolumeLeft << 16) | dwVolumeRight;
|
|
|
|
|
|
|
|
return dwVolume;
|
|
|
|
}
|
|
|
|
|
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;
|
2013-02-28 01:57:18 +04:00
|
|
|
int percent_left;
|
|
|
|
int percent_right;
|
2012-05-29 19:36:47 +04:00
|
|
|
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)
|
2013-02-28 01:57:18 +04:00
|
|
|
rdpsnd_alsa_open_mixer(alsa);
|
2012-05-29 19:36:47 +04:00
|
|
|
|
|
|
|
left = (value & 0xFFFF);
|
|
|
|
right = ((value >> 16) & 0xFFFF);
|
|
|
|
|
2013-02-28 01:57:18 +04:00
|
|
|
percent_left = (left * 100) / 0xFFFF;
|
|
|
|
percent_right = (right * 100) / 0xFFFF;
|
|
|
|
|
2012-05-29 19:36:47 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-02-26 20:16:33 +04:00
|
|
|
BYTE* rdpsnd_alsa_process_audio_sample(rdpsndDevicePlugin* device, BYTE* data, int* size)
|
2011-08-15 12:28:52 +04:00
|
|
|
{
|
|
|
|
int frames;
|
2013-02-21 22:25:43 +04:00
|
|
|
BYTE* srcData;
|
|
|
|
int srcFrameSize;
|
|
|
|
int dstFrameSize;
|
2012-10-03 07:01:16 +04:00
|
|
|
rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*) device;
|
2011-08-15 12:28:52 +04:00
|
|
|
|
2013-02-20 02:47:55 +04:00
|
|
|
if (alsa->wformat == WAVE_FORMAT_ADPCM)
|
2012-05-24 17:26:17 +04:00
|
|
|
{
|
|
|
|
alsa->dsp_context->decode_ms_adpcm(alsa->dsp_context,
|
2013-02-21 22:25:43 +04:00
|
|
|
data, *size, alsa->source_channels, alsa->block_size);
|
2013-02-21 12:38:36 +04:00
|
|
|
|
2013-02-21 22:25:43 +04:00
|
|
|
*size = alsa->dsp_context->adpcm_size;
|
|
|
|
srcData = alsa->dsp_context->adpcm_buffer;
|
2012-05-24 17:26:17 +04:00
|
|
|
}
|
2013-02-20 02:47:55 +04:00
|
|
|
else if (alsa->wformat == WAVE_FORMAT_DVI_ADPCM)
|
2011-08-15 12:28:52 +04:00
|
|
|
{
|
2012-05-09 12:15:23 +04:00
|
|
|
alsa->dsp_context->decode_ima_adpcm(alsa->dsp_context,
|
2013-02-21 22:25:43 +04:00
|
|
|
data, *size, alsa->source_channels, alsa->block_size);
|
2013-02-21 12:38:36 +04:00
|
|
|
|
2013-02-21 22:25:43 +04:00
|
|
|
*size = alsa->dsp_context->adpcm_size;
|
|
|
|
srcData = alsa->dsp_context->adpcm_buffer;
|
2011-08-15 12:28:52 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-02-21 22:25:43 +04:00
|
|
|
srcData = data;
|
2011-08-15 12:28:52 +04:00
|
|
|
}
|
|
|
|
|
2013-02-21 22:25:43 +04:00
|
|
|
srcFrameSize = alsa->source_channels * alsa->bytes_per_channel;
|
|
|
|
dstFrameSize = alsa->actual_channels * alsa->bytes_per_channel;
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2013-02-21 22:25:43 +04:00
|
|
|
if ((*size % srcFrameSize) != 0)
|
|
|
|
return NULL;
|
2011-08-15 12:28:52 +04:00
|
|
|
|
2013-02-21 22:25:43 +04:00
|
|
|
if (!((alsa->source_rate == alsa->actual_rate) && (alsa->source_channels == alsa->actual_channels)))
|
2011-08-15 12:28:52 +04:00
|
|
|
{
|
2013-02-21 22:25:43 +04:00
|
|
|
alsa->dsp_context->resample(alsa->dsp_context, srcData, alsa->bytes_per_channel,
|
|
|
|
alsa->source_channels, alsa->source_rate, *size / srcFrameSize,
|
2012-05-09 12:15:23 +04:00
|
|
|
alsa->actual_channels, alsa->actual_rate);
|
2013-02-21 22:25:43 +04:00
|
|
|
|
2012-05-09 12:15:23 +04:00
|
|
|
frames = alsa->dsp_context->resampled_frames;
|
2013-02-19 19:19:08 +04:00
|
|
|
|
2013-02-21 22:25:43 +04:00
|
|
|
*size = frames * dstFrameSize;
|
|
|
|
srcData = alsa->dsp_context->resampled_buffer;
|
2011-08-15 12:28:52 +04:00
|
|
|
}
|
|
|
|
|
2013-02-21 22:25:43 +04:00
|
|
|
data = srcData;
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2013-02-26 06:46:48 +04:00
|
|
|
static void rdpsnd_alsa_wave_decode(rdpsndDevicePlugin* device, RDPSND_WAVE* wave)
|
2013-02-21 22:25:43 +04:00
|
|
|
{
|
2013-02-26 06:46:48 +04:00
|
|
|
int size;
|
|
|
|
BYTE* data;
|
2013-02-21 22:25:43 +04:00
|
|
|
|
2013-02-26 06:46:48 +04:00
|
|
|
size = wave->length;
|
2013-02-26 20:16:33 +04:00
|
|
|
data = rdpsnd_alsa_process_audio_sample(device, wave->data, &size);
|
2013-02-21 22:25:43 +04:00
|
|
|
|
2013-02-26 06:46:48 +04:00
|
|
|
wave->data = (BYTE*) malloc(size);
|
|
|
|
CopyMemory(wave->data, data, size);
|
|
|
|
wave->length = size;
|
2013-02-21 22:25:43 +04:00
|
|
|
}
|
|
|
|
|
2013-02-26 06:46:48 +04:00
|
|
|
static void rdpsnd_alsa_wave_play(rdpsndDevicePlugin* device, RDPSND_WAVE* wave)
|
2013-02-21 22:25:43 +04:00
|
|
|
{
|
2013-02-26 06:46:48 +04:00
|
|
|
BYTE* data;
|
|
|
|
int offset;
|
|
|
|
int length;
|
|
|
|
int status;
|
|
|
|
int frame_size;
|
2013-02-28 00:10:35 +04:00
|
|
|
UINT32 wCurrentTime;
|
|
|
|
snd_htimestamp_t tstamp;
|
|
|
|
snd_pcm_uframes_t frames;
|
|
|
|
|
2013-02-21 22:25:43 +04:00
|
|
|
rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*) device;
|
|
|
|
|
2013-02-26 06:46:48 +04:00
|
|
|
offset = 0;
|
|
|
|
data = wave->data;
|
|
|
|
length = wave->length;
|
|
|
|
frame_size = alsa->actual_channels * alsa->bytes_per_channel;
|
2013-02-21 22:25:43 +04:00
|
|
|
|
2013-02-28 00:10:35 +04:00
|
|
|
if (alsa->wLocalTimeClose)
|
|
|
|
{
|
|
|
|
wCurrentTime = GetTickCount();
|
|
|
|
|
|
|
|
if (snd_pcm_htimestamp(alsa->pcm_handle, &frames, &tstamp) == -EPIPE)
|
|
|
|
{
|
|
|
|
if (wCurrentTime > alsa->wLocalTimeClose)
|
|
|
|
snd_pcm_recover(alsa->pcm_handle, -EPIPE, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
alsa->wLocalTimeClose = 0;
|
|
|
|
}
|
2013-02-21 22:25:43 +04:00
|
|
|
|
2013-02-26 06:46:48 +04:00
|
|
|
while (offset < length)
|
|
|
|
{
|
|
|
|
status = snd_pcm_writei(alsa->pcm_handle, &data[offset], (length - offset) / frame_size);
|
2013-02-21 22:25:43 +04:00
|
|
|
|
2013-02-26 06:46:48 +04:00
|
|
|
if (status == -EPIPE)
|
|
|
|
{
|
|
|
|
snd_pcm_recover(alsa->pcm_handle, status, 0);
|
|
|
|
status = 0;
|
|
|
|
}
|
|
|
|
else if (status == -EAGAIN)
|
|
|
|
{
|
|
|
|
status = 0;
|
|
|
|
}
|
|
|
|
else if (status < 0)
|
|
|
|
{
|
2013-03-29 02:06:34 +04:00
|
|
|
fprintf(stderr, "status: %d\n", status);
|
2013-02-26 06:46:48 +04:00
|
|
|
snd_pcm_close(alsa->pcm_handle);
|
|
|
|
alsa->pcm_handle = NULL;
|
|
|
|
rdpsnd_alsa_open((rdpsndDevicePlugin*) alsa, NULL, alsa->latency);
|
|
|
|
break;
|
|
|
|
}
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2013-02-26 06:46:48 +04:00
|
|
|
offset += status * frame_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(data);
|
|
|
|
|
2013-02-28 00:10:35 +04:00
|
|
|
snd_pcm_htimestamp(alsa->pcm_handle, &frames, &tstamp);
|
2013-02-26 20:50:48 +04:00
|
|
|
|
2013-02-28 00:10:35 +04:00
|
|
|
wave->wPlaybackDelay = ((frames * 1000) / alsa->actual_rate);
|
2013-02-26 20:50:48 +04:00
|
|
|
|
2013-02-26 06:46:48 +04:00
|
|
|
wave->wLocalTimeB = GetTickCount();
|
2013-02-27 02:54:16 +04:00
|
|
|
wave->wLocalTimeB += wave->wPlaybackDelay;
|
2013-02-26 06:46:48 +04:00
|
|
|
wave->wLatency = (UINT16) (wave->wLocalTimeB - wave->wLocalTimeA);
|
|
|
|
wave->wTimeStampB = wave->wTimeStampA + wave->wLatency;
|
|
|
|
|
2013-03-29 02:06:34 +04:00
|
|
|
//fprintf(stderr, "wTimeStampA: %d wTimeStampB: %d wLatency: %d\n", wave->wTimeStampA, wave->wTimeStampB, wave->wLatency);
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2013-02-26 06:46:48 +04:00
|
|
|
device->WaveConfirm(device, wave);
|
2011-08-15 12:28:52 +04:00
|
|
|
}
|
|
|
|
|
2012-11-19 02:32:18 +04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-11-06 04:55:15 +04:00
|
|
|
#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
|
|
|
{
|
2012-11-19 02:32:18 +04:00
|
|
|
ADDIN_ARGV* args;
|
2011-08-15 12:28:52 +04:00
|
|
|
rdpsndAlsaPlugin* alsa;
|
|
|
|
|
2012-11-20 07:31:15 +04:00
|
|
|
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;
|
2013-02-28 01:57:18 +04:00
|
|
|
alsa->device.GetVolume = rdpsnd_alsa_get_volume;
|
2011-08-15 12:28:52 +04:00
|
|
|
alsa->device.SetVolume = rdpsnd_alsa_set_volume;
|
2013-02-26 06:46:48 +04:00
|
|
|
alsa->device.WaveDecode = rdpsnd_alsa_wave_decode;
|
2013-02-21 22:25:43 +04:00
|
|
|
alsa->device.WavePlay = rdpsnd_alsa_wave_play;
|
2011-08-15 12:28:52 +04:00
|
|
|
alsa->device.Close = rdpsnd_alsa_close;
|
|
|
|
alsa->device.Free = rdpsnd_alsa_free;
|
|
|
|
|
2012-11-19 02:32:18 +04:00
|
|
|
args = pEntryPoints->args;
|
|
|
|
rdpsnd_alsa_parse_addin_args((rdpsndDevicePlugin*) alsa, args);
|
2012-10-03 07:01:16 +04:00
|
|
|
|
2012-11-26 10:23:23 +04:00
|
|
|
if (!alsa->device_name)
|
|
|
|
alsa->device_name = _strdup("default");
|
|
|
|
|
2013-02-20 02:47:55 +04:00
|
|
|
alsa->pcm_handle = 0;
|
2011-08-15 12:28:52 +04:00
|
|
|
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;
|
|
|
|
|
2012-05-09 12:15:23 +04:00
|
|
|
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;
|
|
|
|
}
|