Shaka Packager SDK
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
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/file/file.h"
15 #include "packager/file/file_closer.h"
16 #include "packager/media/base/audio_stream_info.h"
17 #include "packager/media/base/buffer_reader.h"
18 #include "packager/media/base/decrypt_config.h"
19 #include "packager/media/base/key_source.h"
20 #include "packager/media/base/macros.h"
21 #include "packager/media/base/media_sample.h"
22 #include "packager/media/base/rcheck.h"
23 #include "packager/media/base/video_stream_info.h"
24 #include "packager/media/codecs/avc_decoder_configuration_record.h"
25 #include "packager/media/codecs/es_descriptor.h"
26 #include "packager/media/codecs/hevc_decoder_configuration_record.h"
27 #include "packager/media/codecs/vp_codec_configuration_record.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  if (!moov_) {
261  // For seekable files, we seek to the 'moov' and load the 'moov' first
262  // then seek back (see LoadMoov function for details); we do not support
263  // having 'mdat' before 'moov' for non-seekable files. The code ends up
264  // here only if it is a non-seekable file.
265  NOTIMPLEMENTED() << " Non-seekable Files with 'mdat' box before 'moov' "
266  "box is not supported.";
267  *err = true;
268  return false;
269  } else {
270  // This can happen if there are unused 'mdat' boxes, which is unusual
271  // but allowed by the spec. Ignore the 'mdat' and proceed.
272  LOG(INFO)
273  << "Ignore unused 'mdat' box - this could be as a result of extra "
274  "not usable 'mdat' or 'mdat' associated with unrecognized track.";
275  }
276  }
277 
278  // Set up mdat offset for ReadMDATsUntil().
279  mdat_tail_ = queue_.head() + reader->size();
280 
281  if (reader->type() == FOURCC_moov) {
282  *err = !ParseMoov(reader.get());
283  } else if (reader->type() == FOURCC_moof) {
284  moof_head_ = queue_.head();
285  *err = !ParseMoof(reader.get());
286 
287  // Return early to avoid evicting 'moof' data from queue. Auxiliary info may
288  // be located anywhere in the file, including inside the 'moof' itself.
289  // (Since 'default-base-is-moof' is mandated, no data references can come
290  // before the head of the 'moof', so keeping this box around is sufficient.)
291  return !(*err);
292  } else {
293  VLOG(2) << "Skipping top-level box: " << FourCCToString(reader->type());
294  }
295 
296  queue_.Pop(static_cast<int>(reader->size()));
297  return !(*err);
298 }
299 
300 bool MP4MediaParser::ParseMoov(BoxReader* reader) {
301  if (moov_)
302  return true; // Already parsed the 'moov' box.
303 
304  moov_.reset(new Movie);
305  RCHECK(moov_->Parse(reader));
306  runs_.reset();
307 
308  std::vector<std::shared_ptr<StreamInfo>> streams;
309 
310  for (std::vector<Track>::const_iterator track = moov_->tracks.begin();
311  track != moov_->tracks.end(); ++track) {
312  const uint32_t timescale = track->media.header.timescale;
313 
314  // Calculate duration (based on timescale).
315  uint64_t duration = 0;
316  if (track->media.header.duration > 0) {
317  duration = track->media.header.duration;
318  } else if (moov_->extends.header.fragment_duration > 0) {
319  DCHECK(moov_->header.timescale != 0);
320  duration = Rescale(moov_->extends.header.fragment_duration,
321  moov_->header.timescale,
322  timescale);
323  } else if (moov_->header.duration > 0 &&
324  moov_->header.duration != std::numeric_limits<uint64_t>::max()) {
325  DCHECK(moov_->header.timescale != 0);
326  duration =
327  Rescale(moov_->header.duration, moov_->header.timescale, timescale);
328  }
329 
330  const SampleDescription& samp_descr =
331  track->media.information.sample_table.description;
332 
333  size_t desc_idx = 0;
334 
335  // Read sample description index from mvex if it exists otherwise read
336  // from the first entry in Sample To Chunk box.
337  if (moov_->extends.tracks.size() > 0) {
338  for (size_t t = 0; t < moov_->extends.tracks.size(); t++) {
339  const TrackExtends& trex = moov_->extends.tracks[t];
340  if (trex.track_id == track->header.track_id) {
341  desc_idx = trex.default_sample_description_index;
342  break;
343  }
344  }
345  } else {
346  const std::vector<ChunkInfo>& chunk_info =
347  track->media.information.sample_table.sample_to_chunk.chunk_info;
348  RCHECK(chunk_info.size() > 0);
349  desc_idx = chunk_info[0].sample_description_index;
350  }
351  RCHECK(desc_idx > 0);
352  desc_idx -= 1; // BMFF descriptor index is one-based
353 
354  if (samp_descr.type == kAudio) {
355  RCHECK(!samp_descr.audio_entries.empty());
356 
357  // It is not uncommon to find otherwise-valid files with incorrect sample
358  // description indices, so we fail gracefully in that case.
359  if (desc_idx >= samp_descr.audio_entries.size())
360  desc_idx = 0;
361 
362  const AudioSampleEntry& entry = samp_descr.audio_entries[desc_idx];
363  const FourCC actual_format = entry.GetActualFormat();
364  Codec codec = FourCCToCodec(actual_format);
365  uint8_t num_channels = 0;
366  uint32_t sampling_frequency = 0;
367  uint64_t codec_delay_ns = 0;
368  uint8_t audio_object_type = 0;
369  uint32_t max_bitrate = 0;
370  uint32_t avg_bitrate = 0;
371  std::vector<uint8_t> codec_config;
372 
373  switch (actual_format) {
374  case FOURCC_mp4a:
375  // Check if it is MPEG4 AAC defined in ISO 14496 Part 3 or
376  // supported MPEG2 AAC variants.
377  if (entry.esds.es_descriptor.IsAAC()) {
378  codec = kCodecAAC;
379  const AACAudioSpecificConfig& aac_audio_specific_config =
380  entry.esds.aac_audio_specific_config;
381  num_channels = aac_audio_specific_config.GetNumChannels();
382  sampling_frequency =
383  aac_audio_specific_config.GetSamplesPerSecond();
384  audio_object_type = aac_audio_specific_config.GetAudioObjectType();
385  codec_config = entry.esds.es_descriptor.decoder_specific_info();
386  break;
387  } else if (entry.esds.es_descriptor.IsDTS()) {
388  ObjectType audio_type = entry.esds.es_descriptor.object_type();
389  switch (audio_type) {
390  case kDTSC:
391  codec = kCodecDTSC;
392  break;
393  case kDTSE:
394  codec = kCodecDTSE;
395  break;
396  case kDTSH:
397  codec = kCodecDTSH;
398  break;
399  case kDTSL:
400  codec = kCodecDTSL;
401  break;
402  default:
403  LOG(ERROR) << "Unsupported audio type " << audio_type
404  << " in stsd box.";
405  return false;
406  }
407  num_channels = entry.channelcount;
408  // For dts audio in esds, current supported number of channels is 6
409  // as the only supported channel layout is 5.1.
410  if (num_channels != kDtsAudioNumChannels) {
411  LOG(ERROR) << "Unsupported channel count " << num_channels
412  << " for audio type " << audio_type << ".";
413  return false;
414  }
415  sampling_frequency = entry.samplerate;
416  max_bitrate = entry.esds.es_descriptor.max_bitrate();
417  avg_bitrate = entry.esds.es_descriptor.avg_bitrate();
418  } else {
419  LOG(ERROR) << "Unsupported audio format 0x" << std::hex
420  << actual_format << " in stsd box.";
421  return false;
422  }
423  break;
424  case FOURCC_dtsc:
425  FALLTHROUGH_INTENDED;
426  case FOURCC_dtsh:
427  FALLTHROUGH_INTENDED;
428  case FOURCC_dtsl:
429  FALLTHROUGH_INTENDED;
430  case FOURCC_dtse:
431  FALLTHROUGH_INTENDED;
432  case FOURCC_dtsm:
433  codec_config = entry.ddts.extra_data;
434  max_bitrate = entry.ddts.max_bitrate;
435  avg_bitrate = entry.ddts.avg_bitrate;
436  num_channels = entry.channelcount;
437  sampling_frequency = entry.samplerate;
438  break;
439  case FOURCC_ac_3:
440  codec_config = entry.dac3.data;
441  num_channels = entry.channelcount;
442  sampling_frequency = entry.samplerate;
443  break;
444  case FOURCC_ec_3:
445  codec_config = entry.dec3.data;
446  num_channels = entry.channelcount;
447  sampling_frequency = entry.samplerate;
448  break;
449  case FOURCC_Opus:
450  codec_config = entry.dops.opus_identification_header;
451  num_channels = entry.channelcount;
452  sampling_frequency = entry.samplerate;
453  RCHECK(sampling_frequency != 0);
454  codec_delay_ns =
455  entry.dops.preskip * kNanosecondsPerSecond / sampling_frequency;
456  break;
457  default:
458  LOG(ERROR) << "Unsupported audio format 0x" << std::hex
459  << actual_format << " in stsd box.";
460  return false;
461  }
462 
463  // Extract possible seek preroll.
464  uint64_t seek_preroll_ns = 0;
465  for (const auto& sample_group_description :
466  track->media.information.sample_table.sample_group_descriptions) {
467  if (sample_group_description.grouping_type != FOURCC_roll)
468  continue;
469  const auto& audio_roll_recovery_entries =
470  sample_group_description.audio_roll_recovery_entries;
471  if (audio_roll_recovery_entries.size() != 1) {
472  LOG(WARNING) << "Unexpected number of entries in "
473  "SampleGroupDescription table with grouping type "
474  "'roll'.";
475  break;
476  }
477  const int16_t roll_distance_in_samples =
478  audio_roll_recovery_entries[0].roll_distance;
479  if (roll_distance_in_samples < 0) {
480  RCHECK(sampling_frequency != 0);
481  seek_preroll_ns = kNanosecondsPerSecond *
482  (-roll_distance_in_samples) / sampling_frequency;
483  } else {
484  LOG(WARNING)
485  << "Roll distance is supposed to be negative, but seeing "
486  << roll_distance_in_samples;
487  }
488  break;
489  }
490 
491  // The stream will be decrypted if a |decryptor_source_| is available.
492  const bool is_encrypted =
493  decryptor_source_
494  ? false
495  : entry.sinf.info.track_encryption.default_is_protected == 1;
496  DVLOG(1) << "is_audio_track_encrypted_: " << is_encrypted;
497  streams.emplace_back(new AudioStreamInfo(
498  track->header.track_id, timescale, duration, codec,
499  AudioStreamInfo::GetCodecString(codec, audio_object_type),
500  codec_config.data(), codec_config.size(), entry.samplesize,
501  num_channels, sampling_frequency, seek_preroll_ns, codec_delay_ns,
502  max_bitrate, avg_bitrate, track->media.header.language.code,
503  is_encrypted));
504  }
505 
506  if (samp_descr.type == kVideo) {
507  RCHECK(!samp_descr.video_entries.empty());
508  if (desc_idx >= samp_descr.video_entries.size())
509  desc_idx = 0;
510  const VideoSampleEntry& entry = samp_descr.video_entries[desc_idx];
511 
512  uint32_t coded_width = entry.width;
513  uint32_t coded_height = entry.height;
514  uint32_t pixel_width = entry.pixel_aspect.h_spacing;
515  uint32_t pixel_height = entry.pixel_aspect.v_spacing;
516  if (pixel_width == 0 && pixel_height == 0) {
517  pixel_width = 1;
518  pixel_height = 1;
519  }
520  std::string codec_string;
521  uint8_t nalu_length_size = 0;
522 
523  const FourCC actual_format = entry.GetActualFormat();
524  const Codec video_codec = FourCCToCodec(actual_format);
525  switch (actual_format) {
526  case FOURCC_avc1:
527  case FOURCC_avc3: {
528  AVCDecoderConfigurationRecord avc_config;
529  if (!avc_config.Parse(entry.codec_configuration.data)) {
530  LOG(ERROR) << "Failed to parse avcc.";
531  return false;
532  }
533  codec_string = avc_config.GetCodecString(actual_format);
534  nalu_length_size = avc_config.nalu_length_size();
535 
536  if (coded_width != avc_config.coded_width() ||
537  coded_height != avc_config.coded_height()) {
538  LOG(WARNING) << "Resolution in VisualSampleEntry (" << coded_width
539  << "," << coded_height
540  << ") does not match with resolution in "
541  "AVCDecoderConfigurationRecord ("
542  << avc_config.coded_width() << ","
543  << avc_config.coded_height()
544  << "). Use AVCDecoderConfigurationRecord.";
545  coded_width = avc_config.coded_width();
546  coded_height = avc_config.coded_height();
547  }
548 
549  if (pixel_width != avc_config.pixel_width() ||
550  pixel_height != avc_config.pixel_height()) {
551  LOG_IF(WARNING, pixel_width != 1 || pixel_height != 1)
552  << "Pixel aspect ratio in PASP box (" << pixel_width << ","
553  << pixel_height
554  << ") does not match with SAR in AVCDecoderConfigurationRecord "
555  "("
556  << avc_config.pixel_width() << "," << avc_config.pixel_height()
557  << "). Use AVCDecoderConfigurationRecord.";
558  pixel_width = avc_config.pixel_width();
559  pixel_height = avc_config.pixel_height();
560  }
561  break;
562  }
563  case FOURCC_hev1:
564  case FOURCC_hvc1: {
565  HEVCDecoderConfigurationRecord hevc_config;
566  if (!hevc_config.Parse(entry.codec_configuration.data)) {
567  LOG(ERROR) << "Failed to parse hevc.";
568  return false;
569  }
570  codec_string = hevc_config.GetCodecString(actual_format);
571  nalu_length_size = hevc_config.nalu_length_size();
572  break;
573  }
574  case FOURCC_vp08:
575  case FOURCC_vp09:
576  case FOURCC_vp10: {
577  VPCodecConfigurationRecord vp_config;
578  if (!vp_config.ParseMP4(entry.codec_configuration.data)) {
579  LOG(ERROR) << "Failed to parse vpcc.";
580  return false;
581  }
582  codec_string = vp_config.GetCodecString(video_codec);
583  break;
584  }
585  default:
586  LOG(ERROR) << "Unsupported video format "
587  << FourCCToString(actual_format) << " in stsd box.";
588  return false;
589  }
590 
591  // The stream will be decrypted if a |decryptor_source_| is available.
592  const bool is_encrypted =
593  decryptor_source_
594  ? false
595  : entry.sinf.info.track_encryption.default_is_protected == 1;
596  DVLOG(1) << "is_video_track_encrypted_: " << is_encrypted;
597  std::shared_ptr<VideoStreamInfo> video_stream_info(new VideoStreamInfo(
598  track->header.track_id, timescale, duration, video_codec,
599  GetH26xStreamFormat(actual_format), codec_string,
600  entry.codec_configuration.data.data(),
601  entry.codec_configuration.data.size(), coded_width, coded_height,
602  pixel_width, pixel_height,
603  0, // trick_play_factor
604  nalu_length_size, track->media.header.language.code, is_encrypted));
605 
606  // Set pssh raw data if it has.
607  if (moov_->pssh.size() > 0) {
608  std::vector<uint8_t> pssh_raw_data;
609  for (const auto& pssh : moov_->pssh) {
610  pssh_raw_data.insert(pssh_raw_data.end(), pssh.raw_box.begin(),
611  pssh.raw_box.end());
612  }
613  video_stream_info->set_eme_init_data(pssh_raw_data.data(),
614  pssh_raw_data.size());
615  }
616 
617  streams.push_back(video_stream_info);
618  }
619  }
620 
621  init_cb_.Run(streams);
622  if (!FetchKeysIfNecessary(moov_->pssh))
623  return false;
624  runs_.reset(new TrackRunIterator(moov_.get()));
625  RCHECK(runs_->Init());
626  ChangeState(kEmittingSamples);
627  return true;
628 }
629 
630 bool MP4MediaParser::ParseMoof(BoxReader* reader) {
631  // Must already have initialization segment.
632  RCHECK(moov_.get());
633  MovieFragment moof;
634  RCHECK(moof.Parse(reader));
635  if (!runs_)
636  runs_.reset(new TrackRunIterator(moov_.get()));
637  RCHECK(runs_->Init(moof));
638  if (!FetchKeysIfNecessary(moof.pssh))
639  return false;
640  ChangeState(kEmittingSamples);
641  return true;
642 }
643 
644 bool MP4MediaParser::FetchKeysIfNecessary(
645  const std::vector<ProtectionSystemSpecificHeader>& headers) {
646  if (headers.empty())
647  return true;
648 
649  // An error will be returned later if the samples need to be decrypted.
650  if (!decryption_key_source_)
651  return true;
652 
653  std::vector<uint8_t> pssh_raw_data;
654  for (const auto& header : headers) {
655  pssh_raw_data.insert(pssh_raw_data.end(), header.raw_box.begin(),
656  header.raw_box.end());
657  }
658  Status status =
659  decryption_key_source_->FetchKeys(EmeInitDataType::CENC, pssh_raw_data);
660  if (!status.ok()) {
661  LOG(ERROR) << "Error fetching decryption keys: " << status;
662  return false;
663  }
664  return true;
665 }
666 
667 bool MP4MediaParser::EnqueueSample(bool* err) {
668  if (!runs_->IsRunValid()) {
669  // Remain in kEnqueueingSamples state, discarding data, until the end of
670  // the current 'mdat' box has been appended to the queue.
671  if (!queue_.Trim(mdat_tail_))
672  return false;
673 
674  ChangeState(kParsingBoxes);
675  return true;
676  }
677 
678  if (!runs_->IsSampleValid()) {
679  runs_->AdvanceRun();
680  return true;
681  }
682 
683  DCHECK(!(*err));
684 
685  const uint8_t* buf;
686  int buf_size;
687  queue_.Peek(&buf, &buf_size);
688  if (!buf_size)
689  return false;
690 
691  // Skip this entire track if it is not audio nor video.
692  if (!runs_->is_audio() && !runs_->is_video())
693  runs_->AdvanceRun();
694 
695  // Attempt to cache the auxiliary information first. Aux info is usually
696  // placed in a contiguous block before the sample data, rather than being
697  // interleaved. If we didn't cache it, this would require that we retain the
698  // start of the segment buffer while reading samples. Aux info is typically
699  // quite small compared to sample data, so this pattern is useful on
700  // memory-constrained devices where the source buffer consumes a substantial
701  // portion of the total system memory.
702  if (runs_->AuxInfoNeedsToBeCached()) {
703  queue_.PeekAt(runs_->aux_info_offset() + moof_head_, &buf, &buf_size);
704  if (buf_size < runs_->aux_info_size())
705  return false;
706  *err = !runs_->CacheAuxInfo(buf, buf_size);
707  return !*err;
708  }
709 
710  int64_t sample_offset = runs_->sample_offset() + moof_head_;
711  queue_.PeekAt(sample_offset, &buf, &buf_size);
712  if (buf_size < runs_->sample_size()) {
713  if (sample_offset < queue_.head()) {
714  LOG(ERROR) << "Incorrect sample offset " << sample_offset
715  << " < " << queue_.head();
716  *err = true;
717  }
718  return false;
719  }
720 
721  const uint8_t* media_data = buf;
722  const size_t media_data_size = runs_->sample_size();
723  // Use a dummy data size of 0 to avoid copying overhead.
724  // Actual media data is set later.
725  const size_t kDummyDataSize = 0;
726  std::shared_ptr<MediaSample> stream_sample(
727  MediaSample::CopyFrom(media_data, kDummyDataSize, runs_->is_keyframe()));
728 
729  if (runs_->is_encrypted()) {
730  std::shared_ptr<uint8_t> decrypted_media_data(
731  new uint8_t[media_data_size], std::default_delete<uint8_t[]>());
732  std::unique_ptr<DecryptConfig> decrypt_config = runs_->GetDecryptConfig();
733  if (!decrypt_config) {
734  *err = true;
735  LOG(ERROR) << "Missing decrypt config.";
736  return false;
737  }
738 
739  if (!decryptor_source_) {
740  stream_sample->SetData(media_data, media_data_size);
741  // If the demuxer does not have the decryptor_source_, store
742  // decrypt_config so that the demuxed sample can be decrypted later.
743  stream_sample->set_decrypt_config(std::move(decrypt_config));
744  stream_sample->set_is_encrypted(true);
745  } else {
746  if (!decryptor_source_->DecryptSampleBuffer(decrypt_config.get(),
747  media_data, media_data_size,
748  decrypted_media_data.get())) {
749  *err = true;
750  LOG(ERROR) << "Cannot decrypt samples.";
751  return false;
752  }
753  stream_sample->TransferData(std::move(decrypted_media_data),
754  media_data_size);
755  }
756  } else {
757  stream_sample->SetData(media_data, media_data_size);
758  }
759 
760  stream_sample->set_dts(runs_->dts());
761  stream_sample->set_pts(runs_->cts());
762  stream_sample->set_duration(runs_->duration());
763 
764  DVLOG(3) << "Pushing frame: "
765  << ", key=" << runs_->is_keyframe()
766  << ", dur=" << runs_->duration()
767  << ", dts=" << runs_->dts()
768  << ", cts=" << runs_->cts()
769  << ", size=" << runs_->sample_size();
770 
771  if (!new_sample_cb_.Run(runs_->track_id(), stream_sample)) {
772  *err = true;
773  LOG(ERROR) << "Failed to process the sample.";
774  return false;
775  }
776 
777  runs_->AdvanceSample();
778  return true;
779 }
780 
781 bool MP4MediaParser::ReadAndDiscardMDATsUntil(const int64_t offset) {
782  bool err = false;
783  while (mdat_tail_ < offset) {
784  const uint8_t* buf;
785  int size;
786  queue_.PeekAt(mdat_tail_, &buf, &size);
787 
788  FourCC type;
789  uint64_t box_sz;
790  if (!BoxReader::StartBox(buf, size, &type, &box_sz, &err))
791  break;
792 
793  mdat_tail_ += box_sz;
794  }
795  queue_.Trim(std::min(mdat_tail_, offset));
796  return !err;
797 }
798 
799 void MP4MediaParser::ChangeState(State new_state) {
800  DVLOG(2) << "Changing state: " << new_state;
801  state_ = new_state;
802 }
803 
804 } // namespace mp4
805 } // namespace media
806 } // namespace shaka
void PeekAt(int64_t offset, const uint8_t **buf, int *size)
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(EmeInitDataType init_data_type, const std::vector< uint8_t > &init_data)=0
static File * OpenWithNoBuffering(const char *file_name, const char *mode)
Definition: file.cc:187
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:42
KeySource is responsible for encryption key acquisition.
Definition: key_source.h: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:54
static BoxReader * ReadBox(const uint8_t *buf, const size_t buf_size, bool *err)
Definition: box_reader.cc:36