Bug 1457427 - Create a new GraphDriver method which performs the switching to next driver. r=padenot
authorAlex Chronopoulos <achronop@gmail.com>
Mon, 07 May 2018 19:35:56 +0200
changeset 471466 1b0e226380452fd9aa86d2fe1be3cde82d48283f
parent 471465 b8edee5bd7d252159791b1bd964db5bb1cb6f5c5
child 471467 dc1bf6cad8ce1d6efac4ff9a067c35a2f357dbdd
push id9374
push userjlund@mozilla.com
push dateMon, 18 Jun 2018 21:43:20 +0000
treeherdermozilla-beta@160e085dfb0b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspadenot
bugs1457427
milestone62.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 1457427 - Create a new GraphDriver method which performs the switching to next driver. r=padenot Restructuring of the existing code no functional change. MozReview-Commit-ID: JWaa8LgG4MR
dom/media/GraphDriver.cpp
dom/media/GraphDriver.h
--- a/dom/media/GraphDriver.cpp
+++ b/dom/media/GraphDriver.cpp
@@ -93,16 +93,26 @@ void GraphDriver::EnsureNextIteration()
 }
 
 bool GraphDriver::Switching()
 {
   GraphImpl()->GetMonitor().AssertCurrentThreadOwns();
   return mNextDriver || mPreviousDriver;
 }
 
+void GraphDriver::SwitchToNextDriver()
+{
+  GraphImpl()->GetMonitor().AssertCurrentThreadOwns();
+  MOZ_ASSERT(NextDriver());
+  NextDriver()->SetGraphTime(this, mIterationStart, mIterationEnd);
+  GraphImpl()->SetCurrentDriver(NextDriver());
+  NextDriver()->Start();
+  SetNextDriver(nullptr);
+}
+
 GraphDriver* GraphDriver::NextDriver()
 {
   GraphImpl()->GetMonitor().AssertCurrentThreadOwns();
   return mNextDriver;
 }
 
 GraphDriver* GraphDriver::PreviousDriver()
 {
@@ -221,19 +231,17 @@ ThreadedDriver::Revive()
 {
   // Note: only called on MainThread, without monitor
   // We know were weren't in a running state
   LOG(LogLevel::Debug, ("AudioCallbackDriver reviving."));
   // If we were switching, switch now. Otherwise, tell thread to run the main
   // loop again.
   MonitorAutoLock mon(mGraphImpl->GetMonitor());
   if (NextDriver()) {
-    NextDriver()->SetGraphTime(this, mIterationStart, mIterationEnd);
-    mGraphImpl->SetCurrentDriver(NextDriver());
-    NextDriver()->Start();
+    SwitchToNextDriver();
   } else {
     nsCOMPtr<nsIRunnable> event = new MediaStreamGraphInitThreadRunnable(this);
     mThread->EventTarget()->Dispatch(event.forget(), NS_DISPATCH_NORMAL);
   }
 }
 
 void
 ThreadedDriver::Shutdown()
@@ -319,19 +327,17 @@ ThreadedDriver::RunThread()
       // Enter shutdown mode. The stable-state handler will detect this
       // and complete shutdown if the graph does not get restarted.
       mGraphImpl->SignalMainThreadCleanup();
       return;
     }
     MonitorAutoLock lock(GraphImpl()->GetMonitor());
     if (NextDriver()) {
       LOG(LogLevel::Debug, ("Switching to AudioCallbackDriver"));
-      NextDriver()->SetGraphTime(this, mIterationStart, mIterationEnd);
-      mGraphImpl->SetCurrentDriver(NextDriver());
-      NextDriver()->Start();
+      SwitchToNextDriver();
       return;
     }
   }
 }
 
 MediaTime
 SystemClockDriver::GetIntervalForIteration()
 {
@@ -773,19 +779,17 @@ AudioCallbackDriver::Revive()
 {
   // Note: only called on MainThread, without monitor
   // We know were weren't in a running state
   LOG(LogLevel::Debug, ("AudioCallbackDriver reviving."));
   // If we were switching, switch now. Otherwise, start the audio thread again.
   MonitorAutoLock mon(mGraphImpl->GetMonitor());
   if (NextDriver()) {
     RemoveCallback();
-    NextDriver()->SetGraphTime(this, mIterationStart, mIterationEnd);
-    mGraphImpl->SetCurrentDriver(NextDriver());
-    NextDriver()->Start();
+    SwitchToNextDriver();
   } else {
     LOG(LogLevel::Debug,
         ("Starting audio threads for MediaStreamGraph %p from a new thread.",
          mGraphImpl));
     RefPtr<AsyncCubebTask> initEvent =
       new AsyncCubebTask(this, AsyncCubebOperation::INIT);
     initEvent->Dispatch();
   }
@@ -1015,19 +1019,17 @@ AudioCallbackDriver::DataCallback(const 
     // If the audio stream has not been started by the previous driver or
     // the graph itself, keep it alive.
     MonitorAutoLock mon(mGraphImpl->GetMonitor());
     if (!IsStarted()) {
       return aFrames;
     }
     LOG(LogLevel::Debug, ("Switching to system driver."));
     RemoveCallback();
-    NextDriver()->SetGraphTime(this, mIterationStart, mIterationEnd);
-    mGraphImpl->SetCurrentDriver(NextDriver());
-    NextDriver()->Start();
+    SwitchToNextDriver();
     // Returning less than aFrames starts the draining and eventually stops the
     // audio thread. This function will never get called again.
     return aFrames - 1;
   }
 
   return aFrames;
 }
 
@@ -1183,22 +1185,20 @@ void AudioCallbackDriver::CompleteAudioC
     mPromisesForOperation.AppendElements(array);
   }
 }
 
 void AudioCallbackDriver::FallbackToSystemClockDriver()
 {
   GraphImpl()->GetMonitor().AssertCurrentThreadOwns();
   SystemClockDriver* nextDriver = new SystemClockDriver(GraphImpl());
+  nextDriver->MarkAsFallback();
   SetNextDriver(nextDriver);
-  nextDriver->MarkAsFallback();
-  nextDriver->SetGraphTime(this, mIterationStart, mIterationEnd);
   // We're not using SwitchAtNextIteration here, because there
   // won't be a next iteration if we don't restart things manually:
   // the audio stream just signaled that it's in error state.
-  mGraphImpl->SetCurrentDriver(nextDriver);
-  nextDriver->Start();
+  SwitchToNextDriver();
 }
 
 } // namespace mozilla
 
 // avoid redefined macro in unified build
 #undef LOG
--- a/dom/media/GraphDriver.h
+++ b/dom/media/GraphDriver.h
@@ -130,16 +130,18 @@ public:
    * how often we want to wakeup/how much we want to process per iteration), or
    * it can be indirectly set by the latency of the audio backend, and the
    * number of buffers of this audio backend: say we have four buffers, and 40ms
    * latency, we will get a callback approximately every 10ms. */
   virtual uint32_t IterationDuration() = 0;
 
   /* Return whether we are switching or not. */
   bool Switching();
+  /* Implement the switching of the driver and the necessary updates */
+  void SwitchToNextDriver();
 
   // Those are simply or setting the associated pointer, but assert that the
   // lock is held.
   GraphDriver* NextDriver();
   GraphDriver* PreviousDriver();
   void SetNextDriver(GraphDriver* aNextDriver);
   void SetPreviousDriver(GraphDriver* aPreviousDriver);