1// Copyright 2014 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_WEBRTC_MOCK_PEER_CONNECTION_DEPENDENCY_FACTORY_H_
6#define CONTENT_RENDERER_MEDIA_WEBRTC_MOCK_PEER_CONNECTION_DEPENDENCY_FACTORY_H_
7
8#include <set>
9#include <string>
10#include <vector>
11
12#include "base/compiler_specific.h"
13#include "content/renderer/media/webrtc/peer_connection_dependency_factory.h"
14#include "third_party/libjingle/source/talk/app/webrtc/mediaconstraintsinterface.h"
15#include "third_party/libjingle/source/talk/media/base/videorenderer.h"
16
17namespace content {
18
19class WebAudioCapturerSource;
20
21class MockVideoRenderer : public cricket::VideoRenderer {
22 public:
23  MockVideoRenderer();
24  virtual ~MockVideoRenderer();
25  virtual bool SetSize(int width, int height, int reserved) OVERRIDE;
26  virtual bool RenderFrame(const cricket::VideoFrame* frame) OVERRIDE;
27
28  int width() const { return width_; }
29  int height() const { return height_; }
30  int num() const { return num_; }
31
32 private:
33  int width_;
34  int height_;
35  int num_;
36};
37
38class MockVideoSource : public webrtc::VideoSourceInterface {
39 public:
40  MockVideoSource();
41
42  virtual void RegisterObserver(webrtc::ObserverInterface* observer) OVERRIDE;
43  virtual void UnregisterObserver(webrtc::ObserverInterface* observer) OVERRIDE;
44  virtual MediaSourceInterface::SourceState state() const OVERRIDE;
45  virtual cricket::VideoCapturer* GetVideoCapturer() OVERRIDE;
46  virtual void AddSink(cricket::VideoRenderer* output) OVERRIDE;
47  virtual void RemoveSink(cricket::VideoRenderer* output) OVERRIDE;
48  virtual cricket::VideoRenderer* FrameInput() OVERRIDE;
49  virtual const cricket::VideoOptions* options() const OVERRIDE;
50
51  // Changes the state of the source to live and notifies the observer.
52  void SetLive();
53  // Changes the state of the source to ended and notifies the observer.
54  void SetEnded();
55  // Set the video capturer.
56  void SetVideoCapturer(cricket::VideoCapturer* capturer);
57
58  // Test helpers.
59  int GetLastFrameWidth() const;
60  int GetLastFrameHeight() const;
61  int GetFrameNum() const;
62
63 protected:
64  virtual ~MockVideoSource();
65
66 private:
67  void FireOnChanged();
68
69  std::vector<webrtc::ObserverInterface*> observers_;
70  MediaSourceInterface::SourceState state_;
71  scoped_ptr<cricket::VideoCapturer> capturer_;
72  MockVideoRenderer renderer_;
73};
74
75class MockAudioSource : public webrtc::AudioSourceInterface {
76 public:
77  explicit MockAudioSource(
78      const webrtc::MediaConstraintsInterface* constraints);
79
80  virtual void RegisterObserver(webrtc::ObserverInterface* observer) OVERRIDE;
81  virtual void UnregisterObserver(webrtc::ObserverInterface* observer) OVERRIDE;
82  virtual MediaSourceInterface::SourceState state() const OVERRIDE;
83
84  // Changes the state of the source to live and notifies the observer.
85  void SetLive();
86  // Changes the state of the source to ended and notifies the observer.
87  void SetEnded();
88
89  const webrtc::MediaConstraintsInterface::Constraints& optional_constraints() {
90    return optional_constraints_;
91  }
92
93  const webrtc::MediaConstraintsInterface::Constraints&
94  mandatory_constraints() {
95    return mandatory_constraints_;
96  }
97
98 protected:
99  virtual ~MockAudioSource();
100
101 private:
102  webrtc::ObserverInterface* observer_;
103  MediaSourceInterface::SourceState state_;
104  webrtc::MediaConstraintsInterface::Constraints optional_constraints_;
105  webrtc::MediaConstraintsInterface::Constraints mandatory_constraints_;
106};
107
108class MockWebRtcVideoTrack : public webrtc::VideoTrackInterface {
109 public:
110  MockWebRtcVideoTrack(const std::string& id,
111                      webrtc::VideoSourceInterface* source);
112  virtual void AddRenderer(webrtc::VideoRendererInterface* renderer) OVERRIDE;
113  virtual void RemoveRenderer(
114      webrtc::VideoRendererInterface* renderer) OVERRIDE;
115  virtual std::string kind() const OVERRIDE;
116  virtual std::string id() const OVERRIDE;
117  virtual bool enabled() const OVERRIDE;
118  virtual TrackState state() const OVERRIDE;
119  virtual bool set_enabled(bool enable) OVERRIDE;
120  virtual bool set_state(TrackState new_state) OVERRIDE;
121  virtual void RegisterObserver(webrtc::ObserverInterface* observer) OVERRIDE;
122  virtual void UnregisterObserver(webrtc::ObserverInterface* observer) OVERRIDE;
123  virtual webrtc::VideoSourceInterface* GetSource() const OVERRIDE;
124
125 protected:
126  virtual ~MockWebRtcVideoTrack();
127
128 private:
129  bool enabled_;
130  std::string id_;
131  TrackState state_;
132  scoped_refptr<webrtc::VideoSourceInterface> source_;
133  webrtc::ObserverInterface* observer_;
134  webrtc::VideoRendererInterface* renderer_;
135};
136
137class MockMediaStream : public webrtc::MediaStreamInterface {
138 public:
139  explicit MockMediaStream(const std::string& label);
140
141  virtual bool AddTrack(webrtc::AudioTrackInterface* track) OVERRIDE;
142  virtual bool AddTrack(webrtc::VideoTrackInterface* track) OVERRIDE;
143  virtual bool RemoveTrack(webrtc::AudioTrackInterface* track) OVERRIDE;
144  virtual bool RemoveTrack(webrtc::VideoTrackInterface* track) OVERRIDE;
145  virtual std::string label() const OVERRIDE;
146  virtual webrtc::AudioTrackVector GetAudioTracks() OVERRIDE;
147  virtual webrtc::VideoTrackVector GetVideoTracks() OVERRIDE;
148  virtual rtc::scoped_refptr<webrtc::AudioTrackInterface>
149      FindAudioTrack(const std::string& track_id) OVERRIDE;
150  virtual rtc::scoped_refptr<webrtc::VideoTrackInterface>
151      FindVideoTrack(const std::string& track_id) OVERRIDE;
152  virtual void RegisterObserver(webrtc::ObserverInterface* observer) OVERRIDE;
153  virtual void UnregisterObserver(webrtc::ObserverInterface* observer) OVERRIDE;
154
155 protected:
156  virtual ~MockMediaStream();
157
158 private:
159  void NotifyObservers();
160
161  std::string label_;
162  webrtc::AudioTrackVector audio_track_vector_;
163  webrtc::VideoTrackVector video_track_vector_;
164
165  typedef std::set<webrtc::ObserverInterface*> ObserverSet;
166  ObserverSet observers_;
167};
168
169// A mock factory for creating different objects for
170// RTC PeerConnections.
171class MockPeerConnectionDependencyFactory
172     : public PeerConnectionDependencyFactory {
173 public:
174  MockPeerConnectionDependencyFactory();
175  virtual ~MockPeerConnectionDependencyFactory();
176
177  virtual scoped_refptr<webrtc::PeerConnectionInterface> CreatePeerConnection(
178      const webrtc::PeerConnectionInterface::RTCConfiguration& config,
179      const webrtc::MediaConstraintsInterface* constraints,
180      blink::WebFrame* frame,
181      webrtc::PeerConnectionObserver* observer) OVERRIDE;
182  virtual scoped_refptr<webrtc::AudioSourceInterface>
183      CreateLocalAudioSource(
184          const webrtc::MediaConstraintsInterface* constraints) OVERRIDE;
185  virtual WebRtcVideoCapturerAdapter* CreateVideoCapturer(
186      bool is_screen_capture) OVERRIDE;
187  virtual scoped_refptr<webrtc::VideoSourceInterface>
188      CreateVideoSource(
189          cricket::VideoCapturer* capturer,
190          const blink::WebMediaConstraints& constraints) OVERRIDE;
191  virtual scoped_refptr<WebAudioCapturerSource> CreateWebAudioSource(
192      blink::WebMediaStreamSource* source) OVERRIDE;
193  virtual scoped_refptr<webrtc::MediaStreamInterface>
194      CreateLocalMediaStream(const std::string& label) OVERRIDE;
195  virtual scoped_refptr<webrtc::VideoTrackInterface>
196      CreateLocalVideoTrack(const std::string& id,
197                            webrtc::VideoSourceInterface* source) OVERRIDE;
198  virtual scoped_refptr<webrtc::VideoTrackInterface>
199      CreateLocalVideoTrack(const std::string& id,
200                            cricket::VideoCapturer* capturer) OVERRIDE;
201  virtual webrtc::SessionDescriptionInterface* CreateSessionDescription(
202      const std::string& type,
203      const std::string& sdp,
204      webrtc::SdpParseError* error) OVERRIDE;
205  virtual webrtc::IceCandidateInterface* CreateIceCandidate(
206      const std::string& sdp_mid,
207      int sdp_mline_index,
208      const std::string& sdp) OVERRIDE;
209
210  virtual scoped_refptr<WebRtcAudioCapturer> CreateAudioCapturer(
211      int render_view_id, const StreamDeviceInfo& device_info,
212      const blink::WebMediaConstraints& constraints,
213      MediaStreamAudioSource* audio_source) OVERRIDE;
214  void FailToCreateNextAudioCapturer() {
215    fail_to_create_next_audio_capturer_ = true;
216  }
217
218  virtual void StartLocalAudioTrack(
219      WebRtcLocalAudioTrack* audio_track) OVERRIDE;
220
221  MockAudioSource* last_audio_source() { return last_audio_source_.get(); }
222  MockVideoSource* last_video_source() { return last_video_source_.get(); }
223
224 private:
225  bool fail_to_create_next_audio_capturer_;
226  scoped_refptr <MockAudioSource> last_audio_source_;
227  scoped_refptr <MockVideoSource> last_video_source_;
228
229  DISALLOW_COPY_AND_ASSIGN(MockPeerConnectionDependencyFactory);
230};
231
232}  // namespace content
233
234#endif  // CONTENT_RENDERER_MEDIA_WEBRTC_MOCK_PEER_CONNECTION_DEPENDENCY_FACTORY_H_
235