]> git.sesse.net Git - vlc/blob - modules/audio_output/sdl.c
* ./src/audio_output/output.c: reverted my previous aout_OutputNextBuffer
[vlc] / modules / audio_output / sdl.c
1 /*****************************************************************************
2  * sdl.c : SDL audio output plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2002 VideoLAN
5  * $Id: sdl.c,v 1.6 2002/08/25 09:40:00 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 #include "aout_internal.h"
38
39 #include SDL_INCLUDE_FILE
40
41 #define FRAME_SIZE 2048
42
43 /*****************************************************************************
44  * aout_sys_t: SDL audio output method descriptor
45  *****************************************************************************
46  * This structure is part of the audio output thread descriptor.
47  * It describes the specific properties of an audio device.
48  *****************************************************************************/
49 struct aout_sys_t
50 {   
51     mtime_t call_time;
52     mtime_t buffer_time;
53 };
54
55 /*****************************************************************************
56  * Local prototypes
57  *****************************************************************************/
58 static int  Open        ( vlc_object_t * );
59 static void Close       ( vlc_object_t * );
60
61 static int  SetFormat   ( aout_instance_t * );
62 static void Play        ( aout_instance_t * );
63
64 static void SDLCallback ( void *, Uint8 *, int );
65
66 /*****************************************************************************
67  * Module descriptor
68  *****************************************************************************/
69 vlc_module_begin();
70     set_description( _("Simple DirectMedia Layer audio module") );
71     set_capability( "audio output", 40 );
72     set_callbacks( Open, Close );
73 vlc_module_end();
74
75 /*****************************************************************************
76  * Open: open the audio device
77  *****************************************************************************/
78 static int Open ( vlc_object_t *p_this )
79 {
80     aout_instance_t *p_aout = (aout_instance_t *)p_this;
81     aout_sys_t * p_sys;
82
83     Uint32 i_flags = SDL_INIT_AUDIO;
84
85     if( SDL_WasInit( i_flags ) )
86     {
87         return VLC_EGENERIC;
88     }
89
90     /* Allocate structure */
91     p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
92     if( p_sys == NULL )
93     {
94         msg_Err( p_aout, "out of memory" );
95         return VLC_ENOMEM;
96     }
97
98     p_aout->output.pf_setformat = SetFormat;
99     p_aout->output.pf_play = Play;
100
101 #ifndef WIN32
102     /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet*/
103     i_flags |= SDL_INIT_EVENTTHREAD;
104 #endif
105 #ifdef DEBUG
106     /* In debug mode you may want vlc to dump a core instead of staying
107      * stuck */
108     i_flags |= SDL_INIT_NOPARACHUTE;
109 #endif
110
111     /* Initialize library */
112     if( SDL_Init( i_flags ) < 0 )
113     {
114         msg_Err( p_aout, "cannot initialize SDL (%s)", SDL_GetError() );
115         free( p_sys );
116         return VLC_EGENERIC;
117     }
118
119     return VLC_SUCCESS;
120 }
121
122 /*****************************************************************************
123  * SetFormat: reset the audio device and sets its format
124  *****************************************************************************/
125 static int SetFormat( aout_instance_t *p_aout )
126 {
127     aout_sys_t * p_sys = p_aout->output.p_sys;
128
129     /* TODO: finish and clean this */
130     SDL_AudioSpec desired;
131
132     desired.freq       = p_aout->output.output.i_rate;
133     desired.format     = AUDIO_S16SYS;
134     desired.channels   = p_aout->output.output.i_channels;
135     desired.callback   = SDLCallback;
136     desired.userdata   = p_aout;
137     desired.samples    = FRAME_SIZE;
138
139     /* Open the sound device - FIXME : get the "natural" parameters */
140     if( SDL_OpenAudio( &desired, NULL ) < 0 )
141     {
142         return VLC_EGENERIC;
143     }
144
145     p_aout->output.output.i_format = AOUT_FMT_S16_NE;
146     p_aout->output.i_nb_samples = FRAME_SIZE;
147
148     p_sys->call_time = 0;
149     p_sys->buffer_time = (mtime_t)FRAME_SIZE * 1000000
150                           / p_aout->output.output.i_rate;
151
152     SDL_PauseAudio( 0 );
153
154     return VLC_SUCCESS;
155 }
156
157 /*****************************************************************************
158  * Play: play a sound samples buffer
159  *****************************************************************************/
160 static void Play( aout_instance_t * p_aout )
161 {
162 }
163
164 /*****************************************************************************
165  * Close: close the audio device
166  *****************************************************************************/
167 static void Close ( vlc_object_t *p_this )
168 {
169     aout_instance_t *p_aout = (aout_instance_t *)p_this;
170     aout_sys_t * p_sys = p_aout->output.p_sys;
171
172     SDL_PauseAudio( 1 );
173     SDL_CloseAudio();
174     SDL_QuitSubSystem( SDL_INIT_AUDIO );
175
176     free( p_sys );
177 }
178
179 /*****************************************************************************
180  * SDLCallback: what to do once SDL has played sound samples
181  *****************************************************************************/
182 static void SDLCallback( void * _p_aout, byte_t * p_stream, int i_len )
183 {
184     aout_instance_t * p_aout = (aout_instance_t *)_p_aout;
185     aout_sys_t * p_sys = p_aout->output.p_sys;
186
187     aout_buffer_t * p_buffer;
188
189     /* We try to stay around call_time + buffer_time/2. This is kludgy but
190      * unavoidable because SDL is completely unable to 1. tell us about its
191      * latency, and 2. call SDLCallback at regular intervals. */
192     if( mdate() < p_sys->call_time + p_sys->buffer_time / 2 )
193     {
194         /* We can't wait too much, because SDL will be lost, and we can't
195          * wait too little, because we are not sure that there will be
196          * samples in the queue. */
197         mwait( p_sys->call_time + p_sys->buffer_time / 4 );
198         p_sys->call_time += p_sys->buffer_time;
199     }
200     else
201     {
202         p_sys->call_time = mdate() + p_sys->buffer_time / 4;
203     }
204
205     /* Tell the output we're playing samples at call_time + 2*buffer_time */
206     p_buffer = aout_OutputNextBuffer( p_aout, p_sys->call_time
207                                        + 2 * p_sys->buffer_time, VLC_TRUE );
208
209     if ( i_len != FRAME_SIZE * sizeof(s16)
210                     * p_aout->output.output.i_channels )
211     {
212         msg_Err( p_aout, "SDL doesn't know its buffer size (%d)", i_len );
213     }
214
215     if ( p_buffer != NULL )
216     {
217         p_aout->p_vlc->pf_memcpy( p_stream, p_buffer->p_buffer, i_len );
218         aout_BufferFree( p_buffer );
219     }
220     else
221     {
222         p_aout->p_vlc->pf_memset( p_stream, 0, i_len );
223     }
224 }
225