ServerSideAdInsertionMediaSource


@UnstableApi
public final class ServerSideAdInsertionMediaSource extends BaseMediaSource implements 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 methods

boolean

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

MediaPeriod
createPeriod(
    MediaSource.MediaPeriodId id,
    Allocator allocator,
    long startPositionUs
)

Returns a new MediaPeriod identified by periodId.

MediaItem

Returns the MediaItem whose media is provided by the source.

void

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

void
onDownstreamFormatChanged(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId 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).

void
onDrmKeysLoaded(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId
)

Called each time keys are loaded.

void
onDrmKeysRemoved(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId
)

Called each time offline keys are removed.

void
onDrmKeysRestored(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId
)

Called each time offline keys are restored.

void
onDrmSessionAcquired(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId,
    @DrmSession.State int state
)

Called each time a drm session is acquired.

void
onDrmSessionManagerError(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId,
    Exception error
)

Called when a drm error occurs.

void
onDrmSessionReleased(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId
)

Called each time a drm session is released.

void
onLoadCanceled(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId,
    LoadEventInfo loadEventInfo,
    MediaLoadData mediaLoadData
)

Called when a load is canceled.

void
onLoadCompleted(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId,
    LoadEventInfo loadEventInfo,
    MediaLoadData mediaLoadData
)

Called when a load ends.

void
onLoadError(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId,
    LoadEventInfo loadEventInfo,
    MediaLoadData mediaLoadData,
    IOException error,
    boolean wasCanceled
)

Called when a load error occurs.

void
onLoadStarted(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId,
    LoadEventInfo loadEventInfo,
    MediaLoadData mediaLoadData
)

Called when a load begins.

void

Called when the Timeline has been refreshed.

