{"id":543,"date":"2022-03-09T23:37:54","date_gmt":"2022-03-09T15:37:54","guid":{"rendered":"https:\/\/www.fanyamin.com\/wordpress\/?p=543"},"modified":"2022-03-10T08:58:54","modified_gmt":"2022-03-10T00:58:54","slug":"webrtc-%e6%ba%90%e7%a0%81%e9%98%85%e8%af%bb%e7%ac%94%e8%ae%b0%e4%b9%8backnowledgedbitrateestimator","status":"publish","type":"post","link":"https:\/\/www.fanyamin.com\/wordpress\/?p=543","title":{"rendered":"WebRTC \u6e90\u7801\u9605\u8bfb\u7b14\u8bb0\u4e4bAcknowledgedBitrateEstimator"},"content":{"rendered":"<h1>AcknowledgedBitrateEstimatorInterface<\/h1>\n<p>\u786e\u8ba4\u7684\u6bd4\u7279\u7387\u4f30\u7b97\u5668\u63a5\u53e3<\/p>\n<pre><code>class AcknowledgedBitrateEstimatorInterface {\n public:\n  static std::unique_ptr&lt;AcknowledgedBitrateEstimatorInterface&gt; Create(\n      const WebRtcKeyValueConfig* key_value_config);\n  virtual ~AcknowledgedBitrateEstimatorInterface();\n\n  virtual void IncomingPacketFeedbackVector(\n      const std::vector&lt;PacketResult&gt;&amp; packet_feedback_vector) = 0;\n  virtual absl::optional&lt;DataRate&gt; bitrate() const = 0;\n  virtual absl::optional&lt;DataRate&gt; PeekRate() const = 0;\n  virtual void SetAlr(bool in_alr) = 0;\n  virtual void SetAlrEndedTime(Timestamp alr_ended_time) = 0;\n};<\/code><\/pre>\n<h1>AcknowledgedBitrateEstimator<\/h1>\n<p>\u786e\u8ba4\u7684\u6bd4\u7279\u7387\u4f30\u7b97\u5668<\/p>\n<pre><code>class AcknowledgedBitrateEstimator\n    : public AcknowledgedBitrateEstimatorInterface {\n public:\n  AcknowledgedBitrateEstimator(\n      const WebRtcKeyValueConfig* key_value_config,\n      std::unique_ptr&lt;BitrateEstimator&gt; bitrate_estimator);\n\n  explicit AcknowledgedBitrateEstimator(\n      const WebRtcKeyValueConfig* key_value_config);\n  ~AcknowledgedBitrateEstimator() override;\n\n  void IncomingPacketFeedbackVector(\n      const std::vector&lt;PacketResult&gt;&amp; packet_feedback_vector) override;\n  absl::optional&lt;DataRate&gt; bitrate() const override;\n  absl::optional&lt;DataRate&gt; PeekRate() const override;\n  void SetAlr(bool in_alr) override;\n  void SetAlrEndedTime(Timestamp alr_ended_time) override;\n\n private:\n  absl::optional&lt;Timestamp&gt; alr_ended_time_;\n  bool in_alr_;\n  std::unique_ptr&lt;BitrateEstimator&gt; bitrate_estimator_;\n};<\/code><\/pre>\n<h2>AcknowledgedBitrateEstimator::IncomingPacketFeedbackVector<\/h2>\n<pre><code class=\"language-cpp\">void AcknowledgedBitrateEstimator::IncomingPacketFeedbackVector(\n    const std::vector&lt;PacketResult&gt;&amp; packet_feedback_vector) {\n  RTC_DCHECK(std::is_sorted(packet_feedback_vector.begin(),\n                            packet_feedback_vector.end(),\n                            PacketResult::ReceiveTimeOrder()));\n  \/\/\u904d\u5386\u6570\u636e\u5305\u53cd\u9988\u5217\u8868\n  for (const auto&amp; packet : packet_feedback_vector) {\n    \/\/\u5f53 ALR \u505c\u6b62\uff0c \u53d1\u9001\u7684\u5305\u7684\u65f6\u95f4\u6bd4 ALR \u7684\u505c\u6b62\u65f6\u95f4\u8fd8\u665a\uff0c\u5219\u671f\u671b\u4e00\u4e2a\u5feb\u901f\u7684\u901f\u7387\u6539\u53d8\n    if (alr_ended_time_ &amp;&amp; packet.sent_packet.send_time &gt; *alr_ended_time_) {\n      bitrate_estimator_-&gt;ExpectFastRateChange();\n      alr_ended_time_.reset();\n    }\n    \/\/\u786e\u8ba4\u7684\u4f30\u8ba1\u91cf\u7b49\u4e8e\u53d1\u9001\u7684\u5305\u5927\u5c0f\uff0c \u518d\u52a0\u4e0a\u5148\u524d\u672a\u786e\u8ba4\u7684\u5305\n    DataSize acknowledged_estimate = packet.sent_packet.size;\n    acknowledged_estimate += packet.sent_packet.prior_unacked_data;\n    \/\/\u4ee5\u5305\u7684\u63a5\u6536\u65f6\u95f4\uff0c\u786e\u8ba4\u4f30\u8ba1\u91cf\uff0c\u662f\u5426\u5728 ALR \u4e09\u4e2a\u53c2\u6570\u6765\u4fee\u6b63\u5e26\u5bbd\u4f30\u7b97\u5668\n    bitrate_estimator_-&gt;Update(packet.receive_time, acknowledged_estimate,\n                               in_alr_);\n  }\n}<\/code><\/pre>\n<h1>BitrateEstimator<\/h1>\n<p>\u8ba1\u7b97\u7ed9\u5b9a\u7684\u786e\u8ba4\u7684\u541e\u5410\u91cf\u7684\u8d1d\u53f6\u65af\u4f30\u8ba1\u503c\uff0c\u5176\u4e2d\u5305\u542b\u5230\u8fbe\u65f6\u95f4\u548c\u6709\u6548\u8d1f\u8f7d\u5927\u5c0f\u3002\u4e0e\u5f53\u524d\u4f30\u8ba1\u503c\u76f8\u5dee\u751a\u8fdc\u6216\u57fa\u4e8e\u5c11\u91cf\u6570\u636e\u5305\u7684\u6837\u672c\u6743\u91cd\u8f83\u5c0f\uff0c\u56e0\u4e3a\u5b83\u4eec\u88ab\u8ba4\u4e3a\u66f4\u6709\u53ef\u80fd\u662f\u7531\u4e0e\u62e5\u585e\u65e0\u5173\u7684\u5ef6\u8fdf\u5cf0\u503c\u5f15\u8d77\u7684\u3002<\/p>\n<pre><code class=\"language-cpp\">\/\/ Computes a bayesian estimate of the throughput given acks containing\n\/\/ the arrival time and payload size. Samples which are far from the current\n\/\/ estimate or are based on few packets are given a smaller weight, as they\n\/\/ are considered to be more likely to have been caused by, e.g., delay spikes\n\/\/ unrelated to congestion.\nclass BitrateEstimator {\n public:\n  explicit BitrateEstimator(const WebRtcKeyValueConfig* key_value_config);\n  virtual ~BitrateEstimator();\n  virtual void Update(Timestamp at_time, DataSize amount, bool in_alr);\n\n  virtual absl::optional&lt;DataRate&gt; bitrate() const;\n  absl::optional&lt;DataRate&gt; PeekRate() const;\n\n  virtual void ExpectFastRateChange();\n\n private:\n  float UpdateWindow(int64_t now_ms,\n                     int bytes,\n                     int rate_window_ms,\n                     bool* is_small_sample);\n  int sum_;\n  FieldTrialConstrained&lt;int&gt; initial_window_ms_;\n  FieldTrialConstrained&lt;int&gt; noninitial_window_ms_;\n  FieldTrialParameter&lt;double&gt; uncertainty_scale_;\n  FieldTrialParameter&lt;double&gt; uncertainty_scale_in_alr_;\n  FieldTrialParameter&lt;double&gt; small_sample_uncertainty_scale_;\n  FieldTrialParameter&lt;DataSize&gt; small_sample_threshold_;\n  FieldTrialParameter&lt;DataRate&gt; uncertainty_symmetry_cap_;\n  FieldTrialParameter&lt;DataRate&gt; estimate_floor_;\n  int64_t current_window_ms_;\n  int64_t prev_time_ms_;\n  float bitrate_estimate_kbps_;\n  float bitrate_estimate_var_;\n};\n<\/code><\/pre>\n<h2>BitrateEstimator::Update<\/h2>\n<p>\u8fd9\u91cc\u7528\u5230\u4e86\u8d1d\u53f6\u65af\u4f30\u8ba1, \u5e26\u5bbd\u4f30\u7b97\u51c6\u786e\u7684\u6982\u7387\u662f\u968f\u7740\u5b9e\u9645\u7684\u4f20\u8f93\u6bd4\u7279\u7387\u7684\u53d8\u5316\u800c\u8c03\u6574\u7684<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.fanyamin.com\/wordpress\/wp-content\/uploads\/2022\/03\/image-1646840749447.png\" alt=\"file\" \/><\/p>\n<p>\u73b0\u8c61 B \u51fa\u73b0\u7684\u60c5\u51b5\u4e0b\u4e8b\u4ef6 A \u53d1\u751f\u7684\u6982\u7387, \u7b49\u4e8e\u4e8b\u4ef6 A \u53d1\u751f\u65f6\u73b0\u8c61 B \u51fa\u73b0\u7684\u6982\u7387,\u4e58\u4ee5\u4e8b\u4ef6 A \u53d1\u751f\u7684\u6982\u7387, \u518d\u9664\u4ee5\u73b0\u8c61 B \u51fa\u73b0\u7684\u6982\u7387<\/p>\n<pre><code class=\"language-cpp\">\nvoid BitrateEstimator::Update(Timestamp at_time, DataSize amount, bool in_alr) {\n  int rate_window_ms = noninitial_window_ms_.Get();\n  \/\/ We use a larger window at the beginning to get a more stable sample that\n  \/\/ we can use to initialize the estimate.\n  if (bitrate_estimate_kbps_ &lt; 0.f)\n    rate_window_ms = initial_window_ms_.Get();\n  bool is_small_sample = false;\n  float bitrate_sample_kbps = UpdateWindow(at_time.ms(), amount.bytes(),\n                                           rate_window_ms, &amp;is_small_sample);\n  if (bitrate_sample_kbps &lt; 0.0f)\n    return;\n  if (bitrate_estimate_kbps_ &lt; 0.0f) {\n    \/\/ This is the very first sample we get. Use it to initialize the estimate.\n    bitrate_estimate_kbps_ = bitrate_sample_kbps;\n    return;\n  }\n  \/\/ Optionally use higher uncertainty for very small samples to avoid dropping\n  \/\/ estimate and for samples obtained in ALR.\n  float scale = uncertainty_scale_;\n  if (is_small_sample &amp;&amp; bitrate_sample_kbps &lt; bitrate_estimate_kbps_) {\n    scale = small_sample_uncertainty_scale_;\n  } else if (in_alr &amp;&amp; bitrate_sample_kbps &lt; bitrate_estimate_kbps_) {\n    \/\/ Optionally use higher uncertainty for samples obtained during ALR.\n    scale = uncertainty_scale_in_alr_;\n  }\n  \/\/ Define the sample uncertainty as a function of how far away it is from the\n  \/\/ current estimate. With low values of uncertainty_symmetry_cap_ we add more\n  \/\/ uncertainty to increases than to decreases. For higher values we approach\n  \/\/ symmetry.\n  float sample_uncertainty =\n      scale * std::abs(bitrate_estimate_kbps_ - bitrate_sample_kbps) \/\n      (bitrate_estimate_kbps_ +\n       std::min(bitrate_sample_kbps,\n                uncertainty_symmetry_cap_.Get().kbps&lt;float&gt;()));\n\n  float sample_var = sample_uncertainty * sample_uncertainty;\n  \/\/ Update a bayesian estimate of the rate, weighting it lower if the sample\n  \/\/ uncertainty is large.\n  \/\/ The bitrate estimate uncertainty is increased with each update to model\n  \/\/ that the bitrate changes over time.\n  float pred_bitrate_estimate_var = bitrate_estimate_var_ + 5.f;\n  bitrate_estimate_kbps_ = (sample_var * bitrate_estimate_kbps_ +\n                            pred_bitrate_estimate_var * bitrate_sample_kbps) \/\n                           (sample_var + pred_bitrate_estimate_var);\n  bitrate_estimate_kbps_ =\n      std::max(bitrate_estimate_kbps_, estimate_floor_.Get().kbps&lt;float&gt;());\n  bitrate_estimate_var_ = sample_var * pred_bitrate_estimate_var \/\n                          (sample_var + pred_bitrate_estimate_var);\n  BWE_TEST_LOGGING_PLOT(1, &quot;acknowledged_bitrate&quot;, at_time.ms(),\n                        bitrate_estimate_kbps_ * 1000);\n}\n<\/code><\/pre>\n<h2>UpdateWindow<\/h2>\n<p>\u4fee\u6539\u5f53\u524d\u65f6\u95f4\u6240\u5728\u7684\u6ed1\u52a8\u7a97\u53e3\u7684\u4f20\u8f93\u5b57\u8282\u6570<\/p>\n<pre><code class=\"language-cpp\">\nfloat BitrateEstimator::UpdateWindow(int64_t now_ms,\n                                     int bytes,\n                                     int rate_window_ms,\n                                     bool* is_small_sample) {\n  RTC_DCHECK(is_small_sample != nullptr);\n  \/\/ Reset if time moves backwards.\n  if (now_ms &lt; prev_time_ms_) {\n    prev_time_ms_ = -1;\n    sum_ = 0;\n    current_window_ms_ = 0;\n  }\n  if (prev_time_ms_ &gt;= 0) {\n    current_window_ms_ += now_ms - prev_time_ms_;\n    \/\/ Reset if nothing has been received for more than a full window.\n    if (now_ms - prev_time_ms_ &gt; rate_window_ms) {\n      sum_ = 0;\n      current_window_ms_ %= rate_window_ms;\n    }\n  }\n  prev_time_ms_ = now_ms;\n  float bitrate_sample = -1.0f;\n  if (current_window_ms_ &gt;= rate_window_ms) {\n    *is_small_sample = sum_ &lt; small_sample_threshold_-&gt;bytes();\n    bitrate_sample = 8.0f * sum_ \/ static_cast&lt;float&gt;(rate_window_ms);\n    current_window_ms_ -= rate_window_ms;\n    sum_ = 0;\n  }\n  sum_ += bytes;\n  return bitrate_sample;\n}\n\nabsl::optional&lt;DataRate&gt; BitrateEstimator::bitrate() const {\n  if (bitrate_estimate_kbps_ &lt; 0.f)\n    return absl::nullopt;\n  return DataRate::KilobitsPerSec(bitrate_estimate_kbps_);\n}\n\nabsl::optional&lt;DataRate&gt; BitrateEstimator::PeekRate() const {\n  if (current_window_ms_ &gt; 0)\n    return DataSize::Bytes(sum_) \/ TimeDelta::Millis(current_window_ms_);\n  return absl::nullopt;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>AcknowledgedBitrateEstimatorInterface \u786e\u8ba4\u7684\u6bd4\u7279\u7387\u4f30\u7b97\u5668\u63a5\u53e3 class AcknowledgedBitrateEstimatorInterface { public: static std::unique_ptr&lt;AcknowledgedBitrateEstimatorInterface&gt; Create( const WebRtcKeyValueConfig* key_value_config); virtual ~AcknowledgedBitrateEstimatorInterface(); virtual void IncomingPacketFeedbackVector( const std::vector&lt;PacketResult&gt;&amp; packet_feedback_vector) = 0; virtual absl::optional&lt;DataRate&gt; bitrate() const = 0; virtual absl::optional&lt;DataRate&gt; PeekRate() const = 0; virtual void SetAlr(bool in_alr) = 0; virtual void SetAlrEndedTime(Timestamp alr_ended_time) = 0; }; AcknowledgedBitrateEstimator \u786e\u8ba4\u7684\u6bd4\u7279\u7387\u4f30\u7b97\u5668 class AcknowledgedBitrateEstimator : public AcknowledgedBitrateEstimatorInterface { [&hellip;] <a class=\"read-more\" href=\"https:\/\/www.fanyamin.com\/wordpress\/?p=543\" title=\"Permanent Link to: WebRTC \u6e90\u7801\u9605\u8bfb\u7b14\u8bb0\u4e4bAcknowledgedBitrateEstimator\">&rarr;Read&nbsp;more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-543","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/543"}],"collection":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=543"}],"version-history":[{"count":8,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/543\/revisions"}],"predecessor-version":[{"id":552,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/543\/revisions\/552"}],"wp:attachment":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}