Replace DefaultBandwidthMeter with CountryAndNetworkTypeBandwidthMeter.

This removes the experimental bandwidth meter and uses it as the new default.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=215404065
This commit is contained in:
tonihei 2018-10-02 09:41:59 -07:00 committed by Oliver Woodman
parent 34c286682e
commit 4386d5d466
4 changed files with 453 additions and 698 deletions

View File

@ -2,6 +2,8 @@
### dev-v2 (not yet released) ### ### dev-v2 (not yet released) ###
* Improve initial bandwidth meter estimates using the current country and
network type.
* Do not retry failed loads whose error is `FileNotFoundException`. * Do not retry failed loads whose error is `FileNotFoundException`.
* Add convenience methods `Player.next`, `Player.previous`, `Player.hasNext` * Add convenience methods `Player.next`, `Player.previous`, `Player.hasNext`
and `Player.hasPrevious` and `Player.hasPrevious`

View File

@ -1,592 +0,0 @@
/*
* Copyright (C) 2016 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 com.google.android.exoplayer2.upstream;
import android.content.Context;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.SparseArray;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Clock;
import com.google.android.exoplayer2.util.ClosedSource;
import com.google.android.exoplayer2.util.EventDispatcher;
import com.google.android.exoplayer2.util.SlidingPercentile;
import com.google.android.exoplayer2.util.Util;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* Estimates bandwidth by listening to data transfers.
*
* <p>The bandwidth estimate is calculated using a {@link SlidingPercentile} and is updated each
* time a transfer ends. The initial estimate is based on the current operator's network country
* code or the locale of the user, as well as the network connection type. This can be configured in
* the {@link Builder}.
*/
@ClosedSource(reason = "Needs testing")
public final class CountryAndNetworkTypeBandwidthMeter implements BandwidthMeter, TransferListener {
/**
* Country groups used to determine the default initial bitrate estimate. The group assignment for
* each country is an array of group indices for [Wifi, 2G, 3G, 4G].
*/
public static final Map<String, int[]> DEFAULT_INITIAL_BITRATE_COUNTRY_GROUPS =
createInitialBitrateCountryGroupAssignment();
/** Default initial Wifi bitrate estimate in bits per second. */
public static final long[] DEFAULT_INITIAL_BITRATE_ESTIMATES_WIFI =
new long[] {5_700_000, 3_400_000, 1_900_000, 1_000_000, 400_000};
/** Default initial 2G bitrate estimates in bits per second. */
public static final long[] DEFAULT_INITIAL_BITRATE_ESTIMATES_2G =
new long[] {169_000, 129_000, 114_000, 102_000, 87_000};
/** Default initial 3G bitrate estimates in bits per second. */
public static final long[] DEFAULT_INITIAL_BITRATE_ESTIMATES_3G =
new long[] {2_100_000, 1_300_000, 950_000, 700_000, 400_000};
/** Default initial 4G bitrate estimates in bits per second. */
public static final long[] DEFAULT_INITIAL_BITRATE_ESTIMATES_4G =
new long[] {6_900_000, 4_300_000, 2_700_000, 1_600_000, 450_000};
/**
* Default initial bitrate estimate used when the device is offline or the network type cannot be
* determined, in bits per second.
*/
public static final long DEFAULT_INITIAL_BITRATE_ESTIMATE = 1_000_000;
/** Default maximum weight for the sliding window. */
public static final int DEFAULT_SLIDING_WINDOW_MAX_WEIGHT = 2000;
/** Builder for a bandwidth meter. */
public static final class Builder {
private final @Nullable Context context;
private @Nullable Handler eventHandler;
private @Nullable EventListener eventListener;
private SparseArray<Long> initialBitrateEstimates;
private int slidingWindowMaxWeight;
private Clock clock;
/** @deprecated Use {@link #Builder(Context)} instead. */
@Deprecated
public Builder() {
this(/* context= */ null);
}
/**
* Creates a builder with default parameters and without listener.
*
* @param context A context.
*/
public Builder(@Nullable Context context) {
this.context = context == null ? null : context.getApplicationContext();
initialBitrateEstimates = getInitialBitrateEstimatesForCountry(Util.getCountryCode(context));
slidingWindowMaxWeight = DEFAULT_SLIDING_WINDOW_MAX_WEIGHT;
clock = Clock.DEFAULT;
}
/**
* Sets an event listener for new bandwidth estimates.
*
* @param eventHandler A handler for events.
* @param eventListener A listener of events.
* @return This builder.
* @throws IllegalArgumentException If the event handler or listener are null.
*/
public Builder setEventListener(Handler eventHandler, EventListener eventListener) {
Assertions.checkArgument(eventHandler != null && eventListener != null);
this.eventHandler = eventHandler;
this.eventListener = eventListener;
return this;
}
/**
* Sets the maximum weight for the sliding window.
*
* @param slidingWindowMaxWeight The maximum weight for the sliding window.
* @return This builder.
*/
public Builder setSlidingWindowMaxWeight(int slidingWindowMaxWeight) {
this.slidingWindowMaxWeight = slidingWindowMaxWeight;
return this;
}
/**
* Sets the initial bitrate estimate in bits per second that should be assumed when a bandwidth
* estimate is unavailable.
*
* @param initialBitrateEstimate The initial bitrate estimate in bits per second.
* @return This builder.
*/
public Builder setInitialBitrateEstimate(long initialBitrateEstimate) {
for (int i = 0; i < initialBitrateEstimates.size(); i++) {
initialBitrateEstimates.setValueAt(i, initialBitrateEstimate);
}
return this;
}
/**
* Sets the initial bitrate estimate in bits per second for a network type that should be
* assumed when a bandwidth estimate is unavailable and the current network connection is of the
* specified type.
*
* @param networkType The {@link C.NetworkType} this initial estimate is for.
* @param initialBitrateEstimate The initial bitrate estimate in bits per second.
* @return This builder.
*/
public Builder setInitialBitrateEstimate(
@C.NetworkType int networkType, long initialBitrateEstimate) {
initialBitrateEstimates.put(networkType, initialBitrateEstimate);
return this;
}
/**
* Sets the initial bitrate estimates to the default values of the specified country. The
* initial estimates are used when a bandwidth estimate is unavailable.
*
* @param countryCode The ISO 3166-1 alpha-2 country code of the country whose default bitrate
* estimates should be used.
* @return This builder.
*/
public Builder setInitialBitrateEstimate(String countryCode) {
initialBitrateEstimates =
getInitialBitrateEstimatesForCountry(Util.toUpperInvariant(countryCode));
return this;
}
/**
* Sets the clock used to estimate bandwidth from data transfers. Should only be set for testing
* purposes.
*
* @param clock The clock used to estimate bandwidth from data transfers.
* @return This builder.
*/
public Builder setClock(Clock clock) {
this.clock = clock;
return this;
}
/**
* Builds the bandwidth meter.
*
* @return A bandwidth meter with the configured properties.
*/
public CountryAndNetworkTypeBandwidthMeter build() {
Long initialBitrateEstimate = initialBitrateEstimates.get(Util.getNetworkType(context));
if (initialBitrateEstimate == null) {
initialBitrateEstimate = initialBitrateEstimates.get(C.NETWORK_TYPE_UNKNOWN);
}
CountryAndNetworkTypeBandwidthMeter bandwidthMeter =
new CountryAndNetworkTypeBandwidthMeter(
initialBitrateEstimate, slidingWindowMaxWeight, clock);
if (eventHandler != null && eventListener != null) {
bandwidthMeter.addEventListener(eventHandler, eventListener);
}
return bandwidthMeter;
}
private static SparseArray<Long> getInitialBitrateEstimatesForCountry(String countryCode) {
int[] groupIndices = getCountryGroupIndices(countryCode);
SparseArray<Long> result = new SparseArray<>(/* initialCapacity= */ 6);
result.append(C.NETWORK_TYPE_UNKNOWN, DEFAULT_INITIAL_BITRATE_ESTIMATE);
result.append(C.NETWORK_TYPE_WIFI, DEFAULT_INITIAL_BITRATE_ESTIMATES_WIFI[groupIndices[0]]);
result.append(C.NETWORK_TYPE_2G, DEFAULT_INITIAL_BITRATE_ESTIMATES_2G[groupIndices[1]]);
result.append(C.NETWORK_TYPE_3G, DEFAULT_INITIAL_BITRATE_ESTIMATES_3G[groupIndices[2]]);
result.append(C.NETWORK_TYPE_4G, DEFAULT_INITIAL_BITRATE_ESTIMATES_4G[groupIndices[3]]);
// Assume default Wifi bitrate for Ethernet to prevent using the slower fallback bitrate.
result.append(
C.NETWORK_TYPE_ETHERNET, DEFAULT_INITIAL_BITRATE_ESTIMATES_WIFI[groupIndices[0]]);
return result;
}
private static int[] getCountryGroupIndices(String countryCode) {
int[] groupIndices = DEFAULT_INITIAL_BITRATE_COUNTRY_GROUPS.get(countryCode);
// Assume median group if not found.
return groupIndices == null ? new int[] {2, 2, 2, 2} : groupIndices;
}
}
private static final int ELAPSED_MILLIS_FOR_ESTIMATE = 2000;
private static final int BYTES_TRANSFERRED_FOR_ESTIMATE = 512 * 1024;
private final EventDispatcher<EventListener> eventDispatcher;
private final SlidingPercentile slidingPercentile;
private final Clock clock;
private int streamCount;
private long sampleStartTimeMs;
private long sampleBytesTransferred;
private long totalElapsedTimeMs;
private long totalBytesTransferred;
private long bitrateEstimate;
/** Creates a bandwidth meter with default parameters. */
public CountryAndNetworkTypeBandwidthMeter() {
this(DEFAULT_INITIAL_BITRATE_ESTIMATE, DEFAULT_SLIDING_WINDOW_MAX_WEIGHT, Clock.DEFAULT);
}
/** @deprecated Use {@link Builder} instead. */
@Deprecated
public CountryAndNetworkTypeBandwidthMeter(Handler eventHandler, EventListener eventListener) {
this(DEFAULT_INITIAL_BITRATE_ESTIMATE, DEFAULT_SLIDING_WINDOW_MAX_WEIGHT, Clock.DEFAULT);
if (eventHandler != null && eventListener != null) {
addEventListener(eventHandler, eventListener);
}
}
/** @deprecated Use {@link Builder} instead. */
@Deprecated
public CountryAndNetworkTypeBandwidthMeter(
Handler eventHandler, EventListener eventListener, int maxWeight) {
this(DEFAULT_INITIAL_BITRATE_ESTIMATE, maxWeight, Clock.DEFAULT);
if (eventHandler != null && eventListener != null) {
addEventListener(eventHandler, eventListener);
}
}
private CountryAndNetworkTypeBandwidthMeter(
long initialBitrateEstimate, int maxWeight, Clock clock) {
this.eventDispatcher = new EventDispatcher<>();
this.slidingPercentile = new SlidingPercentile(maxWeight);
this.clock = clock;
bitrateEstimate = initialBitrateEstimate;
}
@Override
public synchronized long getBitrateEstimate() {
return bitrateEstimate;
}
@Override
public @Nullable TransferListener getTransferListener() {
return this;
}
@Override
public void addEventListener(Handler eventHandler, EventListener eventListener) {
eventDispatcher.addListener(eventHandler, eventListener);
}
@Override
public void removeEventListener(EventListener eventListener) {
eventDispatcher.removeListener(eventListener);
}
@Override
public void onTransferInitializing(DataSource source, DataSpec dataSpec, boolean isNetwork) {
// Do nothing.
}
@Override
public synchronized void onTransferStart(
DataSource source, DataSpec dataSpec, boolean isNetwork) {
if (!isNetwork) {
return;
}
if (streamCount == 0) {
sampleStartTimeMs = clock.elapsedRealtime();
}
streamCount++;
}
@Override
public synchronized void onBytesTransferred(
DataSource source, DataSpec dataSpec, boolean isNetwork, int bytes) {
if (!isNetwork) {
return;
}
sampleBytesTransferred += bytes;
}
@Override
public synchronized void onTransferEnd(DataSource source, DataSpec dataSpec, boolean isNetwork) {
if (!isNetwork) {
return;
}
Assertions.checkState(streamCount > 0);
long nowMs = clock.elapsedRealtime();
int sampleElapsedTimeMs = (int) (nowMs - sampleStartTimeMs);
totalElapsedTimeMs += sampleElapsedTimeMs;
totalBytesTransferred += sampleBytesTransferred;
if (sampleElapsedTimeMs > 0) {
float bitsPerSecond = (sampleBytesTransferred * 8000) / sampleElapsedTimeMs;
slidingPercentile.addSample((int) Math.sqrt(sampleBytesTransferred), bitsPerSecond);
if (totalElapsedTimeMs >= ELAPSED_MILLIS_FOR_ESTIMATE
|| totalBytesTransferred >= BYTES_TRANSFERRED_FOR_ESTIMATE) {
bitrateEstimate = (long) slidingPercentile.getPercentile(0.5f);
}
}
notifyBandwidthSample(sampleElapsedTimeMs, sampleBytesTransferred, bitrateEstimate);
if (--streamCount > 0) {
sampleStartTimeMs = nowMs;
}
sampleBytesTransferred = 0;
}
private void notifyBandwidthSample(int elapsedMs, long bytes, long bitrate) {
eventDispatcher.dispatch(listener -> listener.onBandwidthSample(elapsedMs, bytes, bitrate));
}
private static Map<String, int[]> createInitialBitrateCountryGroupAssignment() {
HashMap<String, int[]> countryGroupAssignment = new HashMap<>();
countryGroupAssignment.put("AD", new int[] {1, 0, 0, 0});
countryGroupAssignment.put("AE", new int[] {1, 3, 4, 4});
countryGroupAssignment.put("AF", new int[] {4, 4, 3, 2});
countryGroupAssignment.put("AG", new int[] {3, 2, 1, 2});
countryGroupAssignment.put("AI", new int[] {1, 0, 0, 2});
countryGroupAssignment.put("AL", new int[] {1, 1, 1, 1});
countryGroupAssignment.put("AM", new int[] {2, 2, 4, 3});
countryGroupAssignment.put("AO", new int[] {2, 4, 2, 0});
countryGroupAssignment.put("AR", new int[] {2, 3, 2, 3});
countryGroupAssignment.put("AS", new int[] {3, 4, 4, 1});
countryGroupAssignment.put("AT", new int[] {0, 1, 0, 0});
countryGroupAssignment.put("AU", new int[] {0, 3, 0, 0});
countryGroupAssignment.put("AW", new int[] {1, 1, 0, 4});
countryGroupAssignment.put("AX", new int[] {0, 1, 0, 0});
countryGroupAssignment.put("AZ", new int[] {3, 3, 2, 2});
countryGroupAssignment.put("BA", new int[] {1, 1, 1, 2});
countryGroupAssignment.put("BB", new int[] {0, 1, 0, 0});
countryGroupAssignment.put("BD", new int[] {2, 1, 3, 2});
countryGroupAssignment.put("BE", new int[] {0, 0, 0, 0});
countryGroupAssignment.put("BF", new int[] {4, 4, 4, 1});
countryGroupAssignment.put("BG", new int[] {0, 0, 0, 1});
countryGroupAssignment.put("BH", new int[] {2, 1, 3, 4});
countryGroupAssignment.put("BI", new int[] {4, 3, 4, 4});
countryGroupAssignment.put("BJ", new int[] {4, 3, 4, 3});
countryGroupAssignment.put("BL", new int[] {1, 0, 1, 2});
countryGroupAssignment.put("BM", new int[] {1, 0, 0, 0});
countryGroupAssignment.put("BN", new int[] {4, 3, 3, 3});
countryGroupAssignment.put("BO", new int[] {2, 2, 1, 2});
countryGroupAssignment.put("BQ", new int[] {1, 1, 2, 4});
countryGroupAssignment.put("BR", new int[] {2, 3, 2, 2});
countryGroupAssignment.put("BS", new int[] {1, 1, 0, 2});
countryGroupAssignment.put("BT", new int[] {3, 0, 2, 1});
countryGroupAssignment.put("BW", new int[] {4, 4, 2, 3});
countryGroupAssignment.put("BY", new int[] {1, 1, 1, 1});
countryGroupAssignment.put("BZ", new int[] {2, 3, 3, 1});
countryGroupAssignment.put("CA", new int[] {0, 2, 2, 3});
countryGroupAssignment.put("CD", new int[] {4, 4, 2, 1});
countryGroupAssignment.put("CF", new int[] {4, 4, 3, 3});
countryGroupAssignment.put("CG", new int[] {4, 4, 4, 4});
countryGroupAssignment.put("CH", new int[] {0, 0, 0, 0});
countryGroupAssignment.put("CI", new int[] {4, 4, 4, 4});
countryGroupAssignment.put("CK", new int[] {2, 4, 2, 0});
countryGroupAssignment.put("CL", new int[] {2, 2, 2, 3});
countryGroupAssignment.put("CM", new int[] {3, 4, 3, 1});
countryGroupAssignment.put("CN", new int[] {2, 0, 1, 2});
countryGroupAssignment.put("CO", new int[] {2, 3, 2, 1});
countryGroupAssignment.put("CR", new int[] {2, 2, 4, 4});
countryGroupAssignment.put("CU", new int[] {4, 4, 4, 1});
countryGroupAssignment.put("CV", new int[] {2, 2, 2, 4});
countryGroupAssignment.put("CW", new int[] {1, 1, 0, 0});
countryGroupAssignment.put("CX", new int[] {1, 2, 2, 2});
countryGroupAssignment.put("CY", new int[] {1, 1, 0, 0});
countryGroupAssignment.put("CZ", new int[] {0, 1, 0, 0});
countryGroupAssignment.put("DE", new int[] {0, 2, 2, 2});
countryGroupAssignment.put("DJ", new int[] {3, 4, 4, 0});
countryGroupAssignment.put("DK", new int[] {0, 0, 0, 0});
countryGroupAssignment.put("DM", new int[] {2, 0, 3, 4});
countryGroupAssignment.put("DO", new int[] {3, 3, 4, 4});
countryGroupAssignment.put("DZ", new int[] {3, 3, 4, 4});
countryGroupAssignment.put("EC", new int[] {2, 3, 3, 1});
countryGroupAssignment.put("EE", new int[] {0, 0, 0, 0});
countryGroupAssignment.put("EG", new int[] {3, 3, 1, 1});
countryGroupAssignment.put("EH", new int[] {2, 0, 2, 3});
countryGroupAssignment.put("ER", new int[] {4, 2, 2, 2});
countryGroupAssignment.put("ES", new int[] {0, 0, 1, 1});
countryGroupAssignment.put("ET", new int[] {4, 4, 4, 0});
countryGroupAssignment.put("FI", new int[] {0, 0, 1, 0});
countryGroupAssignment.put("FJ", new int[] {3, 2, 3, 3});
countryGroupAssignment.put("FK", new int[] {3, 4, 2, 1});
countryGroupAssignment.put("FM", new int[] {4, 2, 4, 0});
countryGroupAssignment.put("FO", new int[] {0, 0, 0, 1});
countryGroupAssignment.put("FR", new int[] {1, 0, 2, 1});
countryGroupAssignment.put("GA", new int[] {3, 3, 2, 1});
countryGroupAssignment.put("GB", new int[] {0, 1, 3, 2});
countryGroupAssignment.put("GD", new int[] {2, 0, 3, 0});
countryGroupAssignment.put("GE", new int[] {1, 1, 0, 3});
countryGroupAssignment.put("GF", new int[] {1, 2, 4, 4});
countryGroupAssignment.put("GG", new int[] {0, 1, 0, 0});
countryGroupAssignment.put("GH", new int[] {3, 2, 2, 2});
countryGroupAssignment.put("GI", new int[] {0, 0, 0, 1});
countryGroupAssignment.put("GL", new int[] {2, 4, 1, 4});
countryGroupAssignment.put("GM", new int[] {4, 3, 3, 0});
countryGroupAssignment.put("GN", new int[] {4, 4, 3, 4});
countryGroupAssignment.put("GP", new int[] {2, 2, 1, 3});
countryGroupAssignment.put("GQ", new int[] {4, 4, 3, 1});
countryGroupAssignment.put("GR", new int[] {1, 1, 0, 1});
countryGroupAssignment.put("GT", new int[] {3, 2, 3, 4});
countryGroupAssignment.put("GU", new int[] {1, 0, 4, 4});
countryGroupAssignment.put("GW", new int[] {4, 4, 4, 0});
countryGroupAssignment.put("GY", new int[] {3, 4, 1, 0});
countryGroupAssignment.put("HK", new int[] {0, 2, 3, 4});
countryGroupAssignment.put("HN", new int[] {3, 3, 2, 2});
countryGroupAssignment.put("HR", new int[] {1, 0, 0, 2});
countryGroupAssignment.put("HT", new int[] {3, 3, 3, 3});
countryGroupAssignment.put("HU", new int[] {0, 0, 1, 0});
countryGroupAssignment.put("ID", new int[] {2, 3, 3, 4});
countryGroupAssignment.put("IE", new int[] {0, 0, 1, 1});
countryGroupAssignment.put("IL", new int[] {0, 1, 1, 3});
countryGroupAssignment.put("IM", new int[] {0, 1, 0, 1});
countryGroupAssignment.put("IN", new int[] {2, 3, 3, 4});
countryGroupAssignment.put("IO", new int[] {4, 2, 2, 2});
countryGroupAssignment.put("IQ", new int[] {3, 3, 4, 3});
countryGroupAssignment.put("IR", new int[] {3, 2, 4, 4});
countryGroupAssignment.put("IS", new int[] {0, 0, 0, 0});
countryGroupAssignment.put("IT", new int[] {1, 0, 1, 3});
countryGroupAssignment.put("JE", new int[] {0, 0, 0, 1});
countryGroupAssignment.put("JM", new int[] {3, 3, 3, 2});
countryGroupAssignment.put("JO", new int[] {1, 1, 1, 2});
countryGroupAssignment.put("JP", new int[] {0, 1, 1, 2});
countryGroupAssignment.put("KE", new int[] {3, 3, 3, 3});
countryGroupAssignment.put("KG", new int[] {2, 2, 3, 3});
countryGroupAssignment.put("KH", new int[] {1, 0, 4, 4});
countryGroupAssignment.put("KI", new int[] {4, 4, 4, 4});
countryGroupAssignment.put("KM", new int[] {4, 4, 2, 2});
countryGroupAssignment.put("KN", new int[] {1, 0, 1, 3});
countryGroupAssignment.put("KP", new int[] {1, 2, 2, 2});
countryGroupAssignment.put("KR", new int[] {0, 4, 0, 2});
countryGroupAssignment.put("KW", new int[] {1, 2, 1, 2});
countryGroupAssignment.put("KY", new int[] {1, 1, 0, 2});
countryGroupAssignment.put("KZ", new int[] {1, 2, 2, 3});
countryGroupAssignment.put("LA", new int[] {3, 2, 2, 2});
countryGroupAssignment.put("LB", new int[] {3, 2, 0, 0});
countryGroupAssignment.put("LC", new int[] {2, 2, 1, 0});
countryGroupAssignment.put("LI", new int[] {0, 0, 1, 2});
countryGroupAssignment.put("LK", new int[] {1, 1, 2, 2});
countryGroupAssignment.put("LR", new int[] {3, 4, 3, 1});
countryGroupAssignment.put("LS", new int[] {3, 3, 2, 0});
countryGroupAssignment.put("LT", new int[] {0, 0, 0, 1});
countryGroupAssignment.put("LU", new int[] {0, 0, 1, 0});
countryGroupAssignment.put("LV", new int[] {0, 0, 0, 0});
countryGroupAssignment.put("LY", new int[] {4, 4, 4, 4});
countryGroupAssignment.put("MA", new int[] {2, 1, 2, 2});
countryGroupAssignment.put("MC", new int[] {1, 0, 1, 0});
countryGroupAssignment.put("MD", new int[] {1, 1, 0, 0});
countryGroupAssignment.put("ME", new int[] {1, 2, 2, 3});
countryGroupAssignment.put("MF", new int[] {1, 4, 3, 3});
countryGroupAssignment.put("MG", new int[] {3, 4, 1, 2});
countryGroupAssignment.put("MH", new int[] {4, 0, 2, 3});
countryGroupAssignment.put("MK", new int[] {1, 0, 0, 1});
countryGroupAssignment.put("ML", new int[] {4, 4, 4, 4});
countryGroupAssignment.put("MM", new int[] {2, 3, 1, 2});
countryGroupAssignment.put("MN", new int[] {2, 2, 2, 4});
countryGroupAssignment.put("MO", new int[] {0, 1, 4, 4});
countryGroupAssignment.put("MP", new int[] {0, 0, 4, 4});
countryGroupAssignment.put("MQ", new int[] {1, 1, 1, 3});
countryGroupAssignment.put("MR", new int[] {4, 2, 4, 2});
countryGroupAssignment.put("MS", new int[] {1, 2, 1, 2});
countryGroupAssignment.put("MT", new int[] {0, 0, 0, 0});
countryGroupAssignment.put("MU", new int[] {2, 2, 4, 4});
countryGroupAssignment.put("MV", new int[] {4, 2, 0, 1});
countryGroupAssignment.put("MW", new int[] {3, 2, 1, 1});
countryGroupAssignment.put("MX", new int[] {2, 4, 3, 1});
countryGroupAssignment.put("MY", new int[] {2, 3, 3, 3});
countryGroupAssignment.put("MZ", new int[] {3, 3, 2, 4});
countryGroupAssignment.put("NA", new int[] {4, 2, 1, 1});
countryGroupAssignment.put("NC", new int[] {2, 1, 3, 3});
countryGroupAssignment.put("NE", new int[] {4, 4, 4, 4});
countryGroupAssignment.put("NF", new int[] {0, 2, 2, 2});
countryGroupAssignment.put("NG", new int[] {3, 4, 2, 2});
countryGroupAssignment.put("NI", new int[] {3, 4, 3, 3});
countryGroupAssignment.put("NL", new int[] {0, 1, 3, 2});
countryGroupAssignment.put("NO", new int[] {0, 0, 1, 0});
countryGroupAssignment.put("NP", new int[] {2, 3, 2, 2});
countryGroupAssignment.put("NR", new int[] {4, 3, 4, 1});
countryGroupAssignment.put("NU", new int[] {4, 2, 2, 2});
countryGroupAssignment.put("NZ", new int[] {0, 0, 0, 1});
countryGroupAssignment.put("OM", new int[] {2, 2, 1, 3});
countryGroupAssignment.put("PA", new int[] {1, 3, 2, 3});
countryGroupAssignment.put("PE", new int[] {2, 2, 4, 4});
countryGroupAssignment.put("PF", new int[] {2, 2, 0, 1});
countryGroupAssignment.put("PG", new int[] {4, 4, 4, 4});
countryGroupAssignment.put("PH", new int[] {3, 0, 4, 4});
countryGroupAssignment.put("PK", new int[] {3, 3, 3, 3});
countryGroupAssignment.put("PL", new int[] {1, 0, 1, 3});
countryGroupAssignment.put("PM", new int[] {0, 2, 2, 3});
countryGroupAssignment.put("PR", new int[] {2, 3, 4, 3});
countryGroupAssignment.put("PS", new int[] {2, 3, 0, 4});
countryGroupAssignment.put("PT", new int[] {1, 1, 1, 1});
countryGroupAssignment.put("PW", new int[] {3, 2, 3, 0});
countryGroupAssignment.put("PY", new int[] {2, 1, 3, 3});
countryGroupAssignment.put("QA", new int[] {2, 3, 1, 2});
countryGroupAssignment.put("RE", new int[] {1, 1, 2, 2});
countryGroupAssignment.put("RO", new int[] {0, 1, 1, 3});
countryGroupAssignment.put("RS", new int[] {1, 1, 0, 0});
countryGroupAssignment.put("RU", new int[] {0, 1, 1, 1});
countryGroupAssignment.put("RW", new int[] {3, 4, 3, 1});
countryGroupAssignment.put("SA", new int[] {3, 2, 2, 3});
countryGroupAssignment.put("SB", new int[] {4, 4, 3, 0});
countryGroupAssignment.put("SC", new int[] {4, 2, 0, 1});
countryGroupAssignment.put("SD", new int[] {3, 4, 4, 4});
countryGroupAssignment.put("SE", new int[] {0, 0, 0, 0});
countryGroupAssignment.put("SG", new int[] {1, 2, 3, 3});
countryGroupAssignment.put("SH", new int[] {4, 2, 2, 2});
countryGroupAssignment.put("SI", new int[] {0, 1, 0, 0});
countryGroupAssignment.put("SJ", new int[] {3, 2, 0, 2});
countryGroupAssignment.put("SK", new int[] {0, 1, 0, 1});
countryGroupAssignment.put("SL", new int[] {4, 3, 2, 4});
countryGroupAssignment.put("SM", new int[] {1, 0, 1, 1});
countryGroupAssignment.put("SN", new int[] {4, 4, 4, 2});
countryGroupAssignment.put("SO", new int[] {4, 4, 4, 3});
countryGroupAssignment.put("SR", new int[] {3, 2, 2, 3});
countryGroupAssignment.put("SS", new int[] {4, 3, 4, 2});
countryGroupAssignment.put("ST", new int[] {3, 2, 2, 2});
countryGroupAssignment.put("SV", new int[] {2, 3, 2, 3});
countryGroupAssignment.put("SX", new int[] {2, 4, 2, 0});
countryGroupAssignment.put("SY", new int[] {4, 4, 2, 0});
countryGroupAssignment.put("SZ", new int[] {3, 4, 1, 1});
countryGroupAssignment.put("TC", new int[] {2, 1, 2, 1});
countryGroupAssignment.put("TD", new int[] {4, 4, 4, 3});
countryGroupAssignment.put("TG", new int[] {3, 2, 2, 0});
countryGroupAssignment.put("TH", new int[] {1, 3, 4, 4});
countryGroupAssignment.put("TJ", new int[] {4, 4, 4, 4});
countryGroupAssignment.put("TL", new int[] {4, 2, 4, 4});
countryGroupAssignment.put("TM", new int[] {4, 1, 3, 3});
countryGroupAssignment.put("TN", new int[] {2, 2, 1, 2});
countryGroupAssignment.put("TO", new int[] {2, 3, 3, 1});
countryGroupAssignment.put("TR", new int[] {1, 2, 0, 2});
countryGroupAssignment.put("TT", new int[] {2, 1, 1, 0});
countryGroupAssignment.put("TV", new int[] {4, 2, 2, 4});
countryGroupAssignment.put("TW", new int[] {0, 0, 0, 1});
countryGroupAssignment.put("TZ", new int[] {3, 3, 3, 2});
countryGroupAssignment.put("UA", new int[] {0, 2, 1, 3});
countryGroupAssignment.put("UG", new int[] {4, 3, 2, 2});
countryGroupAssignment.put("US", new int[] {0, 1, 3, 3});
countryGroupAssignment.put("UY", new int[] {2, 1, 2, 2});
countryGroupAssignment.put("UZ", new int[] {4, 3, 2, 4});
countryGroupAssignment.put("VA", new int[] {1, 2, 2, 2});
countryGroupAssignment.put("VC", new int[] {2, 0, 3, 2});
countryGroupAssignment.put("VE", new int[] {3, 4, 4, 3});
countryGroupAssignment.put("VG", new int[] {3, 1, 3, 4});
countryGroupAssignment.put("VI", new int[] {1, 0, 2, 4});
countryGroupAssignment.put("VN", new int[] {0, 2, 4, 4});
countryGroupAssignment.put("VU", new int[] {4, 1, 3, 2});
countryGroupAssignment.put("WS", new int[] {3, 2, 3, 0});
countryGroupAssignment.put("XK", new int[] {1, 2, 1, 0});
countryGroupAssignment.put("YE", new int[] {4, 4, 4, 2});
countryGroupAssignment.put("YT", new int[] {3, 1, 1, 2});
countryGroupAssignment.put("ZA", new int[] {2, 3, 1, 2});
countryGroupAssignment.put("ZM", new int[] {3, 3, 3, 1});
countryGroupAssignment.put("ZW", new int[] {3, 3, 2, 1});
return Collections.unmodifiableMap(countryGroupAssignment);
}
}

