2015-05-18 08:40:32 +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/. */
|
|
|
|
|
2016-11-28 13:08:01 +03:00
|
|
|
#include "AutoTaskQueue.h"
|
2015-05-18 08:40:32 +03:00
|
|
|
#include "Layers.h"
|
|
|
|
#include "MediaData.h"
|
|
|
|
#include "MediaInfo.h"
|
|
|
|
#include "MediaFormatReader.h"
|
2016-10-28 12:43:19 +03:00
|
|
|
#include "MediaPrefs.h"
|
2015-05-18 08:40:32 +03:00
|
|
|
#include "MediaResource.h"
|
|
|
|
#include "VideoUtils.h"
|
2016-05-27 09:33:48 +03:00
|
|
|
#include "VideoFrameContainer.h"
|
2016-11-28 13:08:01 +03:00
|
|
|
#include "mozilla/dom/HTMLMediaElement.h"
|
2016-10-07 11:13:33 +03:00
|
|
|
#include "mozilla/layers/ShadowLayers.h"
|
2016-11-29 08:03:36 +03:00
|
|
|
#include "mozilla/AbstractThread.h"
|
2016-11-28 13:08:01 +03:00
|
|
|
#include "mozilla/CDMProxy.h"
|
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "mozilla/Telemetry.h"
|
|
|
|
#include "mozilla/SharedThreadPool.h"
|
|
|
|
#include "mozilla/SyncRunnable.h"
|
2017-01-26 15:56:46 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2016-11-28 13:08:01 +03:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsPrintfCString.h"
|
|
|
|
#include "nsSize.h"
|
2015-05-18 08:40:32 +03:00
|
|
|
|
|
|
|
#include <algorithm>
|
2016-10-28 12:43:19 +03:00
|
|
|
#include <queue>
|
2015-05-18 08:40:32 +03:00
|
|
|
|
2015-05-31 07:18:12 +03:00
|
|
|
using namespace mozilla::media;
|
|
|
|
|
2015-05-18 08:40:32 +03:00
|
|
|
using mozilla::layers::Image;
|
|
|
|
using mozilla::layers::LayerManager;
|
|
|
|
using mozilla::layers::LayersBackend;
|
|
|
|
|
2015-11-15 16:49:01 +03:00
|
|
|
static mozilla::LazyLogModule sFormatDecoderLog("MediaFormatReader");
|
2016-07-24 15:30:07 +03:00
|
|
|
mozilla::LazyLogModule gMediaDemuxerLog("MediaDemuxer");
|
2015-11-15 16:49:01 +03:00
|
|
|
|
|
|
|
#define LOG(arg, ...) MOZ_LOG(sFormatDecoderLog, mozilla::LogLevel::Debug, ("MediaFormatReader(%p)::%s: " arg, this, __func__, ##__VA_ARGS__))
|
|
|
|
#define LOGV(arg, ...) MOZ_LOG(sFormatDecoderLog, mozilla::LogLevel::Verbose, ("MediaFormatReader(%p)::%s: " arg, this, __func__, ##__VA_ARGS__))
|
2015-05-18 08:40:32 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2016-10-28 12:43:19 +03:00
|
|
|
/**
|
|
|
|
* This is a singleton which controls the number of decoders that can be
|
|
|
|
* created concurrently. Before calling PDMFactory::CreateDecoder(), Alloc()
|
|
|
|
* must be called to get a token object as a permission to create a decoder.
|
|
|
|
* The token should stay alive until Shutdown() is called on the decoder.
|
|
|
|
* The destructor of the token will restore the decoder count so it is available
|
|
|
|
* for next calls of Alloc().
|
|
|
|
*/
|
|
|
|
class DecoderAllocPolicy
|
|
|
|
{
|
|
|
|
using TrackType = TrackInfo::TrackType;
|
|
|
|
|
|
|
|
public:
|
|
|
|
class Token
|
|
|
|
{
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Token)
|
|
|
|
protected:
|
|
|
|
virtual ~Token() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
using Promise = MozPromise<RefPtr<Token>, bool, true>;
|
|
|
|
|
|
|
|
// Acquire a token for decoder creation. Thread-safe.
|
2016-11-21 17:46:40 +03:00
|
|
|
auto Alloc() -> RefPtr<Promise>;
|
2016-10-28 12:43:19 +03:00
|
|
|
|
|
|
|
// Called by ClearOnShutdown() to delete the singleton.
|
|
|
|
void operator=(decltype(nullptr));
|
|
|
|
|
2016-11-21 17:46:40 +03:00
|
|
|
// Get the singleton for the given track type. Thread-safe.
|
|
|
|
static DecoderAllocPolicy& Instance(TrackType aTrack);
|
2016-10-28 12:43:19 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
class AutoDeallocToken;
|
|
|
|
using PromisePrivate = Promise::Private;
|
2016-11-22 20:29:45 +03:00
|
|
|
explicit DecoderAllocPolicy(TrackType aTrack);
|
2016-10-28 12:43:19 +03:00
|
|
|
~DecoderAllocPolicy();
|
|
|
|
// Called by the destructor of TokenImpl to restore the decoder limit.
|
|
|
|
void Dealloc();
|
|
|
|
// Decrement the decoder limit and resolve a promise if available.
|
|
|
|
void ResolvePromise(ReentrantMonitorAutoEnter& aProofOfLock);
|
|
|
|
|
2016-11-03 04:56:17 +03:00
|
|
|
// Protect access to Instance().
|
|
|
|
static StaticMutex sMutex;
|
|
|
|
|
2016-10-28 12:43:19 +03:00
|
|
|
ReentrantMonitor mMonitor;
|
|
|
|
// The number of decoders available for creation.
|
|
|
|
int mDecoderLimit;
|
2016-11-21 17:46:40 +03:00
|
|
|
// Track type.
|
|
|
|
const TrackType mTrack;
|
2016-10-28 12:43:19 +03:00
|
|
|
// Requests to acquire tokens.
|
|
|
|
std::queue<RefPtr<PromisePrivate>> mPromises;
|
|
|
|
};
|
|
|
|
|
2016-11-03 04:56:17 +03:00
|
|
|
StaticMutex DecoderAllocPolicy::sMutex;
|
|
|
|
|
2016-10-28 12:43:19 +03:00
|
|
|
class DecoderAllocPolicy::AutoDeallocToken : public Token
|
|
|
|
{
|
2016-11-21 17:46:40 +03:00
|
|
|
public:
|
2016-11-22 20:29:45 +03:00
|
|
|
explicit AutoDeallocToken(TrackType aTrack)
|
2016-11-21 17:46:40 +03:00
|
|
|
: mTrack(aTrack)
|
|
|
|
{}
|
|
|
|
|
2016-10-28 12:43:19 +03:00
|
|
|
private:
|
2016-11-21 17:46:40 +03:00
|
|
|
~AutoDeallocToken()
|
|
|
|
{
|
|
|
|
DecoderAllocPolicy::Instance(mTrack).Dealloc();
|
|
|
|
}
|
|
|
|
|
|
|
|
const TrackType mTrack;
|
2016-10-28 12:43:19 +03:00
|
|
|
};
|
|
|
|
|
2016-11-21 17:46:40 +03:00
|
|
|
DecoderAllocPolicy::DecoderAllocPolicy(TrackType aTrack)
|
2016-10-28 12:43:19 +03:00
|
|
|
: mMonitor("DecoderAllocPolicy::mMonitor")
|
|
|
|
, mDecoderLimit(MediaPrefs::MediaDecoderLimit())
|
2016-11-21 17:46:40 +03:00
|
|
|
, mTrack(aTrack)
|
2016-10-28 12:43:19 +03:00
|
|
|
{
|
2016-11-29 08:03:36 +03:00
|
|
|
// Non DocGroup-version AbstractThread::MainThread is fine for ClearOnShutdown().
|
2016-10-28 12:43:19 +03:00
|
|
|
AbstractThread::MainThread()->Dispatch(NS_NewRunnableFunction([this] () {
|
|
|
|
ClearOnShutdown(this, ShutdownPhase::ShutdownThreads);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
DecoderAllocPolicy::~DecoderAllocPolicy()
|
|
|
|
{
|
|
|
|
while (!mPromises.empty()) {
|
|
|
|
RefPtr<PromisePrivate> p = mPromises.front().forget();
|
|
|
|
mPromises.pop();
|
|
|
|
p->Reject(true, __func__);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DecoderAllocPolicy&
|
2016-11-21 17:46:40 +03:00
|
|
|
DecoderAllocPolicy::Instance(TrackType aTrack)
|
2016-10-28 12:43:19 +03:00
|
|
|
{
|
2016-11-03 04:56:17 +03:00
|
|
|
StaticMutexAutoLock lock(sMutex);
|
2016-11-21 17:46:40 +03:00
|
|
|
if (aTrack == TrackType::kAudioTrack) {
|
|
|
|
static auto sAudioPolicy = new DecoderAllocPolicy(TrackType::kAudioTrack);
|
|
|
|
return *sAudioPolicy;
|
|
|
|
} else {
|
|
|
|
static auto sVideoPolicy = new DecoderAllocPolicy(TrackType::kVideoTrack);
|
|
|
|
return *sVideoPolicy;
|
|
|
|
}
|
2016-10-28 12:43:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
auto
|
2016-11-21 17:46:40 +03:00
|
|
|
DecoderAllocPolicy::Alloc() -> RefPtr<Promise>
|
2016-10-28 12:43:19 +03:00
|
|
|
{
|
2016-11-21 17:46:40 +03:00
|
|
|
// No decoder limit set.
|
|
|
|
if (mDecoderLimit < 0) {
|
2016-10-28 12:43:19 +03:00
|
|
|
return Promise::CreateAndResolve(new Token(), __func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
ReentrantMonitorAutoEnter mon(mMonitor);
|
|
|
|
RefPtr<PromisePrivate> p = new PromisePrivate(__func__);
|
|
|
|
mPromises.push(p);
|
|
|
|
ResolvePromise(mon);
|
|
|
|
return p.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DecoderAllocPolicy::Dealloc()
|
|
|
|
{
|
|
|
|
ReentrantMonitorAutoEnter mon(mMonitor);
|
|
|
|
++mDecoderLimit;
|
|
|
|
ResolvePromise(mon);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DecoderAllocPolicy::ResolvePromise(ReentrantMonitorAutoEnter& aProofOfLock)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mDecoderLimit >= 0);
|
|
|
|
|
|
|
|
if (mDecoderLimit > 0 && !mPromises.empty()) {
|
|
|
|
--mDecoderLimit;
|
|
|
|
RefPtr<PromisePrivate> p = mPromises.front().forget();
|
|
|
|
mPromises.pop();
|
2016-11-21 17:46:40 +03:00
|
|
|
p->Resolve(new AutoDeallocToken(mTrack), __func__);
|
2016-10-28 12:43:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DecoderAllocPolicy::operator=(std::nullptr_t)
|
|
|
|
{
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
2016-10-26 12:37:19 +03:00
|
|
|
class MediaFormatReader::DecoderFactory
|
|
|
|
{
|
|
|
|
using InitPromise = MediaDataDecoder::InitPromise;
|
2016-10-27 10:16:06 +03:00
|
|
|
using TokenPromise = DecoderAllocPolicy::Promise;
|
|
|
|
using Token = DecoderAllocPolicy::Token;
|
2016-10-26 12:37:19 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
explicit DecoderFactory(MediaFormatReader* aOwner) : mOwner(aOwner) {}
|
|
|
|
void CreateDecoder(TrackType aTrack);
|
2017-01-26 15:56:46 +03:00
|
|
|
// Shutdown any decoder pending initialization.
|
|
|
|
RefPtr<ShutdownPromise> ShutdownDecoder(TrackType aTrack)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aTrack == TrackInfo::kAudioTrack ||
|
|
|
|
aTrack == TrackInfo::kVideoTrack);
|
|
|
|
auto& data = aTrack == TrackInfo::kAudioTrack ? mAudio : mVideo;
|
|
|
|
data.mTokenRequest.DisconnectIfExists();
|
|
|
|
data.mInitRequest.DisconnectIfExists();
|
|
|
|
if (!data.mDecoder) {
|
|
|
|
return ShutdownPromise::CreateAndResolve(true, __func__);
|
|
|
|
}
|
|
|
|
if (data.mShutdownRequest.Exists()) {
|
|
|
|
// A shutdown is already in progress due to a prior initialization error,
|
|
|
|
// return the existing promise.
|
|
|
|
data.mShutdownRequest.Disconnect();
|
|
|
|
RefPtr<ShutdownPromise> p = data.mShutdownPromise.forget();
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
RefPtr<MediaDataDecoder> decoder = data.mDecoder.forget();
|
|
|
|
return decoder->Shutdown();
|
|
|
|
}
|
2016-10-26 12:37:19 +03:00
|
|
|
|
|
|
|
private:
|
2016-10-27 10:16:06 +03:00
|
|
|
class Wrapper;
|
|
|
|
|
2016-10-26 12:37:19 +03:00
|
|
|
enum class Stage : int8_t
|
|
|
|
{
|
|
|
|
None,
|
2016-10-27 10:16:06 +03:00
|
|
|
WaitForToken,
|
|
|
|
CreateDecoder,
|
2016-10-26 12:37:19 +03:00
|
|
|
WaitForInit
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Data
|
|
|
|
{
|
|
|
|
Stage mStage = Stage::None;
|
2016-10-27 10:16:06 +03:00
|
|
|
RefPtr<Token> mToken;
|
2016-10-26 12:37:19 +03:00
|
|
|
RefPtr<MediaDataDecoder> mDecoder;
|
2017-01-26 15:56:46 +03:00
|
|
|
MozPromiseRequestHolder<TokenPromise> mTokenRequest;
|
|
|
|
MozPromiseRequestHolder<InitPromise> mInitRequest;
|
|
|
|
MozPromiseRequestHolder<ShutdownPromise> mShutdownRequest;
|
|
|
|
RefPtr<ShutdownPromise> mShutdownPromise;
|
2016-10-26 12:37:19 +03:00
|
|
|
} mAudio, mVideo;
|
|
|
|
|
|
|
|
void RunStage(TrackType aTrack);
|
|
|
|
MediaResult DoCreateDecoder(TrackType aTrack);
|
|
|
|
void DoInitDecoder(TrackType aTrack);
|
|
|
|
|
|
|
|
MediaFormatReader* const mOwner; // guaranteed to be valid by the owner.
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::DecoderFactory::CreateDecoder(TrackType aTrack)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aTrack == TrackInfo::kAudioTrack ||
|
|
|
|
aTrack == TrackInfo::kVideoTrack);
|
|
|
|
RunStage(aTrack);
|
|
|
|
}
|
|
|
|
|
2016-10-27 10:16:06 +03:00
|
|
|
class MediaFormatReader::DecoderFactory::Wrapper : public MediaDataDecoder
|
|
|
|
{
|
|
|
|
using Token = DecoderAllocPolicy::Token;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Wrapper(already_AddRefed<MediaDataDecoder> aDecoder,
|
|
|
|
already_AddRefed<Token> aToken)
|
|
|
|
: mDecoder(aDecoder), mToken(aToken) {}
|
|
|
|
|
|
|
|
RefPtr<InitPromise> Init() override { return mDecoder->Init(); }
|
2017-01-26 15:56:46 +03:00
|
|
|
RefPtr<DecodePromise> Decode(MediaRawData* aSample) override
|
|
|
|
{
|
|
|
|
return mDecoder->Decode(aSample);
|
|
|
|
}
|
|
|
|
RefPtr<DecodePromise> Drain() override { return mDecoder->Drain(); }
|
|
|
|
RefPtr<FlushPromise> Flush() override { return mDecoder->Flush(); }
|
2016-10-27 10:16:06 +03:00
|
|
|
bool IsHardwareAccelerated(nsACString& aFailureReason) const override
|
|
|
|
{
|
|
|
|
return mDecoder->IsHardwareAccelerated(aFailureReason);
|
|
|
|
}
|
|
|
|
const char* GetDescriptionName() const override
|
|
|
|
{
|
|
|
|
return mDecoder->GetDescriptionName();
|
|
|
|
}
|
|
|
|
void SetSeekThreshold(const media::TimeUnit& aTime) override
|
|
|
|
{
|
|
|
|
mDecoder->SetSeekThreshold(aTime);
|
|
|
|
}
|
2016-11-25 09:22:40 +03:00
|
|
|
bool SupportDecoderRecycling() const override
|
|
|
|
{
|
|
|
|
return mDecoder->SupportDecoderRecycling();
|
|
|
|
}
|
2017-01-26 15:56:46 +03:00
|
|
|
RefPtr<ShutdownPromise> Shutdown() override
|
2016-10-27 10:16:06 +03:00
|
|
|
{
|
2017-01-26 15:56:46 +03:00
|
|
|
RefPtr<MediaDataDecoder> decoder = mDecoder.forget();
|
|
|
|
RefPtr<Token> token = mToken.forget();
|
|
|
|
return decoder->Shutdown()->Then(
|
|
|
|
AbstractThread::GetCurrent(), __func__,
|
|
|
|
[token]() {},
|
|
|
|
[token]() { MOZ_RELEASE_ASSERT(false, "Can't reach here"); });
|
2016-10-27 10:16:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<MediaDataDecoder> mDecoder;
|
|
|
|
RefPtr<Token> mToken;
|
|
|
|
};
|
|
|
|
|
2016-10-26 12:37:19 +03:00
|
|
|
void
|
|
|
|
MediaFormatReader::DecoderFactory::RunStage(TrackType aTrack)
|
|
|
|
{
|
|
|
|
auto& data = aTrack == TrackInfo::kAudioTrack ? mAudio : mVideo;
|
|
|
|
|
|
|
|
switch (data.mStage) {
|
|
|
|
case Stage::None: {
|
2016-10-27 10:16:06 +03:00
|
|
|
MOZ_ASSERT(!data.mToken);
|
2017-01-11 11:33:29 +03:00
|
|
|
DecoderAllocPolicy::Instance(aTrack).Alloc()->Then(
|
2016-10-27 10:16:06 +03:00
|
|
|
mOwner->OwnerThread(), __func__,
|
|
|
|
[this, &data, aTrack] (Token* aToken) {
|
2017-01-26 15:56:46 +03:00
|
|
|
data.mTokenRequest.Complete();
|
2016-10-27 10:16:06 +03:00
|
|
|
data.mToken = aToken;
|
|
|
|
data.mStage = Stage::CreateDecoder;
|
|
|
|
RunStage(aTrack);
|
|
|
|
},
|
|
|
|
[&data] () {
|
2017-01-26 15:56:46 +03:00
|
|
|
data.mTokenRequest.Complete();
|
2016-10-27 10:16:06 +03:00
|
|
|
data.mStage = Stage::None;
|
2017-01-26 15:56:46 +03:00
|
|
|
})->Track(data.mTokenRequest);
|
2016-10-27 10:16:06 +03:00
|
|
|
data.mStage = Stage::WaitForToken;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Stage::WaitForToken: {
|
|
|
|
MOZ_ASSERT(!data.mToken);
|
2017-01-26 15:56:46 +03:00
|
|
|
MOZ_ASSERT(data.mTokenRequest.Exists());
|
2016-10-27 10:16:06 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Stage::CreateDecoder: {
|
|
|
|
MOZ_ASSERT(data.mToken);
|
2016-10-26 12:37:19 +03:00
|
|
|
MOZ_ASSERT(!data.mDecoder);
|
2017-01-26 15:56:46 +03:00
|
|
|
MOZ_ASSERT(!data.mInitRequest.Exists());
|
2016-10-26 12:37:19 +03:00
|
|
|
|
|
|
|
MediaResult rv = DoCreateDecoder(aTrack);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING("Error constructing decoders");
|
2016-10-27 10:16:06 +03:00
|
|
|
data.mToken = nullptr;
|
2016-10-26 12:37:19 +03:00
|
|
|
data.mStage = Stage::None;
|
|
|
|
mOwner->NotifyError(aTrack, rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-27 10:16:06 +03:00
|
|
|
data.mDecoder = new Wrapper(data.mDecoder.forget(), data.mToken.forget());
|
2016-10-26 12:37:19 +03:00
|
|
|
DoInitDecoder(aTrack);
|
|
|
|
data.mStage = Stage::WaitForInit;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Stage::WaitForInit: {
|
|
|
|
MOZ_ASSERT(data.mDecoder);
|
2017-01-26 15:56:46 +03:00
|
|
|
MOZ_ASSERT(data.mInitRequest.Exists());
|
2016-10-26 12:37:19 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaResult
|
|
|
|
MediaFormatReader::DecoderFactory::DoCreateDecoder(TrackType aTrack)
|
|
|
|
{
|
|
|
|
auto& ownerData = mOwner->GetDecoderData(aTrack);
|
|
|
|
auto& data = aTrack == TrackInfo::kAudioTrack ? mAudio : mVideo;
|
|
|
|
|
|
|
|
auto decoderCreatingError = "error creating audio decoder";
|
|
|
|
MediaResult result = MediaResult(NS_ERROR_DOM_MEDIA_FATAL_ERR, decoderCreatingError);
|
|
|
|
|
|
|
|
if (!mOwner->mPlatform) {
|
|
|
|
mOwner->mPlatform = new PDMFactory();
|
|
|
|
if (mOwner->IsEncrypted()) {
|
|
|
|
MOZ_ASSERT(mOwner->mCDMProxy);
|
|
|
|
mOwner->mPlatform->SetCDMProxy(mOwner->mCDMProxy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (aTrack) {
|
|
|
|
case TrackInfo::kAudioTrack: {
|
|
|
|
data.mDecoder = mOwner->mPlatform->CreateDecoder({
|
|
|
|
ownerData.mInfo
|
|
|
|
? *ownerData.mInfo->GetAsAudioInfo()
|
|
|
|
: *ownerData.mOriginalInfo->GetAsAudioInfo(),
|
|
|
|
ownerData.mTaskQueue,
|
|
|
|
mOwner->mCrashHelper,
|
|
|
|
ownerData.mIsBlankDecode,
|
|
|
|
&result
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case TrackType::kVideoTrack: {
|
|
|
|
// Decoders use the layers backend to decide if they can use hardware decoding,
|
|
|
|
// so specify LAYERS_NONE if we want to forcibly disable it.
|
|
|
|
data.mDecoder = mOwner->mPlatform->CreateDecoder({
|
|
|
|
ownerData.mInfo
|
|
|
|
? *ownerData.mInfo->GetAsVideoInfo()
|
|
|
|
: *ownerData.mOriginalInfo->GetAsVideoInfo(),
|
|
|
|
ownerData.mTaskQueue,
|
|
|
|
mOwner->mKnowsCompositor,
|
|
|
|
mOwner->GetImageContainer(),
|
|
|
|
mOwner->mCrashHelper,
|
|
|
|
ownerData.mIsBlankDecode,
|
|
|
|
&result
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.mDecoder) {
|
2017-01-26 15:56:46 +03:00
|
|
|
return NS_OK;
|
2016-10-26 12:37:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ownerData.mDescription = decoderCreatingError;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::DecoderFactory::DoInitDecoder(TrackType aTrack)
|
|
|
|
{
|
|
|
|
auto& ownerData = mOwner->GetDecoderData(aTrack);
|
|
|
|
auto& data = aTrack == TrackInfo::kAudioTrack ? mAudio : mVideo;
|
|
|
|
|
2017-01-26 15:56:46 +03:00
|
|
|
data.mDecoder->Init()
|
|
|
|
->Then(mOwner->OwnerThread(), __func__,
|
|
|
|
[this, &data, &ownerData](TrackType aTrack) {
|
|
|
|
data.mInitRequest.Complete();
|
|
|
|
data.mStage = Stage::None;
|
|
|
|
MutexAutoLock lock(ownerData.mMutex);
|
|
|
|
ownerData.mDecoder = data.mDecoder.forget();
|
|
|
|
ownerData.mDescription = ownerData.mDecoder->GetDescriptionName();
|
|
|
|
mOwner->SetVideoDecodeThreshold();
|
|
|
|
mOwner->ScheduleUpdate(aTrack);
|
|
|
|
},
|
|
|
|
[this, &data, &ownerData, aTrack](const MediaResult& aError) {
|
|
|
|
data.mInitRequest.Complete();
|
|
|
|
MOZ_RELEASE_ASSERT(!ownerData.mDecoder,
|
|
|
|
"Can't have a decoder already set");
|
|
|
|
data.mStage = Stage::None;
|
|
|
|
data.mShutdownPromise = data.mDecoder->Shutdown();
|
|
|
|
data.mShutdownPromise
|
|
|
|
->Then(
|
|
|
|
mOwner->OwnerThread(), __func__,
|
|
|
|
[this, &data, aTrack, aError]() {
|
|
|
|
data.mShutdownRequest.Complete();
|
|
|
|
data.mShutdownPromise = nullptr;
|
|
|
|
data.mDecoder = nullptr;
|
|
|
|
mOwner->NotifyError(aTrack, aError);
|
|
|
|
},
|
|
|
|
[]() { MOZ_RELEASE_ASSERT(false, "Can't ever get here"); })
|
|
|
|
->Track(data.mShutdownRequest);
|
|
|
|
})
|
|
|
|
->Track(data.mInitRequest);
|
2016-10-26 12:37:19 +03:00
|
|
|
}
|
|
|
|
|
2016-11-28 13:08:01 +03:00
|
|
|
// DemuxerProxy ensures that the original main demuxer is only ever accessed
|
|
|
|
// via its own dedicated task queue.
|
|
|
|
// This ensure that the reader's taskqueue will never blocked while a demuxer
|
|
|
|
// is itself blocked attempting to access the MediaCache or the MediaResource.
|
|
|
|
class MediaFormatReader::DemuxerProxy
|
|
|
|
{
|
|
|
|
using TrackType = TrackInfo::TrackType;
|
|
|
|
class Wrapper;
|
|
|
|
|
|
|
|
public:
|
2016-11-29 08:03:36 +03:00
|
|
|
explicit DemuxerProxy(MediaDataDemuxer* aDemuxer, AbstractThread* mainThread)
|
2016-11-28 13:08:01 +03:00
|
|
|
: mTaskQueue(new AutoTaskQueue(
|
2016-11-29 08:03:36 +03:00
|
|
|
GetMediaThreadPool(MediaThreadType::PLATFORM_DECODER),
|
|
|
|
mainThread))
|
2016-11-28 13:08:01 +03:00
|
|
|
, mData(new Data(aDemuxer))
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(DemuxerProxy);
|
|
|
|
}
|
|
|
|
|
|
|
|
~DemuxerProxy()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(DemuxerProxy);
|
|
|
|
mData->mAudioDemuxer = nullptr;
|
|
|
|
mData->mVideoDemuxer = nullptr;
|
|
|
|
RefPtr<Data> data = mData.forget();
|
|
|
|
mTaskQueue->Dispatch(
|
|
|
|
// We need to clear our reference to the demuxer now. So that in the event
|
|
|
|
// the init promise wasn't resolved, such as what can happen with the
|
|
|
|
// mediasource demuxer that is waiting on more data, it will force the
|
|
|
|
// init promise to be rejected.
|
|
|
|
NS_NewRunnableFunction([data]() { data->mDemuxer = nullptr; }));
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<MediaDataDemuxer::InitPromise> Init();
|
|
|
|
|
|
|
|
Wrapper*
|
|
|
|
GetTrackDemuxer(TrackType aTrack, uint32_t aTrackNumber)
|
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(mData && mData->mInitDone);
|
|
|
|
|
|
|
|
switch (aTrack) {
|
|
|
|
case TrackInfo::kAudioTrack:
|
|
|
|
return mData->mAudioDemuxer;
|
|
|
|
case TrackInfo::kVideoTrack:
|
|
|
|
return mData->mVideoDemuxer;
|
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t GetNumberTracks(TrackType aTrack) const
|
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(mData && mData->mInitDone);
|
|
|
|
|
|
|
|
switch (aTrack) {
|
|
|
|
case TrackInfo::kAudioTrack:
|
|
|
|
return mData->mNumAudioTrack;
|
|
|
|
case TrackInfo::kVideoTrack:
|
|
|
|
return mData->mNumVideoTrack;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsSeekable() const
|
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(mData && mData->mInitDone);
|
|
|
|
|
|
|
|
return mData->mSeekable;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsSeekableOnlyInBufferedRanges() const
|
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(mData && mData->mInitDone);
|
|
|
|
|
|
|
|
return mData->mSeekableOnlyInBufferedRange;
|
|
|
|
}
|
|
|
|
|
|
|
|
UniquePtr<EncryptionInfo> GetCrypto() const
|
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(mData && mData->mInitDone);
|
|
|
|
|
|
|
|
if (!mData->mCrypto) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto crypto = MakeUnique<EncryptionInfo>();
|
|
|
|
*crypto = *mData->mCrypto;
|
|
|
|
return crypto;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<NotifyDataArrivedPromise> NotifyDataArrived();
|
|
|
|
|
|
|
|
bool ShouldComputeStartTime() const
|
|
|
|
{
|
|
|
|
MOZ_RELEASE_ASSERT(mData && mData->mInitDone);
|
|
|
|
|
|
|
|
return mData->mShouldComputeStartTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const RefPtr<AutoTaskQueue> mTaskQueue;
|
|
|
|
struct Data
|
|
|
|
{
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Data)
|
|
|
|
|
|
|
|
explicit Data(MediaDataDemuxer* aDemuxer)
|
|
|
|
: mInitDone(false)
|
|
|
|
, mDemuxer(aDemuxer)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
Atomic<bool> mInitDone;
|
|
|
|
// Only ever accessed over mTaskQueue once.
|
|
|
|
RefPtr<MediaDataDemuxer> mDemuxer;
|
|
|
|
// Only accessed once InitPromise has been resolved and immutable after.
|
|
|
|
// So we can safely access them without the use of the mutex.
|
|
|
|
uint32_t mNumAudioTrack = 0;
|
|
|
|
RefPtr<Wrapper> mAudioDemuxer;
|
|
|
|
uint32_t mNumVideoTrack = 0;
|
|
|
|
RefPtr<Wrapper> mVideoDemuxer;
|
|
|
|
bool mSeekable = false;
|
|
|
|
bool mSeekableOnlyInBufferedRange = false;
|
|
|
|
bool mShouldComputeStartTime = true;
|
|
|
|
UniquePtr<EncryptionInfo> mCrypto;
|
|
|
|
private:
|
|
|
|
~Data() { }
|
|
|
|
};
|
|
|
|
RefPtr<Data> mData;
|
|
|
|
};
|
|
|
|
|
|
|
|
class MediaFormatReader::DemuxerProxy::Wrapper : public MediaTrackDemuxer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Wrapper(MediaTrackDemuxer* aTrackDemuxer, AutoTaskQueue* aTaskQueue)
|
|
|
|
: mMutex("TrackDemuxer Mutex")
|
|
|
|
, mTaskQueue(aTaskQueue)
|
|
|
|
, mGetSamplesMayBlock(aTrackDemuxer->GetSamplesMayBlock())
|
|
|
|
, mInfo(aTrackDemuxer->GetInfo())
|
|
|
|
, mTrackDemuxer(aTrackDemuxer)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
UniquePtr<TrackInfo> GetInfo() const override
|
|
|
|
{
|
|
|
|
if (!mInfo) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return mInfo->Clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<SeekPromise> Seek(const media::TimeUnit& aTime) override
|
|
|
|
{
|
|
|
|
RefPtr<Wrapper> self = this;
|
|
|
|
return InvokeAsync(
|
|
|
|
mTaskQueue, __func__,
|
|
|
|
[self, aTime]() { return self->mTrackDemuxer->Seek(aTime); })
|
|
|
|
->Then(mTaskQueue, __func__,
|
|
|
|
[self]() { self->UpdateRandomAccessPoint(); },
|
|
|
|
[self]() { self->UpdateRandomAccessPoint(); });
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<SamplesPromise> GetSamples(int32_t aNumSamples) override
|
|
|
|
{
|
|
|
|
RefPtr<Wrapper> self = this;
|
|
|
|
return InvokeAsync(mTaskQueue, __func__,
|
|
|
|
[self, aNumSamples]() {
|
|
|
|
return self->mTrackDemuxer->GetSamples(aNumSamples);
|
|
|
|
})
|
|
|
|
->Then(mTaskQueue, __func__,
|
|
|
|
[self]() { self->UpdateRandomAccessPoint(); },
|
|
|
|
[self]() { self->UpdateRandomAccessPoint(); });
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GetSamplesMayBlock() const override
|
|
|
|
{
|
|
|
|
return mGetSamplesMayBlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reset() override
|
|
|
|
{
|
|
|
|
RefPtr<Wrapper> self = this;
|
|
|
|
mTaskQueue->Dispatch(NS_NewRunnableFunction([self]() {
|
|
|
|
self->mTrackDemuxer->Reset();
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult GetNextRandomAccessPoint(TimeUnit* aTime) override
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
if (NS_SUCCEEDED(mNextRandomAccessPointResult)) {
|
|
|
|
*aTime = mNextRandomAccessPoint;
|
|
|
|
}
|
|
|
|
return mNextRandomAccessPointResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<SkipAccessPointPromise>
|
|
|
|
SkipToNextRandomAccessPoint(const media::TimeUnit& aTimeThreshold) override
|
|
|
|
{
|
|
|
|
RefPtr<Wrapper> self = this;
|
|
|
|
return InvokeAsync(
|
|
|
|
mTaskQueue, __func__,
|
|
|
|
[self, aTimeThreshold]() {
|
|
|
|
return self->mTrackDemuxer->SkipToNextRandomAccessPoint(
|
|
|
|
aTimeThreshold);
|
|
|
|
})
|
|
|
|
->Then(mTaskQueue, __func__,
|
|
|
|
[self]() { self->UpdateRandomAccessPoint(); },
|
|
|
|
[self]() { self->UpdateRandomAccessPoint(); });
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeIntervals GetBuffered() override
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
return mBuffered;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BreakCycles() override { }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Mutex mMutex;
|
|
|
|
const RefPtr<AutoTaskQueue> mTaskQueue;
|
|
|
|
const bool mGetSamplesMayBlock;
|
|
|
|
const UniquePtr<TrackInfo> mInfo;
|
|
|
|
// mTrackDemuxer is only ever accessed on demuxer's task queue.
|
|
|
|
RefPtr<MediaTrackDemuxer> mTrackDemuxer;
|
|
|
|
// All following members are protected by mMutex
|
|
|
|
nsresult mNextRandomAccessPointResult = NS_OK;
|
|
|
|
TimeUnit mNextRandomAccessPoint;
|
|
|
|
TimeIntervals mBuffered;
|
|
|
|
friend class DemuxerProxy;
|
|
|
|
|
|
|
|
~Wrapper()
|
|
|
|
{
|
|
|
|
RefPtr<MediaTrackDemuxer> trackDemuxer = mTrackDemuxer.forget();
|
|
|
|
mTaskQueue->Dispatch(NS_NewRunnableFunction(
|
|
|
|
[trackDemuxer]() { trackDemuxer->BreakCycles(); }));
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateRandomAccessPoint()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
|
|
|
|
if (!mTrackDemuxer) {
|
|
|
|
// Detached.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mNextRandomAccessPointResult =
|
|
|
|
mTrackDemuxer->GetNextRandomAccessPoint(&mNextRandomAccessPoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateBuffered()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mTaskQueue->IsCurrentThreadIn());
|
|
|
|
if (!mTrackDemuxer) {
|
|
|
|
// Detached.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mBuffered = mTrackDemuxer->GetBuffered();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
RefPtr<MediaDataDemuxer::InitPromise>
|
|
|
|
MediaFormatReader::DemuxerProxy::Init()
|
|
|
|
{
|
|
|
|
RefPtr<Data> data = mData;
|
|
|
|
RefPtr<AutoTaskQueue> taskQueue = mTaskQueue;
|
|
|
|
return InvokeAsync(mTaskQueue, __func__,
|
|
|
|
[data, taskQueue]() {
|
|
|
|
if (!data->mDemuxer) {
|
|
|
|
return MediaDataDemuxer::InitPromise::CreateAndReject(
|
|
|
|
NS_ERROR_DOM_MEDIA_CANCELED, __func__);
|
|
|
|
}
|
|
|
|
return data->mDemuxer->Init();
|
|
|
|
})
|
|
|
|
->Then(taskQueue, __func__,
|
|
|
|
[data, taskQueue]() {
|
|
|
|
if (!data->mDemuxer) { // Was shutdown.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
data->mNumAudioTrack =
|
|
|
|
data->mDemuxer->GetNumberTracks(TrackInfo::kAudioTrack);
|
|
|
|
if (data->mNumAudioTrack) {
|
|
|
|
RefPtr<MediaTrackDemuxer> d =
|
|
|
|
data->mDemuxer->GetTrackDemuxer(TrackInfo::kAudioTrack, 0);
|
|
|
|
if (d) {
|
|
|
|
RefPtr<Wrapper> wrapper =
|
|
|
|
new DemuxerProxy::Wrapper(d, taskQueue);
|
|
|
|
wrapper->UpdateBuffered();
|
|
|
|
data->mAudioDemuxer = wrapper;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data->mNumVideoTrack =
|
|
|
|
data->mDemuxer->GetNumberTracks(TrackInfo::kVideoTrack);
|
|
|
|
if (data->mNumVideoTrack) {
|
|
|
|
RefPtr<MediaTrackDemuxer> d =
|
|
|
|
data->mDemuxer->GetTrackDemuxer(TrackInfo::kVideoTrack, 0);
|
|
|
|
if (d) {
|
|
|
|
RefPtr<Wrapper> wrapper =
|
|
|
|
new DemuxerProxy::Wrapper(d, taskQueue);
|
|
|
|
wrapper->UpdateBuffered();
|
|
|
|
data->mVideoDemuxer = wrapper;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
data->mCrypto = data->mDemuxer->GetCrypto();
|
|
|
|
data->mSeekable = data->mDemuxer->IsSeekable();
|
|
|
|
data->mSeekableOnlyInBufferedRange =
|
|
|
|
data->mDemuxer->IsSeekableOnlyInBufferedRanges();
|
|
|
|
data->mShouldComputeStartTime =
|
|
|
|
data->mDemuxer->ShouldComputeStartTime();
|
|
|
|
data->mInitDone = true;
|
|
|
|
},
|
|
|
|
[]() {});
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<MediaFormatReader::NotifyDataArrivedPromise>
|
|
|
|
MediaFormatReader::DemuxerProxy::NotifyDataArrived()
|
|
|
|
{
|
|
|
|
RefPtr<Data> data = mData;
|
|
|
|
return InvokeAsync(mTaskQueue, __func__, [data]() {
|
|
|
|
if (!data->mDemuxer) {
|
|
|
|
// Was shutdown.
|
|
|
|
return NotifyDataArrivedPromise::CreateAndReject(
|
|
|
|
NS_ERROR_DOM_MEDIA_CANCELED, __func__);
|
|
|
|
}
|
|
|
|
data->mDemuxer->NotifyDataArrived();
|
|
|
|
if (data->mAudioDemuxer) {
|
|
|
|
data->mAudioDemuxer->UpdateBuffered();
|
|
|
|
}
|
|
|
|
if (data->mVideoDemuxer) {
|
|
|
|
data->mVideoDemuxer->UpdateBuffered();
|
|
|
|
}
|
|
|
|
return NotifyDataArrivedPromise::CreateAndResolve(true, __func__);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-05-18 08:40:32 +03:00
|
|
|
static const char*
|
|
|
|
TrackTypeToStr(TrackInfo::TrackType aTrack)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aTrack == TrackInfo::kAudioTrack ||
|
|
|
|
aTrack == TrackInfo::kVideoTrack ||
|
|
|
|
aTrack == TrackInfo::kTextTrack);
|
|
|
|
switch (aTrack) {
|
|
|
|
case TrackInfo::kAudioTrack:
|
|
|
|
return "Audio";
|
|
|
|
case TrackInfo::kVideoTrack:
|
|
|
|
return "Video";
|
|
|
|
case TrackInfo::kTextTrack:
|
|
|
|
return "Text";
|
|
|
|
default:
|
|
|
|
return "Unknown";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaFormatReader::MediaFormatReader(AbstractMediaDecoder* aDecoder,
|
2015-10-15 03:04:00 +03:00
|
|
|
MediaDataDemuxer* aDemuxer,
|
2016-10-07 11:13:33 +03:00
|
|
|
VideoFrameContainer* aVideoFrameContainer)
|
2015-10-09 04:25:23 +03:00
|
|
|
: MediaDecoderReader(aDecoder)
|
2016-09-01 12:25:54 +03:00
|
|
|
, mAudio(this, MediaData::AUDIO_DATA,
|
2016-05-30 19:24:00 +03:00
|
|
|
Preferences::GetUint("media.audio-max-decode-error", 3))
|
2016-09-01 12:25:54 +03:00
|
|
|
, mVideo(this, MediaData::VIDEO_DATA,
|
2016-05-30 19:24:00 +03:00
|
|
|
Preferences::GetUint("media.video-max-decode-error", 2))
|
2017-02-01 15:25:12 +03:00
|
|
|
, mDemuxer(new DemuxerProxy(aDemuxer, aDecoder
|
|
|
|
? aDecoder->AbstractMainThread()
|
|
|
|
: AbstractThread::MainThread()))
|
2015-10-07 04:00:52 +03:00
|
|
|
, mDemuxerInitDone(false)
|
2015-05-18 08:40:32 +03:00
|
|
|
, mLastReportedNumDecodedFrames(0)
|
2016-07-26 02:36:55 +03:00
|
|
|
, mPreviousDecodedKeyframeTime_us(sNoPreviousDecodedKeyframe)
|
2015-05-18 08:40:32 +03:00
|
|
|
, mInitDone(false)
|
2015-06-12 02:26:58 +03:00
|
|
|
, mTrackDemuxersMayBlock(false)
|
2016-05-05 08:01:51 +03:00
|
|
|
, mSeekScheduled(false)
|
2015-10-15 03:04:00 +03:00
|
|
|
, mVideoFrameContainer(aVideoFrameContainer)
|
2016-10-26 12:37:19 +03:00
|
|
|
, mDecoderFactory(new DecoderFactory(this))
|
2015-05-18 08:40:32 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aDemuxer);
|
|
|
|
MOZ_COUNT_CTOR(MediaFormatReader);
|
2016-11-08 05:23:12 +03:00
|
|
|
|
2017-02-01 15:25:12 +03:00
|
|
|
if (aDecoder && aDecoder->CompositorUpdatedEvent()) {
|
|
|
|
mCompositorUpdatedListener =
|
|
|
|
aDecoder->CompositorUpdatedEvent()->Connect(
|
|
|
|
mTaskQueue, this, &MediaFormatReader::NotifyCompositorUpdated);
|
2016-11-08 05:23:12 +03:00
|
|
|
}
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
MediaFormatReader::~MediaFormatReader()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(MediaFormatReader);
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ShutdownPromise>
|
2015-05-18 08:40:32 +03:00
|
|
|
MediaFormatReader::Shutdown()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2017-01-26 15:56:46 +03:00
|
|
|
LOG("");
|
2015-05-18 08:40:32 +03:00
|
|
|
|
|
|
|
mDemuxerInitRequest.DisconnectIfExists();
|
2016-11-28 13:08:01 +03:00
|
|
|
mNotifyDataArrivedPromise.DisconnectIfExists();
|
2016-09-10 12:56:50 +03:00
|
|
|
mMetadataPromise.RejectIfExists(NS_ERROR_DOM_MEDIA_CANCELED, __func__);
|
2016-09-10 09:48:53 +03:00
|
|
|
mSeekPromise.RejectIfExists(NS_ERROR_DOM_MEDIA_CANCELED, __func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
mSkipRequest.DisconnectIfExists();
|
|
|
|
|
2017-01-26 15:56:46 +03:00
|
|
|
if (mAudio.HasPromise()) {
|
|
|
|
mAudio.RejectPromise(NS_ERROR_DOM_MEDIA_CANCELED, __func__);
|
|
|
|
}
|
|
|
|
if (mVideo.HasPromise()) {
|
|
|
|
mVideo.RejectPromise(NS_ERROR_DOM_MEDIA_CANCELED, __func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
2017-01-26 15:56:46 +03:00
|
|
|
|
|
|
|
nsTArray<RefPtr<ShutdownPromise>> promises;
|
|
|
|
|
|
|
|
if (HasAudio()) {
|
2015-05-25 08:09:16 +03:00
|
|
|
mAudio.ResetDemuxer();
|
2015-05-18 08:40:32 +03:00
|
|
|
mAudio.mTrackDemuxer->BreakCycles();
|
|
|
|
mAudio.mTrackDemuxer = nullptr;
|
2017-01-26 15:56:46 +03:00
|
|
|
mAudio.ResetState();
|
|
|
|
promises.AppendElement(ShutdownDecoderWithPromise(TrackInfo::kAudioTrack));
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
2017-01-26 15:56:46 +03:00
|
|
|
if (HasVideo()) {
|
2015-05-25 08:09:16 +03:00
|
|
|
mVideo.ResetDemuxer();
|
2015-05-18 08:40:32 +03:00
|
|
|
mVideo.mTrackDemuxer->BreakCycles();
|
|
|
|
mVideo.mTrackDemuxer = nullptr;
|
2017-01-26 15:56:46 +03:00
|
|
|
mVideo.ResetState();
|
|
|
|
promises.AppendElement(ShutdownDecoderWithPromise(TrackInfo::kVideoTrack));
|
|
|
|
}
|
|
|
|
|
|
|
|
mDemuxer = nullptr;
|
|
|
|
mCompositorUpdatedListener.DisconnectIfExists();
|
|
|
|
|
|
|
|
if (promises.IsEmpty()) {
|
|
|
|
TearDownDecoders();
|
|
|
|
return MediaDecoderReader::Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<ShutdownPromise> p = mShutdownPromise.Ensure(__func__);
|
|
|
|
ShutdownPromise::All(OwnerThread(), promises)
|
|
|
|
->Then(OwnerThread(), __func__, this,
|
|
|
|
&MediaFormatReader::TearDownDecoders,
|
|
|
|
&MediaFormatReader::TearDownDecoders);
|
|
|
|
|
|
|
|
mShutdown = true;
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<ShutdownPromise>
|
|
|
|
MediaFormatReader::ShutdownDecoderWithPromise(TrackType aTrack)
|
|
|
|
{
|
|
|
|
LOGV("%s", TrackTypeToStr(aTrack));
|
|
|
|
|
|
|
|
auto& decoder = GetDecoderData(aTrack);
|
|
|
|
if (!decoder.mFlushed && decoder.mDecoder) {
|
|
|
|
// The decoder has yet to be flushed.
|
|
|
|
// We always flush the decoder prior to a shutdown to ensure that all the
|
|
|
|
// potentially pending operations on the decoder are completed.
|
|
|
|
decoder.Flush();
|
|
|
|
return decoder.mShutdownPromise.Ensure(__func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (decoder.mFlushRequest.Exists() || decoder.mShutdownRequest.Exists()) {
|
|
|
|
// Let the current flush or shutdown operation complete, Flush will continue
|
|
|
|
// shutting down the current decoder now that the shutdown promise is set.
|
|
|
|
return decoder.mShutdownPromise.Ensure(__func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!decoder.mDecoder) {
|
|
|
|
// Shutdown any decoders that may be in the process of being initialized
|
|
|
|
// in the Decoder Factory.
|
|
|
|
// This will be a no-op until we're processing the final decoder shutdown
|
|
|
|
// prior to the MediaFormatReader being shutdown.
|
|
|
|
return mDecoderFactory->ShutdownDecoder(aTrack);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, let's just shut down the currently active decoder.
|
|
|
|
decoder.ShutdownDecoder();
|
|
|
|
return decoder.mShutdownPromise.Ensure(__func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::ShutdownDecoder(TrackType aTrack)
|
|
|
|
{
|
|
|
|
LOG("%s", TrackTypeToStr(aTrack));
|
|
|
|
auto& decoder = GetDecoderData(aTrack);
|
|
|
|
if (!decoder.mDecoder) {
|
|
|
|
LOGV("Already shut down");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!decoder.mShutdownPromise.IsEmpty()) {
|
|
|
|
LOGV("Shutdown already in progress");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Unused << ShutdownDecoderWithPromise(aTrack);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::TearDownDecoders()
|
|
|
|
{
|
|
|
|
if (mAudio.mTaskQueue) {
|
|
|
|
mAudio.mTaskQueue->BeginShutdown();
|
|
|
|
mAudio.mTaskQueue->AwaitShutdownAndIdle();
|
|
|
|
mAudio.mTaskQueue = nullptr;
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
if (mVideo.mTaskQueue) {
|
|
|
|
mVideo.mTaskQueue->BeginShutdown();
|
|
|
|
mVideo.mTaskQueue->AwaitShutdownAndIdle();
|
|
|
|
mVideo.mTaskQueue = nullptr;
|
|
|
|
}
|
|
|
|
|
2017-01-26 15:56:46 +03:00
|
|
|
mDecoderFactory = nullptr;
|
2015-05-18 08:40:32 +03:00
|
|
|
mPlatform = nullptr;
|
2016-05-22 16:39:55 +03:00
|
|
|
mVideoFrameContainer = nullptr;
|
2015-05-18 08:40:32 +03:00
|
|
|
|
2017-01-26 15:56:46 +03:00
|
|
|
if (mShutdownPromise.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
2016-11-08 05:23:12 +03:00
|
|
|
|
2017-01-26 15:56:46 +03:00
|
|
|
MediaDecoderReader::Shutdown();
|
|
|
|
mShutdownPromise.Resolve(true, __func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::InitLayersBackendType()
|
|
|
|
{
|
|
|
|
// Extract the layer manager backend type so that platform decoders
|
|
|
|
// can determine whether it's worthwhile using hardware accelerated
|
|
|
|
// video decoding.
|
2015-10-15 03:04:00 +03:00
|
|
|
if (!mDecoder) {
|
|
|
|
return;
|
|
|
|
}
|
2015-05-18 08:40:32 +03:00
|
|
|
MediaDecoderOwner* owner = mDecoder->GetOwner();
|
|
|
|
if (!owner) {
|
|
|
|
NS_WARNING("MediaFormatReader without a decoder owner, can't get HWAccel");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dom::HTMLMediaElement* element = owner->GetMediaElement();
|
|
|
|
NS_ENSURE_TRUE_VOID(element);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<LayerManager> layerManager =
|
2015-05-18 08:40:32 +03:00
|
|
|
nsContentUtils::LayerManagerForDocument(element->OwnerDoc());
|
|
|
|
NS_ENSURE_TRUE_VOID(layerManager);
|
|
|
|
|
2016-10-07 11:13:33 +03:00
|
|
|
mKnowsCompositor = layerManager->AsShadowForwarder();
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2016-11-08 18:09:34 +03:00
|
|
|
MediaFormatReader::InitInternal()
|
2015-05-18 08:40:32 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Must be on main thread.");
|
|
|
|
|
|
|
|
InitLayersBackendType();
|
|
|
|
|
|
|
|
mAudio.mTaskQueue =
|
2016-05-24 11:48:26 +03:00
|
|
|
new TaskQueue(GetMediaThreadPool(MediaThreadType::PLATFORM_DECODER));
|
2015-05-18 08:40:32 +03:00
|
|
|
mVideo.mTaskQueue =
|
2016-05-24 11:48:26 +03:00
|
|
|
new TaskQueue(GetMediaThreadPool(MediaThreadType::PLATFORM_DECODER));
|
2015-05-18 08:40:32 +03:00
|
|
|
|
2017-02-01 15:25:12 +03:00
|
|
|
if (mDecoder) {
|
|
|
|
// Note: GMPCrashHelper must be created on main thread, as it may use
|
|
|
|
// weak references, which aren't threadsafe.
|
|
|
|
mCrashHelper = mDecoder->GetCrashHelper();
|
|
|
|
}
|
2015-05-18 08:40:32 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-01-26 15:56:46 +03:00
|
|
|
class DispatchKeyNeededEvent : public Runnable
|
|
|
|
{
|
2015-05-18 08:40:32 +03:00
|
|
|
public:
|
|
|
|
DispatchKeyNeededEvent(AbstractMediaDecoder* aDecoder,
|
|
|
|
nsTArray<uint8_t>& aInitData,
|
|
|
|
const nsString& aInitDataType)
|
|
|
|
: mDecoder(aDecoder)
|
|
|
|
, mInitData(aInitData)
|
|
|
|
, mInitDataType(aInitDataType)
|
|
|
|
{
|
|
|
|
}
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2015-05-18 08:40:32 +03:00
|
|
|
// Note: Null check the owner, as the decoder could have been shutdown
|
|
|
|
// since this event was dispatched.
|
|
|
|
MediaDecoderOwner* owner = mDecoder->GetOwner();
|
|
|
|
if (owner) {
|
|
|
|
owner->DispatchEncrypted(mInitData, mInitDataType);
|
|
|
|
}
|
|
|
|
mDecoder = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AbstractMediaDecoder> mDecoder;
|
2015-05-18 08:40:32 +03:00
|
|
|
nsTArray<uint8_t> mInitData;
|
|
|
|
nsString mInitDataType;
|
|
|
|
};
|
2015-09-27 13:40:03 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::SetCDMProxy(CDMProxy* aProxy)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CDMProxy> proxy = aProxy;
|
|
|
|
RefPtr<MediaFormatReader> self = this;
|
2015-09-27 13:40:03 +03:00
|
|
|
nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction([=] () {
|
2015-10-08 09:05:06 +03:00
|
|
|
MOZ_ASSERT(self->OnTaskQueue());
|
2015-09-27 13:40:03 +03:00
|
|
|
self->mCDMProxy = proxy;
|
|
|
|
});
|
2015-10-08 09:05:06 +03:00
|
|
|
OwnerThread()->Dispatch(r.forget());
|
2015-09-27 13:40:03 +03:00
|
|
|
}
|
2015-05-18 08:40:32 +03:00
|
|
|
|
2015-09-27 13:59:52 +03:00
|
|
|
bool
|
2017-01-26 15:56:46 +03:00
|
|
|
MediaFormatReader::IsWaitingOnCDMResource()
|
|
|
|
{
|
2015-09-27 13:59:52 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2015-09-27 13:40:03 +03:00
|
|
|
return IsEncrypted() && !mCDMProxy;
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaDecoderReader::MetadataPromise>
|
2015-10-21 05:59:56 +03:00
|
|
|
MediaFormatReader::AsyncReadMetadata()
|
2015-05-18 08:40:32 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
|
2015-10-21 04:28:04 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mMetadataPromise.IsEmpty());
|
2015-08-11 06:50:07 +03:00
|
|
|
|
2015-05-18 08:40:32 +03:00
|
|
|
if (mInitDone) {
|
|
|
|
// We are returning from dormant.
|
2015-10-21 04:28:04 +03:00
|
|
|
RefPtr<MetadataHolder> metadata = new MetadataHolder();
|
|
|
|
metadata->mInfo = mInfo;
|
|
|
|
metadata->mTags = nullptr;
|
|
|
|
return MetadataPromise::CreateAndResolve(metadata, __func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
2015-10-21 04:28:04 +03:00
|
|
|
RefPtr<MetadataPromise> p = mMetadataPromise.Ensure(__func__);
|
|
|
|
|
2017-01-11 11:33:29 +03:00
|
|
|
mDemuxer->Init()
|
|
|
|
->Then(OwnerThread(), __func__, this,
|
|
|
|
&MediaFormatReader::OnDemuxerInitDone,
|
|
|
|
&MediaFormatReader::OnDemuxerInitFailed)
|
|
|
|
->Track(mDemuxerInitRequest);
|
2015-05-18 08:40:32 +03:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::OnDemuxerInitDone(nsresult)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
mDemuxerInitRequest.Complete();
|
|
|
|
|
2015-10-07 04:00:52 +03:00
|
|
|
mDemuxerInitDone = true;
|
|
|
|
|
2016-01-08 03:22:53 +03:00
|
|
|
UniquePtr<MetadataTags> tags(MakeUnique<MetadataTags>());
|
|
|
|
|
2016-05-12 05:00:21 +03:00
|
|
|
RefPtr<PDMFactory> platform;
|
|
|
|
if (!IsWaitingOnCDMResource()) {
|
|
|
|
platform = new PDMFactory();
|
|
|
|
}
|
|
|
|
|
2015-05-18 08:40:32 +03:00
|
|
|
// To decode, we need valid video and a place to put it.
|
|
|
|
bool videoActive = !!mDemuxer->GetNumberTracks(TrackInfo::kVideoTrack) &&
|
2015-10-15 03:04:00 +03:00
|
|
|
GetImageContainer();
|
2015-05-18 08:40:32 +03:00
|
|
|
|
|
|
|
if (videoActive) {
|
|
|
|
// We currently only handle the first video track.
|
|
|
|
mVideo.mTrackDemuxer = mDemuxer->GetTrackDemuxer(TrackInfo::kVideoTrack, 0);
|
2015-08-26 04:25:49 +03:00
|
|
|
if (!mVideo.mTrackDemuxer) {
|
2016-09-10 12:56:50 +03:00
|
|
|
mMetadataPromise.Reject(NS_ERROR_DOM_MEDIA_METADATA_ERR, __func__);
|
2015-08-26 04:25:49 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-05-12 05:00:21 +03:00
|
|
|
|
2016-05-12 04:42:20 +03:00
|
|
|
UniquePtr<TrackInfo> videoInfo = mVideo.mTrackDemuxer->GetInfo();
|
|
|
|
videoActive = videoInfo && videoInfo->IsValid();
|
|
|
|
if (videoActive) {
|
2016-05-12 05:00:21 +03:00
|
|
|
if (platform && !platform->SupportsMimeType(videoInfo->mMimeType, nullptr)) {
|
|
|
|
// We have no decoder for this track. Error.
|
2016-09-10 12:56:50 +03:00
|
|
|
mMetadataPromise.Reject(NS_ERROR_DOM_MEDIA_METADATA_ERR, __func__);
|
2016-05-12 05:00:21 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-05-12 04:42:20 +03:00
|
|
|
mInfo.mVideo = *videoInfo->GetAsVideoInfo();
|
|
|
|
for (const MetadataTag& tag : videoInfo->mTags) {
|
|
|
|
tags->Put(tag.mKey, tag.mValue);
|
|
|
|
}
|
2016-10-26 12:09:41 +03:00
|
|
|
mVideo.mOriginalInfo = Move(videoInfo);
|
2016-05-12 04:42:20 +03:00
|
|
|
mTrackDemuxersMayBlock |= mVideo.mTrackDemuxer->GetSamplesMayBlock();
|
|
|
|
} else {
|
|
|
|
mVideo.mTrackDemuxer->BreakCycles();
|
|
|
|
mVideo.mTrackDemuxer = nullptr;
|
2016-01-08 03:22:53 +03:00
|
|
|
}
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool audioActive = !!mDemuxer->GetNumberTracks(TrackInfo::kAudioTrack);
|
|
|
|
if (audioActive) {
|
|
|
|
mAudio.mTrackDemuxer = mDemuxer->GetTrackDemuxer(TrackInfo::kAudioTrack, 0);
|
2015-08-26 04:25:49 +03:00
|
|
|
if (!mAudio.mTrackDemuxer) {
|
2016-09-10 12:56:50 +03:00
|
|
|
mMetadataPromise.Reject(NS_ERROR_DOM_MEDIA_METADATA_ERR, __func__);
|
2015-08-26 04:25:49 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-05-12 05:00:21 +03:00
|
|
|
|
2016-04-27 10:35:58 +03:00
|
|
|
UniquePtr<TrackInfo> audioInfo = mAudio.mTrackDemuxer->GetInfo();
|
|
|
|
// We actively ignore audio tracks that we know we can't play.
|
2016-05-12 05:00:21 +03:00
|
|
|
audioActive = audioInfo && audioInfo->IsValid() &&
|
|
|
|
(!platform ||
|
|
|
|
platform->SupportsMimeType(audioInfo->mMimeType, nullptr));
|
|
|
|
|
2016-04-27 10:35:58 +03:00
|
|
|
if (audioActive) {
|
|
|
|
mInfo.mAudio = *audioInfo->GetAsAudioInfo();
|
|
|
|
for (const MetadataTag& tag : audioInfo->mTags) {
|
|
|
|
tags->Put(tag.mKey, tag.mValue);
|
|
|
|
}
|
2016-10-26 12:09:41 +03:00
|
|
|
mAudio.mOriginalInfo = Move(audioInfo);
|
2016-04-27 10:35:58 +03:00
|
|
|
mTrackDemuxersMayBlock |= mAudio.mTrackDemuxer->GetSamplesMayBlock();
|
|
|
|
} else {
|
|
|
|
mAudio.mTrackDemuxer->BreakCycles();
|
|
|
|
mAudio.mTrackDemuxer = nullptr;
|
2016-01-08 03:22:53 +03:00
|
|
|
}
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
UniquePtr<EncryptionInfo> crypto = mDemuxer->GetCrypto();
|
2015-10-15 03:04:00 +03:00
|
|
|
if (mDecoder && crypto && crypto->IsEncrypted()) {
|
2015-05-18 08:40:32 +03:00
|
|
|
// Try and dispatch 'encrypted'. Won't go if ready state still HAVE_NOTHING.
|
|
|
|
for (uint32_t i = 0; i < crypto->mInitDatas.Length(); i++) {
|
|
|
|
NS_DispatchToMainThread(
|
2016-06-08 05:07:09 +03:00
|
|
|
new DispatchKeyNeededEvent(mDecoder, crypto->mInitDatas[i].mInitData, crypto->mInitDatas[i].mType));
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
mInfo.mCrypto = *crypto;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t videoDuration = HasVideo() ? mInfo.mVideo.mDuration : 0;
|
|
|
|
int64_t audioDuration = HasAudio() ? mInfo.mAudio.mDuration : 0;
|
|
|
|
|
|
|
|
int64_t duration = std::max(videoDuration, audioDuration);
|
|
|
|
if (duration != -1) {
|
2015-05-31 07:18:12 +03:00
|
|
|
mInfo.mMetadataDuration = Some(TimeUnit::FromMicroseconds(duration));
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
2015-12-02 10:42:32 +03:00
|
|
|
mInfo.mMediaSeekable = mDemuxer->IsSeekable();
|
2016-02-04 07:31:21 +03:00
|
|
|
mInfo.mMediaSeekableOnlyInBufferedRanges =
|
|
|
|
mDemuxer->IsSeekableOnlyInBufferedRanges();
|
2015-05-18 08:40:32 +03:00
|
|
|
|
2015-07-14 17:25:44 +03:00
|
|
|
if (!videoActive && !audioActive) {
|
2016-09-10 12:56:50 +03:00
|
|
|
mMetadataPromise.Reject(NS_ERROR_DOM_MEDIA_METADATA_ERR, __func__);
|
2015-07-14 17:25:44 +03:00
|
|
|
return;
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
2016-10-27 10:21:05 +03:00
|
|
|
mTags = Move(tags);
|
2015-10-21 04:28:04 +03:00
|
|
|
mInitDone = true;
|
2016-10-27 10:21:05 +03:00
|
|
|
|
|
|
|
// Try to get the start time.
|
|
|
|
// For MSE case, the start time of each track is assumed to be 0.
|
|
|
|
// For others, we must demux the first sample to know the start time for each
|
|
|
|
// track.
|
2016-12-21 09:58:38 +03:00
|
|
|
if (!mDemuxer->ShouldComputeStartTime()) {
|
2016-10-27 10:21:05 +03:00
|
|
|
mAudio.mFirstDemuxedSampleTime.emplace(TimeUnit::FromMicroseconds(0));
|
|
|
|
mVideo.mFirstDemuxedSampleTime.emplace(TimeUnit::FromMicroseconds(0));
|
|
|
|
} else {
|
|
|
|
if (HasAudio()) {
|
|
|
|
RequestDemuxSamples(TrackInfo::kAudioTrack);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HasVideo()) {
|
|
|
|
RequestDemuxSamples(TrackInfo::kVideoTrack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MaybeResolveMetadataPromise();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::MaybeResolveMetadataPromise()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
|
|
|
|
if ((HasAudio() && mAudio.mFirstDemuxedSampleTime.isNothing()) ||
|
|
|
|
(HasVideo() && mVideo.mFirstDemuxedSampleTime.isNothing())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeUnit startTime =
|
|
|
|
std::min(mAudio.mFirstDemuxedSampleTime.refOr(TimeUnit::FromInfinity()),
|
|
|
|
mVideo.mFirstDemuxedSampleTime.refOr(TimeUnit::FromInfinity()));
|
|
|
|
|
|
|
|
if (!startTime.IsInfinite()) {
|
|
|
|
mInfo.mStartTime = startTime; // mInfo.mStartTime is initialized to 0.
|
|
|
|
}
|
|
|
|
|
2016-12-19 14:39:21 +03:00
|
|
|
RefPtr<MetadataHolder> metadata = new MetadataHolder();
|
|
|
|
metadata->mInfo = mInfo;
|
|
|
|
metadata->mTags = mTags->Count() ? mTags.release() : nullptr;
|
2016-11-28 13:08:01 +03:00
|
|
|
|
|
|
|
// We now have all the informations required to calculate the initial buffered
|
|
|
|
// range.
|
|
|
|
mHasStartTime = true;
|
|
|
|
UpdateBuffered();
|
|
|
|
|
2015-10-21 04:28:04 +03:00
|
|
|
mMetadataPromise.Resolve(metadata, __func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
2016-09-28 03:34:57 +03:00
|
|
|
bool
|
|
|
|
MediaFormatReader::IsEncrypted() const
|
|
|
|
{
|
|
|
|
return (HasAudio() && mInfo.mAudio.mCrypto.mValid) ||
|
|
|
|
(HasVideo() && mInfo.mVideo.mCrypto.mValid);
|
|
|
|
}
|
|
|
|
|
2015-05-18 08:40:32 +03:00
|
|
|
void
|
2016-09-12 05:22:20 +03:00
|
|
|
MediaFormatReader::OnDemuxerInitFailed(const MediaResult& aError)
|
2015-05-18 08:40:32 +03:00
|
|
|
{
|
|
|
|
mDemuxerInitRequest.Complete();
|
2016-09-12 05:22:20 +03:00
|
|
|
mMetadataPromise.Reject(aError, __func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::ReadUpdatedMetadata(MediaInfo* aInfo)
|
|
|
|
{
|
|
|
|
*aInfo = mInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaFormatReader::DecoderData&
|
|
|
|
MediaFormatReader::GetDecoderData(TrackType aTrack)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aTrack == TrackInfo::kAudioTrack ||
|
|
|
|
aTrack == TrackInfo::kVideoTrack);
|
|
|
|
if (aTrack == TrackInfo::kAudioTrack) {
|
|
|
|
return mAudio;
|
|
|
|
}
|
|
|
|
return mVideo;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MediaFormatReader::ShouldSkip(bool aSkipToNextKeyframe, media::TimeUnit aTimeThreshold)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(HasVideo());
|
|
|
|
media::TimeUnit nextKeyframe;
|
|
|
|
nsresult rv = mVideo.mTrackDemuxer->GetNextRandomAccessPoint(&nextKeyframe);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return aSkipToNextKeyframe;
|
|
|
|
}
|
2016-05-06 08:54:49 +03:00
|
|
|
return (nextKeyframe < aTimeThreshold ||
|
|
|
|
(mVideo.mTimeThreshold &&
|
2016-05-18 06:48:05 +03:00
|
|
|
mVideo.mTimeThreshold.ref().EndTime() < aTimeThreshold)) &&
|
2016-05-17 17:26:34 +03:00
|
|
|
nextKeyframe.ToMicroseconds() >= 0 && !nextKeyframe.IsInfinite();
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
2016-04-22 09:16:49 +03:00
|
|
|
RefPtr<MediaDecoderReader::MediaDataPromise>
|
2015-05-18 08:40:32 +03:00
|
|
|
MediaFormatReader::RequestVideoData(bool aSkipToNextKeyframe,
|
2015-10-20 12:33:00 +03:00
|
|
|
int64_t aTimeThreshold)
|
2015-05-18 08:40:32 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mSeekPromise.IsEmpty(), "No sample requests allowed while seeking");
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mVideo.HasPromise(), "No duplicate sample requests");
|
2015-07-15 07:43:35 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mVideo.mSeekRequest.Exists() ||
|
|
|
|
mVideo.mTimeThreshold.isSome());
|
2015-05-18 08:40:32 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!IsSeeking(), "called mid-seek");
|
|
|
|
LOGV("RequestVideoData(%d, %lld)", aSkipToNextKeyframe, aTimeThreshold);
|
|
|
|
|
|
|
|
if (!HasVideo()) {
|
|
|
|
LOG("called with no video track");
|
2016-09-10 09:48:53 +03:00
|
|
|
return MediaDataPromise::CreateAndReject(NS_ERROR_DOM_MEDIA_FATAL_ERR, __func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (IsSeeking()) {
|
|
|
|
LOG("called mid-seek. Rejecting.");
|
2016-09-10 09:48:53 +03:00
|
|
|
return MediaDataPromise::CreateAndReject(NS_ERROR_DOM_MEDIA_CANCELED, __func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mShutdown) {
|
|
|
|
NS_WARNING("RequestVideoData on shutdown MediaFormatReader!");
|
2016-09-10 09:48:53 +03:00
|
|
|
return MediaDataPromise::CreateAndReject(NS_ERROR_DOM_MEDIA_CANCELED, __func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
media::TimeUnit timeThreshold{media::TimeUnit::FromMicroseconds(aTimeThreshold)};
|
2016-05-05 07:52:58 +03:00
|
|
|
// Ensure we have no pending seek going as ShouldSkip could return out of date
|
|
|
|
// information.
|
|
|
|
if (!mVideo.HasInternalSeekPending() &&
|
|
|
|
ShouldSkip(aSkipToNextKeyframe, timeThreshold)) {
|
2016-05-05 08:06:40 +03:00
|
|
|
RefPtr<MediaDataPromise> p = mVideo.EnsurePromise(__func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
SkipVideoDemuxToNextKeyFrame(timeThreshold);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2016-05-05 08:11:21 +03:00
|
|
|
RefPtr<MediaDataPromise> p = mVideo.EnsurePromise(__func__);
|
2016-09-01 12:25:54 +03:00
|
|
|
ScheduleUpdate(TrackInfo::kVideoTrack);
|
2015-08-24 05:50:27 +03:00
|
|
|
|
2015-05-18 08:40:32 +03:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-09-12 05:22:20 +03:00
|
|
|
MediaFormatReader::OnDemuxFailed(TrackType aTrack, const MediaResult& aError)
|
2015-05-18 08:40:32 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2016-09-12 05:22:20 +03:00
|
|
|
LOG("Failed to demux %s, failure:%u",
|
|
|
|
aTrack == TrackType::kVideoTrack ? "video" : "audio", aError.Code());
|
2015-05-18 08:40:32 +03:00
|
|
|
auto& decoder = GetDecoderData(aTrack);
|
|
|
|
decoder.mDemuxRequest.Complete();
|
2016-09-12 05:22:20 +03:00
|
|
|
switch (aError.Code()) {
|
|
|
|
case NS_ERROR_DOM_MEDIA_END_OF_STREAM:
|
2016-05-23 09:09:12 +03:00
|
|
|
if (!decoder.mWaitingForData) {
|
|
|
|
decoder.mNeedDraining = true;
|
|
|
|
}
|
2015-06-15 03:58:12 +03:00
|
|
|
NotifyEndOfStream(aTrack);
|
2015-05-18 08:40:32 +03:00
|
|
|
break;
|
2016-09-12 05:22:20 +03:00
|
|
|
case NS_ERROR_DOM_MEDIA_WAITING_FOR_DATA:
|
2015-12-08 22:30:25 +03:00
|
|
|
if (!decoder.mWaitingForData) {
|
|
|
|
decoder.mNeedDraining = true;
|
|
|
|
}
|
2015-05-18 08:42:01 +03:00
|
|
|
NotifyWaitingForData(aTrack);
|
2015-05-18 08:40:32 +03:00
|
|
|
break;
|
2016-09-12 05:22:20 +03:00
|
|
|
case NS_ERROR_DOM_MEDIA_CANCELED:
|
2015-05-18 08:40:32 +03:00
|
|
|
if (decoder.HasPromise()) {
|
2016-09-10 09:48:53 +03:00
|
|
|
decoder.RejectPromise(NS_ERROR_DOM_MEDIA_CANCELED, __func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2016-09-12 05:22:20 +03:00
|
|
|
NotifyError(aTrack, aError);
|
2015-05-18 08:40:32 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::DoDemuxVideo()
|
|
|
|
{
|
2016-10-27 10:21:05 +03:00
|
|
|
auto p = mVideo.mTrackDemuxer->GetSamples(1);
|
|
|
|
|
|
|
|
if (mVideo.mFirstDemuxedSampleTime.isNothing()) {
|
|
|
|
RefPtr<MediaFormatReader> self = this;
|
2016-12-04 03:51:25 +03:00
|
|
|
p = p->Then(OwnerThread(), __func__,
|
2016-10-27 10:21:05 +03:00
|
|
|
[self] (RefPtr<MediaTrackDemuxer::SamplesHolder> aSamples) {
|
|
|
|
self->OnFirstDemuxCompleted(TrackInfo::kVideoTrack, aSamples);
|
|
|
|
},
|
|
|
|
[self] (const MediaResult& aError) {
|
|
|
|
self->OnFirstDemuxFailed(TrackInfo::kVideoTrack, aError);
|
2016-11-30 13:19:26 +03:00
|
|
|
});
|
2016-10-27 10:21:05 +03:00
|
|
|
}
|
|
|
|
|
2017-01-11 11:33:29 +03:00
|
|
|
p->Then(OwnerThread(), __func__, this,
|
|
|
|
&MediaFormatReader::OnVideoDemuxCompleted,
|
|
|
|
&MediaFormatReader::OnVideoDemuxFailed)
|
|
|
|
->Track(mVideo.mDemuxRequest);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-10-18 08:24:48 +03:00
|
|
|
MediaFormatReader::OnVideoDemuxCompleted(RefPtr<MediaTrackDemuxer::SamplesHolder> aSamples)
|
2015-05-18 08:40:32 +03:00
|
|
|
{
|
2015-06-15 03:58:12 +03:00
|
|
|
LOGV("%d video samples demuxed (sid:%d)",
|
|
|
|
aSamples->mSamples.Length(),
|
|
|
|
aSamples->mSamples[0]->mTrackInfo ? aSamples->mSamples[0]->mTrackInfo->GetID() : 0);
|
2015-05-18 08:40:32 +03:00
|
|
|
mVideo.mDemuxRequest.Complete();
|
|
|
|
mVideo.mQueuedSamples.AppendElements(aSamples->mSamples);
|
|
|
|
ScheduleUpdate(TrackInfo::kVideoTrack);
|
|
|
|
}
|
|
|
|
|
2016-04-22 09:16:49 +03:00
|
|
|
RefPtr<MediaDecoderReader::MediaDataPromise>
|
2015-05-18 08:40:32 +03:00
|
|
|
MediaFormatReader::RequestAudioData()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mAudio.HasPromise(), "No duplicate sample requests");
|
2016-05-16 11:15:45 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(IsVideoSeeking() || mSeekPromise.IsEmpty(),
|
|
|
|
"No sample requests allowed while seeking");
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(IsVideoSeeking() ||
|
|
|
|
!mAudio.mSeekRequest.Exists() ||
|
|
|
|
mAudio.mTimeThreshold.isSome());
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(IsVideoSeeking() || !IsSeeking(), "called mid-seek");
|
2015-05-18 08:40:32 +03:00
|
|
|
LOGV("");
|
|
|
|
|
|
|
|
if (!HasAudio()) {
|
|
|
|
LOG("called with no audio track");
|
2016-09-10 09:48:53 +03:00
|
|
|
return MediaDataPromise::CreateAndReject(NS_ERROR_DOM_MEDIA_FATAL_ERR, __func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (IsSeeking()) {
|
|
|
|
LOG("called mid-seek. Rejecting.");
|
2016-09-10 09:48:53 +03:00
|
|
|
return MediaDataPromise::CreateAndReject(NS_ERROR_DOM_MEDIA_CANCELED, __func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mShutdown) {
|
|
|
|
NS_WARNING("RequestAudioData on shutdown MediaFormatReader!");
|
2016-09-10 09:48:53 +03:00
|
|
|
return MediaDataPromise::CreateAndReject(NS_ERROR_DOM_MEDIA_CANCELED, __func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
2016-05-05 08:11:21 +03:00
|
|
|
RefPtr<MediaDataPromise> p = mAudio.EnsurePromise(__func__);
|
2016-09-01 12:25:54 +03:00
|
|
|
ScheduleUpdate(TrackInfo::kAudioTrack);
|
2015-08-11 06:50:07 +03:00
|
|
|
|
2015-05-18 08:40:32 +03:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::DoDemuxAudio()
|
|
|
|
{
|
2016-10-27 10:21:05 +03:00
|
|
|
auto p = mAudio.mTrackDemuxer->GetSamples(1);
|
|
|
|
|
|
|
|
if (mAudio.mFirstDemuxedSampleTime.isNothing()) {
|
|
|
|
RefPtr<MediaFormatReader> self = this;
|
2016-12-04 03:51:25 +03:00
|
|
|
p = p->Then(OwnerThread(), __func__,
|
2016-10-27 10:21:05 +03:00
|
|
|
[self] (RefPtr<MediaTrackDemuxer::SamplesHolder> aSamples) {
|
|
|
|
self->OnFirstDemuxCompleted(TrackInfo::kAudioTrack, aSamples);
|
|
|
|
},
|
|
|
|
[self] (const MediaResult& aError) {
|
|
|
|
self->OnFirstDemuxFailed(TrackInfo::kAudioTrack, aError);
|
2016-11-30 13:19:26 +03:00
|
|
|
});
|
2016-10-27 10:21:05 +03:00
|
|
|
}
|
|
|
|
|
2017-01-11 11:33:29 +03:00
|
|
|
p->Then(OwnerThread(), __func__, this,
|
|
|
|
&MediaFormatReader::OnAudioDemuxCompleted,
|
|
|
|
&MediaFormatReader::OnAudioDemuxFailed)
|
|
|
|
->Track(mAudio.mDemuxRequest);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-10-18 08:24:48 +03:00
|
|
|
MediaFormatReader::OnAudioDemuxCompleted(RefPtr<MediaTrackDemuxer::SamplesHolder> aSamples)
|
2015-05-18 08:40:32 +03:00
|
|
|
{
|
2015-06-15 03:58:12 +03:00
|
|
|
LOGV("%d audio samples demuxed (sid:%d)",
|
|
|
|
aSamples->mSamples.Length(),
|
|
|
|
aSamples->mSamples[0]->mTrackInfo ? aSamples->mSamples[0]->mTrackInfo->GetID() : 0);
|
2015-05-18 08:40:32 +03:00
|
|
|
mAudio.mDemuxRequest.Complete();
|
|
|
|
mAudio.mQueuedSamples.AppendElements(aSamples->mSamples);
|
|
|
|
ScheduleUpdate(TrackInfo::kAudioTrack);
|
|
|
|
}
|
|
|
|
|
2015-05-18 08:42:01 +03:00
|
|
|
void
|
2017-01-26 15:56:46 +03:00
|
|
|
MediaFormatReader::NotifyNewOutput(TrackType aTrack,
|
|
|
|
const MediaDataDecoder::DecodedData& aResults)
|
2015-05-18 08:42:01 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
auto& decoder = GetDecoderData(aTrack);
|
2017-01-26 15:56:46 +03:00
|
|
|
for (auto& sample : aResults) {
|
|
|
|
LOGV("Received new %s sample time:%lld duration:%lld",
|
|
|
|
TrackTypeToStr(aTrack), sample->mTime, sample->mDuration);
|
|
|
|
decoder.mOutput.AppendElement(sample);
|
|
|
|
decoder.mNumSamplesOutput++;
|
|
|
|
decoder.mNumOfConsecutiveError = 0;
|
|
|
|
}
|
|
|
|
LOG("Done processing new %s samples", TrackTypeToStr(aTrack));
|
2015-05-18 08:42:01 +03:00
|
|
|
ScheduleUpdate(aTrack);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::NotifyDrainComplete(TrackType aTrack)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
auto& decoder = GetDecoderData(aTrack);
|
2015-07-26 14:52:22 +03:00
|
|
|
LOG("%s", TrackTypeToStr(aTrack));
|
2015-05-18 08:42:01 +03:00
|
|
|
decoder.mDrainComplete = true;
|
|
|
|
ScheduleUpdate(aTrack);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-09-10 02:56:53 +03:00
|
|
|
MediaFormatReader::NotifyError(TrackType aTrack, const MediaResult& aError)
|
2015-05-18 08:42:01 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2016-09-20 08:59:42 +03:00
|
|
|
NS_WARNING(aError.Description().get());
|
2015-05-25 08:09:16 +03:00
|
|
|
LOGV("%s Decoding error", TrackTypeToStr(aTrack));
|
2015-05-18 08:42:01 +03:00
|
|
|
auto& decoder = GetDecoderData(aTrack);
|
2016-05-30 19:24:00 +03:00
|
|
|
decoder.mError = decoder.HasFatalError() ? decoder.mError : Some(aError);
|
2015-05-18 08:42:01 +03:00
|
|
|
ScheduleUpdate(aTrack);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::NotifyWaitingForData(TrackType aTrack)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
auto& decoder = GetDecoderData(aTrack);
|
|
|
|
decoder.mWaitingForData = true;
|
2015-12-08 22:30:25 +03:00
|
|
|
if (decoder.mTimeThreshold) {
|
|
|
|
decoder.mTimeThreshold.ref().mWaiting = true;
|
|
|
|
}
|
2015-05-18 08:42:01 +03:00
|
|
|
ScheduleUpdate(aTrack);
|
|
|
|
}
|
|
|
|
|
2015-06-15 03:58:12 +03:00
|
|
|
void
|
|
|
|
MediaFormatReader::NotifyEndOfStream(TrackType aTrack)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
auto& decoder = GetDecoderData(aTrack);
|
|
|
|
decoder.mDemuxEOS = true;
|
|
|
|
ScheduleUpdate(aTrack);
|
|
|
|
}
|
|
|
|
|
2015-05-18 08:40:32 +03:00
|
|
|
bool
|
|
|
|
MediaFormatReader::NeedInput(DecoderData& aDecoder)
|
|
|
|
{
|
2017-01-26 15:56:46 +03:00
|
|
|
// The decoder will not be fed a new raw sample until the current decoding
|
|
|
|
// requests has completed.
|
2015-05-18 08:40:32 +03:00
|
|
|
return
|
2016-09-01 12:25:54 +03:00
|
|
|
(aDecoder.HasPromise() || aDecoder.mTimeThreshold.isSome()) &&
|
2016-06-02 14:08:05 +03:00
|
|
|
!aDecoder.HasPendingDrain() &&
|
2016-05-30 19:24:00 +03:00
|
|
|
!aDecoder.HasFatalError() &&
|
2015-05-25 08:09:16 +03:00
|
|
|
!aDecoder.mDemuxRequest.Exists() &&
|
2016-09-01 12:25:54 +03:00
|
|
|
!aDecoder.mOutput.Length() &&
|
2016-05-05 07:52:58 +03:00
|
|
|
!aDecoder.HasInternalSeekPending() &&
|
2017-01-26 15:56:46 +03:00
|
|
|
!aDecoder.mDecodeRequest.Exists();
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::ScheduleUpdate(TrackType aTrack)
|
|
|
|
{
|
2015-05-18 08:42:01 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
if (mShutdown) {
|
|
|
|
return;
|
|
|
|
}
|
2015-05-18 08:40:32 +03:00
|
|
|
auto& decoder = GetDecoderData(aTrack);
|
|
|
|
if (decoder.mUpdateScheduled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LOGV("SchedulingUpdate(%s)", TrackTypeToStr(aTrack));
|
|
|
|
decoder.mUpdateScheduled = true;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsIRunnable> task(
|
2016-05-05 11:45:00 +03:00
|
|
|
NewRunnableMethod<TrackType>(this, &MediaFormatReader::Update, aTrack));
|
2015-07-16 21:31:21 +03:00
|
|
|
OwnerThread()->Dispatch(task.forget());
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MediaFormatReader::UpdateReceivedNewData(TrackType aTrack)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
auto& decoder = GetDecoderData(aTrack);
|
|
|
|
|
|
|
|
if (!decoder.mReceivedNewData) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-12-08 22:30:25 +03:00
|
|
|
|
2016-05-05 07:52:58 +03:00
|
|
|
// We do not want to clear mWaitingForData while there are pending
|
|
|
|
// demuxing or seeking operations that could affect the value of this flag.
|
|
|
|
// This is in order to ensure that we will retry once they complete as we may
|
|
|
|
// now have new data that could potentially allow those operations to
|
|
|
|
// successfully complete if tried again.
|
|
|
|
if (decoder.mSeekRequest.Exists()) {
|
|
|
|
// Nothing more to do until this operation complete.
|
|
|
|
return true;
|
|
|
|
}
|
2016-05-21 18:02:43 +03:00
|
|
|
|
|
|
|
if (aTrack == TrackType::kVideoTrack && mSkipRequest.Exists()) {
|
|
|
|
LOGV("Skipping in progress, nothing more to do");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-05 07:52:58 +03:00
|
|
|
if (decoder.mDemuxRequest.Exists()) {
|
|
|
|
// We may have pending operations to process, so we want to continue
|
|
|
|
// after UpdateReceivedNewData returns.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-02 14:08:05 +03:00
|
|
|
if (decoder.HasPendingDrain()) {
|
2015-12-08 22:30:25 +03:00
|
|
|
// We do not want to clear mWaitingForData or mDemuxEOS while
|
|
|
|
// a drain is in progress in order to properly complete the operation.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder.mReceivedNewData = false;
|
|
|
|
if (decoder.mTimeThreshold) {
|
|
|
|
decoder.mTimeThreshold.ref().mWaiting = false;
|
|
|
|
}
|
|
|
|
decoder.mWaitingForData = false;
|
|
|
|
|
2016-05-30 19:24:00 +03:00
|
|
|
if (decoder.HasFatalError()) {
|
2015-05-25 08:09:16 +03:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-05 07:52:58 +03:00
|
|
|
|
2016-05-16 11:15:45 +03:00
|
|
|
if (!mSeekPromise.IsEmpty() &&
|
|
|
|
(!IsVideoSeeking() || aTrack == TrackInfo::kVideoTrack)) {
|
2016-05-05 08:01:51 +03:00
|
|
|
MOZ_ASSERT(!decoder.HasPromise());
|
2016-05-16 11:15:45 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT((IsVideoSeeking() || !mAudio.mTimeThreshold) &&
|
|
|
|
!mVideo.mTimeThreshold,
|
2016-05-05 08:01:51 +03:00
|
|
|
"InternalSeek must have been aborted when Seek was first called");
|
2016-05-16 11:15:45 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT((IsVideoSeeking() || !mAudio.HasWaitingPromise()) &&
|
|
|
|
!mVideo.HasWaitingPromise(),
|
2016-05-05 08:01:51 +03:00
|
|
|
"Waiting promises must have been rejected when Seek was first called");
|
2016-05-16 11:15:45 +03:00
|
|
|
if (mVideo.mSeekRequest.Exists() ||
|
|
|
|
(!IsVideoSeeking() && mAudio.mSeekRequest.Exists())) {
|
2016-05-05 08:01:51 +03:00
|
|
|
// Already waiting for a seek to complete. Nothing more to do.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
LOG("Attempting Seek");
|
|
|
|
ScheduleSeek();
|
|
|
|
return true;
|
|
|
|
}
|
2016-05-05 07:52:58 +03:00
|
|
|
if (decoder.HasInternalSeekPending() || decoder.HasWaitingPromise()) {
|
|
|
|
if (decoder.HasInternalSeekPending()) {
|
|
|
|
LOG("Attempting Internal Seek");
|
|
|
|
InternalSeek(aTrack, decoder.mTimeThreshold.ref());
|
|
|
|
}
|
2016-10-27 12:25:21 +03:00
|
|
|
if (decoder.HasWaitingPromise() && !decoder.IsWaiting()) {
|
2016-05-05 07:52:58 +03:00
|
|
|
MOZ_ASSERT(!decoder.HasPromise());
|
|
|
|
LOG("We have new data. Resolving WaitingPromise");
|
|
|
|
decoder.mWaitingPromise.Resolve(decoder.mType, __func__);
|
|
|
|
}
|
2015-05-18 08:40:32 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::RequestDemuxSamples(TrackType aTrack)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
auto& decoder = GetDecoderData(aTrack);
|
2015-05-25 08:09:16 +03:00
|
|
|
MOZ_ASSERT(!decoder.mDemuxRequest.Exists());
|
2015-05-18 08:40:32 +03:00
|
|
|
|
|
|
|
if (!decoder.mQueuedSamples.IsEmpty()) {
|
|
|
|
// No need to demux new samples.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (decoder.mDemuxEOS) {
|
|
|
|
// Nothing left to demux.
|
2015-12-08 22:30:25 +03:00
|
|
|
// We do not want to attempt to demux while in waiting for data mode
|
|
|
|
// as it would retrigger an unecessary drain.
|
2015-05-18 08:40:32 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-05-25 08:09:16 +03:00
|
|
|
|
|
|
|
LOGV("Requesting extra demux %s", TrackTypeToStr(aTrack));
|
2015-05-18 08:40:32 +03:00
|
|
|
if (aTrack == TrackInfo::kVideoTrack) {
|
|
|
|
DoDemuxVideo();
|
|
|
|
} else {
|
|
|
|
DoDemuxAudio();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-09 06:50:37 +03:00
|
|
|
void
|
2015-05-18 08:40:32 +03:00
|
|
|
MediaFormatReader::DecodeDemuxedSamples(TrackType aTrack,
|
2015-10-20 12:33:00 +03:00
|
|
|
MediaRawData* aSample)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
auto& decoder = GetDecoderData(aTrack);
|
2017-01-26 15:56:46 +03:00
|
|
|
RefPtr<MediaFormatReader> self = this;
|
|
|
|
decoder.mFlushed = false;
|
|
|
|
decoder.mDecoder->Decode(aSample)
|
|
|
|
->Then(mTaskQueue, __func__,
|
|
|
|
[self, this, aTrack, &decoder]
|
|
|
|
(const MediaDataDecoder::DecodedData& aResults) {
|
|
|
|
decoder.mDecodeRequest.Complete();
|
|
|
|
NotifyNewOutput(aTrack, aResults);
|
|
|
|
},
|
|
|
|
[self, this, aTrack, &decoder](const MediaResult& aError) {
|
|
|
|
decoder.mDecodeRequest.Complete();
|
|
|
|
NotifyError(aTrack, aError);
|
|
|
|
})
|
|
|
|
->Track(decoder.mDecodeRequest);
|
2015-10-20 12:33:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::HandleDemuxedSamples(TrackType aTrack,
|
2015-05-18 08:40:32 +03:00
|
|
|
AbstractMediaDecoder::AutoNotifyDecoded& aA)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2016-06-08 15:28:24 +03:00
|
|
|
|
2015-05-18 08:40:32 +03:00
|
|
|
auto& decoder = GetDecoderData(aTrack);
|
|
|
|
|
2017-01-26 15:56:46 +03:00
|
|
|
if (decoder.mFlushRequest.Exists() || decoder.mShutdownRequest.Exists()) {
|
|
|
|
LOGV("Decoder operation in progress, let it complete.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-18 08:40:32 +03:00
|
|
|
if (decoder.mQueuedSamples.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
2015-06-15 03:58:12 +03:00
|
|
|
|
2016-10-26 12:37:19 +03:00
|
|
|
if (!decoder.mDecoder) {
|
|
|
|
mDecoderFactory->CreateDecoder(aTrack);
|
2015-08-11 06:50:07 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-25 08:09:16 +03:00
|
|
|
LOGV("Giving %s input to decoder", TrackTypeToStr(aTrack));
|
2015-05-18 08:42:01 +03:00
|
|
|
|
2015-05-18 08:40:32 +03:00
|
|
|
// Decode all our demuxed frames.
|
2015-06-15 03:58:12 +03:00
|
|
|
while (decoder.mQueuedSamples.Length()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaRawData> sample = decoder.mQueuedSamples[0];
|
|
|
|
RefPtr<SharedTrackInfo> info = sample->mTrackInfo;
|
2015-06-15 03:58:12 +03:00
|
|
|
|
|
|
|
if (info && decoder.mLastStreamSourceID != info->GetID()) {
|
2016-11-29 10:03:14 +03:00
|
|
|
bool supportRecycling = MediaPrefs::MediaDecoderCheckRecycling() &&
|
|
|
|
decoder.mDecoder->SupportDecoderRecycling();
|
2015-07-13 09:25:04 +03:00
|
|
|
if (decoder.mNextStreamSourceID.isNothing() ||
|
|
|
|
decoder.mNextStreamSourceID.ref() != info->GetID()) {
|
2016-11-25 09:22:40 +03:00
|
|
|
if (!supportRecycling) {
|
|
|
|
LOG("%s stream id has changed from:%d to:%d, draining decoder.",
|
2015-07-13 09:25:04 +03:00
|
|
|
TrackTypeToStr(aTrack), decoder.mLastStreamSourceID,
|
|
|
|
info->GetID());
|
2016-11-25 09:22:40 +03:00
|
|
|
decoder.mNeedDraining = true;
|
|
|
|
decoder.mNextStreamSourceID = Some(info->GetID());
|
|
|
|
ScheduleUpdate(aTrack);
|
|
|
|
return;
|
|
|
|
}
|
2015-07-13 09:25:04 +03:00
|
|
|
}
|
|
|
|
|
2016-11-25 09:22:40 +03:00
|
|
|
LOG("%s stream id has changed from:%d to:%d.",
|
2015-06-15 03:58:12 +03:00
|
|
|
TrackTypeToStr(aTrack), decoder.mLastStreamSourceID,
|
|
|
|
info->GetID());
|
|
|
|
decoder.mLastStreamSourceID = info->GetID();
|
2015-12-08 22:30:25 +03:00
|
|
|
decoder.mNextStreamSourceID.reset();
|
2017-01-26 15:56:46 +03:00
|
|
|
decoder.mInfo = info;
|
|
|
|
|
2016-11-25 09:22:40 +03:00
|
|
|
if (!supportRecycling) {
|
|
|
|
LOG("Decoder does not support recycling, recreate decoder.");
|
2017-01-26 15:56:46 +03:00
|
|
|
// If flushing is required, it will clear our array of queued samples.
|
|
|
|
// So make a copy now.
|
|
|
|
nsTArray<RefPtr<MediaRawData>> samples{ Move(decoder.mQueuedSamples) };
|
|
|
|
ShutdownDecoder(aTrack);
|
2016-11-25 09:22:40 +03:00
|
|
|
if (sample->mKeyframe) {
|
|
|
|
decoder.mQueuedSamples.AppendElements(Move(samples));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-15 03:58:12 +03:00
|
|
|
if (sample->mKeyframe) {
|
2016-09-01 12:25:54 +03:00
|
|
|
ScheduleUpdate(aTrack);
|
2015-06-15 03:58:12 +03:00
|
|
|
} else {
|
2016-05-18 06:48:05 +03:00
|
|
|
TimeInterval time =
|
|
|
|
TimeInterval(TimeUnit::FromMicroseconds(sample->mTime),
|
|
|
|
TimeUnit::FromMicroseconds(sample->GetEndTime()));
|
2016-02-01 07:05:25 +03:00
|
|
|
InternalSeekTarget seekTarget =
|
2016-05-18 06:48:05 +03:00
|
|
|
decoder.mTimeThreshold.refOr(InternalSeekTarget(time, false));
|
2015-06-15 03:58:12 +03:00
|
|
|
LOG("Stream change occurred on a non-keyframe. Seeking to:%lld",
|
2016-05-18 06:48:05 +03:00
|
|
|
sample->mTime);
|
2015-12-08 22:30:25 +03:00
|
|
|
InternalSeek(aTrack, seekTarget);
|
2015-06-15 03:58:12 +03:00
|
|
|
}
|
2015-08-11 06:50:07 +03:00
|
|
|
return;
|
2015-06-15 03:58:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
LOGV("Input:%lld (dts:%lld kf:%d)",
|
|
|
|
sample->mTime, sample->mTimecode, sample->mKeyframe);
|
|
|
|
decoder.mNumSamplesInput++;
|
|
|
|
decoder.mSizeOfQueue++;
|
|
|
|
if (aTrack == TrackInfo::kVideoTrack) {
|
2016-07-18 03:41:40 +03:00
|
|
|
aA.mStats.mParsedFrames++;
|
2015-06-15 03:58:12 +03:00
|
|
|
}
|
2015-10-20 12:33:00 +03:00
|
|
|
|
2016-11-17 10:23:07 +03:00
|
|
|
DecodeDemuxedSamples(aTrack, sample);
|
2015-10-20 12:33:00 +03:00
|
|
|
|
2015-06-15 03:58:12 +03:00
|
|
|
decoder.mQueuedSamples.RemoveElementAt(0);
|
2017-01-26 15:56:46 +03:00
|
|
|
break;
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-08 22:30:25 +03:00
|
|
|
void
|
2016-02-01 07:05:25 +03:00
|
|
|
MediaFormatReader::InternalSeek(TrackType aTrack, const InternalSeekTarget& aTarget)
|
2015-12-08 22:30:25 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2016-05-05 07:52:58 +03:00
|
|
|
LOG("%s internal seek to %f",
|
2016-05-18 06:48:05 +03:00
|
|
|
TrackTypeToStr(aTrack), aTarget.Time().ToSeconds());
|
2016-05-05 07:52:58 +03:00
|
|
|
|
2015-12-08 22:30:25 +03:00
|
|
|
auto& decoder = GetDecoderData(aTrack);
|
2016-05-05 08:06:40 +03:00
|
|
|
decoder.Flush();
|
2015-12-08 22:30:25 +03:00
|
|
|
decoder.ResetDemuxer();
|
2016-05-05 07:52:58 +03:00
|
|
|
decoder.mTimeThreshold = Some(aTarget);
|
|
|
|
RefPtr<MediaFormatReader> self = this;
|
2017-01-11 11:33:29 +03:00
|
|
|
decoder.mTrackDemuxer->Seek(decoder.mTimeThreshold.ref().Time())
|
|
|
|
->Then(OwnerThread(), __func__,
|
|
|
|
[self, aTrack] (media::TimeUnit aTime) {
|
|
|
|
auto& decoder = self->GetDecoderData(aTrack);
|
|
|
|
decoder.mSeekRequest.Complete();
|
|
|
|
MOZ_ASSERT(decoder.mTimeThreshold,
|
|
|
|
"Seek promise must be disconnected when timethreshold is reset");
|
|
|
|
decoder.mTimeThreshold.ref().mHasSeeked = true;
|
|
|
|
self->SetVideoDecodeThreshold();
|
|
|
|
self->ScheduleUpdate(aTrack);
|
|
|
|
},
|
|
|
|
[self, aTrack] (const MediaResult& aError) {
|
|
|
|
auto& decoder = self->GetDecoderData(aTrack);
|
|
|
|
decoder.mSeekRequest.Complete();
|
|
|
|
switch (aError.Code()) {
|
|
|
|
case NS_ERROR_DOM_MEDIA_WAITING_FOR_DATA:
|
|
|
|
self->NotifyWaitingForData(aTrack);
|
|
|
|
break;
|
|
|
|
case NS_ERROR_DOM_MEDIA_END_OF_STREAM:
|
|
|
|
decoder.mTimeThreshold.reset();
|
|
|
|
self->NotifyEndOfStream(aTrack);
|
|
|
|
break;
|
|
|
|
case NS_ERROR_DOM_MEDIA_CANCELED:
|
|
|
|
decoder.mTimeThreshold.reset();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
decoder.mTimeThreshold.reset();
|
|
|
|
self->NotifyError(aTrack, aError);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
->Track(decoder.mSeekRequest);
|
2015-12-08 22:30:25 +03:00
|
|
|
}
|
|
|
|
|
2015-07-13 09:21:04 +03:00
|
|
|
void
|
|
|
|
MediaFormatReader::DrainDecoder(TrackType aTrack)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
|
|
|
|
auto& decoder = GetDecoderData(aTrack);
|
2015-07-15 07:43:35 +03:00
|
|
|
if (!decoder.mNeedDraining || decoder.mDraining) {
|
2015-07-13 09:21:04 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-07-16 08:47:51 +03:00
|
|
|
decoder.mNeedDraining = false;
|
2015-07-28 07:09:43 +03:00
|
|
|
if (!decoder.mDecoder ||
|
|
|
|
decoder.mNumSamplesInput == decoder.mNumSamplesOutput) {
|
2015-07-26 14:52:22 +03:00
|
|
|
// No frames to drain.
|
2017-01-26 15:56:46 +03:00
|
|
|
LOGV("Draining %s with nothing to drain", TrackTypeToStr(aTrack));
|
2015-07-26 14:52:22 +03:00
|
|
|
NotifyDrainComplete(aTrack);
|
|
|
|
return;
|
|
|
|
}
|
2015-07-15 07:43:35 +03:00
|
|
|
decoder.mDraining = true;
|
2017-01-26 15:56:46 +03:00
|
|
|
RefPtr<MediaFormatReader> self = this;
|
|
|
|
decoder.mDecoder->Drain()
|
|
|
|
->Then(mTaskQueue, __func__,
|
|
|
|
[self, this, aTrack, &decoder]
|
|
|
|
(const MediaDataDecoder::DecodedData& aResults) {
|
|
|
|
decoder.mDrainRequest.Complete();
|
|
|
|
NotifyNewOutput(aTrack, aResults);
|
|
|
|
NotifyDrainComplete(aTrack);
|
|
|
|
},
|
|
|
|
[self, this, aTrack, &decoder](const MediaResult& aError) {
|
|
|
|
decoder.mDrainRequest.Complete();
|
|
|
|
NotifyError(aTrack, aError);
|
|
|
|
})
|
|
|
|
->Track(decoder.mDrainRequest);
|
2015-07-13 09:21:04 +03:00
|
|
|
LOG("Requesting %s decoder to drain", TrackTypeToStr(aTrack));
|
|
|
|
}
|
|
|
|
|
2015-05-18 08:40:32 +03:00
|
|
|
void
|
|
|
|
MediaFormatReader::Update(TrackType aTrack)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
|
2015-10-19 21:11:35 +03:00
|
|
|
if (mShutdown) {
|
2015-05-18 08:40:32 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-25 08:09:16 +03:00
|
|
|
LOGV("Processing update for %s", TrackTypeToStr(aTrack));
|
2015-05-18 08:40:32 +03:00
|
|
|
|
|
|
|
bool needOutput = false;
|
|
|
|
auto& decoder = GetDecoderData(aTrack);
|
2015-05-18 08:42:01 +03:00
|
|
|
decoder.mUpdateScheduled = false;
|
|
|
|
|
2015-10-19 21:11:35 +03:00
|
|
|
if (!mInitDone) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-26 06:59:46 +03:00
|
|
|
if (aTrack == TrackType::kVideoTrack && mSkipRequest.Exists()) {
|
|
|
|
LOGV("Skipping in progress, nothing more to do");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-18 08:42:01 +03:00
|
|
|
if (UpdateReceivedNewData(aTrack)) {
|
2015-05-25 08:09:16 +03:00
|
|
|
LOGV("Nothing more to do");
|
2015-05-18 08:42:01 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-05-18 08:40:32 +03:00
|
|
|
|
2016-05-05 07:52:58 +03:00
|
|
|
if (decoder.mSeekRequest.Exists()) {
|
|
|
|
LOGV("Seeking hasn't completed, nothing more to do");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!decoder.HasInternalSeekPending() ||
|
|
|
|
(!decoder.mOutput.Length() &&
|
|
|
|
!decoder.mQueuedSamples.Length()),
|
|
|
|
"No frames can be demuxed or decoded while an internal seek is pending");
|
|
|
|
|
2015-05-25 08:09:16 +03:00
|
|
|
// Record number of frames decoded and parsed. Automatically update the
|
|
|
|
// stats counters using the AutoNotifyDecoded stack-based class.
|
|
|
|
AbstractMediaDecoder::AutoNotifyDecoded a(mDecoder);
|
|
|
|
|
2015-12-18 01:37:03 +03:00
|
|
|
// Drop any frames found prior our internal seek target.
|
|
|
|
while (decoder.mTimeThreshold && decoder.mOutput.Length()) {
|
|
|
|
RefPtr<MediaData>& output = decoder.mOutput[0];
|
2016-02-01 07:05:25 +03:00
|
|
|
InternalSeekTarget target = decoder.mTimeThreshold.ref();
|
2015-12-18 01:37:03 +03:00
|
|
|
media::TimeUnit time = media::TimeUnit::FromMicroseconds(output->mTime);
|
2016-05-18 06:48:05 +03:00
|
|
|
if (time >= target.Time()) {
|
2015-12-18 01:37:03 +03:00
|
|
|
// We have reached our internal seek target.
|
|
|
|
decoder.mTimeThreshold.reset();
|
2016-07-26 02:36:55 +03:00
|
|
|
// We might have dropped some keyframes.
|
|
|
|
mPreviousDecodedKeyframeTime_us = sNoPreviousDecodedKeyframe;
|
2015-12-18 01:37:03 +03:00
|
|
|
}
|
2016-05-18 06:48:05 +03:00
|
|
|
if (time < target.Time() || (target.mDropTarget && target.Contains(time))) {
|
2015-12-18 01:37:03 +03:00
|
|
|
LOGV("Internal Seeking: Dropping %s frame time:%f wanted:%f (kf:%d)",
|
|
|
|
TrackTypeToStr(aTrack),
|
|
|
|
media::TimeUnit::FromMicroseconds(output->mTime).ToSeconds(),
|
2016-05-18 06:48:05 +03:00
|
|
|
target.Time().ToSeconds(),
|
2015-12-18 01:37:03 +03:00
|
|
|
output->mKeyframe);
|
|
|
|
decoder.mOutput.RemoveElementAt(0);
|
2016-05-19 12:03:39 +03:00
|
|
|
decoder.mSizeOfQueue -= 1;
|
2015-12-18 01:37:03 +03:00
|
|
|
}
|
2015-05-18 08:42:01 +03:00
|
|
|
}
|
2015-07-13 09:21:04 +03:00
|
|
|
|
2016-06-08 05:59:57 +03:00
|
|
|
while (decoder.mOutput.Length() && decoder.mOutput[0]->mType == MediaData::NULL_DATA) {
|
|
|
|
LOGV("Dropping null data. Time: %lld", decoder.mOutput[0]->mTime);
|
|
|
|
decoder.mOutput.RemoveElementAt(0);
|
|
|
|
decoder.mSizeOfQueue -= 1;
|
|
|
|
}
|
|
|
|
|
2015-05-18 08:42:01 +03:00
|
|
|
if (decoder.HasPromise()) {
|
|
|
|
needOutput = true;
|
2015-12-18 01:37:03 +03:00
|
|
|
if (decoder.mOutput.Length()) {
|
2016-05-19 12:03:39 +03:00
|
|
|
RefPtr<MediaData> output = decoder.mOutput[0];
|
|
|
|
decoder.mOutput.RemoveElementAt(0);
|
|
|
|
decoder.mSizeOfQueue -= 1;
|
|
|
|
decoder.mLastSampleTime =
|
|
|
|
Some(TimeInterval(TimeUnit::FromMicroseconds(output->mTime),
|
|
|
|
TimeUnit::FromMicroseconds(output->GetEndTime())));
|
|
|
|
decoder.mNumSamplesOutputTotal++;
|
|
|
|
ReturnOutput(output, aTrack);
|
2015-05-18 08:42:01 +03:00
|
|
|
// We have a decoded sample ready to be returned.
|
2015-07-20 22:14:37 +03:00
|
|
|
if (aTrack == TrackType::kVideoTrack) {
|
2015-12-18 01:37:03 +03:00
|
|
|
uint64_t delta =
|
|
|
|
decoder.mNumSamplesOutputTotal - mLastReportedNumDecodedFrames;
|
2016-07-18 03:41:40 +03:00
|
|
|
a.mStats.mDecodedFrames = static_cast<uint32_t>(delta);
|
2015-12-18 01:37:03 +03:00
|
|
|
mLastReportedNumDecodedFrames = decoder.mNumSamplesOutputTotal;
|
2016-07-26 02:36:55 +03:00
|
|
|
if (output->mKeyframe) {
|
|
|
|
if (mPreviousDecodedKeyframeTime_us < output->mTime) {
|
|
|
|
// There is a previous keyframe -> Record inter-keyframe stats.
|
|
|
|
uint64_t segment_us = output->mTime - mPreviousDecodedKeyframeTime_us;
|
|
|
|
a.mStats.mInterKeyframeSum_us += segment_us;
|
|
|
|
a.mStats.mInterKeyframeCount += 1;
|
|
|
|
if (a.mStats.mInterKeyFrameMax_us < segment_us) {
|
|
|
|
a.mStats.mInterKeyFrameMax_us = segment_us;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mPreviousDecodedKeyframeTime_us = output->mTime;
|
|
|
|
}
|
2015-07-13 23:12:23 +03:00
|
|
|
nsCString error;
|
2015-07-20 22:14:37 +03:00
|
|
|
mVideo.mIsHardwareAccelerated =
|
2015-07-13 23:12:23 +03:00
|
|
|
mVideo.mDecoder && mVideo.mDecoder->IsHardwareAccelerated(error);
|
2015-07-20 22:14:37 +03:00
|
|
|
}
|
2016-05-30 19:24:00 +03:00
|
|
|
} else if (decoder.HasFatalError()) {
|
2015-12-18 01:37:03 +03:00
|
|
|
LOG("Rejecting %s promise: DECODE_ERROR", TrackTypeToStr(aTrack));
|
2016-09-10 09:48:53 +03:00
|
|
|
decoder.RejectPromise(decoder.mError.ref(), __func__);
|
2015-12-08 22:30:25 +03:00
|
|
|
return;
|
2015-12-18 01:37:03 +03:00
|
|
|
} else if (decoder.mDrainComplete) {
|
2015-12-08 22:30:25 +03:00
|
|
|
bool wasDraining = decoder.mDraining;
|
2015-05-18 08:42:01 +03:00
|
|
|
decoder.mDrainComplete = false;
|
2015-07-15 07:43:35 +03:00
|
|
|
decoder.mDraining = false;
|
2015-12-08 22:30:25 +03:00
|
|
|
if (decoder.mDemuxEOS) {
|
2015-12-18 01:37:03 +03:00
|
|
|
LOG("Rejecting %s promise: EOS", TrackTypeToStr(aTrack));
|
2016-09-10 09:48:53 +03:00
|
|
|
decoder.RejectPromise(NS_ERROR_DOM_MEDIA_END_OF_STREAM, __func__);
|
2015-12-08 22:30:25 +03:00
|
|
|
} else if (decoder.mWaitingForData) {
|
|
|
|
if (wasDraining && decoder.mLastSampleTime &&
|
|
|
|
!decoder.mNextStreamSourceID) {
|
|
|
|
// We have completed draining the decoder following WaitingForData.
|
|
|
|
// Set up the internal seek machinery to be able to resume from the
|
|
|
|
// last sample decoded.
|
|
|
|
LOG("Seeking to last sample time: %lld",
|
2016-05-18 06:48:05 +03:00
|
|
|
decoder.mLastSampleTime.ref().mStart.ToMicroseconds());
|
2016-02-01 07:05:25 +03:00
|
|
|
InternalSeek(aTrack, InternalSeekTarget(decoder.mLastSampleTime.ref(), true));
|
2015-12-08 22:30:25 +03:00
|
|
|
}
|
2016-02-08 16:36:47 +03:00
|
|
|
if (!decoder.mReceivedNewData) {
|
|
|
|
LOG("Rejecting %s promise: WAITING_FOR_DATA", TrackTypeToStr(aTrack));
|
2016-09-10 09:48:53 +03:00
|
|
|
decoder.RejectPromise(NS_ERROR_DOM_MEDIA_WAITING_FOR_DATA, __func__);
|
2016-02-08 16:36:47 +03:00
|
|
|
}
|
2015-12-08 22:30:25 +03:00
|
|
|
}
|
|
|
|
// Now that draining has completed, we check if we have received
|
|
|
|
// new data again as the result may now be different from the earlier
|
|
|
|
// run.
|
2016-05-05 07:52:58 +03:00
|
|
|
if (UpdateReceivedNewData(aTrack) || decoder.mSeekRequest.Exists()) {
|
2015-12-08 22:30:25 +03:00
|
|
|
LOGV("Nothing more to do");
|
|
|
|
return;
|
2015-07-13 09:21:04 +03:00
|
|
|
}
|
2016-05-24 12:58:36 +03:00
|
|
|
} else if (decoder.mDemuxEOS && !decoder.mNeedDraining &&
|
2016-06-02 14:08:05 +03:00
|
|
|
!decoder.HasPendingDrain() && decoder.mQueuedSamples.IsEmpty()) {
|
2016-05-24 12:58:36 +03:00
|
|
|
// It is possible to transition from WAITING_FOR_DATA directly to EOS
|
|
|
|
// state during the internal seek; in which case no draining would occur.
|
|
|
|
// There is no more samples left to be decoded and we are already in
|
|
|
|
// EOS state. We can immediately reject the data promise.
|
|
|
|
LOG("Rejecting %s promise: EOS", TrackTypeToStr(aTrack));
|
2016-09-10 09:48:53 +03:00
|
|
|
decoder.RejectPromise(NS_ERROR_DOM_MEDIA_END_OF_STREAM, __func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-12 14:24:48 +03:00
|
|
|
if (decoder.mNeedDraining) {
|
2015-07-13 09:21:04 +03:00
|
|
|
DrainDecoder(aTrack);
|
2015-05-25 08:09:16 +03:00
|
|
|
return;
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
2016-09-10 03:49:54 +03:00
|
|
|
if (decoder.mError && !decoder.HasFatalError()) {
|
2016-11-08 05:22:37 +03:00
|
|
|
bool needsNewDecoder = decoder.mError.ref() == NS_ERROR_DOM_MEDIA_NEED_NEW_DECODER;
|
|
|
|
if (!needsNewDecoder && ++decoder.mNumOfConsecutiveError > decoder.mMaxConsecutiveError) {
|
2016-09-13 06:03:21 +03:00
|
|
|
NotifyError(aTrack, decoder.mError.ref());
|
2016-05-30 19:24:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-09-13 06:03:21 +03:00
|
|
|
decoder.mError.reset();
|
2016-05-30 19:24:00 +03:00
|
|
|
LOG("%s decoded error count %d", TrackTypeToStr(aTrack),
|
|
|
|
decoder.mNumOfConsecutiveError);
|
|
|
|
media::TimeUnit nextKeyframe;
|
|
|
|
if (aTrack == TrackType::kVideoTrack && !decoder.HasInternalSeekPending() &&
|
|
|
|
NS_SUCCEEDED(decoder.mTrackDemuxer->GetNextRandomAccessPoint(&nextKeyframe))) {
|
2016-11-08 05:22:37 +03:00
|
|
|
if (needsNewDecoder) {
|
2017-01-26 15:56:46 +03:00
|
|
|
ShutdownDecoder(aTrack);
|
2016-11-08 05:22:37 +03:00
|
|
|
}
|
2016-05-30 19:24:00 +03:00
|
|
|
SkipVideoDemuxToNextKeyFrame(decoder.mLastSampleTime.refOr(TimeInterval()).Length());
|
2016-10-26 00:48:47 +03:00
|
|
|
} else if (aTrack == TrackType::kAudioTrack) {
|
|
|
|
decoder.Flush();
|
2016-05-30 19:24:00 +03:00
|
|
|
}
|
2017-01-26 15:56:46 +03:00
|
|
|
return;
|
2016-05-30 19:24:00 +03:00
|
|
|
}
|
|
|
|
|
2015-12-08 22:30:25 +03:00
|
|
|
bool needInput = NeedInput(decoder);
|
2015-05-18 08:40:32 +03:00
|
|
|
|
2017-01-26 15:56:46 +03:00
|
|
|
LOGV("Update(%s) ni=%d no=%d in:%llu out:%llu qs=%u decoding:%d flushing:%d "
|
|
|
|
"shutdown:%d pending:%u waiting:%d promise:%d sid:%u",
|
|
|
|
TrackTypeToStr(aTrack), needInput, needOutput, decoder.mNumSamplesInput,
|
|
|
|
decoder.mNumSamplesOutput, uint32_t(size_t(decoder.mSizeOfQueue)),
|
|
|
|
decoder.mDecodeRequest.Exists(), decoder.mFlushRequest.Exists(),
|
|
|
|
decoder.mShutdownRequest.Exists(), uint32_t(decoder.mOutput.Length()),
|
2016-10-27 12:25:21 +03:00
|
|
|
decoder.mWaitingForData, decoder.HasPromise(),
|
2016-12-20 10:53:39 +03:00
|
|
|
decoder.mLastStreamSourceID);
|
2015-12-08 22:30:25 +03:00
|
|
|
|
2016-10-27 12:25:21 +03:00
|
|
|
if ((decoder.mWaitingForData &&
|
2016-12-20 10:53:39 +03:00
|
|
|
(!decoder.mTimeThreshold || decoder.mTimeThreshold.ref().mWaiting))) {
|
2015-12-08 22:30:25 +03:00
|
|
|
// Nothing more we can do at present.
|
2016-10-27 12:25:21 +03:00
|
|
|
LOGV("Still waiting for data or key.");
|
2015-12-08 22:30:25 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!needInput) {
|
|
|
|
LOGV("No need for additional input (pending:%u)",
|
|
|
|
uint32_t(decoder.mOutput.Length()));
|
|
|
|
return;
|
|
|
|
}
|
2015-05-25 08:09:16 +03:00
|
|
|
|
2015-05-18 08:40:32 +03:00
|
|
|
// Demux samples if we don't have some.
|
|
|
|
RequestDemuxSamples(aTrack);
|
2015-10-20 12:33:00 +03:00
|
|
|
|
|
|
|
HandleDemuxedSamples(aTrack, a);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::ReturnOutput(MediaData* aData, TrackType aTrack)
|
|
|
|
{
|
2016-08-25 13:18:22 +03:00
|
|
|
MOZ_ASSERT(GetDecoderData(aTrack).HasPromise());
|
2016-06-08 05:59:57 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aData->mType != MediaData::NULL_DATA);
|
2016-05-06 08:56:47 +03:00
|
|
|
LOG("Resolved data promise for %s [%lld, %lld]", TrackTypeToStr(aTrack),
|
|
|
|
aData->mTime, aData->GetEndTime());
|
|
|
|
|
2015-05-18 08:40:32 +03:00
|
|
|
if (aTrack == TrackInfo::kAudioTrack) {
|
2016-11-17 10:23:07 +03:00
|
|
|
AudioData* audioData = static_cast<AudioData*>(aData);
|
|
|
|
|
|
|
|
if (audioData->mChannels != mInfo.mAudio.mChannels ||
|
|
|
|
audioData->mRate != mInfo.mAudio.mRate) {
|
|
|
|
LOG("change of audio format (rate:%d->%d). "
|
|
|
|
"This is an unsupported configuration",
|
|
|
|
mInfo.mAudio.mRate, audioData->mRate);
|
|
|
|
mInfo.mAudio.mRate = audioData->mRate;
|
|
|
|
mInfo.mAudio.mChannels = audioData->mChannels;
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
2016-05-05 08:11:21 +03:00
|
|
|
mAudio.ResolvePromise(aData, __func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
} else if (aTrack == TrackInfo::kVideoTrack) {
|
2016-11-17 10:23:07 +03:00
|
|
|
VideoData* videoData = static_cast<VideoData*>(aData);
|
|
|
|
|
|
|
|
if (videoData->mDisplay != mInfo.mVideo.mDisplay) {
|
|
|
|
LOG("change of video display size (%dx%d->%dx%d)",
|
|
|
|
mInfo.mVideo.mDisplay.width, mInfo.mVideo.mDisplay.height,
|
|
|
|
videoData->mDisplay.width, videoData->mDisplay.height);
|
|
|
|
mInfo.mVideo.mDisplay = videoData->mDisplay;
|
2016-01-12 13:03:13 +03:00
|
|
|
}
|
2016-05-05 08:11:21 +03:00
|
|
|
mVideo.ResolvePromise(aData, __func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
MediaFormatReader::SizeOfVideoQueueInFrames()
|
|
|
|
{
|
|
|
|
return SizeOfQueue(TrackInfo::kVideoTrack);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
MediaFormatReader::SizeOfAudioQueueInFrames()
|
|
|
|
{
|
|
|
|
return SizeOfQueue(TrackInfo::kAudioTrack);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
MediaFormatReader::SizeOfQueue(TrackType aTrack)
|
|
|
|
{
|
|
|
|
auto& decoder = GetDecoderData(aTrack);
|
2015-05-18 08:42:01 +03:00
|
|
|
return decoder.mSizeOfQueue;
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaDecoderReader::WaitForDataPromise>
|
2015-05-18 08:40:32 +03:00
|
|
|
MediaFormatReader::WaitForData(MediaData::Type aType)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
TrackType trackType = aType == MediaData::VIDEO_DATA ?
|
|
|
|
TrackType::kVideoTrack : TrackType::kAudioTrack;
|
|
|
|
auto& decoder = GetDecoderData(trackType);
|
2016-10-27 12:25:21 +03:00
|
|
|
if (!decoder.IsWaiting()) {
|
|
|
|
// We aren't waiting for anything.
|
2015-07-20 09:05:16 +03:00
|
|
|
return WaitForDataPromise::CreateAndResolve(decoder.mType, __func__);
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WaitForDataPromise> p = decoder.mWaitingPromise.Ensure(__func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
ScheduleUpdate(trackType);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2016-05-31 07:32:37 +03:00
|
|
|
MediaFormatReader::ResetDecode(TrackSet aTracks)
|
2015-05-18 08:40:32 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2015-05-25 08:09:16 +03:00
|
|
|
LOGV("");
|
2015-05-18 08:40:32 +03:00
|
|
|
|
|
|
|
mSeekPromise.RejectIfExists(NS_OK, __func__);
|
|
|
|
mSkipRequest.DisconnectIfExists();
|
|
|
|
|
|
|
|
// Do the same for any data wait promises.
|
2016-05-31 07:32:37 +03:00
|
|
|
if (aTracks.contains(TrackInfo::kAudioTrack)) {
|
|
|
|
mAudio.mWaitingPromise.RejectIfExists(
|
|
|
|
WaitForDataRejectValue(MediaData::AUDIO_DATA,
|
|
|
|
WaitForDataRejectValue::CANCELED), __func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aTracks.contains(TrackInfo::kVideoTrack)) {
|
|
|
|
mVideo.mWaitingPromise.RejectIfExists(
|
|
|
|
WaitForDataRejectValue(MediaData::VIDEO_DATA,
|
|
|
|
WaitForDataRejectValue::CANCELED), __func__);
|
2016-05-16 09:50:04 +03:00
|
|
|
}
|
2015-05-18 08:40:32 +03:00
|
|
|
|
|
|
|
// Reset miscellaneous seeking state.
|
|
|
|
mPendingSeekTime.reset();
|
|
|
|
|
2016-05-31 07:32:37 +03:00
|
|
|
if (HasVideo() && aTracks.contains(TrackInfo::kVideoTrack)) {
|
2016-05-13 04:14:12 +03:00
|
|
|
mVideo.ResetDemuxer();
|
2016-05-05 08:06:40 +03:00
|
|
|
Reset(TrackInfo::kVideoTrack);
|
2015-06-15 03:58:12 +03:00
|
|
|
if (mVideo.HasPromise()) {
|
2016-09-10 09:48:53 +03:00
|
|
|
mVideo.RejectPromise(NS_ERROR_DOM_MEDIA_CANCELED, __func__);
|
2015-06-15 03:58:12 +03:00
|
|
|
}
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
2016-04-18 09:33:52 +03:00
|
|
|
|
2016-05-31 07:32:37 +03:00
|
|
|
if (HasAudio() && aTracks.contains(TrackInfo::kAudioTrack)) {
|
2016-05-13 04:14:12 +03:00
|
|
|
mAudio.ResetDemuxer();
|
2016-05-05 08:06:40 +03:00
|
|
|
Reset(TrackInfo::kAudioTrack);
|
2015-06-15 03:58:12 +03:00
|
|
|
if (mAudio.HasPromise()) {
|
2016-09-10 09:48:53 +03:00
|
|
|
mAudio.RejectPromise(NS_ERROR_DOM_MEDIA_CANCELED, __func__);
|
2015-06-15 03:58:12 +03:00
|
|
|
}
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
2016-05-31 07:32:37 +03:00
|
|
|
|
|
|
|
return MediaDecoderReader::ResetDecode(aTracks);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-05-05 08:06:40 +03:00
|
|
|
MediaFormatReader::Reset(TrackType aTrack)
|
2015-05-18 08:40:32 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2016-05-05 08:06:40 +03:00
|
|
|
LOG("Reset(%s) BEGIN", TrackTypeToStr(aTrack));
|
2015-05-18 08:40:32 +03:00
|
|
|
|
|
|
|
auto& decoder = GetDecoderData(aTrack);
|
|
|
|
|
|
|
|
decoder.ResetState();
|
2016-05-05 08:06:40 +03:00
|
|
|
decoder.Flush();
|
|
|
|
|
|
|
|
LOG("Reset(%s) END", TrackTypeToStr(aTrack));
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
2016-05-26 07:58:48 +03:00
|
|
|
void
|
|
|
|
MediaFormatReader::DropDecodedSamples(TrackType aTrack)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
auto& decoder = GetDecoderData(aTrack);
|
|
|
|
size_t lengthDecodedQueue = decoder.mOutput.Length();
|
|
|
|
if (lengthDecodedQueue && decoder.mTimeThreshold.isSome()) {
|
|
|
|
TimeUnit time =
|
|
|
|
TimeUnit::FromMicroseconds(decoder.mOutput.LastElement()->mTime);
|
|
|
|
if (time >= decoder.mTimeThreshold.ref().Time()) {
|
|
|
|
// We would have reached our internal seek target.
|
|
|
|
decoder.mTimeThreshold.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
decoder.mOutput.Clear();
|
|
|
|
decoder.mSizeOfQueue -= lengthDecodedQueue;
|
|
|
|
if (aTrack == TrackInfo::kVideoTrack && mDecoder) {
|
2016-07-18 03:41:40 +03:00
|
|
|
mDecoder->NotifyDecodedFrames({ 0, 0, lengthDecodedQueue });
|
2016-05-26 07:58:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-18 08:40:32 +03:00
|
|
|
void
|
|
|
|
MediaFormatReader::SkipVideoDemuxToNextKeyFrame(media::TimeUnit aTimeThreshold)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
LOG("Skipping up to %lld", aTimeThreshold.ToMicroseconds());
|
|
|
|
|
2016-05-26 07:58:48 +03:00
|
|
|
// We've reached SkipVideoDemuxToNextKeyFrame when our decoding is late.
|
|
|
|
// As such we can drop all already decoded samples and discard all pending
|
|
|
|
// samples.
|
|
|
|
DropDecodedSamples(TrackInfo::kVideoTrack);
|
|
|
|
|
2017-01-11 11:33:29 +03:00
|
|
|
mVideo.mTrackDemuxer->SkipToNextRandomAccessPoint(aTimeThreshold)
|
|
|
|
->Then(OwnerThread(), __func__, this,
|
|
|
|
&MediaFormatReader::OnVideoSkipCompleted,
|
|
|
|
&MediaFormatReader::OnVideoSkipFailed)
|
|
|
|
->Track(mSkipRequest);
|
2016-05-19 10:02:43 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-05-06 08:58:51 +03:00
|
|
|
|
2016-05-19 10:02:43 +03:00
|
|
|
void
|
|
|
|
MediaFormatReader::VideoSkipReset(uint32_t aSkipped)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2016-05-26 07:58:48 +03:00
|
|
|
|
|
|
|
// Some frames may have been output by the decoder since we initiated the
|
|
|
|
// videoskip process and we know they would be late.
|
|
|
|
DropDecodedSamples(TrackInfo::kVideoTrack);
|
|
|
|
// Report the pending frames as dropped.
|
2016-05-19 10:02:43 +03:00
|
|
|
if (mDecoder) {
|
2016-07-18 03:41:40 +03:00
|
|
|
mDecoder->NotifyDecodedFrames({ 0, 0, SizeOfVideoQueueInFrames() });
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
2016-05-19 10:02:43 +03:00
|
|
|
// Cancel any pending demux request and pending demuxed samples.
|
|
|
|
mVideo.mDemuxRequest.DisconnectIfExists();
|
|
|
|
Reset(TrackType::kVideoTrack);
|
|
|
|
|
|
|
|
if (mDecoder) {
|
2016-07-18 03:41:40 +03:00
|
|
|
mDecoder->NotifyDecodedFrames({ aSkipped, 0, aSkipped });
|
2016-05-07 04:23:32 +03:00
|
|
|
}
|
|
|
|
|
2016-05-19 10:02:43 +03:00
|
|
|
mVideo.mNumSamplesSkippedTotal += aSkipped;
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::OnVideoSkipCompleted(uint32_t aSkipped)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
LOG("Skipping succeeded, skipped %u frames", aSkipped);
|
|
|
|
mSkipRequest.Complete();
|
2016-05-19 10:02:43 +03:00
|
|
|
|
|
|
|
VideoSkipReset(aSkipped);
|
|
|
|
|
2016-09-01 12:25:54 +03:00
|
|
|
ScheduleUpdate(TrackInfo::kVideoTrack);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::OnVideoSkipFailed(MediaTrackDemuxer::SkipFailureHolder aFailure)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
LOG("Skipping failed, skipped %u frames", aFailure.mSkipped);
|
|
|
|
mSkipRequest.Complete();
|
2016-05-19 10:02:43 +03:00
|
|
|
|
2016-09-12 05:22:20 +03:00
|
|
|
switch (aFailure.mFailure.Code()) {
|
|
|
|
case NS_ERROR_DOM_MEDIA_END_OF_STREAM:
|
|
|
|
case NS_ERROR_DOM_MEDIA_WAITING_FOR_DATA:
|
2016-05-26 07:58:48 +03:00
|
|
|
// Some frames may have been output by the decoder since we initiated the
|
|
|
|
// videoskip process and we know they would be late.
|
|
|
|
DropDecodedSamples(TrackInfo::kVideoTrack);
|
2016-05-19 10:02:43 +03:00
|
|
|
// We can't complete the skip operation, will just service a video frame
|
|
|
|
// normally.
|
2016-09-01 12:25:54 +03:00
|
|
|
ScheduleUpdate(TrackInfo::kVideoTrack);
|
2015-06-15 03:58:12 +03:00
|
|
|
break;
|
2016-09-12 05:22:20 +03:00
|
|
|
case NS_ERROR_DOM_MEDIA_CANCELED:
|
2015-08-25 14:35:15 +03:00
|
|
|
if (mVideo.HasPromise()) {
|
2016-09-12 05:22:20 +03:00
|
|
|
mVideo.RejectPromise(aFailure.mFailure, __func__);
|
2015-08-25 14:35:15 +03:00
|
|
|
}
|
2015-06-15 03:58:12 +03:00
|
|
|
break;
|
|
|
|
default:
|
2016-09-12 05:22:20 +03:00
|
|
|
NotifyError(TrackType::kVideoTrack, aFailure.mFailure);
|
2015-06-15 03:58:12 +03:00
|
|
|
break;
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaDecoderReader::SeekPromise>
|
2016-11-29 17:42:50 +03:00
|
|
|
MediaFormatReader::Seek(const SeekTarget& aTarget)
|
2015-05-18 08:40:32 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
|
2016-01-28 13:24:30 +03:00
|
|
|
LOG("aTarget=(%lld)", aTarget.GetTime().ToMicroseconds());
|
2015-05-18 08:40:32 +03:00
|
|
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mSeekPromise.IsEmpty());
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mVideo.HasPromise());
|
2016-05-13 09:35:43 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aTarget.IsVideoOnly() || !mAudio.HasPromise());
|
2015-05-18 08:40:32 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mPendingSeekTime.isNothing());
|
2015-06-15 03:58:12 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mVideo.mTimeThreshold.isNothing());
|
2016-05-13 09:35:43 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aTarget.IsVideoOnly() || mAudio.mTimeThreshold.isNothing());
|
2015-05-18 08:40:32 +03:00
|
|
|
|
2016-02-04 07:31:21 +03:00
|
|
|
if (!mInfo.mMediaSeekable && !mInfo.mMediaSeekableOnlyInBufferedRanges) {
|
2015-05-18 08:40:32 +03:00
|
|
|
LOG("Seek() END (Unseekable)");
|
|
|
|
return SeekPromise::CreateAndReject(NS_ERROR_FAILURE, __func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mShutdown) {
|
|
|
|
return SeekPromise::CreateAndReject(NS_ERROR_FAILURE, __func__);
|
|
|
|
}
|
|
|
|
|
2016-11-13 09:42:19 +03:00
|
|
|
SetSeekTarget(aTarget);
|
2015-05-18 08:40:32 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SeekPromise> p = mSeekPromise.Ensure(__func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
|
2016-05-05 08:01:51 +03:00
|
|
|
ScheduleSeek();
|
2015-05-18 08:40:32 +03:00
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2016-05-16 13:15:17 +03:00
|
|
|
void
|
|
|
|
MediaFormatReader::SetSeekTarget(const SeekTarget& aTarget)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
|
2016-10-16 17:15:29 +03:00
|
|
|
mOriginalSeekTarget = aTarget;
|
|
|
|
mFallbackSeekTime = mPendingSeekTime = Some(aTarget.GetTime());
|
2016-10-29 04:47:49 +03:00
|
|
|
}
|
|
|
|
|
2016-05-05 08:01:51 +03:00
|
|
|
void
|
|
|
|
MediaFormatReader::ScheduleSeek()
|
|
|
|
{
|
|
|
|
if (mSeekScheduled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mSeekScheduled = true;
|
|
|
|
OwnerThread()->Dispatch(NewRunnableMethod(this, &MediaFormatReader::AttemptSeek));
|
|
|
|
}
|
|
|
|
|
2015-05-18 08:40:32 +03:00
|
|
|
void
|
|
|
|
MediaFormatReader::AttemptSeek()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2016-05-05 08:01:51 +03:00
|
|
|
|
|
|
|
mSeekScheduled = false;
|
|
|
|
|
2015-09-28 08:23:28 +03:00
|
|
|
if (mPendingSeekTime.isNothing()) {
|
|
|
|
return;
|
|
|
|
}
|
2016-05-13 04:14:12 +03:00
|
|
|
|
|
|
|
if (HasVideo()) {
|
|
|
|
mVideo.ResetDemuxer();
|
|
|
|
mVideo.ResetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't reset the audio demuxer not state when seeking video only
|
|
|
|
// as it will cause the audio to seek back to the beginning
|
|
|
|
// resulting in out-of-sync audio from video.
|
2016-05-16 09:47:57 +03:00
|
|
|
if (HasAudio() && !mOriginalSeekTarget.IsVideoOnly()) {
|
2016-05-13 04:14:12 +03:00
|
|
|
mAudio.ResetDemuxer();
|
|
|
|
mAudio.ResetState();
|
|
|
|
}
|
|
|
|
|
2015-05-18 08:40:32 +03:00
|
|
|
if (HasVideo()) {
|
|
|
|
DoVideoSeek();
|
|
|
|
} else if (HasAudio()) {
|
|
|
|
DoAudioSeek();
|
|
|
|
} else {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-09-12 05:22:20 +03:00
|
|
|
MediaFormatReader::OnSeekFailed(TrackType aTrack, const MediaResult& aError)
|
2015-05-18 08:40:32 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2016-09-12 05:22:20 +03:00
|
|
|
LOGV("%s failure:%u", TrackTypeToStr(aTrack), aError.Code());
|
2015-05-18 08:40:32 +03:00
|
|
|
if (aTrack == TrackType::kVideoTrack) {
|
2015-06-15 03:58:12 +03:00
|
|
|
mVideo.mSeekRequest.Complete();
|
2015-05-18 08:40:32 +03:00
|
|
|
} else {
|
2015-06-15 03:58:12 +03:00
|
|
|
mAudio.mSeekRequest.Complete();
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
2016-09-12 05:22:20 +03:00
|
|
|
if (aError == NS_ERROR_DOM_MEDIA_WAITING_FOR_DATA) {
|
2015-08-11 07:15:19 +03:00
|
|
|
if (HasVideo() && aTrack == TrackType::kAudioTrack &&
|
2016-05-16 09:47:57 +03:00
|
|
|
mFallbackSeekTime.isSome() &&
|
|
|
|
mPendingSeekTime.ref() != mFallbackSeekTime.ref()) {
|
2015-08-11 07:15:19 +03:00
|
|
|
// We have failed to seek audio where video seeked to earlier.
|
|
|
|
// Attempt to seek instead to the closest point that we know we have in
|
|
|
|
// order to limit A/V sync discrepency.
|
|
|
|
|
|
|
|
// Ensure we have the most up to date buffered ranges.
|
|
|
|
UpdateReceivedNewData(TrackType::kAudioTrack);
|
|
|
|
Maybe<media::TimeUnit> nextSeekTime;
|
|
|
|
// Find closest buffered time found after video seeked time.
|
|
|
|
for (const auto& timeRange : mAudio.mTimeRanges) {
|
|
|
|
if (timeRange.mStart >= mPendingSeekTime.ref()) {
|
|
|
|
nextSeekTime.emplace(timeRange.mStart);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nextSeekTime.isNothing() ||
|
2016-05-16 09:47:57 +03:00
|
|
|
nextSeekTime.ref() > mFallbackSeekTime.ref()) {
|
|
|
|
nextSeekTime = Some(mFallbackSeekTime.ref());
|
2015-08-11 07:15:19 +03:00
|
|
|
LOG("Unable to seek audio to video seek time. A/V sync may be broken");
|
|
|
|
} else {
|
2016-05-16 09:47:57 +03:00
|
|
|
mFallbackSeekTime.reset();
|
2015-08-11 07:15:19 +03:00
|
|
|
}
|
|
|
|
mPendingSeekTime = nextSeekTime;
|
|
|
|
DoAudioSeek();
|
|
|
|
return;
|
|
|
|
}
|
2015-05-18 08:42:01 +03:00
|
|
|
NotifyWaitingForData(aTrack);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
2015-06-15 03:58:12 +03:00
|
|
|
MOZ_ASSERT(!mVideo.mSeekRequest.Exists() && !mAudio.mSeekRequest.Exists());
|
2015-05-18 08:40:32 +03:00
|
|
|
mPendingSeekTime.reset();
|
2016-12-16 11:57:27 +03:00
|
|
|
|
|
|
|
auto type = aTrack == TrackType::kAudioTrack
|
|
|
|
? MediaData::AUDIO_DATA : MediaData::VIDEO_DATA;
|
|
|
|
mSeekPromise.Reject(SeekRejectValue(type, aError), __func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::DoVideoSeek()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mPendingSeekTime.isSome());
|
2015-05-25 08:09:16 +03:00
|
|
|
LOGV("Seeking video to %lld", mPendingSeekTime.ref().ToMicroseconds());
|
2015-05-18 08:40:32 +03:00
|
|
|
media::TimeUnit seekTime = mPendingSeekTime.ref();
|
2017-01-11 11:33:29 +03:00
|
|
|
mVideo.mTrackDemuxer->Seek(seekTime)
|
|
|
|
->Then(OwnerThread(), __func__, this,
|
|
|
|
&MediaFormatReader::OnVideoSeekCompleted,
|
|
|
|
&MediaFormatReader::OnVideoSeekFailed)
|
|
|
|
->Track(mVideo.mSeekRequest);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::OnVideoSeekCompleted(media::TimeUnit aTime)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2015-05-25 08:09:16 +03:00
|
|
|
LOGV("Video seeked to %lld", aTime.ToMicroseconds());
|
2015-06-15 03:58:12 +03:00
|
|
|
mVideo.mSeekRequest.Complete();
|
2015-05-18 08:40:32 +03:00
|
|
|
|
2016-07-26 02:36:55 +03:00
|
|
|
mPreviousDecodedKeyframeTime_us = sNoPreviousDecodedKeyframe;
|
|
|
|
|
2016-06-08 05:59:57 +03:00
|
|
|
SetVideoDecodeThreshold();
|
|
|
|
|
2016-05-16 09:47:57 +03:00
|
|
|
if (HasAudio() && !mOriginalSeekTarget.IsVideoOnly()) {
|
2016-04-18 09:33:52 +03:00
|
|
|
MOZ_ASSERT(mPendingSeekTime.isSome());
|
2016-05-16 09:47:57 +03:00
|
|
|
if (mOriginalSeekTarget.IsFast()) {
|
2016-01-28 15:25:15 +03:00
|
|
|
// We are performing a fast seek. We need to seek audio to where the
|
|
|
|
// video seeked to, to ensure proper A/V sync once playback resume.
|
|
|
|
mPendingSeekTime = Some(aTime);
|
|
|
|
}
|
2015-05-18 08:40:32 +03:00
|
|
|
DoAudioSeek();
|
|
|
|
} else {
|
|
|
|
mPendingSeekTime.reset();
|
2016-01-28 13:20:29 +03:00
|
|
|
mSeekPromise.Resolve(aTime, __func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-26 02:36:55 +03:00
|
|
|
void
|
2016-09-12 05:22:20 +03:00
|
|
|
MediaFormatReader::OnVideoSeekFailed(const MediaResult& aError)
|
2016-07-26 02:36:55 +03:00
|
|
|
{
|
|
|
|
mPreviousDecodedKeyframeTime_us = sNoPreviousDecodedKeyframe;
|
2016-09-12 05:22:20 +03:00
|
|
|
OnSeekFailed(TrackType::kVideoTrack, aError);
|
2016-07-26 02:36:55 +03:00
|
|
|
}
|
|
|
|
|
2016-06-08 05:59:57 +03:00
|
|
|
void
|
|
|
|
MediaFormatReader::SetVideoDecodeThreshold()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
|
|
|
|
if (!HasVideo() || !mVideo.mDecoder) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mVideo.mTimeThreshold && !IsSeeking()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeUnit threshold;
|
|
|
|
if (mVideo.mTimeThreshold) {
|
|
|
|
// For internalSeek.
|
|
|
|
threshold = mVideo.mTimeThreshold.ref().Time();
|
|
|
|
} else if (IsSeeking()) {
|
|
|
|
// If IsSeeking() is true, then video seek must have completed already.
|
|
|
|
TimeUnit keyframe;
|
|
|
|
if (NS_FAILED(mVideo.mTrackDemuxer->GetNextRandomAccessPoint(&keyframe))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the key frame is invalid/infinite, it means the target position is
|
|
|
|
// closing to end of stream. We don't want to skip any frame at this point.
|
|
|
|
if (!keyframe.IsValid() || keyframe.IsInfinite()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
threshold = mOriginalSeekTarget.GetTime();
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG("Set seek threshold to %lld", threshold.ToMicroseconds());
|
|
|
|
mVideo.mDecoder->SetSeekThreshold(threshold);
|
|
|
|
}
|
|
|
|
|
2015-05-18 08:40:32 +03:00
|
|
|
void
|
|
|
|
MediaFormatReader::DoAudioSeek()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mPendingSeekTime.isSome());
|
2015-05-25 08:09:16 +03:00
|
|
|
LOGV("Seeking audio to %lld", mPendingSeekTime.ref().ToMicroseconds());
|
2015-05-18 08:40:32 +03:00
|
|
|
media::TimeUnit seekTime = mPendingSeekTime.ref();
|
2017-01-11 11:33:29 +03:00
|
|
|
mAudio.mTrackDemuxer->Seek(seekTime)
|
|
|
|
->Then(OwnerThread(), __func__, this,
|
|
|
|
&MediaFormatReader::OnAudioSeekCompleted,
|
|
|
|
&MediaFormatReader::OnAudioSeekFailed)
|
|
|
|
->Track(mAudio.mSeekRequest);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::OnAudioSeekCompleted(media::TimeUnit aTime)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2015-05-25 08:09:16 +03:00
|
|
|
LOGV("Audio seeked to %lld", aTime.ToMicroseconds());
|
2015-06-15 03:58:12 +03:00
|
|
|
mAudio.mSeekRequest.Complete();
|
2015-05-18 08:40:32 +03:00
|
|
|
mPendingSeekTime.reset();
|
2016-01-28 13:20:29 +03:00
|
|
|
mSeekPromise.Resolve(aTime, __func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
2016-07-26 02:36:55 +03:00
|
|
|
void
|
2016-09-12 05:22:20 +03:00
|
|
|
MediaFormatReader::OnAudioSeekFailed(const MediaResult& aError)
|
2016-07-26 02:36:55 +03:00
|
|
|
{
|
2016-09-12 05:22:20 +03:00
|
|
|
OnSeekFailed(TrackType::kAudioTrack, aError);
|
2016-07-26 02:36:55 +03:00
|
|
|
}
|
|
|
|
|
2016-08-17 10:03:30 +03:00
|
|
|
void MediaFormatReader::ReleaseResources()
|
2015-05-18 08:40:32 +03:00
|
|
|
{
|
2017-01-26 15:56:46 +03:00
|
|
|
LOGV("");
|
|
|
|
if (mShutdown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ShutdownDecoder(TrackInfo::kAudioTrack);
|
|
|
|
ShutdownDecoder(TrackInfo::kVideoTrack);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MediaFormatReader::VideoIsHardwareAccelerated() const
|
|
|
|
{
|
2015-07-20 22:14:37 +03:00
|
|
|
return mVideo.mIsHardwareAccelerated;
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-11-28 13:08:01 +03:00
|
|
|
MediaFormatReader::NotifyTrackDemuxers()
|
2015-05-18 08:40:32 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
|
2016-05-06 08:56:47 +03:00
|
|
|
LOGV("");
|
|
|
|
|
2015-09-15 06:03:48 +03:00
|
|
|
if (!mInitDone) {
|
|
|
|
return;
|
|
|
|
}
|
2016-11-28 13:08:01 +03:00
|
|
|
|
2015-05-18 08:40:32 +03:00
|
|
|
if (HasVideo()) {
|
|
|
|
mVideo.mReceivedNewData = true;
|
|
|
|
ScheduleUpdate(TrackType::kVideoTrack);
|
|
|
|
}
|
|
|
|
if (HasAudio()) {
|
|
|
|
mAudio.mReceivedNewData = true;
|
|
|
|
ScheduleUpdate(TrackType::kAudioTrack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-11-28 13:08:01 +03:00
|
|
|
MediaFormatReader::NotifyDataArrived()
|
2015-05-18 08:40:32 +03:00
|
|
|
{
|
2015-06-17 01:37:48 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2016-11-28 13:08:01 +03:00
|
|
|
|
|
|
|
if (mShutdown || !mDemuxer ||
|
|
|
|
(!mDemuxerInitDone && !mDemuxerInitRequest.Exists())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mNotifyDataArrivedPromise.Exists()) {
|
|
|
|
// Already one in progress. Reschedule for later.
|
|
|
|
RefPtr<nsIRunnable> task(
|
|
|
|
NewRunnableMethod(this, &MediaFormatReader::NotifyDataArrived));
|
|
|
|
OwnerThread()->Dispatch(task.forget());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<MediaFormatReader> self = this;
|
2017-01-11 11:33:29 +03:00
|
|
|
mDemuxer->NotifyDataArrived()
|
|
|
|
->Then(OwnerThread(), __func__,
|
|
|
|
[self]() {
|
|
|
|
self->mNotifyDataArrivedPromise.Complete();
|
|
|
|
self->UpdateBuffered();
|
|
|
|
self->NotifyTrackDemuxers();
|
|
|
|
},
|
|
|
|
[self]() { self->mNotifyDataArrivedPromise.Complete(); })
|
|
|
|
->Track(mNotifyDataArrivedPromise);
|
2016-11-28 13:08:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::UpdateBuffered()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
|
|
|
|
if (mShutdown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mInitDone || !mHasStartTime) {
|
|
|
|
mBuffered = TimeIntervals();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HasVideo()) {
|
|
|
|
mVideo.mTimeRanges = mVideo.mTrackDemuxer->GetBuffered();
|
|
|
|
bool hasLastEnd;
|
|
|
|
media::TimeUnit lastEnd = mVideo.mTimeRanges.GetEnd(&hasLastEnd);
|
|
|
|
if (hasLastEnd) {
|
|
|
|
if (mVideo.mLastTimeRangesEnd
|
|
|
|
&& mVideo.mLastTimeRangesEnd.ref() < lastEnd) {
|
|
|
|
// New data was added after our previous end, we can clear the EOS flag.
|
|
|
|
mVideo.mDemuxEOS = false;
|
|
|
|
ScheduleUpdate(TrackInfo::kVideoTrack);
|
|
|
|
}
|
|
|
|
mVideo.mLastTimeRangesEnd = Some(lastEnd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (HasAudio()) {
|
|
|
|
mAudio.mTimeRanges = mAudio.mTrackDemuxer->GetBuffered();
|
|
|
|
bool hasLastEnd;
|
|
|
|
media::TimeUnit lastEnd = mAudio.mTimeRanges.GetEnd(&hasLastEnd);
|
|
|
|
if (hasLastEnd) {
|
|
|
|
if (mAudio.mLastTimeRangesEnd
|
|
|
|
&& mAudio.mLastTimeRangesEnd.ref() < lastEnd) {
|
|
|
|
// New data was added after our previous end, we can clear the EOS flag.
|
|
|
|
mAudio.mDemuxEOS = false;
|
|
|
|
ScheduleUpdate(TrackInfo::kAudioTrack);
|
|
|
|
}
|
|
|
|
mAudio.mLastTimeRangesEnd = Some(lastEnd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
media::TimeIntervals intervals;
|
|
|
|
if (HasAudio() && HasVideo()) {
|
|
|
|
intervals = media::Intersection(mVideo.mTimeRanges, mAudio.mTimeRanges);
|
|
|
|
} else if (HasAudio()) {
|
|
|
|
intervals = mAudio.mTimeRanges;
|
|
|
|
} else if (HasVideo()) {
|
|
|
|
intervals = mVideo.mTimeRanges;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!intervals.Length() ||
|
|
|
|
intervals.GetStart() == media::TimeUnit::FromMicroseconds(0)) {
|
|
|
|
// IntervalSet already starts at 0 or is empty, nothing to shift.
|
|
|
|
mBuffered = intervals;
|
|
|
|
} else {
|
|
|
|
mBuffered =
|
|
|
|
intervals.Shift(media::TimeUnit() - mInfo.mStartTime);
|
|
|
|
}
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
2015-10-15 03:04:00 +03:00
|
|
|
layers::ImageContainer*
|
|
|
|
MediaFormatReader::GetImageContainer()
|
|
|
|
{
|
|
|
|
return mVideoFrameContainer
|
|
|
|
? mVideoFrameContainer->GetImageContainer() : nullptr;
|
|
|
|
}
|
|
|
|
|
2016-01-18 02:21:59 +03:00
|
|
|
void
|
2017-01-18 11:51:31 +03:00
|
|
|
MediaFormatReader::GetMozDebugReaderData(nsACString& aString)
|
2016-01-18 02:21:59 +03:00
|
|
|
{
|
|
|
|
nsAutoCString result;
|
2016-01-22 06:01:50 +03:00
|
|
|
const char* audioName = "unavailable";
|
|
|
|
const char* videoName = audioName;
|
|
|
|
|
|
|
|
if (HasAudio()) {
|
2016-11-29 17:50:08 +03:00
|
|
|
MutexAutoLock lock(mAudio.mMutex);
|
2016-01-22 06:01:50 +03:00
|
|
|
audioName = mAudio.mDescription;
|
|
|
|
}
|
|
|
|
if (HasVideo()) {
|
2016-11-29 17:50:08 +03:00
|
|
|
MutexAutoLock mon(mVideo.mMutex);
|
2016-01-22 06:01:50 +03:00
|
|
|
videoName = mVideo.mDescription;
|
|
|
|
}
|
|
|
|
|
|
|
|
result += nsPrintfCString("audio decoder: %s\n", audioName);
|
|
|
|
result += nsPrintfCString("audio frames decoded: %lld\n",
|
|
|
|
mAudio.mNumSamplesOutputTotal);
|
2016-05-05 08:11:21 +03:00
|
|
|
if (HasAudio()) {
|
2017-01-26 15:56:46 +03:00
|
|
|
result += nsPrintfCString("audio state: ni=%d no=%d demuxr:%d demuxq:%d tt:%f tths:%d in:%llu out:%llu qs=%u pending:%u waiting:%d sid:%u\n",
|
2016-05-05 08:11:21 +03:00
|
|
|
NeedInput(mAudio), mAudio.HasPromise(),
|
|
|
|
mAudio.mDemuxRequest.Exists(),
|
|
|
|
int(mAudio.mQueuedSamples.Length()),
|
|
|
|
mAudio.mTimeThreshold
|
2016-05-18 06:48:05 +03:00
|
|
|
? mAudio.mTimeThreshold.ref().Time().ToSeconds()
|
2016-05-05 08:11:21 +03:00
|
|
|
: -1.0,
|
|
|
|
mAudio.mTimeThreshold
|
|
|
|
? mAudio.mTimeThreshold.ref().mHasSeeked
|
|
|
|
: -1,
|
|
|
|
mAudio.mNumSamplesInput, mAudio.mNumSamplesOutput,
|
|
|
|
unsigned(size_t(mAudio.mSizeOfQueue)),
|
|
|
|
unsigned(mAudio.mOutput.Length()),
|
2016-12-20 10:53:39 +03:00
|
|
|
mAudio.mWaitingForData,
|
2016-10-27 12:26:01 +03:00
|
|
|
mAudio.mLastStreamSourceID);
|
2016-05-05 08:11:21 +03:00
|
|
|
}
|
2016-01-22 06:01:50 +03:00
|
|
|
result += nsPrintfCString("video decoder: %s\n", videoName);
|
2016-01-18 02:21:59 +03:00
|
|
|
result += nsPrintfCString("hardware video decoding: %s\n",
|
|
|
|
VideoIsHardwareAccelerated() ? "enabled" : "disabled");
|
2016-01-22 06:01:50 +03:00
|
|
|
result += nsPrintfCString("video frames decoded: %lld (skipped:%lld)\n",
|
2016-01-18 02:21:59 +03:00
|
|
|
mVideo.mNumSamplesOutputTotal,
|
|
|
|
mVideo.mNumSamplesSkippedTotal);
|
2016-05-05 08:11:21 +03:00
|
|
|
if (HasVideo()) {
|
2017-01-26 15:56:46 +03:00
|
|
|
result += nsPrintfCString("video state: ni=%d no=%d demuxr:%d demuxq:%d tt:%f tths:%d in:%llu out:%llu qs=%u pending:%u waiting:%d sid:%u\n",
|
2016-05-05 08:11:21 +03:00
|
|
|
NeedInput(mVideo), mVideo.HasPromise(),
|
|
|
|
mVideo.mDemuxRequest.Exists(),
|
|
|
|
int(mVideo.mQueuedSamples.Length()),
|
|
|
|
mVideo.mTimeThreshold
|
2016-05-18 06:48:05 +03:00
|
|
|
? mVideo.mTimeThreshold.ref().Time().ToSeconds()
|
2016-05-05 08:11:21 +03:00
|
|
|
: -1.0,
|
|
|
|
mVideo.mTimeThreshold
|
|
|
|
? mVideo.mTimeThreshold.ref().mHasSeeked
|
|
|
|
: -1,
|
|
|
|
mVideo.mNumSamplesInput, mVideo.mNumSamplesOutput,
|
|
|
|
unsigned(size_t(mVideo.mSizeOfQueue)),
|
|
|
|
unsigned(mVideo.mOutput.Length()),
|
2016-12-20 10:53:39 +03:00
|
|
|
mVideo.mWaitingForData,
|
2016-10-27 12:26:01 +03:00
|
|
|
mVideo.mLastStreamSourceID);
|
2016-05-05 08:11:21 +03:00
|
|
|
}
|
2017-01-18 11:51:31 +03:00
|
|
|
aString += result;
|
2016-01-18 02:21:59 +03:00
|
|
|
}
|
|
|
|
|
2016-07-29 09:51:18 +03:00
|
|
|
void
|
|
|
|
MediaFormatReader::SetVideoBlankDecode(bool aIsBlankDecode)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
return SetBlankDecode(TrackType::kVideoTrack, aIsBlankDecode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::SetBlankDecode(TrackType aTrack, bool aIsBlankDecode)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
|
2017-01-16 10:20:10 +03:00
|
|
|
auto& decoder = GetDecoderData(aTrack);
|
2016-07-29 09:51:18 +03:00
|
|
|
if (decoder.mIsBlankDecode == aIsBlankDecode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-16 10:20:10 +03:00
|
|
|
LOG("%s, decoder.mIsBlankDecode = %d => aIsBlankDecode = %d",
|
|
|
|
TrackTypeToStr(aTrack), decoder.mIsBlankDecode, aIsBlankDecode);
|
|
|
|
|
2016-07-29 09:51:18 +03:00
|
|
|
decoder.mIsBlankDecode = aIsBlankDecode;
|
2017-01-26 15:56:46 +03:00
|
|
|
ShutdownDecoder(aTrack);
|
2016-07-29 09:51:18 +03:00
|
|
|
}
|
|
|
|
|
2016-10-27 10:21:05 +03:00
|
|
|
void
|
|
|
|
MediaFormatReader::OnFirstDemuxCompleted(TrackInfo::TrackType aType,
|
|
|
|
RefPtr<MediaTrackDemuxer::SamplesHolder> aSamples)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
|
|
|
|
if (mShutdown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& decoder = GetDecoderData(aType);
|
|
|
|
MOZ_ASSERT(decoder.mFirstDemuxedSampleTime.isNothing());
|
|
|
|
decoder.mFirstDemuxedSampleTime.emplace(
|
|
|
|
TimeUnit::FromMicroseconds(aSamples->mSamples[0]->mTime));
|
|
|
|
MaybeResolveMetadataPromise();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::OnFirstDemuxFailed(TrackInfo::TrackType aType,
|
|
|
|
const MediaResult& aError)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
|
|
|
|
if (mShutdown) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& decoder = GetDecoderData(aType);
|
|
|
|
MOZ_ASSERT(decoder.mFirstDemuxedSampleTime.isNothing());
|
|
|
|
decoder.mFirstDemuxedSampleTime.emplace(TimeUnit::FromInfinity());
|
|
|
|
MaybeResolveMetadataPromise();
|
|
|
|
}
|
|
|
|
|
2015-05-18 08:40:32 +03:00
|
|
|
} // namespace mozilla
|