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