Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(43)

Side by Side Diff: media/base/container_names_unittest.cc

Issue 14495010: Add UMA stats for audio/video containers. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 // Template to extract array size.
13 template <typename T, size_t N> inline size_t size(T(&)[N]) { return N; }
xhwang 2013/04/29 21:00:25 Can you use arraysize directly? https://code.goo
jrummell 2013/04/30 21:36:50 Done.
14
15 #define BOM "\xef\xbb\xbf"
xhwang 2013/04/29 21:00:25 what is BOM? Use a more readable name? Or add a co
jrummell 2013/04/30 21:36:50 Done.
16
17 // Test that the names are in the correct sorted order.
18 TEST(ContainerNamesTest, CheckSortOrder) {
19 for (size_t i = 1; i < size(ContainerNames::container_name_mapping); ++i) {
20 ASSERT_LT(strcmp(ContainerNames::container_name_mapping[i - 1].name,
xhwang 2013/04/29 21:00:25 Use base::strcasecmp ? https://code.google.com/p/
jrummell 2013/04/30 21:36:50 Done. Also in container_names.cc.
21 ContainerNames::container_name_mapping[i].name),
22 0)
23 << ContainerNames::container_name_mapping[i - 1].name << " and "
24 << ContainerNames::container_name_mapping[i].name
25 << " are in the wrong order.";
26 }
27 }
28
29 // Test that string lookup works for all names in the list.
30 TEST(ContainerNamesTest, NameLookup) {
31 ContainerNames cont;
32
33 // Search for all strings.
34 for (size_t i = 0; i < size(ContainerNames::container_name_mapping); ++i) {
35 const char* name = ContainerNames::container_name_mapping[i].name;
36 ASSERT_EQ(ContainerNames::container_name_mapping[i].id,
37 cont.LookupContainer(name)) << "Unable to lookup " << name;
38 }
39
40 // Check that some name not in the list fails.
41 ASSERT_EQ(CONTAINER_UNKNOWN, cont.LookupContainer("foo"));
42 }
43
44 // Test that small buffers are handled correctly.
45 TEST(ContainerNamesTest, CheckSmallBufferSize) {
46 ContainerNames container;
47
48 // Empty buffer.
49 uint8_t buffer[0];
50 EXPECT_EQ(CONTAINER_UNKNOWN, container.LookupContainer(buffer, 0));
51
52 // Try a simple string
53 const char buffer1[] = "******** START SCRIPT ********";
54 EXPECT_EQ(CONTAINER_SUBVIEWER1,
55 container.LookupContainer((uint8_t*)buffer1, strlen(buffer1)));
56
57 // Try adding BOM.
58 const char buffer2[] = "\xef\xbb\xbf"
59 "Interplay MVE File\x1A\x00\x1A";
60 EXPECT_EQ(CONTAINER_IPMOVIE,
61 container.LookupContainer((uint8_t*)buffer2, 24));
62 // Repeat but leave out a character (so it won't match)
63 EXPECT_EQ(CONTAINER_UNKNOWN,
64 container.LookupContainer((uint8_t*)buffer2, 23));
65
66 // Try a simple SRT file.
67 const char buffer3[] =
68 "1\n"
69 "00:03:23,550 --> 00:03:24,375\n"
70 "You always had a hard time finding your place in this world.\n"
71 "\n"
72 "2\n"
73 "00:03:24,476 --> 00:03:25,175\n"
74 "What are you talking about?\n";
75 EXPECT_EQ(CONTAINER_SRT,
76 container.LookupContainer((uint8_t*)buffer3, strlen(buffer3)));
77
78 // Try a simple tag. Note that the tag checking requires
79 // at least 128 characters.
80 const char buffer4[256] = ".snd";
81 EXPECT_EQ(CONTAINER_AU,
82 container.LookupContainer((uint8_t*)buffer4, sizeof(buffer4)));
83
84 // HLS has it's own loop, so try it
85 const char buffer5[256] = "#EXTM3U"
86 "some other random stuff"
87 "#EXT-X-MEDIA-SEQUENCE:";
88 EXPECT_EQ(CONTAINER_HLS,
89 container.LookupContainer((uint8_t*)buffer5, sizeof(buffer5)));
90
91 // PJS has several loops, so try it
92 const char buffer6[256] = "1234567890,123456,0\n"
93 "some filler\n"
94 "\"quoted string\"\n";
95 EXPECT_EQ(CONTAINER_PJS,
96 container.LookupContainer((uint8_t*)buffer6, sizeof(buffer6)));
97
98 // try a large buffer all 0
99 char buffer7[4096];
100 memset(buffer7, 0, sizeof(buffer7));
101 EXPECT_EQ(CONTAINER_UNKNOWN,
102 container.LookupContainer((uint8_t*)buffer7, sizeof(buffer7)));
103
104 // reuse buffer, but all \n this time
105 memset(buffer7, '\n', sizeof(buffer7));
106 EXPECT_EQ(CONTAINER_UNKNOWN,
107 container.LookupContainer((uint8_t*)buffer7, sizeof(buffer7)));
108 }
109
110 typedef struct Mapping {
111 enum FFmpegContainerName id;
112 int length;
113 const char* name;
114 } Mapping;
115
116 // The following are the fixed strings compared in ContainerNames.
117 // Since the first 4 characters are used as a TAG, this checks that the
118 // TAG is defined correctly.
119 const Mapping fixed_strings[] = {
120 { CONTAINER_AMR, 0, "#!AMR" },
121 { CONTAINER_APC, 0, "CRYO_APC" },
122 { CONTAINER_AQTITLE, 0, "-->> 23" },
123 { CONTAINER_ASF, 16, "\x30\x26\xb2\x75\x8e\x66\xcf\x11\xa6\xd9\x00"
124 "\xaa\x00\x62\xce\x6c" },
125 { CONTAINER_ASS, 0, "[Script Info]" },
126 { CONTAINER_ASS, 0, BOM "[Script Info]" },
127 { CONTAINER_CONCAT, 0, "ffconcat version 1.0" },
128 { CONTAINER_DNXHD, 43, "\x00\x00\x02\x80\x01 789*123456789*123456789"
129 "*123456789*\x04\xd3" },
130 { CONTAINER_FFMETADATA, 0, ";FFMETADATA" },
131 { CONTAINER_IDF, 12, "\x04\x31\x2e\x34\x00\x00\x00\x00\x4f\x00\x15"
132 "\x00" },
133 { CONTAINER_ILBC, 0, "#!iLBC" },
134 { CONTAINER_ISS, 0, "IMA_ADPCM_Sound" },
135 { CONTAINER_IV8, 0, "\x01\x01\x03\xb8\x80\x60" },
136 { CONTAINER_JV, 0, "JVxx Compression by John M Phillips Copyright "
137 "(C) 1995 The Bitmap Brothers Ltd." },
138 { CONTAINER_LIBNUT, 0, "nut/multimedia container" },
139 { CONTAINER_LXF, 8, "LEITCH\x00\x00" },
140 { CONTAINER_NUV, 0, "NuppelVideo" },
141 { CONTAINER_NUV, 0, "MythTVVideo" },
142 { CONTAINER_PAF, 0, "Packed Animation File V1.0\n(c) 1992-96 "
143 "Amazing Studio\x0a\x1a" },
144 { CONTAINER_REALTEXT, 0, "<window" },
145 { CONTAINER_REALTEXT, 0, BOM "<window" },
146 { CONTAINER_RPL, 0, "ARMovie\x0A" },
147 { CONTAINER_SAMI, 0, "<SAMI>" },
148 { CONTAINER_SAMI, 0, BOM "<SAMI>" },
149 { CONTAINER_SMJPEG, 8, "\x00\x0aSMJPEG" },
150 { CONTAINER_VOBSUB, 0, "# VobSub index file," },
151 { CONTAINER_VOC, 0, "Creative Voice File\x1A" },
152 { CONTAINER_W64, 42, "riff\x2e\x91\xcf\x11\xa5\xd6\x28\xdb\x04\xc1"
153 "\x00\x00 89*1234wave\xf3\xac\xd3\x11\x8c\xd1"
154 "\x00\xc0\x4f\x8e\xdb\x8a" },
155 { CONTAINER_WEBVTT, 0, "WEBVTT" },
156 { CONTAINER_WEBVTT, 0, BOM "WEBVTT" },
157 { CONTAINER_WTV, 16, "\xb7\xd8\x00\x20\x37\x49\xda\x11\xa6\x4e\x00"
158 "\x07\xe9\x5e\xad\x8d" },
159 { CONTAINER_YUV4MPEGPIPE, 0, "YUV4MPEG2" },
160 { CONTAINER_UNKNOWN, 0, "" }
161 };
162
163 // Test that containers that start with fixed strings are handled correctly.
164 // This is to verify that the TAG matches the first 4 characters of the string.
165 TEST(ContainerNamesTest, CheckFixedStrings) {
166 ContainerNames container;
167 int index = 0;
168 while (fixed_strings[index].id != CONTAINER_UNKNOWN) {
169 char buffer[256];
170 int length = fixed_strings[index].length;
171 if (length == 0)
172 length = strlen(fixed_strings[index].name);
173 memcpy(buffer, fixed_strings[index].name, length);
174 // Put some random characters after the string.
175 buffer[length] = 'a';
176 buffer[length + 1] = 'b';
177 EXPECT_EQ(fixed_strings[index].id,
178 container.LookupContainer((uint8_t*)buffer, sizeof(buffer)));
179 ++index;
180 }
181 }
182
183 // Determine the container type of a specified file.
184 FFmpegContainerName TestFile(const base::FilePath& filename) {
185 // Open the file.
186 FILE* f = fopen(filename.value().c_str(), "r");
187 EXPECT_NE(f, (FILE*)NULL);
188
189 // Read the first few bytes.
190 uint8_t buffer[8192];
191 size_t read = fread(buffer, sizeof(uint8_t), sizeof(buffer), f);
192
193 // Now determine the type.
194 ContainerNames container;
195 return container.LookupContainer(buffer, read);
196 }
197
198 // Test several files to ensure that the container is detected properly.
199 TEST(ContainerNamesTest, FileCheck) {
200 EXPECT_EQ(CONTAINER_OGG, TestFile(GetTestDataFilePath("bear.ogv")));
201 EXPECT_EQ(CONTAINER_OGG, TestFile(GetTestDataFilePath("9ch.ogg")));
202 EXPECT_EQ(CONTAINER_WAV, TestFile(GetTestDataFilePath("4ch.wav")));
203 EXPECT_EQ(CONTAINER_WAV, TestFile(GetTestDataFilePath("sfx_f32le.wav")));
204 EXPECT_EQ(CONTAINER_WAV, TestFile(GetTestDataFilePath("sfx_s16le.wav")));
205 EXPECT_EQ(CONTAINER_MOV, TestFile(GetTestDataFilePath("bear-1280x720.mp4")));
206 EXPECT_EQ(CONTAINER_MOV, TestFile(GetTestDataFilePath("sfx.m4a")));
207 EXPECT_EQ(CONTAINER_WEBM, TestFile(GetTestDataFilePath("bear-320x240.webm")));
208 EXPECT_EQ(CONTAINER_MP3, TestFile(GetTestDataFilePath("id3_test.mp3")));
209 EXPECT_EQ(CONTAINER_MP3, TestFile(GetTestDataFilePath("sfx.mp3")));
210
211 // now try a few non containers
212 EXPECT_EQ(CONTAINER_UNKNOWN, TestFile(GetTestDataFilePath("ten_byte_file")));
213 EXPECT_EQ(CONTAINER_UNKNOWN, TestFile(GetTestDataFilePath("README")));
214 EXPECT_EQ(CONTAINER_UNKNOWN,
215 TestFile(GetTestDataFilePath("bali_640x360_P422.yuv")));
216 EXPECT_EQ(CONTAINER_UNKNOWN,
217 TestFile(GetTestDataFilePath("bali_640x360_RGB24.rgb")));
218 EXPECT_EQ(CONTAINER_UNKNOWN,
219 TestFile(GetTestDataFilePath("webm_vp8_track_entry")));
220 }
221
222 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698