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/wm/window_util.h"
9 #include "ui/aura/env.h"
10 #include "ui/aura/root_window.h"
11 #include "ui/aura/window.h"
12 #include "ui/gfx/rect.h"
16 const int VideoDetector::kMinUpdateWidth
= 333;
17 const int VideoDetector::kMinUpdateHeight
= 250;
18 const int VideoDetector::kMinFramesPerSecond
= 15;
19 const double VideoDetector::kNotifyIntervalSec
= 1.0;
21 // Stores information about updates to a window and determines whether it's
22 // likely that a video is playing in it.
23 class VideoDetector::WindowInfo
{
25 WindowInfo() : buffer_start_(0), buffer_size_(0) {}
27 // Handles an update within a window, returning true if it appears that
28 // video is currently playing in the window.
29 bool RecordUpdateAndCheckForVideo(const gfx::Rect
& region
,
30 base::TimeTicks now
) {
31 if (region
.width() < kMinUpdateWidth
|| region
.height() < kMinUpdateHeight
)
34 // If the buffer is full, drop the first timestamp.
35 if (buffer_size_
== static_cast<size_t>(kMinFramesPerSecond
)) {
36 buffer_start_
= (buffer_start_
+ 1) % kMinFramesPerSecond
;
40 update_times_
[(buffer_start_
+ buffer_size_
) % kMinFramesPerSecond
] = now
;
43 return buffer_size_
== static_cast<size_t>(kMinFramesPerSecond
) &&
44 (now
- update_times_
[buffer_start_
]).InSecondsF() <= 1.0;
48 // Circular buffer containing update times of the last (up to
49 // |kMinFramesPerSecond|) video-sized updates to this window.
50 base::TimeTicks update_times_
[kMinFramesPerSecond
];
52 // Index into |update_times_| of the oldest update.
55 // Number of updates stored in |update_times_|.
58 DISALLOW_COPY_AND_ASSIGN(WindowInfo
);
61 VideoDetector::VideoDetector()
62 : observer_manager_(this),
63 is_shutting_down_(false) {
64 aura::Env::GetInstance()->AddObserver(this);
65 Shell::GetInstance()->AddShellObserver(this);
68 VideoDetector::~VideoDetector() {
69 Shell::GetInstance()->RemoveShellObserver(this);
70 aura::Env::GetInstance()->RemoveObserver(this);
73 void VideoDetector::AddObserver(VideoDetectorObserver
* observer
) {
74 observers_
.AddObserver(observer
);
77 void VideoDetector::RemoveObserver(VideoDetectorObserver
* observer
) {
78 observers_
.RemoveObserver(observer
);
81 void VideoDetector::OnWindowInitialized(aura::Window
* window
) {
82 observer_manager_
.Add(window
);
85 void VideoDetector::OnWindowPaintScheduled(aura::Window
* window
,
86 const gfx::Rect
& region
) {
87 if (is_shutting_down_
)
89 linked_ptr
<WindowInfo
>& info
= window_infos_
[window
];
91 info
.reset(new WindowInfo
);
94 !now_for_test_
.is_null() ? now_for_test_
: base::TimeTicks::Now();
95 if (info
->RecordUpdateAndCheckForVideo(region
, now
))
96 MaybeNotifyObservers(window
, now
);
99 void VideoDetector::OnWindowDestroyed(aura::Window
* window
) {
100 window_infos_
.erase(window
);
101 observer_manager_
.Remove(window
);
104 void VideoDetector::OnAppTerminating() {
105 // Stop checking video activity once the shutdown
106 // process starts. crbug.com/231696.
107 is_shutting_down_
= true;
110 void VideoDetector::MaybeNotifyObservers(aura::Window
* window
,
111 base::TimeTicks now
) {
112 if (!last_observer_notification_time_
.is_null() &&
113 (now
- last_observer_notification_time_
).InSecondsF() <
117 if (!window
->IsVisible())
120 gfx::Rect root_bounds
= window
->GetRootWindow()->bounds();
121 if (!window
->GetBoundsInRootWindow().Intersects(root_bounds
))
124 aura::Window
* toplevel_window
= wm::GetActivatableWindow(window
);
126 toplevel_window
? wm::IsWindowFullscreen(toplevel_window
) : false;
128 FOR_EACH_OBSERVER(VideoDetectorObserver
,
130 OnVideoDetected(is_fullscreen
));
131 last_observer_notification_time_
= now
;