2017-06-19 10:50:09 +03: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"
|
2017-08-03 09:05:28 +03:00
|
|
|
#include "DecoderTraits.h"
|
|
|
|
#include "MediaDecoderStateMachine.h"
|
|
|
|
#include "MediaFormatReader.h"
|
2017-06-19 10:50:09 +03:00
|
|
|
#include "MediaResource.h"
|
2017-06-23 09:12:41 +03:00
|
|
|
#include "MediaShutdownManager.h"
|
2017-06-19 10:50:09 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2017-08-04 11:02:40 +03:00
|
|
|
extern LazyLogModule gMediaDecoderLog;
|
|
|
|
#define LOG(x, ...) \
|
|
|
|
MOZ_LOG( \
|
|
|
|
gMediaDecoderLog, LogLevel::Debug, ("Decoder=%p " x, this, ##__VA_ARGS__))
|
|
|
|
|
2017-06-20 13:10:56 +03:00
|
|
|
ChannelMediaDecoder::ResourceCallback::ResourceCallback(
|
|
|
|
AbstractThread* aMainThread)
|
|
|
|
: mAbstractMainThread(aMainThread)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aMainThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ChannelMediaDecoder::ResourceCallback::Connect(ChannelMediaDecoder* aDecoder)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mDecoder = aDecoder;
|
|
|
|
mTimer = do_CreateInstance("@mozilla.org/timer;1");
|
|
|
|
mTimer->SetTarget(mAbstractMainThread->AsEventTarget());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ChannelMediaDecoder::ResourceCallback::Disconnect()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (mDecoder) {
|
|
|
|
mDecoder = nullptr;
|
|
|
|
mTimer->Cancel();
|
|
|
|
mTimer = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaDecoderOwner*
|
|
|
|
ChannelMediaDecoder::ResourceCallback::GetMediaOwner() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return mDecoder ? mDecoder->GetOwner() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ChannelMediaDecoder::ResourceCallback::NotifyNetworkError()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (mDecoder) {
|
|
|
|
mDecoder->NetworkError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
ChannelMediaDecoder::ResourceCallback::TimerCallback(nsITimer* aTimer,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
ResourceCallback* thiz = static_cast<ResourceCallback*>(aClosure);
|
|
|
|
MOZ_ASSERT(thiz->mDecoder);
|
|
|
|
thiz->mDecoder->NotifyDataArrivedInternal();
|
|
|
|
thiz->mTimerArmed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ChannelMediaDecoder::ResourceCallback::NotifyDataArrived()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
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 22:13:25 +03:00
|
|
|
mTimer->InitWithNamedFuncCallback(
|
|
|
|
TimerCallback, this, sDelay, nsITimer::TYPE_ONE_SHOT,
|
|
|
|
"ChannelMediaDecoder::ResourceCallback::TimerCallback");
|
2017-06-20 13:10:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ChannelMediaDecoder::ResourceCallback::NotifyDataEnded(nsresult aStatus)
|
|
|
|
{
|
|
|
|
RefPtr<ResourceCallback> self = this;
|
2017-06-12 22:34:10 +03:00
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
|
|
|
|
"ChannelMediaDecoder::ResourceCallback::NotifyDataEnded",
|
|
|
|
[=]() {
|
2017-06-20 13:10:56 +03:00
|
|
|
if (!self->mDecoder) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self->mDecoder->NotifyDownloadEnded(aStatus);
|
|
|
|
if (NS_SUCCEEDED(aStatus)) {
|
|
|
|
MediaDecoderOwner* owner = self->GetMediaOwner();
|
|
|
|
MOZ_ASSERT(owner);
|
|
|
|
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.
|
2017-08-23 09:32:51 +03:00
|
|
|
owner->NotifySuspendedByCache(true);
|
2017-06-20 13:10:56 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
mAbstractMainThread->Dispatch(r.forget());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ChannelMediaDecoder::ResourceCallback::NotifyPrincipalChanged()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (mDecoder) {
|
|
|
|
mDecoder->NotifyPrincipalChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-08-23 09:32:51 +03:00
|
|
|
ChannelMediaDecoder::ResourceCallback::NotifySuspendedStatusChanged(
|
|
|
|
bool aSuspendedByCache)
|
2017-06-20 13:10:56 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-08-23 09:32:51 +03:00
|
|
|
MediaDecoderOwner* owner = GetMediaOwner();
|
|
|
|
if (owner) {
|
|
|
|
AbstractThread::AutoEnter context(owner->AbstractMainThread());
|
|
|
|
owner->NotifySuspendedByCache(aSuspendedByCache);
|
2017-06-20 13:10:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ChannelMediaDecoder::ResourceCallback::NotifyBytesConsumed(int64_t aBytes,
|
|
|
|
int64_t aOffset)
|
|
|
|
{
|
|
|
|
RefPtr<ResourceCallback> self = this;
|
2017-06-12 22:34:10 +03:00
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction(
|
|
|
|
"ChannelMediaDecoder::ResourceCallback::NotifyBytesConsumed",
|
|
|
|
[=]() {
|
2017-06-20 13:10:56 +03:00
|
|
|
if (self->mDecoder) {
|
|
|
|
self->mDecoder->NotifyBytesConsumed(aBytes, aOffset);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
mAbstractMainThread->Dispatch(r.forget());
|
|
|
|
}
|
|
|
|
|
2017-06-19 10:50:09 +03:00
|
|
|
ChannelMediaDecoder::ChannelMediaDecoder(MediaDecoderInit& aInit)
|
|
|
|
: MediaDecoder(aInit)
|
2017-06-20 13:10:56 +03:00
|
|
|
, mResourceCallback(new ResourceCallback(aInit.mOwner->AbstractMainThread()))
|
2017-08-04 11:02:40 +03:00
|
|
|
, mWatchManager(this, aInit.mOwner->AbstractMainThread())
|
2017-06-20 13:10:56 +03:00
|
|
|
{
|
|
|
|
mResourceCallback->Connect(this);
|
2017-08-04 11:02:40 +03:00
|
|
|
|
|
|
|
// mIgnoreProgressData
|
|
|
|
mWatchManager.Watch(mLogicallySeeking, &ChannelMediaDecoder::SeekingChanged);
|
2017-06-20 13:10:56 +03:00
|
|
|
}
|
|
|
|
|
2017-08-04 10:29:55 +03:00
|
|
|
bool
|
|
|
|
ChannelMediaDecoder::CanClone()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return mResource && mResource->CanClone();
|
|
|
|
}
|
|
|
|
|
2017-08-03 12:39:55 +03:00
|
|
|
already_AddRefed<ChannelMediaDecoder>
|
|
|
|
ChannelMediaDecoder::Clone(MediaDecoderInit& aInit)
|
|
|
|
{
|
2017-08-15 08:52:17 +03:00
|
|
|
if (!mResource || !DecoderTraits::IsSupportedType(aInit.mContainerType)) {
|
2017-08-03 12:47:23 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2017-08-15 08:52:17 +03:00
|
|
|
RefPtr<ChannelMediaDecoder> decoder = new ChannelMediaDecoder(aInit);
|
2017-08-03 12:47:23 +03:00
|
|
|
if (!decoder) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
nsresult rv = decoder->Load(mResource);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
decoder->Shutdown();
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-08-03 12:39:55 +03:00
|
|
|
return decoder.forget();
|
|
|
|
}
|
|
|
|
|
2017-08-03 09:05:28 +03: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 12:31:51 +03:00
|
|
|
init.mMediaDecoderOwnerID = mOwner;
|
2017-08-03 09:05:28 +03:00
|
|
|
mReader = DecoderTraits::CreateReader(ContainerType(), init);
|
|
|
|
return new MediaDecoderStateMachine(this, mReader);
|
|
|
|
}
|
|
|
|
|
2017-06-20 13:10:56 +03:00
|
|
|
void
|
|
|
|
ChannelMediaDecoder::Shutdown()
|
2017-06-19 10:50:09 +03:00
|
|
|
{
|
2017-08-04 11:02:40 +03:00
|
|
|
mWatchManager.Shutdown();
|
2017-06-20 13:10:56 +03:00
|
|
|
mResourceCallback->Disconnect();
|
|
|
|
MediaDecoder::Shutdown();
|
2017-08-07 13:09:56 +03:00
|
|
|
|
|
|
|
// Force any outstanding seek and byterange requests to complete
|
|
|
|
// to prevent shutdown from deadlocking.
|
|
|
|
if (mResource) {
|
|
|
|
mResource->Close();
|
|
|
|
}
|
2017-06-19 10:50:09 +03:00
|
|
|
}
|
|
|
|
|
2017-06-23 09:12:41 +03:00
|
|
|
nsresult
|
|
|
|
ChannelMediaDecoder::OpenResource(nsIStreamListener** aStreamListener)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (aStreamListener) {
|
|
|
|
*aStreamListener = nullptr;
|
|
|
|
}
|
|
|
|
return mResource->Open(aStreamListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2017-06-23 10:39:57 +03:00
|
|
|
ChannelMediaDecoder::Load(nsIChannel* aChannel,
|
|
|
|
bool aIsPrivateBrowsing,
|
|
|
|
nsIStreamListener** aStreamListener)
|
2017-06-23 09:12:41 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2017-06-23 10:39:57 +03:00
|
|
|
MOZ_ASSERT(!mResource);
|
|
|
|
|
|
|
|
mResource =
|
2017-08-04 09:52:22 +03:00
|
|
|
BaseMediaResource::Create(mResourceCallback, aChannel, aIsPrivateBrowsing);
|
2017-06-23 10:39:57 +03:00
|
|
|
if (!mResource) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2017-06-23 09:12:41 +03:00
|
|
|
|
|
|
|
nsresult rv = MediaShutdownManager::Instance().Register(this);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = OpenResource(aStreamListener);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2017-08-09 06:06:29 +03:00
|
|
|
// Set mode to METADATA since we are about to read metadata.
|
|
|
|
mResource->SetReadMode(MediaCacheStream::MODE_METADATA);
|
|
|
|
|
2017-06-23 09:12:41 +03:00
|
|
|
SetStateMachine(CreateStateMachine());
|
|
|
|
NS_ENSURE_TRUE(GetStateMachine(), NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
return InitializeStateMachine();
|
|
|
|
}
|
|
|
|
|
2017-06-23 10:39:57 +03:00
|
|
|
nsresult
|
2017-08-04 09:52:22 +03:00
|
|
|
ChannelMediaDecoder::Load(BaseMediaResource* aOriginal)
|
2017-06-23 10:39:57 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(!mResource);
|
|
|
|
|
|
|
|
mResource = aOriginal->CloneData(mResourceCallback);
|
|
|
|
if (!mResource) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = MediaShutdownManager::Instance().Register(this);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = OpenResource(nullptr);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
SetStateMachine(CreateStateMachine());
|
|
|
|
NS_ENSURE_TRUE(GetStateMachine(), NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
return InitializeStateMachine();
|
|
|
|
}
|
|
|
|
|
2017-08-04 11:02:40 +03:00
|
|
|
void
|
|
|
|
ChannelMediaDecoder::NotifyDownloadEnded(nsresult aStatus)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!IsShutdown());
|
|
|
|
AbstractThread::AutoEnter context(AbstractMainThread());
|
|
|
|
|
|
|
|
LOG("NotifyDownloadEnded, status=%" PRIx32, static_cast<uint32_t>(aStatus));
|
|
|
|
|
|
|
|
if (aStatus == NS_BINDING_ABORTED) {
|
|
|
|
// Download has been cancelled by user.
|
|
|
|
GetOwner()->LoadAborted();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdatePlaybackRate();
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(aStatus)) {
|
|
|
|
// A final progress event will be fired by the MediaResource calling
|
|
|
|
// DownloadSuspended on the element.
|
|
|
|
// Also NotifySuspendedStatusChanged() will be called to update readyState
|
|
|
|
// if download ended with success.
|
|
|
|
} else if (aStatus != NS_BASE_STREAM_CLOSED) {
|
|
|
|
NetworkError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ChannelMediaDecoder::NotifyBytesConsumed(int64_t aBytes, int64_t aOffset)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!IsShutdown());
|
|
|
|
AbstractThread::AutoEnter context(AbstractMainThread());
|
|
|
|
|
|
|
|
if (mIgnoreProgressData) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(GetStateMachine());
|
|
|
|
if (aOffset >= mDecoderPosition) {
|
|
|
|
mPlaybackStatistics.AddBytes(aBytes);
|
|
|
|
}
|
|
|
|
mDecoderPosition = aOffset + aBytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ChannelMediaDecoder::SeekingChanged()
|
|
|
|
{
|
|
|
|
// Stop updating the bytes downloaded for progress notifications when
|
|
|
|
// seeking to prevent wild changes to the progress notification.
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mIgnoreProgressData = mLogicallySeeking;
|
|
|
|
}
|
|
|
|
|
2017-08-07 06:48:43 +03:00
|
|
|
bool
|
|
|
|
ChannelMediaDecoder::CanPlayThroughImpl()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
NS_ENSURE_TRUE(GetStateMachine(), false);
|
|
|
|
return GetStatistics().CanPlayThrough();
|
|
|
|
}
|
|
|
|
|
2017-08-17 10:20:40 +03:00
|
|
|
bool
|
|
|
|
ChannelMediaDecoder::IsLiveStream()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return mResource->IsLiveStream();
|
|
|
|
}
|
|
|
|
|
2017-08-07 08:23:43 +03:00
|
|
|
void
|
|
|
|
ChannelMediaDecoder::OnPlaybackEvent(MediaEventType aEvent)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MediaDecoder::OnPlaybackEvent(aEvent);
|
|
|
|
switch (aEvent) {
|
|
|
|
case MediaEventType::PlaybackStarted:
|
|
|
|
mPlaybackStatistics.Start();
|
|
|
|
break;
|
|
|
|
case MediaEventType::PlaybackStopped:
|
|
|
|
mPlaybackStatistics.Stop();
|
|
|
|
ComputePlaybackRate();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ChannelMediaDecoder::DurationChanged()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
AbstractThread::AutoEnter context(AbstractMainThread());
|
|
|
|
MediaDecoder::DurationChanged();
|
|
|
|
// Duration has changed so we should recompute playback rate
|
|
|
|
UpdatePlaybackRate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ChannelMediaDecoder::DownloadProgressed()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
AbstractThread::AutoEnter context(AbstractMainThread());
|
|
|
|
MediaDecoder::DownloadProgressed();
|
|
|
|
UpdatePlaybackRate();
|
|
|
|
mResource->ThrottleReadahead(ShouldThrottleDownload());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ChannelMediaDecoder::ComputePlaybackRate()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mResource);
|
|
|
|
|
|
|
|
int64_t length = mResource->GetLength();
|
|
|
|
if (mozilla::IsFinite<double>(mDuration) && mDuration > 0 && length >= 0) {
|
|
|
|
mPlaybackRateReliable = true;
|
|
|
|
mPlaybackBytesPerSecond = length / mDuration;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool reliable = false;
|
|
|
|
mPlaybackBytesPerSecond = mPlaybackStatistics.GetRateAtLastStop(&reliable);
|
|
|
|
mPlaybackRateReliable = reliable;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ChannelMediaDecoder::UpdatePlaybackRate()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mResource);
|
|
|
|
|
|
|
|
ComputePlaybackRate();
|
|
|
|
uint32_t rate = mPlaybackBytesPerSecond;
|
|
|
|
|
|
|
|
if (mPlaybackRateReliable) {
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
mResource->SetPlaybackRate(rate);
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaStatistics
|
|
|
|
ChannelMediaDecoder::GetStatistics()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mResource);
|
|
|
|
|
|
|
|
MediaStatistics result;
|
|
|
|
result.mDownloadRate =
|
|
|
|
mResource->GetDownloadRate(&result.mDownloadRateReliable);
|
|
|
|
result.mDownloadPosition = mResource->GetCachedDataEnd(mDecoderPosition);
|
|
|
|
result.mTotalBytes = mResource->GetLength();
|
|
|
|
result.mPlaybackRate = mPlaybackBytesPerSecond;
|
|
|
|
result.mPlaybackRateReliable = mPlaybackRateReliable;
|
|
|
|
result.mDecoderPosition = mDecoderPosition;
|
|
|
|
result.mPlaybackPosition = mPlaybackPosition;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ChannelMediaDecoder::ShouldThrottleDownload()
|
|
|
|
{
|
|
|
|
// We throttle the download if either the throttle override pref is set
|
|
|
|
// (so that we can always throttle in Firefox on mobile) or if the download
|
|
|
|
// is fast enough that there's no concern about playback being interrupted.
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
NS_ENSURE_TRUE(GetStateMachine(), false);
|
|
|
|
|
|
|
|
int64_t length = mResource->GetLength();
|
|
|
|
if (length > 0 &&
|
|
|
|
length <= int64_t(MediaPrefs::MediaMemoryCacheMaxSize()) * 1024) {
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Preferences::GetBool("media.throttle-regardless-of-download-rate",
|
|
|
|
false)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaStatistics stats = GetStatistics();
|
|
|
|
if (!stats.mDownloadRateReliable || !stats.mPlaybackRateReliable) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
uint32_t factor =
|
|
|
|
std::max(2u, Preferences::GetUint("media.throttle-factor", 2));
|
|
|
|
return stats.mDownloadRate > factor * stats.mPlaybackRate;
|
|
|
|
}
|
|
|
|
|
2017-08-24 12:35:24 +03:00
|
|
|
void
|
|
|
|
ChannelMediaDecoder::AddSizeOfResources(ResourceSizes* aSizes)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (mResource) {
|
|
|
|
aSizes->mByteSize += mResource->SizeOfIncludingThis(aSizes->mMallocSizeOf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-24 11:58:06 +03:00
|
|
|
already_AddRefed<nsIPrincipal>
|
|
|
|
ChannelMediaDecoder::GetCurrentPrincipal()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return mResource ? mResource->GetCurrentPrincipal() : nullptr;
|
|
|
|
}
|
|
|
|
|
2017-08-23 19:06:23 +03:00
|
|
|
bool
|
|
|
|
ChannelMediaDecoder::IsTransportSeekable()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return mResource->IsTransportSeekable();
|
|
|
|
}
|
|
|
|
|
2017-08-07 13:09:56 +03: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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ChannelMediaDecoder::Resume()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (mResource) {
|
|
|
|
mResource->Resume();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-24 13:08:37 +03:00
|
|
|
void
|
|
|
|
ChannelMediaDecoder::PinForSeek()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (!mResource || mPinnedForSeek) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mPinnedForSeek = true;
|
|
|
|
mResource->Pin();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ChannelMediaDecoder::UnpinForSeek()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!IsShutdown());
|
|
|
|
if (!mResource || !mPinnedForSeek) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mPinnedForSeek = false;
|
|
|
|
mResource->Unpin();
|
|
|
|
}
|
|
|
|
|
2017-08-09 06:06:29 +03:00
|
|
|
void
|
|
|
|
ChannelMediaDecoder::MetadataLoaded(
|
|
|
|
UniquePtr<MediaInfo> aInfo,
|
|
|
|
UniquePtr<MetadataTags> aTags,
|
|
|
|
MediaDecoderEventVisibility aEventVisibility)
|
|
|
|
{
|
|
|
|
MediaDecoder::MetadataLoaded(Move(aInfo), Move(aTags), aEventVisibility);
|
|
|
|
// Set mode to PLAYBACK after reading metadata.
|
|
|
|
mResource->SetReadMode(MediaCacheStream::MODE_PLAYBACK);
|
|
|
|
}
|
|
|
|
|
2017-06-19 10:50:09 +03:00
|
|
|
} // namespace mozilla
|
2017-08-04 11:02:40 +03:00
|
|
|
|
|
|
|
// avoid redefined macro in unified build
|
|
|
|
#undef LOG
|