# ServerSideAdInsertionUtil

Artifact: [androidx.media3:media3-exoplayer](https://developer.android.com/jetpack/androidx/releases/media3) [View Source](https://cs.android.com/search?q=file:androidx/media3/exoplayer/source/ads/ServerSideAdInsertionUtil.java+class:androidx.media3.exoplayer.source.ads.ServerSideAdInsertionUtil)

*** ** * ** ***

<br />

```
@UnstableApi
class ServerSideAdInsertionUtil
```

<br />

*** ** * ** ***

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

## Summary

| ### Public functions |
|---|---|
| `java-static https://developer.android.com/reference/kotlin/androidx/media3/common/AdPlaybackState!` | `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/ads/ServerSideAdInsertionUtil#addAdGroupToAdPlaybackState(androidx.media3.common.AdPlaybackState,long,long,long...)( adPlaybackState: https://developer.android.com/reference/kotlin/androidx/media3/common/AdPlaybackState!, fromPositionUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, contentResumeOffsetUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, adDurationsUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html>! )` Adds a new server-side inserted ad group to an `https://developer.android.com/reference/kotlin/androidx/media3/common/AdPlaybackState`. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/ads/ServerSideAdInsertionUtil#getAdCountInGroup(androidx.media3.common.AdPlaybackState,int)(adPlaybackState: https://developer.android.com/reference/kotlin/androidx/media3/common/AdPlaybackState!, adGroupIndex: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Returns the number of ads in an ad group, treating an unknown number as zero ads. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/ads/ServerSideAdInsertionUtil#getMediaPeriodPositionUs(long,androidx.media3.exoplayer.source.MediaSource.MediaPeriodId,androidx.media3.common.AdPlaybackState)( positionUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, mediaPeriodId: https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/MediaSource.MediaPeriodId!, adPlaybackState: https://developer.android.com/reference/kotlin/androidx/media3/common/AdPlaybackState! )` Returns the position in a `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/MediaPeriod` for a position in the underlying server-side inserted ads stream. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/ads/ServerSideAdInsertionUtil#getMediaPeriodPositionUsForAd(long,int,int,androidx.media3.common.AdPlaybackState)( positionUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, adGroupIndex: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, adIndexInAdGroup: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, adPlaybackState: https://developer.android.com/reference/kotlin/androidx/media3/common/AdPlaybackState! )` Returns the position in an ad `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/MediaPeriod` for a position in the underlying server-side inserted ads stream. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/ads/ServerSideAdInsertionUtil#getMediaPeriodPositionUsForContent(long,int,androidx.media3.common.AdPlaybackState)( positionUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, nextAdGroupIndex: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, adPlaybackState: https://developer.android.com/reference/kotlin/androidx/media3/common/AdPlaybackState! )` Returns the position in a content `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/MediaPeriod` for a position in the underlying server-side inserted ads stream. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/ads/ServerSideAdInsertionUtil#getStreamPositionUs(androidx.media3.common.Player,java.lang.Object)(player: https://developer.android.com/reference/kotlin/androidx/media3/common/Player!, adsId: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html!)` Returns the position in the underlying server-side inserted ads stream of the given ads ID for the current playback position in the `https://developer.android.com/reference/kotlin/androidx/media3/common/Player`. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/ads/ServerSideAdInsertionUtil#getStreamPositionUs(long,androidx.media3.exoplayer.source.MediaSource.MediaPeriodId,androidx.media3.common.AdPlaybackState)( positionUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, mediaPeriodId: https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/MediaSource.MediaPeriodId!, adPlaybackState: https://developer.android.com/reference/kotlin/androidx/media3/common/AdPlaybackState! )` Returns the position in the underlying server-side inserted ads stream for a position in a `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/MediaPeriod`. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/ads/ServerSideAdInsertionUtil#getStreamPositionUsForAd(long,int,int,androidx.media3.common.AdPlaybackState)( positionUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, adGroupIndex: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, adIndexInAdGroup: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, adPlaybackState: https://developer.android.com/reference/kotlin/androidx/media3/common/AdPlaybackState! )` Returns the position in the underlying server-side inserted ads stream for a position in an ad `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/MediaPeriod`. |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/ads/ServerSideAdInsertionUtil#getStreamPositionUsForContent(long,int,androidx.media3.common.AdPlaybackState)( positionUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html, nextAdGroupIndex: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, adPlaybackState: https://developer.android.com/reference/kotlin/androidx/media3/common/AdPlaybackState! )` Returns the position in the underlying server-side inserted ads stream for a position in a content `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/MediaPeriod`. |

## Public functions

### addAdGroupToAdPlaybackState

```
java-static fun addAdGroupToAdPlaybackState(
    adPlaybackState: AdPlaybackState!,
    fromPositionUs: Long,
    contentResumeOffsetUs: Long,
    adDurationsUs: LongArray<Long>!
): AdPlaybackState!
```

Adds a new server-side inserted ad group to an `https://developer.android.com/reference/kotlin/androidx/media3/common/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: https://developer.android.com/reference/kotlin/androidx/media3/common/AdPlaybackState!` | The existing `https://developer.android.com/reference/kotlin/androidx/media3/common/AdPlaybackState`. |
| `fromPositionUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The position in the underlying server-side inserted ads stream at which the ad group starts, in microseconds. |
| `contentResumeOffsetUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | 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. |
| `adDurationsUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html>!` | The durations of the ads to be added to the group, in microseconds. |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/media3/common/AdPlaybackState!` | The updated `https://developer.android.com/reference/kotlin/androidx/media3/common/AdPlaybackState`. |

### getAdCountInGroup

```
java-static fun getAdCountInGroup(adPlaybackState: AdPlaybackState!, adGroupIndex: Int): Int
```

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

| Parameters |
|---|---|
| `adPlaybackState: https://developer.android.com/reference/kotlin/androidx/media3/common/AdPlaybackState!` | The `https://developer.android.com/reference/kotlin/androidx/media3/common/AdPlaybackState`. |
| `adGroupIndex: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The index of the ad group. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The number of ads in the ad group. |

### getMediaPeriodPositionUs

```
java-static fun getMediaPeriodPositionUs(
    positionUs: Long,
    mediaPeriodId: MediaSource.MediaPeriodId!,
    adPlaybackState: AdPlaybackState!
): Long
```

Returns the position in a `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/MediaPeriod` for a position in the underlying server-side inserted ads stream.

| Parameters |
|---|---|
| `positionUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The position in the underlying server-side inserted ads stream, in microseconds. |
| `mediaPeriodId: https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/MediaSource.MediaPeriodId!` | The `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/MediaSource.MediaPeriodId` of the `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/MediaPeriod`. |
| `adPlaybackState: https://developer.android.com/reference/kotlin/androidx/media3/common/AdPlaybackState!` | The `https://developer.android.com/reference/kotlin/androidx/media3/common/AdPlaybackState` defining the ad groups. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The position in the `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/MediaPeriod`, in microseconds. |

### getMediaPeriodPositionUsForAd

```
java-static fun getMediaPeriodPositionUsForAd(
    positionUs: Long,
    adGroupIndex: Int,
    adIndexInAdGroup: Int,
    adPlaybackState: AdPlaybackState!
): Long
```

Returns the position in an ad `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/MediaPeriod` for a position in the underlying server-side inserted ads stream.

| Parameters |
|---|---|
| `positionUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The position in the underlying server-side inserted ads stream, in microseconds. |
| `adGroupIndex: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The ad group index of the ad. |
| `adIndexInAdGroup: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The index of the ad in the ad group. |
| `adPlaybackState: https://developer.android.com/reference/kotlin/androidx/media3/common/AdPlaybackState!` | The `https://developer.android.com/reference/kotlin/androidx/media3/common/AdPlaybackState` defining the ad groups. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The position in the ad `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/MediaPeriod`, in microseconds. |

### getMediaPeriodPositionUsForContent

```
java-static fun getMediaPeriodPositionUsForContent(
    positionUs: Long,
    nextAdGroupIndex: Int,
    adPlaybackState: AdPlaybackState!
): Long
```

Returns the position in a content `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/MediaPeriod` for a position in the underlying server-side inserted ads stream.

| Parameters |
|---|---|
| `positionUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The position in the underlying server-side inserted ads stream, in microseconds. |
| `nextAdGroupIndex: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The next ad group index after the content, or `https://developer.android.com/reference/kotlin/androidx/media3/common/C#INDEX_UNSET()` if there is no following ad group. Ad groups from this index are not used to adjust the position. |
| `adPlaybackState: https://developer.android.com/reference/kotlin/androidx/media3/common/AdPlaybackState!` | The `https://developer.android.com/reference/kotlin/androidx/media3/common/AdPlaybackState` defining the ad groups. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The position in the content `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/MediaPeriod`, in microseconds. |

### getStreamPositionUs

```
java-static fun getStreamPositionUs(player: Player!, adsId: Any!): Long
```

Returns the position in the underlying server-side inserted ads stream of the given ads ID for the current playback position in the `https://developer.android.com/reference/kotlin/androidx/media3/common/Player`.

| Parameters |
|---|---|
| `player: https://developer.android.com/reference/kotlin/androidx/media3/common/Player!` | The `https://developer.android.com/reference/kotlin/androidx/media3/common/Player`. |
| `adsId: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html!` | The ads ID of the period for which to get the stream position. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The position in the underlying server-side inserted ads stream, in microseconds, or `https://developer.android.com/reference/kotlin/androidx/media3/common/C#TIME_UNSET()` if it can't be determined. |

### getStreamPositionUs

```
java-static fun getStreamPositionUs(
    positionUs: Long,
    mediaPeriodId: MediaSource.MediaPeriodId!,
    adPlaybackState: AdPlaybackState!
): Long
```

Returns the position in the underlying server-side inserted ads stream for a position in a `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/MediaPeriod`.

| Parameters |
|---|---|
| `positionUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The position in the `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/MediaPeriod`, in microseconds. |
| `mediaPeriodId: https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/MediaSource.MediaPeriodId!` | The `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/MediaSource.MediaPeriodId` of the `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/MediaPeriod`. |
| `adPlaybackState: https://developer.android.com/reference/kotlin/androidx/media3/common/AdPlaybackState!` | The `https://developer.android.com/reference/kotlin/androidx/media3/common/AdPlaybackState` defining the ad groups. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The position in the underlying server-side inserted ads stream, in microseconds. |

### getStreamPositionUsForAd

```
java-static fun getStreamPositionUsForAd(
    positionUs: Long,
    adGroupIndex: Int,
    adIndexInAdGroup: Int,
    adPlaybackState: AdPlaybackState!
): Long
```

Returns the position in the underlying server-side inserted ads stream for a position in an ad `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/MediaPeriod`.

| Parameters |
|---|---|
| `positionUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The position in the ad `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/MediaPeriod`, in microseconds. |
| `adGroupIndex: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The ad group index of the ad. |
| `adIndexInAdGroup: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The index of the ad in the ad group. |
| `adPlaybackState: https://developer.android.com/reference/kotlin/androidx/media3/common/AdPlaybackState!` | The `https://developer.android.com/reference/kotlin/androidx/media3/common/AdPlaybackState` defining the ad groups. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The position in the underlying server-side inserted ads stream, in microseconds. |

### getStreamPositionUsForContent

```
java-static fun getStreamPositionUsForContent(
    positionUs: Long,
    nextAdGroupIndex: Int,
    adPlaybackState: AdPlaybackState!
): Long
```

Returns the position in the underlying server-side inserted ads stream for a position in a content `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/MediaPeriod`.

| Parameters |
|---|---|
| `positionUs: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The position in the content `https://developer.android.com/reference/kotlin/androidx/media3/exoplayer/source/MediaPeriod`, in microseconds. |
| `nextAdGroupIndex: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The next ad group index after the content, or `https://developer.android.com/reference/kotlin/androidx/media3/common/C#INDEX_UNSET()` if there is no following ad group. Ad groups from this index are not used to adjust the position. |
| `adPlaybackState: https://developer.android.com/reference/kotlin/androidx/media3/common/AdPlaybackState!` | The `https://developer.android.com/reference/kotlin/androidx/media3/common/AdPlaybackState` defining the ad groups. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | The position in the underlying server-side inserted ads stream, in microseconds. |