DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
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 edash_packager {
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(error::FILE_FAILURE, "Cannot close file " + file_name);
33  }
34  return Status::OK;
35 }
36 
37 mkvmuxer::int32 MkvWriter::Write(const void* buf, mkvmuxer::uint32 len) {
38  DCHECK(file_);
39 
40  const char* data = reinterpret_cast<const char*>(buf);
41  int64_t total_bytes_written = 0;
42  while (total_bytes_written < len) {
43  const int64_t written =
44  file_->Write(data + total_bytes_written, len - total_bytes_written);
45  if (written < 0)
46  return written;
47 
48  total_bytes_written += written;
49  }
50 
51  DCHECK_EQ(total_bytes_written, len);
52  position_ += len;
53  return 0;
54 }
55 
56 int64_t MkvWriter::WriteFromFile(File* source) {
57  return WriteFromFile(source, kWholeFile);
58 }
59 
60 int64_t MkvWriter::WriteFromFile(File* source, uint64_t max_copy) {
61  DCHECK(file_);
62 
63  const int64_t size = File::CopyFile(source, file_.get(), max_copy);
64  if (size < 0)
65  return size;
66 
67  position_ += size;
68  return size;
69 }
70 
71 mkvmuxer::int64 MkvWriter::Position() const {
72  return position_;
73 }
74 
75 mkvmuxer::int32 MkvWriter::Position(mkvmuxer::int64 position) {
76  DCHECK(file_);
77 
78  if (file_->Seek(position)) {
79  position_ = position;
80  return 0;
81  } else {
82  return -1;
83  }
84 }
85 
86 bool MkvWriter::Seekable() const {
87  return seekable_;
88 }
89 
90 void MkvWriter::ElementStartNotify(mkvmuxer::uint64 element_id,
91  mkvmuxer::int64 position) {}
92 
93 } // namespace media
94 } // namespace edash_packager
mkvmuxer::int64 Position() const override
Definition: mkv_writer.cc:71
static int64_t CopyFile(File *source, File *destination)
Definition: file.cc:231
Define an abstract file interface.
Definition: file.h:24
virtual bool Open()=0
Internal open. Should not be used directly.
Status Close()
Closes the file. MUST call Open before calling any other methods.
Definition: mkv_writer.cc:29
int64_t WriteFromFile(File *source)
Definition: mkv_writer.cc:56
bool Seekable() const override
Definition: mkv_writer.cc:86
Status Open(const std::string &name)
Definition: mkv_writer.cc:16
mkvmuxer::int32 Write(const void *buf, mkvmuxer::uint32 len) override
Definition: mkv_writer.cc:37
void ElementStartNotify(mkvmuxer::uint64 element_id, mkvmuxer::int64 position) override
Definition: mkv_writer.cc:90