Extend TestVideoTrackSource API

Bug: b/272350185
Change-Id: Ibc53e7a9ee8f572475d86fc78de1c1ed71078910
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/299140
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Commit-Queue: Artem Titov <titovartem@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#39678}
diff --git a/api/test/video/test_video_track_source.h b/api/test/video/test_video_track_source.h
index 4e2d138..449228f 100644
--- a/api/test/video/test_video_track_source.h
+++ b/api/test/video/test_video_track_source.h
@@ -62,6 +62,16 @@
 
   virtual void SetScreencast(bool is_screencast) = 0;
 
+  // TODO(titovartem): make next 4 methods pure virtual.
+  virtual void SetEnableAdaptation(bool enable_adaptation) {}
+
+  virtual int GetFrameWidth() const { return 0; }
+  virtual int GetFrameHeight() const { return 0; }
+
+  virtual void OnOutputFormatRequest(int width,
+                                     int height,
+                                     const absl::optional<int>& max_fps) {}
+
  protected:
   virtual rtc::VideoSourceInterface<VideoFrame>* source() = 0;
 
diff --git a/test/frame_generator_capturer.cc b/test/frame_generator_capturer.cc
index c69fca0..5d0e2e8 100644
--- a/test/frame_generator_capturer.cc
+++ b/test/frame_generator_capturer.cc
@@ -29,6 +29,7 @@
 namespace webrtc {
 namespace test {
 namespace {
+
 std::string TransformFilePath(std::string path) {
   static const std::string resource_prefix = "res://";
   int ext_pos = path.rfind('.');
@@ -41,6 +42,7 @@
   }
   return path;
 }
+
 }  // namespace
 
 FrameGeneratorCapturer::FrameGeneratorCapturer(
@@ -177,49 +179,39 @@
 }
 
 void FrameGeneratorCapturer::InsertFrame() {
-  absl::optional<Resolution> resolution;
+  MutexLock lock(&lock_);
+  if (sending_) {
+    FrameGeneratorInterface::VideoFrameData frame_data =
+        frame_generator_->NextFrame();
+    // TODO(srte): Use more advanced frame rate control to allow arbritrary
+    // fractions.
+    int decimation =
+        std::round(static_cast<double>(source_fps_) / target_capture_fps_);
+    for (int i = 1; i < decimation; ++i)
+      frame_data = frame_generator_->NextFrame();
 
-  {
-    MutexLock lock(&lock_);
-    if (sending_) {
-      FrameGeneratorInterface::VideoFrameData frame_data =
-          frame_generator_->NextFrame();
-      // TODO(srte): Use more advanced frame rate control to allow arbritrary
-      // fractions.
-      int decimation =
-          std::round(static_cast<double>(source_fps_) / target_capture_fps_);
-      for (int i = 1; i < decimation; ++i)
-        frame_data = frame_generator_->NextFrame();
-
-      VideoFrame frame =
-          VideoFrame::Builder()
-              .set_video_frame_buffer(frame_data.buffer)
-              .set_rotation(fake_rotation_)
-              .set_timestamp_us(clock_->TimeInMicroseconds())
-              .set_ntp_time_ms(clock_->CurrentNtpInMilliseconds())
-              .set_update_rect(frame_data.update_rect)
-              .set_color_space(fake_color_space_)
-              .build();
-      if (first_frame_capture_time_ == -1) {
-        first_frame_capture_time_ = frame.ntp_time_ms();
-      }
-
-      resolution = Resolution{frame.width(), frame.height()};
-
-      TestVideoCapturer::OnFrame(frame);
+    VideoFrame frame = VideoFrame::Builder()
+                           .set_video_frame_buffer(frame_data.buffer)
+                           .set_rotation(fake_rotation_)
+                           .set_timestamp_us(clock_->TimeInMicroseconds())
+                           .set_ntp_time_ms(clock_->CurrentNtpInMilliseconds())
+                           .set_update_rect(frame_data.update_rect)
+                           .set_color_space(fake_color_space_)
+                           .build();
+    if (first_frame_capture_time_ == -1) {
+      first_frame_capture_time_ = frame.ntp_time_ms();
     }
-  }
 
-  if (resolution) {
-    MutexLock lock(&stats_lock_);
-    source_resolution_ = resolution;
+    TestVideoCapturer::OnFrame(frame);
   }
 }
 
 absl::optional<FrameGeneratorCapturer::Resolution>
-FrameGeneratorCapturer::GetResolution() {
-  MutexLock lock(&stats_lock_);
-  return source_resolution_;
+FrameGeneratorCapturer::GetResolution() const {
+  FrameGeneratorInterface::Resolution resolution =
+      frame_generator_->GetResolution();
+  return Resolution{.width = static_cast<int>(resolution.width),
+                    .height = static_cast<int>(resolution.height)};
 }
 
 void FrameGeneratorCapturer::Start() {
@@ -266,6 +258,14 @@
   target_capture_fps_ = std::min(source_fps_, target_framerate);
 }
 
+int FrameGeneratorCapturer::GetFrameWidth() const {
+  return static_cast<int>(frame_generator_->GetResolution().width);
+}
+
+int FrameGeneratorCapturer::GetFrameHeight() const {
+  return static_cast<int>(frame_generator_->GetResolution().height);
+}
+
 void FrameGeneratorCapturer::OnOutputFormatRequest(
     int width,
     int height,
diff --git a/test/frame_generator_capturer.h b/test/frame_generator_capturer.h
index e310e40..20b3556 100644
--- a/test/frame_generator_capturer.h
+++ b/test/frame_generator_capturer.h
@@ -132,11 +132,14 @@
   void ChangeResolution(size_t width, size_t height);
   void ChangeFramerate(int target_framerate);
 
+  int GetFrameWidth() const override;
+  int GetFrameHeight() const override;
+
   struct Resolution {
     int width;
     int height;
   };
-  absl::optional<Resolution> GetResolution();
+  absl::optional<Resolution> GetResolution() const;
 
   void OnOutputFormatRequest(int width,
                              int height,
@@ -178,9 +181,6 @@
 
   int64_t first_frame_capture_time_;
 
-  Mutex stats_lock_;
-  absl::optional<Resolution> source_resolution_ RTC_GUARDED_BY(&stats_lock_);
-
   // Must be the last field, so it will be deconstructed first as tasks
   // in the TaskQueue access other fields of the instance of this class.
   rtc::TaskQueue task_queue_;
diff --git a/test/frame_generator_capturer_unittest.cc b/test/frame_generator_capturer_unittest.cc
index d8371f4..0098b0e 100644
--- a/test/frame_generator_capturer_unittest.cc
+++ b/test/frame_generator_capturer_unittest.cc
@@ -71,7 +71,9 @@
   config.squares_video->framerate = 20;
   auto capturer = FrameGeneratorCapturer::Create(
       time.GetClock(), *time.GetTaskQueueFactory(), config);
-  EXPECT_FALSE(capturer->GetResolution());
+  EXPECT_TRUE(capturer->GetResolution());
+  EXPECT_EQ(kWidth, capturer->GetResolution()->width);
+  EXPECT_EQ(kHeight, capturer->GetResolution()->height);
   capturer->Start();
   time.AdvanceTime(TimeDelta::Seconds(1));
   ASSERT_TRUE(capturer->GetResolution());
diff --git a/test/mac_capturer.h b/test/mac_capturer.h
index 3d7ee77..35cd1cc 100644
--- a/test/mac_capturer.h
+++ b/test/mac_capturer.h
@@ -10,6 +10,7 @@
 #ifndef TEST_MAC_CAPTURER_H_
 #define TEST_MAC_CAPTURER_H_
 
+#include <cstddef>
 #include <memory>
 #include <vector>
 
@@ -33,6 +34,9 @@
 
   void OnFrame(const VideoFrame& frame) override;
 
+  int GetFrameWidth() const override { return static_cast<int>(width_); }
+  int GetFrameHeight() const override { return static_cast<int>(height_); }
+
  private:
   MacCapturer(size_t width,
               size_t height,
@@ -40,6 +44,8 @@
               size_t capture_device_index);
   void Destroy();
 
+  size_t width_;
+  size_t height_;
   void* capturer_;
   void* adapter_;
 };
diff --git a/test/mac_capturer.mm b/test/mac_capturer.mm
index da8e9b7..9b14f28 100644
--- a/test/mac_capturer.mm
+++ b/test/mac_capturer.mm
@@ -64,6 +64,8 @@
                          size_t height,
                          size_t target_fps,
                          size_t capture_device_index) {
+  width_ = width;
+  height_ = height;
   RTCTestVideoSourceAdapter *adapter = [[RTCTestVideoSourceAdapter alloc] init];
   adapter_ = (__bridge_retained void *)adapter;
   adapter.capturer = this;
diff --git a/test/pc/e2e/media/test_video_capturer_video_track_source.h b/test/pc/e2e/media/test_video_capturer_video_track_source.h
index 350766b..70db07b 100644
--- a/test/pc/e2e/media/test_video_capturer_video_track_source.h
+++ b/test/pc/e2e/media/test_video_capturer_video_track_source.h
@@ -49,13 +49,27 @@
 
   void Stop() override { SetState(kMuted); }
 
+  int GetFrameWidth() const override {
+    return video_capturer_->GetFrameWidth();
+  }
+
+  int GetFrameHeight() const override {
+    return video_capturer_->GetFrameHeight();
+  }
+
   bool is_screencast() const override {
     RTC_DCHECK_RUN_ON(&sequence_checker_);
     return is_screencast_;
   }
 
-  void SetDisableAdaptation(bool disable_adaptation) {
-    video_capturer_->SetDisableAdaptation(disable_adaptation);
+  void SetEnableAdaptation(bool enable_adaptation) {
+    video_capturer_->SetEnableAdaptation(enable_adaptation);
+  }
+
+  void OnOutputFormatRequest(int width,
+                             int height,
+                             const absl::optional<int>& max_fps) override {
+    video_capturer_->OnOutputFormatRequest(width, height, max_fps);
   }
 
   void SetScreencast(bool is_screencast) override {
diff --git a/test/test_video_capturer.cc b/test/test_video_capturer.cc
index b55eefc..3098731 100644
--- a/test/test_video_capturer.cc
+++ b/test/test_video_capturer.cc
@@ -19,6 +19,7 @@
 
 namespace webrtc {
 namespace test {
+
 TestVideoCapturer::~TestVideoCapturer() = default;
 
 void TestVideoCapturer::OnOutputFormatRequest(
@@ -40,12 +41,12 @@
 
   VideoFrame frame = MaybePreprocess(original_frame);
 
-  bool disable_adaptation;
+  bool enable_adaptation;
   {
     MutexLock lock(&lock_);
-    disable_adaptation = disable_adaptation_;
+    enable_adaptation = enable_adaptation_;
   }
-  if (disable_adaptation) {
+  if (enable_adaptation) {
     broadcaster_.OnFrame(frame);
   }
 
diff --git a/test/test_video_capturer.h b/test/test_video_capturer.h
index 3fc03f0..48b7f7a 100644
--- a/test/test_video_capturer.h
+++ b/test/test_video_capturer.h
@@ -41,14 +41,17 @@
     MutexLock lock(&lock_);
     preprocessor_ = std::move(preprocessor);
   }
-  void SetDisableAdaptation(bool disable_adaptation) {
+  void SetEnableAdaptation(bool enable_adaptation) {
     MutexLock lock(&lock_);
-    disable_adaptation_ = disable_adaptation;
+    enable_adaptation_ = enable_adaptation;
   }
   void OnOutputFormatRequest(int width,
                              int height,
                              const absl::optional<int>& max_fps);
 
+  virtual int GetFrameWidth() const = 0;
+  virtual int GetFrameHeight() const = 0;
+
  protected:
   void OnFrame(const VideoFrame& frame);
   rtc::VideoSinkWants GetSinkWants();
@@ -59,7 +62,7 @@
 
   Mutex lock_;
   std::unique_ptr<FramePreprocessor> preprocessor_ RTC_GUARDED_BY(lock_);
-  bool disable_adaptation_ RTC_GUARDED_BY(lock_) = false;
+  bool enable_adaptation_ RTC_GUARDED_BY(lock_) = false;
   rtc::VideoBroadcaster broadcaster_;
   cricket::VideoAdapter video_adapter_;
 };
diff --git a/test/vcm_capturer.cc b/test/vcm_capturer.cc
index a037f9e..0a9226e 100644
--- a/test/vcm_capturer.cc
+++ b/test/vcm_capturer.cc
@@ -27,6 +27,8 @@
                        size_t height,
                        size_t target_fps,
                        size_t capture_device_index) {
+  width_ = width;
+  height_ = height;
   std::unique_ptr<VideoCaptureModule::DeviceInfo> device_info(
       VideoCaptureFactory::CreateDeviceInfo());
 
diff --git a/test/vcm_capturer.h b/test/vcm_capturer.h
index 5418dc9..da2b948 100644
--- a/test/vcm_capturer.h
+++ b/test/vcm_capturer.h
@@ -31,6 +31,9 @@
 
   void OnFrame(const VideoFrame& frame) override;
 
+  int GetFrameWidth() const override { return static_cast<int>(width_); }
+  int GetFrameHeight() const override { return static_cast<int>(height_); }
+
  private:
   VcmCapturer();
   bool Init(size_t width,
@@ -39,6 +42,8 @@
             size_t capture_device_index);
   void Destroy();
 
+  size_t width_;
+  size_t height_;
   rtc::scoped_refptr<VideoCaptureModule> vcm_;
   VideoCaptureCapability capability_;
 };