]> git.sesse.net Git - vlc/blobdiff - plugins/sdl/aout_sdl.c
* ALL: new module API. Makes a few things a lot simpler, and we gain
[vlc] / plugins / sdl / aout_sdl.c
index 865766d4cd4dbaea6fbbd8882a58cb5d8afbf938..1fc103d75db81e524ec6232bcd8ec0cb0988bb98 100644 (file)
@@ -2,7 +2,7 @@
  * aout_sdl.c : audio sdl functions library
  *****************************************************************************
  * Copyright (C) 1999-2001 VideoLAN
- * $Id: aout_sdl.c,v 1.22 2001/12/30 07:09:56 sam Exp $
+ * $Id: aout_sdl.c,v 1.31 2002/07/31 20:56:52 sam Exp $
  *
  * Authors: Michel Kaempf <maxx@via.ecp.fr>
  *          Samuel Hocevar <sam@zoy.org>
 #include <fcntl.h>                                       /* open(), O_WRONLY */
 #include <string.h>                                            /* strerror() */
 #include <unistd.h>                                      /* write(), close() */
-#include <stdio.h>                                           /* "intf_msg.h" */
 #include <stdlib.h>                            /* calloc(), malloc(), free() */
 
-#include <videolan/vlc.h>
+#include <vlc/vlc.h>
+#include <vlc/aout.h>
 
 #include SDL_INCLUDE_FILE
 
-#include "audio_output.h"                                   /* aout_thread_t */
-
 /*****************************************************************************
  * aout_sys_t: dsp audio output method descriptor
  *****************************************************************************
 /* the overflow limit is used to prevent the fifo from growing too big */
 #define OVERFLOWLIMIT 100000
 
-typedef struct aout_sys_s
+struct aout_sys_t
 {
     byte_t  * audio_buf;
     int i_audio_end;
 
-    boolean_t b_active;
-
-} aout_sys_t;
+    vlc_bool_t b_active;
+};
 
 /*****************************************************************************
- * Local prototypes.
+ * Local prototypes
  *****************************************************************************/
-static int     aout_Probe       ( probedata_t *p_data );
-static int     aout_Open        ( aout_thread_t *p_aout );
-static int     aout_SetFormat   ( aout_thread_t *p_aout );
-static long    aout_GetBufInfo  ( aout_thread_t *p_aout, long l_buffer_info );
-static void    aout_Play        ( aout_thread_t *p_aout,
-                                  byte_t *buffer, int i_size );
-static void    aout_Close       ( aout_thread_t *p_aout );
+static int     SetFormat   ( aout_thread_t * );
+static int     GetBufInfo  ( aout_thread_t *, int );
+static void    Play        ( aout_thread_t *, byte_t *, int );
 
-static void    aout_SDLCallback ( void *userdata, Uint8 *stream, int len );
+static void    SDLCallback ( void *, Uint8 *, int );
 
 /*****************************************************************************
- * Functions exported as capabilities. They are declared as static so that
- * we don't pollute the namespace too much.
- *****************************************************************************/
-void _M( aout_getfunctions )( function_list_t * p_function_list )
-{
-    p_function_list->pf_probe = aout_Probe;
-    p_function_list->functions.aout.pf_open = aout_Open;
-    p_function_list->functions.aout.pf_setformat = aout_SetFormat;
-    p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
-    p_function_list->functions.aout.pf_play = aout_Play;
-    p_function_list->functions.aout.pf_close = aout_Close;
-}
-
-/*****************************************************************************
- * aout_Probe: probe the audio device and return a score
+ * OpenAudio: open the audio device
  *****************************************************************************
- * This function tries to initialize SDL audio and returns a score to the
- * plugin manager so that it can select the best plugin.
+ * This function opens the dsp as a usual non-blocking write-only file, and
+ * modifies the p_aout->i_fd with the file's descriptor.
  *****************************************************************************/
