Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / browser / media / android / browser_media_player_manager.h
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.
4
5 #ifndef CONTENT_BROWSER_MEDIA_ANDROID_BROWSER_MEDIA_PLAYER_MANAGER_H_
6 #define CONTENT_BROWSER_MEDIA_ANDROID_BROWSER_MEDIA_PLAYER_MANAGER_H_
7
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
12
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"
25
26 namespace media {
27 class DemuxerAndroid;
28 class MediaDrmBridge;
29 }
30
31 namespace content {
32 class BrowserDemuxerAndroid;
33 class ContentViewCoreImpl;
34 class WebContents;
35
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);
48
49   // Returns a new instance using the registered factory if available.
50   static BrowserMediaPlayerManager* Create(RenderViewHost* rvh);
51
52   ContentViewCoreImpl* GetContentViewCore() const;
53
54   virtual ~BrowserMediaPlayerManager();
55
56   // WebContentsObserver overrides.
57   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
58
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
66   // Called when browser player wants the renderer media element to seek.
67   // Any actual seek started by renderer will be handled by browser in OnSeek().
68   void OnSeekRequest(int player_id, const base::TimeDelta& time_to_seek);
69
70   // media::MediaPlayerManager overrides.
71   virtual void OnTimeUpdate(
72       int player_id, base::TimeDelta current_time) OVERRIDE;
73   virtual void OnMediaMetadataChanged(
74       int player_id,
75       base::TimeDelta duration,
76       int width,
77       int height,
78       bool success) OVERRIDE;
79   virtual void OnPlaybackComplete(int player_id) OVERRIDE;
80   virtual void OnMediaInterrupted(int player_id) OVERRIDE;
81   virtual void OnBufferingUpdate(int player_id, int percentage) OVERRIDE;
82   virtual void OnSeekComplete(
83       int player_id,
84       const base::TimeDelta& current_time) OVERRIDE;
85   virtual void OnError(int player_id, int error) OVERRIDE;
86   virtual void OnVideoSizeChanged(
87       int player_id, int width, int height) OVERRIDE;
88   virtual void RequestMediaResources(int player_id) OVERRIDE;
89   virtual void ReleaseMediaResources(int player_id) OVERRIDE;
90   virtual media::MediaResourceGetter* GetMediaResourceGetter() OVERRIDE;
91   virtual media::MediaPlayerAndroid* GetFullscreenPlayer() OVERRIDE;
92   virtual media::MediaPlayerAndroid* GetPlayer(int player_id) OVERRIDE;
93   virtual media::MediaDrmBridge* GetDrmBridge(int media_keys_id) OVERRIDE;
94   virtual void DestroyAllMediaPlayers() OVERRIDE;
95   virtual void OnProtectedSurfaceRequested(int player_id) OVERRIDE;
96   virtual void OnSessionCreated(int media_keys_id,
97                                 uint32 session_id,
98                                 const std::string& web_session_id) OVERRIDE;
99   virtual void OnSessionMessage(int media_keys_id,
100                                 uint32 session_id,
101                                 const std::vector<uint8>& message,
102                                 const GURL& destination_url) OVERRIDE;
103   virtual void OnSessionReady(int media_keys_id, uint32 session_id) OVERRIDE;
104   virtual void OnSessionClosed(int media_keys_id, uint32 session_id) OVERRIDE;
105   virtual void OnSessionError(int media_keys_id,
106                               uint32 session_id,
107                               media::MediaKeys::KeyError error_code,
108                               int system_code) OVERRIDE;
109
110 #if defined(VIDEO_HOLE)
111   void AttachExternalVideoSurface(int player_id, jobject surface);
112   void DetachExternalVideoSurface(int player_id);
113 #endif  // defined(VIDEO_HOLE)
114
115   // Called to disble the current fullscreen playback if the video is encrypted.
116   // TODO(qinmin): remove this once we have the new fullscreen mode.
117   void DisableFullscreenEncryptedMediaPlayback();
118
119  protected:
120   // Clients must use Create() or subclass constructor.
121   explicit BrowserMediaPlayerManager(RenderViewHost* render_view_host);
122
123   // Message handlers.
124   virtual void OnEnterFullscreen(int player_id);
125   virtual void OnExitFullscreen(int player_id);
126   virtual void OnInitialize(
127       MediaPlayerHostMsg_Initialize_Type type,
128       int player_id,
129       const GURL& url,
130       const GURL& first_party_for_cookies,
131       int demuxer_client_id);
132   virtual void OnStart(int player_id);
133   virtual void OnSeek(int player_id, const base::TimeDelta& time);
134   virtual void OnPause(int player_id, bool is_media_related_action);
135   virtual void OnSetVolume(int player_id, double volume);
136   virtual void OnSetPoster(int player_id, const GURL& poster);
137   virtual void OnReleaseResources(int player_id);
138   virtual void OnDestroyPlayer(int player_id);
139   virtual void ReleaseFullscreenPlayer(media::MediaPlayerAndroid* player);
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                        MediaKeysHostMsg_CreateSession_Type content_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);
152   void OnDestroyCdm(int media_keys_id);
153
154   // Cancels all pending session creations associated with |media_keys_id|.
155   void CancelAllPendingSessionCreations(int media_keys_id);
156
157 #if defined(VIDEO_HOLE)
158   virtual void OnNotifyExternalSurface(
159       int player_id, bool is_request, const gfx::RectF& rect);
160 #endif  // defined(VIDEO_HOLE)
161
162   // Adds a given player to the list.
163   void AddPlayer(media::MediaPlayerAndroid* player);
164
165   // Removes the player with the specified id.
166   void RemovePlayer(int player_id);
167
168   // Replaces a player with the specified id with a given MediaPlayerAndroid
169   // object. This will also return the original MediaPlayerAndroid object that
170   // was replaced.
171   scoped_ptr<media::MediaPlayerAndroid> SwapPlayer(
172       int player_id,
173       media::MediaPlayerAndroid* player);
174
175   // Adds a new MediaDrmBridge for the given |uuid|, |media_keys_id|, and
176   // |frame_url|.
177   void AddDrmBridge(int media_keys_id,
178                     const std::vector<uint8>& uuid,
179                     const GURL& frame_url);
180
181   // Removes the DRM bridge with the specified id.
182   void RemoveDrmBridge(int media_keys_id);
183
184  private:
185   // If |permitted| is false, it does nothing but send
186   // |MediaKeysMsg_SessionError| IPC message.
187   // The primary use case is infobar permission callback, i.e., when infobar
188   // can decide user's intention either from interacting with the actual info
189   // bar or from the saved preference.
190   void CreateSessionIfPermitted(int media_keys_id,
191                                 uint32 session_id,
192                                 const std::string& content_type,
193                                 const std::vector<uint8>& init_data,
194                                 bool permitted);
195
196   // Constructs a MediaPlayerAndroid object. Declared static to permit embedders
197   // to override functionality.
198   //
199   // Objects must call |manager->RequestMediaResources()| before decoding
200   // and |manager->ReleaseMediaSources()| after finishing. This allows the
201   // manager to track decoding resources across the process and free them as
202   // needed.
203   static media::MediaPlayerAndroid* CreateMediaPlayer(
204       MediaPlayerHostMsg_Initialize_Type type,
205       int player_id,
206       const GURL& url,
207       const GURL& first_party_for_cookies,
208       int demuxer_client_id,
209       bool hide_url_log,
210       media::MediaPlayerManager* manager,
211       BrowserDemuxerAndroid* demuxer);
212
213   // An array of managed players.
214   ScopedVector<media::MediaPlayerAndroid> players_;
215
216   // An array of managed media DRM bridges.
217   ScopedVector<media::MediaDrmBridge> drm_bridges_;
218
219   // a set of media keys IDs that are pending approval or approved to access
220   // device DRM credentials.
221   // These 2 sets does not cover all the EME videos. If a video only streams
222   // clear data, it will not be included in either set.
223   std::set<int> media_keys_ids_pending_approval_;
224   std::set<int> media_keys_ids_approved_;
225
226   // The fullscreen video view object or NULL if video is not played in
227   // fullscreen.
228   scoped_ptr<ContentVideoView> video_view_;
229
230   // Player ID of the fullscreen media player.
231   int fullscreen_player_id_;
232
233   // The player ID pending to enter fullscreen.
234   int pending_fullscreen_player_id_;
235
236   // Whether the fullscreen player has been Release()-d.
237   bool fullscreen_player_is_released_;
238
239   WebContents* web_contents_;
240
241   // Object for retrieving resources media players.
242   scoped_ptr<media::MediaResourceGetter> media_resource_getter_;
243
244   base::WeakPtrFactory<BrowserMediaPlayerManager> weak_ptr_factory_;
245
246   DISALLOW_COPY_AND_ASSIGN(BrowserMediaPlayerManager);
247 };
248
249 }  // namespace content
250
251 #endif  // CONTENT_BROWSER_MEDIA_ANDROID_BROWSER_MEDIA_PLAYER_MANAGER_H_