OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/media/media_web_contents_observer.h" | 5 #include "content/browser/media/media_web_contents_observer.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
10 #include "content/browser/media/audible_metrics.h" | 10 #include "content/browser/media/audible_metrics.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 web_contents(), audio_stream_monitor->IsCurrentlyAudible()); | 57 web_contents(), audio_stream_monitor->IsCurrentlyAudible()); |
58 } | 58 } |
59 | 59 |
60 bool MediaWebContentsObserver::OnMessageReceived( | 60 bool MediaWebContentsObserver::OnMessageReceived( |
61 const IPC::Message& msg, | 61 const IPC::Message& msg, |
62 RenderFrameHost* render_frame_host) { | 62 RenderFrameHost* render_frame_host) { |
63 bool handled = true; | 63 bool handled = true; |
64 // TODO(dalecurtis): These should no longer be FrameHostMsg. | 64 // TODO(dalecurtis): These should no longer be FrameHostMsg. |
65 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(MediaWebContentsObserver, msg, | 65 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(MediaWebContentsObserver, msg, |
66 render_frame_host) | 66 render_frame_host) |
| 67 IPC_MESSAGE_HANDLER(FrameHostMsg_MediaDestroyedNotification, |
| 68 OnMediaDestroyedNotification) |
67 IPC_MESSAGE_HANDLER(FrameHostMsg_MediaPlayingNotification, | 69 IPC_MESSAGE_HANDLER(FrameHostMsg_MediaPlayingNotification, |
68 OnMediaPlayingNotification) | 70 OnMediaPlayingNotification) |
69 IPC_MESSAGE_HANDLER(FrameHostMsg_MediaPausedNotification, | 71 IPC_MESSAGE_HANDLER(FrameHostMsg_MediaPausedNotification, |
70 OnMediaPausedNotification) | 72 OnMediaPausedNotification) |
71 IPC_MESSAGE_UNHANDLED(handled = false) | 73 IPC_MESSAGE_UNHANDLED(handled = false) |
72 IPC_END_MESSAGE_MAP() | 74 IPC_END_MESSAGE_MAP() |
73 return handled; | 75 return handled; |
74 } | 76 } |
75 | 77 |
| 78 void MediaWebContentsObserver::WasShown() { |
| 79 // Restore power save blocker if there are active video players running. |
| 80 if (!active_video_players_.empty() && !video_power_save_blocker_) |
| 81 CreateVideoPowerSaveBlocker(); |
| 82 } |
| 83 |
| 84 void MediaWebContentsObserver::WasHidden() { |
| 85 // If there are entities capturing screenshots or video (e.g., mirroring), |
| 86 // don't release the power save blocker. |
| 87 if (!web_contents()->GetCapturerCount()) |
| 88 video_power_save_blocker_.reset(); |
| 89 } |
| 90 |
| 91 void MediaWebContentsObserver::OnMediaDestroyedNotification( |
| 92 RenderFrameHost* render_frame_host, |
| 93 int64_t player_cookie) { |
| 94 OnMediaPausedNotification(render_frame_host, player_cookie, true); |
| 95 } |
| 96 |
76 void MediaWebContentsObserver::OnMediaPlayingNotification( | 97 void MediaWebContentsObserver::OnMediaPlayingNotification( |
77 RenderFrameHost* render_frame_host, | 98 RenderFrameHost* render_frame_host, |
78 int64_t player_cookie, | 99 int64_t player_cookie, |
79 bool has_video, | 100 bool has_video, |
80 bool has_audio, | 101 bool has_audio, |
81 bool is_remote) { | 102 bool is_remote, |
| 103 base::TimeDelta duration) { |
82 // Ignore the videos playing remotely and don't hold the wake lock for the | 104 // Ignore the videos playing remotely and don't hold the wake lock for the |
83 // screen. TODO(dalecurtis): Is this correct? It means observers will not | 105 // screen. TODO(dalecurtis): Is this correct? It means observers will not |
84 // receive play and pause messages. | 106 // receive play and pause messages. |
85 if (is_remote) | 107 if (is_remote) |
86 return; | 108 return; |
87 | 109 |
88 const MediaPlayerId id(render_frame_host, player_cookie); | 110 const MediaPlayerId id(render_frame_host, player_cookie); |
89 if (has_audio) { | 111 if (has_audio) { |
90 AddMediaPlayerEntry(id, &active_audio_players_); | 112 AddMediaPlayerEntry(id, &active_audio_players_); |
91 | 113 |
(...skipping 15 matching lines...) Expand all Loading... |
107 } | 129 } |
108 } | 130 } |
109 | 131 |
110 // Notify observers of the new player. | 132 // Notify observers of the new player. |
111 DCHECK(has_audio || has_video); | 133 DCHECK(has_audio || has_video); |
112 static_cast<WebContentsImpl*>(web_contents())->MediaStartedPlaying(id); | 134 static_cast<WebContentsImpl*>(web_contents())->MediaStartedPlaying(id); |
113 } | 135 } |
114 | 136 |
115 void MediaWebContentsObserver::OnMediaPausedNotification( | 137 void MediaWebContentsObserver::OnMediaPausedNotification( |
116 RenderFrameHost* render_frame_host, | 138 RenderFrameHost* render_frame_host, |
117 int64_t player_cookie) { | 139 int64_t player_cookie, |
| 140 bool reached_end_of_stream) { |
118 const MediaPlayerId id(render_frame_host, player_cookie); | 141 const MediaPlayerId id(render_frame_host, player_cookie); |
119 const bool removed_audio = RemoveMediaPlayerEntry(id, &active_audio_players_); | 142 const bool removed_audio = RemoveMediaPlayerEntry(id, &active_audio_players_); |
120 const bool removed_video = RemoveMediaPlayerEntry(id, &active_video_players_); | 143 const bool removed_video = RemoveMediaPlayerEntry(id, &active_video_players_); |
121 MaybeReleasePowerSaveBlockers(); | 144 MaybeReleasePowerSaveBlockers(); |
122 | 145 |
123 if (removed_audio || removed_video) { | 146 if (removed_audio || removed_video) { |
124 // Notify observers the player has been "paused". | 147 // Notify observers the player has been "paused". |
125 static_cast<WebContentsImpl*>(web_contents())->MediaStoppedPlaying(id); | 148 static_cast<WebContentsImpl*>(web_contents())->MediaStoppedPlaying(id); |
126 } | 149 } |
127 } | 150 } |
(...skipping 26 matching lines...) Expand all Loading... |
154 video_power_save_blocker_ = PowerSaveBlocker::Create( | 177 video_power_save_blocker_ = PowerSaveBlocker::Create( |
155 PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep, | 178 PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep, |
156 PowerSaveBlocker::kReasonVideoPlayback, "Playing video"); | 179 PowerSaveBlocker::kReasonVideoPlayback, "Playing video"); |
157 // TODO(mfomitchev): Support PowerSaveBlocker on Aura - crbug.com/546718. | 180 // TODO(mfomitchev): Support PowerSaveBlocker on Aura - crbug.com/546718. |
158 #if defined(OS_ANDROID) && !defined(USE_AURA) | 181 #if defined(OS_ANDROID) && !defined(USE_AURA) |
159 static_cast<PowerSaveBlockerImpl*>(video_power_save_blocker_.get()) | 182 static_cast<PowerSaveBlockerImpl*>(video_power_save_blocker_.get()) |
160 ->InitDisplaySleepBlocker(web_contents()); | 183 ->InitDisplaySleepBlocker(web_contents()); |
161 #endif | 184 #endif |
162 } | 185 } |
163 | 186 |
164 void MediaWebContentsObserver::WasShown() { | |
165 // Restore power save blocker if there are active video players running. | |
166 if (!active_video_players_.empty() && !video_power_save_blocker_) | |
167 CreateVideoPowerSaveBlocker(); | |
168 } | |
169 | |
170 void MediaWebContentsObserver::WasHidden() { | |
171 // If there are entities capturing screenshots or video (e.g., mirroring), | |
172 // don't release the power save blocker. | |
173 if (!web_contents()->GetCapturerCount()) | |
174 video_power_save_blocker_.reset(); | |
175 } | |
176 | |
177 void MediaWebContentsObserver::MaybeReleasePowerSaveBlockers() { | 187 void MediaWebContentsObserver::MaybeReleasePowerSaveBlockers() { |
178 // If there are no more audio players and we don't have audio stream | 188 // If there are no more audio players and we don't have audio stream |
179 // monitoring, release the audio power save blocker here instead of during | 189 // monitoring, release the audio power save blocker here instead of during |
180 // NotifyNavigationStateChanged(). | 190 // NotifyNavigationStateChanged(). |
181 if (active_audio_players_.empty() && | 191 if (active_audio_players_.empty() && |
182 !AudioStreamMonitor::monitoring_available()) { | 192 !AudioStreamMonitor::monitoring_available()) { |
183 audio_power_save_blocker_.reset(); | 193 audio_power_save_blocker_.reset(); |
184 } | 194 } |
185 | 195 |
186 // If there are no more video players, clear the video power save blocker. | 196 // If there are no more video players, clear the video power save blocker. |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 if (it == player_map->end()) | 236 if (it == player_map->end()) |
227 return; | 237 return; |
228 | 238 |
229 for (int64_t player_cookie : it->second) | 239 for (int64_t player_cookie : it->second) |
230 removed_players->insert(MediaPlayerId(render_frame_host, player_cookie)); | 240 removed_players->insert(MediaPlayerId(render_frame_host, player_cookie)); |
231 | 241 |
232 player_map->erase(it); | 242 player_map->erase(it); |
233 } | 243 } |
234 | 244 |
235 } // namespace content | 245 } // namespace content |
OLD | NEW |