Home | History | Annotate | Download | only in media
      1 // Copyright (c) 2012 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_MOCK_MEDIA_STREAM_DEPENDENCY_FACTORY_H_
      6 #define CONTENT_RENDERER_MEDIA_MOCK_MEDIA_STREAM_DEPENDENCY_FACTORY_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/compiler_specific.h"
     12 #include "content/renderer/media/media_stream_dependency_factory.h"
     13 #include "third_party/libjingle/source/talk/app/webrtc/mediaconstraintsinterface.h"
     14 
     15 namespace content {
     16 
     17 class MockVideoSource : public webrtc::VideoSourceInterface {
     18  public:
     19   MockVideoSource();
     20 
     21   virtual void RegisterObserver(webrtc::ObserverInterface* observer) OVERRIDE;
     22   virtual void UnregisterObserver(webrtc::ObserverInterface* observer) OVERRIDE;
     23   virtual MediaSourceInterface::SourceState state() const OVERRIDE;
     24   virtual cricket::VideoCapturer* GetVideoCapturer() OVERRIDE;
     25   virtual void AddSink(cricket::VideoRenderer* output) OVERRIDE;
     26   virtual void RemoveSink(cricket::VideoRenderer* output) OVERRIDE;
     27   virtual const cricket::VideoOptions* options() const OVERRIDE;
     28 
     29   // Changes the state of the source to live and notifies the observer.
     30   void SetLive();
     31   // Changes the state of the source to ended and notifies the observer.
     32   void SetEnded();
     33   // Set the video capturer.
     34   void SetVideoCapturer(cricket::VideoCapturer* capturer);
     35 
     36  protected:
     37   virtual ~MockVideoSource();
     38 
     39  private:
     40   void FireOnChanged();
     41 
     42   std::vector<webrtc::ObserverInterface*> observers_;
     43   MediaSourceInterface::SourceState state_;
     44   scoped_ptr<cricket::VideoCapturer> capturer_;
     45 };
     46 
     47 class MockAudioSource : public webrtc::AudioSourceInterface {
     48  public:
     49   explicit MockAudioSource(
     50       const webrtc::MediaConstraintsInterface* constraints);
     51 
     52   virtual void RegisterObserver(webrtc::ObserverInterface* observer) OVERRIDE;
     53   virtual void UnregisterObserver(webrtc::ObserverInterface* observer) OVERRIDE;
     54   virtual MediaSourceInterface::SourceState state() const OVERRIDE;
     55 
     56   // Changes the state of the source to live and notifies the observer.
     57   void SetLive();
     58   // Changes the state of the source to ended and notifies the observer.
     59   void SetEnded();
     60 
     61   const webrtc::MediaConstraintsInterface::Constraints& optional_constraints() {
     62     return optional_constraints_;
     63   }
     64 
     65   const webrtc::MediaConstraintsInterface::Constraints&
     66   mandatory_constraints() {
     67     return mandatory_constraints_;
     68   }
     69 
     70  protected:
     71   virtual ~MockAudioSource();
     72 
     73  private:
     74   webrtc::ObserverInterface* observer_;
     75   MediaSourceInterface::SourceState state_;
     76   webrtc::MediaConstraintsInterface::Constraints optional_constraints_;
     77   webrtc::MediaConstraintsInterface::Constraints mandatory_constraints_;
     78 };
     79 
     80 class MockLocalVideoTrack : public webrtc::VideoTrackInterface {
     81  public:
     82   MockLocalVideoTrack(std::string id,
     83                       webrtc::VideoSourceInterface* source);
     84   virtual void AddRenderer(webrtc::VideoRendererInterface* renderer) OVERRIDE;
     85   virtual void RemoveRenderer(
     86       webrtc::VideoRendererInterface* renderer) OVERRIDE;
     87   virtual cricket::VideoRenderer* FrameInput() OVERRIDE;
     88   virtual std::string kind() const OVERRIDE;
     89   virtual std::string id() const OVERRIDE;
     90   virtual bool enabled() const OVERRIDE;
     91   virtual TrackState state() const OVERRIDE;
     92   virtual bool set_enabled(bool enable) OVERRIDE;
     93   virtual bool set_state(TrackState new_state) OVERRIDE;
     94   virtual void RegisterObserver(webrtc::ObserverInterface* observer) OVERRIDE;
     95   virtual void UnregisterObserver(webrtc::ObserverInterface* observer) OVERRIDE;
     96   virtual webrtc::VideoSourceInterface* GetSource() const OVERRIDE;
     97 
     98  protected:
     99   virtual ~MockLocalVideoTrack();
    100 
    101  private:
    102   bool enabled_;
    103   std::string id_;
    104   TrackState state_;
    105   scoped_refptr<webrtc::VideoSourceInterface> source_;
    106   webrtc::ObserverInterface* observer_;
    107 };
    108 
    109 // A mock factory for creating different objects for
    110 // RTC MediaStreams and PeerConnections.
    111 class MockMediaStreamDependencyFactory : public MediaStreamDependencyFactory {
    112  public:
    113   MockMediaStreamDependencyFactory();
    114   virtual ~MockMediaStreamDependencyFactory();
    115 
    116   virtual scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
    117       const webrtc::PeerConnectionInterface::IceServers& ice_servers,
    118       const webrtc::MediaConstraintsInterface* constraints,
    119       WebKit::WebFrame* frame,
    120       webrtc::PeerConnectionObserver* observer) OVERRIDE;
    121   virtual scoped_refptr<webrtc::AudioSourceInterface>
    122       CreateLocalAudioSource(
    123           const webrtc::MediaConstraintsInterface* constraints) OVERRIDE;
    124   virtual scoped_refptr<webrtc::VideoSourceInterface>
    125       CreateLocalVideoSource(
    126           int video_session_id,
    127           bool is_screencast,
    128           const webrtc::MediaConstraintsInterface* constraints) OVERRIDE;
    129   virtual scoped_refptr<WebRtcAudioCapturer> CreateWebAudioSource(
    130       WebKit::WebMediaStreamSource* source) OVERRIDE;
    131   virtual scoped_refptr<webrtc::MediaStreamInterface>
    132       CreateLocalMediaStream(const std::string& label) OVERRIDE;
    133   virtual scoped_refptr<webrtc::VideoTrackInterface>
    134       CreateLocalVideoTrack(const std::string& id,
    135                             webrtc::VideoSourceInterface* source) OVERRIDE;
    136   virtual scoped_refptr<webrtc::VideoTrackInterface>
    137       CreateLocalVideoTrack(const std::string& id,
    138                             cricket::VideoCapturer* capturer) OVERRIDE;
    139   virtual scoped_refptr<webrtc::AudioTrackInterface>
    140       CreateLocalAudioTrack(const std::string& id,
    141                             const scoped_refptr<WebRtcAudioCapturer>& capturer,
    142                             webrtc::AudioSourceInterface* source) OVERRIDE;
    143   virtual webrtc::SessionDescriptionInterface* CreateSessionDescription(
    144       const std::string& type,
    145       const std::string& sdp,
    146       webrtc::SdpParseError* error) OVERRIDE;
    147   virtual webrtc::IceCandidateInterface* CreateIceCandidate(
    148       const std::string& sdp_mid,
    149       int sdp_mline_index,
    150       const std::string& sdp) OVERRIDE;
    151 
    152   virtual bool EnsurePeerConnectionFactory() OVERRIDE;
    153   virtual bool PeerConnectionFactoryCreated() OVERRIDE;
    154 
    155   virtual scoped_refptr<WebRtcAudioCapturer> MaybeCreateAudioCapturer(
    156       int render_view_id, const StreamDeviceInfo& device_info) OVERRIDE;
    157 
    158   MockAudioSource* last_audio_source() { return last_audio_source_.get(); }
    159   MockVideoSource* last_video_source() { return last_video_source_.get(); }
    160 
    161  private:
    162   bool mock_pc_factory_created_;
    163   scoped_refptr <MockAudioSource> last_audio_source_;
    164   scoped_refptr <MockVideoSource> last_video_source_;
    165 
    166   DISALLOW_COPY_AND_ASSIGN(MockMediaStreamDependencyFactory);
    167 };
    168 
    169 }  // namespace content
    170 
    171 #endif  // CONTENT_RENDERER_MEDIA_MOCK_MEDIA_STREAM_DEPENDENCY_FACTORY_H_
    172