2020-07-30 21:26:45 +00:00
|
|
|
// Copyright 2020 Google LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/media/formats/webvtt/webvtt_muxer.h>
|
2020-07-30 21:26:45 +00:00
|
|
|
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/file/file.h>
|
|
|
|
#include <packager/file/file_closer.h>
|
|
|
|
#include <packager/media/formats/webvtt/webvtt_utils.h>
|
|
|
|
#include <packager/status/status_macros.h>
|
2020-07-30 21:26:45 +00:00
|
|
|
|
|
|
|
namespace shaka {
|
|
|
|
namespace media {
|
|
|
|
namespace webvtt {
|
|
|
|
|
2020-10-08 21:46:37 +00:00
|
|
|
WebVttMuxer::WebVttMuxer(const MuxerOptions& options) : TextMuxer(options) {}
|
2020-07-30 21:26:45 +00:00
|
|
|
WebVttMuxer::~WebVttMuxer() {}
|
|
|
|
|
2020-10-08 21:46:37 +00:00
|
|
|
Status WebVttMuxer::InitializeStream(TextStreamInfo* stream) {
|
|
|
|
stream->set_codec(kCodecWebVtt);
|
|
|
|
stream->set_codec_string("wvtt");
|
2020-07-30 21:26:45 +00:00
|
|
|
|
2020-08-26 21:21:09 +00:00
|
|
|
const std::string preamble = WebVttGetPreamble(*stream);
|
|
|
|
buffer_.reset(new WebVttFileBuffer(
|
|
|
|
options().transport_stream_timestamp_offset_ms, preamble));
|
2020-07-30 21:26:45 +00:00
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
2020-10-08 21:46:37 +00:00
|
|
|
Status WebVttMuxer::AddTextSampleInternal(const TextSample& sample) {
|
2020-08-24 22:23:15 +00:00
|
|
|
if (sample.id().find('\n') != std::string::npos) {
|
|
|
|
return Status(error::MUXER_FAILURE, "Text id cannot contain newlines");
|
2020-07-30 21:26:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buffer_->Append(sample);
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status WebVttMuxer::WriteToFile(const std::string& filename, uint64_t* size) {
|
|
|
|
// Write everything to the file before telling the manifest so that the
|
|
|
|
// file will exist on disk.
|
|
|
|
std::unique_ptr<File, FileCloser> file(File::Open(filename.c_str(), "w"));
|
|
|
|
if (!file) {
|
|
|
|
return Status(error::FILE_FAILURE, "Failed to open " + filename);
|
|
|
|
}
|
|
|
|
|
2021-02-04 22:02:33 +00:00
|
|
|
buffer_->WriteTo(file.get(), size);
|
2020-07-30 21:26:45 +00:00
|
|
|
buffer_->Reset();
|
|
|
|
if (!file.release()->Close()) {
|
|
|
|
return Status(error::FILE_FAILURE, "Failed to close " + filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace webvtt
|
|
|
|
} // namespace media
|
|
|
|
} // namespace shaka
|