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