void
onUpstreamDiscarded(
    int windowIndex,
    MediaSource.MediaPeriodId 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.

void

Releases the period.

void
setAdPlaybackStates(
    ImmutableMap<ObjectAdPlaybackState> adPlaybackStates,
    Timeline contentTimeline
)

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

void

Updates the MediaItem for this source.

Protected methods

void

Disables the source, see disable.

void

Enables the source, see enable.

void

Starts source preparation and enables the source, see prepareSource.

void

Releases the source, see releaseSource.

Inherited methods

From androidx.media3.exoplayer.source.BaseMediaSource
final void
@UnstableApi
addDrmEventListener(
    Handler handler,
    DrmSessionEventListener eventListener
)

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

final void
@UnstableApi
addEventListener(
    Handler handler,
    MediaSourceEventListener eventListener
)

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

final DrmSessionEventListener.EventDispatcher

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

final DrmSessionEventListener.EventDispatcher
createDrmEventDispatcher(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId
)

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

final MediaSourceEventListener.EventDispatcher

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

final MediaSourceEventListener.EventDispatcher
createEventDispatcher(
    MediaSource.MediaPeriodId mediaPeriodId,
    long mediaTimeOffsetMs
)

This method is deprecated.

Use createEventDispatcher instead.

final MediaSourceEventListener.EventDispatcher
createEventDispatcher(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId
)

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

final MediaSourceEventListener.EventDispatcher
createEventDispatcher(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId,
    long mediaTimeOffsetMs
)

This method is deprecated.

Use createEventDispatcher instead.

final void

Disables the source for the creation of MediaPeriods.

final void

Enables the source for the creation of MediaPeriods.

final PlayerId

Returns the PlayerId of the player using this media source.

final boolean

Returns whether the source is enabled.

final void
@UnstableApi
prepareSource(
    MediaSource.MediaSourceCaller caller,
    @Nullable TransferListener mediaTransferListener
)

This method is deprecated.

Implement prepareSource instead.

final void
@UnstableApi
prepareSource(
    MediaSource.MediaSourceCaller caller,
    @Nullable TransferListener mediaTransferListener,
    PlayerId playerId
)

Registers a MediaSourceCaller.

final boolean

Returns whether the source has prepareSource called.

final void

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

final void

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

final void

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

final void

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

final void

Sets the PlayerId of the player using this media source.

From androidx.media3.exoplayer.source.MediaSource
@Nullable 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

public ServerSideAdInsertionMediaSource(
    MediaSource mediaSource,
    @Nullable ServerSideAdInsertionMediaSource.AdPlaybackStateUpdater adPlaybackStateUpdater
)

Creates the media source.

Parameters
MediaSource mediaSource

The MediaSource to wrap.

@Nullable ServerSideAdInsertionMediaSource.AdPlaybackStateUpdater adPlaybackStateUpdater

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

Public methods

canUpdateMediaItem

public boolean canUpdateMediaItem(MediaItem mediaItem)

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

public MediaPeriod createPeriod(
    MediaSource.MediaPeriodId id,
    Allocator allocator,
    long startPositionUs
)

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
MediaSource.MediaPeriodId id

The identifier of the period.

Allocator allocator

An Allocator from which to obtain media buffer allocations.

long startPositionUs

The expected start position, in microseconds.

Returns
MediaPeriod

A new MediaPeriod.

getMediaItem

public MediaItem getMediaItem()

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

public void maybeThrowSourceInfoRefreshError()

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

public void onDownstreamFormatChanged(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId 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).

Parameters
int windowIndex

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

@Nullable MediaSource.MediaPeriodId mediaPeriodId

The MediaPeriodId the media belongs to.

MediaLoadData mediaLoadData

The MediaLoadData defining the newly selected downstream data.

onDrmKeysLoaded

public void onDrmKeysLoaded(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId
)

Called each time keys are loaded.

Parameters
int windowIndex

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

@Nullable MediaSource.MediaPeriodId mediaPeriodId

The MediaPeriodId associated with the drm session.

onDrmKeysRemoved

public void onDrmKeysRemoved(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId
)

Called each time offline keys are removed.

Parameters
int windowIndex

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

@Nullable MediaSource.MediaPeriodId mediaPeriodId

The MediaPeriodId associated with the drm session.

onDrmKeysRestored

public void onDrmKeysRestored(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId
)

Called each time offline keys are restored.

Parameters
int windowIndex

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

@Nullable MediaSource.MediaPeriodId mediaPeriodId

The MediaPeriodId associated with the drm session.

onDrmSessionAcquired

public void onDrmSessionAcquired(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId,
    @DrmSession.State int state
)

Called each time a drm session is acquired.

Parameters
int windowIndex

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

@Nullable MediaSource.MediaPeriodId mediaPeriodId

The MediaPeriodId associated with the drm session.

@DrmSession.State int state

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

onDrmSessionManagerError

public void onDrmSessionManagerError(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId,
    Exception error
)

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
int windowIndex

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

@Nullable MediaSource.MediaPeriodId mediaPeriodId

The MediaPeriodId associated with the drm session.

Exception error

The corresponding exception.

onDrmSessionReleased

public void onDrmSessionReleased(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId
)

Called each time a drm session is released.

Parameters
int windowIndex

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

@Nullable MediaSource.MediaPeriodId mediaPeriodId

The MediaPeriodId associated with the drm session.

onLoadCanceled

public void onLoadCanceled(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId,
    LoadEventInfo loadEventInfo,
    MediaLoadData mediaLoadData
)

Called when a load is canceled.

Parameters
int windowIndex

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

@Nullable MediaSource.MediaPeriodId 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

public void onLoadCompleted(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId,
    LoadEventInfo loadEventInfo,
    MediaLoadData mediaLoadData
)

Called when a load ends.

Parameters
int windowIndex

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

@Nullable MediaSource.MediaPeriodId 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

public void onLoadError(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId,
    LoadEventInfo loadEventInfo,
    MediaLoadData mediaLoadData,
    IOException error,
    boolean wasCanceled
)

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
int windowIndex

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

@Nullable MediaSource.MediaPeriodId 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.

IOException error

The load error.

boolean wasCanceled

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

onLoadStarted

public void onLoadStarted(
    int windowIndex,
    @Nullable MediaSource.MediaPeriodId mediaPeriodId,
    LoadEventInfo loadEventInfo,
    MediaLoadData mediaLoadData
)

Called when a load begins.

Parameters
int windowIndex

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

@Nullable MediaSource.MediaPeriodId 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

public void onSourceInfoRefreshed(MediaSource source, Timeline timeline)

Called when the Timeline has been refreshed.

Called on the playback thread.

Parameters
MediaSource source

The MediaSource whose info has been refreshed.

Timeline timeline

The source's timeline.

onUpstreamDiscarded

public void onUpstreamDiscarded(
    int windowIndex,
    MediaSource.MediaPeriodId 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.

Parameters
int windowIndex

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

MediaSource.MediaPeriodId mediaPeriodId

The MediaPeriodId the media belongs to.

MediaLoadData mediaLoadData

The MediaLoadData defining the media being discarded.

releasePeriod

public void releasePeriod(MediaPeriod mediaPeriod)

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

public void setAdPlaybackStates(
    ImmutableMap<ObjectAdPlaybackState> adPlaybackStates,
    Timeline contentTimeline
)

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
ImmutableMap<ObjectAdPlaybackState> adPlaybackStates

The map of AdPlaybackState keyed by their period UID.

Timeline contentTimeline

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

updateMediaItem

public void updateMediaItem(MediaItem mediaItem)

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 methods

disableInternal

protected void disableInternal()

Disables the source, see disable.

enableInternal

protected void enableInternal()

Enables the source, see enable.

prepareSourceInternal

protected void prepareSourceInternal(@Nullable TransferListener mediaTransferListener)

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

Parameters
@Nullable TransferListener mediaTransferListener

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 void releaseSourceInternal()

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