]> git.sesse.net Git - vlc/blob - modules/audio_output/sdl.c
Major change of the channels management. p_format->i_channels disappeares
[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.15 2002/11/14 22:38:47 massiot Exp $
6  *
7  * Authors: Michel Kaempf <maxx@via.ecp.fr>
8  *          Samuel 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 module") );
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
83     /* Check that no one uses the DSP. */
84     Uint32 i_flags = SDL_INIT_AUDIO;
85     if( SDL_WasInit( i_flags ) )
86     {
87         return VLC_EGENERIC;
88     }
89
90 #ifndef WIN32
91     /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet */
92     i_flags |= SDL_INIT_EVENTTHREAD;
93 #endif
94 #ifdef DEBUG
95     /* In debug mode you may want vlc to dump a core instead of staying
96      * stuck */
97     i_flags |= SDL_INIT_NOPARACHUTE;
98 #endif
99
100     /* Initialize library */
101     if( SDL_Init( i_flags ) < 0 )
102     {
103         msg_Err( p_aout, "cannot initialize SDL (%s)", SDL_GetError() );
104         return VLC_EGENERIC;
105     }
106
107     if ( var_Type( p_aout, "audio-device" ) ==
108              (VLC_VAR_STRING | VLC_VAR_ISLIST) )
109     {
110         /* The user has selected an audio device. */
111         vlc_value_t val;
112         var_Get( p_aout, "audio-device", &val );
113         if ( !strcmp( val.psz_string, N_("Stereo") ) )
114         {
115             p_aout->output.output.i_physical_channels
116                 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
117         }
118         else if ( !strcmp( val.psz_string, N_("Mono") ) )
119         {
120             p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
121         }
122         free( val.psz_string );
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             vlc_value_t val;
176             var_Create( p_aout, "audio-device", VLC_VAR_STRING | VLC_VAR_ISLIST );
177             val.psz_string = (obtained.channels == 2) ? N_("Stereo") :
178                               N_("Mono");
179             var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
180             var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart,
181                              NULL );
182         }
183     }
184     else if ( var_Type( p_aout, "audio-device" ) < 0 )
185     {
186         /* First launch. */
187         vlc_value_t val;
188         var_Create( p_aout, "audio-device", VLC_VAR_STRING | VLC_VAR_ISLIST );
189         val.psz_string = N_("Stereo");
190         var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
191         val.psz_string = N_("Mono");
192         var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
193         if ( i_nb_channels == 2 )
194         {
195             val.psz_string = N_("Stereo");
196         }
197         else
198         {
199             val.psz_string = N_("Mono");
200         }
201         var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val );
202
203         var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart,
204                          NULL );
205     }
206
207     p_aout->output.output.i_rate = obtained.freq;
208     p_aout->output.i_nb_samples = obtained.samples;
209     p_aout->output.pf_play = Play;
210
211     return VLC_SUCCESS;
212 }
213
214 /*****************************************************************************
215  * Play: play a sound samples buffer
216  *****************************************************************************/
217 static void Play( aout_instance_t * p_aout )
218 {
219 }
220
221 /*****************************************************************************
222  * Close: close the audio device
223  *****************************************************************************/
224 static void Close ( vlc_object_t *p_this )
225 {
226     SDL_PauseAudio( 1 );
227     SDL_CloseAudio();
228     SDL_QuitSubSystem( SDL_INIT_AUDIO );
229 }
230
231 /*****************************************************************************
232  * SDLCallback: what to do once SDL has played sound samples
233  *****************************************************************************/
234 static void SDLCallback( void * _p_aout, byte_t * p_stream, int i_len )
235 {
236     aout_instance_t * p_aout = (aout_instance_t *)_p_aout;
237     aout_buffer_t *   p_buffer;
238
239     /* SDL is unable to call us at regular times, or tell us its current
240      * hardware latency, or the buffer state. So we just pop data and throw
241      * it at SDL's face. Nah. */
242
243     vlc_mutex_lock( &p_aout->output_fifo_lock );
244     p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
245     vlc_mutex_unlock( &p_aout->output_fifo_lock );
246
247     if ( p_buffer != NULL )
248     {
249         p_aout->p_vlc->pf_memcpy( p_stream, p_buffer->p_buffer, i_len );
250         aout_BufferFree( p_buffer );
251     }
252     else
253     {
254         p_aout->p_vlc->pf_memset( p_stream, 0, i_len );
255     }
256 }
257