1 /*****************************************************************************
2 * sdl.c : SDL audio output plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2002 VideoLAN
5 * $Id: sdl.c,v 1.14 2002/10/16 23:12:45 massiot Exp $
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>
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 *****************************************************************************/
27 /*****************************************************************************
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() */
38 #include "aout_internal.h"
40 #include SDL_INCLUDE_FILE
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 *, int );
64 /*****************************************************************************
66 *****************************************************************************/
68 set_description( _("Simple DirectMedia Layer audio module") );
69 set_capability( "audio output", 40 );
70 add_shortcut( "sdl" );
71 set_callbacks( Open, Close );
74 /*****************************************************************************
75 * Open: open the audio device
76 *****************************************************************************/
77 static int Open ( vlc_object_t *p_this )
79 aout_instance_t *p_aout = (aout_instance_t *)p_this;
80 SDL_AudioSpec desired, obtained;
83 /* Check that no one uses the DSP. */
84 Uint32 i_flags = SDL_INIT_AUDIO;
85 if( SDL_WasInit( i_flags ) )
91 /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet */
92 i_flags |= SDL_INIT_EVENTTHREAD;
95 /* In debug mode you may want vlc to dump a core instead of staying
97 i_flags |= SDL_INIT_NOPARACHUTE;
100 /* Initialize library */
101 if( SDL_Init( i_flags ) < 0 )
103 msg_Err( p_aout, "cannot initialize SDL (%s)", SDL_GetError() );
107 i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
108 if ( i_nb_channels > 2 )
110 /* SDL doesn't support more than two channels. */
112 p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
114 desired.freq = p_aout->output.output.i_rate;
115 desired.format = AUDIO_S16SYS;
116 desired.channels = i_nb_channels;
117 desired.callback = SDLCallback;
118 desired.userdata = p_aout;
119 desired.samples = FRAME_SIZE;
121 /* Open the sound device. */
122 if( SDL_OpenAudio( &desired, &obtained ) < 0 )
129 /* Now have a look at what we got. */
130 switch ( obtained.format )
133 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l'); break;
135 p_aout->output.output.i_format = VLC_FOURCC('s','1','6','b'); break;
137 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','l'); break;
139 p_aout->output.output.i_format = VLC_FOURCC('u','1','6','b'); break;
141 p_aout->output.output.i_format = VLC_FOURCC('s','8',' ',' '); break;
143 p_aout->output.output.i_format = VLC_FOURCC('u','8',' ',' '); break;
145 /* Volume is entirely done in software. */
146 aout_VolumeSoftInit( p_aout );
148 if ( obtained.channels != i_nb_channels )
150 p_aout->output.output.i_channels = (obtained.channels == 2 ?
154 p_aout->output.output.i_rate = obtained.freq;
155 p_aout->output.i_nb_samples = obtained.samples;
156 p_aout->output.pf_play = Play;
161 /*****************************************************************************
162 * Play: play a sound samples buffer
163 *****************************************************************************/
164 static void Play( aout_instance_t * p_aout )
168 /*****************************************************************************
169 * Close: close the audio device
170 *****************************************************************************/
171 static void Close ( vlc_object_t *p_this )
175 SDL_QuitSubSystem( SDL_INIT_AUDIO );
178 /*****************************************************************************
179 * SDLCallback: what to do once SDL has played sound samples
180 *****************************************************************************/
181 static void SDLCallback( void * _p_aout, byte_t * p_stream, int i_len )
183 aout_instance_t * p_aout = (aout_instance_t *)_p_aout;
184 aout_buffer_t * p_buffer;
186 /* SDL is unable to call us at regular times, or tell us its current
187 * hardware latency, or the buffer state. So we just pop data and throw
188 * it at SDL's face. Nah. */
190 vlc_mutex_lock( &p_aout->output_fifo_lock );
191 p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
192 vlc_mutex_unlock( &p_aout->output_fifo_lock );
194 if ( p_buffer != NULL )
196 p_aout->p_vlc->pf_memcpy( p_stream, p_buffer->p_buffer, i_len );
197 aout_BufferFree( p_buffer );
201 p_aout->p_vlc->pf_memset( p_stream, 0, i_len );