7 #include "packager/media/formats/mp4/fragmenter.h"
11 #include "packager/media/base/buffer_writer.h"
12 #include "packager/media/base/media_sample.h"
13 #include "packager/media/formats/mp4/box_definitions.h"
15 namespace edash_packager {
20 const int64_t kInvalidTime = std::numeric_limits<int64_t>::max();
25 fragment_initialized_(false),
26 fragment_finalized_(false),
27 fragment_duration_(0),
28 presentation_start_time_(kInvalidTime),
29 earliest_presentation_time_(kInvalidTime),
30 first_sap_time_(kInvalidTime) {
34 Fragmenter::~Fragmenter() {}
38 CHECK_GT(sample->duration(), 0);
40 if (!fragment_initialized_) {
47 traf_->runs[0].sample_sizes.push_back(sample->data_size());
48 traf_->runs[0].sample_durations.push_back(sample->duration());
49 traf_->runs[0].sample_flags.push_back(
50 sample->is_key_frame() ? 0 : TrackFragmentHeader::kNonKeySampleMask);
52 data_->AppendArray(sample->data(), sample->data_size());
53 fragment_duration_ += sample->duration();
55 int64_t pts = sample->pts();
59 if (earliest_presentation_time_ > pts)
60 earliest_presentation_time_ = pts;
62 traf_->runs[0].sample_composition_time_offsets.push_back(pts - sample->dts());
63 if (pts != sample->dts())
64 traf_->runs[0].flags |= TrackFragmentRun::kSampleCompTimeOffsetsPresentMask;
66 if (sample->is_key_frame()) {
67 if (first_sap_time_ == kInvalidTime)
68 first_sap_time_ = pts;
74 fragment_initialized_ =
true;
75 fragment_finalized_ =
false;
76 traf_->decode_time.decode_time = first_sample_dts;
78 traf_->runs.resize(1);
79 traf_->runs[0].flags = TrackFragmentRun::kDataOffsetPresentMask;
80 traf_->header.sample_description_index = 1;
81 traf_->header.flags = TrackFragmentHeader::kDefaultBaseIsMoofMask |
82 TrackFragmentHeader::kSampleDescriptionIndexPresentMask;
83 fragment_duration_ = 0;
84 earliest_presentation_time_ = kInvalidTime;
85 first_sap_time_ = kInvalidTime;
92 traf_->runs[0].sample_count = traf_->runs[0].sample_sizes.size();
94 &traf_->header.default_sample_duration)) {
95 traf_->header.flags |=
96 TrackFragmentHeader::kDefaultSampleDurationPresentMask;
98 traf_->runs[0].flags |= TrackFragmentRun::kSampleDurationPresentMask;
101 &traf_->header.default_sample_size)) {
102 traf_->header.flags |= TrackFragmentHeader::kDefaultSampleSizePresentMask;
104 traf_->runs[0].flags |= TrackFragmentRun::kSampleSizePresentMask;
107 &traf_->header.default_sample_flags)) {
108 traf_->header.flags |= TrackFragmentHeader::kDefaultSampleFlagsPresentMask;
110 traf_->runs[0].flags |= TrackFragmentRun::kSampleFlagsPresentMask;
113 fragment_finalized_ =
true;
114 fragment_initialized_ =
false;
119 reference->reference_type =
false;
120 reference->subsegment_duration = fragment_duration_;
121 reference->starts_with_sap = StartsWithSAP();
122 if (kInvalidTime == first_sap_time_) {
123 reference->sap_type = SegmentReference::TypeUnknown;
124 reference->sap_delta_time = 0;
126 reference->sap_type = SegmentReference::Type1;
127 reference->sap_delta_time = first_sap_time_ - earliest_presentation_time_;
129 reference->earliest_presentation_time = earliest_presentation_time_;
132 bool Fragmenter::StartsWithSAP() {
133 DCHECK(!traf_->runs.empty());
134 uint32_t start_sample_flag;
135 if (traf_->runs[0].flags & TrackFragmentRun::kSampleFlagsPresentMask) {
136 DCHECK(!traf_->runs[0].sample_flags.empty());
137 start_sample_flag = traf_->runs[0].sample_flags[0];
139 DCHECK(traf_->header.flags &
140 TrackFragmentHeader::kDefaultSampleFlagsPresentMask);
141 start_sample_flag = traf_->header.default_sample_flags;
143 return (start_sample_flag & TrackFragmentHeader::kNonKeySampleMask) == 0;