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