From ac63ac7193e4b344d3c682afce9bc1488d6df573 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=B6ller?= Date: Tue, 8 Jan 2019 13:47:12 +0100 Subject: [PATCH] Update refcounting of AudioState to use rtc::RefCountedObject Bug: webrtc:8270, webrtc:9305 Change-Id: I9ce76ebe358b3f34d2ad424861a396a0dc2a537d Reviewed-on: https://webrtc-review.googlesource.com/c/116486 Reviewed-by: Karl Wiberg Commit-Queue: Niels Moller Cr-Commit-Position: refs/heads/master@{#26177} --- audio/audio_state.cc | 18 ++---------------- audio/audio_state.h | 10 +--------- audio/audio_state_unittest.cc | 21 +++++++++++---------- 3 files changed, 14 insertions(+), 35 deletions(-) diff --git a/audio/audio_state.cc b/audio/audio_state.cc index 7d473ae631..ce9e89480f 100644 --- a/audio/audio_state.cc +++ b/audio/audio_state.cc @@ -17,9 +17,9 @@ #include "absl/memory/memory.h" #include "audio/audio_receive_stream.h" #include "modules/audio_device/include/audio_device.h" -#include "rtc_base/atomicops.h" #include "rtc_base/checks.h" #include "rtc_base/logging.h" +#include "rtc_base/refcountedobject.h" #include "rtc_base/thread.h" namespace webrtc { @@ -168,20 +168,6 @@ void AudioState::SetStereoChannelSwapping(bool enable) { audio_transport_.SetStereoChannelSwapping(enable); } -// Reference count; implementation copied from rtc::RefCountedObject. -void AudioState::AddRef() const { - rtc::AtomicOps::Increment(&ref_count_); -} - -// Reference count; implementation copied from rtc::RefCountedObject. -rtc::RefCountReleaseStatus AudioState::Release() const { - if (rtc::AtomicOps::Decrement(&ref_count_) == 0) { - delete this; - return rtc::RefCountReleaseStatus::kDroppedLastRef; - } - return rtc::RefCountReleaseStatus::kOtherRefsRemained; -} - void AudioState::UpdateAudioTransportWithSendingStreams() { RTC_DCHECK(thread_checker_.CalledOnValidThread()); std::vector sending_streams; @@ -199,6 +185,6 @@ void AudioState::UpdateAudioTransportWithSendingStreams() { rtc::scoped_refptr AudioState::Create( const AudioState::Config& config) { - return rtc::scoped_refptr(new internal::AudioState(config)); + return new rtc::RefCountedObject(config); } } // namespace webrtc diff --git a/audio/audio_state.h b/audio/audio_state.h index 9e302c4007..f633f2ffcd 100644 --- a/audio/audio_state.h +++ b/audio/audio_state.h @@ -30,7 +30,7 @@ class AudioReceiveStream; namespace internal { -class AudioState final : public webrtc::AudioState { +class AudioState : public webrtc::AudioState { public: explicit AudioState(const AudioState::Config& config); ~AudioState() override; @@ -60,10 +60,6 @@ class AudioState final : public webrtc::AudioState { void RemoveSendingStream(webrtc::AudioSendStream* stream); private: - // rtc::RefCountInterface implementation. - void AddRef() const override; - rtc::RefCountReleaseStatus Release() const override; - void UpdateAudioTransportWithSendingStreams(); rtc::ThreadChecker thread_checker_; @@ -72,10 +68,6 @@ class AudioState final : public webrtc::AudioState { bool recording_enabled_ = true; bool playout_enabled_ = true; - // Reference count; implementation copied from rtc::RefCountedObject. - // TODO(nisse): Use RefCountedObject or RefCountedBase instead. - mutable volatile int ref_count_ = 0; - // Transports mixed audio from the mixer to the audio device and // recorded audio to the sending streams. AudioTransportImpl audio_transport_; diff --git a/audio/audio_state_unittest.cc b/audio/audio_state_unittest.cc index dc622df00d..790b6433dd 100644 --- a/audio/audio_state_unittest.cc +++ b/audio/audio_state_unittest.cc @@ -98,14 +98,14 @@ TEST(AudioStateTest, Create) { TEST(AudioStateTest, ConstructDestruct) { ConfigHelper helper; - std::unique_ptr audio_state( - new internal::AudioState(helper.config())); + rtc::scoped_refptr audio_state( + new rtc::RefCountedObject(helper.config())); } TEST(AudioStateTest, RecordedAudioArrivesAtSingleStream) { ConfigHelper helper; - std::unique_ptr audio_state( - new internal::AudioState(helper.config())); + rtc::scoped_refptr audio_state( + new rtc::RefCountedObject(helper.config())); MockAudioSendStream stream; audio_state->AddSendingStream(&stream, 8000, 2); @@ -142,8 +142,8 @@ TEST(AudioStateTest, RecordedAudioArrivesAtSingleStream) { TEST(AudioStateTest, RecordedAudioArrivesAtMultipleStreams) { ConfigHelper helper; - std::unique_ptr audio_state( - new internal::AudioState(helper.config())); + rtc::scoped_refptr audio_state( + new rtc::RefCountedObject(helper.config())); MockAudioSendStream stream_1; MockAudioSendStream stream_2; @@ -196,8 +196,9 @@ TEST(AudioStateTest, EnableChannelSwap) { constexpr size_t kNumChannels = 2; ConfigHelper helper; - std::unique_ptr audio_state( - new internal::AudioState(helper.config())); + rtc::scoped_refptr audio_state( + new rtc::RefCountedObject(helper.config())); + audio_state->SetStereoChannelSwapping(true); MockAudioSendStream stream; @@ -227,8 +228,8 @@ TEST(AudioStateTest, InputLevelStats) { constexpr size_t kNumChannels = 1; ConfigHelper helper; - std::unique_ptr audio_state( - new internal::AudioState(helper.config())); + rtc::scoped_refptr audio_state( + new rtc::RefCountedObject(helper.config())); // Push a silent buffer -> Level stats should be zeros except for duration. {