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_stream_video_source.h"
13 #include "content/renderer/media/webrtc/mock_peer_connection_dependency_factory.h"
14 #include "content/renderer/media/webrtc/webrtc_local_audio_track_adapter.h"
15 #include "content/renderer/media/webrtc/webrtc_media_stream_adapter.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/WebKit/public/platform/WebMediaStream.h"
18 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
19 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
20 #include "third_party/WebKit/public/platform/WebVector.h"
24 class WebRtcMediaStreamAdapterTest
: public ::testing::Test
{
26 virtual void SetUp() {
27 child_process_
.reset(new ChildProcess());
28 dependency_factory_
.reset(new MockPeerConnectionDependencyFactory());
31 blink::WebMediaStream
CreateBlinkMediaStream(bool audio
, bool video
) {
32 blink::WebVector
<blink::WebMediaStreamTrack
> audio_track_vector(
33 audio
? static_cast<size_t>(1) : 0);
35 blink::WebMediaStreamSource audio_source
;
36 audio_source
.initialize("audio",
37 blink::WebMediaStreamSource::TypeAudio
,
39 audio_source
.setExtraData(new MediaStreamAudioSource());
41 audio_track_vector
[0].initialize(audio_source
);
42 MediaStreamTrack
* native_track
=
44 WebRtcLocalAudioTrackAdapter::Create(
45 audio_track_vector
[0].id().utf8(), NULL
),
47 audio_track_vector
[0].setExtraData(native_track
);
50 blink::WebVector
<blink::WebMediaStreamTrack
> video_track_vector(
51 video
? static_cast<size_t>(1) : 0);
52 MediaStreamSource::SourceStoppedCallback dummy_callback
;
54 blink::WebMediaStreamSource video_source
;
55 video_source
.initialize("video",
56 blink::WebMediaStreamSource::TypeVideo
,
58 MediaStreamVideoSource
* native_source
=
59 new MockMediaStreamVideoSource(false);
60 video_source
.setExtraData(native_source
);
61 blink::WebMediaConstraints constraints
;
62 constraints
.initialize();
63 video_track_vector
[0] = MediaStreamVideoTrack::CreateVideoTrack(
64 native_source
, constraints
,
65 MediaStreamVideoSource::ConstraintsCallback(), true);
68 blink::WebMediaStream stream_desc
;
69 stream_desc
.initialize("media stream",
72 stream_desc
.setExtraData(new MediaStream(stream_desc
));
76 void CreateWebRtcMediaStream(const blink::WebMediaStream
& blink_stream
,
77 size_t expected_number_of_audio_tracks
,
78 size_t expected_number_of_video_tracks
) {
79 adapter_
.reset(new WebRtcMediaStreamAdapter(
80 blink_stream
, dependency_factory_
.get()));
82 EXPECT_EQ(expected_number_of_audio_tracks
,
83 adapter_
->webrtc_media_stream()->GetAudioTracks().size());
84 EXPECT_EQ(expected_number_of_video_tracks
,
85 adapter_
->webrtc_media_stream()->GetVideoTracks().size());
86 EXPECT_EQ(blink_stream
.id().utf8(),
87 adapter_
->webrtc_media_stream()->label());
90 webrtc::MediaStreamInterface
* webrtc_stream() {
91 return adapter_
->webrtc_media_stream();
95 base::MessageLoop message_loop_
;
96 scoped_ptr
<ChildProcess
> child_process_
;
97 scoped_ptr
<MockPeerConnectionDependencyFactory
> dependency_factory_
;
98 scoped_ptr
<WebRtcMediaStreamAdapter
> adapter_
;
101 TEST_F(WebRtcMediaStreamAdapterTest
, CreateWebRtcMediaStream
) {
102 blink::WebMediaStream blink_stream
= CreateBlinkMediaStream(true, true);
103 CreateWebRtcMediaStream(blink_stream
, 1, 1);
106 // Test that we don't crash if a MediaStream is created in Blink with an unknown
107 // audio sources. This can happen if a MediaStream is created with
108 // remote audio track.
109 TEST_F(WebRtcMediaStreamAdapterTest
,
110 CreateWebRtcMediaStreamWithoutAudioSource
) {
111 // Create a blink MediaStream description.
112 blink::WebMediaStreamSource audio_source
;
113 audio_source
.initialize("audio source",
114 blink::WebMediaStreamSource::TypeAudio
,
117 blink::WebVector
<blink::WebMediaStreamTrack
> audio_tracks(
118 static_cast<size_t>(1));
119 audio_tracks
[0].initialize(audio_source
.id(), audio_source
);
120 blink::WebVector
<blink::WebMediaStreamTrack
> video_tracks(
121 static_cast<size_t>(0));
123 blink::WebMediaStream blink_stream
;
124 blink_stream
.initialize("new stream", audio_tracks
, video_tracks
);
125 blink_stream
.setExtraData(
126 new content::MediaStream(blink_stream
));
127 CreateWebRtcMediaStream(blink_stream
, 0, 0);
130 TEST_F(WebRtcMediaStreamAdapterTest
, RemoveAndAddTrack
) {
131 blink::WebMediaStream blink_stream
= CreateBlinkMediaStream(true, true);
132 CreateWebRtcMediaStream(blink_stream
, 1, 1);
134 MediaStream
* native_stream
= MediaStream::GetMediaStream(blink_stream
);
136 blink::WebVector
<blink::WebMediaStreamTrack
> audio_tracks
;
137 blink_stream
.audioTracks(audio_tracks
);
139 native_stream
->RemoveTrack(audio_tracks
[0]);
140 EXPECT_TRUE(webrtc_stream()->GetAudioTracks().empty());
142 blink::WebVector
<blink::WebMediaStreamTrack
> video_tracks
;
143 blink_stream
.videoTracks(video_tracks
);
145 native_stream
->RemoveTrack(video_tracks
[0]);
146 EXPECT_TRUE(webrtc_stream()->GetVideoTracks().empty());
148 native_stream
->AddTrack(audio_tracks
[0]);
149 EXPECT_EQ(1u, webrtc_stream()->GetAudioTracks().size());
151 native_stream
->AddTrack(video_tracks
[0]);
152 EXPECT_EQ(1u, webrtc_stream()->GetVideoTracks().size());
155 } // namespace content