1 /*****************************************************************************
2 * oss.c : OSS /dev/dsp module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2002 VideoLAN
5 * $Id: oss.c,v 1.37 2002/12/07 23:50:30 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_HASCHOICE );
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 );
209 val.b_bool = VLC_TRUE;
210 var_Set( p_aout, "intf-change", val );
213 /*****************************************************************************
214 * Open: open the audio device (the digital sound processor)
215 *****************************************************************************
216 * This function opens the dsp as a usual non-blocking write-only file, and
217 * modifies the p_aout->p_sys->i_fd with the file's descriptor.
218 *****************************************************************************/
219 static int Open( vlc_object_t *p_this )
221 aout_instance_t * p_aout = (aout_instance_t *)p_this;
222 struct aout_sys_t * p_sys;
226 /* Allocate structure */
227 p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
230 msg_Err( p_aout, "out of memory" );
234 /* Get device name */
235 if( (psz_device = config_GetPsz( p_aout, "dspdev" )) == NULL )
237 msg_Err( p_aout, "no audio device given (maybe /dev/dsp ?)" );
242 /* Open the sound device */
243 p_sys->i_fd = open( psz_device, O_WRONLY );
245 if( p_sys->i_fd < 0 )
247 msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
252 p_aout->output.pf_play = Play;
254 if ( var_Type( p_aout, "audio-device" ) < 0 )
259 if ( var_Get( p_aout, "audio-device", &val ) < 0 )
261 /* Probe() has failed. */
266 if ( !strcmp( val.psz_string, N_("S/PDIF") ) )
268 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
270 else if ( !strcmp( val.psz_string, N_("5.1") ) )
272 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
273 p_aout->output.output.i_physical_channels
274 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
275 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT
278 else if ( !strcmp( val.psz_string, N_("2 Front 2 Rear") ) )
280 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
281 p_aout->output.output.i_physical_channels
282 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
283 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT;
285 else if ( !strcmp( val.psz_string, "Stereo" ) )
287 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
288 p_aout->output.output.i_physical_channels
289 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
291 else if ( !strcmp( val.psz_string, "Mono" ) )
293 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
294 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
296 free( val.psz_string );
298 /* Reset the DSP device */
299 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
301 msg_Err( p_aout, "cannot reset OSS audio device" );
302 close( p_sys->i_fd );
307 /* Set the output format */
308 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
310 int i_format = AFMT_AC3;
312 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
313 || i_format != AFMT_AC3 )
315 msg_Err( p_aout, "cannot reset OSS audio device" );
316 close( p_sys->i_fd );
321 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
322 p_aout->output.i_nb_samples = A52_FRAME_NB;
323 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
324 p_aout->output.output.i_frame_length = A52_FRAME_NB;
326 aout_VolumeNoneInit( p_aout );
329 if ( !AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
331 unsigned int i_format = AFMT_S16_NE;
332 unsigned int i_frame_size, i_fragments;
334 unsigned int i_nb_channels;
335 audio_buf_info audio_buf;
337 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
339 msg_Err( p_aout, "cannot set audio output format" );
340 close( p_sys->i_fd );
348 p_aout->output.output.i_format = VLC_FOURCC('u','8',' ',' ');
351 p_aout->output.output.i_format = VLC_FOURCC('s','8',' ',' ');
354 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','l');
357 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l');
360 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','b');
363 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','b');
366 msg_Err( p_aout, "OSS fell back to an unknown format (%d)",
368 close( p_sys->i_fd );
373 i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
375 /* Set the number of channels */
376 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) < 0 ||
377 i_nb_channels != aout_FormatNbChannels( &p_aout->output.output ) )
379 msg_Err( p_aout, "cannot set number of audio channels (%x)",
380 aout_FormatPrintChannels( &p_aout->output.output) );
381 close( p_sys->i_fd );
386 /* Set the output rate */
387 i_rate = p_aout->output.output.i_rate;
388 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
390 msg_Err( p_aout, "cannot set audio output rate (%i)",
391 p_aout->output.output.i_rate );
392 close( p_sys->i_fd );
397 if( i_rate != p_aout->output.output.i_rate )
399 p_aout->output.output.i_rate = i_rate;
402 /* Set the fragment size */
403 aout_FormatPrepare( &p_aout->output.output );
405 /* i_fragment = xxxxyyyy where: xxxx is fragtotal
406 * 1 << yyyy is fragsize */
408 i_frame_size = FRAME_SIZE * p_aout->output.output.i_bytes_per_frame;
409 while( i_frame_size >>= 1 )
413 i_fragments |= FRAME_COUNT << 16;
414 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
416 msg_Warn( p_aout, "cannot set fragment size (%.8x)", i_fragments );
419 if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf ) < 0 )
421 msg_Err( p_aout, "cannot get fragment size" );
422 close( p_sys->i_fd );
428 /* Number of fragments actually allocated */
429 p_aout->output.p_sys->i_fragstotal = audio_buf.fragstotal;
431 /* Maximum duration the soundcard's buffer can hold */
432 p_aout->output.p_sys->max_buffer_duration =
433 (mtime_t)audio_buf.fragstotal * audio_buf.fragsize * 1000000
434 / p_aout->output.output.i_bytes_per_frame
435 / p_aout->output.output.i_rate
436 * p_aout->output.output.i_frame_length;
438 p_aout->output.i_nb_samples = audio_buf.fragsize /
439 p_aout->output.output.i_bytes_per_frame;
442 aout_VolumeSoftInit( p_aout );
445 p_aout->output.p_sys->b_workaround_buggy_driver =
446 config_GetInt( p_aout, "oss-buggy" );
448 /* Create OSS thread and wait for its readiness. */
449 if( vlc_thread_create( p_aout, "aout", OSSThread,
450 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
452 msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
453 close( p_sys->i_fd );
461 /*****************************************************************************
462 * Play: nothing to do
463 *****************************************************************************/
464 static void Play( aout_instance_t *p_aout )
468 /*****************************************************************************
469 * Close: close the dsp audio device
470 *****************************************************************************/
471 static void Close( vlc_object_t * p_this )
473 aout_instance_t *p_aout = (aout_instance_t *)p_this;
474 struct aout_sys_t * p_sys = p_aout->output.p_sys;
476 p_aout->b_die = VLC_TRUE;
477 vlc_thread_join( p_aout );
478 p_aout->b_die = VLC_FALSE;
480 ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
481 close( p_sys->i_fd );
486 /*****************************************************************************
487 * BufferDuration: buffer status query
488 *****************************************************************************
489 * This function returns the duration in microseconds of the current buffer.
490 *****************************************************************************/
491 static mtime_t BufferDuration( aout_instance_t * p_aout )
493 struct aout_sys_t * p_sys = p_aout->output.p_sys;
494 audio_buf_info audio_buf;
497 /* Fill the audio_buf_info structure:
498 * - fragstotal: total number of fragments allocated
499 * - fragsize: size of a fragment in bytes
500 * - bytes: available space in bytes (includes partially used fragments)
501 * Note! 'bytes' could be more than fragments*fragsize */
502 ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
504 /* calculate number of available fragments (not partially used ones) */
505 i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
507 /* Return the fragment duration */
508 return (mtime_t)i_bytes * 1000000
509 / p_aout->output.output.i_bytes_per_frame
510 / p_aout->output.output.i_rate
511 * p_aout->output.output.i_frame_length;
514 /*****************************************************************************
515 * OSSThread: asynchronous thread used to DMA the data to the device
516 *****************************************************************************/
517 static int OSSThread( aout_instance_t * p_aout )
519 struct aout_sys_t * p_sys = p_aout->output.p_sys;
520 mtime_t next_date = 0;
522 while ( !p_aout->b_die )
524 aout_buffer_t * p_buffer = NULL;
528 if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
530 mtime_t buffered = BufferDuration( p_aout );
532 if( p_aout->output.p_sys->b_workaround_buggy_driver )
534 #define i_fragstotal p_aout->output.p_sys->i_fragstotal
535 /* Wait a bit - we don't want our buffer to be full */
536 if( buffered > (p_aout->output.p_sys->max_buffer_duration
537 / i_fragstotal * (i_fragstotal - 1)) )
539 msleep((p_aout->output.p_sys->max_buffer_duration
541 buffered = BufferDuration( p_aout );
546 /* Next buffer will be played at mdate() + buffered */
547 p_buffer = aout_OutputNextBuffer( p_aout, mdate() + buffered,
550 if( p_buffer == NULL &&
551 buffered > ( p_aout->output.p_sys->max_buffer_duration
552 / p_aout->output.p_sys->i_fragstotal ) )
554 /* If we have at least a fragment full, then we can wait a
555 * little and retry to get a new audio buffer instead of
556 * playing a blank sample */
557 msleep( ( p_aout->output.p_sys->max_buffer_duration
558 / p_aout->output.p_sys->i_fragstotal / 2 ) );
564 /* emu10k1 driver does not report Buffer Duration correctly in
565 * passthrough mode so we have to cheat */
572 mtime_t delay = next_date - mdate();
573 if( delay > AOUT_PTS_TOLERANCE )
579 while( !p_aout->b_die && ! ( p_buffer =
580 aout_OutputNextBuffer( p_aout, next_date, VLC_TRUE ) ) )
587 if ( p_buffer != NULL )
589 p_bytes = p_buffer->p_buffer;
590 i_size = p_buffer->i_nb_bytes;
591 /* This is theoretical ... we'll see next iteration whether
593 next_date += p_buffer->end_date - p_buffer->start_date;
597 i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
598 * p_aout->output.output.i_bytes_per_frame;
599 p_bytes = malloc( i_size );
600 memset( p_bytes, 0, i_size );
604 i_tmp = write( p_sys->i_fd, p_bytes, i_size );
608 msg_Err( p_aout, "write failed (%s)", strerror(errno) );
611 if ( p_buffer != NULL )
613 aout_BufferFree( p_buffer );