Bug 1371882 - If content length <= 'media.memory_cache_max_size', use a discrete memory-backed MediaCache - r=cpearce draft
authorGerald Squelart <gsquelart@mozilla.com>
Fri, 09 Jun 2017 23:15:04 +1200
changeset 595156 6b975af79ceafe876e3ea780a6d3a1ce7d319281
parent 595155 cad3880119afe6dee6e2478a1cdc8c8998815ea4
child 595157 da8d1ce70dea0e49c9f145fdb5134814194887a1
push id64265
push usergsquelart@mozilla.com
push dateFri, 16 Jun 2017 03:37:56 +0000
reviewerscpearce
bugs1371882
milestone56.0a1
Bug 1371882 - If content length <= 'media.memory_cache_max_size', use a discrete memory-backed MediaCache - r=cpearce MozReview-Commit-ID: HH1R6LjKkxb
dom/media/MediaCache.cpp
dom/media/MediaPrefs.h
modules/libpref/init/all.js
--- a/dom/media/MediaCache.cpp
+++ b/dom/media/MediaCache.cpp
@@ -737,16 +737,30 @@ MediaCache::ShutdownAndDestroyThis()
 
   delete this;
 }
 
 /* static */ MediaCache*
 MediaCache::GetMediaCache(int64_t aContentLength)
 {
   NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
+  if (aContentLength > 0 &&
+      aContentLength <=
+        int64_t(MediaPrefs::MediaMemoryCacheMaxSize()) * 1024) {
+    // Small-enough resource, use a new memory-backed MediaCache.
+    MediaCache* mc = new MediaCache();
+    nsresult rv = mc->Init();
+    if (NS_SUCCEEDED(rv)) {
+      return mc;
+    }
+    // Memory-backed MediaCache initialization failed, clean up and try for a
+    // file-backed MediaCache below.
+    delete mc;
+  }
+
   if (gMediaCache) {
     return gMediaCache;
   }
 
   gMediaCache = new MediaCache();
   nsresult rv = gMediaCache->Init();
   if (NS_FAILED(rv)) {
     delete gMediaCache;
--- a/dom/media/MediaPrefs.h
+++ b/dom/media/MediaPrefs.h
@@ -82,16 +82,17 @@ private:
       AssertMainThread();
       PrefAddVarCache(&mValue, aPreference, mValue);
     }
   };
 
   // This is where DECL_MEDIA_PREF for each of the preferences should go.
 
   // Cache sizes.
+  DECL_MEDIA_PREF("media.memory_cache_max_size",              MediaMemoryCacheMaxSize, uint32_t, 8192);
   DECL_MEDIA_PREF("media.cache.resource-index",               MediaResourceIndexCache, uint32_t, 8192);
 
   // AudioSink
   DECL_MEDIA_PREF("accessibility.monoaudio.enable",           MonoAudio, bool, false);
   DECL_MEDIA_PREF("media.resampling.enabled",                 AudioSinkResampling, bool, false);
   DECL_MEDIA_PREF("media.resampling.rate",                    AudioSinkResampleRate, uint32_t, 48000);
 #if defined(XP_WIN) || defined(XP_DARWIN) || defined(MOZ_PULSEAUDIO)
   // libcubeb backend implement .get_preferred_channel_layout
--- a/modules/libpref/init/all.js
+++ b/modules/libpref/init/all.js
@@ -312,25 +312,29 @@ pref("print.use_simplify_page", false);
 // Disable support for MathML
 pref("mathml.disabled",    false);
 
 // Enable scale transform for stretchy MathML operators. See bug 414277.
 pref("mathml.scale_stretchy_operators.enabled", true);
 
 pref("media.dormant-on-pause-timeout-ms", 5000);
 
-// Media cache size in kilobytes
+// File-backed MediaCache size in kilobytes
 pref("media.cache_size", 512000);
 // When a network connection is suspended, don't resume it until the
 // amount of buffered data falls below this threshold (in seconds).
 pref("media.cache_resume_threshold", 30);
 // Stop reading ahead when our buffered data is this many seconds ahead
 // of the current playback position. This limit can stop us from using arbitrary
 // amounts of network bandwidth prefetching huge videos.
 pref("media.cache_readahead_limit", 60);
+// If a resource is known to be smaller than this size (in kilobytes), a
+// memory-backed MediaCache may be used; otherwise the (single shared
+// global) file-backed MediaCache is used.
+pref("media.memory_cache_max_size", 8192);
 
 // Cache size hint (in bytes) for each MediaResourceIndex.
 // 0 -> no cache. Will use next power of 2, clamped to 32B-128KB.
 pref("media.cache.resource-index", 8192);
 
 // We'll throttle the download if the download rate is throttle-factor times
 // the estimated playback rate, AND we satisfy the cache readahead_limit
 // above. The estimated playback rate is time_duration/length_in_bytes.