Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / ash / wm / video_detector.cc
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"
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"
15
16 namespace ash {
17
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;
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:
27   WindowInfo() : buffer_start_(0), buffer_size_(0) {}
28
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)
34       return false;
35
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_--;
40     }
41
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;
47   }
48
49  private:
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];
53
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_;
59
60   DISALLOW_COPY_AND_ASSIGN(WindowInfo);
61 };
62
63 VideoDetector::VideoDetector()
64     : observer_manager_(this),
65       is_shutting_down_(false) {
66   aura::Env::GetInstance()->AddObserver(this);
67   Shell::GetInstance()->AddShellObserver(this);
68 }
69
70 VideoDetector::~VideoDetector() {
71   Shell::GetInstance()->RemoveShellObserver(this);
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) {
89   if (is_shutting_down_)
90     return;
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
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
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
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,
135                                              NULL);
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   }
147
148   FOR_EACH_OBSERVER(VideoDetectorObserver,
149                     observers_,
150                     OnVideoDetected(fullscreen_window_exists));
151   last_observer_notification_time_ = now;
152 }
153
154 }  // namespace ash