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