Bug 1355393. P2 - write the bytes out when we've collected a whole block. draft
authorJW Wang <jwwang@mozilla.com>
Tue, 11 Apr 2017 16:06:08 +0800
changeset 561711 efa44e59dbc02491d4bead5274c291cf2d0518a2
parent 561710 4eeaa9aea53cd99210146f9c67fec974ec712ef2
child 561712 329adb8a4414f32e2f83ba79af06d288d7b25705
push id53817
push userjwwang@mozilla.com
push dateThu, 13 Apr 2017 03:12:57 +0000
bugs1355393
milestone55.0a1
Bug 1355393. P2 - write the bytes out when we've collected a whole block. MozReview-Commit-ID: 3kAk235rZig
dom/media/MediaCache.cpp
--- a/dom/media/MediaCache.cpp
+++ b/dom/media/MediaCache.cpp
@@ -1781,46 +1781,35 @@ MediaCacheStream::NotifyDataReceived(int
             this, (long long)mChannelOffset, (long long)aSize));
 
   // We process the data one block (or part of a block) at a time
   while (size > 0) {
     uint32_t blockIndex = mChannelOffset/BLOCK_SIZE;
     int32_t blockOffset = int32_t(mChannelOffset - blockIndex*BLOCK_SIZE);
     int32_t chunkSize = std::min<int64_t>(BLOCK_SIZE - blockOffset, size);
 
-    // This gets set to something non-null if we have a whole block
-    // of data to write to the cache
-    const char* blockDataToStore = nullptr;
-    ReadMode mode = MODE_PLAYBACK;
-    if (blockOffset == 0 && chunkSize == BLOCK_SIZE) {
-      // We received a whole block, so avoid a useless copy through
-      // mPartialBlockBuffer
-      blockDataToStore = data;
+    if (blockOffset == 0) {
+      // We've just started filling this buffer so now is a good time
+      // to clear this flag.
+      mMetadataInPartialBlockBuffer = false;
+    }
+
+    ReadMode mode = mMetadataInPartialBlockBuffer
+      ? MODE_METADATA : MODE_PLAYBACK;
+
+    if (blockOffset + chunkSize == BLOCK_SIZE) {
+      // We have a whole block now to write it out.
+      auto data1 = MakeSpan<const uint8_t>(
+        reinterpret_cast<uint8_t*>(mPartialBlockBuffer.get()), blockOffset);
+      auto data2 = MakeSpan<const uint8_t>(
+        reinterpret_cast<const uint8_t*>(data), chunkSize);
+      gMediaCache->AllocateAndWriteBlock(this, mode, data1, data2);
     } else {
-      if (blockOffset == 0) {
-        // We've just started filling this buffer so now is a good time
-        // to clear this flag.
-        mMetadataInPartialBlockBuffer = false;
-      }
       memcpy(reinterpret_cast<char*>(mPartialBlockBuffer.get()) + blockOffset,
              data, chunkSize);
-
-      if (blockOffset + chunkSize == BLOCK_SIZE) {
-        // We completed a block, so lets write it out.
-        blockDataToStore = reinterpret_cast<char*>(mPartialBlockBuffer.get());
-        if (mMetadataInPartialBlockBuffer) {
-          mode = MODE_METADATA;
-        }
-      }
-    }
-
-    if (blockDataToStore) {
-      const uint8_t* p = reinterpret_cast<const uint8_t*>(blockDataToStore);
-      auto data = MakeSpan<const uint8_t>(p, BLOCK_SIZE);
-      gMediaCache->AllocateAndWriteBlock(this, mode, data);
     }
 
     mChannelOffset += chunkSize;
     size -= chunkSize;
     data += chunkSize;
   }
 
   MediaCache::ResourceStreamIterator iter(mResourceID);