Package com.google.android.exoplayer2.testutil

## Class FakeMediaSource

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

- All Implemented Interfaces:
    `MediaSource`
  Direct Known Subclasses:
    `FakeAdaptiveMediaSource`

      public class FakeMediaSource
      extends BaseMediaSource

  Fake `MediaSource` that provides a given timeline. Creating the period will return a `FakeMediaPeriod` with a `TrackGroupArray` using the given `Format`s.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class | Description
    --- | --- | ---
    `static class` | `FakeMediaSource.InitialTimeline` | A forwarding timeline to provide an initial timeline for fake multi window sources.

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

      `MediaSource.Factory, MediaSource.MediaPeriodId, MediaSource.MediaSourceCaller`

  - ### Field Summary

    Fields
    Modifier and Type | Field | Description
    --- | --- | ---
    `static MediaItem` | `FAKE_MEDIA_ITEM` | The media item used by the fake media source.

  - ### Constructor Summary

    Constructors
    Constructor | Description
    --- | ---
    `FakeMediaSource()` | Creates a `FakeMediaSource` with a default `FakeTimeline`.
    `FakeMediaSource​(Timeline timeline, DrmSessionManager drmSessionManager, Format... formats)` | Creates a `FakeMediaSource`.
    `FakeMediaSource​(Timeline timeline, DrmSessionManager drmSessionManager, FakeMediaPeriod.TrackDataFactory trackDataFactory, Format... formats)` | Creates a `FakeMediaSource`.
    `FakeMediaSource​(Timeline timeline, DrmSessionManager drmSessionManager, FakeMediaPeriod.TrackDataFactory trackDataFactory, TrackGroupArray trackGroupArray)` | Creates a `FakeMediaSource`.
    `FakeMediaSource​(Timeline timeline, Format... formats)` | Creates a `FakeMediaSource`.

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method | Description
    --- | --- | ---
    `void` | `assertMediaPeriodCreated​(MediaSource.MediaPeriodId mediaPeriodId)` | Assert that a media period for the given id has been created.
    `void` | `assertReleased()` | Assert that the source and all periods have been released.
    `protected MediaPeriod` | `createMediaPeriod​(MediaSource.MediaPeriodId id, TrackGroupArray trackGroupArray, Allocator allocator, MediaSourceEventListener.EventDispatcher mediaSourceEventDispatcher, DrmSessionManager drmSessionManager, DrmSessionEventListener.EventDispatcher drmEventDispatcher, TransferListener transferListener)` | Creates a `MediaPeriod` for this media source.
    `MediaPeriod` | `createPeriod​(MediaSource.MediaPeriodId id, Allocator allocator, long startPositionUs)` | Returns a new `MediaPeriod` identified by `periodId`.
    `static FakeMediaSource` | `createWithWindowId​(Object windowId)` | Convenience method to create a `FakeMediaSource` with the given window id.
    `List<MediaSource.MediaPeriodId>` | `getCreatedMediaPeriods()` | Returns a list of `MediaSource.MediaPeriodId`s, with one element for each created media period.
    `Timeline` | `getInitialTimeline()` | 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.
    `MediaItem` | `getMediaItem()` | Returns the `MediaItem` whose media is provided by the source.
    `protected Timeline` | `getTimeline()` |
    `boolean` | `isPrepared()` | Returns whether the source is currently prepared.
    `boolean` | `isSingleWindow()` | Returns true if the media source is guaranteed to never have zero or more than one window.
    `void` | `maybeThrowSourceInfoRefreshError()` | Throws any pending error encountered while loading or refreshing source information.
    `void` | `prepareSourceInternal​(TransferListener mediaTransferListener)` | Starts source preparation and enables the source, see `BaseMediaSource.prepareSource(MediaSourceCaller, TransferListener, PlayerId)`.
    `protected void` | `releaseMediaPeriod​(MediaPeriod mediaPeriod)` | Releases a media period created by `createMediaPeriod(MediaPeriodId, TrackGroupArray, Allocator, MediaSourceEventListener.EventDispatcher, DrmSessionManager, DrmSessionEventListener.EventDispatcher, TransferListener)`.
    `void` | `releasePeriod​(MediaPeriod mediaPeriod)` | Releases the period.
    `protected void` | `releaseSourceInternal()` | Releases the source, see `BaseMediaSource.releaseSource(MediaSourceCaller)`.
    `void` | `setAllowPreparation​(boolean allowPreparation)` | Sets whether the next call to `BaseMediaSource.prepareSource(com.google.android.exoplayer2.source.MediaSource.MediaSourceCaller, com.google.android.exoplayer2.upstream.TransferListener)` is allowed to finish.
    `void` | `setNewSourceInfo​(Timeline newTimeline)` | Sets a new timeline.
    `void` | `setNewSourceInfo​(Timeline newTimeline, boolean sendManifestLoadEvents)` | Sets a new timeline.

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

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

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

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