-static int aout_Probe( probedata_t *p_data )
+int E_(OpenAudio) ( vlc_object_t *p_this )
 {
-#if 0
-    SDL_AudioSpec desired, obtained;
-#endif
-
-    if( SDL_WasInit( SDL_INIT_AUDIO ) != 0 )
-    {
-        return( 0 );
-    }
+    aout_thread_t * p_aout = (aout_thread_t *)p_this;
 
-#if 0
-    /* Start AudioSDL */
-    if( SDL_Init(SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE) != 0 )
-    {
-        intf_DbgMsg( "aout: SDL_Init failed (%s)", SDL_GetError() );
-        return( 0 );
-    }
-
-    desired.freq       = 11025;                                 /* frequency */
-    desired.format     = AUDIO_U8;                        /* unsigned 8 bits */
-    desired.channels   = 2;                                          /* mono */
-    desired.callback   = aout_SDLCallback;    /* callback function mandatory */
-    desired.userdata   = NULL;                     /* null parm for callback */
-    desired.samples    = 4096;
+    SDL_AudioSpec desired;
 
-    /* If we were unable to open the device, there is no way we can use
-     * the plugin. Return a score of 0. */
-    if( SDL_OpenAudio( &desired, &obtained ) < 0 )
+    if( SDL_WasInit( SDL_INIT_AUDIO ) != 0 )
     {
-        intf_DbgMsg( "aout: SDL_OpenAudio failed (%s)", SDL_GetError() );
-        return( 0 );
+        return( 1 );
     }
 
-    /* Otherwise, there are good chances we can use this plugin, return 100. */
-    intf_DbgMsg( "aout: SDL_OpenAudio successfully run" );
-    SDL_CloseAudio();
-#endif
-
-    return( 40 );
-}
-
-/*****************************************************************************
- * aout_Open: open the audio device
- *****************************************************************************
- * This function opens the dsp as a usual non-blocking write-only file, and
- * modifies the p_aout->i_fd with the file's descriptor.
- *****************************************************************************/
-static int aout_Open( aout_thread_t *p_aout )
-{
-    SDL_AudioSpec desired;
-    int i_channels = p_aout->b_stereo ? 2 : 1;
+    p_aout->pf_setformat = SetFormat;
+    p_aout->pf_getbufinfo = GetBufInfo;
+    p_aout->pf_play = Play;
 
     /* Allocate structure */
     p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
 
     if( p_aout->p_sys == NULL )
     {
-        intf_ErrMsg( "aout error: %s", strerror(ENOMEM) );
+        msg_Err( p_aout, "out of memory" );
         return( 1 );
     }
 
@@ -166,7 +107,7 @@ static int aout_Open( aout_thread_t *p_aout )
 #endif
                 ) < 0 )
     {
-        intf_ErrMsg( "aout error: can't initialize SDL (%s)", SDL_GetError() );
+        msg_Err( p_aout, "cannot initialize SDL (%s)", SDL_GetError() );
         free( p_aout->p_sys );
         return( 1 );
     }
@@ -175,20 +116,17 @@ static int aout_Open( aout_thread_t *p_aout )
     p_aout->p_sys->audio_buf = malloc( OVERFLOWLIMIT );
 
     /* Initialize some variables */
-    p_aout->psz_device = 0;
-    p_aout->i_format   = AOUT_FORMAT_DEFAULT;
-    p_aout->i_channels = 1 + main_GetIntVariable( AOUT_STEREO_VAR,
-                                                  AOUT_STEREO_DEFAULT );
-    p_aout->l_rate     = main_GetIntVariable( AOUT_RATE_VAR,
-                                              AOUT_RATE_DEFAULT );
-
-    desired.freq =     p_aout->l_rate;
 
     /* TODO: write conversion beetween AOUT_FORMAT_DEFAULT
      * AND AUDIO* from SDL. */
+    desired.freq       = p_aout->i_rate;
+#ifdef WORDS_BIGENDIAN
+    desired.format     = AUDIO_S16MSB;                     /* stereo 16 bits */
+#else
     desired.format     = AUDIO_S16LSB;                     /* stereo 16 bits */
-    desired.channels   = i_channels;
-    desired.callback   = aout_SDLCallback;
+#endif
+    desired.channels   = p_aout->i_channels;
+    desired.callback   = SDLCallback;
     desired.userdata   = p_aout->p_sys;
     desired.samples    = 1024;
 
@@ -199,7 +137,7 @@ static int aout_Open( aout_thread_t *p_aout )
      */
     if( SDL_OpenAudio( &desired, NULL ) < 0 )
     {
-        intf_ErrMsg( "aout error: SDL_OpenAudio failed (%s)", SDL_GetError() );
+        msg_Err( p_aout, "SDL_OpenAudio failed (%s)", SDL_GetError() );
         SDL_QuitSubSystem( SDL_INIT_AUDIO );
         free( p_aout->p_sys );
         return( -1 );
@@ -212,24 +150,27 @@ static int aout_Open( aout_thread_t *p_aout )
 }
 
 /*****************************************************************************
- * aout_SetFormat: reset the audio device and sets its format
+ * SetFormat: reset the audio device and sets its format
  *****************************************************************************
  * This functions resets the audio device, tries to initialize the output
  * format with the value contained in the dsp structure, and if this value
  * could not be set, the default value returned by ioctl is set. It then
  * does the same for the stereo mode, and for the output rate.
  *****************************************************************************/
