]> git.sesse.net Git - vlc/blob - plugins/sdl/aout_sdl.c
* Ported Glide and MGA plugins to the new module API. MGA never worked,
[vlc] / plugins / sdl / aout_sdl.c
1 /*****************************************************************************
2  * aout_sdl.c : audio sdl functions library
3  *****************************************************************************
4  * Copyright (C) 1999, 2000 VideoLAN
5  *
6  * Authors: Michel Kaempf <maxx@via.ecp.fr>
7  *          Samuel Hocevar <sam@zoy.org>
8  *          Pierre Baillet <oct@zoy.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 #define MODULE_NAME sdl
26 #include "modules_inner.h"
27
28 /*****************************************************************************
29  * Preamble
30  *****************************************************************************/
31 #include "defs.h"
32
33 #include <errno.h>                                                 /* ENOMEM */
34 #include <fcntl.h>                                       /* open(), O_WRONLY */
35 #include <sys/ioctl.h>                                            /* ioctl() */
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/SDL.h>                                     /* SDL base include */
42
43 #include "config.h"
44 #include "common.h"                                     /* boolean_t, byte_t */
45 #include "threads.h"
46 #include "mtime.h"
47 #include "tests.h"
48
49 #include "audio_output.h"                                   /* aout_thread_t */
50
51 #include "intf_msg.h"                        /* intf_DbgMsg(), intf_ErrMsg() */
52 #include "main.h"
53
54 #include "modules.h"
55
56 /*****************************************************************************
57  * aout_sys_t: dsp audio output method descriptor
58  *****************************************************************************
59  * This structure is part of the audio output thread descriptor.
60  * It describes the dsp specific properties of an audio device.
61  *****************************************************************************/
62
63 /* the overflow limit is used to prevent the fifo from growing too big */
64 #define OVERFLOWLIMIT 100000
65
66 typedef struct aout_sys_s
67 {
68     byte_t  * audio_buf;
69     int i_audio_end;
70
71     boolean_t b_active;
72
73 } aout_sys_t;
74
75 /*****************************************************************************
76  * Local prototypes.
77  *****************************************************************************/
78 static int     aout_Probe       ( probedata_t *p_data );
79 static int     aout_Open        ( aout_thread_t *p_aout );
80 static int     aout_SetFormat   ( aout_thread_t *p_aout );
81 static long    aout_GetBufInfo  ( aout_thread_t *p_aout, long l_buffer_info );
82 static void    aout_Play        ( aout_thread_t *p_aout,
83                                   byte_t *buffer, int i_size );
84 static void    aout_Close       ( aout_thread_t *p_aout );
85
86 static void    aout_SDLCallback ( void *userdata, Uint8 *stream, int len );
87
88 /*****************************************************************************
89  * Functions exported as capabilities. They are declared as static so that
90  * we don't pollute the namespace too much.
91  *****************************************************************************/
92 void _M( aout_getfunctions )( function_list_t * p_function_list )
93 {
94     p_function_list->pf_probe = aout_Probe;
95     p_function_list->functions.aout.pf_open = aout_Open;
96     p_function_list->functions.aout.pf_setformat = aout_SetFormat;
97     p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
98     p_function_list->functions.aout.pf_play = aout_Play;
99     p_function_list->functions.aout.pf_close = aout_Close;
100 }
101
102 /*****************************************************************************
103  * aout_Probe: probe the audio device and return a score
104  *****************************************************************************
105  * This function tries to initialize SDL audio and returns a score to the
106  * plugin manager so that it can select the best plugin.
107  *****************************************************************************/
108 static int aout_Probe( probedata_t *p_data )
109 {
110     SDL_AudioSpec desired, obtained;
111
112     return 0;
113     /* Start AudioSDL */
114     if( SDL_Init(SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE) != 0 )
115     {
116         intf_DbgMsg( "aout: SDL_Init failed (%s)", SDL_GetError() );
117         return( 0 );
118     }
119
120     desired.freq       = 11025;                                 /* frequency */
121     desired.format     = AUDIO_U8;                        /* unsigned 8 bits */
122     desired.channels   = 2;                                          /* mono */
123     desired.callback   = NULL;                   /* no callback function yet */
124     desired.userdata   = NULL;                     /* null parm for callback */
125     desired.samples    = 4096;
126
127     /* If we were unable to open the device, there is no way we can use
128      * the plugin. Return a score of 0. */
129     if( SDL_OpenAudio( &desired, &obtained ) < 0 )
130     {
131         intf_DbgMsg( "aout: SDL_OpenAudio failed (%s)", SDL_GetError() );
132         return( 0 );
133     }
134
135     /* Otherwise, there are good chances we can use this plugin, return 100. */
136     SDL_CloseAudio();
137
138     if( TestMethod( AOUT_METHOD_VAR, "sdl" ) )
139     {
140         return( 999 );
141     }
142
143     return( 40 );
144 }
145
146 /*****************************************************************************
147  * aout_Open: open the audio device
148  *****************************************************************************
149  * This function opens the dsp as a usual non-blocking write-only file, and
150  * modifies the p_aout->i_fd with the file's descriptor.
151  *****************************************************************************/
152 static int aout_Open( aout_thread_t *p_aout )
153 {
154     SDL_AudioSpec desired;
155     int i_channels = p_aout->b_stereo ? 2 : 1;
156
157    /* Allocate structure */
158     p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
159
160     if( p_aout->p_sys == NULL )
161     {
162         intf_ErrMsg( "aout error: %s", strerror(ENOMEM) );
163         return( 1 );
164     }
165
166     p_aout->p_sys->i_audio_end = 0;
167     p_aout->p_sys->audio_buf = malloc( OVERFLOWLIMIT );
168
169     /* Initialize some variables */
170     p_aout->psz_device = 0;
171     p_aout->i_format   = AOUT_FORMAT_DEFAULT;
172     p_aout->i_channels = 1 + main_GetIntVariable( AOUT_STEREO_VAR,
173                                                   AOUT_STEREO_DEFAULT );
174     p_aout->l_rate     = main_GetIntVariable( AOUT_RATE_VAR,
175                                               AOUT_RATE_DEFAULT );
176
177     desired.freq =     p_aout->l_rate;
178
179     /* TODO: write conversion beetween AOUT_FORMAT_DEFAULT
180      * AND AUDIO* from SDL. */
181     desired.format     = AUDIO_S16LSB;                     /* stereo 16 bits */
182     desired.channels   = i_channels;
183     desired.callback   = aout_SDLCallback;
184     desired.userdata   = p_aout->p_sys;
185     desired.samples    = 1024;
186
187     /* Open the sound device
188      * we just ask the SDL to wrap at the good frequency if the one we
189      * ask for is unavailable. This is done by setting the second parar
190      * to NULL
191      */
192     if( SDL_OpenAudio( &desired, NULL ) < 0 )
193     {
194         intf_ErrMsg( "aout error: SDL_OpenAudio failed (%s)", SDL_GetError() );
195         return( -1 );
196     }
197
198     p_aout->p_sys->b_active = 1;
199     SDL_PauseAudio( 0 );
200
201     return( 0 );
202 }
203
204 /*****************************************************************************
205  * aout_SetFormat: reset the audio device and sets its format
206  *****************************************************************************
207  * This functions resets the audio device, tries to initialize the output
208  * format with the value contained in the dsp structure, and if this value
209  * could not be set, the default value returned by ioctl is set. It then
210  * does the same for the stereo mode, and for the output rate.
211  *****************************************************************************/
212 static int aout_SetFormat( aout_thread_t *p_aout )
213 {
214     /* TODO: finish and clean this */
215     SDL_AudioSpec desired;
216     int i_stereo = p_aout->b_stereo ? 2 : 1;
217
218     /*i_format = p_aout->i_format;*/
219     desired.freq       = p_aout->l_rate;             /* Set the output rate */
220     desired.format     = AUDIO_S16LSB;                    /* stereo 16 bits */
221     desired.channels   = i_stereo;
222     desired.callback   = aout_SDLCallback;
223     desired.userdata   = p_aout->p_sys;
224     desired.samples    = 2048;
225
226     /* Open the sound device */
227     SDL_PauseAudio( 1 );
228     SDL_CloseAudio();
229
230     if( SDL_OpenAudio( &desired, NULL ) < 0 )
231     {
232         p_aout->p_sys->b_active = 0;
233         return( -1 );
234     }
235
236     p_aout->p_sys->b_active = 1;
237     SDL_PauseAudio( 0 );
238
239     return( 0 );
240 }
241
242 /*****************************************************************************
243  * aout_GetBufInfo: buffer status query
244  *****************************************************************************
245  * returns the number of bytes in the audio buffer compared to the size of
246  * l_buffer_limit...
247  *****************************************************************************/
248 static long aout_GetBufInfo( aout_thread_t *p_aout, long l_buffer_limit )
249 {
250     if(l_buffer_limit > p_aout->p_sys->i_audio_end)
251     {
252         /* returning 0 here juste gives awful sound in the speakers :/ */
253         return( l_buffer_limit );
254     }
255     return( p_aout->p_sys->i_audio_end - l_buffer_limit);
256 }
257
258 /*****************************************************************************
259  * aout_Play: play a sound samples buffer
260  *****************************************************************************
261  * This function writes a buffer of i_length bytes in the dsp
262  *****************************************************************************/
263 static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
264 {
265     byte_t * audio_buf = p_aout->p_sys->audio_buf;
266
267     SDL_LockAudio();                                     /* Stop callbacking */
268
269     p_aout->p_sys->audio_buf = realloc( audio_buf,
270                                         p_aout->p_sys->i_audio_end + i_size);
271     memcpy( p_aout->p_sys->audio_buf + p_aout->p_sys->i_audio_end,
272             buffer, i_size);
273
274     p_aout->p_sys->i_audio_end += i_size;
275
276     SDL_UnlockAudio();                                  /* go on callbacking */
277 }
278
279 /*****************************************************************************
280  * aout_Close: close the audio device
281  *****************************************************************************/
282 static void aout_Close( aout_thread_t *p_aout )
283 {
284     if( p_aout->p_sys->b_active )
285     {
286         SDL_PauseAudio( 1 );                                  /* pause audio */
287
288         if( p_aout->p_sys->audio_buf != NULL )  /* do we have a buffer now ? */
289         {
290             free( p_aout->p_sys->audio_buf );
291         }
292     }
293
294     SDL_CloseAudio();
295
296     free( p_aout->p_sys );                              /* Close the Output. */
297 }
298
299 /*****************************************************************************
300  * aout_SDLCallback: what to do once SDL has played sound samples
301  *****************************************************************************/
302 static void aout_SDLCallback( void *userdata, byte_t *stream, int len )
303 {
304     struct aout_sys_s * p_sys = userdata;
305
306     if( p_sys->i_audio_end > OVERFLOWLIMIT )
307     {
308         intf_ErrMsg( "aout error: aout_SDLCallback overflowed" );
309
310         free( p_sys->audio_buf );
311         p_sys->audio_buf = NULL;
312
313         p_sys->i_audio_end = 0;
314         /* we've gone to slow, increase output freq */
315     }
316
317     /* if we are not in underrun */
318     if( p_sys->i_audio_end > len )
319     {
320         p_sys->i_audio_end -= len;
321         memcpy( stream, p_sys->audio_buf, len );
322         memmove( p_sys->audio_buf, p_sys->audio_buf + len, p_sys->i_audio_end );
323     }
324 }
325