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" |
11 #include "content/browser/media/audio_stream_monitor.h" | 11 #include "content/browser/media/audio_stream_monitor.h" |
12 #include "content/browser/power_save_blocker_impl.h" | 12 #include "content/browser/power_save_blocker_impl.h" |
13 #include "content/browser/web_contents/web_contents_impl.h" | 13 #include "content/browser/web_contents/web_contents_impl.h" |
14 #include "content/common/frame_messages.h" | 14 #include "content/common/media/media_player_delegate_messages.h" |
15 #include "content/public/browser/render_frame_host.h" | 15 #include "content/public/browser/render_frame_host.h" |
16 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
17 #include "ipc/ipc_message_macros.h" | 17 #include "ipc/ipc_message_macros.h" |
18 | 18 |
19 namespace content { | 19 namespace content { |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 static base::LazyInstance<AudibleMetrics>::Leaky g_audible_metrics = | 23 static base::LazyInstance<AudibleMetrics>::Leaky g_audible_metrics = |
24 LAZY_INSTANCE_INITIALIZER; | 24 LAZY_INSTANCE_INITIALIZER; |
(...skipping 32 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_MediaPlayingNotification, | 67 IPC_MESSAGE_HANDLER(MediaPlayerDelegateHostMsg_OnMediaDestroyed, |
68 OnMediaPlayingNotification) | 68 OnMediaDestroyed) |
69 IPC_MESSAGE_HANDLER(FrameHostMsg_MediaPausedNotification, | 69 IPC_MESSAGE_HANDLER(MediaPlayerDelegateHostMsg_OnMediaPlaying, |
70 OnMediaPausedNotification) | 70 OnMediaPlaying) |
| 71 IPC_MESSAGE_HANDLER(MediaPlayerDelegateHostMsg_OnMediaPaused, OnMediaPaused) |
71 IPC_MESSAGE_UNHANDLED(handled = false) | 72 IPC_MESSAGE_UNHANDLED(handled = false) |
72 IPC_END_MESSAGE_MAP() | 73 IPC_END_MESSAGE_MAP() |
73 return handled; | 74 return handled; |
74 } | 75 } |
75 | 76 |
76 void MediaWebContentsObserver::OnMediaPlayingNotification( | 77 void MediaWebContentsObserver::WasShown() { |
| 78 // Restore power save blocker if there are active video players running. |
| 79 if (!active_video_players_.empty() && !video_power_save_blocker_) |
| 80 CreateVideoPowerSaveBlocker(); |
| 81 } |
| 82 |
| 83 void MediaWebContentsObserver::WasHidden() { |
| 84 // If there are entities capturing screenshots or video (e.g., mirroring), |
| 85 // don't release the power save blocker. |
| 86 if (!web_contents()->GetCapturerCount()) |
| 87 video_power_save_blocker_.reset(); |
| 88 } |
| 89 |
| 90 void MediaWebContentsObserver::OnMediaDestroyed( |
77 RenderFrameHost* render_frame_host, | 91 RenderFrameHost* render_frame_host, |
78 int64_t player_cookie, | 92 int delegate_id) { |
| 93 OnMediaPaused(render_frame_host, delegate_id, true); |
| 94 } |
| 95 |
| 96 void MediaWebContentsObserver::OnMediaPlaying( |
| 97 RenderFrameHost* render_frame_host, |
| 98 int delegate_id, |
79 bool has_video, | 99 bool has_video, |
80 bool has_audio, | 100 bool has_audio, |
81 bool is_remote) { | 101 bool is_remote, |
| 102 base::TimeDelta duration) { |
82 // Ignore the videos playing remotely and don't hold the wake lock for the | 103 // 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 | 104 // screen. TODO(dalecurtis): Is this correct? It means observers will not |
84 // receive play and pause messages. | 105 // receive play and pause messages. |
85 if (is_remote) | 106 if (is_remote) |
86 return; | 107 return; |
87 | 108 |
88 const MediaPlayerId id(render_frame_host, player_cookie); | 109 const MediaPlayerId id(render_frame_host, delegate_id); |
89 if (has_audio) { | 110 if (has_audio) { |
90 AddMediaPlayerEntry(id, &active_audio_players_); | 111 AddMediaPlayerEntry(id, &active_audio_players_); |
91 | 112 |
92 // If we don't have audio stream monitoring, allocate the audio power save | 113 // If we don't have audio stream monitoring, allocate the audio power save |
93 // blocker here instead of during NotifyNavigationStateChanged(). | 114 // blocker here instead of during NotifyNavigationStateChanged(). |
94 if (!audio_power_save_blocker_ && | 115 if (!audio_power_save_blocker_ && |
95 !AudioStreamMonitor::monitoring_available()) { | 116 !AudioStreamMonitor::monitoring_available()) { |
96 CreateAudioPowerSaveBlocker(); | 117 CreateAudioPowerSaveBlocker(); |
97 } | 118 } |
98 } | 119 } |
99 | 120 |
100 if (has_video) { | 121 if (has_video) { |
101 AddMediaPlayerEntry(id, &active_video_players_); | 122 AddMediaPlayerEntry(id, &active_video_players_); |
102 | 123 |
103 // If we're not hidden and have just created a player, create a blocker. | 124 // If we're not hidden and have just created a player, create a blocker. |
104 if (!video_power_save_blocker_ && | 125 if (!video_power_save_blocker_ && |
105 !static_cast<WebContentsImpl*>(web_contents())->IsHidden()) { | 126 !static_cast<WebContentsImpl*>(web_contents())->IsHidden()) { |
106 CreateVideoPowerSaveBlocker(); | 127 CreateVideoPowerSaveBlocker(); |
107 } | 128 } |
108 } | 129 } |
109 | 130 |
110 // Notify observers of the new player. | 131 // Notify observers of the new player. |
111 DCHECK(has_audio || has_video); | 132 DCHECK(has_audio || has_video); |
112 static_cast<WebContentsImpl*>(web_contents())->MediaStartedPlaying(id); | 133 static_cast<WebContentsImpl*>(web_contents())->MediaStartedPlaying(id); |
113 } | 134 } |
114 | 135 |
115 void MediaWebContentsObserver::OnMediaPausedNotification( | 136 void MediaWebContentsObserver::OnMediaPaused(RenderFrameHost* render_frame_host, |
116 RenderFrameHost* render_frame_host, | 137 int delegate_id, |
117 int64_t player_cookie) { | 138 bool reached_end_of_stream) { |
118 const MediaPlayerId id(render_frame_host, player_cookie); | 139 const MediaPlayerId id(render_frame_host, delegate_id); |
119 const bool removed_audio = RemoveMediaPlayerEntry(id, &active_audio_players_); | 140 const bool removed_audio = RemoveMediaPlayerEntry(id, &active_audio_players_); |
120 const bool removed_video = RemoveMediaPlayerEntry(id, &active_video_players_); | 141 const bool removed_video = RemoveMediaPlayerEntry(id, &active_video_players_); |
121 MaybeReleasePowerSaveBlockers(); | 142 MaybeReleasePowerSaveBlockers(); |
122 | 143 |
123 if (removed_audio || removed_video) { | 144 if (removed_audio || removed_video) { |
124 // Notify observers the player has been "paused". | 145 // Notify observers the player has been "paused". |
125 static_cast<WebContentsImpl*>(web_contents())->MediaStoppedPlaying(id); | 146 static_cast<WebContentsImpl*>(web_contents())->MediaStoppedPlaying(id); |
126 } | 147 } |
127 } | 148 } |
128 | 149 |
(...skipping 25 matching lines...) Expand all Loading... |
154 video_power_save_blocker_ = PowerSaveBlocker::Create( | 175 video_power_save_blocker_ = PowerSaveBlocker::Create( |
155 PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep, | 176 PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep, |
156 PowerSaveBlocker::kReasonVideoPlayback, "Playing video"); | 177 PowerSaveBlocker::kReasonVideoPlayback, "Playing video"); |
157 // TODO(mfomitchev): Support PowerSaveBlocker on Aura - crbug.com/546718. | 178 // TODO(mfomitchev): Support PowerSaveBlocker on Aura - crbug.com/546718. |
158 #if defined(OS_ANDROID) && !defined(USE_AURA) | 179 #if defined(OS_ANDROID) && !defined(USE_AURA) |
159 static_cast<PowerSaveBlockerImpl*>(video_power_save_blocker_.get()) | 180 static_cast<PowerSaveBlockerImpl*>(video_power_save_blocker_.get()) |
160 ->InitDisplaySleepBlocker(web_contents()); | 181 ->InitDisplaySleepBlocker(web_contents()); |
161 #endif | 182 #endif |
162 } | 183 } |
163 | 184 |
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() { | 185 void MediaWebContentsObserver::MaybeReleasePowerSaveBlockers() { |
178 // If there are no more audio players and we don't have audio stream | 186 // 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 | 187 // monitoring, release the audio power save blocker here instead of during |
180 // NotifyNavigationStateChanged(). | 188 // NotifyNavigationStateChanged(). |
181 if (active_audio_players_.empty() && | 189 if (active_audio_players_.empty() && |
182 !AudioStreamMonitor::monitoring_available()) { | 190 !AudioStreamMonitor::monitoring_available()) { |
183 audio_power_save_blocker_.reset(); | 191 audio_power_save_blocker_.reset(); |
184 } | 192 } |
185 | 193 |
186 // If there are no more video players, clear the video power save blocker. | 194 // If there are no more video players, clear the video power save blocker. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 } | 227 } |
220 | 228 |
221 void MediaWebContentsObserver::RemoveAllMediaPlayerEntries( | 229 void MediaWebContentsObserver::RemoveAllMediaPlayerEntries( |
222 RenderFrameHost* render_frame_host, | 230 RenderFrameHost* render_frame_host, |
223 ActiveMediaPlayerMap* player_map, | 231 ActiveMediaPlayerMap* player_map, |
224 std::set<MediaPlayerId>* removed_players) { | 232 std::set<MediaPlayerId>* removed_players) { |
225 auto it = player_map->find(render_frame_host); | 233 auto it = player_map->find(render_frame_host); |
226 if (it == player_map->end()) | 234 if (it == player_map->end()) |
227 return; | 235 return; |
228 | 236 |
229 for (int64_t player_cookie : it->second) | 237 for (int delegate_id : it->second) |
230 removed_players->insert(MediaPlayerId(render_frame_host, player_cookie)); | 238 removed_players->insert(MediaPlayerId(render_frame_host, delegate_id)); |
231 | 239 |
232 player_map->erase(it); | 240 player_map->erase(it); |
233 } | 241 } |
234 | 242 |
235 } // namespace content | 243 } // namespace content |
OLD | NEW |