Bug 1388228. P6 - following P5, we are now able to move some methods that are related to nsIChannel to BaseMediaResource. r=gerald

So we reduce the number of unimplemented methods in the sub-classes of MediaResource.

MozReview-Commit-ID: EAmUEv9WQk8

--HG--
extra : rebase_source : deed5fd089e8c42a5a6ab0546e0781d0061591e5
This commit is contained in:
JW Wang 2017-08-07 18:09:56 +08:00
Родитель 6ab3fdad07
Коммит cf112f4b88
17 изменённых файлов: 115 добавлений и 124 удалений

Просмотреть файл

@ -36,9 +36,6 @@ protected:
}
private:
nsresult Close() override { return NS_OK; }
void Suspend(bool aCloseImmediately) override {}
void Resume() override {}
// Get the current principal for the channel
already_AddRefed<nsIPrincipal> GetCurrentPrincipal() override
{
@ -48,7 +45,6 @@ private:
// These methods are called off the main thread.
// The mode is initially MODE_PLAYBACK.
void SetReadMode(MediaCacheStream::ReadMode aMode) override {}
void SetPlaybackRate(uint32_t aBytesPerSecond) override {}
nsresult ReadAt(int64_t aOffset, char* aBuffer,
uint32_t aCount, uint32_t* aBytes) override
{
@ -67,7 +63,6 @@ private:
void Pin() override {}
void Unpin() override {}
double GetDownloadRate(bool* aIsReliable) override { *aIsReliable = false; return 0.; }
int64_t GetLength() override { return mLength; }
int64_t GetNextCachedData(int64_t aOffset) override { return aOffset; }
int64_t GetCachedDataEnd(int64_t aOffset) override
@ -90,11 +85,6 @@ private:
return NS_OK;
}
nsresult Open(nsIStreamListener** aStreamListener) override
{
return NS_ERROR_FAILURE;
}
nsresult GetCachedRanges(MediaByteRangeSet& aRanges) override
{
aRanges += MediaByteRange(0, int64_t(mLength));

Просмотреть файл

@ -214,6 +214,12 @@ ChannelMediaDecoder::Shutdown()
mWatchManager.Shutdown();
mResourceCallback->Disconnect();
MediaDecoder::Shutdown();
// Force any outstanding seek and byterange requests to complete
// to prevent shutdown from deadlocking.
if (mResource) {
mResource->Close();
}
}
nsresult
@ -468,6 +474,33 @@ ChannelMediaDecoder::ShouldThrottleDownload()
return stats.mDownloadRate > factor * stats.mPlaybackRate;
}
void
ChannelMediaDecoder::SetLoadInBackground(bool aLoadInBackground)
{
MOZ_ASSERT(NS_IsMainThread());
if (mResource) {
mResource->SetLoadInBackground(aLoadInBackground);
}
}
void
ChannelMediaDecoder::Suspend()
{
MOZ_ASSERT(NS_IsMainThread());
if (mResource) {
mResource->Suspend(true);
}
}
void
ChannelMediaDecoder::Resume()
{
MOZ_ASSERT(NS_IsMainThread());
if (mResource) {
mResource->Resume();
}
}
} // namespace mozilla
// avoid redefined macro in unified build

Просмотреть файл

@ -78,6 +78,10 @@ public:
bool aIsPrivateBrowsing,
nsIStreamListener** aStreamListener);
void SetLoadInBackground(bool aLoadInBackground) override;
void Suspend() override;
void Resume() override;
private:
virtual ChannelMediaDecoder* CloneImpl(MediaDecoderInit& aInit) = 0;
nsresult OpenResource(nsIStreamListener** aStreamListener);

Просмотреть файл

