1 // Copyright (c) 2013 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 <string> 6 7 #include "base/basictypes.h" 8 #include "base/logging.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "media/base/android/media_codec_bridge.h" 11 #include "media/base/decoder_buffer.h" 12 #include "media/base/test_data_util.h" 13 #include "testing/gmock/include/gmock/gmock.h" 14 15 namespace { 16 17 // The first frame of 18 // http://www.html5rocks.com/en/tutorials/audio/quick/test.mp3 19 unsigned char test_mp3[] = { 20 0xff, 0xfb, 0xd2, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0x05, 0x00, 21 0x00, 0x00, 0x00, 0x00, 0x0d, 0x20, 0x00, 0x00, 0x00, 0x2a, 0x7e, 0x40, 22 0xc0, 0x19, 0x4a, 0x80, 0x0d, 0x60, 0x48, 0x1b, 0x40, 0xf7, 0xbd, 0xb9, 23 0xd9, 0x40, 0x6f, 0x82, 0x01, 0x8b, 0x17, 0xa0, 0x80, 0xc5, 0x01, 0xad, 24 0x9a, 0xd3, 0x00, 0x12, 0xc0, 0x72, 0x93, 0x67, 0xd0, 0x03, 0x6f, 0xa4, 25 0xc0, 0xc3, 0x23, 0xee, 0x9b, 0xc0, 0xcc, 0x02, 0xa0, 0xa1, 0x30, 0x0c, 26 0x52, 0x2d, 0xfd, 0x6e, 0x08, 0x83, 0x60, 0x40, 0x46, 0x06, 0x4b, 0x20, 27 0x82, 0x82, 0x7f, 0xd4, 0x81, 0xe7, 0x00, 0x64, 0x20, 0x18, 0xec, 0xc2, 28 0x06, 0x57, 0x0f, 0x81, 0x93, 0x0b, 0x00, 0x66, 0xe3, 0xb7, 0xe8, 0x32, 29 0x6e, 0xf0, 0x32, 0xb0, 0x58, 0x0c, 0x7c, 0x3a, 0x03, 0x22, 0x14, 0x80, 30 0xc9, 0x01, 0x80, 0x30, 0x20, 0x14, 0x0c, 0x96, 0x73, 0xfe, 0x9f, 0x6c, 31 0x0c, 0xd2, 0x25, 0x0f, 0xdc, 0x0c, 0x32, 0x43, 0x03, 0x27, 0x87, 0xc0, 32 0xc2, 0xc0, 0x20, 0xfc, 0x42, 0xc5, 0xff, 0xff, 0xd4, 0x80, 0x01, 0x01, 33 0x80, 0xc3, 0x81, 0x01, 0x95, 0x03, 0x28, 0x82, 0xc0, 0xc3, 0x01, 0xa1, 34 0x06, 0x81, 0x87, 0xc2, 0x40, 0x64, 0xc1, 0xf0, 0x12, 0x02, 0xff, 0xf6, 35 0x5b, 0x9f, 0x44, 0xdc, 0xdd, 0x0b, 0x38, 0x59, 0xe0, 0x31, 0x71, 0x60, 36 0x0c, 0xb4, 0x22, 0x03, 0x3b, 0x96, 0x40, 0xc8, 0x63, 0x90, 0x0a, 0x23, 37 0x81, 0x9e, 0x4c, 0x20, 0x65, 0xb3, 0x18, 0x19, 0x6c, 0x42, 0x06, 0x36, 38 0x1d, 0x01, 0x90, 0x87, 0xdf, 0xff, 0xd0, 0x65, 0xa6, 0xea, 0x66, 0xfd, 39 0x40, 0x0c, 0x48, 0x03, 0x1a, 0x09, 0x01, 0x21, 0x98, 0x19, 0x2c, 0x36, 40 0x06, 0x43, 0x21, 0x81, 0x92, 0xca, 0x60, 0x64, 0x70, 0xb8, 0x19, 0x20, 41 0x6c, 0x02, 0x83, 0x80, 0xcb, 0x60, 0x65, 0x32, 0x28, 0x18, 0x64, 0x24, 42 0x06, 0x3a, 0x0c, 0x00, 0xe1, 0x00, 0x18, 0xd0, 0x35, 0xff, 0xff, 0xff, 43 0xe8, 0x32, 0xef, 0xb2, 0x90, 0x65, 0xbb, 0xdd, 0x94, 0x82, 0x0b, 0x4c, 44 0xfa, 0x25, 0xf3, 0x74, 0x13, 0x0f, 0xf8, 0x19, 0x28, 0x84, 0x06, 0x36, 45 0x11, 0x01, 0x20, 0x80, 0x18, 0xb4, 0x52, 0x0e, 0x15, 0x00, 0x30, 0x50, 46 0x0c, 0x84, 0x32, 0x03, 0x11, 0x04, 0x03, 0x48, 0x04, 0x00, 0x00, 0x31, 47 0x21, 0x00, 0x0c, 0x84, 0x18, 0x03, 0x07, 0x85, 0x40, 0xc6, 0xa5, 0x70, 48 0x32, 0xb8, 0x7c, 0x0c, 0x54, 0x04, 0x00, 0xd0, 0x08, 0x59, 0x58, 0x18, 49 0x20, 0x14, 0x06, 0x30, 0x30, 0x01, 0x9b, 0x86, 0x00, 0x6b, 0x54, 0xa8, 50 0x19, 0x8c, 0x2a, 0x06, 0x16, 0x09, 0x01, 0xa0, 0xd0, 0xa0, 0x69, 0x74, 51 0xb8, 0x19, 0xc4, 0x4a, 0xa3, 0xda, 0x9d, 0x1e, 0x4f, 0x05, 0xc0, 0x5b, 52 0x0b, 0x03, 0xc2, 0x76, 0xa3, 0x4f, 0xb9, 0x16, 0xc2, 0x70, 0x41, 0x07, 53 0xa0, 0x84, 0x16, 0x38, 0x4a, 0xc8, 0xaf, 0xee, 0x7f, 0x93, 0xb5, 0x5c, 54 0x39, 0x1e, 0x29, 0xd9, 0x8c, 0x80, 0xb5, 0x80, 0xe6, 0x85, 0xb2, 0x99, 55 0x68, 0x85, 0x46, 0x91, 0x60, 0xdb, 0x06, 0xfa, 0x38, 0x7a, 0xc7, 0xac, 56 0x85, 0xa8, 0xd3, 0xe6, 0x99, 0x3b, 0x66, 0x43, 0x23, 0x1f, 0x84, 0xe1, 57 0x65, 0x5e, 0xbc, 0x84, 0x18, 0x62, 0xe6, 0x42, 0x0b, 0x82, 0xe4, 0xd3, 58 0x42, 0xd2, 0x05, 0x81, 0x4e, 0xe4, 0x9f, 0x8c, 0xc8, 0x7f, 0xa3, 0xe0, 59 0x8d, 0xf1, 0x0f, 0x38, 0xe5, 0x3f, 0xc4, 0x2c, 0x24, 0x65, 0x8d, 0xb9, 60 0x58, 0xac, 0x39, 0x0e, 0x37, 0x99, 0x2e, 0x85, 0xe0, 0xb7, 0x98, 0x41, 61 0x20, 0x38, 0x1b, 0x95, 0x07, 0xfa, 0xa8, 0x9c, 0x21, 0x0f, 0x13, 0x8c, 62 0xa5, 0xc1, 0x76, 0xae, 0x0b, 0xc1, 0x30, 0x27, 0x08, 0xc1, 0xf6, 0x4d, 63 0xce, 0xb4, 0x41, 0x38, 0x1e, 0x82, 0x10, 0x74, 0x45, 0x91, 0x90, 0xff, 64 0x41, 0x8b, 0x62, 0x1a, 0x71, 0xb6, 0x45, 0x63, 0x8c, 0xce, 0xb8, 0x54, 65 0x1b, 0xe8, 0x5d, 0x9e, 0x35, 0x9d, 0x6c, 0xac, 0xe8, 0x83, 0xa1, 0xe9, 66 0x3f, 0x13, 0x74, 0x11, 0x04, 0x10, 0xf1, 0x37, 0x38, 0xc6, 0x00, 0x60, 67 0x27, 0x48, 0x38, 0x85, 0x92, 0x76, 0xb7, 0xf3, 0xa7, 0x1c, 0x4b, 0xf9, 68 0x3b, 0x5a, 0x88, 0xac, 0x60, 0x1b, 0x85, 0x81, 0x16, 0xab, 0x44, 0x17, 69 0x08, 0x2e, 0x0f, 0xd4, 0xe2, 0xde, 0x49, 0xc9, 0xe1, 0xc0, 0xc0, 0xa0, 70 0x7e, 0x73, 0xa1, 0x67, 0xf8, 0xf5, 0x9f, 0xc4, 0x21, 0x50, 0x4f, 0x05, 71 0x2c, 0xfc, 0x5c, 0xaa, 0x85, 0xb0, 0xfa, 0x67, 0x80, 0x7e, 0x0f, 0xfd, 72 0x92, 0x30, 0xd5, 0xa0, 0xd4, 0x05, 0xdd, 0x06, 0x68, 0x1d, 0x6e, 0x4e, 73 0x8b, 0x79, 0xd6, 0xfc, 0xff, 0x2e, 0x6e, 0x7c, 0xba, 0x03, 0x90, 0xd4, 74 0x25, 0x65, 0x8e, 0xe7, 0x3a, 0xd1, 0xd6, 0xdc, 0xf0, 0xbe, 0x12, 0xc4, 75 0x31, 0x08, 0x16, 0x70, 0x31, 0x85, 0x61, 0x38, 0x27, 0x0a, 0x91, 0x5f, 76 0x03, 0x38, 0xeb, 0x37, 0x13, 0x48, 0x41, 0xbe, 0x7f, 0x04, 0x70, 0x62, 77 0x2b, 0x15, 0x91, 0x67, 0x63, 0x4f, 0xad, 0xa7, 0x1d, 0x3f, 0x44, 0x17, 78 0x02, 0x08, 0x0d, 0xf2, 0xfc, 0x03, 0xa0, 0x74, 0x21, 0x8b, 0x07, 0x3a, 79 0x8d, 0x0f, 0x54, 0x58, 0x94, 0x12, 0xc5, 0x62, 0x18, 0xb9, 0x42, 0xf0, 80 0x6c, 0x73, 0xa0, 0x92, 0xad, 0x27, 0x1c, 0x20, 0x0f, 0xc1, 0xca, 0x44, 81 0x87, 0x47, 0xc5, 0x43, 0x23, 0x01, 0xda, 0x23, 0xe2, 0x89, 0x38, 0x9f, 82 0x1f, 0x8d, 0x8c, 0xc6, 0x95, 0xa3, 0x34, 0x21, 0x21, 0x2d, 0x49, 0xea, 83 0x4b, 0x05, 0x85, 0xf5, 0x58, 0x25, 0x13, 0xcd, 0x51, 0x19, 0x1a, 0x88, 84 0xa6, 0x83, 0xd6, 0xd0, 0xbc, 0x25, 0x19, 0x1c, 0x92, 0x12, 0x44, 0x5d, 85 0x1c, 0x04, 0xf1, 0x99, 0xdf, 0x92, 0x8e, 0x09, 0x85, 0xf3, 0x88, 0x82, 86 0x4c, 0x22, 0x17, 0xc5, 0x25, 0x23, 0xed, 0x78, 0xf5, 0x41, 0xd1, 0xe9, 87 0x8a, 0xb3, 0x52, 0xd1, 0x3d, 0x79, 0x81, 0x4d, 0x31, 0x24, 0xf9, 0x38, 88 0x96, 0xbc, 0xf4, 0x8c, 0x25, 0xe9, 0xf2, 0x73, 0x94, 0x85, 0xc2, 0x61, 89 0x6a, 0x34, 0x68, 0x65, 0x78, 0x87, 0xa6, 0x4f 90 }; 91 92 } // namespace 93 94 namespace media { 95 96 static const int kPresentationTimeBase = 100; 97 98 void DecodeMediaFrame( 99 VideoCodecBridge* media_codec, const uint8* data, size_t data_size, 100 const base::TimeDelta input_presentation_timestamp, 101 const base::TimeDelta initial_timestamp_lower_bound) { 102 base::TimeDelta input_pts = input_presentation_timestamp; 103 base::TimeDelta timestamp = initial_timestamp_lower_bound; 104 base::TimeDelta new_timestamp; 105 for (int i = 0; i < 10; ++i) { 106 int input_buf_index = media_codec->DequeueInputBuffer( 107 MediaCodecBridge::kTimeOutInfinity); 108 media_codec->QueueInputBuffer( 109 input_buf_index, data, data_size, input_presentation_timestamp); 110 size_t unused_offset = 0; 111 size_t size = 0; 112 bool eos = false; 113 int output_buf_index = media_codec->DequeueOutputBuffer( 114 MediaCodecBridge::kTimeOutInfinity, 115 &unused_offset, &size, &new_timestamp, &eos); 116 if (output_buf_index > 0) 117 media_codec->ReleaseOutputBuffer(output_buf_index, false); 118 // Output time stamp should not be smaller than old timestamp. 119 ASSERT_TRUE(new_timestamp >= timestamp); 120 input_pts += base::TimeDelta::FromMicroseconds(33000); 121 timestamp = new_timestamp; 122 } 123 } 124 125 TEST(MediaCodecBridgeTest, Initialize) { 126 if (!MediaCodecBridge::IsAvailable()) 127 return; 128 129 scoped_ptr<media::MediaCodecBridge> media_codec; 130 media_codec.reset(VideoCodecBridge::Create(kCodecH264)); 131 } 132 133 TEST(MediaCodecBridgeTest, DoNormal) { 134 if (!MediaCodecBridge::IsAvailable()) 135 return; 136 137 scoped_ptr<media::AudioCodecBridge> media_codec; 138 media_codec.reset(AudioCodecBridge::Create(kCodecMP3)); 139 140 media_codec->Start(kCodecMP3, 44100, 2, NULL, 0, false, NULL); 141 142 int input_buf_index = media_codec->DequeueInputBuffer( 143 MediaCodecBridge::kTimeOutInfinity); 144 ASSERT_GE(input_buf_index, 0); 145 146 int64 input_pts = kPresentationTimeBase; 147 media_codec->QueueInputBuffer( 148 input_buf_index, test_mp3, sizeof(test_mp3), 149 base::TimeDelta::FromMicroseconds(++input_pts)); 150 151 input_buf_index = media_codec->DequeueInputBuffer( 152 MediaCodecBridge::kTimeOutInfinity); 153 media_codec->QueueInputBuffer( 154 input_buf_index, test_mp3, sizeof(test_mp3), 155 base::TimeDelta::FromMicroseconds(++input_pts)); 156 157 input_buf_index = media_codec->DequeueInputBuffer( 158 MediaCodecBridge::kTimeOutInfinity); 159 media_codec->QueueEOS(input_buf_index); 160 161 input_pts = kPresentationTimeBase; 162 bool eos = false; 163 while (!eos) { 164 size_t unused_offset = 0; 165 size_t size = 0; 166 base::TimeDelta timestamp; 167 int output_buf_index = media_codec->DequeueOutputBuffer( 168 MediaCodecBridge::kTimeOutInfinity, 169 &unused_offset, &size, ×tamp, &eos); 170 switch (output_buf_index) { 171 case MediaCodecBridge::INFO_TRY_AGAIN_LATER: 172 FAIL(); 173 return; 174 175 case MediaCodecBridge::INFO_OUTPUT_FORMAT_CHANGED: 176 continue; 177 178 case MediaCodecBridge::INFO_OUTPUT_BUFFERS_CHANGED: 179 media_codec->GetOutputBuffers(); 180 continue; 181 } 182 EXPECT_LE(1u, size); 183 if (!eos) 184 EXPECT_EQ(++input_pts, timestamp.InMicroseconds()); 185 ASSERT_LE(input_pts, kPresentationTimeBase + 2); 186 } 187 ASSERT_EQ(input_pts, kPresentationTimeBase + 2); 188 } 189 190 TEST(MediaCodecBridgeTest, InvalidVorbisHeader) { 191 if (!MediaCodecBridge::IsAvailable()) 192 return; 193 194 scoped_ptr<media::AudioCodecBridge> media_codec; 195 media_codec.reset(AudioCodecBridge::Create(kCodecVorbis)); 196 197 // The first byte of the header is not 0x02. 198 uint8 invalid_first_byte[] = { 0x00, 0xff, 0xff, 0xff, 0xff }; 199 EXPECT_FALSE(media_codec->Start( 200 kCodecVorbis, 44100, 2, invalid_first_byte, sizeof(invalid_first_byte), 201 false, NULL)); 202 203 // Size of the header does not match with the data we passed in. 204 uint8 invalid_size[] = { 0x02, 0x01, 0xff, 0x01, 0xff }; 205 EXPECT_FALSE(media_codec->Start( 206 kCodecVorbis, 44100, 2, invalid_size, sizeof(invalid_size), false, NULL)); 207 208 // Size of the header is too large. 209 size_t large_size = 8 * 1024 * 1024 + 2; 210 uint8* very_large_header = new uint8[large_size]; 211 very_large_header[0] = 0x02; 212 for (size_t i = 1; i < large_size - 1; ++i) 213 very_large_header[i] = 0xff; 214 very_large_header[large_size - 1] = 0xfe; 215 EXPECT_FALSE(media_codec->Start( 216 kCodecVorbis, 44100, 2, very_large_header, 0x80000000, false, NULL)); 217 delete[] very_large_header; 218 } 219 220 TEST(MediaCodecBridgeTest, PresentationTimestampsDoNotDecrease) { 221 if (!MediaCodecBridge::IsAvailable()) 222 return; 223 224 scoped_ptr<VideoCodecBridge> media_codec; 225 media_codec.reset(VideoCodecBridge::Create(kCodecVP8)); 226 EXPECT_TRUE(media_codec->Start( 227 kCodecVP8, gfx::Size(320, 240), NULL, NULL)); 228 scoped_refptr<DecoderBuffer> buffer = 229 ReadTestDataFile("vp8-I-frame-320x240"); 230 DecodeMediaFrame( 231 media_codec.get(), buffer->data(), buffer->data_size(), 232 base::TimeDelta(), base::TimeDelta()); 233 234 // Simulate a seek to 10 seconds, and each chunk has 2 I-frames. 235 std::vector<uint8> chunk(buffer->data(), 236 buffer->data() + buffer->data_size()); 237 chunk.insert(chunk.end(), buffer->data(), 238 buffer->data() + buffer->data_size()); 239 media_codec->Reset(); 240 DecodeMediaFrame(media_codec.get(), &chunk[0], chunk.size(), 241 base::TimeDelta::FromMicroseconds(10000000), 242 base::TimeDelta::FromMicroseconds(9900000)); 243 244 // Simulate a seek to 5 seconds. 245 media_codec->Reset(); 246 DecodeMediaFrame(media_codec.get(), &chunk[0], chunk.size(), 247 base::TimeDelta::FromMicroseconds(5000000), 248 base::TimeDelta::FromMicroseconds(4900000)); 249 } 250 251 TEST(MediaCodecBridgeTest, CreateUnsupportedCodec) { 252 EXPECT_EQ(NULL, AudioCodecBridge::Create(kUnknownAudioCodec)); 253 EXPECT_EQ(NULL, VideoCodecBridge::Create(kUnknownVideoCodec)); 254 } 255 256 } // namespace media 257