Bug 1373577. P1 - devritualize MediaResource::MediaReadAt(). r=jya
authorJW Wang <jwwang@mozilla.com>
Thu, 15 Jun 2017 17:17:08 +0800
changeset 596522 0eacbd673e5af7ecb45ff5037e5c8bd70e0537d8
parent 596521 064975f358bc7efe7a6da2fcb1583e0ba1f65698
child 596523 5f983c055532daa35bffc6347661427e407eb4fd
push id64675
push usermak77@bonardo.net
push dateMon, 19 Jun 2017 12:08:05 +0000
reviewersjya
bugs1373577
milestone56.0a1
Bug 1373577. P1 - devritualize MediaResource::MediaReadAt(). r=jya The difference is the sub-class override doesn't call DispatchBytesConsumed(). It doesn't make sense not to call that after you do consume some data. For callers that want silent reads, they should call ReadFromCache() instead. MozReview-Commit-ID: J3dn8qgWKoL
dom/media/MediaResource.cpp
dom/media/MediaResource.h
--- a/dom/media/MediaResource.cpp
+++ b/dom/media/MediaResource.cpp
@@ -720,41 +720,16 @@ nsresult ChannelMediaResource::ReadAt(in
 
   nsresult rv = mCacheStream.ReadAt(aOffset, aBuffer, aCount, aBytes);
   if (NS_SUCCEEDED(rv)) {
     DispatchBytesConsumed(*aBytes, aOffset);
   }
   return rv;
 }
 
-already_AddRefed<MediaByteBuffer>
-ChannelMediaResource::MediaReadAt(int64_t aOffset, uint32_t aCount)
-{
-  NS_ASSERTION(!NS_IsMainThread(), "Don't call on main thread");
-
-  RefPtr<MediaByteBuffer> bytes = new MediaByteBuffer();
-  bool ok = bytes->SetLength(aCount, fallible);
-  NS_ENSURE_TRUE(ok, nullptr);
-  char* curr = reinterpret_cast<char*>(bytes->Elements());
-  const char* start = curr;
-  while (aCount > 0) {
-    uint32_t bytesRead;
-    nsresult rv = mCacheStream.ReadAt(aOffset, curr, aCount, &bytesRead);
-    NS_ENSURE_SUCCESS(rv, nullptr);
-    if (!bytesRead) {
-      break;
-    }
-    aOffset += bytesRead;
-    aCount -= bytesRead;
-    curr += bytesRead;
-  }
-  bytes->SetLength(curr - start);
-  return bytes.forget();
-}
-
 void
 ChannelMediaResource::ThrottleReadahead(bool bThrottle)
 {
   mCacheStream.ThrottleReadahead(bThrottle);
 }
 
 int64_t ChannelMediaResource::Tell()
 {
@@ -1189,17 +1164,16 @@ public:
 
   // Other thread
   void     SetReadMode(MediaCacheStream::ReadMode aMode) override {}
   void     SetPlaybackRate(uint32_t aBytesPerSecond) override {}
   nsresult ReadAt(int64_t aOffset, char* aBuffer,
                   uint32_t aCount, uint32_t* aBytes) override;
   // (Probably) file-based, caching recommended.
   bool ShouldCacheReads() override { return true; }
-  already_AddRefed<MediaByteBuffer> MediaReadAt(int64_t aOffset, uint32_t aCount) override;
   int64_t  Tell() override;
 
   // Any thread
   void    Pin() override {}
   void    Unpin() override {}
   double  GetDownloadRate(bool* aIsReliable) override
   {
     // The data's all already here
@@ -1480,25 +1454,16 @@ nsresult FileMediaResource::ReadAt(int64
   }
   if (NS_SUCCEEDED(rv)) {
     DispatchBytesConsumed(*aBytes, aOffset);
   }
   return rv;
 }
 
 already_AddRefed<MediaByteBuffer>
-FileMediaResource::MediaReadAt(int64_t aOffset, uint32_t aCount)
-{
-  NS_ASSERTION(!NS_IsMainThread(), "Don't call on main thread");
-
-  MutexAutoLock lock(mLock);
-  return UnsafeMediaReadAt(aOffset, aCount);
-}
-
-already_AddRefed<MediaByteBuffer>
 FileMediaResource::UnsafeMediaReadAt(int64_t aOffset, uint32_t aCount)
 {
   RefPtr<MediaByteBuffer> bytes = new MediaByteBuffer();
   bool ok = bytes->SetLength(aCount, fallible);
   NS_ENSURE_TRUE(ok, nullptr);
   nsresult rv = UnsafeSeek(nsISeekableStream::NS_SEEK_SET, aOffset);
   NS_ENSURE_SUCCESS(rv, nullptr);
   char* curr = reinterpret_cast<char*>(bytes->Elements());
--- a/dom/media/MediaResource.h
+++ b/dom/media/MediaResource.h
@@ -205,17 +205,17 @@ public:
   // E.g. Caching lockless and memory-based MediaResource subclasses would be a
   // waste, but caching lock/IO-bound resources means reducing the impact of
   // each read.
   virtual bool ShouldCacheReads() = 0;
   // This method returns nullptr if anything fails.
   // Otherwise, it returns an owned buffer.
   // MediaReadAt may return fewer bytes than requested if end of stream is
   // encountered. There is no need to call it again to get more data.
-  virtual already_AddRefed<MediaByteBuffer> MediaReadAt(int64_t aOffset, uint32_t aCount)
+  already_AddRefed<MediaByteBuffer> MediaReadAt(int64_t aOffset, uint32_t aCount)
   {
     RefPtr<MediaByteBuffer> bytes = new MediaByteBuffer();
     bool ok = bytes->SetLength(aCount, fallible);
     NS_ENSURE_TRUE(ok, nullptr);
     char* curr = reinterpret_cast<char*>(bytes->Elements());
     const char* start = curr;
     while (aCount > 0) {
       uint32_t bytesRead;
@@ -580,17 +580,16 @@ public:
 
   // Other thread
   void     SetReadMode(MediaCacheStream::ReadMode aMode) override;
   void     SetPlaybackRate(uint32_t aBytesPerSecond) override;
   nsresult ReadAt(int64_t offset, char* aBuffer,
                   uint32_t aCount, uint32_t* aBytes) override;
   // Data stored in IO&lock-encumbered MediaCacheStream, caching recommended.
   bool ShouldCacheReads() override { return true; }
-  already_AddRefed<MediaByteBuffer> MediaReadAt(int64_t aOffset, uint32_t aCount) override;
   int64_t Tell() override;
 
   // Any thread
   void    Pin() override;
   void    Unpin() override;
   double  GetDownloadRate(bool* aIsReliable) override;
   int64_t GetLength() override;
   int64_t GetNextCachedData(int64_t aOffset) override;