@ -475,12 +475,6 @@ MediaDecoder::Shutdown()
mAbstractMainThread->Dispatch(r.forget());
}
// Force any outstanding seek and byterange requests to complete
// to prevent shutdown from deadlocking.
if (MediaResource* r = GetResource()) {
r->Close();
}
// Ask the owner to remove its audio/video tracks.
GetOwner()->RemoveMediaTracks();
@ -1243,33 +1237,6 @@ MediaDecoder::SetFragmentEndTime(double aTime)
}
}
void
MediaDecoder::Suspend()
{
MOZ_ASSERT(NS_IsMainThread());
if (MediaResource* r = GetResource()) {
r->Suspend(true);
}
}
void
MediaDecoder::Resume()
{
MOZ_ASSERT(NS_IsMainThread());
if (MediaResource* r = GetResource()) {
r->Resume();
}
}
void
MediaDecoder::SetLoadInBackground(bool aLoadInBackground)
{
MOZ_ASSERT(NS_IsMainThread());
if (MediaResource* r = GetResource()) {
r->SetLoadInBackground(aLoadInBackground);
}
}
void
MediaDecoder::SetPlaybackRate(double aPlaybackRate)
{

Просмотреть файл

@ -234,21 +234,21 @@ public:
// media element when it is sent to the bfcache, or when we need
// to throttle the download. Call on the main thread only. This can
// be called multiple times, there's an internal "suspend count".
virtual void Suspend();
virtual void Suspend() {}
// Resume any media downloads that have been suspended. Called by the
// media element when it is restored from the bfcache, or when we need
// to stop throttling the download. Call on the main thread only.
// The download will only actually resume once as many Resume calls
// have been made as Suspend calls.
virtual void Resume();
virtual void Resume() {}
// Moves any existing channel loads into or out of background. Background
// loads don't block the load event. This is called when we stop or restart
// delaying the load event. This also determines whether any new loads
// initiated (for example to seek) will be in the background. This calls
// SetLoadInBackground() on mResource.
void SetLoadInBackground(bool aLoadInBackground);
virtual void SetLoadInBackground(bool aLoadInBackground) {}
MediaDecoderStateMachine* GetStateMachine() const;
void SetStateMachine(MediaDecoderStateMachine* aStateMachine);

Просмотреть файл

@ -160,28 +160,12 @@ public:
// the main thread.
NS_DECL_THREADSAFE_ISUPPORTS
// Close the resource, stop any listeners, channels, etc.
// Cancels any currently blocking Read request and forces that request to
// return an error.
virtual nsresult Close() = 0;
// Suspend any downloads that are in progress.
// If aCloseImmediately is set, resources should be released immediately
// since we don't expect to resume again any time soon. Otherwise we
// may resume again soon so resources should be held for a little
// while.
virtual void Suspend(bool aCloseImmediately) = 0;
// Resume any downloads that have been suspended.
virtual void Resume() = 0;
// Get the current principal for the channel
virtual already_AddRefed<nsIPrincipal> GetCurrentPrincipal() = 0;
// These methods are called off the main thread.
// The mode is initially MODE_PLAYBACK.
virtual void SetReadMode(MediaCacheStream::ReadMode aMode) = 0;
// 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.
virtual void SetPlaybackRate(uint32_t aBytesPerSecond) = 0;
// 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,
@ -205,20 +189,11 @@ public:
return bytes.forget();
}
// Pass true to limit the amount of readahead data (specified by
// "media.cache_readahead_limit") or false to read as much as the
// cache size allows.
virtual void ThrottleReadahead(bool bThrottle) { }
// Report the current offset in bytes from the start of the stream.
// This is used to approximate where we currently are in the playback of a
// media.
// A call to ReadAt will update this position.
virtual int64_t Tell() = 0;
// Moves any existing channel loads into or out of background. Background
// loads don't block the load event. This also determines whether or not any
// new loads initiated (for example to seek) will be in the background.
virtual void SetLoadInBackground(bool aLoadInBackground) {}
// Ensures that the value returned by IsSuspendedByCache below is up to date
// (i.e. the cache has examined this stream at least once).
virtual void EnsureCacheUpToDate() {}
@ -228,10 +203,6 @@ public:
// while the stream is pinned.
virtual void Pin() = 0;
virtual void Unpin() = 0;
// Get the estimated download rate in bytes per second (assuming no
// pausing of the channel is requested by Gecko).
// *aIsReliable is set to true if we think the estimate is useful.
virtual double GetDownloadRate(bool* aIsReliable) = 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
@ -280,12 +251,6 @@ public:
// requests are supported by the connection/server.
virtual bool IsTransportSeekable() = 0;
/**
* Open the stream. This creates a stream listener and returns it in
* aStreamListener; this listener needs to be notified of incoming data.
*/
virtual nsresult Open(nsIStreamListener** aStreamListener) = 0;
/**
* Fills aRanges with MediaByteRanges representing the data which is cached
* in the media cache. Stream should be pinned during call and while
@ -325,6 +290,47 @@ public:
nsIChannel* aChannel,
bool aIsPrivateBrowsing);
// Close the resource, stop any listeners, channels, etc.
// Cancels any currently blocking Read request and forces that request to
// return an error.
virtual nsresult Close() = 0;
// Pass true to limit the amount of readahead data (specified by
// "media.cache_readahead_limit") or false to read as much as the
// cache size allows.
virtual void ThrottleReadahead(bool bThrottle) {}
// 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.
virtual void SetPlaybackRate(uint32_t aBytesPerSecond) = 0;
// Get the estimated download rate in bytes per second (assuming no
// pausing of the channel is requested by Gecko).
// *aIsReliable is set to true if we think the estimate is useful.
virtual double GetDownloadRate(bool* aIsReliable) = 0;
// Moves any existing channel loads into or out of background. Background
// loads don't block the load event. This also determines whether or not any
// new loads initiated (for example to seek) will be in the background.
void SetLoadInBackground(bool aLoadInBackground);
// Suspend any downloads that are in progress.
// If aCloseImmediately is set, resources should be released immediately
// since we don't expect to resume again any time soon. Otherwise we
// may resume again soon so resources should be held for a little
// while.
virtual void Suspend(bool aCloseImmediately) = 0;
// Resume any downloads that have been suspended.
virtual void Resume() = 0;
/**
* Open the stream. This creates a stream listener and returns it in
* aStreamListener; this listener needs to be notified of incoming data.
*/
virtual nsresult Open(nsIStreamListener** aStreamListener) = 0;
// If this returns false, then we shouldn't try to clone this MediaResource
// because its underlying resources are not suitable for reuse (e.g.
// because the underlying connection has been lost, or this resource
@ -341,8 +347,6 @@ public:
return nullptr;
}
void SetLoadInBackground(bool aLoadInBackground) override;
size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override
{
// Might be useful to track in the future:

Просмотреть файл

@ -17,7 +17,7 @@ MockMediaResource::MockMediaResource(const char* aFileName)
}
nsresult
MockMediaResource::Open(nsIStreamListener** aStreamListener)
MockMediaResource::Open()
{
mFileHandle = fopen(mFileName, "rb");
if (mFileHandle == nullptr) {

Просмотреть файл

@ -16,15 +16,11 @@ class MockMediaResource : public MediaResource
{
public:
explicit MockMediaResource(const char* aFileName);
nsresult Close() override { return NS_OK; }
void Suspend(bool aCloseImmediately) override {}
void Resume() override {}
already_AddRefed<nsIPrincipal> GetCurrentPrincipal() override
{
return nullptr;
}
void SetReadMode(MediaCacheStream::ReadMode aMode) override {}
void SetPlaybackRate(uint32_t aBytesPerSecond) override {}
nsresult ReadAt(int64_t aOffset, char* aBuffer, uint32_t aCount,
uint32_t* aBytes) override;
// Data stored in file, caching recommended.
@ -32,7 +28,6 @@ public:
int64_t Tell() override { return 0; }
void Pin() override {}
void Unpin() override {}
double GetDownloadRate(bool* aIsReliable) override { return 0; }
int64_t GetLength() override;
int64_t GetNextCachedData(int64_t aOffset) override;
int64_t GetCachedDataEnd(int64_t aOffset) override;
@ -52,7 +47,7 @@ public:
}
bool IsTransportSeekable() override { return true; }
nsresult Open(nsIStreamListener** aStreamListener) override;
nsresult Open();
nsresult GetCachedRanges(MediaByteRangeSet& aRanges) override;
void MockClearBufferedRanges();

Просмотреть файл

@ -308,7 +308,7 @@ protected:
}
for (auto& target: mTargets) {
ASSERT_EQ(NS_OK, target.mResource->Open(nullptr));
ASSERT_EQ(NS_OK, target.mResource->Open());
ASSERT_TRUE(target.mDemuxer->Init());
}
}

