Bug 1491132 - Clamp cache reads to content size. r=gerald

Prior bug 1416085, reads were clamped to the content's duration (if known). It appears that the new code relied on MediaCacheReadBlockFromCache to properly account for the end of content.

However, this was not the case, the MediaCache always reads (and write) one full block at a time regardles of the size requested (a block is 32768 bytes).

Rather than clamping in the Read() method as it used to be, we clamp in ReadBlockFromCache as such safety will benefit other callers that would have otherwise also returned garbage reads.

Differential Revision: https://phabricator.services.mozilla.com/D5964

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Jean-Yves Avenard 2018-09-17 17:41:31 +00:00
parent dc5d097f0f
commit 8768e0b175

View File

@ -2619,7 +2619,8 @@ MediaCacheStream::ReadBlockFromCache(AutoLock& aLock,
// OffsetToBlockIndexUnchecked() is always non-negative.
uint32_t index = OffsetToBlockIndexUnchecked(aOffset);
int32_t cacheBlock = index < mBlocks.Length() ? mBlocks[index] : -1;
if (cacheBlock < 0) {
if (cacheBlock < 0 ||
(mStreamLength >= 0 && aOffset >= mStreamLength)) {
// Not in the cache.
return 0;
}
@ -2629,6 +2630,13 @@ MediaCacheStream::ReadBlockFromCache(AutoLock& aLock,
// BLOCK_SIZE bytes.
aBuffer = aBuffer.First(BLOCK_SIZE);
}
if (mStreamLength >= 0 &&
aBuffer.Length() > uint32_t(mStreamLength - aOffset)) {
// Clamp reads to stream's length
aBuffer = aBuffer.First(mStreamLength - aOffset);
}
// |BLOCK_SIZE - OffsetInBlock(aOffset)| <= BLOCK_SIZE
int32_t bytesToRead =
std::min<int32_t>(BLOCK_SIZE - OffsetInBlock(aOffset), aBuffer.Length());