Bug 1264195: Re-add SetMicrophoneActive() calls into the GraphDriver r=padenot
authorRandell Jesup <rjesup@jesup.org>
Wed, 13 Apr 2016 14:31:35 -0400
changeset 316870 3f87c987333bc59831fe9a3142abbad591a9f3f9
parent 316869 1442d37b64495edacde3e4e0aba8e6e627adb966
child 316871 3a5d44c806e50e4be1cf7a3d2e674c2faa632b88
push id9480
push userjlund@mozilla.com
push dateMon, 25 Apr 2016 17:12:58 +0000
treeherdermozilla-aurora@0d6a91c76a9e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspadenot
bugs1264195
milestone48.0a1
Bug 1264195: Re-add SetMicrophoneActive() calls into the GraphDriver r=padenot With bustage fix for warnings as errors
dom/media/GraphDriver.cpp
dom/media/GraphDriver.h
dom/media/MediaStreamGraph.cpp
--- a/dom/media/GraphDriver.cpp
+++ b/dom/media/GraphDriver.cpp
@@ -556,18 +556,18 @@ AudioCallbackDriver::AudioCallbackDriver
   , mSampleRate(0)
   , mInputChannels(1)
   , mIterationDurationMS(MEDIA_GRAPH_TARGET_PERIOD_MS)
   , mStarted(false)
   , mAudioInput(nullptr)
   , mAudioChannel(aGraphImpl->AudioChannel())
   , mAddedMixer(false)
   , mInCallback(false)
+#ifdef XP_MACOSX
   , mMicrophoneActive(false)
-#ifdef XP_MACOSX
   , mCallbackReceivedWhileSwitching(0)
 #endif
 {
   STREAM_LOG(LogLevel::Debug, ("AudioCallbackDriver ctor for graph %p", aGraphImpl));
 }
 
 AudioCallbackDriver::~AudioCallbackDriver()
 {
@@ -655,16 +655,23 @@ AudioCallbackDriver::Init()
       MonitorAutoLock lock(GraphImpl()->GetMonitor());
       SetNextDriver(new SystemClockDriver(GraphImpl()));
       NextDriver()->SetGraphTime(this, mIterationStart, mIterationEnd);
       mGraphImpl->SetCurrentDriver(NextDriver());
       NextDriver()->Start();
       return;
     }
   }
+#ifdef XP_MACOSX
+  // Currently, only mac cares about this
+  bool aec;
+  Unused << mGraphImpl->AudioTrackPresent(aec);
+  SetMicrophoneActive(aec);
+#endif
+
   cubeb_stream_register_device_changed_callback(mAudioStream,
                                                 AudioCallbackDriver::DeviceChangedCallback_s);
 
   StartStream();
 
   STREAM_LOG(LogLevel::Debug, ("AudioCallbackDriver started."));
 }
 
