IndexedDBFactory now ForceCloses databases.
[chromium-blink-merge.git] / content / browser / media / android / browser_media_player_manager.h
blobb8e24ae14545f4c261312c286703fd3702836e1c
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_
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
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"
24 #include "url/gurl.h"
26 namespace media {
27 class DemuxerAndroid;
28 class MediaDrmBridge;
31 namespace content {
32 class BrowserDemuxerAndroid;
33 class ContentViewCoreImpl;
34 class WebContents;
36 // This class manages all the MediaPlayerAndroid objects. It receives
37 // control operations from the the render process, and forwards
38 // them to corresponding MediaPlayerAndroid object. Callbacks from
39 // MediaPlayerAndroid objects are converted to IPCs and then sent to the
40 // render process.
41 class CONTENT_EXPORT BrowserMediaPlayerManager
42 : public WebContentsObserver,
43 public media::MediaPlayerManager {
44 public:
45 // Permits embedders to provide an extended version of the class.
46 typedef BrowserMediaPlayerManager* (*Factory)(RenderViewHost*);
47 static void RegisterFactory(Factory factory);
49 // Returns a new instance using the registered factory if available.
50 static BrowserMediaPlayerManager* Create(RenderViewHost* rvh);
52 ContentViewCoreImpl* GetContentViewCore() const;
54 virtual ~BrowserMediaPlayerManager();
56 // WebContentsObserver overrides.
57 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
59 // Fullscreen video playback controls.
60 virtual void FullscreenPlayerPlay();
61 virtual void FullscreenPlayerPause();
62 virtual void FullscreenPlayerSeek(int msec);
63 virtual void ExitFullscreen(bool release_media_player);
64 virtual void SetVideoSurface(gfx::ScopedJavaSurface surface);
65 virtual void SuspendFullscreen();
66 virtual void ResumeFullscreen(gfx::ScopedJavaSurface surface);
68 // Called when browser player wants the renderer media element to seek.
69 // Any actual seek started by renderer will be handled by browser in OnSeek().
70 void OnSeekRequest(int player_id, const base::TimeDelta& time_to_seek);
72 // media::MediaPlayerManager overrides.
73 virtual void OnTimeUpdate(
74 int player_id, base::TimeDelta current_time) OVERRIDE;
75 virtual void OnMediaMetadataChanged(
76 int player_id,
77 base::TimeDelta duration,
78 int width,
79 int height,
80 bool success) OVERRIDE;
81 virtual void OnPlaybackComplete(int player_id) OVERRIDE;
82 virtual void OnMediaInterrupted(int player_id) OVERRIDE;
83 virtual void OnBufferingUpdate(int player_id, int percentage) OVERRIDE;
84 virtual void OnSeekComplete(
85 int player_id,
86 const base::TimeDelta& current_time) OVERRIDE;
87 virtual void OnError(int player_id, int error) OVERRIDE;
88 virtual void OnVideoSizeChanged(
89 int player_id, int width, int height) OVERRIDE;
90 virtual void RequestMediaResources(int player_id) OVERRIDE;
91 virtual void ReleaseMediaResources(int player_id) OVERRIDE;
92 virtual media::MediaResourceGetter* GetMediaResourceGetter() OVERRIDE;
93 virtual media::MediaPlayerAndroid* GetFullscreenPlayer() OVERRIDE;
94 virtual media::MediaPlayerAndroid* GetPlayer(int player_id) OVERRIDE;
95 virtual media::MediaDrmBridge* GetDrmBridge(int media_keys_id) OVERRIDE;
96 virtual void DestroyAllMediaPlayers() OVERRIDE;
97 virtual void OnProtectedSurfaceRequested(int player_id) OVERRIDE;
98 virtual void OnSessionCreated(int media_keys_id,
99 uint32 session_id,
100 const std::string& web_session_id) OVERRIDE;
101 virtual void OnSessionMessage(int media_keys_id,
102 uint32 session_id,
103 const std::vector<uint8>& message,
104 const std::string& destination_url) OVERRIDE;
105 virtual void OnSessionReady(int media_keys_id, uint32 session_id) OVERRIDE;
106 virtual void OnSessionClosed(int media_keys_id, uint32 session_id) OVERRIDE;
107 virtual void OnSessionError(int media_keys_id,
108 uint32 session_id,
109 media::MediaKeys::KeyError error_code,
110 int system_code) OVERRIDE;
112 #if defined(VIDEO_HOLE)
113 void AttachExternalVideoSurface(int player_id, jobject surface);
114 void DetachExternalVideoSurface(int player_id);
115 #endif // defined(VIDEO_HOLE)
117 // Called to disble the current fullscreen playback if the video is encrypted.
118 // TODO(qinmin): remove this once we have the new fullscreen mode.
119 void DisableFullscreenEncryptedMediaPlayback();
121 protected:
122 // Clients must use Create() or subclass constructor.
123 explicit BrowserMediaPlayerManager(RenderViewHost* render_view_host);
125 // Message handlers.
126 virtual void OnEnterFullscreen(int player_id);
127 virtual void OnExitFullscreen(int player_id);
128 virtual void OnInitialize(
129 MediaPlayerHostMsg_Initialize_Type type,
130 int player_id,
131 const GURL& url,
132 const GURL& first_party_for_cookies,
133 int demuxer_client_id);
134 virtual void OnStart(int player_id);
135 virtual void OnSeek(int player_id, const base::TimeDelta& time);
136 virtual void OnPause(int player_id, bool is_media_related_action);
137 virtual void OnSetVolume(int player_id, double volume);
138 virtual void OnReleaseResources(int player_id);
139 virtual void OnDestroyPlayer(int player_id);
140 void OnInitializeCDM(int media_keys_id,
141 const std::vector<uint8>& uuid,
142 const GURL& frame_url);
143 void OnCreateSession(int media_keys_id,
144 uint32 session_id,
145 const std::string& type,
146 const std::vector<uint8>& init_data);
147 void OnUpdateSession(int media_keys_id,
148 uint32 session_id,
149 const std::vector<uint8>& response);
150 void OnReleaseSession(int media_keys_id, uint32 session_id);
151 void OnSetMediaKeys(int player_id, int media_keys_id);
153 void OnCancelAllPendingSessionCreations(int media_keys_id);
155 #if defined(VIDEO_HOLE)
156 virtual void OnNotifyExternalSurface(
157 int player_id, bool is_request, const gfx::RectF& rect);
158 #endif // defined(VIDEO_HOLE)
160 // Adds a given player to the list.
161 void AddPlayer(media::MediaPlayerAndroid* player);
163 // Removes the player with the specified id.
164 void RemovePlayer(int player_id);
166 // Replaces a player with the specified id with a given MediaPlayerAndroid
167 // object. This will also return the original MediaPlayerAndroid object that
168 // was replaced.
169 scoped_ptr<media::MediaPlayerAndroid> SwapPlayer(
170 int player_id,
171 media::MediaPlayerAndroid* player);
173 // Adds a new MediaDrmBridge for the given |uuid|, |media_keys_id|, and
174 // |frame_url|.
175 void AddDrmBridge(int media_keys_id,
176 const std::vector<uint8>& uuid,
177 const GURL& frame_url);
179 // Removes the DRM bridge with the specified id.
180 void RemoveDrmBridge(int media_keys_id);
182 private:
183 // If |permitted| is false, it does nothing but send
184 // |MediaKeysMsg_SessionError| IPC message.
185 // The primary use case is infobar permission callback, i.e., when infobar
186 // can decide user's intention either from interacting with the actual info
187 // bar or from the saved preference.
188 void CreateSessionIfPermitted(int media_keys_id,
189 uint32 session_id,
190 const std::string& type,
191 const std::vector<uint8>& init_data,
192 bool permitted);
194 // Constructs a MediaPlayerAndroid object. Declared static to permit embedders
195 // to override functionality.
197 // Objects must call |manager->RequestMediaResources()| before decoding
198 // and |manager->ReleaseMediaSources()| after finishing. This allows the
199 // manager to track decoding resources across the process and free them as
200 // needed.
201 static media::MediaPlayerAndroid* CreateMediaPlayer(
202 MediaPlayerHostMsg_Initialize_Type type,
203 int player_id,
204 const GURL& url,
205 const GURL& first_party_for_cookies,
206 int demuxer_client_id,
207 bool hide_url_log,
208 media::MediaPlayerManager* manager,
209 BrowserDemuxerAndroid* demuxer);
211 // An array of managed players.
212 ScopedVector<media::MediaPlayerAndroid> players_;
214 // An array of managed media DRM bridges.
215 ScopedVector<media::MediaDrmBridge> drm_bridges_;
217 // a set of media keys IDs that are pending approval or approved to access
218 // device DRM credentials.
219 // These 2 sets does not cover all the EME videos. If a video only streams
220 // clear data, it will not be included in either set.
221 std::set<int> media_keys_ids_pending_approval_;
222 std::set<int> media_keys_ids_approved_;
224 // The fullscreen video view object or NULL if video is not played in
225 // fullscreen.
226 scoped_ptr<ContentVideoView> video_view_;
228 // Player ID of the fullscreen media player.
229 int fullscreen_player_id_;
231 // The player ID pending to enter fullscreen.
232 int pending_fullscreen_player_id_;
234 WebContents* web_contents_;
236 // Object for retrieving resources media players.
237 scoped_ptr<media::MediaResourceGetter> media_resource_getter_;
239 base::WeakPtrFactory<BrowserMediaPlayerManager> weak_ptr_factory_;
241 DISALLOW_COPY_AND_ASSIGN(BrowserMediaPlayerManager);
244 } // namespace content
246 #endif // CONTENT_BROWSER_MEDIA_ANDROID_BROWSER_MEDIA_PLAYER_MANAGER_H_