Bug 1280023: [MSE] P1. Never evict data in the media segment currently being played. r?gerald draft
authorJean-Yves Avenard <jyavenard@mozilla.com>
Thu, 08 Sep 2016 21:00:46 +1000
changeset 411663 f61c1fb7bf1e3884a5d44b5dccadcefb2340e637
parent 411608 e58570d948fab9cdc77afd75c7233342b2544299
child 411664 d624b6f069de38d975a2e7a220df3dc907e5419c
push id28946
push userbmo:jyavenard@mozilla.com
push dateThu, 08 Sep 2016 11:17:37 +0000
reviewersgerald
bugs1280023
milestone51.0a1
Bug 1280023: [MSE] P1. Never evict data in the media segment currently being played. r?gerald MozReview-Commit-ID: 3yCHYzdavWA
dom/media/mediasource/TrackBuffersManager.cpp
--- a/dom/media/mediasource/TrackBuffersManager.cpp
+++ b/dom/media/mediasource/TrackBuffersManager.cpp
@@ -453,21 +453,31 @@ TrackBuffersManager::DoEvictData(const T
   }
 
   if (mSizeSourceBuffer <= finalSize) {
     return;
   }
 
   toEvict = mSizeSourceBuffer - finalSize;
 
-  // Still some to remove. Remove data starting from the end, up to 30s ahead
-  // of the later of the playback time or the next sample to be demuxed.
-  // 30s is a value chosen as it appears to work with YouTube.
-  TimeUnit upperLimit =
-    std::max(aPlaybackTime, track.mNextSampleTime) + TimeUnit::FromSeconds(30);
+  // See if we can evict data into the future.
+  // We do not evict data from the currently used buffered interval.
+
+  TimeUnit currentPosition = std::max(aPlaybackTime, track.mNextSampleTime);
+  TimeIntervals futureBuffered(TimeInterval(currentPosition, TimeUnit::FromInfinity()));
+  futureBuffered.Intersection(track.mBufferedRanges);
+  futureBuffered.SetFuzz(MediaSourceDemuxer::EOS_FUZZ / 2);
+  if (futureBuffered.Length() <= 1) {
+    // We have one continuous segment ahead of us:
+    // nothing further can be evicted.
+    return;
+  }
+
+  // Don't evict before the end of the current segment
+  TimeUnit upperLimit = futureBuffered[0].mEnd;
   uint32_t evictedFramesStartIndex = buffer.Length();
   for (int32_t i = buffer.Length() - 1; i >= 0; i--) {
     const auto& frame = buffer[i];
     if (frame->mTime <= upperLimit.ToMicroseconds() || toEvict < 0) {
       // We've reached a frame that shouldn't be evicted -> Evict after it -> i+1.
       // Or the previous loop reached the eviction threshold -> Evict from it -> i+1.
       evictedFramesStartIndex = i + 1;
       break;