2017-07-26 12:00:44 +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 "AudioNotificationReceiver.h"
|
|
|
|
#include "mozilla/Logging.h" // for LazyLogModule
|
|
|
|
#include "mozilla/StaticMutex.h" // for StaticMutex
|
|
|
|
#include "mozilla/StaticPtr.h" // for StaticAutoPtr
|
|
|
|
#include "nsAppRunner.h" // for XRE_IsContentProcess
|
|
|
|
#include "nsTArray.h" // for nsTArray
|
|
|
|
|
|
|
|
static mozilla::LazyLogModule sLogger("AudioNotificationReceiver");
|
|
|
|
|
|
|
|
#undef ANR_LOG
|
|
|
|
#define ANR_LOG(...) MOZ_LOG(sLogger, mozilla::LogLevel::Debug, (__VA_ARGS__))
|
|
|
|
#undef ANR_LOGW
|
|
|
|
#define ANR_LOGW(...) MOZ_LOG(sLogger, mozilla::LogLevel::Warning, (__VA_ARGS__))
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace audio {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A list containing all clients subscribering the device-changed notifications.
|
|
|
|
*/
|
2017-08-29 12:40:06 +03:00
|
|
|
static StaticAutoPtr<nsTArray<DeviceChangeListener*>> sSubscribers;
|
2017-07-26 12:00:44 +03:00
|
|
|
static StaticMutex sMutex;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AudioNotificationReceiver Implementation
|
|
|
|
*/
|
|
|
|
/* static */ void
|
2017-08-29 12:40:06 +03:00
|
|
|
AudioNotificationReceiver::Register(DeviceChangeListener* aDeviceChangeListener)
|
2017-07-26 12:00:44 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(XRE_IsContentProcess());
|
|
|
|
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
|
|
|
if (!sSubscribers) {
|
2017-08-29 12:40:06 +03:00
|
|
|
sSubscribers = new nsTArray<DeviceChangeListener*>();
|
2017-07-26 12:00:44 +03:00
|
|
|
}
|
2017-08-29 12:40:06 +03:00
|
|
|
sSubscribers->AppendElement(aDeviceChangeListener);
|
2017-07-26 12:00:44 +03:00
|
|
|
|
2017-08-29 12:40:06 +03:00
|
|
|
ANR_LOG("The DeviceChangeListener: %p is registered successfully.",
|
|
|
|
aDeviceChangeListener);
|
2017-07-26 12:00:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
2017-08-29 12:40:06 +03:00
|
|
|
AudioNotificationReceiver::Unregister(DeviceChangeListener* aDeviceChangeListener)
|
2017-07-26 12:00:44 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(XRE_IsContentProcess());
|
|
|
|
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
|
|
|
MOZ_ASSERT(!sSubscribers->IsEmpty(), "No subscriber.");
|
|
|
|
|
2017-08-29 12:40:06 +03:00
|
|
|
sSubscribers->RemoveElement(aDeviceChangeListener);
|
2017-08-04 09:20:48 +03:00
|
|
|
if (sSubscribers->IsEmpty()) {
|
|
|
|
// Clear the static pointer here to prevent memory leak.
|
|
|
|
sSubscribers = nullptr;
|
|
|
|
}
|
2017-07-26 12:00:44 +03:00
|
|
|
|
2017-08-29 12:40:06 +03:00
|
|
|
ANR_LOG("The DeviceChangeListener: %p is unregistered successfully.",
|
|
|
|
aDeviceChangeListener);
|
2017-07-26 12:00:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
AudioNotificationReceiver::NotifyDefaultDeviceChanged()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(XRE_IsContentProcess());
|
|
|
|
|
|
|
|
StaticMutexAutoLock lock(sMutex);
|
2017-08-04 10:01:39 +03:00
|
|
|
|
2017-08-29 12:40:06 +03:00
|
|
|
// Do nothing when there is no DeviceChangeListener.
|
2017-08-04 10:01:39 +03:00
|
|
|
if (!sSubscribers) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-29 12:40:06 +03:00
|
|
|
for (DeviceChangeListener* stream : *sSubscribers) {
|
|
|
|
ANR_LOG("Notify the DeviceChangeListener: %p "
|
|
|
|
"that the default device has been changed.", stream);
|
2017-07-26 12:00:44 +03:00
|
|
|
stream->ResetDefaultDevice();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace audio
|
2017-08-04 10:01:39 +03:00
|
|
|
} // namespace mozilla
|