2017-09-18 14:33:49 +03:00
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_dom_media_ChannelMediaResource_h
|
|
|
|
#define mozilla_dom_media_ChannelMediaResource_h
|
|
|
|
|
2017-10-04 21:57:11 +03:00
|
|
|
#include "BaseMediaResource.h"
|
|
|
|
#include "MediaCache.h"
|
2017-10-04 21:34:07 +03:00
|
|
|
#include "mozilla/Mutex.h"
|
2017-10-04 21:57:11 +03:00
|
|
|
#include "nsIChannelEventSink.h"
|
2017-10-04 21:34:07 +03:00
|
|
|
#include "nsIHttpChannel.h"
|
2017-10-04 21:57:11 +03:00
|
|
|
#include "nsIInterfaceRequestor.h"
|
|
|
|
#include "nsIThreadRetargetableStreamListener.h"
|
2017-09-18 14:33:49 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2017-10-04 17:35:11 +03:00
|
|
|
/**
|
|
|
|
* This class is responsible for managing the suspend count and report suspend
|
|
|
|
* status of channel.
|
|
|
|
**/
|
|
|
|
class ChannelSuspendAgent
|
|
|
|
{
|
|
|
|
public:
|
2017-11-08 11:16:24 +03:00
|
|
|
explicit ChannelSuspendAgent(MediaCacheStream& aCacheStream)
|
|
|
|
: mCacheStream(aCacheStream)
|
2017-10-04 17:35:11 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// True when the channel has been suspended or needs to be suspended.
|
|
|
|
bool IsSuspended();
|
|
|
|
|
|
|
|
// Return true when the channel is logically suspended, i.e. the suspend
|
|
|
|
// count goes from 0 to 1.
|
|
|
|
bool Suspend();
|
|
|
|
|
|
|
|
// Return true only when the suspend count is equal to zero.
|
|
|
|
bool Resume();
|
|
|
|
|
2017-11-08 11:16:24 +03:00
|
|
|
// Tell the agent to manage the suspend status of the channel.
|
|
|
|
void Delegate(nsIChannel* aChannel);
|
|
|
|
// Stop the management of the suspend status of the channel.
|
|
|
|
void Revoke();
|
2017-10-04 17:35:11 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Only suspends channel but not changes the suspend count.
|
|
|
|
void SuspendInternal();
|
|
|
|
|
2017-11-08 11:16:24 +03:00
|
|
|
nsIChannel* mChannel = nullptr;
|
2017-10-24 06:25:41 +03:00
|
|
|
MediaCacheStream& mCacheStream;
|
2017-10-04 17:35:11 +03:00
|
|
|
uint32_t mSuspendCount = 0;
|
2017-10-24 06:25:41 +03:00
|
|
|
bool mIsChannelSuspended = false;
|
2017-10-04 17:35:11 +03: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
|
|
|
DDLoggedTypeDeclNameAndBase(ChannelMediaResource, BaseMediaResource);
|
|
|
|
|
2017-09-18 14:33:49 +03:00
|
|
|
/**
|
|
|
|
* This is the MediaResource implementation that wraps Necko channels.
|
|
|
|
* Much of its functionality is actually delegated to MediaCache via
|
|
|
|
* an underlying MediaCacheStream.
|
|
|
|
*
|
|
|
|
* All synchronization is performed by MediaCacheStream; all off-main-
|
|
|
|
* thread operations are delegated directly to that object.
|
|
|
|
*/
|
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 ChannelMediaResource
|
|
|
|
: public BaseMediaResource
|
|
|
|
, public DecoderDoctorLifeLogger<ChannelMediaResource>
|
2017-09-18 14:33:49 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
ChannelMediaResource(MediaResourceCallback* aDecoder,
|
|
|
|
nsIChannel* aChannel,
|
|
|
|
nsIURI* aURI,
|
2017-11-29 11:49:54 +03:00
|
|
|
bool aIsPrivateBrowsing = false);
|
2017-09-18 14:33:49 +03:00
|
|
|
~ChannelMediaResource();
|
|
|
|
|
|
|
|
// These are called on the main thread by MediaCache. These must
|
|
|
|
// not block or grab locks, because the media cache is holding its lock.
|
|
|
|
// Notify that data is available from the cache. This can happen even
|
|
|
|
// if this stream didn't read any data, since another stream might have
|
|
|
|
// received data for the same resource.
|
|
|
|
void CacheClientNotifyDataReceived();
|
|
|
|
// Notify that we reached the end of the stream. This can happen even
|
|
|
|
// if this stream didn't read any data, since another stream might have
|
|
|
|
// received data for the same resource.
|
|
|
|
void CacheClientNotifyDataEnded(nsresult aStatus);
|
|
|
|
// Notify that the principal for the cached resource changed.
|
|
|
|
void CacheClientNotifyPrincipalChanged();
|
|
|
|
// Notify the decoder that the cache suspended status changed.
|
2017-11-23 06:30:07 +03:00
|
|
|
void CacheClientNotifySuspendedStatusChanged(bool aSuspended);
|
2017-09-18 14:33:49 +03:00
|
|
|
|
|
|
|
// These are called on the main thread by MediaCache. These shouldn't block,
|
|
|
|
// but they may grab locks --- the media cache is not holding its lock
|
|
|
|
// when these are called.
|
|
|
|
// Start a new load at the given aOffset. The old load is cancelled
|
|
|
|
// and no more data from the old load will be notified via
|
|
|
|
// MediaCacheStream::NotifyDataReceived/Ended.
|
2017-10-24 04:23:12 +03:00
|
|
|
void CacheClientSeek(int64_t aOffset, bool aResume);
|
2017-09-18 14:33:49 +03:00
|
|
|
// Suspend the current load since data is currently not wanted
|
2017-10-24 04:38:34 +03:00
|
|
|
void CacheClientSuspend();
|
2017-09-18 14:33:49 +03:00
|
|
|
// Resume the current load since data is wanted again
|
2017-10-24 04:38:34 +03:00
|
|
|
void CacheClientResume();
|
2017-09-18 14:33:49 +03:00
|
|
|
|
|
|
|
bool IsSuspended();
|
|
|
|
|
|
|
|
void ThrottleReadahead(bool bThrottle) override;
|
|
|
|
|
|
|
|
// Main thread
|
|
|
|
nsresult Open(nsIStreamListener** aStreamListener) override;
|
|
|
|
nsresult Close() override;
|
|
|
|
void Suspend(bool aCloseImmediately) override;
|
|
|
|
void Resume() override;
|
|
|
|
already_AddRefed<nsIPrincipal> GetCurrentPrincipal() override;
|
|
|
|
bool CanClone() override;
|
|
|
|
already_AddRefed<BaseMediaResource> CloneData(
|
|
|
|
MediaResourceCallback* aDecoder) override;
|
|
|
|
nsresult ReadFromCache(char* aBuffer, int64_t aOffset, uint32_t aCount) override;
|
|
|
|
|
|
|
|
// Other thread
|
|
|
|
void SetReadMode(MediaCacheStream::ReadMode aMode) override;
|
|
|
|
void SetPlaybackRate(uint32_t aBytesPerSecond) override;
|
|
|
|
nsresult ReadAt(int64_t offset, char* aBuffer,
|
|
|
|
uint32_t aCount, uint32_t* aBytes) override;
|
|
|
|
// Data stored in IO&lock-encumbered MediaCacheStream, caching recommended.
|
|
|
|
bool ShouldCacheReads() override { return true; }
|
|
|
|
|
|
|
|
// Any thread
|
|
|
|
void Pin() override;
|
|
|
|
void Unpin() override;
|
|
|
|
double GetDownloadRate(bool* aIsReliable) override;
|
|
|
|
int64_t GetLength() override;
|
|
|
|
int64_t GetNextCachedData(int64_t aOffset) override;
|
|
|
|
int64_t GetCachedDataEnd(int64_t aOffset) override;
|
|
|
|
bool IsDataCachedToEndOfResource(int64_t aOffset) override;
|
|
|
|
bool IsTransportSeekable() override;
|
|
|
|
|
|
|
|
size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override {
|
|
|
|
// Might be useful to track in the future:
|
|
|
|
// - mListener (seems minor)
|
|
|
|
size_t size = BaseMediaResource::SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
size += mCacheStream.SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override {
|
|
|
|
return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2017-10-25 08:57:39 +03:00
|
|
|
nsCString GetDebugInfo() override;
|
|
|
|
|
2017-09-18 14:33:49 +03:00
|
|
|
class Listener final
|
|
|
|
: public nsIStreamListener
|
|
|
|
, public nsIInterfaceRequestor
|
|
|
|
, public nsIChannelEventSink
|
|
|
|
, public nsIThreadRetargetableStreamListener
|
|
|
|
{
|
|
|
|
~Listener() {}
|
|
|
|
public:
|
2017-09-20 06:41:08 +03:00
|
|
|
Listener(ChannelMediaResource* aResource, int64_t aOffset, uint32_t aLoadID)
|
2017-09-20 09:37:18 +03:00
|
|
|
: mMutex("Listener.mMutex")
|
|
|
|
, mResource(aResource)
|
2017-09-18 14:33:49 +03:00
|
|
|
, mOffset(aOffset)
|
2017-09-20 06:41:08 +03:00
|
|
|
, mLoadID(aLoadID)
|
2017-09-18 14:33:49 +03:00
|
|
|
{}
|
|
|
|
|
2017-09-20 12:14:01 +03:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2017-09-18 14:33:49 +03:00
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
|
|
|
NS_DECL_NSISTREAMLISTENER
|
|
|
|
NS_DECL_NSICHANNELEVENTSINK
|
|
|
|
NS_DECL_NSIINTERFACEREQUESTOR
|
|
|
|
NS_DECL_NSITHREADRETARGETABLESTREAMLISTENER
|
|
|
|
|
2017-09-20 09:37:18 +03:00
|
|
|
void Revoke();
|
2017-09-21 12:16:40 +03:00
|
|
|
|
2017-09-18 14:33:49 +03:00
|
|
|
private:
|
2017-09-20 09:37:18 +03:00
|
|
|
Mutex mMutex;
|
|
|
|
// mResource should only be modified on the main thread with the lock.
|
|
|
|
// So it can be read without lock on the main thread or on other threads
|
|
|
|
// with the lock.
|
2017-09-21 12:16:40 +03:00
|
|
|
RefPtr<ChannelMediaResource> mResource;
|
2017-11-08 09:25:31 +03:00
|
|
|
|
2017-09-18 14:33:49 +03:00
|
|
|
const int64_t mOffset;
|
2017-09-20 06:41:08 +03:00
|
|
|
const uint32_t mLoadID;
|
2017-09-18 14:33:49 +03:00
|
|
|
};
|
|
|
|
friend class Listener;
|
|
|
|
|
|
|
|
nsresult GetCachedRanges(MediaByteRangeSet& aRanges) override;
|
|
|
|
|
|
|
|
protected:
|
2017-10-24 04:23:12 +03:00
|
|
|
nsresult Seek(int64_t aOffset, bool aResume);
|
|
|
|
|
2017-09-18 14:33:49 +03:00
|
|
|
// These are called on the main thread by Listener.
|
|
|
|
nsresult OnStartRequest(nsIRequest* aRequest, int64_t aRequestOffset);
|
2017-11-22 05:35:48 +03:00
|
|
|
nsresult OnStopRequest(nsIRequest* aRequest, nsresult aStatus);
|
2017-09-20 06:41:08 +03:00
|
|
|
nsresult OnDataAvailable(uint32_t aLoadID,
|
2017-09-18 14:33:49 +03:00
|
|
|
nsIInputStream* aStream,
|
|
|
|
uint32_t aCount);
|
|
|
|
nsresult OnChannelRedirect(nsIChannel* aOld,
|
|
|
|
nsIChannel* aNew,
|
|
|
|
uint32_t aFlags,
|
|
|
|
int64_t aOffset);
|
|
|
|
|
|
|
|
// Opens the channel, using an HTTP byte range request to start at aOffset
|
|
|
|
// if possible. Main thread only.
|
|
|
|
nsresult OpenChannel(int64_t aOffset);
|
|
|
|
nsresult RecreateChannel();
|
|
|
|
// Add headers to HTTP request. Main thread only.
|
|
|
|
nsresult SetupChannelHeaders(int64_t aOffset);
|
|
|
|
// Closes the channel. Main thread only.
|
|
|
|
void CloseChannel();
|
|
|
|
// Update the principal for the resource. Main thread only.
|
|
|
|
void UpdatePrincipal();
|
|
|
|
|
|
|
|
int64_t GetOffset() const;
|
|
|
|
|
|
|
|
// Parses 'Content-Range' header and returns results via parameters.
|
|
|
|
// Returns error if header is not available, values are not parse-able or
|
|
|
|
// values are out of range.
|
|
|
|
nsresult ParseContentRangeHeader(nsIHttpChannel * aHttpChan,
|
|
|
|
int64_t& aRangeStart,
|
|
|
|
int64_t& aRangeEnd,
|
|
|
|
int64_t& aRangeTotal);
|
|
|
|
|
2017-09-20 06:41:08 +03:00
|
|
|
struct Closure
|
|
|
|
{
|
|
|
|
uint32_t mLoadID;
|
|
|
|
ChannelMediaResource* mResource;
|
|
|
|
};
|
|
|
|
|
2017-09-18 14:33:49 +03:00
|
|
|
static nsresult CopySegmentToCache(nsIInputStream* aInStream,
|
2017-09-20 06:41:08 +03:00
|
|
|
void* aClosure,
|
2017-09-18 14:33:49 +03:00
|
|
|
const char* aFromSegment,
|
|
|
|
uint32_t aToOffset,
|
|
|
|
uint32_t aCount,
|
|
|
|
uint32_t* aWriteCount);
|
|
|
|
|
|
|
|
// Main thread access only
|
2017-10-27 08:46:44 +03:00
|
|
|
// True if Close() has been called.
|
|
|
|
bool mClosed = false;
|
2017-11-07 09:26:10 +03:00
|
|
|
// The last reported seekability state for the underlying channel
|
|
|
|
bool mIsTransportSeekable = false;
|
2017-09-18 14:33:49 +03:00
|
|
|
RefPtr<Listener> mListener;
|
2017-09-20 06:41:08 +03:00
|
|
|
// A mono-increasing integer to uniquely identify the channel we are loading.
|
|
|
|
uint32_t mLoadID = 0;
|
2017-10-27 09:33:16 +03:00
|
|
|
// Used by the cache to store the offset to seek to when we are resumed.
|
|
|
|
// -1 means no seek initiated by the cache is waiting.
|
|
|
|
int64_t mPendingSeekOffset = -1;
|
2017-09-18 14:33:49 +03:00
|
|
|
|
|
|
|
// Any thread access
|
|
|
|
MediaCacheStream mCacheStream;
|
|
|
|
|
|
|
|
ChannelSuspendAgent mSuspendAgent;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_media_ChannelMediaResource_h
|