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 #ifndef CONTENT_BROWSER_MEDIA_ANDROID_BROWSER_MEDIA_PLAYER_MANAGER_H_
6 #define CONTENT_BROWSER_MEDIA_ANDROID_BROWSER_MEDIA_PLAYER_MANAGER_H_
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/scoped_vector.h"
16 #include "base/time/time.h"
17 #include "content/browser/android/content_video_view.h"
18 #include "content/common/media/cdm_messages_enums.h"
19 #include "content/common/media/media_player_messages_enums_android.h"
20 #include "content/public/browser/web_contents_observer.h"
21 #include "media/base/android/media_player_android.h"
22 #include "media/base/android/media_player_manager.h"
23 #include "ui/gfx/rect_f.h"
32 class BrowserDemuxerAndroid
;
33 class ContentViewCoreImpl
;
34 class ExternalVideoSurfaceContainer
;
37 // This class manages all the MediaPlayerAndroid objects. It receives
38 // control operations from the the render process, and forwards
39 // them to corresponding MediaPlayerAndroid object. Callbacks from
40 // MediaPlayerAndroid objects are converted to IPCs and then sent to the
42 class CONTENT_EXPORT BrowserMediaPlayerManager
43 : public WebContentsObserver
,
44 public media::MediaPlayerManager
{
46 // Permits embedders to provide an extended version of the class.
47 typedef BrowserMediaPlayerManager
* (*Factory
)(RenderViewHost
*);
48 static void RegisterFactory(Factory factory
);
50 // Returns a new instance using the registered factory if available.
51 static BrowserMediaPlayerManager
* Create(RenderViewHost
* rvh
);
53 ContentViewCoreImpl
* GetContentViewCore() const;
55 virtual ~BrowserMediaPlayerManager();
57 // WebContentsObserver overrides.
58 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
60 // Fullscreen video playback controls.
61 virtual void FullscreenPlayerPlay();
62 virtual void FullscreenPlayerPause();
63 virtual void FullscreenPlayerSeek(int msec
);
64 virtual void ExitFullscreen(bool release_media_player
);
65 virtual void SetVideoSurface(gfx::ScopedJavaSurface surface
);
67 // Called when browser player wants the renderer media element to seek.
68 // Any actual seek started by renderer will be handled by browser in OnSeek().
69 void OnSeekRequest(int player_id
, const base::TimeDelta
& time_to_seek
);
71 // media::MediaPlayerManager overrides.
72 virtual void OnTimeUpdate(
73 int player_id
, base::TimeDelta current_time
) OVERRIDE
;
74 virtual void OnMediaMetadataChanged(
76 base::TimeDelta duration
,
79 bool success
) OVERRIDE
;
80 virtual void OnPlaybackComplete(int player_id
) OVERRIDE
;
81 virtual void OnMediaInterrupted(int player_id
) OVERRIDE
;
82 virtual void OnBufferingUpdate(int player_id
, int percentage
) OVERRIDE
;
83 virtual void OnSeekComplete(
85 const base::TimeDelta
& current_time
) OVERRIDE
;
86 virtual void OnError(int player_id
, int error
) OVERRIDE
;
87 virtual void OnVideoSizeChanged(
88 int player_id
, int width
, int height
) OVERRIDE
;
89 virtual media::MediaResourceGetter
* GetMediaResourceGetter() OVERRIDE
;
90 virtual media::MediaPlayerAndroid
* GetFullscreenPlayer() OVERRIDE
;
91 virtual media::MediaPlayerAndroid
* GetPlayer(int player_id
) OVERRIDE
;
92 virtual media::MediaDrmBridge
* GetDrmBridge(int cdm_id
) OVERRIDE
;
93 virtual void DestroyAllMediaPlayers() OVERRIDE
;
94 virtual void RequestFullScreen(int player_id
) OVERRIDE
;
95 virtual void OnSessionCreated(int cdm_id
,
97 const std::string
& web_session_id
) OVERRIDE
;
98 virtual void OnSessionMessage(int cdm_id
,
100 const std::vector
<uint8
>& message
,
101 const GURL
& destination_url
) OVERRIDE
;
102 virtual void OnSessionReady(int cdm_id
, uint32 session_id
) OVERRIDE
;
103 virtual void OnSessionClosed(int cdm_id
, uint32 session_id
) OVERRIDE
;
104 virtual void OnSessionError(int cdm_id
,
106 media::MediaKeys::KeyError error_code
,
107 uint32 system_code
) OVERRIDE
;
109 #if defined(VIDEO_HOLE)
110 void AttachExternalVideoSurface(int player_id
, jobject surface
);
111 void DetachExternalVideoSurface(int player_id
);
112 void OnFrameInfoUpdated();
113 #endif // defined(VIDEO_HOLE)
116 // Clients must use Create() or subclass constructor.
117 explicit BrowserMediaPlayerManager(RenderViewHost
* render_view_host
);
120 virtual void OnEnterFullscreen(int player_id
);
121 virtual void OnExitFullscreen(int player_id
);
122 virtual void OnInitialize(
123 MediaPlayerHostMsg_Initialize_Type type
,
126 const GURL
& first_party_for_cookies
,
127 int demuxer_client_id
);
128 virtual void OnStart(int player_id
);
129 virtual void OnSeek(int player_id
, const base::TimeDelta
& time
);
130 virtual void OnPause(int player_id
, bool is_media_related_action
);
131 virtual void OnSetVolume(int player_id
, double volume
);
132 virtual void OnSetPoster(int player_id
, const GURL
& poster
);
133 virtual void OnReleaseResources(int player_id
);
134 virtual void OnDestroyPlayer(int player_id
);
135 virtual void ReleaseFullscreenPlayer(media::MediaPlayerAndroid
* player
);
136 void OnInitializeCdm(int cdm_id
,
137 const std::string
& key_system
,
138 const GURL
& frame_url
);
139 void OnCreateSession(int cdm_id
,
141 CdmHostMsg_CreateSession_ContentType content_type
,
142 const std::vector
<uint8
>& init_data
);
143 void OnUpdateSession(int cdm_id
,
145 const std::vector
<uint8
>& response
);
146 void OnReleaseSession(int cdm_id
, uint32 session_id
);
147 void OnSetCdm(int player_id
, int cdm_id
);
148 void OnDestroyCdm(int cdm_id
);
150 // Cancels all pending session creations associated with |cdm_id|.
151 void CancelAllPendingSessionCreations(int cdm_id
);
153 // Adds a given player to the list.
154 void AddPlayer(media::MediaPlayerAndroid
* player
);
156 // Removes the player with the specified id.
157 void RemovePlayer(int player_id
);
159 // Replaces a player with the specified id with a given MediaPlayerAndroid
160 // object. This will also return the original MediaPlayerAndroid object that
162 scoped_ptr
<media::MediaPlayerAndroid
> SwapPlayer(
164 media::MediaPlayerAndroid
* player
);
166 // Adds a new MediaDrmBridge for the given |key_system|, |cdm_id|, and
168 void AddDrmBridge(int cdm_id
,
169 const std::string
& key_system
,
170 const GURL
& frame_url
);
172 // Removes the DRM bridge with the specified id.
173 void RemoveDrmBridge(int cdm_id
);
176 // If |permitted| is false, it does nothing but send
177 // |CdmMsg_SessionError| IPC message.
178 // The primary use case is infobar permission callback, i.e., when infobar
179 // can decide user's intention either from interacting with the actual info
180 // bar or from the saved preference.
181 void CreateSessionIfPermitted(int cdm_id
,
183 const std::string
& content_type
,
184 const std::vector
<uint8
>& init_data
,
187 // Constructs a MediaPlayerAndroid object.
188 media::MediaPlayerAndroid
* CreateMediaPlayer(
189 MediaPlayerHostMsg_Initialize_Type type
,
192 const GURL
& first_party_for_cookies
,
193 int demuxer_client_id
,
195 media::MediaPlayerManager
* manager
,
196 BrowserDemuxerAndroid
* demuxer
);
198 // MediaPlayerAndroid must call this before it is going to decode
199 // media streams. This helps the manager object maintain an array
200 // of active MediaPlayerAndroid objects and release the resources
201 // when needed. Currently we only count video resources as they are
202 // constrained by hardware and memory limits.
203 virtual void OnMediaResourcesRequested(int player_id
);
205 // Similar to the above call, MediaPlayerAndroid must call this method when
206 // releasing all the decoding resources.
207 virtual void OnMediaResourcesReleased(int player_id
);
209 #if defined(VIDEO_HOLE)
210 void OnNotifyExternalSurface(
211 int player_id
, bool is_request
, const gfx::RectF
& rect
);
212 void OnRequestExternalSurface(int player_id
, const gfx::RectF
& rect
);
213 #endif // defined(VIDEO_HOLE)
215 // An array of managed players.
216 ScopedVector
<media::MediaPlayerAndroid
> players_
;
218 // An array of managed media DRM bridges.
219 ScopedVector
<media::MediaDrmBridge
> drm_bridges_
;
221 // The fullscreen video view object or NULL if video is not played in
223 scoped_ptr
<ContentVideoView
> video_view_
;
225 #if defined(VIDEO_HOLE)
226 scoped_ptr
<ExternalVideoSurfaceContainer
> external_video_surface_container_
;
229 // Player ID of the fullscreen media player.
230 int fullscreen_player_id_
;
232 // Whether the fullscreen player has been Release()-d.
233 bool fullscreen_player_is_released_
;
235 WebContents
* web_contents_
;
237 // Object for retrieving resources media players.
238 scoped_ptr
<media::MediaResourceGetter
> media_resource_getter_
;
240 // NOTE: Weak pointers must be invalidated before all other member variables.
241 base::WeakPtrFactory
<BrowserMediaPlayerManager
> weak_ptr_factory_
;
243 DISALLOW_COPY_AND_ASSIGN(BrowserMediaPlayerManager
);
246 } // namespace content
248 #endif // CONTENT_BROWSER_MEDIA_ANDROID_BROWSER_MEDIA_PLAYER_MANAGER_H_