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