mirror of
https://github.com/samsonjs/media.git
synced 2026-04-27 15:07:40 +00:00
Separate ads rendering and AdsManager init
In a later change it will be necessary to be able to destroy the ads manager if all ads are skipped while creating ads rendering settings. This change prepares for doing that by not having the ads manager passed into the method (so the caller can null or initialize it). PiperOrigin-RevId: 315488830
This commit is contained in:
parent
5d74fced1d
commit
80f4197e0f
1 changed files with 32 additions and 28 deletions
|
|
@ -383,7 +383,7 @@ public final class ImaAdsLoader
|
||||||
private int lastVolumePercentage;
|
private int lastVolumePercentage;
|
||||||
|
|
||||||
@Nullable private AdsManager adsManager;
|
@Nullable private AdsManager adsManager;
|
||||||
private boolean initializedAdsManager;
|
private boolean isAdsManagerInitialized;
|
||||||
private boolean hasAdPlaybackState;
|
private boolean hasAdPlaybackState;
|
||||||
@Nullable private AdLoadException pendingAdLoadError;
|
@Nullable private AdLoadException pendingAdLoadError;
|
||||||
private Timeline timeline;
|
private Timeline timeline;
|
||||||
|
|
@ -980,9 +980,16 @@ public final class ImaAdsLoader
|
||||||
if (contentDurationUs != C.TIME_UNSET) {
|
if (contentDurationUs != C.TIME_UNSET) {
|
||||||
adPlaybackState = adPlaybackState.withContentDurationUs(contentDurationUs);
|
adPlaybackState = adPlaybackState.withContentDurationUs(contentDurationUs);
|
||||||
}
|
}
|
||||||
if (!initializedAdsManager && adsManager != null) {
|
@Nullable AdsManager adsManager = this.adsManager;
|
||||||
initializedAdsManager = true;
|
if (!isAdsManagerInitialized && adsManager != null) {
|
||||||
initializeAdsManager(adsManager);
|
isAdsManagerInitialized = true;
|
||||||
|
AdsRenderingSettings adsRenderingSettings = setupAdsRendering();
|
||||||
|
adsManager.init(adsRenderingSettings);
|
||||||
|
adsManager.start();
|
||||||
|
updateAdPlaybackState();
|
||||||
|
if (DEBUG) {
|
||||||
|
Log.d(TAG, "Initialized with ads rendering settings: " + adsRenderingSettings);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
handleTimelineOrPositionChanged();
|
handleTimelineOrPositionChanged();
|
||||||
}
|
}
|
||||||
|
|
@ -1047,7 +1054,8 @@ public final class ImaAdsLoader
|
||||||
|
|
||||||
// Internal methods.
|
// Internal methods.
|
||||||
|
|
||||||
private void initializeAdsManager(AdsManager adsManager) {
|
/** Configures ads rendering for starting playback, returning the settings for the IMA SDK. */
|
||||||
|
private AdsRenderingSettings setupAdsRendering() {
|
||||||
AdsRenderingSettings adsRenderingSettings = imaFactory.createAdsRenderingSettings();
|
AdsRenderingSettings adsRenderingSettings = imaFactory.createAdsRenderingSettings();
|
||||||
adsRenderingSettings.setEnablePreloading(true);
|
adsRenderingSettings.setEnablePreloading(true);
|
||||||
adsRenderingSettings.setMimeTypes(supportedMimeTypes);
|
adsRenderingSettings.setMimeTypes(supportedMimeTypes);
|
||||||
|
|
@ -1063,13 +1071,19 @@ public final class ImaAdsLoader
|
||||||
}
|
}
|
||||||
|
|
||||||
// Skip ads based on the start position as required.
|
// Skip ads based on the start position as required.
|
||||||
long[] adGroupTimesUs = getAdGroupTimesUs(adsManager.getAdCuePoints());
|
long[] adGroupTimesUs = adPlaybackState.adGroupTimesUs;
|
||||||
long contentPositionMs =
|
long contentPositionMs =
|
||||||
getContentPeriodPositionMs(Assertions.checkNotNull(player), timeline, period);
|
getContentPeriodPositionMs(Assertions.checkNotNull(player), timeline, period);
|
||||||
int adGroupIndexForPosition =
|
int adGroupIndexForPosition =
|
||||||
adPlaybackState.getAdGroupIndexForPositionUs(
|
adPlaybackState.getAdGroupIndexForPositionUs(
|
||||||
C.msToUs(contentPositionMs), C.msToUs(contentDurationMs));
|
C.msToUs(contentPositionMs), C.msToUs(contentDurationMs));
|
||||||
if (adGroupIndexForPosition > 0 && adGroupIndexForPosition != C.INDEX_UNSET) {
|
if (adGroupIndexForPosition != C.INDEX_UNSET) {
|
||||||
|
// Provide the player's initial position to trigger loading and playing the ad. If there are
|
||||||
|
// no midrolls, we are playing a preroll and any pending content position wouldn't be cleared.
|
||||||
|
if (hasMidrollAdGroups(adGroupTimesUs)) {
|
||||||
|
pendingContentPositionMs = contentPositionMs;
|
||||||
|
}
|
||||||
|
if (adGroupIndexForPosition > 0) {
|
||||||
// Skip any ad groups before the one at or immediately before the playback position.
|
// Skip any ad groups before the one at or immediately before the playback position.
|
||||||
for (int i = 0; i < adGroupIndexForPosition; i++) {
|
for (int i = 0; i < adGroupIndexForPosition; i++) {
|
||||||
adPlaybackState = adPlaybackState.withSkippedAdGroup(i);
|
adPlaybackState = adPlaybackState.withSkippedAdGroup(i);
|
||||||
|
|
@ -1081,18 +1095,8 @@ public final class ImaAdsLoader
|
||||||
double midpointTimeUs = (adGroupForPositionTimeUs + adGroupBeforeTimeUs) / 2d;
|
double midpointTimeUs = (adGroupForPositionTimeUs + adGroupBeforeTimeUs) / 2d;
|
||||||
adsRenderingSettings.setPlayAdsAfterTime(midpointTimeUs / C.MICROS_PER_SECOND);
|
adsRenderingSettings.setPlayAdsAfterTime(midpointTimeUs / C.MICROS_PER_SECOND);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (adGroupIndexForPosition != C.INDEX_UNSET && hasMidrollAdGroups(adGroupTimesUs)) {
|
|
||||||
// Provide the player's initial position to trigger loading and playing the ad.
|
|
||||||
pendingContentPositionMs = contentPositionMs;
|
|
||||||
}
|
|
||||||
|
|
||||||
adsManager.init(adsRenderingSettings);
|
|
||||||
adsManager.start();
|
|
||||||
updateAdPlaybackState();
|
|
||||||
if (DEBUG) {
|
|
||||||
Log.d(TAG, "Initialized with ads rendering settings: " + adsRenderingSettings);
|
|
||||||
}
|
}
|
||||||
|
return adsRenderingSettings;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleAdEvent(AdEvent adEvent) {
|
private void handleAdEvent(AdEvent adEvent) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue