Skip to content

Commit

Permalink
Port PlatformMediaSession enums to the new IPC serialization format
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=264807

Reviewed by Chris Dumez.

Port the enums to the serialization format, and the flags enum
to OptionSet.

* Source/WebCore/Modules/webaudio/AudioContext.cpp:
(WebCore::AudioContext::resumeRendering):
(WebCore::AudioContext::suspend):
(WebCore::AudioContext::resume):
(WebCore::AudioContext::suspendPlayback):
(WebCore::AudioContext::defaultDestinationWillBecomeConnected):
* Source/WebCore/html/HTMLMediaElement.cpp:
(WebCore::HTMLMediaElement::pausedForUserInteraction const):
(WebCore::HTMLMediaElement::virtualHasPendingActivity const):
(WebCore::HTMLMediaElement::shouldOverrideBackgroundPlaybackRestriction const):
(WebCore::HTMLMediaElement::updateShouldAutoplay):
(WebCore::HTMLMediaElement::playbackControlsManagerBehaviorRestrictionsTimerFired):
* Source/WebCore/html/MediaElementSession.cpp:
(WebCore::MediaElementSession::visibilityChanged):
(WebCore::MediaElementSession::clientDataBufferingTimerFired):
(WebCore::MediaElementSession::preferredBufferingPolicy const):
(WebCore::MediaElementSession::nowPlayingInfo const):
(WebCore::MediaElementSession::updateMediaUsageIfChanged):
* Source/WebCore/platform/audio/PlatformMediaSession.cpp:
(WebCore::convertEnumerationToString):
(WebCore::PlatformMediaSession::beginInterruption):
(WebCore::PlatformMediaSession::endInterruption):
(WebCore::PlatformMediaSession::clientWillBeginAutoplaying):
(WebCore::PlatformMediaSession::clientWillBeginPlayback):
(WebCore::PlatformMediaSession::processClientWillPausePlayback):
(WebCore::PlatformMediaSession::pauseSession):
* Source/WebCore/platform/audio/PlatformMediaSession.h:
* Source/WebCore/platform/audio/PlatformMediaSession.serialization.in:
* Source/WebCore/platform/audio/PlatformMediaSessionManager.cpp:
(WebCore::PlatformMediaSessionManager::sessionWillBeginPlayback):
(WebCore::PlatformMediaSessionManager::sessionWillEndPlayback):
(WebCore::PlatformMediaSessionManager::sessionStateChanged):
(WebCore::PlatformMediaSessionManager::applicationWillBecomeInactive):
(WebCore::PlatformMediaSessionManager::applicationDidBecomeActive):
(WebCore::PlatformMediaSessionManager::applicationDidEnterBackground):
(WebCore::PlatformMediaSessionManager::applicationWillEnterForeground):
(WebCore::PlatformMediaSessionManager::sessionIsPlayingToWirelessPlaybackTargetChanged):
(WebCore::PlatformMediaSessionManager::processSystemWillSleep):
(WebCore::PlatformMediaSessionManager::processSystemDidWake):
(WebCore::PlatformMediaSessionManager::mediaPlaybackIsPaused):
(WebCore::PlatformMediaSessionManager::suspendAllMediaPlaybackForGroup):
(WebCore::PlatformMediaSessionManager::resumeAllMediaPlaybackForGroup):
* Source/WebCore/platform/audio/cocoa/MediaSessionManagerCocoa.mm:
(WebCore::MediaSessionManagerCocoa::sessionWillEndPlayback):
* Source/WebCore/testing/Internals.cpp:
(WebCore::Internals::beginMediaSessionInterruption):
(WebCore::Internals::endMediaSessionInterruption):

Canonical link: https://commits.webkit.org/270719@main
  • Loading branch information
csaavedra committed Nov 14, 2023
1 parent 0150f2f commit 362150b
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 165 deletions.
12 changes: 6 additions & 6 deletions Source/WebCore/Modules/webaudio/AudioContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ void AudioContext::resumeRendering(DOMPromiseDeferred<void>&& promise)
// Since we update the state asynchronously, we may have been interrupted after the
// call to resume() and before this lambda runs. In this case, we don't want to
// reset the state to running.
bool interrupted = m_mediaSession->state() == PlatformMediaSession::Interrupted;
bool interrupted = m_mediaSession->state() == PlatformMediaSession::State::Interrupted;
setState(interrupted ? State::Interrupted : State::Running);
if (interrupted)
addReaction(State::Running, WTFMove(promise));
Expand Down Expand Up @@ -413,7 +413,7 @@ void AudioContext::suspend(ReasonForSuspension)
if (isClosed() || m_wasSuspendedByScript)
return;

