Skip to content

Commit

Permalink
Add ad event listeners in the Looper event of the ad manager callback
Browse files Browse the repository at this point in the history
#minor-release

PiperOrigin-RevId: 509189206
  • Loading branch information
marcbaechinger authored and christosts committed Feb 13, 2023
1 parent 15f0865 commit e98670e
Showing 1 changed file with 13 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -557,11 +557,10 @@ public void prepareSourceInternal(@Nullable TransferListener mediaTransferListen
StreamManagerLoadable streamManagerLoadable =
new StreamManagerLoadable(
sdkAdsLoader,
adsLoader.configuration,
/* imaServerSideAdInsertionMediaSource= */ this,
streamRequest,
streamPlayer,
applicationAdErrorListener,
loadVideoTimeoutMs);
applicationAdErrorListener);
loader.startLoading(
streamManagerLoadable,
new StreamManagerLoadableCallback(),
Expand Down Expand Up @@ -636,7 +635,6 @@ private void setStreamManager(@Nullable StreamManager streamManager) {
}
this.streamManager.removeAdEventListener(componentListener);
this.streamManager.destroy();
this.streamManager = null;
}
this.streamManager = streamManager;
if (streamManager != null) {
Expand All @@ -647,6 +645,12 @@ private void setStreamManager(@Nullable StreamManager streamManager) {
if (applicationAdErrorListener != null) {
streamManager.addAdErrorListener(applicationAdErrorListener);
}
AdsRenderingSettings adsRenderingSettings =
ImaSdkFactory.getInstance().createAdsRenderingSettings();
adsRenderingSettings.setLoadVideoTimeout(loadVideoTimeoutMs);
adsRenderingSettings.setFocusSkipButtonWhenAvailable(
adsLoader.configuration.focusSkipButtonWhenAvailable);
streamManager.init(adsRenderingSettings);
}
}

Expand Down Expand Up @@ -913,7 +917,6 @@ private final class StreamManagerLoadableCallback
@Override
public void onLoadCompleted(
StreamManagerLoadable loadable, long elapsedRealtimeMs, long loadDurationMs) {
mainHandler.post(() -> setStreamManager(checkNotNull(loadable.getStreamManager())));
setContentUri(checkNotNull(loadable.getContentUri()));
}

Expand Down Expand Up @@ -944,14 +947,12 @@ private static class StreamManagerLoadable
implements Loadable, AdsLoadedListener, AdErrorListener {

private final com.google.ads.interactivemedia.v3.api.AdsLoader adsLoader;
private final ServerSideAdInsertionConfiguration serverSideAdInsertionConfiguration;
private final ImaServerSideAdInsertionMediaSource imaServerSideAdInsertionMediaSource;
private final StreamRequest request;
private final StreamPlayer streamPlayer;
@Nullable private final AdErrorListener adErrorListener;
private final int loadVideoTimeoutMs;
private final ConditionVariable conditionVariable;

@Nullable private volatile StreamManager streamManager;
@Nullable private volatile Uri contentUri;
private volatile boolean cancelled;
private volatile boolean error;
Expand All @@ -961,17 +962,15 @@ private static class StreamManagerLoadable
/** Creates an instance. */
private StreamManagerLoadable(
com.google.ads.interactivemedia.v3.api.AdsLoader adsLoader,
ServerSideAdInsertionConfiguration serverSideAdInsertionConfiguration,
ImaServerSideAdInsertionMediaSource imaServerSideAdInsertionMediaSource,
StreamRequest request,
StreamPlayer streamPlayer,
@Nullable AdErrorListener adErrorListener,
int loadVideoTimeoutMs) {
@Nullable AdErrorListener adErrorListener) {
this.adsLoader = adsLoader;
this.serverSideAdInsertionConfiguration = serverSideAdInsertionConfiguration;
this.imaServerSideAdInsertionMediaSource = imaServerSideAdInsertionMediaSource;
this.request = request;
this.streamPlayer = streamPlayer;
this.adErrorListener = adErrorListener;
this.loadVideoTimeoutMs = loadVideoTimeoutMs;
conditionVariable = new ConditionVariable();
errorCode = -1;
}
Expand All @@ -982,12 +981,6 @@ public Uri getContentUri() {
return contentUri;
}

/** Returns the stream manager or null if not yet loaded. */
@Nullable
public StreamManager getStreamManager() {
return streamManager;
}

// Implement Loadable.

@Override
Expand Down Expand Up @@ -1041,14 +1034,7 @@ public void onAdsManagerLoaded(AdsManagerLoadedEvent event) {
conditionVariable.open();
return;
}
AdsRenderingSettings adsRenderingSettings =
ImaSdkFactory.getInstance().createAdsRenderingSettings();
adsRenderingSettings.setLoadVideoTimeout(loadVideoTimeoutMs);
adsRenderingSettings.setFocusSkipButtonWhenAvailable(
serverSideAdInsertionConfiguration.focusSkipButtonWhenAvailable);
// After initialization completed the streamUri will be reported to the streamPlayer.
streamManager.init(adsRenderingSettings);
this.streamManager = streamManager;
imaServerSideAdInsertionMediaSource.setStreamManager(streamManager);
}

// AdErrorEvent.AdErrorListener implementation.
Expand Down

0 comments on commit e98670e

Please sign in to comment.