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