]> git.sesse.net Git - vlc/blobdiff - src/audio_output/audio_output.c
* Changed aout_FormatTo* ; aout_FormatToSize does no longer exist.
[vlc] / src / audio_output / audio_output.c
index ba20bc4e59f1cff175967a859e5b81dac822177b..15605d8f4db74685b4c3365ccbba19332ecfb907 100644 (file)
@@ -1,11 +1,10 @@
 /*****************************************************************************
- * audio_output.c : audio output thread
+ * audio_output.c : audio output instance
  *****************************************************************************
- * Copyright (C) 1999-2001 VideoLAN
- * $Id: audio_output.c,v 1.76 2002/02/15 13:32:54 sam Exp $
+ * Copyright (C) 2002 VideoLAN
+ * $Id: audio_output.c,v 1.95 2002/08/14 00:23:59 massiot Exp $
  *
- * Authors: Michel Kaempf <maxx@via.ecp.fr>
- *          Cyril Deguet <asmax@via.ecp.fr>
+ * Authors: 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
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <stdio.h>                                           /* "intf_msg.h" */
 #include <stdlib.h>                            /* calloc(), malloc(), free() */
 #include <string.h>
 
-#include <videolan/vlc.h>
+#include <vlc/vlc.h>
 
-#ifdef HAVE_UNISTD_H
-#   include <unistd.h>                                           /* getpid() */
-#endif
-
-#ifdef WIN32                   /* getpid() for win32 is located in process.h */
-#   include <process.h>
+#ifdef HAVE_ALLOCA_H
+#   include <alloca.h>
 #endif
 
 #include "audio_output.h"
-#include "aout_common.h"
-
-/*****************************************************************************
- * Local prototypes
- *****************************************************************************/
-static int aout_SpawnThread( aout_thread_t * p_aout );
+#include "aout_internal.h"
 
 /*****************************************************************************
- * aout_InitBank: initialize the audio output bank.
+ * aout_NewInstance: initialize aout structure
  *****************************************************************************/
-void aout_InitBank ( void )
+aout_instance_t * __aout_NewInstance( vlc_object_t * p_parent )
 {
-    p_aout_bank->i_count = 0;
+    aout_instance_t * p_aout;
+
+    /* Allocate descriptor. */
+    p_aout = vlc_object_create( p_parent, VLC_OBJECT_AOUT );
+    if( p_aout == NULL )
+    {
+        return NULL;
+    }
 
-    vlc_mutex_init( &p_aout_bank->lock );
+    /* Initialize members. */
+    vlc_mutex_init( p_parent, &p_aout->input_lock );
+    vlc_cond_init( p_parent, &p_aout->input_signal );
+    p_aout->i_inputs_active = 0;
+    p_aout->b_change_requested = 0;
+    p_aout->i_nb_inputs = 0;
+
+    vlc_mutex_init( p_parent, &p_aout->mixer_lock );
+    vlc_cond_init( p_parent, &p_aout->mixer_signal );
+    p_aout->b_mixer_active = 0;
+
+    vlc_object_attach( p_aout, p_parent->p_vlc );
+
+    return p_aout;
 }
 
 /*****************************************************************************
- * aout_EndBank: empty the audio output bank.
- *****************************************************************************
- * This function ends all unused audio outputs and empties the bank in
- * case of success.
+ * aout_DeleteInstance: destroy aout structure
  *****************************************************************************/
-void aout_EndBank ( void )
+void aout_DeleteInstance( aout_instance_t * p_aout )
 {
-    /* Ask all remaining audio outputs to die */
-    while( p_aout_bank->i_count )
-    {
-        aout_DestroyThread(
-                p_aout_bank->pp_aout[ --p_aout_bank->i_count ], NULL );
-    }
+    vlc_mutex_destroy( &p_aout->input_lock );
+    vlc_cond_destroy( &p_aout->input_signal );
+    vlc_mutex_destroy( &p_aout->mixer_lock );
+    vlc_cond_destroy( &p_aout->mixer_signal );
 
-    vlc_mutex_destroy( &p_aout_bank->lock );
+    /* Free structure. */
+    vlc_object_destroy( p_aout );
 }
 
 /*****************************************************************************
- * aout_CreateThread: initialize audio thread
+ * aout_BufferNew : ask for a new empty buffer
  *****************************************************************************/
