2016-03-03 05:57:11 +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 "Benchmark.h"
|
2016-03-09 07:34:50 +03:00
|
|
|
#include "BufferMediaResource.h"
|
|
|
|
#include "MediaData.h"
|
2016-03-03 05:57:11 +03:00
|
|
|
#include "PDMFactory.h"
|
|
|
|
#include "WebMDemuxer.h"
|
|
|
|
#include "WebMSample.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
2016-03-03 06:23:19 +03:00
|
|
|
#include "mozilla/Telemetry.h"
|
2016-03-17 09:20:21 +03:00
|
|
|
#include "mozilla/dom/ContentChild.h"
|
2016-03-03 05:57:11 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2016-03-09 07:34:50 +03:00
|
|
|
const char* VP9Benchmark::sBenchmarkFpsPref = "media.benchmark.vp9.fps";
|
|
|
|
bool VP9Benchmark::sHasRunTest = false;
|
2016-03-03 05:57:11 +03:00
|
|
|
|
|
|
|
bool
|
2016-03-09 07:34:50 +03:00
|
|
|
VP9Benchmark::IsVP9DecodeFast()
|
2016-03-03 05:57:11 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2016-03-09 07:34:50 +03:00
|
|
|
bool hasPref = Preferences::HasUserValue(sBenchmarkFpsPref);
|
|
|
|
|
|
|
|
if (!sHasRunTest && !hasPref) {
|
2016-03-03 05:57:11 +03:00
|
|
|
sHasRunTest = true;
|
|
|
|
|
2016-03-09 07:34:50 +03:00
|
|
|
RefPtr<WebMDemuxer> demuxer =
|
|
|
|
new WebMDemuxer(new BufferMediaResource(sWebMSample, sizeof(sWebMSample), nullptr,
|
|
|
|
NS_LITERAL_CSTRING("video/webm")));
|
2016-03-09 13:38:49 +03:00
|
|
|
PDMFactory::Init();
|
|
|
|
|
2016-03-09 07:34:50 +03:00
|
|
|
RefPtr<Benchmark> estimiser =
|
|
|
|
new Benchmark(demuxer,
|
|
|
|
{
|
|
|
|
Preferences::GetInt("media.benchmark.frames", 300), // frames to measure
|
|
|
|
1, // start benchmarking after decoding this frame.
|
|
|
|
8, // loop after decoding that many frames.
|
|
|
|
TimeDuration::FromMilliseconds(
|
|
|
|
Preferences::GetUint("media.benchmark.timeout", 1000))
|
|
|
|
});
|
|
|
|
estimiser->Run()->Then(
|
|
|
|
AbstractThread::MainThread(), __func__,
|
|
|
|
[](uint32_t aDecodeFps) {
|
2016-03-17 09:20:21 +03:00
|
|
|
if (XRE_IsContentProcess()) {
|
|
|
|
dom::ContentChild* contentChild = dom::ContentChild::GetSingleton();
|
|
|
|
if (contentChild) {
|
|
|
|
contentChild->SendNotifyBenchmarkResult(NS_LITERAL_STRING("VP9"),
|
|
|
|
aDecodeFps);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Preferences::SetUint(sBenchmarkFpsPref, aDecodeFps);
|
|
|
|
}
|
2016-03-09 07:34:50 +03:00
|
|
|
Telemetry::Accumulate(Telemetry::ID::VIDEO_VP9_BENCHMARK_FPS, aDecodeFps);
|
|
|
|
},
|
|
|
|
[]() { });
|
2016-03-03 05:57:11 +03:00
|
|
|
}
|
|
|
|
|
2016-03-09 07:34:50 +03:00
|
|
|
if (!hasPref) {
|
2016-03-03 05:57:11 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t decodeFps = Preferences::GetUint(sBenchmarkFpsPref);
|
|
|
|
uint32_t threshold =
|
|
|
|
Preferences::GetUint("media.benchmark.vp9.threshold", 150);
|
|
|
|
|
|
|
|
return decodeFps >= threshold;
|
|
|
|
}
|
|
|
|
|
2016-03-09 07:34:50 +03:00
|
|
|
Benchmark::Benchmark(MediaDataDemuxer* aDemuxer, const Parameters& aParameters)
|
2016-03-09 13:38:49 +03:00
|
|
|
: QueueObject(AbstractThread::GetCurrent())
|
2016-03-09 07:34:50 +03:00
|
|
|
, mParameters(aParameters)
|
2016-03-03 05:57:11 +03:00
|
|
|
, mKeepAliveUntilComplete(this)
|
2016-03-09 07:34:50 +03:00
|
|
|
, mPlaybackState(this, aDemuxer)
|
2016-03-03 05:57:11 +03:00
|
|
|
{
|
2016-03-09 07:34:50 +03:00
|
|
|
MOZ_COUNT_CTOR(Benchmark);
|
2016-03-09 13:38:49 +03:00
|
|
|
MOZ_ASSERT(Thread(), "Must be run in task queue");
|
2016-03-09 07:34:50 +03:00
|
|
|
}
|
2016-03-03 05:57:11 +03:00
|
|
|
|
2016-03-09 07:34:50 +03:00
|
|
|
Benchmark::~Benchmark()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(Benchmark);
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<Benchmark::BenchmarkPromise>
|
|
|
|
Benchmark::Run()
|
|
|
|
{
|
2016-03-09 13:38:49 +03:00
|
|
|
MOZ_ASSERT(OnThread());
|
|
|
|
|
2016-03-09 07:34:50 +03:00
|
|
|
RefPtr<BenchmarkPromise> p = mPromise.Ensure(__func__);
|
2016-03-03 05:57:11 +03:00
|
|
|
RefPtr<Benchmark> self = this;
|
|
|
|
mPlaybackState.Dispatch(
|
|
|
|
NS_NewRunnableFunction([self]() { self->mPlaybackState.DemuxSamples(); }));
|
2016-03-09 07:34:50 +03:00
|
|
|
return p;
|
2016-03-03 05:57:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-03-09 07:34:50 +03:00
|
|
|
Benchmark::ReturnResult(uint32_t aDecodeFps)
|
2016-03-03 05:57:11 +03:00
|
|
|
{
|
2016-03-09 13:38:49 +03:00
|
|
|
MOZ_ASSERT(OnThread());
|
2016-03-03 05:57:11 +03:00
|
|
|
|
2016-03-09 07:34:50 +03:00
|
|
|
mPromise.ResolveIfExists(aDecodeFps, __func__);
|
2016-03-03 05:57:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Benchmark::Dispose()
|
|
|
|
{
|
2016-03-09 13:38:49 +03:00
|
|
|
MOZ_ASSERT(OnThread());
|
2016-03-03 05:57:11 +03:00
|
|
|
|
|
|
|
mKeepAliveUntilComplete = nullptr;
|
2016-03-09 07:34:50 +03:00
|
|
|
mPromise.RejectIfExists(false, __func__);
|
2016-03-03 05:57:11 +03:00
|
|
|
}
|
|
|
|
|
2016-03-09 13:38:49 +03:00
|
|
|
void
|
|
|
|
Benchmark::Init()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
PDMFactory::Init();
|
|
|
|
}
|
|
|
|
|
2016-03-09 07:34:50 +03:00
|
|
|
BenchmarkPlayback::BenchmarkPlayback(Benchmark* aMainThreadState,
|
|
|
|
MediaDataDemuxer* aDemuxer)
|
2016-03-03 05:57:11 +03:00
|
|
|
: QueueObject(new TaskQueue(GetMediaThreadPool(MediaThreadType::PLAYBACK)))
|
|
|
|
, mMainThreadState(aMainThreadState)
|
2016-03-09 07:34:50 +03:00
|
|
|
, mDecoderTaskQueue(new FlushableTaskQueue(GetMediaThreadPool(
|
|
|
|
MediaThreadType::PLATFORM_DECODER)))
|
|
|
|
, mDemuxer(aDemuxer)
|
|
|
|
, mSampleIndex(0)
|
|
|
|
, mFrameCount(0)
|
|
|
|
, mFinished(false)
|
2016-03-03 05:57:11 +03:00
|
|
|
{
|
2016-03-09 13:38:49 +03:00
|
|
|
MOZ_ASSERT(static_cast<Benchmark*>(mMainThreadState)->OnThread());
|
2016-03-03 05:57:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BenchmarkPlayback::DemuxSamples()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnThread());
|
|
|
|
|
2016-03-09 07:34:50 +03:00
|
|
|
RefPtr<Benchmark> ref(mMainThreadState);
|
|
|
|
mDemuxer->Init()->Then(
|
2016-03-03 05:57:11 +03:00
|
|
|
Thread(), __func__,
|
2016-03-09 07:34:50 +03:00
|
|
|
[this, ref](nsresult aResult) {
|
2016-03-03 05:57:11 +03:00
|
|
|
MOZ_ASSERT(OnThread());
|
2016-03-09 07:34:50 +03:00
|
|
|
mTrackDemuxer =
|
|
|
|
mDemuxer->GetTrackDemuxer(TrackInfo::kVideoTrack, 0);
|
|
|
|
if (!mTrackDemuxer) {
|
|
|
|
MainThreadShutdown();
|
2016-03-11 00:37:09 +03:00
|
|
|
return;
|
2016-03-09 07:34:50 +03:00
|
|
|
}
|
|
|
|
DemuxNextSample();
|
2016-03-03 05:57:11 +03:00
|
|
|
},
|
2016-03-09 07:34:50 +03:00
|
|
|
[this, ref](DemuxerFailureReason aReason) { MainThreadShutdown(); });
|
2016-03-03 05:57:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-03-09 07:34:50 +03:00
|
|
|
BenchmarkPlayback::DemuxNextSample()
|
2016-03-03 05:57:11 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnThread());
|
|
|
|
|
2016-03-09 07:34:50 +03:00
|
|
|
RefPtr<Benchmark> ref(mMainThreadState);
|
|
|
|
RefPtr<MediaTrackDemuxer::SamplesPromise> promise = mTrackDemuxer->GetSamples();
|
|
|
|
promise->Then(
|
|
|
|
Thread(), __func__,
|
|
|
|
[this, ref](RefPtr<MediaTrackDemuxer::SamplesHolder> aHolder) {
|
|
|
|
mSamples.AppendElements(Move(aHolder->mSamples));
|
|
|
|
if (ref->mParameters.mStopAtFrame &&
|
|
|
|
mSamples.Length() == (size_t)ref->mParameters.mStopAtFrame.ref()) {
|
|
|
|
InitDecoder(Move(*mTrackDemuxer->GetInfo()));
|
|
|
|
} else {
|
|
|
|
Dispatch(NS_NewRunnableFunction([this, ref]() { DemuxNextSample(); }));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[this, ref](DemuxerFailureReason aReason) {
|
|
|
|
switch (aReason) {
|
|
|
|
case DemuxerFailureReason::END_OF_STREAM:
|
|
|
|
InitDecoder(Move(*mTrackDemuxer->GetInfo()));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MainThreadShutdown();
|
|
|
|
}
|
|
|
|
});
|
2016-03-03 05:57:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-03-09 07:34:50 +03:00
|
|
|
BenchmarkPlayback::InitDecoder(TrackInfo&& aInfo)
|
2016-03-03 05:57:11 +03:00
|
|
|
{
|
2016-03-09 07:34:50 +03:00
|
|
|
MOZ_ASSERT(OnThread());
|
2016-03-03 05:57:11 +03:00
|
|
|
|
|
|
|
RefPtr<PDMFactory> platform = new PDMFactory();
|
2016-03-09 07:34:50 +03:00
|
|
|
mDecoder = platform->CreateDecoder(aInfo, mDecoderTaskQueue, this);
|
|
|
|
if (!mDecoder) {
|
|
|
|
MainThreadShutdown();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
RefPtr<Benchmark> ref(mMainThreadState);
|
2016-03-03 05:57:11 +03:00
|
|
|
mDecoder->Init()->Then(
|
2016-03-09 13:14:01 +03:00
|
|
|
Thread(), __func__,
|
2016-03-09 07:34:50 +03:00
|
|
|
[this, ref](TrackInfo::TrackType aTrackType) {
|
2016-03-09 13:14:01 +03:00
|
|
|
InputExhausted();
|
2016-03-03 05:57:11 +03:00
|
|
|
},
|
|
|
|
[this, ref](MediaDataDecoder::DecoderFailureReason aReason) {
|
2016-03-09 07:34:50 +03:00
|
|
|
MainThreadShutdown();
|
2016-03-03 05:57:11 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-03-09 07:34:50 +03:00
|
|
|
BenchmarkPlayback::MainThreadShutdown()
|
2016-03-03 05:57:11 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(OnThread());
|
|
|
|
|
2016-03-17 04:58:21 +03:00
|
|
|
if (mFinished) {
|
|
|
|
// Nothing more to do.
|
|
|
|
return;
|
|
|
|
}
|
2016-03-11 00:37:09 +03:00
|
|
|
mFinished = true;
|
|
|
|
|
2016-03-09 07:34:50 +03:00
|
|
|
if (mDecoder) {
|
|
|
|
mDecoder->Flush();
|
|
|
|
mDecoder->Shutdown();
|
|
|
|
mDecoder = nullptr;
|
|
|
|
}
|
2016-03-03 05:57:11 +03:00
|
|
|
|
2016-03-09 07:34:50 +03:00
|
|
|
mDecoderTaskQueue->BeginShutdown();
|
|
|
|
mDecoderTaskQueue->AwaitShutdownAndIdle();
|
|
|
|
mDecoderTaskQueue = nullptr;
|
2016-03-03 05:57:11 +03:00
|
|
|
|
2016-03-09 07:34:50 +03:00
|
|
|
if (mTrackDemuxer) {
|
|
|
|
mTrackDemuxer->Reset();
|
|
|
|
mTrackDemuxer->BreakCycles();
|
|
|
|
mTrackDemuxer = nullptr;
|
2016-03-03 05:57:11 +03:00
|
|
|
}
|
|
|
|
|
2016-03-09 07:34:50 +03:00
|
|
|
RefPtr<Benchmark> ref(mMainThreadState);
|
|
|
|
Thread()->AsTaskQueue()->BeginShutdown()->Then(
|
|
|
|
ref->Thread(), __func__,
|
|
|
|
[ref]() { ref->Dispose(); },
|
|
|
|
[]() { MOZ_CRASH("not reached"); });
|
2016-03-03 05:57:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-03-09 07:34:50 +03:00
|
|
|
BenchmarkPlayback::Output(MediaData* aData)
|
2016-03-03 05:57:11 +03:00
|
|
|
{
|
2016-03-09 07:34:50 +03:00
|
|
|
RefPtr<Benchmark> ref(mMainThreadState);
|
|
|
|
Dispatch(NS_NewRunnableFunction([this, ref]() {
|
|
|
|
mFrameCount++;
|
|
|
|
if (mFrameCount == ref->mParameters.mStartupFrame) {
|
|
|
|
mDecodeStartTime = TimeStamp::Now();
|
|
|
|
}
|
|
|
|
int32_t frames = mFrameCount - ref->mParameters.mStartupFrame;
|
|
|
|
TimeDuration elapsedTime = TimeStamp::Now() - mDecodeStartTime;
|
|
|
|
if (!mFinished &&
|
|
|
|
(frames == ref->mParameters.mFramesToMeasure ||
|
|
|
|
elapsedTime >= ref->mParameters.mTimeout)) {
|
|
|
|
uint32_t decodeFps = frames / elapsedTime.ToSeconds();
|
|
|
|
MainThreadShutdown();
|
|
|
|
ref->Dispatch(NS_NewRunnableFunction([ref, decodeFps]() {
|
|
|
|
ref->ReturnResult(decodeFps);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}));
|
2016-03-03 05:57:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-03-09 07:34:50 +03:00
|
|
|
BenchmarkPlayback::Error()
|
2016-03-03 05:57:11 +03:00
|
|
|
{
|
2016-03-09 07:34:50 +03:00
|
|
|
RefPtr<Benchmark> ref(mMainThreadState);
|
|
|
|
Dispatch(NS_NewRunnableFunction([this, ref]() { MainThreadShutdown(); }));
|
2016-03-03 05:57:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-03-09 07:34:50 +03:00
|
|
|
BenchmarkPlayback::InputExhausted()
|
2016-03-03 05:57:11 +03:00
|
|
|
{
|
2016-03-09 07:34:50 +03:00
|
|
|
RefPtr<Benchmark> ref(mMainThreadState);
|
|
|
|
Dispatch(NS_NewRunnableFunction([this, ref]() {
|
|
|
|
MOZ_ASSERT(OnThread());
|
|
|
|
if (mFinished || mSampleIndex >= mSamples.Length()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mDecoder->Input(mSamples[mSampleIndex]);
|
|
|
|
mSampleIndex++;
|
|
|
|
if (mSampleIndex == mSamples.Length()) {
|
|
|
|
if (ref->mParameters.mStopAtFrame) {
|
|
|
|
mSampleIndex = 0;
|
|
|
|
} else {
|
|
|
|
mDecoder->Drain();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
2016-03-03 05:57:11 +03:00
|
|
|
|
2016-03-09 07:34:50 +03:00
|
|
|
void
|
|
|
|
BenchmarkPlayback::DrainComplete()
|
|
|
|
{
|
|
|
|
RefPtr<Benchmark> ref(mMainThreadState);
|
|
|
|
Dispatch(NS_NewRunnableFunction([this, ref]() {
|
|
|
|
int32_t frames = mFrameCount - ref->mParameters.mStartupFrame;
|
|
|
|
TimeDuration elapsedTime = TimeStamp::Now() - mDecodeStartTime;
|
|
|
|
uint32_t decodeFps = frames / elapsedTime.ToSeconds();
|
|
|
|
MainThreadShutdown();
|
|
|
|
ref->Dispatch(NS_NewRunnableFunction([ref, decodeFps]() {
|
|
|
|
ref->ReturnResult(decodeFps);
|
|
|
|
}));
|
|
|
|
}));
|
2016-03-03 05:57:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-03-09 07:34:50 +03:00
|
|
|
BenchmarkPlayback::OnReaderTaskQueue()
|
2016-03-03 05:57:11 +03:00
|
|
|
{
|
2016-03-09 07:34:50 +03:00
|
|
|
return OnThread();
|
2016-03-03 05:57:11 +03:00
|
|
|
}
|
2016-03-09 07:34:50 +03:00
|
|
|
|
2016-03-03 05:57:11 +03:00
|
|
|
}
|