1 /*****************************************************************************
2 * sdl.c : SDL audio output plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2002 the VideoLAN team
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>
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.
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.
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 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
30 #include <unistd.h> /* write(), close() */
39 #include SDL_INCLUDE_FILE
41 #define FRAME_SIZE 2048
43 /*****************************************************************************
44 * aout_sys_t: SDL audio output method descriptor
45 *****************************************************************************
46 * This structure is part of the audio output thread descriptor.
47 * It describes the specific properties of an audio device.
48 *****************************************************************************/
55 /*****************************************************************************
57 *****************************************************************************/
58 static int Open ( vlc_object_t
* );
59 static void Close ( vlc_object_t
* );
60 static void Play ( aout_instance_t
* );
61 static void SDLCallback ( void *, byte_t
*, int );
63 /*****************************************************************************
65 *****************************************************************************/
67 set_shortname( "SDL" );
68 set_description( _("Simple DirectMedia Layer audio output") );
69 set_capability( "audio output", 40 );
70 set_category( CAT_AUDIO
);
71 set_subcategory( SUBCAT_AUDIO_AOUT
);
72 add_shortcut( "sdl" );
73 set_callbacks( Open
, Close
);
76 /*****************************************************************************
77 * Open: open the audio device
78 *****************************************************************************/
79 static int Open ( vlc_object_t
*p_this
)
81 aout_instance_t
*p_aout
= (aout_instance_t
*)p_this
;
82 SDL_AudioSpec desired
, obtained
;
84 vlc_value_t val
, text
;
86 /* Check that no one uses the DSP. */
87 uint32_t i_flags
= SDL_INIT_AUDIO
;
88 if( SDL_WasInit( i_flags
) )
94 /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet */
95 i_flags
|= SDL_INIT_EVENTTHREAD
;
98 /* In debug mode you may want vlc to dump a core instead of staying
100 i_flags
|= SDL_INIT_NOPARACHUTE
;
103 /* Initialize library */
104 if( SDL_Init( i_flags
) < 0 )
106 msg_Err( p_aout
, "cannot initialize SDL (%s)", SDL_GetError() );
110 if ( var_Type( p_aout
, "audio-device" ) != 0 )
112 /* The user has selected an audio device. */
114 var_Get( p_aout
, "audio-device", &val
);
115 if ( val
.i_int
== AOUT_VAR_STEREO
)
117 p_aout
->output
.output
.i_physical_channels
118 = AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
;
120 else if ( val
.i_int
== AOUT_VAR_MONO
)
122 p_aout
->output
.output
.i_physical_channels
= AOUT_CHAN_CENTER
;
126 i_nb_channels
= aout_FormatNbChannels( &p_aout
->output
.output
);
127 if ( i_nb_channels
> 2 )
129 /* SDL doesn't support more than two channels. */
131 p_aout
->output
.output
.i_physical_channels
132 = AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
;
134 desired
.freq
= p_aout
->output
.output
.i_rate
;
135 desired
.format
= AUDIO_S16SYS
;
136 desired
.channels
= i_nb_channels
;
137 desired
.callback
= SDLCallback
;
138 desired
.userdata
= p_aout
;
139 desired
.samples
= FRAME_SIZE
;
141 /* Open the sound device. */
142 if( SDL_OpenAudio( &desired
, &obtained
) < 0 )
149 /* Now have a look at what we got. */
150 switch ( obtained
.format
)
153 p_aout
->output
.output
.i_format
= VLC_FOURCC('s','1','6','l'); break;
155 p_aout
->output
.output
.i_format
= VLC_FOURCC('s','1','6','b'); break;
157 p_aout
->output
.output
.i_format
= VLC_FOURCC('u','1','6','l'); break;
159 p_aout
->output
.output
.i_format
= VLC_FOURCC('u','1','6','b'); break;
161 p_aout
->output
.output
.i_format
= VLC_FOURCC('s','8',' ',' '); break;
163 p_aout
->output
.output
.i_format
= VLC_FOURCC('u','8',' ',' '); break;
165 /* Volume is entirely done in software. */
166 aout_VolumeSoftInit( p_aout
);
168 if ( obtained
.channels
!= i_nb_channels
)
170 p_aout
->output
.output
.i_physical_channels
= (obtained
.channels
== 2 ?
171 AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
:
174 if ( var_Type( p_aout
, "audio-device" ) == 0 )
176 var_Create( p_aout
, "audio-device",
177 VLC_VAR_INTEGER
| VLC_VAR_HASCHOICE
);
178 text
.psz_string
= _("Audio Device");
179 var_Change( p_aout
, "audio-device", VLC_VAR_SETTEXT
, &text
, NULL
);
181 val
.i_int
= (obtained
.channels
== 2) ? AOUT_VAR_STEREO
:
183 text
.psz_string
= (obtained
.channels
== 2) ? N_("Stereo") :
185 var_Change( p_aout
, "audio-device",
186 VLC_VAR_ADDCHOICE
, &val
, &text
);
187 var_AddCallback( p_aout
, "audio-device", aout_ChannelsRestart
,
191 else if ( var_Type( p_aout
, "audio-device" ) == 0 )
194 var_Create( p_aout
, "audio-device",
195 VLC_VAR_INTEGER
| VLC_VAR_HASCHOICE
);
196 text
.psz_string
= _("Audio Device");
197 var_Change( p_aout
, "audio-device", VLC_VAR_SETTEXT
, &text
, NULL
);
199 val
.i_int
= AOUT_VAR_STEREO
;
200 text
.psz_string
= N_("Stereo");
201 var_Change( p_aout
, "audio-device", VLC_VAR_ADDCHOICE
, &val
, &text
);
202 val
.i_int
= AOUT_VAR_MONO
;
203 text
.psz_string
= N_("Mono");
204 var_Change( p_aout
, "audio-device", VLC_VAR_ADDCHOICE
, &val
, &text
);
205 if ( i_nb_channels
== 2 )
207 val
.i_int
= AOUT_VAR_STEREO
;
211 val
.i_int
= AOUT_VAR_MONO
;
213 var_Change( p_aout
, "audio-device", VLC_VAR_SETDEFAULT
, &val
, NULL
);
214 var_AddCallback( p_aout
, "audio-device", aout_ChannelsRestart
, NULL
);
217 val
.b_bool
= VLC_TRUE
;
218 var_Set( p_aout
, "intf-change", val
);
220 p_aout
->output
.output
.i_rate
= obtained
.freq
;
221 p_aout
->output
.i_nb_samples
= obtained
.samples
;
222 p_aout
->output
.pf_play
= Play
;
227 /*****************************************************************************
228 * Play: play a sound samples buffer
229 *****************************************************************************/
230 static void Play( aout_instance_t
* p_aout
)
235 /*****************************************************************************
236 * Close: close the audio device
237 *****************************************************************************/
238 static void Close ( vlc_object_t
*p_this
)
243 SDL_QuitSubSystem( SDL_INIT_AUDIO
);
246 /*****************************************************************************
247 * SDLCallback: what to do once SDL has played sound samples
248 *****************************************************************************/
249 static void SDLCallback( void * _p_aout
, byte_t
* p_stream
, int i_len
)
251 aout_instance_t
* p_aout
= (aout_instance_t
*)_p_aout
;
252 aout_buffer_t
* p_buffer
;
254 /* SDL is unable to call us at regular times, or tell us its current
255 * hardware latency, or the buffer state. So we just pop data and throw
256 * it at SDL's face. Nah. */
258 vlc_mutex_lock( &p_aout
->output_fifo_lock
);
259 p_buffer
= aout_FifoPop( p_aout
, &p_aout
->output
.fifo
);
260 vlc_mutex_unlock( &p_aout
->output_fifo_lock
);
262 if ( p_buffer
!= NULL
)
264 p_aout
->p_libvlc
->pf_memcpy( p_stream
, p_buffer
->p_buffer
, i_len
);
265 aout_BufferFree( p_buffer
);
269 p_aout
->p_libvlc
->pf_memset( p_stream
, 0, i_len
);