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 "base/files/file_util.h" 6#include "media/base/container_names.h" 7#include "media/base/test_data_util.h" 8#include "testing/gtest/include/gtest/gtest.h" 9 10namespace media { 11 12namespace container_names { 13 14// Using a macros to simplify tests. Since EXPECT_EQ outputs the second argument 15// as a string when it fails, this lets the output identify what item actually 16// failed. 17#define VERIFY(buffer, name) \ 18 EXPECT_EQ(name, \ 19 DetermineContainer(reinterpret_cast<const uint8*>(buffer), \ 20 sizeof(buffer))) 21 22// Test that small buffers are handled correctly. 23TEST(ContainerNamesTest, CheckSmallBuffer) { 24 // Empty buffer. 25 char buffer[1]; // ([0] not allowed on win) 26 VERIFY(buffer, CONTAINER_UNKNOWN); 27 28 // Try a simple SRT file. 29 char buffer1[] = 30 "1\n" 31 "00:03:23,550 --> 00:03:24,375\n" 32 "You always had a hard time finding your place in this world.\n" 33 "\n" 34 "2\n" 35 "00:03:24,476 --> 00:03:25,175\n" 36 "What are you talking about?\n"; 37 VERIFY(buffer1, CONTAINER_SRT); 38 39 // HLS has it's own loop. 40 char buffer2[] = "#EXTM3U" 41 "some other random stuff" 42 "#EXT-X-MEDIA-SEQUENCE:"; 43 VERIFY(buffer2, CONTAINER_HLS); 44 45 // Try a large buffer all zeros. 46 char buffer3[4096]; 47 memset(buffer3, 0, sizeof(buffer3)); 48 VERIFY(buffer3, CONTAINER_UNKNOWN); 49 50 // Reuse buffer, but all \n this time. 51 memset(buffer3, '\n', sizeof(buffer3)); 52 VERIFY(buffer3, CONTAINER_UNKNOWN); 53} 54 55#define BYTE_ORDER_MARK "\xef\xbb\xbf" 56 57// Note that the comparisons need at least 12 bytes, so make sure the buffer is 58// at least that size. 59const char kAmrBuffer[12] = "#!AMR"; 60uint8 kAsfBuffer[] = { 0x30, 0x26, 0xb2, 0x75, 0x8e, 0x66, 0xcf, 0x11, 0xa6, 61 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c }; 62const char kAss1Buffer[] = "[Script Info]"; 63const char kAss2Buffer[] = BYTE_ORDER_MARK "[Script Info]"; 64uint8 kCafBuffer[] = { 'c', 'a', 'f', 'f', 0, 1, 0, 0, 'd', 'e', 's', 'c', 0, 0, 65 0, 0, 0, 0, 0, 32, 64, 229, 136, 128, 0, 0, 0, 0, 'a', 66 'a', 'c', ' ', 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 67 0, 2, 0, 0, 0, 0 }; 68const char kDtshdBuffer[12] = "DTSHDHDR"; 69const char kDxaBuffer[16] = "DEXA"; 70const char kFlacBuffer[12] = "fLaC"; 71uint8 kFlvBuffer[12] = { 'F', 'L', 'V', 0, 0, 0, 0, 1, 0, 0, 0, 0 }; 72uint8 kIrcamBuffer[] = { 0x64, 0xa3, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1 }; 73const char kRm1Buffer[12] = ".RMF\0\0"; 74const char kRm2Buffer[12] = ".ra\xfd"; 75uint8 kWtvBuffer[] = { 0xb7, 0xd8, 0x00, 0x20, 0x37, 0x49, 0xda, 0x11, 0xa6, 76 0x4e, 0x00, 0x07, 0xe9, 0x5e, 0xad, 0x8d }; 77uint8 kBug263073Buffer[] = { 78 0x00, 0x00, 0x00, 0x18, 0x66, 0x74, 0x79, 0x70, 0x6d, 0x70, 0x34, 0x32, 79 0x00, 0x00, 0x00, 0x00, 0x69, 0x73, 0x6f, 0x6d, 0x6d, 0x70, 0x34, 0x32, 80 0x00, 0x00, 0x00, 0x01, 0x6d, 0x64, 0x61, 0x74, 0x00, 0x00, 0x00, 0x00, 81 0xaa, 0x2e, 0x22, 0xcf, 0x00, 0x00, 0x00, 0x37, 0x67, 0x64, 0x00, 0x28, 82 0xac, 0x2c, 0xa4, 0x01, 0xe0, 0x08, 0x9f, 0x97, 0x01, 0x52, 0x02, 0x02, 83 0x02, 0x80, 0x00, 0x01}; 84 85// Test that containers that start with fixed strings are handled correctly. 86// This is to verify that the TAG matches the first 4 characters of the string. 87TEST(ContainerNamesTest, CheckFixedStrings) { 88 VERIFY(kAmrBuffer, CONTAINER_AMR); 89 VERIFY(kAsfBuffer, CONTAINER_ASF); 90 VERIFY(kAss1Buffer, CONTAINER_ASS); 91 VERIFY(kAss2Buffer, CONTAINER_ASS); 92 VERIFY(kCafBuffer, CONTAINER_CAF); 93 VERIFY(kDtshdBuffer, CONTAINER_DTSHD); 94 VERIFY(kDxaBuffer, CONTAINER_DXA); 95 VERIFY(kFlacBuffer, CONTAINER_FLAC); 96 VERIFY(kFlvBuffer, CONTAINER_FLV); 97 VERIFY(kIrcamBuffer, CONTAINER_IRCAM); 98 VERIFY(kRm1Buffer, CONTAINER_RM); 99 VERIFY(kRm2Buffer, CONTAINER_RM); 100 VERIFY(kWtvBuffer, CONTAINER_WTV); 101 VERIFY(kBug263073Buffer, CONTAINER_MOV); 102} 103 104// Determine the container type of a specified file. 105void TestFile(MediaContainerName expected, const base::FilePath& filename) { 106 char buffer[8192]; 107 108 // Windows implementation of ReadFile fails if file smaller than desired size, 109 // so use file length if file less than 8192 bytes (http://crbug.com/243885). 110 int read_size = sizeof(buffer); 111 int64 actual_size; 112 if (base::GetFileSize(filename, &actual_size) && actual_size < read_size) 113 read_size = actual_size; 114 int read = base::ReadFile(filename, buffer, read_size); 115 116 // Now verify the type. 117 EXPECT_EQ(expected, 118 DetermineContainer(reinterpret_cast<const uint8*>(buffer), read)) 119 << "Failure with file " << filename.value(); 120} 121 122TEST(ContainerNamesTest, FileCheckOGG) { 123 TestFile(CONTAINER_OGG, GetTestDataFilePath("bear.ogv")); 124 TestFile(CONTAINER_OGG, GetTestDataFilePath("9ch.ogg")); 125} 126 127TEST(ContainerNamesTest, FileCheckWAV) { 128 TestFile(CONTAINER_WAV, GetTestDataFilePath("4ch.wav")); 129 TestFile(CONTAINER_WAV, GetTestDataFilePath("sfx_f32le.wav")); 130 TestFile(CONTAINER_WAV, GetTestDataFilePath("sfx_s16le.wav")); 131} 132 133TEST(ContainerNamesTest, FileCheckMOV) { 134 TestFile(CONTAINER_MOV, GetTestDataFilePath("bear-1280x720.mp4")); 135 TestFile(CONTAINER_MOV, GetTestDataFilePath("sfx.m4a")); 136} 137 138TEST(ContainerNamesTest, FileCheckWEBM) { 139 TestFile(CONTAINER_WEBM, GetTestDataFilePath("bear-320x240.webm")); 140 TestFile(CONTAINER_WEBM, GetTestDataFilePath("no_streams.webm")); 141 TestFile(CONTAINER_WEBM, GetTestDataFilePath("webm_ebml_element")); 142} 143 144TEST(ContainerNamesTest, FileCheckMP3) { 145 TestFile(CONTAINER_MP3, GetTestDataFilePath("id3_test.mp3")); 146 TestFile(CONTAINER_MP3, GetTestDataFilePath("sfx.mp3")); 147} 148 149TEST(ContainerNamesTest, FileCheckAC3) { 150 TestFile(CONTAINER_AC3, GetTestDataFilePath("bear.ac3")); 151} 152 153TEST(ContainerNamesTest, FileCheckAAC) { 154 TestFile(CONTAINER_AAC, GetTestDataFilePath("bear.adts")); 155} 156 157TEST(ContainerNamesTest, FileCheckAIFF) { 158 TestFile(CONTAINER_AIFF, GetTestDataFilePath("bear.aiff")); 159} 160 161TEST(ContainerNamesTest, FileCheckASF) { 162 TestFile(CONTAINER_ASF, GetTestDataFilePath("bear.asf")); 163} 164 165TEST(ContainerNamesTest, FileCheckAVI) { 166 TestFile(CONTAINER_AVI, GetTestDataFilePath("bear.avi")); 167} 168 169TEST(ContainerNamesTest, FileCheckEAC3) { 170 TestFile(CONTAINER_EAC3, GetTestDataFilePath("bear.eac3")); 171} 172 173TEST(ContainerNamesTest, FileCheckFLAC) { 174 TestFile(CONTAINER_FLAC, GetTestDataFilePath("bear.flac")); 175} 176 177TEST(ContainerNamesTest, FileCheckFLV) { 178 TestFile(CONTAINER_FLV, GetTestDataFilePath("bear.flv")); 179} 180 181TEST(ContainerNamesTest, FileCheckH261) { 182 TestFile(CONTAINER_H261, GetTestDataFilePath("bear.h261")); 183} 184 185TEST(ContainerNamesTest, FileCheckH263) { 186 TestFile(CONTAINER_H263, GetTestDataFilePath("bear.h263")); 187} 188 189TEST(ContainerNamesTest, FileCheckMJPEG) { 190 TestFile(CONTAINER_MJPEG, GetTestDataFilePath("bear.mjpeg")); 191} 192 193TEST(ContainerNamesTest, FileCheckMPEG2PS) { 194 TestFile(CONTAINER_MPEG2PS, GetTestDataFilePath("bear.mpeg")); 195} 196 197TEST(ContainerNamesTest, FileCheckMPEG2TS) { 198 TestFile(CONTAINER_MPEG2TS, GetTestDataFilePath("bear.m2ts")); 199} 200 201TEST(ContainerNamesTest, FileCheckRM) { 202 TestFile(CONTAINER_RM, GetTestDataFilePath("bear.rm")); 203} 204 205TEST(ContainerNamesTest, FileCheckSWF) { 206 TestFile(CONTAINER_SWF, GetTestDataFilePath("bear.swf")); 207} 208 209// Try a few non containers. 210TEST(ContainerNamesTest, FileCheckUNKNOWN) { 211 TestFile(CONTAINER_UNKNOWN, GetTestDataFilePath("ten_byte_file")); 212 TestFile(CONTAINER_UNKNOWN, GetTestDataFilePath("README")); 213 TestFile(CONTAINER_UNKNOWN, GetTestDataFilePath("bali_640x360_P422.yuv")); 214 TestFile(CONTAINER_UNKNOWN, GetTestDataFilePath("bali_640x360_RGB24.rgb")); 215 TestFile(CONTAINER_UNKNOWN, GetTestDataFilePath("webm_vp8_track_entry")); 216} 217 218} // namespace container_names 219 220} // namespace media 221