Skip to content

Commit

Permalink
Pass AdsMediaSource to other AdsLoader methods
Browse files Browse the repository at this point in the history
Issue: #3750
PiperOrigin-RevId: 341020676
  • Loading branch information
andrewlewis committed Nov 6, 2020
1 parent d94943f commit 0c301fe
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ public void start(
}

@Override
public void stop() {
public void stop(AdsMediaSource adsMediaSource) {
if (player != null && adTagLoader != null) {
adTagLoader.stop();
}
Expand All @@ -508,14 +508,19 @@ public void release() {
}

@Override
public void handlePrepareComplete(int adGroupIndex, int adIndexInAdGroup) {
public void handlePrepareComplete(
AdsMediaSource adsMediaSource, int adGroupIndex, int adIndexInAdGroup) {
if (adTagLoader != null) {
adTagLoader.handlePrepareComplete(adGroupIndex, adIndexInAdGroup);
}
}

@Override
public void handlePrepareError(int adGroupIndex, int adIndexInAdGroup, IOException exception) {
public void handlePrepareError(
AdsMediaSource adsMediaSource,
int adGroupIndex,
int adIndexInAdGroup,
IOException exception) {
if (adTagLoader != null) {
adTagLoader.handlePrepareError(adGroupIndex, adIndexInAdGroup, exception);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public void startAndCallbacksAfterRelease() {
imaAdsLoader.onPositionDiscontinuity(Player.DISCONTINUITY_REASON_SEEK);
adEventListener.onAdEvent(getAdEvent(AdEventType.CONTENT_RESUME_REQUESTED, /* ad= */ null));
imaAdsLoader.handlePrepareError(
/* adGroupIndex= */ 0, /* adIndexInAdGroup= */ 0, new IOException());
adsMediaSource, /* adGroupIndex= */ 0, /* adIndexInAdGroup= */ 0, new IOException());
}

@Test
Expand Down Expand Up @@ -836,7 +836,7 @@ public void stop_unregistersAllVideoControlOverlays() {
imaAdsLoader.start(
adsMediaSource, TEST_DATA_SPEC, TEST_ADS_ID, adViewProvider, adsLoaderListener);
imaAdsLoader.requestAds(TEST_DATA_SPEC, adViewGroup);
imaAdsLoader.stop();
imaAdsLoader.stop(adsMediaSource);

InOrder inOrder = inOrder(mockAdDisplayContainer);
inOrder.verify(mockAdDisplayContainer).registerFriendlyObstruction(mockFriendlyObstruction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
*
* <p>{@link #start(AdsMediaSource, DataSpec, Object, AdViewProvider, EventListener)} will be called
* when an ads media source first initializes, at which point the loader can request ads. If the
* player enters the background, {@link #stop()} will be called. Loaders should maintain any ad
* playback state in preparation for a later call to {@link #start(AdsMediaSource, DataSpec, Object,
* AdViewProvider, EventListener)}. If an ad is playing when the player is detached, update the ad
* playback state with the current playback position using {@link
* player enters the background, {@link #stop(AdsMediaSource)} will be called. Loaders should
* maintain any ad playback state in preparation for a later call to {@link #start(AdsMediaSource,
* DataSpec, Object, AdViewProvider, EventListener)}. If an ad is playing when the player is
* detached, update the ad playback state with the current playback position using {@link
* AdPlaybackState#withAdResumePositionUs(long)}.
*
* <p>If {@link EventListener#onAdPlaybackState(AdPlaybackState)} has been called, the
Expand Down Expand Up @@ -218,26 +218,31 @@ void start(
/**
* Stops using the ads loader for playback and deregisters the event listener. Called on the main
* thread by {@link AdsMediaSource}.
*
* @param adsMediaSource The ads media source requesting to stop loading/playing ads.
*/
void stop();
void stop(AdsMediaSource adsMediaSource);

/**
* Notifies the ads loader that preparation of an ad media period is complete. Called on the main
* thread by {@link AdsMediaSource}.
*
* @param adsMediaSource The ads media source for which preparation of ad media completed.
* @param adGroupIndex The index of the ad group.
* @param adIndexInAdGroup The index of the ad in the ad group.
*/
void handlePrepareComplete(int adGroupIndex, int adIndexInAdGroup);
void handlePrepareComplete(AdsMediaSource adsMediaSource, int adGroupIndex, int adIndexInAdGroup);

/**
* Notifies the ads loader that the player was not able to prepare media for a given ad.
* Implementations should update the ad playback state as the specified ad has failed to load.
* Called on the main thread by {@link AdsMediaSource}.
*
* @param adsMediaSource The ads media source for which preparation of ad media failed.
* @param adGroupIndex The index of the ad group.
* @param adIndexInAdGroup The index of the ad in the ad group.
* @param exception The preparation error.
*/
void handlePrepareError(int adGroupIndex, int adIndexInAdGroup, IOException exception);
void handlePrepareError(
AdsMediaSource adsMediaSource, int adGroupIndex, int adIndexInAdGroup, IOException exception);
}
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ protected void releaseSourceInternal() {
contentTimeline = null;
adPlaybackState = null;
adMediaSourceHolders = new AdMediaSourceHolder[0][];
mainHandler.post(adsLoader::stop);
mainHandler.post(() -> adsLoader.stop(/* adsMediaSource= */ this));
}

@Override
Expand Down Expand Up @@ -385,7 +385,9 @@ public void onPrepareComplete(MediaPeriodId mediaPeriodId) {
mainHandler.post(
() ->
adsLoader.handlePrepareComplete(
mediaPeriodId.adGroupIndex, mediaPeriodId.adIndexInAdGroup));
/* adsMediaSource= */ AdsMediaSource.this,
mediaPeriodId.adGroupIndex,
mediaPeriodId.adIndexInAdGroup));
}

@Override
Expand All @@ -402,7 +404,10 @@ public void onPrepareError(MediaPeriodId mediaPeriodId, IOException exception) {
mainHandler.post(
() ->
adsLoader.handlePrepareError(
mediaPeriodId.adGroupIndex, mediaPeriodId.adIndexInAdGroup, exception));
/* adsMediaSource= */ AdsMediaSource.this,
mediaPeriodId.adGroupIndex,
mediaPeriodId.adIndexInAdGroup,
exception));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9029,13 +9029,18 @@ public void start(
AdsLoader.EventListener eventListener) {}

@Override
public void stop() {}
public void stop(AdsMediaSource adsMediaSource) {}

@Override
public void handlePrepareComplete(int adGroupIndex, int adIndexInAdGroup) {}
public void handlePrepareComplete(
AdsMediaSource adsMediaSource, int adGroupIndex, int adIndexInAdGroup) {}

@Override
public void handlePrepareError(int adGroupIndex, int adIndexInAdGroup, IOException exception) {}
public void handlePrepareError(
AdsMediaSource adsMediaSource,
int adGroupIndex,
int adIndexInAdGroup,
IOException exception) {}
}

private static class FakeAdViewProvider implements AdsLoader.AdViewProvider {
Expand Down

0 comments on commit 0c301fe

Please sign in to comment.