goog_cc 0.1.4

A direct Rust port of the Google Congestion Control algorithm from WebRTC
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
/*
 *  Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

use std::collections::VecDeque;

use crate::{
    api::{
        network_control::{NetworkControllerConfig, NetworkControllerInterface},
        transport::{
            BandwidthUsage, NetworkAvailability, NetworkControlUpdate, NetworkEstimate,
            NetworkRouteChange, NetworkStateEstimate, PacedPacketInfo, PacerConfig,
            ProbeClusterConfig, ProcessInterval, ReceivedPacket, RemoteBitrateReport,
            RoundTripTimeUpdate, SentPacket, StreamsConfig, TargetRateConstraints,
            TargetTransferRate, TransportLossReport, TransportPacketsFeedback,
        },
        units::{DataRate, DataSize, TimeDelta, Timestamp},
    },
    experiments::{FieldTrials, RateControlSettings},
    AcknowledgedBitrateEstimator,
    remote_bitrate_estimator::CONGESTION_CONTROLLER_MIN_BITRATE,
};

use super::{
    AcknowledgedBitrateEstimatorInterface, AlrDetector, BandwidthLimitedCause,
    CongestionWindowPushbackController, DelayBasedBwe, DelayBasedBweResult, LossBasedState,
    ProbeBitrateEstimator, ProbeController, SendSideBandwidthEstimation,
};

#[derive(Clone, Debug)]
pub struct SafeResetOnRouteChange {
    pub enabled: bool, // Enabled
    pub ack: bool,     // ack
}

impl Default for SafeResetOnRouteChange {
    fn default() -> Self {
        Self {
            enabled: true,
            ack: true,
        }
    }
}

/// Google Congestion Control specific configuration.
///
/// NOTE: NetworkStateEstimator and NetworkStatePredictor have not been ported.
/// These are optional traits and do not seem to be implemented, except perhaps by Google internally.
#[derive(Clone, Debug, Default)]
pub struct GoogCcConfig {
    // network_state_estimator: Box<dyn NetworkStateEstimator>,
    // network_state_predictor: Box<dyn NetworkStatePredictor>,
    pub feedback_only: bool,
}

/// Google Congestion Control.
///
/// This is the congestion controller used within WebRTC.
/// It has been extracted from the libwebrtc source code and ported to Rust to avoid linking headaches.
/// Use the [NetworkControllerInterface] to interact with this controller.
pub struct GoogCcNetworkController {
    field_trials: FieldTrials,

    packet_feedback_only: bool,
    safe_reset_on_route_change: bool,
    safe_reset_acknowledged_rate: bool,
    use_min_allocatable_as_lower_bound: bool,
    ignore_probes_lower_than_network_estimate: bool,
    limit_probes_lower_than_throughput_estimate: bool,
    rate_control_settings: RateControlSettings,
    pace_at_max_of_bwe_and_lower_link_capacity: bool,
    limit_pacingfactor_by_upper_link_capacity_estimate: bool,

    probe_controller: ProbeController,
    congestion_window_pushback_controller: Option<CongestionWindowPushbackController>,

    bandwidth_estimation: SendSideBandwidthEstimation,
    alr_detector: AlrDetector,
    probe_bitrate_estimator: ProbeBitrateEstimator,
    delay_based_bwe: DelayBasedBwe,
    acknowledged_bitrate_estimator: Box<dyn AcknowledgedBitrateEstimatorInterface>,

    initial_config: Option<NetworkControllerConfig>,

    min_target_rate: DataRate,
    min_data_rate: DataRate,
    max_data_rate: DataRate, // = DataRate::plus_infinity();
    starting_rate: Option<DataRate>,

    first_packet_sent: bool,

    estimate: Option<NetworkStateEstimate>,

    next_loss_update: Timestamp, // = Timestamp::minus_infinity();
    lost_packets_since_last_loss_update: i64,
    expected_packets_since_last_loss_update: i64,

    feedback_max_rtts: VecDeque<i64>,

    last_loss_based_target_rate: DataRate,
    last_pushback_target_rate: DataRate,
    last_stable_target_rate: DataRate,
    last_loss_base_state: LossBasedState,

    last_estimated_fraction_loss: u8,
    last_estimated_round_trip_time: TimeDelta, //= TimeDelta::plus_infinity();

    pacing_factor: f64,
    min_total_allocated_bitrate: DataRate,
    max_padding_rate: DataRate,

    previously_in_alr: bool,

    current_data_window: Option<DataSize>,
}

impl GoogCcNetworkController {
    // From RTCPSender video report interval.
    const LOSS_UPDATE_INTERVAL: TimeDelta = TimeDelta::from_millis(1000);

    // Pacing-rate relative to our target send rate.
    // Multiplicative factor that is applied to the target bitrate to calculate
    // the number of bytes that can be transmitted per interval.
    // Increasing this factor will result in lower delays in cases of bitrate
    // overshoots from the encoder.
    const DEFAULT_PACE_MULTIPLIER: f64 = 2.5;

    // If the probe result is far below the current throughput estimate
    // it's unlikely that the probe is accurate, so we don't want to drop too far.
    // However, if we actually are overusing, we want to drop to something slightly
    // below the current throughput estimate to drain the network queues.
    const PROBE_DROP_THROUGHPUT_FRACTION: f64 = 0.85;

    pub fn new(config: NetworkControllerConfig, goog_cc_config: GoogCcConfig) -> Self {
        let field_trials = config.field_trials.clone();
        let rate_control_settings = RateControlSettings::new(&field_trials);

        assert!(config.constraints.at_time.is_finite());
        let mut delay_based_bwe = DelayBasedBwe::new(&field_trials);
        delay_based_bwe.set_min_bitrate(CONGESTION_CONTROLLER_MIN_BITRATE);

        Self {
            packet_feedback_only: goog_cc_config.feedback_only,
            safe_reset_on_route_change: field_trials.safe_reset_on_route_change.enabled,
            safe_reset_acknowledged_rate: field_trials.safe_reset_on_route_change.ack,
            use_min_allocatable_as_lower_bound: field_trials.min_alloc_as_lower_bound,
            ignore_probes_lower_than_network_estimate: field_trials
                .ignore_probes_lower_than_network_state_estimate,
            limit_probes_lower_than_throughput_estimate: field_trials
                .limit_probes_lower_than_throughput_estimate,
            pace_at_max_of_bwe_and_lower_link_capacity: field_trials
                .pace_at_max_of_bwe_and_lower_link_capacity,
            limit_pacingfactor_by_upper_link_capacity_estimate: field_trials
                .limit_pacing_factor_by_upper_link_capacity_estimate,

            probe_controller: ProbeController::new(field_trials.probing_configuration.clone()),
            congestion_window_pushback_controller: rate_control_settings
                .use_congestion_window_pushback()
                .then(|| CongestionWindowPushbackController::new(&field_trials)),
            rate_control_settings,
            bandwidth_estimation: SendSideBandwidthEstimation::new(field_trials.clone()),
            alr_detector: AlrDetector::new(field_trials.alr_detector_parameters.clone()),
            probe_bitrate_estimator: ProbeBitrateEstimator::default(),
            delay_based_bwe,
            acknowledged_bitrate_estimator: AcknowledgedBitrateEstimator::create(&field_trials),

            min_target_rate: DataRate::zero(),
            min_data_rate: DataRate::zero(),
            max_data_rate: DataRate::plus_infinity(),
            starting_rate: config.constraints.starting_rate,
            first_packet_sent: false,
            estimate: None,
            next_loss_update: Timestamp::minus_infinity(),
            lost_packets_since_last_loss_update: 0,
            expected_packets_since_last_loss_update: 0,
            feedback_max_rtts: VecDeque::new(),
            last_loss_based_target_rate: config.constraints.starting_rate.unwrap(),
            last_pushback_target_rate: config.constraints.starting_rate.unwrap(),
            last_stable_target_rate: config.constraints.starting_rate.unwrap(),
            last_loss_base_state: LossBasedState::DelayBasedEstimate,
            last_estimated_fraction_loss: 0,
            last_estimated_round_trip_time: TimeDelta::plus_infinity(),
            pacing_factor: config
                .stream_based_config
                .pacing_factor
                .unwrap_or(Self::DEFAULT_PACE_MULTIPLIER),
            min_total_allocated_bitrate: config
                .stream_based_config
                .min_total_allocated_bitrate
                .unwrap_or(DataRate::zero()),

            max_padding_rate: config
                .stream_based_config
                .max_padding_rate
                .unwrap_or(DataRate::zero()),
            previously_in_alr: false,
            current_data_window: None,

            initial_config: Some(config),
            field_trials,
        }
    }

    pub fn get_network_state(&self, at_time: Timestamp) -> NetworkControlUpdate {
        NetworkControlUpdate {
            target_rate: Some(TargetTransferRate {
                network_estimate: NetworkEstimate {
                    at_time,
                    loss_rate_ratio: self.last_estimated_fraction_loss as f32 / 255.0,
                    round_trip_time: self.last_estimated_round_trip_time,
                    bwe_period: self.delay_based_bwe.get_expected_bwe_period(),
                    ..Default::default()
                },
                at_time,
                target_rate: self.last_pushback_target_rate,
                stable_target_rate: self.bandwidth_estimation.get_estimated_link_capacity(),
                ..Default::default()
            }),
            pacer_config: Some(self.get_pacing_rates(at_time)),
            congestion_window: self.current_data_window,
            ..Default::default()
        }
    }

    fn reset_constraints(
        &mut self,
        new_constraints: TargetRateConstraints,
    ) -> Vec<ProbeClusterConfig> {
        self.min_target_rate = new_constraints.min_data_rate.unwrap_or(DataRate::zero());
        self.max_data_rate = new_constraints
            .max_data_rate
            .unwrap_or(DataRate::plus_infinity());
        self.starting_rate = new_constraints.starting_rate;
        self.clamp_constraints();

        self.bandwidth_estimation.set_bitrates(
            self.starting_rate,
            self.min_data_rate,
            self.max_data_rate,
            new_constraints.at_time,
        );

        if let Some(starting_rate) = self.starting_rate {
            self.delay_based_bwe.set_start_bitrate(starting_rate);
        }
        self.delay_based_bwe.set_min_bitrate(self.min_data_rate);

        self.probe_controller.set_bitrates(
            self.min_data_rate,
            self.starting_rate.unwrap_or(DataRate::zero()),
            self.max_data_rate,
            new_constraints.at_time,
        )
    }

    fn clamp_constraints(&mut self) {
        // TODO(holmer): We should make sure the default bitrates are set to 10 kbps,
        // and that we don't try to set the min bitrate to 0 from any applications.
        // The congestion controller should allow a min bitrate of 0.
        self.min_data_rate = std::cmp::max(self.min_target_rate, CONGESTION_CONTROLLER_MIN_BITRATE);
        if self.use_min_allocatable_as_lower_bound {
            self.min_data_rate =
                std::cmp::max(self.min_data_rate, self.min_total_allocated_bitrate);
        }
        if self.max_data_rate < self.min_data_rate {
            tracing::warn!("max bitrate smaller than min bitrate");
            self.max_data_rate = self.min_data_rate;
        }
        match self.starting_rate {
            Some(starting_rate) if starting_rate < self.min_data_rate => {
                tracing::warn!("start bitrate smaller than min bitrate");
                self.starting_rate = Some(self.min_data_rate);
            }
            _ => {}
        }
    }
    fn maybe_trigger_on_network_changed(
        &mut self,
        update: &mut NetworkControlUpdate,
        at_time: Timestamp,
    ) {
        let fraction_loss: u8 = self.bandwidth_estimation.fraction_loss();
        let round_trip_time: TimeDelta = self.bandwidth_estimation.round_trip_time();
        let loss_based_target_rate: DataRate = self.bandwidth_estimation.target_rate();
        let loss_based_state: LossBasedState = self.bandwidth_estimation.loss_based_state();
        let mut pushback_target_rate: DataRate = loss_based_target_rate;

        let mut cwnd_reduce_ratio: f64 = 0.0;
        if let Some(congestion_window_pushback_controller) =
            &mut self.congestion_window_pushback_controller
        {
            let mut pushback_rate: i64 = congestion_window_pushback_controller
                .update_target_bitrate(loss_based_target_rate.bps() as _)
                as _;
            pushback_rate = self
                .bandwidth_estimation
                .get_min_bitrate()
                .max(pushback_rate);
            pushback_target_rate = DataRate::from_bits_per_sec(pushback_rate);
            if self
                .rate_control_settings
                .use_congestion_window_drop_frame_only()
            {
                cwnd_reduce_ratio = (loss_based_target_rate.bps_float()
                    - pushback_target_rate.bps_float())
                    / loss_based_target_rate.bps_float();
            }
        }
        let mut stable_target_rate: DataRate =
            self.bandwidth_estimation.get_estimated_link_capacity();
        stable_target_rate = std::cmp::min(stable_target_rate, pushback_target_rate);

        if (loss_based_target_rate != self.last_loss_based_target_rate)
            || (loss_based_state != self.last_loss_base_state)
            || (fraction_loss != self.last_estimated_fraction_loss)
            || (round_trip_time != self.last_estimated_round_trip_time)
            || (pushback_target_rate != self.last_pushback_target_rate)
            || (stable_target_rate != self.last_stable_target_rate)
        {
            self.last_loss_based_target_rate = loss_based_target_rate;
            self.last_pushback_target_rate = pushback_target_rate;
            self.last_estimated_fraction_loss = fraction_loss;
            self.last_estimated_round_trip_time = round_trip_time;
            self.last_stable_target_rate = stable_target_rate;
            self.last_loss_base_state = loss_based_state;

            self.alr_detector
                .set_estimated_bitrate(loss_based_target_rate.bps());

            let bwe_period: TimeDelta = self.delay_based_bwe.get_expected_bwe_period();

            let mut target_rate_msg = TargetTransferRate {
                at_time,
                stable_target_rate,
                network_estimate: NetworkEstimate {
                    at_time,
                    round_trip_time,
                    loss_rate_ratio: fraction_loss as f32 / 255.0,
                    bwe_period,
                    ..Default::default()
                },
                ..Default::default()
            };

            if self
                .rate_control_settings
                .use_congestion_window_drop_frame_only()
            {
                target_rate_msg.target_rate = loss_based_target_rate;
                target_rate_msg.cwnd_reduce_ratio = cwnd_reduce_ratio;
            } else {
                target_rate_msg.target_rate = pushback_target_rate;
            }

            update.target_rate = Some(target_rate_msg);

            let mut probes = self.probe_controller.set_estimated_bitrate(
                loss_based_target_rate,
                get_bandwidth_limited_cause(
                    self.bandwidth_estimation.loss_based_state(),
                    self.bandwidth_estimation.is_rtt_above_limit(),
                    self.delay_based_bwe.last_state(),
                ),
                at_time,
            );
            update.probe_cluster_configs.append(&mut probes);
            update.pacer_config = Some(self.get_pacing_rates(at_time));
            tracing::trace!(
                "bwe {} pushback_target_bps={} estimate_bps={}",
                at_time.ms(),
                self.last_pushback_target_rate.bps(),
                loss_based_target_rate.bps()
            );
        }
    }

    fn get_pacing_rates(&self, at_time: Timestamp) -> PacerConfig {
        // Pacing rate is based on target rate before congestion window pushback,
        // because we don't want to build queues in the pacer when pushback occurs.
        let mut pacing_rate = match self.estimate {
            Some(estimate)
                if self.pace_at_max_of_bwe_and_lower_link_capacity
                    && !self.bandwidth_estimation.pace_at_loss_based_estimate() =>
            {
                *[
                    self.min_total_allocated_bitrate,
                    estimate.link_capacity_lower,
                    self.last_loss_based_target_rate,
                ]
                .iter()
                .max()
                .unwrap()
                    * self.pacing_factor
            }
            _ => {
                self.min_total_allocated_bitrate
                    .max(self.last_loss_based_target_rate)
                    * self.pacing_factor
            }
        };

        match self.estimate {
            Some(estimate)
                if self.limit_pacingfactor_by_upper_link_capacity_estimate
                    && estimate.link_capacity_upper.is_finite()
                    && pacing_rate > estimate.link_capacity_upper =>
            {
                pacing_rate = *[
                    estimate.link_capacity_upper,
                    self.min_total_allocated_bitrate,
                    self.last_loss_based_target_rate,
                ]
                .iter()
                .max()
                .unwrap()
            }
            _ => {}
        };

        let mut padding_rate: DataRate =
            if self.last_loss_base_state == LossBasedState::IncreaseUsingPadding {
                self.max_padding_rate.max(self.last_loss_based_target_rate)
            } else {
                self.max_padding_rate
            };
        padding_rate = std::cmp::min(padding_rate, self.last_pushback_target_rate);
        let mut msg = PacerConfig::default();
        msg.at_time = at_time;
        msg.time_window = TimeDelta::from_seconds(1);
        msg.data_window = pacing_rate * msg.time_window;
        msg.pad_window = padding_rate * msg.time_window;
        msg
    }

    fn set_network_state_estimate(&mut self, estimate: Option<NetworkStateEstimate>) {
        let prev_estimate = self.estimate;
        self.estimate = estimate;

        match (self.estimate, prev_estimate) {
            (Some(estimate), Some(prev_estimate))
                if estimate.update_time == prev_estimate.update_time => {}
            (Some(estimate), _) => self.probe_controller.set_network_state_estimate(estimate),
            _ => {}
        };
    }

    fn update_congestion_window_size(&mut self) {
        let min_feedback_max_rtt: TimeDelta =
            TimeDelta::from_millis(*self.feedback_max_rtts.iter().min().unwrap());

        const MIN_CWND: DataSize = DataSize::from_bytes(2 * 1500);
        let time_window: TimeDelta = min_feedback_max_rtt
            + TimeDelta::from_millis(
                self.rate_control_settings
                    .get_congestion_window_additional_time_ms(),
            );

        let mut data_window: DataSize = self.last_loss_based_target_rate * time_window;
        if let Some(current_data_window) = self.current_data_window {
            data_window = std::cmp::max(MIN_CWND, (data_window + current_data_window) / 2);
        } else {
            data_window = std::cmp::max(MIN_CWND, data_window);
        }
        self.current_data_window = Some(data_window);
    }
}

impl NetworkControllerInterface for GoogCcNetworkController {
    fn on_network_availability(&mut self, msg: NetworkAvailability) -> NetworkControlUpdate {
        NetworkControlUpdate {
            probe_cluster_configs: self.probe_controller.on_network_availability(msg),
            ..Default::default()
        }
    }

    fn on_network_route_change(&mut self, mut msg: NetworkRouteChange) -> NetworkControlUpdate {
        if self.safe_reset_on_route_change {
            let mut estimated_bitrate: Option<DataRate>;
            if self.safe_reset_acknowledged_rate {
                estimated_bitrate = self.acknowledged_bitrate_estimator.bitrate();
                if estimated_bitrate.is_none() {
                    estimated_bitrate = self.acknowledged_bitrate_estimator.peek_rate();
                }
            } else {
                estimated_bitrate = Some(self.bandwidth_estimation.target_rate());
            }
            if let Some(estimated_bitrate) = estimated_bitrate {
                msg.constraints.starting_rate = Some(match msg.constraints.starting_rate {
                    Some(starting_rate) => starting_rate.min(estimated_bitrate),
                    None => estimated_bitrate,
                });
            }
        }

        self.acknowledged_bitrate_estimator =
            AcknowledgedBitrateEstimator::create(&self.field_trials);
        self.probe_bitrate_estimator = ProbeBitrateEstimator::default();
        self.delay_based_bwe = DelayBasedBwe::new(&self.field_trials);
        self.bandwidth_estimation.on_route_change();
        self.probe_controller.reset(msg.at_time);
        let mut update = NetworkControlUpdate {
            probe_cluster_configs: self.reset_constraints(msg.constraints),
            ..Default::default()
        };
        self.maybe_trigger_on_network_changed(&mut update, msg.at_time);
        update
    }

    fn on_process_interval(&mut self, msg: ProcessInterval) -> NetworkControlUpdate {
        let mut update = NetworkControlUpdate::default();
        if let Some(initial_config) = self.initial_config.take() {
            update.probe_cluster_configs = self.reset_constraints(initial_config.constraints);
            update.pacer_config = Some(self.get_pacing_rates(msg.at_time));

            if let Some(requests_alr_probing) =
                initial_config.stream_based_config.requests_alr_probing
            {
                self.probe_controller
                    .enable_periodic_alr_probing(requests_alr_probing);
            }
            if let Some(enable_repeated_initial_probing) = initial_config
                .stream_based_config
                .enable_repeated_initial_probing
            {
                self.probe_controller
                    .enable_repeated_initial_probing(enable_repeated_initial_probing);
            }
            let total_bitrate: Option<DataRate> = initial_config
                .stream_based_config
                .max_total_allocated_bitrate;
            if let Some(total_bitrate) = total_bitrate {
                let mut probes = self
                    .probe_controller
                    .on_max_total_allocated_bitrate(total_bitrate, msg.at_time);
                update.probe_cluster_configs.append(&mut probes);
            }
        }

        if let (Some(congestion_window_pushback_controller), Some(pacer_queue)) = (
            &mut self.congestion_window_pushback_controller,
            msg.pacer_queue,
        ) {
            congestion_window_pushback_controller.update_pacing_queue(pacer_queue.bytes())
        };
        self.bandwidth_estimation.update_estimate(msg.at_time);
        let start_time_ms: Option<i64> = self
            .alr_detector
            .get_application_limited_region_start_time();
        self.probe_controller.set_alr_start_time_ms(start_time_ms);

        let mut probes = self.probe_controller.process(msg.at_time);
        update.probe_cluster_configs.append(&mut probes);

        if self.rate_control_settings.use_congestion_window() && !self.feedback_max_rtts.is_empty()
        {
            self.update_congestion_window_size();
        }

        match (
            &mut self.congestion_window_pushback_controller,
            self.current_data_window,
        ) {
            (Some(congestion_window_pushback_controller), Some(current_data_window)) => {
                congestion_window_pushback_controller
                    .update_outstanding_data(current_data_window.bytes())
            }
            _ => update.congestion_window = self.current_data_window,
        };

        self.maybe_trigger_on_network_changed(&mut update, msg.at_time);
        update
    }

    fn on_remote_bitrate_report(&mut self, msg: RemoteBitrateReport) -> NetworkControlUpdate {
        if self.packet_feedback_only {
            tracing::error!("Received REMB for packet feedback only GoogCC");
            return NetworkControlUpdate::default();
        }
        self.bandwidth_estimation
            .update_receiver_estimate(msg.receive_time, msg.bandwidth);
        NetworkControlUpdate::default()
    }

    fn on_round_trip_time_update(&mut self, msg: RoundTripTimeUpdate) -> NetworkControlUpdate {
        if self.packet_feedback_only || msg.smoothed {
            return NetworkControlUpdate::default();
        }
        assert!(!msg.round_trip_time.is_zero());
        self.delay_based_bwe.on_rtt_update(msg.round_trip_time);
        self.bandwidth_estimation
            .update_rtt(msg.round_trip_time, msg.receive_time);
        NetworkControlUpdate::default()
    }

    fn on_sent_packet(&mut self, sent_packet: SentPacket) -> NetworkControlUpdate {
        self.alr_detector
            .on_bytes_sent(sent_packet.size.bytes() as _, sent_packet.send_time.ms());
        self.acknowledged_bitrate_estimator.set_alr(
            self.alr_detector
                .get_application_limited_region_start_time()
                .is_some(),
        );

        if !self.first_packet_sent {
            self.first_packet_sent = true;
            // Initialize feedback time to send time to allow estimation of RTT until
            // first feedback is received.
            self.bandwidth_estimation
                .update_propagation_rtt(sent_packet.send_time, TimeDelta::zero());
        }
        self.bandwidth_estimation.on_sent_packet(sent_packet);

        if let Some(congestion_window_pushback_controller) =
            &mut self.congestion_window_pushback_controller
        {
            congestion_window_pushback_controller
                .update_outstanding_data(sent_packet.data_in_flight.bytes());
            let mut update = NetworkControlUpdate::default();
            self.maybe_trigger_on_network_changed(&mut update, sent_packet.send_time);
            update
        } else {
            NetworkControlUpdate::default()
        }
    }

    fn on_received_packet(&mut self, _: ReceivedPacket) -> NetworkControlUpdate {
        NetworkControlUpdate::default()
    }

    fn on_streams_config(&mut self, msg: StreamsConfig) -> NetworkControlUpdate {
        let mut update = NetworkControlUpdate::default();
        if let Some(requests_alr_probing) = msg.requests_alr_probing {
            self.probe_controller
                .enable_periodic_alr_probing(requests_alr_probing);
        }
        if let Some(max_total_allocated_bitrate) = msg.max_total_allocated_bitrate {
            update.probe_cluster_configs = self
                .probe_controller
                .on_max_total_allocated_bitrate(max_total_allocated_bitrate, msg.at_time);
        }

        let mut pacing_changed: bool = false;
        match msg.pacing_factor {
            Some(pacing_factor) if pacing_factor != self.pacing_factor => {
                self.pacing_factor = pacing_factor;
                pacing_changed = true;
            }
            _ => {}
        };
        match msg.min_total_allocated_bitrate {
            Some(min_total_allocated_bitrate)
                if min_total_allocated_bitrate != self.min_total_allocated_bitrate =>
            {
                self.min_total_allocated_bitrate = min_total_allocated_bitrate;
                pacing_changed = true;

                if self.use_min_allocatable_as_lower_bound {
                    self.clamp_constraints();
                    self.delay_based_bwe.set_min_bitrate(self.min_data_rate);
                    self.bandwidth_estimation
                        .set_min_max_bitrate(self.min_data_rate, self.max_data_rate);
                }
            }
            _ => {}
        };

        match msg.max_padding_rate {
            Some(max_padding_rate) if max_padding_rate != self.max_padding_rate => {
                self.max_padding_rate = max_padding_rate;
                pacing_changed = true;
            }
            _ => {}
        };

        if pacing_changed {
            update.pacer_config = Some(self.get_pacing_rates(msg.at_time));
        }
        update
    }

    fn on_target_rate_constraints(
        &mut self,
        constraints: TargetRateConstraints,
    ) -> NetworkControlUpdate {
        let mut update = NetworkControlUpdate {
            probe_cluster_configs: self.reset_constraints(constraints),
            ..Default::default()
        };
        self.maybe_trigger_on_network_changed(&mut update, constraints.at_time);
        update
    }

    fn on_transport_loss_report(&mut self, msg: TransportLossReport) -> NetworkControlUpdate {
        if self.packet_feedback_only {
            return NetworkControlUpdate::default();
        }
        let total_packets_delta: i64 =
            msg.packets_received_delta as i64 + msg.packets_lost_delta as i64;
        self.bandwidth_estimation.update_packets_lost(
            msg.packets_lost_delta as _,
            total_packets_delta,
            msg.receive_time,
        );
        NetworkControlUpdate::default()
    }

    fn on_transport_packets_feedback(
        &mut self,
        report: TransportPacketsFeedback,
    ) -> NetworkControlUpdate {
        if report.packet_feedbacks.is_empty() {
            // TODO(bugs.webrtc.org/10125): Design a better mechanism to safe-guard
            // against building very large network queues.
            return NetworkControlUpdate::default();
        }

        if let Some(congestion_window_pushback_controller) =
            &mut self.congestion_window_pushback_controller
        {
            congestion_window_pushback_controller
                .update_outstanding_data(report.data_in_flight.bytes());
        }
        let mut max_feedback_rtt: TimeDelta = TimeDelta::minus_infinity();
        let mut min_propagation_rtt: TimeDelta = TimeDelta::plus_infinity();
        let max_recv_time = report
            .received_with_send_info()
            .map(|x| x.receive_time)
            .max()
            .unwrap();

        for feedback in report.received_with_send_info() {
            let feedback_rtt: TimeDelta = report.feedback_time - feedback.sent_packet.send_time;
            let min_pending_time: TimeDelta = max_recv_time - feedback.receive_time;
            let propagation_rtt: TimeDelta = feedback_rtt - min_pending_time;
            max_feedback_rtt = std::cmp::max(max_feedback_rtt, feedback_rtt);
            min_propagation_rtt = std::cmp::min(min_propagation_rtt, propagation_rtt);
        }

        if max_feedback_rtt.is_finite() {
            self.feedback_max_rtts.push_back(max_feedback_rtt.ms());
            const MAX_FEEDBACK_RTT_WINDOW: usize = 32;
            if self.feedback_max_rtts.len() > MAX_FEEDBACK_RTT_WINDOW {
                self.feedback_max_rtts.pop_front();
            }
            // TODO(srte): Use time since last unacknowledged packet.
            self.bandwidth_estimation
                .update_propagation_rtt(report.feedback_time, min_propagation_rtt);
        }
        if self.packet_feedback_only {
            if !self.feedback_max_rtts.is_empty() {
                let sum_rtt_ms: i64 = self.feedback_max_rtts.iter().sum();
                let mean_rtt_ms: i64 = sum_rtt_ms / self.feedback_max_rtts.len() as i64;
                self.delay_based_bwe
                    .on_rtt_update(TimeDelta::from_millis(mean_rtt_ms));
            }

            let mut feedback_min_rtt: TimeDelta = TimeDelta::plus_infinity();
            for packet_feedback in report.received_with_send_info() {
                let pending_time: TimeDelta = max_recv_time - packet_feedback.receive_time;
                let rtt: TimeDelta =
                    report.feedback_time - packet_feedback.sent_packet.send_time - pending_time;
                // Value used for predicting NACK round trip time in FEC controller.
                feedback_min_rtt = std::cmp::min(rtt, feedback_min_rtt);
            }
            if feedback_min_rtt.is_finite() {
                self.bandwidth_estimation
                    .update_rtt(feedback_min_rtt, report.feedback_time);
            }

            self.expected_packets_since_last_loss_update +=
                report.packets_with_feedback().len() as i64;
            for packet_feedback in report.packets_with_feedback() {
                if !packet_feedback.is_received() {
                    self.lost_packets_since_last_loss_update += 1;
                }
            }
            if report.feedback_time > self.next_loss_update {
                self.next_loss_update = report.feedback_time + Self::LOSS_UPDATE_INTERVAL;
                self.bandwidth_estimation.update_packets_lost(
                    self.lost_packets_since_last_loss_update,
                    self.expected_packets_since_last_loss_update,
                    report.feedback_time,
                );
                self.expected_packets_since_last_loss_update = 0;
                self.lost_packets_since_last_loss_update = 0;
            }
        }
        let alr_start_time: Option<i64> = self
            .alr_detector
            .get_application_limited_region_start_time();

        if self.previously_in_alr && alr_start_time.is_none() {
            let now_ms: i64 = report.feedback_time.ms();
            self.acknowledged_bitrate_estimator
                .set_alr_ended_time(report.feedback_time);
            self.probe_controller.set_alr_ended_time_ms(now_ms);
        }
        self.previously_in_alr = alr_start_time.is_some();
        self.acknowledged_bitrate_estimator
            .incoming_packet_feedback(&report.sorted_by_receive_time());
        let acknowledged_bitrate = self.acknowledged_bitrate_estimator.bitrate();
        self.bandwidth_estimation
            .set_acknowledged_rate(acknowledged_bitrate, report.feedback_time);
        for feedback in report.sorted_by_receive_time() {
            if feedback.sent_packet.pacing_info.probe_cluster_id != PacedPacketInfo::NOT_APROBE {
                self.probe_bitrate_estimator
                    .handle_probe_and_estimate_bitrate(&feedback);
            }
        }

        let mut probe_bitrate: Option<DataRate> = self
            .probe_bitrate_estimator
            .fetch_and_reset_last_estimated_bitrate();
        match (probe_bitrate, self.estimate) {
            (Some(current), Some(estimate))
                if self.ignore_probes_lower_than_network_estimate
                    && current < self.delay_based_bwe.last_estimate()
                    && current < estimate.link_capacity_lower =>
            {
                probe_bitrate = None;
            }
            _ => {}
        };

        match (probe_bitrate, acknowledged_bitrate) {
            (Some(probe), Some(acknowledged))
                if self.limit_probes_lower_than_throughput_estimate =>
            {
                // Limit the backoff to something slightly below the acknowledged
                // bitrate. ("Slightly below" because we want to drain the queues
                // if we are actually overusing.)
                // The acknowledged bitrate shouldn't normally be higher than the delay
                // based estimate, but it could happen e.g. due to packet bursts or
                // encoder overshoot. We use std::cmp::min to ensure that a probe result
                // below the current BWE never causes an increase.
                let limit: DataRate = std::cmp::min(
                    self.delay_based_bwe.last_estimate(),
                    acknowledged * Self::PROBE_DROP_THROUGHPUT_FRACTION,
                );
                probe_bitrate = Some(std::cmp::max(probe, limit));
            }
            _ => {}
        };

        let mut update = NetworkControlUpdate::default();

        let result: DelayBasedBweResult = self.delay_based_bwe.incoming_packet_feedback_vector(
            &report,
            acknowledged_bitrate,
            probe_bitrate, /*self.estimate,*/
            alr_start_time.is_some(),
        );

        if result.updated {
            if result.probe {
                self.bandwidth_estimation
                    .set_send_bitrate(result.target_bitrate, report.feedback_time);
            }
            // Since SetSendBitrate now resets the delay-based estimate, we have to
            // call UpdateDelayBasedEstimate after SetSendBitrate.
            self.bandwidth_estimation
                .update_delay_based_estimate(report.feedback_time, result.target_bitrate);
        }
        self.bandwidth_estimation.update_loss_based_estimator(
            &report,
            result.delay_detector_state,
            probe_bitrate,
            alr_start_time.is_some(),
        );
        if result.updated {
            // Update the estimate in the ProbeController, in case we want to probe.
            self.maybe_trigger_on_network_changed(&mut update, report.feedback_time);
        }

        let recovered_from_overuse = result.recovered_from_overuse;

        if recovered_from_overuse {
            self.probe_controller.set_alr_start_time_ms(alr_start_time);
            let mut probes = self.probe_controller.request_probe(report.feedback_time);
            update.probe_cluster_configs.append(&mut probes);
        }

        // No valid RTT could be because send-side BWE isn't used, in which case
        // we don't try to limit the outstanding packets.
        if self.rate_control_settings.use_congestion_window() && max_feedback_rtt.is_finite() {
            self.update_congestion_window_size();
        }
        if let (Some(congestion_window_pushback_controller), Some(current_data_window)) = (
            &mut self.congestion_window_pushback_controller,
            self.current_data_window,
        ) {
            congestion_window_pushback_controller.set_data_window(current_data_window);
        } else {
            update.congestion_window = self.current_data_window;
        }

        update
    }

    fn on_network_state_estimate(
        &mut self,
        estimate: NetworkStateEstimate,
    ) -> NetworkControlUpdate {
        self.set_network_state_estimate(Some(estimate));
        NetworkControlUpdate::default()
    }

    fn get_process_interval() -> TimeDelta {
        TimeDelta::from_millis(25)
    }
}

