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
|
|
|
|
|
|
|
|
#include "packager/media/formats/webvtt/webvtt_muxer.h"
|
|
|
|
|
|
|
|
#include "packager/file/file.h"
|
|
|
|
#include "packager/file/file_closer.h"
|
2020-08-24 22:23:15 +00:00
|
|
|
#include "packager/media/formats/webvtt/webvtt_utils.h"
|
2020-07-30 21:26:45 +00:00
|
|
|
#include "packager/status_macros.h"
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer_->WriteTo(file.get());
|
|
|
|
buffer_->Reset();
|
|
|
|
if (!file.release()->Close()) {
|
|
|
|
return Status(error::FILE_FAILURE, "Failed to close " + filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size) {
|
|
|
|
*size = File::GetFileSize(filename.c_str());
|
|
|
|
}
|
|
|
|
return Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace webvtt
|
|
|
|
} // namespace media
|
|
|
|
} // namespace shaka
|