]> git.sesse.net Git - vlc/blob - plugins/sdl/aout_sdl.c
1a7fe78f6f459ab2ff9513556369f21c509e86d1
[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.30 2002/07/20 18:01:43 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 <stdlib.h>                            /* calloc(), malloc(), free() */
34
35 #include <vlc/vlc.h>
36 #include <vlc/aout.h>
37
38 #include SDL_INCLUDE_FILE
39
40 /*****************************************************************************
41  * aout_sys_t: dsp audio output method descriptor
42  *****************************************************************************
43  * This structure is part of the audio output thread descriptor.
44  * It describes the dsp specific properties of an audio device.
45  *****************************************************************************/
46
47 /* the overflow limit is used to prevent the fifo from growing too big */
48 #define OVERFLOWLIMIT 100000
49
50 struct aout_sys_t
51 {
52     byte_t  * audio_buf;
53     int i_audio_end;
54
55     vlc_bool_t b_active;
56 };
57
58 /*****************************************************************************
59  * Local prototypes.
60  *****************************************************************************/
61 static int     aout_Open        ( aout_thread_t *p_aout );
62 static int     aout_SetFormat   ( aout_thread_t *p_aout );
63 static int     aout_GetBufInfo  ( aout_thread_t *p_aout, int i_buffer_info );
64 static void    aout_Play        ( aout_thread_t *p_aout,
65                                   byte_t *buffer, int i_size );
66 static void    aout_Close       ( aout_thread_t *p_aout );
67
68 static void    aout_SDLCallback ( void *userdata, Uint8 *stream, int len );
69
70 /*****************************************************************************
71  * Functions exported as capabilities. They are declared as static so that
72  * we don't pollute the namespace too much.
73  *****************************************************************************/
74 void _M( aout_getfunctions )( function_list_t * p_function_list )
75 {
76     p_function_list->functions.aout.pf_open = aout_Open;
77     p_function_list->functions.aout.pf_setformat = aout_SetFormat;
78     p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
79     p_function_list->functions.aout.pf_play = aout_Play;
80     p_function_list->functions.aout.pf_close = aout_Close;
81 }
82
83 /*****************************************************************************
84  * aout_Open: open the audio device
85  *****************************************************************************
86  * This function opens the dsp as a usual non-blocking write-only file, and
87  * modifies the p_aout->i_fd with the file's descriptor.
88  *****************************************************************************/
89 static int aout_Open( aout_thread_t *p_aout )
90 {
91     SDL_AudioSpec desired;
92
93     if( SDL_WasInit( SDL_INIT_AUDIO ) != 0 )
94     {
95         return( 1 );
96     }
97
98     /* Allocate structure */
99     p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
100
101     if( p_aout->p_sys == NULL )
102     {
103         msg_Err( p_aout, "out of memory" );
104         return( 1 );
105     }
106
107     /* Initialize library */
108     if( SDL_Init( SDL_INIT_AUDIO
109 #ifndef WIN32
110     /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet*/
111                 | SDL_INIT_EVENTTHREAD
112 #endif
113 #ifdef DEBUG
114     /* In debug mode you may want vlc to dump a core instead of staying
115      * stuck */
116                 | SDL_INIT_NOPARACHUTE
117 #endif
118                 ) < 0 )
119     {
120         msg_Err( p_aout, "cannot initialize SDL (%s)", SDL_GetError() );
121         free( p_aout->p_sys );
122         return( 1 );
123     }
124
125     p_aout->p_sys->i_audio_end = 0;
126     p_aout->p_sys->audio_buf = malloc( OVERFLOWLIMIT );
127
128     /* Initialize some variables */
129
130     /* TODO: write conversion beetween AOUT_FORMAT_DEFAULT
131      * AND AUDIO* from SDL. */
132     desired.freq       = p_aout->i_rate;
133 #ifdef WORDS_BIGENDIAN
134     desired.format     = AUDIO_S16MSB;                     /* stereo 16 bits */
135 #else
136     desired.format     = AUDIO_S16LSB;                     /* stereo 16 bits */
137 #endif
138     desired.channels   = p_aout->i_channels;
139     desired.callback   = aout_SDLCallback;
140     desired.userdata   = p_aout->p_sys;
141     desired.samples    = 1024;
142
143     /* Open the sound device
144      * we just ask the SDL to wrap at the good frequency if the one we
145      * ask for is unavailable. This is done by setting the second parar
146      * to NULL
147      */
148     if( SDL_OpenAudio( &desired, NULL ) < 0 )
149     {
150         msg_Err( p_aout, "SDL_OpenAudio failed (%s)", SDL_GetError() );
151         SDL_QuitSubSystem( SDL_INIT_AUDIO );
152         free( p_aout->p_sys );
153         return( -1 );
154     }
155
156     p_aout->p_sys->b_active = 1;
157     SDL_PauseAudio( 0 );
158
159     return( 0 );
160 }
161
162 /*****************************************************************************
163  * aout_SetFormat: reset the audio device and sets its format
164  *****************************************************************************
165  * This functions resets the audio device, tries to initialize the output
166  * format with the value contained in the dsp structure, and if this value
167  * could not be set, the default value returned by ioctl is set. It then
168  * does the same for the stereo mode, and for the output rate.
169  *****************************************************************************/
170 static int aout_SetFormat( aout_thread_t *p_aout )
171 {
172     /* TODO: finish and clean this */
173     SDL_AudioSpec desired;
174
175     /*i_format = p_aout->i_format;*/
176     desired.freq       = p_aout->i_rate;             /* Set the output rate */
177 #ifdef WORDS_BIGENDIAN
178     desired.format     = AUDIO_S16MSB;                    /* stereo 16 bits */
179 #else
180     desired.format     = AUDIO_S16LSB;                    /* stereo 16 bits */
181 #endif
182     desired.channels   = p_aout->i_channels;
183     desired.callback   = aout_SDLCallback;
184     desired.userdata   = p_aout->p_sys;
185     desired.samples    = 2048;
186
187     /* Open the sound device */
188     SDL_PauseAudio( 1 );
189     SDL_CloseAudio();
190
191     if( SDL_OpenAudio( &desired, NULL ) < 0 )
192     {
193         p_aout->p_sys->b_active = 0;
194         return( -1 );
195     }
196
197     p_aout->p_sys->b_active = 1;
198     SDL_PauseAudio( 0 );
199
200     return( 0 );
201 }
202
203 /*****************************************************************************
204  * aout_GetBufInfo: buffer status query
205  *****************************************************************************
206  * returns the number of bytes in the audio buffer compared to the size of
207  * i_buffer_limit...
208  *****************************************************************************/
209 static int aout_GetBufInfo( aout_thread_t *p_aout, int i_buffer_limit )
210 {
211     if(i_buffer_limit > p_aout->p_sys->i_audio_end)
212     {
213         /* returning 0 here juste gives awful sound in the speakers :/ */
214         return( i_buffer_limit );
215     }
216     return( p_aout->p_sys->i_audio_end - i_buffer_limit);
217 }
218
219 /*****************************************************************************
220  * aout_Play: play a sound samples buffer
221  *****************************************************************************
222  * This function writes a buffer of i_length bytes in the dsp
223  *****************************************************************************/
224 static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
225 {
226     byte_t * audio_buf = p_aout->p_sys->audio_buf;
227
228     SDL_LockAudio();                                     /* Stop callbacking */
229
230     p_aout->p_sys->audio_buf = realloc( audio_buf,
231                                         p_aout->p_sys->i_audio_end + i_size);
232     memcpy( p_aout->p_sys->audio_buf + p_aout->p_sys->i_audio_end,
233             buffer, i_size);
234
235     p_aout->p_sys->i_audio_end += i_size;
236
237     SDL_UnlockAudio();                                  /* go on callbacking */
238 }
239
240 /*****************************************************************************
241  * aout_Close: close the audio device
242  *****************************************************************************/
243 static void aout_Close( aout_thread_t *p_aout )
244 {
245     if( p_aout->p_sys->b_active )
246     {
247         SDL_PauseAudio( 1 );                                  /* pause audio */
248
249         if( p_aout->p_sys->audio_buf != NULL )  /* do we have a buffer now ? */
250         {
251             free( p_aout->p_sys->audio_buf );
252         }
253     }
254
255     SDL_CloseAudio();
256
257     SDL_QuitSubSystem( SDL_INIT_AUDIO );
258
259     free( p_aout->p_sys );                              /* Close the Output. */
260 }
261
262 /*****************************************************************************
263  * aout_SDLCallback: what to do once SDL has played sound samples
264  *****************************************************************************/
265 static void aout_SDLCallback( void *userdata, byte_t *stream, int len )
266 {
267     aout_sys_t * p_sys = userdata;
268
269     if( p_sys->i_audio_end > OVERFLOWLIMIT )
270     {
271 //X        msg_Err( p_aout, "aout_SDLCallback overflowed" );
272
273         free( p_sys->audio_buf );
274         p_sys->audio_buf = NULL;
275
276         p_sys->i_audio_end = 0;
277         /* we've gone to slow, increase output freq */
278     }
279
280     /* if we are not in underrun */
281     if( p_sys->i_audio_end > len )
282     {
283         p_sys->i_audio_end -= len;
284         memcpy( stream, p_sys->audio_buf, len );
285         memmove( p_sys->audio_buf, p_sys->audio_buf + len, p_sys->i_audio_end );
286     }
287 }
288