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/mock_data_channel_impl.h"
11 #include "content/renderer/media/webrtc/mock_peer_connection_dependency_factory.h"
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
;
27 class MockStreamCollection
: public webrtc::StreamCollectionInterface
{
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
)
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
);
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
);
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
) {
72 ~MockStreamCollection() override
{}
75 typedef std::vector
<rtc::scoped_refptr
<MediaStreamInterface
> >
77 StreamVector streams_
;
80 class MockDtmfSender
: public DtmfSenderInterface
{
82 explicit MockDtmfSender(AudioTrackInterface
* track
)
87 void RegisterObserver(DtmfSenderObserverInterface
* observer
) override
{
90 void UnregisterObserver() override
{ observer_
= NULL
; }
91 bool CanInsertDtmf() override
{ return true; }
92 bool InsertDtmf(const std::string
& tones
,
94 int inter_tone_gap
) override
{
97 inter_tone_gap_
= inter_tone_gap
;
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_
; }
106 ~MockDtmfSender() override
{}
109 rtc::scoped_refptr
<AudioTrackInterface
> track_
;
110 DtmfSenderObserverInterface
* observer_
;
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
>),
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
);
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
) {
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_
)
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
,
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.
198 report2
.set_timestamp(44);
199 report2
.AddString(webrtc::StatsReport::kStatsValueNameFingerprintAlgorithm
,
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
);
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
) {
229 created_sessiondescription_
.reset(
230 dependency_factory_
->CreateSessionDescription("unknown", kDummyOffer
,
234 void MockPeerConnectionImpl::CreateAnswer(
235 CreateSessionDescriptionObserver
* observer
,
236 const MediaConstraintsInterface
* constraints
) {
238 created_sessiondescription_
.reset(
239 dependency_factory_
->CreateSessionDescription("unknown", kDummyAnswer
,
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
) {
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
) {
275 } // namespace content