1 /*
2  *  Copyright (c) 2016 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 <limits>
12 #include <memory>
13 
14 #include "webrtc/modules/video_coding/utility/default_video_bitrate_allocator.h"
15 #include "webrtc/test/gtest.h"
16 
17 namespace webrtc {
18 namespace {
19 uint32_t kMaxBitrateBps = 1000000;
20 uint32_t kMinBitrateBps = 50000;
21 uint32_t kMaxFramerate = 30;
22 }  // namespace
23 
24 class DefaultVideoBitrateAllocatorTest : public ::testing::Test {
25  public:
DefaultVideoBitrateAllocatorTest()26   DefaultVideoBitrateAllocatorTest() {}
~DefaultVideoBitrateAllocatorTest()27   virtual ~DefaultVideoBitrateAllocatorTest() {}
28 
SetUp()29   void SetUp() override {
30     codec_.codecType = kVideoCodecVP8;
31     codec_.minBitrate = kMinBitrateBps / 1000;
32     codec_.maxBitrate = kMaxBitrateBps / 1000;
33     codec_.targetBitrate = (kMinBitrateBps + kMaxBitrateBps) / 2000;
34     codec_.maxFramerate = kMaxFramerate;
35     allocator_.reset(new DefaultVideoBitrateAllocator(codec_));
36   }
37 
38  protected:
39   VideoCodec codec_;
40   std::unique_ptr<DefaultVideoBitrateAllocator> allocator_;
41 };
42 
TEST_F(DefaultVideoBitrateAllocatorTest,ZeroIsOff)43 TEST_F(DefaultVideoBitrateAllocatorTest, ZeroIsOff) {
44   BitrateAllocation allocation = allocator_->GetAllocation(0, kMaxFramerate);
45   EXPECT_EQ(0u, allocation.get_sum_bps());
46 }
47 
TEST_F(DefaultVideoBitrateAllocatorTest,CapsToMin)48 TEST_F(DefaultVideoBitrateAllocatorTest, CapsToMin) {
49   BitrateAllocation allocation = allocator_->GetAllocation(1, kMaxFramerate);
50   EXPECT_EQ(kMinBitrateBps, allocation.get_sum_bps());
51 
52   allocation = allocator_->GetAllocation(kMinBitrateBps - 1, kMaxFramerate);
53   EXPECT_EQ(kMinBitrateBps, allocation.get_sum_bps());
54 
55   allocation = allocator_->GetAllocation(kMinBitrateBps, kMaxFramerate);
56   EXPECT_EQ(kMinBitrateBps, allocation.get_sum_bps());
57 }
58 
TEST_F(DefaultVideoBitrateAllocatorTest,CapsToMax)59 TEST_F(DefaultVideoBitrateAllocatorTest, CapsToMax) {
60   BitrateAllocation allocation =
61       allocator_->GetAllocation(kMaxBitrateBps, kMaxFramerate);
62   EXPECT_EQ(kMaxBitrateBps, allocation.get_sum_bps());
63 
64   allocation = allocator_->GetAllocation(kMaxBitrateBps + 1, kMaxFramerate);
65   EXPECT_EQ(kMaxBitrateBps, allocation.get_sum_bps());
66 
67   allocation = allocator_->GetAllocation(std::numeric_limits<uint32_t>::max(),
68                                          kMaxFramerate);
69   EXPECT_EQ(kMaxBitrateBps, allocation.get_sum_bps());
70 }
71 
TEST_F(DefaultVideoBitrateAllocatorTest,GoodInBetween)72 TEST_F(DefaultVideoBitrateAllocatorTest, GoodInBetween) {
73   BitrateAllocation allocation =
74       allocator_->GetAllocation(kMinBitrateBps + 1, kMaxFramerate);
75   EXPECT_EQ(kMinBitrateBps + 1, allocation.get_sum_bps());
76 
77   allocation = allocator_->GetAllocation(kMaxBitrateBps - 1, kMaxFramerate);
78   EXPECT_EQ(kMaxBitrateBps - 1, allocation.get_sum_bps());
79 }
80 }  // namespace webrtc
81