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