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.
5 #ifndef CONTENT_RENDERER_MEDIA_WEBRTC_MOCK_PEER_CONNECTION_DEPENDENCY_FACTORY_H_
6 #define CONTENT_RENDERER_MEDIA_WEBRTC_MOCK_PEER_CONNECTION_DEPENDENCY_FACTORY_H_
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"
19 class WebAudioCapturerSource
;
20 typedef std::set
<webrtc::ObserverInterface
*> ObserverSet
;
22 class MockVideoRenderer
: public cricket::VideoRenderer
{
25 ~MockVideoRenderer() override
;
26 bool SetSize(int width
, int height
, int reserved
) override
;
27 bool RenderFrame(const cricket::VideoFrame
* frame
) override
;
29 int width() const { return width_
; }
30 int height() const { return height_
; }
31 int num() const { return num_
; }
39 class MockVideoSource
: public webrtc::VideoSourceInterface
{
43 void RegisterObserver(webrtc::ObserverInterface
* observer
) override
;
44 void UnregisterObserver(webrtc::ObserverInterface
* observer
) override
;
45 MediaSourceInterface::SourceState
state() const override
;
46 cricket::VideoCapturer
* GetVideoCapturer() override
;
47 void AddSink(cricket::VideoRenderer
* output
) override
;
48 void RemoveSink(cricket::VideoRenderer
* output
) override
;
49 cricket::VideoRenderer
* FrameInput() override
;
50 const cricket::VideoOptions
* options() const override
;
52 void Restart() override
;
54 // Changes the state of the source to live and notifies the observer.
56 // Changes the state of the source to ended and notifies the observer.
58 // Set the video capturer.
59 void SetVideoCapturer(cricket::VideoCapturer
* capturer
);
62 int GetLastFrameWidth() const;
63 int GetLastFrameHeight() const;
64 int GetFrameNum() const;
67 ~MockVideoSource() override
;
72 std::vector
<webrtc::ObserverInterface
*> observers_
;
73 MediaSourceInterface::SourceState state_
;
74 scoped_ptr
<cricket::VideoCapturer
> capturer_
;
75 MockVideoRenderer renderer_
;
78 class MockAudioSource
: public webrtc::AudioSourceInterface
{
80 explicit MockAudioSource(
81 const webrtc::MediaConstraintsInterface
* constraints
);
83 void RegisterObserver(webrtc::ObserverInterface
* observer
) override
;
84 void UnregisterObserver(webrtc::ObserverInterface
* observer
) override
;
85 MediaSourceInterface::SourceState
state() const override
;
87 // Changes the state of the source to live and notifies the observer.
89 // Changes the state of the source to ended and notifies the observer.
92 const webrtc::MediaConstraintsInterface::Constraints
& optional_constraints() {
93 return optional_constraints_
;
96 const webrtc::MediaConstraintsInterface::Constraints
&
97 mandatory_constraints() {
98 return mandatory_constraints_
;
102 ~MockAudioSource() override
;
105 ObserverSet observers_
;
106 MediaSourceInterface::SourceState state_
;
107 webrtc::MediaConstraintsInterface::Constraints optional_constraints_
;
108 webrtc::MediaConstraintsInterface::Constraints mandatory_constraints_
;
111 class MockWebRtcVideoTrack
: public webrtc::VideoTrackInterface
{
113 MockWebRtcVideoTrack(const std::string
& id
,
114 webrtc::VideoSourceInterface
* source
);
115 void AddRenderer(webrtc::VideoRendererInterface
* renderer
) override
;
116 void RemoveRenderer(webrtc::VideoRendererInterface
* renderer
) override
;
117 std::string
kind() const override
;
118 std::string
id() const override
;
119 bool enabled() const override
;
120 TrackState
state() const override
;
121 bool set_enabled(bool enable
) override
;
122 bool set_state(TrackState new_state
) override
;
123 void RegisterObserver(webrtc::ObserverInterface
* observer
) override
;
124 void UnregisterObserver(webrtc::ObserverInterface
* observer
) override
;
125 webrtc::VideoSourceInterface
* GetSource() const override
;
128 ~MockWebRtcVideoTrack() override
;
134 scoped_refptr
<webrtc::VideoSourceInterface
> source_
;
135 ObserverSet observers_
;
136 webrtc::VideoRendererInterface
* renderer_
;
139 class MockMediaStream
: public webrtc::MediaStreamInterface
{
141 explicit MockMediaStream(const std::string
& label
);
143 bool AddTrack(webrtc::AudioTrackInterface
* track
) override
;
144 bool AddTrack(webrtc::VideoTrackInterface
* track
) override
;
145 bool RemoveTrack(webrtc::AudioTrackInterface
* track
) override
;
146 bool RemoveTrack(webrtc::VideoTrackInterface
* track
) override
;
147 std::string
label() const override
;
148 webrtc::AudioTrackVector
GetAudioTracks() override
;
149 webrtc::VideoTrackVector
GetVideoTracks() override
;
150 rtc::scoped_refptr
<webrtc::AudioTrackInterface
> FindAudioTrack(
151 const std::string
& track_id
) override
;
152 rtc::scoped_refptr
<webrtc::VideoTrackInterface
> FindVideoTrack(
153 const std::string
& track_id
) override
;
154 void RegisterObserver(webrtc::ObserverInterface
* observer
) override
;
155 void UnregisterObserver(webrtc::ObserverInterface
* observer
) override
;
158 ~MockMediaStream() override
;
161 void NotifyObservers();
164 webrtc::AudioTrackVector audio_track_vector_
;
165 webrtc::VideoTrackVector video_track_vector_
;
167 ObserverSet observers_
;
170 // A mock factory for creating different objects for
171 // RTC PeerConnections.
172 class MockPeerConnectionDependencyFactory
173 : public PeerConnectionDependencyFactory
{
175 MockPeerConnectionDependencyFactory();
176 ~MockPeerConnectionDependencyFactory() override
;
178 scoped_refptr
<webrtc::PeerConnectionInterface
> CreatePeerConnection(
179 const webrtc::PeerConnectionInterface::RTCConfiguration
& config
,
180 const webrtc::MediaConstraintsInterface
* constraints
,
181 blink::WebFrame
* frame
,
182 webrtc::PeerConnectionObserver
* observer
) override
;
183 scoped_refptr
<webrtc::AudioSourceInterface
> CreateLocalAudioSource(
184 const webrtc::MediaConstraintsInterface
* constraints
) override
;
185 WebRtcVideoCapturerAdapter
* CreateVideoCapturer(
186 bool is_screen_capture
) override
;
187 scoped_refptr
<webrtc::VideoSourceInterface
> CreateVideoSource(
188 cricket::VideoCapturer
* capturer
,
189 const blink::WebMediaConstraints
& constraints
) override
;
190 scoped_refptr
<WebAudioCapturerSource
> CreateWebAudioSource(
191 blink::WebMediaStreamSource
* source
) override
;
192 scoped_refptr
<webrtc::MediaStreamInterface
> CreateLocalMediaStream(
193 const std::string
& label
) override
;
194 scoped_refptr
<webrtc::VideoTrackInterface
> CreateLocalVideoTrack(
195 const std::string
& id
,
196 webrtc::VideoSourceInterface
* source
) override
;
197 scoped_refptr
<webrtc::VideoTrackInterface
> CreateLocalVideoTrack(
198 const std::string
& id
,
199 cricket::VideoCapturer
* capturer
) override
;
200 webrtc::SessionDescriptionInterface
* CreateSessionDescription(
201 const std::string
& type
,
202 const std::string
& sdp
,
203 webrtc::SdpParseError
* error
) override
;
204 webrtc::IceCandidateInterface
* CreateIceCandidate(
205 const std::string
& sdp_mid
,
207 const std::string
& sdp
) override
;
209 scoped_refptr
<WebRtcAudioCapturer
> CreateAudioCapturer(
211 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;
218 void StartLocalAudioTrack(WebRtcLocalAudioTrack
* audio_track
) override
;
220 MockAudioSource
* last_audio_source() { return last_audio_source_
.get(); }
221 MockVideoSource
* last_video_source() { return last_video_source_
.get(); }
224 bool fail_to_create_next_audio_capturer_
;
225 scoped_refptr
<MockAudioSource
> last_audio_source_
;
226 scoped_refptr
<MockVideoSource
> last_video_source_
;
228 DISALLOW_COPY_AND_ASSIGN(MockPeerConnectionDependencyFactory
);
231 } // namespace content
233 #endif // CONTENT_RENDERER_MEDIA_WEBRTC_MOCK_PEER_CONNECTION_DEPENDENCY_FACTORY_H_