--- a/dom/media/MediaCache.cpp
+++ b/dom/media/MediaCache.cpp
@@ -18,18 +18,20 @@
#include "nsISeekableStream.h"
#include "nsIPrincipal.h"
#include "mozilla/Attributes.h"
#include "mozilla/Services.h"
#include <algorithm>
namespace mozilla {
+#undef LOG
LazyLogModule gMediaCacheLog("MediaCache");
-#define CACHE_LOG(type, msg) MOZ_LOG(gMediaCacheLog, type, msg)
+#define LOG(...) MOZ_LOG(gMediaCacheLog, LogLevel::Debug, (__VA_ARGS__))
+
// Readahead blocks for non-seekable streams will be limited to this
// fraction of the cache space. We don't normally evict such blocks
// because replacing them requires a seek, but we need to make sure
// they don't monopolize the cache.
static const double NONSEEKABLE_READAHEAD_MAX = 0.5;
// Data N seconds before the current playback position is given the same priority
@@ -943,17 +945,17 @@ MediaCache::FreeBlock(int32_t aBlock)
mReentrantMonitor.AssertCurrentThreadIn();
Block* block = &mIndex[aBlock];
if (block->mOwners.IsEmpty()) {
// already free
return;
}
- CACHE_LOG(LogLevel::Debug, ("Released block %d", aBlock));
+ LOG("Released block %d", aBlock);
for (uint32_t i = 0; i < block->mOwners.Length(); ++i) {
BlockOwner* bo = &block->mOwners[i];
GetListForBlock(bo)->RemoveBlock(aBlock);
bo->mStream->mBlocks[bo->mStreamBlock] = -1;
}
block->mOwners.Clear();
mFreeBlocks.AddFirstBlock(aBlock);
@@ -1109,30 +1111,31 @@ MediaCache::Update()
PredictNextUse(now, destinationBlockIndex) > latestPredictedUseForOverflow) {
// Reuse blocks in the main part of the cache that are less useful than
// the least useful overflow blocks
nsresult rv = mFileCache->MoveBlock(blockIndex, destinationBlockIndex);
if (NS_SUCCEEDED(rv)) {
// We successfully copied the file data.
- CACHE_LOG(LogLevel::Debug, ("Swapping blocks %d and %d (trimming cache)",
- blockIndex, destinationBlockIndex));
+ LOG("Swapping blocks %d and %d (trimming cache)",
+ blockIndex, destinationBlockIndex);
// Swapping the block metadata here lets us maintain the
// correct positions in the linked lists
SwapBlocks(blockIndex, destinationBlockIndex);
//Free the overflowing block even if the copy failed.
- CACHE_LOG(LogLevel::Debug, ("Released block %d (trimming cache)", blockIndex));
+ LOG("Released block %d (trimming cache)", blockIndex);
FreeBlock(blockIndex);
}
} else {
- CACHE_LOG(LogLevel::Debug, ("Could not trim cache block %d (destination %d, predicted next use %f, latest predicted use for overflow %f",
- blockIndex, destinationBlockIndex,
- PredictNextUse(now, destinationBlockIndex).ToSeconds(),
- latestPredictedUseForOverflow.ToSeconds()));
+ LOG("Could not trim cache block %d (destination %d, "
+ "predicted next use %f, latest predicted use for overflow %f",
+ blockIndex, destinationBlockIndex,
+ PredictNextUse(now, destinationBlockIndex).ToSeconds(),
+ latestPredictedUseForOverflow.ToSeconds());
}
}
// Try chopping back the array of cache entries and the cache file.
Truncate();
// Count the blocks allocated for readahead of non-seekable streams
// (these blocks can't be freed but we don't want them to monopolize the
// cache)
@@ -1157,17 +1160,17 @@ MediaCache::Update()
int32_t resumeThreshold = Preferences::GetInt("media.cache_resume_threshold", 10);
int32_t readaheadLimit = Preferences::GetInt("media.cache_readahead_limit", 30);
for (uint32_t i = 0; i < mStreams.Length(); ++i) {
actions.AppendElement(NONE);
MediaCacheStream* stream = mStreams[i];
if (stream->mClosed) {
- CACHE_LOG(LogLevel::Debug, ("Stream %p closed", stream));
+ LOG("Stream %p closed", stream);
continue;
}
// Figure out where we should be reading from. It's the first
// uncached byte after the current mStreamOffset.
int64_t dataOffset = stream->GetCachedDataEndInternal(stream->mStreamOffset);
MOZ_ASSERT(dataOffset >= 0);
@@ -1208,81 +1211,80 @@ MediaCache::Update()
// read. We don't want to try to read if we're suspended, because that
// might create a new channel and seek unnecessarily (and incorrectly,
// since HTTP doesn't allow seeking to the actual EOF), and we don't want
// to suspend if we're not suspended and already reading at the end of
// the stream, since there just might be more data than the server
// advertised with Content-Length, and we may as well keep reading.
// But we don't want to seek to the end of the stream if we're not
// already there.
- CACHE_LOG(LogLevel::Debug, ("Stream %p at end of stream", stream));
+ LOG("Stream %p at end of stream", stream);
enableReading = !stream->mCacheSuspended &&
stream->mStreamLength == stream->mChannelOffset;
} else if (desiredOffset < stream->mStreamOffset) {
// We're reading to try to catch up to where the current stream
// reader wants to be. Better not stop.
- CACHE_LOG(LogLevel::Debug, ("Stream %p catching up", stream));
+ LOG("Stream %p catching up", stream);
enableReading = true;
} else if (desiredOffset < stream->mStreamOffset + BLOCK_SIZE) {
// The stream reader is waiting for us, or nearly so. Better feed it.
- CACHE_LOG(LogLevel::Debug, ("Stream %p feeding reader", stream));
+ LOG("Stream %p feeding reader", stream);
enableReading = true;
} else if (!stream->mIsTransportSeekable &&
nonSeekableReadaheadBlockCount >= maxBlocks*NONSEEKABLE_READAHEAD_MAX) {
// This stream is not seekable and there are already too many blocks
// being cached for readahead for nonseekable streams (which we can't
// free). So stop reading ahead now.
- CACHE_LOG(LogLevel::Debug, ("Stream %p throttling non-seekable readahead", stream));
+ LOG("Stream %p throttling non-seekable readahead", stream);
enableReading = false;
} else if (mIndex.Length() > uint32_t(maxBlocks)) {
// We're in the process of bringing the cache size back to the
// desired limit, so don't bring in more data yet
- CACHE_LOG(LogLevel::Debug, ("Stream %p throttling to reduce cache size", stream));
+ LOG("Stream %p throttling to reduce cache size", stream);
enableReading = false;
} else {
TimeDuration predictedNewDataUse = PredictNextUseForIncomingData(stream);
if (stream->mCacheSuspended &&
predictedNewDataUse.ToSeconds() > resumeThreshold) {
// Don't need data for a while, so don't bother waking up the stream
- CACHE_LOG(LogLevel::Debug, ("Stream %p avoiding wakeup since more data is not needed", stream));
+ LOG("Stream %p avoiding wakeup since more data is not needed", stream);
enableReading = false;
} else if (predictedNewDataUse.ToSeconds() > readaheadLimit) {
// Don't read ahead more than this much
- CACHE_LOG(LogLevel::Debug, ("Stream %p throttling to avoid reading ahead too far", stream));
+ LOG("Stream %p throttling to avoid reading ahead too far", stream);
enableReading = false;
} else if (freeBlockCount > 0) {
// Free blocks in the cache, so keep reading
- CACHE_LOG(LogLevel::Debug, ("Stream %p reading since there are free blocks", stream));
+ LOG("Stream %p reading since there are free blocks", stream);
enableReading = true;
} else if (latestNextUse <= TimeDuration(0)) {
// No reusable blocks, so can't read anything
- CACHE_LOG(LogLevel::Debug, ("Stream %p throttling due to no reusable blocks", stream));
+ LOG("Stream %p throttling due to no reusable blocks", stream);
enableReading = false;
} else {
// Read ahead if the data we expect to read is more valuable than
// the least valuable block in the main part of the cache
- CACHE_LOG(LogLevel::Debug, ("Stream %p predict next data in %f, current worst block is %f",
- stream, predictedNewDataUse.ToSeconds(), latestNextUse.ToSeconds()));
+ LOG("Stream %p predict next data in %f, current worst block is %f",
+ stream, predictedNewDataUse.ToSeconds(), latestNextUse.ToSeconds());
enableReading = predictedNewDataUse < latestNextUse;
}
}
if (enableReading) {
for (uint32_t j = 0; j < i; ++j) {
MediaCacheStream* other = mStreams[j];
if (other->mResourceID == stream->mResourceID &&
!other->mClosed && !other->mClient->IsSuspended() &&
other->mChannelOffset/BLOCK_SIZE == desiredOffset/BLOCK_SIZE) {
// This block is already going to be read by the other stream.
// So don't try to read it from this stream as well.
enableReading = false;
- CACHE_LOG(LogLevel::Debug, ("Stream %p waiting on same block (%"
- PRId64 ") from stream %p",
- stream, desiredOffset/BLOCK_SIZE, other));
+ LOG("Stream %p waiting on same block (%" PRId64 ") from stream %p",
+ stream, desiredOffset/BLOCK_SIZE, other);
break;
}
}
}
if (stream->mChannelOffset != desiredOffset && enableReading) {
// We need to seek now.
NS_ASSERTION(stream->mIsTransportSeekable || desiredOffset == 0,
@@ -1335,28 +1337,28 @@ MediaCache::Update()
}
for (uint32_t i = 0; i < mStreams.Length(); ++i) {
MediaCacheStream* stream = mStreams[i];
nsresult rv;
switch (actions[i]) {
case SEEK:
case SEEK_AND_RESUME:
- CACHE_LOG(LogLevel::Debug, ("Stream %p CacheSeek to %lld (resume=%d)", stream,
- (long long)stream->mChannelOffset, actions[i] == SEEK_AND_RESUME));
+ LOG("Stream %p CacheSeek to %" PRId64 " (resume=%d)", stream,
+ stream->mChannelOffset, actions[i] == SEEK_AND_RESUME);
rv = stream->mClient->CacheClientSeek(stream->mChannelOffset,
actions[i] == SEEK_AND_RESUME);
break;
case RESUME:
- CACHE_LOG(LogLevel::Debug, ("Stream %p Resumed", stream));
+ LOG("Stream %p Resumed", stream);
rv = stream->mClient->CacheClientResume();
QueueSuspendedStatusUpdate(stream->mResourceID);
break;
case SUSPEND:
- CACHE_LOG(LogLevel::Debug, ("Stream %p Suspended", stream));
+ LOG("Stream %p Suspended", stream);
rv = stream->mClient->CacheClientSuspend();
QueueSuspendedStatusUpdate(stream->mResourceID);
break;
default:
rv = NS_OK;
break;
}
@@ -1494,32 +1496,33 @@ MediaCache::AllocateAndWriteBlock(
ResourceStreamIterator iter(aStream->mResourceID);
while (MediaCacheStream* stream = iter.Next()) {
while (streamBlockIndex >= int32_t(stream->mBlocks.Length())) {
stream->mBlocks.AppendElement(-1);
}
if (stream->mBlocks[streamBlockIndex] >= 0) {
// We no longer want to own this block
int32_t globalBlockIndex = stream->mBlocks[streamBlockIndex];
- CACHE_LOG(LogLevel::Debug, ("Released block %d from stream %p block %d(%lld)",
- globalBlockIndex, stream, streamBlockIndex, (long long)streamBlockIndex*BLOCK_SIZE));
+ LOG("Released block %d from stream %p block %d(%" PRId64 ")",
+ globalBlockIndex, stream, streamBlockIndex,
+ streamBlockIndex*BLOCK_SIZE);
RemoveBlockOwner(globalBlockIndex, stream);
}
}
// Extend the mBlocks array as necessary
TimeStamp now = TimeStamp::Now();
int32_t blockIndex = FindBlockForIncomingData(now, aStream);
if (blockIndex >= 0) {
FreeBlock(blockIndex);
Block* block = &mIndex[blockIndex];
- CACHE_LOG(LogLevel::Debug, ("Allocated block %d to stream %p block %d(%lld)",
- blockIndex, aStream, streamBlockIndex, (long long)streamBlockIndex*BLOCK_SIZE));
+ LOG("Allocated block %d to stream %p block %d(%" PRId64 ")",
+ blockIndex, aStream, streamBlockIndex, streamBlockIndex*BLOCK_SIZE);
mFreeBlocks.RemoveBlock(blockIndex);
// Tell each stream using this resource about the new block.
ResourceStreamIterator iter(aStream->mResourceID);
while (MediaCacheStream* stream = iter.Next()) {
BlockOwner* bo = block->mOwners.AppendElement();
if (!bo)
@@ -1543,48 +1546,48 @@ MediaCache::AllocateAndWriteBlock(
// the block in the list.
bo->mClass = READAHEAD_BLOCK;
InsertReadaheadBlock(bo, blockIndex);
}
}
nsresult rv = mFileCache->WriteBlock(blockIndex, aData1, aData2);
if (NS_FAILED(rv)) {
- CACHE_LOG(LogLevel::Debug, ("Released block %d from stream %p block %d(%lld)",
- blockIndex, aStream, streamBlockIndex, (long long)streamBlockIndex*BLOCK_SIZE));
+ LOG("Released block %d from stream %p block %d(%" PRId64 ")",
+ blockIndex, aStream, streamBlockIndex, streamBlockIndex*BLOCK_SIZE);
FreeBlock(blockIndex);
}
}
// Queue an Update since the cache state has changed (for example
// we might want to stop loading because the cache is full)
QueueUpdate();
}
void
MediaCache::OpenStream(MediaCacheStream* aStream)
{
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
- CACHE_LOG(LogLevel::Debug, ("Stream %p opened", aStream));
+ LOG("Stream %p opened", aStream);
mStreams.AppendElement(aStream);
aStream->mResourceID = AllocateResourceID();
// Queue an update since a new stream has been opened.
gMediaCache->QueueUpdate();
}
void
MediaCache::ReleaseStream(MediaCacheStream* aStream)
{
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
- CACHE_LOG(LogLevel::Debug, ("Stream %p closed", aStream));
+ LOG("Stream %p closed", aStream);
mStreams.RemoveElement(aStream);
// Update MediaCache again for |mStreams| is changed.
// We need to re-run Update() to ensure streams reading from the same resource
// as the removed stream get a chance to continue reading.
gMediaCache->QueueUpdate();
}
@@ -1595,18 +1598,18 @@ MediaCache::ReleaseStreamBlocks(MediaCac
// XXX scanning the entire stream doesn't seem great, if not much of it
// is cached, but the only easy alternative is to scan the entire cache
// which isn't better
uint32_t length = aStream->mBlocks.Length();
for (uint32_t i = 0; i < length; ++i) {
int32_t blockIndex = aStream->mBlocks[i];
if (blockIndex >= 0) {
- CACHE_LOG(LogLevel::Debug, ("Released block %d from stream %p block %d(%lld)",
- blockIndex, aStream, i, (long long)i*BLOCK_SIZE));
+ LOG("Released block %d from stream %p block %d(%" PRId64 ")",
+ blockIndex, aStream, i, i*BLOCK_SIZE);
RemoveBlockOwner(blockIndex, aStream);
}
}
}
void
MediaCache::Truncate()
{
@@ -1771,18 +1774,18 @@ MediaCacheStream::NotifyDataReceived(int
}
}
}
ReentrantMonitorAutoEnter mon(gMediaCache->GetReentrantMonitor());
int64_t size = aSize;
const char* data = aData;
- CACHE_LOG(LogLevel::Debug, ("Stream %p DataReceived at %lld count=%lld",
- this, (long long)mChannelOffset, (long long)aSize));
+ LOG("Stream %p DataReceived at %" PRId64 " count=%" PRId64,
+ this, mChannelOffset, aSize);
// We process the data one block (or part of a block) at a time
while (size > 0) {
uint32_t blockIndex = mChannelOffset/BLOCK_SIZE;
int32_t blockOffset = int32_t(mChannelOffset - blockIndex*BLOCK_SIZE);
int32_t chunkSize = std::min<int64_t>(BLOCK_SIZE - blockOffset, size);
if (blockOffset == 0) {
@@ -1828,22 +1831,21 @@ MediaCacheStream::NotifyDataReceived(int
void
MediaCacheStream::FlushPartialBlockInternal(bool aNotifyAll,
ReentrantMonitorAutoEnter& aReentrantMonitor)
{
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
int32_t blockOffset = int32_t(mChannelOffset%BLOCK_SIZE);
if (blockOffset > 0) {
- CACHE_LOG(LogLevel::Debug,
- ("Stream %p writing partial block: [%d] bytes; "
- "mStreamOffset [%" PRId64 "] mChannelOffset[%"
- PRId64 "] mStreamLength [%" PRId64 "] notifying: [%s]",
- this, blockOffset, mStreamOffset, mChannelOffset, mStreamLength,
- aNotifyAll ? "yes" : "no"));
+ LOG("Stream %p writing partial block: [%d] bytes; "
+ "mStreamOffset [%" PRId64 "] mChannelOffset[%"
+ PRId64 "] mStreamLength [%" PRId64 "] notifying: [%s]",
+ this, blockOffset, mStreamOffset, mChannelOffset, mStreamLength,
+ aNotifyAll ? "yes" : "no");
// Write back the partial block
memset(mPartialBlockBuffer.get() + blockOffset, 0, BLOCK_SIZE - blockOffset);
auto data = MakeSpan<const uint8_t>(mPartialBlockBuffer.get(), BLOCK_SIZE);
gMediaCache->AllocateAndWriteBlock(this,
mMetadataInPartialBlockBuffer ? MODE_METADATA : MODE_PLAYBACK, data);
}
@@ -2180,17 +2182,17 @@ MediaCacheStream::Seek(int32_t aWhence,
NS_ERROR("Unknown whence");
return NS_ERROR_FAILURE;
}
if (newOffset < 0)
return NS_ERROR_FAILURE;
mStreamOffset = newOffset;
- CACHE_LOG(LogLevel::Debug, ("Stream %p Seek to %lld", this, (long long)mStreamOffset));
+ LOG("Stream %p Seek to %" PRId64, this, mStreamOffset);
gMediaCache->NoteSeek(this, oldOffset);
gMediaCache->QueueUpdate();
return NS_OK;
}
int64_t
MediaCacheStream::Tell()
@@ -2298,17 +2300,17 @@ MediaCacheStream::Read(char* aBuffer, ui
count += bytes;
}
if (count > 0) {
// Some data was read, so queue an update since block priorities may
// have changed
gMediaCache->QueueUpdate();
}
- CACHE_LOG(LogLevel::Debug, ("Stream %p Read at %" PRId64 " count=%d", this, streamOffset-count, count));
+ LOG("Stream %p Read at %" PRId64 " count=%d", this, streamOffset-count, count);
*aBytes = count;
mStreamOffset = streamOffset;
return NS_OK;
}
nsresult
MediaCacheStream::ReadAt(int64_t aOffset, char* aBuffer,
uint32_t aCount, uint32_t* aBytes)
@@ -2467,8 +2469,10 @@ nsresult MediaCacheStream::GetCachedRang
NS_ASSERTION(startOffset == -1 || startOffset > endOffset,
"Must have advanced to start of next range, or hit end of stream");
}
return NS_OK;
}
} // namespace mozilla
+// avoid redefined macro in unified build
+#undef LOG