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/protection_system_specific_info.h"
21 #include "packager/media/base/video_stream_info.h"
22 #include "packager/media/file/file.h"
23 #include "packager/media/file/file_closer.h"
24 #include "packager/media/filters/avc_decoder_configuration.h"
25 #include "packager/media/filters/hevc_decoder_configuration.h"
26 #include "packager/media/filters/vp_codec_configuration.h"
27 #include "packager/media/formats/mp4/box_definitions.h"
28 #include "packager/media/formats/mp4/box_reader.h"
29 #include "packager/media/formats/mp4/es_descriptor.h"
30 #include "packager/media/formats/mp4/rcheck.h"
31 #include "packager/media/formats/mp4/track_run_iterator.h"
32 
33 namespace edash_packager {
34 namespace media {
35 namespace mp4 {
36 namespace {
37 
38 uint64_t Rescale(uint64_t time_in_old_scale,
39  uint32_t old_scale,
40  uint32_t new_scale) {
41  return (static_cast<double>(time_in_old_scale) / old_scale) * new_scale;
42 }
43 
44 VideoCodec FourCCToVideoCodec(FourCC fourcc) {
45  switch (fourcc) {
46  case FOURCC_AVC1:
47  return kCodecH264;
48  case FOURCC_HEV1:
49  return kCodecHEV1;
50  case FOURCC_HVC1:
51  return kCodecHVC1;
52  case FOURCC_VP08:
53  return kCodecVP8;
54  case FOURCC_VP09:
55  return kCodecVP9;
56  case FOURCC_VP10:
57  return kCodecVP10;
58  default:
59  return kUnknownVideoCodec;
60  }
61 }
62 
63 AudioCodec FourCCToAudioCodec(FourCC fourcc) {
64  switch(fourcc) {
65  case FOURCC_DTSC:
66  return kCodecDTSC;
67  case FOURCC_DTSH:
68  return kCodecDTSH;
69  case FOURCC_DTSL:
70  return kCodecDTSL;
71  case FOURCC_DTSE:
72  return kCodecDTSE;
73  case FOURCC_DTSP:
74  return kCodecDTSP;
75  case FOURCC_DTSM:
76  return kCodecDTSM;
77  case FOURCC_AC3:
78  return kCodecAC3;
79  case FOURCC_EAC3:
80  return kCodecEAC3;
81  default:
82  return kUnknownAudioCodec;
83  }
84 }
85 
86 const char kWidevineKeySystemId[] = "edef8ba979d64acea3c827dcd51d21ed";
87 // Default DTS audio number of channels for 5.1 channel layout.
88 const uint8_t kDtsAudioNumChannels = 6;
89 
90 } // namespace
91 
92 MP4MediaParser::MP4MediaParser()
93  : state_(kWaitingForInit),
94  decryption_key_source_(NULL),
95  moof_head_(0),
96  mdat_tail_(0) {}
97 
98 MP4MediaParser::~MP4MediaParser() {}
99 
100 void MP4MediaParser::Init(const InitCB& init_cb,
101  const NewSampleCB& new_sample_cb,
102  KeySource* decryption_key_source) {
103  DCHECK_EQ(state_, kWaitingForInit);
104  DCHECK(init_cb_.is_null());
105  DCHECK(!init_cb.is_null());
106  DCHECK(!new_sample_cb.is_null());
107 
108  ChangeState(kParsingBoxes);
109  init_cb_ = init_cb;
110  new_sample_cb_ = new_sample_cb;
111  decryption_key_source_ = decryption_key_source;
112  if (decryption_key_source)
113  decryptor_source_.reset(new DecryptorSource(decryption_key_source));
114 }
115 
116 void MP4MediaParser::Reset() {
117  queue_.Reset();
118  runs_.reset();
119  moof_head_ = 0;
120  mdat_tail_ = 0;
121 }
122 
124  DCHECK_NE(state_, kWaitingForInit);
125  Reset();
126  ChangeState(kParsingBoxes);
127  return true;
128 }
129 
130 bool MP4MediaParser::Parse(const uint8_t* buf, int size) {
131  DCHECK_NE(state_, kWaitingForInit);
132 
133  if (state_ == kError)
134  return false;
135 
136  queue_.Push(buf, size);
137 
138  bool result, err = false;
139 
140  do {
141  if (state_ == kParsingBoxes) {
142  result = ParseBox(&err);
143  } else {
144  DCHECK_EQ(kEmittingSamples, state_);
145  result = EnqueueSample(&err);
146  if (result) {
147  int64_t max_clear = runs_->GetMaxClearOffset() + moof_head_;
148  err = !ReadAndDiscardMDATsUntil(max_clear);
149  }
150  }
151  } while (result && !err);
152 
153  if (err) {
154  DLOG(ERROR) << "Error while parsing MP4";
155  moov_.reset();
156  Reset();
157  ChangeState(kError);
158  return false;
159  }
160 
161  return true;
162 }
163 
164 bool MP4MediaParser::LoadMoov(const std::string& file_path) {
165  scoped_ptr<File, FileCloser> file(
166  File::OpenWithNoBuffering(file_path.c_str(), "r"));
167  if (!file) {
168  LOG(ERROR) << "Unable to open media file '" << file_path << "'";
169  return false;
170  }
171  if (!file->Seek(0)) {
172  LOG(WARNING) << "Filesystem does not support seeking on file '" << file_path
173  << "'";
174  return false;
175  }
176 
177  uint64_t file_position(0);
178  bool mdat_seen(false);
179  while (true) {
180  const uint32_t kBoxHeaderReadSize(16);
181  std::vector<uint8_t> buffer(kBoxHeaderReadSize);
182  int64_t bytes_read = file->Read(&buffer[0], kBoxHeaderReadSize);
183  if (bytes_read == 0) {
184  LOG(ERROR) << "Could not find 'moov' box in file '" << file_path << "'";
185  return false;
186  }
187  if (bytes_read < kBoxHeaderReadSize) {
188  LOG(ERROR) << "Error reading media file '" << file_path << "'";
189  return false;
190  }
191  uint64_t box_size;
192  FourCC box_type;
193  bool err;
194  if (!BoxReader::StartTopLevelBox(&buffer[0], kBoxHeaderReadSize, &box_type,
195  &box_size, &err)) {
196  LOG(ERROR) << "Could not start top level box from file '" << file_path
197  << "'";
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::ReadTopLevelBox(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  uint8_t audio_object_type = 0;
350  uint32_t max_bitrate = 0;
351  uint32_t avg_bitrate = 0;
352  std::vector<uint8_t> extra_data;
353 
354  switch (actual_format) {
355  case FOURCC_MP4A:
356  // Check if it is MPEG4 AAC defined in ISO 14496 Part 3 or
357  // supported MPEG2 AAC variants.
358  if (entry.esds.es_descriptor.IsAAC()) {
359  codec = kCodecAAC;
360  const AACAudioSpecificConfig& aac_audio_specific_config =
361  entry.esds.aac_audio_specific_config;
362  num_channels = aac_audio_specific_config.num_channels();
363  sampling_frequency = aac_audio_specific_config.frequency();
364  audio_object_type = aac_audio_specific_config.audio_object_type();
365  extra_data = entry.esds.es_descriptor.decoder_specific_info();
366  break;
367  } else if (entry.esds.es_descriptor.IsDTS()) {
368  ObjectType audio_type = entry.esds.es_descriptor.object_type();
369  switch (audio_type) {
370  case kDTSC:
371  codec = kCodecDTSC;
372  break;
373  case kDTSE:
374  codec = kCodecDTSE;
375  break;
376  case kDTSH:
377  codec = kCodecDTSH;
378  break;
379  case kDTSL:
380  codec = kCodecDTSL;
381  break;
382  default:
383  LOG(ERROR) << "Unsupported audio type " << audio_type
384  << " in stsd box.";
385  return false;
386  }
387  num_channels = entry.esds.aac_audio_specific_config.num_channels();
388  // For dts audio in esds, current supported number of channels is 6
389  // as the only supported channel layout is 5.1.
390  if (num_channels != kDtsAudioNumChannels) {
391  LOG(ERROR) << "Unsupported channel count " << num_channels
392  << " for audio type " << audio_type << ".";
393  return false;
394  }
395  sampling_frequency = entry.samplerate;
396  max_bitrate = entry.esds.es_descriptor.max_bitrate();
397  avg_bitrate = entry.esds.es_descriptor.avg_bitrate();
398  } else {
399  LOG(ERROR) << "Unsupported audio format 0x" << std::hex
400  << actual_format << " in stsd box.";
401  return false;
402  }
403  break;
404  case FOURCC_DTSC:
405  FALLTHROUGH_INTENDED;
406  case FOURCC_DTSH:
407  FALLTHROUGH_INTENDED;
408  case FOURCC_DTSL:
409  FALLTHROUGH_INTENDED;
410  case FOURCC_DTSE:
411  FALLTHROUGH_INTENDED;
412  case FOURCC_DTSM:
413  extra_data = entry.ddts.extra_data;
414  max_bitrate = entry.ddts.max_bitrate;
415  avg_bitrate = entry.ddts.avg_bitrate;
416  num_channels = entry.channelcount;
417  sampling_frequency = entry.samplerate;
418  break;
419  case FOURCC_AC3:
420  extra_data = entry.dac3.data;
421  num_channels = entry.channelcount;
422  sampling_frequency = entry.samplerate;
423  break;
424  case FOURCC_EAC3:
425  extra_data = entry.dec3.data;
426  num_channels = entry.channelcount;
427  sampling_frequency = entry.samplerate;
428  break;
429  default:
430  LOG(ERROR) << "Unsupported audio format 0x" << std::hex
431  << actual_format << " in stsd box.";
432  return false;
433  }
434 
435  bool is_encrypted = entry.sinf.info.track_encryption.is_encrypted;
436  DVLOG(1) << "is_audio_track_encrypted_: " << is_encrypted;
437  streams.push_back(new AudioStreamInfo(
438  track->header.track_id,
439  timescale,
440  duration,
441  codec,
442  AudioStreamInfo::GetCodecString(codec, audio_object_type),
443  track->media.header.language.code,
444  entry.samplesize,
445  num_channels,
446  sampling_frequency,
447  max_bitrate,
448  avg_bitrate,
449  extra_data.data(),
450  extra_data.size(),
451  is_encrypted));
452  }
453 
454  if (samp_descr.type == kVideo) {
455  RCHECK(!samp_descr.video_entries.empty());
456  if (desc_idx >= samp_descr.video_entries.size())
457  desc_idx = 0;
458  const VideoSampleEntry& entry = samp_descr.video_entries[desc_idx];
459 
460  uint32_t coded_width = entry.width;
461  uint32_t coded_height = entry.height;
462  uint32_t pixel_width = entry.pixel_aspect.h_spacing;
463  uint32_t pixel_height = entry.pixel_aspect.v_spacing;
464  if (pixel_width == 0 && pixel_height == 0) {
465  pixel_width = 1;
466  pixel_height = 1;
467  }
468  std::string codec_string;
469  uint8_t nalu_length_size = 0;
470 
471  const FourCC actual_format = entry.GetActualFormat();
472  const VideoCodec video_codec = FourCCToVideoCodec(actual_format);
473  switch (actual_format) {
474  case FOURCC_AVC1: {
475  AVCDecoderConfiguration avc_config;
476  if (!avc_config.Parse(entry.codec_config_record.data)) {
477  LOG(ERROR) << "Failed to parse avcc.";
478  return false;
479  }
480  codec_string = avc_config.GetCodecString();
481  nalu_length_size = avc_config.nalu_length_size();
482 
483  if (coded_width != avc_config.coded_width() ||
484  coded_height != avc_config.coded_height()) {
485  LOG(WARNING) << "Resolution in VisualSampleEntry (" << coded_width
486  << "," << coded_height
487  << ") does not match with resolution in "
488  "AVCDecoderConfigurationRecord ("
489  << avc_config.coded_width() << ","
490  << avc_config.coded_height()
491  << "). Use AVCDecoderConfigurationRecord.";
492  coded_width = avc_config.coded_width();
493  coded_height = avc_config.coded_height();
494  }
495 
496  if (pixel_width != avc_config.pixel_width() ||
497  pixel_height != avc_config.pixel_height()) {
498  LOG_IF(WARNING, pixel_width != 1 || pixel_height != 1)
499  << "Pixel aspect ratio in PASP box (" << pixel_width << ","
500  << pixel_height
501  << ") does not match with SAR in AVCDecoderConfigurationRecord "
502  "("
503  << avc_config.pixel_width() << "," << avc_config.pixel_height()
504  << "). Use AVCDecoderConfigurationRecord.";
505  pixel_width = avc_config.pixel_width();
506  pixel_height = avc_config.pixel_height();
507  }
508  break;
509  }
510  case FOURCC_HEV1:
511  case FOURCC_HVC1: {
512  HEVCDecoderConfiguration hevc_config;
513  if (!hevc_config.Parse(entry.codec_config_record.data)) {
514  LOG(ERROR) << "Failed to parse hevc.";
515  return false;
516  }
517  codec_string = hevc_config.GetCodecString(video_codec);
518  nalu_length_size = hevc_config.nalu_length_size();
519  break;
520  }
521  case FOURCC_VP08:
522  case FOURCC_VP09:
523  case FOURCC_VP10: {
524  VPCodecConfiguration vp_config;
525  if (!vp_config.Parse(entry.codec_config_record.data)) {
526  LOG(ERROR) << "Failed to parse vpcc.";
527  return false;
528  }
529  codec_string = vp_config.GetCodecString(video_codec);
530  break;
531  }
532  default:
533  LOG(ERROR) << "Unsupported video format "
534  << FourCCToString(actual_format) << " in stsd box.";
535  return false;
536  }
537 
538  bool is_encrypted = entry.sinf.info.track_encryption.is_encrypted;
539  DVLOG(1) << "is_video_track_encrypted_: " << is_encrypted;
540  streams.push_back(new VideoStreamInfo(
541  track->header.track_id, timescale, duration, video_codec,
542  codec_string, track->media.header.language.code, coded_width,
543  coded_height, pixel_width, pixel_height,
544  0, // trick_play_rate
545  nalu_length_size, entry.codec_config_record.data.data(),
546  entry.codec_config_record.data.size(), is_encrypted));
547  }
548  }
549 
550  init_cb_.Run(streams);
551  if (!FetchKeysIfNecessary(moov_->pssh))
552  return false;
553  runs_.reset(new TrackRunIterator(moov_.get()));
554  RCHECK(runs_->Init());
555  ChangeState(kEmittingSamples);
556  return true;
557 }
558 
559 bool MP4MediaParser::ParseMoof(BoxReader* reader) {
560  // Must already have initialization segment.
561  RCHECK(moov_.get());
562  MovieFragment moof;
563  RCHECK(moof.Parse(reader));
564  if (!runs_)
565  runs_.reset(new TrackRunIterator(moov_.get()));
566  RCHECK(runs_->Init(moof));
567  if (!FetchKeysIfNecessary(moof.pssh))
568  return false;
569  ChangeState(kEmittingSamples);
570  return true;
571 }
572 
573 bool MP4MediaParser::FetchKeysIfNecessary(
574  const std::vector<ProtectionSystemSpecificHeader>& headers) {
575  if (headers.empty())
576  return true;
577 
578  // An error will be returned later if the samples need to be decrypted.
579  if (!decryption_key_source_)
580  return true;
581 
582  // TODO(tinskip): Pass in raw 'pssh' boxes to FetchKeys. This will allow
583  // supporting multiple keysystems. Move this to KeySource.
584  std::vector<uint8_t> widevine_system_id;
585  base::HexStringToBytes(kWidevineKeySystemId, &widevine_system_id);
586  for (std::vector<ProtectionSystemSpecificHeader>::const_iterator iter =
587  headers.begin(); iter != headers.end(); ++iter) {
588  ProtectionSystemSpecificInfo info;
589  RCHECK(info.Parse(iter->raw_box.data(), iter->raw_box.size()));
590  if (info.system_id() == widevine_system_id) {
591  Status status = decryption_key_source_->FetchKeys(info.pssh_data());
592  if (!status.ok()) {
593  LOG(ERROR) << "Error fetching decryption keys: " << status;
594  return false;
595  }
596  return true;
597  }
598  }
599 
600  LOG(ERROR) << "No viable 'pssh' box found for content decryption.";
601  return false;
602 }
603 
604 bool MP4MediaParser::EnqueueSample(bool* err) {
605  if (!runs_->IsRunValid()) {
606  // Remain in kEnqueueingSamples state, discarding data, until the end of
607  // the current 'mdat' box has been appended to the queue.
608  if (!queue_.Trim(mdat_tail_))
609  return false;
610 
611  ChangeState(kParsingBoxes);
612  return true;
613  }
614 
615  if (!runs_->IsSampleValid()) {
616  runs_->AdvanceRun();
617  return true;
618  }
619 
620  DCHECK(!(*err));
621 
622  const uint8_t* buf;
623  int buf_size;
624  queue_.Peek(&buf, &buf_size);
625  if (!buf_size)
626  return false;
627 
628  // Skip this entire track if it is not audio nor video.
629  if (!runs_->is_audio() && !runs_->is_video())
630  runs_->AdvanceRun();
631 
632  // Attempt to cache the auxiliary information first. Aux info is usually
633  // placed in a contiguous block before the sample data, rather than being
634  // interleaved. If we didn't cache it, this would require that we retain the
635  // start of the segment buffer while reading samples. Aux info is typically
636  // quite small compared to sample data, so this pattern is useful on
637  // memory-constrained devices where the source buffer consumes a substantial
638  // portion of the total system memory.
639  if (runs_->AuxInfoNeedsToBeCached()) {
640  queue_.PeekAt(runs_->aux_info_offset() + moof_head_, &buf, &buf_size);
641  if (buf_size < runs_->aux_info_size())
642  return false;
643  *err = !runs_->CacheAuxInfo(buf, buf_size);
644  return !*err;
645  }
646 
647  int64_t sample_offset = runs_->sample_offset() + moof_head_;
648  queue_.PeekAt(sample_offset, &buf, &buf_size);
649  if (buf_size < runs_->sample_size()) {
650  if (sample_offset < queue_.head()) {
651  LOG(ERROR) << "Incorrect sample offset " << sample_offset
652  << " < " << queue_.head();
653  *err = true;
654  }
655  return false;
656  }
657 
658  scoped_refptr<MediaSample> stream_sample(MediaSample::CopyFrom(
659  buf, runs_->sample_size(), runs_->is_keyframe()));
660  if (runs_->is_encrypted()) {
661  if (!decryptor_source_) {
662  *err = true;
663  LOG(ERROR) << "Encrypted media sample encountered, but decryption is not "
664  "enabled";
665  return false;
666  }
667 
668  scoped_ptr<DecryptConfig> decrypt_config = runs_->GetDecryptConfig();
669  if (!decrypt_config ||
670  !decryptor_source_->DecryptSampleBuffer(decrypt_config.get(),
671  stream_sample->writable_data(),
672  stream_sample->data_size())) {
673  *err = true;
674  LOG(ERROR) << "Cannot decrypt samples.";
675  return false;
676  }
677  }
678 
679  stream_sample->set_dts(runs_->dts());
680  stream_sample->set_pts(runs_->cts());
681  stream_sample->set_duration(runs_->duration());
682 
683  DVLOG(3) << "Pushing frame: "
684  << ", key=" << runs_->is_keyframe()
685  << ", dur=" << runs_->duration()
686  << ", dts=" << runs_->dts()
687  << ", cts=" << runs_->cts()
688  << ", size=" << runs_->sample_size();
689 
690  if (!new_sample_cb_.Run(runs_->track_id(), stream_sample)) {
691  *err = true;
692  LOG(ERROR) << "Failed to process the sample.";
693  return false;
694  }
695 
696  runs_->AdvanceSample();
697  return true;
698 }
699 
700 bool MP4MediaParser::ReadAndDiscardMDATsUntil(const int64_t offset) {
701  bool err = false;
702  while (mdat_tail_ < offset) {
703  const uint8_t* buf;
704  int size;
705  queue_.PeekAt(mdat_tail_, &buf, &size);
706 
707  FourCC type;
708  uint64_t box_sz;
709  if (!BoxReader::StartTopLevelBox(buf, size, &type, &box_sz, &err))
710  break;
711 
712  mdat_tail_ += box_sz;
713  }
714  queue_.Trim(std::min(mdat_tail_, offset));
715  return !err;
716 }
717 
718 void MP4MediaParser::ChangeState(State new_state) {
719  DVLOG(2) << "Changing state: " << new_state;
720  state_ = new_state;
721 }
722 
723 } // namespace mp4
724 } // namespace media
725 } // 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:30
KeySource is responsible for encryption key acquisition.
Definition: key_source.h:29
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)