- add sources.
[platform/framework/web/crosswalk.git] / src / content / renderer / media / mock_media_stream_dependency_factory.cc
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.
4
5 #include "content/renderer/media/mock_media_stream_dependency_factory.h"
6
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"
16
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;
29
30 namespace content {
31
32 template <class V>
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) {
38       break;
39     }
40   }
41   return it;
42 };
43
44 class MockMediaStream : public webrtc::MediaStreamInterface {
45  public:
46   explicit MockMediaStream(const std::string& label)
47       : label_(label),
48         observer_(NULL) {
49   }
50   virtual bool AddTrack(AudioTrackInterface* track) OVERRIDE {
51     audio_track_vector_.push_back(track);
52     if (observer_)
53       observer_->OnChanged();
54     return true;
55   }
56   virtual bool AddTrack(VideoTrackInterface* track) OVERRIDE {
57     video_track_vector_.push_back(track);
58     if (observer_)
59       observer_->OnChanged();
60     return true;
61   }
62   virtual bool RemoveTrack(AudioTrackInterface* track) OVERRIDE {
63     AudioTrackVector::iterator it = FindTrack(&audio_track_vector_,
64                                               track->id());
65     if (it == audio_track_vector_.end())
66       return false;
67     audio_track_vector_.erase(it);
68     if (observer_)
69       observer_->OnChanged();
70     return true;
71   }
72   virtual bool RemoveTrack(VideoTrackInterface* track) OVERRIDE {
73     VideoTrackVector::iterator it = FindTrack(&video_track_vector_,
74                                               track->id());
75     if (it == video_track_vector_.end())
76       return false;
77     video_track_vector_.erase(it);
78     if (observer_)
79       observer_->OnChanged();
80     return true;
81   }
82   virtual std::string label() const OVERRIDE { return label_; }
83   virtual AudioTrackVector GetAudioTracks() OVERRIDE {
84     return audio_track_vector_;
85   }
86   virtual VideoTrackVector GetVideoTracks() OVERRIDE {
87     return video_track_vector_;
88   }
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;
93   }
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;
98   }
99   virtual void RegisterObserver(ObserverInterface* observer) OVERRIDE {
100     DCHECK(!observer_);
101     observer_ = observer;
102   }
103   virtual void UnregisterObserver(ObserverInterface* observer) OVERRIDE {
104     DCHECK(observer_ == observer);
105     observer_ = NULL;
106   }
107
108  protected:
109   virtual ~MockMediaStream() {}
110
111  private:
112   std::string label_;
113   AudioTrackVector audio_track_vector_;
114   VideoTrackVector video_track_vector_;
115   webrtc::ObserverInterface* observer_;
116 };
117
118 MockAudioSource::MockAudioSource(
119     const webrtc::MediaConstraintsInterface* constraints)
120     : observer_(NULL),
121       state_(MediaSourceInterface::kInitializing),
122       optional_constraints_(constraints->GetOptional()),
123       mandatory_constraints_(constraints->GetMandatory()) {
124 }
125
126 MockAudioSource::~MockAudioSource() {}
127
128 void MockAudioSource::RegisterObserver(webrtc::ObserverInterface* observer) {
129   observer_ = observer;
130 }
131
132 void MockAudioSource::UnregisterObserver(webrtc::ObserverInterface* observer) {
133   DCHECK(observer_ == observer);
134   observer_ = NULL;
135 }
136
137 void MockAudioSource::SetLive() {
138   DCHECK(state_ == MediaSourceInterface::kInitializing ||
139          state_ == MediaSourceInterface::kLive);
140   state_ = MediaSourceInterface::kLive;
141   if (observer_)
142     observer_->OnChanged();
143 }
144
145 void MockAudioSource::SetEnded() {
146   DCHECK_NE(MediaSourceInterface::kEnded, state_);
147   state_ = MediaSourceInterface::kEnded;
148   if (observer_)
149     observer_->OnChanged();
150 }
151
152 webrtc::MediaSourceInterface::SourceState MockAudioSource::state() const {
153   return state_;
154 }
155
156 MockVideoSource::MockVideoSource()
157     : state_(MediaSourceInterface::kInitializing) {
158 }
159
160 MockVideoSource::~MockVideoSource() {}
161
162 void MockVideoSource::SetVideoCapturer(cricket::VideoCapturer* capturer) {
163   capturer_.reset(capturer);
164 }
165
166 cricket::VideoCapturer* MockVideoSource::GetVideoCapturer() {
167   return capturer_.get();
168 }
169
170 void MockVideoSource::AddSink(cricket::VideoRenderer* output) {
171   NOTIMPLEMENTED();
172 }
173
174 void MockVideoSource::RemoveSink(cricket::VideoRenderer* output) {
175   NOTIMPLEMENTED();
176 }
177
178 cricket::VideoRenderer* MockVideoSource::FrameInput() {
179   NOTIMPLEMENTED();
180   return NULL;
181 }
182
183 void MockVideoSource::RegisterObserver(webrtc::ObserverInterface* observer) {
184   observers_.push_back(observer);
185 }
186
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);
192       break;
193     }
194   }
195 }
196
197 void MockVideoSource::FireOnChanged() {
198   std::vector<ObserverInterface*> observers(observers_);
199   for (std::vector<ObserverInterface*>::iterator it = observers.begin();
200        it != observers.end(); ++it) {
201     (*it)->OnChanged();
202   }
203 }
204
205 void MockVideoSource::SetLive() {
206   DCHECK(state_ == MediaSourceInterface::kInitializing ||
207          state_ == MediaSourceInterface::kLive);
208   state_ = MediaSourceInterface::kLive;
209   FireOnChanged();
210 }
211
212 void MockVideoSource::SetEnded() {
213   DCHECK_NE(MediaSourceInterface::kEnded, state_);
214   state_ = MediaSourceInterface::kEnded;
215   FireOnChanged();
216 }
217
218 webrtc::MediaSourceInterface::SourceState MockVideoSource::state() const {
219   return state_;
220 }
221
222 const cricket::VideoOptions* MockVideoSource::options() const {
223   NOTIMPLEMENTED();
224   return NULL;
225 }
226
227 MockLocalVideoTrack::MockLocalVideoTrack(std::string id,
228                                          webrtc::VideoSourceInterface* source)
229     : enabled_(false),
230       id_(id),
231       state_(MediaStreamTrackInterface::kLive),
232       source_(source),
233       observer_(NULL) {
234 }
235
236 MockLocalVideoTrack::~MockLocalVideoTrack() {}
237
238 void MockLocalVideoTrack::AddRenderer(VideoRendererInterface* renderer) {
239   NOTIMPLEMENTED();
240 }
241
242 void MockLocalVideoTrack::RemoveRenderer(VideoRendererInterface* renderer) {
243   NOTIMPLEMENTED();
244 }
245
246 std::string MockLocalVideoTrack::kind() const {
247   NOTIMPLEMENTED();
248   return std::string();
249 }
250
251 std::string MockLocalVideoTrack::id() const { return id_; }
252
253 bool MockLocalVideoTrack::enabled() const { return enabled_; }
254
255 MockLocalVideoTrack::TrackState MockLocalVideoTrack::state() const {
256   return state_;
257 }
258
259 bool MockLocalVideoTrack::set_enabled(bool enable) {
260   enabled_ = enable;
261   return true;
262 }
263
264 bool MockLocalVideoTrack::set_state(TrackState new_state) {
265   state_ = new_state;
266   if (observer_)
267     observer_->OnChanged();
268   return true;
269 }
270
271 void MockLocalVideoTrack::RegisterObserver(ObserverInterface* observer) {
272   observer_ = observer;
273 }
274
275 void MockLocalVideoTrack::UnregisterObserver(ObserverInterface* observer) {
276   DCHECK(observer_ == observer);
277   observer_ = NULL;
278 }
279
280 VideoSourceInterface* MockLocalVideoTrack::GetSource() const {
281   return source_.get();
282 }
283
284 class MockSessionDescription : public SessionDescriptionInterface {
285  public:
286   MockSessionDescription(const std::string& type,
287                          const std::string& sdp)
288       : type_(type),
289         sdp_(sdp) {
290   }
291   virtual ~MockSessionDescription() {}
292   virtual cricket::SessionDescription* description() OVERRIDE {
293     NOTIMPLEMENTED();
294     return NULL;
295   }
296   virtual const cricket::SessionDescription* description() const OVERRIDE {
297     NOTIMPLEMENTED();
298     return NULL;
299   }
300   virtual std::string session_id() const OVERRIDE {
301     NOTIMPLEMENTED();
302     return std::string();
303   }
304   virtual std::string session_version() const OVERRIDE {
305     NOTIMPLEMENTED();
306     return std::string();
307   }
308   virtual std::string type() const OVERRIDE {
309     return type_;
310   }
311   virtual bool AddCandidate(const IceCandidateInterface* candidate) OVERRIDE {
312     NOTIMPLEMENTED();
313     return false;
314   }
315   virtual size_t number_of_mediasections() const OVERRIDE {
316     NOTIMPLEMENTED();
317     return 0;
318   }
319   virtual const IceCandidateCollection* candidates(
320       size_t mediasection_index) const OVERRIDE {
321     NOTIMPLEMENTED();
322     return NULL;
323   }
324
325   virtual bool ToString(std::string* out) const OVERRIDE {
326     *out = sdp_;
327     return true;
328   }
329
330  private:
331   std::string type_;
332   std::string sdp_;
333 };
334
335 class MockIceCandidate : public IceCandidateInterface {
336  public:
337   MockIceCandidate(const std::string& sdp_mid,
338                    int sdp_mline_index,
339                    const std::string& sdp)
340       : sdp_mid_(sdp_mid),
341         sdp_mline_index_(sdp_mline_index),
342         sdp_(sdp) {
343   }
344   virtual ~MockIceCandidate() {}
345   virtual std::string sdp_mid() const OVERRIDE {
346     return sdp_mid_;
347   }
348   virtual int sdp_mline_index() const OVERRIDE {
349     return sdp_mline_index_;
350   }
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.
354     NOTREACHED();
355     cricket::Candidate* candidate = NULL;
356     return *candidate;
357   }
358   virtual bool ToString(std::string* out) const OVERRIDE {
359     *out = sdp_;
360     return true;
361   }
362
363  private:
364   std::string sdp_mid_;
365   int sdp_mline_index_;
366   std::string sdp_;
367 };
368
369 MockMediaStreamDependencyFactory::MockMediaStreamDependencyFactory()
370     : MediaStreamDependencyFactory(NULL, NULL),
371       mock_pc_factory_created_(false) {
372 }
373
374 MockMediaStreamDependencyFactory::~MockMediaStreamDependencyFactory() {}
375
376 bool MockMediaStreamDependencyFactory::EnsurePeerConnectionFactory() {
377   mock_pc_factory_created_ = true;
378   return true;
379 }
380
381 bool MockMediaStreamDependencyFactory::PeerConnectionFactoryCreated() {
382   return mock_pc_factory_created_;
383 }
384
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);
393 }
394
395 scoped_refptr<webrtc::AudioSourceInterface>
396 MockMediaStreamDependencyFactory::CreateLocalAudioSource(
397     const webrtc::MediaConstraintsInterface* constraints) {
398   last_audio_source_ =
399       new talk_base::RefCountedObject<MockAudioSource>(constraints);
400   return last_audio_source_;
401 }
402
403 scoped_refptr<webrtc::VideoSourceInterface>
404 MockMediaStreamDependencyFactory::CreateLocalVideoSource(
405     int video_session_id,
406     bool is_screencast,
407     const webrtc::MediaConstraintsInterface* constraints) {
408   last_video_source_ = new talk_base::RefCountedObject<MockVideoSource>();
409   return last_video_source_;
410 }
411
412 scoped_refptr<WebAudioCapturerSource>
413 MockMediaStreamDependencyFactory::CreateWebAudioSource(
414     WebKit::WebMediaStreamSource* source,
415     RTCMediaConstraints* constraints) {
416   return NULL;
417 }
418
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);
424 }
425
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>(
433           id, source));
434   return track;
435 }
436
437 scoped_refptr<webrtc::VideoTrackInterface>
438 MockMediaStreamDependencyFactory::CreateLocalVideoTrack(
439     const std::string& id,
440     cricket::VideoCapturer* capturer) {
441   DCHECK(mock_pc_factory_created_);
442
443   scoped_refptr<MockVideoSource> source =
444       new talk_base::RefCountedObject<MockVideoSource>();
445   source->SetVideoCapturer(capturer);
446
447   return new talk_base::RefCountedObject<MockLocalVideoTrack>(id, source.get());
448 }
449
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);
462 }
463
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);
470 }
471
472 webrtc::IceCandidateInterface*
473 MockMediaStreamDependencyFactory::CreateIceCandidate(
474     const std::string& sdp_mid,
475     int sdp_mline_index,
476     const std::string& sdp) {
477   return new MockIceCandidate(sdp_mid, sdp_mline_index, sdp);
478 }
479
480 scoped_refptr<WebRtcAudioCapturer>
481 MockMediaStreamDependencyFactory::MaybeCreateAudioCapturer(
482     int render_view_id, const StreamDeviceInfo& device_info) {
483   return WebRtcAudioCapturer::CreateCapturer();
484 }
485
486 }  // namespace content