diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /dom/media/AudioNotificationReceiver.cpp | |
parent | Initial commit. (diff) | |
download | firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/media/AudioNotificationReceiver.cpp')
-rw-r--r-- | dom/media/AudioNotificationReceiver.cpp | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/dom/media/AudioNotificationReceiver.cpp b/dom/media/AudioNotificationReceiver.cpp new file mode 100644 index 0000000000..7bb215cc2a --- /dev/null +++ b/dom/media/AudioNotificationReceiver.cpp @@ -0,0 +1,88 @@ +/* -*- 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. + */ +static StaticAutoPtr<nsTArray<DeviceChangeListener*>> sSubscribers; +static StaticMutex sMutex; + +/* + * AudioNotificationReceiver Implementation + */ +/* static */ +void AudioNotificationReceiver::Register( + DeviceChangeListener* aDeviceChangeListener) { + MOZ_ASSERT(XRE_IsContentProcess()); + + StaticMutexAutoLock lock(sMutex); + if (!sSubscribers) { + sSubscribers = new nsTArray<DeviceChangeListener*>(); + } + sSubscribers->AppendElement(aDeviceChangeListener); + + ANR_LOG("The DeviceChangeListener: %p is registered successfully.", + aDeviceChangeListener); +} + +/* static */ +void AudioNotificationReceiver::Unregister( + DeviceChangeListener* aDeviceChangeListener) { + MOZ_ASSERT(XRE_IsContentProcess()); + + StaticMutexAutoLock lock(sMutex); + MOZ_ASSERT(!sSubscribers->IsEmpty(), "No subscriber."); + + sSubscribers->RemoveElement(aDeviceChangeListener); + if (sSubscribers->IsEmpty()) { + // Clear the static pointer here to prevent memory leak. + sSubscribers = nullptr; + } + + ANR_LOG("The DeviceChangeListener: %p is unregistered successfully.", + aDeviceChangeListener); +} + +/* static */ +void AudioNotificationReceiver::NotifyDefaultDeviceChanged() { + MOZ_ASSERT(XRE_IsContentProcess()); + + StaticMutexAutoLock lock(sMutex); + + // Do nothing when there is no DeviceChangeListener. + if (!sSubscribers) { + return; + } + + for (DeviceChangeListener* stream : *sSubscribers) { + ANR_LOG( + "Notify the DeviceChangeListener: %p " + "that the default device has been changed.", + stream); + stream->ResetDefaultDevice(); + } +} + +} // namespace audio +} // namespace mozilla |