1 /*****************************************************************************
2 * oss.c : OSS /dev/dsp module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2002 VideoLAN
5 * $Id: oss.c,v 1.30 2002/10/20 12:23:47 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 /*****************************************************************************
57 * aout_sys_t: OSS audio output method descriptor
58 *****************************************************************************
59 * This structure is part of the audio output thread descriptor.
60 * It describes the dsp specific properties of an audio device.
61 *****************************************************************************/
67 /* This must be a power of 2. */
68 #define FRAME_SIZE 1024
70 #define A52_FRAME_NB 1536
72 /*****************************************************************************
74 *****************************************************************************/
75 static int Open ( vlc_object_t * );
76 static void Close ( vlc_object_t * );
78 static void Play ( aout_instance_t * );
79 static int OSSThread ( aout_instance_t * );
81 /*****************************************************************************
83 *****************************************************************************/
85 add_category_hint( N_("OSS"), NULL );
86 add_file( "dspdev", "/dev/dsp", aout_FindAndRestart,
87 N_("OSS dsp device"), NULL );
88 set_description( _("Linux OSS /dev/dsp module") );
89 set_capability( "audio output", 100 );
90 add_shortcut( "oss" );
91 set_callbacks( Open, Close );
94 /*****************************************************************************
95 * Open: open the audio device (the digital sound processor)
96 *****************************************************************************
97 * This function opens the dsp as a usual non-blocking write-only file, and
98 * modifies the p_aout->p_sys->i_fd with the file's descriptor.
99 *****************************************************************************/
100 static int Open( vlc_object_t *p_this )
102 aout_instance_t * p_aout = (aout_instance_t *)p_this;
103 struct aout_sys_t * p_sys;
106 /* Allocate structure */
107 p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
110 msg_Err( p_aout, "out of memory" );
114 /* Get device name */
115 if( (psz_device = config_GetPsz( p_aout, "dspdev" )) == NULL )
117 msg_Err( p_aout, "no audio device given (maybe /dev/dsp ?)" );
122 /* Open the sound device */
123 if( (p_sys->i_fd = open( psz_device, O_WRONLY )) < 0 )
125 msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
132 p_aout->output.pf_play = Play;
134 /* Reset the DSP device */
135 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
137 msg_Err( p_aout, "cannot reset OSS audio device" );
138 close( p_sys->i_fd );
143 /* Set the output format */
144 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
146 int i_format = AFMT_AC3;
148 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
149 || i_format != AFMT_AC3 )
151 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
155 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
156 p_aout->output.i_nb_samples = A52_FRAME_NB;
157 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
158 p_aout->output.output.i_frame_length = A52_FRAME_NB;
160 aout_VolumeNoneInit( p_aout );
164 if ( !AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
166 int i_format = AFMT_S16_NE;
167 int i_frame_size, i_fragments;
171 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
173 msg_Err( p_aout, "cannot set audio output format" );
174 close( p_sys->i_fd );
182 p_aout->output.output.i_format = VLC_FOURCC('u','8',' ',' ');
185 p_aout->output.output.i_format = VLC_FOURCC('s','8',' ',' ');
188 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','l');
191 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l');
194 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','b');
197 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','b');
200 msg_Err( p_aout, "OSS fell back to an unknown format (%d)",
202 close( p_sys->i_fd );
207 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
208 p_aout->output.i_nb_samples = FRAME_SIZE;
210 aout_VolumeSoftInit( p_aout );
212 /* Set the fragment size
213 * i_fragment = xxxxyyyy where: xxxx is fragtotal
214 * 1 << yyyy is fragsize */
216 i_frame_size = FRAME_SIZE;
217 while( i_frame_size >>= 1 )
221 i_fragments |= FRAME_COUNT << 16;
222 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
224 msg_Warn( p_aout, "cannot set fragment size (%.8x)", i_fragments );
227 /* These cases are desperate because of the OSS API and A/52 spec. */
228 switch ( p_aout->output.output.i_channels )
233 case AOUT_CHAN_STEREO | AOUT_CHAN_LFE:
234 case AOUT_CHAN_2F1R | AOUT_CHAN_LFE:
235 case AOUT_CHAN_3F1R | AOUT_CHAN_LFE:
236 case AOUT_CHAN_DOLBY | AOUT_CHAN_LFE:
237 p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
239 case AOUT_CHAN_CHANNEL | AOUT_CHAN_LFE:
240 case AOUT_CHAN_CHANNEL1 | AOUT_CHAN_LFE:
241 case AOUT_CHAN_CHANNEL2 | AOUT_CHAN_LFE:
242 case AOUT_CHAN_MONO | AOUT_CHAN_LFE:
243 case AOUT_CHAN_2F2R | AOUT_CHAN_LFE:
244 p_aout->output.output.i_channels &= ~AOUT_CHAN_LFE;
247 p_aout->output.output.i_channels = AOUT_CHAN_2F2R;
250 /* In a nutshell, possible types : AOUT_CHAN_STEREO (and al.),
251 AOUT_CHAN_2F2R, AOUT_CHAN_3F1R | AOUT_CHAN_LFE. */
253 i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
255 if ( i_nb_channels > 2 )
257 /* Check that the device supports this. */
259 #ifdef SNDCTL_DSP_GETCHANNELMASK
261 if ( ioctl( p_sys->i_fd, SNDCTL_DSP_GETCHANNELMASK,
264 if ( !(i_chanmask & DSP_BIND_FRONT) )
266 msg_Err( p_aout, "No front channels ! (%x)",
268 close( p_sys->i_fd );
273 if ( !(i_chanmask & DSP_BIND_SURR) )
275 p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
279 if ( p_aout->output.output.i_channels ==
280 (AOUT_CHAN_3F2R | AOUT_CHAN_LFE)
281 && !(i_chanmask & DSP_BIND_CENTER_LFE) )
283 p_aout->output.output.i_channels = AOUT_CHAN_2F2R;
290 /* The driver doesn't support this call, assume it is stereo. */
291 p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
296 /* Set the number of channels */
297 if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) < 0 )
299 msg_Err( p_aout, "cannot set number of audio channels (%i)",
300 p_aout->output.output.i_channels );
301 close( p_sys->i_fd );
306 if ( i_nb_channels != aout_FormatNbChannels( &p_aout->output.output ) )
308 switch ( i_nb_channels )
310 case 1: p_aout->output.output.i_channels = AOUT_CHAN_MONO; break;
311 case 2: p_aout->output.output.i_channels = AOUT_CHAN_STEREO; break;
312 case 4: p_aout->output.output.i_channels = AOUT_CHAN_2F2R; break;
314 msg_Err( p_aout, "Unsupported downmixing (%d)", i_nb_channels );
315 close( p_sys->i_fd );
321 /* Set the output rate */
322 i_rate = p_aout->output.output.i_rate;
323 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
325 msg_Err( p_aout, "cannot set audio output rate (%i)",
326 p_aout->output.output.i_rate );
327 close( p_sys->i_fd );
332 if( i_rate != p_aout->output.output.i_rate )
334 p_aout->output.output.i_rate = i_rate;
338 /* Create OSS thread and wait for its readiness. */
339 if( vlc_thread_create( p_aout, "aout", OSSThread,
340 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
342 msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
343 close( p_sys->i_fd );
352 /*****************************************************************************
353 * Play: nothing to do
354 *****************************************************************************/
355 static void Play( aout_instance_t *p_aout )
359 /*****************************************************************************
360 * Close: close the dsp audio device
361 *****************************************************************************/
362 static void Close( vlc_object_t * p_this )
364 aout_instance_t *p_aout = (aout_instance_t *)p_this;
365 struct aout_sys_t * p_sys = p_aout->output.p_sys;
367 p_aout->b_die = VLC_TRUE;
368 vlc_thread_join( p_aout );
369 p_aout->b_die = VLC_FALSE;
371 ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
372 close( p_sys->i_fd );
378 /*****************************************************************************
379 * BufferDuration: buffer status query
380 *****************************************************************************
381 * This function returns the duration in microseconds of the current buffer.
382 *****************************************************************************/
383 static mtime_t BufferDuration( aout_instance_t * p_aout )
385 struct aout_sys_t * p_sys = p_aout->output.p_sys;
386 audio_buf_info audio_buf;
389 /* Fill the audio_buf_info structure:
390 * - fragstotal: total number of fragments allocated
391 * - fragsize: size of a fragment in bytes
392 * - bytes: available space in bytes (includes partially used fragments)
393 * Note! 'bytes' could be more than fragments*fragsize */
394 ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
396 /* calculate number of available fragments (not partially used ones) */
397 i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
399 /* Return the fragment duration */
400 return (mtime_t)i_bytes * 1000000
401 / p_aout->output.output.i_bytes_per_frame
402 / p_aout->output.output.i_rate
403 * p_aout->output.output.i_frame_length;
406 /*****************************************************************************
407 * OSSThread: asynchronous thread used to DMA the data to the device
408 *****************************************************************************/
409 static int OSSThread( aout_instance_t * p_aout )
411 struct aout_sys_t * p_sys = p_aout->output.p_sys;
412 mtime_t next_date = 0;
414 while ( !p_aout->b_die )
416 aout_buffer_t * p_buffer = NULL;
420 if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
422 mtime_t buffered = BufferDuration( p_aout );
424 /* Wait a bit - we don't want our buffer to be full */
425 while( buffered > AOUT_PTS_TOLERANCE * 2 )
427 msleep( buffered / 2 - 10000 );
428 buffered = BufferDuration( p_aout );
433 /* This is the _real_ presentation date */
434 next_date = mdate() + buffered;
438 /* Give a hint to the audio output about our drift, but
439 * not too much because we want to make it happy with our
440 * nicely calculated dates. */
441 next_date = ( (next_date * 7) + (mdate() + buffered) ) / 8;
444 /* Next buffer will be played at mdate()+buffered */
445 p_buffer = aout_OutputNextBuffer( p_aout, next_date, VLC_FALSE );
449 /* emu10k1 driver does not report Buffer Duration correctly in
450 * passthrough mode so we have to cheat */
457 mtime_t delay = next_date - mdate();
458 if( delay > AOUT_PTS_TOLERANCE )
464 while( !p_aout->b_die && ! ( p_buffer =
465 aout_OutputNextBuffer( p_aout, next_date, VLC_TRUE ) ) )
472 if ( p_buffer != NULL )
474 p_bytes = p_buffer->p_buffer;
475 i_size = p_buffer->i_nb_bytes;
476 /* This is theoretical ... we'll see next iteration whether
478 next_date += p_buffer->end_date - p_buffer->start_date;
482 i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
483 * p_aout->output.output.i_bytes_per_frame;
484 p_bytes = malloc( i_size );
485 memset( p_bytes, 0, i_size );
489 i_tmp = write( p_sys->i_fd, p_bytes, i_size );
493 msg_Err( p_aout, "write failed (%s)", strerror(errno) );
496 if ( p_buffer != NULL )
498 aout_BufferFree( p_buffer );