1 /*****************************************************************************
2 * oss.c : OSS /dev/dsp module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2002 VideoLAN
5 * $Id: oss.c,v 1.57 2003/04/20 21:19:41 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 /* 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 audio output") );
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 in non-blocking mode, because ALSA's OSS
278 * emulation and some broken OSS drivers would make a blocking call
279 * wait forever until the device is available. Since this breaks the
280 * OSS spec, we immediately put it back to blocking mode if the
281 * operation was successful. */
282 p_sys->i_fd = open( psz_device, O_WRONLY | O_NDELAY );
283 if( p_sys->i_fd < 0 )
285 msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
290 /* if the opening was ok, put the device back in blocking mode */
291 fcntl( p_sys->i_fd, F_SETFL,
292 fcntl( p_sys->i_fd, F_GETFL ) &~ FNDELAY );
296 p_aout->output.pf_play = Play;
298 if ( var_Type( p_aout, "audio-device" ) == 0 )
303 if ( var_Get( p_aout, "audio-device", &val ) < 0 )
305 /* Probe() has failed. */
310 if ( !strcmp( val.psz_string, N_("A/52 over S/PDIF") ) )
312 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
314 else if ( !strcmp( val.psz_string, N_("5.1") ) )
316 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
317 p_aout->output.output.i_physical_channels
318 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
319 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
322 else if ( !strcmp( val.psz_string, N_("2 Front 2 Rear") ) )
324 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
325 p_aout->output.output.i_physical_channels
326 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
327 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
329 else if ( !strcmp( val.psz_string, N_("Stereo") ) )
331 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
332 p_aout->output.output.i_physical_channels
333 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
335 else if ( !strcmp( val.psz_string, N_("Mono") ) )
337 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
338 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
342 /* This should not happen ! */
343 msg_Err( p_aout, "internal: can't find audio-device (%s)",
346 free( val.psz_string );
349 free( val.psz_string );
351 val.b_bool = VLC_TRUE;
352 var_Set( p_aout, "intf-change", val );
354 /* Reset the DSP device */
355 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
357 msg_Err( p_aout, "cannot reset OSS audio device" );
358 close( p_sys->i_fd );
363 /* Set the output format */
364 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
366 int i_format = AFMT_AC3;
368 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
369 || i_format != AFMT_AC3 )
371 msg_Err( p_aout, "cannot reset OSS audio device" );
372 close( p_sys->i_fd );
377 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
378 p_aout->output.i_nb_samples = A52_FRAME_NB;
379 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
380 p_aout->output.output.i_frame_length = A52_FRAME_NB;
382 aout_VolumeNoneInit( p_aout );
385 if ( !AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
387 unsigned int i_format = AFMT_S16_NE;
388 unsigned int i_frame_size, i_fragments;
390 unsigned int i_nb_channels;
391 audio_buf_info audio_buf;
393 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
395 msg_Err( p_aout, "cannot set audio output format" );
396 close( p_sys->i_fd );
404 p_aout->output.output.i_format = VLC_FOURCC('u','8',' ',' ');
407 p_aout->output.output.i_format = VLC_FOURCC('s','8',' ',' ');
410 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','l');
413 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l');
416 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','b');
419 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','b');
422 msg_Err( p_aout, "OSS fell back to an unknown format (%d)",
424 close( p_sys->i_fd );
429 i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
431 /* Set the number of channels */
432 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) < 0 ||
433 i_nb_channels != aout_FormatNbChannels( &p_aout->output.output ) )
435 msg_Err( p_aout, "cannot set number of audio channels (%s)",
436 aout_FormatPrintChannels( &p_aout->output.output) );
437 close( p_sys->i_fd );
442 /* Set the output rate */
443 i_rate = p_aout->output.output.i_rate;
444 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
446 msg_Err( p_aout, "cannot set audio output rate (%i)",
447 p_aout->output.output.i_rate );
448 close( p_sys->i_fd );
453 if( i_rate != p_aout->output.output.i_rate )
455 p_aout->output.output.i_rate = i_rate;
458 /* Set the fragment size */
459 aout_FormatPrepare( &p_aout->output.output );
461 /* i_fragment = xxxxyyyy where: xxxx is fragtotal
462 * 1 << yyyy is fragsize */
464 i_frame_size = FRAME_SIZE * p_aout->output.output.i_bytes_per_frame;
465 while( i_frame_size >>= 1 )
469 i_fragments |= FRAME_COUNT << 16;
470 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
472 msg_Warn( p_aout, "cannot set fragment size (%.8x)", i_fragments );
475 if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf ) < 0 )
477 msg_Err( p_aout, "cannot get fragment size" );
478 close( p_sys->i_fd );
484 /* Number of fragments actually allocated */
485 p_aout->output.p_sys->i_fragstotal = audio_buf.fragstotal;
487 /* Maximum duration the soundcard's buffer can hold */
488 p_aout->output.p_sys->max_buffer_duration =
489 (mtime_t)audio_buf.fragstotal * audio_buf.fragsize * 1000000
490 / p_aout->output.output.i_bytes_per_frame
491 / p_aout->output.output.i_rate
492 * p_aout->output.output.i_frame_length;
494 p_aout->output.i_nb_samples = audio_buf.fragsize /
495 p_aout->output.output.i_bytes_per_frame;
498 aout_VolumeSoftInit( p_aout );
501 p_aout->output.p_sys->b_workaround_buggy_driver =
502 config_GetInt( p_aout, "oss-buggy" );
504 /* Create OSS thread and wait for its readiness. */
505 if( vlc_thread_create( p_aout, "aout", OSSThread,
506 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
508 msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
509 close( p_sys->i_fd );
517 /*****************************************************************************
518 * Play: nothing to do
519 *****************************************************************************/
520 static void Play( aout_instance_t *p_aout )
524 /*****************************************************************************
525 * Close: close the dsp audio device
526 *****************************************************************************/
527 static void Close( vlc_object_t * p_this )
529 aout_instance_t *p_aout = (aout_instance_t *)p_this;
530 struct aout_sys_t * p_sys = p_aout->output.p_sys;
532 p_aout->b_die = VLC_TRUE;
533 vlc_thread_join( p_aout );
534 p_aout->b_die = VLC_FALSE;
536 ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
537 close( p_sys->i_fd );
542 /*****************************************************************************
543 * BufferDuration: buffer status query
544 *****************************************************************************
545 * This function returns the duration in microseconds of the current buffer.
546 *****************************************************************************/
547 static mtime_t BufferDuration( aout_instance_t * p_aout )
549 struct aout_sys_t * p_sys = p_aout->output.p_sys;
550 audio_buf_info audio_buf;
553 /* Fill the audio_buf_info structure:
554 * - fragstotal: total number of fragments allocated
555 * - fragsize: size of a fragment in bytes
556 * - bytes: available space in bytes (includes partially used fragments)
557 * Note! 'bytes' could be more than fragments*fragsize */
558 ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
560 /* calculate number of available fragments (not partially used ones) */
561 i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
563 /* Return the fragment duration */
564 return (mtime_t)i_bytes * 1000000
565 / p_aout->output.output.i_bytes_per_frame
566 / p_aout->output.output.i_rate
567 * p_aout->output.output.i_frame_length;
570 /*****************************************************************************
571 * OSSThread: asynchronous thread used to DMA the data to the device
572 *****************************************************************************/
573 static int OSSThread( aout_instance_t * p_aout )
575 struct aout_sys_t * p_sys = p_aout->output.p_sys;
576 mtime_t next_date = 0;
578 while ( !p_aout->b_die )
580 aout_buffer_t * p_buffer = NULL;
584 if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
586 mtime_t buffered = BufferDuration( p_aout );
588 if( p_aout->output.p_sys->b_workaround_buggy_driver )
590 #define i_fragstotal p_aout->output.p_sys->i_fragstotal
591 /* Wait a bit - we don't want our buffer to be full */
592 if( buffered > (p_aout->output.p_sys->max_buffer_duration
593 / i_fragstotal * (i_fragstotal - 1)) )
595 msleep((p_aout->output.p_sys->max_buffer_duration
597 buffered = BufferDuration( p_aout );
602 /* Next buffer will be played at mdate() + buffered */
603 p_buffer = aout_OutputNextBuffer( p_aout, mdate() + buffered,
606 if( p_buffer == NULL &&
607 buffered > ( p_aout->output.p_sys->max_buffer_duration
608 / p_aout->output.p_sys->i_fragstotal ) )
610 /* If we have at least a fragment full, then we can wait a
611 * little and retry to get a new audio buffer instead of
612 * playing a blank sample */
613 msleep( ( p_aout->output.p_sys->max_buffer_duration
614 / p_aout->output.p_sys->i_fragstotal / 2 ) );
620 /* emu10k1 driver does not report Buffer Duration correctly in
621 * passthrough mode so we have to cheat */
628 mtime_t delay = next_date - mdate();
629 if( delay > AOUT_PTS_TOLERANCE )
635 while( !p_aout->b_die && ! ( p_buffer =
636 aout_OutputNextBuffer( p_aout, next_date, VLC_TRUE ) ) )
643 if ( p_buffer != NULL )
645 p_bytes = p_buffer->p_buffer;
646 i_size = p_buffer->i_nb_bytes;
647 /* This is theoretical ... we'll see next iteration whether
649 next_date += p_buffer->end_date - p_buffer->start_date;
653 i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
654 * p_aout->output.output.i_bytes_per_frame;
655 p_bytes = malloc( i_size );
656 memset( p_bytes, 0, i_size );
660 i_tmp = write( p_sys->i_fd, p_bytes, i_size );
664 msg_Err( p_aout, "write failed (%s)", strerror(errno) );
667 if ( p_buffer != NULL )
669 aout_BufferFree( p_buffer );