Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / media / mock_peer_connection_impl.cc
blob4be2d7781b2c0f9f3c6f32a5a2bf5fd556723328
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.
5 #include "content/renderer/media/mock_peer_connection_impl.h"
7 #include <vector>
9 #include "base/logging.h"
10 #include "content/renderer/media/mock_data_channel_impl.h"
11 #include "content/renderer/media/webrtc/mock_peer_connection_dependency_factory.h"
13 using testing::_;
14 using webrtc::AudioTrackInterface;
15 using webrtc::CreateSessionDescriptionObserver;
16 using webrtc::DtmfSenderInterface;
17 using webrtc::DtmfSenderObserverInterface;
18 using webrtc::IceCandidateInterface;
19 using webrtc::MediaConstraintsInterface;
20 using webrtc::MediaStreamInterface;
21 using webrtc::PeerConnectionInterface;
22 using webrtc::SessionDescriptionInterface;
23 using webrtc::SetSessionDescriptionObserver;
25 namespace content {
27 class MockStreamCollection : public webrtc::StreamCollectionInterface {
28 public:
29 size_t count() override { return streams_.size(); }
30 MediaStreamInterface* at(size_t index) override { return streams_[index]; }
31 MediaStreamInterface* find(const std::string& label) override {
32 for (size_t i = 0; i < streams_.size(); ++i) {
33 if (streams_[i]->label() == label)
34 return streams_[i];
36 return NULL;
38 webrtc::MediaStreamTrackInterface* FindAudioTrack(
39 const std::string& id) override {
40 for (size_t i = 0; i < streams_.size(); ++i) {
41 webrtc::MediaStreamTrackInterface* track =
42 streams_.at(i)->FindAudioTrack(id);
43 if (track)
44 return track;
46 return NULL;
48 webrtc::MediaStreamTrackInterface* FindVideoTrack(
49 const std::string& id) override {
50 for (size_t i = 0; i < streams_.size(); ++i) {
51 webrtc::MediaStreamTrackInterface* track =
52 streams_.at(i)->FindVideoTrack(id);
53 if (track)
54 return track;
56 return NULL;
58 void AddStream(MediaStreamInterface* stream) {
59 streams_.push_back(stream);
61 void RemoveStream(MediaStreamInterface* stream) {
62 StreamVector::iterator it = streams_.begin();
63 for (; it != streams_.end(); ++it) {
64 if (it->get() == stream) {
65 streams_.erase(it);
66 break;
71 protected:
72 ~MockStreamCollection() override {}
74 private:
75 typedef std::vector<rtc::scoped_refptr<MediaStreamInterface> >
76 StreamVector;
77 StreamVector streams_;
80 class MockDtmfSender : public DtmfSenderInterface {
81 public:
82 explicit MockDtmfSender(AudioTrackInterface* track)
83 : track_(track),
84 observer_(NULL),
85 duration_(0),
86 inter_tone_gap_(0) {}
87 void RegisterObserver(DtmfSenderObserverInterface* observer) override {
88 observer_ = observer;
90 void UnregisterObserver() override { observer_ = NULL; }
91 bool CanInsertDtmf() override { return true; }
92 bool InsertDtmf(const std::string& tones,
93 int duration,
94 int inter_tone_gap) override {
95 tones_ = tones;
96 duration_ = duration;
97 inter_tone_gap_ = inter_tone_gap;
98 return true;
100 const AudioTrackInterface* track() const override { return track_.get(); }
101 std::string tones() const override { return tones_; }
102 int duration() const override { return duration_; }
103 int inter_tone_gap() const override { return inter_tone_gap_; }
105 protected:
106 ~MockDtmfSender() override {}
108 private:
109 rtc::scoped_refptr<AudioTrackInterface> track_;
110 DtmfSenderObserverInterface* observer_;
111 std::string tones_;
112 int duration_;
113 int inter_tone_gap_;
116 const char MockPeerConnectionImpl::kDummyOffer[] = "dummy offer";
117 const char MockPeerConnectionImpl::kDummyAnswer[] = "dummy answer";
119 MockPeerConnectionImpl::MockPeerConnectionImpl(
120 MockPeerConnectionDependencyFactory* factory,
121 webrtc::PeerConnectionObserver* observer)
122 : dependency_factory_(factory),
123 local_streams_(new rtc::RefCountedObject<MockStreamCollection>),
124 remote_streams_(new rtc::RefCountedObject<MockStreamCollection>),
125 hint_audio_(false),
126 hint_video_(false),
127 getstats_result_(true),
128 sdp_mline_index_(-1),
129 observer_(observer) {
130 ON_CALL(*this, SetLocalDescription(_, _)).WillByDefault(testing::Invoke(
131 this, &MockPeerConnectionImpl::SetLocalDescriptionWorker));
132 ON_CALL(*this, SetRemoteDescription(_, _)).WillByDefault(testing::Invoke(
133 this, &MockPeerConnectionImpl::SetRemoteDescriptionWorker));
136 MockPeerConnectionImpl::~MockPeerConnectionImpl() {}
138 rtc::scoped_refptr<webrtc::StreamCollectionInterface>
139 MockPeerConnectionImpl::local_streams() {
140 return local_streams_;
143 rtc::scoped_refptr<webrtc::StreamCollectionInterface>
144 MockPeerConnectionImpl::remote_streams() {
145 return remote_streams_;
148 bool MockPeerConnectionImpl::AddStream(MediaStreamInterface* local_stream) {
149 DCHECK(stream_label_.empty());
150 stream_label_ = local_stream->label();
151 local_streams_->AddStream(local_stream);
152 return true;
155 void MockPeerConnectionImpl::RemoveStream(
156 MediaStreamInterface* local_stream) {
157 DCHECK_EQ(stream_label_, local_stream->label());
158 stream_label_.clear();
159 local_streams_->RemoveStream(local_stream);
162 rtc::scoped_refptr<DtmfSenderInterface>
163 MockPeerConnectionImpl::CreateDtmfSender(AudioTrackInterface* track) {
164 if (!track) {
165 return NULL;
167 return new rtc::RefCountedObject<MockDtmfSender>(track);
170 rtc::scoped_refptr<webrtc::DataChannelInterface>
171 MockPeerConnectionImpl::CreateDataChannel(const std::string& label,
172 const webrtc::DataChannelInit* config) {
173 return new rtc::RefCountedObject<MockDataChannel>(label, config);
176 bool MockPeerConnectionImpl::GetStats(
177 webrtc::StatsObserver* observer,
178 webrtc::MediaStreamTrackInterface* track,
179 StatsOutputLevel level) {
180 if (!getstats_result_)
181 return false;
183 DCHECK_EQ(kStatsOutputLevelStandard, level);
184 webrtc::StatsReport report1(webrtc::StatsReport::NewTypedId(
185 webrtc::StatsReport::kStatsReportTypeSsrc, "1234"));
186 webrtc::StatsReport report2(webrtc::StatsReport::NewTypedId(
187 webrtc::StatsReport::kStatsReportTypeSession, "nontrack"));
188 report1.set_timestamp(42);
189 report1.AddString(webrtc::StatsReport::kStatsValueNameFingerprint,
190 "trackvalue");
192 webrtc::StatsReports reports;
193 reports.push_back(&report1);
195 // If selector is given, we pass back one report.
196 // If selector is not given, we pass back two.
197 if (!track) {
198 report2.set_timestamp(44);
199 report2.AddString(webrtc::StatsReport::kStatsValueNameFingerprintAlgorithm,
200 "somevalue");
201 reports.push_back(&report2);
204 // Note that the callback is synchronous, not asynchronous; it will
205 // happen before the request call completes.
206 observer->OnComplete(reports);
208 return true;
211 const webrtc::SessionDescriptionInterface*
212 MockPeerConnectionImpl::local_description() const {
213 return local_desc_.get();
216 const webrtc::SessionDescriptionInterface*
217 MockPeerConnectionImpl::remote_description() const {
218 return remote_desc_.get();
221 void MockPeerConnectionImpl::AddRemoteStream(MediaStreamInterface* stream) {
222 remote_streams_->AddStream(stream);
225 void MockPeerConnectionImpl::CreateOffer(
226 CreateSessionDescriptionObserver* observer,
227 const MediaConstraintsInterface* constraints) {
228 DCHECK(observer);
229 created_sessiondescription_.reset(
230 dependency_factory_->CreateSessionDescription("unknown", kDummyOffer,
231 NULL));
234 void MockPeerConnectionImpl::CreateAnswer(
235 CreateSessionDescriptionObserver* observer,
236 const MediaConstraintsInterface* constraints) {
237 DCHECK(observer);
238 created_sessiondescription_.reset(
239 dependency_factory_->CreateSessionDescription("unknown", kDummyAnswer,
240 NULL));
243 void MockPeerConnectionImpl::SetLocalDescriptionWorker(
244 SetSessionDescriptionObserver* observer,
245 SessionDescriptionInterface* desc) {
246 desc->ToString(&description_sdp_);
247 local_desc_.reset(desc);
250 void MockPeerConnectionImpl::SetRemoteDescriptionWorker(
251 SetSessionDescriptionObserver* observer,
252 SessionDescriptionInterface* desc) {
253 desc->ToString(&description_sdp_);
254 remote_desc_.reset(desc);
257 bool MockPeerConnectionImpl::UpdateIce(
258 const IceServers& configuration,
259 const MediaConstraintsInterface* constraints) {
260 return true;
263 bool MockPeerConnectionImpl::AddIceCandidate(
264 const IceCandidateInterface* candidate) {
265 sdp_mid_ = candidate->sdp_mid();
266 sdp_mline_index_ = candidate->sdp_mline_index();
267 return candidate->ToString(&ice_sdp_);
270 void MockPeerConnectionImpl::RegisterUMAObserver(
271 webrtc::UMAObserver* observer) {
272 NOTIMPLEMENTED();
275 } // namespace content