Bug 1336271 - ignore ftyp box when parsing mp4 metadata. r=jya
☠☠ backed out by 4fa8134024ea ☠ ☠
authorAlfredo Yang <ayang@mozilla.com>
Wed, 03 May 2017 16:55:00 +0800
changeset 573232 2c7d3f3cb9e8ab7842f344b50fafb89d73ddfaec
parent 573231 a56275e2228bbe67f787718aea3f2e99d8ff9726
child 573233 31163f835c341bd9416a0e95b8ac2d4e8f86a44f
push id57306
push userbmo:emilio+bugs@crisal.io
push dateFri, 05 May 2017 10:08:55 +0000
reviewersjya
bugs1336271
milestone55.0a1
Bug 1336271 - ignore ftyp box when parsing mp4 metadata. r=jya MozReview-Commit-ID: IarGzXtmGQ2
media/libstagefright/binding/MoofParser.cpp
media/libstagefright/binding/include/mp4_demuxer/MoofParser.h
--- a/media/libstagefright/binding/MoofParser.cpp
+++ b/media/libstagefright/binding/MoofParser.cpp
@@ -143,81 +143,64 @@ MoofParser::BlockingReadNextMoof()
       byteRanges += MediaByteRange(mOffset, box.Range().mEnd);
       return RebuildFragmentedIndex(context);
     }
   }
   return false;
 }
 
 void
-MoofParser::ScanForMetadata(mozilla::MediaByteRange& aFtyp,
-                            mozilla::MediaByteRange& aMoov)
+MoofParser::ScanForMetadata(mozilla::MediaByteRange& aMoov)
 {
   int64_t length = std::numeric_limits<int64_t>::max();
   mSource->Length(&length);
   MediaByteRangeSet byteRanges;
   byteRanges += MediaByteRange(0, length);
   RefPtr<mp4_demuxer::BlockingStream> stream = new BlockingStream(mSource);
 
+  mozilla::MediaByteRange initRange;
   BoxContext context(stream, byteRanges);
   for (Box box(&context, mOffset); box.IsAvailable(); box = box.Next()) {
-    if (box.IsType("ftyp")) {
-      aFtyp = box.Range();
-      continue;
-    }
+    initRange = initRange.Span(box.Range());
     if (box.IsType("moov")) {
-      aMoov = box.Range();
-      break;
+      // mInitRange is from stream start position to end of moov.
+      mInitRange = aMoov = initRange;
+      return;
     }
   }
-  mInitRange = aFtyp.Span(aMoov);
 }
 
 bool
 MoofParser::HasMetadata()
 {
-  MediaByteRange ftyp;
   MediaByteRange moov;
-  ScanForMetadata(ftyp, moov);
-  return !!ftyp.Length() && !!moov.Length();
+  ScanForMetadata(moov);
+  return !!moov.Length();
 }
 
 already_AddRefed<mozilla::MediaByteBuffer>
 MoofParser::Metadata()
 {
-  MediaByteRange ftyp;
   MediaByteRange moov;
-  ScanForMetadata(ftyp, moov);
-  CheckedInt<MediaByteBuffer::size_type> ftypLength = ftyp.Length();
+  ScanForMetadata(moov);
   CheckedInt<MediaByteBuffer::size_type> moovLength = moov.Length();
-  if (!ftypLength.isValid() || !moovLength.isValid()
-      || !ftypLength.value() || !moovLength.value()) {
-    // No ftyp or moov, or they cannot be used as array size.
-    return nullptr;
-  }
-  CheckedInt<MediaByteBuffer::size_type> totalLength = ftypLength + moovLength;
-  if (!totalLength.isValid()) {
-    // Addition overflow, or sum cannot be used as array size.
+  if (!moovLength.isValid() || !moovLength.value()) {
+    // No moov, or they cannot be used as array size.
     return nullptr;
   }
   RefPtr<MediaByteBuffer> metadata = new MediaByteBuffer();
-  if (!metadata->SetLength(totalLength.value(), fallible)) {
+  if (!metadata->SetLength(moovLength.value(), fallible)) {
     // OOM
     return nullptr;
   }
 
   RefPtr<mp4_demuxer::BlockingStream> stream = new BlockingStream(mSource);
   size_t read;
   bool rv =
-    stream->ReadAt(ftyp.mStart, metadata->Elements(), ftypLength.value(), &read);
-  if (!rv || read != ftypLength.value()) {
-    return nullptr;
-  }
-  rv =
-    stream->ReadAt(moov.mStart, metadata->Elements() + ftypLength.value(), moovLength.value(), &read);
+    stream->ReadAt(moov.mStart, metadata->Elements(), moovLength.value(), &read);
   if (!rv || read != moovLength.value()) {
     return nullptr;
   }
   return metadata.forget();
 }
 
 Interval<Microseconds>
 MoofParser::GetCompositionRange(const MediaByteRangeSet& aByteRanges)
--- a/media/libstagefright/binding/include/mp4_demuxer/MoofParser.h
+++ b/media/libstagefright/binding/include/mp4_demuxer/MoofParser.h
@@ -294,18 +294,17 @@ public:
   Edts mEdts;
   Sinf mSinf;
 
   nsTArray<CencSampleEncryptionInfoEntry> mTrackSampleEncryptionInfoEntries;
   nsTArray<SampleToGroupEntry> mTrackSampleToGroupEntries;
 
   nsTArray<Moof>& Moofs() { return mMoofs; }
 private:
-  void ScanForMetadata(mozilla::MediaByteRange& aFtyp,
-                       mozilla::MediaByteRange& aMoov);
+  void ScanForMetadata(mozilla::MediaByteRange& aMoov);
   nsTArray<Moof> mMoofs;
   nsTArray<MediaByteRange> mMediaRanges;
   bool mIsAudio;
   uint64_t mLastDecodeTime;
 };
 }
 
 #endif