| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef WEBKIT_MEDIA_ANDROID_WEBMEDIAPLAYER_ANDROID_H_ | 5 #ifndef WEBKIT_MEDIA_ANDROID_WEBMEDIAPLAYER_ANDROID_H_ |
| 6 #define WEBKIT_MEDIA_ANDROID_WEBMEDIAPLAYER_ANDROID_H_ | 6 #define WEBKIT_MEDIA_ANDROID_WEBMEDIAPLAYER_ANDROID_H_ |
| 7 | 7 |
| 8 #include <jni.h> | 8 #include <jni.h> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/time.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMediaPlayer.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMediaPlayer.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebVideoFrame.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSize.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSize.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebVideoFrame.h" | |
| 18 | |
| 19 namespace WebKit { | |
| 20 class WebCookieJar; | |
| 21 class WebFrame; | |
| 22 } | |
| 23 | |
| 24 namespace media { | |
| 25 class MediaPlayerBridge; | |
| 26 } | |
| 27 | 18 |
| 28 namespace webkit_media { | 19 namespace webkit_media { |
| 29 | 20 |
| 30 class StreamTextureFactory; | 21 class StreamTextureFactory; |
| 31 class StreamTextureProxy; | 22 class StreamTextureProxy; |
| 32 class WebMediaPlayerManagerAndroid; | 23 class WebMediaPlayerManagerAndroid; |
| 33 class WebMediaPlayerProxyAndroid; | |
| 34 | 24 |
| 35 // This class serves as the android implementation of WebKit::WebMediaPlayer. | 25 // An abstract class that serves as the common base class for implementing |
| 36 // It implements all the playback functions by forwarding calls to android | 26 // WebKit::WebMediaPlayer on Android. |
| 37 // media player, and reports player state changes to the webkit. | 27 class WebMediaPlayerAndroid |
| 38 class WebMediaPlayerAndroid : | 28 : public WebKit::WebMediaPlayer, |
| 39 public WebKit::WebMediaPlayer, | 29 public MessageLoop::DestructionObserver { |
| 40 public MessageLoop::DestructionObserver, | |
| 41 public base::SupportsWeakPtr<WebMediaPlayerAndroid> { | |
| 42 public: | 30 public: |
| 43 WebMediaPlayerAndroid(WebKit::WebFrame* frame, | |
| 44 WebKit::WebMediaPlayerClient* client, | |
| 45 WebKit::WebCookieJar* cookie_jar, | |
| 46 webkit_media::WebMediaPlayerManagerAndroid* manager, | |
| 47 webkit_media::StreamTextureFactory* factory); | |
| 48 virtual ~WebMediaPlayerAndroid(); | |
| 49 | |
| 50 // Set |incognito_mode_| to true if in incognito mode. | |
| 51 static void InitIncognito(bool incognito_mode); | |
| 52 | |
| 53 // Resource loading. | 31 // Resource loading. |
| 54 virtual void load(const WebKit::WebURL& url, CORSMode cors_mode); | 32 virtual void load(const WebKit::WebURL& url, CORSMode cors_mode); |
| 55 virtual void cancelLoad(); | 33 virtual void cancelLoad(); |
| 56 | 34 |
| 57 // Playback controls. | 35 // Playback controls. |
| 58 virtual void play(); | 36 virtual void play(); |
| 59 virtual void pause(); | 37 virtual void pause(); |
| 60 virtual void seek(float seconds); | 38 virtual void seek(float seconds); |
| 61 virtual bool supportsFullscreen() const; | 39 virtual bool supportsFullscreen() const; |
| 62 virtual bool supportsSave() const; | 40 virtual bool supportsSave() const; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 // Provide statistics. | 84 // Provide statistics. |
| 107 virtual unsigned decodedFrameCount() const; | 85 virtual unsigned decodedFrameCount() const; |
| 108 virtual unsigned droppedFrameCount() const; | 86 virtual unsigned droppedFrameCount() const; |
| 109 virtual unsigned audioDecodedByteCount() const; | 87 virtual unsigned audioDecodedByteCount() const; |
| 110 virtual unsigned videoDecodedByteCount() const; | 88 virtual unsigned videoDecodedByteCount() const; |
| 111 | 89 |
| 112 // Methods called from VideoLayerChromium. These methods are running on the | 90 // Methods called from VideoLayerChromium. These methods are running on the |
| 113 // compositor thread. | 91 // compositor thread. |
| 114 virtual WebKit::WebVideoFrame* getCurrentFrame(); | 92 virtual WebKit::WebVideoFrame* getCurrentFrame(); |
| 115 virtual void putCurrentFrame(WebKit::WebVideoFrame*); | 93 virtual void putCurrentFrame(WebKit::WebVideoFrame*); |
| 116 virtual void setStreamTextureClient( | 94 |
| 117 WebKit::WebStreamTextureClient* client); | 95 // This gets called both on compositor and main thread to set the callback |
| 96 // target when a frame is produced. |
| 97 virtual void setStreamTextureClient(WebKit::WebStreamTextureClient* client); |
| 118 | 98 |
| 119 // Media player callback handlers. | 99 // Media player callback handlers. |
| 120 void OnMediaPrepared(); | 100 virtual void OnMediaPrepared(base::TimeDelta duration); |
| 121 void OnPlaybackComplete(); | 101 virtual void OnPlaybackComplete(); |
| 122 void OnBufferingUpdate(int percentage); | 102 virtual void OnBufferingUpdate(int percentage); |
| 123 void OnSeekComplete(); | 103 virtual void OnSeekComplete(base::TimeDelta current_time); |
| 124 void OnMediaError(int error_type); | 104 virtual void OnMediaError(int error_type); |
| 125 void OnMediaInfo(int info_type); | 105 virtual void OnVideoSizeChanged(int width, int height); |
| 126 void OnVideoSizeChanged(int width, int height); | |
| 127 | 106 |
| 128 // This function is called by WebMediaPlayerManagerAndroid to pause the video | 107 // Called to update the current time. |
| 129 // and release |media_player_| and its surface texture when we switch tabs. | 108 virtual void OnTimeUpdate(base::TimeDelta current_time) = 0; |
| 109 |
| 110 // Called when the player is released. |
| 111 virtual void OnPlayerReleased(); |
| 112 |
| 113 // This function is called by the WebMediaPlayerManagerAndroid to pause the |
| 114 // video and release the media player and surface texture when we switch tabs. |
| 130 // However, the actual GlTexture is not released to keep the video screenshot. | 115 // However, the actual GlTexture is not released to keep the video screenshot. |
| 131 void ReleaseMediaResources(); | 116 virtual void ReleaseMediaResources(); |
| 132 | 117 |
| 133 // Whether |media_player_| has been initialized. | 118 // Method to set the surface for video. |
| 134 bool IsInitialized() const; | 119 virtual void SetVideoSurface(jobject j_surface) = 0; |
| 135 | 120 |
| 136 // Method inherited from DestructionObserver. | 121 // Method inherited from DestructionObserver. |
| 137 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; | 122 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; |
| 138 | 123 |
| 139 private: | 124 protected: |
| 140 // Create a media player to load the |url_| and prepare for playback. | 125 // Construct a WebMediaPlayerAndroid object with reference to the |
| 141 // Because of limited decoding resources on mobile devices, idle media players | 126 // client, manager and stream texture factory. |
| 142 // could get released. In that case, we call this function to get a new media | 127 WebMediaPlayerAndroid(WebKit::WebMediaPlayerClient* client, |
| 143 // player when needed. | 128 WebMediaPlayerManagerAndroid* manager, |
| 144 void InitializeMediaPlayer(); | 129 StreamTextureFactory* factory); |
| 130 virtual ~WebMediaPlayerAndroid(); |
| 145 | 131 |
| 146 // Functions that implements media player control. | 132 // Helper method to update the playing state. |
| 147 void PlayInternal(); | 133 virtual void UpdatePlayingState(bool is_playing_); |
| 148 void PauseInternal(); | |
| 149 void SeekInternal(float seconds); | |
| 150 | 134 |
| 151 // Helper methods for posting task for setting states and update WebKit. | 135 // Helper methods for posting task for setting states and update WebKit. |
| 152 void UpdateNetworkState(WebKit::WebMediaPlayer::NetworkState state); | 136 virtual void UpdateNetworkState(WebKit::WebMediaPlayer::NetworkState state); |
| 153 void UpdateReadyState(WebKit::WebMediaPlayer::ReadyState state); | 137 virtual void UpdateReadyState(WebKit::WebMediaPlayer::ReadyState state); |
| 154 | 138 |
| 155 // whether the current process is incognito mode | 139 // Helper method to reestablish the surface texture peer for android |
| 156 static bool incognito_mode_; | 140 // mediaplayer. |
| 141 virtual void EstablishSurfaceTexturePeer(); |
| 157 | 142 |
| 158 WebKit::WebFrame* frame_; | 143 // Method to be implemented by child classes. |
| 144 // Initialize the media player bridge object. |
| 145 virtual void InitializeMediaPlayer(GURL url) = 0; |
| 159 | 146 |
| 147 // Inform the media player to start playing. |
| 148 virtual void PlayInternal() = 0; |
| 149 |
| 150 // Inform the media player to pause. |
| 151 virtual void PauseInternal() = 0; |
| 152 |
| 153 // Inform the media player to seek to a particular position. |
| 154 virtual void SeekInternal(base::TimeDelta time) = 0; |
| 155 |
| 156 // Get the current time from the media player. |
| 157 virtual float GetCurrentTimeInternal() const = 0; |
| 158 |
| 159 // Release the Android Media player. |
| 160 virtual void ReleaseResourcesInternal() = 0; |
| 161 |
| 162 // Cleaning up all remaining resources as this object is about to get deleted. |
| 163 virtual void Destroy() = 0; |
| 164 |
| 165 WebKit::WebMediaPlayerClient* client() { return client_; } |
| 166 |
| 167 int player_id() { return player_id_; } |
| 168 |
| 169 private: |
| 160 WebKit::WebMediaPlayerClient* const client_; | 170 WebKit::WebMediaPlayerClient* const client_; |
| 161 | 171 |
| 162 // Save the list of buffered time ranges. | 172 // Save the list of buffered time ranges. |
| 163 WebKit::WebTimeRanges buffered_; | 173 WebKit::WebTimeRanges buffered_; |
| 164 | 174 |
| 165 // Bridge to the android media player. | |
| 166 scoped_ptr<media::MediaPlayerBridge> media_player_; | |
| 167 | |
| 168 // Size of the media element. | |
| 169 WebKit::WebSize texture_size_; | |
| 170 | |
| 171 // Size of the video. | 175 // Size of the video. |
| 172 WebKit::WebSize natural_size_; | 176 WebKit::WebSize natural_size_; |
| 173 | 177 |
| 174 // The video frame object used for renderering by WebKit. | 178 // The video frame object used for renderering by WebKit. |
| 175 scoped_ptr<WebKit::WebVideoFrame> video_frame_; | 179 scoped_ptr<WebKit::WebVideoFrame> video_frame_; |
| 176 | 180 |
| 177 // Message loops for main renderer thread. | 181 // Message loop for main renderer thread. |
| 178 MessageLoop* main_loop_; | 182 MessageLoop* main_loop_; |
| 179 | 183 |
| 180 // Proxy object that delegates method calls on Render Thread. | |
| 181 // This object is created on the Render Thread and is only called in the | |
| 182 // destructor. | |
| 183 scoped_refptr<WebMediaPlayerProxyAndroid> proxy_; | |
| 184 | |
| 185 // If this is set to true, prepare of the media player is done. | |
| 186 bool prepared_; | |
| 187 | |
| 188 // URL of the media file to be fetched. | 184 // URL of the media file to be fetched. |
| 189 GURL url_; | 185 GURL url_; |
| 190 | 186 |
| 191 // Media duration. | 187 // Media duration. |
| 192 float duration_; | 188 base::TimeDelta duration_; |
| 193 | 189 |
| 194 // When switching tabs, we release the media player. This variable keeps | 190 // The time android media player is trying to seek. |
| 195 // track of the current playback time so that a seek will be performed | |
| 196 // next time the media player gets recreated. | |
| 197 float pending_seek_; | 191 float pending_seek_; |
| 198 | 192 |
| 199 // Internal seek state. | 193 // Internal seek state. |
| 200 bool seeking_; | 194 bool seeking_; |
| 201 | 195 |
| 202 // Whether playback has completed. | |
| 203 float playback_completed_; | |
| 204 | |
| 205 // Whether loading has progressed since the last call to didLoadingProgress. | 196 // Whether loading has progressed since the last call to didLoadingProgress. |
| 206 mutable bool did_loading_progress_; | 197 mutable bool did_loading_progress_; |
| 207 | 198 |
| 208 // Pointer to the cookie jar to get the cookie for the media url. | 199 // Manager for managing this object. |
| 209 WebKit::WebCookieJar* cookie_jar_; | 200 WebMediaPlayerManagerAndroid* manager_; |
| 210 | 201 |
| 211 // Manager for managing this media player. | 202 // Player ID assigned by the |manager_|. |
| 212 webkit_media::WebMediaPlayerManagerAndroid* manager_; | |
| 213 | |
| 214 // Player ID assigned by the media player manager. | |
| 215 int player_id_; | 203 int player_id_; |
| 216 | 204 |
| 217 // Whether the user has clicked the play button while media player | |
| 218 // is preparing. | |
| 219 bool pending_play_event_; | |
| 220 | |
| 221 // Current player states. | 205 // Current player states. |
| 222 WebKit::WebMediaPlayer::NetworkState network_state_; | 206 WebKit::WebMediaPlayer::NetworkState network_state_; |
| 223 WebKit::WebMediaPlayer::ReadyState ready_state_; | 207 WebKit::WebMediaPlayer::ReadyState ready_state_; |
| 224 | 208 |
| 225 // GL texture ID allocated to the video. | 209 // GL texture ID allocated to the video. |
| 226 unsigned int texture_id_; | 210 unsigned int texture_id_; |
| 227 | 211 |
| 228 // Stream texture ID allocated to the video. | 212 // Stream texture ID allocated to the video. |
| 229 unsigned int stream_id_; | 213 unsigned int stream_id_; |
| 230 | 214 |
| 231 // Whether |media_player_| needs to re-establish the surface texture peer. | 215 // Whether the mediaplayer is playing. |
| 216 bool is_playing_; |
| 217 |
| 218 // Whether media player needs to re-establish the surface texture peer. |
| 232 bool needs_establish_peer_; | 219 bool needs_establish_peer_; |
| 233 | 220 |
| 234 // Object for allocating stream textures. | 221 // Object for allocating stream textures. |
| 235 scoped_ptr<webkit_media::StreamTextureFactory> stream_texture_factory_; | 222 scoped_ptr<StreamTextureFactory> stream_texture_factory_; |
| 236 | 223 |
| 237 // Object for calling back the compositor thread to repaint the video when a | 224 // Object for calling back the compositor thread to repaint the video when a |
| 238 // frame available. It should be initialized on the compositor thread. | 225 // frame available. It should be initialized on the compositor thread. |
| 239 scoped_ptr<webkit_media::StreamTextureProxy> stream_texture_proxy_; | 226 scoped_ptr<StreamTextureProxy> stream_texture_proxy_; |
| 240 | 227 |
| 241 DISALLOW_COPY_AND_ASSIGN(WebMediaPlayerAndroid); | 228 DISALLOW_COPY_AND_ASSIGN(WebMediaPlayerAndroid); |
| 242 }; | 229 }; |
| 243 | 230 |
| 244 } // namespace webkit_media | 231 } // namespace webkit_media |
| 245 | 232 |
| 246 #endif // WEBKIT_MEDIA_ANDROID_WEBMEDIAPLAYER_ANDROID_H_ | 233 #endif // WEBKIT_MEDIA_ANDROID_WEBMEDIAPLAYER_ANDROID_H_ |
| OLD | NEW |