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