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
;
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
) {
47 MockMediaStream::MockMediaStream(const std::string
& label
) : label_(label
) {}
49 bool MockMediaStream::AddTrack(AudioTrackInterface
* track
) {
50 audio_track_vector_
.push_back(track
);
55 bool MockMediaStream::AddTrack(VideoTrackInterface
* track
) {
56 video_track_vector_
.push_back(track
);
61 bool MockMediaStream::RemoveTrack(AudioTrackInterface
* track
) {
62 AudioTrackVector::iterator it
= FindTrack(&audio_track_vector_
,
64 if (it
== audio_track_vector_
.end())
66 audio_track_vector_
.erase(it
);
71 bool MockMediaStream::RemoveTrack(VideoTrackInterface
* track
) {
72 VideoTrackVector::iterator it
= FindTrack(&video_track_vector_
,
74 if (it
== video_track_vector_
.end())
76 video_track_vector_
.erase(it
);
81 std::string
MockMediaStream::label() const {
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();
123 MockMediaStream::~MockMediaStream() {}
125 class MockRtcVideoCapturer
: public WebRtcVideoCapturerAdapter
{
127 explicit MockRtcVideoCapturer(bool is_screencast
)
128 : WebRtcVideoCapturerAdapter(is_screencast
),
129 number_of_capturered_frames_(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 {
144 int GetLastFrameHeight() const {
148 int GetFrameNum() const {
149 return number_of_capturered_frames_
;
153 int number_of_capturered_frames_
;
158 MockVideoRenderer::MockVideoRenderer()
163 MockVideoRenderer::~MockVideoRenderer() {}
165 bool MockVideoRenderer::SetSize(int width
, int height
, int reserved
) {
171 bool MockVideoRenderer::RenderFrame(const cricket::VideoFrame
* frame
) {
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 {
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
) {
217 void MockVideoSource::RemoveSink(cricket::VideoRenderer
* output
) {
221 void MockVideoSource::Stop() {
224 void MockVideoSource::Restart() {
228 cricket::VideoRenderer
* MockVideoSource::FrameInput() {
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
);
246 void MockVideoSource::FireOnChanged() {
247 std::vector
<ObserverInterface
*> observers(observers_
);
248 for (std::vector
<ObserverInterface
*>::iterator it
= observers
.begin();
249 it
!= observers
.end(); ++it
) {
254 void MockVideoSource::SetLive() {
255 DCHECK(state_
== MediaSourceInterface::kInitializing
||
256 state_
== MediaSourceInterface::kLive
);
257 state_
= MediaSourceInterface::kLive
;
261 void MockVideoSource::SetEnded() {
262 DCHECK_NE(MediaSourceInterface::kEnded
, state_
);
263 state_
= MediaSourceInterface::kEnded
;
267 webrtc::MediaSourceInterface::SourceState
MockVideoSource::state() const {
271 const cricket::VideoOptions
* MockVideoSource::options() const {
276 int MockVideoSource::GetLastFrameWidth() const {
279 static_cast<MockRtcVideoCapturer
*>(capturer_
.get())->GetLastFrameWidth();
282 int MockVideoSource::GetLastFrameHeight() const {
285 static_cast<MockRtcVideoCapturer
*>(capturer_
.get())->GetLastFrameHeight();
288 int MockVideoSource::GetFrameNum() const {
290 return static_cast<MockRtcVideoCapturer
*>(capturer_
.get())->GetFrameNum();
293 MockWebRtcVideoTrack::MockWebRtcVideoTrack(
294 const std::string
& id
,
295 webrtc::VideoSourceInterface
* source
)
298 state_(MediaStreamTrackInterface::kLive
),
303 MockWebRtcVideoTrack::~MockWebRtcVideoTrack() {}
305 void MockWebRtcVideoTrack::AddRenderer(VideoRendererInterface
* renderer
) {
307 renderer_
= renderer
;
310 void MockWebRtcVideoTrack::RemoveRenderer(VideoRendererInterface
* renderer
) {
311 DCHECK_EQ(renderer_
, renderer
);
315 std::string
MockWebRtcVideoTrack::kind() const {
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 {
328 bool MockWebRtcVideoTrack::set_enabled(bool enable
) {
333 bool MockWebRtcVideoTrack::set_state(TrackState new_state
) {
335 for (auto& o
: observers_
)
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
{
356 MockSessionDescription(const std::string
& type
,
357 const std::string
& sdp
)
361 ~MockSessionDescription() override
{}
362 cricket::SessionDescription
* description() override
{
366 const cricket::SessionDescription
* description() const override
{
370 std::string
session_id() const override
{
372 return std::string();
374 std::string
session_version() const override
{
376 return std::string();
378 std::string
type() const override
{ return type_
; }
379 bool AddCandidate(const IceCandidateInterface
* candidate
) override
{
383 size_t number_of_mediasections() const override
{
387 const IceCandidateCollection
* candidates(
388 size_t mediasection_index
) const override
{
393 bool ToString(std::string
* out
) const override
{
403 class MockIceCandidate
: public IceCandidateInterface
{
405 MockIceCandidate(const std::string
& sdp_mid
,
407 const std::string
& sdp
)
409 sdp_mline_index_(sdp_mline_index
),
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
{
424 std::string sdp_mid_
;
425 int sdp_mline_index_
;
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
) {
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
) {
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
>(
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
);
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
,
515 const std::string
& sdp
) {
516 return new MockIceCandidate(sdp_mid
, sdp_mline_index
, sdp
);
519 scoped_refptr
<WebRtcAudioCapturer
>
520 MockPeerConnectionDependencyFactory::CreateAudioCapturer(
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;
529 DCHECK(audio_source
);
530 return WebRtcAudioCapturer::CreateCapturer(-1, device_info
, constraints
, NULL
,
534 void MockPeerConnectionDependencyFactory::StartLocalAudioTrack(
535 WebRtcLocalAudioTrack
* audio_track
) {
536 audio_track
->Start();
539 } // namespace content