1 /*****************************************************************************
2 * oss.c : OSS /dev/dsp module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2002 VideoLAN
5 * $Id: oss.c,v 1.59 2003/05/09 22:03:06 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
69 /*****************************************************************************
70 * aout_sys_t: OSS audio output method descriptor
71 *****************************************************************************
72 * This structure is part of the audio output thread descriptor.
73 * It describes the dsp specific properties of an audio device.
74 *****************************************************************************/
78 int b_workaround_buggy_driver;
80 mtime_t max_buffer_duration;
83 /* This must be a power of 2. */
84 #define FRAME_SIZE 1024
87 /*****************************************************************************
89 *****************************************************************************/
90 static int Open ( vlc_object_t * );
91 static void Close ( vlc_object_t * );
93 static void Play ( aout_instance_t * );
94 static int OSSThread ( aout_instance_t * );
96 static mtime_t BufferDuration( aout_instance_t * p_aout );
98 /*****************************************************************************
100 *****************************************************************************/
101 #define BUGGY_TEXT N_("try to work around buggy OSS drivers")
102 #define BUGGY_LONGTEXT N_( \
103 "Some buggy OSS drivers just don't like when their internal buffers " \
104 "are completely filled (the sound gets heavily hashed). If you have one " \
105 "of these drivers, then you need to enable this option." )
108 add_category_hint( N_("OSS"), NULL, VLC_FALSE );
109 add_file( "dspdev", "/dev/dsp", aout_FindAndRestart,
110 N_("OSS dsp device"), NULL, VLC_FALSE );
111 add_bool( "oss-buggy", 0, NULL, BUGGY_TEXT, BUGGY_LONGTEXT, VLC_TRUE );
112 set_description( _("Linux OSS audio output") );
113 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;
125 vlc_value_t val, text;
126 int i_format, i_nb_channels;
128 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
129 text.psz_string = _("Audio device");
130 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
132 /* Test for multi-channel. */
133 #ifdef SNDCTL_DSP_GETCHANNELMASK
134 if ( aout_FormatNbChannels( &p_aout->output.output ) > 2 )
136 /* Check that the device supports this. */
141 i_format = AFMT_S16_NE;
142 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
143 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
145 msg_Err( p_aout, "cannot reset OSS audio device" );
146 var_Destroy( p_aout, "audio-device" );
150 if ( ioctl( p_sys->i_fd, SNDCTL_DSP_GETCHANNELMASK,
153 if ( !(i_chanmask & DSP_BIND_FRONT) )
155 msg_Err( p_aout, "No front channels ! (%x)",
160 if ( (i_chanmask & (DSP_BIND_SURR | DSP_BIND_CENTER_LFE))
161 && (p_aout->output.output.i_physical_channels ==
162 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
163 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
166 val.i_int = AOUT_VAR_5_1;
167 text.psz_string = N_("5.1");
168 var_Change( p_aout, "audio-device",
169 VLC_VAR_ADDCHOICE, &val, &text );
172 if ( (i_chanmask & DSP_BIND_SURR)
173 && (p_aout->output.output.i_physical_channels &
174 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
175 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT)) )
177 val.i_int = AOUT_VAR_2F2R;
178 text.psz_string = N_("2 Front 2 Rear");
179 var_Change( p_aout, "audio-device",
180 VLC_VAR_ADDCHOICE, &val, &text );
187 i_format = AFMT_S16_NE;
188 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
189 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
191 msg_Err( p_aout, "cannot reset OSS audio device" );
192 var_Destroy( p_aout, "audio-device" );
196 /* Test for stereo. */
198 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
199 && i_nb_channels == 2 )
201 val.i_int = AOUT_VAR_STEREO;
202 text.psz_string = N_("Stereo");
203 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
207 i_format = AFMT_S16_NE;
208 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
209 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
211 msg_Err( p_aout, "cannot reset OSS audio device" );
212 var_Destroy( p_aout, "audio-device" );
218 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
219 && i_nb_channels == 1 )
221 val.i_int = AOUT_VAR_MONO;
222 text.psz_string = N_("Mono");
223 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
224 if ( p_aout->output.output.i_physical_channels == AOUT_CHAN_CENTER )
226 var_Set( p_aout, "audio-device", val );
230 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
232 msg_Err( p_aout, "cannot reset OSS audio device" );
233 var_Destroy( p_aout, "audio-device" );
237 /* Test for spdif. */
238 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
242 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) >= 0
243 && i_format == AFMT_AC3 )
245 val.i_int = AOUT_VAR_SPDIF;
246 text.psz_string = N_("A/52 over S/PDIF");
247 var_Change( p_aout, "audio-device",
248 VLC_VAR_ADDCHOICE, &val, &text );
249 if( config_GetInt( p_aout, "spdif" ) )
250 var_Set( p_aout, "audio-device", val );
252 else if( config_GetInt( p_aout, "spdif" ) )
254 msg_Warn( p_aout, "s/pdif not supported by card" );
258 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart,
262 /*****************************************************************************
263 * Open: open the audio device (the digital sound processor)
264 *****************************************************************************
265 * This function opens the dsp as a usual non-blocking write-only file, and
266 * modifies the p_aout->p_sys->i_fd with the file's descriptor.
267 *****************************************************************************/
268 static int Open( vlc_object_t *p_this )
270 aout_instance_t * p_aout = (aout_instance_t *)p_this;
271 struct aout_sys_t * p_sys;
275 /* Allocate structure */
276 p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
279 msg_Err( p_aout, "out of memory" );
283 /* Get device name */
284 if( (psz_device = config_GetPsz( p_aout, "dspdev" )) == NULL )
286 msg_Err( p_aout, "no audio device given (maybe /dev/dsp ?)" );
291 /* Open the sound device in non-blocking mode, because ALSA's OSS
292 * emulation and some broken OSS drivers would make a blocking call
293 * wait forever until the device is available. Since this breaks the
294 * OSS spec, we immediately put it back to blocking mode if the
295 * operation was successful. */
296 p_sys->i_fd = open( psz_device, O_WRONLY | O_NDELAY );
297 if( p_sys->i_fd < 0 )
299 msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
304 /* if the opening was ok, put the device back in blocking mode */
305 fcntl( p_sys->i_fd, F_SETFL,
306 fcntl( p_sys->i_fd, F_GETFL ) &~ FNDELAY );
310 p_aout->output.pf_play = Play;
312 if ( var_Type( p_aout, "audio-device" ) == 0 )
317 if ( var_Get( p_aout, "audio-device", &val ) < 0 )
319 /* Probe() has failed. */
324 if ( val.i_int == AOUT_VAR_SPDIF )
326 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
328 else if ( val.i_int == AOUT_VAR_5_1 )
330 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
331 p_aout->output.output.i_physical_channels
332 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
333 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
336 else if ( val.i_int == AOUT_VAR_2F2R )
338 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
339 p_aout->output.output.i_physical_channels
340 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
341 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
343 else if ( val.i_int == AOUT_VAR_STEREO )
345 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
346 p_aout->output.output.i_physical_channels
347 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
349 else if ( val.i_int == AOUT_VAR_MONO )
351 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
352 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
356 /* This should not happen ! */
357 msg_Err( p_aout, "internal: can't find audio-device (%i)", val.i_int );
362 val.b_bool = VLC_TRUE;
363 var_Set( p_aout, "intf-change", val );
365 /* Reset the DSP device */
366 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
368 msg_Err( p_aout, "cannot reset OSS audio device" );
369 close( p_sys->i_fd );
374 /* Set the output format */
375 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
377 int i_format = AFMT_AC3;
379 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
380 || i_format != AFMT_AC3 )
382 msg_Err( p_aout, "cannot reset OSS audio device" );
383 close( p_sys->i_fd );
388 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
389 p_aout->output.i_nb_samples = A52_FRAME_NB;
390 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
391 p_aout->output.output.i_frame_length = A52_FRAME_NB;
393 aout_VolumeNoneInit( p_aout );
396 if ( !AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
398 unsigned int i_format = AFMT_S16_NE;
399 unsigned int i_frame_size, i_fragments;
401 unsigned int i_nb_channels;
402 audio_buf_info audio_buf;
404 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
406 msg_Err( p_aout, "cannot set audio output format" );
407 close( p_sys->i_fd );
415 p_aout->output.output.i_format = VLC_FOURCC('u','8',' ',' ');
418 p_aout->output.output.i_format = VLC_FOURCC('s','8',' ',' ');
421 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','l');
424 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l');
427 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','b');
430 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','b');
433 msg_Err( p_aout, "OSS fell back to an unknown format (%d)",
435 close( p_sys->i_fd );
440 i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
442 /* Set the number of channels */
443 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) < 0 ||
444 i_nb_channels != aout_FormatNbChannels( &p_aout->output.output ) )
446 msg_Err( p_aout, "cannot set number of audio channels (%s)",
447 aout_FormatPrintChannels( &p_aout->output.output) );
448 close( p_sys->i_fd );
453 /* Set the output rate */
454 i_rate = p_aout->output.output.i_rate;
455 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
457 msg_Err( p_aout, "cannot set audio output rate (%i)",
458 p_aout->output.output.i_rate );
459 close( p_sys->i_fd );
464 if( i_rate != p_aout->output.output.i_rate )
466 p_aout->output.output.i_rate = i_rate;
469 /* Set the fragment size */
470 aout_FormatPrepare( &p_aout->output.output );
472 /* i_fragment = xxxxyyyy where: xxxx is fragtotal
473 * 1 << yyyy is fragsize */
475 i_frame_size = FRAME_SIZE * p_aout->output.output.i_bytes_per_frame;
476 while( i_frame_size >>= 1 )
480 i_fragments |= FRAME_COUNT << 16;
481 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
483 msg_Warn( p_aout, "cannot set fragment size (%.8x)", i_fragments );
486 if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf ) < 0 )
488 msg_Err( p_aout, "cannot get fragment size" );
489 close( p_sys->i_fd );
495 /* Number of fragments actually allocated */
496 p_aout->output.p_sys->i_fragstotal = audio_buf.fragstotal;
498 /* Maximum duration the soundcard's buffer can hold */
499 p_aout->output.p_sys->max_buffer_duration =
500 (mtime_t)audio_buf.fragstotal * audio_buf.fragsize * 1000000
501 / p_aout->output.output.i_bytes_per_frame
502 / p_aout->output.output.i_rate
503 * p_aout->output.output.i_frame_length;
505 p_aout->output.i_nb_samples = audio_buf.fragsize /
506 p_aout->output.output.i_bytes_per_frame;
509 aout_VolumeSoftInit( p_aout );
512 p_aout->output.p_sys->b_workaround_buggy_driver =
513 config_GetInt( p_aout, "oss-buggy" );
515 /* Create OSS thread and wait for its readiness. */
516 if( vlc_thread_create( p_aout, "aout", OSSThread,
517 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
519 msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
520 close( p_sys->i_fd );
528 /*****************************************************************************
529 * Play: nothing to do
530 *****************************************************************************/
531 static void Play( aout_instance_t *p_aout )
535 /*****************************************************************************
536 * Close: close the dsp audio device
537 *****************************************************************************/
538 static void Close( vlc_object_t * p_this )
540 aout_instance_t *p_aout = (aout_instance_t *)p_this;
541 struct aout_sys_t * p_sys = p_aout->output.p_sys;
543 p_aout->b_die = VLC_TRUE;
544 vlc_thread_join( p_aout );
545 p_aout->b_die = VLC_FALSE;
547 ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
548 close( p_sys->i_fd );
553 /*****************************************************************************
554 * BufferDuration: buffer status query
555 *****************************************************************************
556 * This function returns the duration in microseconds of the current buffer.
557 *****************************************************************************/
558 static mtime_t BufferDuration( aout_instance_t * p_aout )
560 struct aout_sys_t * p_sys = p_aout->output.p_sys;
561 audio_buf_info audio_buf;
564 /* Fill the audio_buf_info structure:
565 * - fragstotal: total number of fragments allocated
566 * - fragsize: size of a fragment in bytes
567 * - bytes: available space in bytes (includes partially used fragments)
568 * Note! 'bytes' could be more than fragments*fragsize */
569 ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
571 /* calculate number of available fragments (not partially used ones) */
572 i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
574 /* Return the fragment duration */
575 return (mtime_t)i_bytes * 1000000
576 / p_aout->output.output.i_bytes_per_frame
577 / p_aout->output.output.i_rate
578 * p_aout->output.output.i_frame_length;
581 /*****************************************************************************
582 * OSSThread: asynchronous thread used to DMA the data to the device
583 *****************************************************************************/
584 static int OSSThread( aout_instance_t * p_aout )
586 struct aout_sys_t * p_sys = p_aout->output.p_sys;
587 mtime_t next_date = 0;
589 while ( !p_aout->b_die )
591 aout_buffer_t * p_buffer = NULL;
595 if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
597 mtime_t buffered = BufferDuration( p_aout );
599 if( p_aout->output.p_sys->b_workaround_buggy_driver )
601 #define i_fragstotal p_aout->output.p_sys->i_fragstotal
602 /* Wait a bit - we don't want our buffer to be full */
603 if( buffered > (p_aout->output.p_sys->max_buffer_duration
604 / i_fragstotal * (i_fragstotal - 1)) )
606 msleep((p_aout->output.p_sys->max_buffer_duration
608 buffered = BufferDuration( p_aout );
613 /* Next buffer will be played at mdate() + buffered */
614 p_buffer = aout_OutputNextBuffer( p_aout, mdate() + buffered,
617 if( p_buffer == NULL &&
618 buffered > ( p_aout->output.p_sys->max_buffer_duration
619 / p_aout->output.p_sys->i_fragstotal ) )
621 /* If we have at least a fragment full, then we can wait a
622 * little and retry to get a new audio buffer instead of
623 * playing a blank sample */
624 msleep( ( p_aout->output.p_sys->max_buffer_duration
625 / p_aout->output.p_sys->i_fragstotal / 2 ) );
631 /* emu10k1 driver does not report Buffer Duration correctly in
632 * passthrough mode so we have to cheat */
639 mtime_t delay = next_date - mdate();
640 if( delay > AOUT_PTS_TOLERANCE )
646 while( !p_aout->b_die && ! ( p_buffer =
647 aout_OutputNextBuffer( p_aout, next_date, VLC_TRUE ) ) )
654 if ( p_buffer != NULL )
656 p_bytes = p_buffer->p_buffer;
657 i_size = p_buffer->i_nb_bytes;
658 /* This is theoretical ... we'll see next iteration whether
660 next_date += p_buffer->end_date - p_buffer->start_date;
664 i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
665 * p_aout->output.output.i_bytes_per_frame;
666 p_bytes = malloc( i_size );
667 memset( p_bytes, 0, i_size );
671 i_tmp = write( p_sys->i_fd, p_bytes, i_size );
675 msg_Err( p_aout, "write failed (%s)", strerror(errno) );
678 if ( p_buffer != NULL )
680 aout_BufferFree( p_buffer );