2022-08-26 15:44:59 +00:00
|
|
|
// Copyright 2014 Google LLC. All rights reserved.
|
2014-02-14 23:21:05 +00:00
|
|
|
//
|
|
|
|
// 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-14 00:08:54 +00:00
|
|
|
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/media/base/buffer_writer.h"
|
2013-11-14 00:08:54 +00:00
|
|
|
|
2022-11-04 22:46:41 +00:00
|
|
|
#include "absl/base/internal/endian.h"
|
|
|
|
#include "glog/logging.h"
|
2017-07-10 18:26:22 +00:00
|
|
|
#include "packager/file/file.h"
|
2013-11-14 00:08:54 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2013-11-14 00:08:54 +00:00
|
|
|
namespace media {
|
|
|
|
|
|
|
|
BufferWriter::BufferWriter() {
|
|
|
|
const size_t kDefaultReservedCapacity = 0x40000; // 256KB.
|
|
|
|
buf_.reserve(kDefaultReservedCapacity);
|
|
|
|
}
|
|
|
|
BufferWriter::BufferWriter(size_t reserved_size_in_bytes) {
|
|
|
|
buf_.reserve(reserved_size_in_bytes);
|
|
|
|
}
|
|
|
|
BufferWriter::~BufferWriter() {}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
void BufferWriter::AppendInt(uint8_t v) {
|
|
|
|
buf_.push_back(v);
|
|
|
|
}
|
|
|
|
void BufferWriter::AppendInt(uint16_t v) {
|
2022-11-04 22:46:41 +00:00
|
|
|
AppendInternal(absl::big_endian::FromHost16(v));
|
2014-09-30 21:52:21 +00:00
|
|
|
}
|
|
|
|
void BufferWriter::AppendInt(uint32_t v) {
|
2022-11-04 22:46:41 +00:00
|
|
|
AppendInternal(absl::big_endian::FromHost32(v));
|
2014-09-30 21:52:21 +00:00
|
|
|
}
|
|
|
|
void BufferWriter::AppendInt(uint64_t v) {
|
2022-11-04 22:46:41 +00:00
|
|
|
AppendInternal(absl::big_endian::FromHost64(v));
|
2014-09-30 21:52:21 +00:00
|
|
|
}
|
|
|
|
void BufferWriter::AppendInt(int16_t v) {
|
2022-11-04 22:46:41 +00:00
|
|
|
AppendInternal(absl::big_endian::FromHost16(v));
|
2014-09-30 21:52:21 +00:00
|
|
|
}
|
|
|
|
void BufferWriter::AppendInt(int32_t v) {
|
2022-11-04 22:46:41 +00:00
|
|
|
AppendInternal(absl::big_endian::FromHost32(v));
|
2014-09-30 21:52:21 +00:00
|
|
|
}
|
|
|
|
void BufferWriter::AppendInt(int64_t v) {
|
2022-11-04 22:46:41 +00:00
|
|
|
AppendInternal(absl::big_endian::FromHost64(v));
|
2014-09-30 21:52:21 +00:00
|
|
|
}
|
2013-11-14 00:08:54 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
void BufferWriter::AppendNBytes(uint64_t v, size_t num_bytes) {
|
2013-11-14 00:08:54 +00:00
|
|
|
DCHECK_GE(sizeof(v), num_bytes);
|
2022-11-04 22:46:41 +00:00
|
|
|
v = absl::big_endian::FromHost64(v);
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint8_t* data = reinterpret_cast<uint8_t*>(&v);
|
2013-11-14 00:08:54 +00:00
|
|
|
AppendArray(&data[sizeof(v) - num_bytes], num_bytes);
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
void BufferWriter::AppendVector(const std::vector<uint8_t>& v) {
|
2013-11-14 00:08:54 +00:00
|
|
|
buf_.insert(buf_.end(), v.begin(), v.end());
|
|
|
|
}
|
|
|
|
|
2018-05-07 22:54:59 +00:00
|
|
|
void BufferWriter::AppendString(const std::string& s) {
|
|
|
|
buf_.insert(buf_.end(), s.begin(), s.end());
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
void BufferWriter::AppendArray(const uint8_t* buf, size_t size) {
|
2013-11-14 00:08:54 +00:00
|
|
|
buf_.insert(buf_.end(), buf, buf + size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BufferWriter::AppendBuffer(const BufferWriter& buffer) {
|
|
|
|
buf_.insert(buf_.end(), buffer.buf_.begin(), buffer.buf_.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
Status BufferWriter::WriteToFile(File* file) {
|
|
|
|
DCHECK(file);
|
2014-12-13 00:13:48 +00:00
|
|
|
DCHECK(!buf_.empty());
|
2021-02-02 21:09:18 +00:00
|
|
|
|
2013-11-14 00:08:54 +00:00
|
|
|
size_t remaining_size = buf_.size();
|
2014-09-30 21:52:21 +00:00
|
|
|
const uint8_t* buf = &buf_[0];
|
2013-11-14 00:08:54 +00:00
|
|
|
while (remaining_size > 0) {
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t size_written = file->Write(buf, remaining_size);
|
2013-11-14 00:08:54 +00:00
|
|
|
if (size_written <= 0) {
|
|
|
|
return Status(error::FILE_FAILURE,
|
|
|
|
"Fail to write to file in BufferWriter");
|
|
|
|
}
|
|
|
|
remaining_size -= size_written;
|
|
|
|
buf += size_written;
|
|
|
|
}
|
|
|
|
buf_.clear();
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void BufferWriter::AppendInternal(T v) {
|
2014-09-30 21:52:21 +00:00
|
|
|
AppendArray(reinterpret_cast<uint8_t*>(&v), sizeof(T));
|
2013-11-14 00:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|