blob: 7847067388d9b73a49589dbbedec6892ddeb01f0 [file] [log] [blame]
// Copyright 2021 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/file_webm_muxer_delegate.h"
#include <cstdint>
#include "base/containers/span.h"
#include "base/logging.h"
namespace media {
FileWebmMuxerDelegate::FileWebmMuxerDelegate(base::File webm_file)
: webm_file_(std::move(webm_file)) {
FileWebmMuxerDelegate::~FileWebmMuxerDelegate() = default;
void FileWebmMuxerDelegate::InitSegment(mkvmuxer::Segment* segment) {
// According to the Matroska specs [1], it is possible to seek without the
// Cues elements, but it would be much more difficult because a video player
// would have to "hunt and peck through the file looking for the correct
// timestamp". So the use of Cues are recommended, because they allow for
// optimized seeking to absolute timestamps within the Segment.
// [1]:
mkvmuxer::int64 FileWebmMuxerDelegate::Position() const {
return position_.ValueOrDie();
mkvmuxer::int32 FileWebmMuxerDelegate::Position(mkvmuxer::int64 position) {
position_ = position;
webm_file_.Seek(base::File::FROM_BEGIN, position);
return 0;
bool FileWebmMuxerDelegate::Seekable() const {
return true;
void FileWebmMuxerDelegate::ElementStartNotify(mkvmuxer::uint64 element_id,
mkvmuxer::int64 position) {}
mkvmuxer::int32 FileWebmMuxerDelegate::DoWrite(const void* buf,
mkvmuxer::uint32 len) {
const bool success = webm_file_.WriteAtCurrentPosAndCheck(base::as_bytes(
base::make_span(reinterpret_cast<const uint8_t*>(buf),
reinterpret_cast<const uint8_t*>(buf) + len)));
LOG_IF(ERROR, !success) << "Failed to write muxer data to file.";
return success ? 0 : -1;
} // namespace media