2009-09-15 06:30:44 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2009-04-01 04:52:56 +04:00
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
2012-05-21 15:12:37 +04: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 04:52:56 +04:00
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
#include "MediaCache.h"
|
2017-06-20 18:56:55 +03:00
|
|
|
|
2017-09-18 14:33:49 +03:00
|
|
|
#include "ChannelMediaResource.h"
|
2012-03-28 04:04:26 +04:00
|
|
|
#include "FileBlockCache.h"
|
2017-06-09 05:26:22 +03:00
|
|
|
#include "MediaBlockCacheBase.h"
|
2017-06-20 18:56:55 +03:00
|
|
|
#include "MediaResource.h"
|
2017-06-09 09:56:34 +03:00
|
|
|
#include "MemoryBlockCache.h"
|
2013-09-06 00:25:17 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2017-10-06 12:41:21 +03:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2017-11-02 06:36:56 +03:00
|
|
|
#include "mozilla/ErrorNames.h"
|
2017-06-20 18:56:55 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2018-01-05 05:49:38 +03:00
|
|
|
#include "mozilla/Monitor.h"
|
2017-06-20 18:56:55 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
2013-09-06 00:25:17 +04:00
|
|
|
#include "mozilla/Services.h"
|
2017-06-08 05:32:13 +03:00
|
|
|
#include "mozilla/StaticPtr.h"
|
2020-03-10 02:36:39 +03:00
|
|
|
#include "mozilla/StaticPrefs_browser.h"
|
2019-07-26 04:10:23 +03:00
|
|
|
#include "mozilla/StaticPrefs_media.h"
|
2017-05-23 05:44:06 +03:00
|
|
|
#include "mozilla/Telemetry.h"
|
2017-06-20 18:56:55 +03:00
|
|
|
#include "nsContentUtils.h"
|
2019-05-07 02:38:02 +03:00
|
|
|
#include "nsINetworkLinkService.h"
|
2017-06-20 18:56:55 +03:00
|
|
|
#include "nsIObserverService.h"
|
2017-10-25 08:57:39 +03:00
|
|
|
#include "nsPrintfCString.h"
|
2017-10-24 05:32:47 +03:00
|
|
|
#include "nsProxyRelease.h"
|
2021-03-23 13:36:38 +03:00
|
|
|
#include "nsTHashSet.h"
|
2017-06-20 18:56:55 +03:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "prio.h"
|
2019-05-07 02:38:02 +03:00
|
|
|
#include "VideoUtils.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2012-11-14 23:45:33 +04: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 08:29:02 +04:00
|
|
|
|
2017-04-12 09:51:34 +03:00
|
|
|
#undef LOG
|
2017-05-11 11:49:36 +03:00
|
|
|
#undef LOGI
|
2017-11-02 06:36:56 +03:00
|
|
|
#undef LOGE
|
2015-11-15 16:49:01 +03:00
|
|
|
LazyLogModule gMediaCacheLog("MediaCache");
|
2017-04-12 09:51:34 +03:00
|
|
|
#define LOG(...) MOZ_LOG(gMediaCacheLog, LogLevel::Debug, (__VA_ARGS__))
|
2017-05-11 11:49:36 +03:00
|
|
|
#define LOGI(...) MOZ_LOG(gMediaCacheLog, LogLevel::Info, (__VA_ARGS__))
|
2017-11-02 06:36:56 +03:00
|
|
|
#define LOGE(...) \
|
|
|
|
NS_DebugBreak(NS_DEBUG_WARNING, nsPrintfCString(__VA_ARGS__).get(), nullptr, \
|
|
|
|
__FILE__, __LINE__)
|
2017-04-12 09:51:34 +03:00
|
|
|
|
2017-10-04 17:55:50 +03:00
|
|
|
// For HTTP seeking, if number of bytes needing to be
|
|
|
|
// seeked forward is less than this value then a read is
|
|
|
|
// done rather than a byte range request.
|
|
|
|
//
|
|
|
|
// If we assume a 100Mbit connection, and assume reissuing an HTTP seek causes
|
|
|
|
// a delay of 200ms, then in that 200ms we could have simply read ahead 2MB. So
|
|
|
|
// setting SEEK_VS_READ_THRESHOLD to 1MB sounds reasonable.
|
|
|
|
static const int64_t SEEK_VS_READ_THRESHOLD = 1 * 1024 * 1024;
|
2009-04-01 04:52:56 +04:00
|
|
|
|
|
|
|
// 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 17:52:57 +04: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 04:52:56 +04:00
|
|
|
|
|
|
|
// When looking for a reusable block, scan forward this many blocks
|
2010-10-26 05:21:16 +04:00
|
|
|
// from the desired "best" block location to look for free blocks,
|
2009-04-01 04:52:56 +04: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 19:56:38 +04:00
|
|
|
static const uint32_t FREE_BLOCK_SCAN_LIMIT = 16;
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2009-06-12 05:43:23 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
// Turn this on to do very expensive cache state validation
|
|
|
|
// #define DEBUG_VERIFY_CACHE
|
|
|
|
#endif
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class MediaCacheFlusher final : public nsIObserver,
|
2015-03-27 21:52:19 +03:00
|
|
|
public nsSupportsWeakReference {
|
2010-09-29 04:02:08 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
2017-06-08 05:32:13 +03:00
|
|
|
static void RegisterMediaCache(MediaCache* aMediaCache);
|
|
|
|
static void UnregisterMediaCache(MediaCache* aMediaCache);
|
|
|
|
|
|
|
|
private:
|
2020-03-04 18:39:20 +03:00
|
|
|
MediaCacheFlusher() = default;
|
|
|
|
~MediaCacheFlusher() = default;
|
2017-06-08 05:32:13 +03:00
|
|
|
|
|
|
|
// Singleton instance created when a first MediaCache is registered, and
|
|
|
|
// released when the last MediaCache is unregistered.
|
|
|
|
// The observer service will keep a weak reference to it, for notifications.
|
|
|
|
static StaticRefPtr<MediaCacheFlusher> gMediaCacheFlusher;
|
|
|
|
|
|
|
|
nsTArray<MediaCache*> mMediaCaches;
|
2010-09-29 04:02:08 +04:00
|
|
|
};
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
StaticRefPtr<MediaCacheFlusher> MediaCacheFlusher::gMediaCacheFlusher;
|
2010-09-29 04:02:08 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(MediaCacheFlusher, nsIObserver, nsISupportsWeakReference)
|
2010-09-29 04:02:08 +04:00
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
void MediaCacheFlusher::RegisterMediaCache(MediaCache* aMediaCache) {
|
2017-06-08 05:32:13 +03:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
|
|
|
if (!gMediaCacheFlusher) {
|
|
|
|
gMediaCacheFlusher = new MediaCacheFlusher();
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (observerService) {
|
|
|
|
observerService->AddObserver(gMediaCacheFlusher, "last-pb-context-exited",
|
|
|
|
true);
|
|
|
|
observerService->AddObserver(gMediaCacheFlusher,
|
|
|
|
"cacheservice:empty-cache", true);
|
2019-05-07 02:38:02 +03:00
|
|
|
observerService->AddObserver(
|
|
|
|
gMediaCacheFlusher, "contentchild:network-link-type-changed", true);
|
|
|
|
observerService->AddObserver(gMediaCacheFlusher,
|
|
|
|
NS_NETWORK_LINK_TYPE_TOPIC, true);
|
2017-06-08 05:32:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gMediaCacheFlusher->mMediaCaches.AppendElement(aMediaCache);
|
2010-09-29 04:02:08 +04:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
void MediaCacheFlusher::UnregisterMediaCache(MediaCache* aMediaCache) {
|
2017-06-08 05:32:13 +03:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
2010-09-29 04:02:08 +04:00
|
|
|
|
2017-06-08 05:32:13 +03:00
|
|
|
gMediaCacheFlusher->mMediaCaches.RemoveElement(aMediaCache);
|
2010-09-29 04:02:08 +04:00
|
|
|
|
2017-06-08 05:32:13 +03:00
|
|
|
if (gMediaCacheFlusher->mMediaCaches.Length() == 0) {
|
|
|
|
gMediaCacheFlusher = nullptr;
|
2010-09-29 04:02:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-15 06:02:25 +03:00
|
|
|
class MediaCache {
|
2018-01-05 05:49:38 +03:00
|
|
|
using AutoLock = MonitorAutoLock;
|
2017-11-22 12:14:02 +03:00
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
public:
|
2017-06-15 06:02:25 +03:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(MediaCache)
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
friend class MediaCacheStream::BlockList;
|
|
|
|
typedef MediaCacheStream::BlockList BlockList;
|
2015-10-12 02:01:26 +03:00
|
|
|
static const int64_t BLOCK_SIZE = MediaCacheStream::BLOCK_SIZE;
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2017-06-08 06:59:43 +03:00
|
|
|
// Get an instance of a MediaCache (or nullptr if initialization failed).
|
|
|
|
// aContentLength is the content length if known already, otherwise -1.
|
|
|
|
// If the length is known and considered small enough, a discrete MediaCache
|
|
|
|
// with memory backing will be given. Otherwise the one MediaCache with
|
|
|
|
// file backing will be provided.
|
2020-01-22 03:19:28 +03:00
|
|
|
// If aIsPrivateBrowsing is true, only initialization of a memory backed
|
|
|
|
// MediaCache will be attempted, returning nullptr if that fails.
|
|
|
|
static RefPtr<MediaCache> GetMediaCache(int64_t aContentLength,
|
|
|
|
bool aIsPrivateBrowsing);
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2019-11-14 01:40:14 +03:00
|
|
|
nsISerialEventTarget* OwnerThread() const { return sThread; }
|
2017-09-13 11:51:12 +03:00
|
|
|
|
2010-10-13 18:14:08 +04:00
|
|
|
// Brutally flush the cache contents. Main thread only.
|
2017-06-08 05:32:13 +03:00
|
|
|
void Flush();
|
2010-10-13 18:14:08 +04:00
|
|
|
|
2017-04-06 11:39:14 +03:00
|
|
|
// Close all streams associated with private browsing windows. This will
|
|
|
|
// also remove the blocks from the cache since we don't want to leave any
|
|
|
|
// traces when PB is done.
|
2017-06-08 05:32:13 +03:00
|
|
|
void CloseStreamsForPrivateBrowsing();
|
2017-04-06 11:39:14 +03:00
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
// Cache-file access methods. These are the lowest-level cache methods.
|
2020-10-03 03:32:53 +03:00
|
|
|
// mMonitor must be held; these can be called on any thread.
|
2009-04-01 04:52:56 +04:00
|
|
|
// This can return partial reads.
|
2020-10-03 03:32:53 +03:00
|
|
|
// Note mMonitor will be dropped while doing IO. The caller need
|
2017-04-10 06:55:26 +03:00
|
|
|
// to handle changes happening when the monitor is not held.
|
2017-11-22 12:14:02 +03:00
|
|
|
nsresult ReadCacheFile(AutoLock&, int64_t aOffset, void* aData,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aLength, int32_t* aBytes);
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2017-10-26 06:13:38 +03:00
|
|
|
// The generated IDs are always positive.
|
2017-11-22 12:14:02 +03:00
|
|
|
int64_t AllocateResourceID(AutoLock&) { return ++mNextResourceID; }
|
2012-03-20 11:55:40 +04:00
|
|
|
|
2020-10-03 03:32:53 +03:00
|
|
|
// mMonitor must be held, called on main thread.
|
2009-04-01 04:52:56 +04: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.
|
2017-11-22 12:14:02 +03:00
|
|
|
void OpenStream(AutoLock&, MediaCacheStream* aStream, bool aIsClone = false);
|
2009-04-01 04:52:56 +04:00
|
|
|
// Remove aStream from the list of streams.
|
2017-12-19 12:51:51 +03:00
|
|
|
void ReleaseStream(AutoLock&, MediaCacheStream* aStream);
|
2009-04-01 04:52:56 +04:00
|
|
|
// Free all blocks belonging to aStream.
|
2017-11-22 12:14:02 +03:00
|
|
|
void ReleaseStreamBlocks(AutoLock&, MediaCacheStream* aStream);
|
2009-04-01 04:52:56 +04:00
|
|
|
// Find a cache entry for this data, and write the data into it
|
2017-09-14 05:39:06 +03:00
|
|
|
void AllocateAndWriteBlock(
|
|
|
|
AutoLock&, MediaCacheStream* aStream, int32_t aStreamBlockIndex,
|
2021-04-09 19:03:44 +03:00
|
|
|
Span<const uint8_t> aData1,
|
2017-04-11 10:47:46 +03:00
|
|
|
Span<const uint8_t> aData2 = Span<const uint8_t>());
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2020-10-03 03:32:53 +03:00
|
|
|
// mMonitor must be held; can be called on any thread
|
2009-04-01 04:52:56 +04: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().
|
2017-11-22 12:14:02 +03:00
|
|
|
void NoteSeek(AutoLock&, MediaCacheStream* aStream, int64_t aOldOffset);
|
2009-04-01 04:52:56 +04: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().
|
2017-11-22 12:14:02 +03:00
|
|
|
void NoteBlockUsage(AutoLock&, MediaCacheStream* aStream, int32_t aBlockIndex,
|
|
|
|
int64_t aStreamOffset, MediaCacheStream::ReadMode aMode,
|
|
|
|
TimeStamp aNow);
|
2009-09-15 06:30:44 +04:00
|
|
|
// Mark aStream as having the block, adding it as an owner.
|
2017-11-22 12:14:02 +03:00
|
|
|
void AddBlockOwnerAsReadahead(AutoLock&, int32_t aBlockIndex,
|
|
|
|
MediaCacheStream* aStream,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aStreamBlockIndex);
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2021-04-21 00:35:31 +03:00
|
|
|
// This queues a call to Update() on the media cache thread.
|
2017-11-22 12:14:02 +03:00
|
|
|
void QueueUpdate(AutoLock&);
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2014-12-19 01:48:00 +03:00
|
|
|
// Notify all streams for the resource ID that the suspended status changed
|
|
|
|
// at the end of MediaCache::Update.
|
2017-11-22 12:14:02 +03:00
|
|
|
void QueueSuspendedStatusUpdate(AutoLock&, int64_t aResourceID);
|
2014-12-19 01:48:00 +03:00
|
|
|
|
2021-04-21 00:35:31 +03:00
|
|
|
// Updates the cache state asynchronously on the media cache thread:
|
2009-04-01 04:52:56 +04:00
|
|
|
// -- 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 05:43:23 +04:00
|
|
|
#ifdef DEBUG_VERIFY_CACHE
|
2009-04-01 04:52:56 +04:00
|
|
|
// Verify invariants, especially block list invariants
|
2017-11-22 12:14:02 +03:00
|
|
|
void Verify(AutoLock&);
|
2009-04-01 04:52:56 +04:00
|
|
|
#else
|
2017-11-22 12:14:02 +03:00
|
|
|
void Verify(AutoLock&) {}
|
2009-04-01 04:52:56 +04:00
|
|
|
#endif
|
|
|
|
|
2018-01-05 05:49:38 +03:00
|
|
|
mozilla::Monitor& Monitor() {
|
2018-05-24 15:36:58 +03:00
|
|
|
// This method should only be called outside the main thread.
|
|
|
|
// The MOZ_DIAGNOSTIC_ASSERT(!NS_IsMainThread()) assertion should be
|
|
|
|
// re-added as part of bug 1464045
|
2017-12-20 10:28:52 +03:00
|
|
|
return mMonitor;
|
|
|
|
}
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2019-05-07 02:38:02 +03:00
|
|
|
// Polls whether we're on a cellular network connection, and posts a task
|
|
|
|
// to the MediaCache thread to set the value of MediaCache::sOnCellular.
|
|
|
|
// Call on main thread only.
|
|
|
|
static void UpdateOnCellular();
|
|
|
|
|
2009-11-18 03:38:16 +03:00
|
|
|
/**
|
|
|
|
* An iterator that makes it easy to iterate through all streams that
|
|
|
|
* have a given resource ID and are not closed.
|
2017-12-15 06:23:27 +03:00
|
|
|
* Must be used while holding the media cache lock.
|
2009-11-18 03:38:16 +03:00
|
|
|
*/
|
2017-06-08 06:32:13 +03:00
|
|
|
class ResourceStreamIterator {
|
2009-09-15 06:30:45 +04:00
|
|
|
public:
|
2017-06-08 06:32:13 +03:00
|
|
|
ResourceStreamIterator(MediaCache* aMediaCache, int64_t aResourceID)
|
|
|
|
: mMediaCache(aMediaCache), mResourceID(aResourceID), mNext(0) {
|
2018-01-05 05:49:38 +03:00
|
|
|
aMediaCache->mMonitor.AssertCurrentThreadOwns();
|
2017-06-08 06:32:13 +03:00
|
|
|
}
|
2017-12-14 11:22:53 +03:00
|
|
|
MediaCacheStream* Next(AutoLock& aLock) {
|
2017-06-08 06:32:13 +03:00
|
|
|
while (mNext < mMediaCache->mStreams.Length()) {
|
|
|
|
MediaCacheStream* stream = mMediaCache->mStreams[mNext];
|
2009-09-15 06:30:45 +04:00
|
|
|
++mNext;
|
2017-12-14 11:22:53 +03:00
|
|
|
if (stream->GetResourceID() == mResourceID && !stream->IsClosed(aLock))
|
2009-09-15 06:30:45 +04:00
|
|
|
return stream;
|
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2009-09-15 06:30:45 +04:00
|
|
|
}
|
2018-11-19 16:25:37 +03:00
|
|
|
|
2009-09-15 06:30:45 +04:00
|
|
|
private:
|
2017-06-08 06:32:13 +03:00
|
|
|
MediaCache* mMediaCache;
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t mResourceID;
|
|
|
|
uint32_t mNext;
|
2009-09-15 06:30:45 +04:00
|
|
|
};
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
protected:
|
2017-06-15 08:32:26 +03:00
|
|
|
explicit MediaCache(MediaBlockCacheBase* aCache)
|
2017-11-22 12:14:02 +03:00
|
|
|
: mMonitor("MediaCache.mMonitor"),
|
2017-06-15 07:32:06 +03:00
|
|
|
mBlockCache(aCache),
|
2017-06-09 03:53:11 +03:00
|
|
|
mUpdateQueued(false)
|
|
|
|
#ifdef DEBUG
|
|
|
|
,
|
|
|
|
mInUpdate(false)
|
|
|
|
#endif
|
|
|
|
{
|
2017-06-15 06:02:25 +03:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only construct MediaCache on main thread");
|
2017-06-09 03:53:11 +03:00
|
|
|
MOZ_COUNT_CTOR(MediaCache);
|
|
|
|
MediaCacheFlusher::RegisterMediaCache(this);
|
2019-05-07 02:38:02 +03:00
|
|
|
UpdateOnCellular();
|
2017-06-09 03:53:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
~MediaCache() {
|
2017-06-15 06:02:25 +03:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only destroy MediaCache on main thread");
|
|
|
|
if (this == gMediaCache) {
|
2017-06-15 08:10:54 +03:00
|
|
|
LOG("~MediaCache(Global file-backed MediaCache)");
|
2017-06-15 06:02:25 +03:00
|
|
|
// This is the file-backed MediaCache, reset the global pointer.
|
|
|
|
gMediaCache = nullptr;
|
2017-06-15 08:10:54 +03:00
|
|
|
} else {
|
|
|
|
LOG("~MediaCache(Memory-backed MediaCache %p)", this);
|
2017-06-09 09:56:34 +03:00
|
|
|
}
|
2017-06-15 08:10:54 +03:00
|
|
|
MediaCacheFlusher::UnregisterMediaCache(this);
|
|
|
|
NS_ASSERTION(mStreams.IsEmpty(), "Stream(s) still open!");
|
|
|
|
Truncate();
|
|
|
|
NS_ASSERTION(mIndex.Length() == 0, "Blocks leaked?");
|
2017-06-09 03:53:11 +03:00
|
|
|
|
|
|
|
MOZ_COUNT_DTOR(MediaCache);
|
|
|
|
}
|
|
|
|
|
2019-05-07 02:38:02 +03:00
|
|
|
static size_t CacheSize() {
|
|
|
|
MOZ_ASSERT(sThread->IsOnCurrentThread());
|
2019-06-28 07:09:05 +03:00
|
|
|
return sOnCellular ? StaticPrefs::media_cache_size_cellular()
|
|
|
|
: StaticPrefs::media_cache_size();
|
2019-05-07 02:38:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t ReadaheadLimit() {
|
|
|
|
MOZ_ASSERT(sThread->IsOnCurrentThread());
|
2019-06-28 07:09:05 +03:00
|
|
|
return sOnCellular ? StaticPrefs::media_cache_readahead_limit_cellular()
|
|
|
|
: StaticPrefs::media_cache_readahead_limit();
|
2019-05-07 02:38:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t ResumeThreshold() {
|
2019-06-28 07:09:05 +03:00
|
|
|
return sOnCellular ? StaticPrefs::media_cache_resume_threshold_cellular()
|
|
|
|
: StaticPrefs::media_cache_resume_threshold();
|
2019-05-07 02:38:02 +03:00
|
|
|
}
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
// 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.
|
2017-11-22 12:14:02 +03:00
|
|
|
int32_t FindBlockForIncomingData(AutoLock&, TimeStamp aNow,
|
2017-09-14 05:42:54 +03:00
|
|
|
MediaCacheStream* aStream,
|
|
|
|
int32_t aStreamBlockIndex);
|
2009-04-01 04:52:56 +04: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.
|
2017-11-22 12:14:02 +03:00
|
|
|
int32_t FindReusableBlock(AutoLock&, TimeStamp aNow,
|
2012-11-14 23:46:40 +04:00
|
|
|
MediaCacheStream* aForStream,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aForStreamBlock,
|
|
|
|
int32_t aMaxSearchBlockIndex);
|
2017-11-22 12:14:02 +03:00
|
|
|
bool BlockIsReusable(AutoLock&, int32_t aBlockIndex);
|
2009-04-01 04:52:56 +04: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.
|
2017-11-22 12:14:02 +03:00
|
|
|
void AppendMostReusableBlock(AutoLock&, BlockList* aBlockList,
|
2012-08-22 19:56:38 +04:00
|
|
|
nsTArray<uint32_t>* aResult,
|
|
|
|
int32_t aBlockIndexLimit);
|
2009-04-01 04:52:56 +04: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 06:30:44 +04:00
|
|
|
struct BlockOwner {
|
2020-03-04 18:39:20 +03:00
|
|
|
constexpr BlockOwner() = default;
|
2009-04-01 04:52:56 +04:00
|
|
|
|
|
|
|
// The stream that owns this block, or null if the block is free.
|
2016-08-10 10:04:39 +03:00
|
|
|
MediaCacheStream* mStream = nullptr;
|
2009-04-01 04:52:56 +04:00
|
|
|
// The block index in the stream. Valid only if mStream is non-null.
|
2016-08-10 10:04:39 +03:00
|
|
|
// Initialized to an insane value to highlight misuse.
|
|
|
|
uint32_t mStreamBlock = UINT32_MAX;
|
2009-04-01 04:52:56 +04:00
|
|
|
// Time at which this block was last used. Valid only if
|
|
|
|
// mClass is METADATA_BLOCK or PLAYED_BLOCK.
|
2016-08-10 10:04:39 +03:00
|
|
|
TimeStamp mLastUseTime;
|
|
|
|
BlockClass mClass = READAHEAD_BLOCK;
|
2009-04-01 04:52:56 +04:00
|
|
|
};
|
|
|
|
|
2009-09-15 06:30:44 +04:00
|
|
|
struct Block {
|
|
|
|
// Free blocks have an empty mOwners array
|
|
|
|
nsTArray<BlockOwner> mOwners;
|
|
|
|
};
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
// Get the BlockList that the block should belong to given its
|
2009-09-15 06:30:44 +04:00
|
|
|
// current owner
|
2017-11-22 12:14:02 +03:00
|
|
|
BlockList* GetListForBlock(AutoLock&, BlockOwner* aBlock);
|
2009-09-15 06:30:44 +04:00
|
|
|
// Get the BlockOwner for the given block index and owning stream
|
|
|
|
// (returns null if the stream does not own the block)
|
2017-11-22 12:14:02 +03:00
|
|
|
BlockOwner* GetBlockOwner(AutoLock&, int32_t aBlockIndex,
|
|
|
|
MediaCacheStream* aStream);
|
2009-09-15 06:30:44 +04:00
|
|
|
// Returns true iff the block is free
|
2012-08-22 19:56:38 +04:00
|
|
|
bool IsBlockFree(int32_t aBlockIndex) {
|
2009-09-15 06:30:44 +04:00
|
|
|
return mIndex[aBlockIndex].mOwners.IsEmpty();
|
|
|
|
}
|
|
|
|
// Add the block to the free list and mark its streams as not having
|
|
|
|
// the block in cache
|
2017-11-22 12:14:02 +03:00
|
|
|
void FreeBlock(AutoLock&, int32_t aBlock);
|
2009-09-15 06:30:44 +04: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.
|
2017-11-22 12:14:02 +03:00
|
|
|
void RemoveBlockOwner(AutoLock&, int32_t aBlockIndex,
|
|
|
|
MediaCacheStream* aStream);
|
2009-04-01 04:52:56 +04:00
|
|
|
// Swap all metadata associated with the two blocks. The caller
|
|
|
|
// is responsible for swapping up any cache file state.
|
2017-11-22 12:14:02 +03:00
|
|
|
void SwapBlocks(AutoLock&, int32_t aBlockIndex1, int32_t aBlockIndex2);
|
2009-09-15 06:30:44 +04:00
|
|
|
// Insert the block into the readahead block list for the stream
|
2009-04-01 04:52:56 +04:00
|
|
|
// at the right point in the list.
|
2017-11-22 12:14:02 +03:00
|
|
|
void InsertReadaheadBlock(AutoLock&, BlockOwner* aBlockOwner,
|
|
|
|
int32_t aBlockIndex);
|
2009-04-01 04:52:56 +04:00
|
|
|
|
|
|
|
// Guess the duration until block aBlock will be next used
|
2017-11-22 12:14:02 +03:00
|
|
|
TimeDuration PredictNextUse(AutoLock&, TimeStamp aNow, int32_t aBlock);
|
2009-04-01 04:52:56 +04:00
|
|
|
// Guess the duration until the next incoming data on aStream will be used
|
2017-11-22 12:14:02 +03:00
|
|
|
TimeDuration PredictNextUseForIncomingData(AutoLock&,
|
|
|
|
MediaCacheStream* aStream);
|
2009-04-01 04:52:56 +04:00
|
|
|
|
|
|
|
// Truncate the file and index array if there are free blocks at the
|
|
|
|
// end
|
|
|
|
void Truncate();
|
|
|
|
|
2017-11-27 10:04:21 +03:00
|
|
|
void FlushInternal(AutoLock&);
|
|
|
|
|
2017-06-08 08:12:13 +03:00
|
|
|
// There is at most one file-backed media cache.
|
2017-06-15 06:02:25 +03:00
|
|
|
// It is owned by all MediaCacheStreams that use it.
|
|
|
|
// This is a raw pointer set by GetMediaCache(), and reset by ~MediaCache(),
|
|
|
|
// both on the main thread; and is not accessed anywhere else.
|
2021-04-21 00:35:30 +03:00
|
|
|
static inline MediaCache* gMediaCache = nullptr;
|
2017-06-08 08:12:13 +03:00
|
|
|
|
2009-09-15 06:30:45 +04:00
|
|
|
// This member is main-thread only. It's used to allocate unique
|
|
|
|
// resource IDs to streams.
|
2017-10-26 06:13:38 +03:00
|
|
|
int64_t mNextResourceID = 0;
|
2009-04-01 04:52:56 +04: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.
|
2018-01-05 05:49:38 +03:00
|
|
|
mozilla::Monitor mMonitor;
|
2017-12-15 06:23:27 +03:00
|
|
|
// This must always be accessed when the monitor is held.
|
2012-11-14 23:46:40 +04:00
|
|
|
nsTArray<MediaCacheStream*> mStreams;
|
2009-04-01 04:52:56 +04:00
|
|
|
// The Blocks describing the cache entries.
|
|
|
|
nsTArray<Block> mIndex;
|
2020-10-03 03:36:40 +03:00
|
|
|
|
2017-06-14 08:01:46 +03:00
|
|
|
RefPtr<MediaBlockCacheBase> mBlockCache;
|
2009-04-01 04:52:56 +04: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 10:19:26 +04:00
|
|
|
bool mUpdateQueued;
|
2009-04-01 04:52:56 +04:00
|
|
|
#ifdef DEBUG
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mInUpdate;
|
2009-04-01 04:52:56 +04:00
|
|
|
#endif
|
2014-12-19 01:48:00 +03:00
|
|
|
// A list of resource IDs to notify about the change in suspended status.
|
|
|
|
nsTArray<int64_t> mSuspendedStatusToNotify;
|
2017-09-13 11:51:12 +03:00
|
|
|
// The thread on which we will run data callbacks from the channels.
|
2017-10-23 11:51:17 +03:00
|
|
|
// Note this thread is shared among all MediaCache instances.
|
2021-04-21 00:35:30 +03:00
|
|
|
static inline StaticRefPtr<nsIThread> sThread;
|
2017-10-06 12:41:21 +03:00
|
|
|
// True if we've tried to init sThread. Note we try once only so it is safe
|
|
|
|
// to access sThread on all threads.
|
2021-04-21 00:35:30 +03:00
|
|
|
static inline bool sThreadInit = false;
|
2018-01-09 05:29:10 +03:00
|
|
|
|
|
|
|
private:
|
2019-05-07 02:38:02 +03:00
|
|
|
// MediaCache thread only. True if we're on a cellular network connection.
|
2021-04-21 00:35:30 +03:00
|
|
|
static inline bool sOnCellular = false;
|
2019-05-07 02:38:02 +03:00
|
|
|
|
2021-04-21 00:35:30 +03:00
|
|
|
// Try to trim the cache back to its desired size, if necessary. Return the
|
|
|
|
// amount of free block counts after trimming.
|
|
|
|
int32_t TrimCacheIfNeeded(AutoLock& aLock, const TimeStamp& aNow);
|
|
|
|
|
2021-04-21 00:35:31 +03:00
|
|
|
struct StreamAction {
|
|
|
|
enum { NONE, SEEK, RESUME, SUSPEND } mTag = NONE;
|
|
|
|
// Members for 'SEEK' only.
|
|
|
|
bool mResume = false;
|
|
|
|
int64_t mSeekTarget = -1;
|
|
|
|
};
|
|
|
|
// In each update, media cache would determine an action for each stream,
|
|
|
|
// possible actions are: keeping the stream unchanged, seeking to the new
|
|
|
|
// position, resuming its channel or suspending its channel. The action would
|
|
|
|
// be determined by considering a lot of different factors, eg. stream's data
|
|
|
|
// offset and length, how many free or reusable blocks are avaliable, the
|
|
|
|
// predicted time for the next block...e.t.c. This function will write the
|
|
|
|
// corresponding action for each stream in `mStreams` into `aActions`.
|
|
|
|
void DetermineActionsForStreams(AutoLock& aLock, const TimeStamp& aNow,
|
|
|
|
nsTArray<StreamAction>& aActions,
|
|
|
|
int32_t aFreeBlockCount);
|
|
|
|
|
2018-01-09 05:29:10 +03:00
|
|
|
// Used by MediaCacheStream::GetDebugInfo() only for debugging.
|
|
|
|
// Don't add new callers to this function.
|
2019-05-27 19:15:33 +03:00
|
|
|
friend void MediaCacheStream::GetDebugInfo(
|
|
|
|
dom::MediaCacheStreamDebugInfo& aInfo);
|
2018-01-09 05:29:10 +03:00
|
|
|
mozilla::Monitor& GetMonitorOnTheMainThread() {
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(NS_IsMainThread());
|
|
|
|
return mMonitor;
|
|
|
|
}
|
2009-04-01 04:52:56 +04:00
|
|
|
};
|
|
|
|
|
2019-05-07 02:38:02 +03:00
|
|
|
void MediaCache::UpdateOnCellular() {
|
|
|
|
NS_ASSERTION(NS_IsMainThread(),
|
|
|
|
"Only call on main thread"); // JNI required on Android...
|
|
|
|
bool onCellular = OnCellularConnection();
|
|
|
|
LOG("MediaCache::UpdateOnCellular() onCellular=%d", onCellular);
|
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
|
|
|
|
"MediaCache::UpdateOnCellular", [=]() { sOnCellular = onCellular; });
|
|
|
|
sThread->Dispatch(r.forget());
|
|
|
|
}
|
|
|
|
|
2010-09-29 04:02:08 +04:00
|
|
|
NS_IMETHODIMP
|
2014-01-04 19:02:17 +04:00
|
|
|
MediaCacheFlusher::Observe(nsISupports* aSubject, char const* aTopic,
|
|
|
|
char16_t const* aData) {
|
2017-06-08 05:32:13 +03:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
|
2012-04-17 07:16:47 +04:00
|
|
|
if (strcmp(aTopic, "last-pb-context-exited") == 0) {
|
2017-06-08 05:32:13 +03:00
|
|
|
for (MediaCache* mc : mMediaCaches) {
|
|
|
|
mc->CloseStreamsForPrivateBrowsing();
|
|
|
|
}
|
2017-04-06 11:39:14 +03:00
|
|
|
return NS_OK;
|
2010-09-29 04:02:08 +04:00
|
|
|
}
|
2016-08-08 06:04:55 +03:00
|
|
|
if (strcmp(aTopic, "cacheservice:empty-cache") == 0) {
|
2017-06-08 05:32:13 +03:00
|
|
|
for (MediaCache* mc : mMediaCaches) {
|
|
|
|
mc->Flush();
|
|
|
|
}
|
2017-04-06 11:39:14 +03:00
|
|
|
return NS_OK;
|
2013-06-18 09:44:00 +04:00
|
|
|
}
|
2019-05-07 02:38:02 +03:00
|
|
|
if (strcmp(aTopic, "contentchild:network-link-type-changed") == 0 ||
|
|
|
|
strcmp(aTopic, NS_NETWORK_LINK_TYPE_TOPIC) == 0) {
|
|
|
|
MediaCache::UpdateOnCellular();
|
|
|
|
}
|
2010-09-29 04:02:08 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-04-06 11:20:00 +03:00
|
|
|
MediaCacheStream::MediaCacheStream(ChannelMediaResource* aClient,
|
|
|
|
bool aIsPrivateBrowsing)
|
2017-06-08 08:30:40 +03:00
|
|
|
: mMediaCache(nullptr),
|
|
|
|
mClient(aClient),
|
|
|
|
mIsTransportSeekable(false),
|
|
|
|
mCacheSuspended(false),
|
|
|
|
mChannelEnded(false),
|
|
|
|
mStreamOffset(0),
|
|
|
|
mPlaybackBytesPerSecond(10000),
|
|
|
|
mPinCount(0),
|
2018-06-15 10:25:02 +03:00
|
|
|
mNotifyDataEndedStatus(NS_ERROR_NOT_INITIALIZED),
|
2017-06-08 08:30:40 +03:00
|
|
|
mIsPrivateBrowsing(aIsPrivateBrowsing) {}
|
2018-11-19 16:25:37 +03:00
|
|
|
|
2014-03-06 01:31:04 +04:00
|
|
|
size_t MediaCacheStream::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
|
2019-07-11 15:15:24 +03:00
|
|
|
AutoLock lock(mMediaCache->Monitor());
|
|
|
|
|
2014-03-06 01:31:04 +04:00
|
|
|
// Looks like these are not owned:
|
|
|
|
// - mClient
|
2015-07-29 09:24:24 +03:00
|
|
|
size_t size = mBlocks.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
2014-03-06 01:31:04 +04:00
|
|
|
size += mReadaheadBlocks.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
size += mMetadataBlocks.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
size += mPlayedBlocks.SizeOfExcludingThis(aMallocSizeOf);
|
2015-12-03 02:07:59 +03:00
|
|
|
size += aMallocSizeOf(mPartialBlockBuffer.get());
|
2014-03-06 01:31:04 +04:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t MediaCacheStream::BlockList::SizeOfExcludingThis(
|
|
|
|
MallocSizeOf aMallocSizeOf) const {
|
2015-07-29 11:50:52 +03:00
|
|
|
return mEntries.ShallowSizeOfExcludingThis(aMallocSizeOf);
|
2014-03-06 01:31:04 +04:00
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
void MediaCacheStream::BlockList::AddFirstBlock(int32_t aBlock) {
|
2009-09-15 06:30:44 +04:00
|
|
|
NS_ASSERTION(!mEntries.GetEntry(aBlock), "Block already in list");
|
|
|
|
Entry* entry = mEntries.PutEntry(aBlock);
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
if (mFirstBlock < 0) {
|
2009-09-15 06:30:44 +04:00
|
|
|
entry->mNextBlock = entry->mPrevBlock = aBlock;
|
2009-04-01 04:52:56 +04:00
|
|
|
} else {
|
2009-09-15 06:30:44 +04: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 04:52:56 +04:00
|
|
|
}
|
|
|
|
mFirstBlock = aBlock;
|
|
|
|
++mCount;
|
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
void MediaCacheStream::BlockList::AddAfter(int32_t aBlock, int32_t aBefore) {
|
2009-09-15 06:30:44 +04: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 04:52:56 +04:00
|
|
|
++mCount;
|
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
void MediaCacheStream::BlockList::RemoveBlock(int32_t aBlock) {
|
2009-09-15 06:30:44 +04:00
|
|
|
Entry* entry = mEntries.GetEntry(aBlock);
|
2017-04-18 05:23:56 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(entry, "Block not in list");
|
2009-09-15 06:30:44 +04:00
|
|
|
|
|
|
|
if (entry->mNextBlock == aBlock) {
|
2017-04-18 05:23:56 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(entry->mPrevBlock == aBlock,
|
|
|
|
"Linked list inconsistency");
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mFirstBlock == aBlock, "Linked list inconsistency");
|
2009-04-01 04:52:56 +04:00
|
|
|
mFirstBlock = -1;
|
|
|
|
} else {
|
|
|
|
if (mFirstBlock == aBlock) {
|
2009-09-15 06:30:44 +04:00
|
|
|
mFirstBlock = entry->mNextBlock;
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
2009-09-15 06:30:44 +04:00
|
|
|
mEntries.GetEntry(entry->mNextBlock)->mPrevBlock = entry->mPrevBlock;
|
|
|
|
mEntries.GetEntry(entry->mPrevBlock)->mNextBlock = entry->mNextBlock;
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
2015-09-25 05:05:16 +03:00
|
|
|
mEntries.RemoveEntry(entry);
|
2009-04-01 04:52:56 +04:00
|
|
|
--mCount;
|
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
int32_t MediaCacheStream::BlockList::GetLastBlock() const {
|
2009-04-01 04:52:56 +04:00
|
|
|
if (mFirstBlock < 0) return -1;
|
2009-09-15 06:30:44 +04:00
|
|
|
return mEntries.GetEntry(mFirstBlock)->mPrevBlock;
|
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
int32_t MediaCacheStream::BlockList::GetNextBlock(int32_t aBlock) const {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t block = mEntries.GetEntry(aBlock)->mNextBlock;
|
2009-09-15 06:30:44 +04:00
|
|
|
if (block == mFirstBlock) return -1;
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
int32_t MediaCacheStream::BlockList::GetPrevBlock(int32_t aBlock) const {
|
2009-09-15 06:30:44 +04:00
|
|
|
if (aBlock == mFirstBlock) return -1;
|
|
|
|
return mEntries.GetEntry(aBlock)->mPrevBlock;
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2012-11-14 23:46:40 +04:00
|
|
|
void MediaCacheStream::BlockList::Verify() {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t count = 0;
|
2009-04-01 04:52:56 +04:00
|
|
|
if (mFirstBlock >= 0) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t block = mFirstBlock;
|
2009-04-01 04:52:56 +04:00
|
|
|
do {
|
2009-09-15 06:30:44 +04:00
|
|
|
Entry* entry = mEntries.GetEntry(block);
|
|
|
|
NS_ASSERTION(mEntries.GetEntry(entry->mNextBlock)->mPrevBlock == block,
|
2009-04-01 04:52:56 +04:00
|
|
|
"Bad prev link");
|
2009-09-15 06:30:44 +04:00
|
|
|
NS_ASSERTION(mEntries.GetEntry(entry->mPrevBlock)->mNextBlock == block,
|
|
|
|
"Bad next link");
|
|
|
|
block = entry->mNextBlock;
|
2009-04-01 04:52:56 +04:00
|
|
|
++count;
|
|
|
|
} while (block != mFirstBlock);
|
|
|
|
}
|
|
|
|
NS_ASSERTION(count == mCount, "Bad count");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static void UpdateSwappedBlockIndex(int32_t* aBlockIndex, int32_t aBlock1Index,
|
|
|
|
int32_t aBlock2Index) {
|
|
|
|
int32_t index = *aBlockIndex;
|
2009-04-01 04:52:56 +04:00
|
|
|
if (index == aBlock1Index) {
|
|
|
|
*aBlockIndex = aBlock2Index;
|
|
|
|
} else if (index == aBlock2Index) {
|
|
|
|
*aBlockIndex = aBlock1Index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
void MediaCacheStream::BlockList::NotifyBlockSwapped(int32_t aBlockIndex1,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aBlockIndex2) {
|
2009-09-15 06:30:44 +04:00
|
|
|
Entry* e1 = mEntries.GetEntry(aBlockIndex1);
|
|
|
|
Entry* e2 = mEntries.GetEntry(aBlockIndex2);
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t e1Prev = -1, e1Next = -1, e2Prev = -1, e2Next = -1;
|
2009-09-15 06:30:44 +04:00
|
|
|
|
|
|
|
// Fix mFirstBlock
|
2009-04-01 04:52:56 +04:00
|
|
|
UpdateSwappedBlockIndex(&mFirstBlock, aBlockIndex1, aBlockIndex2);
|
2009-09-15 06:30:44 +04: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;
|
2017-06-14 02:54:26 +03:00
|
|
|
mEntries.RemoveEntry(e1);
|
2009-10-23 06:18:54 +04:00
|
|
|
// Refresh pointer after hashtable mutation.
|
|
|
|
e2 = mEntries.GetEntry(aBlockIndex2);
|
2009-09-15 06:30:44 +04:00
|
|
|
}
|
|
|
|
if (e2) {
|
|
|
|
e2Prev = e2->mPrevBlock;
|
|
|
|
e2Next = e2->mNextBlock;
|
2017-06-14 02:54:26 +03:00
|
|
|
mEntries.RemoveEntry(e2);
|
2009-09-15 06:30:44 +04:00
|
|
|
}
|
|
|
|
// 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 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2017-11-27 10:04:21 +03:00
|
|
|
void MediaCache::FlushInternal(AutoLock& aLock) {
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t blockIndex = 0; blockIndex < mIndex.Length(); ++blockIndex) {
|
2017-11-27 10:04:21 +03:00
|
|
|
FreeBlock(aLock, blockIndex);
|
2010-10-13 18:14:08 +04:00
|
|
|
}
|
|
|
|
|
2017-06-15 07:12:30 +03:00
|
|
|
// Truncate index array.
|
2010-10-13 18:14:08 +04:00
|
|
|
Truncate();
|
|
|
|
NS_ASSERTION(mIndex.Length() == 0, "Blocks leaked?");
|
2017-09-18 05:52:17 +03:00
|
|
|
// Reset block cache to its pristine state.
|
|
|
|
mBlockCache->Flush();
|
2010-10-13 18:14:08 +04:00
|
|
|
}
|
|
|
|
|
2017-11-27 10:04:21 +03:00
|
|
|
void MediaCache::Flush() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
|
2020-01-07 19:59:11 +03:00
|
|
|
"MediaCache::Flush", [self = RefPtr<MediaCache>(this)]() mutable {
|
2017-11-27 10:04:21 +03:00
|
|
|
AutoLock lock(self->mMonitor);
|
|
|
|
self->FlushInternal(lock);
|
2020-01-07 19:59:11 +03:00
|
|
|
// Ensure MediaCache is deleted on the main thread.
|
2020-04-07 18:16:23 +03:00
|
|
|
NS_ReleaseOnMainThread("MediaCache::Flush", self.forget());
|
2017-11-27 10:04:21 +03:00
|
|
|
});
|
|
|
|
sThread->Dispatch(r.forget());
|
|
|
|
}
|
|
|
|
|
2017-04-06 11:39:14 +03:00
|
|
|
void MediaCache::CloseStreamsForPrivateBrowsing() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2018-01-05 06:34:59 +03:00
|
|
|
sThread->Dispatch(NS_NewRunnableFunction(
|
|
|
|
"MediaCache::CloseStreamsForPrivateBrowsing",
|
2020-01-07 19:59:11 +03:00
|
|
|
[self = RefPtr<MediaCache>(this)]() mutable {
|
2018-01-05 06:34:59 +03:00
|
|
|
AutoLock lock(self->mMonitor);
|
|
|
|
// Copy mStreams since CloseInternal() will change the array.
|
2020-05-05 14:38:10 +03:00
|
|
|
for (MediaCacheStream* s : self->mStreams.Clone()) {
|
2018-01-05 06:34:59 +03:00
|
|
|
if (s->mIsPrivateBrowsing) {
|
|
|
|
s->CloseInternal(lock);
|
2018-11-19 16:25:37 +03:00
|
|
|
}
|
2018-01-05 06:34:59 +03:00
|
|
|
}
|
2020-01-07 19:59:11 +03:00
|
|
|
// Ensure MediaCache is deleted on the main thread.
|
2020-04-07 18:16:23 +03:00
|
|
|
NS_ReleaseOnMainThread("MediaCache::CloseStreamsForPrivateBrowsing",
|
|
|
|
self.forget());
|
2018-01-05 06:34:59 +03:00
|
|
|
}));
|
2017-04-06 11:39:14 +03:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
2020-01-22 03:19:28 +03:00
|
|
|
RefPtr<MediaCache> MediaCache::GetMediaCache(int64_t aContentLength,
|
|
|
|
bool aIsPrivateBrowsing) {
|
2017-06-08 06:09:40 +03:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
2017-10-23 11:51:17 +03:00
|
|
|
|
|
|
|
if (!sThreadInit) {
|
|
|
|
sThreadInit = true;
|
|
|
|
nsCOMPtr<nsIThread> thread;
|
|
|
|
nsresult rv = NS_NewNamedThread("MediaCache", getter_AddRefs(thread));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Failed to create a thread for MediaCache.");
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-02-13 17:38:48 +03:00
|
|
|
sThread = ToRefPtr(std::move(thread));
|
2017-10-27 06:38:53 +03:00
|
|
|
|
|
|
|
static struct ClearThread {
|
|
|
|
// Called during shutdown to clear sThread.
|
|
|
|
void operator=(std::nullptr_t) {
|
2021-07-12 21:50:35 +03:00
|
|
|
MOZ_ASSERT(sThread, "We should only clear sThread once.");
|
|
|
|
sThread->Shutdown();
|
|
|
|
sThread = nullptr;
|
2017-10-27 06:38:53 +03:00
|
|
|
}
|
|
|
|
} sClearThread;
|
2021-02-27 17:38:14 +03:00
|
|
|
ClearOnShutdown(&sClearThread, ShutdownPhase::XPCOMShutdownThreads);
|
2017-10-23 11:51:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!sThread) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-01-22 03:19:28 +03:00
|
|
|
const int64_t mediaMemoryCacheMaxSize =
|
|
|
|
static_cast<int64_t>(StaticPrefs::media_memory_cache_max_size()) * 1024;
|
|
|
|
|
|
|
|
// Force usage of in-memory cache if we are in private browsing mode
|
|
|
|
// and the forceMediaMemoryCache pref is set
|
|
|
|
// We will not attempt to create an on-disk cache if this is the case
|
|
|
|
const bool forceMediaMemoryCache =
|
|
|
|
aIsPrivateBrowsing &&
|
|
|
|
StaticPrefs::browser_privatebrowsing_forceMediaMemoryCache();
|
|
|
|
|
|
|
|
// Alternatively, use an in-memory cache if the media will fit entirely
|
|
|
|
// in memory
|
|
|
|
// aContentLength < 0 indicates we do not know content's actual size
|
|
|
|
const bool contentFitsInMediaMemoryCache =
|
|
|
|
(aContentLength > 0) && (aContentLength <= mediaMemoryCacheMaxSize);
|
|
|
|
|
|
|
|
// Try to allocate a memory cache for our content
|
|
|
|
if (contentFitsInMediaMemoryCache || forceMediaMemoryCache) {
|
|
|
|
// Figure out how large our cache should be
|
|
|
|
int64_t cacheSize = 0;
|
|
|
|
if (contentFitsInMediaMemoryCache) {
|
|
|
|
cacheSize = aContentLength;
|
|
|
|
} else if (forceMediaMemoryCache) {
|
|
|
|
// Unknown content length, we'll give the maximum allowed cache size
|
|
|
|
// just to be sure.
|
|
|
|
if (aContentLength < 0) {
|
|
|
|
cacheSize = mediaMemoryCacheMaxSize;
|
|
|
|
} else {
|
|
|
|
// If the content length is less than the maximum allowed cache size,
|
|
|
|
// use that, otherwise we cap it to max size.
|
|
|
|
cacheSize = std::min(aContentLength, mediaMemoryCacheMaxSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<MediaBlockCacheBase> bc = new MemoryBlockCache(cacheSize);
|
2017-06-15 07:32:06 +03:00
|
|
|
nsresult rv = bc->Init();
|
2017-06-09 14:15:04 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2017-06-15 08:32:26 +03:00
|
|
|
RefPtr<MediaCache> mc = new MediaCache(bc);
|
2017-06-15 08:10:54 +03:00
|
|
|
LOG("GetMediaCache(%" PRIi64 ") -> Memory MediaCache %p", aContentLength,
|
|
|
|
mc.get());
|
2017-06-09 14:15:04 +03:00
|
|
|
return mc;
|
|
|
|
}
|
2020-01-22 03:19:28 +03:00
|
|
|
|
|
|
|
// MemoryBlockCache initialization failed.
|
|
|
|
// If we require use of a memory media cache, we will bail here.
|
|
|
|
// Otherwise use a file-backed MediaCache below.
|
|
|
|
if (forceMediaMemoryCache) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-06-09 14:15:04 +03:00
|
|
|
}
|
|
|
|
|
2017-06-08 06:09:40 +03:00
|
|
|
if (gMediaCache) {
|
2017-06-12 06:42:26 +03:00
|
|
|
LOG("GetMediaCache(%" PRIi64 ") -> Existing file-backed MediaCache",
|
|
|
|
aContentLength);
|
2017-06-08 06:09:40 +03:00
|
|
|
return gMediaCache;
|
|
|
|
}
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2017-06-15 07:32:06 +03:00
|
|
|
RefPtr<MediaBlockCacheBase> bc = new FileBlockCache();
|
|
|
|
nsresult rv = bc->Init();
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2017-06-15 08:32:26 +03:00
|
|
|
gMediaCache = new MediaCache(bc);
|
2017-06-15 07:32:06 +03:00
|
|
|
LOG("GetMediaCache(%" PRIi64 ") -> Created file-backed MediaCache",
|
2017-06-12 06:42:26 +03:00
|
|
|
aContentLength);
|
|
|
|
} else {
|
2017-06-15 07:32:06 +03:00
|
|
|
LOG("GetMediaCache(%" PRIi64 ") -> Failed to create file-backed MediaCache",
|
2017-06-12 06:42:26 +03:00
|
|
|
aContentLength);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
2017-06-08 06:09:40 +03:00
|
|
|
|
|
|
|
return gMediaCache;
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
nsresult MediaCache::ReadCacheFile(AutoLock&, int64_t aOffset, void* aData,
|
|
|
|
int32_t aLength, int32_t* aBytes) {
|
2018-01-05 05:48:14 +03:00
|
|
|
if (!mBlockCache) {
|
2009-04-01 04:52:56 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2017-04-10 06:55:26 +03:00
|
|
|
}
|
2018-01-05 05:48:14 +03:00
|
|
|
return mBlockCache->Read(aOffset, reinterpret_cast<uint8_t*>(aData), aLength,
|
|
|
|
aBytes);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2017-07-06 06:42:56 +03:00
|
|
|
// Allowed range is whatever can be accessed with an int32_t block index.
|
|
|
|
static bool IsOffsetAllowed(int64_t aOffset) {
|
|
|
|
return aOffset < (int64_t(INT32_MAX) + 1) * MediaCache::BLOCK_SIZE &&
|
|
|
|
aOffset >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert 64-bit offset to 32-bit block index.
|
|
|
|
// Assumes offset range-check was already done.
|
|
|
|
static int32_t OffsetToBlockIndexUnchecked(int64_t aOffset) {
|
|
|
|
// Still check for allowed range in debug builds, to catch out-of-range
|
|
|
|
// issues early during development.
|
|
|
|
MOZ_ASSERT(IsOffsetAllowed(aOffset));
|
|
|
|
return int32_t(aOffset / MediaCache::BLOCK_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert 64-bit offset to 32-bit block index. -1 if out of allowed range.
|
|
|
|
static int32_t OffsetToBlockIndex(int64_t aOffset) {
|
|
|
|
return IsOffsetAllowed(aOffset) ? OffsetToBlockIndexUnchecked(aOffset) : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert 64-bit offset to 32-bit offset inside a block.
|
|
|
|
// Will not fail (even if offset is outside allowed range), so there is no
|
|
|
|
// need to check for errors.
|
|
|
|
static int32_t OffsetInBlock(int64_t aOffset) {
|
|
|
|
// Still check for allowed range in debug builds, to catch out-of-range
|
|
|
|
// issues early during development.
|
|
|
|
MOZ_ASSERT(IsOffsetAllowed(aOffset));
|
|
|
|
return int32_t(aOffset % MediaCache::BLOCK_SIZE);
|
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
int32_t MediaCache::FindBlockForIncomingData(AutoLock& aLock, TimeStamp aNow,
|
2017-09-14 05:42:54 +03:00
|
|
|
MediaCacheStream* aStream,
|
|
|
|
int32_t aStreamBlockIndex) {
|
2017-11-20 11:30:05 +03:00
|
|
|
MOZ_ASSERT(sThread->IsOnCurrentThread());
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2017-07-06 06:42:56 +03:00
|
|
|
int32_t blockIndex =
|
2017-11-22 12:14:02 +03:00
|
|
|
FindReusableBlock(aLock, aNow, aStream, aStreamBlockIndex, INT32_MAX);
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2009-09-15 06:30:44 +04:00
|
|
|
if (blockIndex < 0 || !IsBlockFree(blockIndex)) {
|
2009-04-01 04:52:56 +04: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.
|
2019-05-07 02:38:02 +03:00
|
|
|
if ((mIndex.Length() <
|
|
|
|
uint32_t(mBlockCache->GetMaxBlocks(MediaCache::CacheSize())) ||
|
2017-07-10 01:23:02 +03:00
|
|
|
blockIndex < 0 ||
|
2017-11-22 12:14:02 +03:00
|
|
|
PredictNextUseForIncomingData(aLock, aStream) >=
|
|
|
|
PredictNextUse(aLock, aNow, blockIndex))) {
|
2009-04-01 04:52:56 +04:00
|
|
|
blockIndex = mIndex.Length();
|
2020-04-24 16:31:14 +03:00
|
|
|
// XXX(Bug 1631371) Check if this should use a fallible operation as it
|
|
|
|
// pretended earlier.
|
|
|
|
mIndex.AppendElement();
|
2009-04-01 04:52:56 +04:00
|
|
|
mFreeBlocks.AddFirstBlock(blockIndex);
|
|
|
|
return blockIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return blockIndex;
|
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
bool MediaCache::BlockIsReusable(AutoLock&, int32_t aBlockIndex) {
|
2009-09-15 06:30:44 +04:00
|
|
|
Block* block = &mIndex[aBlockIndex];
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < block->mOwners.Length(); ++i) {
|
2012-11-14 23:46:40 +04:00
|
|
|
MediaCacheStream* stream = block->mOwners[i].mStream;
|
2009-10-15 06:20:49 +04:00
|
|
|
if (stream->mPinCount > 0 ||
|
2017-07-06 06:42:56 +03:00
|
|
|
uint32_t(OffsetToBlockIndex(stream->mStreamOffset)) ==
|
|
|
|
block->mOwners[i].mStreamBlock) {
|
2011-09-30 03:34:37 +04:00
|
|
|
return false;
|
2009-09-15 06:30:44 +04:00
|
|
|
}
|
|
|
|
}
|
2011-09-30 03:34:37 +04:00
|
|
|
return true;
|
2009-09-15 06:30:44 +04:00
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
void MediaCache::AppendMostReusableBlock(AutoLock& aLock, BlockList* aBlockList,
|
|
|
|
nsTArray<uint32_t>* aResult,
|
|
|
|
int32_t aBlockIndexLimit) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t blockIndex = aBlockList->GetLastBlock();
|
2009-09-15 06:30:44 +04:00
|
|
|
if (blockIndex < 0) return;
|
2009-04-01 04:52:56 +04:00
|
|
|
do {
|
|
|
|
// Don't consider blocks for pinned streams, or blocks that are
|
2009-09-15 06:30:44 +04:00
|
|
|
// beyond the specified limit, or a block that contains a stream's
|
2009-04-01 04:52:56 +04:00
|
|
|
// current read position (such a block contains both played data
|
|
|
|
// and readahead data)
|
2017-11-22 12:14:02 +03:00
|
|
|
if (blockIndex < aBlockIndexLimit && BlockIsReusable(aLock, blockIndex)) {
|
2009-04-01 04:52:56 +04:00
|
|
|
aResult->AppendElement(blockIndex);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-15 06:30:44 +04:00
|
|
|
blockIndex = aBlockList->GetPrevBlock(blockIndex);
|
|
|
|
} while (blockIndex >= 0);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
int32_t MediaCache::FindReusableBlock(AutoLock& aLock, TimeStamp aNow,
|
|
|
|
MediaCacheStream* aForStream,
|
|
|
|
int32_t aForStreamBlock,
|
|
|
|
int32_t aMaxSearchBlockIndex) {
|
2017-11-20 11:30:05 +03:00
|
|
|
MOZ_ASSERT(sThread->IsOnCurrentThread());
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2014-05-09 05:03:35 +04:00
|
|
|
uint32_t length =
|
|
|
|
std::min(uint32_t(aMaxSearchBlockIndex), uint32_t(mIndex.Length()));
|
2009-04-01 04:52:56 +04:00
|
|
|
|
|
|
|
if (aForStream && aForStreamBlock > 0 &&
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t(aForStreamBlock) <= aForStream->mBlocks.Length()) {
|
|
|
|
int32_t prevCacheBlock = aForStream->mBlocks[aForStreamBlock - 1];
|
2009-04-01 04:52:56 +04:00
|
|
|
if (prevCacheBlock >= 0) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t freeBlockScanEnd =
|
2013-01-15 16:22:03 +04:00
|
|
|
std::min(length, prevCacheBlock + FREE_BLOCK_SCAN_LIMIT);
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = prevCacheBlock; i < freeBlockScanEnd; ++i) {
|
2009-09-15 06:30:44 +04:00
|
|
|
if (IsBlockFree(i)) return i;
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mFreeBlocks.IsEmpty()) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t blockIndex = mFreeBlocks.GetFirstBlock();
|
2009-04-01 04:52:56 +04:00
|
|
|
do {
|
|
|
|
if (blockIndex < aMaxSearchBlockIndex) return blockIndex;
|
2009-09-15 06:30:44 +04:00
|
|
|
blockIndex = mFreeBlocks.GetNextBlock(blockIndex);
|
|
|
|
} while (blockIndex >= 0);
|
2009-04-01 04:52:56 +04: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.
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<uint32_t, 8> candidates;
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < mStreams.Length(); ++i) {
|
2012-11-14 23:46:40 +04:00
|
|
|
MediaCacheStream* stream = mStreams[i];
|
2009-09-15 06:30:44 +04:00
|
|
|
if (stream->mPinCount > 0) {
|
|
|
|
// No point in even looking at this stream's blocks
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
AppendMostReusableBlock(aLock, &stream->mMetadataBlocks, &candidates,
|
|
|
|
length);
|
|
|
|
AppendMostReusableBlock(aLock, &stream->mPlayedBlocks, &candidates, length);
|
2009-09-15 06:30:44 +04:00
|
|
|
|
|
|
|
// 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 17:17:54 +04:00
|
|
|
if (stream->mIsTransportSeekable) {
|
2017-11-22 12:14:02 +03:00
|
|
|
AppendMostReusableBlock(aLock, &stream->mReadaheadBlocks, &candidates,
|
|
|
|
length);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-11 13:39:02 +04:00
|
|
|
TimeDuration latestUse;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t latestUseBlock = -1;
|
|
|
|
for (uint32_t i = 0; i < candidates.Length(); ++i) {
|
2017-11-22 12:14:02 +03:00
|
|
|
TimeDuration nextUse = PredictNextUse(aLock, aNow, candidates[i]);
|
2009-04-01 04:52:56 +04:00
|
|
|
if (nextUse > latestUse) {
|
|
|
|
latestUse = nextUse;
|
|
|
|
latestUseBlock = candidates[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return latestUseBlock;
|
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
MediaCache::BlockList* MediaCache::GetListForBlock(AutoLock&,
|
|
|
|
BlockOwner* aBlock) {
|
2009-04-01 04:52:56 +04:00
|
|
|
switch (aBlock->mClass) {
|
|
|
|
case METADATA_BLOCK:
|
|
|
|
NS_ASSERTION(aBlock->mStream, "Metadata block has no stream?");
|
2009-09-15 06:30:44 +04:00
|
|
|
return &aBlock->mStream->mMetadataBlocks;
|
2009-04-01 04:52:56 +04:00
|
|
|
case PLAYED_BLOCK:
|
|
|
|
NS_ASSERTION(aBlock->mStream, "Metadata block has no stream?");
|
2009-09-15 06:30:44 +04:00
|
|
|
return &aBlock->mStream->mPlayedBlocks;
|
2009-04-01 04:52:56 +04: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 18:20:58 +04:00
|
|
|
return nullptr;
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
MediaCache::BlockOwner* MediaCache::GetBlockOwner(AutoLock&,
|
|
|
|
int32_t aBlockIndex,
|
|
|
|
MediaCacheStream* aStream) {
|
2009-09-15 06:30:44 +04:00
|
|
|
Block* block = &mIndex[aBlockIndex];
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < block->mOwners.Length(); ++i) {
|
2009-09-15 06:30:44 +04:00
|
|
|
if (block->mOwners[i].mStream == aStream) return &block->mOwners[i];
|
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2009-09-15 06:30:44 +04:00
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
void MediaCache::SwapBlocks(AutoLock& aLock, int32_t aBlockIndex1,
|
|
|
|
int32_t aBlockIndex2) {
|
2009-04-01 04:52:56 +04:00
|
|
|
Block* block1 = &mIndex[aBlockIndex1];
|
|
|
|
Block* block2 = &mIndex[aBlockIndex2];
|
|
|
|
|
2009-09-15 06:30:44 +04:00
|
|
|
block1->mOwners.SwapElements(block2->mOwners);
|
2009-04-01 04:52:56 +04:00
|
|
|
|
|
|
|
// Now all references to block1 have to be replaced with block2 and
|
2009-09-15 06:30:44 +04:00
|
|
|
// vice versa.
|
|
|
|
// First update stream references to blocks via mBlocks.
|
|
|
|
const Block* blocks[] = {block1, block2};
|
2012-08-22 19:56:38 +04: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 06:30:44 +04:00
|
|
|
const BlockOwner* b = &blocks[i]->mOwners[j];
|
|
|
|
b->mStream->mBlocks[b->mStreamBlock] = blockIndices[i];
|
|
|
|
}
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2009-09-15 06:30:44 +04:00
|
|
|
// Now update references to blocks in block lists.
|
|
|
|
mFreeBlocks.NotifyBlockSwapped(aBlockIndex1, aBlockIndex2);
|
|
|
|
|
2021-03-23 13:36:38 +03:00
|
|
|
nsTHashSet<MediaCacheStream*> visitedStreams;
|
2009-09-15 06:30:44 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = 0; i < 2; ++i) {
|
|
|
|
for (uint32_t j = 0; j < blocks[i]->mOwners.Length(); ++j) {
|
2012-11-14 23:46:40 +04:00
|
|
|
MediaCacheStream* stream = blocks[i]->mOwners[j].mStream;
|
2009-09-15 06:30:44 +04:00
|
|
|
// Make sure that we don't update the same stream twice --- that
|
|
|
|
// would result in swapping the block references back again!
|
2021-03-23 13:36:38 +03:00
|
|
|
if (!visitedStreams.EnsureInserted(stream)) continue;
|
2009-09-15 06:30:44 +04:00
|
|
|
stream->mReadaheadBlocks.NotifyBlockSwapped(aBlockIndex1, aBlockIndex2);
|
|
|
|
stream->mPlayedBlocks.NotifyBlockSwapped(aBlockIndex1, aBlockIndex2);
|
|
|
|
stream->mMetadataBlocks.NotifyBlockSwapped(aBlockIndex1, aBlockIndex2);
|
|
|
|
}
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
Verify(aLock);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
void MediaCache::RemoveBlockOwner(AutoLock& aLock, int32_t aBlockIndex,
|
|
|
|
MediaCacheStream* aStream) {
|
2009-09-15 06:30:44 +04:00
|
|
|
Block* block = &mIndex[aBlockIndex];
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < block->mOwners.Length(); ++i) {
|
2009-09-15 06:30:44 +04:00
|
|
|
BlockOwner* bo = &block->mOwners[i];
|
|
|
|
if (bo->mStream == aStream) {
|
2017-11-22 12:14:02 +03:00
|
|
|
GetListForBlock(aLock, bo)->RemoveBlock(aBlockIndex);
|
2009-09-15 06:30:44 +04:00
|
|
|
bo->mStream->mBlocks[bo->mStreamBlock] = -1;
|
|
|
|
block->mOwners.RemoveElementAt(i);
|
|
|
|
if (block->mOwners.IsEmpty()) {
|
|
|
|
mFreeBlocks.AddFirstBlock(aBlockIndex);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
void MediaCache::AddBlockOwnerAsReadahead(AutoLock& aLock, int32_t aBlockIndex,
|
|
|
|
MediaCacheStream* aStream,
|
|
|
|
int32_t aStreamBlockIndex) {
|
2009-09-15 06:30:44 +04: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;
|
2017-11-22 12:14:02 +03:00
|
|
|
InsertReadaheadBlock(aLock, bo, aBlockIndex);
|
2009-09-15 06:30:44 +04:00
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
void MediaCache::FreeBlock(AutoLock& aLock, int32_t aBlock) {
|
2009-04-01 04:52:56 +04:00
|
|
|
Block* block = &mIndex[aBlock];
|
2009-09-15 06:30:44 +04:00
|
|
|
if (block->mOwners.IsEmpty()) {
|
|
|
|
// already free
|
|
|
|
return;
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
2009-09-15 06:30:44 +04:00
|
|
|
|
2017-04-12 09:51:34 +03:00
|
|
|
LOG("Released block %d", aBlock);
|
2009-09-15 06:30:44 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < block->mOwners.Length(); ++i) {
|
2009-09-15 06:30:44 +04:00
|
|
|
BlockOwner* bo = &block->mOwners[i];
|
2017-11-22 12:14:02 +03:00
|
|
|
GetListForBlock(aLock, bo)->RemoveBlock(aBlock);
|
2009-09-15 06:30:44 +04:00
|
|
|
bo->mStream->mBlocks[bo->mStreamBlock] = -1;
|
|
|
|
}
|
|
|
|
block->mOwners.Clear();
|
2009-04-01 04:52:56 +04:00
|
|
|
mFreeBlocks.AddFirstBlock(aBlock);
|
2017-11-22 12:14:02 +03:00
|
|
|
Verify(aLock);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
TimeDuration MediaCache::PredictNextUse(AutoLock&, TimeStamp aNow,
|
|
|
|
int32_t aBlock) {
|
2017-11-20 11:30:05 +03:00
|
|
|
MOZ_ASSERT(sThread->IsOnCurrentThread());
|
2009-09-15 06:30:44 +04:00
|
|
|
NS_ASSERTION(!IsBlockFree(aBlock), "aBlock is free");
|
2009-04-01 04:52:56 +04:00
|
|
|
|
|
|
|
Block* block = &mIndex[aBlock];
|
2009-09-15 06:30:44 +04: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 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < block->mOwners.Length(); ++i) {
|
2009-09-15 06:30:44 +04: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 17:52:57 +04:00
|
|
|
case PLAYED_BLOCK: {
|
2009-09-15 06:30:44 +04: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 19:56:38 +04:00
|
|
|
NS_ASSERTION(static_cast<int64_t>(bo->mStreamBlock) * BLOCK_SIZE <
|
2009-09-15 06:30:44 +04:00
|
|
|
bo->mStream->mStreamOffset,
|
|
|
|
"Played block after the current stream position?");
|
2013-06-04 17:52:57 +04: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(std::min<int64_t>(
|
2013-06-05 06:44:20 +04:00
|
|
|
millisecondsBehind * REPLAY_PENALTY_FACTOR, INT32_MAX));
|
2009-09-15 06:30:44 +04:00
|
|
|
break;
|
2013-06-04 17:52:57 +04:00
|
|
|
}
|
2009-09-15 06:30:44 +04:00
|
|
|
case READAHEAD_BLOCK: {
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t bytesAhead =
|
|
|
|
static_cast<int64_t>(bo->mStreamBlock) * BLOCK_SIZE -
|
|
|
|
bo->mStream->mStreamOffset;
|
2009-09-15 06:30:44 +04:00
|
|
|
NS_ASSERTION(bytesAhead >= 0,
|
|
|
|
"Readahead block before the current stream position?");
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t millisecondsAhead =
|
2009-09-15 06:30:44 +04:00
|
|
|
bytesAhead * 1000 / bo->mStream->mPlaybackBytesPerSecond;
|
|
|
|
prediction = TimeDuration::FromMilliseconds(
|
2013-01-15 16:22:03 +04:00
|
|
|
std::min<int64_t>(millisecondsAhead, INT32_MAX));
|
2009-09-15 06:30:44 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
NS_ERROR("Invalid class for predicting next use");
|
|
|
|
return TimeDuration(0);
|
|
|
|
}
|
|
|
|
if (i == 0 || prediction < result) {
|
|
|
|
result = prediction;
|
|
|
|
}
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
2009-09-15 06:30:44 +04:00
|
|
|
return result;
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
TimeDuration MediaCache::PredictNextUseForIncomingData(
|
|
|
|
AutoLock&, MediaCacheStream* aStream) {
|
2017-11-20 11:30:05 +03:00
|
|
|
MOZ_ASSERT(sThread->IsOnCurrentThread());
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t bytesAhead = aStream->mChannelOffset - aStream->mStreamOffset;
|
2009-04-01 04:52:56 +04:00
|
|
|
if (bytesAhead <= -BLOCK_SIZE) {
|
|
|
|
// Hmm, no idea when data behind us will be used. Guess 24 hours.
|
2009-04-11 13:39:02 +04:00
|
|
|
return TimeDuration::FromSeconds(24 * 60 * 60);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
2009-04-11 13:39:02 +04:00
|
|
|
if (bytesAhead <= 0) return TimeDuration(0);
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t millisecondsAhead =
|
|
|
|
bytesAhead * 1000 / aStream->mPlaybackBytesPerSecond;
|
2009-04-11 13:39:02 +04:00
|
|
|
return TimeDuration::FromMilliseconds(
|
2013-01-15 16:22:03 +04:00
|
|
|
std::min<int64_t>(millisecondsAhead, INT32_MAX));
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
void MediaCache::Update() {
|
2017-10-23 12:12:07 +03:00
|
|
|
MOZ_ASSERT(sThread->IsOnCurrentThread());
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
AutoLock lock(mMonitor);
|
2017-10-24 04:04:06 +03:00
|
|
|
|
2021-04-21 00:35:31 +03:00
|
|
|
mUpdateQueued = false;
|
|
|
|
#ifdef DEBUG
|
|
|
|
mInUpdate = true;
|
|
|
|
#endif
|
|
|
|
const TimeStamp now = TimeStamp::Now();
|
|
|
|
const int32_t freeBlockCount = TrimCacheIfNeeded(lock, now);
|
2017-09-20 11:12:31 +03:00
|
|
|
|
2009-10-09 15:46:23 +04: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.
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<StreamAction, 10> actions;
|
2021-04-21 00:35:31 +03:00
|
|
|
DetermineActionsForStreams(lock, now, actions, freeBlockCount);
|
2009-10-09 15:46:23 +04:00
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
#ifdef DEBUG
|
2021-04-21 00:35:31 +03:00
|
|
|
mInUpdate = false;
|
2009-04-01 04:52:56 +04:00
|
|
|
#endif
|
2021-04-21 00:35:31 +03: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.
|
|
|
|
for (uint32_t i = 0; i < mStreams.Length(); ++i) {
|
|
|
|
MediaCacheStream* stream = mStreams[i];
|
|
|
|
switch (actions[i].mTag) {
|
|
|
|
case StreamAction::SEEK:
|
|
|
|
stream->mCacheSuspended = false;
|
|
|
|
stream->mChannelEnded = false;
|
|
|
|
break;
|
|
|
|
case StreamAction::RESUME:
|
|
|
|
stream->mCacheSuspended = false;
|
|
|
|
break;
|
|
|
|
case StreamAction::SUSPEND:
|
|
|
|
stream->mCacheSuspended = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mStreams.Length(); ++i) {
|
|
|
|
MediaCacheStream* stream = mStreams[i];
|
|
|
|
switch (actions[i].mTag) {
|
|
|
|
case StreamAction::SEEK:
|
|
|
|
LOG("Stream %p CacheSeek to %" PRId64 " (resume=%d)", stream,
|
|
|
|
actions[i].mSeekTarget, actions[i].mResume);
|
|
|
|
stream->mClient->CacheClientSeek(actions[i].mSeekTarget,
|
|
|
|
actions[i].mResume);
|
|
|
|
break;
|
|
|
|
case StreamAction::RESUME:
|
|
|
|
LOG("Stream %p Resumed", stream);
|
|
|
|
stream->mClient->CacheClientResume();
|
|
|
|
QueueSuspendedStatusUpdate(lock, stream->mResourceID);
|
|
|
|
break;
|
|
|
|
case StreamAction::SUSPEND:
|
|
|
|
LOG("Stream %p Suspended", stream);
|
|
|
|
stream->mClient->CacheClientSuspend();
|
|
|
|
QueueSuspendedStatusUpdate(lock, stream->mResourceID);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify streams about the suspended status changes.
|
|
|
|
for (uint32_t i = 0; i < mSuspendedStatusToNotify.Length(); ++i) {
|
|
|
|
MediaCache::ResourceStreamIterator iter(this, mSuspendedStatusToNotify[i]);
|
|
|
|
while (MediaCacheStream* stream = iter.Next(lock)) {
|
|
|
|
stream->mClient->CacheClientNotifySuspendedStatusChanged(
|
|
|
|
stream->AreAllStreamsForResourceSuspended(lock));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mSuspendedStatusToNotify.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t MediaCache::TrimCacheIfNeeded(AutoLock& aLock, const TimeStamp& aNow) {
|
|
|
|
MOZ_ASSERT(sThread->IsOnCurrentThread());
|
|
|
|
|
|
|
|
const int32_t maxBlocks = mBlockCache->GetMaxBlocks(MediaCache::CacheSize());
|
|
|
|
|
|
|
|
int32_t freeBlockCount = mFreeBlocks.GetCount();
|
|
|
|
TimeDuration latestPredictedUseForOverflow = 0;
|
|
|
|
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(aLock, aNow, blockIndex);
|
|
|
|
latestPredictedUseForOverflow =
|
|
|
|
std::max(latestPredictedUseForOverflow, predictedUse);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
freeBlockCount += maxBlocks - mIndex.Length();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now try to move overflowing blocks to the main part of the cache.
|
|
|
|
for (int32_t blockIndex = mIndex.Length() - 1; blockIndex >= maxBlocks;
|
|
|
|
--blockIndex) {
|
|
|
|
if (IsBlockFree(blockIndex)) continue;
|
|
|
|
|
|
|
|
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.
|
|
|
|
int32_t destinationBlockIndex =
|
|
|
|
FindReusableBlock(aLock, aNow, 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't evict |destinationBlockIndex| if it is within [cur, end) otherwise
|
|
|
|
// a new channel will be opened to download this block again which is bad.
|
|
|
|
bool inCurrentCachedRange = false;
|
|
|
|
for (BlockOwner& owner : mIndex[destinationBlockIndex].mOwners) {
|
|
|
|
MediaCacheStream* stream = owner.mStream;
|
|
|
|
int64_t end = OffsetToBlockIndexUnchecked(
|
|
|
|
stream->GetCachedDataEndInternal(aLock, stream->mStreamOffset));
|
|
|
|
int64_t cur = OffsetToBlockIndexUnchecked(stream->mStreamOffset);
|
|
|
|
if (cur <= owner.mStreamBlock && owner.mStreamBlock < end) {
|
|
|
|
inCurrentCachedRange = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (inCurrentCachedRange) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsBlockFree(destinationBlockIndex) ||
|
|
|
|
PredictNextUse(aLock, aNow, destinationBlockIndex) >
|
|
|
|
latestPredictedUseForOverflow) {
|
|
|
|
// Reuse blocks in the main part of the cache that are less useful than
|
|
|
|
// the least useful overflow blocks
|
|
|
|
|
|
|
|
nsresult rv = mBlockCache->MoveBlock(blockIndex, destinationBlockIndex);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// We successfully copied the file data.
|
|
|
|
LOG("Swapping blocks %d and %d (trimming cache)", blockIndex,
|
|
|
|
destinationBlockIndex);
|
|
|
|
// Swapping the block metadata here lets us maintain the
|
|
|
|
// correct positions in the linked lists
|
|
|
|
SwapBlocks(aLock, blockIndex, destinationBlockIndex);
|
|
|
|
// Free the overflowing block even if the copy failed.
|
|
|
|
LOG("Released block %d (trimming cache)", blockIndex);
|
|
|
|
FreeBlock(aLock, blockIndex);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG("Could not trim cache block %d (destination %d, "
|
|
|
|
"predicted next use %f, latest predicted use for overflow %f",
|
|
|
|
blockIndex, destinationBlockIndex,
|
|
|
|
PredictNextUse(aLock, aNow, destinationBlockIndex).ToSeconds(),
|
|
|
|
latestPredictedUseForOverflow.ToSeconds());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Try chopping back the array of cache entries and the cache file.
|
|
|
|
Truncate();
|
|
|
|
return freeBlockCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MediaCache::DetermineActionsForStreams(AutoLock& aLock,
|
|
|
|
const TimeStamp& aNow,
|
|
|
|
nsTArray<StreamAction>& aActions,
|
|
|
|
int32_t aFreeBlockCount) {
|
|
|
|
MOZ_ASSERT(sThread->IsOnCurrentThread());
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2017-10-24 04:04:06 +03: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)
|
|
|
|
int32_t nonSeekableReadaheadBlockCount = 0;
|
|
|
|
for (uint32_t i = 0; i < mStreams.Length(); ++i) {
|
|
|
|
MediaCacheStream* stream = mStreams[i];
|
|
|
|
if (!stream->mIsTransportSeekable) {
|
|
|
|
nonSeekableReadaheadBlockCount += stream->mReadaheadBlocks.GetCount();
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
2017-10-24 04:04:06 +03:00
|
|
|
}
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2017-10-24 04:04:06 +03:00
|
|
|
// If freeBlockCount is zero, then compute the latest of
|
|
|
|
// the predicted next-uses for all blocks
|
|
|
|
TimeDuration latestNextUse;
|
2021-04-21 00:35:30 +03:00
|
|
|
const int32_t maxBlocks = mBlockCache->GetMaxBlocks(MediaCache::CacheSize());
|
2021-04-21 00:35:31 +03:00
|
|
|
if (aFreeBlockCount == 0) {
|
2021-04-21 00:35:31 +03:00
|
|
|
const int32_t reusableBlock =
|
2021-04-21 00:35:31 +03:00
|
|
|
FindReusableBlock(aLock, aNow, nullptr, 0, maxBlocks);
|
2017-10-24 04:04:06 +03:00
|
|
|
if (reusableBlock >= 0) {
|
2021-04-21 00:35:31 +03:00
|
|
|
latestNextUse = PredictNextUse(aLock, aNow, reusableBlock);
|
2017-10-24 04:04:06 +03:00
|
|
|
}
|
|
|
|
}
|
2014-10-21 01:32:11 +04:00
|
|
|
|
2017-10-24 04:04:06 +03:00
|
|
|
for (uint32_t i = 0; i < mStreams.Length(); ++i) {
|
2021-04-21 00:35:31 +03:00
|
|
|
aActions.AppendElement(StreamAction{});
|
2009-10-09 15:46:23 +04:00
|
|
|
|
2017-10-24 04:04:06 +03:00
|
|
|
MediaCacheStream* stream = mStreams[i];
|
|
|
|
if (stream->mClosed) {
|
|
|
|
LOG("Stream %p closed", stream);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-10-25 04:37:58 +03:00
|
|
|
// We make decisions based on mSeekTarget when there is a pending seek.
|
|
|
|
// Otherwise we will keep issuing seek requests until mChannelOffset
|
|
|
|
// is changed by NotifyDataStarted() which is bad.
|
2021-04-21 00:35:31 +03:00
|
|
|
const int64_t channelOffset = stream->mSeekTarget != -1
|
|
|
|
? stream->mSeekTarget
|
|
|
|
: stream->mChannelOffset;
|
2017-10-25 04:37:58 +03:00
|
|
|
|
2017-10-24 04:04:06 +03:00
|
|
|
// Figure out where we should be reading from. It's the first
|
|
|
|
// uncached byte after the current mStreamOffset.
|
2021-04-21 00:35:31 +03:00
|
|
|
const int64_t dataOffset =
|
2021-04-21 00:35:31 +03:00
|
|
|
stream->GetCachedDataEndInternal(aLock, stream->mStreamOffset);
|
2017-10-24 04:04:06 +03:00
|
|
|
MOZ_ASSERT(dataOffset >= 0);
|
|
|
|
|
|
|
|
// Compute where we'd actually seek to to read at readOffset
|
|
|
|
int64_t desiredOffset = dataOffset;
|
|
|
|
if (stream->mIsTransportSeekable) {
|
2017-10-25 04:37:58 +03:00
|
|
|
if (desiredOffset > channelOffset &&
|
|
|
|
desiredOffset <= channelOffset + SEEK_VS_READ_THRESHOLD) {
|
2017-10-24 04:04:06 +03:00
|
|
|
// Assume it's more efficient to just keep reading up to the
|
|
|
|
// desired position instead of trying to seek
|
2017-10-25 04:37:58 +03:00
|
|
|
desiredOffset = channelOffset;
|
2017-10-24 04:04:06 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We can't seek directly to the desired offset...
|
2017-10-25 04:37:58 +03:00
|
|
|
if (channelOffset > desiredOffset) {
|
2017-10-24 04:04:06 +03:00
|
|
|
// 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...
|
2021-04-21 00:35:31 +03:00
|
|
|
ReleaseStreamBlocks(aLock, stream);
|
2009-04-01 04:52:56 +04:00
|
|
|
} else {
|
2017-10-24 04:04:06 +03:00
|
|
|
// otherwise reading forward is looking good, so just stay where we
|
|
|
|
// are and don't trigger a channel seek!
|
2017-10-25 04:37:58 +03:00
|
|
|
desiredOffset = channelOffset;
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
2017-10-24 04:04:06 +03:00
|
|
|
}
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2017-10-24 04:04:06 +03: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.
|
|
|
|
bool enableReading;
|
|
|
|
if (stream->mStreamLength >= 0 && dataOffset >= stream->mStreamLength) {
|
|
|
|
// We want data at the end of the stream, where there's nothing to
|
|
|
|
// 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.
|
|
|
|
LOG("Stream %p at end of stream", stream);
|
2017-10-25 04:37:58 +03:00
|
|
|
enableReading =
|
|
|
|
!stream->mCacheSuspended && stream->mStreamLength == channelOffset;
|
2017-10-24 04:04:06 +03: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.
|
|
|
|
LOG("Stream %p catching up", stream);
|
|
|
|
enableReading = true;
|
|
|
|
} else if (desiredOffset < stream->mStreamOffset + BLOCK_SIZE) {
|
|
|
|
// The stream reader is waiting for us, or nearly so. Better feed it.
|
|
|
|
LOG("Stream %p feeding reader", stream);
|
|
|
|
enableReading = true;
|
|
|
|
} else if (!stream->mIsTransportSeekable &&
|
|
|
|
nonSeekableReadaheadBlockCount >=
|
|
|
|
maxBlocks * NONSEEKABLE_READAHEAD_MAX) {
|
|
|
|
// This stream is not seekable and there are already too many blocks
|
|
|
|
// being cached for readahead for nonseekable streams (which we can't
|
|
|
|
// free). So stop reading ahead now.
|
|
|
|
LOG("Stream %p throttling non-seekable readahead", stream);
|
|
|
|
enableReading = false;
|
|
|
|
} else if (mIndex.Length() > uint32_t(maxBlocks)) {
|
|
|
|
// We're in the process of bringing the cache size back to the
|
|
|
|
// desired limit, so don't bring in more data yet
|
|
|
|
LOG("Stream %p throttling to reduce cache size", stream);
|
|
|
|
enableReading = false;
|
|
|
|
} else {
|
2017-11-22 12:14:02 +03:00
|
|
|
TimeDuration predictedNewDataUse =
|
2021-04-21 00:35:31 +03:00
|
|
|
PredictNextUseForIncomingData(aLock, stream);
|
2017-10-24 04:04:06 +03:00
|
|
|
|
|
|
|
if (stream->mThrottleReadahead && stream->mCacheSuspended &&
|
2021-04-21 00:35:31 +03:00
|
|
|
predictedNewDataUse.ToSeconds() > MediaCache::ResumeThreshold()) {
|
2017-10-24 04:04:06 +03:00
|
|
|
// Don't need data for a while, so don't bother waking up the stream
|
|
|
|
LOG("Stream %p avoiding wakeup since more data is not needed", stream);
|
|
|
|
enableReading = false;
|
|
|
|
} else if (stream->mThrottleReadahead &&
|
2021-04-21 00:35:31 +03:00
|
|
|
predictedNewDataUse.ToSeconds() >
|
|
|
|
MediaCache::ReadaheadLimit()) {
|
2017-10-24 04:04:06 +03:00
|
|
|
// Don't read ahead more than this much
|
|
|
|
LOG("Stream %p throttling to avoid reading ahead too far", stream);
|
2011-09-30 03:34:37 +04:00
|
|
|
enableReading = false;
|
2021-04-21 00:35:31 +03:00
|
|
|
} else if (aFreeBlockCount > 0) {
|
2017-10-24 04:04:06 +03:00
|
|
|
// Free blocks in the cache, so keep reading
|
|
|
|
LOG("Stream %p reading since there are free blocks", stream);
|
|
|
|
enableReading = true;
|
|
|
|
} else if (latestNextUse <= TimeDuration(0)) {
|
|
|
|
// No reusable blocks, so can't read anything
|
|
|
|
LOG("Stream %p throttling due to no reusable blocks", stream);
|
2011-09-30 03:34:37 +04:00
|
|
|
enableReading = false;
|
2009-10-09 15:46:23 +04:00
|
|
|
} else {
|
2017-10-24 04:04:06 +03:00
|
|
|
// Read ahead if the data we expect to read is more valuable than
|
|
|
|
// the least valuable block in the main part of the cache
|
|
|
|
LOG("Stream %p predict next data in %f, current worst block is %f",
|
|
|
|
stream, predictedNewDataUse.ToSeconds(), latestNextUse.ToSeconds());
|
|
|
|
enableReading = predictedNewDataUse < latestNextUse;
|
2009-10-09 15:46:23 +04:00
|
|
|
}
|
2017-10-24 04:04:06 +03:00
|
|
|
}
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2017-10-24 04:04:06 +03:00
|
|
|
if (enableReading) {
|
|
|
|
for (uint32_t j = 0; j < i; ++j) {
|
|
|
|
MediaCacheStream* other = mStreams[j];
|
|
|
|
if (other->mResourceID == stream->mResourceID && !other->mClosed &&
|
2017-11-15 11:45:57 +03:00
|
|
|
!other->mClientSuspended && !other->mChannelEnded &&
|
2017-10-25 04:37:58 +03:00
|
|
|
OffsetToBlockIndexUnchecked(other->mSeekTarget != -1
|
|
|
|
? other->mSeekTarget
|
|
|
|
: other->mChannelOffset) ==
|
2017-10-24 04:04:06 +03:00
|
|
|
OffsetToBlockIndexUnchecked(desiredOffset)) {
|
|
|
|
// This block is already going to be read by the other stream.
|
|
|
|
// So don't try to read it from this stream as well.
|
|
|
|
enableReading = false;
|
|
|
|
LOG("Stream %p waiting on same block (%" PRId32 ") from stream %p",
|
|
|
|
stream, OffsetToBlockIndexUnchecked(desiredOffset), other);
|
|
|
|
break;
|
2009-09-15 06:30:45 +04:00
|
|
|
}
|
|
|
|
}
|
2017-10-24 04:04:06 +03:00
|
|
|
}
|
2009-10-09 15:46:23 +04:00
|
|
|
|
2017-10-25 04:37:58 +03:00
|
|
|
if (channelOffset != desiredOffset && enableReading) {
|
2017-10-24 04:04:06 +03:00
|
|
|
// We need to seek now.
|
|
|
|
NS_ASSERTION(stream->mIsTransportSeekable || desiredOffset == 0,
|
|
|
|
"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.
|
2017-10-25 04:37:58 +03:00
|
|
|
stream->mSeekTarget =
|
2017-10-24 04:04:06 +03:00
|
|
|
OffsetToBlockIndexUnchecked(desiredOffset) * BLOCK_SIZE;
|
2021-04-21 00:35:31 +03:00
|
|
|
aActions[i].mTag = StreamAction::SEEK;
|
|
|
|
aActions[i].mResume = stream->mCacheSuspended;
|
|
|
|
aActions[i].mSeekTarget = stream->mSeekTarget;
|
2017-10-24 04:04:06 +03:00
|
|
|
} else if (enableReading && stream->mCacheSuspended) {
|
2021-04-21 00:35:31 +03:00
|
|
|
aActions[i].mTag = StreamAction::RESUME;
|
2017-10-24 04:04:06 +03:00
|
|
|
} else if (!enableReading && !stream->mCacheSuspended) {
|
2021-04-21 00:35:31 +03:00
|
|
|
aActions[i].mTag = StreamAction::SUSPEND;
|
2009-09-15 06:30:45 +04:00
|
|
|
}
|
2020-10-04 10:19:18 +03:00
|
|
|
LOG("Stream %p, mCacheSuspended=%d, enableReading=%d, action=%s", stream,
|
|
|
|
stream->mCacheSuspended, enableReading,
|
2021-04-21 00:35:31 +03:00
|
|
|
aActions[i].mTag == StreamAction::SEEK ? "SEEK"
|
|
|
|
: aActions[i].mTag == StreamAction::RESUME ? "RESUME"
|
|
|
|
: aActions[i].mTag == StreamAction::SUSPEND ? "SUSPEND"
|
|
|
|
: "NONE");
|
2021-04-21 00:35:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
void MediaCache::QueueUpdate(AutoLock&) {
|
2009-04-01 04:52:56 +04: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");
|
2021-04-21 00:35:32 +03:00
|
|
|
if (mUpdateQueued) {
|
|
|
|
return;
|
|
|
|
}
|
2011-09-30 03:34:37 +04:00
|
|
|
mUpdateQueued = true;
|
2021-04-21 00:35:32 +03:00
|
|
|
sThread->Dispatch(NS_NewRunnableFunction(
|
|
|
|
"MediaCache::QueueUpdate", [self = RefPtr<MediaCache>(this)]() mutable {
|
|
|
|
self->Update();
|
|
|
|
// Ensure MediaCache is deleted on the main thread.
|
|
|
|
NS_ReleaseOnMainThread("UpdateEvent::mMediaCache", self.forget());
|
|
|
|
}));
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
void MediaCache::QueueSuspendedStatusUpdate(AutoLock&, int64_t aResourceID) {
|
2014-12-19 01:48:00 +03:00
|
|
|
if (!mSuspendedStatusToNotify.Contains(aResourceID)) {
|
|
|
|
mSuspendedStatusToNotify.AppendElement(aResourceID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-12 05:43:23 +04:00
|
|
|
#ifdef DEBUG_VERIFY_CACHE
|
2017-11-22 12:14:02 +03:00
|
|
|
void MediaCache::Verify(AutoLock&) {
|
2009-04-01 04:52:56 +04:00
|
|
|
mFreeBlocks.Verify();
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < mStreams.Length(); ++i) {
|
2012-11-14 23:46:40 +04:00
|
|
|
MediaCacheStream* stream = mStreams[i];
|
2009-04-01 04:52:56 +04:00
|
|
|
stream->mReadaheadBlocks.Verify();
|
2009-09-15 06:30:44 +04:00
|
|
|
stream->mPlayedBlocks.Verify();
|
|
|
|
stream->mMetadataBlocks.Verify();
|
|
|
|
|
|
|
|
// Verify that the readahead blocks are listed in stream block order
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t block = stream->mReadaheadBlocks.GetFirstBlock();
|
|
|
|
int32_t lastStreamBlock = -1;
|
2009-09-15 06:30:44 +04:00
|
|
|
while (block >= 0) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t j = 0;
|
2009-09-15 06:30:44 +04:00
|
|
|
while (mIndex[block].mOwners[j].mStream != stream) {
|
|
|
|
++j;
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t nextStreamBlock = int32_t(mIndex[block].mOwners[j].mStreamBlock);
|
2009-09-15 06:30:44 +04:00
|
|
|
NS_ASSERTION(lastStreamBlock < nextStreamBlock,
|
|
|
|
"Blocks not increasing in readahead stream");
|
|
|
|
lastStreamBlock = nextStreamBlock;
|
|
|
|
block = stream->mReadaheadBlocks.GetNextBlock(block);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
void MediaCache::InsertReadaheadBlock(AutoLock& aLock, BlockOwner* aBlockOwner,
|
|
|
|
int32_t aBlockIndex) {
|
2009-04-01 04:52:56 +04:00
|
|
|
// Find the last block whose stream block is before aBlockIndex's
|
|
|
|
// stream block, and insert after it
|
2012-11-14 23:46:40 +04:00
|
|
|
MediaCacheStream* stream = aBlockOwner->mStream;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t readaheadIndex = stream->mReadaheadBlocks.GetLastBlock();
|
2009-09-15 06:30:44 +04:00
|
|
|
while (readaheadIndex >= 0) {
|
2017-11-22 12:14:02 +03:00
|
|
|
BlockOwner* bo = GetBlockOwner(aLock, readaheadIndex, stream);
|
2009-09-15 06:30:44 +04:00
|
|
|
NS_ASSERTION(bo, "stream must own its blocks");
|
|
|
|
if (bo->mStreamBlock < aBlockOwner->mStreamBlock) {
|
2009-04-01 04:52:56 +04:00
|
|
|
stream->mReadaheadBlocks.AddAfter(aBlockIndex, readaheadIndex);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-15 06:30:44 +04:00
|
|
|
NS_ASSERTION(bo->mStreamBlock > aBlockOwner->mStreamBlock,
|
2009-04-01 04:52:56 +04:00
|
|
|
"Duplicated blocks??");
|
2009-09-15 06:30:44 +04:00
|
|
|
readaheadIndex = stream->mReadaheadBlocks.GetPrevBlock(readaheadIndex);
|
2009-09-15 06:30:44 +04:00
|
|
|
}
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
stream->mReadaheadBlocks.AddFirstBlock(aBlockIndex);
|
2017-11-22 12:14:02 +03:00
|
|
|
Verify(aLock);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
void MediaCache::AllocateAndWriteBlock(AutoLock& aLock,
|
|
|
|
MediaCacheStream* aStream,
|
2017-09-14 05:39:06 +03:00
|
|
|
int32_t aStreamBlockIndex,
|
|
|
|
Span<const uint8_t> aData1,
|
|
|
|
Span<const uint8_t> aData2) {
|
2017-11-20 11:30:05 +03:00
|
|
|
MOZ_ASSERT(sThread->IsOnCurrentThread());
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2009-09-15 06:30:45 +04:00
|
|
|
// Remove all cached copies of this block
|
2017-06-08 06:32:13 +03:00
|
|
|
ResourceStreamIterator iter(this, aStream->mResourceID);
|
2017-12-14 11:22:53 +03:00
|
|
|
while (MediaCacheStream* stream = iter.Next(aLock)) {
|
2017-09-14 05:39:06 +03:00
|
|
|
while (aStreamBlockIndex >= int32_t(stream->mBlocks.Length())) {
|
2009-09-15 06:30:45 +04:00
|
|
|
stream->mBlocks.AppendElement(-1);
|
|
|
|
}
|
2017-09-14 05:39:06 +03:00
|
|
|
if (stream->mBlocks[aStreamBlockIndex] >= 0) {
|
2009-09-15 06:30:45 +04:00
|
|
|
// We no longer want to own this block
|
2017-09-14 05:39:06 +03:00
|
|
|
int32_t globalBlockIndex = stream->mBlocks[aStreamBlockIndex];
|
2017-04-12 09:51:34 +03:00
|
|
|
LOG("Released block %d from stream %p block %d(%" PRId64 ")",
|
2017-09-14 05:39:06 +03:00
|
|
|
globalBlockIndex, stream, aStreamBlockIndex,
|
|
|
|
aStreamBlockIndex * BLOCK_SIZE);
|
2017-11-22 12:14:02 +03:00
|
|
|
RemoveBlockOwner(aLock, globalBlockIndex, stream);
|
2009-09-15 06:30:45 +04:00
|
|
|
}
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2009-09-15 06:30:45 +04:00
|
|
|
// Extend the mBlocks array as necessary
|
|
|
|
|
2009-04-11 13:39:02 +04:00
|
|
|
TimeStamp now = TimeStamp::Now();
|
2017-09-14 05:42:54 +03:00
|
|
|
int32_t blockIndex =
|
2017-11-22 12:14:02 +03:00
|
|
|
FindBlockForIncomingData(aLock, now, aStream, aStreamBlockIndex);
|
2009-04-01 04:52:56 +04:00
|
|
|
if (blockIndex >= 0) {
|
2017-11-22 12:14:02 +03:00
|
|
|
FreeBlock(aLock, blockIndex);
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2010-10-26 05:21:16 +04:00
|
|
|
Block* block = &mIndex[blockIndex];
|
2017-04-12 09:51:34 +03:00
|
|
|
LOG("Allocated block %d to stream %p block %d(%" PRId64 ")", blockIndex,
|
2017-09-14 05:39:06 +03:00
|
|
|
aStream, aStreamBlockIndex, aStreamBlockIndex * BLOCK_SIZE);
|
2009-09-15 06:30:44 +04:00
|
|
|
|
2017-06-08 06:32:13 +03:00
|
|
|
ResourceStreamIterator iter(this, aStream->mResourceID);
|
2017-12-14 11:22:53 +03:00
|
|
|
while (MediaCacheStream* stream = iter.Next(aLock)) {
|
2009-09-15 06:30:45 +04:00
|
|
|
BlockOwner* bo = block->mOwners.AppendElement();
|
2017-04-18 05:44:38 +03:00
|
|
|
if (!bo) {
|
|
|
|
// Roll back mOwners if any allocation fails.
|
|
|
|
block->mOwners.Clear();
|
2009-09-15 06:30:45 +04:00
|
|
|
return;
|
2017-04-18 05:44:38 +03:00
|
|
|
}
|
2009-09-15 06:30:45 +04:00
|
|
|
bo->mStream = stream;
|
2017-04-18 05:44:38 +03:00
|
|
|
}
|
|
|
|
|
2017-04-23 16:18:07 +03:00
|
|
|
if (block->mOwners.IsEmpty()) {
|
|
|
|
// This happens when all streams with the resource id are closed. We can
|
|
|
|
// just return here now and discard the data.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-18 05:44:38 +03:00
|
|
|
// Tell each stream using this resource about the new block.
|
|
|
|
for (auto& bo : block->mOwners) {
|
2017-09-14 05:39:06 +03:00
|
|
|
bo.mStreamBlock = aStreamBlockIndex;
|
2017-04-18 05:44:38 +03:00
|
|
|
bo.mLastUseTime = now;
|
2017-09-14 05:39:06 +03:00
|
|
|
bo.mStream->mBlocks[aStreamBlockIndex] = blockIndex;
|
|
|
|
if (aStreamBlockIndex * BLOCK_SIZE < bo.mStream->mStreamOffset) {
|
2021-04-09 19:03:44 +03:00
|
|
|
bo.mClass = PLAYED_BLOCK;
|
2009-09-15 06:30:45 +04:00
|
|
|
// 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).
|
2017-11-22 12:14:02 +03:00
|
|
|
GetListForBlock(aLock, &bo)->AddFirstBlock(blockIndex);
|
|
|
|
Verify(aLock);
|
2009-09-15 06:30:45 +04:00
|
|
|
} 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.
|
2017-04-18 05:44:38 +03:00
|
|
|
bo.mClass = READAHEAD_BLOCK;
|
2017-11-22 12:14:02 +03:00
|
|
|
InsertReadaheadBlock(aLock, &bo, blockIndex);
|
2009-09-15 06:30:45 +04:00
|
|
|
}
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2017-04-18 05:44:38 +03:00
|
|
|
// Invariant: block->mOwners.IsEmpty() iff we can find an entry
|
|
|
|
// in mFreeBlocks for a given blockIndex.
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!block->mOwners.IsEmpty());
|
|
|
|
mFreeBlocks.RemoveBlock(blockIndex);
|
|
|
|
|
2017-06-14 08:01:46 +03:00
|
|
|
nsresult rv = mBlockCache->WriteBlock(blockIndex, aData1, aData2);
|
2009-04-01 04:52:56 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2017-04-12 09:51:34 +03:00
|
|
|
LOG("Released block %d from stream %p block %d(%" PRId64 ")", blockIndex,
|
2017-09-14 05:39:06 +03:00
|
|
|
aStream, aStreamBlockIndex, aStreamBlockIndex * BLOCK_SIZE);
|
2017-11-22 12:14:02 +03:00
|
|
|
FreeBlock(aLock, blockIndex);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Queue an Update since the cache state has changed (for example
|
|
|
|
// we might want to stop loading because the cache is full)
|
2017-11-22 12:14:02 +03:00
|
|
|
QueueUpdate(aLock);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
void MediaCache::OpenStream(AutoLock& aLock, MediaCacheStream* aStream,
|
|
|
|
bool aIsClone) {
|
2020-10-04 10:19:18 +03:00
|
|
|
LOG("Stream %p opened, aIsClone=%d, mCacheSuspended=%d, "
|
|
|
|
"mDidNotifyDataEnded=%d",
|
|
|
|
aStream, aIsClone, aStream->mCacheSuspended,
|
|
|
|
aStream->mDidNotifyDataEnded);
|
2009-04-01 04:52:56 +04:00
|
|
|
mStreams.AppendElement(aStream);
|
2017-10-26 06:13:38 +03:00
|
|
|
|
|
|
|
// A cloned stream should've got the ID from its original.
|
|
|
|
if (!aIsClone) {
|
|
|
|
MOZ_ASSERT(aStream->mResourceID == 0, "mResourceID has been initialized.");
|
2017-11-22 12:14:02 +03:00
|
|
|
aStream->mResourceID = AllocateResourceID(aLock);
|
2017-10-26 06:13:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We should have a valid ID now no matter it is cloned or not.
|
|
|
|
MOZ_ASSERT(aStream->mResourceID > 0, "mResourceID is invalid");
|
2010-08-13 04:41:47 +04:00
|
|
|
|
|
|
|
// Queue an update since a new stream has been opened.
|
2017-11-22 12:14:02 +03:00
|
|
|
QueueUpdate(aLock);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2017-12-19 12:51:51 +03:00
|
|
|
void MediaCache::ReleaseStream(AutoLock&, MediaCacheStream* aStream) {
|
|
|
|
MOZ_ASSERT(OwnerThread()->IsOnCurrentThread());
|
2017-04-12 09:51:34 +03:00
|
|
|
LOG("Stream %p closed", aStream);
|
2009-04-01 04:52:56 +04:00
|
|
|
mStreams.RemoveElement(aStream);
|
2017-12-19 12:51:51 +03:00
|
|
|
// The caller needs to call QueueUpdate() to re-run Update().
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
void MediaCache::ReleaseStreamBlocks(AutoLock& aLock,
|
|
|
|
MediaCacheStream* aStream) {
|
2009-04-01 04:52:56 +04: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 19:56:38 +04:00
|
|
|
uint32_t length = aStream->mBlocks.Length();
|
|
|
|
for (uint32_t i = 0; i < length; ++i) {
|
|
|
|
int32_t blockIndex = aStream->mBlocks[i];
|
2009-04-01 04:52:56 +04:00
|
|
|
if (blockIndex >= 0) {
|
2017-04-12 09:51:34 +03:00
|
|
|
LOG("Released block %d from stream %p block %d(%" PRId64 ")", blockIndex,
|
|
|
|
aStream, i, i * BLOCK_SIZE);
|
2017-11-22 12:14:02 +03:00
|
|
|
RemoveBlockOwner(aLock, blockIndex, aStream);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
void MediaCache::Truncate() {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t end;
|
2009-04-01 04:52:56 +04:00
|
|
|
for (end = mIndex.Length(); end > 0; --end) {
|
2009-09-15 06:30:44 +04:00
|
|
|
if (!IsBlockFree(end - 1)) break;
|
2009-04-01 04:52:56 +04:00
|
|
|
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.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
void MediaCache::NoteBlockUsage(AutoLock& aLock, MediaCacheStream* aStream,
|
2016-03-24 09:59:33 +03:00
|
|
|
int32_t aBlockIndex, int64_t aStreamOffset,
|
2017-11-22 12:14:02 +03:00
|
|
|
MediaCacheStream::ReadMode aMode,
|
|
|
|
TimeStamp aNow) {
|
2009-04-01 04:52:56 +04:00
|
|
|
if (aBlockIndex < 0) {
|
|
|
|
// this block is not in the cache yet
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
BlockOwner* bo = GetBlockOwner(aLock, aBlockIndex, aStream);
|
2009-09-15 06:30:44 +04:00
|
|
|
if (!bo) {
|
2009-04-01 04:52:56 +04: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
|
2016-03-24 09:59:33 +03:00
|
|
|
NS_ASSERTION(bo->mStreamBlock * BLOCK_SIZE <= aStreamOffset,
|
2009-04-01 04:52:56 +04:00
|
|
|
"Using a block that's behind the read position?");
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
GetListForBlock(aLock, bo)->RemoveBlock(aBlockIndex);
|
2009-09-15 06:30:44 +04:00
|
|
|
bo->mClass =
|
2012-11-14 23:46:40 +04:00
|
|
|
(aMode == MediaCacheStream::MODE_METADATA || bo->mClass == METADATA_BLOCK)
|
2017-01-27 15:20:37 +03:00
|
|
|
? METADATA_BLOCK
|
|
|
|
: PLAYED_BLOCK;
|
2009-04-01 04:52:56 +04:00
|
|
|
// Since this is just being used now, it can definitely be at the front
|
|
|
|
// of mMetadataBlocks or mPlayedBlocks
|
2017-11-22 12:14:02 +03:00
|
|
|
GetListForBlock(aLock, bo)->AddFirstBlock(aBlockIndex);
|
2009-09-15 06:30:44 +04:00
|
|
|
bo->mLastUseTime = aNow;
|
2017-11-22 12:14:02 +03:00
|
|
|
Verify(aLock);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
void MediaCache::NoteSeek(AutoLock& aLock, MediaCacheStream* aStream,
|
|
|
|
int64_t aOldOffset) {
|
2009-04-01 04:52:56 +04: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.
|
2017-07-06 06:42:56 +03:00
|
|
|
int32_t blockIndex = OffsetToBlockIndex(aOldOffset);
|
|
|
|
if (blockIndex < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t endIndex =
|
2017-07-06 06:42:56 +03:00
|
|
|
std::min(OffsetToBlockIndex(aStream->mStreamOffset + (BLOCK_SIZE - 1)),
|
|
|
|
int32_t(aStream->mBlocks.Length()));
|
|
|
|
if (endIndex < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2009-04-11 13:39:02 +04:00
|
|
|
TimeStamp now = TimeStamp::Now();
|
2009-04-01 04:52:56 +04:00
|
|
|
while (blockIndex < endIndex) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t cacheBlockIndex = aStream->mBlocks[blockIndex];
|
2009-04-01 04:52:56 +04:00
|
|
|
if (cacheBlockIndex >= 0) {
|
|
|
|
// Marking the block used may not be exactly what we want but
|
|
|
|
// it's simple
|
2017-11-22 12:14:02 +03:00
|
|
|
NoteBlockUsage(aLock, aStream, cacheBlockIndex, aStream->mStreamOffset,
|
|
|
|
MediaCacheStream::MODE_PLAYBACK, now);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
++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 19:56:38 +04:00
|
|
|
int32_t blockIndex =
|
2017-07-06 06:42:56 +03:00
|
|
|
OffsetToBlockIndex(aStream->mStreamOffset + (BLOCK_SIZE - 1));
|
|
|
|
if (blockIndex < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t endIndex =
|
2017-07-06 06:42:56 +03:00
|
|
|
std::min(OffsetToBlockIndex(aOldOffset + (BLOCK_SIZE - 1)),
|
|
|
|
int32_t(aStream->mBlocks.Length()));
|
|
|
|
if (endIndex < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2009-04-01 04:52:56 +04:00
|
|
|
while (blockIndex < endIndex) {
|
2014-11-18 03:20:00 +03:00
|
|
|
MOZ_ASSERT(endIndex > 0);
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t cacheBlockIndex = aStream->mBlocks[endIndex - 1];
|
2009-04-01 04:52:56 +04:00
|
|
|
if (cacheBlockIndex >= 0) {
|
2017-11-22 12:14:02 +03:00
|
|
|
BlockOwner* bo = GetBlockOwner(aLock, cacheBlockIndex, aStream);
|
2009-09-15 06:30:44 +04:00
|
|
|
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 04:52:56 +04: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 06:30:44 +04:00
|
|
|
aStream->mReadaheadBlocks.AddFirstBlock(cacheBlockIndex);
|
2017-11-22 12:14:02 +03:00
|
|
|
Verify(aLock);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
--endIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 10:06:39 +03:00
|
|
|
void MediaCacheStream::NotifyLoadID(uint32_t aLoadID) {
|
|
|
|
MOZ_ASSERT(aLoadID > 0);
|
2017-11-16 10:41:36 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
|
|
|
|
"MediaCacheStream::NotifyLoadID",
|
|
|
|
[client = RefPtr<ChannelMediaResource>(mClient), this, aLoadID]() {
|
2017-11-22 12:14:02 +03:00
|
|
|
AutoLock lock(mMediaCache->Monitor());
|
2017-11-16 10:41:36 +03:00
|
|
|
mLoadID = aLoadID;
|
|
|
|
});
|
|
|
|
OwnerThread()->Dispatch(r.forget());
|
2017-11-10 10:06:39 +03:00
|
|
|
}
|
|
|
|
|
2017-11-16 10:41:36 +03:00
|
|
|
void MediaCacheStream::NotifyDataStartedInternal(uint32_t aLoadID,
|
|
|
|
int64_t aOffset,
|
|
|
|
bool aSeekable,
|
|
|
|
int64_t aLength) {
|
|
|
|
MOZ_ASSERT(OwnerThread()->IsOnCurrentThread());
|
2017-09-20 06:41:08 +03:00
|
|
|
MOZ_ASSERT(aLoadID > 0);
|
2017-11-16 07:05:19 +03:00
|
|
|
LOG("Stream %p DataStarted: %" PRId64 " aLoadID=%u aLength=%" PRId64, this,
|
|
|
|
aOffset, aLoadID, aLength);
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
AutoLock lock(mMediaCache->Monitor());
|
2017-10-25 04:37:58 +03:00
|
|
|
NS_WARNING_ASSERTION(aOffset == mSeekTarget || aOffset == mChannelOffset,
|
2016-09-01 08:01:16 +03:00
|
|
|
"Server is giving us unexpected offset");
|
2014-11-10 22:58:52 +03:00
|
|
|
MOZ_ASSERT(aOffset >= 0);
|
2017-11-16 07:05:19 +03:00
|
|
|
if (aLength >= 0) {
|
|
|
|
mStreamLength = aLength;
|
|
|
|
}
|
2009-04-01 04:52:56 +04:00
|
|
|
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 16:22:03 +04:00
|
|
|
mStreamLength = std::max(mStreamLength, mChannelOffset);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
2017-09-20 06:41:08 +03:00
|
|
|
mLoadID = aLoadID;
|
2017-10-23 12:00:57 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(aOffset == 0 || aSeekable,
|
|
|
|
"channel offset must be zero when we become non-seekable");
|
|
|
|
mIsTransportSeekable = aSeekable;
|
|
|
|
// Queue an Update since we may change our strategy for dealing
|
|
|
|
// with this stream
|
2017-11-22 12:14:02 +03:00
|
|
|
mMediaCache->QueueUpdate(lock);
|
2017-10-25 04:37:58 +03:00
|
|
|
|
|
|
|
// Reset mSeekTarget since the seek is completed so MediaCache::Update() will
|
|
|
|
// make decisions based on mChannelOffset instead of mSeekTarget.
|
|
|
|
mSeekTarget = -1;
|
2017-11-16 09:50:49 +03:00
|
|
|
|
|
|
|
// Reset these flags since a new load has begun.
|
|
|
|
mChannelEnded = false;
|
|
|
|
mDidNotifyDataEnded = false;
|
2017-11-29 11:49:54 +03:00
|
|
|
|
|
|
|
UpdateDownloadStatistics(lock);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2017-11-16 10:41:36 +03:00
|
|
|
void MediaCacheStream::NotifyDataStarted(uint32_t aLoadID, int64_t aOffset,
|
|
|
|
bool aSeekable, int64_t aLength) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aLoadID > 0);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
|
|
|
|
"MediaCacheStream::NotifyDataStarted",
|
|
|
|
[=, client = RefPtr<ChannelMediaResource>(mClient)]() {
|
|
|
|
NotifyDataStartedInternal(aLoadID, aOffset, aSeekable, aLength);
|
|
|
|
});
|
|
|
|
OwnerThread()->Dispatch(r.forget());
|
|
|
|
}
|
|
|
|
|
2017-09-20 06:41:08 +03:00
|
|
|
void MediaCacheStream::NotifyDataReceived(uint32_t aLoadID, uint32_t aCount,
|
2017-11-01 18:20:11 +03:00
|
|
|
const uint8_t* aData) {
|
2017-11-15 12:56:10 +03:00
|
|
|
MOZ_ASSERT(OwnerThread()->IsOnCurrentThread());
|
2017-09-20 06:41:08 +03:00
|
|
|
MOZ_ASSERT(aLoadID > 0);
|
2017-09-08 12:46:56 +03:00
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
AutoLock lock(mMediaCache->Monitor());
|
2017-09-19 10:32:54 +03:00
|
|
|
if (mClosed) {
|
|
|
|
// Nothing to do if the stream is closed.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-01 18:20:11 +03:00
|
|
|
LOG("Stream %p DataReceived at %" PRId64 " count=%u aLoadID=%u", this,
|
2017-09-20 06:41:08 +03:00
|
|
|
mChannelOffset, aCount, aLoadID);
|
|
|
|
|
|
|
|
if (mLoadID != aLoadID) {
|
|
|
|
// mChannelOffset is updated to a new position when loading a new channel.
|
|
|
|
// We should discard the data coming from the old channel so it won't be
|
|
|
|
// stored to the wrong positoin.
|
|
|
|
return;
|
|
|
|
}
|
2017-11-01 18:20:11 +03:00
|
|
|
|
2017-11-29 11:49:54 +03:00
|
|
|
mDownloadStatistics.AddBytes(aCount);
|
|
|
|
|
2018-01-15 05:09:02 +03:00
|
|
|
// True if we commit any blocks to the cache.
|
|
|
|
bool cacheUpdated = false;
|
|
|
|
|
2020-08-07 10:49:47 +03:00
|
|
|
auto source = Span<const uint8_t>(aData, aCount);
|
2009-04-01 04:52:56 +04:00
|
|
|
|
|
|
|
// We process the data one block (or part of a block) at a time
|
2017-11-01 18:20:11 +03:00
|
|
|
while (!source.IsEmpty()) {
|
|
|
|
// The data we've collected so far in the partial block.
|
2020-08-07 10:49:47 +03:00
|
|
|
auto partial = Span<const uint8_t>(mPartialBlockBuffer.get(),
|
|
|
|
OffsetInBlock(mChannelOffset));
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2017-11-01 18:20:11 +03:00
|
|
|
// The number of bytes needed to complete the partial block.
|
|
|
|
size_t remaining = BLOCK_SIZE - partial.Length();
|
2017-04-11 11:06:08 +03:00
|
|
|
|
2017-11-01 18:20:11 +03:00
|
|
|
if (source.Length() >= remaining) {
|
2017-04-11 11:06:08 +03:00
|
|
|
// We have a whole block now to write it out.
|
2017-11-01 18:20:11 +03:00
|
|
|
mMediaCache->AllocateAndWriteBlock(
|
2021-04-09 19:03:44 +03:00
|
|
|
lock, this, OffsetToBlockIndexUnchecked(mChannelOffset), partial,
|
|
|
|
source.First(remaining));
|
2017-11-01 18:20:11 +03:00
|
|
|
source = source.From(remaining);
|
|
|
|
mChannelOffset += remaining;
|
2018-01-15 05:09:02 +03:00
|
|
|
cacheUpdated = true;
|
2009-04-01 04:52:56 +04:00
|
|
|
} else {
|
2017-11-01 18:20:11 +03:00
|
|
|
// The buffer to be filled in the partial block.
|
2020-08-07 10:49:47 +03:00
|
|
|
auto buf = Span<uint8_t>(mPartialBlockBuffer.get() + partial.Length(),
|
|
|
|
remaining);
|
2017-11-01 18:20:11 +03:00
|
|
|
memcpy(buf.Elements(), source.Elements(), source.Length());
|
|
|
|
mChannelOffset += source.Length();
|
|
|
|
break;
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-08 08:30:40 +03:00
|
|
|
MediaCache::ResourceStreamIterator iter(mMediaCache, mResourceID);
|
2017-12-14 11:22:53 +03:00
|
|
|
while (MediaCacheStream* stream = iter.Next(lock)) {
|
2009-09-15 06:30:45 +04:00
|
|
|
if (stream->mStreamLength >= 0) {
|
|
|
|
// The stream is at least as long as what we've read
|
2013-01-15 16:22:03 +04:00
|
|
|
stream->mStreamLength = std::max(stream->mStreamLength, mChannelOffset);
|
2009-09-15 06:30:45 +04:00
|
|
|
}
|
|
|
|
stream->mClient->CacheClientNotifyDataReceived();
|
|
|
|
}
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
// XXX it would be fairly easy to optimize things a lot more to
|
|
|
|
// avoid waking up reader threads unnecessarily
|
2018-01-15 05:09:02 +03:00
|
|
|
if (cacheUpdated) {
|
|
|
|
// Wake up the reader who is waiting for the committed blocks.
|
|
|
|
lock.NotifyAll();
|
|
|
|
}
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
void MediaCacheStream::FlushPartialBlockInternal(AutoLock& aLock,
|
|
|
|
bool aNotifyAll) {
|
2017-11-20 11:30:05 +03:00
|
|
|
MOZ_ASSERT(OwnerThread()->IsOnCurrentThread());
|
|
|
|
|
2017-09-14 05:39:06 +03:00
|
|
|
int32_t blockIndex = OffsetToBlockIndexUnchecked(mChannelOffset);
|
2017-07-06 06:42:56 +03:00
|
|
|
int32_t blockOffset = OffsetInBlock(mChannelOffset);
|
2009-04-01 04:52:56 +04:00
|
|
|
if (blockOffset > 0) {
|
2017-04-12 09:51:34 +03:00
|
|
|
LOG("Stream %p writing partial block: [%d] bytes; "
|
|
|
|
"mStreamOffset [%" PRId64 "] mChannelOffset[%" PRId64
|
|
|
|
"] mStreamLength [%" PRId64 "] notifying: [%s]",
|
|
|
|
this, blockOffset, mStreamOffset, mChannelOffset, mStreamLength,
|
|
|
|
aNotifyAll ? "yes" : "no");
|
2012-12-07 03:27:08 +04:00
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
// Write back the partial block
|
2017-04-11 11:52:38 +03:00
|
|
|
memset(mPartialBlockBuffer.get() + blockOffset, 0,
|
|
|
|
BLOCK_SIZE - blockOffset);
|
2020-08-07 10:49:47 +03:00
|
|
|
auto data = Span<const uint8_t>(mPartialBlockBuffer.get(), BLOCK_SIZE);
|
2021-04-09 19:03:44 +03:00
|
|
|
mMediaCache->AllocateAndWriteBlock(aLock, this, blockIndex, data);
|
2014-11-06 08:53:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// |mChannelOffset == 0| means download ends with no bytes received.
|
|
|
|
// We should also wake up those readers who are waiting for data
|
|
|
|
// that will never come.
|
|
|
|
if ((blockOffset > 0 || mChannelOffset == 0) && aNotifyAll) {
|
|
|
|
// Wake up readers who may be waiting for this data
|
2017-11-22 12:14:02 +03:00
|
|
|
aLock.NotifyAll();
|
2012-12-07 03:27:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-29 11:49:54 +03:00
|
|
|
void MediaCacheStream::UpdateDownloadStatistics(AutoLock&) {
|
|
|
|
if (mChannelEnded || mClientSuspended) {
|
|
|
|
mDownloadStatistics.Stop();
|
|
|
|
} else {
|
|
|
|
mDownloadStatistics.Start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-24 18:38:02 +03:00
|
|
|
void MediaCacheStream::NotifyDataEndedInternal(uint32_t aLoadID,
|
|
|
|
nsresult aStatus) {
|
2017-11-20 11:30:05 +03:00
|
|
|
MOZ_ASSERT(OwnerThread()->IsOnCurrentThread());
|
2017-11-22 12:14:02 +03:00
|
|
|
AutoLock lock(mMediaCache->Monitor());
|
2012-12-07 03:27:08 +04:00
|
|
|
|
2017-11-10 10:06:39 +03:00
|
|
|
if (mClosed || aLoadID != mLoadID) {
|
|
|
|
// Nothing to do if the stream is closed or a new load has begun.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-09-24 11:25:00 +04:00
|
|
|
// It is prudent to update channel/cache status before calling
|
|
|
|
// CacheClientNotifyDataEnded() which will read |mChannelEnded|.
|
|
|
|
mChannelEnded = true;
|
2017-11-22 12:14:02 +03:00
|
|
|
mMediaCache->QueueUpdate(lock);
|
2012-12-07 03:27:08 +04:00
|
|
|
|
2017-11-29 11:49:54 +03:00
|
|
|
UpdateDownloadStatistics(lock);
|
|
|
|
|
2017-11-15 11:45:57 +03:00
|
|
|
if (NS_FAILED(aStatus)) {
|
|
|
|
// Notify the client about this network error.
|
|
|
|
mDidNotifyDataEnded = true;
|
|
|
|
mNotifyDataEndedStatus = aStatus;
|
|
|
|
mClient->CacheClientNotifyDataEnded(aStatus);
|
|
|
|
// Wake up the readers so they can fail gracefully.
|
2017-11-22 12:14:02 +03:00
|
|
|
lock.NotifyAll();
|
2017-11-15 11:45:57 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note we don't flush the partial block when download ends abnormally for
|
|
|
|
// the padding zeros will give wrong data to other streams.
|
2017-11-22 12:14:02 +03:00
|
|
|
FlushPartialBlockInternal(lock, true);
|
2017-11-15 11:45:57 +03:00
|
|
|
|
2017-06-08 08:30:40 +03:00
|
|
|
MediaCache::ResourceStreamIterator iter(mMediaCache, mResourceID);
|
2017-12-14 11:22:53 +03:00
|
|
|
while (MediaCacheStream* stream = iter.Next(lock)) {
|
2017-11-15 11:45:57 +03:00
|
|
|
// We read the whole stream, so remember the true length
|
|
|
|
stream->mStreamLength = mChannelOffset;
|
2014-05-11 22:43:00 +04:00
|
|
|
if (!stream->mDidNotifyDataEnded) {
|
2011-11-24 03:05:12 +04:00
|
|
|
stream->mDidNotifyDataEnded = true;
|
|
|
|
stream->mNotifyDataEndedStatus = aStatus;
|
|
|
|
stream->mClient->CacheClientNotifyDataEnded(aStatus);
|
2009-09-15 06:30:45 +04:00
|
|
|
}
|
|
|
|
}
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2018-05-24 18:38:02 +03:00
|
|
|
void MediaCacheStream::NotifyDataEnded(uint32_t aLoadID, nsresult aStatus) {
|
2017-11-10 10:06:39 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aLoadID > 0);
|
|
|
|
|
|
|
|
RefPtr<ChannelMediaResource> client = mClient;
|
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
|
2018-05-24 18:38:02 +03:00
|
|
|
"MediaCacheStream::NotifyDataEnded", [client, this, aLoadID, aStatus]() {
|
|
|
|
NotifyDataEndedInternal(aLoadID, aStatus);
|
2017-11-10 10:06:39 +03:00
|
|
|
});
|
|
|
|
OwnerThread()->Dispatch(r.forget());
|
|
|
|
}
|
|
|
|
|
2017-10-24 06:25:41 +03:00
|
|
|
void MediaCacheStream::NotifyClientSuspended(bool aSuspended) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
RefPtr<ChannelMediaResource> client = mClient;
|
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
|
|
|
|
"MediaCacheStream::NotifyClientSuspended", [client, this, aSuspended]() {
|
2017-11-22 12:14:02 +03:00
|
|
|
AutoLock lock(mMediaCache->Monitor());
|
2017-11-20 09:21:22 +03:00
|
|
|
if (!mClosed && mClientSuspended != aSuspended) {
|
2017-10-24 06:25:41 +03:00
|
|
|
mClientSuspended = aSuspended;
|
|
|
|
// mClientSuspended changes the decision of reading streams.
|
2017-11-22 12:14:02 +03:00
|
|
|
mMediaCache->QueueUpdate(lock);
|
2017-11-29 11:49:54 +03:00
|
|
|
UpdateDownloadStatistics(lock);
|
2018-01-15 05:25:11 +03:00
|
|
|
if (mClientSuspended) {
|
|
|
|
// Download is suspended. Wake up the readers that might be able to
|
|
|
|
// get data from the partial block.
|
|
|
|
lock.NotifyAll();
|
2018-11-19 16:25:37 +03:00
|
|
|
}
|
2018-01-15 05:25:11 +03:00
|
|
|
}
|
2017-10-24 06:25:41 +03:00
|
|
|
});
|
|
|
|
OwnerThread()->Dispatch(r.forget());
|
|
|
|
}
|
|
|
|
|
2017-12-07 10:43:36 +03:00
|
|
|
void MediaCacheStream::NotifyResume() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
|
|
|
|
"MediaCacheStream::NotifyResume",
|
|
|
|
[this, client = RefPtr<ChannelMediaResource>(mClient)]() {
|
|
|
|
AutoLock lock(mMediaCache->Monitor());
|
|
|
|
if (mClosed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Don't resume download if we are already at the end of the stream for
|
|
|
|
// seek will fail and be wasted anyway.
|
2017-12-19 09:35:25 +03:00
|
|
|
int64_t offset = mSeekTarget != -1 ? mSeekTarget : mChannelOffset;
|
|
|
|
if (mStreamLength < 0 || offset < mStreamLength) {
|
2017-12-07 10:43:36 +03:00
|
|
|
mClient->CacheClientSeek(offset, false);
|
|
|
|
// DownloadResumed() will be notified when a new channel is opened.
|
|
|
|
}
|
|
|
|
// The channel remains dead. If we want to read some other data in the
|
|
|
|
// future, CacheClientSeek() will be called to reopen the channel.
|
|
|
|
});
|
|
|
|
OwnerThread()->Dispatch(r.forget());
|
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
MediaCacheStream::~MediaCacheStream() {
|
2017-12-19 12:51:51 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Only call on main thread");
|
|
|
|
MOZ_ASSERT(!mPinCount, "Unbalanced Pin");
|
|
|
|
MOZ_ASSERT(!mMediaCache || mClosed);
|
2017-06-02 01:43:45 +03:00
|
|
|
|
|
|
|
uint32_t lengthKb = uint32_t(std::min(
|
|
|
|
std::max(mStreamLength, int64_t(0)) / 1024, int64_t(UINT32_MAX)));
|
|
|
|
LOG("MediaCacheStream::~MediaCacheStream(this=%p) "
|
|
|
|
"MEDIACACHESTREAM_LENGTH_KB=%" PRIu32,
|
|
|
|
this, lengthKb);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
bool MediaCacheStream::AreAllStreamsForResourceSuspended(AutoLock& aLock) {
|
2017-11-20 11:30:05 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
|
2017-06-08 08:30:40 +03:00
|
|
|
MediaCache::ResourceStreamIterator iter(mMediaCache, mResourceID);
|
2013-04-24 13:19:02 +04:00
|
|
|
// Look for a stream that's able to read the data we need
|
|
|
|
int64_t dataOffset = -1;
|
2017-12-14 11:22:53 +03:00
|
|
|
while (MediaCacheStream* stream = iter.Next(aLock)) {
|
2013-04-24 13:19:02 +04:00
|
|
|
if (stream->mCacheSuspended || stream->mChannelEnded || stream->mClosed) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (dataOffset < 0) {
|
2017-11-22 12:14:02 +03:00
|
|
|
dataOffset = GetCachedDataEndInternal(aLock, mStreamOffset);
|
2012-02-15 08:35:01 +04:00
|
|
|
}
|
2013-04-24 13:19:02 +04:00
|
|
|
// Ignore streams that are reading beyond the data we need
|
|
|
|
if (stream->mChannelOffset > dataOffset) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return false;
|
2011-12-09 08:48:40 +04:00
|
|
|
}
|
2013-04-24 13:19:02 +04:00
|
|
|
|
2011-11-30 09:05:49 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-14 01:40:14 +03:00
|
|
|
RefPtr<GenericPromise> MediaCacheStream::Close() {
|
2017-12-14 11:22:53 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (!mMediaCache) {
|
2019-11-14 01:40:14 +03:00
|
|
|
return GenericPromise::CreateAndResolve(true, __func__);
|
2017-06-09 04:48:06 +03:00
|
|
|
}
|
2019-11-14 01:40:14 +03:00
|
|
|
|
|
|
|
return InvokeAsync(OwnerThread(), "MediaCacheStream::Close",
|
|
|
|
[this, client = RefPtr<ChannelMediaResource>(mClient)] {
|
|
|
|
AutoLock lock(mMediaCache->Monitor());
|
|
|
|
CloseInternal(lock);
|
|
|
|
return GenericPromise::CreateAndResolve(true, __func__);
|
|
|
|
});
|
2017-12-15 05:29:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void MediaCacheStream::CloseInternal(AutoLock& aLock) {
|
|
|
|
MOZ_ASSERT(OwnerThread()->IsOnCurrentThread());
|
2015-08-17 01:07:50 +03:00
|
|
|
|
2017-12-14 11:22:53 +03:00
|
|
|
if (mClosed) {
|
|
|
|
return;
|
|
|
|
}
|
2017-10-24 05:00:23 +03:00
|
|
|
|
2014-12-19 01:48:00 +03:00
|
|
|
// Closing a stream will change the return value of
|
|
|
|
// MediaCacheStream::AreAllStreamsForResourceSuspended as well as
|
|
|
|
// ChannelMediaResource::IsSuspendedByCache. Let's notify it.
|
2017-12-15 05:29:29 +03:00
|
|
|
mMediaCache->QueueSuspendedStatusUpdate(aLock, mResourceID);
|
2017-08-29 11:46:42 +03:00
|
|
|
|
2017-09-08 12:46:56 +03:00
|
|
|
mClosed = true;
|
2017-12-15 05:29:29 +03:00
|
|
|
mMediaCache->ReleaseStreamBlocks(aLock, this);
|
2017-12-19 12:51:51 +03:00
|
|
|
mMediaCache->ReleaseStream(aLock, this);
|
2009-04-01 04:52:56 +04:00
|
|
|
// Wake up any blocked readers
|
2017-12-15 05:29:29 +03:00
|
|
|
aLock.NotifyAll();
|
2017-08-29 11:46:42 +03:00
|
|
|
|
2017-12-15 05:29:29 +03:00
|
|
|
// Queue an Update since we may have created more free space.
|
|
|
|
mMediaCache->QueueUpdate(aLock);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
void MediaCacheStream::Pin() {
|
2018-01-04 05:30:07 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2017-11-22 12:14:02 +03:00
|
|
|
AutoLock lock(mMediaCache->Monitor());
|
2009-04-01 04:52:56 +04: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
|
2017-11-22 12:14:02 +03:00
|
|
|
mMediaCache->QueueUpdate(lock);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
void MediaCacheStream::Unpin() {
|
2018-01-04 05:30:07 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2017-11-22 12:14:02 +03:00
|
|
|
AutoLock lock(mMediaCache->Monitor());
|
2009-04-01 04:52:56 +04: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
|
2017-11-22 12:14:02 +03:00
|
|
|
mMediaCache->QueueUpdate(lock);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2018-05-23 22:27:31 +03:00
|
|
|
int64_t MediaCacheStream::GetLength() const {
|
2017-12-07 10:50:41 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2017-11-22 12:14:02 +03:00
|
|
|
AutoLock lock(mMediaCache->Monitor());
|
2009-04-01 04:52:56 +04:00
|
|
|
return mStreamLength;
|
|
|
|
}
|
|
|
|
|
2018-05-24 15:36:58 +03:00
|
|
|
MediaCacheStream::LengthAndOffset MediaCacheStream::GetLengthAndOffset() const {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
AutoLock lock(mMediaCache->Monitor());
|
|
|
|
return {mStreamLength, mChannelOffset};
|
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
int64_t MediaCacheStream::GetNextCachedData(int64_t aOffset) {
|
2017-11-20 11:30:05 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2017-11-22 12:14:02 +03:00
|
|
|
AutoLock lock(mMediaCache->Monitor());
|
|
|
|
return GetNextCachedDataInternal(lock, aOffset);
|
2009-05-18 02:15:57 +04:00
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
int64_t MediaCacheStream::GetCachedDataEnd(int64_t aOffset) {
|
2017-12-05 10:23:43 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2017-11-22 12:14:02 +03:00
|
|
|
AutoLock lock(mMediaCache->Monitor());
|
|
|
|
return GetCachedDataEndInternal(lock, aOffset);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
bool MediaCacheStream::IsDataCachedToEndOfStream(int64_t aOffset) {
|
2017-11-20 11:30:05 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2017-11-22 12:14:02 +03:00
|
|
|
AutoLock lock(mMediaCache->Monitor());
|
2009-04-01 04:52:56 +04:00
|
|
|
if (mStreamLength < 0) return false;
|
2017-11-22 12:14:02 +03:00
|
|
|
return GetCachedDataEndInternal(lock, aOffset) >= mStreamLength;
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
int64_t MediaCacheStream::GetCachedDataEndInternal(AutoLock&, int64_t aOffset) {
|
2017-07-06 06:42:56 +03:00
|
|
|
int32_t blockIndex = OffsetToBlockIndex(aOffset);
|
|
|
|
if (blockIndex < 0) {
|
|
|
|
return aOffset;
|
|
|
|
}
|
|
|
|
while (size_t(blockIndex) < mBlocks.Length() && mBlocks[blockIndex] != -1) {
|
2009-04-01 04:52:56 +04:00
|
|
|
++blockIndex;
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t result = blockIndex * BLOCK_SIZE;
|
2017-07-06 06:42:56 +03:00
|
|
|
if (blockIndex == OffsetToBlockIndexUnchecked(mChannelOffset)) {
|
2009-04-01 04:52:56 +04:00
|
|
|
// 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 16:22:03 +04:00
|
|
|
result = std::min(result, mStreamLength);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
2013-01-15 16:22:03 +04:00
|
|
|
return std::max(result, aOffset);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
int64_t MediaCacheStream::GetNextCachedDataInternal(AutoLock&,
|
|
|
|
int64_t aOffset) {
|
2009-05-18 02:15:57 +04:00
|
|
|
if (aOffset == mStreamLength) return -1;
|
2010-10-26 05:21:16 +04:00
|
|
|
|
2017-07-06 06:42:56 +03:00
|
|
|
int32_t startBlockIndex = OffsetToBlockIndex(aOffset);
|
|
|
|
if (startBlockIndex < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
int32_t channelBlockIndex = OffsetToBlockIndexUnchecked(mChannelOffset);
|
2009-05-18 02:15:57 +04: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;
|
|
|
|
}
|
|
|
|
|
2017-07-06 06:42:56 +03:00
|
|
|
if (size_t(startBlockIndex) >= mBlocks.Length()) return -1;
|
2009-05-18 02:15:57 +04:00
|
|
|
|
|
|
|
// Is the current block cached?
|
|
|
|
if (mBlocks[startBlockIndex] != -1) return aOffset;
|
|
|
|
|
|
|
|
// Count the number of uncached blocks
|
2017-07-06 06:42:56 +03:00
|
|
|
bool hasPartialBlock = OffsetInBlock(mChannelOffset) != 0;
|
|
|
|
int32_t blockIndex = startBlockIndex + 1;
|
2011-09-30 03:34:37 +04:00
|
|
|
while (true) {
|
2009-05-18 02:15:57 +04:00
|
|
|
if ((hasPartialBlock && blockIndex == channelBlockIndex) ||
|
2017-07-06 06:42:56 +03:00
|
|
|
(size_t(blockIndex) < mBlocks.Length() && mBlocks[blockIndex] != -1)) {
|
2009-05-18 02:15:57 +04:00
|
|
|
// 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?
|
2017-07-06 06:42:56 +03:00
|
|
|
if (size_t(blockIndex) >= mBlocks.Length()) return -1;
|
2009-05-18 02:15:57 +04:00
|
|
|
|
|
|
|
++blockIndex;
|
|
|
|
}
|
|
|
|
|
2018-06-18 08:43:11 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("Should return in loop");
|
2009-05-18 02:15:57 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
void MediaCacheStream::SetReadMode(ReadMode aMode) {
|
2017-11-28 05:51:47 +03:00
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
|
|
|
|
"MediaCacheStream::SetReadMode",
|
|
|
|
[this, client = RefPtr<ChannelMediaResource>(mClient), aMode]() {
|
|
|
|
AutoLock lock(mMediaCache->Monitor());
|
|
|
|
if (!mClosed && mCurrentMode != aMode) {
|
|
|
|
mCurrentMode = aMode;
|
|
|
|
mMediaCache->QueueUpdate(lock);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
OwnerThread()->Dispatch(r.forget());
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
void MediaCacheStream::SetPlaybackRate(uint32_t aBytesPerSecond) {
|
2017-12-05 09:15:38 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2017-11-16 10:41:36 +03:00
|
|
|
MOZ_ASSERT(aBytesPerSecond > 0, "Zero playback rate not allowed");
|
|
|
|
|
2017-12-05 09:15:38 +03:00
|
|
|
AutoLock lock(mMediaCache->Monitor());
|
|
|
|
if (!mClosed && mPlaybackBytesPerSecond != aBytesPerSecond) {
|
|
|
|
mPlaybackBytesPerSecond = aBytesPerSecond;
|
|
|
|
mMediaCache->QueueUpdate(lock);
|
|
|
|
}
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
nsresult MediaCacheStream::Seek(AutoLock& aLock, int64_t aOffset) {
|
2017-11-02 09:46:24 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2017-11-02 09:46:24 +03:00
|
|
|
if (!IsOffsetAllowed(aOffset)) {
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
2017-11-02 09:26:24 +03:00
|
|
|
if (mClosed) {
|
2017-11-02 09:46:24 +03:00
|
|
|
return NS_ERROR_ABORT;
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
2017-11-02 09:46:24 +03:00
|
|
|
|
2017-11-02 09:26:24 +03:00
|
|
|
int64_t oldOffset = mStreamOffset;
|
2017-11-02 09:46:24 +03:00
|
|
|
mStreamOffset = aOffset;
|
2017-04-12 09:51:34 +03:00
|
|
|
LOG("Stream %p Seek to %" PRId64, this, mStreamOffset);
|
2017-11-22 12:14:02 +03:00
|
|
|
mMediaCache->NoteSeek(aLock, this, oldOffset);
|
|
|
|
mMediaCache->QueueUpdate(aLock);
|
2009-04-01 04:52:56 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-05-11 11:49:36 +03:00
|
|
|
void MediaCacheStream::ThrottleReadahead(bool bThrottle) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-11-16 10:41:36 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
|
|
|
|
"MediaCacheStream::ThrottleReadahead",
|
|
|
|
[client = RefPtr<ChannelMediaResource>(mClient), this, bThrottle]() {
|
2017-11-22 12:14:02 +03:00
|
|
|
AutoLock lock(mMediaCache->Monitor());
|
2017-11-16 10:41:36 +03:00
|
|
|
if (!mClosed && mThrottleReadahead != bThrottle) {
|
|
|
|
LOGI("Stream %p ThrottleReadahead %d", this, bThrottle);
|
|
|
|
mThrottleReadahead = bThrottle;
|
2017-11-22 12:14:02 +03:00
|
|
|
mMediaCache->QueueUpdate(lock);
|
2017-11-16 10:41:36 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
OwnerThread()->Dispatch(r.forget());
|
2017-05-11 11:49:36 +03:00
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
uint32_t MediaCacheStream::ReadPartialBlock(AutoLock&, int64_t aOffset,
|
|
|
|
Span<char> aBuffer) {
|
2017-11-02 06:36:56 +03:00
|
|
|
MOZ_ASSERT(IsOffsetAllowed(aOffset));
|
|
|
|
|
|
|
|
if (OffsetToBlockIndexUnchecked(mChannelOffset) !=
|
|
|
|
OffsetToBlockIndexUnchecked(aOffset) ||
|
|
|
|
aOffset >= mChannelOffset) {
|
|
|
|
// Not in the partial block or no data to read.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-07 10:49:47 +03:00
|
|
|
auto source = Span<const uint8_t>(
|
2017-11-02 06:36:56 +03:00
|
|
|
mPartialBlockBuffer.get() + OffsetInBlock(aOffset),
|
|
|
|
OffsetInBlock(mChannelOffset) - OffsetInBlock(aOffset));
|
|
|
|
// We have |source.Length() <= BLOCK_SIZE < INT32_MAX| to guarantee
|
|
|
|
// that |bytesToRead| can fit into a uint32_t.
|
|
|
|
uint32_t bytesToRead = std::min(aBuffer.Length(), source.Length());
|
|
|
|
memcpy(aBuffer.Elements(), source.Elements(), bytesToRead);
|
|
|
|
return bytesToRead;
|
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
Result<uint32_t, nsresult> MediaCacheStream::ReadBlockFromCache(
|
2017-11-03 11:56:58 +03:00
|
|
|
AutoLock& aLock, int64_t aOffset, Span<char> aBuffer,
|
|
|
|
bool aNoteBlockUsage) {
|
2017-11-02 06:36:56 +03:00
|
|
|
MOZ_ASSERT(IsOffsetAllowed(aOffset));
|
|
|
|
|
|
|
|
// OffsetToBlockIndexUnchecked() is always non-negative.
|
|
|
|
uint32_t index = OffsetToBlockIndexUnchecked(aOffset);
|
|
|
|
int32_t cacheBlock = index < mBlocks.Length() ? mBlocks[index] : -1;
|
2018-09-17 20:41:31 +03:00
|
|
|
if (cacheBlock < 0 || (mStreamLength >= 0 && aOffset >= mStreamLength)) {
|
2017-11-02 06:36:56 +03:00
|
|
|
// Not in the cache.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aBuffer.Length() > size_t(BLOCK_SIZE)) {
|
|
|
|
// Clamp the buffer to avoid overflow below since we will read at most
|
|
|
|
// BLOCK_SIZE bytes.
|
|
|
|
aBuffer = aBuffer.First(BLOCK_SIZE);
|
|
|
|
}
|
2018-09-17 20:41:31 +03:00
|
|
|
|
|
|
|
if (mStreamLength >= 0 &&
|
2018-09-19 11:34:15 +03:00
|
|
|
int64_t(aBuffer.Length()) > mStreamLength - aOffset) {
|
2018-09-17 20:41:31 +03:00
|
|
|
// Clamp reads to stream's length
|
|
|
|
aBuffer = aBuffer.First(mStreamLength - aOffset);
|
|
|
|
}
|
|
|
|
|
2017-11-02 06:36:56 +03:00
|
|
|
// |BLOCK_SIZE - OffsetInBlock(aOffset)| <= BLOCK_SIZE
|
|
|
|
int32_t bytesToRead =
|
|
|
|
std::min<int32_t>(BLOCK_SIZE - OffsetInBlock(aOffset), aBuffer.Length());
|
|
|
|
int32_t bytesRead = 0;
|
2017-11-22 12:14:02 +03:00
|
|
|
nsresult rv = mMediaCache->ReadCacheFile(
|
|
|
|
aLock, cacheBlock * BLOCK_SIZE + OffsetInBlock(aOffset),
|
2017-11-02 06:36:56 +03:00
|
|
|
aBuffer.Elements(), bytesToRead, &bytesRead);
|
|
|
|
|
|
|
|
// Ensure |cacheBlock * BLOCK_SIZE + OffsetInBlock(aOffset)| won't overflow.
|
|
|
|
static_assert(INT64_MAX >= BLOCK_SIZE * (uint32_t(INT32_MAX) + 1),
|
|
|
|
"BLOCK_SIZE too large!");
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
nsCString name;
|
|
|
|
GetErrorName(rv, name);
|
|
|
|
LOGE("Stream %p ReadCacheFile failed, rv=%s", this, name.Data());
|
|
|
|
return mozilla::Err(rv);
|
|
|
|
}
|
|
|
|
|
2017-11-03 11:56:58 +03:00
|
|
|
if (aNoteBlockUsage) {
|
2017-11-22 12:14:02 +03:00
|
|
|
mMediaCache->NoteBlockUsage(aLock, this, cacheBlock, aOffset, mCurrentMode,
|
|
|
|
TimeStamp::Now());
|
2017-11-03 11:56:58 +03:00
|
|
|
}
|
|
|
|
|
2017-11-02 06:36:56 +03:00
|
|
|
return bytesRead;
|
|
|
|
}
|
|
|
|
|
2018-01-05 05:49:38 +03:00
|
|
|
nsresult MediaCacheStream::Read(AutoLock& aLock, char* aBuffer, uint32_t aCount,
|
|
|
|
uint32_t* aBytes) {
|
2017-11-20 11:30:05 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2016-03-23 16:40:18 +03:00
|
|
|
// Cache the offset in case it is changed again when we are waiting for the
|
|
|
|
// monitor to be notified to avoid reading at the wrong position.
|
|
|
|
auto streamOffset = mStreamOffset;
|
|
|
|
|
2017-11-03 11:56:58 +03:00
|
|
|
// The buffer we are about to fill.
|
2020-08-07 10:49:47 +03:00
|
|
|
auto buffer = Span<char>(aBuffer, aCount);
|
2017-11-03 11:56:58 +03:00
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
// Read one block (or part of a block) at a time
|
2017-11-03 11:56:58 +03:00
|
|
|
while (!buffer.IsEmpty()) {
|
2017-10-31 09:54:13 +03:00
|
|
|
if (mClosed) {
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
|
2017-11-03 11:56:58 +03:00
|
|
|
if (!IsOffsetAllowed(streamOffset)) {
|
2017-10-31 09:54:13 +03:00
|
|
|
LOGE("Stream %p invalid offset=%" PRId64, this, streamOffset);
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
2017-07-06 06:42:56 +03:00
|
|
|
}
|
2017-10-31 09:54:13 +03:00
|
|
|
|
2017-11-03 11:56:58 +03:00
|
|
|
if (mStreamLength >= 0 && streamOffset >= mStreamLength) {
|
2009-04-01 04:52:56 +04:00
|
|
|
// Don't try to read beyond the end of the stream
|
2017-11-03 11:56:58 +03:00
|
|
|
break;
|
|
|
|
}
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
Result<uint32_t, nsresult> rv = ReadBlockFromCache(
|
2018-01-05 05:49:38 +03:00
|
|
|
aLock, streamOffset, buffer, true /* aNoteBlockUsage */);
|
2017-11-03 11:56:58 +03:00
|
|
|
if (rv.isErr()) {
|
|
|
|
return rv.unwrapErr();
|
|
|
|
}
|
2017-10-27 08:56:46 +03:00
|
|
|
|
2017-11-03 11:56:58 +03:00
|
|
|
uint32_t bytes = rv.unwrap();
|
|
|
|
if (bytes > 0) {
|
|
|
|
// Got data from the cache successfully. Read next block.
|
|
|
|
streamOffset += bytes;
|
|
|
|
buffer = buffer.From(bytes);
|
2011-11-24 03:01:50 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-11-03 11:56:58 +03:00
|
|
|
// See if we can use the data in the partial block of any stream reading
|
|
|
|
// this resource. Note we use the partial block only when it is completed,
|
|
|
|
// that is reaching EOS.
|
|
|
|
bool foundDataInPartialBlock = false;
|
|
|
|
MediaCache::ResourceStreamIterator iter(mMediaCache, mResourceID);
|
2018-01-05 05:49:38 +03:00
|
|
|
while (MediaCacheStream* stream = iter.Next(aLock)) {
|
2017-11-03 11:56:58 +03:00
|
|
|
if (OffsetToBlockIndexUnchecked(stream->mChannelOffset) ==
|
|
|
|
OffsetToBlockIndexUnchecked(streamOffset) &&
|
|
|
|
stream->mChannelOffset == stream->mStreamLength) {
|
2018-01-05 05:49:38 +03:00
|
|
|
uint32_t bytes = stream->ReadPartialBlock(aLock, streamOffset, buffer);
|
2017-11-03 11:56:58 +03:00
|
|
|
streamOffset += bytes;
|
|
|
|
buffer = buffer.From(bytes);
|
|
|
|
foundDataInPartialBlock = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (foundDataInPartialBlock) {
|
|
|
|
// Break for we've reached EOS.
|
|
|
|
break;
|
|
|
|
}
|
2011-11-24 03:01:50 +04:00
|
|
|
|
2017-12-13 13:28:16 +03:00
|
|
|
if (mDidNotifyDataEnded && NS_FAILED(mNotifyDataEndedStatus)) {
|
|
|
|
// Since download ends abnormally, there is no point in waiting for new
|
|
|
|
// data to come. We will check the partial block to read as many bytes as
|
|
|
|
// possible before exiting this function.
|
2018-01-05 05:49:38 +03:00
|
|
|
bytes = ReadPartialBlock(aLock, streamOffset, buffer);
|
2017-12-13 13:28:16 +03:00
|
|
|
streamOffset += bytes;
|
|
|
|
buffer = buffer.From(bytes);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-11-03 11:56:58 +03:00
|
|
|
if (mStreamOffset != streamOffset) {
|
|
|
|
// Update mStreamOffset before we drop the lock. We need to run
|
|
|
|
// Update() again since stream reading strategy might have changed.
|
|
|
|
mStreamOffset = streamOffset;
|
2018-01-05 05:49:38 +03:00
|
|
|
mMediaCache->QueueUpdate(aLock);
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
2017-11-03 11:56:58 +03:00
|
|
|
|
|
|
|
// No data to read, so block
|
2018-01-05 05:49:38 +03:00
|
|
|
aLock.Wait();
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
|
|
|
|
2017-11-03 11:56:58 +03:00
|
|
|
uint32_t count = buffer.Elements() - aBuffer;
|
2017-11-01 11:53:29 +03:00
|
|
|
*aBytes = count;
|
|
|
|
if (count == 0) {
|
|
|
|
return NS_OK;
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
2017-11-01 11:53:29 +03:00
|
|
|
|
|
|
|
// Some data was read, so queue an update since block priorities may
|
|
|
|
// have changed
|
2018-01-05 05:49:38 +03:00
|
|
|
mMediaCache->QueueUpdate(aLock);
|
2017-11-01 11:53:29 +03:00
|
|
|
|
2017-04-12 09:51:34 +03:00
|
|
|
LOG("Stream %p Read at %" PRId64 " count=%d", this, streamOffset - count,
|
|
|
|
count);
|
2016-03-23 16:40:18 +03:00
|
|
|
mStreamOffset = streamOffset;
|
2009-04-01 04:52:56 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-07-17 05:54:52 +04:00
|
|
|
nsresult MediaCacheStream::ReadAt(int64_t aOffset, char* aBuffer,
|
|
|
|
uint32_t aCount, uint32_t* aBytes) {
|
2017-11-20 11:30:05 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2017-11-22 12:14:02 +03:00
|
|
|
AutoLock lock(mMediaCache->Monitor());
|
|
|
|
nsresult rv = Seek(lock, aOffset);
|
2013-07-17 05:54:52 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2018-01-05 05:49:38 +03:00
|
|
|
return Read(lock, aBuffer, aCount, aBytes);
|
2013-07-17 05:54:52 +04:00
|
|
|
}
|
|
|
|
|
2017-11-02 06:36:56 +03:00
|
|
|
nsresult MediaCacheStream::ReadFromCache(char* aBuffer, int64_t aOffset,
|
|
|
|
uint32_t aCount) {
|
2017-11-20 11:30:05 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2017-11-22 12:14:02 +03:00
|
|
|
AutoLock lock(mMediaCache->Monitor());
|
2009-05-18 02:15:57 +04:00
|
|
|
|
2017-11-02 06:36:56 +03:00
|
|
|
// The buffer we are about to fill.
|
2020-08-07 10:49:47 +03:00
|
|
|
auto buffer = Span<char>(aBuffer, aCount);
|
2017-11-02 06:36:56 +03:00
|
|
|
|
2009-05-18 02:15:57 +04:00
|
|
|
// Read one block (or part of a block) at a time
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t streamOffset = aOffset;
|
2017-11-02 06:36:56 +03:00
|
|
|
while (!buffer.IsEmpty()) {
|
2017-04-10 08:57:13 +03:00
|
|
|
if (mClosed) {
|
|
|
|
// We need to check |mClosed| in each iteration which might be changed
|
2017-06-08 08:30:40 +03:00
|
|
|
// after calling |mMediaCache->ReadCacheFile|.
|
2017-04-10 08:57:13 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2017-11-02 06:36:56 +03:00
|
|
|
|
|
|
|
if (!IsOffsetAllowed(streamOffset)) {
|
|
|
|
LOGE("Stream %p invalid offset=%" PRId64, this, streamOffset);
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
2009-05-18 02:15:57 +04:00
|
|
|
}
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
Result<uint32_t, nsresult> rv =
|
|
|
|
ReadBlockFromCache(lock, streamOffset, buffer);
|
2017-11-02 06:36:56 +03:00
|
|
|
if (rv.isErr()) {
|
|
|
|
return rv.unwrapErr();
|
2009-05-18 02:15:57 +04:00
|
|
|
}
|
2017-11-02 06:36:56 +03:00
|
|
|
|
2017-11-02 06:36:56 +03:00
|
|
|
uint32_t bytes = rv.unwrap();
|
|
|
|
if (bytes > 0) {
|
|
|
|
// Read data from the cache successfully. Let's try next block.
|
|
|
|
streamOffset += bytes;
|
|
|
|
buffer = buffer.From(bytes);
|
|
|
|
continue;
|
2017-11-02 06:36:56 +03:00
|
|
|
}
|
2017-11-02 06:36:56 +03:00
|
|
|
|
|
|
|
// The partial block is our last chance to get data.
|
2017-11-22 12:14:02 +03:00
|
|
|
bytes = ReadPartialBlock(lock, streamOffset, buffer);
|
2017-11-02 06:36:56 +03:00
|
|
|
if (bytes < buffer.Length()) {
|
|
|
|
// Not enough data to read.
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return for we've got all the requested bytes.
|
|
|
|
return NS_OK;
|
2009-05-18 02:15:57 +04:00
|
|
|
}
|
2010-10-26 05:21:16 +04:00
|
|
|
|
2009-05-18 02:15:57 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-06-08 06:59:43 +03:00
|
|
|
nsresult MediaCacheStream::Init(int64_t aContentLength) {
|
2009-04-01 04:52:56 +04:00
|
|
|
NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
|
2017-09-05 12:39:57 +03:00
|
|
|
MOZ_ASSERT(!mMediaCache, "Has been initialized.");
|
2009-09-15 06:30:44 +04:00
|
|
|
|
2017-06-08 06:59:43 +03:00
|
|
|
if (aContentLength > 0) {
|
2017-06-12 05:27:46 +03:00
|
|
|
uint32_t length = uint32_t(std::min(aContentLength, int64_t(UINT32_MAX)));
|
2017-11-16 07:05:19 +03:00
|
|
|
LOG("MediaCacheStream::Init(this=%p) "
|
2017-06-12 05:27:46 +03:00
|
|
|
"MEDIACACHESTREAM_NOTIFIED_LENGTH=%" PRIu32,
|
|
|
|
this, length);
|
|
|
|
|
2017-06-08 06:59:43 +03:00
|
|
|
mStreamLength = aContentLength;
|
|
|
|
}
|
|
|
|
|
2020-01-22 03:19:28 +03:00
|
|
|
mMediaCache = MediaCache::GetMediaCache(aContentLength, mIsPrivateBrowsing);
|
2017-06-08 08:30:40 +03:00
|
|
|
if (!mMediaCache) {
|
2009-04-01 04:52:56 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2017-06-08 06:09:40 +03:00
|
|
|
}
|
2017-11-22 12:14:02 +03:00
|
|
|
|
2017-12-19 12:38:40 +03:00
|
|
|
OwnerThread()->Dispatch(NS_NewRunnableFunction(
|
|
|
|
"MediaCacheStream::Init",
|
|
|
|
[this, res = RefPtr<ChannelMediaResource>(mClient)]() {
|
|
|
|
AutoLock lock(mMediaCache->Monitor());
|
|
|
|
mMediaCache->OpenStream(lock, this);
|
|
|
|
}));
|
|
|
|
|
2009-09-15 06:30:44 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-14 23:46:40 +04:00
|
|
|
void MediaCacheStream::InitAsClone(MediaCacheStream* aOriginal) {
|
2017-09-05 12:28:14 +03:00
|
|
|
MOZ_ASSERT(!mMediaCache, "Has been initialized.");
|
|
|
|
MOZ_ASSERT(aOriginal->mMediaCache, "Don't clone an uninitialized stream.");
|
2012-03-20 11:55:40 +04:00
|
|
|
|
2017-12-14 11:08:17 +03:00
|
|
|
// Use the same MediaCache as our clone.
|
|
|
|
mMediaCache = aOriginal->mMediaCache;
|
2017-12-19 12:32:55 +03:00
|
|
|
OwnerThread()->Dispatch(NS_NewRunnableFunction(
|
|
|
|
"MediaCacheStream::InitAsClone",
|
|
|
|
[this, aOriginal, r1 = RefPtr<ChannelMediaResource>(mClient),
|
|
|
|
r2 = RefPtr<ChannelMediaResource>(aOriginal->mClient)]() {
|
|
|
|
InitAsCloneInternal(aOriginal);
|
|
|
|
}));
|
|
|
|
}
|
2018-11-19 16:25:37 +03:00
|
|
|
|
2017-12-19 12:32:55 +03:00
|
|
|
void MediaCacheStream::InitAsCloneInternal(MediaCacheStream* aOriginal) {
|
|
|
|
MOZ_ASSERT(OwnerThread()->IsOnCurrentThread());
|
2018-01-05 11:31:39 +03:00
|
|
|
AutoLock lock(mMediaCache->Monitor());
|
2020-10-04 10:19:18 +03:00
|
|
|
LOG("MediaCacheStream::InitAsCloneInternal(this=%p, original=%p)", this,
|
|
|
|
aOriginal);
|
2017-11-15 10:24:26 +03:00
|
|
|
|
2018-01-05 11:31:39 +03:00
|
|
|
// Download data and notify events if necessary. Note the order is important
|
|
|
|
// in order to mimic the behavior of data being downloaded from the channel.
|
2009-09-15 06:30:44 +04:00
|
|
|
|
2018-01-05 11:31:39 +03:00
|
|
|
// Step 1: copy/download data from the original stream.
|
|
|
|
mResourceID = aOriginal->mResourceID;
|
2009-09-15 06:30:44 +04:00
|
|
|
mStreamLength = aOriginal->mStreamLength;
|
2012-11-30 17:17:54 +04:00
|
|
|
mIsTransportSeekable = aOriginal->mIsTransportSeekable;
|
2017-11-29 11:49:54 +03:00
|
|
|
mDownloadStatistics = aOriginal->mDownloadStatistics;
|
|
|
|
mDownloadStatistics.Stop();
|
2009-09-15 06:30:44 +04:00
|
|
|
|
2018-01-05 11:31:39 +03:00
|
|
|
// Grab cache blocks from aOriginal as readahead blocks for our stream
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < aOriginal->mBlocks.Length(); ++i) {
|
|
|
|
int32_t cacheBlockIndex = aOriginal->mBlocks[i];
|
2009-09-15 06:30:44 +04:00
|
|
|
if (cacheBlockIndex < 0) continue;
|
|
|
|
|
|
|
|
while (i >= mBlocks.Length()) {
|
|
|
|
mBlocks.AppendElement(-1);
|
|
|
|
}
|
2009-10-09 15:48:30 +04:00
|
|
|
// Every block is a readahead block for the clone because the clone's
|
|
|
|
// initial stream offset is zero
|
2017-11-22 12:14:02 +03:00
|
|
|
mMediaCache->AddBlockOwnerAsReadahead(lock, cacheBlockIndex, this, i);
|
2009-09-15 06:30:44 +04:00
|
|
|
}
|
2017-10-26 06:13:38 +03:00
|
|
|
|
2017-12-16 18:50:07 +03:00
|
|
|
// Copy the partial block.
|
|
|
|
mChannelOffset = aOriginal->mChannelOffset;
|
|
|
|
memcpy(mPartialBlockBuffer.get(), aOriginal->mPartialBlockBuffer.get(),
|
|
|
|
BLOCK_SIZE);
|
2017-11-15 10:14:21 +03:00
|
|
|
|
2018-01-05 11:31:39 +03:00
|
|
|
// Step 2: notify the client that we have new data so the decoder has a chance
|
|
|
|
// to compute 'canplaythrough' and buffer ranges.
|
|
|
|
mClient->CacheClientNotifyDataReceived();
|
|
|
|
|
|
|
|
// Step 3: notify download ended if necessary.
|
|
|
|
if (aOriginal->mDidNotifyDataEnded &&
|
|
|
|
NS_SUCCEEDED(aOriginal->mNotifyDataEndedStatus)) {
|
|
|
|
mNotifyDataEndedStatus = aOriginal->mNotifyDataEndedStatus;
|
|
|
|
mDidNotifyDataEnded = true;
|
|
|
|
mClient->CacheClientNotifyDataEnded(mNotifyDataEndedStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 4: notify download is suspended by the cache.
|
|
|
|
mClientSuspended = true;
|
|
|
|
mCacheSuspended = true;
|
|
|
|
mChannelEnded = true;
|
|
|
|
mClient->CacheClientSuspend();
|
Bug 1646719 - P2: Queue a suspended status check when cloning a cache-stream r=jya
- What this patch does:
Send a "download-suspended-by-cache" signal to the media-element paired
with a cloned cache-stream right after the cache-stream is cloned. That
signal help the media-element to decide if it's ready to enter the
HAVE_ENOUGH_DATA state and fire a "canplaythrough" event.
- Why this patch is needed:
It solves a WPT timeout issue. That WPT test waits a "canplaythrough"
event.
- What problem this patch solves is:
This patch addresses the problem mentioned in [1].
Each media-element pairs with a cache-stream downloading the data it
needs. When the data-download of a cache-stream gets suspended, the
media-element paired with the cache-stream should be notified. This
notification is one of the factor to move the ready-state of the
media-element to HAVE_ENOUGH_DATA. However, the media-element paired
with a cloned cache-stream may never receive this notification. And the
worst is that it may never have a chance to download enough data it
needs to move the ready-state to HAVE_ENOUGH_DATA as well at the same
time, in the case mentioned below.
This can happen when a media-element paired with a cloned cache-stream
is created after the cache is full, and all of the cache-streams are
suspended. (Cloned cache-stream is a cache-stream cloned from another
cache-stream that shares the same underlying data with it since their
paired media-elements have the same `src` (of `HTMLMediaElement`)).
"canplaythrough" event is fired when the ready-state is transited to
HAVE_ENOUGH_DATA. As a result, it will never be fired in this case.
In usual case, if the cache-stream gets suspended from non-suspended, it
will send a "download-suspended-by-cache=true" signal to its paired
media-element when running `MediaCache::Update()`. In fact, all other
cache-streams sharing the same underlying data will send this signal at
the same time if necessary. (Later, once the cache-stream is resumed
from suspended to non-suspended it will send a
"download-suspended-by-cache=false" signal to its paired media-element.
All other cache-streams sharing the same underlying data will do the
same if necessary.) The media-element keeps tracking that signal it
receives. After the first-frame of the media-element is loaded, the
ready-state of the media-element will be transited to HAVE_ENOUGH_DATA
by force if the signal is true. (Otherwise, the ready-state will be
inferred by other information.)
When cloning a cache-stream from another one, the cloned cache-stream is
suspended by default. If it's added to a jammed cache that all of the
cache-streams are suspended since the cache is full, then it never has a
chance to fire the "download-suspended-by-cache" signal. Both its
source-stream and itself have no status-change between suspended and
non-suspended, so `MediaCache::Update` is unable to send the signal.
In this case, we should force the media-element paired with the newly
cloned cache-stream transits its ready-state to HAVE_ENOUGH_DATA, which
follows the existing mechanism, by queueing a status-change-update when
cloning the stream. The status-change-update will be run in the
`MediaCache::Update` and it will check what the signal should be sent.
Once the "download-suspended-by-cache=true" is sent, the
"canplaythrough" event of the media-element can be dispatched after
its first-frame is loaded. (The event listeners can possibly make the
media-element starts playing, which is likely to cause a cache-seek that
can revitalize the cache eventually.)
[1] https://bugzilla.mozilla.org/show_bug.cgi?id=1646719#c5
Differential Revision: https://phabricator.services.mozilla.com/D92125
2020-10-10 02:07:05 +03:00
|
|
|
mMediaCache->QueueSuspendedStatusUpdate(lock, mResourceID);
|
2017-12-19 12:32:55 +03:00
|
|
|
|
2018-01-05 11:31:39 +03:00
|
|
|
// Step 5: add the stream to be managed by the cache.
|
|
|
|
mMediaCache->OpenStream(lock, this, true /* aIsClone */);
|
2017-12-19 12:32:55 +03:00
|
|
|
// Wake up the reader which is waiting for the cloned data.
|
|
|
|
lock.NotifyAll();
|
2009-04-01 04:52:56 +04:00
|
|
|
}
|
2011-03-24 01:28:58 +03:00
|
|
|
|
2019-11-14 01:40:14 +03:00
|
|
|
nsISerialEventTarget* MediaCacheStream::OwnerThread() const {
|
2017-09-13 11:51:12 +03:00
|
|
|
return mMediaCache->OwnerThread();
|
|
|
|
}
|
|
|
|
|
2015-11-24 12:16:52 +03:00
|
|
|
nsresult MediaCacheStream::GetCachedRanges(MediaByteRangeSet& aRanges) {
|
2017-11-20 11:30:05 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2011-03-24 01:28:58 +03:00
|
|
|
// Take the monitor, so that the cached data ranges can't grow while we're
|
|
|
|
// trying to loop over them.
|
2017-11-22 12:14:02 +03:00
|
|
|
AutoLock lock(mMediaCache->Monitor());
|
2011-03-24 01:28:58 +03: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");
|
|
|
|
|
2017-11-22 12:14:02 +03:00
|
|
|
int64_t startOffset = GetNextCachedDataInternal(lock, 0);
|
2011-03-24 01:28:58 +03:00
|
|
|
while (startOffset >= 0) {
|
2017-11-22 12:14:02 +03:00
|
|
|
int64_t endOffset = GetCachedDataEndInternal(lock, startOffset);
|
2011-03-24 01:28:58 +03:00
|
|
|
NS_ASSERTION(startOffset < endOffset,
|
|
|
|
"Buffered range must end after its start");
|
|
|
|
// Bytes [startOffset..endOffset] are cached.
|
2015-11-24 12:16:52 +03:00
|
|
|
aRanges += MediaByteRange(startOffset, endOffset);
|
2017-11-22 12:14:02 +03:00
|
|
|
startOffset = GetNextCachedDataInternal(lock, endOffset);
|
2011-03-24 01:28:58 +03:00
|
|
|
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 23:45:33 +04:00
|
|
|
|
2017-11-29 11:49:54 +03:00
|
|
|
double MediaCacheStream::GetDownloadRate(bool* aIsReliable) {
|
2017-12-05 10:23:43 +03:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2017-11-29 11:49:54 +03:00
|
|
|
AutoLock lock(mMediaCache->Monitor());
|
|
|
|
return mDownloadStatistics.GetRate(aIsReliable);
|
|
|
|
}
|
|
|
|
|
2019-05-27 19:15:33 +03:00
|
|
|
void MediaCacheStream::GetDebugInfo(dom::MediaCacheStreamDebugInfo& aInfo) {
|
2018-01-09 05:29:10 +03:00
|
|
|
AutoLock lock(mMediaCache->GetMonitorOnTheMainThread());
|
2019-05-27 19:15:33 +03:00
|
|
|
aInfo.mStreamLength = mStreamLength;
|
|
|
|
aInfo.mChannelOffset = mChannelOffset;
|
|
|
|
aInfo.mCacheSuspended = mCacheSuspended;
|
|
|
|
aInfo.mChannelEnded = mChannelEnded;
|
|
|
|
aInfo.mLoadID = mLoadID;
|
2017-10-25 08:57:39 +03:00
|
|
|
}
|
|
|
|
|
2012-11-14 23:45:33 +04:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2017-04-12 09:51:34 +03:00
|
|
|
// avoid redefined macro in unified build
|
|
|
|
#undef LOG
|
2017-05-11 11:49:36 +03:00
|
|
|
#undef LOGI
|