1 /*****************************************************************************
2 * oss.c : OSS /dev/dsp module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2002 VideoLAN
5 * $Id: oss.c,v 1.31 2002/10/24 17:36:42 gbazin 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 /*****************************************************************************
57 * aout_sys_t: OSS audio output method descriptor
58 *****************************************************************************
59 * This structure is part of the audio output thread descriptor.
60 * It describes the dsp specific properties of an audio device.
61 *****************************************************************************/
65 int b_workaround_buggy_driver;
68 /* This must be a power of 2. */
69 #define FRAME_SIZE 1024
71 #define A52_FRAME_NB 1536
73 /* estimation of the size of the internal soundcard driver
74 * (used for buggy drivers) */
75 #define OSS_BUFFER_SIZE 20000
77 /*****************************************************************************
79 *****************************************************************************/
80 static int Open ( vlc_object_t * );
81 static void Close ( vlc_object_t * );
83 static void Play ( aout_instance_t * );
84 static int OSSThread ( aout_instance_t * );
86 /*****************************************************************************
88 *****************************************************************************/
89 #define BUGGY_TEXT N_("Try to work around buggy OSS drivers")
90 #define BUGGY_LONGTEXT N_( \
91 "Some buggy OSS drivers just don't like when their internal buffers " \
92 "are completely filled (the sound gets heavily hashed). If you have one " \
93 "of these drivers, then you need to enable this option." )
96 add_category_hint( N_("OSS"), NULL );
97 add_file( "dspdev", "/dev/dsp", aout_FindAndRestart,
98 N_("OSS dsp device"), NULL );
99 add_bool( "oss-buggy", 0, NULL, BUGGY_TEXT, BUGGY_LONGTEXT );
100 set_description( _("Linux OSS /dev/dsp module") );
101 set_capability( "audio output", 100 );
102 add_shortcut( "oss" );
103 set_callbacks( Open, Close );
106 /*****************************************************************************
107 * Open: open the audio device (the digital sound processor)
108 *****************************************************************************
109 * This function opens the dsp as a usual non-blocking write-only file, and
110 * modifies the p_aout->p_sys->i_fd with the file's descriptor.
111 *****************************************************************************/
112 static int Open( vlc_object_t *p_this )
114 aout_instance_t * p_aout = (aout_instance_t *)p_this;
115 struct aout_sys_t * p_sys;
118 /* Allocate structure */
119 p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
122 msg_Err( p_aout, "out of memory" );
126 /* Get device name */
127 if( (psz_device = config_GetPsz( p_aout, "dspdev" )) == NULL )
129 msg_Err( p_aout, "no audio device given (maybe /dev/dsp ?)" );
134 p_aout->output.p_sys->b_workaround_buggy_driver =
135 config_GetInt( p_aout, "oss-buggy" );
137 if( p_aout->output.p_sys->b_workaround_buggy_driver )
138 p_sys->i_fd = open( psz_device, O_WRONLY|O_NONBLOCK );
140 p_sys->i_fd = open( psz_device, O_WRONLY );
142 /* Open the sound device */
143 if( p_sys->i_fd < 0 )
145 msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
152 p_aout->output.pf_play = Play;
154 /* Reset the DSP device */
155 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
157 msg_Err( p_aout, "cannot reset OSS audio device" );
158 close( p_sys->i_fd );
163 /* Set the output format */
164 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
166 int i_format = AFMT_AC3;
168 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
169 || i_format != AFMT_AC3 )
171 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
175 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
176 p_aout->output.i_nb_samples = A52_FRAME_NB;
177 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
178 p_aout->output.output.i_frame_length = A52_FRAME_NB;
180 aout_VolumeNoneInit( p_aout );
184 if ( !AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
186 int i_format = AFMT_S16_NE;
187 int i_frame_size, i_fragments;
191 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
193 msg_Err( p_aout, "cannot set audio output format" );
194 close( p_sys->i_fd );
202 p_aout->output.output.i_format = VLC_FOURCC('u','8',' ',' ');
205 p_aout->output.output.i_format = VLC_FOURCC('s','8',' ',' ');
208 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','l');
211 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l');
214 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','b');
217 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','b');
220 msg_Err( p_aout, "OSS fell back to an unknown format (%d)",
222 close( p_sys->i_fd );
227 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
228 p_aout->output.i_nb_samples = FRAME_SIZE;
230 aout_VolumeSoftInit( p_aout );
232 /* Set the fragment size
233 * i_fragment = xxxxyyyy where: xxxx is fragtotal
234 * 1 << yyyy is fragsize */
236 i_frame_size = FRAME_SIZE;
237 while( i_frame_size >>= 1 )
241 i_fragments |= FRAME_COUNT << 16;
242 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
244 msg_Warn( p_aout, "cannot set fragment size (%.8x)", i_fragments );
247 /* These cases are desperate because of the OSS API and A/52 spec. */
248 switch ( p_aout->output.output.i_channels )
253 case AOUT_CHAN_STEREO | AOUT_CHAN_LFE:
254 case AOUT_CHAN_2F1R | AOUT_CHAN_LFE:
255 case AOUT_CHAN_3F1R | AOUT_CHAN_LFE:
256 case AOUT_CHAN_DOLBY | AOUT_CHAN_LFE:
257 p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
259 case AOUT_CHAN_CHANNEL | AOUT_CHAN_LFE:
260 case AOUT_CHAN_CHANNEL1 | AOUT_CHAN_LFE:
261 case AOUT_CHAN_CHANNEL2 | AOUT_CHAN_LFE:
262 case AOUT_CHAN_MONO | AOUT_CHAN_LFE:
263 case AOUT_CHAN_2F2R | AOUT_CHAN_LFE:
264 p_aout->output.output.i_channels &= ~AOUT_CHAN_LFE;
267 p_aout->output.output.i_channels = AOUT_CHAN_2F2R;
270 /* In a nutshell, possible types : AOUT_CHAN_STEREO (and al.),
271 AOUT_CHAN_2F2R, AOUT_CHAN_3F1R | AOUT_CHAN_LFE. */
273 i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
275 if ( i_nb_channels > 2 )
277 /* Check that the device supports this. */
279 #ifdef SNDCTL_DSP_GETCHANNELMASK
281 if ( ioctl( p_sys->i_fd, SNDCTL_DSP_GETCHANNELMASK,
284 if ( !(i_chanmask & DSP_BIND_FRONT) )
286 msg_Err( p_aout, "No front channels ! (%x)",
288 close( p_sys->i_fd );
293 if ( !(i_chanmask & DSP_BIND_SURR) )
295 p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
299 if ( p_aout->output.output.i_channels ==
300 (AOUT_CHAN_3F2R | AOUT_CHAN_LFE)
301 && !(i_chanmask & DSP_BIND_CENTER_LFE) )
303 p_aout->output.output.i_channels = AOUT_CHAN_2F2R;
310 /* The driver doesn't support this call, assume it is stereo. */
311 p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
316 /* Set the number of channels */
317 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) < 0 )
319 msg_Err( p_aout, "cannot set number of audio channels (%i)",
320 p_aout->output.output.i_channels );
321 close( p_sys->i_fd );
326 if ( i_nb_channels != aout_FormatNbChannels( &p_aout->output.output ) )
328 switch ( i_nb_channels )
330 case 1: p_aout->output.output.i_channels = AOUT_CHAN_MONO; break;
331 case 2: p_aout->output.output.i_channels = AOUT_CHAN_STEREO; break;
332 case 4: p_aout->output.output.i_channels = AOUT_CHAN_2F2R; break;
334 msg_Err( p_aout, "Unsupported downmixing (%d)", i_nb_channels );
335 close( p_sys->i_fd );
341 /* Set the output rate */
342 i_rate = p_aout->output.output.i_rate;
343 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
345 msg_Err( p_aout, "cannot set audio output rate (%i)",
346 p_aout->output.output.i_rate );
347 close( p_sys->i_fd );
352 if( i_rate != p_aout->output.output.i_rate )
354 p_aout->output.output.i_rate = i_rate;
358 /* Create OSS thread and wait for its readiness. */
359 if( vlc_thread_create( p_aout, "aout", OSSThread,
360 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
362 msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
363 close( p_sys->i_fd );
372 /*****************************************************************************
373 * Play: nothing to do
374 *****************************************************************************/
375 static void Play( aout_instance_t *p_aout )
379 /*****************************************************************************
380 * Close: close the dsp audio device
381 *****************************************************************************/
382 static void Close( vlc_object_t * p_this )
384 aout_instance_t *p_aout = (aout_instance_t *)p_this;
385 struct aout_sys_t * p_sys = p_aout->output.p_sys;
387 p_aout->b_die = VLC_TRUE;
388 vlc_thread_join( p_aout );
389 p_aout->b_die = VLC_FALSE;
391 ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
392 close( p_sys->i_fd );
398 /*****************************************************************************
399 * BufferDuration: buffer status query
400 *****************************************************************************
401 * This function returns the duration in microseconds of the current buffer.
402 *****************************************************************************/
403 static mtime_t BufferDuration( aout_instance_t * p_aout )
405 struct aout_sys_t * p_sys = p_aout->output.p_sys;
406 audio_buf_info audio_buf;
409 /* Fill the audio_buf_info structure:
410 * - fragstotal: total number of fragments allocated
411 * - fragsize: size of a fragment in bytes
412 * - bytes: available space in bytes (includes partially used fragments)
413 * Note! 'bytes' could be more than fragments*fragsize */
414 ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
416 /* calculate number of available fragments (not partially used ones) */
417 i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
419 /* Return the fragment duration */
420 return (mtime_t)i_bytes * 1000000
421 / p_aout->output.output.i_bytes_per_frame
422 / p_aout->output.output.i_rate
423 * p_aout->output.output.i_frame_length;
426 /*****************************************************************************
427 * OSSThread: asynchronous thread used to DMA the data to the device
428 *****************************************************************************/
429 static int OSSThread( aout_instance_t * p_aout )
431 struct aout_sys_t * p_sys = p_aout->output.p_sys;
432 mtime_t next_date = 0;
434 while ( !p_aout->b_die )
436 aout_buffer_t * p_buffer = NULL;
440 if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
442 mtime_t buffered = BufferDuration( p_aout );
444 if( p_aout->output.p_sys->b_workaround_buggy_driver )
446 /* Wait a bit - we don't want our buffer to be full */
447 while( buffered > OSS_BUFFER_SIZE )
449 msleep( buffered - OSS_BUFFER_SIZE * 2 );
450 buffered = BufferDuration( p_aout );
456 /* This is the _real_ presentation date */
457 next_date = mdate() + buffered;
461 /* Give a hint to the audio output about our drift, but
462 * not too much because we want to make it happy with our
463 * nicely calculated dates. */
464 next_date = ( (next_date * 7) + (mdate() + buffered) ) / 8;
467 /* Next buffer will be played at mdate()+buffered */
468 p_buffer = aout_OutputNextBuffer( p_aout, next_date, VLC_FALSE );
472 /* emu10k1 driver does not report Buffer Duration correctly in
473 * passthrough mode so we have to cheat */
480 mtime_t delay = next_date - mdate();
481 if( delay > AOUT_PTS_TOLERANCE )
487 while( !p_aout->b_die && ! ( p_buffer =
488 aout_OutputNextBuffer( p_aout, next_date, VLC_TRUE ) ) )
495 if ( p_buffer != NULL )
497 p_bytes = p_buffer->p_buffer;
498 i_size = p_buffer->i_nb_bytes;
499 /* This is theoretical ... we'll see next iteration whether
501 next_date += p_buffer->end_date - p_buffer->start_date;
505 i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
506 * p_aout->output.output.i_bytes_per_frame;
507 p_bytes = malloc( i_size );
508 memset( p_bytes, 0, i_size );
512 i_tmp = write( p_sys->i_fd, p_bytes, i_size );
516 msg_Err( p_aout, "write failed (%s)", strerror(errno) );
519 if ( p_buffer != NULL )
521 aout_BufferFree( p_buffer );