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 <kwiberg@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#26177}
diff --git a/audio/audio_state.cc b/audio/audio_state.cc
index 7d473ae..ce9e894 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 @@
   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<webrtc::AudioSendStream*> sending_streams;
@@ -199,6 +185,6 @@
 
 rtc::scoped_refptr<AudioState> AudioState::Create(
     const AudioState::Config& config) {
-  return rtc::scoped_refptr<AudioState>(new internal::AudioState(config));
+  return new rtc::RefCountedObject<internal::AudioState>(config);
 }
 }  // namespace webrtc
diff --git a/audio/audio_state.h b/audio/audio_state.h
index 9e302c4..f633f2f 100644
--- a/audio/audio_state.h
+++ b/audio/audio_state.h
@@ -30,7 +30,7 @@
 
 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 @@
   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 @@
   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 dc622df..790b643 100644
--- a/audio/audio_state_unittest.cc
+++ b/audio/audio_state_unittest.cc
@@ -98,14 +98,14 @@
 
 TEST(AudioStateTest, ConstructDestruct) {
   ConfigHelper helper;
-  std::unique_ptr<internal::AudioState> audio_state(
-      new internal::AudioState(helper.config()));
+  rtc::scoped_refptr<internal::AudioState> audio_state(
+      new rtc::RefCountedObject<internal::AudioState>(helper.config()));
 }
 
 TEST(AudioStateTest, RecordedAudioArrivesAtSingleStream) {
   ConfigHelper helper;
-  std::unique_ptr<internal::AudioState> audio_state(
-      new internal::AudioState(helper.config()));
+  rtc::scoped_refptr<internal::AudioState> audio_state(
+      new rtc::RefCountedObject<internal::AudioState>(helper.config()));
 
   MockAudioSendStream stream;
   audio_state->AddSendingStream(&stream, 8000, 2);
@@ -142,8 +142,8 @@
 
 TEST(AudioStateTest, RecordedAudioArrivesAtMultipleStreams) {
   ConfigHelper helper;
-  std::unique_ptr<internal::AudioState> audio_state(
-      new internal::AudioState(helper.config()));
+  rtc::scoped_refptr<internal::AudioState> audio_state(
+      new rtc::RefCountedObject<internal::AudioState>(helper.config()));
 
   MockAudioSendStream stream_1;
   MockAudioSendStream stream_2;
@@ -196,8 +196,9 @@
   constexpr size_t kNumChannels = 2;
 
   ConfigHelper helper;
-  std::unique_ptr<internal::AudioState> audio_state(
-      new internal::AudioState(helper.config()));
+  rtc::scoped_refptr<internal::AudioState> audio_state(
+      new rtc::RefCountedObject<internal::AudioState>(helper.config()));
+
   audio_state->SetStereoChannelSwapping(true);
 
   MockAudioSendStream stream;
@@ -227,8 +228,8 @@
   constexpr size_t kNumChannels = 1;
 
   ConfigHelper helper;
-  std::unique_ptr<internal::AudioState> audio_state(
-      new internal::AudioState(helper.config()));
+  rtc::scoped_refptr<internal::AudioState> audio_state(
+      new rtc::RefCountedObject<internal::AudioState>(helper.config()));
 
   // Push a silent buffer -> Level stats should be zeros except for duration.
   {