2009-09-15 02:30:44 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2009-04-01 00:52:56 +00:00
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2009-04-01 00:52:56 +00:00
|
|
|
|
2011-04-29 19:21:57 +00:00
|
|
|
#include "mozilla/ReentrantMonitor.h"
|
2009-05-18 18:15:05 +00:00
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
#include "MediaCache.h"
|
2009-04-01 00:52:56 +00:00
|
|
|
#include "prio.h"
|
2012-04-30 03:11:00 +00:00
|
|
|
#include "nsContentUtils.h"
|
2009-04-01 00:52:56 +00:00
|
|
|
#include "nsThreadUtils.h"
|
2012-02-15 04:35:01 +00:00
|
|
|
#include "MediaResource.h"
|
2009-04-01 00:52:56 +00:00
|
|
|
#include "prlog.h"
|
2011-05-25 06:32:00 +00:00
|
|
|
#include "mozilla/Preferences.h"
|
2012-03-28 00:04:26 +00:00
|
|
|
#include "FileBlockCache.h"
|
2013-09-05 17:29:38 +00:00
|
|
|
#include "nsAnonymousTemporaryFile.h"
|
2013-09-05 20:25:17 +00:00
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsISeekableStream.h"
|
|
|
|
#include "nsIPrincipal.h"
|
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/Services.h"
|
2013-01-15 12:22:03 +00:00
|
|
|
#include <algorithm>
|
2009-04-01 00:52:56 +00:00
|
|
|
|
2012-11-14 19:45:33 +00:00
|
|
|
namespace mozilla {
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 04:29:02 +00:00
|
|
|
|
2009-04-01 00:52:56 +00:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
PRLogModuleInfo* gMediaCacheLog;
|
2013-11-21 03:02:42 +00:00
|
|
|
#define CACHE_LOG(type, msg) PR_LOG(gMediaCacheLog, type, msg)
|
2009-04-01 00:52:56 +00:00
|
|
|
#else
|
2013-11-21 03:02:42 +00:00
|
|
|
#define CACHE_LOG(type, msg)
|
2009-04-01 00:52:56 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2013-06-04 13:52:57 +00:00
|
|
|
// Data N seconds before the current playback position is given the same priority
|
|
|
|
// as data REPLAY_PENALTY_FACTOR*N seconds ahead of the current playback
|
|
|
|
// position. REPLAY_PENALTY_FACTOR is greater than 1 to reflect that
|
|
|
|
// data in the past is less likely to be played again than data in the future.
|
|
|
|
// We want to give data just behind the current playback position reasonably
|
|
|
|
// high priority in case codecs need to retrieve that data (e.g. because
|
|
|
|
// tracks haven't been muxed well or are being decoded at uneven rates).
|
|
|
|
// 1/REPLAY_PENALTY_FACTOR as much data will be kept behind the
|
|
|
|
// current playback position as will be kept ahead of the current playback
|
|
|
|
// position.
|
|
|
|
static const uint32_t REPLAY_PENALTY_FACTOR = 3;
|
2009-04-01 00:52:56 +00:00
|
|
|
|
|
|
|
// When looking for a reusable block, scan forward this many blocks
|
2010-10-26 01:21:16 +00:00
|
|
|
// from the desired "best" block location to look for free blocks,
|
2009-04-01 00:52:56 +00:00
|
|
|
// before we resort to scanning the whole cache. The idea is to try to
|
|
|
|
// store runs of stream blocks close-to-consecutively in the cache if we
|
|
|
|
// can.
|
2012-08-22 15:56:38 +00:00
|
|
|
static const uint32_t FREE_BLOCK_SCAN_LIMIT = 16;
|
2009-04-01 00:52:56 +00:00
|
|
|
|
2013-07-19 09:00:19 +00:00
|
|
|
// Try to save power by not resuming paused reads if the stream won't need new
|
|
|
|
// data within this time interval in the future
|
|
|
|
static const uint32_t CACHE_POWERSAVE_WAKEUP_LOW_THRESHOLD_MS = 10000;
|
|
|
|
|
2009-06-12 01:43:23 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
// Turn this on to do very expensive cache state validation
|
|
|
|
// #define DEBUG_VERIFY_CACHE
|
|
|
|
#endif
|
|
|
|
|
2009-09-15 02:30:45 +00:00
|
|
|
// There is at most one media cache (although that could quite easily be
|
|
|
|
// relaxed if we wanted to manage multiple caches with independent
|
|
|
|
// size limits).
|
2012-11-14 19:46:40 +00:00
|
|
|
static MediaCache* gMediaCache;
|
2009-09-15 02:30:45 +00:00
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
class MediaCacheFlusher MOZ_FINAL : public nsIObserver,
|
2012-06-19 02:30:09 +00:00
|
|
|
public nsSupportsWeakReference {
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheFlusher() {}
|
|
|
|
~MediaCacheFlusher();
|
2010-09-29 00:02:08 +00:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
|
|
|
static void Init();
|
|
|
|
};
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
static MediaCacheFlusher* gMediaCacheFlusher;
|
2010-09-29 00:02:08 +00:00
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
NS_IMPL_ISUPPORTS2(MediaCacheFlusher, nsIObserver, nsISupportsWeakReference)
|
2010-09-29 00:02:08 +00:00
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheFlusher::~MediaCacheFlusher()
|
2010-09-29 00:02:08 +00:00
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
gMediaCacheFlusher = nullptr;
|
2010-09-29 00:02:08 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaCacheFlusher::Init()
|
2010-09-29 00:02:08 +00:00
|
|
|
{
|
|
|
|
if (gMediaCacheFlusher) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
gMediaCacheFlusher = new MediaCacheFlusher();
|
2010-09-29 00:02:08 +00:00
|
|
|
NS_ADDREF(gMediaCacheFlusher);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (observerService) {
|
2012-04-17 03:16:47 +00:00
|
|
|
observerService->AddObserver(gMediaCacheFlusher, "last-pb-context-exited", true);
|
2013-06-18 05:44:00 +00:00
|
|
|
observerService->AddObserver(gMediaCacheFlusher, "network-clear-cache-stored-anywhere", true);
|
2010-09-29 00:02:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
class MediaCache {
|
2009-04-01 00:52:56 +00:00
|
|
|
public:
|
2012-11-14 19:46:40 +00:00
|
|
|
friend class MediaCacheStream::BlockList;
|
|
|
|
typedef MediaCacheStream::BlockList BlockList;
|
2009-04-01 00:52:56 +00:00
|
|
|
enum {
|
2012-11-14 19:46:40 +00:00
|
|
|
BLOCK_SIZE = MediaCacheStream::BLOCK_SIZE
|
2009-04-01 00:52:56 +00:00
|
|
|
};
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache() : mNextResourceID(1),
|
|
|
|
mReentrantMonitor("MediaCache.mReentrantMonitor"),
|
2012-03-28 00:04:26 +00:00
|
|
|
mUpdateQueued(false)
|
2009-04-01 00:52:56 +00:00
|
|
|
#ifdef DEBUG
|
2011-09-29 23:34:37 +00:00
|
|
|
, mInUpdate(false)
|
2009-04-01 00:52:56 +00:00
|
|
|
#endif
|
|
|
|
{
|
2012-11-14 19:46:40 +00:00
|
|
|
MOZ_COUNT_CTOR(MediaCache);
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
2012-11-14 19:46:40 +00:00
|
|
|
~MediaCache() {
|
2009-04-01 00:52:56 +00:00
|
|
|
NS_ASSERTION(mStreams.IsEmpty(), "Stream(s) still open!");
|
|
|
|
Truncate();
|
|
|
|
NS_ASSERTION(mIndex.Length() == 0, "Blocks leaked?");
|
2012-04-01 22:34:11 +00:00
|
|
|
if (mFileCache) {
|
|
|
|
mFileCache->Close();
|
2012-07-30 14:20:58 +00:00
|
|
|
mFileCache = nullptr;
|
2012-04-01 22:34:11 +00:00
|
|
|
}
|
2012-11-14 19:46:40 +00:00
|
|
|
MOZ_COUNT_DTOR(MediaCache);
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
|
2010-10-13 14:14:08 +00:00
|
|
|
// Main thread only. Creates the backing cache file. If this fails,
|
|
|
|
// then the cache is still in a semi-valid state; mFD will be null,
|
|
|
|
// so all I/O on the cache file will fail.
|
2009-04-01 00:52:56 +00:00
|
|
|
nsresult Init();
|
|
|
|
// Shut down the global cache if it's no longer needed. We shut down
|
|
|
|
// the cache as soon as there are no streams. This means that during
|
|
|
|
// normal operation we are likely to start up the cache and shut it down
|
|
|
|
// many times, but that's OK since starting it up is cheap and
|
|
|
|
// shutting it down cleans things up and releases disk space.
|
|
|
|
static void MaybeShutdown();
|
|
|
|
|
2010-10-13 14:14:08 +00:00
|
|
|
// Brutally flush the cache contents. Main thread only.
|
|
|
|
static void Flush();
|
|
|
|
void FlushInternal();
|
|
|
|
|
2009-04-01 00:52:56 +00:00
|
|
|
// Cache-file access methods. These are the lowest-level cache methods.
|
2011-04-29 19:21:57 +00:00
|
|
|
// mReentrantMonitor must be held; these can be called on any thread.
|
2009-04-01 00:52:56 +00:00
|
|
|
// This can return partial reads.
|
2012-08-22 15:56:38 +00:00
|
|
|
nsresult ReadCacheFile(int64_t aOffset, void* aData, int32_t aLength,
|
|
|
|
int32_t* aBytes);
|
2009-04-01 00:52:56 +00:00
|
|
|
// This will fail if all aLength bytes are not read
|
2012-08-22 15:56:38 +00:00
|
|
|
nsresult ReadCacheFileAllBytes(int64_t aOffset, void* aData, int32_t aLength);
|
2009-04-01 00:52:56 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t AllocateResourceID()
|
2012-03-20 07:55:40 +00:00
|
|
|
{
|
|
|
|
mReentrantMonitor.AssertCurrentThreadIn();
|
|
|
|
return mNextResourceID++;
|
|
|
|
}
|
|
|
|
|
2011-04-29 19:21:57 +00:00
|
|
|
// mReentrantMonitor must be held, called on main thread.
|
2009-04-01 00:52:56 +00:00
|
|
|
// These methods are used by the stream to set up and tear down streams,
|
|
|
|
// and to handle reads and writes.
|
|
|
|
// Add aStream to the list of streams.
|
2012-11-14 19:46:40 +00:00
|
|
|
void OpenStream(MediaCacheStream* aStream);
|
2009-04-01 00:52:56 +00:00
|
|
|
// Remove aStream from the list of streams.
|
2012-11-14 19:46:40 +00:00
|
|
|
void ReleaseStream(MediaCacheStream* aStream);
|
2009-04-01 00:52:56 +00:00
|
|
|
// Free all blocks belonging to aStream.
|
2012-11-14 19:46:40 +00:00
|
|
|
void ReleaseStreamBlocks(MediaCacheStream* aStream);
|
2009-04-01 00:52:56 +00:00
|
|
|
// Find a cache entry for this data, and write the data into it
|
2012-11-14 19:46:40 +00:00
|
|
|
void AllocateAndWriteBlock(MediaCacheStream* aStream, const void* aData,
|
|
|
|
MediaCacheStream::ReadMode aMode);
|
2009-04-01 00:52:56 +00:00
|
|
|
|
2011-04-29 19:21:57 +00:00
|
|
|
// mReentrantMonitor must be held; can be called on any thread
|
2009-04-01 00:52:56 +00:00
|
|
|
// Notify the cache that a seek has been requested. Some blocks may
|
|
|
|
// need to change their class between PLAYED_BLOCK and READAHEAD_BLOCK.
|
|
|
|
// This does not trigger channel seeks directly, the next Update()
|
|
|
|
// will do that if necessary. The caller will call QueueUpdate().
|
2012-11-14 19:46:40 +00:00
|
|
|
void NoteSeek(MediaCacheStream* aStream, int64_t aOldOffset);
|
2009-04-01 00:52:56 +00:00
|
|
|
// Notify the cache that a block has been read from. This is used
|
|
|
|
// to update last-use times. The block may not actually have a
|
|
|
|
// cache entry yet since Read can read data from a stream's
|
|
|
|
// in-memory mPartialBlockBuffer while the block is only partly full,
|
|
|
|
// and thus hasn't yet been committed to the cache. The caller will
|
|
|
|
// call QueueUpdate().
|
2012-11-14 19:46:40 +00:00
|
|
|
void NoteBlockUsage(MediaCacheStream* aStream, int32_t aBlockIndex,
|
|
|
|
MediaCacheStream::ReadMode aMode, TimeStamp aNow);
|
2009-09-15 02:30:44 +00:00
|
|
|
// Mark aStream as having the block, adding it as an owner.
|
2012-11-14 19:46:40 +00:00
|
|
|
void AddBlockOwnerAsReadahead(int32_t aBlockIndex, MediaCacheStream* aStream,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t aStreamBlockIndex);
|
2009-04-01 00:52:56 +00:00
|
|
|
|
|
|
|
// This queues a call to Update() on the main thread.
|
|
|
|
void QueueUpdate();
|
|
|
|
|
|
|
|
// Updates the cache state asynchronously on the main thread:
|
|
|
|
// -- try to trim the cache back to its desired size, if necessary
|
|
|
|
// -- suspend channels that are going to read data that's lower priority
|
|
|
|
// than anything currently cached
|
|
|
|
// -- resume channels that are going to read data that's higher priority
|
|
|
|
// than something currently cached
|
|
|
|
// -- seek channels that need to seek to a new location
|
|
|
|
void Update();
|
|
|
|
|
2009-06-12 01:43:23 +00:00
|
|
|
#ifdef DEBUG_VERIFY_CACHE
|
2009-04-01 00:52:56 +00:00
|
|
|
// Verify invariants, especially block list invariants
|
|
|
|
void Verify();
|
|
|
|
#else
|
|
|
|
void Verify() {}
|
|
|
|
#endif
|
|
|
|
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitor& GetReentrantMonitor() { return mReentrantMonitor; }
|
2009-04-01 00:52:56 +00:00
|
|
|
|
2009-11-18 00:38:16 +00:00
|
|
|
/**
|
|
|
|
* An iterator that makes it easy to iterate through all streams that
|
|
|
|
* have a given resource ID and are not closed.
|
2012-04-30 03:12:42 +00:00
|
|
|
* Can be used on the main thread or while holding the media cache lock.
|
2009-11-18 00:38:16 +00:00
|
|
|
*/
|
2009-09-15 02:30:45 +00:00
|
|
|
class ResourceStreamIterator {
|
|
|
|
public:
|
2012-08-22 15:56:38 +00:00
|
|
|
ResourceStreamIterator(int64_t aResourceID) :
|
2009-09-15 02:30:45 +00:00
|
|
|
mResourceID(aResourceID), mNext(0) {}
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream* Next()
|
2009-09-15 02:30:45 +00:00
|
|
|
{
|
|
|
|
while (mNext < gMediaCache->mStreams.Length()) {
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream* stream = gMediaCache->mStreams[mNext];
|
2009-09-15 02:30:45 +00:00
|
|
|
++mNext;
|
2009-11-18 00:38:16 +00:00
|
|
|
if (stream->GetResourceID() == mResourceID && !stream->IsClosed())
|
2009-09-15 02:30:45 +00:00
|
|
|
return stream;
|
|
|
|
}
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2009-09-15 02:30:45 +00:00
|
|
|
}
|
|
|
|
private:
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t mResourceID;
|
|
|
|
uint32_t mNext;
|
2009-09-15 02:30:45 +00:00
|
|
|
};
|
|
|
|
|
2009-04-01 00:52:56 +00:00
|
|
|
protected:
|
|
|
|
// 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
|
|
|
|
// and return it. Can return -1 on OOM.
|
2012-11-14 19:46:40 +00:00
|
|
|
int32_t FindBlockForIncomingData(TimeStamp aNow, MediaCacheStream* aStream);
|
2009-04-01 00:52:56 +00:00
|
|
|
// Find a reusable block --- a free block, if there is one, otherwise
|
|
|
|
// the reusable block with the latest predicted-next-use, or -1 if
|
|
|
|
// there aren't any freeable blocks. Only block indices less than
|
|
|
|
// aMaxSearchBlockIndex are considered. If aForStream is non-null,
|
|
|
|
// then aForStream and aForStreamBlock indicate what media data will
|
|
|
|
// be placed; FindReusableBlock will favour returning free blocks
|
|
|
|
// near other blocks for that point in the stream.
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t FindReusableBlock(TimeStamp aNow,
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream* aForStream,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t aForStreamBlock,
|
|
|
|
int32_t aMaxSearchBlockIndex);
|
|
|
|
bool BlockIsReusable(int32_t aBlockIndex);
|
2009-04-01 00:52:56 +00:00
|
|
|
// Given a list of blocks sorted with the most reusable blocks at the
|
|
|
|
// end, find the last block whose stream is not pinned (if any)
|
|
|
|
// and whose cache entry index is less than aBlockIndexLimit
|
|
|
|
// and append it to aResult.
|
|
|
|
void AppendMostReusableBlock(BlockList* aBlockList,
|
2012-08-22 15:56:38 +00:00
|
|
|
nsTArray<uint32_t>* aResult,
|
|
|
|
int32_t aBlockIndexLimit);
|
2009-04-01 00:52:56 +00:00
|
|
|
|
|
|
|
enum BlockClass {
|
|
|
|
// block belongs to mMetadataBlockList because data has been consumed
|
|
|
|
// from it in "metadata mode" --- in particular blocks read during
|
|
|
|
// Ogg seeks go into this class. These blocks may have played data
|
|
|
|
// in them too.
|
|
|
|
METADATA_BLOCK,
|
|
|
|
// block belongs to mPlayedBlockList because its offset is
|
|
|
|
// less than the stream's current reader position
|
|
|
|
PLAYED_BLOCK,
|
|
|
|
// block belongs to the stream's mReadaheadBlockList because its
|
|
|
|
// offset is greater than or equal to the stream's current
|
|
|
|
// reader position
|
|
|
|
READAHEAD_BLOCK
|
|
|
|
};
|
|
|
|
|
2009-09-15 02:30:44 +00:00
|
|
|
struct BlockOwner {
|
2012-07-30 14:20:58 +00:00
|
|
|
BlockOwner() : mStream(nullptr), mClass(READAHEAD_BLOCK) {}
|
2009-04-01 00:52:56 +00:00
|
|
|
|
|
|
|
// The stream that owns this block, or null if the block is free.
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream* mStream;
|
2009-04-01 00:52:56 +00:00
|
|
|
// The block index in the stream. Valid only if mStream is non-null.
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t mStreamBlock;
|
2009-04-01 00:52:56 +00:00
|
|
|
// Time at which this block was last used. Valid only if
|
|
|
|
// mClass is METADATA_BLOCK or PLAYED_BLOCK.
|
2009-04-11 09:39:02 +00:00
|
|
|
TimeStamp mLastUseTime;
|
2009-04-01 00:52:56 +00:00
|
|
|
BlockClass mClass;
|
|
|
|
};
|
|
|
|
|
2009-09-15 02:30:44 +00:00
|
|
|
struct Block {
|
|
|
|
// Free blocks have an empty mOwners array
|
|
|
|
nsTArray<BlockOwner> mOwners;
|
|
|
|
};
|
|
|
|
|
2009-04-01 00:52:56 +00:00
|
|
|
// Get the BlockList that the block should belong to given its
|
2009-09-15 02:30:44 +00:00
|
|
|
// current owner
|
|
|
|
BlockList* GetListForBlock(BlockOwner* aBlock);
|
|
|
|
// Get the BlockOwner for the given block index and owning stream
|
|
|
|
// (returns null if the stream does not own the block)
|
2012-11-14 19:46:40 +00:00
|
|
|
BlockOwner* GetBlockOwner(int32_t aBlockIndex, MediaCacheStream* aStream);
|
2009-09-15 02:30:44 +00:00
|
|
|
// Returns true iff the block is free
|
2012-08-22 15:56:38 +00:00
|
|
|
bool IsBlockFree(int32_t aBlockIndex)
|
2009-09-15 02:30:44 +00:00
|
|
|
{ return mIndex[aBlockIndex].mOwners.IsEmpty(); }
|
|
|
|
// Add the block to the free list and mark its streams as not having
|
|
|
|
// the block in cache
|
2012-08-22 15:56:38 +00:00
|
|
|
void FreeBlock(int32_t aBlock);
|
2009-09-15 02:30:44 +00:00
|
|
|
// Mark aStream as not having the block, removing it as an owner. If
|
|
|
|
// the block has no more owners it's added to the free list.
|
2012-11-14 19:46:40 +00:00
|
|
|
void RemoveBlockOwner(int32_t aBlockIndex, MediaCacheStream* aStream);
|
2009-04-01 00:52:56 +00:00
|
|
|
// Swap all metadata associated with the two blocks. The caller
|
|
|
|
// is responsible for swapping up any cache file state.
|
2012-08-22 15:56:38 +00:00
|
|
|
void SwapBlocks(int32_t aBlockIndex1, int32_t aBlockIndex2);
|
2009-09-15 02:30:44 +00:00
|
|
|
// Insert the block into the readahead block list for the stream
|
2009-04-01 00:52:56 +00:00
|
|
|
// at the right point in the list.
|
2012-08-22 15:56:38 +00:00
|
|
|
void InsertReadaheadBlock(BlockOwner* aBlockOwner, int32_t aBlockIndex);
|
2009-04-01 00:52:56 +00:00
|
|
|
|
|
|
|
// Guess the duration until block aBlock will be next used
|
2012-08-22 15:56:38 +00:00
|
|
|
TimeDuration PredictNextUse(TimeStamp aNow, int32_t aBlock);
|
2009-04-01 00:52:56 +00:00
|
|
|
// Guess the duration until the next incoming data on aStream will be used
|
2012-11-14 19:46:40 +00:00
|
|
|
TimeDuration PredictNextUseForIncomingData(MediaCacheStream* aStream);
|
2009-04-01 00:52:56 +00:00
|
|
|
|
|
|
|
// Truncate the file and index array if there are free blocks at the
|
|
|
|
// end
|
|
|
|
void Truncate();
|
|
|
|
|
2009-09-15 02:30:45 +00:00
|
|
|
// This member is main-thread only. It's used to allocate unique
|
|
|
|
// resource IDs to streams.
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t mNextResourceID;
|
2009-04-01 00:52:56 +00:00
|
|
|
|
|
|
|
// The monitor protects all the data members here. Also, off-main-thread
|
|
|
|
// readers that need to block will Wait() on this monitor. When new
|
|
|
|
// data becomes available in the cache, we NotifyAll() on this monitor.
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitor mReentrantMonitor;
|
2012-04-30 03:12:42 +00:00
|
|
|
// This is only written while on the main thread and the monitor is held.
|
|
|
|
// Thus, it can be safely read from the main thread or while holding the monitor.
|
2012-11-14 19:46:40 +00:00
|
|
|
nsTArray<MediaCacheStream*> mStreams;
|
2009-04-01 00:52:56 +00:00
|
|
|
// The Blocks describing the cache entries.
|
|
|
|
nsTArray<Block> mIndex;
|
2012-03-28 00:04:26 +00:00
|
|
|
// Writer which performs IO, asynchronously writing cache blocks.
|
|
|
|
nsRefPtr<FileBlockCache> mFileCache;
|
2009-04-01 00:52:56 +00:00
|
|
|
// The list of free blocks; they are not ordered.
|
|
|
|
BlockList mFreeBlocks;
|
|
|
|
// True if an event to run Update() has been queued but not processed
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mUpdateQueued;
|
2009-04-01 00:52:56 +00:00
|
|
|
#ifdef DEBUG
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mInUpdate;
|
2009-04-01 00:52:56 +00:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2010-09-29 00:02:08 +00:00
|
|
|
NS_IMETHODIMP
|
2014-01-04 15:02:17 +00:00
|
|
|
MediaCacheFlusher::Observe(nsISupports *aSubject, char const *aTopic, char16_t const *aData)
|
2010-09-29 00:02:08 +00:00
|
|
|
{
|
2012-04-17 03:16:47 +00:00
|
|
|
if (strcmp(aTopic, "last-pb-context-exited") == 0) {
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::Flush();
|
2010-09-29 00:02:08 +00:00
|
|
|
}
|
2013-06-18 05:44:00 +00:00
|
|
|
if (strcmp(aTopic, "network-clear-cache-stored-anywhere") == 0) {
|
|
|
|
MediaCache::Flush();
|
|
|
|
}
|
2010-09-29 00:02:08 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-05 20:25:17 +00:00
|
|
|
MediaCacheStream::MediaCacheStream(ChannelMediaResource* aClient)
|
|
|
|
: mClient(aClient),
|
|
|
|
mInitialized(false),
|
|
|
|
mHasHadUpdate(false),
|
|
|
|
mClosed(false),
|
|
|
|
mDidNotifyDataEnded(false),
|
|
|
|
mResourceID(0),
|
|
|
|
mIsTransportSeekable(false),
|
|
|
|
mCacheSuspended(false),
|
|
|
|
mChannelEnded(false),
|
|
|
|
mChannelOffset(0),
|
|
|
|
mStreamLength(-1),
|
|
|
|
mStreamOffset(0),
|
|
|
|
mPlaybackBytesPerSecond(10000),
|
|
|
|
mPinCount(0),
|
|
|
|
mCurrentMode(MODE_PLAYBACK),
|
2014-02-11 06:43:52 +00:00
|
|
|
mMetadataInPartialBlockBuffer(false),
|
|
|
|
mPartialBlockBuffer(new int64_t[BLOCK_SIZE/sizeof(int64_t)])
|
2013-09-05 20:25:17 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-03-05 21:31:04 +00:00
|
|
|
size_t MediaCacheStream::SizeOfExcludingThis(
|
|
|
|
MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
// Looks like these are not owned:
|
|
|
|
// - mClient
|
|
|
|
// - mPrincipal
|
|
|
|
size_t size = mBlocks.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
size += mReadaheadBlocks.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
size += mMetadataBlocks.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
size += mPlayedBlocks.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
size += mPartialBlockBuffer.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t MediaCacheStream::BlockList::SizeOfExcludingThis(
|
|
|
|
MallocSizeOf aMallocSizeOf) const
|
|
|
|
{
|
|
|
|
return mEntries.SizeOfExcludingThis(/* sizeOfEntryExcludingThis = */ nullptr,
|
|
|
|
aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaCacheStream::BlockList::AddFirstBlock(int32_t aBlock)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2009-09-15 02:30:44 +00:00
|
|
|
NS_ASSERTION(!mEntries.GetEntry(aBlock), "Block already in list");
|
|
|
|
Entry* entry = mEntries.PutEntry(aBlock);
|
|
|
|
|
2009-04-01 00:52:56 +00:00
|
|
|
if (mFirstBlock < 0) {
|
2009-09-15 02:30:44 +00:00
|
|
|
entry->mNextBlock = entry->mPrevBlock = aBlock;
|
2009-04-01 00:52:56 +00:00
|
|
|
} else {
|
2009-09-15 02:30:44 +00:00
|
|
|
entry->mNextBlock = mFirstBlock;
|
|
|
|
entry->mPrevBlock = mEntries.GetEntry(mFirstBlock)->mPrevBlock;
|
|
|
|
mEntries.GetEntry(entry->mNextBlock)->mPrevBlock = aBlock;
|
|
|
|
mEntries.GetEntry(entry->mPrevBlock)->mNextBlock = aBlock;
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
mFirstBlock = aBlock;
|
|
|
|
++mCount;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaCacheStream::BlockList::AddAfter(int32_t aBlock, int32_t aBefore)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2009-09-15 02:30:44 +00:00
|
|
|
NS_ASSERTION(!mEntries.GetEntry(aBlock), "Block already in list");
|
|
|
|
Entry* entry = mEntries.PutEntry(aBlock);
|
|
|
|
|
|
|
|
Entry* addAfter = mEntries.GetEntry(aBefore);
|
|
|
|
NS_ASSERTION(addAfter, "aBefore not in list");
|
|
|
|
|
|
|
|
entry->mNextBlock = addAfter->mNextBlock;
|
|
|
|
entry->mPrevBlock = aBefore;
|
|
|
|
mEntries.GetEntry(entry->mNextBlock)->mPrevBlock = aBlock;
|
|
|
|
mEntries.GetEntry(entry->mPrevBlock)->mNextBlock = aBlock;
|
2009-04-01 00:52:56 +00:00
|
|
|
++mCount;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaCacheStream::BlockList::RemoveBlock(int32_t aBlock)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2009-09-15 02:30:44 +00:00
|
|
|
Entry* entry = mEntries.GetEntry(aBlock);
|
|
|
|
NS_ASSERTION(entry, "Block not in list");
|
|
|
|
|
|
|
|
if (entry->mNextBlock == aBlock) {
|
|
|
|
NS_ASSERTION(entry->mPrevBlock == aBlock, "Linked list inconsistency");
|
2009-04-01 00:52:56 +00:00
|
|
|
NS_ASSERTION(mFirstBlock == aBlock, "Linked list inconsistency");
|
|
|
|
mFirstBlock = -1;
|
|
|
|
} else {
|
|
|
|
if (mFirstBlock == aBlock) {
|
2009-09-15 02:30:44 +00:00
|
|
|
mFirstBlock = entry->mNextBlock;
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
2009-09-15 02:30:44 +00:00
|
|
|
mEntries.GetEntry(entry->mNextBlock)->mPrevBlock = entry->mPrevBlock;
|
|
|
|
mEntries.GetEntry(entry->mPrevBlock)->mNextBlock = entry->mNextBlock;
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
2009-09-15 02:30:44 +00:00
|
|
|
mEntries.RemoveEntry(aBlock);
|
2009-04-01 00:52:56 +00:00
|
|
|
--mCount;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
int32_t MediaCacheStream::BlockList::GetLastBlock() const
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
|
|
|
if (mFirstBlock < 0)
|
|
|
|
return -1;
|
2009-09-15 02:30:44 +00:00
|
|
|
return mEntries.GetEntry(mFirstBlock)->mPrevBlock;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
int32_t MediaCacheStream::BlockList::GetNextBlock(int32_t aBlock) const
|
2009-09-15 02:30:44 +00:00
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t block = mEntries.GetEntry(aBlock)->mNextBlock;
|
2009-09-15 02:30:44 +00:00
|
|
|
if (block == mFirstBlock)
|
|
|
|
return -1;
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
int32_t MediaCacheStream::BlockList::GetPrevBlock(int32_t aBlock) const
|
2009-09-15 02:30:44 +00:00
|
|
|
{
|
|
|
|
if (aBlock == mFirstBlock)
|
|
|
|
return -1;
|
|
|
|
return mEntries.GetEntry(aBlock)->mPrevBlock;
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2012-11-14 19:46:40 +00:00
|
|
|
void MediaCacheStream::BlockList::Verify()
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t count = 0;
|
2009-04-01 00:52:56 +00:00
|
|
|
if (mFirstBlock >= 0) {
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t block = mFirstBlock;
|
2009-04-01 00:52:56 +00:00
|
|
|
do {
|
2009-09-15 02:30:44 +00:00
|
|
|
Entry* entry = mEntries.GetEntry(block);
|
|
|
|
NS_ASSERTION(mEntries.GetEntry(entry->mNextBlock)->mPrevBlock == block,
|
2009-04-01 00:52:56 +00:00
|
|
|
"Bad prev link");
|
2009-09-15 02:30:44 +00:00
|
|
|
NS_ASSERTION(mEntries.GetEntry(entry->mPrevBlock)->mNextBlock == block,
|
|
|
|
"Bad next link");
|
|
|
|
block = entry->mNextBlock;
|
2009-04-01 00:52:56 +00:00
|
|
|
++count;
|
|
|
|
} while (block != mFirstBlock);
|
|
|
|
}
|
|
|
|
NS_ASSERTION(count == mCount, "Bad count");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
static void UpdateSwappedBlockIndex(int32_t* aBlockIndex,
|
|
|
|
int32_t aBlock1Index, int32_t aBlock2Index)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t index = *aBlockIndex;
|
2009-04-01 00:52:56 +00:00
|
|
|
if (index == aBlock1Index) {
|
|
|
|
*aBlockIndex = aBlock2Index;
|
|
|
|
} else if (index == aBlock2Index) {
|
|
|
|
*aBlockIndex = aBlock1Index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream::BlockList::NotifyBlockSwapped(int32_t aBlockIndex1,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t aBlockIndex2)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2009-09-15 02:30:44 +00:00
|
|
|
Entry* e1 = mEntries.GetEntry(aBlockIndex1);
|
|
|
|
Entry* e2 = mEntries.GetEntry(aBlockIndex2);
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t e1Prev = -1, e1Next = -1, e2Prev = -1, e2Next = -1;
|
2009-09-15 02:30:44 +00:00
|
|
|
|
|
|
|
// Fix mFirstBlock
|
2009-04-01 00:52:56 +00:00
|
|
|
UpdateSwappedBlockIndex(&mFirstBlock, aBlockIndex1, aBlockIndex2);
|
2009-09-15 02:30:44 +00:00
|
|
|
|
|
|
|
// Fix mNextBlock/mPrevBlock links. First capture previous/next links
|
|
|
|
// so we don't get confused due to aliasing.
|
|
|
|
if (e1) {
|
|
|
|
e1Prev = e1->mPrevBlock;
|
|
|
|
e1Next = e1->mNextBlock;
|
|
|
|
}
|
|
|
|
if (e2) {
|
|
|
|
e2Prev = e2->mPrevBlock;
|
|
|
|
e2Next = e2->mNextBlock;
|
|
|
|
}
|
|
|
|
// Update the entries.
|
|
|
|
if (e1) {
|
|
|
|
mEntries.GetEntry(e1Prev)->mNextBlock = aBlockIndex2;
|
|
|
|
mEntries.GetEntry(e1Next)->mPrevBlock = aBlockIndex2;
|
|
|
|
}
|
|
|
|
if (e2) {
|
|
|
|
mEntries.GetEntry(e2Prev)->mNextBlock = aBlockIndex1;
|
|
|
|
mEntries.GetEntry(e2Next)->mPrevBlock = aBlockIndex1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fix hashtable keys. First remove stale entries.
|
|
|
|
if (e1) {
|
|
|
|
e1Prev = e1->mPrevBlock;
|
|
|
|
e1Next = e1->mNextBlock;
|
|
|
|
mEntries.RemoveEntry(aBlockIndex1);
|
2009-10-23 02:18:54 +00:00
|
|
|
// Refresh pointer after hashtable mutation.
|
|
|
|
e2 = mEntries.GetEntry(aBlockIndex2);
|
2009-09-15 02:30:44 +00:00
|
|
|
}
|
|
|
|
if (e2) {
|
|
|
|
e2Prev = e2->mPrevBlock;
|
|
|
|
e2Next = e2->mNextBlock;
|
|
|
|
mEntries.RemoveEntry(aBlockIndex2);
|
|
|
|
}
|
|
|
|
// Put new entries back.
|
|
|
|
if (e1) {
|
|
|
|
e1 = mEntries.PutEntry(aBlockIndex2);
|
|
|
|
e1->mNextBlock = e1Next;
|
|
|
|
e1->mPrevBlock = e1Prev;
|
|
|
|
}
|
|
|
|
if (e2) {
|
|
|
|
e2 = mEntries.PutEntry(aBlockIndex1);
|
|
|
|
e2->mNextBlock = e2Next;
|
|
|
|
e2->mPrevBlock = e2Prev;
|
|
|
|
}
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::Init()
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
2012-03-28 00:04:26 +00:00
|
|
|
NS_ASSERTION(!mFileCache, "Cache file already open?");
|
2009-04-01 00:52:56 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
PRFileDesc* fileDesc = nullptr;
|
2012-09-25 00:50:30 +00:00
|
|
|
nsresult rv = NS_OpenAnonymousTemporaryFile(&fileDesc);
|
2012-03-28 00:04:26 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv,rv);
|
|
|
|
|
|
|
|
mFileCache = new FileBlockCache();
|
|
|
|
rv = mFileCache->Open(fileDesc);
|
2010-10-26 01:21:16 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv,rv);
|
2009-04-01 00:52:56 +00:00
|
|
|
|
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (!gMediaCacheLog) {
|
2012-11-14 19:46:40 +00:00
|
|
|
gMediaCacheLog = PR_NewLogModule("MediaCache");
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheFlusher::Init();
|
2010-09-29 00:02:08 +00:00
|
|
|
|
2009-04-01 00:52:56 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-10-13 14:14:08 +00:00
|
|
|
void
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::Flush()
|
2010-10-13 14:14:08 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
|
|
|
if (!gMediaCache)
|
|
|
|
return;
|
|
|
|
|
|
|
|
gMediaCache->FlushInternal();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::FlushInternal()
|
2010-10-13 14:14:08 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2010-10-13 14:14:08 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t blockIndex = 0; blockIndex < mIndex.Length(); ++blockIndex) {
|
2010-10-13 14:14:08 +00:00
|
|
|
FreeBlock(blockIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Truncate file, close it, and reopen
|
|
|
|
Truncate();
|
|
|
|
NS_ASSERTION(mIndex.Length() == 0, "Blocks leaked?");
|
2012-03-28 00:04:26 +00:00
|
|
|
if (mFileCache) {
|
|
|
|
mFileCache->Close();
|
2012-07-30 14:20:58 +00:00
|
|
|
mFileCache = nullptr;
|
2010-10-13 14:14:08 +00:00
|
|
|
}
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
2009-04-01 00:52:56 +00:00
|
|
|
void
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::MaybeShutdown()
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2010-10-26 01:21:16 +00:00
|
|
|
NS_ASSERTION(NS_IsMainThread(),
|
2012-11-14 19:46:40 +00:00
|
|
|
"MediaCache::MaybeShutdown called on non-main thread");
|
2009-04-01 00:52:56 +00:00
|
|
|
if (!gMediaCache->mStreams.IsEmpty()) {
|
|
|
|
// Don't shut down yet, streams are still alive
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since we're on the main thread, no-one is going to add a new stream
|
|
|
|
// while we shut down.
|
|
|
|
// This function is static so we don't have to delete 'this'.
|
|
|
|
delete gMediaCache;
|
2012-07-30 14:20:58 +00:00
|
|
|
gMediaCache = nullptr;
|
2010-09-29 00:02:08 +00:00
|
|
|
NS_IF_RELEASE(gMediaCacheFlusher);
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
InitMediaCache()
|
|
|
|
{
|
|
|
|
if (gMediaCache)
|
|
|
|
return;
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
gMediaCache = new MediaCache();
|
2009-04-01 00:52:56 +00:00
|
|
|
if (!gMediaCache)
|
|
|
|
return;
|
|
|
|
|
|
|
|
nsresult rv = gMediaCache->Init();
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
delete gMediaCache;
|
2012-07-30 14:20:58 +00:00
|
|
|
gMediaCache = nullptr;
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::ReadCacheFile(int64_t aOffset, void* aData, int32_t aLength,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t* aBytes)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
mReentrantMonitor.AssertCurrentThreadIn();
|
2009-04-01 00:52:56 +00:00
|
|
|
|
2012-03-28 00:04:26 +00:00
|
|
|
if (!mFileCache)
|
2009-04-01 00:52:56 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
return mFileCache->Read(aOffset, reinterpret_cast<uint8_t*>(aData), aLength, aBytes);
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::ReadCacheFileAllBytes(int64_t aOffset, void* aData, int32_t aLength)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
mReentrantMonitor.AssertCurrentThreadIn();
|
2009-04-01 00:52:56 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t offset = aOffset;
|
|
|
|
int32_t count = aLength;
|
2009-04-01 00:52:56 +00:00
|
|
|
// Cast to char* so we can do byte-wise pointer arithmetic
|
|
|
|
char* data = static_cast<char*>(aData);
|
|
|
|
while (count > 0) {
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t bytes;
|
2009-04-01 00:52:56 +00:00
|
|
|
nsresult rv = ReadCacheFile(offset, data, count, &bytes);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
if (bytes == 0)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
count -= bytes;
|
|
|
|
data += bytes;
|
|
|
|
offset += bytes;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
static int32_t GetMaxBlocks()
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
|
|
|
// We look up the cache size every time. This means dynamic changes
|
|
|
|
// to the pref are applied.
|
|
|
|
// Cache size is in KB
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t cacheSize = Preferences::GetInt("media.cache_size", 500*1024);
|
2012-11-14 19:46:40 +00:00
|
|
|
int64_t maxBlocks = static_cast<int64_t>(cacheSize)*1024/MediaCache::BLOCK_SIZE;
|
2013-01-15 12:22:03 +00:00
|
|
|
maxBlocks = std::max<int64_t>(maxBlocks, 1);
|
|
|
|
return int32_t(std::min<int64_t>(maxBlocks, INT32_MAX));
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::FindBlockForIncomingData(TimeStamp aNow,
|
|
|
|
MediaCacheStream* aStream)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
mReentrantMonitor.AssertCurrentThreadIn();
|
2009-04-01 00:52:56 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t blockIndex = FindReusableBlock(aNow, aStream,
|
2012-09-28 06:57:33 +00:00
|
|
|
aStream->mChannelOffset/BLOCK_SIZE, INT32_MAX);
|
2009-04-01 00:52:56 +00:00
|
|
|
|
2009-09-15 02:30:44 +00:00
|
|
|
if (blockIndex < 0 || !IsBlockFree(blockIndex)) {
|
2009-04-01 00:52:56 +00:00
|
|
|
// The block returned is already allocated.
|
|
|
|
// Don't reuse it if a) there's room to expand the cache or
|
|
|
|
// b) the data we're going to store in the free block is not higher
|
|
|
|
// priority than the data already stored in the free block.
|
|
|
|
// The latter can lead us to go over the cache limit a bit.
|
2012-08-22 15:56:38 +00:00
|
|
|
if ((mIndex.Length() < uint32_t(GetMaxBlocks()) || blockIndex < 0 ||
|
2009-04-01 00:52:56 +00:00
|
|
|
PredictNextUseForIncomingData(aStream) >= PredictNextUse(aNow, blockIndex))) {
|
|
|
|
blockIndex = mIndex.Length();
|
|
|
|
if (!mIndex.AppendElement())
|
|
|
|
return -1;
|
|
|
|
mFreeBlocks.AddFirstBlock(blockIndex);
|
|
|
|
return blockIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return blockIndex;
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::BlockIsReusable(int32_t aBlockIndex)
|
2009-09-15 02:30:44 +00:00
|
|
|
{
|
|
|
|
Block* block = &mIndex[aBlockIndex];
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < block->mOwners.Length(); ++i) {
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream* stream = block->mOwners[i].mStream;
|
2009-10-15 02:20:49 +00:00
|
|
|
if (stream->mPinCount > 0 ||
|
2009-09-15 02:30:44 +00:00
|
|
|
stream->mStreamOffset/BLOCK_SIZE == block->mOwners[i].mStreamBlock) {
|
2011-09-29 23:34:37 +00:00
|
|
|
return false;
|
2009-09-15 02:30:44 +00:00
|
|
|
}
|
|
|
|
}
|
2011-09-29 23:34:37 +00:00
|
|
|
return true;
|
2009-09-15 02:30:44 +00:00
|
|
|
}
|
|
|
|
|
2009-04-01 00:52:56 +00:00
|
|
|
void
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::AppendMostReusableBlock(BlockList* aBlockList,
|
2012-08-22 15:56:38 +00:00
|
|
|
nsTArray<uint32_t>* aResult,
|
|
|
|
int32_t aBlockIndexLimit)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
mReentrantMonitor.AssertCurrentThreadIn();
|
2009-04-01 00:52:56 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t blockIndex = aBlockList->GetLastBlock();
|
2009-09-15 02:30:44 +00:00
|
|
|
if (blockIndex < 0)
|
2009-04-01 00:52:56 +00:00
|
|
|
return;
|
|
|
|
do {
|
|
|
|
// Don't consider blocks for pinned streams, or blocks that are
|
2009-09-15 02:30:44 +00:00
|
|
|
// beyond the specified limit, or a block that contains a stream's
|
2009-04-01 00:52:56 +00:00
|
|
|
// current read position (such a block contains both played data
|
|
|
|
// and readahead data)
|
2009-09-15 02:30:44 +00:00
|
|
|
if (blockIndex < aBlockIndexLimit && BlockIsReusable(blockIndex)) {
|
2009-04-01 00:52:56 +00:00
|
|
|
aResult->AppendElement(blockIndex);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-15 02:30:44 +00:00
|
|
|
blockIndex = aBlockList->GetPrevBlock(blockIndex);
|
|
|
|
} while (blockIndex >= 0);
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::FindReusableBlock(TimeStamp aNow,
|
|
|
|
MediaCacheStream* aForStream,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t aForStreamBlock,
|
|
|
|
int32_t aMaxSearchBlockIndex)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
mReentrantMonitor.AssertCurrentThreadIn();
|
2009-04-01 00:52:56 +00:00
|
|
|
|
2013-01-15 12:22:03 +00:00
|
|
|
uint32_t length = std::min(uint32_t(aMaxSearchBlockIndex), mIndex.Length());
|
2009-04-01 00:52:56 +00:00
|
|
|
|
|
|
|
if (aForStream && aForStreamBlock > 0 &&
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t(aForStreamBlock) <= aForStream->mBlocks.Length()) {
|
|
|
|
int32_t prevCacheBlock = aForStream->mBlocks[aForStreamBlock - 1];
|
2009-04-01 00:52:56 +00:00
|
|
|
if (prevCacheBlock >= 0) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t freeBlockScanEnd =
|
2013-01-15 12:22:03 +00:00
|
|
|
std::min(length, prevCacheBlock + FREE_BLOCK_SCAN_LIMIT);
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = prevCacheBlock; i < freeBlockScanEnd; ++i) {
|
2009-09-15 02:30:44 +00:00
|
|
|
if (IsBlockFree(i))
|
2009-04-01 00:52:56 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mFreeBlocks.IsEmpty()) {
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t blockIndex = mFreeBlocks.GetFirstBlock();
|
2009-04-01 00:52:56 +00:00
|
|
|
do {
|
|
|
|
if (blockIndex < aMaxSearchBlockIndex)
|
|
|
|
return blockIndex;
|
2009-09-15 02:30:44 +00:00
|
|
|
blockIndex = mFreeBlocks.GetNextBlock(blockIndex);
|
|
|
|
} while (blockIndex >= 0);
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build a list of the blocks we should consider for the "latest
|
|
|
|
// predicted time of next use". We can exploit the fact that the block
|
|
|
|
// linked lists are ordered by increasing time of next use. This is
|
|
|
|
// actually the whole point of having the linked lists.
|
2012-08-22 15:56:38 +00:00
|
|
|
nsAutoTArray<uint32_t,8> candidates;
|
|
|
|
for (uint32_t i = 0; i < mStreams.Length(); ++i) {
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream* stream = mStreams[i];
|
2009-09-15 02:30:44 +00:00
|
|
|
if (stream->mPinCount > 0) {
|
|
|
|
// No point in even looking at this stream's blocks
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
AppendMostReusableBlock(&stream->mMetadataBlocks, &candidates, length);
|
|
|
|
AppendMostReusableBlock(&stream->mPlayedBlocks, &candidates, length);
|
|
|
|
|
|
|
|
// Don't consider readahead blocks in non-seekable streams. If we
|
|
|
|
// remove the block we won't be able to seek back to read it later.
|
2012-11-30 13:17:54 +00:00
|
|
|
if (stream->mIsTransportSeekable) {
|
2009-09-15 02:30:44 +00:00
|
|
|
AppendMostReusableBlock(&stream->mReadaheadBlocks, &candidates, length);
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-11 09:39:02 +00:00
|
|
|
TimeDuration latestUse;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t latestUseBlock = -1;
|
|
|
|
for (uint32_t i = 0; i < candidates.Length(); ++i) {
|
2009-04-11 09:39:02 +00:00
|
|
|
TimeDuration nextUse = PredictNextUse(aNow, candidates[i]);
|
2009-04-01 00:52:56 +00:00
|
|
|
if (nextUse > latestUse) {
|
|
|
|
latestUse = nextUse;
|
|
|
|
latestUseBlock = candidates[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return latestUseBlock;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::BlockList*
|
|
|
|
MediaCache::GetListForBlock(BlockOwner* aBlock)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
|
|
|
switch (aBlock->mClass) {
|
|
|
|
case METADATA_BLOCK:
|
|
|
|
NS_ASSERTION(aBlock->mStream, "Metadata block has no stream?");
|
2009-09-15 02:30:44 +00:00
|
|
|
return &aBlock->mStream->mMetadataBlocks;
|
2009-04-01 00:52:56 +00:00
|
|
|
case PLAYED_BLOCK:
|
|
|
|
NS_ASSERTION(aBlock->mStream, "Metadata block has no stream?");
|
2009-09-15 02:30:44 +00:00
|
|
|
return &aBlock->mStream->mPlayedBlocks;
|
2009-04-01 00:52:56 +00:00
|
|
|
case READAHEAD_BLOCK:
|
|
|
|
NS_ASSERTION(aBlock->mStream, "Readahead block has no stream?");
|
|
|
|
return &aBlock->mStream->mReadaheadBlocks;
|
|
|
|
default:
|
|
|
|
NS_ERROR("Invalid block class");
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::BlockOwner*
|
|
|
|
MediaCache::GetBlockOwner(int32_t aBlockIndex, MediaCacheStream* aStream)
|
2009-09-15 02:30:44 +00:00
|
|
|
{
|
|
|
|
Block* block = &mIndex[aBlockIndex];
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < block->mOwners.Length(); ++i) {
|
2009-09-15 02:30:44 +00:00
|
|
|
if (block->mOwners[i].mStream == aStream)
|
|
|
|
return &block->mOwners[i];
|
|
|
|
}
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2009-09-15 02:30:44 +00:00
|
|
|
}
|
|
|
|
|
2009-04-01 00:52:56 +00:00
|
|
|
void
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::SwapBlocks(int32_t aBlockIndex1, int32_t aBlockIndex2)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
mReentrantMonitor.AssertCurrentThreadIn();
|
2009-04-01 00:52:56 +00:00
|
|
|
|
|
|
|
Block* block1 = &mIndex[aBlockIndex1];
|
|
|
|
Block* block2 = &mIndex[aBlockIndex2];
|
|
|
|
|
2009-09-15 02:30:44 +00:00
|
|
|
block1->mOwners.SwapElements(block2->mOwners);
|
2009-04-01 00:52:56 +00:00
|
|
|
|
|
|
|
// Now all references to block1 have to be replaced with block2 and
|
2009-09-15 02:30:44 +00:00
|
|
|
// vice versa.
|
|
|
|
// First update stream references to blocks via mBlocks.
|
|
|
|
const Block* blocks[] = { block1, block2 };
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t blockIndices[] = { aBlockIndex1, aBlockIndex2 };
|
|
|
|
for (int32_t i = 0; i < 2; ++i) {
|
|
|
|
for (uint32_t j = 0; j < blocks[i]->mOwners.Length(); ++j) {
|
2009-09-15 02:30:44 +00:00
|
|
|
const BlockOwner* b = &blocks[i]->mOwners[j];
|
|
|
|
b->mStream->mBlocks[b->mStreamBlock] = blockIndices[i];
|
|
|
|
}
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
|
2009-09-15 02:30:44 +00:00
|
|
|
// Now update references to blocks in block lists.
|
|
|
|
mFreeBlocks.NotifyBlockSwapped(aBlockIndex1, aBlockIndex2);
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
nsTHashtable<nsPtrHashKey<MediaCacheStream> > visitedStreams;
|
2009-09-15 02:30:44 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
for (int32_t i = 0; i < 2; ++i) {
|
|
|
|
for (uint32_t j = 0; j < blocks[i]->mOwners.Length(); ++j) {
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream* stream = blocks[i]->mOwners[j].mStream;
|
2009-09-15 02:30:44 +00:00
|
|
|
// Make sure that we don't update the same stream twice --- that
|
|
|
|
// would result in swapping the block references back again!
|
|
|
|
if (visitedStreams.GetEntry(stream))
|
|
|
|
continue;
|
|
|
|
visitedStreams.PutEntry(stream);
|
|
|
|
stream->mReadaheadBlocks.NotifyBlockSwapped(aBlockIndex1, aBlockIndex2);
|
|
|
|
stream->mPlayedBlocks.NotifyBlockSwapped(aBlockIndex1, aBlockIndex2);
|
|
|
|
stream->mMetadataBlocks.NotifyBlockSwapped(aBlockIndex1, aBlockIndex2);
|
|
|
|
}
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Verify();
|
|
|
|
}
|
|
|
|
|
2009-09-15 02:30:44 +00:00
|
|
|
void
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::RemoveBlockOwner(int32_t aBlockIndex, MediaCacheStream* aStream)
|
2009-09-15 02:30:44 +00:00
|
|
|
{
|
|
|
|
Block* block = &mIndex[aBlockIndex];
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < block->mOwners.Length(); ++i) {
|
2009-09-15 02:30:44 +00:00
|
|
|
BlockOwner* bo = &block->mOwners[i];
|
|
|
|
if (bo->mStream == aStream) {
|
|
|
|
GetListForBlock(bo)->RemoveBlock(aBlockIndex);
|
|
|
|
bo->mStream->mBlocks[bo->mStreamBlock] = -1;
|
|
|
|
block->mOwners.RemoveElementAt(i);
|
|
|
|
if (block->mOwners.IsEmpty()) {
|
|
|
|
mFreeBlocks.AddFirstBlock(aBlockIndex);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-15 02:30:44 +00:00
|
|
|
void
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::AddBlockOwnerAsReadahead(int32_t aBlockIndex,
|
|
|
|
MediaCacheStream* aStream,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t aStreamBlockIndex)
|
2009-09-15 02:30:44 +00:00
|
|
|
{
|
|
|
|
Block* block = &mIndex[aBlockIndex];
|
|
|
|
if (block->mOwners.IsEmpty()) {
|
|
|
|
mFreeBlocks.RemoveBlock(aBlockIndex);
|
|
|
|
}
|
|
|
|
BlockOwner* bo = block->mOwners.AppendElement();
|
|
|
|
bo->mStream = aStream;
|
|
|
|
bo->mStreamBlock = aStreamBlockIndex;
|
|
|
|
aStream->mBlocks[aStreamBlockIndex] = aBlockIndex;
|
|
|
|
bo->mClass = READAHEAD_BLOCK;
|
|
|
|
InsertReadaheadBlock(bo, aBlockIndex);
|
|
|
|
}
|
|
|
|
|
2009-04-01 00:52:56 +00:00
|
|
|
void
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::FreeBlock(int32_t aBlock)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
mReentrantMonitor.AssertCurrentThreadIn();
|
2009-04-01 00:52:56 +00:00
|
|
|
|
|
|
|
Block* block = &mIndex[aBlock];
|
2009-09-15 02:30:44 +00:00
|
|
|
if (block->mOwners.IsEmpty()) {
|
|
|
|
// already free
|
|
|
|
return;
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
2009-09-15 02:30:44 +00:00
|
|
|
|
2013-11-21 03:02:42 +00:00
|
|
|
CACHE_LOG(PR_LOG_DEBUG, ("Released block %d", aBlock));
|
2009-09-15 02:30:44 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < block->mOwners.Length(); ++i) {
|
2009-09-15 02:30:44 +00:00
|
|
|
BlockOwner* bo = &block->mOwners[i];
|
|
|
|
GetListForBlock(bo)->RemoveBlock(aBlock);
|
|
|
|
bo->mStream->mBlocks[bo->mStreamBlock] = -1;
|
|
|
|
}
|
|
|
|
block->mOwners.Clear();
|
2009-04-01 00:52:56 +00:00
|
|
|
mFreeBlocks.AddFirstBlock(aBlock);
|
|
|
|
Verify();
|
|
|
|
}
|
|
|
|
|
2009-04-11 09:39:02 +00:00
|
|
|
TimeDuration
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::PredictNextUse(TimeStamp aNow, int32_t aBlock)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
mReentrantMonitor.AssertCurrentThreadIn();
|
2009-09-15 02:30:44 +00:00
|
|
|
NS_ASSERTION(!IsBlockFree(aBlock), "aBlock is free");
|
2009-04-01 00:52:56 +00:00
|
|
|
|
|
|
|
Block* block = &mIndex[aBlock];
|
2009-09-15 02:30:44 +00:00
|
|
|
// Blocks can be belong to multiple streams. The predicted next use
|
|
|
|
// time is the earliest time predicted by any of the streams.
|
|
|
|
TimeDuration result;
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < block->mOwners.Length(); ++i) {
|
2009-09-15 02:30:44 +00:00
|
|
|
BlockOwner* bo = &block->mOwners[i];
|
|
|
|
TimeDuration prediction;
|
|
|
|
switch (bo->mClass) {
|
|
|
|
case METADATA_BLOCK:
|
|
|
|
// This block should be managed in LRU mode. For metadata we predict
|
|
|
|
// that the time until the next use is the time since the last use.
|
|
|
|
prediction = aNow - bo->mLastUseTime;
|
|
|
|
break;
|
2013-06-04 13:52:57 +00:00
|
|
|
case PLAYED_BLOCK: {
|
2009-09-15 02:30:44 +00:00
|
|
|
// This block should be managed in LRU mode, and we should impose
|
|
|
|
// a "replay delay" to reflect the likelihood of replay happening
|
2012-08-22 15:56:38 +00:00
|
|
|
NS_ASSERTION(static_cast<int64_t>(bo->mStreamBlock)*BLOCK_SIZE <
|
2009-09-15 02:30:44 +00:00
|
|
|
bo->mStream->mStreamOffset,
|
|
|
|
"Played block after the current stream position?");
|
2013-06-04 13:52:57 +00:00
|
|
|
int64_t bytesBehind =
|
|
|
|
bo->mStream->mStreamOffset - static_cast<int64_t>(bo->mStreamBlock)*BLOCK_SIZE;
|
|
|
|
int64_t millisecondsBehind =
|
|
|
|
bytesBehind*1000/bo->mStream->mPlaybackBytesPerSecond;
|
|
|
|
prediction = TimeDuration::FromMilliseconds(
|
2013-06-05 02:44:20 +00:00
|
|
|
std::min<int64_t>(millisecondsBehind*REPLAY_PENALTY_FACTOR, INT32_MAX));
|
2009-09-15 02:30:44 +00:00
|
|
|
break;
|
2013-06-04 13:52:57 +00:00
|
|
|
}
|
2009-09-15 02:30:44 +00:00
|
|
|
case READAHEAD_BLOCK: {
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t bytesAhead =
|
|
|
|
static_cast<int64_t>(bo->mStreamBlock)*BLOCK_SIZE - bo->mStream->mStreamOffset;
|
2009-09-15 02:30:44 +00:00
|
|
|
NS_ASSERTION(bytesAhead >= 0,
|
|
|
|
"Readahead block before the current stream position?");
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t millisecondsAhead =
|
2009-09-15 02:30:44 +00:00
|
|
|
bytesAhead*1000/bo->mStream->mPlaybackBytesPerSecond;
|
|
|
|
prediction = TimeDuration::FromMilliseconds(
|
2013-01-15 12:22:03 +00:00
|
|
|
std::min<int64_t>(millisecondsAhead, INT32_MAX));
|
2009-09-15 02:30:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
NS_ERROR("Invalid class for predicting next use");
|
|
|
|
return TimeDuration(0);
|
|
|
|
}
|
|
|
|
if (i == 0 || prediction < result) {
|
|
|
|
result = prediction;
|
|
|
|
}
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
2009-09-15 02:30:44 +00:00
|
|
|
return result;
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
|
2009-04-11 09:39:02 +00:00
|
|
|
TimeDuration
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::PredictNextUseForIncomingData(MediaCacheStream* aStream)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
mReentrantMonitor.AssertCurrentThreadIn();
|
2009-04-01 00:52:56 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t bytesAhead = aStream->mChannelOffset - aStream->mStreamOffset;
|
2009-04-01 00:52:56 +00:00
|
|
|
if (bytesAhead <= -BLOCK_SIZE) {
|
|
|
|
// Hmm, no idea when data behind us will be used. Guess 24 hours.
|
2009-04-11 09:39:02 +00:00
|
|
|
return TimeDuration::FromSeconds(24*60*60);
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
if (bytesAhead <= 0)
|
2009-04-11 09:39:02 +00:00
|
|
|
return TimeDuration(0);
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t millisecondsAhead = bytesAhead*1000/aStream->mPlaybackBytesPerSecond;
|
2009-04-11 09:39:02 +00:00
|
|
|
return TimeDuration::FromMilliseconds(
|
2013-01-15 12:22:03 +00:00
|
|
|
std::min<int64_t>(millisecondsAhead, INT32_MAX));
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
|
2011-12-02 04:43:42 +00:00
|
|
|
enum StreamAction { NONE, SEEK, SEEK_AND_RESUME, RESUME, SUSPEND };
|
2009-10-09 11:46:23 +00:00
|
|
|
|
2009-04-01 00:52:56 +00:00
|
|
|
void
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::Update()
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
2009-10-09 11:46:23 +00:00
|
|
|
// The action to use for each stream. We store these so we can make
|
|
|
|
// decisions while holding the cache lock but implement those decisions
|
|
|
|
// without holding the cache lock, since we need to call out to
|
|
|
|
// stream, decoder and element code.
|
|
|
|
nsAutoTArray<StreamAction,10> actions;
|
|
|
|
|
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2011-09-29 23:34:37 +00:00
|
|
|
mUpdateQueued = false;
|
2009-04-01 00:52:56 +00:00
|
|
|
#ifdef DEBUG
|
2011-09-29 23:34:37 +00:00
|
|
|
mInUpdate = true;
|
2009-04-01 00:52:56 +00:00
|
|
|
#endif
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t maxBlocks = GetMaxBlocks();
|
2009-10-09 11:46:23 +00:00
|
|
|
TimeStamp now = TimeStamp::Now();
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t freeBlockCount = mFreeBlocks.GetCount();
|
2009-10-09 11:46:23 +00:00
|
|
|
TimeDuration latestPredictedUseForOverflow = 0;
|
2013-07-19 09:00:19 +00:00
|
|
|
if (mIndex.Length() > uint32_t(maxBlocks)) {
|
|
|
|
// Try to trim back the cache to its desired maximum size. The cache may
|
|
|
|
// have overflowed simply due to data being received when we have
|
|
|
|
// no blocks in the main part of the cache that are free or lower
|
|
|
|
// priority than the new data. The cache can also be overflowing because
|
|
|
|
// the media.cache_size preference was reduced.
|
|
|
|
// First, figure out what the least valuable block in the cache overflow
|
|
|
|
// is. We don't want to replace any blocks in the main part of the
|
|
|
|
// cache whose expected time of next use is earlier or equal to that.
|
|
|
|
// If we allow that, we can effectively end up discarding overflowing
|
|
|
|
// blocks (by moving an overflowing block to the main part of the cache,
|
|
|
|
// and then overwriting it with another overflowing block), and we try
|
|
|
|
// to avoid that since it requires HTTP seeks.
|
|
|
|
// We also use this loop to eliminate overflowing blocks from
|
|
|
|
// freeBlockCount.
|
|
|
|
for (int32_t blockIndex = mIndex.Length() - 1; blockIndex >= maxBlocks;
|
|
|
|
--blockIndex) {
|
|
|
|
if (IsBlockFree(blockIndex)) {
|
|
|
|
// Don't count overflowing free blocks in our free block count
|
|
|
|
--freeBlockCount;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
TimeDuration predictedUse = PredictNextUse(now, blockIndex);
|
|
|
|
latestPredictedUseForOverflow = std::max(latestPredictedUseForOverflow, predictedUse);
|
2009-10-09 11:46:23 +00:00
|
|
|
}
|
2013-07-19 09:00:19 +00:00
|
|
|
} else {
|
|
|
|
freeBlockCount += maxBlocks - mIndex.Length();
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
|
2009-10-09 11:46:23 +00:00
|
|
|
// Now try to move overflowing blocks to the main part of the cache.
|
2012-08-22 15:56:38 +00:00
|
|
|
for (int32_t blockIndex = mIndex.Length() - 1; blockIndex >= maxBlocks;
|
2009-10-09 11:46:23 +00:00
|
|
|
--blockIndex) {
|
|
|
|
if (IsBlockFree(blockIndex))
|
|
|
|
continue;
|
2009-04-01 00:52:56 +00:00
|
|
|
|
2009-10-09 11:46:23 +00:00
|
|
|
Block* block = &mIndex[blockIndex];
|
|
|
|
// Try to relocate the block close to other blocks for the first stream.
|
|
|
|
// There is no point in trying to make it close to other blocks in
|
|
|
|
// *all* the streams it might belong to.
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t destinationBlockIndex =
|
2009-10-09 11:46:23 +00:00
|
|
|
FindReusableBlock(now, block->mOwners[0].mStream,
|
|
|
|
block->mOwners[0].mStreamBlock, maxBlocks);
|
|
|
|
if (destinationBlockIndex < 0) {
|
|
|
|
// Nowhere to place this overflow block. We won't be able to
|
|
|
|
// place any more overflow blocks.
|
|
|
|
break;
|
|
|
|
}
|
2009-04-01 00:52:56 +00:00
|
|
|
|
2009-10-09 11:46:23 +00:00
|
|
|
if (IsBlockFree(destinationBlockIndex) ||
|
|
|
|
PredictNextUse(now, destinationBlockIndex) > latestPredictedUseForOverflow) {
|
|
|
|
// Reuse blocks in the main part of the cache that are less useful than
|
|
|
|
// the least useful overflow blocks
|
2012-03-28 00:04:26 +00:00
|
|
|
|
|
|
|
nsresult rv = mFileCache->MoveBlock(blockIndex, destinationBlockIndex);
|
|
|
|
|
2009-04-01 00:52:56 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2012-03-28 00:04:26 +00:00
|
|
|
// We successfully copied the file data.
|
2013-11-21 03:02:42 +00:00
|
|
|
CACHE_LOG(PR_LOG_DEBUG, ("Swapping blocks %d and %d (trimming cache)",
|
|
|
|
blockIndex, destinationBlockIndex));
|
2012-03-28 00:04:26 +00:00
|
|
|
// 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.
|
2013-11-21 03:02:42 +00:00
|
|
|
CACHE_LOG(PR_LOG_DEBUG, ("Released block %d (trimming cache)", blockIndex));
|
2009-10-09 11:46:23 +00:00
|
|
|
FreeBlock(blockIndex);
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
2010-07-20 01:29:27 +00:00
|
|
|
} else {
|
2013-11-21 03:02:42 +00:00
|
|
|
CACHE_LOG(PR_LOG_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()));
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-09 11:46:23 +00:00
|
|
|
// Try chopping back the array of cache entries and the cache file.
|
|
|
|
Truncate();
|
2009-04-01 00:52:56 +00:00
|
|
|
|
2009-10-09 11:46:23 +00:00
|
|
|
// 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)
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t nonSeekableReadaheadBlockCount = 0;
|
|
|
|
for (uint32_t i = 0; i < mStreams.Length(); ++i) {
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream* stream = mStreams[i];
|
2012-11-30 13:17:54 +00:00
|
|
|
if (!stream->mIsTransportSeekable) {
|
2009-10-09 11:46:23 +00:00
|
|
|
nonSeekableReadaheadBlockCount += stream->mReadaheadBlocks.GetCount();
|
|
|
|
}
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
|
2009-10-09 11:46:23 +00:00
|
|
|
// If freeBlockCount is zero, then compute the latest of
|
|
|
|
// the predicted next-uses for all blocks
|
|
|
|
TimeDuration latestNextUse;
|
|
|
|
if (freeBlockCount == 0) {
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t reusableBlock = FindReusableBlock(now, nullptr, 0, maxBlocks);
|
2009-10-09 11:46:23 +00:00
|
|
|
if (reusableBlock >= 0) {
|
|
|
|
latestNextUse = PredictNextUse(now, reusableBlock);
|
|
|
|
}
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < mStreams.Length(); ++i) {
|
2009-10-09 11:46:23 +00:00
|
|
|
actions.AppendElement(NONE);
|
2009-04-01 00:52:56 +00:00
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream* stream = mStreams[i];
|
2009-10-09 11:46:23 +00:00
|
|
|
if (stream->mClosed)
|
|
|
|
continue;
|
|
|
|
|
2009-10-09 11:48:30 +00:00
|
|
|
// Figure out where we should be reading from. It's the first
|
2009-10-09 11:46:23 +00:00
|
|
|
// uncached byte after the current mStreamOffset.
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t dataOffset = stream->GetCachedDataEndInternal(stream->mStreamOffset);
|
2009-10-09 11:48:30 +00:00
|
|
|
|
|
|
|
// Compute where we'd actually seek to to read at readOffset
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t desiredOffset = dataOffset;
|
2012-11-30 13:17:54 +00:00
|
|
|
if (stream->mIsTransportSeekable) {
|
2009-10-09 11:46:23 +00:00
|
|
|
if (desiredOffset > stream->mChannelOffset &&
|
|
|
|
desiredOffset <= stream->mChannelOffset + SEEK_VS_READ_THRESHOLD) {
|
|
|
|
// Assume it's more efficient to just keep reading up to the
|
|
|
|
// desired position instead of trying to seek
|
|
|
|
desiredOffset = stream->mChannelOffset;
|
|
|
|
}
|
2009-04-01 00:52:56 +00:00
|
|
|
} else {
|
2009-10-09 11:46:23 +00:00
|
|
|
// We can't seek directly to the desired offset...
|
|
|
|
if (stream->mChannelOffset > desiredOffset) {
|
|
|
|
// Reading forward won't get us anywhere, we need to go backwards.
|
|
|
|
// Seek back to 0 (the client will reopen the stream) and then
|
|
|
|
// read forward.
|
|
|
|
NS_WARNING("Can't seek backwards, so seeking to 0");
|
|
|
|
desiredOffset = 0;
|
|
|
|
// Flush cached blocks out, since if this is a live stream
|
|
|
|
// the cached data may be completely different next time we
|
|
|
|
// read it. We have to assume that live streams don't
|
|
|
|
// advertise themselves as being seekable...
|
|
|
|
ReleaseStreamBlocks(stream);
|
|
|
|
} else {
|
|
|
|
// otherwise reading forward is looking good, so just stay where we
|
|
|
|
// are and don't trigger a channel seek!
|
|
|
|
desiredOffset = stream->mChannelOffset;
|
|
|
|
}
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
|
2009-10-09 11:46:23 +00:00
|
|
|
// Figure out if we should be reading data now or not. It's amazing
|
|
|
|
// how complex this is, but each decision is simple enough.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool enableReading;
|
2009-10-09 11:48:30 +00:00
|
|
|
if (stream->mStreamLength >= 0 && dataOffset >= stream->mStreamLength) {
|
|
|
|
// We want data at the end of the stream, where there's nothing to
|
2009-10-09 11:46:23 +00:00
|
|
|
// 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.
|
2013-11-21 03:02:42 +00:00
|
|
|
CACHE_LOG(PR_LOG_DEBUG, ("Stream %p at end of stream", stream));
|
2009-10-09 11:46:23 +00:00
|
|
|
enableReading = !stream->mCacheSuspended &&
|
2009-10-09 11:48:30 +00:00
|
|
|
stream->mStreamLength == stream->mChannelOffset;
|
2009-10-09 11:46:23 +00:00
|
|
|
} 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.
|
2013-11-21 03:02:42 +00:00
|
|
|
CACHE_LOG(PR_LOG_DEBUG, ("Stream %p catching up", stream));
|
2011-09-29 23:34:37 +00:00
|
|
|
enableReading = true;
|
2009-10-09 11:46:23 +00:00
|
|
|
} else if (desiredOffset < stream->mStreamOffset + BLOCK_SIZE) {
|
|
|
|
// The stream reader is waiting for us, or nearly so. Better feed it.
|
2013-11-21 03:02:42 +00:00
|
|
|
CACHE_LOG(PR_LOG_DEBUG, ("Stream %p feeding reader", stream));
|
2011-09-29 23:34:37 +00:00
|
|
|
enableReading = true;
|
2012-11-30 13:17:54 +00:00
|
|
|
} else if (!stream->mIsTransportSeekable &&
|
2009-10-09 11:46:23 +00:00
|
|
|
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.
|
2013-11-21 03:02:42 +00:00
|
|
|
CACHE_LOG(PR_LOG_DEBUG, ("Stream %p throttling non-seekable readahead", stream));
|
2011-09-29 23:34:37 +00:00
|
|
|
enableReading = false;
|
2012-08-22 15:56:38 +00:00
|
|
|
} else if (mIndex.Length() > uint32_t(maxBlocks)) {
|
2009-10-09 11:46:23 +00:00
|
|
|
// We're in the process of bringing the cache size back to the
|
|
|
|
// desired limit, so don't bring in more data yet
|
2013-11-21 03:02:42 +00:00
|
|
|
CACHE_LOG(PR_LOG_DEBUG, ("Stream %p throttling to reduce cache size", stream));
|
2011-09-29 23:34:37 +00:00
|
|
|
enableReading = false;
|
2009-10-09 11:46:23 +00:00
|
|
|
} else {
|
|
|
|
TimeDuration predictedNewDataUse = PredictNextUseForIncomingData(stream);
|
2013-07-19 09:00:19 +00:00
|
|
|
|
|
|
|
if (stream->mCacheSuspended &&
|
|
|
|
predictedNewDataUse.ToMilliseconds() > CACHE_POWERSAVE_WAKEUP_LOW_THRESHOLD_MS) {
|
|
|
|
// Don't need data for a while, so don't bother waking up the stream
|
2013-11-21 03:02:42 +00:00
|
|
|
CACHE_LOG(PR_LOG_DEBUG, ("Stream %p avoiding wakeup since more data is not needed", stream));
|
2013-07-19 09:00:19 +00:00
|
|
|
enableReading = false;
|
|
|
|
} else if (freeBlockCount > 0) {
|
|
|
|
// Free blocks in the cache, so keep reading
|
2013-11-21 03:02:42 +00:00
|
|
|
CACHE_LOG(PR_LOG_DEBUG, ("Stream %p reading since there are free blocks", stream));
|
2013-07-19 09:00:19 +00:00
|
|
|
enableReading = true;
|
|
|
|
} else if (latestNextUse <= TimeDuration(0)) {
|
|
|
|
// No reusable blocks, so can't read anything
|
2013-11-21 03:02:42 +00:00
|
|
|
CACHE_LOG(PR_LOG_DEBUG, ("Stream %p throttling due to no reusable blocks", stream));
|
2013-07-19 09:00:19 +00:00
|
|
|
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
|
2013-11-21 03:02:42 +00:00
|
|
|
CACHE_LOG(PR_LOG_DEBUG, ("Stream %p predict next data in %f, current worst block is %f",
|
|
|
|
stream, predictedNewDataUse.ToSeconds(), latestNextUse.ToSeconds()));
|
2013-07-19 09:00:19 +00:00
|
|
|
enableReading = predictedNewDataUse < latestNextUse;
|
|
|
|
}
|
2009-10-09 11:46:23 +00:00
|
|
|
}
|
2009-04-01 00:52:56 +00:00
|
|
|
|
2009-10-09 11:46:23 +00:00
|
|
|
if (enableReading) {
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t j = 0; j < i; ++j) {
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream* other = mStreams[j];
|
2009-10-09 11:46:23 +00:00
|
|
|
if (other->mResourceID == stream->mResourceID &&
|
2010-08-13 00:41:47 +00:00
|
|
|
!other->mClient->IsSuspended() &&
|
2009-11-18 00:38:16 +00:00
|
|
|
other->mChannelOffset/BLOCK_SIZE == desiredOffset/BLOCK_SIZE) {
|
2009-10-09 11:46:23 +00:00
|
|
|
// This block is already going to be read by the other stream.
|
|
|
|
// So don't try to read it from this stream as well.
|
2011-09-29 23:34:37 +00:00
|
|
|
enableReading = false;
|
2013-11-21 03:02:42 +00:00
|
|
|
CACHE_LOG(PR_LOG_DEBUG, ("Stream %p waiting on same block (%lld) from stream %p",
|
|
|
|
stream, desiredOffset/BLOCK_SIZE, other));
|
2009-10-09 11:46:23 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-09-15 02:30:45 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-09 11:46:23 +00:00
|
|
|
|
|
|
|
if (stream->mChannelOffset != desiredOffset && enableReading) {
|
|
|
|
// We need to seek now.
|
2012-11-30 13:17:54 +00:00
|
|
|
NS_ASSERTION(stream->mIsTransportSeekable || desiredOffset == 0,
|
2009-10-09 11:46:23 +00:00
|
|
|
"Trying to seek in a non-seekable stream!");
|
|
|
|
// Round seek offset down to the start of the block. This is essential
|
|
|
|
// because we don't want to think we have part of a block already
|
|
|
|
// in mPartialBlockBuffer.
|
|
|
|
stream->mChannelOffset = (desiredOffset/BLOCK_SIZE)*BLOCK_SIZE;
|
2011-12-02 04:43:42 +00:00
|
|
|
actions[i] = stream->mCacheSuspended ? SEEK_AND_RESUME : SEEK;
|
2009-10-09 11:46:23 +00:00
|
|
|
} else if (enableReading && stream->mCacheSuspended) {
|
|
|
|
actions[i] = RESUME;
|
|
|
|
} else if (!enableReading && !stream->mCacheSuspended) {
|
|
|
|
actions[i] = SUSPEND;
|
|
|
|
}
|
2009-09-15 02:30:45 +00:00
|
|
|
}
|
2009-10-09 11:46:23 +00:00
|
|
|
#ifdef DEBUG
|
2011-09-29 23:34:37 +00:00
|
|
|
mInUpdate = false;
|
2009-10-09 11:46:23 +00:00
|
|
|
#endif
|
|
|
|
}
|
2009-09-15 02:30:45 +00:00
|
|
|
|
2009-10-09 11:46:23 +00:00
|
|
|
// Update the channel state without holding our cache lock. While we're
|
|
|
|
// doing this, decoder threads may be running and seeking, reading or changing
|
|
|
|
// other cache state. That's OK, they'll trigger new Update events and we'll
|
|
|
|
// get back here and revise our decisions. The important thing here is that
|
|
|
|
// performing these actions only depends on mChannelOffset and
|
2011-12-02 04:43:42 +00:00
|
|
|
// the action, which can only be written by the main thread (i.e., this
|
2009-10-09 11:46:23 +00:00
|
|
|
// thread), so we don't have races here.
|
2011-12-02 04:43:42 +00:00
|
|
|
|
|
|
|
// First, update the mCacheSuspended/mCacheEnded flags so that they're all correct
|
|
|
|
// when we fire our CacheClient commands below. Those commands can rely on these flags
|
|
|
|
// being set correctly for all streams.
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < mStreams.Length(); ++i) {
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream* stream = mStreams[i];
|
2009-10-09 11:46:23 +00:00
|
|
|
switch (actions[i]) {
|
|
|
|
case SEEK:
|
2011-12-02 04:43:42 +00:00
|
|
|
case SEEK_AND_RESUME:
|
2011-09-29 23:34:37 +00:00
|
|
|
stream->mCacheSuspended = false;
|
2011-11-30 23:09:10 +00:00
|
|
|
stream->mChannelEnded = false;
|
2009-10-09 11:46:23 +00:00
|
|
|
break;
|
2011-12-02 04:43:42 +00:00
|
|
|
case RESUME:
|
|
|
|
stream->mCacheSuspended = false;
|
|
|
|
break;
|
|
|
|
case SUSPEND:
|
|
|
|
stream->mCacheSuspended = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
stream->mHasHadUpdate = true;
|
|
|
|
}
|
2009-10-09 11:46:23 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < mStreams.Length(); ++i) {
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream* stream = mStreams[i];
|
2011-12-02 04:43:42 +00:00
|
|
|
nsresult rv;
|
|
|
|
switch (actions[i]) {
|
|
|
|
case SEEK:
|
|
|
|
case SEEK_AND_RESUME:
|
2013-11-21 03:02:42 +00:00
|
|
|
CACHE_LOG(PR_LOG_DEBUG, ("Stream %p CacheSeek to %lld (resume=%d)", stream,
|
|
|
|
(long long)stream->mChannelOffset, actions[i] == SEEK_AND_RESUME));
|
2011-12-02 04:43:42 +00:00
|
|
|
rv = stream->mClient->CacheClientSeek(stream->mChannelOffset,
|
|
|
|
actions[i] == SEEK_AND_RESUME);
|
|
|
|
break;
|
2009-10-09 11:46:23 +00:00
|
|
|
case RESUME:
|
2013-11-21 03:02:42 +00:00
|
|
|
CACHE_LOG(PR_LOG_DEBUG, ("Stream %p Resumed", stream));
|
2009-04-01 00:52:56 +00:00
|
|
|
rv = stream->mClient->CacheClientResume();
|
2009-10-09 11:46:23 +00:00
|
|
|
break;
|
|
|
|
case SUSPEND:
|
2013-11-21 03:02:42 +00:00
|
|
|
CACHE_LOG(PR_LOG_DEBUG, ("Stream %p Suspended", stream));
|
2009-04-01 00:52:56 +00:00
|
|
|
rv = stream->mClient->CacheClientSuspend();
|
2009-10-09 11:46:23 +00:00
|
|
|
break;
|
|
|
|
default:
|
2011-12-02 04:43:42 +00:00
|
|
|
rv = NS_OK;
|
2009-10-09 11:46:23 +00:00
|
|
|
break;
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2009-10-09 11:46:23 +00:00
|
|
|
// Close the streams that failed due to error. This will cause all
|
|
|
|
// client Read and Seek operations on those streams to fail. Blocked
|
|
|
|
// Reads will also be woken up.
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 04:29:02 +00:00
|
|
|
stream->CloseInternal(mon);
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class UpdateEvent : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
if (gMediaCache) {
|
|
|
|
gMediaCache->Update();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::QueueUpdate()
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
mReentrantMonitor.AssertCurrentThreadIn();
|
2009-04-01 00:52:56 +00:00
|
|
|
|
|
|
|
// Queuing an update while we're in an update raises a high risk of
|
|
|
|
// triggering endless events
|
|
|
|
NS_ASSERTION(!mInUpdate,
|
|
|
|
"Queuing an update while we're in an update");
|
|
|
|
if (mUpdateQueued)
|
|
|
|
return;
|
2011-09-29 23:34:37 +00:00
|
|
|
mUpdateQueued = true;
|
2009-04-01 00:52:56 +00:00
|
|
|
nsCOMPtr<nsIRunnable> event = new UpdateEvent();
|
|
|
|
NS_DispatchToMainThread(event);
|
|
|
|
}
|
|
|
|
|
2009-06-12 01:43:23 +00:00
|
|
|
#ifdef DEBUG_VERIFY_CACHE
|
2009-04-01 00:52:56 +00:00
|
|
|
void
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::Verify()
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
mReentrantMonitor.AssertCurrentThreadIn();
|
2009-04-01 00:52:56 +00:00
|
|
|
|
|
|
|
mFreeBlocks.Verify();
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < mStreams.Length(); ++i) {
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream* stream = mStreams[i];
|
2009-04-01 00:52:56 +00:00
|
|
|
stream->mReadaheadBlocks.Verify();
|
2009-09-15 02:30:44 +00:00
|
|
|
stream->mPlayedBlocks.Verify();
|
|
|
|
stream->mMetadataBlocks.Verify();
|
|
|
|
|
|
|
|
// Verify that the readahead blocks are listed in stream block order
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t block = stream->mReadaheadBlocks.GetFirstBlock();
|
|
|
|
int32_t lastStreamBlock = -1;
|
2009-09-15 02:30:44 +00:00
|
|
|
while (block >= 0) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t j = 0;
|
2009-09-15 02:30:44 +00:00
|
|
|
while (mIndex[block].mOwners[j].mStream != stream) {
|
|
|
|
++j;
|
|
|
|
}
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t nextStreamBlock =
|
|
|
|
int32_t(mIndex[block].mOwners[j].mStreamBlock);
|
2009-09-15 02:30:44 +00:00
|
|
|
NS_ASSERTION(lastStreamBlock < nextStreamBlock,
|
|
|
|
"Blocks not increasing in readahead stream");
|
|
|
|
lastStreamBlock = nextStreamBlock;
|
|
|
|
block = stream->mReadaheadBlocks.GetNextBlock(block);
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::InsertReadaheadBlock(BlockOwner* aBlockOwner,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t aBlockIndex)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
mReentrantMonitor.AssertCurrentThreadIn();
|
2009-04-01 00:52:56 +00:00
|
|
|
|
|
|
|
// Find the last block whose stream block is before aBlockIndex's
|
|
|
|
// stream block, and insert after it
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream* stream = aBlockOwner->mStream;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t readaheadIndex = stream->mReadaheadBlocks.GetLastBlock();
|
2009-09-15 02:30:44 +00:00
|
|
|
while (readaheadIndex >= 0) {
|
|
|
|
BlockOwner* bo = GetBlockOwner(readaheadIndex, stream);
|
|
|
|
NS_ASSERTION(bo, "stream must own its blocks");
|
|
|
|
if (bo->mStreamBlock < aBlockOwner->mStreamBlock) {
|
2009-04-01 00:52:56 +00:00
|
|
|
stream->mReadaheadBlocks.AddAfter(aBlockIndex, readaheadIndex);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-15 02:30:44 +00:00
|
|
|
NS_ASSERTION(bo->mStreamBlock > aBlockOwner->mStreamBlock,
|
2009-04-01 00:52:56 +00:00
|
|
|
"Duplicated blocks??");
|
2009-09-15 02:30:44 +00:00
|
|
|
readaheadIndex = stream->mReadaheadBlocks.GetPrevBlock(readaheadIndex);
|
2009-09-15 02:30:44 +00:00
|
|
|
}
|
|
|
|
|
2009-04-01 00:52:56 +00:00
|
|
|
stream->mReadaheadBlocks.AddFirstBlock(aBlockIndex);
|
|
|
|
Verify();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::AllocateAndWriteBlock(MediaCacheStream* aStream, const void* aData,
|
|
|
|
MediaCacheStream::ReadMode aMode)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
mReentrantMonitor.AssertCurrentThreadIn();
|
2009-04-01 00:52:56 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t streamBlockIndex = aStream->mChannelOffset/BLOCK_SIZE;
|
2009-09-15 02:30:45 +00:00
|
|
|
|
|
|
|
// Remove all cached copies of this block
|
|
|
|
ResourceStreamIterator iter(aStream->mResourceID);
|
2012-11-14 19:46:40 +00:00
|
|
|
while (MediaCacheStream* stream = iter.Next()) {
|
2012-08-22 15:56:38 +00:00
|
|
|
while (streamBlockIndex >= int32_t(stream->mBlocks.Length())) {
|
2009-09-15 02:30:45 +00:00
|
|
|
stream->mBlocks.AppendElement(-1);
|
|
|
|
}
|
|
|
|
if (stream->mBlocks[streamBlockIndex] >= 0) {
|
|
|
|
// We no longer want to own this block
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t globalBlockIndex = stream->mBlocks[streamBlockIndex];
|
2013-11-21 03:02:42 +00:00
|
|
|
CACHE_LOG(PR_LOG_DEBUG, ("Released block %d from stream %p block %d(%lld)",
|
|
|
|
globalBlockIndex, stream, streamBlockIndex, (long long)streamBlockIndex*BLOCK_SIZE));
|
2009-09-15 02:30:45 +00:00
|
|
|
RemoveBlockOwner(globalBlockIndex, stream);
|
|
|
|
}
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
|
2009-09-15 02:30:45 +00:00
|
|
|
// Extend the mBlocks array as necessary
|
|
|
|
|
2009-04-11 09:39:02 +00:00
|
|
|
TimeStamp now = TimeStamp::Now();
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t blockIndex = FindBlockForIncomingData(now, aStream);
|
2009-04-01 00:52:56 +00:00
|
|
|
if (blockIndex >= 0) {
|
2009-09-15 02:30:44 +00:00
|
|
|
FreeBlock(blockIndex);
|
2009-04-01 00:52:56 +00:00
|
|
|
|
2010-10-26 01:21:16 +00:00
|
|
|
Block* block = &mIndex[blockIndex];
|
2013-11-21 03:02:42 +00:00
|
|
|
CACHE_LOG(PR_LOG_DEBUG, ("Allocated block %d to stream %p block %d(%lld)",
|
|
|
|
blockIndex, aStream, streamBlockIndex, (long long)streamBlockIndex*BLOCK_SIZE));
|
2009-09-15 02:30:44 +00:00
|
|
|
|
2009-04-01 00:52:56 +00:00
|
|
|
mFreeBlocks.RemoveBlock(blockIndex);
|
2009-09-15 02:30:45 +00:00
|
|
|
|
|
|
|
// Tell each stream using this resource about the new block.
|
|
|
|
ResourceStreamIterator iter(aStream->mResourceID);
|
2012-11-14 19:46:40 +00:00
|
|
|
while (MediaCacheStream* stream = iter.Next()) {
|
2009-09-15 02:30:45 +00:00
|
|
|
BlockOwner* bo = block->mOwners.AppendElement();
|
|
|
|
if (!bo)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bo->mStream = stream;
|
|
|
|
bo->mStreamBlock = streamBlockIndex;
|
|
|
|
bo->mLastUseTime = now;
|
|
|
|
stream->mBlocks[streamBlockIndex] = blockIndex;
|
|
|
|
if (streamBlockIndex*BLOCK_SIZE < stream->mStreamOffset) {
|
2012-11-14 19:46:40 +00:00
|
|
|
bo->mClass = aMode == MediaCacheStream::MODE_PLAYBACK
|
2009-09-15 02:30:45 +00:00
|
|
|
? PLAYED_BLOCK : METADATA_BLOCK;
|
|
|
|
// This must be the most-recently-used block, since we
|
|
|
|
// marked it as used now (which may be slightly bogus, but we'll
|
|
|
|
// treat it as used for simplicity).
|
|
|
|
GetListForBlock(bo)->AddFirstBlock(blockIndex);
|
|
|
|
Verify();
|
|
|
|
} else {
|
|
|
|
// This may not be the latest readahead block, although it usually
|
|
|
|
// will be. We may have to scan for the right place to insert
|
|
|
|
// the block in the list.
|
|
|
|
bo->mClass = READAHEAD_BLOCK;
|
|
|
|
InsertReadaheadBlock(bo, blockIndex);
|
|
|
|
}
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
nsresult rv = mFileCache->WriteBlock(blockIndex, reinterpret_cast<const uint8_t*>(aData));
|
2009-04-01 00:52:56 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
2013-11-21 03:02:42 +00:00
|
|
|
CACHE_LOG(PR_LOG_DEBUG, ("Released block %d from stream %p block %d(%lld)",
|
|
|
|
blockIndex, aStream, streamBlockIndex, (long long)streamBlockIndex*BLOCK_SIZE));
|
2009-04-01 00:52:56 +00:00
|
|
|
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
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::OpenStream(MediaCacheStream* aStream)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2013-11-21 03:02:42 +00:00
|
|
|
CACHE_LOG(PR_LOG_DEBUG, ("Stream %p opened", aStream));
|
2009-04-01 00:52:56 +00:00
|
|
|
mStreams.AppendElement(aStream);
|
2012-03-20 07:55:40 +00:00
|
|
|
aStream->mResourceID = AllocateResourceID();
|
2010-08-13 00:41:47 +00:00
|
|
|
|
|
|
|
// Queue an update since a new stream has been opened.
|
|
|
|
gMediaCache->QueueUpdate();
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::ReleaseStream(MediaCacheStream* aStream)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(mReentrantMonitor);
|
2013-11-21 03:02:42 +00:00
|
|
|
CACHE_LOG(PR_LOG_DEBUG, ("Stream %p closed", aStream));
|
2009-04-01 00:52:56 +00:00
|
|
|
mStreams.RemoveElement(aStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::ReleaseStreamBlocks(MediaCacheStream* aStream)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
mReentrantMonitor.AssertCurrentThreadIn();
|
2009-04-01 00:52:56 +00:00
|
|
|
|
|
|
|
// 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
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t length = aStream->mBlocks.Length();
|
|
|
|
for (uint32_t i = 0; i < length; ++i) {
|
|
|
|
int32_t blockIndex = aStream->mBlocks[i];
|
2009-04-01 00:52:56 +00:00
|
|
|
if (blockIndex >= 0) {
|
2013-11-21 03:02:42 +00:00
|
|
|
CACHE_LOG(PR_LOG_DEBUG, ("Released block %d from stream %p block %d(%lld)",
|
|
|
|
blockIndex, aStream, i, (long long)i*BLOCK_SIZE));
|
2009-09-15 02:30:44 +00:00
|
|
|
RemoveBlockOwner(blockIndex, aStream);
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::Truncate()
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t end;
|
2009-04-01 00:52:56 +00:00
|
|
|
for (end = mIndex.Length(); end > 0; --end) {
|
2009-09-15 02:30:44 +00:00
|
|
|
if (!IsBlockFree(end - 1))
|
2009-04-01 00:52:56 +00:00
|
|
|
break;
|
|
|
|
mFreeBlocks.RemoveBlock(end - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (end < mIndex.Length()) {
|
|
|
|
mIndex.TruncateLength(end);
|
|
|
|
// XXX We could truncate the cache file here, but we don't seem
|
|
|
|
// to have a cross-platform API for doing that. At least when all
|
|
|
|
// streams are closed we shut down the cache, which erases the
|
|
|
|
// file at that point.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::NoteBlockUsage(MediaCacheStream* aStream, int32_t aBlockIndex,
|
|
|
|
MediaCacheStream::ReadMode aMode,
|
2009-04-11 09:39:02 +00:00
|
|
|
TimeStamp aNow)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
mReentrantMonitor.AssertCurrentThreadIn();
|
2009-04-01 00:52:56 +00:00
|
|
|
|
|
|
|
if (aBlockIndex < 0) {
|
|
|
|
// this block is not in the cache yet
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-09-15 02:30:44 +00:00
|
|
|
BlockOwner* bo = GetBlockOwner(aBlockIndex, aStream);
|
|
|
|
if (!bo) {
|
2009-04-01 00:52:56 +00:00
|
|
|
// this block is not in the cache yet
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The following check has to be <= because the stream offset has
|
|
|
|
// not yet been updated for the data read from this block
|
2009-09-15 02:30:44 +00:00
|
|
|
NS_ASSERTION(bo->mStreamBlock*BLOCK_SIZE <= bo->mStream->mStreamOffset,
|
2009-04-01 00:52:56 +00:00
|
|
|
"Using a block that's behind the read position?");
|
|
|
|
|
2009-09-15 02:30:44 +00:00
|
|
|
GetListForBlock(bo)->RemoveBlock(aBlockIndex);
|
|
|
|
bo->mClass =
|
2012-11-14 19:46:40 +00:00
|
|
|
(aMode == MediaCacheStream::MODE_METADATA || bo->mClass == METADATA_BLOCK)
|
2009-04-01 00:52:56 +00:00
|
|
|
? METADATA_BLOCK : PLAYED_BLOCK;
|
|
|
|
// Since this is just being used now, it can definitely be at the front
|
|
|
|
// of mMetadataBlocks or mPlayedBlocks
|
2009-09-15 02:30:44 +00:00
|
|
|
GetListForBlock(bo)->AddFirstBlock(aBlockIndex);
|
|
|
|
bo->mLastUseTime = aNow;
|
2009-04-01 00:52:56 +00:00
|
|
|
Verify();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::NoteSeek(MediaCacheStream* aStream, int64_t aOldOffset)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
mReentrantMonitor.AssertCurrentThreadIn();
|
2009-04-01 00:52:56 +00:00
|
|
|
|
|
|
|
if (aOldOffset < aStream->mStreamOffset) {
|
|
|
|
// We seeked forward. Convert blocks from readahead to played.
|
|
|
|
// Any readahead block that intersects the seeked-over range must
|
|
|
|
// be converted.
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t blockIndex = aOldOffset/BLOCK_SIZE;
|
|
|
|
int32_t endIndex =
|
2013-01-15 12:22:03 +00:00
|
|
|
std::min<int64_t>((aStream->mStreamOffset + BLOCK_SIZE - 1)/BLOCK_SIZE,
|
2009-04-01 00:52:56 +00:00
|
|
|
aStream->mBlocks.Length());
|
2009-04-11 09:39:02 +00:00
|
|
|
TimeStamp now = TimeStamp::Now();
|
2009-04-01 00:52:56 +00:00
|
|
|
while (blockIndex < endIndex) {
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t cacheBlockIndex = aStream->mBlocks[blockIndex];
|
2009-04-01 00:52:56 +00:00
|
|
|
if (cacheBlockIndex >= 0) {
|
|
|
|
// Marking the block used may not be exactly what we want but
|
|
|
|
// it's simple
|
2012-11-14 19:46:40 +00:00
|
|
|
NoteBlockUsage(aStream, cacheBlockIndex, MediaCacheStream::MODE_PLAYBACK,
|
2009-04-01 00:52:56 +00:00
|
|
|
now);
|
|
|
|
}
|
|
|
|
++blockIndex;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We seeked backward. Convert from played to readahead.
|
|
|
|
// Any played block that is entirely after the start of the seeked-over
|
|
|
|
// range must be converted.
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t blockIndex =
|
2009-04-01 00:52:56 +00:00
|
|
|
(aStream->mStreamOffset + BLOCK_SIZE - 1)/BLOCK_SIZE;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t endIndex =
|
2013-01-15 12:22:03 +00:00
|
|
|
std::min<int64_t>((aOldOffset + BLOCK_SIZE - 1)/BLOCK_SIZE,
|
2009-04-01 00:52:56 +00:00
|
|
|
aStream->mBlocks.Length());
|
|
|
|
while (blockIndex < endIndex) {
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t cacheBlockIndex = aStream->mBlocks[endIndex - 1];
|
2009-04-01 00:52:56 +00:00
|
|
|
if (cacheBlockIndex >= 0) {
|
2009-09-15 02:30:44 +00:00
|
|
|
BlockOwner* bo = GetBlockOwner(cacheBlockIndex, aStream);
|
|
|
|
NS_ASSERTION(bo, "Stream doesn't own its blocks?");
|
|
|
|
if (bo->mClass == PLAYED_BLOCK) {
|
|
|
|
aStream->mPlayedBlocks.RemoveBlock(cacheBlockIndex);
|
|
|
|
bo->mClass = READAHEAD_BLOCK;
|
2009-04-01 00:52:56 +00:00
|
|
|
// Adding this as the first block is sure to be OK since
|
|
|
|
// this must currently be the earliest readahead block
|
|
|
|
// (that's why we're proceeding backwards from the end of
|
|
|
|
// the seeked range to the start)
|
2009-09-15 02:30:44 +00:00
|
|
|
aStream->mReadaheadBlocks.AddFirstBlock(cacheBlockIndex);
|
2009-04-01 00:52:56 +00:00
|
|
|
Verify();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--endIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream::NotifyDataLength(int64_t aLength)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(gMediaCache->GetReentrantMonitor());
|
2009-04-01 00:52:56 +00:00
|
|
|
mStreamLength = aLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream::NotifyDataStarted(int64_t aOffset)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(gMediaCache->GetReentrantMonitor());
|
2009-04-01 00:52:56 +00:00
|
|
|
NS_WARN_IF_FALSE(aOffset == mChannelOffset,
|
|
|
|
"Server is giving us unexpected offset");
|
|
|
|
mChannelOffset = aOffset;
|
|
|
|
if (mStreamLength >= 0) {
|
|
|
|
// If we started reading at a certain offset, then for sure
|
|
|
|
// the stream is at least that long.
|
2013-01-15 12:22:03 +00:00
|
|
|
mStreamLength = std::max(mStreamLength, mChannelOffset);
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-30 03:12:42 +00:00
|
|
|
bool
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream::UpdatePrincipal(nsIPrincipal* aPrincipal)
|
2009-05-13 21:52:50 +00:00
|
|
|
{
|
2012-04-30 03:12:42 +00:00
|
|
|
return nsContentUtils::CombineResourcePrincipals(&mPrincipal, aPrincipal);
|
2009-05-13 21:52:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream::NotifyDataReceived(int64_t aSize, const char* aData,
|
2009-05-13 21:52:50 +00:00
|
|
|
nsIPrincipal* aPrincipal)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
2012-04-30 03:12:42 +00:00
|
|
|
// Update principals before putting the data in the cache. This is important,
|
|
|
|
// we want to make sure all principals are updated before any consumer
|
|
|
|
// can see the new data.
|
|
|
|
// We do this without holding the cache monitor, in case the client wants
|
|
|
|
// to do something that takes a lock.
|
|
|
|
{
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::ResourceStreamIterator iter(mResourceID);
|
|
|
|
while (MediaCacheStream* stream = iter.Next()) {
|
2012-04-30 03:12:42 +00:00
|
|
|
if (stream->UpdatePrincipal(aPrincipal)) {
|
|
|
|
stream->mClient->CacheClientNotifyPrincipalChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(gMediaCache->GetReentrantMonitor());
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t size = aSize;
|
2009-04-01 00:52:56 +00:00
|
|
|
const char* data = aData;
|
|
|
|
|
2013-11-21 03:02:42 +00:00
|
|
|
CACHE_LOG(PR_LOG_DEBUG, ("Stream %p DataReceived at %lld count=%lld",
|
|
|
|
this, (long long)mChannelOffset, (long long)aSize));
|
2009-04-01 00:52:56 +00:00
|
|
|
|
|
|
|
// We process the data one block (or part of a block) at a time
|
|
|
|
while (size > 0) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t blockIndex = mChannelOffset/BLOCK_SIZE;
|
|
|
|
int32_t blockOffset = int32_t(mChannelOffset - blockIndex*BLOCK_SIZE);
|
2013-01-15 12:22:03 +00:00
|
|
|
int32_t chunkSize = std::min<int64_t>(BLOCK_SIZE - blockOffset, size);
|
2009-04-01 00:52:56 +00:00
|
|
|
|
|
|
|
// This gets set to something non-null if we have a whole block
|
|
|
|
// of data to write to the cache
|
2012-07-30 14:20:58 +00:00
|
|
|
const char* blockDataToStore = nullptr;
|
2009-04-01 00:52:56 +00:00
|
|
|
ReadMode mode = MODE_PLAYBACK;
|
|
|
|
if (blockOffset == 0 && chunkSize == BLOCK_SIZE) {
|
|
|
|
// We received a whole block, so avoid a useless copy through
|
|
|
|
// mPartialBlockBuffer
|
|
|
|
blockDataToStore = data;
|
|
|
|
} else {
|
|
|
|
if (blockOffset == 0) {
|
|
|
|
// We've just started filling this buffer so now is a good time
|
|
|
|
// to clear this flag.
|
2011-09-29 23:34:37 +00:00
|
|
|
mMetadataInPartialBlockBuffer = false;
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
2014-02-11 06:43:52 +00:00
|
|
|
memcpy(reinterpret_cast<char*>(mPartialBlockBuffer.get()) + blockOffset,
|
2009-04-01 00:52:56 +00:00
|
|
|
data, chunkSize);
|
|
|
|
|
|
|
|
if (blockOffset + chunkSize == BLOCK_SIZE) {
|
|
|
|
// We completed a block, so lets write it out.
|
2014-02-11 06:43:52 +00:00
|
|
|
blockDataToStore = reinterpret_cast<char*>(mPartialBlockBuffer.get());
|
2009-04-01 00:52:56 +00:00
|
|
|
if (mMetadataInPartialBlockBuffer) {
|
|
|
|
mode = MODE_METADATA;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (blockDataToStore) {
|
|
|
|
gMediaCache->AllocateAndWriteBlock(this, blockDataToStore, mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
mChannelOffset += chunkSize;
|
|
|
|
size -= chunkSize;
|
|
|
|
data += chunkSize;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::ResourceStreamIterator iter(mResourceID);
|
|
|
|
while (MediaCacheStream* stream = iter.Next()) {
|
2009-09-15 02:30:45 +00:00
|
|
|
if (stream->mStreamLength >= 0) {
|
|
|
|
// The stream is at least as long as what we've read
|
2013-01-15 12:22:03 +00:00
|
|
|
stream->mStreamLength = std::max(stream->mStreamLength, mChannelOffset);
|
2009-09-15 02:30:45 +00:00
|
|
|
}
|
|
|
|
stream->mClient->CacheClientNotifyDataReceived();
|
|
|
|
}
|
|
|
|
|
2009-04-01 00:52:56 +00:00
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-12-06 23:27:08 +00:00
|
|
|
MediaCacheStream::FlushPartialBlockInternal(bool aNotifyAll)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(gMediaCache->GetReentrantMonitor());
|
2009-04-01 00:52:56 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t blockOffset = int32_t(mChannelOffset%BLOCK_SIZE);
|
2009-04-01 00:52:56 +00:00
|
|
|
if (blockOffset > 0) {
|
2013-11-21 03:02:42 +00:00
|
|
|
CACHE_LOG(PR_LOG_DEBUG,
|
|
|
|
("Stream %p writing partial block: [%d] bytes; "
|
|
|
|
"mStreamOffset [%lld] mChannelOffset[%lld] mStreamLength [%lld] "
|
|
|
|
"notifying: [%s]",
|
|
|
|
this, blockOffset, mStreamOffset, mChannelOffset, mStreamLength,
|
|
|
|
aNotifyAll ? "yes" : "no"));
|
2012-12-06 23:27:08 +00:00
|
|
|
|
2009-04-01 00:52:56 +00:00
|
|
|
// Write back the partial block
|
2014-02-11 06:43:52 +00:00
|
|
|
memset(reinterpret_cast<char*>(mPartialBlockBuffer.get()) + blockOffset, 0,
|
2009-04-01 00:52:56 +00:00
|
|
|
BLOCK_SIZE - blockOffset);
|
|
|
|
gMediaCache->AllocateAndWriteBlock(this, mPartialBlockBuffer,
|
|
|
|
mMetadataInPartialBlockBuffer ? MODE_METADATA : MODE_PLAYBACK);
|
2012-12-06 23:27:08 +00:00
|
|
|
if (aNotifyAll) {
|
|
|
|
// Wake up readers who may be waiting for this data
|
|
|
|
mon.NotifyAll();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaCacheStream::FlushPartialBlock()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
|
|
|
ReentrantMonitorAutoEnter mon(gMediaCache->GetReentrantMonitor());
|
|
|
|
|
|
|
|
// Write the current partial block to memory.
|
|
|
|
// Note: This writes a full block, so if data is not at the end of the
|
|
|
|
// stream, the decoder must subsequently choose correct start and end offsets
|
|
|
|
// for reading/seeking.
|
|
|
|
FlushPartialBlockInternal(false);
|
|
|
|
|
|
|
|
gMediaCache->QueueUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaCacheStream::NotifyDataEnded(nsresult aStatus)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
|
|
|
ReentrantMonitorAutoEnter mon(gMediaCache->GetReentrantMonitor());
|
|
|
|
|
|
|
|
if (NS_FAILED(aStatus)) {
|
|
|
|
// Disconnect from other streams sharing our resource, since they
|
|
|
|
// should continue trying to load. Our load might have been deliberately
|
|
|
|
// canceled and that shouldn't affect other streams.
|
|
|
|
mResourceID = gMediaCache->AllocateResourceID();
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
2009-09-15 02:30:45 +00:00
|
|
|
|
2012-12-06 23:27:08 +00:00
|
|
|
FlushPartialBlockInternal(true);
|
|
|
|
|
2011-11-23 23:05:12 +00:00
|
|
|
if (!mDidNotifyDataEnded) {
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::ResourceStreamIterator iter(mResourceID);
|
|
|
|
while (MediaCacheStream* stream = iter.Next()) {
|
2011-11-23 23:05:12 +00:00
|
|
|
if (NS_SUCCEEDED(aStatus)) {
|
|
|
|
// We read the whole stream, so remember the true length
|
|
|
|
stream->mStreamLength = mChannelOffset;
|
|
|
|
}
|
|
|
|
NS_ASSERTION(!stream->mDidNotifyDataEnded, "Stream already ended!");
|
|
|
|
stream->mDidNotifyDataEnded = true;
|
|
|
|
stream->mNotifyDataEndedStatus = aStatus;
|
|
|
|
stream->mClient->CacheClientNotifyDataEnded(aStatus);
|
2009-09-15 02:30:45 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-30 23:09:10 +00:00
|
|
|
|
|
|
|
mChannelEnded = true;
|
2012-03-20 07:55:40 +00:00
|
|
|
gMediaCache->QueueUpdate();
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream::~MediaCacheStream()
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
NS_ASSERTION(!mPinCount, "Unbalanced Pin");
|
|
|
|
|
2010-06-17 19:46:28 +00:00
|
|
|
if (gMediaCache) {
|
|
|
|
NS_ASSERTION(mClosed, "Stream was not closed");
|
|
|
|
gMediaCache->ReleaseStream(this);
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::MaybeShutdown();
|
2010-06-17 19:46:28 +00:00
|
|
|
}
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-30 13:17:54 +00:00
|
|
|
MediaCacheStream::SetTransportSeekable(bool aIsTransportSeekable)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(gMediaCache->GetReentrantMonitor());
|
2012-11-30 13:17:54 +00:00
|
|
|
NS_ASSERTION(mIsTransportSeekable || aIsTransportSeekable ||
|
2009-04-01 00:52:56 +00:00
|
|
|
mChannelOffset == 0, "channel offset must be zero when we become non-seekable");
|
2012-11-30 13:17:54 +00:00
|
|
|
mIsTransportSeekable = aIsTransportSeekable;
|
2009-04-01 00:52:56 +00:00
|
|
|
// Queue an Update since we may change our strategy for dealing
|
|
|
|
// with this stream
|
|
|
|
gMediaCache->QueueUpdate();
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2012-11-30 13:17:54 +00:00
|
|
|
MediaCacheStream::IsTransportSeekable()
|
2009-05-18 02:02:20 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(gMediaCache->GetReentrantMonitor());
|
2012-11-30 13:17:54 +00:00
|
|
|
return mIsTransportSeekable;
|
2009-05-18 02:02:20 +00:00
|
|
|
}
|
|
|
|
|
2011-11-30 05:05:49 +00:00
|
|
|
bool
|
2013-05-02 22:59:18 +00:00
|
|
|
MediaCacheStream::AreAllStreamsForResourceSuspended()
|
2011-11-30 05:05:49 +00:00
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(gMediaCache->GetReentrantMonitor());
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCache::ResourceStreamIterator iter(mResourceID);
|
2013-04-24 09:19:02 +00:00
|
|
|
// Look for a stream that's able to read the data we need
|
|
|
|
int64_t dataOffset = -1;
|
2012-11-14 19:46:40 +00:00
|
|
|
while (MediaCacheStream* stream = iter.Next()) {
|
2013-04-24 09:19:02 +00:00
|
|
|
if (stream->mCacheSuspended || stream->mChannelEnded || stream->mClosed) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (dataOffset < 0) {
|
|
|
|
dataOffset = GetCachedDataEndInternal(mStreamOffset);
|
2012-02-15 04:35:01 +00:00
|
|
|
}
|
2013-04-24 09:19:02 +00:00
|
|
|
// Ignore streams that are reading beyond the data we need
|
|
|
|
if (stream->mChannelOffset > dataOffset) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return false;
|
2011-12-09 04:48:40 +00:00
|
|
|
}
|
2013-04-24 09:19:02 +00:00
|
|
|
|
2011-11-30 05:05:49 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-04-01 00:52:56 +00:00
|
|
|
void
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream::Close()
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(gMediaCache->GetReentrantMonitor());
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 04:29:02 +00:00
|
|
|
CloseInternal(mon);
|
2009-04-01 00:52:56 +00:00
|
|
|
// Queue an Update since we may have created more free space. Don't do
|
|
|
|
// it from CloseInternal since that gets called by Update() itself
|
|
|
|
// sometimes, and we try to not to queue updates from Update().
|
|
|
|
gMediaCache->QueueUpdate();
|
|
|
|
}
|
|
|
|
|
2011-11-30 05:05:49 +00:00
|
|
|
void
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream::EnsureCacheUpdate()
|
2011-11-30 05:05:49 +00:00
|
|
|
{
|
|
|
|
if (mHasHadUpdate)
|
|
|
|
return;
|
|
|
|
gMediaCache->Update();
|
|
|
|
}
|
|
|
|
|
2009-04-01 00:52:56 +00:00
|
|
|
void
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream::CloseInternal(ReentrantMonitorAutoEnter& aReentrantMonitor)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
|
|
|
if (mClosed)
|
|
|
|
return;
|
2011-09-29 23:34:37 +00:00
|
|
|
mClosed = true;
|
2009-04-01 00:52:56 +00:00
|
|
|
gMediaCache->ReleaseStreamBlocks(this);
|
|
|
|
// Wake up any blocked readers
|
2011-04-29 19:21:57 +00:00
|
|
|
aReentrantMonitor.NotifyAll();
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream::Pin()
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(gMediaCache->GetReentrantMonitor());
|
2009-04-01 00:52:56 +00:00
|
|
|
++mPinCount;
|
|
|
|
// Queue an Update since we may no longer want to read more into the
|
|
|
|
// cache, if this stream's block have become non-evictable
|
|
|
|
gMediaCache->QueueUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream::Unpin()
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(gMediaCache->GetReentrantMonitor());
|
2009-04-01 00:52:56 +00:00
|
|
|
NS_ASSERTION(mPinCount > 0, "Unbalanced Unpin");
|
|
|
|
--mPinCount;
|
|
|
|
// Queue an Update since we may be able to read more into the
|
|
|
|
// cache, if this stream's block have become evictable
|
|
|
|
gMediaCache->QueueUpdate();
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream::GetLength()
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(gMediaCache->GetReentrantMonitor());
|
2009-04-01 00:52:56 +00:00
|
|
|
return mStreamLength;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream::GetNextCachedData(int64_t aOffset)
|
2009-05-17 22:15:57 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(gMediaCache->GetReentrantMonitor());
|
2009-05-17 22:15:57 +00:00
|
|
|
return GetNextCachedDataInternal(aOffset);
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream::GetCachedDataEnd(int64_t aOffset)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(gMediaCache->GetReentrantMonitor());
|
2009-04-01 00:52:56 +00:00
|
|
|
return GetCachedDataEndInternal(aOffset);
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream::IsDataCachedToEndOfStream(int64_t aOffset)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(gMediaCache->GetReentrantMonitor());
|
2009-04-01 00:52:56 +00:00
|
|
|
if (mStreamLength < 0)
|
2011-09-29 23:34:37 +00:00
|
|
|
return false;
|
2009-04-01 00:52:56 +00:00
|
|
|
return GetCachedDataEndInternal(aOffset) >= mStreamLength;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream::GetCachedDataEndInternal(int64_t aOffset)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
gMediaCache->GetReentrantMonitor().AssertCurrentThreadIn();
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t startBlockIndex = aOffset/BLOCK_SIZE;
|
|
|
|
uint32_t blockIndex = startBlockIndex;
|
2009-04-01 00:52:56 +00:00
|
|
|
while (blockIndex < mBlocks.Length() && mBlocks[blockIndex] != -1) {
|
|
|
|
++blockIndex;
|
|
|
|
}
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t result = blockIndex*BLOCK_SIZE;
|
2009-04-01 00:52:56 +00:00
|
|
|
if (blockIndex == mChannelOffset/BLOCK_SIZE) {
|
|
|
|
// The block containing mChannelOffset may be partially read but not
|
|
|
|
// yet committed to the main cache
|
|
|
|
result = mChannelOffset;
|
|
|
|
}
|
|
|
|
if (mStreamLength >= 0) {
|
|
|
|
// The last block in the cache may only be partially valid, so limit
|
|
|
|
// the cached range to the stream length
|
2013-01-15 12:22:03 +00:00
|
|
|
result = std::min(result, mStreamLength);
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
2013-01-15 12:22:03 +00:00
|
|
|
return std::max(result, aOffset);
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream::GetNextCachedDataInternal(int64_t aOffset)
|
2009-05-17 22:15:57 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
gMediaCache->GetReentrantMonitor().AssertCurrentThreadIn();
|
2009-05-17 22:15:57 +00:00
|
|
|
if (aOffset == mStreamLength)
|
|
|
|
return -1;
|
2010-10-26 01:21:16 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t startBlockIndex = aOffset/BLOCK_SIZE;
|
|
|
|
uint32_t channelBlockIndex = mChannelOffset/BLOCK_SIZE;
|
2009-05-17 22:15:57 +00:00
|
|
|
|
|
|
|
if (startBlockIndex == channelBlockIndex &&
|
|
|
|
aOffset < mChannelOffset) {
|
|
|
|
// The block containing mChannelOffset is partially read, but not
|
|
|
|
// yet committed to the main cache. aOffset lies in the partially
|
|
|
|
// read portion, thus it is effectively cached.
|
|
|
|
return aOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (startBlockIndex >= mBlocks.Length())
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// Is the current block cached?
|
|
|
|
if (mBlocks[startBlockIndex] != -1)
|
|
|
|
return aOffset;
|
|
|
|
|
|
|
|
// Count the number of uncached blocks
|
2011-09-29 06:19:26 +00:00
|
|
|
bool hasPartialBlock = (mChannelOffset % BLOCK_SIZE) != 0;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t blockIndex = startBlockIndex + 1;
|
2011-09-29 23:34:37 +00:00
|
|
|
while (true) {
|
2009-05-17 22:15:57 +00:00
|
|
|
if ((hasPartialBlock && blockIndex == channelBlockIndex) ||
|
|
|
|
(blockIndex < mBlocks.Length() && mBlocks[blockIndex] != -1)) {
|
|
|
|
// We at the incoming channel block, which has has data in it,
|
|
|
|
// or are we at a cached block. Return index of block start.
|
|
|
|
return blockIndex * BLOCK_SIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No more cached blocks?
|
|
|
|
if (blockIndex >= mBlocks.Length())
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
++blockIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_NOTREACHED("Should return in loop");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-04-01 00:52:56 +00:00
|
|
|
void
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream::SetReadMode(ReadMode aMode)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(gMediaCache->GetReentrantMonitor());
|
2009-04-01 00:52:56 +00:00
|
|
|
if (aMode == mCurrentMode)
|
|
|
|
return;
|
|
|
|
mCurrentMode = aMode;
|
|
|
|
gMediaCache->QueueUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream::SetPlaybackRate(uint32_t aBytesPerSecond)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aBytesPerSecond > 0, "Zero playback rate not allowed");
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(gMediaCache->GetReentrantMonitor());
|
2009-04-01 00:52:56 +00:00
|
|
|
if (aBytesPerSecond == mPlaybackBytesPerSecond)
|
|
|
|
return;
|
|
|
|
mPlaybackBytesPerSecond = aBytesPerSecond;
|
|
|
|
gMediaCache->QueueUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream::Seek(int32_t aWhence, int64_t aOffset)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Don't call on main thread");
|
|
|
|
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(gMediaCache->GetReentrantMonitor());
|
2009-04-01 00:52:56 +00:00
|
|
|
if (mClosed)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t oldOffset = mStreamOffset;
|
2009-04-01 00:52:56 +00:00
|
|
|
switch (aWhence) {
|
|
|
|
case PR_SEEK_END:
|
|
|
|
if (mStreamLength < 0)
|
|
|
|
return NS_ERROR_FAILURE;
|
2011-02-17 16:41:07 +00:00
|
|
|
mStreamOffset = mStreamLength + aOffset;
|
2009-04-01 00:52:56 +00:00
|
|
|
break;
|
|
|
|
case PR_SEEK_CUR:
|
2011-02-17 16:41:07 +00:00
|
|
|
mStreamOffset += aOffset;
|
2009-04-01 00:52:56 +00:00
|
|
|
break;
|
|
|
|
case PR_SEEK_SET:
|
2011-02-17 16:41:07 +00:00
|
|
|
mStreamOffset = aOffset;
|
2009-04-01 00:52:56 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_ERROR("Unknown whence");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-11-21 03:02:42 +00:00
|
|
|
CACHE_LOG(PR_LOG_DEBUG, ("Stream %p Seek to %lld", this, (long long)mStreamOffset));
|
2009-04-01 00:52:56 +00:00
|
|
|
gMediaCache->NoteSeek(this, oldOffset);
|
|
|
|
|
|
|
|
gMediaCache->QueueUpdate();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream::Tell()
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Don't call on main thread");
|
|
|
|
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(gMediaCache->GetReentrantMonitor());
|
2009-04-01 00:52:56 +00:00
|
|
|
return mStreamOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream::Read(char* aBuffer, uint32_t aCount, uint32_t* aBytes)
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Don't call on main thread");
|
|
|
|
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(gMediaCache->GetReentrantMonitor());
|
2009-04-01 00:52:56 +00:00
|
|
|
if (mClosed)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t count = 0;
|
2009-04-01 00:52:56 +00:00
|
|
|
// Read one block (or part of a block) at a time
|
|
|
|
while (count < aCount) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t streamBlock = uint32_t(mStreamOffset/BLOCK_SIZE);
|
|
|
|
uint32_t offsetInStreamBlock =
|
|
|
|
uint32_t(mStreamOffset - streamBlock*BLOCK_SIZE);
|
2013-01-15 12:22:03 +00:00
|
|
|
int64_t size = std::min(aCount - count, BLOCK_SIZE - offsetInStreamBlock);
|
2009-04-01 00:52:56 +00:00
|
|
|
|
|
|
|
if (mStreamLength >= 0) {
|
|
|
|
// Don't try to read beyond the end of the stream
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t bytesRemaining = mStreamLength - mStreamOffset;
|
2009-04-01 00:52:56 +00:00
|
|
|
if (bytesRemaining <= 0) {
|
|
|
|
// Get out of here and return NS_OK
|
|
|
|
break;
|
|
|
|
}
|
2013-01-15 12:22:03 +00:00
|
|
|
size = std::min(size, bytesRemaining);
|
2012-02-28 04:38:46 +00:00
|
|
|
// Clamp size until 64-bit file size issues (bug 500784) are fixed.
|
2013-01-15 12:22:03 +00:00
|
|
|
size = std::min(size, int64_t(INT32_MAX));
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t bytes;
|
|
|
|
int32_t cacheBlock = streamBlock < mBlocks.Length() ? mBlocks[streamBlock] : -1;
|
2011-11-23 23:01:50 +00:00
|
|
|
if (cacheBlock < 0) {
|
|
|
|
// We don't have a complete cached block here.
|
|
|
|
|
|
|
|
if (count > 0) {
|
|
|
|
// Some data has been read, so return what we've got instead of
|
|
|
|
// blocking or trying to find a stream with a partial block.
|
|
|
|
break;
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
2011-11-23 23:01:50 +00:00
|
|
|
|
|
|
|
// See if the data is available in the partial cache block of any
|
|
|
|
// stream reading this resource. We need to do this in case there is
|
|
|
|
// another stream with this resource that has all the data to the end of
|
|
|
|
// the stream but the data doesn't end on a block boundary.
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream* streamWithPartialBlock = nullptr;
|
|
|
|
MediaCache::ResourceStreamIterator iter(mResourceID);
|
|
|
|
while (MediaCacheStream* stream = iter.Next()) {
|
2012-08-22 15:56:38 +00:00
|
|
|
if (uint32_t(stream->mChannelOffset/BLOCK_SIZE) == streamBlock &&
|
2011-11-23 23:01:50 +00:00
|
|
|
mStreamOffset < stream->mChannelOffset) {
|
|
|
|
streamWithPartialBlock = stream;
|
2009-04-01 00:52:56 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-11-23 23:01:50 +00:00
|
|
|
}
|
|
|
|
if (streamWithPartialBlock) {
|
|
|
|
// We can just use the data in mPartialBlockBuffer. In fact we should
|
|
|
|
// use it rather than waiting for the block to fill and land in
|
|
|
|
// the cache.
|
2013-01-15 12:22:03 +00:00
|
|
|
bytes = std::min<int64_t>(size, streamWithPartialBlock->mChannelOffset - mStreamOffset);
|
2011-11-23 23:01:50 +00:00
|
|
|
memcpy(aBuffer,
|
2014-02-11 06:43:52 +00:00
|
|
|
reinterpret_cast<char*>(streamWithPartialBlock->mPartialBlockBuffer.get()) + offsetInStreamBlock, bytes);
|
2011-11-23 23:01:50 +00:00
|
|
|
if (mCurrentMode == MODE_METADATA) {
|
|
|
|
streamWithPartialBlock->mMetadataInPartialBlockBuffer = true;
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
2011-11-23 23:01:50 +00:00
|
|
|
mStreamOffset += bytes;
|
|
|
|
count = bytes;
|
|
|
|
break;
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
|
2011-11-23 23:01:50 +00:00
|
|
|
// No data has been read yet, so block
|
|
|
|
mon.Wait();
|
|
|
|
if (mClosed) {
|
|
|
|
// We may have successfully read some data, but let's just throw
|
|
|
|
// that out.
|
|
|
|
return NS_ERROR_FAILURE;
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
2011-11-23 23:01:50 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
gMediaCache->NoteBlockUsage(this, cacheBlock, mCurrentMode, TimeStamp::Now());
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t offset = cacheBlock*BLOCK_SIZE + offsetInStreamBlock;
|
2012-09-28 06:57:33 +00:00
|
|
|
NS_ABORT_IF_FALSE(size >= 0 && size <= INT32_MAX, "Size out of range.");
|
2012-08-22 15:56:38 +00:00
|
|
|
nsresult rv = gMediaCache->ReadCacheFile(offset, aBuffer + count, int32_t(size), &bytes);
|
2011-11-23 23:01:50 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
if (count == 0)
|
|
|
|
return rv;
|
|
|
|
// If we did successfully read some data, may as well return it
|
|
|
|
break;
|
2009-04-01 00:52:56 +00:00
|
|
|
}
|
|
|
|
mStreamOffset += bytes;
|
|
|
|
count += bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count > 0) {
|
|
|
|
// Some data was read, so queue an update since block priorities may
|
|
|
|
// have changed
|
|
|
|
gMediaCache->QueueUpdate();
|
|
|
|
}
|
2013-11-21 03:02:42 +00:00
|
|
|
CACHE_LOG(PR_LOG_DEBUG,
|
|
|
|
("Stream %p Read at %lld count=%d", this, (long long)(mStreamOffset-count), count));
|
2009-04-01 00:52:56 +00:00
|
|
|
*aBytes = count;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-07-17 01:54:52 +00:00
|
|
|
nsresult
|
|
|
|
MediaCacheStream::ReadAt(int64_t aOffset, char* aBuffer,
|
|
|
|
uint32_t aCount, uint32_t* aBytes)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!NS_IsMainThread(), "Don't call on main thread");
|
|
|
|
|
|
|
|
ReentrantMonitorAutoEnter mon(gMediaCache->GetReentrantMonitor());
|
|
|
|
nsresult rv = Seek(nsISeekableStream::NS_SEEK_SET, aOffset);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
return Read(aBuffer, aCount, aBytes);
|
|
|
|
}
|
|
|
|
|
2009-05-17 22:15:57 +00:00
|
|
|
nsresult
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream::ReadFromCache(char* aBuffer,
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t aOffset,
|
|
|
|
int64_t aCount)
|
2009-05-17 22:15:57 +00:00
|
|
|
{
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(gMediaCache->GetReentrantMonitor());
|
2009-05-17 22:15:57 +00:00
|
|
|
if (mClosed)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// Read one block (or part of a block) at a time
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t count = 0;
|
|
|
|
int64_t streamOffset = aOffset;
|
2009-05-17 22:15:57 +00:00
|
|
|
while (count < aCount) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t streamBlock = uint32_t(streamOffset/BLOCK_SIZE);
|
|
|
|
uint32_t offsetInStreamBlock =
|
|
|
|
uint32_t(streamOffset - streamBlock*BLOCK_SIZE);
|
2013-01-15 12:22:03 +00:00
|
|
|
int64_t size = std::min<int64_t>(aCount - count, BLOCK_SIZE - offsetInStreamBlock);
|
2009-05-17 22:15:57 +00:00
|
|
|
|
|
|
|
if (mStreamLength >= 0) {
|
|
|
|
// Don't try to read beyond the end of the stream
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t bytesRemaining = mStreamLength - streamOffset;
|
2009-05-17 22:15:57 +00:00
|
|
|
if (bytesRemaining <= 0) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2013-01-15 12:22:03 +00:00
|
|
|
size = std::min(size, bytesRemaining);
|
2012-02-29 23:52:02 +00:00
|
|
|
// Clamp size until 64-bit file size issues (bug 500784) are fixed.
|
2013-01-15 12:22:03 +00:00
|
|
|
size = std::min(size, int64_t(INT32_MAX));
|
2009-05-17 22:15:57 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t bytes;
|
|
|
|
uint32_t channelBlock = uint32_t(mChannelOffset/BLOCK_SIZE);
|
|
|
|
int32_t cacheBlock = streamBlock < mBlocks.Length() ? mBlocks[streamBlock] : -1;
|
2009-05-17 22:15:57 +00:00
|
|
|
if (channelBlock == streamBlock && streamOffset < mChannelOffset) {
|
|
|
|
// We can just use the data in mPartialBlockBuffer. In fact we should
|
|
|
|
// use it rather than waiting for the block to fill and land in
|
|
|
|
// the cache.
|
2013-01-15 12:22:03 +00:00
|
|
|
bytes = std::min<int64_t>(size, mChannelOffset - streamOffset);
|
2009-05-17 22:15:57 +00:00
|
|
|
memcpy(aBuffer + count,
|
2014-02-11 06:43:52 +00:00
|
|
|
reinterpret_cast<char*>(mPartialBlockBuffer.get()) + offsetInStreamBlock, bytes);
|
2009-05-17 22:15:57 +00:00
|
|
|
} else {
|
|
|
|
if (cacheBlock < 0) {
|
|
|
|
// We expect all blocks to be cached! Fail!
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t offset = cacheBlock*BLOCK_SIZE + offsetInStreamBlock;
|
2012-09-28 06:57:33 +00:00
|
|
|
NS_ABORT_IF_FALSE(size >= 0 && size <= INT32_MAX, "Size out of range.");
|
2012-08-22 15:56:38 +00:00
|
|
|
nsresult rv = gMediaCache->ReadCacheFile(offset, aBuffer + count, int32_t(size), &bytes);
|
2009-05-17 22:15:57 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
streamOffset += bytes;
|
|
|
|
count += bytes;
|
|
|
|
}
|
2010-10-26 01:21:16 +00:00
|
|
|
|
2009-05-17 22:15:57 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-04-01 00:52:56 +00:00
|
|
|
nsresult
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream::Init()
|
2009-04-01 00:52:56 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
2009-09-15 02:30:44 +00:00
|
|
|
if (mInitialized)
|
|
|
|
return NS_OK;
|
|
|
|
|
2009-04-01 00:52:56 +00:00
|
|
|
InitMediaCache();
|
|
|
|
if (!gMediaCache)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
gMediaCache->OpenStream(this);
|
2011-09-29 23:34:37 +00:00
|
|
|
mInitialized = true;
|
2009-09-15 02:30:44 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-11-14 19:46:40 +00:00
|
|
|
MediaCacheStream::InitAsClone(MediaCacheStream* aOriginal)
|
2009-09-15 02:30:44 +00:00
|
|
|
{
|
2012-03-20 07:55:40 +00:00
|
|
|
if (!aOriginal->IsAvailableForSharing())
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2009-09-15 02:30:44 +00:00
|
|
|
if (mInitialized)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsresult rv = Init();
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2009-09-15 02:30:45 +00:00
|
|
|
mResourceID = aOriginal->mResourceID;
|
2009-09-15 02:30:44 +00:00
|
|
|
|
|
|
|
// Grab cache blocks from aOriginal as readahead blocks for our stream
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(gMediaCache->GetReentrantMonitor());
|
2009-09-15 02:30:44 +00:00
|
|
|
|
|
|
|
mPrincipal = aOriginal->mPrincipal;
|
|
|
|
mStreamLength = aOriginal->mStreamLength;
|
2012-11-30 13:17:54 +00:00
|
|
|
mIsTransportSeekable = aOriginal->mIsTransportSeekable;
|
2009-09-15 02:30:44 +00:00
|
|
|
|
2009-10-09 11:48:30 +00:00
|
|
|
// Cloned streams are initially suspended, since there is no channel open
|
|
|
|
// initially for a clone.
|
2011-09-29 23:34:37 +00:00
|
|
|
mCacheSuspended = true;
|
2011-11-30 23:09:10 +00:00
|
|
|
mChannelEnded = true;
|
2009-10-09 11:48:30 +00:00
|
|
|
|
2011-11-23 23:05:12 +00:00
|
|
|
if (aOriginal->mDidNotifyDataEnded) {
|
|
|
|
mNotifyDataEndedStatus = aOriginal->mNotifyDataEndedStatus;
|
|
|
|
mDidNotifyDataEnded = true;
|
|
|
|
mClient->CacheClientNotifyDataEnded(mNotifyDataEndedStatus);
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < aOriginal->mBlocks.Length(); ++i) {
|
|
|
|
int32_t cacheBlockIndex = aOriginal->mBlocks[i];
|
2009-09-15 02:30:44 +00:00
|
|
|
if (cacheBlockIndex < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
while (i >= mBlocks.Length()) {
|
|
|
|
mBlocks.AppendElement(-1);
|
|
|
|
}
|
2009-10-09 11:48:30 +00:00
|
|
|
// Every block is a readahead block for the clone because the clone's initial
|
|
|
|
// stream offset is zero
|
2009-09-15 02:30:44 +00:00
|
|
|
gMediaCache->AddBlockOwnerAsReadahead(cacheBlockIndex, this, i);
|
|
|
|
}
|
|
|
|
|
2009-04-01 00:52:56 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-03-23 22:28:58 +00:00
|
|
|
|
2012-11-14 19:46:40 +00:00
|
|
|
nsresult MediaCacheStream::GetCachedRanges(nsTArray<MediaByteRange>& aRanges)
|
2011-03-23 22:28:58 +00:00
|
|
|
{
|
|
|
|
// Take the monitor, so that the cached data ranges can't grow while we're
|
|
|
|
// trying to loop over them.
|
2011-04-29 19:21:57 +00:00
|
|
|
ReentrantMonitorAutoEnter mon(gMediaCache->GetReentrantMonitor());
|
2011-03-23 22:28:58 +00:00
|
|
|
|
|
|
|
// We must be pinned while running this, otherwise the cached data ranges may
|
|
|
|
// shrink while we're trying to loop over them.
|
|
|
|
NS_ASSERTION(mPinCount > 0, "Must be pinned");
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t startOffset = GetNextCachedData(0);
|
2011-03-23 22:28:58 +00:00
|
|
|
while (startOffset >= 0) {
|
2012-08-22 15:56:38 +00:00
|
|
|
int64_t endOffset = GetCachedDataEnd(startOffset);
|
2011-03-23 22:28:58 +00:00
|
|
|
NS_ASSERTION(startOffset < endOffset, "Buffered range must end after its start");
|
|
|
|
// Bytes [startOffset..endOffset] are cached.
|
2012-02-15 04:35:01 +00:00
|
|
|
aRanges.AppendElement(MediaByteRange(startOffset, endOffset));
|
2011-03-23 22:28:58 +00:00
|
|
|
startOffset = GetNextCachedData(endOffset);
|
|
|
|
NS_ASSERTION(startOffset == -1 || startOffset > endOffset,
|
|
|
|
"Must have advanced to start of next range, or hit end of stream");
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-11-14 19:45:33 +00:00
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|