Update pc/ to not use implicit conversion from scoped_refptr<T> to T*.

Bug: webrtc:13464
Change-Id: I768646af8ded6338ef51486b8d69db1ad71e9a2c
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/259500
Reviewed-by: Henrik Boström <hbos@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#36588}
diff --git a/api/scoped_refptr.h b/api/scoped_refptr.h
index 8f45f89..a967960 100644
--- a/api/scoped_refptr.h
+++ b/api/scoped_refptr.h
@@ -104,6 +104,7 @@
   }
 
   T* get() const { return ptr_; }
+  explicit operator bool() const { return ptr_ != nullptr; }
   operator T*() const { return ptr_; }
   T& operator*() const { return *ptr_; }
   T* operator->() const { return ptr_; }
diff --git a/pc/audio_rtp_receiver.cc b/pc/audio_rtp_receiver.cc
index 58f4dfa..065814b 100644
--- a/pc/audio_rtp_receiver.cc
+++ b/pc/audio_rtp_receiver.cc
@@ -251,7 +251,7 @@
       }
     }
     if (removed) {
-      existing_stream->RemoveTrack(track_);
+      existing_stream->RemoveTrack(track_.get());
     }
   }
   // Add remote track to any streams that are new.
@@ -265,7 +265,7 @@
       }
     }
     if (added) {
-      stream->AddTrack(track_);
+      stream->AddTrack(track_.get());
     }
   }
   streams_ = streams;
diff --git a/pc/jsep_transport.cc b/pc/jsep_transport.cc
index 00447b0..b6fb189 100644
--- a/pc/jsep_transport.cc
+++ b/pc/jsep_transport.cc
@@ -202,7 +202,7 @@
   if (!local_fp) {
     local_certificate_ = nullptr;
   } else {
-    error = VerifyCertificateFingerprint(local_certificate_, local_fp);
+    error = VerifyCertificateFingerprint(local_certificate_.get(), local_fp);
     if (!error.ok()) {
       local_description_.reset();
       return error;
diff --git a/pc/media_stream_observer.cc b/pc/media_stream_observer.cc
index 28caccf..6264a76 100644
--- a/pc/media_stream_observer.cc
+++ b/pc/media_stream_observer.cc
@@ -54,7 +54,7 @@
             [cached_track](const AudioTrackVector::value_type& new_track) {
               return new_track->id() == cached_track->id();
             })) {
-      audio_track_removed_callback_(cached_track.get(), stream_);
+      audio_track_removed_callback_(cached_track.get(), stream_.get());
     }
   }
 
@@ -65,7 +65,7 @@
             [new_track](const AudioTrackVector::value_type& cached_track) {
               return new_track->id() == cached_track->id();
             })) {
-      audio_track_added_callback_(new_track.get(), stream_);
+      audio_track_added_callback_(new_track.get(), stream_.get());
     }
   }
 
@@ -76,7 +76,7 @@
             [cached_track](const VideoTrackVector::value_type& new_track) {
               return new_track->id() == cached_track->id();
             })) {
-      video_track_removed_callback_(cached_track.get(), stream_);
+      video_track_removed_callback_(cached_track.get(), stream_.get());
     }
   }
 
@@ -87,7 +87,7 @@
             [new_track](const VideoTrackVector::value_type& cached_track) {
               return new_track->id() == cached_track->id();
             })) {
-      video_track_added_callback_(new_track.get(), stream_);
+      video_track_added_callback_(new_track.get(), stream_.get());
     }
   }
 
diff --git a/pc/media_stream_observer.h b/pc/media_stream_observer.h
index 4c4f221..83bbd20 100644
--- a/pc/media_stream_observer.h
+++ b/pc/media_stream_observer.h
@@ -34,7 +34,7 @@
           video_track_removed_callback);
   ~MediaStreamObserver() override;
 
-  const MediaStreamInterface* stream() const { return stream_; }
+  const MediaStreamInterface* stream() const { return stream_.get(); }
 
   void OnChanged() override;
 
diff --git a/pc/media_stream_unittest.cc b/pc/media_stream_unittest.cc
index 1d69356..7224c51 100644
--- a/pc/media_stream_unittest.cc
+++ b/pc/media_stream_unittest.cc
@@ -66,10 +66,10 @@
     ASSERT_TRUE(audio_track_.get() != NULL);
     EXPECT_EQ(MediaStreamTrackInterface::kLive, audio_track_->state());
 
-    EXPECT_TRUE(stream_->AddTrack(video_track_));
-    EXPECT_FALSE(stream_->AddTrack(video_track_));
-    EXPECT_TRUE(stream_->AddTrack(audio_track_));
-    EXPECT_FALSE(stream_->AddTrack(audio_track_));
+    EXPECT_TRUE(stream_->AddTrack(video_track_.get()));
+    EXPECT_FALSE(stream_->AddTrack(video_track_.get()));
+    EXPECT_TRUE(stream_->AddTrack(audio_track_.get()));
+    EXPECT_FALSE(stream_->AddTrack(audio_track_.get()));
   }
 
   void ChangeTrack(MediaStreamTrackInterface* track) {
@@ -118,17 +118,17 @@
 }
 
 TEST_F(MediaStreamTest, RemoveTrack) {
-  MockObserver observer(stream_);
+  MockObserver observer(stream_.get());
 
   EXPECT_CALL(observer, OnChanged()).Times(Exactly(2));
 
-  EXPECT_TRUE(stream_->RemoveTrack(audio_track_));
-  EXPECT_FALSE(stream_->RemoveTrack(audio_track_));
+  EXPECT_TRUE(stream_->RemoveTrack(audio_track_.get()));
+  EXPECT_FALSE(stream_->RemoveTrack(audio_track_.get()));
   EXPECT_EQ(0u, stream_->GetAudioTracks().size());
   EXPECT_EQ(0u, stream_->GetAudioTracks().size());
 
-  EXPECT_TRUE(stream_->RemoveTrack(video_track_));
-  EXPECT_FALSE(stream_->RemoveTrack(video_track_));
+  EXPECT_TRUE(stream_->RemoveTrack(video_track_.get()));
+  EXPECT_FALSE(stream_->RemoveTrack(video_track_.get()));
 
   EXPECT_EQ(0u, stream_->GetVideoTracks().size());
   EXPECT_EQ(0u, stream_->GetVideoTracks().size());
diff --git a/pc/peer_connection.cc b/pc/peer_connection.cc
index e789179..bf8bd96 100644
--- a/pc/peer_connection.cc
+++ b/pc/peer_connection.cc
@@ -644,7 +644,7 @@
   stats_collector_ = RTCStatsCollector::Create(this);
 
   sdp_handler_ = SdpOfferAnswerHandler::Create(this, configuration,
-                                               dependencies, context_);
+                                               dependencies, context_.get());
 
   rtp_manager_ = std::make_unique<RtpTransmissionManager>(
       IsUnifiedPlan(), signaling_thread(), worker_thread(), channel_manager(),
@@ -858,7 +858,7 @@
     LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_STATE,
                          "PeerConnection is closed.");
   }
-  if (rtp_manager()->FindSenderForTrack(track)) {
+  if (rtp_manager()->FindSenderForTrack(track.get())) {
     LOG_AND_RETURN_ERROR(
         RTCErrorType::INVALID_PARAMETER,
         "Sender already exists for track " + track->id() + ".");
@@ -866,7 +866,7 @@
   auto sender_or_error = rtp_manager()->AddTrack(track, stream_ids);
   if (sender_or_error.ok()) {
     sdp_handler_->UpdateNegotiationNeeded();
-    stats_->AddTrack(track);
+    stats_->AddTrack(track.get());
   }
   return sender_or_error;
 }
@@ -898,11 +898,11 @@
     bool removed;
     if (sender->media_type() == cricket::MEDIA_TYPE_AUDIO) {
       removed = rtp_manager()->GetAudioTransceiver()->internal()->RemoveSender(
-          sender);
+          sender.get());
     } else {
       RTC_DCHECK_EQ(cricket::MEDIA_TYPE_VIDEO, sender->media_type());
       removed = rtp_manager()->GetVideoTransceiver()->internal()->RemoveSender(
-          sender);
+          sender.get());
     }
     if (!removed) {
       LOG_AND_RETURN_ERROR(
diff --git a/pc/peer_connection_adaptation_integrationtest.cc b/pc/peer_connection_adaptation_integrationtest.cc
index b5a5f52..5dc26e0 100644
--- a/pc/peer_connection_adaptation_integrationtest.cc
+++ b/pc/peer_connection_adaptation_integrationtest.cc
@@ -65,7 +65,7 @@
           periodic_track_source_config, /* remote */ false);
   TrackWithPeriodicSource track_with_source;
   track_with_source.track =
-      factory->CreateVideoTrack("PeriodicTrack", periodic_track_source);
+      factory->CreateVideoTrack("PeriodicTrack", periodic_track_source.get());
   track_with_source.periodic_track_source = periodic_track_source;
   return track_with_source;
 }
diff --git a/pc/peer_connection_crypto_unittest.cc b/pc/peer_connection_crypto_unittest.cc
index 15a0472..d87d0b0 100644
--- a/pc/peer_connection_crypto_unittest.cc
+++ b/pc/peer_connection_crypto_unittest.cc
@@ -110,7 +110,7 @@
       return nullptr;
     }
 
-    observer->SetPeerConnectionInterface(result.value());
+    observer->SetPeerConnectionInterface(result.value().get());
     return std::make_unique<PeerConnectionWrapper>(
         pc_factory_, result.MoveValue(), std::move(observer));
   }
@@ -662,10 +662,10 @@
         rtc::make_ref_counted<MockCreateSessionDescriptionObserver>();
     observers.push_back(observer);
     if (sdp_type_ == SdpType::kOffer) {
-      pc->pc()->CreateOffer(observer,
+      pc->pc()->CreateOffer(observer.get(),
                             PeerConnectionInterface::RTCOfferAnswerOptions());
     } else {
-      pc->pc()->CreateAnswer(observer,
+      pc->pc()->CreateAnswer(observer.get(),
                              PeerConnectionInterface::RTCOfferAnswerOptions());
     }
   }
diff --git a/pc/peer_connection_data_channel_unittest.cc b/pc/peer_connection_data_channel_unittest.cc
index 05268b3..3bb2088 100644
--- a/pc/peer_connection_data_channel_unittest.cc
+++ b/pc/peer_connection_data_channel_unittest.cc
@@ -144,7 +144,7 @@
       return nullptr;
     }
 
-    observer->SetPeerConnectionInterface(result.value());
+    observer->SetPeerConnectionInterface(result.value().get());
     auto wrapper = std::make_unique<PeerConnectionWrapperForDataChannelTest>(
         pc_factory, result.MoveValue(), std::move(observer));
     wrapper->set_sctp_transport_factory(fake_sctp_transport_factory);
diff --git a/pc/peer_connection_end_to_end_unittest.cc b/pc/peer_connection_end_to_end_unittest.cc
index 27b8902..08e3cb1 100644
--- a/pc/peer_connection_end_to_end_unittest.cc
+++ b/pc/peer_connection_end_to_end_unittest.cc
@@ -509,14 +509,16 @@
   Negotiate();
   WaitForConnection();
 
-  WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
-  WaitForDataChannelsToOpen(callee_dc, caller_signaled_data_channels_, 0);
+  WaitForDataChannelsToOpen(caller_dc.get(), callee_signaled_data_channels_, 0);
+  WaitForDataChannelsToOpen(callee_dc.get(), caller_signaled_data_channels_, 0);
 
-  TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[0]);
-  TestDataChannelSendAndReceive(callee_dc, caller_signaled_data_channels_[0]);
+  TestDataChannelSendAndReceive(caller_dc.get(),
+                                callee_signaled_data_channels_[0].get());
+  TestDataChannelSendAndReceive(callee_dc.get(),
+                                caller_signaled_data_channels_[0].get());
 
-  CloseDataChannels(caller_dc, callee_signaled_data_channels_, 0);
-  CloseDataChannels(callee_dc, caller_signaled_data_channels_, 0);
+  CloseDataChannels(caller_dc.get(), callee_signaled_data_channels_, 0);
+  CloseDataChannels(callee_dc.get(), caller_signaled_data_channels_, 0);
 }
 
 // Verifies that a DataChannel created after the negotiation can transition to
@@ -534,7 +536,7 @@
   WaitForConnection();
 
   // Wait for the data channel created pre-negotiation to be opened.
-  WaitForDataChannelsToOpen(dummy, callee_signaled_data_channels_, 0);
+  WaitForDataChannelsToOpen(dummy.get(), callee_signaled_data_channels_, 0);
 
   // Create new DataChannels after the negotiation and verify their states.
   rtc::scoped_refptr<DataChannelInterface> caller_dc(
@@ -542,14 +544,16 @@
   rtc::scoped_refptr<DataChannelInterface> callee_dc(
       callee_->CreateDataChannel("hello", init));
 
-  WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1);
-  WaitForDataChannelsToOpen(callee_dc, caller_signaled_data_channels_, 0);
+  WaitForDataChannelsToOpen(caller_dc.get(), callee_signaled_data_channels_, 1);
+  WaitForDataChannelsToOpen(callee_dc.get(), caller_signaled_data_channels_, 0);
 
-  TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1]);
-  TestDataChannelSendAndReceive(callee_dc, caller_signaled_data_channels_[0]);
+  TestDataChannelSendAndReceive(caller_dc.get(),
+                                callee_signaled_data_channels_[1].get());
+  TestDataChannelSendAndReceive(callee_dc.get(),
+                                caller_signaled_data_channels_[0].get());
 
-  CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1);
-  CloseDataChannels(callee_dc, caller_signaled_data_channels_, 0);
+  CloseDataChannels(caller_dc.get(), callee_signaled_data_channels_, 1);
+  CloseDataChannels(callee_dc.get(), caller_signaled_data_channels_, 0);
 }
 
 // Verifies that a DataChannel created can transfer large messages.
@@ -566,7 +570,7 @@
   WaitForConnection();
 
   // Wait for the data channel created pre-negotiation to be opened.
-  WaitForDataChannelsToOpen(dummy, callee_signaled_data_channels_, 0);
+  WaitForDataChannelsToOpen(dummy.get(), callee_signaled_data_channels_, 0);
 
   // Create new DataChannels after the negotiation and verify their states.
   rtc::scoped_refptr<DataChannelInterface> caller_dc(
@@ -574,16 +578,16 @@
   rtc::scoped_refptr<DataChannelInterface> callee_dc(
       callee_->CreateDataChannel("hello", init));
 
-  WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1);
-  WaitForDataChannelsToOpen(callee_dc, caller_signaled_data_channels_, 0);
+  WaitForDataChannelsToOpen(caller_dc.get(), callee_signaled_data_channels_, 1);
+  WaitForDataChannelsToOpen(callee_dc.get(), caller_signaled_data_channels_, 0);
 
-  TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1],
-                                256 * 1024);
-  TestDataChannelSendAndReceive(callee_dc, caller_signaled_data_channels_[0],
-                                256 * 1024);
+  TestDataChannelSendAndReceive(
+      caller_dc.get(), callee_signaled_data_channels_[1].get(), 256 * 1024);
+  TestDataChannelSendAndReceive(
+      callee_dc.get(), caller_signaled_data_channels_[0].get(), 256 * 1024);
 
-  CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1);
-  CloseDataChannels(callee_dc, caller_signaled_data_channels_, 0);
+  CloseDataChannels(caller_dc.get(), callee_signaled_data_channels_, 1);
+  CloseDataChannels(callee_dc.get(), caller_signaled_data_channels_, 0);
 }
 
 // Verifies that DataChannel IDs are even/odd based on the DTLS roles.
@@ -628,14 +632,18 @@
 
   Negotiate();
   WaitForConnection();
-  WaitForDataChannelsToOpen(caller_dc_1, callee_signaled_data_channels_, 0);
-  WaitForDataChannelsToOpen(caller_dc_2, callee_signaled_data_channels_, 1);
+  WaitForDataChannelsToOpen(caller_dc_1.get(), callee_signaled_data_channels_,
+                            0);
+  WaitForDataChannelsToOpen(caller_dc_2.get(), callee_signaled_data_channels_,
+                            1);
 
   std::unique_ptr<webrtc::MockDataChannelObserver> dc_1_observer(
-      new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[0]));
+      new webrtc::MockDataChannelObserver(
+          callee_signaled_data_channels_[0].get()));
 
   std::unique_ptr<webrtc::MockDataChannelObserver> dc_2_observer(
-      new webrtc::MockDataChannelObserver(callee_signaled_data_channels_[1]));
+      new webrtc::MockDataChannelObserver(
+          callee_signaled_data_channels_[1].get()));
 
   const std::string message_1 = "hello 1";
   const std::string message_2 = "hello 2";
@@ -666,7 +674,7 @@
   Negotiate();
   WaitForConnection();
 
-  WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
+  WaitForDataChannelsToOpen(caller_dc.get(), callee_signaled_data_channels_, 0);
   int first_channel_id = caller_dc->id();
   // Wait for the local side to say it's closed, but not the remote side.
   // Previously, the channel on which Close is called reported being closed
@@ -676,13 +684,14 @@
 
   // Create a new channel and ensure it works after closing the previous one.
   caller_dc = caller_->CreateDataChannel("data2", init);
-  WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1);
+  WaitForDataChannelsToOpen(caller_dc.get(), callee_signaled_data_channels_, 1);
   // Since the second channel was created after the first finished closing, it
   // should be able to re-use the first one's ID.
   EXPECT_EQ(first_channel_id, caller_dc->id());
-  TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1]);
+  TestDataChannelSendAndReceive(caller_dc.get(),
+                                callee_signaled_data_channels_[1].get());
 
-  CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1);
+  CloseDataChannels(caller_dc.get(), callee_signaled_data_channels_, 1);
 }
 
 // Similar to the above test, but don't wait for the first channel to finish
@@ -699,20 +708,21 @@
   Negotiate();
   WaitForConnection();
 
-  WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
+  WaitForDataChannelsToOpen(caller_dc.get(), callee_signaled_data_channels_, 0);
   int first_channel_id = caller_dc->id();
   caller_dc->Close();
 
   // Immediately create a new channel, before waiting for the previous one to
   // transition to "closed".
   caller_dc = caller_->CreateDataChannel("data2", init);
-  WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 1);
+  WaitForDataChannelsToOpen(caller_dc.get(), callee_signaled_data_channels_, 1);
   // Since the second channel was created while the first was still closing,
   // it should have been assigned a different ID.
   EXPECT_NE(first_channel_id, caller_dc->id());
-  TestDataChannelSendAndReceive(caller_dc, callee_signaled_data_channels_[1]);
+  TestDataChannelSendAndReceive(caller_dc.get(),
+                                callee_signaled_data_channels_[1].get());
 
