ServerSideAdInsertionUtil


@UnstableApi
public final class ServerSideAdInsertionUtil


A static utility class with methods to work with server-side inserted ads.

Summary

Public methods

static AdPlaybackState
addAdGroupToAdPlaybackState(
    AdPlaybackState adPlaybackState,
    long fromPositionUs,
    long contentResumeOffsetUs,
    long[] adDurationsUs
)

Adds a new server-side inserted ad group to an AdPlaybackState.

static int
getAdCountInGroup(AdPlaybackState adPlaybackState, int adGroupIndex)

Returns the number of ads in an ad group, treating an unknown number as zero ads.

static long
getMediaPeriodPositionUs(
    long positionUs,
    MediaSource.MediaPeriodId mediaPeriodId,
    AdPlaybackState adPlaybackState
)

Returns the position in a MediaPeriod for a position in the underlying server-side inserted ads stream.

static long
getMediaPeriodPositionUsForAd(
    long positionUs,
    int adGroupIndex,
    int adIndexInAdGroup,
    AdPlaybackState adPlaybackState
)

Returns the position in an ad MediaPeriod for a position in the underlying server-side inserted ads stream.

static long
getMediaPeriodPositionUsForContent(
    long positionUs,
    int nextAdGroupIndex,
    AdPlaybackState adPlaybackState
)

Returns the position in a content MediaPeriod for a position in the underlying server-side inserted ads stream.

static long
getStreamPositionUs(Player player, AdPlaybackState adPlaybackState)

Returns the position in the underlying server-side inserted ads stream for the current playback position in the Player.

static long
getStreamPositionUs(
    long positionUs,
    MediaSource.MediaPeriodId mediaPeriodId,
    AdPlaybackState adPlaybackState
)

Returns the position in the underlying server-side inserted ads stream for a position in a MediaPeriod.

static long
getStreamPositionUsForAd(
    long positionUs,
    int adGroupIndex,
    int adIndexInAdGroup,
    AdPlaybackState adPlaybackState
)

Returns the position in the underlying server-side inserted ads stream for a position in an ad MediaPeriod.

static long
getStreamPositionUsForContent(
    long positionUs,
    int nextAdGroupIndex,
    AdPlaybackState adPlaybackState
)

Returns the position in the underlying server-side inserted ads stream for a position in a content MediaPeriod.

Public methods

addAdGroupToAdPlaybackState

public static AdPlaybackState addAdGroupToAdPlaybackState(
    AdPlaybackState adPlaybackState,
    long fromPositionUs,
    long contentResumeOffsetUs,
    long[] adDurationsUs
)

Adds a new server-side inserted ad group to an AdPlaybackState.

If the first ad with a non-zero duration is not the first ad in the group, all ads before that ad are marked as skipped.

Parameters
AdPlaybackState adPlaybackState

The existing AdPlaybackState.

long fromPositionUs

The position in the underlying server-side inserted ads stream at which the ad group starts, in microseconds.

long contentResumeOffsetUs

The timestamp offset which should be added to the content stream when resuming playback after the ad group. An offset of 0 collapses the ad group to a single insertion point, an offset of toPositionUs-fromPositionUs keeps the original stream timestamps after the ad group.

long[] adDurationsUs

The durations of the ads to be added to the group, in microseconds.

Returns
AdPlaybackState

The updated AdPlaybackState.

getAdCountInGroup

public static int getAdCountInGroup(AdPlaybackState adPlaybackState, int adGroupIndex)

Returns the number of ads in an ad group, treating an unknown number as zero ads.

Parameters
AdPlaybackState adPlaybackState

The AdPlaybackState.

int adGroupIndex

The index of the ad group.

Returns
int

The number of ads in the ad group.

getMediaPeriodPositionUs

public static long getMediaPeriodPositionUs(
    long positionUs,
    MediaSource.MediaPeriodId mediaPeriodId,
    AdPlaybackState adPlaybackState
)

Returns the position in a MediaPeriod for a position in the underlying server-side inserted ads stream.

Parameters
long positionUs

