diff --git a/pc/BUILD.gn b/pc/BUILD.gn index 66f6cf1f4c..52e2daf24a 100644 --- a/pc/BUILD.gn +++ b/pc/BUILD.gn @@ -499,6 +499,7 @@ if (rtc_include_tests) { "../rtc_base:rtc_base_approved", "../rtc_base:rtc_base_tests_main", "../rtc_base:rtc_base_tests_utils", + "../rtc_base:rtc_task_queue_api", "../system_wrappers:metrics_default", "../system_wrappers:runtime_enabled_features_default", "../test:audio_codec_mocks", diff --git a/pc/videocapturertracksource_unittest.cc b/pc/videocapturertracksource_unittest.cc index 819a7224ce..34d11b58a9 100644 --- a/pc/videocapturertracksource_unittest.cc +++ b/pc/videocapturertracksource_unittest.cc @@ -18,13 +18,18 @@ #include "media/base/fakevideocapturer.h" #include "media/base/fakevideorenderer.h" #include "pc/videocapturertracksource.h" +#include "rtc_base/arraysize.h" +#include "rtc_base/event.h" #include "rtc_base/gunit.h" +#include "rtc_base/task_queue.h" +using cricket::FOURCC_I420; +using cricket::VideoFormat; using webrtc::FakeConstraints; -using webrtc::VideoCapturerTrackSource; using webrtc::MediaConstraintsInterface; using webrtc::MediaSourceInterface; using webrtc::ObserverInterface; +using webrtc::VideoCapturerTrackSource; using webrtc::VideoTrackSourceInterface; namespace { @@ -42,23 +47,13 @@ class TestVideoCapturer : public cricket::FakeVideoCapturer { public: explicit TestVideoCapturer(bool is_screencast) : FakeVideoCapturer(is_screencast), test_without_formats_(false) { - std::vector formats; - formats.push_back( - cricket::VideoFormat(1280, 720, cricket::VideoFormat::FpsToInterval(30), - cricket::FOURCC_I420)); - formats.push_back( - cricket::VideoFormat(640, 480, cricket::VideoFormat::FpsToInterval(30), - cricket::FOURCC_I420)); - formats.push_back( - cricket::VideoFormat(640, 400, cricket::VideoFormat::FpsToInterval(30), - cricket::FOURCC_I420)); - formats.push_back( - cricket::VideoFormat(320, 240, cricket::VideoFormat::FpsToInterval(30), - cricket::FOURCC_I420)); - formats.push_back( - cricket::VideoFormat(352, 288, cricket::VideoFormat::FpsToInterval(30), - cricket::FOURCC_I420)); - ResetSupportedFormats(formats); + static const auto fps = VideoFormat::FpsToInterval(30); + static const VideoFormat formats[] = { + {1280, 720, fps, FOURCC_I420}, {640, 480, fps, FOURCC_I420}, + {640, 400, fps, FOURCC_I420}, {320, 240, fps, FOURCC_I420}, + {352, 288, fps, FOURCC_I420}, + }; + ResetSupportedFormats({&formats[0], &formats[arraysize(formats)]}); } // This function is used for resetting the supported capture formats and @@ -67,22 +62,21 @@ class TestVideoCapturer : public cricket::FakeVideoCapturer { // Chrome implementation. void TestWithoutCameraFormats() { test_without_formats_ = true; - std::vector formats; + std::vector formats; ResetSupportedFormats(formats); } - virtual cricket::CaptureState Start( - const cricket::VideoFormat& capture_format) { + virtual cricket::CaptureState Start(const VideoFormat& capture_format) { if (test_without_formats_) { - std::vector formats; + std::vector formats; formats.push_back(capture_format); ResetSupportedFormats(formats); } return FakeVideoCapturer::Start(capture_format); } - virtual bool GetBestCaptureFormat(const cricket::VideoFormat& desired, - cricket::VideoFormat* best_format) { + virtual bool GetBestCaptureFormat(const VideoFormat& desired, + VideoFormat* best_format) { if (test_without_formats_) { *best_format = desired; return true; @@ -131,6 +125,16 @@ class VideoCapturerTrackSourceTest : public testing::Test { source_->AddOrUpdateSink(&renderer_, rtc::VideoSinkWants()); } + void CaptureSingleFrame() { + rtc::Event event(false, false); + task_queue_.PostTask([this, &event]() { + ASSERT_TRUE(capturer_->CaptureFrame()); + event.Set(); + }); + event.Wait(rtc::Event::kForever); + } + + rtc::TaskQueue task_queue_{"VideoCapturerTrackSourceTest"}; std::unique_ptr capturer_cleanup_; TestVideoCapturer* capturer_; cricket::FakeVideoRenderer renderer_; @@ -147,7 +151,7 @@ TEST_F(VideoCapturerTrackSourceTest, CapturerStartStop) { EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), kMaxWaitMs); - ASSERT_TRUE(capturer_->CaptureFrame()); + CaptureSingleFrame(); EXPECT_EQ(1, renderer_.num_rendered_frames()); capturer_->Stop(); @@ -178,7 +182,7 @@ TEST_F(VideoCapturerTrackSourceTest, MandatoryConstraintCif5Fps) { CreateVideoCapturerSource(&constraints); EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), kMaxWaitMs); - const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); + const VideoFormat* format = capturer_->GetCaptureFormat(); ASSERT_TRUE(format != NULL); EXPECT_EQ(352, format->width); EXPECT_EQ(288, format->height); @@ -198,7 +202,7 @@ TEST_F(VideoCapturerTrackSourceTest, MandatoryMinVgaOptional720P) { CreateVideoCapturerSource(&constraints); EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), kMaxWaitMs); - const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); + const VideoFormat* format = capturer_->GetCaptureFormat(); ASSERT_TRUE(format != NULL); EXPECT_EQ(1280, format->width); EXPECT_EQ(720, format->height); @@ -219,7 +223,7 @@ TEST_F(VideoCapturerTrackSourceTest, MandatoryAspectRatio4To3) { CreateVideoCapturerSource(&constraints); EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), kMaxWaitMs); - const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); + const VideoFormat* format = capturer_->GetCaptureFormat(); ASSERT_TRUE(format != NULL); EXPECT_EQ(640, format->width); EXPECT_EQ(480, format->height); @@ -244,7 +248,7 @@ TEST_F(VideoCapturerTrackSourceTest, OptionalAspectRatioTooHigh) { CreateVideoCapturerSource(&constraints); EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), kMaxWaitMs); - const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); + const VideoFormat* format = capturer_->GetCaptureFormat(); ASSERT_TRUE(format != NULL); double aspect_ratio = static_cast(format->width) / format->height; EXPECT_LT(aspect_ratio, 2); @@ -258,7 +262,7 @@ TEST_F(VideoCapturerTrackSourceTest, NoCameraCapability) { CreateVideoCapturerSource(); EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), kMaxWaitMs); - const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); + const VideoFormat* format = capturer_->GetCaptureFormat(); ASSERT_TRUE(format != NULL); EXPECT_EQ(640, format->width); EXPECT_EQ(480, format->height); @@ -280,7 +284,7 @@ TEST_F(VideoCapturerTrackSourceTest, NoCameraCapability16To9Ratio) { CreateVideoCapturerSource(&constraints); EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), kMaxWaitMs); - const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); + const VideoFormat* format = capturer_->GetCaptureFormat(); double aspect_ratio = static_cast(format->width) / format->height; EXPECT_LE(requested_aspect_ratio, aspect_ratio); } @@ -395,7 +399,7 @@ TEST_F(VideoCapturerTrackSourceTest, MixedOptionsAndConstraints) { CreateVideoCapturerSource(&constraints); EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), kMaxWaitMs); - const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); + const VideoFormat* format = capturer_->GetCaptureFormat(); ASSERT_TRUE(format != NULL); EXPECT_EQ(352, format->width); EXPECT_EQ(288, format->height); @@ -414,7 +418,7 @@ TEST_F(VideoCapturerTrackSourceTest, ScreencastResolutionNoConstraint) { ASSERT_TRUE(source_->is_screencast()); EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), kMaxWaitMs); - const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); + const VideoFormat* format = capturer_->GetCaptureFormat(); ASSERT_TRUE(format != NULL); EXPECT_EQ(640, format->width); EXPECT_EQ(480, format->height); @@ -435,7 +439,7 @@ TEST_F(VideoCapturerTrackSourceTest, ScreencastResolutionWithConstraint) { ASSERT_TRUE(source_->is_screencast()); EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), kMaxWaitMs); - const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); + const VideoFormat* format = capturer_->GetCaptureFormat(); ASSERT_TRUE(format != NULL); EXPECT_EQ(480, format->width); EXPECT_EQ(270, format->height); @@ -459,7 +463,7 @@ TEST_F(VideoCapturerTrackSourceTest, OptionalSubOneFpsConstraints) { CreateVideoCapturerSource(&constraints); EXPECT_EQ_WAIT(MediaSourceInterface::kLive, state_observer_->state(), kMaxWaitMs); - const cricket::VideoFormat* format = capturer_->GetCaptureFormat(); + const VideoFormat* format = capturer_->GetCaptureFormat(); ASSERT_TRUE(format != NULL); EXPECT_EQ(1, format->framerate()); }