Просмотреть файл

@ -43,7 +43,7 @@ public:
, mTaskQueue(new TaskQueue(GetMediaThreadPool(MediaThreadType::PLAYBACK)))
, mIndex(0)
{
EXPECT_EQ(NS_OK, resource->Open(nullptr));
EXPECT_EQ(NS_OK, resource->Open());
}
template<typename Function>

Просмотреть файл

@ -50,8 +50,8 @@ TEST(MediaDataDecoder, H264)
/* DecoderDoctorDiagnostics* */ nullptr)) {
EXPECT_TRUE(true);
} else {
RefPtr<MediaResource> resource = new MockMediaResource("gizmo.mp4");
nsresult rv = resource->Open(nullptr);
RefPtr<MockMediaResource> resource = new MockMediaResource("gizmo.mp4");
nsresult rv = resource->Open();
EXPECT_TRUE(NS_SUCCEEDED(rv));
BenchmarkRunner runner(new Benchmark(new MP4Demuxer(resource)));
@ -64,8 +64,8 @@ TEST(MediaDataDecoder, VP9)
if (!WebMDecoder::IsSupportedType(MediaContainerType(MEDIAMIMETYPE("video/webm")))) {
EXPECT_TRUE(true);
} else {
RefPtr<MediaResource> resource = new MockMediaResource("vp9cake.webm");
nsresult rv = resource->Open(nullptr);
RefPtr<MockMediaResource> resource = new MockMediaResource("vp9cake.webm");
nsresult rv = resource->Open();
EXPECT_TRUE(NS_SUCCEEDED(rv));
BenchmarkRunner runner(new Benchmark(new WebMDemuxer(resource)));

Просмотреть файл

@ -112,4 +112,22 @@ HLSDecoder::Pause()
return MediaDecoder::Pause();
}
void
HLSDecoder::Suspend()
{
MOZ_ASSERT(NS_IsMainThread());
if (mResource) {
mResource->Suspend();
}
}
void
HLSDecoder::Resume()
{
MOZ_ASSERT(NS_IsMainThread());
if (mResource) {
mResource->Resume();
}
}
} // namespace mozilla

