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_ExitFullscreen
,
80 GetMediaPlayerManager(render_frame_host
),
81 BrowserMediaPlayerManager::OnExitFullscreen
)
82 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_Initialize
,
83 GetMediaPlayerManager(render_frame_host
),
84 BrowserMediaPlayerManager::OnInitialize
)
85 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_Start
,
86 GetMediaPlayerManager(render_frame_host
),
87 BrowserMediaPlayerManager::OnStart
)
88 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_Seek
,
89 GetMediaPlayerManager(render_frame_host
),
90 BrowserMediaPlayerManager::OnSeek
)
91 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_Pause
,
92 GetMediaPlayerManager(render_frame_host
),
93 BrowserMediaPlayerManager::OnPause
)
94 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_SetVolume
,
95 GetMediaPlayerManager(render_frame_host
),
96 BrowserMediaPlayerManager::OnSetVolume
)
97 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_SetPoster
,
98 GetMediaPlayerManager(render_frame_host
),
99 BrowserMediaPlayerManager::OnSetPoster
)
100 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_Release
,
101 GetMediaPlayerManager(render_frame_host
),
102 BrowserMediaPlayerManager::OnReleaseResources
)
103 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_DestroyMediaPlayer
,
104 GetMediaPlayerManager(render_frame_host
),
105 BrowserMediaPlayerManager::OnDestroyPlayer
)
106 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_RequestRemotePlayback
,
107 GetMediaPlayerManager(render_frame_host
),
108 BrowserMediaPlayerManager::OnRequestRemotePlayback
)
110 MediaPlayerHostMsg_RequestRemotePlaybackControl
,
111 GetMediaPlayerManager(render_frame_host
),
112 BrowserMediaPlayerManager::OnRequestRemotePlaybackControl
)
113 #if defined(VIDEO_HOLE)
114 IPC_MESSAGE_FORWARD(MediaPlayerHostMsg_NotifyExternalSurface
,
115 GetMediaPlayerManager(render_frame_host
),
116 BrowserMediaPlayerManager::OnNotifyExternalSurface
)
117 #endif // defined(VIDEO_HOLE)
118 IPC_MESSAGE_UNHANDLED(handled
= false)
119 IPC_END_MESSAGE_MAP()
123 bool MediaWebContentsObserver::OnMediaPlayerSetCdmMessageReceived(
124 const IPC::Message
& msg
,
125 RenderFrameHost
* render_frame_host
) {
127 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(
128 MediaWebContentsObserver
, msg
, render_frame_host
)
129 IPC_MESSAGE_HANDLER(MediaPlayerHostMsg_SetCdm
, OnSetCdm
)
130 IPC_MESSAGE_UNHANDLED(handled
= false)
131 IPC_END_MESSAGE_MAP()
135 void MediaWebContentsObserver::OnSetCdm(RenderFrameHost
* render_frame_host
,
138 media::MediaPlayerAndroid
* media_player
=
139 GetMediaPlayerManager(render_frame_host
)->GetPlayer(player_id
);
141 NOTREACHED() << "OnSetCdm: MediaPlayer not found for " << player_id
;
145 // MediaPlayerAndroid runs on the same thread as BrowserCdmManager.
146 BrowserCdmManager
* browser_cdm_manager
=
147 BrowserCdmManager::FromProcess(render_frame_host
->GetProcess()->GetID());
148 if (!browser_cdm_manager
) {
149 NOTREACHED() << "OnSetCdm: CDM not found for " << cdm_id
;
153 media::BrowserCdm
* cdm
=
154 browser_cdm_manager
->GetCdm(render_frame_host
->GetRoutingID(), cdm_id
);
156 NOTREACHED() << "OnSetCdm: CDM not found for " << cdm_id
;
160 // TODO(xhwang): This could possibly fail. In that case we should reject the
162 media_player
->SetCdm(cdm
);
165 BrowserMediaPlayerManager
* MediaWebContentsObserver::GetMediaPlayerManager(
166 RenderFrameHost
* render_frame_host
) {
167 uintptr_t key
= reinterpret_cast<uintptr_t>(render_frame_host
);
168 if (!media_player_managers_
.contains(key
)) {
169 media_player_managers_
.set(
171 make_scoped_ptr(BrowserMediaPlayerManager::Create(
172 render_frame_host
, GetMediaPlayersObserver())));
174 return media_player_managers_
.get(key
);
177 MediaPlayersObserver
*
178 MediaWebContentsObserver::GetMediaPlayersObserver() const {
179 AudioStateProvider
* provider
=
180 static_cast<WebContentsImpl
*>(web_contents())->audio_state_provider();
182 MediaPlayersObserver
* audio_observer
=
183 static_cast<MediaPlayersObserver
*>(provider
);
185 DCHECK(audio_observer
);
186 return audio_observer
;
189 #if defined(VIDEO_HOLE)
190 void MediaWebContentsObserver::OnFrameInfoUpdated() {
191 for (MediaPlayerManagerMap::iterator iter
= media_player_managers_
.begin();
192 iter
!= media_player_managers_
.end(); ++iter
) {
193 BrowserMediaPlayerManager
* manager
= iter
->second
;
194 manager
->OnFrameInfoUpdated();
197 #endif // defined(VIDEO_HOLE)
199 #endif // defined(OS_ANDROID)
201 } // namespace content