22 #include <freerdp/config.h>
28 #include <winpr/crt.h>
29 #include <winpr/string.h>
30 #include <winpr/cmdline.h>
31 #include <winpr/sysinfo.h>
32 #include <winpr/collections.h>
40 #include <oss-includes.h>
41 #include <sys/ioctl.h>
43 #include <freerdp/types.h>
44 #include <freerdp/settings.h>
45 #include <freerdp/channels/log.h>
47 #include "rdpsnd_main.h"
51 rdpsndDevicePlugin device;
57 int supported_formats;
63 #define OSS_LOG_ERR(_text, _error) \
68 char ebuffer[256] = { 0 }; \
69 WLog_ERR(TAG, "%s: %i - %s", (_text), (_error), \
70 winpr_strerror((_error), ebuffer, sizeof(ebuffer))); \
74 static int rdpsnd_oss_get_format(
const AUDIO_FORMAT* format)
76 switch (format->wFormatTag)
79 switch (format->wBitsPerSample)
98 static BOOL rdpsnd_oss_format_supported(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* format)
101 rdpsndOssPlugin* oss = (rdpsndOssPlugin*)device;
103 if (device == NULL || format == NULL)
106 switch (format->wFormatTag)
108 case WAVE_FORMAT_PCM:
109 if (format->cbSize != 0 || format->nSamplesPerSec > 48000 ||
110 (format->wBitsPerSample != 8 && format->wBitsPerSample != 16) ||
111 (format->nChannels != 1 && format->nChannels != 2))
120 req_fmt = rdpsnd_oss_get_format(format);
123 if (oss->pcm_handle != -1)
125 if ((req_fmt & oss->supported_formats) == 0)
137 static BOOL rdpsnd_oss_set_format(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* format,
141 rdpsndOssPlugin* oss = (rdpsndOssPlugin*)device;
143 if (device == NULL || oss->pcm_handle == -1 || format == NULL)
146 oss->latency = latency;
147 CopyMemory(&(oss->format), format,
sizeof(
AUDIO_FORMAT));
148 tmp = rdpsnd_oss_get_format(format);
150 if (ioctl(oss->pcm_handle, SNDCTL_DSP_SETFMT, &tmp) == -1)
152 OSS_LOG_ERR(
"SNDCTL_DSP_SETFMT failed", errno);
156 tmp = format->nChannels;
158 if (ioctl(oss->pcm_handle, SNDCTL_DSP_CHANNELS, &tmp) == -1)
160 OSS_LOG_ERR(
"SNDCTL_DSP_CHANNELS failed", errno);
164 tmp = (int)format->nSamplesPerSec;
166 if (ioctl(oss->pcm_handle, SNDCTL_DSP_SPEED, &tmp) == -1)
168 OSS_LOG_ERR(
"SNDCTL_DSP_SPEED failed", errno);
172 tmp = format->nBlockAlign;
174 if (ioctl(oss->pcm_handle, SNDCTL_DSP_SETFRAGMENT, &tmp) == -1)
176 OSS_LOG_ERR(
"SNDCTL_DSP_SETFRAGMENT failed", errno);
183 static void rdpsnd_oss_open_mixer(rdpsndOssPlugin* oss)
186 char mixer_name[PATH_MAX] =
"/dev/mixer";
188 if (oss->mixer_handle != -1)
191 if (oss->dev_unit != -1)
192 (void)sprintf_s(mixer_name, PATH_MAX - 1,
"/dev/mixer%i", oss->dev_unit);
194 if ((oss->mixer_handle = open(mixer_name, O_RDWR)) < 0)
196 OSS_LOG_ERR(
"mixer open failed", errno);
197 oss->mixer_handle = -1;
201 if (ioctl(oss->mixer_handle, SOUND_MIXER_READ_DEVMASK, &devmask) == -1)
203 OSS_LOG_ERR(
"SOUND_MIXER_READ_DEVMASK failed", errno);
204 close(oss->mixer_handle);
205 oss->mixer_handle = -1;
210 static BOOL rdpsnd_oss_open(rdpsndDevicePlugin* device,
const AUDIO_FORMAT* format, UINT32 latency)
212 char dev_name[PATH_MAX] =
"/dev/dsp";
213 rdpsndOssPlugin* oss = (rdpsndOssPlugin*)device;
215 if (device == NULL || oss->pcm_handle != -1)
218 if (oss->dev_unit != -1)
219 (void)sprintf_s(dev_name, PATH_MAX - 1,
"/dev/dsp%i", oss->dev_unit);
221 WLog_INFO(TAG,
"open: %s", dev_name);
223 if ((oss->pcm_handle = open(dev_name, O_WRONLY)) < 0)
225 OSS_LOG_ERR(
"sound dev open failed", errno);
226 oss->pcm_handle = -1;
233 if (ioctl(oss->pcm_handle, SNDCTL_DSP_GETCAPS, &mask) == -1)
235 OSS_LOG_ERR(
"SNDCTL_DSP_GETCAPS failed, try ignory", errno);
237 else if ((mask & PCM_CAP_OUTPUT) == 0)
239 OSS_LOG_ERR(
"Device does not supports playback", EOPNOTSUPP);
240 close(oss->pcm_handle);
241 oss->pcm_handle = -1;
247 if (ioctl(oss->pcm_handle, SNDCTL_DSP_GETFMTS, &oss->supported_formats) == -1)
249 OSS_LOG_ERR(
"SNDCTL_DSP_GETFMTS failed", errno);
250 close(oss->pcm_handle);
251 oss->pcm_handle = -1;
255 rdpsnd_oss_set_format(device, format, latency);
256 rdpsnd_oss_open_mixer(oss);
260 static void rdpsnd_oss_close(rdpsndDevicePlugin* device)
262 rdpsndOssPlugin* oss = (rdpsndOssPlugin*)device;
267 if (oss->pcm_handle != -1)
269 WLog_INFO(TAG,
"close: dsp");
270 close(oss->pcm_handle);
271 oss->pcm_handle = -1;
274 if (oss->mixer_handle != -1)
276 WLog_INFO(TAG,
"close: mixer");
277 close(oss->mixer_handle);
278 oss->mixer_handle = -1;
282 static void rdpsnd_oss_free(rdpsndDevicePlugin* device)
284 rdpsndOssPlugin* oss = (rdpsndOssPlugin*)device;
289 rdpsnd_oss_close(device);
293 static UINT32 rdpsnd_oss_get_volume(rdpsndDevicePlugin* device)
297 UINT16 dwVolumeLeft = 0;
298 UINT16 dwVolumeRight = 0;
299 rdpsndOssPlugin* oss = (rdpsndOssPlugin*)device;
301 dwVolumeLeft = ((50 * 0xFFFF) / 100);
302 dwVolumeRight = ((50 * 0xFFFF) / 100);
303 dwVolume = ((dwVolumeLeft << 16) | dwVolumeRight);
305 if (device == NULL || oss->mixer_handle == -1)
308 if (ioctl(oss->mixer_handle, MIXER_READ(SOUND_MIXER_VOLUME), &vol) == -1)
310 OSS_LOG_ERR(
"MIXER_READ", errno);
314 dwVolumeLeft = (((vol & 0x7f) * 0xFFFF) / 100);
315 dwVolumeRight = ((((vol >> 8) & 0x7f) * 0xFFFF) / 100);
316 dwVolume = ((dwVolumeLeft << 16) | dwVolumeRight);
320 static BOOL rdpsnd_oss_set_volume(rdpsndDevicePlugin* device, UINT32 value)
322 rdpsndOssPlugin* oss = (rdpsndOssPlugin*)device;
325 if (device == NULL || oss->mixer_handle == -1)
328 unsigned left = (((value & 0xFFFF) * 100) / 0xFFFF);
329 unsigned right = ((((value >> 16) & 0xFFFF) * 100) / 0xFFFF);
331 left |= (right << 8);
333 if (ioctl(oss->mixer_handle, MIXER_WRITE(SOUND_MIXER_VOLUME), &left) == -1)
335 OSS_LOG_ERR(
"WRITE_MIXER", errno);
342 static UINT rdpsnd_oss_play(rdpsndDevicePlugin* device,
const BYTE* data,
size_t size)
344 rdpsndOssPlugin* oss = (rdpsndOssPlugin*)device;
346 if (device == NULL || oss->mixer_handle == -1)
351 ssize_t status = write(oss->pcm_handle, data, size);
355 OSS_LOG_ERR(
"write fail", errno);
356 rdpsnd_oss_close(device);
357 rdpsnd_oss_open(device, NULL, oss->latency);
363 if ((
size_t)status <= size)
364 size -= (size_t)status;
372 static int rdpsnd_oss_parse_addin_args(rdpsndDevicePlugin* device,
const ADDIN_ARGV* args)
375 char* str_num = NULL;
379 rdpsndOssPlugin* oss = (rdpsndOssPlugin*)device;
381 NULL, NULL, -1, NULL,
"device" },
382 { NULL, 0, NULL, NULL, NULL, -1, NULL, NULL } };
384 COMMAND_LINE_SIGIL_NONE | COMMAND_LINE_SEPARATOR_COLON | COMMAND_LINE_IGN_UNKNOWN_KEYWORD;
386 CommandLineParseArgumentsA(args->argc, args->argv, rdpsnd_oss_args, flags, oss, NULL, NULL);
391 arg = rdpsnd_oss_args;
396 if (!(arg->Flags & COMMAND_LINE_VALUE_PRESENT))
399 CommandLineSwitchStart(arg) CommandLineSwitchCase(arg,
"dev")
401 str_num = _strdup(arg->Value);
404 return ERROR_OUTOFMEMORY;
407 long val = strtol(str_num, &eptr, 10);
409 if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
412 return CHANNEL_RC_NULL_DATA;
415 oss->dev_unit = (int)val;
418 if (oss->dev_unit < 0 || *eptr !=
'\0')
423 CommandLineSwitchEnd(arg)
424 }
while ((arg = CommandLineFindNextArgumentA(arg)) != NULL);
434 FREERDP_ENTRY_POINT(UINT VCAPITYPE oss_freerdp_rdpsnd_client_subsystem_entry(
438 rdpsndOssPlugin* oss = (rdpsndOssPlugin*)calloc(1,
sizeof(rdpsndOssPlugin));
441 return CHANNEL_RC_NO_MEMORY;
443 oss->device.Open = rdpsnd_oss_open;
444 oss->device.FormatSupported = rdpsnd_oss_format_supported;
445 oss->device.GetVolume = rdpsnd_oss_get_volume;
446 oss->device.SetVolume = rdpsnd_oss_set_volume;
447 oss->device.Play = rdpsnd_oss_play;
448 oss->device.Close = rdpsnd_oss_close;
449 oss->device.Free = rdpsnd_oss_free;
450 oss->pcm_handle = -1;
451 oss->mixer_handle = -1;
453 args = pEntryPoints->args;
454 if (rdpsnd_oss_parse_addin_args(&oss->device, args) < 0)
457 return ERROR_INVALID_PARAMETER;
459 pEntryPoints->pRegisterRdpsndDevice(pEntryPoints->rdpsnd, (rdpsndDevicePlugin*)oss);
460 return CHANNEL_RC_OK;