View File

@ -15,20 +15,57 @@
*/ */
package com.google.android.exoplayer2.upstream; package com.google.android.exoplayer2.upstream;
import android.content.Context;
import android.os.Handler; import android.os.Handler;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.util.SparseArray;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Clock; import com.google.android.exoplayer2.util.Clock;
import com.google.android.exoplayer2.util.EventDispatcher; import com.google.android.exoplayer2.util.EventDispatcher;
import com.google.android.exoplayer2.util.SlidingPercentile; import com.google.android.exoplayer2.util.SlidingPercentile;
import com.google.android.exoplayer2.util.Util;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/** /**
* Estimates bandwidth by listening to data transfers. The bandwidth estimate is calculated using a * Estimates bandwidth by listening to data transfers.
* {@link SlidingPercentile} and is updated each time a transfer ends. *
* <p>The bandwidth estimate is calculated using a {@link SlidingPercentile} and is updated each
* time a transfer ends. The initial estimate is based on the current operator's network country
* code or the locale of the user, as well as the network connection type. This can be configured in
* the {@link Builder}.
*/ */
public final class DefaultBandwidthMeter implements BandwidthMeter, TransferListener { public final class DefaultBandwidthMeter implements BandwidthMeter, TransferListener {
/** Default initial bitrate estimate in bits per second. */ /**
* Country groups used to determine the default initial bitrate estimate. The group assignment for
* each country is an array of group indices for [Wifi, 2G, 3G, 4G].
*/
public static final Map<String, int[]> DEFAULT_INITIAL_BITRATE_COUNTRY_GROUPS =
createInitialBitrateCountryGroupAssignment();
/** Default initial Wifi bitrate estimate in bits per second. */
public static final long[] DEFAULT_INITIAL_BITRATE_ESTIMATES_WIFI =
new long[] {5_700_000, 3_400_000, 1_900_000, 1_000_000, 400_000};
/** Default initial 2G bitrate estimates in bits per second. */
public static final long[] DEFAULT_INITIAL_BITRATE_ESTIMATES_2G =
new long[] {169_000, 129_000, 114_000, 102_000, 87_000};
/** Default initial 3G bitrate estimates in bits per second. */
public static final long[] DEFAULT_INITIAL_BITRATE_ESTIMATES_3G =
new long[] {2_100_000, 1_300_000, 950_000, 700_000, 400_000};
/** Default initial 4G bitrate estimates in bits per second. */
public static final long[] DEFAULT_INITIAL_BITRATE_ESTIMATES_4G =
new long[] {6_900_000, 4_300_000, 2_700_000, 1_600_000, 450_000};
/**
* Default initial bitrate estimate used when the device is offline or the network type cannot be
* determined, in bits per second.
*/
public static final long DEFAULT_INITIAL_BITRATE_ESTIMATE = 1_000_000; public static final long DEFAULT_INITIAL_BITRATE_ESTIMATE = 1_000_000;
/** Default maximum weight for the sliding window. */ /** Default maximum weight for the sliding window. */
@ -37,15 +74,28 @@ public final class DefaultBandwidthMeter implements BandwidthMeter, TransferList
/** Builder for a bandwidth meter. */ /** Builder for a bandwidth meter. */
public static final class Builder { public static final class Builder {
private @Nullable Handler eventHandler; @Nullable private final Context context;
private @Nullable EventListener eventListener;
private long initialBitrateEstimate; @Nullable private Handler eventHandler;
@Nullable private EventListener eventListener;
private SparseArray<Long> initialBitrateEstimates;
private int slidingWindowMaxWeight; private int slidingWindowMaxWeight;
private Clock clock; private Clock clock;
/** Creates a builder with default parameters and without listener. */ /** @deprecated Use {@link #Builder(Context)} instead. */
@Deprecated
public Builder() { public Builder() {
initialBitrateEstimate = DEFAULT_INITIAL_BITRATE_ESTIMATE; this(/* context= */ null);
}
/**
* Creates a builder with default parameters and without listener.
*
* @param context A context.
*/
public Builder(@Nullable Context context) {
this.context = context == null ? null : context.getApplicationContext();
initialBitrateEstimates = getInitialBitrateEstimatesForCountry(Util.getCountryCode(context));
slidingWindowMaxWeight = DEFAULT_SLIDING_WINDOW_MAX_WEIGHT; slidingWindowMaxWeight = DEFAULT_SLIDING_WINDOW_MAX_WEIGHT;
clock = Clock.DEFAULT; clock = Clock.DEFAULT;
} }
@ -84,7 +134,38 @@ public final class DefaultBandwidthMeter implements BandwidthMeter, TransferList
* @return This builder. * @return This builder.
*/ */
public Builder setInitialBitrateEstimate(long initialBitrateEstimate) { public Builder setInitialBitrateEstimate(long initialBitrateEstimate) {
this.initialBitrateEstimate = initialBitrateEstimate; for (int i = 0; i < initialBitrateEstimates.size(); i++) {
initialBitrateEstimates.setValueAt(i, initialBitrateEstimate);
}
return this;
}
/**
* Sets the initial bitrate estimate in bits per second for a network type that should be
* assumed when a bandwidth estimate is unavailable and the current network connection is of the
* specified type.
*
* @param networkType The {@link C.NetworkType} this initial estimate is for.
* @param initialBitrateEstimate The initial bitrate estimate in bits per second.
* @return This builder.
*/
public Builder setInitialBitrateEstimate(
@C.NetworkType int networkType, long initialBitrateEstimate) {
initialBitrateEstimates.put(networkType, initialBitrateEstimate);
return this;
}
/**
* Sets the initial bitrate estimates to the default values of the specified country. The
* initial estimates are used when a bandwidth estimate is unavailable.
*
* @param countryCode The ISO 3166-1 alpha-2 country code of the country whose default bitrate
* estimates should be used.
* @return This builder.
*/
public Builder setInitialBitrateEstimate(String countryCode) {
initialBitrateEstimates =
getInitialBitrateEstimatesForCountry(Util.toUpperInvariant(countryCode));
return this; return this;
} }
@ -106,6 +187,10 @@ public final class DefaultBandwidthMeter implements BandwidthMeter, TransferList
* @return A bandwidth meter with the configured properties. * @return A bandwidth meter with the configured properties.
*/ */
public DefaultBandwidthMeter build() { public DefaultBandwidthMeter build() {
Long initialBitrateEstimate = initialBitrateEstimates.get(Util.getNetworkType(context));
if (initialBitrateEstimate == null) {
initialBitrateEstimate = initialBitrateEstimates.get(C.NETWORK_TYPE_UNKNOWN);
}
DefaultBandwidthMeter bandwidthMeter = DefaultBandwidthMeter bandwidthMeter =
new DefaultBandwidthMeter(initialBitrateEstimate, slidingWindowMaxWeight, clock); new DefaultBandwidthMeter(initialBitrateEstimate, slidingWindowMaxWeight, clock);
if (eventHandler != null && eventListener != null) { if (eventHandler != null && eventListener != null) {
@ -113,6 +198,26 @@ public final class DefaultBandwidthMeter implements BandwidthMeter, TransferList
} }
return bandwidthMeter; return bandwidthMeter;
} }
private static SparseArray<Long> getInitialBitrateEstimatesForCountry(String countryCode) {
int[] groupIndices = getCountryGroupIndices(countryCode);
SparseArray<Long> result = new SparseArray<>(/* initialCapacity= */ 6);
result.append(C.NETWORK_TYPE_UNKNOWN, DEFAULT_INITIAL_BITRATE_ESTIMATE);
result.append(C.NETWORK_TYPE_WIFI, DEFAULT_INITIAL_BITRATE_ESTIMATES_WIFI[groupIndices[0]]);
result.append(C.NETWORK_TYPE_2G, DEFAULT_INITIAL_BITRATE_ESTIMATES_2G[groupIndices[1]]);
result.append(C.NETWORK_TYPE_3G, DEFAULT_INITIAL_BITRATE_ESTIMATES_3G[groupIndices[2]]);
result.append(C.NETWORK_TYPE_4G, DEFAULT_INITIAL_BITRATE_ESTIMATES_4G[groupIndices[3]]);
// Assume default Wifi bitrate for Ethernet to prevent using the slower fallback bitrate.
result.append(
C.NETWORK_TYPE_ETHERNET, DEFAULT_INITIAL_BITRATE_ESTIMATES_WIFI[groupIndices[0]]);
return result;
}
private static int[] getCountryGroupIndices(String countryCode) {
int[] groupIndices = DEFAULT_INITIAL_BITRATE_COUNTRY_GROUPS.get(countryCode);
// Assume median group if not found.
return groupIndices == null ? new int[] {2, 2, 2, 2} : groupIndices;
}
} }
private static final int ELAPSED_MILLIS_FOR_ESTIMATE = 2000; private static final int ELAPSED_MILLIS_FOR_ESTIMATE = 2000;
@ -153,10 +258,7 @@ public final class DefaultBandwidthMeter implements BandwidthMeter, TransferList
} }
} }
private DefaultBandwidthMeter( private DefaultBandwidthMeter(long initialBitrateEstimate, int maxWeight, Clock clock) {
long initialBitrateEstimate,
int maxWeight,
Clock clock) {
this.eventDispatcher = new EventDispatcher<>(); this.eventDispatcher = new EventDispatcher<>();
this.slidingPercentile = new SlidingPercentile(maxWeight); this.slidingPercentile = new SlidingPercentile(maxWeight);
this.clock = clock; this.clock = clock;
@ -169,7 +271,8 @@ public final class DefaultBandwidthMeter implements BandwidthMeter, TransferList
} }
@Override @Override
public @Nullable TransferListener getTransferListener() { @Nullable
public TransferListener getTransferListener() {
return this; return this;
} }
@ -237,4 +340,249 @@ public final class DefaultBandwidthMeter implements BandwidthMeter, TransferList
private void notifyBandwidthSample(int elapsedMs, long bytes, long bitrate) { private void notifyBandwidthSample(int elapsedMs, long bytes, long bitrate) {
eventDispatcher.dispatch(listener -> listener.onBandwidthSample(elapsedMs, bytes, bitrate)); eventDispatcher.dispatch(listener -> listener.onBandwidthSample(elapsedMs, bytes, bitrate));
} }
private static Map<String, int[]> createInitialBitrateCountryGroupAssignment() {
HashMap<String, int[]> countryGroupAssignment = new HashMap<>();
countryGroupAssignment.put("AD", new int[] {1, 0, 0, 0});
countryGroupAssignment.put("AE", new int[] {1, 3, 4, 4});
countryGroupAssignment.put("AF", new int[] {4, 4, 3, 2});
countryGroupAssignment.put("AG", new int[] {3, 2, 1, 2});
countryGroupAssignment.put("AI", new int[] {1, 0, 0, 2});
countryGroupAssignment.put("AL", new int[] {1, 1, 1, 1});
countryGroupAssignment.put("AM", new int[] {2, 2, 4, 3});
countryGroupAssignment.put("AO", new int[] {2, 4, 2, 0});
countryGroupAssignment.put("AR", new int[] {2, 3, 2, 3});
countryGroupAssignment.put("AS", new int[] {3, 4, 4, 1});
countryGroupAssignment.put("AT", new int[] {0, 1, 0, 0});
countryGroupAssignment.put("AU", new int[] {0, 3, 0, 0});
countryGroupAssignment.put("AW", new int[] {1, 1, 0, 4});
countryGroupAssignment.put("AX", new int[] {0, 1, 0, 0});
countryGroupAssignment.put("AZ", new int[] {3, 3, 2, 2});
countryGroupAssignment.put("BA", new int[] {1, 1, 1, 2});
countryGroupAssignment.put("BB", new int[] {0, 1, 0, 0});
countryGroupAssignment.put("BD", new int[] {2, 1, 3, 2});
countryGroupAssignment.put("BE", new int[] {0, 0, 0, 0});
countryGroupAssignment.put("BF", new int[] {4, 4, 4, 1});
countryGroupAssignment.put("BG", new int[] {0, 0, 0, 1});
countryGroupAssignment.put("BH", new int[] {2, 1, 3, 4});
countryGroupAssignment.put("BI", new int[] {4, 3, 4, 4});
countryGroupAssignment.put("BJ", new int[] {4, 3, 4, 3});
countryGroupAssignment.put("BL", new int[] {1, 0, 1, 2});
countryGroupAssignment.put("BM", new int[] {1, 0, 0, 0});
countryGroupAssignment.put("BN", new int[] {4, 3, 3, 3});
countryGroupAssignment.put("BO", new int[] {2, 2, 1, 2});
countryGroupAssignment.put("BQ", new int[] {1, 1, 2, 4});
countryGroupAssignment.put("BR", new int[] {2, 3, 2, 2});
countryGroupAssignment.put("BS", new int[] {1, 1, 0, 2});
countryGroupAssignment.put("BT", new int[] {3, 0, 2, 1});
countryGroupAssignment.put("BW", new int[] {4, 4, 2, 3});
countryGroupAssignment.put("BY", new int[] {1, 1, 1, 1});
countryGroupAssignment.put("BZ", new int[] {2, 3, 3, 1});
countryGroupAssignment.put("CA", new int[] {0, 2, 2, 3});
countryGroupAssignment.put("CD", new int[] {4, 4, 2, 1});
countryGroupAssignment.put("CF", new int[] {4, 4, 3, 3});
countryGroupAssignment.put("CG", new int[] {4, 4, 4, 4});
countryGroupAssignment.put("CH", new int[] {0, 0, 0, 0});
countryGroupAssignment.put("CI", new int[] {4, 4, 4, 4});
countryGroupAssignment.put("CK", new int[] {2, 4, 2, 0});
countryGroupAssignment.put("CL", new int[] {2, 2, 2, 3});
countryGroupAssignment.put("CM", new int[] {3, 4, 3, 1});
countryGroupAssignment.put("CN", new int[] {2, 0, 1, 2});
countryGroupAssignment.put("CO", new int[] {2, 3, 2, 1});
countryGroupAssignment.put("CR", new int[] {2, 2, 4, 4});
countryGroupAssignment.put("CU", new int[] {4, 4, 4, 1});
countryGroupAssignment.put("CV", new int[] {2, 2, 2, 4});
countryGroupAssignment.put("CW", new int[] {1, 1, 0, 0});
countryGroupAssignment.put("CX", new int[] {1, 2, 2, 2});
countryGroupAssignment.put("CY", new int[] {1, 1, 0, 0});
countryGroupAssignment.put("CZ", new int[] {0, 1, 0, 0});
countryGroupAssignment.put("DE", new int[] {0, 2, 2, 2});
countryGroupAssignment.put("DJ", new int[] {3, 4, 4, 0});
countryGroupAssignment.put("DK", new int[] {0, 0, 0, 0});
countryGroupAssignment.put("DM", new int[] {2, 0, 3, 4});
countryGroupAssignment.put("DO", new int[] {3, 3, 4, 4});
countryGroupAssignment.put("DZ", new int[] {3, 3, 4, 4});
countryGroupAssignment.put("EC", new int[] {2, 3, 3, 1});
countryGroupAssignment.put("EE", new int[] {0, 0, 0, 0});
countryGroupAssignment.put("EG", new int[] {3, 3, 1, 1});
countryGroupAssignment.put("EH", new int[] {2, 0, 2, 3});
countryGroupAssignment.put("ER", new int[] {4, 2, 2, 2});
countryGroupAssignment.put("ES", new int[] {0, 0, 1, 1});
countryGroupAssignment.put("ET", new int[] {4, 4, 4, 0});
countryGroupAssignment.put("FI", new int[] {0, 0, 1, 0});
countryGroupAssignment.put("FJ", new int[] {3, 2, 3, 3});
countryGroupAssignment.put("FK", new int[] {3, 4, 2, 1});
countryGroupAssignment.put("FM", new int[] {4, 2, 4, 0});
countryGroupAssignment.put("FO", new int[] {0, 0, 0, 1});
countryGroupAssignment.put("FR", new int[] {1, 0, 2, 1});
countryGroupAssignment.put("GA", new int[] {3, 3, 2, 1});
countryGroupAssignment.put("GB", new int[] {0, 1, 3, 2});
countryGroupAssignment.put("GD", new int[] {2, 0, 3, 0});
countryGroupAssignment.put("GE", new int[] {1, 1, 0, 3});
countryGroupAssignment.put("GF", new int[] {1, 2, 4, 4});
countryGroupAssignment.put("GG", new int[] {0, 1, 0, 0});
countryGroupAssignment.put("GH", new int[] {3, 2, 2, 2});
countryGroupAssignment.put("GI", new int[] {0, 0, 0, 1});
countryGroupAssignment.put("GL", new int[] {2, 4, 1, 4});
countryGroupAssignment.put("GM", new int[] {4, 3, 3, 0});
countryGroupAssignment.put("GN", new int[] {4, 4, 3, 4});
countryGroupAssignment.put("GP", new int[] {2, 2, 1, 3});
countryGroupAssignment.put("GQ", new int[] {4, 4, 3, 1});
countryGroupAssignment.put("GR", new int[] {1, 1, 0, 1});
countryGroupAssignment.put("GT", new int[] {3, 2, 3, 4});
countryGroupAssignment.put("GU", new int[] {1, 0, 4, 4});
countryGroupAssignment.put("GW", new int[] {4, 4, 4, 0});
countryGroupAssignment.put("GY", new int[] {3, 4, 1, 0});
countryGroupAssignment.put("HK", new int[] {0, 2, 3, 4});
countryGroupAssignment.put("HN", new int[] {3, 3, 2, 2});
countryGroupAssignment.put("HR", new int[] {1, 0, 0, 2});
countryGroupAssignment.put("HT", new int[] {3, 3, 3, 3});
countryGroupAssignment.put("HU", new int[] {0, 0, 1, 0});
countryGroupAssignment.put("ID", new int[] {2, 3, 3, 4});
countryGroupAssignment.put("IE", new int[] {0, 0, 1, 1});
countryGroupAssignment.put("IL", new int[] {0, 1, 1, 3});
countryGroupAssignment.put("IM", new int[] {0, 1, 0, 1});
countryGroupAssignment.put("IN", new int[] {2, 3, 3, 4});
countryGroupAssignment.put("IO", new int[] {4, 2, 2, 2});
countryGroupAssignment.put("IQ", new int[] {3, 3, 4, 3});
countryGroupAssignment.put("IR", new int[] {3, 2, 4, 4});
countryGroupAssignment.put("IS", new int[] {0, 0, 0, 0});
countryGroupAssignment.put("IT", new int[] {1, 0, 1, 3});
countryGroupAssignment.put("JE", new int[] {0, 0, 0, 1});
countryGroupAssignment.put("JM", new int[] {3, 3, 3, 2});
countryGroupAssignment.put("JO", new int[] {1, 1, 1, 2});
countryGroupAssignment.put("JP", new int[] {0, 1, 1, 2});
countryGroupAssignment.put("KE", new int[] {3, 3, 3, 3});
countryGroupAssignment.put("KG", new int[] {2, 2, 3, 3});
countryGroupAssignment.put("KH", new int[] {1, 0, 4, 4});
countryGroupAssignment.put("KI", new int[] {4, 4, 4, 4});
countryGroupAssignment.put("KM", new int[] {4, 4, 2, 2});
countryGroupAssignment.put("KN", new int[] {1, 0, 1, 3});
countryGroupAssignment.put("KP", new int[] {1, 2, 2, 2});
countryGroupAssignment.put("KR", new int[] {0, 4, 0, 2});
countryGroupAssignment.put("KW", new int[] {1, 2, 1, 2});
countryGroupAssignment.put("KY", new int[] {1, 1, 0, 2});
countryGroupAssignment.put("KZ", new int[] {1, 2, 2, 3});
countryGroupAssignment.put("LA", new int[] {3, 2, 2, 2});
countryGroupAssignment.put("LB", new int[] {3, 2, 0, 0});
countryGroupAssignment.put("LC", new int[] {2, 2, 1, 0});
countryGroupAssignment.put("LI", new int[] {0, 0, 1, 2});
countryGroupAssignment.put("LK", new int[] {1, 1, 2, 2});
countryGroupAssignment.put("LR", new int[] {3, 4, 3, 1});
countryGroupAssignment.put("LS", new int[] {3, 3, 2, 0});
countryGroupAssignment.put("LT", new int[] {0, 0, 0, 1});
countryGroupAssignment.put("LU", new int[] {0, 0, 1, 0});
countryGroupAssignment.put("LV", new int[] {0, 0, 0, 0});
countryGroupAssignment.put("LY", new int[] {4, 4, 4, 4});
countryGroupAssignment.put("MA", new int[] {2, 1, 2, 2});
countryGroupAssignment.put("MC", new int[] {1, 0, 1, 0});
countryGroupAssignment.put("MD", new int[] {1, 1, 0, 0});
countryGroupAssignment.put("ME", new int[] {1, 2, 2, 3});
countryGroupAssignment.put("MF", new int[] {1, 4, 3, 3});
countryGroupAssignment.put("MG", new int[] {3, 4, 1, 2});
countryGroupAssignment.put("MH", new int[] {4, 0, 2, 3});
countryGroupAssignment.put("MK", new int[] {1, 0, 0, 1});
countryGroupAssignment.put("ML", new int[] {4, 4, 4, 4});
countryGroupAssignment.put("MM", new int[] {2, 3, 1, 2});
countryGroupAssignment.put("MN", new int[] {2, 2, 2, 4});
countryGroupAssignment.put("MO", new int[] {0, 1, 4, 4});
countryGroupAssignment.put("MP", new int[] {0, 0, 4, 4});
countryGroupAssignment.put("MQ", new int[] {1, 1, 1, 3});
countryGroupAssignment.put("MR", new int[] {4, 2, 4, 2});
countryGroupAssignment.put("MS", new int[] {1, 2, 1, 2});
countryGroupAssignment.put("MT", new int[] {0, 0, 0, 0});
countryGroupAssignment.put("MU", new int[] {2, 2, 4, 4});
countryGroupAssignment.put("MV", new int[] {4, 2, 0, 1});
countryGroupAssignment.put("MW", new int[] {3, 2, 1, 1});
countryGroupAssignment.put("MX", new int[] {2, 4, 3, 1});
countryGroupAssignment.put("MY", new int[] {2, 3, 3, 3});
countryGroupAssignment.put("MZ", new int[] {3, 3, 2, 4});
countryGroupAssignment.put("NA", new int[] {4, 2, 1, 1});
countryGroupAssignment.put("NC", new int[] {2, 1, 3, 3});
countryGroupAssignment.put("NE", new int[] {4, 4, 4, 4});
countryGroupAssignment.put("NF", new int[] {0, 2, 2, 2});
countryGroupAssignment.put("NG", new int[] {3, 4, 2, 2});
countryGroupAssignment.put("NI", new int[] {3, 4, 3, 3});
countryGroupAssignment.put("NL", new int[] {0, 1, 3, 2});
countryGroupAssignment.put("NO", new int[] {0, 0, 1, 0});
countryGroupAssignment.put("NP", new int[] {2, 3, 2, 2});
countryGroupAssignment.put("NR", new int[] {4, 3, 4, 1});
countryGroupAssignment.put("NU", new int[] {4, 2, 2, 2});
countryGroupAssignment.put("NZ", new int[] {0, 0, 0, 1});
countryGroupAssignment.put("OM", new int[] {2, 2, 1, 3});
countryGroupAssignment.put("PA", new int[] {1, 3, 2, 3});
countryGroupAssignment.put("PE", new int[] {2, 2, 4, 4});
countryGroupAssignment.put("PF", new int[] {2, 2, 0, 1});
countryGroupAssignment.put("PG", new int[] {4, 4, 4, 4});
countryGroupAssignment.put("PH", new int[] {3, 0, 4, 4});
countryGroupAssignment.put("PK", new int[] {3, 3, 3, 3});
countryGroupAssignment.put("PL", new int[] {1, 0, 1, 3});
countryGroupAssignment.put("PM", new int[] {0, 2, 2, 3});
countryGroupAssignment.put("PR", new int[] {2, 3, 4, 3});
countryGroupAssignment.put("PS", new int[] {2, 3, 0, 4});
countryGroupAssignment.put("PT", new int[] {1, 1, 1, 1});
countryGroupAssignment.put("PW", new int[] {3, 2, 3, 0});
countryGroupAssignment.put("PY", new int[] {2, 1, 3, 3});
countryGroupAssignment.put("QA", new int[] {2, 3, 1, 2});
countryGroupAssignment.put("RE", new int[] {1, 1, 2, 2});
countryGroupAssignment.put("RO", new int[] {0, 1, 1, 3});
countryGroupAssignment.put("RS", new int[] {1, 1, 0, 0});
countryGroupAssignment.put("RU", new int[] {0, 1, 1, 1});
countryGroupAssignment.put("RW", new int[] {3, 4, 3, 1});
countryGroupAssignment.put("SA", new int[] {3, 2, 2, 3});
countryGroupAssignment.put("SB", new int[] {4, 4, 3, 0});
countryGroupAssignment.put("SC", new int[] {4, 2, 0, 1});
countryGroupAssignment.put("SD", new int[] {3, 4, 4, 4});
countryGroupAssignment.put("SE", new int[] {0, 0, 0, 0});
countryGroupAssignment.put("SG", new int[] {1, 2, 3, 3});
countryGroupAssignment.put("SH", new int[] {4, 2, 2, 2});
countryGroupAssignment.put("SI", new int[] {0, 1, 0, 0});
countryGroupAssignment.put("SJ", new int[] {3, 2, 0, 2});
countryGroupAssignment.put("SK", new int[] {0, 1, 0, 1});
countryGroupAssignment.put("SL", new int[] {4, 3, 2, 4});
countryGroupAssignment.put("SM", new int[] {1, 0, 1, 1});
countryGroupAssignment.put("SN", new int[] {4, 4, 4, 2});
countryGroupAssignment.put("SO", new int[] {4, 4, 4, 3});
countryGroupAssignment.put("SR", new int[] {3, 2, 2, 3});
countryGroupAssignment.put("SS", new int[] {4, 3, 4, 2});
countryGroupAssignment.put("ST", new int[] {3, 2, 2, 2});
countryGroupAssignment.put("SV", new int[] {2, 3, 2, 3});
countryGroupAssignment.put("SX", new int[] {2, 4, 2, 0});
countryGroupAssignment.put("SY", new int[] {4, 4, 2, 0});
countryGroupAssignment.put("SZ", new int[] {3, 4, 1, 1});
countryGroupAssignment.put("TC", new int[] {2, 1, 2, 1});
countryGroupAssignment.put("TD", new int[] {4, 4, 4, 3});
countryGroupAssignment.put("TG", new int[] {3, 2, 2, 0});
countryGroupAssignment.put("TH", new int[] {1, 3, 4, 4});
countryGroupAssignment.put("TJ", new int[] {4, 4, 4, 4});
countryGroupAssignment.put("TL", new int[] {4, 2, 4, 4});
countryGroupAssignment.put("TM", new int[] {4, 1, 3, 3});
countryGroupAssignment.put("TN", new int[] {2, 2, 1, 2});
countryGroupAssignment.put("TO", new int[] {2, 3, 3, 1});
countryGroupAssignment.put("TR", new int[] {1, 2, 0, 2});
countryGroupAssignment.put("TT", new int[] {2, 1, 1, 0});
countryGroupAssignment.put("TV", new int[] {4, 2, 2, 4});
countryGroupAssignment.put("TW", new int[] {0, 0, 0, 1});
countryGroupAssignment.put("TZ", new int[] {3, 3, 3, 2});
countryGroupAssignment.put("UA", new int[] {0, 2, 1, 3});
countryGroupAssignment.put("UG", new int[] {4, 3, 2, 2});
countryGroupAssignment.put("US", new int[] {0, 1, 3, 3});
countryGroupAssignment.put("UY", new int[] {2, 1, 2, 2});
countryGroupAssignment.put("UZ", new int[] {4, 3, 2, 4});
countryGroupAssignment.put("VA", new int[] {1, 2, 2, 2});
countryGroupAssignment.put("VC", new int[] {2, 0, 3, 2});
countryGroupAssignment.put("VE", new int[] {3, 4, 4, 3});
countryGroupAssignment.put("VG", new int[] {3, 1, 3, 4});
countryGroupAssignment.put("VI", new int[] {1, 0, 2, 4});
countryGroupAssignment.put("VN", new int[] {0, 2, 4, 4});
countryGroupAssignment.put("VU", new int[] {4, 1, 3, 2});
countryGroupAssignment.put("WS", new int[] {3, 2, 3, 0});
countryGroupAssignment.put("XK", new int[] {1, 2, 1, 0});
countryGroupAssignment.put("YE", new int[] {4, 4, 4, 2});
countryGroupAssignment.put("YT", new int[] {3, 1, 1, 2});
countryGroupAssignment.put("ZA", new int[] {2, 3, 1, 2});
countryGroupAssignment.put("ZM", new int[] {3, 3, 3, 1});
countryGroupAssignment.put("ZW", new int[] {3, 3, 2, 1});
return Collections.unmodifiableMap(countryGroupAssignment);
}
} }