fn get_bandwidth_limited_cause(
    loss_based_state: LossBasedState,
    is_rtt_above_limit: bool,
    bandwidth_usage: BandwidthUsage,
) -> BandwidthLimitedCause {
    if bandwidth_usage == BandwidthUsage::Overusing || bandwidth_usage == BandwidthUsage::Underusing
    {
        return BandwidthLimitedCause::DelayBasedLimitedDelayIncreased;
    } else if is_rtt_above_limit {
        return BandwidthLimitedCause::RttBasedBackOffHighRtt;
    }
    match loss_based_state {
        // Probes may not be sent in this state.
        LossBasedState::Decreasing => BandwidthLimitedCause::LossLimitedBwe,
        LossBasedState::IncreaseUsingPadding =>
        // Probes may not be sent in this state.
        {
            BandwidthLimitedCause::LossLimitedBwe
        }
        LossBasedState::Increasing =>
        // Probes may be sent in this state.
        {
            BandwidthLimitedCause::LossLimitedBweIncreasing
        }
        LossBasedState::DelayBasedEstimate => BandwidthLimitedCause::DelayBasedLimited,
    }
}

#[cfg(test)]
mod test {
    use approx::assert_relative_eq;
    use test_trace::test;

    use crate::api::transport::PacketResult;

