video_source_restrictions_unittest.cc (6337B)
1 /* 2 * Copyright (c) 2020 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #include "call/adaptation/video_source_restrictions.h" 12 13 #include <cstddef> 14 #include <optional> 15 16 #include "test/gtest.h" 17 namespace webrtc { 18 19 namespace { 20 21 constexpr size_t kHdPixels = 1280 * 720; 22 23 const VideoSourceRestrictions kUnlimited; 24 const VideoSourceRestrictions k15fps(std::nullopt, std::nullopt, 15.0); 25 const VideoSourceRestrictions kHd(kHdPixels, kHdPixels, std::nullopt); 26 const VideoSourceRestrictions kHd15fps(kHdPixels, kHdPixels, 15.0); 27 const VideoSourceRestrictions kVga7fps(kHdPixels / 2, kHdPixels / 2, 7.0); 28 29 VideoSourceRestrictions RestrictionsFromMaxPixelsPerFrame( 30 size_t max_pixels_per_frame) { 31 return VideoSourceRestrictions(max_pixels_per_frame, std::nullopt, 32 std::nullopt); 33 } 34 35 VideoSourceRestrictions RestrictionsFromMaxFrameRate(double max_frame_rate) { 36 return VideoSourceRestrictions(std::nullopt, std::nullopt, max_frame_rate); 37 } 38 39 } // namespace 40 41 TEST(VideoSourceRestrictionsTest, DidIncreaseResolution) { 42 // smaller restrictions -> larger restrictions 43 EXPECT_TRUE(DidIncreaseResolution(RestrictionsFromMaxPixelsPerFrame(10), 44 RestrictionsFromMaxPixelsPerFrame(11))); 45 // unrestricted -> restricted 46 EXPECT_FALSE(DidIncreaseResolution(VideoSourceRestrictions(), 47 RestrictionsFromMaxPixelsPerFrame(10))); 48 // restricted -> unrestricted 49 EXPECT_TRUE(DidIncreaseResolution(RestrictionsFromMaxPixelsPerFrame(10), 50 VideoSourceRestrictions())); 51 // restricted -> equally restricted 52 EXPECT_FALSE(DidIncreaseResolution(RestrictionsFromMaxPixelsPerFrame(10), 53 RestrictionsFromMaxPixelsPerFrame(10))); 54 // unrestricted -> unrestricted 55 EXPECT_FALSE(DidIncreaseResolution(VideoSourceRestrictions(), 56 VideoSourceRestrictions())); 57 // larger restrictions -> smaller restrictions 58 EXPECT_FALSE(DidIncreaseResolution(RestrictionsFromMaxPixelsPerFrame(10), 59 RestrictionsFromMaxPixelsPerFrame(9))); 60 } 61 62 TEST(VideoSourceRestrictionsTest, DidDecreaseFrameRate) { 63 // samller restrictions -> larger restrictions 64 EXPECT_FALSE(DidDecreaseFrameRate(RestrictionsFromMaxFrameRate(10), 65 RestrictionsFromMaxFrameRate(11))); 66 // unrestricted -> restricted 67 EXPECT_TRUE(DidDecreaseFrameRate(VideoSourceRestrictions(), 68 RestrictionsFromMaxFrameRate(10))); 69 // restricted -> unrestricted 70 EXPECT_FALSE(DidDecreaseFrameRate(RestrictionsFromMaxFrameRate(10), 71 VideoSourceRestrictions())); 72 // restricted -> equally restricted 73 EXPECT_FALSE(DidDecreaseFrameRate(RestrictionsFromMaxFrameRate(10), 74 RestrictionsFromMaxFrameRate(10))); 75 // unrestricted -> unrestricted 76 EXPECT_FALSE(DidDecreaseFrameRate(VideoSourceRestrictions(), 77 VideoSourceRestrictions())); 78 // larger restrictions -> samller restrictions 79 EXPECT_TRUE(DidDecreaseFrameRate(RestrictionsFromMaxFrameRate(10), 80 RestrictionsFromMaxFrameRate(9))); 81 } 82 83 TEST(VideoSourceRestrictionsTest, DidRestrictionsChangeFalseForSame) { 84 EXPECT_FALSE(DidRestrictionsDecrease(kUnlimited, kUnlimited)); 85 EXPECT_FALSE(DidRestrictionsIncrease(kUnlimited, kUnlimited)); 86 87 // Both resolution and fps restricted. 88 EXPECT_FALSE(DidRestrictionsDecrease(kHd15fps, kHd15fps)); 89 EXPECT_FALSE(DidRestrictionsIncrease(kHd15fps, kHd15fps)); 90 } 91 92 TEST(VideoSourceRestrictions, 93 DidRestrictionsIncreaseTrueWhenPixelsOrFrameRateDecreased) { 94 // Unlimited > Limited resolution. 95 EXPECT_TRUE(DidRestrictionsIncrease(kUnlimited, kHd)); 96 // Unlimited > limited fps. 97 EXPECT_TRUE(DidRestrictionsIncrease(kUnlimited, k15fps)); 98 // Unlimited > limited resolution + limited fps. 99 EXPECT_TRUE(DidRestrictionsIncrease(kUnlimited, kHd15fps)); 100 // Limited resolution > limited resolution + limited fps. 101 EXPECT_TRUE(DidRestrictionsIncrease(kHd, kHd15fps)); 102 // Limited fps > limited resolution + limited fps. 103 EXPECT_TRUE(DidRestrictionsIncrease(k15fps, kHd15fps)); 104 // Limited resolution + fps > More limited resolution + more limited fps 105 EXPECT_TRUE(DidRestrictionsIncrease(kHd15fps, kVga7fps)); 106 } 107 108 TEST(VideoSourceRestrictions, 109 DidRestrictionsDecreaseTrueWhenPixelsOrFrameRateIncreased) { 110 // Limited resolution < Unlimited. 111 EXPECT_TRUE(DidRestrictionsDecrease(kHd, kUnlimited)); 112 // Limited fps < Unlimited. 113 EXPECT_TRUE(DidRestrictionsDecrease(k15fps, kUnlimited)); 114 // Limited resolution + limited fps < unlimited. 115 EXPECT_TRUE(DidRestrictionsDecrease(kHd15fps, kUnlimited)); 116 // Limited resolution + limited fps < limited resolution. 117 EXPECT_TRUE(DidRestrictionsDecrease(kHd15fps, kHd)); 118 // Limited resolution + limited fps < limited fps. 119 EXPECT_TRUE(DidRestrictionsDecrease(kHd15fps, k15fps)); 120 // More limited resolution + more limited fps < limited resolution + fps 121 EXPECT_TRUE(DidRestrictionsDecrease(kVga7fps, kHd15fps)); 122 } 123 124 TEST(VideoSourceRestrictions, 125 DidRestrictionsChangeFalseWhenFrameRateAndPixelsChangeDifferently) { 126 // One changed framerate, the other resolution; not an increase or decrease. 127 EXPECT_FALSE(DidRestrictionsIncrease(kHd, k15fps)); 128 EXPECT_FALSE(DidRestrictionsDecrease(kHd, k15fps)); 129 } 130 131 TEST(VideoSourceRestrictions, UpdateMin) { 132 VideoSourceRestrictions one(kHdPixels / 2, kHdPixels, 7.0); 133 VideoSourceRestrictions two(kHdPixels, kHdPixels / 3, 15.0); 134 135 one.UpdateMin(two); 136 137 EXPECT_EQ(one.max_pixels_per_frame(), kHdPixels / 2); 138 EXPECT_EQ(one.target_pixels_per_frame(), kHdPixels / 3); 139 EXPECT_EQ(one.max_frame_rate(), 7.0); 140 141 two.UpdateMin(one); 142 143 EXPECT_EQ(two.max_pixels_per_frame(), kHdPixels / 2); 144 EXPECT_EQ(two.target_pixels_per_frame(), kHdPixels / 3); 145 EXPECT_EQ(two.max_frame_rate(), 7.0); 146 } 147 148 } // namespace webrtc