1 /*****************************************************************************
2 * aout_sdl.c : audio sdl functions library
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: aout_sdl.c,v 1.31 2002/07/31 20:56:52 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 SetFormat ( aout_thread_t * );
62 static int GetBufInfo ( aout_thread_t *, int );
63 static void Play ( aout_thread_t *, byte_t *, int );
65 static void SDLCallback ( void *, Uint8 *, int );
67 /*****************************************************************************
68 * OpenAudio: open the audio device
69 *****************************************************************************
70 * This function opens the dsp as a usual non-blocking write-only file, and
71 * modifies the p_aout->i_fd with the file's descriptor.
72 *****************************************************************************/
73 int E_(OpenAudio) ( vlc_object_t *p_this )
75 aout_thread_t * p_aout = (aout_thread_t *)p_this;
77 SDL_AudioSpec desired;
79 if( SDL_WasInit( SDL_INIT_AUDIO ) != 0 )
84 p_aout->pf_setformat = SetFormat;
85 p_aout->pf_getbufinfo = GetBufInfo;
86 p_aout->pf_play = Play;
88 /* Allocate structure */
89 p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
91 if( p_aout->p_sys == NULL )
93 msg_Err( p_aout, "out of memory" );
97 /* Initialize library */
98 if( SDL_Init( SDL_INIT_AUDIO
100 /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet*/
101 | SDL_INIT_EVENTTHREAD
104 /* In debug mode you may want vlc to dump a core instead of staying
106 | SDL_INIT_NOPARACHUTE
110 msg_Err( p_aout, "cannot initialize SDL (%s)", SDL_GetError() );
111 free( p_aout->p_sys );
115 p_aout->p_sys->i_audio_end = 0;
116 p_aout->p_sys->audio_buf = malloc( OVERFLOWLIMIT );
118 /* Initialize some variables */
120 /* TODO: write conversion beetween AOUT_FORMAT_DEFAULT
121 * AND AUDIO* from SDL. */
122 desired.freq = p_aout->i_rate;
123 #ifdef WORDS_BIGENDIAN
124 desired.format = AUDIO_S16MSB; /* stereo 16 bits */
126 desired.format = AUDIO_S16LSB; /* stereo 16 bits */
128 desired.channels = p_aout->i_channels;
129 desired.callback = SDLCallback;
130 desired.userdata = p_aout->p_sys;
131 desired.samples = 1024;
133 /* Open the sound device
134 * we just ask the SDL to wrap at the good frequency if the one we
135 * ask for is unavailable. This is done by setting the second parar
138 if( SDL_OpenAudio( &desired, NULL ) < 0 )
140 msg_Err( p_aout, "SDL_OpenAudio failed (%s)", SDL_GetError() );
141 SDL_QuitSubSystem( SDL_INIT_AUDIO );
142 free( p_aout->p_sys );
146 p_aout->p_sys->b_active = 1;
152 /*****************************************************************************
153 * SetFormat: reset the audio device and sets its format
154 *****************************************************************************
155 * This functions resets the audio device, tries to initialize the output
156 * format with the value contained in the dsp structure, and if this value
157 * could not be set, the default value returned by ioctl is set. It then
158 * does the same for the stereo mode, and for the output rate.
159 *****************************************************************************/
160 static int SetFormat( aout_thread_t *p_aout )
162 /* TODO: finish and clean this */
163 SDL_AudioSpec desired;
165 /*i_format = p_aout->i_format;*/
166 desired.freq = p_aout->i_rate; /* Set the output rate */
167 #ifdef WORDS_BIGENDIAN
168 desired.format = AUDIO_S16MSB; /* stereo 16 bits */
170 desired.format = AUDIO_S16LSB; /* stereo 16 bits */
172 desired.channels = p_aout->i_channels;
173 desired.callback = SDLCallback;
174 desired.userdata = p_aout->p_sys;
175 desired.samples = 2048;
177 /* Open the sound device */
181 if( SDL_OpenAudio( &desired, NULL ) < 0 )
183 p_aout->p_sys->b_active = 0;
187 p_aout->p_sys->b_active = 1;
193 /*****************************************************************************
194 * GetBufInfo: buffer status query
195 *****************************************************************************
196 * returns the number of bytes in the audio buffer compared to the size of
198 *****************************************************************************/
199 static int GetBufInfo( aout_thread_t *p_aout, int i_buffer_limit )
201 if(i_buffer_limit > p_aout->p_sys->i_audio_end)
203 /* returning 0 here juste gives awful sound in the speakers :/ */
204 return( i_buffer_limit );
206 return( p_aout->p_sys->i_audio_end - i_buffer_limit);
209 /*****************************************************************************
210 * Play: play a sound samples buffer
211 *****************************************************************************
212 * This function writes a buffer of i_length bytes in the dsp
213 *****************************************************************************/
214 static void Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
216 byte_t * audio_buf = p_aout->p_sys->audio_buf;
218 SDL_LockAudio(); /* Stop callbacking */
220 p_aout->p_sys->audio_buf = realloc( audio_buf,
221 p_aout->p_sys->i_audio_end + i_size);
222 memcpy( p_aout->p_sys->audio_buf + p_aout->p_sys->i_audio_end,
225 p_aout->p_sys->i_audio_end += i_size;
227 SDL_UnlockAudio(); /* go on callbacking */
230 /*****************************************************************************
231 * CloseAudio: close the audio device
232 *****************************************************************************/
233 void E_(CloseAudio) ( vlc_object_t *p_this )
235 aout_thread_t * p_aout = (aout_thread_t *)p_this;
237 if( p_aout->p_sys->b_active )
239 SDL_PauseAudio( 1 ); /* pause audio */
241 if( p_aout->p_sys->audio_buf != NULL ) /* do we have a buffer now ? */
243 free( p_aout->p_sys->audio_buf );
249 SDL_QuitSubSystem( SDL_INIT_AUDIO );
251 free( p_aout->p_sys ); /* Close the Output. */
254 /*****************************************************************************
255 * SDLCallback: what to do once SDL has played sound samples
256 *****************************************************************************/
257 static void SDLCallback( void *userdata, byte_t *stream, int len )
259 aout_sys_t * p_sys = userdata;
261 if( p_sys->i_audio_end > OVERFLOWLIMIT )
263 //X msg_Err( p_aout, "aout_SDLCallback overflowed" );
265 free( p_sys->audio_buf );
266 p_sys->audio_buf = NULL;
268 p_sys->i_audio_end = 0;
269 /* we've gone to slow, increase output freq */
272 /* if we are not in underrun */
273 if( p_sys->i_audio_end > len )
275 p_sys->i_audio_end -= len;
276 memcpy( stream, p_sys->audio_buf, len );
277 memmove( p_sys->audio_buf, p_sys->audio_buf + len, p_sys->i_audio_end );