DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
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 MEDIA_FORMATS_MP4_BOX_READER_H_
6 #define MEDIA_FORMATS_MP4_BOX_READER_H_
7 
8 #include <map>
9 #include <vector>
10 
11 #include "packager/base/compiler_specific.h"
12 #include "packager/base/logging.h"
13 #include "packager/media/base/buffer_reader.h"
14 #include "packager/media/base/fourccs.h"
15 #include "packager/media/base/rcheck.h"
16 
17 namespace shaka {
18 namespace media {
19 namespace mp4 {
20 
21 struct Box;
22 
24 class BoxReader : public BufferReader {
25  public:
26  ~BoxReader();
27 
38  static BoxReader* ReadTopLevelBox(const uint8_t* buf,
39  const size_t buf_size,
40  bool* err);
41 
51  static bool StartTopLevelBox(const uint8_t* buf,
52  const size_t buf_size,
53  FourCC* type,
54  uint64_t* box_size,
55  bool* err) WARN_UNUSED_RESULT;
56 
61  static bool IsValidTopLevelBox(const FourCC& type);
62 
67  bool ScanChildren() WARN_UNUSED_RESULT;
68 
70  bool ChildExist(Box* child) WARN_UNUSED_RESULT;
71 
75  bool ReadChild(Box* child) WARN_UNUSED_RESULT;
76 
79  bool TryReadChild(Box* child) WARN_UNUSED_RESULT;
80 
83  template <typename T>
84  bool ReadChildren(std::vector<T>* children) WARN_UNUSED_RESULT;
85 
88  template <typename T>
89  bool TryReadChildren(std::vector<T>* children) WARN_UNUSED_RESULT;
90 
94  template <typename T>
95  bool ReadAllChildren(std::vector<T>* children) WARN_UNUSED_RESULT;
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, 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.
132 template <typename T>
133 bool BoxReader::ReadChildren(std::vector<T>* children) {
134  RCHECK(TryReadChildren(children) && !children->empty());
135  return true;
136 }
137 
138 template <typename T>
139 bool 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));
152  delete itr->second;
153  ++child_itr;
154  }
155  children_.erase(start_itr, end_itr);
156 
157  DVLOG(2) << "Found " << children->size() << " " << FourCCToString(child_type)
158  << " boxes.";
159  return true;
160 }
161 
162 template <typename T>
163 bool BoxReader::ReadAllChildren(std::vector<T>* children) {
164  DCHECK(!scanned_);
165  scanned_ = true;
166 
167  while (pos() < size()) {
168  BoxReader child_reader(&data()[pos()], size() - pos());
169  bool err;
170  if (!child_reader.ReadHeader(&err))
171  return false;
172 
173  T child;
174  RCHECK(child.Parse(&child_reader));
175  children->push_back(child);
176  RCHECK(SkipBytes(child_reader.size()));
177  }
178 
179  return true;
180 }
181 
182 } // namespace mp4
183 } // namespace media
184 } // namespace shaka
185 
186 #endif // MEDIA_FORMATS_MP4_BOX_READER_H_
bool TryReadChildren(std::vector< T > *children) WARN_UNUSED_RESULT
Definition: box_reader.h:139
bool ReadAllChildren(std::vector< T > *children) WARN_UNUSED_RESULT
Definition: box_reader.h:163
static BoxReader * ReadTopLevelBox(const uint8_t *buf, const size_t buf_size, bool *err)
Definition: box_reader.cc:37
static bool IsValidTopLevelBox(const FourCC &type)
Definition: box_reader.cc:78
bool ScanChildren() WARN_UNUSED_RESULT
Definition: box_reader.cc:104
bool ReadChildren(std::vector< T > *children) WARN_UNUSED_RESULT
Definition: box_reader.h:133
bool SkipBytes(size_t num_bytes) WARN_UNUSED_RESULT
Class for reading MP4 boxes.
Definition: box_reader.h:24
bool ReadChild(Box *child) WARN_UNUSED_RESULT
Definition: box_reader.cc:124
bool TryReadChild(Box *child) WARN_UNUSED_RESULT
Definition: box_reader.cc:141
bool ChildExist(Box *child) WARN_UNUSED_RESULT
Definition: box_reader.cc:137
static bool StartTopLevelBox(const uint8_t *buf, const size_t buf_size, FourCC *type, uint64_t *box_size, bool *err) WARN_UNUSED_RESULT
Definition: box_reader.cc:60