Avoid running NullAudioPoller without receiving streams

Fixes an issue with NullAudioPoller calling the mixer every 10 ms when
no call is ongoing.

Bug: b/142775365
Change-Id: I77eeddadaf08b358cce2b389c70e4f2baf1d5627
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/157176
Reviewed-by: Henrik Andreassson <henrika@webrtc.org>
Commit-Queue: Gustaf Ullberg <gustaf@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#29508}
diff --git a/audio/audio_state.cc b/audio/audio_state.cc
index 03cf730..3ca1dd7 100644
--- a/audio/audio_state.cc
+++ b/audio/audio_state.cc
@@ -63,6 +63,7 @@
   }
 
   // Make sure playback is initialized; start playing if enabled.
+  UpdateNullAudioPollerState();
   auto* adm = config_.audio_device_module.get();
   if (!adm->Playing()) {
     if (adm->InitPlayout() == 0) {
@@ -81,6 +82,7 @@
   RTC_DCHECK_EQ(1, count);
   config_.audio_mixer->RemoveSource(
       static_cast<internal::AudioReceiveStream*>(stream));
+  UpdateNullAudioPollerState();
   if (receiving_streams_.empty()) {
     config_.audio_device_module->StopPlayout();
   }
@@ -124,13 +126,13 @@
   if (playout_enabled_ != enabled) {
     playout_enabled_ = enabled;
     if (enabled) {
-      null_audio_poller_.reset();
+      UpdateNullAudioPollerState();
       if (!receiving_streams_.empty()) {
         config_.audio_device_module->StartPlayout();
       }
     } else {
       config_.audio_device_module->StopPlayout();
-      null_audio_poller_ = std::make_unique<NullAudioPoller>(&audio_transport_);
+      UpdateNullAudioPollerState();
     }
   }
 }
@@ -168,6 +170,17 @@
   audio_transport_.UpdateSendingStreams(std::move(sending_streams),
                                         max_sample_rate_hz, max_num_channels);
 }
+
+void AudioState::UpdateNullAudioPollerState() {
+  // Run NullAudioPoller when there are receiving streams and playout is
+  // disabled.
+  if (!receiving_streams_.empty() && !playout_enabled_) {
+    if (!null_audio_poller_)
+      null_audio_poller_ = std::make_unique<NullAudioPoller>(&audio_transport_);
+  } else {
+    null_audio_poller_.reset();
+  }
+}
 }  // namespace internal
 
 rtc::scoped_refptr<AudioState> AudioState::Create(
diff --git a/audio/audio_state.h b/audio/audio_state.h
index 15d1641..f696d5a 100644
--- a/audio/audio_state.h
+++ b/audio/audio_state.h
@@ -60,6 +60,7 @@
 
  private:
   void UpdateAudioTransportWithSendingStreams();
+  void UpdateNullAudioPollerState();
 
   rtc::ThreadChecker thread_checker_;
   rtc::ThreadChecker process_thread_checker_;