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 std::unique_ptr<MkvWriter> writer(
new MkvWriter);
41 Status status = writer->Open(options().output_file_name);
46 segmenter_.reset(
new MultiSegmentSegmenter(options()));
48 segmenter_.reset(
new TwoPassSingleSegmentSegmenter(options()));
51 Status initialized = segmenter_->Initialize(
52 std::move(writer), streams()[0].
get(), progress_listener(),
53 muxer_listener(), encryption_key_source(), max_sd_pixels(),
54 max_hd_pixels(), max_uhd1_pixels(), clear_lead_in_seconds());
56 if (!initialized.ok())
59 FireOnMediaStartEvent();
63 Status WebMMuxer::Finalize() {
65 Status segmenter_finalized = segmenter_->Finalize();
67 if (!segmenter_finalized.ok())
68 return segmenter_finalized;
70 FireOnMediaEndEvent();
75 Status WebMMuxer::DoAddSample(std::shared_ptr<MediaSample> sample) {
77 return segmenter_->AddSample(sample);
80 void WebMMuxer::FireOnMediaStartEvent() {
81 if (!muxer_listener())
84 DCHECK(!streams().empty()) <<
"Media started without a stream.";
86 const uint32_t timescale = streams().front()->time_scale();
87 muxer_listener()->
OnMediaStart(options(), *streams().front(), timescale,
88 MuxerListener::kContainerWebM);
91 void WebMMuxer::FireOnMediaEndEvent() {
92 if (!muxer_listener())
95 uint64_t init_range_start = 0;
96 uint64_t init_range_end = 0;
97 const bool has_init_range =
98 segmenter_->GetInitRangeStartAndEnd(&init_range_start, &init_range_end);
100 uint64_t index_range_start = 0;
101 uint64_t index_range_end = 0;
102 const bool has_index_range = segmenter_->GetIndexRangeStartAndEnd(
103 &index_range_start, &index_range_end);
105 const float duration_seconds = segmenter_->GetDuration();
107 const int64_t file_size =
109 if (file_size <= 0) {
110 LOG(ERROR) <<
"Invalid file size: " << file_size;
114 muxer_listener()->
OnMediaEnd(has_init_range, init_range_start, init_range_end,
115 has_index_range, index_range_start,
116 index_range_end, duration_seconds, file_size);