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