The adaptive algorithm of bandwidth in WEBRTC

Source: Internet
Author: User

Transferred from: http://www.xuebuyuan.com/1248366.html

The bandwidth adaptive algorithm in WEBRTC is divided into two types:

1, the originator bandwidth control, the principle is the RTCP in the packet loss statistics to dynamically increase or decrease the bandwidth, in the reduction of bandwidth using the TFRC algorithm to increase the smoothness.

2, the receiver bandwidth estimation, the principle is and by the receipt of RTP data, the estimated bandwidth, with the Kalman filter, the transmission time and reception time of each frame is analyzed, so as to obtain the network bandwidth utilization, revise the estimated bandwidth.

The two algorithms complement each other, and the receiver sends the estimated bandwidth to the originator, which, in combination with the received bandwidth and packet loss rate, adjusts the transmitted bandwidth.

The following is a detailed analysis of two algorithms:

2, receiver-side bandwidth estimation algorithm analysis

Combined with document HTTP://TOOLS.IETF.ORG/HTML/DRAFT-ALVESTRAND-RTCWEB-CONGESTION-02 and source Webrtc/modules/remote_bitrate_ estimator/overuse_detector.cc for analysis

Bandwidth estimation Model: D (i) = DL (i)/C + W (i) d (i) two frame data transmission time difference, DL (i) two frame data size difference, C for network transmission capability, W (i) is our focus, it is mainly determined by three factors: transmission rate, network routing capability, and network transmission Transmission capacity. W (i) conforms to the Gaussian distribution, as a conclusion: when W (i) is increased, it consumes too much bandwidth (over-using), and when W (i) is reduced, consumes less bandwidth (under-using), and for 0 o'clock, the exact bandwidth is used. So, as long as we can calculate w (i), we can determine the current network usage, thereby increasing or reducing the rate of transmission.

Algorithm principle: That is application kalman-filters
Theta_hat (i) = [1/c_hat (i) m_hat (i)]^t//I The State of the time point is represented by C, M, and Theta_hat (i) is the estimated value at this time

Z (i) = d (i)-H_bar (i) ^t * Theta_hat (i-1)//d (i) is the test value, can be easily calculated, the following can be considered as D (i-1) estimates, so Z (i) is the deviation of D (i), that is, the residual

Theta_hat (i) = Theta_hat (i-1) + Z (i) * K_bar (i)///OK, this is the result we want, the key is the selection of K value, the following two formula is to take the K value, the concrete derivation see the subsequent blog post.

E (i-1) * H_bar (i)
K_bar (i) =--------------------------------------------
Var_v_hat + h_bar (i) ^t * E (i-1) * H_bar (i)

E (i) = (I-k_bar (i) * H_bar (i) ^t) * E (i-1) + Q (i)//h_bar (i) calculated from the packet size of the frame

Thus, we only need to know the current frame length, send time, receive time and the state of the previous frame, you can calculate the network usage.

Next, take a look at the code:

