simulated_network.h 3.4 KB

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