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/id3_tag.h" 14 #include "packager/media/base/media_sample.h" 15 #include "packager/media/base/muxer_options.h" 16 #include "packager/media/base/muxer_util.h" 17 #include "packager/media/base/stream_info.h" 18 #include "packager/media/chunking/chunking_handler.h" 19 #include "packager/media/event/progress_listener.h" 20 #include "packager/media/formats/mp4/box_definitions.h" 21 #include "packager/media/formats/mp4/fragmenter.h" 22 #include "packager/media/formats/mp4/key_frame_info.h" 23 #include "packager/version/version.h" 31 uint64_t Rescale(uint64_t time_in_old_scale,
34 return static_cast<double>(time_in_old_scale) / old_scale * new_scale;
39 Segmenter::Segmenter(
const MuxerOptions& options,
40 std::unique_ptr<FileType> ftyp,
41 std::unique_ptr<Movie> moov)
43 ftyp_(
std::move(ftyp)),
44 moov_(
std::move(moov)),
45 moof_(new MovieFragment()),
46 fragment_buffer_(new BufferWriter()),
47 sidx_(new SegmentIndex()) {}
49 Segmenter::~Segmenter() {}
52 const std::vector<std::shared_ptr<const StreamInfo>>& streams,
55 DCHECK_LT(0u, streams.size());
56 muxer_listener_ = muxer_listener;
57 progress_listener_ = progress_listener;
58 moof_->header.sequence_number = 0;
60 moof_->tracks.resize(streams.size());
61 fragmenters_.resize(streams.size());
62 stream_durations_.resize(streams.size());
64 for (uint32_t i = 0; i < streams.size(); ++i) {
65 moof_->tracks[i].header.track_id = i + 1;
66 if (streams[i]->stream_type() == kStreamVideo) {
68 if (sidx_->reference_id == 0)
69 sidx_->reference_id = i + 1;
72 const EditList& edit_list = moov_->tracks[i].edit.list;
73 int64_t edit_list_offset = 0;
74 if (edit_list.edits.size() > 0) {
75 DCHECK_EQ(edit_list.edits.size(), 1u);
76 edit_list_offset = edit_list.edits.front().media_time;
79 fragmenters_[i].reset(
80 new Fragmenter(streams[i], &moof_->tracks[i], edit_list_offset));
84 if (sidx_->reference_id == 0)
85 sidx_->reference_id = 1;
86 sidx_->timescale = streams[GetReferenceStreamId()]->time_scale();
89 progress_target_ = streams[GetReferenceStreamId()]->duration();
92 moov_->header.timescale = sidx_->timescale;
93 moof_->header.sequence_number = 1;
96 const std::string version = GetPackagerVersion();
97 if (!version.empty()) {
98 moov_->metadata.handler.handler_type = FOURCC_ID32;
99 moov_->metadata.id3v2.language.code =
"eng";
103 CHECK(id3_tag.
WriteToVector(&moov_->metadata.id3v2.id3v2_data));
105 return DoInitialize();
112 moov_->extends.header.fragment_duration = 0;
113 for (
size_t i = 0; i < stream_durations_.size(); ++i) {
115 Rescale(stream_durations_[i], moov_->tracks[i].media.header.timescale,
116 moov_->header.timescale);
117 if (duration > moov_->extends.header.fragment_duration)
118 moov_->extends.header.fragment_duration = duration;
125 if (moov_->extends.tracks[stream_id].default_sample_duration == 0) {
126 moov_->extends.tracks[stream_id].default_sample_duration =
130 DCHECK_LT(stream_id, fragmenters_.size());
131 Fragmenter* fragmenter = fragmenters_[stream_id].get();
132 if (fragmenter->fragment_finalized()) {
133 return Status(error::FRAGMENT_FINALIZED,
134 "Current fragment is finalized already.");
137 Status status = fragmenter->AddSample(sample);
141 if (sample_duration_ == 0)
142 sample_duration_ = sample.duration();
143 stream_durations_[stream_id] += sample.duration();
149 if (segment_info.key_rotation_encryption_config) {
150 FinalizeFragmentForKeyRotation(
151 stream_id, segment_info.is_encrypted,
152 *segment_info.key_rotation_encryption_config);
155 DCHECK_LT(stream_id, fragmenters_.size());
156 Fragmenter* fragmenter = fragmenters_[stream_id].get();
158 Status status = fragmenter->FinalizeFragment();
163 for (
const std::unique_ptr<Fragmenter>& fragmenter : fragmenters_) {
164 if (!fragmenter->fragment_finalized())
173 uint64_t next_traf_position = moof_->HeaderSize() + moof_->header.box_size();
174 for (
size_t i = 0; i < moof_->tracks.size(); ++i) {
176 if (traf.auxiliary_offset.offsets.size() > 0) {
177 DCHECK_EQ(traf.auxiliary_offset.offsets.size(), 1u);
178 DCHECK(!traf.sample_encryption.sample_encryption_entries.empty());
180 next_traf_position += traf.
box_size();
183 traf.auxiliary_offset.offsets[0] =
184 next_traf_position - traf.sample_encryption.
box_size() +
188 traf.runs[0].data_offset = data_offset + mdat.data_size;
189 mdat.data_size +=
static_cast<uint32_t
>(fragmenters_[i]->data()->Size());
193 sidx_->references.resize(sidx_->references.size() + 1);
194 fragmenters_[GetReferenceStreamId()]->GenerateSegmentReference(
195 &sidx_->references[sidx_->references.size() - 1]);
196 sidx_->references[sidx_->references.size() - 1].referenced_size =
197 data_offset + mdat.data_size;
199 const uint64_t moof_start_offset = fragment_buffer_->Size();
202 moof_->Write(fragment_buffer_.get());
205 bool first_key_frame =
true;
206 for (
const std::unique_ptr<Fragmenter>& fragmenter : fragmenters_) {
211 if (!fragmenter->key_frame_infos().empty() && first_key_frame) {
213 fragmenter->key_frame_infos().front();
214 first_key_frame =
false;
215 key_frame_infos_.push_back(
216 {key_frame_info.timestamp, moof_start_offset,
217 fragment_buffer_->Size() - moof_start_offset + key_frame_info.size});
219 fragment_buffer_->AppendBuffer(*fragmenter->data());
223 ++moof_->header.sequence_number;
225 for (std::unique_ptr<Fragmenter>& fragmenter : fragmenters_)
226 fragmenter->ClearFragmentFinalized();
227 if (!segment_info.is_subsegment) {
228 Status status = DoFinalizeSegment();
230 sidx_->references.clear();
231 key_frame_infos_.clear();
237 uint32_t Segmenter::GetReferenceTimeScale()
const {
238 return moov_->header.timescale;
241 double Segmenter::GetDuration()
const {
242 uint64_t duration = moov_->extends.header.fragment_duration;
247 return static_cast<double>(duration) / moov_->header.timescale;
251 accumulated_progress_ += progress;
253 if (!progress_listener_)
return;
254 if (progress_target_ == 0)
return;
258 if (accumulated_progress_ >= progress_target_) {
259 progress_listener_->OnProgress(1.0);
261 progress_listener_->OnProgress(static_cast<double>(accumulated_progress_) /
266 void Segmenter::SetComplete() {
267 if (!progress_listener_)
return;
268 progress_listener_->OnProgress(1.0);
271 uint32_t Segmenter::GetReferenceStreamId() {
273 return sidx_->reference_id - 1;
276 void Segmenter::FinalizeFragmentForKeyRotation(
278 bool fragment_encrypted,
280 if (options_.mp4_params.include_pssh_in_stream) {
281 const std::vector<ProtectionSystemSpecificInfo>& system_info =
282 encryption_config.key_system_info;
283 moof_->pssh.resize(system_info.size());
284 for (
size_t i = 0; i < system_info.size(); i++)
285 moof_->pssh[i].raw_box = system_info[i].psshs;
288 <<
"Key rotation and no pssh in stream may not work well together.";
298 if (!fragment_encrypted)
301 DCHECK_LT(stream_id, moof_->tracks.size());
303 traf.sample_group_descriptions.resize(traf.sample_group_descriptions.size() +
306 traf.sample_group_descriptions.back();
307 sample_group_description.grouping_type = FOURCC_seig;
309 sample_group_description.cenc_sample_encryption_info_entries.resize(1);
311 sample_group_description.cenc_sample_encryption_info_entries.back();
312 sample_group_entry.is_protected = 1;
313 sample_group_entry.per_sample_iv_size = encryption_config.per_sample_iv_size;
314 sample_group_entry.constant_iv = encryption_config.constant_iv;
315 sample_group_entry.crypt_byte_block = encryption_config.crypt_byte_block;
316 sample_group_entry.skip_byte_block = encryption_config.skip_byte_block;
317 sample_group_entry.key_id = encryption_config.key_id;
All the methods that are virtual are virtual for mocking.