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 // Pauses all video players manages by this class. 71 void PauseVideo(); 72 73 // media::MediaPlayerManager overrides. 74 virtual void OnTimeUpdate( 75 int player_id, base::TimeDelta current_time) OVERRIDE; 76 virtual void OnMediaMetadataChanged( 77 int player_id, 78 base::TimeDelta duration, 79 int width, 80 int height, 81 bool success) OVERRIDE; 82 virtual void OnPlaybackComplete(int player_id) OVERRIDE; 83 virtual void OnMediaInterrupted(int player_id) OVERRIDE; 84 virtual void OnBufferingUpdate(int player_id, int percentage) OVERRIDE; 85 virtual void OnSeekComplete( 86 int player_id, 87 const base::TimeDelta& current_time) OVERRIDE; 88 virtual void OnError(int player_id, int error) OVERRIDE; 89 virtual void OnVideoSizeChanged( 90 int player_id, int width, int height) OVERRIDE; 91 virtual media::MediaResourceGetter* GetMediaResourceGetter() OVERRIDE; 92 virtual media::MediaUrlInterceptor* GetMediaUrlInterceptor() OVERRIDE; 93 virtual media::MediaPlayerAndroid* GetFullscreenPlayer() OVERRIDE; 94 virtual media::MediaPlayerAndroid* GetPlayer(int player_id) OVERRIDE; 95 virtual void RequestFullScreen(int player_id) OVERRIDE; 96 #if defined(VIDEO_HOLE) 97 virtual bool ShouldUseVideoOverlayForEmbeddedEncryptedVideo() OVERRIDE; 98 99 void AttachExternalVideoSurface(int player_id, jobject surface); 100 void DetachExternalVideoSurface(int player_id); 101 void OnFrameInfoUpdated(); 102 #endif // defined(VIDEO_HOLE) 103 104 // Message handlers. 105 virtual void OnEnterFullscreen(int player_id); 106 virtual void OnExitFullscreen(int player_id); 107 virtual void OnInitialize( 108 const MediaPlayerHostMsg_Initialize_Params& media_player_params); 109 virtual void OnStart(int player_id); 110 virtual void OnSeek(int player_id, const base::TimeDelta& time); 111 virtual void OnPause(int player_id, bool is_media_related_action); 112 virtual void OnSetVolume(int player_id, double volume); 113 virtual void OnSetPoster(int player_id, const GURL& poster); 114 virtual void OnReleaseResources(int player_id); 115 virtual void OnDestroyPlayer(int player_id); 116 virtual void ReleaseFullscreenPlayer(media::MediaPlayerAndroid* player); 117 #if defined(VIDEO_HOLE) 118 void OnNotifyExternalSurface( 119 int player_id, bool is_request, const gfx::RectF& rect); 120 #endif // defined(VIDEO_HOLE) 121 122 protected: 123 // Clients must use Create() or subclass constructor. 124 explicit BrowserMediaPlayerManager(RenderFrameHost* render_frame_host); 125 web_contents()126 WebContents* web_contents() const { return web_contents_; } 127 128 // Adds a given player to the list. 129 void AddPlayer(media::MediaPlayerAndroid* player); 130 131 // Removes the player with the specified id. 132 void RemovePlayer(int player_id); 133 134 // Replaces a player with the specified id with a given MediaPlayerAndroid 135 // object. This will also return the original MediaPlayerAndroid object that 136 // was replaced. 137 scoped_ptr<media::MediaPlayerAndroid> SwapPlayer( 138 int player_id, 139 media::MediaPlayerAndroid* player); 140 141 int RoutingID(); 142 143 // Helper function to send messages to RenderFrameObserver. 144 bool Send(IPC::Message* msg); 145 146 private: 147 // Constructs a MediaPlayerAndroid object. 148 media::MediaPlayerAndroid* CreateMediaPlayer( 149 const MediaPlayerHostMsg_Initialize_Params& media_player_params, 150 bool hide_url_log, 151 media::MediaPlayerManager* manager, 152 BrowserDemuxerAndroid* demuxer); 153 154 // MediaPlayerAndroid must call this before it is going to decode 155 // media streams. This helps the manager object maintain an array 156 // of active MediaPlayerAndroid objects and release the resources 157 // when needed. Currently we only count video resources as they are 158 // constrained by hardware and memory limits. 159 virtual void OnMediaResourcesRequested(int player_id); 160 161 // Similar to the above call, MediaPlayerAndroid must call this method when 162 // releasing all the decoding resources. 163 virtual void OnMediaResourcesReleased(int player_id); 164 165 #if defined(VIDEO_HOLE) 166 void OnRequestExternalSurface(int player_id, const gfx::RectF& rect); 167 #endif // defined(VIDEO_HOLE) 168 169 RenderFrameHost* const render_frame_host_; 170 171 // An array of managed players. 172 ScopedVector<media::MediaPlayerAndroid> players_; 173 174 // The fullscreen video view object or NULL if video is not played in 175 // fullscreen. 176 scoped_ptr<ContentVideoView> video_view_; 177 178 #if defined(VIDEO_HOLE) 179 scoped_ptr<ExternalVideoSurfaceContainer> external_video_surface_container_; 180 #endif 181 182 // Player ID of the fullscreen media player. 183 int fullscreen_player_id_; 184 185 // Whether the fullscreen player has been Release()-d. 186 bool fullscreen_player_is_released_; 187 188 WebContents* const web_contents_; 189 190 // Object for retrieving resources media players. 191 scoped_ptr<media::MediaResourceGetter> media_resource_getter_; 192 193 // NOTE: Weak pointers must be invalidated before all other member variables. 194 base::WeakPtrFactory<BrowserMediaPlayerManager> weak_ptr_factory_; 195 196 DISALLOW_COPY_AND_ASSIGN(BrowserMediaPlayerManager); 197 }; 198 199 } // namespace content 200 201 #endif // CONTENT_BROWSER_MEDIA_ANDROID_BROWSER_MEDIA_PLAYER_MANAGER_H_ 202