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 (stream_info_->is_encrypted()) {
161 Status status = FinalizeFragmentForEncryption();
167 traf_->runs[0].sample_count =
168 static_cast<uint32_t
>(traf_->runs[0].sample_sizes.size());
170 &traf_->header.default_sample_duration)) {
171 traf_->header.flags |=
172 TrackFragmentHeader::kDefaultSampleDurationPresentMask;
174 traf_->runs[0].flags |= TrackFragmentRun::kSampleDurationPresentMask;
177 &traf_->header.default_sample_size)) {
178 traf_->header.flags |= TrackFragmentHeader::kDefaultSampleSizePresentMask;
180 traf_->runs[0].flags |= TrackFragmentRun::kSampleSizePresentMask;
183 &traf_->header.default_sample_flags)) {
184 traf_->header.flags |= TrackFragmentHeader::kDefaultSampleFlagsPresentMask;
186 traf_->runs[0].flags |= TrackFragmentRun::kSampleFlagsPresentMask;
194 DCHECK_EQ(traf_->sample_to_groups.size(), 0u);
195 if (seek_preroll_ > 0) {
196 traf_->sample_to_groups.resize(traf_->sample_to_groups.size() + 1);
197 SampleToGroup& sample_to_group = traf_->sample_to_groups.back();
198 sample_to_group.grouping_type = FOURCC_roll;
200 sample_to_group.entries.resize(1);
202 sample_to_group_entry.sample_count = traf_->runs[0].sample_count;
203 sample_to_group_entry.group_description_index =
204 SampleToGroupEntry::kTrackGroupDescriptionIndexBase + 1;
206 for (
const auto& sample_group_description :
207 traf_->sample_group_descriptions) {
208 traf_->sample_to_groups.resize(traf_->sample_to_groups.size() + 1);
209 SampleToGroup& sample_to_group = traf_->sample_to_groups.back();
210 sample_to_group.grouping_type = sample_group_description.grouping_type;
212 sample_to_group.entries.resize(1);
214 sample_to_group_entry.sample_count = traf_->runs[0].sample_count;
215 sample_to_group_entry.group_description_index =
216 SampleToGroupEntry::kTrackFragmentGroupDescriptionIndexBase + 1;
219 fragment_finalized_ =
true;
220 fragment_initialized_ =
false;
226 reference->reference_type =
false;
227 reference->subsegment_duration = fragment_duration_;
228 reference->starts_with_sap = StartsWithSAP();
229 if (kInvalidTime == first_sap_time_) {
230 reference->sap_type = SegmentReference::TypeUnknown;
231 reference->sap_delta_time = 0;
233 reference->sap_type = SegmentReference::Type1;
234 reference->sap_delta_time = first_sap_time_ - earliest_presentation_time_;
236 reference->earliest_presentation_time = earliest_presentation_time_;
239 Status Fragmenter::FinalizeFragmentForEncryption() {
241 if (sample_encryption.sample_encryption_entries.empty()) {
245 const uint32_t kClearSampleDescriptionIndex = 2;
246 traf_->header.sample_description_index = kClearSampleDescriptionIndex;
249 if (sample_encryption.sample_encryption_entries.size() !=
250 traf_->runs[0].sample_sizes.size()) {
251 LOG(ERROR) <<
"Partially encrypted segment is not supported";
252 return Status(error::MUXER_FAILURE,
253 "Partially encrypted segment is not supported.");
257 sample_encryption.sample_encryption_entries.front();
258 const bool use_subsample_encryption =
259 !sample_encryption_entry.subsamples.empty();
260 if (use_subsample_encryption)
261 traf_->sample_encryption.flags |= SampleEncryption::kUseSubsampleEncryption;
262 traf_->sample_encryption.iv_size =
static_cast<uint8_t
>(
263 sample_encryption_entry.initialization_vector.size());
266 traf_->auxiliary_offset.offsets.push_back(0);
270 saiz.sample_count =
static_cast<uint32_t
>(saiz.sample_info_sizes.size());
271 DCHECK_EQ(saiz.sample_info_sizes.size(),
272 traf_->sample_encryption.sample_encryption_entries.size());
274 &saiz.default_sample_info_size)) {
275 saiz.default_sample_info_size = 0;
280 if (saiz.default_sample_info_size == 0 && saiz.sample_info_sizes.empty()) {
281 DCHECK(!use_subsample_encryption);
285 saiz.sample_count = 0;
286 traf_->auxiliary_offset.offsets.clear();
291 bool Fragmenter::StartsWithSAP()
const {
292 DCHECK(!traf_->runs.empty());
293 uint32_t start_sample_flag;
294 if (traf_->runs[0].flags & TrackFragmentRun::kSampleFlagsPresentMask) {
295 DCHECK(!traf_->runs[0].sample_flags.empty());
296 start_sample_flag = traf_->runs[0].sample_flags[0];
298 DCHECK(traf_->header.flags &
299 TrackFragmentHeader::kDefaultSampleFlagsPresentMask);
300 start_sample_flag = traf_->header.default_sample_flags;
302 return (start_sample_flag & TrackFragmentHeader::kNonKeySampleMask) == 0;
All the methods that are virtual are virtual for mocking.