Bug 1412737. P4 - wake up readers only when we have blocks committed to the cache.
Per P2 changes, a reader will always read data from the cache or from the last
block in the memory. NotifyDataReceived() will be slightly more efficient
if we don't wake up readers unnecessarily when there are no new blocks committed
to the cache.
MozReview-Commit-ID: 3UWHbvtEUmu
--- a/dom/media/MediaCache.cpp
+++ b/dom/media/MediaCache.cpp
@@ -2013,16 +2013,19 @@ MediaCacheStream::NotifyDataReceived(uin
if (mLoadID != aLoadID) {
// mChannelOffset is updated to a new position when loading a new channel.
// We should discard the data coming from the old channel so it won't be
// stored to the wrong positoin.
return;
}
+ // True if we commit any blocks to the cache.
+ bool cacheUpdated = false;
+
auto source = MakeSpan<const uint8_t>(aData, aCount);
// We process the data one block (or part of a block) at a time
while (!source.IsEmpty()) {
// The data we've collected so far in the partial block.
auto partial = MakeSpan<const uint8_t>(mPartialBlockBuffer.get(),
OffsetInBlock(mChannelOffset));
@@ -2040,16 +2043,17 @@ MediaCacheStream::NotifyDataReceived(uin
mMediaCache->AllocateAndWriteBlock(
this,
OffsetToBlockIndexUnchecked(mChannelOffset),
mMetadataInPartialBlockBuffer ? MODE_METADATA : MODE_PLAYBACK,
partial,
source.First(remaining));
source = source.From(remaining);
mChannelOffset += remaining;
+ cacheUpdated = true;
} else {
// The buffer to be filled in the partial block.
auto buf = MakeSpan<uint8_t>(mPartialBlockBuffer.get() + partial.Length(),
remaining);
memcpy(buf.Elements(), source.Elements(), source.Length());
mChannelOffset += source.Length();
break;
}
@@ -2059,20 +2063,22 @@ MediaCacheStream::NotifyDataReceived(uin
while (MediaCacheStream* stream = iter.Next()) {
if (stream->mStreamLength >= 0) {
// The stream is at least as long as what we've read
stream->mStreamLength = std::max(stream->mStreamLength, mChannelOffset);
}
stream->mClient->CacheClientNotifyDataReceived();
}
- // Notify in case there's a waiting reader
// XXX it would be fairly easy to optimize things a lot more to
// avoid waking up reader threads unnecessarily
- mon.NotifyAll();
+ if (cacheUpdated) {
+ // Wake up the reader who is waiting for the committed blocks.
+ mon.NotifyAll();
+ }
}
void
MediaCacheStream::FlushPartialBlockInternal(bool aNotifyAll,
ReentrantMonitorAutoEnter& aReentrantMonitor)
{
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");