-aout_thread_t *aout_CreateThread( int *pi_status, int i_channels, long l_rate )
+aout_buffer_t * aout_BufferNew( aout_instance_t * p_aout,
+                                aout_input_t * p_input,
+                                size_t i_nb_samples )
 {
-    aout_thread_t * p_aout;                             /* thread descriptor */
-#if 0
-    int             i_status;                               /* thread status */
-#endif
-
-    /* Allocate descriptor */
-    p_aout = (aout_thread_t *) malloc( sizeof(aout_thread_t) );
-    if( p_aout == NULL )
+    aout_buffer_t * p_buffer;
+    mtime_t duration = (1000000 * (mtime_t)i_nb_samples)
+                        / p_input->input.i_rate;
+
+    /* This necessarily allocates in the heap. */
+    aout_BufferAlloc( &p_input->input_alloc, duration, NULL, p_buffer );
+    p_buffer->i_nb_samples = i_nb_samples;
+    p_buffer->i_nb_bytes = duration
+                              * aout_FormatToByterate( &p_input->input )
+                              / 1000000;
+
+    if ( p_buffer == NULL )
     {
-        return( NULL );
+        msg_Err( p_aout, "NULL buffer !" );
     }
-
-    p_aout->i_latency = 0;
-    p_aout->l_rate = main_GetIntVariable( AOUT_RATE_VAR, l_rate );
-    p_aout->i_channels = 1 + main_GetIntVariable( AOUT_STEREO_VAR,
-                                                  i_channels - 1 );
-
-    intf_WarnMsg( 3, "aout info: thread created, channels %d, rate %d",
-                  p_aout->i_channels, p_aout->l_rate );
-    
-    /* Maybe we should pass this setting in argument */
-    p_aout->i_format = AOUT_FORMAT_DEFAULT;
-
-    /* special setting for ac3 pass-through mode */
-    /* FIXME is it necessary ? (cf ac3_adec.c) */
-    if( main_GetIntVariable( AOUT_SPDIF_VAR, 0 ) && p_main->b_ac3 )
-    {
-        intf_WarnMsg( 4, "aout info: setting ac3 spdif" );
-        p_aout->i_format = AOUT_FMT_AC3;
-    }
-    
-    if( p_aout->l_rate == 0 )
-    {
-        intf_ErrMsg( "aout error: null sample rate" );
-        free( p_aout );
-        return( NULL );
-    }
-
-    /* FIXME: only works for i_channels == 1 or 2 ?? */
-    p_aout->b_stereo = ( p_aout->i_channels == 2 ) ? 1 : 0;
-
-    /* Choose the best module */
-    p_aout->p_module = module_Need( MODULE_CAPABILITY_AOUT,
-                           main_GetPszVariable( AOUT_METHOD_VAR, NULL ), 
-                           (void *)p_aout );
-
-    if( p_aout->p_module == NULL )
-    {
-        intf_ErrMsg( "aout error: no suitable aout module" );
-        free( p_aout );
-        return( NULL );
-    }
-
-#define aout_functions p_aout->p_module->p_functions->aout.functions.aout
-    p_aout->pf_open       = aout_functions.pf_open;
-    p_aout->pf_setformat  = aout_functions.pf_setformat;
-    p_aout->pf_getbufinfo = aout_functions.pf_getbufinfo;
-    p_aout->pf_play       = aout_functions.pf_play;
-    p_aout->pf_close      = aout_functions.pf_close;
-#undef aout_functions
-
-    /*
-     * Initialize audio device
-     */
-    if ( p_aout->pf_setformat( p_aout ) )
+    else
     {
-        p_aout->pf_close( p_aout );
-        module_Unneed( p_aout->p_module );
-        free( p_aout );
-        return( NULL );
+        p_buffer->start_date = p_buffer->end_date = 0;
     }
 
-    /* Initialize the volume level */
-    p_aout->i_volume = main_GetIntVariable( AOUT_VOLUME_VAR, VOLUME_DEFAULT );
-    p_aout->i_savedvolume = 0;
-    
-    /* FIXME: maybe it would be cleaner to change SpawnThread prototype
-     * see vout to handle status correctly ?? however, it is not critical since
-     * this thread is only called in main and all calls are blocking */
-    if( aout_SpawnThread( p_aout ) )
-    {
-        p_aout->pf_close( p_aout );
-        module_Unneed( p_aout->p_module );
-        free( p_aout );
-        return( NULL );
-    }
-
-    return( p_aout );
+    return p_buffer;
 }
 
