You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
4.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2018 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef CALL_SIMULATED_NETWORK_H_
  11. #define CALL_SIMULATED_NETWORK_H_
  12. #include <stdint.h>
  13. #include <deque>
  14. #include <queue>
  15. #include <vector>
  16. #include "absl/types/optional.h"
  17. #include "api/test/simulated_network.h"
  18. #include "api/units/data_size.h"
  19. #include "api/units/timestamp.h"
  20. #include "rtc_base/critical_section.h"
  21. #include "rtc_base/race_checker.h"
  22. #include "rtc_base/random.h"
  23. #include "rtc_base/thread_annotations.h"
  24. #include "rtc_base/thread_checker.h"
  25. namespace webrtc {
  26. // Implementation of the CoDel active queue management algorithm. Loosely based
  27. // on CoDel pseudocode from ACMQueue. CoDel keeps queuing delays low by dropping
  28. // packets when delay is high. For each packet ready for dequeue, call
  29. // DropDequeuePacket with the packet parameters to update the CoDel state.
  30. class CoDelSimulation {
  31. public:
  32. CoDelSimulation();
  33. ~CoDelSimulation();
  34. // Returns true if packet should be dropped.
  35. bool DropDequeuedPacket(Timestamp now,
  36. Timestamp enqueing_time,
  37. DataSize packet_size,
  38. DataSize queue_size);
  39. private:
  40. enum State { kNormal, kPending, kDropping };
  41. Timestamp enter_drop_state_at_ = Timestamp::PlusInfinity();
  42. Timestamp last_drop_at_ = Timestamp::MinusInfinity();
  43. int drop_count_ = 0;
  44. int previous_drop_count_ = 0;
  45. State state_ = State::kNormal;
  46. };
  47. // Class simulating a network link. This is a simple and naive solution just
  48. // faking capacity and adding an extra transport delay in addition to the
  49. // capacity introduced delay.
  50. class SimulatedNetwork : public NetworkBehaviorInterface {
  51. public:
  52. using Config = BuiltInNetworkBehaviorConfig;
  53. explicit SimulatedNetwork(Config config, uint64_t random_seed = 1);
  54. ~SimulatedNetwork() override;
  55. // Sets a new configuration. This won't affect packets already in the pipe.
  56. void SetConfig(const Config& config);
  57. void PauseTransmissionUntil(int64_t until_us);
  58. // NetworkBehaviorInterface
  59. bool EnqueuePacket(PacketInFlightInfo packet) override;
  60. std::vector<PacketDeliveryInfo> DequeueDeliverablePackets(
  61. int64_t receive_time_us) override;
  62. absl::optional<int64_t> NextDeliveryTimeUs() const override;
  63. private:
  64. struct PacketInfo {
  65. PacketInFlightInfo packet;
  66. int64_t arrival_time_us;
  67. };
  68. // Contains current configuration state.
  69. struct ConfigState {
  70. // Static link configuration.
  71. Config config;
  72. // The probability to drop the packet if we are currently dropping a
  73. // burst of packet
  74. double prob_loss_bursting;
  75. // The probability to drop a burst of packets.
  76. double prob_start_bursting;
  77. // Used for temporary delay spikes.
  78. int64_t pause_transmission_until_us = 0;
  79. };
  80. // Moves packets from capacity- to delay link.
  81. void UpdateCapacityQueue(ConfigState state, int64_t time_now_us)
  82. RTC_RUN_ON(&process_checker_);
  83. ConfigState GetConfigState() const;
  84. rtc::CriticalSection config_lock_;
  85. // |process_checker_| guards the data structures involved in delay and loss
  86. // processes, such as the packet queues.
  87. rtc::RaceChecker process_checker_;
  88. CoDelSimulation codel_controller_ RTC_GUARDED_BY(process_checker_);
  89. std::queue<PacketInfo> capacity_link_ RTC_GUARDED_BY(process_checker_);
  90. Random random_;
  91. std::deque<PacketInfo> delay_link_ RTC_GUARDED_BY(process_checker_);
  92. ConfigState config_state_ RTC_GUARDED_BY(config_lock_);
  93. // Are we currently dropping a burst of packets?
  94. bool bursting_;
  95. int64_t queue_size_bytes_ RTC_GUARDED_BY(process_checker_) = 0;
  96. int64_t pending_drain_bits_ RTC_GUARDED_BY(process_checker_) = 0;
  97. absl::optional<int64_t> last_capacity_link_visit_us_
  98. RTC_GUARDED_BY(process_checker_);
  99. absl::optional<int64_t> next_process_time_us_
  100. RTC_GUARDED_BY(process_checker_);
  101. };
  102. } // namespace webrtc
  103. #endif // CALL_SIMULATED_NETWORK_H_