20 #include <freerdp/config.h>
27 #include <winpr/crt.h>
35 #if defined(__OpenBSD__)
36 #include <soundcard.h>
38 #include <sys/soundcard.h>
40 #include <sys/ioctl.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))); \
72 static 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);
97 if (ioctl(oss->pcm_handle, SNDCTL_DSP_GETCAPS, &mask) == -1)
99 OSS_LOG_ERR(
"SNDCTL_DSP_GETCAPS failed, try ignory", errno);
101 else if ((mask & PCM_CAP_OUTPUT) == 0)
103 OSS_LOG_ERR(
"Device does not supports playback", EOPNOTSUPP);
104 close(oss->pcm_handle);
105 oss->pcm_handle = -1;
110 const int rc = ioctl(oss->pcm_handle, SNDCTL_DSP_GETFMTS, &tmp);
113 OSS_LOG_ERR(
"SNDCTL_DSP_GETFMTS failed", errno);
114 close(oss->pcm_handle);
115 oss->pcm_handle = -1;
119 if ((AFMT_S16_LE & tmp) == 0)
121 OSS_LOG_ERR(
"SNDCTL_DSP_GETFMTS - AFMT_S16_LE", EOPNOTSUPP);
122 close(oss->pcm_handle);
123 oss->pcm_handle = -1;
127 WLog_INFO(TAG,
"open: %s", oss->dev_name);
131 static BOOL tsmf_oss_set_format(ITSMFAudioDevice* audio, UINT32 sample_rate, UINT32 channels,
132 UINT32 bits_per_sample)
135 TSMFOssAudioDevice* oss = (TSMFOssAudioDevice*)audio;
137 if (oss == NULL || oss->pcm_handle == -1)
140 oss->sample_rate = sample_rate;
141 oss->channels = channels;
142 oss->bits_per_sample = bits_per_sample;
145 if (ioctl(oss->pcm_handle, SNDCTL_DSP_SETFMT, &tmp) == -1)
146 OSS_LOG_ERR(
"SNDCTL_DSP_SETFMT failed", errno);
150 if (ioctl(oss->pcm_handle, SNDCTL_DSP_CHANNELS, &tmp) == -1)
151 OSS_LOG_ERR(
"SNDCTL_DSP_CHANNELS failed", errno);
155 if (ioctl(oss->pcm_handle, SNDCTL_DSP_SPEED, &tmp) == -1)
156 OSS_LOG_ERR(
"SNDCTL_DSP_SPEED failed", errno);
158 tmp = ((bits_per_sample / 8) * channels * sample_rate);
160 if (ioctl(oss->pcm_handle, SNDCTL_DSP_SETFRAGMENT, &tmp) == -1)
161 OSS_LOG_ERR(
"SNDCTL_DSP_SETFRAGMENT failed", errno);
163 DEBUG_TSMF(
"sample_rate %" PRIu32
" channels %" PRIu32
" bits_per_sample %" PRIu32
"",
164 sample_rate, channels, bits_per_sample);
168 static BOOL tsmf_oss_play(ITSMFAudioDevice* audio,
const BYTE* data, UINT32 data_size)
171 TSMFOssAudioDevice* oss = (TSMFOssAudioDevice*)audio;
172 DEBUG_TSMF(
"tsmf_oss_play: data_size %" PRIu32
"", data_size);
174 if (oss == NULL || oss->pcm_handle == -1)
177 if (data == NULL || data_size == 0)
181 oss->data_size_last = data_size;
183 while (offset < data_size)
185 const ssize_t status = write(oss->pcm_handle, &data[offset], (data_size - offset));
189 OSS_LOG_ERR(
"write fail", errno);
199 static UINT64 tsmf_oss_get_latency(ITSMFAudioDevice* audio)
202 TSMFOssAudioDevice* oss = (TSMFOssAudioDevice*)audio;
212 static BOOL tsmf_oss_flush(ITSMFAudioDevice* audio)
217 static void tsmf_oss_free(ITSMFAudioDevice* audio)
219 TSMFOssAudioDevice* oss = (TSMFOssAudioDevice*)audio;
224 if (oss->pcm_handle != -1)
226 WLog_INFO(TAG,
"close: %s", oss->dev_name);
227 close(oss->pcm_handle);
233 FREERDP_ENTRY_POINT(UINT VCAPITYPE oss_freerdp_tsmf_client_audio_subsystem_entry(
void* ptr))
235 ITSMFAudioDevice** sptr = (ITSMFAudioDevice**)ptr;
239 TSMFOssAudioDevice* oss = calloc(1,
sizeof(TSMFOssAudioDevice));
241 return ERROR_OUTOFMEMORY;
243 oss->iface.Open = tsmf_oss_open;
244 oss->iface.SetFormat = tsmf_oss_set_format;
245 oss->iface.Play = tsmf_oss_play;
246 oss->iface.GetLatency = tsmf_oss_get_latency;
247 oss->iface.Flush = tsmf_oss_flush;
248 oss->iface.Free = tsmf_oss_free;
249 oss->pcm_handle = -1;
251 return CHANNEL_RC_OK;