1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/renderer/media/mock_media_stream_dependency_factory.h"
#include "base/logging.h"
#include "content/renderer/media/mock_peer_connection_impl.h"
#include "third_party/libjingle/source/talk/app/webrtc/mediastreaminterface.h"
#include "third_party/libjingle/source/talk/base/scoped_ref_ptr.h"
using webrtc::AudioSourceInterface;
using webrtc::AudioTrackInterface;
using webrtc::AudioTrackVector;
using webrtc::IceCandidateCollection;
using webrtc::IceCandidateInterface;
using webrtc::MediaStreamInterface;
using webrtc::ObserverInterface;
using webrtc::SessionDescriptionInterface;
using webrtc::VideoRendererInterface;
using webrtc::VideoSourceInterface;
using webrtc::VideoTrackInterface;
using webrtc::VideoTrackVector;
namespace content {
template <class V>
static typename V::iterator FindTrack(V* vector,
const std::string& track_id) {
typename V::iterator it = vector->begin();
for (; it != vector->end(); ++it) {
if ((*it)->id() == track_id) {
break;
}
}
return it;
};
class MockMediaStream : public webrtc::MediaStreamInterface {
public:
explicit MockMediaStream(const std::string& label)
: label_(label) {
}
virtual bool AddTrack(AudioTrackInterface* track) OVERRIDE {
audio_track_vector_.push_back(track);
return true;
}
virtual bool AddTrack(VideoTrackInterface* track) OVERRIDE {
video_track_vector_.push_back(track);
return true;
}
virtual bool RemoveTrack(AudioTrackInterface* track) OVERRIDE {
NOTIMPLEMENTED();
return false;
}
virtual bool RemoveTrack(VideoTrackInterface* track) OVERRIDE {
NOTIMPLEMENTED();
return false;
}
virtual std::string label() const OVERRIDE { return label_; }
virtual AudioTrackVector GetAudioTracks() OVERRIDE {
return audio_track_vector_;
}
virtual VideoTrackVector GetVideoTracks() OVERRIDE {
return video_track_vector_;
}
virtual talk_base::scoped_refptr<AudioTrackInterface>
FindAudioTrack(const std::string& track_id) OVERRIDE {
AudioTrackVector::iterator it = FindTrack(&audio_track_vector_, track_id);
return it == audio_track_vector_.end() ? NULL : *it;
}
virtual talk_base::scoped_refptr<VideoTrackInterface>
FindVideoTrack(const std::string& track_id) OVERRIDE {
VideoTrackVector::iterator it = FindTrack(&video_track_vector_, track_id);
return it == video_track_vector_.end() ? NULL : *it;
}
virtual void RegisterObserver(ObserverInterface* observer) OVERRIDE {
NOTIMPLEMENTED();
}
virtual void UnregisterObserver(ObserverInterface* observer) OVERRIDE {
NOTIMPLEMENTED();
}
protected:
virtual ~MockMediaStream() {}
private:
std::string label_;
AudioTrackVector audio_track_vector_;
VideoTrackVector video_track_vector_;
};
MockAudioSource::MockAudioSource(
const webrtc::MediaConstraintsInterface* constraints)
: observer_(NULL),
state_(MediaSourceInterface::kInitializing),
optional_constraints_(constraints->GetOptional()),
mandatory_constraints_(constraints->GetMandatory()) {
}
MockAudioSource::~MockAudioSource() {}
void MockAudioSource::RegisterObserver(webrtc::ObserverInterface* observer) {
observer_ = observer;
}
void MockAudioSource::UnregisterObserver(webrtc::ObserverInterface* observer) {
DCHECK(observer_ == observer);
observer_ = NULL;
}
void MockAudioSource::SetLive() {
DCHECK_EQ(MediaSourceInterface::kInitializing, state_);
state_ = MediaSourceInterface::kLive;
if (observer_)
observer_->OnChanged();
}
void MockAudioSource::SetEnded() {
DCHECK_NE(MediaSourceInterface::kEnded, state_);
state_ = MediaSourceInterface::kEnded;
if (observer_)
observer_->OnChanged();
}
webrtc::MediaSourceInterface::SourceState MockAudioSource::state() const {
return state_;
}
MockVideoSource::MockVideoSource()
: observer_(NULL),
state_(MediaSourceInterface::kInitializing) {
}
MockVideoSource::~MockVideoSource() {}
cricket::VideoCapturer* MockVideoSource::GetVideoCapturer() {
NOTIMPLEMENTED();
return NULL;
}
void MockVideoSource::AddSink(cricket::VideoRenderer* output) {
NOTIMPLEMENTED();
}
void MockVideoSource::RemoveSink(cricket::VideoRenderer* output) {
NOTIMPLEMENTED();
}
void MockVideoSource::RegisterObserver(webrtc::ObserverInterface* observer) {
observer_ = observer;
}
void MockVideoSource::UnregisterObserver(webrtc::ObserverInterface* observer) {
DCHECK(observer_ == observer);
observer_ = NULL;
}
void MockVideoSource::SetLive() {
DCHECK_EQ(MediaSourceInterface::kInitializing, state_);
state_ = MediaSourceInterface::kLive;
if (observer_)
observer_->OnChanged();
}
void MockVideoSource::SetEnded() {
DCHECK_NE(MediaSourceInterface::kEnded, state_);
state_ = MediaSourceInterface::kEnded;
if (observer_)
observer_->OnChanged();
}
webrtc::MediaSourceInterface::SourceState MockVideoSource::state() const {
return state_;
}
const cricket::VideoOptions* MockVideoSource::options() const {
NOTIMPLEMENTED();
return NULL;
}
MockLocalVideoTrack::MockLocalVideoTrack(std::string id,
webrtc::VideoSourceInterface* source)
: enabled_(false),
id_(id),
source_(source) {
}
MockLocalVideoTrack::~MockLocalVideoTrack() {}
void MockLocalVideoTrack::AddRenderer(VideoRendererInterface* renderer) {
NOTIMPLEMENTED();
}
void MockLocalVideoTrack::RemoveRenderer(VideoRendererInterface* renderer) {
NOTIMPLEMENTED();
}
cricket::VideoRenderer* MockLocalVideoTrack::FrameInput() {
NOTIMPLEMENTED();
return NULL;
}
std::string MockLocalVideoTrack::kind() const {
NOTIMPLEMENTED();
return "";
}
std::string MockLocalVideoTrack::id() const { return id_; }
bool MockLocalVideoTrack::enabled() const { return enabled_; }
MockLocalVideoTrack::TrackState MockLocalVideoTrack::state() const {
NOTIMPLEMENTED();
return kInitializing;
}
bool MockLocalVideoTrack::set_enabled(bool enable) {
enabled_ = enable;
return true;
}
bool MockLocalVideoTrack::set_state(TrackState new_state) {
NOTIMPLEMENTED();
return false;
}
void MockLocalVideoTrack::RegisterObserver(ObserverInterface* observer) {
NOTIMPLEMENTED();
}
void MockLocalVideoTrack::UnregisterObserver(ObserverInterface* observer) {
NOTIMPLEMENTED();
}
VideoSourceInterface* MockLocalVideoTrack::GetSource() const {
return source_;
}
std::string MockLocalAudioTrack::kind() const {
NOTIMPLEMENTED();
return "";
}
std::string MockLocalAudioTrack::id() const { return id_; }
bool MockLocalAudioTrack::enabled() const { return enabled_; }
MockLocalVideoTrack::TrackState MockLocalAudioTrack::state() const {
NOTIMPLEMENTED();
return kInitializing;
}
bool MockLocalAudioTrack::set_enabled(bool enable) {
enabled_ = enable;
return true;
}
bool MockLocalAudioTrack::set_state(TrackState new_state) {
NOTIMPLEMENTED();
return false;
}
void MockLocalAudioTrack::RegisterObserver(ObserverInterface* observer) {
NOTIMPLEMENTED();
}
void MockLocalAudioTrack::UnregisterObserver(ObserverInterface* observer) {
NOTIMPLEMENTED();
}
AudioSourceInterface* MockLocalAudioTrack::GetSource() const {
NOTIMPLEMENTED();
return NULL;
}
class MockSessionDescription : public SessionDescriptionInterface {
public:
MockSessionDescription(const std::string& type,
const std::string& sdp)
: type_(type),
sdp_(sdp) {
}
virtual ~MockSessionDescription() {}
virtual cricket::SessionDescription* description() OVERRIDE {
NOTIMPLEMENTED();
return NULL;
}
virtual const cricket::SessionDescription* description() const OVERRIDE {
NOTIMPLEMENTED();
return NULL;
}
virtual std::string session_id() const OVERRIDE {
NOTIMPLEMENTED();
return "";
}
virtual std::string session_version() const OVERRIDE {
NOTIMPLEMENTED();
return "";
}
virtual std::string type() const OVERRIDE {
return type_;
}
virtual bool AddCandidate(const IceCandidateInterface* candidate) OVERRIDE {
NOTIMPLEMENTED();
return false;
}
virtual size_t number_of_mediasections() const OVERRIDE {
NOTIMPLEMENTED();
return 0;
}
virtual const IceCandidateCollection* candidates(
size_t mediasection_index) const OVERRIDE {
NOTIMPLEMENTED();
return NULL;
}
virtual bool ToString(std::string* out) const OVERRIDE {
*out = sdp_;
return true;
}
private:
std::string type_;
std::string sdp_;
};
class MockIceCandidate : public IceCandidateInterface {
public:
MockIceCandidate(const std::string& sdp_mid,
int sdp_mline_index,
const std::string& sdp)
: sdp_mid_(sdp_mid),
sdp_mline_index_(sdp_mline_index),
sdp_(sdp) {
}
virtual ~MockIceCandidate() {}
virtual std::string sdp_mid() const OVERRIDE {
return sdp_mid_;
}
virtual int sdp_mline_index() const OVERRIDE {
return sdp_mline_index_;
}
virtual const cricket::Candidate& candidate() const OVERRIDE {
// This function should never be called. It will intentionally crash. The
// base class forces us to return a reference.
NOTREACHED();
cricket::Candidate* candidate = NULL;
return *candidate;
}
virtual bool ToString(std::string* out) const OVERRIDE {
*out = sdp_;
return true;
}
private:
std::string sdp_mid_;
int sdp_mline_index_;
std::string sdp_;
};
MockMediaStreamDependencyFactory::MockMediaStreamDependencyFactory()
: MediaStreamDependencyFactory(NULL, NULL),
mock_pc_factory_created_(false) {
}
MockMediaStreamDependencyFactory::~MockMediaStreamDependencyFactory() {}
bool MockMediaStreamDependencyFactory::EnsurePeerConnectionFactory() {
mock_pc_factory_created_ = true;
return true;
}
bool MockMediaStreamDependencyFactory::PeerConnectionFactoryCreated() {
return mock_pc_factory_created_;
}
scoped_refptr<webrtc::PeerConnectionInterface>
MockMediaStreamDependencyFactory::CreatePeerConnection(
const webrtc::PeerConnectionInterface::IceServers& ice_servers,
const webrtc::MediaConstraintsInterface* constraints,
WebKit::WebFrame* frame,
webrtc::PeerConnectionObserver* observer) {
DCHECK(mock_pc_factory_created_);
return new talk_base::RefCountedObject<MockPeerConnectionImpl>(this);
}
scoped_refptr<webrtc::AudioSourceInterface>
MockMediaStreamDependencyFactory::CreateLocalAudioSource(
const webrtc::MediaConstraintsInterface* constraints) {
last_audio_source_ =
new talk_base::RefCountedObject<MockAudioSource>(constraints);
return last_audio_source_;
}
scoped_refptr<webrtc::VideoSourceInterface>
MockMediaStreamDependencyFactory::CreateLocalVideoSource(
int video_session_id,
bool is_screencast,
const webrtc::MediaConstraintsInterface* constraints) {
last_video_source_ = new talk_base::RefCountedObject<MockVideoSource>();
return last_video_source_;
}
bool MockMediaStreamDependencyFactory::InitializeAudioSource(
const StreamDeviceInfo& device_info) {
return true;
}
bool MockMediaStreamDependencyFactory::CreateWebAudioSource(
WebKit::WebMediaStreamSource* source) {
return true;
}
scoped_refptr<webrtc::MediaStreamInterface>
MockMediaStreamDependencyFactory::CreateLocalMediaStream(
const std::string& label) {
DCHECK(mock_pc_factory_created_);
return new talk_base::RefCountedObject<MockMediaStream>(label);
}
scoped_refptr<webrtc::VideoTrackInterface>
MockMediaStreamDependencyFactory::CreateLocalVideoTrack(
const std::string& id,
webrtc::VideoSourceInterface* source) {
DCHECK(mock_pc_factory_created_);
scoped_refptr<webrtc::VideoTrackInterface> track(
new talk_base::RefCountedObject<MockLocalVideoTrack>(
id, source));
return track;
}
scoped_refptr<webrtc::AudioTrackInterface>
MockMediaStreamDependencyFactory::CreateLocalAudioTrack(
const std::string& id,
webrtc::AudioSourceInterface* source) {
DCHECK(mock_pc_factory_created_);
scoped_refptr<webrtc::AudioTrackInterface> track(
new talk_base::RefCountedObject<MockLocalAudioTrack>(id));
return track;
}
SessionDescriptionInterface*
MockMediaStreamDependencyFactory::CreateSessionDescription(
const std::string& type,
const std::string& sdp,
webrtc::SdpParseError* error) {
return new MockSessionDescription(type, sdp);
}
webrtc::IceCandidateInterface*
MockMediaStreamDependencyFactory::CreateIceCandidate(
const std::string& sdp_mid,
int sdp_mline_index,
const std::string& sdp) {
return new MockIceCandidate(sdp_mid, sdp_mline_index, sdp);
}
} // namespace content
|