5 #include <packager/media/formats/mp4/box_reader.h>
11 #include <absl/log/check.h>
12 #include <absl/log/log.h>
13 #include <absl/strings/str_format.h>
15 #include <packager/macros/logging.h>
16 #include <packager/media/formats/mp4/box.h>
22 BoxReader::BoxReader(
const uint8_t* buf,
size_t size)
23 : BufferReader(buf, size), type_(FOURCC_NULL), scanned_(false) {
28 BoxReader::~BoxReader() {
29 if (scanned_ && !children_.empty()) {
30 for (ChildMap::iterator itr = children_.begin(); itr != children_.end();
32 DVLOG(1) <<
"Skipping unknown box: " << FourCCToString(itr->first);
39 const size_t buf_size,
41 std::unique_ptr<BoxReader> reader(
new BoxReader(buf, buf_size));
42 if (!reader->ReadHeader(err))
46 if (reader->type() == FOURCC_mdat)
47 return reader.release();
49 if (reader->size() <= buf_size)
50 return reader.release();
56 bool BoxReader::StartBox(
const uint8_t* buf,
57 const size_t buf_size,
62 if (!reader.ReadHeader(err))
64 *type = reader.type();
65 *box_size = reader.size();
69 bool BoxReader::ScanChildren() {
73 while (pos() < size()) {
74 std::unique_ptr<BoxReader> child(
75 new BoxReader(&data()[pos()], size() - pos()));
77 if (!child->ReadHeader(&err))
80 FourCC box_type = child->type();
81 size_t box_size = child->size();
82 children_.insert(std::pair<FourCC, std::unique_ptr<BoxReader>>(
83 box_type, std::move(child)));
84 VLOG(2) <<
"Child " << FourCCToString(box_type) <<
" size 0x" << std::hex
85 << box_size << std::dec;
86 RCHECK(SkipBytes(box_size));
92 bool BoxReader::ReadChild(
Box* child) {
94 FourCC child_type = child->
BoxType();
96 ChildMap::iterator itr = children_.find(child_type);
97 RCHECK(itr != children_.end());
98 DVLOG(2) <<
"Found a " << FourCCToString(child_type) <<
" box.";
99 RCHECK(child->
Parse(itr->second.get()));
100 children_.erase(itr);
104 bool BoxReader::ChildExist(
Box* child) {
105 return children_.count(child->
BoxType()) > 0;
108 bool BoxReader::TryReadChild(
Box* child) {
109 if (!children_.count(child->
BoxType()))
111 return ReadChild(child);
114 bool BoxReader::ReadHeader(
bool* err) {
118 if (!ReadNBytesInto8(&size,
sizeof(uint32_t)) || !ReadFourCC(&type_))
123 NOTIMPLEMENTED() << absl::StrFormat(
"Box '%s' run to EOS.",
124 FourCCToString(type_).c_str());
127 }
else if (size == 1) {
134 LOG(ERROR) << absl::StrFormat(
"Box '%s' with size (%" PRIu64
136 FourCCToString(type_).c_str(), size);
142 if (size >
static_cast<uint64_t
>(std::numeric_limits<int32_t>::max()) &&
143 type_ != FOURCC_mdat) {
144 LOG(ERROR) << absl::StrFormat(
"Box '%s' size (%" PRIu64
") is too large.",
145 FourCCToString(type_).c_str(), size);
All the methods that are virtual are virtual for mocking.