Skip to content

Commit

Permalink
Add setPlayerId(PlayerId) and prepareSourceCalled to BaseMediaSource
Browse files Browse the repository at this point in the history
Also add getLastCreatedSource to FakeMediaSourceFactory which will be helpful in testing.

PiperOrigin-RevId: 563463475
  • Loading branch information
tianyif authored and microkatz committed Sep 21, 2023
1 parent 087f84f commit 7ff999f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,29 @@ protected final boolean isEnabled() {
* Returns the {@link PlayerId} of the player using this media source.
*
* <p>Must only be used when the media source is {@link #prepareSourceInternal(TransferListener)
* prepared}.
* prepared} or has {@linkplain #setPlayerId a player ID set}.
*/
protected final PlayerId getPlayerId() {
return checkStateNotNull(playerId);
}

/**
* Sets the {@link PlayerId} of the player using this media source.
*
* @param playerId The player ID to be set.
*/
protected final void setPlayerId(PlayerId playerId) {
this.playerId = playerId;
}

/**
* Returns whether the source has {@link MediaSource#prepareSource(MediaSourceCaller,
* TransferListener, PlayerId)} called.
*/
protected final boolean prepareSourceCalled() {
return !mediaSourceCallers.isEmpty();
}

@Override
public final void addEventListener(Handler handler, MediaSourceEventListener eventListener) {
Assertions.checkNotNull(handler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package androidx.media3.test.utils;

import static androidx.media3.common.util.Assertions.checkNotNull;

import androidx.media3.common.AdPlaybackState;
import androidx.media3.common.C;
import androidx.media3.common.MediaItem;
Expand All @@ -26,6 +28,7 @@
import androidx.media3.exoplayer.upstream.LoadErrorHandlingPolicy;
import androidx.media3.test.utils.FakeTimeline.TimelineWindowDefinition;
import com.google.common.collect.ImmutableList;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;

/** Fake {@link MediaSourceFactory} that creates a {@link FakeMediaSource}. */
@UnstableApi
Expand All @@ -36,6 +39,18 @@ public final class FakeMediaSourceFactory implements MediaSourceFactory {
/** The window UID used by media sources that are created by the factory. */
public static final Object DEFAULT_WINDOW_UID = new Object();

private @MonotonicNonNull FakeMediaSource lastCreatedSource;

/**
* Returns the last created {@link FakeMediaSource}.
*
* <p>Must be called after at least one {@link FakeMediaSource} is {@link
* FakeMediaSourceFactory#createMediaSource(MediaItem) created}.
*/
public FakeMediaSource getLastCreatedSource() {
return checkNotNull(lastCreatedSource);
}

@Override
public MediaSourceFactory setDrmSessionManagerProvider(
DrmSessionManagerProvider drmSessionManagerProvider) {
Expand Down Expand Up @@ -68,6 +83,7 @@ public MediaSource createMediaSource(MediaItem mediaItem) {
/* windowOffsetInFirstPeriodUs= */ Util.msToUs(123456789),
ImmutableList.of(AdPlaybackState.NONE),
mediaItem);
return new FakeMediaSource(new FakeTimeline(timelineWindowDefinition));
lastCreatedSource = new FakeMediaSource(new FakeTimeline(timelineWindowDefinition));
return lastCreatedSource;
}
}

0 comments on commit 7ff999f

Please sign in to comment.