WebRtc 中RateStatistics類

RateStatistics類位于rtc_base 目錄下面,屬于WebRtc 基礎(chǔ)類之一睡毒。這個(gè)類主要功能是統(tǒng)計(jì)一段時(shí)間內(nèi)的帶寬的情況来惧。由于很多地方用到這個(gè)帶寬統(tǒng)計(jì)的功能,所以有很多地方調(diào)用到了RateStatistics演顾。

RateStatistics 主要提供了四個(gè)公有函數(shù):


  void Reset();

  void Update(size_t count, int64_t now_ms);

  absl::optional Rate(int64_t now_ms)const;

  bool SetWindowSize(int64_t window_size_ms, int64_t now_ms);


void RateStatistics::Reset() {

  accumulated_count_ = 0;

  num_samples_ = 0;

  oldest_time_ = - max_window_size_ms_;

  oldest_index_ = 0;

  current_window_size_ms_ = max_window_size_ms_;

  for(int64_t i =0; i < max_window_size_ms_; i++)

    buckets_[i] = Bucket();

}

Reset函數(shù)就是各種參數(shù)重置了


voidRateStatistics::Update(size_t count, int64_t now_ms) {

  if(now_ms < oldest_time_) {

    return;

  }

  EraseOld(now_ms);

  // First ever sample, reset window to start now.

  if (!IsInitialized())

    oldest_time_= now_ms;

  uint32_t now_offset =static_cast(now_ms -oldest_time_);

  RTC_DCHECK_LT(now_offset, max_window_size_ms_);

  uint32_t index =oldest_index_+ now_offset;

  if (index >= max_window_size_ms_)

    index -=max_window_size_ms_;

  buckets_[index].sum += count;

  ++buckets_[index].samples;

  accumulated_count_ += count;

  ++num_samples_;

}

Update函數(shù)實(shí)際上在進(jìn)行添加的操作供搀。在添加之前先刪除舊的數(shù)據(jù),刪除舊的數(shù)據(jù)EraseOld钠至,實(shí)際上是按照一定的規(guī)則刪除的葛虐,如果沒(méi)有達(dá)到條件是不會(huì)刪除任何數(shù)據(jù)。
用當(dāng)前的值棕洋,減去最早的記錄值得到一個(gè)偏移量挡闰,偏移量加上最早的序號(hào),得到的值就是桶(buckets)的索引序號(hào)掰盘。oldest_time_ 和oldest_index_在這個(gè)函數(shù)中只是使用摄悯,這兩個(gè)值都是在刪除舊數(shù)據(jù)和重置的時(shí)候進(jìn)行修改(第一次插入數(shù)據(jù)除外)。


absl::optional<uint32_t> RateStatistics::Rate(int64_t now_ms) const {


  const_cast(this)->EraseOld(now_ms);

  int64_t active_window_size = now_ms -oldest_time_+1;

  if(num_samples_==0|| active_window_size <=1||

      (num_samples_<=1&& active_window_size< current_window_size_ms_)) {

    return absl::nullopt;

  }

  float scale =scale_/ active_window_size;

  return static_cast(accumulated_count_* scale +0.5f);

}

這個(gè)函數(shù)就是獲取帶寬值了愧捕。這里有一個(gè)空值的返回奢驯,當(dāng)桶里面只有一個(gè)數(shù)據(jù)的時(shí)候,或者沒(méi)有達(dá)到最大的窗口大小直接返回一個(gè)空值次绘。其他情況才是對(duì)根據(jù)當(dāng)前的時(shí)間瘪阁,獲取一段時(shí)間內(nèi)(max_window_size_ms_)帶寬值。


