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

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: fixed event listener leak 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().addEventListener(EventTypeNames::DOMNodeInsertedIntoDocument,
33 this, false);
34 videoElement().addEventListener(EventTypeNames::DOMNodeRemovedFromDocument,
35 this, false);
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::attach() {
46 videoElement().document().addEventListener(
47 EventTypeNames::webkitfullscreenchange, this, true);
48 videoElement().document().addEventListener(EventTypeNames::fullscreenchange,
49 this, true);
50 }
51
52 void MediaCustomControlsFullscreenDetector::detach() {
53 videoElement().document().removeEventListener(
54 EventTypeNames::webkitfullscreenchange, this, true);
55 videoElement().document().removeEventListener(
56 EventTypeNames::fullscreenchange, this, true);
57 }
58
59 bool MediaCustomControlsFullscreenDetector::computeIsDominantVideoForTests(
60 const IntRect& targetRect,
61 const IntRect& rootRect,
62 const IntRect& intersectionRect) {
63 if (targetRect.isEmpty() || rootRect.isEmpty())
64 return false;
65
66 const float xOccupationProportion =
67 1.0f * intersectionRect.width() / rootRect.width();
68 const float yOccupationProportion =
69 1.0f * intersectionRect.height() / rootRect.height();
70
71 // If the viewport is mostly occupied by the video, return true.
72 if (std::min(xOccupationProportion, yOccupationProportion) >=
73 kMostlyFillViewportThresholdOfOccupationProportion) {
74 return true;
75 }
76
77 // If neither of the dimensions of the viewport is mostly occupied by the
78 // video, return false.
79 if (std::max(xOccupationProportion, yOccupationProportion) <
80 kMostlyFillViewportThresholdOfOccupationProportion) {
81 return false;
82 }
83
84 // If the video is mostly visible in the indominant dimension, return true.
85 // Otherwise return false.
86 if (xOccupationProportion > yOccupationProportion) {
87 return targetRect.height() *
88 kMostlyFillViewportThresholdOfVisibleProportion <
89 intersectionRect.height();
90 }
91 return targetRect.width() * kMostlyFillViewportThresholdOfVisibleProportion <
92 intersectionRect.width();
93 }
94
95 void MediaCustomControlsFullscreenDetector::handleEvent(
96 ExecutionContext* context,
97 Event* event) {
98 if (event->type() == EventTypeNames::DOMNodeInsertedIntoDocument) {
99 attach();
100 } else if (event->type() == EventTypeNames::DOMNodeRemovedFromDocument) {
101 detach();
102 }
mlamouri (slow - plz ping) 2017/03/02 14:23:36 What about: if (event->type() == ...) { attach()
mlamouri (slow - plz ping) 2017/03/02 14:27:18 Zhiqiang told me how wrong I was. Second time toda
Zhiqiang Zhang (Slow) 2017/03/02 14:33:42 Added DCHECK() and comments explaining why
103
104 // Video is not loaded yet.
105 if (videoElement().getReadyState() < HTMLMediaElement::kHaveMetadata)
106 return;
107
108 if (!videoElement().isConnected() || !isVideoOrParentFullscreen()) {
109 m_checkViewportIntersectionTimer.stop();
110
111 if (videoElement().webMediaPlayer())
112 videoElement().webMediaPlayer()->setIsEffectivelyFullscreen(false);
113
114 return;
115 }
116
117 m_checkViewportIntersectionTimer.startOneShot(kCheckFullscreenIntervalSeconds,
118 BLINK_FROM_HERE);
119 }
120
121 void MediaCustomControlsFullscreenDetector::
122 onCheckViewportIntersectionTimerFired(TimerBase*) {
123 DCHECK(isVideoOrParentFullscreen());
124 IntersectionGeometry geometry(nullptr, videoElement(), Vector<Length>(),
125 true);
126 geometry.computeGeometry();
127
128 bool isDominant = computeIsDominantVideoForTests(
129 geometry.targetIntRect(), geometry.rootIntRect(),
130 geometry.intersectionIntRect());
131
132 if (videoElement().webMediaPlayer())
133 videoElement().webMediaPlayer()->setIsEffectivelyFullscreen(isDominant);
134 }
135
136 bool MediaCustomControlsFullscreenDetector::isVideoOrParentFullscreen() {
137 Element* fullscreenElement =
138 Fullscreen::currentFullScreenElementFrom(videoElement().document());
139 if (!fullscreenElement)
140 return false;
141
142 return fullscreenElement->contains(&videoElement());
143 }
144
145 DEFINE_TRACE(MediaCustomControlsFullscreenDetector) {
146 EventListener::trace(visitor);
147 visitor->trace(m_videoElement);
148 }
149
150 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698