1 /*****************************************************************************
2 * oss.c : OSS /dev/dsp module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2002 VideoLAN
5 * $Id: oss.c,v 1.33 2002/11/14 22:38:47 massiot 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;
67 mtime_t max_buffer_duration;
70 /* This must be a power of 2. */
71 #define FRAME_SIZE 1024
74 /*****************************************************************************
76 *****************************************************************************/
77 static int Open ( vlc_object_t * );
78 static void Close ( vlc_object_t * );
80 static void Play ( aout_instance_t * );
81 static int OSSThread ( aout_instance_t * );
83 static mtime_t BufferDuration( aout_instance_t * p_aout );
85 /*****************************************************************************
87 *****************************************************************************/
88 #define BUGGY_TEXT N_("Try to work around buggy OSS drivers")
89 #define BUGGY_LONGTEXT N_( \
90 "Some buggy OSS drivers just don't like when their internal buffers " \
91 "are completely filled (the sound gets heavily hashed). If you have one " \
92 "of these drivers, then you need to enable this option." )
95 add_category_hint( N_("OSS"), NULL );
96 add_file( "dspdev", "/dev/dsp", aout_FindAndRestart,
97 N_("OSS dsp device"), NULL );
98 add_bool( "oss-buggy", 0, NULL, BUGGY_TEXT, BUGGY_LONGTEXT );
99 set_description( _("Linux OSS /dev/dsp module") );
100 set_capability( "audio output", 100 );
101 add_shortcut( "oss" );
102 set_callbacks( Open, Close );
105 /*****************************************************************************
106 * Probe: probe the audio device for available formats and channels
107 *****************************************************************************/
108 static void Probe( aout_instance_t * p_aout )
110 struct aout_sys_t * p_sys = p_aout->output.p_sys;
112 int i_format, i_nb_channels;
114 var_Create( p_aout, "audio-device", VLC_VAR_STRING | VLC_VAR_ISLIST );
116 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
118 msg_Err( p_aout, "cannot reset OSS audio device" );
119 var_Destroy( p_aout, "audio-device" );
123 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
127 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) >= 0
128 && i_format == AFMT_AC3 )
130 val.psz_string = N_("S/PDIF");
131 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
135 /* Go to PCM mode. */
136 i_format = AFMT_S16_NE;
137 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
138 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
143 #ifdef SNDCTL_DSP_GETCHANNELMASK
144 if ( aout_FormatNbChannels( &p_aout->output.output ) > 2 )
146 /* Check that the device supports this. */
149 if ( ioctl( p_sys->i_fd, SNDCTL_DSP_GETCHANNELMASK,
152 if ( !(i_chanmask & DSP_BIND_FRONT) )
154 msg_Err( p_aout, "No front channels ! (%x)",
159 if ( (i_chanmask & (DSP_BIND_SURR | DSP_BIND_CENTER_LFE))
160 && (p_aout->output.output.i_physical_channels ==
161 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
162 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
165 val.psz_string = N_("5.1");
166 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
169 if ( (i_chanmask & DSP_BIND_SURR)
170 && (p_aout->output.output.i_physical_channels &
171 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
172 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT)) )
174 val.psz_string = N_("2 Front 2 Rear");
175 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
181 /* Test for stereo. */
183 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
184 && i_nb_channels == 2 )
186 val.psz_string = N_("Stereo");
187 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
191 i_format = AFMT_S16_NE;
192 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
193 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
195 msg_Err( p_aout, "cannot reset OSS audio device" );
196 var_Destroy( p_aout, "audio-device" );
202 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
203 && i_nb_channels == 1 )
205 val.psz_string = N_("Mono");
206 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
210 /*****************************************************************************
211 * Open: open the audio device (the digital sound processor)
212 *****************************************************************************
213 * This function opens the dsp as a usual non-blocking write-only file, and
214 * modifies the p_aout->p_sys->i_fd with the file's descriptor.
215 *****************************************************************************/
216 static int Open( vlc_object_t *p_this )
218 aout_instance_t * p_aout = (aout_instance_t *)p_this;
219 struct aout_sys_t * p_sys;
223 /* Allocate structure */
224 p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
227 msg_Err( p_aout, "out of memory" );
231 /* Get device name */
232 if( (psz_device = config_GetPsz( p_aout, "dspdev" )) == NULL )
234 msg_Err( p_aout, "no audio device given (maybe /dev/dsp ?)" );
239 /* Open the sound device */
240 p_sys->i_fd = open( psz_device, O_WRONLY );
242 if( p_sys->i_fd < 0 )
244 msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
249 p_aout->output.pf_play = Play;
251 if ( var_Type( p_aout, "audio-device" ) < 0 )
256 if ( var_Get( p_aout, "audio-device", &val ) < 0 )
258 /* Probe() has failed. */
263 if ( !strcmp( val.psz_string, N_("S/PDIF") ) )
265 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
267 else if ( !strcmp( val.psz_string, N_("5.1") ) )
269 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
270 p_aout->output.output.i_physical_channels
271 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
272 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT
275 else if ( !strcmp( val.psz_string, N_("2 Front 2 Rear") ) )
277 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
278 p_aout->output.output.i_physical_channels
279 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
280 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT;
282 else if ( !strcmp( val.psz_string, "Stereo" ) )
284 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
285 p_aout->output.output.i_physical_channels
286 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
288 else if ( !strcmp( val.psz_string, "Mono" ) )
290 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
291 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
293 free( val.psz_string );
295 /* Reset the DSP device */
296 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
298 msg_Err( p_aout, "cannot reset OSS audio device" );
299 close( p_sys->i_fd );
304 /* Set the output format */
305 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
307 int i_format = AFMT_AC3;
309 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
310 || i_format != AFMT_AC3 )
312 msg_Err( p_aout, "cannot reset OSS audio device" );
313 close( p_sys->i_fd );
318 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
319 p_aout->output.i_nb_samples = A52_FRAME_NB;
320 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
321 p_aout->output.output.i_frame_length = A52_FRAME_NB;
323 aout_VolumeNoneInit( p_aout );
326 if ( !AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
328 int i_format = AFMT_S16_NE;
329 int i_frame_size, i_fragments;
332 audio_buf_info audio_buf;
334 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
336 msg_Err( p_aout, "cannot set audio output format" );
337 close( p_sys->i_fd );
345 p_aout->output.output.i_format = VLC_FOURCC('u','8',' ',' ');
348 p_aout->output.output.i_format = VLC_FOURCC('s','8',' ',' ');
351 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','l');
354 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l');
357 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','b');
360 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','b');
363 msg_Err( p_aout, "OSS fell back to an unknown format (%d)",
365 close( p_sys->i_fd );
370 i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
372 /* Set the number of channels */
373 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) < 0 ||
374 i_nb_channels != aout_FormatNbChannels( &p_aout->output.output ) )
376 msg_Err( p_aout, "cannot set number of audio channels (%x)",
377 aout_FormatPrintChannels( &p_aout->output.output) );
378 close( p_sys->i_fd );
383 /* Set the output rate */
384 i_rate = p_aout->output.output.i_rate;
385 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
387 msg_Err( p_aout, "cannot set audio output rate (%i)",
388 p_aout->output.output.i_rate );
389 close( p_sys->i_fd );
394 if( i_rate != p_aout->output.output.i_rate )
396 p_aout->output.output.i_rate = i_rate;
399 /* Set the fragment size */
400 aout_FormatPrepare( &p_aout->output.output );
402 /* i_fragment = xxxxyyyy where: xxxx is fragtotal
403 * 1 << yyyy is fragsize */
405 i_frame_size = FRAME_SIZE * p_aout->output.output.i_bytes_per_frame;
406 while( i_frame_size >>= 1 )
410 i_fragments |= FRAME_COUNT << 16;
411 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
413 msg_Warn( p_aout, "cannot set fragment size (%.8x)", i_fragments );
416 if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf ) < 0 )
418 msg_Err( p_aout, "cannot get fragment size" );
419 close( p_sys->i_fd );
425 /* Number of fragments actually allocated */
426 p_aout->output.p_sys->i_fragstotal = audio_buf.fragstotal;
428 /* Maximum duration the soundcard's buffer can hold */
429 p_aout->output.p_sys->max_buffer_duration =
430 (mtime_t)audio_buf.fragstotal * audio_buf.fragsize * 1000000
431 / p_aout->output.output.i_bytes_per_frame
432 / p_aout->output.output.i_rate
433 * p_aout->output.output.i_frame_length;
435 p_aout->output.i_nb_samples = audio_buf.fragsize /
436 p_aout->output.output.i_bytes_per_frame;
439 aout_VolumeSoftInit( p_aout );
442 p_aout->output.p_sys->b_workaround_buggy_driver =
443 config_GetInt( p_aout, "oss-buggy" );
445 /* Create OSS thread and wait for its readiness. */
446 if( vlc_thread_create( p_aout, "aout", OSSThread,
447 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
449 msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
450 close( p_sys->i_fd );
458 /*****************************************************************************
459 * Play: nothing to do
460 *****************************************************************************/
461 static void Play( aout_instance_t *p_aout )
465 /*****************************************************************************
466 * Close: close the dsp audio device
467 *****************************************************************************/
468 static void Close( vlc_object_t * p_this )
470 aout_instance_t *p_aout = (aout_instance_t *)p_this;
471 struct aout_sys_t * p_sys = p_aout->output.p_sys;
473 p_aout->b_die = VLC_TRUE;
474 vlc_thread_join( p_aout );
475 p_aout->b_die = VLC_FALSE;
477 ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
478 close( p_sys->i_fd );
483 /*****************************************************************************
484 * BufferDuration: buffer status query
485 *****************************************************************************
486 * This function returns the duration in microseconds of the current buffer.
487 *****************************************************************************/
488 static mtime_t BufferDuration( aout_instance_t * p_aout )
490 struct aout_sys_t * p_sys = p_aout->output.p_sys;
491 audio_buf_info audio_buf;
494 /* Fill the audio_buf_info structure:
495 * - fragstotal: total number of fragments allocated
496 * - fragsize: size of a fragment in bytes
497 * - bytes: available space in bytes (includes partially used fragments)
498 * Note! 'bytes' could be more than fragments*fragsize */
499 ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
501 /* calculate number of available fragments (not partially used ones) */
502 i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
504 /* Return the fragment duration */
505 return (mtime_t)i_bytes * 1000000
506 / p_aout->output.output.i_bytes_per_frame
507 / p_aout->output.output.i_rate
508 * p_aout->output.output.i_frame_length;
511 /*****************************************************************************
512 * OSSThread: asynchronous thread used to DMA the data to the device
513 *****************************************************************************/
514 static int OSSThread( aout_instance_t * p_aout )
516 struct aout_sys_t * p_sys = p_aout->output.p_sys;
517 mtime_t next_date = 0;
519 while ( !p_aout->b_die )
521 aout_buffer_t * p_buffer = NULL;
525 if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
527 mtime_t buffered = BufferDuration( p_aout );
529 if( p_aout->output.p_sys->b_workaround_buggy_driver )
531 #define i_fragstotal p_aout->output.p_sys->i_fragstotal
532 /* Wait a bit - we don't want our buffer to be full */
533 if( buffered > (p_aout->output.p_sys->max_buffer_duration
534 / i_fragstotal * (i_fragstotal - 1)) )
536 msleep((p_aout->output.p_sys->max_buffer_duration
538 buffered = BufferDuration( p_aout );
545 /* This is the _real_ presentation date */
546 next_date = mdate() + buffered;
550 /* Give a hint to the audio output about our drift, but
551 * not too much because we want to make it happy with our
552 * nicely calculated dates. */
553 next_date = ( (next_date * 7) + (mdate() + buffered) ) / 8;
556 /* Next buffer will be played at mdate()+buffered */
557 p_buffer = aout_OutputNextBuffer( p_aout, next_date, VLC_FALSE );
561 /* emu10k1 driver does not report Buffer Duration correctly in
562 * passthrough mode so we have to cheat */
569 mtime_t delay = next_date - mdate();
570 if( delay > AOUT_PTS_TOLERANCE )
576 while( !p_aout->b_die && ! ( p_buffer =
577 aout_OutputNextBuffer( p_aout, next_date, VLC_TRUE ) ) )
584 if ( p_buffer != NULL )
586 p_bytes = p_buffer->p_buffer;
587 i_size = p_buffer->i_nb_bytes;
588 /* This is theoretical ... we'll see next iteration whether
590 next_date += p_buffer->end_date - p_buffer->start_date;
594 i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
595 * p_aout->output.output.i_bytes_per_frame;
596 p_bytes = malloc( i_size );
597 memset( p_bytes, 0, i_size );
601 i_tmp = write( p_sys->i_fd, p_bytes, i_size );
605 msg_Err( p_aout, "write failed (%s)", strerror(errno) );
608 if ( p_buffer != NULL )
610 aout_BufferFree( p_buffer );