Revert of Fix autoplay during prerendering for media elements on Android. (patchset...
[chromium-blink-merge.git] / content / renderer / media / android / webmediaplayer_android.h
bloba10ad3274e80349809d8f78c62ab05ebccd8a2dc
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.
5 #ifndef CONTENT_RENDERER_MEDIA_ANDROID_WEBMEDIAPLAYER_ANDROID_H_
6 #define CONTENT_RENDERER_MEDIA_ANDROID_WEBMEDIAPLAYER_ANDROID_H_
8 #include <jni.h>
9 #include <string>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/threading/thread_checker.h"
17 #include "base/time/default_tick_clock.h"
18 #include "base/time/time.h"
19 #include "cc/layers/video_frame_provider.h"
20 #include "content/common/media/media_player_messages_enums_android.h"
21 #include "content/public/renderer/render_frame_observer.h"
22 #include "content/renderer/media/android/media_info_loader.h"
23 #include "content/renderer/media/android/media_source_delegate.h"
24 #include "content/renderer/media/android/stream_texture_factory.h"
25 #include "gpu/command_buffer/common/mailbox.h"
26 #include "media/base/android/media_player_android.h"
27 #include "media/base/cdm_context.h"
28 #include "media/base/demuxer_stream.h"
29 #include "media/base/media_keys.h"
30 #include "media/base/time_delta_interpolator.h"
31 #include "media/blink/webmediaplayer_util.h"
32 #include "media/cdm/proxy_decryptor.h"
33 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
34 #include "third_party/WebKit/public/platform/WebMediaPlayer.h"
35 #include "third_party/WebKit/public/platform/WebSize.h"
36 #include "third_party/WebKit/public/platform/WebURL.h"
37 #include "third_party/skia/include/core/SkBitmap.h"
38 #include "ui/gfx/geometry/rect_f.h"
40 namespace base {
41 class SingleThreadTaskRunner;
44 namespace blink {
45 class WebContentDecryptionModule;
46 class WebContentDecryptionModuleResult;
47 class WebFrame;
48 class WebMediaPlayerClient;
49 class WebMediaPlayerEncryptedMediaClient;
50 class WebURL;
53 namespace cc_blink {
54 class WebLayerImpl;
57 namespace gpu {
58 struct MailboxHolder;
61 namespace media {
62 class CdmContext;
63 class CdmFactory;
64 class MediaLog;
65 class MediaPermission;
66 class WebContentDecryptionModuleImpl;
67 class WebMediaPlayerDelegate;
70 namespace content {
72 class RendererCdmManager;
73 class RendererMediaPlayerManager;
75 // This class implements blink::WebMediaPlayer by keeping the android
76 // media player in the browser process. It listens to all the status changes
77 // sent from the browser process and sends playback controls to the media
78 // player.
79 class WebMediaPlayerAndroid : public blink::WebMediaPlayer,
80 public cc::VideoFrameProvider,
81 public RenderFrameObserver,
82 public StreamTextureFactoryContextObserver {
83 public:
84 // Construct a WebMediaPlayerAndroid object. This class communicates with the
85 // MediaPlayerAndroid object in the browser process through |proxy|.
86 // TODO(qinmin): |frame| argument is used to determine whether the current
87 // player can enter fullscreen. This logic should probably be moved into
88 // blink, so that enterFullscreen() will not be called if another video is
89 // already in fullscreen.
90 WebMediaPlayerAndroid(
91 blink::WebFrame* frame,
92 blink::WebMediaPlayerClient* client,
93 blink::WebMediaPlayerEncryptedMediaClient* encrypted_client,
94 base::WeakPtr<media::WebMediaPlayerDelegate> delegate,
95 RendererMediaPlayerManager* player_manager,
96 media::CdmFactory* cdm_factory,
97 media::MediaPermission* media_permission,
98 blink::WebContentDecryptionModule* initial_cdm,
99 scoped_refptr<StreamTextureFactory> factory,
100 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
101 media::MediaLog* media_log);
102 virtual ~WebMediaPlayerAndroid();
104 // blink::WebMediaPlayer implementation.
105 virtual bool supportsOverlayFullscreenVideo();
106 virtual void enterFullscreen();
108 // Resource loading.
109 virtual void load(LoadType load_type,
110 const blink::WebURL& url,
111 CORSMode cors_mode);
113 // Playback controls.
114 virtual void play();
115 virtual void pause();
116 virtual void seek(double seconds);
117 virtual bool supportsSave() const;
118 virtual void setRate(double rate);
119 virtual void setVolume(double volume);
120 virtual void setSinkId(const blink::WebString& device_id,
121 media::WebSetSinkIdCB* raw_web_callbacks);
122 virtual void requestRemotePlayback();
123 virtual void requestRemotePlaybackControl();
124 virtual blink::WebTimeRanges buffered() const;
125 virtual blink::WebTimeRanges seekable() const;
127 // Poster image, as defined in the <video> element.
128 virtual void setPoster(const blink::WebURL& poster) override;
130 // Methods for painting.
131 // FIXME: This path "only works" on Android. It is a workaround for the
132 // issue that Skia could not handle Android's GL_TEXTURE_EXTERNAL_OES texture
133 // internally. It should be removed and replaced by the normal paint path.
134 // https://code.google.com/p/skia/issues/detail?id=1189
135 virtual void paint(blink::WebCanvas* canvas,
136 const blink::WebRect& rect,
137 unsigned char alpha,
138 SkXfermode::Mode mode);
140 bool copyVideoTextureToPlatformTexture(
141 blink::WebGraphicsContext3D* web_graphics_context,
142 unsigned int texture,
143 unsigned int internal_format,
144 unsigned int type,
145 bool premultiply_alpha,
146 bool flip_y) override;
148 // True if the loaded media has a playable video/audio track.
149 virtual bool hasVideo() const;
150 virtual bool hasAudio() const;
152 virtual bool isRemote() const;
154 // Dimensions of the video.
155 virtual blink::WebSize naturalSize() const;
157 // Getters of playback state.
158 virtual bool paused() const;
159 virtual bool seeking() const;
160 virtual double duration() const;
161 virtual double timelineOffset() const;
162 virtual double currentTime() const;
164 virtual bool didLoadingProgress();
166 // Internal states of loading and network.
167 virtual blink::WebMediaPlayer::NetworkState networkState() const;
168 virtual blink::WebMediaPlayer::ReadyState readyState() const;
170 virtual bool hasSingleSecurityOrigin() const;
171 virtual bool didPassCORSAccessCheck() const;
173 virtual double mediaTimeForTimeValue(double timeValue) const;
175 // Provide statistics.
176 virtual unsigned decodedFrameCount() const;
177 virtual unsigned droppedFrameCount() const;
178 virtual unsigned audioDecodedByteCount() const;
179 virtual unsigned videoDecodedByteCount() const;
181 // cc::VideoFrameProvider implementation. These methods are running on the
182 // compositor thread.
183 void SetVideoFrameProviderClient(
184 cc::VideoFrameProvider::Client* client) override;
185 bool UpdateCurrentFrame(base::TimeTicks deadline_min,
186 base::TimeTicks deadline_max) override;
187 bool HasCurrentFrame() override;
188 scoped_refptr<media::VideoFrame> GetCurrentFrame() override;
189 void PutCurrentFrame() override;
191 // Media player callback handlers.
192 void OnMediaMetadataChanged(base::TimeDelta duration, int width,
193 int height, bool success);
194 void OnPlaybackComplete();
195 void OnBufferingUpdate(int percentage);
196 void OnSeekRequest(const base::TimeDelta& time_to_seek);
197 void OnSeekComplete(const base::TimeDelta& current_time);
198 void OnMediaError(int error_type);
199 void OnVideoSizeChanged(int width, int height);
200 void OnDurationChanged(const base::TimeDelta& duration);
202 // Called to update the current time.
203 void OnTimeUpdate(base::TimeDelta current_timestamp,
204 base::TimeTicks current_time_ticks);
206 // Functions called when media player status changes.
207 void OnConnectedToRemoteDevice(const std::string& remote_playback_message);
208 void OnDisconnectedFromRemoteDevice();
209 void OnDidExitFullscreen();
210 void OnMediaPlayerPlay();
211 void OnMediaPlayerPause();
212 void OnRemoteRouteAvailabilityChanged(bool routes_available);
214 // StreamTextureFactoryContextObserver implementation.
215 void ResetStreamTextureProxy() override;
217 // Called when the player is released.
218 virtual void OnPlayerReleased();
220 // This function is called by the RendererMediaPlayerManager to pause the
221 // video and release the media player and surface texture when we switch tabs.
222 // However, the actual GlTexture is not released to keep the video screenshot.
223 virtual void ReleaseMediaResources();
225 // RenderFrameObserver implementation.
226 void OnDestruct() override;
228 #if defined(VIDEO_HOLE)
229 // Calculate the boundary rectangle of the media player (i.e. location and
230 // size of the video frame).
231 // Returns true if the geometry has been changed since the last call.
232 bool UpdateBoundaryRectangle();
234 const gfx::RectF GetBoundaryRectangle();
235 #endif // defined(VIDEO_HOLE)
237 virtual MediaKeyException generateKeyRequest(
238 const blink::WebString& key_system,
239 const unsigned char* init_data,
240 unsigned init_data_length);
241 virtual MediaKeyException addKey(
242 const blink::WebString& key_system,
243 const unsigned char* key,
244 unsigned key_length,
245 const unsigned char* init_data,
246 unsigned init_data_length,
247 const blink::WebString& session_id);
248 virtual MediaKeyException cancelKeyRequest(
249 const blink::WebString& key_system,
250 const blink::WebString& session_id);
252 virtual void setContentDecryptionModule(
253 blink::WebContentDecryptionModule* cdm,
254 blink::WebContentDecryptionModuleResult result);
256 void OnKeyAdded(const std::string& session_id);
257 void OnKeyError(const std::string& session_id,
258 media::MediaKeys::KeyError error_code,
259 uint32 system_code);
260 void OnKeyMessage(const std::string& session_id,
261 const std::vector<uint8>& message,
262 const GURL& destination_url);
264 void OnMediaSourceOpened(blink::WebMediaSource* web_media_source);
266 void OnEncryptedMediaInitData(media::EmeInitDataType init_data_type,
267 const std::vector<uint8>& init_data);
269 // Called when a decoder detects that the key needed to decrypt the stream
270 // is not available.
271 void OnWaitingForDecryptionKey();
273 protected:
274 // Helper method to update the playing state.
275 void UpdatePlayingState(bool is_playing_);
277 // Helper methods for posting task for setting states and update WebKit.
278 void UpdateNetworkState(blink::WebMediaPlayer::NetworkState state);
279 void UpdateReadyState(blink::WebMediaPlayer::ReadyState state);
280 void TryCreateStreamTextureProxyIfNeeded();
281 void DoCreateStreamTexture();
283 // Helper method to reestablish the surface texture peer for android
284 // media player.
285 void EstablishSurfaceTexturePeer();
287 // Requesting whether the surface texture peer needs to be reestablished.
288 void SetNeedsEstablishPeer(bool needs_establish_peer);
290 private:
291 void InitializePlayer(const GURL& url,
292 const GURL& first_party_for_cookies,
293 bool allowed_stored_credentials,
294 int demuxer_client_id);
295 void Pause(bool is_media_related_action);
296 void DrawRemotePlaybackText(const std::string& remote_playback_message);
297 void ReallocateVideoFrame();
298 void SetCurrentFrameInternal(scoped_refptr<media::VideoFrame>& frame);
299 void RemoveSurfaceTextureAndProxy();
300 void DidLoadMediaInfo(MediaInfoLoader::Status status,
301 const GURL& redirected_url,
302 const GURL& first_party_for_cookies,
303 bool allow_stored_credentials);
304 bool IsKeySystemSupported(const std::string& key_system);
305 bool IsLocalResource();
307 // Actually do the work for generateKeyRequest/addKey so they can easily
308 // report results to UMA.
309 MediaKeyException GenerateKeyRequestInternal(const std::string& key_system,
310 const unsigned char* init_data,
311 unsigned init_data_length);
312 MediaKeyException AddKeyInternal(const std::string& key_system,
313 const unsigned char* key,
314 unsigned key_length,
315 const unsigned char* init_data,
316 unsigned init_data_length,
317 const std::string& session_id);
318 MediaKeyException CancelKeyRequestInternal(const std::string& key_system,
319 const std::string& session_id);
321 // Called when |cdm_context| is ready.
322 void OnCdmContextReady(media::CdmContext* cdm_context);
324 // Sets the CDM. Should only be called when |is_player_initialized_| is true
325 // and a new non-null |cdm_context_| is available. Fires |cdm_attached_cb_|
326 // with the result after the CDM is attached.
327 void SetCdmInternal(const media::CdmAttachedCB& cdm_attached_cb);
329 // Requests that this object notifies when a decryptor is ready through the
330 // |decryptor_ready_cb| provided.
331 // If |decryptor_ready_cb| is null, the existing callback will be fired with
332 // NULL immediately and reset.
333 void SetDecryptorReadyCB(const media::DecryptorReadyCB& decryptor_ready_cb);
335 // Called when the ContentDecryptionModule has been attached to the
336 // pipeline/decoders.
337 void ContentDecryptionModuleAttached(
338 blink::WebContentDecryptionModuleResult result,
339 bool success);
341 bool IsHLSStream() const;
342 // Report whether the loaded url, after following redirects, points to a HLS
343 // playlist, and record the origin of the player.
344 void ReportHLSMetrics() const;
346 blink::WebFrame* const frame_;
348 blink::WebMediaPlayerClient* const client_;
349 blink::WebMediaPlayerEncryptedMediaClient* const encrypted_client_;
351 // |delegate_| is used to notify the browser process of the player status, so
352 // that the browser process can control screen locks.
353 // TODO(qinmin): Currently android mediaplayer takes care of the screen
354 // lock. So this is only used for media source. Will apply this to regular
355 // media tag once http://crbug.com/247892 is fixed.
356 base::WeakPtr<media::WebMediaPlayerDelegate> delegate_;
358 // Save the list of buffered time ranges.
359 blink::WebTimeRanges buffered_;
361 // Size of the video.
362 blink::WebSize natural_size_;
364 // Size that has been sent to StreamTexture.
365 blink::WebSize cached_stream_texture_size_;
367 // The video frame object used for rendering by the compositor.
368 scoped_refptr<media::VideoFrame> current_frame_;
369 base::Lock current_frame_lock_;
371 base::ThreadChecker main_thread_checker_;
373 // Message loop for media thread.
374 const scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_;
376 // URL of the media file to be fetched.
377 GURL url_;
379 // URL of the media file after |media_info_loader_| resolves all the
380 // redirections.
381 GURL redirected_url_;
383 // Media duration.
384 base::TimeDelta duration_;
386 // Flag to remember if we have a trusted duration_ value provided by
387 // MediaSourceDelegate notifying OnDurationChanged(). In this case, ignore
388 // any subsequent duration value passed to OnMediaMetadataChange().
389 bool ignore_metadata_duration_change_;
391 // Seek gets pending if another seek is in progress. Only last pending seek
392 // will have effect.
393 bool pending_seek_;
394 base::TimeDelta pending_seek_time_;
396 // Internal seek state.
397 bool seeking_;
398 base::TimeDelta seek_time_;
400 // Whether loading has progressed since the last call to didLoadingProgress.
401 bool did_loading_progress_;
403 // Manages this object and delegates player calls to the browser process.
404 // Owned by RenderFrameImpl.
405 RendererMediaPlayerManager* const player_manager_;
407 // TODO(xhwang): Remove |cdm_factory_| when prefixed EME is deprecated. See
408 // http://crbug.com/249976
409 media::CdmFactory* const cdm_factory_;
411 media::MediaPermission* media_permission_;
413 // Player ID assigned by the |player_manager_|.
414 int player_id_;
416 // Current player states.
417 blink::WebMediaPlayer::NetworkState network_state_;
418 blink::WebMediaPlayer::ReadyState ready_state_;
420 // GL texture ID allocated to the video.
421 unsigned int texture_id_;
423 // GL texture mailbox for texture_id_ to provide in the VideoFrame, and sync
424 // point for when the mailbox was produced.
425 gpu::Mailbox texture_mailbox_;
427 // Stream texture ID allocated to the video.
428 unsigned int stream_id_;
430 // Whether the media player has been initialized.
431 bool is_player_initialized_;
433 // Whether the media player is playing.
434 bool is_playing_;
436 // Whether media player needs to re-establish the surface texture peer.
437 bool needs_establish_peer_;
439 // Whether the video size info is available.
440 bool has_size_info_;
442 const scoped_refptr<base::SingleThreadTaskRunner> compositor_loop_;
444 // Object for allocating stream textures.
445 scoped_refptr<StreamTextureFactory> stream_texture_factory_;
447 // Object for calling back the compositor thread to repaint the video when a
448 // frame available. It should be initialized on the compositor thread.
449 // Accessed on main thread and on compositor thread when main thread is
450 // blocked.
451 ScopedStreamTextureProxy stream_texture_proxy_;
453 // Whether media player needs external surface.
454 // Only used for the VIDEO_HOLE logic.
455 bool needs_external_surface_;
457 // Whether the player is in fullscreen.
458 bool is_fullscreen_;
460 // A pointer back to the compositor to inform it about state changes. This is
461 // not NULL while the compositor is actively using this webmediaplayer.
462 // Accessed on main thread and on compositor thread when main thread is
463 // blocked.
464 cc::VideoFrameProvider::Client* video_frame_provider_client_;
466 scoped_ptr<cc_blink::WebLayerImpl> video_weblayer_;
468 #if defined(VIDEO_HOLE)
469 // A rectangle represents the geometry of video frame, when computed last
470 // time.
471 gfx::RectF last_computed_rect_;
473 // Whether to use the video overlay for all embedded video.
474 // True only for testing.
475 bool force_use_overlay_embedded_video_;
476 #endif // defined(VIDEO_HOLE)
478 MediaPlayerHostMsg_Initialize_Type player_type_;
480 // Whether the browser is currently connected to a remote media player.
481 bool is_remote_;
483 scoped_refptr<media::MediaLog> media_log_;
485 scoped_ptr<MediaInfoLoader> info_loader_;
487 // The currently selected key system. Empty string means that no key system
488 // has been selected.
489 std::string current_key_system_;
491 // Temporary for EME v0.1. Not needed for unprefixed EME, and can be removed
492 // when prefixed EME is removed.
493 media::EmeInitDataType init_data_type_;
495 // Manages decryption keys and decrypts encrypted frames.
496 scoped_ptr<media::ProxyDecryptor> proxy_decryptor_;
498 // Non-owned pointer to the CdmContext. Updated in the constructor,
499 // generateKeyRequest() or setContentDecryptionModule().
500 media::CdmContext* cdm_context_;
502 // This is only Used by Clear Key key system implementation, where a renderer
503 // side CDM will be used. This is similar to WebMediaPlayerImpl. For other key
504 // systems, a browser side CDM will be used and we set CDM by calling
505 // player_manager_->SetCdm() directly.
506 media::DecryptorReadyCB decryptor_ready_cb_;
508 SkBitmap bitmap_;
510 // Whether stored credentials are allowed to be passed to the server.
511 bool allow_stored_credentials_;
513 // Whether the resource is local.
514 bool is_local_resource_;
516 // base::TickClock used by |interpolator_|.
517 base::DefaultTickClock default_tick_clock_;
519 // Tracks the most recent media time update and provides interpolated values
520 // as playback progresses.
521 media::TimeDeltaInterpolator interpolator_;
523 scoped_ptr<MediaSourceDelegate> media_source_delegate_;
525 // NOTE: Weak pointers must be invalidated before all other member variables.
526 base::WeakPtrFactory<WebMediaPlayerAndroid> weak_factory_;
528 DISALLOW_COPY_AND_ASSIGN(WebMediaPlayerAndroid);
531 } // namespace content
533 #endif // CONTENT_RENDERER_MEDIA_ANDROID_WEBMEDIAPLAYER_ANDROID_H_