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"
10 #include "base/logging.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
<talk_base::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
) {
92 virtual void RegisterObserver(
93 webrtc::DataChannelObserver
* observer
) OVERRIDE
{
96 virtual void UnregisterObserver() OVERRIDE
{
99 virtual std::string
label() const OVERRIDE
{
103 virtual bool reliable() const OVERRIDE
{
107 virtual DataState
state() const OVERRIDE
{
111 virtual uint64
buffered_amount() const OVERRIDE
{
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
;
125 virtual ~MockDataChannel() {}
130 webrtc::DataChannelInterface::DataState state_
;
133 class MockDtmfSender
: public DtmfSenderInterface
{
135 explicit MockDtmfSender(AudioTrackInterface
* track
)
139 inter_tone_gap_(0) {}
140 virtual void RegisterObserver(
141 DtmfSenderObserverInterface
* observer
) OVERRIDE
{
142 observer_
= observer
;
144 virtual void UnregisterObserver() OVERRIDE
{
147 virtual bool CanInsertDtmf() OVERRIDE
{
150 virtual bool InsertDtmf(const std::string
& tones
, int duration
,
151 int inter_tone_gap
) OVERRIDE
{
153 duration_
= duration
;
154 inter_tone_gap_
= inter_tone_gap
;
157 virtual const AudioTrackInterface
* track() const OVERRIDE
{
160 virtual std::string
tones() const OVERRIDE
{
163 virtual int duration() const OVERRIDE
{ return duration_
; }
164 virtual int inter_tone_gap() const OVERRIDE
{ return inter_tone_gap_
; }
167 virtual ~MockDtmfSender() {}
170 talk_base::scoped_refptr
<AudioTrackInterface
> track_
;
171 DtmfSenderObserverInterface
* observer_
;
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
>),
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
);
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
) {
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_
)
243 std::vector
<webrtc::StatsReport
> reports
;
244 webrtc::StatsReport report
;
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.
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
);
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
) {
288 created_sessiondescription_
.reset(
289 dependency_factory_
->CreateSessionDescription("unknown", kDummyOffer
,
293 void MockPeerConnectionImpl::CreateAnswer(
294 CreateSessionDescriptionObserver
* observer
,
295 const MediaConstraintsInterface
* constraints
) {
297 created_sessiondescription_
.reset(
298 dependency_factory_
->CreateSessionDescription("unknown", kDummyAnswer
,
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
) {
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