    use super::*;

    // Count dips from a constant high bandwidth level within a short window.
    /*
    fn count_bandwidth_dips(mut bandwidth_history: VecDeque<DataRate>, threshold: DataRate) -> i64 {
        if bandwidth_history.is_empty() {
            return 1;
        }
        let first = bandwidth_history.pop_front().unwrap();

        let mut dips: i64 = 0;
        let mut state_high: bool = true;
        while let Some(front) = bandwidth_history.pop_front() {
            if front + threshold < first && state_high {
                dips += 1;
                state_high = false;
            } else if front == first {
                state_high = true;
            } else if front > first {
                // If this is toggling we will catch it later when front becomes first.
                state_high = false;
            }
        }
        dips
    }
    */

    const INITIAL_BITRATE_KBPS: i64 = 60;
    const INITIAL_BITRATE: DataRate = DataRate::from_kilobits_per_sec(INITIAL_BITRATE_KBPS);
    const DEFAULT_PACING_RATE: f64 = 2.5;

    /*
    fn CreateVideoSendingClient(
        s: &Scenerio,
        config: CallClientConfig,
        send_link: Vec<EmulatedNetworkNode>,
        return_link: Vec<EmulatedNetworkNode>) -> CallClient {
    let client = s.CreateClient("send", config);
    let route = s.CreateRoutes(client, send_link,
                                    s.CreateClient("return", CallClientConfig()),
                                    return_link);
      s.CreateVideoStream(route.forward(), VideoStreamConfig());
      return client;
    }
    */

