DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
box_buffer.h
1 // Copyright 2014 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #ifndef MEDIA_FORMATS_MP4_BOX_BUFFER_H_
8 #define MEDIA_FORMATS_MP4_BOX_BUFFER_H_
9 
10 #include "packager/base/compiler_specific.h"
11 #include "packager/media/base/buffer_writer.h"
12 #include "packager/media/formats/mp4/box.h"
13 #include "packager/media/formats/mp4/box_reader.h"
14 
15 namespace edash_packager {
16 namespace media {
17 namespace mp4 {
18 
23 class BoxBuffer {
24  public:
27  explicit BoxBuffer(BoxReader* reader) : reader_(reader), writer_(NULL) {
28  DCHECK(reader);
29  }
32  explicit BoxBuffer(BufferWriter* writer) : reader_(NULL), writer_(writer) {
33  DCHECK(writer);
34  }
35  ~BoxBuffer() {}
36 
38  bool Reading() const { return reader_ != NULL; }
39 
42  size_t Pos() const {
43  if (reader_)
44  return reader_->pos();
45  return writer_->Size();
46  }
47 
52  size_t Size() const {
53  if (reader_)
54  return reader_->size();
55  return writer_->Size();
56  }
57 
60  bool ReadWriteUInt8(uint8_t* v) {
61  if (reader_)
62  return reader_->Read1(v);
63  writer_->AppendInt(*v);
64  return true;
65  }
66  bool ReadWriteUInt16(uint16_t* v) {
67  if (reader_)
68  return reader_->Read2(v);
69  writer_->AppendInt(*v);
70  return true;
71  }
72  bool ReadWriteUInt32(uint32_t* v) {
73  if (reader_)
74  return reader_->Read4(v);
75  writer_->AppendInt(*v);
76  return true;
77  }
78  bool ReadWriteUInt64(uint64_t* v) {
79  if (reader_)
80  return reader_->Read8(v);
81  writer_->AppendInt(*v);
82  return true;
83  }
84  bool ReadWriteInt16(int16_t* v) {
85  if (reader_)
86  return reader_->Read2s(v);
87  writer_->AppendInt(*v);
88  return true;
89  }
90  bool ReadWriteInt32(int32_t* v) {
91  if (reader_)
92  return reader_->Read4s(v);
93  writer_->AppendInt(*v);
94  return true;
95  }
96  bool ReadWriteInt64(int64_t* v) {
97  if (reader_)
98  return reader_->Read8s(v);
99  writer_->AppendInt(*v);
100  return true;
101  }
103 
107  bool ReadWriteUInt64NBytes(uint64_t* v, size_t num_bytes) {
108  if (reader_)
109  return reader_->ReadNBytesInto8(v, num_bytes);
110  writer_->AppendNBytes(*v, num_bytes);
111  return true;
112  }
113  bool ReadWriteInt64NBytes(int64_t* v, size_t num_bytes) {
114  if (reader_)
115  return reader_->ReadNBytesInto8s(v, num_bytes);
116  writer_->AppendNBytes(*v, num_bytes);
117  return true;
118  }
119  bool ReadWriteVector(std::vector<uint8_t>* vector, size_t count) {
120  if (reader_)
121  return reader_->ReadToVector(vector, count);
122  DCHECK_EQ(vector->size(), count);
123  writer_->AppendVector(*vector);
124  return true;
125  }
126  bool ReadWriteFourCC(FourCC* fourcc) {
127  if (reader_)
128  return reader_->ReadFourCC(fourcc);
129  writer_->AppendInt(static_cast<uint32_t>(*fourcc));
130  return true;
131  }
132 
136  if (reader_)
137  return reader_->ScanChildren();
138  // NOP in write mode.
139  return true;
140  }
141 
144  bool ReadWriteChild(Box* box) {
145  if (reader_)
146  return reader_->ReadChild(box);
147  // The box is mandatory, i.e. the box size should not be 0.
148  DCHECK_NE(0u, box->atom_size);
149  CHECK(box->ReadWriteInternal(this));
150  return true;
151  }
152 
155  bool TryReadWriteChild(Box* box) {
156  if (reader_)
157  return reader_->TryReadChild(box);
158  // The box is optional, i.e. it can be skipped if the box size is 0.
159  if (box->atom_size != 0)
160  CHECK(box->ReadWriteInternal(this));
161  return true;
162  }
163 
167  bool IgnoreBytes(size_t num_bytes) {
168  if (reader_)
169  return reader_->SkipBytes(num_bytes);
170  std::vector<uint8_t> vector(num_bytes, 0);
171  writer_->AppendVector(vector);
172  return true;
173  }
174 
176  BoxReader* reader() { return reader_; }
178  BufferWriter* writer() { return writer_; }
179 
180  private:
181  BoxReader* reader_;
182  BufferWriter* writer_;
183 
184  DISALLOW_COPY_AND_ASSIGN(BoxBuffer);
185 };
186 
187 } // namespace mp4
188 } // namespace media
189 } // namespace edash_packager
190 
191 #endif // MEDIA_FORMATS_MP4_BOX_BUFFER_H_
void AppendNBytes(uint64_t v, size_t num_bytes)
bool ReadChild(Box *child) WARN_UNUSED_RESULT
Definition: box_reader.cc:123
bool Read1(uint8_t *v) WARN_UNUSED_RESULT
bool TryReadChild(Box *child) WARN_UNUSED_RESULT
Definition: box_reader.cc:140
bool ReadWriteUInt64NBytes(uint64_t *v, size_t num_bytes)
Definition: box_buffer.h:107
bool ScanChildren() WARN_UNUSED_RESULT
Definition: box_reader.cc:103
bool IgnoreBytes(size_t num_bytes)
Definition: box_buffer.h:167
bool SkipBytes(size_t num_bytes) WARN_UNUSED_RESULT
BoxBuffer(BufferWriter *writer)
Definition: box_buffer.h:32
bool ReadNBytesInto8(uint64_t *v, size_t num_bytes) WARN_UNUSED_RESULT
Class for reading MP4 boxes.
Definition: box_reader.h:24