- add sources.
[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 WebContents;
34
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
39 // render process.
40 class CONTENT_EXPORT BrowserMediaPlayerManager
41     : public WebContentsObserver,
42       public media::MediaPlayerManager {
43  public:
44   // Permits embedders to provide an extended version of the class.
45   typedef BrowserMediaPlayerManager* (*Factory)(RenderViewHost*);
46   static void RegisterFactory(Factory factory);
47
48   // Returns a new instance using the registered factory if available.
49   static BrowserMediaPlayerManager* Create(RenderViewHost* rvh);
50
51   virtual ~BrowserMediaPlayerManager();
52
53   // WebContentsObserver overrides.
54   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
55
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);
62
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);
66
67   // media::MediaPlayerManager overrides.
68   virtual void OnTimeUpdate(
69       int player_id, base::TimeDelta current_time) OVERRIDE;
70   virtual void OnMediaMetadataChanged(
71       int player_id,
72       base::TimeDelta duration,
73       int width,
74       int height,
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(
80       int player_id,
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;
103
104 #if defined(GOOGLE_TV)
105   void AttachExternalVideoSurface(int player_id, jobject surface);
106   void DetachExternalVideoSurface(int player_id);
107 #endif
108
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();
112
113  protected:
114   // Clients must use Create() or subclass constructor.
115   explicit BrowserMediaPlayerManager(RenderViewHost* render_view_host);
116
117   // Message handlers.
118   virtual void OnEnterFullscreen(int player_id);
119   virtual void OnExitFullscreen(int player_id);
120   virtual void OnInitialize(
121       MediaPlayerHostMsg_Initialize_Type type,
122       int player_id,
123       const GURL& url,
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);
144
145 #if defined(GOOGLE_TV)
146   virtual void OnNotifyExternalSurface(
147       int player_id, bool is_request, const gfx::RectF& rect);
148 #endif
149
150   // Adds a given player to the list.
151   void AddPlayer(media::MediaPlayerAndroid* player);
152
153   // Removes the player with the specified id.
154   void RemovePlayer(int player_id);
155
156   // Replaces a player with the specified id with a given MediaPlayerAndroid
157   // object. This will also return the original MediaPlayerAndroid object that
158   // was replaced.
159   scoped_ptr<media::MediaPlayerAndroid> SwapPlayer(
160       int player_id,
161       media::MediaPlayerAndroid* player);
162
163   // Add a new MediaDrmBridge for the given |uuid|, |media_keys_id|, and
164   // |frame_url|.
165   void AddDrmBridge(int media_keys_id,
166                     const std::vector<uint8>& uuid,
167                     const GURL& frame_url);
168
169   // Removes the DRM bridge with the specified id.
170   void RemoveDrmBridge(int media_keys_id);
171
172  private:
173   void GenerateKeyIfAllowed(int media_keys_id,
174                             const std::string& type,
175                             const std::vector<uint8>& init_data,
176                             bool allowed);
177
178   // Constructs a MediaPlayerAndroid object. Declared static to permit embedders
179   // to override functionality.
180   //
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
184   // needed.
185   static media::MediaPlayerAndroid* CreateMediaPlayer(
186       MediaPlayerHostMsg_Initialize_Type type,
187       int player_id,
188       const GURL& url,
189       const GURL& first_party_for_cookies,
190       int demuxer_client_id,
191       bool hide_url_log,
192       media::MediaPlayerManager* manager,
193       BrowserDemuxerAndroid* demuxer);
194
195   // An array of managed players.
196   ScopedVector<media::MediaPlayerAndroid> players_;
197
198   // An array of managed media DRM bridges.
199   ScopedVector<media::MediaDrmBridge> drm_bridges_;
200
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_;
207
208   // The fullscreen video view object or NULL if video is not played in
209   // fullscreen.
210   scoped_ptr<ContentVideoView> video_view_;
211
212   // Player ID of the fullscreen media player.
213   int fullscreen_player_id_;
214
215   // The player ID pending to enter fullscreen.
216   int pending_fullscreen_player_id_;
217
218   WebContents* web_contents_;
219
220   // Object for retrieving resources media players.
221   scoped_ptr<media::MediaResourceGetter> media_resource_getter_;
222
223   base::WeakPtrFactory<BrowserMediaPlayerManager> weak_ptr_factory_;
224
225   DISALLOW_COPY_AND_ASSIGN(BrowserMediaPlayerManager);
226 };
227
228 }  // namespace content
229
230 #endif  // CONTENT_BROWSER_MEDIA_ANDROID_BROWSER_MEDIA_PLAYER_MANAGER_H_