    fn create_route_change(
        time: Timestamp,
        start_rate: Option<DataRate>,
        min_rate: Option<DataRate>,
        max_rate: Option<DataRate>,
    ) -> NetworkRouteChange {
        NetworkRouteChange {
            at_time: time,
            constraints: TargetRateConstraints {
                at_time: time,
                starting_rate: start_rate,
                min_data_rate: min_rate,
                max_data_rate: max_rate,
            },
        }
    }

    fn create_packet_result(
        arrival_time: Timestamp,
        send_time: Timestamp,
        payload_size: usize,
        pacing_info: PacedPacketInfo,
    ) -> PacketResult {
        PacketResult {
            sent_packet: SentPacket {
                send_time,
                size: DataSize::from_bytes(payload_size as _),
                pacing_info,
                ..Default::default()
            },
            receive_time: arrival_time,
            ..Default::default()
        }
    }

    // Simulate sending packets and receiving transport feedback during
    // `runtime_ms`, then return the final target birate.
    fn packet_transmission_and_feedback_block<T: NetworkControllerInterface>(
        controller: &mut T,
        runtime_ms: i64,
        delay: i64,
        current_time: &mut Timestamp,
    ) -> Option<DataRate> {
        let mut update: NetworkControlUpdate;
        let mut target_bitrate: Option<DataRate> = None;
        let mut delay_buildup: i64 = 0;
        let start_time_ms: i64 = current_time.ms();
        while current_time.ms() - start_time_ms < runtime_ms {
            const PAYLOAD_SIZE: usize = 1000;
            let packet: PacketResult = create_packet_result(
                *current_time + TimeDelta::from_millis(delay_buildup),
                *current_time,
                PAYLOAD_SIZE,
                PacedPacketInfo::default(),
            );
            delay_buildup += delay;
            update = controller.on_sent_packet(packet.sent_packet);
            if let Some(target_rate) = update.target_rate {
                target_bitrate = Some(target_rate.target_rate);
            }
            let feedback = TransportPacketsFeedback {
                feedback_time: packet.receive_time,
                packet_feedbacks: vec![packet],
                ..Default::default()
            };
            update = controller.on_transport_packets_feedback(feedback);
            if let Some(target_rate) = update.target_rate {
                target_bitrate = Some(target_rate.target_rate);
            }
            *current_time += TimeDelta::from_millis(50);
            update = controller.on_process_interval(ProcessInterval {
                at_time: *current_time,
                ..Default::default()
            });
            if let Some(target_rate) = update.target_rate {
                target_bitrate = Some(target_rate.target_rate);
            }
        }
        target_bitrate
    }

    // Create transport packets feedback with a built-up delay.
    fn create_transport_packets_feedback(
        per_packet_network_delay: TimeDelta,
        one_way_delay: TimeDelta,
        send_time: Timestamp,
    ) -> TransportPacketsFeedback {
        let mut delay_buildup: TimeDelta = one_way_delay;
        const FEEDBACK_SIZE: i64 = 3;
        const PAYLOAD_SIZE: usize = 1000;
        let mut feedback = TransportPacketsFeedback::default();
        for _ in 0..FEEDBACK_SIZE {
            let packet: PacketResult = create_packet_result(
                /*arrival_time=*/ send_time + delay_buildup,
                send_time,
                PAYLOAD_SIZE,
                PacedPacketInfo::default(),
            );
            delay_buildup += per_packet_network_delay;
            feedback.feedback_time = packet.receive_time + one_way_delay;
            feedback.packet_feedbacks.push(packet);
        }
        feedback
    }

    // Scenarios:

    /*
    fn UpdatesTargetRateBasedOnLinkCapacity(test_name: &str) {
    let factory = CreateFeedbackOnlyFactory();
      let s = Scenario::new("googcc_unit/target_capacity" + std::string(test_name), false);
      let mut config: CallClientConfig::default();
      config.transport.cc_factory = &factory;
      config.transport.rates.min_rate = DataRate::KilobitsPerSec(10);
      config.transport.rates.max_rate = DataRate::KilobitsPerSec(1500);
      config.transport.rates.start_rate = DataRate::KilobitsPerSec(300);
    let send_net = s.CreateMutableSimulationNode(|c: &mut NetworkSimulationConfig| {
        c.bandwidth = DataRate::KilobitsPerSec(500);
        c.delay = TimeDelta::Millis(100);
        c.loss_rate = 0.0;
      });
    let ret_net = s.CreateMutableSimulationNode(
          |c: &mut NetworkSimulationConfig| { c.delay = TimeDelta::Millis(100); });
    let truth: &StatesPrinter = s.CreatePrinter(
          "send.truth.txt", TimeDelta::plus_infinity(), {send_net.ConfigPrinter()});

    let client = CreateVideoSendingClient(&s, config, {send_net.node()},
                                              {ret_net.node()});

      truth.PrintRow();
      s.RunFor(TimeDelta::Seconds(25));
      truth.PrintRow();
      assert_relative_eq!(client.target_rate().kbps(), 450, 100);

      send_net.UpdateConfig(|c: &mut NetworkSimulationConfig| {
        c.bandwidth = DataRate::KilobitsPerSec(800);
        c.delay = TimeDelta::Millis(100);
      });

      truth.PrintRow();
      s.RunFor(TimeDelta::Seconds(20));
      truth.PrintRow();
      assert_relative_eq!(client.target_rate().kbps(), 750, 150);

      send_net.UpdateConfig(|c: &mut NetworkSimulationConfig| {
        c.bandwidth = DataRate::KilobitsPerSec(100);
        c.delay = TimeDelta::Millis(200);
      });
      ret_net.UpdateConfig(
          |c: &mut NetworkSimulationConfig| { c.delay = TimeDelta::Millis(200); });

      truth.PrintRow();
      s.RunFor(TimeDelta::Seconds(50));
      truth.PrintRow();
      assert_relative_eq!(client.target_rate().kbps(), 90, 25);
    }

    fn RunRembDipScenario(test_name: &str) -> DataRate {
      let s = Scenario::new(test_name);
      let net_conf = NetworkSimulationConfig::new();
      net_conf.bandwidth = DataRate::KilobitsPerSec(2000);
      net_conf.delay = TimeDelta::Millis(50);
    let client = s.CreateClient("send", |c: &mut CallClientConfig| {
        c.transport.rates.start_rate = DataRate::KilobitsPerSec(1000);
      });
    let send_net = {s.CreateSimulationNode(net_conf)};
    let ret_net = {s.CreateSimulationNode(net_conf)};
    let route = s.CreateRoutes(
          client, send_net, s.CreateClient("return", CallClientConfig()), ret_net);
      s.CreateVideoStream(route.forward(), VideoStreamConfig());

      s.RunFor(TimeDelta::Seconds(10));
      assert!(client.send_bandwidth().kbps() > 1500);

      let RembLimit: DataRate = DataRate::KilobitsPerSec(250);
      client.SetRemoteBitrate(RembLimit);
      s.RunFor(TimeDelta::Seconds(1));
      assert_eq!(client.send_bandwidth(), RembLimit);

      let RembLimitLifted: DataRate = DataRate::KilobitsPerSec(10000);
      client.SetRemoteBitrate(RembLimitLifted);
      s.RunFor(TimeDelta::Seconds(10));

      return client.send_bandwidth();
    }
    */

