Bug 1392178 - move MediaResource::CachedReadAt to MediaResourceIndex. draft
authorJW Wang <jwwang@mozilla.com>
Mon, 21 Aug 2017 14:33:38 +0800
changeset 650291 73765ef7cc368d44b57889eab962cf75f35545ea
parent 650288 212104d817ed451336ae20ee5c2cc7da6a1ca59e
child 727357 d9953dd328639542976e4164ec42194bdc83a4e5
push id75336
push userjwwang@mozilla.com
push dateTue, 22 Aug 2017 06:09:50 +0000
bugs1392178
milestone57.0a1
Bug 1392178 - move MediaResource::CachedReadAt to MediaResourceIndex. MozReview-Commit-ID: LnAXXaRQ4C
dom/media/MediaResource.h
dom/media/webm/WebMBufferedParser.cpp
--- a/dom/media/MediaResource.h
+++ b/dom/media/MediaResource.h
@@ -171,27 +171,16 @@ public:
   virtual nsresult ReadAt(int64_t aOffset, char* aBuffer,
                           uint32_t aCount, uint32_t* aBytes) = 0;
   // Indicate whether caching data in advance of reads is worth it.
   // E.g. Caching lockless and memory-based MediaResource subclasses would be a
   // waste, but caching lock/IO-bound resources means reducing the impact of
   // each read.
   virtual bool ShouldCacheReads() = 0;
 
-  already_AddRefed<MediaByteBuffer> CachedReadAt(int64_t aOffset, uint32_t aCount)
-  {
-    RefPtr<MediaByteBuffer> bytes = new MediaByteBuffer();
-    bool ok = bytes->SetLength(aCount, fallible);
-    NS_ENSURE_TRUE(ok, nullptr);
-    char* curr = reinterpret_cast<char*>(bytes->Elements());
-    nsresult rv = ReadFromCache(curr, aOffset, aCount);
-    NS_ENSURE_SUCCESS(rv, nullptr);
-    return bytes.forget();
-  }
-
   // Report the current offset in bytes from the start of the stream.
   // This is used to approximate where we currently are in the playback of a
   // media.
   // A call to ReadAt will update this position.
   virtual int64_t Tell() = 0;
 
   // These can be called on any thread.
   // Cached blocks associated with this stream will not be evicted
@@ -773,16 +762,29 @@ public:
         break;
       }
       aCount -= bytesRead;
       curr += bytesRead;
     }
     bytes->SetLength(curr - start);
     return bytes.forget();
   }
+
+  already_AddRefed<MediaByteBuffer> CachedMediaReadAt(int64_t aOffset,
+                                                      uint32_t aCount) const
+  {
+    RefPtr<MediaByteBuffer> bytes = new MediaByteBuffer();
+    bool ok = bytes->SetLength(aCount, fallible);
+    NS_ENSURE_TRUE(ok, nullptr);
+    char* curr = reinterpret_cast<char*>(bytes->Elements());
+    nsresult rv = mResource->ReadFromCache(curr, aOffset, aCount);
+    NS_ENSURE_SUCCESS(rv, nullptr);
+    return bytes.forget();
+  }
+
   // Get the length of the stream in bytes. Returns -1 if not known.
   // This can change over time; after a seek operation, a misbehaving
   // server may give us a resource of a different length to what it had
   // reported previously --- or it may just lie in its Content-Length
   // header and give us more or less data than it reported. We will adjust
   // the result of GetLength to reflect the data that's actually arriving.
   int64_t GetLength() const { return mResource->GetLength(); }
 
--- a/dom/media/webm/WebMBufferedParser.cpp
+++ b/dom/media/webm/WebMBufferedParser.cpp
@@ -441,20 +441,22 @@ void WebMBufferedState::UpdateIndex(cons
         length -= uint32_t(adjust);
       } else {
         mRangeParsers.InsertElementAt(idx, WebMBufferedParser(offset));
         if (idx) {
           mRangeParsers[idx].SetTimecodeScale(mRangeParsers[0].GetTimecodeScale());
         }
       }
     }
+
+    MediaResourceIndex res(aResource);
     while (length > 0) {
       static const uint32_t BLOCK_SIZE = 1048576;
       uint32_t block = std::min(length, BLOCK_SIZE);
-      RefPtr<MediaByteBuffer> bytes = aResource->CachedReadAt(offset, block);
+      RefPtr<MediaByteBuffer> bytes = res.CachedMediaReadAt(offset, block);
       if (!bytes) {
         break;
       }
       NotifyDataArrived(bytes->Elements(), bytes->Length(), offset);
       length -= bytes->Length();
       offset += bytes->Length();
     }
   }