blob: a00f130b2ffccbcaade0d2d8872c0f078f5e32b3 [file]
/*
* Copyright (C) 2007-2022 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. ``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
* 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 "HTMLVideoElement.h"
#if ENABLE(VIDEO)
#include "CSSPropertyNames.h"
#include "Chrome.h"
#include "ChromeClient.h"
#include "Document.h"
#include "DocumentPage.h"
#include "DocumentQuirks.h"
#include "DocumentView.h"
#include "ElementInlines.h"
#include "EventNames.h"
#include "FrameDestructionObserverInlines.h"
#include "HTMLImageLoader.h"
#include "HTMLNames.h"
#include "ImageBuffer.h"
#include "JSDOMPromiseDeferred.h"
#include "JSVideoFrameRequestCallback.h"
#include "LazyLoadVideoObserver.h"
#include "LocalDOMWindow.h"
#include "LocalFrame.h"
#include "Logging.h"
#include "MediaPlayerPrivate.h"
#include "Page.h"
#include "Performance.h"
#include "PictureInPictureSupport.h"
#include "RenderImage.h"
#include "RenderLayerCompositor.h"
#include "RenderObjectDocument.h"
#include "RenderVideoInlines.h"
#include "RenderView.h"
#include "ScriptController.h"
#include "Settings.h"
#include "ShareableBitmap.h"
#include "VideoFrameMetadata.h"
#include <wtf/NativePromise.h>
#include <wtf/ReducedResolutionSeconds.h>
#include <wtf/TZoneMallocInlines.h>
#include <wtf/text/TextStream.h>
#if ENABLE(VIDEO_PRESENTATION_MODE)
#include "VideoPresentationModel.h"
#endif
#if ENABLE(PICTURE_IN_PICTURE_API)
#include "EventTarget.h"
#include "HTMLVideoElementPictureInPicture.h"
#include "PictureInPictureObserver.h"
#endif
#if RELEASE_LOG_DISABLED
#define HTMLVIDEOELEMENT_RELEASE_LOG(formatString, ...)
#else
#define HTMLVIDEOELEMENT_RELEASE_LOG(formatString, ...) \
do { \
if (willLog(WTFLogLevel::Always)) { \
RELEASE_LOG_FORWARDABLE(Media, HTMLVideoElement##formatString, logIdentifier(), ##__VA_ARGS__); \
if (logger().hasEnabledInspector()) { \
std::array<char, 1024> buffer { }; \
SAFE_SPRINTF(std::span { buffer }, MESSAGE_HTMLVideoElement##formatString, logIdentifier(), ##__VA_ARGS__); \
logger().toObservers(logChannel(), WTFLogLevel::Always, { }, String::fromUTF8(buffer.data())); \
} \
} \
} while (0)
#endif
namespace WebCore {
WTF_MAKE_TZONE_ALLOCATED_IMPL(HTMLVideoElement);
using namespace HTMLNames;
inline HTMLVideoElement::HTMLVideoElement(const QualifiedName& tagName, Document& document, bool createdByParser)
: HTMLMediaElement(tagName, document, createdByParser)
{
ASSERT(hasTagName(videoTag));
m_defaultPosterURL = AtomString { document.settings().defaultVideoPosterURL() };
}
HTMLVideoElement::~HTMLVideoElement()
{
LazyLoadVideoObserver::unobserve(*this, protect(document()));
}
Ref<HTMLVideoElement> HTMLVideoElement::create(const QualifiedName& tagName, Document& document, bool createdByParser)
{
Ref videoElement = adoptRef(*new HTMLVideoElement(tagName, document, createdByParser));
#if ENABLE(PICTURE_IN_PICTURE_API)
HTMLVideoElementPictureInPicture::providePictureInPictureTo(videoElement);
#endif
LazyLoadVideoObserver::observe(videoElement);
videoElement->suspendIfNeeded();
return videoElement;
}
Ref<HTMLVideoElement> HTMLVideoElement::create(Document& document)
{
return create(videoTag, document, false);
}
bool HTMLVideoElement::rendererIsNeeded(const Style::ComputedStyle& style)
{
return HTMLElement::rendererIsNeeded(style);
}
RenderPtr<RenderElement> HTMLVideoElement::createElementRenderer(Style::ComputedStyle&& style, const RenderTreePosition&)
{
return createRenderer<RenderVideo>(*this, WTF::move(style));
}
void HTMLVideoElement::didAttachRenderers()
{
HTMLMediaElement::didAttachRenderers();
if (shouldDisplayPosterImage()) {
if (!m_imageLoader)
lazyInitialize(m_imageLoader, makeUniqueWithoutRefCountedCheck<HTMLImageLoader>(*this));
m_imageLoader->updateFromElement();
if (CheckedPtr renderer = this->renderer())
protect(renderer->imageResource())->setCachedImage(protect(m_imageLoader->image()));
}
}
void HTMLVideoElement::acceleratedRenderingStateChanged()
{
scheduleUpdateAcceleratedRenderingState();
}
bool HTMLVideoElement::supportsAcceleratedRendering() const
{
RefPtr player = this->player();
return player && player->supportsAcceleratedRendering();
}
void HTMLVideoElement::mediaPlayerRenderingModeChanged()
{
HTMLVIDEOELEMENT_RELEASE_LOG(MediaPlayerRenderingModeChanged);
// Kick off a fake recalcStyle that will update the compositing tree.
scheduleUpdateAcceleratedRenderingState();
invalidateStyleAndLayerComposition();
}
void HTMLVideoElement::computeAcceleratedRenderingStateAndUpdateMediaPlayer()
{
RefPtr player = this->player();
if (!player)
return;
#if ENABLE(VIDEO_PRESENTATION_MODE)
// This function must return "true" when the video is playing in the
// picture-in-picture window or if it is in fullscreen.
// Otherwise, the MediaPlayerPrivate* may destroy the video layer if
// it is no longer in the DOM.
bool isInFullScreen = (fullscreenMode() != VideoFullscreenModeNone) || isChangingVideoFullscreenMode();
#else
bool isInFullScreen = false;
#endif
CheckedPtr renderer = this->renderer();
// 311380@main added the viewport intersection to this condition. Some clients keep
// displaying the video layer they host after scrolling it out of view or hiding their
// web view, so for those ignore it as it was before.
bool isIntersectingViewport = m_isIntersectingViewport || protect(document())->quirks().shouldDisableMediaLayerTeardownOnPageVisibilityChangeQuirk();
bool canBeAccelerated = player->supportsAcceleratedRendering() && (isInFullScreen || (isIntersectingViewport && renderer && protect(renderer->view())->compositor().hasAcceleratedCompositing()));
if (canBeAccelerated == m_renderingCanBeAccelerated)
return;
m_renderingCanBeAccelerated = canBeAccelerated;
player->acceleratedRenderingStateChanged(); // This call will trigger a call back to `mediaPlayerRenderingCanBeAccelerated()` from the MediaPlayer.
}
#if PLATFORM(IOS_FAMILY)
bool HTMLVideoElement::canShowWhileLocked() const
{
RefPtr page = document().page();
return page && page->canShowWhileLocked();
}
#endif
void HTMLVideoElement::collectPresentationalHintsForAttribute(const QualifiedName& name, const AtomString& value, MutableStyleProperties& style)
{
if (name == widthAttr) {
addHTMLLengthToStyle(style, CSSPropertyWidth, value);
applyAspectRatioFromWidthAndHeightAttributesToStyle(value, attributeWithoutSynchronization(heightAttr), style);
} else if (name == heightAttr) {
addHTMLLengthToStyle(style, CSSPropertyHeight, value);
applyAspectRatioFromWidthAndHeightAttributesToStyle(attributeWithoutSynchronization(widthAttr), value, style);
} else
HTMLMediaElement::collectPresentationalHintsForAttribute(name, value, style);
}
bool HTMLVideoElement::hasPresentationalHintsForAttribute(const QualifiedName& name) const
{
if (name == widthAttr || name == heightAttr)
return true;
return HTMLMediaElement::hasPresentationalHintsForAttribute(name);
}
void HTMLVideoElement::attributeChanged(const QualifiedName& name, const AtomString& oldValue, const AtomString& newValue, AttributeModificationReason attributeModificationReason)
{
if (name == posterAttr) {
if (shouldDisplayPosterImage()) {
if (!m_imageLoader)
lazyInitialize(m_imageLoader, makeUniqueWithoutRefCountedCheck<HTMLImageLoader>(*this));
m_imageLoader->updateFromElementIgnoringPreviousError();
} else {
if (CheckedPtr renderer = this->renderer()) {
protect(renderer->imageResource())->clearCachedImage();
renderer->updateFromElement();
}
}
} else {
HTMLMediaElement::attributeChanged(name, oldValue, newValue, attributeModificationReason);
if (name == webkitprojectionAttr && oldValue != newValue && document().settings().spatialVideoRenderingEnabled())
scheduleEvent(eventNames().webkitprojectionchangedEvent);
#if PLATFORM(IOS_FAMILY) && ENABLE(WIRELESS_PLAYBACK_TARGET)
if (name == webkitairplayAttr)
protect(mediaSession())->setWirelessVideoPlaybackDisabled(isWirelessPlaybackTargetDisabled());
#endif
}
}
bool HTMLVideoElement::supportsFullscreen(HTMLMediaElementEnums::VideoFullscreenMode videoFullscreenMode) const
{
RefPtr player = this->player();
if (!player)
return false;
if (videoFullscreenMode == HTMLMediaElementEnums::VideoFullscreenModePictureInPicture) {
if (!mediaSession().allowsPictureInPicture())
return false;
if (!player->supportsPictureInPicture())
return false;
}
RefPtr page = document().page();
if (!page)
return false;
if (!player->supportsFullscreen())
return false;
#if HAVE(AVEXPERIENCECONTROLLER)
if (document().settings().isAVExperienceControllerFullscreenEnabled())
return true;
#endif
#if ENABLE(FULLSCREEN_API)
#if PLATFORM(IOS_FAMILY)
// Fullscreen implemented by player.
if (!document().settings().videoFullscreenRequiresElementFullscreen())
return true;
#endif
if (videoFullscreenMode == HTMLMediaElementEnums::VideoFullscreenModeStandard && !page->isDocumentFullscreenEnabled())
return false;
// If the full screen API is enabled and is supported for the current element
// do not require that the player has a video track to enter full screen.
if (videoFullscreenMode == HTMLMediaElementEnums::VideoFullscreenModeStandard && page->chrome().client().supportsFullScreenForElement(*this, false))
return true;
#endif
if (!player->hasVideo())
return false;
return page->chrome().client().supportsVideoFullscreen(videoFullscreenMode);
}
#if ENABLE(FULLSCREEN_API) && PLATFORM(IOS_FAMILY)
void HTMLVideoElement::requestFullscreen(FullscreenOptions&&, RefPtr<DeferredPromise>&& promise)
{
webkitSetPresentationMode(HTMLVideoElement::VideoPresentationMode::Fullscreen);
if (promise)
promise->resolve();
}
#endif
unsigned HTMLVideoElement::videoWidth() const
{
RefPtr player = this->player();
if (!player)
return 0;
return clampToUnsigned(player->naturalSize().width());
}
unsigned HTMLVideoElement::videoHeight() const
{
RefPtr player = this->player();
if (!player)
return 0;
return clampToUnsigned(player->naturalSize().height());
}
void HTMLVideoElement::scheduleResizeEvent(const FloatSize& naturalSize)
{
m_lastReportedNaturalSize = naturalSize;
HTMLVIDEOELEMENT_RELEASE_LOG(ScheduleResizeEvent, naturalSize.width(), naturalSize.height());
scheduleEvent(eventNames().resizeEvent);
}
void HTMLVideoElement::scheduleResizeEventIfSizeChanged(const FloatSize& naturalSize)
{
if (m_lastReportedNaturalSize != naturalSize)
scheduleResizeEvent(naturalSize);
}
bool HTMLVideoElement::isURLAttribute(const Attribute& attribute) const
{
return attribute.name() == posterAttr || HTMLMediaElement::isURLAttribute(attribute);
}
String HTMLVideoElement::imageSourceURL() const
{
const auto& url = attributeWithoutSynchronization(posterAttr);
if (!StringView(url).containsOnly<isASCIIWhitespace<char16_t>>())
return url;
return m_defaultPosterURL;
}
bool HTMLVideoElement::shouldDisplayPosterImage() const
{
if (!showPosterFlag())
return false;
if (posterImageURL().isEmpty())
return false;
CheckedPtr renderer = this->renderer();
if (renderer && renderer->failedToLoadPosterImage())
return false;
return true;
}
void HTMLVideoElement::mediaPlayerFirstVideoFrameAvailable()
{
HTMLVIDEOELEMENT_RELEASE_LOG(MediaPlayerFirstVideoFrameAvailable, showPosterFlag());
if (showPosterFlag())
return;
invalidateStyleAndLayerComposition();
if (RefPtr player = this->player())
player->prepareForRendering();
if (CheckedPtr renderer = this->renderer()) {
renderer->updateFromElement();
protect(document())->didPaintImage(*this, nullptr, renderer->videoBox());
}
}
std::optional<ColorSpace> HTMLVideoElement::colorSpace() const
{
RefPtr player = this->player();
if (!player)
return std::nullopt;
return player->colorSpace();
}
RefPtr<ImageBuffer> HTMLVideoElement::createBufferForPainting(const FloatSize& size, RenderingMode renderingMode, const ColorSpace& colorSpace, ImageBufferFormat pixelFormat) const
{
CheckedPtr view = document().view();
CheckedPtr root = view ? view->root() : nullptr;
auto* hostWindow = root ? root->hostWindow() : nullptr;
return ImageBuffer::create(size, renderingMode, RenderingPurpose::MediaPainting, 1, colorSpace, pixelFormat, hostWindow);
}
void HTMLVideoElement::paint(GraphicsContext& context, const FloatRect& destRect)
{
RefPtr player = this->player();
if (!player)
return;
player->paint(context, destRect);
}
void HTMLVideoElement::paintCurrentFrameInContext(GraphicsContext& context, const FloatRect& destRect)
{
RefPtr player = this->player();
if (!player)
return;
if (!player->isVisibleForCanvas()) {
player->setVisibleForCanvas(true); // Make player visible or it won't draw.
visibilityStateChanged();
}
player->paintCurrentFrameInContext(context, destRect);
}
bool HTMLVideoElement::hasAvailableVideoFrame() const
{
RefPtr player = this->player();
return player && player->hasVideo() && player->hasAvailableVideoFrame();
}
bool HTMLVideoElement::shouldGetNativeImageForCanvasDrawing() const
{
if (!player())
return false;
return protect(player())->shouldGetNativeImageForCanvasDrawing();
}
RefPtr<NativeImage> HTMLVideoElement::nativeImageForCurrentTime() const
{
RefPtr player = this->player();
return player ? player->nativeImageForCurrentTime() : nullptr;
}
RefPtr<ShareableBitmap> HTMLVideoElement::bitmapImageForCurrentTimeSync() const
{
RefPtr player = this->player();
if (!player)
return { };
return player->bitmapImageForCurrentTimeSync();
}
Ref<HTMLVideoElement::BitmapImagePromise> HTMLVideoElement::bitmapImageForCurrentTime() const
{
RefPtr player = this->player();
if (!player)
return BitmapImagePromise::createAndReject();
return player->bitmapImageForCurrentTime();
}
ExceptionOr<void> HTMLVideoElement::webkitEnterFullscreen()
{
ALWAYS_LOG(LOGIDENTIFIER);
if (isFullscreen())
return { };
// Generate an exception if this isn't called in response to a user gesture, or if the
// element does not support fullscreen, or the element is changing fullscreen mode.
if (!protect(mediaSession())->fullscreenPermitted() || !supportsFullscreen(HTMLMediaElementEnums::VideoFullscreenModeStandard) || isChangingVideoFullscreenMode())
return Exception { ExceptionCode::InvalidStateError };
enterFullscreen();
return { };
}
void HTMLVideoElement::webkitExitFullscreen()
{
ALWAYS_LOG(LOGIDENTIFIER);
if (isFullscreen() && !isChangingVideoFullscreenMode())
exitFullscreen();
}
bool HTMLVideoElement::webkitSupportsFullscreen()
{
return supportsFullscreen(HTMLMediaElementEnums::VideoFullscreenModeStandard);
}
bool HTMLVideoElement::webkitDisplayingFullscreen()
{
return isFullscreen();
}
void HTMLVideoElement::ancestorWillEnterFullscreen()
{
#if PLATFORM(MAC) && ENABLE(VIDEO_PRESENTATION_MODE)
if (fullscreenMode() == VideoFullscreenModeNone)
return;
// If this video element's presentation mode is not inline, but its ancestor
// is entering fullscreen, exit its current fullscreen mode.
exitToFullscreenModeWithoutAnimationIfPossible(fullscreenMode(), VideoFullscreenModeNone);
#endif
}
#if ENABLE(WIRELESS_PLAYBACK_TARGET)
bool HTMLVideoElement::webkitWirelessVideoPlaybackDisabled() const
{
return protect(mediaSession())->wirelessVideoPlaybackDisabled();
}
#endif
void HTMLVideoElement::didMoveToNewDocument(Document& oldDocument, Document& newDocument)
{
if (m_imageLoader)
m_imageLoader->elementDidMoveToNewDocument(oldDocument);
LazyLoadVideoObserver::unobserve(*this, oldDocument);
LazyLoadVideoObserver::observe(*this);
HTMLMediaElement::didMoveToNewDocument(oldDocument, newDocument);
}
#if ENABLE(MEDIA_STATISTICS)
unsigned HTMLVideoElement::webkitDecodedFrameCount() const
{
RefPtr player = this->player();
if (!player)
return 0;
return player->decodedFrameCount();
}
unsigned HTMLVideoElement::webkitDroppedFrameCount() const
{
RefPtr player = this->player();
if (!player)
return 0;
return player->droppedFrameCount();
}
#endif
URL HTMLVideoElement::posterImageURL() const
{
auto url = imageSourceURL().trim(isASCIIWhitespace);
if (url.isEmpty())
return URL();
return protect(document())->encodingParseURL(url);
}
#if ENABLE(VIDEO_PRESENTATION_MODE)
bool HTMLVideoElement::webkitSupportsPresentationMode(VideoPresentationMode mode) const
{
if (mode == VideoPresentationMode::Fullscreen)
return supportsFullscreen(HTMLMediaElementEnums::VideoFullscreenModeStandard);
if (mode == VideoPresentationMode::PictureInPicture) {
if (!supportsPictureInPicture())
return false;
return supportsFullscreen(HTMLMediaElementEnums::VideoFullscreenModePictureInPicture);
}
if (mode == VideoPresentationMode::Inline)
return !protect(mediaSession())->requiresFullscreenForVideoPlayback();
return false;
}
static inline HTMLMediaElementEnums::VideoFullscreenMode NODELETE toFullscreenMode(HTMLVideoElement::VideoPresentationMode mode)
{
switch (mode) {
case HTMLVideoElement::VideoPresentationMode::Fullscreen:
return HTMLMediaElementEnums::VideoFullscreenModeStandard;
case HTMLVideoElement::VideoPresentationMode::PictureInPicture:
return HTMLMediaElementEnums::VideoFullscreenModePictureInPicture;
case HTMLVideoElement::VideoPresentationMode::Inline:
return HTMLMediaElementEnums::VideoFullscreenModeNone;
case HTMLVideoElement::VideoPresentationMode::InWindow:
return HTMLMediaElementEnums::VideoFullscreenModeInWindow;
}
ASSERT_NOT_REACHED();
return HTMLMediaElementEnums::VideoFullscreenModeNone;
}
HTMLVideoElement::VideoPresentationMode HTMLVideoElement::toPresentationMode(HTMLMediaElementEnums::VideoFullscreenMode mode)
{
if (mode == HTMLMediaElementEnums::VideoFullscreenModeStandard)
return HTMLVideoElement::VideoPresentationMode::Fullscreen;
if (mode & HTMLMediaElementEnums::VideoFullscreenModePictureInPicture)
return HTMLVideoElement::VideoPresentationMode::PictureInPicture;
if (mode == HTMLMediaElementEnums::VideoFullscreenModeNone)
return HTMLVideoElement::VideoPresentationMode::Inline;
if (mode == HTMLMediaElementEnums::VideoFullscreenModeInWindow)
return HTMLVideoElement::VideoPresentationMode::InWindow;
ASSERT_NOT_REACHED();
return HTMLVideoElement::VideoPresentationMode::Inline;
}
void HTMLVideoElement::webkitSetPresentationMode(VideoPresentationMode mode)
{
INFO_LOG(LOGIDENTIFIER, ", mode = ", mode);
if (!isChangingVideoFullscreenMode())
setPresentationMode(mode);
}
void HTMLVideoElement::setPresentationMode(VideoPresentationMode mode)
{
if (toPresentationMode(fullscreenMode()) == mode)
return;
auto videoFullscreenMode = toFullscreenMode(mode);
INFO_LOG(LOGIDENTIFIER, ", videoFullscreenMode = ", videoFullscreenMode);
if (videoFullscreenMode == VideoFullscreenModeNone) {
if (isFullscreen()) {
if (toPresentationMode(fullscreenMode()) == VideoPresentationMode::PictureInPicture)
m_exitingPictureInPicture = true;
exitFullscreen();
}
return;
}
#if ENABLE(PICTURE_IN_PICTURE_API)
bool requiresUserGesture = mode != VideoPresentationMode::PictureInPicture || !protect(document())->pictureInPictureElement();
#else
bool requiresUserGesture = true;
#endif
if ((requiresUserGesture && !protect(mediaSession())->fullscreenPermitted()) || !supportsFullscreen(videoFullscreenMode))
return;
if (videoFullscreenMode == VideoFullscreenModePictureInPicture)
m_enteringPictureInPicture = true;
else if (fullscreenMode() == VideoFullscreenModePictureInPicture)
m_exitingPictureInPicture = true;
enterFullscreen(videoFullscreenMode);
}
auto HTMLVideoElement::webkitPresentationMode() const -> VideoPresentationMode
{
return toPresentationMode(fullscreenMode());
}
auto HTMLVideoElement::webkitPresentationModeForBindings() const -> VideoPresentationMode
{
auto mode = webkitPresentationMode();
if (mode == HTMLVideoElement::VideoPresentationMode::InWindow)
return HTMLVideoElement::VideoPresentationMode::Inline;
return mode;
}
void HTMLVideoElement::didEnterFullscreenOrPictureInPicture(const FloatSize& size)
{
if (RefPtr player = this->player())
player->setInFullscreenOrPictureInPicture(true);
if (m_enteringPictureInPicture) {
m_enteringPictureInPicture = false;
setChangingVideoFullscreenMode(false);
// The PiP transition just settled: RenderVideo's object-view-box crop-bypass is
// gated on isChangingVideoFullscreenMode(), so force a compositing update now to pick
// up the newly-unlocked bypass rather than waiting for some unrelated future update.
// Only needed when object-view-box is actually in play.
if (CheckedPtr renderer = this->renderer(); renderer && renderer->hasObjectViewBoxSet())
renderer->contentChanged(ContentChangeType::Video);
#if ENABLE(PICTURE_IN_PICTURE_API)
if (RefPtr observer = m_pictureInPictureObserver.get())
observer->didEnterPictureInPicture(flooredIntSize(size));
#else
UNUSED_PARAM(size);
#endif
return;
}
if (m_exitingPictureInPicture) {
m_exitingPictureInPicture = false;
#if ENABLE(PICTURE_IN_PICTURE_API)
if (RefPtr observer = m_pictureInPictureObserver.get())
observer->didExitPictureInPicture();
#endif
}
HTMLMediaElement::didBecomeFullscreenElement();
}
void HTMLVideoElement::didExitFullscreenOrPictureInPicture()
{
if (RefPtr player = this->player())
player->setInFullscreenOrPictureInPicture(false);
if (m_exitingPictureInPicture) {
m_exitingPictureInPicture = false;
setChangingVideoFullscreenMode(false);
// Same as the enter-PiP settle path above: force a compositing update so the crop
// (no longer bypassed, now that we're settled back to non-fullscreen) re-applies now.
// Only needed when object-view-box is actually in play.
if (CheckedPtr renderer = this->renderer(); renderer && renderer->hasObjectViewBoxSet())
renderer->contentChanged(ContentChangeType::Video);
#if ENABLE(PICTURE_IN_PICTURE_API)
if (RefPtr observer = m_pictureInPictureObserver.get())
observer->didExitPictureInPicture();
#endif
return;
}
HTMLMediaElement::didStopBeingFullscreenElement();
}
#if ENABLE(LINEAR_MEDIA_PLAYER)
void HTMLVideoElement::didEnterExternalPlayback()
{
m_isInExternalPlayback = true;
if (RefPtr player = this->player())
player->setInFullscreenOrPictureInPicture(true);
}
void HTMLVideoElement::didExitExternalPlayback()
{
m_isInExternalPlayback = false;
if (RefPtr player = this->player())
player->setInFullscreenOrPictureInPicture(false);
}
#endif
bool HTMLVideoElement::isChangingPresentationMode() const
{
return isChangingVideoFullscreenMode();
}
void HTMLVideoElement::setVideoFullscreenFrame(const FloatRect& frame)
{
HTMLMediaElement::setVideoFullscreenFrame(frame);
if (toPresentationMode(fullscreenMode()) != VideoPresentationMode::PictureInPicture)
return;
#if ENABLE(PICTURE_IN_PICTURE_API)
if (!m_enteringPictureInPicture && !m_exitingPictureInPicture) {
if (RefPtr observer = m_pictureInPictureObserver.get())
observer->pictureInPictureWindowResized(IntSize(frame.size()));
}
#endif
}
#if ENABLE(PICTURE_IN_PICTURE_API)
void HTMLVideoElement::setPictureInPictureObserver(PictureInPictureObserver* observer)
{
m_pictureInPictureObserver = observer;
}
#endif
#endif // ENABLE(VIDEO_PRESENTATION_MODE)
#if PLATFORM(MAC) && ENABLE(VIDEO_PRESENTATION_MODE)
void HTMLVideoElement::exitToFullscreenModeWithoutAnimationIfPossible(HTMLMediaElementEnums::VideoFullscreenMode fromMode, HTMLMediaElementEnums::VideoFullscreenMode toMode)
{
RefPtr page = document().page();
if (page->chrome().client().supportsVideoFullscreen(fromMode))
page->chrome().client().exitVideoFullscreenToModeWithoutAnimation(*this, toMode);
}
#endif
unsigned HTMLVideoElement::requestVideoFrameCallback(Ref<VideoFrameRequestCallback>&& callback)
{
if (m_videoFrameRequests.isEmpty()) {
if (RefPtr player = this->player())
player->startVideoFrameMetadataGathering();
}
auto identifier = ++m_nextVideoFrameRequestIndex;
m_videoFrameRequests.append(makeUniqueRef<VideoFrameRequest>(identifier, WTF::move(callback)));
if (RefPtr page = document().page())
page->scheduleRenderingUpdate(RenderingUpdateStep::VideoFrameCallbacks);
return identifier;
}
void HTMLVideoElement::cancelVideoFrameCallback(unsigned identifier)
{
// Search first the requests currently being serviced, and mark them as cancelled if found.
auto index = m_servicedVideoFrameRequests.findIf([identifier](auto& request) { return request->identifier == identifier; });
if (index != notFound) {
m_servicedVideoFrameRequests[index]->callback = nullptr;
return;
}
index = m_videoFrameRequests.findIf([identifier](auto& request) { return request->identifier == identifier; });
if (index == notFound)
return;
m_videoFrameRequests.removeAt(index);
if (m_videoFrameRequests.isEmpty()) {
if (RefPtr player = this->player())
player->stopVideoFrameMetadataGathering();
}
}
void HTMLVideoElement::suspend(ReasonForSuspension reason)
{
#if ENABLE(PICTURE_IN_PICTURE_API)
if (reason == ReasonForSuspension::BackForwardCache)
protect(HTMLVideoElementPictureInPicture::from(*this))->didExitPictureInPicture();
#endif
HTMLMediaElement::suspend(reason);
}
void HTMLVideoElement::stop()
{
m_videoFrameRequests.clear();
for (auto& request : m_servicedVideoFrameRequests)
request->callback = nullptr;
HTMLMediaElement::stop();
}
void HTMLVideoElement::viewportIntersectionChanged(bool isIntersecting)
{
if (m_isIntersectingViewport == isIntersecting)
return;
m_isIntersectingViewport = isIntersecting;
isVisibleInViewportChanged();
scheduleUpdateAcceleratedRenderingState();
}
static void processVideoFrameMetadataTimestamps(VideoFrameMetadata& metadata, Performance& performance)
{
metadata.presentationTime = performance.relativeTimeFromTimeOriginInReducedResolution(MonotonicTime::fromRawSeconds(metadata.presentationTime));
metadata.expectedDisplayTime = performance.relativeTimeFromTimeOriginInReducedResolution(MonotonicTime::fromRawSeconds(metadata.expectedDisplayTime));
if (metadata.captureTime)
metadata.captureTime = performance.relativeTimeFromTimeOriginInReducedResolution(MonotonicTime::fromRawSeconds(*metadata.captureTime));
if (metadata.receiveTime)
metadata.receiveTime = performance.relativeTimeFromTimeOriginInReducedResolution(MonotonicTime::fromRawSeconds(*metadata.receiveTime));
}
void HTMLVideoElement::serviceRequestVideoFrameCallbacks(ReducedResolutionSeconds now)
{
if (!player())
return;
// If the requestVideoFrameCallback is called before the readyState >= HaveCurrentData,
// calls to createImageBitmap() with this element will result in a failed promise. Delay
// notifying the callback until we reach the HaveCurrentData state.
if (readyState() < HAVE_CURRENT_DATA)
return;
auto videoFrameMetadata = protect(player())->videoFrameMetadata();
if (!videoFrameMetadata || !document().window())
return;
RefPtr frame = document().frame();
if (!frame)
return;
CheckedRef script = frame->script();
if (script->isPaused())
return;
processVideoFrameMetadataTimestamps(*videoFrameMetadata, protect(protect(document().window())->performance()));
Ref protectedThis { *this };
m_videoFrameRequests.swap(m_servicedVideoFrameRequests);
for (auto& request : m_servicedVideoFrameRequests) {
DOMWrapperWorld* world = nullptr;
if (request->callback) {
if (RefPtr jsCallback = dynamicDowncast<JSVideoFrameRequestCallback>(*request->callback)) {
if (auto* globalObject = jsCallback->callbackData()->globalObject())
world = &globalObject->world();
}
}
if (!script->canExecuteScripts(ReasonForCallingCanExecuteScripts::AboutToExecuteScript, world))
continue;
if (RefPtr callback = std::exchange(request->callback, { }))
callback->invoke(now.milliseconds(), *videoFrameMetadata);
}
m_servicedVideoFrameRequests.clear();
if (m_videoFrameRequests.isEmpty()) {
if (RefPtr player = this->player())
player->stopVideoFrameMetadataGathering();
}
}
void HTMLVideoElement::mediaPlayerEngineUpdated()
{
HTMLMediaElement::mediaPlayerEngineUpdated();
if (!m_videoFrameRequests.isEmpty()) {
if (RefPtr player = this->player())
player->startVideoFrameMetadataGathering();
}
// If the RenderLayerCompositor had queried the element's MediaPlayer::supportsAcceleratedRendering prior the player having been created it would have been set to false.
mediaPlayerRenderingModeChanged();
}
void HTMLVideoElement::setVideoFullscreenStandby(bool value)
{
if (videoFullscreenStandby() == value)
return;
if (!document().page())
return;
if (!document().page()->chrome().client().supportsVideoFullscreenStandby())
return;
setVideoFullscreenStandbyInternal(value);
#if ENABLE(VIDEO_PRESENTATION_MODE)
if (RefPtr player = this->player())
player->videoFullscreenStandbyChanged();
#endif
if (fullscreenMode() != VideoFullscreenModeNone)
return;
if (videoFullscreenStandby())
document().page()->chrome().client().enterVideoFullscreenForVideoElement(*this, VideoFullscreenModeNone, true);
else {
document().page()->chrome().client().exitVideoFullscreenForVideoElement(*this, [this, protectedThis = Ref { *this }](auto success) mutable {
setVideoFullscreenStandbyInternal(!success);
});
}
}
ExceptionOr<void> HTMLVideoElement::enterFullscreenIgnoringPermissionsPolicy()
{
ignoreFullscreenPermissionPolicyOnNextCallToEnterFullscreen();
return webkitEnterFullscreen();
}
#if ENABLE(VIDEO_PRESENTATION_MODE)
void HTMLVideoElement::setPresentationModeIgnoringPermissionsPolicy(VideoPresentationMode mode)
{
ignoreFullscreenPermissionPolicyOnNextCallToEnterFullscreen();
setPresentationMode(mode);
}
#endif
} // namespace WebCore
#endif // ENABLE(VIDEO)