Bug 1374173 - Make MediaCacheStream::Seek and Read internal - r?jwwang draft
authorGerald Squelart <gsquelart@mozilla.com>
Tue, 13 Jun 2017 15:57:46 +1200
changeset 596336 aceddfeddadbb5db6b91a73aa03d9a9a10799be2
parent 596219 95543bdc59bd038a3d5d084b85a4fec493c349ee
child 596337 b67a0f0d7b89c261c9771a6537a8031998fa5252
push id64581
push usergsquelart@mozilla.com
push dateMon, 19 Jun 2017 04:51:32 +0000
reviewersjwwang
bugs1374173
milestone56.0a1
Bug 1374173 - Make MediaCacheStream::Seek and Read internal - r?jwwang MozReview-Commit-ID: 9tPETuUYDrV
dom/media/MediaCache.cpp
dom/media/MediaCache.h
--- a/dom/media/MediaCache.cpp
+++ b/dom/media/MediaCache.cpp
@@ -2238,21 +2238,20 @@ MediaCacheStream::SetPlaybackRate(uint32
   ReentrantMonitorAutoEnter mon(mMediaCache->GetReentrantMonitor());
   if (aBytesPerSecond == mPlaybackBytesPerSecond)
     return;
   mPlaybackBytesPerSecond = aBytesPerSecond;
   mMediaCache->QueueUpdate();
 }
 
 nsresult
-MediaCacheStream::Seek(int32_t aWhence, int64_t aOffset)
+MediaCacheStream::SeekInternal(int32_t aWhence, int64_t aOffset)
 {
-  NS_ASSERTION(!NS_IsMainThread(), "Don't call on main thread");
+  mMediaCache->GetReentrantMonitor().AssertCurrentThreadIn();
 
-  ReentrantMonitorAutoEnter mon(mMediaCache->GetReentrantMonitor());
   if (mClosed)
     return NS_ERROR_FAILURE;
 
   int64_t oldOffset = mStreamOffset;
   int64_t newOffset = mStreamOffset;
   switch (aWhence) {
   case PR_SEEK_END:
     if (mStreamLength < 0)
@@ -2296,21 +2295,20 @@ MediaCacheStream::ThrottleReadahead(bool
 int64_t
 MediaCacheStream::Tell()
 {
   ReentrantMonitorAutoEnter mon(mMediaCache->GetReentrantMonitor());
   return mStreamOffset;
 }
 
 nsresult
-MediaCacheStream::Read(char* aBuffer, uint32_t aCount, uint32_t* aBytes)
+MediaCacheStream::ReadInternal(char* aBuffer, uint32_t aCount, uint32_t* aBytes)
 {
-  NS_ASSERTION(!NS_IsMainThread(), "Don't call on main thread");
+  mMediaCache->GetReentrantMonitor().AssertCurrentThreadIn();
 
-  ReentrantMonitorAutoEnter mon(mMediaCache->GetReentrantMonitor());
   if (mClosed)
     return NS_ERROR_FAILURE;
 
   // Cache the offset in case it is changed again when we are waiting for the
   // monitor to be notified to avoid reading at the wrong position.
   auto streamOffset = mStreamOffset;
 
   uint32_t count = 0;
@@ -2369,17 +2367,17 @@ MediaCacheStream::Read(char* aBuffer, ui
           streamWithPartialBlock->mMetadataInPartialBlockBuffer = true;
         }
         streamOffset += bytes;
         count = bytes;
         break;
       }
 
       // No data has been read yet, so block
-      mon.Wait();
+      mMediaCache->GetReentrantMonitor().Wait();
       if (mClosed) {
         // We may have successfully read some data, but let's just throw
         // that out.
         return NS_ERROR_FAILURE;
       }
       continue;
     }
 
@@ -2414,19 +2412,19 @@ MediaCacheStream::Read(char* aBuffer, ui
 
 nsresult
 MediaCacheStream::ReadAt(int64_t aOffset, char* aBuffer,
                          uint32_t aCount, uint32_t* aBytes)
 {
   NS_ASSERTION(!NS_IsMainThread(), "Don't call on main thread");
 
   ReentrantMonitorAutoEnter mon(mMediaCache->GetReentrantMonitor());
-  nsresult rv = Seek(nsISeekableStream::NS_SEEK_SET, aOffset);
+  nsresult rv = SeekInternal(nsISeekableStream::NS_SEEK_SET, aOffset);
   if (NS_FAILED(rv)) return rv;
-  return Read(aBuffer, aCount, aBytes);
+  return ReadInternal(aBuffer, aCount, aBytes);
 }
 
 nsresult
 MediaCacheStream::ReadFromCache(char* aBuffer, int64_t aOffset, int64_t aCount)
 {
   ReentrantMonitorAutoEnter mon(mMediaCache->GetReentrantMonitor());
 
   // Read one block (or part of a block) at a time
--- a/dom/media/MediaCache.h
+++ b/dom/media/MediaCache.h
@@ -330,27 +330,22 @@ public:
 
   // Returns true when all streams for this resource are suspended or their
   // channel has ended.
   bool AreAllStreamsForResourceSuspended();
 
   // These methods must be called on a different thread from the main
   // thread. They should always be called on the same thread for a given
   // stream.
-  // This can fail when aWhence is NS_SEEK_END and no stream length
-  // is known.
-  nsresult Seek(int32_t aWhence, int64_t aOffset);
   int64_t Tell();
+  // Seeks to aOffset in the stream then performs a Read operation.
   // *aBytes gets the number of bytes that were actually read. This can
   // be less than aCount. If the first byte of data is not in the cache,
   // this will block until the data is available or the stream is
   // closed, otherwise it won't block.
-  nsresult Read(char* aBuffer, uint32_t aCount, uint32_t* aBytes);
-  // Seeks to aOffset in the stream then performs a Read operation. See
-  // 'Read' for argument and return details.
   nsresult ReadAt(int64_t aOffset, char* aBuffer,
                   uint32_t aCount, uint32_t* aBytes);
 
   void ThrottleReadahead(bool bThrottle);
 
   size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const;
 
 private:
@@ -434,16 +429,25 @@ private:
   // that the cache monitor is held. Main thread only.
   // aReentrantMonitor is the nsAutoReentrantMonitor wrapper holding the cache monitor.
   // This is used to NotifyAll to wake up threads that might be
   // blocked on reading from this stream.
   void CloseInternal(ReentrantMonitorAutoEnter& aReentrantMonitor);
   // Update mPrincipal given that data has been received from aPrincipal
   bool UpdatePrincipal(nsIPrincipal* aPrincipal);
 
+  // This can fail when aWhence is NS_SEEK_END and no stream length
+  // is known.
+  nsresult SeekInternal(int32_t aWhence, int64_t aOffset);
+  // *aBytes gets the number of bytes that were actually read. This can
+  // be less than aCount. If the first byte of data is not in the cache,
+  // this will block until the data is available or the stream is
+  // closed, otherwise it won't block.
+  nsresult ReadInternal(char* aBuffer, uint32_t aCount, uint32_t* aBytes);
+
   // Instance of MediaCache to use with this MediaCacheStream.
   RefPtr<MediaCache> mMediaCache;
 
   // These fields are main-thread-only.
   ChannelMediaResource*  mClient;
   nsCOMPtr<nsIPrincipal> mPrincipal;
   // Set to true when MediaCache::Update() has finished while this stream
   // was present.