Bug 1127554 - Get rid of infallible allocation in MP4Sample copy constructor. v2 r=mattwoodrow
authorBobby Holley <bobbyholley@gmail.com>
Wed, 11 Feb 2015 14:31:24 -0800
changeset 249913 bbda80b65439a11b7f7c4d1ffb4540465894a7c1
parent 249912 ec501c1722c02202b6e03cec277d948974a65e73
child 249914 c975f91c0466ec8d1c23c0d327df47ac373e5dfb
push id4489
push userraliiev@mozilla.com
push dateMon, 23 Feb 2015 15:17:55 +0000
treeherdermozilla-beta@fd7c3dc24146 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmattwoodrow
bugs1127554
milestone37.0a2
Bug 1127554 - Get rid of infallible allocation in MP4Sample copy constructor. v2 r=mattwoodrow
dom/media/fmp4/apple/AppleATDecoder.cpp
media/libstagefright/binding/DecoderData.cpp
media/libstagefright/binding/include/mp4_demuxer/DecoderData.h
--- a/dom/media/fmp4/apple/AppleATDecoder.cpp
+++ b/dom/media/fmp4/apple/AppleATDecoder.cpp
@@ -461,21 +461,25 @@ static void
                 AudioStreamPacketDescription* aPackets)
 {
 }
 
 nsresult
 AppleATDecoder::GetImplicitAACMagicCookie(const mp4_demuxer::MP4Sample* aSample)
 {
   // Prepend ADTS header to AAC audio.
-  mp4_demuxer::MP4Sample adtssample(*aSample);
+  nsAutoPtr<mp4_demuxer::MP4Sample> adtssample(aSample->Clone());
+  if (!adtssample) {
+    return NS_ERROR_OUT_OF_MEMORY;
+  }
+
   bool rv = mp4_demuxer::Adts::ConvertSample(mConfig.channel_count,
                                              mConfig.frequency_index,
                                              mConfig.aac_profile,
-                                             &adtssample);
+                                             adtssample);
   if (!rv) {
     NS_WARNING("Failed to apply ADTS header");
     return NS_ERROR_FAILURE;
   }
   if (!mStream) {
     OSStatus rv = AudioFileStreamOpen(this,
                                       _MetadataCallback,
                                       _SampleCallback,
@@ -483,18 +487,18 @@ AppleATDecoder::GetImplicitAACMagicCooki
                                       &mStream);
     if (rv) {
       NS_WARNING("Couldn't open AudioFileStream");
       return NS_ERROR_FAILURE;
     }
   }
 
   OSStatus status = AudioFileStreamParseBytes(mStream,
-                                              adtssample.size,
-                                              adtssample.data,
+                                              adtssample->size,
+                                              adtssample->data,
                                               0 /* discontinuity */);
   if (status) {
     NS_WARNING("Couldn't parse sample");
   }
 
   if (status || mFileStreamError || mMagicCookie.Length()) {
     // We have decoded a magic cookie or an error occurred as such
     // we won't need the stream any longer.
--- a/media/libstagefright/binding/DecoderData.cpp
+++ b/media/libstagefright/binding/DecoderData.cpp
@@ -205,29 +205,34 @@ MP4Sample::MP4Sample()
   , byte_offset(0)
   , is_sync_point(0)
   , data(nullptr)
   , size(0)
   , extra_data(nullptr)
 {
 }
 
-MP4Sample::MP4Sample(const MP4Sample& copy)
-  : mMediaBuffer(nullptr)
-  , decode_timestamp(copy.decode_timestamp)
-  , composition_timestamp(copy.composition_timestamp)
-  , duration(copy.duration)
-  , byte_offset(copy.byte_offset)
-  , is_sync_point(copy.is_sync_point)
-  , size(copy.size)
-  , crypto(copy.crypto)
-  , extra_data(copy.extra_data)
+MP4Sample*
+MP4Sample::Clone() const
 {
-  extra_buffer = data = new uint8_t[size];
-  memcpy(data, copy.data, size);
+  nsAutoPtr<MP4Sample> s(new MP4Sample());
+  s->decode_timestamp = decode_timestamp;
+  s->composition_timestamp = composition_timestamp;
+  s->duration = duration;
+  s->byte_offset = byte_offset;
+  s->is_sync_point = is_sync_point;
+  s->size = size;
+  s->crypto = crypto;
+  s->extra_data = extra_data;
+  s->extra_buffer = s->data = new (fallible) uint8_t[size];
+  if (!s->extra_buffer) {
+    return nullptr;
+  }
+  memcpy(s->data, data, size);
+  return s.forget();
 }
 
 MP4Sample::~MP4Sample()
 {
   if (mMediaBuffer) {
     mMediaBuffer->release();
   }
 }
--- a/media/libstagefright/binding/include/mp4_demuxer/DecoderData.h
+++ b/media/libstagefright/binding/include/mp4_demuxer/DecoderData.h
@@ -150,18 +150,18 @@ public:
 };
 
 typedef int64_t Microseconds;
 
 class MP4Sample
 {
 public:
   MP4Sample();
-  MP4Sample(const MP4Sample& copy);
   virtual ~MP4Sample();
+  MP4Sample* Clone() const;
   void Update(int64_t& aMediaTime, int64_t& aTimestampOffset);
   void Pad(size_t aPaddingBytes);
 
   stagefright::MediaBuffer* mMediaBuffer;
 
   Microseconds decode_timestamp;
   Microseconds composition_timestamp;
   Microseconds duration;
@@ -173,12 +173,14 @@ public:
 
   CryptoSample crypto;
   nsRefPtr<ByteBuffer> extra_data;
 
   void Prepend(const uint8_t* aData, size_t aSize);
   void Replace(const uint8_t* aData, size_t aSize);
 
   nsAutoArrayPtr<uint8_t> extra_buffer;
+private:
+  MP4Sample(const MP4Sample&); // Not implemented
 };
 }
 
 #endif