simulated_network.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 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 API_TEST_SIMULATED_NETWORK_H_
  11. #define API_TEST_SIMULATED_NETWORK_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <deque>
  15. #include <queue>
  16. #include <vector>
  17. #include "absl/types/optional.h"
  18. #include "rtc_base/critical_section.h"
  19. #include "rtc_base/random.h"
  20. #include "rtc_base/thread_annotations.h"
  21. namespace webrtc {
  22. struct PacketInFlightInfo {
  23. PacketInFlightInfo(size_t size, int64_t send_time_us, uint64_t packet_id)
  24. : size(size), send_time_us(send_time_us), packet_id(packet_id) {}
  25. size_t size;
  26. int64_t send_time_us;
  27. // Unique identifier for the packet in relation to other packets in flight.
  28. uint64_t packet_id;
  29. };
  30. struct PacketDeliveryInfo {
  31. static constexpr int kNotReceived = -1;
  32. PacketDeliveryInfo(PacketInFlightInfo source, int64_t receive_time_us)
  33. : receive_time_us(receive_time_us), packet_id(source.packet_id) {}
  34. int64_t receive_time_us;
  35. uint64_t packet_id;
  36. };
  37. // BuiltInNetworkBehaviorConfig is a built-in network behavior configuration
  38. // for built-in network behavior that will be used by WebRTC if no custom
  39. // NetworkBehaviorInterface is provided.
  40. struct BuiltInNetworkBehaviorConfig {
  41. BuiltInNetworkBehaviorConfig() {}
  42. // Queue length in number of packets.
  43. size_t queue_length_packets = 0;
  44. // Delay in addition to capacity induced delay.
  45. int queue_delay_ms = 0;
  46. // Standard deviation of the extra delay.
  47. int delay_standard_deviation_ms = 0;
  48. // Link capacity in kbps.
  49. int link_capacity_kbps = 0;
  50. // Random packet loss.
  51. int loss_percent = 0;
  52. // If packets are allowed to be reordered.
  53. bool allow_reordering = false;
  54. // The average length of a burst of lost packets.
  55. int avg_burst_loss_length = -1;
  56. // Additional bytes to add to packet size.
  57. int packet_overhead = 0;
  58. // Enable CoDel active queue management.
  59. bool codel_active_queue_management = false;
  60. };
  61. class NetworkBehaviorInterface {
  62. public:
  63. virtual bool EnqueuePacket(PacketInFlightInfo packet_info) = 0;
  64. // Retrieves all packets that should be delivered by the given receive time.
  65. virtual std::vector<PacketDeliveryInfo> DequeueDeliverablePackets(
  66. int64_t receive_time_us) = 0;
  67. // Returns time in microseconds when caller should call
  68. // DequeueDeliverablePackets to get next set of packets to deliver.
  69. virtual absl::optional<int64_t> NextDeliveryTimeUs() const = 0;
  70. virtual ~NetworkBehaviorInterface() = default;
  71. };
  72. // Class simulating a network link. This is a simple and naive solution just
  73. // faking capacity and adding an extra transport delay in addition to the
  74. // capacity introduced delay.
  75. class SimulatedNetworkInterface : public NetworkBehaviorInterface {
  76. public:
  77. // Sets a new configuration. This won't affect packets already in the pipe.
  78. virtual void SetConfig(const BuiltInNetworkBehaviorConfig& config) = 0;
  79. virtual void UpdateConfig(
  80. std::function<void(BuiltInNetworkBehaviorConfig*)> config_modifier) = 0;
  81. virtual void PauseTransmissionUntil(int64_t until_us) = 0;
  82. };
  83. } // namespace webrtc
  84. #endif // API_TEST_SIMULATED_NETWORK_H_