Replace most explicit `new RefCountedObject...` with make_ref_counted

Bug: webrtc:12701
Change-Id: Iab91f1e96715eed7f9ddcfe1ca55510a18c817eb
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/224544
Reviewed-by: Artem Titov <titovartem@webrtc.org>
Reviewed-by: Tomas Gunnarsson <tommi@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#35786}
diff --git a/examples/androidnativeapi/jni/android_call_client.cc b/examples/androidnativeapi/jni/android_call_client.cc
index 3c7c86d..2c5e1af 100644
--- a/examples/androidnativeapi/jni/android_call_client.cc
+++ b/examples/androidnativeapi/jni/android_call_client.cc
@@ -208,7 +208,7 @@
 
 void AndroidCallClient::Connect() {
   webrtc::MutexLock lock(&pc_mutex_);
-  pc_->CreateOffer(new rtc::RefCountedObject<CreateOfferObserver>(pc_),
+  pc_->CreateOffer(rtc::make_ref_counted<CreateOfferObserver>(pc_),
                    webrtc::PeerConnectionInterface::RTCOfferAnswerOptions());
 }
 
@@ -258,7 +258,7 @@
 
   // Ownership of desc was transferred to us, now we transfer it forward.
   pc_->SetLocalDescription(
-      new rtc::RefCountedObject<SetLocalSessionDescriptionObserver>(), desc);
+      rtc::make_ref_counted<SetLocalSessionDescriptionObserver>(), desc);
 
   // Generate a fake answer.
   std::unique_ptr<webrtc::SessionDescriptionInterface> answer(
diff --git a/examples/objcnativeapi/objc/objc_call_client.mm b/examples/objcnativeapi/objc/objc_call_client.mm
index c1d84f7..081b5bc 100644
--- a/examples/objcnativeapi/objc/objc_call_client.mm
+++ b/examples/objcnativeapi/objc/objc_call_client.mm
@@ -168,7 +168,7 @@
 
 void ObjCCallClient::Connect() {
   webrtc::MutexLock lock(&pc_mutex_);
-  pc_->CreateOffer(new rtc::RefCountedObject<CreateOfferObserver>(pc_),
+  pc_->CreateOffer(rtc::make_ref_counted<CreateOfferObserver>(pc_),
                    webrtc::PeerConnectionInterface::RTCOfferAnswerOptions());
 }
 
@@ -214,7 +214,7 @@
   RTC_LOG(LS_INFO) << "Created offer: " << sdp;
 
   // Ownership of desc was transferred to us, now we transfer it forward.
-  pc_->SetLocalDescription(new rtc::RefCountedObject<SetLocalSessionDescriptionObserver>(), desc);
+  pc_->SetLocalDescription(rtc::make_ref_counted<SetLocalSessionDescriptionObserver>(), desc);
 
   // Generate a fake answer.
   std::unique_ptr<webrtc::SessionDescriptionInterface> answer(
diff --git a/examples/peerconnection/client/conductor.cc b/examples/peerconnection/client/conductor.cc
index 65958a4..93e95b6 100644
--- a/examples/peerconnection/client/conductor.cc
+++ b/examples/peerconnection/client/conductor.cc
@@ -59,7 +59,7 @@
     : public webrtc::SetSessionDescriptionObserver {
  public:
   static DummySetSessionDescriptionObserver* Create() {
-    return new rtc::RefCountedObject<DummySetSessionDescriptionObserver>();
+    return rtc::make_ref_counted<DummySetSessionDescriptionObserver>();
   }
   virtual void OnSuccess() { RTC_LOG(LS_INFO) << __FUNCTION__; }
   virtual void OnFailure(webrtc::RTCError error) {
diff --git a/examples/peerconnection/client/linux/main.cc b/examples/peerconnection/client/linux/main.cc
index ccca7b1..47f4f36 100644
--- a/examples/peerconnection/client/linux/main.cc
+++ b/examples/peerconnection/client/linux/main.cc
@@ -101,8 +101,7 @@
   rtc::InitializeSSL();
   // Must be constructed after we set the socketserver.
   PeerConnectionClient client;
-  rtc::scoped_refptr<Conductor> conductor(
-      new rtc::RefCountedObject<Conductor>(&client, &wnd));
+  auto conductor = rtc::make_ref_counted<Conductor>(&client, &wnd);
   socket_server.set_client(&client);
   socket_server.set_conductor(conductor);
 
diff --git a/examples/peerconnection/client/main.cc b/examples/peerconnection/client/main.cc
index 3ab4176..04a331d 100644
--- a/examples/peerconnection/client/main.cc
+++ b/examples/peerconnection/client/main.cc
@@ -107,8 +107,7 @@
 
   rtc::InitializeSSL();
   PeerConnectionClient client;
-  rtc::scoped_refptr<Conductor> conductor(
-      new rtc::RefCountedObject<Conductor>(&client, &wnd));
+  auto conductor = rtc::make_ref_counted<Conductor>(&client, &wnd);
 
   // Main loop.
   MSG msg;
diff --git a/examples/unityplugin/simple_peer_connection.cc b/examples/unityplugin/simple_peer_connection.cc
index 34abbe2..16c580e 100644
--- a/examples/unityplugin/simple_peer_connection.cc
+++ b/examples/unityplugin/simple_peer_connection.cc
@@ -99,7 +99,7 @@
     : public webrtc::SetSessionDescriptionObserver {
  public:
   static DummySetSessionDescriptionObserver* Create() {
-    return new rtc::RefCountedObject<DummySetSessionDescriptionObserver>();
+    return rtc::make_ref_counted<DummySetSessionDescriptionObserver>();
   }
   virtual void OnSuccess() { RTC_LOG(LS_INFO) << __FUNCTION__; }
   virtual void OnFailure(webrtc::RTCError error) {
@@ -446,10 +446,9 @@
     RTC_DCHECK(texture_helper != nullptr)
         << "Cannot get the Surface Texture Helper.";
 
-    rtc::scoped_refptr<webrtc::jni::AndroidVideoTrackSource> source(
-        new rtc::RefCountedObject<webrtc::jni::AndroidVideoTrackSource>(
-            g_signaling_thread.get(), env, /* is_screencast= */ false,
-            /* align_timestamps= */ true));
+    auto source = rtc::make_ref_counted<webrtc::jni::AndroidVideoTrackSource>(
+        g_signaling_thread.get(), env, /*is_screencast=*/false,
+        /*align_timestamps=*/true);
 
     // link with VideoCapturer (Camera);
     jmethodID link_camera_method = webrtc::GetStaticMethodID(
diff --git a/examples/unityplugin/unity_plugin_apis.cc b/examples/unityplugin/unity_plugin_apis.cc
index 34c28d9..672330f 100644
--- a/examples/unityplugin/unity_plugin_apis.cc
+++ b/examples/unityplugin/unity_plugin_apis.cc
@@ -27,7 +27,7 @@
                          const char* credential,
                          bool mandatory_receive_video) {
   g_peer_connection_map[g_peer_connection_id] =
-      new rtc::RefCountedObject<SimplePeerConnection>();
+      rtc::make_ref_counted<SimplePeerConnection>();
 
   if (!g_peer_connection_map[g_peer_connection_id]->InitializePeerConnection(
           turn_urls, no_of_urls, username, credential, mandatory_receive_video))
diff --git a/modules/audio_coding/acm2/audio_coding_module_unittest.cc b/modules/audio_coding/acm2/audio_coding_module_unittest.cc
index 7ddb3ed..e401e93 100644
--- a/modules/audio_coding/acm2/audio_coding_module_unittest.cc
+++ b/modules/audio_coding/acm2/audio_coding_module_unittest.cc
@@ -1051,8 +1051,7 @@
     rtc::scoped_refptr<AudioDecoderFactory> fact_;  // Fallback factory.
   };
 
-  rtc::scoped_refptr<rtc::RefCountedObject<ADFactory>> factory(
-      new rtc::RefCountedObject<ADFactory>);
+  auto factory = rtc::make_ref_counted<ADFactory>();
   std::string others_checksum_reference =
       GetCPUInfo(kAVX2) != 0 ? "454996a7adb3f62b259a53a09ff624cf"
                              : "f0148c5ef84e74e019ac7057af839102";
@@ -1540,9 +1539,8 @@
   const auto opus_decoder =
       AudioDecoderMultiChannelOpus::MakeAudioDecoder(*decoder_config);
 
-  rtc::scoped_refptr<AudioDecoderFactory> decoder_factory(
-      new rtc::RefCountedObject<test::AudioDecoderProxyFactory>(
-          opus_decoder.get()));
+  rtc::scoped_refptr<AudioDecoderFactory> decoder_factory =
+      rtc::make_ref_counted<test::AudioDecoderProxyFactory>(opus_decoder.get());
 
   // Set up an EXTERNAL DECODER to parse 4 channels.
   Run(AcmReceiverBitExactnessOldApi::PlatformChecksum(  // audio checksum
diff --git a/modules/rtp_rtcp/source/rtp_sender_video_unittest.cc b/modules/rtp_rtcp/source/rtp_sender_video_unittest.cc
index e337c33..dc845e4 100644
--- a/modules/rtp_rtcp/source/rtp_sender_video_unittest.cc
+++ b/modules/rtp_rtcp/source/rtp_sender_video_unittest.cc
@@ -775,8 +775,7 @@
 
   rtp_module_->RegisterRtpHeaderExtension(
       RtpDependencyDescriptorExtension::Uri(), kDependencyDescriptorId);
-  rtc::scoped_refptr<MockFrameEncryptor> encryptor(
-      new rtc::RefCountedObject<NiceMock<MockFrameEncryptor>>);
+  auto encryptor = rtc::make_ref_counted<NiceMock<MockFrameEncryptor>>();
   ON_CALL(*encryptor, GetMaxCiphertextByteSize).WillByDefault(ReturnArg<1>());
   ON_CALL(*encryptor, Encrypt)
       .WillByDefault(WithArgs<3, 5>(
diff --git a/pc/peer_connection_signaling_unittest.cc b/pc/peer_connection_signaling_unittest.cc
index 71ef51f..48a9988 100644
--- a/pc/peer_connection_signaling_unittest.cc
+++ b/pc/peer_connection_signaling_unittest.cc
@@ -1135,7 +1135,7 @@
             // operation executed immediately.
             RTC_DCHECK(!pc->GetTransceivers()[0]->mid().has_value());
             pc->SetLocalDescription(
-                new rtc::RefCountedObject<MockSetSessionDescriptionObserver>(),
+                rtc::make_ref_counted<MockSetSessionDescriptionObserver>(),
                 desc);
             EXPECT_TRUE(pc->GetTransceivers()[0]->mid().has_value());
           });
diff --git a/pc/rtc_stats_collector_unittest.cc b/pc/rtc_stats_collector_unittest.cc
index 971d8d3..cbb17c0 100644
--- a/pc/rtc_stats_collector_unittest.cc
+++ b/pc/rtc_stats_collector_unittest.cc
@@ -221,8 +221,7 @@
       const std::string& id,
       MediaStreamTrackInterface::TrackState state,
       bool create_fake_audio_processor) {
-    rtc::scoped_refptr<FakeAudioTrackForStats> audio_track_stats(
-        new rtc::RefCountedObject<FakeAudioTrackForStats>(id));
+    auto audio_track_stats = rtc::make_ref_counted<FakeAudioTrackForStats>(id);
     audio_track_stats->set_state(state);
     if (create_fake_audio_processor) {
       audio_track_stats->processor_ =
@@ -254,9 +253,8 @@
   static rtc::scoped_refptr<FakeVideoTrackSourceForStats> Create(
       int input_width,
       int input_height) {
-    return rtc::scoped_refptr<FakeVideoTrackSourceForStats>(
-        new rtc::RefCountedObject<FakeVideoTrackSourceForStats>(input_width,
-                                                                input_height));
+    return rtc::make_ref_counted<FakeVideoTrackSourceForStats>(input_width,
+                                                               input_height);
   }
 
   FakeVideoTrackSourceForStats(int input_width, int input_height)
@@ -301,9 +299,8 @@
       const std::string& id,
       MediaStreamTrackInterface::TrackState state,
       rtc::scoped_refptr<VideoTrackSourceInterface> source) {
-    rtc::scoped_refptr<FakeVideoTrackForStats> video_track(
-        new rtc::RefCountedObject<FakeVideoTrackForStats>(id,
-                                                          std::move(source)));
+    auto video_track =
+        rtc::make_ref_counted<FakeVideoTrackForStats>(id, std::move(source));
     video_track->set_state(state);
     return video_track;
   }
@@ -353,8 +350,7 @@
               media_type == cricket::MEDIA_TYPE_AUDIO) ||
              (track->kind() == MediaStreamTrackInterface::kVideoKind &&
               media_type == cricket::MEDIA_TYPE_VIDEO));
-  rtc::scoped_refptr<MockRtpSenderInternal> sender(
-      new rtc::RefCountedObject<MockRtpSenderInternal>());
+  auto sender = rtc::make_ref_counted<MockRtpSenderInternal>();
   EXPECT_CALL(*sender, track()).WillRepeatedly(Return(track));
   EXPECT_CALL(*sender, ssrc()).WillRepeatedly(Return(ssrc));
   EXPECT_CALL(*sender, media_type()).WillRepeatedly(Return(media_type));
@@ -375,8 +371,7 @@
     const rtc::scoped_refptr<MediaStreamTrackInterface>& track,
     uint32_t ssrc,
     int attachment_id) {
-  rtc::scoped_refptr<MockRtpReceiverInternal> receiver(
-      new rtc::RefCountedObject<MockRtpReceiverInternal>());
+  auto receiver = rtc::make_ref_counted<MockRtpReceiverInternal>();
   EXPECT_CALL(*receiver, track()).WillRepeatedly(Return(track));
   EXPECT_CALL(*receiver, streams())
       .WillRepeatedly(
@@ -626,7 +621,7 @@
 class RTCStatsCollectorTest : public ::testing::Test {
  public:
   RTCStatsCollectorTest()
-      : pc_(new rtc::RefCountedObject<FakePeerConnectionForStats>()),
+      : pc_(rtc::make_ref_counted<FakePeerConnectionForStats>()),
         stats_(new RTCStatsCollectorWrapper(pc_)) {}
 
   void ExpectReportContainsCertificateInfo(
@@ -3328,10 +3323,8 @@
 // Test that nothing bad happens if a callback causes GetStatsReport to be
 // called again recursively. Regression test for crbug.com/webrtc/8973.
 TEST_F(RTCStatsCollectorTest, DoNotCrashWhenGetStatsCalledDuringCallback) {
-  rtc::scoped_refptr<RecursiveCallback> callback1(
-      new rtc::RefCountedObject<RecursiveCallback>(stats_.get()));
-  rtc::scoped_refptr<RecursiveCallback> callback2(
-      new rtc::RefCountedObject<RecursiveCallback>(stats_.get()));
+  auto callback1 = rtc::make_ref_counted<RecursiveCallback>(stats_.get());
+  auto callback2 = rtc::make_ref_counted<RecursiveCallback>(stats_.get());
   stats_->stats_collector()->GetStatsReport(callback1);
   stats_->stats_collector()->GetStatsReport(callback2);
   EXPECT_TRUE_WAIT(callback1->called(), kGetStatsReportTimeoutMs);
@@ -3452,8 +3445,7 @@
 };
 
 TEST(RTCStatsCollectorTestWithFakeCollector, ThreadUsageAndResultsMerging) {
-  rtc::scoped_refptr<FakePeerConnectionForStats> pc(
-      new rtc::RefCountedObject<FakePeerConnectionForStats>());
+  auto pc = rtc::make_ref_counted<FakePeerConnectionForStats>();
   rtc::scoped_refptr<FakeRTCStatsCollector> stats_collector(
       FakeRTCStatsCollector::Create(pc, 50 * rtc::kNumMicrosecsPerMillisec));
   stats_collector->VerifyThreadUsageAndResultsMerging();
diff --git a/rtc_base/rtc_certificate_generator_unittest.cc b/rtc_base/rtc_certificate_generator_unittest.cc
index bef85f2..076f643 100644
--- a/rtc_base/rtc_certificate_generator_unittest.cc
+++ b/rtc_base/rtc_certificate_generator_unittest.cc
@@ -70,7 +70,7 @@
 class RTCCertificateGeneratorTest : public ::testing::Test {
  public:
   RTCCertificateGeneratorTest()
-      : fixture_(new RefCountedObject<RTCCertificateGeneratorFixture>()) {}
+      : fixture_(make_ref_counted<RTCCertificateGeneratorFixture>()) {}
 
  protected:
   static constexpr int kGenerationTimeoutMs = 10000;
diff --git a/sdk/objc/native/src/objc_frame_buffer.mm b/sdk/objc/native/src/objc_frame_buffer.mm
index 34d5d4e..e156c34 100644
--- a/sdk/objc/native/src/objc_frame_buffer.mm
+++ b/sdk/objc/native/src/objc_frame_buffer.mm
@@ -66,10 +66,7 @@
 }
 
 rtc::scoped_refptr<I420BufferInterface> ObjCFrameBuffer::ToI420() {
-  rtc::scoped_refptr<I420BufferInterface> buffer =
-      rtc::make_ref_counted<ObjCI420FrameBuffer>([frame_buffer_ toI420]);
-
-  return buffer;
+  return rtc::make_ref_counted<ObjCI420FrameBuffer>([frame_buffer_ toI420]);
 }
 
 id<RTC_OBJC_TYPE(RTCVideoFrameBuffer)> ObjCFrameBuffer::wrapped_frame_buffer() const {
diff --git a/sdk/objc/native/src/objc_video_decoder_factory.mm b/sdk/objc/native/src/objc_video_decoder_factory.mm
index e144fda..da3b302 100644
--- a/sdk/objc/native/src/objc_video_decoder_factory.mm
+++ b/sdk/objc/native/src/objc_video_decoder_factory.mm
@@ -56,8 +56,7 @@
 
   int32_t RegisterDecodeCompleteCallback(DecodedImageCallback *callback) override {
     [decoder_ setCallback:^(RTC_OBJC_TYPE(RTCVideoFrame) * frame) {
-      const rtc::scoped_refptr<VideoFrameBuffer> buffer =
-          rtc::make_ref_counted<ObjCFrameBuffer>(frame.buffer);
+      const auto buffer = rtc::make_ref_counted<ObjCFrameBuffer>(frame.buffer);
       VideoFrame videoFrame =
           VideoFrame::Builder()
               .set_video_frame_buffer(buffer)
diff --git a/test/peer_scenario/peer_scenario_client.cc b/test/peer_scenario/peer_scenario_client.cc
index e6fadc7..2767de2 100644
--- a/test/peer_scenario/peer_scenario_client.cc
+++ b/test/peer_scenario/peer_scenario_client.cc
@@ -321,9 +321,8 @@
                                                  config.generator);
   res.capturer = capturer.get();
   capturer->Init();
-  res.source =
-      new rtc::RefCountedObject<FrameGeneratorCapturerVideoTrackSource>(
-          std::move(capturer), config.screencast);
+  res.source = rtc::make_ref_counted<FrameGeneratorCapturerVideoTrackSource>(
+      std::move(capturer), config.screencast);
   auto track = pc_factory_->CreateVideoTrack(track_id, res.source);
   res.track = track;
   res.sender = peer_connection_->AddTrack(track, {kCommonStreamId}).MoveValue();
diff --git a/test/peer_scenario/peer_scenario_client.h b/test/peer_scenario/peer_scenario_client.h
index 91ce1e4..ab6aac9 100644
--- a/test/peer_scenario/peer_scenario_client.h
+++ b/test/peer_scenario/peer_scenario_client.h
@@ -104,10 +104,11 @@
   };
 
   struct VideoSendTrack {
+    // Raw pointer to the capturer owned by `source`.
     FrameGeneratorCapturer* capturer;
-    FrameGeneratorCapturerVideoTrackSource* source;
-    VideoTrackInterface* track;
-    RtpSenderInterface* sender;
+    rtc::scoped_refptr<FrameGeneratorCapturerVideoTrackSource> source;
+    rtc::scoped_refptr<VideoTrackInterface> track;
+    rtc::scoped_refptr<RtpSenderInterface> sender;
   };
 
   PeerScenarioClient(