ServerSideAdInsertionMediaSource


@UnstableApi
class ServerSideAdInsertionMediaSource : BaseMediaSource, MediaSource.MediaSourceCaller, MediaSourceEventListener, DrmSessionEventListener


A MediaSource for server-side inserted ad breaks.

The media source publishes a Timeline for the wrapped MediaSource with the server-side inserted ad breaks and ensures that playback continues seamlessly with the wrapped media across all transitions.

The ad breaks need to be specified using setAdPlaybackStates and can be updated during playback.

Summary

Nested types

Receives ad playback state update requests when the Timeline of the content media source has changed.

Public constructors

Creates the media source.

Public functions

Boolean

Returns whether the MediaItem for this source can be updated with the provided item.

MediaPeriod!
createPeriod(
    id: MediaSource.MediaPeriodId!,
    allocator: Allocator!,
    startPositionUs: Long
)

Returns a new MediaPeriod identified by periodId.

MediaItem!

Returns the MediaItem whose media is provided by the source.

Unit

Throws any pending error encountered while loading or refreshing source information.

Unit
onDownstreamFormatChanged(
    windowIndex: Int,
    mediaPeriodId: MediaSource.MediaPeriodId?,
    mediaLoadData: MediaLoadData!
)

Called when a downstream format change occurs (i.e. when the format of the media being read from one or more SampleStreams provided by the source changes).

Unit
onDrmKeysLoaded(
    windowIndex: Int,
    mediaPeriodId: MediaSource.MediaPeriodId?
)

Called each time keys are loaded.

Unit
onDrmKeysRemoved(
    windowIndex: Int,
    mediaPeriodId: MediaSource.MediaPeriodId?
)

Called each time offline keys are removed.

Unit
onDrmKeysRestored(
    windowIndex: Int,
    mediaPeriodId: MediaSource.MediaPeriodId?
)

Called each time offline keys are restored.

Unit
onDrmSessionAcquired(
    windowIndex: Int,
    mediaPeriodId: MediaSource.MediaPeriodId?,
    @DrmSession.State state: Int
)

Called each time a drm session is acquired.

Unit
onDrmSessionManagerError(
    windowIndex: Int,
    mediaPeriodId: MediaSource.MediaPeriodId?,
    error: Exception!
)

Called when a drm error occurs.

Unit
onDrmSessionReleased(
    windowIndex: Int,
    mediaPeriodId: MediaSource.MediaPeriodId?
)

Called each time a drm session is released.

Unit
onLoadCanceled(
    windowIndex: Int,
    mediaPeriodId: MediaSource.MediaPeriodId?,
    loadEventInfo: LoadEventInfo!,
    mediaLoadData: MediaLoadData!
)

Called when a load is canceled.

Unit
onLoadCompleted(
    windowIndex: Int,
    mediaPeriodId: MediaSource.MediaPeriodId?,
    loadEventInfo: LoadEventInfo!,
    mediaLoadData: MediaLoadData!
)

Called when a load ends.

Unit
onLoadError(
    windowIndex: Int,
    mediaPeriodId: MediaSource.MediaPeriodId?,
    loadEventInfo: LoadEventInfo!,
    mediaLoadData: MediaLoadData!,
    error: IOException!,
    wasCanceled: Boolean
)

Called when a load error occurs.

Unit
onLoadStarted(
    windowIndex: Int,
    mediaPeriodId: MediaSource.MediaPeriodId?,
    loadEventInfo: LoadEventInfo!,
    mediaLoadData: MediaLoadData!
)

Called when a load begins.

Unit
onSourceInfoRefreshed(source: MediaSource!, timeline: Timeline!)

Called when the Timeline has been refreshed.

Unit
onUpstreamDiscarded(
    windowIndex: Int,
    mediaPeriodId: MediaSource.MediaPeriodId!,
    mediaLoadData: MediaLoadData!
)

Called when data is removed from the back of a media buffer, typically so that it can be re-buffered in a different format.

Unit
releasePeriod(mediaPeriod: MediaPeriod!)

Releases the period.

