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