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"
9 #include "base/logging.h"
10 #include "content/renderer/media/webrtc/mock_peer_connection_dependency_factory.h"
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
;
26 class MockStreamCollection
: public webrtc::StreamCollectionInterface
{
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
)
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
);
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
);
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
) {
75 virtual ~MockStreamCollection() {}
78 typedef std::vector
<rtc::scoped_refptr
<MediaStreamInterface
> >
80 StreamVector streams_
;
83 class MockDataChannel
: public webrtc::DataChannelInterface
{
85 MockDataChannel(const std::string
& label
,
86 const webrtc::DataChannelInit
* config
)
88 reliable_(config
->reliable
),
89 state_(webrtc::DataChannelInterface::kConnecting
),
93 virtual void RegisterObserver(
94 webrtc::DataChannelObserver
* observer
) OVERRIDE
{
97 virtual void UnregisterObserver() OVERRIDE
{
100 virtual std::string
label() const OVERRIDE
{
104 virtual bool reliable() const OVERRIDE
{
108 virtual bool ordered() const OVERRIDE
{
109 return config_
.ordered
;
112 virtual unsigned short maxRetransmitTime() const OVERRIDE
{
113 return config_
.maxRetransmitTime
;
116 virtual unsigned short maxRetransmits() const OVERRIDE
{
117 return config_
.maxRetransmits
;
120 virtual std::string
protocol() const OVERRIDE
{
121 return config_
.protocol
;
124 virtual bool negotiated() const OVERRIDE
{
125 return config_
.negotiated
;
128 virtual int id() const OVERRIDE
{
133 virtual DataState
state() const OVERRIDE
{
137 virtual uint64
buffered_amount() const OVERRIDE
{
142 virtual void Close() OVERRIDE
{
143 state_
= webrtc::DataChannelInterface::kClosing
;
146 virtual bool Send(const webrtc::DataBuffer
& buffer
) OVERRIDE
{
147 return state_
== webrtc::DataChannelInterface::kOpen
;
151 virtual ~MockDataChannel() {}
156 webrtc::DataChannelInterface::DataState state_
;
157 webrtc::DataChannelInit config_
;
160 class MockDtmfSender
: public DtmfSenderInterface
{
162 explicit MockDtmfSender(AudioTrackInterface
* track
)
166 inter_tone_gap_(0) {}
167 virtual void RegisterObserver(
168 DtmfSenderObserverInterface
* observer
) OVERRIDE
{
169 observer_
= observer
;
171 virtual void UnregisterObserver() OVERRIDE
{
174 virtual bool CanInsertDtmf() OVERRIDE
{
177 virtual bool InsertDtmf(const std::string
& tones
, int duration
,
178 int inter_tone_gap
) OVERRIDE
{
180 duration_
= duration
;
181 inter_tone_gap_
= inter_tone_gap
;
184 virtual const AudioTrackInterface
* track() const OVERRIDE
{
187 virtual std::string
tones() const OVERRIDE
{
190 virtual int duration() const OVERRIDE
{ return duration_
; }
191 virtual int inter_tone_gap() const OVERRIDE
{ return inter_tone_gap_
; }
194 virtual ~MockDtmfSender() {}
197 rtc::scoped_refptr
<AudioTrackInterface
> track_
;
198 DtmfSenderObserverInterface
* observer_
;
204 const char MockPeerConnectionImpl::kDummyOffer
[] = "dummy offer";
205 const char MockPeerConnectionImpl::kDummyAnswer
[] = "dummy answer";
207 MockPeerConnectionImpl::MockPeerConnectionImpl(
208 MockPeerConnectionDependencyFactory
* factory
)
209 : dependency_factory_(factory
),
210 local_streams_(new rtc::RefCountedObject
<MockStreamCollection
>),
211 remote_streams_(new rtc::RefCountedObject
<MockStreamCollection
>),
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
));
222 MockPeerConnectionImpl::~MockPeerConnectionImpl() {}
224 rtc::scoped_refptr
<webrtc::StreamCollectionInterface
>
225 MockPeerConnectionImpl::local_streams() {
226 return local_streams_
;
229 rtc::scoped_refptr
<webrtc::StreamCollectionInterface
>
230 MockPeerConnectionImpl::remote_streams() {
231 return remote_streams_
;
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
);
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
);
250 rtc::scoped_refptr
<DtmfSenderInterface
>
251 MockPeerConnectionImpl::CreateDtmfSender(AudioTrackInterface
* track
) {
255 return new rtc::RefCountedObject
<MockDtmfSender
>(track
);
258 rtc::scoped_refptr
<webrtc::DataChannelInterface
>
259 MockPeerConnectionImpl::CreateDataChannel(const std::string
& label
,
260 const webrtc::DataChannelInit
* config
) {
261 return new rtc::RefCountedObject
<MockDataChannel
>(label
, config
);
264 bool MockPeerConnectionImpl::GetStats(
265 webrtc::StatsObserver
* observer
,
266 webrtc::MediaStreamTrackInterface
* track
,
267 StatsOutputLevel level
) {
268 if (!getstats_result_
)
271 DCHECK_EQ(kStatsOutputLevelStandard
, level
);
272 webrtc::StatsReport report1
, report2
;
274 report1
.type
= "ssrc";
275 report1
.timestamp
= 42;
276 report1
.values
.push_back(
277 webrtc::StatsReport::Value(
278 webrtc::StatsReport::kStatsValueNameFingerprint
,
281 webrtc::StatsReports reports
;
282 reports
.push_back(&report1
);
284 // If selector is given, we pass back one report.
285 // If selector is not given, we pass back two.
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
,
294 reports
.push_back(&report2
);
297 // Note that the callback is synchronous, not asynchronous; it will
298 // happen before the request call completes.
299 observer
->OnComplete(reports
);
304 const webrtc::SessionDescriptionInterface
*
305 MockPeerConnectionImpl::local_description() const {
306 return local_desc_
.get();
309 const webrtc::SessionDescriptionInterface
*
310 MockPeerConnectionImpl::remote_description() const {
311 return remote_desc_
.get();
314 void MockPeerConnectionImpl::AddRemoteStream(MediaStreamInterface
* stream
) {
315 remote_streams_
->AddStream(stream
);
318 void MockPeerConnectionImpl::CreateOffer(
319 CreateSessionDescriptionObserver
* observer
,
320 const MediaConstraintsInterface
* constraints
) {
322 created_sessiondescription_
.reset(
323 dependency_factory_
->CreateSessionDescription("unknown", kDummyOffer
,
327 void MockPeerConnectionImpl::CreateAnswer(
328 CreateSessionDescriptionObserver
* observer
,
329 const MediaConstraintsInterface
* constraints
) {
331 created_sessiondescription_
.reset(
332 dependency_factory_
->CreateSessionDescription("unknown", kDummyAnswer
,
336 void MockPeerConnectionImpl::SetLocalDescriptionWorker(
337 SetSessionDescriptionObserver
* observer
,
338 SessionDescriptionInterface
* desc
) {
339 desc
->ToString(&description_sdp_
);
340 local_desc_
.reset(desc
);
343 void MockPeerConnectionImpl::SetRemoteDescriptionWorker(
344 SetSessionDescriptionObserver
* observer
,
345 SessionDescriptionInterface
* desc
) {
346 desc
->ToString(&description_sdp_
);
347 remote_desc_
.reset(desc
);
350 bool MockPeerConnectionImpl::UpdateIce(
351 const IceServers
& configuration
,
352 const MediaConstraintsInterface
* constraints
) {
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_
);
363 void MockPeerConnectionImpl::RegisterUMAObserver(
364 webrtc::UMAObserver
* observer
) {
368 } // namespace content