2 * Copyright (C) 2014 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #if PLATFORM(IOS) || (PLATFORM(MAC) && ENABLE(VIDEO_PRESENTATION_MODE))
29 #import "VideoFullscreenModelVideoElement.h"
34 #import "MediaControlsHost.h"
35 #import "PlaybackSessionModelMediaElement.h"
36 #import <QuartzCore/CoreAnimation.h>
37 #import <WebCore/Event.h>
38 #import <WebCore/EventListener.h>
39 #import <WebCore/EventNames.h>
40 #import <WebCore/HTMLElement.h>
41 #import <WebCore/HTMLVideoElement.h>
42 #import <WebCore/Page.h>
43 #import <WebCore/TextTrackList.h>
44 #import <WebCore/TimeRanges.h>
45 #import <wtf/NeverDestroyed.h>
46 #import <wtf/SoftLinking.h>
48 using namespace WebCore;
50 VideoFullscreenModelVideoElement::VideoFullscreenModelVideoElement()
51 : EventListener(EventListener::CPPEventListenerType)
55 VideoFullscreenModelVideoElement::~VideoFullscreenModelVideoElement()
59 void VideoFullscreenModelVideoElement::setVideoElement(HTMLVideoElement* videoElement)
61 if (m_videoElement == videoElement)
64 if (m_videoElement && m_videoElement->videoFullscreenLayer())
65 m_videoElement->setVideoFullscreenLayer(nullptr);
67 if (m_videoElement && m_isListening) {
68 for (auto& eventName : observedEventNames())
69 m_videoElement->removeEventListener(eventName, *this, false);
71 m_isListening = false;
73 m_videoElement = videoElement;
76 for (auto& eventName : observedEventNames())
77 m_videoElement->addEventListener(eventName, *this, false);
81 updateForEventName(eventNameAll());
84 void VideoFullscreenModelVideoElement::handleEvent(WebCore::ScriptExecutionContext&, WebCore::Event& event)
86 updateForEventName(event.type());
89 void VideoFullscreenModelVideoElement::updateForEventName(const WTF::AtomicString& eventName)
91 if (m_clients.isEmpty())
94 bool all = eventName == eventNameAll();
97 || eventName == eventNames().resizeEvent) {
98 setHasVideo(m_videoElement);
99 setVideoDimensions(m_videoElement ? FloatSize(m_videoElement->videoWidth(), m_videoElement->videoHeight()) : FloatSize());
103 void VideoFullscreenModelVideoElement::willExitFullscreen()
106 m_videoElement->willExitFullscreen();
109 void VideoFullscreenModelVideoElement::setVideoFullscreenLayer(PlatformLayer* videoLayer, WTF::Function<void()>&& completionHandler)
111 if (m_videoFullscreenLayer == videoLayer) {
116 m_videoFullscreenLayer = videoLayer;
118 [m_videoFullscreenLayer setAnchorPoint:CGPointMake(0, 0)];
120 [m_videoFullscreenLayer setAnchorPoint:CGPointMake(0.5, 0.5)];
122 [m_videoFullscreenLayer setBounds:m_videoFrame];
124 if (!m_videoElement) {
129 m_videoElement->setVideoFullscreenLayer(m_videoFullscreenLayer.get(), WTFMove(completionHandler));
132 void VideoFullscreenModelVideoElement::waitForPreparedForInlineThen(WTF::Function<void()>&& completionHandler)
134 if (!m_videoElement) {
139 m_videoElement->waitForPreparedForInlineThen(WTFMove(completionHandler));
142 void VideoFullscreenModelVideoElement::requestFullscreenMode(HTMLMediaElementEnums::VideoFullscreenMode mode, bool finishedWithMedia)
144 if (m_videoElement && m_videoElement->fullscreenMode() != mode)
145 m_videoElement->setFullscreenMode(mode);
147 if (m_videoElement && finishedWithMedia && mode == MediaPlayerEnums::VideoFullscreenModeNone) {
148 if (m_videoElement->document().isMediaDocument()) {
149 if (DOMWindow* window = m_videoElement->document().domWindow()) {
150 if (History* history = window->history())
157 void VideoFullscreenModelVideoElement::setVideoLayerFrame(FloatRect rect)
160 [m_videoFullscreenLayer setBounds:CGRect(rect)];
162 m_videoElement->setVideoFullscreenFrame(rect);
165 void VideoFullscreenModelVideoElement::setVideoLayerGravity(VideoFullscreenModel::VideoGravity gravity)
167 MediaPlayer::VideoGravity videoGravity = MediaPlayer::VideoGravityResizeAspect;
168 if (gravity == VideoFullscreenModel::VideoGravityResize)
169 videoGravity = MediaPlayer::VideoGravityResize;
170 else if (gravity == VideoFullscreenModel::VideoGravityResizeAspect)
171 videoGravity = MediaPlayer::VideoGravityResizeAspect;
172 else if (gravity == VideoFullscreenModel::VideoGravityResizeAspectFill)
173 videoGravity = MediaPlayer::VideoGravityResizeAspectFill;
175 ASSERT_NOT_REACHED();
177 m_videoElement->setVideoFullscreenGravity(videoGravity);
180 const Vector<AtomicString>& VideoFullscreenModelVideoElement::observedEventNames()
182 static const auto names = makeNeverDestroyed(Vector<AtomicString> { eventNames().resizeEvent });
186 const AtomicString& VideoFullscreenModelVideoElement::eventNameAll()
188 static NeverDestroyed<AtomicString> sEventNameAll = "allEvents";
189 return sEventNameAll;
192 void VideoFullscreenModelVideoElement::fullscreenModeChanged(HTMLMediaElementEnums::VideoFullscreenMode videoFullscreenMode)
195 m_videoElement->fullscreenModeChanged(videoFullscreenMode);
198 void VideoFullscreenModelVideoElement::addClient(VideoFullscreenModelClient& client)
200 ASSERT(!m_clients.contains(&client));
201 m_clients.add(&client);
204 void VideoFullscreenModelVideoElement::removeClient(VideoFullscreenModelClient& client)
206 ASSERT(m_clients.contains(&client));
207 m_clients.remove(&client);
210 bool VideoFullscreenModelVideoElement::isVisible() const
215 if (Page* page = m_videoElement->document().page())
216 return page->isVisible();
221 void VideoFullscreenModelVideoElement::setHasVideo(bool hasVideo)
223 if (hasVideo == m_hasVideo)
226 m_hasVideo = hasVideo;
228 for (auto& client : m_clients)
229 client->hasVideoChanged(m_hasVideo);
232 void VideoFullscreenModelVideoElement::setVideoDimensions(const FloatSize& videoDimensions)
234 if (m_videoDimensions == videoDimensions)
237 m_videoDimensions = videoDimensions;
239 for (auto& client : m_clients)
240 client->videoDimensionsChanged(m_videoDimensions);
243 void VideoFullscreenModelVideoElement::willEnterPictureInPicture()
245 for (auto& client : m_clients)
246 client->willEnterPictureInPicture();
249 void VideoFullscreenModelVideoElement::didEnterPictureInPicture()
251 for (auto& client : m_clients)
252 client->didEnterPictureInPicture();
255 void VideoFullscreenModelVideoElement::failedToEnterPictureInPicture()
257 for (auto& client : m_clients)
258 client->failedToEnterPictureInPicture();
261 void VideoFullscreenModelVideoElement::willExitPictureInPicture()
263 for (auto& client : m_clients)
264 client->willExitPictureInPicture();
267 void VideoFullscreenModelVideoElement::didExitPictureInPicture()
269 for (auto& client : m_clients)
270 client->didExitPictureInPicture();