DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (c68fe15a81fc)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
/* This Source Code Form is subject to the terms of the Mozilla Public
/* 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 "MediaPlaybackStatus.h"


#include "MediaControlUtils.h"

namespace mozilla {
namespace dom {


#undef LOG
#define LOG(msg, ...)                        \
  MOZ_LOG(gMediaControlLog, LogLevel::Debug, \
          ("MediaPlaybackStatus=%p, " msg, this, ##__VA_ARGS__))


void MediaPlaybackStatus::UpdateMediaPlaybackState(uint64_t aContextId,
                                                   MediaPlaybackState aState) {
  LOG("Update playback state '%s' for context %" PRIu64,
      ToMediaPlaybackStateStr(aState), aContextId);
      ToMediaPlaybackStateStr(aState), aContextId);
  MOZ_ASSERT(NS_IsMainThread());

  ContextMediaInfo& info = GetNotNullContextInfo(aContextId);
  if (aState == MediaPlaybackState::eStarted) {
    info.IncreaseControlledMediaNum();
    info.IncreaseControlledMediaNum();
  } else if (aState == MediaPlaybackState::eStopped) {
    info.DecreaseControlledMediaNum();
  } else if (aState == MediaPlaybackState::ePlayed) {
    info.IncreasePlayingMediaNum();
  } else {
    MOZ_ASSERT(aState == MediaPlaybackState::ePaused);
    info.DecreasePlayingMediaNum();
  }
  }

  // The context still has controlled media, we should keep its alive.
  if (info.IsAnyMediaBeingControlled()) {
    return;
  }
  }
  MOZ_ASSERT(!info.IsPlaying());
  MOZ_ASSERT(!info.IsAudible());
  // DO NOT access `info` after this line.
  DestroyContextInfo(aContextId);
}
}

void MediaPlaybackStatus::DestroyContextInfo(uint64_t aContextId) {
  MOZ_ASSERT(NS_IsMainThread());
  LOG("Remove context %" PRIu64, aContextId);
  mContextInfoMap.Remove(aContextId);
  mContextInfoMap.Remove(aContextId);
  // If the removed context is owning the audio focus, we would find another
  // context to take the audio focus if it's possible.
  if (IsContextOwningAudioFocus(aContextId)) {
    ChooseNewContextToOwnAudioFocus();
  }
  }
}

void MediaPlaybackStatus::UpdateMediaAudibleState(uint64_t aContextId,
                                                  MediaAudibleState aState) {
  LOG("Update audible state '%s' for context %" PRIu64,
      ToMediaAudibleStateStr(aState), aContextId);
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(NS_IsMainThread());
  ContextMediaInfo& info = GetNotNullContextInfo(aContextId);
  if (aState == MediaAudibleState::eAudible) {
    info.IncreaseAudibleMediaNum();
  } else {
    MOZ_ASSERT(aState == MediaAudibleState::eInaudible);
    MOZ_ASSERT(aState == MediaAudibleState::eInaudible);
    info.DecreaseAudibleMediaNum();
  }
  }
  if (ShouldRequestAudioFocusForInfo(info)) {
    SetOwningAudioFocusContextId(Some(aContextId));
  } else if (ShouldAbandonAudioFocusForInfo(info)) {
    ChooseNewContextToOwnAudioFocus();
  }
  }
}

bool MediaPlaybackStatus::IsPlaying() const {
  MOZ_ASSERT(NS_IsMainThread());
  for (auto iter = mContextInfoMap.ConstIter(); !iter.Done(); iter.Next()) {
  for (auto iter = mContextInfoMap.ConstIter(); !iter.Done(); iter.Next()) {
    if (iter.Data()->IsPlaying()) {
      return true;
    }
  }
  return false;
  return false;
}

bool MediaPlaybackStatus::IsAudible() const {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(NS_IsMainThread());
  for (auto iter = mContextInfoMap.ConstIter(); !iter.Done(); iter.Next()) {
    if (iter.Data()->IsAudible()) {
      return true;
    }
  }
  }
  return false;
}

bool MediaPlaybackStatus::IsAnyMediaBeingControlled() const {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(NS_IsMainThread());
  for (auto iter = mContextInfoMap.ConstIter(); !iter.Done(); iter.Next()) {
    if (iter.Data()->IsAnyMediaBeingControlled()) {
      return true;
    }
  }
  }
  return false;
}

MediaPlaybackStatus::ContextMediaInfo&
MediaPlaybackStatus::ContextMediaInfo&
MediaPlaybackStatus::GetNotNullContextInfo(uint64_t aContextId) {
  MOZ_ASSERT(NS_IsMainThread());
  if (!mContextInfoMap.Contains(aContextId)) {
    mContextInfoMap.Put(aContextId, MakeUnique<ContextMediaInfo>(aContextId));
  }
  }
  return *(mContextInfoMap.GetValue(aContextId)->get());
}

Maybe<uint64_t> MediaPlaybackStatus::GetAudioFocusOwnerContextId() const {
  return mOwningAudioFocusContextId;
  return mOwningAudioFocusContextId;
}

void MediaPlaybackStatus::ChooseNewContextToOwnAudioFocus() {
  for (auto iter = mContextInfoMap.ConstIter(); !iter.Done(); iter.Next()) {
  for (auto iter = mContextInfoMap.ConstIter(); !iter.Done(); iter.Next()) {
    if (iter.Data()->IsAudible()) {
      SetOwningAudioFocusContextId(Some(iter.Data()->Id()));
      return;
    }
  }
  }
  // No context is audible, so no one should the own audio focus.
  SetOwningAudioFocusContextId(Nothing());
}

void MediaPlaybackStatus::SetOwningAudioFocusContextId(
void MediaPlaybackStatus::SetOwningAudioFocusContextId(
    Maybe<uint64_t>&& aContextId) {
  if (mOwningAudioFocusContextId == aContextId) {
    return;
  }
  mOwningAudioFocusContextId = aContextId;
  mOwningAudioFocusContextId = aContextId;
}

bool MediaPlaybackStatus::ShouldRequestAudioFocusForInfo(
    const ContextMediaInfo& aInfo) const {
  return aInfo.IsAudible() && !IsContextOwningAudioFocus(aInfo.Id());
  return aInfo.IsAudible() && !IsContextOwningAudioFocus(aInfo.Id());
}

bool MediaPlaybackStatus::ShouldAbandonAudioFocusForInfo(
    const ContextMediaInfo& aInfo) const {
  // The owner becomes inaudible and there is other context still playing, so we
  // should switch the audio focus to the audible context.
  // should switch the audio focus to the audible context.
  return !aInfo.IsAudible() && IsContextOwningAudioFocus(aInfo.Id()) &&
         IsAudible();
}

bool MediaPlaybackStatus::IsContextOwningAudioFocus(uint64_t aContextId) const {
bool MediaPlaybackStatus::IsContextOwningAudioFocus(uint64_t aContextId) const {
  return mOwningAudioFocusContextId ? *mOwningAudioFocusContextId == aContextId
                                    : false;
}

}  // namespace dom
}  // namespace mozilla