blob: 7547e7d16c6ff07772b57e9c7b11e88ca829543a [file] [log] [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/muxers/mp4_box_writer.h"
#include "base/big_endian.h"
#include "media/muxers/box_byte_stream.h"
#include "media/muxers/mp4_muxer_context.h"
#include "media/muxers/output_position_tracker.h"
namespace media {
Mp4BoxWriter::Mp4BoxWriter(const Mp4MuxerContext& context)
: context_(context) {}
Mp4BoxWriter::~Mp4BoxWriter() = default;
void Mp4BoxWriter::WriteAndFlush() {
BoxByteStream writer;
// It will write itself as well as children boxes.
Write(writer);
// Update the total size on respective boxes.
std::vector<uint8_t> buffer = writer.EndWrite();
// Write the entire boxes to the blob.
context().GetOutputPositionTracker().WriteString(
base::StringPiece(reinterpret_cast<char*>(buffer.data()), buffer.size()));
}
void Mp4BoxWriter::WriteChildren(BoxByteStream& writer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
for (auto& child_box : child_boxes_) {
child_box->Write(writer);
}
}
void Mp4BoxWriter::AddChildBox(std::unique_ptr<Mp4BoxWriter> box_writer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
child_boxes_.push_back(std::move(box_writer));
}
void Mp4BoxWriter::WriteBox(BoxByteStream& writer, mp4::FourCC fourcc) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
writer.WritePlaceholderSizeU32();
writer.WriteU32(fourcc);
}
void Mp4BoxWriter::WriteFullBox(BoxByteStream& writer,
mp4::FourCC fourcc,
uint32_t flags,
uint8_t version) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
writer.WritePlaceholderSizeU32();
writer.WriteU32(fourcc);
writer.WriteU8(version);
writer.WriteU24(flags);
}
} // namespace media