Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / renderer / media / webmediaplayer_ms.h
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 CONTENT_RENDERER_MEDIA_WEBMEDIAPLAYER_MS_H_
6 #define CONTENT_RENDERER_MEDIA_WEBMEDIAPLAYER_MS_H_
7
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/synchronization/lock.h"
12 #include "base/threading/thread_checker.h"
13 #include "cc/layers/video_frame_provider.h"
14 #include "media/filters/skcanvas_video_renderer.h"
15 #include "skia/ext/platform_canvas.h"
16 #include "third_party/WebKit/public/platform/WebMediaPlayer.h"
17 #include "url/gurl.h"
18
19 namespace blink {
20 class WebFrame;
21 class WebMediaPlayerClient;
22 }
23
24 namespace media {
25 class MediaLog;
26 }
27
28
29 namespace content {
30 class MediaStreamAudioRenderer;
31 class MediaStreamRendererFactory;
32 class VideoFrameProvider;
33 class WebLayerImpl;
34 class WebMediaPlayerDelegate;
35
36 // WebMediaPlayerMS delegates calls from WebCore::MediaPlayerPrivate to
37 // Chrome's media player when "src" is from media stream.
38 //
39 // WebMediaPlayerMS works with multiple objects, the most important ones are:
40 //
41 // VideoFrameProvider
42 //   provides video frames for rendering.
43 //
44 // TODO(wjia): add AudioPlayer.
45 // AudioPlayer
46 //   plays audio streams.
47 //
48 // blink::WebMediaPlayerClient
49 //   WebKit client of this media player object.
50 class WebMediaPlayerMS
51     : public blink::WebMediaPlayer,
52       public cc::VideoFrameProvider,
53       public base::SupportsWeakPtr<WebMediaPlayerMS> {
54  public:
55   // Construct a WebMediaPlayerMS with reference to the client, and
56   // a MediaStreamClient which provides VideoFrameProvider.
57   WebMediaPlayerMS(blink::WebFrame* frame,
58                    blink::WebMediaPlayerClient* client,
59                    base::WeakPtr<WebMediaPlayerDelegate> delegate,
60                    media::MediaLog* media_log,
61                    scoped_ptr<MediaStreamRendererFactory> factory);
62   virtual ~WebMediaPlayerMS();
63
64   virtual void load(LoadType load_type,
65                     const blink::WebURL& url,
66                     CORSMode cors_mode);
67
68   // Playback controls.
69   virtual void play();
70   virtual void pause();
71   virtual bool supportsSave() const;
72   virtual void seek(double seconds);
73   virtual void setRate(double rate);
74   virtual void setVolume(double volume);
75   virtual void setPreload(blink::WebMediaPlayer::Preload preload);
76   virtual blink::WebTimeRanges buffered() const;
77   virtual double maxTimeSeekable() const;
78
79   // Methods for painting.
80   virtual void paint(blink::WebCanvas* canvas,
81                      const blink::WebRect& rect,
82                      unsigned char alpha);
83
84   // True if the loaded media has a playable video/audio track.
85   virtual bool hasVideo() const;
86   virtual bool hasAudio() const;
87
88   // Dimensions of the video.
89   virtual blink::WebSize naturalSize() const;
90
91   // Getters of playback state.
92   virtual bool paused() const;
93   virtual bool seeking() const;
94   virtual double duration() const;
95   virtual double currentTime() const;
96
97   // Internal states of loading and network.
98   virtual blink::WebMediaPlayer::NetworkState networkState() const;
99   virtual blink::WebMediaPlayer::ReadyState readyState() const;
100
101   virtual bool didLoadingProgress();
102
103   virtual bool hasSingleSecurityOrigin() const;
104   virtual bool didPassCORSAccessCheck() const;
105
106   virtual double mediaTimeForTimeValue(double timeValue) const;
107
108   virtual unsigned decodedFrameCount() const;
109   virtual unsigned droppedFrameCount() const;
110   virtual unsigned audioDecodedByteCount() const;
111   virtual unsigned videoDecodedByteCount() const;
112
113   // VideoFrameProvider implementation.
114   virtual void SetVideoFrameProviderClient(
115       cc::VideoFrameProvider::Client* client) OVERRIDE;
116   virtual scoped_refptr<media::VideoFrame> GetCurrentFrame() OVERRIDE;
117   virtual void PutCurrentFrame(const scoped_refptr<media::VideoFrame>& frame)
118       OVERRIDE;
119
120  private:
121   // The callback for VideoFrameProvider to signal a new frame is available.
122   void OnFrameAvailable(const scoped_refptr<media::VideoFrame>& frame);
123   // Need repaint due to state change.
124   void RepaintInternal();
125
126   // The callback for source to report error.
127   void OnSourceError();
128
129   // Helpers that set the network/ready state and notifies the client if
130   // they've changed.
131   void SetNetworkState(blink::WebMediaPlayer::NetworkState state);
132   void SetReadyState(blink::WebMediaPlayer::ReadyState state);
133
134   // Getter method to |client_|.
135   blink::WebMediaPlayerClient* GetClient();
136
137   blink::WebFrame* frame_;
138
139   blink::WebMediaPlayer::NetworkState network_state_;
140   blink::WebMediaPlayer::ReadyState ready_state_;
141
142   blink::WebTimeRanges buffered_;
143
144   float volume_;
145
146   // Used for DCHECKs to ensure methods calls executed in the correct thread.
147   base::ThreadChecker thread_checker_;
148
149   blink::WebMediaPlayerClient* client_;
150
151   base::WeakPtr<WebMediaPlayerDelegate> delegate_;
152
153   // Specify content:: to disambiguate from cc::.
154   scoped_refptr<content::VideoFrameProvider> video_frame_provider_;
155   bool paused_;
156
157   // |current_frame_| is updated only on main thread. The object it holds
158   // can be freed on the compositor thread if it is the last to hold a
159   // reference but media::VideoFrame is a thread-safe ref-pointer. It is
160   // however read on the compositing thread so locking is required around all
161   // modifications on the main thread, and all reads on the compositing thread.
162   scoped_refptr<media::VideoFrame> current_frame_;
163   // |current_frame_used_| is updated on both main and compositing thread.
164   // It's used to track whether |current_frame_| was painted for detecting
165   // when to increase |dropped_frame_count_|.
166   bool current_frame_used_;
167   // |current_frame_lock_| protects |current_frame_used_| and |current_frame_|.
168   base::Lock current_frame_lock_;
169   bool pending_repaint_;
170
171   scoped_ptr<WebLayerImpl> video_weblayer_;
172
173   // A pointer back to the compositor to inform it about state changes. This is
174   // not NULL while the compositor is actively using this webmediaplayer.
175   cc::VideoFrameProvider::Client* video_frame_provider_client_;
176
177   bool received_first_frame_;
178   base::TimeDelta current_time_;
179   unsigned total_frame_count_;
180   unsigned dropped_frame_count_;
181   media::SkCanvasVideoRenderer video_renderer_;
182
183   scoped_refptr<MediaStreamAudioRenderer> audio_renderer_;
184
185   scoped_refptr<media::MediaLog> media_log_;
186
187   scoped_ptr<MediaStreamRendererFactory> renderer_factory_;
188
189   DISALLOW_COPY_AND_ASSIGN(WebMediaPlayerMS);
190 };
191
192 }  // namespace content
193
194 #endif  // CONTENT_RENDERER_MEDIA_WEBMEDIAPLAYER_MS_H_