tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

AudioFocusManager.cpp (1918B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 #include "AudioFocusManager.h"
      6 
      7 #include "MediaControlService.h"
      8 #include "MediaControlUtils.h"
      9 #include "MediaController.h"
     10 #include "mozilla/Logging.h"
     11 #include "mozilla/StaticPrefs_media.h"
     12 #include "mozilla/dom/CanonicalBrowsingContext.h"
     13 #include "nsThreadUtils.h"
     14 
     15 #undef LOG
     16 #define LOG(msg, ...)                        \
     17  MOZ_LOG(gMediaControlLog, LogLevel::Debug, \
     18          ("AudioFocusManager=%p, " msg, this, ##__VA_ARGS__))
     19 
     20 namespace mozilla::dom {
     21 
     22 void AudioFocusManager::RequestAudioFocus(IMediaController* aController) {
     23  MOZ_ASSERT(aController);
     24  if (mOwningFocusControllers.Contains(aController)) {
     25    return;
     26  }
     27  ClearFocusControllersIfNeeded();
     28  LOG("Controller %" PRId64 " grants audio focus", aController->Id());
     29  mOwningFocusControllers.AppendElement(aController);
     30 }
     31 
     32 void AudioFocusManager::RevokeAudioFocus(IMediaController* aController) {
     33  MOZ_ASSERT(aController);
     34  if (!mOwningFocusControllers.Contains(aController)) {
     35    return;
     36  }
     37  LOG("Controller %" PRId64 " loses audio focus", aController->Id());
     38  mOwningFocusControllers.RemoveElement(aController);
     39 }
     40 
     41 void AudioFocusManager::ClearFocusControllersIfNeeded() {
     42  // Enable audio focus management will start the audio competition which is
     43  // only allowing one controller playing at a time.
     44  if (!StaticPrefs::media_audioFocus_management()) {
     45    return;
     46  }
     47 
     48  for (auto& controller : mOwningFocusControllers) {
     49    LOG("Controller %" PRId64 " loses audio focus in audio competitition",
     50        controller->Id());
     51    controller->Stop();
     52  }
     53  mOwningFocusControllers.Clear();
     54 }
     55 
     56 uint32_t AudioFocusManager::GetAudioFocusNums() const {
     57  return mOwningFocusControllers.Length();
     58 }
     59 
     60 }  // namespace mozilla::dom