-  CloseDataChannels(caller_dc, callee_signaled_data_channels_, 1);
+  CloseDataChannels(caller_dc.get(), callee_signaled_data_channels_, 1);
 }
 
 // This tests that if a data channel is closed remotely while not referenced
@@ -730,7 +740,7 @@
   Negotiate();
   WaitForConnection();
 
-  WaitForDataChannelsToOpen(caller_dc, callee_signaled_data_channels_, 0);
+  WaitForDataChannelsToOpen(caller_dc.get(), callee_signaled_data_channels_, 0);
   // This removes the reference to the remote data channel that we hold.
   callee_signaled_data_channels_.clear();
   caller_dc->Close();
diff --git a/pc/peer_connection_factory_unittest.cc b/pc/peer_connection_factory_unittest.cc
index 1768bed..442a331 100644
--- a/pc/peer_connection_factory_unittest.cc
+++ b/pc/peer_connection_factory_unittest.cc
@@ -472,9 +472,9 @@
 
   ASSERT_TRUE(source.get() != NULL);
   rtc::scoped_refptr<VideoTrackInterface> track(
-      factory_->CreateVideoTrack("testlabel", source));
+      factory_->CreateVideoTrack("testlabel", source.get()));
   ASSERT_TRUE(track.get() != NULL);
-  FakeVideoTrackRenderer local_renderer(track);
+  FakeVideoTrackRenderer local_renderer(track.get());
 
   EXPECT_EQ(0, local_renderer.num_rendered_frames());
   source->InjectFrame(frame_source.GetFrame());
diff --git a/pc/peer_connection_header_extension_unittest.cc b/pc/peer_connection_header_extension_unittest.cc
index 34e0cd2..ba78dd8 100644
--- a/pc/peer_connection_header_extension_unittest.cc
+++ b/pc/peer_connection_header_extension_unittest.cc
@@ -106,7 +106,7 @@
     auto result = pc_factory->CreatePeerConnectionOrError(
         config, std::move(pc_dependencies));
     EXPECT_TRUE(result.ok());
-    observer->SetPeerConnectionInterface(result.value());
+    observer->SetPeerConnectionInterface(result.value().get());
     return std::make_unique<PeerConnectionWrapper>(
         pc_factory, result.MoveValue(), std::move(observer));
   }
diff --git a/pc/peer_connection_histogram_unittest.cc b/pc/peer_connection_histogram_unittest.cc
index 8837ab0..84525b3 100644
--- a/pc/peer_connection_histogram_unittest.cc
+++ b/pc/peer_connection_histogram_unittest.cc
@@ -352,7 +352,7 @@
       return nullptr;
     }
 
-    observer->SetPeerConnectionInterface(result.value());
+    observer->SetPeerConnectionInterface(result.value().get());
     auto wrapper = std::make_unique<PeerConnectionWrapperForUsageHistogramTest>(
         pc_factory, result.MoveValue(), std::move(observer));
     return wrapper;
diff --git a/pc/peer_connection_ice_unittest.cc b/pc/peer_connection_ice_unittest.cc
index bd619bb..5676837 100644
--- a/pc/peer_connection_ice_unittest.cc
+++ b/pc/peer_connection_ice_unittest.cc
@@ -175,7 +175,7 @@
       return nullptr;
     }
 
-    observer->SetPeerConnectionInterface(result.value());
+    observer->SetPeerConnectionInterface(result.value().get());
     auto wrapper = std::make_unique<PeerConnectionWrapperForIceTest>(
         pc_factory_, result.MoveValue(), std::move(observer));
     wrapper->set_network(fake_network);
@@ -809,7 +809,7 @@
   // Chain an operation that will block AddIceCandidate() from executing.
   auto answer_observer =
       rtc::make_ref_counted<MockCreateSessionDescriptionObserver>();
-  callee->pc()->CreateAnswer(answer_observer, RTCOfferAnswerOptions());
+  callee->pc()->CreateAnswer(answer_observer.get(), RTCOfferAnswerOptions());
 
   auto jsep_candidate =
       callee->CreateJsepCandidateForFirstTransport(&candidate);
@@ -857,7 +857,7 @@
   // Chain an operation that will block AddIceCandidate() from executing.
   auto answer_observer =
       rtc::make_ref_counted<MockCreateSessionDescriptionObserver>();
-  callee->pc()->CreateAnswer(answer_observer, RTCOfferAnswerOptions());
+  callee->pc()->CreateAnswer(answer_observer.get(), RTCOfferAnswerOptions());
 
   auto jsep_candidate =
       callee->CreateJsepCandidateForFirstTransport(&candidate);
diff --git a/pc/peer_connection_integrationtest.cc b/pc/peer_connection_integrationtest.cc
index ef817ba..7866e07 100644
--- a/pc/peer_connection_integrationtest.cc
+++ b/pc/peer_connection_integrationtest.cc
@@ -578,7 +578,8 @@
       ASSERT_EQ(2U, transceivers.size());
       ASSERT_EQ(cricket::MEDIA_TYPE_VIDEO,
                 transceivers[1]->receiver()->media_type());
-      transceivers[1]->sender()->SetTrack(caller()->CreateLocalVideoTrack());
+      transceivers[1]->sender()->SetTrack(
+          caller()->CreateLocalVideoTrack().get());
       transceivers[1]->SetDirectionWithError(
           RtpTransceiverDirection::kSendRecv);
     });
@@ -1333,8 +1334,9 @@
   for (const auto& receiver : callee()->pc()->GetReceivers()) {
     // We received frames, so we definitely should have nonzero "received bytes"
     // stats at this point.
-    EXPECT_GT(callee()->OldGetStatsForTrack(receiver->track())->BytesReceived(),
-              0);
+    EXPECT_GT(
+        callee()->OldGetStatsForTrack(receiver->track().get())->BytesReceived(),
+        0);
   }
 }
 
@@ -1355,8 +1357,8 @@
 
   // The callee received frames, so we definitely should have nonzero "sent
   // bytes" stats at this point.
-  EXPECT_GT(caller()->OldGetStatsForTrack(audio_track)->BytesSent(), 0);
-  EXPECT_GT(caller()->OldGetStatsForTrack(video_track)->BytesSent(), 0);
+  EXPECT_GT(caller()->OldGetStatsForTrack(audio_track.get())->BytesSent(), 0);
+  EXPECT_GT(caller()->OldGetStatsForTrack(video_track.get())->BytesSent(), 0);
 }
 
 // Test that we can get capture start ntp time.
@@ -1379,10 +1381,9 @@
 
   // Get the audio output level stats. Note that the level is not available
   // until an RTCP packet has been received.
-  EXPECT_TRUE_WAIT(
-      callee()->OldGetStatsForTrack(remote_audio_track)->CaptureStartNtpTime() >
-          0,
-      2 * kMaxWaitForFramesMs);
+  EXPECT_TRUE_WAIT(callee()->OldGetStatsForTrack(remote_audio_track.get())
+                           ->CaptureStartNtpTime() > 0,
+                   2 * kMaxWaitForFramesMs);
 }
 
 // Test that the track ID is associated with all local and remote SSRC stats
@@ -2434,10 +2435,14 @@
   EXPECT_EQ_WAIT(webrtc::PeerConnectionInterface::kIceConnectionConnected,
                  callee()->ice_connection_state(), kMaxWaitForFramesMs);
   // Now set the tracks, and expect frames to immediately start flowing.
-  EXPECT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
-  EXPECT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
-  EXPECT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
-  EXPECT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
+  EXPECT_TRUE(
+      caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack().get()));
+  EXPECT_TRUE(
+      caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack().get()));
+  EXPECT_TRUE(
+      callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack().get()));
+  EXPECT_TRUE(
+      callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack().get()));
   MediaExpectations media_expectations;
   media_expectations.ExpectBidirectionalAudioAndVideo();
   ASSERT_TRUE(ExpectNewFrames(media_expectations));
@@ -2473,10 +2478,14 @@
   // Now set the tracks, and expect frames to immediately start flowing.
   auto callee_audio_sender = callee()->pc()->GetSenders()[0];
   auto callee_video_sender = callee()->pc()->GetSenders()[1];
-  ASSERT_TRUE(caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack()));
-  ASSERT_TRUE(caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack()));
-  ASSERT_TRUE(callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack()));
-  ASSERT_TRUE(callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack()));
+  ASSERT_TRUE(
+      caller_audio_sender->SetTrack(caller()->CreateLocalAudioTrack().get()));
+  ASSERT_TRUE(
+      caller_video_sender->SetTrack(caller()->CreateLocalVideoTrack().get()));
+  ASSERT_TRUE(
+      callee_audio_sender->SetTrack(callee()->CreateLocalAudioTrack().get()));
+  ASSERT_TRUE(
+      callee_video_sender->SetTrack(callee()->CreateLocalVideoTrack().get()));
   MediaExpectations media_expectations;
   media_expectations.ExpectBidirectionalAudioAndVideo();
   ASSERT_TRUE(ExpectNewFrames(media_expectations));
@@ -2841,7 +2850,7 @@
   ASSERT_TRUE(wrapper);
   wrapper->CreateDataChannel();
   auto observer = rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
-  wrapper->pc()->SetLocalDescription(observer,
+  wrapper->pc()->SetLocalDescription(observer.get(),
                                      wrapper->CreateOfferAndWait().release());
 }
 
@@ -3326,7 +3335,7 @@
   caller()->AddVideoTrack();
   callee()->AddVideoTrack();
   auto observer = rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
-  callee()->pc()->SetLocalDescription(observer,
+  callee()->pc()->SetLocalDescription(observer.get(),
                                       callee()->CreateOfferAndWait().release());
   EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
   caller()->CreateAndSetAndSignalOffer();  // Implicit rollback.
@@ -3344,7 +3353,7 @@
 
   auto sld_observer =
       rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
-  callee()->pc()->SetLocalDescription(sld_observer,
+  callee()->pc()->SetLocalDescription(sld_observer.get(),
                                       callee()->CreateOfferAndWait().release());
   EXPECT_TRUE_WAIT(sld_observer->called(), kDefaultTimeout);
   EXPECT_EQ(sld_observer->error(), "");
@@ -3352,7 +3361,7 @@
   auto srd_observer =
       rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
   callee()->pc()->SetRemoteDescription(
-      srd_observer, caller()->CreateOfferAndWait().release());
+      srd_observer.get(), caller()->CreateOfferAndWait().release());
   EXPECT_TRUE_WAIT(srd_observer->called(), kDefaultTimeout);
   EXPECT_EQ(srd_observer->error(), "");
 
diff --git a/pc/peer_connection_interface_unittest.cc b/pc/peer_connection_interface_unittest.cc
index 245c0f4..104e3f4 100644
--- a/pc/peer_connection_interface_unittest.cc
+++ b/pc/peer_connection_interface_unittest.cc
@@ -549,14 +549,14 @@
       rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
           webrtc::AudioTrack::Create(kAudioTracks[i * tracks_per_stream + j],
                                      nullptr));
-      stream->AddTrack(audio_track);
+      stream->AddTrack(audio_track.get());
 
       // Add a local video track.
       rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
           webrtc::VideoTrack::Create(kVideoTracks[i * tracks_per_stream + j],
                                      webrtc::FakeVideoTrackSource::Create(),
                                      rtc::Thread::Current()));
-      stream->AddTrack(video_track);
+      stream->AddTrack(video_track.get());
     }
 
     local_collection->AddStream(stream);
@@ -810,13 +810,14 @@
   }
 
   void ReleasePeerConnection() {
-    pc_ = NULL;
-    observer_.SetPeerConnectionInterface(NULL);
+    pc_ = nullptr;
+    observer_.SetPeerConnectionInterface(nullptr);
   }
 
   rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
       const std::string& label) {
-    return pc_factory_->CreateVideoTrack(label, FakeVideoTrackSource::Create());
+    return pc_factory_->CreateVideoTrack(label,
+                                         FakeVideoTrackSource::Create().get());
   }
 
   void AddVideoTrack(const std::string& track_label,
@@ -829,8 +830,8 @@
   void AddVideoStream(const std::string& label) {
     rtc::scoped_refptr<MediaStreamInterface> stream(
         pc_factory_->CreateLocalMediaStream(label));
-    stream->AddTrack(CreateVideoTrack(label + "v0"));
-    ASSERT_TRUE(pc_->AddStream(stream));
+    stream->AddTrack(CreateVideoTrack(label + "v0").get());
+    ASSERT_TRUE(pc_->AddStream(stream.get()));
   }
 
   rtc::scoped_refptr<AudioTrackInterface> CreateAudioTrack(
@@ -848,8 +849,8 @@
   void AddAudioStream(const std::string& label) {
     rtc::scoped_refptr<MediaStreamInterface> stream(
         pc_factory_->CreateLocalMediaStream(label));
-    stream->AddTrack(CreateAudioTrack(label + "a0"));
-    ASSERT_TRUE(pc_->AddStream(stream));
+    stream->AddTrack(CreateAudioTrack(label + "a0").get());
+    ASSERT_TRUE(pc_->AddStream(stream.get()));
   }
 
   void AddAudioVideoStream(const std::string& stream_id,
@@ -858,9 +859,9 @@
     // Create a local stream.
     rtc::scoped_refptr<MediaStreamInterface> stream(
         pc_factory_->CreateLocalMediaStream(stream_id));
-    stream->AddTrack(CreateAudioTrack(audio_track_label));
-    stream->AddTrack(CreateVideoTrack(video_track_label));
-    ASSERT_TRUE(pc_->AddStream(stream));
+    stream->AddTrack(CreateAudioTrack(audio_track_label).get());
+    stream->AddTrack(CreateVideoTrack(video_track_label).get());
+    ASSERT_TRUE(pc_->AddStream(stream.get()));
   }
 
   rtc::scoped_refptr<RtpReceiverInterface> GetFirstReceiverOfType(
@@ -879,9 +880,11 @@
     auto observer =
         rtc::make_ref_counted<MockCreateSessionDescriptionObserver>();
     if (offer) {
-      pc_->CreateOffer(observer, options ? *options : RTCOfferAnswerOptions());
+      pc_->CreateOffer(observer.get(),
+                       options ? *options : RTCOfferAnswerOptions());
     } else {
-      pc_->CreateAnswer(observer, options ? *options : RTCOfferAnswerOptions());
+      pc_->CreateAnswer(observer.get(),
+                        options ? *options : RTCOfferAnswerOptions());
     }
     EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
     *desc = observer->MoveDescription();
@@ -903,9 +906,9 @@
       bool local) {
     auto observer = rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
     if (local) {
-      pc_->SetLocalDescription(observer, desc.release());
+      pc_->SetLocalDescription(observer.get(), desc.release());
     } else {
-      pc_->SetRemoteDescription(observer, desc.release());
+      pc_->SetRemoteDescription(observer.get(), desc.release());
     }
     if (pc_->signaling_state() != PeerConnectionInterface::kClosed) {
       EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
@@ -928,7 +931,7 @@
   // be required.
   bool DoGetStats(MediaStreamTrackInterface* track) {
     auto observer = rtc::make_ref_counted<MockStatsObserver>();
-    if (!pc_->GetStats(observer, track,
+    if (!pc_->GetStats(observer.get(), track,
                        PeerConnectionInterface::kStatsOutputLevelStandard))
       return false;
     EXPECT_TRUE_WAIT(observer->called(), kTimeout);
@@ -939,7 +942,7 @@
   bool DoGetRTCStats() {
     auto callback =
         rtc::make_ref_counted<webrtc::MockRTCStatsCollectorCallback>();
-    pc_->GetStats(callback);
+    pc_->GetStats(callback.get());
     EXPECT_TRUE_WAIT(callback->called(), kTimeout);
     return callback->called();
   }
@@ -963,12 +966,12 @@
     const cricket::MediaContentDescription* desc =
         cricket::GetFirstAudioContentDescription(
             pc_->remote_description()->description());
-    ASSERT_TRUE(desc != NULL);
+    ASSERT_TRUE(desc != nullptr);
     EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
 
     desc = cricket::GetFirstVideoContentDescription(
         pc_->remote_description()->description());
-    ASSERT_TRUE(desc != NULL);
+    ASSERT_TRUE(desc != nullptr);
     EXPECT_GT(desc->rtp_header_extensions().size(), 0u);
   }
 
@@ -1115,21 +1118,21 @@
     if (number_of_audio_tracks > 0) {
       sdp_ms1 += std::string(kSdpStringAudio);
       sdp_ms1 += std::string(kSdpStringMs1Audio0);
-      AddAudioTrack(kAudioTracks[0], stream);
+      AddAudioTrack(kAudioTracks[0], stream.get());
     }
     if (number_of_audio_tracks > 1) {
       sdp_ms1 += kSdpStringMs1Audio1;
-      AddAudioTrack(kAudioTracks[1], stream);
+      AddAudioTrack(kAudioTracks[1], stream.get());
     }
 
     if (number_of_video_tracks > 0) {
       sdp_ms1 += std::string(kSdpStringVideo);
       sdp_ms1 += std::string(kSdpStringMs1Video0);
-      AddVideoTrack(kVideoTracks[0], stream);
+      AddVideoTrack(kVideoTracks[0], stream.get());
     }
     if (number_of_video_tracks > 1) {
       sdp_ms1 += kSdpStringMs1Video1;
-      AddVideoTrack(kVideoTracks[1], stream);
+      AddVideoTrack(kVideoTracks[1], stream.get());
     }
 
     return std::unique_ptr<SessionDescriptionInterface>(
@@ -1140,7 +1143,7 @@
                      MediaStreamInterface* stream) {
     rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
         webrtc::AudioTrack::Create(track_id, nullptr));
-    ASSERT_TRUE(stream->AddTrack(audio_track));
+    ASSERT_TRUE(stream->AddTrack(audio_track.get()));
   }
 
   void AddVideoTrack(const std::string& track_id,
@@ -1149,7 +1152,7 @@
         webrtc::VideoTrack::Create(track_id,
                                    webrtc::FakeVideoTrackSource::Create(),
                                    rtc::Thread::Current()));
-    ASSERT_TRUE(stream->AddTrack(video_track));
+    ASSERT_TRUE(stream->AddTrack(video_track.get()));
   }
 
   std::unique_ptr<SessionDescriptionInterface> CreateOfferWithOneAudioTrack() {
@@ -1195,7 +1198,7 @@
     RTC_DCHECK(pc_);
     auto observer =
         rtc::make_ref_counted<MockCreateSessionDescriptionObserver>();
-    pc_->CreateOffer(observer, offer_answer_options);
+    pc_->CreateOffer(observer.get(), offer_answer_options);
     EXPECT_EQ_WAIT(true, observer->called(), kTimeout);
     return observer->MoveDescription();
   }
@@ -1394,7 +1397,7 @@
   auto result = pc_factory_->CreatePeerConnectionOrError(
       config, std::move(pc_dependencies));
   EXPECT_TRUE(result.ok());
-  observer_.SetPeerConnectionInterface(result.value());
+  observer_.SetPeerConnectionInterface(result.value().get());
 
   // Now validate that the config fields set above were applied to the
   // PortAllocator, as flags or otherwise.
@@ -1458,10 +1461,10 @@
   rtc::scoped_refptr<MediaStreamInterface> stream(
       pc_factory_->CreateLocalMediaStream(kStreamId3));
   rtc::scoped_refptr<AudioTrackInterface> audio_track(
-      pc_factory_->CreateAudioTrack(kStreamId3,
-                                    static_cast<AudioSourceInterface*>(NULL)));
+      pc_factory_->CreateAudioTrack(
+          kStreamId3, static_cast<AudioSourceInterface*>(nullptr)));
   stream->AddTrack(audio_track.get());
-  EXPECT_TRUE(pc_->AddStream(stream));
+  EXPECT_TRUE(pc_->AddStream(stream.get()));
   EXPECT_EQ(3u, pc_->local_streams()->count());
 
   // Remove the third stream.
@@ -1824,7 +1827,7 @@
   MediaStreamInterface* stream = pc_->local_streams()->at(0);
 
   // Remove the video track.
-  stream->RemoveTrack(stream->GetVideoTracks()[0]);
+  stream->RemoveTrack(stream->GetVideoTracks()[0].get());
 
   std::unique_ptr<SessionDescriptionInterface> offer;
   ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
@@ -1858,7 +1861,7 @@
   ASSERT_LT(0u, pc_->GetReceivers().size());
   rtc::scoped_refptr<MediaStreamTrackInterface> remote_audio =
       pc_->GetReceivers()[0]->track();
-  EXPECT_TRUE(DoGetStats(remote_audio));
+  EXPECT_TRUE(DoGetStats(remote_audio.get()));
 
   // Remove the stream. Since we are sending to our selves the local
   // and the remote stream is the same.
@@ -1868,7 +1871,7 @@
 
   // Test that we still can get statistics for the old track. Even if it is not
   // sent any longer.
-  EXPECT_TRUE(DoGetStats(remote_audio));
+  EXPECT_TRUE(DoGetStats(remote_audio.get()));
 }
 
 // Test that we can get stats on a video track.
@@ -1876,15 +1879,15 @@
   InitiateCall();
   auto video_receiver = GetFirstReceiverOfType(cricket::MEDIA_TYPE_VIDEO);
   ASSERT_TRUE(video_receiver);
-  EXPECT_TRUE(DoGetStats(video_receiver->track()));
+  EXPECT_TRUE(DoGetStats(video_receiver->track().get()));
 }
 
 // Test that we don't get statistics for an invalid track.
 TEST_P(PeerConnectionInterfaceTest, GetStatsForInvalidTrack) {
   InitiateCall();
   rtc::scoped_refptr<AudioTrackInterface> unknown_audio_track(
-      pc_factory_->CreateAudioTrack("unknown track", NULL));
-  EXPECT_FALSE(DoGetStats(unknown_audio_track));
+      pc_factory_->CreateAudioTrack("unknown track", nullptr));
+  EXPECT_FALSE(DoGetStats(unknown_audio_track.get()));
 }
 
 TEST_P(PeerConnectionInterfaceTest, GetRTCStatsBeforeAndAfterCalling) {
@@ -2048,22 +2051,22 @@
                                        webrtc::kFireFoxSdpOffer, nullptr));
   EXPECT_TRUE(DoSetSessionDescription(std::move(desc), false));
   CreateAnswerAsLocalDescription();
