DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
fragmenter.cc
1 // Copyright 2014 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include "packager/media/formats/mp4/fragmenter.h"
8 
9 #include <limits>
10 
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"
15 
16 namespace shaka {
17 namespace media {
18 namespace mp4 {
19 
20 namespace {
21 const int64_t kInvalidTime = std::numeric_limits<int64_t>::max();
22 
23 uint64_t GetSeekPreroll(const StreamInfo& stream_info) {
24  if (stream_info.stream_type() != kStreamAudio)
25  return 0;
26  const AudioStreamInfo& audio_stream_info =
27  static_cast<const AudioStreamInfo&>(stream_info);
28  return audio_stream_info.seek_preroll_ns();
29 }
30 } // namespace
31 
32 Fragmenter::Fragmenter(scoped_refptr<StreamInfo> info, TrackFragment* traf)
33  : use_decoding_timestamp_in_timeline_(false),
34  traf_(traf),
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) {
42  DCHECK(traf);
43 }
44 
45 Fragmenter::~Fragmenter() {}
46 
47 Status Fragmenter::AddSample(scoped_refptr<MediaSample> sample) {
48  DCHECK(sample);
49  if (sample->duration() == 0) {
50  LOG(WARNING) << "Unexpected sample with zero duration @ dts "
51  << sample->dts();
52  }
53 
54  if (!fragment_initialized_) {
55  Status status = InitializeFragment(sample->dts());
56  if (!status.ok())
57  return status;
58  }
59 
60  if (sample->side_data_size() > 0)
61  LOG(WARNING) << "MP4 samples do not support side data. Side data ignored.";
62 
63  // Fill in sample parameters. It will be optimized later.
64  traf_->runs[0].sample_sizes.push_back(sample->data_size());
65  traf_->runs[0].sample_durations.push_back(sample->duration());
66  traf_->runs[0].sample_flags.push_back(
67  sample->is_key_frame() ? 0 : TrackFragmentHeader::kNonKeySampleMask);
68 
69  data_->AppendArray(sample->data(), sample->data_size());
70  fragment_duration_ += sample->duration();
71 
72  const int64_t pts = sample->pts();
73  const int64_t dts = sample->dts();
74 
75  const int64_t timestamp = use_decoding_timestamp_in_timeline_ ? dts : pts;
76  // Set |earliest_presentation_time_| to |timestamp| if |timestamp| is smaller
77  // or if it is not yet initialized (kInvalidTime > timestamp is always true).
78  if (earliest_presentation_time_ > timestamp)
79  earliest_presentation_time_ = timestamp;
80 
81  traf_->runs[0].sample_composition_time_offsets.push_back(pts - dts);
82  if (pts != dts)
83  traf_->runs[0].flags |= TrackFragmentRun::kSampleCompTimeOffsetsPresentMask;
84 
85  if (sample->is_key_frame()) {
86  if (first_sap_time_ == kInvalidTime)
87  first_sap_time_ = pts;
88  }
89  return Status::OK;
90 }
91 
92 Status Fragmenter::InitializeFragment(int64_t first_sample_dts) {
93  fragment_initialized_ = true;
94  fragment_finalized_ = false;
95  traf_->decode_time.decode_time = first_sample_dts;
96  traf_->runs.clear();
97  traf_->runs.resize(1);
98  traf_->runs[0].flags = TrackFragmentRun::kDataOffsetPresentMask;
99  traf_->sample_group_descriptions.clear();
100  traf_->sample_to_groups.clear();
101  traf_->header.sample_description_index = 1; // 1-based.
102  traf_->header.flags = TrackFragmentHeader::kDefaultBaseIsMoofMask |
103  TrackFragmentHeader::kSampleDescriptionIndexPresentMask;
104  fragment_duration_ = 0;
105  earliest_presentation_time_ = kInvalidTime;
106  first_sap_time_ = kInvalidTime;
107  data_.reset(new BufferWriter());
108  return Status::OK;
109 }
110 
112  // Optimize trun box.
113  traf_->runs[0].sample_count = traf_->runs[0].sample_sizes.size();
114  if (OptimizeSampleEntries(&traf_->runs[0].sample_durations,
115  &traf_->header.default_sample_duration)) {
116  traf_->header.flags |=
117  TrackFragmentHeader::kDefaultSampleDurationPresentMask;
118  } else {
119  traf_->runs[0].flags |= TrackFragmentRun::kSampleDurationPresentMask;
120  }
121  if (OptimizeSampleEntries(&traf_->runs[0].sample_sizes,
122  &traf_->header.default_sample_size)) {
123  traf_->header.flags |= TrackFragmentHeader::kDefaultSampleSizePresentMask;
124  } else {
125  traf_->runs[0].flags |= TrackFragmentRun::kSampleSizePresentMask;
126  }
127  if (OptimizeSampleEntries(&traf_->runs[0].sample_flags,
128  &traf_->header.default_sample_flags)) {
129  traf_->header.flags |= TrackFragmentHeader::kDefaultSampleFlagsPresentMask;
130  } else {
131  traf_->runs[0].flags |= TrackFragmentRun::kSampleFlagsPresentMask;
132  }
133 
134  // Add SampleToGroup boxes. A SampleToGroup box with grouping type of 'roll'
135  // needs to be added if there is seek preroll, referencing sample group
136  // description in track level; Also need to add SampleToGroup boxes
137  // correponding to every SampleGroupDescription boxes, referencing sample
138  // group description in fragment level.
139  DCHECK_EQ(traf_->sample_to_groups.size(), 0u);
140  if (seek_preroll_ > 0) {
141  traf_->sample_to_groups.resize(traf_->sample_to_groups.size() + 1);
142  SampleToGroup& sample_to_group = traf_->sample_to_groups.back();
143  sample_to_group.grouping_type = FOURCC_roll;
144 
145  sample_to_group.entries.resize(1);
146  SampleToGroupEntry& sample_to_group_entry = sample_to_group.entries.back();
147  sample_to_group_entry.sample_count = traf_->runs[0].sample_count;
148  sample_to_group_entry.group_description_index =
149  SampleToGroupEntry::kTrackGroupDescriptionIndexBase + 1;
150  }
151  for (const auto& sample_group_description :
152  traf_->sample_group_descriptions) {
153  traf_->sample_to_groups.resize(traf_->sample_to_groups.size() + 1);
154  SampleToGroup& sample_to_group = traf_->sample_to_groups.back();
155  sample_to_group.grouping_type = sample_group_description.grouping_type;
156 
157  sample_to_group.entries.resize(1);
158  SampleToGroupEntry& sample_to_group_entry = sample_to_group.entries.back();
159  sample_to_group_entry.sample_count = traf_->runs[0].sample_count;
160  sample_to_group_entry.group_description_index =
161  SampleToGroupEntry::kTrackFragmentGroupDescriptionIndexBase + 1;
162  }
163 
164  fragment_finalized_ = true;
165  fragment_initialized_ = false;
166 }
167 
169  // NOTE: Daisy chain is not supported currently.
170  reference->reference_type = false;
171  reference->subsegment_duration = fragment_duration_;
172  reference->starts_with_sap = StartsWithSAP();
173  if (kInvalidTime == first_sap_time_) {
174  reference->sap_type = SegmentReference::TypeUnknown;
175  reference->sap_delta_time = 0;
176  } else {
177  reference->sap_type = SegmentReference::Type1;
178  reference->sap_delta_time = first_sap_time_ - earliest_presentation_time_;
179  }
180  reference->earliest_presentation_time = earliest_presentation_time_;
181 }
182 
183 bool Fragmenter::StartsWithSAP() {
184  DCHECK(!traf_->runs.empty());
185  uint32_t start_sample_flag;
186  if (traf_->runs[0].flags & TrackFragmentRun::kSampleFlagsPresentMask) {
187  DCHECK(!traf_->runs[0].sample_flags.empty());
188  start_sample_flag = traf_->runs[0].sample_flags[0];
189  } else {
190  DCHECK(traf_->header.flags &
191  TrackFragmentHeader::kDefaultSampleFlagsPresentMask);
192  start_sample_flag = traf_->header.default_sample_flags;
193  }
194  return (start_sample_flag & TrackFragmentHeader::kNonKeySampleMask) == 0;
195 }
196 
197 } // namespace mp4
198 } // namespace media
199 } // namespace shaka
Fragmenter(scoped_refptr< StreamInfo > info, TrackFragment *traf)
Definition: fragmenter.cc:32
virtual Status AddSample(scoped_refptr< MediaSample > sample)
Definition: fragmenter.cc:47
virtual Status InitializeFragment(int64_t first_sample_dts)
Definition: fragmenter.cc:92
virtual void FinalizeFragment()
Finalize and optimize the fragment.
Definition: fragmenter.cc:111
void GenerateSegmentReference(SegmentReference *reference)
Fill reference with current fragment information.
Definition: fragmenter.cc:168
bool OptimizeSampleEntries(std::vector< T > *entries, T *default_value)
Definition: fragmenter.h:102