-
 /*****************************************************************************
- * Declare the different aout thread functions
+ * aout_BufferDelete : destroy an undecoded buffer
  *****************************************************************************/
-DECLARE_AOUT_THREAD( S16, s16, ( p_aout->s32_buffer[l_buffer] / 
-        AOUT_MAX_FIFOS ) )
-
-DECLARE_AOUT_THREAD( U16, u16, (( p_aout->s32_buffer[l_buffer] / 
-        AOUT_MAX_FIFOS) + 128) )
-        
-DECLARE_AOUT_THREAD( U8, u8, (( p_aout->s32_buffer[l_buffer] / 
-        AOUT_MAX_FIFOS / 256) + 128 ) )
-
-DECLARE_AOUT_THREAD( S8, s8, ( p_aout->s32_buffer[l_buffer] / 
-        AOUT_MAX_FIFOS / 256) )
-        
+void aout_BufferDelete( aout_instance_t * p_aout, aout_input_t * p_input,
+                        aout_buffer_t * p_buffer )
+{
+    aout_BufferFree( p_buffer );
+}
 
 /*****************************************************************************
- * aout_SpawnThread
+ * aout_BufferPlay : filter & mix the decoded buffer
  *****************************************************************************/
-static int aout_SpawnThread( aout_thread_t * p_aout )
+void aout_BufferPlay( aout_instance_t * p_aout, aout_input_t * p_input,
+                      aout_buffer_t * p_buffer )
 {
-    int     i_fifo;
-    long    l_bytes;
-    void (* pf_aout_thread)( aout_thread_t * ) = NULL;
-
-    /* We want the audio output thread to live */
-    p_aout->b_die = 0;
-    p_aout->b_active = 1;
+    vlc_bool_t b_run_mixer = 0;
 
-    /* Initialize the fifos lock */
-    vlc_mutex_init( &p_aout->fifos_lock );
-    /* Initialize audio fifos : set all fifos as empty and initialize locks */
-    for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
+    if ( p_buffer->start_date == 0 )
     {
-        p_aout->fifo[i_fifo].i_type = AOUT_EMPTY_FIFO;
-        vlc_mutex_init( &p_aout->fifo[i_fifo].data_lock );
-        vlc_cond_init( &p_aout->fifo[i_fifo].data_wait );
+        msg_Warn( p_aout, "non-dated buffer received" );
+        aout_BufferFree( p_buffer );
     }
-
-    /* Compute the size (in audio units) of the audio output buffer. Although
-     * AOUT_BUFFER_DURATION is given in microseconds, the output rate is given
-     * in Hz, that's why we need to divide by 10^6 microseconds (1 second) */
-    p_aout->l_units = (long)( ((s64)p_aout->l_rate * AOUT_BUFFER_DURATION) / 1000000 );
-    p_aout->l_msleep = (long)( ((s64)p_aout->l_units * 1000000) / (s64)p_aout->l_rate );
-
-    /* Make pf_aout_thread point to the right thread function, and compute the
-     * byte size of the audio output buffer */
-    switch ( p_aout->i_channels )
+    else
     {
-    /* Audio output is mono */
-    case 1:
-        switch ( p_aout->i_format )
-        {
-        case AOUT_FMT_U8:
-            intf_WarnMsg( 2, "aout info: unsigned 8 bits mono thread" );
-            l_bytes = 1 * sizeof(u8) * p_aout->l_units;
-            pf_aout_thread = aout_U8Thread;
-            break;
-
-        case AOUT_FMT_S8:
-            intf_WarnMsg( 2, "aout info: signed 8 bits mono thread" );
-            l_bytes = 1 * sizeof(s8) * p_aout->l_units;
-            pf_aout_thread = aout_S8Thread;
-            break;
-
-        case AOUT_FMT_U16_LE:
-        case AOUT_FMT_U16_BE:
-            intf_WarnMsg( 2, "aout info: unsigned 16 bits mono thread" );
-            l_bytes = 1 * sizeof(u16) * p_aout->l_units;
-            pf_aout_thread = aout_U16Thread;
-            break;
-
-        case AOUT_FMT_S16_LE:
-        case AOUT_FMT_S16_BE:
-            intf_WarnMsg( 2, "aout info: signed 16 bits mono thread" );
-            l_bytes = 1 * sizeof(s16) * p_aout->l_units;
-            pf_aout_thread = aout_S16Thread;
-            break;
-
-        default:
-            intf_ErrMsg( "aout error: unknown audio output format (%i)",
-                         p_aout->i_format );
-            return( -1 );
-        }
-        break;
-
-    /* Audio output is stereo */
-    case 2:
-        switch ( p_aout->i_format )
-        {
-        case AOUT_FMT_U8:
-            intf_WarnMsg( 2, "aout info: unsigned 8 bits stereo thread" );
-            l_bytes = 2 * sizeof(u8) * p_aout->l_units;
-            pf_aout_thread = aout_U8Thread;
-            break;
-
-        case AOUT_FMT_S8:
-            intf_WarnMsg( 2, "aout info: signed 8 bits stereo thread" );
-            l_bytes = 2 * sizeof(s8) * p_aout->l_units;
-            pf_aout_thread = aout_S8Thread;
-            break;
-
-        case AOUT_FMT_U16_LE:
-        case AOUT_FMT_U16_BE:
-            intf_WarnMsg( 2, "aout info: unsigned 16 bits stereo thread" );
-            l_bytes = 2 * sizeof(u16) * p_aout->l_units;
-            pf_aout_thread = aout_U16Thread;
-            break;
-
-        case AOUT_FMT_S16_LE:
-        case AOUT_FMT_S16_BE:
-            intf_WarnMsg( 2, "aout info: signed 16 bits stereo thread" );
-            l_bytes = 2 * sizeof(s16) * p_aout->l_units;
-            pf_aout_thread = aout_S16Thread;
-            break;
-
-        case AOUT_FMT_AC3:
-            intf_WarnMsg( 2, "aout info: ac3 pass-through thread" );
-            l_bytes = SPDIF_FRAME_SIZE;
-            pf_aout_thread = aout_SpdifThread;
-            break;
-
-        default:
-            intf_ErrMsg( "aout error: unknown audio output format %i",
-                         p_aout->i_format );
-            return( -1 );
-        }
-        break;
-
-    default:
-        intf_ErrMsg( "aout error: unknown number of audio channels (%i)",
-                     p_aout->i_channels );
-        return( -1 );
+        p_buffer->end_date = p_buffer->start_date
+                                + (mtime_t)(p_buffer->i_nb_samples * 1000000)
+                                    / p_input->input.i_rate;
     }
 
-    /* Allocate the memory needed by the audio output buffers, and set to zero
-     * the s32 buffer's memory */
-    p_aout->buffer = malloc( l_bytes );
-    if ( p_aout->buffer == NULL )
-    {
-        intf_ErrMsg( "aout error: cannot create output buffer" );
-        return( -1 );
-    }
+    /* If the buffer is too early, wait a while. */
+    mwait( p_buffer->start_date - AOUT_MAX_PREPARE_TIME );
 
-    p_aout->s32_buffer = (s32 *)calloc( p_aout->l_units,
-                                        sizeof(s32) << ( p_aout->b_stereo ) );
-    if ( p_aout->s32_buffer == NULL )
+    aout_InputPlay( p_aout, p_input, p_buffer );
+
+    /* Run the mixer if it is able to run. */
+    vlc_mutex_lock( &p_aout->mixer_lock );
+    if ( !p_aout->b_mixer_active )
     {
-        intf_ErrMsg( "aout error: cannot create the s32 output buffer" );
-        free( p_aout->buffer );
-        return( -1 );
+        p_aout->b_mixer_active = 1;
+        b_run_mixer = 1;
     }
+    vlc_mutex_unlock( &p_aout->mixer_lock );
 
-    /* Rough estimate of the playing date */
-    p_aout->date = mdate() + p_main->i_desync;
-
-    /* Launch the thread */
-    if ( vlc_thread_create( &p_aout->thread_id, "audio output",
-                            (vlc_thread_func_t)pf_aout_thread, p_aout ) )
+    if ( b_run_mixer )
     {
-        intf_ErrMsg( "aout error: cannot spawn audio output thread" );
-        free( p_aout->buffer );
-        free( p_aout->s32_buffer );
-        return( -1 );
+        aout_MixerRun( p_aout );
+        vlc_mutex_lock( &p_aout->mixer_lock );
+        p_aout->b_mixer_active = 0;
+        vlc_cond_broadcast( &p_aout->mixer_signal );
+        vlc_mutex_unlock( &p_aout->mixer_lock );
     }
-
-    intf_WarnMsg( 2, "aout info: audio output thread %i spawned", getpid() );
-    return( 0 );
 }
 
 /*****************************************************************************
- * aout_DestroyThread
+ * aout_FormatToByterate : compute the number of bytes per second
  *****************************************************************************/
