DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs 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 
29 mkvmuxer::int32 MkvWriter::Write(const void* buf, mkvmuxer::uint32 len) {
30  DCHECK(file_);
31 
32  const char* data = reinterpret_cast<const char*>(buf);
33  int64_t total_bytes_written = 0;
34  while (total_bytes_written < len) {
35  const int64_t written =
36  file_->Write(data + total_bytes_written, len - total_bytes_written);
37  if (written < 0)
38  return written;
39 
40  total_bytes_written += written;
41  }
42 
43  DCHECK_EQ(total_bytes_written, len);
44  position_ += len;
45  return 0;
46 }
47 
48 int64_t MkvWriter::WriteFromFile(File* source) {
49  return WriteFromFile(source, kWholeFile);
50 }
51 
52 int64_t MkvWriter::WriteFromFile(File* source, uint64_t max_copy) {
53  DCHECK(file_);
54 
55  const int64_t size = File::CopyFile(source, file_.get(), max_copy);
56  if (size < 0)
57  return size;
58 
59  position_ += size;
60  return size;
61 }
62 
63 mkvmuxer::int64 MkvWriter::Position() const {
64  return position_;
65 }
66 
67 mkvmuxer::int32 MkvWriter::Position(mkvmuxer::int64 position) {
68  DCHECK(file_);
69 
70  if (file_->Seek(position)) {
71  position_ = position;
72  return 0;
73  } else {
74  return -1;
75  }
76 }
77 
78 bool MkvWriter::Seekable() const {
79  return seekable_;
80 }
81 
82 void MkvWriter::ElementStartNotify(mkvmuxer::uint64 element_id,
83  mkvmuxer::int64 position) {}
84 
85 } // namespace media
86 } // namespace edash_packager
mkvmuxer::int64 Position() const override
Definition: mkv_writer.cc:63
static int64_t CopyFile(File *source, File *destination)
Definition: file.cc:210
Define an abstract file interface.
Definition: file.h:23
virtual bool Open()=0
Internal open. Should not be used directly.
int64_t WriteFromFile(File *source)
Definition: mkv_writer.cc:48
bool Seekable() const override
Definition: mkv_writer.cc:78
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:29
void ElementStartNotify(mkvmuxer::uint64 element_id, mkvmuxer::int64 position) override
Definition: mkv_writer.cc:82