Bug 1061079 - Make BufferStream work for a moving window; r=edwin
authorAnthony Jones <ajones@mozilla.com>
Thu, 11 Sep 2014 15:57:33 +1200
changeset 204768 3331b49ff9e6063c4f2347e2a729631bbbca760a
parent 204767 f68c40ef23276ca3e6f4d612046545afc77a7334
child 204769 2097b83ffbab847fa200bb891fc0bd3ac98d3463
push id27465
push usercbook@mozilla.com
push dateThu, 11 Sep 2014 13:27:15 +0000
treeherdermozilla-central@ef81f73048dc [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersedwin
bugs1061079
milestone35.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1061079 - Make BufferStream work for a moving window; r=edwin
media/libstagefright/binding/BufferStream.cpp
media/libstagefright/binding/MoofParser.cpp
media/libstagefright/binding/include/mp4_demuxer/BufferStream.h
media/libstagefright/binding/include/mp4_demuxer/MoofParser.h
media/libstagefright/binding/include/mp4_demuxer/mp4_demuxer.h
--- a/media/libstagefright/binding/BufferStream.cpp
+++ b/media/libstagefright/binding/BufferStream.cpp
@@ -1,35 +1,58 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "mp4_demuxer/BufferStream.h"
+#include "MediaResource.h"
 #include <algorithm>
 
+using namespace mozilla;
+
 namespace mp4_demuxer {
 
-BufferStream::BufferStream(const uint8_t* aData, size_t aLength)
-  : mData(aData), mLength(aLength)
+BufferStream::BufferStream()
+  : mStartOffset(0)
 {
-
 }
 
 /*virtual*/ bool
 BufferStream::ReadAt(int64_t aOffset, void* aData, size_t aLength,
                      size_t* aBytesRead)
 {
-  if (aOffset > mLength) {
+  if (aOffset < mStartOffset || aOffset > mStartOffset + mData.Length()) {
     return false;
   }
-  *aBytesRead = std::min(aLength, mLength - (size_t) aOffset);
-  memcpy(aData, mData + aOffset, *aBytesRead);
+  *aBytesRead =
+    std::min(aLength, size_t(mStartOffset + mData.Length() - aOffset));
+  memcpy(aData, &mData[aOffset - mStartOffset], *aBytesRead);
   return true;
 }
 
 /*virtual*/ bool
 BufferStream::Length(int64_t* aLength)
 {
-  *aLength = mLength;
+  *aLength = mStartOffset + mData.Length();
   return true;
 }
 
+/* virtual */ void
+BufferStream::DiscardBefore(int64_t aOffset)
+{
+  if (aOffset > mStartOffset) {
+    mData.RemoveElementsAt(0, aOffset - mStartOffset);
+    mStartOffset = aOffset;
+  }
 }
+
+void
+BufferStream::AppendBytes(const uint8_t* aData, size_t aLength)
+{
+  mData.AppendElements(aData, aLength);
+}
+
+MediaByteRange
+BufferStream::GetByteRange()
+{
+  return MediaByteRange(mStartOffset, mStartOffset + mData.Length());
+}
+}
--- a/media/libstagefright/binding/MoofParser.cpp
+++ b/media/libstagefright/binding/MoofParser.cpp
@@ -32,21 +32,26 @@ MoofParser::RebuildFragmentedIndex(const
 
       mMoofs.AppendElement(moof);
     }
     mOffset = box.NextOffset();
   }
 }
 
 Interval<Microseconds>
-MoofParser::GetCompositionRange()
+MoofParser::GetCompositionRange(const nsTArray<MediaByteRange>& aByteRanges)
 {
   Interval<Microseconds> compositionRange;
+  BoxContext context(mSource, aByteRanges);
   for (size_t i = 0; i < mMoofs.Length(); i++) {
-    compositionRange = compositionRange.Extents(mMoofs[i].mTimeRange);
+    Moof& moof = mMoofs[i];
+    Box box(&context, moof.mRange.mStart);
+    if (box.IsAvailable()) {
+      compositionRange = compositionRange.Extents(moof.mTimeRange);
+    }
   }
   return compositionRange;
 }
 
 bool
 MoofParser::ReachedEnd()
 {
   int64_t length;
--- a/media/libstagefright/binding/include/mp4_demuxer/BufferStream.h
+++ b/media/libstagefright/binding/include/mp4_demuxer/BufferStream.h
@@ -1,30 +1,37 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef BUFFER_STREAM_H_
 #define BUFFER_STREAM_H_
 
 #include "mp4_demuxer/mp4_demuxer.h"
+#include "nsTArray.h"
 
 namespace mp4_demuxer {
 
 class BufferStream : public Stream
 {
 public:
   /* BufferStream does not take ownership of aData nor does it make a copy.
    * Therefore BufferStream shouldn't get used after aData is destroyed.
    */
-  BufferStream(const uint8_t* aData, size_t aLength);
+  BufferStream();
 
   virtual bool ReadAt(int64_t aOffset, void* aData, size_t aLength,
                       size_t* aBytesRead) MOZ_OVERRIDE;
   virtual bool Length(int64_t* aLength) MOZ_OVERRIDE;
 
+  virtual void DiscardBefore(int64_t aOffset) MOZ_OVERRIDE;
+
+  void AppendBytes(const uint8_t* aData, size_t aLength);
+
+  mozilla::MediaByteRange GetByteRange();
+
 private:
-  const uint8_t* mData;
-  size_t mLength;
+  int64_t mStartOffset;
+  nsTArray<uint8_t> mData;
 };
 }
 
 #endif
--- a/media/libstagefright/binding/include/mp4_demuxer/MoofParser.h
+++ b/media/libstagefright/binding/include/mp4_demuxer/MoofParser.h
@@ -126,17 +126,18 @@ public:
   MoofParser(Stream* aSource, uint32_t aTrackId)
     : mSource(aSource), mOffset(0), mTrex(aTrackId)
   {
     // Setting the mTrex.mTrackId to 0 is a nasty work around for calculating
     // the composition range for MSE. We need an array of tracks.
   }
   void RebuildFragmentedIndex(
     const nsTArray<mozilla::MediaByteRange>& aByteRanges);
-  Interval<Microseconds> GetCompositionRange();
+  Interval<Microseconds> GetCompositionRange(
+    const nsTArray<mozilla::MediaByteRange>& aByteRanges);
   bool ReachedEnd();
   void ParseMoov(Box& aBox);
   void ParseTrak(Box& aBox);
   void ParseMdia(Box& aBox, Tkhd& aTkhd);
   void ParseMvex(Box& aBox);
 
   mozilla::MediaByteRange mInitRange;
   nsRefPtr<Stream> mSource;
--- a/media/libstagefright/binding/include/mp4_demuxer/mp4_demuxer.h
+++ b/media/libstagefright/binding/include/mp4_demuxer/mp4_demuxer.h
@@ -23,16 +23,18 @@ class Stream
 {
 public:
   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Stream);
 
   virtual bool ReadAt(int64_t offset, void* data, size_t size,
                       size_t* bytes_read) = 0;
   virtual bool Length(int64_t* size) = 0;
 
+  virtual void DiscardBefore(int64_t offset) {}
+
 protected:
   virtual ~Stream() {}
 };
 
 enum TrackType { kVideo = 1, kAudio };
 
 class MP4Demuxer
 {