OLD | NEW |
---|---|
(Empty) | |
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/stringprintf.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 | |
10 namespace media { | |
11 | |
12 // Friend class to get access to internals of ContainerNames. | |
13 class ContainerTest : public testing::Test { | |
14 public: | |
15 // Returns the number of elements in container_name_mapping. | |
16 static int MappingSize() { | |
17 int size = ContainerNames::ContainerNameMappingSize(); | |
18 EXPECT_GT(size, 0); | |
19 return size; | |
20 } | |
21 | |
22 // Returns the mapping at the specified index. | |
23 static ContainerNameMapping Mapping(int index) { | |
24 EXPECT_GE(index, 0); | |
25 EXPECT_LE(index, MappingSize()); | |
26 return ContainerNames::container_name_mapping[index]; | |
27 } | |
28 | |
29 // Determine the container based on a name. | |
30 static FFmpegContainerName Lookup(const char* name) { | |
31 return ContainerNames::LookupContainer(name); | |
32 } | |
33 | |
34 // Verify the container name matches the result based | |
35 // on the contents of a buffer. | |
36 static void Verify(FFmpegContainerName name, | |
xhwang
2013/05/03 19:59:06
Verify seems too generic. How about ExpectContaine
jrummell
2013/05/03 23:56:21
Done.
| |
37 char* buffer, | |
38 size_t buffer_size) { | |
39 EXPECT_EQ(name, | |
40 ContainerNames::DetermineContainer( | |
41 reinterpret_cast<uint8_t*>(buffer), buffer_size)); | |
42 } | |
43 }; | |
44 | |
45 // Test that the names are in the correct sorted order. | |
46 TEST(ContainerNamesTest, CheckSortOrder) { | |
47 for (int i = 1; i < ContainerTest::MappingSize(); ++i) { | |
48 EXPECT_LT(strcasecmp(ContainerTest::Mapping(i - 1).name, | |
49 ContainerTest::Mapping(i).name), | |
50 0) | |
51 << ContainerTest::Mapping(i - 1).name << " and " | |
52 << ContainerTest::Mapping(i).name << " are in the wrong order."; | |
53 } | |
54 } | |
55 | |
56 // Test that string lookup works for all names in the list. | |
57 TEST(ContainerNamesTest, NameLookup) { | |
58 // Search for all strings. | |
59 for (int i = 0; i < ContainerTest::MappingSize(); ++i) { | |
60 const char* name = ContainerTest::Mapping(i).name; | |
61 EXPECT_EQ(ContainerTest::Mapping(i).id, ContainerTest::Lookup(name)) | |
62 << "Unable to lookup " << name; | |
63 } | |
64 | |
65 // Check that some name not in the list fails. | |
66 EXPECT_EQ(CONTAINER_UNKNOWN, ContainerTest::Lookup("foo")); | |
67 // A name before the first one in the list. | |
68 EXPECT_EQ(CONTAINER_UNKNOWN, ContainerTest::Lookup(" ")); | |
69 // A name after the last one in the list. | |
70 EXPECT_EQ(CONTAINER_UNKNOWN, ContainerTest::Lookup("ZZZ")); | |
71 } | |
72 | |
73 #define BYTE_ORDER_MARK "\xef\xbb\xbf" | |
74 | |
75 // Test that small buffers are handled correctly. | |
76 TEST(ContainerNamesTest, CheckSmallBufferSize) { | |
77 // Empty buffer. | |
78 char buffer[0]; | |
79 ContainerTest::Verify(CONTAINER_UNKNOWN, buffer, 0); | |
80 | |
81 // Try a simple string | |
82 char buffer1[] = "******** START SCRIPT ********"; | |
83 ContainerTest::Verify(CONTAINER_SUBVIEWER1, buffer1, strlen(buffer1)); | |
84 | |
85 // Try adding byte order mark at beginning. | |
86 char buffer2[] = BYTE_ORDER_MARK "Interplay MVE File\x1A\x00\x1A"; | |
87 ContainerTest::Verify(CONTAINER_IPMOVIE, buffer2, 24); | |
88 // Repeat but leave out a character (so it won't match) | |
89 ContainerTest::Verify(CONTAINER_UNKNOWN, buffer2, 23); | |
90 | |
91 // Try a simple SRT file. | |
92 char buffer3[] = | |
93 "1\n" | |
94 "00:03:23,550 --> 00:03:24,375\n" | |
95 "You always had a hard time finding your place in this world.\n" | |
96 "\n" | |
97 "2\n" | |
98 "00:03:24,476 --> 00:03:25,175\n" | |
99 "What are you talking about?\n"; | |
100 ContainerTest::Verify(CONTAINER_SRT, buffer3, strlen(buffer3)); | |
101 | |
102 // Try a simple tag. Note that the tag checking requires | |
103 // at least 128 characters. | |
104 char buffer4[256] = ".snd"; | |
xhwang
2013/05/03 19:59:06
will 128 work?
jrummell
2013/05/03 23:56:21
Done.
| |
105 ContainerTest::Verify(CONTAINER_AU, buffer4, sizeof(buffer4)); | |
106 | |
107 // HLS has it's own loop, so try it | |
xhwang
2013/05/03 19:59:06
Here and below, comments should be complete senten
jrummell
2013/05/03 23:56:21
Done.
| |
108 char buffer5[256] = "#EXTM3U" | |
109 "some other random stuff" | |
xhwang
2013/05/03 19:59:06
indent
jrummell
2013/05/03 23:56:21
Done.
| |
110 "#EXT-X-MEDIA-SEQUENCE:"; | |
111 ContainerTest::Verify(CONTAINER_HLS, buffer5, sizeof(buffer5)); | |
112 | |
113 // PJS has several loops, so try it | |
114 char buffer6[256] = "1234567890,123456,0\n" | |
115 "some filler\n" | |
xhwang
2013/05/03 19:59:06
indent
jrummell
2013/05/03 23:56:21
Done.
| |
116 "\"quoted string\"\n"; | |
117 ContainerTest::Verify(CONTAINER_PJS, buffer6, sizeof(buffer6)); | |
118 | |
119 // try a large buffer all 0 | |
120 char buffer7[4096]; | |
121 memset(buffer7, 0, sizeof(buffer7)); | |
122 ContainerTest::Verify(CONTAINER_UNKNOWN, buffer7, sizeof(buffer7)); | |
123 | |
124 // reuse buffer, but all \n this time | |
125 memset(buffer7, '\n', sizeof(buffer7)); | |
126 ContainerTest::Verify(CONTAINER_UNKNOWN, buffer7, sizeof(buffer7)); | |
127 } | |
128 | |
129 struct Mapping { | |
xhwang
2013/05/03 19:59:06
Document what this is.
jrummell
2013/05/03 23:56:21
Done.
| |
130 FFmpegContainerName id; | |
131 int length; | |
xhwang
2013/05/03 19:59:06
Comment about what this length is. Why you have 0
jrummell
2013/05/03 23:56:21
Done.
| |
132 const char* name; | |
133 }; | |
134 | |
135 // The following are the fixed strings compared in ContainerNames. | |
136 // Since the first 4 characters are used as a TAG, this checks that the | |
137 // TAG is defined correctly. | |
138 const Mapping fixed_strings[] = { | |
xhwang
2013/05/03 19:59:06
Use kCamelCase for consts.
jrummell
2013/05/03 23:56:21
Done.
| |
139 { CONTAINER_AMR, 0, "#!AMR" }, | |
140 { CONTAINER_APC, 0, "CRYO_APC" }, | |
141 { CONTAINER_AQTITLE, 0, "-->> 23" }, | |
142 { CONTAINER_ASF, 16, "\x30\x26\xb2\x75\x8e\x66\xcf\x11\xa6\xd9\x00" | |
143 "\xaa\x00\x62\xce\x6c" }, | |
144 { CONTAINER_ASS, 0, "[Script Info]" }, | |
145 { CONTAINER_ASS, 0, BYTE_ORDER_MARK "[Script Info]" }, | |
146 { CONTAINER_CONCAT, 0, "ffconcat version 1.0" }, | |
147 { CONTAINER_DNXHD, 43, "\x00\x00\x02\x80\x01 789*123456789*123456789" | |
148 "*123456789*\x04\xd3" }, | |
149 { CONTAINER_FFMETADATA, 0, ";FFMETADATA" }, | |
150 { CONTAINER_IDF, 12, "\x04\x31\x2e\x34\x00\x00\x00\x00\x4f\x00\x15" | |
151 "\x00" }, | |
152 { CONTAINER_ILBC, 0, "#!iLBC" }, | |
153 { CONTAINER_ISS, 0, "IMA_ADPCM_Sound" }, | |
154 { CONTAINER_IV8, 0, "\x01\x01\x03\xb8\x80\x60" }, | |
155 { CONTAINER_JV, 0, "JVxx Compression by John M Phillips Copyright " | |
156 "(C) 1995 The Bitmap Brothers Ltd." }, | |
157 { CONTAINER_LIBNUT, 0, "nut/multimedia container" }, | |
158 { CONTAINER_LXF, 8, "LEITCH\x00\x00" }, | |
159 { CONTAINER_NUV, 0, "NuppelVideo" }, | |
160 { CONTAINER_NUV, 0, "MythTVVideo" }, | |
161 { CONTAINER_PAF, 0, "Packed Animation File V1.0\n(c) 1992-96 " | |
162 "Amazing Studio\x0a\x1a" }, | |
163 { CONTAINER_REALTEXT, 0, "<window" }, | |
164 { CONTAINER_REALTEXT, 0, BYTE_ORDER_MARK "<window" }, | |
165 { CONTAINER_RPL, 0, "ARMovie\x0A" }, | |
166 { CONTAINER_SAMI, 0, "<SAMI>" }, | |
167 { CONTAINER_SAMI, 0, BYTE_ORDER_MARK "<SAMI>" }, | |
168 { CONTAINER_SMJPEG, 8, "\x00\x0aSMJPEG" }, | |
169 { CONTAINER_VPLAYER, 0, "12:34:56.789=" }, | |
170 { CONTAINER_VOBSUB, 0, "# VobSub index file," }, | |
171 { CONTAINER_VOC, 0, "Creative Voice File\x1A" }, | |
172 { CONTAINER_W64, 42, "riff\x2e\x91\xcf\x11\xa5\xd6\x28\xdb\x04\xc1" | |
173 "\x00\x00 89*1234wave\xf3\xac\xd3\x11\x8c\xd1" | |
174 "\x00\xc0\x4f\x8e\xdb\x8a" }, | |
175 { CONTAINER_WEBVTT, 0, "WEBVTT" }, | |
176 { CONTAINER_WEBVTT, 0, BYTE_ORDER_MARK "WEBVTT" }, | |
177 { CONTAINER_WTV, 16, "\xb7\xd8\x00\x20\x37\x49\xda\x11\xa6\x4e\x00" | |
178 "\x07\xe9\x5e\xad\x8d" }, | |
179 { CONTAINER_YUV4MPEGPIPE, 0, "YUV4MPEG2" } | |
180 }; | |
181 | |
182 // Test that containers that start with fixed strings are handled correctly. | |
183 // This is to verify that the TAG matches the first 4 characters of the string. | |
184 TEST(ContainerNamesTest, CheckFixedStrings) { | |
185 for (int i = 0; i < static_cast<int>(arraysize(fixed_strings)); ++i) { | |
186 char buffer[256]; | |
187 int length = fixed_strings[i].length; | |
188 if (length == 0) | |
189 length = strlen(fixed_strings[i].name); | |
190 memcpy(buffer, fixed_strings[i].name, length); | |
191 // Put some random characters after the string. | |
192 buffer[length] = 'a'; | |
193 buffer[length + 1] = 'b'; | |
194 ContainerTest::Verify( | |
195 fixed_strings[i].id, buffer, sizeof(buffer)); | |
196 } | |
197 } | |
198 | |
199 // Determine the container type of a specified file. | |
200 void TestFile(FFmpegContainerName expected, const base::FilePath& filename) { | |
201 // Open the file. | |
202 FILE* f = fopen(filename.value().c_str(), "r"); | |
203 EXPECT_NE(f, (FILE*)NULL); | |
204 | |
205 // Read the first few bytes. | |
206 char buffer[8192]; | |
207 size_t read = fread(buffer, sizeof(char), sizeof(buffer), f); | |
xhwang
2013/05/03 19:59:06
copy from other comment: Use file_util::ReadFile()
jrummell
2013/05/03 23:56:21
Done.
| |
208 | |
209 // Now verify the type. | |
210 ContainerTest::Verify(expected, buffer, read); | |
211 } | |
212 | |
213 // Test several files to ensure that the container is detected properly. | |
214 TEST(ContainerNamesTest, FileCheck) { | |
215 TestFile(CONTAINER_OGG, GetTestDataFilePath("bear.ogv")); | |
216 TestFile(CONTAINER_OGG, GetTestDataFilePath("9ch.ogg")); | |
217 TestFile(CONTAINER_WAV, GetTestDataFilePath("4ch.wav")); | |
218 TestFile(CONTAINER_WAV, GetTestDataFilePath("sfx_f32le.wav")); | |
219 TestFile(CONTAINER_WAV, GetTestDataFilePath("sfx_s16le.wav")); | |
220 TestFile(CONTAINER_MOV, GetTestDataFilePath("bear-1280x720.mp4")); | |
221 TestFile(CONTAINER_MOV, GetTestDataFilePath("sfx.m4a")); | |
222 TestFile(CONTAINER_WEBM, GetTestDataFilePath("bear-320x240.webm")); | |
223 TestFile(CONTAINER_MP3, GetTestDataFilePath("id3_test.mp3")); | |
224 TestFile(CONTAINER_MP3, GetTestDataFilePath("sfx.mp3")); | |
225 | |
226 // now try a few non containers | |
227 TestFile(CONTAINER_UNKNOWN, GetTestDataFilePath("ten_byte_file")); | |
228 TestFile(CONTAINER_UNKNOWN, GetTestDataFilePath("README")); | |
229 TestFile(CONTAINER_UNKNOWN, GetTestDataFilePath("bali_640x360_P422.yuv")); | |
230 TestFile(CONTAINER_UNKNOWN, GetTestDataFilePath("bali_640x360_RGB24.rgb")); | |
231 TestFile(CONTAINER_UNKNOWN, GetTestDataFilePath("webm_vp8_track_entry")); | |
232 } | |
233 | |
234 } // namespace media | |
OLD | NEW |