public abstract class

WrappingMediaSource

extends CompositeMediaSource<java.lang.Void>

 java.lang.Object

androidx.media3.exoplayer.source.BaseMediaSource

androidx.media3.exoplayer.source.CompositeMediaSource<java.lang.Void>

↳androidx.media3.exoplayer.source.WrappingMediaSource

Subclasses:

MaskingMediaSource, ClippingMediaSource, FilteringMediaSource, LoopingMediaSource, PreloadMediaSource

Gradle dependencies

compile group: 'androidx.media3', name: 'media3-exoplayer', version: '1.5.0-alpha01'

  • groupId: androidx.media3
  • artifactId: media3-exoplayer
  • version: 1.5.0-alpha01

Artifact androidx.media3:media3-exoplayer:1.5.0-alpha01 it located at Google repository (https://maven.google.com/)

Overview

An abstract MediaSource wrapping a single child MediaSource.

The implementation may want to override the following methods as needed:

Other methods like WrappingMediaSource.prepareSourceInternal(TransferListener), CompositeMediaSource.enableInternal(), CompositeMediaSource.disableInternal() or CompositeMediaSource.releaseSourceInternal() only need to be overwritten if required for resource management.

Summary

Fields
protected final MediaSourcemediaSource

The wrapped child MediaSource.

Constructors
protectedWrappingMediaSource(MediaSource mediaSource)

Creates the wrapping MediaSource.

Methods
public booleancanUpdateMediaItem(MediaItem mediaItem)

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

Creates the requested MediaPeriod.

protected final voiddisableChildSource()

Disables the child source.

protected final voidenableChildSource()

Enables the child source.

public TimelinegetInitialTimeline()

public MediaItemgetMediaItem()

protected MediaSource.MediaPeriodIdgetMediaPeriodIdForChildMediaPeriodId(MediaSource.MediaPeriodId mediaPeriodId)

Returns the in the wrapping source corresponding to the specified in a child source.

protected MediaSource.MediaPeriodIdgetMediaPeriodIdForChildMediaPeriodId(java.lang.Object childSourceId, MediaSource.MediaPeriodId mediaPeriodId)

Returns the in the composite source corresponding to the specified in a child source.

protected longgetMediaTimeForChildMediaTime(long mediaTimeMs, MediaSource.MediaPeriodId mediaPeriodId)

Returns the media time in the MediaPeriod of the wrapping source corresponding to the specified media time in the MediaPeriod of the child source.

protected longgetMediaTimeForChildMediaTime(java.lang.Object childSourceId, long mediaTimeMs, MediaSource.MediaPeriodId mediaPeriodId)

Returns the media time in the MediaPeriod of the composite source corresponding to the specified media time in the MediaPeriod of the child source.

protected intgetWindowIndexForChildWindowIndex(int windowIndex)

Returns the window index in the wrapping source corresponding to the specified window index in a child source.

protected intgetWindowIndexForChildWindowIndex(java.lang.Object childSourceId, int windowIndex)

Returns the window index in the composite source corresponding to the specified window index in a child source.

public booleanisSingleWindow()

protected voidonChildSourceInfoRefreshed(Timeline newTimeline)

Called when the child source info has been refreshed.

protected abstract voidonChildSourceInfoRefreshed(java.lang.Object childSourceId, MediaSource mediaSource, Timeline newTimeline)

Called when the source info of a child source has been refreshed.

protected final voidprepareChildSource()

Prepares the wrapped child source.

protected voidprepareSourceInternal()

Starts source preparation and enables the source, see MediaSource.prepareSource(MediaSource.MediaSourceCaller, TransferListener, PlayerId).

protected abstract voidprepareSourceInternal(TransferListener mediaTransferListener)

Starts source preparation and enables the source, see MediaSource.prepareSource(MediaSource.MediaSourceCaller, TransferListener, PlayerId).

protected final voidreleaseChildSource()

Releases the child source.

public voidreleasePeriod(MediaPeriod mediaPeriod)

Releases a MediaPeriod.

public voidupdateMediaItem(MediaItem mediaItem)

from CompositeMediaSource<T>disableChildSource, disableInternal, enableChildSource, enableInternal, maybeThrowSourceInfoRefreshError, prepareChildSource, releaseChildSource, releaseSourceInternal
from BaseMediaSourceaddDrmEventListener, addEventListener, createDrmEventDispatcher, createDrmEventDispatcher, createEventDispatcher, createEventDispatcher, createEventDispatcher, createEventDispatcher, disable, enable, getPlayerId, isEnabled, prepareSource, prepareSource, prepareSourceCalled, refreshSourceInfo, releaseSource, removeDrmEventListener, removeEventListener, setPlayerId
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Fields

protected final MediaSource mediaSource

The wrapped child MediaSource.

Constructors

protected WrappingMediaSource(MediaSource mediaSource)

Creates the wrapping MediaSource.

Parameters:

mediaSource: The wrapped child MediaSource.

Methods

protected abstract void prepareSourceInternal(TransferListener mediaTransferListener)

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

protected void prepareSourceInternal()

Starts source preparation and enables the source, see MediaSource.prepareSource(MediaSource.MediaSourceCaller, TransferListener, PlayerId). This method is called at most once until the next call to CompositeMediaSource.releaseSourceInternal().

public Timeline getInitialTimeline()

public boolean isSingleWindow()

public MediaItem getMediaItem()

This method can be overridden to amend the MediaItem of the child source. It is only used before the child source is prepared.

public boolean canUpdateMediaItem(MediaItem mediaItem)

This method can be overridden to change whether the MediaItem of the child source can be updated.

public void updateMediaItem(MediaItem mediaItem)

This method can be overridden to change how the MediaItem of the child source is updated.

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

Creates the requested MediaPeriod.

This method typically forwards to the wrapped media source and optionally wraps the returned MediaPeriod.

See also: MediaSource.createPeriod(MediaSource.MediaPeriodId, Allocator, long)

public void releasePeriod(MediaPeriod mediaPeriod)

Releases a MediaPeriod.

This method typically forwards to the wrapped media source and optionally unwraps the provided MediaPeriod.

See also: MediaSource.releasePeriod(MediaPeriod)

protected abstract void onChildSourceInfoRefreshed(java.lang.Object childSourceId, MediaSource mediaSource, Timeline newTimeline)

Called when the source info of a child source has been refreshed.

Parameters:

childSourceId: The unique id used to prepare the child source.
mediaSource: The child source whose source info has been refreshed.
newTimeline: The timeline of the child source.

protected void onChildSourceInfoRefreshed(Timeline newTimeline)

Called when the child source info has been refreshed.

This Timeline can be amended if needed, for example using ForwardingTimeline. The Timeline for the wrapping source needs to be published with BaseMediaSource.refreshSourceInfo(Timeline).

Parameters:

newTimeline: The timeline of the child source.

protected int getWindowIndexForChildWindowIndex(java.lang.Object childSourceId, int windowIndex)

Returns the window index in the composite source corresponding to the specified window index in a child source. The default implementation does not change the window index.

Parameters:

childSourceId: The unique id used to prepare the child source.
windowIndex: A window index of the child source.

Returns:

The corresponding window index in the composite source.

protected int getWindowIndexForChildWindowIndex(int windowIndex)

Returns the window index in the wrapping source corresponding to the specified window index in a child source. The default implementation does not change the window index.

Parameters:

windowIndex: A window index of the child source.

Returns:

The corresponding window index in the wrapping source.

protected MediaSource.MediaPeriodId getMediaPeriodIdForChildMediaPeriodId(java.lang.Object childSourceId, MediaSource.MediaPeriodId mediaPeriodId)

Returns the in the composite source corresponding to the specified in a child source. The default implementation does not change the media period id.

Parameters:

childSourceId: The unique id used to prepare the child source.
mediaPeriodId: A of the child source.

Returns:

The corresponding in the composite source. Null if no corresponding media period id can be determined.

protected MediaSource.MediaPeriodId getMediaPeriodIdForChildMediaPeriodId(MediaSource.MediaPeriodId mediaPeriodId)

Returns the in the wrapping source corresponding to the specified in a child source. The default implementation does not change the media period id.

Parameters:

mediaPeriodId: A of the child source.

Returns:

The corresponding in the wrapping source. Null if no corresponding media period id can be determined.

protected long getMediaTimeForChildMediaTime(java.lang.Object childSourceId, long mediaTimeMs, MediaSource.MediaPeriodId mediaPeriodId)

Returns the media time in the MediaPeriod of the composite source corresponding to the specified media time in the MediaPeriod of the child source. The default implementation does not change the media time.

Parameters:

childSourceId: The unique id used to prepare the child source.
mediaTimeMs: A media time in the MediaPeriod of the child source, in milliseconds.
mediaPeriodId: The of the MediaPeriod of the child source, or null if the time does not relate to a specific MediaPeriod.

Returns:

The corresponding media time in the MediaPeriod of the composite source, in milliseconds.

protected long getMediaTimeForChildMediaTime(long mediaTimeMs, MediaSource.MediaPeriodId mediaPeriodId)

Returns the media time in the MediaPeriod of the wrapping source corresponding to the specified media time in the MediaPeriod of the child source. The default implementation does not change the media time.

Parameters:

mediaTimeMs: A media time in the MediaPeriod of the child source, in milliseconds.
mediaPeriodId: The of the MediaPeriod of the child source, or null if the time does not relate to a specific MediaPeriod.

Returns:

The corresponding media time in the MediaPeriod of the wrapping source, in milliseconds.

protected final void prepareChildSource()

Prepares the wrapped child source.

WrappingMediaSource.onChildSourceInfoRefreshed(Timeline) will be called when the child source updates its timeline.

If sources aren't explicitly released with WrappingMediaSource.releaseChildSource() they will be released in CompositeMediaSource.releaseSourceInternal().

protected final void enableChildSource()

Enables the child source.

protected final void disableChildSource()

Disables the child source.

protected final void releaseChildSource()

Releases the child source.

Source

/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package androidx.media3.exoplayer.source;

import androidx.annotation.Nullable;
import androidx.media3.common.MediaItem;
import androidx.media3.common.Timeline;
import androidx.media3.common.util.UnstableApi;
import androidx.media3.datasource.TransferListener;
import androidx.media3.exoplayer.analytics.PlayerId;
import androidx.media3.exoplayer.upstream.Allocator;

/**
 * An abstract {@link MediaSource} wrapping a single child {@link MediaSource}.
 *
 * <p>The implementation may want to override the following methods as needed:
 *
 * <ul>
 *   <li>{@link #getMediaItem()}: Amend the {@link MediaItem} for this media source. This is only
 *       used before the child source is prepared. You may also want to override {@link
 *       #canUpdateMediaItem} or {@link #updateMediaItem} to intercept further updates to the {@link
 *       MediaItem}.
 *   <li>{@link #onChildSourceInfoRefreshed(Timeline)}: Called whenever the child source's {@link
 *       Timeline} changed. This {@link Timeline} can be amended if needed, for example using {@link
 *       ForwardingTimeline}. The {@link Timeline} for the wrapping source needs to be published
 *       with {@link #refreshSourceInfo(Timeline)}.
 *   <li>{@link #createPeriod}/{@link #releasePeriod}: These methods create and release {@link
 *       MediaPeriod} instances. They typically forward to the wrapped media source and optionally
 *       wrap the returned {@link MediaPeriod}.
 * </ul>
 *
 * <p>Other methods like {@link #prepareSourceInternal}, {@link #enableInternal}, {@link
 * #disableInternal} or {@link #releaseSourceInternal} only need to be overwritten if required for
 * resource management.
 */
@UnstableApi
public abstract class WrappingMediaSource extends CompositeMediaSource<Void> {

  private static final Void CHILD_SOURCE_ID = null;

  /** The wrapped child {@link MediaSource}. */
  protected final MediaSource mediaSource;

  /**
   * Creates the wrapping {@link MediaSource}.
   *
   * @param mediaSource The wrapped child {@link MediaSource}.
   */
  protected WrappingMediaSource(MediaSource mediaSource) {
    this.mediaSource = mediaSource;
  }

  @Override
  protected final void prepareSourceInternal(@Nullable TransferListener mediaTransferListener) {
    super.prepareSourceInternal(mediaTransferListener);
    prepareSourceInternal();
  }

  /**
   * Starts source preparation and enables the source, see {@link #prepareSource(MediaSourceCaller,
   * TransferListener, PlayerId)}. This method is called at most once until the next call to {@link
   * #releaseSourceInternal()}.
   */
  protected void prepareSourceInternal() {
    prepareChildSource();
  }

  @Nullable
  @Override
  public Timeline getInitialTimeline() {
    return mediaSource.getInitialTimeline();
  }

  @Override
  public boolean isSingleWindow() {
    return mediaSource.isSingleWindow();
  }

  /**
   * {@inheritDoc}
   *
   * <p>This method can be overridden to amend the {@link MediaItem} of the child source. It is only
   * used before the child source is prepared.
   */
  @Override
  public MediaItem getMediaItem() {
    return mediaSource.getMediaItem();
  }

  /**
   * {@inheritDoc}
   *
   * <p>This method can be overridden to change whether the {@link MediaItem} of the child source
   * can be updated.
   */
  @Override
  public boolean canUpdateMediaItem(MediaItem mediaItem) {
    return mediaSource.canUpdateMediaItem(mediaItem);
  }

  /**
   * {@inheritDoc}
   *
   * <p>This method can be overridden to change how the {@link MediaItem} of the child source is
   * updated.
   */
  @Override
  public void updateMediaItem(MediaItem mediaItem) {
    mediaSource.updateMediaItem(mediaItem);
  }

  /**
   * Creates the requested {@link MediaPeriod}.
   *
   * <p>This method typically forwards to the wrapped media source and optionally wraps the returned
   * {@link MediaPeriod}.
   *
   * @see MediaSource#createPeriod(MediaPeriodId, Allocator, long)
   */
  @Override
  public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator, long startPositionUs) {
    return mediaSource.createPeriod(id, allocator, startPositionUs);
  }

  /**
   * Releases a {@link MediaPeriod}.
   *
   * <p>This method typically forwards to the wrapped media source and optionally unwraps the
   * provided {@link MediaPeriod}.
   *
   * @see MediaSource#releasePeriod(MediaPeriod)
   */
  @Override
  public void releasePeriod(MediaPeriod mediaPeriod) {
    mediaSource.releasePeriod(mediaPeriod);
  }

  @Override
  protected final void onChildSourceInfoRefreshed(
      Void childSourceId, MediaSource mediaSource, Timeline newTimeline) {
    onChildSourceInfoRefreshed(newTimeline);
  }

  /**
   * Called when the child source info has been refreshed.
   *
   * <p>This {@link Timeline} can be amended if needed, for example using {@link
   * ForwardingTimeline}. The {@link Timeline} for the wrapping source needs to be published with
   * {@link #refreshSourceInfo(Timeline)}.
   *
   * @param newTimeline The timeline of the child source.
   */
  protected void onChildSourceInfoRefreshed(Timeline newTimeline) {
    refreshSourceInfo(newTimeline);
  }

  @Override
  protected final int getWindowIndexForChildWindowIndex(Void childSourceId, int windowIndex) {
    return getWindowIndexForChildWindowIndex(windowIndex);
  }

  /**
   * Returns the window index in the wrapping source corresponding to the specified window index in
   * a child source. The default implementation does not change the window index.
   *
   * @param windowIndex A window index of the child source.
   * @return The corresponding window index in the wrapping source.
   */
  protected int getWindowIndexForChildWindowIndex(int windowIndex) {
    return windowIndex;
  }

  @Nullable
  @Override
  protected final MediaPeriodId getMediaPeriodIdForChildMediaPeriodId(
      Void childSourceId, MediaPeriodId mediaPeriodId) {
    return getMediaPeriodIdForChildMediaPeriodId(mediaPeriodId);
  }

  /**
   * Returns the {@link MediaPeriodId} in the wrapping source corresponding to the specified {@link
   * MediaPeriodId} in a child source. The default implementation does not change the media period
   * id.
   *
   * @param mediaPeriodId A {@link MediaPeriodId} of the child source.
   * @return The corresponding {@link MediaPeriodId} in the wrapping source. Null if no
   *     corresponding media period id can be determined.
   */
  @Nullable
  protected MediaPeriodId getMediaPeriodIdForChildMediaPeriodId(MediaPeriodId mediaPeriodId) {
    return mediaPeriodId;
  }

  @Override
  protected final long getMediaTimeForChildMediaTime(
      Void childSourceId, long mediaTimeMs, @Nullable MediaPeriodId mediaPeriodId) {
    return getMediaTimeForChildMediaTime(mediaTimeMs, mediaPeriodId);
  }

  /**
   * Returns the media time in the {@link MediaPeriod} of the wrapping source corresponding to the
   * specified media time in the {@link MediaPeriod} of the child source. The default implementation
   * does not change the media time.
   *
   * @param mediaTimeMs A media time in the {@link MediaPeriod} of the child source, in
   *     milliseconds.
   * @param mediaPeriodId The {@link MediaPeriodId} of the {@link MediaPeriod} of the child source,
   *     or null if the time does not relate to a specific {@link MediaPeriod}.
   * @return The corresponding media time in the {@link MediaPeriod} of the wrapping source, in
   *     milliseconds.
   */
  protected long getMediaTimeForChildMediaTime(
      long mediaTimeMs, @Nullable MediaPeriodId mediaPeriodId) {
    return mediaTimeMs;
  }

  /**
   * Prepares the wrapped child source.
   *
   * <p>{@link #onChildSourceInfoRefreshed(Timeline)} will be called when the child source updates
   * its timeline.
   *
   * <p>If sources aren't explicitly released with {@link #releaseChildSource()} they will be
   * released in {@link #releaseSourceInternal()}.
   */
  protected final void prepareChildSource() {
    prepareChildSource(CHILD_SOURCE_ID, mediaSource);
  }

  /** Enables the child source. */
  protected final void enableChildSource() {
    enableChildSource(CHILD_SOURCE_ID);
  }

  /** Disables the child source. */
  protected final void disableChildSource() {
    disableChildSource(CHILD_SOURCE_ID);
  }

  /** Releases the child source. */
  protected final void releaseChildSource() {
    releaseChildSource(CHILD_SOURCE_ID);
  }
}