From f05b39009dcebcdbe0736e20325e951377a7d053 Mon Sep 17 00:00:00 2001 From: Sanborn Hilland Date: Wed, 29 Jun 2016 15:27:19 -0400 Subject: [PATCH] Attempt to fetch one chunk prior on seeking As per issue #330, manifests are only required to be approx. correct with respect to their description of the media segments. In order to avoid stalling when seeking through content like this, fetch the chunk prior to the one that supposedly contains the current time. This is an attempt to apply a similar fix to Ia7bb74d0120. --- lib/media/segment_index.js | 16 ++++++++++++++++ lib/media/stream.js | 6 +++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/media/segment_index.js b/lib/media/segment_index.js index 4a2acffd50..d49c6dcb5a 100644 --- a/lib/media/segment_index.js +++ b/lib/media/segment_index.js @@ -120,6 +120,22 @@ shaka.media.SegmentIndex.prototype.find = function(time) { }; +/** + * Finds a SegmentReference immediately prior to the requested time. + * + * This function can trigger an update, which may add or remove + * SegmentReferences. + * + * @param {number} time The time in seconds. + * @return {shaka.media.SegmentReference} The SegmentReference prior to the + * specified time, or null if no such SegmentReference exists. + */ +shaka.media.SegmentIndex.prototype.findBefore = function(time) { + var i = shaka.media.SegmentReference.find(this.references, time); + return i <= 0 ? null : this.references[i - 1]; +}; + + /** * Integrates |segmentIndex| into this SegmentIndex. "Integration" is * implementation dependent, but can be assumed to combine the two diff --git a/lib/media/stream.js b/lib/media/stream.js index d0055eaa67..dbe034470c 100644 --- a/lib/media/stream.js +++ b/lib/media/stream.js @@ -648,9 +648,13 @@ shaka.media.Stream.prototype.getNext_ = function( if (last != null) { return last.endTime != null ? segmentIndex.find(last.endTime) : null; } else { + // Try to get the segment prior to currentTime in order to gracefully handle + // cases where the manifest description of segment durations is different + // than the actual duration of segments as per #330. // Return the last SegmentReference if no segments have been inserted so // that we will always compute a timestamp correction and resolve started(). - return segmentIndex.find(currentTime) || + return segmentIndex.findBefore(currentTime) || + segmentIndex.find(currentTime) || (segmentIndex.length() ? segmentIndex.last() : null); } };