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
--- 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(