Shaka Packager SDK
webvtt_muxer.cc
1 // Copyright 2020 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/webvtt/webvtt_muxer.h>
8 
9 #include <packager/file.h>
10 #include <packager/file/file_closer.h>
11 #include <packager/macros/status.h>
12 #include <packager/media/formats/webvtt/webvtt_utils.h>
13 
14 namespace shaka {
15 namespace media {
16 namespace webvtt {
17 
18 WebVttMuxer::WebVttMuxer(const MuxerOptions& options) : TextMuxer(options) {}
19 WebVttMuxer::~WebVttMuxer() {}
20 
21 Status WebVttMuxer::InitializeStream(TextStreamInfo* stream) {
22  stream->set_codec(kCodecWebVtt);
23  stream->set_codec_string("wvtt");
24 
25  const std::string preamble = WebVttGetPreamble(*stream);
26  buffer_.reset(new WebVttFileBuffer(
27  options().transport_stream_timestamp_offset_ms, preamble));
28  return Status::OK;
29 }
30 
31 Status WebVttMuxer::AddTextSampleInternal(const TextSample& sample) {
32  if (sample.id().find('\n') != std::string::npos) {
33  return Status(error::MUXER_FAILURE, "Text id cannot contain newlines");
34  }
35 
36  buffer_->Append(sample);
37  return Status::OK;
38 }
39 
40 Status WebVttMuxer::WriteToFile(const std::string& filename, uint64_t* size) {
41  // Write everything to the file before telling the manifest so that the
42  // file will exist on disk.
43  std::unique_ptr<File, FileCloser> file(File::Open(filename.c_str(), "w"));
44  if (!file) {
45  return Status(error::FILE_FAILURE, "Failed to open " + filename);
46  }
47 
48  buffer_->WriteTo(file.get(), size);
49  buffer_->Reset();
50  if (!file.release()->Close()) {
51  return Status(error::FILE_FAILURE, "Failed to close " + filename);
52  }
53 
54  return Status::OK;
55 }
56 
57 } // namespace webvtt
58 } // namespace media
59 } // namespace shaka
WebVttMuxer(const MuxerOptions &options)
Create a WebMMuxer object from MuxerOptions.
Definition: webvtt_muxer.cc:18
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66
This structure contains the list of configuration options for Muxer.
Definition: muxer_options.h:19