Move ImaAdsLoader Player.Listener implementation to internal class

PiperOrigin-RevId: 416613846
This commit is contained in:
ibaker 2021-12-15 19:56:42 +00:00 committed by Ian Baker
parent 9316b4f49b
commit 4bf7ffd9a7
3 changed files with 66 additions and 61 deletions

View file

@ -86,7 +86,7 @@ import java.util.Set;
* href="https://developers.google.com/interactive-media-ads/docs/sdks/android/client-side/omsdk">IMA
* SDK Open Measurement documentation</a> for more information.
*/
public final class ImaAdsLoader implements Player.Listener, AdsLoader {
public final class ImaAdsLoader implements AdsLoader {
static {
ExoPlayerLibraryInfo.registerModule("goog.exo.ima");
@ -386,6 +386,7 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
private final ImaUtil.Configuration configuration;
private final Context context;
private final ImaUtil.ImaFactory imaFactory;
private final PlayerListenerImpl playerListener;
private final HashMap<Object, AdTagLoader> adTagLoaderByAdsId;
private final HashMap<AdsMediaSource, AdTagLoader> adTagLoaderByAdsMediaSource;
private final Timeline.Period period;
@ -402,6 +403,7 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
this.context = context.getApplicationContext();
this.configuration = configuration;
this.imaFactory = imaFactory;
playerListener = new PlayerListenerImpl();
supportedMimeTypes = ImmutableList.of();
adTagLoaderByAdsId = new HashMap<>();
adTagLoaderByAdsMediaSource = new HashMap<>();
@ -532,7 +534,7 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
if (player == null) {
return;
}
player.addListener(this);
player.addListener(playerListener);
}
@Nullable AdTagLoader adTagLoader = adTagLoaderByAdsId.get(adsId);
@ -554,7 +556,7 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
}
if (player != null && adTagLoaderByAdsMediaSource.isEmpty()) {
player.removeListener(this);
player.removeListener(playerListener);
player = null;
}
}
@ -562,7 +564,7 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
@Override
public void release() {
if (player != null) {
player.removeListener(this);
player.removeListener(playerListener);
player = null;
maybeUpdateCurrentAdTagLoader();
}
@ -602,37 +604,6 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
.handlePrepareError(adGroupIndex, adIndexInAdGroup, exception);
}
// Player.Listener implementation.
@Override
public void onTimelineChanged(Timeline timeline, @Player.TimelineChangeReason int reason) {
if (timeline.isEmpty()) {
// The player is being reset or contains no media.
return;
}
maybeUpdateCurrentAdTagLoader();
maybePreloadNextPeriodAds();
}
@Override
public void onPositionDiscontinuity(
Player.PositionInfo oldPosition,
Player.PositionInfo newPosition,
@Player.DiscontinuityReason int reason) {
maybeUpdateCurrentAdTagLoader();
maybePreloadNextPeriodAds();
}
@Override
public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {
maybePreloadNextPeriodAds();
}
@Override
public void onRepeatModeChanged(@Player.RepeatMode int repeatMode) {
maybePreloadNextPeriodAds();
}
// Internal methods.
private void maybeUpdateCurrentAdTagLoader() {
@ -672,7 +643,7 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
}
private void maybePreloadNextPeriodAds() {
@Nullable Player player = this.player;
@Nullable Player player = ImaAdsLoader.this.player;
if (player == null) {
return;
}
@ -706,6 +677,38 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
nextAdTagLoader.maybePreloadAds(Util.usToMs(periodPositionUs), Util.usToMs(period.durationUs));
}
private final class PlayerListenerImpl implements Player.Listener {
@Override
public void onTimelineChanged(Timeline timeline, @Player.TimelineChangeReason int reason) {
if (timeline.isEmpty()) {
// The player is being reset or contains no media.
return;
}
maybeUpdateCurrentAdTagLoader();
maybePreloadNextPeriodAds();
}
@Override
public void onPositionDiscontinuity(
Player.PositionInfo oldPosition,
Player.PositionInfo newPosition,
@Player.DiscontinuityReason int reason) {
maybeUpdateCurrentAdTagLoader();
maybePreloadNextPeriodAds();
}
@Override
public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {
maybePreloadNextPeriodAds();
}
@Override
public void onRepeatModeChanged(@Player.RepeatMode int repeatMode) {
maybePreloadNextPeriodAds();
}
}
/**
* Default {@link ImaUtil.ImaFactory} for non-test usage, which delegates to {@link
* ImaSdkFactory}.

View file

@ -15,10 +15,13 @@
*/
package com.google.android.exoplayer2.ext.ima;
import static com.google.android.exoplayer2.util.Assertions.checkState;
import android.os.Looper;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.PlaybackException;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.TracksInfo;
@ -182,6 +185,29 @@ import com.google.android.exoplayer2.util.Util;
}
}
/**
* Sets an error on this player.
*
* <p>This will propagate the error to {@link Player.Listener#onPlayerError(PlaybackException)}
* and {@link Player.Listener#onPlayerErrorChanged(PlaybackException)} and will also update the
* state to {@link Player#STATE_IDLE}.
*
* <p>The player must be in {@link #STATE_BUFFERING} or {@link #STATE_READY}.
*/
@SuppressWarnings("deprecation") // Calling deprecated listener.onPlayerStateChanged()
public void setPlayerError(PlaybackException error) {
checkState(state == STATE_BUFFERING || state == STATE_READY);
this.state = Player.STATE_IDLE;
listeners.sendEvent(
Player.EVENT_PLAYBACK_STATE_CHANGED,
listener -> {
listener.onPlayerError(error);
listener.onPlayerErrorChanged(error);
listener.onPlayerStateChanged(playWhenReady, state);
listener.onPlaybackStateChanged(state);
});
}
// ExoPlayer methods. Other methods are unsupported.
@Override

