1 /*****************************************************************************
2 * aout_sdl.c : audio sdl functions library
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: aout_sdl.c,v 1.30 2002/07/20 18:01:43 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 /*****************************************************************************
28 *****************************************************************************/
29 #include <errno.h> /* ENOMEM */
30 #include <fcntl.h> /* open(), O_WRONLY */
31 #include <string.h> /* strerror() */
32 #include <unistd.h> /* write(), close() */
33 #include <stdlib.h> /* calloc(), malloc(), free() */
38 #include SDL_INCLUDE_FILE
40 /*****************************************************************************
41 * aout_sys_t: dsp audio output method descriptor
42 *****************************************************************************
43 * This structure is part of the audio output thread descriptor.
44 * It describes the dsp specific properties of an audio device.
45 *****************************************************************************/
47 /* the overflow limit is used to prevent the fifo from growing too big */
48 #define OVERFLOWLIMIT 100000
58 /*****************************************************************************
60 *****************************************************************************/
61 static int aout_Open ( aout_thread_t *p_aout );
62 static int aout_SetFormat ( aout_thread_t *p_aout );
63 static int aout_GetBufInfo ( aout_thread_t *p_aout, int i_buffer_info );
64 static void aout_Play ( aout_thread_t *p_aout,
65 byte_t *buffer, int i_size );
66 static void aout_Close ( aout_thread_t *p_aout );
68 static void aout_SDLCallback ( void *userdata, Uint8 *stream, int len );
70 /*****************************************************************************
71 * Functions exported as capabilities. They are declared as static so that
72 * we don't pollute the namespace too much.
73 *****************************************************************************/
74 void _M( aout_getfunctions )( function_list_t * p_function_list )
76 p_function_list->functions.aout.pf_open = aout_Open;
77 p_function_list->functions.aout.pf_setformat = aout_SetFormat;
78 p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
79 p_function_list->functions.aout.pf_play = aout_Play;
80 p_function_list->functions.aout.pf_close = aout_Close;
83 /*****************************************************************************
84 * aout_Open: open the audio device
85 *****************************************************************************
86 * This function opens the dsp as a usual non-blocking write-only file, and
87 * modifies the p_aout->i_fd with the file's descriptor.
88 *****************************************************************************/
89 static int aout_Open( aout_thread_t *p_aout )
91 SDL_AudioSpec desired;
93 if( SDL_WasInit( SDL_INIT_AUDIO ) != 0 )
98 /* Allocate structure */
99 p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
101 if( p_aout->p_sys == NULL )
103 msg_Err( p_aout, "out of memory" );
107 /* Initialize library */
108 if( SDL_Init( SDL_INIT_AUDIO
110 /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet*/
111 | SDL_INIT_EVENTTHREAD
114 /* In debug mode you may want vlc to dump a core instead of staying
116 | SDL_INIT_NOPARACHUTE
120 msg_Err( p_aout, "cannot initialize SDL (%s)", SDL_GetError() );
121 free( p_aout->p_sys );
125 p_aout->p_sys->i_audio_end = 0;
126 p_aout->p_sys->audio_buf = malloc( OVERFLOWLIMIT );
128 /* Initialize some variables */
130 /* TODO: write conversion beetween AOUT_FORMAT_DEFAULT
131 * AND AUDIO* from SDL. */
132 desired.freq = p_aout->i_rate;
133 #ifdef WORDS_BIGENDIAN
134 desired.format = AUDIO_S16MSB; /* stereo 16 bits */
136 desired.format = AUDIO_S16LSB; /* stereo 16 bits */
138 desired.channels = p_aout->i_channels;
139 desired.callback = aout_SDLCallback;
140 desired.userdata = p_aout->p_sys;
141 desired.samples = 1024;
143 /* Open the sound device
144 * we just ask the SDL to wrap at the good frequency if the one we
145 * ask for is unavailable. This is done by setting the second parar
148 if( SDL_OpenAudio( &desired, NULL ) < 0 )
150 msg_Err( p_aout, "SDL_OpenAudio failed (%s)", SDL_GetError() );
151 SDL_QuitSubSystem( SDL_INIT_AUDIO );
152 free( p_aout->p_sys );
156 p_aout->p_sys->b_active = 1;
162 /*****************************************************************************
163 * aout_SetFormat: reset the audio device and sets its format
164 *****************************************************************************
165 * This functions resets the audio device, tries to initialize the output
166 * format with the value contained in the dsp structure, and if this value
167 * could not be set, the default value returned by ioctl is set. It then
168 * does the same for the stereo mode, and for the output rate.
169 *****************************************************************************/
170 static int aout_SetFormat( aout_thread_t *p_aout )
172 /* TODO: finish and clean this */
173 SDL_AudioSpec desired;
175 /*i_format = p_aout->i_format;*/
176 desired.freq = p_aout->i_rate; /* Set the output rate */
177 #ifdef WORDS_BIGENDIAN
178 desired.format = AUDIO_S16MSB; /* stereo 16 bits */
180 desired.format = AUDIO_S16LSB; /* stereo 16 bits */
182 desired.channels = p_aout->i_channels;
183 desired.callback = aout_SDLCallback;
184 desired.userdata = p_aout->p_sys;
185 desired.samples = 2048;
187 /* Open the sound device */
191 if( SDL_OpenAudio( &desired, NULL ) < 0 )
193 p_aout->p_sys->b_active = 0;
197 p_aout->p_sys->b_active = 1;
203 /*****************************************************************************
204 * aout_GetBufInfo: buffer status query
205 *****************************************************************************
206 * returns the number of bytes in the audio buffer compared to the size of
208 *****************************************************************************/
209 static int aout_GetBufInfo( aout_thread_t *p_aout, int i_buffer_limit )
211 if(i_buffer_limit > p_aout->p_sys->i_audio_end)
213 /* returning 0 here juste gives awful sound in the speakers :/ */
214 return( i_buffer_limit );
216 return( p_aout->p_sys->i_audio_end - i_buffer_limit);
219 /*****************************************************************************
220 * aout_Play: play a sound samples buffer
221 *****************************************************************************
222 * This function writes a buffer of i_length bytes in the dsp
223 *****************************************************************************/
224 static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
226 byte_t * audio_buf = p_aout->p_sys->audio_buf;
228 SDL_LockAudio(); /* Stop callbacking */
230 p_aout->p_sys->audio_buf = realloc( audio_buf,
231 p_aout->p_sys->i_audio_end + i_size);
232 memcpy( p_aout->p_sys->audio_buf + p_aout->p_sys->i_audio_end,
235 p_aout->p_sys->i_audio_end += i_size;
237 SDL_UnlockAudio(); /* go on callbacking */
240 /*****************************************************************************
241 * aout_Close: close the audio device
242 *****************************************************************************/
243 static void aout_Close( aout_thread_t *p_aout )
245 if( p_aout->p_sys->b_active )
247 SDL_PauseAudio( 1 ); /* pause audio */
249 if( p_aout->p_sys->audio_buf != NULL ) /* do we have a buffer now ? */
251 free( p_aout->p_sys->audio_buf );
257 SDL_QuitSubSystem( SDL_INIT_AUDIO );
259 free( p_aout->p_sys ); /* Close the Output. */
262 /*****************************************************************************
263 * aout_SDLCallback: what to do once SDL has played sound samples
264 *****************************************************************************/
265 static void aout_SDLCallback( void *userdata, byte_t *stream, int len )
267 aout_sys_t * p_sys = userdata;
269 if( p_sys->i_audio_end > OVERFLOWLIMIT )
271 //X msg_Err( p_aout, "aout_SDLCallback overflowed" );
273 free( p_sys->audio_buf );
274 p_sys->audio_buf = NULL;
276 p_sys->i_audio_end = 0;
277 /* we've gone to slow, increase output freq */
280 /* if we are not in underrun */
281 if( p_sys->i_audio_end > len )
283 p_sys->i_audio_end -= len;
284 memcpy( stream, p_sys->audio_buf, len );
285 memmove( p_sys->audio_buf, p_sys->audio_buf + len, p_sys->i_audio_end );