1 /*****************************************************************************
2 * oss.c : OSS /dev/dsp module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2002 VideoLAN
5 * $Id: oss.c,v 1.42 2003/01/07 14:58:33 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." )
94 #define SPDIF_TEXT N_("Try to use S/PDIF output")
95 #define SPDIF_LONGTEXT N_( \
96 "Sometimes we attempt to use the S/PDIF output, even if nothing is " \
97 "connected to it. Un-checking this option disables this behaviour, " \
98 "and permanently selects analog PCM output." )
101 add_category_hint( N_("OSS"), NULL );
102 add_file( "dspdev", "/dev/dsp", aout_FindAndRestart,
103 N_("OSS dsp device"), NULL );
104 add_bool( "oss-buggy", 0, NULL, BUGGY_TEXT, BUGGY_LONGTEXT );
105 add_bool( "spdif", 1, NULL, SPDIF_TEXT, SPDIF_LONGTEXT );
106 set_description( _("Linux OSS /dev/dsp module") );
107 set_capability( "audio output", 100 );
108 add_shortcut( "oss" );
109 set_callbacks( Open, Close );
112 /*****************************************************************************
113 * Probe: probe the audio device for available formats and channels
114 *****************************************************************************/
115 static void Probe( aout_instance_t * p_aout )
117 struct aout_sys_t * p_sys = p_aout->output.p_sys;
119 int i_format, i_nb_channels;
121 var_Create( p_aout, "audio-device", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
123 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
125 msg_Err( p_aout, "cannot reset OSS audio device" );
126 var_Destroy( p_aout, "audio-device" );
130 if ( config_GetInt( p_aout, "spdif" )
131 && AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
135 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) >= 0
136 && i_format == AFMT_AC3 )
138 val.psz_string = N_("A/52 over S/PDIF");
139 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
143 /* Go to PCM mode. */
144 i_format = AFMT_S16_NE;
145 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
146 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
151 #ifdef SNDCTL_DSP_GETCHANNELMASK
152 if ( aout_FormatNbChannels( &p_aout->output.output ) > 2 )
154 /* Check that the device supports this. */
157 if ( ioctl( p_sys->i_fd, SNDCTL_DSP_GETCHANNELMASK,
160 if ( !(i_chanmask & DSP_BIND_FRONT) )
162 msg_Err( p_aout, "No front channels ! (%x)",
167 if ( (i_chanmask & (DSP_BIND_SURR | DSP_BIND_CENTER_LFE))
168 && (p_aout->output.output.i_physical_channels ==
169 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
170 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
173 val.psz_string = N_("5.1");
174 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
177 if ( (i_chanmask & DSP_BIND_SURR)
178 && (p_aout->output.output.i_physical_channels &
179 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
180 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT)) )
182 val.psz_string = N_("2 Front 2 Rear");
183 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
189 /* Test for stereo. */
191 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
192 && i_nb_channels == 2 )
194 val.psz_string = N_("Stereo");
195 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
199 i_format = AFMT_S16_NE;
200 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
201 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
203 msg_Err( p_aout, "cannot reset OSS audio device" );
204 var_Destroy( p_aout, "audio-device" );
210 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
211 && i_nb_channels == 1 )
213 val.psz_string = N_("Mono");
214 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
217 val.b_bool = VLC_TRUE;
218 var_Set( p_aout, "intf-change", val );
221 /*****************************************************************************
222 * Open: open the audio device (the digital sound processor)
223 *****************************************************************************
224 * This function opens the dsp as a usual non-blocking write-only file, and
225 * modifies the p_aout->p_sys->i_fd with the file's descriptor.
226 *****************************************************************************/
227 static int Open( vlc_object_t *p_this )
229 aout_instance_t * p_aout = (aout_instance_t *)p_this;
230 struct aout_sys_t * p_sys;
234 /* Allocate structure */
235 p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
238 msg_Err( p_aout, "out of memory" );
242 /* Get device name */
243 if( (psz_device = config_GetPsz( p_aout, "dspdev" )) == NULL )
245 msg_Err( p_aout, "no audio device given (maybe /dev/dsp ?)" );
250 /* Open the sound device */
251 p_sys->i_fd = open( psz_device, O_WRONLY );
252 if( p_sys->i_fd < 0 )
254 msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
260 p_aout->output.pf_play = Play;
262 if ( var_Type( p_aout, "audio-device" ) == 0 )
267 if ( var_Get( p_aout, "audio-device", &val ) < 0 )
269 /* Probe() has failed. */
274 if ( !strcmp( val.psz_string, N_("A/52 over S/PDIF") ) )
276 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
278 else if ( !strcmp( val.psz_string, N_("5.1") ) )
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 | AOUT_CHAN_CENTER
283 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT
286 else if ( !strcmp( val.psz_string, N_("2 Front 2 Rear") ) )
288 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
289 p_aout->output.output.i_physical_channels
290 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
291 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT;
293 else if ( !strcmp( val.psz_string, "Stereo" ) )
295 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
296 p_aout->output.output.i_physical_channels
297 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
299 else if ( !strcmp( val.psz_string, "Mono" ) )
301 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
302 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
304 free( val.psz_string );
306 /* Reset the DSP device */
307 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
309 msg_Err( p_aout, "cannot reset OSS audio device" );
310 close( p_sys->i_fd );
315 /* Set the output format */
316 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
318 int i_format = AFMT_AC3;
320 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
321 || i_format != AFMT_AC3 )
323 msg_Err( p_aout, "cannot reset OSS audio device" );
324 close( p_sys->i_fd );
329 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
330 p_aout->output.i_nb_samples = A52_FRAME_NB;
331 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
332 p_aout->output.output.i_frame_length = A52_FRAME_NB;
334 aout_VolumeNoneInit( p_aout );
337 if ( !AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
339 unsigned int i_format = AFMT_S16_NE;
340 unsigned int i_frame_size, i_fragments;
342 unsigned int i_nb_channels;
343 audio_buf_info audio_buf;
345 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
347 msg_Err( p_aout, "cannot set audio output format" );
348 close( p_sys->i_fd );
356 p_aout->output.output.i_format = VLC_FOURCC('u','8',' ',' ');
359 p_aout->output.output.i_format = VLC_FOURCC('s','8',' ',' ');
362 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','l');
365 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l');
368 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','b');
371 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','b');
374 msg_Err( p_aout, "OSS fell back to an unknown format (%d)",
376 close( p_sys->i_fd );
381 i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
383 /* Set the number of channels */
384 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) < 0 ||
385 i_nb_channels != aout_FormatNbChannels( &p_aout->output.output ) )
387 msg_Err( p_aout, "cannot set number of audio channels (%s)",
388 aout_FormatPrintChannels( &p_aout->output.output) );
389 close( p_sys->i_fd );
394 /* Set the output rate */
395 i_rate = p_aout->output.output.i_rate;
396 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
398 msg_Err( p_aout, "cannot set audio output rate (%i)",
399 p_aout->output.output.i_rate );
400 close( p_sys->i_fd );
405 if( i_rate != p_aout->output.output.i_rate )
407 p_aout->output.output.i_rate = i_rate;
410 /* Set the fragment size */
411 aout_FormatPrepare( &p_aout->output.output );
413 /* i_fragment = xxxxyyyy where: xxxx is fragtotal
414 * 1 << yyyy is fragsize */
416 i_frame_size = FRAME_SIZE * p_aout->output.output.i_bytes_per_frame;
417 while( i_frame_size >>= 1 )
421 i_fragments |= FRAME_COUNT << 16;
422 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
424 msg_Warn( p_aout, "cannot set fragment size (%.8x)", i_fragments );
427 if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf ) < 0 )
429 msg_Err( p_aout, "cannot get fragment size" );
430 close( p_sys->i_fd );
436 /* Number of fragments actually allocated */
437 p_aout->output.p_sys->i_fragstotal = audio_buf.fragstotal;
439 /* Maximum duration the soundcard's buffer can hold */
440 p_aout->output.p_sys->max_buffer_duration =
441 (mtime_t)audio_buf.fragstotal * audio_buf.fragsize * 1000000
442 / p_aout->output.output.i_bytes_per_frame
443 / p_aout->output.output.i_rate
444 * p_aout->output.output.i_frame_length;
446 p_aout->output.i_nb_samples = audio_buf.fragsize /
447 p_aout->output.output.i_bytes_per_frame;
450 aout_VolumeSoftInit( p_aout );
453 p_aout->output.p_sys->b_workaround_buggy_driver =
454 config_GetInt( p_aout, "oss-buggy" );
456 /* Create OSS thread and wait for its readiness. */
457 if( vlc_thread_create( p_aout, "aout", OSSThread,
458 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
460 msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
461 close( p_sys->i_fd );
469 /*****************************************************************************
470 * Play: nothing to do
471 *****************************************************************************/
472 static void Play( aout_instance_t *p_aout )
476 /*****************************************************************************
477 * Close: close the dsp audio device
478 *****************************************************************************/
479 static void Close( vlc_object_t * p_this )
481 aout_instance_t *p_aout = (aout_instance_t *)p_this;
482 struct aout_sys_t * p_sys = p_aout->output.p_sys;
484 p_aout->b_die = VLC_TRUE;
485 vlc_thread_join( p_aout );
486 p_aout->b_die = VLC_FALSE;
488 ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
489 close( p_sys->i_fd );
494 /*****************************************************************************
495 * BufferDuration: buffer status query
496 *****************************************************************************
497 * This function returns the duration in microseconds of the current buffer.
498 *****************************************************************************/
499 static mtime_t BufferDuration( aout_instance_t * p_aout )
501 struct aout_sys_t * p_sys = p_aout->output.p_sys;
502 audio_buf_info audio_buf;
505 /* Fill the audio_buf_info structure:
506 * - fragstotal: total number of fragments allocated
507 * - fragsize: size of a fragment in bytes
508 * - bytes: available space in bytes (includes partially used fragments)
509 * Note! 'bytes' could be more than fragments*fragsize */
510 ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
512 /* calculate number of available fragments (not partially used ones) */
513 i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
515 /* Return the fragment duration */
516 return (mtime_t)i_bytes * 1000000
517 / p_aout->output.output.i_bytes_per_frame
518 / p_aout->output.output.i_rate
519 * p_aout->output.output.i_frame_length;
522 /*****************************************************************************
523 * OSSThread: asynchronous thread used to DMA the data to the device
524 *****************************************************************************/
525 static int OSSThread( aout_instance_t * p_aout )
527 struct aout_sys_t * p_sys = p_aout->output.p_sys;
528 mtime_t next_date = 0;
530 while ( !p_aout->b_die )
532 aout_buffer_t * p_buffer = NULL;
536 if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
538 mtime_t buffered = BufferDuration( p_aout );
540 if( p_aout->output.p_sys->b_workaround_buggy_driver )
542 #define i_fragstotal p_aout->output.p_sys->i_fragstotal
543 /* Wait a bit - we don't want our buffer to be full */
544 if( buffered > (p_aout->output.p_sys->max_buffer_duration
545 / i_fragstotal * (i_fragstotal - 1)) )
547 msleep((p_aout->output.p_sys->max_buffer_duration
549 buffered = BufferDuration( p_aout );
554 /* Next buffer will be played at mdate() + buffered */
555 p_buffer = aout_OutputNextBuffer( p_aout, mdate() + buffered,
558 if( p_buffer == NULL &&
559 buffered > ( p_aout->output.p_sys->max_buffer_duration
560 / p_aout->output.p_sys->i_fragstotal ) )
562 /* If we have at least a fragment full, then we can wait a
563 * little and retry to get a new audio buffer instead of
564 * playing a blank sample */
565 msleep( ( p_aout->output.p_sys->max_buffer_duration
566 / p_aout->output.p_sys->i_fragstotal / 2 ) );
572 /* emu10k1 driver does not report Buffer Duration correctly in
573 * passthrough mode so we have to cheat */
580 mtime_t delay = next_date - mdate();
581 if( delay > AOUT_PTS_TOLERANCE )
587 while( !p_aout->b_die && ! ( p_buffer =
588 aout_OutputNextBuffer( p_aout, next_date, VLC_TRUE ) ) )
595 if ( p_buffer != NULL )
597 p_bytes = p_buffer->p_buffer;
598 i_size = p_buffer->i_nb_bytes;
599 /* This is theoretical ... we'll see next iteration whether
601 next_date += p_buffer->end_date - p_buffer->start_date;
605 i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
606 * p_aout->output.output.i_bytes_per_frame;
607 p_bytes = malloc( i_size );
608 memset( p_bytes, 0, i_size );
612 i_tmp = write( p_sys->i_fd, p_bytes, i_size );
616 msg_Err( p_aout, "write failed (%s)", strerror(errno) );
619 if ( p_buffer != NULL )
621 aout_BufferFree( p_buffer );