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.
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "media/base/video_frame.h"
11 #include "media/cast/cast_defines.h"
12 #include "media/cast/cast_environment.h"
13 #include "media/cast/sender/video_encoder_impl.h"
14 #include "media/cast/test/fake_single_thread_task_runner.h"
15 #include "media/cast/test/utility/video_utility.h"
16 #include "testing/gmock/include/gmock/gmock.h"
24 class TestVideoEncoderCallback
25 : public base::RefCountedThreadSafe
<TestVideoEncoderCallback
> {
27 TestVideoEncoderCallback() {}
29 void SetExpectedResult(uint32 expected_frame_id
,
30 uint32 expected_last_referenced_frame_id
,
31 const base::TimeTicks
& expected_capture_time
) {
32 expected_frame_id_
= expected_frame_id
;
33 expected_last_referenced_frame_id_
= expected_last_referenced_frame_id
;
34 expected_capture_time_
= expected_capture_time
;
37 void DeliverEncodedVideoFrame(
38 scoped_ptr
<EncodedFrame
> encoded_frame
) {
39 if (expected_frame_id_
== expected_last_referenced_frame_id_
) {
40 EXPECT_EQ(EncodedFrame::KEY
, encoded_frame
->dependency
);
42 EXPECT_EQ(EncodedFrame::DEPENDENT
,
43 encoded_frame
->dependency
);
45 EXPECT_EQ(expected_frame_id_
, encoded_frame
->frame_id
);
46 EXPECT_EQ(expected_last_referenced_frame_id_
,
47 encoded_frame
->referenced_frame_id
);
48 EXPECT_EQ(expected_capture_time_
, encoded_frame
->reference_time
);
52 virtual ~TestVideoEncoderCallback() {}
55 friend class base::RefCountedThreadSafe
<TestVideoEncoderCallback
>;
57 uint32 expected_frame_id_
;
58 uint32 expected_last_referenced_frame_id_
;
59 base::TimeTicks expected_capture_time_
;
61 DISALLOW_COPY_AND_ASSIGN(TestVideoEncoderCallback
);
65 class VideoEncoderImplTest
: public ::testing::Test
{
67 VideoEncoderImplTest()
68 : test_video_encoder_callback_(new TestVideoEncoderCallback()) {
69 video_config_
.ssrc
= 1;
70 video_config_
.incoming_feedback_ssrc
= 2;
71 video_config_
.rtp_payload_type
= 127;
72 video_config_
.use_external_encoder
= false;
73 video_config_
.width
= 320;
74 video_config_
.height
= 240;
75 video_config_
.max_bitrate
= 5000000;
76 video_config_
.min_bitrate
= 1000000;
77 video_config_
.start_bitrate
= 2000000;
78 video_config_
.max_qp
= 56;
79 video_config_
.min_qp
= 0;
80 video_config_
.max_frame_rate
= 30;
81 video_config_
.max_number_of_video_buffers_used
= 3;
82 video_config_
.codec
= CODEC_VIDEO_VP8
;
83 gfx::Size
size(video_config_
.width
, video_config_
.height
);
84 video_frame_
= media::VideoFrame::CreateFrame(
85 VideoFrame::I420
, size
, gfx::Rect(size
), size
, base::TimeDelta());
86 PopulateVideoFrame(video_frame_
.get(), 123);
89 virtual ~VideoEncoderImplTest() {}
91 virtual void SetUp() OVERRIDE
{
92 testing_clock_
= new base::SimpleTestTickClock();
93 task_runner_
= new test::FakeSingleThreadTaskRunner(testing_clock_
);
95 new CastEnvironment(scoped_ptr
<base::TickClock
>(testing_clock_
).Pass(),
101 virtual void TearDown() OVERRIDE
{
102 video_encoder_
.reset();
103 task_runner_
->RunTasks();
106 void Configure(int max_unacked_frames
) {
107 video_encoder_
.reset(new VideoEncoderImpl(
108 cast_environment_
, video_config_
, max_unacked_frames
));
111 base::SimpleTestTickClock
* testing_clock_
; // Owned by CastEnvironment.
112 scoped_refptr
<TestVideoEncoderCallback
> test_video_encoder_callback_
;
113 VideoSenderConfig video_config_
;
114 scoped_refptr
<test::FakeSingleThreadTaskRunner
> task_runner_
;
115 scoped_ptr
<VideoEncoder
> video_encoder_
;
116 scoped_refptr
<media::VideoFrame
> video_frame_
;
118 scoped_refptr
<CastEnvironment
> cast_environment_
;
120 DISALLOW_COPY_AND_ASSIGN(VideoEncoderImplTest
);
123 TEST_F(VideoEncoderImplTest
, EncodePattern30fpsRunningOutOfAck
) {
126 VideoEncoder::FrameEncodedCallback frame_encoded_callback
=
127 base::Bind(&TestVideoEncoderCallback::DeliverEncodedVideoFrame
,
128 test_video_encoder_callback_
.get());
130 base::TimeTicks capture_time
;
131 capture_time
+= base::TimeDelta::FromMilliseconds(33);
132 test_video_encoder_callback_
->SetExpectedResult(0, 0, capture_time
);
133 EXPECT_TRUE(video_encoder_
->EncodeVideoFrame(
134 video_frame_
, capture_time
, frame_encoded_callback
));
135 task_runner_
->RunTasks();
137 capture_time
+= base::TimeDelta::FromMilliseconds(33);
138 video_encoder_
->LatestFrameIdToReference(0);
139 test_video_encoder_callback_
->SetExpectedResult(1, 0, capture_time
);
140 EXPECT_TRUE(video_encoder_
->EncodeVideoFrame(
141 video_frame_
, capture_time
, frame_encoded_callback
));
142 task_runner_
->RunTasks();
144 capture_time
+= base::TimeDelta::FromMilliseconds(33);
145 video_encoder_
->LatestFrameIdToReference(1);
146 test_video_encoder_callback_
->SetExpectedResult(2, 1, capture_time
);
147 EXPECT_TRUE(video_encoder_
->EncodeVideoFrame(
148 video_frame_
, capture_time
, frame_encoded_callback
));
149 task_runner_
->RunTasks();
151 video_encoder_
->LatestFrameIdToReference(2);
153 for (int i
= 3; i
< 6; ++i
) {
154 capture_time
+= base::TimeDelta::FromMilliseconds(33);
155 test_video_encoder_callback_
->SetExpectedResult(i
, 2, capture_time
);
156 EXPECT_TRUE(video_encoder_
->EncodeVideoFrame(
157 video_frame_
, capture_time
, frame_encoded_callback
));
158 task_runner_
->RunTasks();
162 // TODO(pwestin): Re-enabled after redesign the encoder to control number of
164 TEST_F(VideoEncoderImplTest
, DISABLED_EncodePattern60fpsRunningOutOfAck
) {
165 video_config_
.max_number_of_video_buffers_used
= 1;
168 base::TimeTicks capture_time
;
169 VideoEncoder::FrameEncodedCallback frame_encoded_callback
=
170 base::Bind(&TestVideoEncoderCallback::DeliverEncodedVideoFrame
,
171 test_video_encoder_callback_
.get());
173 capture_time
+= base::TimeDelta::FromMilliseconds(33);
174 test_video_encoder_callback_
->SetExpectedResult(0, 0, capture_time
);
175 EXPECT_TRUE(video_encoder_
->EncodeVideoFrame(
176 video_frame_
, capture_time
, frame_encoded_callback
));
177 task_runner_
->RunTasks();
179 video_encoder_
->LatestFrameIdToReference(0);
180 capture_time
+= base::TimeDelta::FromMilliseconds(33);
181 test_video_encoder_callback_
->SetExpectedResult(1, 0, capture_time
);
182 EXPECT_TRUE(video_encoder_
->EncodeVideoFrame(
183 video_frame_
, capture_time
, frame_encoded_callback
));
184 task_runner_
->RunTasks();
186 video_encoder_
->LatestFrameIdToReference(1);
187 capture_time
+= base::TimeDelta::FromMilliseconds(33);
188 test_video_encoder_callback_
->SetExpectedResult(2, 0, capture_time
);
189 EXPECT_TRUE(video_encoder_
->EncodeVideoFrame(
190 video_frame_
, capture_time
, frame_encoded_callback
));
191 task_runner_
->RunTasks();
193 video_encoder_
->LatestFrameIdToReference(2);
195 for (int i
= 3; i
< 9; ++i
) {
196 capture_time
+= base::TimeDelta::FromMilliseconds(33);
197 test_video_encoder_callback_
->SetExpectedResult(i
, 2, capture_time
);
198 EXPECT_TRUE(video_encoder_
->EncodeVideoFrame(
199 video_frame_
, capture_time
, frame_encoded_callback
));
200 task_runner_
->RunTasks();
204 // TODO(pwestin): Re-enabled after redesign the encoder to control number of
206 TEST_F(VideoEncoderImplTest
,
207 DISABLED_EncodePattern60fps200msDelayRunningOutOfAck
) {
210 base::TimeTicks capture_time
;
211 VideoEncoder::FrameEncodedCallback frame_encoded_callback
=
212 base::Bind(&TestVideoEncoderCallback::DeliverEncodedVideoFrame
,
213 test_video_encoder_callback_
.get());
215 capture_time
+= base::TimeDelta::FromMilliseconds(33);
216 test_video_encoder_callback_
->SetExpectedResult(0, 0, capture_time
);
217 EXPECT_TRUE(video_encoder_
->EncodeVideoFrame(
218 video_frame_
, capture_time
, frame_encoded_callback
));
219 task_runner_
->RunTasks();
221 video_encoder_
->LatestFrameIdToReference(0);
222 capture_time
+= base::TimeDelta::FromMilliseconds(33);
223 test_video_encoder_callback_
->SetExpectedResult(1, 0, capture_time
);
224 EXPECT_TRUE(video_encoder_
->EncodeVideoFrame(
225 video_frame_
, capture_time
, frame_encoded_callback
));
226 task_runner_
->RunTasks();
228 video_encoder_
->LatestFrameIdToReference(1);
229 capture_time
+= base::TimeDelta::FromMilliseconds(33);
230 test_video_encoder_callback_
->SetExpectedResult(2, 0, capture_time
);
231 EXPECT_TRUE(video_encoder_
->EncodeVideoFrame(
232 video_frame_
, capture_time
, frame_encoded_callback
));
233 task_runner_
->RunTasks();
235 video_encoder_
->LatestFrameIdToReference(2);
236 capture_time
+= base::TimeDelta::FromMilliseconds(33);
237 test_video_encoder_callback_
->SetExpectedResult(3, 0, capture_time
);
238 EXPECT_TRUE(video_encoder_
->EncodeVideoFrame(
239 video_frame_
, capture_time
, frame_encoded_callback
));
240 task_runner_
->RunTasks();
242 video_encoder_
->LatestFrameIdToReference(3);
243 capture_time
+= base::TimeDelta::FromMilliseconds(33);
244 test_video_encoder_callback_
->SetExpectedResult(4, 0, capture_time
);
245 EXPECT_TRUE(video_encoder_
->EncodeVideoFrame(
246 video_frame_
, capture_time
, frame_encoded_callback
));
247 task_runner_
->RunTasks();
249 video_encoder_
->LatestFrameIdToReference(4);
251 for (int i
= 5; i
< 17; ++i
) {
252 test_video_encoder_callback_
->SetExpectedResult(i
, 4, capture_time
);
253 EXPECT_TRUE(video_encoder_
->EncodeVideoFrame(
254 video_frame_
, capture_time
, frame_encoded_callback
));
255 task_runner_
->RunTasks();