FreeRDP/channels/audin/client/oss/audin_oss.c

569 lines
13 KiB
C
Raw Normal View History

2015-03-13 01:41:44 +03:00
/**
* FreeRDP: A Remote Desktop Protocol Implementation
* Audio Input Redirection Virtual Channel - OSS implementation
*
* Copyright (c) 2015 Rozhuk Ivan <rozhuk.im@gmail.com>
2015-06-02 10:50:38 +03:00
* Copyright 2015 Thincast Technologies GmbH
* Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
2015-03-13 01:41:44 +03: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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winpr/crt.h>
#include <winpr/synch.h>
2015-07-03 14:26:15 +03:00
#include <winpr/string.h>
2015-03-13 01:41:44 +03:00
#include <winpr/thread.h>
#include <winpr/cmdline.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <limits.h>
#include <unistd.h>
#if defined(__OpenBSD__)
2015-06-22 20:45:56 +03:00
#include <soundcard.h>
#else
2015-06-22 20:45:56 +03:00
#include <sys/soundcard.h>
#endif
2015-03-13 01:41:44 +03:00
#include <sys/ioctl.h>
#include <freerdp/addin.h>
#include <freerdp/codec/dsp.h>
#include <freerdp/channels/rdpsnd.h>
#include "audin_main.h"
2015-06-22 20:45:56 +03:00
typedef struct _AudinOSSDevice
{
2015-03-13 01:41:44 +03:00
IAudinDevice iface;
FREERDP_DSP_CONTEXT* dsp_context;
HANDLE thread;
HANDLE stopEvent;
2015-06-22 20:45:56 +03:00
2015-03-13 01:41:44 +03:00
audinFormat format;
UINT32 FramesPerPacket;
int dev_unit;
AudinReceive receive;
void* user_data;
rdpContext* rdpcontext;
2015-03-13 01:41:44 +03:00
} AudinOSSDevice;
#define OSS_LOG_ERR(_text, _error) \
if (_error != 0) \
WLog_ERR(TAG, "%s: %i - %s\n", _text, _error, strerror(_error));
2015-06-22 20:45:56 +03:00
static int audin_oss_get_format(audinFormat* format)
{
switch (format->wFormatTag)
{
case WAVE_FORMAT_PCM:
switch (format->wBitsPerSample)
{
case 8:
return AFMT_S8;
2015-06-22 20:45:56 +03:00
case 16:
return AFMT_S16_LE;
}
2015-03-13 01:41:44 +03:00
2015-06-22 20:45:56 +03:00
break;
2015-06-22 20:45:56 +03:00
case WAVE_FORMAT_ALAW:
return AFMT_A_LAW;
2015-03-13 01:41:44 +03:00
#if 0 /* This does not work on my desktop. */
2015-06-22 20:45:56 +03:00
case WAVE_FORMAT_MULAW:
return AFMT_MU_LAW;
2015-03-13 01:41:44 +03:00
#endif
2015-06-22 20:45:56 +03:00
case WAVE_FORMAT_ADPCM:
case WAVE_FORMAT_DVI_ADPCM:
return AFMT_S16_LE;
2015-03-13 01:41:44 +03:00
}
return 0;
}
static BOOL audin_oss_format_supported(IAudinDevice* device,
audinFormat* format)
2015-06-22 20:45:56 +03:00
{
2015-03-13 01:41:44 +03:00
int req_fmt = 0;
if (device == NULL || format == NULL)
return FALSE;
2015-06-22 20:45:56 +03:00
switch (format->wFormatTag)
{
case WAVE_FORMAT_PCM:
if (format->cbSize != 0 ||
format->nSamplesPerSec > 48000 ||
(format->wBitsPerSample != 8 && format->wBitsPerSample != 16) ||
(format->nChannels != 1 && format->nChannels != 2))
2015-06-22 20:45:56 +03:00
return FALSE;
break;
2015-06-22 20:45:56 +03:00
case WAVE_FORMAT_ADPCM:
case WAVE_FORMAT_DVI_ADPCM:
if (format->nSamplesPerSec > 48000 ||
format->wBitsPerSample != 4 ||
(format->nChannels != 1 && format->nChannels != 2))
2015-06-22 20:45:56 +03:00
return FALSE;
break;
2015-03-13 01:41:44 +03:00
}
2015-06-22 20:45:56 +03:00
2015-03-13 01:41:44 +03:00
req_fmt = audin_oss_get_format(format);
2015-06-22 20:45:56 +03:00
2015-03-13 01:41:44 +03:00
if (req_fmt == 0)
return FALSE;
return TRUE;
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
static UINT audin_oss_set_format(IAudinDevice* device, audinFormat* format,
UINT32 FramesPerPacket)
2015-06-22 20:45:56 +03:00
{
AudinOSSDevice* oss = (AudinOSSDevice*)device;
2015-03-13 01:41:44 +03:00
if (device == NULL || format == NULL)
2015-06-02 10:50:38 +03:00
return ERROR_INVALID_PARAMETER;
2015-06-22 20:45:56 +03:00
2015-03-13 01:41:44 +03:00
oss->FramesPerPacket = FramesPerPacket;
CopyMemory(&(oss->format), format, sizeof(audinFormat));
2015-06-22 20:45:56 +03:00
switch (format->wFormatTag)
{
case WAVE_FORMAT_ADPCM:
case WAVE_FORMAT_DVI_ADPCM:
oss->FramesPerPacket *= 4; /* Compression ratio. */
oss->format.wBitsPerSample *= 4;
break;
2015-03-13 01:41:44 +03:00
}
2015-06-02 10:50:38 +03:00
return CHANNEL_RC_OK;
2015-03-13 01:41:44 +03:00
}
2015-06-22 20:45:56 +03:00
static void* audin_oss_thread_func(void* arg)
2015-03-13 01:41:44 +03:00
{
char dev_name[PATH_MAX] = "/dev/dsp";
char mixer_name[PATH_MAX] = "/dev/mixer";
int pcm_handle = -1, mixer_handle;
BYTE* buffer = NULL, *encoded_data = NULL;
2015-03-13 01:41:44 +03:00
int tmp, buffer_size, encoded_size;
2015-06-22 20:45:56 +03:00
AudinOSSDevice* oss = (AudinOSSDevice*)arg;
UINT error = 0;
DWORD status;
2015-03-13 01:41:44 +03:00
if (arg == NULL)
{
error = ERROR_INVALID_PARAMETER;
2015-03-13 01:41:44 +03:00
goto err_out;
}
2015-03-13 01:41:44 +03:00
freerdp_channel_init_thread_context(oss->rdpcontext);
2015-03-13 01:41:44 +03:00
if (oss->dev_unit != -1)
2015-06-22 20:45:56 +03:00
{
2015-07-03 14:26:15 +03:00
sprintf_s(dev_name, (PATH_MAX - 1), "/dev/dsp%i", oss->dev_unit);
sprintf_s(mixer_name, PATH_MAX - 1, "/dev/mixer%i", oss->dev_unit);
}
2015-06-22 20:45:56 +03:00
2015-03-13 01:41:44 +03:00
WLog_INFO(TAG, "open: %s", dev_name);
2015-06-22 20:45:56 +03:00
if ((pcm_handle = open(dev_name, O_RDONLY)) < 0)
{
2015-03-13 01:41:44 +03:00
OSS_LOG_ERR("sound dev open failed", errno);
error = ERROR_INTERNAL_ERROR;
2015-03-13 01:41:44 +03:00
goto err_out;
}
/* Set rec volume to 100%. */
2015-06-22 20:45:56 +03:00
if ((mixer_handle = open(mixer_name, O_RDWR)) < 0)
{
OSS_LOG_ERR("mixer open failed, not critical", errno);
2015-06-22 20:45:56 +03:00
}
else
{
tmp = (100 | (100 << 8));
2015-06-22 20:45:56 +03:00
if (ioctl(mixer_handle, MIXER_WRITE(SOUND_MIXER_MIC), &tmp) == -1)
OSS_LOG_ERR("WRITE_MIXER - SOUND_MIXER_MIC, not critical", errno);
2015-06-22 20:45:56 +03:00
tmp = (100 | (100 << 8));
2015-06-22 20:45:56 +03:00
if (ioctl(mixer_handle, MIXER_WRITE(SOUND_MIXER_RECLEV), &tmp) == -1)
OSS_LOG_ERR("WRITE_MIXER - SOUND_MIXER_RECLEV, not critical", errno);
2015-06-22 20:45:56 +03:00
close(mixer_handle);
}
2015-06-22 20:45:56 +03:00
2015-03-13 01:41:44 +03:00
#if 0 /* FreeBSD OSS implementation at this moment (2015.03) does not set PCM_CAP_INPUT flag. */
tmp = 0;
2015-06-22 20:45:56 +03:00
if (ioctl(pcm_handle, SNDCTL_DSP_GETCAPS, &tmp) == -1)
{
2015-03-13 01:41:44 +03:00
OSS_LOG_ERR("SNDCTL_DSP_GETCAPS failed, try ignory", errno);
2015-06-22 20:45:56 +03:00
}
else if ((tmp & PCM_CAP_INPUT) == 0)
{
2015-03-13 01:41:44 +03:00
OSS_LOG_ERR("Device does not supports playback", EOPNOTSUPP);
goto err_out;
}
2015-06-22 20:45:56 +03:00
2015-03-13 01:41:44 +03:00
#endif
/* Set format. */
tmp = audin_oss_get_format(&oss->format);
2015-06-22 20:45:56 +03:00
2015-03-13 01:41:44 +03:00
if (ioctl(pcm_handle, SNDCTL_DSP_SETFMT, &tmp) == -1)
OSS_LOG_ERR("SNDCTL_DSP_SETFMT failed", errno);
2015-06-22 20:45:56 +03:00
2015-03-13 01:41:44 +03:00
tmp = oss->format.nChannels;
2015-06-22 20:45:56 +03:00
2015-03-13 01:41:44 +03:00
if (ioctl(pcm_handle, SNDCTL_DSP_CHANNELS, &tmp) == -1)
OSS_LOG_ERR("SNDCTL_DSP_CHANNELS failed", errno);
2015-06-22 20:45:56 +03:00
2015-03-13 01:41:44 +03:00
tmp = oss->format.nSamplesPerSec;
2015-06-22 20:45:56 +03:00
2015-03-13 01:41:44 +03:00
if (ioctl(pcm_handle, SNDCTL_DSP_SPEED, &tmp) == -1)
OSS_LOG_ERR("SNDCTL_DSP_SPEED failed", errno);
2015-06-22 20:45:56 +03:00
2015-03-13 01:41:44 +03:00
tmp = oss->format.nBlockAlign;
2015-06-22 20:45:56 +03:00
2015-03-13 01:41:44 +03:00
if (ioctl(pcm_handle, SNDCTL_DSP_SETFRAGMENT, &tmp) == -1)
OSS_LOG_ERR("SNDCTL_DSP_SETFRAGMENT failed", errno);
buffer_size = (oss->FramesPerPacket * oss->format.nChannels *
(oss->format.wBitsPerSample / 8));
buffer = (BYTE*)calloc((buffer_size + sizeof(void*)), sizeof(BYTE));
2015-06-22 20:45:56 +03:00
if (NULL == buffer)
{
2015-03-13 01:41:44 +03:00
OSS_LOG_ERR("malloc() fail", errno);
error = ERROR_NOT_ENOUGH_MEMORY;
2015-03-13 01:41:44 +03:00
goto err_out;
}
freerdp_dsp_context_reset_adpcm(oss->dsp_context);
while (1)
2015-06-22 20:45:56 +03:00
{
status = WaitForSingleObject(oss->stopEvent, 0);
if (status == WAIT_FAILED)
{
error = GetLastError();
WLog_ERR(TAG, "WaitForSingleObject failed with error %lu", error);
goto err_out;
}
if (status == WAIT_OBJECT_0)
break;
2015-03-13 01:41:44 +03:00
tmp = read(pcm_handle, buffer, buffer_size);
2015-06-22 20:45:56 +03:00
/* Error happen. */
if (tmp < 0)
{
2015-03-13 01:41:44 +03:00
OSS_LOG_ERR("read() error", errno);
continue;
}
2015-06-22 20:45:56 +03:00
2015-03-13 01:41:44 +03:00
if (tmp < buffer_size) /* Not enouth data. */
continue;
2015-06-22 20:45:56 +03:00
2015-03-13 01:41:44 +03:00
/* Process. */
switch (oss->format.wFormatTag)
{
case WAVE_FORMAT_ADPCM:
if (!oss->dsp_context->encode_ms_adpcm(oss->dsp_context,
buffer, buffer_size, oss->format.nChannels, oss->format.nBlockAlign))
{
error = ERROR_INTERNAL_ERROR;
goto err_out;
}
encoded_data = oss->dsp_context->adpcm_buffer;
encoded_size = oss->dsp_context->adpcm_size;
break;
case WAVE_FORMAT_DVI_ADPCM:
if (!oss->dsp_context->encode_ima_adpcm(oss->dsp_context,
buffer, buffer_size, oss->format.nChannels, oss->format.nBlockAlign))
{
error = ERROR_INTERNAL_ERROR;
goto err_out;
}
encoded_data = oss->dsp_context->adpcm_buffer;
encoded_size = oss->dsp_context->adpcm_size;
break;
default:
encoded_data = buffer;
encoded_size = buffer_size;
break;
2015-03-13 01:41:44 +03:00
}
2015-06-02 10:50:38 +03:00
if ((error = oss->receive(encoded_data, encoded_size, oss->user_data)))
{
WLog_ERR(TAG, "oss->receive failed with error %lu", error);
2015-03-13 01:41:44 +03:00
break;
2015-06-02 10:50:38 +03:00
}
2015-03-13 01:41:44 +03:00
}
err_out:
if (error && oss->rdpcontext)
setChannelError(oss->rdpcontext, error,
"audin_oss_thread_func reported an error");
2015-03-13 01:41:44 +03:00
if (pcm_handle != -1)
2015-06-22 20:45:56 +03:00
{
WLog_INFO(TAG, "close: %s", dev_name);
2015-03-13 01:41:44 +03:00
close(pcm_handle);
}
2015-03-13 01:41:44 +03:00
2015-06-22 20:45:56 +03:00
free(buffer);
2015-03-13 01:41:44 +03:00
ExitThread(0);
return NULL;
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
static UINT audin_oss_open(IAudinDevice* device, AudinReceive receive,
void* user_data)
{
AudinOSSDevice* oss = (AudinOSSDevice*)device;
2015-03-13 01:41:44 +03:00
oss->receive = receive;
oss->user_data = user_data;
2015-06-02 10:50:38 +03:00
if (!(oss->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
{
WLog_ERR(TAG, "CreateEvent failed!");
return ERROR_INTERNAL_ERROR;
}
2015-06-02 10:50:38 +03:00
if (!(oss->thread = CreateThread(NULL, 0,
(LPTHREAD_START_ROUTINE)audin_oss_thread_func, oss, 0, NULL)))
2015-06-02 10:50:38 +03:00
{
WLog_ERR(TAG, "CreateThread failed!");
CloseHandle(oss->stopEvent);
oss->stopEvent = NULL;
return ERROR_INTERNAL_ERROR;
}
return CHANNEL_RC_OK;
2015-03-13 01:41:44 +03:00
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
static UINT audin_oss_close(IAudinDevice* device)
2015-06-22 20:45:56 +03:00
{
UINT error;
AudinOSSDevice* oss = (AudinOSSDevice*)device;
2015-03-13 01:41:44 +03:00
if (device == NULL)
2015-06-02 10:50:38 +03:00
return ERROR_INVALID_PARAMETER;
2015-03-13 01:41:44 +03:00
2015-06-22 20:45:56 +03:00
if (oss->stopEvent != NULL)
{
2015-03-13 01:41:44 +03:00
SetEvent(oss->stopEvent);
if (WaitForSingleObject(oss->thread, INFINITE) == WAIT_FAILED)
{
error = GetLastError();
WLog_ERR(TAG, "WaitForSingleObject failed with error %lu", error);
return error;
}
2015-03-13 01:41:44 +03:00
CloseHandle(oss->stopEvent);
oss->stopEvent = NULL;
CloseHandle(oss->thread);
oss->thread = NULL;
}
oss->receive = NULL;
oss->user_data = NULL;
2015-06-02 10:50:38 +03:00
return CHANNEL_RC_OK;
2015-03-13 01:41:44 +03:00
}
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
static UINT audin_oss_free(IAudinDevice* device)
2015-06-22 20:45:56 +03:00
{
AudinOSSDevice* oss = (AudinOSSDevice*)device;
2015-06-02 10:50:38 +03:00
int error;
2015-03-13 01:41:44 +03:00
if (device == NULL)
2015-06-02 10:50:38 +03:00
return ERROR_INVALID_PARAMETER;
2015-03-13 01:41:44 +03:00
2015-06-02 10:50:38 +03:00
if ((error = audin_oss_close(device)))
{
WLog_ERR(TAG, "audin_oss_close failed with error code %d!", error);
}
2015-03-13 01:41:44 +03:00
freerdp_dsp_context_free(oss->dsp_context);
free(oss);
2015-06-02 10:50:38 +03:00
return CHANNEL_RC_OK;
2015-03-13 01:41:44 +03:00
}
2015-06-22 20:45:56 +03:00
COMMAND_LINE_ARGUMENT_A audin_oss_args[] =
{
2015-03-13 01:41:44 +03:00
{ "dev", COMMAND_LINE_VALUE_REQUIRED, "<device>", NULL, NULL, -1, NULL, "audio device name" },
{ NULL, 0, NULL, NULL, NULL, -1, NULL, NULL }
};
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
static UINT audin_oss_parse_addin_args(AudinOSSDevice* device, ADDIN_ARGV* args)
2015-06-22 20:45:56 +03:00
{
2015-03-13 01:41:44 +03:00
int status;
2015-06-22 20:45:56 +03:00
char* str_num, *eptr;
2015-03-13 01:41:44 +03:00
DWORD flags;
2015-06-22 20:45:56 +03:00
COMMAND_LINE_ARGUMENT_A* arg;
AudinOSSDevice* oss = (AudinOSSDevice*)device;
flags = COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON |
COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
status = CommandLineParseArgumentsA(args->argc, (const char**)args->argv,
audin_oss_args, flags, oss, NULL, NULL);
2015-06-22 20:45:56 +03:00
2015-03-13 01:41:44 +03:00
if (status < 0)
2015-06-02 10:50:38 +03:00
return ERROR_INVALID_PARAMETER;
2015-03-13 01:41:44 +03:00
arg = audin_oss_args;
2015-06-22 20:45:56 +03:00
do
{
2015-03-13 01:41:44 +03:00
if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
continue;
CommandLineSwitchStart(arg)
2015-06-22 20:45:56 +03:00
CommandLineSwitchCase(arg, "dev")
{
2015-03-13 01:41:44 +03:00
str_num = _strdup(arg->Value);
2015-06-02 10:50:38 +03:00
if (!str_num)
{
WLog_ERR(TAG, "_strdup failed!");
return CHANNEL_RC_NO_MEMORY;
}
2015-03-13 01:41:44 +03:00
oss->dev_unit = strtol(str_num, &eptr, 10);
2015-06-22 20:45:56 +03:00
2015-03-13 01:41:44 +03:00
if (oss->dev_unit < 0 || *eptr != '\0')
oss->dev_unit = -1;
2015-06-22 20:45:56 +03:00
2015-03-13 01:41:44 +03:00
free(str_num);
}
CommandLineSwitchEnd(arg)
2015-06-22 20:45:56 +03:00
}
while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
2015-06-02 10:50:38 +03:00
return CHANNEL_RC_OK;
2015-03-13 01:41:44 +03:00
}
#ifdef BUILTIN_CHANNELS
2015-03-13 01:41:44 +03:00
#define freerdp_audin_client_subsystem_entry oss_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
2015-03-13 01:41:44 +03:00
#endif
/**
* Function description
*
* @return 0 on success, otherwise a Win32 error code
*/
UINT freerdp_audin_client_subsystem_entry(PFREERDP_AUDIN_DEVICE_ENTRY_POINTS
pEntryPoints)
2015-06-22 20:45:56 +03:00
{
ADDIN_ARGV* args;
AudinOSSDevice* oss;
UINT error;
2015-06-02 10:50:38 +03:00
oss = (AudinOSSDevice*)calloc(1, sizeof(AudinOSSDevice));
2015-06-02 10:50:38 +03:00
if (!oss)
{
WLog_ERR(TAG, "calloc failed!");
return CHANNEL_RC_NO_MEMORY;
}
2015-03-13 01:41:44 +03:00
oss->iface.Open = audin_oss_open;
oss->iface.FormatSupported = audin_oss_format_supported;
oss->iface.SetFormat = audin_oss_set_format;
oss->iface.Close = audin_oss_close;
oss->iface.Free = audin_oss_free;
oss->rdpcontext = pEntryPoints->rdpcontext;
2015-03-13 01:41:44 +03:00
oss->dev_unit = -1;
args = pEntryPoints->args;
2015-06-02 10:50:38 +03:00
if ((error = audin_oss_parse_addin_args(oss, args)))
{
WLog_ERR(TAG, "audin_oss_parse_addin_args failed with errorcode %lu!", error);
goto error_out;
}
2015-03-13 01:41:44 +03:00
oss->dsp_context = freerdp_dsp_context_new();
2015-06-02 10:50:38 +03:00
if (!oss->dsp_context)
{
WLog_ERR(TAG, "freerdp_dsp_context_new failed!");
error = CHANNEL_RC_NO_MEMORY;
goto error_out;
}
2015-03-13 01:41:44 +03:00
if ((error = pEntryPoints->pRegisterAudinDevice(pEntryPoints->plugin,
(IAudinDevice*) oss)))
2015-06-02 10:50:38 +03:00
{
WLog_ERR(TAG, "RegisterAudinDevice failed with error %lu!", error);
goto error_out;
}
return CHANNEL_RC_OK;
error_out:
freerdp_dsp_context_free(oss->dsp_context);
free(oss);
return error;
2015-03-13 01:41:44 +03:00
}