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:
|
2018-01-05 11:31:39 +03:00
|
|
|
explicit ChannelSuspendAgent(MediaCacheStream& aCacheStream)
|
2017-11-08 11:16:24 +03:00
|
|
|
: 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;
|
2018-01-05 11:31:39 +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
|
|
|
{
|
2017-12-08 06:08:38 +03:00
|
|
|
// Store information shared among resources. Main thread only.
|
|
|
|
struct SharedInfo
|
|
|
|
{
|
|
|
|
NS_INLINE_DECL_REFCOUNTING(SharedInfo);
|
|
|
|
nsCOMPtr<nsIPrincipal> mPrincipal;
|
|
|
|
nsTArray<ChannelMediaResource*> mResources;
|
|
|
|
|
|
|
|
private:
|
|
|
|
~SharedInfo() = default;
|
|
|
|
};
|
|
|
|
RefPtr<SharedInfo> mSharedInfo;
|
|
|
|
|
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;
|
2017-12-06 11:00:20 +03:00
|
|
|
bool IsLiveStream() const override { return mIsLiveStream; }
|
2017-09-18 14:33:49 +03:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
// 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,
|
Bug 1443942 - Ensure MediaCacheStreams are initialized with the length of the resource, not the length of the byte range response. r=jya
I'm seeing intermittent failures of test_midflight_redirect_blocked. In this
test, our custom server responds to Firefox's 0- HTTP Byte Range request with a
[0,200] response. When Firefox requests 200-, the server responds with a cross
origin redirect, and then the remainder of the resource.
However sometimes while running test_midflight_redirect_blocked the MP4 demuxer
reads through all 200 bytes while trying to parse metadata before the redirect
has occurred and fed more data into the cache, and so the demuxer thinks it's
hit end of stream, and reports a failure. The demuxer thinks it's hit end of
stream, because we initialize the MediaCacheStream length in
ChannelMediaResource::Open() with the value of the Content-Length HTTP header.
But in an HTTP byte range response, the Content-Length header tells you the
length of the range returned, not the length of the entire resource. The length
of the resource is in the Content-Range header, we need to use that if
available.
So to fix this intermittent test failure, we need to also parse the
Content-Range header in ChannelMediaResource::Open(), and use the length from
that if available.
MozReview-Commit-ID: 29pPRsUvxag
--HG--
extra : rebase_source : ba1ef03d65ebd22698a29d8385f36b4c747ccf43
2018-04-04 03:36:00 +03:00
|
|
|
int64_t& aRangeTotal) const;
|
|
|
|
|
|
|
|
// Calculates the length of the resource using HTTP headers, if this
|
|
|
|
// is an HTTP channel. Returns -1 on failure, or for non HTTP channels.
|
|
|
|
int64_t CalculateStreamLength() const;
|
2017-09-18 14:33:49 +03:00
|
|
|
|
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-12-06 11:00:20 +03:00
|
|
|
bool mIsLiveStream = false;
|
2017-09-18 14:33:49 +03:00
|
|
|
|
|
|
|
// Any thread access
|
|
|
|
MediaCacheStream mCacheStream;
|
|
|
|
|
|
|
|
ChannelSuspendAgent mSuspendAgent;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_dom_media_ChannelMediaResource_h
|