Unit
setAdPlaybackStates(
    adPlaybackStates: ImmutableMap<Any!, AdPlaybackState!>!,
    contentTimeline: Timeline!
)

Sets the map of ad playback states published by this source.

Unit

Updates the MediaItem for this source.

Protected functions

Unit

Disables the source, see disable.

Unit

Enables the source, see enable.

Unit
prepareSourceInternal(mediaTransferListener: TransferListener?)

Starts source preparation and enables the source, see prepareSource.

Unit

Releases the source, see releaseSource.

Inherited functions

From androidx.media3.exoplayer.source.BaseMediaSource
Unit
addDrmEventListener(
    handler: Handler!,
    eventListener: DrmSessionEventListener!
)

Adds a DrmSessionEventListener to the list of listeners which are notified of DRM events for this media source.

Unit
addEventListener(
    handler: Handler!,
    eventListener: MediaSourceEventListener!
)

Adds a MediaSourceEventListener to the list of listeners which are notified of media source events.

DrmSessionEventListener.EventDispatcher!

Returns a DrmSessionEventListener.EventDispatcher which dispatches all events to the registered listeners with the specified MediaPeriodId

DrmSessionEventListener.EventDispatcher!
createDrmEventDispatcher(
    windowIndex: Int,
    mediaPeriodId: MediaSource.MediaPeriodId?
)

Returns a DrmSessionEventListener.EventDispatcher which dispatches all events to the registered listeners with the specified window index and MediaPeriodId.

MediaSourceEventListener.EventDispatcher!

Returns a MediaSourceEventListener.EventDispatcher which dispatches all events to the registered listeners with the specified MediaPeriodId.

MediaSourceEventListener.EventDispatcher!
createEventDispatcher(
    mediaPeriodId: MediaSource.MediaPeriodId!,
    mediaTimeOffsetMs: Long
)

This function is deprecated.

Use createEventDispatcher instead.

MediaSourceEventListener.EventDispatcher!
createEventDispatcher(
    windowIndex: Int,
    mediaPeriodId: MediaSource.MediaPeriodId?
)

Returns a MediaSourceEventListener.EventDispatcher which dispatches all events to the registered listeners with the specified window index and MediaPeriodId.

MediaSourceEventListener.EventDispatcher!
createEventDispatcher(
    windowIndex: Int,
    mediaPeriodId: MediaSource.MediaPeriodId?,
    mediaTimeOffsetMs: Long
)

This function is deprecated.

Use createEventDispatcher instead.

Unit

Disables the source for the creation of MediaPeriods.

Unit

Enables the source for the creation of MediaPeriods.

Boolean

Returns whether the source is enabled.

Unit
prepareSource(
    caller: MediaSource.MediaSourceCaller!,
    mediaTransferListener: TransferListener?
)

This function is deprecated.

Implement prepareSource instead.

Unit
prepareSource(
    caller: MediaSource.MediaSourceCaller!,
    mediaTransferListener: TransferListener?,
    playerId: PlayerId!
)

Registers a MediaSourceCaller.

Boolean

Returns whether the source has prepareSource called.

Unit

Updates timeline and manifest and notifies all listeners of the update.

Unit

Unregisters a caller, and disables and releases the source if no longer required.

Unit

Removes a DrmSessionEventListener from the list of listeners which are notified of DRM events for this media source.

Unit

Removes a MediaSourceEventListener from the list of listeners which are notified of media source events.

From androidx.media3.exoplayer.drm.DrmSessionEventListener
Unit
onDrmSessionAcquired(
    windowIndex: Int,
    mediaPeriodId: MediaSource.MediaPeriodId?
)

This function is deprecated.

Implement onDrmSessionAcquired instead.

From androidx.media3.exoplayer.source.MediaSource
Timeline?

Returns the initial placeholder timeline that is returned immediately when the real timeline is not yet known, or null to let the player create an initial timeline.

Boolean

Returns true if the media source is guaranteed to never have zero or more than one window.

Public constructors

ServerSideAdInsertionMediaSource

