| OLD | NEW |
| (Empty) |
| 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 WEBKIT_RENDERER_MEDIA_ANDROID_WEBMEDIAPLAYER_ANDROID_H_ | |
| 6 #define WEBKIT_RENDERER_MEDIA_ANDROID_WEBMEDIAPLAYER_ANDROID_H_ | |
| 7 | |
| 8 #include <jni.h> | |
| 9 | |
| 10 #include "base/basictypes.h" | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/memory/weak_ptr.h" | |
| 14 #include "base/message_loop.h" | |
| 15 #include "base/time.h" | |
| 16 #include "cc/layers/video_frame_provider.h" | |
| 17 #include "media/base/android/media_player_android.h" | |
| 18 #include "media/base/demuxer_stream.h" | |
| 19 #include "media/base/media_keys.h" | |
| 20 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h" | |
| 21 #include "third_party/WebKit/public/platform/WebSize.h" | |
| 22 #include "third_party/WebKit/public/platform/WebURL.h" | |
| 23 #include "third_party/WebKit/public/web/WebMediaPlayer.h" | |
| 24 #include "ui/gfx/rect_f.h" | |
| 25 #include "webkit/renderer/media/android/media_source_delegate.h" | |
| 26 #include "webkit/renderer/media/android/stream_texture_factory_android.h" | |
| 27 #include "webkit/renderer/media/crypto/proxy_decryptor.h" | |
| 28 #include "webkit/renderer/media/media_info_loader.h" | |
| 29 | |
| 30 namespace media { | |
| 31 class Demuxer; | |
| 32 class MediaLog; | |
| 33 } | |
| 34 | |
| 35 namespace WebKit { | |
| 36 class WebFrame; | |
| 37 } | |
| 38 | |
| 39 namespace webkit { | |
| 40 class WebLayerImpl; | |
| 41 } | |
| 42 | |
| 43 namespace webkit_media { | |
| 44 | |
| 45 class MediaStreamClient; | |
| 46 class WebMediaPlayerDelegate; | |
| 47 class WebMediaPlayerManagerAndroid; | |
| 48 class WebMediaPlayerProxyAndroid; | |
| 49 | |
| 50 #if defined(GOOGLE_TV) | |
| 51 class MediaStreamAudioRenderer; | |
| 52 #endif | |
| 53 | |
| 54 // This class implements WebKit::WebMediaPlayer by keeping the android | |
| 55 // media player in the browser process. It listens to all the status changes | |
| 56 // sent from the browser process and sends playback controls to the media | |
| 57 // player. | |
| 58 class WebMediaPlayerAndroid | |
| 59 : public WebKit::WebMediaPlayer, | |
| 60 public cc::VideoFrameProvider, | |
| 61 public base::MessageLoop::DestructionObserver, | |
| 62 public base::SupportsWeakPtr<WebMediaPlayerAndroid> { | |
| 63 public: | |
| 64 // Construct a WebMediaPlayerAndroid object. This class communicates | |
| 65 // with the MediaPlayerAndroid object in the browser process through | |
| 66 // |proxy|. | |
| 67 // TODO(qinmin): |frame| argument is used to determine whether the current | |
| 68 // player can enter fullscreen. This logic should probably be moved into | |
| 69 // blink, so that enterFullscreen() will not be called if another video is | |
| 70 // already in fullscreen. | |
| 71 WebMediaPlayerAndroid(WebKit::WebFrame* frame, | |
| 72 WebKit::WebMediaPlayerClient* client, | |
| 73 base::WeakPtr<WebMediaPlayerDelegate> delegate, | |
| 74 WebMediaPlayerManagerAndroid* manager, | |
| 75 WebMediaPlayerProxyAndroid* proxy, | |
| 76 StreamTextureFactory* factory, | |
| 77 media::MediaLog* media_log); | |
| 78 virtual ~WebMediaPlayerAndroid(); | |
| 79 | |
| 80 // WebKit::WebMediaPlayer implementation. | |
| 81 virtual void enterFullscreen(); | |
| 82 virtual void exitFullscreen(); | |
| 83 virtual bool canEnterFullscreen() const; | |
| 84 | |
| 85 // Resource loading. | |
| 86 virtual void load(const WebKit::WebURL& url, CORSMode cors_mode); | |
| 87 virtual void load(const WebKit::WebURL& url, | |
| 88 WebKit::WebMediaSource* media_source, | |
| 89 CORSMode cors_mode); | |
| 90 | |
| 91 // Playback controls. | |
| 92 virtual void play(); | |
| 93 virtual void pause(); | |
| 94 virtual void seek(double seconds); | |
| 95 virtual bool supportsFullscreen() const; | |
| 96 virtual bool supportsSave() const; | |
| 97 virtual void setRate(double rate); | |
| 98 virtual void setVolume(double volume); | |
| 99 virtual const WebKit::WebTimeRanges& buffered(); | |
| 100 virtual double maxTimeSeekable() const; | |
| 101 | |
| 102 // Methods for painting. | |
| 103 virtual void paint(WebKit::WebCanvas* canvas, | |
| 104 const WebKit::WebRect& rect, | |
| 105 unsigned char alpha); | |
| 106 | |
| 107 virtual bool copyVideoTextureToPlatformTexture( | |
| 108 WebKit::WebGraphicsContext3D* web_graphics_context, | |
| 109 unsigned int texture, | |
| 110 unsigned int level, | |
| 111 unsigned int internal_format, | |
| 112 unsigned int type, | |
| 113 bool premultiply_alpha, | |
| 114 bool flip_y); | |
| 115 | |
| 116 // True if the loaded media has a playable video/audio track. | |
| 117 virtual bool hasVideo() const; | |
| 118 virtual bool hasAudio() const; | |
| 119 | |
| 120 // Dimensions of the video. | |
| 121 virtual WebKit::WebSize naturalSize() const; | |
| 122 | |
| 123 // Getters of playback state. | |
| 124 virtual bool paused() const; | |
| 125 virtual bool seeking() const; | |
| 126 virtual double duration() const; | |
| 127 virtual double currentTime() const; | |
| 128 | |
| 129 virtual bool didLoadingProgress() const; | |
| 130 | |
| 131 // Internal states of loading and network. | |
| 132 virtual WebKit::WebMediaPlayer::NetworkState networkState() const; | |
| 133 virtual WebKit::WebMediaPlayer::ReadyState readyState() const; | |
| 134 | |
| 135 virtual bool hasSingleSecurityOrigin() const; | |
| 136 virtual bool didPassCORSAccessCheck() const; | |
| 137 | |
| 138 virtual double mediaTimeForTimeValue(double timeValue) const; | |
| 139 | |
| 140 // Provide statistics. | |
| 141 virtual unsigned decodedFrameCount() const; | |
| 142 virtual unsigned droppedFrameCount() const; | |
| 143 virtual unsigned audioDecodedByteCount() const; | |
| 144 virtual unsigned videoDecodedByteCount() const; | |
| 145 | |
| 146 // cc::VideoFrameProvider implementation. These methods are running on the | |
| 147 // compositor thread. | |
| 148 virtual void SetVideoFrameProviderClient( | |
| 149 cc::VideoFrameProvider::Client* client) OVERRIDE; | |
| 150 virtual scoped_refptr<media::VideoFrame> GetCurrentFrame() OVERRIDE; | |
| 151 virtual void PutCurrentFrame(const scoped_refptr<media::VideoFrame>& frame) | |
| 152 OVERRIDE; | |
| 153 | |
| 154 // Media player callback handlers. | |
| 155 void OnMediaMetadataChanged(base::TimeDelta duration, int width, | |
| 156 int height, bool success); | |
| 157 void OnPlaybackComplete(); | |
| 158 void OnBufferingUpdate(int percentage); | |
| 159 void OnSeekComplete(base::TimeDelta current_time); | |
| 160 void OnMediaError(int error_type); | |
| 161 void OnVideoSizeChanged(int width, int height); | |
| 162 void OnMediaSeekRequest(base::TimeDelta time_to_seek); | |
| 163 void OnMediaConfigRequest(); | |
| 164 void OnDurationChange(const base::TimeDelta& duration); | |
| 165 | |
| 166 // Called to update the current time. | |
| 167 void OnTimeUpdate(base::TimeDelta current_time); | |
| 168 | |
| 169 // Functions called when media player status changes. | |
| 170 void OnMediaPlayerPlay(); | |
| 171 void OnMediaPlayerPause(); | |
| 172 void OnDidEnterFullscreen(); | |
| 173 void OnDidExitFullscreen(); | |
| 174 | |
| 175 // Called when the player is released. | |
| 176 virtual void OnPlayerReleased(); | |
| 177 | |
| 178 // This function is called by the WebMediaPlayerManagerAndroid to pause the | |
| 179 // video and release the media player and surface texture when we switch tabs. | |
| 180 // However, the actual GlTexture is not released to keep the video screenshot. | |
| 181 virtual void ReleaseMediaResources(); | |
| 182 | |
| 183 // Method inherited from DestructionObserver. | |
| 184 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; | |
| 185 | |
| 186 // Detach the player from its manager. | |
| 187 void Detach(); | |
| 188 | |
| 189 #if defined(GOOGLE_TV) | |
| 190 // Retrieve geometry of the media player (i.e. location and size of the video | |
| 191 // frame) if changed. Returns true only if the geometry has been changed since | |
| 192 // the last call. | |
| 193 bool RetrieveGeometryChange(gfx::RectF* rect); | |
| 194 #endif | |
| 195 | |
| 196 virtual MediaKeyException generateKeyRequest( | |
| 197 const WebKit::WebString& key_system, | |
| 198 const unsigned char* init_data, | |
| 199 unsigned init_data_length) OVERRIDE; | |
| 200 virtual MediaKeyException addKey( | |
| 201 const WebKit::WebString& key_system, | |
| 202 const unsigned char* key, | |
| 203 unsigned key_length, | |
| 204 const unsigned char* init_data, | |
| 205 unsigned init_data_length, | |
| 206 const WebKit::WebString& session_id) OVERRIDE; | |
| 207 virtual MediaKeyException cancelKeyRequest( | |
| 208 const WebKit::WebString& key_system, | |
| 209 const WebKit::WebString& session_id) OVERRIDE; | |
| 210 | |
| 211 void OnKeyAdded(const std::string& session_id); | |
| 212 void OnKeyError(const std::string& session_id, | |
| 213 media::MediaKeys::KeyError error_code, | |
| 214 int system_code); | |
| 215 void OnKeyMessage(const std::string& session_id, | |
| 216 const std::string& message, | |
| 217 const std::string& destination_url); | |
| 218 | |
| 219 void OnNeedKey(const std::string& type, | |
| 220 const std::string& session_id, | |
| 221 scoped_ptr<uint8[]> init_data, | |
| 222 int init_data_size); | |
| 223 | |
| 224 #if defined(GOOGLE_TV) | |
| 225 bool InjectMediaStream(MediaStreamClient* media_stream_client, | |
| 226 media::Demuxer* demuxer, | |
| 227 const base::Closure& destroy_demuxer_cb); | |
| 228 #endif | |
| 229 | |
| 230 // Called when DemuxerStreamPlayer needs to read data from ChunkDemuxer. | |
| 231 void OnReadFromDemuxer(media::DemuxerStream::Type type, bool seek_done); | |
| 232 | |
| 233 protected: | |
| 234 // Helper method to update the playing state. | |
| 235 void UpdatePlayingState(bool is_playing_); | |
| 236 | |
| 237 // Helper methods for posting task for setting states and update WebKit. | |
| 238 void UpdateNetworkState(WebKit::WebMediaPlayer::NetworkState state); | |
| 239 void UpdateReadyState(WebKit::WebMediaPlayer::ReadyState state); | |
| 240 | |
| 241 // Helper method to reestablish the surface texture peer for android | |
| 242 // media player. | |
| 243 void EstablishSurfaceTexturePeer(); | |
| 244 | |
| 245 // Requesting whether the surface texture peer needs to be reestablished. | |
| 246 void SetNeedsEstablishPeer(bool needs_establish_peer); | |
| 247 | |
| 248 void InitializeMediaPlayer(const WebKit::WebURL& url); | |
| 249 | |
| 250 #if defined(GOOGLE_TV) | |
| 251 // Request external surface for out-of-band composition. | |
| 252 void RequestExternalSurface(); | |
| 253 #endif | |
| 254 | |
| 255 private: | |
| 256 void ReallocateVideoFrame(); | |
| 257 void DidLoadMediaInfo(MediaInfoLoader::Status status); | |
| 258 | |
| 259 // Actually do the work for generateKeyRequest/addKey so they can easily | |
| 260 // report results to UMA. | |
| 261 MediaKeyException GenerateKeyRequestInternal( | |
| 262 const WebKit::WebString& key_system, | |
| 263 const unsigned char* init_data, | |
| 264 unsigned init_data_length); | |
| 265 MediaKeyException AddKeyInternal(const WebKit::WebString& key_system, | |
| 266 const unsigned char* key, | |
| 267 unsigned key_length, | |
| 268 const unsigned char* init_data, | |
| 269 unsigned init_data_length, | |
| 270 const WebKit::WebString& session_id); | |
| 271 MediaKeyException CancelKeyRequestInternal( | |
| 272 const WebKit::WebString& key_system, | |
| 273 const WebKit::WebString& session_id); | |
| 274 | |
| 275 WebKit::WebFrame* const frame_; | |
| 276 | |
| 277 WebKit::WebMediaPlayerClient* const client_; | |
| 278 | |
| 279 // |delegate_| is used to notify the browser process of the player status, so | |
| 280 // that the browser process can control screen locks. | |
| 281 // TODO(qinmin): Currently android mediaplayer takes care of the screen | |
| 282 // lock. So this is only used for media source. Will apply this to regular | |
| 283 // media tag once http://crbug.com/247892 is fixed. | |
| 284 base::WeakPtr<WebMediaPlayerDelegate> delegate_; | |
| 285 | |
| 286 // Save the list of buffered time ranges. | |
| 287 WebKit::WebTimeRanges buffered_; | |
| 288 | |
| 289 // Size of the video. | |
| 290 WebKit::WebSize natural_size_; | |
| 291 | |
| 292 // The video frame object used for rendering by the compositor. | |
| 293 scoped_refptr<media::VideoFrame> current_frame_; | |
| 294 | |
| 295 // Message loop for main renderer thread. | |
| 296 const scoped_refptr<base::MessageLoopProxy> main_loop_; | |
| 297 | |
| 298 // URL of the media file to be fetched. | |
| 299 GURL url_; | |
| 300 | |
| 301 // Media duration. | |
| 302 base::TimeDelta duration_; | |
| 303 | |
| 304 // Flag to remember if we have a trusted duration_ value provided by | |
| 305 // MediaSourceDelegate notifying OnDurationChange(). In this case, ignore | |
| 306 // any subsequent duration value passed to OnMediaMetadataChange(). | |
| 307 bool ignore_metadata_duration_change_; | |
| 308 | |
| 309 // The time android media player is trying to seek. | |
| 310 double pending_seek_; | |
| 311 | |
| 312 // Internal seek state. | |
| 313 bool seeking_; | |
| 314 | |
| 315 // Whether loading has progressed since the last call to didLoadingProgress. | |
| 316 mutable bool did_loading_progress_; | |
| 317 | |
| 318 // Manager for managing this object. | |
| 319 WebMediaPlayerManagerAndroid* manager_; | |
| 320 | |
| 321 // Player ID assigned by the |manager_|. | |
| 322 int player_id_; | |
| 323 | |
| 324 // Current player states. | |
| 325 WebKit::WebMediaPlayer::NetworkState network_state_; | |
| 326 WebKit::WebMediaPlayer::ReadyState ready_state_; | |
| 327 | |
| 328 // GL texture ID allocated to the video. | |
| 329 unsigned int texture_id_; | |
| 330 | |
| 331 // Stream texture ID allocated to the video. | |
| 332 unsigned int stream_id_; | |
| 333 | |
| 334 // Whether the mediaplayer is playing. | |
| 335 bool is_playing_; | |
| 336 | |
| 337 // Whether media player needs to re-establish the surface texture peer. | |
| 338 bool needs_establish_peer_; | |
| 339 | |
| 340 // Whether |stream_texture_proxy_| is initialized. | |
| 341 bool stream_texture_proxy_initialized_; | |
| 342 | |
| 343 // Whether the video size info is available. | |
| 344 bool has_size_info_; | |
| 345 | |
| 346 // Whether the video metadata and info are available. | |
| 347 bool has_media_metadata_; | |
| 348 bool has_media_info_; | |
| 349 | |
| 350 // Object for allocating stream textures. | |
| 351 scoped_ptr<StreamTextureFactory> stream_texture_factory_; | |
| 352 | |
| 353 // Object for calling back the compositor thread to repaint the video when a | |
| 354 // frame available. It should be initialized on the compositor thread. | |
| 355 ScopedStreamTextureProxy stream_texture_proxy_; | |
| 356 | |
| 357 // Whether media player needs external surface. | |
| 358 bool needs_external_surface_; | |
| 359 | |
| 360 // A pointer back to the compositor to inform it about state changes. This is | |
| 361 // not NULL while the compositor is actively using this webmediaplayer. | |
| 362 cc::VideoFrameProvider::Client* video_frame_provider_client_; | |
| 363 | |
| 364 scoped_ptr<webkit::WebLayerImpl> video_weblayer_; | |
| 365 | |
| 366 #if defined(GOOGLE_TV) | |
| 367 // A rectangle represents the geometry of video frame, when computed last | |
| 368 // time. | |
| 369 gfx::RectF last_computed_rect_; | |
| 370 | |
| 371 // Media Stream related fields. | |
| 372 media::Demuxer* demuxer_; | |
| 373 base::Closure destroy_demuxer_cb_; | |
| 374 scoped_refptr<MediaStreamAudioRenderer> audio_renderer_; | |
| 375 #endif | |
| 376 | |
| 377 scoped_ptr<MediaSourceDelegate, | |
| 378 MediaSourceDelegate::Destroyer> media_source_delegate_; | |
| 379 | |
| 380 media::MediaPlayerAndroid::SourceType source_type_; | |
| 381 | |
| 382 // Proxy object that delegates method calls on Render Thread. | |
| 383 // This object is created on the Render Thread and is only called in the | |
| 384 // destructor. | |
| 385 WebMediaPlayerProxyAndroid* proxy_; | |
| 386 | |
| 387 // The current playing time. Because the media player is in the browser | |
| 388 // process, it will regularly update the |current_time_| by calling | |
| 389 // OnTimeUpdate(). | |
| 390 double current_time_; | |
| 391 | |
| 392 media::MediaLog* media_log_; | |
| 393 MediaStreamClient* media_stream_client_; | |
| 394 | |
| 395 scoped_ptr<MediaInfoLoader> info_loader_; | |
| 396 | |
| 397 // The currently selected key system. Empty string means that no key system | |
| 398 // has been selected. | |
| 399 WebKit::WebString current_key_system_; | |
| 400 | |
| 401 // Temporary for EME v0.1. In the future the init data type should be passed | |
| 402 // through GenerateKeyRequest() directly from WebKit. | |
| 403 std::string init_data_type_; | |
| 404 | |
| 405 // The decryptor that manages decryption keys and decrypts encrypted frames. | |
| 406 scoped_ptr<ProxyDecryptor> decryptor_; | |
| 407 | |
| 408 DISALLOW_COPY_AND_ASSIGN(WebMediaPlayerAndroid); | |
| 409 }; | |
| 410 | |
| 411 } // namespace webkit_media | |
| 412 | |
| 413 #endif // WEBKIT_RENDERER_MEDIA_ANDROID_WEBMEDIAPLAYER_ANDROID_H_ | |
| OLD | NEW |