Shaka Packager SDK
mp4_muxer.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/mp4_muxer.h"
8 
9 #include <algorithm>
10 
11 #include "packager/base/time/clock.h"
12 #include "packager/base/time/time.h"
13 #include "packager/file/file.h"
14 #include "packager/media/base/aes_encryptor.h"
15 #include "packager/media/base/audio_stream_info.h"
16 #include "packager/media/base/fourccs.h"
17 #include "packager/media/base/key_source.h"
18 #include "packager/media/base/media_sample.h"
19 #include "packager/media/base/text_stream_info.h"
20 #include "packager/media/base/video_stream_info.h"
21 #include "packager/media/codecs/es_descriptor.h"
22 #include "packager/media/event/muxer_listener.h"
23 #include "packager/media/formats/mp4/box_definitions.h"
24 #include "packager/media/formats/mp4/multi_segment_segmenter.h"
25 #include "packager/media/formats/mp4/single_segment_segmenter.h"
26 #include "packager/status_macros.h"
27 
28 namespace shaka {
29 namespace media {
30 namespace mp4 {
31 
32 namespace {
33 
34 // Sets the range start and end value from offset and size.
35 // |start| and |end| are for byte-range-spec specified in RFC2616.
36 void SetStartAndEndFromOffsetAndSize(size_t offset,
37  size_t size,
38  Range* range) {
39  DCHECK(range);
40  range->start = static_cast<uint32_t>(offset);
41  // Note that ranges are inclusive. So we need - 1.
42  range->end = range->start + static_cast<uint32_t>(size) - 1;
43 }
44 
45 FourCC CodecToFourCC(Codec codec, H26xStreamFormat h26x_stream_format) {
46  switch (codec) {
47  case kCodecH264:
48  return h26x_stream_format ==
49  H26xStreamFormat::kNalUnitStreamWithParameterSetNalus
50  ? FOURCC_avc3
51  : FOURCC_avc1;
52  case kCodecH265:
53  return h26x_stream_format ==
54  H26xStreamFormat::kNalUnitStreamWithParameterSetNalus
55  ? FOURCC_hev1
56  : FOURCC_hvc1;
57  case kCodecVP8:
58  return FOURCC_vp08;
59  case kCodecVP9:
60  return FOURCC_vp09;
61  case kCodecVP10:
62  return FOURCC_vp10;
63  case kCodecAAC:
64  return FOURCC_mp4a;
65  case kCodecAC3:
66  return FOURCC_ac_3;
67  case kCodecDTSC:
68  return FOURCC_dtsc;
69  case kCodecDTSH:
70  return FOURCC_dtsh;
71  case kCodecDTSL:
72  return FOURCC_dtsl;
73  case kCodecDTSE:
74  return FOURCC_dtse;
75  case kCodecDTSM:
76  return FOURCC_dtsm;
77  case kCodecEAC3:
78  return FOURCC_ec_3;
79  case kCodecFlac:
80  return FOURCC_fLaC;
81  case kCodecOpus:
82  return FOURCC_Opus;
83  default:
84  return FOURCC_NULL;
85  }
86 }
87 
88 void GenerateSinf(FourCC old_type,
89  const EncryptionConfig& encryption_config,
90  ProtectionSchemeInfo* sinf) {
91  sinf->format.format = old_type;
92 
93  DCHECK_NE(encryption_config.protection_scheme, FOURCC_NULL);
94  sinf->type.type = encryption_config.protection_scheme;
95 
96  // The version of cenc implemented here. CENC 4.
97  const int kCencSchemeVersion = 0x00010000;
98  sinf->type.version = kCencSchemeVersion;
99 
100  auto& track_encryption = sinf->info.track_encryption;
101  track_encryption.default_is_protected = 1;
102 
103  track_encryption.default_crypt_byte_block =
104  encryption_config.crypt_byte_block;
105  track_encryption.default_skip_byte_block = encryption_config.skip_byte_block;
106  switch (encryption_config.protection_scheme) {
107  case FOURCC_cenc:
108  case FOURCC_cbc1:
109  DCHECK_EQ(track_encryption.default_crypt_byte_block, 0u);
110  DCHECK_EQ(track_encryption.default_skip_byte_block, 0u);
111  // CENCv3 10.1 ‘cenc’ AES-CTR scheme and 10.2 ‘cbc1’ AES-CBC scheme:
112  // The version of the Track Encryption Box (‘tenc’) SHALL be 0.
113  track_encryption.version = 0;
114  break;
115  case FOURCC_cbcs:
116  case FOURCC_cens:
117  if (track_encryption.default_skip_byte_block == 0) {
118  // Some clients, e.g. Safari v11.0.3 does not like having
119  // crypt_byte_block as a non-zero value when skip_byte_block is zero.
120  track_encryption.default_crypt_byte_block = 0;
121  }
122  // CENCv3 10.3 ‘cens’ AES-CTR subsample pattern encryption scheme and
123  // 10.4 ‘cbcs’ AES-CBC subsample pattern encryption scheme:
124  // The version of the Track Encryption Box (‘tenc’) SHALL be 1.
125  track_encryption.version = 1;
126  break;
127  default:
128  NOTREACHED() << "Unexpected protection scheme "
129  << encryption_config.protection_scheme;
130  }
131 
132  track_encryption.default_per_sample_iv_size =
133  encryption_config.per_sample_iv_size;
134  track_encryption.default_constant_iv = encryption_config.constant_iv;
135  track_encryption.default_kid = encryption_config.key_id;
136 }
137 
138 } // namespace
139 
140 MP4Muxer::MP4Muxer(const MuxerOptions& options) : Muxer(options) {}
141 MP4Muxer::~MP4Muxer() {}
142 
143 Status MP4Muxer::InitializeMuxer() {
144  // Muxer will be delay-initialized after seeing the first sample.
145  to_be_initialized_ = true;
146  return Status::OK;
147 }
148 
149 Status MP4Muxer::Finalize() {
150  // This happens on streams that are not initialized, i.e. not going through
151  // DelayInitializeMuxer, which can only happen if there are no samples from
152  // the stream.
153  if (!segmenter_) {
154  DCHECK(to_be_initialized_);
155  LOG(INFO) << "Skip stream '" << options().output_file_name
156  << "' which does not contain any sample.";
157  return Status::OK;
158  }
159 
160  Status segmenter_finalized = segmenter_->Finalize();
161 
162  if (!segmenter_finalized.ok())
163  return segmenter_finalized;
164 
165  FireOnMediaEndEvent();
166  LOG(INFO) << "MP4 file '" << options().output_file_name << "' finalized.";
167  return Status::OK;
168 }
169 
170 Status MP4Muxer::AddSample(size_t stream_id, const MediaSample& sample) {
171  if (to_be_initialized_) {
172  RETURN_IF_ERROR(UpdateEditListOffsetFromSample(sample));
173  RETURN_IF_ERROR(DelayInitializeMuxer());
174  to_be_initialized_ = false;
175  }
176  DCHECK(segmenter_);
177  return segmenter_->AddSample(stream_id, sample);
178 }
179 
180 Status MP4Muxer::FinalizeSegment(size_t stream_id,
181  const SegmentInfo& segment_info) {
182  DCHECK(segmenter_);
183  VLOG(3) << "Finalizing " << (segment_info.is_subsegment ? "sub" : "")
184  << "segment " << segment_info.start_timestamp << " duration "
185  << segment_info.duration;
186  return segmenter_->FinalizeSegment(stream_id, segment_info);
187 }
188 
189 Status MP4Muxer::DelayInitializeMuxer() {
190  DCHECK(!streams().empty());
191 
192  std::unique_ptr<FileType> ftyp(new FileType);
193  std::unique_ptr<Movie> moov(new Movie);
194 
195  ftyp->major_brand = FOURCC_isom;
196  ftyp->compatible_brands.push_back(FOURCC_iso8);
197  ftyp->compatible_brands.push_back(FOURCC_mp41);
198  ftyp->compatible_brands.push_back(FOURCC_dash);
199 
200  if (streams().size() == 1) {
201  FourCC codec_fourcc = FOURCC_NULL;
202  if (streams()[0]->stream_type() == kStreamVideo) {
203  codec_fourcc =
204  CodecToFourCC(streams()[0]->codec(),
205  static_cast<const VideoStreamInfo*>(streams()[0].get())
206  ->h26x_stream_format());
207  if (codec_fourcc != FOURCC_NULL)
208  ftyp->compatible_brands.push_back(codec_fourcc);
209  }
210 
211  // CMAF allows only one track/stream per file.
212  // CMAF requires single initialization switching for AVC3/HEV1, which is not
213  // supported yet.
214  if (codec_fourcc != FOURCC_avc3 && codec_fourcc != FOURCC_hev1)
215  ftyp->compatible_brands.push_back(FOURCC_cmfc);
216  }
217 
218  moov->header.creation_time = IsoTimeNow();
219  moov->header.modification_time = IsoTimeNow();
220  moov->header.next_track_id = static_cast<uint32_t>(streams().size()) + 1;
221 
222  moov->tracks.resize(streams().size());
223  moov->extends.tracks.resize(streams().size());
224 
225  // Initialize tracks.
226  for (uint32_t i = 0; i < streams().size(); ++i) {
227  const StreamInfo* stream = streams()[i].get();
228  Track& trak = moov->tracks[i];
229  trak.header.track_id = i + 1;
230 
231  TrackExtends& trex = moov->extends.tracks[i];
232  trex.track_id = trak.header.track_id;
233  trex.default_sample_description_index = 1;
234 
235  bool generate_trak_result = false;
236  switch (stream->stream_type()) {
237  case kStreamVideo:
238  generate_trak_result = GenerateVideoTrak(
239  static_cast<const VideoStreamInfo*>(stream), &trak, i + 1);
240  break;
241  case kStreamAudio:
242  generate_trak_result = GenerateAudioTrak(
243  static_cast<const AudioStreamInfo*>(stream), &trak, i + 1);
244  break;
245  case kStreamText:
246  generate_trak_result = GenerateTextTrak(
247  static_cast<const TextStreamInfo*>(stream), &trak, i + 1);
248  break;
249  default:
250  NOTIMPLEMENTED() << "Not implemented for stream type: "
251  << stream->stream_type();
252  }
253  if (!generate_trak_result)
254  return Status(error::MUXER_FAILURE, "Failed to generate trak.");
255 
256  // Generate EditList if needed. See UpdateEditListOffsetFromSample() for
257  // more information.
258  if (edit_list_offset_.value() > 0) {
259  EditListEntry entry;
260  entry.media_time = edit_list_offset_.value();
261  entry.media_rate_integer = 1;
262  trak.edit.list.edits.push_back(entry);
263  }
264 
265  if (stream->is_encrypted() && options().mp4_params.include_pssh_in_stream) {
266  const auto& key_system_info = stream->encryption_config().key_system_info;
267  moov->pssh.resize(key_system_info.size());
268  for (size_t j = 0; j < key_system_info.size(); j++)
269  moov->pssh[j].raw_box = key_system_info[j].psshs;
270  }
271  }
272 
273  if (options().segment_template.empty()) {
274  segmenter_.reset(new SingleSegmentSegmenter(options(), std::move(ftyp),
275  std::move(moov)));
276  } else {
277  segmenter_.reset(
278  new MultiSegmentSegmenter(options(), std::move(ftyp), std::move(moov)));
279  }
280 
281  const Status segmenter_initialized =
282  segmenter_->Initialize(streams(), muxer_listener(), progress_listener());
283  if (!segmenter_initialized.ok())
284  return segmenter_initialized;
285 
286  FireOnMediaStartEvent();
287  return Status::OK;
288 }
289 
290 Status MP4Muxer::UpdateEditListOffsetFromSample(const MediaSample& sample) {
291  if (edit_list_offset_)
292  return Status::OK;
293 
294  const int64_t pts = sample.pts();
295  const int64_t dts = sample.dts();
296  // An EditList entry is inserted if one of the below conditions occur [4]:
297  // (1) pts > dts for the first sample. Due to Chrome's dts bug [1], dts is
298  // used in buffered range API, while pts is used elsewhere (players,
299  // manifests, and Chrome's own appendWindow check etc.), this
300  // inconsistency creates various problems, including possible stalls
301  // during playback. Since Chrome adjusts pts only when seeing EditList
302  // [2], we can insert an EditList with the time equal to difference of pts
303  // and dts to make aligned buffered ranges using pts and dts. This
304  // effectively workarounds the dts bug. It is also recommended by ISO-BMFF
305  // specification [3].
306  // (2) pts == dts and with pts < 0. This happens for some audio codecs where a
307  // negative presentation timestamp signals that the sample is not supposed
308  // to be shown, i.e. for audio priming. EditList is needed to encode
309  // negative timestamps.
310  // [1] https://crbug.com/718641, fixed but behind MseBufferByPts, still not
311  // enabled as of M67.
312  // [2] This is actually a bug, see https://crbug.com/354518. It looks like
313  // Chrome is planning to enable the fix for [1] before addressing this
314  // bug, so we are safe.
315  // [3] ISO 14496-12:2015 8.6.6.1
316  // It is recommended that such an edit be used to establish a presentation
317  // time of 0 for the first presented sample, when composition offsets are
318  // used.
319  // [4] ISO 23009-19:2018 7.5.13
320  // In two cases, an EditBox containing a single EditListBox with the
321  // following constraints may be present in the CMAF header of a CMAF track
322  // to adjust the presentation time of all media samples in the CMAF track.
323  // a) The first case is a video CMAF track file using v0 TrackRunBoxes
324  // with positive composition offsets to reorder video media samples.
325  // b) The second case is an audio CMAF track where each media sample's
326  // presentation time does not equal its composition time.
327  const int64_t pts_dts_offset = pts - dts;
328  if (pts_dts_offset > 0) {
329  if (pts < 0) {
330  LOG(ERROR) << "Negative presentation timestamp (" << pts
331  << ") is not supported when there is an offset between "
332  "presentation timestamp and decoding timestamp ("
333  << dts << ").";
334  return Status(error::MUXER_FAILURE,
335  "Unsupported negative pts when there is an offset between "
336  "pts and dts.");
337  }
338  edit_list_offset_ = pts_dts_offset;
339  return Status::OK;
340  }
341  if (pts_dts_offset < 0) {
342  LOG(ERROR) << "presentation timestamp (" << pts
343  << ") is not supposed to be greater than decoding timestamp ("
344  << dts << ").";
345  return Status(error::MUXER_FAILURE, "Not expecting pts < dts.");
346  }
347  edit_list_offset_ = std::max(-sample.pts(), static_cast<int64_t>(0));
348  return Status::OK;
349 }
350 
351 void MP4Muxer::InitializeTrak(const StreamInfo* info, Track* trak) {
352  int64_t now = IsoTimeNow();
353  trak->header.creation_time = now;
354  trak->header.modification_time = now;
355  trak->header.duration = 0;
356  trak->media.header.creation_time = now;
357  trak->media.header.modification_time = now;
358  trak->media.header.timescale = info->time_scale();
359  trak->media.header.duration = 0;
360  if (!info->language().empty()) {
361  // Strip off the subtag, if any.
362  std::string main_language = info->language();
363  size_t dash = main_language.find('-');
364  if (dash != std::string::npos) {
365  main_language.erase(dash);
366  }
367 
368  // ISO-639-2/T main language code should be 3 characters.
369  if (main_language.size() != 3) {
370  LOG(WARNING) << "'" << main_language << "' is not a valid ISO-639-2 "
371  << "language code, ignoring.";
372  } else {
373  trak->media.header.language.code = main_language;
374  }
375  }
376 }
377 
378 bool MP4Muxer::GenerateVideoTrak(const VideoStreamInfo* video_info,
379  Track* trak,
380  uint32_t track_id) {
381  InitializeTrak(video_info, trak);
382 
383  // width and height specify the track's visual presentation size as
384  // fixed-point 16.16 values.
385  uint32_t pixel_width = video_info->pixel_width();
386  uint32_t pixel_height = video_info->pixel_height();
387  if (pixel_width == 0 || pixel_height == 0) {
388  LOG(WARNING) << "pixel width/height are not set. Assuming 1:1.";
389  pixel_width = 1;
390  pixel_height = 1;
391  }
392  const double sample_aspect_ratio =
393  static_cast<double>(pixel_width) / pixel_height;
394  trak->header.width = video_info->width() * sample_aspect_ratio * 0x10000;
395  trak->header.height = video_info->height() * 0x10000;
396 
397  VideoSampleEntry video;
398  video.format =
399  CodecToFourCC(video_info->codec(), video_info->h26x_stream_format());
400  video.width = video_info->width();
401  video.height = video_info->height();
402  video.codec_configuration.data = video_info->codec_config();
403  if (pixel_width != 1 || pixel_height != 1) {
404  video.pixel_aspect.h_spacing = pixel_width;
405  video.pixel_aspect.v_spacing = pixel_height;
406  }
407 
408  SampleDescription& sample_description =
409  trak->media.information.sample_table.description;
410  sample_description.type = kVideo;
411  sample_description.video_entries.push_back(video);
412 
413  if (video_info->is_encrypted()) {
414  if (video_info->has_clear_lead()) {
415  // Add a second entry for clear content.
416  sample_description.video_entries.push_back(video);
417  }
418  // Convert the first entry to an encrypted entry.
419  VideoSampleEntry& entry = sample_description.video_entries[0];
420  GenerateSinf(entry.format, video_info->encryption_config(), &entry.sinf);
421  entry.format = FOURCC_encv;
422  }
423  return true;
424 }
425 
426 bool MP4Muxer::GenerateAudioTrak(const AudioStreamInfo* audio_info,
427  Track* trak,
428  uint32_t track_id) {
429  InitializeTrak(audio_info, trak);
430 
431  trak->header.volume = 0x100;
432 
433  AudioSampleEntry audio;
434  audio.format =
435  CodecToFourCC(audio_info->codec(), H26xStreamFormat::kUnSpecified);
436  switch(audio_info->codec()){
437  case kCodecAAC:
438  audio.esds.es_descriptor.set_object_type(
439  ObjectType::kISO_14496_3); // MPEG4 AAC.
440  audio.esds.es_descriptor.set_esid(track_id);
441  audio.esds.es_descriptor.set_decoder_specific_info(
442  audio_info->codec_config());
443  audio.esds.es_descriptor.set_max_bitrate(audio_info->max_bitrate());
444  audio.esds.es_descriptor.set_avg_bitrate(audio_info->avg_bitrate());
445  break;
446  case kCodecDTSC:
447  case kCodecDTSH:
448  case kCodecDTSL:
449  case kCodecDTSE:
450  case kCodecDTSM:
451  audio.ddts.extra_data = audio_info->codec_config();
452  audio.ddts.max_bitrate = audio_info->max_bitrate();
453  audio.ddts.avg_bitrate = audio_info->avg_bitrate();
454  audio.ddts.sampling_frequency = audio_info->sampling_frequency();
455  audio.ddts.pcm_sample_depth = audio_info->sample_bits();
456  break;
457  case kCodecAC3:
458  audio.dac3.data = audio_info->codec_config();
459  break;
460  case kCodecEAC3:
461  audio.dec3.data = audio_info->codec_config();
462  break;
463  case kCodecFlac:
464  audio.dfla.data = audio_info->codec_config();
465  break;
466  case kCodecOpus:
467  audio.dops.opus_identification_header = audio_info->codec_config();
468  break;
469  default:
470  NOTIMPLEMENTED() << " Unsupported audio codec " << audio_info->codec();
471  return false;
472  }
473 
474  if (audio_info->codec() == kCodecAC3 || audio_info->codec() == kCodecEAC3) {
475  // AC3 and EC3 does not fill in actual channel count and sample size in
476  // sample description entry. Instead, two constants are used.
477  audio.channelcount = 2;
478  audio.samplesize = 16;
479  } else {
480  audio.channelcount = audio_info->num_channels();
481  audio.samplesize = audio_info->sample_bits();
482  }
483  audio.samplerate = audio_info->sampling_frequency();
484  SampleTable& sample_table = trak->media.information.sample_table;
485  SampleDescription& sample_description = sample_table.description;
486  sample_description.type = kAudio;
487  sample_description.audio_entries.push_back(audio);
488 
489  if (audio_info->is_encrypted()) {
490  if (audio_info->has_clear_lead()) {
491  // Add a second entry for clear content.
492  sample_description.audio_entries.push_back(audio);
493  }
494  // Convert the first entry to an encrypted entry.
495  AudioSampleEntry& entry = sample_description.audio_entries[0];
496  GenerateSinf(entry.format, audio_info->encryption_config(), &entry.sinf);
497  entry.format = FOURCC_enca;
498  }
499 
500  // Opus requires at least one sample group description box and at least one
501  // sample to group box with grouping type 'roll' within sample table box.
502  if (audio_info->codec() == kCodecOpus) {
503  sample_table.sample_group_descriptions.resize(1);
504  SampleGroupDescription& sample_group_description =
505  sample_table.sample_group_descriptions.back();
506  sample_group_description.grouping_type = FOURCC_roll;
507  sample_group_description.audio_roll_recovery_entries.resize(1);
508  // The roll distance is expressed in sample units and always takes negative
509  // values.
510  const uint64_t kNanosecondsPerSecond = 1000000000ull;
511  sample_group_description.audio_roll_recovery_entries[0].roll_distance =
512  (0 - (audio_info->seek_preroll_ns() * audio.samplerate +
513  kNanosecondsPerSecond / 2)) /
514  kNanosecondsPerSecond;
515 
516  sample_table.sample_to_groups.resize(1);
517  SampleToGroup& sample_to_group = sample_table.sample_to_groups.back();
518  sample_to_group.grouping_type = FOURCC_roll;
519 
520  sample_to_group.entries.resize(1);
521  SampleToGroupEntry& sample_to_group_entry = sample_to_group.entries.back();
522  // All samples are in track fragments.
523  sample_to_group_entry.sample_count = 0;
524  sample_to_group_entry.group_description_index =
525  SampleToGroupEntry::kTrackGroupDescriptionIndexBase + 1;
526  } else if (audio_info->seek_preroll_ns() != 0) {
527  LOG(WARNING) << "Unexpected seek preroll for codec " << audio_info->codec();
528  }
529  return true;
530 }
531 
532 bool MP4Muxer::GenerateTextTrak(const TextStreamInfo* text_info,
533  Track* trak,
534  uint32_t track_id) {
535  InitializeTrak(text_info, trak);
536 
537  if (text_info->codec_string() == "wvtt") {
538  // Handle WebVTT.
539  TextSampleEntry webvtt;
540  webvtt.format = FOURCC_wvtt;
541  webvtt.config.config.assign(text_info->codec_config().begin(),
542  text_info->codec_config().end());
543  // TODO(rkuroiwa): This should be the source file URI(s). Putting bogus
544  // string for now so that the box will be there for samples with overlapping
545  // cues.
546  webvtt.label.source_label = "source_label";
547  SampleDescription& sample_description =
548  trak->media.information.sample_table.description;
549  sample_description.type = kText;
550  sample_description.text_entries.push_back(webvtt);
551  return true;
552  }
553  NOTIMPLEMENTED() << text_info->codec_string()
554  << " handling not implemented yet.";
555  return false;
556 }
557 
558 base::Optional<Range> MP4Muxer::GetInitRangeStartAndEnd() {
559  size_t range_offset = 0;
560  size_t range_size = 0;
561  const bool has_range = segmenter_->GetInitRange(&range_offset, &range_size);
562 
563  if (!has_range)
564  return base::nullopt;
565 
566  Range range;
567  SetStartAndEndFromOffsetAndSize(range_offset, range_size, &range);
568  return range;
569 }
570 
571 base::Optional<Range> MP4Muxer::GetIndexRangeStartAndEnd() {
572  size_t range_offset = 0;
573  size_t range_size = 0;
574  const bool has_range = segmenter_->GetIndexRange(&range_offset, &range_size);
575 
576  if (!has_range)
577  return base::nullopt;
578 
579  Range range;
580  SetStartAndEndFromOffsetAndSize(range_offset, range_size, &range);
581  return range;
582 }
583 
584 void MP4Muxer::FireOnMediaStartEvent() {
585  if (!muxer_listener())
586  return;
587 
588  if (streams().size() > 1) {
589  LOG(ERROR) << "MuxerListener cannot take more than 1 stream.";
590  return;
591  }
592  DCHECK(!streams().empty()) << "Media started without a stream.";
593 
594  const uint32_t timescale = segmenter_->GetReferenceTimeScale();
595  muxer_listener()->OnMediaStart(options(), *streams().front(), timescale,
596  MuxerListener::kContainerMp4);
597 }
598 
599 void MP4Muxer::FireOnMediaEndEvent() {
600  if (!muxer_listener())
601  return;
602 
603  MuxerListener::MediaRanges media_range;
604  media_range.init_range = GetInitRangeStartAndEnd();
605  media_range.index_range = GetIndexRangeStartAndEnd();
606  media_range.subsegment_ranges = segmenter_->GetSegmentRanges();
607 
608  const float duration_seconds = static_cast<float>(segmenter_->GetDuration());
609  muxer_listener()->OnMediaEnd(media_range, duration_seconds);
610 }
611 
612 uint64_t MP4Muxer::IsoTimeNow() {
613  // Time in seconds from Jan. 1, 1904 to epoch time, i.e. Jan. 1, 1970.
614  const uint64_t kIsomTimeOffset = 2082844800l;
615  return kIsomTimeOffset +
616  (clock() ? clock()->Now() : base::Time::Now()).ToDoubleT();
617 }
618 
619 } // namespace mp4
620 } // namespace media
621 } // namespace shaka
base::Optional< Range > init_range
Range of the initialization section of a segment.
Abstract class holds stream information.
Definition: stream_info.h:61
base::Optional< Range > index_range
Range of the index section of a segment.
MP4Muxer(const MuxerOptions &options)
Create a MP4Muxer object from MuxerOptions.
Definition: mp4_muxer.cc:140
All the methods that are virtual are virtual for mocking.
This structure contains the list of configuration options for Muxer.
Definition: muxer_options.h:20
Mp4OutputParams mp4_params
MP4 (ISO-BMFF) specific parameters.
Definition: muxer_options.h:25
virtual void OnMediaEnd(const MediaRanges &media_ranges, float duration_seconds)=0
Class to hold a media sample.
Definition: media_sample.h:22
virtual void OnMediaStart(const MuxerOptions &muxer_options, const StreamInfo &stream_info, uint32_t time_scale, ContainerType container_type)=0
Holds video stream information.
Holds audio stream information.