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 "base/memory/scoped_ptr.h"
6 #include "base/message_loop/message_loop.h"
7 #include "content/child/child_process.h"
8 #include "content/renderer/media/media_stream.h"
9 #include "content/renderer/media/media_stream_audio_source.h"
10 #include "content/renderer/media/media_stream_video_source.h"
11 #include "content/renderer/media/media_stream_video_track.h"
12 #include "content/renderer/media/mock_media_constraint_factory.h"
13 #include "content/renderer/media/mock_media_stream_video_source.h"
14 #include "content/renderer/media/webrtc/mock_peer_connection_dependency_factory.h"
15 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h"
16 #include "content/renderer/media/webrtc/webrtc_media_stream_adapter.h"
17 #include "content/renderer/media/webrtc_local_audio_track.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "third_party/WebKit/public/platform/WebMediaStream.h"
20 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
21 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
22 #include "third_party/WebKit/public/platform/WebVector.h"
23 #include "third_party/WebKit/public/web/WebHeap.h"
27 class WebRtcMediaStreamAdapterTest
: public ::testing::Test
{
29 void SetUp() override
{
30 child_process_
.reset(new ChildProcess());
31 dependency_factory_
.reset(new MockPeerConnectionDependencyFactory());
34 void TearDown() override
{
36 blink::WebHeap::collectAllGarbageForTesting();
39 blink::WebMediaStream
CreateBlinkMediaStream(bool audio
, bool video
) {
40 blink::WebVector
<blink::WebMediaStreamTrack
> audio_track_vector(
41 audio
? static_cast<size_t>(1) : 0);
43 blink::WebMediaStreamSource audio_source
;
44 audio_source
.initialize("audio",
45 blink::WebMediaStreamSource::TypeAudio
,
47 false /* remote */, true /* readonly */);
48 audio_source
.setExtraData(new MediaStreamAudioSource());
50 audio_track_vector
[0].initialize(audio_source
);
51 StreamDeviceInfo
device_info(MEDIA_DEVICE_AUDIO_CAPTURE
, "Mock device",
53 MockMediaConstraintFactory constraint_factory
;
54 const blink::WebMediaConstraints constraints
=
55 constraint_factory
.CreateWebMediaConstraints();
56 scoped_refptr
<WebRtcAudioCapturer
> capturer(
57 WebRtcAudioCapturer::CreateCapturer(-1, device_info
, constraints
,
59 scoped_refptr
<WebRtcLocalAudioTrackAdapter
> adapter(
60 WebRtcLocalAudioTrackAdapter::Create(
61 audio_track_vector
[0].id().utf8(), nullptr));
62 scoped_ptr
<WebRtcLocalAudioTrack
> native_track(
63 new WebRtcLocalAudioTrack(adapter
.get(), capturer
, nullptr));
64 audio_track_vector
[0].setExtraData(native_track
.release());
67 blink::WebVector
<blink::WebMediaStreamTrack
> video_track_vector(
68 video
? static_cast<size_t>(1) : 0);
69 MediaStreamSource::SourceStoppedCallback dummy_callback
;
71 blink::WebMediaStreamSource video_source
;
72 video_source
.initialize("video",
73 blink::WebMediaStreamSource::TypeVideo
,
75 false /* remote */, true /* readonly */);
76 MediaStreamVideoSource
* native_source
=
77 new MockMediaStreamVideoSource(false);
78 video_source
.setExtraData(native_source
);
79 blink::WebMediaConstraints constraints
;
80 constraints
.initialize();
81 video_track_vector
[0] = MediaStreamVideoTrack::CreateVideoTrack(
82 native_source
, constraints
,
83 MediaStreamVideoSource::ConstraintsCallback(), true);
86 blink::WebMediaStream stream_desc
;
87 stream_desc
.initialize("media stream",
90 stream_desc
.setExtraData(new MediaStream(stream_desc
));
94 void CreateWebRtcMediaStream(const blink::WebMediaStream
& blink_stream
,
95 size_t expected_number_of_audio_tracks
,
96 size_t expected_number_of_video_tracks
) {
97 adapter_
.reset(new WebRtcMediaStreamAdapter(
98 blink_stream
, dependency_factory_
.get()));
100 EXPECT_EQ(expected_number_of_audio_tracks
,
101 adapter_
->webrtc_media_stream()->GetAudioTracks().size());
102 EXPECT_EQ(expected_number_of_video_tracks
,
103 adapter_
->webrtc_media_stream()->GetVideoTracks().size());
104 EXPECT_EQ(blink_stream
.id().utf8(),
105 adapter_
->webrtc_media_stream()->label());
108 webrtc::MediaStreamInterface
* webrtc_stream() {
109 return adapter_
->webrtc_media_stream();
113 base::MessageLoop message_loop_
;
114 scoped_ptr
<ChildProcess
> child_process_
;
115 scoped_ptr
<MockPeerConnectionDependencyFactory
> dependency_factory_
;
116 scoped_ptr
<WebRtcMediaStreamAdapter
> adapter_
;
119 TEST_F(WebRtcMediaStreamAdapterTest
, CreateWebRtcMediaStream
) {
120 blink::WebMediaStream blink_stream
= CreateBlinkMediaStream(true, true);
121 CreateWebRtcMediaStream(blink_stream
, 1, 1);
124 // Test that we don't crash if a MediaStream is created in Blink with an unknown
125 // audio sources. This can happen if a MediaStream is created with
126 // remote audio track.
127 TEST_F(WebRtcMediaStreamAdapterTest
,
128 CreateWebRtcMediaStreamWithoutAudioSource
) {
129 // Create a blink MediaStream description.
130 blink::WebMediaStreamSource audio_source
;
131 audio_source
.initialize("audio source",
132 blink::WebMediaStreamSource::TypeAudio
,
134 false /* remote */, true /* readonly */);
136 blink::WebVector
<blink::WebMediaStreamTrack
> audio_tracks(
137 static_cast<size_t>(1));
138 audio_tracks
[0].initialize(audio_source
.id(), audio_source
);
139 blink::WebVector
<blink::WebMediaStreamTrack
> video_tracks(
140 static_cast<size_t>(0));
142 blink::WebMediaStream blink_stream
;
143 blink_stream
.initialize("new stream", audio_tracks
, video_tracks
);
144 blink_stream
.setExtraData(
145 new content::MediaStream(blink_stream
));
146 CreateWebRtcMediaStream(blink_stream
, 0, 0);
149 TEST_F(WebRtcMediaStreamAdapterTest
, RemoveAndAddTrack
) {
150 blink::WebMediaStream blink_stream
= CreateBlinkMediaStream(true, true);
151 CreateWebRtcMediaStream(blink_stream
, 1, 1);
153 MediaStream
* native_stream
= MediaStream::GetMediaStream(blink_stream
);
155 blink::WebVector
<blink::WebMediaStreamTrack
> audio_tracks
;
156 blink_stream
.audioTracks(audio_tracks
);
158 native_stream
->RemoveTrack(audio_tracks
[0]);
159 EXPECT_TRUE(webrtc_stream()->GetAudioTracks().empty());
161 blink::WebVector
<blink::WebMediaStreamTrack
> video_tracks
;
162 blink_stream
.videoTracks(video_tracks
);
164 native_stream
->RemoveTrack(video_tracks
[0]);
165 EXPECT_TRUE(webrtc_stream()->GetVideoTracks().empty());
167 native_stream
->AddTrack(audio_tracks
[0]);
168 EXPECT_EQ(1u, webrtc_stream()->GetAudioTracks().size());
170 native_stream
->AddTrack(video_tracks
[0]);
171 EXPECT_EQ(1u, webrtc_stream()->GetVideoTracks().size());
174 } // namespace content