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