sdl.c 9.13 KB
Newer Older
1
/*****************************************************************************
2
 * sdl.c : SDL audio output plugin for vlc
3
 *****************************************************************************
4
 * Copyright (C) 2000-2002 the VideoLAN team
zorglub's avatar
zorglub committed
5
 * $Id$
6 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>
Sam Hocevar's avatar
Sam Hocevar committed
11
 *
12 13 14 15
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
Sam Hocevar's avatar
Sam Hocevar committed
16
 *
17 18 19 20 21 22 23
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
24
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 26 27 28 29 30 31
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <unistd.h>                                      /* write(), close() */

32 33 34 35
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

36
#include <vlc_common.h>
37
#include <vlc_plugin.h>
zorglub's avatar
zorglub committed
38
#include <vlc_aout.h>
39

40
#include <SDL.h>
41

42 43 44 45 46 47 48 49 50
#define FRAME_SIZE 2048

/*****************************************************************************
 * aout_sys_t: SDL audio output method descriptor
 *****************************************************************************
 * This structure is part of the audio output thread descriptor.
 * It describes the specific properties of an audio device.
 *****************************************************************************/
struct aout_sys_t
Sam Hocevar's avatar
Sam Hocevar committed
51
{
52
    mtime_t next_date;
53 54
    mtime_t buffer_time;
};
55 56 57 58

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
59 60
static int  Open        ( vlc_object_t * );
static void Close       ( vlc_object_t * );
61
static void Play        ( audio_output_t * );
62
static void SDLCallback ( void *, uint8_t *, int );
63 64

/*****************************************************************************
65
 * Module descriptor
66
 *****************************************************************************/
67 68 69 70 71 72 73 74 75
vlc_module_begin ()
    set_shortname( "SDL" )
    set_description( N_("Simple DirectMedia Layer audio output") )
    set_capability( "audio output", 40 )
    set_category( CAT_AUDIO )
    set_subcategory( SUBCAT_AUDIO_AOUT )
    add_shortcut( "sdl" )
    set_callbacks( Open, Close )
vlc_module_end ()
76 77 78 79 80

/*****************************************************************************
 * Open: open the audio device
 *****************************************************************************/
static int Open ( vlc_object_t *p_this )
81
{
82
    audio_output_t *p_aout = (audio_output_t *)p_this;
83 84
    SDL_AudioSpec desired, obtained;
    int i_nb_channels;
gbazin's avatar
 
gbazin committed
85
    vlc_value_t val, text;
86

87
    /* Check that no one uses the DSP. */
Sam Hocevar's avatar
Sam Hocevar committed
88
    uint32_t i_flags = SDL_INIT_AUDIO;
89
    if( SDL_WasInit( i_flags ) )
90
    {
91 92 93
        return VLC_EGENERIC;
    }

94
    i_flags |= SDL_INIT_EVENTTHREAD;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
95
#ifndef NDEBUG
96 97
    /* In debug mode you may want vlc to dump a core instead of staying
     * stuck */
98
    i_flags |= SDL_INIT_NOPARACHUTE;
99
#endif
100 101 102

    /* Initialize library */
    if( SDL_Init( i_flags ) < 0 )
103 104
    {
        msg_Err( p_aout, "cannot initialize SDL (%s)", SDL_GetError() );
105
        return VLC_EGENERIC;
106 107
    }

ivoire's avatar
ivoire committed
108
    if( var_Get( p_aout, "audio-device", &val ) != VLC_ENOVAR )
109 110
    {
        /* The user has selected an audio device. */
gbazin's avatar
 
gbazin committed
111
        if ( val.i_int == AOUT_VAR_STEREO )
112
        {
113
            p_aout->format.i_physical_channels
114 115
                = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
        }
gbazin's avatar
 
gbazin committed
116
        else if ( val.i_int == AOUT_VAR_MONO )
117
        {
118
            p_aout->format.i_physical_channels = AOUT_CHAN_CENTER;
119 120 121
        }
    }

122
    i_nb_channels = aout_FormatNbChannels( &p_aout->format );
123 124 125 126
    if ( i_nb_channels > 2 )
    {
        /* SDL doesn't support more than two channels. */
        i_nb_channels = 2;
127
        p_aout->format.i_physical_channels
128
            = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
129
    }
130
    desired.freq       = p_aout->format.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;
136

137 138
    /* Open the sound device. */
    if( SDL_OpenAudio( &desired, &obtained ) < 0 )
139
    {
140
        return VLC_EGENERIC;
141 142
    }

143
    SDL_PauseAudio( 0 );
144

145 146 147 148
    /* Now have a look at what we got. */
    switch ( obtained.format )
    {
    case AUDIO_S16LSB:
149
        p_aout->format.i_format = VLC_CODEC_S16L; break;
150
    case AUDIO_S16MSB:
151
        p_aout->format.i_format = VLC_CODEC_S16B; break;
152
    case AUDIO_U16LSB:
153
        p_aout->format.i_format = VLC_CODEC_U16L; break;
154
    case AUDIO_U16MSB:
155
        p_aout->format.i_format = VLC_CODEC_U16B; break;
156
    case AUDIO_S8:
157
        p_aout->format.i_format = VLC_CODEC_S8; break;
158
    case AUDIO_U8:
159
        p_aout->format.i_format = VLC_CODEC_U8; break;
160 161 162 163 164 165
    }
    /* Volume is entirely done in software. */
    aout_VolumeSoftInit( p_aout );

    if ( obtained.channels != i_nb_channels )
    {
166
        p_aout->format.i_physical_channels = (obtained.channels == 2 ?
167 168 169
                                            AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT :
                                            AOUT_CHAN_CENTER);

gbazin's avatar
 
gbazin committed
170
        if ( var_Type( p_aout, "audio-device" ) == 0 )
171
        {
gbazin's avatar
 
gbazin committed
172 173
            var_Create( p_aout, "audio-device",
                        VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
174
            text.psz_string = _("Audio Device");
gbazin's avatar
 
gbazin committed
175 176 177
            var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );

            val.i_int = (obtained.channels == 2) ? AOUT_VAR_STEREO :
178
                        AOUT_VAR_MONO;
179 180
            text.psz_string = (obtained.channels == 2) ? _("Stereo") :
                              _("Mono");
gbazin's avatar
 
gbazin committed
181 182
            var_Change( p_aout, "audio-device",
                        VLC_VAR_ADDCHOICE, &val, &text );
183 184 185
            var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart,
                             NULL );
        }
186
    }
