Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(448)

Side by Side Diff: third_party/WebKit/Source/core/html/MediaCustomControlsFullscreenDetector.cpp

Issue 2696893002: [Blink>Media] Add heuristic for dominant video detection for Android (Closed)
Patch Set: fixing logic in WMPI Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "core/html/MediaCustomControlsFullscreenDetector.h"
6
7 #include "core/dom/Fullscreen.h"
8 #include "core/dom/TaskRunnerHelper.h"
9 #include "core/events/Event.h"
10 #include "core/html/HTMLVideoElement.h"
11 #include "core/layout/IntersectionGeometry.h"
12
13 namespace blink {
14
15 namespace {
16
17 constexpr double kCheckFullscreenIntervalSeconds = 1.0f;
18 constexpr float kMostlyFillViewportThresholdOfOccupationProportion = 0.85f;
19 constexpr float kMostlyFillViewportThresholdOfVisibleProportion = 0.75f;
20
21 } // anonymous namespace
22
23 MediaCustomControlsFullscreenDetector::MediaCustomControlsFullscreenDetector(
24 HTMLVideoElement& video)
25 : EventListener(CPPEventListenerType),
26 m_videoElement(video),
27 m_checkViewportIntersectionTimer(
28 TaskRunnerHelper::get(TaskType::Unthrottled, &video.document()),
29 this,
30 &MediaCustomControlsFullscreenDetector::
31 onCheckViewportIntersectionTimerFired) {
32 videoElement().document().addEventListener(
33 EventTypeNames::webkitfullscreenchange, this, true);
34 videoElement().document().addEventListener(EventTypeNames::fullscreenchange,
35 this, true);
36
37 videoElement().addEventListener(EventTypeNames::loadedmetadata, this, true);
38 }
39
40 bool MediaCustomControlsFullscreenDetector::operator==(
41 const EventListener& other) const {
42 return this == &other;
43 }
44
45 void MediaCustomControlsFullscreenDetector::didMoveToNewDocument(
46 Document& oldDocument) {
47 oldDocument.removeEventListener(EventTypeNames::webkitfullscreenchange, this,
48 true);
49 oldDocument.removeEventListener(EventTypeNames::fullscreenchange, this, true);
50
51 videoElement().document().addEventListener(
52 EventTypeNames::webkitfullscreenchange, this, true);
53 videoElement().document().addEventListener(EventTypeNames::fullscreenchange,
54 this, true);
55 }
56
57 bool MediaCustomControlsFullscreenDetector::computeIsDominantVideo(
58 const IntRect& targetRect,
59 const IntRect& rootRect,
60 const IntRect& intersectionRect) {
61 if (targetRect.isEmpty() || rootRect.isEmpty())
62 return false;
63
64 const float xOccupationProportion =
65 1.0f * intersectionRect.width() / rootRect.width();
66 const float yOccupationProportion =
67 1.0f * intersectionRect.height() / rootRect.height();
68
69 // If the viewport is mostly occupied by the video, return true.
70 if (std::min(xOccupationProportion, yOccupationProportion) >=
71 kMostlyFillViewportThresholdOfOccupationProportion) {
72 return true;
73 }
74
75 // If neither of the dimensions of the viewport is mostly occupied by the
76 // video, return false.
77 if (std::max(xOccupationProportion, yOccupationProportion) <
78 kMostlyFillViewportThresholdOfOccupationProportion) {
79 return false;
80 }
81
82 // If the video is mostly visible in the indominant dimension, return true.
83 // Otherwise return false.
84 if (xOccupationProportion > yOccupationProportion) {
85 return targetRect.height() *
86 kMostlyFillViewportThresholdOfVisibleProportion <
87 intersectionRect.height();
88 }
89 return targetRect.width() * kMostlyFillViewportThresholdOfVisibleProportion <
90 intersectionRect.width();
91 }
92
93 void MediaCustomControlsFullscreenDetector::handleEvent(
94 ExecutionContext* context,
95 Event* event) {
96 DCHECK(event->type() == EventTypeNames::fullscreenchange ||
97 event->type() == EventTypeNames::webkitfullscreenchange ||
98 event->type() == EventTypeNames::loadedmetadata);
xjz 2017/02/25 00:49:12 Do you also want to listen to the resize event of
Zhiqiang Zhang (Slow) 2017/02/25 18:48:27 Not for now. We are counting on the page to resize
99
100 // Video is not loaded yet.
101 if (videoElement().getReadyState() < HTMLMediaElement::kHaveMetadata)
102 return;
103
104 if (!isVideoOrParentFullscreen()) {
105 m_checkViewportIntersectionTimer.stop();
106
107 if (videoElement().webMediaPlayer()) {
108 videoElement().webMediaPlayer()->setIsEffectivelyFullscreen(false);
109 }
110 return;
111 }
112
113 m_checkViewportIntersectionTimer.startOneShot(kCheckFullscreenIntervalSeconds,
114 BLINK_FROM_HERE);
115 }
116
117 void MediaCustomControlsFullscreenDetector::
118 onCheckViewportIntersectionTimerFired(TimerBase*) {
119 DCHECK(isVideoOrParentFullscreen());
120 IntersectionGeometry geometry(nullptr, videoElement(), Vector<Length>(),
121 true);
122 geometry.computeGeometry();
123
124 bool isDominant =
125 computeIsDominantVideo(geometry.targetIntRect(), geometry.rootIntRect(),
126 geometry.intersectionIntRect());
127
128 if (videoElement().webMediaPlayer()) {
129 videoElement().webMediaPlayer()->setIsEffectivelyFullscreen(isDominant);
130 }
131 }
132
133 bool MediaCustomControlsFullscreenDetector::isVideoOrParentFullscreen() {
134 Element* fullscreenElement =
135 Fullscreen::currentFullScreenElementFrom(videoElement().document());
136 if (!fullscreenElement)
137 return false;
138
139 Element* currentElement = &videoElement();
miu 2017/02/24 22:44:39 IIRC, there's some kind of isAncestorOf() utility
Zhiqiang Zhang (Slow) 2017/02/25 18:48:27 Yes, Node.contains(). Done :)
140 while (currentElement) {
141 if (fullscreenElement == currentElement)
142 return true;
143
144 currentElement = currentElement->parentElement();
145 }
146
147 return false;
148 }
149
150 DEFINE_TRACE(MediaCustomControlsFullscreenDetector) {
151 EventListener::trace(visitor);
152 visitor->trace(m_videoElement);
153 }
154
155 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698