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