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 ReadWriteFourCC(FourCC* fourcc) {
149  if (reader_)
150  return reader_->ReadFourCC(fourcc);
151  writer_->AppendInt(static_cast<uint32_t>(*fourcc));
152  return true;
153  }
154 
158  if (reader_)
159  return reader_->ScanChildren();
160  // NOP in write mode.
161  return true;
162  }
163 
166  bool ReadWriteChild(Box* box) {
167  if (reader_)
168  return reader_->ReadChild(box);
169  // The box is mandatory, i.e. the box size should not be 0.
170  DCHECK_NE(0u, box->box_size());
171  CHECK(box->ReadWriteInternal(this));
172  return true;
173  }
174 
177  bool TryReadWriteChild(Box* box) {
178  if (reader_)
179  return reader_->TryReadChild(box);
180  // The box is optional, i.e. it can be skipped if the box size is 0.
181  if (box->box_size() != 0)
182  CHECK(box->ReadWriteInternal(this));
183  return true;
184  }
185 
189  bool IgnoreBytes(size_t num_bytes) {
190  if (reader_)
191  return reader_->SkipBytes(num_bytes);
192  std::vector<uint8_t> vector(num_bytes, 0);
193  writer_->AppendVector(vector);
194  return true;
195  }
196 
198  BoxReader* reader() { return reader_; }
200  BufferWriter* writer() { return writer_; }
201 
202  private:
203  BoxReader* reader_;
204  BufferWriter* writer_;
205 
206  DISALLOW_COPY_AND_ASSIGN(BoxBuffer);
207 };
208 
209 } // namespace mp4
210 } // namespace media
211 } // namespace shaka
212 
213 #endif // PACKAGER_MEDIA_FORMATS_MP4_BOX_BUFFER_H_
bool TryReadWriteChild(Box *box)
Definition: box_buffer.h:177
size_t BytesLeft() const
Definition: box_buffer.h:62
All the methods that are virtual are virtual for mocking.
bool ScanChildren() WARN_UNUSED_RESULT
Definition: box_reader.cc:67
void AppendNBytes(uint64_t v, size_t num_bytes)
bool ReadNBytesInto8(uint64_t *v, size_t num_bytes) WARN_UNUSED_RESULT
bool Read1(uint8_t *v) WARN_UNUSED_RESULT
uint32_t box_size()
Definition: box.h:55
BufferWriter * writer()
Definition: box_buffer.h:200
bool IgnoreBytes(size_t num_bytes)
Definition: box_buffer.h:189
bool SkipBytes(size_t num_bytes) WARN_UNUSED_RESULT
Class for reading MP4 boxes.
Definition: box_reader.h:25
bool ReadWriteString(std::string *str, size_t size)
Definition: box_buffer.h:139
bool ReadChild(Box *child) WARN_UNUSED_RESULT
Definition: box_reader.cc:88
bool ReadWriteUInt64NBytes(uint64_t *v, size_t num_bytes)
Definition: box_buffer.h:117
bool TryReadChild(Box *child) WARN_UNUSED_RESULT
Definition: box_reader.cc:104
BoxBuffer(BufferWriter *writer)
Definition: box_buffer.h:34
BoxBuffer(BoxReader *reader)
Definition: box_buffer.h:29
bool ReadWriteChild(Box *box)
Definition: box_buffer.h:166