1 /*****************************************************************************
2 * oss.c : OSS /dev/dsp module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2002 VideoLAN
5 * $Id: oss.c,v 1.54 2003/03/10 10:41:22 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 /* Patches for ignorant OSS versions */
58 # define AFMT_AC3 0x00000400 /* Dolby Digital AC3 */
62 # ifdef WORDS_BIGENDIAN
63 # define AFMT_S16_NE AFMT_S16_BE
65 # define AFMT_S16_NE AFMT_S16_LE
70 /*****************************************************************************
71 * aout_sys_t: OSS audio output method descriptor
72 *****************************************************************************
73 * This structure is part of the audio output thread descriptor.
74 * It describes the dsp specific properties of an audio device.
75 *****************************************************************************/
79 int b_workaround_buggy_driver;
81 mtime_t max_buffer_duration;
84 /* This must be a power of 2. */
85 #define FRAME_SIZE 1024
88 /*****************************************************************************
90 *****************************************************************************/
91 static int Open ( vlc_object_t * );
92 static void Close ( vlc_object_t * );
94 static void Play ( aout_instance_t * );
95 static int OSSThread ( aout_instance_t * );
97 static mtime_t BufferDuration( aout_instance_t * p_aout );
99 /*****************************************************************************
101 *****************************************************************************/
102 #define BUGGY_TEXT N_("try to work around buggy OSS drivers")
103 #define BUGGY_LONGTEXT N_( \
104 "Some buggy OSS drivers just don't like when their internal buffers " \
105 "are completely filled (the sound gets heavily hashed). If you have one " \
106 "of these drivers, then you need to enable this option." )
109 add_category_hint( N_("OSS"), NULL, VLC_FALSE );
110 add_file( "dspdev", "/dev/dsp", aout_FindAndRestart,
111 N_("OSS dsp device"), NULL, VLC_FALSE );
112 add_bool( "oss-buggy", 0, NULL, BUGGY_TEXT, BUGGY_LONGTEXT, VLC_TRUE );
113 set_description( _("Linux OSS /dev/dsp module") );
114 set_capability( "audio output", 100 );
115 add_shortcut( "oss" );
116 set_callbacks( Open, Close );
119 /*****************************************************************************
120 * Probe: probe the audio device for available formats and channels
121 *****************************************************************************/
122 static void Probe( aout_instance_t * p_aout )
124 struct aout_sys_t * p_sys = p_aout->output.p_sys;
126 int i_format, i_nb_channels;
128 var_Create( p_aout, "audio-device", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
130 /* Test for multi-channel. */
131 #ifdef SNDCTL_DSP_GETCHANNELMASK
132 if ( aout_FormatNbChannels( &p_aout->output.output ) > 2 )
134 /* Check that the device supports this. */
139 i_format = AFMT_S16_NE;
140 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
141 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
143 msg_Err( p_aout, "cannot reset OSS audio device" );
144 var_Destroy( p_aout, "audio-device" );
148 if ( ioctl( p_sys->i_fd, SNDCTL_DSP_GETCHANNELMASK,
151 if ( !(i_chanmask & DSP_BIND_FRONT) )
153 msg_Err( p_aout, "No front channels ! (%x)",
158 if ( (i_chanmask & (DSP_BIND_SURR | DSP_BIND_CENTER_LFE))
159 && (p_aout->output.output.i_physical_channels ==
160 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
161 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
164 val.psz_string = N_("5.1");
165 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
168 if ( (i_chanmask & DSP_BIND_SURR)
169 && (p_aout->output.output.i_physical_channels &
170 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
171 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT)) )
173 val.psz_string = N_("2 Front 2 Rear");
174 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
181 i_format = AFMT_S16_NE;
182 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
183 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
185 msg_Err( p_aout, "cannot reset OSS audio device" );
186 var_Destroy( p_aout, "audio-device" );
190 /* Test for stereo. */
192 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
193 && i_nb_channels == 2 )
195 val.psz_string = N_("Stereo");
196 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
200 i_format = AFMT_S16_NE;
201 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
202 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
204 msg_Err( p_aout, "cannot reset OSS audio device" );
205 var_Destroy( p_aout, "audio-device" );
211 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
212 && i_nb_channels == 1 )
214 val.psz_string = N_("Mono");
215 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
216 if ( p_aout->output.output.i_physical_channels == AOUT_CHAN_CENTER )
218 var_Set( p_aout, "audio-device", val );
222 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
224 msg_Err( p_aout, "cannot reset OSS audio device" );
225 var_Destroy( p_aout, "audio-device" );
229 /* Test for spdif. */
230 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
234 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) >= 0
235 && i_format == AFMT_AC3 )
237 val.psz_string = N_("A/52 over S/PDIF");
238 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
239 if( config_GetInt( p_aout, "spdif" ) )
240 var_Set( p_aout, "audio-device", val );
244 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart,
248 /*****************************************************************************
249 * Open: open the audio device (the digital sound processor)
250 *****************************************************************************
251 * This function opens the dsp as a usual non-blocking write-only file, and
252 * modifies the p_aout->p_sys->i_fd with the file's descriptor.
253 *****************************************************************************/
254 static int Open( vlc_object_t *p_this )
256 aout_instance_t * p_aout = (aout_instance_t *)p_this;
257 struct aout_sys_t * p_sys;
261 /* Allocate structure */
262 p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
265 msg_Err( p_aout, "out of memory" );
269 /* Get device name */
270 if( (psz_device = config_GetPsz( p_aout, "dspdev" )) == NULL )
272 msg_Err( p_aout, "no audio device given (maybe /dev/dsp ?)" );
277 /* Open the sound device */
278 p_sys->i_fd = open( psz_device, O_WRONLY | O_NDELAY );
279 if( p_sys->i_fd < 0 )
281 msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
286 /* if the opening was ok, put the device back in blocking mode */
287 fcntl( p_sys->i_fd, F_SETFL,
288 fcntl( p_sys->i_fd, F_GETFL ) &~ FNDELAY );
292 p_aout->output.pf_play = Play;
294 if ( var_Type( p_aout, "audio-device" ) == 0 )
299 if ( var_Get( p_aout, "audio-device", &val ) < 0 )
301 /* Probe() has failed. */
306 if ( !strcmp( val.psz_string, N_("A/52 over S/PDIF") ) )
308 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
310 else if ( !strcmp( val.psz_string, N_("5.1") ) )
312 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
313 p_aout->output.output.i_physical_channels
314 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
315 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
318 else if ( !strcmp( val.psz_string, N_("2 Front 2 Rear") ) )
320 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
321 p_aout->output.output.i_physical_channels
322 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
323 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT;
325 else if ( !strcmp( val.psz_string, N_("Stereo") ) )
327 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
328 p_aout->output.output.i_physical_channels
329 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
331 else if ( !strcmp( val.psz_string, N_("Mono") ) )
333 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
334 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
338 /* This should not happen ! */
339 msg_Err( p_aout, "internal: can't find audio-device (%s)",
342 free( val.psz_string );
345 free( val.psz_string );
347 val.b_bool = VLC_TRUE;
348 var_Set( p_aout, "intf-change", val );
350 /* Reset the DSP device */
351 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
353 msg_Err( p_aout, "cannot reset OSS audio device" );
354 close( p_sys->i_fd );
359 /* Set the output format */
360 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
362 int i_format = AFMT_AC3;
364 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
365 || i_format != AFMT_AC3 )
367 msg_Err( p_aout, "cannot reset OSS audio device" );
368 close( p_sys->i_fd );
373 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
374 p_aout->output.i_nb_samples = A52_FRAME_NB;
375 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
376 p_aout->output.output.i_frame_length = A52_FRAME_NB;
378 aout_VolumeNoneInit( p_aout );
381 if ( !AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
383 unsigned int i_format = AFMT_S16_NE;
384 unsigned int i_frame_size, i_fragments;
386 unsigned int i_nb_channels;
387 audio_buf_info audio_buf;
389 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
391 msg_Err( p_aout, "cannot set audio output format" );
392 close( p_sys->i_fd );
400 p_aout->output.output.i_format = VLC_FOURCC('u','8',' ',' ');
403 p_aout->output.output.i_format = VLC_FOURCC('s','8',' ',' ');
406 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','l');
409 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l');
412 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','b');
415 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','b');
418 msg_Err( p_aout, "OSS fell back to an unknown format (%d)",
420 close( p_sys->i_fd );
425 i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
427 /* Set the number of channels */
428 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) < 0 ||
429 i_nb_channels != aout_FormatNbChannels( &p_aout->output.output ) )
431 msg_Err( p_aout, "cannot set number of audio channels (%s)",
432 aout_FormatPrintChannels( &p_aout->output.output) );
433 close( p_sys->i_fd );
438 /* Set the output rate */
439 i_rate = p_aout->output.output.i_rate;
440 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
442 msg_Err( p_aout, "cannot set audio output rate (%i)",
443 p_aout->output.output.i_rate );
444 close( p_sys->i_fd );
449 if( i_rate != p_aout->output.output.i_rate )
451 p_aout->output.output.i_rate = i_rate;
454 /* Set the fragment size */
455 aout_FormatPrepare( &p_aout->output.output );
457 /* i_fragment = xxxxyyyy where: xxxx is fragtotal
458 * 1 << yyyy is fragsize */
460 i_frame_size = FRAME_SIZE * p_aout->output.output.i_bytes_per_frame;
461 while( i_frame_size >>= 1 )
465 i_fragments |= FRAME_COUNT << 16;
466 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
468 msg_Warn( p_aout, "cannot set fragment size (%.8x)", i_fragments );
471 if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf ) < 0 )
473 msg_Err( p_aout, "cannot get fragment size" );
474 close( p_sys->i_fd );
480 /* Number of fragments actually allocated */
481 p_aout->output.p_sys->i_fragstotal = audio_buf.fragstotal;
483 /* Maximum duration the soundcard's buffer can hold */
484 p_aout->output.p_sys->max_buffer_duration =
485 (mtime_t)audio_buf.fragstotal * audio_buf.fragsize * 1000000
486 / p_aout->output.output.i_bytes_per_frame
487 / p_aout->output.output.i_rate
488 * p_aout->output.output.i_frame_length;
490 p_aout->output.i_nb_samples = audio_buf.fragsize /
491 p_aout->output.output.i_bytes_per_frame;
494 aout_VolumeSoftInit( p_aout );
497 p_aout->output.p_sys->b_workaround_buggy_driver =
498 config_GetInt( p_aout, "oss-buggy" );
500 /* Create OSS thread and wait for its readiness. */
501 if( vlc_thread_create( p_aout, "aout", OSSThread,
502 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
504 msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
505 close( p_sys->i_fd );
513 /*****************************************************************************
514 * Play: nothing to do
515 *****************************************************************************/
516 static void Play( aout_instance_t *p_aout )
520 /*****************************************************************************
521 * Close: close the dsp audio device
522 *****************************************************************************/
523 static void Close( vlc_object_t * p_this )
525 aout_instance_t *p_aout = (aout_instance_t *)p_this;
526 struct aout_sys_t * p_sys = p_aout->output.p_sys;
528 p_aout->b_die = VLC_TRUE;
529 vlc_thread_join( p_aout );
530 p_aout->b_die = VLC_FALSE;
532 ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
533 close( p_sys->i_fd );
538 /*****************************************************************************
539 * BufferDuration: buffer status query
540 *****************************************************************************
541 * This function returns the duration in microseconds of the current buffer.
542 *****************************************************************************/
543 static mtime_t BufferDuration( aout_instance_t * p_aout )
545 struct aout_sys_t * p_sys = p_aout->output.p_sys;
546 audio_buf_info audio_buf;
549 /* Fill the audio_buf_info structure:
550 * - fragstotal: total number of fragments allocated
551 * - fragsize: size of a fragment in bytes
552 * - bytes: available space in bytes (includes partially used fragments)
553 * Note! 'bytes' could be more than fragments*fragsize */
554 ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
556 /* calculate number of available fragments (not partially used ones) */
557 i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
559 /* Return the fragment duration */
560 return (mtime_t)i_bytes * 1000000
561 / p_aout->output.output.i_bytes_per_frame
562 / p_aout->output.output.i_rate
563 * p_aout->output.output.i_frame_length;
566 /*****************************************************************************
567 * OSSThread: asynchronous thread used to DMA the data to the device
568 *****************************************************************************/
569 static int OSSThread( aout_instance_t * p_aout )
571 struct aout_sys_t * p_sys = p_aout->output.p_sys;
572 mtime_t next_date = 0;
574 while ( !p_aout->b_die )
576 aout_buffer_t * p_buffer = NULL;
580 if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
582 mtime_t buffered = BufferDuration( p_aout );
584 if( p_aout->output.p_sys->b_workaround_buggy_driver )
586 #define i_fragstotal p_aout->output.p_sys->i_fragstotal
587 /* Wait a bit - we don't want our buffer to be full */
588 if( buffered > (p_aout->output.p_sys->max_buffer_duration
589 / i_fragstotal * (i_fragstotal - 1)) )
591 msleep((p_aout->output.p_sys->max_buffer_duration
593 buffered = BufferDuration( p_aout );
598 /* Next buffer will be played at mdate() + buffered */
599 p_buffer = aout_OutputNextBuffer( p_aout, mdate() + buffered,
602 if( p_buffer == NULL &&
603 buffered > ( p_aout->output.p_sys->max_buffer_duration
604 / p_aout->output.p_sys->i_fragstotal ) )
606 /* If we have at least a fragment full, then we can wait a
607 * little and retry to get a new audio buffer instead of
608 * playing a blank sample */
609 msleep( ( p_aout->output.p_sys->max_buffer_duration
610 / p_aout->output.p_sys->i_fragstotal / 2 ) );
616 /* emu10k1 driver does not report Buffer Duration correctly in
617 * passthrough mode so we have to cheat */
624 mtime_t delay = next_date - mdate();
625 if( delay > AOUT_PTS_TOLERANCE )
631 while( !p_aout->b_die && ! ( p_buffer =
632 aout_OutputNextBuffer( p_aout, next_date, VLC_TRUE ) ) )
639 if ( p_buffer != NULL )
641 p_bytes = p_buffer->p_buffer;
642 i_size = p_buffer->i_nb_bytes;
643 /* This is theoretical ... we'll see next iteration whether
645 next_date += p_buffer->end_date - p_buffer->start_date;
649 i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
650 * p_aout->output.output.i_bytes_per_frame;
651 p_bytes = malloc( i_size );
652 memset( p_bytes, 0, i_size );
656 i_tmp = write( p_sys->i_fd, p_bytes, i_size );
660 msg_Err( p_aout, "write failed (%s)", strerror(errno) );
663 if ( p_buffer != NULL )
665 aout_BufferFree( p_buffer );