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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/html/MediaCustomControlsFullscreenDetector.cpp
diff --git a/third_party/WebKit/Source/core/html/MediaCustomControlsFullscreenDetector.cpp b/third_party/WebKit/Source/core/html/MediaCustomControlsFullscreenDetector.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..e6933289f15dfbf943f210191f97ba047b7653cb
--- /dev/null
+++ b/third_party/WebKit/Source/core/html/MediaCustomControlsFullscreenDetector.cpp
@@ -0,0 +1,139 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "core/html/MediaCustomControlsFullscreenDetector.h"
+
+#include "core/dom/DocumentFullscreen.h"
+#include "core/dom/TaskRunnerHelper.h"
+#include "core/events/Event.h"
+#include "core/html/HTMLVideoElement.h"
+#include "core/layout/IntersectionGeometry.h"
+
+namespace blink {
+
+namespace {
+
+constexpr double kCheckFullscreenIntervalSeconds = 1.0f;
+constexpr float kMostlyFillViewportThresholdOfOccupationProportion = 0.85f;
+constexpr float kMostlyFillViewportThresholdOfVisibleProportion = 0.75f;
+
+} // anonymous namespace
+
+MediaCustomControlsFullscreenDetector::MediaCustomControlsFullscreenDetector(
+ HTMLVideoElement& video)
+ : EventListener(CPPEventListenerType),
+ m_videoElement(video),
+ m_checkViewportIntersectionTimer(
+ TaskRunnerHelper::get(TaskType::Unthrottled, &video.document()),
+ this,
+ &MediaCustomControlsFullscreenDetector::
+ onCheckViewportIntersectionTimerFired) {
+ videoElement().document().addEventListener(
+ EventTypeNames::webkitfullscreenchange, this, true);
+ videoElement().document().addEventListener(EventTypeNames::fullscreenchange,
+ this, true);
+
+ videoElement().addEventListener(EventTypeNames::loadedmetadata, this, true);
+}
+
+bool MediaCustomControlsFullscreenDetector::operator==(
+ const EventListener& other) const {
+ return this == &other;
+}
+
+void MediaCustomControlsFullscreenDetector::didMoveToNewDocument(
+ Document& oldDocument) {
+ oldDocument.removeEventListener(EventTypeNames::webkitfullscreenchange, this,
+ true);
+ oldDocument.removeEventListener(EventTypeNames::fullscreenchange, this, true);
+
+ videoElement().document().addEventListener(
+ EventTypeNames::webkitfullscreenchange, this, true);
+ videoElement().document().addEventListener(EventTypeNames::fullscreenchange,
+ this, true);
+}
+
+bool MediaCustomControlsFullscreenDetector::computeIsDominantVideo(
+ const IntRect& targetRect,
+ const IntRect& rootRect,
+ const IntRect& intersectionRect) {
+ const float xOccupationProportion =
+ 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
+ const float yOccupationProportion =
+ 1.0f * intersectionRect.height() / rootRect.height();
+
+ // If the viewport is mostly occupied by the video, return true.
+ if (std::min(xOccupationProportion, yOccupationProportion) >=
+ kMostlyFillViewportThresholdOfOccupationProportion) {
+ return true;
+ }
+
+ // If neither of the dimensions of the viewport is mostly occupied by the
+ // video, return false.
+ if (std::max(xOccupationProportion, yOccupationProportion) <
+ kMostlyFillViewportThresholdOfOccupationProportion) {
+ return false;
+ }
+
+ // If the video is mostly visible in the indominant dimension, return true.
+ // Otherwise return false.
+ if (xOccupationProportion > yOccupationProportion) {
+ return targetRect.height() *
+ kMostlyFillViewportThresholdOfVisibleProportion <
+ intersectionRect.height();
+ }
+ return targetRect.width() * kMostlyFillViewportThresholdOfVisibleProportion <
+ intersectionRect.width();
+}
+
+void MediaCustomControlsFullscreenDetector::handleEvent(
+ ExecutionContext* context,
+ Event* event) {
+ DCHECK(event->type() == EventTypeNames::fullscreenchange ||
+ event->type() == EventTypeNames::webkitfullscreenchange ||
+ event->type() == EventTypeNames::loadedmetadata);
+
+ // Video is not loaded yet.
+ if (videoElement().getReadyState() < HTMLMediaElement::kHaveMetadata)
+ return;
+
+ if (!isVideoOrParentFullscreen()) {
+ m_checkViewportIntersectionTimer.stop();
+
+ if (videoElement().webMediaPlayer()) {
+ videoElement().webMediaPlayer()->setEffectivelyFullscreen(false);
+ }
+ return;
+ }
+
+ m_checkViewportIntersectionTimer.startOneShot(kCheckFullscreenIntervalSeconds,
+ BLINK_FROM_HERE);
+}
+
+void MediaCustomControlsFullscreenDetector::
+ onCheckViewportIntersectionTimerFired(TimerBase*) {
+ DCHECK(isVideoOrParentFullscreen());
+ IntersectionGeometry geometry(nullptr, videoElement(), Vector<Length>(),
+ true);
+ geometry.computeGeometry();
+
+ bool isDominant =
+ computeIsDominantVideo(geometry.targetIntRect(), geometry.rootIntRect(),
+ geometry.intersectionIntRect());
+
+ if (videoElement().webMediaPlayer()) {
+ videoElement().webMediaPlayer()->setEffectivelyFullscreen(isDominant);
+ }
+}
+
+bool MediaCustomControlsFullscreenDetector::isVideoOrParentFullscreen() {
+ 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
+}
+
+DEFINE_TRACE(MediaCustomControlsFullscreenDetector) {
+ EventListener::trace(visitor);
+ visitor->trace(m_videoElement);
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698