Bug 1312337. Part 2 - implement a policy object to control the number of decoders to be created concurrently.
MozReview-Commit-ID: 7D4PXedDC3D
--- a/dom/media/MediaFormatReader.cpp
+++ b/dom/media/MediaFormatReader.cpp
@@ -1,47 +1,173 @@
/* -*- 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/CDMProxy.h"
+#include "mozilla/ClearOnShutdown.h"
#include "mozilla/dom/HTMLMediaElement.h"
#include "mozilla/Preferences.h"
#include "mozilla/Telemetry.h"
#include "nsContentUtils.h"
#include "nsPrintfCString.h"
#include "nsSize.h"
#include "Layers.h"
#include "MediaData.h"
#include "MediaInfo.h"
#include "MediaFormatReader.h"
+#include "MediaPrefs.h"
#include "MediaResource.h"
#include "mozilla/SharedThreadPool.h"
#include "VideoUtils.h"
#include "VideoFrameContainer.h"
#include "mozilla/layers/ShadowLayers.h"
#include <algorithm>
+#include <queue>
using namespace mozilla::media;
using mozilla::layers::Image;
using mozilla::layers::LayerManager;
using mozilla::layers::LayersBackend;
static mozilla::LazyLogModule sFormatDecoderLog("MediaFormatReader");
mozilla::LazyLogModule gMediaDemuxerLog("MediaDemuxer");
#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__))
namespace mozilla {
+/**
+ * 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.
+ auto Alloc(TrackType aTrack) -> RefPtr<Promise>;
+
+ // Called by ClearOnShutdown() to delete the singleton.
+ void operator=(decltype(nullptr));
+
+ // Get the singleton. Thread-safe.
+ static DecoderAllocPolicy& Instance();
+
+private:
+ class AutoDeallocToken;
+ using PromisePrivate = Promise::Private;
+ DecoderAllocPolicy();
+ ~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);
+
+ ReentrantMonitor mMonitor;
+ // The number of decoders available for creation.
+ int mDecoderLimit;
+ // Requests to acquire tokens.
+ std::queue<RefPtr<PromisePrivate>> mPromises;
+};
+
+class DecoderAllocPolicy::AutoDeallocToken : public Token
+{
+private:
+ ~AutoDeallocToken() { DecoderAllocPolicy::Instance().Dealloc(); }
+};
+
+DecoderAllocPolicy::DecoderAllocPolicy()
+ : mMonitor("DecoderAllocPolicy::mMonitor")
+ , mDecoderLimit(MediaPrefs::MediaDecoderLimit())
+{
+ 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&
+DecoderAllocPolicy::Instance()
+{
+ // Note: Function-static initialization is not thread-safe in VS 2013.
+ // Don't uplift this code to 45esr.
+ static auto sPolicy = new DecoderAllocPolicy();
+ return *sPolicy;
+}
+
+auto
+DecoderAllocPolicy::Alloc(TrackType aTrack) -> RefPtr<Promise>
+{
+ // No limit for audio decoders or a negative number.
+ if (aTrack == TrackInfo::kAudioTrack || mDecoderLimit < 0) {
+ 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();
+ p->Resolve(new AutoDeallocToken(), __func__);
+ }
+}
+
+void
+DecoderAllocPolicy::operator=(std::nullptr_t)
+{
+ delete this;
+}
+
class MediaFormatReader::DecoderFactory
{
using InitPromise = MediaDataDecoder::InitPromise;
public:
explicit DecoderFactory(MediaFormatReader* aOwner) : mOwner(aOwner) {}
void CreateDecoder(TrackType aTrack);