Package com.google.android.exoplayer2.source

## Class BaseMediaSource

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

- All Implemented Interfaces:
    `MediaSource`
  Direct Known Subclasses:
    `CompositeMediaSource`, `DashMediaSource`, `FakeMediaSource`, `HlsMediaSource`, `ProgressiveMediaSource`, `RtspMediaSource`, `SilenceMediaSource`, `SingleSampleMediaSource`, `SsMediaSource`

      public abstract class BaseMediaSource
      extends Object
      implements MediaSource

  Base `MediaSource` implementation to handle parallel reuse and to keep a list of `MediaSourceEventListener`s.

  Whenever an implementing subclass needs to provide a new timeline, it must call `refreshSourceInfo(Timeline)` to notify all listeners.

- - ### Nested Class Summary

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

      `MediaSource.MediaPeriodId, MediaSource.MediaSourceCaller`

  - ### Constructor Summary

    Constructors
    Constructor | Description
    --- | ---
    `BaseMediaSource()` |

  - ### Method Summary

    All Methods Instance Methods Abstract Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `void` | `addDrmEventListener​(Handler handler, DrmSessionEventListener eventListener)` | Adds a `DrmSessionEventListener` to the list of listeners which are notified of DRM events for this media source.
    `void` | `addEventListener​(Handler handler, MediaSourceEventListener eventListener)` | Adds a `MediaSourceEventListener` to the list of listeners which are notified of media source events.
    `protected DrmSessionEventListener.EventDispatcher` | `createDrmEventDispatcher​(int windowIndex, MediaSource.MediaPeriodId mediaPeriodId)` | Returns a `DrmSessionEventListener.EventDispatcher` which dispatches all events to the registered listeners with the specified window index and `MediaSource.MediaPeriodId`.
    `protected DrmSessionEventListener.EventDispatcher` | `createDrmEventDispatcher​(MediaSource.MediaPeriodId mediaPeriodId)` | Returns a `DrmSessionEventListener.EventDispatcher` which dispatches all events to the registered listeners with the specified `MediaSource.MediaPeriodId`
    `protected MediaSourceEventListener.EventDispatcher` | `createEventDispatcher​(int windowIndex, MediaSource.MediaPeriodId mediaPeriodId, long mediaTimeOffsetMs)` | Returns a `MediaSourceEventListener.EventDispatcher` which dispatches all events to the registered listeners with the specified window index, `MediaSource.MediaPeriodId` and time offset.
    `protected MediaSourceEventListener.EventDispatcher` | `createEventDispatcher​(MediaSource.MediaPeriodId mediaPeriodId)` | Returns a `MediaSourceEventListener.EventDispatcher` which dispatches all events to the registered listeners with the specified `MediaSource.MediaPeriodId`.
    `protected MediaSourceEventListener.EventDispatcher` | `createEventDispatcher​(MediaSource.MediaPeriodId mediaPeriodId, long mediaTimeOffsetMs)` | Returns a `MediaSourceEventListener.EventDispatcher` which dispatches all events to the registered listeners with the specified `MediaSource.MediaPeriodId` and time offset.
    `void` | `disable​(MediaSource.MediaSourceCaller caller)` | Disables the source for the creation of `MediaPeriods`.
    `protected void` | `disableInternal()` | Disables the source, see `disable(MediaSourceCaller)`.
    `void` | `enable​(MediaSource.MediaSourceCaller caller)` | Enables the source for the creation of `MediaPeriods`.
    `protected void` | `enableInternal()` | Enables the source, see `enable(MediaSourceCaller)`.
    `protected boolean` | `isEnabled()` | Returns whether the source is enabled.
    `void` | `prepareSource​(MediaSource.MediaSourceCaller caller, TransferListener mediaTransferListener)` | Registers a `MediaSource.MediaSourceCaller`.
    `protected abstract void` | `prepareSourceInternal​(TransferListener mediaTransferListener)` | Starts source preparation and enables the source, see `prepareSource(MediaSourceCaller, TransferListener)`.
    `protected void` | `refreshSourceInfo​(Timeline timeline)` | Updates timeline and manifest and notifies all listeners of the update.
    `void` | `releaseSource​(MediaSource.MediaSourceCaller caller)` | Unregisters a caller, and disables and releases the source if no longer required.
    `protected abstract void` | `releaseSourceInternal()` | Releases the source, see `releaseSource(MediaSourceCaller)`.
    `void` | `removeDrmEventListener​(DrmSessionEventListener eventListener)` | Removes a `DrmSessionEventListener` from the list of listeners which are notified of DRM events for this media source.
    `void` | `removeEventListener​(MediaSourceEventListener eventListener)` | Removes a `MediaSourceEventListener` from the list of listeners which are notified of media source events.

    - ### 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.source.MediaSource

      `createPeriod, getInitialTimeline, getMediaItem, getTag, isSingleWindow, maybeThrowSourceInfoRefreshError, releasePeriod`

