2022-08-26 15:44:59 +00:00
|
|
|
// Copyright 2015 Google LLC. All rights reserved.
|
2015-10-28 17:23:08 +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
|
|
|
|
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/media/formats/webm/mkv_writer.h>
|
2015-10-28 17:23:08 +00:00
|
|
|
|
2023-10-13 19:42:47 +00:00
|
|
|
#include <absl/log/check.h>
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2015-10-28 17:23:08 +00:00
|
|
|
namespace media {
|
|
|
|
|
|
|
|
MkvWriter::MkvWriter() : position_(0) {}
|
|
|
|
|
|
|
|
MkvWriter::~MkvWriter() {}
|
|
|
|
|
|
|
|
Status MkvWriter::Open(const std::string& name) {
|
|
|
|
DCHECK(!file_);
|
|
|
|
file_.reset(File::Open(name.c_str(), "w"));
|
|
|
|
if (!file_)
|
|
|
|
return Status(error::FILE_FAILURE, "Unable to open file for writing.");
|
|
|
|
|
|
|
|
// This may produce an error message; however there isn't a seekable method
|
|
|
|
// on File.
|
|
|
|
seekable_ = file_->Seek(0);
|
|
|
|
position_ = 0;
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2016-04-15 23:00:27 +00:00
|
|
|
Status MkvWriter::Close() {
|
|
|
|
const std::string file_name = file_->file_name();
|
|
|
|
if (!file_.release()->Close()) {
|
2017-09-07 23:05:20 +00:00
|
|
|
return Status(
|
|
|
|
error::FILE_FAILURE,
|
|
|
|
"Cannot close file " + file_name +
|
|
|
|
", possibly file permission issue or running out of disk space.");
|
2016-04-15 23:00:27 +00:00
|
|
|
}
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2015-10-28 17:23:08 +00:00
|
|
|
mkvmuxer::int32 MkvWriter::Write(const void* buf, mkvmuxer::uint32 len) {
|
|
|
|
DCHECK(file_);
|
|
|
|
|
|
|
|
const char* data = reinterpret_cast<const char*>(buf);
|
|
|
|
int64_t total_bytes_written = 0;
|
|
|
|
while (total_bytes_written < len) {
|
|
|
|
const int64_t written =
|
|
|
|
file_->Write(data + total_bytes_written, len - total_bytes_written);
|
|
|
|
if (written < 0)
|
|
|
|
return written;
|
|
|
|
|
|
|
|
total_bytes_written += written;
|
|
|
|
}
|
|
|
|
|
|
|
|
DCHECK_EQ(total_bytes_written, len);
|
|
|
|
position_ += len;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t MkvWriter::WriteFromFile(File* source) {
|
|
|
|
return WriteFromFile(source, kWholeFile);
|
|
|
|
}
|
|
|
|
|
2016-08-14 22:28:21 +00:00
|
|
|
int64_t MkvWriter::WriteFromFile(File* source, int64_t max_copy) {
|
2015-10-28 17:23:08 +00:00
|
|
|
DCHECK(file_);
|
|
|
|
|
2022-12-17 05:40:00 +00:00
|
|
|
const int64_t size = File::Copy(source, file_.get(), max_copy);
|
2015-10-28 17:23:08 +00:00
|
|
|
if (size < 0)
|
|
|
|
return size;
|
|
|
|
|
|
|
|
position_ += size;
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
mkvmuxer::int64 MkvWriter::Position() const {
|
|
|
|
return position_;
|
|
|
|
}
|
|
|
|
|
|
|
|
mkvmuxer::int32 MkvWriter::Position(mkvmuxer::int64 position) {
|
|
|
|
DCHECK(file_);
|
|
|
|
|
|
|
|
if (file_->Seek(position)) {
|
|
|
|
position_ = position;
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MkvWriter::Seekable() const {
|
|
|
|
return seekable_;
|
|
|
|
}
|
|
|
|
|
2022-12-17 05:40:00 +00:00
|
|
|
void MkvWriter::ElementStartNotify(mkvmuxer::uint64 /*element_id*/,
|
|
|
|
mkvmuxer::int64 /*position*/) {}
|
2015-10-28 17:23:08 +00:00
|
|
|
|
|
|
|
} // namespace media
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|