Reland: FrameGeneratorCapturer: don't generate video before Start is called

It is partial reland, which adds call to Start() to all relevant places,
but doesn't actually switches frame generator to not produce frames from
the moment it was created.

Bug: b/272350185
Change-Id: I6e3bd7af6f5cd8d9baff79c2aada7b2ddfae1c8d
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/310782
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Reviewed-by: Ilya Nikolaevskiy <ilnik@webrtc.org>
Reviewed-by: Florent Castelli <orphis@webrtc.org>
Commit-Queue: Artem Titov <titovartem@webrtc.org>
Reviewed-by: Erik Språng <sprang@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#40379}
diff --git a/call/bitrate_estimator_tests.cc b/call/bitrate_estimator_tests.cc
index f44cdfd..f17a037 100644
--- a/call/bitrate_estimator_tests.cc
+++ b/call/bitrate_estimator_tests.cc
@@ -186,6 +186,7 @@
               test::VideoTestConstants::kDefaultFramerate,
               *test->task_queue_factory_);
       frame_generator_capturer_->Init();
+      frame_generator_capturer_->Start();
       send_stream_->SetSource(frame_generator_capturer_.get(),
                               DegradationPreference::MAINTAIN_FRAMERATE);
       send_stream_->Start();
diff --git a/pc/peer_connection_field_trial_tests.cc b/pc/peer_connection_field_trial_tests.cc
index c3b3a2d..7799c9d 100644
--- a/pc/peer_connection_field_trial_tests.cc
+++ b/pc/peer_connection_field_trial_tests.cc
@@ -237,6 +237,7 @@
   auto video_track_source =
       rtc::make_ref_counted<FrameGeneratorCapturerVideoTrackSource>(
           config, clock_, /*is_screencast=*/false);
+  video_track_source->Start();
   caller->AddTrack(pc_factory_->CreateVideoTrack(video_track_source, "v"));
   WrapperPtr callee = CreatePeerConnection();
 
diff --git a/pc/test/frame_generator_capturer_video_track_source.h b/pc/test/frame_generator_capturer_video_track_source.h
index 50a3d26..79a5b34 100644
--- a/pc/test/frame_generator_capturer_video_track_source.h
+++ b/pc/test/frame_generator_capturer_video_track_source.h
@@ -64,9 +64,15 @@
 
   ~FrameGeneratorCapturerVideoTrackSource() = default;
 
-  void Start() { SetState(kLive); }
+  void Start() {
+    SetState(kLive);
+    video_capturer_->Start();
+  }
 
-  void Stop() { SetState(kMuted); }
+  void Stop() {
+    SetState(kMuted);
+    video_capturer_->Stop();
+  }
 
   bool is_screencast() const override { return is_screencast_; }
 
diff --git a/test/BUILD.gn b/test/BUILD.gn
index 3a0a359..5187205 100644
--- a/test/BUILD.gn
+++ b/test/BUILD.gn
@@ -1260,6 +1260,7 @@
       ":run_loop",
       ":scoped_key_value_config",
       ":test_support",
+      ":test_video_capturer",
       ":video_test_common",
       ":video_test_constants",
       "../api:array_view",
diff --git a/test/call_test.cc b/test/call_test.cc
index 7a1bbd2..b8a1cd7 100644
--- a/test/call_test.cc
+++ b/test/call_test.cc
@@ -634,7 +634,14 @@
     audio_recv_stream->Start();
 }
 
+void CallTest::StartVideoSources() {
+  for (size_t i = 0; i < video_sources_.size(); ++i) {
+    video_sources_[i]->Start();
+  }
+}
+
 void CallTest::StartVideoStreams() {
+  StartVideoSources();
   for (size_t i = 0; i < video_send_streams_.size(); ++i) {
     std::vector<bool> active_rtp_streams(
         video_send_configs_[i].rtp.ssrcs.size(), true);
diff --git a/test/call_test.h b/test/call_test.h
index 41db9ce..08d0e49 100644
--- a/test/call_test.h
+++ b/test/call_test.h
@@ -36,6 +36,7 @@
 #include "test/rtp_rtcp_observer.h"
 #include "test/run_loop.h"
 #include "test/scoped_key_value_config.h"
+#include "test/test_video_capturer.h"
 #include "test/video_test_constants.h"
 
 namespace webrtc {
@@ -162,6 +163,7 @@
   void ConnectVideoSourcesToStreams();
 
   void Start();
+  void StartVideoSources();
   void StartVideoStreams();
   void Stop();
   void StopVideoStreams();
@@ -210,8 +212,7 @@
   std::vector<FlexfecReceiveStream*> flexfec_receive_streams_;
 
   test::FrameGeneratorCapturer* frame_generator_capturer_;
-  std::vector<std::unique_ptr<rtc::VideoSourceInterface<VideoFrame>>>
-      video_sources_;
+  std::vector<std::unique_ptr<TestVideoCapturer>> video_sources_;
   DegradationPreference degradation_preference_ =
       DegradationPreference::MAINTAIN_FRAMERATE;
 
diff --git a/test/peer_scenario/peer_scenario_client.cc b/test/peer_scenario/peer_scenario_client.cc
index 3419b9e..697bf05 100644
--- a/test/peer_scenario/peer_scenario_client.cc
+++ b/test/peer_scenario/peer_scenario_client.cc
@@ -338,6 +338,7 @@
   capturer->Init();
   res.source = rtc::make_ref_counted<FrameGeneratorCapturerVideoTrackSource>(
       std::move(capturer), config.screencast);
+  res.source->Start();
   auto track = pc_factory_->CreateVideoTrack(res.source, track_id);
   res.track = track.get();
   res.sender =
diff --git a/test/scenario/video_stream.cc b/test/scenario/video_stream.cc
index ec13976..e082aa3 100644
--- a/test/scenario/video_stream.cc
+++ b/test/scenario/video_stream.cc
@@ -419,6 +419,7 @@
   send_config.suspend_below_min_bitrate =
       config.encoder.suspend_below_min_bitrate;
 
+  video_capturer_->Start();
   sender_->SendTask([&] {
     if (config.stream.fec_controller_factory) {
       send_stream_ = sender_->call_->CreateVideoSendStream(
diff --git a/video/end_to_end_tests/multi_codec_receive_tests.cc b/video/end_to_end_tests/multi_codec_receive_tests.cc
index 377209b..2d8bf05 100644
--- a/video/end_to_end_tests/multi_codec_receive_tests.cc
+++ b/video/end_to_end_tests/multi_codec_receive_tests.cc
@@ -254,6 +254,7 @@
       GetVideoSendStream()->Start();
       CreateFrameGeneratorCapturer(kFps, kWidth / 2, kHeight / 2);
       ConnectVideoSourcesToStreams();
+      StartVideoSources();
     });
     EXPECT_TRUE(observer_.Wait()) << "Timed out waiting for frames.";
   }
diff --git a/video/end_to_end_tests/rtp_rtcp_tests.cc b/video/end_to_end_tests/rtp_rtcp_tests.cc
index 009a387..fbd8585 100644
--- a/video/end_to_end_tests/rtp_rtcp_tests.cc
+++ b/video/end_to_end_tests/rtp_rtcp_tests.cc
@@ -339,6 +339,7 @@
             ->SendRtcp(packet.data(), packet.size());
       }
       CreateFrameGeneratorCapturer(30, 1280, 720);
+      StartVideoSources();
     });
 
     observer.ResetExpectedSsrcs(1);