2013-12-18 07:59:11 +04: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 "MediaShutdownManager.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "mozilla/StaticPtr.h"
|
|
|
|
#include "MediaDecoder.h"
|
2015-05-19 21:15:34 +03:00
|
|
|
#include "mozilla/Logging.h"
|
2013-12-18 07:59:11 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2015-11-15 16:49:01 +03:00
|
|
|
extern LazyLogModule gMediaDecoderLog;
|
2015-05-21 23:22:04 +03:00
|
|
|
#define DECODER_LOG(type, msg) MOZ_LOG(gMediaDecoderLog, type, msg)
|
2014-02-18 02:53:53 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(MediaShutdownManager, nsIObserver)
|
2013-12-18 07:59:11 +04:00
|
|
|
|
|
|
|
MediaShutdownManager::MediaShutdownManager()
|
2016-01-04 06:53:02 +03:00
|
|
|
: mIsObservingShutdown(false)
|
|
|
|
, mIsDoingXPCOMShutDown(false)
|
|
|
|
, mCompletedShutdown(false)
|
2013-12-18 07:59:11 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_COUNT_CTOR(MediaShutdownManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaShutdownManager::~MediaShutdownManager()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_COUNT_DTOR(MediaShutdownManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that we don't use ClearOnShutdown() on this StaticRefPtr, as that
|
|
|
|
// may interfere with our shutdown listener.
|
|
|
|
StaticRefPtr<MediaShutdownManager> MediaShutdownManager::sInstance;
|
|
|
|
|
|
|
|
MediaShutdownManager&
|
|
|
|
MediaShutdownManager::Instance()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (!sInstance) {
|
|
|
|
sInstance = new MediaShutdownManager();
|
|
|
|
}
|
|
|
|
return *sInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaShutdownManager::EnsureCorrectShutdownObserverState()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mIsDoingXPCOMShutDown);
|
2014-02-18 02:53:53 +04:00
|
|
|
bool needShutdownObserver = mDecoders.Count() > 0;
|
2013-12-18 07:59:11 +04:00
|
|
|
if (needShutdownObserver != mIsObservingShutdown) {
|
|
|
|
mIsObservingShutdown = needShutdownObserver;
|
|
|
|
if (mIsObservingShutdown) {
|
|
|
|
nsContentUtils::RegisterShutdownObserver(this);
|
|
|
|
} else {
|
|
|
|
nsContentUtils::UnregisterShutdownObserver(this);
|
|
|
|
// Clear our singleton reference. This will probably delete
|
|
|
|
// this instance, so don't deref |this| clearing sInstance.
|
|
|
|
sInstance = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaShutdownManager::Register(MediaDecoder* aDecoder)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
// Don't call Register() after you've Unregistered() all the decoders,
|
|
|
|
// that's not going to work.
|
|
|
|
MOZ_ASSERT(!mDecoders.Contains(aDecoder));
|
|
|
|
mDecoders.PutEntry(aDecoder);
|
|
|
|
MOZ_ASSERT(mDecoders.Contains(aDecoder));
|
|
|
|
MOZ_ASSERT(mDecoders.Count() > 0);
|
|
|
|
EnsureCorrectShutdownObserverState();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaShutdownManager::Unregister(MediaDecoder* aDecoder)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mDecoders.Contains(aDecoder));
|
|
|
|
if (!mIsDoingXPCOMShutDown) {
|
|
|
|
mDecoders.RemoveEntry(aDecoder);
|
|
|
|
EnsureCorrectShutdownObserverState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
MediaShutdownManager::Observe(nsISupports *aSubjet,
|
|
|
|
const char *aTopic,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t *someData)
|
2013-12-18 07:59:11 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID) == 0) {
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaShutdownManager::Shutdown()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(sInstance);
|
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
DECODER_LOG(LogLevel::Debug, ("MediaShutdownManager::Shutdown() start..."));
|
2014-02-18 02:53:53 +04:00
|
|
|
|
2013-12-18 07:59:11 +04:00
|
|
|
// Mark that we're shutting down, so that Unregister(*) calls don't remove
|
|
|
|
// hashtable entries. If Unregsiter(*) was to remove from the hash table,
|
|
|
|
// the iterations over the hashtables below would be disrupted.
|
|
|
|
mIsDoingXPCOMShutDown = true;
|
|
|
|
|
|
|
|
// Iterate over the decoders and shut them down, and remove them from the
|
|
|
|
// hashtable.
|
2016-01-04 06:53:02 +03:00
|
|
|
nsTArray<RefPtr<ShutdownPromise>> promises;
|
2015-07-16 20:59:54 +03:00
|
|
|
for (auto iter = mDecoders.Iter(); !iter.Done(); iter.Next()) {
|
2016-01-04 06:53:02 +03:00
|
|
|
promises.AppendElement(iter.Get()->GetKey()->Shutdown()->Then(
|
|
|
|
// We want to ensure that all shutdowns have completed, regardless
|
|
|
|
// of the ShutdownPromise being resolved or rejected. At this stage,
|
|
|
|
// a MediaDecoder's ShutdownPromise is only ever resolved, but as this may
|
|
|
|
// change in the future we want to avoid nasty surprises, so we wrap the
|
|
|
|
// ShutdownPromise into our own that will only ever be resolved.
|
|
|
|
AbstractThread::MainThread(), __func__,
|
|
|
|
[]() -> RefPtr<ShutdownPromise> {
|
|
|
|
return ShutdownPromise::CreateAndResolve(true, __func__);
|
|
|
|
},
|
|
|
|
[]() -> RefPtr<ShutdownPromise> {
|
|
|
|
return ShutdownPromise::CreateAndResolve(true, __func__);
|
|
|
|
})->CompletionPromise());
|
2015-07-16 20:59:54 +03:00
|
|
|
iter.Remove();
|
|
|
|
}
|
2014-02-18 02:53:53 +04:00
|
|
|
|
2016-01-04 06:53:02 +03:00
|
|
|
if (!promises.IsEmpty()) {
|
|
|
|
ShutdownPromise::All(AbstractThread::MainThread(), promises)
|
|
|
|
->Then(AbstractThread::MainThread(), __func__, this,
|
|
|
|
&MediaShutdownManager::FinishShutdown,
|
|
|
|
&MediaShutdownManager::FinishShutdown);
|
|
|
|
// Wait for all decoders to complete their async shutdown...
|
|
|
|
while (!mCompletedShutdown) {
|
|
|
|
NS_ProcessNextEvent(NS_GetCurrentThread(), true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-18 07:59:11 +04:00
|
|
|
// Remove the MediaShutdownManager instance from the shutdown observer
|
|
|
|
// list.
|
|
|
|
nsContentUtils::UnregisterShutdownObserver(this);
|
|
|
|
|
|
|
|
// Clear the singleton instance. The only remaining reference should be the
|
|
|
|
// reference that the observer service used to call us with. The
|
|
|
|
// MediaShutdownManager will be deleted once the observer service cleans
|
|
|
|
// up after it finishes its notifications.
|
|
|
|
sInstance = nullptr;
|
2014-02-18 02:53:53 +04:00
|
|
|
|
2015-06-04 01:25:57 +03:00
|
|
|
DECODER_LOG(LogLevel::Debug, ("MediaShutdownManager::Shutdown() end."));
|
2013-12-18 07:59:11 +04:00
|
|
|
}
|
|
|
|
|
2016-01-04 06:53:02 +03:00
|
|
|
void
|
|
|
|
MediaShutdownManager::FinishShutdown()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mCompletedShutdown = true;
|
|
|
|
}
|
|
|
|
|
2013-12-18 07:59:11 +04:00
|
|
|
} // namespace mozilla
|