Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / media / media_web_contents_observer.cc
blob8e48e6878fc976a86faf837894ab68c1ba16418a
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.
5 #include "content/browser/media/media_web_contents_observer.h"
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"
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)
21 namespace content {
23 MediaWebContentsObserver::MediaWebContentsObserver(
24 WebContents* web_contents)
25 : WebContentsObserver(web_contents)
29 MediaWebContentsObserver::~MediaWebContentsObserver() {
32 void MediaWebContentsObserver::RenderFrameDeleted(
33 RenderFrameHost* render_frame_host) {
34 #if defined(OS_ANDROID)
35 uintptr_t key = reinterpret_cast<uintptr_t>(render_frame_host);
36 // Always destroy the media players before CDMs because we do not support
37 // detaching CDMs from media players yet. See http://crbug.com/330324
38 media_player_managers_.erase(key);
39 #endif
40 // TODO(xhwang): Currently MediaWebContentsObserver, BrowserMediaPlayerManager
41 // and BrowserCdmManager all run on browser UI thread. So this call is okay.
42 // In the future we need to support the case where MediaWebContentsObserver
43 // get notified on browser UI thread, but BrowserMediaPlayerManager and
44 // BrowserCdmManager run on a different thread.
45 BrowserCdmManager* browser_cdm_manager =
46 BrowserCdmManager::FromProcess(render_frame_host->GetProcess()->GetID());
47 if (browser_cdm_manager)
48 browser_cdm_manager->RenderFrameDeleted(render_frame_host->GetRoutingID());
51 #if defined(OS_ANDROID)
53 bool MediaWebContentsObserver::OnMessageReceived(
54 const IPC::Message& msg,
55 RenderFrameHost* render_frame_host) {
56 if (OnMediaPlayerMessageReceived(msg, render_frame_host))
57 return true;
59 if (OnMediaPlayerSetCdmMessageReceived(msg, render_frame_host))
60 return true;
62 return false;
65 bool MediaWebContentsObserver::OnMediaPlayerMessageReceived(
66 const IPC::Message& msg,
67 RenderFrameHost* render_frame_host) {
68 bool handled = true;
69 IPC_BEGIN_MESSAGE_MAP(MediaWebContentsObserver, msg)
70 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_EnterFullscreen,
71 GetMediaPlayerManager(render_frame_host),
72 BrowserMediaPlayerManager::OnEnterFullscreen)
73 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_Initialize,
74 GetMediaPlayerManager(render_frame_host),
75 BrowserMediaPlayerManager::OnInitialize)
76 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_Start,
77 GetMediaPlayerManager(render_frame_host),
78 BrowserMediaPlayerManager::OnStart)
79 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_Seek,
80 GetMediaPlayerManager(render_frame_host),
81 BrowserMediaPlayerManager::OnSeek)
82 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_Pause,
83 GetMediaPlayerManager(render_frame_host),
84 BrowserMediaPlayerManager::OnPause)
85 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_SetVolume,
86 GetMediaPlayerManager(render_frame_host),
87 BrowserMediaPlayerManager::OnSetVolume)
88 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_SetPoster,
89 GetMediaPlayerManager(render_frame_host),
90 BrowserMediaPlayerManager::OnSetPoster)
91 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_Release,
92 GetMediaPlayerManager(render_frame_host),
93 BrowserMediaPlayerManager::OnReleaseResources)
94 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_DestroyMediaPlayer,
95 GetMediaPlayerManager(render_frame_host),
96 BrowserMediaPlayerManager::OnDestroyPlayer)
97 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_RequestRemotePlayback,
98 GetMediaPlayerManager(render_frame_host),
99 BrowserMediaPlayerManager::OnRequestRemotePlayback)
100 IPC_MESSAGE_FORWARD(
101 MediaPlayerHostMsg_RequestRemotePlaybackControl,
102 GetMediaPlayerManager(render_frame_host),
103 BrowserMediaPlayerManager::OnRequestRemotePlaybackControl)
104 #if defined(VIDEO_HOLE)
105 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_NotifyExternalSurface,
106 GetMediaPlayerManager(render_frame_host),
107 BrowserMediaPlayerManager::OnNotifyExternalSurface)
108 #endif // defined(VIDEO_HOLE)
109 IPC_MESSAGE_UNHANDLED(handled = false)
110 IPC_END_MESSAGE_MAP()
111 return handled;
114 bool MediaWebContentsObserver::OnMediaPlayerSetCdmMessageReceived(
115 const IPC::Message& msg,
116 RenderFrameHost* render_frame_host) {
117 bool handled = true;
118 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(
119 MediaWebContentsObserver, msg, render_frame_host)
120 IPC_MESSAGE_HANDLER(MediaPlayerHostMsg_SetCdm, OnSetCdm)
121 IPC_MESSAGE_UNHANDLED(handled = false)
122 IPC_END_MESSAGE_MAP()
123 return handled;
126 void MediaWebContentsObserver::OnSetCdm(RenderFrameHost* render_frame_host,
127 int player_id,
128 int cdm_id) {
129 media::MediaPlayerAndroid* media_player =
130 GetMediaPlayerManager(render_frame_host)->GetPlayer(player_id);
131 if (!media_player) {
132 NOTREACHED() << "OnSetCdm: MediaPlayer not found for " << player_id;
133 return;
136 // MediaPlayerAndroid runs on the same thread as BrowserCdmManager.
137 BrowserCdmManager* browser_cdm_manager =
138 BrowserCdmManager::FromProcess(render_frame_host->GetProcess()->GetID());
139 if (!browser_cdm_manager) {
140 NOTREACHED() << "OnSetCdm: CDM not found for " << cdm_id;
141 return;
144 media::BrowserCdm* cdm =
145 browser_cdm_manager->GetCdm(render_frame_host->GetRoutingID(), cdm_id);
146 if (!cdm) {
147 NOTREACHED() << "OnSetCdm: CDM not found for " << cdm_id;
148 return;
151 // TODO(xhwang): This could possibly fail. In that case we should reject the
152 // promise.
153 media_player->SetCdm(cdm);
156 BrowserMediaPlayerManager* MediaWebContentsObserver::GetMediaPlayerManager(
157 RenderFrameHost* render_frame_host) {
158 uintptr_t key = reinterpret_cast<uintptr_t>(render_frame_host);
159 if (!media_player_managers_.contains(key)) {
160 media_player_managers_.set(
161 key,
162 make_scoped_ptr(BrowserMediaPlayerManager::Create(render_frame_host)));
164 return media_player_managers_.get(key);
167 #if defined(VIDEO_HOLE)
168 void MediaWebContentsObserver::OnFrameInfoUpdated() {
169 for (MediaPlayerManagerMap::iterator iter = media_player_managers_.begin();
170 iter != media_player_managers_.end(); ++iter) {
171 BrowserMediaPlayerManager* manager = iter->second;
172 manager->OnFrameInfoUpdated();
175 #endif // defined(VIDEO_HOLE)
177 #endif // defined(OS_ANDROID)
179 } // namespace content