7 #include "packager/media/formats/mp4/fragmenter.h" 11 #include "packager/media/base/audio_stream_info.h" 12 #include "packager/media/base/buffer_writer.h" 13 #include "packager/media/base/media_sample.h" 14 #include "packager/media/formats/mp4/box_definitions.h" 15 #include "packager/media/formats/mp4/key_frame_info.h" 22 const int64_t kInvalidTime = std::numeric_limits<int64_t>::max();
24 uint64_t GetSeekPreroll(
const StreamInfo& stream_info) {
25 if (stream_info.stream_type() != kStreamAudio)
27 const AudioStreamInfo& audio_stream_info =
28 static_cast<const AudioStreamInfo&
>(stream_info);
29 return audio_stream_info.seek_preroll_ns();
32 void NewSampleEncryptionEntry(
const DecryptConfig& decrypt_config,
34 TrackFragment* traf) {
35 SampleEncryption& sample_encryption = traf->sample_encryption;
36 SampleEncryptionEntry sample_encryption_entry;
38 sample_encryption_entry.initialization_vector = decrypt_config.iv();
39 sample_encryption_entry.subsamples = decrypt_config.subsamples();
40 sample_encryption.sample_encryption_entries.push_back(
41 sample_encryption_entry);
42 traf->auxiliary_size.sample_info_sizes.push_back(
43 sample_encryption_entry.ComputeSize());
50 : stream_info_(
std::move(stream_info)),
51 use_decoding_timestamp_in_timeline_(false),
53 seek_preroll_(GetSeekPreroll(*stream_info_)),
54 fragment_initialized_(false),
55 fragment_finalized_(false),
56 fragment_duration_(0),
57 earliest_presentation_time_(kInvalidTime),
58 first_sap_time_(kInvalidTime) {
63 Fragmenter::~Fragmenter() {}
66 if (sample.duration() == 0) {
67 LOG(WARNING) <<
"Unexpected sample with zero duration @ dts " 71 if (!fragment_initialized_) {
77 if (sample.side_data_size() > 0)
78 LOG(WARNING) <<
"MP4 samples do not support side data. Side data ignored.";
81 traf_->runs[0].sample_sizes.push_back(
82 static_cast<uint32_t>(sample.data_size()));
83 traf_->runs[0].sample_durations.push_back(sample.duration());
84 traf_->runs[0].sample_flags.push_back(
85 sample.is_key_frame() ? 0 : TrackFragmentHeader::kNonKeySampleMask);
87 if (sample.decrypt_config()) {
88 NewSampleEncryptionEntry(
89 *sample.decrypt_config(),
90 !stream_info_->encryption_config().constant_iv.empty(), traf_);
93 if (stream_info_->stream_type() == StreamType::kStreamVideo &&
94 sample.is_key_frame()) {
95 key_frame_infos_.push_back({
static_cast<uint64_t
>(sample.pts()),
96 data_->Size(), sample.data_size()});
99 data_->AppendArray(sample.data(), sample.data_size());
100 fragment_duration_ += sample.duration();
102 const int64_t pts = sample.pts();
103 const int64_t dts = sample.dts();
105 const int64_t timestamp = use_decoding_timestamp_in_timeline_ ? dts : pts;
108 if (earliest_presentation_time_ > timestamp)
109 earliest_presentation_time_ = timestamp;
111 traf_->runs[0].sample_composition_time_offsets.push_back(pts - dts);
113 traf_->runs[0].flags |= TrackFragmentRun::kSampleCompTimeOffsetsPresentMask;
115 if (sample.is_key_frame()) {
116 if (first_sap_time_ == kInvalidTime)
117 first_sap_time_ = pts;
123 fragment_initialized_ =
true;
124 fragment_finalized_ =
false;
125 traf_->decode_time.decode_time = first_sample_dts;
127 traf_->runs.resize(1);
128 traf_->runs[0].flags = TrackFragmentRun::kDataOffsetPresentMask;
129 traf_->auxiliary_size.sample_info_sizes.clear();
130 traf_->auxiliary_offset.offsets.clear();
131 traf_->sample_encryption.sample_encryption_entries.clear();
132 traf_->sample_group_descriptions.clear();
133 traf_->sample_to_groups.clear();
134 traf_->header.sample_description_index = 1;
135 traf_->header.flags = TrackFragmentHeader::kDefaultBaseIsMoofMask |
136 TrackFragmentHeader::kSampleDescriptionIndexPresentMask;
138 fragment_duration_ = 0;
139 earliest_presentation_time_ = kInvalidTime;
140 first_sap_time_ = kInvalidTime;
142 key_frame_infos_.clear();
147 if (stream_info_->is_encrypted()) {
148 Status status = FinalizeFragmentForEncryption();
154 traf_->runs[0].sample_count =
155 static_cast<uint32_t
>(traf_->runs[0].sample_sizes.size());
157 &traf_->header.default_sample_duration)) {
158 traf_->header.flags |=
159 TrackFragmentHeader::kDefaultSampleDurationPresentMask;
161 traf_->runs[0].flags |= TrackFragmentRun::kSampleDurationPresentMask;
164 &traf_->header.default_sample_size)) {
165 traf_->header.flags |= TrackFragmentHeader::kDefaultSampleSizePresentMask;
167 traf_->runs[0].flags |= TrackFragmentRun::kSampleSizePresentMask;
170 &traf_->header.default_sample_flags)) {
171 traf_->header.flags |= TrackFragmentHeader::kDefaultSampleFlagsPresentMask;
173 traf_->runs[0].flags |= TrackFragmentRun::kSampleFlagsPresentMask;
181 DCHECK_EQ(traf_->sample_to_groups.size(), 0u);
182 if (seek_preroll_ > 0) {
183 traf_->sample_to_groups.resize(traf_->sample_to_groups.size() + 1);
184 SampleToGroup& sample_to_group = traf_->sample_to_groups.back();
185 sample_to_group.grouping_type = FOURCC_roll;
187 sample_to_group.entries.resize(1);
189 sample_to_group_entry.sample_count = traf_->runs[0].sample_count;
190 sample_to_group_entry.group_description_index =
191 SampleToGroupEntry::kTrackGroupDescriptionIndexBase + 1;
193 for (
const auto& sample_group_description :
194 traf_->sample_group_descriptions) {
195 traf_->sample_to_groups.resize(traf_->sample_to_groups.size() + 1);
196 SampleToGroup& sample_to_group = traf_->sample_to_groups.back();
197 sample_to_group.grouping_type = sample_group_description.grouping_type;
199 sample_to_group.entries.resize(1);
201 sample_to_group_entry.sample_count = traf_->runs[0].sample_count;
202 sample_to_group_entry.group_description_index =
203 SampleToGroupEntry::kTrackFragmentGroupDescriptionIndexBase + 1;
206 fragment_finalized_ =
true;
207 fragment_initialized_ =
false;
213 reference->reference_type =
false;
214 reference->subsegment_duration = fragment_duration_;
215 reference->starts_with_sap = StartsWithSAP();
216 if (kInvalidTime == first_sap_time_) {
217 reference->sap_type = SegmentReference::TypeUnknown;
218 reference->sap_delta_time = 0;
220 reference->sap_type = SegmentReference::Type1;
221 reference->sap_delta_time = first_sap_time_ - earliest_presentation_time_;
223 reference->earliest_presentation_time = earliest_presentation_time_;
226 Status Fragmenter::FinalizeFragmentForEncryption() {
228 if (sample_encryption.sample_encryption_entries.empty()) {
232 const uint32_t kClearSampleDescriptionIndex = 2;
233 traf_->header.sample_description_index = kClearSampleDescriptionIndex;
236 if (sample_encryption.sample_encryption_entries.size() !=
237 traf_->runs[0].sample_sizes.size()) {
238 LOG(ERROR) <<
"Partially encrypted segment is not supported";
239 return Status(error::MUXER_FAILURE,
240 "Partially encrypted segment is not supported.");
244 sample_encryption.sample_encryption_entries.front();
245 const bool use_subsample_encryption =
246 !sample_encryption_entry.subsamples.empty();
247 if (use_subsample_encryption)
248 traf_->sample_encryption.flags |= SampleEncryption::kUseSubsampleEncryption;
249 traf_->sample_encryption.iv_size =
static_cast<uint8_t
>(
250 sample_encryption_entry.initialization_vector.size());
253 traf_->auxiliary_offset.offsets.push_back(0);
257 saiz.sample_count =
static_cast<uint32_t
>(saiz.sample_info_sizes.size());
258 DCHECK_EQ(saiz.sample_info_sizes.size(),
259 traf_->sample_encryption.sample_encryption_entries.size());
261 &saiz.default_sample_info_size)) {
262 saiz.default_sample_info_size = 0;
267 if (saiz.default_sample_info_size == 0 && saiz.sample_info_sizes.empty()) {
268 DCHECK(!use_subsample_encryption);
272 saiz.sample_count = 0;
273 traf_->auxiliary_offset.offsets.clear();
278 bool Fragmenter::StartsWithSAP() {
279 DCHECK(!traf_->runs.empty());
280 uint32_t start_sample_flag;
281 if (traf_->runs[0].flags & TrackFragmentRun::kSampleFlagsPresentMask) {
282 DCHECK(!traf_->runs[0].sample_flags.empty());
283 start_sample_flag = traf_->runs[0].sample_flags[0];
285 DCHECK(traf_->header.flags &
286 TrackFragmentHeader::kDefaultSampleFlagsPresentMask);
287 start_sample_flag = traf_->header.default_sample_flags;
289 return (start_sample_flag & TrackFragmentHeader::kNonKeySampleMask) == 0;
All the methods that are virtual are virtual for mocking.