1 /*****************************************************************************
2 * oss.c : OSS /dev/dsp module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2002 VideoLAN
5 * $Id: oss.c,v 1.52 2003/02/20 01:52:45 sigmunau 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." )
108 #define SPDIF_TEXT N_("try to use S/PDIF output")
109 #define SPDIF_LONGTEXT N_( \
110 "Sometimes we attempt to use the S/PDIF output, even if nothing is " \
111 "connected to it. Un-checking this option disables this behaviour, " \
112 "and permanently selects analog PCM output." )
115 add_category_hint( N_("OSS"), NULL, VLC_FALSE );
116 add_file( "dspdev", "/dev/dsp", aout_FindAndRestart,
117 N_("OSS dsp device"), NULL, VLC_FALSE );
118 add_bool( "oss-buggy", 0, NULL, BUGGY_TEXT, BUGGY_LONGTEXT, VLC_TRUE );
119 add_bool( "spdif", 1, NULL, SPDIF_TEXT, SPDIF_LONGTEXT, VLC_FALSE );
120 set_description( _("Linux OSS /dev/dsp module") );
121 set_capability( "audio output", 100 );
122 add_shortcut( "oss" );
123 set_callbacks( Open, Close );
126 /*****************************************************************************
127 * Probe: probe the audio device for available formats and channels
128 *****************************************************************************/
129 static void Probe( aout_instance_t * p_aout )
131 struct aout_sys_t * p_sys = p_aout->output.p_sys;
133 int i_format, i_nb_channels;
135 var_Create( p_aout, "audio-device", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
137 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
139 msg_Err( p_aout, "cannot reset OSS audio device" );
140 var_Destroy( p_aout, "audio-device" );
144 if ( config_GetInt( p_aout, "spdif" )
145 && AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
149 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) >= 0
150 && i_format == AFMT_AC3 )
152 val.psz_string = N_("A/52 over S/PDIF");
153 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
157 /* Go to PCM mode. */
158 i_format = AFMT_S16_NE;
159 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
160 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
165 #ifdef SNDCTL_DSP_GETCHANNELMASK
166 if ( aout_FormatNbChannels( &p_aout->output.output ) > 2 )
168 /* Check that the device supports this. */
171 if ( ioctl( p_sys->i_fd, SNDCTL_DSP_GETCHANNELMASK,
174 if ( !(i_chanmask & DSP_BIND_FRONT) )
176 msg_Err( p_aout, "No front channels ! (%x)",
181 if ( (i_chanmask & (DSP_BIND_SURR | DSP_BIND_CENTER_LFE))
182 && (p_aout->output.output.i_physical_channels ==
183 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
184 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
187 val.psz_string = N_("5.1");
188 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
191 if ( (i_chanmask & DSP_BIND_SURR)
192 && (p_aout->output.output.i_physical_channels &
193 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
194 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT)) )
196 val.psz_string = N_("2 Front 2 Rear");
197 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
203 /* Test for stereo. */
205 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
206 && i_nb_channels == 2 )
208 val.psz_string = N_("Stereo");
209 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
213 i_format = AFMT_S16_NE;
214 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
215 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
217 msg_Err( p_aout, "cannot reset OSS audio device" );
218 var_Destroy( p_aout, "audio-device" );
224 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
225 && i_nb_channels == 1 )
227 val.psz_string = N_("Mono");
228 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
229 if ( p_aout->output.output.i_physical_channels == AOUT_CHAN_CENTER )
231 var_Set( p_aout, "audio-device", val );
236 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart,
240 /*****************************************************************************
241 * Open: open the audio device (the digital sound processor)
242 *****************************************************************************
243 * This function opens the dsp as a usual non-blocking write-only file, and
244 * modifies the p_aout->p_sys->i_fd with the file's descriptor.
245 *****************************************************************************/
246 static int Open( vlc_object_t *p_this )
248 aout_instance_t * p_aout = (aout_instance_t *)p_this;
249 struct aout_sys_t * p_sys;
253 /* Allocate structure */
254 p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
257 msg_Err( p_aout, "out of memory" );
261 /* Get device name */
262 if( (psz_device = config_GetPsz( p_aout, "dspdev" )) == NULL )
264 msg_Err( p_aout, "no audio device given (maybe /dev/dsp ?)" );
269 /* Open the sound device */
270 p_sys->i_fd = open( psz_device, O_WRONLY | O_NDELAY );
271 if( p_sys->i_fd < 0 )
273 msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
278 /* if the opening was ok, put the device back in blocking mode */
279 fcntl( p_sys->i_fd, F_SETFL,
280 fcntl( p_sys->i_fd, F_GETFL ) &~ FNDELAY );
284 p_aout->output.pf_play = Play;
286 if ( var_Type( p_aout, "audio-device" ) == 0 )
291 if ( var_Get( p_aout, "audio-device", &val ) < 0 )
293 /* Probe() has failed. */
298 if ( !strcmp( val.psz_string, N_("A/52 over S/PDIF") ) )
300 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
302 else if ( !strcmp( val.psz_string, N_("5.1") ) )
304 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
305 p_aout->output.output.i_physical_channels
306 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
307 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT
310 else if ( !strcmp( val.psz_string, N_("2 Front 2 Rear") ) )
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
315 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT;
317 else if ( !strcmp( val.psz_string, N_("Stereo") ) )
319 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
320 p_aout->output.output.i_physical_channels
321 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
323 else if ( !strcmp( val.psz_string, N_("Mono") ) )
325 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
326 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
330 /* This should not happen ! */
331 msg_Err( p_aout, "internal: can't find audio-device (%s)",
334 free( val.psz_string );
337 free( val.psz_string );
339 val.b_bool = VLC_TRUE;
340 var_Set( p_aout, "intf-change", val );
342 /* Reset the DSP device */
343 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
345 msg_Err( p_aout, "cannot reset OSS audio device" );
346 close( p_sys->i_fd );
351 /* Set the output format */
352 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
354 int i_format = AFMT_AC3;
356 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
357 || i_format != AFMT_AC3 )
359 msg_Err( p_aout, "cannot reset OSS audio device" );
360 close( p_sys->i_fd );
365 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
366 p_aout->output.i_nb_samples = A52_FRAME_NB;
367 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
368 p_aout->output.output.i_frame_length = A52_FRAME_NB;
370 aout_VolumeNoneInit( p_aout );
373 if ( !AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
375 unsigned int i_format = AFMT_S16_NE;
376 unsigned int i_frame_size, i_fragments;
378 unsigned int i_nb_channels;
379 audio_buf_info audio_buf;
381 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
383 msg_Err( p_aout, "cannot set audio output format" );
384 close( p_sys->i_fd );
392 p_aout->output.output.i_format = VLC_FOURCC('u','8',' ',' ');
395 p_aout->output.output.i_format = VLC_FOURCC('s','8',' ',' ');
398 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','l');
401 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l');
404 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','b');
407 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','b');
410 msg_Err( p_aout, "OSS fell back to an unknown format (%d)",
412 close( p_sys->i_fd );
417 i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
419 /* Set the number of channels */
420 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) < 0 ||
421 i_nb_channels != aout_FormatNbChannels( &p_aout->output.output ) )
423 msg_Err( p_aout, "cannot set number of audio channels (%s)",
424 aout_FormatPrintChannels( &p_aout->output.output) );
425 close( p_sys->i_fd );
430 /* Set the output rate */
431 i_rate = p_aout->output.output.i_rate;
432 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
434 msg_Err( p_aout, "cannot set audio output rate (%i)",
435 p_aout->output.output.i_rate );
436 close( p_sys->i_fd );
441 if( i_rate != p_aout->output.output.i_rate )
443 p_aout->output.output.i_rate = i_rate;
446 /* Set the fragment size */
447 aout_FormatPrepare( &p_aout->output.output );
449 /* i_fragment = xxxxyyyy where: xxxx is fragtotal
450 * 1 << yyyy is fragsize */
452 i_frame_size = FRAME_SIZE * p_aout->output.output.i_bytes_per_frame;
453 while( i_frame_size >>= 1 )
457 i_fragments |= FRAME_COUNT << 16;
458 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
460 msg_Warn( p_aout, "cannot set fragment size (%.8x)", i_fragments );
463 if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf ) < 0 )
465 msg_Err( p_aout, "cannot get fragment size" );
466 close( p_sys->i_fd );
472 /* Number of fragments actually allocated */
473 p_aout->output.p_sys->i_fragstotal = audio_buf.fragstotal;
475 /* Maximum duration the soundcard's buffer can hold */
476 p_aout->output.p_sys->max_buffer_duration =
477 (mtime_t)audio_buf.fragstotal * audio_buf.fragsize * 1000000
478 / p_aout->output.output.i_bytes_per_frame
479 / p_aout->output.output.i_rate
480 * p_aout->output.output.i_frame_length;
482 p_aout->output.i_nb_samples = audio_buf.fragsize /
483 p_aout->output.output.i_bytes_per_frame;
486 aout_VolumeSoftInit( p_aout );
489 p_aout->output.p_sys->b_workaround_buggy_driver =
490 config_GetInt( p_aout, "oss-buggy" );
492 /* Create OSS thread and wait for its readiness. */
493 if( vlc_thread_create( p_aout, "aout", OSSThread,
494 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
496 msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
497 close( p_sys->i_fd );
505 /*****************************************************************************
506 * Play: nothing to do
507 *****************************************************************************/
508 static void Play( aout_instance_t *p_aout )
512 /*****************************************************************************
513 * Close: close the dsp audio device
514 *****************************************************************************/
515 static void Close( vlc_object_t * p_this )
517 aout_instance_t *p_aout = (aout_instance_t *)p_this;
518 struct aout_sys_t * p_sys = p_aout->output.p_sys;
520 p_aout->b_die = VLC_TRUE;
521 vlc_thread_join( p_aout );
522 p_aout->b_die = VLC_FALSE;
524 ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
525 close( p_sys->i_fd );
530 /*****************************************************************************
531 * BufferDuration: buffer status query
532 *****************************************************************************
533 * This function returns the duration in microseconds of the current buffer.
534 *****************************************************************************/
535 static mtime_t BufferDuration( aout_instance_t * p_aout )
537 struct aout_sys_t * p_sys = p_aout->output.p_sys;
538 audio_buf_info audio_buf;
541 /* Fill the audio_buf_info structure:
542 * - fragstotal: total number of fragments allocated
543 * - fragsize: size of a fragment in bytes
544 * - bytes: available space in bytes (includes partially used fragments)
545 * Note! 'bytes' could be more than fragments*fragsize */
546 ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
548 /* calculate number of available fragments (not partially used ones) */
549 i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
551 /* Return the fragment duration */
552 return (mtime_t)i_bytes * 1000000
553 / p_aout->output.output.i_bytes_per_frame
554 / p_aout->output.output.i_rate
555 * p_aout->output.output.i_frame_length;
558 /*****************************************************************************
559 * OSSThread: asynchronous thread used to DMA the data to the device
560 *****************************************************************************/
561 static int OSSThread( aout_instance_t * p_aout )
563 struct aout_sys_t * p_sys = p_aout->output.p_sys;
564 mtime_t next_date = 0;
566 while ( !p_aout->b_die )
568 aout_buffer_t * p_buffer = NULL;
572 if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
574 mtime_t buffered = BufferDuration( p_aout );
576 if( p_aout->output.p_sys->b_workaround_buggy_driver )
578 #define i_fragstotal p_aout->output.p_sys->i_fragstotal
579 /* Wait a bit - we don't want our buffer to be full */
580 if( buffered > (p_aout->output.p_sys->max_buffer_duration
581 / i_fragstotal * (i_fragstotal - 1)) )
583 msleep((p_aout->output.p_sys->max_buffer_duration
585 buffered = BufferDuration( p_aout );
590 /* Next buffer will be played at mdate() + buffered */
591 p_buffer = aout_OutputNextBuffer( p_aout, mdate() + buffered,
594 if( p_buffer == NULL &&
595 buffered > ( p_aout->output.p_sys->max_buffer_duration
596 / p_aout->output.p_sys->i_fragstotal ) )
598 /* If we have at least a fragment full, then we can wait a
599 * little and retry to get a new audio buffer instead of
600 * playing a blank sample */
601 msleep( ( p_aout->output.p_sys->max_buffer_duration
602 / p_aout->output.p_sys->i_fragstotal / 2 ) );
608 /* emu10k1 driver does not report Buffer Duration correctly in
609 * passthrough mode so we have to cheat */
616 mtime_t delay = next_date - mdate();
617 if( delay > AOUT_PTS_TOLERANCE )
623 while( !p_aout->b_die && ! ( p_buffer =
624 aout_OutputNextBuffer( p_aout, next_date, VLC_TRUE ) ) )
631 if ( p_buffer != NULL )
633 p_bytes = p_buffer->p_buffer;
634 i_size = p_buffer->i_nb_bytes;
635 /* This is theoretical ... we'll see next iteration whether
637 next_date += p_buffer->end_date - p_buffer->start_date;
641 i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
642 * p_aout->output.output.i_bytes_per_frame;
643 p_bytes = malloc( i_size );
644 memset( p_bytes, 0, i_size );
648 i_tmp = write( p_sys->i_fd, p_bytes, i_size );
652 msg_Err( p_aout, "write failed (%s)", strerror(errno) );
655 if ( p_buffer != NULL )
657 aout_BufferFree( p_buffer );