1 /*****************************************************************************
2 * oss.c : OSS /dev/dsp module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2002 the VideoLAN team
7 * Authors: Michel Kaempf <maxx@via.ecp.fr>
8 * Sam Hocevar <sam@zoy.org>
9 * Christophe Massiot <massiot@via.ecp.fr>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
34 #include <errno.h> /* ENOMEM */
35 #include <fcntl.h> /* open(), O_WRONLY */
36 #include <sys/ioctl.h> /* ioctl() */
37 #include <unistd.h> /* write(), close() */
39 #include <vlc_common.h>
40 #include <vlc_plugin.h>
48 /* SNDCTL_DSP_RESET, SNDCTL_DSP_SETFMT, SNDCTL_DSP_STEREO, SNDCTL_DSP_SPEED,
49 * SNDCTL_DSP_GETOSPACE */
50 #ifdef HAVE_SOUNDCARD_H
51 # include <soundcard.h>
52 #elif defined( HAVE_SYS_SOUNDCARD_H )
53 # include <sys/soundcard.h>
54 #elif defined( HAVE_MACHINE_SOUNDCARD_H )
55 # include <machine/soundcard.h>
58 /* Patches for ignorant OSS versions */
60 # define AFMT_AC3 0x00000400 /* Dolby Digital AC3 */
64 # ifdef WORDS_BIGENDIAN
65 # define AFMT_S16_NE AFMT_S16_BE
67 # define AFMT_S16_NE AFMT_S16_LE
71 /*****************************************************************************
72 * aout_sys_t: OSS audio output method descriptor
73 *****************************************************************************
74 * This structure is part of the audio output thread descriptor.
75 * It describes the DSP specific properties of an audio device.
76 *****************************************************************************/
80 int b_workaround_buggy_driver;
82 mtime_t max_buffer_duration;
85 /* This must be a power of 2. */
86 #define FRAME_SIZE 1024
87 #define FRAME_COUNT 32
89 /*****************************************************************************
91 *****************************************************************************/
92 static int Open ( vlc_object_t * );
93 static void Close ( vlc_object_t * );
95 static void Play ( aout_instance_t * );
96 static void* OSSThread ( vlc_object_t * );
98 static mtime_t BufferDuration( aout_instance_t * p_aout );
100 /*****************************************************************************
102 *****************************************************************************/
103 #define BUGGY_TEXT N_("Try to work around buggy OSS drivers")
104 #define BUGGY_LONGTEXT N_( \
105 "Some buggy OSS drivers just don't like when their internal buffers " \
106 "are completely filled (the sound gets heavily hashed). If you have one " \
107 "of these drivers, then you need to enable this option." )
110 set_shortname( "OSS" )
111 set_description( N_("UNIX OSS audio output") )
113 set_category( CAT_AUDIO )
114 set_subcategory( SUBCAT_AUDIO_AOUT )
115 add_file( "oss-audio-device", "/dev/dsp", aout_FindAndRestart,
116 N_("OSS DSP device"), NULL, false );
117 add_deprecated_alias( "dspdev" ) /* deprecated since 0.9.3 */
118 add_bool( "oss-buggy", 0, NULL, BUGGY_TEXT, BUGGY_LONGTEXT, true )
120 set_capability( "audio output", 100 )
121 add_shortcut( "oss" )
122 set_callbacks( Open, Close )
125 /*****************************************************************************
126 * Probe: probe the audio device for available formats and channels
127 *****************************************************************************/
128 static void Probe( aout_instance_t * p_aout )
130 struct aout_sys_t * p_sys = p_aout->output.p_sys;
131 vlc_value_t val, text;
132 int i_format, i_nb_channels;
134 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
135 text.psz_string = _("Audio Device");
136 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
138 /* Test for multi-channel. */
139 #ifdef SNDCTL_DSP_GETCHANNELMASK
140 if ( aout_FormatNbChannels( &p_aout->output.output ) > 2 )
142 /* Check that the device supports this. */
147 i_format = AFMT_S16_NE;
148 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
149 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
151 msg_Err( p_aout, "cannot reset OSS audio device" );
152 var_Destroy( p_aout, "audio-device" );
156 if ( ioctl( p_sys->i_fd, SNDCTL_DSP_GETCHANNELMASK,
159 if ( !(i_chanmask & DSP_BIND_FRONT) )
161 msg_Err( p_aout, "no front channels! (%x)",
166 if ( (i_chanmask & (DSP_BIND_SURR | DSP_BIND_CENTER_LFE))
167 && (p_aout->output.output.i_physical_channels ==
168 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
169 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
172 val.i_int = AOUT_VAR_5_1;
173 text.psz_string = "5.1";
174 var_Change( p_aout, "audio-device",
175 VLC_VAR_ADDCHOICE, &val, &text );
178 if ( (i_chanmask & DSP_BIND_SURR)
179 && (p_aout->output.output.i_physical_channels &
180 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
181 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT)) )
183 val.i_int = AOUT_VAR_2F2R;
184 text.psz_string = N_("2 Front 2 Rear");
185 var_Change( p_aout, "audio-device",
186 VLC_VAR_ADDCHOICE, &val, &text );
193 i_format = AFMT_S16_NE;
194 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
195 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
197 msg_Err( p_aout, "cannot reset OSS audio device" );
198 var_Destroy( p_aout, "audio-device" );
202 /* Test for stereo. */
204 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
205 && i_nb_channels == 2 )
207 val.i_int = AOUT_VAR_STEREO;
208 text.psz_string = N_("Stereo");
209 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
213 i_format = AFMT_S16_NE;
214 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
215 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
217 msg_Err( p_aout, "cannot reset OSS audio device" );
218 var_Destroy( p_aout, "audio-device" );
224 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
225 && i_nb_channels == 1 )
227 val.i_int = AOUT_VAR_MONO;
228 text.psz_string = N_("Mono");
229 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
230 if ( p_aout->output.output.i_physical_channels == AOUT_CHAN_CENTER )
232 var_Set( p_aout, "audio-device", val );
236 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
238 msg_Err( p_aout, "cannot reset OSS audio device" );
239 var_Destroy( p_aout, "audio-device" );
243 /* Test for spdif. */
244 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
248 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) >= 0
249 && i_format == AFMT_AC3 )
251 val.i_int = AOUT_VAR_SPDIF;
252 text.psz_string = N_("A/52 over S/PDIF");
253 var_Change( p_aout, "audio-device",
254 VLC_VAR_ADDCHOICE, &val, &text );
255 if( config_GetInt( p_aout, "spdif" ) )
256 var_Set( p_aout, "audio-device", val );
258 else if( config_GetInt( p_aout, "spdif" ) )
260 msg_Warn( p_aout, "S/PDIF not supported by card" );
264 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart,
268 /*****************************************************************************
269 * Open: open the audio device (the digital sound processor)
270 *****************************************************************************
271 * This function opens the DSP as a usual non-blocking write-only file, and
272 * modifies the p_aout->p_sys->i_fd with the file's descriptor.
273 *****************************************************************************/
274 static int Open( vlc_object_t *p_this )
276 aout_instance_t * p_aout = (aout_instance_t *)p_this;
277 struct aout_sys_t * p_sys;
281 /* Allocate structure */
282 p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
286 /* Get device name */
287 if( (psz_device = config_GetPsz( p_aout, "oss-audio-device" )) == NULL )
289 msg_Err( p_aout, "no audio device specified (maybe /dev/dsp?)" );
294 /* Open the sound device in non-blocking mode, because ALSA's OSS
295 * emulation and some broken OSS drivers would make a blocking call
296 * wait forever until the device is available. Since this breaks the
297 * OSS spec, we immediately put it back to blocking mode if the
298 * operation was successful. */
299 p_sys->i_fd = open( psz_device, O_WRONLY | O_NDELAY );
300 if( p_sys->i_fd < 0 )
302 msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
308 /* if the opening was ok, put the device back in blocking mode */
309 fcntl( p_sys->i_fd, F_SETFL,
310 fcntl( p_sys->i_fd, F_GETFL ) &~ FNDELAY );
314 p_aout->output.pf_play = Play;
316 if ( var_Type( p_aout, "audio-device" ) == 0 )
321 if ( var_Get( p_aout, "audio-device", &val ) < 0 )
323 /* Probe() has failed. */
328 if ( val.i_int == AOUT_VAR_SPDIF )
330 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
332 else if ( val.i_int == AOUT_VAR_5_1 )
334 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
335 p_aout->output.output.i_physical_channels
336 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
337 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
340 else if ( val.i_int == AOUT_VAR_2F2R )
342 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
343 p_aout->output.output.i_physical_channels
344 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
345 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
347 else if ( val.i_int == AOUT_VAR_STEREO )
349 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
350 p_aout->output.output.i_physical_channels
351 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
353 else if ( val.i_int == AOUT_VAR_MONO )
355 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
356 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
360 /* This should not happen ! */
361 msg_Err( p_aout, "internal: can't find audio-device (%i)", val.i_int );
367 var_Set( p_aout, "intf-change", val );
369 /* Reset the DSP device */
370 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
372 msg_Err( p_aout, "cannot reset OSS audio device" );
373 close( p_sys->i_fd );
378 /* Set the output format */
379 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
381 int i_format = AFMT_AC3;
383 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
384 || i_format != AFMT_AC3 )
386 msg_Err( p_aout, "cannot reset OSS audio device" );
387 close( p_sys->i_fd );
392 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
393 p_aout->output.i_nb_samples = A52_FRAME_NB;
394 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
395 p_aout->output.output.i_frame_length = A52_FRAME_NB;
397 aout_VolumeNoneInit( p_aout );
400 if ( !AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
402 unsigned int i_format = AFMT_S16_NE;
403 unsigned int i_frame_size, i_fragments;
405 unsigned int i_nb_channels;
406 audio_buf_info audio_buf;
408 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
410 msg_Err( p_aout, "cannot set audio output format" );
411 close( p_sys->i_fd );
419 p_aout->output.output.i_format = VLC_FOURCC('u','8',' ',' ');
422 p_aout->output.output.i_format = VLC_FOURCC('s','8',' ',' ');
425 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','l');
428 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l');
431 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','b');
434 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','b');
437 msg_Err( p_aout, "OSS fell back to an unknown format (%d)",
439 close( p_sys->i_fd );
444 i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
446 /* Set the number of channels */
447 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) < 0 ||
448 i_nb_channels != aout_FormatNbChannels( &p_aout->output.output ) )
450 msg_Err( p_aout, "cannot set number of audio channels (%s)",
451 aout_FormatPrintChannels( &p_aout->output.output) );
452 close( p_sys->i_fd );
457 /* Set the output rate */
458 i_rate = p_aout->output.output.i_rate;
459 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
461 msg_Err( p_aout, "cannot set audio output rate (%i)",
462 p_aout->output.output.i_rate );
463 close( p_sys->i_fd );
468 if( i_rate != p_aout->output.output.i_rate )
470 p_aout->output.output.i_rate = i_rate;
473 /* Set the fragment size */
474 aout_FormatPrepare( &p_aout->output.output );
476 /* i_fragment = xxxxyyyy where: xxxx is fragtotal
477 * 1 << yyyy is fragsize */
478 i_frame_size = ((uint64_t)p_aout->output.output.i_bytes_per_frame * p_aout->output.output.i_rate * 65536) / (48000 * 2 * 2) / FRAME_COUNT;
480 while( i_fragments < 12 && (1U << i_fragments) < i_frame_size )
484 i_fragments |= FRAME_COUNT << 16;
485 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
487 msg_Warn( p_aout, "cannot set fragment size (%.8x)", i_fragments );
490 if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf ) < 0 )
492 msg_Err( p_aout, "cannot get fragment size" );
493 close( p_sys->i_fd );
499 /* Number of fragments actually allocated */
500 p_aout->output.p_sys->i_fragstotal = audio_buf.fragstotal;
502 /* Maximum duration the soundcard's buffer can hold */
503 p_aout->output.p_sys->max_buffer_duration =
504 (mtime_t)audio_buf.fragstotal * audio_buf.fragsize * 1000000
505 / p_aout->output.output.i_bytes_per_frame
506 / p_aout->output.output.i_rate
507 * p_aout->output.output.i_frame_length;
509 p_aout->output.i_nb_samples = audio_buf.fragsize /
510 p_aout->output.output.i_bytes_per_frame;
513 aout_VolumeSoftInit( p_aout );
516 p_aout->output.p_sys->b_workaround_buggy_driver =
517 config_GetInt( p_aout, "oss-buggy" );
519 /* Create OSS thread and wait for its readiness. */
520 if( vlc_thread_create( p_aout, "aout", OSSThread,
521 VLC_THREAD_PRIORITY_OUTPUT ) )
523 msg_Err( p_aout, "cannot create OSS thread (%m)" );
524 close( p_sys->i_fd );
532 /*****************************************************************************
533 * Play: nothing to do
534 *****************************************************************************/
535 static void Play( aout_instance_t *p_aout )
540 /*****************************************************************************
541 * Close: close the DSP audio device
542 *****************************************************************************/
543 static void Close( vlc_object_t * p_this )
545 aout_instance_t *p_aout = (aout_instance_t *)p_this;
546 struct aout_sys_t * p_sys = p_aout->output.p_sys;
548 vlc_object_kill( p_aout );
549 vlc_thread_join( p_aout );
550 p_aout->b_die = false;
552 ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
553 close( p_sys->i_fd );
558 /*****************************************************************************
559 * BufferDuration: buffer status query
560 *****************************************************************************
561 * This function returns the duration in microseconds of the current buffer.
562 *****************************************************************************/
563 static mtime_t BufferDuration( aout_instance_t * p_aout )
565 struct aout_sys_t * p_sys = p_aout->output.p_sys;
566 audio_buf_info audio_buf;
569 /* Fill the audio_buf_info structure:
570 * - fragstotal: total number of fragments allocated
571 * - fragsize: size of a fragment in bytes
572 * - bytes: available space in bytes (includes partially used fragments)
573 * Note! 'bytes' could be more than fragments*fragsize */
574 ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
576 /* calculate number of available fragments (not partially used ones) */
577 i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
579 /* Return the fragment duration */
580 return (mtime_t)i_bytes * 1000000
581 / p_aout->output.output.i_bytes_per_frame
582 / p_aout->output.output.i_rate
583 * p_aout->output.output.i_frame_length;
586 /*****************************************************************************
587 * OSSThread: asynchronous thread used to DMA the data to the device
588 *****************************************************************************/
589 static void* OSSThread( vlc_object_t *p_this )
591 aout_instance_t * p_aout = (aout_instance_t*)p_this;
592 struct aout_sys_t * p_sys = p_aout->output.p_sys;
593 mtime_t next_date = 0;
594 int canc = vlc_savecancel ();
596 while ( vlc_object_alive (p_aout) )
598 aout_buffer_t * p_buffer = NULL;
602 if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
604 mtime_t buffered = BufferDuration( p_aout );
606 if( p_aout->output.p_sys->b_workaround_buggy_driver )
608 #define i_fragstotal p_aout->output.p_sys->i_fragstotal
609 /* Wait a bit - we don't want our buffer to be full */
610 if( buffered > (p_aout->output.p_sys->max_buffer_duration
611 / i_fragstotal * (i_fragstotal - 1)) )
613 msleep((p_aout->output.p_sys->max_buffer_duration
615 buffered = BufferDuration( p_aout );
620 /* Next buffer will be played at mdate() + buffered */
621 p_buffer = aout_OutputNextBuffer( p_aout, mdate() + buffered,
624 if( p_buffer == NULL &&
625 buffered > ( p_aout->output.p_sys->max_buffer_duration
626 / p_aout->output.p_sys->i_fragstotal ) )
628 /* If we have at least a fragment full, then we can wait a
629 * little and retry to get a new audio buffer instead of
630 * playing a blank sample */
631 msleep( ( p_aout->output.p_sys->max_buffer_duration
632 / p_aout->output.p_sys->i_fragstotal / 2 ) );
638 /* emu10k1 driver does not report Buffer Duration correctly in
639 * passthrough mode so we have to cheat */
646 mtime_t delay = next_date - mdate();
647 if( delay > AOUT_PTS_TOLERANCE )
653 while( vlc_object_alive (p_aout) && ! ( p_buffer =
654 aout_OutputNextBuffer( p_aout, next_date, true ) ) )
661 if ( p_buffer != NULL )
663 p_bytes = p_buffer->p_buffer;
664 i_size = p_buffer->i_nb_bytes;
665 /* This is theoretical ... we'll see next iteration whether
667 next_date += p_buffer->end_date - p_buffer->start_date;
671 i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
672 * p_aout->output.output.i_bytes_per_frame;
673 p_bytes = malloc( i_size );
674 memset( p_bytes, 0, i_size );
678 i_tmp = write( p_sys->i_fd, p_bytes, i_size );
682 msg_Err( p_aout, "write failed (%m)" );
685 if ( p_buffer != NULL )
687 aout_BufferFree( p_buffer );
695 vlc_restorecancel (canc);