1 /*****************************************************************************
2 * oss.c : OSS /dev/dsp module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2002 VideoLAN
7 * Authors: Michel Kaempf <maxx@via.ecp.fr>
8 * Sam 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 set_description( _("Linux OSS audio output") );
110 set_category( CAT_AUDIO );
111 set_subcategory( SUBCAT_AUDIO_AOUT );
112 add_file( "dspdev", "/dev/dsp", aout_FindAndRestart,
113 N_("OSS DSP device"), NULL, VLC_FALSE );
114 add_bool( "oss-buggy", 0, NULL, BUGGY_TEXT, BUGGY_LONGTEXT, VLC_TRUE );
116 set_capability( "audio output", 100 );
117 add_shortcut( "oss" );
118 set_callbacks( Open, Close );
121 /*****************************************************************************
122 * Probe: probe the audio device for available formats and channels
123 *****************************************************************************/
124 static void Probe( aout_instance_t * p_aout )
126 struct aout_sys_t * p_sys = p_aout->output.p_sys;
127 vlc_value_t val, text;
128 int i_format, i_nb_channels;
130 var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
131 text.psz_string = _("Audio Device");
132 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
134 /* Test for multi-channel. */
135 #ifdef SNDCTL_DSP_GETCHANNELMASK
136 if ( aout_FormatNbChannels( &p_aout->output.output ) > 2 )
138 /* Check that the device supports this. */
143 i_format = AFMT_S16_NE;
144 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
145 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
147 msg_Err( p_aout, "cannot reset OSS audio device" );
148 var_Destroy( p_aout, "audio-device" );
152 if ( ioctl( p_sys->i_fd, SNDCTL_DSP_GETCHANNELMASK,
155 if ( !(i_chanmask & DSP_BIND_FRONT) )
157 msg_Err( p_aout, "No front channels ! (%x)",
162 if ( (i_chanmask & (DSP_BIND_SURR | DSP_BIND_CENTER_LFE))
163 && (p_aout->output.output.i_physical_channels ==
164 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
165 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
168 val.i_int = AOUT_VAR_5_1;
169 text.psz_string = N_("5.1");
170 var_Change( p_aout, "audio-device",
171 VLC_VAR_ADDCHOICE, &val, &text );
174 if ( (i_chanmask & DSP_BIND_SURR)
175 && (p_aout->output.output.i_physical_channels &
176 (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
177 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT)) )
179 val.i_int = AOUT_VAR_2F2R;
180 text.psz_string = N_("2 Front 2 Rear");
181 var_Change( p_aout, "audio-device",
182 VLC_VAR_ADDCHOICE, &val, &text );
189 i_format = AFMT_S16_NE;
190 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
191 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
193 msg_Err( p_aout, "cannot reset OSS audio device" );
194 var_Destroy( p_aout, "audio-device" );
198 /* Test for stereo. */
200 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
201 && i_nb_channels == 2 )
203 val.i_int = AOUT_VAR_STEREO;
204 text.psz_string = N_("Stereo");
205 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
209 i_format = AFMT_S16_NE;
210 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
211 ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
213 msg_Err( p_aout, "cannot reset OSS audio device" );
214 var_Destroy( p_aout, "audio-device" );
220 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
221 && i_nb_channels == 1 )
223 val.i_int = AOUT_VAR_MONO;
224 text.psz_string = N_("Mono");
225 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
226 if ( p_aout->output.output.i_physical_channels == AOUT_CHAN_CENTER )
228 var_Set( p_aout, "audio-device", val );
232 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
234 msg_Err( p_aout, "cannot reset OSS audio device" );
235 var_Destroy( p_aout, "audio-device" );
239 /* Test for spdif. */
240 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
244 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) >= 0
245 && i_format == AFMT_AC3 )
247 val.i_int = AOUT_VAR_SPDIF;
248 text.psz_string = N_("A/52 over S/PDIF");
249 var_Change( p_aout, "audio-device",
250 VLC_VAR_ADDCHOICE, &val, &text );
251 if( config_GetInt( p_aout, "spdif" ) )
252 var_Set( p_aout, "audio-device", val );
254 else if( config_GetInt( p_aout, "spdif" ) )
256 msg_Warn( p_aout, "s/pdif not supported by card" );
260 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart,
264 /*****************************************************************************
265 * Open: open the audio device (the digital sound processor)
266 *****************************************************************************
267 * This function opens the DSP as a usual non-blocking write-only file, and
268 * modifies the p_aout->p_sys->i_fd with the file's descriptor.
269 *****************************************************************************/
270 static int Open( vlc_object_t *p_this )
272 aout_instance_t * p_aout = (aout_instance_t *)p_this;
273 struct aout_sys_t * p_sys;
277 /* Allocate structure */
278 p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
281 msg_Err( p_aout, "out of memory" );
285 /* Get device name */
286 if( (psz_device = config_GetPsz( p_aout, "dspdev" )) == NULL )
288 msg_Err( p_aout, "no audio device specified (maybe /dev/dsp?)" );
293 /* Open the sound device in non-blocking mode, because ALSA's OSS
294 * emulation and some broken OSS drivers would make a blocking call
295 * wait forever until the device is available. Since this breaks the
296 * OSS spec, we immediately put it back to blocking mode if the
297 * operation was successful. */
298 p_sys->i_fd = open( psz_device, O_WRONLY | O_NDELAY );
299 if( p_sys->i_fd < 0 )
301 msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
306 /* if the opening was ok, put the device back in blocking mode */
307 fcntl( p_sys->i_fd, F_SETFL,
308 fcntl( p_sys->i_fd, F_GETFL ) &~ FNDELAY );
312 p_aout->output.pf_play = Play;
314 if ( var_Type( p_aout, "audio-device" ) == 0 )
319 if ( var_Get( p_aout, "audio-device", &val ) < 0 )
321 /* Probe() has failed. */
326 if ( val.i_int == AOUT_VAR_SPDIF )
328 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
330 else if ( val.i_int == AOUT_VAR_5_1 )
332 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
333 p_aout->output.output.i_physical_channels
334 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
335 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
338 else if ( val.i_int == AOUT_VAR_2F2R )
340 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
341 p_aout->output.output.i_physical_channels
342 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
343 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
345 else if ( val.i_int == AOUT_VAR_STEREO )
347 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
348 p_aout->output.output.i_physical_channels
349 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
351 else if ( val.i_int == AOUT_VAR_MONO )
353 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
354 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
358 /* This should not happen ! */
359 msg_Err( p_aout, "internal: can't find audio-device (%i)", val.i_int );
364 val.b_bool = VLC_TRUE;
365 var_Set( p_aout, "intf-change", val );
367 /* Reset the DSP device */
368 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
370 msg_Err( p_aout, "cannot reset OSS audio device" );
371 close( p_sys->i_fd );
376 /* Set the output format */
377 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
379 int i_format = AFMT_AC3;
381 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
382 || i_format != AFMT_AC3 )
384 msg_Err( p_aout, "cannot reset OSS audio device" );
385 close( p_sys->i_fd );
390 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
391 p_aout->output.i_nb_samples = A52_FRAME_NB;
392 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
393 p_aout->output.output.i_frame_length = A52_FRAME_NB;
395 aout_VolumeNoneInit( p_aout );
398 if ( !AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
400 unsigned int i_format = AFMT_S16_NE;
401 unsigned int i_frame_size, i_fragments;
403 unsigned int i_nb_channels;
404 audio_buf_info audio_buf;
406 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
408 msg_Err( p_aout, "cannot set audio output format" );
409 close( p_sys->i_fd );
417 p_aout->output.output.i_format = VLC_FOURCC('u','8',' ',' ');
420 p_aout->output.output.i_format = VLC_FOURCC('s','8',' ',' ');
423 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','l');
426 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l');
429 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','b');
432 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','b');
435 msg_Err( p_aout, "OSS fell back to an unknown format (%d)",
437 close( p_sys->i_fd );
442 i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
444 /* Set the number of channels */
445 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) < 0 ||
446 i_nb_channels != aout_FormatNbChannels( &p_aout->output.output ) )
448 msg_Err( p_aout, "cannot set number of audio channels (%s)",
449 aout_FormatPrintChannels( &p_aout->output.output) );
450 close( p_sys->i_fd );
455 /* Set the output rate */
456 i_rate = p_aout->output.output.i_rate;
457 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
459 msg_Err( p_aout, "cannot set audio output rate (%i)",
460 p_aout->output.output.i_rate );
461 close( p_sys->i_fd );
466 if( i_rate != p_aout->output.output.i_rate )
468 p_aout->output.output.i_rate = i_rate;
471 /* Set the fragment size */
472 aout_FormatPrepare( &p_aout->output.output );
474 /* i_fragment = xxxxyyyy where: xxxx is fragtotal
475 * 1 << yyyy is fragsize */
477 i_frame_size = FRAME_SIZE * p_aout->output.output.i_bytes_per_frame;
478 while( i_frame_size >>= 1 )
482 i_fragments |= FRAME_COUNT << 16;
483 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
485 msg_Warn( p_aout, "cannot set fragment size (%.8x)", i_fragments );
488 if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf ) < 0 )
490 msg_Err( p_aout, "cannot get fragment size" );
491 close( p_sys->i_fd );
497 /* Number of fragments actually allocated */
498 p_aout->output.p_sys->i_fragstotal = audio_buf.fragstotal;
500 /* Maximum duration the soundcard's buffer can hold */
501 p_aout->output.p_sys->max_buffer_duration =
502 (mtime_t)audio_buf.fragstotal * audio_buf.fragsize * 1000000
503 / p_aout->output.output.i_bytes_per_frame
504 / p_aout->output.output.i_rate
505 * p_aout->output.output.i_frame_length;
507 p_aout->output.i_nb_samples = audio_buf.fragsize /
508 p_aout->output.output.i_bytes_per_frame;
511 aout_VolumeSoftInit( p_aout );
514 p_aout->output.p_sys->b_workaround_buggy_driver =
515 config_GetInt( p_aout, "oss-buggy" );
517 /* Create OSS thread and wait for its readiness. */
518 if( vlc_thread_create( p_aout, "aout", OSSThread,
519 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
521 msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
522 close( p_sys->i_fd );
530 /*****************************************************************************
531 * Play: nothing to do
532 *****************************************************************************/
533 static void Play( aout_instance_t *p_aout )
537 /*****************************************************************************
538 * Close: close the DSP audio device
539 *****************************************************************************/
540 static void Close( vlc_object_t * p_this )
542 aout_instance_t *p_aout = (aout_instance_t *)p_this;
543 struct aout_sys_t * p_sys = p_aout->output.p_sys;
545 p_aout->b_die = VLC_TRUE;
546 vlc_thread_join( p_aout );
547 p_aout->b_die = VLC_FALSE;
549 ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
550 close( p_sys->i_fd );
555 /*****************************************************************************
556 * BufferDuration: buffer status query
557 *****************************************************************************
558 * This function returns the duration in microseconds of the current buffer.
559 *****************************************************************************/
560 static mtime_t BufferDuration( aout_instance_t * p_aout )
562 struct aout_sys_t * p_sys = p_aout->output.p_sys;
563 audio_buf_info audio_buf;
566 /* Fill the audio_buf_info structure:
567 * - fragstotal: total number of fragments allocated
568 * - fragsize: size of a fragment in bytes
569 * - bytes: available space in bytes (includes partially used fragments)
570 * Note! 'bytes' could be more than fragments*fragsize */
571 ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
573 /* calculate number of available fragments (not partially used ones) */
574 i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
576 /* Return the fragment duration */
577 return (mtime_t)i_bytes * 1000000
578 / p_aout->output.output.i_bytes_per_frame
579 / p_aout->output.output.i_rate
580 * p_aout->output.output.i_frame_length;
583 /*****************************************************************************
584 * OSSThread: asynchronous thread used to DMA the data to the device
585 *****************************************************************************/
586 static int OSSThread( aout_instance_t * p_aout )
588 struct aout_sys_t * p_sys = p_aout->output.p_sys;
589 mtime_t next_date = 0;
591 while ( !p_aout->b_die )
593 aout_buffer_t * p_buffer = NULL;
597 if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
599 mtime_t buffered = BufferDuration( p_aout );
601 if( p_aout->output.p_sys->b_workaround_buggy_driver )
603 #define i_fragstotal p_aout->output.p_sys->i_fragstotal
604 /* Wait a bit - we don't want our buffer to be full */
605 if( buffered > (p_aout->output.p_sys->max_buffer_duration
606 / i_fragstotal * (i_fragstotal - 1)) )
608 msleep((p_aout->output.p_sys->max_buffer_duration
610 buffered = BufferDuration( p_aout );
615 /* Next buffer will be played at mdate() + buffered */
616 p_buffer = aout_OutputNextBuffer( p_aout, mdate() + buffered,
619 if( p_buffer == NULL &&
620 buffered > ( p_aout->output.p_sys->max_buffer_duration
621 / p_aout->output.p_sys->i_fragstotal ) )
623 /* If we have at least a fragment full, then we can wait a
624 * little and retry to get a new audio buffer instead of
625 * playing a blank sample */
626 msleep( ( p_aout->output.p_sys->max_buffer_duration
627 / p_aout->output.p_sys->i_fragstotal / 2 ) );
633 /* emu10k1 driver does not report Buffer Duration correctly in
634 * passthrough mode so we have to cheat */
641 mtime_t delay = next_date - mdate();
642 if( delay > AOUT_PTS_TOLERANCE )
648 while( !p_aout->b_die && ! ( p_buffer =
649 aout_OutputNextBuffer( p_aout, next_date, VLC_TRUE ) ) )
656 if ( p_buffer != NULL )
658 p_bytes = p_buffer->p_buffer;
659 i_size = p_buffer->i_nb_bytes;
660 /* This is theoretical ... we'll see next iteration whether
662 next_date += p_buffer->end_date - p_buffer->start_date;
666 i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
667 * p_aout->output.output.i_bytes_per_frame;
668 p_bytes = malloc( i_size );
669 memset( p_bytes, 0, i_size );
673 i_tmp = write( p_sys->i_fd, p_bytes, i_size );
677 msg_Err( p_aout, "write failed (%s)", strerror(errno) );
680 if ( p_buffer != NULL )
682 aout_BufferFree( p_buffer );