7 #include <packager/media/formats/webm/webm_muxer.h>
9 #include <absl/log/check.h>
11 #include <packager/macros/logging.h>
12 #include <packager/media/base/fourccs.h>
13 #include <packager/media/base/media_sample.h>
14 #include <packager/media/base/stream_info.h>
15 #include <packager/media/formats/webm/mkv_writer.h>
16 #include <packager/media/formats/webm/multi_segment_segmenter.h>
17 #include <packager/media/formats/webm/single_segment_segmenter.h>
18 #include <packager/media/formats/webm/two_pass_single_segment_segmenter.h>
25 WebMMuxer::~WebMMuxer() {}
27 Status WebMMuxer::InitializeMuxer() {
28 CHECK_EQ(streams().size(), 1U);
30 if (streams()[0]->is_encrypted() &&
31 streams()[0]->encryption_config().protection_scheme != FOURCC_cenc) {
32 LOG(ERROR) <<
"WebM does not support protection scheme other than 'cenc'.";
33 return Status(error::INVALID_ARGUMENT,
34 "WebM does not support protection scheme other than 'cenc'.");
37 if (!options().segment_template.empty()) {
38 segmenter_.reset(
new MultiSegmentSegmenter(options()));
40 segmenter_.reset(
new TwoPassSingleSegmentSegmenter(options()));
43 Status initialized = segmenter_->Initialize(
44 *streams()[0], progress_listener(), muxer_listener());
45 if (!initialized.ok())
48 FireOnMediaStartEvent();
52 Status WebMMuxer::Finalize() {
55 Status segmenter_finalized = segmenter_->Finalize();
57 if (!segmenter_finalized.ok())
58 return segmenter_finalized;
60 FireOnMediaEndEvent();
65 Status WebMMuxer::AddMediaSample(
size_t stream_id,
const MediaSample& sample) {
67 DCHECK_EQ(stream_id, 0u);
68 if (sample.pts() < 0) {
69 LOG(ERROR) <<
"Seeing negative timestamp " << sample.pts();
70 return Status(error::MUXER_FAILURE,
"Unsupported negative timestamp.");
72 return segmenter_->AddSample(sample);
75 Status WebMMuxer::FinalizeSegment(
size_t stream_id,
76 const SegmentInfo& segment_info) {
78 DCHECK_EQ(stream_id, 0u);
80 if (segment_info.key_rotation_encryption_config) {
81 NOTIMPLEMENTED() <<
"Key rotation is not implemented for WebM.";
82 return Status(error::UNIMPLEMENTED,
83 "Key rotation is not implemented for WebM");
85 return segmenter_->FinalizeSegment(
86 segment_info.start_timestamp, segment_info.duration,
87 segment_info.is_subsegment, segment_info.segment_number);
90 void WebMMuxer::FireOnMediaStartEvent() {
91 if (!muxer_listener())
94 DCHECK(!streams().empty()) <<
"Media started without a stream.";
96 const int32_t timescale = streams().front()->time_scale();
97 muxer_listener()->
OnMediaStart(options(), *streams().front(), timescale,
98 MuxerListener::kContainerWebM);
101 void WebMMuxer::FireOnMediaEndEvent() {
102 if (!muxer_listener())
105 MuxerListener::MediaRanges media_range;
107 uint64_t init_range_start = 0;
108 uint64_t init_range_end = 0;
109 const bool has_init_range =
110 segmenter_->GetInitRangeStartAndEnd(&init_range_start, &init_range_end);
111 if (has_init_range) {
113 r.start = init_range_start;
114 r.end = init_range_end;
115 media_range.init_range = r;
118 uint64_t index_range_start = 0;
119 uint64_t index_range_end = 0;
120 const bool has_index_range = segmenter_->GetIndexRangeStartAndEnd(
121 &index_range_start, &index_range_end);
122 if (has_index_range) {
124 r.start = index_range_start;
125 r.end = index_range_end;
126 media_range.index_range = r;
129 media_range.subsegment_ranges = segmenter_->GetSegmentRanges();
131 const float duration_seconds = segmenter_->GetDurationInSeconds();
132 muxer_listener()->
OnMediaEnd(media_range, duration_seconds);
All the methods that are virtual are virtual for mocking.