dom/media/AudioNotificationReceiver.cpp
author Chun-Min Chang <chun.m.chang@gmail.com>
Fri, 04 Aug 2017 14:20:48 +0800
changeset 422364 357a94f6484f6aafc305fcb643686e0b8863597b
parent 421963 c3154d4481dff7541dde3991037b5357801c1e66
child 421661 2e414f305aabc2795cd4e62704559e86eb402710
permissions -rw-r--r--
Bug 1387058 - part1: Clear the static pointer when no more AudioStreams; r=jwwang

/* -*- 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 "AudioStream.h"          // for AudioStream
#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<AudioStream*>> sSubscribers;
static StaticMutex sMutex;

/*
 * AudioNotificationReceiver Implementation
 */
/* static */ void
AudioNotificationReceiver::Register(AudioStream* aAudioStream)
{
  MOZ_ASSERT(XRE_IsContentProcess());

  StaticMutexAutoLock lock(sMutex);
  if (!sSubscribers) {
    sSubscribers = new nsTArray<AudioStream*>();
  }
  sSubscribers->AppendElement(aAudioStream);

  ANR_LOG("The AudioStream: %p is registered successfully.", aAudioStream);
}

/* static */ void
AudioNotificationReceiver::Unregister(AudioStream* aAudioStream)
{
  MOZ_ASSERT(XRE_IsContentProcess());

  StaticMutexAutoLock lock(sMutex);
  MOZ_ASSERT(!sSubscribers->IsEmpty(), "No subscriber.");

  sSubscribers->RemoveElement(aAudioStream);
  if (sSubscribers->IsEmpty()) {
    // Clear the static pointer here to prevent memory leak.
    sSubscribers = nullptr;
  }

  ANR_LOG("The AudioStream: %p is unregistered successfully.", aAudioStream);
}

/* static */ void
AudioNotificationReceiver::NotifyDefaultDeviceChanged()
{
  MOZ_ASSERT(XRE_IsContentProcess());

  StaticMutexAutoLock lock(sMutex);
  for (AudioStream* stream : *sSubscribers) {
    ANR_LOG("Notify the AudioStream: %p that the default device has been changed.", stream);
    stream->ResetDefaultDevice();
  }
}

} // namespace audio
} // namespace mozilla