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 // Tests PPB_MediaStreamVideoTrack interface.
7 #include "ppapi/tests/test_media_stream_video_track.h"
9 #include "ppapi/c/private/ppb_testing_private.h"
10 #include "ppapi/cpp/completion_callback.h"
11 #include "ppapi/cpp/instance.h"
12 #include "ppapi/cpp/var.h"
13 #include "ppapi/cpp/video_frame.h"
14 #include "ppapi/tests/test_utils.h"
15 #include "ppapi/tests/testing_instance.h"
17 REGISTER_TEST_CASE(MediaStreamVideoTrack
);
21 const int32_t kTimes
= 3;
22 const int32_t kDefaultWidth
= 640;
23 const int32_t kDefaultHeight
= 480;
24 const char kJSCode
[] =
25 "function gotStream(stream) {"
26 " var track = stream.getVideoTracks()[0];"
27 " var plugin = document.getElementById('plugin');"
28 " plugin.postMessage(track);"
32 " video: { mandatory: { minWidth: 640, minHeight: 480 } }"
34 "navigator.getUserMedia ="
35 " navigator.getUserMedia || navigator.webkitGetUserMedia;"
36 "navigator.getUserMedia(constraints,"
37 " gotStream, function() {});";
40 TestMediaStreamVideoTrack::TestMediaStreamVideoTrack(TestingInstance
* instance
)
42 event_(instance_
->pp_instance()) {
45 bool TestMediaStreamVideoTrack::Init() {
49 TestMediaStreamVideoTrack::~TestMediaStreamVideoTrack() {
52 void TestMediaStreamVideoTrack::RunTests(const std::string
& filter
) {
53 RUN_TEST(Create
, filter
);
54 RUN_TEST(GetFrame
, filter
);
55 RUN_TEST(Configure
, filter
);
58 void TestMediaStreamVideoTrack::HandleMessage(const pp::Var
& message
) {
59 if (message
.is_resource())
60 video_track_
= pp::MediaStreamVideoTrack(message
.AsResource());
64 std::string
TestMediaStreamVideoTrack::TestCreate() {
66 instance_
->EvalScript(kJSCode
);
70 ASSERT_FALSE(video_track_
.is_null());
71 ASSERT_FALSE(video_track_
.HasEnded());
72 ASSERT_FALSE(video_track_
.GetId().empty());
76 ASSERT_TRUE(video_track_
.HasEnded());
77 video_track_
= pp::MediaStreamVideoTrack();
81 std::string
TestMediaStreamVideoTrack::TestGetFrame() {
83 instance_
->EvalScript(kJSCode
);
87 ASSERT_FALSE(video_track_
.is_null());
88 ASSERT_FALSE(video_track_
.HasEnded());
89 ASSERT_FALSE(video_track_
.GetId().empty());
91 PP_TimeDelta timestamp
= 0.0;
93 // Get |kTimes| frames.
94 for (int i
= 0; i
< kTimes
; ++i
) {
95 TestCompletionCallbackWithOutput
<pp::VideoFrame
> cc(
96 instance_
->pp_instance(), false);
97 cc
.WaitForResult(video_track_
.GetFrame(cc
.GetCallback()));
98 ASSERT_EQ(PP_OK
, cc
.result());
99 pp::VideoFrame frame
= cc
.output();
100 ASSERT_FALSE(frame
.is_null());
101 ASSERT_EQ(frame
.GetFormat(), PP_VIDEOFRAME_FORMAT_YV12
);
104 ASSERT_TRUE(frame
.GetSize(&size
));
105 ASSERT_EQ(size
.width(), kDefaultWidth
);
106 ASSERT_EQ(size
.height(), kDefaultHeight
);
108 ASSERT_GE(frame
.GetTimestamp(), timestamp
);
109 timestamp
= frame
.GetTimestamp();
111 ASSERT_GT(frame
.GetDataBufferSize(), 0U);
112 ASSERT_TRUE(frame
.GetDataBuffer() != NULL
);
114 video_track_
.RecycleFrame(frame
);
116 // A recycled frame should be invalidated.
117 ASSERT_EQ(frame
.GetFormat(), PP_VIDEOFRAME_FORMAT_UNKNOWN
);
118 ASSERT_FALSE(frame
.GetSize(&size
));
119 ASSERT_EQ(frame
.GetDataBufferSize(), 0U);
120 ASSERT_TRUE(frame
.GetDataBuffer() == NULL
);
124 video_track_
.Close();
125 ASSERT_TRUE(video_track_
.HasEnded());
126 video_track_
= pp::MediaStreamVideoTrack();
130 std::string
TestMediaStreamVideoTrack::TestConfigure() {
132 instance_
->EvalScript(kJSCode
);
136 ASSERT_FALSE(video_track_
.is_null());
137 ASSERT_FALSE(video_track_
.HasEnded());
138 ASSERT_FALSE(video_track_
.GetId().empty());
143 int32_t expected_format
;
145 { PP_VIDEOFRAME_FORMAT_BGRA
, PP_VIDEOFRAME_FORMAT_BGRA
}, // To RGBA.
146 { PP_VIDEOFRAME_FORMAT_I420
, PP_VIDEOFRAME_FORMAT_I420
}, // To I420.
147 { PP_VIDEOFRAME_FORMAT_YV12
, PP_VIDEOFRAME_FORMAT_YV12
}, // To YV12.
148 { PP_VIDEOFRAME_FORMAT_BGRA
, PP_VIDEOFRAME_FORMAT_BGRA
}, // To RGBA.
149 { PP_VIDEOFRAME_FORMAT_UNKNOWN
, PP_VIDEOFRAME_FORMAT_YV12
}, // To default.
151 for (size_t i
= 0; i
< sizeof(formats
) / sizeof(formats
[0]); ++i
) {
152 TestCompletionCallback
cc1(instance_
->pp_instance(), false);
153 int32_t attrib_list
[] = {
154 PP_MEDIASTREAMVIDEOTRACK_ATTRIB_FORMAT
, formats
[i
].format
,
155 PP_MEDIASTREAMVIDEOTRACK_ATTRIB_NONE
,
157 cc1
.WaitForResult(video_track_
.Configure(attrib_list
, cc1
.GetCallback()));
158 ASSERT_EQ(PP_OK
, cc1
.result());
160 for (int j
= 0; j
< kTimes
; ++j
) {
161 TestCompletionCallbackWithOutput
<pp::VideoFrame
> cc2(
162 instance_
->pp_instance(), false);
163 cc2
.WaitForResult(video_track_
.GetFrame(cc2
.GetCallback()));
164 ASSERT_EQ(PP_OK
, cc2
.result());
165 pp::VideoFrame frame
= cc2
.output();
166 ASSERT_FALSE(frame
.is_null());
167 ASSERT_EQ(frame
.GetFormat(), formats
[i
].expected_format
);
170 ASSERT_TRUE(frame
.GetSize(&size
));
171 ASSERT_EQ(size
.width(), kDefaultWidth
);
172 ASSERT_EQ(size
.height(), kDefaultHeight
);
174 ASSERT_GT(frame
.GetDataBufferSize(), 0U);
175 ASSERT_TRUE(frame
.GetDataBuffer() != NULL
);
177 video_track_
.RecycleFrame(frame
);
185 int32_t expect_width
;
186 int32_t expect_height
;
188 { 72, 72, 72, 72 }, // To 72x27.
189 { 1024, 768, 1024, 768 }, // To 1024x768.
190 { 0, 0, kDefaultWidth
, kDefaultHeight
}, // To default.
192 for (size_t i
= 0; i
< sizeof(sizes
) / sizeof(sizes
[0]); ++i
) {
193 TestCompletionCallback
cc1(instance_
->pp_instance(), false);
194 int32_t attrib_list
[] = {
195 PP_MEDIASTREAMVIDEOTRACK_ATTRIB_WIDTH
, sizes
[i
].width
,
196 PP_MEDIASTREAMVIDEOTRACK_ATTRIB_HEIGHT
, sizes
[i
].height
,
197 PP_MEDIASTREAMVIDEOTRACK_ATTRIB_NONE
,
199 cc1
.WaitForResult(video_track_
.Configure(attrib_list
, cc1
.GetCallback()));
200 ASSERT_EQ(PP_OK
, cc1
.result());
202 for (int j
= 0; j
< kTimes
; ++j
) {
203 TestCompletionCallbackWithOutput
<pp::VideoFrame
> cc2(
204 instance_
->pp_instance(), false);
205 cc2
.WaitForResult(video_track_
.GetFrame(cc2
.GetCallback()));
206 ASSERT_EQ(PP_OK
, cc2
.result());
207 pp::VideoFrame frame
= cc2
.output();
208 ASSERT_FALSE(frame
.is_null());
209 ASSERT_EQ(frame
.GetFormat(), PP_VIDEOFRAME_FORMAT_YV12
);
212 ASSERT_TRUE(frame
.GetSize(&size
));
213 ASSERT_EQ(size
.width(), sizes
[i
].expect_width
);
214 ASSERT_EQ(size
.height(), sizes
[i
].expect_height
);
216 ASSERT_GT(frame
.GetDataBufferSize(), 0U);
217 ASSERT_TRUE(frame
.GetDataBuffer() != NULL
);
219 video_track_
.RecycleFrame(frame
);
224 video_track_
.Close();
225 ASSERT_TRUE(video_track_
.HasEnded());
226 video_track_
= pp::MediaStreamVideoTrack();