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