-  ASSERT_TRUE(pc_->local_description() != NULL);
-  ASSERT_TRUE(pc_->remote_description() != NULL);
+  ASSERT_TRUE(pc_->local_description() != nullptr);
+  ASSERT_TRUE(pc_->remote_description() != nullptr);
 
   const cricket::ContentInfo* content =
       cricket::GetFirstAudioContent(pc_->local_description()->description());
-  ASSERT_TRUE(content != NULL);
+  ASSERT_TRUE(content != nullptr);
   EXPECT_FALSE(content->rejected);
 
   content =
       cricket::GetFirstVideoContent(pc_->local_description()->description());
-  ASSERT_TRUE(content != NULL);
+  ASSERT_TRUE(content != nullptr);
   EXPECT_FALSE(content->rejected);
 #ifdef WEBRTC_HAVE_SCTP
   content =
       cricket::GetFirstDataContent(pc_->local_description()->description());
-  ASSERT_TRUE(content != NULL);
+  ASSERT_TRUE(content != nullptr);
   EXPECT_FALSE(content->rejected);
 #endif
 }
@@ -2452,13 +2455,13 @@
 
   pc_->Close();
 
-  pc_->RemoveStream(local_stream);
-  EXPECT_FALSE(pc_->AddStream(local_stream));
+  pc_->RemoveStream(local_stream.get());
+  EXPECT_FALSE(pc_->AddStream(local_stream.get()));
 
   EXPECT_FALSE(pc_->CreateDataChannelOrError("test", NULL).ok());
 
-  EXPECT_TRUE(pc_->local_description() != NULL);
-  EXPECT_TRUE(pc_->remote_description() != NULL);
+  EXPECT_TRUE(pc_->local_description() != nullptr);
+  EXPECT_TRUE(pc_->remote_description() != nullptr);
 
   std::unique_ptr<SessionDescriptionInterface> offer;
   EXPECT_FALSE(DoCreateOffer(&offer, nullptr));
@@ -2481,7 +2484,7 @@
 TEST_P(PeerConnectionInterfaceTest, CloseAndGetStats) {
   InitiateCall();
   pc_->Close();
-  DoGetStats(NULL);
+  DoGetStats(nullptr);
 }
 
 // NOTE: The series of tests below come from what used to be
@@ -2523,14 +2526,14 @@
       CreateSessionDescriptionAndReference(1, 1);
   EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1)));
   EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
-                                       reference_collection_));
+                                       reference_collection_.get()));
 
   // Add extra audio and video tracks to the same MediaStream.
   std::unique_ptr<SessionDescriptionInterface> desc_ms1_two_tracks =
       CreateSessionDescriptionAndReference(2, 2);
   EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms1_two_tracks)));
   EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
-                                       reference_collection_));
+                                       reference_collection_.get()));
   rtc::scoped_refptr<AudioTrackInterface> audio_track2 =
       observer_.remote_streams()->at(0)->GetAudioTracks()[1];
   EXPECT_EQ(webrtc::MediaStreamTrackInterface::kLive, audio_track2->state());
@@ -2541,14 +2544,14 @@
   // Remove the extra audio and video tracks.
   std::unique_ptr<SessionDescriptionInterface> desc_ms2 =
       CreateSessionDescriptionAndReference(1, 1);
-  MockTrackObserver audio_track_observer(audio_track2);
-  MockTrackObserver video_track_observer(video_track2);
+  MockTrackObserver audio_track_observer(audio_track2.get());
+  MockTrackObserver video_track_observer(video_track2.get());
 
   EXPECT_CALL(audio_track_observer, OnChanged()).Times(Exactly(1));
   EXPECT_CALL(video_track_observer, OnChanged()).Times(Exactly(1));
   EXPECT_TRUE(DoSetRemoteDescription(std::move(desc_ms2)));
   EXPECT_TRUE(CompareStreamCollections(observer_.remote_streams(),
-                                       reference_collection_));
+                                       reference_collection_.get()));
   // Track state may be updated asynchronously.
   EXPECT_EQ_WAIT(webrtc::MediaStreamTrackInterface::kEnded,
                  audio_track2->state(), kTimeout);
@@ -2611,8 +2614,8 @@
   CreatePeerConnection(config);
   CreateAndSetRemoteOffer(GetSdpStringWithStream1());
   MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
-  remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
-  remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
+  remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0].get());
+  remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0].get());
 
   std::unique_ptr<SessionDescriptionInterface> local_answer(
       webrtc::CreateSessionDescription(SdpType::kAnswer,
@@ -2696,8 +2699,8 @@
   CreatePeerConnection(config);
   CreateAndSetRemoteOffer(GetSdpStringWithStream1());
   MediaStreamInterface* remote_stream = observer_.remote_streams()->at(0);
-  remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0]);
-  remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0]);
+  remote_stream->RemoveTrack(remote_stream->GetAudioTracks()[0].get());
+  remote_stream->RemoveTrack(remote_stream->GetVideoTracks()[0].get());
 
   CreateAndSetRemoteOffer(kSdpStringWithoutStreams);
 
@@ -2918,7 +2921,7 @@
   // Change the ssrc of the audio and video track.
   cricket::MediaContentDescription* desc =
       cricket::GetFirstAudioContentDescription(modified_offer->description());
-  ASSERT_TRUE(desc != NULL);
+  ASSERT_TRUE(desc != nullptr);
   for (StreamParams& stream : desc->mutable_streams()) {
     for (unsigned int& ssrc : stream.ssrcs) {
       ++ssrc;
@@ -2927,7 +2930,7 @@
 
   desc =
       cricket::GetFirstVideoContentDescription(modified_offer->description());
-  ASSERT_TRUE(desc != NULL);
+  ASSERT_TRUE(desc != nullptr);
   for (StreamParams& stream : desc->mutable_streams()) {
     for (unsigned int& ssrc : stream.ssrcs) {
       ++ssrc;
@@ -2966,9 +2969,9 @@
   // Add a new MediaStream but with the same tracks as in the first stream.
   rtc::scoped_refptr<webrtc::MediaStreamInterface> stream_1(
       webrtc::MediaStream::Create(kStreams[1]));
-  stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0]);
-  stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0]);
-  pc_->AddStream(stream_1);
+  stream_1->AddTrack(stream_collection->at(0)->GetVideoTracks()[0].get());
+  stream_1->AddTrack(stream_collection->at(0)->GetAudioTracks()[0].get());
+  pc_->AddStream(stream_1.get());
 
   ASSERT_TRUE(DoCreateOffer(&offer, nullptr));
   EXPECT_TRUE(DoSetLocalDescription(std::move(offer)));
@@ -3576,27 +3579,27 @@
   CreatePeerConnectionWithoutDtls();
   rtc::scoped_refptr<MediaStreamInterface> stream(
       pc_factory_->CreateLocalMediaStream(kStreamId1));
-  pc_->AddStream(stream);
+  pc_->AddStream(stream.get());
   rtc::scoped_refptr<AudioTrackInterface> audio_track(
       CreateAudioTrack("audio_track"));
   rtc::scoped_refptr<VideoTrackInterface> video_track(
       CreateVideoTrack("video_track"));
-  stream->AddTrack(audio_track);
+  stream->AddTrack(audio_track.get());
   EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
   observer_.renegotiation_needed_ = false;
 
   CreateOfferReceiveAnswer();
-  stream->AddTrack(video_track);
+  stream->AddTrack(video_track.get());
   EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
   observer_.renegotiation_needed_ = false;
 
   CreateOfferReceiveAnswer();
-  stream->RemoveTrack(audio_track);
+  stream->RemoveTrack(audio_track.get());
   EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
   observer_.renegotiation_needed_ = false;
 
   CreateOfferReceiveAnswer();
-  stream->RemoveTrack(video_track);
+  stream->RemoveTrack(video_track.get());
   EXPECT_TRUE_WAIT(observer_.renegotiation_needed_, kTimeout);
   observer_.renegotiation_needed_ = false;
 }
@@ -3663,7 +3666,7 @@
     auto result =
         pcf_->CreatePeerConnectionOrError(config, std::move(pc_dependencies));
     EXPECT_TRUE(result.ok());
-    observer_.SetPeerConnectionInterface(result.value());
+    observer_.SetPeerConnectionInterface(result.value().get());
     return result.value()->GetConfiguration().media_config;
   }
 
@@ -3679,7 +3682,7 @@
   auto result =
       pcf_->CreatePeerConnectionOrError(config, std::move(pc_dependencies));
   EXPECT_TRUE(result.ok());
-  observer_.SetPeerConnectionInterface(result.value());
+  observer_.SetPeerConnectionInterface(result.value().get());
   result.value()->Close();  // No abort -> ok.
   SUCCEED();
 }
diff --git a/pc/peer_connection_jsep_unittest.cc b/pc/peer_connection_jsep_unittest.cc
index d244b31..2700629 100644
--- a/pc/peer_connection_jsep_unittest.cc
+++ b/pc/peer_connection_jsep_unittest.cc
@@ -123,7 +123,7 @@
       return nullptr;
     }
 
-    observer->SetPeerConnectionInterface(result.value());
+    observer->SetPeerConnectionInterface(result.value().get());
     return std::make_unique<PeerConnectionWrapper>(
         pc_factory, result.MoveValue(), std::move(observer));
   }
@@ -1363,7 +1363,7 @@
 
   auto caller = CreatePeerConnection();
   auto transceiver = caller->AddTransceiver(cricket::MEDIA_TYPE_AUDIO);
-  transceiver->sender()->SetTrack(caller->CreateAudioTrack(kTrackId));
+  transceiver->sender()->SetTrack(caller->CreateAudioTrack(kTrackId).get());
 
   auto offer = caller->CreateOffer();
   auto contents = offer->description()->contents();
diff --git a/pc/peer_connection_message_handler.cc b/pc/peer_connection_message_handler.cc
index 77db3e4..1d3dcf8 100644
--- a/pc/peer_connection_message_handler.cc
+++ b/pc/peer_connection_message_handler.cc
@@ -117,7 +117,7 @@
     case MSG_GETSTATS: {
       GetStatsMsg* param = static_cast<GetStatsMsg*>(msg->pdata);
       StatsReports reports;
-      param->stats->GetStats(param->track, &reports);
+      param->stats->GetStats(param->track.get(), &reports);
       param->observer->OnComplete(reports);
       delete param;
       break;
diff --git a/pc/peer_connection_rampup_tests.cc b/pc/peer_connection_rampup_tests.cc
index dc5a11f..e1ebebb 100644
--- a/pc/peer_connection_rampup_tests.cc
+++ b/pc/peer_connection_rampup_tests.cc
@@ -123,14 +123,15 @@
     video_track_sources_.back()->Start();
     return rtc::scoped_refptr<VideoTrackInterface>(
         pc_factory()->CreateVideoTrack(rtc::CreateRandomUuid(),
-                                       video_track_sources_.back()));
+                                       video_track_sources_.back().get()));
   }
 
   rtc::scoped_refptr<AudioTrackInterface> CreateLocalAudioTrack(
       const cricket::AudioOptions options) {
     rtc::scoped_refptr<AudioSourceInterface> source =
         pc_factory()->CreateAudioSource(options);
-    return pc_factory()->CreateAudioTrack(rtc::CreateRandomUuid(), source);
+    return pc_factory()->CreateAudioTrack(rtc::CreateRandomUuid(),
+                                          source.get());
   }
 
  private:
diff --git a/pc/peer_connection_rtp_unittest.cc b/pc/peer_connection_rtp_unittest.cc
index 19073d2..80a4a46 100644
--- a/pc/peer_connection_rtp_unittest.cc
+++ b/pc/peer_connection_rtp_unittest.cc
@@ -138,7 +138,7 @@
     auto result = pc_factory_->CreatePeerConnectionOrError(
         config, PeerConnectionDependencies(observer.get()));
     EXPECT_TRUE(result.ok());
-    observer->SetPeerConnectionInterface(result.value());
+    observer->SetPeerConnectionInterface(result.value().get());
     return std::make_unique<PeerConnectionWrapper>(
         pc_factory_, result.MoveValue(), std::move(observer));
   }
@@ -921,7 +921,7 @@
       rtc::make_ref_counted<webrtc::MockSetSessionDescriptionObserver>();
 
   auto offer = caller->CreateOfferAndSetAsLocal();
-  callee->pc()->SetRemoteDescription(observer, offer.release());
+  callee->pc()->SetRemoteDescription(observer.get(), offer.release());
   callee = nullptr;
   rtc::Thread::Current()->ProcessMessages(0);
   EXPECT_FALSE(observer->called());
@@ -1989,7 +1989,7 @@
   EXPECT_TRUE(sender1->SetTrack(nullptr));
   auto sender2 = caller->AddTrack(track);
   EXPECT_TRUE(sender2);
-  EXPECT_TRUE(sender1->SetTrack(track));
+  EXPECT_TRUE(sender1->SetTrack(track.get()));
 
   if (sdp_semantics_ == SdpSemantics::kPlanB_DEPRECATED) {
     // TODO(hbos): When https://crbug.com/webrtc/8734 is resolved, this should
diff --git a/pc/peer_connection_signaling_unittest.cc b/pc/peer_connection_signaling_unittest.cc
index 87fb9bb..54c49af 100644
--- a/pc/peer_connection_signaling_unittest.cc
+++ b/pc/peer_connection_signaling_unittest.cc
@@ -153,7 +153,7 @@
       return nullptr;
     }
 
-    observer->SetPeerConnectionInterface(result.value());
+    observer->SetPeerConnectionInterface(result.value().get());
     return std::make_unique<PeerConnectionWrapperForSignalingTest>(
         pc_factory_, result.MoveValue(), std::move(observer));
   }
@@ -571,7 +571,7 @@
   rtc::scoped_refptr<MockCreateSessionDescriptionObserver> observers[100];
   for (auto& observer : observers) {
     observer = rtc::make_ref_counted<MockCreateSessionDescriptionObserver>();
-    caller->pc()->CreateOffer(observer, options);
+    caller->pc()->CreateOffer(observer.get(), options);
   }
 
   // Destroy the PeerConnection.
@@ -593,7 +593,7 @@
   auto caller = CreatePeerConnection();
   auto observer = rtc::make_ref_counted<MockCreateSessionDescriptionObserver>();
   caller->pc()->Close();
-  caller->pc()->CreateOffer(observer, RTCOfferAnswerOptions());
+  caller->pc()->CreateOffer(observer.get(), RTCOfferAnswerOptions());
   caller.reset(nullptr);
   EXPECT_TRUE(observer->called());
 }
@@ -680,7 +680,7 @@
   auto offer = caller->CreateOffer(RTCOfferAnswerOptions());
 
   auto observer = MockSetSessionDescriptionObserver::Create();
-  caller->pc()->SetLocalDescription(observer, offer.release());
+  caller->pc()->SetLocalDescription(observer.get(), offer.release());
   // The old observer is not invoked immediately.
   EXPECT_FALSE(observer->called());
   // Process all currently pending messages by waiting for a posted task to run.
@@ -720,7 +720,7 @@
   // Synchronously invoke CreateOffer() and SetRemoteDescription(). The
   // SetRemoteDescription() operation should be chained to be executed
   // asynchronously, when CreateOffer() completes.
