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/message_loop/message_loop.h"
6 #include "base/run_loop.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "base/thread_task_runner_handle.h"
9 #include "content/child/child_process.h"
10 #include "content/renderer/media/media_stream_video_track.h"
11 #include "content/renderer/media/mock_media_stream_video_sink.h"
12 #include "content/renderer/media/webrtc/media_stream_remote_video_source.h"
13 #include "content/renderer/media/webrtc/mock_peer_connection_dependency_factory.h"
14 #include "content/renderer/media/webrtc/track_observer.h"
15 #include "media/base/video_frame.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/WebKit/public/web/WebHeap.h"
18 #include "third_party/libjingle/source/talk/media/webrtc/webrtcvideoframe.h"
22 ACTION_P(RunClosure
, closure
) {
26 class MediaStreamRemoteVideoSourceUnderTest
27 : public MediaStreamRemoteVideoSource
{
29 explicit MediaStreamRemoteVideoSourceUnderTest(
30 scoped_ptr
<TrackObserver
> observer
)
31 : MediaStreamRemoteVideoSource(observer
.Pass()) {
33 using MediaStreamRemoteVideoSource::RenderInterfaceForTest
;
36 class MediaStreamRemoteVideoSourceTest
37 : public ::testing::Test
{
39 MediaStreamRemoteVideoSourceTest()
40 : child_process_(new ChildProcess()),
41 mock_factory_(new MockPeerConnectionDependencyFactory()),
42 webrtc_video_track_(mock_factory_
->CreateLocalVideoTrack(
44 static_cast<cricket::VideoCapturer
*>(NULL
))),
45 remote_source_(new MediaStreamRemoteVideoSourceUnderTest(
46 scoped_ptr
<TrackObserver
>(
47 new TrackObserver(base::ThreadTaskRunnerHandle::Get(),
48 webrtc_video_track_
.get())).Pass())),
49 number_of_successful_constraints_applied_(0),
50 number_of_failed_constraints_applied_(0) {
51 webkit_source_
.initialize(base::UTF8ToUTF16("dummy_source_id"),
52 blink::WebMediaStreamSource::TypeVideo
,
53 base::UTF8ToUTF16("dummy_source_name"),
54 true /* remote */ , true /* readonly */);
55 webkit_source_
.setExtraData(remote_source_
);
58 void TearDown() override
{
59 webkit_source_
.reset();
60 blink::WebHeap::collectAllGarbageForTesting();
63 MediaStreamRemoteVideoSourceUnderTest
* source() {
64 return remote_source_
;
67 MediaStreamVideoTrack
* CreateTrack() {
69 blink::WebMediaConstraints constraints
;
70 constraints
.initialize();
71 return new MediaStreamVideoTrack(
75 &MediaStreamRemoteVideoSourceTest::OnConstraintsApplied
,
76 base::Unretained(this)),
80 int NumberOfSuccessConstraintsCallbacks() const {
81 return number_of_successful_constraints_applied_
;
84 int NumberOfFailedConstraintsCallbacks() const {
85 return number_of_failed_constraints_applied_
;
88 void StopWebRtcTrack() {
89 static_cast<MockWebRtcVideoTrack
*>(webrtc_video_track_
.get())->set_state(
90 webrtc::MediaStreamTrackInterface::kEnded
);
93 const blink::WebMediaStreamSource
& webkit_source() const {
94 return webkit_source_
;
98 void OnConstraintsApplied(MediaStreamSource
* source
,
99 MediaStreamRequestResult result
,
100 const blink::WebString
& result_name
) {
101 ASSERT_EQ(source
, remote_source_
);
102 if (result
== MEDIA_DEVICE_OK
)
103 ++number_of_successful_constraints_applied_
;
105 ++number_of_failed_constraints_applied_
;
108 base::MessageLoopForUI message_loop_
;
109 scoped_ptr
<ChildProcess
> child_process_
;
110 scoped_ptr
<MockPeerConnectionDependencyFactory
> mock_factory_
;
111 scoped_refptr
<webrtc::VideoTrackInterface
> webrtc_video_track_
;
112 // |remote_source_| is owned by |webkit_source_|.
113 MediaStreamRemoteVideoSourceUnderTest
* remote_source_
;
114 blink::WebMediaStreamSource webkit_source_
;
115 int number_of_successful_constraints_applied_
;
116 int number_of_failed_constraints_applied_
;
119 TEST_F(MediaStreamRemoteVideoSourceTest
, StartTrack
) {
120 scoped_ptr
<MediaStreamVideoTrack
> track(CreateTrack());
121 EXPECT_EQ(1, NumberOfSuccessConstraintsCallbacks());
123 MockMediaStreamVideoSink sink
;
124 track
->AddSink(&sink
, sink
.GetDeliverFrameCB());
125 base::RunLoop run_loop
;
126 base::Closure quit_closure
= run_loop
.QuitClosure();
127 EXPECT_CALL(sink
, OnVideoFrame()).WillOnce(
128 RunClosure(quit_closure
));
129 cricket::WebRtcVideoFrame webrtc_frame
;
130 webrtc_frame
.InitToBlack(320, 240, 1, 1, 0, 1);
131 source()->RenderInterfaceForTest()->RenderFrame(&webrtc_frame
);
134 EXPECT_EQ(1, sink
.number_of_frames());
135 track
->RemoveSink(&sink
);
138 TEST_F(MediaStreamRemoteVideoSourceTest
, RemoteTrackStop
) {
139 scoped_ptr
<MediaStreamVideoTrack
> track(CreateTrack());
141 MockMediaStreamVideoSink sink
;
142 track
->AddSink(&sink
, sink
.GetDeliverFrameCB());
143 EXPECT_EQ(blink::WebMediaStreamSource::ReadyStateLive
, sink
.state());
144 EXPECT_EQ(blink::WebMediaStreamSource::ReadyStateLive
,
145 webkit_source().readyState());
147 base::RunLoop().RunUntilIdle();
148 EXPECT_EQ(blink::WebMediaStreamSource::ReadyStateEnded
,
149 webkit_source().readyState());
150 EXPECT_EQ(blink::WebMediaStreamSource::ReadyStateEnded
, sink
.state());
152 track
->RemoveSink(&sink
);
155 } // namespace content