Bug 1221991 - [3.1] Manage module state via accessor functions. r=snorp
authorEugen Sawin <esawin@mozilla.com>
Fri, 06 Nov 2015 21:14:51 +0100
changeset 272990 b680d92e4afc3e27289f5efded9c95a2846db7f6
parent 272989 980de242783881b58ab6ab7612995a3e22970f0f
child 272991 439f35445959b812736f1b1d85a8e1856dea9163
push id68125
push useresawin@mozilla.com
push dateTue, 17 Nov 2015 19:50:49 +0000
treeherdermozilla-inbound@439f35445959 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssnorp
bugs1221991
milestone45.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 1221991 - [3.1] Manage module state via accessor functions. r=snorp
dom/media/platforms/android/AndroidDecoderModule.cpp
dom/media/platforms/android/AndroidDecoderModule.h
--- a/dom/media/platforms/android/AndroidDecoderModule.cpp
+++ b/dom/media/platforms/android/AndroidDecoderModule.cpp
@@ -411,19 +411,19 @@ MediaCodecDataDecoder::InitDecoder(Surfa
 }
 
 // This is in usec, so that's 10ms.
 static const int64_t kDecoderTimeout = 10000;
 
 #define BREAK_ON_DECODER_ERROR() \
   if (NS_FAILED(res)) { \
     NS_WARNING("Exiting decoder loop due to exception"); \
-    if (mState == kDrainDecoder) { \
+    if (State() == kDrainDecoder) { \
       INVOKE_CALLBACK(DrainComplete); \
-      mState = kDecoding; \
+      State(kDecoding); \
     } \
     INVOKE_CALLBACK(Error); \
     break; \
   }
 
 nsresult
 MediaCodecDataDecoder::GetInputBuffer(
     JNIEnv* aEnv, int aIndex, jni::Object::LocalRef* aBuffer)
@@ -447,42 +447,42 @@ MediaCodecDataDecoder::GetInputBuffer(
   return NS_ERROR_FAILURE;
 }
 
 bool
 MediaCodecDataDecoder::WaitForInput()
 {
   MonitorAutoLock lock(mMonitor);
 
-  while (mState == kDecoding && mQueue.empty()) {
+  while (State() == kDecoding && mQueue.empty()) {
     // Signal that we require more input.
     INVOKE_CALLBACK(InputExhausted);
     lock.Wait();
   }
 
-  return mState != kStopping;
+  return State() != kStopping;
 }
 
 
 MediaRawData*
 MediaCodecDataDecoder::PeekNextSample()
 {
   MonitorAutoLock lock(mMonitor);
 
-  if (mState == kFlushing) {
+  if (State() == kFlushing) {
     mDecoder->Flush();
     ClearQueue();
-    mState = kDecoding;
+    State(kDecoding);
     lock.Notify();
     return nullptr;
   }
 
   if (mQueue.empty()) {
-    if (mState == kDrainQueue) {
-      mState = kDrainDecoder;
+    if (State() == kDrainQueue) {
+      State(kDrainDecoder);
     }
     return nullptr;
   }
 
   // We're not stopping or flushing, so try to get a sample.
   return mQueue.front();
 }
 
@@ -538,29 +538,29 @@ MediaCodecDataDecoder::QueueEOS()
   res = mDecoder->DequeueInputBuffer(kDecoderTimeout, &inputIndex);
   if (NS_FAILED(res) || inputIndex < 0) {
     return res;
   }
 
   res = mDecoder->QueueInputBuffer(inputIndex, 0, 0, 0,
                                    MediaCodec::BUFFER_FLAG_END_OF_STREAM);
   if (NS_SUCCEEDED(res)) {
-    mState = kDrainWaitEOS;
+    State(kDrainWaitEOS);
     mMonitor.Notify();
   }
   return res;
 }
 
 void
 MediaCodecDataDecoder::HandleEOS(int32_t aOutputStatus)
 {
   MonitorAutoLock lock(mMonitor);
 
-  if (mState == kDrainWaitEOS) {
-    mState = kDecoding;
+  if (State() == kDrainWaitEOS) {
+    State(kDecoding);
     mMonitor.Notify();
 
     INVOKE_CALLBACK(DrainComplete);
   }
 
   mDecoder->ReleaseOutputBuffer(aOutputStatus, false);
 }
 
@@ -605,17 +605,17 @@ MediaCodecDataDecoder::DecoderLoop()
   MediaFormat::LocalRef outputFormat(frame.GetEnv());
   nsresult res = NS_OK;
 
   while (WaitForInput()) {
     sample = PeekNextSample();
 
     {
       MonitorAutoLock lock(mMonitor);
-      if (mState == kDrainDecoder) {
+      if (State() == kDrainDecoder) {
         MOZ_ASSERT(!sample, "Shouldn't have a sample when pushing EOF frame");
         res = QueueEOS();
         BREAK_ON_DECODER_ERROR();
       }
     }
 
     if (sample) {
       res = QueueSample(sample);
@@ -671,20 +671,38 @@ MediaCodecDataDecoder::DecoderLoop()
       BREAK_ON_DECODER_ERROR();
     }
   }
 
   Cleanup();
 
   // We're done.
   MonitorAutoLock lock(mMonitor);
-  mState = kShutdown;
+  State(kShutdown);
   mMonitor.Notify();
 }
 
+MediaCodecDataDecoder::ModuleState
+MediaCodecDataDecoder::State() const
+{
+  return mState;
+}
+
+void
+MediaCodecDataDecoder::State(ModuleState aState)
+{
+  if (aState == kDrainDecoder) {
+    MOZ_ASSERT(mState == kDrainQueue);
+  } else if (aState == kDrainWaitEOS) {
+    MOZ_ASSERT(mState == kDrainDecoder);
+  }
+
+  mState = aState;
+}
+
 void
 MediaCodecDataDecoder::ClearQueue()
 {
   mMonitor.AssertCurrentThreadOwns();
 
   while (!mQueue.empty()) {
     mQueue.pop();
   }
@@ -714,55 +732,55 @@ MediaCodecDataDecoder::ResetOutputBuffer
 {
   return mDecoder->GetOutputBuffers(ReturnTo(&mOutputBuffers));
 }
 
 nsresult
 MediaCodecDataDecoder::Flush()
 {
   MonitorAutoLock lock(mMonitor);
-  mState = kFlushing;
+  State(kFlushing);
   lock.Notify();
 
-  while (mState == kFlushing) {
+  while (State() == kFlushing) {
     lock.Wait();
   }
 
   return NS_OK;
 }
 
 nsresult
 MediaCodecDataDecoder::Drain()
 {
   MonitorAutoLock lock(mMonitor);
-  if (mState == kDrainDecoder || mState == kDrainQueue) {
+  if (State() == kDrainDecoder || State() == kDrainQueue) {
     return NS_OK;
   }
 
-  mState = kDrainQueue;
+  State(kDrainQueue);
   lock.Notify();
 
   return NS_OK;
 }
 
 
 nsresult
 MediaCodecDataDecoder::Shutdown()
 {
   MonitorAutoLock lock(mMonitor);
 
-  if (!mThread || mState == kStopping) {
+  if (!mThread || State() == kStopping) {
     // Already shutdown or in the process of doing so
     return NS_OK;
   }
 
-  mState = kStopping;
+  State(kStopping);
   lock.Notify();
 
-  while (mState == kStopping) {
+  while (State() == kStopping) {
     lock.Wait();
   }
 
   mThread->Shutdown();
   mThread = nullptr;
 
   mDecoder->Stop();
   mDecoder->Release();
--- a/dom/media/platforms/android/AndroidDecoderModule.h
+++ b/dom/media/platforms/android/AndroidDecoderModule.h
@@ -95,16 +95,18 @@ protected:
   MediaRawData* PeekNextSample();
   nsresult QueueSample(const MediaRawData* aSample);
   nsresult QueueEOS();
   void HandleEOS(int32_t aOutputStatus);
   media::TimeUnit GetOutputDuration();
   nsresult ProcessOutput(widget::sdk::BufferInfo::Param aInfo,
                          widget::sdk::MediaFormat::Param aFormat,
                          int32_t aStatus);
+  ModuleState State() const;
+  void State(ModuleState aState);
   void DecoderLoop();
 
   virtual void ClearQueue();
 
   MediaData::Type mType;
 
   nsAutoCString mMimeType;
   widget::sdk::MediaFormat::GlobalRef mFormat;
@@ -116,17 +118,17 @@ protected:
   jni::ObjectArray::GlobalRef mInputBuffers;
   jni::ObjectArray::GlobalRef mOutputBuffers;
 
   nsCOMPtr<nsIThread> mThread;
 
   // Only these members are protected by mMonitor.
   Monitor mMonitor;
 
-  State mState;
+  ModuleState mState;
 
   SampleQueue mQueue;
   // Durations are stored in microseconds.
   std::queue<media::TimeUnit> mDurations;
 };
 
 } // namespace mozilla