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