diff --git a/packager/media/formats/mp4/mp4_media_parser.cc b/packager/media/formats/mp4/mp4_media_parser.cc index ad09ab5461..614436b584 100644 --- a/packager/media/formats/mp4/mp4_media_parser.cc +++ b/packager/media/formats/mp4/mp4_media_parser.cc @@ -257,12 +257,22 @@ bool MP4MediaParser::ParseBox(bool* err) { return false; if (reader->type() == FOURCC_mdat) { - // The code ends up here only if a MOOV box is not yet seen. - DCHECK(!moov_); - - NOTIMPLEMENTED() << " Files with MDAT before MOOV is not supported yet."; - *err = true; - return false; + if (!moov_) { + // For seekable files, we seek to the 'moov' and load the 'moov' first + // then seek back (see LoadMoov function for details); we do not support + // having 'mdat' before 'moov' for non-seekable files. The code ends up + // here only if it is a non-seekable file. + NOTIMPLEMENTED() << " Non-seekable Files with 'mdat' box before 'moov' " + "box is not supported."; + *err = true; + return false; + } else { + // This can happen if there are unused 'mdat' boxes, which is unusual + // but allowed by the spec. Ignore the 'mdat' and proceed. + LOG(INFO) + << "Ignore unused 'mdat' box - this could be as a result of extra " + "not usable 'mdat' or 'mdat' associated with unrecognized track."; + } } // Set up mdat offset for ReadMDATsUntil(). diff --git a/packager/media/formats/mp4/mp4_media_parser_unittest.cc b/packager/media/formats/mp4/mp4_media_parser_unittest.cc index ea12f9a8f8..ba62152a4e 100644 --- a/packager/media/formats/mp4/mp4_media_parser_unittest.cc +++ b/packager/media/formats/mp4/mp4_media_parser_unittest.cc @@ -188,6 +188,15 @@ TEST_F(MP4MediaParserTest, TrailingMoov) { EXPECT_EQ(201u, num_samples_); } +TEST_F(MP4MediaParserTest, TrailingMoovAndAdditionalMdat) { + // The additional mdat should just be ignored, so the parse is still + // successful with the same result. + EXPECT_TRUE( + ParseMP4File("bear-640x360-trailing-moov-additional-mdat.mp4", 1024)); + EXPECT_EQ(2u, num_streams_); + EXPECT_EQ(201u, num_samples_); +} + TEST_F(MP4MediaParserTest, Flush) { // Flush while reading sample data, then start a new stream. InitializeParser(NULL); diff --git a/packager/media/test/data/README b/packager/media/test/data/README index fc6cc634bb..5ed47b5331 100644 --- a/packager/media/test/data/README +++ b/packager/media/test/data/README @@ -36,6 +36,7 @@ bear-640x360.mp4 - Same as above, but in a different resolution. bear-640x360-hevc.mp4 - Same content, but encoded with HEVC. bear-320x180.mp4 - Same as above, but in a different resolution. bear-640x360-trailing-moov.mp4 - Same content, but with moov box in the end. +bear-640x360-trailing-moov-additional-mdat.mp4 - Same content, but with moov box in the end and an additional unused mdat, which should be ignored. bear-640x360-av_frag.mp4 - Same content, but in fragmented mp4. bear-640x360-aac_lc-silent_right.mp4 - Audio only, stereo, but right channel is silent, with AAC-LC profile. bear-640x360-aac_he-silent_right.mp4 - Same as above, but with AAC-HE profile. diff --git a/packager/media/test/data/bear-640x360-trailing-moov-additional-mdat.mp4 b/packager/media/test/data/bear-640x360-trailing-moov-additional-mdat.mp4 new file mode 100644 index 0000000000..d2a7e791c7 Binary files /dev/null and b/packager/media/test/data/bear-640x360-trailing-moov-additional-mdat.mp4 differ