blob: f0a7aaac908db0fcf62bf3dd66ee1984f9e4a1b1 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// Copyright (c) 2012 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 "ash/wm/video_detector.h"
6
7#include "ash/shell.h"
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +00008#include "ash/shell_window_ids.h"
Torne (Richard Coles)68043e12013-09-26 13:24:57 +01009#include "ash/wm/window_state.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000010#include "ui/aura/env.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000011#include "ui/aura/window.h"
Torne (Richard Coles)a1401312014-03-18 10:20:56 +000012#include "ui/aura/window_event_dispatcher.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000013#include "ui/gfx/rect.h"
Torne (Richard Coles)a1401312014-03-18 10:20:56 +000014#include "ui/wm/core/window_util.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000015
16namespace ash {
17
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010018const int VideoDetector::kMinUpdateWidth = 333;
19const int VideoDetector::kMinUpdateHeight = 250;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000020const int VideoDetector::kMinFramesPerSecond = 15;
21const double VideoDetector::kNotifyIntervalSec = 1.0;
22
23// Stores information about updates to a window and determines whether it's
24// likely that a video is playing in it.
25class VideoDetector::WindowInfo {
26 public:
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010027 WindowInfo() : buffer_start_(0), buffer_size_(0) {}
Torne (Richard Coles)58218062012-11-14 11:43:16 +000028
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010029 // Handles an update within a window, returning true if it appears that
30 // video is currently playing in the window.
Torne (Richard Coles)58218062012-11-14 11:43:16 +000031 bool RecordUpdateAndCheckForVideo(const gfx::Rect& region,
32 base::TimeTicks now) {
33 if (region.width() < kMinUpdateWidth || region.height() < kMinUpdateHeight)
34 return false;
35
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010036 // If the buffer is full, drop the first timestamp.
37 if (buffer_size_ == static_cast<size_t>(kMinFramesPerSecond)) {
38 buffer_start_ = (buffer_start_ + 1) % kMinFramesPerSecond;
39 buffer_size_--;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000040 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +000041
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010042 update_times_[(buffer_start_ + buffer_size_) % kMinFramesPerSecond] = now;
43 buffer_size_++;
44
45 return buffer_size_ == static_cast<size_t>(kMinFramesPerSecond) &&
46 (now - update_times_[buffer_start_]).InSecondsF() <= 1.0;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000047 }
48
49 private:
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010050 // Circular buffer containing update times of the last (up to
51 // |kMinFramesPerSecond|) video-sized updates to this window.
52 base::TimeTicks update_times_[kMinFramesPerSecond];
Torne (Richard Coles)58218062012-11-14 11:43:16 +000053
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010054 // Index into |update_times_| of the oldest update.
55 size_t buffer_start_;
56
57 // Number of updates stored in |update_times_|.
58 size_t buffer_size_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000059
60 DISALLOW_COPY_AND_ASSIGN(WindowInfo);
61};
62
63VideoDetector::VideoDetector()
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010064 : observer_manager_(this),
65 is_shutting_down_(false) {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000066 aura::Env::GetInstance()->AddObserver(this);
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010067 Shell::GetInstance()->AddShellObserver(this);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000068}
69
70VideoDetector::~VideoDetector() {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010071 Shell::GetInstance()->RemoveShellObserver(this);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000072 aura::Env::GetInstance()->RemoveObserver(this);
73}
74
75void VideoDetector::AddObserver(VideoDetectorObserver* observer) {
76 observers_.AddObserver(observer);
77}
78
79void VideoDetector::RemoveObserver(VideoDetectorObserver* observer) {
80 observers_.RemoveObserver(observer);
81}
82
83void VideoDetector::OnWindowInitialized(aura::Window* window) {
84 observer_manager_.Add(window);
85}
86
87void VideoDetector::OnWindowPaintScheduled(aura::Window* window,
88 const gfx::Rect& region) {
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010089 if (is_shutting_down_)
90 return;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000091 linked_ptr<WindowInfo>& info = window_infos_[window];
92 if (!info.get())
93 info.reset(new WindowInfo);
94
95 base::TimeTicks now =
96 !now_for_test_.is_null() ? now_for_test_ : base::TimeTicks::Now();
97 if (info->RecordUpdateAndCheckForVideo(region, now))
98 MaybeNotifyObservers(window, now);
99}
100
101void VideoDetector::OnWindowDestroyed(aura::Window* window) {
102 window_infos_.erase(window);
103 observer_manager_.Remove(window);
104}
105
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100106void VideoDetector::OnAppTerminating() {
107 // Stop checking video activity once the shutdown
108 // process starts. crbug.com/231696.
109 is_shutting_down_ = true;
110}
111
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000112void VideoDetector::MaybeNotifyObservers(aura::Window* window,
113 base::TimeTicks now) {
114 if (!last_observer_notification_time_.is_null() &&
115 (now - last_observer_notification_time_).InSecondsF() <
116 kNotifyIntervalSec)
117 return;
118
119 if (!window->IsVisible())
120 return;
121
122 gfx::Rect root_bounds = window->GetRootWindow()->bounds();
123 if (!window->GetBoundsInRootWindow().Intersects(root_bounds))
124 return;
125
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000126 // As a relatively-cheap way to avoid flipping back and forth between
127 // fullscreen and non-fullscreen notifications when one video is playing in a
128 // fullscreen window and a second video is playing in a non-fullscreen window,
129 // report fullscreen video whenever a fullscreen window exists on any desktop
130 // regardless of whether the video is actually playing in that window:
131 // http://crbug.com/340666
132 bool fullscreen_window_exists = false;
133 std::vector<aura::Window*> containers =
Ben Murdochc5cede92014-04-10 11:22:14 +0100134 Shell::GetContainersFromAllRootWindows(kShellWindowId_DefaultContainer,
135 NULL);
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000136 for (std::vector<aura::Window*>::const_iterator container =
137 containers.begin(); container != containers.end(); ++container) {
138 const aura::Window::Windows& windows = (*container)->children();
139 for (aura::Window::Windows::const_iterator window = windows.begin();
140 window != windows.end(); ++window) {
141 if (wm::GetWindowState(*window)->IsFullscreen()) {
142 fullscreen_window_exists = true;
143 break;
144 }
145 }
146 }
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000147
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000148 FOR_EACH_OBSERVER(VideoDetectorObserver,
149 observers_,
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000150 OnVideoDetected(fullscreen_window_exists));
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000151 last_observer_notification_time_ = now;
152}
153
154} // namespace ash