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