The position in the underlying server-side inserted ads stream, in microseconds.

MediaSource.MediaPeriodId mediaPeriodId

The MediaPeriodId of the MediaPeriod.

AdPlaybackState adPlaybackState

The AdPlaybackState defining the ad groups.

Returns
long

The position in the MediaPeriod, in microseconds.

getMediaPeriodPositionUsForAd

public static long getMediaPeriodPositionUsForAd(
    long positionUs,
    int adGroupIndex,
    int adIndexInAdGroup,
    AdPlaybackState adPlaybackState
)

Returns the position in an ad MediaPeriod for a position in the underlying server-side inserted ads stream.

Parameters
long positionUs

The position in the underlying server-side inserted ads stream, in microseconds.

int adGroupIndex

The ad group index of the ad.

int adIndexInAdGroup

The index of the ad in the ad group.

AdPlaybackState adPlaybackState

The AdPlaybackState defining the ad groups.

Returns
long

The position in the ad MediaPeriod, in microseconds.

getMediaPeriodPositionUsForContent

public static long getMediaPeriodPositionUsForContent(
    long positionUs,
    int nextAdGroupIndex,
    AdPlaybackState adPlaybackState
)

Returns the position in a content MediaPeriod for a position in the underlying server-side inserted ads stream.

Parameters
long positionUs

The position in the underlying server-side inserted ads stream, in microseconds.

int nextAdGroupIndex

The next ad group index after the content, or INDEX_UNSET if there is no following ad group. Ad groups from this index are not used to adjust the position.

AdPlaybackState adPlaybackState

The AdPlaybackState defining the ad groups.

Returns
long

The position in the content MediaPeriod, in microseconds.

getStreamPositionUs

public static long getStreamPositionUs(Player player, AdPlaybackState adPlaybackState)

Returns the position in the underlying server-side inserted ads stream for the current playback position in the Player.

Parameters
Player player

The Player.

AdPlaybackState adPlaybackState

The AdPlaybackState defining the ad groups.

Returns
long

The position in the underlying server-side inserted ads stream, in microseconds, or TIME_UNSET if it can't be determined.

getStreamPositionUs

public static long getStreamPositionUs(
    long positionUs,
    MediaSource.MediaPeriodId mediaPeriodId,
    AdPlaybackState adPlaybackState
)

Returns the position in the underlying server-side inserted ads stream for a position in a MediaPeriod.

Parameters
long positionUs

The position in the MediaPeriod, in microseconds.

MediaSource.MediaPeriodId mediaPeriodId

The MediaPeriodId of the MediaPeriod.

AdPlaybackState adPlaybackState

The AdPlaybackState defining the ad groups.

Returns
long

The position in the underlying server-side inserted ads stream, in microseconds.

getStreamPositionUsForAd

public static long getStreamPositionUsForAd(
    long positionUs,
    int adGroupIndex,
    int adIndexInAdGroup,
    AdPlaybackState adPlaybackState
)

Returns the position in the underlying server-side inserted ads stream for a position in an ad MediaPeriod.

Parameters
long positionUs

The position in the ad MediaPeriod, in microseconds.

int adGroupIndex

The ad group index of the ad.

int adIndexInAdGroup

The index of the ad in the ad group.

AdPlaybackState adPlaybackState

The AdPlaybackState defining the ad groups.

Returns
long

The position in the underlying server-side inserted ads stream, in microseconds.

getStreamPositionUsForContent

public static long getStreamPositionUsForContent(
    long positionUs,
    int nextAdGroupIndex,
    AdPlaybackState adPlaybackState
)

Returns the position in the underlying server-side inserted ads stream for a position in a content MediaPeriod.

Parameters
long positionUs

The position in the content MediaPeriod, in microseconds.

int nextAdGroupIndex

The next ad group index after the content, or INDEX_UNSET if there is no following ad group. Ad groups from this index are not used to adjust the position.

AdPlaybackState adPlaybackState

The AdPlaybackState defining the ad groups.

Returns
long

The position in the underlying server-side inserted ads stream, in microseconds.