1 // 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.
5 #include "ash/wm/video_detector.h"
8 #include "ash/shell_window_ids.h"
9 #include "ash/wm/window_state.h"
10 #include "ui/aura/env.h"
11 #include "ui/aura/window.h"
12 #include "ui/aura/window_event_dispatcher.h"
13 #include "ui/gfx/rect.h"
14 #include "ui/wm/core/window_util.h"
18 const int VideoDetector::kMinUpdateWidth
= 333;
19 const int VideoDetector::kMinUpdateHeight
= 250;
20 const int VideoDetector::kMinFramesPerSecond
= 15;
21 const double VideoDetector::kNotifyIntervalSec
= 1.0;
23 // Stores information about updates to a window and determines whether it's
24 // likely that a video is playing in it.
25 class VideoDetector::WindowInfo
{
27 WindowInfo() : buffer_start_(0), buffer_size_(0) {}
29 // Handles an update within a window, returning true if it appears that
30 // video is currently playing in the window.
31 bool RecordUpdateAndCheckForVideo(const gfx::Rect
& region
,
32 base::TimeTicks now
) {
33 if (region
.width() < kMinUpdateWidth
|| region
.height() < kMinUpdateHeight
)
36 // 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
;
42 update_times_
[(buffer_start_
+ buffer_size_
) % kMinFramesPerSecond
] = now
;
45 return buffer_size_
== static_cast<size_t>(kMinFramesPerSecond
) &&
46 (now
- update_times_
[buffer_start_
]).InSecondsF() <= 1.0;
50 // Circular buffer containing update times of the last (up to
51 // |kMinFramesPerSecond|) video-sized updates to this window.
52 base::TimeTicks update_times_
[kMinFramesPerSecond
];
54 // Index into |update_times_| of the oldest update.
57 // Number of updates stored in |update_times_|.
60 DISALLOW_COPY_AND_ASSIGN(WindowInfo
);
63 VideoDetector::VideoDetector()
64 : observer_manager_(this),
65 is_shutting_down_(false) {
66 aura::Env::GetInstance()->AddObserver(this);
67 Shell::GetInstance()->AddShellObserver(this);
70 VideoDetector::~VideoDetector() {
71 Shell::GetInstance()->RemoveShellObserver(this);
72 aura::Env::GetInstance()->RemoveObserver(this);
75 void VideoDetector::AddObserver(VideoDetectorObserver
* observer
) {
76 observers_
.AddObserver(observer
);
79 void VideoDetector::RemoveObserver(VideoDetectorObserver
* observer
) {
80 observers_
.RemoveObserver(observer
);
83 void VideoDetector::OnWindowInitialized(aura::Window
* window
) {
84 observer_manager_
.Add(window
);
87 void VideoDetector::OnWindowPaintScheduled(aura::Window
* window
,
88 const gfx::Rect
& region
) {
89 if (is_shutting_down_
)
91 linked_ptr
<WindowInfo
>& info
= window_infos_
[window
];
93 info
.reset(new WindowInfo
);
96 !now_for_test_
.is_null() ? now_for_test_
: base::TimeTicks::Now();
97 if (info
->RecordUpdateAndCheckForVideo(region
, now
))
98 MaybeNotifyObservers(window
, now
);
101 void VideoDetector::OnWindowDestroyed(aura::Window
* window
) {
102 window_infos_
.erase(window
);
103 observer_manager_
.Remove(window
);
106 void VideoDetector::OnAppTerminating() {
107 // Stop checking video activity once the shutdown
108 // process starts. crbug.com/231696.
109 is_shutting_down_
= true;
112 void VideoDetector::MaybeNotifyObservers(aura::Window
* window
,
113 base::TimeTicks now
) {
114 if (!last_observer_notification_time_
.is_null() &&
115 (now
- last_observer_notification_time_
).InSecondsF() <
119 if (!window
->IsVisible())
122 gfx::Rect root_bounds
= window
->GetRootWindow()->bounds();
123 if (!window
->GetBoundsInRootWindow().Intersects(root_bounds
))
126 // 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
=
134 Shell::GetContainersFromAllRootWindows(kShellWindowId_DefaultContainer
,
136 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;
148 FOR_EACH_OBSERVER(VideoDetectorObserver
,
150 OnVideoDetected(fullscreen_window_exists
));
151 last_observer_notification_time_
= now
;