diff --git a/dom/media/MediaCache.cpp b/dom/media/MediaCache.cpp index 4786741360b5..6233fca5597e 100644 --- a/dom/media/MediaCache.cpp +++ b/dom/media/MediaCache.cpp @@ -24,8 +24,10 @@ namespace mozilla { #undef LOG +#undef LOGI LazyLogModule gMediaCacheLog("MediaCache"); #define LOG(...) MOZ_LOG(gMediaCacheLog, LogLevel::Debug, (__VA_ARGS__)) +#define LOGI(...) MOZ_LOG(gMediaCacheLog, LogLevel::Info, (__VA_ARGS__)) // Readahead blocks for non-seekable streams will be limited to this @@ -1243,12 +1245,14 @@ MediaCache::Update() } else { TimeDuration predictedNewDataUse = PredictNextUseForIncomingData(stream); - if (stream->mCacheSuspended && + if (stream->mThrottleReadahead && + stream->mCacheSuspended && predictedNewDataUse.ToSeconds() > resumeThreshold) { // Don't need data for a while, so don't bother waking up the stream LOG("Stream %p avoiding wakeup since more data is not needed", stream); enableReading = false; - } else if (predictedNewDataUse.ToSeconds() > readaheadLimit) { + } else if (stream->mThrottleReadahead && + predictedNewDataUse.ToSeconds() > readaheadLimit) { // Don't read ahead more than this much LOG("Stream %p throttling to avoid reading ahead too far", stream); enableReading = false; @@ -2208,6 +2212,18 @@ MediaCacheStream::Seek(int32_t aWhence, int64_t aOffset) return NS_OK; } +void +MediaCacheStream::ThrottleReadahead(bool bThrottle) +{ + MOZ_ASSERT(NS_IsMainThread()); + if (mThrottleReadahead != bThrottle) { + LOGI("Stream %p ThrottleReadahead %d", this, bThrottle); + mThrottleReadahead = bThrottle; + ReentrantMonitorAutoEnter mon(gMediaCache->GetReentrantMonitor()); + gMediaCache->QueueUpdate(); + } +} + int64_t MediaCacheStream::Tell() { @@ -2490,3 +2506,4 @@ nsresult MediaCacheStream::GetCachedRanges(MediaByteRangeSet& aRanges) // avoid redefined macro in unified build #undef LOG +#undef LOGI diff --git a/dom/media/MediaCache.h b/dom/media/MediaCache.h index 821f1e4d5c3d..556a42e5af13 100644 --- a/dom/media/MediaCache.h +++ b/dom/media/MediaCache.h @@ -348,6 +348,8 @@ public: nsresult ReadAt(int64_t aOffset, char* aBuffer, uint32_t aCount, uint32_t* aBytes); + void ThrottleReadahead(bool bThrottle); + size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const; private: @@ -514,6 +516,8 @@ private: // True if associated with a private browsing window. const bool mIsPrivateBrowsing; + + bool mThrottleReadahead = false; }; } // namespace mozilla diff --git a/dom/media/MediaResource.cpp b/dom/media/MediaResource.cpp index 678f8ecb4c8e..b9151da54cb1 100644 --- a/dom/media/MediaResource.cpp +++ b/dom/media/MediaResource.cpp @@ -716,6 +716,12 @@ ChannelMediaResource::MediaReadAt(int64_t aOffset, uint32_t aCount) return bytes.forget(); } +void +ChannelMediaResource::ThrottleReadahead(bool bThrottle) +{ + mCacheStream.ThrottleReadahead(bThrottle); +} + int64_t ChannelMediaResource::Tell() { return mCacheStream.Tell(); diff --git a/dom/media/MediaResource.h b/dom/media/MediaResource.h index a604b59391ce..2dd486a8e5ad 100644 --- a/dom/media/MediaResource.h +++ b/dom/media/MediaResource.h @@ -253,6 +253,11 @@ public: return bytes.forget(); } + // Pass true to limit the amount of readahead data (specified by + // "media.cache_readahead_limit") or false to read as much as the + // cache size allows. + virtual void ThrottleReadahead(bool bThrottle) { } + // Report the current offset in bytes from the start of the stream. // This is used to approximate where we currently are in the playback of a // media. @@ -556,6 +561,8 @@ public: // Resume the current load since data is wanted again nsresult CacheClientResume(); + void ThrottleReadahead(bool bThrottle) override; + // Ensure that the media cache writes any data held in its partial block. // Called on the main thread. void FlushCache() override;