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/test/fake_single_thread_task_runner.h"
14 #include "media/cast/test/fake_video_encode_accelerator.h"
15 #include "media/cast/test/utility/video_utility.h"
16 #include "media/cast/video_sender/external_video_encoder.h"
17 #include "testing/gmock/include/gmock/gmock.h"
26 void CreateVideoEncodeAccelerator(
27 const scoped_refptr
<base::SingleThreadTaskRunner
>& task_runner
,
28 scoped_ptr
<VideoEncodeAccelerator
> fake_vea
,
29 const ReceiveVideoEncodeAcceleratorCallback
& callback
) {
30 callback
.Run(task_runner
, fake_vea
.Pass());
33 void CreateSharedMemory(
34 size_t size
, const ReceiveVideoEncodeMemoryCallback
& callback
) {
35 scoped_ptr
<base::SharedMemory
> shm(new base::SharedMemory());
36 if (!shm
->CreateAndMapAnonymous(size
)) {
40 callback
.Run(shm
.Pass());
43 class TestVideoEncoderCallback
44 : public base::RefCountedThreadSafe
<TestVideoEncoderCallback
> {
46 TestVideoEncoderCallback() {}
48 void SetExpectedResult(bool expected_key_frame
,
49 uint8 expected_frame_id
,
50 uint8 expected_last_referenced_frame_id
,
51 const base::TimeTicks
& expected_capture_time
) {
52 expected_key_frame_
= expected_key_frame
;
53 expected_frame_id_
= expected_frame_id
;
54 expected_last_referenced_frame_id_
= expected_last_referenced_frame_id
;
55 expected_capture_time_
= expected_capture_time
;
58 void DeliverEncodedVideoFrame(
59 scoped_ptr
<transport::EncodedVideoFrame
> encoded_frame
,
60 const base::TimeTicks
& capture_time
) {
61 EXPECT_EQ(expected_key_frame_
, encoded_frame
->key_frame
);
62 EXPECT_EQ(expected_frame_id_
, encoded_frame
->frame_id
);
63 EXPECT_EQ(expected_last_referenced_frame_id_
,
64 encoded_frame
->last_referenced_frame_id
);
65 EXPECT_EQ(expected_capture_time_
, capture_time
);
69 virtual ~TestVideoEncoderCallback() {}
72 friend class base::RefCountedThreadSafe
<TestVideoEncoderCallback
>;
74 bool expected_key_frame_
;
75 uint8 expected_frame_id_
;
76 uint8 expected_last_referenced_frame_id_
;
77 base::TimeTicks expected_capture_time_
;
79 DISALLOW_COPY_AND_ASSIGN(TestVideoEncoderCallback
);
83 class ExternalVideoEncoderTest
: public ::testing::Test
{
85 ExternalVideoEncoderTest()
86 : test_video_encoder_callback_(new TestVideoEncoderCallback()) {
87 video_config_
.rtp_config
.ssrc
= 1;
88 video_config_
.incoming_feedback_ssrc
= 2;
89 video_config_
.rtp_config
.payload_type
= 127;
90 video_config_
.use_external_encoder
= true;
91 video_config_
.width
= 320;
92 video_config_
.height
= 240;
93 video_config_
.max_bitrate
= 5000000;
94 video_config_
.min_bitrate
= 1000000;
95 video_config_
.start_bitrate
= 2000000;
96 video_config_
.max_qp
= 56;
97 video_config_
.min_qp
= 0;
98 video_config_
.max_frame_rate
= 30;
99 video_config_
.max_number_of_video_buffers_used
= 3;
100 video_config_
.codec
= transport::kVp8
;
101 gfx::Size
size(video_config_
.width
, video_config_
.height
);
102 video_frame_
= media::VideoFrame::CreateFrame(
103 VideoFrame::I420
, size
, gfx::Rect(size
), size
, base::TimeDelta());
104 PopulateVideoFrame(video_frame_
, 123);
106 testing_clock_
= new base::SimpleTestTickClock();
107 task_runner_
= new test::FakeSingleThreadTaskRunner(testing_clock_
);
109 new CastEnvironment(scoped_ptr
<base::TickClock
>(testing_clock_
).Pass(),
114 fake_vea_
= new test::FakeVideoEncodeAccelerator(task_runner_
);
115 scoped_ptr
<VideoEncodeAccelerator
> fake_vea(fake_vea_
);
116 video_encoder_
.reset(
117 new ExternalVideoEncoder(cast_environment_
,
119 base::Bind(&CreateVideoEncodeAccelerator
,
121 base::Passed(&fake_vea
)),
122 base::Bind(&CreateSharedMemory
)));
125 virtual ~ExternalVideoEncoderTest() {}
127 base::SimpleTestTickClock
* testing_clock_
; // Owned by CastEnvironment.
128 test::FakeVideoEncodeAccelerator
* fake_vea_
; // Owned by video_encoder_.
129 scoped_refptr
<TestVideoEncoderCallback
> test_video_encoder_callback_
;
130 VideoSenderConfig video_config_
;
131 scoped_refptr
<test::FakeSingleThreadTaskRunner
> task_runner_
;
132 scoped_ptr
<VideoEncoder
> video_encoder_
;
133 scoped_refptr
<media::VideoFrame
> video_frame_
;
134 scoped_refptr
<CastEnvironment
> cast_environment_
;
136 DISALLOW_COPY_AND_ASSIGN(ExternalVideoEncoderTest
);
139 TEST_F(ExternalVideoEncoderTest
, EncodePattern30fpsRunningOutOfAck
) {
140 task_runner_
->RunTasks(); // Run the initializer on the correct thread.
142 VideoEncoder::FrameEncodedCallback frame_encoded_callback
=
143 base::Bind(&TestVideoEncoderCallback::DeliverEncodedVideoFrame
,
144 test_video_encoder_callback_
.get());
146 base::TimeTicks capture_time
;
147 capture_time
+= base::TimeDelta::FromMilliseconds(33);
148 test_video_encoder_callback_
->SetExpectedResult(true, 0, 0, capture_time
);
149 EXPECT_TRUE(video_encoder_
->EncodeVideoFrame(
150 video_frame_
, capture_time
, frame_encoded_callback
));
151 task_runner_
->RunTasks();
153 for (int i
= 0; i
< 6; ++i
) {
154 capture_time
+= base::TimeDelta::FromMilliseconds(33);
155 test_video_encoder_callback_
->SetExpectedResult(
156 false, i
+ 1, i
, capture_time
);
157 EXPECT_TRUE(video_encoder_
->EncodeVideoFrame(
158 video_frame_
, capture_time
, frame_encoded_callback
));
159 task_runner_
->RunTasks();
161 // We need to run the task to cleanup the GPU instance.
162 video_encoder_
.reset(NULL
);
163 task_runner_
->RunTasks();
166 TEST_F(ExternalVideoEncoderTest
, SkipNextFrame
) {
167 task_runner_
->RunTasks(); // Run the initializer on the correct thread.
169 VideoEncoder::FrameEncodedCallback frame_encoded_callback
=
170 base::Bind(&TestVideoEncoderCallback::DeliverEncodedVideoFrame
,
171 test_video_encoder_callback_
.get());
173 base::TimeTicks capture_time
;
174 capture_time
+= base::TimeDelta::FromMilliseconds(33);
175 test_video_encoder_callback_
->SetExpectedResult(true, 0, 0, capture_time
);
176 EXPECT_TRUE(video_encoder_
->EncodeVideoFrame(
177 video_frame_
, capture_time
, frame_encoded_callback
));
178 task_runner_
->RunTasks();
180 video_encoder_
->SkipNextFrame(true);
181 for (int i
= 0; i
< 2; ++i
) {
182 capture_time
+= base::TimeDelta::FromMilliseconds(33);
183 EXPECT_FALSE(video_encoder_
->EncodeVideoFrame(
184 video_frame_
, capture_time
, frame_encoded_callback
));
185 task_runner_
->RunTasks();
188 video_encoder_
->SkipNextFrame(false);
189 for (int i
= 0; i
< 2; ++i
) {
190 capture_time
+= base::TimeDelta::FromMilliseconds(33);
191 test_video_encoder_callback_
->SetExpectedResult(
192 false, i
+ 1, i
, capture_time
);
193 EXPECT_TRUE(video_encoder_
->EncodeVideoFrame(
194 video_frame_
, capture_time
, frame_encoded_callback
));
195 task_runner_
->RunTasks();
197 // We need to run the task to cleanup the GPU instance.
198 video_encoder_
.reset(NULL
);
199 task_runner_
->RunTasks();
202 TEST_F(ExternalVideoEncoderTest
, StreamHeader
) {
203 task_runner_
->RunTasks(); // Run the initializer on the correct thread.
205 VideoEncoder::FrameEncodedCallback frame_encoded_callback
=
206 base::Bind(&TestVideoEncoderCallback::DeliverEncodedVideoFrame
,
207 test_video_encoder_callback_
.get());
209 // Force the FakeVideoEncodeAccelerator to return a dummy non-key frame first.
210 fake_vea_
->SendDummyFrameForTesting(false);
212 // Verify the first returned bitstream buffer is still a key frame.
213 base::TimeTicks capture_time
;
214 capture_time
+= base::TimeDelta::FromMilliseconds(33);
215 test_video_encoder_callback_
->SetExpectedResult(true, 0, 0, capture_time
);
216 EXPECT_TRUE(video_encoder_
->EncodeVideoFrame(
217 video_frame_
, capture_time
, frame_encoded_callback
));
218 task_runner_
->RunTasks();
220 // We need to run the task to cleanup the GPU instance.
221 video_encoder_
.reset(NULL
);
222 task_runner_
->RunTasks();