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