-static int aout_SetFormat( aout_thread_t *p_aout )
+static int SetFormat( aout_thread_t *p_aout )
 {
     /* TODO: finish and clean this */
     SDL_AudioSpec desired;
-    int i_stereo = p_aout->b_stereo ? 2 : 1;
 
     /*i_format = p_aout->i_format;*/
-    desired.freq       = p_aout->l_rate;             /* Set the output rate */
+    desired.freq       = p_aout->i_rate;             /* Set the output rate */
+#ifdef WORDS_BIGENDIAN
+    desired.format     = AUDIO_S16MSB;                    /* stereo 16 bits */
+#else
     desired.format     = AUDIO_S16LSB;                    /* stereo 16 bits */
-    desired.channels   = i_stereo;
-    desired.callback   = aout_SDLCallback;
+#endif
+    desired.channels   = p_aout->i_channels;
+    desired.callback   = SDLCallback;
     desired.userdata   = p_aout->p_sys;
     desired.samples    = 2048;
 
@@ -246,33 +187,31 @@ static int aout_SetFormat( aout_thread_t *p_aout )
     p_aout->p_sys->b_active = 1;
     SDL_PauseAudio( 0 );
 
-    p_aout->i_latency = 0;
-    
     return( 0 );
 }
 
 /*****************************************************************************
- * aout_GetBufInfo: buffer status query
+ * GetBufInfo: buffer status query
  *****************************************************************************
  * returns the number of bytes in the audio buffer compared to the size of
- * l_buffer_limit...
+ * i_buffer_limit...
  *****************************************************************************/
-static long aout_GetBufInfo( aout_thread_t *p_aout, long l_buffer_limit )
+static int GetBufInfo( aout_thread_t *p_aout, int i_buffer_limit )
 {
-    if(l_buffer_limit > p_aout->p_sys->i_audio_end)
+    if(i_buffer_limit > p_aout->p_sys->i_audio_end)
     {
         /* returning 0 here juste gives awful sound in the speakers :/ */
-        return( l_buffer_limit );
+        return( i_buffer_limit );
     }
-    return( p_aout->p_sys->i_audio_end - l_buffer_limit);
+    return( p_aout->p_sys->i_audio_end - i_buffer_limit);
 }
 
 /*****************************************************************************
- * aout_Play: play a sound samples buffer
+ * Play: play a sound samples buffer
  *****************************************************************************
  * This function writes a buffer of i_length bytes in the dsp
  *****************************************************************************/
-static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
+static void Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
 {
     byte_t * audio_buf = p_aout->p_sys->audio_buf;
 
@@ -289,10 +228,12 @@ static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
 }
 
 /*****************************************************************************
- * aout_Close: close the audio device
+ * CloseAudio: close the audio device
  *****************************************************************************/
-static void aout_Close( aout_thread_t *p_aout )
+void E_(CloseAudio) ( vlc_object_t *p_this )
 {
+    aout_thread_t * p_aout = (aout_thread_t *)p_this;
+
     if( p_aout->p_sys->b_active )
     {
         SDL_PauseAudio( 1 );                                  /* pause audio */
@@ -311,15 +252,15 @@ static void aout_Close( aout_thread_t *p_aout )
 }
 
 /*****************************************************************************
- * aout_SDLCallback: what to do once SDL has played sound samples
+ * SDLCallback: what to do once SDL has played sound samples
  *****************************************************************************/
-static void aout_SDLCallback( void *userdata, byte_t *stream, int len )
+static void SDLCallback( void *userdata, byte_t *stream, int len )
 {
-    struct aout_sys_s * p_sys = userdata;
+    aout_sys_t * p_sys = userdata;
 
     if( p_sys->i_audio_end > OVERFLOWLIMIT )
     {
-        intf_ErrMsg( "aout error: aout_SDLCallback overflowed" );
+//X        msg_Err( p_aout, "aout_SDLCallback overflowed" );
 
         free( p_sys->audio_buf );
         p_sys->audio_buf = NULL;