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
--- 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)
{