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