1774fe8d8974ee5830da1467db7fe0149308bd08
[platform/framework/web/crosswalk-tizen.git] /
1 // Copyright 2015 Samsung Electronics Inc. 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 CONTENT_RENDERER_MEDIA_TIZEN_WEBMEDIAPLAYER_EFL_H_
6 #define CONTENT_RENDERER_MEDIA_TIZEN_WEBMEDIAPLAYER_EFL_H_
7
8 #include <vector>
9
10 #include "base/message_loop/message_loop.h"
11 #include "base/memory/shared_memory.h"
12 #include "cc/layers/video_frame_provider_client_impl.h"
13 #include "content/renderer/media/tizen/media_source_delegate_efl.h"
14 #include "content/renderer/media/tizen/renderer_media_player_manager_efl.h"
15 #include "media/base/cdm_context.h"
16 #include "media/base/video_frame.h"
17 #include "media/blink/video_frame_compositor.h"
18 #include "media/blink/webmediaplayer_delegate.h"
19 #include "media/blink/webmediaplayer_params.h"
20 #include "media/renderers/skcanvas_video_renderer.h"
21 #include "third_party/WebKit/public/platform/WebMediaPlayer.h"
22 #include "third_party/WebKit/public/platform/WebMediaPlayerEncryptedMediaClient.h"
23
24 #if defined(OS_TIZEN_TV_PRODUCT)
25 #define USE_NO_WAIT_SIGNAL_MEDIA_PACKET_THREAD 1
26 #endif
27
28 namespace cc {
29 class VideoLayer;
30 }
31
32 namespace blink {
33 class WebLocalFrame;
34 class WebMediaPlayerClient;
35 }
36
37 namespace cc_blink {
38 class WebLayerImpl;
39 }
40
41 namespace media {
42 class GpuVideoAcceleratorFactories;
43 class MediaLog;
44 class WebMediaPlayerDelegate;
45 class WebMediaPlayerEncryptedMediaClient;
46 }
47
48 namespace content {
49 class RendererMediaPlayerManager;
50
51 // This class implements blink::WebMediaPlayer by keeping the efl
52 // media player in the browser process. It listens to all the status changes
53 // sent from the browser process and sends playback controls to the media
54 // player.
55 class WebMediaPlayerEfl
56     : public blink::WebMediaPlayer,
57       public base::SupportsWeakPtr<WebMediaPlayerEfl>,
58       public NON_EXPORTED_BASE(media::WebMediaPlayerDelegate::Observer) {
59  public:
60   // Construct a WebMediaPlayerEfl object. This class communicates
61   // with the WebMediaPlayerEfl object in the browser process through
62   // |proxy|.
63   WebMediaPlayerEfl(blink::WebLocalFrame* frame,
64                     RendererMediaPlayerManager* manager,
65                     blink::WebMediaPlayerClient* client,
66                     blink::WebMediaPlayerEncryptedMediaClient* encrypted_client,
67                     base::WeakPtr<media::WebMediaPlayerDelegate> delegate,
68                     const media::WebMediaPlayerParams& params,
69                     bool video_hole);
70   ~WebMediaPlayerEfl() override;
71
72   // blink::WebMediaPlayer implementation.
73   void load(LoadType load_type,
74             const blink::WebMediaPlayerSource& source,
75             CORSMode cors_mode) override;
76
77   // Playback controls.
78   void play() override;
79   void pause() override;
80   bool supportsSave() const override;
81   void seek(double seconds) override;
82   void setRate(double) override;
83   void setVolume(double) override;
84   blink::WebTimeRanges buffered() const override;
85   blink::WebTimeRanges seekable() const override;
86   void setSinkId(const blink::WebString& sink_id,
87                  const blink::WebSecurityOrigin& security_origin,
88                  blink::WebSetSinkIdCallbacks* web_callback) override;
89
90   bool copyVideoTextureToPlatformTexture(
91       gpu::gles2::GLES2Interface* web_graphics_context,
92       unsigned int texture,
93       unsigned int internal_format,
94       unsigned int type,
95       bool premultiply_alpha,
96       bool flip_y) override;
97
98 #if defined(TIZEN_TBM_SUPPORT) && defined(OS_TIZEN_TV_PRODUCT)
99   unsigned zeroCopyVideoTbmToPlatformTexture(
100                       gpu::gles2::GLES2Interface*, unsigned) override;
101   void deleteVideoFrame(unsigned) override;
102 #endif
103
104   // True if the loaded media has a playable video/audio track.
105   bool hasVideo() const override;
106   bool hasAudio() const override;
107
108   // Dimension of the video.
109   blink::WebSize naturalSize() const override;
110
111   // Getters of playback state.
112   bool paused() const override;
113   bool seeking() const override;
114   double duration() const override;
115   double currentTime() const override;
116   void suspend() override;
117   void resume() override;
118   double getStartDate() const override;
119   void activate() override;
120
121 #if defined(OS_TIZEN_TV_PRODUCT)
122   void setTranslatedURL(const blink::WebString& url) { translated_url_ = url; }
123 #endif
124
125   // Internal states of loading and network.
126   blink::WebMediaPlayer::NetworkState getNetworkState() const override;
127   blink::WebMediaPlayer::ReadyState getReadyState() const override;
128
129   blink::WebString getErrorMessage() override;
130   bool didLoadingProgress() override;
131
132   bool hasSingleSecurityOrigin() const override;
133   bool didPassCORSAccessCheck() const override;
134
135   double mediaTimeForTimeValue(double timeValue) const override;
136
137   unsigned decodedFrameCount() const override;
138   unsigned droppedFrameCount() const override;
139   size_t audioDecodedByteCount() const override;
140   size_t videoDecodedByteCount() const override;
141
142   void paint(blink::WebCanvas* canvas,
143              const blink::WebRect& rect,
144              SkPaint& paint) override;
145
146   void setContentDecryptionModule(
147       blink::WebContentDecryptionModule* cdm,
148       blink::WebContentDecryptionModuleResult result) override;
149
150 #if defined(TIZEN_MULTIMEDIA_SUPPORT)
151   void launchSystemVolumeController() override;
152 #endif
153
154   // WebMediaPlayerDelegate::Observer implementation.
155   void OnHidden() override;
156   void OnShown() override;
157   bool OnSuspendRequested(bool must_suspend) override;
158   void OnPlay() override;
159   void OnPause() override;
160   void OnVolumeMultiplierUpdate(double multiplier) override;
161
162   void enteredFullscreen();
163   void exitedFullscreen();
164
165   void SetReadyState(WebMediaPlayer::ReadyState state);
166   void SetNetworkState(WebMediaPlayer::NetworkState state);
167
168   void OnNewFrameAvailable(base::SharedMemoryHandle foreign_memory_handle,
169                            uint32_t length,
170                            base::TimeDelta timestamp);
171
172 #if defined(TIZEN_TBM_SUPPORT)
173   void OnNewTbmBufferAvailable(const gfx::TbmBufferHandle& tbm_handle,
174                                base::TimeDelta timestamp,
175                                const base::Closure& cb);
176 #endif
177
178 #if defined(TIZEN_VIDEO_HOLE)
179   void OnDrawableContentRectChanged(gfx::Rect rect);
180 #endif
181
182   void OnMediaDataChange(int width, int height, int media);
183   void OnDurationChange(base::TimeDelta duration);
184   void OnTimeUpdate(base::TimeDelta current_time);
185   void OnBufferUpdate(int buffered_ms);
186   void OnTimeChanged();
187   void OnPauseStateChange(bool state);
188   void OnSeekComplete();
189   void OnRequestSeek(base::TimeDelta seek_time);
190
191   void OnMediaSourceOpened(blink::WebMediaSource* web_media_source);
192   void OnEncryptedMediaInitData(media::EmeInitDataType init_data_type,
193                                 const std::vector<uint8_t>& init_data);
194   void SetCdmReadyCB(const MediaSourceDelegateEfl::CdmReadyCB& cdm_ready_cb);
195   void OnDemuxerSeekDone();
196
197   void RequestPause();
198   void ReleaseMediaResource();
199   void InitializeMediaResource();
200   void OnPlayerSuspend(bool is_preempted);
201   void OnPlayerResumed(bool is_preempted);
202
203 #if defined(OS_TIZEN_TV_PRODUCT)
204   void OnAddAudioTrack(const std::string& kind,
205                        const std::string& language,
206                        const std::string& id,
207                        bool selected);
208   void OnAddVideoTrack(const std::string& kind,
209                        const std::string& language,
210                        const std::string& id,
211                        bool selected);
212   void OnAddTextTrack(const std::string& label,
213                       const std::string& language,
214                       const std::string& id);
215   void OnSeekableTimeChange(base::TimeDelta min_time, base::TimeDelta max_time);
216   void OnDrmError();
217 #endif
218
219 #if defined(TIZEN_MULTIMEDIA_SUPPORT)
220   void OnSystemVolumeChange(int volume);
221 #endif
222
223   // Called when a decoder detects that the key needed to decrypt the stream
224   // is not available.
225   void OnWaitingForDecryptionKey();
226 #if defined(TIZEN_VIDEO_HOLE)
227   void CreateVideoHoleFrame();
228
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();
233   gfx::RectF GetBoundaryRectangle() const;
234   void StartLayerBoundUpdateTimer();
235   void StopLayerBoundUpdateTimer();
236   void OnLayerBoundUpdateTimerFired();
237   bool ShouldCreateVideoHoleFrame() const;
238 #endif
239
240 #if defined(TIZEN_VIDEO_MANUAL_ROTATION_SUPPORT)
241   void OnAutoRotateSettingChanged(bool auto_rotate);
242 #endif
243
244 #if defined(TIZEN_TBM_SUPPORT) && defined(OS_TIZEN_TV_PRODUCT)
245   void NotifyVideoTexturing();
246 #endif
247
248 #if defined(USE_NO_WAIT_SIGNAL_MEDIA_PACKET_THREAD)
249   void SetCurrentFrameInternal(scoped_refptr<media::VideoFrame>& frame);
250   scoped_refptr<media::VideoFrame> GetCurrentFrame();
251 #endif
252
253 #if defined(OS_TIZEN_TV_PRODUCT)
254   void OnInbandEventTextTrack(const std::string& info);
255   void OnInbandEventTextCue(const std::string& info,
256                             unsigned int id,
257                             long long int start_time,
258                             long long int end_time);
259 #endif
260
261  private:
262   // Called after |defer_load_cb_| has decided to allow the load. If
263   // |defer_load_cb_| is null this is called immediately.
264   void DoLoad(LoadType load_type, const blink::WebURL& url);
265
266   void Pause(bool is_media_related_action);
267
268   void OnNaturalSizeChanged(gfx::Size size);
269   void OnOpacityChanged(bool opaque);
270
271   // Returns the current video frame from |compositor_|. Blocks until the
272   // compositor can return the frame.
273 #if defined(OS_TIZEN_TV_PRODUCT)
274   scoped_refptr<media::VideoFrame> GetCurrentFrameFromCompositor(
275              bool create_tbm = true);
276 #else
277   scoped_refptr<media::VideoFrame> GetCurrentFrameFromCompositor();
278 #endif
279
280   // Called whenever there is new frame to be painted.
281   void FrameReady(const scoped_refptr<media::VideoFrame>& frame);
282
283   blink::WebLocalFrame* const frame_;
284
285   blink::WebMediaPlayer::NetworkState network_state_;
286   blink::WebMediaPlayer::ReadyState ready_state_;
287
288   // Message loops for posting tasks on Chrome's main thread. Also used
289   // for DCHECKs so methods calls won't execute in the wrong thread.
290   const scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
291   scoped_refptr<base::SingleThreadTaskRunner> media_task_runner_;
292
293   // Manager for managing this object and for delegating method calls on
294   // Render Thread.
295   content::RendererMediaPlayerManager* manager_;
296
297   blink::WebMediaPlayerClient* client_;
298   blink::WebMediaPlayerEncryptedMediaClient* encrypted_client_;
299
300   scoped_refptr<media::MediaLog> media_log_;
301
302   base::WeakPtr<media::WebMediaPlayerDelegate> delegate_;
303
304   media::WebMediaPlayerParams::DeferLoadCB defer_load_cb_;
305   media::WebMediaPlayerParams::Context3DCB context_3d_cb_;
306
307
308   // The compositor layer for displaying the video content when using
309   // composited playback.
310   std::unique_ptr<cc_blink::WebLayerImpl> video_weblayer_;
311
312   // Video rendering members.
313   scoped_refptr<base::SingleThreadTaskRunner> compositor_task_runner_;
314   media::VideoFrameCompositor* compositor_;
315   media::SkCanvasVideoRenderer skcanvas_video_renderer_;
316
317   base::WeakPtrFactory<WebMediaPlayerEfl> weak_factory_;
318   std::unique_ptr<content::MediaSourceDelegateEfl> media_source_delegate_;
319   MediaPlayerHostMsg_Initialize_Type player_type_;
320   blink::WebString content_mime_type_;
321
322 #if defined(OS_TIZEN_TV_PRODUCT)
323   blink::WebString translated_url_;
324   bool is_live_stream_;
325   base::TimeDelta min_seekable_time_;
326   base::TimeDelta max_seekable_time_;
327   typedef base::hash_map<unsigned,
328                        scoped_refptr<media::VideoFrame>> VideoHandlesMap;
329   VideoHandlesMap video_handles_map_;
330 #endif
331
332   // Player ID assigned by the |manager_|.
333   int player_id_;
334
335   int video_width_;
336   int video_height_;
337
338   bool audio_;
339   bool video_;
340
341   base::TimeDelta current_time_;
342   base::TimeDelta duration_;
343   bool is_paused_;
344   bool is_seeking_;
345   base::TimeDelta seek_time_;
346   bool pending_seek_;
347   base::TimeDelta pending_seek_time_;
348 #if defined(OS_TIZEN_TV_PRODUCT)
349   bool is_paused_before_suspend_;
350 #endif
351
352   // Whether the video is known to be opaque or not.
353   bool opaque_;
354   bool is_fullscreen_;
355 #if defined(TIZEN_VIDEO_HOLE)
356   bool is_draw_ready_;
357   bool pending_play_;
358 #if defined(TIZEN_TBM_SUPPORT) && defined(OS_TIZEN_TV_PRODUCT)
359   bool is_video_texturing_;
360 #endif
361   bool is_video_hole_;
362
363   // A rectangle represents the geometry of video frame, when computed last
364   // time.
365   gfx::RectF last_computed_rect_;
366   base::RepeatingTimer layer_bound_update_timer_;
367 #endif
368   gfx::Size natural_size_;
369   blink::WebTimeRanges buffered_;
370   mutable bool did_loading_progress_;
371   int delegate_id_;
372
373   // The last volume received by setVolume() and the last volume multiplier from
374   // OnVolumeMultiplierUpdate(). The multiplier is typical 1.0, but may be less
375   // if the WebMediaPlayerDelegate has requested a volume reduction (ducking)
376   // for a transient sound.  Playout volume is derived by volume * multiplier.
377   double volume_;
378   double volume_multiplier_;
379 #if defined(OS_TIZEN_TV_PRODUCT)
380   bool force_update_playback_position_;
381 #endif
382
383   // Non-owned pointer to the CdmContext. Updated in the constructor
384   // or setContentDecryptionModule().
385   media::CdmContext* cdm_context_;
386   media::EmeInitDataType init_data_type_;
387   MediaSourceDelegateEfl::CdmReadyCB pending_cdm_ready_cb_;
388
389   // Factories for supporting video accelerators. May be null.
390   media::GpuVideoAcceleratorFactories* gpu_factories_;
391
392   scoped_refptr<cc::VideoLayer> layer_;
393 #if defined(USE_NO_WAIT_SIGNAL_MEDIA_PACKET_THREAD)
394   // The video frame object used for rendering by the compositor.
395   scoped_refptr<media::VideoFrame> current_frame_;
396   base::Lock current_frame_lock_;
397 #endif
398
399   DISALLOW_COPY_AND_ASSIGN(WebMediaPlayerEfl);
400 };
401
402 }  // namespace content
403
404 #endif  // CONTENT_RENDERER_MEDIA_TIZEN_WEBMEDIAPLAYER_EFL_H_