mirror of https://github.com/FreeRDP/FreeRDP
rdpsnd: add ALSA sub-plugin.
This commit is contained in:
parent
a2a9fdb630
commit
a122006b0e
|
@ -67,6 +67,7 @@ check_include_files(unistd.h HAVE_UNISTD_H)
|
|||
# Libraries that we have a hard dependency on
|
||||
find_package(OpenSSL REQUIRED)
|
||||
find_package(ZLIB REQUIRED)
|
||||
find_package(ALSA)
|
||||
|
||||
# Endian
|
||||
test_big_endian(BIG_ENDIAN)
|
||||
|
|
|
@ -28,3 +28,8 @@ set_target_properties(rdpsnd PROPERTIES PREFIX "")
|
|||
target_link_libraries(rdpsnd freerdp-utils)
|
||||
|
||||
install(TARGETS rdpsnd DESTINATION ${FREERDP_PLUGIN_PATH})
|
||||
|
||||
if(ALSA_FOUND)
|
||||
add_subdirectory(alsa)
|
||||
endif()
|
||||
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
# FreeRDP: A Remote Desktop Protocol Client
|
||||
# FreeRDP cmake build script
|
||||
#
|
||||
# Copyright 2011 O.S. Systems Software Ltda.
|
||||
# Copyright 2011 Otavio Salvador <otavio@ossystems.com.br>
|
||||
# Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
||||
#
|
||||
# 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.
|
||||
|
||||
set(RDPSND_ALSA_SRCS
|
||||
rdpsnd_alsa.c
|
||||
)
|
||||
|
||||
include_directories(..)
|
||||
include_directories(${ALSA_INCLUDE_DIRS})
|
||||
|
||||
add_library(rdpsnd_alsa SHARED ${RDPSND_ALSA_SRCS})
|
||||
set_target_properties(rdpsnd_alsa PROPERTIES PREFIX "")
|
||||
|
||||
target_link_libraries(rdpsnd_alsa freerdp-utils)
|
||||
target_link_libraries(rdpsnd_alsa ${ALSA_LIBRARIES})
|
||||
|
||||
install(TARGETS rdpsnd_alsa DESTINATION ${FREERDP_PLUGIN_PATH})
|
|
@ -0,0 +1,337 @@
|
|||
/**
|
||||
* FreeRDP: A Remote Desktop Protocol client.
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <alsa/asoundlib.h>
|
||||
#include <freerdp/types.h>
|
||||
#include <freerdp/utils/memory.h>
|
||||
#include <freerdp/utils/dsp.h>
|
||||
#include <freerdp/utils/svc_plugin.h>
|
||||
|
||||
#include "rdpsnd_main.h"
|
||||
|
||||
typedef struct rdpsnd_alsa_plugin rdpsndAlsaPlugin;
|
||||
struct rdpsnd_alsa_plugin
|
||||
{
|
||||
rdpsndDevicePlugin device;
|
||||
|
||||
char* device_name;
|
||||
snd_pcm_t* out_handle;
|
||||
uint32 source_rate;
|
||||
uint32 actual_rate;
|
||||
snd_pcm_format_t format;
|
||||
uint32 source_channels;
|
||||
uint32 actual_channels;
|
||||
int bytes_per_channel;
|
||||
int wformat;
|
||||
int block_size;
|
||||
ADPCM adpcm;
|
||||
};
|
||||
|
||||
static void rdpsnd_alsa_set_params(rdpsndAlsaPlugin* alsa)
|
||||
{
|
||||
snd_pcm_hw_params_t* hw_params;
|
||||
snd_pcm_sw_params_t* sw_params;
|
||||
int error;
|
||||
snd_pcm_uframes_t frames;
|
||||
|
||||
snd_pcm_drop(alsa->out_handle);
|
||||
|
||||
error = snd_pcm_hw_params_malloc(&hw_params);
|
||||
if (error < 0)
|
||||
{
|
||||
DEBUG_WARN("snd_pcm_hw_params_malloc failed");
|
||||
return;
|
||||
}
|
||||
snd_pcm_hw_params_any(alsa->out_handle, hw_params);
|
||||
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);
|
||||
frames = alsa->actual_rate * 4;
|
||||
snd_pcm_hw_params_set_buffer_size_near(alsa->out_handle, hw_params,
|
||||
&frames);
|
||||
snd_pcm_hw_params(alsa->out_handle, hw_params);
|
||||
snd_pcm_hw_params_free(hw_params);
|
||||
|
||||
error = snd_pcm_sw_params_malloc(&sw_params);
|
||||
if (error < 0)
|
||||
{
|
||||
DEBUG_WARN("snd_pcm_sw_params_malloc failed");
|
||||
return;
|
||||
}
|
||||
snd_pcm_sw_params_current(alsa->out_handle, sw_params);
|
||||
snd_pcm_sw_params_set_start_threshold(alsa->out_handle, sw_params,
|
||||
frames / 2);
|
||||
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",
|
||||
(int)frames, (double)frames / 2.0 / (double)alsa->actual_rate);
|
||||
if ((alsa->actual_rate != alsa->source_rate) ||
|
||||
(alsa->actual_channels != alsa->source_channels))
|
||||
{
|
||||
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)
|
||||
{
|
||||
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;
|
||||
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;
|
||||
|
||||
case 0x11: /* IMA ADPCM */
|
||||
alsa->format = SND_PCM_FORMAT_S16_LE;
|
||||
alsa->bytes_per_channel = 2;
|
||||
break;
|
||||
}
|
||||
alsa->wformat = format->wFormatTag;
|
||||
alsa->block_size = format->nBlockAlign;
|
||||
}
|
||||
|
||||
rdpsnd_alsa_set_params(alsa);
|
||||
}
|
||||
|
||||
static void rdpsnd_alsa_open(rdpsndDevicePlugin* device, rdpsndFormat* format)
|
||||
{
|
||||
rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
|
||||
int error;
|
||||
|
||||
if (alsa->out_handle != 0)
|
||||
return;
|
||||
|
||||
DEBUG_SVC("opening");
|
||||
|
||||
error = snd_pcm_open(&alsa->out_handle, alsa->device_name,
|
||||
SND_PCM_STREAM_PLAYBACK, 0);
|
||||
if (error < 0)
|
||||
{
|
||||
DEBUG_WARN("snd_pcm_open failed");
|
||||
}
|
||||
else
|
||||
{
|
||||
memset(&alsa->adpcm, 0, sizeof(ADPCM));
|
||||
rdpsnd_alsa_set_format(device, format);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
static void rdpsnd_alsa_free(rdpsndDevicePlugin* device)
|
||||
{
|
||||
rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
|
||||
|
||||
rdpsnd_alsa_close(device);
|
||||
xfree(alsa->device_name);
|
||||
xfree(alsa);
|
||||
}
|
||||
|
||||
static boolean rdpsnd_alsa_format_supported(rdpsndDevicePlugin* device, rdpsndFormat* format)
|
||||
{
|
||||
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;
|
||||
}
|
||||
break;
|
||||
|
||||
case 0x11: /* IMA ADPCM */
|
||||
if (format->nSamplesPerSec <= 48000 &&
|
||||
format->wBitsPerSample == 4 &&
|
||||
(format->nChannels == 1 || format->nChannels == 2))
|
||||
{
|
||||
return True;
|
||||
}
|
||||
break;
|
||||
}
|
||||
return False;
|
||||
}
|
||||
|
||||
static void rdpsnd_alsa_set_volume(rdpsndDevicePlugin* device, uint32 value)
|
||||
{
|
||||
}
|
||||
|
||||
static void rdpsnd_alsa_play(rdpsndDevicePlugin* device, uint8* data, int size)
|
||||
{
|
||||
rdpsndAlsaPlugin* alsa = (rdpsndAlsaPlugin*)device;
|
||||
uint8* decoded_data;
|
||||
int decoded_size;
|
||||
uint8* src;
|
||||
uint8* resampled_data;
|
||||
int len;
|
||||
int error;
|
||||
int frames;
|
||||
int rbytes_per_frame;
|
||||
int sbytes_per_frame;
|
||||
uint8* pindex;
|
||||
uint8* end;
|
||||
|
||||
if (alsa->out_handle == 0)
|
||||
return;
|
||||
|
||||
if (alsa->wformat == 0x11)
|
||||
{
|
||||
decoded_data = dsp_decode_ima_adpcm(&alsa->adpcm,
|
||||
data, size, alsa->source_channels, alsa->block_size, &decoded_size);
|
||||
size = decoded_size;
|
||||
src = decoded_data;
|
||||
}
|
||||
else
|
||||
{
|
||||
decoded_data = NULL;
|
||||
src = data;
|
||||
}
|
||||
|
||||
sbytes_per_frame = alsa->source_channels * alsa->bytes_per_channel;
|
||||
rbytes_per_frame = alsa->actual_channels * alsa->bytes_per_channel;
|
||||
if ((size % sbytes_per_frame) != 0)
|
||||
{
|
||||
DEBUG_WARN("error len mod");
|
||||
return;
|
||||
}
|
||||
|
||||
if ((alsa->source_rate == alsa->actual_rate) &&
|
||||
(alsa->source_channels == alsa->actual_channels))
|
||||
{
|
||||
resampled_data = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
resampled_data = dsp_resample(src, alsa->bytes_per_channel,
|
||||
alsa->source_channels, alsa->source_rate, size / sbytes_per_frame,
|
||||
alsa->actual_channels, alsa->actual_rate, &frames);
|
||||
DEBUG_SVC("resampled %d frames at %d to %d frames at %d",
|
||||
size / sbytes_per_frame, alsa->source_rate, frames, alsa->actual_rate);
|
||||
size = frames * rbytes_per_frame;
|
||||
src = resampled_data;
|
||||
}
|
||||
|
||||
pindex = src;
|
||||
end = pindex + size;
|
||||
while (pindex < end)
|
||||
{
|
||||
len = end - pindex;
|
||||
frames = len / rbytes_per_frame;
|
||||
error = snd_pcm_writei(alsa->out_handle, pindex, frames);
|
||||
if (error == -EPIPE)
|
||||
{
|
||||
snd_pcm_recover(alsa->out_handle, error, 0);
|
||||
error = 0;
|
||||
}
|
||||
else if (error < 0)
|
||||
{
|
||||
DEBUG_WARN("error %d", error);
|
||||
snd_pcm_close(alsa->out_handle);
|
||||
alsa->out_handle = 0;
|
||||
rdpsnd_alsa_open(device, NULL);
|
||||
break;
|
||||
}
|
||||
pindex += error * rbytes_per_frame;
|
||||
}
|
||||
|
||||
if (resampled_data)
|
||||
free(resampled_data);
|
||||
if (decoded_data)
|
||||
free(decoded_data);
|
||||
}
|
||||
|
||||
int FreeRDPRdpsndDeviceEntry(PFREERDP_RDPSND_DEVICE_ENTRY_POINTS pEntryPoints)
|
||||
{
|
||||
rdpsndAlsaPlugin* alsa;
|
||||
FRDP_PLUGIN_DATA* data;
|
||||
|
||||
alsa = xnew(rdpsndAlsaPlugin);
|
||||
|
||||
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.Close = rdpsnd_alsa_close;
|
||||
alsa->device.Free = rdpsnd_alsa_free;
|
||||
|
||||
data = pEntryPoints->plugin_data;
|
||||
if (data && strcmp((char*)data->data[0], "alsa") == 0)
|
||||
{
|
||||
alsa->device_name = xstrdup((char*)data->data[1]);
|
||||
}
|
||||
if (alsa->device_name == NULL)
|
||||
{
|
||||
alsa->device_name = xstrdup("default");
|
||||
}
|
||||
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;
|
||||
|
||||
pEntryPoints->pRegisterRdpsndDevice(pEntryPoints->rdpsnd, (rdpsndDevicePlugin*)alsa);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -213,6 +213,10 @@ static void rdpsnd_process_message_formats(rdpsndPlugin* rdpsnd, STREAM* data_in
|
|||
stream_seek(data_in, format->cbSize);
|
||||
format->data = NULL;
|
||||
|
||||
DEBUG_SVC("wFormatTag=%d nChannels=%d nSamplesPerSec=%d nBlockAlign=%d wBitsPerSample=%d",
|
||||
format->wFormatTag, format->nChannels, format->nSamplesPerSec,
|
||||
format->nBlockAlign, format->wBitsPerSample);
|
||||
|
||||
if (rdpsnd->fixed_format > 0 && rdpsnd->fixed_format != format->wFormatTag)
|
||||
continue;
|
||||
if (rdpsnd->fixed_channel > 0 && rdpsnd->fixed_channel != format->nChannels)
|
||||
|
@ -221,6 +225,8 @@ static void rdpsnd_process_message_formats(rdpsndPlugin* rdpsnd, STREAM* data_in
|
|||
continue;
|
||||
if (rdpsnd->device && rdpsnd->device->FormatSupported(rdpsnd->device, format))
|
||||
{
|
||||
DEBUG_SVC("format supported.");
|
||||
|
||||
stream_check_size(data_out, 18 + format->cbSize);
|
||||
stream_write(data_out, format_mark, 18 + format->cbSize);
|
||||
if (format->cbSize > 0)
|
||||
|
|
|
@ -36,13 +36,13 @@ struct rdpsnd_format
|
|||
|
||||
typedef struct rdpsnd_device_plugin rdpsndDevicePlugin;
|
||||
|
||||
typedef boolean (*pcFormatSupported) (rdpsndDevicePlugin* devplugin, rdpsndFormat* format);
|
||||
typedef void (*pcOpen) (rdpsndDevicePlugin* devplugin, rdpsndFormat* format);
|
||||
typedef void (*pcSetFormat) (rdpsndDevicePlugin* devplugin, rdpsndFormat* format);
|
||||
typedef void (*pcSetVolume) (rdpsndDevicePlugin* devplugin, uint32 value);
|
||||
typedef void (*pcPlay) (rdpsndDevicePlugin* devplugin, uint8* data, int size);
|
||||
typedef void (*pcClose) (rdpsndDevicePlugin* devplugin);
|
||||
typedef void (*pcFree) (rdpsndDevicePlugin* devplugin);
|
||||
typedef boolean (*pcFormatSupported) (rdpsndDevicePlugin* device, rdpsndFormat* format);
|
||||
typedef void (*pcOpen) (rdpsndDevicePlugin* device, rdpsndFormat* format);
|
||||
typedef void (*pcSetFormat) (rdpsndDevicePlugin* device, rdpsndFormat* format);
|
||||
typedef void (*pcSetVolume) (rdpsndDevicePlugin* device, uint32 value);
|
||||
typedef void (*pcPlay) (rdpsndDevicePlugin* device, uint8* data, int size);
|
||||
typedef void (*pcClose) (rdpsndDevicePlugin* device);
|
||||
typedef void (*pcFree) (rdpsndDevicePlugin* device);
|
||||
|
||||
struct rdpsnd_device_plugin
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue