]> git.sesse.net Git - vlc/blobdiff - src/audio_output/output.c
aout_PacketPlay: use aout_TimeReport(), restore resampling
[vlc] / src / audio_output / output.c
index 8cac4afe91ced2d0a07e0118eb5e15a38cdf02f3..83f0601ac6bc287905fe4614fe5dc2f1bbfa2828 100644 (file)
@@ -28,6 +28,8 @@
 # include "config.h"
 #endif
 
+#include <math.h>
+
 #include <assert.h>
 #include <vlc_common.h>
 #include <vlc_aout.h>
  *****************************************************************************
  * This function is entered with the mixer lock.
  *****************************************************************************/
-int aout_OutputNew( audio_output_t * p_aout,
+int aout_OutputNew( audio_output_t *p_aout,
                     const audio_sample_format_t * p_format )
 {
-    vlc_assert_locked( &p_aout->lock );
+    aout_owner_t *owner = aout_owner (p_aout);
+
+    aout_assert_locked( p_aout );
     p_aout->format = *p_format;
 
     /* Retrieve user defaults. */
@@ -56,8 +60,8 @@ int aout_OutputNew( audio_output_t * p_aout,
     aout_FormatPrepare( &p_aout->format );
 
     /* Find the best output plug-in. */
-    p_aout->module = module_need( p_aout, "audio output", "$aout", false );
-    if ( p_aout->module == NULL )
+    owner->module = module_need (p_aout, "audio output", "$aout", false);
+    if (owner->module == NULL)
     {
         msg_Err( p_aout, "no suitable audio output module" );
         return -1;
@@ -157,43 +161,39 @@ int aout_OutputNew( audio_output_t * p_aout,
     var_TriggerCallback( p_aout, "intf-change" );
 
     aout_FormatPrepare( &p_aout->format );
-
-    /* Prepare FIFO. */
-    aout_FifoInit( p_aout, &p_aout->fifo, p_aout->format.i_rate );
     aout_FormatPrint( p_aout, "output", &p_aout->format );
 
     /* Choose the mixer format. */
-    p_aout->mixer_format = p_aout->format;
-    if ( AOUT_FMT_NON_LINEAR(&p_aout->format) )
-        p_aout->mixer_format.i_format = p_format->i_format;
+    owner->mixer_format = p_aout->format;
+    if (AOUT_FMT_NON_LINEAR(&p_aout->format))
+        owner->mixer_format.i_format = p_format->i_format;
     else
     /* Most audio filters can only deal with single-precision,
      * so lets always use that when hardware supports floating point. */
     if( HAVE_FPU )
-        p_aout->mixer_format.i_format = VLC_CODEC_FL32;
+        owner->mixer_format.i_format = VLC_CODEC_FL32;
     else
     /* Otherwise, audio filters will not work. Use fixed-point if the input has
      * more than 16-bits depth. */
     if( p_format->i_bitspersample > 16 )
-        p_aout->mixer_format.i_format = VLC_CODEC_FI32;
+        owner->mixer_format.i_format = VLC_CODEC_FI32;
     else
     /* Fallback to 16-bits. This avoids pointless conversion to and from
      * 32-bits samples for the sole purpose of software mixing. */
-        p_aout->mixer_format.i_format = VLC_CODEC_S16N;
+        owner->mixer_format.i_format = VLC_CODEC_S16N;
 
-    aout_FormatPrepare( &p_aout->mixer_format );
-    aout_FormatPrint( p_aout, "mixer", &p_aout->mixer_format );
+    aout_FormatPrepare (&owner->mixer_format);
+    aout_FormatPrint (p_aout, "mixer", &owner->mixer_format);
 
     /* Create filters. */
-    p_aout->i_nb_filters = 0;
-    if ( aout_FiltersCreatePipeline( p_aout, p_aout->pp_filters,
-                                     &p_aout->i_nb_filters,
-                                     &p_aout->mixer_format,
-                                     &p_aout->format ) < 0 )
+    owner->nb_filters = 0;
+    if (aout_FiltersCreatePipeline (p_aout, owner->filters,
+                                    &owner->nb_filters, &owner->mixer_format,
+                                    &p_aout->format) < 0)
     {
         msg_Err( p_aout, "couldn't create audio output pipeline" );
-        module_unneed( p_aout, p_aout->module );
-        p_aout->module = NULL;
+        module_unneed (p_aout, owner->module);
+        owner->module = NULL;
         return -1;
     }
     return 0;
@@ -206,47 +206,40 @@ int aout_OutputNew( audio_output_t * p_aout,
  *****************************************************************************/
 void aout_OutputDelete( audio_output_t * p_aout )
 {
-    vlc_assert_locked( &p_aout->lock );
+    aout_owner_t *owner = aout_owner (p_aout);
 
-    if( p_aout->module == NULL )
+    aout_assert_locked( p_aout );
+
+    if (owner->module == NULL)
         return;
 
-    module_unneed( p_aout, p_aout->module );
+    module_unneed (p_aout, owner->module);
     aout_VolumeNoneInit( p_aout ); /* clear volume callback */
-    p_aout->module = NULL;
-    aout_FiltersDestroyPipeline( p_aout->pp_filters, p_aout->i_nb_filters );
-    aout_FifoDestroy( &p_aout->fifo );
+    owner->module = NULL;
+    aout_FiltersDestroyPipeline (owner->filters, owner->nb_filters);
 }
 
-static block_t *aout_OutputSlice( audio_output_t *, aout_fifo_t * );
-
 /*****************************************************************************
  * aout_OutputPlay : play a buffer
  *****************************************************************************
  * This function is entered with the mixer lock.
  *****************************************************************************/
-void aout_OutputPlay( audio_output_t * p_aout, aout_buffer_t * p_buffer )
+void aout_OutputPlay (audio_output_t *aout, block_t *block)
 {
-    vlc_assert_locked( &p_aout->lock );
+    aout_owner_t *owner = aout_owner (aout);
 
-    aout_FiltersPlay( p_aout->pp_filters, p_aout->i_nb_filters, &p_buffer );
-    if( !p_buffer )
+    aout_assert_locked (aout);
+
+    aout_FiltersPlay (owner->filters, owner->nb_filters, &block);
+    if (block == NULL)
         return;
-    if( p_buffer->i_buffer == 0 )
+    if (block->i_buffer == 0)
     {
-        block_Release( p_buffer );
+        block_Release (block);
         return;
     }
 
-    aout_fifo_t *fifo = &p_aout->p_input->fifo;
-    /* XXX: cleanup */
-    aout_FifoPush( fifo, p_buffer );
-
-    while( (p_buffer = aout_OutputSlice( p_aout, fifo ) ) != NULL )
-    {
-        aout_FifoPush( &p_aout->fifo, p_buffer );
-        p_aout->pf_play( p_aout );
-    }
+    aout->pf_play (aout, block);
 }
 
 /**
@@ -256,16 +249,9 @@ void aout_OutputPlay( audio_output_t * p_aout, aout_buffer_t * p_buffer )
  */
 void aout_OutputPause( audio_output_t *aout, bool pause, mtime_t date )
 {
-    vlc_assert_locked( &aout->lock );
-
+    aout_assert_locked( aout );
     if( aout->pf_pause != NULL )
         aout->pf_pause( aout, pause, date );
-    if( !pause )
-    {
-        mtime_t duration = date - aout->p_input->i_pause_date;
-        /* XXX: ^ onk onk! gruik! ^ */
-        aout_FifoMoveDates( &aout->fifo, duration );
-    }
 }
 
 /**
@@ -276,11 +262,10 @@ void aout_OutputPause( audio_output_t *aout, bool pause, mtime_t date )
  */
 void aout_OutputFlush( audio_output_t *aout, bool wait )
 {
-    vlc_assert_locked( &aout->lock );
+    aout_assert_locked( aout );
 
     if( aout->pf_flush != NULL )
         aout->pf_flush( aout, wait );
-    aout_FifoReset( &aout->fifo );
 }
 
 
@@ -304,8 +289,10 @@ void aout_VolumeNoneInit (audio_output_t *aout)
 {
     /* aout_New() -safely- calls this function without the lock, before any
      * other thread knows of this audio output instance.
-    vlc_assert_locked (&aout->lock); */
+    aout_assert_locked (aout); */
     aout->pf_volume_set = aout_VolumeNoneSet;
+    var_Destroy (aout, "volume");
+    var_Destroy (aout, "mute");
 }
 
 /**
@@ -313,7 +300,9 @@ void aout_VolumeNoneInit (audio_output_t *aout)
  */
 static int aout_VolumeSoftSet (audio_output_t *aout, float volume, bool mute)
 {
-    vlc_assert_locked (&aout->lock);
+    aout_owner_t *owner = aout_owner (aout);
+
+    aout_assert_locked (aout);
 
     /* Cubic mapping from software volume to amplification factor.
      * This provides a good tradeoff between low and high volume ranges.
@@ -326,7 +315,7 @@ static int aout_VolumeSoftSet (audio_output_t *aout, float volume, bool mute)
     else
         volume = 0.;
 
-    aout->mixer_multiplier = volume;
+    owner->volume.multiplier = volume;
     return 0;
 }
 
@@ -341,7 +330,7 @@ void aout_VolumeSoftInit (audio_output_t *aout)
     audio_volume_t volume = var_InheritInteger (aout, "volume");
     bool mute = var_InheritBool (aout, "mute");
 
-    vlc_assert_locked (&aout->lock);
+    aout_assert_locked (aout);
     aout->pf_volume_set = aout_VolumeSoftSet;
     aout_VolumeSoftSet (aout, volume / (float)AOUT_VOLUME_DEFAULT, mute);
 }
@@ -353,8 +342,10 @@ void aout_VolumeSoftInit (audio_output_t *aout)
  */
 void aout_VolumeHardInit (audio_output_t *aout, aout_volume_cb setter)
 {
-    vlc_assert_locked (&aout->lock);
+    aout_assert_locked (aout);
     aout->pf_volume_set = setter;
+    var_Create (aout, "volume", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
+    var_Create (aout, "mute", VLC_VAR_BOOL|VLC_VAR_DOINHERIT);
 }
 
 /**
@@ -363,38 +354,125 @@ void aout_VolumeHardInit (audio_output_t *aout, aout_volume_cb setter)
  * @param setter volume setter callback
  * @param volume current custom volume
  * @param mute current mute flag
- * @note Audio output plugins that cannot apply the volume
- * should call this function during activation.
+ *
+ * @warning The caller (i.e. the audio output plug-in) is responsible for
+ * interlocking and synchronizing call to this function and to the
+ * audio_output_t.pf_volume_set callback. This ensures that VLC gets correct
+ * volume information (possibly with a latency).
  */
 void aout_VolumeHardSet (audio_output_t *aout, float volume, bool mute)
 {
-#warning FIXME
-    /* REVISIT: This is tricky. We cannot acquire the volume lock as this gets
-     * called from the audio output (it would cause a lock inversion).
-     * We also should not override the input manager volume, but only the
-     * volume of the current audio output... FIXME */
-    msg_Err (aout, "%s(%f, %u)", __func__, volume, (unsigned)mute);
+    audio_volume_t vol = lroundf (volume * (float)AOUT_VOLUME_DEFAULT);
+
+    /* We cannot acquire the volume lock as this gets called from the audio
+     * output plug-in (it would cause a lock inversion). */
+    var_SetInteger (aout, "volume", vol);
+    var_SetBool (aout, "mute", mute);
+    var_TriggerCallback (aout, "intf-change");
+}
+
+
+/*** Packet-oriented audio output support ***/
+
+static inline aout_packet_t *aout_packet (audio_output_t *aout)
+{
+    return (aout_packet_t *)(aout->sys);
+}
+
+void aout_PacketInit (audio_output_t *aout, aout_packet_t *p, unsigned samples)
+{
+    assert (p == aout_packet (aout));
+
+    vlc_mutex_init (&p->lock);
+    aout_FifoInit (aout, &p->partial, aout->format.i_rate);
+    aout_FifoInit (aout, &p->fifo, aout->format.i_rate);
+    p->pause_date = VLC_TS_INVALID;
+    p->time_report = VLC_TS_INVALID;
+    p->samples = samples;
+    p->starving = true;
+}
+
+void aout_PacketDestroy (audio_output_t *aout)
+{
+    aout_packet_t *p = aout_packet (aout);
+
+    aout_FifoDestroy (&p->partial);
+    aout_FifoDestroy (&p->fifo);
+    vlc_mutex_destroy (&p->lock);
+}
+
+static block_t *aout_OutputSlice (audio_output_t *);
+
+void aout_PacketPlay (audio_output_t *aout, block_t *block)
+{
+    aout_packet_t *p = aout_packet (aout);
+    mtime_t time_report;
+
+    vlc_mutex_lock (&p->lock);
+    aout_FifoPush (&p->partial, block);
+    while ((block = aout_OutputSlice (aout)) != NULL)
+        aout_FifoPush (&p->fifo, block);
+
+    time_report = p->time_report;
+    p->time_report = VLC_TS_INVALID;
+    vlc_mutex_unlock (&p->lock);
+
+    if (time_report != VLC_TS_INVALID)
+        aout_TimeReport (aout, mdate () + time_report);
+}
+
+void aout_PacketPause (audio_output_t *aout, bool pause, mtime_t date)
+{
+    aout_packet_t *p = aout_packet (aout);
+
+    if (pause)
+    {
+        assert (p->pause_date == VLC_TS_INVALID);
+        p->pause_date = date;
+    }
+    else
+    {
+        assert (p->pause_date != VLC_TS_INVALID);
+
+        mtime_t duration = date - p->pause_date;
+
+        p->pause_date = VLC_TS_INVALID;
+        vlc_mutex_lock (&p->lock);
+        aout_FifoMoveDates (&p->partial, duration);
+        aout_FifoMoveDates (&p->fifo, duration);
+        vlc_mutex_unlock (&p->lock);
+    }
 }
 
+void aout_PacketFlush (audio_output_t *aout, bool drain)
+{
+    aout_packet_t *p = aout_packet (aout);
+
+    vlc_mutex_lock (&p->lock);
+    aout_FifoReset (&p->partial);
+    aout_FifoReset (&p->fifo);
+    vlc_mutex_unlock (&p->lock);
+
+    (void) drain; /* TODO */
+}
 
-/*** Buffer management ***/
 
 /**
  * Rearranges audio blocks in correct number of samples.
  * @note (FIXME) This is left here for historical reasons. It belongs in the
  * output code. Besides, this operation should be avoided if possible.
  */
-static block_t *aout_OutputSlice (audio_output_t *p_aout, aout_fifo_t *p_fifo)
+static block_t *aout_OutputSlice (audio_output_t *p_aout)
 {
-    const unsigned samples = p_aout->i_nb_samples;
-    /* FIXME: Remove this silly constraint. Just pass buffers as they come to
-     * "smart" audio outputs. */
+    aout_packet_t *p = aout_packet (p_aout);
+    aout_fifo_t *p_fifo = &p->partial;
+    const unsigned samples = p->samples;
     assert( samples > 0 );
 
-    vlc_assert_locked( &p_aout->lock );
+    vlc_assert_locked( &p->lock );
 
     /* Retrieve the date of the next buffer. */
-    date_t exact_start_date = p_aout->fifo.end_date;
+    date_t exact_start_date = p->fifo.end_date;
     mtime_t start_date = date_Get( &exact_start_date );
 
     /* See if we have enough data to prepare a new buffer for the audio output. */
@@ -465,7 +543,7 @@ static block_t *aout_OutputSlice (audio_output_t *p_aout, aout_fifo_t *p_fifo)
         if( delta < 0 )
         {
             /* Is it really the best way to do it ? */
-            aout_FifoReset( &p_aout->fifo );
+            aout_FifoReset (&p->fifo);
             return NULL;
         }
         if( delta > 0 )
@@ -529,80 +607,77 @@ static block_t *aout_OutputSlice (audio_output_t *p_aout, aout_fifo_t *p_fifo)
     return p_buffer;
 }
 
-/*****************************************************************************
- * aout_OutputNextBuffer : give the audio output plug-in the right buffer
- *****************************************************************************
- * If b_can_sleek is 1, the aout core functions won't try to resample
- * new buffers to catch up - that is we suppose that the output plug-in can
- * compensate it by itself. S/PDIF outputs should always set b_can_sleek = 1.
- * This function is entered with no lock at all :-).
- *****************************************************************************/
-aout_buffer_t * aout_OutputNextBuffer( audio_output_t * p_aout,
-                                       mtime_t start_date,
-                                       bool b_can_sleek )
+/**
+ * Dequeues the next audio packet (a.k.a. audio fragment).
+ * The audio output plugin must first call aout_PacketPlay() to queue the
+ * decoded audio samples. Typically, audio_output_t.pf_play is set to, or calls
+ * aout_PacketPlay().
+ * @note This function is considered legacy. Please do not use this function in
+ * new audio output plugins.
+ * @param p_aout audio output instance
+ * @param start_date expected PTS of the audio packet
+ */
+block_t *aout_PacketNext (audio_output_t *p_aout, mtime_t start_date)
 {
-    aout_fifo_t *p_fifo = &p_aout->fifo;
-    aout_buffer_t * p_buffer;
-    mtime_t now = mdate();
-
-    aout_lock( p_aout );
-
-    /* Drop the audio sample if the audio output is really late.
-     * In the case of b_can_sleek, we don't use a resampler so we need to be
-     * a lot more severe. */
-    while( ((p_buffer = p_fifo->p_first) != NULL)
-     && p_buffer->i_pts < (b_can_sleek ? start_date : now) - AOUT_MAX_PTS_DELAY )
+    aout_packet_t *p = aout_packet (p_aout);
+    aout_fifo_t *p_fifo = &p->fifo;
+    block_t *p_buffer;
+    const bool b_can_sleek = AOUT_FMT_NON_LINEAR (&p_aout->format);
+    const mtime_t now = mdate ();
+    const mtime_t threshold =
+        (b_can_sleek ? start_date : now) - AOUT_MAX_PTS_DELAY;
+
+    vlc_mutex_lock( &p->lock );
+    if( p->pause_date != VLC_TS_INVALID )
+        goto out; /* paused: do not dequeue buffers */
+
+    for (;;)
     {
-        msg_Dbg( p_aout, "audio output is too slow (%"PRId64"), "
-                 "trashing %"PRId64"us", now - p_buffer->i_pts,
-                 p_buffer->i_length );
-        aout_BufferFree( aout_FifoPop( p_fifo ) );
-    }
+        p_buffer = p_fifo->p_first;
+        if (p_buffer == NULL)
+            goto out; /* nothing to play */
 
-    if( p_buffer == NULL )
-    {
-#if 0 /* This is bad because the audio output might just be trying to fill
-       * in its internal buffers. And anyway, it's up to the audio output
-       * to deal with this kind of starvation. */
-
-        /* Set date to 0, to allow the mixer to send a new buffer ASAP */
-        aout_FifoReset( &p_aout->fifo );
-        if ( !p_aout->b_starving )
-            msg_Dbg( p_aout,
-                 "audio output is starving (no input), playing silence" );
-        p_aout->b_starving = true;
-#endif
-        goto out;
+        if (p_buffer->i_pts >= threshold)
+            break;
+
+        /* Drop the audio sample if the audio output is really late.
+         * In the case of b_can_sleek, we don't use a resampler so we need to
+         * be a lot more severe. */
+        msg_Dbg (p_aout, "audio output is too slow (%"PRId64" us): "
+                 " trashing %"PRId64" us", threshold - p_buffer->i_pts,
+                 p_buffer->i_length);
+        block_Release (aout_FifoPop (p_fifo));
     }
 
     mtime_t delta = start_date - p_buffer->i_pts;
-    /* Here we suppose that all buffers have the same duration - this is
-     * generally true, and anyway if it's wrong it won't be a disaster.
-     */
-    if ( 0 > delta + p_buffer->i_length )
+    /* This assumes that all buffers have the same duration. This is true
+     * since aout_PacketPlay() (aout_OutputSlice()) is used. */
+    if (0 >= delta + p_buffer->i_length)
     {
-        if ( !p_aout->b_starving )
-            msg_Dbg( p_aout, "audio output is starving (%"PRId64"), "
-                     "playing silence", -delta );
-        p_aout->b_starving = true;
-        p_buffer = NULL;
-        goto out;
+        if (!p->starving)
+        {
+            msg_Dbg (p_aout, "audio output is starving (%"PRId64"), "
+                     "playing silence", delta);
+            p->starving = true;
+        }
+        goto out; /* nothing to play _yet_ */
     }
 
-    p_aout->b_starving = false;
+    p->starving = false;
     p_buffer = aout_FifoPop( p_fifo );
 
-    if!b_can_sleek
-     && ( delta > AOUT_MAX_PTS_DELAY || delta < -AOUT_MAX_PTS_ADVANCE ) )
+    if (!b_can_sleek
+     && (delta < -AOUT_MAX_PTS_ADVANCE || AOUT_MAX_PTS_DELAY < delta))
     {
-        /* Try to compensate the drift by doing some resampling. */
-        msg_Warn( p_aout, "output date isn't PTS date, requesting "
-                  "resampling (%"PRId64")", delta );
-
-        aout_FifoMoveDates( &p_aout->p_input->fifo, delta );
-        aout_FifoMoveDates( p_fifo, delta );
+        msg_Warn (p_aout, "audio output out of sync, "
+                          "adjusting dates (%"PRId64" us)", delta);
+        aout_FifoMoveDates (&p->partial, delta);
+        aout_FifoMoveDates (p_fifo, delta);
+        p->time_report = delta;
     }
-out:
-    aout_unlock( p_aout );
+    vlc_mutex_unlock( &p->lock );
     return p_buffer;
+out:
+    vlc_mutex_unlock( &p->lock );
+    return NULL;
 }