Skip to content

Commit

Permalink
Move SimpleExoPlayer.Builder logic to ExoPlayer.Builder.
Browse files Browse the repository at this point in the history
SimpleExoPlayer Builder now wraps an ExoPlayer.Builder, rather than the
other way round.

PiperOrigin-RevId: 404509106
  • Loading branch information
Samrobbo authored and ojw28 committed Oct 21, 2021
1 parent 2ebbdbe commit 140ef75
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 157 deletions.
156 changes: 118 additions & 38 deletions library/core/src/main/java/com/google/android/exoplayer2/ExoPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package com.google.android.exoplayer2;

import static com.google.android.exoplayer2.util.Assertions.checkArgument;
import static com.google.android.exoplayer2.util.Assertions.checkState;

import android.content.Context;
import android.media.AudioTrack;
import android.media.MediaCodec;
Expand Down Expand Up @@ -359,7 +362,34 @@ default void onExperimentalSleepingForOffloadChanged(boolean sleepingForOffload)
@SuppressWarnings("deprecation")
final class Builder {

private final SimpleExoPlayer.Builder wrappedBuilder;
/* package */ final Context context;
/* package */ final RenderersFactory renderersFactory;

/* package */ Clock clock;
/* package */ long foregroundModeTimeoutMs;
/* package */ TrackSelector trackSelector;
/* package */ MediaSourceFactory mediaSourceFactory;
/* package */ LoadControl loadControl;
/* package */ BandwidthMeter bandwidthMeter;
/* package */ AnalyticsCollector analyticsCollector;
/* package */ Looper looper;
@Nullable /* package */ PriorityTaskManager priorityTaskManager;
/* package */ AudioAttributes audioAttributes;
/* package */ boolean handleAudioFocus;
@C.WakeMode /* package */ int wakeMode;
/* package */ boolean handleAudioBecomingNoisy;
/* package */ boolean skipSilenceEnabled;
@C.VideoScalingMode /* package */ int videoScalingMode;
@C.VideoChangeFrameRateStrategy /* package */ int videoChangeFrameRateStrategy;
/* package */ boolean useLazyPreparation;
/* package */ SeekParameters seekParameters;
/* package */ long seekBackIncrementMs;
/* package */ long seekForwardIncrementMs;
/* package */ LivePlaybackSpeedControl livePlaybackSpeedControl;
/* package */ long releaseTimeoutMs;
/* package */ long detachSurfaceTimeoutMs;
/* package */ boolean pauseAtEndOfMediaItems;
/* package */ boolean buildCalled;

/**
* Creates a builder.
Expand Down Expand Up @@ -404,7 +434,7 @@ final class Builder {
* @param context A {@link Context}.
*/
public Builder(Context context) {
wrappedBuilder = new SimpleExoPlayer.Builder(context);
this(context, new DefaultRenderersFactory(context), new DefaultExtractorsFactory());
}

/**
Expand All @@ -417,7 +447,7 @@ public Builder(Context context) {
* player.
*/
public Builder(Context context, RenderersFactory renderersFactory) {
wrappedBuilder = new SimpleExoPlayer.Builder(context, renderersFactory);
this(context, renderersFactory, new DefaultExtractorsFactory());
}

/**
Expand All @@ -430,7 +460,7 @@ public Builder(Context context, RenderersFactory renderersFactory) {
* its container.
*/
public Builder(Context context, ExtractorsFactory extractorsFactory) {
wrappedBuilder = new SimpleExoPlayer.Builder(context, extractorsFactory);
this(context, new DefaultRenderersFactory(context), extractorsFactory);
}

/**
Expand All @@ -446,7 +476,14 @@ public Builder(Context context, ExtractorsFactory extractorsFactory) {
*/
public Builder(
Context context, RenderersFactory renderersFactory, ExtractorsFactory extractorsFactory) {
wrappedBuilder = new SimpleExoPlayer.Builder(context, renderersFactory, extractorsFactory);
this(
context,
renderersFactory,
new DefaultTrackSelector(context),
new DefaultMediaSourceFactory(context, extractorsFactory),
new DefaultLoadControl(),
DefaultBandwidthMeter.getSingletonInstance(context),
new AnalyticsCollector(Clock.DEFAULT));
}

/**
Expand All @@ -472,15 +509,26 @@ public Builder(
LoadControl loadControl,
BandwidthMeter bandwidthMeter,
AnalyticsCollector analyticsCollector) {
wrappedBuilder =
new SimpleExoPlayer.Builder(
context,
renderersFactory,
trackSelector,
mediaSourceFactory,
loadControl,
bandwidthMeter,
analyticsCollector);
this.context = context;
this.renderersFactory = renderersFactory;
this.trackSelector = trackSelector;
this.mediaSourceFactory = mediaSourceFactory;
this.loadControl = loadControl;
this.bandwidthMeter = bandwidthMeter;
this.analyticsCollector = analyticsCollector;
looper = Util.getCurrentOrMainLooper();
audioAttributes = AudioAttributes.DEFAULT;
wakeMode = C.WAKE_MODE_NONE;
videoScalingMode = C.VIDEO_SCALING_MODE_DEFAULT;
videoChangeFrameRateStrategy = C.VIDEO_CHANGE_FRAME_RATE_STRATEGY_ONLY_IF_SEAMLESS;
useLazyPreparation = true;
seekParameters = SeekParameters.DEFAULT;
seekBackIncrementMs = C.DEFAULT_SEEK_BACK_INCREMENT_MS;
seekForwardIncrementMs = C.DEFAULT_SEEK_FORWARD_INCREMENT_MS;
livePlaybackSpeedControl = new DefaultLivePlaybackSpeedControl.Builder().build();
clock = Clock.DEFAULT;
releaseTimeoutMs = DEFAULT_RELEASE_TIMEOUT_MS;
detachSurfaceTimeoutMs = DEFAULT_DETACH_SURFACE_TIMEOUT_MS;
}

/**
Expand All @@ -493,7 +541,8 @@ public Builder(
* @param timeoutMs The time limit in milliseconds.
*/
public Builder experimentalSetForegroundModeTimeoutMs(long timeoutMs) {
wrappedBuilder.experimentalSetForegroundModeTimeoutMs(timeoutMs);
checkState(!buildCalled);
foregroundModeTimeoutMs = timeoutMs;
return this;
}

Expand All @@ -505,7 +554,8 @@ public Builder experimentalSetForegroundModeTimeoutMs(long timeoutMs) {
* @throws IllegalStateException If {@link #build()} has already been called.
*/
public Builder setTrackSelector(TrackSelector trackSelector) {
wrappedBuilder.setTrackSelector(trackSelector);
checkState(!buildCalled);
this.trackSelector = trackSelector;
return this;
}

Expand All @@ -517,7 +567,8 @@ public Builder setTrackSelector(TrackSelector trackSelector) {
* @throws IllegalStateException If {@link #build()} has already been called.
*/
public Builder setMediaSourceFactory(MediaSourceFactory mediaSourceFactory) {
wrappedBuilder.setMediaSourceFactory(mediaSourceFactory);
checkState(!buildCalled);
this.mediaSourceFactory = mediaSourceFactory;
return this;
}

Expand All @@ -529,7 +580,8 @@ public Builder setMediaSourceFactory(MediaSourceFactory mediaSourceFactory) {
* @throws IllegalStateException If {@link #build()} has already been called.
*/
public Builder setLoadControl(LoadControl loadControl) {
wrappedBuilder.setLoadControl(loadControl);
checkState(!buildCalled);
this.loadControl = loadControl;
return this;
}

Expand All @@ -541,7 +593,8 @@ public Builder setLoadControl(LoadControl loadControl) {
* @throws IllegalStateException If {@link #build()} has already been called.
*/
public Builder setBandwidthMeter(BandwidthMeter bandwidthMeter) {
wrappedBuilder.setBandwidthMeter(bandwidthMeter);
checkState(!buildCalled);
this.bandwidthMeter = bandwidthMeter;
return this;
}

Expand All @@ -554,7 +607,8 @@ public Builder setBandwidthMeter(BandwidthMeter bandwidthMeter) {
* @throws IllegalStateException If {@link #build()} has already been called.
*/
public Builder setLooper(Looper looper) {
wrappedBuilder.setLooper(looper);
checkState(!buildCalled);
this.looper = looper;
return this;
}

Expand All @@ -566,7 +620,8 @@ public Builder setLooper(Looper looper) {
* @throws IllegalStateException If {@link #build()} has already been called.
*/
public Builder setAnalyticsCollector(AnalyticsCollector analyticsCollector) {
wrappedBuilder.setAnalyticsCollector(analyticsCollector);
checkState(!buildCalled);
this.analyticsCollector = analyticsCollector;
return this;
}

Expand All @@ -580,7 +635,8 @@ public Builder setAnalyticsCollector(AnalyticsCollector analyticsCollector) {
* @throws IllegalStateException If {@link #build()} has already been called.
*/
public Builder setPriorityTaskManager(@Nullable PriorityTaskManager priorityTaskManager) {
wrappedBuilder.setPriorityTaskManager(priorityTaskManager);
checkState(!buildCalled);
this.priorityTaskManager = priorityTaskManager;
return this;
}

Expand All @@ -598,7 +654,9 @@ public Builder setPriorityTaskManager(@Nullable PriorityTaskManager priorityTask
* @throws IllegalStateException If {@link #build()} has already been called.
*/
public Builder setAudioAttributes(AudioAttributes audioAttributes, boolean handleAudioFocus) {
wrappedBuilder.setAudioAttributes(audioAttributes, handleAudioFocus);
checkState(!buildCalled);
this.audioAttributes = audioAttributes;
this.handleAudioFocus = handleAudioFocus;
return this;
}

Expand All @@ -620,7 +678,8 @@ public Builder setAudioAttributes(AudioAttributes audioAttributes, boolean handl
* @throws IllegalStateException If {@link #build()} has already been called.
*/
public Builder setWakeMode(@C.WakeMode int wakeMode) {
wrappedBuilder.setWakeMode(wakeMode);
checkState(!buildCalled);
this.wakeMode = wakeMode;
return this;
}

Expand All @@ -636,7 +695,8 @@ public Builder setWakeMode(@C.WakeMode int wakeMode) {
* @throws IllegalStateException If {@link #build()} has already been called.
*/
public Builder setHandleAudioBecomingNoisy(boolean handleAudioBecomingNoisy) {
wrappedBuilder.setHandleAudioBecomingNoisy(handleAudioBecomingNoisy);
checkState(!buildCalled);
this.handleAudioBecomingNoisy = handleAudioBecomingNoisy;
return this;
}

Expand All @@ -648,7 +708,8 @@ public Builder setHandleAudioBecomingNoisy(boolean handleAudioBecomingNoisy) {
* @throws IllegalStateException If {@link #build()} has already been called.
*/
public Builder setSkipSilenceEnabled(boolean skipSilenceEnabled) {
wrappedBuilder.setSkipSilenceEnabled(skipSilenceEnabled);
checkState(!buildCalled);
this.skipSilenceEnabled = skipSilenceEnabled;
return this;
}

Expand All @@ -663,7 +724,8 @@ public Builder setSkipSilenceEnabled(boolean skipSilenceEnabled) {
* @throws IllegalStateException If {@link #build()} has already been called.
*/
public Builder setVideoScalingMode(@C.VideoScalingMode int videoScalingMode) {
wrappedBuilder.setVideoScalingMode(videoScalingMode);
checkState(!buildCalled);
this.videoScalingMode = videoScalingMode;
return this;
}

Expand All @@ -683,7 +745,8 @@ public Builder setVideoScalingMode(@C.VideoScalingMode int videoScalingMode) {
*/
public Builder setVideoChangeFrameRateStrategy(
@C.VideoChangeFrameRateStrategy int videoChangeFrameRateStrategy) {
wrappedBuilder.setVideoChangeFrameRateStrategy(videoChangeFrameRateStrategy);
checkState(!buildCalled);
this.videoChangeFrameRateStrategy = videoChangeFrameRateStrategy;
return this;
}

Expand All @@ -699,7 +762,8 @@ public Builder setVideoChangeFrameRateStrategy(
* @throws IllegalStateException If {@link #build()} has already been called.
*/
public Builder setUseLazyPreparation(boolean useLazyPreparation) {
wrappedBuilder.setUseLazyPreparation(useLazyPreparation);
checkState(!buildCalled);
this.useLazyPreparation = useLazyPreparation;
return this;
}

Expand All @@ -711,7 +775,8 @@ public Builder setUseLazyPreparation(boolean useLazyPreparation) {
* @throws IllegalStateException If {@link #build()} has already been called.
*/
public Builder setSeekParameters(SeekParameters seekParameters) {
wrappedBuilder.setSeekParameters(seekParameters);
checkState(!buildCalled);
this.seekParameters = seekParameters;
return this;
}

Expand All @@ -724,7 +789,9 @@ public Builder setSeekParameters(SeekParameters seekParameters) {
* @throws IllegalStateException If {@link #build()} has already been called.
*/
public Builder setSeekBackIncrementMs(@IntRange(from = 1) long seekBackIncrementMs) {
wrappedBuilder.setSeekBackIncrementMs(seekBackIncrementMs);
checkArgument(seekBackIncrementMs > 0);
checkState(!buildCalled);
this.seekBackIncrementMs = seekBackIncrementMs;
return this;
}

Expand All @@ -737,7 +804,9 @@ public Builder setSeekBackIncrementMs(@IntRange(from = 1) long seekBackIncrement
* @throws IllegalStateException If {@link #build()} has already been called.
*/
public Builder setSeekForwardIncrementMs(@IntRange(from = 1) long seekForwardIncrementMs) {
wrappedBuilder.setSeekForwardIncrementMs(seekForwardIncrementMs);
checkArgument(seekForwardIncrementMs > 0);
checkState(!buildCalled);
this.seekForwardIncrementMs = seekForwardIncrementMs;
return this;
}

Expand All @@ -753,7 +822,8 @@ public Builder setSeekForwardIncrementMs(@IntRange(from = 1) long seekForwardInc
* @throws IllegalStateException If {@link #build()} has already been called.
*/
public Builder setReleaseTimeoutMs(long releaseTimeoutMs) {
wrappedBuilder.setReleaseTimeoutMs(releaseTimeoutMs);
checkState(!buildCalled);
this.releaseTimeoutMs = releaseTimeoutMs;
return this;
}

Expand All @@ -769,7 +839,8 @@ public Builder setReleaseTimeoutMs(long releaseTimeoutMs) {
* @throws IllegalStateException If {@link #build()} has already been called.
*/
public Builder setDetachSurfaceTimeoutMs(long detachSurfaceTimeoutMs) {
wrappedBuilder.setDetachSurfaceTimeoutMs(detachSurfaceTimeoutMs);
checkState(!buildCalled);
this.detachSurfaceTimeoutMs = detachSurfaceTimeoutMs;
return this;
}

Expand All @@ -786,7 +857,8 @@ public Builder setDetachSurfaceTimeoutMs(long detachSurfaceTimeoutMs) {
* @throws IllegalStateException If {@link #build()} has already been called.
*/
public Builder setPauseAtEndOfMediaItems(boolean pauseAtEndOfMediaItems) {
wrappedBuilder.setPauseAtEndOfMediaItems(pauseAtEndOfMediaItems);
checkState(!buildCalled);
this.pauseAtEndOfMediaItems = pauseAtEndOfMediaItems;
return this;
}

Expand All @@ -799,7 +871,8 @@ public Builder setPauseAtEndOfMediaItems(boolean pauseAtEndOfMediaItems) {
* @throws IllegalStateException If {@link #build()} has already been called.
*/
public Builder setLivePlaybackSpeedControl(LivePlaybackSpeedControl livePlaybackSpeedControl) {
wrappedBuilder.setLivePlaybackSpeedControl(livePlaybackSpeedControl);
checkState(!buildCalled);
this.livePlaybackSpeedControl = livePlaybackSpeedControl;
return this;
}

Expand All @@ -813,7 +886,8 @@ public Builder setLivePlaybackSpeedControl(LivePlaybackSpeedControl livePlayback
*/
@VisibleForTesting
public Builder setClock(Clock clock) {
wrappedBuilder.setClock(clock);
checkState(!buildCalled);
this.clock = clock;
return this;
}

Expand All @@ -823,7 +897,13 @@ public Builder setClock(Clock clock) {
* @throws IllegalStateException If this method has already been called.
*/
public ExoPlayer build() {
return wrappedBuilder.build();
return buildSimpleExoPlayer();
}

/* package */ SimpleExoPlayer buildSimpleExoPlayer() {
checkState(!buildCalled);
buildCalled = true;
return new SimpleExoPlayer(/* builder= */ this);
}
}

Expand Down
Loading

0 comments on commit 140ef75

Please sign in to comment.