-  callee->pc()->CreateOffer(offer_observer, RTCOfferAnswerOptions());
+  callee->pc()->CreateOffer(offer_observer.get(), RTCOfferAnswerOptions());
   callee->pc()->SetRemoteDescription(
       std::move(offer),
       rtc::make_ref_counted<FakeSetRemoteDescriptionObserver>());
@@ -802,7 +802,7 @@
   auto callee_set_remote_description_observer =
       MockSetSessionDescriptionObserver::Create();
   callee->pc()->SetRemoteDescription(
-      callee_set_remote_description_observer,
+      callee_set_remote_description_observer.get(),
       CloneSessionDescription(caller->pc()->pending_local_description())
           .release());
 
@@ -822,7 +822,7 @@
   auto caller_set_remote_description_observer =
       MockSetSessionDescriptionObserver::Create();
   caller->pc()->SetRemoteDescription(
-      caller_set_remote_description_observer,
+      caller_set_remote_description_observer.get(),
       CloneSessionDescription(callee->pc()->current_local_description())
           .release());
   EXPECT_TRUE_WAIT(caller_set_remote_description_observer->called(),
@@ -1126,7 +1126,7 @@
   // waiting for it would not ensure synchronicity.
   RTC_DCHECK(!caller->pc()->GetTransceivers()[0]->mid().has_value());
   caller->pc()->SetLocalDescription(
-      rtc::make_ref_counted<MockSetSessionDescriptionObserver>(),
+      rtc::make_ref_counted<MockSetSessionDescriptionObserver>().get(),
       offer.release());
   EXPECT_TRUE(caller->pc()->GetTransceivers()[0]->mid().has_value());
 }
@@ -1162,11 +1162,12 @@
             // operation executed immediately.
             RTC_DCHECK(!pc->GetTransceivers()[0]->mid().has_value());
             pc->SetLocalDescription(
-                rtc::make_ref_counted<MockSetSessionDescriptionObserver>(),
+                rtc::make_ref_counted<MockSetSessionDescriptionObserver>()
+                    .get(),
                 desc);
             EXPECT_TRUE(pc->GetTransceivers()[0]->mid().has_value());
           });
-  caller->pc()->CreateOffer(offer_observer, RTCOfferAnswerOptions());
+  caller->pc()->CreateOffer(offer_observer.get(), RTCOfferAnswerOptions());
   EXPECT_TRUE_WAIT(offer_observer->was_called(), kWaitTimeout);
 }
 
@@ -1253,7 +1254,7 @@
   EXPECT_TRUE(caller->observer()->has_negotiation_needed_event());
 
   auto observer = rtc::make_ref_counted<MockCreateSessionDescriptionObserver>();
-  caller->pc()->CreateOffer(observer, RTCOfferAnswerOptions());
+  caller->pc()->CreateOffer(observer.get(), RTCOfferAnswerOptions());
   // For this test to work, the operation has to be pending, i.e. the observer
   // has not yet been invoked.
   EXPECT_FALSE(observer->called());
diff --git a/pc/peer_connection_simulcast_unittest.cc b/pc/peer_connection_simulcast_unittest.cc
index e732b65..e704aeb 100644
--- a/pc/peer_connection_simulcast_unittest.cc
+++ b/pc/peer_connection_simulcast_unittest.cc
@@ -140,7 +140,7 @@
     auto result =
         pc_factory_->CreatePeerConnectionOrError(config, std::move(pcd));
     EXPECT_TRUE(result.ok());
-    observer->SetPeerConnectionInterface(result.value());
+    observer->SetPeerConnectionInterface(result.value().get());
     return result.MoveValue();
   }
 
diff --git a/pc/peer_connection_wrapper.cc b/pc/peer_connection_wrapper.cc
index 6a7a30e..e23b0f5f 100644
--- a/pc/peer_connection_wrapper.cc
+++ b/pc/peer_connection_wrapper.cc
@@ -135,7 +135,7 @@
     rtc::FunctionView<void(CreateSessionDescriptionObserver*)> fn,
     std::string* error_out) {
   auto observer = rtc::make_ref_counted<MockCreateSessionDescriptionObserver>();
-  fn(observer);
+  fn(observer.get());
   EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
   if (error_out && !observer->result()) {
     *error_out = observer->error();
@@ -179,7 +179,7 @@
     rtc::FunctionView<void(SetSessionDescriptionObserver*)> fn,
     std::string* error_out) {
   auto observer = rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
-  fn(observer);
+  fn(observer.get());
   EXPECT_EQ_WAIT(true, observer->called(), kDefaultTimeout);
   if (error_out && !observer->result()) {
     *error_out = observer->error();
@@ -277,7 +277,8 @@
 
 rtc::scoped_refptr<VideoTrackInterface> PeerConnectionWrapper::CreateVideoTrack(
     const std::string& label) {
-  return pc_factory()->CreateVideoTrack(label, FakeVideoTrackSource::Create());
+  return pc_factory()->CreateVideoTrack(label,
+                                        FakeVideoTrackSource::Create().get());
 }
 
 rtc::scoped_refptr<RtpSenderInterface> PeerConnectionWrapper::AddTrack(
@@ -329,7 +330,7 @@
 rtc::scoped_refptr<const webrtc::RTCStatsReport>
 PeerConnectionWrapper::GetStats() {
   auto callback = rtc::make_ref_counted<MockRTCStatsCollectorCallback>();
-  pc()->GetStats(callback);
+  pc()->GetStats(callback.get());
   EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
   return callback->report();
 }
diff --git a/pc/rtc_stats_collector.cc b/pc/rtc_stats_collector.cc
index 6f10334..537582f 100644
--- a/pc/rtc_stats_collector.cc
+++ b/pc/rtc_stats_collector.cc
@@ -1893,7 +1893,8 @@
       inbound_audio->track_id =
           RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
               kReceiver,
-              track_media_info_map.GetAttachmentIdByTrack(audio_track).value());
+              track_media_info_map.GetAttachmentIdByTrack(audio_track.get())
+                  .value());
     }
     inbound_audio->transport_id = transport_id;
     // Remote-outbound.
@@ -1924,7 +1925,8 @@
         track_media_info_map.GetAudioTrack(voice_sender_info);
     if (audio_track) {
       int attachment_id =
-          track_media_info_map.GetAttachmentIdByTrack(audio_track).value();
+          track_media_info_map.GetAttachmentIdByTrack(audio_track.get())
+              .value();
       outbound_audio->track_id =
           RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
                                                                attachment_id);
@@ -1985,7 +1987,8 @@
       inbound_video->track_id =
           RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(
               kReceiver,
-              track_media_info_map.GetAttachmentIdByTrack(video_track).value());
+              track_media_info_map.GetAttachmentIdByTrack(video_track.get())
+                  .value());
     }
     inbound_video->transport_id = transport_id;
     report->AddStats(std::move(inbound_video));
@@ -2007,7 +2010,8 @@
         track_media_info_map.GetVideoTrack(video_sender_info);
     if (video_track) {
       int attachment_id =
-          track_media_info_map.GetAttachmentIdByTrack(video_track).value();
+          track_media_info_map.GetAttachmentIdByTrack(video_track.get())
+              .value();
       outbound_video->track_id =
           RTCMediaStreamTrackStatsIDFromDirectionAndAttachment(kSender,
                                                                attachment_id);
diff --git a/pc/rtc_stats_collector_unittest.cc b/pc/rtc_stats_collector_unittest.cc
index 1438615..910f0a4 100644
--- a/pc/rtc_stats_collector_unittest.cc
+++ b/pc/rtc_stats_collector_unittest.cc
@@ -427,8 +427,8 @@
       rtc::scoped_refptr<FakePeerConnectionForStats> pc)
       : pc_(pc),
         stats_collector_(
-            RTCStatsCollector::Create(pc, 50 * rtc::kNumMicrosecsPerMillisec)) {
-  }
+            RTCStatsCollector::Create(pc.get(),
+                                      50 * rtc::kNumMicrosecsPerMillisec)) {}
 
   rtc::scoped_refptr<RTCStatsCollector> stats_collector() {
     return stats_collector_;
@@ -631,7 +631,7 @@
  private:
   rtc::scoped_refptr<const RTCStatsReport> WaitForReport(
       rtc::scoped_refptr<RTCStatsObtainer> callback) {
-    EXPECT_TRUE_WAIT(callback->report(), kGetStatsReportTimeoutMs);
+    EXPECT_TRUE_WAIT(callback->report() != nullptr, kGetStatsReportTimeoutMs);
     int64_t after = rtc::TimeUTCMicros();
     for (const RTCStats& stats : *callback->report()) {
       if (stats.type() == RTCRemoteInboundRtpStreamStats::kType ||
@@ -920,7 +920,7 @@
 TEST_F(RTCStatsCollectorTest, SingleCallback) {
   rtc::scoped_refptr<const RTCStatsReport> result;
   stats_->stats_collector()->GetStatsReport(RTCStatsObtainer::Create(&result));
-  EXPECT_TRUE_WAIT(result, kGetStatsReportTimeoutMs);
+  EXPECT_TRUE_WAIT(result != nullptr, kGetStatsReportTimeoutMs);
 }
 
 TEST_F(RTCStatsCollectorTest, MultipleCallbacks) {
@@ -928,9 +928,9 @@
   stats_->stats_collector()->GetStatsReport(RTCStatsObtainer::Create(&a));
   stats_->stats_collector()->GetStatsReport(RTCStatsObtainer::Create(&b));
   stats_->stats_collector()->GetStatsReport(RTCStatsObtainer::Create(&c));
-  EXPECT_TRUE_WAIT(a, kGetStatsReportTimeoutMs);
-  EXPECT_TRUE_WAIT(b, kGetStatsReportTimeoutMs);
-  EXPECT_TRUE_WAIT(c, kGetStatsReportTimeoutMs);
+  EXPECT_TRUE_WAIT(a != nullptr, kGetStatsReportTimeoutMs);
+  EXPECT_TRUE_WAIT(b != nullptr, kGetStatsReportTimeoutMs);
+  EXPECT_TRUE_WAIT(c != nullptr, kGetStatsReportTimeoutMs);
 
   EXPECT_EQ(a.get(), b.get());
   EXPECT_EQ(b.get(), c.get());
@@ -959,9 +959,9 @@
   // Cache is invalidated after 50 ms.
   fake_clock_.AdvanceTime(TimeDelta::Millis(51));
   stats_->stats_collector()->GetStatsReport(RTCStatsObtainer::Create(&c));
-  EXPECT_TRUE_WAIT(a, kGetStatsReportTimeoutMs);
-  EXPECT_TRUE_WAIT(b, kGetStatsReportTimeoutMs);
-  EXPECT_TRUE_WAIT(c, kGetStatsReportTimeoutMs);
+  EXPECT_TRUE_WAIT(a != nullptr, kGetStatsReportTimeoutMs);
+  EXPECT_TRUE_WAIT(b != nullptr, kGetStatsReportTimeoutMs);
+  EXPECT_TRUE_WAIT(c != nullptr, kGetStatsReportTimeoutMs);
   EXPECT_EQ(a.get(), b.get());
   // The act of doing `AdvanceTime` processes all messages. If this was not the
   // case we might not require `c` to be fresher than `b`.
@@ -1748,10 +1748,10 @@
   rtc::scoped_refptr<const RTCStatsReport> report = stats_->GetStatsReport();
 
   RTCMediaStreamStats expected_local_stream(
-      IdForType<RTCMediaStreamStats>(report), report->timestamp_us());
+      IdForType<RTCMediaStreamStats>(report.get()), report->timestamp_us());
   expected_local_stream.stream_identifier = local_stream->id();
   expected_local_stream.track_ids = {
-      IdForType<RTCMediaStreamTrackStats>(report)};
+      IdForType<RTCMediaStreamTrackStats>(report.get())};
   ASSERT_TRUE(report->Get(expected_local_stream.id()))
       << "Did not find " << expected_local_stream.id() << " in "
       << report->ToJson();
@@ -1760,7 +1760,7 @@
       report->Get(expected_local_stream.id())->cast_to<RTCMediaStreamStats>());
 
   RTCMediaStreamTrackStats expected_local_audio_track_ssrc1(
-      IdForType<RTCMediaStreamTrackStats>(report), report->timestamp_us(),
+      IdForType<RTCMediaStreamTrackStats>(report.get()), report->timestamp_us(),
       RTCMediaStreamTrackKind::kAudio);
   expected_local_audio_track_ssrc1.track_identifier = local_audio_track->id();
   expected_local_audio_track_ssrc1.media_source_id =
@@ -1819,10 +1819,10 @@
   rtc::scoped_refptr<const RTCStatsReport> report = stats_->GetStatsReport();
 
   RTCMediaStreamStats expected_remote_stream(
-      IdForType<RTCMediaStreamStats>(report), report->timestamp_us());
+      IdForType<RTCMediaStreamStats>(report.get()), report->timestamp_us());
   expected_remote_stream.stream_identifier = remote_stream->id();
-  expected_remote_stream.track_ids =
-      std::vector<std::string>({IdForType<RTCMediaStreamTrackStats>(report)});
+  expected_remote_stream.track_ids = std::vector<std::string>(
+      {IdForType<RTCMediaStreamTrackStats>(report.get())});
   ASSERT_TRUE(report->Get(expected_remote_stream.id()))
       << "Did not find " << expected_remote_stream.id() << " in "
       << report->ToJson();
@@ -1831,7 +1831,7 @@
       report->Get(expected_remote_stream.id())->cast_to<RTCMediaStreamStats>());
 
   RTCMediaStreamTrackStats expected_remote_audio_track(
-      IdForType<RTCMediaStreamTrackStats>(report), report->timestamp_us(),
+      IdForType<RTCMediaStreamTrackStats>(report.get()), report->timestamp_us(),
       RTCMediaStreamTrackKind::kAudio);
   expected_remote_audio_track.track_identifier = remote_audio_track->id();
   // `expected_remote_audio_track.media_source_id` should be undefined
@@ -2168,7 +2168,7 @@
   expected_video.ssrc = 1;
   expected_video.media_type = "video";
   expected_video.kind = "video";
-  expected_video.track_id = IdForType<RTCMediaStreamTrackStats>(report);
+  expected_video.track_id = IdForType<RTCMediaStreamTrackStats>(report.get());
   expected_video.transport_id = "RTCTransport_TransportName_1";
   expected_video.codec_id = "RTCCodec_VideoMid_Inbound_42";
   expected_video.fir_count = 5;
@@ -2259,7 +2259,7 @@
   expected_audio.ssrc = 1;
   expected_audio.media_type = "audio";
   expected_audio.kind = "audio";
-  expected_audio.track_id = IdForType<RTCMediaStreamTrackStats>(report);
+  expected_audio.track_id = IdForType<RTCMediaStreamTrackStats>(report.get());
   expected_audio.transport_id = "RTCTransport_TransportName_1";
   expected_audio.codec_id = "RTCCodec_AudioMid_Outbound_42";
   expected_audio.packets_sent = 2;
@@ -2661,7 +2661,7 @@
   expected_audio.ssrc = 1;
   expected_audio.media_type = "audio";
   expected_audio.kind = "audio";
-  expected_audio.track_id = IdForType<RTCMediaStreamTrackStats>(report);
+  expected_audio.track_id = IdForType<RTCMediaStreamTrackStats>(report.get());
   expected_audio.transport_id = "RTCTransport_TransportName_1";
   expected_audio.codec_id = "RTCCodec_AudioMid_Outbound_42";
   expected_audio.packets_sent = 2;
@@ -3200,7 +3200,7 @@
   rtc::scoped_refptr<const RTCStatsReport> report = stats_->GetStatsReport();
 
   RTCMediaStreamTrackStats expected_local_audio_track_ssrc1(
-      IdForType<RTCMediaStreamTrackStats>(report), report->timestamp_us(),
+      IdForType<RTCMediaStreamTrackStats>(report.get()), report->timestamp_us(),
       RTCMediaStreamTrackKind::kAudio);
   expected_local_audio_track_ssrc1.track_identifier = local_audio_track->id();
   expected_local_audio_track_ssrc1.media_source_id =
@@ -3493,7 +3493,8 @@
 TEST(RTCStatsCollectorTestWithFakeCollector, ThreadUsageAndResultsMerging) {
   auto pc = rtc::make_ref_counted<FakePeerConnectionForStats>();
   rtc::scoped_refptr<FakeRTCStatsCollector> stats_collector(
-      FakeRTCStatsCollector::Create(pc, 50 * rtc::kNumMicrosecsPerMillisec));
+      FakeRTCStatsCollector::Create(pc.get(),
+                                    50 * rtc::kNumMicrosecsPerMillisec));
   stats_collector->VerifyThreadUsageAndResultsMerging();
 }
 
diff --git a/pc/rtc_stats_integrationtest.cc b/pc/rtc_stats_integrationtest.cc
index 085f063..21acf3a 100644
--- a/pc/rtc_stats_integrationtest.cc
+++ b/pc/rtc_stats_integrationtest.cc
@@ -174,8 +174,8 @@
       PeerConnectionInterface* pc) {
     rtc::scoped_refptr<RTCStatsObtainer> stats_obtainer =
         RTCStatsObtainer::Create();
-    pc->GetStats(stats_obtainer);
-    EXPECT_TRUE_WAIT(stats_obtainer->report(), kGetStatsTimeoutMs);
+    pc->GetStats(stats_obtainer.get());
+    EXPECT_TRUE_WAIT(stats_obtainer->report() != nullptr, kGetStatsTimeoutMs);
     return stats_obtainer->report();
   }
 
@@ -186,7 +186,7 @@
     rtc::scoped_refptr<RTCStatsObtainer> stats_obtainer =
         RTCStatsObtainer::Create();
     pc->GetStats(selector, stats_obtainer);
-    EXPECT_TRUE_WAIT(stats_obtainer->report(), kGetStatsTimeoutMs);
+    EXPECT_TRUE_WAIT(stats_obtainer->report() != nullptr, kGetStatsTimeoutMs);
     return stats_obtainer->report();
   }
 
