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_state.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"
13 #include "ui/views/corewm/window_util.h"
17 const int VideoDetector::kMinUpdateWidth
= 333;
18 const int VideoDetector::kMinUpdateHeight
= 250;
19 const int VideoDetector::kMinFramesPerSecond
= 15;
20 const double VideoDetector::kNotifyIntervalSec
= 1.0;
22 // Stores information about updates to a window and determines whether it's
23 // likely that a video is playing in it.
24 class VideoDetector::WindowInfo
{
26 WindowInfo() : buffer_start_(0), buffer_size_(0) {}
28 // Handles an update within a window, returning true if it appears that
29 // video is currently playing in the window.
30 bool RecordUpdateAndCheckForVideo(const gfx::Rect
& region
,
31 base::TimeTicks now
) {
32 if (region
.width() < kMinUpdateWidth
|| region
.height() < kMinUpdateHeight
)
35 // If the buffer is full, drop the first timestamp.
36 if (buffer_size_
== static_cast<size_t>(kMinFramesPerSecond
)) {
37 buffer_start_
= (buffer_start_
+ 1) % kMinFramesPerSecond
;
41 update_times_
[(buffer_start_
+ buffer_size_
) % kMinFramesPerSecond
] = now
;
44 return buffer_size_
== static_cast<size_t>(kMinFramesPerSecond
) &&
45 (now
- update_times_
[buffer_start_
]).InSecondsF() <= 1.0;
49 // Circular buffer containing update times of the last (up to
50 // |kMinFramesPerSecond|) video-sized updates to this window.
51 base::TimeTicks update_times_
[kMinFramesPerSecond
];
53 // Index into |update_times_| of the oldest update.
56 // Number of updates stored in |update_times_|.
59 DISALLOW_COPY_AND_ASSIGN(WindowInfo
);
62 VideoDetector::VideoDetector()
63 : observer_manager_(this),
64 is_shutting_down_(false) {
65 aura::Env::GetInstance()->AddObserver(this);
66 Shell::GetInstance()->AddShellObserver(this);
69 VideoDetector::~VideoDetector() {
70 Shell::GetInstance()->RemoveShellObserver(this);
71 aura::Env::GetInstance()->RemoveObserver(this);
74 void VideoDetector::AddObserver(VideoDetectorObserver
* observer
) {
75 observers_
.AddObserver(observer
);
78 void VideoDetector::RemoveObserver(VideoDetectorObserver
* observer
) {
79 observers_
.RemoveObserver(observer
);
82 void VideoDetector::OnWindowInitialized(aura::Window
* window
) {
83 observer_manager_
.Add(window
);
86 void VideoDetector::OnWindowPaintScheduled(aura::Window
* window
,
87 const gfx::Rect
& region
) {
88 if (is_shutting_down_
)
90 linked_ptr
<WindowInfo
>& info
= window_infos_
[window
];
92 info
.reset(new WindowInfo
);
95 !now_for_test_
.is_null() ? now_for_test_
: base::TimeTicks::Now();
96 if (info
->RecordUpdateAndCheckForVideo(region
, now
))
97 MaybeNotifyObservers(window
, now
);
100 void VideoDetector::OnWindowDestroyed(aura::Window
* window
) {
101 window_infos_
.erase(window
);
102 observer_manager_
.Remove(window
);
105 void VideoDetector::OnAppTerminating() {
106 // Stop checking video activity once the shutdown
107 // process starts. crbug.com/231696.
108 is_shutting_down_
= true;
111 void VideoDetector::MaybeNotifyObservers(aura::Window
* window
,
112 base::TimeTicks now
) {
113 if (!last_observer_notification_time_
.is_null() &&
114 (now
- last_observer_notification_time_
).InSecondsF() <
118 if (!window
->IsVisible())
121 gfx::Rect root_bounds
= window
->GetRootWindow()->bounds();
122 if (!window
->GetBoundsInRootWindow().Intersects(root_bounds
))
125 aura::Window
* toplevel_window
= views::corewm::GetToplevelWindow(window
);
126 bool is_fullscreen
= toplevel_window
?
127 wm::GetWindowState(toplevel_window
)->IsFullscreen() : false;
129 FOR_EACH_OBSERVER(VideoDetectorObserver
,
131 OnVideoDetected(is_fullscreen
));
132 last_observer_notification_time_
= now
;