Shaka Packager SDK
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 PACKAGER_MEDIA_FORMATS_MP4_BOX_BUFFER_H_
8 #define PACKAGER_MEDIA_FORMATS_MP4_BOX_BUFFER_H_
9 
10 #include <string>
11 
12 #include "packager/base/compiler_specific.h"
13 #include "packager/media/base/buffer_writer.h"
14 #include "packager/media/formats/mp4/box.h"
15 #include "packager/media/formats/mp4/box_reader.h"
16 
17 namespace shaka {
18 namespace media {
19 namespace mp4 {
20 
25 class BoxBuffer {
26  public:
29  explicit BoxBuffer(BoxReader* reader) : reader_(reader), writer_(NULL) {
30  DCHECK(reader);
31  }
34  explicit BoxBuffer(BufferWriter* writer) : reader_(NULL), writer_(writer) {
35  DCHECK(writer);
36  }
37  ~BoxBuffer() {}
38 
40  bool Reading() const { return reader_ != NULL; }
41 
44  size_t Pos() const {
45  if (reader_)
46  return reader_->pos();
47  return writer_->Size();
48  }
49 
54  size_t Size() const {
55  if (reader_)
56  return reader_->size();
57  return writer_->Size();
58  }
59 
62  size_t BytesLeft() const {
63  if (reader_)
64  return reader_->size() - reader_->pos();
65  return 0;
66  }
67 
70  bool ReadWriteUInt8(uint8_t* v) {
71  if (reader_)
72  return reader_->Read1(v);
73  writer_->AppendInt(*v);
74  return true;
75  }
76  bool ReadWriteUInt16(uint16_t* v) {
77  if (reader_)
78  return reader_->Read2(v);
79  writer_->AppendInt(*v);
80  return true;
81  }
82  bool ReadWriteUInt32(uint32_t* v) {
83  if (reader_)
84  return reader_->Read4(v);
85  writer_->AppendInt(*v);
86  return true;
87  }
88  bool ReadWriteUInt64(uint64_t* v) {
89  if (reader_)
90  return reader_->Read8(v);
91  writer_->AppendInt(*v);
92  return true;
93  }
94  bool ReadWriteInt16(int16_t* v) {
95  if (reader_)
96  return reader_->Read2s(v);
97  writer_->AppendInt(*v);
98  return true;
99  }
100  bool ReadWriteInt32(int32_t* v) {
101  if (reader_)
102  return reader_->Read4s(v);
103  writer_->AppendInt(*v);
104  return true;
105  }
106  bool ReadWriteInt64(int64_t* v) {
107  if (reader_)
108  return reader_->Read8s(v);
109  writer_->AppendInt(*v);
110  return true;
111  }
113 
117  bool ReadWriteUInt64NBytes(uint64_t* v, size_t num_bytes) {
118  if (reader_)
119  return reader_->ReadNBytesInto8(v, num_bytes);
120  writer_->AppendNBytes(*v, num_bytes);
121  return true;
122  }
123  bool ReadWriteInt64NBytes(int64_t* v, size_t num_bytes) {
124  if (reader_)
125  return reader_->ReadNBytesInto8s(v, num_bytes);
126  writer_->AppendNBytes(*v, num_bytes);
127  return true;
128  }
129  bool ReadWriteVector(std::vector<uint8_t>* vector, size_t count) {
130  if (reader_)
131  return reader_->ReadToVector(vector, count);
132  DCHECK_EQ(vector->size(), count);
133  writer_->AppendArray(vector->data(), count);
134  return true;
135  }
136 
139  bool ReadWriteString(std::string* str, size_t size) {
140  if (reader_)
141  return reader_->ReadToString(str, size);
142  DCHECK_EQ(str->size(), size);
143  writer_->AppendArray(reinterpret_cast<const uint8_t*>(str->data()),
144  str->size());
145  return true;
146  }
147 
148  bool ReadWriteCString(std::string* str) {
149  if (reader_)
150  return reader_->ReadCString(str);
151  // Cannot contain embedded nulls.
152  DCHECK_EQ(str->find('\0'), std::string::npos);
153  writer_->AppendString(*str);
154  writer_->AppendInt(static_cast<uint8_t>('\0'));
155  return true;
156  }
157 
158  bool ReadWriteFourCC(FourCC* fourcc) {
159  if (reader_)
160  return reader_->ReadFourCC(fourcc);
161  writer_->AppendInt(static_cast<uint32_t>(*fourcc));
162  return true;
163  }
164 
168  if (reader_)
169  return reader_->ScanChildren();
170  // NOP in write mode.
171  return true;
172  }
173 
176  bool ReadWriteChild(Box* box) {
177  if (reader_)
178  return reader_->ReadChild(box);
179  // The box is mandatory, i.e. the box size should not be 0.
180  DCHECK_NE(0u, box->box_size());
181  CHECK(box->ReadWriteInternal(this));
182  return true;
183  }
184 
187  bool TryReadWriteChild(Box* box) {
188  if (reader_)
189  return reader_->TryReadChild(box);
190  // The box is optional, i.e. it can be skipped if the box size is 0.
191  if (box->box_size() != 0)
192  CHECK(box->ReadWriteInternal(this));
193  return true;
194  }
195 
199  bool IgnoreBytes(size_t num_bytes) {
200  if (reader_)
201  return reader_->SkipBytes(num_bytes);
202  std::vector<uint8_t> vector(num_bytes, 0);
203  writer_->AppendVector(vector);
204  return true;
205  }
206 
208  BoxReader* reader() { return reader_; }
210  BufferWriter* writer() { return writer_; }
211 
212  private:
213  BoxReader* reader_;
214  BufferWriter* writer_;
215 
216  DISALLOW_COPY_AND_ASSIGN(BoxBuffer);
217 };
218 
219 } // namespace mp4
220 } // namespace media
221 } // namespace shaka
222 
223 #endif // PACKAGER_MEDIA_FORMATS_MP4_BOX_BUFFER_H_
bool SkipBytes(size_t num_bytes) WARN_UNUSED_RESULT
bool Read1(uint8_t *v) WARN_UNUSED_RESULT
bool ReadNBytesInto8(uint64_t *v, size_t num_bytes) WARN_UNUSED_RESULT
bool ReadCString(std::string *str) WARN_UNUSED_RESULT
Reads a null-terminated string.
void AppendNBytes(uint64_t v, size_t num_bytes)
BoxBuffer(BufferWriter *writer)
Definition: box_buffer.h:34
BufferWriter * writer()
Definition: box_buffer.h:210
bool IgnoreBytes(size_t num_bytes)
Definition: box_buffer.h:199
bool ReadWriteUInt64NBytes(uint64_t *v, size_t num_bytes)
Definition: box_buffer.h:117
BoxBuffer(BoxReader *reader)
Definition: box_buffer.h:29
bool TryReadWriteChild(Box *box)
Definition: box_buffer.h:187
size_t BytesLeft() const
Definition: box_buffer.h:62
bool ReadWriteString(std::string *str, size_t size)
Definition: box_buffer.h:139
bool ReadWriteChild(Box *box)
Definition: box_buffer.h:176
Class for reading MP4 boxes.
Definition: box_reader.h:25
bool TryReadChild(Box *child) WARN_UNUSED_RESULT
Definition: box_reader.cc:106
bool ReadChild(Box *child) WARN_UNUSED_RESULT
Definition: box_reader.cc:90
bool ScanChildren() WARN_UNUSED_RESULT
Definition: box_reader.cc:67
All the methods that are virtual are virtual for mocking.
uint32_t box_size()
Definition: box.h:55