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 "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"
26 struct MediaPlayerHostMsg_Initialize_Params;
29 class BrowserDemuxerAndroid;
30 class ContentViewCoreImpl;
31 class ExternalVideoSurfaceContainer;
32 class RenderFrameHost;
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
40 class CONTENT_EXPORT BrowserMediaPlayerManager
41 : public media::MediaPlayerManager {
43 // Permits embedders to provide an extended version of the class.
44 typedef BrowserMediaPlayerManager* (*Factory)(RenderFrameHost*);
45 static void RegisterFactory(Factory factory);
47 // Returns a new instance using the registered factory if available.
48 static BrowserMediaPlayerManager* Create(RenderFrameHost* rfh);
50 ContentViewCoreImpl* GetContentViewCore() const;
52 virtual ~BrowserMediaPlayerManager();
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);
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);
65 // Pauses all video players manages by this class.
68 // media::MediaPlayerManager overrides.
69 virtual void OnTimeUpdate(
70 int player_id, base::TimeDelta current_time) OVERRIDE;
71 virtual void OnMediaMetadataChanged(
73 base::TimeDelta duration,
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(
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;
93 void AttachExternalVideoSurface(int player_id, jobject surface);
94 void DetachExternalVideoSurface(int player_id);
95 void OnFrameInfoUpdated();
96 #endif // defined(VIDEO_HOLE)
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)
117 // Clients must use Create() or subclass constructor.
118 explicit BrowserMediaPlayerManager(RenderFrameHost* render_frame_host);
120 WebContents* web_contents() const { return web_contents_; }
122 // Adds a given player to the list.
123 void AddPlayer(media::MediaPlayerAndroid* player);
125 // Removes the player with the specified id.
126 void RemovePlayer(int player_id);
128 // Replaces a player with the specified id with a given MediaPlayerAndroid
129 // object. This will also return the original MediaPlayerAndroid object that
131 scoped_ptr<media::MediaPlayerAndroid> SwapPlayer(
133 media::MediaPlayerAndroid* player);
137 // Helper function to send messages to RenderFrameObserver.
138 bool Send(IPC::Message* msg);
141 // Constructs a MediaPlayerAndroid object.
142 media::MediaPlayerAndroid* CreateMediaPlayer(
143 const MediaPlayerHostMsg_Initialize_Params& media_player_params,
145 media::MediaPlayerManager* manager,
146 BrowserDemuxerAndroid* demuxer);
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);
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);
159 #if defined(VIDEO_HOLE)
160 void OnRequestExternalSurface(int player_id, const gfx::RectF& rect);
161 #endif // defined(VIDEO_HOLE)
163 RenderFrameHost* const render_frame_host_;
165 // An array of managed players.
166 ScopedVector<media::MediaPlayerAndroid> players_;
168 // The fullscreen video view object or NULL if video is not played in
170 scoped_ptr<ContentVideoView> video_view_;
172 #if defined(VIDEO_HOLE)
173 scoped_ptr<ExternalVideoSurfaceContainer> external_video_surface_container_;
176 // Player ID of the fullscreen media player.
177 int fullscreen_player_id_;
179 // Whether the fullscreen player has been Release()-d.
180 bool fullscreen_player_is_released_;
182 WebContents* const web_contents_;
184 // Object for retrieving resources media players.
185 scoped_ptr<media::MediaResourceGetter> media_resource_getter_;
187 // NOTE: Weak pointers must be invalidated before all other member variables.
188 base::WeakPtrFactory<BrowserMediaPlayerManager> weak_ptr_factory_;
190 DISALLOW_COPY_AND_ASSIGN(BrowserMediaPlayerManager);
193 } // namespace content
195 #endif // CONTENT_BROWSER_MEDIA_ANDROID_BROWSER_MEDIA_PLAYER_MANAGER_H_