7 #include "packager/media/formats/mp4/segmenter.h"
11 #include "packager/base/logging.h"
12 #include "packager/media/base/buffer_writer.h"
13 #include "packager/media/base/media_sample.h"
14 #include "packager/media/base/muxer_options.h"
15 #include "packager/media/base/muxer_util.h"
16 #include "packager/media/base/stream_info.h"
17 #include "packager/media/chunking/chunking_handler.h"
18 #include "packager/media/event/progress_listener.h"
19 #include "packager/media/formats/mp4/box_definitions.h"
20 #include "packager/media/formats/mp4/fragmenter.h"
21 #include "packager/version/version.h"
29 uint64_t Rescale(uint64_t time_in_old_scale,
32 return static_cast<double>(time_in_old_scale) / old_scale * new_scale;
37 Segmenter::Segmenter(
const MuxerOptions& options,
38 std::unique_ptr<FileType> ftyp,
39 std::unique_ptr<Movie> moov)
41 ftyp_(std::move(ftyp)),
42 moov_(std::move(moov)),
43 moof_(new MovieFragment()),
44 fragment_buffer_(new BufferWriter()),
45 sidx_(new SegmentIndex()) {}
47 Segmenter::~Segmenter() {}
50 const std::vector<std::shared_ptr<StreamInfo>>& streams,
53 DCHECK_LT(0u, streams.size());
54 muxer_listener_ = muxer_listener;
55 progress_listener_ = progress_listener;
56 moof_->header.sequence_number = 0;
58 moof_->tracks.resize(streams.size());
59 fragmenters_.resize(streams.size());
60 stream_durations_.resize(streams.size());
62 for (uint32_t i = 0; i < streams.size(); ++i) {
63 moof_->tracks[i].header.track_id = i + 1;
64 if (streams[i]->stream_type() == kStreamVideo) {
66 if (sidx_->reference_id == 0)
67 sidx_->reference_id = i + 1;
69 fragmenters_[i].reset(
new Fragmenter(streams[i], &moof_->tracks[i]));
73 for (uint32_t i = 0; i < streams.size(); ++i)
74 fragmenters_[i]->set_use_decoding_timestamp_in_timeline(
true);
78 if (sidx_->reference_id == 0)
79 sidx_->reference_id = 1;
80 sidx_->timescale = streams[GetReferenceStreamId()]->time_scale();
83 progress_target_ = streams[GetReferenceStreamId()]->duration();
86 moov_->header.timescale = sidx_->timescale;
87 moof_->header.sequence_number = 1;
90 const std::string version = GetPackagerVersion();
91 if (!version.empty()) {
92 moov_->metadata.handler.handler_type = FOURCC_ID32;
93 moov_->metadata.id3v2.language.code =
"eng";
94 moov_->metadata.id3v2.private_frame.owner = GetPackagerProjectUrl();
95 moov_->metadata.id3v2.private_frame.value = version;
97 return DoInitialize();
104 moov_->extends.header.fragment_duration = 0;
105 for (
size_t i = 0; i < stream_durations_.size(); ++i) {
107 Rescale(stream_durations_[i], moov_->tracks[i].media.header.timescale,
108 moov_->header.timescale);
109 if (duration > moov_->extends.header.fragment_duration)
110 moov_->extends.header.fragment_duration = duration;
116 std::shared_ptr<MediaSample> sample) {
118 if (moov_->extends.tracks[stream_id].default_sample_duration == 0) {
119 moov_->extends.tracks[stream_id].default_sample_duration =
123 DCHECK_LT(stream_id, fragmenters_.size());
124 Fragmenter* fragmenter = fragmenters_[stream_id].get();
125 if (fragmenter->fragment_finalized()) {
126 return Status(error::FRAGMENT_FINALIZED,
127 "Current fragment is finalized already.");
130 Status status = fragmenter->AddSample(sample);
134 if (sample_duration_ == 0)
135 sample_duration_ = sample->duration();
136 stream_durations_[stream_id] += sample->duration();
141 std::shared_ptr<SegmentInfo> segment_info) {
142 if (segment_info->key_rotation_encryption_config) {
143 FinalizeFragmentForKeyRotation(
144 stream_id, segment_info->is_encrypted,
145 *segment_info->key_rotation_encryption_config);
148 DCHECK_LT(stream_id, fragmenters_.size());
149 Fragmenter* fragmenter = fragmenters_[stream_id].get();
151 Status status = fragmenter->FinalizeFragment();
156 for (
const std::unique_ptr<Fragmenter>& fragmenter : fragmenters_) {
157 if (!fragmenter->fragment_finalized())
166 uint64_t next_traf_position = moof_->HeaderSize() + moof_->header.box_size();
167 for (
size_t i = 0; i < moof_->tracks.size(); ++i) {
169 if (traf.auxiliary_offset.offsets.size() > 0) {
170 DCHECK_EQ(traf.auxiliary_offset.offsets.size(), 1u);
171 DCHECK(!traf.sample_encryption.sample_encryption_entries.empty());
173 next_traf_position += traf.
box_size();
176 traf.auxiliary_offset.offsets[0] =
177 next_traf_position - traf.sample_encryption.
box_size() +
181 traf.runs[0].data_offset = data_offset + mdat.data_size;
182 mdat.data_size +=
static_cast<uint32_t
>(fragmenters_[i]->data()->Size());
186 sidx_->references.resize(sidx_->references.size() + 1);
187 fragmenters_[GetReferenceStreamId()]->GenerateSegmentReference(
188 &sidx_->references[sidx_->references.size() - 1]);
189 sidx_->references[sidx_->references.size() - 1].referenced_size =
190 data_offset + mdat.data_size;
193 moof_->Write(fragment_buffer_.get());
195 for (
const std::unique_ptr<Fragmenter>& fragmenter : fragmenters_)
196 fragment_buffer_->AppendBuffer(*fragmenter->data());
199 ++moof_->header.sequence_number;
201 for (std::unique_ptr<Fragmenter>& fragmenter : fragmenters_)
202 fragmenter->ClearFragmentFinalized();
203 if (!segment_info->is_subsegment) {
204 Status status = DoFinalizeSegment();
206 sidx_->references.clear();
212 uint32_t Segmenter::GetReferenceTimeScale()
const {
213 return moov_->header.timescale;
216 double Segmenter::GetDuration()
const {
217 uint64_t duration = moov_->extends.header.fragment_duration;
222 return static_cast<double>(duration) / moov_->header.timescale;
226 accumulated_progress_ += progress;
228 if (!progress_listener_)
return;
229 if (progress_target_ == 0)
return;
233 if (accumulated_progress_ >= progress_target_) {
236 progress_listener_->
OnProgress(static_cast<double>(accumulated_progress_) /
241 void Segmenter::SetComplete() {
242 if (!progress_listener_)
return;
246 uint32_t Segmenter::GetReferenceStreamId() {
248 return sidx_->reference_id - 1;
251 void Segmenter::FinalizeFragmentForKeyRotation(
253 bool fragment_encrypted,
255 if (options_.
mp4_params.include_pssh_in_stream) {
256 const std::vector<ProtectionSystemSpecificInfo>& system_info =
257 encryption_config.key_system_info;
258 moof_->pssh.resize(system_info.size());
259 for (
size_t i = 0; i < system_info.size(); i++)
260 moof_->pssh[i].raw_box = system_info[i].CreateBox();
263 <<
"Key rotation and no pssh in stream may not work well together.";
273 if (!fragment_encrypted)
276 DCHECK_LT(stream_id, moof_->tracks.size());
277 TrackFragment& traf = moof_->tracks[stream_id];
278 traf.sample_group_descriptions.resize(traf.sample_group_descriptions.size() +
280 SampleGroupDescription& sample_group_description =
281 traf.sample_group_descriptions.back();
282 sample_group_description.grouping_type = FOURCC_seig;
284 sample_group_description.cenc_sample_encryption_info_entries.resize(1);
285 CencSampleEncryptionInfoEntry& sample_group_entry =
286 sample_group_description.cenc_sample_encryption_info_entries.back();
287 sample_group_entry.is_protected = 1;
288 sample_group_entry.per_sample_iv_size = encryption_config.per_sample_iv_size;
289 sample_group_entry.constant_iv = encryption_config.constant_iv;
290 sample_group_entry.crypt_byte_block = encryption_config.crypt_byte_block;
291 sample_group_entry.skip_byte_block = encryption_config.skip_byte_block;
292 sample_group_entry.key_id = encryption_config.key_id;
bool use_decoding_timestamp_in_timeline