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