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_
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/time/time.h"
17 #include "cc/layers/video_frame_provider.h"
18 #include "content/common/media/media_player_messages_enums_android.h"
19 #include "content/public/renderer/render_frame_observer.h"
20 #include "content/renderer/media/android/media_info_loader.h"
21 #include "content/renderer/media/android/media_source_delegate.h"
22 #include "content/renderer/media/android/stream_texture_factory.h"
23 #include "content/renderer/media/crypto/proxy_decryptor.h"
24 #include "gpu/command_buffer/common/mailbox.h"
25 #include "media/base/android/media_player_android.h"
26 #include "media/base/demuxer_stream.h"
27 #include "media/base/media_keys.h"
28 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
29 #include "third_party/WebKit/public/platform/WebMediaPlayer.h"
30 #include "third_party/WebKit/public/platform/WebSize.h"
31 #include "third_party/WebKit/public/platform/WebURL.h"
32 #include "ui/gfx/rect_f.h"
35 class MessageLoopProxy;
39 class WebContentDecryptionModule;
53 class RendererCdmManager;
54 class RendererMediaPlayerManager;
55 class WebContentDecryptionModuleImpl;
57 class WebMediaPlayerDelegate;
59 // This class implements blink::WebMediaPlayer by keeping the android
60 // media player in the browser process. It listens to all the status changes
61 // sent from the browser process and sends playback controls to the media
63 class WebMediaPlayerAndroid : public blink::WebMediaPlayer,
64 public cc::VideoFrameProvider,
65 public RenderFrameObserver {
67 // Construct a WebMediaPlayerAndroid object. This class communicates with the
68 // MediaPlayerAndroid object in the browser process through |proxy|.
69 // TODO(qinmin): |frame| argument is used to determine whether the current
70 // player can enter fullscreen. This logic should probably be moved into
71 // blink, so that enterFullscreen() will not be called if another video is
72 // already in fullscreen.
73 WebMediaPlayerAndroid(blink::WebFrame* frame,
74 blink::WebMediaPlayerClient* client,
75 base::WeakPtr<WebMediaPlayerDelegate> delegate,
76 RendererMediaPlayerManager* player_manager,
77 RendererCdmManager* cdm_manager,
78 scoped_refptr<StreamTextureFactory> factory,
79 const scoped_refptr<base::MessageLoopProxy>& media_loop,
80 media::MediaLog* media_log);
81 virtual ~WebMediaPlayerAndroid();
83 // blink::WebMediaPlayer implementation.
84 virtual void enterFullscreen();
85 virtual void exitFullscreen();
86 virtual bool canEnterFullscreen() const;
89 virtual void load(LoadType load_type,
90 const blink::WebURL& url,
96 virtual void seek(double seconds);
97 virtual bool supportsSave() const;
98 virtual void setRate(double rate);
99 virtual void setVolume(double volume);
100 virtual blink::WebTimeRanges buffered() const;
101 virtual double maxTimeSeekable() const;
103 // Poster image, as defined in the <video> element.
104 virtual void setPoster(const blink::WebURL& poster) OVERRIDE;
106 // Methods for painting.
107 virtual void paint(blink::WebCanvas* canvas,
108 const blink::WebRect& rect,
109 unsigned char alpha);
111 virtual bool copyVideoTextureToPlatformTexture(
112 blink::WebGraphicsContext3D* web_graphics_context,
113 unsigned int texture,
115 unsigned int internal_format,
117 bool premultiply_alpha,
120 // True if the loaded media has a playable video/audio track.
121 virtual bool hasVideo() const;
122 virtual bool hasAudio() const;
124 // Dimensions of the video.
125 virtual blink::WebSize naturalSize() const;
127 // Getters of playback state.
128 virtual bool paused() const;
129 virtual bool seeking() const;
130 virtual double duration() const;
131 virtual double timelineOffset() const;
132 virtual double currentTime() const;
134 virtual bool didLoadingProgress();
136 // Internal states of loading and network.
137 virtual blink::WebMediaPlayer::NetworkState networkState() const;
138 virtual blink::WebMediaPlayer::ReadyState readyState() const;
140 virtual bool hasSingleSecurityOrigin() const;
141 virtual bool didPassCORSAccessCheck() const;
143 virtual double mediaTimeForTimeValue(double timeValue) const;
145 // Provide statistics.
146 virtual unsigned decodedFrameCount() const;
147 virtual unsigned droppedFrameCount() const;
148 virtual unsigned audioDecodedByteCount() const;
149 virtual unsigned videoDecodedByteCount() const;
151 // cc::VideoFrameProvider implementation. These methods are running on the
152 // compositor thread.
153 virtual void SetVideoFrameProviderClient(
154 cc::VideoFrameProvider::Client* client) OVERRIDE;
155 virtual scoped_refptr<media::VideoFrame> GetCurrentFrame() OVERRIDE;
156 virtual void PutCurrentFrame(const scoped_refptr<media::VideoFrame>& frame)
159 // Media player callback handlers.
160 void OnMediaMetadataChanged(const base::TimeDelta& duration, int width,
161 int height, bool success);
162 void OnPlaybackComplete();
163 void OnBufferingUpdate(int percentage);
164 void OnSeekRequest(const base::TimeDelta& time_to_seek);
165 void OnSeekComplete(const base::TimeDelta& current_time);
166 void OnMediaError(int error_type);
167 void OnVideoSizeChanged(int width, int height);
168 void OnDurationChanged(const base::TimeDelta& duration);
170 // Called to update the current time.
171 void OnTimeUpdate(const base::TimeDelta& current_time);
173 // Functions called when media player status changes.
174 void OnConnectedToRemoteDevice(const std::string& remote_playback_message);
175 void OnDisconnectedFromRemoteDevice();
176 void OnDidEnterFullscreen();
177 void OnDidExitFullscreen();
178 void OnMediaPlayerPlay();
179 void OnMediaPlayerPause();
180 void OnRequestFullscreen();
182 // Called when the player is released.
183 virtual void OnPlayerReleased();
185 // This function is called by the RendererMediaPlayerManager to pause the
186 // video and release the media player and surface texture when we switch tabs.
187 // However, the actual GlTexture is not released to keep the video screenshot.
188 virtual void ReleaseMediaResources();
190 // RenderFrameObserver implementation.
191 virtual void OnDestruct() OVERRIDE;
193 #if defined(VIDEO_HOLE)
194 // Calculate the boundary rectangle of the media player (i.e. location and
195 // size of the video frame).
196 // Returns true if the geometry has been changed since the last call.
197 bool UpdateBoundaryRectangle();
199 const gfx::RectF GetBoundaryRectangle();
200 #endif // defined(VIDEO_HOLE)
202 virtual MediaKeyException generateKeyRequest(
203 const blink::WebString& key_system,
204 const unsigned char* init_data,
205 unsigned init_data_length);
206 virtual MediaKeyException addKey(
207 const blink::WebString& key_system,
208 const unsigned char* key,
210 const unsigned char* init_data,
211 unsigned init_data_length,
212 const blink::WebString& session_id);
213 virtual MediaKeyException cancelKeyRequest(
214 const blink::WebString& key_system,
215 const blink::WebString& session_id);
216 virtual void setContentDecryptionModule(
217 blink::WebContentDecryptionModule* cdm);
219 void OnKeyAdded(const std::string& session_id);
220 void OnKeyError(const std::string& session_id,
221 media::MediaKeys::KeyError error_code,
223 void OnKeyMessage(const std::string& session_id,
224 const std::vector<uint8>& message,
225 const GURL& destination_url);
227 void OnMediaSourceOpened(blink::WebMediaSource* web_media_source);
229 void OnNeedKey(const std::string& type,
230 const std::vector<uint8>& init_data);
232 // TODO(xhwang): Implement WebMediaPlayer::setContentDecryptionModule().
233 // See: http://crbug.com/224786
236 // Helper method to update the playing state.
237 void UpdatePlayingState(bool is_playing_);
239 // Helper methods for posting task for setting states and update WebKit.
240 void UpdateNetworkState(blink::WebMediaPlayer::NetworkState state);
241 void UpdateReadyState(blink::WebMediaPlayer::ReadyState state);
242 void TryCreateStreamTextureProxyIfNeeded();
243 void DoCreateStreamTexture();
245 // Helper method to reestablish the surface texture peer for android
247 void EstablishSurfaceTexturePeer();
249 // Requesting whether the surface texture peer needs to be reestablished.
250 void SetNeedsEstablishPeer(bool needs_establish_peer);
253 void Pause(bool is_media_related_action);
254 void DrawRemotePlaybackText(const std::string& remote_playback_message);
255 void ReallocateVideoFrame();
256 void SetCurrentFrameInternal(scoped_refptr<media::VideoFrame>& frame);
257 void DidLoadMediaInfo(MediaInfoLoader::Status status);
258 bool IsKeySystemSupported(const std::string& key_system);
260 // Actually do the work for generateKeyRequest/addKey so they can easily
261 // report results to UMA.
262 MediaKeyException GenerateKeyRequestInternal(const std::string& key_system,
263 const unsigned char* init_data,
264 unsigned init_data_length);
265 MediaKeyException AddKeyInternal(const std::string& key_system,
266 const unsigned char* key,
268 const unsigned char* init_data,
269 unsigned init_data_length,
270 const std::string& session_id);
271 MediaKeyException CancelKeyRequestInternal(const std::string& key_system,
272 const std::string& session_id);
274 // Requests that this object notifies when a decryptor is ready through the
275 // |decryptor_ready_cb| provided.
276 // If |decryptor_ready_cb| is null, the existing callback will be fired with
277 // NULL immediately and reset.
278 void SetDecryptorReadyCB(const media::DecryptorReadyCB& decryptor_ready_cb);
280 blink::WebFrame* const frame_;
282 blink::WebMediaPlayerClient* const client_;
284 // |delegate_| is used to notify the browser process of the player status, so
285 // that the browser process can control screen locks.
286 // TODO(qinmin): Currently android mediaplayer takes care of the screen
287 // lock. So this is only used for media source. Will apply this to regular
288 // media tag once http://crbug.com/247892 is fixed.
289 base::WeakPtr<WebMediaPlayerDelegate> delegate_;
291 // Save the list of buffered time ranges.
292 blink::WebTimeRanges buffered_;
294 // Size of the video.
295 blink::WebSize natural_size_;
297 // Size that has been sent to StreamTexture.
298 blink::WebSize cached_stream_texture_size_;
300 // The video frame object used for rendering by the compositor.
301 scoped_refptr<media::VideoFrame> current_frame_;
302 base::Lock current_frame_lock_;
304 base::ThreadChecker main_thread_checker_;
306 // Message loop for media thread.
307 const scoped_refptr<base::MessageLoopProxy> media_loop_;
309 // URL of the media file to be fetched.
313 base::TimeDelta duration_;
315 // Flag to remember if we have a trusted duration_ value provided by
316 // MediaSourceDelegate notifying OnDurationChanged(). In this case, ignore
317 // any subsequent duration value passed to OnMediaMetadataChange().
318 bool ignore_metadata_duration_change_;
320 // Seek gets pending if another seek is in progress. Only last pending seek
323 base::TimeDelta pending_seek_time_;
325 // Internal seek state.
327 base::TimeDelta seek_time_;
329 // Whether loading has progressed since the last call to didLoadingProgress.
330 bool did_loading_progress_;
332 // Manages this object and delegates player calls to the browser process.
333 // Owned by RenderFrameImpl.
334 RendererMediaPlayerManager* player_manager_;
336 // Delegates EME calls to the browser process. Owned by RenderFrameImpl.
337 // TODO(xhwang): Remove |cdm_manager_| when prefixed EME is deprecated. See
338 // http://crbug.com/249976
339 RendererCdmManager* cdm_manager_;
341 // Player ID assigned by the |player_manager_|.
344 // Current player states.
345 blink::WebMediaPlayer::NetworkState network_state_;
346 blink::WebMediaPlayer::ReadyState ready_state_;
348 // GL texture ID allocated to the video.
349 unsigned int texture_id_;
351 // GL texture mailbox for texture_id_ to provide in the VideoFrame, and sync
352 // point for when the mailbox was produced.
353 gpu::Mailbox texture_mailbox_;
355 // Stream texture ID allocated to the video.
356 unsigned int stream_id_;
358 // Whether the mediaplayer is playing.
361 // Whether the mediaplayer has already started playing.
362 bool playing_started_;
364 // Whether media player needs to re-establish the surface texture peer.
365 bool needs_establish_peer_;
367 // Whether |stream_texture_proxy_| is initialized.
368 bool stream_texture_proxy_initialized_;
370 // Whether the video size info is available.
373 // Whether the video metadata and info are available.
374 bool has_media_metadata_;
375 bool has_media_info_;
377 // Object for allocating stream textures.
378 scoped_refptr<StreamTextureFactory> stream_texture_factory_;
380 // Object for calling back the compositor thread to repaint the video when a
381 // frame available. It should be initialized on the compositor thread.
382 ScopedStreamTextureProxy stream_texture_proxy_;
384 // Whether media player needs external surface.
385 // Only used for the VIDEO_HOLE logic.
386 bool needs_external_surface_;
388 // A pointer back to the compositor to inform it about state changes. This is
389 // not NULL while the compositor is actively using this webmediaplayer.
390 cc::VideoFrameProvider::Client* video_frame_provider_client_;
392 scoped_ptr<WebLayerImpl> video_weblayer_;
394 #if defined(VIDEO_HOLE)
395 // A rectangle represents the geometry of video frame, when computed last
397 gfx::RectF last_computed_rect_;
399 // Whether to use the video overlay for all embedded video.
400 // True only for testing.
401 bool force_use_overlay_embedded_video_;
402 #endif // defined(VIDEO_HOLE)
404 scoped_ptr<MediaSourceDelegate,
405 MediaSourceDelegate::Destroyer> media_source_delegate_;
407 // Internal pending playback state.
408 // Store a playback request that cannot be started immediately.
409 bool pending_playback_;
411 MediaPlayerHostMsg_Initialize_Type player_type_;
413 // The current playing time. Because the media player is in the browser
414 // process, it will regularly update the |current_time_| by calling
416 double current_time_;
418 // Whether the browser is currently connected to a remote media player.
421 media::MediaLog* media_log_;
423 scoped_ptr<MediaInfoLoader> info_loader_;
425 // The currently selected key system. Empty string means that no key system
426 // has been selected.
427 std::string current_key_system_;
429 // Temporary for EME v0.1. In the future the init data type should be passed
430 // through GenerateKeyRequest() directly from WebKit.
431 std::string init_data_type_;
433 // Manages decryption keys and decrypts encrypted frames.
434 scoped_ptr<ProxyDecryptor> proxy_decryptor_;
436 // Non-owned pointer to the CDM. Updated via calls to
437 // setContentDecryptionModule().
438 WebContentDecryptionModuleImpl* web_cdm_;
440 // This is only Used by Clear Key key system implementation, where a renderer
441 // side CDM will be used. This is similar to WebMediaPlayerImpl. For other key
442 // systems, a browser side CDM will be used and we set CDM by calling
443 // player_manager_->SetCdm() directly.
444 media::DecryptorReadyCB decryptor_ready_cb_;
446 // NOTE: Weak pointers must be invalidated before all other member variables.
447 base::WeakPtrFactory<WebMediaPlayerAndroid> weak_factory_;
449 DISALLOW_COPY_AND_ASSIGN(WebMediaPlayerAndroid);
452 } // namespace content
454 #endif // CONTENT_RENDERER_MEDIA_ANDROID_WEBMEDIAPLAYER_ANDROID_H_