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