2009-04-01 04:52:56 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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
|
|
|
#ifndef MediaCache_h_
|
|
|
|
#define MediaCache_h_
|
2009-04-01 04:52:56 +04:00
|
|
|
|
Bug 1407810 - Use DDLogger in media stack - r=jwwang
Mostly-mechanical additions:
- Log constructions&destructions, usually by just inheriting from
DecoderDoctorLifeLogger, otherwise with explicit log commands (for internal
classes for which DecoderDoctorTraits can't be specialized),
- Log links between most objects, e.g.: Media element -> decoder -> state
machine -> reader -> demuxer -> resource, etc.
And logging some important properties and events (JS events, duration change,
frames being decoded, etc.)
More will be added later on, from just converting MOZ_LOGs, and as needed.
MozReview-Commit-ID: KgNhHSz35t0
--HG--
extra : rebase_source : dd7206e350e32671adc6f3b9e54ebf777251de2c
2017-10-10 09:55:27 +03:00
|
|
|
#include "DecoderDoctorLogger.h"
|
2017-06-20 18:56:55 +03:00
|
|
|
#include "Intervals.h"
|
2017-11-02 06:36:56 +03:00
|
|
|
#include "mozilla/Result.h"
|
2017-06-20 18:56:55 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2009-05-14 01:52:50 +04:00
|
|
|
#include "nsCOMPtr.h"
|
2013-09-06 00:25:17 +04:00
|
|
|
#include "nsHashKeys.h"
|
2017-06-20 18:56:55 +03:00
|
|
|
#include "nsTArray.h"
|
2013-09-06 00:25:17 +04:00
|
|
|
#include "nsTHashtable.h"
|
|
|
|
|
2017-11-29 11:49:54 +03:00
|
|
|
#include "MediaChannelStatistics.h"
|
|
|
|
|
2017-09-13 11:51:12 +03:00
|
|
|
class nsIEventTarget;
|
2013-09-06 00:25:17 +04:00
|
|
|
class nsIPrincipal;
|
2009-04-01 04:52:56 +04:00
|
|
|
|
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
|
|
|
namespace mozilla {
|
2012-02-15 08:35:01 +04:00
|
|
|
// defined in MediaResource.h
|
|
|
|
class ChannelMediaResource;
|
2015-11-24 12:16:52 +03:00
|
|
|
typedef media::IntervalSet<int64_t> MediaByteRangeSet;
|
2012-02-15 08:35:01 +04:00
|
|
|
class MediaResource;
|
2018-01-05 05:49:38 +03:00
|
|
|
class MonitorAutoLock;
|
2011-03-24 01:28:58 +03:00
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
/**
|
|
|
|
* Media applications want fast, "on demand" random access to media data,
|
|
|
|
* for pausing, seeking, etc. But we are primarily interested
|
|
|
|
* in transporting media data using HTTP over the Internet, which has
|
|
|
|
* high latency to open a connection, requires a new connection for every
|
|
|
|
* seek, may not even support seeking on some connections (especially
|
|
|
|
* live streams), and uses a push model --- data comes from the server
|
|
|
|
* and you don't have much control over the rate. Also, transferring data
|
|
|
|
* over the Internet can be slow and/or unpredictable, so we want to read
|
|
|
|
* ahead to buffer and cache as much data as possible.
|
2014-10-13 02:53:42 +04:00
|
|
|
*
|
2009-04-01 04:52:56 +04:00
|
|
|
* The job of the media cache is to resolve this impedance mismatch.
|
|
|
|
* The media cache reads data from Necko channels into file-backed storage,
|
|
|
|
* and offers a random-access file-like API to the stream data
|
2012-11-14 23:46:40 +04:00
|
|
|
* (MediaCacheStream). Along the way it solves several problems:
|
2009-04-01 04:52:56 +04:00
|
|
|
* -- The cache intelligently reads ahead to prefetch data that may be
|
|
|
|
* needed in the future
|
|
|
|
* -- The size of the cache is bounded so that we don't fill up
|
|
|
|
* storage with read-ahead data
|
|
|
|
* -- Cache replacement is managed globally so that the most valuable
|
|
|
|
* data (across all streams) is retained
|
|
|
|
* -- The cache can suspend Necko channels temporarily when their data is
|
|
|
|
* not wanted (yet)
|
|
|
|
* -- The cache translates file-like seek requests to HTTP seeks,
|
|
|
|
* including optimizations like not triggering a new seek if it would
|
|
|
|
* be faster to just keep reading until we reach the seek point. The
|
|
|
|
* "seek to EOF" idiom to determine file size is also handled efficiently
|
|
|
|
* (seeking to EOF and then seeking back to the previous offset does not
|
|
|
|
* trigger any Necko activity)
|
|
|
|
* -- The cache also handles the case where the server does not support
|
|
|
|
* seeking
|
2012-11-14 23:46:40 +04:00
|
|
|
* -- Necko can only send data to the main thread, but MediaCacheStream
|
2009-04-01 04:52:56 +04:00
|
|
|
* can distribute data to any thread
|
|
|
|
* -- The cache exposes APIs so clients can detect what data is
|
|
|
|
* currently held
|
2014-10-13 02:53:42 +04:00
|
|
|
*
|
2009-04-01 04:52:56 +04:00
|
|
|
* Note that although HTTP is the most important transport and we only
|
|
|
|
* support transport-level seeking via HTTP byte-ranges, the media cache
|
|
|
|
* works with any kind of Necko channels and provides random access to
|
|
|
|
* cached data even for, e.g., FTP streams.
|
2014-10-13 02:53:42 +04:00
|
|
|
*
|
2009-04-01 04:52:56 +04:00
|
|
|
* The media cache is not persistent. It does not currently allow
|
|
|
|
* data from one load to be used by other loads, either within the same
|
|
|
|
* browser session or across browser sessions. The media cache file
|
|
|
|
* is marked "delete on close" so it will automatically disappear in the
|
|
|
|
* event of a browser crash or shutdown.
|
2014-10-13 02:53:42 +04:00
|
|
|
*
|
2009-04-01 04:52:56 +04:00
|
|
|
* The media cache is block-based. Streams are divided into blocks of a
|
|
|
|
* fixed size (currently 4K) and we cache blocks. A single cache contains
|
|
|
|
* blocks for all streams.
|
2014-10-13 02:53:42 +04:00
|
|
|
*
|
2009-04-01 04:52:56 +04:00
|
|
|
* The cache size is controlled by the media.cache_size preference
|
2010-06-16 03:12:38 +04:00
|
|
|
* (which is in KB). The default size is 500MB.
|
2014-10-13 02:53:42 +04:00
|
|
|
*
|
2009-04-01 04:52:56 +04:00
|
|
|
* The replacement policy predicts a "time of next use" for each block
|
|
|
|
* in the cache. When we need to free a block, the block with the latest
|
|
|
|
* "time of next use" will be evicted. Blocks are divided into
|
|
|
|
* different classes, each class having its own predictor:
|
|
|
|
* FREE_BLOCK: these blocks are effectively infinitely far in the future;
|
|
|
|
* a free block will always be chosen for replacement before other classes
|
|
|
|
* of blocks.
|
|
|
|
* METADATA_BLOCK: these are blocks that contain data that has been read
|
|
|
|
* by the decoder in "metadata mode", e.g. while the decoder is searching
|
|
|
|
* the stream during a seek operation. These blocks are managed with an
|
|
|
|
* LRU policy; the "time of next use" is predicted to be as far in the
|
|
|
|
* future as the last use was in the past.
|
|
|
|
* PLAYED_BLOCK: these are blocks that have not been read in "metadata
|
|
|
|
* mode", and contain data behind the current decoder read point. (They
|
|
|
|
* may not actually have been read by the decoder, if the decoder seeked
|
|
|
|
* forward.) These blocks are managed with an LRU policy except that we add
|
|
|
|
* REPLAY_DELAY seconds of penalty to their predicted "time of next use",
|
|
|
|
* to reflect the uncertainty about whether replay will actually happen
|
|
|
|
* or not.
|
|
|
|
* READAHEAD_BLOCK: these are blocks that have not been read in
|
|
|
|
* "metadata mode" and that are entirely ahead of the current decoder
|
|
|
|
* read point. (They may actually have been read by the decoder in the
|
|
|
|
* past if the decoder has since seeked backward.) We predict the
|
|
|
|
* time of next use for these blocks by assuming steady playback and
|
|
|
|
* dividing the number of bytes between the block and the current decoder
|
|
|
|
* read point by the decoder's estimate of its playback rate in bytes
|
|
|
|
* per second. This ensures that the blocks farthest ahead are considered
|
|
|
|
* least valuable.
|
|
|
|
* For efficient prediction of the "latest time of next use", we maintain
|
|
|
|
* linked lists of blocks in each class, ordering blocks by time of
|
|
|
|
* next use. READAHEAD_BLOCKS have one linked list per stream, since their
|
|
|
|
* time of next use depends on stream parameters, but the other lists
|
|
|
|
* are global.
|
2014-10-13 02:53:42 +04:00
|
|
|
*
|
2009-04-01 04:52:56 +04:00
|
|
|
* A block containing a current decoder read point can contain data
|
|
|
|
* both behind and ahead of the read point. It will be classified as a
|
|
|
|
* PLAYED_BLOCK but we will give it special treatment so it is never
|
|
|
|
* evicted --- it actually contains the highest-priority readahead data
|
|
|
|
* as well as played data.
|
2014-10-13 02:53:42 +04:00
|
|
|
*
|
2009-04-01 04:52:56 +04:00
|
|
|
* "Time of next use" estimates are also used for flow control. When
|
|
|
|
* reading ahead we can predict the time of next use for the data that
|
|
|
|
* will be read. If the predicted time of next use is later then the
|
|
|
|
* prediction for all currently cached blocks, and the cache is full, then
|
|
|
|
* we should suspend reading from the Necko channel.
|
2014-10-13 02:53:42 +04:00
|
|
|
*
|
2009-04-01 04:52:56 +04:00
|
|
|
* Unfortunately suspending the Necko channel can't immediately stop the
|
|
|
|
* flow of data from the server. First our desire to suspend has to be
|
|
|
|
* transmitted to the server (in practice, Necko stops reading from the
|
|
|
|
* socket, which causes the kernel to shrink its advertised TCP receive
|
|
|
|
* window size to zero). Then the server can stop sending the data, but
|
|
|
|
* we will receive data roughly corresponding to the product of the link
|
|
|
|
* bandwidth multiplied by the round-trip latency. We deal with this by
|
|
|
|
* letting the cache overflow temporarily and then trimming it back by
|
|
|
|
* moving overflowing blocks back into the body of the cache, replacing
|
|
|
|
* less valuable blocks as they become available. We try to avoid simply
|
|
|
|
* discarding overflowing readahead data.
|
2014-10-13 02:53:42 +04:00
|
|
|
*
|
2009-04-01 04:52:56 +04:00
|
|
|
* All changes to the actual contents of the cache happen on the main
|
|
|
|
* thread, since that's where Necko's notifications happen.
|
|
|
|
*
|
|
|
|
* The media cache maintains at most one Necko channel for each stream.
|
|
|
|
* (In the future it might be advantageous to relax this, e.g. so that a
|
|
|
|
* seek to near the end of the file can happen without disturbing
|
|
|
|
* the loading of data from the beginning of the file.) The Necko channel
|
2012-11-14 23:46:40 +04:00
|
|
|
* is managed through ChannelMediaResource; MediaCache does not
|
2009-04-01 04:52:56 +04:00
|
|
|
* depend on Necko directly.
|
2014-10-13 02:53:42 +04:00
|
|
|
*
|
2009-04-01 04:52:56 +04:00
|
|
|
* Every time something changes that might affect whether we want to
|
|
|
|
* read from a Necko channel, or whether we want to seek on the Necko
|
|
|
|
* channel --- such as data arriving or data being consumed by the
|
2012-11-14 23:46:40 +04:00
|
|
|
* decoder --- we asynchronously trigger MediaCache::Update on the main
|
2009-04-01 04:52:56 +04:00
|
|
|
* thread. That method implements most cache policy. It evaluates for
|
|
|
|
* each stream whether we want to suspend or resume the stream and what
|
|
|
|
* offset we should seek to, if any. It is also responsible for trimming
|
|
|
|
* back the cache size to its desired limit by moving overflowing blocks
|
|
|
|
* into the main part of the cache.
|
2014-10-13 02:53:42 +04:00
|
|
|
*
|
2009-04-01 04:52:56 +04:00
|
|
|
* Streams can be opened in non-seekable mode. In non-seekable mode,
|
2012-02-15 08:35:01 +04:00
|
|
|
* the cache will only call ChannelMediaResource::CacheClientSeek with
|
2009-04-01 04:52:56 +04:00
|
|
|
* a 0 offset. The cache tries hard not to discard readahead data
|
|
|
|
* for non-seekable streams, since that could trigger a potentially
|
|
|
|
* disastrous re-read of the entire stream. It's up to cache clients
|
|
|
|
* to try to avoid requesting seeks on such streams.
|
2014-10-13 02:53:42 +04:00
|
|
|
*
|
2012-11-14 23:46:40 +04:00
|
|
|
* MediaCache has a single internal monitor for all synchronization.
|
2009-04-01 04:52:56 +04:00
|
|
|
* This is treated as the lowest level monitor in the media code. So,
|
2012-11-14 23:46:40 +04:00
|
|
|
* we must not acquire any MediaDecoder locks or MediaResource locks
|
|
|
|
* while holding the MediaCache lock. But it's OK to hold those locks
|
|
|
|
* and then get the MediaCache lock.
|
2014-10-13 02:53:42 +04:00
|
|
|
*
|
2012-11-14 23:46:40 +04:00
|
|
|
* MediaCache associates a principal with each stream. CacheClientSeek
|
2009-05-14 01:52:50 +04:00
|
|
|
* can trigger new HTTP requests; due to redirects to other domains,
|
|
|
|
* each HTTP load can return data with a different principal. This
|
2012-11-14 23:46:40 +04:00
|
|
|
* principal must be passed to NotifyDataReceived, and MediaCache
|
2009-05-14 01:52:50 +04:00
|
|
|
* will detect when different principals are associated with data in the
|
|
|
|
* same stream, and replace them with a null principal.
|
2009-04-01 04:52:56 +04:00
|
|
|
*/
|
2012-11-14 23:46:40 +04:00
|
|
|
class MediaCache;
|
2009-04-01 04:52:56 +04:00
|
|
|
|
Bug 1407810 - Use DDLogger in media stack - r=jwwang
Mostly-mechanical additions:
- Log constructions&destructions, usually by just inheriting from
DecoderDoctorLifeLogger, otherwise with explicit log commands (for internal
classes for which DecoderDoctorTraits can't be specialized),
- Log links between most objects, e.g.: Media element -> decoder -> state
machine -> reader -> demuxer -> resource, etc.
And logging some important properties and events (JS events, duration change,
frames being decoded, etc.)
More will be added later on, from just converting MOZ_LOGs, and as needed.
MozReview-Commit-ID: KgNhHSz35t0
--HG--
extra : rebase_source : dd7206e350e32671adc6f3b9e54ebf777251de2c
2017-10-10 09:55:27 +03:00
|
|
|
DDLoggedTypeDeclName(MediaCacheStream);
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
/**
|
|
|
|
* If the cache fails to initialize then Init will fail, so nonstatic
|
|
|
|
* methods of this class can assume gMediaCache is non-null.
|
2014-10-13 02:53:42 +04:00
|
|
|
*
|
2009-04-01 04:52:56 +04:00
|
|
|
* This class can be directly embedded as a value.
|
|
|
|
*/
|
Bug 1407810 - Use DDLogger in media stack - r=jwwang
Mostly-mechanical additions:
- Log constructions&destructions, usually by just inheriting from
DecoderDoctorLifeLogger, otherwise with explicit log commands (for internal
classes for which DecoderDoctorTraits can't be specialized),
- Log links between most objects, e.g.: Media element -> decoder -> state
machine -> reader -> demuxer -> resource, etc.
And logging some important properties and events (JS events, duration change,
frames being decoded, etc.)
More will be added later on, from just converting MOZ_LOGs, and as needed.
MozReview-Commit-ID: KgNhHSz35t0
--HG--
extra : rebase_source : dd7206e350e32671adc6f3b9e54ebf777251de2c
2017-10-10 09:55:27 +03:00
|
|
|
class MediaCacheStream : public DecoderDoctorLifeLogger<MediaCacheStream>
|
|
|
|
{
|
2018-01-05 05:49:38 +03:00
|
|
|
using AutoLock = MonitorAutoLock;
|
2017-11-22 12:14:02 +03:00
|
|
|
|
2012-02-15 08:35:01 +04:00
|
|
|
public:
|
2015-10-12 02:01:26 +03:00
|
|
|
// This needs to be a power of two
|
|
|
|
static const int64_t BLOCK_SIZE = 32768;
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
enum ReadMode {
|
|
|
|
MODE_METADATA,
|
|
|
|
MODE_PLAYBACK
|
|
|
|
};
|
|
|
|
|
|
|
|
// aClient provides the underlying transport that cache will use to read
|
|
|
|
// data for this stream.
|
2017-04-06 11:20:00 +03:00
|
|
|
MediaCacheStream(ChannelMediaResource* aClient, bool aIsPrivateBrowsing);
|
2012-11-14 23:46:40 +04:00
|
|
|
~MediaCacheStream();
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2017-06-08 06:59:43 +03:00
|
|
|
// Set up this stream with the cache. Can fail on OOM.
|
|
|
|
// aContentLength is the content length if known, otherwise -1.
|
|
|
|
// Exactly one of InitAsClone or Init must be called before any other method
|
|
|
|
// on this class. Does nothing if already initialized.
|
|
|
|
nsresult Init(int64_t aContentLength);
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2009-09-15 06:30:44 +04:00
|
|
|
// Set up this stream with the cache, assuming it's for the same data
|
2017-09-05 12:28:14 +03:00
|
|
|
// as the aOriginal stream.
|
2017-06-08 06:59:43 +03:00
|
|
|
// Exactly one of InitAsClone or Init must be called before any other method
|
2017-09-05 12:28:14 +03:00
|
|
|
// on this class.
|
2017-12-16 18:50:07 +03:00
|
|
|
void InitAsClone(MediaCacheStream* aOriginal);
|
2009-09-15 06:30:44 +04:00
|
|
|
|
2017-09-13 11:51:12 +03:00
|
|
|
nsIEventTarget* OwnerThread() const;
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
// These are called on the main thread.
|
2012-02-15 08:35:01 +04:00
|
|
|
// This must be called (and return) before the ChannelMediaResource
|
2012-11-14 23:46:40 +04:00
|
|
|
// used to create this MediaCacheStream is deleted.
|
2009-04-01 04:52:56 +04:00
|
|
|
void Close();
|
2017-09-08 12:46:56 +03:00
|
|
|
// This returns true when the stream has been closed.
|
2017-12-14 11:22:53 +03:00
|
|
|
bool IsClosed(AutoLock&) const { return mClosed; }
|
2017-09-08 12:46:56 +03:00
|
|
|
// Returns true when this stream is can be shared by a new resource load.
|
|
|
|
// Called on the main thread only.
|
2017-12-14 11:22:53 +03:00
|
|
|
bool IsAvailableForSharing() const { return !mIsPrivateBrowsing; }
|
2009-04-01 04:52:56 +04:00
|
|
|
|
|
|
|
// These callbacks are called on the main thread by the client
|
|
|
|
// when data has been received via the channel.
|
2017-11-16 07:05:19 +03:00
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
// Notifies the cache that a load has begun. We pass the offset
|
|
|
|
// because in some cases the offset might not be what the cache
|
|
|
|
// requested. In particular we might unexpectedly start providing
|
|
|
|
// data at offset 0. This need not be called if the offset is the
|
|
|
|
// offset that the cache requested in
|
2012-02-15 08:35:01 +04:00
|
|
|
// ChannelMediaResource::CacheClientSeek. This can be called at any
|
2009-05-18 06:02:20 +04:00
|
|
|
// time by the client, not just after a CacheClientSeek.
|
2017-11-16 07:05:19 +03:00
|
|
|
//
|
2017-10-23 12:00:57 +03:00
|
|
|
// aSeekable tells us whether the stream is seekable or not. Non-seekable
|
|
|
|
// streams will always pass 0 for aOffset to CacheClientSeek. This should only
|
|
|
|
// be called while the stream is at channel offset 0. Seekability can
|
|
|
|
// change during the lifetime of the MediaCacheStream --- every time
|
|
|
|
// we do an HTTP load the seekability may be different (and sometimes
|
|
|
|
// is, in practice, due to the effects of caching proxies).
|
2017-11-16 07:05:19 +03:00
|
|
|
//
|
|
|
|
// aLength tells the cache what the server said the data length is going to
|
|
|
|
// be. The actual data length may be greater (we receive more data than
|
|
|
|
// specified) or smaller (the stream ends before we reach the given
|
|
|
|
// length), because servers can lie. The server's reported data length
|
|
|
|
// *and* the actual data length can even vary over time because a
|
|
|
|
// misbehaving server may feed us a different stream after each seek
|
|
|
|
// operation. So this is really just a hint. The cache may however
|
|
|
|
// stop reading (suspend the channel) when it thinks we've read all the
|
|
|
|
// data available based on an incorrect reported length. Seeks relative
|
|
|
|
// EOF also depend on the reported length if we haven't managed to
|
|
|
|
// read the whole stream yet.
|
|
|
|
void NotifyDataStarted(uint32_t aLoadID,
|
|
|
|
int64_t aOffset,
|
|
|
|
bool aSeekable,
|
|
|
|
int64_t aLength);
|
2009-04-01 04:52:56 +04:00
|
|
|
// Notifies the cache that data has been received. The stream already
|
|
|
|
// knows the offset because data is received in sequence and
|
|
|
|
// the starting offset is known via NotifyDataStarted or because
|
|
|
|
// the cache requested the offset in
|
2012-02-15 08:35:01 +04:00
|
|
|
// ChannelMediaResource::CacheClientSeek, or because it defaulted to 0.
|
2017-11-01 18:20:11 +03:00
|
|
|
void NotifyDataReceived(uint32_t aLoadID,
|
|
|
|
uint32_t aCount,
|
|
|
|
const uint8_t* aData);
|
2017-11-10 10:06:39 +03:00
|
|
|
|
|
|
|
// Set the load ID so the following NotifyDataEnded() call can work properly.
|
|
|
|
// Used in some rare cases where NotifyDataEnded() is called without the
|
|
|
|
// preceding NotifyDataStarted().
|
|
|
|
void NotifyLoadID(uint32_t aLoadID);
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
// Notifies the cache that the channel has closed with the given status.
|
2017-11-10 10:06:39 +03:00
|
|
|
void NotifyDataEnded(uint32_t aLoadID,
|
|
|
|
nsresult aStatus,
|
|
|
|
bool aReopenOnError = false);
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2017-10-24 06:25:41 +03:00
|
|
|
// Notifies the stream that the suspend status of the client has changed.
|
|
|
|
// Main thread only.
|
|
|
|
void NotifyClientSuspended(bool aSuspended);
|
|
|
|
|
2017-12-07 10:43:36 +03:00
|
|
|
// Notifies the stream to resume download at the current offset.
|
|
|
|
void NotifyResume();
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
// These methods can be called on any thread.
|
|
|
|
// Cached blocks associated with this stream will not be evicted
|
|
|
|
// while the stream is pinned.
|
|
|
|
void Pin();
|
|
|
|
void Unpin();
|
2017-11-16 07:05:19 +03:00
|
|
|
// See comments above for NotifyDataStarted about how the length
|
2009-04-01 04:52:56 +04:00
|
|
|
// can vary over time. Returns -1 if no length is known. Returns the
|
|
|
|
// reported length if we haven't got any better information. If
|
|
|
|
// the stream ended normally we return the length we actually got.
|
|
|
|
// If we've successfully read data beyond the originally reported length,
|
|
|
|
// we return the end of the data we've read.
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t GetLength();
|
2012-04-30 07:12:42 +04:00
|
|
|
// Returns the unique resource ID. Call only on the main thread or while
|
|
|
|
// holding the media cache lock.
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t GetResourceID() { return mResourceID; }
|
2009-04-01 04:52:56 +04:00
|
|
|
// Returns the end of the bytes starting at the given offset
|
|
|
|
// which are in cache.
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t GetCachedDataEnd(int64_t aOffset);
|
2009-05-18 02:15:57 +04:00
|
|
|
// Returns the offset of the first byte of cached data at or after aOffset,
|
|
|
|
// or -1 if there is no such cached data.
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t GetNextCachedData(int64_t aOffset);
|
2011-03-24 01:28:58 +03:00
|
|
|
// Fills aRanges with the ByteRanges representing the data which is currently
|
|
|
|
// cached. Locks the media cache while running, to prevent any ranges
|
|
|
|
// growing. The stream should be pinned while this runs and while its results
|
|
|
|
// are used, to ensure no data is evicted.
|
2015-11-24 12:16:52 +03:00
|
|
|
nsresult GetCachedRanges(MediaByteRangeSet& aRanges);
|
2009-05-18 02:15:57 +04:00
|
|
|
|
2017-11-29 11:49:54 +03:00
|
|
|
double GetDownloadRate(bool* aIsReliable);
|
|
|
|
|
2009-05-18 02:15:57 +04:00
|
|
|
// Reads from buffered data only. Will fail if not all data to be read is
|
|
|
|
// in the cache. Will not mark blocks as read. Can be called from the main
|
|
|
|
// thread. It's the caller's responsibility to wrap the call in a pin/unpin,
|
|
|
|
// and also to check that the range they want is cached before calling this.
|
2017-11-02 06:36:56 +03:00
|
|
|
nsresult ReadFromCache(char* aBuffer, int64_t aOffset, uint32_t aCount);
|
2009-05-18 02:15:57 +04:00
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
// IsDataCachedToEndOfStream returns true if all the data from
|
|
|
|
// aOffset to the end of the stream (the server-reported end, if the
|
|
|
|
// real end is not known) is in cache. If we know nothing about the
|
|
|
|
// end of the stream, this returns false.
|
2012-08-22 19:56:38 +04:00
|
|
|
bool IsDataCachedToEndOfStream(int64_t aOffset);
|
2009-04-01 04:52:56 +04:00
|
|
|
// The mode is initially MODE_PLAYBACK.
|
|
|
|
void SetReadMode(ReadMode aMode);
|
|
|
|
// This is the client's estimate of the playback rate assuming
|
|
|
|
// the media plays continuously. The cache can't guess this itself
|
|
|
|
// because it doesn't know when the decoder was paused, buffering, etc.
|
|
|
|
// Do not pass zero.
|
2012-08-22 19:56:38 +04:00
|
|
|
void SetPlaybackRate(uint32_t aBytesPerSecond);
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2011-12-01 03:09:10 +04:00
|
|
|
// Returns true when all streams for this resource are suspended or their
|
|
|
|
// channel has ended.
|
2017-11-22 12:14:02 +03:00
|
|
|
bool AreAllStreamsForResourceSuspended(AutoLock&);
|
2011-11-30 09:05:49 +04:00
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
// These methods must be called on a different thread from the main
|
|
|
|
// thread. They should always be called on the same thread for a given
|
|
|
|
// stream.
|
|
|
|
// *aBytes gets the number of bytes that were actually read. This can
|
|
|
|
// be less than aCount. If the first byte of data is not in the cache,
|
|
|
|
// this will block until the data is available or the stream is
|
|
|
|
// closed, otherwise it won't block.
|
2018-01-05 05:49:38 +03:00
|
|
|
nsresult Read(AutoLock&, char* aBuffer, uint32_t aCount, uint32_t* aBytes);
|
2017-06-23 04:18:07 +03:00
|
|
|
// Seeks to aOffset in the stream then performs a Read operation. See
|
|
|
|
// 'Read' for argument and return details.
|
2013-07-17 05:54:52 +04:00
|
|
|
nsresult ReadAt(int64_t aOffset, char* aBuffer,
|
|
|
|
uint32_t aCount, uint32_t* aBytes);
|
2014-03-06 01:31:04 +04:00
|
|
|
|
2017-05-11 11:49:36 +03:00
|
|
|
void ThrottleReadahead(bool bThrottle);
|
|
|
|
|
2014-03-06 01:31:04 +04:00
|
|
|
size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const;
|
|
|
|
|
2017-10-25 08:57:39 +03:00
|
|
|
nsCString GetDebugInfo();
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
private:
|
2012-11-14 23:46:40 +04:00
|
|
|
friend class MediaCache;
|
2009-04-01 04:52:56 +04:00
|
|
|
|
|
|
|
/**
|
2009-09-15 06:30:44 +04:00
|
|
|
* A doubly-linked list of blocks. Add/Remove/Get methods are all
|
|
|
|
* constant time. We declare this here so that a stream can contain a
|
|
|
|
* BlockList of its read-ahead blocks. Blocks are referred to by index
|
2012-11-14 23:46:40 +04:00
|
|
|
* into the MediaCache::mIndex array.
|
2014-10-13 02:53:42 +04:00
|
|
|
*
|
2009-09-15 06:30:44 +04:00
|
|
|
* Blocks can belong to more than one list at the same time, because
|
|
|
|
* the next/prev pointers are not stored in the block.
|
2009-04-01 04:52:56 +04:00
|
|
|
*/
|
|
|
|
class BlockList {
|
|
|
|
public:
|
2013-09-02 12:41:57 +04:00
|
|
|
BlockList() : mFirstBlock(-1), mCount(0) {}
|
2009-04-01 04:52:56 +04:00
|
|
|
~BlockList() {
|
|
|
|
NS_ASSERTION(mFirstBlock == -1 && mCount == 0,
|
|
|
|
"Destroying non-empty block list");
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
void AddFirstBlock(int32_t aBlock);
|
|
|
|
void AddAfter(int32_t aBlock, int32_t aBefore);
|
|
|
|
void RemoveBlock(int32_t aBlock);
|
2009-04-01 04:52:56 +04:00
|
|
|
// Returns the first block in the list, or -1 if empty
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t GetFirstBlock() const { return mFirstBlock; }
|
2009-04-01 04:52:56 +04:00
|
|
|
// Returns the last block in the list, or -1 if empty
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t GetLastBlock() const;
|
2009-09-15 06:30:44 +04:00
|
|
|
// Returns the next block in the list after aBlock or -1 if
|
|
|
|
// aBlock is the last block
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t GetNextBlock(int32_t aBlock) const;
|
2009-09-15 06:30:44 +04:00
|
|
|
// Returns the previous block in the list before aBlock or -1 if
|
|
|
|
// aBlock is the first block
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t GetPrevBlock(int32_t aBlock) const;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool IsEmpty() const { return mFirstBlock < 0; }
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t GetCount() const { return mCount; }
|
2009-09-15 06:30:44 +04:00
|
|
|
// The contents of aBlockIndex1 and aBlockIndex2 have been swapped
|
2012-08-22 19:56:38 +04:00
|
|
|
void NotifyBlockSwapped(int32_t aBlockIndex1, int32_t aBlockIndex2);
|
2009-04-01 04:52:56 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
// Verify linked-list invariants
|
|
|
|
void Verify();
|
|
|
|
#else
|
|
|
|
void Verify() {}
|
|
|
|
#endif
|
|
|
|
|
2014-03-06 01:31:04 +04:00
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
private:
|
2009-09-15 06:30:44 +04:00
|
|
|
struct Entry : public nsUint32HashKey {
|
2014-09-01 07:50:23 +04:00
|
|
|
explicit Entry(KeyTypePointer aKey) : nsUint32HashKey(aKey) { }
|
2009-09-15 06:30:44 +04:00
|
|
|
Entry(const Entry& toCopy) : nsUint32HashKey(&toCopy.GetKey()),
|
|
|
|
mNextBlock(toCopy.mNextBlock), mPrevBlock(toCopy.mPrevBlock) {}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t mNextBlock;
|
|
|
|
int32_t mPrevBlock;
|
2009-09-15 06:30:44 +04:00
|
|
|
};
|
|
|
|
nsTHashtable<Entry> mEntries;
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
// The index of the first block in the list, or -1 if the list is empty.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t mFirstBlock;
|
2009-04-01 04:52:56 +04:00
|
|
|
// The number of blocks in the list.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t mCount;
|
2009-04-01 04:52:56 +04:00
|
|
|
};
|
|
|
|
|
2017-11-02 06:36:56 +03:00
|
|
|
// Read data from the partial block and return the number of bytes read
|
|
|
|
// successfully. 0 if aOffset is not an offset in the partial block or there
|
|
|
|
// is nothing to read.
|
2017-11-22 12:14:02 +03:00
|
|
|
uint32_t ReadPartialBlock(AutoLock&, int64_t aOffset, Span<char> aBuffer);
|
2017-11-02 06:36:56 +03:00
|
|
|
|
|
|
|
// Read data from the cache block specified by aOffset. Return the number of
|
|
|
|
// bytes read successfully or an error code if any failure.
|
2017-11-22 12:14:02 +03:00
|
|
|
Result<uint32_t, nsresult> ReadBlockFromCache(AutoLock&,
|
|
|
|
int64_t aOffset,
|
2017-11-03 11:56:58 +03:00
|
|
|
Span<char> aBuffer,
|
|
|
|
bool aNoteBlockUsage = false);
|
2017-11-02 06:36:56 +03:00
|
|
|
|
2017-11-02 09:46:24 +03:00
|
|
|
// Non-main thread only.
|
2017-11-22 12:14:02 +03:00
|
|
|
nsresult Seek(AutoLock&, int64_t aOffset);
|
2017-11-02 09:46:24 +03:00
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
// Returns the end of the bytes starting at the given offset
|
|
|
|
// which are in cache.
|
|
|
|
// This method assumes that the cache monitor is held and can be called on
|
|
|
|
// any thread.
|
2017-11-22 12:14:02 +03:00
|
|
|
int64_t GetCachedDataEndInternal(AutoLock&, int64_t aOffset);
|
2009-05-18 02:15:57 +04:00
|
|
|
// Returns the offset of the first byte of cached data at or after aOffset,
|
|
|
|
// or -1 if there is no such cached data.
|
|
|
|
// This method assumes that the cache monitor is held and can be called on
|
|
|
|
// any thread.
|
2017-11-22 12:14:02 +03:00
|
|
|
int64_t GetNextCachedDataInternal(AutoLock&, int64_t aOffset);
|
2017-11-20 11:00:22 +03:00
|
|
|
// Used by |NotifyDataEnded| to write |mPartialBlock| to disk.
|
2012-12-07 03:27:08 +04:00
|
|
|
// If |aNotifyAll| is true, this function will wake up readers who may be
|
|
|
|
// waiting on the media cache monitor. Called on the main thread only.
|
2017-11-22 12:14:02 +03:00
|
|
|
void FlushPartialBlockInternal(AutoLock&, bool aNotify);
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2017-11-16 10:41:36 +03:00
|
|
|
void NotifyDataStartedInternal(uint32_t aLoadID,
|
|
|
|
int64_t aOffset,
|
|
|
|
bool aSeekable,
|
|
|
|
int64_t aLength);
|
|
|
|
|
2017-11-10 10:06:39 +03:00
|
|
|
void NotifyDataEndedInternal(uint32_t aLoadID,
|
|
|
|
nsresult aStatus,
|
|
|
|
bool aReopenOnError);
|
|
|
|
|
2017-11-29 11:49:54 +03:00
|
|
|
void UpdateDownloadStatistics(AutoLock&);
|
|
|
|
|
2017-12-15 05:29:29 +03:00
|
|
|
void CloseInternal(AutoLock&);
|
2017-12-19 12:32:55 +03:00
|
|
|
void InitAsCloneInternal(MediaCacheStream* aOriginal);
|
2017-12-15 05:29:29 +03:00
|
|
|
|
2017-06-08 08:30:40 +03:00
|
|
|
// Instance of MediaCache to use with this MediaCacheStream.
|
2017-06-15 06:02:25 +03:00
|
|
|
RefPtr<MediaCache> mMediaCache;
|
2017-06-08 08:30:40 +03:00
|
|
|
|
2017-09-20 11:43:19 +03:00
|
|
|
ChannelMediaResource* const mClient;
|
|
|
|
|
2012-04-30 07:12:42 +04:00
|
|
|
// The following fields must be written holding the cache's monitor and
|
|
|
|
// only on the main thread, thus can be read either on the main thread
|
|
|
|
// or while holding the cache's monitor.
|
2009-04-01 04:52:56 +04:00
|
|
|
|
2017-09-08 12:46:56 +03:00
|
|
|
// Set to true when the stream has been closed either explicitly or
|
|
|
|
// due to an internal cache error
|
|
|
|
bool mClosed = false;
|
2012-04-30 07:12:42 +04:00
|
|
|
// This is a unique ID representing the resource we're loading.
|
|
|
|
// All streams with the same mResourceID are loading the same
|
|
|
|
// underlying resource and should share data.
|
2017-10-26 06:13:38 +03:00
|
|
|
// Initialized to 0 as invalid. Will be allocated a valid ID (always positive)
|
|
|
|
// from the cache.
|
|
|
|
int64_t mResourceID = 0;
|
2009-10-09 15:46:23 +04:00
|
|
|
// The last reported seekability state for the underlying channel
|
2012-11-30 17:17:54 +04:00
|
|
|
bool mIsTransportSeekable;
|
2011-09-30 03:34:37 +04:00
|
|
|
// True if the cache has suspended our channel because the cache is
|
2009-10-09 15:46:23 +04:00
|
|
|
// full and the priority of the data that would be received is lower
|
|
|
|
// than the priority of the data already in the cache
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mCacheSuspended;
|
2011-12-01 03:09:10 +04:00
|
|
|
// True if the channel ended and we haven't seeked it again.
|
|
|
|
bool mChannelEnded;
|
2009-10-09 15:46:23 +04:00
|
|
|
|
2017-09-20 11:43:19 +03:00
|
|
|
// The following fields are protected by the cache's monitor and can be written
|
2009-10-09 15:46:23 +04:00
|
|
|
// by any thread.
|
|
|
|
|
2017-09-20 11:29:46 +03:00
|
|
|
// The reported or discovered length of the data, or -1 if nothing is known
|
|
|
|
int64_t mStreamLength = -1;
|
2017-09-20 11:23:21 +03:00
|
|
|
// The offset where the next data from the channel will arrive
|
|
|
|
int64_t mChannelOffset = 0;
|
2009-10-09 15:46:23 +04:00
|
|
|
// The offset where the reader is positioned in the stream
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t mStreamOffset;
|
2009-04-01 04:52:56 +04:00
|
|
|
// For each block in the stream data, maps to the cache entry for the
|
|
|
|
// block, or -1 if the block is not cached.
|
2012-08-22 19:56:38 +04:00
|
|
|
nsTArray<int32_t> mBlocks;
|
2009-04-01 04:52:56 +04:00
|
|
|
// The list of read-ahead blocks, ordered by stream offset; the first
|
|
|
|
// block is the earliest in the stream (so the last block will be the
|
|
|
|
// least valuable).
|
|
|
|
BlockList mReadaheadBlocks;
|
2009-09-15 06:30:44 +04:00
|
|
|
// The list of metadata blocks; the first block is the most recently used
|
|
|
|
BlockList mMetadataBlocks;
|
|
|
|
// The list of played-back blocks; the first block is the most recently used
|
|
|
|
BlockList mPlayedBlocks;
|
2009-04-01 04:52:56 +04:00
|
|
|
// The last reported estimate of the decoder's playback rate
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mPlaybackBytesPerSecond;
|
2009-04-01 04:52:56 +04:00
|
|
|
// The number of times this stream has been Pinned without a
|
|
|
|
// corresponding Unpin
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mPinCount;
|
2017-11-15 10:24:26 +03:00
|
|
|
// True if CacheClientNotifyDataEnded has been called for this stream.
|
|
|
|
bool mDidNotifyDataEnded = false;
|
2012-03-20 11:55:40 +04:00
|
|
|
// The status used when we did CacheClientNotifyDataEnded. Only valid
|
|
|
|
// when mDidNotifyDataEnded is true.
|
2011-11-24 03:05:12 +04:00
|
|
|
nsresult mNotifyDataEndedStatus;
|
2009-04-01 04:52:56 +04:00
|
|
|
// The last reported read mode
|
2017-11-28 05:43:50 +03:00
|
|
|
ReadMode mCurrentMode = MODE_METADATA;
|
2011-09-30 03:34:37 +04:00
|
|
|
// True if some data in mPartialBlockBuffer has been read as metadata
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mMetadataInPartialBlockBuffer;
|
2017-09-20 06:41:08 +03:00
|
|
|
// The load ID of the current channel. Used to check whether the data is
|
|
|
|
// coming from an old channel and should be discarded.
|
|
|
|
uint32_t mLoadID = 0;
|
2017-10-25 04:37:58 +03:00
|
|
|
// The seek target initiated by MediaCache. -1 if no seek is going on.
|
|
|
|
int64_t mSeekTarget = -1;
|
2009-10-09 15:46:23 +04:00
|
|
|
|
2017-09-20 11:37:32 +03:00
|
|
|
bool mThrottleReadahead = false;
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
// Data received for the block containing mChannelOffset. Data needs
|
|
|
|
// to wait here so we can write back a complete block. The first
|
|
|
|
// mChannelOffset%BLOCK_SIZE bytes have been filled in with good data,
|
|
|
|
// the rest are garbage.
|
2014-02-11 10:43:52 +04:00
|
|
|
// Heap allocate this buffer since the exact power-of-2 will cause allocation
|
|
|
|
// slop when combined with the rest of the object members.
|
2017-09-20 11:43:19 +03:00
|
|
|
// This partial buffer should always be read/write within the cache's monitor.
|
|
|
|
const UniquePtr<uint8_t[]> mPartialBlockBuffer =
|
|
|
|
MakeUnique<uint8_t[]>(BLOCK_SIZE);
|
2017-04-06 11:20:00 +03:00
|
|
|
|
|
|
|
// True if associated with a private browsing window.
|
|
|
|
const bool mIsPrivateBrowsing;
|
2017-10-24 06:25:41 +03:00
|
|
|
|
|
|
|
// True if the client is suspended. Accessed on the owner thread only.
|
|
|
|
bool mClientSuspended = false;
|
2017-11-29 11:49:54 +03:00
|
|
|
|
|
|
|
MediaChannelStatistics mDownloadStatistics;
|
2009-04-01 04:52:56 +04:00
|
|
|
};
|
|
|
|
|
2012-11-14 23:45:33 +04:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
#endif
|