ServerSideAdInsertionMediaSource(
    mediaSource: MediaSource!,
    adPlaybackStateUpdater: ServerSideAdInsertionMediaSource.AdPlaybackStateUpdater?
)

Creates the media source.

Parameters
mediaSource: MediaSource!

The MediaSource to wrap.

adPlaybackStateUpdater: ServerSideAdInsertionMediaSource.AdPlaybackStateUpdater?

The optional AdPlaybackStateUpdater to be called before a source refresh is published.

Public functions

canUpdateMediaItem

fun canUpdateMediaItem(mediaItem: MediaItem!): Boolean

Returns whether the MediaItem for this source can be updated with the provided item.

Should not be called directly from application code.

This method must be called on the application thread.

Parameters
mediaItem: MediaItem!

The new MediaItem.

Returns
Boolean

Whether the source can be updated using this item.

createPeriod

fun createPeriod(
    id: MediaSource.MediaPeriodId!,
    allocator: Allocator!,
    startPositionUs: Long
): MediaPeriod!

Returns a new MediaPeriod identified by periodId.

Should not be called directly from application code.

This method must be called on the playback thread and only if the source is enabled.

Parameters
id: MediaSource.MediaPeriodId!

The identifier of the period.

allocator: Allocator!

An Allocator from which to obtain media buffer allocations.

startPositionUs: Long

The expected start position, in microseconds.

Returns
MediaPeriod!

A new MediaPeriod.

getMediaItem

fun getMediaItem(): MediaItem!

Returns the MediaItem whose media is provided by the source.

Should not be called directly from application code.

This method must be called on the application thread.

maybeThrowSourceInfoRefreshError

fun maybeThrowSourceInfoRefreshError(): Unit

Throws any pending error encountered while loading or refreshing source information.

Should not be called directly from application code.

This method must be called on the playback thread and only after prepareSource.

onDownstreamFormatChanged

fun onDownstreamFormatChanged(
    windowIndex: Int,
    mediaPeriodId: MediaSource.MediaPeriodId?,
    mediaLoadData: MediaLoadData!
): Unit

Called when a downstream format change occurs (i.e. when the format of the media being read from one or more SampleStreams provided by the source changes).

Parameters
windowIndex: Int

The window index in the timeline of the media source this load belongs to.

mediaPeriodId: MediaSource.MediaPeriodId?

The MediaPeriodId the media belongs to.

mediaLoadData: MediaLoadData!

The MediaLoadData defining the newly selected downstream data.

onDrmKeysLoaded

fun onDrmKeysLoaded(
    windowIndex: Int,
    mediaPeriodId: MediaSource.MediaPeriodId?
): Unit

Called each time keys are loaded.

Parameters
windowIndex: Int

The window index in the timeline this media period belongs to.

mediaPeriodId: MediaSource.MediaPeriodId?

The MediaPeriodId associated with the drm session.

onDrmKeysRemoved

fun onDrmKeysRemoved(
    windowIndex: Int,
    mediaPeriodId: MediaSource.MediaPeriodId?
): Unit

Called each time offline keys are removed.

Parameters
windowIndex: Int

The window index in the timeline this media period belongs to.

mediaPeriodId: MediaSource.MediaPeriodId?

The MediaPeriodId associated with the drm session.

onDrmKeysRestored

fun onDrmKeysRestored(
    windowIndex: Int,
    mediaPeriodId: MediaSource.MediaPeriodId?
): Unit

Called each time offline keys are restored.

Parameters
windowIndex: Int

The window index in the timeline this media period belongs to.

mediaPeriodId: MediaSource.MediaPeriodId?

The MediaPeriodId associated with the drm session.

onDrmSessionAcquired

fun onDrmSessionAcquired(
    windowIndex: Int,
    mediaPeriodId: MediaSource.MediaPeriodId?,
    @DrmSession.State state: Int
): Unit

Called each time a drm session is acquired.

Parameters
windowIndex: Int

The window index in the timeline this media period belongs to.

mediaPeriodId: MediaSource.MediaPeriodId?

The MediaPeriodId associated with the drm session.