    fn create_controller(
        field_trials: FieldTrials,
        feedback_only: bool,
    ) -> GoogCcNetworkController {
        let config = NetworkControllerConfig {
            field_trials,
            constraints: TargetRateConstraints {
                at_time: Timestamp::zero(),
                min_data_rate: Some(DataRate::from_kilobits_per_sec(0)),
                max_data_rate: Some(DataRate::from_kilobits_per_sec(5 * INITIAL_BITRATE_KBPS)),
                starting_rate: Some(DataRate::from_kilobits_per_sec(INITIAL_BITRATE_KBPS)),
            },
            ..Default::default()
        };

        let goog_cc_config = GoogCcConfig { feedback_only };

        GoogCcNetworkController::new(config, goog_cc_config)
    }

    #[test]
    fn initialize_target_rate_on_first_process_interval_after_network_available() {
        let mut controller = create_controller(FieldTrials::default(), false);

        let _ = controller.on_network_availability(NetworkAvailability {
            at_time: Timestamp::from_millis(123456),
            network_available: true,
        });
        let update = controller.on_process_interval(ProcessInterval {
            at_time: Timestamp::from_millis(123456),
            ..Default::default()
        });

        assert_eq!(update.target_rate.unwrap().target_rate, INITIAL_BITRATE);
        assert_eq!(
            update.pacer_config.unwrap().data_rate(),
            INITIAL_BITRATE * DEFAULT_PACING_RATE
        );
        assert_eq!(
            update.probe_cluster_configs[0].target_data_rate,
            INITIAL_BITRATE * 3
        );
        assert_eq!(
            update.probe_cluster_configs[1].target_data_rate,
            INITIAL_BITRATE * 5
        );
    }

    #[test]
    fn reacts_to_changed_network_conditions() {
        let mut controller = create_controller(FieldTrials::default(), false);
        let mut current_time: Timestamp = Timestamp::from_millis(123);
        controller.on_network_availability(NetworkAvailability {
            at_time: current_time,
            network_available: true,
        });
        controller.on_process_interval(ProcessInterval {
            at_time: current_time,
            ..Default::default()
        });
        controller.on_remote_bitrate_report(RemoteBitrateReport {
            receive_time: current_time,
            bandwidth: INITIAL_BITRATE * 2,
        });

        current_time += TimeDelta::from_millis(25);
        let update = controller.on_process_interval(ProcessInterval {
            at_time: current_time,
            ..Default::default()
        });
        assert_eq!(update.target_rate.unwrap().target_rate, INITIAL_BITRATE * 2);
        assert_eq!(
            update.pacer_config.unwrap().data_rate(),
            INITIAL_BITRATE * 2 * DEFAULT_PACING_RATE
        );

        controller.on_remote_bitrate_report(RemoteBitrateReport {
            receive_time: current_time,
            bandwidth: INITIAL_BITRATE,
        });
        current_time += TimeDelta::from_millis(25);
        let update = controller.on_process_interval(ProcessInterval {
            at_time: current_time,
            ..Default::default()
        });
        assert_eq!(update.target_rate.unwrap().target_rate, INITIAL_BITRATE);
        assert_eq!(
            update.pacer_config.unwrap().data_rate(),
            INITIAL_BITRATE * DEFAULT_PACING_RATE
        );
    }

    #[test]
    fn on_network_route_changed() {
        let mut controller = create_controller(FieldTrials::default(), false);
        let current_time: Timestamp = Timestamp::from_millis(123);
        controller.on_network_availability(NetworkAvailability {
            at_time: current_time,
            network_available: true,
        });
        let new_bitrate: DataRate = DataRate::from_bits_per_sec(200000);

        let update = controller.on_network_route_change(create_route_change(
            current_time,
            Some(new_bitrate),
            None,
            None,
        ));
        assert_eq!(update.target_rate.unwrap().target_rate, new_bitrate);
        assert_eq!(
            update.pacer_config.unwrap().data_rate(),
            new_bitrate * DEFAULT_PACING_RATE
        );
        assert_eq!(update.probe_cluster_configs.len(), 2);

        // If the bitrate is reset to -1, the new starting bitrate will be
        // the minimum default bitrate.
        const DEFAULT_MIN_BITRATE: DataRate = DataRate::from_kilobits_per_sec(5);
        let update =
            controller.on_network_route_change(create_route_change(current_time, None, None, None));
        assert_eq!(update.target_rate.unwrap().target_rate, DEFAULT_MIN_BITRATE);
        assert_relative_eq!(
            update.pacer_config.unwrap().data_rate().bps_float(),
            DEFAULT_MIN_BITRATE.bps_float() * DEFAULT_PACING_RATE,
            epsilon = 10.0
        );
        assert_eq!(update.probe_cluster_configs.len(), 2);
    }

    #[test]
    fn probe_on_route_change() {
        let mut controller = create_controller(FieldTrials::default(), false);
        let mut current_time: Timestamp = Timestamp::from_millis(123);
        controller.on_network_availability(NetworkAvailability {
            at_time: current_time,
            network_available: true,
        });
        current_time += TimeDelta::from_seconds(3);

        let update = controller.on_network_route_change(create_route_change(
            current_time,
            Some(2 * INITIAL_BITRATE),
            Some(DataRate::zero()),
            Some(20 * INITIAL_BITRATE),
        ));

        assert!(update.pacer_config.is_some());
        assert_eq!(update.target_rate.unwrap().target_rate, INITIAL_BITRATE * 2);
        assert_eq!(update.probe_cluster_configs.len(), 2);
        assert_eq!(
            update.probe_cluster_configs[0].target_data_rate,
            INITIAL_BITRATE * 6
        );
        assert_eq!(
            update.probe_cluster_configs[1].target_data_rate,
            INITIAL_BITRATE * 12
        );

        controller.on_process_interval(ProcessInterval {
            at_time: current_time,
            ..Default::default()
        });
    }

    #[test]
    fn probe_after_route_change_when_transport_writable() {
        let mut controller = create_controller(FieldTrials::default(), false);
        let current_time: Timestamp = Timestamp::from_millis(123);

        let mut update: NetworkControlUpdate =
            controller.on_network_availability(NetworkAvailability {
                at_time: current_time,
                network_available: false,
            });
        assert!(update.probe_cluster_configs.is_empty());

        update = controller.on_network_route_change(create_route_change(
            current_time,
            Some(2 * INITIAL_BITRATE),
            Some(DataRate::zero()),
            Some(20 * INITIAL_BITRATE),
        ));
        // Transport is not writable. So not point in sending a probe.
        assert!(update.probe_cluster_configs.is_empty());

        // Probe is sent when transport becomes writable.
        update = controller.on_network_availability(NetworkAvailability {
            at_time: current_time,
            network_available: true,
        });
        assert!(!update.probe_cluster_configs.is_empty());
    }

    // Bandwidth estimation is updated when feedbacks are received.
    // Feedbacks which show an increasing delay cause the estimation to be reduced.
    #[test]
    fn updates_delay_based_estimate() {
        let mut controller = create_controller(FieldTrials::default(), false);
        const RUN_TIME_MS: i64 = 6000;
        let mut current_time: Timestamp = Timestamp::from_millis(123);
        controller.on_network_availability(NetworkAvailability {
            at_time: current_time,
            network_available: true,
        });

        // The test must run and insert packets/feedback long enough that the
        // BWE computes a valid estimate. This is first done in an environment which
        // simulates no bandwidth limitation, and therefore not built-up delay.
        let target_bitrate_before_delay: Option<DataRate> = packet_transmission_and_feedback_block(
            &mut controller,
            RUN_TIME_MS,
            0,
            &mut current_time,
        );
        assert!(target_bitrate_before_delay.is_some());

        // Repeat, but this time with a building delay, and make sure that the
        // estimation is adjusted downwards.
        let target_bitrate_after_delay: Option<DataRate> = packet_transmission_and_feedback_block(
            &mut controller,
            RUN_TIME_MS,
            50,
            &mut current_time,
        );
        assert!(target_bitrate_after_delay.unwrap() < target_bitrate_before_delay.unwrap());
    }

    #[test]
    fn pace_at_max_of_lower_link_capacity_and_bwe() {
        let field_trials = FieldTrials {
            pace_at_max_of_bwe_and_lower_link_capacity: true,
            ..Default::default()
        };
        let mut controller = create_controller(field_trials, false);
        let mut current_time: Timestamp = Timestamp::from_millis(123);
        controller.on_network_availability(NetworkAvailability {
            at_time: current_time,
            network_available: true,
        });
        controller.on_process_interval(ProcessInterval {
            at_time: current_time,
            ..Default::default()
        });
        current_time += TimeDelta::from_millis(100);
        let mut network_estimate: NetworkStateEstimate = NetworkStateEstimate {
            link_capacity_lower: 10 * INITIAL_BITRATE,
            ..Default::default()
        };
        controller.set_network_state_estimate(Some(network_estimate));
        // OnNetworkStateEstimate does not trigger processing a new estimate. So add a
        // dummy loss report to trigger a BWE update in the next process interval.
        let loss_report = TransportLossReport {
            start_time: current_time,
            end_time: current_time,
            receive_time: current_time,
            packets_received_delta: 50,
            packets_lost_delta: 1,
        };
        controller.on_transport_loss_report(loss_report);
        let update = controller.on_process_interval(ProcessInterval {
            at_time: current_time,
            ..Default::default()
        });
        assert!(update.pacer_config.is_some());
        assert!(update.target_rate.is_some());
        assert!(update.target_rate.unwrap().target_rate < network_estimate.link_capacity_lower);
        assert_eq!(
            update.pacer_config.unwrap().data_rate().kbps_float(),
            network_estimate.link_capacity_lower.kbps_float() * DEFAULT_PACING_RATE
        );

        current_time += TimeDelta::from_millis(100);
        // Set a low link capacity estimate and verify that pacing rate is set
        // relative to loss based/delay based estimate.
        network_estimate = NetworkStateEstimate {
            link_capacity_lower: 0.5 * INITIAL_BITRATE,
            ..Default::default()
        };
        controller.set_network_state_estimate(Some(network_estimate));
        // Again, we need to inject a dummy loss report to trigger an update of the
        // BWE in the next process interval.
        let loss_report = TransportLossReport {
            start_time: current_time,
            end_time: current_time,
            receive_time: current_time,
            packets_received_delta: 50,
            packets_lost_delta: 0,
        };
        controller.on_transport_loss_report(loss_report);
        let update = controller.on_process_interval(ProcessInterval {
            at_time: current_time,
            ..Default::default()
        });
        assert!(update.target_rate.is_some());
        assert!(
            update.target_rate.as_ref().unwrap().target_rate > network_estimate.link_capacity_lower
        );
        // NOTE: These are off by 4 bps
        assert_eq!(
            update.pacer_config.unwrap().data_rate().kbps(),
            (update.target_rate.unwrap().target_rate * DEFAULT_PACING_RATE).kbps(),
        );
    }

