]> git.sesse.net Git - vlc/blob - modules/audio_output/sdl.c
Merge branch 'master' of git@git.videolan.org:vlc
[vlc] / modules / audio_output / sdl.c
1 /*****************************************************************************
2  * sdl.c : SDL audio output plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2002 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Michel Kaempf <maxx@via.ecp.fr>
8  *          Sam Hocevar <sam@zoy.org>
9  *          Pierre Baillet <oct@zoy.org>
10  *          Christophe Massiot <massiot@via.ecp.fr>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <unistd.h>                                      /* write(), close() */
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <vlc/vlc.h>
37 #include <vlc_aout.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 next_date;
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 static void Play        ( aout_instance_t * );
61 static void SDLCallback ( void *, byte_t *, int );
62
63 /*****************************************************************************
64  * Module descriptor
65  *****************************************************************************/
66 vlc_module_begin();
67     set_shortname( "SDL" );
68     set_description( _("Simple DirectMedia Layer audio output") );
69     set_capability( "audio output", 40 );
70     set_category( CAT_AUDIO );
71     set_subcategory( SUBCAT_AUDIO_AOUT );
72     add_shortcut( "sdl" );
73     set_callbacks( Open, Close );
74 vlc_module_end();
75
76 /*****************************************************************************
77  * Open: open the audio device
78  *****************************************************************************/
79 static int Open ( vlc_object_t *p_this )
80 {
81     aout_instance_t *p_aout = (aout_instance_t *)p_this;
82     SDL_AudioSpec desired, obtained;
83     int i_nb_channels;
84     vlc_value_t val, text;
85
86     /* Check that no one uses the DSP. */
87     uint32_t i_flags = SDL_INIT_AUDIO;
88     if( SDL_WasInit( i_flags ) )
89     {
90         return VLC_EGENERIC;
91     }
92
93 #ifndef WIN32
94     /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet */
95     i_flags |= SDL_INIT_EVENTTHREAD;
96 #endif
97 #ifndef NDEBUG
98     /* In debug mode you may want vlc to dump a core instead of staying
99      * stuck */
100     i_flags |= SDL_INIT_NOPARACHUTE;
101 #endif
102
103     /* Initialize library */
104     if( SDL_Init( i_flags ) < 0 )
105     {
106         msg_Err( p_aout, "cannot initialize SDL (%s)", SDL_GetError() );
107         return VLC_EGENERIC;
108     }
109
110     if ( var_Type( p_aout, "audio-device" ) != 0 )
111     {
112         /* The user has selected an audio device. */
113         vlc_value_t val;
114         var_Get( p_aout, "audio-device", &val );
115         if ( val.i_int == AOUT_VAR_STEREO )
116         {
117             p_aout->output.output.i_physical_channels
118                 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
119         }
120         else if ( val.i_int == AOUT_VAR_MONO )
121         {
122             p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
123         }
124     }
125
126     i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
127     if ( i_nb_channels > 2 )
128     {
129         /* SDL doesn't support more than two channels. */
130         i_nb_channels = 2;
131         p_aout->output.output.i_physical_channels
132             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
133     }
134     desired.freq       = p_aout->output.output.i_rate;
135     desired.format     = AUDIO_S16SYS;
136     desired.channels   = i_nb_channels;
137     desired.callback   = SDLCallback;
138     desired.userdata   = p_aout;
139     desired.samples    = FRAME_SIZE;
140
141     /* Open the sound device. */
142     if( SDL_OpenAudio( &desired, &obtained ) < 0 )
143     {
144         return VLC_EGENERIC;
145     }
146
147     SDL_PauseAudio( 0 );
148
149     /* Now have a look at what we got. */
150     switch ( obtained.format )
151     {
152     case AUDIO_S16LSB:
153         p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l'); break;
154     case AUDIO_S16MSB:
155         p_aout->output.output.i_format = VLC_FOURCC('s','1','6','b'); break;
156     case AUDIO_U16LSB:
157         p_aout->output.output.i_format = VLC_FOURCC('u','1','6','l'); break;
158     case AUDIO_U16MSB:
159         p_aout->output.output.i_format = VLC_FOURCC('u','1','6','b'); break;
160     case AUDIO_S8:
161         p_aout->output.output.i_format = VLC_FOURCC('s','8',' ',' '); break;
162     case AUDIO_U8:
163         p_aout->output.output.i_format = VLC_FOURCC('u','8',' ',' '); break;
164     }
165     /* Volume is entirely done in software. */
166     aout_VolumeSoftInit( p_aout );
167
168     if ( obtained.channels != i_nb_channels )
169     {
170         p_aout->output.output.i_physical_channels = (obtained.channels == 2 ?
171                                             AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT :
172                                             AOUT_CHAN_CENTER);
173
174         if ( var_Type( p_aout, "audio-device" ) == 0 )
175         {
176             var_Create( p_aout, "audio-device",
177                         VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
178             text.psz_string = _("Audio Device");
179             var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
180
181             val.i_int = (obtained.channels == 2) ? AOUT_VAR_STEREO :
182                         AOUT_VAR_MONO;
183             text.psz_string = (obtained.channels == 2) ? N_("Stereo") :
184                               N_("Mono");
185             var_Change( p_aout, "audio-device",
186                         VLC_VAR_ADDCHOICE, &val, &text );
187             var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart,
188                              NULL );
189         }
190     }
191     else if ( var_Type( p_aout, "audio-device" ) == 0 )
192     {
193         /* First launch. */
194         var_Create( p_aout, "audio-device",
195                     VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
196         text.psz_string = _("Audio Device");
197         var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
198
199         val.i_int = AOUT_VAR_STEREO;
200         text.psz_string = N_("Stereo");
201         var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
202         val.i_int = AOUT_VAR_MONO;
203         text.psz_string = N_("Mono");
204         var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
205         if ( i_nb_channels == 2 )
206         {
207             val.i_int = AOUT_VAR_STEREO;
208         }
209         else
210         {
211             val.i_int = AOUT_VAR_MONO;
212         }
213         var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
214         var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
215     }
216
217     val.b_bool = true;
218     var_Set( p_aout, "intf-change", val );
219
220     p_aout->output.output.i_rate = obtained.freq;
221     p_aout->output.i_nb_samples = obtained.samples;
222     p_aout->output.pf_play = Play;
223
224     return VLC_SUCCESS;
225 }
226
227 /*****************************************************************************
228  * Play: play a sound samples buffer
229  *****************************************************************************/
230 static void Play( aout_instance_t * p_aout )
231 {
232     VLC_UNUSED(p_aout);
233 }
234
235 /*****************************************************************************
236  * Close: close the audio device
237  *****************************************************************************/
238 static void Close ( vlc_object_t *p_this )
239 {
240     VLC_UNUSED(p_this);
241     SDL_PauseAudio( 1 );
242     SDL_CloseAudio();
243     SDL_QuitSubSystem( SDL_INIT_AUDIO );
244 }
245
246 /*****************************************************************************
247  * SDLCallback: what to do once SDL has played sound samples
248  *****************************************************************************/
249 static void SDLCallback( void * _p_aout, byte_t * p_stream, int i_len )
250 {
251     aout_instance_t * p_aout = (aout_instance_t *)_p_aout;
252     aout_buffer_t *   p_buffer;
253
254     /* SDL is unable to call us at regular times, or tell us its current
255      * hardware latency, or the buffer state. So we just pop data and throw
256      * it at SDL's face. Nah. */
257
258     vlc_mutex_lock( &p_aout->output_fifo_lock );
259     p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
260     vlc_mutex_unlock( &p_aout->output_fifo_lock );
261
262     if ( p_buffer != NULL )
263     {
264         p_aout->p_libvlc->pf_memcpy( p_stream, p_buffer->p_buffer, i_len );
265         aout_BufferFree( p_buffer );
266     }
267     else
268     {
269         p_aout->p_libvlc->pf_memset( p_stream, 0, i_len );
270     }
271 }
272