1 /*
2  *  Copyright (c) 2017 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 "modules/congestion_controller/goog_cc/acknowledged_bitrate_estimator.h"
12 
13 #include <memory>
14 #include <utility>
15 
16 #include "api/transport/field_trial_based_config.h"
17 #include "rtc_base/fake_clock.h"
18 #include "test/gmock.h"
19 #include "test/gtest.h"
20 
21 using ::testing::_;
22 using ::testing::InSequence;
23 using ::testing::NiceMock;
24 using ::testing::Return;
25 
26 namespace webrtc {
27 
28 namespace {
29 
30 constexpr int64_t kFirstArrivalTimeMs = 10;
31 constexpr int64_t kFirstSendTimeMs = 10;
32 constexpr uint16_t kSequenceNumber = 1;
33 constexpr size_t kPayloadSize = 10;
34 
35 class MockBitrateEstimator : public BitrateEstimator {
36  public:
37   using BitrateEstimator::BitrateEstimator;
38   MOCK_METHOD(void,
39               Update,
40               (Timestamp at_time, DataSize data_size, bool in_alr),
41               (override));
42   MOCK_METHOD(absl::optional<DataRate>, bitrate, (), (const, override));
43   MOCK_METHOD(void, ExpectFastRateChange, (), (override));
44 };
45 
46 struct AcknowledgedBitrateEstimatorTestStates {
47   FieldTrialBasedConfig field_trial_config;
48   std::unique_ptr<AcknowledgedBitrateEstimator> acknowledged_bitrate_estimator;
49   MockBitrateEstimator* mock_bitrate_estimator;
50 };
51 
CreateTestStates()52 AcknowledgedBitrateEstimatorTestStates CreateTestStates() {
53   AcknowledgedBitrateEstimatorTestStates states;
54   auto mock_bitrate_estimator =
55       std::make_unique<MockBitrateEstimator>(&states.field_trial_config);
56   states.mock_bitrate_estimator = mock_bitrate_estimator.get();
57   states.acknowledged_bitrate_estimator =
58       std::make_unique<AcknowledgedBitrateEstimator>(
59           &states.field_trial_config, std::move(mock_bitrate_estimator));
60   return states;
61 }
62 
CreateFeedbackVector()63 std::vector<PacketResult> CreateFeedbackVector() {
64   std::vector<PacketResult> packet_feedback_vector(2);
65   packet_feedback_vector[0].receive_time =
66       Timestamp::Millis(kFirstArrivalTimeMs);
67   packet_feedback_vector[0].sent_packet.send_time =
68       Timestamp::Millis(kFirstSendTimeMs);
69   packet_feedback_vector[0].sent_packet.sequence_number = kSequenceNumber;
70   packet_feedback_vector[0].sent_packet.size = DataSize::Bytes(kPayloadSize);
71   packet_feedback_vector[1].receive_time =
72       Timestamp::Millis(kFirstArrivalTimeMs + 10);
73   packet_feedback_vector[1].sent_packet.send_time =
74       Timestamp::Millis(kFirstSendTimeMs + 10);
75   packet_feedback_vector[1].sent_packet.sequence_number = kSequenceNumber;
76   packet_feedback_vector[1].sent_packet.size =
77       DataSize::Bytes(kPayloadSize + 10);
78   return packet_feedback_vector;
79 }
80 
81 }  // anonymous namespace
82 
TEST(TestAcknowledgedBitrateEstimator,UpdateBandwidth)83 TEST(TestAcknowledgedBitrateEstimator, UpdateBandwidth) {
84   auto states = CreateTestStates();
85   auto packet_feedback_vector = CreateFeedbackVector();
86   {
87     InSequence dummy;
88     EXPECT_CALL(*states.mock_bitrate_estimator,
89                 Update(packet_feedback_vector[0].receive_time,
90                        packet_feedback_vector[0].sent_packet.size,
91                        /*in_alr*/ false))
92         .Times(1);
93     EXPECT_CALL(*states.mock_bitrate_estimator,
94                 Update(packet_feedback_vector[1].receive_time,
95                        packet_feedback_vector[1].sent_packet.size,
96                        /*in_alr*/ false))
97         .Times(1);
98   }
99   states.acknowledged_bitrate_estimator->IncomingPacketFeedbackVector(
100       packet_feedback_vector);
101 }
102 
TEST(TestAcknowledgedBitrateEstimator,ExpectFastRateChangeWhenLeftAlr)103 TEST(TestAcknowledgedBitrateEstimator, ExpectFastRateChangeWhenLeftAlr) {
104   auto states = CreateTestStates();
105   auto packet_feedback_vector = CreateFeedbackVector();
106   {
107     InSequence dummy;
108     EXPECT_CALL(*states.mock_bitrate_estimator,
109                 Update(packet_feedback_vector[0].receive_time,
110                        packet_feedback_vector[0].sent_packet.size,
111                        /*in_alr*/ false))
112         .Times(1);
113     EXPECT_CALL(*states.mock_bitrate_estimator, ExpectFastRateChange())
114         .Times(1);
115     EXPECT_CALL(*states.mock_bitrate_estimator,
116                 Update(packet_feedback_vector[1].receive_time,
117                        packet_feedback_vector[1].sent_packet.size,
118                        /*in_alr*/ false))
119         .Times(1);
120   }
121   states.acknowledged_bitrate_estimator->SetAlrEndedTime(
122       Timestamp::Millis(kFirstArrivalTimeMs + 1));
123   states.acknowledged_bitrate_estimator->IncomingPacketFeedbackVector(
124       packet_feedback_vector);
125 }
126 
TEST(TestAcknowledgedBitrateEstimator,ReturnBitrate)127 TEST(TestAcknowledgedBitrateEstimator, ReturnBitrate) {
128   auto states = CreateTestStates();
129   absl::optional<DataRate> return_value = DataRate::KilobitsPerSec(42);
130   EXPECT_CALL(*states.mock_bitrate_estimator, bitrate())
131       .Times(1)
132       .WillOnce(Return(return_value));
133   EXPECT_EQ(return_value, states.acknowledged_bitrate_estimator->bitrate());
134 }
135 
136 }  // namespace webrtc*/
137