Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "ash/wm/video_detector.h" |
| 6 | |
| 7 | #include "ash/shell.h" |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 8 | #include "ash/shell_window_ids.h" |
Torne (Richard Coles) | 68043e1 | 2013-09-26 13:24:57 +0100 | [diff] [blame] | 9 | #include "ash/wm/window_state.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 10 | #include "ui/aura/env.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 11 | #include "ui/aura/window.h" |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 12 | #include "ui/aura/window_event_dispatcher.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 13 | #include "ui/gfx/rect.h" |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 14 | #include "ui/wm/core/window_util.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 15 | |
| 16 | namespace ash { |
| 17 | |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 18 | const int VideoDetector::kMinUpdateWidth = 333; |
| 19 | const int VideoDetector::kMinUpdateHeight = 250; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 20 | const int VideoDetector::kMinFramesPerSecond = 15; |
| 21 | const 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. |
| 25 | class VideoDetector::WindowInfo { |
| 26 | public: |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 27 | WindowInfo() : buffer_start_(0), buffer_size_(0) {} |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 28 | |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 29 | // Handles an update within a window, returning true if it appears that |
| 30 | // video is currently playing in the window. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 31 | 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) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 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; |
| 39 | buffer_size_--; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 40 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 41 | |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 42 | 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) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | private: |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 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]; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 53 | |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 54 | // 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) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 59 | |
| 60 | DISALLOW_COPY_AND_ASSIGN(WindowInfo); |
| 61 | }; |
| 62 | |
| 63 | VideoDetector::VideoDetector() |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 64 | : observer_manager_(this), |
| 65 | is_shutting_down_(false) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 66 | aura::Env::GetInstance()->AddObserver(this); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 67 | Shell::GetInstance()->AddShellObserver(this); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | VideoDetector::~VideoDetector() { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 71 | Shell::GetInstance()->RemoveShellObserver(this); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 72 | aura::Env::GetInstance()->RemoveObserver(this); |
| 73 | } |
| 74 | |
| 75 | void VideoDetector::AddObserver(VideoDetectorObserver* observer) { |
| 76 | observers_.AddObserver(observer); |
| 77 | } |
| 78 | |
| 79 | void VideoDetector::RemoveObserver(VideoDetectorObserver* observer) { |
| 80 | observers_.RemoveObserver(observer); |
| 81 | } |
| 82 | |
| 83 | void VideoDetector::OnWindowInitialized(aura::Window* window) { |
| 84 | observer_manager_.Add(window); |
| 85 | } |
| 86 | |
| 87 | void VideoDetector::OnWindowPaintScheduled(aura::Window* window, |
| 88 | const gfx::Rect& region) { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 89 | if (is_shutting_down_) |
| 90 | return; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 91 | 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 | |
| 101 | void VideoDetector::OnWindowDestroyed(aura::Window* window) { |
| 102 | window_infos_.erase(window); |
| 103 | observer_manager_.Remove(window); |
| 104 | } |
| 105 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 106 | void 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) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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() < |
| 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) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 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 = |
Ben Murdoch | c5cede9 | 2014-04-10 11:22:14 +0100 | [diff] [blame] | 134 | Shell::GetContainersFromAllRootWindows(kShellWindowId_DefaultContainer, |
| 135 | NULL); |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 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; |
| 143 | break; |
| 144 | } |
| 145 | } |
| 146 | } |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 147 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 148 | FOR_EACH_OBSERVER(VideoDetectorObserver, |
| 149 | observers_, |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 150 | OnVideoDetected(fullscreen_window_exists)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 151 | last_observer_notification_time_ = now; |
| 152 | } |
| 153 | |
| 154 | } // namespace ash |