Shaka Packager SDK
segmenter.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/segmenter.h"
8 
9 #include <algorithm>
10 
11 #include "packager/base/logging.h"
12 #include "packager/media/base/buffer_writer.h"
13 #include "packager/media/base/id3_tag.h"
14 #include "packager/media/base/media_sample.h"
15 #include "packager/media/base/muxer_options.h"
16 #include "packager/media/base/muxer_util.h"
17 #include "packager/media/base/stream_info.h"
18 #include "packager/media/chunking/chunking_handler.h"
19 #include "packager/media/event/progress_listener.h"
20 #include "packager/media/formats/mp4/box_definitions.h"
21 #include "packager/media/formats/mp4/fragmenter.h"
22 #include "packager/media/formats/mp4/key_frame_info.h"
23 #include "packager/version/version.h"
24 
25 namespace shaka {
26 namespace media {
27 namespace mp4 {
28 
29 namespace {
30 
31 uint64_t Rescale(uint64_t time_in_old_scale,
32  uint32_t old_scale,
33  uint32_t new_scale) {
34  return static_cast<double>(time_in_old_scale) / old_scale * new_scale;
35 }
36 
37 } // namespace
38 
39 Segmenter::Segmenter(const MuxerOptions& options,
40  std::unique_ptr<FileType> ftyp,
41  std::unique_ptr<Movie> moov)
42  : options_(options),
43  ftyp_(std::move(ftyp)),
44  moov_(std::move(moov)),
45  moof_(new MovieFragment()),
46  fragment_buffer_(new BufferWriter()),
47  sidx_(new SegmentIndex()) {}
48 
49 Segmenter::~Segmenter() {}
50 
52  const std::vector<std::shared_ptr<const StreamInfo>>& streams,
53  MuxerListener* muxer_listener,
54  ProgressListener* progress_listener) {
55  DCHECK_LT(0u, streams.size());
56  muxer_listener_ = muxer_listener;
57  progress_listener_ = progress_listener;
58  moof_->header.sequence_number = 0;
59 
60  moof_->tracks.resize(streams.size());
61  fragmenters_.resize(streams.size());
62  stream_durations_.resize(streams.size());
63 
64  for (uint32_t i = 0; i < streams.size(); ++i) {
65  moof_->tracks[i].header.track_id = i + 1;
66  if (streams[i]->stream_type() == kStreamVideo) {
67  // Use the first video stream as the reference stream (which is 1-based).
68  if (sidx_->reference_id == 0)
69  sidx_->reference_id = i + 1;
70  }
71  fragmenters_[i].reset(new Fragmenter(streams[i], &moof_->tracks[i]));
72  }
73 
74  if (options_.mp4_params.use_decoding_timestamp_in_timeline) {
75  for (uint32_t i = 0; i < streams.size(); ++i)
76  fragmenters_[i]->set_use_decoding_timestamp_in_timeline(true);
77  }
78  // Only allow |EPT| to be adjusted for the first file.
79  if (options_.output_file_index == 0) {
80  for (uint32_t i = 0; i < streams.size(); ++i)
81  fragmenters_[i]->set_allow_adjust_earliest_presentation_time(true);
82  }
83 
84  // Choose the first stream if there is no VIDEO.
85  if (sidx_->reference_id == 0)
86  sidx_->reference_id = 1;
87  sidx_->timescale = streams[GetReferenceStreamId()]->time_scale();
88 
89  // Use media duration as progress target.
90  progress_target_ = streams[GetReferenceStreamId()]->duration();
91 
92  // Use the reference stream's time scale as movie time scale.
93  moov_->header.timescale = sidx_->timescale;
94  moof_->header.sequence_number = 1;
95 
96  // Fill in version information.
97  const std::string version = GetPackagerVersion();
98  if (!version.empty()) {
99  moov_->metadata.handler.handler_type = FOURCC_ID32;
100  moov_->metadata.id3v2.language.code = "eng";
101 
102  Id3Tag id3_tag;
103  id3_tag.AddPrivateFrame(GetPackagerProjectUrl(), version);
104  CHECK(id3_tag.WriteToVector(&moov_->metadata.id3v2.id3v2_data));
105  }
106  return DoInitialize();
107 }
108 
110  // Set movie duration. Note that the duration in mvhd, tkhd, mdhd should not
111  // be touched, i.e. kept at 0. The updated moov box will be written to output
112  // file for VOD and static live case only.
113  moov_->extends.header.fragment_duration = 0;
114  for (size_t i = 0; i < stream_durations_.size(); ++i) {
115  uint64_t duration =
116  Rescale(stream_durations_[i], moov_->tracks[i].media.header.timescale,
117  moov_->header.timescale);
118  if (duration > moov_->extends.header.fragment_duration)
119  moov_->extends.header.fragment_duration = duration;
120  }
121  return DoFinalize();
122 }
123 
124 Status Segmenter::AddSample(size_t stream_id, const MediaSample& sample) {
125  // Set default sample duration if it has not been set yet.
126  if (moov_->extends.tracks[stream_id].default_sample_duration == 0) {
127  moov_->extends.tracks[stream_id].default_sample_duration =
128  sample.duration();
129  }
130 
131  DCHECK_LT(stream_id, fragmenters_.size());
132  Fragmenter* fragmenter = fragmenters_[stream_id].get();
133  if (fragmenter->fragment_finalized()) {
134  return Status(error::FRAGMENT_FINALIZED,
135  "Current fragment is finalized already.");
136  }
137 
138  Status status = fragmenter->AddSample(sample);
139  if (!status.ok())
140  return status;
141 
142  if (sample_duration_ == 0)
143  sample_duration_ = sample.duration();
144  stream_durations_[stream_id] += sample.duration();
145  return Status::OK;
146 }
147 
149  const SegmentInfo& segment_info) {
150  if (segment_info.key_rotation_encryption_config) {
151  FinalizeFragmentForKeyRotation(
152  stream_id, segment_info.is_encrypted,
153  *segment_info.key_rotation_encryption_config);
154  }
155 
156  DCHECK_LT(stream_id, fragmenters_.size());
157  Fragmenter* fragmenter = fragmenters_[stream_id].get();
158  DCHECK(fragmenter);
159  Status status = fragmenter->FinalizeFragment();
160  if (!status.ok())
161  return status;
162 
163  // Check if all tracks are ready for fragmentation.
164  for (const std::unique_ptr<Fragmenter>& fragmenter : fragmenters_) {
165  if (!fragmenter->fragment_finalized())
166  return Status::OK;
167  }
168 
169  MediaData mdat;
170  // Data offset relative to 'moof': moof size + mdat header size.
171  // The code will also update box sizes for moof_ and its child boxes.
172  uint64_t data_offset = moof_->ComputeSize() + mdat.HeaderSize();
173  // 'traf' should follow 'mfhd' moof header box.
174  uint64_t next_traf_position = moof_->HeaderSize() + moof_->header.box_size();
175  for (size_t i = 0; i < moof_->tracks.size(); ++i) {
176  TrackFragment& traf = moof_->tracks[i];
177  if (traf.auxiliary_offset.offsets.size() > 0) {
178  DCHECK_EQ(traf.auxiliary_offset.offsets.size(), 1u);
179  DCHECK(!traf.sample_encryption.sample_encryption_entries.empty());
180 
181  next_traf_position += traf.box_size();
182  // SampleEncryption 'senc' box should be the last box in 'traf'.
183  // |auxiliary_offset| should point to the data of SampleEncryption.
184  traf.auxiliary_offset.offsets[0] =
185  next_traf_position - traf.sample_encryption.box_size() +
186  traf.sample_encryption.HeaderSize() +
187  sizeof(uint32_t); // for sample count field in 'senc'
188  }
189  traf.runs[0].data_offset = data_offset + mdat.data_size;
190  mdat.data_size += static_cast<uint32_t>(fragmenters_[i]->data()->Size());
191  }
192 
193  // Generate segment reference.
194  sidx_->references.resize(sidx_->references.size() + 1);
195  fragmenters_[GetReferenceStreamId()]->GenerateSegmentReference(
196  &sidx_->references[sidx_->references.size() - 1]);
197  sidx_->references[sidx_->references.size() - 1].referenced_size =
198  data_offset + mdat.data_size;
199 
200  const uint64_t moof_start_offset = fragment_buffer_->Size();
201 
202  // Write the fragment to buffer.
203  moof_->Write(fragment_buffer_.get());
204  mdat.WriteHeader(fragment_buffer_.get());
205 
206  bool first_key_frame = true;
207  for (const std::unique_ptr<Fragmenter>& fragmenter : fragmenters_) {
208  // https://goo.gl/xcFus6 6. Trick play requirements
209  // 6.10. If using fMP4, I-frame segments must include the 'moof' header
210  // associated with the I-frame. It also implies that only the first key
211  // frame can be included.
212  if (!fragmenter->key_frame_infos().empty() && first_key_frame) {
213  const KeyFrameInfo& key_frame_info =
214  fragmenter->key_frame_infos().front();
215  first_key_frame = false;
216  key_frame_infos_.push_back(
217  {key_frame_info.timestamp, moof_start_offset,
218  fragment_buffer_->Size() - moof_start_offset + key_frame_info.size});
219  }
220  fragment_buffer_->AppendBuffer(*fragmenter->data());
221  }
222 
223  // Increase sequence_number for next fragment.
224  ++moof_->header.sequence_number;
225 
226  for (std::unique_ptr<Fragmenter>& fragmenter : fragmenters_)
227  fragmenter->ClearFragmentFinalized();
228  if (!segment_info.is_subsegment) {
229  Status status = DoFinalizeSegment();
230  // Reset segment information to initial state.
231  sidx_->references.clear();
232  key_frame_infos_.clear();
233  return status;
234  }
235  return Status::OK;
236 }
237 
238 uint32_t Segmenter::GetReferenceTimeScale() const {
239  return moov_->header.timescale;
240 }
241 
242 double Segmenter::GetDuration() const {
243  uint64_t duration = moov_->extends.header.fragment_duration;
244  if (duration == 0) {
245  // Handling the case where this is not properly initialized.
246  return 0.0;
247  }
248  return static_cast<double>(duration) / moov_->header.timescale;
249 }
250 
251 void Segmenter::UpdateProgress(uint64_t progress) {
252  accumulated_progress_ += progress;
253 
254  if (!progress_listener_) return;
255  if (progress_target_ == 0) return;
256  // It might happen that accumulated progress exceeds progress_target due to
257  // computation errors, e.g. rounding error. Cap it so it never reports > 100%
258  // progress.
259  if (accumulated_progress_ >= progress_target_) {
260  progress_listener_->OnProgress(1.0);
261  } else {
262  progress_listener_->OnProgress(static_cast<double>(accumulated_progress_) /
263  progress_target_);
264  }
265 }
266 
267 void Segmenter::SetComplete() {
268  if (!progress_listener_) return;
269  progress_listener_->OnProgress(1.0);
270 }
271 
272 uint32_t Segmenter::GetReferenceStreamId() {
273  DCHECK(sidx_);
274  return sidx_->reference_id - 1;
275 }
276 
277 void Segmenter::FinalizeFragmentForKeyRotation(
278  size_t stream_id,
279  bool fragment_encrypted,
280  const EncryptionConfig& encryption_config) {
281  if (options_.mp4_params.include_pssh_in_stream) {
282  const std::vector<ProtectionSystemSpecificInfo>& system_info =
283  encryption_config.key_system_info;
284  moof_->pssh.resize(system_info.size());
285  for (size_t i = 0; i < system_info.size(); i++)
286  moof_->pssh[i].raw_box = system_info[i].psshs;
287  } else {
288  LOG(WARNING)
289  << "Key rotation and no pssh in stream may not work well together.";
290  }
291 
292  // Skip the following steps if the current fragment is not going to be
293  // encrypted. 'pssh' box needs to be included in the fragment, which is
294  // performed above, regardless of whether the fragment is encrypted. This is
295  // necessary for two reasons: 1) Requesting keys before reaching encrypted
296  // content avoids playback delay due to license requests; 2) In Chrome, CDM
297  // must be initialized before starting the playback and CDM can only be
298  // initialized with a valid 'pssh'.
299  if (!fragment_encrypted)
300  return;
301 
302  DCHECK_LT(stream_id, moof_->tracks.size());
303  TrackFragment& traf = moof_->tracks[stream_id];
304  traf.sample_group_descriptions.resize(traf.sample_group_descriptions.size() +
305  1);
306  SampleGroupDescription& sample_group_description =
307  traf.sample_group_descriptions.back();
308  sample_group_description.grouping_type = FOURCC_seig;
309 
310  sample_group_description.cenc_sample_encryption_info_entries.resize(1);
311  CencSampleEncryptionInfoEntry& sample_group_entry =
312  sample_group_description.cenc_sample_encryption_info_entries.back();
313  sample_group_entry.is_protected = 1;
314  sample_group_entry.per_sample_iv_size = encryption_config.per_sample_iv_size;
315  sample_group_entry.constant_iv = encryption_config.constant_iv;
316  sample_group_entry.crypt_byte_block = encryption_config.crypt_byte_block;
317  sample_group_entry.skip_byte_block = encryption_config.skip_byte_block;
318  sample_group_entry.key_id = encryption_config.key_id;
319 }
320 
321 } // namespace mp4
322 } // namespace media
323 } // namespace shaka
uint32_t HeaderSize() const final
Definition: box.cc:75
virtual bool WriteToVector(std::vector< uint8_t > *output)
Definition: id3_tag.cc:67
STL namespace.
virtual void AddPrivateFrame(const std::string &owner, const std::string &data)
Definition: id3_tag.cc:49
All the methods that are virtual are virtual for mocking.
Status AddSample(const MediaSample &sample)
Definition: segmenter.cc:159
virtual Status FinalizeSegment(uint64_t start_timestamp, uint64_t duration_timestamp, bool is_subsegment)=0
Finalize the (sub)segment.
Definition: segmenter.cc:195
uint32_t ComputeSize()
Definition: box.cc:50
This class listens to progress updates events.
Status Initialize(const StreamInfo &info, ProgressListener *progress_listener, MuxerListener *muxer_listener)
Definition: segmenter.cc:78
Tracks key frame information.
uint32_t box_size()
Definition: box.h:55
virtual uint32_t HeaderSize() const
Definition: box.cc:55
Class to hold a media sample.
Definition: media_sample.h:22
void UpdateProgress(uint64_t progress)
Update segmentation progress using ProgressListener.
Definition: segmenter.cc:270
void WriteHeader(BufferWriter *writer)
Definition: box.cc:38