Minor cleanup to BandwidthMeter/DefaultBandwidthMeter

- Improve variable naming
- In the edge case that bytes are transferred in a sample that
  has 0 elapsed time, carry the bytes forward into the next
  sample. If we don't do this then the estimate will be calculated
  as though those bytes were never transferred. In practice I
  suspect this very rarely occurs.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=218551208
This commit is contained in:
olly 2018-10-24 11:56:58 -07:00 committed by Oliver Woodman
parent 1b6801c091
commit 4fe14c7693
2 changed files with 18 additions and 15 deletions

View File

@ -29,30 +29,33 @@ public interface BandwidthMeter {
interface EventListener { interface EventListener {
/** /**
* Called periodically to indicate that bytes have been transferred. * Called periodically to indicate that bytes have been transferred or the estimated bitrate has
* changed.
* *
* <p>Note: The estimated bitrate is typically derived from more information than just {@code * <p>Note: The estimated bitrate is typically derived from more information than just {@code
* bytes} and {@code elapsedMs}. * bytes} and {@code elapsedMs}.
* *
* @param elapsedMs The time taken to transfer the bytes, in milliseconds. * @param elapsedMs The time taken to transfer {@code bytesTransferred}, in milliseconds. This
* @param bytes The number of bytes transferred. * is at most the elapsed time since the last callback, but may be less if there were
* @param bitrate The estimated bitrate in bits/sec. * periods during which data was not being transferred.
* @param bytesTransferred The number of bytes transferred since the last callback.
* @param bitrateEstimate The estimated bitrate in bits/sec.
*/ */
void onBandwidthSample(int elapsedMs, long bytes, long bitrate); void onBandwidthSample(int elapsedMs, long bytesTransferred, long bitrateEstimate);
} }
/** Returns the estimated bandwidth in bits/sec. */ /** Returns the estimated bitrate. */
long getBitrateEstimate(); long getBitrateEstimate();
/** /**
* Returns the {@link TransferListener} that this instance uses to gather bandwidth information * Returns the {@link TransferListener} that this instance uses to gather bandwidth information
* from data transfers. May be null, if no transfer listener is used. * from data transfers. May be null if the implementation does not listen to data transfers.
*/ */
@Nullable @Nullable
TransferListener getTransferListener(); TransferListener getTransferListener();
/** /**
* Adds an {@link EventListener} to be informed of bandwidth samples. * Adds an {@link EventListener}.
* *
* @param eventHandler A handler for events. * @param eventHandler A handler for events.
* @param eventListener A listener of events. * @param eventListener A listener of events.

View File

@ -309,16 +309,16 @@ public final class DefaultBandwidthMeter implements BandwidthMeter, TransferList
|| totalBytesTransferred >= BYTES_TRANSFERRED_FOR_ESTIMATE) { || totalBytesTransferred >= BYTES_TRANSFERRED_FOR_ESTIMATE) {
bitrateEstimate = (long) slidingPercentile.getPercentile(0.5f); bitrateEstimate = (long) slidingPercentile.getPercentile(0.5f);
} }
} notifyBandwidthSample(sampleElapsedTimeMs, sampleBytesTransferred, bitrateEstimate);
notifyBandwidthSample(sampleElapsedTimeMs, sampleBytesTransferred, bitrateEstimate);
if (--streamCount > 0) {
sampleStartTimeMs = nowMs; sampleStartTimeMs = nowMs;
} sampleBytesTransferred = 0;
sampleBytesTransferred = 0; } // Else any sample bytes transferred will be carried forward into the next sample.
streamCount--;
} }
private void notifyBandwidthSample(int elapsedMs, long bytes, long bitrate) { private void notifyBandwidthSample(int elapsedMs, long bytesTransferred, long bitrateEstimate) {
eventDispatcher.dispatch(listener -> listener.onBandwidthSample(elapsedMs, bytes, bitrate)); eventDispatcher.dispatch(
listener -> listener.onBandwidthSample(elapsedMs, bytesTransferred, bitrateEstimate));
} }
private long getInitialBitrateEstimateForNetworkType(@C.NetworkType int networkType) { private long getInitialBitrateEstimateForNetworkType(@C.NetworkType int networkType) {