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 "media/base/cdm_context.h"
14 #include "ui/gfx/geometry/rect_f.h"
18 RendererMediaPlayerManager::RendererMediaPlayerManager(
19 RenderFrame
* render_frame
)
20 : RenderFrameObserver(render_frame
),
21 next_media_player_id_(0) {
24 RendererMediaPlayerManager::~RendererMediaPlayerManager() {
25 DCHECK(media_players_
.empty())
26 << "RendererMediaPlayerManager is owned by RenderFrameImpl and is "
27 "destroyed only after all media players are destroyed.";
30 bool RendererMediaPlayerManager::OnMessageReceived(const IPC::Message
& msg
) {
32 IPC_BEGIN_MESSAGE_MAP(RendererMediaPlayerManager
, msg
)
33 IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaMetadataChanged
,
34 OnMediaMetadataChanged
)
35 IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaPlaybackCompleted
,
36 OnMediaPlaybackCompleted
)
37 IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaBufferingUpdate
,
38 OnMediaBufferingUpdate
)
39 IPC_MESSAGE_HANDLER(MediaPlayerMsg_SeekRequest
, OnSeekRequest
)
40 IPC_MESSAGE_HANDLER(MediaPlayerMsg_SeekCompleted
, OnSeekCompleted
)
41 IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaError
, OnMediaError
)
42 IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaVideoSizeChanged
,
44 IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaTimeUpdate
, OnTimeUpdate
)
45 IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaPlayerReleased
,
46 OnMediaPlayerReleased
)
47 IPC_MESSAGE_HANDLER(MediaPlayerMsg_ConnectedToRemoteDevice
,
48 OnConnectedToRemoteDevice
)
49 IPC_MESSAGE_HANDLER(MediaPlayerMsg_DisconnectedFromRemoteDevice
,
50 OnDisconnectedFromRemoteDevice
)
51 IPC_MESSAGE_HANDLER(MediaPlayerMsg_RequestFullscreen
,
53 IPC_MESSAGE_HANDLER(MediaPlayerMsg_DidExitFullscreen
, OnDidExitFullscreen
)
54 IPC_MESSAGE_HANDLER(MediaPlayerMsg_DidMediaPlayerPlay
, OnPlayerPlay
)
55 IPC_MESSAGE_HANDLER(MediaPlayerMsg_DidMediaPlayerPause
, OnPlayerPause
)
56 IPC_MESSAGE_HANDLER(MediaPlayerMsg_RemoteRouteAvailabilityChanged
,
57 OnRemoteRouteAvailabilityChanged
)
58 IPC_MESSAGE_UNHANDLED(handled
= false)
63 void RendererMediaPlayerManager::WasHidden() {
64 ReleaseVideoResources();
67 void RendererMediaPlayerManager::Initialize(
68 MediaPlayerHostMsg_Initialize_Type type
,
71 const GURL
& first_party_for_cookies
,
72 int demuxer_client_id
,
73 const GURL
& frame_url
,
74 bool allow_credentials
) {
75 MediaPlayerHostMsg_Initialize_Params media_player_params
;
76 media_player_params
.type
= type
;
77 media_player_params
.player_id
= player_id
;
78 media_player_params
.demuxer_client_id
= demuxer_client_id
;
79 media_player_params
.url
= url
;
80 media_player_params
.first_party_for_cookies
= first_party_for_cookies
;
81 media_player_params
.frame_url
= frame_url
;
82 media_player_params
.allow_credentials
= allow_credentials
;
84 Send(new MediaPlayerHostMsg_Initialize(routing_id(), media_player_params
));
87 void RendererMediaPlayerManager::Start(int player_id
) {
88 Send(new MediaPlayerHostMsg_Start(routing_id(), player_id
));
91 void RendererMediaPlayerManager::Pause(
93 bool is_media_related_action
) {
94 Send(new MediaPlayerHostMsg_Pause(
95 routing_id(), player_id
, is_media_related_action
));
98 void RendererMediaPlayerManager::Seek(
100 const base::TimeDelta
& time
) {
101 Send(new MediaPlayerHostMsg_Seek(routing_id(), player_id
, time
));
104 void RendererMediaPlayerManager::SetVolume(int player_id
, double volume
) {
105 Send(new MediaPlayerHostMsg_SetVolume(routing_id(), player_id
, volume
));
108 void RendererMediaPlayerManager::SetPoster(int player_id
, const GURL
& poster
) {
109 Send(new MediaPlayerHostMsg_SetPoster(routing_id(), player_id
, poster
));
112 void RendererMediaPlayerManager::ReleaseResources(int player_id
) {
113 Send(new MediaPlayerHostMsg_Release(routing_id(), player_id
));
116 void RendererMediaPlayerManager::DestroyPlayer(int player_id
) {
117 Send(new MediaPlayerHostMsg_DestroyMediaPlayer(routing_id(), player_id
));
120 void RendererMediaPlayerManager::RequestRemotePlayback(int player_id
) {
121 Send(new MediaPlayerHostMsg_RequestRemotePlayback(routing_id(), player_id
));
124 void RendererMediaPlayerManager::RequestRemotePlaybackControl(int player_id
) {
125 Send(new MediaPlayerHostMsg_RequestRemotePlaybackControl(routing_id(),
129 void RendererMediaPlayerManager::OnMediaMetadataChanged(
131 base::TimeDelta duration
,
135 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
137 player
->OnMediaMetadataChanged(duration
, width
, height
, success
);
140 void RendererMediaPlayerManager::OnMediaPlaybackCompleted(int player_id
) {
141 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
143 player
->OnPlaybackComplete();
146 void RendererMediaPlayerManager::OnMediaBufferingUpdate(int player_id
,
148 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
150 player
->OnBufferingUpdate(percent
);
153 void RendererMediaPlayerManager::OnSeekRequest(
155 const base::TimeDelta
& time_to_seek
) {
156 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
158 player
->OnSeekRequest(time_to_seek
);
161 void RendererMediaPlayerManager::OnSeekCompleted(
163 const base::TimeDelta
& current_time
) {
164 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
166 player
->OnSeekComplete(current_time
);
169 void RendererMediaPlayerManager::OnMediaError(int player_id
, int error
) {
170 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
172 player
->OnMediaError(error
);
175 void RendererMediaPlayerManager::OnVideoSizeChanged(int player_id
,
178 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
180 player
->OnVideoSizeChanged(width
, height
);
183 void RendererMediaPlayerManager::OnTimeUpdate(
185 base::TimeDelta current_timestamp
,
186 base::TimeTicks current_time_ticks
) {
187 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
189 player
->OnTimeUpdate(current_timestamp
, current_time_ticks
);
192 void RendererMediaPlayerManager::OnMediaPlayerReleased(int player_id
) {
193 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
195 player
->OnPlayerReleased();
198 void RendererMediaPlayerManager::OnConnectedToRemoteDevice(int player_id
,
199 const std::string
& remote_playback_message
) {
200 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
202 player
->OnConnectedToRemoteDevice(remote_playback_message
);
205 void RendererMediaPlayerManager::OnDisconnectedFromRemoteDevice(int player_id
) {
206 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
208 player
->OnDisconnectedFromRemoteDevice();
211 void RendererMediaPlayerManager::OnDidExitFullscreen(int player_id
) {
212 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
214 player
->OnDidExitFullscreen();
217 void RendererMediaPlayerManager::OnPlayerPlay(int player_id
) {
218 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
220 player
->OnMediaPlayerPlay();
223 void RendererMediaPlayerManager::OnPlayerPause(int player_id
) {
224 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
226 player
->OnMediaPlayerPause();
229 void RendererMediaPlayerManager::OnRequestFullscreen(int player_id
) {
230 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
232 player
->OnRequestFullscreen();
235 void RendererMediaPlayerManager::OnRemoteRouteAvailabilityChanged(
237 bool routes_available
) {
238 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
240 player
->OnRemoteRouteAvailabilityChanged(routes_available
);
243 void RendererMediaPlayerManager::EnterFullscreen(int player_id
) {
244 Send(new MediaPlayerHostMsg_EnterFullscreen(routing_id(), player_id
));
247 void RendererMediaPlayerManager::ExitFullscreen(int player_id
) {
248 Send(new MediaPlayerHostMsg_ExitFullscreen(routing_id(), player_id
));
251 void RendererMediaPlayerManager::SetCdm(int player_id
, int cdm_id
) {
252 if (cdm_id
== media::CdmContext::kInvalidCdmId
) {
256 Send(new MediaPlayerHostMsg_SetCdm(routing_id(), player_id
, cdm_id
));
259 int RendererMediaPlayerManager::RegisterMediaPlayer(
260 WebMediaPlayerAndroid
* player
) {
261 media_players_
[next_media_player_id_
] = player
;
262 return next_media_player_id_
++;
265 void RendererMediaPlayerManager::UnregisterMediaPlayer(int player_id
) {
266 media_players_
.erase(player_id
);
269 void RendererMediaPlayerManager::ReleaseVideoResources() {
270 std::map
<int, WebMediaPlayerAndroid
*>::iterator player_it
;
271 for (player_it
= media_players_
.begin(); player_it
!= media_players_
.end();
273 WebMediaPlayerAndroid
* player
= player_it
->second
;
275 // Do not release if an audio track is still playing
276 if (player
&& (player
->paused() || player
->hasVideo()))
277 player
->ReleaseMediaResources();
281 WebMediaPlayerAndroid
* RendererMediaPlayerManager::GetMediaPlayer(
283 std::map
<int, WebMediaPlayerAndroid
*>::iterator iter
=
284 media_players_
.find(player_id
);
285 if (iter
!= media_players_
.end())
290 #if defined(VIDEO_HOLE)
291 void RendererMediaPlayerManager::RequestExternalSurface(
293 const gfx::RectF
& geometry
) {
294 Send(new MediaPlayerHostMsg_NotifyExternalSurface(
295 routing_id(), player_id
, true, geometry
));
298 void RendererMediaPlayerManager::DidCommitCompositorFrame() {
299 std::map
<int, gfx::RectF
> geometry_change
;
300 RetrieveGeometryChanges(&geometry_change
);
301 for (std::map
<int, gfx::RectF
>::iterator it
= geometry_change
.begin();
302 it
!= geometry_change
.end();
304 Send(new MediaPlayerHostMsg_NotifyExternalSurface(
305 routing_id(), it
->first
, false, it
->second
));
309 void RendererMediaPlayerManager::RetrieveGeometryChanges(
310 std::map
<int, gfx::RectF
>* changes
) {
311 DCHECK(changes
->empty());
312 for (std::map
<int, WebMediaPlayerAndroid
*>::iterator player_it
=
313 media_players_
.begin();
314 player_it
!= media_players_
.end();
316 WebMediaPlayerAndroid
* player
= player_it
->second
;
318 if (player
&& player
->hasVideo()) {
319 if (player
->UpdateBoundaryRectangle())
320 (*changes
)[player_it
->first
] = player
->GetBoundaryRectangle();
326 RendererMediaPlayerManager::ShouldUseVideoOverlayForEmbeddedEncryptedVideo() {
327 const RendererPreferences
& prefs
= static_cast<RenderFrameImpl
*>(
328 render_frame())->render_view()->renderer_preferences();
329 return prefs
.use_video_overlay_for_embedded_encrypted_video
;
331 #endif // defined(VIDEO_HOLE)
333 } // namespace content