Просмотреть файл

@ -41,6 +41,9 @@ public:
void Pause() override;
void Suspend() override;
void Resume() override;
private:
bool CanPlayThroughImpl() override final
{

Просмотреть файл

@ -82,7 +82,8 @@ HLSResource::onError(int aErrorCode)
}
}
void HLSResource::Suspend(bool aCloseImmediately)
void
HLSResource::Suspend()
{
MOZ_ASSERT(NS_IsMainThread(), "Don't call on non-main thread");
HLS_DEBUG("HLSResource", "Should suspend the resource fetching.");

Просмотреть файл

@ -46,17 +46,14 @@ class HLSResource final : public MediaResource
public:
HLSResource(HLSDecoder* aDecoder, nsIChannel* aChannel, nsIURI* aURI);
~HLSResource();
nsresult Close() override { return NS_OK; }
void Suspend(bool aCloseImmediately) override;
void Resume() override;
void Suspend();
void Resume();
void SetReadMode(MediaCacheStream::ReadMode aMode) override { UNIMPLEMENTED(); }
void SetPlaybackRate(uint32_t aBytesPerSecond) override { UNIMPLEMENTED(); }
nsresult ReadAt(int64_t aOffset, char* aBuffer, uint32_t aCount, uint32_t* aBytes) override { UNIMPLEMENTED(); return NS_ERROR_FAILURE; }
bool ShouldCacheReads() override { UNIMPLEMENTED(); return false; }
int64_t Tell() override { UNIMPLEMENTED(); return -1; }
void Pin() override { UNIMPLEMENTED(); }
void Unpin() override { UNIMPLEMENTED(); }
double GetDownloadRate(bool* aIsReliable) override { UNIMPLEMENTED(); *aIsReliable = false; return 0; }
int64_t GetLength() override { UNIMPLEMENTED(); return -1; }
int64_t GetNextCachedData(int64_t aOffset) override { UNIMPLEMENTED(); return -1; }
int64_t GetCachedDataEnd(int64_t aOffset) override { UNIMPLEMENTED(); return -1; }
@ -64,7 +61,6 @@ public:
bool IsSuspendedByCache() override { UNIMPLEMENTED(); return false; }
bool IsSuspended() override { UNIMPLEMENTED(); return false; }
nsresult ReadFromCache(char* aBuffer, int64_t aOffset, uint32_t aCount) override { UNIMPLEMENTED(); return NS_ERROR_FAILURE; }
nsresult Open(nsIStreamListener** aStreamListener) override { UNIMPLEMENTED(); return NS_OK; }
already_AddRefed<nsIPrincipal> GetCurrentPrincipal() override
{

Просмотреть файл

@ -32,17 +32,12 @@ public:
, mEnded(false)
{}
nsresult Close() override { return NS_OK; }
void Suspend(bool aCloseImmediately) override { UNIMPLEMENTED(); }
void Resume() override { UNIMPLEMENTED(); }
void SetReadMode(MediaCacheStream::ReadMode aMode) override { UNIMPLEMENTED(); }
void SetPlaybackRate(uint32_t aBytesPerSecond) override { UNIMPLEMENTED(); }
nsresult ReadAt(int64_t aOffset, char* aBuffer, uint32_t aCount, uint32_t* aBytes) override { UNIMPLEMENTED(); return NS_ERROR_FAILURE; }
bool ShouldCacheReads() override { UNIMPLEMENTED(); return false; }
int64_t Tell() override { UNIMPLEMENTED(); return -1; }
void Pin() override { UNIMPLEMENTED(); }
void Unpin() override { UNIMPLEMENTED(); }
double GetDownloadRate(bool* aIsReliable) override { UNIMPLEMENTED(); *aIsReliable = false; return 0; }
int64_t GetLength() override { UNIMPLEMENTED(); return -1; }
int64_t GetNextCachedData(int64_t aOffset) override { UNIMPLEMENTED(); return -1; }
int64_t GetCachedDataEnd(int64_t aOffset) override { UNIMPLEMENTED(); return -1; }
@ -50,7 +45,6 @@ public:
bool IsSuspendedByCache() override { UNIMPLEMENTED(); return false; }
bool IsSuspended() override { UNIMPLEMENTED(); return false; }
nsresult ReadFromCache(char* aBuffer, int64_t aOffset, uint32_t aCount) override { UNIMPLEMENTED(); return NS_ERROR_FAILURE; }
nsresult Open(nsIStreamListener** aStreamListener) override { UNIMPLEMENTED(); return NS_ERROR_FAILURE; }
already_AddRefed<nsIPrincipal> GetCurrentPrincipal() override
{

Просмотреть файл

@ -39,9 +39,7 @@ class SourceBufferResource final : public MediaResource
{
public:
SourceBufferResource();
nsresult Close() override;
void Suspend(bool aCloseImmediately) override { UNIMPLEMENTED(); }
void Resume() override { UNIMPLEMENTED(); }
nsresult Close();
already_AddRefed<nsIPrincipal> GetCurrentPrincipal() override
{
UNIMPLEMENTED();
@ -51,7 +49,6 @@ public:
{
UNIMPLEMENTED();
}
void SetPlaybackRate(uint32_t aBytesPerSecond) override { UNIMPLEMENTED(); }
nsresult ReadAt(int64_t aOffset,
char* aBuffer,
uint32_t aCount,
@ -61,12 +58,6 @@ public:
int64_t Tell() override { return mOffset; }
void Pin() override { UNIMPLEMENTED(); }
void Unpin() override { UNIMPLEMENTED(); }
double GetDownloadRate(bool* aIsReliable) override
{
UNIMPLEMENTED();
*aIsReliable = false;
return 0;
}
int64_t GetLength() override { return mInputBuffer.GetLength(); }
int64_t GetNextCachedData(int64_t aOffset) override
{
@ -109,11 +100,6 @@ public:
UNIMPLEMENTED();
return true;
}
nsresult Open(nsIStreamListener** aStreamListener) override
{
UNIMPLEMENTED();
return NS_ERROR_FAILURE;
}
nsresult GetCachedRanges(MediaByteRangeSet& aRanges) override
{