Rewrite AudioState null poller to use TaskQueueBase interface

Bug: webrtc:9702, webrtc:11318
Change-Id: If39871b8b2b1ccbfb17827bc795874f9ecc317d0
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/271289
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Reviewed-by: Jakob Ivarsson‎ <jakobi@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#37797}
This commit is contained in:
Danil Chapovalov 2022-08-16 13:23:19 +02:00 committed by WebRTC LUCI CQ
parent cca884d1cc
commit 0cf140d720
7 changed files with 42 additions and 131 deletions

View File

@ -33,8 +33,6 @@ rtc_library("audio") {
"channel_send_frame_transformer_delegate.cc",
"channel_send_frame_transformer_delegate.h",
"conversion.h",
"null_audio_poller.cc",
"null_audio_poller.h",
"remix_resample.cc",
"remix_resample.h",
]
@ -63,6 +61,7 @@ rtc_library("audio") {
"../api/task_queue",
"../api/task_queue:pending_task_safety_flag",
"../api/transport/rtp:rtp_source",
"../api/units:time_delta",
"../call:audio_sender_interface",
"../call:bitrate_allocator",
"../call:call_interfaces",
@ -85,11 +84,9 @@ rtc_library("audio") {
"../modules/pacing",
"../modules/rtp_rtcp",
"../modules/rtp_rtcp:rtp_rtcp_format",
"../rtc_base",
"../rtc_base:audio_format_to_string",
"../rtc_base:buffer",
"../rtc_base:checks",
"../rtc_base:location",
"../rtc_base:logging",
"../rtc_base:macromagic",
"../rtc_base:race_checker",
@ -106,6 +103,7 @@ rtc_library("audio") {
"../rtc_base/experiments:field_trial_parser",
"../rtc_base/synchronization:mutex",
"../rtc_base/system:no_unique_address",
"../rtc_base/task_utils:repeating_task",
"../system_wrappers",
"../system_wrappers:field_trial",
"../system_wrappers:metrics",

View File

@ -15,12 +15,14 @@
#include <utility>
#include <vector>
#include "api/sequence_checker.h"
#include "api/task_queue/task_queue_base.h"
#include "api/units/time_delta.h"
#include "audio/audio_receive_stream.h"
#include "audio/audio_send_stream.h"
#include "modules/audio_device/include/audio_device.h"
#include "rtc_base/checks.h"
#include "rtc_base/logging.h"
#include "rtc_base/thread.h"
namespace webrtc {
namespace internal {
@ -36,9 +38,10 @@ AudioState::AudioState(const AudioState::Config& config)
}
AudioState::~AudioState() {
RTC_DCHECK(thread_checker_.IsCurrent());
RTC_DCHECK_RUN_ON(&thread_checker_);
RTC_DCHECK(receiving_streams_.empty());
RTC_DCHECK(sending_streams_.empty());
RTC_DCHECK(!null_audio_poller_.Running());
}
AudioProcessing* AudioState::audio_processing() {
@ -51,7 +54,7 @@ AudioTransport* AudioState::audio_transport() {
void AudioState::AddReceivingStream(
webrtc::AudioReceiveStreamInterface* stream) {
RTC_DCHECK(thread_checker_.IsCurrent());
RTC_DCHECK_RUN_ON(&thread_checker_);
RTC_DCHECK_EQ(0, receiving_streams_.count(stream));
receiving_streams_.insert(stream);
if (!config_.audio_mixer->AddSource(
@ -75,7 +78,7 @@ void AudioState::AddReceivingStream(
void AudioState::RemoveReceivingStream(
webrtc::AudioReceiveStreamInterface* stream) {
RTC_DCHECK(thread_checker_.IsCurrent());
RTC_DCHECK_RUN_ON(&thread_checker_);
auto count = receiving_streams_.erase(stream);
RTC_DCHECK_EQ(1, count);
config_.audio_mixer->RemoveSource(
@ -89,7 +92,7 @@ void AudioState::RemoveReceivingStream(
void AudioState::AddSendingStream(webrtc::AudioSendStream* stream,
int sample_rate_hz,
size_t num_channels) {
RTC_DCHECK(thread_checker_.IsCurrent());
RTC_DCHECK_RUN_ON(&thread_checker_);
auto& properties = sending_streams_[stream];
properties.sample_rate_hz = sample_rate_hz;
properties.num_channels = num_channels;
@ -109,7 +112,7 @@ void AudioState::AddSendingStream(webrtc::AudioSendStream* stream,
}
void AudioState::RemoveSendingStream(webrtc::AudioSendStream* stream) {
RTC_DCHECK(thread_checker_.IsCurrent());
RTC_DCHECK_RUN_ON(&thread_checker_);
auto count = sending_streams_.erase(stream);
RTC_DCHECK_EQ(1, count);
UpdateAudioTransportWithSendingStreams();
@ -120,7 +123,7 @@ void AudioState::RemoveSendingStream(webrtc::AudioSendStream* stream) {
void AudioState::SetPlayout(bool enabled) {
RTC_LOG(LS_INFO) << "SetPlayout(" << enabled << ")";
RTC_DCHECK(thread_checker_.IsCurrent());
RTC_DCHECK_RUN_ON(&thread_checker_);
if (playout_enabled_ != enabled) {
playout_enabled_ = enabled;
if (enabled) {
@ -137,7 +140,7 @@ void AudioState::SetPlayout(bool enabled) {
void AudioState::SetRecording(bool enabled) {
RTC_LOG(LS_INFO) << "SetRecording(" << enabled << ")";
RTC_DCHECK(thread_checker_.IsCurrent());
RTC_DCHECK_RUN_ON(&thread_checker_);
if (recording_enabled_ != enabled) {
recording_enabled_ = enabled;
if (enabled) {
@ -173,10 +176,32 @@ 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_);
if (!null_audio_poller_.Running()) {
AudioTransport* audio_transport = &audio_transport_;
null_audio_poller_ = RepeatingTaskHandle::Start(
TaskQueueBase::Current(), [audio_transport] {
static constexpr size_t kNumChannels = 1;
static constexpr uint32_t kSamplesPerSecond = 48'000;
// 10ms of samples
static constexpr size_t kNumSamples = kSamplesPerSecond / 100;
// 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.
return TimeDelta::Millis(10);
});
}
} else {
null_audio_poller_.reset();
null_audio_poller_.Stop();
}
}
} // namespace internal

View File

@ -16,10 +16,11 @@
#include "api/sequence_checker.h"
#include "audio/audio_transport_impl.h"
#include "audio/null_audio_poller.h"
#include "call/audio_state.h"
#include "rtc_base/containers/flat_set.h"
#include "rtc_base/ref_count.h"
#include "rtc_base/task_utils/repeating_task.h"
#include "rtc_base/thread_annotations.h"
namespace webrtc {
@ -61,7 +62,7 @@ class AudioState : public webrtc::AudioState {
private:
void UpdateAudioTransportWithSendingStreams();
void UpdateNullAudioPollerState();
void UpdateNullAudioPollerState() RTC_RUN_ON(&thread_checker_);
SequenceChecker thread_checker_;
SequenceChecker process_thread_checker_;
@ -76,7 +77,7 @@ class AudioState : public webrtc::AudioState {
// 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_ RTC_GUARDED_BY(&thread_checker_);
webrtc::flat_set<webrtc::AudioReceiveStreamInterface*> receiving_streams_;
struct StreamProperties {

View File

@ -41,7 +41,6 @@
#include "modules/rtp_rtcp/source/rtp_rtcp_config.h"
#include "modules/rtp_rtcp/source/rtp_rtcp_impl2.h"
#include "rtc_base/checks.h"
#include "rtc_base/location.h"
#include "rtc_base/logging.h"
#include "rtc_base/numerics/safe_minmax.h"
#include "rtc_base/race_checker.h"

View File

@ -33,7 +33,6 @@
#include "modules/rtp_rtcp/source/rtp_rtcp_impl2.h"
#include "rtc_base/checks.h"
#include "rtc_base/event.h"
#include "rtc_base/location.h"
#include "rtc_base/logging.h"
#include "rtc_base/numerics/safe_conversions.h"
#include "rtc_base/race_checker.h"

View File

@ -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

View File

@ -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 "api/sequence_checker.h"
#include "modules/audio_device/include/audio_device_defines.h"
#include "rtc_base/message_handler.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:
SequenceChecker thread_checker_;
AudioTransport* const audio_transport_;
int64_t reschedule_at_;
};
} // namespace internal
} // namespace webrtc
#endif // AUDIO_NULL_AUDIO_POLLER_H_