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