- - ### Constructor Detail

    - #### BaseMediaSource

          public BaseMediaSource()

  - ### Method Detail

    - #### prepareSourceInternal

          protected abstract void prepareSourceInternal​(@Nullable
                                                        TransferListener mediaTransferListener)

      Starts source preparation and enables the source, see `prepareSource(MediaSourceCaller, TransferListener)`. This method is called at most once until the next call to `releaseSourceInternal()`.
      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.

    - #### enableInternal

          protected void enableInternal()

      Enables the source, see `enable(MediaSourceCaller)`.

    - #### disableInternal

          protected void disableInternal()

      Disables the source, see `disable(MediaSourceCaller)`.

    - #### releaseSourceInternal

          protected abstract void releaseSourceInternal()

      Releases the source, see `releaseSource(MediaSourceCaller)`. This method is called exactly once after each call to `prepareSourceInternal(TransferListener)`.

    - #### refreshSourceInfo

          protected final void refreshSourceInfo​(Timeline timeline)

      Updates timeline and manifest and notifies all listeners of the update.
      Parameters:
        `timeline` - The new `Timeline`.

    - #### createEventDispatcher

          protected final MediaSourceEventListener.EventDispatcher createEventDispatcher​(@Nullable
                                                                                         MediaSource.MediaPeriodId mediaPeriodId)

      Returns a `MediaSourceEventListener.EventDispatcher` which dispatches all events to the registered listeners with the specified `MediaSource.MediaPeriodId`.
      Parameters:
        `mediaPeriodId` - The `MediaSource.MediaPeriodId` to be reported with the events. May be null, if the events do not belong to a specific media period.
      Returns:
        An event dispatcher with pre-configured media period id.

    - #### createEventDispatcher

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

      Returns a `MediaSourceEventListener.EventDispatcher` which dispatches all events to the registered listeners with the specified `MediaSource.MediaPeriodId` and time offset.
      Parameters:
        `mediaPeriodId` - The `MediaSource.MediaPeriodId` to be reported with the events.
        `mediaTimeOffsetMs` - The offset to be added to all media times, in milliseconds.
      Returns:
        An event dispatcher with pre-configured media period id and time offset.

    - #### createEventDispatcher

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

      Returns a `MediaSourceEventListener.EventDispatcher` which dispatches all events to the registered listeners with the specified window index, `MediaSource.MediaPeriodId` and time offset.
      Parameters:
        `windowIndex` - The timeline window index to be reported with the events.
        `mediaPeriodId` - The `MediaSource.MediaPeriodId` to be reported with the events. May be null, if the events do not belong to a specific media period.
        `mediaTimeOffsetMs` - The offset to be added to all media times, in milliseconds.
      Returns:
        An event dispatcher with pre-configured media period id and time offset.

    - #### createDrmEventDispatcher

          protected final DrmSessionEventListener.EventDispatcher createDrmEventDispatcher​(@Nullable
                                                                                           MediaSource.MediaPeriodId mediaPeriodId)

      Returns a `DrmSessionEventListener.EventDispatcher` which dispatches all events to the registered listeners with the specified `MediaSource.MediaPeriodId`
      Parameters:
        `mediaPeriodId` - The `MediaSource.MediaPeriodId` to be reported with the events. May be null, if the events do not belong to a specific media period.
      Returns:
        An event dispatcher with pre-configured media period id.

    - #### createDrmEventDispatcher

          protected 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 `MediaSource.MediaPeriodId`.
      Parameters:
        `windowIndex` - The timeline window index to be reported with the events.
        `mediaPeriodId` - The `MediaSource.MediaPeriodId` to be reported with the events. May be null, if the events do not belong to a specific media period.
      Returns:
        An event dispatcher with pre-configured media period id and time offset.

    - #### isEnabled

          protected final boolean isEnabled()

      Returns whether the source is enabled.

    - #### addEventListener

          public final void addEventListener​(Handler handler,
                                             MediaSourceEventListener eventListener)

      Description copied from interface: `MediaSource`
      Adds a `MediaSourceEventListener` to the list of listeners which are notified of media source events.
      Specified by:
        `addEventListener` in interface `MediaSource`
      Parameters:
        `handler` - A handler on the which listener events will be posted.
        `eventListener` - The listener to be added.

    - #### removeEventListener

          public final void removeEventListener​(MediaSourceEventListener eventListener)

      Description copied from interface: `MediaSource`
      Removes a `MediaSourceEventListener` from the list of listeners which are notified of media source events.
      Specified by:
        `removeEventListener` in interface `MediaSource`
      Parameters:
        `eventListener` - The listener to be removed.

    - #### addDrmEventListener

          public final void addDrmEventListener​(Handler handler,
                                                DrmSessionEventListener eventListener)

      Description copied from interface: `MediaSource`
      Adds a `DrmSessionEventListener` to the list of listeners which are notified of DRM events for this media source.
      Specified by:
        `addDrmEventListener` in interface `MediaSource`
      Parameters:
        `handler` - A handler on the which listener events will be posted.
        `eventListener` - The listener to be added.

    - #### removeDrmEventListener

          public final void removeDrmEventListener​(DrmSessionEventListener eventListener)

      Description copied from interface: `MediaSource`
      Removes a `DrmSessionEventListener` from the list of listeners which are notified of DRM events for this media source.
      Specified by:
        `removeDrmEventListener` in interface `MediaSource`
      Parameters:
        `eventListener` - The listener to be removed.

    - #### prepareSource

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

      Description copied from interface: `MediaSource`
      Registers a `MediaSource.MediaSourceCaller`. Starts source preparation if needed and enables the source for the creation of `MediaPerods`.

      Should not be called directly from application code.

      `MediaSource.MediaSourceCaller.onSourceInfoRefreshed(MediaSource, Timeline)` will be called once the source has a `Timeline`.

      For each call to this method, a call to `MediaSource.releaseSource(MediaSourceCaller)` is needed to remove the caller and to release the source if no longer required.

      Specified by:
        `prepareSource` in interface `MediaSource`
      Parameters:
        `caller` - The `MediaSource.MediaSourceCaller` to be registered.
        `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 be only informed of transfers related to the media loads and not of auxiliary loads for manifests and other data.

    - #### enable

          public final void enable​(MediaSource.MediaSourceCaller caller)

      Description copied from interface: `MediaSource`
      Enables the source for the creation of `MediaPeriods`.

      Should not be called directly from application code.

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

      Specified by:
        `enable` in interface `MediaSource`
      Parameters:
        `caller` - The `MediaSource.MediaSourceCaller` enabling the source.

    - #### disable

          public final void disable​(MediaSource.MediaSourceCaller caller)

      Description copied from interface: `MediaSource`
      Disables the source for the creation of `MediaPeriods`. The implementation should not hold onto limited resources used for the creation of media periods.

      Should not be called directly from application code.

      Must only be called after all `MediaPeriods` previously created by `MediaSource.createPeriod(MediaPeriodId, Allocator, long)` have been released by `MediaSource.releasePeriod(MediaPeriod)`.

      Specified by:
        `disable` in interface `MediaSource`
      Parameters:
        `caller` - The `MediaSource.MediaSourceCaller` disabling the source.

    - #### releaseSource

          public final void releaseSource​(MediaSource.MediaSourceCaller caller)

      Description copied from interface: `MediaSource`
      Unregisters a caller, and disables and releases the source if no longer required.

      Should not be called directly from application code.

      Must only be called if all created `MediaPeriods` have been released by `MediaSource.releasePeriod(MediaPeriod)`.

      Specified by:
        `releaseSource` in interface `MediaSource`
      Parameters:
        `caller` - The `MediaSource.MediaSourceCaller` to be unregistered.