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

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 // Test that the names are in the correct sorted order.
13 TEST(ContainerNamesTest, CheckSortOrder) {
14 ASSERT_GT(ContainerNames::ContainerNameMappingSize(), 0u);
15 for (size_t i = 1; i < ContainerNames::ContainerNameMappingSize(); ++i) {
xhwang 2013/05/01 01:49:11 s/size_t/int
jrummell 2013/05/02 21:53:09 Done.
16 ASSERT_LT(strcasecmp(ContainerNames::container_name_mapping[i - 1].name,
17 ContainerNames::container_name_mapping[i].name),
18 0)
19 << ContainerNames::container_name_mapping[i - 1].name << " and "
20 << ContainerNames::container_name_mapping[i].name
21 << " are in the wrong order.";
22 }
23 }
24
25 // Test that string lookup works for all names in the list.
26 TEST(ContainerNamesTest, NameLookup) {
27 // Search for all strings.
28 for (size_t i = 0; i < ContainerNames::ContainerNameMappingSize(); ++i) {
29 const char* name = ContainerNames::container_name_mapping[i].name;
30 ASSERT_EQ(ContainerNames::container_name_mapping[i].id,
31 ContainerNames::LookupContainer(name)) << "Unable to lookup "
32 << name;
33 }
34
35 // Check that some name not in the list fails.
36 ASSERT_EQ(CONTAINER_UNKNOWN, ContainerNames::LookupContainer("foo"));
37 }
38
39 #define BYTE_ORDER_MARK "\xef\xbb\xbf"
40
41 // Test that small buffers are handled correctly.
42 TEST(ContainerNamesTest, CheckSmallBufferSize) {
43 // Empty buffer.
44 uint8_t buffer[0];
45 EXPECT_EQ(CONTAINER_UNKNOWN, ContainerNames::DetermineContainer(buffer, 0));
46
47 // Try a simple string
48 const char buffer1[] = "******** START SCRIPT ********";
49 EXPECT_EQ(
50 CONTAINER_SUBVIEWER1,
51 ContainerNames::DetermineContainer((uint8_t*)buffer1, strlen(buffer1)));
52
53 // Try adding byte order mark at beginning.
54 const char buffer2[] = BYTE_ORDER_MARK "Interplay MVE File\x1A\x00\x1A";
55 EXPECT_EQ(CONTAINER_IPMOVIE,
56 ContainerNames::DetermineContainer((uint8_t*)buffer2, 24));
57 // Repeat but leave out a character (so it won't match)
58 EXPECT_EQ(CONTAINER_UNKNOWN,
59 ContainerNames::DetermineContainer((uint8_t*)buffer2, 23));
60
61 // Try a simple SRT file.
62 const char buffer3[] =
63 "1\n"
64 "00:03:23,550 --> 00:03:24,375\n"
65 "You always had a hard time finding your place in this world.\n"
66 "\n"
67 "2\n"
68 "00:03:24,476 --> 00:03:25,175\n"
69 "What are you talking about?\n";
70 EXPECT_EQ(
71 CONTAINER_SRT,
72 ContainerNames::DetermineContainer((uint8_t*)buffer3, strlen(buffer3)));
xhwang 2013/05/01 01:49:11 Write a helper function to do this EXPECT_EQ()?
jrummell 2013/05/02 21:53:09 Done.
73
74 // Try a simple tag. Note that the tag checking requires
75 // at least 128 characters.
76 const char buffer4[256] = ".snd";
77 EXPECT_EQ(
78 CONTAINER_AU,
79 ContainerNames::DetermineContainer((uint8_t*)buffer4, sizeof(buffer4)));
80
81 // HLS has it's own loop, so try it
82 const char buffer5[256] = "#EXTM3U"
83 "some other random stuff"
84 "#EXT-X-MEDIA-SEQUENCE:";
85 EXPECT_EQ(
86 CONTAINER_HLS,
87 ContainerNames::DetermineContainer((uint8_t*)buffer5, sizeof(buffer5)));
88
89 // PJS has several loops, so try it
90 const char buffer6[256] = "1234567890,123456,0\n"
91 "some filler\n"
92 "\"quoted string\"\n";
93 EXPECT_EQ(
94 CONTAINER_PJS,
95 ContainerNames::DetermineContainer((uint8_t*)buffer6, sizeof(buffer6)));
96
97 // try a large buffer all 0
98 char buffer7[4096];
99 memset(buffer7, 0, sizeof(buffer7));
100 EXPECT_EQ(
101 CONTAINER_UNKNOWN,
102 ContainerNames::DetermineContainer((uint8_t*)buffer7, sizeof(buffer7)));
103
104 // reuse buffer, but all \n this time
105 memset(buffer7, '\n', sizeof(buffer7));
106 EXPECT_EQ(
107 CONTAINER_UNKNOWN,
108 ContainerNames::DetermineContainer((uint8_t*)buffer7, sizeof(buffer7)));
109 }
110
111 struct Mapping {
112 enum FFmpegContainerName id;
xhwang 2013/05/01 01:49:11 drop enum
jrummell 2013/05/02 21:53:09 Done.
113 int length;
114 const char* name;
115 };
116
117 // The following are the fixed strings compared in ContainerNames.
118 // Since the first 4 characters are used as a TAG, this checks that the
119 // TAG is defined correctly.
120 const Mapping fixed_strings[] = {
121 { CONTAINER_AMR, 0, "#!AMR" },
122 { CONTAINER_APC, 0, "CRYO_APC" },
123 { CONTAINER_AQTITLE, 0, "-->> 23" },
124 { CONTAINER_ASF, 16, "\x30\x26\xb2\x75\x8e\x66\xcf\x11\xa6\xd9\x00"
125 "\xaa\x00\x62\xce\x6c" },
126 { CONTAINER_ASS, 0, "[Script Info]" },
127 { CONTAINER_ASS, 0, BYTE_ORDER_MARK "[Script Info]" },
128 { CONTAINER_CONCAT, 0, "ffconcat version 1.0" },
129 { CONTAINER_DNXHD, 43, "\x00\x00\x02\x80\x01 789*123456789*123456789"
130 "*123456789*\x04\xd3" },
131 { CONTAINER_FFMETADATA, 0, ";FFMETADATA" },
132 { CONTAINER_IDF, 12, "\x04\x31\x2e\x34\x00\x00\x00\x00\x4f\x00\x15"
133 "\x00" },
134 { CONTAINER_ILBC, 0, "#!iLBC" },
135 { CONTAINER_ISS, 0, "IMA_ADPCM_Sound" },
136 { CONTAINER_IV8, 0, "\x01\x01\x03\xb8\x80\x60" },
137 { CONTAINER_JV, 0, "JVxx Compression by John M Phillips Copyright "
138 "(C) 1995 The Bitmap Brothers Ltd." },
139 { CONTAINER_LIBNUT, 0, "nut/multimedia container" },
140 { CONTAINER_LXF, 8, "LEITCH\x00\x00" },
141 { CONTAINER_NUV, 0, "NuppelVideo" },
142 { CONTAINER_NUV, 0, "MythTVVideo" },
143 { CONTAINER_PAF, 0, "Packed Animation File V1.0\n(c) 1992-96 "
144 "Amazing Studio\x0a\x1a" },
145 { CONTAINER_REALTEXT, 0, "<window" },
146 { CONTAINER_REALTEXT, 0, BYTE_ORDER_MARK "<window" },
147 { CONTAINER_RPL, 0, "ARMovie\x0A" },
148 { CONTAINER_SAMI, 0, "<SAMI>" },
149 { CONTAINER_SAMI, 0, BYTE_ORDER_MARK "<SAMI>" },
150 { CONTAINER_SMJPEG, 8, "\x00\x0aSMJPEG" },
151 { CONTAINER_VOBSUB, 0, "# VobSub index file," },
152 { CONTAINER_VOC, 0, "Creative Voice File\x1A" },
153 { CONTAINER_W64, 42, "riff\x2e\x91\xcf\x11\xa5\xd6\x28\xdb\x04\xc1"
154 "\x00\x00 89*1234wave\xf3\xac\xd3\x11\x8c\xd1"
155 "\x00\xc0\x4f\x8e\xdb\x8a" },
156 { CONTAINER_WEBVTT, 0, "WEBVTT" },
157 { CONTAINER_WEBVTT, 0, BYTE_ORDER_MARK "WEBVTT" },
158 { CONTAINER_WTV, 16, "\xb7\xd8\x00\x20\x37\x49\xda\x11\xa6\x4e\x00"
159 "\x07\xe9\x5e\xad\x8d" },
160 { CONTAINER_YUV4MPEGPIPE, 0, "YUV4MPEG2" },
161 { CONTAINER_UNKNOWN, 0, "" }
162 };
163
164 // Test that containers that start with fixed strings are handled correctly.
165 // This is to verify that the TAG matches the first 4 characters of the string.
166 TEST(ContainerNamesTest, CheckFixedStrings) {
167 int index = 0;
168 while (fixed_strings[index].id != CONTAINER_UNKNOWN) {
xhwang 2013/05/01 01:49:11 why not use a for loop?
jrummell 2013/05/02 21:53:09 Done.
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(
178 fixed_strings[index].id,
179 ContainerNames::DetermineContainer((uint8_t*)buffer, sizeof(buffer)));
xhwang 2013/05/01 01:49:11 sizeof(buffer) is always 256. is this what we want
jrummell 2013/05/02 21:53:09 Yes. Some of the tests need at least 128 character
180 ++index;
181 }
182 }
183
184 // Determine the container type of a specified file.
185 FFmpegContainerName TestFile(const base::FilePath& filename) {
186 // Open the file.
187 FILE* f = fopen(filename.value().c_str(), "r");
188 EXPECT_NE(f, (FILE*)NULL);
189
190 // Read the first few bytes.
191 uint8_t buffer[8192];
192 size_t read = fread(buffer, sizeof(uint8_t), sizeof(buffer), f);
xhwang 2013/05/01 01:49:11 Use ReadFile? https://code.google.com/p/chromium/
jrummell 2013/05/02 21:53:09 I don't want the whole file ... just the first 8K
xhwang 2013/05/03 19:59:06 In that case, use ReadFile()? https://code.google.
jrummell 2013/05/03 23:56:20 Done.
193
194 // Now determine the type.
195 return ContainerNames::DetermineContainer(buffer, read);
196 }
197
198 // Test several files to ensure that the container is detected properly.
199 TEST(ContainerNamesTest, FileCheck) {
xhwang 2013/05/01 01:49:11 Nice test!
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