Bug 1364001. P1 - add an API to throttle download. r=cpearce

MozReview-Commit-ID: HdmAgvo1GE3

--HG--
extra : rebase_source : 97f0aa3e40c6c774ad7681ee2aae69393c47e741
extra : intermediate-source : 9f2e1751e19f88625d2a89dba0b1648f97d2478f
extra : source : b68fed514292e5e8c7815633052daeee24f64a95
This commit is contained in:
JW Wang 2017-05-11 16:49:36 +08:00
parent a9b339e353
commit c6c155a08e
4 changed files with 36 additions and 2 deletions

View File

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

View File

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

View File

@ -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();

View File

@ -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;