20#include <freerdp/config.h>
35#if defined(__OpenBSD__)
38#include <sys/soundcard.h>
42#include <freerdp/types.h>
43#include <freerdp/codec/dsp.h>
45#include "tsmf_audio.h"
49 ITSMFAudioDevice iface;
51 char dev_name[PATH_MAX];
56 UINT32 bits_per_sample;
58 UINT32 data_size_last;
61#define OSS_LOG_ERR(_text, _error) \
66 char ebuffer[256] = { 0 }; \
67 WLog_ERR(TAG, "%s: %i - %s", (_text), (_error), \
68 winpr_strerror((_error), ebuffer, sizeof(ebuffer))); \
72static BOOL tsmf_oss_open(ITSMFAudioDevice* audio,
const char* device)
75 TSMFOssAudioDevice* oss = (TSMFOssAudioDevice*)audio;
77 if (oss == NULL || oss->pcm_handle != -1)
82 strncpy(oss->dev_name,
"/dev/dsp",
sizeof(oss->dev_name));
86 strncpy(oss->dev_name, device,
sizeof(oss->dev_name) - 1);
89 if ((oss->pcm_handle = open(oss->dev_name, O_WRONLY)) < 0)
91 OSS_LOG_ERR(
"sound dev open failed", errno);
96 const int rc = ioctl(oss->pcm_handle, SNDCTL_DSP_GETFMTS, &tmp);
99 OSS_LOG_ERR(
"SNDCTL_DSP_GETFMTS failed", errno);
100 close(oss->pcm_handle);
101 oss->pcm_handle = -1;
105 if ((AFMT_S16_LE & tmp) == 0)
107 OSS_LOG_ERR(
"SNDCTL_DSP_GETFMTS - AFMT_S16_LE", EOPNOTSUPP);
108 close(oss->pcm_handle);
109 oss->pcm_handle = -1;
113 WLog_INFO(TAG,
"open: %s", oss->dev_name);
117static BOOL tsmf_oss_set_format(ITSMFAudioDevice* audio, UINT32 sample_rate, UINT32 channels,
118 UINT32 bits_per_sample)
121 TSMFOssAudioDevice* oss = (TSMFOssAudioDevice*)audio;
123 if (oss == NULL || oss->pcm_handle == -1)
126 oss->sample_rate = sample_rate;
127 oss->channels = channels;
128 oss->bits_per_sample = bits_per_sample;
131 if (ioctl(oss->pcm_handle, SNDCTL_DSP_SETFMT, &tmp) == -1)
132 OSS_LOG_ERR(
"SNDCTL_DSP_SETFMT failed", errno);
136 if (ioctl(oss->pcm_handle, SNDCTL_DSP_CHANNELS, &tmp) == -1)
137 OSS_LOG_ERR(
"SNDCTL_DSP_CHANNELS failed", errno);
141 if (ioctl(oss->pcm_handle, SNDCTL_DSP_SPEED, &tmp) == -1)
142 OSS_LOG_ERR(
"SNDCTL_DSP_SPEED failed", errno);
144 tmp = ((bits_per_sample / 8) * channels * sample_rate);
146 if (ioctl(oss->pcm_handle, SNDCTL_DSP_SETFRAGMENT, &tmp) == -1)
147 OSS_LOG_ERR(
"SNDCTL_DSP_SETFRAGMENT failed", errno);
149 DEBUG_TSMF(
"sample_rate %" PRIu32
" channels %" PRIu32
" bits_per_sample %" PRIu32
"",
150 sample_rate, channels, bits_per_sample);
154static BOOL tsmf_oss_play(ITSMFAudioDevice* audio,
const BYTE* data, UINT32 data_size)
157 TSMFOssAudioDevice* oss = (TSMFOssAudioDevice*)audio;
158 DEBUG_TSMF(
"tsmf_oss_play: data_size %" PRIu32
"", data_size);
160 if (oss == NULL || oss->pcm_handle == -1)
163 if (data == NULL || data_size == 0)
167 oss->data_size_last = data_size;
169 while (offset < data_size)
171 const ssize_t status = write(oss->pcm_handle, &data[offset], (data_size - offset));
175 OSS_LOG_ERR(
"write fail", errno);
185static UINT64 tsmf_oss_get_latency(ITSMFAudioDevice* audio)
188 TSMFOssAudioDevice* oss = (TSMFOssAudioDevice*)audio;
198static BOOL tsmf_oss_flush(ITSMFAudioDevice* audio)
203static void tsmf_oss_free(ITSMFAudioDevice* audio)
205 TSMFOssAudioDevice* oss = (TSMFOssAudioDevice*)audio;
210 if (oss->pcm_handle != -1)
212 WLog_INFO(TAG,
"close: %s", oss->dev_name);
213 close(oss->pcm_handle);
219FREERDP_ENTRY_POINT(UINT VCAPITYPE oss_freerdp_tsmf_client_audio_subsystem_entry(
void* ptr))
221 ITSMFAudioDevice** sptr = (ITSMFAudioDevice**)ptr;
225 TSMFOssAudioDevice* oss = calloc(1,
sizeof(TSMFOssAudioDevice));
227 return ERROR_OUTOFMEMORY;
229 oss->iface.Open = tsmf_oss_open;
230 oss->iface.SetFormat = tsmf_oss_set_format;
231 oss->iface.Play = tsmf_oss_play;
232 oss->iface.GetLatency = tsmf_oss_get_latency;
233 oss->iface.Flush = tsmf_oss_flush;
234 oss->iface.Free = tsmf_oss_free;
235 oss->pcm_handle = -1;
237 return CHANNEL_RC_OK;