1 /*****************************************************************************
2 * oss.c : OSS /dev/dsp module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2002 VideoLAN
5 * $Id: oss.c,v 1.45 2003/01/14 22:44:29 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 /*****************************************************************************
62 * aout_sys_t: OSS audio output method descriptor
63 *****************************************************************************
64 * This structure is part of the audio output thread descriptor.
65 * It describes the dsp specific properties of an audio device.
66 *****************************************************************************/
70 int b_workaround_buggy_driver;
72 mtime_t max_buffer_duration;
75 /* This must be a power of 2. */
76 #define FRAME_SIZE 1024
79 /*****************************************************************************
81 *****************************************************************************/
82 static int Open ( vlc_object_t * );
83 static void Close ( vlc_object_t * );
85 static void Play ( aout_instance_t * );
86 static int OSSThread ( aout_instance_t * );
88 static mtime_t BufferDuration( aout_instance_t * p_aout );
90 /*****************************************************************************
92 *****************************************************************************/
93 #define BUGGY_TEXT N_("Try to work around buggy OSS drivers")
94 #define BUGGY_LONGTEXT N_( \
95 "Some buggy OSS drivers just don't like when their internal buffers " \
96 "are completely filled (the sound gets heavily hashed). If you have one " \
97 "of these drivers, then you need to enable this option." )
99 #define SPDIF_TEXT N_("Try to use S/PDIF output")
100 #define SPDIF_LONGTEXT N_( \
101 "Sometimes we attempt to use the S/PDIF output, even if nothing is " \
102 "connected to it. Un-checking this option disables this behaviour, " \
103 "and permanently selects analog PCM output." )
106 add_category_hint( N_("OSS"), NULL );
107 add_file( "dspdev", "/dev/dsp", aout_FindAndRestart,
108 N_("OSS dsp device"), NULL );
109 add_bool( "oss-buggy", 0, NULL, BUGGY_TEXT, BUGGY_LONGTEXT );
110 add_bool( "spdif", 1, NULL, SPDIF_TEXT, SPDIF_LONGTEXT );
111 set_description( _("Linux OSS /dev/dsp module") );
112 set_capability( "audio output", 100 );
113 add_shortcut( "oss" );
114 set_callbacks( Open, Close );
117 /*****************************************************************************
118 * Probe: probe the audio device for available formats and channels
119 *****************************************************************************/
120 static void Probe( aout_instance_t * p_aout )
122 struct aout_sys_t * p_sys = p_aout->output.p_sys;
124 int i_format, i_nb_channels;
126 var_Create( p_aout, "audio-device", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
128 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
130 msg_Err( p_aout, "cannot reset OSS audio device" );
131 var_Destroy( p_aout, "audio-device" );
135 if ( config_GetInt( p_aout, "spdif" )
136 && AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
140 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) >= 0
141 && i_format == AFMT_AC3 )
143 val.psz_string = N_("A/52 over S/PDIF");
144 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
148 /* Go to PCM mode. */
149 i_format = AFMT_S16_NE;
150 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
151 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
156 #ifdef SNDCTL_DSP_GETCHANNELMASK
157 if ( aout_FormatNbChannels( &p_aout->output.output ) > 2 )
159 /* Check that the device supports this. */
162 if ( ioctl( p_sys->i_fd, SNDCTL_DSP_GETCHANNELMASK,
165 if ( !(i_chanmask & DSP_BIND_FRONT) )
167 msg_Err( p_aout, "No front channels ! (%x)",
172 if ( (i_chanmask & (DSP_BIND_SURR | DSP_BIND_CENTER_LFE))
173 && (p_aout->output.output.i_physical_channels ==
174 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
175 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
178 val.psz_string = N_("5.1");
179 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
182 if ( (i_chanmask & DSP_BIND_SURR)
183 && (p_aout->output.output.i_physical_channels &
184 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
185 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT)) )
187 val.psz_string = N_("2 Front 2 Rear");
188 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
194 /* Test for stereo. */
196 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
197 && i_nb_channels == 2 )
199 val.psz_string = N_("Stereo");
200 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
204 i_format = AFMT_S16_NE;
205 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
206 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
208 msg_Err( p_aout, "cannot reset OSS audio device" );
209 var_Destroy( p_aout, "audio-device" );
215 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
216 && i_nb_channels == 1 )
218 val.psz_string = N_("Mono");
219 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
223 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart,
227 /*****************************************************************************
228 * Open: open the audio device (the digital sound processor)
229 *****************************************************************************
230 * This function opens the dsp as a usual non-blocking write-only file, and
231 * modifies the p_aout->p_sys->i_fd with the file's descriptor.
232 *****************************************************************************/
233 static int Open( vlc_object_t *p_this )
235 aout_instance_t * p_aout = (aout_instance_t *)p_this;
236 struct aout_sys_t * p_sys;
240 /* Allocate structure */
241 p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
244 msg_Err( p_aout, "out of memory" );
248 /* Get device name */
249 if( (psz_device = config_GetPsz( p_aout, "dspdev" )) == NULL )
251 msg_Err( p_aout, "no audio device given (maybe /dev/dsp ?)" );
256 /* Open the sound device */
257 p_sys->i_fd = open( psz_device, O_WRONLY );
258 if( p_sys->i_fd < 0 )
260 msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
266 p_aout->output.pf_play = Play;
268 if ( var_Type( p_aout, "audio-device" ) == 0 )
273 if ( var_Get( p_aout, "audio-device", &val ) < 0 )
275 /* Probe() has failed. */
280 if ( !strcmp( val.psz_string, N_("A/52 over S/PDIF") ) )
282 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
284 else if ( !strcmp( val.psz_string, N_("5.1") ) )
286 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
287 p_aout->output.output.i_physical_channels
288 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
289 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT
292 else if ( !strcmp( val.psz_string, N_("2 Front 2 Rear") ) )
294 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
295 p_aout->output.output.i_physical_channels
296 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
297 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT;
299 else if ( !strcmp( val.psz_string, N_("Stereo") ) )
301 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
302 p_aout->output.output.i_physical_channels
303 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
305 else if ( !strcmp( val.psz_string, N_("Mono") ) )
307 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
308 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
312 /* This should not happen ! */
313 msg_Err( p_aout, "internal: can't find audio-device (%s)",
318 free( val.psz_string );
320 val.b_bool = VLC_TRUE;
321 var_Set( p_aout, "intf-change", val );
323 /* Reset the DSP device */
324 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
326 msg_Err( p_aout, "cannot reset OSS audio device" );
327 close( p_sys->i_fd );
332 /* Set the output format */
333 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
335 int i_format = AFMT_AC3;
337 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
338 || i_format != AFMT_AC3 )
340 msg_Err( p_aout, "cannot reset OSS audio device" );
341 close( p_sys->i_fd );
346 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
347 p_aout->output.i_nb_samples = A52_FRAME_NB;
348 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
349 p_aout->output.output.i_frame_length = A52_FRAME_NB;
351 aout_VolumeNoneInit( p_aout );
354 if ( !AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
356 unsigned int i_format = AFMT_S16_NE;
357 unsigned int i_frame_size, i_fragments;
359 unsigned int i_nb_channels;
360 audio_buf_info audio_buf;
362 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
364 msg_Err( p_aout, "cannot set audio output format" );
365 close( p_sys->i_fd );
373 p_aout->output.output.i_format = VLC_FOURCC('u','8',' ',' ');
376 p_aout->output.output.i_format = VLC_FOURCC('s','8',' ',' ');
379 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','l');
382 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l');
385 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','b');
388 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','b');
391 msg_Err( p_aout, "OSS fell back to an unknown format (%d)",
393 close( p_sys->i_fd );
398 i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
400 /* Set the number of channels */
401 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) < 0 ||
402 i_nb_channels != aout_FormatNbChannels( &p_aout->output.output ) )
404 msg_Err( p_aout, "cannot set number of audio channels (%s)",
405 aout_FormatPrintChannels( &p_aout->output.output) );
406 close( p_sys->i_fd );
411 /* Set the output rate */
412 i_rate = p_aout->output.output.i_rate;
413 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
415 msg_Err( p_aout, "cannot set audio output rate (%i)",
416 p_aout->output.output.i_rate );
417 close( p_sys->i_fd );
422 if( i_rate != p_aout->output.output.i_rate )
424 p_aout->output.output.i_rate = i_rate;
427 /* Set the fragment size */
428 aout_FormatPrepare( &p_aout->output.output );
430 /* i_fragment = xxxxyyyy where: xxxx is fragtotal
431 * 1 << yyyy is fragsize */
433 i_frame_size = FRAME_SIZE * p_aout->output.output.i_bytes_per_frame;
434 while( i_frame_size >>= 1 )
438 i_fragments |= FRAME_COUNT << 16;
439 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
441 msg_Warn( p_aout, "cannot set fragment size (%.8x)", i_fragments );
444 if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf ) < 0 )
446 msg_Err( p_aout, "cannot get fragment size" );
447 close( p_sys->i_fd );
453 /* Number of fragments actually allocated */
454 p_aout->output.p_sys->i_fragstotal = audio_buf.fragstotal;
456 /* Maximum duration the soundcard's buffer can hold */
457 p_aout->output.p_sys->max_buffer_duration =
458 (mtime_t)audio_buf.fragstotal * audio_buf.fragsize * 1000000
459 / p_aout->output.output.i_bytes_per_frame
460 / p_aout->output.output.i_rate
461 * p_aout->output.output.i_frame_length;
463 p_aout->output.i_nb_samples = audio_buf.fragsize /
464 p_aout->output.output.i_bytes_per_frame;
467 aout_VolumeSoftInit( p_aout );
470 p_aout->output.p_sys->b_workaround_buggy_driver =
471 config_GetInt( p_aout, "oss-buggy" );
473 /* Create OSS thread and wait for its readiness. */
474 if( vlc_thread_create( p_aout, "aout", OSSThread,
475 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
477 msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
478 close( p_sys->i_fd );
486 /*****************************************************************************
487 * Play: nothing to do
488 *****************************************************************************/
489 static void Play( aout_instance_t *p_aout )
493 /*****************************************************************************
494 * Close: close the dsp audio device
495 *****************************************************************************/
496 static void Close( vlc_object_t * p_this )
498 aout_instance_t *p_aout = (aout_instance_t *)p_this;
499 struct aout_sys_t * p_sys = p_aout->output.p_sys;
501 p_aout->b_die = VLC_TRUE;
502 vlc_thread_join( p_aout );
503 p_aout->b_die = VLC_FALSE;
505 ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
506 close( p_sys->i_fd );
511 /*****************************************************************************
512 * BufferDuration: buffer status query
513 *****************************************************************************
514 * This function returns the duration in microseconds of the current buffer.
515 *****************************************************************************/
516 static mtime_t BufferDuration( aout_instance_t * p_aout )
518 struct aout_sys_t * p_sys = p_aout->output.p_sys;
519 audio_buf_info audio_buf;
522 /* Fill the audio_buf_info structure:
523 * - fragstotal: total number of fragments allocated
524 * - fragsize: size of a fragment in bytes
525 * - bytes: available space in bytes (includes partially used fragments)
526 * Note! 'bytes' could be more than fragments*fragsize */
527 ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
529 /* calculate number of available fragments (not partially used ones) */
530 i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
532 /* Return the fragment duration */
533 return (mtime_t)i_bytes * 1000000
534 / p_aout->output.output.i_bytes_per_frame
535 / p_aout->output.output.i_rate
536 * p_aout->output.output.i_frame_length;
539 /*****************************************************************************
540 * OSSThread: asynchronous thread used to DMA the data to the device
541 *****************************************************************************/
542 static int OSSThread( aout_instance_t * p_aout )
544 struct aout_sys_t * p_sys = p_aout->output.p_sys;
545 mtime_t next_date = 0;
547 while ( !p_aout->b_die )
549 aout_buffer_t * p_buffer = NULL;
553 if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
555 mtime_t buffered = BufferDuration( p_aout );
557 if( p_aout->output.p_sys->b_workaround_buggy_driver )
559 #define i_fragstotal p_aout->output.p_sys->i_fragstotal
560 /* Wait a bit - we don't want our buffer to be full */
561 if( buffered > (p_aout->output.p_sys->max_buffer_duration
562 / i_fragstotal * (i_fragstotal - 1)) )
564 msleep((p_aout->output.p_sys->max_buffer_duration
566 buffered = BufferDuration( p_aout );
571 /* Next buffer will be played at mdate() + buffered */
572 p_buffer = aout_OutputNextBuffer( p_aout, mdate() + buffered,
575 if( p_buffer == NULL &&
576 buffered > ( p_aout->output.p_sys->max_buffer_duration
577 / p_aout->output.p_sys->i_fragstotal ) )
579 /* If we have at least a fragment full, then we can wait a
580 * little and retry to get a new audio buffer instead of
581 * playing a blank sample */
582 msleep( ( p_aout->output.p_sys->max_buffer_duration
583 / p_aout->output.p_sys->i_fragstotal / 2 ) );
589 /* emu10k1 driver does not report Buffer Duration correctly in
590 * passthrough mode so we have to cheat */
597 mtime_t delay = next_date - mdate();
598 if( delay > AOUT_PTS_TOLERANCE )
604 while( !p_aout->b_die && ! ( p_buffer =
605 aout_OutputNextBuffer( p_aout, next_date, VLC_TRUE ) ) )
612 if ( p_buffer != NULL )
614 p_bytes = p_buffer->p_buffer;
615 i_size = p_buffer->i_nb_bytes;
616 /* This is theoretical ... we'll see next iteration whether
618 next_date += p_buffer->end_date - p_buffer->start_date;
622 i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
623 * p_aout->output.output.i_bytes_per_frame;
624 p_bytes = malloc( i_size );
625 memset( p_bytes, 0, i_size );
629 i_tmp = write( p_sys->i_fd, p_bytes, i_size );
633 msg_Err( p_aout, "write failed (%s)", strerror(errno) );
636 if ( p_buffer != NULL )
638 aout_BufferFree( p_buffer );