View file

@ -56,7 +56,6 @@ import com.google.ads.interactivemedia.v3.api.player.VideoAdPlayer;
import com.google.ads.interactivemedia.v3.api.player.VideoProgressUpdate;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.PlaybackException;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.Timeline;
@ -276,30 +275,7 @@ public final class ImaAdsLoaderTest {
ExoPlaybackException anException =
ExoPlaybackException.createForSource(
new IOException(), PlaybackException.ERROR_CODE_IO_UNSPECIFIED);
imaAdsLoader.onPlayerErrorChanged(anException);
imaAdsLoader.onPlayerError(anException);
imaAdsLoader.onPositionDiscontinuity(
new Player.PositionInfo(
/* windowUid= */ new Object(),
/* windowIndex= */ 0,
/* mediaItem= */ MediaItem.fromUri("http://google.com/0"),
/* periodUid= */ new Object(),
/* periodIndex= */ 0,
/* positionMs= */ 10_000,
/* contentPositionMs= */ 0,
/* adGroupIndex= */ -1,
/* adIndexInAdGroup= */ -1),
new Player.PositionInfo(
/* windowUid= */ new Object(),
/* windowIndex= */ 1,
/* mediaItem= */ MediaItem.fromUri("http://google.com/1"),
/* periodUid= */ new Object(),
/* periodIndex= */ 0,
/* positionMs= */ 20_000,
/* contentPositionMs= */ 0,
/* adGroupIndex= */ -1,
/* adIndexInAdGroup= */ -1),
Player.DISCONTINUITY_REASON_SEEK);
fakePlayer.setPlayerError(anException);
adEventListener.onAdEvent(getAdEvent(AdEventType.CONTENT_RESUME_REQUESTED, /* ad= */ null));
imaAdsLoader.handlePrepareError(
adsMediaSource, /* adGroupIndex= */ 0, /* adIndexInAdGroup= */ 0, new IOException());