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