{"id":535,"date":"2022-03-09T23:24:01","date_gmt":"2022-03-09T15:24:01","guid":{"rendered":"https:\/\/www.fanyamin.com\/wordpress\/?p=535"},"modified":"2022-03-29T09:15:36","modified_gmt":"2022-03-29T01:15:36","slug":"webrtc-%e6%ba%90%e7%a0%81%e9%98%85%e8%af%bb%e7%ac%94%e8%ae%b0%e4%b9%8b-alrdetector","status":"publish","type":"post","link":"https:\/\/www.fanyamin.com\/wordpress\/?p=535","title":{"rendered":"WebRTC \u6e90\u7801\u9605\u8bfb\u7b14\u8bb0\u4e4b AlrDetector"},"content":{"rendered":"<h1>Overview<\/h1>\n<p>ALR\uff08Application limited region detector\uff09\u7684\u57fa\u672c\u539f\u7406\u5c31\u662f<br \/>\nSentBitRate\/EstimatedBitRate \u7684\u767e\u5206\u6bd4\u4e0e kAlrStartUsagePercent\uff0850\uff09\u505a\u6bd4\u8f83\uff0c\u5f53\u5c0f\u4e8e\u8be5\u503c\u8ba4\u4e3a\u7f51\u7edc\u53d7\u9650\uff0c\u9700\u8981\u542f\u52a8 probe \u91cd\u65b0\u63a2\u6d4b\u5e26\u5bbd\uff0c\u5f53\u5927\u4e8e kAlrEndUsagePercent\uff0880\uff09\uff0c\u8ba4\u4e3a\u7f51\u7edc\u6062\u590d\uff0c \u505c\u6b62\u8fdb\u884c\u542f\u52a8\u4e0b\u6b21 probe \u63a2\u6d4b<\/p>\n<h1>AlrDetectorConfig<\/h1>\n<p>\u53d1\u9001\u6d41\u91cf\u6bd4\u7387(sent_bitrate\/estimated_bitrate) \u4f5c\u4e3a\u7f51\u7edc\u5bb9\u91cf\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u786e\u5b9a\u5e94\u7528\u7a0b\u5e8f\u53d7\u9650\u533a\u57df ALR(Application-Limited Region)\u3002<\/p>\n<p>ALR \u533a\u57df\u5728\u5e26\u5bbd\u5269\u4f59\u7387\u5927\u4e8e kAlrStartUsageRatio (\u9ed8\u8ba4\u4e3a0.8) \u65f6\u5f00\u59cb\uff0c\u5728\u5e26\u5bbd\u5269\u4f59\u7387\u5927\u4e8e kAlrEndUsageRatio (\u9ed8\u8ba4\u4e3a 0.5) \u65f6\u7ed3\u675f\u3002<\/p>\n<p>\u6ce8\u610f\uff1a<\/p>\n<ul>\n<li>\u5e26\u5bbd\u5269\u4f59\u7387 = 1 - send_bitrate\/estimated_bitrate<\/li>\n<li>\u5728\u5bf9\u5e94\u7528\u6709\u9650\u533a\u57df\u7684 BW \u8c03\u6574\u8fdb\u884c\u6709\u6548\u5fae\u8c03\u4e4b\u524d\uff0c\u8fd9\u662f\u6709\u610f\u505a\u4e86\u4e00\u4e9b\u4fdd\u5b88\u7684\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-cpp\">\nstruct AlrDetectorConfig {\n  \/\/ Sent traffic ratio as a function of network capacity used to determine\n  \/\/ application-limited region. ALR region start when bandwidth usage drops\n  \/\/ below kAlrStartUsageRatio and ends when it raises above\n  \/\/ kAlrEndUsageRatio. NOTE: This is intentionally conservative at the moment\n  \/\/ until BW adjustments of application limited region is fine tuned.\n  double bandwidth_usage_ratio = 0.65;\n  double start_budget_level_ratio = 0.80;\n  double stop_budget_level_ratio = 0.50;\n  std::unique_ptr&lt;StructParametersParser&gt; Parser();\n};<\/code><\/pre>\n<h1>IntervalBudget<\/h1>\n<p>\u5728\u4e00\u5b9a\u65f6\u95f4\u95f4\u9694\u5185\u7684\u9884\u7b97(\u53ef\u4f7f\u7528\u5b57\u8282\u6570)<\/p>\n<pre><code>class IntervalBudget {\n public:\n  explicit IntervalBudget(int initial_target_rate_kbps);\n  IntervalBudget(int initial_target_rate_kbps, bool can_build_up_underuse);\n  void set_target_rate_kbps(int target_rate_kbps);\n\n  \/\/ TODO(tschumim): Unify IncreaseBudget and UseBudget to one function.\n  void IncreaseBudget(int64_t delta_time_ms);\n  void UseBudget(size_t bytes);\n\n  size_t bytes_remaining() const;\n  double budget_ratio() const;\n  int target_rate_kbps() const;\n\n private:\n  \/\/\u76ee\u6807\u7801\u7387\n  int target_rate_kbps_;\n  \/\/\u6700\u5927\u9884\u7b97\u5b57\u8282\u6570\n  int64_t max_bytes_in_budget_;\n  \/\/\u5269\u4f59\u5b57\u8282\u6570\n  int64_t bytes_remaining_;\n  \/\/\u662f\u5426\u6784\u5efa\u4f7f\u7528\u4e0d\u8db3?\n  bool can_build_up_underuse_;\n};\n\nIntervalBudget::IntervalBudget(int initial_target_rate_kbps)\n    : IntervalBudget(initial_target_rate_kbps, false) {}\n\nIntervalBudget::IntervalBudget(int initial_target_rate_kbps,\n                               bool can_build_up_underuse)\n    : bytes_remaining_(0), can_build_up_underuse_(can_build_up_underuse) {\n  set_target_rate_kbps(initial_target_rate_kbps);\n}\n\nvoid IntervalBudget::set_target_rate_kbps(int target_rate_kbps) {\n  target_rate_kbps_ = target_rate_kbps;\n  max_bytes_in_budget_ = (kWindowMs * target_rate_kbps_) \/ 8;\n  bytes_remaining_ = std::min(std::max(-max_bytes_in_budget_, bytes_remaining_),\n                              max_bytes_in_budget_);\n}\n\/\/\u589e\u5927\u9884\u7b97\nvoid IntervalBudget::IncreaseBudget(int64_t delta_time_ms) {\n  int64_t bytes = target_rate_kbps_ * delta_time_ms \/ 8;\n  if (bytes_remaining_ &lt; 0 || can_build_up_underuse_) {\n    \/\/ We overused last interval, compensate this interval.\n    bytes_remaining_ = std::min(bytes_remaining_ + bytes, max_bytes_in_budget_);\n  } else {\n    \/\/ If we underused last interval we can&#039;t use it this interval.\n    bytes_remaining_ = std::min(bytes, max_bytes_in_budget_);\n  }\n}\n\n\/\/\u4f7f\u7528\u9884\u7b97: \u5269\u4f59\u7684\u5b57\u8282\u6570\u518d\u51cf\u6cd5\u4f20\u5165\u7684\u5b57\u8282\u6570\nvoid IntervalBudget::UseBudget(size_t bytes) {\n  bytes_remaining_ = std::max(bytes_remaining_ - static_cast&lt;int&gt;(bytes),\n                              -max_bytes_in_budget_);\n}\n\nsize_t IntervalBudget::bytes_remaining() const {\n  return rtc::saturated_cast&lt;size_t&gt;(std::max&lt;int64_t&gt;(0, bytes_remaining_));\n}\n\/\/\u9884\u7b97\u5269\u4f59\u7387: \u5269\u4f59\u7684\u5b57\u8282\/\u6700\u5927\u9884\u7b97\u5b57\u8282\u6570\ndouble IntervalBudget::budget_ratio() const {\n  if (max_bytes_in_budget_ == 0)\n    return 0.0;\n  return static_cast&lt;double&gt;(bytes_remaining_) \/ max_bytes_in_budget_;\n}\n<\/code><\/pre>\n<h1>AlrDetector<\/h1>\n<p>\u5e94\u7528\u7a0b\u5e8f\u53d7\u9650\u533a\u57df\u68c0\u6d4b\u5668 AlrDetector \u662f\u4e00\u4e2a\u7c7b\uff0c\u5b83\u5229\u7528\u7ecf\u8fc7\u7684\u65f6\u95f4\u548c\u53d1\u9001\u7684\u5b57\u8282\u7684\u4fe1\u53f7\u6765\u4f30\u8ba1\u5f53\u524d\u7f51\u7edc\u6d41\u91cf\u662f\u5426\u53d7\u5230\u5e94\u7528\u7a0b\u5e8f\u751f\u6210\u6d41\u91cf\u7684\u80fd\u529b\u7684\u9650\u5236\u3002<\/p>\n<p>AlrDetector\u63d0\u4f9b\u4e86\u4e00\u4e2a\u4fe1\u53f7\uff0c\u53ef\u7528\u4e8e\u8c03\u6574\u4f30\u8ba1\u5e26\u5bbd\u3002<br \/>\n\u6ce8\u610f\uff1a\u8fd9\u4e2a\u7c7b\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/p>\n<pre><code class=\"language-cpp\">\/\/ Application limited region detector is a class that utilizes signals of\n\/\/ elapsed time and bytes sent to estimate whether network traffic is\n\/\/ currently limited by the application&#039;s ability to generate traffic.\n\/\/\n\/\/ AlrDetector provides a signal that can be utilized to adjust\n\/\/ estimate bandwidth.\n\/\/ Note: This class is not thread-safe.\nclass AlrDetector {\n public:\n  AlrDetector(AlrDetectorConfig config, RtcEventLog* event_log);\n  explicit AlrDetector(const WebRtcKeyValueConfig* key_value_config);\n  AlrDetector(const WebRtcKeyValueConfig* key_value_config,\n              RtcEventLog* event_log);\n  ~AlrDetector();\n\n  void OnBytesSent(size_t bytes_sent, int64_t send_time_ms);\n\n  \/\/ Set current estimated bandwidth.\n  void SetEstimatedBitrate(int bitrate_bps);\n\n  \/\/ Returns time in milliseconds when the current application-limited region\n  \/\/ started or empty result if the sender is currently not application-limited.\n  absl::optional&lt;int64_t&gt; GetApplicationLimitedRegionStartTime() const;\n\n private:\n  friend class GoogCcStatePrinter;\n  const AlrDetectorConfig conf_;\n\n  absl::optional&lt;int64_t&gt; last_send_time_ms_;\n\n  IntervalBudget alr_budget_;\n  absl::optional&lt;int64_t&gt; alr_started_time_ms_;\n\n  RtcEventLog* event_log_;\n};\n<\/code><\/pre>\n<h2>AlrDetector::OnBytesSent<\/h2>\n<pre><code>\nvoid AlrDetector::OnBytesSent(size_t bytes_sent, int64_t send_time_ms) {\n  if (!last_send_time_ms_.has_value()) {\n    last_send_time_ms_ = send_time_ms;\n    \/\/ Since the duration for sending the bytes is unknwon, return without\n    \/\/ updating alr state.\n    return;\n  }\n  int64_t delta_time_ms = send_time_ms - *last_send_time_ms_;\n  last_send_time_ms_ = send_time_ms;\n\n  alr_budget_.UseBudget(bytes_sent);\n  alr_budget_.IncreaseBudget(delta_time_ms);\n  bool state_changed = false;\n  \/\/\u5982\u679c\u5269\u4f59\u5b57\u8282\u6bd4\u7387 \u5927\u4e8e start_budget_level_ratio, \u5e94\u7528\u7a0b\u5e8f\u53d7\u9650,\u6ca1\u6709\u5145\u5206\u5229\u7528\u5e26\u5bbd, \u5e94\u8be5\u542f\u52a8\u63a2\u6d4b,\u8bbe\u7f6e alr_started_time_ms_= \u5f53\u524d\u65f6\u95f4\n  if (alr_budget_.budget_ratio() &gt; conf_.start_budget_level_ratio &amp;&amp;\n      !alr_started_time_ms_) { \n    alr_started_time_ms_.emplace(rtc::TimeMillis());\n    state_changed = true;\n  } \/\/\u5982\u679c\u5269\u4f59\u5b57\u8282\u6bd4\u7387 \u5c0f\u4e8e stop_budget_level_ratio, \u5e26\u5bbd\u5229\u7528\u7684\u8fd8\u53ef\u4ee5, \u5e94\u8be5\u505c\u6b62\u63a2\u6d4b alr_started_time_ms_= 0\n  else if (alr_budget_.budget_ratio() &lt; conf_.stop_budget_level_ratio &amp;&amp;\n             alr_started_time_ms_) {\n    state_changed = true;\n    alr_started_time_ms_.reset();\n  }\n  if (event_log_ &amp;&amp; state_changed) {\n    event_log_-&gt;Log(\n        std::make_unique&lt;RtcEventAlrState&gt;(alr_started_time_ms_.has_value()));\n  }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Overview ALR\uff08Application limited region detector\uff09\u7684\u57fa\u672c\u539f\u7406\u5c31\u662f SentBitRate\/EstimatedBitRate \u7684\u767e\u5206\u6bd4\u4e0e kAlrStartUsagePercent\uff0850\uff09\u505a\u6bd4\u8f83\uff0c\u5f53\u5c0f\u4e8e\u8be5\u503c\u8ba4\u4e3a\u7f51\u7edc\u53d7\u9650\uff0c\u9700\u8981\u542f\u52a8 probe \u91cd\u65b0\u63a2\u6d4b\u5e26\u5bbd\uff0c\u5f53\u5927\u4e8e kAlrEndUsagePercent\uff0880\uff09\uff0c\u8ba4\u4e3a\u7f51\u7edc\u6062\u590d\uff0c \u505c\u6b62\u8fdb\u884c\u542f\u52a8\u4e0b\u6b21 probe \u63a2\u6d4b AlrDetectorConfig \u53d1\u9001\u6d41\u91cf\u6bd4\u7387(sent_bitrate\/estimated_bitrate) \u4f5c\u4e3a\u7f51\u7edc\u5bb9\u91cf\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u786e\u5b9a\u5e94\u7528\u7a0b\u5e8f\u53d7\u9650\u533a\u57df ALR(Application-Limited Region)\u3002 ALR \u533a\u57df\u5728\u5e26\u5bbd\u5269\u4f59\u7387\u5927\u4e8e kAlrStartUsageRatio (\u9ed8\u8ba4\u4e3a0.8) \u65f6\u5f00\u59cb\uff0c\u5728\u5e26\u5bbd\u5269\u4f59\u7387\u5927\u4e8e kAlrEndUsageRatio (\u9ed8\u8ba4\u4e3a 0.5) \u65f6\u7ed3\u675f\u3002 \u6ce8\u610f\uff1a \u5e26\u5bbd\u5269\u4f59\u7387 = 1 &#8211; send_bitrate\/estimated_bitrate \u5728\u5bf9\u5e94\u7528\u6709\u9650\u533a\u57df\u7684 BW \u8c03\u6574\u8fdb\u884c\u6709\u6548\u5fae\u8c03\u4e4b\u524d\uff0c\u8fd9\u662f\u6709\u610f\u505a\u4e86\u4e00\u4e9b\u4fdd\u5b88\u7684\u3002 struct AlrDetectorConfig { \/\/ Sent traffic ratio as a function of network capacity used to determine \/\/ application-limited [&hellip;] <a class=\"read-more\" href=\"https:\/\/www.fanyamin.com\/wordpress\/?p=535\" title=\"Permanent Link to: WebRTC \u6e90\u7801\u9605\u8bfb\u7b14\u8bb0\u4e4b AlrDetector\">&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":[15],"tags":[],"class_list":["post-535","post","type-post","status-publish","format-standard","hentry","category-webrtc"],"_links":{"self":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/535"}],"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=535"}],"version-history":[{"count":8,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/535\/revisions"}],"predecessor-version":[{"id":680,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/535\/revisions\/680"}],"wp:attachment":[{"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fanyamin.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}