Bug 1371882 - Let GetMediaCache decide which block cache to use - r?cpearce draft
authorGerald Squelart <gsquelart@mozilla.com>
Thu, 15 Jun 2017 16:32:06 +1200
changeset 595168 6da7c3e9504cd58c6d3afc656ae58b9ab445db5e
parent 595167 38054b35434d5f77a17fe4273ef626228486e117
child 595169 2994a8575637e5fb4c894221360c7868a5f1f9d1
push id64265
push usergsquelart@mozilla.com
push dateFri, 16 Jun 2017 03:37:56 +0000
reviewerscpearce
bugs1371882
milestone56.0a1
Bug 1371882 - Let GetMediaCache decide which block cache to use - r?cpearce This allows a fallback to the file-backed MediaCache, if a MemoryBlockCache could not be created and initialized (which may happen in the next patch, where MemoryBlockCache will take care of not using more than MediaMemoryCachesCombinedLimit). MediaCache::Init() is not needed anymore, as its only work was to initialize its block cache. MozReview-Commit-ID: ItAdOPuxEvt
dom/media/MediaCache.cpp
--- a/dom/media/MediaCache.cpp
+++ b/dom/media/MediaCache.cpp
@@ -247,20 +247,21 @@ public:
     }
   private:
     MediaCache* mMediaCache;
     int64_t  mResourceID;
     uint32_t mNext;
   };
 
 protected:
-  explicit MediaCache(int64_t aContentLength)
+  explicit MediaCache(int64_t aContentLength, MediaBlockCacheBase* aCache)
     : mContentLength(aContentLength)
     , mNextResourceID(1)
     , mReentrantMonitor("MediaCache.mReentrantMonitor")
+    , mBlockCache(aCache)
     , mUpdateQueued(false)
 #ifdef DEBUG
     , mInUpdate(false)
 #endif
   {
     NS_ASSERTION(NS_IsMainThread(), "Only construct MediaCache on main thread");
     MOZ_COUNT_CTOR(MediaCache);
     MediaCacheFlusher::RegisterMediaCache(this);
@@ -301,21 +302,16 @@ protected:
       Telemetry::Accumulate(
         Telemetry::HistogramID::MEDIACACHE_BLOCKOWNERS_WATERMARK,
         mBlockOwnersWatermark);
     }
 
     MOZ_COUNT_DTOR(MediaCache);
   }
 
-  // Main thread only. Creates the backing cache file. If this fails,
-  // then the cache is still in a semi-valid state; mFD will be null,
-  // so all I/O on the cache file will fail.
-  nsresult Init();
-
   // Find a free or reusable block and return its index. If there are no
   // free blocks and no reusable blocks, add a new block to the cache
   // and return it. Can return -1 on OOM.
   int32_t FindBlockForIncomingData(TimeStamp aNow, MediaCacheStream* aStream);
   // Find a reusable block --- a free block, if there is one, otherwise
   // the reusable block with the latest predicted-next-use, or -1 if
   // there aren't any freeable blocks. Only block indices less than
   // aMaxSearchBlockIndex are considered. If aForStream is non-null,
@@ -669,35 +665,16 @@ MediaCacheStream::BlockList::NotifyBlock
   }
   if (e2) {
     e2 = mEntries.PutEntry(aBlockIndex1);
     e2->mNextBlock = e2Next;
     e2->mPrevBlock = e2Prev;
   }
 }
 
-nsresult
-MediaCache::Init()
-{
-  NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
-  NS_ASSERTION(!mBlockCache, "Block cache already open?");
-
-  if (mContentLength <= 0) {
-    // The global MediaCache uses a file-backed storage for its resource blocks.
-    mBlockCache = new FileBlockCache();
-  } else {
-    // Non-global MediaCaches keep the whole resource in memory.
-    mBlockCache = new MemoryBlockCache(mContentLength);
-  }
-  nsresult rv = mBlockCache->Init();
-  NS_ENSURE_SUCCESS(rv,rv);
-
-  return NS_OK;
-}
-
 void
 MediaCache::Flush()
 {
   NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
   ReentrantMonitorAutoEnter mon(mReentrantMonitor);
 
   for (uint32_t blockIndex = 0; blockIndex < mIndex.Length(); ++blockIndex) {
     FreeBlock(blockIndex);
@@ -731,45 +708,46 @@ MediaCache::GetMediaCache(int64_t aConte
       aContentLength <=
         int64_t(MediaPrefs::MediaMemoryCacheMaxSize()) * 1024 &&
       size_t(gMediaMemoryCachesCombinedSize + aContentLength) <=
         std::min(
         size_t(MediaPrefs::MediaMemoryCachesCombinedLimitKb()) * 1024,
         sysmem * MediaPrefs::MediaMemoryCachesCombinedLimitPcSysmem() / 100)) {
     // Small-enough resource (and we are under the maximum memory usage), use
     // a new memory-backed MediaCache.
-    RefPtr<MediaCache> mc = new MediaCache(aContentLength);
-    nsresult rv = mc->Init();
+    RefPtr<MediaBlockCacheBase> bc = new MemoryBlockCache(aContentLength);
+    nsresult rv = bc->Init();
     if (NS_SUCCEEDED(rv)) {
+      RefPtr<MediaCache> mc = new MediaCache(aContentLength, bc);
       gMediaMemoryCachesCombinedSize += aContentLength;
       LOG("GetMediaCache(%" PRIi64
           ") -> Memory MediaCache %p, combined size %" PRIi64,
           aContentLength,
           mc.get(),
           gMediaMemoryCachesCombinedSize);
       return mc;
     }
-    // Memory-backed MediaCache initialization failed, clean up and try for a
+    // MemoryBlockCache initialization failed, clean up and try for a
     // file-backed MediaCache below.
   }
 
   if (gMediaCache) {
     LOG("GetMediaCache(%" PRIi64 ") -> Existing file-backed MediaCache",
         aContentLength);
     return gMediaCache;
   }
 
-  gMediaCache = new MediaCache(-1);
-  nsresult rv = gMediaCache->Init();
-  if (NS_FAILED(rv)) {
-    gMediaCache = nullptr;
-    LOG("GetMediaCache(%" PRIi64 ") -> Failed to create file-backed MediaCache",
+  RefPtr<MediaBlockCacheBase> bc = new FileBlockCache();
+  nsresult rv = bc->Init();
+  if (NS_SUCCEEDED(rv)) {
+    gMediaCache = new MediaCache(-1, bc);
+    LOG("GetMediaCache(%" PRIi64 ") -> Created file-backed MediaCache",
         aContentLength);
   } else {
-    LOG("GetMediaCache(%" PRIi64 ") -> Created file-backed MediaCache",
+    LOG("GetMediaCache(%" PRIi64 ") -> Failed to create file-backed MediaCache",
         aContentLength);
   }
 
   return gMediaCache;
 }
 
 nsresult
 MediaCache::ReadCacheFile(