Bug 1128410 - Make memory allocation dynamic. r=kentuckyfriedtakahe, a=sledru
authorJean-Yves Avenard <jyavenard@mozilla.com>
Mon, 02 Feb 2015 21:36:51 +1100
changeset 243652 08a02585bc60
parent 243651 cd4ce87ac395
child 243653 47e26f891d66
push id4421
push userryanvm@gmail.com
push date2015-02-02 19:52 +0000
treeherdermozilla-beta@08a02585bc60 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskentuckyfriedtakahe, sledru
bugs1128410
milestone36.0
Bug 1128410 - Make memory allocation dynamic. r=kentuckyfriedtakahe, a=sledru Allocations are fallibles.
media/libstagefright/frameworks/av/media/libstagefright/MPEG4Extractor.cpp
--- a/media/libstagefright/frameworks/av/media/libstagefright/MPEG4Extractor.cpp
+++ b/media/libstagefright/frameworks/av/media/libstagefright/MPEG4Extractor.cpp
@@ -32,16 +32,17 @@
 #include <media/stagefright/foundation/ABuffer.h>
 #include <media/stagefright/foundation/ADebug.h>
 #include <media/stagefright/foundation/AMessage.h>
 #include <media/stagefright/MediaBuffer.h>
 #include <media/stagefright/MediaDefs.h>
 #include <media/stagefright/MediaSource.h>
 #include <media/stagefright/MetaData.h>
 #include <utils/String8.h>
