7 #include "packager/media/formats/mp4/fragmenter.h"
11 #include "packager/media/base/buffer_writer.h"
12 #include "packager/media/base/audio_stream_info.h"
13 #include "packager/media/base/media_sample.h"
14 #include "packager/media/formats/mp4/box_definitions.h"
21 const int64_t kInvalidTime = std::numeric_limits<int64_t>::max();
23 uint64_t GetSeekPreroll(
const StreamInfo& stream_info) {
24 if (stream_info.stream_type() != kStreamAudio)
26 const AudioStreamInfo& audio_stream_info =
27 static_cast<const AudioStreamInfo&
>(stream_info);
28 return audio_stream_info.seek_preroll_ns();
33 : use_decoding_timestamp_in_timeline_(false),
35 seek_preroll_(GetSeekPreroll(*info)),
36 fragment_initialized_(false),
37 fragment_finalized_(false),
38 fragment_duration_(0),
39 presentation_start_time_(kInvalidTime),
40 earliest_presentation_time_(kInvalidTime),
41 first_sap_time_(kInvalidTime) {
45 Fragmenter::~Fragmenter() {}
49 CHECK_GT(sample->duration(), 0);
51 if (!fragment_initialized_) {
57 if (sample->side_data_size() > 0)
58 LOG(WARNING) <<
"MP4 samples do not support side data. Side data ignored.";
61 traf_->runs[0].sample_sizes.push_back(sample->data_size());
62 traf_->runs[0].sample_durations.push_back(sample->duration());
63 traf_->runs[0].sample_flags.push_back(
64 sample->is_key_frame() ? 0 : TrackFragmentHeader::kNonKeySampleMask);
66 data_->AppendArray(sample->data(), sample->data_size());
67 fragment_duration_ += sample->duration();
69 const int64_t pts = sample->pts();
70 const int64_t dts = sample->dts();
72 const int64_t timestamp = use_decoding_timestamp_in_timeline_ ? dts : pts;
75 if (earliest_presentation_time_ > timestamp)
76 earliest_presentation_time_ = timestamp;
78 traf_->runs[0].sample_composition_time_offsets.push_back(pts - dts);
80 traf_->runs[0].flags |= TrackFragmentRun::kSampleCompTimeOffsetsPresentMask;
82 if (sample->is_key_frame()) {
83 if (first_sap_time_ == kInvalidTime)
84 first_sap_time_ = pts;
90 fragment_initialized_ =
true;
91 fragment_finalized_ =
false;
92 traf_->decode_time.decode_time = first_sample_dts;
94 traf_->runs.resize(1);
95 traf_->runs[0].flags = TrackFragmentRun::kDataOffsetPresentMask;
96 traf_->sample_group_descriptions.clear();
97 traf_->sample_to_groups.clear();
98 traf_->header.sample_description_index = 1;
99 traf_->header.flags = TrackFragmentHeader::kDefaultBaseIsMoofMask |
100 TrackFragmentHeader::kSampleDescriptionIndexPresentMask;
101 fragment_duration_ = 0;
102 earliest_presentation_time_ = kInvalidTime;
103 first_sap_time_ = kInvalidTime;
110 traf_->runs[0].sample_count = traf_->runs[0].sample_sizes.size();
112 &traf_->header.default_sample_duration)) {
113 traf_->header.flags |=
114 TrackFragmentHeader::kDefaultSampleDurationPresentMask;
116 traf_->runs[0].flags |= TrackFragmentRun::kSampleDurationPresentMask;
119 &traf_->header.default_sample_size)) {
120 traf_->header.flags |= TrackFragmentHeader::kDefaultSampleSizePresentMask;
122 traf_->runs[0].flags |= TrackFragmentRun::kSampleSizePresentMask;
125 &traf_->header.default_sample_flags)) {
126 traf_->header.flags |= TrackFragmentHeader::kDefaultSampleFlagsPresentMask;
128 traf_->runs[0].flags |= TrackFragmentRun::kSampleFlagsPresentMask;
136 DCHECK_EQ(traf_->sample_to_groups.size(), 0u);
137 if (seek_preroll_ > 0) {
138 traf_->sample_to_groups.resize(traf_->sample_to_groups.size() + 1);
139 SampleToGroup& sample_to_group = traf_->sample_to_groups.back();
140 sample_to_group.grouping_type = FOURCC_roll;
142 sample_to_group.entries.resize(1);
144 sample_to_group_entry.sample_count = traf_->runs[0].sample_count;
145 sample_to_group_entry.group_description_index =
146 SampleToGroupEntry::kTrackGroupDescriptionIndexBase + 1;
148 for (
const auto& sample_group_description :
149 traf_->sample_group_descriptions) {
150 traf_->sample_to_groups.resize(traf_->sample_to_groups.size() + 1);
151 SampleToGroup& sample_to_group = traf_->sample_to_groups.back();
152 sample_to_group.grouping_type = sample_group_description.grouping_type;
154 sample_to_group.entries.resize(1);
156 sample_to_group_entry.sample_count = traf_->runs[0].sample_count;
157 sample_to_group_entry.group_description_index =
158 SampleToGroupEntry::kTrackFragmentGroupDescriptionIndexBase + 1;
161 fragment_finalized_ =
true;
162 fragment_initialized_ =
false;
167 reference->reference_type =
false;
168 reference->subsegment_duration = fragment_duration_;
169 reference->starts_with_sap = StartsWithSAP();
170 if (kInvalidTime == first_sap_time_) {
171 reference->sap_type = SegmentReference::TypeUnknown;
172 reference->sap_delta_time = 0;
174 reference->sap_type = SegmentReference::Type1;
175 reference->sap_delta_time = first_sap_time_ - earliest_presentation_time_;
177 reference->earliest_presentation_time = earliest_presentation_time_;
180 bool Fragmenter::StartsWithSAP() {
181 DCHECK(!traf_->runs.empty());
182 uint32_t start_sample_flag;
183 if (traf_->runs[0].flags & TrackFragmentRun::kSampleFlagsPresentMask) {
184 DCHECK(!traf_->runs[0].sample_flags.empty());
185 start_sample_flag = traf_->runs[0].sample_flags[0];
187 DCHECK(traf_->header.flags &
188 TrackFragmentHeader::kDefaultSampleFlagsPresentMask);
189 start_sample_flag = traf_->header.default_sample_flags;
191 return (start_sample_flag & TrackFragmentHeader::kNonKeySampleMask) == 0;