void RateStatistics::EraseOld(int64_t  now_ms) {

  if (!IsInitialized())

    return;

  // New oldest time that is included in data set.

  int64_t new_oldest_time = now_ms - current_window_size_ms_+1;

  // New oldest time is older than the current one, no need to cull data.

  if(new_oldest_time <=oldest_time_)

    return;

  // Loop over buckets and remove too old data points.

  while(num_samples_>0&&oldest_time_< new_oldest_time) {

    const Bucket& oldest_bucket = buckets_[oldest_index_];

    RTC_DCHECK_GE(accumulated_count_, oldest_bucket.sum);

    RTC_DCHECK_GE(num_samples_, oldest_bucket.samples);

    accumulated_count_-= oldest_bucket.sum;

    num_samples_-= oldest_bucket.samples;

    buckets_[oldest_index_] = Bucket();

    if (++oldest_index_ >= max_window_size_ms_)

      oldest_index_ = 0;

    ++oldest_time_;

  }

  oldest_time_= new_oldest_time;

}

刪除舊數(shù)據(jù)的函數(shù)邮偎。最早的數(shù)據(jù)oldest_time_小于 now_ms - current_window_size_ms_+1的時(shí)候才會(huì)觸發(fā)管跺,這時(shí)候一般情況下num_samples_會(huì)大于0。然后循環(huán)刪除最久的數(shù)據(jù)禾进。


bool RateStatistics::SetWindowSize(int64_t window_size_ms, int64_t now_ms) {

  if(window_size_ms <=0 || window_size_ms >max_window_size_ms_)

    return false;

  current_window_size_ms_ = window_size_ms;

  EraseOld(now_ms);

  return true;

}

設(shè)置窗口大小的函數(shù)


bool RateStatistics::IsInitialized() const {

  return oldest_time_ != -max_window_size_ms_;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末豁跑,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子泻云,更是在濱河造成了極大的恐慌艇拍,老刑警劉巖狐蜕,帶你破解...
    沈念sama閱讀 217,907評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異卸夕,居然都是意外死亡层释,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)快集,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)贡羔,“玉大人,你說(shuō)我怎么就攤上這事个初≈瘟Γ” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,298評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵勃黍,是天一觀的道長(zhǎng)宵统。 經(jīng)常有香客問(wèn)我,道長(zhǎng)覆获,這世上最難降的妖魔是什么马澈? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,586評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮弄息,結(jié)果婚禮上痊班,老公的妹妹穿的比我還像新娘。我一直安慰自己摹量,他們只是感情好涤伐,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,633評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著缨称,像睡著了一般凝果。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上睦尽,一...
    開(kāi)封第一講書(shū)人閱讀 51,488評(píng)論 1 302
  • 那天器净,我揣著相機(jī)與錄音,去河邊找鬼当凡。 笑死山害,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的沿量。 我是一名探鬼主播浪慌,決...
    沈念sama閱讀 40,275評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼朴则!你這毒婦竟也來(lái)了权纤?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,176評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎妖碉,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體芥被,經(jīng)...
    沈念sama閱讀 45,619評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡欧宜,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,819評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了拴魄。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片冗茸。...
    茶點(diǎn)故事閱讀 39,932評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖匹中,靈堂內(nèi)的尸體忽然破棺而出夏漱,到底是詐尸還是另有隱情,我是刑警寧澤顶捷,帶...
    沈念sama閱讀 35,655評(píng)論 5 346
  • 正文 年R本政府宣布挂绰,位于F島的核電站,受9級(jí)特大地震影響服赎,放射性物質(zhì)發(fā)生泄漏葵蒂。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,265評(píng)論 3 329
  • 文/蒙蒙 一重虑、第九天 我趴在偏房一處隱蔽的房頂上張望践付。 院中可真熱鬧,春花似錦缺厉、人聲如沸永高。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,871評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)命爬。三九已至,卻和暖如春辐脖,著一層夾襖步出監(jiān)牢的瞬間遇骑,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,994評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工揖曾, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留落萎,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,095評(píng)論 3 370
  • 正文 我出身青樓炭剪,卻偏偏與公主長(zhǎng)得像练链,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子奴拦,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,884評(píng)論 2 354