Bug 1412737. P3 - remove the while loops in the callers of MediaCacheStream::Read(). r=bechen,gerald
☠☠ backed out by 3f797c2aedfb ☠ ☠
authorJW Wang <jwwang@mozilla.com>
Mon, 30 Oct 2017 12:44:24 +0800
changeset 441479 b8ae4f1e89c92e7f3b0a5f0335497d54fc848e27
parent 441478 dd35b8813ca1e7ad8cbafcde88435b1ddc62ce9a
child 441480 13b3569d56c4788967266f8f33a1e8860a92d8a5
push id8130
push userryanvm@gmail.com
push dateThu, 09 Nov 2017 00:28:20 +0000
treeherdermozilla-beta@a49a6fa54363 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbechen, gerald
bugs1412737
milestone58.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1412737. P3 - remove the while loops in the callers of MediaCacheStream::Read(). r=bechen,gerald MozReview-Commit-ID: 7UvmenWQ0Ah
dom/media/MediaResource.cpp
--- a/dom/media/MediaResource.cpp
+++ b/dom/media/MediaResource.cpp
@@ -409,83 +409,42 @@ MediaResourceIndex::CacheOrReadAt(int64_
 }
 
 nsresult
 MediaResourceIndex::UncachedReadAt(int64_t aOffset,
                                    char* aBuffer,
                                    uint32_t aCount,
                                    uint32_t* aBytes) const
 {
-  *aBytes = 0;
   if (aOffset < 0) {
     return NS_ERROR_ILLEGAL_VALUE;
   }
-  if (aCount != 0) {
-    for (;;) {
-      uint32_t bytesRead = 0;
-      nsresult rv = mResource->ReadAt(aOffset, aBuffer, aCount, &bytesRead);
-      if (NS_FAILED(rv)) {
-        return rv;
-      }
-      if (bytesRead == 0) {
-        break;
-      }
-      *aBytes += bytesRead;
-      aCount -= bytesRead;
-      if (aCount == 0) {
-        break;
-      }
-      aOffset += bytesRead;
-      if (aOffset < 0) {
-        // Very unlikely overflow.
-        return NS_ERROR_FAILURE;
-      }
-      aBuffer += bytesRead;
-    }
+  if (aCount == 0) {
+    *aBytes = 0;
+    return NS_OK;
   }
-  return NS_OK;
+  return mResource->ReadAt(aOffset, aBuffer, aCount, aBytes);
 }
 
 nsresult
 MediaResourceIndex::UncachedRangedReadAt(int64_t aOffset,
                                          char* aBuffer,
                                          uint32_t aRequestedCount,
                                          uint32_t aExtraCount,
                                          uint32_t* aBytes) const
 {
-  *aBytes = 0;
   uint32_t count = aRequestedCount + aExtraCount;
   if (aOffset < 0 || count < aRequestedCount) {
     return NS_ERROR_ILLEGAL_VALUE;
   }
-  if (count != 0) {
-    for (;;) {
-      uint32_t bytesRead = 0;
-      nsresult rv = mResource->ReadAt(aOffset, aBuffer, count, &bytesRead);
-      if (NS_FAILED(rv)) {
-        return rv;
-      }
-      if (bytesRead == 0) {
-        break;
-      }
-      *aBytes += bytesRead;
-      count -= bytesRead;
-      if (count <= aExtraCount) {
-        // We have read at least aRequestedCount, don't loop anymore.
-        break;
-      }
-      aOffset += bytesRead;
-      if (aOffset < 0) {
-        // Very unlikely overflow.
-        return NS_ERROR_FAILURE;
-      }
-      aBuffer += bytesRead;
-    }
+  if (count == 0) {
+    *aBytes = 0;
+    return NS_OK;
   }
-  return NS_OK;
+  return mResource->ReadAt(aOffset, aBuffer, count, aBytes);
 }
 
 nsresult
 MediaResourceIndex::Seek(int32_t aWhence, int64_t aOffset)
 {
   switch (aWhence) {
     case SEEK_SET:
       break;
@@ -511,40 +470,27 @@ MediaResourceIndex::Seek(int32_t aWhence
   mOffset = aOffset;
 
   return NS_OK;
 }
 
 already_AddRefed<MediaByteBuffer>
 MediaResourceIndex::MediaReadAt(int64_t aOffset, uint32_t aCount) const
 {
+  NS_ENSURE_TRUE(aOffset >= 0, nullptr);
   RefPtr<MediaByteBuffer> bytes = new MediaByteBuffer();
-  if (aOffset < 0) {
-    return bytes.forget();
-  }
   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 = mResource->ReadAt(aOffset, curr, aCount, &bytesRead);
-    NS_ENSURE_SUCCESS(rv, nullptr);
-    if (!bytesRead) {
-      break;
-    }
-    aOffset += bytesRead;
-    if (aOffset < 0) {
-      // Very unlikely overflow.
-      break;
-    }
-    aCount -= bytesRead;
-    curr += bytesRead;
-  }
-  bytes->SetLength(curr - start);
+
+  uint32_t bytesRead = 0;
+  nsresult rv = mResource->ReadAt(
+    aOffset, reinterpret_cast<char*>(bytes->Elements()), aCount, &bytesRead);
+  NS_ENSURE_SUCCESS(rv, nullptr);
+
+  bytes->SetLength(bytesRead);
   return bytes.forget();
 }
 
 already_AddRefed<MediaByteBuffer>
 MediaResourceIndex::CachedMediaReadAt(int64_t aOffset, uint32_t aCount) const
 {
   RefPtr<MediaByteBuffer> bytes = new MediaByteBuffer();
   bool ok = bytes->SetLength(aCount, fallible);