Bug 1159027: Part2. Extend BufferStream to work with external buffer array. r=kentuckyfriedtakahe
authorJean-Yves Avenard <jyavenard@mozilla.com>
Mon, 11 May 2015 20:54:59 +1000
changeset 243354 66c9b5468e3101d1e382d776812fa75d06457d01
parent 243353 d6a525aad9de80ae90a7f5e72e4645a634e17a93
child 243355 4d58f20f7252be597a200d51df3ebdb2208d8960
push id28738
push usercbook@mozilla.com
push dateTue, 12 May 2015 14:11:31 +0000
treeherdermozilla-central@bedce1b405a3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskentuckyfriedtakahe
bugs1159027
milestone40.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 1159027: Part2. Extend BufferStream to work with external buffer array. r=kentuckyfriedtakahe Also make BufferStream::AppendBytes fallible
media/libstagefright/binding/BufferStream.cpp
media/libstagefright/binding/include/mp4_demuxer/BufferStream.h
--- a/media/libstagefright/binding/BufferStream.cpp
+++ b/media/libstagefright/binding/BufferStream.cpp
@@ -1,65 +1,77 @@
 /* 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 "MediaData.h"
 #include "MediaResource.h"
 #include <algorithm>
 
 using namespace mozilla;
 
 namespace mp4_demuxer {
 
 BufferStream::BufferStream()
   : mStartOffset(0)
+  , mData(new mozilla::MediaLargeByteBuffer)
+{
+}
+
+BufferStream::BufferStream(mozilla::MediaLargeByteBuffer* aBuffer)
+  : mStartOffset(0)
+  , mData(aBuffer)
+{
+}
+
+BufferStream::~BufferStream()
 {
 }
 
 /*virtual*/ bool
 BufferStream::ReadAt(int64_t aOffset, void* aData, size_t aLength,
                      size_t* aBytesRead)
 {
-  if (aOffset < mStartOffset || aOffset > mStartOffset + mData.Length()) {
+  if (aOffset < mStartOffset || aOffset > mStartOffset + mData->Length()) {
     return false;
   }
   *aBytesRead =
-    std::min(aLength, size_t(mStartOffset + mData.Length() - aOffset));
-  memcpy(aData, &mData[aOffset - mStartOffset], *aBytesRead);
+    std::min(aLength, size_t(mStartOffset + mData->Length() - aOffset));
+  memcpy(aData, &(*mData)[aOffset - mStartOffset], *aBytesRead);
   return true;
 }
 
 /*virtual*/ bool
 BufferStream::CachedReadAt(int64_t aOffset, void* aData, size_t aLength,
                            size_t* aBytesRead)
 {
   return ReadAt(aOffset, aData, aLength, aBytesRead);
 }
 
 /*virtual*/ bool
 BufferStream::Length(int64_t* aLength)
 {
-  *aLength = mStartOffset + mData.Length();
+  *aLength = mStartOffset + mData->Length();
   return true;
 }
 
 /* virtual */ void
 BufferStream::DiscardBefore(int64_t aOffset)
 {
   if (aOffset > mStartOffset) {
-    mData.RemoveElementsAt(0, aOffset - mStartOffset);
+    mData->RemoveElementsAt(0, aOffset - mStartOffset);
     mStartOffset = aOffset;
   }
 }
 
-void
+bool
 BufferStream::AppendBytes(const uint8_t* aData, size_t aLength)
 {
-  mData.AppendElements(aData, aLength);
+  return mData->AppendElements(aData, aLength);
 }
 
 MediaByteRange
 BufferStream::GetByteRange()
 {
-  return MediaByteRange(mStartOffset, mStartOffset + mData.Length());
+  return MediaByteRange(mStartOffset, mStartOffset + mData->Length());
 }
 }
--- a/media/libstagefright/binding/include/mp4_demuxer/BufferStream.h
+++ b/media/libstagefright/binding/include/mp4_demuxer/BufferStream.h
@@ -1,39 +1,46 @@
 /* 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 "mp4_demuxer/Stream.h"
 #include "nsTArray.h"
+#include "MediaResource.h"
+
+namespace mozilla {
+class MediaLargeByteBuffer;
+}
 
 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();
+  explicit BufferStream(mozilla::MediaLargeByteBuffer* aBuffer);
 
   virtual bool ReadAt(int64_t aOffset, void* aData, size_t aLength,
                       size_t* aBytesRead) override;
   virtual bool CachedReadAt(int64_t aOffset, void* aData, size_t aLength,
                             size_t* aBytesRead) override;
   virtual bool Length(int64_t* aLength) override;
 
   virtual void DiscardBefore(int64_t aOffset) override;
 
-  void AppendBytes(const uint8_t* aData, size_t aLength);
+  bool AppendBytes(const uint8_t* aData, size_t aLength);
 
   mozilla::MediaByteRange GetByteRange();
 
 private:
+  ~BufferStream();
   int64_t mStartOffset;
-  nsTArray<uint8_t> mData;
+  nsRefPtr<mozilla::MediaLargeByteBuffer> mData;
 };
 }
 
 #endif