1 /*****************************************************************************
2 * oss.c : OSS /dev/dsp module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2002 VideoLAN
5 * $Id: oss.c,v 1.48 2003/01/28 22:03:21 sam Exp $
7 * Authors: Michel Kaempf <maxx@via.ecp.fr>
8 * Samuel 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <errno.h> /* ENOMEM */
30 #include <fcntl.h> /* open(), O_WRONLY */
31 #include <sys/ioctl.h> /* ioctl() */
32 #include <string.h> /* strerror() */
33 #include <unistd.h> /* write(), close() */
34 #include <stdlib.h> /* calloc(), malloc(), free() */
44 #include "aout_internal.h"
46 /* SNDCTL_DSP_RESET, SNDCTL_DSP_SETFMT, SNDCTL_DSP_STEREO, SNDCTL_DSP_SPEED,
47 * SNDCTL_DSP_GETOSPACE */
48 #ifdef HAVE_SOUNDCARD_H
49 # include <soundcard.h>
50 #elif defined( HAVE_SYS_SOUNDCARD_H )
51 # include <sys/soundcard.h>
52 #elif defined( HAVE_MACHINE_SOUNDCARD_H )
53 # include <machine/soundcard.h>
56 /* AFMT_AC3 is really IEC61937 / IEC60958, mpeg/ac3/dts over spdif */
58 # define AFMT_AC3 0x00000400 /* Dolby Digital AC3 */
61 # define AFMT_S16_NE 0x00000010
63 /*****************************************************************************
64 * aout_sys_t: OSS audio output method descriptor
65 *****************************************************************************
66 * This structure is part of the audio output thread descriptor.
67 * It describes the dsp specific properties of an audio device.
68 *****************************************************************************/
72 int b_workaround_buggy_driver;
74 mtime_t max_buffer_duration;
77 /* This must be a power of 2. */
78 #define FRAME_SIZE 1024
81 /*****************************************************************************
83 *****************************************************************************/
84 static int Open ( vlc_object_t * );
85 static void Close ( vlc_object_t * );
87 static void Play ( aout_instance_t * );
88 static int OSSThread ( aout_instance_t * );
90 static mtime_t BufferDuration( aout_instance_t * p_aout );
92 /*****************************************************************************
94 *****************************************************************************/
95 #define BUGGY_TEXT N_("try to work around buggy OSS drivers")
96 #define BUGGY_LONGTEXT N_( \
97 "Some buggy OSS drivers just don't like when their internal buffers " \
98 "are completely filled (the sound gets heavily hashed). If you have one " \
99 "of these drivers, then you need to enable this option." )
101 #define SPDIF_TEXT N_("try to use S/PDIF output")
102 #define SPDIF_LONGTEXT N_( \
103 "Sometimes we attempt to use the S/PDIF output, even if nothing is " \
104 "connected to it. Un-checking this option disables this behaviour, " \
105 "and permanently selects analog PCM output." )
108 add_category_hint( N_("OSS"), NULL );
109 add_file( "dspdev", "/dev/dsp", aout_FindAndRestart,
110 N_("OSS dsp device"), NULL );
111 add_bool( "oss-buggy", 0, NULL, BUGGY_TEXT, BUGGY_LONGTEXT );
112 add_bool( "spdif", 1, NULL, SPDIF_TEXT, SPDIF_LONGTEXT );
113 set_description( _("Linux OSS /dev/dsp module") );
114 set_capability( "audio output", 100 );
115 add_shortcut( "oss" );
116 set_callbacks( Open, Close );
119 /*****************************************************************************
120 * Probe: probe the audio device for available formats and channels
121 *****************************************************************************/
122 static void Probe( aout_instance_t * p_aout )
124 struct aout_sys_t * p_sys = p_aout->output.p_sys;
126 int i_format, i_nb_channels;
128 var_Create( p_aout, "audio-device", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
130 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
132 msg_Err( p_aout, "cannot reset OSS audio device" );
133 var_Destroy( p_aout, "audio-device" );
137 if ( config_GetInt( p_aout, "spdif" )
138 && AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
142 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) >= 0
143 && i_format == AFMT_AC3 )
145 val.psz_string = N_("A/52 over S/PDIF");
146 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
150 /* Go to PCM mode. */
151 i_format = AFMT_S16_NE;
152 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
153 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
158 #ifdef SNDCTL_DSP_GETCHANNELMASK
159 if ( aout_FormatNbChannels( &p_aout->output.output ) > 2 )
161 /* Check that the device supports this. */
164 if ( ioctl( p_sys->i_fd, SNDCTL_DSP_GETCHANNELMASK,
167 if ( !(i_chanmask & DSP_BIND_FRONT) )
169 msg_Err( p_aout, "No front channels ! (%x)",
174 if ( (i_chanmask & (DSP_BIND_SURR | DSP_BIND_CENTER_LFE))
175 && (p_aout->output.output.i_physical_channels ==
176 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
177 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
180 val.psz_string = N_("5.1");
181 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
184 if ( (i_chanmask & DSP_BIND_SURR)
185 && (p_aout->output.output.i_physical_channels &
186 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
187 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT)) )
189 val.psz_string = N_("2 Front 2 Rear");
190 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
196 /* Test for stereo. */
198 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
199 && i_nb_channels == 2 )
201 val.psz_string = N_("Stereo");
202 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
206 i_format = AFMT_S16_NE;
207 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
208 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
210 msg_Err( p_aout, "cannot reset OSS audio device" );
211 var_Destroy( p_aout, "audio-device" );
217 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
218 && i_nb_channels == 1 )
220 val.psz_string = N_("Mono");
221 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
222 if ( p_aout->output.output.i_physical_channels == AOUT_CHAN_CENTER )
224 var_Set( p_aout, "audio-device", val );
229 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart,
233 /*****************************************************************************
234 * Open: open the audio device (the digital sound processor)
235 *****************************************************************************
236 * This function opens the dsp as a usual non-blocking write-only file, and
237 * modifies the p_aout->p_sys->i_fd with the file's descriptor.
238 *****************************************************************************/
239 static int Open( vlc_object_t *p_this )
241 aout_instance_t * p_aout = (aout_instance_t *)p_this;
242 struct aout_sys_t * p_sys;
246 /* Allocate structure */
247 p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
250 msg_Err( p_aout, "out of memory" );
254 /* Get device name */
255 if( (psz_device = config_GetPsz( p_aout, "dspdev" )) == NULL )
257 msg_Err( p_aout, "no audio device given (maybe /dev/dsp ?)" );
262 /* Open the sound device */
263 p_sys->i_fd = open( psz_device, O_WRONLY );
264 if( p_sys->i_fd < 0 )
266 msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
272 p_aout->output.pf_play = Play;
274 if ( var_Type( p_aout, "audio-device" ) == 0 )
279 if ( var_Get( p_aout, "audio-device", &val ) < 0 )
281 /* Probe() has failed. */
286 if ( !strcmp( val.psz_string, N_("A/52 over S/PDIF") ) )
288 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
290 else if ( !strcmp( val.psz_string, N_("5.1") ) )
292 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
293 p_aout->output.output.i_physical_channels
294 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
295 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT
298 else if ( !strcmp( val.psz_string, N_("2 Front 2 Rear") ) )
300 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
301 p_aout->output.output.i_physical_channels
302 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
303 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT;
305 else if ( !strcmp( val.psz_string, N_("Stereo") ) )
307 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
308 p_aout->output.output.i_physical_channels
309 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
311 else if ( !strcmp( val.psz_string, N_("Mono") ) )
313 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
314 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
318 /* This should not happen ! */
319 msg_Err( p_aout, "internal: can't find audio-device (%s)",
324 free( val.psz_string );
326 val.b_bool = VLC_TRUE;
327 var_Set( p_aout, "intf-change", val );
329 /* Reset the DSP device */
330 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
332 msg_Err( p_aout, "cannot reset OSS audio device" );
333 close( p_sys->i_fd );
338 /* Set the output format */
339 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
341 int i_format = AFMT_AC3;
343 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
344 || i_format != AFMT_AC3 )
346 msg_Err( p_aout, "cannot reset OSS audio device" );
347 close( p_sys->i_fd );
352 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
353 p_aout->output.i_nb_samples = A52_FRAME_NB;
354 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
355 p_aout->output.output.i_frame_length = A52_FRAME_NB;
357 aout_VolumeNoneInit( p_aout );
360 if ( !AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
362 unsigned int i_format = AFMT_S16_NE;
363 unsigned int i_frame_size, i_fragments;
365 unsigned int i_nb_channels;
366 audio_buf_info audio_buf;
368 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
370 msg_Err( p_aout, "cannot set audio output format" );
371 close( p_sys->i_fd );
379 p_aout->output.output.i_format = VLC_FOURCC('u','8',' ',' ');
382 p_aout->output.output.i_format = VLC_FOURCC('s','8',' ',' ');
385 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','l');
388 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l');
391 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','b');
394 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','b');
397 msg_Err( p_aout, "OSS fell back to an unknown format (%d)",
399 close( p_sys->i_fd );
404 i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
406 /* Set the number of channels */
407 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) < 0 ||
408 i_nb_channels != aout_FormatNbChannels( &p_aout->output.output ) )
410 msg_Err( p_aout, "cannot set number of audio channels (%s)",
411 aout_FormatPrintChannels( &p_aout->output.output) );
412 close( p_sys->i_fd );
417 /* Set the output rate */
418 i_rate = p_aout->output.output.i_rate;
419 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
421 msg_Err( p_aout, "cannot set audio output rate (%i)",
422 p_aout->output.output.i_rate );
423 close( p_sys->i_fd );
428 if( i_rate != p_aout->output.output.i_rate )
430 p_aout->output.output.i_rate = i_rate;
433 /* Set the fragment size */
434 aout_FormatPrepare( &p_aout->output.output );
436 /* i_fragment = xxxxyyyy where: xxxx is fragtotal
437 * 1 << yyyy is fragsize */
439 i_frame_size = FRAME_SIZE * p_aout->output.output.i_bytes_per_frame;
440 while( i_frame_size >>= 1 )
444 i_fragments |= FRAME_COUNT << 16;
445 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
447 msg_Warn( p_aout, "cannot set fragment size (%.8x)", i_fragments );
450 if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf ) < 0 )
452 msg_Err( p_aout, "cannot get fragment size" );
453 close( p_sys->i_fd );
459 /* Number of fragments actually allocated */
460 p_aout->output.p_sys->i_fragstotal = audio_buf.fragstotal;
462 /* Maximum duration the soundcard's buffer can hold */
463 p_aout->output.p_sys->max_buffer_duration =
464 (mtime_t)audio_buf.fragstotal * audio_buf.fragsize * 1000000
465 / p_aout->output.output.i_bytes_per_frame
466 / p_aout->output.output.i_rate
467 * p_aout->output.output.i_frame_length;
469 p_aout->output.i_nb_samples = audio_buf.fragsize /
470 p_aout->output.output.i_bytes_per_frame;
473 aout_VolumeSoftInit( p_aout );
476 p_aout->output.p_sys->b_workaround_buggy_driver =
477 config_GetInt( p_aout, "oss-buggy" );
479 /* Create OSS thread and wait for its readiness. */
480 if( vlc_thread_create( p_aout, "aout", OSSThread,
481 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
483 msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
484 close( p_sys->i_fd );
492 /*****************************************************************************
493 * Play: nothing to do
494 *****************************************************************************/
495 static void Play( aout_instance_t *p_aout )
499 /*****************************************************************************
500 * Close: close the dsp audio device
501 *****************************************************************************/
502 static void Close( vlc_object_t * p_this )
504 aout_instance_t *p_aout = (aout_instance_t *)p_this;
505 struct aout_sys_t * p_sys = p_aout->output.p_sys;
507 p_aout->b_die = VLC_TRUE;
508 vlc_thread_join( p_aout );
509 p_aout->b_die = VLC_FALSE;
511 ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
512 close( p_sys->i_fd );
517 /*****************************************************************************
518 * BufferDuration: buffer status query
519 *****************************************************************************
520 * This function returns the duration in microseconds of the current buffer.
521 *****************************************************************************/
522 static mtime_t BufferDuration( aout_instance_t * p_aout )
524 struct aout_sys_t * p_sys = p_aout->output.p_sys;
525 audio_buf_info audio_buf;
528 /* Fill the audio_buf_info structure:
529 * - fragstotal: total number of fragments allocated
530 * - fragsize: size of a fragment in bytes
531 * - bytes: available space in bytes (includes partially used fragments)
532 * Note! 'bytes' could be more than fragments*fragsize */
533 ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
535 /* calculate number of available fragments (not partially used ones) */
536 i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
538 /* Return the fragment duration */
539 return (mtime_t)i_bytes * 1000000
540 / p_aout->output.output.i_bytes_per_frame
541 / p_aout->output.output.i_rate
542 * p_aout->output.output.i_frame_length;
545 /*****************************************************************************
546 * OSSThread: asynchronous thread used to DMA the data to the device
547 *****************************************************************************/
548 static int OSSThread( aout_instance_t * p_aout )
550 struct aout_sys_t * p_sys = p_aout->output.p_sys;
551 mtime_t next_date = 0;
553 while ( !p_aout->b_die )
555 aout_buffer_t * p_buffer = NULL;
559 if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
561 mtime_t buffered = BufferDuration( p_aout );
563 if( p_aout->output.p_sys->b_workaround_buggy_driver )
565 #define i_fragstotal p_aout->output.p_sys->i_fragstotal
566 /* Wait a bit - we don't want our buffer to be full */
567 if( buffered > (p_aout->output.p_sys->max_buffer_duration
568 / i_fragstotal * (i_fragstotal - 1)) )
570 msleep((p_aout->output.p_sys->max_buffer_duration
572 buffered = BufferDuration( p_aout );
577 /* Next buffer will be played at mdate() + buffered */
578 p_buffer = aout_OutputNextBuffer( p_aout, mdate() + buffered,
581 if( p_buffer == NULL &&
582 buffered > ( p_aout->output.p_sys->max_buffer_duration
583 / p_aout->output.p_sys->i_fragstotal ) )
585 /* If we have at least a fragment full, then we can wait a
586 * little and retry to get a new audio buffer instead of
587 * playing a blank sample */
588 msleep( ( p_aout->output.p_sys->max_buffer_duration
589 / p_aout->output.p_sys->i_fragstotal / 2 ) );
595 /* emu10k1 driver does not report Buffer Duration correctly in
596 * passthrough mode so we have to cheat */
603 mtime_t delay = next_date - mdate();
604 if( delay > AOUT_PTS_TOLERANCE )
610 while( !p_aout->b_die && ! ( p_buffer =
611 aout_OutputNextBuffer( p_aout, next_date, VLC_TRUE ) ) )
618 if ( p_buffer != NULL )
620 p_bytes = p_buffer->p_buffer;
621 i_size = p_buffer->i_nb_bytes;
622 /* This is theoretical ... we'll see next iteration whether
624 next_date += p_buffer->end_date - p_buffer->start_date;
628 i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
629 * p_aout->output.output.i_bytes_per_frame;
630 p_bytes = malloc( i_size );
631 memset( p_bytes, 0, i_size );
635 i_tmp = write( p_sys->i_fd, p_bytes, i_size );
639 msg_Err( p_aout, "write failed (%s)", strerror(errno) );
642 if ( p_buffer != NULL )
644 aout_BufferFree( p_buffer );