FreeRDP/channels/tsmf/client/oss/tsmf_oss.c

253 lines
5.7 KiB
C
Raw Normal View History

/**
* FreeRDP: A Remote Desktop Protocol Implementation
* Video Redirection Virtual Channel - OSS Audio Device
*
* Copyright (c) 2015 Rozhuk Ivan <rozhuk.im@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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <winpr/crt.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <limits.h>
#include <unistd.h>
#include <sys/soundcard.h>
#include <sys/ioctl.h>
#include <freerdp/types.h>
#include <freerdp/codec/dsp.h>
#include "tsmf_audio.h"
2015-06-22 20:45:56 +03:00
typedef struct _TSMFOSSAudioDevice
{
ITSMFAudioDevice iface;
char dev_name[PATH_MAX];
int pcm_handle;
2015-06-22 20:45:56 +03:00
UINT32 sample_rate;
UINT32 channels;
UINT32 bits_per_sample;
UINT32 data_size_last;
} TSMFOssAudioDevice;
#define OSS_LOG_ERR(_text, _error) \
if (_error != 0) \
WLog_ERR(TAG, "%s: %i - %s", _text, _error, strerror(_error));
2015-06-22 20:45:56 +03:00
static BOOL tsmf_oss_open(ITSMFAudioDevice* audio, const char* device)
{
int tmp;
int error;
2015-06-22 20:45:56 +03:00
TSMFOssAudioDevice* oss = (TSMFOssAudioDevice*)audio;
if (oss == NULL || oss->pcm_handle != -1)
return FALSE;
2015-06-22 20:45:56 +03:00
if (device == NULL) /* Default device. */
{
strncpy(oss->dev_name, "/dev/dsp", sizeof(oss->dev_name));
2015-06-22 20:45:56 +03:00
}
else
{
strncpy(oss->dev_name, device, sizeof(oss->dev_name));
}
2015-06-22 20:45:56 +03:00
if ((oss->pcm_handle = open(oss->dev_name, O_WRONLY)) < 0)
{
OSS_LOG_ERR("sound dev open failed", errno);
oss->pcm_handle = -1;
return FALSE;
}
2015-06-22 20:45:56 +03:00
#if 0 /* FreeBSD OSS implementation at this moment (2015.03) does not set PCM_CAP_OUTPUT flag. */
tmp = 0;
2015-06-22 20:45:56 +03:00
if (ioctl(oss->pcm_handle, SNDCTL_DSP_GETCAPS, &mask) == -1)
{
OSS_LOG_ERR("SNDCTL_DSP_GETCAPS failed, try ignory", errno);
2015-06-22 20:45:56 +03:00
}
else if ((mask & PCM_CAP_OUTPUT) == 0)
{
OSS_LOG_ERR("Device does not supports playback", EOPNOTSUPP);
close(oss->pcm_handle);
oss->pcm_handle = -1;
return FALSE;
}
2015-06-22 20:45:56 +03:00
#endif
tmp = 0;
2015-06-22 20:45:56 +03:00
if (ioctl(oss->pcm_handle, SNDCTL_DSP_GETFMTS, &tmp) == -1)
{
OSS_LOG_ERR("SNDCTL_DSP_GETFMTS failed", errno);
close(oss->pcm_handle);
oss->pcm_handle = -1;
return FALSE;
}
2015-06-22 20:45:56 +03:00
if ((AFMT_S16_LE & tmp) == 0)
{
OSS_LOG_ERR("SNDCTL_DSP_GETFMTS - AFMT_S16_LE", EOPNOTSUPP);
close(oss->pcm_handle);
oss->pcm_handle = -1;
return FALSE;
}
WLog_INFO(TAG, "open: %s", oss->dev_name);
return TRUE;
}
2015-06-22 20:45:56 +03:00
static BOOL tsmf_oss_set_format(ITSMFAudioDevice* audio, UINT32 sample_rate, UINT32 channels, UINT32 bits_per_sample)
{
int tmp;
2015-06-22 20:45:56 +03:00
TSMFOssAudioDevice* oss = (TSMFOssAudioDevice*)audio;
if (oss == NULL || oss->pcm_handle == -1)
return FALSE;
oss->sample_rate = sample_rate;
oss->channels = channels;
oss->bits_per_sample = bits_per_sample;
tmp = AFMT_S16_LE;
2015-06-22 20:45:56 +03:00
if (ioctl(oss->pcm_handle, SNDCTL_DSP_SETFMT, &tmp) == -1)
OSS_LOG_ERR("SNDCTL_DSP_SETFMT failed", errno);
2015-06-22 20:45:56 +03:00
tmp = channels;
2015-06-22 20:45:56 +03:00
if (ioctl(oss->pcm_handle, SNDCTL_DSP_CHANNELS, &tmp) == -1)
OSS_LOG_ERR("SNDCTL_DSP_CHANNELS failed", errno);
2015-06-22 20:45:56 +03:00
tmp = sample_rate;
2015-06-22 20:45:56 +03:00
if (ioctl(oss->pcm_handle, SNDCTL_DSP_SPEED, &tmp) == -1)
OSS_LOG_ERR("SNDCTL_DSP_SPEED failed", errno);
2015-06-22 20:45:56 +03:00
tmp = ((bits_per_sample / 8) * channels * sample_rate);
2015-06-22 20:45:56 +03:00
if (ioctl(oss->pcm_handle, SNDCTL_DSP_SETFRAGMENT, &tmp) == -1)
OSS_LOG_ERR("SNDCTL_DSP_SETFRAGMENT failed", errno);
DEBUG_TSMF("sample_rate %d channels %d bits_per_sample %d",
2015-06-22 20:45:56 +03:00
sample_rate, channels, bits_per_sample);
return TRUE;
}
2015-06-22 20:45:56 +03:00
static BOOL tsmf_oss_play(ITSMFAudioDevice* audio, BYTE* data, UINT32 data_size)
{
int status;
UINT32 offset;
2015-06-22 20:45:56 +03:00
TSMFOssAudioDevice* oss = (TSMFOssAudioDevice*)audio;
DEBUG_TSMF("tsmf_oss_play: data_size %d", data_size);
if (oss == NULL || oss->pcm_handle == -1)
return FALSE;
2015-06-22 20:45:56 +03:00
if (data == NULL || data_size == 0)
{
free(data);
return TRUE;
}
offset = 0;
oss->data_size_last = data_size;
2015-06-22 20:45:56 +03:00
while (offset < data_size)
{
status = write(oss->pcm_handle, &data[offset], (data_size - offset));
2015-06-22 20:45:56 +03:00
if (status < 0)
{
OSS_LOG_ERR("write fail", errno);
free(data);
return FALSE;
}
2015-06-22 20:45:56 +03:00
offset += status;
}
free(data);
return TRUE;
}
2015-06-22 20:45:56 +03:00
static UINT64 tsmf_oss_get_latency(ITSMFAudioDevice* audio)
{
UINT64 latency = 0;
2015-06-22 20:45:56 +03:00
TSMFOssAudioDevice* oss = (TSMFOssAudioDevice*)audio;
if (oss == NULL)
return 0;
//latency = ((oss->data_size_last / (oss->bits_per_sample / 8)) * oss->sample_rate);
//WLog_INFO(TAG, "latency: %zu", latency);
return latency;
}
2015-06-22 20:45:56 +03:00
static void tsmf_oss_flush(ITSMFAudioDevice* audio)
{
}
2015-06-22 20:45:56 +03:00
static void tsmf_oss_free(ITSMFAudioDevice* audio)
{
TSMFOssAudioDevice* oss = (TSMFOssAudioDevice*)audio;
if (oss == NULL)
return;
2015-06-22 20:45:56 +03:00
if (oss->pcm_handle != -1)
{
WLog_INFO(TAG, "close: %s", oss->dev_name);
close(oss->pcm_handle);
}
2015-06-22 20:45:56 +03:00
free(oss);
}
#ifdef STATIC_CHANNELS
#define freerdp_tsmf_client_audio_subsystem_entry oss_freerdp_tsmf_client_audio_subsystem_entry
#endif
2015-06-22 20:45:56 +03:00
ITSMFAudioDevice* freerdp_tsmf_client_audio_subsystem_entry(void)
{
2015-06-22 20:45:56 +03:00
TSMFOssAudioDevice* oss;
oss = (TSMFOssAudioDevice*)malloc(sizeof(TSMFOssAudioDevice));
ZeroMemory(oss, sizeof(TSMFOssAudioDevice));
oss->iface.Open = tsmf_oss_open;
oss->iface.SetFormat = tsmf_oss_set_format;
oss->iface.Play = tsmf_oss_play;
oss->iface.GetLatency = tsmf_oss_get_latency;
oss->iface.Flush = tsmf_oss_flush;
oss->iface.Free = tsmf_oss_free;
oss->pcm_handle = -1;
return (ITSMFAudioDevice*)oss;
}