Shaka Packager SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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 
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:26
static int64_t CopyFile(File *source, File *destination)
Definition: file.cc:316
int64_t WriteFromFile(File *source)
Definition: mkv_writer.cc:59
void ElementStartNotify(mkvmuxer::uint64 element_id, mkvmuxer::int64 position) override
Definition: mkv_writer.cc:93
mkvmuxer::int64 Position() const override
Definition: mkv_writer.cc:74
virtual bool Open()=0
Internal open. Should not be used directly.
bool Seekable() const override
Definition: mkv_writer.cc:89
Status Open(const std::string &name)
Definition: mkv_writer.cc:16
Status Close()
Closes the file. MUST call Open before calling any other methods.
Definition: mkv_writer.cc:29
mkvmuxer::int32 Write(const void *buf, mkvmuxer::uint32 len) override
Definition: mkv_writer.cc:40