7 #include "packager/media/formats/mp4/fragmenter.h"
12 #include "packager/media/base/audio_stream_info.h"
13 #include "packager/media/base/buffer_writer.h"
14 #include "packager/media/base/media_sample.h"
15 #include "packager/media/formats/mp4/box_definitions.h"
16 #include "packager/media/formats/mp4/key_frame_info.h"
17 #include "packager/status_macros.h"
24 const int64_t kInvalidTime = std::numeric_limits<int64_t>::max();
26 int64_t GetSeekPreroll(
const StreamInfo& stream_info) {
27 if (stream_info.stream_type() != kStreamAudio)
29 const AudioStreamInfo& audio_stream_info =
30 static_cast<const AudioStreamInfo&
>(stream_info);
31 return audio_stream_info.seek_preroll_ns();
34 void NewSampleEncryptionEntry(
const DecryptConfig& decrypt_config,
36 TrackFragment* traf) {
37 SampleEncryption& sample_encryption = traf->sample_encryption;
38 SampleEncryptionEntry sample_encryption_entry;
40 sample_encryption_entry.initialization_vector = decrypt_config.iv();
41 sample_encryption_entry.subsamples = decrypt_config.subsamples();
42 sample_encryption.sample_encryption_entries.push_back(
43 sample_encryption_entry);
44 traf->auxiliary_size.sample_info_sizes.push_back(
45 sample_encryption_entry.ComputeSize());
52 int64_t edit_list_offset)
53 : stream_info_(std::move(stream_info)),
55 edit_list_offset_(edit_list_offset),
56 seek_preroll_(GetSeekPreroll(*stream_info_)),
57 earliest_presentation_time_(kInvalidTime),
58 first_sap_time_(kInvalidTime) {
63 Fragmenter::~Fragmenter() {}
66 const int64_t pts = sample.pts();
67 const int64_t dts = sample.dts();
68 const int64_t duration = sample.duration();
70 LOG(WARNING) <<
"Unexpected sample with zero duration @ dts " << dts;
72 if (!fragment_initialized_)
75 if (sample.side_data_size() > 0)
76 LOG(WARNING) <<
"MP4 samples do not support side data. Side data ignored.";
79 traf_->runs[0].sample_sizes.push_back(
80 static_cast<uint32_t
>(sample.data_size()));
81 traf_->runs[0].sample_durations.push_back(duration);
82 traf_->runs[0].sample_flags.push_back(
83 sample.is_key_frame() ? 0 : TrackFragmentHeader::kNonKeySampleMask);
85 if (sample.decrypt_config()) {
86 NewSampleEncryptionEntry(
87 *sample.decrypt_config(),
88 !stream_info_->encryption_config().constant_iv.empty(), traf_);
91 if (stream_info_->stream_type() == StreamType::kStreamVideo &&
92 sample.is_key_frame()) {
93 key_frame_infos_.push_back(
94 {
static_cast<uint64_t
>(pts), data_->Size(), sample.data_size()});
97 data_->AppendArray(sample.data(), sample.data_size());
99 traf_->runs[0].sample_composition_time_offsets.push_back(pts - dts);
101 traf_->runs[0].flags |= TrackFragmentRun::kSampleCompTimeOffsetsPresentMask;
106 const int64_t end_pts = pts + duration;
109 fragment_duration_ += end_pts;
111 earliest_presentation_time_ = 0;
112 if (sample.is_key_frame())
116 fragment_duration_ += duration;
118 if (earliest_presentation_time_ > pts)
119 earliest_presentation_time_ = pts;
121 if (sample.is_key_frame()) {
122 if (first_sap_time_ == kInvalidTime)
123 first_sap_time_ = pts;
130 fragment_initialized_ =
true;
131 fragment_finalized_ =
false;
136 const int64_t dts_before_edit = first_sample_dts + edit_list_offset_;
137 traf_->decode_time.decode_time = dts_before_edit;
140 traf_->runs.resize(1);
141 traf_->runs[0].flags = TrackFragmentRun::kDataOffsetPresentMask;
142 traf_->auxiliary_size.sample_info_sizes.clear();
143 traf_->auxiliary_offset.offsets.clear();
144 traf_->sample_encryption.sample_encryption_entries.clear();
145 traf_->sample_group_descriptions.clear();
146 traf_->sample_to_groups.clear();
147 traf_->header.sample_description_index = 1;
148 traf_->header.flags = TrackFragmentHeader::kDefaultBaseIsMoofMask |
149 TrackFragmentHeader::kSampleDescriptionIndexPresentMask;
151 fragment_duration_ = 0;
152 earliest_presentation_time_ = kInvalidTime;
153 first_sap_time_ = kInvalidTime;
155 key_frame_infos_.clear();
160 if (!fragment_initialized_)
163 if (stream_info_->is_encrypted()) {
164 Status status = FinalizeFragmentForEncryption();
170 traf_->runs[0].sample_count =
171 static_cast<uint32_t
>(traf_->runs[0].sample_sizes.size());
173 &traf_->header.default_sample_duration)) {
174 traf_->header.flags |=
175 TrackFragmentHeader::kDefaultSampleDurationPresentMask;
177 traf_->runs[0].flags |= TrackFragmentRun::kSampleDurationPresentMask;
180 &traf_->header.default_sample_size)) {
181 traf_->header.flags |= TrackFragmentHeader::kDefaultSampleSizePresentMask;
183 traf_->runs[0].flags |= TrackFragmentRun::kSampleSizePresentMask;
186 &traf_->header.default_sample_flags)) {
187 traf_->header.flags |= TrackFragmentHeader::kDefaultSampleFlagsPresentMask;
189 traf_->runs[0].flags |= TrackFragmentRun::kSampleFlagsPresentMask;
197 DCHECK_EQ(traf_->sample_to_groups.size(), 0u);
198 if (seek_preroll_ > 0) {
199 traf_->sample_to_groups.resize(traf_->sample_to_groups.size() + 1);
200 SampleToGroup& sample_to_group = traf_->sample_to_groups.back();
201 sample_to_group.grouping_type = FOURCC_roll;
203 sample_to_group.entries.resize(1);
205 sample_to_group_entry.sample_count = traf_->runs[0].sample_count;
206 sample_to_group_entry.group_description_index =
207 SampleToGroupEntry::kTrackGroupDescriptionIndexBase + 1;
209 for (
const auto& sample_group_description :
210 traf_->sample_group_descriptions) {
211 traf_->sample_to_groups.resize(traf_->sample_to_groups.size() + 1);
212 SampleToGroup& sample_to_group = traf_->sample_to_groups.back();
213 sample_to_group.grouping_type = sample_group_description.grouping_type;
215 sample_to_group.entries.resize(1);
217 sample_to_group_entry.sample_count = traf_->runs[0].sample_count;
218 sample_to_group_entry.group_description_index =
219 SampleToGroupEntry::kTrackFragmentGroupDescriptionIndexBase + 1;
222 fragment_finalized_ =
true;
223 fragment_initialized_ =
false;
229 reference->reference_type =
false;
230 reference->subsegment_duration = fragment_duration_;
231 reference->starts_with_sap = StartsWithSAP();
232 if (kInvalidTime == first_sap_time_) {
233 reference->sap_type = SegmentReference::TypeUnknown;
234 reference->sap_delta_time = 0;
236 reference->sap_type = SegmentReference::Type1;
237 reference->sap_delta_time = first_sap_time_ - earliest_presentation_time_;
239 reference->earliest_presentation_time = earliest_presentation_time_;
242 Status Fragmenter::FinalizeFragmentForEncryption() {
244 if (sample_encryption.sample_encryption_entries.empty()) {
248 const uint32_t kClearSampleDescriptionIndex = 2;
249 traf_->header.sample_description_index = kClearSampleDescriptionIndex;
252 if (sample_encryption.sample_encryption_entries.size() !=
253 traf_->runs[0].sample_sizes.size()) {
254 LOG(ERROR) <<
"Partially encrypted segment is not supported";
255 return Status(error::MUXER_FAILURE,
256 "Partially encrypted segment is not supported.");
259 const SampleEncryptionEntry& sample_encryption_entry =
260 sample_encryption.sample_encryption_entries.front();
261 const bool use_subsample_encryption =
262 !sample_encryption_entry.subsamples.empty();
263 if (use_subsample_encryption)
264 traf_->sample_encryption.flags |= SampleEncryption::kUseSubsampleEncryption;
265 traf_->sample_encryption.iv_size =
static_cast<uint8_t
>(
266 sample_encryption_entry.initialization_vector.size());
269 traf_->auxiliary_offset.offsets.push_back(0);
272 SampleAuxiliaryInformationSize& saiz = traf_->auxiliary_size;
273 saiz.sample_count =
static_cast<uint32_t
>(saiz.sample_info_sizes.size());
274 DCHECK_EQ(saiz.sample_info_sizes.size(),
275 traf_->sample_encryption.sample_encryption_entries.size());
277 &saiz.default_sample_info_size)) {
278 saiz.default_sample_info_size = 0;
283 if (saiz.default_sample_info_size == 0 && saiz.sample_info_sizes.empty()) {
284 DCHECK(!use_subsample_encryption);
288 saiz.sample_count = 0;
289 traf_->auxiliary_offset.offsets.clear();
294 bool Fragmenter::StartsWithSAP()
const {
295 DCHECK(!traf_->runs.empty());
296 uint32_t start_sample_flag;
297 if (traf_->runs[0].flags & TrackFragmentRun::kSampleFlagsPresentMask) {
298 DCHECK(!traf_->runs[0].sample_flags.empty());
299 start_sample_flag = traf_->runs[0].sample_flags[0];
301 DCHECK(traf_->header.flags &
302 TrackFragmentHeader::kDefaultSampleFlagsPresentMask);
303 start_sample_flag = traf_->header.default_sample_flags;
305 return (start_sample_flag & TrackFragmentHeader::kNonKeySampleMask) == 0;