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/webmediaplayer_android.h"
10 #include "content/renderer/media/crypto/renderer_cdm_manager.h"
11 #include "content/renderer/render_view_impl.h"
12 #include "media/base/cdm_context.h"
13 #include "ui/gfx/geometry/rect_f.h"
17 RendererMediaPlayerManager::RendererMediaPlayerManager(
18 RenderFrame
* render_frame
)
19 : RenderFrameObserver(render_frame
),
20 next_media_player_id_(0) {
23 RendererMediaPlayerManager::~RendererMediaPlayerManager() {
24 DCHECK(media_players_
.empty())
25 << "RendererMediaPlayerManager is owned by RenderFrameImpl and is "
26 "destroyed only after all media players are destroyed.";
29 bool RendererMediaPlayerManager::OnMessageReceived(const IPC::Message
& msg
) {
31 IPC_BEGIN_MESSAGE_MAP(RendererMediaPlayerManager
, msg
)
32 IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaMetadataChanged
,
33 OnMediaMetadataChanged
)
34 IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaPlaybackCompleted
,
35 OnMediaPlaybackCompleted
)
36 IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaBufferingUpdate
,
37 OnMediaBufferingUpdate
)
38 IPC_MESSAGE_HANDLER(MediaPlayerMsg_SeekRequest
, OnSeekRequest
)
39 IPC_MESSAGE_HANDLER(MediaPlayerMsg_SeekCompleted
, OnSeekCompleted
)
40 IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaError
, OnMediaError
)
41 IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaVideoSizeChanged
,
43 IPC_MESSAGE_HANDLER(MediaPlayerMsg_MediaTimeUpdate
, OnTimeUpdate
)
44 IPC_MESSAGE_HANDLER(MediaPlayerMsg_WaitingForDecryptionKey
,
45 OnWaitingForDecryptionKey
)
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_DidExitFullscreen
, OnDidExitFullscreen
)
53 IPC_MESSAGE_HANDLER(MediaPlayerMsg_DidMediaPlayerPlay
, OnPlayerPlay
)
54 IPC_MESSAGE_HANDLER(MediaPlayerMsg_DidMediaPlayerPause
, OnPlayerPause
)
55 IPC_MESSAGE_HANDLER(MediaPlayerMsg_RemoteRouteAvailabilityChanged
,
56 OnRemoteRouteAvailabilityChanged
)
57 IPC_MESSAGE_UNHANDLED(handled
= false)
62 void RendererMediaPlayerManager::WasHidden() {
63 ReleaseVideoResources();
66 void RendererMediaPlayerManager::Initialize(
67 MediaPlayerHostMsg_Initialize_Type type
,
70 const GURL
& first_party_for_cookies
,
71 int demuxer_client_id
,
72 const GURL
& frame_url
,
73 bool allow_credentials
) {
74 MediaPlayerHostMsg_Initialize_Params media_player_params
;
75 media_player_params
.type
= type
;
76 media_player_params
.player_id
= player_id
;
77 media_player_params
.demuxer_client_id
= demuxer_client_id
;
78 media_player_params
.url
= url
;
79 media_player_params
.first_party_for_cookies
= first_party_for_cookies
;
80 media_player_params
.frame_url
= frame_url
;
81 media_player_params
.allow_credentials
= allow_credentials
;
83 Send(new MediaPlayerHostMsg_Initialize(routing_id(), media_player_params
));
86 void RendererMediaPlayerManager::Start(int player_id
) {
87 Send(new MediaPlayerHostMsg_Start(routing_id(), player_id
));
90 void RendererMediaPlayerManager::Pause(
92 bool is_media_related_action
) {
93 Send(new MediaPlayerHostMsg_Pause(
94 routing_id(), player_id
, is_media_related_action
));
97 void RendererMediaPlayerManager::Seek(
99 const base::TimeDelta
& time
) {
100 Send(new MediaPlayerHostMsg_Seek(routing_id(), player_id
, time
));
103 void RendererMediaPlayerManager::SetVolume(int player_id
, double volume
) {
104 Send(new MediaPlayerHostMsg_SetVolume(routing_id(), player_id
, volume
));
107 void RendererMediaPlayerManager::SetPoster(int player_id
, const GURL
& poster
) {
108 Send(new MediaPlayerHostMsg_SetPoster(routing_id(), player_id
, poster
));
111 void RendererMediaPlayerManager::ReleaseResources(int player_id
) {
112 Send(new MediaPlayerHostMsg_Release(routing_id(), player_id
));
115 void RendererMediaPlayerManager::DestroyPlayer(int player_id
) {
116 Send(new MediaPlayerHostMsg_DestroyMediaPlayer(routing_id(), player_id
));
119 void RendererMediaPlayerManager::RequestRemotePlayback(int player_id
) {
120 Send(new MediaPlayerHostMsg_RequestRemotePlayback(routing_id(), player_id
));
123 void RendererMediaPlayerManager::RequestRemotePlaybackControl(int player_id
) {
124 Send(new MediaPlayerHostMsg_RequestRemotePlaybackControl(routing_id(),
128 void RendererMediaPlayerManager::OnMediaMetadataChanged(
130 base::TimeDelta duration
,
134 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
136 player
->OnMediaMetadataChanged(duration
, width
, height
, success
);
139 void RendererMediaPlayerManager::OnMediaPlaybackCompleted(int player_id
) {
140 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
142 player
->OnPlaybackComplete();
145 void RendererMediaPlayerManager::OnMediaBufferingUpdate(int player_id
,
147 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
149 player
->OnBufferingUpdate(percent
);
152 void RendererMediaPlayerManager::OnSeekRequest(
154 const base::TimeDelta
& time_to_seek
) {
155 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
157 player
->OnSeekRequest(time_to_seek
);
160 void RendererMediaPlayerManager::OnSeekCompleted(
162 const base::TimeDelta
& current_time
) {
163 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
165 player
->OnSeekComplete(current_time
);
168 void RendererMediaPlayerManager::OnMediaError(int player_id
, int error
) {
169 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
171 player
->OnMediaError(error
);
174 void RendererMediaPlayerManager::OnVideoSizeChanged(int player_id
,
177 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
179 player
->OnVideoSizeChanged(width
, height
);
182 void RendererMediaPlayerManager::OnTimeUpdate(
184 base::TimeDelta current_timestamp
,
185 base::TimeTicks current_time_ticks
) {
186 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
188 player
->OnTimeUpdate(current_timestamp
, current_time_ticks
);
191 void RendererMediaPlayerManager::OnWaitingForDecryptionKey(int player_id
) {
192 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
194 player
->OnWaitingForDecryptionKey();
197 void RendererMediaPlayerManager::OnMediaPlayerReleased(int player_id
) {
198 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
200 player
->OnPlayerReleased();
203 void RendererMediaPlayerManager::OnConnectedToRemoteDevice(int player_id
,
204 const std::string
& remote_playback_message
) {
205 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
207 player
->OnConnectedToRemoteDevice(remote_playback_message
);
210 void RendererMediaPlayerManager::OnDisconnectedFromRemoteDevice(int player_id
) {
211 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
213 player
->OnDisconnectedFromRemoteDevice();
216 void RendererMediaPlayerManager::OnDidExitFullscreen(int player_id
) {
217 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
219 player
->OnDidExitFullscreen();
222 void RendererMediaPlayerManager::OnPlayerPlay(int player_id
) {
223 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
225 player
->OnMediaPlayerPlay();
228 void RendererMediaPlayerManager::OnPlayerPause(int player_id
) {
229 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
231 player
->OnMediaPlayerPause();
234 void RendererMediaPlayerManager::OnRemoteRouteAvailabilityChanged(
236 bool routes_available
) {
237 WebMediaPlayerAndroid
* player
= GetMediaPlayer(player_id
);
239 player
->OnRemoteRouteAvailabilityChanged(routes_available
);
242 void RendererMediaPlayerManager::EnterFullscreen(int player_id
) {
243 Send(new MediaPlayerHostMsg_EnterFullscreen(routing_id(), player_id
));
246 void RendererMediaPlayerManager::SetCdm(int player_id
, int cdm_id
) {
247 if (cdm_id
== media::CdmContext::kInvalidCdmId
) {
251 Send(new MediaPlayerHostMsg_SetCdm(routing_id(), player_id
, cdm_id
));
254 int RendererMediaPlayerManager::RegisterMediaPlayer(
255 WebMediaPlayerAndroid
* player
) {
256 media_players_
[next_media_player_id_
] = player
;
257 return next_media_player_id_
++;
260 void RendererMediaPlayerManager::UnregisterMediaPlayer(int player_id
) {
261 media_players_
.erase(player_id
);
264 void RendererMediaPlayerManager::ReleaseVideoResources() {
265 std::map
<int, WebMediaPlayerAndroid
*>::iterator player_it
;
266 for (player_it
= media_players_
.begin(); player_it
!= media_players_
.end();
268 WebMediaPlayerAndroid
* player
= player_it
->second
;
270 // Do not release if an audio track is still playing
271 if (player
&& (player
->paused() || player
->hasVideo()))
272 player
->ReleaseMediaResources();
276 WebMediaPlayerAndroid
* RendererMediaPlayerManager::GetMediaPlayer(
278 std::map
<int, WebMediaPlayerAndroid
*>::iterator iter
=
279 media_players_
.find(player_id
);
280 if (iter
!= media_players_
.end())
285 #if defined(VIDEO_HOLE)
286 void RendererMediaPlayerManager::RequestExternalSurface(
288 const gfx::RectF
& geometry
) {
289 Send(new MediaPlayerHostMsg_NotifyExternalSurface(
290 routing_id(), player_id
, true, geometry
));
293 void RendererMediaPlayerManager::DidCommitCompositorFrame() {
294 std::map
<int, gfx::RectF
> geometry_change
;
295 RetrieveGeometryChanges(&geometry_change
);
296 for (std::map
<int, gfx::RectF
>::iterator it
= geometry_change
.begin();
297 it
!= geometry_change
.end();
299 Send(new MediaPlayerHostMsg_NotifyExternalSurface(
300 routing_id(), it
->first
, false, it
->second
));
304 void RendererMediaPlayerManager::RetrieveGeometryChanges(
305 std::map
<int, gfx::RectF
>* changes
) {
306 DCHECK(changes
->empty());
307 for (std::map
<int, WebMediaPlayerAndroid
*>::iterator player_it
=
308 media_players_
.begin();
309 player_it
!= media_players_
.end();
311 WebMediaPlayerAndroid
* player
= player_it
->second
;
313 if (player
&& player
->hasVideo()) {
314 if (player
->UpdateBoundaryRectangle())
315 (*changes
)[player_it
->first
] = player
->GetBoundaryRectangle();
321 RendererMediaPlayerManager::ShouldUseVideoOverlayForEmbeddedEncryptedVideo() {
322 const RendererPreferences
& prefs
= static_cast<RenderFrameImpl
*>(
323 render_frame())->render_view()->renderer_preferences();
324 return prefs
.use_video_overlay_for_embedded_encrypted_video
;
326 #endif // defined(VIDEO_HOLE)
328 } // namespace content