Bug 1340943. Part 1 - rename DecoderAllocPolicy to GlobalAllocPolicy.
MozReview-Commit-ID: DJJwa56TXlC
--- a/dom/media/MediaFormatReader.cpp
+++ b/dom/media/MediaFormatReader.cpp
@@ -48,17 +48,17 @@ 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
+class GlobalAllocPolicy
{
using TrackType = TrackInfo::TrackType;
public:
class Token
{
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Token)
protected:
@@ -69,134 +69,134 @@ public:
// Acquire a token for decoder creation. Thread-safe.
auto Alloc() -> RefPtr<Promise>;
// Called by ClearOnShutdown() to delete the singleton.
void operator=(decltype(nullptr));
// Get the singleton for the given track type. Thread-safe.
- static DecoderAllocPolicy& Instance(TrackType aTrack);
+ static GlobalAllocPolicy& Instance(TrackType aTrack);
private:
class AutoDeallocToken;
using PromisePrivate = Promise::Private;
- DecoderAllocPolicy();
- ~DecoderAllocPolicy();
+ GlobalAllocPolicy();
+ ~GlobalAllocPolicy();
// 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);
// Protect access to Instance().
static StaticMutex sMutex;
ReentrantMonitor mMonitor;
// The number of decoders available for creation.
int mDecoderLimit;
// Requests to acquire tokens.
std::queue<RefPtr<PromisePrivate>> mPromises;
};
-StaticMutex DecoderAllocPolicy::sMutex;
-
-class DecoderAllocPolicy::AutoDeallocToken : public Token
+StaticMutex GlobalAllocPolicy::sMutex;
+
+class GlobalAllocPolicy::AutoDeallocToken : public Token
{
public:
- explicit AutoDeallocToken(DecoderAllocPolicy& aPolicy) : mPolicy(aPolicy) { }
+ explicit AutoDeallocToken(GlobalAllocPolicy& aPolicy) : mPolicy(aPolicy) { }
private:
~AutoDeallocToken()
{
mPolicy.Dealloc();
}
- DecoderAllocPolicy& mPolicy; // reference to a singleton object.
+ GlobalAllocPolicy& mPolicy; // reference to a singleton object.
};
-DecoderAllocPolicy::DecoderAllocPolicy()
+GlobalAllocPolicy::GlobalAllocPolicy()
: mMonitor("DecoderAllocPolicy::mMonitor")
, mDecoderLimit(MediaPrefs::MediaDecoderLimit())
{
// Non DocGroup-version AbstractThread::MainThread is fine for
// ClearOnShutdown().
AbstractThread::MainThread()->Dispatch(NS_NewRunnableFunction([this] () {
ClearOnShutdown(this, ShutdownPhase::ShutdownThreads);
}));
}
-DecoderAllocPolicy::~DecoderAllocPolicy()
+GlobalAllocPolicy::~GlobalAllocPolicy()
{
while (!mPromises.empty()) {
RefPtr<PromisePrivate> p = mPromises.front().forget();
mPromises.pop();
p->Reject(true, __func__);
}
}
-DecoderAllocPolicy&
-DecoderAllocPolicy::Instance(TrackType aTrack)
+GlobalAllocPolicy&
+GlobalAllocPolicy::Instance(TrackType aTrack)
{
StaticMutexAutoLock lock(sMutex);
if (aTrack == TrackType::kAudioTrack) {
- static auto sAudioPolicy = new DecoderAllocPolicy();
+ static auto sAudioPolicy = new GlobalAllocPolicy();
return *sAudioPolicy;
} else {
- static auto sVideoPolicy = new DecoderAllocPolicy();
+ static auto sVideoPolicy = new GlobalAllocPolicy();
return *sVideoPolicy;
}
}
auto
-DecoderAllocPolicy::Alloc() -> RefPtr<Promise>
+GlobalAllocPolicy::Alloc() -> RefPtr<Promise>
{
// No decoder limit set.
if (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()
+GlobalAllocPolicy::Dealloc()
{
ReentrantMonitorAutoEnter mon(mMonitor);
++mDecoderLimit;
ResolvePromise(mon);
}
void
-DecoderAllocPolicy::ResolvePromise(ReentrantMonitorAutoEnter& aProofOfLock)
+GlobalAllocPolicy::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(*this), __func__);
}
}
void
-DecoderAllocPolicy::operator=(std::nullptr_t)
+GlobalAllocPolicy::operator=(std::nullptr_t)
{
delete this;
}
class MediaFormatReader::DecoderFactory
{
using InitPromise = MediaDataDecoder::InitPromise;
- using TokenPromise = DecoderAllocPolicy::Promise;
- using Token = DecoderAllocPolicy::Token;
+ using TokenPromise = GlobalAllocPolicy::Promise;
+ using Token = GlobalAllocPolicy::Token;
public:
explicit DecoderFactory(MediaFormatReader* aOwner)
: mAudio(aOwner->mAudio, TrackInfo::kAudioTrack)
, mVideo(aOwner->mVideo, TrackInfo::kVideoTrack)
, mOwner(WrapNotNull(aOwner)) { }
void CreateDecoder(TrackType aTrack);
@@ -233,20 +233,20 @@ private:
WaitForInit
};
struct Data
{
Data(DecoderData& aOwnerData, TrackType aTrack)
: mOwnerData(aOwnerData)
, mTrack(aTrack)
- , mPolicy(DecoderAllocPolicy::Instance(aTrack)) { }
+ , mPolicy(GlobalAllocPolicy::Instance(aTrack)) { }
DecoderData& mOwnerData;
const TrackType mTrack;
- DecoderAllocPolicy& mPolicy;
+ GlobalAllocPolicy& mPolicy;
Stage mStage = Stage::None;
RefPtr<Token> mToken;
RefPtr<MediaDataDecoder> mDecoder;
MozPromiseRequestHolder<TokenPromise> mTokenRequest;
MozPromiseRequestHolder<InitPromise> mInitRequest;
MozPromiseRequestHolder<ShutdownPromise> mShutdownRequest;
RefPtr<ShutdownPromise> mShutdownPromise;
} mAudio, mVideo;
@@ -264,17 +264,17 @@ MediaFormatReader::DecoderFactory::Creat
{
MOZ_ASSERT(aTrack == TrackInfo::kAudioTrack
|| aTrack == TrackInfo::kVideoTrack);
RunStage(aTrack == TrackInfo::kAudioTrack ? mAudio : mVideo);
}
class MediaFormatReader::DecoderFactory::Wrapper : public MediaDataDecoder
{
- using Token = DecoderAllocPolicy::Token;
+ using Token = GlobalAllocPolicy::Token;
public:
Wrapper(already_AddRefed<MediaDataDecoder> aDecoder,
already_AddRefed<Token> aToken)
: mDecoder(aDecoder), mToken(aToken) {}
RefPtr<InitPromise> Init() override { return mDecoder->Init(); }
RefPtr<DecodePromise> Decode(MediaRawData* aSample) override