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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
|
/*
* Copyright (C) 2014-2021 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "PlatformMediaSession.h"
#if ENABLE(VIDEO) || ENABLE(WEB_AUDIO)
#include "HTMLMediaElement.h"
#include "Logging.h"
#include "MediaPlayer.h"
#include "NowPlayingInfo.h"
#include "PlatformMediaSessionManager.h"
#include <wtf/MediaTime.h>
#include <wtf/RuntimeApplicationChecks.h>
#include <wtf/SetForScope.h>
#include <wtf/TZoneMallocInlines.h>
#include <wtf/text/MakeString.h>
namespace WebCore {
WTF_MAKE_TZONE_ALLOCATED_IMPL(PlatformMediaSession);
String convertEnumerationToString(PlatformMediaSession::State state)
{
static const std::array<NeverDestroyed<String>, 5> values {
MAKE_STATIC_STRING_IMPL("Idle"),
MAKE_STATIC_STRING_IMPL("Autoplaying"),
MAKE_STATIC_STRING_IMPL("Playing"),
MAKE_STATIC_STRING_IMPL("Paused"),
MAKE_STATIC_STRING_IMPL("Interrupted"),
};
static_assert(!static_cast<size_t>(PlatformMediaSession::State::Idle), "PlatformMediaSession::Idle is not 0 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::State::Autoplaying) == 1, "PlatformMediaSession::Autoplaying is not 1 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::State::Playing) == 2, "PlatformMediaSession::Playing is not 2 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::State::Paused) == 3, "PlatformMediaSession::Paused is not 3 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::State::Interrupted) == 4, "PlatformMediaSession::Interrupted is not 4 as expected");
ASSERT(static_cast<size_t>(state) < std::size(values));
return values[static_cast<size_t>(state)];
}
String convertEnumerationToString(PlatformMediaSession::InterruptionType type)
{
static const std::array<NeverDestroyed<String>, 8> values {
MAKE_STATIC_STRING_IMPL("NoInterruption"),
MAKE_STATIC_STRING_IMPL("SystemSleep"),
MAKE_STATIC_STRING_IMPL("EnteringBackground"),
MAKE_STATIC_STRING_IMPL("SystemInterruption"),
MAKE_STATIC_STRING_IMPL("SuspendedUnderLock"),
MAKE_STATIC_STRING_IMPL("InvisibleAutoplay"),
MAKE_STATIC_STRING_IMPL("ProcessInactive"),
MAKE_STATIC_STRING_IMPL("PlaybackSuspended"),
};
static_assert(!static_cast<size_t>(PlatformMediaSession::InterruptionType::NoInterruption), "PlatformMediaSession::NoInterruption is not 0 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::InterruptionType::SystemSleep) == 1, "PlatformMediaSession::SystemSleep is not 1 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::InterruptionType::EnteringBackground) == 2, "PlatformMediaSession::EnteringBackground is not 2 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::InterruptionType::SystemInterruption) == 3, "PlatformMediaSession::SystemInterruption is not 3 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::InterruptionType::SuspendedUnderLock) == 4, "PlatformMediaSession::SuspendedUnderLock is not 4 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::InterruptionType::InvisibleAutoplay) == 5, "PlatformMediaSession::InvisibleAutoplay is not 5 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::InterruptionType::ProcessInactive) == 6, "PlatformMediaSession::ProcessInactive is not 6 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::InterruptionType::PlaybackSuspended) == 7, "PlatformMediaSession::PlaybackSuspended is not 7 as expected");
ASSERT(static_cast<size_t>(type) < std::size(values));
return values[static_cast<size_t>(type)];
}
String convertEnumerationToString(PlatformMediaSession::MediaType mediaType)
{
static const std::array<NeverDestroyed<String>, 5> values {
MAKE_STATIC_STRING_IMPL("None"),
MAKE_STATIC_STRING_IMPL("Video"),
MAKE_STATIC_STRING_IMPL("VideoAudio"),
MAKE_STATIC_STRING_IMPL("Audio"),
MAKE_STATIC_STRING_IMPL("WebAudio"),
};
static_assert(!static_cast<size_t>(PlatformMediaSession::MediaType::None), "PlatformMediaSession::MediaType::None is not 0 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::MediaType::Video) == 1, "PlatformMediaSession::MediaType::Video is not 1 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::MediaType::VideoAudio) == 2, "PlatformMediaSession::MediaType::VideoAudio is not 2 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::MediaType::Audio) == 3, "PlatformMediaSession::MediaType::Audio is not 3 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::MediaType::WebAudio) == 4, "PlatformMediaSession::MediaType::WebAudio is not 4 as expected");
ASSERT(static_cast<size_t>(mediaType) < std::size(values));
return values[static_cast<size_t>(mediaType)];
}
String convertEnumerationToString(PlatformMediaSession::RemoteControlCommandType command)
{
static const std::array<NeverDestroyed<String>, 16> values {
MAKE_STATIC_STRING_IMPL("NoCommand"),
MAKE_STATIC_STRING_IMPL("PlayCommand"),
MAKE_STATIC_STRING_IMPL("PauseCommand"),
MAKE_STATIC_STRING_IMPL("StopCommand"),
MAKE_STATIC_STRING_IMPL("TogglePlayPauseCommand"),
MAKE_STATIC_STRING_IMPL("BeginSeekingBackwardCommand"),
MAKE_STATIC_STRING_IMPL("EndSeekingBackwardCommand"),
MAKE_STATIC_STRING_IMPL("BeginSeekingForwardCommand"),
MAKE_STATIC_STRING_IMPL("EndSeekingForwardCommand"),
MAKE_STATIC_STRING_IMPL("SeekToPlaybackPositionCommand"),
MAKE_STATIC_STRING_IMPL("SkipForwardCommand"),
MAKE_STATIC_STRING_IMPL("SkipBackwardCommand"),
MAKE_STATIC_STRING_IMPL("NextTrackCommand"),
MAKE_STATIC_STRING_IMPL("PreviousTrackCommand"),
MAKE_STATIC_STRING_IMPL("BeginScrubbingCommand"),
MAKE_STATIC_STRING_IMPL("EndScrubbingCommand"),
};
static_assert(!static_cast<size_t>(PlatformMediaSession::RemoteControlCommandType::NoCommand), "PlatformMediaSession::RemoteControlCommandType::NoCommand is not 0 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::RemoteControlCommandType::PlayCommand) == 1, "PlatformMediaSession::RemoteControlCommandType::PlayCommand is not 1 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::RemoteControlCommandType::PauseCommand) == 2, "PlatformMediaSession::RemoteControlCommandType::PauseCommand is not 2 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::RemoteControlCommandType::StopCommand) == 3, "PlatformMediaSession::RemoteControlCommandType::StopCommand is not 3 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::RemoteControlCommandType::TogglePlayPauseCommand) == 4, "PlatformMediaSession::RemoteControlCommandType::TogglePlayPauseCommand is not 4 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::RemoteControlCommandType::BeginSeekingBackwardCommand) == 5, "PlatformMediaSession::RemoteControlCommandType::BeginSeekingBackwardCommand is not 5 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::RemoteControlCommandType::EndSeekingBackwardCommand) == 6, "PlatformMediaSession::RemoteControlCommandType::EndSeekingBackwardCommand is not 6 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::RemoteControlCommandType::BeginSeekingForwardCommand) == 7, "PlatformMediaSession::RemoteControlCommandType::BeginSeekingForwardCommand is not 7 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::RemoteControlCommandType::EndSeekingForwardCommand) == 8, "PlatformMediaSession::RemoteControlCommandType::EndSeekingForwardCommand is not 8 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::RemoteControlCommandType::SeekToPlaybackPositionCommand) == 9, "PlatformMediaSession::RemoteControlCommandType::SeekToPlaybackPositionCommand is not 9 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::RemoteControlCommandType::SkipForwardCommand) == 10, "PlatformMediaSession::RemoteControlCommandType::SkipForwardCommand is not 10 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::RemoteControlCommandType::SkipBackwardCommand) == 11, "PlatformMediaSession::RemoteControlCommandType::SkipBackwardCommand is not 11 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::RemoteControlCommandType::NextTrackCommand) == 12, "PlatformMediaSession::RemoteControlCommandType::NextTrackCommand is not 12 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::RemoteControlCommandType::PreviousTrackCommand) == 13, "PlatformMediaSession::RemoteControlCommandType::PreviousTrackCommand is not 13 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::RemoteControlCommandType::BeginScrubbingCommand) == 14, "PlatformMediaSession::RemoteControlCommandType::BeginScrubbingCommand is not 14 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::RemoteControlCommandType::EndScrubbingCommand) == 15, "PlatformMediaSession::RemoteControlCommandType::EndScrubbingCommand is not 15 as expected");
ASSERT(static_cast<size_t>(command) < std::size(values));
return values[static_cast<size_t>(command)];
}
PlatformMediaSession::~PlatformMediaSession()
{
setActive(false);
}
void PlatformMediaSession::setActive(bool active)
{
if (m_active == active)
return;
m_active = active;
if (RefPtr manager = sessionManager()) {
if (m_active)
manager->addSession(*this);
else
manager->removeSession(*this);
}
}
void PlatformMediaSession::setState(State state)
{
if (state == m_state)
return;
ALWAYS_LOG(LOGIDENTIFIER, state);
m_state = state;
if (m_state == State::Playing && canProduceAudio())
setHasPlayedAudiblySinceLastInterruption(true);
if (RefPtr manager = sessionManager())
manager->sessionStateChanged(*this);
}
size_t PlatformMediaSession::activeInterruptionCount() const
{
size_t count = 0;
for (auto& interruption : m_interruptionStack) {
if (!interruption.ignored)
count++;
}
return count;
}
PlatformMediaSession::InterruptionType PlatformMediaSession::interruptionType() const
{
if (!m_interruptionStack.size())
return InterruptionType::NoInterruption;
return m_interruptionStack.last().type;
}
void PlatformMediaSession::beginInterruption(InterruptionType type)
{
ASSERT(type != InterruptionType::NoInterruption);
ALWAYS_LOG(LOGIDENTIFIER, "state = ", m_state, ", interruption count = ", m_interruptionStack.size(), ", type = ", type);
if (activeInterruptionCount()) {
m_interruptionStack.append({ type, true });
return;
}
if (client().shouldOverrideBackgroundPlaybackRestriction(type)) {
ALWAYS_LOG(LOGIDENTIFIER, "returning early because client says to override interruption");
m_interruptionStack.append({ type, true });
return;
}
m_interruptionStack.append({ type, false });
m_stateToRestore = state();
m_notifyingClient = true;
setState(State::Interrupted);
client().suspendPlayback();
m_notifyingClient = false;
}
void PlatformMediaSession::endInterruption(OptionSet<EndInterruptionFlags> flags)
{
if (m_interruptionStack.isEmpty()) {
ALWAYS_LOG(LOGIDENTIFIER, "!! ignoring spurious interruption end !!");
return;
}
auto interruption = m_interruptionStack.takeLast();
ALWAYS_LOG(LOGIDENTIFIER, "flags = ", (int)flags.toRaw(), ", interruption count = ", m_interruptionStack.size(), " type = ", interruptionType());
if (activeInterruptionCount() || interruption.ignored)
return;
ALWAYS_LOG(LOGIDENTIFIER, "restoring state ", m_stateToRestore);
State stateToRestore = m_stateToRestore;
m_stateToRestore = State::Idle;
setState(stateToRestore);
if (stateToRestore == State::Autoplaying)
client().resumeAutoplaying();
bool shouldResume = flags.contains(EndInterruptionFlags::MayResumePlaying) && stateToRestore == State::Playing;
client().mayResumePlayback(shouldResume);
}
void PlatformMediaSession::clientWillBeginAutoplaying()
{
if (m_notifyingClient)
return;
ALWAYS_LOG(LOGIDENTIFIER, "state = ", m_state);
if (state() == State::Interrupted) {
m_stateToRestore = State::Autoplaying;
ALWAYS_LOG(LOGIDENTIFIER, " setting stateToRestore to \"Autoplaying\"");
return;
}
setState(State::Autoplaying);
}
bool PlatformMediaSession::clientWillBeginPlayback()
{
if (m_notifyingClient)
return true;
ALWAYS_LOG(LOGIDENTIFIER, "state = ", m_state);
SetForScope preparingToPlay(m_preparingToPlay, true);
if (RefPtr manager = sessionManager(); manager && !manager->sessionWillBeginPlayback(*this)) {
if (state() == State::Interrupted)
m_stateToRestore = State::Playing;
return false;
}
m_stateToRestore = State::Playing;
setState(State::Playing);
return true;
}
bool PlatformMediaSession::processClientWillPausePlayback(DelayCallingUpdateNowPlaying shouldDelayCallingUpdateNowPlaying)
{
if (m_notifyingClient)
return true;
ALWAYS_LOG(LOGIDENTIFIER, "state = ", m_state);
if (state() == State::Interrupted) {
m_stateToRestore = State::Paused;
ALWAYS_LOG(LOGIDENTIFIER, " setting stateToRestore to \"Paused\"");
return false;
}
setState(State::Paused);
if (RefPtr manager = sessionManager())
manager->sessionWillEndPlayback(*this, shouldDelayCallingUpdateNowPlaying);
return true;
}
bool PlatformMediaSession::clientWillPausePlayback()
{
ALWAYS_LOG(LOGIDENTIFIER);
return processClientWillPausePlayback(DelayCallingUpdateNowPlaying::No);
}
void PlatformMediaSession::clientWillBeDOMSuspended()
{
ALWAYS_LOG(LOGIDENTIFIER);
processClientWillPausePlayback(DelayCallingUpdateNowPlaying::Yes);
}
void PlatformMediaSession::pauseSession()
{
ALWAYS_LOG(LOGIDENTIFIER);
if (state() == State::Interrupted)
m_stateToRestore = State::Paused;
client().suspendPlayback();
}
void PlatformMediaSession::stopSession()
{
ALWAYS_LOG(LOGIDENTIFIER);
client().suspendPlayback();
if (RefPtr manager = sessionManager())
manager->removeSession(*this);
}
void PlatformMediaSession::didReceiveRemoteControlCommand(RemoteControlCommandType command, const PlatformMediaSession::RemoteCommandArgument& argument)
{
ALWAYS_LOG(LOGIDENTIFIER, command);
client().didReceiveRemoteControlCommand(command, argument);
}
void PlatformMediaSession::isPlayingToWirelessPlaybackTargetChanged(bool isWireless)
{
if (isWireless == m_isPlayingToWirelessPlaybackTarget)
return;
m_isPlayingToWirelessPlaybackTarget = isWireless;
if (RefPtr manager = sessionManager())
manager->sessionIsPlayingToWirelessPlaybackTargetChanged(*this);
}
bool PlatformMediaSession::blockedBySystemInterruption() const
{
return activeInterruptionCount() && interruptionType() == PlatformMediaSession::InterruptionType::SystemInterruption;
}
bool PlatformMediaSession::activeAudioSessionRequired() const
{
if (mediaType() == PlatformMediaSession::MediaType::None)
return false;
if (state() != PlatformMediaSession::State::Playing)
return false;
return canProduceAudio();
}
void PlatformMediaSession::canProduceAudioChanged()
{
if (RefPtr manager = sessionManager())
manager->sessionCanProduceAudioChanged();
}
void PlatformMediaSession::clientCharacteristicsChanged(bool positionChanged)
{
if (RefPtr manager = sessionManager())
manager->clientCharacteristicsChanged(*this, positionChanged);
}
static inline bool isPlayingAudio(PlatformMediaSession::MediaType mediaType)
{
#if ENABLE(VIDEO)
return mediaType == MediaElementSession::MediaType::VideoAudio || mediaType == MediaElementSession::MediaType::Audio;
#else
UNUSED_PARAM(mediaType);
return false;
#endif
}
bool PlatformMediaSession::canPlayConcurrently(const PlatformMediaSessionInterface& otherSession) const
{
auto mediaType = this->mediaType();
auto otherMediaType = otherSession.mediaType();
if (otherMediaType != mediaType && (!isPlayingAudio(mediaType) || !isPlayingAudio(otherMediaType)))
return true;
auto groupID = client().mediaSessionGroupIdentifier();
auto otherGroupID = otherSession.client().mediaSessionGroupIdentifier();
if (!groupID || !otherGroupID || groupID != otherGroupID)
return false;
return client().hasMediaStreamSource() || otherSession.client().hasMediaStreamSource();
}
WeakPtr<PlatformMediaSessionInterface> PlatformMediaSession::selectBestMediaSession(const Vector<WeakPtr<PlatformMediaSessionInterface>>& sessions, PlaybackControlsPurpose purpose)
{
return client().selectBestMediaSession(sessions, purpose);
}
void PlatformMediaSession::setActiveNowPlayingSession(bool isActiveNowPlayingSession)
{
if (isActiveNowPlayingSession == m_isActiveNowPlayingSession)
return;
m_isActiveNowPlayingSession = isActiveNowPlayingSession;
}
#if !RELEASE_LOG_DISABLED
const Logger& PlatformMediaSession::logger() const
{
return client().logger();
}
uint64_t PlatformMediaSession::logIdentifier() const
{
return client().logIdentifier();
}
WTFLogChannel& PlatformMediaSession::logChannel() const
{
return LogMedia;
}
String PlatformMediaSession::description() const
{
return makeString(convertEnumerationToString(mediaType()), ", "_s, convertEnumerationToString(state()));
}
#endif
} // namespace WebCore
#endif // ENABLE(VIDEO) || ENABLE(WEB_AUDIO)
|