1 /*****************************************************************************
2 * oss.c : OSS /dev/dsp module for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2002 VideoLAN
5 * $Id: oss.c,v 1.16 2002/08/24 11:46:44 sam 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
70 #define A52_FRAME_NB 1536
72 /*****************************************************************************
74 *****************************************************************************/
75 static int Open ( vlc_object_t * );
76 static void Close ( vlc_object_t * );
78 static int SetFormat ( aout_instance_t * );
79 static void Play ( aout_instance_t * );
80 static int OSSThread ( aout_instance_t * );
82 /*****************************************************************************
84 *****************************************************************************/
86 add_category_hint( N_("OSS"), NULL );
87 add_file( "dspdev", "/dev/dsp", NULL, N_("OSS dsp device"), NULL );
88 set_description( _("Linux OSS /dev/dsp module") );
89 set_capability( "audio output", 100 );
90 add_shortcut( "dsp" );
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 /* Initialize some variables */
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 /* Create OSS thread and wait for its readiness. */
133 p_sys->b_initialized = VLC_FALSE;
134 if( vlc_thread_create( p_aout, "aout", OSSThread, VLC_FALSE ) )
136 msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
137 close( p_sys->i_fd );
143 p_aout->output.pf_setformat = SetFormat;
144 p_aout->output.pf_play = Play;
149 /*****************************************************************************
150 * SetFormat: reset the dsp and set its format
151 *****************************************************************************
152 * This functions resets the DSP device, tries to initialize the output
153 * format with the value contained in the dsp structure, and if this value
154 * could not be set, the default value returned by ioctl is set. It then
155 * does the same for the stereo mode, and for the output rate.
156 *****************************************************************************/
157 static int SetFormat( aout_instance_t *p_aout )
159 struct aout_sys_t * p_sys = p_aout->output.p_sys;
165 p_sys->b_initialized = VLC_FALSE;
167 /* Reset the DSP device */
168 if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
170 msg_Err( p_aout, "cannot reset OSS audio device" );
174 /* Set the fragment size */
175 i_fragments = FRAME_COUNT << 16 | FRAME_SIZE;
176 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
178 msg_Err( p_aout, "cannot set fragment size (%.8x)", i_fragments );
182 /* Set the output format */
183 if ( p_aout->output.output.i_format == AOUT_FMT_SPDIF )
185 i_format = AOUT_FMT_SPDIF;
186 p_aout->output.i_nb_samples = A52_FRAME_NB;
187 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
188 p_aout->output.output.i_frame_length = A52_FRAME_NB;
192 p_aout->output.output.i_format = i_format = AOUT_FMT_S16_NE;
193 p_aout->output.i_nb_samples = FRAME_SIZE;
196 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
197 || i_format != p_aout->output.output.i_format )
199 msg_Err( p_aout, "cannot set audio output format (%i)", i_format );
203 if ( p_aout->output.output.i_format != AOUT_FMT_SPDIF )
206 if ( p_aout->output.output.i_channels > 2 )
208 msg_Warn( p_aout, "only two channels are supported at the moment" );
209 /* Trigger downmixing */
210 p_aout->output.output.i_channels = 2;
213 /* Set the number of channels */
214 b_stereo = p_aout->output.output.i_channels - 1;
216 if( ioctl( p_sys->i_fd, SNDCTL_DSP_STEREO, &b_stereo ) < 0 )
218 msg_Err( p_aout, "cannot set number of audio channels (%i)",
219 p_aout->output.output.i_channels );
223 if ( b_stereo + 1 != p_aout->output.output.i_channels )
225 msg_Warn( p_aout, "driver forced up/downmixing %li->%li",
226 p_aout->output.output.i_channels,
228 p_aout->output.output.i_channels = b_stereo + 1;
231 /* Set the output rate */
232 i_rate = p_aout->output.output.i_rate;
233 if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
235 msg_Err( p_aout, "cannot set audio output rate (%i)",
236 p_aout->output.output.i_rate );
240 if( i_rate != p_aout->output.output.i_rate )
242 msg_Warn( p_aout, "driver forced resampling %li->%li",
243 p_aout->output.output.i_rate, i_rate );
244 p_aout->output.output.i_rate = i_rate;
248 p_sys->b_initialized = VLC_TRUE;
253 /*****************************************************************************
254 * Play: queue a buffer for playing by OSSThread
255 *****************************************************************************/
256 static void Play( aout_instance_t *p_aout )
260 /*****************************************************************************
261 * Close: close the dsp audio device
262 *****************************************************************************/
263 static void Close( vlc_object_t * p_this )
265 aout_instance_t *p_aout = (aout_instance_t *)p_this;
266 struct aout_sys_t * p_sys = p_aout->output.p_sys;
268 p_aout->b_die = VLC_TRUE;
269 vlc_thread_join( p_aout );
270 p_aout->b_die = VLC_FALSE;
272 ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
273 close( p_sys->i_fd );
279 /*****************************************************************************
280 * GetBufInfo: buffer status query
281 *****************************************************************************
282 * This function fills in the audio_buf_info structure :
283 * - returns : number of available fragments (not partially used ones)
284 * - int fragstotal : total number of fragments allocated
285 * - int fragsize : size of a fragment in bytes
286 * - int bytes : available space in bytes (includes partially used fragments)
287 * Note! 'bytes' could be more than fragments*fragsize
288 *****************************************************************************/
289 static int GetBufInfo( aout_instance_t * p_aout )
291 struct aout_sys_t * p_sys = p_aout->output.p_sys;
292 audio_buf_info audio_buf;
294 ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
296 /* returns the allocated space in bytes */
297 return ( (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes );
300 /*****************************************************************************
301 * OSSThread: asynchronous thread used to DMA the data to the device
302 *****************************************************************************/
303 static int OSSThread( aout_instance_t * p_aout )
305 struct aout_sys_t * p_sys = p_aout->output.p_sys;
307 while ( !p_aout->b_die )
309 aout_buffer_t * p_buffer;
313 if( !p_sys->b_initialized )
315 msleep( THREAD_SLEEP );
319 if ( p_aout->output.output.i_format != AOUT_FMT_SPDIF )
321 mtime_t buffered = (mtime_t)GetBufInfo( p_aout ) * 1000000
322 / p_aout->output.output.i_bytes_per_frame
323 / p_aout->output.output.i_rate
324 * p_aout->output.output.i_frame_length;
326 /* Next buffer will be played at mdate()+buffered, and we tell
327 * the audio output that it can wait for a new packet for
328 * buffered/2 microseconds. */
329 p_buffer = aout_OutputNextBuffer( p_aout, mdate() + buffered,
330 buffered / 2, VLC_FALSE );
334 p_buffer = aout_OutputNextBuffer( p_aout, 0, 0, VLC_TRUE );
337 if ( p_buffer != NULL )
339 p_bytes = p_buffer->p_buffer;
340 i_size = p_buffer->i_nb_bytes;
344 i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
345 * p_aout->output.output.i_bytes_per_frame;
346 p_bytes = malloc( i_size );
347 memset( p_bytes, 0, i_size );
350 i_tmp = write( p_sys->i_fd, p_bytes, i_size );
354 msg_Err( p_aout, "write failed (%s)", strerror(errno) );
357 if ( p_buffer != NULL )
359 aout_BufferFree( p_buffer );