2011-09-21 09:28:02 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-09-21 09:28:02 +04:00
|
|
|
* Audio Input Redirection Virtual Channel - ALSA implementation
|
|
|
|
*
|
|
|
|
* Copyright 2010-2011 Vic Lee
|
2015-06-02 10:50:38 +03:00
|
|
|
* Copyright 2015 Thincast Technologies GmbH
|
|
|
|
* Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
|
2011-09-21 09:28:02 +04:00
|
|
|
*
|
|
|
|
* 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-21 09:28:02 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
2012-11-20 07:31:15 +04:00
|
|
|
#include <winpr/crt.h>
|
2013-03-21 04:44:31 +04:00
|
|
|
#include <winpr/synch.h>
|
|
|
|
#include <winpr/thread.h>
|
2012-11-20 07:31:15 +04:00
|
|
|
#include <winpr/cmdline.h>
|
|
|
|
|
2011-09-21 09:28:02 +04:00
|
|
|
#include <alsa/asoundlib.h>
|
2012-11-20 07:31:15 +04:00
|
|
|
|
|
|
|
#include <freerdp/addin.h>
|
2013-02-20 02:47:55 +04:00
|
|
|
#include <freerdp/codec/dsp.h>
|
2013-02-21 06:34:47 +04:00
|
|
|
#include <freerdp/channels/rdpsnd.h>
|
2011-09-21 09:28:02 +04:00
|
|
|
|
|
|
|
#include "audin_main.h"
|
|
|
|
|
|
|
|
typedef struct _AudinALSADevice
|
|
|
|
{
|
|
|
|
IAudinDevice iface;
|
|
|
|
|
2012-11-20 07:31:15 +04:00
|
|
|
char* device_name;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 frames_per_packet;
|
|
|
|
UINT32 target_rate;
|
|
|
|
UINT32 actual_rate;
|
2011-09-21 09:28:02 +04:00
|
|
|
snd_pcm_format_t format;
|
2012-10-09 11:26:39 +04:00
|
|
|
UINT32 target_channels;
|
|
|
|
UINT32 actual_channels;
|
2011-09-21 09:28:02 +04:00
|
|
|
int bytes_per_channel;
|
|
|
|
int wformat;
|
|
|
|
int block_size;
|
2012-05-09 12:15:23 +04:00
|
|
|
|
|
|
|
FREERDP_DSP_CONTEXT* dsp_context;
|
2011-09-21 09:28:02 +04:00
|
|
|
|
2013-03-21 04:44:31 +04:00
|
|
|
HANDLE thread;
|
|
|
|
HANDLE stopEvent;
|
2011-09-21 09:28:02 +04:00
|
|
|
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE* buffer;
|
2011-09-21 09:28:02 +04:00
|
|
|
int buffer_frames;
|
|
|
|
|
|
|
|
AudinReceive receive;
|
|
|
|
void* user_data;
|
2015-07-15 10:50:35 +03:00
|
|
|
|
|
|
|
rdpContext* rdpcontext;
|
2011-09-21 09:28:02 +04:00
|
|
|
} AudinALSADevice;
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
static BOOL audin_alsa_set_params(AudinALSADevice* alsa, snd_pcm_t* capture_handle)
|
2011-09-21 09:28:02 +04:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
snd_pcm_hw_params_t* hw_params;
|
|
|
|
|
|
|
|
if ((error = snd_pcm_hw_params_malloc(&hw_params)) < 0)
|
|
|
|
{
|
2014-09-12 18:19:32 +04:00
|
|
|
WLog_ERR(TAG, "snd_pcm_hw_params_malloc (%s)",
|
|
|
|
snd_strerror(error));
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-09-21 09:28:02 +04:00
|
|
|
}
|
2013-02-21 06:34:47 +04:00
|
|
|
|
2011-09-21 09:28:02 +04:00
|
|
|
snd_pcm_hw_params_any(capture_handle, hw_params);
|
2013-02-21 06:34:47 +04:00
|
|
|
snd_pcm_hw_params_set_access(capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
|
|
|
|
snd_pcm_hw_params_set_format(capture_handle, hw_params, alsa->format);
|
|
|
|
snd_pcm_hw_params_set_rate_near(capture_handle, hw_params, &alsa->actual_rate, NULL);
|
|
|
|
snd_pcm_hw_params_set_channels_near(capture_handle, hw_params, &alsa->actual_channels);
|
2011-09-21 09:28:02 +04:00
|
|
|
snd_pcm_hw_params(capture_handle, hw_params);
|
|
|
|
snd_pcm_hw_params_free(hw_params);
|
|
|
|
snd_pcm_prepare(capture_handle);
|
|
|
|
|
|
|
|
if ((alsa->actual_rate != alsa->target_rate) ||
|
|
|
|
(alsa->actual_channels != alsa->target_channels))
|
|
|
|
{
|
|
|
|
DEBUG_DVC("actual rate %d / channel %d is "
|
|
|
|
"different from target rate %d / channel %d, resampling required.",
|
|
|
|
alsa->actual_rate, alsa->actual_channels,
|
|
|
|
alsa->target_rate, alsa->target_channels);
|
|
|
|
}
|
2013-02-21 06:34:47 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2011-09-21 09:28:02 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT audin_alsa_thread_receive(AudinALSADevice* alsa, BYTE* src, int size)
|
2011-09-21 09:28:02 +04:00
|
|
|
{
|
|
|
|
int frames;
|
|
|
|
int cframes;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT ret = CHANNEL_RC_OK;
|
2011-09-21 09:28:02 +04:00
|
|
|
int encoded_size;
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE* encoded_data;
|
2011-09-21 09:28:02 +04:00
|
|
|
int rbytes_per_frame;
|
|
|
|
int tbytes_per_frame;
|
2015-07-30 16:49:21 +03:00
|
|
|
int status;
|
2011-09-21 09:28:02 +04:00
|
|
|
|
|
|
|
rbytes_per_frame = alsa->actual_channels * alsa->bytes_per_channel;
|
|
|
|
tbytes_per_frame = alsa->target_channels * alsa->bytes_per_channel;
|
|
|
|
|
|
|
|
if ((alsa->target_rate == alsa->actual_rate) &&
|
|
|
|
(alsa->target_channels == alsa->actual_channels))
|
|
|
|
{
|
|
|
|
frames = size / rbytes_per_frame;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-05-09 12:15:23 +04:00
|
|
|
alsa->dsp_context->resample(alsa->dsp_context, src, alsa->bytes_per_channel,
|
2011-09-21 09:28:02 +04:00
|
|
|
alsa->actual_channels, alsa->actual_rate, size / rbytes_per_frame,
|
2012-05-09 12:15:23 +04:00
|
|
|
alsa->target_channels, alsa->target_rate);
|
|
|
|
frames = alsa->dsp_context->resampled_frames;
|
2011-09-21 09:28:02 +04:00
|
|
|
DEBUG_DVC("resampled %d frames at %d to %d frames at %d",
|
|
|
|
size / rbytes_per_frame, alsa->actual_rate, frames, alsa->target_rate);
|
|
|
|
size = frames * tbytes_per_frame;
|
2012-05-09 12:15:23 +04:00
|
|
|
src = alsa->dsp_context->resampled_buffer;
|
2011-09-21 09:28:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
while (frames > 0)
|
|
|
|
{
|
2015-07-30 16:49:21 +03:00
|
|
|
status = WaitForSingleObject(alsa->stopEvent, 0);
|
|
|
|
|
|
|
|
if (status == WAIT_FAILED)
|
|
|
|
{
|
|
|
|
ret = GetLastError();
|
|
|
|
WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", ret);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status == WAIT_OBJECT_0)
|
2011-10-19 18:55:54 +04:00
|
|
|
break;
|
|
|
|
|
2011-09-21 09:28:02 +04:00
|
|
|
cframes = alsa->frames_per_packet - alsa->buffer_frames;
|
2013-02-21 06:34:47 +04:00
|
|
|
|
2011-09-21 09:28:02 +04:00
|
|
|
if (cframes > frames)
|
|
|
|
cframes = frames;
|
2013-02-21 06:34:47 +04:00
|
|
|
|
|
|
|
CopyMemory(alsa->buffer + alsa->buffer_frames * tbytes_per_frame, src, cframes * tbytes_per_frame);
|
|
|
|
|
2011-09-21 09:28:02 +04:00
|
|
|
alsa->buffer_frames += cframes;
|
2013-02-21 06:34:47 +04:00
|
|
|
|
2011-09-21 09:28:02 +04:00
|
|
|
if (alsa->buffer_frames >= alsa->frames_per_packet)
|
|
|
|
{
|
2013-02-21 06:34:47 +04:00
|
|
|
if (alsa->wformat == WAVE_FORMAT_DVI_ADPCM)
|
2011-09-21 09:28:02 +04:00
|
|
|
{
|
2015-07-15 10:50:35 +03:00
|
|
|
if (!alsa->dsp_context->encode_ima_adpcm(alsa->dsp_context,
|
2011-09-21 09:28:02 +04:00
|
|
|
alsa->buffer, alsa->buffer_frames * tbytes_per_frame,
|
2015-07-15 10:50:35 +03:00
|
|
|
alsa->target_channels, alsa->block_size))
|
|
|
|
{
|
|
|
|
ret = ERROR_INTERNAL_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2013-02-21 06:34:47 +04:00
|
|
|
|
2012-05-09 12:15:23 +04:00
|
|
|
encoded_data = alsa->dsp_context->adpcm_buffer;
|
|
|
|
encoded_size = alsa->dsp_context->adpcm_size;
|
2013-02-21 06:34:47 +04:00
|
|
|
|
2011-09-21 09:28:02 +04:00
|
|
|
DEBUG_DVC("encoded %d to %d",
|
|
|
|
alsa->buffer_frames * tbytes_per_frame, encoded_size);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
encoded_data = alsa->buffer;
|
|
|
|
encoded_size = alsa->buffer_frames * tbytes_per_frame;
|
|
|
|
}
|
|
|
|
|
2015-07-30 16:49:21 +03:00
|
|
|
status = WaitForSingleObject(alsa->stopEvent, 0);
|
|
|
|
|
|
|
|
if (status == WAIT_FAILED)
|
|
|
|
{
|
|
|
|
ret = GetLastError();
|
|
|
|
WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", ret);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status == WAIT_OBJECT_0)
|
2013-09-05 11:33:12 +04:00
|
|
|
break;
|
2011-10-19 18:55:54 +04:00
|
|
|
else
|
2013-09-27 13:39:04 +04:00
|
|
|
{
|
|
|
|
DEBUG_DVC("encoded %d [%d] to %d [%X]", alsa->buffer_frames,
|
|
|
|
tbytes_per_frame, encoded_size,
|
|
|
|
alsa->wformat);
|
2011-10-19 18:55:54 +04:00
|
|
|
ret = alsa->receive(encoded_data, encoded_size, alsa->user_data);
|
2013-09-27 13:39:04 +04:00
|
|
|
}
|
2013-02-21 06:34:47 +04:00
|
|
|
|
2011-09-21 09:28:02 +04:00
|
|
|
alsa->buffer_frames = 0;
|
2013-02-21 06:34:47 +04:00
|
|
|
|
2015-06-02 10:50:38 +03:00
|
|
|
if (ret != CHANNEL_RC_OK)
|
2011-09-21 09:28:02 +04:00
|
|
|
break;
|
|
|
|
}
|
2013-02-21 06:34:47 +04:00
|
|
|
|
2011-09-21 09:28:02 +04:00
|
|
|
src += cframes * tbytes_per_frame;
|
|
|
|
frames -= cframes;
|
|
|
|
}
|
|
|
|
|
2015-06-02 10:50:38 +03:00
|
|
|
return ret;
|
2011-09-21 09:28:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void* audin_alsa_thread_func(void* arg)
|
|
|
|
{
|
2015-06-02 10:50:38 +03:00
|
|
|
long error;
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE* buffer;
|
2011-09-21 09:28:02 +04:00
|
|
|
int rbytes_per_frame;
|
|
|
|
int tbytes_per_frame;
|
|
|
|
snd_pcm_t* capture_handle = NULL;
|
|
|
|
AudinALSADevice* alsa = (AudinALSADevice*) arg;
|
2015-07-30 16:49:21 +03:00
|
|
|
DWORD status;
|
2011-09-21 09:28:02 +04:00
|
|
|
|
|
|
|
DEBUG_DVC("in");
|
|
|
|
|
|
|
|
rbytes_per_frame = alsa->actual_channels * alsa->bytes_per_channel;
|
|
|
|
tbytes_per_frame = alsa->target_channels * alsa->bytes_per_channel;
|
2015-06-02 10:50:38 +03:00
|
|
|
buffer = (BYTE*) calloc(1, rbytes_per_frame * alsa->frames_per_packet);
|
|
|
|
if (!buffer)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "calloc failed!");
|
2015-07-30 16:49:21 +03:00
|
|
|
error = CHANNEL_RC_NO_MEMORY;
|
2015-07-15 10:50:35 +03:00
|
|
|
if (alsa->rdpcontext)
|
2015-07-30 16:49:21 +03:00
|
|
|
setChannelError(alsa->rdpcontext, error, "calloc failed!");
|
|
|
|
goto out;
|
2015-06-02 10:50:38 +03:00
|
|
|
}
|
|
|
|
|
2012-05-09 12:15:23 +04:00
|
|
|
freerdp_dsp_context_reset_adpcm(alsa->dsp_context);
|
2012-11-20 08:49:08 +04:00
|
|
|
|
2015-07-30 16:49:21 +03:00
|
|
|
if ((error = snd_pcm_open(&capture_handle, alsa->device_name, SND_PCM_STREAM_CAPTURE, 0)) < 0)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "snd_pcm_open (%s)", snd_strerror(error));
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!audin_alsa_set_params(alsa, capture_handle))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "audin_alsa_set_params failed");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
status = WaitForSingleObject(alsa->stopEvent, 0);
|
|
|
|
|
|
|
|
if (status == WAIT_FAILED)
|
|
|
|
{
|
|
|
|
error = GetLastError();
|
|
|
|
WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", error);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status == WAIT_OBJECT_0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
error = snd_pcm_readi(capture_handle, buffer, alsa->frames_per_packet);
|
|
|
|
|
|
|
|
if (error == -EPIPE)
|
|
|
|
{
|
|
|
|
snd_pcm_recover(capture_handle, error, 0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (error < 0)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "snd_pcm_readi (%s)", snd_strerror(error));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((error = audin_alsa_thread_receive(alsa, buffer, error * rbytes_per_frame)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "audin_alsa_thread_receive failed with error %lu", error);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2011-09-21 09:28:02 +04:00
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
free(buffer);
|
2013-02-21 06:34:47 +04:00
|
|
|
|
2011-09-21 09:28:02 +04:00
|
|
|
if (capture_handle)
|
|
|
|
snd_pcm_close(capture_handle);
|
2015-07-30 16:49:21 +03:00
|
|
|
out:
|
2011-09-21 09:28:02 +04:00
|
|
|
DEBUG_DVC("out");
|
2015-07-15 10:50:35 +03:00
|
|
|
if (error && alsa->rdpcontext)
|
|
|
|
setChannelError(alsa->rdpcontext, error, "audin_alsa_thread_func reported an error");
|
2015-06-02 10:50:38 +03:00
|
|
|
ExitThread((DWORD)error);
|
2011-09-21 09:28:02 +04:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT audin_alsa_free(IAudinDevice* device)
|
2011-09-21 09:28:02 +04:00
|
|
|
{
|
|
|
|
AudinALSADevice* alsa = (AudinALSADevice*) device;
|
|
|
|
|
2012-05-09 12:15:23 +04:00
|
|
|
freerdp_dsp_context_free(alsa->dsp_context);
|
2012-11-20 07:31:15 +04:00
|
|
|
|
|
|
|
free(alsa->device_name);
|
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
free(alsa);
|
2015-06-02 10:50:38 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2011-09-21 09:28:02 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 10:38:39 +04:00
|
|
|
static BOOL audin_alsa_format_supported(IAudinDevice* device, audinFormat* format)
|
2011-09-21 09:28:02 +04:00
|
|
|
{
|
|
|
|
switch (format->wFormatTag)
|
|
|
|
{
|
2013-02-21 06:34:47 +04:00
|
|
|
case WAVE_FORMAT_PCM:
|
2011-09-21 09:28:02 +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-09-21 09:28:02 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-02-21 06:34:47 +04:00
|
|
|
case WAVE_FORMAT_DVI_ADPCM:
|
2011-09-21 09:28:02 +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-09-21 09:28:02 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2013-02-21 06:34:47 +04:00
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2011-09-21 09:28:02 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT audin_alsa_set_format(IAudinDevice* device, audinFormat* format, UINT32 FramesPerPacket)
|
2011-09-21 09:28:02 +04:00
|
|
|
{
|
|
|
|
int bs;
|
|
|
|
AudinALSADevice* alsa = (AudinALSADevice*) device;
|
|
|
|
|
|
|
|
alsa->target_rate = format->nSamplesPerSec;
|
|
|
|
alsa->actual_rate = format->nSamplesPerSec;
|
|
|
|
alsa->target_channels = format->nChannels;
|
|
|
|
alsa->actual_channels = format->nChannels;
|
2013-02-21 06:34:47 +04:00
|
|
|
|
2011-09-21 09:28:02 +04:00
|
|
|
switch (format->wFormatTag)
|
|
|
|
{
|
2013-02-21 06:34:47 +04:00
|
|
|
case WAVE_FORMAT_PCM:
|
2011-09-21 09:28:02 +04:00
|
|
|
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;
|
|
|
|
|
2013-02-21 06:34:47 +04:00
|
|
|
case WAVE_FORMAT_DVI_ADPCM:
|
2011-09-21 09:28:02 +04:00
|
|
|
alsa->format = SND_PCM_FORMAT_S16_LE;
|
|
|
|
alsa->bytes_per_channel = 2;
|
|
|
|
bs = (format->nBlockAlign - 4 * format->nChannels) * 4;
|
|
|
|
alsa->frames_per_packet = (alsa->frames_per_packet * format->nChannels * 2 /
|
|
|
|
bs + 1) * bs / (format->nChannels * 2);
|
|
|
|
DEBUG_DVC("aligned FramesPerPacket=%d",
|
|
|
|
alsa->frames_per_packet);
|
|
|
|
break;
|
|
|
|
}
|
2013-02-21 06:34:47 +04:00
|
|
|
|
2011-09-21 09:28:02 +04:00
|
|
|
alsa->wformat = format->wFormatTag;
|
|
|
|
alsa->block_size = format->nBlockAlign;
|
2015-06-02 10:50:38 +03:00
|
|
|
return CHANNEL_RC_OK;
|
2011-09-21 09:28:02 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT audin_alsa_open(IAudinDevice* device, AudinReceive receive, void* user_data)
|
2011-09-21 09:28:02 +04:00
|
|
|
{
|
2013-09-05 11:33:12 +04:00
|
|
|
int tbytes_per_frame;
|
2011-09-21 09:28:02 +04:00
|
|
|
AudinALSADevice* alsa = (AudinALSADevice*) device;
|
|
|
|
|
|
|
|
alsa->receive = receive;
|
|
|
|
alsa->user_data = user_data;
|
|
|
|
|
2013-09-05 11:33:12 +04:00
|
|
|
tbytes_per_frame = alsa->target_channels * alsa->bytes_per_channel;
|
2015-06-02 10:50:38 +03:00
|
|
|
alsa->buffer = (BYTE*) calloc(1, tbytes_per_frame * alsa->frames_per_packet);
|
|
|
|
if (!alsa->buffer)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "calloc failed!");
|
|
|
|
return ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
}
|
2013-09-05 11:33:12 +04:00
|
|
|
alsa->buffer_frames = 0;
|
2013-09-05 15:41:32 +04:00
|
|
|
|
2015-06-02 10:50:38 +03:00
|
|
|
if (!(alsa->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "CreateEvent failed!");
|
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
if (!(alsa->thread = CreateThread(NULL, 0,
|
|
|
|
(LPTHREAD_START_ROUTINE) audin_alsa_thread_func, alsa, 0, NULL)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "CreateThread failed!");
|
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
return CHANNEL_RC_OK;
|
|
|
|
error_out:
|
|
|
|
free(alsa->buffer);
|
|
|
|
alsa->buffer = NULL;
|
|
|
|
CloseHandle(alsa->stopEvent);
|
|
|
|
alsa->stopEvent = NULL;
|
|
|
|
return ERROR_INTERNAL_ERROR;
|
2011-09-21 09:28:02 +04:00
|
|
|
}
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT audin_alsa_close(IAudinDevice* device)
|
2011-09-21 09:28:02 +04:00
|
|
|
{
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error = CHANNEL_RC_OK;
|
2011-09-21 09:28:02 +04:00
|
|
|
AudinALSADevice* alsa = (AudinALSADevice*) device;
|
|
|
|
|
2014-12-27 21:50:50 +03:00
|
|
|
if (alsa->stopEvent)
|
|
|
|
{
|
|
|
|
SetEvent(alsa->stopEvent);
|
2015-07-30 16:49:21 +03:00
|
|
|
if (WaitForSingleObject(alsa->thread, INFINITE) == WAIT_FAILED)
|
|
|
|
{
|
|
|
|
error = GetLastError();
|
|
|
|
WLog_ERR(TAG, "WaitForSingleObject failed with error %lu", error);
|
|
|
|
return error;
|
|
|
|
}
|
2014-12-27 21:50:50 +03:00
|
|
|
|
|
|
|
CloseHandle(alsa->stopEvent);
|
|
|
|
alsa->stopEvent = NULL;
|
|
|
|
|
|
|
|
CloseHandle(alsa->thread);
|
|
|
|
alsa->thread = NULL;
|
|
|
|
}
|
2013-09-05 11:33:12 +04:00
|
|
|
|
2015-05-11 10:07:39 +03:00
|
|
|
free(alsa->buffer);
|
2013-09-05 11:33:12 +04:00
|
|
|
alsa->buffer = NULL;
|
2011-09-21 09:28:02 +04:00
|
|
|
|
|
|
|
alsa->receive = NULL;
|
|
|
|
alsa->user_data = NULL;
|
2015-06-02 10:50:38 +03:00
|
|
|
|
2015-07-30 16:49:21 +03:00
|
|
|
return error;
|
2011-09-21 09:28:02 +04:00
|
|
|
}
|
|
|
|
|
2012-11-20 07:31:15 +04:00
|
|
|
COMMAND_LINE_ARGUMENT_A audin_alsa_args[] =
|
|
|
|
{
|
2015-03-13 01:28:44 +03:00
|
|
|
{ "dev", COMMAND_LINE_VALUE_REQUIRED, "<device>", NULL, NULL, -1, NULL, "audio device name" },
|
2012-11-20 07:31:15 +04:00
|
|
|
{ NULL, 0, NULL, NULL, NULL, -1, NULL, NULL }
|
|
|
|
};
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
static UINT audin_alsa_parse_addin_args(AudinALSADevice* device, ADDIN_ARGV* args)
|
2012-11-20 07:31:15 +04:00
|
|
|
{
|
|
|
|
int status;
|
|
|
|
DWORD flags;
|
|
|
|
COMMAND_LINE_ARGUMENT_A* arg;
|
|
|
|
AudinALSADevice* alsa = (AudinALSADevice*) device;
|
|
|
|
|
2015-03-13 01:28:44 +03:00
|
|
|
flags = COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
|
2012-11-20 07:31:15 +04:00
|
|
|
|
|
|
|
status = CommandLineParseArgumentsA(args->argc, (const char**) args->argv, audin_alsa_args, flags, alsa, NULL, NULL);
|
|
|
|
|
|
|
|
arg = audin_alsa_args;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
CommandLineSwitchStart(arg)
|
|
|
|
|
2015-03-13 01:28:44 +03:00
|
|
|
CommandLineSwitchCase(arg, "dev")
|
2012-11-20 07:31:15 +04:00
|
|
|
{
|
|
|
|
alsa->device_name = _strdup(arg->Value);
|
2015-06-02 10:50:38 +03:00
|
|
|
if(!alsa->device_name)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "_strdup failed!");
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
2012-11-20 07:31:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
CommandLineSwitchEnd(arg)
|
|
|
|
}
|
|
|
|
while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
|
2015-06-02 10:50:38 +03:00
|
|
|
|
|
|
|
return CHANNEL_RC_OK;
|
2012-11-20 07:31:15 +04:00
|
|
|
}
|
|
|
|
|
2012-11-06 04:55:15 +04:00
|
|
|
#ifdef STATIC_CHANNELS
|
|
|
|
#define freerdp_audin_client_subsystem_entry alsa_freerdp_audin_client_subsystem_entry
|
2016-03-03 19:11:26 +03:00
|
|
|
#else
|
|
|
|
#define freerdp_audin_client_subsystem_entry FREERDP_API freerdp_audin_client_subsystem_entry
|
2012-11-06 04:55:15 +04:00
|
|
|
#endif
|
|
|
|
|
2015-08-27 15:25:09 +03:00
|
|
|
/**
|
|
|
|
* Function description
|
|
|
|
*
|
|
|
|
* @return 0 on success, otherwise a Win32 error code
|
|
|
|
*/
|
|
|
|
UINT freerdp_audin_client_subsystem_entry(PFREERDP_AUDIN_DEVICE_ENTRY_POINTS pEntryPoints)
|
2011-09-21 09:28:02 +04:00
|
|
|
{
|
2012-11-20 07:31:15 +04:00
|
|
|
ADDIN_ARGV* args;
|
2011-09-21 09:28:02 +04:00
|
|
|
AudinALSADevice* alsa;
|
2015-08-27 15:25:09 +03:00
|
|
|
UINT error;
|
2011-09-21 09:28:02 +04:00
|
|
|
|
2015-06-02 10:50:38 +03:00
|
|
|
alsa = (AudinALSADevice*) calloc(1, sizeof(AudinALSADevice));
|
|
|
|
if (!alsa)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "calloc failed!");
|
|
|
|
return CHANNEL_RC_NO_MEMORY;
|
|
|
|
}
|
2011-09-21 09:28:02 +04:00
|
|
|
|
|
|
|
alsa->iface.Open = audin_alsa_open;
|
|
|
|
alsa->iface.FormatSupported = audin_alsa_format_supported;
|
|
|
|
alsa->iface.SetFormat = audin_alsa_set_format;
|
|
|
|
alsa->iface.Close = audin_alsa_close;
|
|
|
|
alsa->iface.Free = audin_alsa_free;
|
2015-07-15 10:50:35 +03:00
|
|
|
alsa->rdpcontext = pEntryPoints->rdpcontext;
|
2011-09-21 09:28:02 +04:00
|
|
|
|
2012-11-20 07:31:15 +04:00
|
|
|
args = pEntryPoints->args;
|
2012-04-18 21:16:50 +04:00
|
|
|
|
2015-06-02 10:50:38 +03:00
|
|
|
if ((error = audin_alsa_parse_addin_args(alsa, args)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "audin_alsa_parse_addin_args failed with errorcode %lu!", error);
|
|
|
|
goto error_out;
|
|
|
|
}
|
2012-11-20 07:31:15 +04:00
|
|
|
|
|
|
|
if (!alsa->device_name)
|
2015-06-02 10:50:38 +03:00
|
|
|
{
|
2012-11-20 07:31:15 +04:00
|
|
|
alsa->device_name = _strdup("default");
|
2015-06-02 10:50:38 +03:00
|
|
|
if (!alsa->device_name)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "_strdup failed!");
|
|
|
|
error = CHANNEL_RC_NO_MEMORY;
|
|
|
|
goto error_out;
|
|
|
|
}
|
|
|
|
}
|
2011-09-21 09:28:02 +04:00
|
|
|
|
|
|
|
alsa->frames_per_packet = 128;
|
|
|
|
alsa->target_rate = 22050;
|
|
|
|
alsa->actual_rate = 22050;
|
|
|
|
alsa->format = SND_PCM_FORMAT_S16_LE;
|
|
|
|
alsa->target_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();
|
2015-06-02 10:50:38 +03:00
|
|
|
if (!alsa->dsp_context)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "freerdp_dsp_context_new failed!");
|
|
|
|
error = CHANNEL_RC_NO_MEMORY;
|
|
|
|
goto error_out;
|
|
|
|
}
|
2012-05-09 12:15:23 +04:00
|
|
|
|
2015-06-02 10:50:38 +03:00
|
|
|
if ((error = pEntryPoints->pRegisterAudinDevice(pEntryPoints->plugin, (IAudinDevice*) alsa)))
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "RegisterAudinDevice failed with error %lu!", error);
|
|
|
|
goto error_out;
|
|
|
|
}
|
2011-09-21 09:28:02 +04:00
|
|
|
|
2015-06-02 10:50:38 +03:00
|
|
|
return CHANNEL_RC_OK;
|
|
|
|
error_out:
|
|
|
|
freerdp_dsp_context_free(alsa->dsp_context);
|
|
|
|
free(alsa->device_name);
|
|
|
|
free(alsa);
|
|
|
|
return error;
|
2011-09-21 09:28:02 +04:00
|
|
|
}
|