@@ -1058,25 +1065,26 @@ void AudioCallbackDriver::PanOutputIfNee
       cubeb_stream_device_destroy(mAudioStream, out);
     }
   }
 #endif
 }
 
 void
 AudioCallbackDriver::DeviceChangedCallback() {
+#ifdef XP_MACOSX
   MonitorAutoLock mon(mGraphImpl->GetMonitor());
   PanOutputIfNeeded(mMicrophoneActive);
   // On OSX, changing the output device causes the audio thread to no call the
   // audio callback, so we're unable to process real-time input data, and this
   // results in latency building up.
   // We switch to a system driver until audio callbacks are called again, so we
   // still pull from the input stream, so that everything works apart from the
   // audio output.
-#ifdef XP_MACOSX
+
   // Don't bother doing the device switching dance if the graph is not RUNNING
   // (starting up, shutting down), because we haven't started pulling from the
   // SourceMediaStream.
   if (!GraphImpl()->Running()) {
     return;
   }
 
   if (mSelfReference) {
@@ -1091,21 +1099,23 @@ AudioCallbackDriver::DeviceChangedCallba
   mGraphImpl->SetCurrentDriver(mNextDriver);
   mNextDriver->Start();
 #endif
 }
 
 void
 AudioCallbackDriver::SetMicrophoneActive(bool aActive)
 {
+#ifdef XP_MACOSX
   MonitorAutoLock mon(mGraphImpl->GetMonitor());
 
   mMicrophoneActive = aActive;
 
   PanOutputIfNeeded(mMicrophoneActive);
+#endif
 }
 
 uint32_t
 AudioCallbackDriver::IterationDuration()
 {
   // The real fix would be to have an API in cubeb to give us the number. Short
   // of that, we approximate it here. bug 1019507
   return mIterationDurationMS;
--- a/dom/media/GraphDriver.h
+++ b/dom/media/GraphDriver.h
@@ -525,22 +525,23 @@ private:
   /* This is set during initialization, and can be read safely afterwards. */
   dom::AudioChannel mAudioChannel;
   /* Used to queue us to add the mixer callback on first run. */
   bool mAddedMixer;
 
   /* This is atomic and is set by the audio callback thread. It can be read by
    * any thread safely. */
   Atomic<bool> mInCallback;
+
+#ifdef XP_MACOSX
   /**
    * True if microphone is being used by this process. This is synchronized by
    * the graph's monitor. */
   bool mMicrophoneActive;
 
-#ifdef XP_MACOSX
   /* Implements the workaround for the osx audio stack when changing output
    * devices. See comments in .cpp */
   bool OSXDeviceSwitchingWorkaround();
   /* Self-reference that keep this driver alive when switching output audio
    * device and making the graph running temporarily off a SystemClockDriver.  */
   SelfReference<AudioCallbackDriver> mSelfReference;
   /* While switching devices, we keep track of the number of callbacks received,
    * since OSX seems to still call us _sometimes_. */
--- a/dom/media/MediaStreamGraph.cpp
+++ b/dom/media/MediaStreamGraph.cpp
@@ -415,16 +415,18 @@ namespace {
   const uint32_t NOT_VISITED = UINT32_MAX;
   // Value of mCycleMarker for ordered streams in muted cycles.
   const uint32_t IN_MUTED_CYCLE = 1;
 } // namespace
 
 bool
 MediaStreamGraphImpl::AudioTrackPresent(bool& aNeedsAEC)
 {
+  AssertOnGraphThreadOrNotRunning();
+
   bool audioTrackPresent = false;
   for (uint32_t i = 0; i < mStreams.Length() && audioTrackPresent == false; ++i) {
     MediaStream* stream = mStreams[i];
     SourceMediaStream* source = stream->AsSourceStream();
 #ifdef MOZ_WEBRTC
     if (source && source->NeedsMixing()) {
       aNeedsAEC = true;
     }
@@ -496,23 +498,31 @@ MediaStreamGraphImpl::UpdateStreamOrder(
     MonitorAutoLock mon(mMonitor);
     if (mLifecycleState == LIFECYCLE_RUNNING) {
       AudioCallbackDriver* driver = new AudioCallbackDriver(this);
       CurrentDriver()->SwitchAtNextIteration(driver);
     }
   }
 
 #ifdef MOZ_WEBRTC
+  // Whenever we change AEC state, notify the current driver, which also
+  // will sample the state when the driver inits
   if (shouldAEC && !mFarendObserverRef && gFarendObserver) {
     mFarendObserverRef = gFarendObserver;
     mMixer.AddCallback(mFarendObserverRef);
+    if (CurrentDriver()->AsAudioCallbackDriver()) {
+      CurrentDriver()->AsAudioCallbackDriver()->SetMicrophoneActive(true);
+    }
   } else if (!shouldAEC && mFarendObserverRef){
     if (mMixer.FindCallback(mFarendObserverRef)) {
       mMixer.RemoveCallback(mFarendObserverRef);
       mFarendObserverRef = nullptr;
+      if (CurrentDriver()->AsAudioCallbackDriver()) {
+        CurrentDriver()->AsAudioCallbackDriver()->SetMicrophoneActive(false);
+      }
     }
   }
 #endif
 
   if (!mStreamOrderDirty) {
     return;
   }