7 #include "packager/media/formats/webm/webm_muxer.h"
9 #include "packager/media/base/fourccs.h"
10 #include "packager/media/base/media_sample.h"
11 #include "packager/media/base/stream_info.h"
12 #include "packager/media/formats/webm/mkv_writer.h"
13 #include "packager/media/formats/webm/multi_segment_segmenter.h"
14 #include "packager/media/formats/webm/single_segment_segmenter.h"
15 #include "packager/media/formats/webm/two_pass_single_segment_segmenter.h"
22 WebMMuxer::~WebMMuxer() {}
24 Status WebMMuxer::InitializeMuxer() {
25 CHECK_EQ(streams().size(), 1U);
27 if (crypto_period_duration_in_seconds() > 0) {
28 NOTIMPLEMENTED() <<
"Key rotation is not implemented for WebM";
29 return Status(error::UNIMPLEMENTED,
30 "Key rotation is not implemented for WebM");
33 if (encryption_key_source() && (protection_scheme() != FOURCC_cenc)) {
35 <<
"WebM does not support protection scheme other than 'cenc'.";
36 return Status(error::UNIMPLEMENTED,
37 "WebM does not support protection scheme other than 'cenc'.");
40 if (!options().segment_template.empty()) {
41 segmenter_.reset(
new MultiSegmentSegmenter(options()));
43 segmenter_.reset(
new TwoPassSingleSegmentSegmenter(options()));
46 Status initialized = segmenter_->Initialize(
47 streams()[0].
get(), progress_listener(), muxer_listener(),
48 encryption_key_source(), max_sd_pixels(), max_hd_pixels(),
49 max_uhd1_pixels(), clear_lead_in_seconds());
51 if (!initialized.ok())
54 FireOnMediaStartEvent();
58 Status WebMMuxer::Finalize() {
60 Status segmenter_finalized = segmenter_->Finalize();
62 if (!segmenter_finalized.ok())
63 return segmenter_finalized;
65 FireOnMediaEndEvent();
70 Status WebMMuxer::AddSample(
size_t stream_id,
71 std::shared_ptr<MediaSample> sample) {
73 DCHECK_EQ(stream_id, 0u);
74 return segmenter_->AddSample(sample);
77 Status WebMMuxer::FinalizeSegment(
size_t stream_id,
78 std::shared_ptr<SegmentInfo> segment_info) {
80 DCHECK_EQ(stream_id, 0u);
81 return segmenter_->FinalizeSegment(segment_info->start_timestamp,
82 segment_info->duration,
83 segment_info->is_subsegment);
86 void WebMMuxer::FireOnMediaStartEvent() {
87 if (!muxer_listener())
90 DCHECK(!streams().empty()) <<
"Media started without a stream.";
92 const uint32_t timescale = streams().front()->time_scale();
93 muxer_listener()->
OnMediaStart(options(), *streams().front(), timescale,
94 MuxerListener::kContainerWebM);
97 void WebMMuxer::FireOnMediaEndEvent() {
98 if (!muxer_listener())
101 uint64_t init_range_start = 0;
102 uint64_t init_range_end = 0;
103 const bool has_init_range =
104 segmenter_->GetInitRangeStartAndEnd(&init_range_start, &init_range_end);
106 uint64_t index_range_start = 0;
107 uint64_t index_range_end = 0;
108 const bool has_index_range = segmenter_->GetIndexRangeStartAndEnd(
109 &index_range_start, &index_range_end);
111 const float duration_seconds = segmenter_->GetDuration();
113 const int64_t file_size =
115 if (file_size <= 0) {
116 LOG(ERROR) <<
"Invalid file size: " << file_size;
120 muxer_listener()->
OnMediaEnd(has_init_range, init_range_start, init_range_end,
121 has_index_range, index_range_start,
122 index_range_end, duration_seconds, file_size);