Bug 1091976 - Process MP4Reader::NotifyDataArrived() immediately; r=edwin
authorAnthony Jones <ajones@mozilla.com>
Mon, 10 Nov 2014 13:32:09 +1300
changeset 226142 6592c05a4bb2ac4035b44d6739da252e2b6ad2e8
parent 226141 daa70eac3342a08297a86e1878fa14398bfb92f1
child 226143 b36d7a55077726dc7710602c82334c64d26636c0
push id36
push userdburns@mozilla.com
push dateMon, 10 Nov 2014 15:14:02 +0000
reviewersedwin
bugs1091976
milestone36.0a1
Bug 1091976 - Process MP4Reader::NotifyDataArrived() immediately; r=edwin
dom/media/fmp4/MP4Reader.cpp
dom/media/mediasource/SourceBufferResource.cpp
media/libstagefright/binding/Box.cpp
media/libstagefright/binding/BufferStream.cpp
media/libstagefright/binding/include/mp4_demuxer/BufferStream.h
media/libstagefright/binding/include/mp4_demuxer/mp4_demuxer.h
--- a/dom/media/fmp4/MP4Reader.cpp
+++ b/dom/media/fmp4/MP4Reader.cpp
@@ -86,16 +86,29 @@ public:
         return false;
       }
       sum += bytesRead;
     } while (sum < aCount && bytesRead > 0);
     *aBytesRead = sum;
     return true;
   }
 
+  virtual bool CachedReadAt(int64_t aOffset, void* aBuffer, size_t aCount,
+                            size_t* aBytesRead) MOZ_OVERRIDE
+  {
+    nsresult rv = mResource->ReadFromCache(reinterpret_cast<char*>(aBuffer),
+                                           aOffset, aCount);
+    if (NS_FAILED(rv)) {
+      *aBytesRead = 0;
+      return false;
+    }
+    *aBytesRead = aCount;
+    return true;
+  }
+
   virtual bool Length(int64_t* aSize) MOZ_OVERRIDE
   {
     if (mResource->GetLength() < 0)
       return false;
     *aSize = mResource->GetLength();
     return true;
   }
 
@@ -780,24 +793,17 @@ MP4Reader::Seek(int64_t aTime,
 
   GetCallback()->OnSeekCompleted(NS_OK);
 }
 
 void
 MP4Reader::NotifyDataArrived(const char* aBuffer, uint32_t aLength,
                              int64_t aOffset)
 {
-  if (NS_IsMainThread()) {
-    if (GetTaskQueue()) {
-      GetTaskQueue()->Dispatch(
-        NS_NewRunnableMethod(this, &MP4Reader::UpdateIndex));
-    }
-  } else {
-    UpdateIndex();
-  }
+  UpdateIndex();
 }
 
 void
 MP4Reader::UpdateIndex()
 {
   MonitorAutoLock mon(mIndexMonitor);
   if (!mIndexReady) {
     return;
--- a/dom/media/mediasource/SourceBufferResource.cpp
+++ b/dom/media/mediasource/SourceBufferResource.cpp
@@ -130,17 +130,18 @@ SourceBufferResource::SeekInternal(int64
 
 nsresult
 SourceBufferResource::ReadFromCache(char* aBuffer, int64_t aOffset, uint32_t aCount)
 {
   SBR_DEBUG("SourceBufferResource(%p)::ReadFromCache(aBuffer=%p, aOffset=%lld, aCount=%u)",
             this, aBuffer, aOffset, aCount);
   ReentrantMonitorAutoEnter mon(mMonitor);
   int64_t oldOffset = mOffset;
-  nsresult rv = ReadAt(aOffset, aBuffer, aCount, nullptr);
+  uint32_t bytesRead;
+  nsresult rv = ReadAt(aOffset, aBuffer, aCount, &bytesRead);
   mOffset = oldOffset;
   return rv;
 }
 
 uint32_t
 SourceBufferResource::EvictData(uint32_t aThreshold)
 {
   SBR_DEBUG("SourceBufferResource(%p)::EvictData(aThreshold=%u)", this, aThreshold);
--- a/media/libstagefright/binding/Box.cpp
+++ b/media/libstagefright/binding/Box.cpp
@@ -29,30 +29,31 @@ Box::Box(BoxContext* aContext, uint64_t 
 
     byteRange = &mContext->mByteRanges[i];
     if (byteRange->Contains(headerRange)) {
       break;
     }
   }
 
   size_t bytes;
-  if (!mContext->mSource->ReadAt(aOffset, header, sizeof(header), &bytes) ||
+  if (!mContext->mSource->CachedReadAt(aOffset, header, sizeof(header),
+                                       &bytes) ||
       bytes != sizeof(header)) {
     return;
   }
 
   uint64_t size = BigEndian::readUint32(header);
   if (size == 1) {
     uint8_t bigLength[8];
     MediaByteRange bigLengthRange(headerRange.mEnd,
                                   headerRange.mEnd + sizeof(bigLength));
     if ((mParent && !mParent->mRange.Contains(bigLengthRange)) ||
         !byteRange->Contains(bigLengthRange) ||
-        !mContext->mSource->ReadAt(aOffset, bigLength,
-                                   sizeof(bigLengthRange), &bytes) ||
+        !mContext->mSource->CachedReadAt(aOffset, bigLength,
+                                         sizeof(bigLengthRange), &bytes) ||
         bytes != sizeof(bigLengthRange)) {
       return;
     }
     size = BigEndian::readUint64(bigLength);
     mChildOffset = bigLengthRange.mEnd;
   } else {
     mChildOffset = headerRange.mEnd;
   }
@@ -81,17 +82,17 @@ Box::FirstChild() const
   return Box(mContext, mChildOffset, this);
 }
 
 void
 Box::Read(nsTArray<uint8_t>* aDest)
 {
   aDest->SetLength(mRange.mEnd - mChildOffset);
   size_t bytes;
-  if (!mContext->mSource->ReadAt(mChildOffset, &(*aDest)[0], aDest->Length(),
-                                 &bytes) ||
+  if (!mContext->mSource->CachedReadAt(mChildOffset, &(*aDest)[0],
+                                       aDest->Length(), &bytes) ||
       bytes != aDest->Length()) {
     // Byte ranges are being reported incorrectly
     MOZ_ASSERT(false);
     aDest->Clear();
   }
 }
 }
--- a/media/libstagefright/binding/BufferStream.cpp
+++ b/media/libstagefright/binding/BufferStream.cpp
@@ -24,16 +24,23 @@ BufferStream::ReadAt(int64_t aOffset, vo
   }
   *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();
   return true;
 }
 
 /* virtual */ void
 BufferStream::DiscardBefore(int64_t aOffset)
--- a/media/libstagefright/binding/include/mp4_demuxer/BufferStream.h
+++ b/media/libstagefright/binding/include/mp4_demuxer/BufferStream.h
@@ -15,16 +15,18 @@ 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();
 
   virtual bool ReadAt(int64_t aOffset, void* aData, size_t aLength,
                       size_t* aBytesRead) MOZ_OVERRIDE;
+  virtual bool CachedReadAt(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();
 
--- a/media/libstagefright/binding/include/mp4_demuxer/mp4_demuxer.h
+++ b/media/libstagefright/binding/include/mp4_demuxer/mp4_demuxer.h
@@ -21,16 +21,18 @@ typedef int64_t Microseconds;
 
 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 CachedReadAt(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() {}
 };