1 // Copyright 2013 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 "content/renderer/media/android/renderer_media_player_manager.h" 6 7 #include "content/common/media/media_player_messages_android.h" 8 #include "content/public/common/renderer_preferences.h" 9 #include "content/renderer/media/android/renderer_media_player_manager.h" 10 #include "content/renderer/media/android/webmediaplayer_android.h" 11 #include "content/renderer/media/crypto/renderer_cdm_manager.h" 12 #include "content/renderer/render_view_impl.h" 13 #include "ui/gfx/rect_f.h" 14 15 namespace content { 16 17 RendererMediaPlayerManager::RendererMediaPlayerManager( 18 RenderFrame* render_frame) 19 : RenderFrameObserver(render_frame), 20 next_media_player_id_(0), 21 fullscreen_frame_(NULL), 22 pending_fullscreen_frame_(NULL) { 23 } 24 25 RendererMediaPlayerManager::~RendererMediaPlayerManager() { 26 DCHECK(media_players_.empty()) 27 << "RendererMediaPlayerManager is owned by RenderFrameImpl and is " 28 "destroyed only after all media players are destroyed."; 29 } 30 31 bool RendererMediaPlayerManager::OnMessageReceived(const IPC::Message& msg) { 32 bool handled = true; 33 IPC_BEGIN_MESSAGE_MAP(RendererMediaPlayerManager, msg) 34 IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaMetadataChanged, 35 OnMediaMetadataChanged) 36 IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaPlaybackCompleted, 37 OnMediaPlaybackCompleted) 38 IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaBufferingUpdate, 39 OnMediaBufferingUpdate) 40 IPC_MESSAGE_HANDLER(MediaPlayerMsg_SeekRequest, OnSeekRequest) 41 IPC_MESSAGE_HANDLER(MediaPlayerMsg_SeekCompleted, OnSeekCompleted) 42 IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaError, OnMediaError) 43 IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaVideoSizeChanged, 44 OnVideoSizeChanged) 45 IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaTimeUpdate, OnTimeUpdate) 46 IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaPlayerReleased, 47 OnMediaPlayerReleased) 48 IPC_MESSAGE_HANDLER(MediaPlayerMsg_ConnectedToRemoteDevice, 49 OnConnectedToRemoteDevice) 50 IPC_MESSAGE_HANDLER(MediaPlayerMsg_DisconnectedFromRemoteDevice, 51 OnDisconnectedFromRemoteDevice) 52 IPC_MESSAGE_HANDLER(MediaPlayerMsg_RequestFullscreen, 53 OnRequestFullscreen) 54 IPC_MESSAGE_HANDLER(MediaPlayerMsg_DidEnterFullscreen, OnDidEnterFullscreen) 55 IPC_MESSAGE_HANDLER(MediaPlayerMsg_DidExitFullscreen, OnDidExitFullscreen) 56 IPC_MESSAGE_HANDLER(MediaPlayerMsg_DidMediaPlayerPlay, OnPlayerPlay) 57 IPC_MESSAGE_HANDLER(MediaPlayerMsg_DidMediaPlayerPause, OnPlayerPause) 58 IPC_MESSAGE_HANDLER(MediaPlayerMsg_RemoteRouteAvailabilityChanged, 59 OnRemoteRouteAvailabilityChanged) 60 IPC_MESSAGE_UNHANDLED(handled = false) 61 IPC_END_MESSAGE_MAP() 62 return handled; 63 } 64 65 void RendererMediaPlayerManager::WasHidden() { 66 ReleaseVideoResources(); 67 } 68 69 void RendererMediaPlayerManager::Initialize( 70 MediaPlayerHostMsg_Initialize_Type type, 71 int player_id, 72 const GURL& url, 73 const GURL& first_party_for_cookies, 74 int demuxer_client_id, 75 const GURL& frame_url, 76 bool allow_credentials) { 77 MediaPlayerHostMsg_Initialize_Params media_player_params; 78 media_player_params.type = type; 79 media_player_params.player_id = player_id; 80 media_player_params.demuxer_client_id = demuxer_client_id; 81 media_player_params.url = url; 82 media_player_params.first_party_for_cookies = first_party_for_cookies; 83 media_player_params.frame_url = frame_url; 84 media_player_params.allow_credentials = allow_credentials; 85 86 Send(new MediaPlayerHostMsg_Initialize(routing_id(), media_player_params)); 87 } 88 89 void RendererMediaPlayerManager::Start(int player_id) { 90 Send(new MediaPlayerHostMsg_Start(routing_id(), player_id)); 91 } 92 93 void RendererMediaPlayerManager::Pause( 94 int player_id, 95 bool is_media_related_action) { 96 Send(new MediaPlayerHostMsg_Pause( 97 routing_id(), player_id, is_media_related_action)); 98 } 99 100 void RendererMediaPlayerManager::Seek( 101 int player_id, 102 const base::TimeDelta& time) { 103 Send(new MediaPlayerHostMsg_Seek(routing_id(), player_id, time)); 104 } 105 106 void RendererMediaPlayerManager::SetVolume(int player_id, double volume) { 107 Send(new MediaPlayerHostMsg_SetVolume(routing_id(), player_id, volume)); 108 } 109 110 void RendererMediaPlayerManager::SetPoster(int player_id, const GURL& poster) { 111 Send(new MediaPlayerHostMsg_SetPoster(routing_id(), player_id, poster)); 112 } 113 114 void RendererMediaPlayerManager::ReleaseResources(int player_id) { 115 Send(new MediaPlayerHostMsg_Release(routing_id(), player_id)); 116 } 117 118 void RendererMediaPlayerManager::DestroyPlayer(int player_id) { 119 Send(new MediaPlayerHostMsg_DestroyMediaPlayer(routing_id(), player_id)); 120 } 121 122 void RendererMediaPlayerManager::RequestRemotePlayback(int player_id) { 123 Send(new MediaPlayerHostMsg_RequestRemotePlayback(routing_id(), player_id)); 124 } 125 126 void RendererMediaPlayerManager::RequestRemotePlaybackControl(int player_id) { 127 Send(new MediaPlayerHostMsg_RequestRemotePlaybackControl(routing_id(), 128 player_id)); 129 } 130 131 void RendererMediaPlayerManager::OnMediaMetadataChanged( 132 int player_id, 133 base::TimeDelta duration, 134 int width, 135 int height, 136 bool success) { 137 WebMediaPlayerAndroid* player = GetMediaPlayer(player_id); 138 if (player) 139 player->OnMediaMetadataChanged(duration, width, height, success); 140 } 141 142 void RendererMediaPlayerManager::OnMediaPlaybackCompleted(int player_id) { 143 WebMediaPlayerAndroid* player = GetMediaPlayer(player_id); 144 if (player) 145 player->OnPlaybackComplete(); 146 } 147 148 void RendererMediaPlayerManager::OnMediaBufferingUpdate(int player_id, 149 int percent) { 150 WebMediaPlayerAndroid* player = GetMediaPlayer(player_id); 151 if (player) 152 player->OnBufferingUpdate(percent); 153 } 154 155 void RendererMediaPlayerManager::OnSeekRequest( 156 int player_id, 157 const base::TimeDelta& time_to_seek) { 158 WebMediaPlayerAndroid* player = GetMediaPlayer(player_id); 159 if (player) 160 player->OnSeekRequest(time_to_seek); 161 } 162 163 void RendererMediaPlayerManager::OnSeekCompleted( 164 int player_id, 165 const base::TimeDelta& current_time) { 166 WebMediaPlayerAndroid* player = GetMediaPlayer(player_id); 167 if (player) 168 player->OnSeekComplete(current_time); 169 } 170 171 void RendererMediaPlayerManager::OnMediaError(int player_id, int error) { 172 WebMediaPlayerAndroid* player = GetMediaPlayer(player_id); 173 if (player) 174 player->OnMediaError(error); 175 } 176 177 void RendererMediaPlayerManager::OnVideoSizeChanged(int player_id, 178 int width, 179 int height) { 180 WebMediaPlayerAndroid* player = GetMediaPlayer(player_id); 181 if (player) 182 player->OnVideoSizeChanged(width, height); 183 } 184 185 void RendererMediaPlayerManager::OnTimeUpdate( 186 int player_id, 187 base::TimeDelta current_timestamp, 188 base::TimeTicks current_time_ticks) { 189 WebMediaPlayerAndroid* player = GetMediaPlayer(player_id); 190 if (player) 191 player->OnTimeUpdate(current_timestamp, current_time_ticks); 192 } 193 194 void RendererMediaPlayerManager::OnMediaPlayerReleased(int player_id) { 195 WebMediaPlayerAndroid* player = GetMediaPlayer(player_id); 196 if (player) 197 player->OnPlayerReleased(); 198 } 199 200 void RendererMediaPlayerManager::OnConnectedToRemoteDevice(int player_id, 201 const std::string& remote_playback_message) { 202 WebMediaPlayerAndroid* player = GetMediaPlayer(player_id); 203 if (player) 204 player->OnConnectedToRemoteDevice(remote_playback_message); 205 } 206 207 void RendererMediaPlayerManager::OnDisconnectedFromRemoteDevice(int player_id) { 208 WebMediaPlayerAndroid* player = GetMediaPlayer(player_id); 209 if (player) 210 player->OnDisconnectedFromRemoteDevice(); 211 } 212 213 void RendererMediaPlayerManager::OnDidEnterFullscreen(int player_id) { 214 WebMediaPlayerAndroid* player = GetMediaPlayer(player_id); 215 if (player) 216 player->OnDidEnterFullscreen(); 217 } 218 219 void RendererMediaPlayerManager::OnDidExitFullscreen(int player_id) { 220 WebMediaPlayerAndroid* player = GetMediaPlayer(player_id); 221 if (player) 222 player->OnDidExitFullscreen(); 223 } 224 225 void RendererMediaPlayerManager::OnPlayerPlay(int player_id) { 226 WebMediaPlayerAndroid* player = GetMediaPlayer(player_id); 227 if (player) 228 player->OnMediaPlayerPlay(); 229 } 230 231 void RendererMediaPlayerManager::OnPlayerPause(int player_id) { 232 WebMediaPlayerAndroid* player = GetMediaPlayer(player_id); 233 if (player) 234 player->OnMediaPlayerPause(); 235 } 236 237 void RendererMediaPlayerManager::OnRequestFullscreen(int player_id) { 238 WebMediaPlayerAndroid* player = GetMediaPlayer(player_id); 239 if (player) 240 player->OnRequestFullscreen(); 241 } 242 243 void RendererMediaPlayerManager::OnRemoteRouteAvailabilityChanged( 244 int player_id, 245 bool routes_available) { 246 WebMediaPlayerAndroid* player = GetMediaPlayer(player_id); 247 if (player) 248 player->OnRemoteRouteAvailabilityChanged(routes_available); 249 } 250 251 void RendererMediaPlayerManager::EnterFullscreen(int player_id, 252 blink::WebFrame* frame) { 253 pending_fullscreen_frame_ = frame; 254 Send(new MediaPlayerHostMsg_EnterFullscreen(routing_id(), player_id)); 255 } 256 257 void RendererMediaPlayerManager::ExitFullscreen(int player_id) { 258 pending_fullscreen_frame_ = NULL; 259 fullscreen_frame_ = NULL; 260 Send(new MediaPlayerHostMsg_ExitFullscreen(routing_id(), player_id)); 261 } 262 263 void RendererMediaPlayerManager::SetCdm(int player_id, int cdm_id) { 264 if (cdm_id == RendererCdmManager::kInvalidCdmId) { 265 NOTREACHED(); 266 return; 267 } 268 Send(new MediaPlayerHostMsg_SetCdm(routing_id(), player_id, cdm_id)); 269 } 270 271 int RendererMediaPlayerManager::RegisterMediaPlayer( 272 WebMediaPlayerAndroid* player) { 273 media_players_[next_media_player_id_] = player; 274 return next_media_player_id_++; 275 } 276 277 void RendererMediaPlayerManager::UnregisterMediaPlayer(int player_id) { 278 media_players_.erase(player_id); 279 } 280 281 void RendererMediaPlayerManager::ReleaseVideoResources() { 282 std::map<int, WebMediaPlayerAndroid*>::iterator player_it; 283 for (player_it = media_players_.begin(); player_it != media_players_.end(); 284 ++player_it) { 285 WebMediaPlayerAndroid* player = player_it->second; 286 287 // Do not release if an audio track is still playing 288 if (player && (player->paused() || player->hasVideo())) 289 player->ReleaseMediaResources(); 290 } 291 } 292 293 WebMediaPlayerAndroid* RendererMediaPlayerManager::GetMediaPlayer( 294 int player_id) { 295 std::map<int, WebMediaPlayerAndroid*>::iterator iter = 296 media_players_.find(player_id); 297 if (iter != media_players_.end()) 298 return iter->second; 299 return NULL; 300 } 301 302 bool RendererMediaPlayerManager::CanEnterFullscreen(blink::WebFrame* frame) { 303 return (!fullscreen_frame_ && !pending_fullscreen_frame_) 304 || ShouldEnterFullscreen(frame); 305 } 306 307 void RendererMediaPlayerManager::DidEnterFullscreen(blink::WebFrame* frame) { 308 pending_fullscreen_frame_ = NULL; 309 fullscreen_frame_ = frame; 310 } 311 312 void RendererMediaPlayerManager::DidExitFullscreen() { 313 fullscreen_frame_ = NULL; 314 } 315 316 bool RendererMediaPlayerManager::IsInFullscreen(blink::WebFrame* frame) { 317 return fullscreen_frame_ == frame; 318 } 319 320 bool RendererMediaPlayerManager::ShouldEnterFullscreen(blink::WebFrame* frame) { 321 return fullscreen_frame_ == frame || pending_fullscreen_frame_ == frame; 322 } 323 324 #if defined(VIDEO_HOLE) 325 void RendererMediaPlayerManager::RequestExternalSurface( 326 int player_id, 327 const gfx::RectF& geometry) { 328 Send(new MediaPlayerHostMsg_NotifyExternalSurface( 329 routing_id(), player_id, true, geometry)); 330 } 331 332 void RendererMediaPlayerManager::DidCommitCompositorFrame() { 333 std::map<int, gfx::RectF> geometry_change; 334 RetrieveGeometryChanges(&geometry_change); 335 for (std::map<int, gfx::RectF>::iterator it = geometry_change.begin(); 336 it != geometry_change.end(); 337 ++it) { 338 Send(new MediaPlayerHostMsg_NotifyExternalSurface( 339 routing_id(), it->first, false, it->second)); 340 } 341 } 342 343 void RendererMediaPlayerManager::RetrieveGeometryChanges( 344 std::map<int, gfx::RectF>* changes) { 345 DCHECK(changes->empty()); 346 for (std::map<int, WebMediaPlayerAndroid*>::iterator player_it = 347 media_players_.begin(); 348 player_it != media_players_.end(); 349 ++player_it) { 350 WebMediaPlayerAndroid* player = player_it->second; 351 352 if (player && player->hasVideo()) { 353 if (player->UpdateBoundaryRectangle()) 354 (*changes)[player_it->first] = player->GetBoundaryRectangle(); 355 } 356 } 357 } 358 359 bool 360 RendererMediaPlayerManager::ShouldUseVideoOverlayForEmbeddedEncryptedVideo() { 361 const RendererPreferences& prefs = static_cast<RenderFrameImpl*>( 362 render_frame())->render_view()->renderer_preferences(); 363 return prefs.use_video_overlay_for_embedded_encrypted_video; 364 } 365 #endif // defined(VIDEO_HOLE) 366 367 } // namespace content 368