]> git.sesse.net Git - vlc/blob - modules/audio_output/sdl.c
Remove most stray semi-colons in module descriptions
[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_common.h>
37 #include <vlc_plugin.h>
38 #include <vlc_aout.h>
39
40 #include SDL_INCLUDE_FILE
41
42 #define FRAME_SIZE 2048
43
44 /*****************************************************************************
45  * aout_sys_t: SDL audio output method descriptor
46  *****************************************************************************
47  * This structure is part of the audio output thread descriptor.
48  * It describes the specific properties of an audio device.
49  *****************************************************************************/
50 struct aout_sys_t
51 {
52     mtime_t next_date;
53     mtime_t buffer_time;
54 };
55
56 /*****************************************************************************
57  * Local prototypes
58  *****************************************************************************/
59 static int  Open        ( vlc_object_t * );
60 static void Close       ( vlc_object_t * );
61 static void Play        ( aout_instance_t * );
62 static void SDLCallback ( void *, uint8_t *, int );
63
64 /*****************************************************************************
65  * Module descriptor
66  *****************************************************************************/
67 vlc_module_begin ()
68     set_shortname( "SDL" )
69     set_description( N_("Simple DirectMedia Layer audio output") )
70     set_capability( "audio output", 40 )
71     set_category( CAT_AUDIO )
72     set_subcategory( SUBCAT_AUDIO_AOUT )
73     add_shortcut( "sdl" )
74     set_callbacks( Open, Close )
75 vlc_module_end ()
76
77 /*****************************************************************************
78  * Open: open the audio device
79  *****************************************************************************/
80 static int Open ( vlc_object_t *p_this )
81 {
82     aout_instance_t *p_aout = (aout_instance_t *)p_this;
83     SDL_AudioSpec desired, obtained;
84     int i_nb_channels;
85     vlc_value_t val, text;
86
87     /* Check that no one uses the DSP. */
88     uint32_t i_flags = SDL_INIT_AUDIO;
89     if( SDL_WasInit( i_flags ) )
90     {
91         return VLC_EGENERIC;
92     }
93
94 #ifndef WIN32
95     /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet */
96     i_flags |= SDL_INIT_EVENTTHREAD;
97 #endif
98 #ifndef NDEBUG
99     /* In debug mode you may want vlc to dump a core instead of staying
100      * stuck */
101     i_flags |= SDL_INIT_NOPARACHUTE;
102 #endif
103
104     /* Initialize library */
105     if( SDL_Init( i_flags ) < 0 )
106     {
107         msg_Err( p_aout, "cannot initialize SDL (%s)", SDL_GetError() );
108         return VLC_EGENERIC;
109     }
110
111     if ( var_Type( p_aout, "audio-device" ) != 0 )
112     {
113         /* The user has selected an audio device. */
114         vlc_value_t val;
115         var_Get( p_aout, "audio-device", &val );
116         if ( val.i_int == AOUT_VAR_STEREO )
117         {
118             p_aout->output.output.i_physical_channels
119                 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
120         }
121         else if ( val.i_int == AOUT_VAR_MONO )
122         {
123             p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
124         }
125     }
126
127     i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
128     if ( i_nb_channels > 2 )
129     {
130         /* SDL doesn't support more than two channels. */
131         i_nb_channels = 2;
132         p_aout->output.output.i_physical_channels
133             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
134     }
135     desired.freq       = p_aout->output.output.i_rate;
136     desired.format     = AUDIO_S16SYS;
137     desired.channels   = i_nb_channels;
138     desired.callback   = SDLCallback;
139     desired.userdata   = p_aout;
140     desired.samples    = FRAME_SIZE;
141
142     /* Open the sound device. */
143     if( SDL_OpenAudio( &desired, &obtained ) < 0 )
144     {
145         return VLC_EGENERIC;
146     }
147
148     SDL_PauseAudio( 0 );
149
150     /* Now have a look at what we got. */
151     switch ( obtained.format )
152     {
153     case AUDIO_S16LSB:
154         p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l'); break;
155     case AUDIO_S16MSB:
156         p_aout->output.output.i_format = VLC_FOURCC('s','1','6','b'); break;
157     case AUDIO_U16LSB:
158         p_aout->output.output.i_format = VLC_FOURCC('u','1','6','l'); break;
159     case AUDIO_U16MSB:
160         p_aout->output.output.i_format = VLC_FOURCC('u','1','6','b'); break;
161     case AUDIO_S8:
162         p_aout->output.output.i_format = VLC_FOURCC('s','8',' ',' '); break;
163     case AUDIO_U8:
164         p_aout->output.output.i_format = VLC_FOURCC('u','8',' ',' '); break;
165     }
166     /* Volume is entirely done in software. */
167     aout_VolumeSoftInit( p_aout );
168
169     if ( obtained.channels != i_nb_channels )
170     {
171         p_aout->output.output.i_physical_channels = (obtained.channels == 2 ?
172                                             AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT :
173                                             AOUT_CHAN_CENTER);
174
175         if ( var_Type( p_aout, "audio-device" ) == 0 )
176         {
177             var_Create( p_aout, "audio-device",
178                         VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
179             text.psz_string = _("Audio Device");
180             var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
181
182             val.i_int = (obtained.channels == 2) ? AOUT_VAR_STEREO :
183                         AOUT_VAR_MONO;
184             text.psz_string = (obtained.channels == 2) ? N_("Stereo") :
185                               N_("Mono");
186             var_Change( p_aout, "audio-device",
187                         VLC_VAR_ADDCHOICE, &val, &text );
188             var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart,
189                              NULL );
190         }
191     }
192     else if ( var_Type( p_aout, "audio-device" ) == 0 )
193     {
194         /* First launch. */
195         var_Create( p_aout, "audio-device",
196                     VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
197         text.psz_string = _("Audio Device");
198         var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
199
200         val.i_int = AOUT_VAR_STEREO;
201         text.psz_string = N_("Stereo");
202         var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
203         val.i_int = AOUT_VAR_MONO;
204         text.psz_string = N_("Mono");
205         var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
206         if ( i_nb_channels == 2 )
207         {
208             val.i_int = AOUT_VAR_STEREO;
209         }
210         else
211         {
212             val.i_int = AOUT_VAR_MONO;
213         }
214         var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
215         var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
216     }
217
218     val.b_bool = true;
219     var_Set( p_aout, "intf-change", val );
220
221     p_aout->output.output.i_rate = obtained.freq;
222     p_aout->output.i_nb_samples = obtained.samples;
223     p_aout->output.pf_play = Play;
224
225     return VLC_SUCCESS;
226 }
227
228 /*****************************************************************************
229  * Play: play a sound samples buffer
230  *****************************************************************************/
231 static void Play( aout_instance_t * p_aout )
232 {
233     VLC_UNUSED(p_aout);
234 }
235
236 /*****************************************************************************
237  * Close: close the audio device
238  *****************************************************************************/
239 static void Close ( vlc_object_t *p_this )
240 {
241     VLC_UNUSED(p_this);
242     SDL_PauseAudio( 1 );
243     SDL_CloseAudio();
244     SDL_QuitSubSystem( SDL_INIT_AUDIO );
245 }
246
247 /*****************************************************************************
248  * SDLCallback: what to do once SDL has played sound samples
249  *****************************************************************************/
250 static void SDLCallback( void * _p_aout, uint8_t * p_stream, int i_len )
251 {
252     aout_instance_t * p_aout = (aout_instance_t *)_p_aout;
253     aout_buffer_t *   p_buffer;
254
255     /* SDL is unable to call us at regular times, or tell us its current
256      * hardware latency, or the buffer state. So we just pop data and throw
257      * it at SDL's face. Nah. */
258
259     vlc_mutex_lock( &p_aout->output_fifo_lock );
260     p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
261     vlc_mutex_unlock( &p_aout->output_fifo_lock );
262
263     if ( p_buffer != NULL )
264     {
265         vlc_memcpy( p_stream, p_buffer->p_buffer, i_len );
266         aout_BufferFree( p_buffer );
267     }
268     else
269     {
270         vlc_memset( p_stream, 0, i_len );
271     }
272 }
273