5 #include "packager/media/formats/mp4/box_reader.h"
12 #include "packager/base/logging.h"
13 #include "packager/base/strings/stringprintf.h"
14 #include "packager/media/formats/mp4/box.h"
20 BoxReader::BoxReader(
const uint8_t* buf,
size_t size)
21 : BufferReader(buf, size), type_(FOURCC_NULL), scanned_(false) {
26 BoxReader::~BoxReader() {
27 if (scanned_ && !children_.empty()) {
28 for (ChildMap::iterator itr = children_.begin(); itr != children_.end();
30 DVLOG(1) <<
"Skipping unknown box: " << FourCCToString(itr->first);
37 const size_t buf_size,
39 std::unique_ptr<BoxReader> reader(
new BoxReader(buf, buf_size));
40 if (!reader->ReadHeader(err))
44 if (reader->type() == FOURCC_mdat)
45 return reader.release();
47 if (reader->size() <= buf_size)
48 return reader.release();
55 const size_t buf_size,
60 if (!reader.ReadHeader(err))
62 *type = reader.type();
63 *box_size = reader.size();
71 while (pos() < size()) {
72 std::unique_ptr<BoxReader> child(
73 new BoxReader(&data()[pos()], size() - pos()));
75 if (!child->ReadHeader(&err))
78 FourCC box_type = child->type();
79 size_t box_size = child->size();
80 children_.insert(std::pair<FourCC, std::unique_ptr<BoxReader>>(
81 box_type, std::move(child)));
82 VLOG(2) <<
"Child " << FourCCToString(box_type) <<
" size 0x" << std::hex
83 << box_size << std::dec;
92 FourCC child_type = child->
BoxType();
94 ChildMap::iterator itr = children_.find(child_type);
95 RCHECK(itr != children_.end());
96 DVLOG(2) <<
"Found a " << FourCCToString(child_type) <<
" box.";
97 RCHECK(child->
Parse(itr->second.get()));
103 return children_.count(child->
BoxType()) > 0;
107 if (!children_.count(child->
BoxType()))
112 bool BoxReader::ReadHeader(
bool* err) {
121 NOTIMPLEMENTED() << base::StringPrintf(
"Box '%s' run to EOS.",
122 FourCCToString(type_).c_str());
125 }
else if (size == 1) {
132 LOG(ERROR) << base::StringPrintf(
"Box '%s' with size (%" PRIu64
134 FourCCToString(type_).c_str(),
141 if (size >
static_cast<uint64_t
>(std::numeric_limits<int32_t>::max()) &&
142 type_ != FOURCC_mdat) {
143 LOG(ERROR) << base::StringPrintf(
"Box '%s' size (%" PRIu64
145 FourCCToString(type_).c_str(),