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/browser/media/android/media_session_observer.h"
15 #include "content/common/content_export.h"
16 #include "content/common/media/media_player_messages_enums_android.h"
17 #include "content/public/browser/android/content_view_core.h"
18 #include "ipc/ipc_message.h"
19 #include "media/base/android/media_player_android.h"
20 #include "media/base/android/media_player_manager.h"
21 #include "media/base/android/media_url_interceptor.h"
22 #include "ui/gfx/geometry/rect_f.h"
29 struct MediaPlayerHostMsg_Initialize_Params
;
32 class BrowserDemuxerAndroid
;
33 class ContentViewCoreImpl
;
34 class ExternalVideoSurfaceContainer
;
35 class RenderFrameHost
;
38 // This class manages all the MediaPlayerAndroid objects.
39 // It receives control operations from the the render process, and forwards
40 // them to corresponding MediaPlayerAndroid object. Callbacks from
41 // MediaPlayerAndroid objects are converted to IPCs and then sent to the render
43 class CONTENT_EXPORT BrowserMediaPlayerManager
44 : public media::MediaPlayerManager
,
45 public MediaSessionObserver
{
47 // Permits embedders to provide an extended version of the class.
48 typedef BrowserMediaPlayerManager
* (*Factory
)(RenderFrameHost
*);
49 static void RegisterFactory(Factory factory
);
51 // Permits embedders to handle custom urls.
52 static void RegisterMediaUrlInterceptor(
53 media::MediaUrlInterceptor
* media_url_interceptor
);
55 // Pass a java surface object to the MediaPlayerAndroid object
56 // identified by render process handle, render frame ID and player ID.
57 static void SetSurfacePeer(scoped_refptr
<gfx::SurfaceTexture
> surface_texture
,
58 base::ProcessHandle render_process_handle
,
62 // Returns a new instance using the registered factory if available.
63 static BrowserMediaPlayerManager
* Create(RenderFrameHost
* rfh
);
65 ContentViewCore
* GetContentViewCore() const;
67 ~BrowserMediaPlayerManager() override
;
69 // Fullscreen video playback controls.
70 virtual void ExitFullscreen(bool release_media_player
);
71 virtual void SetVideoSurface(gfx::ScopedJavaSurface surface
);
73 // Called when browser player wants the renderer media element to seek.
74 // Any actual seek started by renderer will be handled by browser in OnSeek().
75 void OnSeekRequest(int player_id
, const base::TimeDelta
& time_to_seek
);
77 // Stops and releases every media managed by this class.
78 void ReleaseAllMediaPlayers();
80 // media::MediaPlayerManager overrides.
81 void OnTimeUpdate(int player_id
,
82 base::TimeDelta current_timestamp
,
83 base::TimeTicks current_time_ticks
) override
;
84 void OnMediaMetadataChanged(int player_id
,
85 base::TimeDelta duration
,
88 bool success
) override
;
89 void OnPlaybackComplete(int player_id
) override
;
90 void OnMediaInterrupted(int player_id
) override
;
91 void OnBufferingUpdate(int player_id
, int percentage
) override
;
92 void OnSeekComplete(int player_id
,
93 const base::TimeDelta
& current_time
) override
;
94 void OnError(int player_id
, int error
) override
;
95 void OnVideoSizeChanged(int player_id
, int width
, int height
) override
;
96 void OnWaitingForDecryptionKey(int player_id
) override
;
98 media::MediaResourceGetter
* GetMediaResourceGetter() override
;
99 media::MediaUrlInterceptor
* GetMediaUrlInterceptor() override
;
100 media::MediaPlayerAndroid
* GetFullscreenPlayer() override
;
101 media::MediaPlayerAndroid
* GetPlayer(int player_id
) override
;
102 bool RequestPlay(int player_id
) override
;
103 #if defined(VIDEO_HOLE)
104 void AttachExternalVideoSurface(int player_id
, jobject surface
);
105 void DetachExternalVideoSurface(int player_id
);
106 void OnFrameInfoUpdated();
107 #endif // defined(VIDEO_HOLE)
109 // MediaSessionObserver overrides.
110 void OnSuspend(int player_id
) override
;
111 void OnResume(int player_id
) override
;
114 virtual void OnEnterFullscreen(int player_id
);
115 virtual void OnInitialize(
116 const MediaPlayerHostMsg_Initialize_Params
& media_player_params
);
117 virtual void OnStart(int player_id
);
118 virtual void OnSeek(int player_id
, const base::TimeDelta
& time
);
119 virtual void OnPause(int player_id
, bool is_media_related_action
);
120 virtual void OnSetVolume(int player_id
, double volume
);
121 virtual void OnSetPoster(int player_id
, const GURL
& poster
);
122 virtual void OnReleaseResources(int player_id
);
123 virtual void OnDestroyPlayer(int player_id
);
124 virtual void OnRequestRemotePlayback(int player_id
);
125 virtual void OnRequestRemotePlaybackControl(int player_id
);
126 virtual void ReleaseFullscreenPlayer(media::MediaPlayerAndroid
* player
);
127 #if defined(VIDEO_HOLE)
128 void OnNotifyExternalSurface(
129 int player_id
, bool is_request
, const gfx::RectF
& rect
);
130 #endif // defined(VIDEO_HOLE)
133 // Clients must use Create() or subclass constructor.
134 explicit BrowserMediaPlayerManager(RenderFrameHost
* render_frame_host
);
136 WebContents
* web_contents() const { return web_contents_
; }
138 // Adds a given player to the list.
139 void AddPlayer(media::MediaPlayerAndroid
* player
);
141 // Removes the player with the specified id.
142 void RemovePlayer(int player_id
);
144 // Replaces a player with the specified id with a given MediaPlayerAndroid
145 // object. This will also return the original MediaPlayerAndroid object that
147 scoped_ptr
<media::MediaPlayerAndroid
> SwapPlayer(
149 media::MediaPlayerAndroid
* player
);
153 // Helper function to send messages to RenderFrameObserver.
154 bool Send(IPC::Message
* msg
);
157 // Constructs a MediaPlayerAndroid object.
158 media::MediaPlayerAndroid
* CreateMediaPlayer(
159 const MediaPlayerHostMsg_Initialize_Params
& media_player_params
,
161 BrowserDemuxerAndroid
* demuxer
);
163 // MediaPlayerAndroid must call this before it is going to decode
164 // media streams. This helps the manager object maintain an array
165 // of active MediaPlayerAndroid objects and release the resources
166 // when needed. Currently we only count video resources as they are
167 // constrained by hardware and memory limits.
168 virtual void OnMediaResourcesRequested(int player_id
);
170 // Called when a player releases all decoding resources.
171 void ReleaseMediaResources(int player_id
);
173 // Releases the player. However, don't remove it from |players_|.
174 void ReleasePlayer(media::MediaPlayerAndroid
* player
);
176 #if defined(VIDEO_HOLE)
177 void ReleasePlayerOfExternalVideoSurfaceIfNeeded(int future_player
);
178 void OnRequestExternalSurface(int player_id
, const gfx::RectF
& rect
);
179 #endif // defined(VIDEO_HOLE)
181 RenderFrameHost
* const render_frame_host_
;
183 // An array of managed players.
184 ScopedVector
<media::MediaPlayerAndroid
> players_
;
186 // The fullscreen video view object or NULL if video is not played in
188 scoped_ptr
<ContentVideoView
> video_view_
;
190 #if defined(VIDEO_HOLE)
191 scoped_ptr
<ExternalVideoSurfaceContainer
> external_video_surface_container_
;
194 // Player ID of the fullscreen media player.
195 int fullscreen_player_id_
;
197 // Whether the fullscreen player has been Release()-d.
198 bool fullscreen_player_is_released_
;
200 WebContents
* const web_contents_
;
202 // Object for retrieving resources media players.
203 scoped_ptr
<media::MediaResourceGetter
> media_resource_getter_
;
205 // NOTE: Weak pointers must be invalidated before all other member variables.
206 base::WeakPtrFactory
<BrowserMediaPlayerManager
> weak_ptr_factory_
;
208 DISALLOW_COPY_AND_ASSIGN(BrowserMediaPlayerManager
);
211 } // namespace content
213 #endif // CONTENT_BROWSER_MEDIA_ANDROID_BROWSER_MEDIA_PLAYER_MANAGER_H_