Bug 1401471. P1 - make StreamAction a struct so we can associate data with each action in the future. draft
authorJW Wang <jwwang@mozilla.com>
Wed, 20 Sep 2017 16:12:31 +0800
changeset 668973 57e1b54c03c45af12fc66eb03e4614d6a3df486e
parent 668293 b05a90254c021dc899f35e9d14afc9d83923987a
child 668974 955887fe04b5ded1b75923572c6b39ff455679f4
push id81174
push userjwwang@mozilla.com
push dateFri, 22 Sep 2017 09:08:50 +0000
bugs1401471
milestone57.0a1
Bug 1401471. P1 - make StreamAction a struct so we can associate data with each action in the future. MozReview-Commit-ID: A0ZpunJgNYm
dom/media/MediaCache.cpp
--- a/dom/media/MediaCache.cpp
+++ b/dom/media/MediaCache.cpp
@@ -1118,23 +1118,33 @@ MediaCache::PredictNextUseForIncomingDat
   }
   if (bytesAhead <= 0)
     return TimeDuration(0);
   int64_t millisecondsAhead = bytesAhead*1000/aStream->mPlaybackBytesPerSecond;
   return TimeDuration::FromMilliseconds(
       std::min<int64_t>(millisecondsAhead, INT32_MAX));
 }
 
-enum StreamAction { NONE, SEEK, SEEK_AND_RESUME, RESUME, SUSPEND };
-
 void
 MediaCache::Update()
 {
   NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
 
+  struct StreamAction
+  {
+    enum
+    {
+      NONE,
+      SEEK,
+      SEEK_AND_RESUME,
+      RESUME,
+      SUSPEND
+    } mTag = NONE;
+  };
+
   // The action to use for each stream. We store these so we can make
   // decisions while holding the cache lock but implement those decisions
   // without holding the cache lock, since we need to call out to
   // stream, decoder and element code.
   AutoTArray<StreamAction,10> actions;
 
   {
     ReentrantMonitorAutoEnter mon(mReentrantMonitor);
@@ -1245,17 +1255,17 @@ MediaCache::Update()
         latestNextUse = PredictNextUse(now, reusableBlock);
       }
     }
 
     int32_t resumeThreshold = Preferences::GetInt("media.cache_resume_threshold", 10);
     int32_t readaheadLimit = Preferences::GetInt("media.cache_readahead_limit", 30);
 
     for (uint32_t i = 0; i < mStreams.Length(); ++i) {
-      actions.AppendElement(NONE);
+      actions.AppendElement(StreamAction{});
 
       MediaCacheStream* stream = mStreams[i];
       if (stream->mClosed) {
         LOG("Stream %p closed", stream);
         continue;
       }
 
       // Figure out where we should be reading from. It's the first
@@ -1383,25 +1393,27 @@ MediaCache::Update()
         // We need to seek now.
         NS_ASSERTION(stream->mIsTransportSeekable || desiredOffset == 0,
                      "Trying to seek in a non-seekable stream!");
         // Round seek offset down to the start of the block. This is essential
         // because we don't want to think we have part of a block already
         // in mPartialBlockBuffer.
         stream->mChannelOffset =
           OffsetToBlockIndexUnchecked(desiredOffset) * BLOCK_SIZE;
-        actions[i] = stream->mCacheSuspended ? SEEK_AND_RESUME : SEEK;
+        actions[i].mTag = stream->mCacheSuspended
+                            ? StreamAction::SEEK_AND_RESUME
+                            : StreamAction::SEEK;
         // mChannelOffset is updated to a new position. We don't want data from
         // the old channel to be written to the wrong position. 0 is a sentinel
         // value which will not match any ID passed to NotifyDataReceived().
         stream->mLoadID = 0;
       } else if (enableReading && stream->mCacheSuspended) {
-        actions[i] = RESUME;
+        actions[i].mTag = StreamAction::RESUME;
       } else if (!enableReading && !stream->mCacheSuspended) {
-        actions[i] = SUSPEND;
+        actions[i].mTag = StreamAction::SUSPEND;
       }
     }
 #ifdef DEBUG
     mInUpdate = false;
 #endif
   }
 
   // Update the channel state without holding our cache lock. While we're