    #[test]
    fn limit_pacing_factor_to_upper_link_capacity() {
        let field_trials = FieldTrials {
            limit_pacing_factor_by_upper_link_capacity_estimate: true,
            ..Default::default()
        };
        let mut controller = create_controller(field_trials, false);
        let mut current_time: Timestamp = Timestamp::from_millis(123);
        controller.on_network_availability(NetworkAvailability {
            at_time: current_time,
            network_available: true,
        });
        controller.on_process_interval(ProcessInterval {
            at_time: current_time,
            ..Default::default()
        });
        current_time += TimeDelta::from_millis(100);
        let network_estimate: NetworkStateEstimate = NetworkStateEstimate {
            link_capacity_upper: (INITIAL_BITRATE * DEFAULT_PACING_RATE) / 2,
            ..Default::default()
        };
        controller.set_network_state_estimate(Some(network_estimate));
        // OnNetworkStateEstimate does not trigger processing a new estimate. So add a
        // dummy loss report to trigger a BWE update in the next process interval.
        let loss_report = TransportLossReport {
            start_time: current_time,
            end_time: current_time,
            receive_time: current_time,
            packets_received_delta: 50,
            packets_lost_delta: 1,
        };
        controller.on_transport_loss_report(loss_report);
        let update = controller.on_process_interval(ProcessInterval {
            at_time: current_time,
            ..Default::default()
        });
        assert!(update.pacer_config.is_some());
        assert!(update.target_rate.is_some());
        assert!(update.target_rate.unwrap().target_rate >= INITIAL_BITRATE);
        assert_eq!(
            update.pacer_config.unwrap().data_rate(),
            network_estimate.link_capacity_upper
        );
    }

    // Test congestion window pushback on network delay happens.
    /*
    #[test]
    fn CongestionWindowPushbackOnNetworkDelay() {
    let factory = CreateFeedbackOnlyFactory();
          let field_trials = FieldTrials {
           congestion_window: experiments::CongestionWindowConfig { queue_size_ms: 800, min_bitrate_bps: 30000, ..Default::default() }
           ..Default::default()
          };
      let s = Scenario::new("googcc_unit/cwnd_on_delay", false);
    let send_net =
          s.CreateMutableSimulationNode(|c: &mut NetworkSimulcationConfig| {
            c.bandwidth = DataRate::KilobitsPerSec(1000);
            c.delay = TimeDelta::Millis(100);
          });
    let ret_net = s.CreateSimulationNode(
          |c: &mut NetworkSimulationConfig| { c.delay = TimeDelta::Millis(100); });
      let mut config = CallClientConfig::default();
      config.transport.cc_factory = &factory;
      // Start high so bandwidth drop has max effect.
      config.transport.rates.start_rate = DataRate::KilobitsPerSec(300);
      config.transport.rates.max_rate = DataRate::KilobitsPerSec(2000);
      config.transport.rates.min_rate = DataRate::KilobitsPerSec(10);

    let client = CreateVideoSendingClient(&s, config,
                                              {send_net.node()}, {ret_net});

      s.RunFor(TimeDelta::Seconds(10));
      send_net.PauseTransmissionUntil(s.Now() + TimeDelta::Seconds(10));
      s.RunFor(TimeDelta::Seconds(3));

      // After 3 seconds without feedback from any sent packets, we expect that the
      // target rate is reduced to the minimum pushback threshold
      // DefaultMinPushbackTargetBitrateBps, which is defined as 30 kbps in
      // congestion_window_pushback_controller.
      EXPECT_LT(client.target_rate().kbps(), 40);
    }
    */

    /*
    // Test congestion window pushback on network delay happens.
    #[test]
    fn CongestionWindowPushbackDropFrameOnNetworkDelay() {
    let factory = CreateFeedbackOnlyFactory();
          let field_trials = FieldTrials {
           congestion_window: experiments::CongestionWindowConfig { queue_size_ms: 800, min_bitrate_bps: 30000, drop_frame: true, ..Default::default() }
           ..Default::default()
          };
      let s = Scenario::new("googcc_unit/cwnd_on_delay", false);
    let send_net =
          s.CreateMutableSimulationNode(|c: &mut NetworkSimulcationConfig| {
            c.bandwidth = DataRate::KilobitsPerSec(1000);
            c.delay = TimeDelta::Millis(100);
          });
    let ret_net = s.CreateSimulationNode(
          |c: &mut NetworkSimulationConfig| { c.delay = TimeDelta::Millis(100); });
      let mut config = CallClientConfig::default();
      config.transport.cc_factory = &factory;
      // Start high so bandwidth drop has max effect.
      config.transport.rates.start_rate = DataRate::KilobitsPerSec(300);
      config.transport.rates.max_rate = DataRate::KilobitsPerSec(2000);
      config.transport.rates.min_rate = DataRate::KilobitsPerSec(10);

    let client = CreateVideoSendingClient(&s, config,
                                              {send_net.node()}, {ret_net});

      s.RunFor(TimeDelta::Seconds(10));
      send_net.PauseTransmissionUntil(s.Now() + TimeDelta::Seconds(10));
      s.RunFor(TimeDelta::Seconds(3));

      // As the dropframe is set, after 3 seconds without feedback from any sent
      // packets, we expect that the target rate is not reduced by congestion
      // window.
      assert!(client.target_rate().kbps() > 300);
    }
    */

    /*
    #[test]
    fn PaddingRateLimitedByCongestionWindowInTrial() {
         let field_trials = FieldTrials {
           congestion_window: experiments::CongestionWindowConfig { queue_size_ms: 200, min_bitrate_bps: 30000, ..Default::default() }
           ..Default::default()
          };

      let s = Scenario::new("googcc_unit/padding_limited", false);
    let send_net =
          s.CreateMutableSimulationNode(|c: &mut NetworkSimulcationConfig| {
            c.bandwidth = DataRate::KilobitsPerSec(1000);
            c.delay = TimeDelta::Millis(100);
          });
    let ret_net = s.CreateSimulationNode(
          |c: &mut NetworkSimulationConfig| { c.delay = TimeDelta::Millis(100); });
      let mut config = CallClientConfig::default();
      // Start high so bandwidth drop has max effect.
      config.transport.rates.start_rate = DataRate::KilobitsPerSec(1000);
      config.transport.rates.max_rate = DataRate::KilobitsPerSec(2000);
    let client = s.CreateClient("send", config);
    let route =
          s.CreateRoutes(client, {send_net.node()},
                         s.CreateClient("return", CallClientConfig()), {ret_net});
      let mut video = VideoStreamConfig::default();
      video.stream.pad_to_rate = config.transport.rates.max_rate;
      s.CreateVideoStream(route.forward(), video);

      // Run for a few seconds to allow the controller to stabilize.
      s.RunFor(TimeDelta::Seconds(10));

      // Check that padding rate matches target rate.
      assert_relative_eq!(client.padding_rate().kbps(), client.target_rate().kbps(), 1);

      // Check this is also the case when congestion window pushback kicks in.
      send_net.PauseTransmissionUntil(s.Now() + TimeDelta::Seconds(1));
      assert_relative_eq!(client.padding_rate().kbps(), client.target_rate().kbps(), 1);
    }
    */

    /*
    #[test]
    fn LimitsToFloorIfRttIsHighInTrial() {
      // The field trial limits maximum RTT to 2 seconds, higher RTT means that the
      // controller backs off until it reaches the minimum configured bitrate. This
      // allows the RTT to recover faster than the regular control mechanism would
      // achieve.
      const BandwidthFloor: DataRate = DataRate::KilobitsPerSec(50);
             let field_trials = FieldTrials {
               max_rtt_limit: experiments::MaxRttLimitConfig { max_rtt: TimeDelta::Seconds(2), floor: BandwidthFloor, ..Default::default() },
               ..Default::default()
             };
      // In the test case, we limit the capacity and add a cross traffic packet
      // burst that blocks media from being sent. This causes the RTT to quickly
      // increase above the threshold in the trial.
      const LinkCapacity: DataRate = DataRate::KilobitsPerSec(100);
      const BufferBloatDuration: TimeDelta = TimeDelta::Seconds(10);
      let s = Scenario::new("googcc_unit/limit_trial", false);
    let send_net = s.CreateSimulationNode(|c: &mut NetworkSimulcationConfig| {
        c.bandwidth = LinkCapacity;
        c.delay = TimeDelta::Millis(100);
      });
    let ret_net = s.CreateSimulationNode(
          |c: &mut NetworkSimulationConfig| { c.delay = TimeDelta::Millis(100); });
      let mut config = CallClientConfig::default();
      config.transport.rates.start_rate = LinkCapacity;

    let client = CreateVideoSendingClient(&s, config, {send_net}, {ret_net});
      // Run for a few seconds to allow the controller to stabilize.
      s.RunFor(TimeDelta::Seconds(10));
      const BloatPacketSize: DataSize = DataSize::Bytes(1000);
      const BloatPacketCount: i64 =
          (BufferBloatDuration * LinkCapacity / BloatPacketSize) as i64;
      // This will cause the RTT to be large for a while.
      s.TriggerPacketBurst({send_net}, BloatPacketCount, BloatPacketSize.bytes());
      // Wait to allow the high RTT to be detected and acted upon.
      s.RunFor(TimeDelta::Seconds(6));
      // By now the target rate should have dropped to the minimum configured rate.
      assert_relative_eq!(client.target_rate().kbps(), BandwidthFloor.kbps(), 5);
    }
    */

    /*
    #[test]
    fn UpdatesTargetRateBasedOnLinkCapacity() {
      UpdatesTargetRateBasedOnLinkCapacity();
    }
    */

