Bug 1096089 - Part 3: Add trimming support from beginning of source buffer. r=cajbir, a=lsblakk
authorJean-Yves Avenard <jyavenard@mozilla.com>
Tue, 24 Feb 2015 16:38:42 +1100
changeset 250103 4c92b1dcb67f
parent 250102 d461222b1a07
child 250104 dd1511c04aad
push id4502
push userryanvm@gmail.com
push date2015-02-27 21:16 +0000
treeherdermozilla-beta@9fb3cc1f7ff6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerscajbir, lsblakk
bugs1096089
milestone37.0
Bug 1096089 - Part 3: Add trimming support from beginning of source buffer. r=cajbir, a=lsblakk
dom/media/mediasource/TrackBuffer.cpp
--- a/dom/media/mediasource/TrackBuffer.cpp
+++ b/dom/media/mediasource/TrackBuffer.cpp
@@ -984,41 +984,62 @@ TrackBuffer::RangeRemoval(int64_t aStart
   nsRefPtr<dom::TimeRanges> buffered = new dom::TimeRanges();
   int64_t bufferedEnd = Buffered(buffered) * USECS_PER_S;
   int64_t bufferedStart = buffered->GetStartTime() * USECS_PER_S;
 
   if (bufferedStart < 0 || aStart > bufferedEnd || aEnd < bufferedStart) {
     // Nothing to remove.
     return false;
   }
-  if (aEnd < bufferedEnd) {
-    // TODO. We only handle trimming.
+
+  if (aStart > bufferedStart && aEnd < bufferedEnd) {
+    // TODO. We only handle trimming and removal from the start.
     NS_WARNING("RangeRemoval unsupported arguments. "
-               "Can only handle trimming");
+               "Can only handle trimming (trim left or trim right");
     return false;
   }
 
   nsTArray<SourceBufferDecoder*> decoders;
   decoders.AppendElements(mInitializedDecoders);
 
-  // Only trimming existing buffers.
-  for (size_t i = 0; i < decoders.Length(); ++i) {
-    decoders[i]->Trim(aStart);
-    if (aStart <= buffered->GetStartTime()) {
-      // We've completely emptied it, can clear the data.
-      int64_t size = decoders[i]->GetResource()->GetSize();
-      decoders[i]->GetResource()->EvictData(size, size);
-      if (decoders[i] == mCurrentDecoder ||
-          mParentDecoder->IsActiveReader(decoders[i]->GetReader())) {
-        continue;
+  if (aStart <= bufferedStart && aEnd < bufferedEnd) {
+    // Evict data from beginning.
+    for (size_t i = 0; i < decoders.Length(); ++i) {
+      nsRefPtr<dom::TimeRanges> buffered = new dom::TimeRanges();
+      decoders[i]->GetBuffered(buffered);
+      if (buffered->GetEndTime() < aEnd) {
+        // Can be fully removed.
+        MSE_DEBUG("remove all bufferedEnd=%f time=%f, size=%lld",
+                  buffered->GetEndTime(), time,
+                  decoders[i]->GetResource()->GetSize());
+        decoders[i]->GetResource()->EvictAll();
+      } else {
+        int64_t offset = decoders[i]->ConvertToByteOffset(aEnd);
+        MSE_DEBUG("removing some bufferedEnd=%f offset=%lld size=%lld",
+                  buffered->GetEndTime(), offset,
+                  decoders[i]->GetResource()->GetSize());
+        if (offset > 0) {
+          decoders[i]->GetResource()->EvictData(offset, offset);
+        }
       }
-      MSE_DEBUG("remove empty decoders=%d", i);
-      RemoveDecoder(decoders[i]);
+    }
+  } else {
+    // Only trimming existing buffers.
+    for (size_t i = 0; i < decoders.Length(); ++i) {
+      if (aStart <= buffered->GetStartTime()) {
+        // It will be entirely emptied, can clear all data.
+        decoders[i]->GetResource()->EvictAll();
+      } else {
+        decoders[i]->Trim(aStart);
+      }
     }
   }
+
+  RemoveEmptyDecoders(decoders);
+
   return true;
 }
 
 void
 TrackBuffer::AdjustDecodersTimestampOffset(int32_t aOffset)
 {
   ReentrantMonitorAutoEnter mon(mParentDecoder->GetReentrantMonitor());
   for (uint32_t i = 0; i < mDecoders.Length(); i++) {