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::StartBox(&buffer[0], kBoxHeaderReadSize, &box_type,
196  &box_size, &err)) {
197  LOG(ERROR) << "Could not start box from file '" << file_path << "'";
198  return false;
199  }
200  if (box_type == FOURCC_mdat) {
201  mdat_seen = true;
202  } else if (box_type == FOURCC_moov) {
203  if (!mdat_seen) {
204  // 'moov' is before 'mdat'. Nothing to do.
205  break;
206  }
207  // 'mdat' before 'moov'. Read and parse 'moov'.
208  if (!Parse(&buffer[0], bytes_read)) {
209  LOG(ERROR) << "Error parsing mp4 file '" << file_path << "'";
210  return false;
211  }
212  uint64_t bytes_to_read = box_size - bytes_read;
213  buffer.resize(bytes_to_read);
214  while (bytes_to_read > 0) {
215  bytes_read = file->Read(&buffer[0], bytes_to_read);
216  if (bytes_read <= 0) {
217  LOG(ERROR) << "Error reading 'moov' contents from file '" << file_path
218  << "'";
219  return false;
220  }
221  if (!Parse(&buffer[0], bytes_read)) {
222  LOG(ERROR) << "Error parsing mp4 file '" << file_path << "'";
223  return false;
224  }
225  bytes_to_read -= bytes_read;
226  }
227  queue_.Reset(); // So that we don't need to adjust data offsets.
228  mdat_tail_ = 0; // So it will skip boxes until mdat.
229  break; // Done.
230  }
231  file_position += box_size;
232  if (!file->Seek(file_position)) {
233  LOG(ERROR) << "Error skipping box in mp4 file '" << file_path << "'";
234  return false;
235  }
236  }
237  return true;
238 }
239 
240 bool MP4MediaParser::ParseBox(bool* err) {
241  const uint8_t* buf;
242  int size;
243  queue_.Peek(&buf, &size);
244  if (!size)
245  return false;
246 
247  scoped_ptr<BoxReader> reader(BoxReader::ReadBox(buf, size, err));
248  if (reader.get() == NULL)
249  return false;
250 
251  if (reader->type() == FOURCC_mdat) {
252  // The code ends up here only if a MOOV box is not yet seen.
253  DCHECK(!moov_);
254 
255  NOTIMPLEMENTED() << " Files with MDAT before MOOV is not supported yet.";
256  *err = true;
257  return false;
258  }
259 
260  // Set up mdat offset for ReadMDATsUntil().
261  mdat_tail_ = queue_.head() + reader->size();
262 
263  if (reader->type() == FOURCC_moov) {
264  *err = !ParseMoov(reader.get());
265  } else if (reader->type() == FOURCC_moof) {
266  moof_head_ = queue_.head();
267  *err = !ParseMoof(reader.get());
268 
269  // Return early to avoid evicting 'moof' data from queue. Auxiliary info may
270  // be located anywhere in the file, including inside the 'moof' itself.
271  // (Since 'default-base-is-moof' is mandated, no data references can come
272  // before the head of the 'moof', so keeping this box around is sufficient.)
273  return !(*err);
274  } else {
275  VLOG(2) << "Skipping top-level box: " << FourCCToString(reader->type());
276  }
277 
278  queue_.Pop(reader->size());
279  return !(*err);
280 }
281 
282 bool MP4MediaParser::ParseMoov(BoxReader* reader) {
283  if (moov_)
284  return true; // Already parsed the 'moov' box.
285 
286  moov_.reset(new Movie);
287  RCHECK(moov_->Parse(reader));
288  runs_.reset();
289 
290  std::vector<scoped_refptr<StreamInfo> > streams;
291 
292  for (std::vector<Track>::const_iterator track = moov_->tracks.begin();
293  track != moov_->tracks.end(); ++track) {
294  const uint32_t timescale = track->media.header.timescale;
295 
296  // Calculate duration (based on timescale).
297  uint64_t duration = 0;
298  if (track->media.header.duration > 0) {
299  duration = track->media.header.duration;
300  } else if (moov_->extends.header.fragment_duration > 0) {
301  DCHECK(moov_->header.timescale != 0);
302  duration = Rescale(moov_->extends.header.fragment_duration,
303  moov_->header.timescale,
304  timescale);
305  } else if (moov_->header.duration > 0 &&
306  moov_->header.duration != std::numeric_limits<uint64_t>::max()) {
307  DCHECK(moov_->header.timescale != 0);
308  duration =
309  Rescale(moov_->header.duration, moov_->header.timescale, timescale);
310  }
311 
312  const SampleDescription& samp_descr =
313  track->media.information.sample_table.description;
314 
315  size_t desc_idx = 0;
316 
317  // Read sample description index from mvex if it exists otherwise read
318  // from the first entry in Sample To Chunk box.
319  if (moov_->extends.tracks.size() > 0) {
320  for (size_t t = 0; t < moov_->extends.tracks.size(); t++) {
321  const TrackExtends& trex = moov_->extends.tracks[t];
322  if (trex.track_id == track->header.track_id) {
323  desc_idx = trex.default_sample_description_index;
324  break;
325  }
326  }
327  } else {
328  const std::vector<ChunkInfo>& chunk_info =
329  track->media.information.sample_table.sample_to_chunk.chunk_info;
330  RCHECK(chunk_info.size() > 0);
331  desc_idx = chunk_info[0].sample_description_index;
332  }
333  RCHECK(desc_idx > 0);
334  desc_idx -= 1; // BMFF descriptor index is one-based
335 
336  if (samp_descr.type == kAudio) {
337  RCHECK(!samp_descr.audio_entries.empty());
338 
339  // It is not uncommon to find otherwise-valid files with incorrect sample
340  // description indices, so we fail gracefully in that case.
341  if (desc_idx >= samp_descr.audio_entries.size())
342  desc_idx = 0;
343 
344  const AudioSampleEntry& entry = samp_descr.audio_entries[desc_idx];
345  const FourCC actual_format = entry.GetActualFormat();
346  AudioCodec codec = FourCCToAudioCodec(actual_format);
347  uint8_t num_channels = 0;
348  uint32_t sampling_frequency = 0;
349  uint64_t codec_delay_ns = 0;
350  uint8_t audio_object_type = 0;
351  uint32_t max_bitrate = 0;
352  uint32_t avg_bitrate = 0;
353  std::vector<uint8_t> codec_config;
354 
355  switch (actual_format) {
356  case FOURCC_mp4a:
357  // Check if it is MPEG4 AAC defined in ISO 14496 Part 3 or
358  // supported MPEG2 AAC variants.
359  if (entry.esds.es_descriptor.IsAAC()) {
360  codec = kCodecAAC;
361  const AACAudioSpecificConfig& aac_audio_specific_config =
362  entry.esds.aac_audio_specific_config;
363  num_channels = aac_audio_specific_config.num_channels();
364  sampling_frequency = aac_audio_specific_config.frequency();
365  audio_object_type = aac_audio_specific_config.audio_object_type();
366  codec_config = entry.esds.es_descriptor.decoder_specific_info();
367  break;
368  } else if (entry.esds.es_descriptor.IsDTS()) {
369  ObjectType audio_type = entry.esds.es_descriptor.object_type();
370  switch (audio_type) {
371  case kDTSC:
372  codec = kCodecDTSC;
373  break;
374  case kDTSE:
375  codec = kCodecDTSE;
376  break;
377  case kDTSH:
378  codec = kCodecDTSH;
379  break;
380  case kDTSL:
381  codec = kCodecDTSL;
382  break;
383  default:
384  LOG(ERROR) << "Unsupported audio type " << audio_type
385  << " in stsd box.";
386  return false;
387  }
388  num_channels = entry.esds.aac_audio_specific_config.num_channels();
389  // For dts audio in esds, current supported number of channels is 6
390  // as the only supported channel layout is 5.1.
391  if (num_channels != kDtsAudioNumChannels) {
392  LOG(ERROR) << "Unsupported channel count " << num_channels
393  << " for audio type " << audio_type << ".";
394  return false;
395  }
396  sampling_frequency = entry.samplerate;
397  max_bitrate = entry.esds.es_descriptor.max_bitrate();
398  avg_bitrate = entry.esds.es_descriptor.avg_bitrate();
399  } else {
400  LOG(ERROR) << "Unsupported audio format 0x" << std::hex
401  << actual_format << " in stsd box.";
402  return false;
403  }
404  break;
405  case FOURCC_dtsc:
406  FALLTHROUGH_INTENDED;
407  case FOURCC_dtsh:
408  FALLTHROUGH_INTENDED;
409  case FOURCC_dtsl:
410  FALLTHROUGH_INTENDED;
411  case FOURCC_dtse:
412  FALLTHROUGH_INTENDED;
413  case FOURCC_dtsm:
414  codec_config = entry.ddts.extra_data;
415  max_bitrate = entry.ddts.max_bitrate;
416  avg_bitrate = entry.ddts.avg_bitrate;
417  num_channels = entry.channelcount;
418  sampling_frequency = entry.samplerate;
419  break;
420  case FOURCC_ac_3:
421  codec_config = entry.dac3.data;
422  num_channels = entry.channelcount;
423  sampling_frequency = entry.samplerate;
424  break;
425  case FOURCC_ec_3:
426  codec_config = entry.dec3.data;
427  num_channels = entry.channelcount;
428  sampling_frequency = entry.samplerate;
429  break;
430  case FOURCC_Opus:
431  codec_config = entry.dops.opus_identification_header;
432  num_channels = entry.channelcount;
433  sampling_frequency = entry.samplerate;
434  RCHECK(sampling_frequency != 0);
435  codec_delay_ns =
436  entry.dops.preskip * kNanosecondsPerSecond / sampling_frequency;
437  break;
438  default:
439  LOG(ERROR) << "Unsupported audio format 0x" << std::hex
440  << actual_format << " in stsd box.";
441  return false;
442  }
443 
444  // Extract possible seek preroll.
445  uint64_t seek_preroll_ns = 0;
446  for (const auto& sample_group_description :
447  track->media.information.sample_table.sample_group_descriptions) {
448  if (sample_group_description.grouping_type != FOURCC_roll)
449  continue;
450  const auto& audio_roll_recovery_entries =
451  sample_group_description.audio_roll_recovery_entries;
452  if (audio_roll_recovery_entries.size() != 1) {
453  LOG(WARNING) << "Unexpected number of entries in "
454  "SampleGroupDescription table with grouping type "
455  "'roll'.";
456  break;
457  }
458  const int16_t roll_distance_in_samples =
459  audio_roll_recovery_entries[0].roll_distance;
460  if (roll_distance_in_samples < 0) {
461  RCHECK(sampling_frequency != 0);
462  seek_preroll_ns = kNanosecondsPerSecond *
463  (-roll_distance_in_samples) / sampling_frequency;
464  } else {
465  LOG(WARNING)
466  << "Roll distance is supposed to be negative, but seeing "
467  << roll_distance_in_samples;
468  }
469  break;
470  }
471 
472  const bool is_encrypted =
473  entry.sinf.info.track_encryption.default_is_protected == 1;
474  DVLOG(1) << "is_audio_track_encrypted_: " << is_encrypted;
475  streams.push_back(new AudioStreamInfo(
476  track->header.track_id,
477  timescale,
478  duration,
479  codec,
480  AudioStreamInfo::GetCodecString(codec, audio_object_type),
481  track->media.header.language.code,
482  entry.samplesize,
483  num_channels,
484  sampling_frequency,
485  seek_preroll_ns,
486  codec_delay_ns,
487  max_bitrate,
488  avg_bitrate,
489  codec_config.data(),
490  codec_config.size(),
491  is_encrypted));
492  }
493 
494  if (samp_descr.type == kVideo) {
495  RCHECK(!samp_descr.video_entries.empty());
496  if (desc_idx >= samp_descr.video_entries.size())
497  desc_idx = 0;
498  const VideoSampleEntry& entry = samp_descr.video_entries[desc_idx];
499 
500  uint32_t coded_width = entry.width;
501  uint32_t coded_height = entry.height;
502  uint32_t pixel_width = entry.pixel_aspect.h_spacing;
503  uint32_t pixel_height = entry.pixel_aspect.v_spacing;
504  if (pixel_width == 0 && pixel_height == 0) {
505  pixel_width = 1;
506  pixel_height = 1;
507  }
508  std::string codec_string;
509  uint8_t nalu_length_size = 0;
510 
511  const FourCC actual_format = entry.GetActualFormat();
512  const VideoCodec video_codec = FourCCToVideoCodec(actual_format);
513  switch (actual_format) {
514  case FOURCC_avc1: {
515  AVCDecoderConfigurationRecord avc_config;
516  if (!avc_config.Parse(entry.codec_configuration.data)) {
517  LOG(ERROR) << "Failed to parse avcc.";
518  return false;
519  }
520  codec_string = avc_config.GetCodecString();
521  nalu_length_size = avc_config.nalu_length_size();
522 
523  if (coded_width != avc_config.coded_width() ||
524  coded_height != avc_config.coded_height()) {
525  LOG(WARNING) << "Resolution in VisualSampleEntry (" << coded_width
526  << "," << coded_height
527  << ") does not match with resolution in "
528  "AVCDecoderConfigurationRecord ("
529  << avc_config.coded_width() << ","
530  << avc_config.coded_height()
531  << "). Use AVCDecoderConfigurationRecord.";
532  coded_width = avc_config.coded_width();
533  coded_height = avc_config.coded_height();
534  }
535 
536  if (pixel_width != avc_config.pixel_width() ||
537  pixel_height != avc_config.pixel_height()) {
538  LOG_IF(WARNING, pixel_width != 1 || pixel_height != 1)
539  << "Pixel aspect ratio in PASP box (" << pixel_width << ","
540  << pixel_height
541  << ") does not match with SAR in AVCDecoderConfigurationRecord "
542  "("
543  << avc_config.pixel_width() << "," << avc_config.pixel_height()
544  << "). Use AVCDecoderConfigurationRecord.";
545  pixel_width = avc_config.pixel_width();
546  pixel_height = avc_config.pixel_height();
547  }
548  break;
549  }
550  case FOURCC_hev1:
551  case FOURCC_hvc1: {
552  HEVCDecoderConfigurationRecord hevc_config;
553  if (!hevc_config.Parse(entry.codec_configuration.data)) {
554  LOG(ERROR) << "Failed to parse hevc.";
555  return false;
556  }
557  codec_string = hevc_config.GetCodecString(video_codec);
558  nalu_length_size = hevc_config.nalu_length_size();
559  break;
560  }
561  case FOURCC_vp08:
562  case FOURCC_vp09:
563  case FOURCC_vp10: {
564  VPCodecConfigurationRecord vp_config;
565  if (!vp_config.ParseMP4(entry.codec_configuration.data)) {
566  LOG(ERROR) << "Failed to parse vpcc.";
567  return false;
568  }
569  codec_string = vp_config.GetCodecString(video_codec);
570  break;
571  }
572  default:
573  LOG(ERROR) << "Unsupported video format "
574  << FourCCToString(actual_format) << " in stsd box.";
575  return false;
576  }
577 
578  const bool is_encrypted =
579  entry.sinf.info.track_encryption.default_is_protected == 1;
580  DVLOG(1) << "is_video_track_encrypted_: " << is_encrypted;
581  streams.push_back(new VideoStreamInfo(
582  track->header.track_id, timescale, duration, video_codec,
583  codec_string, track->media.header.language.code, coded_width,
584  coded_height, pixel_width, pixel_height,
585  0, // trick_play_rate
586  nalu_length_size, entry.codec_configuration.data.data(),
587  entry.codec_configuration.data.size(), is_encrypted));
588  }
589  }
590 
591  init_cb_.Run(streams);
592  if (!FetchKeysIfNecessary(moov_->pssh))
593  return false;
594  runs_.reset(new TrackRunIterator(moov_.get()));
595  RCHECK(runs_->Init());
596  ChangeState(kEmittingSamples);
597  return true;
598 }
599 
600 bool MP4MediaParser::ParseMoof(BoxReader* reader) {
601  // Must already have initialization segment.
602  RCHECK(moov_.get());
603  MovieFragment moof;
604  RCHECK(moof.Parse(reader));
605  if (!runs_)
606  runs_.reset(new TrackRunIterator(moov_.get()));
607  RCHECK(runs_->Init(moof));
608  if (!FetchKeysIfNecessary(moof.pssh))
609  return false;
610  ChangeState(kEmittingSamples);
611  return true;
612 }
613 
614 bool MP4MediaParser::FetchKeysIfNecessary(
615  const std::vector<ProtectionSystemSpecificHeader>& headers) {
616  if (headers.empty())
617  return true;
618 
619  // An error will be returned later if the samples need to be decrypted.
620  if (!decryption_key_source_)
621  return true;
622 
623  Status status;
624  for (std::vector<ProtectionSystemSpecificHeader>::const_iterator iter =
625  headers.begin(); iter != headers.end(); ++iter) {
626  status = decryption_key_source_->FetchKeys(iter->raw_box);
627  if (!status.ok()) {
628  // If there is an error, try using the next PSSH box and report if none
629  // work.
630  VLOG(1) << "Unable to fetch decryption keys: " << status
631  << ", trying the next PSSH box";
632  continue;
633  }
634  return true;
635  }
636 
637  if (!status.ok()) {
638  LOG(ERROR) << "Error fetching decryption keys: " << status;
639  return false;
640  }
641 
642  LOG(ERROR) << "No viable 'pssh' box found for content decryption.";
643  return false;
644 }
645 
646 bool MP4MediaParser::EnqueueSample(bool* err) {
647  if (!runs_->IsRunValid()) {
648  // Remain in kEnqueueingSamples state, discarding data, until the end of
649  // the current 'mdat' box has been appended to the queue.
650  if (!queue_.Trim(mdat_tail_))
651  return false;
652 
653  ChangeState(kParsingBoxes);
654  return true;
655  }
656 
657  if (!runs_->IsSampleValid()) {
658  runs_->AdvanceRun();
659  return true;
660  }
661 
662  DCHECK(!(*err));
663 
664  const uint8_t* buf;
665  int buf_size;
666  queue_.Peek(&buf, &buf_size);
667  if (!buf_size)
668  return false;
669 
670  // Skip this entire track if it is not audio nor video.
671  if (!runs_->is_audio() && !runs_->is_video())
672  runs_->AdvanceRun();
673 
674  // Attempt to cache the auxiliary information first. Aux info is usually
675  // placed in a contiguous block before the sample data, rather than being
676  // interleaved. If we didn't cache it, this would require that we retain the
677  // start of the segment buffer while reading samples. Aux info is typically
678  // quite small compared to sample data, so this pattern is useful on
679  // memory-constrained devices where the source buffer consumes a substantial
680  // portion of the total system memory.
681  if (runs_->AuxInfoNeedsToBeCached()) {
682  queue_.PeekAt(runs_->aux_info_offset() + moof_head_, &buf, &buf_size);
683  if (buf_size < runs_->aux_info_size())
684  return false;
685  *err = !runs_->CacheAuxInfo(buf, buf_size);
686  return !*err;
687  }
688 
689  int64_t sample_offset = runs_->sample_offset() + moof_head_;
690  queue_.PeekAt(sample_offset, &buf, &buf_size);
691  if (buf_size < runs_->sample_size()) {
692  if (sample_offset < queue_.head()) {
693  LOG(ERROR) << "Incorrect sample offset " << sample_offset
694  << " < " << queue_.head();
695  *err = true;
696  }
697  return false;
698  }
699 
700  scoped_refptr<MediaSample> stream_sample(MediaSample::CopyFrom(
701  buf, runs_->sample_size(), runs_->is_keyframe()));
702  if (runs_->is_encrypted()) {
703  if (!decryptor_source_) {
704  *err = true;
705  LOG(ERROR) << "Encrypted media sample encountered, but decryption is not "
706  "enabled";
707  return false;
708  }
709 
710  scoped_ptr<DecryptConfig> decrypt_config = runs_->GetDecryptConfig();
711  if (!decrypt_config ||
712  !decryptor_source_->DecryptSampleBuffer(decrypt_config.get(),
713  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
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:156
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
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 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