Package com.google.android.exoplayer2.source.ads

## Class ServerSideInsertedAdsMediaSource

- java.lang.Object
- - com.google.android.exoplayer2.source.BaseMediaSource
  - - com.google.android.exoplayer2.source.ads.ServerSideInsertedAdsMediaSource

- All Implemented Interfaces:
    `DrmSessionEventListener`, `MediaSource`, `MediaSource.MediaSourceCaller`, `MediaSourceEventListener`

      public final class ServerSideInsertedAdsMediaSource
      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 `setAdPlaybackState(com.google.android.exoplayer2.source.ads.AdPlaybackState)` and can be updated during playback.

- - ### Nested Class Summary

    - ### Nested classes/interfaces inherited from interface com.google.android.exoplayer2.drm.DrmSessionEventListener

      `DrmSessionEventListener.EventDispatcher`

    - ### Nested classes/interfaces inherited from interface com.google.android.exoplayer2.source.MediaSource

      `MediaSource.MediaPeriodId, MediaSource.MediaSourceCaller`

    - ### Nested classes/interfaces inherited from interface com.google.android.exoplayer2.source.MediaSourceEventListener

      `MediaSourceEventListener.EventDispatcher`

  - ### Constructor Summary

    Constructors
    Constructor | Description
    --- | ---
    `ServerSideInsertedAdsMediaSource​(MediaSource mediaSource)` | Creates the media source.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `MediaPeriod` | `createPeriod​(MediaSource.MediaPeriodId id, Allocator allocator, long startPositionUs)` | Returns a new `MediaPeriod` identified by `periodId`.
    `protected void` | `disableInternal()` | Disables the source, see `BaseMediaSource.disable(MediaSourceCaller)`.
    `protected void` | `enableInternal()` | Enables the source, see `BaseMediaSource.enable(MediaSourceCaller)`.
    `MediaItem` | `getMediaItem()` | Returns the `MediaItem` whose media is provided by the source.
    `void` | `maybeThrowSourceInfoRefreshError()` | Throws any pending error encountered while loading or refreshing source information.
    `void` | `onDownstreamFormatChanged​(int windowIndex, MediaSource.MediaPeriodId mediaPeriodId, MediaLoadData mediaLoadData)` | Called when a downstream format change occurs (i.e.
    `void` | `onDrmKeysLoaded​(int windowIndex, MediaSource.MediaPeriodId mediaPeriodId)` | Called each time keys are loaded.
    `void` | `onDrmKeysRemoved​(int windowIndex, MediaSource.MediaPeriodId mediaPeriodId)` | Called each time offline keys are removed.
    `void` | `onDrmKeysRestored​(int windowIndex, MediaSource.MediaPeriodId mediaPeriodId)` | Called each time offline keys are restored.
    `void` | `onDrmSessionAcquired​(int windowIndex, MediaSource.MediaPeriodId mediaPeriodId, int state)` | Called each time a drm session is acquired.
    `void` | `onDrmSessionManagerError​(int windowIndex, MediaSource.MediaPeriodId mediaPeriodId, Exception error)` | Called when a drm error occurs.
    `void` | `onDrmSessionReleased​(int windowIndex, MediaSource.MediaPeriodId mediaPeriodId)` | Called each time a drm session is released.
    `void` | `onLoadCanceled​(int windowIndex, MediaSource.MediaPeriodId mediaPeriodId, LoadEventInfo loadEventInfo, MediaLoadData mediaLoadData)` | Called when a load is canceled.
    `void` | `onLoadCompleted​(int windowIndex, MediaSource.MediaPeriodId mediaPeriodId, LoadEventInfo loadEventInfo, MediaLoadData mediaLoadData)` | Called when a load ends.
    `void` | `onLoadError​(int windowIndex, MediaSource.MediaPeriodId mediaPeriodId, LoadEventInfo loadEventInfo, MediaLoadData mediaLoadData, IOException error, boolean wasCanceled)` | Called when a load error occurs.
    `void` | `onLoadStarted​(int windowIndex, MediaSource.MediaPeriodId mediaPeriodId, LoadEventInfo loadEventInfo, MediaLoadData mediaLoadData)` | Called when a load begins.
    `void` | `onSourceInfoRefreshed​(MediaSource source, Timeline timeline)` | 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.
    `protected void` | `prepareSourceInternal​(TransferListener mediaTransferListener)` | Starts source preparation and enables the source, see `BaseMediaSource.prepareSource(MediaSourceCaller, TransferListener)`.
    `void` | `releasePeriod​(MediaPeriod mediaPeriod)` | Releases the period.
    `protected void` | `releaseSourceInternal()` | Releases the source, see `BaseMediaSource.releaseSource(MediaSourceCaller)`.
    `void` | `setAdPlaybackState​(AdPlaybackState adPlaybackState)` | Sets the `AdPlaybackState` published by this source.

    - ### Methods inherited from class com.google.android.exoplayer2.source.BaseMediaSource

      `addDrmEventListener, addEventListener, createDrmEventDispatcher, createDrmEventDispatcher, createEventDispatcher, createEventDispatcher, createEventDispatcher, disable, enable, isEnabled, prepareSource, refreshSourceInfo, releaseSource, removeDrmEventListener, removeEventListener`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

    - ### Methods inherited from interface com.google.android.exoplayer2.drm.DrmSessionEventListener

      `onDrmSessionAcquired`

    - ### Methods inherited from interface com.google.android.exoplayer2.source.MediaSource

      `getInitialTimeline, isSingleWindow`

- - ### Constructor Detail

    - #### ServerSideInsertedAdsMediaSource

          public ServerSideInsertedAdsMediaSource​(MediaSource mediaSource)

      Creates the media source.
      Parameters:
        `mediaSource` - The `MediaSource` to wrap.

  - ### Method Detail

    - #### setAdPlaybackState

          public void setAdPlaybackState​(AdPlaybackState adPlaybackState)

      Sets the `AdPlaybackState` published by this source.

      May be called from any thread.

      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 `C.TIME_END_OF_SOURCE` must be empty and can be used as a placeholder for a future ad group.

      Parameters:
        `adPlaybackState` - The new `AdPlaybackState`.

    - #### getMediaItem

          public MediaItem getMediaItem()

      Description copied from interface: `MediaSource`
      Returns the `MediaItem` whose media is provided by the source.
      Specified by:
        `getMediaItem` in interface `MediaSource`

    - #### prepareSourceInternal

          protected void prepareSourceInternal​(@Nullable
                                               TransferListener mediaTransferListener)

      Description copied from class: `BaseMediaSource`
      Starts source preparation and enables the source, see `BaseMediaSource.prepareSource(MediaSourceCaller, TransferListener)`. This method is called at most once until the next call to `BaseMediaSource.releaseSourceInternal()`.
      Specified by:
        `prepareSourceInternal` in class `BaseMediaSource`
      Parameters:
        `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.

    - #### maybeThrowSourceInfoRefreshError

          public void maybeThrowSourceInfoRefreshError()
                                                throws IOException

      Description copied from interface: `MediaSource`
      Throws any pending error encountered while loading or refreshing source information.

      Should not be called directly from application code.

      Must only be called after `MediaSource.prepareSource(MediaSourceCaller, TransferListener)`.

      Specified by:
        `maybeThrowSourceInfoRefreshError` in interface `MediaSource`
      Throws:
        `IOException`

    - #### enableInternal

          protected void enableInternal()

      Description copied from class: `BaseMediaSource`
      Enables the source, see `BaseMediaSource.enable(MediaSourceCaller)`.
      Overrides:
        `enableInternal` in class `BaseMediaSource`

    - #### disableInternal

          protected void disableInternal()

      Description copied from class: `BaseMediaSource`
      Disables the source, see `BaseMediaSource.disable(MediaSourceCaller)`.
      Overrides:
        `disableInternal` in class `BaseMediaSource`

    - #### onSourceInfoRefreshed

          public void onSourceInfoRefreshed​(MediaSource source,
                                            Timeline timeline)

      Description copied from interface: `MediaSource.MediaSourceCaller`
      Called when the `Timeline` has been refreshed.

      Called on the playback thread.

      Specified by:
        `onSourceInfoRefreshed` in interface `MediaSource.MediaSourceCaller`
      Parameters:
        `source` - The `MediaSource` whose info has been refreshed.
        `timeline` - The source's timeline.

    - #### releaseSourceInternal

          protected void releaseSourceInternal()

      Description copied from class: `BaseMediaSource`
      Releases the source, see `BaseMediaSource.releaseSource(MediaSourceCaller)`. This method is called exactly once after each call to `BaseMediaSource.prepareSourceInternal(TransferListener)`.
      Specified by:
        `releaseSourceInternal` in class `BaseMediaSource`

    - #### createPeriod

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

      Description copied from interface: `MediaSource`
      Returns a new `MediaPeriod` identified by `periodId`.

      Should not be called directly from application code.

      Must only be called if the source is enabled.

      Specified by:
        `createPeriod` in interface `MediaSource`
      Parameters:
        `id` - The identifier of the period.
        `allocator` - An `Allocator` from which to obtain media buffer allocations.
        `startPositionUs` - The expected start position, in microseconds.
      Returns:
        A new `MediaPeriod`.

    - #### releasePeriod

          public void releasePeriod​(MediaPeriod mediaPeriod)

      Description copied from interface: `MediaSource`
      Releases the period.

      Should not be called directly from application code.

      Specified by:
        `releasePeriod` in interface `MediaSource`
      Parameters:
        `mediaPeriod` - The period to release.

    - #### onDrmSessionAcquired

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

      Description copied from interface: `DrmSessionEventListener`
      Called each time a drm session is acquired.
      Specified by:
        `onDrmSessionAcquired` in interface `DrmSessionEventListener`
      Parameters:
        `windowIndex` - The window index in the timeline this media period belongs to.
        `mediaPeriodId` - The `MediaSource.MediaPeriodId` associated with the drm session.
        `state` - The `DrmSession.State` of the session when the acquisition completed.

    - #### onDrmKeysLoaded

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

      Description copied from interface: `DrmSessionEventListener`
      Called each time keys are loaded.
      Specified by:
        `onDrmKeysLoaded` in interface `DrmSessionEventListener`
      Parameters:
        `windowIndex` - The window index in the timeline this media period belongs to.
        `mediaPeriodId` - The `MediaSource.MediaPeriodId` associated with the drm session.

    - #### onDrmSessionManagerError

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

      Description copied from interface: `DrmSessionEventListener`
      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 (`Player.Listener.onPlayerError(com.google.android.exoplayer2.PlaybackException)` 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.

      Specified by:
        `onDrmSessionManagerError` in interface `DrmSessionEventListener`
      Parameters:
        `windowIndex` - The window index in the timeline this media period belongs to.
        `mediaPeriodId` - The `MediaSource.MediaPeriodId` associated with the drm session.
        `error` - The corresponding exception.

    - #### onDrmKeysRestored

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

      Description copied from interface: `DrmSessionEventListener`
      Called each time offline keys are restored.
      Specified by:
        `onDrmKeysRestored` in interface `DrmSessionEventListener`
      Parameters:
        `windowIndex` - The window index in the timeline this media period belongs to.
        `mediaPeriodId` - The `MediaSource.MediaPeriodId` associated with the drm session.

    - #### onDrmKeysRemoved

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

      Description copied from interface: `DrmSessionEventListener`
      Called each time offline keys are removed.
      Specified by:
        `onDrmKeysRemoved` in interface `DrmSessionEventListener`
      Parameters:
        `windowIndex` - The window index in the timeline this media period belongs to.
        `mediaPeriodId` - The `MediaSource.MediaPeriodId` associated with the drm session.

    - #### onDrmSessionReleased

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

      Description copied from interface: `DrmSessionEventListener`
      Called each time a drm session is released.
      Specified by:
        `onDrmSessionReleased` in interface `DrmSessionEventListener`
      Parameters:
        `windowIndex` - The window index in the timeline this media period belongs to.
        `mediaPeriodId` - The `MediaSource.MediaPeriodId` associated with the drm session.

    - #### onLoadStarted

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

      Description copied from interface: `MediaSourceEventListener`
      Called when a load begins.
      Specified by:
        `onLoadStarted` in interface `MediaSourceEventListener`
      Parameters:
        `windowIndex` - The window index in the timeline of the media source this load belongs to.
        `mediaPeriodId` - The `MediaSource.MediaPeriodId` this load belongs to. Null if the load does not belong to a specific media period.
        `loadEventInfo` - The `LoadEventInfo` corresponding to the event. The value of `LoadEventInfo.uri` won't reflect potential redirection yet and `LoadEventInfo.responseHeaders` will be empty.
        `mediaLoadData` - The `MediaLoadData` defining the data being loaded.

    - #### onLoadCompleted

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

      Description copied from interface: `MediaSourceEventListener`
      Called when a load ends.
      Specified by:
        `onLoadCompleted` in interface `MediaSourceEventListener`
      Parameters:
        `windowIndex` - The window index in the timeline of the media source this load belongs to.
        `mediaPeriodId` - The `MediaSource.MediaPeriodId` this load belongs to. Null if the load does not belong to a specific media period.
        `loadEventInfo` - The `LoadEventInfo` corresponding to the event. The values of `LoadEventInfo.elapsedRealtimeMs` and `LoadEventInfo.bytesLoaded` are relative to the corresponding `MediaSourceEventListener.onLoadStarted(int, MediaPeriodId, LoadEventInfo, MediaLoadData)` event.
        `mediaLoadData` - The `MediaLoadData` defining the data being loaded.

    - #### onLoadCanceled

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

      Description copied from interface: `MediaSourceEventListener`
      Called when a load is canceled.
      Specified by:
        `onLoadCanceled` in interface `MediaSourceEventListener`
      Parameters:
        `windowIndex` - The window index in the timeline of the media source this load belongs to.
        `mediaPeriodId` - The `MediaSource.MediaPeriodId` this load belongs to. Null if the load does not belong to a specific media period.
        `loadEventInfo` - The `LoadEventInfo` corresponding to the event. The values of `LoadEventInfo.elapsedRealtimeMs` and `LoadEventInfo.bytesLoaded` are relative to the corresponding `MediaSourceEventListener.onLoadStarted(int, MediaPeriodId, LoadEventInfo, MediaLoadData)` event.
        `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)

      Description copied from interface: `MediaSourceEventListener`
      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, `MediaSourceEventListener.onLoadCanceled(int, com.google.android.exoplayer2.source.MediaSource.MediaPeriodId, com.google.android.exoplayer2.source.LoadEventInfo, com.google.android.exoplayer2.source.MediaLoadData)` 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. `Player.Listener.onPlayerError(com.google.android.exoplayer2.PlaybackException)` 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.

      Specified by:
        `onLoadError` in interface `MediaSourceEventListener`
      Parameters:
        `windowIndex` - The window index in the timeline of the media source this load belongs to.
        `mediaPeriodId` - The `MediaSource.MediaPeriodId` this load belongs to. Null if the load does not belong to a specific media period.
        `loadEventInfo` - The `LoadEventInfo` corresponding to the event. The values of `LoadEventInfo.elapsedRealtimeMs` and `LoadEventInfo.bytesLoaded` are relative to the corresponding `MediaSourceEventListener.onLoadStarted(int, MediaPeriodId, LoadEventInfo, MediaLoadData)` event.
        `mediaLoadData` - The `MediaLoadData` defining the data being loaded.
        `error` - The load error.
        `wasCanceled` - Whether the load was canceled as a result of the error.

    - #### onUpstreamDiscarded

          public void onUpstreamDiscarded​(int windowIndex,
                                          MediaSource.MediaPeriodId mediaPeriodId,
                                          MediaLoadData mediaLoadData)

      Description copied from interface: `MediaSourceEventListener`
      Called when data is removed from the back of a media buffer, typically so that it can be re-buffered in a different format.
      Specified by:
        `onUpstreamDiscarded` in interface `MediaSourceEventListener`
      Parameters:
        `windowIndex` - The window index in the timeline of the media source this load belongs to.
        `mediaPeriodId` - The `MediaSource.MediaPeriodId` the media belongs to.
        `mediaLoadData` - The `MediaLoadData` defining the media being discarded.

    - #### onDownstreamFormatChanged

          public void onDownstreamFormatChanged​(int windowIndex,
                                                @Nullable
                                                MediaSource.MediaPeriodId mediaPeriodId,
                                                MediaLoadData mediaLoadData)

      Description copied from interface: `MediaSourceEventListener`
      Called when a downstream format change occurs (i.e. when the format of the media being read from one or more `SampleStream`s provided by the source changes).
      Specified by:
        `onDownstreamFormatChanged` in interface `MediaSourceEventListener`
      Parameters:
        `windowIndex` - The window index in the timeline of the media source this load belongs to.
        `mediaPeriodId` - The `MediaSource.MediaPeriodId` the media belongs to.
        `mediaLoadData` - The `MediaLoadData` defining the newly selected downstream data.