1 /*****************************************************************************
2 * sdl.c : SDL audio output plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2002 VideoLAN
5 * $Id: sdl.c,v 1.12 2002/09/18 21:21:23 massiot Exp $
7 * Authors: Michel Kaempf <maxx@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.org>
9 * Pierre Baillet <oct@zoy.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <errno.h> /* ENOMEM */
30 #include <fcntl.h> /* open(), O_WRONLY */
31 #include <string.h> /* strerror() */
32 #include <unistd.h> /* write(), close() */
33 #include <stdlib.h> /* calloc(), malloc(), free() */
37 #include "aout_internal.h"
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 *, Uint8 *, int );
63 /*****************************************************************************
65 *****************************************************************************/
67 set_description( _("Simple DirectMedia Layer audio module") );
68 set_capability( "audio output", 40 );
69 set_callbacks( Open, Close );
72 /*****************************************************************************
73 * Open: open the audio device
74 *****************************************************************************/
75 static int Open ( vlc_object_t *p_this )
77 aout_instance_t *p_aout = (aout_instance_t *)p_this;
78 SDL_AudioSpec desired;
80 Uint32 i_flags = SDL_INIT_AUDIO;
82 if( SDL_WasInit( i_flags ) )
87 p_aout->output.pf_play = Play;
89 aout_VolumeSoftInit( p_aout );
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 if ( p_aout->output.output.i_channels > 2 )
108 p_aout->output.output.i_channels = 2;
109 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
110 p_aout->output.i_nb_samples = FRAME_SIZE;
112 /* TODO: finish and clean this */
114 desired.freq = p_aout->output.output.i_rate;
115 desired.format = AUDIO_S16SYS;
116 desired.channels = p_aout->output.output.i_channels;
117 desired.callback = SDLCallback;
118 desired.userdata = p_aout;
119 desired.samples = FRAME_SIZE;
121 /* Open the sound device - FIXME : get the "natural" parameters */
122 if( SDL_OpenAudio( &desired, NULL ) < 0 )
132 /*****************************************************************************
133 * Play: play a sound samples buffer
134 *****************************************************************************/
135 static void Play( aout_instance_t * p_aout )
139 /*****************************************************************************
140 * Close: close the audio device
141 *****************************************************************************/
142 static void Close ( vlc_object_t *p_this )
146 SDL_QuitSubSystem( SDL_INIT_AUDIO );
149 /*****************************************************************************
150 * SDLCallback: what to do once SDL has played sound samples
151 *****************************************************************************/
152 static void SDLCallback( void * _p_aout, byte_t * p_stream, int i_len )
154 aout_instance_t * p_aout = (aout_instance_t *)_p_aout;
155 aout_buffer_t * p_buffer;
157 /* SDL is unable to call us at regular times, or tell us its current
158 * hardware latency, or the buffer state. So we just pop data and throw
159 * it at SDL's face. Nah. */
161 vlc_mutex_lock( &p_aout->output_fifo_lock );
162 p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
163 vlc_mutex_unlock( &p_aout->output_fifo_lock );
165 if ( p_buffer != NULL )
167 p_aout->p_vlc->pf_memcpy( p_stream, p_buffer->p_buffer, i_len );
168 aout_BufferFree( p_buffer );
172 p_aout->p_vlc->pf_memset( p_stream, 0, i_len );