/*****************************************************************************
* Preamble
*****************************************************************************/
-#include <stdlib.h> /* calloc(), malloc(), free() */
-#include <string.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <math.h>
-#include <vlc/vlc.h>
+#include <assert.h>
+#include <vlc_common.h>
#include <vlc_aout.h>
+#include <vlc_aout_intf.h>
+#include <vlc_cpu.h>
+#include <vlc_modules.h>
+
+#include "libvlc.h"
#include "aout_internal.h"
/*****************************************************************************
*****************************************************************************
* This function is entered with the mixer lock.
*****************************************************************************/
-int aout_OutputNew( aout_instance_t * p_aout,
- audio_sample_format_t * p_format )
+int aout_OutputNew( audio_output_t *p_aout,
+ const audio_sample_format_t * p_format )
{
- /* Retrieve user defaults. */
- int i_rate = config_GetInt( p_aout, "aout-rate" );
- vlc_value_t val, text;
- /* kludge to avoid a fpu error when rate is 0... */
- if( i_rate == 0 ) i_rate = -1;
+ aout_owner_t *owner = aout_owner (p_aout);
- memcpy( &p_aout->output.output, p_format, sizeof(audio_sample_format_t) );
- if ( i_rate != -1 )
- p_aout->output.output.i_rate = i_rate;
- aout_FormatPrepare( &p_aout->output.output );
+ aout_assert_locked( p_aout );
+ p_aout->format = *p_format;
- vlc_mutex_lock( &p_aout->output_fifo_lock );
+ /* Retrieve user defaults. */
+ int i_rate = var_InheritInteger( p_aout, "aout-rate" );
+ if ( i_rate != 0 )
+ p_aout->format.i_rate = i_rate;
+ aout_FormatPrepare( &p_aout->format );
/* Find the best output plug-in. */
- p_aout->output.p_module = module_Need( p_aout, "audio output", "$aout", 0);
- if ( p_aout->output.p_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" );
- vlc_mutex_unlock( &p_aout->output_fifo_lock );
return -1;
}
(VLC_VAR_INTEGER | VLC_VAR_HASCHOICE) )
{
/* The user may have selected a different channels configuration. */
- var_Get( p_aout, "audio-channels", &val );
-
- if ( val.i_int == AOUT_VAR_CHAN_RSTEREO )
- {
- p_aout->output.output.i_original_channels |=
- AOUT_CHAN_REVERSESTEREO;
- }
- else if ( val.i_int == AOUT_VAR_CHAN_STEREO )
- {
- p_aout->output.output.i_original_channels =
- AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
- }
- else if ( val.i_int == AOUT_VAR_CHAN_LEFT )
- {
- p_aout->output.output.i_original_channels = AOUT_CHAN_LEFT;
- }
- else if ( val.i_int == AOUT_VAR_CHAN_RIGHT )
- {
- p_aout->output.output.i_original_channels = AOUT_CHAN_RIGHT;
- }
- else if ( val.i_int == AOUT_VAR_CHAN_DOLBYS )
+ switch( var_InheritInteger( p_aout, "audio-channels" ) )
{
- p_aout->output.output.i_original_channels
- = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_DOLBYSTEREO;
+ case AOUT_VAR_CHAN_RSTEREO:
+ p_aout->format.i_original_channels |= AOUT_CHAN_REVERSESTEREO;
+ break;
+ case AOUT_VAR_CHAN_STEREO:
+ p_aout->format.i_original_channels =
+ AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
+ break;
+ case AOUT_VAR_CHAN_LEFT:
+ p_aout->format.i_original_channels = AOUT_CHAN_LEFT;
+ break;
+ case AOUT_VAR_CHAN_RIGHT:
+ p_aout->format.i_original_channels = AOUT_CHAN_RIGHT;
+ break;
+ case AOUT_VAR_CHAN_DOLBYS:
+ p_aout->format.i_original_channels =
+ AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_DOLBYSTEREO;
+ break;
}
}
- else if ( p_aout->output.output.i_physical_channels == AOUT_CHAN_CENTER
- && (p_aout->output.output.i_original_channels
+ else if ( p_aout->format.i_physical_channels == AOUT_CHAN_CENTER
+ && (p_aout->format.i_original_channels
& AOUT_CHAN_PHYSMASK) == (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT) )
{
+ vlc_value_t val, text;
+
/* Mono - create the audio-channels variable. */
var_Create( p_aout, "audio-channels",
VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
val.i_int = AOUT_VAR_CHAN_RIGHT; text.psz_string = _("Right");
var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
- if ( p_aout->output.output.i_original_channels & AOUT_CHAN_DUALMONO )
+ if ( p_aout->format.i_original_channels & AOUT_CHAN_DUALMONO )
{
/* Go directly to the left channel. */
- p_aout->output.output.i_original_channels = AOUT_CHAN_LEFT;
- val.i_int = AOUT_VAR_CHAN_LEFT;
- var_Set( p_aout, "audio-channels", val );
+ p_aout->format.i_original_channels = AOUT_CHAN_LEFT;
+ var_SetInteger( p_aout, "audio-channels", AOUT_VAR_CHAN_LEFT );
}
var_AddCallback( p_aout, "audio-channels", aout_ChannelsRestart,
NULL );
}
- else if ( p_aout->output.output.i_physical_channels ==
+ else if ( p_aout->format.i_physical_channels ==
(AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)
- && (p_aout->output.output.i_original_channels &
+ && (p_aout->format.i_original_channels &
(AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT)) )
{
+ vlc_value_t val, text;
+
/* Stereo - create the audio-channels variable. */
var_Create( p_aout, "audio-channels",
VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
text.psz_string = _("Audio Channels");
var_Change( p_aout, "audio-channels", VLC_VAR_SETTEXT, &text, NULL );
- if ( p_aout->output.output.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
+ if ( p_aout->format.i_original_channels & AOUT_CHAN_DOLBYSTEREO )
{
val.i_int = AOUT_VAR_CHAN_DOLBYS;
text.psz_string = _("Dolby Surround");
var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
val.i_int = AOUT_VAR_CHAN_RSTEREO; text.psz_string=_("Reverse stereo");
var_Change( p_aout, "audio-channels", VLC_VAR_ADDCHOICE, &val, &text );
- if ( p_aout->output.output.i_original_channels & AOUT_CHAN_DUALMONO )
+ if ( p_aout->format.i_original_channels & AOUT_CHAN_DUALMONO )
{
/* Go directly to the left channel. */
- p_aout->output.output.i_original_channels = AOUT_CHAN_LEFT;
- val.i_int = AOUT_VAR_CHAN_LEFT;
- var_Set( p_aout, "audio-channels", val );
+ p_aout->format.i_original_channels = AOUT_CHAN_LEFT;
+ var_SetInteger( p_aout, "audio-channels", AOUT_VAR_CHAN_LEFT );
}
var_AddCallback( p_aout, "audio-channels", aout_ChannelsRestart,
NULL );
}
- val.b_bool = VLC_TRUE;
- var_Set( p_aout, "intf-change", val );
-
- aout_FormatPrepare( &p_aout->output.output );
-
- /* Prepare FIFO. */
- aout_FifoInit( p_aout, &p_aout->output.fifo,
- p_aout->output.output.i_rate );
+ var_TriggerCallback( p_aout, "intf-change" );
- vlc_mutex_unlock( &p_aout->output_fifo_lock );
+ aout_FormatPrepare( &p_aout->format );
+ aout_FormatPrint( p_aout, "output", &p_aout->format );
- aout_FormatPrint( p_aout, "output", &p_aout->output.output );
-
- /* Calculate the resulting mixer output format. */
- memcpy( &p_aout->mixer.mixer, &p_aout->output.output,
- sizeof(audio_sample_format_t) );
- if ( !AOUT_FMT_NON_LINEAR(&p_aout->output.output) )
- {
- /* Non-S/PDIF mixer only deals with float32 or fixed32. */
- p_aout->mixer.mixer.i_format
- = (p_aout->p_libvlc_global->i_cpu & CPU_CAPABILITY_FPU) ?
- VLC_FOURCC('f','l','3','2') :
- VLC_FOURCC('f','i','3','2');
- aout_FormatPrepare( &p_aout->mixer.mixer );
- }
+ /* Choose the mixer 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
- {
- p_aout->mixer.mixer.i_format = p_format->i_format;
- }
+ /* Most audio filters can only deal with single-precision,
+ * so lets always use that when hardware supports floating point. */
+ if( HAVE_FPU )
+ 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 )
+ 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. */
+ owner->mixer_format.i_format = VLC_CODEC_S16N;
- aout_FormatPrint( p_aout, "mixer", &p_aout->mixer.mixer );
+ aout_FormatPrepare (&owner->mixer_format);
+ aout_FormatPrint (p_aout, "mixer", &owner->mixer_format);
/* Create filters. */
- p_aout->output.i_nb_filters = 0;
- if ( aout_FiltersCreatePipeline( p_aout, p_aout->output.pp_filters,
- &p_aout->output.i_nb_filters,
- &p_aout->mixer.mixer,
- &p_aout->output.output ) < 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->output.p_module );
+ module_unneed (p_aout, owner->module);
+ owner->module = NULL;
return -1;
}
-
- /* Prepare hints for the buffer allocator. */
- p_aout->mixer.output_alloc.i_alloc_type = AOUT_ALLOC_HEAP;
- p_aout->mixer.output_alloc.i_bytes_per_sec
- = p_aout->mixer.mixer.i_bytes_per_frame
- * p_aout->mixer.mixer.i_rate
- / p_aout->mixer.mixer.i_frame_length;
-
- aout_FiltersHintBuffers( p_aout, p_aout->output.pp_filters,
- p_aout->output.i_nb_filters,
- &p_aout->mixer.output_alloc );
-
- p_aout->output.b_error = 0;
return 0;
}
*****************************************************************************
* This function is entered with the mixer lock.
*****************************************************************************/
-void aout_OutputDelete( aout_instance_t * p_aout )
+void aout_OutputDelete( audio_output_t * p_aout )
{
- if ( p_aout->output.b_error )
- {
- return;
- }
+ aout_owner_t *owner = aout_owner (p_aout);
- module_Unneed( p_aout, p_aout->output.p_module );
+ aout_assert_locked( p_aout );
- aout_FiltersDestroyPipeline( p_aout, p_aout->output.pp_filters,
- p_aout->output.i_nb_filters );
- aout_FifoDestroy( p_aout, &p_aout->output.fifo );
+ if (owner->module == NULL)
+ return;
- p_aout->output.b_error = VLC_TRUE;
+ module_unneed (p_aout, owner->module);
+ aout_VolumeNoneInit( p_aout ); /* clear volume callback */
+ owner->module = NULL;
+ aout_FiltersDestroyPipeline (owner->filters, owner->nb_filters);
}
/*****************************************************************************
*****************************************************************************
* This function is entered with the mixer lock.
*****************************************************************************/
-void aout_OutputPlay( aout_instance_t * p_aout, aout_buffer_t * p_buffer )
+void aout_OutputPlay (audio_output_t *aout, block_t *block)
{
- aout_FiltersPlay( p_aout, p_aout->output.pp_filters,
- p_aout->output.i_nb_filters,
- &p_buffer );
+ aout_owner_t *owner = aout_owner (aout);
+
+ aout_assert_locked (aout);
- if( p_buffer->i_nb_bytes == 0 )
+ aout_FiltersPlay (owner->filters, owner->nb_filters, &block);
+ if (block == NULL)
+ return;
+ if (block->i_buffer == 0)
{
- aout_BufferFree( p_buffer );
+ block_Release (block);
return;
}
- vlc_mutex_lock( &p_aout->output_fifo_lock );
- aout_FifoPush( p_aout, &p_aout->output.fifo, p_buffer );
- p_aout->output.pf_play( p_aout );
- vlc_mutex_unlock( &p_aout->output_fifo_lock );
+ aout->pf_play (aout, block);
}
-/*****************************************************************************
- * 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( aout_instance_t * p_aout,
- mtime_t start_date,
- vlc_bool_t b_can_sleek )
+/**
+ * Notifies the audio output (if any) of pause/resume events.
+ * This enables the output to expedite pause, instead of waiting for its
+ * buffers to drain.
+ */
+void aout_OutputPause( audio_output_t *aout, bool pause, mtime_t date )
+{
+ aout_assert_locked( aout );
+ if( aout->pf_pause != NULL )
+ aout->pf_pause( aout, pause, date );
+}
+
+/**
+ * Flushes or drains the audio output buffers.
+ * This enables the output to expedite seek and stop.
+ * @param wait if true, wait for buffer playback (i.e. drain),
+ * if false, discard the buffers immediately (i.e. flush)
+ */
+void aout_OutputFlush( audio_output_t *aout, bool wait )
+{
+ aout_assert_locked( aout );
+
+ if( aout->pf_flush != NULL )
+ aout->pf_flush( aout, wait );
+}
+
+
+/*** Volume handling ***/
+
+/**
+ * Dummy volume setter. This is the default volume setter.
+ */
+static int aout_VolumeNoneSet (audio_output_t *aout, float volume, bool mute)
+{
+ (void)aout; (void)volume; (void)mute;
+ return -1;
+}
+
+/**
+ * Configures the dummy volume setter.
+ * @note Audio output plugins for which volume is irrelevant
+ * should call this function during activation.
+ */
+void aout_VolumeNoneInit (audio_output_t *aout)
{
- aout_buffer_t * p_buffer;
+ /* aout_New() -safely- calls this function without the lock, before any
+ * other thread knows of this audio output instance.
+ aout_assert_locked (aout); */
+ aout->pf_volume_set = aout_VolumeNoneSet;
+ var_Destroy (aout, "volume");
+ var_Destroy (aout, "mute");
+}
+
+/**
+ * Volume setter for software volume.
+ */
+static int aout_VolumeSoftSet (audio_output_t *aout, float volume, bool mute)
+{
+ aout_owner_t *owner = aout_owner (aout);
+
+ aout_assert_locked (aout);
- vlc_mutex_lock( &p_aout->output_fifo_lock );
+ /* Cubic mapping from software volume to amplification factor.
+ * This provides a good tradeoff between low and high volume ranges.
+ *
+ * This code is only used for the VLC software mixer. If you change this
+ * formula, be sure to update the aout_VolumeHardInit()-based plugins also.
+ */
+ if (!mute)
+ volume = volume * volume * volume;
+ else
+ volume = 0.;
+
+ owner->volume.multiplier = volume;
+ return 0;
+}
+
+/**
+ * Configures the volume setter for software mixing
+ * and apply the default volume.
+ * @note Audio output plugins that cannot apply the volume
+ * should call this function during activation.
+ */
+void aout_VolumeSoftInit (audio_output_t *aout)
+{
+ audio_volume_t volume = var_InheritInteger (aout, "volume");
+ bool mute = var_InheritBool (aout, "mute");
+
+ aout_assert_locked (aout);
+ aout->pf_volume_set = aout_VolumeSoftSet;
+ aout_VolumeSoftSet (aout, volume / (float)AOUT_VOLUME_DEFAULT, mute);
+}
+
+/**
+ * Configures a custom volume setter. This is used by audio outputs that can
+ * control the hardware volume directly and/or emulate it internally.
+ * @param setter volume setter callback
+ */
+void aout_VolumeHardInit (audio_output_t *aout, aout_volume_cb setter)
+{
+ 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);
+}
+
+/**
+ * Supply or update the current custom ("hardware") volume.
+ * @note This only makes sense after calling aout_VolumeHardInit().
+ * @param setter volume setter callback
+ * @param volume current custom volume
+ * @param mute current mute flag
+ *
+ * @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)
+{
+ 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");
+}
- p_buffer = p_aout->output.fifo.p_first;
- /* 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_buffer->start_date <
- (b_can_sleek ? start_date : mdate()) - AOUT_PTS_TOLERANCE )
+/*** 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)
{
- msg_Dbg( p_aout, "audio output is too slow ("I64Fd"), "
- "trashing "I64Fd"us", mdate() - p_buffer->start_date,
- p_buffer->end_date - p_buffer->start_date );
- p_buffer = p_buffer->p_next;
- aout_BufferFree( p_aout->output.fifo.p_first );
- p_aout->output.fifo.p_first = p_buffer;
+ assert (p->pause_date == VLC_TS_INVALID);
+ p->pause_date = date;
}
-
- if ( p_buffer == NULL )
+ else
{
- p_aout->output.fifo.pp_last = &p_aout->output.fifo.p_first;
-
-#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_FifoSet( p_aout, &p_aout->output.fifo, 0 );
- if ( !p_aout->output.b_starving )
- msg_Dbg( p_aout,
- "audio output is starving (no input), playing silence" );
- p_aout->output.b_starving = 1;
-#endif
+ assert (p->pause_date != VLC_TS_INVALID);
+
+ mtime_t duration = date - p->pause_date;
- vlc_mutex_unlock( &p_aout->output_fifo_lock );
+ 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 */
+}
+
+
+/**
+ * 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_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->lock );
+
+ /* Retrieve the date of the next buffer. */
+ 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. */
+ aout_buffer_t *p_buffer = p_fifo->p_first;
+ if( p_buffer == NULL )
return NULL;
+
+ /* Find the earliest start date available. */
+ if ( start_date == VLC_TS_INVALID )
+ {
+ start_date = p_buffer->i_pts;
+ date_Set( &exact_start_date, start_date );
}
+ /* Compute the end date for the new buffer. */
+ mtime_t end_date = date_Increment( &exact_start_date, samples );
- /* 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 ( p_buffer->start_date > start_date
- + (p_buffer->end_date - p_buffer->start_date) )
- /*
- * + AOUT_PTS_TOLERANCE )
- * There is no reason to want that, it just worsen the scheduling of
- * an audio sample after an output starvation (ie. on start or on resume)
- * --Gibalou
- */
+ /* Check that start_date is available. */
+ mtime_t prev_date;
+ for( ;; )
{
- vlc_mutex_unlock( &p_aout->output_fifo_lock );
- if ( !p_aout->output.b_starving )
- msg_Dbg( p_aout, "audio output is starving ("I64Fd"), "
- "playing silence", p_buffer->start_date - start_date );
- p_aout->output.b_starving = 1;
- return NULL;
+ /* Check for the continuity of start_date */
+ prev_date = p_buffer->i_pts + p_buffer->i_length;
+ if( prev_date >= start_date - 1 )
+ break;
+ /* We authorize a +-1 because rounding errors get compensated
+ * regularly. */
+ msg_Warn( p_aout, "got a packet in the past (%"PRId64")",
+ start_date - prev_date );
+ aout_BufferFree( aout_FifoPop( p_fifo ) );
+
+ p_buffer = p_fifo->p_first;
+ if( p_buffer == NULL )
+ return NULL;
}
- p_aout->output.b_starving = 0;
+ /* Check that we have enough samples. */
+ while( prev_date < end_date )
+ {
+ p_buffer = p_buffer->p_next;
+ if( p_buffer == NULL )
+ return NULL;
+
+ /* Check that all buffers are contiguous. */
+ if( prev_date != p_buffer->i_pts )
+ {
+ msg_Warn( p_aout,
+ "buffer hole, dropping packets (%"PRId64")",
+ p_buffer->i_pts - prev_date );
+
+ aout_buffer_t *p_deleted;
+ while( (p_deleted = p_fifo->p_first) != p_buffer )
+ aout_BufferFree( aout_FifoPop( p_fifo ) );
+ }
+
+ prev_date = p_buffer->i_pts + p_buffer->i_length;
+ }
- if ( !b_can_sleek &&
- ( (p_buffer->start_date - start_date > AOUT_PTS_TOLERANCE)
- || (start_date - p_buffer->start_date > AOUT_PTS_TOLERANCE) ) )
+ if( !AOUT_FMT_NON_LINEAR( &p_aout->format ) )
{
- /* Try to compensate the drift by doing some resampling. */
- int i;
- mtime_t difference = start_date - p_buffer->start_date;
- msg_Warn( p_aout, "output date isn't PTS date, requesting "
- "resampling ("I64Fd")", difference );
-
- vlc_mutex_lock( &p_aout->input_fifos_lock );
- for ( i = 0; i < p_aout->i_nb_inputs; i++ )
+ p_buffer = p_fifo->p_first;
+
+ /* Additionally check that p_first_byte_to_mix is well located. */
+ const unsigned framesize = p_aout->format.i_bytes_per_frame;
+ ssize_t delta = (start_date - p_buffer->i_pts)
+ * p_aout->format.i_rate / CLOCK_FREQ;
+ if( delta != 0 )
+ msg_Warn( p_aout, "input start is not output end (%zd)", delta );
+ if( delta < 0 )
+ {
+ /* Is it really the best way to do it ? */
+ aout_FifoReset (&p->fifo);
+ return NULL;
+ }
+ if( delta > 0 )
{
- aout_fifo_t * p_fifo = &p_aout->pp_inputs[i]->fifo;
+ mtime_t t = delta * CLOCK_FREQ / p_aout->format.i_rate;
+ p_buffer->i_nb_samples -= delta;
+ p_buffer->i_pts += t;
+ p_buffer->i_length -= t;
+ delta *= framesize;
+ p_buffer->p_buffer += delta;
+ p_buffer->i_buffer -= delta;
+ }
+
+ /* Build packet with adequate number of samples */
+ unsigned needed = samples * framesize;
+ p_buffer = block_Alloc( needed );
+ if( unlikely(p_buffer == NULL) )
+ /* XXX: should free input buffers */
+ return NULL;
+ p_buffer->i_nb_samples = samples;
- aout_FifoMoveDates( p_aout, p_fifo, difference );
+ for( uint8_t *p_out = p_buffer->p_buffer; needed > 0; )
+ {
+ aout_buffer_t *p_inbuf = p_fifo->p_first;
+ if( unlikely(p_inbuf == NULL) )
+ {
+ msg_Err( p_aout, "packetization error" );
+ vlc_memset( p_out, 0, needed );
+ break;
+ }
+
+ const uint8_t *p_in = p_inbuf->p_buffer;
+ size_t avail = p_inbuf->i_nb_samples * framesize;
+ if( avail > needed )
+ {
+ vlc_memcpy( p_out, p_in, needed );
+ p_fifo->p_first->p_buffer += needed;
+ p_fifo->p_first->i_buffer -= needed;
+ needed /= framesize;
+ p_fifo->p_first->i_nb_samples -= needed;
+
+ mtime_t t = needed * CLOCK_FREQ / p_aout->format.i_rate;
+ p_fifo->p_first->i_pts += t;
+ p_fifo->p_first->i_length -= t;
+ break;
+ }
+
+ vlc_memcpy( p_out, p_in, avail );
+ needed -= avail;
+ p_out += avail;
+ /* Next buffer */
+ aout_BufferFree( aout_FifoPop( p_fifo ) );
}
+ }
+ else
+ p_buffer = aout_FifoPop( p_fifo );
+
+ p_buffer->i_pts = start_date;
+ p_buffer->i_length = end_date - start_date;
- aout_FifoMoveDates( p_aout, &p_aout->output.fifo, difference );
- vlc_mutex_unlock( &p_aout->input_fifos_lock );
+ return p_buffer;
+}
+
+/**
+ * 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_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 (;;)
+ {
+ p_buffer = p_fifo->p_first;
+ if (p_buffer == NULL)
+ goto out; /* nothing to play */
+
+ 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));
}
- p_aout->output.fifo.p_first = p_buffer->p_next;
- if ( p_buffer->p_next == NULL )
+ mtime_t delta = start_date - p_buffer->i_pts;
+ /* 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)
{
- p_aout->output.fifo.pp_last = &p_aout->output.fifo.p_first;
+ if (!p->starving)
+ {
+ msg_Dbg (p_aout, "audio output is starving (%"PRId64"), "
+ "playing silence", delta);
+ p->starving = true;
+ }
+ goto out; /* nothing to play _yet_ */
}
- vlc_mutex_unlock( &p_aout->output_fifo_lock );
+ p->starving = false;
+ p_buffer = aout_FifoPop( p_fifo );
+
+ if (!b_can_sleek
+ && (delta < -AOUT_MAX_PTS_ADVANCE || AOUT_MAX_PTS_DELAY < 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;
+ }
+ vlc_mutex_unlock( &p->lock );
return p_buffer;
+out:
+ vlc_mutex_unlock( &p->lock );
+ return NULL;
}