m_mediaSession->beginInterruption(PlatformMediaSession::PlaybackSuspended);
m_mediaSession->beginInterruption(PlatformMediaSession::InterruptionType::PlaybackSuspended);
document()->updateIsPlayingMedia();
}

Expand All @@ -422,7 +422,7 @@ void AudioContext::resume()
if (isClosed() || m_wasSuspendedByScript)
return;

m_mediaSession->endInterruption(PlatformMediaSession::MayResumePlaying);
m_mediaSession->endInterruption(PlatformMediaSession::EndInterruptionFlags::MayResumePlaying);
document()->updateIsPlayingMedia();
}

Expand All @@ -442,7 +442,7 @@ void AudioContext::suspendPlayback()
if (exception)
return;

bool interrupted = m_mediaSession->state() == PlatformMediaSession::Interrupted;
bool interrupted = m_mediaSession->state() == PlatformMediaSession::State::Interrupted;
setState(interrupted ? State::Interrupted : State::Suspended);
});
}
Expand Down Expand Up @@ -498,14 +498,14 @@ bool AudioContext::shouldOverrideBackgroundPlaybackRestriction(PlatformMediaSess
void AudioContext::defaultDestinationWillBecomeConnected()
{
// We might need to interrupt if we previously overrode a background interruption.
if (!PlatformMediaSessionManager::sharedManager().isApplicationInBackground() || m_mediaSession->state() == PlatformMediaSession::Interrupted)
if (!PlatformMediaSessionManager::sharedManager().isApplicationInBackground() || m_mediaSession->state() == PlatformMediaSession::State::Interrupted)
return;

// We end the overriden interruption (if any) to get the right count of interruptions and start a new interruption.
m_mediaSession->endInterruption(PlatformMediaSession::EndInterruptionFlags::NoFlags);

m_canOverrideBackgroundPlaybackRestriction = false;
m_mediaSession->beginInterruption(PlatformMediaSession::EnteringBackground);
m_mediaSession->beginInterruption(PlatformMediaSession::InterruptionType::EnteringBackground);
m_canOverrideBackgroundPlaybackRestriction = true;
}

Expand Down
22 changes: 11 additions & 11 deletions Source/WebCore/html/HTMLMediaElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5916,7 +5916,7 @@ bool HTMLMediaElement::stoppedDueToErrors() const

bool HTMLMediaElement::pausedForUserInteraction() const
{
if (mediaSession().state() == PlatformMediaSession::Interrupted)
if (mediaSession().state() == PlatformMediaSession::State::Interrupted)
return true;

return false;
Expand Down Expand Up @@ -6406,12 +6406,12 @@ bool HTMLMediaElement::virtualHasPendingActivity() const
if (!mediaSession)
return false;

if (mediaSession->state() != PlatformMediaSession::Interrupted)
if (mediaSession->state() != PlatformMediaSession::State::Interrupted)
return false;

auto stateToRestore = mediaSession->stateToRestore();
return stateToRestore == PlatformMediaSession::Autoplaying
|| stateToRestore == PlatformMediaSession::Playing;
return stateToRestore == PlatformMediaSession::State::Autoplaying
|| stateToRestore == PlatformMediaSession::State::Playing;
}();

// * It is playing, and is audible to the user:
Expand Down Expand Up @@ -8501,7 +8501,7 @@ bool HTMLMediaElement::supportsSeeking() const

bool HTMLMediaElement::shouldOverrideBackgroundPlaybackRestriction(PlatformMediaSession::InterruptionType type) const
{
if (type == PlatformMediaSession::EnteringBackground) {
if (type == PlatformMediaSession::InterruptionType::EnteringBackground) {
if (isPlayingToExternalTarget()) {
INFO_LOG(LOGIDENTIFIER, "returning true because isPlayingToExternalTarget() is true");
return true;
Expand All @@ -8522,7 +8522,7 @@ bool HTMLMediaElement::shouldOverrideBackgroundPlaybackRestriction(PlatformMedia
return true;
}
#endif
} else if (type == PlatformMediaSession::SuspendedUnderLock) {
} else if (type == PlatformMediaSession::InterruptionType::SuspendedUnderLock) {
if (isPlayingToExternalTarget()) {
INFO_LOG(LOGIDENTIFIER, "returning true because isPlayingToExternalTarget() is true");
return true;
Expand Down Expand Up @@ -8881,24 +8881,24 @@ void HTMLMediaElement::updateShouldAutoplay()
if (canAutoplay) {
if (m_wasInterruptedForInvisibleAutoplay) {
m_wasInterruptedForInvisibleAutoplay = false;
mediaSession().endInterruption(PlatformMediaSession::MayResumePlaying);
mediaSession().endInterruption(PlatformMediaSession::EndInterruptionFlags::MayResumePlaying);
return;
}
if (!isPlaying())
resumeAutoplaying();
return;
}

if (mediaSession().state() == PlatformMediaSession::Interrupted)
if (mediaSession().state() == PlatformMediaSession::State::Interrupted)
return;

if (m_wasInterruptedForInvisibleAutoplay) {
m_wasInterruptedForInvisibleAutoplay = false;
mediaSession().endInterruption(PlatformMediaSession::NoFlags);
mediaSession().endInterruption(PlatformMediaSession::EndInterruptionFlags::NoFlags);
}

m_wasInterruptedForInvisibleAutoplay = true;
mediaSession().beginInterruption(PlatformMediaSession::InvisibleAutoplay);
mediaSession().beginInterruption(PlatformMediaSession::InterruptionType::InvisibleAutoplay);
}

void HTMLMediaElement::updateShouldPlay()
Expand Down Expand Up @@ -8941,7 +8941,7 @@ void HTMLMediaElement::playbackControlsManagerBehaviorRestrictionsTimerFired()

queueCancellableTaskKeepingObjectAlive(*this, TaskSource::MediaElement, m_playbackControlsManagerBehaviorRestrictionsTaskCancellationGroup, [this] () {
auto& mediaElementSession = mediaSession();
if (isPlaying() || mediaElementSession.state() == PlatformMediaSession::Autoplaying || mediaElementSession.state() == PlatformMediaSession::Playing)
if (isPlaying() || mediaElementSession.state() == PlatformMediaSession::State::Autoplaying || mediaElementSession.state() == PlatformMediaSession::State::Playing)
return;

mediaElementSession.addBehaviorRestriction(MediaElementSession::RequirePlaybackToControlControlsManager);
Expand Down
16 changes: 8 additions & 8 deletions Source/WebCore/html/MediaElementSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,21 +275,21 @@ void MediaElementSession::visibilityChanged()
if (!isPlayingAudio) {
if (elementIsHidden) {
ALWAYS_LOG(LOGIDENTIFIER, "Suspending silent playback after page visibility: hidden");
beginInterruption(PlatformMediaSession::EnteringBackground);
beginInterruption(PlatformMediaSession::InterruptionType::EnteringBackground);
} else {
ALWAYS_LOG(LOGIDENTIFIER, "Resuming silent playback after page visibility: showing");
endInterruption(PlatformMediaSession::MayResumePlaying);
endInterruption(PlatformMediaSession::EndInterruptionFlags::MayResumePlaying);
}
return;
}

if (hasBehaviorRestriction(RequirePageVisibilityToPlayAudio)) {
if (elementIsHidden) {
ALWAYS_LOG(LOGIDENTIFIER, "Suspending audible playback after page visibility: hidden");
beginInterruption(PlatformMediaSession::EnteringBackground);
beginInterruption(PlatformMediaSession::InterruptionType::EnteringBackground);
} else {
ALWAYS_LOG(LOGIDENTIFIER, "Resuming audible playback after page visibility: showing");
endInterruption(PlatformMediaSession::MayResumePlaying);
endInterruption(PlatformMediaSession::EndInterruptionFlags::MayResumePlaying);
}
}
}
Expand Down Expand Up @@ -324,7 +324,7 @@ void MediaElementSession::clientDataBufferingTimerFired()

updateClientDataBuffering();

if (state() != Playing || !m_element.elementIsHidden())
if (state() != PlatformMediaSession::State::Playing || !m_element.elementIsHidden())
return;

PlatformMediaSessionManager::SessionRestrictions restrictions = PlatformMediaSessionManager::sharedManager().restrictions(mediaType());
Expand Down Expand Up @@ -500,7 +500,7 @@ MediaPlayer::BufferingPolicy MediaElementSession::preferredBufferingPolicy() con
if (bufferingSuspended())
return MediaPlayer::BufferingPolicy::LimitReadAhead;

if (state() == PlatformMediaSession::Playing)
if (state() == PlatformMediaSession::State::Playing)
return MediaPlayer::BufferingPolicy::Default;

if (shouldOverrideBackgroundLoadingRestriction())
Expand Down Expand Up @@ -1281,7 +1281,7 @@ std::optional<NowPlayingInfo> MediaElementSession::nowPlayingInfo() const
#endif

bool allowsNowPlayingControlsVisibility = page && !page->isVisibleAndActive();
bool isPlaying = state() == PlatformMediaSession::Playing;
bool isPlaying = state() == PlatformMediaSession::State::Playing;

bool supportsSeeking = m_element.supportsSeeking();
double rate = 1.0;
Expand Down Expand Up @@ -1347,7 +1347,7 @@ void MediaElementSession::updateMediaUsageIfChanged()
MediaUsageInfo usage = {
m_element.currentSrc(),
m_element.hasSource(),
state() == PlatformMediaSession::Playing,
state() == PlatformMediaSession::State::Playing,
canShowControlsManager(PlaybackControlsPurpose::ControlsManager),
!page->isVisibleAndActive(),
m_element.isSuspended(),
Expand Down
68 changes: 34 additions & 34 deletions Source/WebCore/platform/audio/PlatformMediaSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ String convertEnumerationToString(PlatformMediaSession::State state)
MAKE_STATIC_STRING_IMPL("Paused"),
MAKE_STATIC_STRING_IMPL("Interrupted"),
};
static_assert(!static_cast<size_t>(PlatformMediaSession::Idle), "PlatformMediaSession::Idle is not 0 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::Autoplaying) == 1, "PlatformMediaSession::Autoplaying is not 1 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::Playing) == 2, "PlatformMediaSession::Playing is not 2 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::Paused) == 3, "PlatformMediaSession::Paused is not 3 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::Interrupted) == 4, "PlatformMediaSession::Interrupted is not 4 as expected");
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)];
}
Expand All @@ -67,14 +67,14 @@ String convertEnumerationToString(PlatformMediaSession::InterruptionType type)
MAKE_STATIC_STRING_IMPL("ProcessInactive"),
MAKE_STATIC_STRING_IMPL("PlaybackSuspended"),
};
static_assert(!static_cast<size_t>(PlatformMediaSession::NoInterruption), "PlatformMediaSession::NoInterruption is not 0 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::SystemSleep) == 1, "PlatformMediaSession::SystemSleep is not 1 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::EnteringBackground) == 2, "PlatformMediaSession::EnteringBackground is not 2 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::SystemInterruption) == 3, "PlatformMediaSession::SystemInterruption is not 3 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::SuspendedUnderLock) == 4, "PlatformMediaSession::SuspendedUnderLock is not 4 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::InvisibleAutoplay) == 5, "PlatformMediaSession::InvisibleAutoplay is not 5 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::ProcessInactive) == 6, "PlatformMediaSession::ProcessInactive is not 6 as expected");
static_assert(static_cast<size_t>(PlatformMediaSession::PlaybackSuspended) == 7, "PlatformMediaSession::PlaybackSuspended is not 7 as expected");
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)];
}
Expand Down Expand Up @@ -170,7 +170,7 @@ void PlatformMediaSession::beginInterruption(InterruptionType type)

