Backout incorrectly pushed changes

This commit is contained in:
Robert O'Callahan 2009-04-06 14:19:27 +12:00
parent 2bb2ba8f08
commit d95f70cfbf
4 changed files with 25 additions and 45 deletions

View File

@ -40,6 +40,7 @@
#define nsMediaCache_h_ #define nsMediaCache_h_
#include "nsTArray.h" #include "nsTArray.h"
#include "prinrval.h"
#include "nsAutoLock.h" #include "nsAutoLock.h"
/** /**

View File

@ -47,7 +47,6 @@
#include "nsMediaStream.h" #include "nsMediaStream.h"
#include "nsMathUtils.h" #include "nsMathUtils.h"
#include "prlog.h" #include "prlog.h"
#include "nsTimeStamp.h"
#ifdef PR_LOGGING #ifdef PR_LOGGING
PRLogModuleInfo* gMediaCacheLog; PRLogModuleInfo* gMediaCacheLog;
@ -75,9 +74,6 @@ static const PRUint32 REPLAY_DELAY = 30;
// can. // can.
static const PRUint32 FREE_BLOCK_SCAN_LIMIT = 16; static const PRUint32 FREE_BLOCK_SCAN_LIMIT = 16;
using mozilla::TimeStamp;
using mozilla::TimeDuration;
class nsMediaCache { class nsMediaCache {
public: public:
friend class nsMediaCacheStream::BlockList; friend class nsMediaCacheStream::BlockList;
@ -152,7 +148,7 @@ public:
// and thus hasn't yet been committed to the cache. The caller will // and thus hasn't yet been committed to the cache. The caller will
// call QueueUpdate(). // call QueueUpdate().
void NoteBlockUsage(PRInt32 aBlockIndex, nsMediaCacheStream::ReadMode aMode, void NoteBlockUsage(PRInt32 aBlockIndex, nsMediaCacheStream::ReadMode aMode,
TimeStamp aNow); PRIntervalTime aNow);
// This queues a call to Update() on the main thread. // This queues a call to Update() on the main thread.
void QueueUpdate(); void QueueUpdate();
@ -179,7 +175,7 @@ protected:
// Find a free or reusable block and return its index. If there are no // Find a free or reusable block and return its index. If there are no
// free blocks and no reusable blocks, add a new block to the cache // free blocks and no reusable blocks, add a new block to the cache
// and return it. Can return -1 on OOM. // and return it. Can return -1 on OOM.
PRInt32 FindBlockForIncomingData(TimeStamp aNow, nsMediaCacheStream* aStream); PRInt32 FindBlockForIncomingData(PRIntervalTime aNow, nsMediaCacheStream* aStream);
// Find a reusable block --- a free block, if there is one, otherwise // Find a reusable block --- a free block, if there is one, otherwise
// the reusable block with the latest predicted-next-use, or -1 if // the reusable block with the latest predicted-next-use, or -1 if
// there aren't any freeable blocks. Only block indices less than // there aren't any freeable blocks. Only block indices less than
@ -187,7 +183,7 @@ protected:
// then aForStream and aForStreamBlock indicate what media data will // then aForStream and aForStreamBlock indicate what media data will
// be placed; FindReusableBlock will favour returning free blocks // be placed; FindReusableBlock will favour returning free blocks
// near other blocks for that point in the stream. // near other blocks for that point in the stream.
PRInt32 FindReusableBlock(TimeStamp aNow, PRInt32 FindReusableBlock(PRIntervalTime aNow,
nsMediaCacheStream* aForStream, nsMediaCacheStream* aForStream,
PRInt32 aForStreamBlock, PRInt32 aForStreamBlock,
PRInt32 aMaxSearchBlockIndex); PRInt32 aMaxSearchBlockIndex);
@ -226,7 +222,7 @@ protected:
PRUint32 mStreamBlock; PRUint32 mStreamBlock;
// Time at which this block was last used. Valid only if // Time at which this block was last used. Valid only if
// mClass is METADATA_BLOCK or PLAYED_BLOCK. // mClass is METADATA_BLOCK or PLAYED_BLOCK.
TimeStamp mLastUseTime; PRIntervalTime mLastUseTime;
// The class is FREE_BLOCK if and only if mStream is null // The class is FREE_BLOCK if and only if mStream is null
BlockClass mClass; BlockClass mClass;
// Next and previous blocks of this class (circular links, so // Next and previous blocks of this class (circular links, so
@ -249,9 +245,9 @@ protected:
void InsertReadaheadBlock(PRInt32 aBlockIndex); void InsertReadaheadBlock(PRInt32 aBlockIndex);
// Guess the duration until block aBlock will be next used // Guess the duration until block aBlock will be next used
TimeDuration PredictNextUse(TimeStamp aNow, PRInt32 aBlock); PRIntervalTime PredictNextUse(PRIntervalTime aNow, PRInt32 aBlock);
// Guess the duration until the next incoming data on aStream will be used // Guess the duration until the next incoming data on aStream will be used
TimeDuration PredictNextUseForIncomingData(nsMediaCacheStream* aStream); PRIntervalTime PredictNextUseForIncomingData(nsMediaCacheStream* aStream);
// Truncate the file and index array if there are free blocks at the // Truncate the file and index array if there are free blocks at the
// end // end
@ -541,7 +537,7 @@ static PRInt32 GetMaxBlocks()
} }
PRInt32 PRInt32
nsMediaCache::FindBlockForIncomingData(TimeStamp aNow, nsMediaCache::FindBlockForIncomingData(PRIntervalTime aNow,
nsMediaCacheStream* aStream) nsMediaCacheStream* aStream)
{ {
PR_ASSERT_CURRENT_THREAD_IN_MONITOR(mMonitor); PR_ASSERT_CURRENT_THREAD_IN_MONITOR(mMonitor);
@ -595,7 +591,7 @@ nsMediaCache::AppendMostReusableBlock(BlockList* aBlockList,
} }
PRInt32 PRInt32
nsMediaCache::FindReusableBlock(TimeStamp aNow, nsMediaCache::FindReusableBlock(PRIntervalTime aNow,
nsMediaCacheStream* aForStream, nsMediaCacheStream* aForStream,
PRInt32 aForStreamBlock, PRInt32 aForStreamBlock,
PRInt32 aMaxSearchBlockIndex) PRInt32 aMaxSearchBlockIndex)
@ -655,10 +651,10 @@ nsMediaCache::FindReusableBlock(TimeStamp aNow,
} }
} }
TimeDuration latestUse = 0; PRIntervalTime latestUse = 0;
PRInt32 latestUseBlock = -1; PRInt32 latestUseBlock = -1;
for (PRUint32 i = 0; i < candidates.Length(); ++i) { for (PRUint32 i = 0; i < candidates.Length(); ++i) {
TimeDuration nextUse = PredictNextUse(aNow, candidates[i]); PRIntervalTime nextUse = PredictNextUse(aNow, candidates[i]);
if (nextUse > latestUse) { if (nextUse > latestUse) {
latestUse = nextUse; latestUse = nextUse;
latestUseBlock = candidates[i]; latestUseBlock = candidates[i];
@ -771,8 +767,8 @@ nsMediaCache::FreeBlock(PRInt32 aBlock)
Verify(); Verify();
} }
TimeDuration PRIntervalTime
nsMediaCache::PredictNextUse(TimeStamp aNow, PRInt32 aBlock) nsMediaCache::PredictNextUse(PRIntervalTime aNow, PRInt32 aBlock)
{ {
PR_ASSERT_CURRENT_THREAD_IN_MONITOR(mMonitor); PR_ASSERT_CURRENT_THREAD_IN_MONITOR(mMonitor);
@ -789,8 +785,7 @@ nsMediaCache::PredictNextUse(TimeStamp aNow, PRInt32 aBlock)
NS_ASSERTION(PRInt64(block->mStreamBlock)*BLOCK_SIZE < NS_ASSERTION(PRInt64(block->mStreamBlock)*BLOCK_SIZE <
block->mStream->mStreamOffset, block->mStream->mStreamOffset,
"Played block after the current stream position?"); "Played block after the current stream position?");
return aNow - block->mLastUseTime + return aNow - block->mLastUseTime + PR_SecondsToInterval(REPLAY_DELAY);
TimeDuration::FromSeconds(REPLAY_DELAY);
case READAHEAD_BLOCK: { case READAHEAD_BLOCK: {
PRInt64 bytesAhead = PRInt64 bytesAhead =
PRInt64(block->mStreamBlock)*BLOCK_SIZE - block->mStream->mStreamOffset; PRInt64(block->mStreamBlock)*BLOCK_SIZE - block->mStream->mStreamOffset;
@ -798,8 +793,7 @@ nsMediaCache::PredictNextUse(TimeStamp aNow, PRInt32 aBlock)
"Readahead block before the current stream position?"); "Readahead block before the current stream position?");
PRInt64 millisecondsAhead = PRInt64 millisecondsAhead =
bytesAhead*1000/block->mStream->mPlaybackBytesPerSecond; bytesAhead*1000/block->mStream->mPlaybackBytesPerSecond;
return TimeDuration::FromMilliseconds( return PR_MillisecondsToInterval(PRUint32(PR_MIN(millisecondsAhead, PR_INT32_MAX)));
PR_MIN(millisecondsAhead, PR_INT32_MAX));
} }
default: default:
NS_ERROR("Invalid class for predicting next use"); NS_ERROR("Invalid class for predicting next use");
@ -807,7 +801,7 @@ nsMediaCache::PredictNextUse(TimeStamp aNow, PRInt32 aBlock)
} }
} }
TimeDuration PRIntervalTime
nsMediaCache::PredictNextUseForIncomingData(nsMediaCacheStream* aStream) nsMediaCache::PredictNextUseForIncomingData(nsMediaCacheStream* aStream)
{ {
PR_ASSERT_CURRENT_THREAD_IN_MONITOR(mMonitor); PR_ASSERT_CURRENT_THREAD_IN_MONITOR(mMonitor);
@ -818,10 +812,9 @@ nsMediaCache::PredictNextUseForIncomingData(nsMediaCacheStream* aStream)
return PR_SecondsToInterval(24*60*60); return PR_SecondsToInterval(24*60*60);
} }
if (bytesAhead <= 0) if (bytesAhead <= 0)
return TimeDuration(); return PR_SecondsToInterval(0);
PRInt64 millisecondsAhead = bytesAhead*1000/aStream->mPlaybackBytesPerSecond; PRInt64 millisecondsAhead = bytesAhead*1000/aStream->mPlaybackBytesPerSecond;
return TimeDuration::FromMilliseconds( return PR_MillisecondsToInterval(PRUint32(PR_MIN(millisecondsAhead, PR_INT32_MAX)));
PR_MIN(millisecondsAhead, PR_INT32_MAX));
} }
void void
@ -836,7 +829,7 @@ nsMediaCache::Update()
#endif #endif
PRInt32 maxBlocks = GetMaxBlocks(); PRInt32 maxBlocks = GetMaxBlocks();
TimeStamp now = TimeStamp::Now(); PRIntervalTime now = PR_IntervalNow();
PRInt32 freeBlockCount = mFreeBlocks.GetCount(); PRInt32 freeBlockCount = mFreeBlocks.GetCount();
// Try to trim back the cache to its desired maximum size. The cache may // Try to trim back the cache to its desired maximum size. The cache may
@ -853,7 +846,7 @@ nsMediaCache::Update()
// to avoid that since it requires HTTP seeks. // to avoid that since it requires HTTP seeks.
// We also use this loop to eliminate overflowing blocks from // We also use this loop to eliminate overflowing blocks from
// freeBlockCount. // freeBlockCount.
TimeDuration latestPredictedUseForOverflow = 0; PRIntervalTime latestPredictedUseForOverflow = 0;
for (PRInt32 blockIndex = mIndex.Length() - 1; blockIndex >= maxBlocks; for (PRInt32 blockIndex = mIndex.Length() - 1; blockIndex >= maxBlocks;
--blockIndex) { --blockIndex) {
nsMediaCacheStream* stream = mIndex[blockIndex].mStream; nsMediaCacheStream* stream = mIndex[blockIndex].mStream;
@ -862,7 +855,7 @@ nsMediaCache::Update()
--freeBlockCount; --freeBlockCount;
continue; continue;
} }
TimeDuration predictedUse = PredictNextUse(now, blockIndex); PRIntervalTime predictedUse = PredictNextUse(now, blockIndex);
latestPredictedUseForOverflow = PR_MAX(latestPredictedUseForOverflow, predictedUse); latestPredictedUseForOverflow = PR_MAX(latestPredictedUseForOverflow, predictedUse);
} }
@ -933,7 +926,7 @@ nsMediaCache::Update()
// If freeBlockCount is zero, then compute the latest of // If freeBlockCount is zero, then compute the latest of
// the predicted next-uses for all blocks // the predicted next-uses for all blocks
TimeDuration latestNextUse; PRIntervalTime latestNextUse = 0;
if (freeBlockCount == 0) { if (freeBlockCount == 0) {
PRInt32 reusableBlock = FindReusableBlock(now, nsnull, 0, maxBlocks); PRInt32 reusableBlock = FindReusableBlock(now, nsnull, 0, maxBlocks);
if (reusableBlock >= 0) { if (reusableBlock >= 0) {
@ -1013,7 +1006,7 @@ nsMediaCache::Update()
// Free blocks in the cache, so keep reading // Free blocks in the cache, so keep reading
LOG(PR_LOG_DEBUG, ("Stream %p reading since there are free blocks", stream)); LOG(PR_LOG_DEBUG, ("Stream %p reading since there are free blocks", stream));
enableReading = PR_TRUE; enableReading = PR_TRUE;
} else if (latestNextUse <= TimeDuration()) { } else if (latestNextUse <= 0) {
// No reusable blocks, so can't read anything // No reusable blocks, so can't read anything
LOG(PR_LOG_DEBUG, ("Stream %p throttling due to no reusable blocks", stream)); LOG(PR_LOG_DEBUG, ("Stream %p throttling due to no reusable blocks", stream));
enableReading = PR_FALSE; enableReading = PR_FALSE;

View File

@ -72,21 +72,6 @@ public:
return TimeDuration(PRInt64(aMilliseconds)*PR_TicksPerSecond()/1000); return TimeDuration(PRInt64(aMilliseconds)*PR_TicksPerSecond()/1000);
} }
TimeDuration operator+(const TimeDuration& aOther) const {
return TimeDuration(mValue + aOther.mValue);
}
TimeDuration operator-(const TimeDuration& aOther) const {
return TimeDuration(mValue - aOther.mValue);
}
TimeDuration& operator+=(const TimeDuration& aOther) {
mValue += aOther.mValue;
return *this;
}
TimeDuration& operator-=(const TimeDuration& aOther) {
mValue -= aOther.mValue;
return *this;
}
PRBool operator<(const TimeDuration& aOther) const { PRBool operator<(const TimeDuration& aOther) const {
return mValue < aOther.mValue; return mValue < aOther.mValue;
} }
@ -102,6 +87,7 @@ public:
// We could define additional operators here: // We could define additional operators here:
// -- convert to/from other time units // -- convert to/from other time units
// -- add/subtract durations
// -- scale duration by a float // -- scale duration by a float
// but let's do that on demand. // but let's do that on demand.
// Comparing durations for equality should be discouraged. // Comparing durations for equality should be discouraged.

View File

@ -96,6 +96,7 @@ CPP_UNIT_TESTS = \
TestPipe.cpp \ TestPipe.cpp \
TestServMgr.cpp \ TestServMgr.cpp \
TestTextFormatter.cpp \ TestTextFormatter.cpp \
TestTimeStamp.cpp \
$(NULL) $(NULL)
ifndef MOZ_ENABLE_LIBXUL ifndef MOZ_ENABLE_LIBXUL
@ -114,7 +115,6 @@ CPP_UNIT_TESTS += \
TestStrings.cpp \ TestStrings.cpp \
TestStorageStream.cpp \ TestStorageStream.cpp \
TestTArray.cpp \ TestTArray.cpp \
TestTimeStamp.cpp \
$(NULL) $(NULL)
endif endif