DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
mp4_media_parser.cc
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "packager/media/formats/mp4/mp4_media_parser.h"
6 
7 #include <algorithm>
8 #include <limits>
9 
10 #include "packager/base/callback.h"
11 #include "packager/base/callback_helpers.h"
12 #include "packager/base/logging.h"
13 #include "packager/base/memory/ref_counted.h"
14 #include "packager/base/strings/string_number_conversions.h"
15 #include "packager/media/base/audio_stream_info.h"
16 #include "packager/media/base/buffer_reader.h"
17 #include "packager/media/base/decrypt_config.h"
18 #include "packager/media/base/key_source.h"
19 #include "packager/media/base/macros.h"
20 #include "packager/media/base/media_sample.h"
21 #include "packager/media/base/rcheck.h"
22 #include "packager/media/base/video_stream_info.h"
23 #include "packager/media/codecs/avc_decoder_configuration_record.h"
24 #include "packager/media/codecs/es_descriptor.h"
25 #include "packager/media/codecs/hevc_decoder_configuration_record.h"
26 #include "packager/media/codecs/vp_codec_configuration_record.h"
27 #include "packager/media/file/file.h"
28 #include "packager/media/file/file_closer.h"
29 #include "packager/media/formats/mp4/box_definitions.h"
30 #include "packager/media/formats/mp4/box_reader.h"
31 #include "packager/media/formats/mp4/track_run_iterator.h"
32 
33 namespace shaka {
34 namespace media {
35 namespace mp4 {
36 namespace {
37 
38 uint64_t Rescale(uint64_t time_in_old_scale,
39  uint32_t old_scale,
40  uint32_t new_scale) {
41  return (static_cast<double>(time_in_old_scale) / old_scale) * new_scale;
42 }
43 
44 Codec FourCCToCodec(FourCC fourcc) {
45  switch (fourcc) {
46  case FOURCC_avc1:
47  return kCodecH264;
48  case FOURCC_hev1:
49  return kCodecHEV1;
50  case FOURCC_hvc1:
51  return kCodecHVC1;
52  case FOURCC_vp08:
53  return kCodecVP8;
54  case FOURCC_vp09:
55  return kCodecVP9;
56  case FOURCC_vp10:
57  return kCodecVP10;
58  case FOURCC_Opus:
59  return kCodecOpus;
60  case FOURCC_dtsc:
61  return kCodecDTSC;
62  case FOURCC_dtsh:
63  return kCodecDTSH;
64  case FOURCC_dtsl:
65  return kCodecDTSL;
66  case FOURCC_dtse:
67  return kCodecDTSE;
68  case FOURCC_dtsp:
69  return kCodecDTSP;
70  case FOURCC_dtsm:
71  return kCodecDTSM;
72  case FOURCC_ac_3:
73  return kCodecAC3;
74  case FOURCC_ec_3:
75  return kCodecEAC3;
76  default:
77  return kUnknownCodec;
78  }
79 }
80 
81 // Default DTS audio number of channels for 5.1 channel layout.
82 const uint8_t kDtsAudioNumChannels = 6;
83 const uint64_t kNanosecondsPerSecond = 1000000000ull;
84 
85 } // namespace
86 
87 MP4MediaParser::MP4MediaParser()
88  : state_(kWaitingForInit),
89  decryption_key_source_(NULL),
90  moof_head_(0),
91  mdat_tail_(0) {}
92 
93 MP4MediaParser::~MP4MediaParser() {}
94 
95 void MP4MediaParser::Init(const InitCB& init_cb,
96  const NewSampleCB& new_sample_cb,
97  KeySource* decryption_key_source) {
98  DCHECK_EQ(state_, kWaitingForInit);
99  DCHECK(init_cb_.is_null());
100  DCHECK(!init_cb.is_null());
101  DCHECK(!new_sample_cb.is_null());
102 
103  ChangeState(kParsingBoxes);
104  init_cb_ = init_cb;
105  new_sample_cb_ = new_sample_cb;
106  decryption_key_source_ = decryption_key_source;
107  if (decryption_key_source)
108  decryptor_source_.reset(new DecryptorSource(decryption_key_source));
109 }
110 
111 void MP4MediaParser::Reset() {
112  queue_.Reset();
113  runs_.reset();
114  moof_head_ = 0;
115  mdat_tail_ = 0;
116 }
117 
119  DCHECK_NE(state_, kWaitingForInit);
120  Reset();
121  ChangeState(kParsingBoxes);
122  return true;
123 }
124 
125 bool MP4MediaParser::Parse(const uint8_t* buf, int size) {
126  DCHECK_NE(state_, kWaitingForInit);
127 
128  if (state_ == kError)
129  return false;
130 
131  queue_.Push(buf, size);
132 
133  bool result, err = false;
134 
135  do {
136  if (state_ == kParsingBoxes) {
137  result = ParseBox(&err);
138  } else {
139  DCHECK_EQ(kEmittingSamples, state_);
140  result = EnqueueSample(&err);
141  if (result) {
142  int64_t max_clear = runs_->GetMaxClearOffset() + moof_head_;
143  err = !ReadAndDiscardMDATsUntil(max_clear);
144  }
145  }
146  } while (result && !err);
147 
148  if (err) {
149  DLOG(ERROR) << "Error while parsing MP4";
150  moov_.reset();
151  Reset();
152  ChangeState(kError);
153  return false;
154  }
155 
156  return true;
157 }
158 
159 bool MP4MediaParser::LoadMoov(const std::string& file_path) {
160  std::unique_ptr<File, FileCloser> file(
161  File::OpenWithNoBuffering(file_path.c_str(), "r"));
162  if (!file) {
163  LOG(ERROR) << "Unable to open media file '" << file_path << "'";
164  return false;
165  }
166  if (!file->Seek(0)) {
167  LOG(WARNING) << "Filesystem does not support seeking on file '" << file_path
168  << "'";
169  return false;
170  }
171 
172  uint64_t file_position(0);
173  bool mdat_seen(false);
174  while (true) {
175  const uint32_t kBoxHeaderReadSize(16);
176  std::vector<uint8_t> buffer(kBoxHeaderReadSize);
177  int64_t bytes_read = file->Read(&buffer[0], kBoxHeaderReadSize);
178  if (bytes_read == 0) {
179  LOG(ERROR) << "Could not find 'moov' box in file '" << file_path << "'";
180  return false;
181  }
182  if (bytes_read < kBoxHeaderReadSize) {
183  LOG(ERROR) << "Error reading media file '" << file_path << "'";
184  return false;
185  }
186  uint64_t box_size;
187  FourCC box_type;
188  bool err;
189  if (!BoxReader::StartBox(&buffer[0], kBoxHeaderReadSize, &box_type,
190  &box_size, &err)) {
191  LOG(ERROR) << "Could not start box from file '" << file_path << "'";
192  return false;
193  }
194  if (box_type == FOURCC_mdat) {
195  mdat_seen = true;
196  } else if (box_type == FOURCC_moov) {
197  if (!mdat_seen) {
198  // 'moov' is before 'mdat'. Nothing to do.
199  break;
200  }
201  // 'mdat' before 'moov'. Read and parse 'moov'.
202  if (!Parse(&buffer[0], bytes_read)) {
203  LOG(ERROR) << "Error parsing mp4 file '" << file_path << "'";
204  return false;
205  }
206  uint64_t bytes_to_read = box_size - bytes_read;
207  buffer.resize(bytes_to_read);
208  while (bytes_to_read > 0) {
209  bytes_read = file->Read(&buffer[0], bytes_to_read);
210  if (bytes_read <= 0) {
211  LOG(ERROR) << "Error reading 'moov' contents from file '" << file_path
212  << "'";
213  return false;
214  }
215  if (!Parse(&buffer[0], bytes_read)) {
216  LOG(ERROR) << "Error parsing mp4 file '" << file_path << "'";
217  return false;
218  }
219  bytes_to_read -= bytes_read;
220  }
221  queue_.Reset(); // So that we don't need to adjust data offsets.
222  mdat_tail_ = 0; // So it will skip boxes until mdat.
223  break; // Done.
224  }
225  file_position += box_size;
226  if (!file->Seek(file_position)) {
227  LOG(ERROR) << "Error skipping box in mp4 file '" << file_path << "'";
228  return false;
229  }
230  }
231  return true;
232 }
233 
234 bool MP4MediaParser::ParseBox(bool* err) {
235  const uint8_t* buf;
236  int size;
237  queue_.Peek(&buf, &size);
238  if (!size)
239  return false;
240 
241  std::unique_ptr<BoxReader> reader(BoxReader::ReadBox(buf, size, err));
242  if (reader.get() == NULL)
243  return false;
244 
245  if (reader->type() == FOURCC_mdat) {
246  // The code ends up here only if a MOOV box is not yet seen.
247  DCHECK(!moov_);
248 
249  NOTIMPLEMENTED() << " Files with MDAT before MOOV is not supported yet.";
250  *err = true;
251  return false;
252  }
253 
254  // Set up mdat offset for ReadMDATsUntil().
255  mdat_tail_ = queue_.head() + reader->size();
256 
257  if (reader->type() == FOURCC_moov) {
258  *err = !ParseMoov(reader.get());
259  } else if (reader->type() == FOURCC_moof) {
260  moof_head_ = queue_.head();
261  *err = !ParseMoof(reader.get());
262 
263  // Return early to avoid evicting 'moof' data from queue. Auxiliary info may
264  // be located anywhere in the file, including inside the 'moof' itself.
265  // (Since 'default-base-is-moof' is mandated, no data references can come
266  // before the head of the 'moof', so keeping this box around is sufficient.)
267  return !(*err);
268  } else {
269  VLOG(2) << "Skipping top-level box: " << FourCCToString(reader->type());
270  }
271 
272  queue_.Pop(reader->size());
273  return !(*err);
274 }
275 
276 bool MP4MediaParser::ParseMoov(BoxReader* reader) {
277  if (moov_)
278  return true; // Already parsed the 'moov' box.
279 
280  moov_.reset(new Movie);
281  RCHECK(moov_->Parse(reader));
282  runs_.reset();
283 
284  std::vector<scoped_refptr<StreamInfo> > streams;
285 
286  for (std::vector<Track>::const_iterator track = moov_->tracks.begin();
287  track != moov_->tracks.end(); ++track) {
288  const uint32_t timescale = track->media.header.timescale;
289 
290  // Calculate duration (based on timescale).
291  uint64_t duration = 0;
292  if (track->media.header.duration > 0) {
293  duration = track->media.header.duration;
294  } else if (moov_->extends.header.fragment_duration > 0) {
295  DCHECK(moov_->header.timescale != 0);
296  duration = Rescale(moov_->extends.header.fragment_duration,
297  moov_->header.timescale,
298  timescale);
299  } else if (moov_->header.duration > 0 &&
300  moov_->header.duration != std::numeric_limits<uint64_t>::max()) {
301  DCHECK(moov_->header.timescale != 0);
302  duration =
303  Rescale(moov_->header.duration, moov_->header.timescale, timescale);
304  }
305 
306  const SampleDescription& samp_descr =
307  track->media.information.sample_table.description;
308 
309  size_t desc_idx = 0;
310 
311  // Read sample description index from mvex if it exists otherwise read
312  // from the first entry in Sample To Chunk box.
313  if (moov_->extends.tracks.size() > 0) {
314  for (size_t t = 0; t < moov_->extends.tracks.size(); t++) {
315  const TrackExtends& trex = moov_->extends.tracks[t];
316  if (trex.track_id == track->header.track_id) {
317  desc_idx = trex.default_sample_description_index;
318  break;
319  }
320  }
321  } else {
322  const std::vector<ChunkInfo>& chunk_info =
323  track->media.information.sample_table.sample_to_chunk.chunk_info;
324  RCHECK(chunk_info.size() > 0);
325  desc_idx = chunk_info[0].sample_description_index;
326  }
327  RCHECK(desc_idx > 0);
328  desc_idx -= 1; // BMFF descriptor index is one-based
329 
330  if (samp_descr.type == kAudio) {
331  RCHECK(!samp_descr.audio_entries.empty());
332 
333  // It is not uncommon to find otherwise-valid files with incorrect sample
334  // description indices, so we fail gracefully in that case.
335  if (desc_idx >= samp_descr.audio_entries.size())
336  desc_idx = 0;
337 
338  const AudioSampleEntry& entry = samp_descr.audio_entries[desc_idx];
339  const FourCC actual_format = entry.GetActualFormat();
340  Codec codec = FourCCToCodec(actual_format);
341  uint8_t num_channels = 0;
342  uint32_t sampling_frequency = 0;
343  uint64_t codec_delay_ns = 0;
344  uint8_t audio_object_type = 0;
345  uint32_t max_bitrate = 0;
346  uint32_t avg_bitrate = 0;
347  std::vector<uint8_t> codec_config;
348 
349  switch (actual_format) {
350  case FOURCC_mp4a:
351  // Check if it is MPEG4 AAC defined in ISO 14496 Part 3 or
352  // supported MPEG2 AAC variants.
353  if (entry.esds.es_descriptor.IsAAC()) {
354  codec = kCodecAAC;
355  const AACAudioSpecificConfig& aac_audio_specific_config =
356  entry.esds.aac_audio_specific_config;
357  num_channels = aac_audio_specific_config.num_channels();
358  sampling_frequency = aac_audio_specific_config.frequency();
359  audio_object_type = aac_audio_specific_config.audio_object_type();
360  codec_config = entry.esds.es_descriptor.decoder_specific_info();
361  break;
362  } else if (entry.esds.es_descriptor.IsDTS()) {
363  ObjectType audio_type = entry.esds.es_descriptor.object_type();
364  switch (audio_type) {
365  case kDTSC:
366  codec = kCodecDTSC;
367  break;
368  case kDTSE:
369  codec = kCodecDTSE;
370  break;
371  case kDTSH:
372  codec = kCodecDTSH;
373  break;
374  case kDTSL:
375  codec = kCodecDTSL;
376  break;
377  default:
378  LOG(ERROR) << "Unsupported audio type " << audio_type
379  << " in stsd box.";
380  return false;
381  }
382  num_channels = entry.esds.aac_audio_specific_config.num_channels();
383  // For dts audio in esds, current supported number of channels is 6
384  // as the only supported channel layout is 5.1.
385  if (num_channels != kDtsAudioNumChannels) {
386  LOG(ERROR) << "Unsupported channel count " << num_channels
387  << " for audio type " << audio_type << ".";
388  return false;
389  }
390  sampling_frequency = entry.samplerate;
391  max_bitrate = entry.esds.es_descriptor.max_bitrate();
392  avg_bitrate = entry.esds.es_descriptor.avg_bitrate();
393  } else {
394  LOG(ERROR) << "Unsupported audio format 0x" << std::hex
395  << actual_format << " in stsd box.";
396  return false;
397  }
398  break;
399  case FOURCC_dtsc:
400  FALLTHROUGH_INTENDED;
401  case FOURCC_dtsh:
402  FALLTHROUGH_INTENDED;
403  case FOURCC_dtsl:
404  FALLTHROUGH_INTENDED;
405  case FOURCC_dtse:
406  FALLTHROUGH_INTENDED;
407  case FOURCC_dtsm:
408  codec_config = entry.ddts.extra_data;
409  max_bitrate = entry.ddts.max_bitrate;
410  avg_bitrate = entry.ddts.avg_bitrate;
411  num_channels = entry.channelcount;
412  sampling_frequency = entry.samplerate;
413  break;
414  case FOURCC_ac_3:
415  codec_config = entry.dac3.data;
416  num_channels = entry.channelcount;
417  sampling_frequency = entry.samplerate;
418  break;
419  case FOURCC_ec_3:
420  codec_config = entry.dec3.data;
421  num_channels = entry.channelcount;
422  sampling_frequency = entry.samplerate;
423  break;
424  case FOURCC_Opus:
425  codec_config = entry.dops.opus_identification_header;
426  num_channels = entry.channelcount;
427  sampling_frequency = entry.samplerate;
428  RCHECK(sampling_frequency != 0);
429  codec_delay_ns =
430  entry.dops.preskip * kNanosecondsPerSecond / sampling_frequency;
431  break;
432  default:
433  LOG(ERROR) << "Unsupported audio format 0x" << std::hex
434  << actual_format << " in stsd box.";
435  return false;
436  }
437 
438  // Extract possible seek preroll.
439  uint64_t seek_preroll_ns = 0;
440  for (const auto& sample_group_description :
441  track->media.information.sample_table.sample_group_descriptions) {
442  if (sample_group_description.grouping_type != FOURCC_roll)
443  continue;
444  const auto& audio_roll_recovery_entries =
445  sample_group_description.audio_roll_recovery_entries;
446  if (audio_roll_recovery_entries.size() != 1) {
447  LOG(WARNING) << "Unexpected number of entries in "
448  "SampleGroupDescription table with grouping type "
449  "'roll'.";
450  break;
451  }
452  const int16_t roll_distance_in_samples =
453  audio_roll_recovery_entries[0].roll_distance;
454  if (roll_distance_in_samples < 0) {
455  RCHECK(sampling_frequency != 0);
456  seek_preroll_ns = kNanosecondsPerSecond *
457  (-roll_distance_in_samples) / sampling_frequency;
458  } else {
459  LOG(WARNING)
460  << "Roll distance is supposed to be negative, but seeing "
461  << roll_distance_in_samples;
462  }
463  break;
464  }
465 
466  const bool is_encrypted =
467  entry.sinf.info.track_encryption.default_is_protected == 1;
468  DVLOG(1) << "is_audio_track_encrypted_: " << is_encrypted;
469  streams.push_back(new AudioStreamInfo(
470  track->header.track_id, timescale, duration, codec,
471  AudioStreamInfo::GetCodecString(codec, audio_object_type),
472  codec_config.data(), codec_config.size(), entry.samplesize,
473  num_channels, sampling_frequency, seek_preroll_ns, codec_delay_ns,
474  max_bitrate, avg_bitrate, track->media.header.language.code,
475  is_encrypted));
476  }
477 
478  if (samp_descr.type == kVideo) {
479  RCHECK(!samp_descr.video_entries.empty());
480  if (desc_idx >= samp_descr.video_entries.size())
481  desc_idx = 0;
482  const VideoSampleEntry& entry = samp_descr.video_entries[desc_idx];
483 
484  uint32_t coded_width = entry.width;
485  uint32_t coded_height = entry.height;
486  uint32_t pixel_width = entry.pixel_aspect.h_spacing;
487  uint32_t pixel_height = entry.pixel_aspect.v_spacing;
488  if (pixel_width == 0 && pixel_height == 0) {
489  pixel_width = 1;
490  pixel_height = 1;
491  }
492  std::string codec_string;
493  uint8_t nalu_length_size = 0;
494 
495  const FourCC actual_format = entry.GetActualFormat();
496  const Codec video_codec = FourCCToCodec(actual_format);
497  switch (actual_format) {
498  case FOURCC_avc1: {
499  AVCDecoderConfigurationRecord avc_config;
500  if (!avc_config.Parse(entry.codec_configuration.data)) {
501  LOG(ERROR) << "Failed to parse avcc.";
502  return false;
503  }
504  codec_string = avc_config.GetCodecString();
505  nalu_length_size = avc_config.nalu_length_size();
506 
507  if (coded_width != avc_config.coded_width() ||
508  coded_height != avc_config.coded_height()) {
509  LOG(WARNING) << "Resolution in VisualSampleEntry (" << coded_width
510  << "," << coded_height
511  << ") does not match with resolution in "
512  "AVCDecoderConfigurationRecord ("
513  << avc_config.coded_width() << ","
514  << avc_config.coded_height()
515  << "). Use AVCDecoderConfigurationRecord.";
516  coded_width = avc_config.coded_width();
517  coded_height = avc_config.coded_height();
518  }
519 
520  if (pixel_width != avc_config.pixel_width() ||
521  pixel_height != avc_config.pixel_height()) {
522  LOG_IF(WARNING, pixel_width != 1 || pixel_height != 1)
523  << "Pixel aspect ratio in PASP box (" << pixel_width << ","
524  << pixel_height
525  << ") does not match with SAR in AVCDecoderConfigurationRecord "
526  "("
527  << avc_config.pixel_width() << "," << avc_config.pixel_height()
528  << "). Use AVCDecoderConfigurationRecord.";
529  pixel_width = avc_config.pixel_width();
530  pixel_height = avc_config.pixel_height();
531  }
532  break;
533  }
534  case FOURCC_hev1:
535  case FOURCC_hvc1: {
536  HEVCDecoderConfigurationRecord hevc_config;
537  if (!hevc_config.Parse(entry.codec_configuration.data)) {
538  LOG(ERROR) << "Failed to parse hevc.";
539  return false;
540  }
541  codec_string = hevc_config.GetCodecString(video_codec);
542  nalu_length_size = hevc_config.nalu_length_size();
543  break;
544  }
545  case FOURCC_vp08:
546  case FOURCC_vp09:
547  case FOURCC_vp10: {
548  VPCodecConfigurationRecord vp_config;
549  if (!vp_config.ParseMP4(entry.codec_configuration.data)) {
550  LOG(ERROR) << "Failed to parse vpcc.";
551  return false;
552  }
553  codec_string = vp_config.GetCodecString(video_codec);
554  break;
555  }
556  default:
557  LOG(ERROR) << "Unsupported video format "
558  << FourCCToString(actual_format) << " in stsd box.";
559  return false;
560  }
561 
562  const bool is_encrypted =
563  entry.sinf.info.track_encryption.default_is_protected == 1;
564  DVLOG(1) << "is_video_track_encrypted_: " << is_encrypted;
565  scoped_refptr<VideoStreamInfo> video_stream_info(new VideoStreamInfo(
566  track->header.track_id, timescale, duration, video_codec,
567  codec_string, entry.codec_configuration.data.data(),
568  entry.codec_configuration.data.size(), coded_width, coded_height,
569  pixel_width, pixel_height,
570  0, // trick_play_rate
571  nalu_length_size, track->media.header.language.code, is_encrypted));
572 
573  // Set pssh raw data if it has.
574  if (moov_->pssh.size() > 0) {
575  std::vector<uint8_t> pssh_raw_data;
576  for (const auto& pssh : moov_->pssh) {
577  pssh_raw_data.insert(pssh_raw_data.end(), pssh.raw_box.begin(),
578  pssh.raw_box.end());
579  }
580  video_stream_info->set_eme_init_data(pssh_raw_data.data(),
581  pssh_raw_data.size());
582  }
583 
584  streams.push_back(video_stream_info);
585  }
586  }
587 
588  init_cb_.Run(streams);
589  if (!FetchKeysIfNecessary(moov_->pssh))
590  return false;
591  runs_.reset(new TrackRunIterator(moov_.get()));
592  RCHECK(runs_->Init());
593  ChangeState(kEmittingSamples);
594  return true;
595 }
596 
597 bool MP4MediaParser::ParseMoof(BoxReader* reader) {
598  // Must already have initialization segment.
599  RCHECK(moov_.get());
600  MovieFragment moof;
601  RCHECK(moof.Parse(reader));
602  if (!runs_)
603  runs_.reset(new TrackRunIterator(moov_.get()));
604  RCHECK(runs_->Init(moof));
605  if (!FetchKeysIfNecessary(moof.pssh))
606  return false;
607  ChangeState(kEmittingSamples);
608  return true;
609 }
610 
611 bool MP4MediaParser::FetchKeysIfNecessary(
612  const std::vector<ProtectionSystemSpecificHeader>& headers) {
613  if (headers.empty())
614  return true;
615 
616  // An error will be returned later if the samples need to be decrypted.
617  if (!decryption_key_source_)
618  return true;
619 
620  Status status;
621  for (std::vector<ProtectionSystemSpecificHeader>::const_iterator iter =
622  headers.begin(); iter != headers.end(); ++iter) {
623  status = decryption_key_source_->FetchKeys(iter->raw_box);
624  if (!status.ok()) {
625  // If there is an error, try using the next PSSH box and report if none
626  // work.
627  VLOG(1) << "Unable to fetch decryption keys: " << status
628  << ", trying the next PSSH box";
629  continue;
630  }
631  return true;
632  }
633 
634  if (!status.ok()) {
635  LOG(ERROR) << "Error fetching decryption keys: " << status;
636  return false;
637  }
638 
639  LOG(ERROR) << "No viable 'pssh' box found for content decryption.";
640  return false;
641 }
642 
643 bool MP4MediaParser::EnqueueSample(bool* err) {
644  if (!runs_->IsRunValid()) {
645  // Remain in kEnqueueingSamples state, discarding data, until the end of
646  // the current 'mdat' box has been appended to the queue.
647  if (!queue_.Trim(mdat_tail_))
648  return false;
649 
650  ChangeState(kParsingBoxes);
651  return true;
652  }
653 
654  if (!runs_->IsSampleValid()) {
655  runs_->AdvanceRun();
656  return true;
657  }
658 
659  DCHECK(!(*err));
660 
661  const uint8_t* buf;
662  int buf_size;
663  queue_.Peek(&buf, &buf_size);
664  if (!buf_size)
665  return false;
666 
667  // Skip this entire track if it is not audio nor video.
668  if (!runs_->is_audio() && !runs_->is_video())
669  runs_->AdvanceRun();
670 
671  // Attempt to cache the auxiliary information first. Aux info is usually
672  // placed in a contiguous block before the sample data, rather than being
673  // interleaved. If we didn't cache it, this would require that we retain the
674  // start of the segment buffer while reading samples. Aux info is typically
675  // quite small compared to sample data, so this pattern is useful on
676  // memory-constrained devices where the source buffer consumes a substantial
677  // portion of the total system memory.
678  if (runs_->AuxInfoNeedsToBeCached()) {
679  queue_.PeekAt(runs_->aux_info_offset() + moof_head_, &buf, &buf_size);
680  if (buf_size < runs_->aux_info_size())
681  return false;
682  *err = !runs_->CacheAuxInfo(buf, buf_size);
683  return !*err;
684  }
685 
686  int64_t sample_offset = runs_->sample_offset() + moof_head_;
687  queue_.PeekAt(sample_offset, &buf, &buf_size);
688  if (buf_size < runs_->sample_size()) {
689  if (sample_offset < queue_.head()) {
690  LOG(ERROR) << "Incorrect sample offset " << sample_offset
691  << " < " << queue_.head();
692  *err = true;
693  }
694  return false;
695  }
696 
697  scoped_refptr<MediaSample> stream_sample(MediaSample::CopyFrom(
698  buf, runs_->sample_size(), runs_->is_keyframe()));
699  if (runs_->is_encrypted()) {
700  std::unique_ptr<DecryptConfig> decrypt_config = runs_->GetDecryptConfig();
701  if (!decrypt_config) {
702  *err = true;
703  LOG(ERROR) << "Missing decrypt config.";
704  return false;
705  }
706 
707  if (!decryptor_source_) {
708  // If the demuxer does not have the decryptor_source_, store
709  // decrypt_config so that the demuxed sample can be decrypted later.
710  stream_sample->set_decrypt_config(std::move(decrypt_config));
711  stream_sample->set_is_encrypted(true);
712  } else if (!decryptor_source_->DecryptSampleBuffer(
713  decrypt_config.get(), stream_sample->writable_data(),
714  stream_sample->data_size())) {
715  *err = true;
716  LOG(ERROR) << "Cannot decrypt samples.";
717  return false;
718  }
719  }
720 
721  stream_sample->set_dts(runs_->dts());
722  stream_sample->set_pts(runs_->cts());
723  stream_sample->set_duration(runs_->duration());
724 
725  DVLOG(3) << "Pushing frame: "
726  << ", key=" << runs_->is_keyframe()
727  << ", dur=" << runs_->duration()
728  << ", dts=" << runs_->dts()
729  << ", cts=" << runs_->cts()
730  << ", size=" << runs_->sample_size();
731 
732  if (!new_sample_cb_.Run(runs_->track_id(), stream_sample)) {
733  *err = true;
734  LOG(ERROR) << "Failed to process the sample.";
735  return false;
736  }
737 
738  runs_->AdvanceSample();
739  return true;
740 }
741 
742 bool MP4MediaParser::ReadAndDiscardMDATsUntil(const int64_t offset) {
743  bool err = false;
744  while (mdat_tail_ < offset) {
745  const uint8_t* buf;
746  int size;
747  queue_.PeekAt(mdat_tail_, &buf, &size);
748 
749  FourCC type;
750  uint64_t box_sz;
751  if (!BoxReader::StartBox(buf, size, &type, &box_sz, &err))
752  break;
753 
754  mdat_tail_ += box_sz;
755  }
756  queue_.Trim(std::min(mdat_tail_, offset));
757  return !err;
758 }
759 
760 void MP4MediaParser::ChangeState(State new_state) {
761  DVLOG(2) << "Changing state: " << new_state;
762  state_ = new_state;
763 }
764 
765 } // namespace mp4
766 } // namespace media
767 } // namespace shaka
void PeekAt(int64_t offset, const uint8_t **buf, int *size)
static File * OpenWithNoBuffering(const char *file_name, const char *mode)
Definition: file.cc:152
bool Flush() override WARN_UNUSED_RESULT
bool Trim(int64_t max_offset)
bool Parse(const uint8_t *buf, int size) override WARN_UNUSED_RESULT
virtual Status FetchKeys(const std::vector< uint8_t > &pssh_box)=0
static std::string GetCodecString(Codec codec, uint8_t audio_object_type)
bool LoadMoov(const std::string &file_path)
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:30
static scoped_refptr< MediaSample > CopyFrom(const uint8_t *data, size_t size, bool is_key_frame)
Definition: media_sample.cc:45
void Init(const InitCB &init_cb, const NewSampleCB &new_sample_cb, KeySource *decryption_key_source) override
DecryptorSource wraps KeySource and is responsible for decryptor management.
static bool StartBox(const uint8_t *buf, const size_t buf_size, FourCC *type, uint64_t *box_size, bool *err) WARN_UNUSED_RESULT
Definition: box_reader.cc:55
static BoxReader * ReadBox(const uint8_t *buf, const size_t buf_size, bool *err)
Definition: box_reader.cc:37