@DrmSession.State state: Int

The DrmSession.State of the session when the acquisition completed.

onDrmSessionManagerError

fun onDrmSessionManagerError(
    windowIndex: Int,
    mediaPeriodId: MediaSource.MediaPeriodId?,
    error: Exception!
): Unit

Called when a drm error occurs.

This method being called does not indicate that playback has failed, or that it will fail. The player may be able to recover from the error and continue. Hence applications should not implement this method to display a user visible error or initiate an application level retry (onPlayerError is the appropriate place to implement such behavior). This method is called to provide the application with an opportunity to log the error if it wishes to do so.

Parameters
windowIndex: Int

The window index in the timeline this media period belongs to.

mediaPeriodId: MediaSource.MediaPeriodId?

The MediaPeriodId associated with the drm session.

error: Exception!

The corresponding exception.

onDrmSessionReleased

fun onDrmSessionReleased(
    windowIndex: Int,
    mediaPeriodId: MediaSource.MediaPeriodId?
): Unit

Called each time a drm session is released.

Parameters
windowIndex: Int

The window index in the timeline this media period belongs to.

mediaPeriodId: MediaSource.MediaPeriodId?

The MediaPeriodId associated with the drm session.

onLoadCanceled

fun onLoadCanceled(
    windowIndex: Int,
    mediaPeriodId: MediaSource.MediaPeriodId?,
    loadEventInfo: LoadEventInfo!,
    mediaLoadData: MediaLoadData!
): Unit

Called when a load is canceled.

Parameters
windowIndex: Int

The window index in the timeline of the media source this load belongs to.

mediaPeriodId: MediaSource.MediaPeriodId?

The MediaPeriodId this load belongs to. Null if the load does not belong to a specific media period.

loadEventInfo: LoadEventInfo!

The LoadEventInfo corresponding to the event. The values of elapsedRealtimeMs and bytesLoaded are relative to the corresponding onLoadStarted event.

mediaLoadData: MediaLoadData!

The MediaLoadData defining the data being loaded.

onLoadCompleted

fun onLoadCompleted(
    windowIndex: Int,
    mediaPeriodId: MediaSource.MediaPeriodId?,
    loadEventInfo: LoadEventInfo!,
    mediaLoadData: MediaLoadData!
): Unit

Called when a load ends.

Parameters
windowIndex: Int

The window index in the timeline of the media source this load belongs to.

mediaPeriodId: MediaSource.MediaPeriodId?

The MediaPeriodId this load belongs to. Null if the load does not belong to a specific media period.

loadEventInfo: LoadEventInfo!

The LoadEventInfo corresponding to the event. The values of elapsedRealtimeMs and bytesLoaded are relative to the corresponding onLoadStarted event.

mediaLoadData: MediaLoadData!

The MediaLoadData defining the data being loaded.

onLoadError

fun onLoadError(
    windowIndex: Int,
    mediaPeriodId: MediaSource.MediaPeriodId?,
    loadEventInfo: LoadEventInfo!,
    mediaLoadData: MediaLoadData!,
    error: IOException!,
    wasCanceled: Boolean
): Unit

Called when a load error occurs.

The error may or may not have resulted in the load being canceled, as indicated by the wasCanceled parameter. If the load was canceled, onLoadCanceled will not be called in addition to this method.

This method being called does not indicate that playback has failed, or that it will fail. The player may be able to recover from the error. Hence applications should not implement this method to display a user visible error or initiate an application level retry. onPlayerError is the appropriate place to implement such behavior. This method is called to provide the application with an opportunity to log the error if it wishes to do so.

Parameters
windowIndex: Int

The window index in the timeline of the media source this load belongs to.

mediaPeriodId: MediaSource.MediaPeriodId?

The MediaPeriodId this load belongs to. Null if the load does not belong to a specific media period.

loadEventInfo: LoadEventInfo!

The LoadEventInfo corresponding to the event. The values of elapsedRealtimeMs and bytesLoaded are relative to the corresponding onLoadStarted event.

mediaLoadData: MediaLoadData!

