7 #include "packager/media/formats/webm/webm_muxer.h"
9 #include "packager/media/base/media_sample.h"
10 #include "packager/media/base/media_stream.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"
17 namespace edash_packager {
22 WebMMuxer::~WebMMuxer() {}
24 Status WebMMuxer::Initialize() {
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 scoped_ptr<MkvWriter> writer(
new MkvWriter);
34 Status status = writer->Open(options().output_file_name);
39 segmenter_.reset(
new MultiSegmentSegmenter(options()));
40 }
else if (writer->Seekable()) {
41 segmenter_.reset(
new SingleSegmentSegmenter(options()));
43 segmenter_.reset(
new TwoPassSingleSegmentSegmenter(options()));
46 Status initialized = segmenter_->Initialize(
47 writer.Pass(), streams()[0]->info().get(), progress_listener(),
48 muxer_listener(), encryption_key_source(), max_sd_pixels(),
49 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::DoAddSample(
const MediaStream* stream,
71 scoped_refptr<MediaSample> sample) {
73 DCHECK(stream == streams()[0]);
74 return segmenter_->AddSample(sample);
77 void WebMMuxer::FireOnMediaStartEvent() {
78 if (!muxer_listener())
81 DCHECK(!streams().empty()) <<
"Media started without a stream.";
83 const uint32_t timescale = streams().front()->info()->time_scale();
84 muxer_listener()->OnMediaStart(options(), *streams().front()->info(),
85 timescale, MuxerListener::kContainerWebM);
88 void WebMMuxer::FireOnMediaEndEvent() {
89 if (!muxer_listener())
92 uint32_t init_range_start = 0;
93 uint32_t init_range_end = 0;
94 const bool has_init_range =
95 segmenter_->GetInitRangeStartAndEnd(&init_range_start, &init_range_end);
97 uint32_t index_range_start = 0;
98 uint32_t index_range_end = 0;
99 const bool has_index_range = segmenter_->GetIndexRangeStartAndEnd(
100 &index_range_start, &index_range_end);
102 const float duration_seconds = segmenter_->GetDuration();
104 const int64_t file_size =
106 if (file_size <= 0) {
107 LOG(ERROR) <<
"Invalid file size: " << file_size;
111 muxer_listener()->OnMediaEnd(has_init_range, init_range_start, init_range_end,
112 has_index_range, index_range_start,
113 index_range_end, duration_seconds, file_size);