1 /*****************************************************************************
2 * aout_sdl.c : audio sdl functions library
3 *****************************************************************************
4 * Copyright (C) 1999, 2000 VideoLAN
5 * $Id: aout_sdl.c,v 1.10 2001/03/21 13:42:34 sam Exp $
7 * Authors: Michel Kaempf <maxx@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.org>
9 * Pierre Baillet <oct@zoy.org>
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 #define MODULE_NAME sdl
27 #include "modules_inner.h"
29 /*****************************************************************************
31 *****************************************************************************/
34 #include <errno.h> /* ENOMEM */
35 #include <fcntl.h> /* open(), O_WRONLY */
36 #include <sys/ioctl.h> /* ioctl() */
37 #include <string.h> /* strerror() */
38 #include <unistd.h> /* write(), close() */
39 #include <stdio.h> /* "intf_msg.h" */
40 #include <stdlib.h> /* calloc(), malloc(), free() */
42 #include <SDL/SDL.h> /* SDL base include */
45 #include "common.h" /* boolean_t, byte_t */
50 #include "audio_output.h" /* aout_thread_t */
52 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
57 /*****************************************************************************
58 * aout_sys_t: dsp audio output method descriptor
59 *****************************************************************************
60 * This structure is part of the audio output thread descriptor.
61 * It describes the dsp specific properties of an audio device.
62 *****************************************************************************/
64 /* the overflow limit is used to prevent the fifo from growing too big */
65 #define OVERFLOWLIMIT 100000
67 typedef struct aout_sys_s
76 /*****************************************************************************
78 *****************************************************************************/
79 static int aout_Probe ( probedata_t *p_data );
80 static int aout_Open ( aout_thread_t *p_aout );
81 static int aout_SetFormat ( aout_thread_t *p_aout );
82 static long aout_GetBufInfo ( aout_thread_t *p_aout, long l_buffer_info );
83 static void aout_Play ( aout_thread_t *p_aout,
84 byte_t *buffer, int i_size );
85 static void aout_Close ( aout_thread_t *p_aout );
87 static void aout_SDLCallback ( void *userdata, Uint8 *stream, int len );
89 /*****************************************************************************
90 * Functions exported as capabilities. They are declared as static so that
91 * we don't pollute the namespace too much.
92 *****************************************************************************/
93 void _M( aout_getfunctions )( function_list_t * p_function_list )
95 p_function_list->pf_probe = aout_Probe;
96 p_function_list->functions.aout.pf_open = aout_Open;
97 p_function_list->functions.aout.pf_setformat = aout_SetFormat;
98 p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
99 p_function_list->functions.aout.pf_play = aout_Play;
100 p_function_list->functions.aout.pf_close = aout_Close;
103 /*****************************************************************************
104 * aout_Probe: probe the audio device and return a score
105 *****************************************************************************
106 * This function tries to initialize SDL audio and returns a score to the
107 * plugin manager so that it can select the best plugin.
108 *****************************************************************************/
109 static int aout_Probe( probedata_t *p_data )
111 SDL_AudioSpec desired, obtained;
115 if( SDL_Init(SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE) != 0 )
117 intf_DbgMsg( "aout: SDL_Init failed (%s)", SDL_GetError() );
121 desired.freq = 11025; /* frequency */
122 desired.format = AUDIO_U8; /* unsigned 8 bits */
123 desired.channels = 2; /* mono */
124 desired.callback = NULL; /* no callback function yet */
125 desired.userdata = NULL; /* null parm for callback */
126 desired.samples = 4096;
128 /* If we were unable to open the device, there is no way we can use
129 * the plugin. Return a score of 0. */
130 if( SDL_OpenAudio( &desired, &obtained ) < 0 )
132 intf_DbgMsg( "aout: SDL_OpenAudio failed (%s)", SDL_GetError() );
136 /* Otherwise, there are good chances we can use this plugin, return 100. */
139 if( TestMethod( AOUT_METHOD_VAR, "sdl" ) )
147 /*****************************************************************************
148 * aout_Open: open the audio device
149 *****************************************************************************
150 * This function opens the dsp as a usual non-blocking write-only file, and
151 * modifies the p_aout->i_fd with the file's descriptor.
152 *****************************************************************************/
153 static int aout_Open( aout_thread_t *p_aout )
155 SDL_AudioSpec desired;
156 int i_channels = p_aout->b_stereo ? 2 : 1;
158 /* Allocate structure */
159 p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
161 if( p_aout->p_sys == NULL )
163 intf_ErrMsg( "aout error: %s", strerror(ENOMEM) );
167 p_aout->p_sys->i_audio_end = 0;
168 p_aout->p_sys->audio_buf = malloc( OVERFLOWLIMIT );
170 /* Initialize some variables */
171 p_aout->psz_device = 0;
172 p_aout->i_format = AOUT_FORMAT_DEFAULT;
173 p_aout->i_channels = 1 + main_GetIntVariable( AOUT_STEREO_VAR,
174 AOUT_STEREO_DEFAULT );
175 p_aout->l_rate = main_GetIntVariable( AOUT_RATE_VAR,
178 desired.freq = p_aout->l_rate;
180 /* TODO: write conversion beetween AOUT_FORMAT_DEFAULT
181 * AND AUDIO* from SDL. */
182 desired.format = AUDIO_S16LSB; /* stereo 16 bits */
183 desired.channels = i_channels;
184 desired.callback = aout_SDLCallback;
185 desired.userdata = p_aout->p_sys;
186 desired.samples = 1024;
188 /* Open the sound device
189 * we just ask the SDL to wrap at the good frequency if the one we
190 * ask for is unavailable. This is done by setting the second parar
193 if( SDL_OpenAudio( &desired, NULL ) < 0 )
195 intf_ErrMsg( "aout error: SDL_OpenAudio failed (%s)", SDL_GetError() );
199 p_aout->p_sys->b_active = 1;
205 /*****************************************************************************
206 * aout_SetFormat: reset the audio device and sets its format
207 *****************************************************************************
208 * This functions resets the audio device, tries to initialize the output
209 * format with the value contained in the dsp structure, and if this value
210 * could not be set, the default value returned by ioctl is set. It then
211 * does the same for the stereo mode, and for the output rate.
212 *****************************************************************************/
213 static int aout_SetFormat( aout_thread_t *p_aout )
215 /* TODO: finish and clean this */
216 SDL_AudioSpec desired;
217 int i_stereo = p_aout->b_stereo ? 2 : 1;
219 /*i_format = p_aout->i_format;*/
220 desired.freq = p_aout->l_rate; /* Set the output rate */
221 desired.format = AUDIO_S16LSB; /* stereo 16 bits */
222 desired.channels = i_stereo;
223 desired.callback = aout_SDLCallback;
224 desired.userdata = p_aout->p_sys;
225 desired.samples = 2048;
227 /* Open the sound device */
231 if( SDL_OpenAudio( &desired, NULL ) < 0 )
233 p_aout->p_sys->b_active = 0;
237 p_aout->p_sys->b_active = 1;
243 /*****************************************************************************
244 * aout_GetBufInfo: buffer status query
245 *****************************************************************************
246 * returns the number of bytes in the audio buffer compared to the size of
248 *****************************************************************************/
249 static long aout_GetBufInfo( aout_thread_t *p_aout, long l_buffer_limit )
251 if(l_buffer_limit > p_aout->p_sys->i_audio_end)
253 /* returning 0 here juste gives awful sound in the speakers :/ */
254 return( l_buffer_limit );
256 return( p_aout->p_sys->i_audio_end - l_buffer_limit);
259 /*****************************************************************************
260 * aout_Play: play a sound samples buffer
261 *****************************************************************************
262 * This function writes a buffer of i_length bytes in the dsp
263 *****************************************************************************/
264 static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
266 byte_t * audio_buf = p_aout->p_sys->audio_buf;
268 SDL_LockAudio(); /* Stop callbacking */
270 p_aout->p_sys->audio_buf = realloc( audio_buf,
271 p_aout->p_sys->i_audio_end + i_size);
272 memcpy( p_aout->p_sys->audio_buf + p_aout->p_sys->i_audio_end,
275 p_aout->p_sys->i_audio_end += i_size;
277 SDL_UnlockAudio(); /* go on callbacking */
280 /*****************************************************************************
281 * aout_Close: close the audio device
282 *****************************************************************************/
283 static void aout_Close( aout_thread_t *p_aout )
285 if( p_aout->p_sys->b_active )
287 SDL_PauseAudio( 1 ); /* pause audio */
289 if( p_aout->p_sys->audio_buf != NULL ) /* do we have a buffer now ? */
291 free( p_aout->p_sys->audio_buf );
297 free( p_aout->p_sys ); /* Close the Output. */
300 /*****************************************************************************
301 * aout_SDLCallback: what to do once SDL has played sound samples
302 *****************************************************************************/
303 static void aout_SDLCallback( void *userdata, byte_t *stream, int len )
305 struct aout_sys_s * p_sys = userdata;
307 if( p_sys->i_audio_end > OVERFLOWLIMIT )
309 intf_ErrMsg( "aout error: aout_SDLCallback overflowed" );
311 free( p_sys->audio_buf );
312 p_sys->audio_buf = NULL;
314 p_sys->i_audio_end = 0;
315 /* we've gone to slow, increase output freq */
318 /* if we are not in underrun */
319 if( p_sys->i_audio_end > len )
321 p_sys->i_audio_end -= len;
322 memcpy( stream, p_sys->audio_buf, len );
323 memmove( p_sys->audio_buf, p_sys->audio_buf + len, p_sys->i_audio_end );