    /*
    #[test]
    fn StableEstimateDoesNotVaryInSteadyState() {
    let factory = CreateFeedbackOnlyFactory();
      let s = Scenario::new("googcc_unit/stable_target", false);
      let mut config = CallClientConfig::default();
      config.transport.cc_factory = &factory;
      let mut net_conf = NetworkSimulationConfig::default();
      net_conf.bandwidth = DataRate::KilobitsPerSec(500);
      net_conf.delay = TimeDelta::Millis(100);
    let send_net = s.CreateSimulationNode(net_conf);
    let ret_net = s.CreateSimulationNode(net_conf);

    let client = CreateVideoSendingClient(&s, config, {send_net}, {ret_net});
      // Run for a while to allow the estimate to stabilize.
      s.RunFor(TimeDelta::Seconds(30));
      let min_stable_target: DataRate = DataRate::plus_infinity();
      let max_stable_target: DataRate = DataRate::minus_infinity();
      let min_target: DataRate = DataRate::plus_infinity();
      let max_target: DataRate = DataRate::minus_infinity();

      // Measure variation in steady state.
       for i in 0..20 {
    let stable_target_rate = client.stable_target_rate();
    let target_rate = client.target_rate();
        assert!(stable_target_rate <= target_rate);

        min_stable_target = std::cmp::min(min_stable_target, stable_target_rate);
        max_stable_target = std::cmp::max(max_stable_target, stable_target_rate);
        min_target = std::cmp::min(min_target, target_rate);
        max_target = std::cmp::max(max_target, target_rate);
        s.RunFor(TimeDelta::Seconds(1));
      }
      // We should expect drops by at least 15% (default backoff.)
      assert!(min_target / max_target < 0.85);
      // We should expect the stable target to be more stable than the immediate one
      assert!(min_stable_target / max_stable_target >= min_target / max_target);
    }
    */

    /*
    #[test]
    fn LossBasedControlUpdatesTargetRateBasedOnLinkCapacity() {
      ScopedFieldTrials trial("WebRTC-Bwe-LossBasedControl/Enabled/");
      // TODO(srte): Should the behavior be unaffected at low loss rates?
      UpdatesTargetRateBasedOnLinkCapacity("_loss_based");
    }
    */

    /*
    #[test]
    fn LossBasedControlDoesModestBackoffToHighLoss() {
      ScopedFieldTrials trial("WebRTC-Bwe-LossBasedControl/Enabled/");
      let s = Scenario::new("googcc_unit/high_loss_channel", false);
      let mut config = CallClientConfig::default();
      config.transport.rates.min_rate = DataRate::KilobitsPerSec(10);
      config.transport.rates.max_rate = DataRate::KilobitsPerSec(1500);
      config.transport.rates.start_rate = DataRate::KilobitsPerSec(300);
    let send_net = s.CreateSimulationNode(|c: &mut NetworkSimulationConfig| {
        c.bandwidth = DataRate::KilobitsPerSec(2000);
        c.delay = TimeDelta::Millis(200);
        c.loss_rate = 0.1;
      });
    let ret_net = s.CreateSimulationNode(
          |c: &mut NetworkSimulationConfig| { c.delay = TimeDelta::Millis(200); });

    let client = CreateVideoSendingClient(&s, config, {send_net}, {ret_net});

      s.RunFor(TimeDelta::Seconds(120));
      // Without LossBasedControl trial, bandwidth drops to ~10 kbps.
      EXPECT_GT(client.target_rate().kbps(), 100);
    }
    */

    /*
    fn AverageBitrateAfterCrossInducedLoss(absl::string_view name) -> DataRate {
      Scenario s(name, false);
      let mut net_conf = NetworkSimulationConfig::default();
      net_conf.bandwidth = DataRate::KilobitsPerSec(1000);
      net_conf.delay = TimeDelta::Millis(100);
      // Short queue length means that we'll induce loss when sudden TCP traffic
      // spikes are induced. This corresponds to ca 200 ms for a packet size of 1000
      // bytes. Such limited buffers are common on for instance wifi routers.
      net_conf.packet_queue_length_limit = 25;

    let send_net = {s.CreateSimulationNode(net_conf)};
    let ret_net = {s.CreateSimulationNode(net_conf)};

    let client = s.CreateClient("send", CallClientConfig());
    let callee = s.CreateClient("return", CallClientConfig());
    let route = s.CreateRoutes(client, send_net, callee, ret_net);
      // TODO(srte): Make this work with RTX enabled or remove it.
    let video = s.CreateVideoStream(route.forward(), [](VideoStreamConfig* c) {
        c.stream.use_rtx = false;
      });
      s.RunFor(TimeDelta::Seconds(10));
      for i in 0..4 {
        // Sends TCP cross traffic inducing loss.
    let tcp_traffic = s.net().StartCrossTraffic(CreateFakeTcpCrossTraffic(
            s.net().CreateRoute(send_net), s.net().CreateRoute(ret_net),
            FakeTcpConfig()));
        s.RunFor(TimeDelta::Seconds(2));
        // Allow the ccongestion controller to recover.
        s.net().StopCrossTraffic(tcp_traffic);
        s.RunFor(TimeDelta::Seconds(20));
      }

      // Querying the video stats from within the expected runtime environment
      // (i.e. the TQ that belongs to the CallClient, not the Scenario TQ that
      // we're currently on).
      VideoReceiveStreamInterface::Stats video_receive_stats;
    let video_stream = video.receive();
      callee.SendTask([&video_stream, &video_receive_stats]() {
        video_receive_stats = video_stream.GetStats();
      });
      return DataSize::Bytes(
                 video_receive_stats.rtp_stats.packet_counter.TotalBytes()) /
             s.TimeSinceStart();
    }
    */

    /*
    #[test]
    fn MaintainsLowRateInSafeResetTrial() {
      const LinkCapacity: DataRate = DataRate::KilobitsPerSec(200);
      const StartRate: DataRate = DataRate::KilobitsPerSec(300);

      ScopedFieldTrials trial("WebRTC-Bwe-SafeResetOnRouteChange/Enabled/");
      Scenario s("googcc_unit/safe_reset_low");
    let send_net = s.CreateSimulationNode([&](NetworkSimulationConfig* c) {
        c.bandwidth = LinkCapacity;
        c.delay = TimeDelta::Millis(10);
      });
    let client = s.CreateClient("send", [&](CallClientConfig* c) {
        c.transport.rates.start_rate = StartRate;
      });
    let route = s.CreateRoutes(
          client, {send_net}, s.CreateClient("return", CallClientConfig()),
          {s.CreateSimulationNode(NetworkSimulationConfig())});
      s.CreateVideoStream(route.forward(), VideoStreamConfig());
      // Allow the controller to stabilize.
      s.RunFor(TimeDelta::Millis(500));
      assert_relative_eq!(client.send_bandwidth().kbps(), LinkCapacity.kbps(), 50);
      s.ChangeRoute(route.forward(), {send_net});
      // Allow new settings to propagate.
      s.RunFor(TimeDelta::Millis(100));
      // Under the trial, the target should be unchanged for low rates.
      assert_relative_eq!(client.send_bandwidth().kbps(), LinkCapacity.kbps(), 50);
    }
    */

    /*
    #[test]
    fn DoNotResetBweUnlessNetworkAdapterChangeOnRoutChange() {
      ScopedFieldTrials trial("WebRTC-Bwe-ResetOnAdapterIdChange/Enabled/");
      Scenario s("googcc_unit/do_not_reset_bwe_unless_adapter_change");

      const LinkCapacity: DataRate = DataRate::KilobitsPerSec(1000);
      const StartRate: DataRate = DataRate::KilobitsPerSec(300);

    let send_net = s.CreateSimulationNode([&](NetworkSimulationConfig* c) {
        c.bandwidth = LinkCapacity;
        c.delay = TimeDelta::Millis(50);
      });
    let client = s.CreateClient("send", [&](CallClientConfig* c) {
        c.transport.rates.start_rate = StartRate;
      });
      client.UpdateNetworkAdapterId(0);
    let route = s.CreateRoutes(
          client, {send_net}, s.CreateClient("return", CallClientConfig()),
          {s.CreateSimulationNode(NetworkSimulationConfig())});
      s.CreateVideoStream(route.forward(), VideoStreamConfig());
      // Allow the controller to stabilize.
      s.RunFor(TimeDelta::Millis(500));
      assert_relative_eq!(client.send_bandwidth().kbps(), LinkCapacity.kbps(), 300);
      s.ChangeRoute(route.forward(), {send_net});
      // Allow new settings to propagate.
      s.RunFor(TimeDelta::Millis(50));
      // Under the trial, the target should not drop.
      assert_relative_eq!(client.send_bandwidth().kbps(), LinkCapacity.kbps(), 300);

      s.RunFor(TimeDelta::Millis(500));
      // But if adapter id change, BWE should reset and start from the beginning if
      // the network route changes.
      client.UpdateNetworkAdapterId(1);
      s.ChangeRoute(route.forward(), {send_net});
      // Allow new settings to propagate.
      s.RunFor(TimeDelta::Millis(50));
      assert_relative_eq!(client.send_bandwidth().kbps(), StartRate.kbps(), 30);
    }
    */

    /*
    #[test]
    fn CutsHighRateInSafeResetTrial() {
      const LinkCapacity: DataRate = DataRate::KilobitsPerSec(1000);
      const StartRate: DataRate = DataRate::KilobitsPerSec(300);

      ScopedFieldTrials trial("WebRTC-Bwe-SafeResetOnRouteChange/Enabled/");
      Scenario s("googcc_unit/safe_reset_high_cut");
    let send_net = s.CreateSimulationNode([&](NetworkSimulationConfig* c) {
        c.bandwidth = LinkCapacity;
        c.delay = TimeDelta::Millis(50);
      });
    let client = s.CreateClient("send", [&](CallClientConfig* c) {
        c.transport.rates.start_rate = StartRate;
      });
    let route = s.CreateRoutes(
          client, {send_net}, s.CreateClient("return", CallClientConfig()),
          {s.CreateSimulationNode(NetworkSimulationConfig())});
      s.CreateVideoStream(route.forward(), VideoStreamConfig());
      // Allow the controller to stabilize.
      s.RunFor(TimeDelta::Millis(500));
      assert_relative_eq!(client.send_bandwidth().kbps(), LinkCapacity.kbps(), 300);
      client.UpdateNetworkAdapterId(1);
      s.ChangeRoute(route.forward(), {send_net});
      // Allow new settings to propagate.
      s.RunFor(TimeDelta::Millis(50));
      // Under the trial, the target should be reset from high values.
      assert_relative_eq!(client.send_bandwidth().kbps(), StartRate.kbps(), 30);
    }
    */

