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 3 // found in the LICENSE file. 4 5 #include "content/browser/media/media_web_contents_observer.h" 6 7 #include "base/memory/scoped_ptr.h" 8 #include "base/stl_util.h" 9 #include "content/browser/media/cdm/browser_cdm_manager.h" 10 #include "content/browser/renderer_host/render_process_host_impl.h" 11 #include "content/public/browser/render_frame_host.h" 12 #include "content/public/browser/web_contents.h" 13 #include "ipc/ipc_message_macros.h" 14 15 #if defined(OS_ANDROID) 16 #include "content/browser/media/android/browser_media_player_manager.h" 17 #include "content/common/media/media_player_messages_android.h" 18 #include "media/base/android/media_player_android.h" 19 #endif // defined(OS_ANDROID) 20 21 namespace content { 22 23 MediaWebContentsObserver::MediaWebContentsObserver( 24 RenderViewHost* render_view_host) 25 : WebContentsObserver(WebContents::FromRenderViewHost(render_view_host)) { 26 } 27 28 MediaWebContentsObserver::~MediaWebContentsObserver() { 29 } 30 31 void MediaWebContentsObserver::RenderFrameDeleted( 32 RenderFrameHost* render_frame_host) { 33 uintptr_t key = reinterpret_cast<uintptr_t>(render_frame_host); 34 // Always destroy the media players before CDMs because we do not support 35 // detaching CDMs from media players yet. See http://crbug.com/330324 36 #if defined(OS_ANDROID) 37 media_player_managers_.erase(key); 38 #endif 39 // TODO(xhwang): Currently MediaWebContentsObserver, BrowserMediaPlayerManager 40 // and BrowserCdmManager all run on browser UI thread. So this call is okay. 41 // In the future we need to support the case where MediaWebContentsObserver 42 // get notified on browser UI thread, but BrowserMediaPlayerManager and 43 // BrowserCdmManager run on a different thread. 44 BrowserCdmManager* browser_cdm_manager = 45 BrowserCdmManager::FromProcess(render_frame_host->GetProcess()->GetID()); 46 if (browser_cdm_manager) 47 browser_cdm_manager->RenderFrameDeleted(render_frame_host->GetRoutingID()); 48 } 49 50 #if defined(OS_ANDROID) 51 52 bool MediaWebContentsObserver::OnMessageReceived( 53 const IPC::Message& msg, 54 RenderFrameHost* render_frame_host) { 55 if (OnMediaPlayerMessageReceived(msg, render_frame_host)) 56 return true; 57 58 if (OnMediaPlayerSetCdmMessageReceived(msg, render_frame_host)) 59 return true; 60 61 return false; 62 } 63 64 bool MediaWebContentsObserver::OnMediaPlayerMessageReceived( 65 const IPC::Message& msg, 66 RenderFrameHost* render_frame_host) { 67 bool handled = true; 68 IPC_BEGIN_MESSAGE_MAP(MediaWebContentsObserver, msg) 69 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_EnterFullscreen, 70 GetMediaPlayerManager(render_frame_host), 71 BrowserMediaPlayerManager::OnEnterFullscreen) 72 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_ExitFullscreen, 73 GetMediaPlayerManager(render_frame_host), 74 BrowserMediaPlayerManager::OnExitFullscreen) 75 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_Initialize, 76 GetMediaPlayerManager(render_frame_host), 77 BrowserMediaPlayerManager::OnInitialize) 78 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_Start, 79 GetMediaPlayerManager(render_frame_host), 80 BrowserMediaPlayerManager::OnStart) 81 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_Seek, 82 GetMediaPlayerManager(render_frame_host), 83 BrowserMediaPlayerManager::OnSeek) 84 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_Pause, 85 GetMediaPlayerManager(render_frame_host), 86 BrowserMediaPlayerManager::OnPause) 87 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_SetVolume, 88 GetMediaPlayerManager(render_frame_host), 89 BrowserMediaPlayerManager::OnSetVolume) 90 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_SetPoster, 91 GetMediaPlayerManager(render_frame_host), 92 BrowserMediaPlayerManager::OnSetPoster) 93 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_Release, 94 GetMediaPlayerManager(render_frame_host), 95 BrowserMediaPlayerManager::OnReleaseResources) 96 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_DestroyMediaPlayer, 97 GetMediaPlayerManager(render_frame_host), 98 BrowserMediaPlayerManager::OnDestroyPlayer) 99 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_RequestRemotePlayback, 100 GetMediaPlayerManager(render_frame_host), 101 BrowserMediaPlayerManager::OnRequestRemotePlayback) 102 IPC_MESSAGE_FORWARD( 103 MediaPlayerHostMsg_RequestRemotePlaybackControl, 104 GetMediaPlayerManager(render_frame_host), 105 BrowserMediaPlayerManager::OnRequestRemotePlaybackControl) 106 #if defined(VIDEO_HOLE) 107 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_NotifyExternalSurface, 108 GetMediaPlayerManager(render_frame_host), 109 BrowserMediaPlayerManager::OnNotifyExternalSurface) 110 #endif // defined(VIDEO_HOLE) 111 IPC_MESSAGE_UNHANDLED(handled = false) 112 IPC_END_MESSAGE_MAP() 113 return handled; 114 } 115 116 bool MediaWebContentsObserver::OnMediaPlayerSetCdmMessageReceived( 117 const IPC::Message& msg, 118 RenderFrameHost* render_frame_host) { 119 bool handled = true; 120 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM( 121 MediaWebContentsObserver, msg, render_frame_host) 122 IPC_MESSAGE_HANDLER(MediaPlayerHostMsg_SetCdm, OnSetCdm) 123 IPC_MESSAGE_UNHANDLED(handled = false) 124 IPC_END_MESSAGE_MAP() 125 return handled; 126 } 127 128 void MediaWebContentsObserver::OnSetCdm(RenderFrameHost* render_frame_host, 129 int player_id, 130 int cdm_id) { 131 media::MediaPlayerAndroid* media_player = 132 GetMediaPlayerManager(render_frame_host)->GetPlayer(player_id); 133 if (!media_player) { 134 NOTREACHED() << "OnSetCdm: MediaPlayer not found for " << player_id; 135 return; 136 } 137 138 // MediaPlayerAndroid runs on the same thread as BrowserCdmManager. 139 BrowserCdmManager* browser_cdm_manager = 140 BrowserCdmManager::FromProcess(render_frame_host->GetProcess()->GetID()); 141 if (!browser_cdm_manager) { 142 NOTREACHED() << "OnSetCdm: CDM not found for " << cdm_id; 143 return; 144 } 145 146 media::BrowserCdm* cdm = 147 browser_cdm_manager->GetCdm(render_frame_host->GetRoutingID(), cdm_id); 148 if (!cdm) { 149 NOTREACHED() << "OnSetCdm: CDM not found for " << cdm_id; 150 return; 151 } 152 153 // TODO(xhwang): This could possibly fail. In that case we should reject the 154 // promise. 155 media_player->SetCdm(cdm); 156 } 157 158 BrowserMediaPlayerManager* MediaWebContentsObserver::GetMediaPlayerManager( 159 RenderFrameHost* render_frame_host) { 160 uintptr_t key = reinterpret_cast<uintptr_t>(render_frame_host); 161 if (!media_player_managers_.contains(key)) { 162 media_player_managers_.set( 163 key, 164 make_scoped_ptr(BrowserMediaPlayerManager::Create(render_frame_host))); 165 } 166 return media_player_managers_.get(key); 167 } 168 169 #if defined(VIDEO_HOLE) 170 void MediaWebContentsObserver::OnFrameInfoUpdated() { 171 for (MediaPlayerManagerMap::iterator iter = media_player_managers_.begin(); 172 iter != media_player_managers_.end(); ++iter) { 173 BrowserMediaPlayerManager* manager = iter->second; 174 manager->OnFrameInfoUpdated(); 175 } 176 } 177 #endif // defined(VIDEO_HOLE) 178 179 #endif // defined(OS_ANDROID) 180 181 } // namespace content 182