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 "base/memory/scoped_ptr.h"
6 #include "content/common/media/media_stream_options.h"
7 #include "content/renderer/media/media_stream_extra_data.h"
8 #include "content/renderer/media/media_stream_source_extra_data.h"
9 #include "content/renderer/media/mock_media_stream_dependency_factory.h"
10 #include "content/renderer/media/mock_web_rtc_peer_connection_handler_client.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/WebKit/Source/Platform/chromium/public/WebMediaConstraints.h"
13 #include "third_party/WebKit/Source/Platform/chromium/public/WebMediaStream.h"
14 #include "third_party/WebKit/Source/Platform/chromium/public/WebMediaStreamSource.h"
15 #include "third_party/WebKit/Source/Platform/chromium/public/WebMediaStreamTrack.h"
16 #include "third_party/WebKit/Source/Platform/chromium/public/WebRTCPeerConnectionHandler.h"
17 #include "third_party/WebKit/Source/Platform/chromium/public/WebVector.h"
18 #include "third_party/libjingle/source/talk/app/webrtc/videosourceinterface.h"
22 class MediaSourceCreatedObserver
{
24 MediaSourceCreatedObserver()
29 void OnCreateNativeSourcesComplete(
30 WebKit::WebMediaStream
* description
,
31 bool request_succeeded
) {
32 result_
= request_succeeded
;
33 description_
= description
;
36 WebKit::WebMediaStream
* description() const {
39 bool result() const { return result_
; }
43 WebKit::WebMediaStream
* description_
;
46 class MediaStreamDependencyFactoryTest
: public ::testing::Test
{
48 virtual void SetUp() {
49 dependency_factory_
.reset(new MockMediaStreamDependencyFactory());
52 WebKit::WebMediaStream
CreateWebKitMediaStream(bool audio
, bool video
) {
53 WebKit::WebVector
<WebKit::WebMediaStreamSource
> audio_sources(
54 audio
? static_cast<size_t>(1) : 0);
55 WebKit::WebVector
<WebKit::WebMediaStreamSource
> video_sources(
56 video
? static_cast<size_t>(1) : 0);
59 StreamDeviceInfo info
;
60 info
.device
.type
= content::MEDIA_DEVICE_AUDIO_CAPTURE
;
61 info
.device
.name
= "audio";
62 info
.device
.sample_rate
= 0;
63 info
.device
.channel_layout
= 0;
65 audio_sources
[0].initialize("audio",
66 WebKit::WebMediaStreamSource::TypeAudio
,
68 audio_sources
[0].setExtraData(
69 new MediaStreamSourceExtraData(info
));
72 StreamDeviceInfo info
;
73 info
.device
.type
= content::MEDIA_DEVICE_VIDEO_CAPTURE
;
74 info
.device
.name
= "video";
76 video_sources
[0].initialize("video",
77 WebKit::WebMediaStreamSource::TypeVideo
,
79 video_sources
[0].setExtraData(
80 new MediaStreamSourceExtraData(info
));
82 WebKit::WebMediaStream stream_desc
;
83 stream_desc
.initialize("media stream", audio_sources
, video_sources
);
88 void CreateNativeSources(WebKit::WebMediaStream
* descriptor
) {
89 MediaSourceCreatedObserver observer
;
90 WebKit::WebMediaConstraints audio_constraints
;
91 dependency_factory_
->CreateNativeMediaSources(
92 WebKit::WebMediaConstraints(),
93 WebKit::WebMediaConstraints(),
96 &MediaSourceCreatedObserver::OnCreateNativeSourcesComplete
,
97 base::Unretained(&observer
)));
99 EXPECT_FALSE(observer
.result());
100 // Change the state of the created source to live. This should trigger
101 // MediaSourceCreatedObserver::OnCreateNativeSourcesComplete
102 if (dependency_factory_
->last_video_source()) {
103 dependency_factory_
->last_audio_source()->SetLive();
104 dependency_factory_
->last_video_source()->SetLive();
106 EXPECT_TRUE(observer
.result());
107 EXPECT_TRUE(observer
.description() == descriptor
);
110 void VerifyMediaStream(const WebKit::WebMediaStream
& stream_desc
,
111 size_t num_audio_tracks
,
112 size_t num_video_tracks
) {
113 content::MediaStreamExtraData
* extra_data
=
114 static_cast<content::MediaStreamExtraData
*>(stream_desc
.extraData());
115 ASSERT_TRUE(extra_data
&& extra_data
->stream());
116 EXPECT_TRUE(extra_data
->is_local());
117 EXPECT_EQ(num_audio_tracks
, extra_data
->stream()->GetAudioTracks().size());
118 EXPECT_EQ(num_video_tracks
, extra_data
->stream()->GetVideoTracks().size());
122 scoped_ptr
<MockMediaStreamDependencyFactory
> dependency_factory_
;
125 TEST_F(MediaStreamDependencyFactoryTest
, CreateRTCPeerConnectionHandler
) {
126 MockWebRTCPeerConnectionHandlerClient client_jsep
;
127 scoped_ptr
<WebKit::WebRTCPeerConnectionHandler
> pc_handler(
128 dependency_factory_
->CreateRTCPeerConnectionHandler(&client_jsep
));
129 EXPECT_TRUE(pc_handler
.get() != NULL
);
132 TEST_F(MediaStreamDependencyFactoryTest
, CreateNativeMediaStream
) {
133 WebKit::WebMediaStream stream_desc
= CreateWebKitMediaStream(true, true);
134 CreateNativeSources(&stream_desc
);
136 dependency_factory_
->CreateNativeLocalMediaStream(&stream_desc
);
137 VerifyMediaStream(stream_desc
, 1, 1);
140 // Test that we don't crash if a MediaStream is created in WebKit with unknown
141 // sources. This can for example happen if a MediaStream is created with
143 TEST_F(MediaStreamDependencyFactoryTest
, CreateNativeMediaStreamWithoutSource
) {
144 // Create a WebKit MediaStream description.
145 WebKit::WebMediaStream stream_desc
;
146 WebKit::WebVector
<WebKit::WebMediaStreamSource
> audio_sources(
147 static_cast<size_t>(1));
148 audio_sources
[0].initialize("audio source",
149 WebKit::WebMediaStreamSource::TypeAudio
,
151 WebKit::WebVector
<WebKit::WebMediaStreamSource
> video_sources(
152 static_cast<size_t>(1));
153 video_sources
[0].initialize("video source",
154 WebKit::WebMediaStreamSource::TypeVideo
,
156 stream_desc
.initialize("new stream", audio_sources
, video_sources
);
158 EXPECT_TRUE(dependency_factory_
->EnsurePeerConnectionFactory());
159 dependency_factory_
->CreateNativeLocalMediaStream(&stream_desc
);
160 VerifyMediaStream(stream_desc
, 0, 0);
163 TEST_F(MediaStreamDependencyFactoryTest
, AddAndRemoveNativeTrack
) {
164 WebKit::WebMediaStream stream_desc
= CreateWebKitMediaStream(true, true);
165 CreateNativeSources(&stream_desc
);
167 dependency_factory_
->CreateNativeLocalMediaStream(&stream_desc
);
168 VerifyMediaStream(stream_desc
, 1, 1);
170 WebKit::WebVector
<WebKit::WebMediaStreamTrack
> audio_tracks
;
171 stream_desc
.audioTracks(audio_tracks
);
172 EXPECT_TRUE(dependency_factory_
->RemoveNativeMediaStreamTrack(
173 stream_desc
, audio_tracks
[0]));
174 VerifyMediaStream(stream_desc
, 0, 1);
176 EXPECT_TRUE(dependency_factory_
->AddNativeMediaStreamTrack(
177 stream_desc
, audio_tracks
[0]));
178 VerifyMediaStream(stream_desc
, 1, 1);
180 WebKit::WebVector
<WebKit::WebMediaStreamTrack
> video_tracks
;
181 stream_desc
.videoTracks(video_tracks
);
182 EXPECT_TRUE(dependency_factory_
->RemoveNativeMediaStreamTrack(
183 stream_desc
, video_tracks
[0]));
184 VerifyMediaStream(stream_desc
, 1, 0);
186 EXPECT_TRUE(dependency_factory_
->AddNativeMediaStreamTrack(
187 stream_desc
, video_tracks
[0]));
188 VerifyMediaStream(stream_desc
, 1, 1);
191 } // namespace content