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/message_loop/message_loop.h"
17 #include "base/time/time.h"
18 #include "cc/layers/video_frame_provider.h"
19 #include "content/common/media/media_player_messages_enums_android.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_android.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/WebSize.h"
30 #include "third_party/WebKit/public/platform/WebURL.h"
31 #include "third_party/WebKit/public/web/WebMediaPlayer.h"
32 #include "ui/gfx/rect_f.h"
48 class WebMediaPlayerDelegate;
49 class RendererMediaPlayerManager;
51 #if defined(GOOGLE_TV)
52 class MediaStreamAudioRenderer;
53 class MediaStreamClient;
56 // This class implements WebKit::WebMediaPlayer by keeping the android
57 // media player in the browser process. It listens to all the status changes
58 // sent from the browser process and sends playback controls to the media
60 class WebMediaPlayerAndroid
61 : public WebKit::WebMediaPlayer,
62 public cc::VideoFrameProvider,
63 public base::MessageLoop::DestructionObserver,
64 public base::SupportsWeakPtr<WebMediaPlayerAndroid> {
66 // Construct a WebMediaPlayerAndroid object. This class communicates
67 // with the MediaPlayerAndroid object in the browser process through
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(
74 WebKit::WebFrame* frame,
75 WebKit::WebMediaPlayerClient* client,
76 base::WeakPtr<WebMediaPlayerDelegate> delegate,
77 RendererMediaPlayerManager* manager,
78 StreamTextureFactory* factory,
79 const scoped_refptr<base::MessageLoopProxy>& media_loop,
80 media::MediaLog* media_log);
81 virtual ~WebMediaPlayerAndroid();
83 // WebKit::WebMediaPlayer implementation.
84 virtual void enterFullscreen();
85 virtual void exitFullscreen();
86 virtual bool canEnterFullscreen() const;
89 virtual void load(LoadType load_type,
90 const WebKit::WebURL& url,
91 CORSMode cors_mode) OVERRIDE;
96 virtual void pause(bool is_media_related_action);
97 virtual void seek(double seconds);
98 virtual bool supportsFullscreen() const;
99 virtual bool supportsSave() const;
100 virtual void setRate(double rate);
101 virtual void setVolume(double volume);
102 virtual const WebKit::WebTimeRanges& buffered();
103 virtual double maxTimeSeekable() const;
105 // Methods for painting.
106 virtual void paint(WebKit::WebCanvas* canvas,
107 const WebKit::WebRect& rect,
108 unsigned char alpha);
110 virtual bool copyVideoTextureToPlatformTexture(
111 WebKit::WebGraphicsContext3D* web_graphics_context,
112 unsigned int texture,
114 unsigned int internal_format,
116 bool premultiply_alpha,
119 // True if the loaded media has a playable video/audio track.
120 virtual bool hasVideo() const;
121 virtual bool hasAudio() const;
123 // Dimensions of the video.
124 virtual WebKit::WebSize naturalSize() const;
126 // Getters of playback state.
127 virtual bool paused() const;
128 virtual bool seeking() const;
129 virtual double duration() const;
130 virtual double currentTime() const;
132 virtual bool didLoadingProgress() const;
134 // Internal states of loading and network.
135 virtual WebKit::WebMediaPlayer::NetworkState networkState() const;
136 virtual WebKit::WebMediaPlayer::ReadyState readyState() const;
138 virtual bool hasSingleSecurityOrigin() const;
139 virtual bool didPassCORSAccessCheck() const;
141 virtual double mediaTimeForTimeValue(double timeValue) const;
143 // Provide statistics.
144 virtual unsigned decodedFrameCount() const;
145 virtual unsigned droppedFrameCount() const;
146 virtual unsigned audioDecodedByteCount() const;
147 virtual unsigned videoDecodedByteCount() const;
149 // cc::VideoFrameProvider implementation. These methods are running on the
150 // compositor thread.
151 virtual void SetVideoFrameProviderClient(
152 cc::VideoFrameProvider::Client* client) OVERRIDE;
153 virtual scoped_refptr<media::VideoFrame> GetCurrentFrame() OVERRIDE;
154 virtual void PutCurrentFrame(const scoped_refptr<media::VideoFrame>& frame)
157 // Media player callback handlers.
158 void OnMediaMetadataChanged(const base::TimeDelta& duration, int width,
159 int height, bool success);
160 void OnPlaybackComplete();
161 void OnBufferingUpdate(int percentage);
162 void OnSeekRequest(const base::TimeDelta& time_to_seek);
163 void OnSeekComplete(const base::TimeDelta& current_time);
164 void OnMediaError(int error_type);
165 void OnVideoSizeChanged(int width, int height);
166 void OnDurationChanged(const base::TimeDelta& duration);
168 // Called to update the current time.
169 void OnTimeUpdate(const base::TimeDelta& current_time);
171 // Functions called when media player status changes.
172 void OnConnectedToRemoteDevice();
173 void OnDisconnectedFromRemoteDevice();
174 void OnDidEnterFullscreen();
175 void OnDidExitFullscreen();
176 void OnMediaPlayerPlay();
177 void OnMediaPlayerPause();
179 // Called when the player is released.
180 virtual void OnPlayerReleased();
182 // This function is called by the RendererMediaPlayerManager to pause the
183 // video and release the media player and surface texture when we switch tabs.
184 // However, the actual GlTexture is not released to keep the video screenshot.
185 virtual void ReleaseMediaResources();
187 // Method inherited from DestructionObserver.
188 virtual void WillDestroyCurrentMessageLoop() OVERRIDE;
190 // Detach the player from its manager.
193 #if defined(GOOGLE_TV)
194 // Retrieve geometry of the media player (i.e. location and size of the video
195 // frame) if changed. Returns true only if the geometry has been changed since
197 bool RetrieveGeometryChange(gfx::RectF* rect);
200 virtual MediaKeyException generateKeyRequest(
201 const WebKit::WebString& key_system,
202 const unsigned char* init_data,
203 unsigned init_data_length) OVERRIDE;
204 virtual MediaKeyException addKey(
205 const WebKit::WebString& key_system,
206 const unsigned char* key,
208 const unsigned char* init_data,
209 unsigned init_data_length,
210 const WebKit::WebString& session_id) OVERRIDE;
211 virtual MediaKeyException cancelKeyRequest(
212 const WebKit::WebString& key_system,
213 const WebKit::WebString& session_id) OVERRIDE;
215 void OnKeyAdded(const std::string& session_id);
216 void OnKeyError(const std::string& session_id,
217 media::MediaKeys::KeyError error_code,
219 void OnKeyMessage(const std::string& session_id,
220 const std::vector<uint8>& message,
221 const std::string& destination_url);
223 void OnMediaSourceOpened(WebKit::WebMediaSource* web_media_source);
225 void OnNeedKey(const std::string& type,
226 const std::vector<uint8>& init_data);
228 #if defined(GOOGLE_TV)
229 bool InjectMediaStream(MediaStreamClient* media_stream_client,
230 media::Demuxer* demuxer,
231 const base::Closure& destroy_demuxer_cb);
234 // Can be called on any thread.
235 static void OnReleaseRemotePlaybackTexture(
236 const scoped_refptr<base::MessageLoopProxy>& main_loop,
237 const base::WeakPtr<WebMediaPlayerAndroid>& player,
241 // Helper method to update the playing state.
242 void UpdatePlayingState(bool is_playing_);
244 // Helper methods for posting task for setting states and update WebKit.
245 void UpdateNetworkState(WebKit::WebMediaPlayer::NetworkState state);
246 void UpdateReadyState(WebKit::WebMediaPlayer::ReadyState state);
247 void TryCreateStreamTextureProxyIfNeeded();
248 void DoCreateStreamTexture();
251 // Helper method to reestablish the surface texture peer for android
253 void EstablishSurfaceTexturePeer();
255 // Requesting whether the surface texture peer needs to be reestablished.
256 void SetNeedsEstablishPeer(bool needs_establish_peer);
258 #if defined(GOOGLE_TV)
259 // Request external surface for out-of-band composition.
260 void RequestExternalSurface();
264 void DrawRemotePlaybackIcon();
265 void ReallocateVideoFrame();
266 void SetCurrentFrameInternal(scoped_refptr<media::VideoFrame>& frame);
267 void DidLoadMediaInfo(MediaInfoLoader::Status status);
268 void DoReleaseRemotePlaybackTexture(uint32 sync_point);
270 // Actually do the work for generateKeyRequest/addKey so they can easily
271 // report results to UMA.
272 MediaKeyException GenerateKeyRequestInternal(
273 const WebKit::WebString& key_system,
274 const unsigned char* init_data,
275 unsigned init_data_length);
276 MediaKeyException AddKeyInternal(const WebKit::WebString& key_system,
277 const unsigned char* key,
279 const unsigned char* init_data,
280 unsigned init_data_length,
281 const WebKit::WebString& session_id);
282 MediaKeyException CancelKeyRequestInternal(
283 const WebKit::WebString& key_system,
284 const WebKit::WebString& session_id);
286 WebKit::WebFrame* const frame_;
288 WebKit::WebMediaPlayerClient* const client_;
290 // |delegate_| is used to notify the browser process of the player status, so
291 // that the browser process can control screen locks.
292 // TODO(qinmin): Currently android mediaplayer takes care of the screen
293 // lock. So this is only used for media source. Will apply this to regular
294 // media tag once http://crbug.com/247892 is fixed.
295 base::WeakPtr<WebMediaPlayerDelegate> delegate_;
297 // Save the list of buffered time ranges.
298 WebKit::WebTimeRanges buffered_;
300 // Size of the video.
301 WebKit::WebSize natural_size_;
303 // Size that has been sent to StreamTexture.
304 WebKit::WebSize cached_stream_texture_size_;
306 // The video frame object used for rendering by the compositor.
307 scoped_refptr<media::VideoFrame> current_frame_;
308 base::Lock current_frame_lock_;
310 base::ThreadChecker main_thread_checker_;
312 // Message loop for main renderer thread.
313 const scoped_refptr<base::MessageLoopProxy> main_loop_;
315 // Message loop for media thread.
316 const scoped_refptr<base::MessageLoopProxy> media_loop_;
318 // URL of the media file to be fetched.
322 base::TimeDelta duration_;
324 // Flag to remember if we have a trusted duration_ value provided by
325 // MediaSourceDelegate notifying OnDurationChanged(). In this case, ignore
326 // any subsequent duration value passed to OnMediaMetadataChange().
327 bool ignore_metadata_duration_change_;
329 // Seek gets pending if another seek is in progress. Only last pending seek
332 base::TimeDelta pending_seek_time_;
334 // Internal seek state.
336 base::TimeDelta seek_time_;
338 // Whether loading has progressed since the last call to didLoadingProgress.
339 mutable bool did_loading_progress_;
341 // Manager for managing this object and for delegating method calls on
343 RendererMediaPlayerManager* manager_;
345 // Player ID assigned by the |manager_|.
348 // Current player states.
349 WebKit::WebMediaPlayer::NetworkState network_state_;
350 WebKit::WebMediaPlayer::ReadyState ready_state_;
352 // GL texture ID used to show the remote playback icon.
353 unsigned int remote_playback_texture_id_;
355 // GL texture ID allocated to the video.
356 unsigned int texture_id_;
358 // GL texture mailbox for texture_id_ to provide in the VideoFrame, and sync
359 // point for when the mailbox was produced.
360 gpu::Mailbox texture_mailbox_;
361 unsigned int texture_mailbox_sync_point_;
363 // Stream texture ID allocated to the video.
364 unsigned int stream_id_;
366 // Whether the mediaplayer is playing.
369 // Whether the mediaplayer has already started playing.
370 bool playing_started_;
372 // Whether media player needs to re-establish the surface texture peer.
373 bool needs_establish_peer_;
375 // Whether |stream_texture_proxy_| is initialized.
376 bool stream_texture_proxy_initialized_;
378 // Whether the video size info is available.
381 // Whether the video metadata and info are available.
382 bool has_media_metadata_;
383 bool has_media_info_;
385 // Object for allocating stream textures.
386 scoped_ptr<StreamTextureFactory> stream_texture_factory_;
388 // Object for calling back the compositor thread to repaint the video when a
389 // frame available. It should be initialized on the compositor thread.
390 ScopedStreamTextureProxy stream_texture_proxy_;
392 // Whether media player needs external surface.
393 bool needs_external_surface_;
395 // A pointer back to the compositor to inform it about state changes. This is
396 // not NULL while the compositor is actively using this webmediaplayer.
397 cc::VideoFrameProvider::Client* video_frame_provider_client_;
399 scoped_ptr<webkit::WebLayerImpl> video_weblayer_;
401 #if defined(GOOGLE_TV)
402 // Pixel threshold for external surface usage. Negative value means that the
403 // threshold is not defined, so that external surface is never used.
404 int external_surface_threshold_;
406 // A rectangle represents the geometry of video frame, when computed last
408 gfx::RectF last_computed_rect_;
410 // Media Stream related fields.
411 media::Demuxer* demuxer_;
412 base::Closure destroy_demuxer_cb_;
413 scoped_refptr<MediaStreamAudioRenderer> audio_renderer_;
414 MediaStreamClient* media_stream_client_;
417 scoped_ptr<MediaSourceDelegate,
418 MediaSourceDelegate::Destroyer> media_source_delegate_;
420 // Internal pending playback state.
421 // Store a playback request that cannot be started immediately.
422 bool pending_playback_;
424 MediaPlayerHostMsg_Initialize_Type player_type_;
426 // The current playing time. Because the media player is in the browser
427 // process, it will regularly update the |current_time_| by calling
429 double current_time_;
431 // Whether the browser is currently connected to a remote media player.
434 media::MediaLog* media_log_;
436 scoped_ptr<MediaInfoLoader> info_loader_;
438 // The currently selected key system. Empty string means that no key system
439 // has been selected.
440 WebKit::WebString current_key_system_;
442 // Temporary for EME v0.1. In the future the init data type should be passed
443 // through GenerateKeyRequest() directly from WebKit.
444 std::string init_data_type_;
446 // The decryptor that manages decryption keys and decrypts encrypted frames.
447 scoped_ptr<ProxyDecryptor> decryptor_;
449 base::WeakPtrFactory<WebMediaPlayerAndroid> weak_factory_;
451 DISALLOW_COPY_AND_ASSIGN(WebMediaPlayerAndroid);
454 } // namespace content
456 #endif // CONTENT_RENDERER_MEDIA_ANDROID_WEBMEDIAPLAYER_ANDROID_H_