Bug 1369322 - MediaResource::ShouldCacheReads to indicate usefulness of caching - r=cpearce
authorGerald Squelart <gsquelart@mozilla.com>
Fri, 02 Jun 2017 14:27:17 +1200
changeset 362454 d6fa3a538265ee491e491c0edc76cd3e568a9b51
parent 362453 0e956bdfab8b666a4a6ec3d05b194c8528ac527e
child 362455 26f5db4ebfaf83230fe961913789213c8b3d2985
push id31977
push userarchaeopteryx@coole-files.de
push dateTue, 06 Jun 2017 09:17:27 +0000
treeherdermozilla-central@d3b8e8571020 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerscpearce
bugs1369322
milestone55.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1369322 - MediaResource::ShouldCacheReads to indicate usefulness of caching - r=cpearce This hint will inform readers if caching is discouraged (e.g., for SourceBufferResource) or recommmended (e.g., for MediaCache-backed ChannelMediaResource). MozReview-Commit-ID: 7hopNS0s5tE
dom/media/BufferMediaResource.h
dom/media/MediaResource.cpp
dom/media/MediaResource.h
dom/media/gtest/MockMediaResource.h
dom/media/mediasource/MediaSourceResource.h
dom/media/mediasource/SourceBufferResource.h
--- a/dom/media/BufferMediaResource.h
+++ b/dom/media/BufferMediaResource.h
@@ -63,16 +63,19 @@ private:
     if (aOffset < 0 || aOffset > mLength) {
       return NS_ERROR_FAILURE;
     }
     *aBytes = std::min(mLength - static_cast<uint32_t>(aOffset), aCount);
     memcpy(aBuffer, mBuffer + aOffset, *aBytes);
     mOffset = aOffset + *aBytes;
     return NS_OK;
   }
+  // Memory-based and no locks, caching discouraged.
+  bool ShouldCacheReads() override { return false; }
+
   int64_t Tell() override { return mOffset; }
 
   void Pin() override {}
   void Unpin() override {}
   double GetDownloadRate(bool* aIsReliable) override { *aIsReliable = false; return 0.; }
   int64_t GetLength() override { return mLength; }
   int64_t GetNextCachedData(int64_t aOffset) override { return aOffset; }
   int64_t GetCachedDataEnd(int64_t aOffset) override
--- a/dom/media/MediaResource.cpp
+++ b/dom/media/MediaResource.cpp
@@ -1172,16 +1172,18 @@ public:
 
   // These methods are called off the main thread.
 
   // Other thread
   void     SetReadMode(MediaCacheStream::ReadMode aMode) override {}
   void     SetPlaybackRate(uint32_t aBytesPerSecond) override {}
   nsresult ReadAt(int64_t aOffset, char* aBuffer,
                   uint32_t aCount, uint32_t* aBytes) override;