- - ### Field Detail

    - #### FAKE_MEDIA_ITEM

          public static final MediaItem FAKE_MEDIA_ITEM

      The media item used by the fake media source.

  - ### Constructor Detail

    - #### FakeMediaSource

          public FakeMediaSource()

      Creates a `FakeMediaSource` with a default `FakeTimeline`.

    - #### FakeMediaSource

          public FakeMediaSource​(@Nullable
                                 Timeline timeline,
                                 Format... formats)

      Creates a `FakeMediaSource`. This media source creates `FakeMediaPeriod`s with a `TrackGroupArray` using the given `Format`s. The provided `Timeline` may be null to prevent an immediate source info refresh message when preparing the media source. It can be manually set later using `setNewSourceInfo(Timeline)`.

    - #### FakeMediaSource

          public FakeMediaSource​(@Nullable
                                 Timeline timeline,
                                 DrmSessionManager drmSessionManager,
                                 Format... formats)

      Creates a `FakeMediaSource`. This media source creates `FakeMediaPeriod`s with a `TrackGroupArray` using the given `Format`s. It passes `drmSessionManager` into the created periods. The provided `Timeline` may be null to prevent an immediate source info refresh message when preparing the media source. It can be manually set later using `setNewSourceInfo(Timeline)`.

    - #### FakeMediaSource

          public FakeMediaSource​(@Nullable
                                 Timeline timeline,
                                 DrmSessionManager drmSessionManager,
                                 @Nullable
                                 FakeMediaPeriod.TrackDataFactory trackDataFactory,
                                 Format... formats)

      Creates a `FakeMediaSource`. This media source creates `FakeMediaPeriod`s with a `TrackGroupArray` using the given `Format`s. It passes `drmSessionManager` and `trackDataFactory` into the created periods. The provided `Timeline` may be null to prevent an immediate source info refresh message when preparing the media source. It can be manually set later using `setNewSourceInfo(Timeline)`.

    - #### FakeMediaSource

          public FakeMediaSource​(@Nullable
                                 Timeline timeline,
                                 DrmSessionManager drmSessionManager,
                                 @Nullable
                                 FakeMediaPeriod.TrackDataFactory trackDataFactory,
                                 TrackGroupArray trackGroupArray)

      Creates a `FakeMediaSource`. This media source creates `FakeMediaPeriod`s with the provided `TrackGroupArray`, `DrmSessionManager` and `FakeMediaPeriod.TrackDataFactory`. The provided `Timeline` may be null to prevent an immediate source info refresh message when preparing the media source. It can be manually set later using `setNewSourceInfo(Timeline)`.

  - ### Method Detail

    - #### createWithWindowId

          public static FakeMediaSource createWithWindowId​(Object windowId)

      Convenience method to create a `FakeMediaSource` with the given window id.

    - #### setAllowPreparation

          public void setAllowPreparation​(boolean allowPreparation)

      Sets whether the next call to `BaseMediaSource.prepareSource(com.google.android.exoplayer2.source.MediaSource.MediaSourceCaller, com.google.android.exoplayer2.upstream.TransferListener)` is allowed to finish. If not allowed, a later call to this method with `allowPreparation` set to true will finish the preparation.
      Parameters:
        `allowPreparation` - Whether preparation is allowed to finish.

    - #### getTimeline

          @Nullable
          protected Timeline getTimeline()

    - #### getMediaItem

          public MediaItem getMediaItem()

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

    - #### getInitialTimeline

          @Nullable
          public Timeline getInitialTimeline()

      Description copied from interface: `MediaSource`
      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.

      The initial timeline must use the same uids for windows and periods that the real timeline will use. It also must provide windows which are marked as dynamic to indicate that the window is expected to change when the real timeline arrives.

      Any media source which has multiple windows should typically provide such an initial timeline to make sure the player reports the correct number of windows immediately.

    - #### isSingleWindow

          public boolean isSingleWindow()

      Description copied from interface: `MediaSource`
      Returns true if the media source is guaranteed to never have zero or more than one window.

      The default implementation returns `true`.

      Returns:
        true if the source has exactly one window.

    - #### prepareSourceInternal

          public void prepareSourceInternal​(@Nullable
                                            TransferListener mediaTransferListener)

      Description copied from class: `BaseMediaSource`
      Starts source preparation and enables the source, see `BaseMediaSource.prepareSource(MediaSourceCaller, TransferListener, PlayerId)`. 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, PlayerId)`.

      Throws:
        `IOException`

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

      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.

      Parameters:
        `mediaPeriod` - The period to release.

    - #### 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`

    - #### setNewSourceInfo

          public void setNewSourceInfo​(Timeline newTimeline)

      Sets a new timeline. If the source is already prepared, this triggers a source info refresh message being sent to the listener.
      Parameters:
        `newTimeline` - The new `Timeline`.

    - #### setNewSourceInfo

          public void setNewSourceInfo​(Timeline newTimeline,
                                       boolean sendManifestLoadEvents)

      Sets a new timeline. If the source is already prepared, this triggers a source info refresh message being sent to the listener.

      Must only be called if preparation is `allowed`.

      Parameters:
        `newTimeline` - The new `Timeline`.
        `sendManifestLoadEvents` - Whether to treat this as a manifest refresh and send manifest load events to listeners.

    - #### isPrepared

          public boolean isPrepared()

      Returns whether the source is currently prepared.

    - #### assertReleased

          public void assertReleased()

      Assert that the source and all periods have been released.

    - #### assertMediaPeriodCreated

          public void assertMediaPeriodCreated​(MediaSource.MediaPeriodId mediaPeriodId)

      Assert that a media period for the given id has been created.

    - #### getCreatedMediaPeriods

          public List<MediaSource.MediaPeriodId> getCreatedMediaPeriods()

      Returns a list of `MediaSource.MediaPeriodId`s, with one element for each created media period.

    - #### createMediaPeriod

          @RequiresNonNull("this.timeline")
          protected MediaPeriod createMediaPeriod​(MediaSource.MediaPeriodId id,
                                                  TrackGroupArray trackGroupArray,
                                                  Allocator allocator,
                                                  MediaSourceEventListener.EventDispatcher mediaSourceEventDispatcher,
                                                  DrmSessionManager drmSessionManager,
                                                  DrmSessionEventListener.EventDispatcher drmEventDispatcher,
                                                  @Nullable
                                                  TransferListener transferListener)

      Creates a `MediaPeriod` for this media source.
      Parameters:
        `id` - The identifier of the period.
        `trackGroupArray` - The `TrackGroupArray` supported by the media period.
        `allocator` - An `Allocator` from which to obtain media buffer allocations.
        `mediaSourceEventDispatcher` - An `MediaSourceEventListener.EventDispatcher` to dispatch media source events.
        `drmEventDispatcher` - An `MediaSourceEventListener.EventDispatcher` to dispatch DRM events.
        `transferListener` - The transfer listener which should be informed of any data transfers. May be null if no listener is available.
      Returns:
        A new `FakeMediaPeriod`.

    - #### releaseMediaPeriod

          protected void releaseMediaPeriod​(MediaPeriod mediaPeriod)

      Releases a media period created by `createMediaPeriod(MediaPeriodId, TrackGroupArray, Allocator, MediaSourceEventListener.EventDispatcher, DrmSessionManager, DrmSessionEventListener.EventDispatcher, TransferListener)`.