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/. */
|
|
|
|
|
|
|
|
#include "mozilla/dom/HTMLMediaElement.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
2016-04-14 00:02:00 +03:00
|
|
|
#include "mozilla/Telemetry.h"
|
2016-02-17 00:55:33 +03:00
|
|
|
#include "nsContentUtils.h"
|
2015-05-18 08:40:32 +03:00
|
|
|
#include "nsPrintfCString.h"
|
|
|
|
#include "nsSize.h"
|
|
|
|
#include "Layers.h"
|
|
|
|
#include "MediaData.h"
|
|
|
|
#include "MediaInfo.h"
|
|
|
|
#include "MediaFormatReader.h"
|
|
|
|
#include "MediaResource.h"
|
2015-08-03 21:44:10 +03:00
|
|
|
#include "mozilla/SharedThreadPool.h"
|
2015-05-18 08:40:32 +03:00
|
|
|
#include "VideoUtils.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#ifdef MOZ_EME
|
|
|
|
#include "mozilla/CDMProxy.h"
|
|
|
|
#endif
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
#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 {
|
|
|
|
|
|
|
|
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,
|
|
|
|
VideoFrameContainer* aVideoFrameContainer,
|
|
|
|
layers::LayersBackend aLayersBackend)
|
2015-10-09 04:25:23 +03:00
|
|
|
: MediaDecoderReader(aDecoder)
|
2015-05-18 08:40:32 +03:00
|
|
|
, mAudio(this, MediaData::AUDIO_DATA, Preferences::GetUint("media.audio-decode-ahead", 2))
|
|
|
|
, mVideo(this, MediaData::VIDEO_DATA, Preferences::GetUint("media.video-decode-ahead", 2))
|
2015-10-07 04:00:52 +03:00
|
|
|
, mDemuxer(aDemuxer)
|
|
|
|
, mDemuxerInitDone(false)
|
2015-05-18 08:40:32 +03:00
|
|
|
, mLastReportedNumDecodedFrames(0)
|
2015-10-15 03:04:00 +03:00
|
|
|
, mLayersBackendType(aLayersBackend)
|
2015-05-18 08:40:32 +03:00
|
|
|
, mInitDone(false)
|
|
|
|
, mIsEncrypted(false)
|
2015-06-12 02:26:58 +03:00
|
|
|
, mTrackDemuxersMayBlock(false)
|
2015-10-20 12:33:00 +03:00
|
|
|
, mDemuxOnly(false)
|
2015-10-15 03:04:00 +03:00
|
|
|
, mVideoFrameContainer(aVideoFrameContainer)
|
2015-05-18 08:40:32 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aDemuxer);
|
|
|
|
MOZ_COUNT_CTOR(MediaFormatReader);
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
2016-02-23 03:19:40 +03:00
|
|
|
if (HasVideo()) {
|
|
|
|
ReportDroppedFramesTelemetry();
|
|
|
|
}
|
|
|
|
|
2015-05-18 08:40:32 +03:00
|
|
|
mDemuxerInitRequest.DisconnectIfExists();
|
2015-07-16 02:59:12 +03:00
|
|
|
mMetadataPromise.RejectIfExists(ReadMetadataFailureReason::METADATA_ERROR, __func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
mSeekPromise.RejectIfExists(NS_ERROR_FAILURE, __func__);
|
|
|
|
mSkipRequest.DisconnectIfExists();
|
|
|
|
|
|
|
|
if (mAudio.mDecoder) {
|
|
|
|
Flush(TrackInfo::kAudioTrack);
|
2015-06-15 03:58:12 +03:00
|
|
|
if (mAudio.HasPromise()) {
|
|
|
|
mAudio.RejectPromise(CANCELED, __func__);
|
|
|
|
}
|
2015-10-21 04:28:04 +03:00
|
|
|
mAudio.mInitPromise.DisconnectIfExists();
|
2016-01-22 06:01:50 +03:00
|
|
|
mAudio.ShutdownDecoder();
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
if (mAudio.mTrackDemuxer) {
|
2015-05-25 08:09:16 +03:00
|
|
|
mAudio.ResetDemuxer();
|
2015-05-18 08:40:32 +03:00
|
|
|
mAudio.mTrackDemuxer->BreakCycles();
|
|
|
|
mAudio.mTrackDemuxer = nullptr;
|
|
|
|
}
|
|
|
|
if (mAudio.mTaskQueue) {
|
|
|
|
mAudio.mTaskQueue->BeginShutdown();
|
|
|
|
mAudio.mTaskQueue->AwaitShutdownAndIdle();
|
|
|
|
mAudio.mTaskQueue = nullptr;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(mAudio.mPromise.IsEmpty());
|
|
|
|
|
|
|
|
if (mVideo.mDecoder) {
|
|
|
|
Flush(TrackInfo::kVideoTrack);
|
2015-06-15 03:58:12 +03:00
|
|
|
if (mVideo.HasPromise()) {
|
|
|
|
mVideo.RejectPromise(CANCELED, __func__);
|
|
|
|
}
|
2015-10-21 04:28:04 +03:00
|
|
|
mVideo.mInitPromise.DisconnectIfExists();
|
2016-01-22 06:01:50 +03:00
|
|
|
mVideo.ShutdownDecoder();
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
if (mVideo.mTrackDemuxer) {
|
2015-05-25 08:09:16 +03:00
|
|
|
mVideo.ResetDemuxer();
|
2015-05-18 08:40:32 +03:00
|
|
|
mVideo.mTrackDemuxer->BreakCycles();
|
|
|
|
mVideo.mTrackDemuxer = nullptr;
|
|
|
|
}
|
|
|
|
if (mVideo.mTaskQueue) {
|
|
|
|
mVideo.mTaskQueue->BeginShutdown();
|
|
|
|
mVideo.mTaskQueue->AwaitShutdownAndIdle();
|
|
|
|
mVideo.mTaskQueue = nullptr;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(mVideo.mPromise.IsEmpty());
|
|
|
|
|
|
|
|
mDemuxer = nullptr;
|
|
|
|
|
|
|
|
mPlatform = nullptr;
|
|
|
|
|
|
|
|
return MediaDecoderReader::Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
mLayersBackendType = layerManager->GetCompositorBackendType();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool sIsEMEEnabled = false;
|
|
|
|
|
|
|
|
nsresult
|
2015-10-13 10:28:57 +03:00
|
|
|
MediaFormatReader::Init()
|
2015-05-18 08:40:32 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Must be on main thread.");
|
2015-10-06 11:56:29 +03:00
|
|
|
PDMFactory::Init();
|
2015-05-18 08:40:32 +03:00
|
|
|
|
|
|
|
InitLayersBackendType();
|
|
|
|
|
|
|
|
mAudio.mTaskQueue =
|
2015-07-16 21:13:49 +03:00
|
|
|
new FlushableTaskQueue(GetMediaThreadPool(MediaThreadType::PLATFORM_DECODER));
|
2015-05-18 08:40:32 +03:00
|
|
|
mVideo.mTaskQueue =
|
2015-07-16 21:13:49 +03:00
|
|
|
new FlushableTaskQueue(GetMediaThreadPool(MediaThreadType::PLATFORM_DECODER));
|
2015-05-18 08:40:32 +03:00
|
|
|
|
|
|
|
static bool sSetupPrefCache = false;
|
|
|
|
if (!sSetupPrefCache) {
|
|
|
|
sSetupPrefCache = true;
|
|
|
|
Preferences::AddBoolVarCache(&sIsEMEEnabled, "media.eme.enabled", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MOZ_EME
|
2016-04-26 03:23:21 +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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
// 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
|
|
|
#endif // MOZ_EME
|
|
|
|
|
2015-09-27 13:59:52 +03:00
|
|
|
bool
|
|
|
|
MediaFormatReader::IsWaitingOnCDMResource() {
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2015-05-18 08:40:32 +03:00
|
|
|
#ifdef MOZ_EME
|
2015-09-27 13:40:03 +03:00
|
|
|
return IsEncrypted() && !mCDMProxy;
|
2015-05-18 08:40:32 +03:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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__);
|
|
|
|
|
2015-05-18 08:40:32 +03:00
|
|
|
mDemuxerInitRequest.Begin(mDemuxer->Init()
|
2015-07-16 21:31:21 +03:00
|
|
|
->Then(OwnerThread(), __func__, this,
|
2015-05-22 22:28:20 +03:00
|
|
|
&MediaFormatReader::OnDemuxerInitDone,
|
|
|
|
&MediaFormatReader::OnDemuxerInitFailed));
|
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>());
|
|
|
|
|
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) {
|
|
|
|
mMetadataPromise.Reject(ReadMetadataFailureReason::METADATA_ERROR, __func__);
|
|
|
|
return;
|
|
|
|
}
|
2015-05-18 08:40:32 +03:00
|
|
|
mInfo.mVideo = *mVideo.mTrackDemuxer->GetInfo()->GetAsVideoInfo();
|
2016-01-08 03:22:53 +03:00
|
|
|
UniquePtr<TrackInfo> info(mVideo.mTrackDemuxer->GetInfo());
|
|
|
|
for (const MetadataTag& tag : info->mTags) {
|
|
|
|
tags->Put(tag.mKey, tag.mValue);
|
|
|
|
}
|
2015-05-18 08:40:32 +03:00
|
|
|
mVideo.mCallback = new DecoderCallback(this, TrackInfo::kVideoTrack);
|
|
|
|
mVideo.mTimeRanges = mVideo.mTrackDemuxer->GetBuffered();
|
2015-06-12 02:26:58 +03:00
|
|
|
mTrackDemuxersMayBlock |= mVideo.mTrackDemuxer->GetSamplesMayBlock();
|
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) {
|
|
|
|
mMetadataPromise.Reject(ReadMetadataFailureReason::METADATA_ERROR, __func__);
|
|
|
|
return;
|
|
|
|
}
|
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.
|
|
|
|
audioActive = audioInfo && audioInfo->IsValid();
|
|
|
|
if (audioActive) {
|
|
|
|
mInfo.mAudio = *audioInfo->GetAsAudioInfo();
|
|
|
|
for (const MetadataTag& tag : audioInfo->mTags) {
|
|
|
|
tags->Put(tag.mKey, tag.mValue);
|
|
|
|
}
|
|
|
|
mAudio.mCallback = new DecoderCallback(this, TrackInfo::kAudioTrack);
|
|
|
|
mAudio.mTimeRanges = mAudio.mTrackDemuxer->GetBuffered();
|
|
|
|
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();
|
|
|
|
|
|
|
|
mIsEncrypted = crypto && crypto->IsEncrypted();
|
|
|
|
|
2015-10-15 03:04:00 +03:00
|
|
|
if (mDecoder && crypto && crypto->IsEncrypted()) {
|
2015-05-18 08:40:32 +03:00
|
|
|
#ifdef MOZ_EME
|
|
|
|
// 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(
|
|
|
|
new DispatchKeyNeededEvent(mDecoder, crypto->mInitDatas[i].mInitData, NS_LITERAL_STRING("cenc")));
|
|
|
|
}
|
|
|
|
#endif // MOZ_EME
|
|
|
|
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) {
|
|
|
|
mMetadataPromise.Reject(ReadMetadataFailureReason::METADATA_ERROR, __func__);
|
|
|
|
return;
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
2015-10-21 04:28:04 +03:00
|
|
|
mInitDone = true;
|
|
|
|
RefPtr<MetadataHolder> metadata = new MetadataHolder();
|
|
|
|
metadata->mInfo = mInfo;
|
2016-02-12 04:40:19 +03:00
|
|
|
metadata->mTags = tags->Count() ? tags.release() : nullptr;
|
2015-10-21 04:28:04 +03:00
|
|
|
mMetadataPromise.Resolve(metadata, __func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::OnDemuxerInitFailed(DemuxerFailureReason aFailure)
|
|
|
|
{
|
|
|
|
mDemuxerInitRequest.Complete();
|
2015-09-15 06:03:48 +03:00
|
|
|
mMetadataPromise.Reject(ReadMetadataFailureReason::METADATA_ERROR, __func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-11-21 14:18:45 +03:00
|
|
|
MediaFormatReader::EnsureDecoderCreated(TrackType aTrack)
|
2015-05-18 08:40:32 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
|
2015-11-21 14:18:45 +03:00
|
|
|
auto& decoder = GetDecoderData(aTrack);
|
|
|
|
|
|
|
|
if (decoder.mDecoder) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-18 08:40:32 +03:00
|
|
|
if (!mPlatform) {
|
2015-10-06 11:56:29 +03:00
|
|
|
mPlatform = new PDMFactory();
|
|
|
|
NS_ENSURE_TRUE(mPlatform, false);
|
2015-05-18 08:40:32 +03:00
|
|
|
if (IsEncrypted()) {
|
|
|
|
#ifdef MOZ_EME
|
2015-09-27 13:59:50 +03:00
|
|
|
MOZ_ASSERT(mCDMProxy);
|
2015-10-06 11:56:29 +03:00
|
|
|
mPlatform->SetCDMProxy(mCDMProxy);
|
2015-05-18 08:40:32 +03:00
|
|
|
#else
|
|
|
|
// EME not supported.
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-21 14:18:45 +03:00
|
|
|
decoder.mDecoderInitialized = false;
|
2015-05-18 08:40:32 +03:00
|
|
|
|
2016-01-22 06:01:50 +03:00
|
|
|
MonitorAutoLock mon(decoder.mMonitor);
|
|
|
|
|
2015-11-21 14:18:45 +03:00
|
|
|
switch (aTrack) {
|
|
|
|
case TrackType::kAudioTrack:
|
|
|
|
decoder.mDecoder =
|
|
|
|
mPlatform->CreateDecoder(decoder.mInfo ?
|
|
|
|
*decoder.mInfo->GetAsAudioInfo() :
|
|
|
|
mInfo.mAudio,
|
|
|
|
decoder.mTaskQueue,
|
2016-04-19 10:36:19 +03:00
|
|
|
decoder.mCallback,
|
|
|
|
/* DecoderDoctorDiagnostics* */ nullptr);
|
2015-11-21 14:18:45 +03:00
|
|
|
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.
|
|
|
|
decoder.mDecoder =
|
|
|
|
mPlatform->CreateDecoder(mVideo.mInfo ?
|
|
|
|
*mVideo.mInfo->GetAsVideoInfo() :
|
|
|
|
mInfo.mVideo,
|
|
|
|
decoder.mTaskQueue,
|
|
|
|
decoder.mCallback,
|
2016-04-19 10:36:19 +03:00
|
|
|
/* DecoderDoctorDiagnostics* */ nullptr,
|
2015-11-21 14:18:45 +03:00
|
|
|
mLayersBackendType,
|
|
|
|
GetImageContainer());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-01-22 06:01:50 +03:00
|
|
|
if (decoder.mDecoder ) {
|
|
|
|
decoder.mDescription = decoder.mDecoder->GetDescriptionName();
|
|
|
|
} else {
|
|
|
|
decoder.mDescription = "error creating decoder";
|
|
|
|
}
|
2015-11-21 14:18:45 +03:00
|
|
|
return decoder.mDecoder != nullptr;
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
2015-09-30 07:35:11 +03:00
|
|
|
bool
|
|
|
|
MediaFormatReader::EnsureDecoderInitialized(TrackType aTrack)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
auto& decoder = GetDecoderData(aTrack);
|
|
|
|
|
|
|
|
if (!decoder.mDecoder || decoder.mInitPromise.Exists()) {
|
|
|
|
MOZ_ASSERT(decoder.mDecoder);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (decoder.mDecoderInitialized) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaFormatReader> self = this;
|
2015-09-30 07:35:11 +03:00
|
|
|
decoder.mInitPromise.Begin(decoder.mDecoder->Init()
|
|
|
|
->Then(OwnerThread(), __func__,
|
|
|
|
[self] (TrackType aTrack) {
|
|
|
|
auto& decoder = self->GetDecoderData(aTrack);
|
|
|
|
decoder.mInitPromise.Complete();
|
|
|
|
decoder.mDecoderInitialized = true;
|
2016-01-22 06:01:50 +03:00
|
|
|
MonitorAutoLock mon(decoder.mMonitor);
|
|
|
|
decoder.mDescription = decoder.mDecoder->GetDescriptionName();
|
2015-09-30 07:35:11 +03:00
|
|
|
self->ScheduleUpdate(aTrack);
|
|
|
|
},
|
|
|
|
[self, aTrack] (MediaDataDecoder::DecoderFailureReason aResult) {
|
|
|
|
auto& decoder = self->GetDecoderData(aTrack);
|
|
|
|
decoder.mInitPromise.Complete();
|
2016-01-22 06:01:50 +03:00
|
|
|
decoder.ShutdownDecoder();
|
2015-09-30 07:35:11 +03:00
|
|
|
self->NotifyError(aTrack);
|
|
|
|
}));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2015-06-15 04:40:39 +03:00
|
|
|
return nextKeyframe < aTimeThreshold && nextKeyframe.ToMicroseconds() >= 0;
|
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-25 08:09:16 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mSkipRequest.Exists(), "called mid-skipping");
|
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-04-22 09:16:49 +03:00
|
|
|
return MediaDataPromise::CreateAndReject(DECODE_ERROR, __func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (IsSeeking()) {
|
|
|
|
LOG("called mid-seek. Rejecting.");
|
2016-04-22 09:16:49 +03:00
|
|
|
return MediaDataPromise::CreateAndReject(CANCELED, __func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mShutdown) {
|
|
|
|
NS_WARNING("RequestVideoData on shutdown MediaFormatReader!");
|
2016-04-22 09:16:49 +03:00
|
|
|
return MediaDataPromise::CreateAndReject(CANCELED, __func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
media::TimeUnit timeThreshold{media::TimeUnit::FromMicroseconds(aTimeThreshold)};
|
2016-05-02 03:53:31 +03:00
|
|
|
// Ensure we have no pending seek going as ShouldSkip could return out of date
|
|
|
|
// information.
|
|
|
|
if (!mVideo.HasInternalSeekPending() &&
|
|
|
|
ShouldSkip(aSkipToNextKeyframe, timeThreshold)) {
|
2015-09-18 07:51:48 +03:00
|
|
|
// Cancel any pending demux request.
|
|
|
|
mVideo.mDemuxRequest.DisconnectIfExists();
|
2015-12-04 03:33:21 +03:00
|
|
|
|
|
|
|
// I think it's still possible for an output to have been sent from the decoder
|
|
|
|
// and is currently sitting in our event queue waiting to be processed. The following
|
|
|
|
// flush won't clear it, and when we return to the event loop it'll be added to our
|
|
|
|
// output queue and be used.
|
|
|
|
// This code will count that as dropped, which was the intent, but not quite true.
|
|
|
|
mDecoder->NotifyDecodedFrames(0, 0, SizeOfVideoQueueInFrames());
|
|
|
|
|
2015-05-18 08:40:32 +03:00
|
|
|
Flush(TrackInfo::kVideoTrack);
|
2016-04-22 09:16:49 +03:00
|
|
|
RefPtr<MediaDataPromise> p = mVideo.mPromise.Ensure(__func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
SkipVideoDemuxToNextKeyFrame(timeThreshold);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2016-04-22 09:16:49 +03:00
|
|
|
RefPtr<MediaDataPromise> p = mVideo.mPromise.Ensure(__func__);
|
2015-08-24 05:50:27 +03:00
|
|
|
NotifyDecodingRequested(TrackInfo::kVideoTrack);
|
|
|
|
|
2015-05-18 08:40:32 +03:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::OnDemuxFailed(TrackType aTrack, DemuxerFailureReason aFailure)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2015-06-15 03:58:12 +03:00
|
|
|
LOG("Failed to demux %s, failure:%d",
|
2015-05-18 08:40:32 +03:00
|
|
|
aTrack == TrackType::kVideoTrack ? "video" : "audio", aFailure);
|
|
|
|
auto& decoder = GetDecoderData(aTrack);
|
|
|
|
decoder.mDemuxRequest.Complete();
|
|
|
|
switch (aFailure) {
|
|
|
|
case DemuxerFailureReason::END_OF_STREAM:
|
2015-06-15 03:58:12 +03:00
|
|
|
NotifyEndOfStream(aTrack);
|
2015-05-18 08:40:32 +03:00
|
|
|
break;
|
|
|
|
case DemuxerFailureReason::DEMUXER_ERROR:
|
2015-05-18 08:42:01 +03:00
|
|
|
NotifyError(aTrack);
|
2015-05-18 08:40:32 +03:00
|
|
|
break;
|
|
|
|
case DemuxerFailureReason::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;
|
|
|
|
case DemuxerFailureReason::CANCELED:
|
|
|
|
case DemuxerFailureReason::SHUTDOWN:
|
|
|
|
if (decoder.HasPromise()) {
|
|
|
|
decoder.RejectPromise(CANCELED, __func__);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::DoDemuxVideo()
|
|
|
|
{
|
|
|
|
// TODO Use DecodeAhead value rather than 1.
|
|
|
|
mVideo.mDemuxRequest.Begin(mVideo.mTrackDemuxer->GetSamples(1)
|
2015-07-16 21:31:21 +03:00
|
|
|
->Then(OwnerThread(), __func__, this,
|
2015-05-22 22:28:20 +03:00
|
|
|
&MediaFormatReader::OnVideoDemuxCompleted,
|
|
|
|
&MediaFormatReader::OnVideoDemuxFailed));
|
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(mSeekPromise.IsEmpty(), "No sample requests allowed while seeking");
|
2015-07-15 07:43:35 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mAudio.mSeekRequest.Exists() ||
|
|
|
|
mAudio.mTimeThreshold.isSome());
|
2015-05-18 08:40:32 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mAudio.HasPromise(), "No duplicate sample requests");
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!IsSeeking(), "called mid-seek");
|
|
|
|
LOGV("");
|
|
|
|
|
|
|
|
if (!HasAudio()) {
|
|
|
|
LOG("called with no audio track");
|
2016-04-22 09:16:49 +03:00
|
|
|
return MediaDataPromise::CreateAndReject(DECODE_ERROR, __func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (IsSeeking()) {
|
|
|
|
LOG("called mid-seek. Rejecting.");
|
2016-04-22 09:16:49 +03:00
|
|
|
return MediaDataPromise::CreateAndReject(CANCELED, __func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mShutdown) {
|
|
|
|
NS_WARNING("RequestAudioData on shutdown MediaFormatReader!");
|
2016-04-22 09:16:49 +03:00
|
|
|
return MediaDataPromise::CreateAndReject(CANCELED, __func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
2016-04-22 09:16:49 +03:00
|
|
|
RefPtr<MediaDataPromise> p = mAudio.mPromise.Ensure(__func__);
|
2015-08-24 05:50:27 +03:00
|
|
|
NotifyDecodingRequested(TrackInfo::kAudioTrack);
|
2015-08-11 06:50:07 +03:00
|
|
|
|
2015-05-18 08:40:32 +03:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::DoDemuxAudio()
|
|
|
|
{
|
|
|
|
// TODO Use DecodeAhead value rather than 1.
|
|
|
|
mAudio.mDemuxRequest.Begin(mAudio.mTrackDemuxer->GetSamples(1)
|
2015-07-16 21:31:21 +03:00
|
|
|
->Then(OwnerThread(), __func__, this,
|
2015-05-22 22:28:20 +03:00
|
|
|
&MediaFormatReader::OnAudioDemuxCompleted,
|
|
|
|
&MediaFormatReader::OnAudioDemuxFailed));
|
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
|
|
|
|
MediaFormatReader::NotifyNewOutput(TrackType aTrack, MediaData* aSample)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2015-08-12 14:24:48 +03:00
|
|
|
LOGV("Received new %s sample time:%lld duration:%lld",
|
|
|
|
TrackTypeToStr(aTrack), aSample->mTime, aSample->mDuration);
|
2015-05-18 08:42:01 +03:00
|
|
|
auto& decoder = GetDecoderData(aTrack);
|
|
|
|
if (!decoder.mOutputRequested) {
|
|
|
|
LOG("MediaFormatReader produced output while flushing, discarding.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
decoder.mOutput.AppendElement(aSample);
|
|
|
|
decoder.mNumSamplesOutput++;
|
2015-08-24 05:57:29 +03:00
|
|
|
decoder.mNumSamplesOutputTotal++;
|
2016-02-23 03:19:40 +03:00
|
|
|
decoder.mNumSamplesOutputTotalSinceTelemetry++;
|
2015-05-18 08:42:01 +03:00
|
|
|
ScheduleUpdate(aTrack);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::NotifyInputExhausted(TrackType aTrack)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2015-05-25 08:09:16 +03:00
|
|
|
LOGV("Decoder has requested more %s data", TrackTypeToStr(aTrack));
|
2015-05-18 08:42:01 +03:00
|
|
|
auto& decoder = GetDecoderData(aTrack);
|
|
|
|
decoder.mInputExhausted = true;
|
|
|
|
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
|
|
|
if (!decoder.mOutputRequested) {
|
|
|
|
LOG("MediaFormatReader called DrainComplete() before flushing, ignoring.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
decoder.mDrainComplete = true;
|
|
|
|
ScheduleUpdate(aTrack);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::NotifyError(TrackType aTrack)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
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);
|
|
|
|
decoder.mError = true;
|
|
|
|
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;
|
2015-07-13 09:21:04 +03:00
|
|
|
decoder.mNeedDraining = true;
|
2015-06-15 03:58:12 +03:00
|
|
|
ScheduleUpdate(aTrack);
|
|
|
|
}
|
|
|
|
|
2015-08-24 05:50:27 +03:00
|
|
|
void
|
|
|
|
MediaFormatReader::NotifyDecodingRequested(TrackType aTrack)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
auto& decoder = GetDecoderData(aTrack);
|
|
|
|
decoder.mDecodingRequested = true;
|
|
|
|
ScheduleUpdate(aTrack);
|
|
|
|
}
|
|
|
|
|
2015-05-18 08:40:32 +03:00
|
|
|
bool
|
|
|
|
MediaFormatReader::NeedInput(DecoderData& aDecoder)
|
|
|
|
{
|
2015-05-18 08:42:01 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2015-05-18 08:40:32 +03:00
|
|
|
// We try to keep a few more compressed samples input than decoded samples
|
|
|
|
// have been output, provided the state machine has requested we send it a
|
|
|
|
// decoded sample. To account for H.264 streams which may require a longer
|
|
|
|
// run of input than we input, decoders fire an "input exhausted" callback,
|
|
|
|
// which overrides our "few more samples" threshold.
|
|
|
|
return
|
2015-07-15 07:43:35 +03:00
|
|
|
!aDecoder.mDraining &&
|
2015-05-18 08:40:32 +03:00
|
|
|
!aDecoder.mError &&
|
2015-08-24 05:50:27 +03:00
|
|
|
aDecoder.mDecodingRequested &&
|
2015-05-25 08:09:16 +03:00
|
|
|
!aDecoder.mDemuxRequest.Exists() &&
|
2016-05-01 16:37:09 +03:00
|
|
|
!aDecoder.mSeekRequest.Exists() &&
|
2015-08-24 05:50:27 +03:00
|
|
|
aDecoder.mOutput.Length() <= aDecoder.mDecodeAhead &&
|
2015-05-18 08:40:32 +03:00
|
|
|
(aDecoder.mInputExhausted || !aDecoder.mQueuedSamples.IsEmpty() ||
|
2015-06-15 03:58:12 +03:00
|
|
|
aDecoder.mTimeThreshold.isSome() ||
|
2015-08-24 05:50:27 +03:00
|
|
|
aDecoder.mNumSamplesInput - aDecoder.mNumSamplesOutput <= aDecoder.mDecodeAhead);
|
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-04-29 15:21:16 +03:00
|
|
|
NS_NewRunnableMethodWithArg<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
|
|
|
|
2015-06-18 00:22:10 +03:00
|
|
|
// Update our cached TimeRange.
|
|
|
|
decoder.mTimeRanges = decoder.mTrackDemuxer->GetBuffered();
|
2015-12-08 22:30:25 +03:00
|
|
|
|
2016-05-01 16:37:09 +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;
|
|
|
|
}
|
|
|
|
if (decoder.mDemuxRequest.Exists()) {
|
|
|
|
// We may have pending operations to process, so we want to continue
|
|
|
|
// after UpdateReceivedNewData returns.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (decoder.mDrainComplete || decoder.mDraining) {
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool hasLastEnd;
|
|
|
|
media::TimeUnit lastEnd = decoder.mTimeRanges.GetEnd(&hasLastEnd);
|
|
|
|
if (hasLastEnd) {
|
2016-02-08 16:35:20 +03:00
|
|
|
if (decoder.mLastTimeRangesEnd && decoder.mLastTimeRangesEnd.ref() < lastEnd) {
|
2015-12-08 22:30:25 +03:00
|
|
|
// New data was added after our previous end, we can clear the EOS flag.
|
|
|
|
decoder.mDemuxEOS = false;
|
|
|
|
}
|
|
|
|
decoder.mLastTimeRangesEnd = Some(lastEnd);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
2015-12-08 22:30:25 +03:00
|
|
|
decoder.mReceivedNewData = false;
|
|
|
|
if (decoder.mTimeThreshold) {
|
|
|
|
decoder.mTimeThreshold.ref().mWaiting = false;
|
|
|
|
}
|
|
|
|
decoder.mWaitingForData = false;
|
|
|
|
|
2015-05-25 08:09:16 +03:00
|
|
|
if (decoder.mError) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-01 16:37:09 +03:00
|
|
|
|
|
|
|
bool hasPendingSeek =
|
|
|
|
decoder.mTimeThreshold && !decoder.mTimeThreshold.ref().mHasSeeked;
|
|
|
|
if (hasPendingSeek || decoder.HasWaitingPromise()) {
|
|
|
|
if (hasPendingSeek) {
|
|
|
|
LOG("Attempting Internal Seek");
|
|
|
|
InternalSeek(aTrack, decoder.mTimeThreshold.ref());
|
|
|
|
}
|
|
|
|
if (decoder.HasWaitingPromise()) {
|
|
|
|
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;
|
|
|
|
}
|
2015-05-25 08:09:16 +03:00
|
|
|
if (!mSeekPromise.IsEmpty()) {
|
2015-05-18 08:40:32 +03:00
|
|
|
MOZ_ASSERT(!decoder.HasPromise());
|
2015-06-15 03:58:12 +03:00
|
|
|
if (mVideo.mSeekRequest.Exists() || mAudio.mSeekRequest.Exists()) {
|
2015-05-18 08:40:32 +03:00
|
|
|
// Already waiting for a seek to complete. Nothing more to do.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
LOG("Attempting Seek");
|
|
|
|
AttemptSeek();
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-20 12:33:00 +03:00
|
|
|
bool
|
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);
|
|
|
|
if (NS_FAILED(decoder.mDecoder->Input(aSample))) {
|
|
|
|
LOG("Unable to pass frame to decoder");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::HandleDemuxedSamples(TrackType aTrack,
|
2015-05-18 08:40:32 +03:00
|
|
|
AbstractMediaDecoder::AutoNotifyDecoded& aA)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
auto& decoder = GetDecoderData(aTrack);
|
|
|
|
|
|
|
|
if (decoder.mQueuedSamples.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
2015-06-15 03:58:12 +03:00
|
|
|
|
2015-11-21 14:18:45 +03:00
|
|
|
if (!EnsureDecoderCreated(aTrack)) {
|
2015-08-11 06:50:07 +03:00
|
|
|
NS_WARNING("Error constructing decoders");
|
|
|
|
NotifyError(aTrack);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-30 07:35:11 +03:00
|
|
|
if (!EnsureDecoderInitialized(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
|
|
|
bool samplesPending = false;
|
|
|
|
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()) {
|
|
|
|
if (samplesPending) {
|
|
|
|
// Let existing samples complete their decoding. We'll resume later.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-13 09:25:04 +03:00
|
|
|
if (decoder.mNextStreamSourceID.isNothing() ||
|
|
|
|
decoder.mNextStreamSourceID.ref() != info->GetID()) {
|
|
|
|
LOG("%s stream id has changed from:%d to:%d, draining decoder.",
|
|
|
|
TrackTypeToStr(aTrack), decoder.mLastStreamSourceID,
|
|
|
|
info->GetID());
|
2016-02-23 03:19:40 +03:00
|
|
|
if (aTrack == TrackType::kVideoTrack) {
|
|
|
|
ReportDroppedFramesTelemetry();
|
|
|
|
}
|
2015-07-13 09:25:04 +03:00
|
|
|
decoder.mNeedDraining = true;
|
|
|
|
decoder.mNextStreamSourceID = Some(info->GetID());
|
2015-08-12 14:24:48 +03:00
|
|
|
ScheduleUpdate(aTrack);
|
2015-07-13 09:25:04 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-15 03:58:12 +03:00
|
|
|
LOG("%s stream id has changed from:%d to:%d, recreating decoder.",
|
|
|
|
TrackTypeToStr(aTrack), decoder.mLastStreamSourceID,
|
|
|
|
info->GetID());
|
|
|
|
decoder.mInfo = info;
|
|
|
|
decoder.mLastStreamSourceID = info->GetID();
|
2015-12-08 22:30:25 +03:00
|
|
|
decoder.mNextStreamSourceID.reset();
|
2015-06-15 03:58:12 +03:00
|
|
|
// Flush will clear our array of queued samples. So make a copy now.
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<MediaRawData>> samples{decoder.mQueuedSamples};
|
2015-06-15 03:58:12 +03:00
|
|
|
Flush(aTrack);
|
2016-01-22 06:01:50 +03:00
|
|
|
decoder.ShutdownDecoder();
|
2015-06-15 03:58:12 +03:00
|
|
|
if (sample->mKeyframe) {
|
2015-08-11 18:29:46 +03:00
|
|
|
decoder.mQueuedSamples.AppendElements(Move(samples));
|
2015-08-29 14:01:13 +03:00
|
|
|
NotifyDecodingRequested(aTrack);
|
2015-06-15 03:58:12 +03:00
|
|
|
} else {
|
2016-02-01 07:05:25 +03:00
|
|
|
InternalSeekTarget seekTarget =
|
|
|
|
decoder.mTimeThreshold.refOr(InternalSeekTarget(TimeUnit::FromMicroseconds(sample->mTime), false));
|
2015-06-15 03:58:12 +03:00
|
|
|
LOG("Stream change occurred on a non-keyframe. Seeking to:%lld",
|
2015-12-08 22:30:25 +03:00
|
|
|
seekTarget.mTime.ToMicroseconds());
|
|
|
|
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.mOutputRequested = true;
|
|
|
|
decoder.mNumSamplesInput++;
|
|
|
|
decoder.mSizeOfQueue++;
|
|
|
|
if (aTrack == TrackInfo::kVideoTrack) {
|
|
|
|
aA.mParsed++;
|
|
|
|
}
|
2015-10-20 12:33:00 +03:00
|
|
|
|
|
|
|
if (mDemuxOnly) {
|
|
|
|
ReturnOutput(sample, aTrack);
|
|
|
|
} else if (!DecodeDemuxedSamples(aTrack, sample)) {
|
2015-08-16 04:18:11 +03:00
|
|
|
NotifyError(aTrack);
|
|
|
|
return;
|
|
|
|
}
|
2015-10-20 12:33:00 +03:00
|
|
|
|
2015-06-15 03:58:12 +03:00
|
|
|
decoder.mQueuedSamples.RemoveElementAt(0);
|
2015-10-20 12:33:00 +03:00
|
|
|
if (mDemuxOnly) {
|
|
|
|
// If demuxed-only case, ReturnOutput will resolve with one demuxed data.
|
|
|
|
// Then we should stop doing the iteration.
|
|
|
|
return;
|
|
|
|
}
|
2015-06-15 03:58:12 +03:00
|
|
|
samplesPending = true;
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
2015-06-12 02:26:58 +03:00
|
|
|
|
|
|
|
// We have serviced the decoder's request for more data.
|
|
|
|
decoder.mInputExhausted = false;
|
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-02 03:53:31 +03:00
|
|
|
LOG("%s internal seek to %f",
|
|
|
|
TrackTypeToStr(aTrack), aTarget.mTime.ToSeconds());
|
|
|
|
|
2015-12-08 22:30:25 +03:00
|
|
|
auto& decoder = GetDecoderData(aTrack);
|
|
|
|
decoder.mTimeThreshold = Some(aTarget);
|
|
|
|
RefPtr<MediaFormatReader> self = this;
|
|
|
|
decoder.ResetDemuxer();
|
|
|
|
decoder.mSeekRequest.Begin(decoder.mTrackDemuxer->Seek(decoder.mTimeThreshold.ref().mTime)
|
|
|
|
->Then(OwnerThread(), __func__,
|
|
|
|
[self, aTrack] (media::TimeUnit aTime) {
|
|
|
|
auto& decoder = self->GetDecoderData(aTrack);
|
|
|
|
decoder.mSeekRequest.Complete();
|
2016-05-01 16:37:09 +03:00
|
|
|
MOZ_ASSERT(decoder.mTimeThreshold);
|
|
|
|
decoder.mTimeThreshold.ref().mHasSeeked = true;
|
2015-12-08 22:30:25 +03:00
|
|
|
self->NotifyDecodingRequested(aTrack);
|
|
|
|
},
|
|
|
|
[self, aTrack] (DemuxerFailureReason aResult) {
|
|
|
|
auto& decoder = self->GetDecoderData(aTrack);
|
|
|
|
decoder.mSeekRequest.Complete();
|
|
|
|
switch (aResult) {
|
|
|
|
case DemuxerFailureReason::WAITING_FOR_DATA:
|
|
|
|
self->NotifyWaitingForData(aTrack);
|
|
|
|
break;
|
|
|
|
case DemuxerFailureReason::END_OF_STREAM:
|
2016-05-01 16:37:09 +03:00
|
|
|
decoder.mTimeThreshold.reset();
|
2015-12-08 22:30:25 +03:00
|
|
|
self->NotifyEndOfStream(aTrack);
|
|
|
|
break;
|
|
|
|
case DemuxerFailureReason::CANCELED:
|
|
|
|
case DemuxerFailureReason::SHUTDOWN:
|
2016-05-01 16:37:09 +03:00
|
|
|
decoder.mTimeThreshold.reset();
|
2015-12-08 22:30:25 +03:00
|
|
|
break;
|
|
|
|
default:
|
2016-05-01 16:37:09 +03:00
|
|
|
decoder.mTimeThreshold.reset();
|
2015-12-08 22:30:25 +03:00
|
|
|
self->NotifyError(aTrack);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// mOutputRequest must be set, otherwise NotifyDrainComplete()
|
|
|
|
// may reject the drain if a Flush recently occurred.
|
2015-07-28 09:36:10 +03:00
|
|
|
decoder.mOutputRequested = true;
|
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.
|
|
|
|
NotifyDrainComplete(aTrack);
|
|
|
|
return;
|
|
|
|
}
|
2015-07-13 09:21:04 +03:00
|
|
|
decoder.mDecoder->Drain();
|
2015-07-15 07:43:35 +03:00
|
|
|
decoder.mDraining = true;
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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);
|
|
|
|
if (time >= target.mTime) {
|
|
|
|
// We have reached our internal seek target.
|
|
|
|
decoder.mTimeThreshold.reset();
|
|
|
|
}
|
|
|
|
if (time < target.mTime || target.mDropTarget) {
|
|
|
|
LOGV("Internal Seeking: Dropping %s frame time:%f wanted:%f (kf:%d)",
|
|
|
|
TrackTypeToStr(aTrack),
|
|
|
|
media::TimeUnit::FromMicroseconds(output->mTime).ToSeconds(),
|
|
|
|
target.mTime.ToSeconds(),
|
|
|
|
output->mKeyframe);
|
|
|
|
decoder.mOutput.RemoveElementAt(0);
|
|
|
|
}
|
2015-05-18 08:42:01 +03:00
|
|
|
}
|
2015-07-13 09:21:04 +03:00
|
|
|
|
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()) {
|
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;
|
|
|
|
a.mDecoded = static_cast<uint32_t>(delta);
|
|
|
|
mLastReportedNumDecodedFrames = decoder.mNumSamplesOutputTotal;
|
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
|
|
|
}
|
2015-12-18 01:37:03 +03:00
|
|
|
RefPtr<MediaData> output = decoder.mOutput[0];
|
|
|
|
decoder.mOutput.RemoveElementAt(0);
|
|
|
|
decoder.mSizeOfQueue -= 1;
|
|
|
|
decoder.mLastSampleTime =
|
|
|
|
Some(media::TimeUnit::FromMicroseconds(output->mTime));
|
|
|
|
ReturnOutput(output, aTrack);
|
|
|
|
} else if (decoder.mError) {
|
|
|
|
LOG("Rejecting %s promise: DECODE_ERROR", TrackTypeToStr(aTrack));
|
2015-12-08 22:30:25 +03:00
|
|
|
decoder.RejectPromise(DECODE_ERROR, __func__);
|
|
|
|
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));
|
2015-07-13 09:21:04 +03:00
|
|
|
decoder.RejectPromise(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",
|
|
|
|
decoder.mLastSampleTime.ref().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));
|
|
|
|
decoder.RejectPromise(WAITING_FOR_DATA, __func__);
|
|
|
|
}
|
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-01 16:37:09 +03:00
|
|
|
if (UpdateReceivedNewData(aTrack) ||
|
|
|
|
(decoder.mTimeThreshold && 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
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2015-12-08 22:30:25 +03:00
|
|
|
bool needInput = NeedInput(decoder);
|
2015-05-18 08:40:32 +03:00
|
|
|
|
2015-12-08 22:30:25 +03:00
|
|
|
LOGV("Update(%s) ni=%d no=%d ie=%d, in:%llu out:%llu qs=%u pending:%u waiting:%d ahead:%d sid:%u",
|
2015-05-25 08:09:16 +03:00
|
|
|
TrackTypeToStr(aTrack), needInput, needOutput, decoder.mInputExhausted,
|
|
|
|
decoder.mNumSamplesInput, decoder.mNumSamplesOutput,
|
2015-08-24 05:50:27 +03:00
|
|
|
uint32_t(size_t(decoder.mSizeOfQueue)), uint32_t(decoder.mOutput.Length()),
|
2015-12-08 22:30:25 +03:00
|
|
|
decoder.mWaitingForData, !decoder.HasPromise(), decoder.mLastStreamSourceID);
|
|
|
|
|
|
|
|
if (decoder.mWaitingForData &&
|
|
|
|
(!decoder.mTimeThreshold || decoder.mTimeThreshold.ref().mWaiting)) {
|
|
|
|
// Nothing more we can do at present.
|
|
|
|
LOGV("Still waiting for data.");
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
auto& decoder = GetDecoderData(aTrack);
|
|
|
|
MOZ_ASSERT(decoder.HasPromise());
|
|
|
|
if (decoder.mDiscontinuity) {
|
2015-05-25 08:09:16 +03:00
|
|
|
LOGV("Setting discontinuity flag");
|
2015-05-18 08:40:32 +03:00
|
|
|
decoder.mDiscontinuity = false;
|
|
|
|
aData->mDiscontinuity = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aTrack == TrackInfo::kAudioTrack) {
|
2015-10-20 12:33:00 +03:00
|
|
|
if (aData->mType != MediaData::RAW_DATA) {
|
|
|
|
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
|
|
|
}
|
2015-10-20 12:33:00 +03:00
|
|
|
mAudio.mPromise.Resolve(aData, __func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
} else if (aTrack == TrackInfo::kVideoTrack) {
|
2016-01-12 13:03:13 +03:00
|
|
|
if (aData->mType != MediaData::RAW_DATA) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2015-10-20 12:33:00 +03:00
|
|
|
mVideo.mPromise.Resolve(aData, __func__);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
2015-05-25 08:09:16 +03:00
|
|
|
LOG("Resolved data promise for %s", TrackTypeToStr(aTrack));
|
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);
|
2015-07-20 09:05:16 +03:00
|
|
|
if (!decoder.mWaitingForData) {
|
|
|
|
// We aren't waiting for data any longer.
|
|
|
|
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
|
|
|
|
MediaFormatReader::ResetDecode()
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
mAudio.mWaitingPromise.RejectIfExists(WaitForDataRejectValue(MediaData::AUDIO_DATA, WaitForDataRejectValue::CANCELED), __func__);
|
|
|
|
mVideo.mWaitingPromise.RejectIfExists(WaitForDataRejectValue(MediaData::VIDEO_DATA, WaitForDataRejectValue::CANCELED), __func__);
|
|
|
|
|
|
|
|
// Reset miscellaneous seeking state.
|
|
|
|
mPendingSeekTime.reset();
|
|
|
|
|
|
|
|
if (HasVideo()) {
|
2015-05-25 08:09:16 +03:00
|
|
|
mVideo.ResetDemuxer();
|
2015-05-18 08:40:32 +03:00
|
|
|
Flush(TrackInfo::kVideoTrack);
|
2015-06-15 03:58:12 +03:00
|
|
|
if (mVideo.HasPromise()) {
|
|
|
|
mVideo.RejectPromise(CANCELED, __func__);
|
|
|
|
}
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
if (HasAudio()) {
|
2015-05-25 08:09:16 +03:00
|
|
|
mAudio.ResetDemuxer();
|
2015-05-18 08:40:32 +03:00
|
|
|
Flush(TrackInfo::kAudioTrack);
|
2015-06-15 03:58:12 +03:00
|
|
|
if (mAudio.HasPromise()) {
|
|
|
|
mAudio.RejectPromise(CANCELED, __func__);
|
|
|
|
}
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
return MediaDecoderReader::ResetDecode();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::Output(TrackType aTrack, MediaData* aSample)
|
|
|
|
{
|
2015-07-30 05:22:12 +03:00
|
|
|
LOGV("Decoded %s sample time=%lld timecode=%lld kf=%d dur=%lld",
|
|
|
|
TrackTypeToStr(aTrack), aSample->mTime, aSample->mTimecode,
|
|
|
|
aSample->mKeyframe, aSample->mDuration);
|
2015-05-18 08:40:32 +03:00
|
|
|
|
|
|
|
if (!aSample) {
|
|
|
|
NS_WARNING("MediaFormatReader::Output() passed a null sample");
|
|
|
|
Error(aTrack);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsIRunnable> task =
|
2016-04-29 15:21:16 +03:00
|
|
|
NS_NewRunnableMethodWithArgs<TrackType, MediaData*>(
|
2015-05-18 08:42:01 +03:00
|
|
|
this, &MediaFormatReader::NotifyNewOutput, aTrack, aSample);
|
2015-07-16 21:31:21 +03:00
|
|
|
OwnerThread()->Dispatch(task.forget());
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::DrainComplete(TrackType aTrack)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsIRunnable> task =
|
2016-04-29 15:21:16 +03:00
|
|
|
NS_NewRunnableMethodWithArg<TrackType>(
|
2015-05-18 08:42:01 +03:00
|
|
|
this, &MediaFormatReader::NotifyDrainComplete, aTrack);
|
2015-07-16 21:31:21 +03:00
|
|
|
OwnerThread()->Dispatch(task.forget());
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::InputExhausted(TrackType aTrack)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsIRunnable> task =
|
2016-04-29 15:21:16 +03:00
|
|
|
NS_NewRunnableMethodWithArg<TrackType>(
|
2015-05-18 08:42:01 +03:00
|
|
|
this, &MediaFormatReader::NotifyInputExhausted, aTrack);
|
2015-07-16 21:31:21 +03:00
|
|
|
OwnerThread()->Dispatch(task.forget());
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::Error(TrackType aTrack)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsIRunnable> task =
|
2016-04-29 15:21:16 +03:00
|
|
|
NS_NewRunnableMethodWithArg<TrackType>(
|
2015-05-18 08:42:01 +03:00
|
|
|
this, &MediaFormatReader::NotifyError, aTrack);
|
2015-07-16 21:31:21 +03:00
|
|
|
OwnerThread()->Dispatch(task.forget());
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::Flush(TrackType aTrack)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
LOG("Flush(%s) BEGIN", TrackTypeToStr(aTrack));
|
|
|
|
|
|
|
|
auto& decoder = GetDecoderData(aTrack);
|
|
|
|
if (!decoder.mDecoder) {
|
2015-12-04 09:31:55 +03:00
|
|
|
decoder.ResetState();
|
2015-05-18 08:40:32 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
decoder.mDecoder->Flush();
|
2015-05-18 08:42:01 +03:00
|
|
|
// Purge the current decoder's state.
|
|
|
|
// ResetState clears mOutputRequested flag so that we ignore all output until
|
|
|
|
// the next request for more data.
|
2015-05-18 08:40:32 +03:00
|
|
|
decoder.ResetState();
|
|
|
|
LOG("Flush(%s) END", TrackTypeToStr(aTrack));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::SkipVideoDemuxToNextKeyFrame(media::TimeUnit aTimeThreshold)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
|
|
|
|
MOZ_ASSERT(mVideo.mDecoder);
|
|
|
|
MOZ_ASSERT(mVideo.HasPromise());
|
2015-08-24 05:50:27 +03:00
|
|
|
MOZ_ASSERT(!mVideo.mDecodingRequested);
|
2015-05-18 08:40:32 +03:00
|
|
|
LOG("Skipping up to %lld", aTimeThreshold.ToMicroseconds());
|
|
|
|
|
|
|
|
if (mVideo.mError) {
|
|
|
|
mVideo.RejectPromise(DECODE_ERROR, __func__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSkipRequest.Begin(mVideo.mTrackDemuxer->SkipToNextRandomAccessPoint(aTimeThreshold)
|
2015-07-16 21:31:21 +03:00
|
|
|
->Then(OwnerThread(), __func__, this,
|
2015-05-22 22:28:20 +03:00
|
|
|
&MediaFormatReader::OnVideoSkipCompleted,
|
|
|
|
&MediaFormatReader::OnVideoSkipFailed));
|
2015-05-18 08:40:32 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::OnVideoSkipCompleted(uint32_t aSkipped)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
LOG("Skipping succeeded, skipped %u frames", aSkipped);
|
|
|
|
mSkipRequest.Complete();
|
2015-10-15 03:04:00 +03:00
|
|
|
if (mDecoder) {
|
|
|
|
mDecoder->NotifyDecodedFrames(aSkipped, 0, aSkipped);
|
|
|
|
}
|
2016-01-18 02:21:59 +03:00
|
|
|
mVideo.mNumSamplesSkippedTotal += aSkipped;
|
2016-02-23 03:19:40 +03:00
|
|
|
mVideo.mNumSamplesSkippedTotalSinceTelemetry += aSkipped;
|
2015-05-18 08:40:32 +03:00
|
|
|
MOZ_ASSERT(!mVideo.mError); // We have flushed the decoder, no frame could
|
|
|
|
// have been decoded (and as such errored)
|
2015-08-24 05:50:27 +03:00
|
|
|
NotifyDecodingRequested(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();
|
2015-10-15 03:04:00 +03:00
|
|
|
if (mDecoder) {
|
|
|
|
mDecoder->NotifyDecodedFrames(aFailure.mSkipped, 0, aFailure.mSkipped);
|
|
|
|
}
|
2015-05-18 08:40:32 +03:00
|
|
|
MOZ_ASSERT(mVideo.HasPromise());
|
2015-06-15 03:58:12 +03:00
|
|
|
switch (aFailure.mFailure) {
|
|
|
|
case DemuxerFailureReason::END_OF_STREAM:
|
|
|
|
NotifyEndOfStream(TrackType::kVideoTrack);
|
|
|
|
break;
|
|
|
|
case DemuxerFailureReason::WAITING_FOR_DATA:
|
|
|
|
NotifyWaitingForData(TrackType::kVideoTrack);
|
|
|
|
break;
|
|
|
|
case DemuxerFailureReason::CANCELED:
|
|
|
|
case DemuxerFailureReason::SHUTDOWN:
|
2015-08-25 14:35:15 +03:00
|
|
|
if (mVideo.HasPromise()) {
|
|
|
|
mVideo.RejectPromise(CANCELED, __func__);
|
|
|
|
}
|
2015-06-15 03:58:12 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NotifyError(TrackType::kVideoTrack);
|
|
|
|
break;
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MediaDecoderReader::SeekPromise>
|
2016-02-01 07:05:25 +03:00
|
|
|
MediaFormatReader::Seek(SeekTarget aTarget, int64_t aUnused)
|
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());
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mAudio.HasPromise());
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mPendingSeekTime.isNothing());
|
2015-06-15 03:58:12 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mVideo.mTimeThreshold.isNothing());
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(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-01-28 15:25:15 +03:00
|
|
|
mOriginalSeekTarget = Some(aTarget);
|
|
|
|
mPendingSeekTime = Some(aTarget.GetTime());
|
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-04-29 15:21:16 +03:00
|
|
|
RefPtr<nsIRunnable> task(
|
|
|
|
NS_NewRunnableMethod(this, &MediaFormatReader::AttemptSeek));
|
|
|
|
OwnerThread()->Dispatch(task.forget());
|
2015-05-18 08:40:32 +03:00
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::AttemptSeek()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2015-09-28 08:23:28 +03:00
|
|
|
if (mPendingSeekTime.isNothing()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// An internal seek may be pending due to Seek queueing multiple tasks calling
|
|
|
|
// AttemptSeek ; we can ignore those by resetting any pending demuxer's seek.
|
|
|
|
mAudio.mSeekRequest.DisconnectIfExists();
|
|
|
|
mVideo.mSeekRequest.DisconnectIfExists();
|
2015-05-18 08:40:32 +03:00
|
|
|
if (HasVideo()) {
|
|
|
|
DoVideoSeek();
|
|
|
|
} else if (HasAudio()) {
|
|
|
|
DoAudioSeek();
|
|
|
|
} else {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaFormatReader::OnSeekFailed(TrackType aTrack, DemuxerFailureReason aResult)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2015-06-15 03:58:12 +03:00
|
|
|
LOGV("%s failure:%d", TrackTypeToStr(aTrack), aResult);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if (aResult == DemuxerFailureReason::WAITING_FOR_DATA) {
|
2015-08-11 07:15:19 +03:00
|
|
|
if (HasVideo() && aTrack == TrackType::kAudioTrack &&
|
2016-01-28 15:25:15 +03:00
|
|
|
mOriginalSeekTarget.isSome() &&
|
|
|
|
mPendingSeekTime.ref() != mOriginalSeekTarget.ref().GetTime()) {
|
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-01-28 15:25:15 +03:00
|
|
|
nextSeekTime.ref() > mOriginalSeekTarget.ref().GetTime()) {
|
|
|
|
nextSeekTime = Some(mOriginalSeekTarget.ref().GetTime());
|
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-01-28 15:25:15 +03:00
|
|
|
mOriginalSeekTarget.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
|
|
|
return;
|
|
|
|
}
|
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();
|
|
|
|
mSeekPromise.Reject(NS_ERROR_FAILURE, __func__);
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2015-06-15 03:58:12 +03:00
|
|
|
mVideo.mSeekRequest.Begin(mVideo.mTrackDemuxer->Seek(seekTime)
|
2015-07-16 21:31:21 +03:00
|
|
|
->Then(OwnerThread(), __func__, this,
|
2015-05-22 22:28:20 +03:00
|
|
|
&MediaFormatReader::OnVideoSeekCompleted,
|
|
|
|
&MediaFormatReader::OnVideoSeekFailed));
|
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
|
|
|
|
|
|
|
if (HasAudio()) {
|
2016-01-28 15:25:15 +03:00
|
|
|
MOZ_ASSERT(mPendingSeekTime.isSome() && mOriginalSeekTarget.isSome());
|
|
|
|
if (mOriginalSeekTarget.ref().IsFast()) {
|
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2015-06-15 03:58:12 +03:00
|
|
|
mAudio.mSeekRequest.Begin(mAudio.mTrackDemuxer->Seek(seekTime)
|
2015-07-16 21:31:21 +03:00
|
|
|
->Then(OwnerThread(), __func__, this,
|
2015-05-22 22:28:20 +03:00
|
|
|
&MediaFormatReader::OnAudioSeekCompleted,
|
|
|
|
&MediaFormatReader::OnAudioSeekFailed));
|
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
|
|
|
}
|
|
|
|
|
2015-05-18 09:15:47 +03:00
|
|
|
media::TimeIntervals
|
|
|
|
MediaFormatReader::GetBuffered()
|
2015-05-18 08:40:32 +03:00
|
|
|
{
|
2015-06-18 00:22:10 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2015-05-18 08:40:32 +03:00
|
|
|
media::TimeIntervals videoti;
|
|
|
|
media::TimeIntervals audioti;
|
2015-05-25 12:27:21 +03:00
|
|
|
media::TimeIntervals intervals;
|
2015-05-18 08:40:32 +03:00
|
|
|
|
2015-05-25 08:09:16 +03:00
|
|
|
if (!mInitDone) {
|
2015-05-25 12:27:21 +03:00
|
|
|
return intervals;
|
|
|
|
}
|
2016-02-03 03:44:17 +03:00
|
|
|
int64_t startTime = 0;
|
2015-07-22 13:20:53 +03:00
|
|
|
if (!ForceZeroStartTime()) {
|
|
|
|
if (!HaveStartTime()) {
|
|
|
|
return intervals;
|
|
|
|
}
|
2015-07-02 02:28:47 +03:00
|
|
|
startTime = StartTime();
|
2015-05-25 08:09:16 +03:00
|
|
|
}
|
2015-06-18 00:22:10 +03:00
|
|
|
// Ensure we have up to date buffered time range.
|
|
|
|
if (HasVideo()) {
|
|
|
|
UpdateReceivedNewData(TrackType::kVideoTrack);
|
|
|
|
}
|
|
|
|
if (HasAudio()) {
|
|
|
|
UpdateReceivedNewData(TrackType::kAudioTrack);
|
|
|
|
}
|
|
|
|
if (HasVideo()) {
|
|
|
|
videoti = mVideo.mTimeRanges;
|
|
|
|
}
|
|
|
|
if (HasAudio()) {
|
|
|
|
audioti = mAudio.mTimeRanges;
|
|
|
|
}
|
|
|
|
if (HasAudio() && HasVideo()) {
|
|
|
|
intervals = media::Intersection(Move(videoti), Move(audioti));
|
|
|
|
} else if (HasAudio()) {
|
|
|
|
intervals = Move(audioti);
|
|
|
|
} else if (HasVideo()) {
|
|
|
|
intervals = Move(videoti);
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
2016-02-03 03:44:17 +03:00
|
|
|
if (!intervals.Length() ||
|
|
|
|
intervals.GetStart() == media::TimeUnit::FromMicroseconds(0)) {
|
|
|
|
// IntervalSet already starts at 0 or is empty, nothing to shift.
|
|
|
|
return intervals;
|
|
|
|
}
|
2015-05-25 12:27:21 +03:00
|
|
|
return intervals.Shift(media::TimeUnit::FromMicroseconds(-startTime));
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
2016-03-09 05:32:49 +03:00
|
|
|
// For the MediaFormatReader override we need to force an update to the
|
|
|
|
// buffered ranges, so we call NotifyDataArrive
|
|
|
|
RefPtr<MediaDecoderReader::BufferedUpdatePromise>
|
|
|
|
MediaFormatReader::UpdateBufferedWithPromise() {
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
// Call NotifyDataArrive to force a recalculation of the buffered
|
|
|
|
// ranges. UpdateBuffered alone will not force a recalculation, so we
|
|
|
|
// use NotifyDataArrived which sets flags to force this recalculation.
|
|
|
|
// See MediaFormatReader::UpdateReceivedNewData for an example of where
|
|
|
|
// the new data flag is used.
|
|
|
|
NotifyDataArrived();
|
|
|
|
return BufferedUpdatePromise::CreateAndResolve(true, __func__);
|
|
|
|
}
|
|
|
|
|
2015-10-21 09:04:20 +03:00
|
|
|
void MediaFormatReader::ReleaseMediaResources()
|
2015-05-18 08:40:32 +03:00
|
|
|
{
|
|
|
|
// Before freeing a video codec, all video buffers needed to be released
|
|
|
|
// even from graphics pipeline.
|
2015-10-15 03:04:00 +03:00
|
|
|
if (mVideoFrameContainer) {
|
|
|
|
mVideoFrameContainer->ClearCurrentFrame();
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
2016-01-22 06:01:50 +03:00
|
|
|
mVideo.mInitPromise.DisconnectIfExists();
|
|
|
|
mVideo.ShutdownDecoder();
|
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
|
2015-11-02 16:28:57 +03:00
|
|
|
MediaFormatReader::NotifyDemuxer()
|
2015-05-18 08:40:32 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
|
2015-10-07 04:00:52 +03:00
|
|
|
if (mShutdown || !mDemuxer ||
|
|
|
|
(!mDemuxerInitDone && !mDemuxerInitRequest.Exists())) {
|
2015-05-18 08:40:32 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-26 09:10:29 +03:00
|
|
|
mDemuxer->NotifyDataArrived();
|
|
|
|
|
2015-09-15 06:03:48 +03:00
|
|
|
if (!mInitDone) {
|
|
|
|
return;
|
|
|
|
}
|
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
|
2015-11-02 16:28:57 +03:00
|
|
|
MediaFormatReader::NotifyDataArrivedInternal()
|
2015-05-18 08:40:32 +03:00
|
|
|
{
|
2015-06-17 01:37:48 +03:00
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
2015-11-02 16:28:57 +03:00
|
|
|
NotifyDemuxer();
|
2015-05-18 08:40:32 +03:00
|
|
|
}
|
|
|
|
|
2015-06-07 00:42:40 +03:00
|
|
|
bool
|
|
|
|
MediaFormatReader::ForceZeroStartTime() const
|
|
|
|
{
|
|
|
|
return !mDemuxer->ShouldComputeStartTime();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
MediaFormatReader::GetMozDebugReaderData(nsAString& aString)
|
|
|
|
{
|
|
|
|
nsAutoCString result;
|
2016-01-22 06:01:50 +03:00
|
|
|
const char* audioName = "unavailable";
|
|
|
|
const char* videoName = audioName;
|
|
|
|
|
|
|
|
if (HasAudio()) {
|
|
|
|
MonitorAutoLock mon(mAudio.mMonitor);
|
|
|
|
audioName = mAudio.mDescription;
|
|
|
|
}
|
|
|
|
if (HasVideo()) {
|
|
|
|
MonitorAutoLock mon(mVideo.mMonitor);
|
|
|
|
videoName = mVideo.mDescription;
|
|
|
|
}
|
|
|
|
|
|
|
|
result += nsPrintfCString("audio decoder: %s\n", audioName);
|
|
|
|
result += nsPrintfCString("audio frames decoded: %lld\n",
|
|
|
|
mAudio.mNumSamplesOutputTotal);
|
|
|
|
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);
|
|
|
|
aString += NS_ConvertUTF8toUTF16(result);
|
|
|
|
}
|
|
|
|
|
2016-02-23 03:19:40 +03:00
|
|
|
void
|
|
|
|
MediaFormatReader::ReportDroppedFramesTelemetry()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnTaskQueue());
|
|
|
|
|
|
|
|
const VideoInfo* info =
|
|
|
|
mVideo.mInfo ? mVideo.mInfo->GetAsVideoInfo() : &mInfo.mVideo;
|
|
|
|
|
|
|
|
if (!info || !mVideo.mDecoder) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString keyPhrase = nsCString("MimeType=");
|
|
|
|
keyPhrase.Append(info->mMimeType);
|
|
|
|
keyPhrase.Append("; ");
|
|
|
|
|
|
|
|
keyPhrase.Append("Resolution=");
|
|
|
|
keyPhrase.AppendInt(info->mDisplay.width);
|
|
|
|
keyPhrase.Append('x');
|
|
|
|
keyPhrase.AppendInt(info->mDisplay.height);
|
|
|
|
keyPhrase.Append("; ");
|
|
|
|
|
|
|
|
keyPhrase.Append("HardwareAcceleration=");
|
|
|
|
if (VideoIsHardwareAccelerated()) {
|
|
|
|
keyPhrase.Append(mVideo.mDecoder->GetDescriptionName());
|
|
|
|
keyPhrase.Append("enabled");
|
|
|
|
} else {
|
|
|
|
keyPhrase.Append("disabled");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mVideo.mNumSamplesOutputTotalSinceTelemetry) {
|
|
|
|
uint32_t percentage =
|
|
|
|
100 * mVideo.mNumSamplesSkippedTotalSinceTelemetry /
|
|
|
|
mVideo.mNumSamplesOutputTotalSinceTelemetry;
|
|
|
|
nsCOMPtr<nsIRunnable> task = NS_NewRunnableFunction([=]() -> void {
|
|
|
|
LOG("Reporting telemetry DROPPED_FRAMES_IN_VIDEO_PLAYBACK");
|
|
|
|
Telemetry::Accumulate(Telemetry::VIDEO_DETAILED_DROPPED_FRAMES_PROPORTION,
|
|
|
|
keyPhrase,
|
|
|
|
percentage);
|
|
|
|
});
|
|
|
|
AbstractThread::MainThread()->Dispatch(task.forget());
|
|
|
|
}
|
|
|
|
|
|
|
|
mVideo.mNumSamplesSkippedTotalSinceTelemetry = 0;
|
|
|
|
mVideo.mNumSamplesOutputTotalSinceTelemetry = 0;
|
|
|
|
}
|
|
|
|
|
2015-05-18 08:40:32 +03:00
|
|
|
} // namespace mozilla
|