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;
93 traf_->runs[0].sample_count = traf_->runs[0].sample_sizes.size();
95 &traf_->header.default_sample_duration)) {
96 traf_->header.flags |=
97 TrackFragmentHeader::kDefaultSampleDurationPresentMask;
99 traf_->runs[0].flags |= TrackFragmentRun::kSampleDurationPresentMask;
102 &traf_->header.default_sample_size)) {
103 traf_->header.flags |= TrackFragmentHeader::kDefaultSampleSizePresentMask;
105 traf_->runs[0].flags |= TrackFragmentRun::kSampleSizePresentMask;
108 &traf_->header.default_sample_flags)) {
109 traf_->header.flags |= TrackFragmentHeader::kDefaultSampleFlagsPresentMask;
111 traf_->runs[0].flags |= TrackFragmentRun::kSampleFlagsPresentMask;
114 fragment_finalized_ =
true;
115 fragment_initialized_ =
false;
120 reference->reference_type =
false;
121 reference->subsegment_duration = fragment_duration_;
122 reference->starts_with_sap = StartsWithSAP();
123 if (kInvalidTime == first_sap_time_) {
124 reference->sap_type = SegmentReference::TypeUnknown;
125 reference->sap_delta_time = 0;
127 reference->sap_type = SegmentReference::Type1;
128 reference->sap_delta_time = first_sap_time_ - earliest_presentation_time_;
130 reference->earliest_presentation_time = earliest_presentation_time_;
133 bool Fragmenter::StartsWithSAP() {
134 DCHECK(!traf_->runs.empty());
135 uint32_t start_sample_flag;
136 if (traf_->runs[0].flags & TrackFragmentRun::kSampleFlagsPresentMask) {
137 DCHECK(!traf_->runs[0].sample_flags.empty());
138 start_sample_flag = traf_->runs[0].sample_flags[0];
140 DCHECK(traf_->header.flags &
141 TrackFragmentHeader::kDefaultSampleFlagsPresentMask);
142 start_sample_flag = traf_->header.default_sample_flags;
144 return (start_sample_flag & TrackFragmentHeader::kNonKeySampleMask) == 0;