1 /*****************************************************************************
2 * oss.c : OSS /dev/dsp module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2002 VideoLAN
5 * $Id: oss.c,v 1.29 2002/10/02 15:37:58 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 /*****************************************************************************
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 #define FRAME_SIZE 1024
69 #define A52_FRAME_NB 1536
71 /*****************************************************************************
73 *****************************************************************************/
74 static int Open ( vlc_object_t * );
75 static void Close ( vlc_object_t * );
77 static void Play ( aout_instance_t * );
78 static int OSSThread ( aout_instance_t * );
80 /*****************************************************************************
82 *****************************************************************************/
84 add_category_hint( N_("OSS"), NULL );
85 add_file( "dspdev", "/dev/dsp", NULL, N_("OSS dsp device"), NULL );
86 set_description( _("Linux OSS /dev/dsp module") );
87 set_capability( "audio output", 100 );
88 add_shortcut( "dsp" );
89 set_callbacks( Open, Close );
92 /*****************************************************************************
93 * Open: open the audio device (the digital sound processor)
94 *****************************************************************************
95 * This function opens the dsp as a usual non-blocking write-only file, and
96 * modifies the p_aout->p_sys->i_fd with the file's descriptor.
97 *****************************************************************************/
98 static int Open( vlc_object_t *p_this )
100 aout_instance_t * p_aout = (aout_instance_t *)p_this;
101 struct aout_sys_t * p_sys;
103 int i_format, i_format_orig;
109 /* Allocate structure */
110 p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
113 msg_Err( p_aout, "out of memory" );
117 /* Initialize some variables */
118 if( (psz_device = config_GetPsz( p_aout, "dspdev" )) == NULL )
120 msg_Err( p_aout, "no audio device given (maybe /dev/dsp ?)" );
125 /* Open the sound device */
126 if( (p_sys->i_fd = open( psz_device, O_WRONLY )) < 0 )
128 msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
135 p_aout->output.pf_play = Play;
137 /* Reset the DSP device */
138 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
140 msg_Err( p_aout, "cannot reset OSS audio device" );
144 /* Set the fragment size
145 * i_fragment = xxxxyyyy where: xxxx is fragtotal
146 * 1 << yyyy is fragsize */
148 i_frame_size = FRAME_SIZE;
149 while( i_frame_size >>= 1 )
153 i_fragments |= FRAME_COUNT << 16;
154 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
156 msg_Err( p_aout, "cannot set fragment size (%.8x)", i_fragments );
160 /* Set the output format */
161 if ( p_aout->output.output.i_format == VLC_FOURCC('s','p','d','i') )
164 i_format_orig = AFMT_AC3;
165 p_aout->output.i_nb_samples = A52_FRAME_NB;
166 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
167 p_aout->output.output.i_frame_length = A52_FRAME_NB;
169 aout_VolumeNoneInit( p_aout );
173 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
174 i_format = AFMT_S16_NE;
175 i_format_orig = AFMT_S16_NE;
176 p_aout->output.i_nb_samples = FRAME_SIZE;
178 aout_VolumeSoftInit( p_aout );
181 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
182 || i_format != i_format_orig )
184 if ( i_format_orig == AFMT_AC3 )
187 msg_Warn( p_aout, "cannot set audio output format (%i)", i_format_orig );
188 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
189 i_format = AFMT_S16_NE;
190 i_format_orig = AFMT_S16_NE;
191 p_aout->output.i_nb_samples = FRAME_SIZE;
192 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
193 || i_format != i_format_orig )
195 msg_Err( p_aout, "cannot set audio output format (%i)",
202 msg_Err( p_aout, "cannot set audio output format (%i)", i_format_orig );
207 if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
210 if ( p_aout->output.output.i_channels > 2 )
212 msg_Warn( p_aout, "only two channels are supported at the moment" );
213 /* Trigger downmixing */
214 p_aout->output.output.i_channels = 2;
217 /* Set the number of channels */
218 b_stereo = p_aout->output.output.i_channels - 1;
220 if( ioctl( p_sys->i_fd, SNDCTL_DSP_STEREO, &b_stereo ) < 0 )
222 msg_Err( p_aout, "cannot set number of audio channels (%i)",
223 p_aout->output.output.i_channels );
227 if ( b_stereo + 1 != p_aout->output.output.i_channels )
229 msg_Warn( p_aout, "driver forced up/downmixing %li->%li",
230 p_aout->output.output.i_channels,
232 p_aout->output.output.i_channels = b_stereo + 1;
235 /* Set the output rate */
236 i_rate = p_aout->output.output.i_rate;
237 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
239 msg_Err( p_aout, "cannot set audio output rate (%i)",
240 p_aout->output.output.i_rate );
244 if( i_rate != p_aout->output.output.i_rate )
246 msg_Warn( p_aout, "driver forced resampling %li->%li",
247 p_aout->output.output.i_rate, i_rate );
248 p_aout->output.output.i_rate = i_rate;
252 /* Create OSS thread and wait for its readiness. */
253 if( vlc_thread_create( p_aout, "aout", OSSThread,
254 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
256 msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
257 close( p_sys->i_fd );
266 /*****************************************************************************
267 * Play: nothing to do
268 *****************************************************************************/
269 static void Play( aout_instance_t *p_aout )
273 /*****************************************************************************
274 * Close: close the dsp audio device
275 *****************************************************************************/
276 static void Close( vlc_object_t * p_this )
278 aout_instance_t *p_aout = (aout_instance_t *)p_this;
279 struct aout_sys_t * p_sys = p_aout->output.p_sys;
281 p_aout->b_die = VLC_TRUE;
282 vlc_thread_join( p_aout );
283 p_aout->b_die = VLC_FALSE;
285 ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
286 close( p_sys->i_fd );
292 /*****************************************************************************
293 * BufferDuration: buffer status query
294 *****************************************************************************
295 * This function returns the duration in microseconds of the current buffer.
296 *****************************************************************************/
297 static mtime_t BufferDuration( aout_instance_t * p_aout )
299 struct aout_sys_t * p_sys = p_aout->output.p_sys;
300 audio_buf_info audio_buf;
303 /* Fill the audio_buf_info structure:
304 * - fragstotal: total number of fragments allocated
305 * - fragsize: size of a fragment in bytes
306 * - bytes: available space in bytes (includes partially used fragments)
307 * Note! 'bytes' could be more than fragments*fragsize */
308 ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
310 /* calculate number of available fragments (not partially used ones) */
311 i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
313 /* Return the fragment duration */
314 return (mtime_t)i_bytes * 1000000
315 / p_aout->output.output.i_bytes_per_frame
316 / p_aout->output.output.i_rate
317 * p_aout->output.output.i_frame_length;
320 /*****************************************************************************
321 * OSSThread: asynchronous thread used to DMA the data to the device
322 *****************************************************************************/
323 static int OSSThread( aout_instance_t * p_aout )
325 struct aout_sys_t * p_sys = p_aout->output.p_sys;
326 mtime_t next_date = 0;
328 while ( !p_aout->b_die )
330 aout_buffer_t * p_buffer = NULL;
334 if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
336 mtime_t buffered = BufferDuration( p_aout );
338 /* Wait a bit - we don't want our buffer to be full */
339 while( buffered > AOUT_PTS_TOLERANCE * 2 )
341 msleep( buffered / 2 - 10000 );
342 buffered = BufferDuration( p_aout );
347 /* This is the _real_ presentation date */
348 next_date = mdate() + buffered;
352 /* Give a hint to the audio output about our drift, but
353 * not too much because we want to make it happy with our
354 * nicely calculated dates. */
355 next_date = ( (next_date * 7) + (mdate() + buffered) ) / 8;
358 /* Next buffer will be played at mdate()+buffered */
359 p_buffer = aout_OutputNextBuffer( p_aout, next_date, VLC_FALSE );
363 /* emu10k1 driver does not report Buffer Duration correctly in
364 * passthrough mode so we have to cheat */
371 mtime_t delay = next_date - mdate();
372 if( delay > AOUT_PTS_TOLERANCE )
378 while( !p_aout->b_die && ! ( p_buffer =
379 aout_OutputNextBuffer( p_aout, next_date, VLC_TRUE ) ) )
386 if ( p_buffer != NULL )
388 p_bytes = p_buffer->p_buffer;
389 i_size = p_buffer->i_nb_bytes;
390 /* This is theoretical ... we'll see next iteration whether
392 next_date += p_buffer->end_date - p_buffer->start_date;
396 i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
397 * p_aout->output.output.i_bytes_per_frame;
398 p_bytes = malloc( i_size );
399 memset( p_bytes, 0, i_size );
403 i_tmp = write( p_sys->i_fd, p_bytes, i_size );
407 msg_Err( p_aout, "write failed (%s)", strerror(errno) );
410 if ( p_buffer != NULL )
412 aout_BufferFree( p_buffer );