summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/sdk/android/src/java/org/webrtc/DynamicBitrateAdjuster.java
blob: 96a15bbfe1b5dd3c575fbed51af25688425ba359 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 *  Copyright 2017 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

package org.webrtc;

/**
 * BitrateAdjuster that tracks the bandwidth produced by an encoder and dynamically adjusts the
 * bitrate.  Used for hardware codecs that pay attention to framerate but still deviate from the
 * target bitrate by unacceptable margins.
 */
class DynamicBitrateAdjuster extends BaseBitrateAdjuster {
  // Change the bitrate at most once every three seconds.
  private static final double BITRATE_ADJUSTMENT_SEC = 3.0;
  // Maximum bitrate adjustment scale - no more than 4 times.
  private static final double BITRATE_ADJUSTMENT_MAX_SCALE = 4;
  // Amount of adjustment steps to reach maximum scale.
  private static final int BITRATE_ADJUSTMENT_STEPS = 20;

  private static final double BITS_PER_BYTE = 8.0;

  // How far the codec has deviated above (or below) the target bitrate (tracked in bytes).
  private double deviationBytes;
  private double timeSinceLastAdjustmentMs;
  private int bitrateAdjustmentScaleExp;

  @Override
  public void setTargets(int targetBitrateBps, double targetFramerateFps) {
    if (this.targetBitrateBps > 0 && targetBitrateBps < this.targetBitrateBps) {
      // Rescale the accumulator level if the accumulator max decreases
      deviationBytes = deviationBytes * targetBitrateBps / this.targetBitrateBps;
    }
    super.setTargets(targetBitrateBps, targetFramerateFps);
  }

  @Override
  public void reportEncodedFrame(int size) {
    if (targetFramerateFps == 0) {
      return;
    }

    // Accumulate the difference between actual and expected frame sizes.
    double expectedBytesPerFrame = (targetBitrateBps / BITS_PER_BYTE) / targetFramerateFps;
    deviationBytes += (size - expectedBytesPerFrame);
    timeSinceLastAdjustmentMs += 1000.0 / targetFramerateFps;

    // Adjust the bitrate when the encoder accumulates one second's worth of data in excess or
    // shortfall of the target.
    double deviationThresholdBytes = targetBitrateBps / BITS_PER_BYTE;

    // Cap the deviation, i.e., don't let it grow beyond some level to avoid using too old data for
    // bitrate adjustment.  This also prevents taking more than 3 "steps" in a given 3-second cycle.
    double deviationCap = BITRATE_ADJUSTMENT_SEC * deviationThresholdBytes;
    deviationBytes = Math.min(deviationBytes, deviationCap);
    deviationBytes = Math.max(deviationBytes, -deviationCap);

    // Do bitrate adjustment every 3 seconds if actual encoder bitrate deviates too much
    // from the target value.
    if (timeSinceLastAdjustmentMs <= 1000 * BITRATE_ADJUSTMENT_SEC) {
      return;
    }

    if (deviationBytes > deviationThresholdBytes) {
      // Encoder generates too high bitrate - need to reduce the scale.
      int bitrateAdjustmentInc = (int) (deviationBytes / deviationThresholdBytes + 0.5);
      bitrateAdjustmentScaleExp -= bitrateAdjustmentInc;
      // Don't let the adjustment scale drop below -BITRATE_ADJUSTMENT_STEPS.
      // This sets a minimum exponent of -1 (bitrateAdjustmentScaleExp / BITRATE_ADJUSTMENT_STEPS).
      bitrateAdjustmentScaleExp = Math.max(bitrateAdjustmentScaleExp, -BITRATE_ADJUSTMENT_STEPS);
      deviationBytes = deviationThresholdBytes;
    } else if (deviationBytes < -deviationThresholdBytes) {
      // Encoder generates too low bitrate - need to increase the scale.
      int bitrateAdjustmentInc = (int) (-deviationBytes / deviationThresholdBytes + 0.5);
      bitrateAdjustmentScaleExp += bitrateAdjustmentInc;
      // Don't let the adjustment scale exceed BITRATE_ADJUSTMENT_STEPS.
      // This sets a maximum exponent of 1 (bitrateAdjustmentScaleExp / BITRATE_ADJUSTMENT_STEPS).
      bitrateAdjustmentScaleExp = Math.min(bitrateAdjustmentScaleExp, BITRATE_ADJUSTMENT_STEPS);
      deviationBytes = -deviationThresholdBytes;
    }
    timeSinceLastAdjustmentMs = 0;
  }

  private double getBitrateAdjustmentScale() {
    return Math.pow(BITRATE_ADJUSTMENT_MAX_SCALE,
        (double) bitrateAdjustmentScaleExp / BITRATE_ADJUSTMENT_STEPS);
  }

  @Override
  public int getAdjustedBitrateBps() {
    return (int) (targetBitrateBps * getBitrateAdjustmentScale());
  }
}