    /*
    #[test]
    fn DetectsHighRateInSafeResetTrial() {
      ScopedFieldTrials trial("WebRTC-Bwe-SafeResetOnRouteChange/Enabled,ack/");
      const InitialLinkCapacity: DataRate = DataRate::KilobitsPerSec(200);
      const NewLinkCapacity: DataRate = DataRate::KilobitsPerSec(800);
      const StartRate: DataRate = DataRate::KilobitsPerSec(300);

      Scenario s("googcc_unit/safe_reset_high_detect");
    let initial_net = s.CreateSimulationNode([&](NetworkSimulationConfig* c) {
        c.bandwidth = InitialLinkCapacity;
        c.delay = TimeDelta::Millis(50);
      });
    let new_net = s.CreateSimulationNode([&](NetworkSimulationConfig* c) {
        c.bandwidth = NewLinkCapacity;
        c.delay = TimeDelta::Millis(50);
      });
    let client = s.CreateClient("send", [&](CallClientConfig* c) {
        c.transport.rates.start_rate = StartRate;
      });
    let route = s.CreateRoutes(
          client, {initial_net}, s.CreateClient("return", CallClientConfig()),
          {s.CreateSimulationNode(NetworkSimulationConfig())});
      s.CreateVideoStream(route.forward(), VideoStreamConfig());
      // Allow the controller to stabilize.
      s.RunFor(TimeDelta::Millis(2000));
      assert_relative_eq!(client.send_bandwidth().kbps(), InitialLinkCapacity.kbps(), 50);
      client.UpdateNetworkAdapterId(1);
      s.ChangeRoute(route.forward(), {new_net});
      // Allow new settings to propagate, but not probes to be received.
      s.RunFor(TimeDelta::Millis(50));
      // Under the field trial, the target rate should be unchanged since it's lower
      // than the starting rate.
      assert_relative_eq!(client.send_bandwidth().kbps(), InitialLinkCapacity.kbps(), 50);
      // However, probing should have made us detect the higher rate.
      // NOTE: This test causes high loss rate, and the loss-based estimator reduces
      // the bitrate, making the test fail if we wait longer than one second here.
      s.RunFor(TimeDelta::Millis(1000));
      EXPECT_GT(client.send_bandwidth().kbps(), NewLinkCapacity.kbps() - 300);
    }
    */

    /*
    #[test]
    fn TargetRateReducedOnPacingBufferBuildupInTrial() {
      // Configure strict pacing to ensure build-up.
      ScopedFieldTrials trial(
          "WebRTC-CongestionWindow/QueueSize:100,MinBitrate:30000/"
          "WebRTC-Video-Pacing/factor:1.0/"
          "WebRTC-AddPacingToCongestionWindowPushback/Enabled/");

      const LinkCapacity: DataRate = DataRate::KilobitsPerSec(1000);
      const StartRate: DataRate = DataRate::KilobitsPerSec(1000);

      Scenario s("googcc_unit/pacing_buffer_buildup");
    let net = s.CreateSimulationNode([&](NetworkSimulationConfig* c) {
        c.bandwidth = LinkCapacity;
        c.delay = TimeDelta::Millis(50);
      });
    let client = s.CreateClient("send", [&](CallClientConfig* c) {
        c.transport.rates.start_rate = StartRate;
      });
    let route = s.CreateRoutes(
          client, {net}, s.CreateClient("return", CallClientConfig()),
          {s.CreateSimulationNode(NetworkSimulationConfig())});
      s.CreateVideoStream(route.forward(), VideoStreamConfig());
      // Allow some time for the buffer to build up.
      s.RunFor(TimeDelta::Seconds(5));

      // Without trial, pacer delay reaches ~250 ms.
      EXPECT_LT(client.GetStats().pacer_delay_ms, 150);
    }
    */

    /*
    #[test]
    fn NoBandwidthTogglingInLossControlTrial() {
      ScopedFieldTrials trial("WebRTC-Bwe-LossBasedControl/Enabled/");
      Scenario s("googcc_unit/no_toggling");
    let send_net = s.CreateSimulationNode([&](NetworkSimulationConfig* c) {
        c.bandwidth = DataRate::KilobitsPerSec(2000);
        c.loss_rate = 0.2;
        c.delay = TimeDelta::Millis(10);
      });

    let client = s.CreateClient("send", [&](CallClientConfig* c) {
        c.transport.rates.start_rate = DataRate::KilobitsPerSec(300);
      });
    let route = s.CreateRoutes(
          client, {send_net}, s.CreateClient("return", CallClientConfig()),
          {s.CreateSimulationNode(NetworkSimulationConfig())});
      s.CreateVideoStream(route.forward(), VideoStreamConfig());
      // Allow the controller to initialize.
      s.RunFor(TimeDelta::Millis(250));

      std::queue<DataRate> bandwidth_history;
      const step: TimeDelta = TimeDelta::Millis(50);
      for (TimeDelta time = TimeDelta::zero(); time < TimeDelta::Millis(2000);
           time += step) {
        s.RunFor(step);
        const window: TimeDelta = TimeDelta::Millis(500);
        if (bandwidth_history.len() >= window / step)
          bandwidth_history.pop();
        bandwidth_history.push(client.send_bandwidth());
        EXPECT_LT(
            CountBandwidthDips(bandwidth_history, DataRate::KilobitsPerSec(100)),
            2);
      }
    }

    #[test]
    fn NoRttBackoffCollapseWhenVideoStops() {
      ScopedFieldTrials trial("WebRTC-Bwe-MaxRttLimit/limit:2s/");
      Scenario s("googcc_unit/rttbackoff_video_stop");
    let send_net = s.CreateSimulationNode([&](NetworkSimulationConfig* c) {
        c.bandwidth = DataRate::KilobitsPerSec(2000);
        c.delay = TimeDelta::Millis(100);
      });

    let client = s.CreateClient("send", [&](CallClientConfig* c) {
        c.transport.rates.start_rate = DataRate::KilobitsPerSec(1000);
      });
    let route = s.CreateRoutes(
          client, {send_net}, s.CreateClient("return", CallClientConfig()),
          {s.CreateSimulationNode(NetworkSimulationConfig())});
    let video = s.CreateVideoStream(route.forward(), VideoStreamConfig());
      // Allow the controller to initialize, then stop video.
      s.RunFor(TimeDelta::Seconds(1));
      video.send().Stop();
      s.RunFor(TimeDelta::Seconds(4));
      EXPECT_GT(client.send_bandwidth().kbps(), 1000);
    }

    #[test]
    fn NoCrashOnVeryLateFeedback() {
      Scenario s;
    let ret_net = s.CreateMutableSimulationNode(NetworkSimulationConfig());
    let route = s.CreateRoutes(
          s.CreateClient("send", CallClientConfig()),
          {s.CreateSimulationNode(NetworkSimulationConfig())},
          s.CreateClient("return", CallClientConfig()), {ret_net.node()});
    let video = s.CreateVideoStream(route.forward(), VideoStreamConfig());
      s.RunFor(TimeDelta::Seconds(5));
      // Delay feedback by several minutes. This will cause removal of the send time
      // history for the packets as long as SendTimeHistoryWindow is configured for
      // a shorter time span.
      ret_net.PauseTransmissionUntil(s.Now() + TimeDelta::Seconds(300));
      // Stopping video stream while waiting to save test execution time.
      video.send().Stop();
      s.RunFor(TimeDelta::Seconds(299));
      // Starting to cause addition of new packet to history, which cause old
      // packets to be removed.
      video.send().Start();
      // Runs until the lost packets are received. We expect that this will run
      // without causing any runtime failures.
      s.RunFor(TimeDelta::Seconds(2));
    }

    #[test]
    fn IsFairToTCP() {
      Scenario s("googcc_unit/tcp_fairness");
      let mut net_conf = NetworkSimulationConfig::default();
      net_conf.bandwidth = DataRate::KilobitsPerSec(1000);
      net_conf.delay = TimeDelta::Millis(50);
    let client = s.CreateClient("send", [&](CallClientConfig* c) {
        c.transport.rates.start_rate = DataRate::KilobitsPerSec(1000);
      });
    let send_net = {s.CreateSimulationNode(net_conf)};
    let ret_net = {s.CreateSimulationNode(net_conf)};
    let route = s.CreateRoutes(
          client, send_net, s.CreateClient("return", CallClientConfig()), ret_net);
      s.CreateVideoStream(route.forward(), VideoStreamConfig());
      s.net().StartCrossTraffic(CreateFakeTcpCrossTraffic(
          s.net().CreateRoute(send_net), s.net().CreateRoute(ret_net),
          FakeTcpConfig()));
      s.RunFor(TimeDelta::Seconds(10));

      // Currently only testing for the upper limit as we in practice back out
      // quite a lot in this scenario. If this behavior is fixed, we should add a
      // lower bound to ensure it stays fixed.
      EXPECT_LT(client.send_bandwidth().kbps(), 750);
    }

    #[test]
    fn FastRampupOnRembCapLifted() {
      let final_estimate: DataRate =
          RunRembDipScenario("googcc_unit/default_fast_rampup_on_remb_cap_lifted");
      EXPECT_GT(final_estimate.kbps(), 1500);
    }

    #[test]
    fn FallbackToLossBasedBweWithoutPacketFeedback() {
      const LinkCapacity: DataRate = DataRate::KilobitsPerSec(1000);
      const StartRate: DataRate = DataRate::KilobitsPerSec(1000);

      let s = Scenario::new("googcc_unit/high_loss_channel", false);
    let net = s.CreateMutableSimulationNode([&](NetworkSimulationConfig* c) {
        c.bandwidth = LinkCapacity;
        c.delay = TimeDelta::Millis(100);
      });
    let client = s.CreateClient("send", [&](CallClientConfig* c) {
        c.transport.rates.start_rate = StartRate;
      });
    let route = s.CreateRoutes(
          client, {net.node()}, s.CreateClient("return", CallClientConfig()),
          {s.CreateSimulationNode(NetworkSimulationConfig())});

      // Create a config without packet feedback.
      VideoStreamConfig video_config;
      video_config.stream.packet_feedback = false;
      s.CreateVideoStream(route.forward(), video_config);

      s.RunFor(TimeDelta::Seconds(20));
      // Bandwith does not backoff because network is normal.
      EXPECT_GE(client.target_rate().kbps(), 500);

      // Update the network to create high loss ratio
      net.UpdateConfig(|c: &mut NetworkSimulationConfig| { c.loss_rate = 0.15; });
      s.RunFor(TimeDelta::Seconds(20));

      // Bandwidth decreases thanks to loss based bwe v0.
      EXPECT_LE(client.target_rate().kbps(), 300);
    }
    */

    #[test]
    fn calculates_rtt_from_transport_feedback() {
        let mut controller = create_controller(FieldTrials::default(), true);
        let mut current_time: Timestamp = Timestamp::from_millis(123);
        let one_way_delay: TimeDelta = TimeDelta::from_millis(10);
        let mut rtt: Option<TimeDelta> = None;

        let feedback: TransportPacketsFeedback = create_transport_packets_feedback(
            /*per_packet_network_delay=*/ TimeDelta::from_millis(50),
            one_way_delay,
            /*send_time=*/ current_time,
        );
        controller.on_transport_packets_feedback(feedback);
        current_time += TimeDelta::from_millis(50);
        let update = controller.on_process_interval(ProcessInterval {
            at_time: current_time,
            ..Default::default()
        });
        if let Some(target_rate) = update.target_rate {
            rtt = Some(target_rate.network_estimate.round_trip_time);
        }
        assert!(rtt.is_some());
        assert_eq!(rtt.unwrap().ms(), 2 * one_way_delay.ms());
    }
}