2013-09-24 01:35:40 +00:00
|
|
|
// Copyright (c) 2012 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-10-11 08:49:50 +00:00
|
|
|
#include <packager/media/formats/mp4/box_buffer.h>
|
2013-09-24 01:35:40 +00:00
|
|
|
|
2023-10-11 08:49:50 +00:00
|
|
|
#include <cstdint>
|
|
|
|
#include <cstring>
|
2016-08-17 17:41:40 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2023-10-13 19:42:47 +00:00
|
|
|
#include <absl/log/check.h>
|
|
|
|
#include <absl/log/log.h>
|
2023-10-11 08:49:50 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2023-10-14 16:36:01 +00:00
|
|
|
#include <packager/macros/logging.h>
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/media/base/rcheck.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 {
|
|
|
|
namespace mp4 {
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
static const uint8_t kSkipBox[] = {
|
2013-11-22 21:24:25 +00:00
|
|
|
// Top-level test box containing three children.
|
|
|
|
0x00, 0x00, 0x00, 0x40, 's', 'k', 'i', 'p', 0x01, 0x02, 0x03, 0x04,
|
|
|
|
0x05, 0x06, 0x07, 0x08, 0xf9, 0x0a, 0x0b, 0x0c, 0xfd, 0x0e, 0x0f, 0x10,
|
|
|
|
// Ordinary (8-byte header) child box.
|
|
|
|
0x00, 0x00, 0x00, 0x0c, 'p', 's', 's', 'h', 0xde, 0xad, 0xbe, 0xef,
|
|
|
|
// Extended-size header child box.
|
|
|
|
0x00, 0x00, 0x00, 0x01, 'p', 's', 's', 'h', 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x14, 0xfa, 0xce, 0xca, 0xfe,
|
|
|
|
// Empty free box.
|
|
|
|
0x00, 0x00, 0x00, 0x08, 'f', 'r', 'e', 'e',
|
|
|
|
// Trailing garbage.
|
|
|
|
0x00};
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
struct FreeBox : Box {
|
2016-04-06 23:21:45 +00:00
|
|
|
FourCC BoxType() const override { return FOURCC_free; }
|
2015-12-15 00:07:51 +00:00
|
|
|
bool ReadWriteInternal(BoxBuffer* buffer) override {
|
2013-09-24 01:35:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-11-09 02:11:13 +00:00
|
|
|
size_t ComputeSizeInternal() override {
|
2013-11-22 21:24:25 +00:00
|
|
|
NOTIMPLEMENTED();
|
|
|
|
return 0;
|
|
|
|
}
|
2013-09-24 01:35:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct PsshBox : Box {
|
2016-04-06 23:21:45 +00:00
|
|
|
FourCC BoxType() const override { return FOURCC_pssh; }
|
2015-12-15 00:07:51 +00:00
|
|
|
bool ReadWriteInternal(BoxBuffer* buffer) override {
|
2013-11-22 21:24:25 +00:00
|
|
|
return buffer->ReadWriteUInt32(&val);
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
2016-11-09 02:11:13 +00:00
|
|
|
size_t ComputeSizeInternal() override {
|
2013-11-22 21:24:25 +00:00
|
|
|
NOTIMPLEMENTED();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint32_t val;
|
2013-09-24 01:35:40 +00:00
|
|
|
};
|
|
|
|
|
2013-11-22 21:24:25 +00:00
|
|
|
struct SkipBox : FullBox {
|
2016-04-06 23:21:45 +00:00
|
|
|
FourCC BoxType() const override { return FOURCC_skip; }
|
2015-12-15 00:07:51 +00:00
|
|
|
bool ReadWriteInternal(BoxBuffer* buffer) override {
|
|
|
|
RCHECK(ReadWriteHeaderInternal(buffer) && buffer->ReadWriteUInt8(&a) &&
|
2014-09-30 21:52:21 +00:00
|
|
|
buffer->ReadWriteUInt8(&b) && buffer->ReadWriteUInt16(&c) &&
|
2013-11-22 21:24:25 +00:00
|
|
|
buffer->ReadWriteInt32(&d) &&
|
2014-09-30 21:52:21 +00:00
|
|
|
buffer->ReadWriteInt64NBytes(&e, sizeof(uint32_t)));
|
2013-11-22 21:24:25 +00:00
|
|
|
RCHECK(buffer->PrepareChildren());
|
|
|
|
if (buffer->Reading()) {
|
|
|
|
DCHECK(buffer->reader());
|
|
|
|
RCHECK(buffer->reader()->ReadChildren(&kids));
|
|
|
|
} else {
|
|
|
|
NOTIMPLEMENTED();
|
|
|
|
}
|
|
|
|
return buffer->TryReadWriteChild(&empty);
|
|
|
|
}
|
2016-11-09 02:11:13 +00:00
|
|
|
size_t ComputeSizeInternal() override {
|
2013-11-22 21:24:25 +00:00
|
|
|
NOTIMPLEMENTED();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
uint8_t a, b;
|
|
|
|
uint16_t c;
|
|
|
|
int32_t d;
|
|
|
|
int64_t e;
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
std::vector<PsshBox> kids;
|
2013-11-22 21:24:25 +00:00
|
|
|
FreeBox empty;
|
2013-09-24 01:35:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class BoxReaderTest : public testing::Test {
|
|
|
|
protected:
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> GetBuf() {
|
|
|
|
return std::vector<uint8_t>(kSkipBox, kSkipBox + sizeof(kSkipBox));
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(BoxReaderTest, ExpectedOperationTest) {
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> buf = GetBuf();
|
2013-09-24 01:35:40 +00:00
|
|
|
bool err;
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<BoxReader> reader(
|
|
|
|
BoxReader::ReadBox(&buf[0], buf.size(), &err));
|
2013-09-24 01:35:40 +00:00
|
|
|
EXPECT_FALSE(err);
|
|
|
|
EXPECT_TRUE(reader.get());
|
|
|
|
|
|
|
|
SkipBox box;
|
|
|
|
EXPECT_TRUE(box.Parse(reader.get()));
|
2013-11-22 21:24:25 +00:00
|
|
|
EXPECT_EQ(0x01, box.version);
|
|
|
|
EXPECT_EQ(0x020304u, box.flags);
|
2013-09-24 01:35:40 +00:00
|
|
|
EXPECT_EQ(0x05, box.a);
|
|
|
|
EXPECT_EQ(0x06, box.b);
|
|
|
|
EXPECT_EQ(0x0708, box.c);
|
2014-09-30 21:52:21 +00:00
|
|
|
EXPECT_EQ(static_cast<int32_t>(0xf90a0b0c), box.d);
|
|
|
|
EXPECT_EQ(static_cast<int32_t>(0xfd0e0f10), box.e);
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
EXPECT_EQ(2u, box.kids.size());
|
|
|
|
EXPECT_EQ(0xdeadbeef, box.kids[0].val);
|
|
|
|
EXPECT_EQ(0xfacecafe, box.kids[1].val);
|
|
|
|
|
2013-11-22 21:24:25 +00:00
|
|
|
// Accounting for the extra byte outside of the box above.
|
2014-09-30 21:52:21 +00:00
|
|
|
EXPECT_EQ(buf.size(), static_cast<uint64_t>(reader->size() + 1));
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BoxReaderTest, OuterTooShortTest) {
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> buf = GetBuf();
|
2013-09-24 01:35:40 +00:00
|
|
|
bool err;
|
|
|
|
|
|
|
|
// Create a soft failure by truncating the outer box.
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<BoxReader> r(
|
|
|
|
BoxReader::ReadBox(&buf[0], buf.size() - 2, &err));
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
EXPECT_FALSE(err);
|
|
|
|
EXPECT_FALSE(r.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BoxReaderTest, InnerTooLongTest) {
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> buf = GetBuf();
|
2013-09-24 01:35:40 +00:00
|
|
|
bool err;
|
|
|
|
|
|
|
|
// Make an inner box too big for its outer box.
|
|
|
|
buf[25] = 1;
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<BoxReader> reader(
|
|
|
|
BoxReader::ReadBox(&buf[0], buf.size(), &err));
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
SkipBox box;
|
|
|
|
EXPECT_FALSE(box.Parse(reader.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BoxReaderTest, ScanChildrenTest) {
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> buf = GetBuf();
|
2013-09-24 01:35:40 +00:00
|
|
|
bool err;
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<BoxReader> reader(
|
|
|
|
BoxReader::ReadBox(&buf[0], buf.size(), &err));
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
EXPECT_TRUE(reader->SkipBytes(16) && reader->ScanChildren());
|
|
|
|
|
|
|
|
FreeBox free;
|
|
|
|
EXPECT_TRUE(reader->ReadChild(&free));
|
|
|
|
EXPECT_FALSE(reader->ReadChild(&free));
|
2013-11-22 21:24:25 +00:00
|
|
|
EXPECT_TRUE(reader->TryReadChild(&free));
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
std::vector<PsshBox> kids;
|
|
|
|
|
|
|
|
EXPECT_TRUE(reader->ReadChildren(&kids));
|
|
|
|
EXPECT_EQ(2u, kids.size());
|
|
|
|
kids.clear();
|
|
|
|
EXPECT_FALSE(reader->ReadChildren(&kids));
|
2013-11-22 21:24:25 +00:00
|
|
|
EXPECT_TRUE(reader->TryReadChildren(&kids));
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BoxReaderTest, ReadAllChildrenTest) {
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> buf = GetBuf();
|
2013-11-22 21:24:25 +00:00
|
|
|
// Modify buffer to exclude its last 'free' box.
|
2013-09-24 01:35:40 +00:00
|
|
|
buf[3] = 0x38;
|
|
|
|
bool err;
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<BoxReader> reader(
|
|
|
|
BoxReader::ReadBox(&buf[0], buf.size(), &err));
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
std::vector<PsshBox> kids;
|
|
|
|
EXPECT_TRUE(reader->SkipBytes(16) && reader->ReadAllChildren(&kids));
|
|
|
|
EXPECT_EQ(2u, kids.size());
|
2013-11-22 21:24:25 +00:00
|
|
|
EXPECT_EQ(kids[0].val, 0xdeadbeef); // Ensure order is preserved.
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BoxReaderTest, SkippingBloc) {
|
2016-05-27 21:38:14 +00:00
|
|
|
static const uint8_t kData[] = {
|
|
|
|
0x00, 0x00, 0x00, 0x09, // Box size.
|
|
|
|
'b', 'l', 'o', 'c', // FourCC.
|
|
|
|
0x00, // Reserved byte.
|
|
|
|
};
|
2014-09-30 21:52:21 +00:00
|
|
|
|
|
|
|
std::vector<uint8_t> buf(kData, kData + sizeof(kData));
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
bool err;
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<BoxReader> reader(
|
|
|
|
BoxReader::ReadBox(&buf[0], buf.size(), &err));
|
2013-09-24 01:35:40 +00:00
|
|
|
|
|
|
|
EXPECT_FALSE(err);
|
|
|
|
EXPECT_TRUE(reader);
|
2016-04-06 23:21:45 +00:00
|
|
|
EXPECT_EQ(FOURCC_bloc, reader->type());
|
2013-09-24 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mp4
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|