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_
13 #include "base/basictypes.h"
14 #include "base/callback.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/scoped_vector.h"
17 #include "base/time/time.h"
18 #include "content/browser/android/content_video_view.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;
35 // This class manages all the MediaPlayerAndroid objects. It receives
36 // control operations from the the render process, and forwards
37 // them to corresponding MediaPlayerAndroid object. Callbacks from
38 // MediaPlayerAndroid objects are converted to IPCs and then sent to the
40 class CONTENT_EXPORT BrowserMediaPlayerManager
41 : public WebContentsObserver,
42 public media::MediaPlayerManager {
44 // Permits embedders to provide an extended version of the class.
45 typedef BrowserMediaPlayerManager* (*Factory)(RenderViewHost*);
46 static void RegisterFactory(Factory factory);
48 // Returns a new instance using the registered factory if available.
49 static BrowserMediaPlayerManager* Create(RenderViewHost* rvh);
51 virtual ~BrowserMediaPlayerManager();
53 // WebContentsObserver overrides.
54 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
56 // Fullscreen video playback controls.
57 virtual void FullscreenPlayerPlay();
58 virtual void FullscreenPlayerPause();
59 virtual void FullscreenPlayerSeek(int msec);
60 virtual void ExitFullscreen(bool release_media_player);
61 virtual void SetVideoSurface(gfx::ScopedJavaSurface surface);
63 // Called when browser player wants the renderer media element to seek.
64 // Any actual seek started by renderer will be handled by browser in OnSeek().
65 void OnSeekRequest(int player_id, const base::TimeDelta& time_to_seek);
67 // media::MediaPlayerManager overrides.
68 virtual void OnTimeUpdate(
69 int player_id, base::TimeDelta current_time) OVERRIDE;
70 virtual void OnMediaMetadataChanged(
72 base::TimeDelta duration,
75 bool success) OVERRIDE;
76 virtual void OnPlaybackComplete(int player_id) OVERRIDE;
77 virtual void OnMediaInterrupted(int player_id) OVERRIDE;
78 virtual void OnBufferingUpdate(int player_id, int percentage) OVERRIDE;
79 virtual void OnSeekComplete(
81 const base::TimeDelta& current_time) OVERRIDE;
82 virtual void OnError(int player_id, int error) OVERRIDE;
83 virtual void OnVideoSizeChanged(
84 int player_id, int width, int height) OVERRIDE;
85 virtual void RequestMediaResources(int player_id) OVERRIDE;
86 virtual void ReleaseMediaResources(int player_id) OVERRIDE;
87 virtual media::MediaResourceGetter* GetMediaResourceGetter() OVERRIDE;
88 virtual media::MediaPlayerAndroid* GetFullscreenPlayer() OVERRIDE;
89 virtual media::MediaPlayerAndroid* GetPlayer(int player_id) OVERRIDE;
90 virtual media::MediaDrmBridge* GetDrmBridge(int media_keys_id) OVERRIDE;
91 virtual void DestroyAllMediaPlayers() OVERRIDE;
92 virtual void OnProtectedSurfaceRequested(int player_id) OVERRIDE;
93 virtual void OnKeyAdded(int media_keys_id,
94 const std::string& session_id) OVERRIDE;
95 virtual void OnKeyError(int media_keys_id,
96 const std::string& session_id,
97 media::MediaKeys::KeyError error_code,
98 int system_code) OVERRIDE;
99 virtual void OnKeyMessage(int media_keys_id,
100 const std::string& session_id,
101 const std::vector<uint8>& message,
102 const std::string& destination_url) OVERRIDE;
104 #if defined(GOOGLE_TV)
105 void AttachExternalVideoSurface(int player_id, jobject surface);
106 void DetachExternalVideoSurface(int player_id);
109 // Called to disble the current fullscreen playback if the video is encrypted.
110 // TODO(qinmin): remove this once we have the new fullscreen mode.
111 void DisableFullscreenEncryptedMediaPlayback();
114 // Clients must use Create() or subclass constructor.
115 explicit BrowserMediaPlayerManager(RenderViewHost* render_view_host);
118 virtual void OnEnterFullscreen(int player_id);
119 virtual void OnExitFullscreen(int player_id);
120 virtual void OnInitialize(
121 MediaPlayerHostMsg_Initialize_Type type,
124 const GURL& first_party_for_cookies,
125 int demuxer_client_id);
126 virtual void OnStart(int player_id);
127 virtual void OnSeek(int player_id, const base::TimeDelta& time);
128 virtual void OnPause(int player_id, bool is_media_related_action);
129 virtual void OnSetVolume(int player_id, double volume);
130 virtual void OnReleaseResources(int player_id);
131 virtual void OnDestroyPlayer(int player_id);
132 void OnInitializeCDM(int media_keys_id,
133 const std::vector<uint8>& uuid,
134 const GURL& frame_url);
135 void OnGenerateKeyRequest(int media_keys_id,
136 const std::string& type,
137 const std::vector<uint8>& init_data);
138 void OnAddKey(int media_keys_id,
139 const std::vector<uint8>& key,
140 const std::vector<uint8>& init_data,
141 const std::string& session_id);
142 void OnCancelKeyRequest(int media_keys_id, const std::string& session_id);
143 void OnSetMediaKeys(int player_id, int media_keys_id);
145 #if defined(GOOGLE_TV)
146 virtual void OnNotifyExternalSurface(
147 int player_id, bool is_request, const gfx::RectF& rect);
150 // Adds a given player to the list.
151 void AddPlayer(media::MediaPlayerAndroid* player);
153 // Removes the player with the specified id.
154 void RemovePlayer(int player_id);
156 // Replaces a player with the specified id with a given MediaPlayerAndroid
157 // object. This will also return the original MediaPlayerAndroid object that
159 scoped_ptr<media::MediaPlayerAndroid> SwapPlayer(
161 media::MediaPlayerAndroid* player);
163 // Add a new MediaDrmBridge for the given |uuid|, |media_keys_id|, and
165 void AddDrmBridge(int media_keys_id,
166 const std::vector<uint8>& uuid,
167 const GURL& frame_url);
169 // Removes the DRM bridge with the specified id.
170 void RemoveDrmBridge(int media_keys_id);
173 void GenerateKeyIfAllowed(int media_keys_id,
174 const std::string& type,
175 const std::vector<uint8>& init_data,
178 // Constructs a MediaPlayerAndroid object. Declared static to permit embedders
179 // to override functionality.
181 // Objects must call |manager->RequestMediaResources()| before decoding
182 // and |manager->ReleaseMediaSources()| after finishing. This allows the
183 // manager to track decoding resources across the process and free them as
185 static media::MediaPlayerAndroid* CreateMediaPlayer(
186 MediaPlayerHostMsg_Initialize_Type type,
189 const GURL& first_party_for_cookies,
190 int demuxer_client_id,
192 media::MediaPlayerManager* manager,
193 BrowserDemuxerAndroid* demuxer);
195 // An array of managed players.
196 ScopedVector<media::MediaPlayerAndroid> players_;
198 // An array of managed media DRM bridges.
199 ScopedVector<media::MediaDrmBridge> drm_bridges_;
201 // a set of media keys IDs that are pending approval or approved to access
202 // device DRM credentials.
203 // These 2 sets does not cover all the EME videos. If a video only streams
204 // clear data, it will not be included in either set.
205 std::set<int> media_keys_ids_pending_approval_;
206 std::set<int> media_keys_ids_approved_;
208 // The fullscreen video view object or NULL if video is not played in
210 scoped_ptr<ContentVideoView> video_view_;
212 // Player ID of the fullscreen media player.
213 int fullscreen_player_id_;
215 // The player ID pending to enter fullscreen.
216 int pending_fullscreen_player_id_;
218 WebContents* web_contents_;
220 // Object for retrieving resources media players.
221 scoped_ptr<media::MediaResourceGetter> media_resource_getter_;
223 base::WeakPtrFactory<BrowserMediaPlayerManager> weak_ptr_factory_;
225 DISALLOW_COPY_AND_ASSIGN(BrowserMediaPlayerManager);
228 } // namespace content
230 #endif // CONTENT_BROWSER_MEDIA_ANDROID_BROWSER_MEDIA_PLAYER_MANAGER_H_