7 #include <packager/media/formats/ttml/ttml_to_mp4_handler.h>
9 #include <absl/log/check.h>
11 #include <packager/macros/status.h>
21 std::shared_ptr<MediaSample> CreateMediaSample(
const std::string& data,
24 DCHECK_GE(start_time, 0);
25 DCHECK_GT(duration, 0);
27 const bool kIsKeyFrame =
true;
30 reinterpret_cast<const uint8_t*
>(data.data()), data.size(), kIsKeyFrame);
31 sample->set_pts(start_time);
32 sample->set_dts(start_time);
33 sample->set_duration(duration);
40 Status TtmlToMp4Handler::InitializeInternal() {
44 Status TtmlToMp4Handler::Process(std::unique_ptr<StreamData> stream_data) {
45 switch (stream_data->stream_data_type) {
46 case StreamDataType::kStreamInfo:
47 return OnStreamInfo(std::move(stream_data));
48 case StreamDataType::kCueEvent:
49 return OnCueEvent(std::move(stream_data));
50 case StreamDataType::kSegmentInfo:
51 return OnSegmentInfo(std::move(stream_data));
52 case StreamDataType::kTextSample:
53 return OnTextSample(std::move(stream_data));
55 return Status(error::INTERNAL_ERROR,
56 "Invalid stream data type (" +
57 StreamDataTypeToString(stream_data->stream_data_type) +
58 ") for this TtmlToMp4 handler");
62 Status TtmlToMp4Handler::OnStreamInfo(std::unique_ptr<StreamData> stream_data) {
64 DCHECK(stream_data->stream_info);
66 auto clone = stream_data->stream_info->Clone();
67 clone->set_codec(kCodecTtml);
68 clone->set_codec_string(
"ttml");
70 if (clone->stream_type() != kStreamText)
71 return Status(error::MUXER_FAILURE,
"Incorrect stream type");
72 auto* text_stream =
static_cast<const TextStreamInfo*
>(clone.get());
73 generator_.Initialize(text_stream->regions(), text_stream->language(),
74 text_stream->time_scale());
77 StreamData::FromStreamInfo(stream_data->stream_index, std::move(clone)));
80 Status TtmlToMp4Handler::OnCueEvent(std::unique_ptr<StreamData> stream_data) {
82 DCHECK(stream_data->cue_event);
83 return Dispatch(std::move(stream_data));
86 Status TtmlToMp4Handler::OnSegmentInfo(
87 std::unique_ptr<StreamData> stream_data) {
89 DCHECK(stream_data->segment_info);
91 const auto& segment = stream_data->segment_info;
94 if (!generator_.Dump(&data))
95 return Status(error::INTERNAL_ERROR,
"Error generating XML");
100 CreateMediaSample(data, segment->start_timestamp, segment->duration)));
102 return Dispatch(std::move(stream_data));
105 Status TtmlToMp4Handler::OnTextSample(std::unique_ptr<StreamData> stream_data) {
107 DCHECK(stream_data->text_sample);
109 auto& sample = stream_data->text_sample;
113 if (sample->body().is_empty()) {
119 generator_.AddSample(*sample);
All the methods that are virtual are virtual for mocking.