-void aout_DestroyThread( aout_thread_t * p_aout, int *pi_status )
+int aout_FormatToByterate( audio_sample_format_t * p_format )
 {
-    int i_fifo;
-    
-    /* FIXME: pi_status is not handled correctly: check vout how to do!?? */
+    int i_result;
 
-    /* Ask thread to kill itself and wait until it's done */
-    p_aout->b_die = 1;
-    vlc_thread_join( p_aout->thread_id ); /* only if pi_status is NULL */
+    switch ( p_format->i_format )
+    {
+    case AOUT_FMT_U8:
+    case AOUT_FMT_S8:
+        i_result = 1;
+        break;
 
-    /* Free the allocated memory */
-    free( p_aout->buffer );
-    free( p_aout->s32_buffer );
+    case AOUT_FMT_U16_LE:
+    case AOUT_FMT_U16_BE:
+    case AOUT_FMT_S16_LE:
+    case AOUT_FMT_S16_BE:
+        i_result = 2;
+        break;
 
-    /* Destroy the condition and mutex locks */
-    for ( i_fifo = 0; i_fifo < AOUT_MAX_FIFOS; i_fifo++ )
-    {
-        vlc_mutex_destroy( &p_aout->fifo[i_fifo].data_lock );
-        vlc_cond_destroy( &p_aout->fifo[i_fifo].data_wait );
-    }
-    vlc_mutex_destroy( &p_aout->fifos_lock );
-    
-    /* Free the plugin */
-    p_aout->pf_close( p_aout );
+    case AOUT_FMT_FLOAT32:
+    case AOUT_FMT_FIXED32:
+        i_result = 4;
+        break;
+
+    case AOUT_FMT_SPDIF:
+    case AOUT_FMT_A52:
+    case AOUT_FMT_DTS:
+        /* For these formats the caller has to indicate the number of bytes
+         * per second it evaluates. */
+        return p_format->i_bytes_per_sec;
 
-    /* Release the aout module */
-    module_Unneed( p_aout->p_module );
+    default:
+        return 0; /* will segfault much sooner... */
+    }
 
-    /* Free structure */
-    free( p_aout );
+    return i_result * p_format->i_channels * p_format->i_rate;
 }