<html><head></head><body>I already told you that this cannot work. If you think about all possible combos, it's kinda obvious that concurrent inputs are necessary, but not sufficient, to make it work.<br><br>You cannot know if PTS will be continuous or if the formats will match, or if there will be an active audio ES at all.<br><br><div class="gmail_quote">Le 21 mars 2019 01:09:10 GMT+08:00, Thomas Guillem <thomas@gllm.fr> a écrit :<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<pre class="k9mail">The player will call aout_OutputSetGaplessEnabled(true|false) when there is a<br>next media to play or not. This call is protected with the same mutex used for<br>start/stop and can be called from anywhere.<br><br>Then, when the stream is draining (aout_DecDrain), it will postpone the actual<br>drain if gapless is enabled, and do the following depending on the next aout<br>call:<br><br> - if play is called just after, the postponed drain is executed and the<br> gapless state is canceled.<br> - if flush is called, the gapless state is canceled and everything is flushed<br> - if stop is called, filters are cleaned but the stream is not stopped, it is<br> saved for a future usage: (via aout_OutputSaveStream()).<br><br>On the next stream start, it will try to recover the previous stream (that<br>could be stopped if the player disabled gapless in the meantime) and will do<br>the following depending on the aout_DecNew parameters:<br><br> - if format are different: cancel gapless state, drain, flush and stop the<br> previous stream.<br><br> - if format are equals: use the previous stream (don't call aout->stop(),<br> aout->start()), and set-up new filters chain with the new clock.<br><br>The final gapless transition will be done from aout_DecPlay():<br><br> - if the previous stream delay (time_get) is compatible with the next clock,<br> blocks will be added in a temporary buffer until the clock jitter delay is<br> reached.<br> - if not, cancel the gapless state, drain and flush the (previous=current)<br> stream (but don't restart it), and play all blocks normally.<br><br>When the jitter delay is reached, blocks added in the temporary buffers can be<br>played and the playback can resume normally.<br><br>Fixes #549<hr> src/audio_output/aout_internal.h | 17 +++<br> src/audio_output/dec.c | 206 +++++++++++++++++++++++++++++--<br> src/audio_output/output.c | 74 +++++++++++<br> 3 files changed, 289 insertions(+), 8 deletions(-)<br><br>diff --git a/src/audio_output/aout_internal.h b/src/audio_output/aout_internal.h<br>index 12fa4dfad9..bab59977eb 100644<br>--- a/src/audio_output/aout_internal.h<br>+++ b/src/audio_output/aout_internal.h<br>@@ -47,6 +47,8 @@ typedef struct<br> aout_filters_t *filters;<br> aout_volume_t *volume;<br> <br>+ bool stream_saved;<br>+<br> struct<br> {<br> vlc_mutex_t lock;<br>@@ -85,6 +87,17 @@ typedef struct<br> atomic_uint buffers_played;<br> atomic_uchar restart;<br> <br>+ bool gapless_enabled;<br>+ enum<br>+ {<br>+ GAPLESS_STATE_NONE,<br>+ GAPLESS_STATE_DRAINED,<br>+ GAPLESS_STATE_STOPPED,<br>+ GAPLESS_STATE_STARTED,<br>+ } gapless_state;<br>+ block_t *gapless_block_chain;<br>+ block_t **gapless_block_last;<br>+<br> atomic_uintptr_t refs;<br> } aout_owner_t;<br> <br>@@ -120,6 +133,10 @@ void aout_Destroy (audio_output_t *);<br> int aout_OutputNew(audio_output_t *, audio_sample_format_t *,<br> aout_filters_cfg_t *filters_cfg);<br> void aout_OutputDelete( audio_output_t * p_aout );<br>+void aout_OutputSetGaplessEnabled(audio_output_t *aout, bool enabled);<br>+bool aout_OutputIsGaplessEnabled(audio_output_t *aout);<br>+int aout_OutputRecoverStream(audio_output_t *aout);<br>+void aout_OutputSaveStream(audio_output_t *aout);<br> <br> <br> /* From common.c : */<br>diff --git a/src/audio_output/dec.c b/src/audio_output/dec.c<br>index 75ea4e6621..e4f1737776 100644<br>--- a/src/audio_output/dec.c<br>+++ b/src/audio_output/dec.c<br>@@ -65,6 +65,44 @@ static bool aout_FormatEquals(const audio_sample_format_t *f1,<br> && f1->i_channels == f2->i_channels;<br> }<br> <br>+static int aout_GaplessReuseStream(audio_output_t *aout,<br>+ const audio_sample_format_t *format,<br>+ vlc_clock_t *clock,<br>+ const audio_replay_gain_t *replay_gain)<br>+{<br>+ aout_owner_t *owner = aout_owner(aout);<br>+<br>+ if (aout_OutputRecoverStream(aout) != VLC_SUCCESS)<br>+ {<br>+ /* Unlikely case: The old stream was already stopped by the player */<br>+ owner->gapless_state = GAPLESS_STATE_NONE;<br>+ return -1;<br>+ }<br>+<br>+ if (!aout_FormatEquals(format, &owner->input_format))<br>+ goto fail;<br>+<br>+ /* Reuse the previous stream but set-up new clock and filters */<br>+ owner->sync.clock = clock;<br>+ owner->filters =<br>+ aout_FiltersNewWithClock(VLC_OBJECT(aout), clock,<br>+ format, &owner->mixer_format,<br>+ &owner->filters_cfg);<br>+ if (owner->filters == NULL)<br>+ goto fail;<br>+<br>+ owner->volume = aout_volume_New (aout, replay_gain);<br>+ owner->gapless_state = GAPLESS_STATE_STARTED;<br>+ msg_Dbg(aout, "trying gapless transition");<br>+ return 0;<br>+fail:<br>+ /* No gapless: drain and delete the old stream */<br>+ aout_Drain(aout);<br>+ aout_OutputDelete(aout);<br>+ owner->gapless_state = GAPLESS_STATE_NONE;<br>+ return -1;<br>+}<br>+<br> /**<br> * Creates an audio output<br> */<br>@@ -74,6 +112,16 @@ int aout_DecNew(audio_output_t *p_aout, const audio_sample_format_t *p_format,<br> assert(p_aout);<br> assert(p_format);<br> assert(clock);<br>+<br>+ aout_owner_t *owner = aout_owner(p_aout);<br>+<br>+ assert(owner->gapless_state == GAPLESS_STATE_NONE<br>+ || owner->gapless_state == GAPLESS_STATE_STOPPED);<br>+<br>+ if (owner->gapless_state == GAPLESS_STATE_STOPPED<br>+ && aout_GaplessReuseStream(p_aout, p_format, clock, p_replay_gain) == 0)<br>+ return 0;<br>+<br> if( p_format->i_bitspersample > 0 )<br> {<br> /* Sanitize audio format, input need to have a valid physical channels<br>@@ -100,8 +148,6 @@ int aout_DecNew(audio_output_t *p_aout, const audio_sample_format_t *p_format,<br> return -1;<br> }<br> <br>- aout_owner_t *owner = aout_owner(p_aout);<br>-<br> /* Create the audio output stream */<br> owner->volume = aout_volume_New (p_aout, p_replay_gain);<br> <br>@@ -147,11 +193,25 @@ void aout_DecDelete (audio_output_t *aout)<br> {<br> aout_owner_t *owner = aout_owner (aout);<br> <br>+ assert(owner->gapless_state == GAPLESS_STATE_NONE<br>+ || owner->gapless_state == GAPLESS_STATE_DRAINED);<br>+<br> if (owner->mixer_format.i_format)<br> {<br>- aout_DecFlush(aout);<br>- aout_FiltersDelete (aout, owner->filters);<br>- aout_OutputDelete (aout);<br>+ if (owner->gapless_state == GAPLESS_STATE_DRAINED)<br>+ {<br>+ /* Gapless transition: save the stream for the next aout_DecNew<br>+ * call */<br>+ aout_OutputSaveStream(aout);<br>+ owner->gapless_state = GAPLESS_STATE_STOPPED;<br>+ aout_FiltersDelete (aout, owner->filters);<br>+ }<br>+ else<br>+ {<br>+ aout_DecFlush(aout);<br>+ aout_FiltersDelete (aout, owner->filters);<br>+ aout_OutputDelete (aout);<br>+ }<br> }<br> aout_volume_Delete (owner->volume);<br> owner->volume = NULL;<br>@@ -206,6 +266,7 @@ static int aout_CheckReady (audio_output_t *aout)<br> }<br> aout_FiltersSetClockDelay(owner->filters, owner->sync.delay);<br> }<br>+ owner->gapless_state = GAPLESS_STATE_NONE;<br> /* TODO: This would be a good time to call clean up any video output<br> * left over by an audio visualization:<br> input_resource_TerminatVout(MAGIC HERE); */<br>@@ -426,6 +487,89 @@ static void aout_Play(audio_output_t *aout, block_t *block,<br> atomic_fetch_add_explicit(&owner->buffers_played, 1, memory_order_relaxed);<br> }<br> <br>+static void aout_GaplessPlay(audio_output_t *aout, block_t *block,<br>+ vlc_tick_t system_now, vlc_tick_t original_pts)<br>+{<br>+ aout_owner_t *owner = aout_owner (aout);<br>+ vlc_tick_t delay;<br>+<br>+ assert(owner->gapless_state == GAPLESS_STATE_STARTED);<br>+<br>+ /* Save blocks until we reach the new clock jitter delay or until we cancel<br>+ * gapless due to timing issues */<br>+ if (block != NULL)<br>+ {<br>+ block->i_dts = original_pts;<br>+ block_ChainLastAppend(&owner->gapless_block_last, block);<br>+ }<br>+<br>+ if (aout->time_get(aout, &delay) != 0)<br>+ {<br>+ msg_Warn(aout, "aout delay invalid: canceling gapless");<br>+ goto cancel;<br>+ }<br>+<br>+ const vlc_tick_t play_date =<br>+ vlc_clock_ConvertToSystem(owner->sync.clock, system_now, 0,<br>+ owner->sync.rate);<br>+ const vlc_tick_t jitter = play_date - system_now;<br>+<br>+ if (delay <= jitter)<br>+ {<br>+ msg_Warn(aout, "clock jitter higher than aout delay: canceling gapless");<br>+ goto cancel;<br>+ }<br>+<br>+ if (jitter >= 0 && block != NULL)<br>+ return; /* need more blocks */<br>+<br>+ if (delay <= AOUT_MAX_PTS_ADVANCE)<br>+ {<br>+ msg_Warn(aout, "delay too short: canceling gapless");<br>+ goto cancel;<br>+ }<br>+<br>+ if (owner->gapless_block_chain)<br>+ {<br>+ const vlc_tick_t block_original_pts = owner->gapless_block_chain->i_dts;<br>+ const vlc_tick_t drift =<br>+ vlc_clock_Update(owner->sync.clock, system_now + delay,<br>+ block_original_pts, owner->sync.rate);<br>+ if (drift != 0)<br>+ {<br>+ msg_Warn(aout, "can't update clock: canceling gapless");<br>+ goto cancel;<br>+ }<br>+ }<br>+<br>+ msg_Dbg(aout, "gapless transition successful");<br>+ owner->gapless_state = GAPLESS_STATE_NONE;<br>+ owner->sync.discontinuity = false;<br>+<br>+cancel:<br>+ if (owner->gapless_state != GAPLESS_STATE_NONE)<br>+ {<br>+ /* Gapless canceled: drain and flush the previous stream */<br>+ aout_Drain(aout);<br>+ aout_DecFlush(aout);<br>+ owner->gapless_state = GAPLESS_STATE_NONE;<br>+ }<br>+<br>+ /* Play every saved blocks now */<br>+ for (block_t *next = NULL, *it = owner->gapless_block_chain; it; it = next)<br>+ {<br>+ next = it->p_next;<br>+ const vlc_tick_t block_original_pts = it->i_dts;<br>+ it->i_dts = VLC_TICK_INVALID;<br>+<br>+ aout_Play(aout, it, system_now, block_original_pts);<br>+ system_now = vlc_tick_now(); /* system_now might be too old */<br>+ }<br>+<br>+ owner->gapless_block_chain = NULL;<br>+ owner->gapless_block_last = &owner->gapless_block_chain;<br>+}<br>+<br> /*****************************************************************************<br> * aout_DecPlay : filter & mix the decoded buffer<br> *****************************************************************************/<br>@@ -433,6 +577,17 @@ int aout_DecPlay(audio_output_t *aout, block_t *block)<br> {<br> aout_owner_t *owner = aout_owner (aout);<br> <br>+ if (unlikely(owner->gapless_state == GAPLESS_STATE_DRAINED))<br>+ {<br>+ /* Drained but not stopped: do the drain that was previously ignored<br>+ * and cancel the gapless transition */<br>+ aout_Drain(aout);<br>+ owner->gapless_state = GAPLESS_STATE_NONE;<br>+ }<br>+<br>+ assert(owner->gapless_state == GAPLESS_STATE_NONE<br>+ || owner->gapless_state == GAPLESS_STATE_STARTED);<br>+<br> assert (block->i_pts != VLC_TICK_INVALID);<br> <br> block->i_length = vlc_tick_from_samples( block->i_nb_samples,<br>@@ -476,7 +631,8 @@ int aout_DecPlay(audio_output_t *aout, block_t *block)<br> aout_volume_Amplify (owner->volume, block);<br> <br> /* Update delay */<br>- if (owner->sync.request_delay != owner->sync.delay)<br>+ if (owner->gapless_state == GAPLESS_STATE_NONE<br>+ && owner->sync.request_delay != owner->sync.delay)<br> {<br> owner->sync.delay = owner->sync.request_delay;<br> vlc_tick_t delta = vlc_clock_SetDelay(owner->sync.clock, owner->sync.delay);<br>@@ -486,7 +642,12 @@ int aout_DecPlay(audio_output_t *aout, block_t *block)<br> }<br> <br> vlc_tick_t system_now = vlc_tick_now();<br>- aout_Play(aout, block, system_now, original_pts);<br>+<br>+ if (owner->gapless_state == GAPLESS_STATE_STARTED)<br>+ aout_GaplessPlay(aout, block, system_now, original_pts);<br>+ else<br>+ aout_Play(aout, block, system_now, original_pts);<br>+<br> return ret;<br> drop:<br> owner->sync.discontinuity = true;<br>@@ -540,6 +701,15 @@ void aout_DecFlush(audio_output_t *aout)<br> <br> if (owner->mixer_format.i_format)<br> {<br>+ if (owner->gapless_state != GAPLESS_STATE_NONE)<br>+ {<br>+ /* Flush gapless buffers and reset state */<br>+ block_ChainRelease(owner->gapless_block_chain);<br>+ owner->gapless_block_chain = NULL;<br>+ owner->gapless_block_last = &owner->gapless_block_chain;<br>+ owner->gapless_state = GAPLESS_STATE_NONE;<br>+ }<br>+<br> aout_FiltersFlush (owner->filters);<br> <br> aout->flush(aout);<br>@@ -571,11 +741,31 @@ void aout_DecDrain(audio_output_t *aout)<br> if (!owner->mixer_format.i_format)<br> return;<br> <br>+ assert(owner->gapless_state == GAPLESS_STATE_NONE<br>+ || owner->gapless_state == GAPLESS_STATE_STARTED);<br>+<br>+ if (owner->gapless_state == GAPLESS_STATE_STARTED)<br>+ {<br>+ /* Drain gapless block chain */<br>+ aout_GaplessPlay(aout, NULL, vlc_tick_now(), VLC_TICK_INVALID);<br>+ }<br>+<br> block_t *block = aout_FiltersDrain (owner->filters);<br> if (block)<br> aout->play(aout, block, vlc_tick_now());<br> <br>- aout_Drain(aout);<br>+ if (AOUT_FMT_LINEAR(&owner->mixer_format)<br>+ && aout_OutputIsGaplessEnabled(aout))<br>+ {<br>+<br>+ /* First gapless state, cancellable if stop/play/flush is not called<br>+ * right after */<br>+ owner->gapless_state = GAPLESS_STATE_DRAINED;<br>+ owner->gapless_block_chain = NULL;<br>+ owner->gapless_block_last = &owner->gapless_block_chain;<br>+ }<br>+ else<br>+ aout_Drain(aout);<br> <br> vlc_clock_Reset(owner->sync.clock);<br> aout_FiltersResetClock(owner->filters);<br>diff --git a/src/audio_output/output.c b/src/audio_output/output.c<br>index 5c378dbb31..6f11628a52 100644<br>--- a/src/audio_output/output.c<br>+++ b/src/audio_output/output.c<br>@@ -223,6 +223,9 @@ audio_output_t *aout_New (vlc_object_t *parent)<br> vlc_viewpoint_init (&owner->vp.value);<br> atomic_init (&owner->vp.update, false);<br> atomic_init(&owner->refs, 0);<br>+ owner->stream_saved = false;<br>+ owner->gapless_enabled = false;<br>+ owner->gapless_state = GAPLESS_STATE_NONE;<br> <br> /* Audio output module callbacks */<br> var_Create (aout, "volume", VLC_VAR_FLOAT);<br>@@ -565,6 +568,7 @@ int aout_OutputNew (audio_output_t *aout, audio_sample_format_t *restrict fmt,<br> aout->current_sink_info.headphones = false;<br> <br> vlc_mutex_lock(&owner->lock);<br>+ assert(!owner->stream_saved);<br> int ret = aout->start(aout, fmt);<br> assert(aout->flush && aout->play && aout->time_get && aout->pause);<br> vlc_mutex_unlock(&owner->lock);<br>@@ -592,10 +596,80 @@ void aout_OutputDelete (audio_output_t *aout)<br> {<br> aout_owner_t *owner = aout_owner(aout);<br> vlc_mutex_lock(&owner->lock);<br>+ assert(!owner->stream_saved);<br> aout->stop (aout);<br> vlc_mutex_unlock(&owner->lock);<br> }<br> <br>+/**<br>+ * Recover the ownership of the stream previously saved<br>+ */<br>+int aout_OutputRecoverStream(audio_output_t *aout)<br>+{<br>+ aout_owner_t *owner = aout_owner(aout);<br>+ vlc_mutex_lock(&owner->lock);<br>+ if (unlikely(!owner->stream_saved))<br>+ {<br>+ /* The stream was already stopped by a previous<br>+ * aout_OutputSetGaplessEnabled(false) from an other thread */<br>+ vlc_mutex_unlock(&owner->lock);<br>+ return VLC_EGENERIC;<br>+ }<br>+<br>+ owner->stream_saved = false;<br>+ vlc_mutex_unlock(&owner->lock);<br>+<br>+ return VLC_SUCCESS;<br>+}<br>+<br>+/**<br>+ * Save the stream for a future usage<br>+ */<br>+void aout_OutputSaveStream(audio_output_t *aout)<br>+{<br>+ aout_owner_t *owner = aout_owner(aout);<br>+<br>+ vlc_mutex_lock(&owner->lock);<br>+<br>+ assert(!owner->stream_saved);<br>+<br>+ /* The caller of aout_OutputSetGaplessEnabled (the player) is now the owner<br>+ * of the stream during gapless transition. */<br>+ owner->stream_saved = true;<br>+ vlc_mutex_unlock(&owner->lock);<br>+}<br>+<br>+/**<br>+ * Enable or disable gapless<br>+ *<br>+ * Can be called by the player when the next media changes<br>+ */<br>+void aout_OutputSetGaplessEnabled(audio_output_t *aout, bool enabled)<br>+{<br>+ aout_owner_t *owner = aout_owner (aout);<br>+<br>+ vlc_mutex_lock(&owner->lock);<br>+<br>+ owner->gapless_enabled = enabled;<br>+<br>+ if (!enabled && unlikely(owner->stream_saved))<br>+ {<br>+ aout->stop (aout);<br>+ owner->stream_saved = false;<br>+ }<br>+<br>+ vlc_mutex_unlock(&owner->lock);<br>+}<br>+<br>+bool aout_OutputIsGaplessEnabled(audio_output_t *aout)<br>+{<br>+ aout_owner_t *owner = aout_owner(aout);<br>+ vlc_mutex_lock(&owner->lock);<br>+ const bool enabled = owner->gapless_enabled;<br>+ vlc_mutex_unlock(&owner->lock);<br>+ return enabled;<br>+}<br>+<br> /**<br> * Gets the volume of the audio output stream (independent of mute).<br> * \return Current audio volume (0. = silent, 1. = nominal),</pre></blockquote></div><br>-- <br>Envoyé de mon appareil Android avec Courriel K-9 Mail. Veuillez excuser ma brièveté.</body></html>