// When interruptions are overridden, m_interruptionType doesn't get set.
// Give nested interruptions a chance when the previous interruptions were overridden.
if (++m_interruptionCount > 1 && m_interruptionType != NoInterruption)
if (++m_interruptionCount > 1 && m_interruptionType != InterruptionType::NoInterruption)
return;

if (client().shouldOverrideBackgroundPlaybackRestriction(type)) {
Expand All @@ -180,15 +180,15 @@ void PlatformMediaSession::beginInterruption(InterruptionType type)

m_stateToRestore = state();
m_notifyingClient = true;
setState(Interrupted);
setState(State::Interrupted);
m_interruptionType = type;
client().suspendPlayback();
m_notifyingClient = false;
}

void PlatformMediaSession::endInterruption(EndInterruptionFlags flags)
void PlatformMediaSession::endInterruption(OptionSet<EndInterruptionFlags> flags)
{
ALWAYS_LOG(LOGIDENTIFIER, "flags = ", (int)flags, ", stateToRestore = ", m_stateToRestore, ", interruption count = ", m_interruptionCount);
ALWAYS_LOG(LOGIDENTIFIER, "flags = ", (int)flags.toRaw(), ", stateToRestore = ", m_stateToRestore, ", interruption count = ", m_interruptionCount);

if (!m_interruptionCount) {
ALWAYS_LOG(LOGIDENTIFIER, "!! ignoring spurious interruption end !!");
Expand All @@ -198,18 +198,18 @@ void PlatformMediaSession::endInterruption(EndInterruptionFlags flags)
if (--m_interruptionCount)
return;

if (m_interruptionType == NoInterruption)
if (m_interruptionType == InterruptionType::NoInterruption)
return;

State stateToRestore = m_stateToRestore;
m_stateToRestore = Idle;
m_interruptionType = NoInterruption;
m_stateToRestore = State::Idle;
m_interruptionType = InterruptionType::NoInterruption;
setState(stateToRestore);

if (stateToRestore == Autoplaying)
if (stateToRestore == State::Autoplaying)
client().resumeAutoplaying();

bool shouldResume = flags & MayResumePlaying && stateToRestore == Playing;
bool shouldResume = flags.contains(EndInterruptionFlags::MayResumePlaying) && stateToRestore == State::Playing;
client().mayResumePlayback(shouldResume);
}

Expand All @@ -219,13 +219,13 @@ void PlatformMediaSession::clientWillBeginAutoplaying()
return;

ALWAYS_LOG(LOGIDENTIFIER, "state = ", m_state);
if (state() == Interrupted) {
m_stateToRestore = Autoplaying;
if (state() == State::Interrupted) {
m_stateToRestore = State::Autoplaying;
ALWAYS_LOG(LOGIDENTIFIER, " setting stateToRestore to \"Autoplaying\"");
return;
}

setState(Autoplaying);
setState(State::Autoplaying);
}

bool PlatformMediaSession::clientWillBeginPlayback()
Expand All @@ -238,13 +238,13 @@ bool PlatformMediaSession::clientWillBeginPlayback()
SetForScope preparingToPlay(m_preparingToPlay, true);

if (!PlatformMediaSessionManager::sharedManager().sessionWillBeginPlayback(*this)) {
if (state() == Interrupted)
m_stateToRestore = Playing;
if (state() == State::Interrupted)
m_stateToRestore = State::Playing;
return false;
}

m_stateToRestore = Playing;
setState(Playing);
m_stateToRestore = State::Playing;
setState(State::Playing);
return true;
}

Expand All @@ -254,13 +254,13 @@ bool PlatformMediaSession::processClientWillPausePlayback(DelayCallingUpdateNowP
return true;

ALWAYS_LOG(LOGIDENTIFIER, "state = ", m_state);
if (state() == Interrupted) {
m_stateToRestore = Paused;
if (state() == State::Interrupted) {
m_stateToRestore = State::Paused;
ALWAYS_LOG(LOGIDENTIFIER, " setting stateToRestore to \"Paused\"");
return false;
}

setState(Paused);
setState(State::Paused);
PlatformMediaSessionManager::sharedManager().sessionWillEndPlayback(*this, shouldDelayCallingUpdateNowPlaying);
return true;
}
Expand All @@ -281,8 +281,8 @@ void PlatformMediaSession::pauseSession()
{
ALWAYS_LOG(LOGIDENTIFIER);

if (state() == Interrupted)
m_stateToRestore = Paused;
if (state() == State::Interrupted)
m_stateToRestore = State::Paused;

m_client.suspendPlayback();
}
Expand Down
Loading

0 comments on commit 362150b

Please sign in to comment.