From 6fd5f33d45b5f154a8ed37b539c6e8d2beff25dd Mon Sep 17 00:00:00 2001 From: Artem Titov Date: Sat, 25 Mar 2023 21:15:09 +0100 Subject: [PATCH] Extend TestVideoTrackSource API Bug: b/272350185 Change-Id: Ibc53e7a9ee8f572475d86fc78de1c1ed71078910 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/299140 Reviewed-by: Mirko Bonadei Commit-Queue: Artem Titov Cr-Commit-Position: refs/heads/main@{#39678} --- api/test/video/test_video_track_source.h | 10 +++ test/frame_generator_capturer.cc | 72 +++++++++---------- test/frame_generator_capturer.h | 8 +-- test/frame_generator_capturer_unittest.cc | 4 +- test/mac_capturer.h | 6 ++ test/mac_capturer.mm | 2 + .../test_video_capturer_video_track_source.h | 18 ++++- test/test_video_capturer.cc | 7 +- test/test_video_capturer.h | 9 ++- test/vcm_capturer.cc | 2 + test/vcm_capturer.h | 5 ++ 11 files changed, 94 insertions(+), 49 deletions(-) diff --git a/api/test/video/test_video_track_source.h b/api/test/video/test_video_track_source.h index 4e2d138be0..449228f81c 100644 --- a/api/test/video/test_video_track_source.h +++ b/api/test/video/test_video_track_source.h @@ -62,6 +62,16 @@ class TestVideoTrackSource : public Notifier { 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& max_fps) {} + protected: virtual rtc::VideoSourceInterface* source() = 0; diff --git a/test/frame_generator_capturer.cc b/test/frame_generator_capturer.cc index c69fca0965..5d0e2e8d9a 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 @@ std::string TransformFilePath(std::string path) { } return path; } + } // namespace FrameGeneratorCapturer::FrameGeneratorCapturer( @@ -177,49 +179,39 @@ bool FrameGeneratorCapturer::Init() { } void FrameGeneratorCapturer::InsertFrame() { - absl::optional 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(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(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::GetResolution() { - MutexLock lock(&stats_lock_); - return source_resolution_; +FrameGeneratorCapturer::GetResolution() const { + FrameGeneratorInterface::Resolution resolution = + frame_generator_->GetResolution(); + return Resolution{.width = static_cast(resolution.width), + .height = static_cast(resolution.height)}; } void FrameGeneratorCapturer::Start() { @@ -266,6 +258,14 @@ void FrameGeneratorCapturer::ChangeFramerate(int target_framerate) { target_capture_fps_ = std::min(source_fps_, target_framerate); } +int FrameGeneratorCapturer::GetFrameWidth() const { + return static_cast(frame_generator_->GetResolution().width); +} + +int FrameGeneratorCapturer::GetFrameHeight() const { + return static_cast(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 e310e40129..20b355669f 100644 --- a/test/frame_generator_capturer.h +++ b/test/frame_generator_capturer.h @@ -132,11 +132,14 @@ class FrameGeneratorCapturer : public TestVideoCapturer { 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 GetResolution(); + absl::optional GetResolution() const; void OnOutputFormatRequest(int width, int height, @@ -178,9 +181,6 @@ class FrameGeneratorCapturer : public TestVideoCapturer { int64_t first_frame_capture_time_; - Mutex stats_lock_; - absl::optional 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 d8371f4efd..0098b0e3b6 100644 --- a/test/frame_generator_capturer_unittest.cc +++ b/test/frame_generator_capturer_unittest.cc @@ -71,7 +71,9 @@ TEST(FrameGeneratorCapturerTest, ChangeResolution) { 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 3d7ee77b45..35cd1ccd1e 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 #include #include @@ -33,6 +34,9 @@ class MacCapturer : public TestVideoCapturer, void OnFrame(const VideoFrame& frame) override; + int GetFrameWidth() const override { return static_cast(width_); } + int GetFrameHeight() const override { return static_cast(height_); } + private: MacCapturer(size_t width, size_t height, @@ -40,6 +44,8 @@ class MacCapturer : public TestVideoCapturer, 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 da8e9b76b6..9b14f28c2a 100644 --- a/test/mac_capturer.mm +++ b/test/mac_capturer.mm @@ -64,6 +64,8 @@ MacCapturer::MacCapturer(size_t width, 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 350766bb49..70db07b31c 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 @@ class TestVideoCapturerVideoTrackSource : public test::TestVideoTrackSource { 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& 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 b55eefcb2f..3098731eb3 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 @@ void TestVideoCapturer::OnFrame(const VideoFrame& original_frame) { 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 3fc03f07eb..48b7f7a7f8 100644 --- a/test/test_video_capturer.h +++ b/test/test_video_capturer.h @@ -41,14 +41,17 @@ class TestVideoCapturer : public rtc::VideoSourceInterface { 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& 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 @@ class TestVideoCapturer : public rtc::VideoSourceInterface { Mutex lock_; std::unique_ptr 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 a037f9eff6..0a9226ef6f 100644 --- a/test/vcm_capturer.cc +++ b/test/vcm_capturer.cc @@ -27,6 +27,8 @@ bool VcmCapturer::Init(size_t width, size_t height, size_t target_fps, size_t capture_device_index) { + width_ = width; + height_ = height; std::unique_ptr device_info( VideoCaptureFactory::CreateDeviceInfo()); diff --git a/test/vcm_capturer.h b/test/vcm_capturer.h index 5418dc9596..da2b948fe0 100644 --- a/test/vcm_capturer.h +++ b/test/vcm_capturer.h @@ -31,6 +31,9 @@ class VcmCapturer : public TestVideoCapturer, void OnFrame(const VideoFrame& frame) override; + int GetFrameWidth() const override { return static_cast(width_); } + int GetFrameHeight() const override { return static_cast(height_); } + private: VcmCapturer(); bool Init(size_t width, @@ -39,6 +42,8 @@ class VcmCapturer : public TestVideoCapturer, size_t capture_device_index); void Destroy(); + size_t width_; + size_t height_; rtc::scoped_refptr vcm_; VideoCaptureCapability capability_; };