View File

@ -24,7 +24,6 @@ import android.net.NetworkInfo;
import android.net.NetworkInfo.DetailedState; import android.net.NetworkInfo.DetailedState;
import android.telephony.TelephonyManager; import android.telephony.TelephonyManager;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.util.ClosedSource;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -33,10 +32,9 @@ import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows; import org.robolectric.Shadows;
import org.robolectric.shadows.ShadowNetworkInfo; import org.robolectric.shadows.ShadowNetworkInfo;
/** Unit test for {@link CountryAndNetworkTypeBandwidthMeter}. */ /** Unit test for {@link DefaultBandwidthMeter}. */
@ClosedSource(reason = "Not ready yet")
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public final class CountryAndNetworkTypeBandwidthMeterTest { public final class DefaultBandwidthMeterTest {
private static final String FAST_COUNTRY_ISO = "EE"; private static final String FAST_COUNTRY_ISO = "EE";
private static final String SLOW_COUNTRY_ISO = "PG"; private static final String SLOW_COUNTRY_ISO = "PG";
@ -106,13 +104,13 @@ public final class CountryAndNetworkTypeBandwidthMeterTest {
@Test @Test
public void defaultInitialBitrateEstimate_forWifi_isGreaterThanEstimateFor2G() { public void defaultInitialBitrateEstimate_forWifi_isGreaterThanEstimateFor2G() {
setActiveNetworkInfo(networkInfoWifi); setActiveNetworkInfo(networkInfoWifi);
CountryAndNetworkTypeBandwidthMeter bandwidthMeterWifi = DefaultBandwidthMeter bandwidthMeterWifi =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application).build(); new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application).build();
long initialEstimateWifi = bandwidthMeterWifi.getBitrateEstimate(); long initialEstimateWifi = bandwidthMeterWifi.getBitrateEstimate();
setActiveNetworkInfo(networkInfo2g); setActiveNetworkInfo(networkInfo2g);
CountryAndNetworkTypeBandwidthMeter bandwidthMeter2g = DefaultBandwidthMeter bandwidthMeter2g =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application).build(); new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application).build();
long initialEstimate2g = bandwidthMeter2g.getBitrateEstimate(); long initialEstimate2g = bandwidthMeter2g.getBitrateEstimate();
assertThat(initialEstimateWifi).isGreaterThan(initialEstimate2g); assertThat(initialEstimateWifi).isGreaterThan(initialEstimate2g);
@ -121,13 +119,13 @@ public final class CountryAndNetworkTypeBandwidthMeterTest {
@Test @Test
public void defaultInitialBitrateEstimate_forWifi_isGreaterThanEstimateFor3G() { public void defaultInitialBitrateEstimate_forWifi_isGreaterThanEstimateFor3G() {
setActiveNetworkInfo(networkInfoWifi); setActiveNetworkInfo(networkInfoWifi);
CountryAndNetworkTypeBandwidthMeter bandwidthMeterWifi = DefaultBandwidthMeter bandwidthMeterWifi =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application).build(); new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application).build();
long initialEstimateWifi = bandwidthMeterWifi.getBitrateEstimate(); long initialEstimateWifi = bandwidthMeterWifi.getBitrateEstimate();
setActiveNetworkInfo(networkInfo3g); setActiveNetworkInfo(networkInfo3g);
CountryAndNetworkTypeBandwidthMeter bandwidthMeter3g = DefaultBandwidthMeter bandwidthMeter3g =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application).build(); new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application).build();
long initialEstimate3g = bandwidthMeter3g.getBitrateEstimate(); long initialEstimate3g = bandwidthMeter3g.getBitrateEstimate();
assertThat(initialEstimateWifi).isGreaterThan(initialEstimate3g); assertThat(initialEstimateWifi).isGreaterThan(initialEstimate3g);
@ -136,13 +134,13 @@ public final class CountryAndNetworkTypeBandwidthMeterTest {
@Test @Test
public void defaultInitialBitrateEstimate_forEthernet_isGreaterThanEstimateFor2G() { public void defaultInitialBitrateEstimate_forEthernet_isGreaterThanEstimateFor2G() {
setActiveNetworkInfo(networkInfoEthernet); setActiveNetworkInfo(networkInfoEthernet);
CountryAndNetworkTypeBandwidthMeter bandwidthMeterEthernet = DefaultBandwidthMeter bandwidthMeterEthernet =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application).build(); new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application).build();
long initialEstimateEthernet = bandwidthMeterEthernet.getBitrateEstimate(); long initialEstimateEthernet = bandwidthMeterEthernet.getBitrateEstimate();
setActiveNetworkInfo(networkInfo2g); setActiveNetworkInfo(networkInfo2g);
CountryAndNetworkTypeBandwidthMeter bandwidthMeter2g = DefaultBandwidthMeter bandwidthMeter2g =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application).build(); new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application).build();
long initialEstimate2g = bandwidthMeter2g.getBitrateEstimate(); long initialEstimate2g = bandwidthMeter2g.getBitrateEstimate();
assertThat(initialEstimateEthernet).isGreaterThan(initialEstimate2g); assertThat(initialEstimateEthernet).isGreaterThan(initialEstimate2g);
@ -151,13 +149,13 @@ public final class CountryAndNetworkTypeBandwidthMeterTest {
@Test @Test
public void defaultInitialBitrateEstimate_forEthernet_isGreaterThanEstimateFor3G() { public void defaultInitialBitrateEstimate_forEthernet_isGreaterThanEstimateFor3G() {
setActiveNetworkInfo(networkInfoEthernet); setActiveNetworkInfo(networkInfoEthernet);
CountryAndNetworkTypeBandwidthMeter bandwidthMeterEthernet = DefaultBandwidthMeter bandwidthMeterEthernet =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application).build(); new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application).build();
long initialEstimateEthernet = bandwidthMeterEthernet.getBitrateEstimate(); long initialEstimateEthernet = bandwidthMeterEthernet.getBitrateEstimate();
setActiveNetworkInfo(networkInfo3g); setActiveNetworkInfo(networkInfo3g);
CountryAndNetworkTypeBandwidthMeter bandwidthMeter3g = DefaultBandwidthMeter bandwidthMeter3g =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application).build(); new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application).build();
long initialEstimate3g = bandwidthMeter3g.getBitrateEstimate(); long initialEstimate3g = bandwidthMeter3g.getBitrateEstimate();
assertThat(initialEstimateEthernet).isGreaterThan(initialEstimate3g); assertThat(initialEstimateEthernet).isGreaterThan(initialEstimate3g);
@ -166,13 +164,13 @@ public final class CountryAndNetworkTypeBandwidthMeterTest {
@Test @Test
public void defaultInitialBitrateEstimate_for4G_isGreaterThanEstimateFor2G() { public void defaultInitialBitrateEstimate_for4G_isGreaterThanEstimateFor2G() {
setActiveNetworkInfo(networkInfo4g); setActiveNetworkInfo(networkInfo4g);
CountryAndNetworkTypeBandwidthMeter bandwidthMeter4g = DefaultBandwidthMeter bandwidthMeter4g =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application).build(); new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application).build();
long initialEstimate4g = bandwidthMeter4g.getBitrateEstimate(); long initialEstimate4g = bandwidthMeter4g.getBitrateEstimate();
setActiveNetworkInfo(networkInfo2g); setActiveNetworkInfo(networkInfo2g);
CountryAndNetworkTypeBandwidthMeter bandwidthMeter2g = DefaultBandwidthMeter bandwidthMeter2g =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application).build(); new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application).build();
long initialEstimate2g = bandwidthMeter2g.getBitrateEstimate(); long initialEstimate2g = bandwidthMeter2g.getBitrateEstimate();
assertThat(initialEstimate4g).isGreaterThan(initialEstimate2g); assertThat(initialEstimate4g).isGreaterThan(initialEstimate2g);
@ -181,13 +179,13 @@ public final class CountryAndNetworkTypeBandwidthMeterTest {
@Test @Test
public void defaultInitialBitrateEstimate_for4G_isGreaterThanEstimateFor3G() { public void defaultInitialBitrateEstimate_for4G_isGreaterThanEstimateFor3G() {
setActiveNetworkInfo(networkInfo4g); setActiveNetworkInfo(networkInfo4g);
CountryAndNetworkTypeBandwidthMeter bandwidthMeter4g = DefaultBandwidthMeter bandwidthMeter4g =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application).build(); new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application).build();
long initialEstimate4g = bandwidthMeter4g.getBitrateEstimate(); long initialEstimate4g = bandwidthMeter4g.getBitrateEstimate();
setActiveNetworkInfo(networkInfo3g); setActiveNetworkInfo(networkInfo3g);
CountryAndNetworkTypeBandwidthMeter bandwidthMeter3g = DefaultBandwidthMeter bandwidthMeter3g =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application).build(); new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application).build();
long initialEstimate3g = bandwidthMeter3g.getBitrateEstimate(); long initialEstimate3g = bandwidthMeter3g.getBitrateEstimate();
assertThat(initialEstimate4g).isGreaterThan(initialEstimate3g); assertThat(initialEstimate4g).isGreaterThan(initialEstimate3g);
@ -196,13 +194,13 @@ public final class CountryAndNetworkTypeBandwidthMeterTest {
@Test @Test
public void defaultInitialBitrateEstimate_for3G_isGreaterThanEstimateFor2G() { public void defaultInitialBitrateEstimate_for3G_isGreaterThanEstimateFor2G() {
setActiveNetworkInfo(networkInfo3g); setActiveNetworkInfo(networkInfo3g);
CountryAndNetworkTypeBandwidthMeter bandwidthMeter3g = DefaultBandwidthMeter bandwidthMeter3g =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application).build(); new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application).build();
long initialEstimate3g = bandwidthMeter3g.getBitrateEstimate(); long initialEstimate3g = bandwidthMeter3g.getBitrateEstimate();
setActiveNetworkInfo(networkInfo2g); setActiveNetworkInfo(networkInfo2g);
CountryAndNetworkTypeBandwidthMeter bandwidthMeter2g = DefaultBandwidthMeter bandwidthMeter2g =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application).build(); new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application).build();
long initialEstimate2g = bandwidthMeter2g.getBitrateEstimate(); long initialEstimate2g = bandwidthMeter2g.getBitrateEstimate();
assertThat(initialEstimate3g).isGreaterThan(initialEstimate2g); assertThat(initialEstimate3g).isGreaterThan(initialEstimate2g);
@ -211,8 +209,8 @@ public final class CountryAndNetworkTypeBandwidthMeterTest {
@Test @Test
public void defaultInitialBitrateEstimate_forOffline_isReasonable() { public void defaultInitialBitrateEstimate_forOffline_isReasonable() {
setActiveNetworkInfo(networkInfoOffline); setActiveNetworkInfo(networkInfoOffline);
CountryAndNetworkTypeBandwidthMeter bandwidthMeter = DefaultBandwidthMeter bandwidthMeter =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application).build(); new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application).build();
long initialEstimate = bandwidthMeter.getBitrateEstimate(); long initialEstimate = bandwidthMeter.getBitrateEstimate();
assertThat(initialEstimate).isGreaterThan(100_000L); assertThat(initialEstimate).isGreaterThan(100_000L);
@ -224,13 +222,13 @@ public final class CountryAndNetworkTypeBandwidthMeterTest {
defaultInitialBitrateEstimate_forWifi_forFastCountry_isGreaterThanEstimateForSlowCountry() { defaultInitialBitrateEstimate_forWifi_forFastCountry_isGreaterThanEstimateForSlowCountry() {
setActiveNetworkInfo(networkInfoWifi); setActiveNetworkInfo(networkInfoWifi);
setNetworkCountryIso(FAST_COUNTRY_ISO); setNetworkCountryIso(FAST_COUNTRY_ISO);
CountryAndNetworkTypeBandwidthMeter bandwidthMeterFast = DefaultBandwidthMeter bandwidthMeterFast =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application).build(); new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application).build();
long initialEstimateFast = bandwidthMeterFast.getBitrateEstimate(); long initialEstimateFast = bandwidthMeterFast.getBitrateEstimate();
setNetworkCountryIso(SLOW_COUNTRY_ISO); setNetworkCountryIso(SLOW_COUNTRY_ISO);
CountryAndNetworkTypeBandwidthMeter bandwidthMeterSlow = DefaultBandwidthMeter bandwidthMeterSlow =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application).build(); new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application).build();
long initialEstimateSlow = bandwidthMeterSlow.getBitrateEstimate(); long initialEstimateSlow = bandwidthMeterSlow.getBitrateEstimate();
assertThat(initialEstimateFast).isGreaterThan(initialEstimateSlow); assertThat(initialEstimateFast).isGreaterThan(initialEstimateSlow);
@ -241,13 +239,13 @@ public final class CountryAndNetworkTypeBandwidthMeterTest {
defaultInitialBitrateEstimate_forEthernet_forFastCountry_isGreaterThanEstimateForSlowCountry() { defaultInitialBitrateEstimate_forEthernet_forFastCountry_isGreaterThanEstimateForSlowCountry() {
setActiveNetworkInfo(networkInfoEthernet); setActiveNetworkInfo(networkInfoEthernet);
setNetworkCountryIso(FAST_COUNTRY_ISO); setNetworkCountryIso(FAST_COUNTRY_ISO);
CountryAndNetworkTypeBandwidthMeter bandwidthMeterFast = DefaultBandwidthMeter bandwidthMeterFast =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application).build(); new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application).build();
long initialEstimateFast = bandwidthMeterFast.getBitrateEstimate(); long initialEstimateFast = bandwidthMeterFast.getBitrateEstimate();
setNetworkCountryIso(SLOW_COUNTRY_ISO); setNetworkCountryIso(SLOW_COUNTRY_ISO);
CountryAndNetworkTypeBandwidthMeter bandwidthMeterSlow = DefaultBandwidthMeter bandwidthMeterSlow =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application).build(); new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application).build();
long initialEstimateSlow = bandwidthMeterSlow.getBitrateEstimate(); long initialEstimateSlow = bandwidthMeterSlow.getBitrateEstimate();
assertThat(initialEstimateFast).isGreaterThan(initialEstimateSlow); assertThat(initialEstimateFast).isGreaterThan(initialEstimateSlow);
@ -258,13 +256,13 @@ public final class CountryAndNetworkTypeBandwidthMeterTest {
defaultInitialBitrateEstimate_for2G_forFastCountry_isGreaterThanEstimateForSlowCountry() { defaultInitialBitrateEstimate_for2G_forFastCountry_isGreaterThanEstimateForSlowCountry() {
setActiveNetworkInfo(networkInfo2g); setActiveNetworkInfo(networkInfo2g);
setNetworkCountryIso(FAST_COUNTRY_ISO); setNetworkCountryIso(FAST_COUNTRY_ISO);
CountryAndNetworkTypeBandwidthMeter bandwidthMeterFast = DefaultBandwidthMeter bandwidthMeterFast =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application).build(); new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application).build();
long initialEstimateFast = bandwidthMeterFast.getBitrateEstimate(); long initialEstimateFast = bandwidthMeterFast.getBitrateEstimate();
setNetworkCountryIso(SLOW_COUNTRY_ISO); setNetworkCountryIso(SLOW_COUNTRY_ISO);
CountryAndNetworkTypeBandwidthMeter bandwidthMeterSlow = DefaultBandwidthMeter bandwidthMeterSlow =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application).build(); new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application).build();
long initialEstimateSlow = bandwidthMeterSlow.getBitrateEstimate(); long initialEstimateSlow = bandwidthMeterSlow.getBitrateEstimate();
assertThat(initialEstimateFast).isGreaterThan(initialEstimateSlow); assertThat(initialEstimateFast).isGreaterThan(initialEstimateSlow);
@ -275,13 +273,13 @@ public final class CountryAndNetworkTypeBandwidthMeterTest {
defaultInitialBitrateEstimate_for3G_forFastCountry_isGreaterThanEstimateForSlowCountry() { defaultInitialBitrateEstimate_for3G_forFastCountry_isGreaterThanEstimateForSlowCountry() {
setActiveNetworkInfo(networkInfo3g); setActiveNetworkInfo(networkInfo3g);
setNetworkCountryIso(FAST_COUNTRY_ISO); setNetworkCountryIso(FAST_COUNTRY_ISO);
CountryAndNetworkTypeBandwidthMeter bandwidthMeterFast = DefaultBandwidthMeter bandwidthMeterFast =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application).build(); new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application).build();
long initialEstimateFast = bandwidthMeterFast.getBitrateEstimate(); long initialEstimateFast = bandwidthMeterFast.getBitrateEstimate();
setNetworkCountryIso(SLOW_COUNTRY_ISO); setNetworkCountryIso(SLOW_COUNTRY_ISO);
CountryAndNetworkTypeBandwidthMeter bandwidthMeterSlow = DefaultBandwidthMeter bandwidthMeterSlow =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application).build(); new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application).build();
long initialEstimateSlow = bandwidthMeterSlow.getBitrateEstimate(); long initialEstimateSlow = bandwidthMeterSlow.getBitrateEstimate();
assertThat(initialEstimateFast).isGreaterThan(initialEstimateSlow); assertThat(initialEstimateFast).isGreaterThan(initialEstimateSlow);
@ -292,13 +290,13 @@ public final class CountryAndNetworkTypeBandwidthMeterTest {
defaultInitialBitrateEstimate_for4g_forFastCountry_isGreaterThanEstimateForSlowCountry() { defaultInitialBitrateEstimate_for4g_forFastCountry_isGreaterThanEstimateForSlowCountry() {
setActiveNetworkInfo(networkInfo4g); setActiveNetworkInfo(networkInfo4g);
setNetworkCountryIso(FAST_COUNTRY_ISO); setNetworkCountryIso(FAST_COUNTRY_ISO);
CountryAndNetworkTypeBandwidthMeter bandwidthMeterFast = DefaultBandwidthMeter bandwidthMeterFast =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application).build(); new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application).build();
long initialEstimateFast = bandwidthMeterFast.getBitrateEstimate(); long initialEstimateFast = bandwidthMeterFast.getBitrateEstimate();
setNetworkCountryIso(SLOW_COUNTRY_ISO); setNetworkCountryIso(SLOW_COUNTRY_ISO);
CountryAndNetworkTypeBandwidthMeter bandwidthMeterSlow = DefaultBandwidthMeter bandwidthMeterSlow =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application).build(); new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application).build();
long initialEstimateSlow = bandwidthMeterSlow.getBitrateEstimate(); long initialEstimateSlow = bandwidthMeterSlow.getBitrateEstimate();
assertThat(initialEstimateFast).isGreaterThan(initialEstimateSlow); assertThat(initialEstimateFast).isGreaterThan(initialEstimateSlow);
@ -307,8 +305,8 @@ public final class CountryAndNetworkTypeBandwidthMeterTest {
@Test @Test
public void initialBitrateEstimateOverwrite_whileConnectedToNetwork_setsInitialEstimate() { public void initialBitrateEstimateOverwrite_whileConnectedToNetwork_setsInitialEstimate() {
setActiveNetworkInfo(networkInfoWifi); setActiveNetworkInfo(networkInfoWifi);
CountryAndNetworkTypeBandwidthMeter bandwidthMeter = DefaultBandwidthMeter bandwidthMeter =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application) new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application)
.setInitialBitrateEstimate(123456789) .setInitialBitrateEstimate(123456789)
.build(); .build();
long initialEstimate = bandwidthMeter.getBitrateEstimate(); long initialEstimate = bandwidthMeter.getBitrateEstimate();
@ -319,8 +317,8 @@ public final class CountryAndNetworkTypeBandwidthMeterTest {
@Test @Test
public void initialBitrateEstimateOverwrite_whileOffline_setsInitialEstimate() { public void initialBitrateEstimateOverwrite_whileOffline_setsInitialEstimate() {
setActiveNetworkInfo(networkInfoOffline); setActiveNetworkInfo(networkInfoOffline);
CountryAndNetworkTypeBandwidthMeter bandwidthMeter = DefaultBandwidthMeter bandwidthMeter =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application) new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application)
.setInitialBitrateEstimate(123456789) .setInitialBitrateEstimate(123456789)
.build(); .build();
long initialEstimate = bandwidthMeter.getBitrateEstimate(); long initialEstimate = bandwidthMeter.getBitrateEstimate();
@ -331,8 +329,8 @@ public final class CountryAndNetworkTypeBandwidthMeterTest {
@Test @Test
public void initialBitrateEstimateOverwrite_forWifi_whileConnectedToWifi_setsInitialEstimate() { public void initialBitrateEstimateOverwrite_forWifi_whileConnectedToWifi_setsInitialEstimate() {
setActiveNetworkInfo(networkInfoWifi); setActiveNetworkInfo(networkInfoWifi);
CountryAndNetworkTypeBandwidthMeter bandwidthMeter = DefaultBandwidthMeter bandwidthMeter =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application) new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application)
.setInitialBitrateEstimate(C.NETWORK_TYPE_WIFI, 123456789) .setInitialBitrateEstimate(C.NETWORK_TYPE_WIFI, 123456789)
.build(); .build();
long initialEstimate = bandwidthMeter.getBitrateEstimate(); long initialEstimate = bandwidthMeter.getBitrateEstimate();
@ -344,8 +342,8 @@ public final class CountryAndNetworkTypeBandwidthMeterTest {
public void public void
initialBitrateEstimateOverwrite_forWifi_whileConnectedToOtherNetwork_doesNotSetInitialEstimate() { initialBitrateEstimateOverwrite_forWifi_whileConnectedToOtherNetwork_doesNotSetInitialEstimate() {
setActiveNetworkInfo(networkInfo2g); setActiveNetworkInfo(networkInfo2g);
CountryAndNetworkTypeBandwidthMeter bandwidthMeter = DefaultBandwidthMeter bandwidthMeter =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application) new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application)
.setInitialBitrateEstimate(C.NETWORK_TYPE_WIFI, 123456789) .setInitialBitrateEstimate(C.NETWORK_TYPE_WIFI, 123456789)
.build(); .build();
long initialEstimate = bandwidthMeter.getBitrateEstimate(); long initialEstimate = bandwidthMeter.getBitrateEstimate();
@ -357,8 +355,8 @@ public final class CountryAndNetworkTypeBandwidthMeterTest {
public void public void
initialBitrateEstimateOverwrite_forEthernet_whileConnectedToEthernet_setsInitialEstimate() { initialBitrateEstimateOverwrite_forEthernet_whileConnectedToEthernet_setsInitialEstimate() {
setActiveNetworkInfo(networkInfoEthernet); setActiveNetworkInfo(networkInfoEthernet);
CountryAndNetworkTypeBandwidthMeter bandwidthMeter = DefaultBandwidthMeter bandwidthMeter =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application) new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application)
.setInitialBitrateEstimate(C.NETWORK_TYPE_ETHERNET, 123456789) .setInitialBitrateEstimate(C.NETWORK_TYPE_ETHERNET, 123456789)
.build(); .build();
long initialEstimate = bandwidthMeter.getBitrateEstimate(); long initialEstimate = bandwidthMeter.getBitrateEstimate();
@ -370,8 +368,8 @@ public final class CountryAndNetworkTypeBandwidthMeterTest {
public void public void
initialBitrateEstimateOverwrite_forEthernet_whileConnectedToOtherNetwork_doesNotSetInitialEstimate() { initialBitrateEstimateOverwrite_forEthernet_whileConnectedToOtherNetwork_doesNotSetInitialEstimate() {
setActiveNetworkInfo(networkInfo2g); setActiveNetworkInfo(networkInfo2g);
CountryAndNetworkTypeBandwidthMeter bandwidthMeter = DefaultBandwidthMeter bandwidthMeter =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application) new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application)
.setInitialBitrateEstimate(C.NETWORK_TYPE_WIFI, 123456789) .setInitialBitrateEstimate(C.NETWORK_TYPE_WIFI, 123456789)
.build(); .build();
long initialEstimate = bandwidthMeter.getBitrateEstimate(); long initialEstimate = bandwidthMeter.getBitrateEstimate();
@ -382,8 +380,8 @@ public final class CountryAndNetworkTypeBandwidthMeterTest {
@Test @Test
public void initialBitrateEstimateOverwrite_for2G_whileConnectedTo2G_setsInitialEstimate() { public void initialBitrateEstimateOverwrite_for2G_whileConnectedTo2G_setsInitialEstimate() {
setActiveNetworkInfo(networkInfo2g); setActiveNetworkInfo(networkInfo2g);
CountryAndNetworkTypeBandwidthMeter bandwidthMeter = DefaultBandwidthMeter bandwidthMeter =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application) new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application)
.setInitialBitrateEstimate(C.NETWORK_TYPE_2G, 123456789) .setInitialBitrateEstimate(C.NETWORK_TYPE_2G, 123456789)
.build(); .build();
long initialEstimate = bandwidthMeter.getBitrateEstimate(); long initialEstimate = bandwidthMeter.getBitrateEstimate();
@ -395,8 +393,8 @@ public final class CountryAndNetworkTypeBandwidthMeterTest {
public void public void
initialBitrateEstimateOverwrite_for2G_whileConnectedToOtherNetwork_doesNotSetInitialEstimate() { initialBitrateEstimateOverwrite_for2G_whileConnectedToOtherNetwork_doesNotSetInitialEstimate() {
setActiveNetworkInfo(networkInfoWifi); setActiveNetworkInfo(networkInfoWifi);
CountryAndNetworkTypeBandwidthMeter bandwidthMeter = DefaultBandwidthMeter bandwidthMeter =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application) new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application)
.setInitialBitrateEstimate(C.NETWORK_TYPE_2G, 123456789) .setInitialBitrateEstimate(C.NETWORK_TYPE_2G, 123456789)
.build(); .build();
long initialEstimate = bandwidthMeter.getBitrateEstimate(); long initialEstimate = bandwidthMeter.getBitrateEstimate();
@ -407,8 +405,8 @@ public final class CountryAndNetworkTypeBandwidthMeterTest {
@Test @Test
public void initialBitrateEstimateOverwrite_for3G_whileConnectedTo3G_setsInitialEstimate() { public void initialBitrateEstimateOverwrite_for3G_whileConnectedTo3G_setsInitialEstimate() {
setActiveNetworkInfo(networkInfo3g); setActiveNetworkInfo(networkInfo3g);
CountryAndNetworkTypeBandwidthMeter bandwidthMeter = DefaultBandwidthMeter bandwidthMeter =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application) new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application)
.setInitialBitrateEstimate(C.NETWORK_TYPE_3G, 123456789) .setInitialBitrateEstimate(C.NETWORK_TYPE_3G, 123456789)
.build(); .build();
long initialEstimate = bandwidthMeter.getBitrateEstimate(); long initialEstimate = bandwidthMeter.getBitrateEstimate();
@ -420,8 +418,8 @@ public final class CountryAndNetworkTypeBandwidthMeterTest {
public void public void
initialBitrateEstimateOverwrite_for3G_whileConnectedToOtherNetwork_doesNotSetInitialEstimate() { initialBitrateEstimateOverwrite_for3G_whileConnectedToOtherNetwork_doesNotSetInitialEstimate() {
setActiveNetworkInfo(networkInfoWifi); setActiveNetworkInfo(networkInfoWifi);
CountryAndNetworkTypeBandwidthMeter bandwidthMeter = DefaultBandwidthMeter bandwidthMeter =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application) new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application)
.setInitialBitrateEstimate(C.NETWORK_TYPE_3G, 123456789) .setInitialBitrateEstimate(C.NETWORK_TYPE_3G, 123456789)
.build(); .build();
long initialEstimate = bandwidthMeter.getBitrateEstimate(); long initialEstimate = bandwidthMeter.getBitrateEstimate();
@ -432,8 +430,8 @@ public final class CountryAndNetworkTypeBandwidthMeterTest {
@Test @Test
public void initialBitrateEstimateOverwrite_for4G_whileConnectedTo4G_setsInitialEstimate() { public void initialBitrateEstimateOverwrite_for4G_whileConnectedTo4G_setsInitialEstimate() {
setActiveNetworkInfo(networkInfo4g); setActiveNetworkInfo(networkInfo4g);
CountryAndNetworkTypeBandwidthMeter bandwidthMeter = DefaultBandwidthMeter bandwidthMeter =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application) new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application)
.setInitialBitrateEstimate(C.NETWORK_TYPE_4G, 123456789) .setInitialBitrateEstimate(C.NETWORK_TYPE_4G, 123456789)
.build(); .build();
long initialEstimate = bandwidthMeter.getBitrateEstimate(); long initialEstimate = bandwidthMeter.getBitrateEstimate();
@ -445,8 +443,8 @@ public final class CountryAndNetworkTypeBandwidthMeterTest {
public void public void
initialBitrateEstimateOverwrite_for4G_whileConnectedToOtherNetwork_doesNotSetInitialEstimate() { initialBitrateEstimateOverwrite_for4G_whileConnectedToOtherNetwork_doesNotSetInitialEstimate() {
setActiveNetworkInfo(networkInfoWifi); setActiveNetworkInfo(networkInfoWifi);
CountryAndNetworkTypeBandwidthMeter bandwidthMeter = DefaultBandwidthMeter bandwidthMeter =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application) new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application)
.setInitialBitrateEstimate(C.NETWORK_TYPE_4G, 123456789) .setInitialBitrateEstimate(C.NETWORK_TYPE_4G, 123456789)
.build(); .build();
long initialEstimate = bandwidthMeter.getBitrateEstimate(); long initialEstimate = bandwidthMeter.getBitrateEstimate();
@ -457,8 +455,8 @@ public final class CountryAndNetworkTypeBandwidthMeterTest {
@Test @Test
public void initialBitrateEstimateOverwrite_forOffline_whileOffline_setsInitialEstimate() { public void initialBitrateEstimateOverwrite_forOffline_whileOffline_setsInitialEstimate() {
setActiveNetworkInfo(networkInfoOffline); setActiveNetworkInfo(networkInfoOffline);
CountryAndNetworkTypeBandwidthMeter bandwidthMeter = DefaultBandwidthMeter bandwidthMeter =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application) new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application)
.setInitialBitrateEstimate(C.NETWORK_TYPE_OFFLINE, 123456789) .setInitialBitrateEstimate(C.NETWORK_TYPE_OFFLINE, 123456789)
.build(); .build();
long initialEstimate = bandwidthMeter.getBitrateEstimate(); long initialEstimate = bandwidthMeter.getBitrateEstimate();
@ -470,8 +468,8 @@ public final class CountryAndNetworkTypeBandwidthMeterTest {
public void public void
initialBitrateEstimateOverwrite_forOffline_whileConnectedToNetwork_doesNotSetInitialEstimate() { initialBitrateEstimateOverwrite_forOffline_whileConnectedToNetwork_doesNotSetInitialEstimate() {
setActiveNetworkInfo(networkInfoWifi); setActiveNetworkInfo(networkInfoWifi);
CountryAndNetworkTypeBandwidthMeter bandwidthMeter = DefaultBandwidthMeter bandwidthMeter =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application) new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application)
.setInitialBitrateEstimate(C.NETWORK_TYPE_OFFLINE, 123456789) .setInitialBitrateEstimate(C.NETWORK_TYPE_OFFLINE, 123456789)
.build(); .build();
long initialEstimate = bandwidthMeter.getBitrateEstimate(); long initialEstimate = bandwidthMeter.getBitrateEstimate();
@ -482,13 +480,13 @@ public final class CountryAndNetworkTypeBandwidthMeterTest {
@Test @Test
public void initialBitrateEstimateOverwrite_forCountry_usesDefaultValuesForCountry() { public void initialBitrateEstimateOverwrite_forCountry_usesDefaultValuesForCountry() {
setNetworkCountryIso(SLOW_COUNTRY_ISO); setNetworkCountryIso(SLOW_COUNTRY_ISO);
CountryAndNetworkTypeBandwidthMeter bandwidthMeterSlow = DefaultBandwidthMeter bandwidthMeterSlow =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application).build(); new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application).build();
long initialEstimateSlow = bandwidthMeterSlow.getBitrateEstimate(); long initialEstimateSlow = bandwidthMeterSlow.getBitrateEstimate();
setNetworkCountryIso(FAST_COUNTRY_ISO); setNetworkCountryIso(FAST_COUNTRY_ISO);
CountryAndNetworkTypeBandwidthMeter bandwidthMeterFastWithSlowOverwrite = DefaultBandwidthMeter bandwidthMeterFastWithSlowOverwrite =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application) new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application)
.setInitialBitrateEstimate(SLOW_COUNTRY_ISO) .setInitialBitrateEstimate(SLOW_COUNTRY_ISO)
.build(); .build();
long initialEstimateFastWithSlowOverwrite = long initialEstimateFastWithSlowOverwrite =
@ -499,12 +497,11 @@ public final class CountryAndNetworkTypeBandwidthMeterTest {
@Test @Test
public void defaultInitialBitrateEstimate_withoutContext_isReasonable() { public void defaultInitialBitrateEstimate_withoutContext_isReasonable() {
CountryAndNetworkTypeBandwidthMeter bandwidthMeterWithBuilder = DefaultBandwidthMeter bandwidthMeterWithBuilder =
new CountryAndNetworkTypeBandwidthMeter.Builder(/* context= */ null).build(); new DefaultBandwidthMeter.Builder(/* context= */ null).build();
long initialEstimateWithBuilder = bandwidthMeterWithBuilder.getBitrateEstimate(); long initialEstimateWithBuilder = bandwidthMeterWithBuilder.getBitrateEstimate();
CountryAndNetworkTypeBandwidthMeter bandwidthMeterWithoutBuilder = DefaultBandwidthMeter bandwidthMeterWithoutBuilder = new DefaultBandwidthMeter();
new CountryAndNetworkTypeBandwidthMeter();
long initialEstimateWithoutBuilder = bandwidthMeterWithoutBuilder.getBitrateEstimate(); long initialEstimateWithoutBuilder = bandwidthMeterWithoutBuilder.getBitrateEstimate();
assertThat(initialEstimateWithBuilder).isGreaterThan(100_000L); assertThat(initialEstimateWithBuilder).isGreaterThan(100_000L);
@ -516,8 +513,8 @@ public final class CountryAndNetworkTypeBandwidthMeterTest {
@Test @Test
public void defaultInitialBitrateEstimate_withoutAccessNetworkStatePermission_isReasonable() { public void defaultInitialBitrateEstimate_withoutAccessNetworkStatePermission_isReasonable() {
Shadows.shadowOf(RuntimeEnvironment.application).denyPermissions(ACCESS_NETWORK_STATE); Shadows.shadowOf(RuntimeEnvironment.application).denyPermissions(ACCESS_NETWORK_STATE);
CountryAndNetworkTypeBandwidthMeter bandwidthMeter = DefaultBandwidthMeter bandwidthMeter =
new CountryAndNetworkTypeBandwidthMeter.Builder(RuntimeEnvironment.application).build(); new DefaultBandwidthMeter.Builder(RuntimeEnvironment.application).build();
long initialEstimate = bandwidthMeter.getBitrateEstimate(); long initialEstimate = bandwidthMeter.getBitrateEstimate();
assertThat(initialEstimate).isGreaterThan(100_000L); assertThat(initialEstimate).isGreaterThan(100_000L);