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 #include "content/renderer/media/mock_peer_connection_impl.h"
6
7 #include <vector>
8
9 #include "base/logging.h"
10 #include "content/renderer/media/webrtc/mock_peer_connection_dependency_factory.h"
11
12 using testing::_;
13 using webrtc::AudioTrackInterface;
14 using webrtc::CreateSessionDescriptionObserver;
15 using webrtc::DtmfSenderInterface;
16 using webrtc::DtmfSenderObserverInterface;
17 using webrtc::IceCandidateInterface;
18 using webrtc::MediaConstraintsInterface;
19 using webrtc::MediaStreamInterface;
20 using webrtc::PeerConnectionInterface;
21 using webrtc::SessionDescriptionInterface;
22 using webrtc::SetSessionDescriptionObserver;
23
24 namespace content {
25
26 class MockStreamCollection : public webrtc::StreamCollectionInterface {
27 public:
count()28 virtual size_t count() OVERRIDE {
29 return streams_.size();
30 }
at(size_t index)31 virtual MediaStreamInterface* at(size_t index) OVERRIDE {
32 return streams_[index];
33 }
find(const std::string & label)34 virtual MediaStreamInterface* find(const std::string& label) OVERRIDE {
35 for (size_t i = 0; i < streams_.size(); ++i) {
36 if (streams_[i]->label() == label)
37 return streams_[i];
38 }
39 return NULL;
40 }
FindAudioTrack(const std::string & id)41 virtual webrtc::MediaStreamTrackInterface* FindAudioTrack(
42 const std::string& id) OVERRIDE {
43 for (size_t i = 0; i < streams_.size(); ++i) {
44 webrtc::MediaStreamTrackInterface* track =
45 streams_.at(i)->FindAudioTrack(id);
46 if (track)
47 return track;
48 }
49 return NULL;
50 }
FindVideoTrack(const std::string & id)51 virtual webrtc::MediaStreamTrackInterface* FindVideoTrack(
52 const std::string& id) OVERRIDE {
53 for (size_t i = 0; i < streams_.size(); ++i) {
54 webrtc::MediaStreamTrackInterface* track =
55 streams_.at(i)->FindVideoTrack(id);
56 if (track)
57 return track;
58 }
59 return NULL;
60 }
AddStream(MediaStreamInterface * stream)61 void AddStream(MediaStreamInterface* stream) {
62 streams_.push_back(stream);
63 }
RemoveStream(MediaStreamInterface * stream)64 void RemoveStream(MediaStreamInterface* stream) {
65 StreamVector::iterator it = streams_.begin();
66 for (; it != streams_.end(); ++it) {
67 if (it->get() == stream) {
68 streams_.erase(it);
69 break;
70 }
71 }
72 }
73
74 protected:
~MockStreamCollection()75 virtual ~MockStreamCollection() {}
76
77 private:
78 typedef std::vector<rtc::scoped_refptr<MediaStreamInterface> >
79 StreamVector;
80 StreamVector streams_;
81 };
82
83 class MockDataChannel : public webrtc::DataChannelInterface {
84 public:
MockDataChannel(const std::string & label,const webrtc::DataChannelInit * config)85 MockDataChannel(const std::string& label,
86 const webrtc::DataChannelInit* config)
87 : label_(label),
88 reliable_(config->reliable),
89 state_(webrtc::DataChannelInterface::kConnecting),
90 config_(*config) {
91 }
92
RegisterObserver(webrtc::DataChannelObserver * observer)93 virtual void RegisterObserver(
94 webrtc::DataChannelObserver* observer) OVERRIDE {
95 }
96
UnregisterObserver()97 virtual void UnregisterObserver() OVERRIDE {
98 }
99
label() const100 virtual std::string label() const OVERRIDE {
101 return label_;
102 }
103
reliable() const104 virtual bool reliable() const OVERRIDE {
105 return reliable_;
106 }
107
ordered() const108 virtual bool ordered() const OVERRIDE {
109 return config_.ordered;
110 }
111
maxRetransmitTime() const112 virtual unsigned short maxRetransmitTime() const OVERRIDE {
113 return config_.maxRetransmitTime;
114 }
115
maxRetransmits() const116 virtual unsigned short maxRetransmits() const OVERRIDE {
117 return config_.maxRetransmits;
118 }
119
protocol() const120 virtual std::string protocol() const OVERRIDE {
121 return config_.protocol;
122 }
123
negotiated() const124 virtual bool negotiated() const OVERRIDE {
125 return config_.negotiated;
126 }
127
id() const128 virtual int id() const OVERRIDE {
129 NOTIMPLEMENTED();
130 return 0;
131 }
132
state() const133 virtual DataState state() const OVERRIDE {
134 return state_;
135 }
136
buffered_amount() const137 virtual uint64 buffered_amount() const OVERRIDE {
138 NOTIMPLEMENTED();
139 return 0;
140 }
141
Close()142 virtual void Close() OVERRIDE {
143 state_ = webrtc::DataChannelInterface::kClosing;
144 }
145
Send(const webrtc::DataBuffer & buffer)146 virtual bool Send(const webrtc::DataBuffer& buffer) OVERRIDE {
147 return state_ == webrtc::DataChannelInterface::kOpen;
148 }
149
150 protected:
~MockDataChannel()151 virtual ~MockDataChannel() {}
152
153 private:
154 std::string label_;
155 bool reliable_;
156 webrtc::DataChannelInterface::DataState state_;
157 webrtc::DataChannelInit config_;
158 };
159
160 class MockDtmfSender : public DtmfSenderInterface {
161 public:
MockDtmfSender(AudioTrackInterface * track)162 explicit MockDtmfSender(AudioTrackInterface* track)
163 : track_(track),
164 observer_(NULL),
165 duration_(0),
166 inter_tone_gap_(0) {}
RegisterObserver(DtmfSenderObserverInterface * observer)167 virtual void RegisterObserver(
168 DtmfSenderObserverInterface* observer) OVERRIDE {
169 observer_ = observer;
170 }
UnregisterObserver()171 virtual void UnregisterObserver() OVERRIDE {
172 observer_ = NULL;
173 }
CanInsertDtmf()174 virtual bool CanInsertDtmf() OVERRIDE {
175 return true;
176 }
InsertDtmf(const std::string & tones,int duration,int inter_tone_gap)177 virtual bool InsertDtmf(const std::string& tones, int duration,
178 int inter_tone_gap) OVERRIDE {
179 tones_ = tones;
180 duration_ = duration;
181 inter_tone_gap_ = inter_tone_gap;
182 return true;
183 }
track() const184 virtual const AudioTrackInterface* track() const OVERRIDE {
185 return track_.get();
186 }
tones() const187 virtual std::string tones() const OVERRIDE {
188 return tones_;
189 }
duration() const190 virtual int duration() const OVERRIDE { return duration_; }
inter_tone_gap() const191 virtual int inter_tone_gap() const OVERRIDE { return inter_tone_gap_; }
192
193 protected:
~MockDtmfSender()194 virtual ~MockDtmfSender() {}
195
196 private:
197 rtc::scoped_refptr<AudioTrackInterface> track_;
198 DtmfSenderObserverInterface* observer_;
199 std::string tones_;
200 int duration_;
201 int inter_tone_gap_;
202 };
203
204 const char MockPeerConnectionImpl::kDummyOffer[] = "dummy offer";
205 const char MockPeerConnectionImpl::kDummyAnswer[] = "dummy answer";
206
MockPeerConnectionImpl(MockPeerConnectionDependencyFactory * factory)207 MockPeerConnectionImpl::MockPeerConnectionImpl(
208 MockPeerConnectionDependencyFactory* factory)
209 : dependency_factory_(factory),
210 local_streams_(new rtc::RefCountedObject<MockStreamCollection>),
211 remote_streams_(new rtc::RefCountedObject<MockStreamCollection>),
212 hint_audio_(false),
213 hint_video_(false),
214 getstats_result_(true),
215 sdp_mline_index_(-1) {
216 ON_CALL(*this, SetLocalDescription(_, _)).WillByDefault(testing::Invoke(
217 this, &MockPeerConnectionImpl::SetLocalDescriptionWorker));
218 ON_CALL(*this, SetRemoteDescription(_, _)).WillByDefault(testing::Invoke(
219 this, &MockPeerConnectionImpl::SetRemoteDescriptionWorker));
220 }
221
~MockPeerConnectionImpl()222 MockPeerConnectionImpl::~MockPeerConnectionImpl() {}
223
224 rtc::scoped_refptr<webrtc::StreamCollectionInterface>
local_streams()225 MockPeerConnectionImpl::local_streams() {
226 return local_streams_;
227 }
228
229 rtc::scoped_refptr<webrtc::StreamCollectionInterface>
remote_streams()230 MockPeerConnectionImpl::remote_streams() {
231 return remote_streams_;
232 }
233
AddStream(MediaStreamInterface * local_stream,const MediaConstraintsInterface * constraints)234 bool MockPeerConnectionImpl::AddStream(
235 MediaStreamInterface* local_stream,
236 const MediaConstraintsInterface* constraints) {
237 DCHECK(stream_label_.empty());
238 stream_label_ = local_stream->label();
239 local_streams_->AddStream(local_stream);
240 return true;
241 }
242
RemoveStream(MediaStreamInterface * local_stream)243 void MockPeerConnectionImpl::RemoveStream(
244 MediaStreamInterface* local_stream) {
245 DCHECK_EQ(stream_label_, local_stream->label());
246 stream_label_.clear();
247 local_streams_->RemoveStream(local_stream);
248 }
249
250 rtc::scoped_refptr<DtmfSenderInterface>
CreateDtmfSender(AudioTrackInterface * track)251 MockPeerConnectionImpl::CreateDtmfSender(AudioTrackInterface* track) {
252 if (!track) {
253 return NULL;
254 }
255 return new rtc::RefCountedObject<MockDtmfSender>(track);
256 }
257
258 rtc::scoped_refptr<webrtc::DataChannelInterface>
CreateDataChannel(const std::string & label,const webrtc::DataChannelInit * config)259 MockPeerConnectionImpl::CreateDataChannel(const std::string& label,
260 const webrtc::DataChannelInit* config) {
261 return new rtc::RefCountedObject<MockDataChannel>(label, config);
262 }
263
GetStats(webrtc::StatsObserver * observer,webrtc::MediaStreamTrackInterface * track,StatsOutputLevel level)264 bool MockPeerConnectionImpl::GetStats(
265 webrtc::StatsObserver* observer,
266 webrtc::MediaStreamTrackInterface* track,
267 StatsOutputLevel level) {
268 if (!getstats_result_)
269 return false;
270
271 DCHECK_EQ(kStatsOutputLevelStandard, level);
272 webrtc::StatsReport report1, report2;
273 report1.id = "1234";
274 report1.type = "ssrc";
275 report1.timestamp = 42;
276 report1.values.push_back(
277 webrtc::StatsReport::Value(
278 webrtc::StatsReport::kStatsValueNameFingerprint,
279 "trackvalue"));
280
281 webrtc::StatsReports reports;
282 reports.push_back(&report1);
283
284 // If selector is given, we pass back one report.
285 // If selector is not given, we pass back two.
286 if (!track) {
287 report2.id = "nontrack";
288 report2.type = "generic";
289 report2.timestamp = 44;
290 report2.values.push_back(
291 webrtc::StatsReport::Value(
292 webrtc::StatsReport::kStatsValueNameFingerprintAlgorithm,
293 "somevalue"));
294 reports.push_back(&report2);
295 }
296
297 // Note that the callback is synchronous, not asynchronous; it will
298 // happen before the request call completes.
299 observer->OnComplete(reports);
300
301 return true;
302 }
303
304 const webrtc::SessionDescriptionInterface*
local_description() const305 MockPeerConnectionImpl::local_description() const {
306 return local_desc_.get();
307 }
308
309 const webrtc::SessionDescriptionInterface*
remote_description() const310 MockPeerConnectionImpl::remote_description() const {
311 return remote_desc_.get();
312 }
313
AddRemoteStream(MediaStreamInterface * stream)314 void MockPeerConnectionImpl::AddRemoteStream(MediaStreamInterface* stream) {
315 remote_streams_->AddStream(stream);
316 }
317
CreateOffer(CreateSessionDescriptionObserver * observer,const MediaConstraintsInterface * constraints)318 void MockPeerConnectionImpl::CreateOffer(
319 CreateSessionDescriptionObserver* observer,
320 const MediaConstraintsInterface* constraints) {
321 DCHECK(observer);
322 created_sessiondescription_.reset(
323 dependency_factory_->CreateSessionDescription("unknown", kDummyOffer,
324 NULL));
325 }
326
CreateAnswer(CreateSessionDescriptionObserver * observer,const MediaConstraintsInterface * constraints)327 void MockPeerConnectionImpl::CreateAnswer(
328 CreateSessionDescriptionObserver* observer,
329 const MediaConstraintsInterface* constraints) {
330 DCHECK(observer);
331 created_sessiondescription_.reset(
332 dependency_factory_->CreateSessionDescription("unknown", kDummyAnswer,
333 NULL));
334 }
335
SetLocalDescriptionWorker(SetSessionDescriptionObserver * observer,SessionDescriptionInterface * desc)336 void MockPeerConnectionImpl::SetLocalDescriptionWorker(
337 SetSessionDescriptionObserver* observer,
338 SessionDescriptionInterface* desc) {
339 desc->ToString(&description_sdp_);
340 local_desc_.reset(desc);
341 }
342
SetRemoteDescriptionWorker(SetSessionDescriptionObserver * observer,SessionDescriptionInterface * desc)343 void MockPeerConnectionImpl::SetRemoteDescriptionWorker(
344 SetSessionDescriptionObserver* observer,
345 SessionDescriptionInterface* desc) {
346 desc->ToString(&description_sdp_);
347 remote_desc_.reset(desc);
348 }
349
UpdateIce(const IceServers & configuration,const MediaConstraintsInterface * constraints)350 bool MockPeerConnectionImpl::UpdateIce(
351 const IceServers& configuration,
352 const MediaConstraintsInterface* constraints) {
353 return true;
354 }
355
AddIceCandidate(const IceCandidateInterface * candidate)356 bool MockPeerConnectionImpl::AddIceCandidate(
357 const IceCandidateInterface* candidate) {
358 sdp_mid_ = candidate->sdp_mid();
359 sdp_mline_index_ = candidate->sdp_mline_index();
360 return candidate->ToString(&ice_sdp_);
361 }
362
RegisterUMAObserver(webrtc::UMAObserver * observer)363 void MockPeerConnectionImpl::RegisterUMAObserver(
364 webrtc::UMAObserver* observer) {
365 NOTIMPLEMENTED();
366 }
367
368 } // namespace content
369