1 // Copyright 2015 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.
4
5 #include <stddef.h>
6 #include <stdint.h>
7
8 #include <memory>
9 #include <string>
10
11 #include "base/files/file_util.h"
12 #include "base/logging.h"
13 #include "base/memory/ptr_util.h"
14 #include "media/base/android/media_codec_bridge_impl.h"
15 #include "media/base/android/media_codec_util.h"
16 #include "media/base/decoder_buffer.h"
17 #include "media/base/media_util.h"
18 #include "media/base/test_data_util.h"
19 #include "media/base/video_frame.h"
20 #include "media/video/h264_parser.h"
21 #include "testing/gmock/include/gmock/gmock.h"
22 #include "third_party/libyuv/include/libyuv/convert_from.h"
23
24 using testing::IsNull;
25 using testing::NotNull;
26
27 namespace {
28
29 // The first frame of
30 // http://www.html5rocks.com/en/tutorials/audio/quick/test.mp3
31 unsigned char test_mp3[] = {
32 0xff, 0xfb, 0xd2, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0x05, 0x00,
33 0x00, 0x00, 0x00, 0x00, 0x0d, 0x20, 0x00, 0x00, 0x00, 0x2a, 0x7e, 0x40,
34 0xc0, 0x19, 0x4a, 0x80, 0x0d, 0x60, 0x48, 0x1b, 0x40, 0xf7, 0xbd, 0xb9,
35 0xd9, 0x40, 0x6f, 0x82, 0x01, 0x8b, 0x17, 0xa0, 0x80, 0xc5, 0x01, 0xad,
36 0x9a, 0xd3, 0x00, 0x12, 0xc0, 0x72, 0x93, 0x67, 0xd0, 0x03, 0x6f, 0xa4,
37 0xc0, 0xc3, 0x23, 0xee, 0x9b, 0xc0, 0xcc, 0x02, 0xa0, 0xa1, 0x30, 0x0c,
38 0x52, 0x2d, 0xfd, 0x6e, 0x08, 0x83, 0x60, 0x40, 0x46, 0x06, 0x4b, 0x20,
39 0x82, 0x82, 0x7f, 0xd4, 0x81, 0xe7, 0x00, 0x64, 0x20, 0x18, 0xec, 0xc2,
40 0x06, 0x57, 0x0f, 0x81, 0x93, 0x0b, 0x00, 0x66, 0xe3, 0xb7, 0xe8, 0x32,
41 0x6e, 0xf0, 0x32, 0xb0, 0x58, 0x0c, 0x7c, 0x3a, 0x03, 0x22, 0x14, 0x80,
42 0xc9, 0x01, 0x80, 0x30, 0x20, 0x14, 0x0c, 0x96, 0x73, 0xfe, 0x9f, 0x6c,
43 0x0c, 0xd2, 0x25, 0x0f, 0xdc, 0x0c, 0x32, 0x43, 0x03, 0x27, 0x87, 0xc0,
44 0xc2, 0xc0, 0x20, 0xfc, 0x42, 0xc5, 0xff, 0xff, 0xd4, 0x80, 0x01, 0x01,
45 0x80, 0xc3, 0x81, 0x01, 0x95, 0x03, 0x28, 0x82, 0xc0, 0xc3, 0x01, 0xa1,
46 0x06, 0x81, 0x87, 0xc2, 0x40, 0x64, 0xc1, 0xf0, 0x12, 0x02, 0xff, 0xf6,
47 0x5b, 0x9f, 0x44, 0xdc, 0xdd, 0x0b, 0x38, 0x59, 0xe0, 0x31, 0x71, 0x60,
48 0x0c, 0xb4, 0x22, 0x03, 0x3b, 0x96, 0x40, 0xc8, 0x63, 0x90, 0x0a, 0x23,
49 0x81, 0x9e, 0x4c, 0x20, 0x65, 0xb3, 0x18, 0x19, 0x6c, 0x42, 0x06, 0x36,
50 0x1d, 0x01, 0x90, 0x87, 0xdf, 0xff, 0xd0, 0x65, 0xa6, 0xea, 0x66, 0xfd,
51 0x40, 0x0c, 0x48, 0x03, 0x1a, 0x09, 0x01, 0x21, 0x98, 0x19, 0x2c, 0x36,
52 0x06, 0x43, 0x21, 0x81, 0x92, 0xca, 0x60, 0x64, 0x70, 0xb8, 0x19, 0x20,
53 0x6c, 0x02, 0x83, 0x80, 0xcb, 0x60, 0x65, 0x32, 0x28, 0x18, 0x64, 0x24,
54 0x06, 0x3a, 0x0c, 0x00, 0xe1, 0x00, 0x18, 0xd0, 0x35, 0xff, 0xff, 0xff,
55 0xe8, 0x32, 0xef, 0xb2, 0x90, 0x65, 0xbb, 0xdd, 0x94, 0x82, 0x0b, 0x4c,
56 0xfa, 0x25, 0xf3, 0x74, 0x13, 0x0f, 0xf8, 0x19, 0x28, 0x84, 0x06, 0x36,
57 0x11, 0x01, 0x20, 0x80, 0x18, 0xb4, 0x52, 0x0e, 0x15, 0x00, 0x30, 0x50,
58 0x0c, 0x84, 0x32, 0x03, 0x11, 0x04, 0x03, 0x48, 0x04, 0x00, 0x00, 0x31,
59 0x21, 0x00, 0x0c, 0x84, 0x18, 0x03, 0x07, 0x85, 0x40, 0xc6, 0xa5, 0x70,
60 0x32, 0xb8, 0x7c, 0x0c, 0x54, 0x04, 0x00, 0xd0, 0x08, 0x59, 0x58, 0x18,
61 0x20, 0x14, 0x06, 0x30, 0x30, 0x01, 0x9b, 0x86, 0x00, 0x6b, 0x54, 0xa8,
62 0x19, 0x8c, 0x2a, 0x06, 0x16, 0x09, 0x01, 0xa0, 0xd0, 0xa0, 0x69, 0x74,
63 0xb8, 0x19, 0xc4, 0x4a, 0xa3, 0xda, 0x9d, 0x1e, 0x4f, 0x05, 0xc0, 0x5b,
64 0x0b, 0x03, 0xc2, 0x76, 0xa3, 0x4f, 0xb9, 0x16, 0xc2, 0x70, 0x41, 0x07,
65 0xa0, 0x84, 0x16, 0x38, 0x4a, 0xc8, 0xaf, 0xee, 0x7f, 0x93, 0xb5, 0x5c,
66 0x39, 0x1e, 0x29, 0xd9, 0x8c, 0x80, 0xb5, 0x80, 0xe6, 0x85, 0xb2, 0x99,
67 0x68, 0x85, 0x46, 0x91, 0x60, 0xdb, 0x06, 0xfa, 0x38, 0x7a, 0xc7, 0xac,
68 0x85, 0xa8, 0xd3, 0xe6, 0x99, 0x3b, 0x66, 0x43, 0x23, 0x1f, 0x84, 0xe1,
69 0x65, 0x5e, 0xbc, 0x84, 0x18, 0x62, 0xe6, 0x42, 0x0b, 0x82, 0xe4, 0xd3,
70 0x42, 0xd2, 0x05, 0x81, 0x4e, 0xe4, 0x9f, 0x8c, 0xc8, 0x7f, 0xa3, 0xe0,
71 0x8d, 0xf1, 0x0f, 0x38, 0xe5, 0x3f, 0xc4, 0x2c, 0x24, 0x65, 0x8d, 0xb9,
72 0x58, 0xac, 0x39, 0x0e, 0x37, 0x99, 0x2e, 0x85, 0xe0, 0xb7, 0x98, 0x41,
73 0x20, 0x38, 0x1b, 0x95, 0x07, 0xfa, 0xa8, 0x9c, 0x21, 0x0f, 0x13, 0x8c,
74 0xa5, 0xc1, 0x76, 0xae, 0x0b, 0xc1, 0x30, 0x27, 0x08, 0xc1, 0xf6, 0x4d,
75 0xce, 0xb4, 0x41, 0x38, 0x1e, 0x82, 0x10, 0x74, 0x45, 0x91, 0x90, 0xff,
76 0x41, 0x8b, 0x62, 0x1a, 0x71, 0xb6, 0x45, 0x63, 0x8c, 0xce, 0xb8, 0x54,
77 0x1b, 0xe8, 0x5d, 0x9e, 0x35, 0x9d, 0x6c, 0xac, 0xe8, 0x83, 0xa1, 0xe9,
78 0x3f, 0x13, 0x74, 0x11, 0x04, 0x10, 0xf1, 0x37, 0x38, 0xc6, 0x00, 0x60,
79 0x27, 0x48, 0x38, 0x85, 0x92, 0x76, 0xb7, 0xf3, 0xa7, 0x1c, 0x4b, 0xf9,
80 0x3b, 0x5a, 0x88, 0xac, 0x60, 0x1b, 0x85, 0x81, 0x16, 0xab, 0x44, 0x17,
81 0x08, 0x2e, 0x0f, 0xd4, 0xe2, 0xde, 0x49, 0xc9, 0xe1, 0xc0, 0xc0, 0xa0,
82 0x7e, 0x73, 0xa1, 0x67, 0xf8, 0xf5, 0x9f, 0xc4, 0x21, 0x50, 0x4f, 0x05,
83 0x2c, 0xfc, 0x5c, 0xaa, 0x85, 0xb0, 0xfa, 0x67, 0x80, 0x7e, 0x0f, 0xfd,
84 0x92, 0x30, 0xd5, 0xa0, 0xd4, 0x05, 0xdd, 0x06, 0x68, 0x1d, 0x6e, 0x4e,
85 0x8b, 0x79, 0xd6, 0xfc, 0xff, 0x2e, 0x6e, 0x7c, 0xba, 0x03, 0x90, 0xd4,
86 0x25, 0x65, 0x8e, 0xe7, 0x3a, 0xd1, 0xd6, 0xdc, 0xf0, 0xbe, 0x12, 0xc4,
87 0x31, 0x08, 0x16, 0x70, 0x31, 0x85, 0x61, 0x38, 0x27, 0x0a, 0x91, 0x5f,
88 0x03, 0x38, 0xeb, 0x37, 0x13, 0x48, 0x41, 0xbe, 0x7f, 0x04, 0x70, 0x62,
89 0x2b, 0x15, 0x91, 0x67, 0x63, 0x4f, 0xad, 0xa7, 0x1d, 0x3f, 0x44, 0x17,
90 0x02, 0x08, 0x0d, 0xf2, 0xfc, 0x03, 0xa0, 0x74, 0x21, 0x8b, 0x07, 0x3a,
91 0x8d, 0x0f, 0x54, 0x58, 0x94, 0x12, 0xc5, 0x62, 0x18, 0xb9, 0x42, 0xf0,
92 0x6c, 0x73, 0xa0, 0x92, 0xad, 0x27, 0x1c, 0x20, 0x0f, 0xc1, 0xca, 0x44,
93 0x87, 0x47, 0xc5, 0x43, 0x23, 0x01, 0xda, 0x23, 0xe2, 0x89, 0x38, 0x9f,
94 0x1f, 0x8d, 0x8c, 0xc6, 0x95, 0xa3, 0x34, 0x21, 0x21, 0x2d, 0x49, 0xea,
95 0x4b, 0x05, 0x85, 0xf5, 0x58, 0x25, 0x13, 0xcd, 0x51, 0x19, 0x1a, 0x88,
96 0xa6, 0x83, 0xd6, 0xd0, 0xbc, 0x25, 0x19, 0x1c, 0x92, 0x12, 0x44, 0x5d,
97 0x1c, 0x04, 0xf1, 0x99, 0xdf, 0x92, 0x8e, 0x09, 0x85, 0xf3, 0x88, 0x82,
98 0x4c, 0x22, 0x17, 0xc5, 0x25, 0x23, 0xed, 0x78, 0xf5, 0x41, 0xd1, 0xe9,
99 0x8a, 0xb3, 0x52, 0xd1, 0x3d, 0x79, 0x81, 0x4d, 0x31, 0x24, 0xf9, 0x38,
100 0x96, 0xbc, 0xf4, 0x8c, 0x25, 0xe9, 0xf2, 0x73, 0x94, 0x85, 0xc2, 0x61,
101 0x6a, 0x34, 0x68, 0x65, 0x78, 0x87, 0xa6, 0x4f};
102 static const size_t kDecodedAudioLengthInBytes = 9216u;
103
104 } // namespace
105
106 namespace media {
107
108 #define SKIP_TEST_IF_MEDIA_CODEC_IS_NOT_AVAILABLE() \
109 do { \
110 if (!MediaCodecUtil::IsMediaCodecAvailable()) { \
111 VLOG(0) << "Could not run test - not supported on device."; \
112 return; \
113 } \
114 } while (0)
115
116 #define SKIP_TEST_IF_HW_H264_IS_NOT_AVAILABLE() \
117 do { \
118 if (!MediaCodecUtil::IsH264EncoderAvailable()) { \
119 VLOG(0) << "Could not run test - h264 not supported on device."; \
120 return; \
121 } \
122 } while (0)
123
124 enum PixelFormat {
125 // Subset of MediaCodecInfo.CodecCapabilities.
126 COLOR_FORMAT_YUV420_PLANAR = 19,
127 COLOR_FORMAT_YUV420_SEMIPLANAR = 21,
128 };
129
130 static const int kPresentationTimeBase = 100;
131 static const int kMaxInputPts = kPresentationTimeBase + 2;
132
InfiniteTimeOut()133 static inline const base::TimeDelta InfiniteTimeOut() {
134 return base::TimeDelta::FromMicroseconds(-1);
135 }
136
DecodeMediaFrame(MediaCodecBridge * media_codec,const uint8_t * data,size_t data_size,const base::TimeDelta input_presentation_timestamp,const base::TimeDelta initial_timestamp_lower_bound)137 void DecodeMediaFrame(MediaCodecBridge* media_codec,
138 const uint8_t* data,
139 size_t data_size,
140 const base::TimeDelta input_presentation_timestamp,
141 const base::TimeDelta initial_timestamp_lower_bound) {
142 base::TimeDelta input_pts = input_presentation_timestamp;
143 base::TimeDelta timestamp = initial_timestamp_lower_bound;
144 base::TimeDelta new_timestamp;
145 for (int i = 0; i < 10; ++i) {
146 int input_buf_index = -1;
147 MediaCodecStatus status =
148 media_codec->DequeueInputBuffer(InfiniteTimeOut(), &input_buf_index);
149 ASSERT_EQ(MEDIA_CODEC_OK, status);
150
151 media_codec->QueueInputBuffer(input_buf_index, data, data_size,
152 input_presentation_timestamp);
153
154 size_t unused_offset = 0;
155 size_t size = 0;
156 bool eos = false;
157 int output_buf_index = -1;
158 status = media_codec->DequeueOutputBuffer(
159 InfiniteTimeOut(), &output_buf_index, &unused_offset, &size,
160 &new_timestamp, &eos, nullptr);
161
162 if (status == MEDIA_CODEC_OK && output_buf_index > 0) {
163 media_codec->ReleaseOutputBuffer(output_buf_index, false);
164 }
165 // Output time stamp should not be smaller than old timestamp.
166 ASSERT_TRUE(new_timestamp >= timestamp);
167 input_pts += base::TimeDelta::FromMicroseconds(33000);
168 timestamp = new_timestamp;
169 }
170 }
171
172 // Performs basic, codec-specific sanity checks on the encoded H264 frame:
173 // - as to key frames, correct sequences of H.264 NALUs (SPS before PPS and
174 // before slices).
175 // - as to non key frames, contain no SPS/PPS infront.
H264Validate(const uint8_t * frame,size_t size)176 void H264Validate(const uint8_t* frame, size_t size) {
177 H264Parser h264_parser;
178 h264_parser.SetStream(frame, static_cast<off_t>(size));
179 bool seen_sps = false;
180 bool seen_pps = false;
181
182 while (1) {
183 H264NALU nalu;
184 H264Parser::Result result;
185
186 result = h264_parser.AdvanceToNextNALU(&nalu);
187 if (result == H264Parser::kEOStream)
188 break;
189 ASSERT_THAT(result, H264Parser::kOk);
190
191 switch (nalu.nal_unit_type) {
192 case H264NALU::kIDRSlice: {
193 ASSERT_TRUE(seen_sps);
194 ASSERT_TRUE(seen_pps);
195 break;
196 }
197
198 case H264NALU::kNonIDRSlice: {
199 ASSERT_FALSE(seen_sps);
200 ASSERT_FALSE(seen_pps);
201 break;
202 }
203
204 case H264NALU::kSPS: {
205 int sps_id;
206 ASSERT_EQ(H264Parser::kOk, h264_parser.ParseSPS(&sps_id));
207 seen_sps = true;
208 break;
209 }
210
211 case H264NALU::kPPS: {
212 ASSERT_TRUE(seen_sps);
213 int pps_id;
214 ASSERT_EQ(H264Parser::kOk, h264_parser.ParsePPS(&pps_id));
215 seen_pps = true;
216 break;
217 }
218
219 default:
220 break;
221 }
222 }
223 }
224
EncodeMediaFrame(MediaCodecBridge * media_codec,const uint8_t * src_data,const size_t src_size,const int width,const int height,const base::TimeDelta input_timestamp)225 void EncodeMediaFrame(MediaCodecBridge* media_codec,
226 const uint8_t* src_data,
227 const size_t src_size,
228 const int width,
229 const int height,
230 const base::TimeDelta input_timestamp) {
231 int input_buf_index = -1;
232 MediaCodecStatus status =
233 media_codec->DequeueInputBuffer(InfiniteTimeOut(), &input_buf_index);
234 ASSERT_EQ(MEDIA_CODEC_OK, status);
235
236 uint8_t* buffer = nullptr;
237 size_t capacity = 0;
238 status = media_codec->GetInputBuffer(input_buf_index, &buffer, &capacity);
239 ASSERT_EQ(MEDIA_CODEC_OK, status);
240
241 // Convert to NV12 because H264 encoder is created with color format
242 // COLOR_FormatYUV420SemiPlanar, both in main code path and unittest here.
243 bool converted =
244 !libyuv::I420ToNV12(src_data, width, src_data + width * height, width / 2,
245 src_data + width * height * 5 / 4, width / 2, buffer,
246 width, buffer + width * height, width, width, height);
247 ASSERT_TRUE(converted == true);
248
249 status = media_codec->QueueInputBuffer(input_buf_index, nullptr, src_size,
250 input_timestamp);
251 ASSERT_EQ(MEDIA_CODEC_OK, status);
252
253 int32_t buf_index = -1;
254 size_t offset = 0;
255 size_t output_size;
256 bool key_frame = false;
257
258 do {
259 status = media_codec->DequeueOutputBuffer(InfiniteTimeOut(), &buf_index,
260 &offset, &output_size, nullptr,
261 nullptr, &key_frame);
262 EXPECT_NE(status, MEDIA_CODEC_ERROR);
263 } while (buf_index < 0);
264 ASSERT_TRUE(status == MEDIA_CODEC_OK && buf_index >= 0);
265
266 std::unique_ptr<uint8_t[]> output_data =
267 std::make_unique<uint8_t[]>(output_size);
268 status = media_codec->CopyFromOutputBuffer(buf_index, offset,
269 output_data.get(), output_size);
270 ASSERT_EQ(MEDIA_CODEC_OK, status);
271
272 H264Validate(output_data.get(), output_size);
273
274 media_codec->ReleaseOutputBuffer(buf_index, false);
275 }
276
NewAudioConfig(AudioCodec codec,std::vector<uint8_t> extra_data=std::vector<uint8_t> (),base::TimeDelta seek_preroll=base::TimeDelta (),int64_t codec_delay=0)277 AudioDecoderConfig NewAudioConfig(
278 AudioCodec codec,
279 std::vector<uint8_t> extra_data = std::vector<uint8_t>(),
280 base::TimeDelta seek_preroll = base::TimeDelta(),
281 int64_t codec_delay = 0) {
282 AudioDecoderConfig config;
283 config.Initialize(codec, kSampleFormatPlanarF32, CHANNEL_LAYOUT_STEREO, 44100,
284 extra_data, EncryptionScheme::kUnencrypted, seek_preroll,
285 codec_delay);
286 return config;
287 }
288
TEST(MediaCodecBridgeTest,CreateH264Decoder)289 TEST(MediaCodecBridgeTest, CreateH264Decoder) {
290 SKIP_TEST_IF_MEDIA_CODEC_IS_NOT_AVAILABLE();
291
292 VideoCodecConfig config;
293 config.codec = kCodecH264;
294 config.codec_type = CodecType::kAny;
295 config.initial_expected_coded_size = gfx::Size(640, 480);
296
297 MediaCodecBridgeImpl::CreateVideoDecoder(config);
298 }
299
TEST(MediaCodecBridgeTest,DoNormal)300 TEST(MediaCodecBridgeTest, DoNormal) {
301 SKIP_TEST_IF_MEDIA_CODEC_IS_NOT_AVAILABLE();
302
303 std::unique_ptr<media::MediaCodecBridge> media_codec =
304 MediaCodecBridgeImpl::CreateAudioDecoder(NewAudioConfig(kCodecMP3),
305 nullptr);
306 ASSERT_THAT(media_codec, NotNull());
307
308 int input_buf_index = -1;
309 MediaCodecStatus status =
310 media_codec->DequeueInputBuffer(InfiniteTimeOut(), &input_buf_index);
311 ASSERT_EQ(MEDIA_CODEC_OK, status);
312 ASSERT_GE(input_buf_index, 0);
313
314 int64_t input_pts = kPresentationTimeBase;
315 media_codec->QueueInputBuffer(input_buf_index, test_mp3, sizeof(test_mp3),
316 base::TimeDelta::FromMicroseconds(++input_pts));
317
318 status = media_codec->DequeueInputBuffer(InfiniteTimeOut(), &input_buf_index);
319 media_codec->QueueInputBuffer(input_buf_index, test_mp3, sizeof(test_mp3),
320 base::TimeDelta::FromMicroseconds(++input_pts));
321
322 status = media_codec->DequeueInputBuffer(InfiniteTimeOut(), &input_buf_index);
323 media_codec->QueueEOS(input_buf_index);
324
325 input_pts = kPresentationTimeBase;
326 bool eos = false;
327 size_t total_size = 0;
328 while (!eos) {
329 size_t unused_offset = 0;
330 size_t size = 0;
331 base::TimeDelta timestamp;
332 int output_buf_index = -1;
333 status = media_codec->DequeueOutputBuffer(InfiniteTimeOut(),
334 &output_buf_index, &unused_offset,
335 &size, ×tamp, &eos, nullptr);
336 switch (status) {
337 case MEDIA_CODEC_TRY_AGAIN_LATER:
338 FAIL();
339 return;
340
341 case MEDIA_CODEC_OUTPUT_FORMAT_CHANGED:
342 continue;
343
344 case MEDIA_CODEC_OUTPUT_BUFFERS_CHANGED:
345 continue;
346
347 default:
348 break;
349 }
350 ASSERT_GE(output_buf_index, 0);
351 EXPECT_LE(1u, size);
352 total_size += size;
353 }
354 EXPECT_EQ(kDecodedAudioLengthInBytes, total_size);
355 ASSERT_LE(input_pts, kMaxInputPts);
356 }
357
TEST(MediaCodecBridgeTest,InvalidVorbisHeader)358 TEST(MediaCodecBridgeTest, InvalidVorbisHeader) {
359 SKIP_TEST_IF_MEDIA_CODEC_IS_NOT_AVAILABLE();
360
361 // The first byte of the header is not 0x02.
362 std::vector<uint8_t> invalid_first_byte = {{0x00, 0xff, 0xff, 0xff, 0xff}};
363 ASSERT_THAT(MediaCodecBridgeImpl::CreateAudioDecoder(
364 NewAudioConfig(kCodecVorbis, invalid_first_byte), nullptr),
365 IsNull());
366
367 // Size of the header is too large.
368 size_t large_size = 8 * 1024 * 1024 + 2;
369 std::vector<uint8_t> large_header(large_size, 0xff);
370 large_header.front() = 0x02;
371 large_header.back() = 0xfe;
372 ASSERT_THAT(MediaCodecBridgeImpl::CreateAudioDecoder(
373 NewAudioConfig(kCodecVorbis, large_header), nullptr),
374 IsNull());
375 }
376
TEST(MediaCodecBridgeTest,InvalidOpusHeader)377 TEST(MediaCodecBridgeTest, InvalidOpusHeader) {
378 SKIP_TEST_IF_MEDIA_CODEC_IS_NOT_AVAILABLE();
379
380 std::vector<uint8_t> dummy_extra_data = {{0, 0}};
381
382 // Codec Delay is < 0.
383 ASSERT_THAT(
384 MediaCodecBridgeImpl::CreateAudioDecoder(
385 NewAudioConfig(kCodecOpus, dummy_extra_data, base::TimeDelta(), -1),
386 nullptr),
387 IsNull());
388
389 // Seek Preroll is < 0.
390 ASSERT_THAT(MediaCodecBridgeImpl::CreateAudioDecoder(
391 NewAudioConfig(kCodecOpus, dummy_extra_data,
392 base::TimeDelta::FromMicroseconds(-1)),
393 nullptr),
394 IsNull());
395 }
396
TEST(MediaCodecBridgeTest,PresentationTimestampsDoNotDecrease)397 TEST(MediaCodecBridgeTest, PresentationTimestampsDoNotDecrease) {
398 if (!MediaCodecUtil::IsVp8DecoderAvailable()) {
399 VLOG(0) << "Could not run test - VP8 not supported on device.";
400 return;
401 }
402
403 VideoCodecConfig config;
404 config.codec = kCodecVP8;
405 config.codec_type = CodecType::kAny;
406 config.initial_expected_coded_size = gfx::Size(320, 240);
407
408 auto media_codec = MediaCodecBridgeImpl::CreateVideoDecoder(config);
409 ASSERT_THAT(media_codec, NotNull());
410 scoped_refptr<DecoderBuffer> buffer = ReadTestDataFile("vp8-I-frame-320x240");
411 DecodeMediaFrame(media_codec.get(), buffer->data(), buffer->data_size(),
412 base::TimeDelta(), base::TimeDelta());
413
414 // Simulate a seek to 10 seconds, and each chunk has 2 I-frames.
415 std::vector<uint8_t> chunk(buffer->data(),
416 buffer->data() + buffer->data_size());
417 chunk.insert(chunk.end(), buffer->data(),
418 buffer->data() + buffer->data_size());
419 media_codec->Flush();
420 DecodeMediaFrame(media_codec.get(), &chunk[0], chunk.size(),
421 base::TimeDelta::FromMicroseconds(10000000),
422 base::TimeDelta::FromMicroseconds(9900000));
423
424 // Simulate a seek to 5 seconds.
425 media_codec->Flush();
426 DecodeMediaFrame(media_codec.get(), &chunk[0], chunk.size(),
427 base::TimeDelta::FromMicroseconds(5000000),
428 base::TimeDelta::FromMicroseconds(4900000));
429 }
430
TEST(MediaCodecBridgeTest,CreateUnsupportedCodec)431 TEST(MediaCodecBridgeTest, CreateUnsupportedCodec) {
432 EXPECT_THAT(MediaCodecBridgeImpl::CreateAudioDecoder(
433 NewAudioConfig(kUnknownAudioCodec), nullptr),
434 IsNull());
435
436 VideoCodecConfig config;
437 config.codec = kUnknownVideoCodec;
438 config.codec_type = CodecType::kAny;
439 config.initial_expected_coded_size = gfx::Size(320, 240);
440 EXPECT_THAT(MediaCodecBridgeImpl::CreateVideoDecoder(config), IsNull());
441 }
442
443 // Test MediaCodec HW H264 encoding and validate the format of encoded frames.
TEST(MediaCodecBridgeTest,H264VideoEncodeAndValidate)444 TEST(MediaCodecBridgeTest, H264VideoEncodeAndValidate) {
445 SKIP_TEST_IF_HW_H264_IS_NOT_AVAILABLE();
446
447 const int width = 640;
448 const int height = 360;
449 const int bit_rate = 300000;
450 const int frame_rate = 30;
451 const int i_frame_interval = 20;
452 const std::set<int> supported_color_formats =
453 MediaCodecUtil::GetEncoderColorFormats("video/avc");
454
455 int color_format;
456 if (supported_color_formats.count(COLOR_FORMAT_YUV420_SEMIPLANAR) > 0) {
457 color_format = COLOR_FORMAT_YUV420_SEMIPLANAR;
458 } else if (supported_color_formats.count(COLOR_FORMAT_YUV420_PLANAR) > 0) {
459 color_format = COLOR_FORMAT_YUV420_PLANAR;
460 } else {
461 VLOG(0) << "Could not run test - YUV420_PLANAR and YUV420_SEMIPLANAR "
462 "unavailable for h264 encode.";
463 return;
464 }
465
466 std::unique_ptr<MediaCodecBridge> media_codec(
467 MediaCodecBridgeImpl::CreateVideoEncoder(
468 kCodecH264, gfx::Size(width, height), bit_rate, frame_rate,
469 i_frame_interval, color_format));
470 ASSERT_THAT(media_codec, NotNull());
471
472 const char kSrcFileName[] = "bali_640x360_P420.yuv";
473 base::FilePath src_file = GetTestDataFilePath(kSrcFileName);
474 int64_t src_file_size = 0;
475 ASSERT_TRUE(base::GetFileSize(src_file, &src_file_size));
476
477 const VideoPixelFormat kInputFormat = PIXEL_FORMAT_I420;
478 const int frame_size = static_cast<int>(
479 VideoFrame::AllocationSize(kInputFormat, gfx::Size(width, height)));
480 ASSERT_TRUE(frame_size > 0);
481 ASSERT_TRUE(src_file_size % frame_size == 0U);
482
483 const int num_frames = src_file_size / frame_size;
484 base::File src(src_file, base::File::FLAG_OPEN | base::File::FLAG_READ);
485 std::unique_ptr<uint8_t[]> frame_data =
486 std::make_unique<uint8_t[]>(frame_size);
487 ASSERT_THAT(
488 src.Read(0, reinterpret_cast<char*>(frame_data.get()), frame_size),
489 frame_size);
490
491 // A monotonically-growing value.
492 base::TimeDelta input_timestamp;
493
494 // Src_file contains 1 frames. Encode it 3 times.
495 for (int frame = 0; frame < num_frames && frame < 3; frame++) {
496 input_timestamp += base::TimeDelta::FromMicroseconds(
497 base::Time::kMicrosecondsPerSecond / frame_rate);
498 EncodeMediaFrame(media_codec.get(), frame_data.get(), frame_size, width,
499 height, input_timestamp);
500 }
501
502 // Reuest key frame and encode 3 more frames. The second key frame should
503 // also contain SPS/PPS NALUs.
504 media_codec->RequestKeyFrameSoon();
505 for (int frame = 0; frame < num_frames && frame < 3; frame++) {
506 input_timestamp += base::TimeDelta::FromMicroseconds(
507 base::Time::kMicrosecondsPerSecond / frame_rate);
508 EncodeMediaFrame(media_codec.get(), frame_data.get(), frame_size, width,
509 height, input_timestamp);
510 }
511 }
512
513 } // namespace media
514