]> git.sesse.net Git - vlc/blobdiff - modules/audio_output/sdl.c
* include/vlc_common.h:
[vlc] / modules / audio_output / sdl.c
index 0d63ee235dfeaa9ed16a3cf2267a8899f8c26664..7695bfd35383d687c33a0722234c6d71fbf8a3e3 100644 (file)
@@ -2,17 +2,18 @@
  * sdl.c : SDL audio output plugin for vlc
  *****************************************************************************
  * Copyright (C) 2000-2002 VideoLAN
- * $Id: sdl.c,v 1.6 2002/08/25 09:40:00 sam Exp $
+ * $Id: sdl.c,v 1.23 2003/10/25 00:49:13 sam Exp $
  *
  * Authors: Michel Kaempf <maxx@via.ecp.fr>
  *          Samuel Hocevar <sam@zoy.org>
  *          Pierre Baillet <oct@zoy.org>
- *      
+ *          Christophe Massiot <massiot@via.ecp.fr>
+ *
  * 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.
- * 
+ *
  * 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
@@ -47,8 +48,8 @@
  * It describes the specific properties of an audio device.
  *****************************************************************************/
 struct aout_sys_t
-{   
-    mtime_t call_time;
+{
+    mtime_t next_date;
     mtime_t buffer_time;
 };
 
@@ -57,18 +58,16 @@ struct aout_sys_t
  *****************************************************************************/
 static int  Open        ( vlc_object_t * );
 static void Close       ( vlc_object_t * );
-
-static int  SetFormat   ( aout_instance_t * );
 static void Play        ( aout_instance_t * );
-
-static void SDLCallback ( void *, Uint8 *, int );
+static void SDLCallback ( void *, byte_t *, int );
 
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
 vlc_module_begin();
-    set_description( _("Simple DirectMedia Layer audio module") );
+    set_description( _("Simple DirectMedia Layer audio output") );
     set_capability( "audio output", 40 );
+    add_shortcut( "sdl" );
     set_callbacks( Open, Close );
 vlc_module_end();
 
@@ -78,28 +77,19 @@ vlc_module_end();
 static int Open ( vlc_object_t *p_this )
 {
     aout_instance_t *p_aout = (aout_instance_t *)p_this;
-    aout_sys_t * p_sys;
-
-    Uint32 i_flags = SDL_INIT_AUDIO;
+    SDL_AudioSpec desired, obtained;
+    int i_nb_channels;
+    vlc_value_t val, text;
 
+    /* Check that no one uses the DSP. */
+    uint32_t i_flags = SDL_INIT_AUDIO;
     if( SDL_WasInit( i_flags ) )
     {
         return VLC_EGENERIC;
     }
 
-    /* Allocate structure */
-    p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
-    if( p_sys == NULL )
-    {
-        msg_Err( p_aout, "out of memory" );
-        return VLC_ENOMEM;
-    }
-
-    p_aout->output.pf_setformat = SetFormat;
-    p_aout->output.pf_play = Play;
-
 #ifndef WIN32
-    /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet*/
+    /* Win32 SDL implementation doesn't support SDL_INIT_EVENTTHREAD yet */
     i_flags |= SDL_INIT_EVENTTHREAD;
 #endif
 #ifdef DEBUG
@@ -112,44 +102,122 @@ static int Open ( vlc_object_t *p_this )
     if( SDL_Init( i_flags ) < 0 )
     {
         msg_Err( p_aout, "cannot initialize SDL (%s)", SDL_GetError() );
-        free( p_sys );
         return VLC_EGENERIC;
     }
 
-    return VLC_SUCCESS;
-}
-
-/*****************************************************************************
- * SetFormat: reset the audio device and sets its format
- *****************************************************************************/
-static int SetFormat( aout_instance_t *p_aout )
-{
-    aout_sys_t * p_sys = p_aout->output.p_sys;
-
-    /* TODO: finish and clean this */
-    SDL_AudioSpec desired;
+    if ( var_Type( p_aout, "audio-device" ) != 0 )
+    {
+        /* The user has selected an audio device. */
+        vlc_value_t val;
+        var_Get( p_aout, "audio-device", &val );
+        if ( val.i_int == AOUT_VAR_STEREO )
+        {
+            p_aout->output.output.i_physical_channels
+                = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
+        }
+        else if ( val.i_int == AOUT_VAR_MONO )
+        {
+            p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
+        }
+    }
 
+    i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
+    if ( i_nb_channels > 2 )
+    {
+        /* SDL doesn't support more than two channels. */
+        i_nb_channels = 2;
+        p_aout->output.output.i_physical_channels
+            = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
+    }
     desired.freq       = p_aout->output.output.i_rate;
     desired.format     = AUDIO_S16SYS;
-    desired.channels   = p_aout->output.output.i_channels;
+    desired.channels   = i_nb_channels;
     desired.callback   = SDLCallback;
     desired.userdata   = p_aout;
     desired.samples    = FRAME_SIZE;
 
-    /* Open the sound device - FIXME : get the "natural" parameters */
-    if( SDL_OpenAudio( &desired, NULL ) < 0 )
+    /* Open the sound device. */
+    if( SDL_OpenAudio( &desired, &obtained ) < 0 )
     {
         return VLC_EGENERIC;
     }
 
-    p_aout->output.output.i_format = AOUT_FMT_S16_NE;
-    p_aout->output.i_nb_samples = FRAME_SIZE;
+    SDL_PauseAudio( 0 );
 
-    p_sys->call_time = 0;
-    p_sys->buffer_time = (mtime_t)FRAME_SIZE * 1000000
-                          / p_aout->output.output.i_rate;
+    /* Now have a look at what we got. */
+    switch ( obtained.format )
+    {
+    case AUDIO_S16LSB:
+        p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l'); break;
+    case AUDIO_S16MSB:
+        p_aout->output.output.i_format = VLC_FOURCC('s','1','6','b'); break;
+    case AUDIO_U16LSB:
+        p_aout->output.output.i_format = VLC_FOURCC('u','1','6','l'); break;
+    case AUDIO_U16MSB:
+        p_aout->output.output.i_format = VLC_FOURCC('u','1','6','b'); break;
+    case AUDIO_S8:
+        p_aout->output.output.i_format = VLC_FOURCC('s','8',' ',' '); break;
+    case AUDIO_U8:
+        p_aout->output.output.i_format = VLC_FOURCC('u','8',' ',' '); break;
+    }
+    /* Volume is entirely done in software. */
+    aout_VolumeSoftInit( p_aout );
 
-    SDL_PauseAudio( 0 );
+    if ( obtained.channels != i_nb_channels )
+    {
+        p_aout->output.output.i_physical_channels = (obtained.channels == 2 ?
+                                            AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT :
+                                            AOUT_CHAN_CENTER);
+
+        if ( var_Type( p_aout, "audio-device" ) == 0 )
+        {
+            var_Create( p_aout, "audio-device",
+                        VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
+            text.psz_string = _("Audio device");
+            var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
+
+            val.i_int = (obtained.channels == 2) ? AOUT_VAR_STEREO :
+                       AOUT_VAR_MONO;
+            text.psz_string = (obtained.channels == 2) ? N_("Stereo") :
+                              N_("Mono");
+            var_Change( p_aout, "audio-device",
+                        VLC_VAR_ADDCHOICE, &val, &text );
+            var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart,
+                             NULL );
+        }
+    }
+    else if ( var_Type( p_aout, "audio-device" ) == 0 )
+    {
+        /* First launch. */
+        var_Create( p_aout, "audio-device",
+                    VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
+        text.psz_string = _("Audio device");
+        var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
+
+        val.i_int = AOUT_VAR_STEREO;
+        text.psz_string = N_("Stereo");
+        var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
+        val.i_int = AOUT_VAR_MONO;
+        text.psz_string = N_("Mono");
+        var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
+        if ( i_nb_channels == 2 )
+        {
+            val.i_int = AOUT_VAR_STEREO;
+        }
+        else
+        {
+            val.i_int = AOUT_VAR_MONO;
+        }
+        var_Change( p_aout, "audio-device", VLC_VAR_SETDEFAULT, &val, NULL );
+        var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
+    }
+
+    val.b_bool = VLC_TRUE;
+    var_Set( p_aout, "intf-change", val );
+
+    p_aout->output.output.i_rate = obtained.freq;
+    p_aout->output.i_nb_samples = obtained.samples;
+    p_aout->output.pf_play = Play;
 
     return VLC_SUCCESS;
 }
@@ -166,14 +234,9 @@ static void Play( aout_instance_t * p_aout )
  *****************************************************************************/
 static void Close ( vlc_object_t *p_this )
 {
-    aout_instance_t *p_aout = (aout_instance_t *)p_this;
-    aout_sys_t * p_sys = p_aout->output.p_sys;
-
     SDL_PauseAudio( 1 );
     SDL_CloseAudio();
     SDL_QuitSubSystem( SDL_INIT_AUDIO );
-
-    free( p_sys );
 }
 
 /*****************************************************************************
@@ -182,35 +245,15 @@ static void Close ( vlc_object_t *p_this )
 static void SDLCallback( void * _p_aout, byte_t * p_stream, int i_len )
 {
     aout_instance_t * p_aout = (aout_instance_t *)_p_aout;
-    aout_sys_t * p_sys = p_aout->output.p_sys;
+    aout_buffer_t *   p_buffer;
 
-    aout_buffer_t * p_buffer;
+    /* 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. */
 
-    /* We try to stay around call_time + buffer_time/2. This is kludgy but
-     * unavoidable because SDL is completely unable to 1. tell us about its
-     * latency, and 2. call SDLCallback at regular intervals. */
-    if( mdate() < p_sys->call_time + p_sys->buffer_time / 2 )
-    {
-        /* We can't wait too much, because SDL will be lost, and we can't
-         * wait too little, because we are not sure that there will be
-         * samples in the queue. */
-        mwait( p_sys->call_time + p_sys->buffer_time / 4 );
-        p_sys->call_time += p_sys->buffer_time;
-    }
-    else
-    {
-        p_sys->call_time = mdate() + p_sys->buffer_time / 4;
-    }
-
-    /* Tell the output we're playing samples at call_time + 2*buffer_time */
-    p_buffer = aout_OutputNextBuffer( p_aout, p_sys->call_time
-                                       + 2 * p_sys->buffer_time, VLC_TRUE );
-
-    if ( i_len != FRAME_SIZE * sizeof(s16)
-                    * p_aout->output.output.i_channels )
-    {
-        msg_Err( p_aout, "SDL doesn't know its buffer size (%d)", i_len );
-    }
+    vlc_mutex_lock( &p_aout->output_fifo_lock );
+    p_buffer = aout_FifoPop( p_aout, &p_aout->output.fifo );
+    vlc_mutex_unlock( &p_aout->output_fifo_lock );
 
     if ( p_buffer != NULL )
     {