public class

ExponentialWeightedAverageStatistic

extends java.lang.Object

implements BandwidthStatistic

 java.lang.Object

↳androidx.media3.exoplayer.upstream.experimental.ExponentialWeightedAverageStatistic

Gradle dependencies

compile group: 'androidx.media3', name: 'media3-exoplayer', version: '1.5.0-alpha01'

  • groupId: androidx.media3
  • artifactId: media3-exoplayer
  • version: 1.5.0-alpha01

Artifact androidx.media3:media3-exoplayer:1.5.0-alpha01 it located at Google repository (https://maven.google.com/)

Overview

A BandwidthStatistic that calculates estimates using an exponential weighted average.

Summary

Fields
public static final doubleDEFAULT_SMOOTHING_FACTOR

The default smoothing factor.

Constructors
publicExponentialWeightedAverageStatistic()

Creates an instance with ExponentialWeightedAverageStatistic.DEFAULT_SMOOTHING_FACTOR.

publicExponentialWeightedAverageStatistic(double smoothingFactor)

Creates an instance.

Methods
public voidaddSample(long bytes, long durationUs)

public longgetBandwidthEstimate()

public voidreset()

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Fields

public static final double DEFAULT_SMOOTHING_FACTOR

The default smoothing factor.

Constructors

public ExponentialWeightedAverageStatistic()

Creates an instance with ExponentialWeightedAverageStatistic.DEFAULT_SMOOTHING_FACTOR.

public ExponentialWeightedAverageStatistic(double smoothingFactor)

Creates an instance.

Parameters:

smoothingFactor: The exponential smoothing factor.

Methods

public void addSample(long bytes, long durationUs)

public long getBandwidthEstimate()

public void reset()

Source

/*
 * Copyright 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package androidx.media3.exoplayer.upstream.experimental;

import static androidx.media3.exoplayer.upstream.experimental.BandwidthEstimator.ESTIMATE_NOT_AVAILABLE;

import androidx.media3.common.util.UnstableApi;

/** A {@link BandwidthStatistic} that calculates estimates using an exponential weighted average. */
@UnstableApi
public class ExponentialWeightedAverageStatistic implements BandwidthStatistic {

  /** The default smoothing factor. */
  public static final double DEFAULT_SMOOTHING_FACTOR = 0.9999;

  private final double smoothingFactor;

  private long bitrateEstimate;

  /** Creates an instance with {@link #DEFAULT_SMOOTHING_FACTOR}. */
  public ExponentialWeightedAverageStatistic() {
    this(DEFAULT_SMOOTHING_FACTOR);
  }

  /**
   * Creates an instance.
   *
   * @param smoothingFactor The exponential smoothing factor.
   */
  public ExponentialWeightedAverageStatistic(double smoothingFactor) {
    this.smoothingFactor = smoothingFactor;
    bitrateEstimate = ESTIMATE_NOT_AVAILABLE;
  }

  @Override
  public void addSample(long bytes, long durationUs) {
    long bitrate = bytes * 8_000_000 / durationUs;
    if (bitrateEstimate == ESTIMATE_NOT_AVAILABLE) {
      bitrateEstimate = bitrate;
      return;
    }
    // Weight smoothing factor by sqrt(bytes).
    double factor = Math.pow(smoothingFactor, Math.sqrt((double) bytes));
    bitrateEstimate = (long) (factor * bitrateEstimate + (1f - factor) * bitrate);
  }

  @Override
  public long getBandwidthEstimate() {
    return bitrateEstimate;
  }

  @Override
  public void reset() {
    bitrateEstimate = ESTIMATE_NOT_AVAILABLE;
  }
}