Shaka Packager SDK
mkv_writer.cc
1 // Copyright 2015 Google LLC. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include <packager/media/formats/webm/mkv_writer.h>
8 
9 #include <absl/log/check.h>
10 
11 namespace shaka {
12 namespace media {
13 
14 MkvWriter::MkvWriter() : position_(0) {}
15 
16 MkvWriter::~MkvWriter() {}
17 
18 Status MkvWriter::Open(const std::string& name) {
19  DCHECK(!file_);
20  file_.reset(File::Open(name.c_str(), "w"));
21  if (!file_)
22  return Status(error::FILE_FAILURE, "Unable to open file for writing.");
23 
24  // This may produce an error message; however there isn't a seekable method
25  // on File.
26  seekable_ = file_->Seek(0);
27  position_ = 0;
28  return Status::OK;
29 }
30 
31 Status MkvWriter::Close() {
32  const std::string file_name = file_->file_name();
33  if (!file_.release()->Close()) {
34  return Status(
35  error::FILE_FAILURE,
36  "Cannot close file " + file_name +
37  ", possibly file permission issue or running out of disk space.");
38  }
39  return Status::OK;
40 }
41 
42 mkvmuxer::int32 MkvWriter::Write(const void* buf, mkvmuxer::uint32 len) {
43  DCHECK(file_);
44 
45  const char* data = reinterpret_cast<const char*>(buf);
46  int64_t total_bytes_written = 0;
47  while (total_bytes_written < len) {
48  const int64_t written =
49  file_->Write(data + total_bytes_written, len - total_bytes_written);
50  if (written < 0)
51  return written;
52 
53  total_bytes_written += written;
54  }
55 
56  DCHECK_EQ(total_bytes_written, len);
57  position_ += len;
58  return 0;
59 }
60 
61 int64_t MkvWriter::WriteFromFile(File* source) {
62  return WriteFromFile(source, kWholeFile);
63 }
64 
65 int64_t MkvWriter::WriteFromFile(File* source, int64_t max_copy) {
66  DCHECK(file_);
67 
68  const int64_t size = File::Copy(source, file_.get(), max_copy);
69  if (size < 0)
70  return size;
71 
72  position_ += size;
73  return size;
74 }
75 
76 mkvmuxer::int64 MkvWriter::Position() const {
77  return position_;
78 }
79 
80 mkvmuxer::int32 MkvWriter::Position(mkvmuxer::int64 position) {
81  DCHECK(file_);
82 
83  if (file_->Seek(position)) {
84  position_ = position;
85  return 0;
86  } else {
87  return -1;
88  }
89 }
90 
91 bool MkvWriter::Seekable() const {
92  return seekable_;
93 }
94 
95 void MkvWriter::ElementStartNotify(mkvmuxer::uint64 /*element_id*/,
96  mkvmuxer::int64 /*position*/) {}
97 
98 } // namespace media
99 } // namespace shaka
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66