Inlines NullAudioPoller functionality into AudioState class.
As part of this, we also use TaskQueue and RepeatedTask rather
than rtc::Thread + rtc::MessageHandler. With the ultimate goal of
deprecating rtc::Thread.
Bug: webrtc:9883
Change-Id: I2fb851ac31ee2431435d51de78ff446572512201
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/167528
Commit-Queue: Sebastian Jansson <srte@webrtc.org>
Reviewed-by: Sam Zackrisson <saza@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#30430}
diff --git a/audio/BUILD.gn b/audio/BUILD.gn
index 80f2d52..afc9082 100644
--- a/audio/BUILD.gn
+++ b/audio/BUILD.gn
@@ -29,8 +29,6 @@
"channel_send.cc",
"channel_send.h",
"conversion.h",
- "null_audio_poller.cc",
- "null_audio_poller.h",
"remix_resample.cc",
"remix_resample.h",
]
@@ -82,6 +80,7 @@
"../rtc_base:rtc_task_queue",
"../rtc_base:safe_minmax",
"../rtc_base/experiments:field_trial_parser",
+ "../rtc_base/task_utils:repeating_task",
"../system_wrappers",
"../system_wrappers:field_trial",
"../system_wrappers:metrics",
diff --git a/audio/audio_state.cc b/audio/audio_state.cc
index 1a4fd77..b103bc6 100644
--- a/audio/audio_state.cc
+++ b/audio/audio_state.cc
@@ -38,6 +38,7 @@
RTC_DCHECK(thread_checker_.IsCurrent());
RTC_DCHECK(receiving_streams_.empty());
RTC_DCHECK(sending_streams_.empty());
+ null_audio_poller_.Stop();
}
AudioProcessing* AudioState::audio_processing() {
@@ -176,10 +177,31 @@
// 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_);
+ if (!null_audio_poller_.Running()) {
+ // TODO(srte): Replace current thread with an explicit task queue
+ // instance.
+ null_audio_poller_ =
+ RepeatingTaskHandle::Start(rtc::Thread::Current(), [this] {
+ // WebRTC uses 10ms audio windows by default
+ constexpr TimeDelta kPollInterval = TimeDelta::ms(10);
+ constexpr Frequency kSampleRate = Frequency::kHz(48);
+ constexpr size_t kSamplesPerPoll =
+ static_cast<size_t>(kSampleRate * kPollInterval);
+ constexpr size_t kNumChannels = 1;
+ int16_t audio_sample_buffer[kSamplesPerPoll * kNumChannels];
+ // Output variables from |NeedMorePlayData|.
+ size_t n_samples;
+ int64_t elapsed_time_ms;
+ int64_t ntp_time_ms;
+ audio_transport_.NeedMorePlayData(kSamplesPerPoll, sizeof(int16_t),
+ kNumChannels, kSampleRate.hertz(),
+ audio_sample_buffer, n_samples,
+ &elapsed_time_ms, &ntp_time_ms);
+ return kPollInterval;
+ });
+ }
} else {
- null_audio_poller_.reset();
+ null_audio_poller_.Stop();
}
}
} // namespace internal
diff --git a/audio/audio_state.h b/audio/audio_state.h
index f696d5a..0cbdf7e 100644
--- a/audio/audio_state.h
+++ b/audio/audio_state.h
@@ -16,11 +16,11 @@
#include <unordered_set>
#include "audio/audio_transport_impl.h"
-#include "audio/null_audio_poller.h"
#include "call/audio_state.h"
#include "rtc_base/constructor_magic.h"
#include "rtc_base/critical_section.h"
#include "rtc_base/ref_count.h"
+#include "rtc_base/task_utils/repeating_task.h"
#include "rtc_base/thread_checker.h"
namespace webrtc {
@@ -75,7 +75,7 @@
// Null audio poller is used to continue polling the audio streams if audio
// playout is disabled so that audio processing still happens and the audio
// stats are still updated.
- std::unique_ptr<NullAudioPoller> null_audio_poller_;
+ RepeatingTaskHandle null_audio_poller_;
std::unordered_set<webrtc::AudioReceiveStream*> receiving_streams_;
struct StreamProperties {
diff --git a/audio/null_audio_poller.cc b/audio/null_audio_poller.cc
deleted file mode 100644
index 22f575d..0000000
--- a/audio/null_audio_poller.cc
+++ /dev/null
@@ -1,71 +0,0 @@
-/*
- * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
- *
- * Use of this source code is governed by a BSD-style license
- * that can be found in the LICENSE file in the root of the source
- * tree. An additional intellectual property rights grant can be found
- * in the file PATENTS. All contributing project authors may
- * be found in the AUTHORS file in the root of the source tree.
- */
-
-#include "audio/null_audio_poller.h"
-
-#include <stddef.h>
-
-#include "rtc_base/checks.h"
-#include "rtc_base/location.h"
-#include "rtc_base/thread.h"
-#include "rtc_base/time_utils.h"
-
-namespace webrtc {
-namespace internal {
-
-namespace {
-
-constexpr int64_t kPollDelayMs = 10; // WebRTC uses 10ms by default
-
-constexpr size_t kNumChannels = 1;
-constexpr uint32_t kSamplesPerSecond = 48000; // 48kHz
-constexpr size_t kNumSamples = kSamplesPerSecond / 100; // 10ms of samples
-
-} // namespace
-
-NullAudioPoller::NullAudioPoller(AudioTransport* audio_transport)
- : audio_transport_(audio_transport),
- reschedule_at_(rtc::TimeMillis() + kPollDelayMs) {
- RTC_DCHECK(audio_transport);
- OnMessage(nullptr); // Start the poll loop.
-}
-
-NullAudioPoller::~NullAudioPoller() {
- RTC_DCHECK(thread_checker_.IsCurrent());
- rtc::Thread::Current()->Clear(this);
-}
-
-void NullAudioPoller::OnMessage(rtc::Message* msg) {
- RTC_DCHECK(thread_checker_.IsCurrent());
-
- // Buffer to hold the audio samples.
- int16_t buffer[kNumSamples * kNumChannels];
- // Output variables from |NeedMorePlayData|.
- size_t n_samples;
- int64_t elapsed_time_ms;
- int64_t ntp_time_ms;
- audio_transport_->NeedMorePlayData(kNumSamples, sizeof(int16_t), kNumChannels,
- kSamplesPerSecond, buffer, n_samples,
- &elapsed_time_ms, &ntp_time_ms);
-
- // Reschedule the next poll iteration. If, for some reason, the given
- // reschedule time has already passed, reschedule as soon as possible.
- int64_t now = rtc::TimeMillis();
- if (reschedule_at_ < now) {
- reschedule_at_ = now;
- }
- rtc::Thread::Current()->PostAt(RTC_FROM_HERE, reschedule_at_, this, 0);
-
- // Loop after next will be kPollDelayMs later.
- reschedule_at_ += kPollDelayMs;
-}
-
-} // namespace internal
-} // namespace webrtc
diff --git a/audio/null_audio_poller.h b/audio/null_audio_poller.h
deleted file mode 100644
index 97cd2c7..0000000
--- a/audio/null_audio_poller.h
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
- *
- * Use of this source code is governed by a BSD-style license
- * that can be found in the LICENSE file in the root of the source
- * tree. An additional intellectual property rights grant can be found
- * in the file PATENTS. All contributing project authors may
- * be found in the AUTHORS file in the root of the source tree.
- */
-
-#ifndef AUDIO_NULL_AUDIO_POLLER_H_
-#define AUDIO_NULL_AUDIO_POLLER_H_
-
-#include <stdint.h>
-
-#include "modules/audio_device/include/audio_device_defines.h"
-#include "rtc_base/message_handler.h"
-#include "rtc_base/thread_checker.h"
-
-namespace webrtc {
-namespace internal {
-
-class NullAudioPoller final : public rtc::MessageHandler {
- public:
- explicit NullAudioPoller(AudioTransport* audio_transport);
- ~NullAudioPoller() override;
-
- protected:
- void OnMessage(rtc::Message* msg) override;
-
- private:
- rtc::ThreadChecker thread_checker_;
- AudioTransport* const audio_transport_;
- int64_t reschedule_at_;
-};
-
-} // namespace internal
-} // namespace webrtc
-
-#endif // AUDIO_NULL_AUDIO_POLLER_H_