+  // (Probably) file-based, caching recommended.
+  bool ShouldCacheReads() override { return true; }
   already_AddRefed<MediaByteBuffer> MediaReadAt(int64_t aOffset, uint32_t aCount) override;
   int64_t  Tell() override;
 
   // Any thread
   void    Pin() override {}
   void    Unpin() override {}
   double  GetDownloadRate(bool* aIsReliable) override
   {
--- a/dom/media/MediaResource.h
+++ b/dom/media/MediaResource.h
@@ -196,16 +196,21 @@ public:
   // because it doesn't know when the decoder was paused, buffering, etc.
   virtual void SetPlaybackRate(uint32_t aBytesPerSecond) = 0;
   // Read up to aCount bytes from the stream. The read starts at
   // aOffset in the stream, seeking to that location initially if
   // it is not the current stream offset. The remaining arguments,
   // results and requirements are the same as per the Read method.
   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;
   // This method returns nullptr if anything fails.
   // Otherwise, it returns an owned buffer.
   // MediaReadAt may return fewer bytes than requested if end of stream is
   // encountered. There is no need to call it again to get more data.
   virtual already_AddRefed<MediaByteBuffer> MediaReadAt(int64_t aOffset, uint32_t aCount)
   {
     RefPtr<MediaByteBuffer> bytes = new MediaByteBuffer();
     bool ok = bytes->SetLength(aCount, fallible);
@@ -573,16 +578,18 @@ public:
   nsresult ReadFromCache(char* aBuffer, int64_t aOffset, uint32_t aCount) override;
   void     EnsureCacheUpToDate() override;
 
   // Other thread
   void     SetReadMode(MediaCacheStream::ReadMode aMode) override;
   void     SetPlaybackRate(uint32_t aBytesPerSecond) override;
   nsresult ReadAt(int64_t offset, char* aBuffer,
                   uint32_t aCount, uint32_t* aBytes) override;
+  // Data stored in IO&lock-encumbered MediaCacheStream, caching recommended.
+  bool ShouldCacheReads() override { return true; }
   already_AddRefed<MediaByteBuffer> MediaReadAt(int64_t aOffset, uint32_t aCount) override;
   int64_t Tell() override;
 
   // Any thread
   void    Pin() override;
   void    Unpin() override;
   double  GetDownloadRate(bool* aIsReliable) override;
   int64_t GetLength() override;
--- a/dom/media/gtest/MockMediaResource.h
+++ b/dom/media/gtest/MockMediaResource.h
@@ -31,16 +31,18 @@ public:
     override
   {
     return nullptr;
   }
   void SetReadMode(MediaCacheStream::ReadMode aMode) override {}
   void SetPlaybackRate(uint32_t aBytesPerSecond) override {}
   nsresult ReadAt(int64_t aOffset, char* aBuffer, uint32_t aCount,
                   uint32_t* aBytes) override;
+  // Data stored in file, caching recommended.
+  bool ShouldCacheReads() override { return true; }
   int64_t Tell() override { return 0; }
   void Pin() override {}
   void Unpin() override {}
   double GetDownloadRate(bool* aIsReliable) override { return 0; }
   int64_t GetLength() override;
   int64_t GetNextCachedData(int64_t aOffset) override;
   int64_t GetCachedDataEnd(int64_t aOffset) override;
   bool IsDataCachedToEndOfResource(int64_t aOffset) override
--- a/dom/media/mediasource/MediaSourceResource.h
+++ b/dom/media/mediasource/MediaSourceResource.h
@@ -33,16 +33,17 @@ public:
   nsresult Close() override { return NS_OK; }
   void Suspend(bool aCloseImmediately) override { UNIMPLEMENTED(); }
   void Resume() override { UNIMPLEMENTED(); }
   bool CanClone() override { UNIMPLEMENTED(); return false; }
   already_AddRefed<MediaResource> CloneData(MediaResourceCallback*) override { UNIMPLEMENTED(); return nullptr; }
   void SetReadMode(MediaCacheStream::ReadMode aMode) override { UNIMPLEMENTED(); }
   void SetPlaybackRate(uint32_t aBytesPerSecond) override  { UNIMPLEMENTED(); }
   nsresult ReadAt(int64_t aOffset, char* aBuffer, uint32_t aCount, uint32_t* aBytes) override { UNIMPLEMENTED(); return NS_ERROR_FAILURE; }
+  bool ShouldCacheReads() override { UNIMPLEMENTED(); return false; }
   int64_t Tell() override { UNIMPLEMENTED(); return -1; }
   void Pin() override { UNIMPLEMENTED(); }
   void Unpin() override { UNIMPLEMENTED(); }
   double GetDownloadRate(bool* aIsReliable) override { UNIMPLEMENTED(); *aIsReliable = false; return 0; }
   int64_t GetLength() override { UNIMPLEMENTED(); return -1; }
   int64_t GetNextCachedData(int64_t aOffset) override { UNIMPLEMENTED(); return -1; }
   int64_t GetCachedDataEnd(int64_t aOffset) override { UNIMPLEMENTED(); return -1; }
   bool IsDataCachedToEndOfResource(int64_t aOffset) override { UNIMPLEMENTED(); return false; }
--- a/dom/media/mediasource/SourceBufferResource.h
+++ b/dom/media/mediasource/SourceBufferResource.h
@@ -56,16 +56,18 @@ public:
   {
     UNIMPLEMENTED();
   }
   void SetPlaybackRate(uint32_t aBytesPerSecond) override { UNIMPLEMENTED(); }
   nsresult ReadAt(int64_t aOffset,
                   char* aBuffer,
                   uint32_t aCount,
                   uint32_t* aBytes) override;
+  // Memory-based and no locks, caching discouraged.
+  bool ShouldCacheReads() override { return false; }
   int64_t Tell() override { return mOffset; }
   void Pin() override { UNIMPLEMENTED(); }
   void Unpin() override { UNIMPLEMENTED(); }
   double GetDownloadRate(bool* aIsReliable) override
   {
     UNIMPLEMENTED();
     *aIsReliable = false;
     return 0;