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"
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_audio_capturer.h"
12 #include "content/renderer/media/webrtc_local_audio_track.h"
13 #include "third_party/libjingle/source/talk/app/webrtc/mediastreaminterface.h"
14 #include "third_party/libjingle/source/talk/base/scoped_ref_ptr.h"
15 #include "third_party/libjingle/source/talk/media/base/videocapturer.h"
17 using webrtc::AudioSourceInterface;
18 using webrtc::AudioTrackInterface;
19 using webrtc::AudioTrackVector;
20 using webrtc::IceCandidateCollection;
21 using webrtc::IceCandidateInterface;
22 using webrtc::MediaStreamInterface;
23 using webrtc::ObserverInterface;
24 using webrtc::SessionDescriptionInterface;
25 using webrtc::VideoRendererInterface;
26 using webrtc::VideoSourceInterface;
27 using webrtc::VideoTrackInterface;
28 using webrtc::VideoTrackVector;
33 static typename V::iterator FindTrack(V* vector,
34 const std::string& track_id) {
35 typename V::iterator it = vector->begin();
36 for (; it != vector->end(); ++it) {
37 if ((*it)->id() == track_id) {
44 class MockMediaStream : public webrtc::MediaStreamInterface {
46 explicit MockMediaStream(const std::string& label)
50 virtual bool AddTrack(AudioTrackInterface* track) OVERRIDE {
51 audio_track_vector_.push_back(track);
53 observer_->OnChanged();
56 virtual bool AddTrack(VideoTrackInterface* track) OVERRIDE {
57 video_track_vector_.push_back(track);
59 observer_->OnChanged();
62 virtual bool RemoveTrack(AudioTrackInterface* track) OVERRIDE {
63 AudioTrackVector::iterator it = FindTrack(&audio_track_vector_,
65 if (it == audio_track_vector_.end())
67 audio_track_vector_.erase(it);
69 observer_->OnChanged();
72 virtual bool RemoveTrack(VideoTrackInterface* track) OVERRIDE {
73 VideoTrackVector::iterator it = FindTrack(&video_track_vector_,
75 if (it == video_track_vector_.end())
77 video_track_vector_.erase(it);
79 observer_->OnChanged();
82 virtual std::string label() const OVERRIDE { return label_; }
83 virtual AudioTrackVector GetAudioTracks() OVERRIDE {
84 return audio_track_vector_;
86 virtual VideoTrackVector GetVideoTracks() OVERRIDE {
87 return video_track_vector_;
89 virtual talk_base::scoped_refptr<AudioTrackInterface>
90 FindAudioTrack(const std::string& track_id) OVERRIDE {
91 AudioTrackVector::iterator it = FindTrack(&audio_track_vector_, track_id);
92 return it == audio_track_vector_.end() ? NULL : *it;
94 virtual talk_base::scoped_refptr<VideoTrackInterface>
95 FindVideoTrack(const std::string& track_id) OVERRIDE {
96 VideoTrackVector::iterator it = FindTrack(&video_track_vector_, track_id);
97 return it == video_track_vector_.end() ? NULL : *it;
99 virtual void RegisterObserver(ObserverInterface* observer) OVERRIDE {
101 observer_ = observer;
103 virtual void UnregisterObserver(ObserverInterface* observer) OVERRIDE {
104 DCHECK(observer_ == observer);
109 virtual ~MockMediaStream() {}
113 AudioTrackVector audio_track_vector_;
114 VideoTrackVector video_track_vector_;
115 webrtc::ObserverInterface* observer_;
118 MockAudioSource::MockAudioSource(
119 const webrtc::MediaConstraintsInterface* constraints)
121 state_(MediaSourceInterface::kInitializing),
122 optional_constraints_(constraints->GetOptional()),
123 mandatory_constraints_(constraints->GetMandatory()) {
126 MockAudioSource::~MockAudioSource() {}
128 void MockAudioSource::RegisterObserver(webrtc::ObserverInterface* observer) {
129 observer_ = observer;
132 void MockAudioSource::UnregisterObserver(webrtc::ObserverInterface* observer) {
133 DCHECK(observer_ == observer);
137 void MockAudioSource::SetLive() {
138 DCHECK(state_ == MediaSourceInterface::kInitializing ||
139 state_ == MediaSourceInterface::kLive);
140 state_ = MediaSourceInterface::kLive;
142 observer_->OnChanged();
145 void MockAudioSource::SetEnded() {
146 DCHECK_NE(MediaSourceInterface::kEnded, state_);
147 state_ = MediaSourceInterface::kEnded;
149 observer_->OnChanged();
152 webrtc::MediaSourceInterface::SourceState MockAudioSource::state() const {
156 MockVideoSource::MockVideoSource()
157 : state_(MediaSourceInterface::kInitializing) {
160 MockVideoSource::~MockVideoSource() {}
162 void MockVideoSource::SetVideoCapturer(cricket::VideoCapturer* capturer) {
163 capturer_.reset(capturer);
166 cricket::VideoCapturer* MockVideoSource::GetVideoCapturer() {
167 return capturer_.get();
170 void MockVideoSource::AddSink(cricket::VideoRenderer* output) {
174 void MockVideoSource::RemoveSink(cricket::VideoRenderer* output) {
178 cricket::VideoRenderer* MockVideoSource::FrameInput() {
183 void MockVideoSource::RegisterObserver(webrtc::ObserverInterface* observer) {
184 observers_.push_back(observer);
187 void MockVideoSource::UnregisterObserver(webrtc::ObserverInterface* observer) {
188 for (std::vector<ObserverInterface*>::iterator it = observers_.begin();
189 it != observers_.end(); ++it) {
190 if (*it == observer) {
191 observers_.erase(it);
197 void MockVideoSource::FireOnChanged() {
198 std::vector<ObserverInterface*> observers(observers_);
199 for (std::vector<ObserverInterface*>::iterator it = observers.begin();
200 it != observers.end(); ++it) {
205 void MockVideoSource::SetLive() {
206 DCHECK(state_ == MediaSourceInterface::kInitializing ||
207 state_ == MediaSourceInterface::kLive);
208 state_ = MediaSourceInterface::kLive;
212 void MockVideoSource::SetEnded() {
213 DCHECK_NE(MediaSourceInterface::kEnded, state_);
214 state_ = MediaSourceInterface::kEnded;
218 webrtc::MediaSourceInterface::SourceState MockVideoSource::state() const {
222 const cricket::VideoOptions* MockVideoSource::options() const {
227 MockLocalVideoTrack::MockLocalVideoTrack(std::string id,
228 webrtc::VideoSourceInterface* source)
231 state_(MediaStreamTrackInterface::kLive),
236 MockLocalVideoTrack::~MockLocalVideoTrack() {}
238 void MockLocalVideoTrack::AddRenderer(VideoRendererInterface* renderer) {
242 void MockLocalVideoTrack::RemoveRenderer(VideoRendererInterface* renderer) {
246 std::string MockLocalVideoTrack::kind() const {
248 return std::string();
251 std::string MockLocalVideoTrack::id() const { return id_; }
253 bool MockLocalVideoTrack::enabled() const { return enabled_; }
255 MockLocalVideoTrack::TrackState MockLocalVideoTrack::state() const {
259 bool MockLocalVideoTrack::set_enabled(bool enable) {
264 bool MockLocalVideoTrack::set_state(TrackState new_state) {
267 observer_->OnChanged();
271 void MockLocalVideoTrack::RegisterObserver(ObserverInterface* observer) {
272 observer_ = observer;
275 void MockLocalVideoTrack::UnregisterObserver(ObserverInterface* observer) {
276 DCHECK(observer_ == observer);
280 VideoSourceInterface* MockLocalVideoTrack::GetSource() const {
281 return source_.get();
284 class MockSessionDescription : public SessionDescriptionInterface {
286 MockSessionDescription(const std::string& type,
287 const std::string& sdp)
291 virtual ~MockSessionDescription() {}
292 virtual cricket::SessionDescription* description() OVERRIDE {
296 virtual const cricket::SessionDescription* description() const OVERRIDE {
300 virtual std::string session_id() const OVERRIDE {
302 return std::string();
304 virtual std::string session_version() const OVERRIDE {
306 return std::string();
308 virtual std::string type() const OVERRIDE {
311 virtual bool AddCandidate(const IceCandidateInterface* candidate) OVERRIDE {
315 virtual size_t number_of_mediasections() const OVERRIDE {
319 virtual const IceCandidateCollection* candidates(
320 size_t mediasection_index) const OVERRIDE {
325 virtual bool ToString(std::string* out) const OVERRIDE {
335 class MockIceCandidate : public IceCandidateInterface {
337 MockIceCandidate(const std::string& sdp_mid,
339 const std::string& sdp)
341 sdp_mline_index_(sdp_mline_index),
344 virtual ~MockIceCandidate() {}
345 virtual std::string sdp_mid() const OVERRIDE {
348 virtual int sdp_mline_index() const OVERRIDE {
349 return sdp_mline_index_;
351 virtual const cricket::Candidate& candidate() const OVERRIDE {
352 // This function should never be called. It will intentionally crash. The
353 // base class forces us to return a reference.
355 cricket::Candidate* candidate = NULL;
358 virtual bool ToString(std::string* out) const OVERRIDE {
364 std::string sdp_mid_;
365 int sdp_mline_index_;
369 MockMediaStreamDependencyFactory::MockMediaStreamDependencyFactory()
370 : MediaStreamDependencyFactory(NULL, NULL),
371 mock_pc_factory_created_(false) {
374 MockMediaStreamDependencyFactory::~MockMediaStreamDependencyFactory() {}
376 bool MockMediaStreamDependencyFactory::EnsurePeerConnectionFactory() {
377 mock_pc_factory_created_ = true;
381 bool MockMediaStreamDependencyFactory::PeerConnectionFactoryCreated() {
382 return mock_pc_factory_created_;
385 scoped_refptr<webrtc::PeerConnectionInterface>
386 MockMediaStreamDependencyFactory::CreatePeerConnection(
387 const webrtc::PeerConnectionInterface::IceServers& ice_servers,
388 const webrtc::MediaConstraintsInterface* constraints,
389 WebKit::WebFrame* frame,
390 webrtc::PeerConnectionObserver* observer) {
391 DCHECK(mock_pc_factory_created_);
392 return new talk_base::RefCountedObject<MockPeerConnectionImpl>(this);
395 scoped_refptr<webrtc::AudioSourceInterface>
396 MockMediaStreamDependencyFactory::CreateLocalAudioSource(
397 const webrtc::MediaConstraintsInterface* constraints) {
399 new talk_base::RefCountedObject<MockAudioSource>(constraints);
400 return last_audio_source_;
403 scoped_refptr<webrtc::VideoSourceInterface>
404 MockMediaStreamDependencyFactory::CreateLocalVideoSource(
405 int video_session_id,
407 const webrtc::MediaConstraintsInterface* constraints) {
408 last_video_source_ = new talk_base::RefCountedObject<MockVideoSource>();
409 return last_video_source_;
412 scoped_refptr<WebAudioCapturerSource>
413 MockMediaStreamDependencyFactory::CreateWebAudioSource(
414 WebKit::WebMediaStreamSource* source,
415 RTCMediaConstraints* constraints) {
419 scoped_refptr<webrtc::MediaStreamInterface>
420 MockMediaStreamDependencyFactory::CreateLocalMediaStream(
421 const std::string& label) {
422 DCHECK(mock_pc_factory_created_);
423 return new talk_base::RefCountedObject<MockMediaStream>(label);
426 scoped_refptr<webrtc::VideoTrackInterface>
427 MockMediaStreamDependencyFactory::CreateLocalVideoTrack(
428 const std::string& id,
429 webrtc::VideoSourceInterface* source) {
430 DCHECK(mock_pc_factory_created_);
431 scoped_refptr<webrtc::VideoTrackInterface> track(
432 new talk_base::RefCountedObject<MockLocalVideoTrack>(
437 scoped_refptr<webrtc::VideoTrackInterface>
438 MockMediaStreamDependencyFactory::CreateLocalVideoTrack(
439 const std::string& id,
440 cricket::VideoCapturer* capturer) {
441 DCHECK(mock_pc_factory_created_);
443 scoped_refptr<MockVideoSource> source =
444 new talk_base::RefCountedObject<MockVideoSource>();
445 source->SetVideoCapturer(capturer);
447 return new talk_base::RefCountedObject<MockLocalVideoTrack>(id, source.get());
450 scoped_refptr<webrtc::AudioTrackInterface>
451 MockMediaStreamDependencyFactory::CreateLocalAudioTrack(
452 const std::string& id,
453 const scoped_refptr<WebRtcAudioCapturer>& capturer,
454 WebAudioCapturerSource* webaudio_source,
455 webrtc::AudioSourceInterface* source,
456 const webrtc::MediaConstraintsInterface* constraints) {
457 DCHECK(mock_pc_factory_created_);
458 DCHECK(!capturer.get());
459 return WebRtcLocalAudioTrack::Create(
460 id, WebRtcAudioCapturer::CreateCapturer(), webaudio_source,
461 source, constraints);
464 SessionDescriptionInterface*
465 MockMediaStreamDependencyFactory::CreateSessionDescription(
466 const std::string& type,
467 const std::string& sdp,
468 webrtc::SdpParseError* error) {
469 return new MockSessionDescription(type, sdp);
472 webrtc::IceCandidateInterface*
473 MockMediaStreamDependencyFactory::CreateIceCandidate(
474 const std::string& sdp_mid,
476 const std::string& sdp) {
477 return new MockIceCandidate(sdp_mid, sdp_mline_index, sdp);
480 scoped_refptr<WebRtcAudioCapturer>
481 MockMediaStreamDependencyFactory::MaybeCreateAudioCapturer(
482 int render_view_id, const StreamDeviceInfo& device_info) {
483 return WebRtcAudioCapturer::CreateCapturer();
486 } // namespace content