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 MediaPlayersObserver
;
36 class RenderFrameHost
;
39 // This class manages all the MediaPlayerAndroid objects.
40 // It receives control operations from the the render process, and forwards
41 // them to corresponding MediaPlayerAndroid object. Callbacks from
42 // MediaPlayerAndroid objects are converted to IPCs and then sent to the render
44 class CONTENT_EXPORT BrowserMediaPlayerManager
45 : public media::MediaPlayerManager
,
46 public MediaSessionObserver
{
48 // Permits embedders to provide an extended version of the class.
49 typedef BrowserMediaPlayerManager
* (*Factory
)(RenderFrameHost
*,
50 MediaPlayersObserver
*);
51 static void RegisterFactory(Factory factory
);
53 // Permits embedders to handle custom urls.
54 static void RegisterMediaUrlInterceptor(
55 media::MediaUrlInterceptor
* media_url_interceptor
);
57 // Pass a java surface object to the MediaPlayerAndroid object
58 // identified by render process handle, render frame ID and player ID.
59 static void SetSurfacePeer(scoped_refptr
<gfx::SurfaceTexture
> surface_texture
,
60 base::ProcessHandle render_process_handle
,
64 // Returns a new instance using the registered factory if available.
65 static BrowserMediaPlayerManager
* Create(
67 MediaPlayersObserver
* audio_monitor
);
69 ContentViewCore
* GetContentViewCore() const;
71 ~BrowserMediaPlayerManager() override
;
73 // Fullscreen video playback controls.
74 virtual void ExitFullscreen(bool release_media_player
);
75 virtual void SetVideoSurface(gfx::ScopedJavaSurface surface
);
77 // Called when browser player wants the renderer media element to seek.
78 // Any actual seek started by renderer will be handled by browser in OnSeek().
79 void OnSeekRequest(int player_id
, const base::TimeDelta
& time_to_seek
);
81 // Stops and releases every media managed by this class.
82 void ReleaseAllMediaPlayers();
84 // media::MediaPlayerManager overrides.
85 void OnTimeUpdate(int player_id
,
86 base::TimeDelta current_timestamp
,
87 base::TimeTicks current_time_ticks
) override
;
88 void OnMediaMetadataChanged(int player_id
,
89 base::TimeDelta duration
,
92 bool success
) override
;
93 void OnPlaybackComplete(int player_id
) override
;
94 void OnMediaInterrupted(int player_id
) override
;
95 void OnBufferingUpdate(int player_id
, int percentage
) override
;
96 void OnSeekComplete(int player_id
,
97 const base::TimeDelta
& current_time
) override
;
98 void OnError(int player_id
, int error
) override
;
99 void OnVideoSizeChanged(int player_id
, int width
, int height
) override
;
100 void OnAudibleStateChanged(
101 int player_id
, bool is_audible_now
) override
;
102 void OnWaitingForDecryptionKey(int player_id
) override
;
104 media::MediaResourceGetter
* GetMediaResourceGetter() override
;
105 media::MediaUrlInterceptor
* GetMediaUrlInterceptor() override
;
106 media::MediaPlayerAndroid
* GetFullscreenPlayer() override
;
107 media::MediaPlayerAndroid
* GetPlayer(int player_id
) override
;
108 bool RequestPlay(int player_id
) override
;
109 #if defined(VIDEO_HOLE)
110 void AttachExternalVideoSurface(int player_id
, jobject surface
);
111 void DetachExternalVideoSurface(int player_id
);
112 void OnFrameInfoUpdated();
113 #endif // defined(VIDEO_HOLE)
115 // MediaSessionObserver overrides.
116 void OnSuspend(int player_id
) override
;
117 void OnResume(int player_id
) override
;
120 virtual void OnEnterFullscreen(int player_id
);
121 virtual void OnInitialize(
122 const MediaPlayerHostMsg_Initialize_Params
& media_player_params
);
123 virtual void OnStart(int player_id
);
124 virtual void OnSeek(int player_id
, const base::TimeDelta
& time
);
125 virtual void OnPause(int player_id
, bool is_media_related_action
);
126 virtual void OnSetVolume(int player_id
, double volume
);
127 virtual void OnSetPoster(int player_id
, const GURL
& poster
);
128 virtual void OnReleaseResources(int player_id
);
129 virtual void OnDestroyPlayer(int player_id
);
130 virtual void OnRequestRemotePlayback(int player_id
);
131 virtual void OnRequestRemotePlaybackControl(int player_id
);
132 virtual void ReleaseFullscreenPlayer(media::MediaPlayerAndroid
* player
);
133 #if defined(VIDEO_HOLE)
134 void OnNotifyExternalSurface(
135 int player_id
, bool is_request
, const gfx::RectF
& rect
);
136 #endif // defined(VIDEO_HOLE)
139 // Clients must use Create() or subclass constructor.
140 BrowserMediaPlayerManager(RenderFrameHost
* render_frame_host
,
141 MediaPlayersObserver
* audio_monitor
);
143 WebContents
* web_contents() const { return web_contents_
; }
145 // Adds a given player to the list.
146 void AddPlayer(media::MediaPlayerAndroid
* player
);
148 // Removes the player with the specified id.
149 void RemovePlayer(int player_id
);
151 // Replaces a player with the specified id with a given MediaPlayerAndroid
152 // object. This will also return the original MediaPlayerAndroid object that
154 scoped_ptr
<media::MediaPlayerAndroid
> SwapPlayer(
156 media::MediaPlayerAndroid
* player
);
160 // Helper function to send messages to RenderFrameObserver.
161 bool Send(IPC::Message
* msg
);
164 // Constructs a MediaPlayerAndroid object.
165 media::MediaPlayerAndroid
* CreateMediaPlayer(
166 const MediaPlayerHostMsg_Initialize_Params
& media_player_params
,
168 BrowserDemuxerAndroid
* demuxer
);
170 // MediaPlayerAndroid must call this before it is going to decode
171 // media streams. This helps the manager object maintain an array
172 // of active MediaPlayerAndroid objects and release the resources
173 // when needed. Currently we only count video resources as they are
174 // constrained by hardware and memory limits.
175 virtual void OnMediaResourcesRequested(int player_id
);
177 // Called when a player releases all decoding resources.
178 void ReleaseMediaResources(int player_id
);
180 // Releases the player. However, don't remove it from |players_|.
181 void ReleasePlayer(media::MediaPlayerAndroid
* player
);
183 #if defined(VIDEO_HOLE)
184 void ReleasePlayerOfExternalVideoSurfaceIfNeeded(int future_player
);
185 void OnRequestExternalSurface(int player_id
, const gfx::RectF
& rect
);
186 #endif // defined(VIDEO_HOLE)
188 RenderFrameHost
* const render_frame_host_
;
190 MediaPlayersObserver
* audio_monitor_
;
192 // An array of managed players.
193 ScopedVector
<media::MediaPlayerAndroid
> players_
;
195 // The fullscreen video view object or NULL if video is not played in
197 scoped_ptr
<ContentVideoView
> video_view_
;
199 #if defined(VIDEO_HOLE)
200 scoped_ptr
<ExternalVideoSurfaceContainer
> external_video_surface_container_
;
203 // Player ID of the fullscreen media player.
204 int fullscreen_player_id_
;
206 // Whether the fullscreen player has been Release()-d.
207 bool fullscreen_player_is_released_
;
209 WebContents
* const web_contents_
;
211 // Object for retrieving resources media players.
212 scoped_ptr
<media::MediaResourceGetter
> media_resource_getter_
;
214 // NOTE: Weak pointers must be invalidated before all other member variables.
215 base::WeakPtrFactory
<BrowserMediaPlayerManager
> weak_ptr_factory_
;
217 DISALLOW_COPY_AND_ASSIGN(BrowserMediaPlayerManager
);
220 } // namespace content
222 #endif // CONTENT_BROWSER_MEDIA_ANDROID_BROWSER_MEDIA_PLAYER_MANAGER_H_