@@ -432,7 +432,7 @@
   }
 
   bool VerifyRTCCertificateStats(const RTCCertificateStats& certificate) {
-    RTCStatsVerifier verifier(report_, &certificate);
+    RTCStatsVerifier verifier(report_.get(), &certificate);
     verifier.TestMemberIsDefined(certificate.fingerprint);
     verifier.TestMemberIsDefined(certificate.fingerprint_algorithm);
     verifier.TestMemberIsDefined(certificate.base64_certificate);
@@ -442,7 +442,7 @@
   }
 
   bool VerifyRTCCodecStats(const RTCCodecStats& codec) {
-    RTCStatsVerifier verifier(report_, &codec);
+    RTCStatsVerifier verifier(report_.get(), &codec);
     verifier.TestMemberIsIDReference(codec.transport_id,
                                      RTCTransportStats::kType);
     verifier.TestMemberIsDefined(codec.payload_type);
@@ -460,7 +460,7 @@
   }
 
   bool VerifyRTCDataChannelStats(const RTCDataChannelStats& data_channel) {
-    RTCStatsVerifier verifier(report_, &data_channel);
+    RTCStatsVerifier verifier(report_.get(), &data_channel);
     verifier.TestMemberIsDefined(data_channel.label);
     verifier.TestMemberIsDefined(data_channel.protocol);
     verifier.TestMemberIsDefined(data_channel.data_channel_identifier);
@@ -475,7 +475,7 @@
   bool VerifyRTCIceCandidatePairStats(
       const RTCIceCandidatePairStats& candidate_pair,
       bool is_selected_pair) {
-    RTCStatsVerifier verifier(report_, &candidate_pair);
+    RTCStatsVerifier verifier(report_.get(), &candidate_pair);
     verifier.TestMemberIsIDReference(candidate_pair.transport_id,
                                      RTCTransportStats::kType);
     verifier.TestMemberIsIDReference(candidate_pair.local_candidate_id,
@@ -526,7 +526,7 @@
   }
 
   bool VerifyRTCIceCandidateStats(const RTCIceCandidateStats& candidate) {
-    RTCStatsVerifier verifier(report_, &candidate);
+    RTCStatsVerifier verifier(report_.get(), &candidate);
     verifier.TestMemberIsIDReference(candidate.transport_id,
                                      RTCTransportStats::kType);
     verifier.TestMemberIsDefined(candidate.is_remote);
@@ -561,7 +561,7 @@
   }
 
   bool VerifyRTCMediaStreamStats(const RTCMediaStreamStats& media_stream) {
-    RTCStatsVerifier verifier(report_, &media_stream);
+    RTCStatsVerifier verifier(report_.get(), &media_stream);
     verifier.TestMemberIsDefined(media_stream.stream_identifier);
     verifier.TestMemberIsIDReference(media_stream.track_ids,
                                      RTCMediaStreamTrackStats::kType);
@@ -570,7 +570,7 @@
 
   bool VerifyRTCMediaStreamTrackStats(
       const RTCMediaStreamTrackStats& media_stream_track) {
-    RTCStatsVerifier verifier(report_, &media_stream_track);
+    RTCStatsVerifier verifier(report_.get(), &media_stream_track);
     verifier.TestMemberIsDefined(media_stream_track.track_identifier);
     verifier.TestMemberIsDefined(media_stream_track.remote_source);
     verifier.TestMemberIsDefined(media_stream_track.ended);
@@ -771,7 +771,7 @@
 
   bool VerifyRTCPeerConnectionStats(
       const RTCPeerConnectionStats& peer_connection) {
-    RTCStatsVerifier verifier(report_, &peer_connection);
+    RTCStatsVerifier verifier(report_.get(), &peer_connection);
     verifier.TestMemberIsNonNegative<uint32_t>(
         peer_connection.data_channels_opened);
     verifier.TestMemberIsNonNegative<uint32_t>(
@@ -808,7 +808,7 @@
 
   bool VerifyRTCInboundRTPStreamStats(
       const RTCInboundRTPStreamStats& inbound_stream) {
-    RTCStatsVerifier verifier(report_, &inbound_stream);
+    RTCStatsVerifier verifier(report_.get(), &inbound_stream);
     VerifyRTCReceivedRtpStreamStats(inbound_stream, verifier,
                                     inbound_stream.media_type.is_defined() &&
                                         *inbound_stream.media_type == "audio");
@@ -936,7 +936,7 @@
 
   bool VerifyRTCOutboundRTPStreamStats(
       const RTCOutboundRTPStreamStats& outbound_stream) {
-    RTCStatsVerifier verifier(report_, &outbound_stream);
+    RTCStatsVerifier verifier(report_.get(), &outbound_stream);
     VerifyRTCRTPStreamStats(outbound_stream, verifier);
     if (outbound_stream.media_type.is_defined() &&
         *outbound_stream.media_type == "video") {
@@ -1048,7 +1048,7 @@
 
   bool VerifyRTCRemoteInboundRtpStreamStats(
       const RTCRemoteInboundRtpStreamStats& remote_inbound_stream) {
-    RTCStatsVerifier verifier(report_, &remote_inbound_stream);
+    RTCStatsVerifier verifier(report_.get(), &remote_inbound_stream);
     VerifyRTCReceivedRtpStreamStats(remote_inbound_stream, verifier, false);
     verifier.TestMemberIsDefined(remote_inbound_stream.fraction_lost);
     verifier.TestMemberIsIDReference(remote_inbound_stream.local_id,
@@ -1064,7 +1064,7 @@
 
   bool VerifyRTCRemoteOutboundRTPStreamStats(
       const RTCRemoteOutboundRtpStreamStats& remote_outbound_stream) {
-    RTCStatsVerifier verifier(report_, &remote_outbound_stream);
+    RTCStatsVerifier verifier(report_.get(), &remote_outbound_stream);
     VerifyRTCRTPStreamStats(remote_outbound_stream, verifier);
     VerifyRTCSentRTPStreamStats(remote_outbound_stream, verifier);
     verifier.TestMemberIsIDReference(remote_outbound_stream.local_id,
@@ -1088,7 +1088,7 @@
   }
 
   bool VerifyRTCAudioSourceStats(const RTCAudioSourceStats& audio_source) {
-    RTCStatsVerifier verifier(report_, &audio_source);
+    RTCStatsVerifier verifier(report_.get(), &audio_source);
     VerifyRTCMediaSourceStats(audio_source, &verifier);
     // Audio level, unlike audio energy, only gets updated at a certain
     // frequency, so we don't require that one to be positive to avoid a race
@@ -1106,7 +1106,7 @@
   }
 
   bool VerifyRTCVideoSourceStats(const RTCVideoSourceStats& video_source) {
-    RTCStatsVerifier verifier(report_, &video_source);
+    RTCStatsVerifier verifier(report_.get(), &video_source);
     VerifyRTCMediaSourceStats(video_source, &verifier);
     // TODO(hbos): This integration test uses fakes that doesn't support
     // VideoTrackSourceInterface::Stats. When this is fixed we should
@@ -1120,7 +1120,7 @@
   }
 
   bool VerifyRTCTransportStats(const RTCTransportStats& transport) {
-    RTCStatsVerifier verifier(report_, &transport);
+    RTCStatsVerifier verifier(report_.get(), &transport);
     verifier.TestMemberIsNonNegative<uint64_t>(transport.bytes_sent);
     verifier.TestMemberIsNonNegative<uint64_t>(transport.packets_sent);
     verifier.TestMemberIsNonNegative<uint64_t>(transport.bytes_received);
@@ -1240,7 +1240,7 @@
 
   rtc::scoped_refptr<RTCStatsObtainer> stats_obtainer =
       RTCStatsObtainer::Create();
-  caller_->pc()->GetStats(stats_obtainer);
+  caller_->pc()->GetStats(stats_obtainer.get());
   // This will destroy the peer connection.
   caller_ = nullptr;
   // Any pending stats requests should have completed in the act of destroying
@@ -1257,7 +1257,7 @@
 
   rtc::scoped_refptr<RTCStatsObtainer> stats_obtainer =
       RTCStatsObtainer::Create();
-  caller_->pc()->GetStats(stats_obtainer);
+  caller_->pc()->GetStats(stats_obtainer.get());
   caller_->pc()->Close();
 
   ASSERT_TRUE(stats_obtainer->report());
diff --git a/pc/rtp_sender.cc b/pc/rtp_sender.cc
index dc53105..6c4c578 100644
--- a/pc/rtp_sender.cc
+++ b/pc/rtp_sender.cc
@@ -651,7 +651,8 @@
       break;
   }
   bool success = worker_thread_->Invoke<bool>(RTC_FROM_HERE, [&] {
-    return video_media_channel()->SetVideoSend(ssrc_, &options, video_track());
+    return video_media_channel()->SetVideoSend(ssrc_, &options,
+                                               video_track().get());
   });
   RTC_DCHECK(success);
 }
diff --git a/pc/rtp_sender_receiver_unittest.cc b/pc/rtp_sender_receiver_unittest.cc
index 20621e4..6b3f6e0 100644
--- a/pc/rtp_sender_receiver_unittest.cc
+++ b/pc/rtp_sender_receiver_unittest.cc
@@ -210,7 +210,7 @@
         FakeVideoTrackSource::Create(is_screencast));
     video_track_ =
         VideoTrack::Create(kVideoTrackId, source, rtc::Thread::Current());
-    EXPECT_TRUE(local_stream_->AddTrack(video_track_));
+    EXPECT_TRUE(local_stream_->AddTrack(video_track_.get()));
   }
 
   void CreateAudioRtpSender() { CreateAudioRtpSender(nullptr); }
@@ -218,13 +218,13 @@
   void CreateAudioRtpSender(
       const rtc::scoped_refptr<LocalAudioSource>& source) {
     audio_track_ = AudioTrack::Create(kAudioTrackId, source);
-    EXPECT_TRUE(local_stream_->AddTrack(audio_track_));
+    EXPECT_TRUE(local_stream_->AddTrack(audio_track_.get()));
     std::unique_ptr<MockSetStreamsObserver> set_streams_observer =
         std::make_unique<MockSetStreamsObserver>();
     audio_rtp_sender_ =
         AudioRtpSender::Create(worker_thread_, audio_track_->id(), nullptr,
                                set_streams_observer.get());
-    ASSERT_TRUE(audio_rtp_sender_->SetTrack(audio_track_));
+    ASSERT_TRUE(audio_rtp_sender_->SetTrack(audio_track_.get()));
     EXPECT_CALL(*set_streams_observer, OnSetStreams());
     audio_rtp_sender_->SetStreams({local_stream_->id()});
     audio_rtp_sender_->SetMediaChannel(voice_media_channel_);
@@ -288,7 +288,7 @@
         std::make_unique<MockSetStreamsObserver>();
     video_rtp_sender_ = VideoRtpSender::Create(
         worker_thread_, video_track_->id(), set_streams_observer.get());
-    ASSERT_TRUE(video_rtp_sender_->SetTrack(video_track_));
+    ASSERT_TRUE(video_rtp_sender_->SetTrack(video_track_.get()));
     EXPECT_CALL(*set_streams_observer, OnSetStreams());
     video_rtp_sender_->SetStreams({local_stream_->id()});
     video_rtp_sender_->SetMediaChannel(video_media_channel_);
@@ -733,7 +733,7 @@
       AudioTrack::Create(kAudioTrackId, nullptr);
 
   // Track but no SSRC.
-  EXPECT_TRUE(audio_rtp_sender_->SetTrack(track));
+  EXPECT_TRUE(audio_rtp_sender_->SetTrack(track.get()));
   VerifyVoiceChannelNoInput();
 
   // SSRC but no track.
@@ -748,7 +748,7 @@
   CreateVideoRtpSenderWithNoTrack();
 
   // Track but no SSRC.
-  EXPECT_TRUE(video_rtp_sender_->SetTrack(video_track_));
+  EXPECT_TRUE(video_rtp_sender_->SetTrack(video_track_.get()));
   VerifyVideoChannelNoInput();
 
   // SSRC but no track.
@@ -764,7 +764,7 @@
   rtc::scoped_refptr<AudioTrackInterface> track =
       AudioTrack::Create(kAudioTrackId, nullptr);
   audio_rtp_sender_->SetSsrc(kAudioSsrc);
-  audio_rtp_sender_->SetTrack(track);
+  audio_rtp_sender_->SetTrack(track.get());
   VerifyVoiceChannelInput();
 
   DestroyAudioRtpSender();
@@ -776,7 +776,7 @@
   CreateAudioRtpSenderWithNoTrack();
   rtc::scoped_refptr<AudioTrackInterface> track =
       AudioTrack::Create(kAudioTrackId, nullptr);
-  audio_rtp_sender_->SetTrack(track);
+  audio_rtp_sender_->SetTrack(track.get());
   audio_rtp_sender_->SetSsrc(kAudioSsrc);
   VerifyVoiceChannelInput();
 
@@ -789,7 +789,7 @@
   AddVideoTrack();
   CreateVideoRtpSenderWithNoTrack();
   video_rtp_sender_->SetSsrc(kVideoSsrc);
-  video_rtp_sender_->SetTrack(video_track_);
+  video_rtp_sender_->SetTrack(video_track_.get());
   VerifyVideoChannelInput();
 
   DestroyVideoRtpSender();
@@ -800,7 +800,7 @@
 TEST_F(RtpSenderReceiverTest, VideoSenderEarlyWarmupTrackThenSsrc) {
   AddVideoTrack();
   CreateVideoRtpSenderWithNoTrack();
-  video_rtp_sender_->SetTrack(video_track_);
+  video_rtp_sender_->SetTrack(video_track_.get());
   video_rtp_sender_->SetSsrc(kVideoSsrc);
   VerifyVideoChannelInput();
 
@@ -897,13 +897,13 @@
 
 TEST_F(RtpSenderReceiverTest, AudioSenderInitParametersMovedAfterNegotiation) {
   audio_track_ = AudioTrack::Create(kAudioTrackId, nullptr);
-  EXPECT_TRUE(local_stream_->AddTrack(audio_track_));
+  EXPECT_TRUE(local_stream_->AddTrack(audio_track_.get()));
 
   std::unique_ptr<MockSetStreamsObserver> set_streams_observer =
       std::make_unique<MockSetStreamsObserver>();
   audio_rtp_sender_ = AudioRtpSender::Create(
       worker_thread_, audio_track_->id(), nullptr, set_streams_observer.get());
-  ASSERT_TRUE(audio_rtp_sender_->SetTrack(audio_track_));
+  ASSERT_TRUE(audio_rtp_sender_->SetTrack(audio_track_.get()));
   EXPECT_CALL(*set_streams_observer, OnSetStreams());
   audio_rtp_sender_->SetStreams({local_stream_->id()});
 
@@ -1094,7 +1094,7 @@
       std::make_unique<MockSetStreamsObserver>();
   video_rtp_sender_ = VideoRtpSender::Create(worker_thread_, video_track_->id(),
                                              set_streams_observer.get());
-  ASSERT_TRUE(video_rtp_sender_->SetTrack(video_track_));
+  ASSERT_TRUE(video_rtp_sender_->SetTrack(video_track_.get()));
   EXPECT_CALL(*set_streams_observer, OnSetStreams());
   video_rtp_sender_->SetStreams({local_stream_->id()});
 
@@ -1135,7 +1135,7 @@
       std::make_unique<MockSetStreamsObserver>();
   video_rtp_sender_ = VideoRtpSender::Create(worker_thread_, video_track_->id(),
                                              set_streams_observer.get());
-  ASSERT_TRUE(video_rtp_sender_->SetTrack(video_track_));
+  ASSERT_TRUE(video_rtp_sender_->SetTrack(video_track_.get()));
   EXPECT_CALL(*set_streams_observer, OnSetStreams());
   video_rtp_sender_->SetStreams({local_stream_->id()});
 
@@ -1524,7 +1524,7 @@
   video_track_->set_content_hint(VideoTrackInterface::ContentHint::kDetailed);
   video_rtp_sender_ = VideoRtpSender::Create(worker_thread_, video_track_->id(),
                                              set_streams_observer.get());
-  ASSERT_TRUE(video_rtp_sender_->SetTrack(video_track_));
+  ASSERT_TRUE(video_rtp_sender_->SetTrack(video_track_.get()));
   EXPECT_CALL(*set_streams_observer, OnSetStreams());
   video_rtp_sender_->SetStreams({local_stream_->id()});
   video_rtp_sender_->SetMediaChannel(video_media_channel_);
diff --git a/pc/rtp_transmission_manager.cc b/pc/rtp_transmission_manager.cc
index 538fa62..1e9443f 100644
--- a/pc/rtp_transmission_manager.cc
+++ b/pc/rtp_transmission_manager.cc
@@ -177,7 +177,7 @@
       transceiver->internal()->set_direction(
           RtpTransceiverDirection::kSendOnly);
     }
-    transceiver->sender()->SetTrack(track);
+    transceiver->sender()->SetTrack(track.get());
     transceiver->internal()->sender_internal()->set_stream_ids(stream_ids);
     transceiver->internal()->set_reused_for_addtrack(true);
   } else {
@@ -227,7 +227,7 @@
         signaling_thread(), VideoRtpSender::Create(worker_thread(), id, this));
     NoteUsageEvent(UsageEvent::VIDEO_ADDED);
   }
-  bool set_track_succeeded = sender->SetTrack(track);
+  bool set_track_succeeded = sender->SetTrack(track.get());
   RTC_DCHECK(set_track_succeeded);
   sender->internal()->set_stream_ids(stream_ids);
   sender->internal()->set_init_send_encodings(send_encodings);
@@ -409,7 +409,7 @@
                         << " doesn't exist.";
     return;
   }
-  GetAudioTransceiver()->internal()->RemoveSender(sender);
+  GetAudioTransceiver()->internal()->RemoveSender(sender.get());
 }
 
 void RtpTransmissionManager::AddVideoTrack(VideoTrackInterface* track,
@@ -448,7 +448,7 @@
                         << " doesn't exist.";
     return;
   }
-  GetVideoTransceiver()->internal()->RemoveSender(sender);
+  GetVideoTransceiver()->internal()->RemoveSender(sender.get());
 }
 
 void RtpTransmissionManager::CreateAudioReceiver(
@@ -511,9 +511,9 @@
     return nullptr;
   }
   if (receiver->media_type() == cricket::MEDIA_TYPE_AUDIO) {
-    GetAudioTransceiver()->internal()->RemoveReceiver(receiver);
+    GetAudioTransceiver()->internal()->RemoveReceiver(receiver.get());
   } else {
-    GetVideoTransceiver()->internal()->RemoveReceiver(receiver);
+    GetVideoTransceiver()->internal()->RemoveReceiver(receiver.get());
   }
   return receiver;
 }
@@ -553,7 +553,7 @@
     rtc::scoped_refptr<AudioTrackInterface> audio_track =
         stream->FindAudioTrack(sender_info.sender_id);
     if (audio_track) {
-      stream->RemoveTrack(audio_track);
+      stream->RemoveTrack(audio_track.get());
     }
   } else if (media_type == cricket::MEDIA_TYPE_VIDEO) {
     // Stopping or destroying a VideoRtpReceiver will end the
@@ -564,7 +564,7 @@
     if (video_track) {
       // There's no guarantee the track is still available, e.g. the track may
       // have been removed from the stream by an application.
-      stream->RemoveTrack(video_track);
+      stream->RemoveTrack(video_track.get());
     }
   } else {
     RTC_DCHECK_NOTREACHED() << "Invalid media type";
diff --git a/pc/scenario_tests/goog_cc_test.cc b/pc/scenario_tests/goog_cc_test.cc
index f0a30df..ea96408 100644
--- a/pc/scenario_tests/goog_cc_test.cc
+++ b/pc/scenario_tests/goog_cc_test.cc
@@ -75,7 +75,7 @@
   auto get_bwe = [&] {
     auto callback =
         rtc::make_ref_counted<webrtc::MockRTCStatsCollectorCallback>();
-    caller->pc()->GetStats(callback);
+    caller->pc()->GetStats(callback.get());
     s.net()->time_controller()->Wait([&] { return callback->called(); });
     auto stats =
         callback->report()->GetStatsOfType<RTCIceCandidatePairStats>()[0];
diff --git a/pc/sdp_offer_answer.cc b/pc/sdp_offer_answer.cc
index 1ad793c..fcec8eb 100644
--- a/pc/sdp_offer_answer.cc
+++ b/pc/sdp_offer_answer.cc
@@ -1100,10 +1100,10 @@
       return;
     if (error.ok()) {
       handler_->pc_->message_handler()->PostSetSessionDescriptionSuccess(
-          inner_observer_);
+          inner_observer_.get());
     } else {
       handler_->pc_->message_handler()->PostSetSessionDescriptionFailure(
-          inner_observer_, std::move(error));
+          inner_observer_.get(), std::move(error));
     }
   }
 
@@ -2029,7 +2029,7 @@
   // Once all processing has finished, fire off callbacks.
   auto observer = pc_->Observer();
   for (const auto& transceiver : now_receiving_transceivers) {
-    pc_->stats()->AddTrack(transceiver->receiver()->track());
+    pc_->stats()->AddTrack(transceiver->receiver()->track().get());
     observer->OnTrack(transceiver);
     observer->OnAddTrack(transceiver->receiver(),
                          transceiver->receiver()->streams());
@@ -2076,7 +2076,7 @@
           RtpTransceiverDirectionHasSend(audio_desc->direction());
       UpdateRemoteSendersList(GetActiveStreams(audio_desc),
                               default_audio_track_needed, audio_desc->type(),
-                              new_streams);
+                              new_streams.get());
     }
   }
 
@@ -2091,7 +2091,7 @@
           RtpTransceiverDirectionHasSend(video_desc->direction());
       UpdateRemoteSendersList(GetActiveStreams(video_desc),
                               default_video_track_needed, video_desc->type(),
-                              new_streams);
+                              new_streams.get());
     }
   }
 
@@ -2229,7 +2229,8 @@
     std::string error = "CreateOffer called when PeerConnection is closed.";
     RTC_LOG(LS_ERROR) << error;
     pc_->message_handler()->PostCreateSessionDescriptionFailure(
-        observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error)));
+        observer.get(),
+        RTCError(RTCErrorType::INVALID_STATE, std::move(error)));
     return;
   }
 
@@ -2239,7 +2240,7 @@
     std::string error_message = GetSessionErrorMsg();
     RTC_LOG(LS_ERROR) << "CreateOffer: " << error_message;
     pc_->message_handler()->PostCreateSessionDescriptionFailure(
-        observer,
+        observer.get(),
         RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
     return;
   }
@@ -2248,7 +2249,8 @@
     std::string error = "CreateOffer called with invalid options.";
     RTC_LOG(LS_ERROR) << error;
     pc_->message_handler()->PostCreateSessionDescriptionFailure(
-        observer, RTCError(RTCErrorType::INVALID_PARAMETER, std::move(error)));
+        observer.get(),
+        RTCError(RTCErrorType::INVALID_PARAMETER, std::move(error)));
     return;
   }
 
@@ -2258,14 +2260,15 @@
     RTCError error = HandleLegacyOfferOptions(options);
     if (!error.ok()) {
       pc_->message_handler()->PostCreateSessionDescriptionFailure(
-          observer, std::move(error));
+          observer.get(), std::move(error));
       return;
     }
   }
 
   cricket::MediaSessionOptions session_options;
   GetOptionsForOffer(options, &session_options);
-  webrtc_session_desc_factory_->CreateOffer(observer, options, session_options);
+  webrtc_session_desc_factory_->CreateOffer(observer.get(), options,
+                                            session_options);
 }
 
 void SdpOfferAnswerHandler::CreateAnswer(
@@ -2313,7 +2316,7 @@
     std::string error_message = GetSessionErrorMsg();
     RTC_LOG(LS_ERROR) << "CreateAnswer: " << error_message;
     pc_->message_handler()->PostCreateSessionDescriptionFailure(
-        observer,
+        observer.get(),
         RTCError(RTCErrorType::INTERNAL_ERROR, std::move(error_message)));
     return;
   }
@@ -2325,7 +2328,8 @@
         "have-remote-offer or have-local-pranswer.";
     RTC_LOG(LS_ERROR) << error;
     pc_->message_handler()->PostCreateSessionDescriptionFailure(
-        observer, RTCError(RTCErrorType::INVALID_STATE, std::move(error)));
+        observer.get(),
+        RTCError(RTCErrorType::INVALID_STATE, std::move(error)));
     return;
   }
 
@@ -2349,7 +2353,7 @@
 
   cricket::MediaSessionOptions session_options;
   GetOptionsForAnswer(options, &session_options);
-  webrtc_session_desc_factory_->CreateAnswer(observer, session_options);
+  webrtc_session_desc_factory_->CreateAnswer(observer.get(), session_options);
 }
 
 void SdpOfferAnswerHandler::DoSetRemoteDescription(
@@ -2775,7 +2779,7 @@
   if (pc_->IsClosed()) {
     return false;
   }
-  if (!CanAddLocalMediaStream(local_streams_, local_stream)) {
+  if (!CanAddLocalMediaStream(local_streams_.get(), local_stream)) {
     return false;
   }
 
@@ -4245,7 +4249,7 @@
   for (const auto& remote_stream : remote_streams) {
     if (remote_stream->GetAudioTracks().empty() &&
         remote_stream->GetVideoTracks().empty()) {
-      remote_streams_->RemoveStream(remote_stream);
+      remote_streams_->RemoveStream(remote_stream.get());
       removed_streams->push_back(remote_stream);
     }
   }
@@ -4373,7 +4377,7 @@
         rtp_manager()->FindSenderInfo(*current_senders, stream_id, sender_id);
     if (!sender_info) {
       current_senders->push_back(RtpSenderInfo(stream_id, sender_id, ssrc));
-      rtp_manager()->OnRemoteSenderAdded(current_senders->back(), stream,
+      rtp_manager()->OnRemoteSenderAdded(current_senders->back(), stream.get(),
                                          media_type);
     }
   }
@@ -4398,7 +4402,7 @@
       current_senders->push_back(
           RtpSenderInfo(kDefaultStreamId, default_sender_id, /*ssrc=*/0));
       rtp_manager()->OnRemoteSenderAdded(current_senders->back(),
-                                         default_stream, media_type);
+                                         default_stream.get(), media_type);
     }
   }
 }
@@ -4633,7 +4637,7 @@
   }
 
   for (auto& stream : streams_to_remove) {
-    remote_streams_->RemoveStream(stream);
+    remote_streams_->RemoveStream(stream.get());
     pc_->Observer()->OnRemoveStream(std::move(stream));
   }
 }
diff --git a/pc/sdp_offer_answer_unittest.cc b/pc/sdp_offer_answer_unittest.cc
index 2f441ef..2ce9e52 100644
--- a/pc/sdp_offer_answer_unittest.cc
+++ b/pc/sdp_offer_answer_unittest.cc
@@ -85,7 +85,7 @@
     auto result = pc_factory_->CreatePeerConnectionOrError(
         config, PeerConnectionDependencies(observer.get()));
     EXPECT_TRUE(result.ok());
-    observer->SetPeerConnectionInterface(result.value());
+    observer->SetPeerConnectionInterface(result.value().get());
     return std::make_unique<PeerConnectionWrapper>(
         pc_factory_, result.MoveValue(), std::move(observer));
   }
diff --git a/pc/stats_collector_unittest.cc b/pc/stats_collector_unittest.cc
index 61d0953..06c43c1 100644
--- a/pc/stats_collector_unittest.cc
+++ b/pc/stats_collector_unittest.cc
@@ -666,7 +666,7 @@
     const std::string kTransportName = "transport";
 
     auto pc = CreatePeerConnection();
-    auto stats = CreateStatsCollector(pc);
+    auto stats = CreateStatsCollector(pc.get());
 
     pc->AddVoiceChannel("audio", kTransportName);
 
@@ -779,10 +779,10 @@
     if (GetParam()) {
       if (!stream_)
         stream_ = MediaStream::Create("streamid");
-      stream_->AddTrack(track_);
-      stats->AddStream(stream_);
+      stream_->AddTrack(track_.get());
+      stats->AddStream(stream_.get());
     } else {
-      stats->AddTrack(track_);
+      stats->AddTrack(track_.get());
     }
     pc->AddSender(CreateMockSender(track_, kSsrcOfTrack));
   }
@@ -794,10 +794,10 @@
                                 rtc::Thread::Current());
     if (GetParam()) {
       stream_ = MediaStream::Create("streamid");
-      stream_->AddTrack(track_);
-      stats->AddStream(stream_);
+      stream_->AddTrack(track_.get());
+      stats->AddStream(stream_.get());
     } else {
-      stats->AddTrack(track_);
+      stats->AddTrack(track_.get());
     }
     pc->AddReceiver(CreateMockReceiver(track_, kSsrcOfTrack));
   }
@@ -813,10 +813,10 @@
     if (GetParam()) {
       if (!stream_)
         stream_ = MediaStream::Create("streamid");
-      stream_->AddTrack(audio_track_);
-      stats->AddStream(stream_);
+      stream_->AddTrack(audio_track_.get());
+      stats->AddStream(stream_.get());
     } else {
-      stats->AddTrack(audio_track_);
+      stats->AddTrack(audio_track_.get());
     }
     return pc->AddSender(CreateMockSender(audio_track_, kSsrcOfTrack));
   }
@@ -826,12 +826,12 @@
                              StatsCollectorForTest* stats) {
     audio_track_ = rtc::make_ref_counted<FakeAudioTrack>(kRemoteTrackId);
     if (GetParam()) {
-      if (stream_ == NULL)
+      if (stream_ == nullptr)
         stream_ = MediaStream::Create("streamid");
-      stream_->AddTrack(audio_track_);
-      stats->AddStream(stream_);
+      stream_->AddTrack(audio_track_.get());
+      stats->AddStream(stream_.get());
     } else {
-      stats->AddTrack(audio_track_);
+      stats->AddTrack(audio_track_.get());
     }
     pc->AddReceiver(CreateMockReceiver(audio_track_, kSsrcOfTrack));
   }
@@ -843,7 +843,7 @@
 
 TEST_F(StatsCollectorTest, FilterOutNegativeDataChannelId) {
   auto pc = CreatePeerConnection();
-  auto stats = CreateStatsCollector(pc);
+  auto stats = CreateStatsCollector(pc.get());
 
   pc->AddSctpDataChannel("hacks");
 
@@ -867,7 +867,7 @@
       DataChannelInterface::DataState::kConnecting);
 
   auto pc = CreatePeerConnection();
-  auto stats = CreateStatsCollector(pc);
+  auto stats = CreateStatsCollector(pc.get());
 
   InternalDataChannelInit init;
   init.id = kDataChannelId;
@@ -906,7 +906,7 @@
   constexpr int64_t kBytesSent = 12345678901234LL;
 
   auto pc = CreatePeerConnection();
-  auto stats = CreateStatsCollector(pc);
+  auto stats = CreateStatsCollector(pc.get());
 
   VideoSenderInfo video_sender_info;
   video_sender_info.add_ssrc(1234);
@@ -917,7 +917,7 @@
 
   pc->AddVideoChannel("video", "transport", video_info);
 
-  AddOutgoingVideoTrack(pc, stats.get());
+  AddOutgoingVideoTrack(pc.get(), stats.get());
 
   stats->UpdateStats(PeerConnectionInterface::kStatsOutputLevelStandard);
   StatsReports reports;
@@ -938,7 +938,7 @@
   constexpr int kPacerDelay = 123;
 
   auto pc = CreatePeerConnection();
-  auto stats = CreateStatsCollector(pc);
+  auto stats = CreateStatsCollector(pc.get());
 
   VoiceSenderInfo voice_sender_info;
   voice_sender_info.add_ssrc(1234);
@@ -950,7 +950,7 @@
   auto* voice_media_channel = pc->AddVoiceChannel("audio", "transport");
   voice_media_channel->SetStats(voice_info);
 
-  AddOutgoingAudioTrack(pc, stats.get());
+  AddOutgoingAudioTrack(pc.get(), stats.get());
 
   Call::Stats call_stats;
   call_stats.send_bandwidth_bps = kSendBandwidth;
@@ -987,7 +987,7 @@
   constexpr int kPacerDelay = 123;
 
   auto pc = CreatePeerConnection();
-  auto stats = CreateStatsCollector(pc);
+  auto stats = CreateStatsCollector(pc.get());
 
   VideoSenderInfo video_sender_info;
   video_sender_info.add_ssrc(1234);
@@ -999,7 +999,7 @@
 
   pc->AddVideoChannel("video", "transport", video_info);
 
-  AddOutgoingVideoTrack(pc, stats.get());
+  AddOutgoingVideoTrack(pc.get(), stats.get());
 
   Call::Stats call_stats;
   call_stats.send_bandwidth_bps = kSendBandwidth;
