2014-02-14 23:21:05 +00:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
2013-11-22 21:24:25 +00:00
|
|
|
|
2017-12-20 00:56:36 +00:00
|
|
|
#ifndef PACKAGER_MEDIA_FORMATS_MP4_BOX_BUFFER_H_
|
|
|
|
#define PACKAGER_MEDIA_FORMATS_MP4_BOX_BUFFER_H_
|
2013-11-22 21:24:25 +00:00
|
|
|
|
2015-11-23 23:12:04 +00:00
|
|
|
#include <string>
|
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/compiler_specific.h"
|
|
|
|
#include "packager/media/base/buffer_writer.h"
|
|
|
|
#include "packager/media/formats/mp4/box.h"
|
|
|
|
#include "packager/media/formats/mp4/box_reader.h"
|
2013-11-22 21:24:25 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2013-11-22 21:24:25 +00:00
|
|
|
namespace media {
|
|
|
|
namespace mp4 {
|
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// Class for MP4 box I/O. Box I/O is symmetric and exclusive, so we can define
|
|
|
|
/// a single method to do either reading or writing box objects.
|
|
|
|
/// BoxBuffer wraps either BoxReader for reading or BufferWriter for writing.
|
|
|
|
/// Thus it is capable of doing either reading or writing, but not both.
|
2013-11-22 21:24:25 +00:00
|
|
|
class BoxBuffer {
|
|
|
|
public:
|
2014-01-23 22:34:39 +00:00
|
|
|
/// Create a reader version of the BoxBuffer.
|
|
|
|
/// @param reader should not be NULL.
|
2013-11-22 21:24:25 +00:00
|
|
|
explicit BoxBuffer(BoxReader* reader) : reader_(reader), writer_(NULL) {
|
|
|
|
DCHECK(reader);
|
|
|
|
}
|
2014-01-23 22:34:39 +00:00
|
|
|
/// Create a writer version of the BoxBuffer.
|
|
|
|
/// @param writer should not be NULL.
|
2013-11-22 21:24:25 +00:00
|
|
|
explicit BoxBuffer(BufferWriter* writer) : reader_(NULL), writer_(writer) {
|
|
|
|
DCHECK(writer);
|
|
|
|
}
|
|
|
|
~BoxBuffer() {}
|
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// @return true for reader, false for writer.
|
2013-11-22 21:24:25 +00:00
|
|
|
bool Reading() const { return reader_ != NULL; }
|
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// @return Current read/write position. In read mode, this is the current
|
|
|
|
/// read position. In write mode, it is the same as Size().
|
2013-11-22 21:24:25 +00:00
|
|
|
size_t Pos() const {
|
|
|
|
if (reader_)
|
|
|
|
return reader_->pos();
|
|
|
|
return writer_->Size();
|
|
|
|
}
|
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// @return Total buffer size. In read mode, it includes data that has already
|
|
|
|
/// been read or skipped, and will not change. In write mode, it
|
|
|
|
/// includes all data that has been written, and will change as more
|
|
|
|
/// data is written.
|
2013-11-22 21:24:25 +00:00
|
|
|
size_t Size() const {
|
|
|
|
if (reader_)
|
|
|
|
return reader_->size();
|
|
|
|
return writer_->Size();
|
|
|
|
}
|
|
|
|
|
2015-11-23 23:12:04 +00:00
|
|
|
/// @return In read mode, return the number of bytes left in the box.
|
|
|
|
/// In write mode, return 0.
|
|
|
|
size_t BytesLeft() const {
|
|
|
|
if (reader_)
|
|
|
|
return reader_->size() - reader_->pos();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// @name Read/write integers of various sizes and signedness.
|
|
|
|
/// @{
|
2014-09-30 21:52:21 +00:00
|
|
|
bool ReadWriteUInt8(uint8_t* v) {
|
2013-11-22 21:24:25 +00:00
|
|
|
if (reader_)
|
|
|
|
return reader_->Read1(v);
|
|
|
|
writer_->AppendInt(*v);
|
|
|
|
return true;
|
|
|
|
}
|
2014-09-30 21:52:21 +00:00
|
|
|
bool ReadWriteUInt16(uint16_t* v) {
|
2013-11-22 21:24:25 +00:00
|
|
|
if (reader_)
|
|
|
|
return reader_->Read2(v);
|
|
|
|
writer_->AppendInt(*v);
|
|
|
|
return true;
|
|
|
|
}
|
2014-09-30 21:52:21 +00:00
|
|
|
bool ReadWriteUInt32(uint32_t* v) {
|
2013-11-22 21:24:25 +00:00
|
|
|
if (reader_)
|
|
|
|
return reader_->Read4(v);
|
|
|
|
writer_->AppendInt(*v);
|
|
|
|
return true;
|
|
|
|
}
|
2014-09-30 21:52:21 +00:00
|
|
|
bool ReadWriteUInt64(uint64_t* v) {
|
2013-11-22 21:24:25 +00:00
|
|
|
if (reader_)
|
|
|
|
return reader_->Read8(v);
|
|
|
|
writer_->AppendInt(*v);
|
|
|
|
return true;
|
|
|
|
}
|
2014-09-30 21:52:21 +00:00
|
|
|
bool ReadWriteInt16(int16_t* v) {
|
2013-11-22 21:24:25 +00:00
|
|
|
if (reader_)
|
|
|
|
return reader_->Read2s(v);
|
|
|
|
writer_->AppendInt(*v);
|
|
|
|
return true;
|
|
|
|
}
|
2014-09-30 21:52:21 +00:00
|
|
|
bool ReadWriteInt32(int32_t* v) {
|
2013-11-22 21:24:25 +00:00
|
|
|
if (reader_)
|
|
|
|
return reader_->Read4s(v);
|
|
|
|
writer_->AppendInt(*v);
|
|
|
|
return true;
|
|
|
|
}
|
2014-09-30 21:52:21 +00:00
|
|
|
bool ReadWriteInt64(int64_t* v) {
|
2013-11-22 21:24:25 +00:00
|
|
|
if (reader_)
|
|
|
|
return reader_->Read8s(v);
|
|
|
|
writer_->AppendInt(*v);
|
|
|
|
return true;
|
|
|
|
}
|
2014-01-23 22:34:39 +00:00
|
|
|
/// @}
|
2013-11-22 21:24:25 +00:00
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// Read/write the least significant |num_bytes| of |v| from/to the buffer.
|
|
|
|
/// @param num_bytes should not be larger than sizeof(v), i.e. 8.
|
|
|
|
/// @return true on success, false otherwise.
|
2014-09-30 21:52:21 +00:00
|
|
|
bool ReadWriteUInt64NBytes(uint64_t* v, size_t num_bytes) {
|
2013-11-22 21:24:25 +00:00
|
|
|
if (reader_)
|
|
|
|
return reader_->ReadNBytesInto8(v, num_bytes);
|
|
|
|
writer_->AppendNBytes(*v, num_bytes);
|
|
|
|
return true;
|
|
|
|
}
|
2014-09-30 21:52:21 +00:00
|
|
|
bool ReadWriteInt64NBytes(int64_t* v, size_t num_bytes) {
|
2013-11-22 21:24:25 +00:00
|
|
|
if (reader_)
|
|
|
|
return reader_->ReadNBytesInto8s(v, num_bytes);
|
|
|
|
writer_->AppendNBytes(*v, num_bytes);
|
|
|
|
return true;
|
|
|
|
}
|
2014-09-30 21:52:21 +00:00
|
|
|
bool ReadWriteVector(std::vector<uint8_t>* vector, size_t count) {
|
2013-11-22 21:24:25 +00:00
|
|
|
if (reader_)
|
|
|
|
return reader_->ReadToVector(vector, count);
|
|
|
|
DCHECK_EQ(vector->size(), count);
|
2016-01-29 21:23:12 +00:00
|
|
|
writer_->AppendArray(vector->data(), count);
|
2013-11-22 21:24:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
2015-11-23 23:12:04 +00:00
|
|
|
|
|
|
|
/// Reads @a size characters from the buffer and sets it to str.
|
|
|
|
/// Writes @a str to the buffer. Write mode ignores @a size.
|
|
|
|
bool ReadWriteString(std::string* str, size_t size) {
|
|
|
|
if (reader_)
|
|
|
|
return reader_->ReadToString(str, size);
|
|
|
|
DCHECK_EQ(str->size(), size);
|
|
|
|
writer_->AppendArray(reinterpret_cast<const uint8_t*>(str->data()),
|
|
|
|
str->size());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-22 21:24:25 +00:00
|
|
|
bool ReadWriteFourCC(FourCC* fourcc) {
|
|
|
|
if (reader_)
|
|
|
|
return reader_->ReadFourCC(fourcc);
|
2014-09-30 21:52:21 +00:00
|
|
|
writer_->AppendInt(static_cast<uint32_t>(*fourcc));
|
2013-11-22 21:24:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// Prepare child boxes for reading/writing.
|
|
|
|
/// @return true on success, false otherwise.
|
2013-11-22 21:24:25 +00:00
|
|
|
bool PrepareChildren() {
|
|
|
|
if (reader_)
|
|
|
|
return reader_->ScanChildren();
|
|
|
|
// NOP in write mode.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// Read/write child box.
|
|
|
|
/// @return true on success, false otherwise.
|
2013-11-22 21:24:25 +00:00
|
|
|
bool ReadWriteChild(Box* box) {
|
|
|
|
if (reader_)
|
|
|
|
return reader_->ReadChild(box);
|
|
|
|
// The box is mandatory, i.e. the box size should not be 0.
|
2015-12-21 18:34:21 +00:00
|
|
|
DCHECK_NE(0u, box->box_size());
|
2015-12-15 00:07:51 +00:00
|
|
|
CHECK(box->ReadWriteInternal(this));
|
2013-11-22 21:24:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// Read/write child box if exists.
|
|
|
|
/// @return true on success, false otherwise.
|
2013-11-22 21:24:25 +00:00
|
|
|
bool TryReadWriteChild(Box* box) {
|
|
|
|
if (reader_)
|
|
|
|
return reader_->TryReadChild(box);
|
|
|
|
// The box is optional, i.e. it can be skipped if the box size is 0.
|
2015-12-21 18:34:21 +00:00
|
|
|
if (box->box_size() != 0)
|
2015-12-15 00:07:51 +00:00
|
|
|
CHECK(box->ReadWriteInternal(this));
|
2013-11-22 21:24:25 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// @param num_bytes specifies number of bytes to skip in read mode or number
|
|
|
|
/// of bytes to be padded with zero in write mode.
|
|
|
|
/// @return true on success, false otherwise.
|
2013-11-22 21:24:25 +00:00
|
|
|
bool IgnoreBytes(size_t num_bytes) {
|
|
|
|
if (reader_)
|
|
|
|
return reader_->SkipBytes(num_bytes);
|
2014-09-30 21:52:21 +00:00
|
|
|
std::vector<uint8_t> vector(num_bytes, 0);
|
2013-11-22 21:24:25 +00:00
|
|
|
writer_->AppendVector(vector);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-23 22:34:39 +00:00
|
|
|
/// @return A pointer to the inner reader object.
|
2013-11-22 21:24:25 +00:00
|
|
|
BoxReader* reader() { return reader_; }
|
2014-01-23 22:34:39 +00:00
|
|
|
/// @return A pointer to the inner writer object.
|
2013-11-22 21:24:25 +00:00
|
|
|
BufferWriter* writer() { return writer_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
BoxReader* reader_;
|
|
|
|
BufferWriter* writer_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(BoxBuffer);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mp4
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|
2013-11-22 21:24:25 +00:00
|
|
|
|
2017-12-20 00:56:36 +00:00
|
|
|
#endif // PACKAGER_MEDIA_FORMATS_MP4_BOX_BUFFER_H_
|