forked from videolan/vlc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oss.c
475 lines (410 loc) · 11.9 KB
/
oss.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*****************************************************************************
* oss.c: Open Sound System audio output plugin for VLC
*****************************************************************************
* Copyright (C) 2000-2002 VLC authors and VideoLAN
* Copyright (C) 2007-2012 Rémi Denis-Courmont
*
* Authors: Michel Kaempf <[email protected]>
* Sam Hocevar <[email protected]>
* Christophe Massiot <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <stdlib.h>
#include <math.h>
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#ifdef HAVE_SOUNDCARD_H
# include <soundcard.h>
#else
# include <sys/soundcard.h>
#endif
#ifndef SNDCTL_DSP_HALT
# define SNDCTL_DSP_HALT SNDCTL_DSP_RESET
#endif
#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_fs.h>
#include <vlc_cpu.h>
#include <vlc_aout.h>
#define A52_FRAME_NB 1536
struct aout_sys_t
{
int fd;
audio_sample_format_t format;
bool starting;
bool mute;
uint8_t level;
char *device;
};
static int Open (vlc_object_t *);
static void Close (vlc_object_t *);
#define AUDIO_DEV_TEXT N_("Audio output device")
#define AUDIO_DEV_LONGTEXT N_("OSS device node path.")
vlc_module_begin ()
set_shortname( "OSS" )
set_description (N_("Open Sound System audio output"))
set_category( CAT_AUDIO )
set_subcategory( SUBCAT_AUDIO_AOUT )
add_string ("oss-audio-device", "",
AUDIO_DEV_TEXT, AUDIO_DEV_LONGTEXT, false)
set_capability( "audio output", 100 )
set_callbacks (Open, Close)
vlc_module_end ()
static int TimeGet (audio_output_t *, mtime_t *);
static void Play (audio_output_t *, block_t *);
static void Pause (audio_output_t *, bool, mtime_t);
static void Flush (audio_output_t *, bool);
static int VolumeSync (audio_output_t *);
static int Start (audio_output_t *aout, audio_sample_format_t *restrict fmt)
{
aout_sys_t* sys = aout->sys;
/* Open the device */
const char *device = sys->device;
if (device == NULL)
device = getenv ("OSS_AUDIODEV");
if (device == NULL)
device = "/dev/dsp";
int fd = vlc_open (device, O_WRONLY);
if (fd == -1)
{
msg_Err (aout, "cannot open OSS device %s: %s", device,
vlc_strerror_c(errno));
return VLC_EGENERIC;
}
msg_Dbg (aout, "using OSS device: %s", device);
/* Select audio format */
int format;
bool spdif = false;
switch (fmt->i_format)
{
#ifdef AFMT_FLOAT
case VLC_CODEC_FL64:
case VLC_CODEC_FL32:
format = AFMT_FLOAT;
break;
#endif
case VLC_CODEC_S32N:
format = AFMT_S32_NE;
break;
case VLC_CODEC_S16N:
format = AFMT_S16_NE;
break;
case VLC_CODEC_U8:
format = AFMT_U8;
break;
default:
if (AOUT_FMT_SPDIF(fmt))
spdif = var_InheritBool (aout, "spdif");
if (spdif)
format = AFMT_AC3;
#ifdef AFMT_FLOAT
else if (HAVE_FPU)
format = AFMT_FLOAT;
#endif
else
format = AFMT_S16_NE;
}
if (ioctl (fd, SNDCTL_DSP_SETFMT, &format) < 0)
{
msg_Err (aout, "cannot set audio format 0x%X: %s", format,
vlc_strerror_c(errno));
goto error;
}
switch (format)
{
case AFMT_U8: fmt->i_format = VLC_CODEC_U8; break;
case AFMT_S16_NE: fmt->i_format = VLC_CODEC_S16N; break;
case AFMT_S32_NE: fmt->i_format = VLC_CODEC_S32N; break;
#ifdef AFMT_FLOAT
case AFMT_FLOAT: fmt->i_format = VLC_CODEC_FL32; break;
#endif
case AFMT_AC3:
if (spdif)
{
fmt->i_format = VLC_CODEC_SPDIFL;
break;
}
default:
msg_Err (aout, "unsupported audio format 0x%X", format);
goto error;
}
/* Select channels count */
int channels = spdif ? 2 : aout_FormatNbChannels (fmt);
if (ioctl (fd, SNDCTL_DSP_CHANNELS, &channels) < 0)
{
msg_Err (aout, "cannot set %d channels: %s", channels,
vlc_strerror_c(errno));
goto error;
}
switch (channels)
{
case 1: channels = AOUT_CHAN_CENTER; break;
case 2: channels = AOUT_CHANS_STEREO; break;
case 4: channels = AOUT_CHANS_4_0; break;
case 6: channels = AOUT_CHANS_5_1; break;
case 8: channels = AOUT_CHANS_7_1; break;
default:
msg_Err (aout, "unsupported channels count %d", channels);
goto error;
}
/* Select sample rate */
int rate = spdif ? 48000 : fmt->i_rate;
if (ioctl (fd, SNDCTL_DSP_SPEED, &rate) < 0)
{
msg_Err (aout, "cannot set %d Hz sample rate: %s", rate,
vlc_strerror_c(errno));
goto error;
}
/* Setup audio_output_t */
aout->time_get = TimeGet;
aout->play = Play;
aout->pause = Pause;
aout->flush = Flush;
if (spdif)
{
fmt->i_bytes_per_frame = AOUT_SPDIF_SIZE;
fmt->i_frame_length = A52_FRAME_NB;
}
else
{
fmt->i_rate = rate;
fmt->i_original_channels =
fmt->i_physical_channels = channels;
}
aout_FormatPrepare (fmt);
/* Select timing */
unsigned bytes;
if (spdif)
bytes = AOUT_SPDIF_SIZE;
else
bytes = fmt->i_rate / (CLOCK_FREQ / AOUT_MIN_PREPARE_TIME)
* fmt->i_bytes_per_frame;
if (unlikely(bytes < 16))
bytes = 16;
int frag = (AOUT_MAX_ADVANCE_TIME / AOUT_MIN_PREPARE_TIME) << 16
| (32 - clz32(bytes - 1));
if (ioctl (fd, SNDCTL_DSP_SETFRAGMENT, &frag) < 0)
msg_Err (aout, "cannot set 0x%08x fragment: %s", frag,
vlc_strerror_c(errno));
sys->fd = fd;
VolumeSync (aout);
sys->starting = true;
sys->format = *fmt;
return VLC_SUCCESS;
error:
close (fd);
return VLC_EGENERIC;
}
static int TimeGet (audio_output_t *aout, mtime_t *restrict pts)
{
aout_sys_t *sys = aout->sys;
int delay;
if (ioctl (sys->fd, SNDCTL_DSP_GETODELAY, &delay) < 0)
{
msg_Warn (aout, "cannot get delay: %s", vlc_strerror_c(errno));
return -1;
}
*pts = (delay * CLOCK_FREQ * sys->format.i_frame_length)
/ (sys->format.i_rate * sys->format.i_bytes_per_frame);
return 0;
}
/**
* Queues one audio buffer to the hardware.
*/
static void Play (audio_output_t *aout, block_t *block)
{
aout_sys_t *sys = aout->sys;
int fd = sys->fd;
while (block->i_buffer > 0)
{
ssize_t bytes = write (fd, block->p_buffer, block->i_buffer);
if (bytes >= 0)
{
block->p_buffer += bytes;
block->i_buffer -= bytes;
}
else
msg_Err (aout, "cannot write samples: %s", vlc_strerror_c(errno));
}
block_Release (block);
/* Dumb OSS cannot send any kind of events for this... */
VolumeSync (aout);
}
/**
* Pauses/resumes the audio playback.
*/
static void Pause (audio_output_t *aout, bool pause, mtime_t date)
{
aout_sys_t *sys = aout->sys;
int fd = sys->fd;
(void) date;
ioctl (fd, pause ? SNDCTL_DSP_SILENCE : SNDCTL_DSP_SKIP, NULL);
}
/**
* Flushes/drains the audio playback buffer.
*/
static void Flush (audio_output_t *aout, bool wait)
{
aout_sys_t *sys = aout->sys;
int fd = sys->fd;
if (wait)
return; /* drain is implicit with OSS */
ioctl (fd, SNDCTL_DSP_HALT, NULL);
}
static int VolumeSync (audio_output_t *aout)
{
aout_sys_t *sys = aout->sys;
int fd = sys->fd;
int level;
if (ioctl (fd, SNDCTL_DSP_GETPLAYVOL, &level) < 0)
return -1;
sys->mute = !level;
if (level) /* try to keep last volume before mute */
sys->level = level;
aout_MuteReport (aout, !level);
aout_VolumeReport (aout, (float)(level & 0xFF) / 100.f);
return 0;
}
/**
* Releases the audio output device.
*/
static void Stop (audio_output_t *aout)
{
aout_sys_t *sys = aout->sys;
int fd = sys->fd;
ioctl (fd, SNDCTL_DSP_HALT, NULL);
close (fd);
sys->fd = -1;
}
static int VolumeSet (audio_output_t *aout, float vol)
{
aout_sys_t *sys = aout->sys;
int fd = sys->fd;
if (fd == -1)
return -1;
int level = lroundf (vol * 100.f);
if (level > 0xFF)
level = 0xFFFF;
else
level |= level << 8;
if (!sys->mute && ioctl (fd, SNDCTL_DSP_SETPLAYVOL, &level) < 0)
{
msg_Err (aout, "cannot set volume: %s", vlc_strerror_c(errno));
return -1;
}
sys->level = level;
aout_VolumeReport (aout, (float)(level & 0xFF) / 100.f);
return 0;
}
static int MuteSet (audio_output_t *aout, bool mute)
{
aout_sys_t *sys = aout->sys;
int fd = sys->fd;
if (fd == -1)
return -1;
int level = mute ? 0 : (sys->level | (sys->level << 8));
if (ioctl (fd, SNDCTL_DSP_SETPLAYVOL, &level) < 0)
{
msg_Err (aout, "cannot mute: %s", vlc_strerror_c(errno));
return -1;
}
sys->mute = mute;
aout_MuteReport (aout, mute);
return 0;
}
static int DevicesEnum (audio_output_t *aout)
{
int fd = vlc_open ("/dev/dsp", O_WRONLY);
if (fd == -1)
return -1;
oss_sysinfo si;
int n = -1;
if (ioctl (fd, SNDCTL_SYSINFO, &si) < 0)
{
msg_Err (aout, "cannot get system infos: %s", vlc_strerror(errno));
goto out;
}
msg_Dbg (aout, "using %s version %s (0x%06X) under %s", si.product,
si.version, si.versionnum, si.license);
for (int i = 0; i < si.numaudios; i++)
{
oss_audioinfo ai = { .dev = i };
if (ioctl (fd, SNDCTL_AUDIOINFO, &ai) < 0)
{
msg_Warn (aout, "cannot get device %d infos: %s", i,
vlc_strerror_c(errno));
continue;
}
if (ai.caps & (PCM_CAP_HIDDEN|PCM_CAP_MODEM))
continue;
if (!(ai.caps & PCM_CAP_OUTPUT))
continue;
if (!ai.enabled)
continue;
aout_HotplugReport (aout, ai.devnode, ai.name);
n++;
}
out:
close (fd);
return n;
}
static int DeviceSelect (audio_output_t *aout, const char *id)
{
aout_sys_t *sys = aout->sys;
char *path = NULL;
if (id != NULL)
{
path = strdup (id);
if (unlikely(path == NULL))
return -1;
}
free (sys->device);
sys->device = path;
aout_DeviceReport (aout, path);
aout_RestartRequest (aout, AOUT_RESTART_OUTPUT);
return 0;
}
static int Open (vlc_object_t *obj)
{
audio_output_t *aout = (audio_output_t *)obj;
aout_sys_t *sys = malloc (sizeof (*sys));
if(unlikely( sys == NULL ))
return VLC_ENOMEM;
sys->fd = -1;
sys->level = 100;
sys->mute = false;
sys->device = var_InheritString (aout, "oss-audio-device");
aout->sys = sys;
aout->start = Start;
aout->stop = Stop;
aout->volume_set = VolumeSet;
aout->mute_set = MuteSet;
aout->device_select = DeviceSelect;
DevicesEnum (aout);
return VLC_SUCCESS;
}
static void Close (vlc_object_t *obj)
{
audio_output_t *aout = (audio_output_t *)obj;
aout_sys_t *sys = aout->sys;
free (sys->device);
free (sys);
}