Skip to content

Commit

Permalink
[visionOS] YouTube audio in background tab stops after looking away f…
Browse files Browse the repository at this point in the history
…rom Safari for one minute

https://bugs.webkit.org/show_bug.cgi?id=261050
rdar://113459873

Reviewed by Tim Horton.

On visionOS, scenes are backgrounded after they are out of the user's field of
view for one minute. This is similar to iPadOS, where the device is locked when
looking away from the screen for two minutes (configurable via Settings).
However, unlike iPadOS, visionOS has no device-level idle state, as the display
is always on when the device is in use, and always off when it is not.

The only way to prevent the aforementioned scene backgrounding is to disable
the application-level idle timer. This timer is currently only disabled when the
document containing a playing media element is visible. If the media element is
playing and in a hidden document (as is the case in a background tab), a system
sleep assertion is taken instead. This assertion has no meaning as there is no
"user-idle" state while the device is in use. Consequently, the idle timer is
active, the scene gets backgrounded after one minute, and playback is paused
by WebKit as the notification is received.

To fix, ensure the idle timer remains disabled when media is playing in a
hidden document.

* Source/WTF/wtf/PlatformHave.h:
* Source/WebCore/html/HTMLMediaElement.cpp:
(WebCore::HTMLMediaElement::shouldDisableSleep const):
* Source/WebCore/html/HTMLMediaElement.h:

Drive-by: Specify the smallest underlying type for the `SleepType` enum class.
Canonical link: https://commits.webkit.org/267615@main
  • Loading branch information
pxlcoder committed Sep 5, 2023
1 parent dae026f commit f4af2e2
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Source/WTF/wtf/PlatformHave.h
Original file line number Diff line number Diff line change
Expand Up @@ -1508,3 +1508,7 @@
#if PLATFORM(VISION)
#define HAVE_APPLE_THERMAL_MITIGATION_SUPPORT 1
#endif

#if !PLATFORM(VISION)
#define HAVE_IDLE_SLEEP_STATE 1
#endif
2 changes: 2 additions & 0 deletions Source/WebCore/html/HTMLMediaElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7634,8 +7634,10 @@ HTMLMediaElement::SleepType HTMLMediaElement::shouldDisableSleep() const
if (shouldBeAbleToSleep)
return SleepType::None;

#if HAVE(IDLE_SLEEP_STATE)
if (m_elementIsHidden)
return SleepType::System;
#endif

return SleepType::Display;
}
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/html/HTMLMediaElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ class HTMLMediaElement
bool isLiveStream() const override { return movieLoadType() == MovieLoadType::LiveStream; }

void updateSleepDisabling();
enum class SleepType { None, Display, System };
enum class SleepType : uint8_t { None, Display, System };
SleepType shouldDisableSleep() const;

DOMWrapperWorld& ensureIsolatedWorld();
Expand Down

0 comments on commit f4af2e2

Please sign in to comment.