Process Alt-Svc headers.
[chromium-blink-merge.git] / content / renderer / media / webrtc / mock_peer_connection_dependency_factory.cc
blob183b003f2cbc0aa91d49931338a146bd47d8b356
1 // Copyright 2014 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/webrtc/mock_peer_connection_dependency_factory.h"
7 #include "base/logging.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "content/renderer/media/mock_peer_connection_impl.h"
10 #include "content/renderer/media/webaudio_capturer_source.h"
11 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h"
12 #include "content/renderer/media/webrtc/webrtc_video_capturer_adapter.h"
13 #include "content/renderer/media/webrtc_audio_capturer.h"
14 #include "content/renderer/media/webrtc_local_audio_track.h"
15 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
16 #include "third_party/libjingle/source/talk/app/webrtc/mediastreaminterface.h"
17 #include "third_party/libjingle/source/talk/media/base/videocapturer.h"
18 #include "third_party/webrtc/base/scoped_ref_ptr.h"
20 using webrtc::AudioSourceInterface;
21 using webrtc::AudioTrackInterface;
22 using webrtc::AudioTrackVector;
23 using webrtc::IceCandidateCollection;
24 using webrtc::IceCandidateInterface;
25 using webrtc::MediaStreamInterface;
26 using webrtc::ObserverInterface;
27 using webrtc::SessionDescriptionInterface;
28 using webrtc::VideoRendererInterface;
29 using webrtc::VideoSourceInterface;
30 using webrtc::VideoTrackInterface;
31 using webrtc::VideoTrackVector;
33 namespace content {
35 template <class V>
36 static typename V::iterator FindTrack(V* vector,
37 const std::string& track_id) {
38 typename V::iterator it = vector->begin();
39 for (; it != vector->end(); ++it) {
40 if ((*it)->id() == track_id) {
41 break;
44 return it;
47 MockMediaStream::MockMediaStream(const std::string& label) : label_(label) {}
49 bool MockMediaStream::AddTrack(AudioTrackInterface* track) {
50 audio_track_vector_.push_back(track);
51 NotifyObservers();
52 return true;
55 bool MockMediaStream::AddTrack(VideoTrackInterface* track) {
56 video_track_vector_.push_back(track);
57 NotifyObservers();
58 return true;
61 bool MockMediaStream::RemoveTrack(AudioTrackInterface* track) {
62 AudioTrackVector::iterator it = FindTrack(&audio_track_vector_,
63 track->id());
64 if (it == audio_track_vector_.end())
65 return false;
66 audio_track_vector_.erase(it);
67 NotifyObservers();
68 return true;
71 bool MockMediaStream::RemoveTrack(VideoTrackInterface* track) {
72 VideoTrackVector::iterator it = FindTrack(&video_track_vector_,
73 track->id());
74 if (it == video_track_vector_.end())
75 return false;
76 video_track_vector_.erase(it);
77 NotifyObservers();
78 return true;
81 std::string MockMediaStream::label() const {
82 return label_;
85 AudioTrackVector MockMediaStream::GetAudioTracks() {
86 return audio_track_vector_;
89 VideoTrackVector MockMediaStream::GetVideoTracks() {
90 return video_track_vector_;
93 rtc::scoped_refptr<AudioTrackInterface> MockMediaStream::FindAudioTrack(
94 const std::string& track_id) {
95 AudioTrackVector::iterator it = FindTrack(&audio_track_vector_, track_id);
96 return it == audio_track_vector_.end() ? NULL : *it;
99 rtc::scoped_refptr<VideoTrackInterface> MockMediaStream::FindVideoTrack(
100 const std::string& track_id) {
101 VideoTrackVector::iterator it = FindTrack(&video_track_vector_, track_id);
102 return it == video_track_vector_.end() ? NULL : *it;
105 void MockMediaStream::RegisterObserver(ObserverInterface* observer) {
106 DCHECK(observers_.find(observer) == observers_.end());
107 observers_.insert(observer);
110 void MockMediaStream::UnregisterObserver(ObserverInterface* observer) {
111 ObserverSet::iterator it = observers_.find(observer);
112 DCHECK(it != observers_.end());
113 observers_.erase(it);
116 void MockMediaStream::NotifyObservers() {
117 for (ObserverSet::iterator it = observers_.begin(); it != observers_.end();
118 ++it) {
119 (*it)->OnChanged();
123 MockMediaStream::~MockMediaStream() {}
125 class MockRtcVideoCapturer : public WebRtcVideoCapturerAdapter {
126 public:
127 explicit MockRtcVideoCapturer(bool is_screencast)
128 : WebRtcVideoCapturerAdapter(is_screencast),
129 number_of_capturered_frames_(0),
130 width_(0),
131 height_(0) {
134 void OnFrameCaptured(const scoped_refptr<media::VideoFrame>& frame) override {
135 ++number_of_capturered_frames_;
136 width_ = frame->visible_rect().width();
137 height_ = frame->visible_rect().height();
140 int GetLastFrameWidth() const {
141 return width_;
144 int GetLastFrameHeight() const {
145 return height_;
148 int GetFrameNum() const {
149 return number_of_capturered_frames_;
152 private:
153 int number_of_capturered_frames_;
154 int width_;
155 int height_;
158 MockVideoRenderer::MockVideoRenderer()
159 : width_(0),
160 height_(0),
161 num_(0) {}
163 MockVideoRenderer::~MockVideoRenderer() {}
165 bool MockVideoRenderer::SetSize(int width, int height, int reserved) {
166 width_ = width;
167 height_ = height;
168 return true;
171 bool MockVideoRenderer::RenderFrame(const cricket::VideoFrame* frame) {
172 ++num_;
173 return true;
176 MockAudioSource::MockAudioSource(
177 const webrtc::MediaConstraintsInterface* constraints)
178 : state_(MediaSourceInterface::kLive),
179 optional_constraints_(constraints->GetOptional()),
180 mandatory_constraints_(constraints->GetMandatory()) {
183 MockAudioSource::~MockAudioSource() {}
185 void MockAudioSource::RegisterObserver(webrtc::ObserverInterface* observer) {
186 DCHECK(observers_.find(observer) == observers_.end());
187 observers_.insert(observer);
190 void MockAudioSource::UnregisterObserver(webrtc::ObserverInterface* observer) {
191 DCHECK(observers_.find(observer) != observers_.end());
192 observers_.erase(observer);
195 webrtc::MediaSourceInterface::SourceState MockAudioSource::state() const {
196 return state_;
199 MockVideoSource::MockVideoSource()
200 : state_(MediaSourceInterface::kInitializing) {
203 MockVideoSource::~MockVideoSource() {}
205 void MockVideoSource::SetVideoCapturer(cricket::VideoCapturer* capturer) {
206 capturer_.reset(capturer);
209 cricket::VideoCapturer* MockVideoSource::GetVideoCapturer() {
210 return capturer_.get();
213 void MockVideoSource::AddSink(cricket::VideoRenderer* output) {
214 NOTIMPLEMENTED();
217 void MockVideoSource::RemoveSink(cricket::VideoRenderer* output) {
218 NOTIMPLEMENTED();
221 void MockVideoSource::Stop() {
222 NOTIMPLEMENTED();
224 void MockVideoSource::Restart() {
225 NOTIMPLEMENTED();
228 cricket::VideoRenderer* MockVideoSource::FrameInput() {
229 return &renderer_;
232 void MockVideoSource::RegisterObserver(webrtc::ObserverInterface* observer) {
233 observers_.push_back(observer);
236 void MockVideoSource::UnregisterObserver(webrtc::ObserverInterface* observer) {
237 for (std::vector<ObserverInterface*>::iterator it = observers_.begin();
238 it != observers_.end(); ++it) {
239 if (*it == observer) {
240 observers_.erase(it);
241 break;
246 void MockVideoSource::FireOnChanged() {
247 std::vector<ObserverInterface*> observers(observers_);
248 for (std::vector<ObserverInterface*>::iterator it = observers.begin();
249 it != observers.end(); ++it) {
250 (*it)->OnChanged();
254 void MockVideoSource::SetLive() {
255 DCHECK(state_ == MediaSourceInterface::kInitializing ||
256 state_ == MediaSourceInterface::kLive);
257 state_ = MediaSourceInterface::kLive;
258 FireOnChanged();
261 void MockVideoSource::SetEnded() {
262 DCHECK_NE(MediaSourceInterface::kEnded, state_);
263 state_ = MediaSourceInterface::kEnded;
264 FireOnChanged();
267 webrtc::MediaSourceInterface::SourceState MockVideoSource::state() const {
268 return state_;
271 const cricket::VideoOptions* MockVideoSource::options() const {
272 NOTIMPLEMENTED();
273 return NULL;
276 int MockVideoSource::GetLastFrameWidth() const {
277 DCHECK(capturer_);
278 return
279 static_cast<MockRtcVideoCapturer*>(capturer_.get())->GetLastFrameWidth();
282 int MockVideoSource::GetLastFrameHeight() const {
283 DCHECK(capturer_);
284 return
285 static_cast<MockRtcVideoCapturer*>(capturer_.get())->GetLastFrameHeight();
288 int MockVideoSource::GetFrameNum() const {
289 DCHECK(capturer_);
290 return static_cast<MockRtcVideoCapturer*>(capturer_.get())->GetFrameNum();
293 MockWebRtcVideoTrack::MockWebRtcVideoTrack(
294 const std::string& id,
295 webrtc::VideoSourceInterface* source)
296 : enabled_(false),
297 id_(id),
298 state_(MediaStreamTrackInterface::kLive),
299 source_(source),
300 renderer_(NULL) {
303 MockWebRtcVideoTrack::~MockWebRtcVideoTrack() {}
305 void MockWebRtcVideoTrack::AddRenderer(VideoRendererInterface* renderer) {
306 DCHECK(!renderer_);
307 renderer_ = renderer;
310 void MockWebRtcVideoTrack::RemoveRenderer(VideoRendererInterface* renderer) {
311 DCHECK_EQ(renderer_, renderer);
312 renderer_ = NULL;
315 std::string MockWebRtcVideoTrack::kind() const {
316 NOTIMPLEMENTED();
317 return std::string();
320 std::string MockWebRtcVideoTrack::id() const { return id_; }
322 bool MockWebRtcVideoTrack::enabled() const { return enabled_; }
324 MockWebRtcVideoTrack::TrackState MockWebRtcVideoTrack::state() const {
325 return state_;
328 bool MockWebRtcVideoTrack::set_enabled(bool enable) {
329 enabled_ = enable;
330 return true;
333 bool MockWebRtcVideoTrack::set_state(TrackState new_state) {
334 state_ = new_state;
335 for (auto& o : observers_)
336 o->OnChanged();
337 return true;
340 void MockWebRtcVideoTrack::RegisterObserver(ObserverInterface* observer) {
341 DCHECK(observers_.find(observer) == observers_.end());
342 observers_.insert(observer);
345 void MockWebRtcVideoTrack::UnregisterObserver(ObserverInterface* observer) {
346 DCHECK(observers_.find(observer) != observers_.end());
347 observers_.erase(observer);
350 VideoSourceInterface* MockWebRtcVideoTrack::GetSource() const {
351 return source_.get();
354 class MockSessionDescription : public SessionDescriptionInterface {
355 public:
356 MockSessionDescription(const std::string& type,
357 const std::string& sdp)
358 : type_(type),
359 sdp_(sdp) {
361 ~MockSessionDescription() override {}
362 cricket::SessionDescription* description() override {
363 NOTIMPLEMENTED();
364 return NULL;
366 const cricket::SessionDescription* description() const override {
367 NOTIMPLEMENTED();
368 return NULL;
370 std::string session_id() const override {
371 NOTIMPLEMENTED();
372 return std::string();
374 std::string session_version() const override {
375 NOTIMPLEMENTED();
376 return std::string();
378 std::string type() const override { return type_; }
379 bool AddCandidate(const IceCandidateInterface* candidate) override {
380 NOTIMPLEMENTED();
381 return false;
383 size_t number_of_mediasections() const override {
384 NOTIMPLEMENTED();
385 return 0;
387 const IceCandidateCollection* candidates(
388 size_t mediasection_index) const override {
389 NOTIMPLEMENTED();
390 return NULL;
393 bool ToString(std::string* out) const override {
394 *out = sdp_;
395 return true;
398 private:
399 std::string type_;
400 std::string sdp_;
403 class MockIceCandidate : public IceCandidateInterface {
404 public:
405 MockIceCandidate(const std::string& sdp_mid,
406 int sdp_mline_index,
407 const std::string& sdp)
408 : sdp_mid_(sdp_mid),
409 sdp_mline_index_(sdp_mline_index),
410 sdp_(sdp) {
411 // Assign an valid address to |candidate_| to pass assert in code.
412 candidate_.set_address(rtc::SocketAddress("127.0.0.1", 5000));
414 ~MockIceCandidate() override {}
415 std::string sdp_mid() const override { return sdp_mid_; }
416 int sdp_mline_index() const override { return sdp_mline_index_; }
417 const cricket::Candidate& candidate() const override { return candidate_; }
418 bool ToString(std::string* out) const override {
419 *out = sdp_;
420 return true;
423 private:
424 std::string sdp_mid_;
425 int sdp_mline_index_;
426 std::string sdp_;
427 cricket::Candidate candidate_;
430 MockPeerConnectionDependencyFactory::MockPeerConnectionDependencyFactory()
431 : PeerConnectionDependencyFactory(NULL),
432 fail_to_create_next_audio_capturer_(false) {
435 MockPeerConnectionDependencyFactory::~MockPeerConnectionDependencyFactory() {}
437 scoped_refptr<webrtc::PeerConnectionInterface>
438 MockPeerConnectionDependencyFactory::CreatePeerConnection(
439 const webrtc::PeerConnectionInterface::RTCConfiguration& config,
440 const webrtc::MediaConstraintsInterface* constraints,
441 blink::WebFrame* frame,
442 webrtc::PeerConnectionObserver* observer) {
443 return new rtc::RefCountedObject<MockPeerConnectionImpl>(this, observer);
446 scoped_refptr<webrtc::AudioSourceInterface>
447 MockPeerConnectionDependencyFactory::CreateLocalAudioSource(
448 const webrtc::MediaConstraintsInterface* constraints) {
449 last_audio_source_ =
450 new rtc::RefCountedObject<MockAudioSource>(constraints);
451 return last_audio_source_;
454 WebRtcVideoCapturerAdapter*
455 MockPeerConnectionDependencyFactory::CreateVideoCapturer(
456 bool is_screen_capture) {
457 return new MockRtcVideoCapturer(is_screen_capture);
460 scoped_refptr<webrtc::VideoSourceInterface>
461 MockPeerConnectionDependencyFactory::CreateVideoSource(
462 cricket::VideoCapturer* capturer,
463 const blink::WebMediaConstraints& constraints) {
464 last_video_source_ = new rtc::RefCountedObject<MockVideoSource>();
465 last_video_source_->SetVideoCapturer(capturer);
466 return last_video_source_;
469 scoped_refptr<WebAudioCapturerSource>
470 MockPeerConnectionDependencyFactory::CreateWebAudioSource(
471 blink::WebMediaStreamSource* source) {
472 return NULL;
475 scoped_refptr<webrtc::MediaStreamInterface>
476 MockPeerConnectionDependencyFactory::CreateLocalMediaStream(
477 const std::string& label) {
478 return new rtc::RefCountedObject<MockMediaStream>(label);
481 scoped_refptr<webrtc::VideoTrackInterface>
482 MockPeerConnectionDependencyFactory::CreateLocalVideoTrack(
483 const std::string& id,
484 webrtc::VideoSourceInterface* source) {
485 scoped_refptr<webrtc::VideoTrackInterface> track(
486 new rtc::RefCountedObject<MockWebRtcVideoTrack>(
487 id, source));
488 return track;
491 scoped_refptr<webrtc::VideoTrackInterface>
492 MockPeerConnectionDependencyFactory::CreateLocalVideoTrack(
493 const std::string& id,
494 cricket::VideoCapturer* capturer) {
495 scoped_refptr<MockVideoSource> source =
496 new rtc::RefCountedObject<MockVideoSource>();
497 source->SetVideoCapturer(capturer);
499 return
500 new rtc::RefCountedObject<MockWebRtcVideoTrack>(id, source.get());
503 SessionDescriptionInterface*
504 MockPeerConnectionDependencyFactory::CreateSessionDescription(
505 const std::string& type,
506 const std::string& sdp,
507 webrtc::SdpParseError* error) {
508 return new MockSessionDescription(type, sdp);
511 webrtc::IceCandidateInterface*
512 MockPeerConnectionDependencyFactory::CreateIceCandidate(
513 const std::string& sdp_mid,
514 int sdp_mline_index,
515 const std::string& sdp) {
516 return new MockIceCandidate(sdp_mid, sdp_mline_index, sdp);
519 scoped_refptr<WebRtcAudioCapturer>
520 MockPeerConnectionDependencyFactory::CreateAudioCapturer(
521 int render_frame_id,
522 const StreamDeviceInfo& device_info,
523 const blink::WebMediaConstraints& constraints,
524 MediaStreamAudioSource* audio_source) {
525 if (fail_to_create_next_audio_capturer_) {
526 fail_to_create_next_audio_capturer_ = false;
527 return NULL;
529 DCHECK(audio_source);
530 return WebRtcAudioCapturer::CreateCapturer(-1, device_info, constraints, NULL,
531 audio_source);
534 void MockPeerConnectionDependencyFactory::StartLocalAudioTrack(
535 WebRtcLocalAudioTrack* audio_track) {
536 audio_track->Start();
539 } // namespace content