4c533885bf61234bc0ae90e6549fc256e96d8c1e
[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 "base/basictypes.h"
9 #include "base/callback.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/scoped_vector.h"
12 #include "base/time/time.h"
13 #include "content/browser/android/content_video_view.h"
14 #include "content/common/content_export.h"
15 #include "content/common/media/media_player_messages_enums_android.h"
16 #include "ipc/ipc_message.h"
17 #include "media/base/android/media_player_android.h"
18 #include "media/base/android/media_player_manager.h"
19 #include "ui/gfx/rect_f.h"
20 #include "url/gurl.h"
21
22 namespace media {
23 class DemuxerAndroid;
24 }
25
26 struct MediaPlayerHostMsg_Initialize_Params;
27
28 namespace content {
29 class BrowserDemuxerAndroid;
30 class ContentViewCoreImpl;
31 class ExternalVideoSurfaceContainer;
32 class RenderFrameHost;
33 class WebContents;
34
35 // This class manages all the MediaPlayerAndroid objects.
36 // It receives 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 render
39 // process.
40 class CONTENT_EXPORT BrowserMediaPlayerManager
41     : public media::MediaPlayerManager {
42  public:
43   // Permits embedders to provide an extended version of the class.
44   typedef BrowserMediaPlayerManager* (*Factory)(RenderFrameHost*);
45   static void RegisterFactory(Factory factory);
46
47   // Returns a new instance using the registered factory if available.
48   static BrowserMediaPlayerManager* Create(RenderFrameHost* rfh);
49
50   ContentViewCoreImpl* GetContentViewCore() const;
51
52   virtual ~BrowserMediaPlayerManager();
53
54   // Fullscreen video playback controls.
55   virtual void FullscreenPlayerPlay();
56   virtual void FullscreenPlayerPause();
57   virtual void FullscreenPlayerSeek(int msec);
58   virtual void ExitFullscreen(bool release_media_player);
59   virtual void SetVideoSurface(gfx::ScopedJavaSurface surface);
60
61   // Called when browser player wants the renderer media element to seek.
62   // Any actual seek started by renderer will be handled by browser in OnSeek().
63   void OnSeekRequest(int player_id, const base::TimeDelta& time_to_seek);
64
65   // Pauses all video players manages by this class.
66   void PauseVideo();
67
68   // media::MediaPlayerManager overrides.
69   virtual void OnTimeUpdate(
70       int player_id, base::TimeDelta current_time) OVERRIDE;
71   virtual void OnMediaMetadataChanged(
72       int player_id,
73       base::TimeDelta duration,
74       int width,
75       int height,
76       bool success) OVERRIDE;
77   virtual void OnPlaybackComplete(int player_id) OVERRIDE;
78   virtual void OnMediaInterrupted(int player_id) OVERRIDE;
79   virtual void OnBufferingUpdate(int player_id, int percentage) OVERRIDE;
80   virtual void OnSeekComplete(
81       int player_id,
82       const base::TimeDelta& current_time) OVERRIDE;
83   virtual void OnError(int player_id, int error) OVERRIDE;
84   virtual void OnVideoSizeChanged(
85       int player_id, int width, int height) OVERRIDE;
86   virtual media::MediaResourceGetter* GetMediaResourceGetter() OVERRIDE;
87   virtual media::MediaPlayerAndroid* GetFullscreenPlayer() OVERRIDE;
88   virtual media::MediaPlayerAndroid* GetPlayer(int player_id) OVERRIDE;
89   virtual void RequestFullScreen(int player_id) OVERRIDE;
90 #if defined(VIDEO_HOLE)
91   virtual bool ShouldUseVideoOverlayForEmbeddedEncryptedVideo() OVERRIDE;
92
93   void AttachExternalVideoSurface(int player_id, jobject surface);
94   void DetachExternalVideoSurface(int player_id);
95   void OnFrameInfoUpdated();
96 #endif  // defined(VIDEO_HOLE)
97
98   // Message handlers.
99   virtual void OnEnterFullscreen(int player_id);
100   virtual void OnExitFullscreen(int player_id);
101   virtual void OnInitialize(
102       const MediaPlayerHostMsg_Initialize_Params& media_player_params);
103   virtual void OnStart(int player_id);
104   virtual void OnSeek(int player_id, const base::TimeDelta& time);
105   virtual void OnPause(int player_id, bool is_media_related_action);
106   virtual void OnSetVolume(int player_id, double volume);
107   virtual void OnSetPoster(int player_id, const GURL& poster);
108   virtual void OnReleaseResources(int player_id);
109   virtual void OnDestroyPlayer(int player_id);
110   virtual void ReleaseFullscreenPlayer(media::MediaPlayerAndroid* player);
111 #if defined(VIDEO_HOLE)
112   void OnNotifyExternalSurface(
113       int player_id, bool is_request, const gfx::RectF& rect);
114 #endif  // defined(VIDEO_HOLE)
115
116  protected:
117   // Clients must use Create() or subclass constructor.
118   explicit BrowserMediaPlayerManager(RenderFrameHost* render_frame_host);
119
120   WebContents* web_contents() const { return web_contents_; }
121
122   // Adds a given player to the list.
123   void AddPlayer(media::MediaPlayerAndroid* player);
124
125   // Removes the player with the specified id.
126   void RemovePlayer(int player_id);
127
128   // Replaces a player with the specified id with a given MediaPlayerAndroid
129   // object. This will also return the original MediaPlayerAndroid object that
130   // was replaced.
131   scoped_ptr<media::MediaPlayerAndroid> SwapPlayer(
132       int player_id,
133       media::MediaPlayerAndroid* player);
134
135   int RoutingID();
136
137   // Helper function to send messages to RenderFrameObserver.
138   bool Send(IPC::Message* msg);
139
140  private:
141   // Constructs a MediaPlayerAndroid object.
142   media::MediaPlayerAndroid* CreateMediaPlayer(
143       const MediaPlayerHostMsg_Initialize_Params& media_player_params,
144       bool hide_url_log,
145       media::MediaPlayerManager* manager,
146       BrowserDemuxerAndroid* demuxer);
147
148   // MediaPlayerAndroid must call this before it is going to decode
149   // media streams. This helps the manager object maintain an array
150   // of active MediaPlayerAndroid objects and release the resources
151   // when needed. Currently we only count video resources as they are
152   // constrained by hardware and memory limits.
153   virtual void OnMediaResourcesRequested(int player_id);
154
155   // Similar to the above call, MediaPlayerAndroid must call this method when
156   // releasing all the decoding resources.
157   virtual void OnMediaResourcesReleased(int player_id);
158
159 #if defined(VIDEO_HOLE)
160   void OnRequestExternalSurface(int player_id, const gfx::RectF& rect);
161 #endif  // defined(VIDEO_HOLE)
162
163   RenderFrameHost* const render_frame_host_;
164
165   // An array of managed players.
166   ScopedVector<media::MediaPlayerAndroid> players_;
167
168   // The fullscreen video view object or NULL if video is not played in
169   // fullscreen.
170   scoped_ptr<ContentVideoView> video_view_;
171
172 #if defined(VIDEO_HOLE)
173   scoped_ptr<ExternalVideoSurfaceContainer> external_video_surface_container_;
174 #endif
175
176   // Player ID of the fullscreen media player.
177   int fullscreen_player_id_;
178
179   // Whether the fullscreen player has been Release()-d.
180   bool fullscreen_player_is_released_;
181
182   WebContents* const web_contents_;
183
184   // Object for retrieving resources media players.
185   scoped_ptr<media::MediaResourceGetter> media_resource_getter_;
186
187   // NOTE: Weak pointers must be invalidated before all other member variables.
188   base::WeakPtrFactory<BrowserMediaPlayerManager> weak_ptr_factory_;
189
190   DISALLOW_COPY_AND_ASSIGN(BrowserMediaPlayerManager);
191 };
192
193 }  // namespace content
194
195 #endif  // CONTENT_BROWSER_MEDIA_ANDROID_BROWSER_MEDIA_PLAYER_MANAGER_H_