mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +00:00
Move ImaAdsLoader Player.Listener implementation to internal class
PiperOrigin-RevId: 416613846
This commit is contained in:
parent
9316b4f49b
commit
4bf7ffd9a7
3 changed files with 66 additions and 61 deletions
|
|
@ -86,7 +86,7 @@ import java.util.Set;
|
||||||
* href="https://developers.google.com/interactive-media-ads/docs/sdks/android/client-side/omsdk">IMA
|
* href="https://developers.google.com/interactive-media-ads/docs/sdks/android/client-side/omsdk">IMA
|
||||||
* SDK Open Measurement documentation</a> for more information.
|
* SDK Open Measurement documentation</a> for more information.
|
||||||
*/
|
*/
|
||||||
public final class ImaAdsLoader implements Player.Listener, AdsLoader {
|
public final class ImaAdsLoader implements AdsLoader {
|
||||||
|
|
||||||
static {
|
static {
|
||||||
ExoPlayerLibraryInfo.registerModule("goog.exo.ima");
|
ExoPlayerLibraryInfo.registerModule("goog.exo.ima");
|
||||||
|
|
@ -386,6 +386,7 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
|
||||||
private final ImaUtil.Configuration configuration;
|
private final ImaUtil.Configuration configuration;
|
||||||
private final Context context;
|
private final Context context;
|
||||||
private final ImaUtil.ImaFactory imaFactory;
|
private final ImaUtil.ImaFactory imaFactory;
|
||||||
|
private final PlayerListenerImpl playerListener;
|
||||||
private final HashMap<Object, AdTagLoader> adTagLoaderByAdsId;
|
private final HashMap<Object, AdTagLoader> adTagLoaderByAdsId;
|
||||||
private final HashMap<AdsMediaSource, AdTagLoader> adTagLoaderByAdsMediaSource;
|
private final HashMap<AdsMediaSource, AdTagLoader> adTagLoaderByAdsMediaSource;
|
||||||
private final Timeline.Period period;
|
private final Timeline.Period period;
|
||||||
|
|
@ -402,6 +403,7 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
|
||||||
this.context = context.getApplicationContext();
|
this.context = context.getApplicationContext();
|
||||||
this.configuration = configuration;
|
this.configuration = configuration;
|
||||||
this.imaFactory = imaFactory;
|
this.imaFactory = imaFactory;
|
||||||
|
playerListener = new PlayerListenerImpl();
|
||||||
supportedMimeTypes = ImmutableList.of();
|
supportedMimeTypes = ImmutableList.of();
|
||||||
adTagLoaderByAdsId = new HashMap<>();
|
adTagLoaderByAdsId = new HashMap<>();
|
||||||
adTagLoaderByAdsMediaSource = new HashMap<>();
|
adTagLoaderByAdsMediaSource = new HashMap<>();
|
||||||
|
|
@ -532,7 +534,7 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
|
||||||
if (player == null) {
|
if (player == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
player.addListener(this);
|
player.addListener(playerListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable AdTagLoader adTagLoader = adTagLoaderByAdsId.get(adsId);
|
@Nullable AdTagLoader adTagLoader = adTagLoaderByAdsId.get(adsId);
|
||||||
|
|
@ -554,7 +556,7 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (player != null && adTagLoaderByAdsMediaSource.isEmpty()) {
|
if (player != null && adTagLoaderByAdsMediaSource.isEmpty()) {
|
||||||
player.removeListener(this);
|
player.removeListener(playerListener);
|
||||||
player = null;
|
player = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -562,7 +564,7 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
|
||||||
@Override
|
@Override
|
||||||
public void release() {
|
public void release() {
|
||||||
if (player != null) {
|
if (player != null) {
|
||||||
player.removeListener(this);
|
player.removeListener(playerListener);
|
||||||
player = null;
|
player = null;
|
||||||
maybeUpdateCurrentAdTagLoader();
|
maybeUpdateCurrentAdTagLoader();
|
||||||
}
|
}
|
||||||
|
|
@ -602,37 +604,6 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
|
||||||
.handlePrepareError(adGroupIndex, adIndexInAdGroup, exception);
|
.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.
|
// Internal methods.
|
||||||
|
|
||||||
private void maybeUpdateCurrentAdTagLoader() {
|
private void maybeUpdateCurrentAdTagLoader() {
|
||||||
|
|
@ -672,7 +643,7 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void maybePreloadNextPeriodAds() {
|
private void maybePreloadNextPeriodAds() {
|
||||||
@Nullable Player player = this.player;
|
@Nullable Player player = ImaAdsLoader.this.player;
|
||||||
if (player == null) {
|
if (player == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -706,6 +677,38 @@ public final class ImaAdsLoader implements Player.Listener, AdsLoader {
|
||||||
nextAdTagLoader.maybePreloadAds(Util.usToMs(periodPositionUs), Util.usToMs(period.durationUs));
|
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
|
* Default {@link ImaUtil.ImaFactory} for non-test usage, which delegates to {@link
|
||||||
* ImaSdkFactory}.
|
* ImaSdkFactory}.
|
||||||
|
|
|
||||||
|
|
@ -15,10 +15,13 @@
|
||||||
*/
|
*/
|
||||||
package com.google.android.exoplayer2.ext.ima;
|
package com.google.android.exoplayer2.ext.ima;
|
||||||
|
|
||||||
|
import static com.google.android.exoplayer2.util.Assertions.checkState;
|
||||||
|
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
import com.google.android.exoplayer2.C;
|
import com.google.android.exoplayer2.C;
|
||||||
import com.google.android.exoplayer2.ExoPlayer;
|
import com.google.android.exoplayer2.ExoPlayer;
|
||||||
import com.google.android.exoplayer2.MediaItem;
|
import com.google.android.exoplayer2.MediaItem;
|
||||||
|
import com.google.android.exoplayer2.PlaybackException;
|
||||||
import com.google.android.exoplayer2.Player;
|
import com.google.android.exoplayer2.Player;
|
||||||
import com.google.android.exoplayer2.Timeline;
|
import com.google.android.exoplayer2.Timeline;
|
||||||
import com.google.android.exoplayer2.TracksInfo;
|
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.
|
// ExoPlayer methods. Other methods are unsupported.
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -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.ads.interactivemedia.v3.api.player.VideoProgressUpdate;
|
||||||
import com.google.android.exoplayer2.C;
|
import com.google.android.exoplayer2.C;
|
||||||
import com.google.android.exoplayer2.ExoPlaybackException;
|
import com.google.android.exoplayer2.ExoPlaybackException;
|
||||||
import com.google.android.exoplayer2.MediaItem;
|
|
||||||
import com.google.android.exoplayer2.PlaybackException;
|
import com.google.android.exoplayer2.PlaybackException;
|
||||||
import com.google.android.exoplayer2.Player;
|
import com.google.android.exoplayer2.Player;
|
||||||
import com.google.android.exoplayer2.Timeline;
|
import com.google.android.exoplayer2.Timeline;
|
||||||
|
|
@ -276,30 +275,7 @@ public final class ImaAdsLoaderTest {
|
||||||
ExoPlaybackException anException =
|
ExoPlaybackException anException =
|
||||||
ExoPlaybackException.createForSource(
|
ExoPlaybackException.createForSource(
|
||||||
new IOException(), PlaybackException.ERROR_CODE_IO_UNSPECIFIED);
|
new IOException(), PlaybackException.ERROR_CODE_IO_UNSPECIFIED);
|
||||||
imaAdsLoader.onPlayerErrorChanged(anException);
|
fakePlayer.setPlayerError(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);
|
|
||||||
adEventListener.onAdEvent(getAdEvent(AdEventType.CONTENT_RESUME_REQUESTED, /* ad= */ null));
|
adEventListener.onAdEvent(getAdEvent(AdEventType.CONTENT_RESUME_REQUESTED, /* ad= */ null));
|
||||||
imaAdsLoader.handlePrepareError(
|
imaAdsLoader.handlePrepareError(
|
||||||
adsMediaSource, /* adGroupIndex= */ 0, /* adIndexInAdGroup= */ 0, new IOException());
|
adsMediaSource, /* adGroupIndex= */ 0, /* adIndexInAdGroup= */ 0, new IOException());
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue