New PeerConnectionFactory::CreateVideoTrack with refcounted source

Bug: webrtc:15017
Change-Id: I04c794d8959583bb4cc5c3898f4175783ec49f16
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/249363
Reviewed-by: Henrik Boström <hbos@webrtc.org>
Reviewed-by: Tomas Gunnarsson <tommi@webrtc.org>
Commit-Queue: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#39635}
diff --git a/api/peer_connection_interface.h b/api/peer_connection_interface.h
index 1097a16..58e2d33 100644
--- a/api/peer_connection_interface.h
+++ b/api/peer_connection_interface.h
@@ -1553,8 +1553,17 @@
   // Creates a new local VideoTrack. The same `source` can be used in several
   // tracks.
   virtual rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
+      rtc::scoped_refptr<VideoTrackSourceInterface> source,
+      absl::string_view label) = 0;
+  // TODO(bugs.webrtc.org/15017): Deprecate this function once Chrome
+  // has been updated - it can't land as deprecated.
+  // ABSL_DEPRECATED("Use version with scoped_refptr")
+  virtual rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
       const std::string& label,
-      VideoTrackSourceInterface* source) = 0;
+      VideoTrackSourceInterface* source) {
+    return CreateVideoTrack(
+        rtc::scoped_refptr<VideoTrackSourceInterface>(source), label);
+  }
 
   // Creates an new AudioTrack. At the moment `source` can be null.
   virtual rtc::scoped_refptr<AudioTrackInterface> CreateAudioTrack(
diff --git a/api/test/mock_peer_connection_factory_interface.h b/api/test/mock_peer_connection_factory_interface.h
index ae1fbfb..67a67b8 100644
--- a/api/test/mock_peer_connection_factory_interface.h
+++ b/api/test/mock_peer_connection_factory_interface.h
@@ -65,6 +65,11 @@
               CreateVideoTrack,
               (const std::string&, VideoTrackSourceInterface*),
               (override));
+  MOCK_METHOD(rtc::scoped_refptr<VideoTrackInterface>,
+              CreateVideoTrack,
+              (rtc::scoped_refptr<VideoTrackSourceInterface>,
+               absl::string_view),
+              (override));
   MOCK_METHOD(rtc::scoped_refptr<AudioTrackInterface>,
               CreateAudioTrack,
               (const std::string&, AudioSourceInterface*),
diff --git a/examples/androidnativeapi/jni/android_call_client.cc b/examples/androidnativeapi/jni/android_call_client.cc
index 7da56e6..ae0a40b 100644
--- a/examples/androidnativeapi/jni/android_call_client.cc
+++ b/examples/androidnativeapi/jni/android_call_client.cc
@@ -186,8 +186,8 @@
 
   RTC_LOG(LS_INFO) << "PeerConnection created: " << pc_.get();
 
-  rtc::scoped_refptr<webrtc::VideoTrackInterface> local_video_track(
-      pcf_->CreateVideoTrack("video", video_source_.get()));
+  rtc::scoped_refptr<webrtc::VideoTrackInterface> local_video_track =
+      pcf_->CreateVideoTrack(video_source_, "video");
   local_video_track->AddOrUpdateSink(local_sink_.get(), rtc::VideoSinkWants());
   pc_->AddTransceiver(local_video_track);
   RTC_LOG(LS_INFO) << "Local video sink set up: " << local_video_track.get();
diff --git a/examples/objcnativeapi/objc/objc_call_client.mm b/examples/objcnativeapi/objc/objc_call_client.mm
index 7dd57b4..90bcfcc 100644
--- a/examples/objcnativeapi/objc/objc_call_client.mm
+++ b/examples/objcnativeapi/objc/objc_call_client.mm
@@ -149,7 +149,7 @@
   RTC_LOG(LS_INFO) << "PeerConnection created: " << pc_.get();
 
   rtc::scoped_refptr<webrtc::VideoTrackInterface> local_video_track =
-      pcf_->CreateVideoTrack("video", video_source_.get());
+      pcf_->CreateVideoTrack(video_source_, "video");
   pc_->AddTransceiver(local_video_track);
   RTC_LOG(LS_INFO) << "Local video sink set up: " << local_video_track.get();
 
diff --git a/examples/peerconnection/client/conductor.cc b/examples/peerconnection/client/conductor.cc
index 965525a..052d417 100644
--- a/examples/peerconnection/client/conductor.cc
+++ b/examples/peerconnection/client/conductor.cc
@@ -468,8 +468,7 @@
       CapturerTrackSource::Create();
   if (video_device) {
     rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track_(
-        peer_connection_factory_->CreateVideoTrack(kVideoLabel,
-                                                   video_device.get()));
+        peer_connection_factory_->CreateVideoTrack(video_device, kVideoLabel));
     main_wnd_->StartLocalRenderer(video_track_.get());
 
     result_or_error = peer_connection_->AddTrack(video_track_, {kStreamId});
diff --git a/examples/unityplugin/simple_peer_connection.cc b/examples/unityplugin/simple_peer_connection.cc
index 861b22f..de49d5c 100644
--- a/examples/unityplugin/simple_peer_connection.cc
+++ b/examples/unityplugin/simple_peer_connection.cc
@@ -460,16 +460,15 @@
     g_camera = (jobject)env->NewGlobalRef(camera_tmp);
 
     rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
-        g_peer_connection_factory->CreateVideoTrack(kVideoLabel,
-                                                    source.release()));
+        g_peer_connection_factory->CreateVideoTrack(source, kVideoLabel));
     stream->AddTrack(video_track);
 #else
     rtc::scoped_refptr<CapturerTrackSource> video_device =
         CapturerTrackSource::Create();
     if (video_device) {
       rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
-          g_peer_connection_factory->CreateVideoTrack(kVideoLabel,
-                                                      video_device.get()));
+          g_peer_connection_factory->CreateVideoTrack(video_device,
+                                                      kVideoLabel));
 
       stream->AddTrack(video_track);
     }
diff --git a/pc/peer_connection_adaptation_integrationtest.cc b/pc/peer_connection_adaptation_integrationtest.cc
index 5922e15..882fa36 100644
--- a/pc/peer_connection_adaptation_integrationtest.cc
+++ b/pc/peer_connection_adaptation_integrationtest.cc
@@ -64,7 +64,7 @@
           periodic_track_source_config, /* remote */ false);
   TrackWithPeriodicSource track_with_source;
   track_with_source.track =
-      factory->CreateVideoTrack("PeriodicTrack", periodic_track_source.get());
+      factory->CreateVideoTrack(periodic_track_source, "PeriodicTrack");
   track_with_source.periodic_track_source = periodic_track_source;
   return track_with_source;
 }
diff --git a/pc/peer_connection_factory.cc b/pc/peer_connection_factory.cc
index afebdd7..eaa69d3 100644
--- a/pc/peer_connection_factory.cc
+++ b/pc/peer_connection_factory.cc
@@ -273,12 +273,11 @@
 }
 
 rtc::scoped_refptr<VideoTrackInterface> PeerConnectionFactory::CreateVideoTrack(
-    const std::string& id,
-    VideoTrackSourceInterface* source) {
+    rtc::scoped_refptr<VideoTrackSourceInterface> source,
+    absl::string_view id) {
   RTC_DCHECK(signaling_thread()->IsCurrent());
-  rtc::scoped_refptr<VideoTrackInterface> track = VideoTrack::Create(
-      id, rtc::scoped_refptr<VideoTrackSourceInterface>(source),
-      worker_thread());
+  rtc::scoped_refptr<VideoTrackInterface> track =
+      VideoTrack::Create(id, source, worker_thread());
   return VideoTrackProxy::Create(signaling_thread(), worker_thread(), track);
 }
 
diff --git a/pc/peer_connection_factory.h b/pc/peer_connection_factory.h
index dac3702..f55d09f 100644
--- a/pc/peer_connection_factory.h
+++ b/pc/peer_connection_factory.h
@@ -85,8 +85,8 @@
       const cricket::AudioOptions& options) override;
 
   rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
-      const std::string& id,
-      VideoTrackSourceInterface* video_source) override;
+      rtc::scoped_refptr<VideoTrackSourceInterface> video_source,
+      absl::string_view id) override;
 
   rtc::scoped_refptr<AudioTrackInterface> CreateAudioTrack(
       const std::string& id,
diff --git a/pc/peer_connection_factory_proxy.h b/pc/peer_connection_factory_proxy.h
index 59e373d..4781497 100644
--- a/pc/peer_connection_factory_proxy.h
+++ b/pc/peer_connection_factory_proxy.h
@@ -43,8 +43,8 @@
               const cricket::AudioOptions&)
 PROXY_METHOD2(rtc::scoped_refptr<VideoTrackInterface>,
               CreateVideoTrack,
-              const std::string&,
-              VideoTrackSourceInterface*)
+              rtc::scoped_refptr<VideoTrackSourceInterface>,
+              absl::string_view)
 PROXY_METHOD2(rtc::scoped_refptr<AudioTrackInterface>,
               CreateAudioTrack,
               const std::string&,
diff --git a/pc/peer_connection_factory_unittest.cc b/pc/peer_connection_factory_unittest.cc
index 6aa7f49..b7fb726 100644
--- a/pc/peer_connection_factory_unittest.cc
+++ b/pc/peer_connection_factory_unittest.cc
@@ -567,7 +567,7 @@
 
   ASSERT_TRUE(source.get() != NULL);
   rtc::scoped_refptr<VideoTrackInterface> track(
-      factory_->CreateVideoTrack("testlabel", source.get()));
+      factory_->CreateVideoTrack(source, "testlabel"));
   ASSERT_TRUE(track.get() != NULL);
   FakeVideoTrackRenderer local_renderer(track.get());
 
diff --git a/pc/peer_connection_field_trial_tests.cc b/pc/peer_connection_field_trial_tests.cc
index 9220623..c3b3a2d 100644
--- a/pc/peer_connection_field_trial_tests.cc
+++ b/pc/peer_connection_field_trial_tests.cc
@@ -237,8 +237,7 @@
   auto video_track_source =
       rtc::make_ref_counted<FrameGeneratorCapturerVideoTrackSource>(
           config, clock_, /*is_screencast=*/false);
-  caller->AddTrack(
-      pc_factory_->CreateVideoTrack("v", video_track_source.get()));
+  caller->AddTrack(pc_factory_->CreateVideoTrack(video_track_source, "v"));
   WrapperPtr callee = CreatePeerConnection();
 
   ASSERT_TRUE(callee->SetRemoteDescription(caller->CreateOfferAndSetAsLocal()));
diff --git a/pc/peer_connection_interface_unittest.cc b/pc/peer_connection_interface_unittest.cc
index f7f408b..afbfcea 100644
--- a/pc/peer_connection_interface_unittest.cc
+++ b/pc/peer_connection_interface_unittest.cc
@@ -814,8 +814,7 @@
 
   rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack(
       const std::string& label) {
-    return pc_factory_->CreateVideoTrack(label,
-                                         FakeVideoTrackSource::Create().get());
+    return pc_factory_->CreateVideoTrack(FakeVideoTrackSource::Create(), label);
   }
 
   void AddVideoTrack(const std::string& track_label,
diff --git a/pc/peer_connection_rampup_tests.cc b/pc/peer_connection_rampup_tests.cc
index 47f463e..0fbbe75 100644
--- a/pc/peer_connection_rampup_tests.cc
+++ b/pc/peer_connection_rampup_tests.cc
@@ -127,8 +127,8 @@
             config, clock, /*is_screencast=*/false));
     video_track_sources_.back()->Start();
     return rtc::scoped_refptr<VideoTrackInterface>(
-        pc_factory()->CreateVideoTrack(rtc::CreateRandomUuid(),
-                                       video_track_sources_.back().get()));
+        pc_factory()->CreateVideoTrack(video_track_sources_.back(),
+                                       rtc::CreateRandomUuid()));
   }
 
   rtc::scoped_refptr<AudioTrackInterface> CreateLocalAudioTrack(
diff --git a/pc/peer_connection_wrapper.cc b/pc/peer_connection_wrapper.cc
index 2fbca1d..44f4256 100644
--- a/pc/peer_connection_wrapper.cc
+++ b/pc/peer_connection_wrapper.cc
@@ -277,8 +277,7 @@
 
 rtc::scoped_refptr<VideoTrackInterface> PeerConnectionWrapper::CreateVideoTrack(
     const std::string& label) {
-  return pc_factory()->CreateVideoTrack(label,
-                                        FakeVideoTrackSource::Create().get());
+  return pc_factory()->CreateVideoTrack(FakeVideoTrackSource::Create(), label);
 }
 
 rtc::scoped_refptr<RtpSenderInterface> PeerConnectionWrapper::AddTrack(
diff --git a/pc/test/integration_test_helpers.h b/pc/test/integration_test_helpers.h
index c2fc799..200a025 100644
--- a/pc/test/integration_test_helpers.h
+++ b/pc/test/integration_test_helpers.h
@@ -868,9 +868,9 @@
     video_track_sources_.emplace_back(
         rtc::make_ref_counted<webrtc::FakePeriodicVideoTrackSource>(
             config, false /* remote */));
-    rtc::scoped_refptr<webrtc::VideoTrackInterface> track(
-        peer_connection_factory_->CreateVideoTrack(
-            rtc::CreateRandomUuid(), video_track_sources_.back().get()));
+    rtc::scoped_refptr<webrtc::VideoTrackInterface> track =
+        peer_connection_factory_->CreateVideoTrack(video_track_sources_.back(),
+                                                   rtc::CreateRandomUuid());
     if (!local_video_renderer_) {
       local_video_renderer_.reset(
           new webrtc::FakeVideoTrackRenderer(track.get()));
diff --git a/pc/test/peer_connection_test_wrapper.cc b/pc/test/peer_connection_test_wrapper.cc
index c35628e..84a01f4 100644
--- a/pc/test/peer_connection_test_wrapper.cc
+++ b/pc/test/peer_connection_test_wrapper.cc
@@ -350,8 +350,7 @@
 
     std::string videotrack_label = stream_id + kVideoTrackLabelBase;
     rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
-        peer_connection_factory_->CreateVideoTrack(videotrack_label,
-                                                   source.get()));
+        peer_connection_factory_->CreateVideoTrack(source, videotrack_label));
 
     stream->AddTrack(video_track);
   }
diff --git a/sdk/android/src/jni/pc/peer_connection_factory.cc b/sdk/android/src/jni/pc/peer_connection_factory.cc
index c2950b3..045a2b9 100644
--- a/sdk/android/src/jni/pc/peer_connection_factory.cc
+++ b/sdk/android/src/jni/pc/peer_connection_factory.cc
@@ -487,8 +487,9 @@
   rtc::scoped_refptr<VideoTrackInterface> track =
       PeerConnectionFactoryFromJava(native_factory)
           ->CreateVideoTrack(
-              JavaToStdString(jni, id),
-              reinterpret_cast<VideoTrackSourceInterface*>(native_source));
+              rtc::scoped_refptr<VideoTrackSourceInterface>(
+                  reinterpret_cast<VideoTrackSourceInterface*>(native_source)),
+              JavaToStdString(jni, id));
   return jlongFromPointer(track.release());
 }
 
diff --git a/sdk/objc/api/peerconnection/RTCVideoTrack.mm b/sdk/objc/api/peerconnection/RTCVideoTrack.mm
index d4862e3..d3296f6 100644
--- a/sdk/objc/api/peerconnection/RTCVideoTrack.mm
+++ b/sdk/objc/api/peerconnection/RTCVideoTrack.mm
@@ -31,7 +31,7 @@
   NSParameterAssert(trackId.length);
   std::string nativeId = [NSString stdStringForString:trackId];
   rtc::scoped_refptr<webrtc::VideoTrackInterface> track =
-      factory.nativeFactory->CreateVideoTrack(nativeId, source.nativeVideoSource.get());
+      factory.nativeFactory->CreateVideoTrack(source.nativeVideoSource, nativeId);
   if (self = [self initWithFactory:factory nativeTrack:track type:RTCMediaStreamTrackTypeVideo]) {
     _source = source;
   }
diff --git a/test/pc/e2e/media/media_helper.cc b/test/pc/e2e/media/media_helper.cc
index e945bd4..d14dcdd 100644
--- a/test/pc/e2e/media/media_helper.cc
+++ b/test/pc/e2e/media/media_helper.cc
@@ -64,8 +64,8 @@
     RTC_LOG(LS_INFO) << "Adding video with video_config.stream_label="
                      << video_config.stream_label.value();
     rtc::scoped_refptr<VideoTrackInterface> track =
-        peer->pc_factory()->CreateVideoTrack(video_config.stream_label.value(),
-                                             source.get());
+        peer->pc_factory()->CreateVideoTrack(source,
+                                             video_config.stream_label.value());
     if (video_config.content_hint.has_value()) {
       track->set_content_hint(video_config.content_hint.value());
     }
diff --git a/test/peer_scenario/peer_scenario_client.cc b/test/peer_scenario/peer_scenario_client.cc
index 5d77f17..907bac3 100644
--- a/test/peer_scenario/peer_scenario_client.cc
+++ b/test/peer_scenario/peer_scenario_client.cc
@@ -323,7 +323,7 @@
   capturer->Init();
   res.source = rtc::make_ref_counted<FrameGeneratorCapturerVideoTrackSource>(
       std::move(capturer), config.screencast);
-  auto track = pc_factory_->CreateVideoTrack(track_id, res.source.get());
+  auto track = pc_factory_->CreateVideoTrack(res.source, track_id);
   res.track = track.get();
   res.sender =
       peer_connection_->AddTrack(track, {kCommonStreamId}).MoveValue().get();