Shaka Packager SDK
Loading...
Searching...
No Matches
box_reader.h
1// Copyright (c) 2012 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#ifndef PACKAGER_MEDIA_FORMATS_MP4_BOX_READER_H_
6#define PACKAGER_MEDIA_FORMATS_MP4_BOX_READER_H_
7
8#include <cstddef>
9#include <cstdint>
10#include <iterator>
11#include <map>
12#include <memory>
13#include <vector>
14
15#include <absl/log/check.h>
16#include <absl/log/log.h>
17
18#include <packager/macros/classes.h>
19#include <packager/media/base/buffer_reader.h>
20#include <packager/media/base/fourccs.h>
21#include <packager/media/base/rcheck.h>
22
23namespace shaka {
24namespace media {
25namespace mp4 {
26
27struct Box;
28
30class BoxReader : public BufferReader {
31 public:
32 ~BoxReader();
33
44 static BoxReader* ReadBox(const uint8_t* buf,
45 const size_t buf_size,
46 bool* err);
47
57 [[nodiscard]] static bool StartBox(const uint8_t* buf,
58 const size_t buf_size,
59 FourCC* type,
60 uint64_t* box_size,
61 bool* err);
62
67 [[nodiscard]] bool ScanChildren();
68
70 [[nodiscard]] bool ChildExist(Box* child);
71
75 [[nodiscard]] bool ReadChild(Box* child);
76
79 [[nodiscard]] bool TryReadChild(Box* child);
80
83 template <typename T>
84 [[nodiscard]] bool ReadChildren(std::vector<T>* children);
85
88 template <typename T>
89 [[nodiscard]] bool TryReadChildren(std::vector<T>* children);
90
94 template <typename T>
95 [[nodiscard]] bool ReadAllChildren(std::vector<T>* children);
96
97 bool ReadFourCC(FourCC* fourcc) {
98 uint32_t val;
99 if (!Read4(&val))
100 return false;
101 *fourcc = static_cast<FourCC>(val);
102 return true;
103 }
104
105 FourCC type() const { return type_; }
106
107 private:
108 BoxReader(const uint8_t* buf, size_t size);
109
110 // Must be called immediately after init. If the return is false, this
111 // indicates that the box header and its contents were not available in the
112 // stream or were nonsensical, and that the box must not be used further. In
113 // this case, if |*err| is false, the problem was simply a lack of data, and
114 // should only be an error condition if some higher-level component knows that
115 // no more data is coming (i.e. EOS or end of containing box). If |*err| is
116 // true, the error is unrecoverable and the stream should be aborted.
117 bool ReadHeader(bool* err);
118
119 FourCC type_;
120
121 typedef std::multimap<FourCC, std::unique_ptr<BoxReader>> ChildMap;
122
123 // The set of child box FourCCs and their corresponding buffer readers. Only
124 // valid if scanned_ is true.
125 ChildMap children_;
126 bool scanned_;
127
128 DISALLOW_COPY_AND_ASSIGN(BoxReader);
129};
130
131// Template definitions.
132template <typename T>
133bool BoxReader::ReadChildren(std::vector<T>* children) {
134 RCHECK(TryReadChildren(children) && !children->empty());
135 return true;
136}
137
138template <typename T>
139bool BoxReader::TryReadChildren(std::vector<T>* children) {
140 DCHECK(scanned_);
141 DCHECK(children->empty());
142
143 children->resize(1);
144 FourCC child_type = (*children)[0].BoxType();
145
146 ChildMap::iterator start_itr = children_.lower_bound(child_type);
147 ChildMap::iterator end_itr = children_.upper_bound(child_type);
148 children->resize(std::distance(start_itr, end_itr));
149 typename std::vector<T>::iterator child_itr = children->begin();
150 for (ChildMap::iterator itr = start_itr; itr != end_itr; ++itr) {
151 RCHECK(child_itr->Parse(itr->second.get()));
152 ++child_itr;
153 }
154 children_.erase(start_itr, end_itr);
155
156 DVLOG(2) << "Found " << children->size() << " " << FourCCToString(child_type)
157 << " boxes.";
158 return true;
159}
160
161template <typename T>
162bool BoxReader::ReadAllChildren(std::vector<T>* children) {
163 DCHECK(!scanned_);
164 scanned_ = true;
165
166 while (pos() < size()) {
167 BoxReader child_reader(&data()[pos()], size() - pos());
168 bool err;
169 if (!child_reader.ReadHeader(&err))
170 return false;
171
172 T child;
173 RCHECK(child.Parse(&child_reader));
174 children->push_back(child);
175 RCHECK(SkipBytes(child_reader.size()));
176 }
177
178 return true;
179}
180
181} // namespace mp4
182} // namespace media
183} // namespace shaka
184
185#endif // PACKAGER_MEDIA_FORMATS_MP4_BOX_READER_H_
bool SkipBytes(size_t num_bytes)
Class for reading MP4 boxes.
Definition box_reader.h:30
static bool StartBox(const uint8_t *buf, const size_t buf_size, FourCC *type, uint64_t *box_size, bool *err)
Definition box_reader.cc:63
bool ReadChildren(std::vector< T > *children)
Definition box_reader.h:133
bool ChildExist(Box *child)
bool ReadChild(Box *child)
Definition box_reader.cc:99
bool TryReadChild(Box *child)
bool ReadAllChildren(std::vector< T > *children)
Definition box_reader.h:162
static BoxReader * ReadBox(const uint8_t *buf, const size_t buf_size, bool *err)
Definition box_reader.cc:45
bool TryReadChildren(std::vector< T > *children)
Definition box_reader.h:139
All the methods that are virtual are virtual for mocking.