The MediaLoadData defining the data being loaded.

error: IOException!

The load error.

wasCanceled: Boolean

Whether the load was canceled as a result of the error.

onLoadStarted

fun onLoadStarted(
    windowIndex: Int,
    mediaPeriodId: MediaSource.MediaPeriodId?,
    loadEventInfo: LoadEventInfo!,
    mediaLoadData: MediaLoadData!
): Unit

Called when a load begins.

Parameters
windowIndex: Int

The window index in the timeline of the media source this load belongs to.

mediaPeriodId: MediaSource.MediaPeriodId?

The MediaPeriodId this load belongs to. Null if the load does not belong to a specific media period.

loadEventInfo: LoadEventInfo!

The LoadEventInfo corresponding to the event. The value of uri won't reflect potential redirection yet and responseHeaders will be empty.

mediaLoadData: MediaLoadData!

The MediaLoadData defining the data being loaded.

onSourceInfoRefreshed

fun onSourceInfoRefreshed(source: MediaSource!, timeline: Timeline!): Unit

Called when the Timeline has been refreshed.

Called on the playback thread.

Parameters
source: MediaSource!

The MediaSource whose info has been refreshed.

timeline: Timeline!

The source's timeline.

onUpstreamDiscarded

fun onUpstreamDiscarded(
    windowIndex: Int,
    mediaPeriodId: MediaSource.MediaPeriodId!,
    mediaLoadData: MediaLoadData!
): Unit

Called when data is removed from the back of a media buffer, typically so that it can be re-buffered in a different format.

Parameters
windowIndex: Int

The window index in the timeline of the media source this load belongs to.

mediaPeriodId: MediaSource.MediaPeriodId!

The MediaPeriodId the media belongs to.

mediaLoadData: MediaLoadData!

The MediaLoadData defining the media being discarded.

releasePeriod

fun releasePeriod(mediaPeriod: MediaPeriod!): Unit

Releases the period.

Should not be called directly from application code.

This method must be called on the playback thread.

Parameters
mediaPeriod: MediaPeriod!

The period to release.

setAdPlaybackStates

fun setAdPlaybackStates(
    adPlaybackStates: ImmutableMap<Any!, AdPlaybackState!>!,
    contentTimeline: Timeline!
): Unit

Sets the map of ad playback states published by this source. The key is the period UID of a period in the contentTimeline.

Each period has an AdPlaybackState that tells where in the period the ad groups start and end. Must only contain server-side inserted ad groups. The number of ad groups and the number of ads within an ad group may only increase. The durations of ads may change and the positions of future ad groups may change. Post-roll ad groups with TIME_END_OF_SOURCE must be empty and can be used as a placeholder for a future ad group.

May be called from any thread.

Parameters
adPlaybackStates: ImmutableMap<Any!, AdPlaybackState!>!

The map of AdPlaybackState keyed by their period UID.

contentTimeline: Timeline!

The content timeline containing the periods with the UIDs used as keys in the map of playback states.

updateMediaItem

fun updateMediaItem(mediaItem: MediaItem!): Unit

Updates the MediaItem for this source.

Should not be called directly from application code.

This method must be called on the playback thread and only if canUpdateMediaItem returns true for the new MediaItem.

Parameters
mediaItem: MediaItem!

The new MediaItem.

Protected functions

disableInternal

protected fun disableInternal(): Unit

Disables the source, see disable.

enableInternal

protected fun enableInternal(): Unit

Enables the source, see enable.

prepareSourceInternal

protected fun prepareSourceInternal(mediaTransferListener: TransferListener?): Unit

Starts source preparation and enables the source, see prepareSource. This method is called at most once until the next call to releaseSourceInternal.

Parameters
mediaTransferListener: TransferListener?

The transfer listener which should be informed of any media data transfers. May be null if no listener is available. Note that this listener should usually be only informed of transfers related to the media loads and not of auxiliary loads for manifests and other data.

releaseSourceInternal

protected fun releaseSourceInternal(): Unit

Releases the source, see releaseSource. This method is called exactly once after each call to prepareSourceInternal.