@@ -1030,7 +1030,7 @@
 // exists in the returned stats.
 TEST_F(StatsCollectorTest, SessionObjectExists) {
   auto pc = CreatePeerConnection();
-  auto stats = CreateStatsCollector(pc);
+  auto stats = CreateStatsCollector(pc.get());
 
   stats->UpdateStats(PeerConnectionInterface::kStatsOutputLevelStandard);
   StatsReports reports;
@@ -1044,7 +1044,7 @@
 // in the returned stats.
 TEST_F(StatsCollectorTest, OnlyOneSessionObjectExists) {
   auto pc = CreatePeerConnection();
-  auto stats = CreateStatsCollector(pc);
+  auto stats = CreateStatsCollector(pc.get());
 
   stats->UpdateStats(PeerConnectionInterface::kStatsOutputLevelStandard);
   stats->UpdateStats(PeerConnectionInterface::kStatsOutputLevelStandard);
@@ -1061,10 +1061,10 @@
 // without calling StatsCollector::UpdateStats.
 TEST_P(StatsCollectorTrackTest, TrackObjectExistsWithoutUpdateStats) {
   auto pc = CreatePeerConnection();
-  auto stats = CreateStatsCollector(pc);
+  auto stats = CreateStatsCollector(pc.get());
 
   pc->AddVideoChannel("video", "transport");
-  AddOutgoingVideoTrack(pc, stats.get());
+  AddOutgoingVideoTrack(pc.get(), stats.get());
 
   // Verfies the existence of the track report.
   StatsReports reports;
@@ -1085,7 +1085,7 @@
   constexpr int64_t kBytesSent = 12345678901234LL;
 
   auto pc = CreatePeerConnection();
-  auto stats = CreateStatsCollector(pc);
+  auto stats = CreateStatsCollector(pc.get());
 
   VideoSenderInfo video_sender_info;
   video_sender_info.add_ssrc(1234);
@@ -1096,7 +1096,7 @@
 
   pc->AddVideoChannel("video", "transport", video_info);
 
-  AddOutgoingVideoTrack(pc, stats.get());
+  AddOutgoingVideoTrack(pc.get(), stats.get());
 
   stats->UpdateStats(PeerConnectionInterface::kStatsOutputLevelStandard);
   StatsReports reports;
@@ -1111,7 +1111,7 @@
 
   // Get report for the specific `track`.
   reports.clear();
-  stats->GetStats(track_, &reports);
+  stats->GetStats(track_.get(), &reports);
   // `reports` should contain at least one session report, one track report,
   // and one ssrc report.
   EXPECT_LE(3u, reports.size());
@@ -1139,7 +1139,7 @@
   constexpr int64_t kBytesSent = 12345678901234LL;
 
   auto pc = CreatePeerConnection();
-  auto stats = CreateStatsCollector(pc);
+  auto stats = CreateStatsCollector(pc.get());
 
   VideoSenderInfo video_sender_info;
   video_sender_info.add_ssrc(1234);
@@ -1150,7 +1150,7 @@
 
   pc->AddVideoChannel("video", "transport", video_info);
 
-  AddOutgoingVideoTrack(pc, stats.get());
+  AddOutgoingVideoTrack(pc.get(), stats.get());
 
   stats->UpdateStats(PeerConnectionInterface::kStatsOutputLevelStandard);
   StatsReports reports;
@@ -1181,10 +1181,10 @@
 // an outgoing SSRC where remote stats are not returned.
 TEST_P(StatsCollectorTrackTest, RemoteSsrcInfoIsAbsent) {
   auto pc = CreatePeerConnection();
-  auto stats = CreateStatsCollector(pc);
+  auto stats = CreateStatsCollector(pc.get());
 
   pc->AddVideoChannel("video", "transport");
-  AddOutgoingVideoTrack(pc, stats.get());
+  AddOutgoingVideoTrack(pc.get(), stats.get());
 
   stats->UpdateStats(PeerConnectionInterface::kStatsOutputLevelStandard);
   StatsReports reports;
@@ -1199,7 +1199,7 @@
 // an outgoing SSRC where stats are returned.
 TEST_P(StatsCollectorTrackTest, RemoteSsrcInfoIsPresent) {
   auto pc = CreatePeerConnection();
-  auto stats = CreateStatsCollector(pc);
+  auto stats = CreateStatsCollector(pc.get());
 
   SsrcReceiverInfo remote_ssrc_stats;
   remote_ssrc_stats.timestamp = 12345.678;
@@ -1212,7 +1212,7 @@
 
   pc->AddVideoChannel("video", "transport", video_info);
 
-  AddOutgoingVideoTrack(pc, stats.get());
+  AddOutgoingVideoTrack(pc.get(), stats.get());
 
   stats->UpdateStats(PeerConnectionInterface::kStatsOutputLevelStandard);
   StatsReports reports;
@@ -1230,7 +1230,7 @@
   constexpr int64_t kNumOfPacketsConcealed = 54321;
 
   auto pc = CreatePeerConnection();
-  auto stats = CreateStatsCollector(pc);
+  auto stats = CreateStatsCollector(pc.get());
 
   VideoReceiverInfo video_receiver_info;
   video_receiver_info.add_ssrc(1234);
@@ -1240,7 +1240,7 @@
 
   pc->AddVideoChannel("video", "transport", video_info);
 
-  AddIncomingVideoTrack(pc, stats.get());
+  AddIncomingVideoTrack(pc.get(), stats.get());
 
   stats->UpdateStats(PeerConnectionInterface::kStatsOutputLevelStandard);
   StatsReports reports;
@@ -1279,7 +1279,7 @@
   const rtc::SocketAddress kRemoteAddress(kRemoteIp, kRemotePort);
 
   auto pc = CreatePeerConnection();
-  auto stats = CreateStatsCollector(pc);
+  auto stats = CreateStatsCollector(pc.get());
 
   cricket::Candidate local;
   EXPECT_GT(local.id().length(), 0u);
@@ -1415,7 +1415,7 @@
 // transport is present.
 TEST_F(StatsCollectorTest, NoTransport) {
   auto pc = CreatePeerConnection();
-  auto stats = CreateStatsCollector(pc);
+  auto stats = CreateStatsCollector(pc.get());
 
   // This will cause the fake PeerConnection to generate a TransportStats entry
   // but with only a single dummy TransportChannelStats.
@@ -1474,16 +1474,16 @@
   }
 
   auto pc = CreatePeerConnection();
-  auto stats = CreateStatsCollector(pc);
+  auto stats = CreateStatsCollector(pc.get());
 
   // Create a local stream with a local audio track and adds it to the stats.
   stream_ = MediaStream::Create("streamid");
   auto local_track =
       rtc::make_ref_counted<FakeAudioTrackWithInitValue>(kLocalTrackId);
-  stream_->AddTrack(local_track);
+  stream_->AddTrack(local_track.get());
   pc->AddSender(CreateMockSender(local_track, kSsrcOfTrack));
   if (GetParam()) {
-    stats->AddStream(stream_);
+    stats->AddStream(stream_.get());
   }
   stats->AddLocalAudioTrack(local_track.get(), kSsrcOfTrack);
 
@@ -1492,10 +1492,10 @@
       MediaStream::Create("remotestreamid"));
   auto remote_track =
       rtc::make_ref_counted<FakeAudioTrackWithInitValue>(kRemoteTrackId);
-  remote_stream->AddTrack(remote_track);
+  remote_stream->AddTrack(remote_track.get());
   pc->AddReceiver(CreateMockReceiver(remote_track, kSsrcOfTrack));
   if (GetParam()) {
-    stats->AddStream(remote_stream);
+    stats->AddStream(remote_stream.get());
   }
 
   VoiceSenderInfo voice_sender_info;
@@ -1561,14 +1561,15 @@
 // AudioTrackInterface::GetStats() method.
 TEST_P(StatsCollectorTrackTest, GetStatsFromLocalAudioTrack) {
   auto pc = CreatePeerConnection();
-  auto stats = CreateStatsCollector(pc);
+  auto stats = CreateStatsCollector(pc.get());
 
-  AddOutgoingAudioTrack(pc, stats.get());
-  stats->AddLocalAudioTrack(audio_track_, kSsrcOfTrack);
+  AddOutgoingAudioTrack(pc.get(), stats.get());
+  stats->AddLocalAudioTrack(audio_track_.get(), kSsrcOfTrack);
 
   VoiceSenderInfo voice_sender_info;
   InitVoiceSenderInfo(&voice_sender_info);
-  UpdateVoiceSenderInfoFromAudioTrack(audio_track_, &voice_sender_info, false);
+  UpdateVoiceSenderInfoFromAudioTrack(audio_track_.get(), &voice_sender_info,
+                                      false);
   VoiceMediaInfo voice_info;
   voice_info.senders.push_back(voice_sender_info);
 
@@ -1576,7 +1577,7 @@
   voice_media_channel->SetStats(voice_info);
 
   StatsReports reports;  // returned values.
-  VerifyAudioTrackStats(audio_track_, stats.get(), voice_info, &reports);
+  VerifyAudioTrackStats(audio_track_.get(), stats.get(), voice_info, &reports);
 
   // Verify that there is no remote report for the local audio track because
   // we did not set it up.
@@ -1589,9 +1590,9 @@
 // correctly.
 TEST_P(StatsCollectorTrackTest, GetStatsFromRemoteStream) {
   auto pc = CreatePeerConnection();
-  auto stats = CreateStatsCollector(pc);
+  auto stats = CreateStatsCollector(pc.get());
 
-  AddIncomingAudioTrack(pc, stats.get());
+  AddIncomingAudioTrack(pc.get(), stats.get());
 
   VoiceReceiverInfo voice_receiver_info;
   InitVoiceReceiverInfo(&voice_receiver_info);
@@ -1603,16 +1604,16 @@
   voice_media_channel->SetStats(voice_info);
 
   StatsReports reports;  // returned values.
-  VerifyAudioTrackStats(audio_track_, stats.get(), voice_info, &reports);
+  VerifyAudioTrackStats(audio_track_.get(), stats.get(), voice_info, &reports);
 }
 
 // This test verifies that a local stats object won't update its statistics
 // after a RemoveLocalAudioTrack() call.
 TEST_P(StatsCollectorTrackTest, GetStatsAfterRemoveAudioStream) {
   auto pc = CreatePeerConnection();
-  auto stats = CreateStatsCollector(pc);
+  auto stats = CreateStatsCollector(pc.get());
 
-  AddOutgoingAudioTrack(pc, stats.get());
+  AddOutgoingAudioTrack(pc.get(), stats.get());
   stats->AddLocalAudioTrack(audio_track_.get(), kSsrcOfTrack);
 
   VoiceSenderInfo voice_sender_info;
@@ -1651,10 +1652,10 @@
 // the same ssrc, they populate stats reports correctly.
 TEST_P(StatsCollectorTrackTest, LocalAndRemoteTracksWithSameSsrc) {
   auto pc = CreatePeerConnection();
-  auto stats = CreateStatsCollector(pc);
+  auto stats = CreateStatsCollector(pc.get());
 
   // Create a local stream with a local audio track and adds it to the stats.
-  AddOutgoingAudioTrack(pc, stats.get());
+  AddOutgoingAudioTrack(pc.get(), stats.get());
   stats->AddLocalAudioTrack(audio_track_.get(), kSsrcOfTrack);
 
   // Create a remote stream with a remote audio track and adds it to the stats.
@@ -1662,8 +1663,8 @@
       MediaStream::Create("remotestreamid"));
   auto remote_track = rtc::make_ref_counted<FakeAudioTrack>(kRemoteTrackId);
   pc->AddReceiver(CreateMockReceiver(remote_track, kSsrcOfTrack));
-  remote_stream->AddTrack(remote_track);
-  stats->AddStream(remote_stream);
+  remote_stream->AddTrack(remote_track.get());
+  stats->AddStream(remote_stream.get());
 
   VoiceSenderInfo voice_sender_info;
   InitVoiceSenderInfo(&voice_sender_info);
@@ -1723,15 +1724,16 @@
   }
 
   auto pc = CreatePeerConnection();
-  auto stats = CreateStatsCollector(pc);
+  auto stats = CreateStatsCollector(pc.get());
 
   // Create a local stream with a local audio track and adds it to the stats.
-  auto sender = AddOutgoingAudioTrack(pc, stats.get());
-  stats->AddLocalAudioTrack(audio_track_, kSsrcOfTrack);
+  auto sender = AddOutgoingAudioTrack(pc.get(), stats.get());
+  stats->AddLocalAudioTrack(audio_track_.get(), kSsrcOfTrack);
 
   VoiceSenderInfo voice_sender_info;
   InitVoiceSenderInfo(&voice_sender_info);
-  UpdateVoiceSenderInfoFromAudioTrack(audio_track_, &voice_sender_info, false);
+  UpdateVoiceSenderInfoFromAudioTrack(audio_track_.get(), &voice_sender_info,
+                                      false);
   voice_sender_info.add_ssrc(kSsrcOfTrack);
   VoiceMediaInfo voice_info;
   voice_info.senders.push_back(voice_sender_info);
@@ -1740,7 +1742,7 @@
   voice_media_channel->SetStats(voice_info);
 
   StatsReports reports;  // returned values.
-  VerifyAudioTrackStats(audio_track_, stats.get(), voice_info, &reports);
+  VerifyAudioTrackStats(audio_track_.get(), stats.get(), voice_info, &reports);
 
   // Remove the previous audio track from the stream.
   stream_->RemoveTrack(audio_track_.get());
@@ -1751,21 +1753,22 @@
   static const std::string kNewTrackId = "new_track_id";
   auto new_audio_track = rtc::make_ref_counted<FakeAudioTrack>(kNewTrackId);
   pc->AddSender(CreateMockSender(new_audio_track, kSsrcOfTrack));
-  stream_->AddTrack(new_audio_track);
+  stream_->AddTrack(new_audio_track.get());
 
-  stats->AddLocalAudioTrack(new_audio_track, kSsrcOfTrack);
+  stats->AddLocalAudioTrack(new_audio_track.get(), kSsrcOfTrack);
   stats->InvalidateCache();
 
   VoiceSenderInfo new_voice_sender_info;
   InitVoiceSenderInfo(&new_voice_sender_info);
-  UpdateVoiceSenderInfoFromAudioTrack(new_audio_track, &new_voice_sender_info,
-                                      false);
+  UpdateVoiceSenderInfoFromAudioTrack(new_audio_track.get(),
+                                      &new_voice_sender_info, false);
   VoiceMediaInfo new_voice_info;
   new_voice_info.senders.push_back(new_voice_sender_info);
   voice_media_channel->SetStats(new_voice_info);
 
   reports.clear();
-  VerifyAudioTrackStats(new_audio_track, stats.get(), new_voice_info, &reports);
+  VerifyAudioTrackStats(new_audio_track.get(), stats.get(), new_voice_info,
+                        &reports);
 }
 
 // Test that if there are two local senders with the same track then two SSRC
@@ -1776,7 +1779,7 @@
   constexpr uint32_t kSecondSsrc = 33;
 
   auto pc = CreatePeerConnection();
-  auto stats = CreateStatsCollector(pc);
+  auto stats = CreateStatsCollector(pc.get());
 
   auto local_track =
       rtc::make_ref_counted<FakeAudioTrackWithInitValue>(kLocalTrackId);
@@ -1836,9 +1839,9 @@
 // This test verifies that stats are correctly set in video send ssrc stats.
 TEST_P(StatsCollectorTrackTest, VerifyVideoSendSsrcStats) {
   auto pc = CreatePeerConnection();
-  auto stats = CreateStatsCollector(pc);
+  auto stats = CreateStatsCollector(pc.get());
 
-  AddOutgoingVideoTrack(pc, stats.get());
+  AddOutgoingVideoTrack(pc.get(), stats.get());
 
   VideoSenderInfo video_sender_info;
   video_sender_info.add_ssrc(1234);
@@ -1863,9 +1866,9 @@
 // This test verifies that stats are correctly set in video receive ssrc stats.
 TEST_P(StatsCollectorTrackTest, VerifyVideoReceiveSsrcStatsNew) {
   auto pc = CreatePeerConnection();
-  auto stats = CreateStatsCollector(pc);
+  auto stats = CreateStatsCollector(pc.get());
 
-  AddIncomingVideoTrack(pc, stats.get());
+  AddIncomingVideoTrack(pc.get(), stats.get());
 
   VideoReceiverInfo video_receiver_info;
   video_receiver_info.add_ssrc(1234);
diff --git a/pc/stream_collection.h b/pc/stream_collection.h
index ed85947..f0f3f07 100644
--- a/pc/stream_collection.h
+++ b/pc/stream_collection.h
@@ -34,14 +34,14 @@
   virtual size_t count() { return media_streams_.size(); }
 
   virtual MediaStreamInterface* at(size_t index) {
-    return media_streams_.at(index);
+    return media_streams_.at(index).get();
   }
 
   virtual MediaStreamInterface* find(const std::string& id) {
     for (StreamVector::iterator it = media_streams_.begin();
          it != media_streams_.end(); ++it) {
       if ((*it)->id().compare(id) == 0) {
-        return (*it);
+        return (*it).get();
       }
     }
     return NULL;
@@ -49,7 +49,8 @@
 
   virtual MediaStreamTrackInterface* FindAudioTrack(const std::string& id) {
     for (size_t i = 0; i < media_streams_.size(); ++i) {
-      MediaStreamTrackInterface* track = media_streams_[i]->FindAudioTrack(id);
+      MediaStreamTrackInterface* track =
+          media_streams_[i]->FindAudioTrack(id).get();
       if (track) {
         return track;
       }
@@ -59,7 +60,8 @@
 
   virtual MediaStreamTrackInterface* FindVideoTrack(const std::string& id) {
     for (size_t i = 0; i < media_streams_.size(); ++i) {
-      MediaStreamTrackInterface* track = media_streams_[i]->FindVideoTrack(id);
+      MediaStreamTrackInterface* track =
+          media_streams_[i]->FindVideoTrack(id).get();
       if (track) {
         return track;
       }
diff --git a/pc/test/fake_peer_connection_for_stats.h b/pc/test/fake_peer_connection_for_stats.h
index 7f8559d..132174e 100644
--- a/pc/test/fake_peer_connection_for_stats.h
+++ b/pc/test/fake_peer_connection_for_stats.h
@@ -180,7 +180,7 @@
   void RemoveSender(rtc::scoped_refptr<RtpSenderInterface> sender) {
     GetOrCreateFirstTransceiverOfType(sender->media_type())
         ->internal()
-        ->RemoveSender(sender);
+        ->RemoveSender(sender.get());
   }
 
   rtc::scoped_refptr<RtpReceiverInterface> AddReceiver(
@@ -198,7 +198,7 @@
   void RemoveReceiver(rtc::scoped_refptr<RtpReceiverInterface> receiver) {
     GetOrCreateFirstTransceiverOfType(receiver->media_type())
         ->internal()
-        ->RemoveReceiver(receiver);
+        ->RemoveReceiver(receiver.get());
   }
 
   FakeVoiceMediaChannelForStats* AddVoiceChannel(
diff --git a/pc/test/integration_test_helpers.h b/pc/test/integration_test_helpers.h
index c35c357..d871319 100644
--- a/pc/test/integration_test_helpers.h
+++ b/pc/test/integration_test_helpers.h
@@ -346,7 +346,7 @@
     // TODO(perkj): Test audio source when it is implemented. Currently audio
     // always use the default input.
     return peer_connection_factory_->CreateAudioTrack(rtc::CreateRandomUuid(),
-                                                      source);
+                                                      source.get());
   }
 
   rtc::scoped_refptr<webrtc::VideoTrackInterface> CreateLocalVideoTrack() {
@@ -415,7 +415,7 @@
     data_channels_.push_back(data_channel_or_error.MoveValue());
     ASSERT_TRUE(data_channels_.back().get() != nullptr);
     data_observers_.push_back(
-        std::make_unique<MockDataChannelObserver>(data_channels_.back()));
+        std::make_unique<MockDataChannelObserver>(data_channels_.back().get()));
   }
 
   // Return the last observed data channel.
@@ -423,7 +423,7 @@
     if (data_channels_.size() == 0) {
       return nullptr;
     }
-    return data_channels_.back();
+    return data_channels_.back().get();
   }
   // Return all data channels.
   const std::vector<rtc::scoped_refptr<DataChannelInterface>>& data_channels() {
@@ -466,7 +466,8 @@
       webrtc::MediaStreamTrackInterface* track) {
     auto observer = rtc::make_ref_counted<MockStatsObserver>();
     EXPECT_TRUE(peer_connection_->GetStats(
-        observer, nullptr, PeerConnectionInterface::kStatsOutputLevelStandard));
+        observer.get(), nullptr,
+        PeerConnectionInterface::kStatsOutputLevelStandard));
     EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
     return observer;
   }
@@ -481,7 +482,7 @@
   rtc::scoped_refptr<const webrtc::RTCStatsReport> NewGetStats() {
     auto callback =
         rtc::make_ref_counted<webrtc::MockRTCStatsCollectorCallback>();
-    peer_connection_->GetStats(callback);
+    peer_connection_->GetStats(callback.get());
     EXPECT_TRUE_WAIT(callback->called(), kDefaultTimeout);
     return callback->report();
   }
@@ -542,7 +543,7 @@
       ADD_FAILURE();
       return nullptr;
     }
-    return pc()->remote_streams();
+    return pc()->remote_streams().get();
   }
 
   StreamCollectionInterface* local_streams() {
@@ -550,7 +551,7 @@
       ADD_FAILURE();
       return nullptr;
     }
-    return pc()->local_streams();
+    return pc()->local_streams().get();
   }
 
   webrtc::PeerConnectionInterface::SignalingState signaling_state() {
@@ -619,8 +620,8 @@
   std::unique_ptr<SessionDescriptionInterface> CreateOfferAndWait() {
     auto observer =
         rtc::make_ref_counted<MockCreateSessionDescriptionObserver>();
-    pc()->CreateOffer(observer, offer_answer_options_);
-    return WaitForDescriptionFromObserver(observer);
+    pc()->CreateOffer(observer.get(), offer_answer_options_);
+    return WaitForDescriptionFromObserver(observer.get());
   }
   bool Rollback() {
     return SetRemoteDescription(
@@ -856,9 +857,10 @@
             config, false /* remote */));
     rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
         peer_connection_factory_->CreateVideoTrack(
-            rtc::CreateRandomUuid(), video_track_sources_.back()));
+            rtc::CreateRandomUuid(), video_track_sources_.back().get()));
     if (!local_video_renderer_) {
-      local_video_renderer_.reset(new webrtc::FakeVideoTrackRenderer(track));
+      local_video_renderer_.reset(
+          new webrtc::FakeVideoTrackRenderer(track.get()));
     }
     return track;
   }
@@ -900,8 +902,8 @@
   std::unique_ptr<SessionDescriptionInterface> CreateAnswer() {
     auto observer =
         rtc::make_ref_counted<MockCreateSessionDescriptionObserver>();
-    pc()->CreateAnswer(observer, offer_answer_options_);
-    return WaitForDescriptionFromObserver(observer);
+    pc()->CreateAnswer(observer.get(), offer_answer_options_);
+    return WaitForDescriptionFromObserver(observer.get());
   }
 
   std::unique_ptr<SessionDescriptionInterface> WaitForDescriptionFromObserver(
@@ -930,7 +932,7 @@
     std::string sdp;
     EXPECT_TRUE(desc->ToString(&sdp));
     RTC_LOG(LS_INFO) << debug_name_ << ": local SDP contents=\n" << sdp;
-    pc()->SetLocalDescription(observer, desc.release());
+    pc()->SetLocalDescription(observer.get(), desc.release());
     RemoveUnusedVideoRenderers();
     // As mentioned above, we need to send the message immediately after
     // SetLocalDescription.
@@ -942,7 +944,7 @@
   bool SetRemoteDescription(std::unique_ptr<SessionDescriptionInterface> desc) {
     auto observer = rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
     RTC_LOG(LS_INFO) << debug_name_ << ": SetRemoteDescription";
-    pc()->SetRemoteDescription(observer, desc.release());
+    pc()->SetRemoteDescription(observer.get(), desc.release());
     RemoveUnusedVideoRenderers();
     EXPECT_TRUE_WAIT(observer->called(), kDefaultTimeout);
     return observer->result();
@@ -1056,7 +1058,7 @@
       ASSERT_TRUE(fake_video_renderers_.find(video_track->id()) ==
                   fake_video_renderers_.end());
       fake_video_renderers_[video_track->id()] =
-          std::make_unique<FakeVideoTrackRenderer>(video_track);
+          std::make_unique<FakeVideoTrackRenderer>(video_track.get());
     }
   }
   void OnRemoveTrack(
@@ -1139,7 +1141,7 @@
     RTC_LOG(LS_INFO) << debug_name_ << ": OnDataChannel";
     data_channels_.push_back(data_channel);
     data_observers_.push_back(
-        std::make_unique<MockDataChannelObserver>(data_channel));
+        std::make_unique<MockDataChannelObserver>(data_channel.get()));
   }
 
   std::string debug_name_;
