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