bitrate_prober.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2014 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 MODULES_PACING_BITRATE_PROBER_H_
  11. #define MODULES_PACING_BITRATE_PROBER_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <queue>
  15. #include "api/transport/field_trial_based_config.h"
  16. #include "api/transport/network_types.h"
  17. #include "rtc_base/experiments/field_trial_parser.h"
  18. namespace webrtc {
  19. class RtcEventLog;
  20. struct BitrateProberConfig {
  21. explicit BitrateProberConfig(const WebRtcKeyValueConfig* key_value_config);
  22. BitrateProberConfig(const BitrateProberConfig&) = default;
  23. BitrateProberConfig& operator=(const BitrateProberConfig&) = default;
  24. ~BitrateProberConfig() = default;
  25. // The minimum number probing packets used.
  26. FieldTrialParameter<int> min_probe_packets_sent;
  27. // A minimum interval between probes to allow scheduling to be feasible.
  28. FieldTrialParameter<TimeDelta> min_probe_delta;
  29. // The minimum probing duration.
  30. FieldTrialParameter<TimeDelta> min_probe_duration;
  31. // Maximum amount of time each probe can be delayed.
  32. FieldTrialParameter<TimeDelta> max_probe_delay;
  33. // If NextProbeTime() is called with a delay higher than specified by
  34. // |max_probe_delay|, abort it.
  35. FieldTrialParameter<bool> abort_delayed_probes;
  36. };
  37. // Note that this class isn't thread-safe by itself and therefore relies
  38. // on being protected by the caller.
  39. class BitrateProber {
  40. public:
  41. explicit BitrateProber(const WebRtcKeyValueConfig& field_trials);
  42. ~BitrateProber();
  43. void SetEnabled(bool enable);
  44. // Returns true if the prober is in a probing session, i.e., it currently
  45. // wants packets to be sent out according to the time returned by
  46. // TimeUntilNextProbe().
  47. bool is_probing() const { return probing_state_ == ProbingState::kActive; }
  48. // Initializes a new probing session if the prober is allowed to probe. Does
  49. // not initialize the prober unless the packet size is large enough to probe
  50. // with.
  51. void OnIncomingPacket(DataSize packet_size);
  52. // Create a cluster used to probe for |bitrate_bps| with |num_probes| number
  53. // of probes.
  54. void CreateProbeCluster(DataRate bitrate, Timestamp now, int cluster_id);
  55. // Returns the time at which the next probe should be sent to get accurate
  56. // probing. If probing is not desired at this time, Timestamp::PlusInfinity()
  57. // will be returned.
  58. // TODO(bugs.webrtc.org/11780): Remove |now| argument when old mode is gone.
  59. Timestamp NextProbeTime(Timestamp now) const;
  60. // Information about the current probing cluster.
  61. absl::optional<PacedPacketInfo> CurrentCluster(Timestamp now);
  62. // Returns the minimum number of bytes that the prober recommends for
  63. // the next probe, or zero if not probing.
  64. DataSize RecommendedMinProbeSize() const;
  65. // Called to report to the prober that a probe has been sent. In case of
  66. // multiple packets per probe, this call would be made at the end of sending
  67. // the last packet in probe. |size| is the total size of all packets in probe.
  68. void ProbeSent(Timestamp now, DataSize size);
  69. private:
  70. enum class ProbingState {
  71. // Probing will not be triggered in this state at all times.
  72. kDisabled,
  73. // Probing is enabled and ready to trigger on the first packet arrival.
  74. kInactive,
  75. // Probe cluster is filled with the set of data rates to be probed and
  76. // probes are being sent.
  77. kActive,
  78. // Probing is enabled, but currently suspended until an explicit trigger
  79. // to start probing again.
  80. kSuspended,
  81. };
  82. // A probe cluster consists of a set of probes. Each probe in turn can be
  83. // divided into a number of packets to accommodate the MTU on the network.
  84. struct ProbeCluster {
  85. PacedPacketInfo pace_info;
  86. int sent_probes = 0;
  87. int sent_bytes = 0;
  88. Timestamp created_at = Timestamp::MinusInfinity();
  89. Timestamp started_at = Timestamp::MinusInfinity();
  90. int retries = 0;
  91. };
  92. Timestamp CalculateNextProbeTime(const ProbeCluster& cluster) const;
  93. ProbingState probing_state_;
  94. // Probe bitrate per packet. These are used to compute the delta relative to
  95. // the previous probe packet based on the size and time when that packet was
  96. // sent.
  97. std::queue<ProbeCluster> clusters_;
  98. // Time the next probe should be sent when in kActive state.
  99. Timestamp next_probe_time_;
  100. int total_probe_count_;
  101. int total_failed_probe_count_;
  102. BitrateProberConfig config_;
  103. };
  104. } // namespace webrtc
  105. #endif // MODULES_PACING_BITRATE_PROBER_H_