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() */
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
42 #define FRAME_SIZE 2048
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 *****************************************************************************/
56 /*****************************************************************************
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 *, uint8_t *, int );
64 /*****************************************************************************
66 *****************************************************************************/
68 set_shortname( "SDL" )
69 set_description( N_("Simple DirectMedia Layer audio output") )
70 set_capability( "audio output", 40 )
71 set_category( CAT_AUDIO
)
72 set_subcategory( SUBCAT_AUDIO_AOUT
)
74 set_callbacks( Open
, Close
)
77 /*****************************************************************************
78 * Open: open the audio device
79 *****************************************************************************/
80 static int Open ( vlc_object_t
*p_this
)
82 aout_instance_t
*p_aout
= (aout_instance_t
*)p_this
;
83 SDL_AudioSpec desired
, obtained
;
85 vlc_value_t val
, text
;
87 /* Check that no one uses the DSP. */
88 uint32_t i_flags
= SDL_INIT_AUDIO
;
89 if( SDL_WasInit( i_flags
) )
94 i_flags
|= SDL_INIT_EVENTTHREAD
;
96 /* In debug mode you may want vlc to dump a core instead of staying
98 i_flags
|= SDL_INIT_NOPARACHUTE
;
101 /* Initialize library */
102 if( SDL_Init( i_flags
) < 0 )
104 msg_Err( p_aout
, "cannot initialize SDL (%s)", SDL_GetError() );
108 if( var_Get( p_aout
, "audio-device", &val
) != VLC_ENOVAR
)
110 /* The user has selected an audio device. */
111 if ( val
.i_int
== AOUT_VAR_STEREO
)
113 p_aout
->output
.output
.i_physical_channels
114 = AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
;
116 else if ( val
.i_int
== AOUT_VAR_MONO
)
118 p_aout
->output
.output
.i_physical_channels
= AOUT_CHAN_CENTER
;
122 i_nb_channels
= aout_FormatNbChannels( &p_aout
->output
.output
);
123 if ( i_nb_channels
> 2 )
125 /* SDL doesn't support more than two channels. */
127 p_aout
->output
.output
.i_physical_channels
128 = AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
;
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
;
137 /* Open the sound device. */
138 if( SDL_OpenAudio( &desired
, &obtained
) < 0 )
145 /* Now have a look at what we got. */
146 switch ( obtained
.format
)
149 p_aout
->output
.output
.i_format
= VLC_CODEC_S16L
; break;
151 p_aout
->output
.output
.i_format
= VLC_CODEC_S16B
; break;
153 p_aout
->output
.output
.i_format
= VLC_CODEC_U16L
; break;
155 p_aout
->output
.output
.i_format
= VLC_CODEC_U16B
; break;
157 p_aout
->output
.output
.i_format
= VLC_CODEC_S8
; break;
159 p_aout
->output
.output
.i_format
= VLC_CODEC_U8
; break;
161 /* Volume is entirely done in software. */
162 aout_VolumeSoftInit( p_aout
);
164 if ( obtained
.channels
!= i_nb_channels
)
166 p_aout
->output
.output
.i_physical_channels
= (obtained
.channels
== 2 ?
167 AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
:
170 if ( var_Type( p_aout
, "audio-device" ) == 0 )
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
);
177 val
.i_int
= (obtained
.channels
== 2) ? AOUT_VAR_STEREO
:
179 text
.psz_string
= (obtained
.channels
== 2) ? _("Stereo") :
181 var_Change( p_aout
, "audio-device",
182 VLC_VAR_ADDCHOICE
, &val
, &text
);
183 var_AddCallback( p_aout
, "audio-device", aout_ChannelsRestart
,
187 else if ( var_Type( p_aout
, "audio-device" ) == 0 )
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
);
195 val
.i_int
= AOUT_VAR_STEREO
;
196 text
.psz_string
= _("Stereo");
197 var_Change( p_aout
, "audio-device", VLC_VAR_ADDCHOICE
, &val
, &text
);
198 val
.i_int
= AOUT_VAR_MONO
;
199 text
.psz_string
= _("Mono");
200 var_Change( p_aout
, "audio-device", VLC_VAR_ADDCHOICE
, &val
, &text
);
201 if ( i_nb_channels
== 2 )
203 val
.i_int
= AOUT_VAR_STEREO
;
207 val
.i_int
= AOUT_VAR_MONO
;
209 var_Change( p_aout
, "audio-device", VLC_VAR_SETDEFAULT
, &val
, NULL
);
210 var_AddCallback( p_aout
, "audio-device", aout_ChannelsRestart
, NULL
);
213 var_SetBool( p_aout
, "intf-change", true );
215 p_aout
->output
.output
.i_rate
= obtained
.freq
;
216 p_aout
->output
.i_nb_samples
= obtained
.samples
;
217 p_aout
->output
.pf_play
= Play
;
222 /*****************************************************************************
223 * Play: play a sound samples buffer
224 *****************************************************************************/
225 static void Play( aout_instance_t
* p_aout
)
230 /*****************************************************************************
231 * Close: close the audio device
232 *****************************************************************************/
233 static void Close ( vlc_object_t
*p_this
)
238 SDL_QuitSubSystem( SDL_INIT_AUDIO
);
241 /*****************************************************************************
242 * SDLCallback: what to do once SDL has played sound samples
243 *****************************************************************************/
244 static void SDLCallback( void * _p_aout
, uint8_t * p_stream
, int i_len
)
246 aout_instance_t
* p_aout
= (aout_instance_t
*)_p_aout
;
247 aout_buffer_t
* p_buffer
;
249 /* SDL is unable to call us at regular times, or tell us its current
250 * hardware latency, or the buffer state. So we just pop data and throw
251 * it at SDL's face. Nah. */
253 vlc_mutex_lock( &p_aout
->output_fifo_lock
);
254 p_buffer
= aout_FifoPop( p_aout
, &p_aout
->output
.fifo
);
255 vlc_mutex_unlock( &p_aout
->output_fifo_lock
);
257 if ( p_buffer
!= NULL
)
259 vlc_memcpy( p_stream
, p_buffer
->p_buffer
, i_len
);
260 aout_BufferFree( p_buffer
);
264 vlc_memset( p_stream
, 0, i_len
);