sdl.c 9.11 KB
Newer Older
1
/*****************************************************************************
2
 * sdl.c : SDL audio output plugin for vlc
3
 *****************************************************************************
4
 * Copyright (C) 2000-2002 the VideoLAN team
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
Antoine Cellerier's avatar
Antoine Cellerier 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>
Clément Stenac's avatar
Clément Stenac 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        ( aout_instance_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
    aout_instance_t *p_aout = (aout_instance_t *)p_this;
83 84
    SDL_AudioSpec desired, obtained;
    int i_nb_channels;
Gildas Bazin's avatar
 
Gildas Bazin 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
    }

Rémi Duraffort's avatar
Rémi Duraffort committed
108
    if( var_Get( p_aout, "audio-device", &val ) != VLC_ENOVAR )
109 110
    {
        /* The user has selected an audio device. */
Gildas Bazin's avatar
 
Gildas Bazin 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;
        }
Gildas Bazin's avatar
 
Gildas Bazin 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);

Gildas Bazin's avatar
 
Gildas Bazin committed
170
        if ( var_Type( p_aout, "audio-device" ) == 0 )
171
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
172 173
            var_Create( p_aout, "audio-device",
                        VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
174
            text.psz_string = _("Audio Device");
Gildas Bazin's avatar
 
Gildas Bazin 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");
Gildas Bazin's avatar
 
Gildas Bazin 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
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
187
    else if ( var_Type( p_aout, "audio-device" ) == 0 )
188 189
    {
        /* First launch. */
Gildas Bazin's avatar
 
Gildas Bazin committed
190 191
        var_Create( p_aout, "audio-device",
                    VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
192
        text.psz_string = _("Audio Device");
Gildas Bazin's avatar
 
Gildas Bazin 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");
Gildas Bazin's avatar
 
Gildas Bazin 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");
Gildas Bazin's avatar
 
Gildas Bazin committed
200
        var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
201 202
        if ( i_nb_channels == 2 )
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
203
            val.i_int = AOUT_VAR_STEREO;
204 205 206
        }
        else
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
207
            val.i_int = AOUT_VAR_MONO;
208
        }
Gildas Bazin's avatar
 
Gildas Bazin 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

220
    return VLC_SUCCESS;
221 222 223 224 225
}

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

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

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

250 251 252
    /* 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. */
253

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

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