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 "media/base/android/media_url_interceptor.h"
20 #include "ui/gfx/geometry/rect_f.h"
27 struct MediaPlayerHostMsg_Initialize_Params
;
30 class BrowserDemuxerAndroid
;
31 class ContentViewCoreImpl
;
32 class ExternalVideoSurfaceContainer
;
33 class RenderFrameHost
;
36 // This class manages all the MediaPlayerAndroid objects.
37 // It receives control operations from the the render process, and forwards
38 // them to corresponding MediaPlayerAndroid object. Callbacks from
39 // MediaPlayerAndroid objects are converted to IPCs and then sent to the render
41 class CONTENT_EXPORT BrowserMediaPlayerManager
42 : public media::MediaPlayerManager
{
44 // Permits embedders to provide an extended version of the class.
45 typedef BrowserMediaPlayerManager
* (*Factory
)(RenderFrameHost
*);
46 static void RegisterFactory(Factory factory
);
48 // Permits embedders to handle custom urls.
49 static void RegisterMediaUrlInterceptor(
50 media::MediaUrlInterceptor
* media_url_interceptor
);
52 // Returns a new instance using the registered factory if available.
53 static BrowserMediaPlayerManager
* Create(RenderFrameHost
* rfh
);
55 ContentViewCoreImpl
* GetContentViewCore() const;
57 ~BrowserMediaPlayerManager() override
;
59 // Fullscreen video playback controls.
60 virtual void ExitFullscreen(bool release_media_player
);
61 virtual void SetVideoSurface(gfx::ScopedJavaSurface surface
);
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
);
67 // Stops and releases every media managed by this class.
68 void ReleaseAllMediaPlayers();
70 // media::MediaPlayerManager overrides.
71 void OnTimeUpdate(int player_id
,
72 base::TimeDelta current_timestamp
,
73 base::TimeTicks current_time_ticks
) override
;
74 void OnMediaMetadataChanged(int player_id
,
75 base::TimeDelta duration
,
78 bool success
) override
;
79 void OnPlaybackComplete(int player_id
) override
;
80 void OnMediaInterrupted(int player_id
) override
;
81 void OnBufferingUpdate(int player_id
, int percentage
) override
;
82 void OnSeekComplete(int player_id
,
83 const base::TimeDelta
& current_time
) override
;
84 void OnError(int player_id
, int error
) override
;
85 void OnVideoSizeChanged(int player_id
, int width
, int height
) override
;
86 media::MediaResourceGetter
* GetMediaResourceGetter() override
;
87 media::MediaUrlInterceptor
* GetMediaUrlInterceptor() override
;
88 media::MediaPlayerAndroid
* GetFullscreenPlayer() override
;
89 media::MediaPlayerAndroid
* GetPlayer(int player_id
) override
;
90 void RequestFullScreen(int player_id
) override
;
91 #if defined(VIDEO_HOLE)
92 bool ShouldUseVideoOverlayForEmbeddedEncryptedVideo() override
;
94 void AttachExternalVideoSurface(int player_id
, jobject surface
);
95 void DetachExternalVideoSurface(int player_id
);
96 void OnFrameInfoUpdated();
97 #endif // defined(VIDEO_HOLE)
100 virtual void OnEnterFullscreen(int player_id
);
101 virtual void OnExitFullscreen(int player_id
);
102 virtual void OnInitialize(
103 const MediaPlayerHostMsg_Initialize_Params
& media_player_params
);
104 virtual void OnStart(int player_id
);
105 virtual void OnSeek(int player_id
, const base::TimeDelta
& time
);
106 virtual void OnPause(int player_id
, bool is_media_related_action
);
107 virtual void OnSetVolume(int player_id
, double volume
);
108 virtual void OnSetPoster(int player_id
, const GURL
& poster
);
109 virtual void OnReleaseResources(int player_id
);
110 virtual void OnDestroyPlayer(int player_id
);
111 virtual void OnRequestRemotePlayback(int player_id
);
112 virtual void OnRequestRemotePlaybackControl(int player_id
);
113 virtual void ReleaseFullscreenPlayer(media::MediaPlayerAndroid
* player
);
114 #if defined(VIDEO_HOLE)
115 void OnNotifyExternalSurface(
116 int player_id
, bool is_request
, const gfx::RectF
& rect
);
117 #endif // defined(VIDEO_HOLE)
120 // Clients must use Create() or subclass constructor.
121 explicit BrowserMediaPlayerManager(RenderFrameHost
* render_frame_host
);
123 WebContents
* web_contents() const { return web_contents_
; }
125 // Adds a given player to the list.
126 void AddPlayer(media::MediaPlayerAndroid
* player
);
128 // Removes the player with the specified id.
129 void RemovePlayer(int player_id
);
131 // Replaces a player with the specified id with a given MediaPlayerAndroid
132 // object. This will also return the original MediaPlayerAndroid object that
134 scoped_ptr
<media::MediaPlayerAndroid
> SwapPlayer(
136 media::MediaPlayerAndroid
* player
);
140 // Helper function to send messages to RenderFrameObserver.
141 bool Send(IPC::Message
* msg
);
144 // Constructs a MediaPlayerAndroid object.
145 media::MediaPlayerAndroid
* CreateMediaPlayer(
146 const MediaPlayerHostMsg_Initialize_Params
& media_player_params
,
148 media::MediaPlayerManager
* manager
,
149 BrowserDemuxerAndroid
* demuxer
);
151 // MediaPlayerAndroid must call this before it is going to decode
152 // media streams. This helps the manager object maintain an array
153 // of active MediaPlayerAndroid objects and release the resources
154 // when needed. Currently we only count video resources as they are
155 // constrained by hardware and memory limits.
156 virtual void OnMediaResourcesRequested(int player_id
);
158 // Called when a player releases all decoding resources.
159 void ReleaseMediaResources(int player_id
);
161 // Releases the player. However, don't remove it from |players_|.
162 void ReleasePlayer(media::MediaPlayerAndroid
* player
);
164 #if defined(VIDEO_HOLE)
165 void ReleasePlayerOfExternalVideoSurfaceIfNeeded(int future_player
);
166 void OnRequestExternalSurface(int player_id
, const gfx::RectF
& rect
);
167 #endif // defined(VIDEO_HOLE)
169 RenderFrameHost
* const render_frame_host_
;
171 // An array of managed players.
172 ScopedVector
<media::MediaPlayerAndroid
> players_
;
174 // The fullscreen video view object or NULL if video is not played in
176 scoped_ptr
<ContentVideoView
> video_view_
;
178 #if defined(VIDEO_HOLE)
179 scoped_ptr
<ExternalVideoSurfaceContainer
> external_video_surface_container_
;
182 // Player ID of the fullscreen media player.
183 int fullscreen_player_id_
;
185 // Whether the fullscreen player has been Release()-d.
186 bool fullscreen_player_is_released_
;
188 WebContents
* const web_contents_
;
190 // Object for retrieving resources media players.
191 scoped_ptr
<media::MediaResourceGetter
> media_resource_getter_
;
193 // NOTE: Weak pointers must be invalidated before all other member variables.
194 base::WeakPtrFactory
<BrowserMediaPlayerManager
> weak_ptr_factory_
;
196 DISALLOW_COPY_AND_ASSIGN(BrowserMediaPlayerManager
);
199 } // namespace content
201 #endif // CONTENT_BROWSER_MEDIA_ANDROID_BROWSER_MEDIA_PLAYER_MANAGER_H_