Fix build break
[chromium-blink-merge.git] / content / renderer / media / mock_peer_connection_impl.cc
blobdb36c3071aff078f02749e07461ab24cf14d9cd1
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_media_stream_dependency_factory.h"
6 #include "content/renderer/media/mock_peer_connection_impl.h"
8 #include <vector>
10 #include "base/logging.h"
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;
24 namespace content {
26 class MockStreamCollection : public webrtc::StreamCollectionInterface {
27 public:
28 virtual size_t count() OVERRIDE {
29 return streams_.size();
31 virtual MediaStreamInterface* at(size_t index) OVERRIDE {
32 return streams_[index];
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];
39 return NULL;
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;
49 return NULL;
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;
59 return NULL;
61 void AddStream(MediaStreamInterface* stream) {
62 streams_.push_back(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;
74 protected:
75 virtual ~MockStreamCollection() {}
77 private:
78 typedef std::vector<talk_base::scoped_refptr<MediaStreamInterface> >
79 StreamVector;
80 StreamVector streams_;
83 class MockDataChannel : public webrtc::DataChannelInterface {
84 public:
85 MockDataChannel(const std::string& label,
86 const webrtc::DataChannelInit* config)
87 : label_(label),
88 reliable_(config->reliable),
89 state_(webrtc::DataChannelInterface::kConnecting) {
92 virtual void RegisterObserver(
93 webrtc::DataChannelObserver* observer) OVERRIDE {
96 virtual void UnregisterObserver() OVERRIDE {
99 virtual std::string label() const OVERRIDE {
100 return label_;
103 virtual bool reliable() const OVERRIDE {
104 return reliable_;
107 virtual DataState state() const OVERRIDE {
108 return state_;
111 virtual uint64 buffered_amount() const OVERRIDE {
112 NOTIMPLEMENTED();
113 return 0;
116 virtual void Close() OVERRIDE {
117 state_ = webrtc::DataChannelInterface::kClosing;
120 virtual bool Send(const webrtc::DataBuffer& buffer) OVERRIDE {
121 return state_ == webrtc::DataChannelInterface::kOpen;
124 protected:
125 virtual ~MockDataChannel() {}
127 private:
128 std::string label_;
129 bool reliable_;
130 webrtc::DataChannelInterface::DataState state_;
133 class MockDtmfSender : public DtmfSenderInterface {
134 public:
135 explicit MockDtmfSender(AudioTrackInterface* track)
136 : track_(track),
137 observer_(NULL),
138 duration_(0),
139 inter_tone_gap_(0) {}
140 virtual void RegisterObserver(
141 DtmfSenderObserverInterface* observer) OVERRIDE {
142 observer_ = observer;
144 virtual void UnregisterObserver() OVERRIDE {
145 observer_ = NULL;
147 virtual bool CanInsertDtmf() OVERRIDE {
148 return true;
150 virtual bool InsertDtmf(const std::string& tones, int duration,
151 int inter_tone_gap) OVERRIDE {
152 tones_ = tones;
153 duration_ = duration;
154 inter_tone_gap_ = inter_tone_gap;
155 return true;
157 virtual const AudioTrackInterface* track() const OVERRIDE {
158 return track_.get();
160 virtual std::string tones() const OVERRIDE {
161 return tones_;
163 virtual int duration() const OVERRIDE { return duration_; }
164 virtual int inter_tone_gap() const OVERRIDE { return inter_tone_gap_; }
166 protected:
167 virtual ~MockDtmfSender() {}
169 private:
170 talk_base::scoped_refptr<AudioTrackInterface> track_;
171 DtmfSenderObserverInterface* observer_;
172 std::string tones_;
173 int duration_;
174 int inter_tone_gap_;
177 const char MockPeerConnectionImpl::kDummyOffer[] = "dummy offer";
178 const char MockPeerConnectionImpl::kDummyAnswer[] = "dummy answer";
180 MockPeerConnectionImpl::MockPeerConnectionImpl(
181 MockMediaStreamDependencyFactory* factory)
182 : dependency_factory_(factory),
183 local_streams_(new talk_base::RefCountedObject<MockStreamCollection>),
184 remote_streams_(new talk_base::RefCountedObject<MockStreamCollection>),
185 hint_audio_(false),
186 hint_video_(false),
187 getstats_result_(true),
188 sdp_mline_index_(-1) {
189 ON_CALL(*this, SetLocalDescription(_, _)).WillByDefault(testing::Invoke(
190 this, &MockPeerConnectionImpl::SetLocalDescriptionWorker));
191 ON_CALL(*this, SetRemoteDescription(_, _)).WillByDefault(testing::Invoke(
192 this, &MockPeerConnectionImpl::SetRemoteDescriptionWorker));
195 MockPeerConnectionImpl::~MockPeerConnectionImpl() {}
197 talk_base::scoped_refptr<webrtc::StreamCollectionInterface>
198 MockPeerConnectionImpl::local_streams() {
199 return local_streams_;
202 talk_base::scoped_refptr<webrtc::StreamCollectionInterface>
203 MockPeerConnectionImpl::remote_streams() {
204 return remote_streams_;
207 bool MockPeerConnectionImpl::AddStream(
208 MediaStreamInterface* local_stream,
209 const MediaConstraintsInterface* constraints) {
210 DCHECK(stream_label_.empty());
211 stream_label_ = local_stream->label();
212 local_streams_->AddStream(local_stream);
213 return true;
216 void MockPeerConnectionImpl::RemoveStream(
217 MediaStreamInterface* local_stream) {
218 DCHECK_EQ(stream_label_, local_stream->label());
219 stream_label_.clear();
220 local_streams_->RemoveStream(local_stream);
223 talk_base::scoped_refptr<DtmfSenderInterface>
224 MockPeerConnectionImpl::CreateDtmfSender(AudioTrackInterface* track) {
225 if (!track) {
226 return NULL;
228 return new talk_base::RefCountedObject<MockDtmfSender>(track);
231 talk_base::scoped_refptr<webrtc::DataChannelInterface>
232 MockPeerConnectionImpl::CreateDataChannel(const std::string& label,
233 const webrtc::DataChannelInit* config) {
234 return new talk_base::RefCountedObject<MockDataChannel>(label, config);
237 bool MockPeerConnectionImpl::GetStats(
238 webrtc::StatsObserver* observer,
239 webrtc::MediaStreamTrackInterface* track) {
240 if (!getstats_result_)
241 return false;
243 std::vector<webrtc::StatsReport> reports;
244 webrtc::StatsReport report;
245 report.id = "1234";
246 report.type = "ssrc";
247 report.timestamp = 42;
248 webrtc::StatsReport::Value value;
249 value.name = "trackname";
250 value.value = "trackvalue";
251 report.values.push_back(value);
252 reports.push_back(report);
253 // If selector is given, we pass back one report.
254 // If selector is not given, we pass back two.
255 if (!track) {
256 report.id = "nontrack";
257 report.type = "generic";
258 report.timestamp = 44;
259 value.name = "somename";
260 value.value = "somevalue";
261 report.values.push_back(value);
262 reports.push_back(report);
264 // Note that the callback is synchronous, not asynchronous; it will
265 // happen before the request call completes.
266 observer->OnComplete(reports);
267 return true;
270 const webrtc::SessionDescriptionInterface*
271 MockPeerConnectionImpl::local_description() const {
272 return local_desc_.get();
275 const webrtc::SessionDescriptionInterface*
276 MockPeerConnectionImpl::remote_description() const {
277 return remote_desc_.get();
280 void MockPeerConnectionImpl::AddRemoteStream(MediaStreamInterface* stream) {
281 remote_streams_->AddStream(stream);
284 void MockPeerConnectionImpl::CreateOffer(
285 CreateSessionDescriptionObserver* observer,
286 const MediaConstraintsInterface* constraints) {
287 DCHECK(observer);
288 created_sessiondescription_.reset(
289 dependency_factory_->CreateSessionDescription("unknown", kDummyOffer,
290 NULL));
293 void MockPeerConnectionImpl::CreateAnswer(
294 CreateSessionDescriptionObserver* observer,
295 const MediaConstraintsInterface* constraints) {
296 DCHECK(observer);
297 created_sessiondescription_.reset(
298 dependency_factory_->CreateSessionDescription("unknown", kDummyAnswer,
299 NULL));
302 void MockPeerConnectionImpl::SetLocalDescriptionWorker(
303 SetSessionDescriptionObserver* observer,
304 SessionDescriptionInterface* desc) {
305 desc->ToString(&description_sdp_);
306 local_desc_.reset(desc);
309 void MockPeerConnectionImpl::SetRemoteDescriptionWorker(
310 SetSessionDescriptionObserver* observer,
311 SessionDescriptionInterface* desc) {
312 desc->ToString(&description_sdp_);
313 remote_desc_.reset(desc);
316 bool MockPeerConnectionImpl::UpdateIce(
317 const IceServers& configuration,
318 const MediaConstraintsInterface* constraints) {
319 return true;
322 bool MockPeerConnectionImpl::AddIceCandidate(
323 const IceCandidateInterface* candidate) {
324 sdp_mid_ = candidate->sdp_mid();
325 sdp_mline_index_ = candidate->sdp_mline_index();
326 return candidate->ToString(&ice_sdp_);
329 } // namespace content