2017-06-19 07:50:09 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "ChannelMediaDecoder.h"
|
2019-05-27 16:15:33 +00:00
|
|
|
#include "ChannelMediaResource.h"
|
2017-08-03 06:05:28 +00:00
|
|
|
#include "DecoderTraits.h"
|
|
|
|
#include "MediaDecoderStateMachine.h"
|
|
|
|
#include "MediaFormatReader.h"
|
2017-10-04 18:57:11 +00:00
|
|
|
#include "BaseMediaResource.h"
|
2017-06-23 06:12:41 +00:00
|
|
|
#include "MediaShutdownManager.h"
|
2020-11-23 16:21:38 +00:00
|
|
|
#include "mozilla/Preferences.h"
|
2019-07-26 01:10:23 +00:00
|
|
|
#include "mozilla/StaticPrefs_media.h"
|
2019-05-06 22:43:09 +00:00
|
|
|
#include "VideoUtils.h"
|
2017-06-19 07:50:09 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2017-08-04 08:02:40 +00:00
|
|
|
extern LazyLogModule gMediaDecoderLog;
|
|
|
|
#define LOG(x, ...) \
|
2017-10-13 05:31:59 +00:00
|
|
|
DDMOZ_LOG(gMediaDecoderLog, LogLevel::Debug, x, ##__VA_ARGS__)
|
2017-08-04 08:02:40 +00:00
|
|
|
|
2017-06-20 10:10:56 +00:00
|
|
|
ChannelMediaDecoder::ResourceCallback::ResourceCallback(
|
|
|
|
AbstractThread* aMainThread)
|
|
|
|
: mAbstractMainThread(aMainThread) {
|
|
|
|
MOZ_ASSERT(aMainThread);
|
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 06:55:27 +00:00
|
|
|
DecoderDoctorLogger::LogConstructionAndBase(
|
|
|
|
"ChannelMediaDecoder::ResourceCallback", this,
|
|
|
|
static_cast<const MediaResourceCallback*>(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
ChannelMediaDecoder::ResourceCallback::~ResourceCallback() {
|
|
|
|
DecoderDoctorLogger::LogDestruction("ChannelMediaDecoder::ResourceCallback",
|
|
|
|
this);
|
2017-06-20 10:10:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChannelMediaDecoder::ResourceCallback::Connect(
|
|
|
|
ChannelMediaDecoder* aDecoder) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mDecoder = aDecoder;
|
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 06:55:27 +00:00
|
|
|
DecoderDoctorLogger::LinkParentAndChild(
|
|
|
|
"ChannelMediaDecoder::ResourceCallback", this, "decoder", mDecoder);
|
2017-10-16 06:15:40 +00:00
|
|
|
mTimer = NS_NewTimer(mAbstractMainThread->AsEventTarget());
|
2017-06-20 10:10:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChannelMediaDecoder::ResourceCallback::Disconnect() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (mDecoder) {
|
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 06:55:27 +00:00
|
|
|
DecoderDoctorLogger::UnlinkParentAndChild(
|
|
|
|
"ChannelMediaDecoder::ResourceCallback", this, mDecoder);
|
2017-06-20 10:10:56 +00:00
|
|
|
mDecoder = nullptr;
|
|
|
|
mTimer->Cancel();
|
|
|
|
mTimer = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-06 07:12:34 +00:00
|
|
|
AbstractThread* ChannelMediaDecoder::ResourceCallback::AbstractMainThread()
|
|
|
|
const {
|
|
|
|
return mAbstractMainThread;
|
|
|
|
}
|
|
|
|
|
2017-06-20 10:10:56 +00:00
|
|
|
MediaDecoderOwner* ChannelMediaDecoder::ResourceCallback::GetMediaOwner()
|
|
|
|
const {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return mDecoder ? mDecoder->GetOwner() : nullptr;
|
|
|
|
}
|
|
|
|
|
2017-11-17 03:07:30 +00:00
|
|
|
void ChannelMediaDecoder::ResourceCallback::NotifyNetworkError(
|
|
|
|
const MediaResult& aError) {
|
2017-06-20 10:10:56 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
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 06:55:27 +00:00
|
|
|
DDLOGEX2("ChannelMediaDecoder::ResourceCallback", this, DDLogCategory::Log,
|
|
|
|
"network_error", aError);
|
2017-06-20 10:10:56 +00:00
|
|
|
if (mDecoder) {
|
2017-11-17 03:07:30 +00:00
|
|
|
mDecoder->NetworkError(aError);
|
2017-06-20 10:10:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-25 22:05:29 +00:00
|
|
|
/* static */
|
|
|
|
void ChannelMediaDecoder::ResourceCallback::TimerCallback(nsITimer* aTimer,
|
|
|
|
void* aClosure) {
|
2017-06-20 10:10:56 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
ResourceCallback* thiz = static_cast<ResourceCallback*>(aClosure);
|
|
|
|
MOZ_ASSERT(thiz->mDecoder);
|
2017-11-30 02:51:56 +00:00
|
|
|
thiz->mDecoder->NotifyReaderDataArrived();
|
2017-06-20 10:10:56 +00:00
|
|
|
thiz->mTimerArmed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChannelMediaDecoder::ResourceCallback::NotifyDataArrived() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
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 06:55:27 +00:00
|
|
|
DDLOGEX2("ChannelMediaDecoder::ResourceCallback", this, DDLogCategory::Log,
|
|
|
|
"data_arrived", true);
|
|
|
|
|
2017-06-20 10:10:56 +00:00
|
|
|
if (!mDecoder) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mDecoder->DownloadProgressed();
|
|
|
|
|
|
|
|
if (mTimerArmed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// In situations where these notifications come from stochastic network
|
|
|
|
// activity, we can save significant computation by throttling the
|
|
|
|
// calls to MediaDecoder::NotifyDataArrived() which will update the buffer
|
|
|
|
// ranges of the reader.
|
|
|
|
mTimerArmed = true;
|
2017-06-29 19:13:25 +00:00
|
|
|
mTimer->InitWithNamedFuncCallback(
|
|
|
|
TimerCallback, this, sDelay, nsITimer::TYPE_ONE_SHOT,
|
|
|
|
"ChannelMediaDecoder::ResourceCallback::TimerCallback");
|
2017-06-20 10:10:56 +00:00
|
|
|
}
|
2018-11-19 13:25:37 +00:00
|
|
|
|
2017-06-20 10:10:56 +00:00
|
|
|
void ChannelMediaDecoder::ResourceCallback::NotifyDataEnded(nsresult aStatus) {
|
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 06:55:27 +00:00
|
|
|
DDLOGEX2("ChannelMediaDecoder::ResourceCallback", this, DDLogCategory::Log,
|
|
|
|
"data_ended", aStatus);
|
2017-11-10 06:27:59 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-11-15 03:07:06 +00:00
|
|
|
if (mDecoder) {
|
|
|
|
mDecoder->NotifyDownloadEnded(aStatus);
|
2017-11-10 06:27:59 +00:00
|
|
|
}
|
2017-06-20 10:10:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChannelMediaDecoder::ResourceCallback::NotifyPrincipalChanged() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
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 06:55:27 +00:00
|
|
|
DDLOGEX2("ChannelMediaDecoder::ResourceCallback", this, DDLogCategory::Log,
|
|
|
|
"principal_changed", true);
|
2017-06-20 10:10:56 +00:00
|
|
|
if (mDecoder) {
|
|
|
|
mDecoder->NotifyPrincipalChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 01:08:30 +00:00
|
|
|
void ChannelMediaDecoder::NotifyPrincipalChanged() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MediaDecoder::NotifyPrincipalChanged();
|
|
|
|
if (!mInitialChannelPrincipalKnown) {
|
|
|
|
// We'll receive one notification when the channel's initial principal
|
|
|
|
// is known, after all HTTP redirects have resolved. This isn't really a
|
|
|
|
// principal change, so return here to avoid the mSameOriginMedia check
|
|
|
|
// below.
|
|
|
|
mInitialChannelPrincipalKnown = true;
|
|
|
|
return;
|
|
|
|
}
|
2018-03-12 22:43:31 +00:00
|
|
|
if (!mSameOriginMedia &&
|
2018-03-05 20:55:03 +00:00
|
|
|
Preferences::GetBool("media.block-midflight-redirects", true)) {
|
|
|
|
// Block mid-flight redirects to non CORS same origin destinations.
|
|
|
|
// See bugs 1441153, 1443942.
|
2018-03-01 01:08:30 +00:00
|
|
|
LOG("ChannnelMediaDecoder prohibited cross origin redirect blocked.");
|
|
|
|
NetworkError(MediaResult(NS_ERROR_DOM_BAD_URI,
|
|
|
|
"Prohibited cross origin redirect blocked"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-23 06:32:51 +00:00
|
|
|
void ChannelMediaDecoder::ResourceCallback::NotifySuspendedStatusChanged(
|
|
|
|
bool aSuspendedByCache) {
|
2017-06-20 10:10:56 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
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 06:55:27 +00:00
|
|
|
DDLOGEX2("ChannelMediaDecoder::ResourceCallback", this, DDLogCategory::Log,
|
|
|
|
"suspended_status_changed", aSuspendedByCache);
|
2017-08-23 06:32:51 +00:00
|
|
|
MediaDecoderOwner* owner = GetMediaOwner();
|
|
|
|
if (owner) {
|
|
|
|
owner->NotifySuspendedByCache(aSuspendedByCache);
|
2017-06-20 10:10:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-19 07:50:09 +00:00
|
|
|
ChannelMediaDecoder::ChannelMediaDecoder(MediaDecoderInit& aInit)
|
|
|
|
: MediaDecoder(aInit),
|
2017-06-20 10:10:56 +00:00
|
|
|
mResourceCallback(
|
|
|
|
new ResourceCallback(aInit.mOwner->AbstractMainThread())) {
|
|
|
|
mResourceCallback->Connect(this);
|
|
|
|
}
|
|
|
|
|
2017-10-16 20:55:26 +00:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<ChannelMediaDecoder> ChannelMediaDecoder::Create(
|
|
|
|
MediaDecoderInit& aInit, DecoderDoctorDiagnostics* aDiagnostics) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
RefPtr<ChannelMediaDecoder> decoder;
|
2021-03-01 20:30:58 +00:00
|
|
|
if (DecoderTraits::CanHandleContainerType(aInit.mContainerType,
|
|
|
|
aDiagnostics) != CANPLAY_NO) {
|
2017-10-16 20:55:26 +00:00
|
|
|
decoder = new ChannelMediaDecoder(aInit);
|
|
|
|
return decoder.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-08-04 07:29:55 +00:00
|
|
|
bool ChannelMediaDecoder::CanClone() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return mResource && mResource->CanClone();
|
|
|
|
}
|
|
|
|
|
2017-08-03 09:39:55 +00:00
|
|
|
already_AddRefed<ChannelMediaDecoder> ChannelMediaDecoder::Clone(
|
|
|
|
MediaDecoderInit& aInit) {
|
2021-03-01 20:30:58 +00:00
|
|
|
if (!mResource || DecoderTraits::CanHandleContainerType(
|
|
|
|
aInit.mContainerType, nullptr) == CANPLAY_NO) {
|
2017-08-03 09:47:23 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2017-08-15 05:52:17 +00:00
|
|
|
RefPtr<ChannelMediaDecoder> decoder = new ChannelMediaDecoder(aInit);
|
2017-08-03 09:47:23 +00:00
|
|
|
nsresult rv = decoder->Load(mResource);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
decoder->Shutdown();
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-08-03 09:39:55 +00:00
|
|
|
return decoder.forget();
|
|
|
|
}
|
|
|
|
|
2017-08-03 06:05:28 +00:00
|
|
|
MediaDecoderStateMachine* ChannelMediaDecoder::CreateStateMachine() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MediaFormatReaderInit init;
|
|
|
|
init.mVideoFrameContainer = GetVideoFrameContainer();
|
|
|
|
init.mKnowsCompositor = GetCompositor();
|
|
|
|
init.mCrashHelper = GetOwner()->CreateGMPCrashHelper();
|
|
|
|
init.mFrameStats = mFrameStats;
|
|
|
|
init.mResource = mResource;
|
2017-08-31 09:31:51 +00:00
|
|
|
init.mMediaDecoderOwnerID = mOwner;
|
2017-08-03 06:05:28 +00:00
|
|
|
mReader = DecoderTraits::CreateReader(ContainerType(), init);
|
|
|
|
return new MediaDecoderStateMachine(this, mReader);
|
|
|
|
}
|
|
|
|
|
2017-06-20 10:10:56 +00:00
|
|
|
void ChannelMediaDecoder::Shutdown() {
|
|
|
|
mResourceCallback->Disconnect();
|
|
|
|
MediaDecoder::Shutdown();
|
2017-08-07 10:09:56 +00:00
|
|
|
|
|
|
|
if (mResource) {
|
2019-11-13 22:40:14 +00:00
|
|
|
// Force any outstanding seek and byterange requests to complete
|
|
|
|
// to prevent shutdown from deadlocking.
|
|
|
|
mResourceClosePromise = mResource->Close();
|
2017-08-07 10:09:56 +00:00
|
|
|
}
|
2017-06-19 07:50:09 +00:00
|
|
|
}
|
|
|
|
|
2019-11-13 22:40:14 +00:00
|
|
|
void ChannelMediaDecoder::ShutdownInternal() {
|
|
|
|
if (!mResourceClosePromise) {
|
|
|
|
MediaShutdownManager::Instance().Unregister(this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mResourceClosePromise->Then(
|
|
|
|
AbstractMainThread(), __func__,
|
|
|
|
[self = RefPtr<ChannelMediaDecoder>(this)] {
|
|
|
|
MediaShutdownManager::Instance().Unregister(self);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-23 07:39:57 +00:00
|
|
|
nsresult ChannelMediaDecoder::Load(nsIChannel* aChannel,
|
|
|
|
bool aIsPrivateBrowsing,
|
|
|
|
nsIStreamListener** aStreamListener) {
|
2017-06-23 06:12:41 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-06-23 07:39:57 +00:00
|
|
|
MOZ_ASSERT(!mResource);
|
2017-09-05 09:39:57 +00:00
|
|
|
MOZ_ASSERT(aStreamListener);
|
2017-06-23 07:39:57 +00:00
|
|
|
|
2017-08-04 06:52:22 +00:00
|
|
|
mResource = BaseMediaResource::Create(mResourceCallback, aChannel,
|
|
|
|
aIsPrivateBrowsing);
|
2017-06-23 07:39:57 +00:00
|
|
|
if (!mResource) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
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 06:55:27 +00:00
|
|
|
DDLINKCHILD("resource", mResource.get());
|
2017-06-23 06:12:41 +00:00
|
|
|
|
|
|
|
nsresult rv = MediaShutdownManager::Instance().Register(this);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-09-05 09:39:57 +00:00
|
|
|
rv = mResource->Open(aStreamListener);
|
2017-06-23 06:12:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
SetStateMachine(CreateStateMachine());
|
|
|
|
NS_ENSURE_TRUE(GetStateMachine(), NS_ERROR_FAILURE);
|
|
|
|
|
2017-12-06 08:00:20 +00:00
|
|
|
GetStateMachine()->DispatchIsLiveStream(mResource->IsLiveStream());
|
|
|
|
|
2017-06-23 06:12:41 +00:00
|
|
|
return InitializeStateMachine();
|
|
|
|
}
|
|
|
|
|
2017-08-04 06:52:22 +00:00
|
|
|
nsresult ChannelMediaDecoder::Load(BaseMediaResource* aOriginal) {
|
2017-06-23 07:39:57 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!mResource);
|
|
|
|
|
|
|
|
mResource = aOriginal->CloneData(mResourceCallback);
|
|
|
|
if (!mResource) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
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 06:55:27 +00:00
|
|
|
DDLINKCHILD("resource", mResource.get());
|
2017-06-23 07:39:57 +00:00
|
|
|
|
|
|
|
nsresult rv = MediaShutdownManager::Instance().Register(this);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetStateMachine(CreateStateMachine());
|
|
|
|
NS_ENSURE_TRUE(GetStateMachine(), NS_ERROR_FAILURE);
|
|
|
|
|
2017-12-06 08:00:20 +00:00
|
|
|
GetStateMachine()->DispatchIsLiveStream(mResource->IsLiveStream());
|
|
|
|
|
2017-06-23 07:39:57 +00:00
|
|
|
return InitializeStateMachine();
|
|
|
|
}
|
|
|
|
|
2017-08-04 08:02:40 +00:00
|
|
|
void ChannelMediaDecoder::NotifyDownloadEnded(nsresult aStatus) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!IsShutdown());
|
|
|
|
|
|
|
|
LOG("NotifyDownloadEnded, status=%" PRIx32, static_cast<uint32_t>(aStatus));
|
|
|
|
|
2017-12-06 08:00:20 +00:00
|
|
|
if (NS_SUCCEEDED(aStatus)) {
|
|
|
|
// Download ends successfully. This is a stream with a finite length.
|
|
|
|
GetStateMachine()->DispatchIsLiveStream(false);
|
|
|
|
}
|
|
|
|
|
2017-11-15 03:07:06 +00:00
|
|
|
MediaDecoderOwner* owner = GetOwner();
|
2017-11-15 03:24:22 +00:00
|
|
|
if (NS_SUCCEEDED(aStatus) || aStatus == NS_BASE_STREAM_CLOSED) {
|
2017-12-05 06:13:22 +00:00
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
|
|
|
|
"ChannelMediaDecoder::UpdatePlaybackRate",
|
2017-12-05 07:21:45 +00:00
|
|
|
[stats = mPlaybackStatistics,
|
|
|
|
res = RefPtr<BaseMediaResource>(mResource), duration = mDuration]() {
|
|
|
|
auto rate = ComputePlaybackRate(stats, res, duration);
|
|
|
|
UpdatePlaybackRate(rate, res);
|
|
|
|
});
|
2017-12-05 06:13:22 +00:00
|
|
|
nsresult rv = GetStateMachine()->OwnerThread()->Dispatch(r.forget());
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
|
2017-12-23 21:40:13 +00:00
|
|
|
Unused << rv;
|
2017-11-15 03:07:06 +00:00
|
|
|
owner->DownloadSuspended();
|
|
|
|
// NotifySuspendedStatusChanged will tell the element that download
|
|
|
|
// has been suspended "by the cache", which is true since we never
|
|
|
|
// download anything. The element can then transition to HAVE_ENOUGH_DATA.
|
|
|
|
owner->NotifySuspendedByCache(true);
|
2017-11-15 03:24:22 +00:00
|
|
|
} else if (aStatus == NS_BINDING_ABORTED) {
|
|
|
|
// Download has been cancelled by user.
|
|
|
|
owner->LoadAborted();
|
|
|
|
} else {
|
2017-11-17 03:07:30 +00:00
|
|
|
NetworkError(MediaResult(aStatus, "Download aborted"));
|
2017-08-04 08:02:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-07 03:48:43 +00:00
|
|
|
bool ChannelMediaDecoder::CanPlayThroughImpl() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-12-05 07:06:34 +00:00
|
|
|
return mCanPlayThrough;
|
2017-08-07 03:48:43 +00:00
|
|
|
}
|
|
|
|
|
2017-12-01 02:33:44 +00:00
|
|
|
void ChannelMediaDecoder::OnPlaybackEvent(MediaPlaybackEvent&& aEvent) {
|
2017-08-07 05:23:43 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-12-01 02:33:44 +00:00
|
|
|
switch (aEvent.mType) {
|
|
|
|
case MediaPlaybackEvent::PlaybackStarted:
|
2017-12-01 03:26:03 +00:00
|
|
|
mPlaybackPosition = aEvent.mData.as<int64_t>();
|
2017-08-07 05:23:43 +00:00
|
|
|
mPlaybackStatistics.Start();
|
|
|
|
break;
|
2017-12-01 03:26:03 +00:00
|
|
|
case MediaPlaybackEvent::PlaybackProgressed: {
|
|
|
|
int64_t newPos = aEvent.mData.as<int64_t>();
|
|
|
|
mPlaybackStatistics.AddBytes(newPos - mPlaybackPosition);
|
|
|
|
mPlaybackPosition = newPos;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MediaPlaybackEvent::PlaybackStopped: {
|
|
|
|
int64_t newPos = aEvent.mData.as<int64_t>();
|
|
|
|
mPlaybackStatistics.AddBytes(newPos - mPlaybackPosition);
|
|
|
|
mPlaybackPosition = newPos;
|
2017-08-07 05:23:43 +00:00
|
|
|
mPlaybackStatistics.Stop();
|
|
|
|
break;
|
2017-12-01 03:26:03 +00:00
|
|
|
}
|
2017-08-07 05:23:43 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2018-05-30 19:15:35 +00:00
|
|
|
MediaDecoder::OnPlaybackEvent(std::move(aEvent));
|
2017-08-07 05:23:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChannelMediaDecoder::DurationChanged() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MediaDecoder::DurationChanged();
|
|
|
|
// Duration has changed so we should recompute playback rate
|
2017-12-05 06:13:22 +00:00
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
|
|
|
|
"ChannelMediaDecoder::UpdatePlaybackRate",
|
2017-12-05 07:21:45 +00:00
|
|
|
[stats = mPlaybackStatistics, res = RefPtr<BaseMediaResource>(mResource),
|
|
|
|
duration = mDuration]() {
|
|
|
|
auto rate = ComputePlaybackRate(stats, res, duration);
|
|
|
|
UpdatePlaybackRate(rate, res);
|
|
|
|
});
|
2017-12-05 06:13:22 +00:00
|
|
|
nsresult rv = GetStateMachine()->OwnerThread()->Dispatch(r.forget());
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
|
2017-12-23 21:40:13 +00:00
|
|
|
Unused << rv;
|
2017-08-07 05:23:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChannelMediaDecoder::DownloadProgressed() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-11-30 03:21:14 +00:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!IsShutdown());
|
2017-12-05 06:13:22 +00:00
|
|
|
|
2018-01-08 06:51:57 +00:00
|
|
|
GetOwner()->DownloadProgressed();
|
|
|
|
|
2017-12-05 07:06:34 +00:00
|
|
|
using StatsPromise = MozPromise<MediaStatistics, bool, true>;
|
|
|
|
InvokeAsync(GetStateMachine()->OwnerThread(), __func__,
|
2017-12-05 07:21:45 +00:00
|
|
|
[playbackStats = mPlaybackStatistics,
|
2017-12-05 07:06:34 +00:00
|
|
|
res = RefPtr<BaseMediaResource>(mResource), duration = mDuration,
|
|
|
|
pos = mPlaybackPosition]() {
|
2017-12-05 07:21:45 +00:00
|
|
|
auto rate = ComputePlaybackRate(playbackStats, res, duration);
|
2017-12-05 07:06:34 +00:00
|
|
|
UpdatePlaybackRate(rate, res);
|
|
|
|
MediaStatistics stats = GetStatistics(rate, res, pos);
|
|
|
|
return StatsPromise::CreateAndResolve(stats, __func__);
|
|
|
|
})
|
|
|
|
->Then(
|
|
|
|
mAbstractMainThread, __func__,
|
|
|
|
[=,
|
|
|
|
self = RefPtr<ChannelMediaDecoder>(this)](MediaStatistics aStats) {
|
|
|
|
if (IsShutdown()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mCanPlayThrough = aStats.CanPlayThrough();
|
|
|
|
GetStateMachine()->DispatchCanPlayThrough(mCanPlayThrough);
|
|
|
|
mResource->ThrottleReadahead(ShouldThrottleDownload(aStats));
|
2018-01-08 06:51:57 +00:00
|
|
|
// Update readyState since mCanPlayThrough might have changed.
|
|
|
|
GetOwner()->UpdateReadyState();
|
2017-12-05 07:06:34 +00:00
|
|
|
},
|
|
|
|
[]() { MOZ_ASSERT_UNREACHABLE("Promise not resolved"); });
|
2017-08-07 05:23:43 +00:00
|
|
|
}
|
|
|
|
|
2017-12-05 07:21:45 +00:00
|
|
|
/* static */ ChannelMediaDecoder::PlaybackRateInfo
|
|
|
|
ChannelMediaDecoder::ComputePlaybackRate(const MediaChannelStatistics& aStats,
|
|
|
|
BaseMediaResource* aResource,
|
|
|
|
double aDuration) {
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2017-08-07 05:23:43 +00:00
|
|
|
|
2017-12-05 07:21:45 +00:00
|
|
|
int64_t length = aResource->GetLength();
|
2019-05-01 23:13:34 +00:00
|
|
|
if (mozilla::IsFinite<double>(aDuration) && aDuration > 0 && length >= 0 &&
|
|
|
|
length / aDuration < UINT32_MAX) {
|
2017-12-05 07:21:45 +00:00
|
|
|
return {uint32_t(length / aDuration), true};
|
2017-08-07 05:23:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool reliable = false;
|
2017-12-05 07:21:45 +00:00
|
|
|
uint32_t rate = aStats.GetRate(&reliable);
|
2017-12-01 07:05:25 +00:00
|
|
|
return {rate, reliable};
|
2017-08-07 05:23:43 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 22:05:29 +00:00
|
|
|
/* static */
|
|
|
|
void ChannelMediaDecoder::UpdatePlaybackRate(const PlaybackRateInfo& aInfo,
|
|
|
|
BaseMediaResource* aResource) {
|
2017-12-05 06:13:22 +00:00
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2017-08-07 05:23:43 +00:00
|
|
|
|
2017-12-01 07:05:25 +00:00
|
|
|
uint32_t rate = aInfo.mRate;
|
2017-08-07 05:23:43 +00:00
|
|
|
|
2017-12-01 07:05:25 +00:00
|
|
|
if (aInfo.mReliable) {
|
2017-08-07 05:23:43 +00:00
|
|
|
// Avoid passing a zero rate
|
|
|
|
rate = std::max(rate, 1u);
|
|
|
|
} else {
|
|
|
|
// Set a minimum rate of 10,000 bytes per second ... sometimes we just
|
|
|
|
// don't have good data
|
|
|
|
rate = std::max(rate, 10000u);
|
|
|
|
}
|
|
|
|
|
2017-12-05 06:13:22 +00:00
|
|
|
aResource->SetPlaybackRate(rate);
|
2017-08-07 05:23:43 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 22:05:29 +00:00
|
|
|
/* static */
|
|
|
|
MediaStatistics ChannelMediaDecoder::GetStatistics(
|
2017-12-05 07:06:34 +00:00
|
|
|
const PlaybackRateInfo& aInfo, BaseMediaResource* aRes,
|
|
|
|
int64_t aPlaybackPosition) {
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2017-08-07 05:23:43 +00:00
|
|
|
|
|
|
|
MediaStatistics result;
|
2017-12-05 07:06:34 +00:00
|
|
|
result.mDownloadRate = aRes->GetDownloadRate(&result.mDownloadRateReliable);
|
|
|
|
result.mDownloadPosition = aRes->GetCachedDataEnd(aPlaybackPosition);
|
|
|
|
result.mTotalBytes = aRes->GetLength();
|
2017-12-01 07:05:25 +00:00
|
|
|
result.mPlaybackRate = aInfo.mRate;
|
|
|
|
result.mPlaybackRateReliable = aInfo.mReliable;
|
2017-12-05 07:06:34 +00:00
|
|
|
result.mPlaybackPosition = aPlaybackPosition;
|
2017-08-07 05:23:43 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-12-01 06:36:35 +00:00
|
|
|
bool ChannelMediaDecoder::ShouldThrottleDownload(
|
|
|
|
const MediaStatistics& aStats) {
|
2017-08-07 05:23:43 +00:00
|
|
|
// We throttle the download if either the throttle override pref is set
|
2019-05-06 22:43:09 +00:00
|
|
|
// (so that we always throttle at the readahead limit on mobile if using
|
|
|
|
// a cellular network) or if the download is fast enough that there's no
|
|
|
|
// concern about playback being interrupted.
|
2017-08-07 05:23:43 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
NS_ENSURE_TRUE(GetStateMachine(), false);
|
|
|
|
|
2017-12-01 06:36:35 +00:00
|
|
|
int64_t length = aStats.mTotalBytes;
|
2017-08-07 05:23:43 +00:00
|
|
|
if (length > 0 &&
|
2019-06-28 04:09:05 +00:00
|
|
|
length <= int64_t(StaticPrefs::media_memory_cache_max_size()) * 1024) {
|
2017-08-07 05:23:43 +00:00
|
|
|
// Don't throttle the download of small resources. This is to speed
|
|
|
|
// up seeking, as seeks into unbuffered ranges would require starting
|
|
|
|
// up a new HTTP transaction, which adds latency.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-06 22:43:09 +00:00
|
|
|
if (OnCellularConnection() &&
|
|
|
|
Preferences::GetBool(
|
|
|
|
"media.throttle-cellular-regardless-of-download-rate", false)) {
|
2017-08-07 05:23:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-01 06:36:35 +00:00
|
|
|
if (!aStats.mDownloadRateReliable || !aStats.mPlaybackRateReliable) {
|
2017-08-07 05:23:43 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
uint32_t factor =
|
|
|
|
std::max(2u, Preferences::GetUint("media.throttle-factor", 2));
|
2017-12-01 06:36:35 +00:00
|
|
|
return aStats.mDownloadRate > factor * aStats.mPlaybackRate;
|
2017-08-07 05:23:43 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 09:35:24 +00:00
|
|
|
void ChannelMediaDecoder::AddSizeOfResources(ResourceSizes* aSizes) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (mResource) {
|
|
|
|
aSizes->mByteSize += mResource->SizeOfIncludingThis(aSizes->mMallocSizeOf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-24 08:58:06 +00:00
|
|
|
already_AddRefed<nsIPrincipal> ChannelMediaDecoder::GetCurrentPrincipal() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return mResource ? mResource->GetCurrentPrincipal() : nullptr;
|
|
|
|
}
|
|
|
|
|
2019-06-04 06:32:37 +00:00
|
|
|
bool ChannelMediaDecoder::HadCrossOriginRedirects() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return mResource ? mResource->HadCrossOriginRedirects() : false;
|
|
|
|
}
|
|
|
|
|
2017-08-23 16:06:23 +00:00
|
|
|
bool ChannelMediaDecoder::IsTransportSeekable() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return mResource->IsTransportSeekable();
|
|
|
|
}
|
|
|
|
|
2017-08-07 10:09:56 +00:00
|
|
|
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);
|
|
|
|
}
|
2020-02-24 17:48:25 +00:00
|
|
|
MediaDecoder::Suspend();
|
2017-08-07 10:09:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChannelMediaDecoder::Resume() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (mResource) {
|
|
|
|
mResource->Resume();
|
|
|
|
}
|
2020-02-24 17:48:25 +00:00
|
|
|
MediaDecoder::Resume();
|
2017-08-07 10:09:56 +00:00
|
|
|
}
|
|
|
|
|
2017-08-09 03:06:29 +00:00
|
|
|
void ChannelMediaDecoder::MetadataLoaded(
|
|
|
|
UniquePtr<MediaInfo> aInfo, UniquePtr<MetadataTags> aTags,
|
|
|
|
MediaDecoderEventVisibility aEventVisibility) {
|
2018-05-30 19:15:35 +00:00
|
|
|
MediaDecoder::MetadataLoaded(std::move(aInfo), std::move(aTags),
|
|
|
|
aEventVisibility);
|
2017-08-09 03:06:29 +00:00
|
|
|
// Set mode to PLAYBACK after reading metadata.
|
|
|
|
mResource->SetReadMode(MediaCacheStream::MODE_PLAYBACK);
|
|
|
|
}
|
|
|
|
|
2019-05-27 16:15:33 +00:00
|
|
|
void ChannelMediaDecoder::GetDebugInfo(dom::MediaDecoderDebugInfo& aInfo) {
|
|
|
|
MediaDecoder::GetDebugInfo(aInfo);
|
2017-10-25 05:57:39 +00:00
|
|
|
if (mResource) {
|
2019-05-27 16:15:33 +00:00
|
|
|
mResource->GetDebugInfo(aInfo.mResource);
|
2017-10-25 05:57:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-19 07:50:09 +00:00
|
|
|
} // namespace mozilla
|
2017-08-04 08:02:40 +00:00
|
|
|
|
|
|
|
// avoid redefined macro in unified build
|
|
|
|
#undef LOG
|