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.
5 #include "content/renderer/media/android/renderer_media_player_manager.h"
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"
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
) {
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.";
31 bool RendererMediaPlayerManager::OnMessageReceived(const IPC::Message
& msg
) {
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
,
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
,
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_PauseVideo
, OnPauseVideo
)
59 IPC_MESSAGE_UNHANDLED(handled
= false)
64 void RendererMediaPlayerManager::Initialize(
65 MediaPlayerHostMsg_Initialize_Type type
,
68 const GURL
& first_party_for_cookies
,
69 int demuxer_client_id
,
70 const GURL
& frame_url
,
71 bool allow_credentials
) {
72 MediaPlayerHostMsg_Initialize_Params media_player_params
;
73 media_player_params
.type
= type
;
74 media_player_params
.player_id
= player_id
;
75 media_player_params
.demuxer_client_id
= demuxer_client_id
;
76 media_player_params
.url
= url
;
77 media_player_params
.first_party_for_cookies
= first_party_for_cookies
;
78 media_player_params
.frame_url
= frame_url
;
79 media_player_params
.allow_credentials
= allow_credentials
;
81 Send(new MediaPlayerHostMsg_Initialize(routing_id(), media_player_params
));
84 void RendererMediaPlayerManager::Start(int player_id
) {
85 Send(new MediaPlayerHostMsg_Start(routing_id(), player_id
));
88 void RendererMediaPlayerManager::Pause(
90 bool is_media_related_action
) {
91 Send(new MediaPlayerHostMsg_Pause(
92 routing_id(), player_id
, is_media_related_action
));
95 void RendererMediaPlayerManager::Seek(
97 const base::TimeDelta
& time
) {
98 Send(new MediaPlayerHostMsg_Seek(routing_id(), player_id
, time
));
101 void RendererMediaPlayerManager::SetVolume(int player_id
, double volume
) {
102 Send(new MediaPlayerHostMsg_SetVolume(routing_id(), player_id
, volume
));
105 void RendererMediaPlayerManager::SetPoster(int player_id
, const GURL
& poster
) {
106 Send(new MediaPlayerHostMsg_SetPoster(routing_id(), player_id
, poster
));
109 void RendererMediaPlayerManager::ReleaseResources(int player_id
) {
110 Send(new MediaPlayerHostMsg_Release(routing_id(), player_id
));
113 void RendererMediaPlayerManager::DestroyPlayer(int player_id
) {
114 Send(new MediaPlayerHostMsg_DestroyMediaPlayer(routing_id(), player_id
));
117 void RendererMediaPlayerManager::OnMediaMetadataChanged(
119 base::TimeDelta duration
,
123 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
125 player
->OnMediaMetadataChanged(duration
, width
, height
, success
);
128 void RendererMediaPlayerManager::OnMediaPlaybackCompleted(int player_id
) {
129 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
131 player
->OnPlaybackComplete();
134 void RendererMediaPlayerManager::OnMediaBufferingUpdate(int player_id
,
136 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
138 player
->OnBufferingUpdate(percent
);
141 void RendererMediaPlayerManager::OnSeekRequest(
143 const base::TimeDelta
& time_to_seek
) {
144 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
146 player
->OnSeekRequest(time_to_seek
);
149 void RendererMediaPlayerManager::OnSeekCompleted(
151 const base::TimeDelta
& current_time
) {
152 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
154 player
->OnSeekComplete(current_time
);
157 void RendererMediaPlayerManager::OnMediaError(int player_id
, int error
) {
158 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
160 player
->OnMediaError(error
);
163 void RendererMediaPlayerManager::OnVideoSizeChanged(int player_id
,
166 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
168 player
->OnVideoSizeChanged(width
, height
);
171 void RendererMediaPlayerManager::OnTimeUpdate(int player_id
,
172 base::TimeDelta current_time
) {
173 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
175 player
->OnTimeUpdate(current_time
);
178 void RendererMediaPlayerManager::OnMediaPlayerReleased(int player_id
) {
179 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
181 player
->OnPlayerReleased();
184 void RendererMediaPlayerManager::OnConnectedToRemoteDevice(int player_id
,
185 const std::string
& remote_playback_message
) {
186 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
188 player
->OnConnectedToRemoteDevice(remote_playback_message
);
191 void RendererMediaPlayerManager::OnDisconnectedFromRemoteDevice(int player_id
) {
192 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
194 player
->OnDisconnectedFromRemoteDevice();
197 void RendererMediaPlayerManager::OnDidEnterFullscreen(int player_id
) {
198 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
200 player
->OnDidEnterFullscreen();
203 void RendererMediaPlayerManager::OnDidExitFullscreen(int player_id
) {
204 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
206 player
->OnDidExitFullscreen();
209 void RendererMediaPlayerManager::OnPlayerPlay(int player_id
) {
210 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
212 player
->OnMediaPlayerPlay();
215 void RendererMediaPlayerManager::OnPlayerPause(int player_id
) {
216 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
218 player
->OnMediaPlayerPause();
221 void RendererMediaPlayerManager::OnRequestFullscreen(int player_id
) {
222 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
224 player
->OnRequestFullscreen();
227 void RendererMediaPlayerManager::OnPauseVideo() {
228 ReleaseVideoResources();
231 void RendererMediaPlayerManager::EnterFullscreen(int player_id
,
232 blink::WebFrame
* frame
) {
233 pending_fullscreen_frame_
= frame
;
234 Send(new MediaPlayerHostMsg_EnterFullscreen(routing_id(), player_id
));
237 void RendererMediaPlayerManager::ExitFullscreen(int player_id
) {
238 pending_fullscreen_frame_
= NULL
;
239 fullscreen_frame_
= NULL
;
240 Send(new MediaPlayerHostMsg_ExitFullscreen(routing_id(), player_id
));
243 void RendererMediaPlayerManager::SetCdm(int player_id
, int cdm_id
) {
244 if (cdm_id
== RendererCdmManager::kInvalidCdmId
) {
248 Send(new MediaPlayerHostMsg_SetCdm(routing_id(), player_id
, cdm_id
));
251 int RendererMediaPlayerManager::RegisterMediaPlayer(
252 WebMediaPlayerAndroid
* player
) {
253 media_players_
[next_media_player_id_
] = player
;
254 return next_media_player_id_
++;
257 void RendererMediaPlayerManager::UnregisterMediaPlayer(int player_id
) {
258 media_players_
.erase(player_id
);
261 void RendererMediaPlayerManager::ReleaseVideoResources() {
262 std::map
<int, WebMediaPlayerAndroid
*>::iterator player_it
;
263 for (player_it
= media_players_
.begin(); player_it
!= media_players_
.end();
265 WebMediaPlayerAndroid
* player
= player_it
->second
;
267 // Do not release if an audio track is still playing
268 if (player
&& (player
->paused() || player
->hasVideo()))
269 player
->ReleaseMediaResources();
273 WebMediaPlayerAndroid
* RendererMediaPlayerManager::GetMediaPlayer(
275 std::map
<int, WebMediaPlayerAndroid
*>::iterator iter
=
276 media_players_
.find(player_id
);
277 if (iter
!= media_players_
.end())
282 bool RendererMediaPlayerManager::CanEnterFullscreen(blink::WebFrame
* frame
) {
283 return (!fullscreen_frame_
&& !pending_fullscreen_frame_
)
284 || ShouldEnterFullscreen(frame
);
287 void RendererMediaPlayerManager::DidEnterFullscreen(blink::WebFrame
* frame
) {
288 pending_fullscreen_frame_
= NULL
;
289 fullscreen_frame_
= frame
;
292 void RendererMediaPlayerManager::DidExitFullscreen() {
293 fullscreen_frame_
= NULL
;
296 bool RendererMediaPlayerManager::IsInFullscreen(blink::WebFrame
* frame
) {
297 return fullscreen_frame_
== frame
;
300 bool RendererMediaPlayerManager::ShouldEnterFullscreen(blink::WebFrame
* frame
) {
301 return fullscreen_frame_
== frame
|| pending_fullscreen_frame_
== frame
;
304 #if defined(VIDEO_HOLE)
305 void RendererMediaPlayerManager::RequestExternalSurface(
307 const gfx::RectF
& geometry
) {
308 Send(new MediaPlayerHostMsg_NotifyExternalSurface(
309 routing_id(), player_id
, true, geometry
));
312 void RendererMediaPlayerManager::DidCommitCompositorFrame() {
313 std::map
<int, gfx::RectF
> geometry_change
;
314 RetrieveGeometryChanges(&geometry_change
);
315 for (std::map
<int, gfx::RectF
>::iterator it
= geometry_change
.begin();
316 it
!= geometry_change
.end();
318 Send(new MediaPlayerHostMsg_NotifyExternalSurface(
319 routing_id(), it
->first
, false, it
->second
));
323 void RendererMediaPlayerManager::RetrieveGeometryChanges(
324 std::map
<int, gfx::RectF
>* changes
) {
325 DCHECK(changes
->empty());
326 for (std::map
<int, WebMediaPlayerAndroid
*>::iterator player_it
=
327 media_players_
.begin();
328 player_it
!= media_players_
.end();
330 WebMediaPlayerAndroid
* player
= player_it
->second
;
332 if (player
&& player
->hasVideo()) {
333 if (player
->UpdateBoundaryRectangle())
334 (*changes
)[player_it
->first
] = player
->GetBoundaryRectangle();
340 RendererMediaPlayerManager::ShouldUseVideoOverlayForEmbeddedEncryptedVideo() {
341 const RendererPreferences
& prefs
= static_cast<RenderFrameImpl
*>(
342 render_frame())->render_view()->renderer_preferences();
343 return prefs
.use_video_overlay_for_embedded_encrypted_video
;
345 #endif // defined(VIDEO_HOLE)
347 } // namespace content