2013-09-24 01:35:40 +00:00
|
|
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <packager/media/base/container_names.h>
|
|
|
|
|
|
|
|
#include <iterator>
|
|
|
|
|
2014-08-21 22:40:44 +00:00
|
|
|
#include <gtest/gtest.h>
|
2014-01-09 23:50:18 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <packager/media/test/test_data_util.h>
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2013-09-24 01:35:40 +00:00
|
|
|
namespace media {
|
|
|
|
|
|
|
|
// Using a macros to simplify tests. Since EXPECT_EQ outputs the second argument
|
|
|
|
// as a string when it fails, this lets the output identify what item actually
|
|
|
|
// failed.
|
2014-09-30 21:52:21 +00:00
|
|
|
#define VERIFY(buffer, name) \
|
|
|
|
EXPECT_EQ(name, \
|
|
|
|
DetermineContainer(reinterpret_cast<const uint8_t*>(buffer), \
|
2013-09-24 01:35:40 +00:00
|
|
|
sizeof(buffer)))
|
|
|
|
|
|
|
|
// Test that small buffers are handled correctly.
|
|
|
|
TEST(ContainerNamesTest, CheckSmallBuffer) {
|
|
|
|
// Empty buffer.
|
2023-12-01 17:32:19 +00:00
|
|
|
char buffer[1] = {'\0'}; // ([0] not allowed on win)
|
2013-09-24 01:35:40 +00:00
|
|
|
VERIFY(buffer, CONTAINER_UNKNOWN);
|
|
|
|
|
|
|
|
// Try a simple SRT file.
|
|
|
|
char buffer1[] =
|
|
|
|
"1\n"
|
|
|
|
"00:03:23,550 --> 00:03:24,375\n"
|
|
|
|
"You always had a hard time finding your place in this world.\n"
|
|
|
|
"\n"
|
|
|
|
"2\n"
|
|
|
|
"00:03:24,476 --> 00:03:25,175\n"
|
|
|
|
"What are you talking about?\n";
|
|
|
|
VERIFY(buffer1, CONTAINER_SRT);
|
|
|
|
|
|
|
|
// HLS has it's own loop.
|
|
|
|
char buffer2[] = "#EXTM3U"
|
|
|
|
"some other random stuff"
|
|
|
|
"#EXT-X-MEDIA-SEQUENCE:";
|
|
|
|
VERIFY(buffer2, CONTAINER_HLS);
|
|
|
|
|
|
|
|
// Try a large buffer all zeros.
|
|
|
|
char buffer3[4096];
|
|
|
|
memset(buffer3, 0, sizeof(buffer3));
|
|
|
|
VERIFY(buffer3, CONTAINER_UNKNOWN);
|
|
|
|
|
|
|
|
// Reuse buffer, but all \n this time.
|
|
|
|
memset(buffer3, '\n', sizeof(buffer3));
|
|
|
|
VERIFY(buffer3, CONTAINER_UNKNOWN);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define BYTE_ORDER_MARK "\xef\xbb\xbf"
|
|
|
|
|
|
|
|
// Note that the comparisons need at least 12 bytes, so make sure the buffer is
|
|
|
|
// at least that size.
|
|
|
|
const char kAmrBuffer[12] = "#!AMR";
|
2014-09-30 21:52:21 +00:00
|
|
|
uint8_t kAsfBuffer[] = {0x30, 0x26, 0xb2, 0x75, 0x8e, 0x66, 0xcf, 0x11,
|
|
|
|
0xa6, 0xd9, 0x00, 0xaa, 0x00, 0x62, 0xce, 0x6c};
|
2013-09-24 01:35:40 +00:00
|
|
|
const char kAss1Buffer[] = "[Script Info]";
|
|
|
|
const char kAss2Buffer[] = BYTE_ORDER_MARK "[Script Info]";
|
2014-09-30 21:52:21 +00:00
|
|
|
uint8_t kCafBuffer[] = {
|
|
|
|
'c', 'a', 'f', 'f', 0, 1, 0, 0, 'd', 'e', 's', 'c', 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
32, 64, 229, 136, 128, 0, 0, 0, 0, 'a', 'a', 'c', ' ', 0, 0, 0, 2, 0, 0,
|
|
|
|
0, 0, 0, 0, 4, 0, 0, 0, 0, 2, 0, 0, 0, 0};
|
2013-09-24 01:35:40 +00:00
|
|
|
const char kDtshdBuffer[12] = "DTSHDHDR";
|
|
|
|
const char kDxaBuffer[16] = "DEXA";
|
|
|
|
const char kFlacBuffer[12] = "fLaC";
|
2014-09-30 21:52:21 +00:00
|
|
|
uint8_t kFlvBuffer[12] = {'F', 'L', 'V', 0, 0, 0, 0, 1, 0, 0, 0, 0};
|
|
|
|
uint8_t kIrcamBuffer[] = {0x64, 0xa3, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1};
|
2013-09-24 01:35:40 +00:00
|
|
|
const char kRm1Buffer[12] = ".RMF\0\0";
|
|
|
|
const char kRm2Buffer[12] = ".ra\xfd";
|
2014-09-30 21:52:21 +00:00
|
|
|
uint8_t kWtvBuffer[] = {0xb7, 0xd8, 0x00, 0x20, 0x37, 0x49, 0xda, 0x11,
|
|
|
|
0xa6, 0x4e, 0x00, 0x07, 0xe9, 0x5e, 0xad, 0x8d};
|
|
|
|
uint8_t kBug263073Buffer[] = {
|
|
|
|
0x00, 0x00, 0x00, 0x18, 0x66, 0x74, 0x79, 0x70, 0x6d, 0x70, 0x34,
|
|
|
|
0x32, 0x00, 0x00, 0x00, 0x00, 0x69, 0x73, 0x6f, 0x6d, 0x6d, 0x70,
|
|
|
|
0x34, 0x32, 0x00, 0x00, 0x00, 0x01, 0x6d, 0x64, 0x61, 0x74, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0xaa, 0x2e, 0x22, 0xcf, 0x00, 0x00, 0x00, 0x37,
|
|
|
|
0x67, 0x64, 0x00, 0x28, 0xac, 0x2c, 0xa4, 0x01, 0xe0, 0x08, 0x9f,
|
|
|
|
0x97, 0x01, 0x52, 0x02, 0x02, 0x02, 0x80, 0x00, 0x01};
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2015-12-16 18:20:13 +00:00
|
|
|
TEST(ContainerNamesTest, FromFormatName) {
|
|
|
|
EXPECT_EQ(CONTAINER_WEBM, DetermineContainerFromFormatName("webm"));
|
|
|
|
EXPECT_EQ(CONTAINER_WEBM, DetermineContainerFromFormatName("WeBm"));
|
|
|
|
EXPECT_EQ(CONTAINER_MOV, DetermineContainerFromFormatName("m4a"));
|
|
|
|
EXPECT_EQ(CONTAINER_MOV, DetermineContainerFromFormatName("m4v"));
|
|
|
|
EXPECT_EQ(CONTAINER_MOV, DetermineContainerFromFormatName("M4v"));
|
2018-03-01 20:27:19 +00:00
|
|
|
EXPECT_EQ(CONTAINER_MOV, DetermineContainerFromFormatName("m4s"));
|
2015-12-16 18:20:13 +00:00
|
|
|
EXPECT_EQ(CONTAINER_MOV, DetermineContainerFromFormatName("mov"));
|
|
|
|
EXPECT_EQ(CONTAINER_MOV, DetermineContainerFromFormatName("mp4"));
|
|
|
|
EXPECT_EQ(CONTAINER_MOV, DetermineContainerFromFormatName("Mp4"));
|
2019-03-26 19:27:42 +00:00
|
|
|
EXPECT_EQ(CONTAINER_MOV, DetermineContainerFromFormatName("cmfa"));
|
|
|
|
EXPECT_EQ(CONTAINER_MOV, DetermineContainerFromFormatName("cmft"));
|
|
|
|
EXPECT_EQ(CONTAINER_MOV, DetermineContainerFromFormatName("cmfv"));
|
2016-04-06 08:26:48 +00:00
|
|
|
EXPECT_EQ(CONTAINER_MPEG2TS, DetermineContainerFromFormatName("ts"));
|
|
|
|
EXPECT_EQ(CONTAINER_MPEG2TS, DetermineContainerFromFormatName("mpeg2ts"));
|
2015-12-16 18:20:13 +00:00
|
|
|
EXPECT_EQ(CONTAINER_UNKNOWN, DetermineContainerFromFormatName("cat"));
|
|
|
|
EXPECT_EQ(CONTAINER_UNKNOWN, DetermineContainerFromFormatName("amp4"));
|
|
|
|
EXPECT_EQ(CONTAINER_UNKNOWN, DetermineContainerFromFormatName(" mp4"));
|
|
|
|
EXPECT_EQ(CONTAINER_UNKNOWN, DetermineContainerFromFormatName(""));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ContainerNamesTest, FromFileName) {
|
|
|
|
EXPECT_EQ(CONTAINER_WEBM, DetermineContainerFromFileName("test.webm"));
|
|
|
|
EXPECT_EQ(CONTAINER_WEBM, DetermineContainerFromFileName("another.wEbM"));
|
|
|
|
EXPECT_EQ(CONTAINER_MOV, DetermineContainerFromFileName("test.m4a"));
|
|
|
|
EXPECT_EQ(CONTAINER_MOV, DetermineContainerFromFileName("file.m4v"));
|
|
|
|
EXPECT_EQ(CONTAINER_MOV, DetermineContainerFromFileName("a file .m4V"));
|
2018-03-01 20:27:19 +00:00
|
|
|
EXPECT_EQ(CONTAINER_MOV, DetermineContainerFromFileName("segment.m4s"));
|
2015-12-16 18:20:13 +00:00
|
|
|
EXPECT_EQ(CONTAINER_MOV, DetermineContainerFromFileName("2_more-files.mp4"));
|
|
|
|
EXPECT_EQ(CONTAINER_MOV, DetermineContainerFromFileName("foo.bar.MP4"));
|
2016-04-06 08:26:48 +00:00
|
|
|
EXPECT_EQ(CONTAINER_MPEG2TS, DetermineContainerFromFileName("a.ts"));
|
|
|
|
EXPECT_EQ(CONTAINER_MPEG2TS, DetermineContainerFromFileName("a.TS"));
|
2015-12-16 18:20:13 +00:00
|
|
|
EXPECT_EQ(CONTAINER_UNKNOWN, DetermineContainerFromFileName("a_bad.gif"));
|
|
|
|
EXPECT_EQ(CONTAINER_UNKNOWN, DetermineContainerFromFileName("a bad.m4v-"));
|
|
|
|
EXPECT_EQ(CONTAINER_UNKNOWN, DetermineContainerFromFileName("a.m4v."));
|
|
|
|
EXPECT_EQ(CONTAINER_UNKNOWN, DetermineContainerFromFileName(""));
|
|
|
|
}
|
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
// Test that containers that start with fixed strings are handled correctly.
|
|
|
|
// This is to verify that the TAG matches the first 4 characters of the string.
|
|
|
|
TEST(ContainerNamesTest, CheckFixedStrings) {
|
|
|
|
VERIFY(kAmrBuffer, CONTAINER_AMR);
|
|
|
|
VERIFY(kAsfBuffer, CONTAINER_ASF);
|
|
|
|
VERIFY(kAss1Buffer, CONTAINER_ASS);
|
|
|
|
VERIFY(kAss2Buffer, CONTAINER_ASS);
|
|
|
|
VERIFY(kCafBuffer, CONTAINER_CAF);
|
|
|
|
VERIFY(kDtshdBuffer, CONTAINER_DTSHD);
|
|
|
|
VERIFY(kDxaBuffer, CONTAINER_DXA);
|
|
|
|
VERIFY(kFlacBuffer, CONTAINER_FLAC);
|
|
|
|
VERIFY(kFlvBuffer, CONTAINER_FLV);
|
|
|
|
VERIFY(kIrcamBuffer, CONTAINER_IRCAM);
|
|
|
|
VERIFY(kRm1Buffer, CONTAINER_RM);
|
|
|
|
VERIFY(kRm2Buffer, CONTAINER_RM);
|
|
|
|
VERIFY(kWtvBuffer, CONTAINER_WTV);
|
|
|
|
VERIFY(kBug263073Buffer, CONTAINER_MOV);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the container type of a specified file.
|
2023-12-01 17:32:19 +00:00
|
|
|
void TestFile(MediaContainerName expected, const std::string& name) {
|
|
|
|
auto path = GetTestDataFilePath(name);
|
|
|
|
std::vector<uint8_t> data = ReadTestDataFile(name);
|
|
|
|
ASSERT_FALSE(data.empty());
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
// Now verify the type.
|
2023-12-01 17:32:19 +00:00
|
|
|
EXPECT_EQ(expected, DetermineContainer(data.data(), data.size()))
|
|
|
|
<< "Failure with file " << path;
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
2015-11-16 23:47:39 +00:00
|
|
|
TEST(ContainerNamesTest, Ttml) {
|
|
|
|
// One of the actual TTMLs from the TTML spec page.
|
|
|
|
const char kTtml[] =
|
|
|
|
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
|
|
|
|
"<tt xml:lang=\"en\" xmlns=\"http://www.w3.org/ns/ttml\">\n"
|
|
|
|
" <body>\n"
|
|
|
|
" <div>\n"
|
|
|
|
" <p dur=\"10s\">\n"
|
|
|
|
" Some subtitle.\n"
|
|
|
|
" </p>\n"
|
|
|
|
" </div>\n"
|
|
|
|
" </body>\n"
|
|
|
|
"</tt>\n";
|
|
|
|
|
|
|
|
EXPECT_EQ(CONTAINER_TTML,
|
|
|
|
DetermineContainer(reinterpret_cast<const uint8_t*>(kTtml),
|
2023-12-01 17:32:19 +00:00
|
|
|
std::size(kTtml)));
|
2015-11-16 23:47:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ContainerNamesTest, WebVtt) {
|
|
|
|
const char kWebVtt[] =
|
|
|
|
"WEBVTT\n"
|
|
|
|
"\n"
|
|
|
|
"00:1.000 --> 00:2.000\n"
|
|
|
|
"Subtitle";
|
|
|
|
EXPECT_EQ(CONTAINER_WEBVTT,
|
|
|
|
DetermineContainer(reinterpret_cast<const uint8_t*>(kWebVtt),
|
2023-12-01 17:32:19 +00:00
|
|
|
std::size(kWebVtt)));
|
2015-11-16 23:47:39 +00:00
|
|
|
|
|
|
|
const uint8_t kUtf8ByteOrderMark[] = {0xef, 0xbb, 0xbf};
|
|
|
|
std::vector<uint8_t> webvtt_with_utf8_byte_order_mark(
|
2023-12-01 17:32:19 +00:00
|
|
|
kUtf8ByteOrderMark, kUtf8ByteOrderMark + std::size(kUtf8ByteOrderMark));
|
2015-11-16 23:47:39 +00:00
|
|
|
webvtt_with_utf8_byte_order_mark.insert(
|
|
|
|
webvtt_with_utf8_byte_order_mark.end(), kWebVtt,
|
2023-12-01 17:32:19 +00:00
|
|
|
kWebVtt + std::size(kWebVtt));
|
2015-11-16 23:47:39 +00:00
|
|
|
|
2016-01-29 21:23:12 +00:00
|
|
|
EXPECT_EQ(CONTAINER_WEBVTT,
|
2016-11-09 02:11:13 +00:00
|
|
|
DetermineContainer(
|
|
|
|
webvtt_with_utf8_byte_order_mark.data(),
|
|
|
|
static_cast<int>(webvtt_with_utf8_byte_order_mark.size())));
|
2015-11-16 23:47:39 +00:00
|
|
|
}
|
|
|
|
|
2013-09-24 01:35:40 +00:00
|
|
|
TEST(ContainerNamesTest, FileCheckOGG) {
|
2023-12-01 17:32:19 +00:00
|
|
|
TestFile(CONTAINER_OGG, "bear.ogv");
|
|
|
|
TestFile(CONTAINER_OGG, "9ch.ogg");
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ContainerNamesTest, FileCheckWAV) {
|
2023-12-01 17:32:19 +00:00
|
|
|
TestFile(CONTAINER_WAV, "4ch.wav");
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ContainerNamesTest, FileCheckMOV) {
|
2023-12-01 17:32:19 +00:00
|
|
|
TestFile(CONTAINER_MOV, "bear-640x360.mp4");
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ContainerNamesTest, FileCheckWEBM) {
|
2023-12-01 17:32:19 +00:00
|
|
|
TestFile(CONTAINER_WEBM, "bear-640x360.webm");
|
|
|
|
TestFile(CONTAINER_WEBM, "no_streams.webm");
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ContainerNamesTest, FileCheckMP3) {
|
2023-12-01 17:32:19 +00:00
|
|
|
TestFile(CONTAINER_MP3, "id3_test.mp3");
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ContainerNamesTest, FileCheckAC3) {
|
2023-12-01 17:32:19 +00:00
|
|
|
TestFile(CONTAINER_AC3, "bear.ac3");
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ContainerNamesTest, FileCheckAAC) {
|
2023-12-01 17:32:19 +00:00
|
|
|
TestFile(CONTAINER_AAC, "bear.adts");
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ContainerNamesTest, FileCheckAIFF) {
|
2023-12-01 17:32:19 +00:00
|
|
|
TestFile(CONTAINER_AIFF, "bear.aiff");
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ContainerNamesTest, FileCheckASF) {
|
2023-12-01 17:32:19 +00:00
|
|
|
TestFile(CONTAINER_ASF, "bear.asf");
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ContainerNamesTest, FileCheckAVI) {
|
2023-12-01 17:32:19 +00:00
|
|
|
TestFile(CONTAINER_AVI, "bear.avi");
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ContainerNamesTest, FileCheckEAC3) {
|
2023-12-01 17:32:19 +00:00
|
|
|
TestFile(CONTAINER_EAC3, "bear.eac3");
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ContainerNamesTest, FileCheckFLAC) {
|
2023-12-01 17:32:19 +00:00
|
|
|
TestFile(CONTAINER_FLAC, "bear.flac");
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ContainerNamesTest, FileCheckFLV) {
|
2023-12-01 17:32:19 +00:00
|
|
|
TestFile(CONTAINER_FLV, "bear.flv");
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ContainerNamesTest, FileCheckH261) {
|
2023-12-01 17:32:19 +00:00
|
|
|
TestFile(CONTAINER_H261, "bear.h261");
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ContainerNamesTest, FileCheckH263) {
|
2023-12-01 17:32:19 +00:00
|
|
|
TestFile(CONTAINER_H263, "bear.h263");
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ContainerNamesTest, FileCheckMJPEG) {
|
2023-12-01 17:32:19 +00:00
|
|
|
TestFile(CONTAINER_MJPEG, "bear.mjpeg");
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ContainerNamesTest, FileCheckMPEG2PS) {
|
2023-12-01 17:32:19 +00:00
|
|
|
TestFile(CONTAINER_MPEG2PS, "bear.mpeg");
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ContainerNamesTest, FileCheckMPEG2TS) {
|
2023-12-01 17:32:19 +00:00
|
|
|
TestFile(CONTAINER_MPEG2TS, "bear.m2ts");
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ContainerNamesTest, FileCheckRM) {
|
2023-12-01 17:32:19 +00:00
|
|
|
TestFile(CONTAINER_RM, "bear.rm");
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ContainerNamesTest, FileCheckSWF) {
|
2023-12-01 17:32:19 +00:00
|
|
|
TestFile(CONTAINER_SWF, "bear.swf");
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try a few non containers.
|
|
|
|
TEST(ContainerNamesTest, FileCheckUNKNOWN) {
|
2023-12-01 17:32:19 +00:00
|
|
|
TestFile(CONTAINER_UNKNOWN, "ten_byte_file");
|
|
|
|
TestFile(CONTAINER_UNKNOWN, "README");
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|