@@ -1412,57 +1424,60 @@ MediaCache::Update()
   // the action, which can only be written by the main thread (i.e., this
   // thread), so we don't have races here.
 
   // First, update the mCacheSuspended/mCacheEnded flags so that they're all correct
   // when we fire our CacheClient commands below. Those commands can rely on these flags
   // being set correctly for all streams.
   for (uint32_t i = 0; i < mStreams.Length(); ++i) {
     MediaCacheStream* stream = mStreams[i];
-    switch (actions[i]) {
-    case SEEK:
-	case SEEK_AND_RESUME:
-      stream->mCacheSuspended = false;
-      stream->mChannelEnded = false;
-      break;
-    case RESUME:
-      stream->mCacheSuspended = false;
-      break;
-    case SUSPEND:
-      stream->mCacheSuspended = true;
-      break;
-    default:
-      break;
+    switch (actions[i].mTag) {
+      case StreamAction::SEEK:
+      case StreamAction::SEEK_AND_RESUME:
+        stream->mCacheSuspended = false;
+        stream->mChannelEnded = false;
+        break;
+      case StreamAction::RESUME:
+        stream->mCacheSuspended = false;
+        break;
+      case StreamAction::SUSPEND:
+        stream->mCacheSuspended = true;
+        break;
+      default:
+        break;
     }
   }
 
   for (uint32_t i = 0; i < mStreams.Length(); ++i) {
     MediaCacheStream* stream = mStreams[i];
     nsresult rv;
-    switch (actions[i]) {
-    case SEEK:
-	case SEEK_AND_RESUME:
-      LOG("Stream %p CacheSeek to %" PRId64 " (resume=%d)", stream,
-          stream->mChannelOffset, actions[i] == SEEK_AND_RESUME);
-      rv = stream->mClient->CacheClientSeek(stream->mChannelOffset,
-                                            actions[i] == SEEK_AND_RESUME);
-      break;
-    case RESUME:
-      LOG("Stream %p Resumed", stream);
-      rv = stream->mClient->CacheClientResume();
-      QueueSuspendedStatusUpdate(stream->mResourceID);
-      break;
-    case SUSPEND:
-      LOG("Stream %p Suspended", stream);
-      rv = stream->mClient->CacheClientSuspend();
-      QueueSuspendedStatusUpdate(stream->mResourceID);
-      break;
-    default:
-      rv = NS_OK;
-      break;
+    switch (actions[i].mTag) {
+      case StreamAction::SEEK:
+      case StreamAction::SEEK_AND_RESUME:
+        LOG("Stream %p CacheSeek to %" PRId64 " (resume=%d)",
+            stream,
+            stream->mChannelOffset,
+            actions[i].mTag == StreamAction::SEEK_AND_RESUME);
+        rv = stream->mClient->CacheClientSeek(stream->mChannelOffset,
+                                              actions[i].mTag ==
+                                                StreamAction::SEEK_AND_RESUME);
+        break;
+      case StreamAction::RESUME:
+        LOG("Stream %p Resumed", stream);
+        rv = stream->mClient->CacheClientResume();
+        QueueSuspendedStatusUpdate(stream->mResourceID);
+        break;
+      case StreamAction::SUSPEND:
+        LOG("Stream %p Suspended", stream);
+        rv = stream->mClient->CacheClientSuspend();
+        QueueSuspendedStatusUpdate(stream->mResourceID);
+        break;
+      default:
+        rv = NS_OK;
+        break;
     }
 
     if (NS_FAILED(rv)) {
       // Close the streams that failed due to error. This will cause all
       // client Read and Seek operations on those streams to fail. Blocked
       // Reads will also be woken up.
       stream->mClient->Close();
     }