[CPP]View
Plaincopy

  1. void Overusedetector::updatekalman (int64_t T_delta,
  2. Double Ts_delta,
  3. uint32_t Frame_size,
  4. uint32_t prev_frame_size) {
  5. Const Double min_frame_period = Updateminframeperiod (Ts_delta);
  6. const Double drift = Currentdrift ();
  7. //Compensate for Drift
  8. Const Double t_ts_delta = t_delta-ts_delta/drift; //i.e. d (i)
  9. double Fs_delta = static_cast<double> (frame_size)-prev_frame_size;
  10. //Update the Kalman filter
  11. Const Double scale_factor = min_frame_period/(1000.0/30.0);
  12. E_[0][0] + = process_noise_[0] * scale_factor;
  13. E_[1][1] + = process_noise_[1] * scale_factor;
  14. if (Hypothesis_ = = kbwoverusing && offset_ < prev_offset_) | |
  15. (Hypothesis_ = = kbwunderusing && offset_ > Prev_offset_)) {
  16. E_[1][1] + = ten * process_noise_[1] * scale_factor;
  17. }
  18. const Double h[2] = {Fs_delta, 1.0}; //IE H_bar
  19. const Double eh[2] = {E_[0][0]*h[0] + e_[0][1]*h[1],
  20. E_[1][0]*h[0] + e_[1][1]*h[1]};
  21. const Double residual = t_ts_delta-slope_*h[0]-offset_; //That is Z (i), slope is 1/c
  22. Const BOOL stable_state =
  23. (Bwe_min (Num_of_deltas_) * FABSF (offset_) < threshold_);
  24. //We try to filter out very late frames. For instance periodic key
  25. //Frames doesn ' t fit the Gaussian model well.
  26. if (FABSF (residual) < 3 * sqrt (var_noise_)) {
  27. Updatenoiseestimate (residual, min_frame_period, stable_state);
  28. } Else {
  29. Updatenoiseestimate (3 * sqrt (var_noise_), Min_frame_period, stable_state);
  30. }
  31. Const Double denom = var_noise_ + h[0]*eh[0] + h[1]*eh[1];
  32. const Double k[2] = {Eh[0]/denom,
  33. EH[1]/denom}; //IE K_bar
  34. const Double ikh[2][2] = {{1.0-k[0]*h[0],-k[0]*h[1]},
  35. {-k[1]*h[0], 1.0-k[1]*h[1]}};
  36. Const Double e00 = e_[0][0];
  37. Const Double e01 = e_[0][1];
  38. //Update State
  39. E_[0][0] = e00 * Ikh[0][0] + e_[1][0] * ikh[0][1];
  40. E_[0][1] = E01 * Ikh[0][0] + e_[1][1] * ikh[0][1];
  41. E_[1][0] = e00 * Ikh[1][0] + e_[1][0] * ikh[1][1];
  42. E_[1][1] = E01 * Ikh[1][0] + e_[1][1] * ikh[1][1];
  43. //covariance matrix, must be positive semi-definite
  44. ASSERT (E_[0][0] + e_[1][1] >= 0 &&
  45. E_[0][0] * e_[1][1]-e_[0][1] * E_[1][0] >= 0 &&
  46. E_[0][0] >= 0);
  47. Slope_ = Slope_ + k[0] * residual; //1/c
  48. prev_offset_ = offset_;
  49. offset_ = offset_ + k[1] * residual; //theta_hat (i)
  50. Detect (Ts_delta);
  51. }

[CPP]View
Plaincopy

  1. Bandwidthusage overusedetector::D etect (double ts_delta) {
  2. if (Num_of_deltas_ < 2) {
  3. return kbwnormal;
  4. }
  5. Const Double T = bwe_min (num_of_deltas_) * offset_; //IE gamma_1
  6. if (FABSF (T) > Threshold_) {
  7. if (offset_ > 0) {
  8. if (time_over_using_ = =-1) {
  9. //Initialize the timer. Assume that we ' ve been
  10. //over-using half of the time since the previous
  11. //Sample.
  12. Time_over_using_ = TS_DELTA/2;
  13. } Else {
  14. //Increment timer
  15. Time_over_using_ + = Ts_delta;
  16. }
  17. over_use_counter_++;
  18. if (Time_over_using_ > Koverusingtimethreshold //koverusingtimethreshold is gamma_2, gamama_3=1
  19. && over_use_counter_ > 1) {
  20. if (offset_ >= prev_offset_) {
  21. Time_over_using_ = 0;
  22. Over_use_counter_ = 0;
  23. Hypothesis_ = kbwoverusing;
  24. }
  25. }
  26. } Else {
  27. Time_over_using_ =-1;
  28. Over_use_counter_ = 0;
  29. Hypothesis_ = kbwunderusing;
  30. }
  31. } Else {
  32. Time_over_using_ =-1;
  33. Over_use_counter_ = 0;
  34. Hypothesis_ = Kbwnormal;
  35. }
  36. return hypothesis_;
  37. }



Reference Documentation:

      1. Http://www.swarthmore.edu/NatSci/echeeve1/Ref/Kalman/ScalarKalman.html
      2. http://tools.ietf.org/html/draft-alvestrand-rtcweb-congestion-02

The adaptive algorithm of bandwidth in WEBRTC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.