1 /*****************************************************************************
2 * oss.c : OSS /dev/dsp module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2002 VideoLAN
5 * $Id: oss.c,v 1.11 2002/08/19 21:31:11 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 *****************************************************************************/
65 volatile vlc_bool_t b_initialized;
68 #define FRAME_SIZE 2048
69 #define A52_FRAME_NB 1536
71 /*****************************************************************************
73 *****************************************************************************/
74 static int Open ( vlc_object_t * );
75 static void Close ( vlc_object_t * );
77 static int SetFormat ( aout_instance_t * );
78 static void Play ( aout_instance_t * );
79 static int OSSThread ( aout_instance_t * );
81 /*****************************************************************************
83 *****************************************************************************/
85 add_category_hint( N_("Audio"), NULL );
86 add_file( "dspdev", "/dev/dsp", NULL, N_("OSS dsp device"), NULL );
87 set_description( _("Linux OSS /dev/dsp module") );
88 set_capability( "audio output", 100 );
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;
104 /* Allocate structure */
105 p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
108 msg_Err( p_aout, "out of memory" );
112 /* Initialize some variables */
113 if( (psz_device = config_GetPsz( p_aout, "dspdev" )) == NULL )
115 msg_Err( p_aout, "no audio device given (maybe /dev/dsp ?)" );
120 /* Open the sound device */
121 if( (p_sys->i_fd = open( psz_device, O_WRONLY )) < 0 )
123 msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
130 /* Create OSS thread and wait for its readiness. */
131 p_sys->b_initialized = VLC_FALSE;
132 if( vlc_thread_create( p_aout, "aout", OSSThread, VLC_FALSE ) )
134 msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
140 p_aout->output.pf_setformat = SetFormat;
141 p_aout->output.pf_play = Play;
146 /*****************************************************************************
147 * SetFormat: reset the dsp and set its format
148 *****************************************************************************
149 * This functions resets the DSP device, tries to initialize the output
150 * format with the value contained in the dsp structure, and if this value
151 * could not be set, the default value returned by ioctl is set. It then
152 * does the same for the stereo mode, and for the output rate.
153 *****************************************************************************/
154 static int SetFormat( aout_instance_t *p_aout )
156 struct aout_sys_t * p_sys = p_aout->output.p_sys;
161 p_sys->b_initialized = VLC_FALSE;
163 /* Reset the DSP device */
164 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
166 msg_Err( p_aout, "cannot reset OSS audio device" );
170 /* Set the output format */
171 if ( p_aout->output.output.i_format == AOUT_FMT_SPDIF )
173 p_aout->output.i_nb_samples = A52_FRAME_NB;
174 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
175 p_aout->output.output.i_frame_length = A52_FRAME_NB;
179 p_aout->output.output.i_format = i_format = AOUT_FMT_S16_NE;
180 p_aout->output.i_nb_samples = FRAME_SIZE;
183 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
184 || i_format != p_aout->output.output.i_format )
186 msg_Err( p_aout, "cannot set audio output format (%i)",
191 if ( p_aout->output.output.i_format != AOUT_FMT_SPDIF )
194 if ( p_aout->output.output.i_channels > 2 )
196 msg_Warn( p_aout, "only two channels are supported at the moment" );
197 /* Trigger downmixing */
198 p_aout->output.output.i_channels = 2;
201 /* Set the number of channels */
202 b_stereo = p_aout->output.output.i_channels - 1;
204 if( ioctl( p_sys->i_fd, SNDCTL_DSP_STEREO, &b_stereo ) < 0 )
206 msg_Err( p_aout, "cannot set number of audio channels (%i)",
207 p_aout->output.output.i_channels );
211 if ( b_stereo + 1 != p_aout->output.output.i_channels )
213 msg_Warn( p_aout, "driver forced up/downmixing %li->%li",
214 p_aout->output.output.i_channels,
216 p_aout->output.output.i_channels = b_stereo + 1;
219 /* Set the output rate */
220 i_rate = p_aout->output.output.i_rate;
221 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
223 msg_Err( p_aout, "cannot set audio output rate (%i)",
224 p_aout->output.output.i_rate );
228 if( i_rate != p_aout->output.output.i_rate )
230 msg_Warn( p_aout, "driver forced resampling %li->%li",
231 p_aout->output.output.i_rate, i_rate );
232 p_aout->output.output.i_rate = i_rate;
236 p_sys->b_initialized = VLC_TRUE;
241 /*****************************************************************************
242 * Play: queue a buffer for playing by OSSThread
243 *****************************************************************************/
244 static void Play( aout_instance_t *p_aout )
248 /*****************************************************************************
249 * Close: close the dsp audio device
250 *****************************************************************************/
251 static void Close( vlc_object_t * p_this )
253 aout_instance_t *p_aout = (aout_instance_t *)p_this;
254 struct aout_sys_t * p_sys = p_aout->output.p_sys;
257 vlc_thread_join( p_aout );
259 close( p_sys->i_fd );
264 /*****************************************************************************
265 * GetBufInfo: buffer status query
266 *****************************************************************************
267 * This function fills in the audio_buf_info structure :
268 * - returns : number of available fragments (not partially used ones)
269 * - int fragstotal : total number of fragments allocated
270 * - int fragsize : size of a fragment in bytes
271 * - int bytes : available space in bytes (includes partially used fragments)
272 * Note! 'bytes' could be more than fragments*fragsize
273 *****************************************************************************/
274 static int GetBufInfo( aout_instance_t * p_aout )
276 struct aout_sys_t * p_sys = p_aout->output.p_sys;
277 audio_buf_info audio_buf;
279 ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
281 /* returns the allocated space in bytes */
282 return ( (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes );
285 /*****************************************************************************
286 * OSSThread: asynchronous thread used to DMA the data to the device
287 *****************************************************************************/
288 static int OSSThread( aout_instance_t * p_aout )
290 struct aout_sys_t * p_sys = p_aout->output.p_sys;
292 while ( !p_aout->b_die )
294 aout_buffer_t * p_buffer;
298 if( !p_sys->b_initialized )
300 msleep( THREAD_SLEEP );
304 if ( p_aout->output.output.i_format != AOUT_FMT_SPDIF )
306 mtime_t next_date = 0;
307 /* Get the presentation date of the next write() operation. It
308 * is equal to the current date + duration of buffered samples.
309 * Order is important here, since GetBufInfo is believed to take
310 * more time than mdate(). */
311 next_date = (mtime_t)GetBufInfo( p_aout ) * 1000000
312 / aout_FormatToByterate( &p_aout->output.output );
313 next_date += mdate();
315 p_buffer = aout_OutputNextBuffer( p_aout, next_date, 0 );
319 p_buffer = aout_OutputNextBuffer( p_aout, 0, 1 );
322 if ( p_buffer != NULL )
324 p_bytes = p_buffer->p_buffer;
325 i_size = p_buffer->i_nb_bytes;
329 i_size = aout_FormatToByterate( &p_aout->output.output )
331 / p_aout->output.output.i_rate;
332 p_bytes = alloca( i_size );
333 memset( p_bytes, 0, i_size );
336 i_tmp = write( p_sys->i_fd, p_bytes, i_size );
340 msg_Err( p_aout, "write failed (%s)", strerror(errno) );
343 if ( p_buffer != NULL )
345 aout_BufferFree( p_buffer );