| 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/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
| 9 #include "content/browser/power_save_blocker_impl.h" | 9 #include "content/browser/power_save_blocker_impl.h" |
| 10 #include "content/browser/web_contents/web_contents_impl.h" | 10 #include "content/browser/web_contents/web_contents_impl.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 } | 34 } |
| 35 } | 35 } |
| 36 | 36 |
| 37 bool MediaWebContentsObserver::OnMessageReceived( | 37 bool MediaWebContentsObserver::OnMessageReceived( |
| 38 const IPC::Message& msg, | 38 const IPC::Message& msg, |
| 39 RenderFrameHost* render_frame_host) { | 39 RenderFrameHost* render_frame_host) { |
| 40 bool handled = true; | 40 bool handled = true; |
| 41 // TODO(dalecurtis): These should no longer be FrameHostMsg. | 41 // TODO(dalecurtis): These should no longer be FrameHostMsg. |
| 42 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(MediaWebContentsObserver, msg, | 42 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(MediaWebContentsObserver, msg, |
| 43 render_frame_host) | 43 render_frame_host) |
| 44 IPC_MESSAGE_HANDLER(FrameHostMsg_MediaDestroyedNotification, |
| 45 OnMediaDestroyedNotification) |
| 44 IPC_MESSAGE_HANDLER(FrameHostMsg_MediaPlayingNotification, | 46 IPC_MESSAGE_HANDLER(FrameHostMsg_MediaPlayingNotification, |
| 45 OnMediaPlayingNotification) | 47 OnMediaPlayingNotification) |
| 46 IPC_MESSAGE_HANDLER(FrameHostMsg_MediaPausedNotification, | 48 IPC_MESSAGE_HANDLER(FrameHostMsg_MediaPausedNotification, |
| 47 OnMediaPausedNotification) | 49 OnMediaPausedNotification) |
| 48 IPC_MESSAGE_UNHANDLED(handled = false) | 50 IPC_MESSAGE_UNHANDLED(handled = false) |
| 49 IPC_END_MESSAGE_MAP() | 51 IPC_END_MESSAGE_MAP() |
| 50 return handled; | 52 return handled; |
| 51 } | 53 } |
| 52 | 54 |
| 55 void MediaWebContentsObserver::WasShown() { |
| 56 // Restore power save blocker if there are active video players running. |
| 57 if (!active_video_players_.empty() && !video_power_save_blocker_) |
| 58 CreateVideoPowerSaveBlocker(); |
| 59 } |
| 60 |
| 61 void MediaWebContentsObserver::WasHidden() { |
| 62 // If there are entities capturing screenshots or video (e.g., mirroring), |
| 63 // don't release the power save blocker. |
| 64 if (!web_contents()->GetCapturerCount()) |
| 65 video_power_save_blocker_.reset(); |
| 66 } |
| 67 |
| 68 void MediaWebContentsObserver::OnMediaDestroyedNotification( |
| 69 RenderFrameHost* render_frame_host, |
| 70 int64_t player_cookie) { |
| 71 OnMediaPausedNotification(render_frame_host, player_cookie, true); |
| 72 } |
| 73 |
| 53 void MediaWebContentsObserver::OnMediaPlayingNotification( | 74 void MediaWebContentsObserver::OnMediaPlayingNotification( |
| 54 RenderFrameHost* render_frame_host, | 75 RenderFrameHost* render_frame_host, |
| 55 int64_t player_cookie, | 76 int64_t player_cookie, |
| 56 bool has_video, | 77 bool has_video, |
| 57 bool has_audio, | 78 bool has_audio, |
| 58 bool is_remote) { | 79 bool is_remote, |
| 80 base::TimeDelta duration) { |
| 59 // Ignore the videos playing remotely and don't hold the wake lock for the | 81 // Ignore the videos playing remotely and don't hold the wake lock for the |
| 60 // screen. TODO(dalecurtis): Is this correct? It means observers will not | 82 // screen. TODO(dalecurtis): Is this correct? It means observers will not |
| 61 // receive play and pause messages. | 83 // receive play and pause messages. |
| 62 if (is_remote) | 84 if (is_remote) |
| 63 return; | 85 return; |
| 64 | 86 |
| 65 const MediaPlayerId id(render_frame_host, player_cookie); | 87 const MediaPlayerId id(render_frame_host, player_cookie); |
| 66 if (has_audio) { | 88 if (has_audio) { |
| 67 AddMediaPlayerEntry(id, &active_audio_players_); | 89 AddMediaPlayerEntry(id, &active_audio_players_); |
| 68 | 90 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 84 } | 106 } |
| 85 } | 107 } |
| 86 | 108 |
| 87 // Notify observers of the new player. | 109 // Notify observers of the new player. |
| 88 DCHECK(has_audio || has_video); | 110 DCHECK(has_audio || has_video); |
| 89 static_cast<WebContentsImpl*>(web_contents())->MediaStartedPlaying(id); | 111 static_cast<WebContentsImpl*>(web_contents())->MediaStartedPlaying(id); |
| 90 } | 112 } |
| 91 | 113 |
| 92 void MediaWebContentsObserver::OnMediaPausedNotification( | 114 void MediaWebContentsObserver::OnMediaPausedNotification( |
| 93 RenderFrameHost* render_frame_host, | 115 RenderFrameHost* render_frame_host, |
| 94 int64_t player_cookie) { | 116 int64_t player_cookie, |
| 117 bool reached_end_of_stream) { |
| 95 const MediaPlayerId id(render_frame_host, player_cookie); | 118 const MediaPlayerId id(render_frame_host, player_cookie); |
| 96 const bool removed_audio = RemoveMediaPlayerEntry(id, &active_audio_players_); | 119 const bool removed_audio = RemoveMediaPlayerEntry(id, &active_audio_players_); |
| 97 const bool removed_video = RemoveMediaPlayerEntry(id, &active_video_players_); | 120 const bool removed_video = RemoveMediaPlayerEntry(id, &active_video_players_); |
| 98 MaybeReleasePowerSaveBlockers(); | 121 MaybeReleasePowerSaveBlockers(); |
| 99 | 122 |
| 100 if (removed_audio || removed_video) { | 123 if (removed_audio || removed_video) { |
| 101 // Notify observers the player has been "paused". | 124 // Notify observers the player has been "paused". |
| 102 static_cast<WebContentsImpl*>(web_contents())->MediaStoppedPlaying(id); | 125 static_cast<WebContentsImpl*>(web_contents())->MediaStoppedPlaying(id); |
| 103 } | 126 } |
| 104 } | 127 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 131 video_power_save_blocker_ = PowerSaveBlocker::Create( | 154 video_power_save_blocker_ = PowerSaveBlocker::Create( |
| 132 PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep, | 155 PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep, |
| 133 PowerSaveBlocker::kReasonVideoPlayback, "Playing video"); | 156 PowerSaveBlocker::kReasonVideoPlayback, "Playing video"); |
| 134 // TODO(mfomitchev): Support PowerSaveBlocker on Aura - crbug.com/546718. | 157 // TODO(mfomitchev): Support PowerSaveBlocker on Aura - crbug.com/546718. |
| 135 #if defined(OS_ANDROID) && !defined(USE_AURA) | 158 #if defined(OS_ANDROID) && !defined(USE_AURA) |
| 136 static_cast<PowerSaveBlockerImpl*>(video_power_save_blocker_.get()) | 159 static_cast<PowerSaveBlockerImpl*>(video_power_save_blocker_.get()) |
| 137 ->InitDisplaySleepBlocker(web_contents()); | 160 ->InitDisplaySleepBlocker(web_contents()); |
| 138 #endif | 161 #endif |
| 139 } | 162 } |
| 140 | 163 |
| 141 void MediaWebContentsObserver::WasShown() { | |
| 142 // Restore power save blocker if there are active video players running. | |
| 143 if (!active_video_players_.empty() && !video_power_save_blocker_) | |
| 144 CreateVideoPowerSaveBlocker(); | |
| 145 } | |
| 146 | |
| 147 void MediaWebContentsObserver::WasHidden() { | |
| 148 // If there are entities capturing screenshots or video (e.g., mirroring), | |
| 149 // don't release the power save blocker. | |
| 150 if (!web_contents()->GetCapturerCount()) | |
| 151 video_power_save_blocker_.reset(); | |
| 152 } | |
| 153 | |
| 154 void MediaWebContentsObserver::MaybeReleasePowerSaveBlockers() { | 164 void MediaWebContentsObserver::MaybeReleasePowerSaveBlockers() { |
| 155 // If there are no more audio players and we don't have audio stream | 165 // If there are no more audio players and we don't have audio stream |
| 156 // monitoring, release the audio power save blocker here instead of during | 166 // monitoring, release the audio power save blocker here instead of during |
| 157 // NotifyNavigationStateChanged(). | 167 // NotifyNavigationStateChanged(). |
| 158 if (active_audio_players_.empty() && | 168 if (active_audio_players_.empty() && |
| 159 !AudioStreamMonitor::monitoring_available()) { | 169 !AudioStreamMonitor::monitoring_available()) { |
| 160 audio_power_save_blocker_.reset(); | 170 audio_power_save_blocker_.reset(); |
| 161 } | 171 } |
| 162 | 172 |
| 163 // If there are no more video players, clear the video power save blocker. | 173 // 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... |
| 203 if (it == player_map->end()) | 213 if (it == player_map->end()) |
| 204 return; | 214 return; |
| 205 | 215 |
| 206 for (int64_t player_cookie : it->second) | 216 for (int64_t player_cookie : it->second) |
| 207 removed_players->insert(MediaPlayerId(render_frame_host, player_cookie)); | 217 removed_players->insert(MediaPlayerId(render_frame_host, player_cookie)); |
| 208 | 218 |
| 209 player_map->erase(it); | 219 player_map->erase(it); |
| 210 } | 220 } |
| 211 | 221 |
| 212 } // namespace content | 222 } // namespace content |
| OLD | NEW |