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_ANDROID_BROWSER_MEDIA_PLAYER_MANAGER_H_ 6 #define CONTENT_BROWSER_ANDROID_BROWSER_MEDIA_PLAYER_MANAGER_H_ 7 8 #include <map> 9 #include <string> 10 #include <vector> 11 12 #include "base/basictypes.h" 13 #include "base/callback.h" 14 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/scoped_vector.h" 16 #include "base/time/time.h" 17 #include "content/browser/android/content_video_view.h" 18 #include "content/public/browser/render_view_host_observer.h" 19 #include "media/base/android/demuxer_stream_player_params.h" 20 #include "media/base/android/media_player_android.h" 21 #include "media/base/android/media_player_manager.h" 22 #include "ui/gfx/rect_f.h" 23 #include "url/gurl.h" 24 25 namespace media { 26 class MediaDrmBridge; 27 } 28 29 namespace content { 30 31 class WebContents; 32 33 // This class manages all the MediaPlayerAndroid objects. It receives 34 // control operations from the the render process, and forwards 35 // them to corresponding MediaPlayerAndroid object. Callbacks from 36 // MediaPlayerAndroid objects are converted to IPCs and then sent to the 37 // render process. 38 class CONTENT_EXPORT BrowserMediaPlayerManager 39 : public RenderViewHostObserver, 40 public media::MediaPlayerManager { 41 public: 42 virtual ~BrowserMediaPlayerManager(); 43 44 // RenderViewHostObserver overrides. 45 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 46 47 // Fullscreen video playback controls. 48 virtual void FullscreenPlayerPlay(); 49 virtual void FullscreenPlayerPause(); 50 virtual void FullscreenPlayerSeek(int msec); 51 virtual void ExitFullscreen(bool release_media_player); 52 virtual void SetVideoSurface(gfx::ScopedJavaSurface surface); 53 54 // media::MediaPlayerManager overrides. 55 virtual void OnTimeUpdate( 56 int player_id, base::TimeDelta current_time) OVERRIDE; 57 virtual void OnMediaMetadataChanged( 58 int player_id, 59 base::TimeDelta duration, 60 int width, 61 int height, 62 bool success) OVERRIDE; 63 virtual void OnPlaybackComplete(int player_id) OVERRIDE; 64 virtual void OnMediaInterrupted(int player_id) OVERRIDE; 65 virtual void OnBufferingUpdate(int player_id, int percentage) OVERRIDE; 66 virtual void OnSeekComplete( 67 int player_id, base::TimeDelta current_time) OVERRIDE; 68 virtual void OnError(int player_id, int error) OVERRIDE; 69 virtual void OnVideoSizeChanged( 70 int player_id, int width, int height) OVERRIDE; 71 virtual void OnReadFromDemuxer(int player_id, 72 media::DemuxerStream::Type type) OVERRIDE; 73 virtual void RequestMediaResources(int player_id) OVERRIDE; 74 virtual void ReleaseMediaResources(int player_id) OVERRIDE; 75 virtual media::MediaResourceGetter* GetMediaResourceGetter() OVERRIDE; 76 virtual media::MediaPlayerAndroid* GetFullscreenPlayer() OVERRIDE; 77 virtual media::MediaPlayerAndroid* GetPlayer(int player_id) OVERRIDE; 78 virtual media::MediaDrmBridge* GetDrmBridge(int media_keys_id) OVERRIDE; 79 virtual void DestroyAllMediaPlayers() OVERRIDE; 80 virtual void OnMediaSeekRequest(int player_id, base::TimeDelta time_to_seek, 81 unsigned seek_request_id) OVERRIDE; 82 virtual void OnMediaConfigRequest(int player_id) OVERRIDE; 83 virtual void OnProtectedSurfaceRequested(int player_id) OVERRIDE; 84 virtual void OnKeyAdded(int media_keys_id, 85 const std::string& session_id) OVERRIDE; 86 virtual void OnKeyError(int media_keys_id, 87 const std::string& session_id, 88 media::MediaKeys::KeyError error_code, 89 int system_code) OVERRIDE; 90 virtual void OnKeyMessage(int media_keys_id, 91 const std::string& session_id, 92 const std::vector<uint8>& message, 93 const std::string& destination_url) OVERRIDE; 94 95 #if defined(GOOGLE_TV) 96 void AttachExternalVideoSurface(int player_id, jobject surface); 97 void DetachExternalVideoSurface(int player_id); 98 #endif 99 100 protected: 101 friend MediaPlayerManager* MediaPlayerManager::Create( 102 content::RenderViewHost*); 103 104 // The instance of this class is supposed to be created by either Create() 105 // method of MediaPlayerManager or the derived classes constructors. 106 explicit BrowserMediaPlayerManager(RenderViewHost* render_view_host); 107 108 // Message handlers. 109 virtual void OnEnterFullscreen(int player_id); 110 virtual void OnExitFullscreen(int player_id); 111 virtual void OnInitialize( 112 int player_id, 113 const GURL& url, 114 media::MediaPlayerAndroid::SourceType source_type, 115 const GURL& first_party_for_cookies); 116 virtual void OnStart(int player_id); 117 virtual void OnSeek(int player_id, base::TimeDelta time); 118 virtual void OnPause(int player_id); 119 virtual void OnSetVolume(int player_id, double volume); 120 virtual void OnReleaseResources(int player_id); 121 virtual void OnDestroyPlayer(int player_id); 122 virtual void OnDemuxerReady( 123 int player_id, 124 const media::MediaPlayerHostMsg_DemuxerReady_Params& params); 125 virtual void OnReadFromDemuxerAck( 126 int player_id, 127 const media::MediaPlayerHostMsg_ReadFromDemuxerAck_Params& params); 128 void OnMediaSeekRequestAck(int player_id, unsigned seek_request_id); 129 void OnInitializeCDM(int media_keys_id, const std::vector<uint8>& uuid); 130 void OnGenerateKeyRequest(int media_keys_id, 131 const std::string& type, 132 const std::vector<uint8>& init_data); 133 void OnAddKey(int media_keys_id, 134 const std::vector<uint8>& key, 135 const std::vector<uint8>& init_data, 136 const std::string& session_id); 137 void OnCancelKeyRequest(int media_keys_id, const std::string& session_id); 138 void OnDurationChanged(int player_id, const base::TimeDelta& duration); 139 void OnSetMediaKeys(int player_id, int media_keys_id); 140 141 #if defined(GOOGLE_TV) 142 virtual void OnNotifyExternalSurface( 143 int player_id, bool is_request, const gfx::RectF& rect); 144 #endif 145 146 // Adds a given player to the list. 147 void AddPlayer(media::MediaPlayerAndroid* player); 148 149 // Removes the player with the specified id. 150 void RemovePlayer(int player_id); 151 152 // Add a new MediaDrmBridge for the given |uuid| and |media_keys_id|. 153 void AddDrmBridge(int media_keys_id, const std::vector<uint8>& uuid); 154 155 // Removes the DRM bridge with the specified id. 156 void RemoveDrmBridge(int media_keys_id); 157 158 private: 159 // An array of managed players. 160 ScopedVector<media::MediaPlayerAndroid> players_; 161 162 // An array of managed media DRM bridges. 163 ScopedVector<media::MediaDrmBridge> drm_bridges_; 164 165 // The fullscreen video view object or NULL if video is not played in 166 // fullscreen. 167 scoped_ptr<ContentVideoView> video_view_; 168 169 // Player ID of the fullscreen media player. 170 int fullscreen_player_id_; 171 172 WebContents* web_contents_; 173 174 // Object for retrieving resources media players. 175 scoped_ptr<media::MediaResourceGetter> media_resource_getter_; 176 177 DISALLOW_COPY_AND_ASSIGN(BrowserMediaPlayerManager); 178 }; 179 180 } // namespace content 181 182 #endif // CONTENT_BROWSER_ANDROID_BROWSER_MEDIA_PLAYER_MANAGER_H_ 183