tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

MediaChannelStatistics.h (2670B)


      1 /* vim:set ts=2 sw=2 sts=2 et cindent: */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #if !defined(MediaChannelStatistics_h_)
      7 #  define MediaChannelStatistics_h_
      8 
      9 #  include "mozilla/TimeStamp.h"
     10 
     11 namespace mozilla {
     12 
     13 // Number of bytes we have accumulated before we assume the connection download
     14 // rate can be reliably calculated. 57 Segments at IW=3 allows slow start to
     15 // reach a CWND of 30 (See bug 831998)
     16 static const int64_t RELIABLE_DATA_THRESHOLD = 57 * 1460;
     17 
     18 /**
     19 * This class is useful for estimating rates of data passing through
     20 * some channel. The idea is that activity on the channel "starts"
     21 * and "stops" over time. At certain times data passes through the
     22 * channel (usually while the channel is active; data passing through
     23 * an inactive channel is ignored). The GetRate() function computes
     24 * an estimate of the "current rate" of the channel, which is some
     25 * kind of average of the data passing through over the time the
     26 * channel is active.
     27 *
     28 * All methods take "now" as a parameter so the user of this class can
     29 * control the timeline used.
     30 */
     31 class MediaChannelStatistics {
     32 public:
     33  MediaChannelStatistics() = default;
     34  MediaChannelStatistics(const MediaChannelStatistics&) = default;
     35  MediaChannelStatistics& operator=(const MediaChannelStatistics&) = default;
     36 
     37  void Reset() {
     38    mLastStartTime = TimeStamp();
     39    mAccumulatedTime = TimeDuration(0);
     40    mAccumulatedBytes = 0;
     41    mIsStarted = false;
     42  }
     43  void Start() {
     44    if (mIsStarted) return;
     45    mLastStartTime = TimeStamp::Now();
     46    mIsStarted = true;
     47  }
     48  void Stop() {
     49    if (!mIsStarted) return;
     50    mAccumulatedTime += TimeStamp::Now() - mLastStartTime;
     51    mIsStarted = false;
     52  }
     53  void AddBytes(int64_t aBytes) {
     54    if (!mIsStarted) {
     55      // ignore this data, it may be related to seeking or some other
     56      // operation we don't care about
     57      return;
     58    }
     59    mAccumulatedBytes += aBytes;
     60  }
     61  double GetRate(bool* aReliable) const {
     62    TimeDuration time = mAccumulatedTime;
     63    if (mIsStarted) {
     64      time += TimeStamp::Now() - mLastStartTime;
     65    }
     66    double seconds = time.ToSeconds();
     67    *aReliable =
     68        (seconds >= 3.0) || (mAccumulatedBytes >= RELIABLE_DATA_THRESHOLD);
     69    if (seconds <= 0.0) return 0.0;
     70    return static_cast<double>(mAccumulatedBytes) / seconds;
     71  }
     72 
     73 private:
     74  int64_t mAccumulatedBytes = 0;
     75  TimeDuration mAccumulatedTime;
     76  TimeStamp mLastStartTime;
     77  bool mIsStarted = false;
     78 };
     79 
     80 }  // namespace mozilla
     81 
     82 #endif  // MediaChannelStatistics_h_