diff --git a/pc/test/mock_peer_connection_observers.h b/pc/test/mock_peer_connection_observers.h
index 51e24b7..e9d97a9 100644
--- a/pc/test/mock_peer_connection_observers.h
+++ b/pc/test/mock_peer_connection_observers.h
@@ -74,7 +74,9 @@
   MediaStreamInterface* RemoteStream(const std::string& label) {
     return remote_streams_->find(label);
   }
-  StreamCollectionInterface* remote_streams() const { return remote_streams_; }
+  StreamCollectionInterface* remote_streams() const {
+    return remote_streams_.get();
+  }
   void OnAddStream(rtc::scoped_refptr<MediaStreamInterface> stream) override {
     last_added_stream_ = stream;
     remote_streams_->AddStream(stream);
@@ -82,7 +84,7 @@
   void OnRemoveStream(
       rtc::scoped_refptr<MediaStreamInterface> stream) override {
     last_removed_stream_ = stream;
-    remote_streams_->RemoveStream(stream);
+    remote_streams_->RemoveStream(stream.get());
   }
   void OnRenegotiationNeeded() override { renegotiation_needed_ = true; }
   void OnNegotiationNeededEvent(uint32_t event_id) override {
diff --git a/pc/test/peer_connection_test_wrapper.cc b/pc/test/peer_connection_test_wrapper.cc
index fef2cfb..f6891cd 100644
--- a/pc/test/peer_connection_test_wrapper.cc
+++ b/pc/test/peer_connection_test_wrapper.cc
@@ -106,7 +106,7 @@
   RTC_DCHECK_RUN_ON(&pc_thread_checker_);
 
   fake_audio_capture_module_ = FakeAudioCaptureModule::Create();
-  if (fake_audio_capture_module_ == NULL) {
+  if (fake_audio_capture_module_ == nullptr) {
     return false;
   }
 
@@ -184,7 +184,7 @@
 
 void PeerConnectionTestWrapper::OnDataChannel(
     rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) {
-  SignalOnDataChannel(data_channel);
+  SignalOnDataChannel(data_channel.get());
 }
 
 void PeerConnectionTestWrapper::OnSuccess(SessionDescriptionInterface* desc) {
@@ -237,7 +237,7 @@
 
   auto observer = rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
   peer_connection_->SetLocalDescription(
-      observer, webrtc::CreateSessionDescription(type, sdp).release());
+      observer.get(), webrtc::CreateSessionDescription(type, sdp).release());
 }
 
 void PeerConnectionTestWrapper::SetRemoteDescription(SdpType type,
@@ -248,7 +248,7 @@
 
   auto observer = rtc::make_ref_counted<MockSetSessionDescriptionObserver>();
   peer_connection_->SetRemoteDescription(
-      observer, webrtc::CreateSessionDescription(type, sdp).release());
+      observer.get(), webrtc::CreateSessionDescription(type, sdp).release());
 }
 
 void PeerConnectionTestWrapper::AddIceCandidate(const std::string& sdp_mid,
@@ -333,8 +333,8 @@
         peer_connection_factory_->CreateAudioSource(options);
     rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
         peer_connection_factory_->CreateAudioTrack(kAudioTrackLabelBase,
-                                                   source));
-    stream->AddTrack(audio_track);
+                                                   source.get()));
+    stream->AddTrack(audio_track.get());
   }
 
   if (video) {
@@ -348,9 +348,10 @@
 
     std::string videotrack_label = stream_id + kVideoTrackLabelBase;
     rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
-        peer_connection_factory_->CreateVideoTrack(videotrack_label, source));
+        peer_connection_factory_->CreateVideoTrack(videotrack_label,
+                                                   source.get()));
 
-    stream->AddTrack(video_track);
+    stream->AddTrack(video_track.get());
   }
   return stream;
 }
diff --git a/pc/track_media_info_map.cc b/pc/track_media_info_map.cc
index 12670dd..bf3ec0f 100644
--- a/pc/track_media_info_map.cc
+++ b/pc/track_media_info_map.cc
@@ -52,7 +52,7 @@
   RTC_DCHECK(remote_video_track_by_ssrc->empty());
   for (const auto& rtp_sender : rtp_senders) {
     cricket::MediaType media_type = rtp_sender->media_type();
-    MediaStreamTrackInterface* track = rtp_sender->track();
+    MediaStreamTrackInterface* track = rtp_sender->track().get();
     if (!track) {
       continue;
     }
@@ -74,7 +74,7 @@
   }
   for (const auto& rtp_receiver : rtp_receivers) {
     cricket::MediaType media_type = rtp_receiver->media_type();
-    MediaStreamTrackInterface* track = rtp_receiver->track();
+    MediaStreamTrackInterface* track = rtp_receiver->track().get();
     RTC_DCHECK(track);
     RtpParameters params = rtp_receiver->GetParameters();
     for (const RtpEncodingParameters& encoding : params.encodings) {
@@ -126,10 +126,10 @@
       &unsignaled_video_track);
 
   for (const auto& sender : rtp_senders) {
-    attachment_id_by_track_[sender->track()] = sender->AttachmentId();
+    attachment_id_by_track_[sender->track().get()] = sender->AttachmentId();
   }
   for (const auto& receiver : rtp_receivers) {
-    attachment_id_by_track_[receiver->track()] = receiver->AttachmentId();
+    attachment_id_by_track_[receiver->track().get()] = receiver->AttachmentId();
   }
 
   if (voice_media_info_) {
diff --git a/pc/track_media_info_map_unittest.cc b/pc/track_media_info_map_unittest.cc
index 8cf3360..e9428ca 100644
--- a/pc/track_media_info_map_unittest.cc
+++ b/pc/track_media_info_map_unittest.cc
@@ -205,10 +205,10 @@
 }  // namespace
 
 TEST_F(TrackMediaInfoMapTest, SingleSenderReceiverPerTrackWithOneSsrc) {
-  AddRtpSenderWithSsrcs({1}, local_audio_track_);
-  AddRtpReceiverWithSsrcs({2}, remote_audio_track_);
-  AddRtpSenderWithSsrcs({3}, local_video_track_);
-  AddRtpReceiverWithSsrcs({4}, remote_video_track_);
+  AddRtpSenderWithSsrcs({1}, local_audio_track_.get());
+  AddRtpReceiverWithSsrcs({2}, remote_audio_track_.get());
+  AddRtpSenderWithSsrcs({3}, local_video_track_.get());
+  AddRtpReceiverWithSsrcs({4}, remote_video_track_.get());
   CreateMap();
 
   // Local audio track <-> RTP audio sender
@@ -241,10 +241,10 @@
 }
 
 TEST_F(TrackMediaInfoMapTest, SingleSenderReceiverPerTrackWithMissingSsrc) {
-  AddRtpSenderWithSsrcs({}, local_audio_track_);
-  AddRtpSenderWithSsrcs({}, local_video_track_);
-  AddRtpReceiverWithSsrcs({}, remote_audio_track_);
-  AddRtpReceiverWithSsrcs({}, remote_video_track_);
+  AddRtpSenderWithSsrcs({}, local_audio_track_.get());
+  AddRtpSenderWithSsrcs({}, local_video_track_.get());
+  AddRtpReceiverWithSsrcs({}, remote_audio_track_.get());
+  AddRtpReceiverWithSsrcs({}, remote_video_track_.get());
   CreateMap();
 
   EXPECT_FALSE(map_->GetVoiceSenderInfos(*local_audio_track_));
@@ -255,10 +255,10 @@
 
 TEST_F(TrackMediaInfoMapTest,
        SingleSenderReceiverPerTrackWithAudioAndVideoUseSameSsrc) {
-  AddRtpSenderWithSsrcs({1}, local_audio_track_);
-  AddRtpReceiverWithSsrcs({2}, remote_audio_track_);
-  AddRtpSenderWithSsrcs({1}, local_video_track_);
-  AddRtpReceiverWithSsrcs({2}, remote_video_track_);
+  AddRtpSenderWithSsrcs({1}, local_audio_track_.get());
+  AddRtpReceiverWithSsrcs({2}, remote_audio_track_.get());
+  AddRtpSenderWithSsrcs({1}, local_video_track_.get());
+  AddRtpReceiverWithSsrcs({2}, remote_video_track_.get());
   CreateMap();
 
   // Local audio track <-> RTP audio sender
@@ -291,8 +291,8 @@
 }
 
 TEST_F(TrackMediaInfoMapTest, SingleMultiSsrcSenderPerTrack) {
-  AddRtpSenderWithSsrcs({1, 2}, local_audio_track_);
-  AddRtpSenderWithSsrcs({3, 4}, local_video_track_);
+  AddRtpSenderWithSsrcs({1, 2}, local_audio_track_.get());
+  AddRtpSenderWithSsrcs({3, 4}, local_video_track_.get());
   CreateMap();
 
   // Local audio track <-> RTP audio senders
@@ -313,10 +313,10 @@
 }
 
 TEST_F(TrackMediaInfoMapTest, MultipleOneSsrcSendersPerTrack) {
-  AddRtpSenderWithSsrcs({1}, local_audio_track_);
-  AddRtpSenderWithSsrcs({2}, local_audio_track_);
-  AddRtpSenderWithSsrcs({3}, local_video_track_);
-  AddRtpSenderWithSsrcs({4}, local_video_track_);
+  AddRtpSenderWithSsrcs({1}, local_audio_track_.get());
+  AddRtpSenderWithSsrcs({2}, local_audio_track_.get());
+  AddRtpSenderWithSsrcs({3}, local_video_track_.get());
+  AddRtpSenderWithSsrcs({4}, local_video_track_.get());
   CreateMap();
 
   // Local audio track <-> RTP audio senders
@@ -343,10 +343,10 @@
 }
 
 TEST_F(TrackMediaInfoMapTest, MultipleMultiSsrcSendersPerTrack) {
-  AddRtpSenderWithSsrcs({1, 2}, local_audio_track_);
-  AddRtpSenderWithSsrcs({3, 4}, local_audio_track_);
-  AddRtpSenderWithSsrcs({5, 6}, local_video_track_);
-  AddRtpSenderWithSsrcs({7, 8}, local_video_track_);
+  AddRtpSenderWithSsrcs({1, 2}, local_audio_track_.get());
+  AddRtpSenderWithSsrcs({3, 4}, local_audio_track_.get());
+  AddRtpSenderWithSsrcs({5, 6}, local_video_track_.get());
+  AddRtpSenderWithSsrcs({7, 8}, local_video_track_.get());
   CreateMap();
 
   // Local audio track <-> RTP audio senders
@@ -374,10 +374,10 @@
 
 // SSRCs can be reused for send and receive in loopback.
 TEST_F(TrackMediaInfoMapTest, SingleSenderReceiverPerTrackWithSsrcNotUnique) {
-  AddRtpSenderWithSsrcs({1}, local_audio_track_);
-  AddRtpReceiverWithSsrcs({1}, remote_audio_track_);
-  AddRtpSenderWithSsrcs({2}, local_video_track_);
-  AddRtpReceiverWithSsrcs({2}, remote_video_track_);
+  AddRtpSenderWithSsrcs({1}, local_audio_track_.get());
+  AddRtpReceiverWithSsrcs({1}, remote_audio_track_.get());
+  AddRtpSenderWithSsrcs({2}, local_video_track_.get());
+  AddRtpReceiverWithSsrcs({2}, remote_video_track_.get());
   CreateMap();
 
   // Local audio track <-> RTP audio senders
@@ -410,10 +410,10 @@
 }
 
 TEST_F(TrackMediaInfoMapTest, SsrcLookupFunction) {
-  AddRtpSenderWithSsrcs({1}, local_audio_track_);
-  AddRtpReceiverWithSsrcs({2}, remote_audio_track_);
-  AddRtpSenderWithSsrcs({3}, local_video_track_);
-  AddRtpReceiverWithSsrcs({4}, remote_video_track_);
+  AddRtpSenderWithSsrcs({1}, local_audio_track_.get());
+  AddRtpReceiverWithSsrcs({2}, remote_audio_track_.get());
+  AddRtpSenderWithSsrcs({3}, local_video_track_.get());
+  AddRtpReceiverWithSsrcs({4}, remote_video_track_.get());
   CreateMap();
   EXPECT_TRUE(map_->GetVoiceSenderInfoBySsrc(1));
   EXPECT_TRUE(map_->GetVoiceReceiverInfoBySsrc(2));
@@ -424,11 +424,12 @@
 }
 
 TEST_F(TrackMediaInfoMapTest, GetAttachmentIdByTrack) {
-  AddRtpSenderWithSsrcs({1}, local_audio_track_);
+  AddRtpSenderWithSsrcs({1}, local_audio_track_.get());
   CreateMap();
   EXPECT_EQ(rtp_senders_[0]->AttachmentId(),
-            map_->GetAttachmentIdByTrack(local_audio_track_));
-  EXPECT_EQ(absl::nullopt, map_->GetAttachmentIdByTrack(local_video_track_));
+            map_->GetAttachmentIdByTrack(local_audio_track_.get()));
+  EXPECT_EQ(absl::nullopt,
+            map_->GetAttachmentIdByTrack(local_video_track_.get()));
 }
 
 // Death tests.
@@ -442,18 +443,18 @@
 };
 
 TEST_F(TrackMediaInfoMapDeathTest, MultipleOneSsrcReceiversPerTrack) {
-  AddRtpReceiverWithSsrcs({1}, remote_audio_track_);
-  AddRtpReceiverWithSsrcs({2}, remote_audio_track_);
-  AddRtpReceiverWithSsrcs({3}, remote_video_track_);
-  AddRtpReceiverWithSsrcs({4}, remote_video_track_);
+  AddRtpReceiverWithSsrcs({1}, remote_audio_track_.get());
+  AddRtpReceiverWithSsrcs({2}, remote_audio_track_.get());
+  AddRtpReceiverWithSsrcs({3}, remote_video_track_.get());
+  AddRtpReceiverWithSsrcs({4}, remote_video_track_.get());
   EXPECT_DEATH(CreateMap(), "");
 }
 
 TEST_F(TrackMediaInfoMapDeathTest, MultipleMultiSsrcReceiversPerTrack) {
-  AddRtpReceiverWithSsrcs({1, 2}, remote_audio_track_);
-  AddRtpReceiverWithSsrcs({3, 4}, remote_audio_track_);
-  AddRtpReceiverWithSsrcs({5, 6}, remote_video_track_);
-  AddRtpReceiverWithSsrcs({7, 8}, remote_video_track_);
+  AddRtpReceiverWithSsrcs({1, 2}, remote_audio_track_.get());
+  AddRtpReceiverWithSsrcs({3, 4}, remote_audio_track_.get());
+  AddRtpReceiverWithSsrcs({5, 6}, remote_video_track_.get());
+  AddRtpReceiverWithSsrcs({7, 8}, remote_video_track_.get());
   EXPECT_DEATH(CreateMap(), "");
 }
 
diff --git a/pc/video_rtp_receiver.cc b/pc/video_rtp_receiver.cc
index 7659d7c..a119415 100644
--- a/pc/video_rtp_receiver.cc
+++ b/pc/video_rtp_receiver.cc
@@ -221,7 +221,7 @@
       }
     }
     if (removed) {
-      existing_stream->RemoveTrack(track_);
+      existing_stream->RemoveTrack(track_.get());
     }
   }
   // Add remote track to any streams that are new.
@@ -235,7 +235,7 @@
       }
     }
     if (added) {
-      stream->AddTrack(track_);
+      stream->AddTrack(track_.get());
     }
   }
   streams_ = streams;
diff --git a/pc/webrtc_session_description_factory.cc b/pc/webrtc_session_description_factory.cc
index 7ccc3bf..21da3a7 100644
--- a/pc/webrtc_session_description_factory.cc
+++ b/pc/webrtc_session_description_factory.cc
@@ -171,7 +171,7 @@
     // `SignalCertificateReady`.
     signaling_thread_->Post(
         RTC_FROM_HERE, this, MSG_USE_CONSTRUCTOR_CERTIFICATE,
-        new rtc::ScopedRefMessageData<rtc::RTCCertificate>(certificate));
+        new rtc::ScopedRefMessageData<rtc::RTCCertificate>(certificate.get()));
   } else {
     // Generate certificate.
     certificate_request_state_ = CERTIFICATE_WAITING;
@@ -350,7 +350,7 @@
                                ? sdp_info_->local_description()->description()
                                : nullptr);
   if (!desc) {
-    PostCreateSessionDescriptionFailed(request.observer,
+    PostCreateSessionDescriptionFailed(request.observer.get(),
                                        "Failed to initialize the offer.");
     return;
   }
@@ -377,7 +377,8 @@
       }
     }
   }
-  PostCreateSessionDescriptionSucceeded(request.observer, std::move(offer));
+  PostCreateSessionDescriptionSucceeded(request.observer.get(),
+                                        std::move(offer));
 }
 
 void WebRtcSessionDescriptionFactory::InternalCreateAnswer(
@@ -411,7 +412,7 @@
               ? sdp_info_->local_description()->description()
               : nullptr);
   if (!desc) {
-    PostCreateSessionDescriptionFailed(request.observer,
+    PostCreateSessionDescriptionFailed(request.observer.get(),
                                        "Failed to initialize the answer.");
     return;
   }
@@ -438,7 +439,8 @@
       }
     }
   }
-  PostCreateSessionDescriptionSucceeded(request.observer, std::move(answer));
+  PostCreateSessionDescriptionSucceeded(request.observer.get(),
+                                        std::move(answer));
 }
 
 void WebRtcSessionDescriptionFactory::FailPendingRequests(
@@ -448,7 +450,7 @@
     const CreateSessionDescriptionRequest& request =
         create_session_description_requests_.front();
     PostCreateSessionDescriptionFailed(
-        request.observer,
+        request.observer.get(),
         ((request.type == CreateSessionDescriptionRequest::kOffer)
              ? "CreateOffer"
              : "CreateAnswer") +