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