• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 OnSessionCreated(int media_keys_id,
94                                 uint32 session_id,
95                                 const std::string& web_session_id) OVERRIDE;
96   virtual void OnSessionMessage(int media_keys_id,
97                                 uint32 session_id,
98                                 const std::vector<uint8>& message,
99                                 const std::string& destination_url) OVERRIDE;
100   virtual void OnSessionReady(int media_keys_id, uint32 session_id) OVERRIDE;
101   virtual void OnSessionClosed(int media_keys_id, uint32 session_id) OVERRIDE;
102   virtual void OnSessionError(int media_keys_id,
103                               uint32 session_id,
104                               media::MediaKeys::KeyError error_code,
105                               int system_code) OVERRIDE;
106 
107 #if defined(VIDEO_HOLE)
108   void AttachExternalVideoSurface(int player_id, jobject surface);
109   void DetachExternalVideoSurface(int player_id);
110 #endif  // defined(VIDEO_HOLE)
111 
112   // Called to disble the current fullscreen playback if the video is encrypted.
113   // TODO(qinmin): remove this once we have the new fullscreen mode.
114   void DisableFullscreenEncryptedMediaPlayback();
115 
116  protected:
117   // Clients must use Create() or subclass constructor.
118   explicit BrowserMediaPlayerManager(RenderViewHost* render_view_host);
119 
120   // Message handlers.
121   virtual void OnEnterFullscreen(int player_id);
122   virtual void OnExitFullscreen(int player_id);
123   virtual void OnInitialize(
124       MediaPlayerHostMsg_Initialize_Type type,
125       int player_id,
126       const GURL& url,
127       const GURL& first_party_for_cookies,
128       int demuxer_client_id);
129   virtual void OnStart(int player_id);
130   virtual void OnSeek(int player_id, const base::TimeDelta& time);
131   virtual void OnPause(int player_id, bool is_media_related_action);
132   virtual void OnSetVolume(int player_id, double volume);
133   virtual void OnReleaseResources(int player_id);
134   virtual void OnDestroyPlayer(int player_id);
135   void OnInitializeCDM(int media_keys_id,
136                        const std::vector<uint8>& uuid,
137                        const GURL& frame_url);
138   void OnCreateSession(int media_keys_id,
139                        uint32 session_id,
140                        const std::string& type,
141                        const std::vector<uint8>& init_data);
142   void OnUpdateSession(int media_keys_id,
143                        uint32 session_id,
144                        const std::vector<uint8>& response);
145   void OnReleaseSession(int media_keys_id, uint32 session_id);
146   void OnSetMediaKeys(int player_id, int media_keys_id);
147 
148 #if defined(VIDEO_HOLE)
149   virtual void OnNotifyExternalSurface(
150       int player_id, bool is_request, const gfx::RectF& rect);
151 #endif  // defined(VIDEO_HOLE)
152 
153   // Adds a given player to the list.
154   void AddPlayer(media::MediaPlayerAndroid* player);
155 
156   // Removes the player with the specified id.
157   void RemovePlayer(int player_id);
158 
159   // Replaces a player with the specified id with a given MediaPlayerAndroid
160   // object. This will also return the original MediaPlayerAndroid object that
161   // was replaced.
162   scoped_ptr<media::MediaPlayerAndroid> SwapPlayer(
163       int player_id,
164       media::MediaPlayerAndroid* player);
165 
166   // Adds a new MediaDrmBridge for the given |uuid|, |media_keys_id|, and
167   // |frame_url|.
168   void AddDrmBridge(int media_keys_id,
169                     const std::vector<uint8>& uuid,
170                     const GURL& frame_url);
171 
172   // Removes the DRM bridge with the specified id.
173   void RemoveDrmBridge(int media_keys_id);
174 
175  private:
176   void GenerateKeyIfAllowed(int media_keys_id,
177                             uint32 session_id,
178                             const std::string& type,
179                             const std::vector<uint8>& init_data,
180                             bool allowed);
181 
182   // Constructs a MediaPlayerAndroid object. Declared static to permit embedders
183   // to override functionality.
184   //
185   // Objects must call |manager->RequestMediaResources()| before decoding
186   // and |manager->ReleaseMediaSources()| after finishing. This allows the
187   // manager to track decoding resources across the process and free them as
188   // needed.
189   static media::MediaPlayerAndroid* CreateMediaPlayer(
190       MediaPlayerHostMsg_Initialize_Type type,
191       int player_id,
192       const GURL& url,
193       const GURL& first_party_for_cookies,
194       int demuxer_client_id,
195       bool hide_url_log,
196       media::MediaPlayerManager* manager,
197       BrowserDemuxerAndroid* demuxer);
198 
199   // An array of managed players.
200   ScopedVector<media::MediaPlayerAndroid> players_;
201 
202   // An array of managed media DRM bridges.
203   ScopedVector<media::MediaDrmBridge> drm_bridges_;
204 
205   // a set of media keys IDs that are pending approval or approved to access
206   // device DRM credentials.
207   // These 2 sets does not cover all the EME videos. If a video only streams
208   // clear data, it will not be included in either set.
209   std::set<int> media_keys_ids_pending_approval_;
210   std::set<int> media_keys_ids_approved_;
211 
212   // The fullscreen video view object or NULL if video is not played in
213   // fullscreen.
214   scoped_ptr<ContentVideoView> video_view_;
215 
216   // Player ID of the fullscreen media player.
217   int fullscreen_player_id_;
218 
219   // The player ID pending to enter fullscreen.
220   int pending_fullscreen_player_id_;
221 
222   // Whether the fullscreen player has been Release()-d.
223   bool fullscreen_player_is_released_;
224 
225   WebContents* web_contents_;
226 
227   // Object for retrieving resources media players.
228   scoped_ptr<media::MediaResourceGetter> media_resource_getter_;
229 
230   base::WeakPtrFactory<BrowserMediaPlayerManager> weak_ptr_factory_;
231 
232   DISALLOW_COPY_AND_ASSIGN(BrowserMediaPlayerManager);
233 };
234 
235 }  // namespace content
236 
237 #endif  // CONTENT_BROWSER_MEDIA_ANDROID_BROWSER_MEDIA_PLAYER_MANAGER_H_
238