Bug 1294634 - Use a switch statement to convert State to string. draft
authorJW Wang <jwwang@mozilla.com>
Fri, 12 Aug 2016 14:54:12 +0800
changeset 400552 e7a5dece41506e72b31b5afc8e70e6fac3e3124e
parent 400551 97ab77483fe79a217fce390803ac2f36749b8218
child 400554 a5e1c9c3ed026568ebfd19946eab92d837023bc9
push id26185
push userjwwang@mozilla.com
push dateMon, 15 Aug 2016 05:15:28 +0000
bugs1294634
milestone51.0a1
Bug 1294634 - Use a switch statement to convert State to string. MozReview-Commit-ID: 513zK0G0Pvi
dom/media/MediaDecoderStateMachine.cpp
dom/media/MediaDecoderStateMachine.h
--- a/dom/media/MediaDecoderStateMachine.cpp
+++ b/dom/media/MediaDecoderStateMachine.cpp
@@ -1033,36 +1033,49 @@ void MediaDecoderStateMachine::UpdatePla
   bool fragmentEnded = mFragmentEndTime >= 0 && GetMediaTime() >= mFragmentEndTime;
   mMetadataManager.DispatchMetadataIfNeeded(TimeUnit::FromMicroseconds(aTime));
 
   if (fragmentEnded) {
     StopPlayback();
   }
 }
 
-static const char* const gMachineStateStr[] = {
-  "DECODING_METADATA",
-  "WAIT_FOR_CDM",
-  "DORMANT",
-  "DECODING",
-  "SEEKING",
-  "BUFFERING",
-  "COMPLETED",
-  "SHUTDOWN",
-  "ERROR"
-};
+/* static */ const char*
+MediaDecoderStateMachine::ToStateStr(State aState)
+{
+  switch (aState) {
+    case DECODER_STATE_DECODING_METADATA: return "DECODING_METADATA";
+    case DECODER_STATE_WAIT_FOR_CDM:      return "WAIT_FOR_CDM";
+    case DECODER_STATE_DORMANT:           return "DORMANT";
+    case DECODER_STATE_DECODING:          return "DECODING";
+    case DECODER_STATE_SEEKING:           return "SEEKING";
+    case DECODER_STATE_BUFFERING:         return "BUFFERING";
+    case DECODER_STATE_COMPLETED:         return "COMPLETED";
+    case DECODER_STATE_SHUTDOWN:          return "SHUTDOWN";
+    case DECODER_STATE_ERROR:             return "ERROR";
+    default: MOZ_ASSERT_UNREACHABLE("Invalid state.");
+  }
+  return "UNKNOWN";
+}
+
+const char*
+MediaDecoderStateMachine::ToStateStr()
+{
+  MOZ_ASSERT(OnTaskQueue());
+  return ToStateStr(mState);
+}
 
 void MediaDecoderStateMachine::SetState(State aState)
 {
   MOZ_ASSERT(OnTaskQueue());
   if (mState == aState) {
     return;
   }
   DECODER_LOG("Change machine state from %s to %s",
-              gMachineStateStr[mState], gMachineStateStr[aState]);
+              ToStateStr(), ToStateStr(aState));
 
   mState = aState;
 
   mIsShutdown = mState == DECODER_STATE_ERROR || mState == DECODER_STATE_SHUTDOWN;
 
   // Clear state-scoped state.
   mSentPlaybackEndedEvent = false;
 }
@@ -2771,17 +2784,17 @@ MediaDecoderStateMachine::DumpDebugInfo(
   // this function before shutdown begins.
   nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction([this] () {
     DUMP_LOG(
       "GetMediaTime=%lld GetClock=%lld "
       "mState=%s mPlayState=%d mDecodingFirstFrame=%d IsPlaying=%d "
       "mAudioStatus=%s mVideoStatus=%s mDecodedAudioEndTime=%lld mDecodedVideoEndTime=%lld "
       "mIsAudioPrerolling=%d mIsVideoPrerolling=%d",
       GetMediaTime(), mMediaSink->IsStarted() ? GetClock() : -1,
-      gMachineStateStr[mState], mPlayState.Ref(), mDecodingFirstFrame, IsPlaying(),
+      ToStateStr(), mPlayState.Ref(), mDecodingFirstFrame, IsPlaying(),
       AudioRequestStatus(), VideoRequestStatus(), mDecodedAudioEndTime, mDecodedVideoEndTime,
       mIsAudioPrerolling, mIsVideoPrerolling);
   });
 
   OwnerThread()->DispatchStateChange(r.forget());
 }
 
 void MediaDecoderStateMachine::AddOutputStream(ProcessedMediaStream* aStream,
--- a/dom/media/MediaDecoderStateMachine.h
+++ b/dom/media/MediaDecoderStateMachine.h
@@ -248,16 +248,19 @@ public:
   // Immutable after construction - may be called on any thread.
   bool IsRealTime() const { return mRealTime; }
 
   size_t SizeOfVideoQueue() const;
 
   size_t SizeOfAudioQueue() const;
 
 private:
+  static const char* ToStateStr(State aState);
+  const char* ToStateStr();
+
   // Functions used by assertions to ensure we're calling things
   // on the appropriate threads.
   bool OnTaskQueue() const;
 
   // Initialization that needs to happen on the task queue. This is the first
   // task that gets run on the task queue, and is dispatched from the MDSM
   // constructor immediately after the task queue is created.
   void InitializationTask(MediaDecoder* aDecoder);