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 "content/child/child_process.h"
9 #include "content/renderer/media/media_stream_video_track.h"
10 #include "content/renderer/media/mock_media_stream_video_sink.h"
11 #include "content/renderer/media/webrtc/media_stream_remote_video_source.h"
12 #include "content/renderer/media/webrtc/mock_peer_connection_dependency_factory.h"
13 #include "media/base/video_frame.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/WebKit/public/web/WebHeap.h"
16 #include "third_party/libjingle/source/talk/media/webrtc/webrtcvideoframe.h"
20 ACTION_P(RunClosure
, closure
) {
24 class MediaStreamRemoteVideoSourceUnderTest
25 : public MediaStreamRemoteVideoSource
{
27 MediaStreamRemoteVideoSourceUnderTest(webrtc::VideoTrackInterface
* track
)
28 : MediaStreamRemoteVideoSource(track
) {
30 using MediaStreamRemoteVideoSource::RenderInterfaceForTest
;
33 class MediaStreamRemoteVideoSourceTest
34 : public ::testing::Test
{
36 MediaStreamRemoteVideoSourceTest()
37 : child_process_(new ChildProcess()),
38 mock_factory_(new MockPeerConnectionDependencyFactory()),
39 webrtc_video_track_(mock_factory_
->CreateLocalVideoTrack(
41 static_cast<cricket::VideoCapturer
*>(NULL
))),
42 remote_source_(new MediaStreamRemoteVideoSourceUnderTest(
43 webrtc_video_track_
.get())),
44 number_of_successful_constraints_applied_(0),
45 number_of_failed_constraints_applied_(0) {
46 webkit_source_
.initialize(base::UTF8ToUTF16("dummy_source_id"),
47 blink::WebMediaStreamSource::TypeVideo
,
48 base::UTF8ToUTF16("dummy_source_name"));
49 webkit_source_
.setExtraData(remote_source_
);
52 virtual void TearDown() override
{
53 webkit_source_
.reset();
54 blink::WebHeap::collectAllGarbageForTesting();
57 MediaStreamRemoteVideoSourceUnderTest
* source() {
58 return remote_source_
;
61 MediaStreamVideoTrack
* CreateTrack() {
63 blink::WebMediaConstraints constraints
;
64 constraints
.initialize();
65 return new MediaStreamVideoTrack(
69 &MediaStreamRemoteVideoSourceTest::OnConstraintsApplied
,
70 base::Unretained(this)),
74 int NumberOfSuccessConstraintsCallbacks() const {
75 return number_of_successful_constraints_applied_
;
78 int NumberOfFailedConstraintsCallbacks() const {
79 return number_of_failed_constraints_applied_
;
82 void StopWebRtcTrack() {
83 static_cast<MockWebRtcVideoTrack
*>(webrtc_video_track_
.get())->set_state(
84 webrtc::MediaStreamTrackInterface::kEnded
);
87 const blink::WebMediaStreamSource
& webkit_source() const {
88 return webkit_source_
;
92 void OnConstraintsApplied(MediaStreamSource
* source
,
93 MediaStreamRequestResult result
,
94 const blink::WebString
& result_name
) {
95 ASSERT_EQ(source
, remote_source_
);
96 if (result
== MEDIA_DEVICE_OK
)
97 ++number_of_successful_constraints_applied_
;
99 ++number_of_failed_constraints_applied_
;
102 base::MessageLoopForUI message_loop_
;
103 scoped_ptr
<ChildProcess
> child_process_
;
104 scoped_ptr
<MockPeerConnectionDependencyFactory
> mock_factory_
;
105 scoped_refptr
<webrtc::VideoTrackInterface
> webrtc_video_track_
;
106 // |remote_source_| is owned by |webkit_source_|.
107 MediaStreamRemoteVideoSourceUnderTest
* remote_source_
;
108 blink::WebMediaStreamSource webkit_source_
;
109 int number_of_successful_constraints_applied_
;
110 int number_of_failed_constraints_applied_
;
113 TEST_F(MediaStreamRemoteVideoSourceTest
, StartTrack
) {
114 scoped_ptr
<MediaStreamVideoTrack
> track(CreateTrack());
115 EXPECT_EQ(1, NumberOfSuccessConstraintsCallbacks());
117 MockMediaStreamVideoSink sink
;
118 track
->AddSink(&sink
, sink
.GetDeliverFrameCB());
119 base::RunLoop run_loop
;
120 base::Closure quit_closure
= run_loop
.QuitClosure();
121 EXPECT_CALL(sink
, OnVideoFrame()).WillOnce(
122 RunClosure(quit_closure
));
123 cricket::WebRtcVideoFrame webrtc_frame
;
124 webrtc_frame
.InitToBlack(320, 240, 1, 1, 0, 1);
125 source()->RenderInterfaceForTest()->RenderFrame(&webrtc_frame
);
128 EXPECT_EQ(1, sink
.number_of_frames());
129 track
->RemoveSink(&sink
);
132 TEST_F(MediaStreamRemoteVideoSourceTest
, RemoteTrackStop
) {
133 scoped_ptr
<MediaStreamVideoTrack
> track(CreateTrack());
135 MockMediaStreamVideoSink sink
;
136 track
->AddSink(&sink
, sink
.GetDeliverFrameCB());
137 EXPECT_EQ(blink::WebMediaStreamSource::ReadyStateLive
, sink
.state());
138 EXPECT_EQ(blink::WebMediaStreamSource::ReadyStateLive
,
139 webkit_source().readyState());
141 EXPECT_EQ(blink::WebMediaStreamSource::ReadyStateEnded
,
142 webkit_source().readyState());
143 EXPECT_EQ(blink::WebMediaStreamSource::ReadyStateEnded
, sink
.state());
145 track
->RemoveSink(&sink
);
148 } // namespace content