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/browser/web_contents/web_contents_impl.h"
12 #include "content/public/browser/render_frame_host.h"
13 #include "content/public/browser/web_contents.h"
14 #include "ipc/ipc_message_macros.h"
16 #if defined(OS_ANDROID)
17 #include "content/browser/android/media_players_observer.h"
18 #include "content/browser/media/android/browser_media_player_manager.h"
19 #include "content/common/media/media_player_messages_android.h"
20 #include "media/base/android/media_player_android.h"
21 #endif // defined(OS_ANDROID)
25 MediaWebContentsObserver::MediaWebContentsObserver(
26 WebContents
* web_contents
)
27 : WebContentsObserver(web_contents
)
31 MediaWebContentsObserver::~MediaWebContentsObserver() {
34 void MediaWebContentsObserver::RenderFrameDeleted(
35 RenderFrameHost
* render_frame_host
) {
36 #if defined(OS_ANDROID)
37 uintptr_t key
= reinterpret_cast<uintptr_t>(render_frame_host
);
38 // Always destroy the media players before CDMs because we do not support
39 // detaching CDMs from media players yet. See http://crbug.com/330324
40 media_player_managers_
.erase(key
);
42 MediaPlayersObserver
* audio_observer
= GetMediaPlayersObserver();
44 audio_observer
->RenderFrameDeleted(render_frame_host
);
46 // TODO(xhwang): Currently MediaWebContentsObserver, BrowserMediaPlayerManager
47 // and BrowserCdmManager all run on browser UI thread. So this call is okay.
48 // In the future we need to support the case where MediaWebContentsObserver
49 // get notified on browser UI thread, but BrowserMediaPlayerManager and
50 // BrowserCdmManager run on a different thread.
51 BrowserCdmManager
* browser_cdm_manager
=
52 BrowserCdmManager::FromProcess(render_frame_host
->GetProcess()->GetID());
53 if (browser_cdm_manager
)
54 browser_cdm_manager
->RenderFrameDeleted(render_frame_host
->GetRoutingID());
57 #if defined(OS_ANDROID)
59 bool MediaWebContentsObserver::OnMessageReceived(
60 const IPC::Message
& msg
,
61 RenderFrameHost
* render_frame_host
) {
62 if (OnMediaPlayerMessageReceived(msg
, render_frame_host
))
65 if (OnMediaPlayerSetCdmMessageReceived(msg
, render_frame_host
))
71 bool MediaWebContentsObserver::OnMediaPlayerMessageReceived(
72 const IPC::Message
& msg
,
73 RenderFrameHost
* render_frame_host
) {
75 IPC_BEGIN_MESSAGE_MAP(MediaWebContentsObserver
, msg
)
76 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_EnterFullscreen
,
77 GetMediaPlayerManager(render_frame_host
),
78 BrowserMediaPlayerManager::OnEnterFullscreen
)
79 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_Initialize
,
80 GetMediaPlayerManager(render_frame_host
),
81 BrowserMediaPlayerManager::OnInitialize
)
82 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_Start
,
83 GetMediaPlayerManager(render_frame_host
),
84 BrowserMediaPlayerManager::OnStart
)
85 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_Seek
,
86 GetMediaPlayerManager(render_frame_host
),
87 BrowserMediaPlayerManager::OnSeek
)
88 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_Pause
,
89 GetMediaPlayerManager(render_frame_host
),
90 BrowserMediaPlayerManager::OnPause
)
91 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_SetVolume
,
92 GetMediaPlayerManager(render_frame_host
),
93 BrowserMediaPlayerManager::OnSetVolume
)
94 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_SetPoster
,
95 GetMediaPlayerManager(render_frame_host
),
96 BrowserMediaPlayerManager::OnSetPoster
)
97 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_Release
,
98 GetMediaPlayerManager(render_frame_host
),
99 BrowserMediaPlayerManager::OnReleaseResources
)
100 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_DestroyMediaPlayer
,
101 GetMediaPlayerManager(render_frame_host
),
102 BrowserMediaPlayerManager::OnDestroyPlayer
)
103 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_RequestRemotePlayback
,
104 GetMediaPlayerManager(render_frame_host
),
105 BrowserMediaPlayerManager::OnRequestRemotePlayback
)
107 MediaPlayerHostMsg_RequestRemotePlaybackControl
,
108 GetMediaPlayerManager(render_frame_host
),
109 BrowserMediaPlayerManager::OnRequestRemotePlaybackControl
)
110 #if defined(VIDEO_HOLE)
111 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_NotifyExternalSurface
,
112 GetMediaPlayerManager(render_frame_host
),
113 BrowserMediaPlayerManager::OnNotifyExternalSurface
)
114 #endif // defined(VIDEO_HOLE)
115 IPC_MESSAGE_UNHANDLED(handled
= false)
116 IPC_END_MESSAGE_MAP()
120 bool MediaWebContentsObserver::OnMediaPlayerSetCdmMessageReceived(
121 const IPC::Message
& msg
,
122 RenderFrameHost
* render_frame_host
) {
124 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(
125 MediaWebContentsObserver
, msg
, render_frame_host
)
126 IPC_MESSAGE_HANDLER(MediaPlayerHostMsg_SetCdm
, OnSetCdm
)
127 IPC_MESSAGE_UNHANDLED(handled
= false)
128 IPC_END_MESSAGE_MAP()
132 void MediaWebContentsObserver::OnSetCdm(RenderFrameHost
* render_frame_host
,
135 media::MediaPlayerAndroid
* media_player
=
136 GetMediaPlayerManager(render_frame_host
)->GetPlayer(player_id
);
138 NOTREACHED() << "OnSetCdm: MediaPlayer not found for " << player_id
;
142 // MediaPlayerAndroid runs on the same thread as BrowserCdmManager.
143 BrowserCdmManager
* browser_cdm_manager
=
144 BrowserCdmManager::FromProcess(render_frame_host
->GetProcess()->GetID());
145 if (!browser_cdm_manager
) {
146 NOTREACHED() << "OnSetCdm: CDM not found for " << cdm_id
;
150 media::BrowserCdm
* cdm
=
151 browser_cdm_manager
->GetCdm(render_frame_host
->GetRoutingID(), cdm_id
);
153 NOTREACHED() << "OnSetCdm: CDM not found for " << cdm_id
;
157 // TODO(xhwang): This could possibly fail. In that case we should reject the
159 media_player
->SetCdm(cdm
);
162 BrowserMediaPlayerManager
* MediaWebContentsObserver::GetMediaPlayerManager(
163 RenderFrameHost
* render_frame_host
) {
164 uintptr_t key
= reinterpret_cast<uintptr_t>(render_frame_host
);
165 if (!media_player_managers_
.contains(key
)) {
166 media_player_managers_
.set(
168 make_scoped_ptr(BrowserMediaPlayerManager::Create(
169 render_frame_host
, GetMediaPlayersObserver())));
171 return media_player_managers_
.get(key
);
174 MediaPlayersObserver
*
175 MediaWebContentsObserver::GetMediaPlayersObserver() const {
176 AudioStateProvider
* provider
=
177 static_cast<WebContentsImpl
*>(web_contents())->audio_state_provider();
179 MediaPlayersObserver
* audio_observer
=
180 static_cast<MediaPlayersObserver
*>(provider
);
182 DCHECK(audio_observer
);
183 return audio_observer
;
186 #if defined(VIDEO_HOLE)
187 void MediaWebContentsObserver::OnFrameInfoUpdated() {
188 for (MediaPlayerManagerMap::iterator iter
= media_player_managers_
.begin();
189 iter
!= media_player_managers_
.end(); ++iter
) {
190 BrowserMediaPlayerManager
* manager
= iter
->second
;
191 manager
->OnFrameInfoUpdated();
194 #endif // defined(VIDEO_HOLE)
196 #endif // defined(OS_ANDROID)
198 } // namespace content