Commit ca4b5d0a by olly Committed by Oliver Woodman

Make context non-optional for DefaultBandwidthMeter

- Added ability to override the DefaultBandwidthMeter network type.
- These methods currently only work properly if called before the
  meter is used, but the plan is for them to be handled in the same
  way as internally detected network changes.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=217887019
parent 4c6507e7
...@@ -18,6 +18,8 @@ ...@@ -18,6 +18,8 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
package="com.google.android.exoplayer2.ext.flac.test"> package="com.google.android.exoplayer2.ext.flac.test">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application android:debuggable="true" <application android:debuggable="true"
android:allowBackup="false" android:allowBackup="false"
tools:ignore="MissingApplicationIcon,HardcodedDebugMode"> tools:ignore="MissingApplicationIcon,HardcodedDebugMode">
......
...@@ -83,7 +83,7 @@ public class FlacPlaybackTest { ...@@ -83,7 +83,7 @@ public class FlacPlaybackTest {
Looper.prepare(); Looper.prepare();
LibflacAudioRenderer audioRenderer = new LibflacAudioRenderer(); LibflacAudioRenderer audioRenderer = new LibflacAudioRenderer();
DefaultTrackSelector trackSelector = new DefaultTrackSelector(); DefaultTrackSelector trackSelector = new DefaultTrackSelector();
player = ExoPlayerFactory.newInstance(new Renderer[] {audioRenderer}, trackSelector); player = ExoPlayerFactory.newInstance(context, new Renderer[] {audioRenderer}, trackSelector);
player.addListener(this); player.addListener(this);
MediaSource mediaSource = MediaSource mediaSource =
new ExtractorMediaSource.Factory( new ExtractorMediaSource.Factory(
......
...@@ -18,6 +18,8 @@ ...@@ -18,6 +18,8 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
package="com.google.android.exoplayer2.ext.opus.test"> package="com.google.android.exoplayer2.ext.opus.test">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application android:debuggable="true" <application android:debuggable="true"
android:allowBackup="false" android:allowBackup="false"
tools:ignore="MissingApplicationIcon,HardcodedDebugMode"> tools:ignore="MissingApplicationIcon,HardcodedDebugMode">
......
...@@ -83,7 +83,7 @@ public class OpusPlaybackTest { ...@@ -83,7 +83,7 @@ public class OpusPlaybackTest {
Looper.prepare(); Looper.prepare();
LibopusAudioRenderer audioRenderer = new LibopusAudioRenderer(); LibopusAudioRenderer audioRenderer = new LibopusAudioRenderer();
DefaultTrackSelector trackSelector = new DefaultTrackSelector(); DefaultTrackSelector trackSelector = new DefaultTrackSelector();
player = ExoPlayerFactory.newInstance(new Renderer[] {audioRenderer}, trackSelector); player = ExoPlayerFactory.newInstance(context, new Renderer[] {audioRenderer}, trackSelector);
player.addListener(this); player.addListener(this);
MediaSource mediaSource = MediaSource mediaSource =
new ExtractorMediaSource.Factory( new ExtractorMediaSource.Factory(
......
...@@ -18,6 +18,8 @@ ...@@ -18,6 +18,8 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
package="com.google.android.exoplayer2.ext.vp9.test"> package="com.google.android.exoplayer2.ext.vp9.test">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application android:debuggable="true" <application android:debuggable="true"
android:allowBackup="false" android:allowBackup="false"
tools:ignore="MissingApplicationIcon,HardcodedDebugMode"> tools:ignore="MissingApplicationIcon,HardcodedDebugMode">
......
...@@ -116,7 +116,7 @@ public class VpxPlaybackTest { ...@@ -116,7 +116,7 @@ public class VpxPlaybackTest {
Looper.prepare(); Looper.prepare();
LibvpxVideoRenderer videoRenderer = new LibvpxVideoRenderer(true, 0); LibvpxVideoRenderer videoRenderer = new LibvpxVideoRenderer(true, 0);
DefaultTrackSelector trackSelector = new DefaultTrackSelector(); DefaultTrackSelector trackSelector = new DefaultTrackSelector();
player = ExoPlayerFactory.newInstance(new Renderer[] {videoRenderer}, trackSelector); player = ExoPlayerFactory.newInstance(context, new Renderer[] {videoRenderer}, trackSelector);
player.addListener(this); player.addListener(this);
MediaSource mediaSource = MediaSource mediaSource =
new ExtractorMediaSource.Factory( new ExtractorMediaSource.Factory(
......
...@@ -82,18 +82,13 @@ public final class DefaultBandwidthMeter implements BandwidthMeter, TransferList ...@@ -82,18 +82,13 @@ public final class DefaultBandwidthMeter implements BandwidthMeter, TransferList
private int slidingWindowMaxWeight; private int slidingWindowMaxWeight;
private Clock clock; private Clock clock;
/** @deprecated Use {@link #Builder(Context)} instead. */
@Deprecated
public Builder() {
this(/* context= */ null);
}
/** /**
* Creates a builder with default parameters and without listener. * Creates a builder with default parameters and without listener.
* *
* @param context A context. * @param context A context.
*/ */
public Builder(@Nullable Context context) { public Builder(Context context) {
// Handling of null is for backward compatibility only.
this.context = context == null ? null : context.getApplicationContext(); this.context = context == null ? null : context.getApplicationContext();
initialBitrateEstimates = getInitialBitrateEstimatesForCountry(Util.getCountryCode(context)); initialBitrateEstimates = getInitialBitrateEstimatesForCountry(Util.getCountryCode(context));
slidingWindowMaxWeight = DEFAULT_SLIDING_WINDOW_MAX_WEIGHT; slidingWindowMaxWeight = DEFAULT_SLIDING_WINDOW_MAX_WEIGHT;
...@@ -187,14 +182,9 @@ public final class DefaultBandwidthMeter implements BandwidthMeter, TransferList ...@@ -187,14 +182,9 @@ 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(
context == null ? C.NETWORK_TYPE_UNKNOWN : Util.getNetworkType(context));
if (initialBitrateEstimate == null) {
initialBitrateEstimate = initialBitrateEstimates.get(C.NETWORK_TYPE_UNKNOWN);
}
DefaultBandwidthMeter bandwidthMeter = DefaultBandwidthMeter bandwidthMeter =
new DefaultBandwidthMeter(initialBitrateEstimate, slidingWindowMaxWeight, clock); new DefaultBandwidthMeter(
context, initialBitrateEstimates, slidingWindowMaxWeight, clock);
if (eventHandler != null && eventListener != null) { if (eventHandler != null && eventListener != null) {
bandwidthMeter.addEventListener(eventHandler, eventListener); bandwidthMeter.addEventListener(eventHandler, eventListener);
} }
...@@ -225,6 +215,7 @@ public final class DefaultBandwidthMeter implements BandwidthMeter, TransferList ...@@ -225,6 +215,7 @@ public final class DefaultBandwidthMeter implements BandwidthMeter, TransferList
private static final int ELAPSED_MILLIS_FOR_ESTIMATE = 2000; private static final int ELAPSED_MILLIS_FOR_ESTIMATE = 2000;
private static final int BYTES_TRANSFERRED_FOR_ESTIMATE = 512 * 1024; private static final int BYTES_TRANSFERRED_FOR_ESTIMATE = 512 * 1024;
private final SparseArray<Long> initialBitrateEstimates;
private final EventDispatcher<EventListener> eventDispatcher; private final EventDispatcher<EventListener> eventDispatcher;
private final SlidingPercentile slidingPercentile; private final SlidingPercentile slidingPercentile;
private final Clock clock; private final Clock clock;
...@@ -237,34 +228,54 @@ public final class DefaultBandwidthMeter implements BandwidthMeter, TransferList ...@@ -237,34 +228,54 @@ public final class DefaultBandwidthMeter implements BandwidthMeter, TransferList
private long totalBytesTransferred; private long totalBytesTransferred;
private long bitrateEstimate; private long bitrateEstimate;
/** Creates a bandwidth meter with default parameters. */
public DefaultBandwidthMeter() {
this(DEFAULT_INITIAL_BITRATE_ESTIMATE, DEFAULT_SLIDING_WINDOW_MAX_WEIGHT, Clock.DEFAULT);
}
/** @deprecated Use {@link Builder} instead. */ /** @deprecated Use {@link Builder} instead. */
@Deprecated @Deprecated
public DefaultBandwidthMeter(Handler eventHandler, EventListener eventListener) { public DefaultBandwidthMeter() {
this(DEFAULT_INITIAL_BITRATE_ESTIMATE, DEFAULT_SLIDING_WINDOW_MAX_WEIGHT, Clock.DEFAULT); this(
if (eventHandler != null && eventListener != null) { /* context= */ null,
addEventListener(eventHandler, eventListener); /* initialBitrateEstimates= */ new SparseArray<>(),
} DEFAULT_SLIDING_WINDOW_MAX_WEIGHT,
Clock.DEFAULT);
} }
/** @deprecated Use {@link Builder} instead. */ /** @deprecated Use {@link Builder} instead. */
@Deprecated @Deprecated
public DefaultBandwidthMeter(Handler eventHandler, EventListener eventListener, int maxWeight) { public DefaultBandwidthMeter(Handler eventHandler, EventListener eventListener) {
this(DEFAULT_INITIAL_BITRATE_ESTIMATE, maxWeight, Clock.DEFAULT); this(
/* context= */ null,
/* initialBitrateEstimates= */ new SparseArray<>(),
DEFAULT_SLIDING_WINDOW_MAX_WEIGHT,
Clock.DEFAULT);
if (eventHandler != null && eventListener != null) { if (eventHandler != null && eventListener != null) {
addEventListener(eventHandler, eventListener); addEventListener(eventHandler, eventListener);
} }
} }
private DefaultBandwidthMeter(long initialBitrateEstimate, int maxWeight, Clock clock) { private DefaultBandwidthMeter(
@Nullable Context context,
SparseArray<Long> initialBitrateEstimates,
int maxWeight,
Clock clock) {
this.initialBitrateEstimates = initialBitrateEstimates;
this.eventDispatcher = new EventDispatcher<>(); this.eventDispatcher = new EventDispatcher<>();
this.slidingPercentile = new SlidingPercentile(maxWeight); this.slidingPercentile = new SlidingPercentile(maxWeight);
this.clock = clock; this.clock = clock;
bitrateEstimate = initialBitrateEstimate; bitrateEstimate =
getInitialBitrateEstimateForNetworkType(
context == null ? C.NETWORK_TYPE_UNKNOWN : Util.getNetworkType(context));
}
/**
* Overrides the network type. Handled in the same way as if the meter had detected a change from
* the current network type to the specified network type.
*
* <p>Applications should not normally call this method. It is intended for testing purposes.
*
* @param networkType The overriding network type.
*/
public synchronized void setNetworkTypeOverride(@C.NetworkType int networkType) {
// TODO: Handle properly as a network change (in same way as non-external network changes).
bitrateEstimate = getInitialBitrateEstimateForNetworkType(networkType);
} }
@Override @Override
...@@ -343,6 +354,17 @@ public final class DefaultBandwidthMeter implements BandwidthMeter, TransferList ...@@ -343,6 +354,17 @@ public final class DefaultBandwidthMeter implements BandwidthMeter, TransferList
eventDispatcher.dispatch(listener -> listener.onBandwidthSample(elapsedMs, bytes, bitrate)); eventDispatcher.dispatch(listener -> listener.onBandwidthSample(elapsedMs, bytes, bitrate));
} }
private long getInitialBitrateEstimateForNetworkType(@C.NetworkType int networkType) {
Long initialBitrateEstimate = initialBitrateEstimates.get(networkType);
if (initialBitrateEstimate == null) {
initialBitrateEstimate = initialBitrateEstimates.get(C.NETWORK_TYPE_UNKNOWN);
}
if (initialBitrateEstimate == null) {
initialBitrateEstimate = DEFAULT_INITIAL_BITRATE_ESTIMATE;
}
return initialBitrateEstimate;
}
private static Map<String, int[]> createInitialBitrateCountryGroupAssignment() { private static Map<String, int[]> createInitialBitrateCountryGroupAssignment() {
HashMap<String, int[]> countryGroupAssignment = new HashMap<>(); HashMap<String, int[]> countryGroupAssignment = new HashMap<>();
countryGroupAssignment.put("AD", new int[] {1, 0, 0, 0}); countryGroupAssignment.put("AD", new int[] {1, 0, 0, 0});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment