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

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: addressed mlamouri's comments Created 3 years, 10 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/DocumentFullscreen.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 const float xOccupationProportion =
62 1.0f * intersectionRect.width() / rootRect.width();
xjz 2017/02/22 18:48:34 Can rootRect be empty? If not, maybe add DCHECK.
Zhiqiang Zhang (Slow) 2017/02/23 21:50:55 Thanks. Now I return false if targetRect or rootRe
xjz 2017/02/23 22:49:46 I don't see any changes though. :)
Zhiqiang Zhang (Slow) 2017/02/24 12:03:39 Sorry, it's a mistake (committed to another checko
63 const float yOccupationProportion =
64 1.0f * intersectionRect.height() / rootRect.height();
65
66 // If the viewport is mostly occupied by the video, return true.
67 if (std::min(xOccupationProportion, yOccupationProportion) >=
68 kMostlyFillViewportThresholdOfOccupationProportion) {
69 return true;
70 }
71
72 // If neither of the dimensions of the viewport is mostly occupied by the
73 // video, return false.
74 if (std::max(xOccupationProportion, yOccupationProportion) <
75 kMostlyFillViewportThresholdOfOccupationProportion) {
76 return false;
77 }
78
79 // If the video is mostly visible in the indominant dimension, return true.
80 // Otherwise return false.
81 if (xOccupationProportion > yOccupationProportion) {
82 return targetRect.height() *
83 kMostlyFillViewportThresholdOfVisibleProportion <
84 intersectionRect.height();
85 }
86 return targetRect.width() * kMostlyFillViewportThresholdOfVisibleProportion <
87 intersectionRect.width();
88 }
89
90 void MediaCustomControlsFullscreenDetector::handleEvent(
91 ExecutionContext* context,
92 Event* event) {
93 DCHECK(event->type() == EventTypeNames::fullscreenchange ||
94 event->type() == EventTypeNames::webkitfullscreenchange ||
95 event->type() == EventTypeNames::loadedmetadata);
96
97 // Video is not loaded yet.
98 if (videoElement().getReadyState() < HTMLMediaElement::kHaveMetadata)
99 return;
100
101 if (!isVideoOrParentFullscreen()) {
102 m_checkViewportIntersectionTimer.stop();
103
104 if (videoElement().webMediaPlayer()) {
105 videoElement().webMediaPlayer()->setEffectivelyFullscreen(false);
106 }
107 return;
108 }
109
110 m_checkViewportIntersectionTimer.startOneShot(kCheckFullscreenIntervalSeconds,
111 BLINK_FROM_HERE);
112 }
113
114 void MediaCustomControlsFullscreenDetector::
115 onCheckViewportIntersectionTimerFired(TimerBase*) {
116 DCHECK(isVideoOrParentFullscreen());
117 IntersectionGeometry geometry(nullptr, videoElement(), Vector<Length>(),
118 true);
119 geometry.computeGeometry();
120
121 bool isDominant =
122 computeIsDominantVideo(geometry.targetIntRect(), geometry.rootIntRect(),
123 geometry.intersectionIntRect());
124
125 if (videoElement().webMediaPlayer()) {
126 videoElement().webMediaPlayer()->setEffectivelyFullscreen(isDominant);
127 }
128 }
129
130 bool MediaCustomControlsFullscreenDetector::isVideoOrParentFullscreen() {
131 return DocumentFullscreen::fullscreenElement(videoElement().document());
xjz 2017/02/22 18:48:34 Can the fullscreen element be the child of the vid
Zhiqiang Zhang (Slow) 2017/02/23 21:50:55 This should be rare and we are focusing on the typ
Zhiqiang Zhang (Slow) 2017/02/24 12:03:40 I reverted back to isVideoOrParentFullscreen(). No
xjz 2017/02/24 18:30:10 The logic sgtm. Just ooc, if the video is the curr
Zhiqiang Zhang (Slow) 2017/02/25 18:48:27 That would bring in several more if-else's. I'd pr
132 }
133
134 DEFINE_TRACE(MediaCustomControlsFullscreenDetector) {
135 EventListener::trace(visitor);
136 visitor->trace(m_videoElement);
137 }
138
139 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698