Bug 1371882 - static MediaCache::GetMediaCache to get file-backed MediaCache - r=cpearce draft
authorGerald Squelart <gsquelart@mozilla.com>
Thu, 08 Jun 2017 15:09:40 +1200
changeset 595147 0b3e6fae71207076812b5cb9172d4497d3e68ea2
parent 595146 d69d52c68512041dd61c5782e3406aa66a487a28
child 595148 d1116e03d4bbef1c6bd1a6be676eb89605a78494
push id64265
push usergsquelart@mozilla.com
push dateFri, 16 Jun 2017 03:37:56 +0000
reviewerscpearce
bugs1371882
milestone56.0a1
Bug 1371882 - static MediaCache::GetMediaCache to get file-backed MediaCache - r=cpearce This is the new recommended way to create&initialize the file-backed MediaCache. In future patches, this will also allow the creation of memory-backed MediaCache objects. MozReview-Commit-ID: 6RUlNW2eBPP
dom/media/MediaCache.cpp
--- a/dom/media/MediaCache.cpp
+++ b/dom/media/MediaCache.cpp
@@ -161,16 +161,20 @@ public:
         this, unsigned(mBlockOwnersWatermark));
     Telemetry::Accumulate(
       Telemetry::HistogramID::MEDIACACHE_BLOCKOWNERS_WATERMARK,
       mBlockOwnersWatermark);
 
     MOZ_COUNT_DTOR(MediaCache);
   }
 
+  // Get an instance of the file-backed MediaCache.
+  // Returns nullptr if initialization failed.
+  static MediaCache* GetMediaCache();
+
   // 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();
   // Shut down the global cache if it's no longer needed. We shut down
   // the cache as soon as there are no streams. This means that during
   // normal operation we are likely to start up the cache and shut it down
   // many times, but that's OK since starting it up is cheap and
@@ -688,28 +692,32 @@ MediaCache::MaybeShutdown()
 
   // Since we're on the main thread, no-one is going to add a new stream
   // while we shut down.
   // This function is static so we don't have to delete 'this'.
   delete gMediaCache;
   gMediaCache = nullptr;
 }
 
-static void
-InitMediaCache()
+/* static */ MediaCache*
+MediaCache::GetMediaCache()
 {
-  if (gMediaCache)
-    return;
+  NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
+  if (gMediaCache) {
+    return gMediaCache;
+  }
 
   gMediaCache = new MediaCache();
   nsresult rv = gMediaCache->Init();
   if (NS_FAILED(rv)) {
     delete gMediaCache;
     gMediaCache = nullptr;
   }
+
+  return gMediaCache;
 }
 
 nsresult
 MediaCache::ReadCacheFile(
   int64_t aOffset, void* aData, int32_t aLength, int32_t* aBytes)
 {
   mReentrantMonitor.AssertCurrentThreadIn();
   RefPtr<FileBlockCache> fileCache = mFileCache;
@@ -2478,19 +2486,20 @@ MediaCacheStream::ReadFromCache(char* aB
 nsresult
 MediaCacheStream::Init()
 {
   NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
 
   if (mInitialized)
     return NS_OK;
 
-  InitMediaCache();
-  if (!gMediaCache)
+  MediaCache::GetMediaCache();
+  if (!gMediaCache) {
     return NS_ERROR_FAILURE;
+  }
   gMediaCache->OpenStream(this);
   mInitialized = true;
   return NS_OK;
 }
 
 nsresult
 MediaCacheStream::InitAsClone(MediaCacheStream* aOriginal)
 {