+#include "nsTArray.h"
 
 namespace stagefright {
 
 class MPEG4Source : public MediaSource {
 public:
     // Caller retains ownership of both "dataSource" and "sampleTable".
     MPEG4Source(const sp<MetaData> &format,
                 const sp<DataSource> &dataSource,
@@ -98,17 +99,17 @@ private:
 
     bool mStarted;
 
     MediaBuffer *mBuffer;
 
     bool mWantsNALFragments;
 
     uint8_t *mSrcBuffer;
-    int32_t mSrcBufferSize;
+    FallibleTArray<uint8_t> mSrcBackend;
 
     size_t parseNALSize(const uint8_t *data) const;
     status_t parseChunk(off64_t *offset);
     status_t parseTrackFragmentData(off64_t offset, off64_t size);
     status_t parseTrackFragmentHeader(off64_t offset, off64_t size);
     status_t parseTrackFragmentRun(off64_t offset, off64_t size);
     status_t parseSampleAuxiliaryInformationSizes(off64_t offset, off64_t size);
     status_t parseSampleAuxiliaryInformationOffsets(off64_t offset, off64_t size);
@@ -160,17 +161,17 @@ private:
         off64_t mSize;
     };
     Vector<AuxRange> mDeferredSaiz;
     Vector<AuxRange> mDeferredSaio;
 
     MPEG4Source(const MPEG4Source &);
     MPEG4Source &operator=(const MPEG4Source &);
 
-    bool ensureSrcBufferAllocated();
+    bool ensureSrcBufferAllocated(int32_t size);
 };
 
 // This custom data source wraps an existing one and satisfies requests
 // falling entirely within a cached range from the cache while forwarding
 // all remaining requests to the wrapped datasource.
 // This is used to cache the full sampletable metadata for a single track,
 // possibly wrapping multiple times to cover all tracks, i.e.
 // Each MPEG4DataSource caches the sampletable metadata for a single track.
@@ -2507,17 +2508,16 @@ MPEG4Source::MPEG4Source(
       mCurrentSampleInfoOffsetsAllocSize(0),
       mCurrentSampleInfoOffsets(NULL),
       mIsAVC(false),
       mNALLengthSize(0),
       mStarted(false),
       mBuffer(NULL),
       mWantsNALFragments(false),
       mSrcBuffer(NULL),
-      mSrcBufferSize(0),
       mTrackExtends(trackExtends) {
 
     mFormat->findInt32(kKeyCryptoMode, &mCryptoMode);
     mDefaultIVSize = 0;
     mFormat->findInt32(kKeyCryptoDefaultIVSize, &mDefaultIVSize);
     uint32_t keytype;
     const void *key;
     size_t keysize;
@@ -2567,35 +2567,34 @@ status_t MPEG4Source::start(MetaData *pa
     int32_t val;
     if (params && params->findInt32(kKeyWantsNALFragments, &val)
         && val != 0) {
         mWantsNALFragments = true;
     } else {
         mWantsNALFragments = false;
     }
 
-    CHECK(mFormat->findInt32(kKeyMaxInputSize, &mSrcBufferSize));
+    mSrcBuffer = mSrcBackend.Elements();
 
     mStarted = true;
 
     return OK;
 }
 
 status_t MPEG4Source::stop() {
     Mutex::Autolock autoLock(mLock);
 
     CHECK(mStarted);
 
     if (mBuffer != NULL) {
         mBuffer->release();
         mBuffer = NULL;
     }
 
-    delete[] mSrcBuffer;
-    mSrcBuffer = NULL;
+    mSrcBackend.Clear();
 
     mStarted = false;
     mCurrentSampleIndex = 0;
 
     return OK;
 }
 
 status_t MPEG4Source::parseChunk(off64_t *offset) {
@@ -3261,25 +3260,24 @@ status_t MPEG4Source::lookForMoof() {
         }
         if (chunk_type == FOURCC('m', 'd', 'a', 't')) {
             return OK;
         }
         offset += chunk_size;
     }
 }
 
-bool MPEG4Source::ensureSrcBufferAllocated() {
-    if (mSrcBuffer) {
+bool MPEG4Source::ensureSrcBufferAllocated(int32_t aSize) {
+    if (mSrcBackend.Length() >= aSize) {
         return true;
     }
-    if (!ValidInputSize(mSrcBufferSize)) {
-      ALOGE("Invalid max input size %d", mSrcBufferSize);
-      return false;
+    if (!mSrcBackend.SetLength(aSize)) {
+        return false;
     }
-    mSrcBuffer = new uint8_t[mSrcBufferSize];
+    mSrcBuffer = mSrcBackend.Elements();
     return true;
 }
 
 status_t MPEG4Source::read(
         MediaBuffer **out, const ReadOptions *options) {
     Mutex::Autolock autoLock(mLock);
 
     CHECK(mStarted);
@@ -3517,17 +3515,17 @@ status_t MPEG4Source::read(
         // the NAL length, stored in four bytes.
         ssize_t num_bytes_read = 0;
         int32_t drm = 0;
         bool usesDRM = (mFormat->findInt32(kKeyIsDRM, &drm) && drm != 0);
         if (usesDRM) {
             num_bytes_read =
                 mDataSource->readAt(offset, (uint8_t*)mBuffer->data(), size);
         } else {
-            if (!ensureSrcBufferAllocated()) {
+            if (!ensureSrcBufferAllocated(size)) {
                 return ERROR_MALFORMED;
             }
             num_bytes_read = mDataSource->readAt(offset, mSrcBuffer, size);
         }
 
         if (num_bytes_read < (ssize_t)size) {
             mBuffer->release();
             mBuffer = NULL;
@@ -3872,17 +3870,17 @@ status_t MPEG4Source::fragmentedRead(
         // the NAL unit's length, stored in four bytes.
         ssize_t num_bytes_read = 0;
         int32_t drm = 0;
         bool usesDRM = (mFormat->findInt32(kKeyIsDRM, &drm) && drm != 0);
         if (usesDRM) {
             num_bytes_read =
                 mDataSource->readAt(offset, (uint8_t*)mBuffer->data(), size);
         } else {
-            if (!ensureSrcBufferAllocated()) {
+            if (!ensureSrcBufferAllocated(size)) {
                 return ERROR_MALFORMED;
             }
             num_bytes_read = mDataSource->readAt(offset, mSrcBuffer, size);
         }
 
         if (num_bytes_read < (ssize_t)size) {
             mBuffer->release();
             mBuffer = NULL;