@UnstableApi
public class FakeMediaSource extends BaseMediaSource

Known direct subclasses
FakeAdaptiveMediaSource

Fake MediaSource that provides a given timeline.


Fake MediaSource that provides a given timeline. Creating the period will return a with a TrackGroupArray using the given Formats.

Summary

Nested types

A forwarding timeline to provide an initial timeline for fake multi window sources.

Constants

static final MediaItem

The media item used by the fake media source.

Public constructors

Creates a FakeMediaSource with a default FakeTimeline.

FakeMediaSource(@Nullable Timeline timeline, Format[] formats)

Creates a FakeMediaSource.

FakeMediaSource(
    @Nullable Timeline timeline,
    DrmSessionManager drmSessionManager,
    Format[] formats
)

Creates a FakeMediaSource.

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

Creates a FakeMediaSource.

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

Creates a FakeMediaSource.

Public methods

void

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

void

Assert that the source and all periods have been released.

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.

static FakeMediaSource

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

List<MediaSource.MediaPeriodId>

Returns a list of MediaPeriodIds, with one element for each created media period.

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

MediaPeriod

Returns the last created active MediaPeriod.

MediaItem

Returns the MediaItem whose media is provided by the source.

boolean

Returns whether the source is currently prepared.

boolean

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

void

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

synchronized void

Starts source preparation and enables the source, see prepareSource.

void

Releases the period.

synchronized void
setAllowPreparation(boolean allowPreparation)

Sets whether the next call to prepareSource is allowed to finish.

void
setCanUpdateMediaItems(boolean canUpdateMediaItems)

Sets whether the source allows to update its MediaItem via updateMediaItem.

void

Sets a new timeline.

synchronized void
setNewSourceInfo(Timeline newTimeline, boolean sendManifestLoadEvents)

Sets a new timeline.

void
setPeriodDefersOnPreparedCallback(
    boolean periodDefersOnPreparedCallback
)

Sets whether the created period should defer to call onPrepared.

void

Updates the MediaItem for this source.

Protected methods

MediaPeriod
@RequiresNonNull(value = "this.timeline")
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.

@Nullable Timeline
void

Releases a media period created by createMediaPeriod.

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.

void

Disables the source, see disable.

final void

Enables the source for the creation of MediaPeriods.

void

Enables the source, see enable.

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.

Constants

FAKE_MEDIA_ITEM

public static final MediaItem FAKE_MEDIA_ITEM

The media item used by the fake media source.

Protected fields

Public constructors

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 FakeMediaPeriods with a TrackGroupArray using the given Formats. 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.

FakeMediaSource

public FakeMediaSource(
    @Nullable Timeline timeline,
    DrmSessionManager drmSessionManager,
    Format[] formats
)

Creates a FakeMediaSource. This media source creates FakeMediaPeriods with a TrackGroupArray using the given Formats. 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.

FakeMediaSource

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

Creates a FakeMediaSource. This media source creates FakeMediaPeriods with a TrackGroupArray using the given Formats. 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.

FakeMediaSource

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

Creates a FakeMediaSource. This media source creates FakeMediaPeriods with the provided TrackGroupArray, DrmSessionManager and . 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.

Public methods

assertMediaPeriodCreated

public void assertMediaPeriodCreated(MediaSource.MediaPeriodId mediaPeriodId)

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

assertReleased

public void assertReleased()

Assert that the source and all periods have been released.

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.

createWithWindowId

public static FakeMediaSource createWithWindowId(Object windowId)

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

getCreatedMediaPeriods

public List<MediaSource.MediaPeriodIdgetCreatedMediaPeriods()

Returns a list of MediaPeriodIds, with one element for each created media period.

getInitialTimeline

public @Nullable 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.

Should not be called directly from application code.

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.

This method must be called on the application thread.

getLastCreatedActiveMediaPeriod

public MediaPeriod getLastCreatedActiveMediaPeriod()

Returns the last created active MediaPeriod.

Must only be called if the source has created at least one period and it hasn't been released.

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.

isPrepared

public boolean isPrepared()

Returns whether the source is currently prepared.

isSingleWindow

public boolean isSingleWindow()

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

Should not be called directly from application code.

The default implementation returns true.

This method must be called on the application thread.

Returns
boolean

true if the source has exactly one window.

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.

prepareSourceInternal

synchronized public 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.

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.

setAllowPreparation

synchronized public void setAllowPreparation(boolean allowPreparation)

Sets whether the next call to prepareSource is allowed to finish. If not allowed, a later call to this method with allowPreparation set to true will finish the preparation.

Parameters
boolean allowPreparation

Whether preparation is allowed to finish.

setCanUpdateMediaItems

public void setCanUpdateMediaItems(boolean canUpdateMediaItems)

Sets whether the source allows to update its MediaItem via updateMediaItem.

Parameters
boolean canUpdateMediaItems

Whether a MediaItem update is possible.

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
Timeline newTimeline

The new Timeline.

setNewSourceInfo

synchronized 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
Timeline newTimeline

The new Timeline.

boolean sendManifestLoadEvents

Whether to treat this as a manifest refresh and send manifest load events to listeners.

setPeriodDefersOnPreparedCallback

public void setPeriodDefersOnPreparedCallback(
    boolean periodDefersOnPreparedCallback
)

Sets whether the created period should defer to call onPrepared. If set to true, onPrepared should be called only after setPreparationComplete has been called, otherwise the preparation completes immediately.

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

createMediaPeriod

@RequiresNonNull(value = "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
MediaSource.MediaPeriodId id

The identifier of the period.

TrackGroupArray trackGroupArray

The TrackGroupArray supported by the media period.

Allocator allocator

An Allocator from which to obtain media buffer allocations.

MediaSourceEventListener.EventDispatcher mediaSourceEventDispatcher

An MediaSourceEventListener.EventDispatcher to dispatch media source events.

DrmSessionManager drmSessionManager

A DrmSessionManager to allow DRM interactions.

DrmSessionEventListener.EventDispatcher drmEventDispatcher

An MediaSourceEventListener.EventDispatcher to dispatch DRM events.

@Nullable TransferListener transferListener

The transfer listener which should be informed of any data transfers. May be null if no listener is available.

Returns
MediaPeriod

A new MediaPeriod.

getTimeline

protected @Nullable Timeline getTimeline()

releaseMediaPeriod

protected void releaseMediaPeriod(MediaPeriod mediaPeriod)

Releases a media period created by createMediaPeriod.

releaseSourceInternal

protected void releaseSourceInternal()

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