2008-10-19 11:39:21 +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-02-15 08:35:01 +04:00
|
|
|
#if !defined(MediaResource_h_)
|
|
|
|
# define MediaResource_h_
|
2008-10-19 11:39:21 +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"
|
2015-11-24 09:57:37 +03:00
|
|
|
# include "Intervals.h"
|
2015-06-17 01:30:11 +03:00
|
|
|
# include "MediaData.h"
|
2012-06-19 06:30:09 +04:00
|
|
|
# include "mozilla/Attributes.h"
|
2017-05-30 12:43:28 +03:00
|
|
|
# include "mozilla/UniquePtr.h"
|
2017-10-04 21:57:11 +03:00
|
|
|
# include "nsISeekableStream.h"
|
2013-09-06 00:25:17 +04:00
|
|
|
# include "nsThreadUtils.h"
|
|
|
|
|
2012-02-15 08:35:01 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2011-03-24 01:28:58 +03:00
|
|
|
// Represents a section of contiguous media, with a start and end offset.
|
|
|
|
// Used to denote ranges of data which are cached.
|
|
|
|
|
2015-11-24 12:37:21 +03:00
|
|
|
typedef media::Interval<int64_t> MediaByteRange;
|
2015-11-24 12:16:52 +03:00
|
|
|
typedef media::IntervalSet<int64_t> MediaByteRangeSet;
|
|
|
|
|
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(MediaResource);
|
|
|
|
|
2012-02-15 08:35:01 +04:00
|
|
|
/**
|
|
|
|
* Provides a thread-safe, seek/read interface to resources
|
2012-11-14 23:46:40 +04:00
|
|
|
* loaded from a URI. Uses MediaCache to cache data received over
|
2012-02-15 08:35:01 +04:00
|
|
|
* Necko's async channel API, thus resolving the mismatch between clients
|
|
|
|
* that need efficient random access to the data and protocols that do not
|
|
|
|
* support efficient random access, such as HTTP.
|
|
|
|
*
|
|
|
|
* Instances of this class must be created on the main thread.
|
|
|
|
* Most methods must be called on the main thread only. Read, Seek and
|
|
|
|
* Tell must only be called on non-main threads. In the case of the Ogg
|
|
|
|
* Decoder they are called on the Decode thread for example. You must
|
|
|
|
* ensure that no threads are calling these methods once Close is called.
|
|
|
|
*
|
2013-08-30 08:51:38 +04:00
|
|
|
* Instances of this class are reference counted. Use nsRefPtr for
|
|
|
|
* managing the lifetime of instances of this class.
|
2012-02-15 08:35:01 +04:00
|
|
|
*
|
|
|
|
* The generic implementation of this class is ChannelMediaResource, which can
|
|
|
|
* handle any URI for which Necko supports AsyncOpen.
|
|
|
|
* The 'file:' protocol can be implemented efficiently with direct random
|
|
|
|
* access, so the FileMediaResource implementation class bypasses the cache.
|
2017-09-18 14:34:43 +03:00
|
|
|
* For cross-process blob URL, CloneableWithRangeMediaResource is used.
|
2012-02-15 08:35:01 +04:00
|
|
|
* MediaResource::Create automatically chooses the best implementation class.
|
|
|
|
*/
|
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 MediaResource : public DecoderDoctorLifeLogger<MediaResource> {
|
2009-03-30 05:19:10 +04:00
|
|
|
public:
|
2014-02-26 04:36:54 +04:00
|
|
|
// Our refcounting is threadsafe, and when our refcount drops to zero
|
|
|
|
// we dispatch an event to the main thread to delete the MediaResource.
|
|
|
|
// Note that this means it's safe for references to this object to be
|
|
|
|
// released on a non main thread, but the destructor will always run on
|
|
|
|
// the main thread.
|
2020-05-19 21:30:55 +03:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING_WITH_DESTROY(MediaResource, Destroy());
|
2013-02-05 01:34:23 +04:00
|
|
|
|
2017-12-04 06:29:35 +03:00
|
|
|
// Close the resource, stop any listeners, channels, etc.
|
|
|
|
// Cancels any currently blocking Read request and forces that request to
|
2019-11-14 01:40:14 +03:00
|
|
|
// return an error. This must be called (and resolve) before the MediaResource
|
|
|
|
// is deleted.
|
|
|
|
virtual RefPtr<GenericPromise> Close() {
|
|
|
|
return GenericPromise::CreateAndResolve(true, __func__);
|
|
|
|
}
|
2017-12-04 06:29:35 +03:00
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
// These methods are called off the main thread.
|
2013-07-17 05:54:52 +04:00
|
|
|
// Read up to aCount bytes from the stream. The read starts at
|
|
|
|
// aOffset in the stream, seeking to that location initially if
|
|
|
|
// it is not the current stream offset. The remaining arguments,
|
|
|
|
// results and requirements are the same as per the Read method.
|
|
|
|
virtual nsresult ReadAt(int64_t aOffset, char* aBuffer, uint32_t aCount,
|
|
|
|
uint32_t* aBytes) = 0;
|
2017-06-02 05:27:17 +03:00
|
|
|
// Indicate whether caching data in advance of reads is worth it.
|
|
|
|
// E.g. Caching lockless and memory-based MediaResource subclasses would be a
|
|
|
|
// waste, but caching lock/IO-bound resources means reducing the impact of
|
|
|
|
// each read.
|
|
|
|
virtual bool ShouldCacheReads() = 0;
|
2015-07-10 09:08:07 +03:00
|
|
|
|
2009-04-01 04:52:56 +04:00
|
|
|
// These can be called on any thread.
|
|
|
|
// Cached blocks associated with this stream will not be evicted
|
|
|
|
// while the stream is pinned.
|
|
|
|
virtual void Pin() = 0;
|
|
|
|
virtual void Unpin() = 0;
|
|
|
|
// Get the length of the stream in bytes. Returns -1 if not known.
|
|
|
|
// This can change over time; after a seek operation, a misbehaving
|
|
|
|
// server may give us a resource of a different length to what it had
|
|
|
|
// reported previously --- or it may just lie in its Content-Length
|
|
|
|
// header and give us more or less data than it reported. We will adjust
|
|
|
|
// the result of GetLength to reflect the data that's actually arriving.
|
2012-08-22 19:56:38 +04:00
|
|
|
virtual int64_t GetLength() = 0;
|
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
|
|
|
virtual int64_t GetNextCachedData(int64_t aOffset) = 0;
|
2017-06-01 02:29:49 +03:00
|
|
|
// Returns the end of the bytes starting at the given offset which are in
|
|
|
|
// cache. Returns aOffset itself if there are zero bytes available there.
|
2012-08-22 19:56:38 +04:00
|
|
|
virtual int64_t GetCachedDataEnd(int64_t aOffset) = 0;
|
2009-04-01 04:52:56 +04:00
|
|
|
// Returns true if all the data from aOffset to the end of the stream
|
|
|
|
// is in cache. If the end of the stream is not known, we return false.
|
2012-08-22 19:56:38 +04:00
|
|
|
virtual bool IsDataCachedToEndOfResource(int64_t aOffset) = 0;
|
2010-08-05 11:40:35 +04:00
|
|
|
// Reads only data which is cached in the media cache. If you try to read
|
|
|
|
// any data which overlaps uncached data, or if aCount bytes otherwise can't
|
|
|
|
// be read, this function will return failure. This function be called from
|
|
|
|
// any thread, and it is the only read operation which is safe to call on
|
|
|
|
// the main thread, since it's guaranteed to be non blocking.
|
|
|
|
virtual nsresult ReadFromCache(char* aBuffer, int64_t aOffset,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aCount) = 0;
|
2009-03-30 05:19:10 +04:00
|
|
|
|
2011-03-24 01:28:58 +03:00
|
|
|
/**
|
2012-02-15 08:35:01 +04:00
|
|
|
* Fills aRanges with MediaByteRanges representing the data which is cached
|
2011-03-24 01:28:58 +03:00
|
|
|
* in the media cache. Stream should be pinned during call and while
|
|
|
|
* aRanges is being used.
|
|
|
|
*/
|
2015-11-24 12:16:52 +03:00
|
|
|
virtual nsresult GetCachedRanges(MediaByteRangeSet& aRanges) = 0;
|
2012-12-07 03:27:08 +04:00
|
|
|
|
2013-05-03 02:59:18 +04:00
|
|
|
protected:
|
2020-03-04 18:39:20 +03:00
|
|
|
virtual ~MediaResource() = default;
|
2014-02-26 04:36:54 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
void Destroy();
|
2012-11-17 08:31:53 +04:00
|
|
|
};
|
|
|
|
|
2014-11-12 07:50:21 +03:00
|
|
|
/**
|
|
|
|
* RAII class that handles pinning and unpinning for MediaResource and derived.
|
|
|
|
* This should be used when making calculations that involve potentially-cached
|
|
|
|
* MediaResource data, so that the state of the world can't change out from
|
|
|
|
* under us.
|
|
|
|
*/
|
|
|
|
template <class T>
|
2015-09-03 19:15:23 +03:00
|
|
|
class MOZ_RAII AutoPinned {
|
2014-11-12 07:50:21 +03:00
|
|
|
public:
|
2020-07-30 17:22:38 +03:00
|
|
|
explicit AutoPinned(T* aResource) : mResource(aResource) {
|
2014-11-12 07:50:21 +03:00
|
|
|
MOZ_ASSERT(mResource);
|
|
|
|
mResource->Pin();
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoPinned() { mResource->Unpin(); }
|
|
|
|
|
|
|
|
operator T*() const { return mResource; }
|
2014-12-25 23:18:38 +03:00
|
|
|
T* operator->() const MOZ_NO_ADDREF_RELEASE_ON_RETURN { return mResource; }
|
2014-11-12 07:50:21 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
T* mResource;
|
|
|
|
};
|
|
|
|
|
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(MediaResourceIndex);
|
|
|
|
|
2015-08-13 04:15:48 +03:00
|
|
|
/*
|
|
|
|
* MediaResourceIndex provides a way to access MediaResource objects.
|
|
|
|
* Read, Seek and Tell must only be called on non-main threads.
|
|
|
|
* In the case of the Ogg Decoder they are called on the Decode thread for
|
|
|
|
* example. You must ensure that no threads are calling these methods once
|
|
|
|
* the MediaResource has been Closed.
|
|
|
|
*/
|
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 MediaResourceIndex : public DecoderDoctorLifeLogger<MediaResourceIndex> {
|
2015-08-13 04:15:48 +03:00
|
|
|
public:
|
2017-10-04 22:25:19 +03:00
|
|
|
explicit MediaResourceIndex(MediaResource* aResource);
|
2015-08-13 04:15:48 +03:00
|
|
|
|
|
|
|
// Read up to aCount bytes from the stream. The buffer must have
|
|
|
|
// enough room for at least aCount bytes. Stores the number of
|
|
|
|
// actual bytes read in aBytes (0 on end of file).
|
|
|
|
// May read less than aCount bytes if the number of
|
|
|
|
// available bytes is less than aCount. Always check *aBytes after
|
|
|
|
// read, and call again if necessary.
|
|
|
|
nsresult Read(char* aBuffer, uint32_t aCount, uint32_t* aBytes);
|
|
|
|
// Seek to the given bytes offset in the stream. aWhence can be
|
|
|
|
// one of:
|
2017-10-04 21:57:11 +03:00
|
|
|
// nsISeekableStream::NS_SEEK_SET
|
|
|
|
// nsISeekableStream::NS_SEEK_CUR
|
|
|
|
// nsISeekableStream::NS_SEEK_END
|
2015-08-13 04:15:48 +03:00
|
|
|
//
|
|
|
|
// In the Http strategy case the cancel will cause the http
|
|
|
|
// channel's listener to close the pipe, forcing an i/o error on any
|
|
|
|
// blocked read. This will allow the decode thread to complete the
|
|
|
|
// event.
|
|
|
|
//
|
|
|
|
// In the case of a seek in progress, the byte range request creates
|
|
|
|
// a new listener. This is done on the main thread via seek
|
|
|
|
// synchronously dispatching an event. This avoids the issue of us
|
|
|
|
// closing the listener but an outstanding byte range request
|
|
|
|
// creating a new one. They run on the same thread so no explicit
|
|
|
|
// synchronisation is required. The byte range request checks for
|
|
|
|
// the cancel flag and does not create a new channel or listener if
|
|
|
|
// we are cancelling.
|
|
|
|
//
|
|
|
|
// The default strategy does not do any seeking - the only issue is
|
|
|
|
// a blocked read which it handles by causing the listener to close
|
|
|
|
// the pipe, as per the http case.
|
|
|
|
//
|
|
|
|
// The file strategy doesn't block for any great length of time so
|
|
|
|
// is fine for a no-op cancel.
|
|
|
|
nsresult Seek(int32_t aWhence, int64_t aOffset);
|
|
|
|
// Report the current offset in bytes from the start of the stream.
|
|
|
|
int64_t Tell() const { return mOffset; }
|
|
|
|
|
|
|
|
// Return the underlying MediaResource.
|
|
|
|
MediaResource* GetResource() const { return mResource; }
|
|
|
|
|
2015-08-13 04:18:34 +03:00
|
|
|
// Read up to aCount bytes from the stream. The read starts at
|
|
|
|
// aOffset in the stream, seeking to that location initially if
|
|
|
|
// it is not the current stream offset.
|
|
|
|
// Unlike MediaResource::ReadAt, ReadAt only returns fewer bytes than
|
|
|
|
// requested if end of stream or an error is encountered. There is no need to
|
|
|
|
// call it again to get more data.
|
2017-05-29 04:36:27 +03:00
|
|
|
// If the resource has cached data past the end of the request, it will be
|
|
|
|
// used to fill a local cache, which should speed up consecutive ReadAt's
|
|
|
|
// (mostly by avoiding using the resource's IOs and locks.)
|
2015-08-13 04:18:34 +03:00
|
|
|
// *aBytes will contain the number of bytes copied, even if an error occurred.
|
|
|
|
// ReadAt doesn't have an impact on the offset returned by Tell().
|
Bug 1368837 - MediaResourceIndex::ReadAt tries to cache last-read block - r=cpearce
This is the core of this bug:
- We try to read past the end of the requested range, and save a block-full of
cached data. ("Block" is a memory range, with an alignment and size being a
power of two, to match existing caching happening in MediaCache and
FileBlockCache, and to play nice with the memory allocator.)
- If part of a requested read touches the existing cache, we can just read from
the cache, which means we don't involve any of the locking and IOs that normal
reads use.
The small extra work needed to cache more data in some reads is largely offset
by all the lock&IO-heavy reads that we can subsequently avoid.
UncachedReadAt, which is used internally by CachedReadAt, is left public
because it could be useful if the caller knows for sure that a particular read
is isolated.
(Note: The printfs, and comparison code in ReadAt, will be removed in later
patches. Also the block size will be later controlled by a pref.)
MozReview-Commit-ID: GFiaP5Io7Hf
--HG--
extra : rebase_source : 2bebcdb7854989b55f2026e92338a00ac29a5376
2017-05-30 05:59:30 +03:00
|
|
|
nsresult ReadAt(int64_t aOffset, char* aBuffer, uint32_t aCount,
|
|
|
|
uint32_t* aBytes);
|
|
|
|
|
|
|
|
// Same as ReadAt, but doesn't try to cache around the read.
|
|
|
|
// Useful if you know that you will not read again from the same area.
|
|
|
|
nsresult UncachedReadAt(int64_t aOffset, char* aBuffer, uint32_t aCount,
|
|
|
|
uint32_t* aBytes) const;
|
Bug 1374180 - MediaResourceIndex won't use ReadFromCache, to keep MediaCache more consistent - r=cpearce
Bug 1368837 (Add lockless cache to MediaResourceIndex) used
MediaResource::ReadFromCache when trying to fill its local cache. But this
meant that these reads would not count as "real reads" for the MediaCache,
which could lead to unexpected states, e.g. "Played block after the current
stream position?"
To solve this, this patch reverts to using normal ReadAt() calls, which let
the MediaCache update itself properly.
While still trying to read more than what is requested (to hopefully fulfill
future reads from the local cache), we don't want to be reading so much that
we block waiting for data past the caller-expected point.
To do this, MediaResourceIndex::UncachedRangedReadAt() is called to try and
read as much as could fill the local cache, but if a single
MediaResource::ReadAt() ends before we can fill the local cache, but *after*
the minimum caller-requested size, we stop trying to read, as such extra read
is at risk of blocking (assuming that the caller knows not to read too far.)
MozReview-Commit-ID: 6fGvJpmkuLz
--HG--
extra : rebase_source : bf8e9f20599a05c8f3f221b55d678f0b5da447a9
2017-06-21 02:35:00 +03:00
|
|
|
|
|
|
|
// Similar to ReadAt, but doesn't try to cache around the read.
|
|
|
|
// Useful if you know that you will not read again from the same area.
|
|
|
|
// Will attempt to read aRequestedCount+aExtraCount, repeatedly calling
|
|
|
|
// MediaResource/ ReadAt()'s until a read returns 0 bytes (so we may actually
|
|
|
|
// get less than aRequestedCount bytes), or until we get at least
|
|
|
|
// aRequestedCount bytes (so we may not get any/all of the aExtraCount bytes.)
|
|
|
|
nsresult UncachedRangedReadAt(int64_t aOffset, char* aBuffer,
|
|
|
|
uint32_t aRequestedCount, uint32_t aExtraCount,
|
|
|
|
uint32_t* aBytes) const;
|
2015-08-13 04:18:34 +03:00
|
|
|
|
2015-08-13 04:15:48 +03:00
|
|
|
// This method returns nullptr if anything fails.
|
|
|
|
// Otherwise, it returns an owned buffer.
|
|
|
|
// MediaReadAt may return fewer bytes than requested if end of stream is
|
|
|
|
// encountered. There is no need to call it again to get more data.
|
2017-06-15 12:39:19 +03:00
|
|
|
// Note this method will not update mOffset.
|
2017-10-04 22:25:19 +03:00
|
|
|
already_AddRefed<MediaByteBuffer> MediaReadAt(int64_t aOffset,
|
|
|
|
uint32_t aCount) const;
|
2017-08-21 09:33:38 +03:00
|
|
|
|
|
|
|
already_AddRefed<MediaByteBuffer> CachedMediaReadAt(int64_t aOffset,
|
2017-10-04 22:25:19 +03:00
|
|
|
uint32_t aCount) const;
|
2017-08-21 09:33:38 +03:00
|
|
|
|
2015-08-13 04:15:48 +03:00
|
|
|
// Get the length of the stream in bytes. Returns -1 if not known.
|
|
|
|
// This can change over time; after a seek operation, a misbehaving
|
|
|
|
// server may give us a resource of a different length to what it had
|
|
|
|
// reported previously --- or it may just lie in its Content-Length
|
|
|
|
// header and give us more or less data than it reported. We will adjust
|
|
|
|
// the result of GetLength to reflect the data that's actually arriving.
|
2017-10-04 22:25:19 +03:00
|
|
|
int64_t GetLength() const;
|
2015-08-13 04:15:48 +03:00
|
|
|
|
|
|
|
private:
|
Bug 1368837 - MediaResourceIndex::ReadAt tries to cache last-read block - r=cpearce
This is the core of this bug:
- We try to read past the end of the requested range, and save a block-full of
cached data. ("Block" is a memory range, with an alignment and size being a
power of two, to match existing caching happening in MediaCache and
FileBlockCache, and to play nice with the memory allocator.)
- If part of a requested read touches the existing cache, we can just read from
the cache, which means we don't involve any of the locking and IOs that normal
reads use.
The small extra work needed to cache more data in some reads is largely offset
by all the lock&IO-heavy reads that we can subsequently avoid.
UncachedReadAt, which is used internally by CachedReadAt, is left public
because it could be useful if the caller knows for sure that a particular read
is isolated.
(Note: The printfs, and comparison code in ReadAt, will be removed in later
patches. Also the block size will be later controlled by a pref.)
MozReview-Commit-ID: GFiaP5Io7Hf
--HG--
extra : rebase_source : 2bebcdb7854989b55f2026e92338a00ac29a5376
2017-05-30 05:59:30 +03:00
|
|
|
// If the resource has cached data past the requested range, try to grab it
|
|
|
|
// into our local cache.
|
|
|
|
// If there is no cached data, or attempting to read it fails, fallback on
|
|
|
|
// a (potentially-blocking) read of just what was requested, so that we don't
|
|
|
|
// get unexpected side-effects by trying to read more than intended.
|
2017-05-30 05:12:38 +03:00
|
|
|
nsresult CacheOrReadAt(int64_t aOffset, char* aBuffer, uint32_t aCount,
|
Bug 1368837 - MediaResourceIndex::ReadAt tries to cache last-read block - r=cpearce
This is the core of this bug:
- We try to read past the end of the requested range, and save a block-full of
cached data. ("Block" is a memory range, with an alignment and size being a
power of two, to match existing caching happening in MediaCache and
FileBlockCache, and to play nice with the memory allocator.)
- If part of a requested read touches the existing cache, we can just read from
the cache, which means we don't involve any of the locking and IOs that normal
reads use.
The small extra work needed to cache more data in some reads is largely offset
by all the lock&IO-heavy reads that we can subsequently avoid.
UncachedReadAt, which is used internally by CachedReadAt, is left public
because it could be useful if the caller knows for sure that a particular read
is isolated.
(Note: The printfs, and comparison code in ReadAt, will be removed in later
patches. Also the block size will be later controlled by a pref.)
MozReview-Commit-ID: GFiaP5Io7Hf
--HG--
extra : rebase_source : 2bebcdb7854989b55f2026e92338a00ac29a5376
2017-05-30 05:59:30 +03:00
|
|
|
uint32_t* aBytes);
|
|
|
|
|
|
|
|
// Maps a file offset to a mCachedBlock index.
|
2017-10-04 22:25:19 +03:00
|
|
|
uint32_t IndexInCache(int64_t aOffsetInFile) const;
|
Bug 1368837 - MediaResourceIndex::ReadAt tries to cache last-read block - r=cpearce
This is the core of this bug:
- We try to read past the end of the requested range, and save a block-full of
cached data. ("Block" is a memory range, with an alignment and size being a
power of two, to match existing caching happening in MediaCache and
FileBlockCache, and to play nice with the memory allocator.)
- If part of a requested read touches the existing cache, we can just read from
the cache, which means we don't involve any of the locking and IOs that normal
reads use.
The small extra work needed to cache more data in some reads is largely offset
by all the lock&IO-heavy reads that we can subsequently avoid.
UncachedReadAt, which is used internally by CachedReadAt, is left public
because it could be useful if the caller knows for sure that a particular read
is isolated.
(Note: The printfs, and comparison code in ReadAt, will be removed in later
patches. Also the block size will be later controlled by a pref.)
MozReview-Commit-ID: GFiaP5Io7Hf
--HG--
extra : rebase_source : 2bebcdb7854989b55f2026e92338a00ac29a5376
2017-05-30 05:59:30 +03:00
|
|
|
|
|
|
|
// Starting file offset of the cache block that contains a given file offset.
|
2017-10-04 22:25:19 +03:00
|
|
|
int64_t CacheOffsetContaining(int64_t aOffsetInFile) const;
|
Bug 1368837 - MediaResourceIndex::ReadAt tries to cache last-read block - r=cpearce
This is the core of this bug:
- We try to read past the end of the requested range, and save a block-full of
cached data. ("Block" is a memory range, with an alignment and size being a
power of two, to match existing caching happening in MediaCache and
FileBlockCache, and to play nice with the memory allocator.)
- If part of a requested read touches the existing cache, we can just read from
the cache, which means we don't involve any of the locking and IOs that normal
reads use.
The small extra work needed to cache more data in some reads is largely offset
by all the lock&IO-heavy reads that we can subsequently avoid.
UncachedReadAt, which is used internally by CachedReadAt, is left public
because it could be useful if the caller knows for sure that a particular read
is isolated.
(Note: The printfs, and comparison code in ReadAt, will be removed in later
patches. Also the block size will be later controlled by a pref.)
MozReview-Commit-ID: GFiaP5Io7Hf
--HG--
extra : rebase_source : 2bebcdb7854989b55f2026e92338a00ac29a5376
2017-05-30 05:59:30 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaResource> mResource;
|
2015-08-13 04:15:48 +03:00
|
|
|
int64_t mOffset;
|
Bug 1368837 - MediaResourceIndex::ReadAt tries to cache last-read block - r=cpearce
This is the core of this bug:
- We try to read past the end of the requested range, and save a block-full of
cached data. ("Block" is a memory range, with an alignment and size being a
power of two, to match existing caching happening in MediaCache and
FileBlockCache, and to play nice with the memory allocator.)
- If part of a requested read touches the existing cache, we can just read from
the cache, which means we don't involve any of the locking and IOs that normal
reads use.
The small extra work needed to cache more data in some reads is largely offset
by all the lock&IO-heavy reads that we can subsequently avoid.
UncachedReadAt, which is used internally by CachedReadAt, is left public
because it could be useful if the caller knows for sure that a particular read
is isolated.
(Note: The printfs, and comparison code in ReadAt, will be removed in later
patches. Also the block size will be later controlled by a pref.)
MozReview-Commit-ID: GFiaP5Io7Hf
--HG--
extra : rebase_source : 2bebcdb7854989b55f2026e92338a00ac29a5376
2017-05-30 05:59:30 +03:00
|
|
|
|
|
|
|
// Local cache used by ReadAt().
|
|
|
|
// mCachedBlock is valid when mCachedBytes != 0, in which case it contains
|
|
|
|
// data of length mCachedBytes, starting at offset `mCachedOffset` in the
|
|
|
|
// resource, located at index `IndexInCache(mCachedOffset)` in mCachedBlock.
|
|
|
|
//
|
|
|
|
// resource: |------------------------------------------------------|
|
2017-05-30 12:43:28 +03:00
|
|
|
// <----------> mCacheBlockSize
|
Bug 1368837 - MediaResourceIndex::ReadAt tries to cache last-read block - r=cpearce
This is the core of this bug:
- We try to read past the end of the requested range, and save a block-full of
cached data. ("Block" is a memory range, with an alignment and size being a
power of two, to match existing caching happening in MediaCache and
FileBlockCache, and to play nice with the memory allocator.)
- If part of a requested read touches the existing cache, we can just read from
the cache, which means we don't involve any of the locking and IOs that normal
reads use.
The small extra work needed to cache more data in some reads is largely offset
by all the lock&IO-heavy reads that we can subsequently avoid.
UncachedReadAt, which is used internally by CachedReadAt, is left public
because it could be useful if the caller knows for sure that a particular read
is isolated.
(Note: The printfs, and comparison code in ReadAt, will be removed in later
patches. Also the block size will be later controlled by a pref.)
MozReview-Commit-ID: GFiaP5Io7Hf
--HG--
extra : rebase_source : 2bebcdb7854989b55f2026e92338a00ac29a5376
2017-05-30 05:59:30 +03:00
|
|
|
// <---------------------------------> mCachedOffset
|
|
|
|
// <--> mCachedBytes
|
|
|
|
// mCachedBlock: |..----....|
|
|
|
|
// CacheOffsetContaining(mCachedOffset) <--> IndexInCache(mCachedOffset)
|
|
|
|
// <------------------------------>
|
2017-05-30 12:43:28 +03:00
|
|
|
const uint32_t mCacheBlockSize;
|
Bug 1368837 - MediaResourceIndex::ReadAt tries to cache last-read block - r=cpearce
This is the core of this bug:
- We try to read past the end of the requested range, and save a block-full of
cached data. ("Block" is a memory range, with an alignment and size being a
power of two, to match existing caching happening in MediaCache and
FileBlockCache, and to play nice with the memory allocator.)
- If part of a requested read touches the existing cache, we can just read from
the cache, which means we don't involve any of the locking and IOs that normal
reads use.
The small extra work needed to cache more data in some reads is largely offset
by all the lock&IO-heavy reads that we can subsequently avoid.
UncachedReadAt, which is used internally by CachedReadAt, is left public
because it could be useful if the caller knows for sure that a particular read
is isolated.
(Note: The printfs, and comparison code in ReadAt, will be removed in later
patches. Also the block size will be later controlled by a pref.)
MozReview-Commit-ID: GFiaP5Io7Hf
--HG--
extra : rebase_source : 2bebcdb7854989b55f2026e92338a00ac29a5376
2017-05-30 05:59:30 +03:00
|
|
|
int64_t mCachedOffset;
|
|
|
|
uint32_t mCachedBytes;
|
2017-05-30 12:43:28 +03:00
|
|
|
UniquePtr<char[]> mCachedBlock;
|
2015-08-13 04:15:48 +03:00
|
|
|
};
|
|
|
|
|
2012-11-14 23:45:33 +04:00
|
|
|
} // namespace mozilla
|
2012-02-15 08:35:01 +04:00
|
|
|
|
2008-10-19 11:39:21 +04:00
|
|
|
#endif
|