]> git.sesse.net Git - vlc/blob - plugins/sdl/aout_sdl.c
* ./BUGS: added a list of known bugs. Please add your findings!
[vlc] / plugins / sdl / aout_sdl.c
1 /*****************************************************************************
2  * aout_sdl.c : audio sdl functions library
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: aout_sdl.c,v 1.23 2002/01/04 14:01:34 sam Exp $
6  *
7  * Authors: Michel Kaempf <maxx@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Pierre Baillet <oct@zoy.org>
10  *
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.
15  *
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.
20  *
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  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
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 <stdio.h>                                           /* "intf_msg.h" */
34 #include <stdlib.h>                            /* calloc(), malloc(), free() */
35
36 #include <videolan/vlc.h>
37
38 #include SDL_INCLUDE_FILE
39
40 #include "audio_output.h"                                   /* aout_thread_t */
41
42 /*****************************************************************************
43  * aout_sys_t: dsp audio output method descriptor
44  *****************************************************************************
45  * This structure is part of the audio output thread descriptor.
46  * It describes the dsp specific properties of an audio device.
47  *****************************************************************************/
48
49 /* the overflow limit is used to prevent the fifo from growing too big */
50 #define OVERFLOWLIMIT 100000
51
52 typedef struct aout_sys_s
53 {
54     byte_t  * audio_buf;
55     int i_audio_end;
56
57     boolean_t b_active;
58
59 } aout_sys_t;
60
61 /*****************************************************************************
62  * Local prototypes.
63  *****************************************************************************/
64 static int     aout_Probe       ( probedata_t *p_data );
65 static int     aout_Open        ( aout_thread_t *p_aout );
66 static int     aout_SetFormat   ( aout_thread_t *p_aout );
67 static long    aout_GetBufInfo  ( aout_thread_t *p_aout, long l_buffer_info );
68 static void    aout_Play        ( aout_thread_t *p_aout,
69                                   byte_t *buffer, int i_size );
70 static void    aout_Close       ( aout_thread_t *p_aout );
71
72 static void    aout_SDLCallback ( void *userdata, Uint8 *stream, int len );
73
74 /*****************************************************************************
75  * Functions exported as capabilities. They are declared as static so that
76  * we don't pollute the namespace too much.
77  *****************************************************************************/
78 void _M( aout_getfunctions )( function_list_t * p_function_list )
79 {
80     p_function_list->pf_probe = aout_Probe;
81     p_function_list->functions.aout.pf_open = aout_Open;
82     p_function_list->functions.aout.pf_setformat = aout_SetFormat;
83     p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
84     p_function_list->functions.aout.pf_play = aout_Play;
85     p_function_list->functions.aout.pf_close = aout_Close;
86 }
87
88 /*****************************************************************************
89  * aout_Probe: probe the audio device and return a score
90  *****************************************************************************
91  * This function tries to initialize SDL audio and returns a score to the
92  * plugin manager so that it can select the best plugin.
93  *****************************************************************************/
94 static int aout_Probe( probedata_t *p_data )
95 {
96 #if 0
97     SDL_AudioSpec desired, obtained;
98 #endif
99
100     if( SDL_WasInit( SDL_INIT_AUDIO ) != 0 )
101     {
102         return( 0 );
103     }
104
105 #if 0
106     /* Start AudioSDL */
107     if( SDL_Init(SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE) != 0 )
108     {
109         intf_DbgMsg( "aout: SDL_Init failed (%s)", SDL_GetError() );
110         return( 0 );
111     }
112
113     desired.freq       = 11025;                                 /* frequency */
114     desired.format     = AUDIO_U8;                        /* unsigned 8 bits */
115     desired.channels   = 2;                                          /* mono */
116     desired.callback   = aout_SDLCallback;    /* callback function mandatory */
117     desired.userdata   = NULL;                     /* null parm for callback */
118     desired.samples    = 4096;
119
120     /* If we were unable to open the device, there is no way we can use
121      * the plugin. Return a score of 0. */
122     if( SDL_OpenAudio( &desired, &obtained ) < 0 )
123     {
124         intf_DbgMsg( "aout: SDL_OpenAudio failed (%s)", SDL_GetError() );
125         return( 0 );
126     }
127
128     /* Otherwise, there are good chances we can use this plugin, return 100. */
129     intf_DbgMsg( "aout: SDL_OpenAudio successfully run" );
130     SDL_CloseAudio();
131 #endif
132
133     return( 40 );
134 }
135
136 /*****************************************************************************
137  * aout_Open: open the audio device
138  *****************************************************************************
139  * This function opens the dsp as a usual non-blocking write-only file, and
140  * modifies the p_aout->i_fd with the file's descriptor.
141  *****************************************************************************/
142 static int aout_Open( aout_thread_t *p_aout )
143 {
144     SDL_AudioSpec desired;
145     int i_channels = p_aout->b_stereo ? 2 : 1;
146
147     if( SDL_WasInit( SDL_INIT_AUDIO ) != 0 )
148     {
149         return( 0 );
150     }
151
152     /* Allocate structure */
153     p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
154
155     if( p_aout->p_sys == NULL )
156     {
157         intf_ErrMsg( "aout error: %s", strerror(ENOMEM) );
158         return( 1 );
159     }
160
161     /* Initialize library */
162     if( SDL_Init( SDL_INIT_AUDIO
163 #ifndef WIN32
164     /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet*/
165                 | SDL_INIT_EVENTTHREAD
166 #endif
167 #ifdef DEBUG
168     /* In debug mode you may want vlc to dump a core instead of staying
169      * stuck */
170                 | SDL_INIT_NOPARACHUTE
171 #endif
172                 ) < 0 )
173     {
174         intf_ErrMsg( "aout error: can't initialize SDL (%s)", SDL_GetError() );
175         free( p_aout->p_sys );
176         return( 1 );
177     }
178
179     p_aout->p_sys->i_audio_end = 0;
180     p_aout->p_sys->audio_buf = malloc( OVERFLOWLIMIT );
181
182     /* Initialize some variables */
183     p_aout->psz_device = 0;
184     p_aout->i_format   = AOUT_FORMAT_DEFAULT;
185     p_aout->i_channels = 1 + main_GetIntVariable( AOUT_STEREO_VAR,
186                                                   AOUT_STEREO_DEFAULT );
187     p_aout->l_rate     = main_GetIntVariable( AOUT_RATE_VAR,
188                                               AOUT_RATE_DEFAULT );
189
190     desired.freq =     p_aout->l_rate;
191
192     /* TODO: write conversion beetween AOUT_FORMAT_DEFAULT
193      * AND AUDIO* from SDL. */
194     desired.format     = AUDIO_S16LSB;                     /* stereo 16 bits */
195     desired.channels   = i_channels;
196     desired.callback   = aout_SDLCallback;
197     desired.userdata   = p_aout->p_sys;
198     desired.samples    = 1024;
199
200     /* Open the sound device
201      * we just ask the SDL to wrap at the good frequency if the one we
202      * ask for is unavailable. This is done by setting the second parar
203      * to NULL
204      */
205     if( SDL_OpenAudio( &desired, NULL ) < 0 )
206     {
207         intf_ErrMsg( "aout error: SDL_OpenAudio failed (%s)", SDL_GetError() );
208         SDL_QuitSubSystem( SDL_INIT_AUDIO );
209         free( p_aout->p_sys );
210         return( -1 );
211     }
212
213     p_aout->p_sys->b_active = 1;
214     SDL_PauseAudio( 0 );
215
216     return( 0 );
217 }
218
219 /*****************************************************************************
220  * aout_SetFormat: reset the audio device and sets its format
221  *****************************************************************************
222  * This functions resets the audio device, tries to initialize the output
223  * format with the value contained in the dsp structure, and if this value
224  * could not be set, the default value returned by ioctl is set. It then
225  * does the same for the stereo mode, and for the output rate.
226  *****************************************************************************/
227 static int aout_SetFormat( aout_thread_t *p_aout )
228 {
229     /* TODO: finish and clean this */
230     SDL_AudioSpec desired;
231     int i_stereo = p_aout->b_stereo ? 2 : 1;
232
233     /*i_format = p_aout->i_format;*/
234     desired.freq       = p_aout->l_rate;             /* Set the output rate */
235     desired.format     = AUDIO_S16LSB;                    /* stereo 16 bits */
236     desired.channels   = i_stereo;
237     desired.callback   = aout_SDLCallback;
238     desired.userdata   = p_aout->p_sys;
239     desired.samples    = 2048;
240
241     /* Open the sound device */
242     SDL_PauseAudio( 1 );
243     SDL_CloseAudio();
244
245     if( SDL_OpenAudio( &desired, NULL ) < 0 )
246     {
247         p_aout->p_sys->b_active = 0;
248         return( -1 );
249     }
250
251     p_aout->p_sys->b_active = 1;
252     SDL_PauseAudio( 0 );
253
254     p_aout->i_latency = 0;
255     
256     return( 0 );
257 }
258
259 /*****************************************************************************
260  * aout_GetBufInfo: buffer status query
261  *****************************************************************************
262  * returns the number of bytes in the audio buffer compared to the size of
263  * l_buffer_limit...
264  *****************************************************************************/
265 static long aout_GetBufInfo( aout_thread_t *p_aout, long l_buffer_limit )
266 {
267     if(l_buffer_limit > p_aout->p_sys->i_audio_end)
268     {
269         /* returning 0 here juste gives awful sound in the speakers :/ */
270         return( l_buffer_limit );
271     }
272     return( p_aout->p_sys->i_audio_end - l_buffer_limit);
273 }
274
275 /*****************************************************************************
276  * aout_Play: play a sound samples buffer
277  *****************************************************************************
278  * This function writes a buffer of i_length bytes in the dsp
279  *****************************************************************************/
280 static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
281 {
282     byte_t * audio_buf = p_aout->p_sys->audio_buf;
283
284     SDL_LockAudio();                                     /* Stop callbacking */
285
286     p_aout->p_sys->audio_buf = realloc( audio_buf,
287                                         p_aout->p_sys->i_audio_end + i_size);
288     memcpy( p_aout->p_sys->audio_buf + p_aout->p_sys->i_audio_end,
289             buffer, i_size);
290
291     p_aout->p_sys->i_audio_end += i_size;
292
293     SDL_UnlockAudio();                                  /* go on callbacking */
294 }
295
296 /*****************************************************************************
297  * aout_Close: close the audio device
298  *****************************************************************************/
299 static void aout_Close( aout_thread_t *p_aout )
300 {
301     if( p_aout->p_sys->b_active )
302     {
303         SDL_PauseAudio( 1 );                                  /* pause audio */
304
305         if( p_aout->p_sys->audio_buf != NULL )  /* do we have a buffer now ? */
306         {
307             free( p_aout->p_sys->audio_buf );
308         }
309     }
310
311     SDL_CloseAudio();
312
313     SDL_QuitSubSystem( SDL_INIT_AUDIO );
314
315     free( p_aout->p_sys );                              /* Close the Output. */
316 }
317
318 /*****************************************************************************
319  * aout_SDLCallback: what to do once SDL has played sound samples
320  *****************************************************************************/
321 static void aout_SDLCallback( void *userdata, byte_t *stream, int len )
322 {
323     struct aout_sys_s * p_sys = userdata;
324
325     if( p_sys->i_audio_end > OVERFLOWLIMIT )
326     {
327         intf_ErrMsg( "aout error: aout_SDLCallback overflowed" );
328
329         free( p_sys->audio_buf );
330         p_sys->audio_buf = NULL;
331
332         p_sys->i_audio_end = 0;
333         /* we've gone to slow, increase output freq */
334     }
335
336     /* if we are not in underrun */
337     if( p_sys->i_audio_end > len )
338     {
339         p_sys->i_audio_end -= len;
340         memcpy( stream, p_sys->audio_buf, len );
341         memmove( p_sys->audio_buf, p_sys->audio_buf + len, p_sys->i_audio_end );
342     }
343 }
344