gbazin's avatar
 
gbazin committed
187
    else if ( var_Type( p_aout, "audio-device" ) == 0 )
188 189
    {
        /* First launch. */
gbazin's avatar
 
gbazin committed
190 191
        var_Create( p_aout, "audio-device",
                    VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
192
        text.psz_string = _("Audio Device");
gbazin's avatar
 
gbazin committed
193 194 195
        var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );

        val.i_int = AOUT_VAR_STEREO;
196
        text.psz_string = _("Stereo");
gbazin's avatar
 
gbazin committed
197 198
        var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
        val.i_int = AOUT_VAR_MONO;
199
        text.psz_string = _("Mono");
gbazin's avatar
 
gbazin committed
200
        var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
201 202
        if ( i_nb_channels == 2 )
        {
gbazin's avatar
 
gbazin committed
203
            val.i_int = AOUT_VAR_STEREO;
204 205 206
        }
        else
        {
gbazin's avatar
 
gbazin committed
207
            val.i_int = AOUT_VAR_MONO;
208
        }
gbazin's avatar
 
gbazin committed
209 210
        var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
        var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
211 212
    }

213
    var_TriggerCallback( p_aout, "intf-change" );
214

215 216 217 218
    p_aout->format.i_rate = obtained.freq;
    p_aout->i_nb_samples = obtained.samples;
    p_aout->pf_play = Play;
    p_aout->pf_pause = NULL;
219
    p_aout->pf_flush = NULL;
220

221
    return VLC_SUCCESS;
222 223 224 225 226
}

/*****************************************************************************
 * Play: play a sound samples buffer
 *****************************************************************************/
227
static void Play( audio_output_t * p_aout )
228
{
229
    VLC_UNUSED(p_aout);
230 231 232
}

/*****************************************************************************
233
 * Close: close the audio device
234
 *****************************************************************************/
235
static void Close ( vlc_object_t *p_this )
236
{
237
    VLC_UNUSED(p_this);
238
    SDL_PauseAudio( 1 );
239 240 241 242 243 244 245
    SDL_CloseAudio();
    SDL_QuitSubSystem( SDL_INIT_AUDIO );
}

/*****************************************************************************
 * SDLCallback: what to do once SDL has played sound samples
 *****************************************************************************/
246
static void SDLCallback( void * _p_aout, uint8_t * p_stream, int i_len )
247
{
248
    audio_output_t * p_aout = (audio_output_t *)_p_aout;
249
    aout_buffer_t *   p_buffer;
250

251 252 253
    /* SDL is unable to call us at regular times, or tell us its current
     * hardware latency, or the buffer state. So we just pop data and throw
     * it at SDL's face. Nah. */
254

255
    vlc_mutex_lock( &p_aout->lock );
256
    p_buffer = aout_FifoPop( &p_aout->fifo );
257
    vlc_mutex_unlock( &p_aout->lock );
258

259 260
    if ( p_buffer != NULL )
    {
261
        vlc_memcpy( p_stream, p_buffer->p_buffer, i_len );
262 263 264
        aout_BufferFree( p_buffer );
    }
    else
265
    {
266
        vlc_memset( p_stream, 0, i_len );
267 268 269
    }
}