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