Plumb PlayerId to MediaSource.

MediaSource can be reused with other Player instances after they
have been released, so we need to set the PlayerId when preparing
the source. Access can mostly be handled by the implementation in
BaseMediaSource.

PiperOrigin-RevId: 408878824
This commit is contained in:
tonihei 2021-11-10 15:55:34 +00:00 committed by Ian Baker
parent f7bb6e0a23
commit 5d8bb7ddd2
17 changed files with 104 additions and 39 deletions

View File

@ -276,7 +276,8 @@ import java.util.concurrent.atomic.AtomicBoolean;
Handler eventHandler = new Handler(applicationLooper); Handler eventHandler = new Handler(applicationLooper);
queue = new MediaPeriodQueue(analyticsCollector, eventHandler); queue = new MediaPeriodQueue(analyticsCollector, eventHandler);
mediaSourceList = new MediaSourceList(/* listener= */ this, analyticsCollector, eventHandler); mediaSourceList =
new MediaSourceList(/* listener= */ this, analyticsCollector, eventHandler, playerId);
// Note: The documentation for Process.THREAD_PRIORITY_AUDIO that states "Applications can // Note: The documentation for Process.THREAD_PRIORITY_AUDIO that states "Applications can
// not normally change to this priority" is incorrect. // not normally change to this priority" is incorrect.

View File

@ -26,6 +26,7 @@ import androidx.media3.common.util.Log;
import androidx.media3.common.util.Util; import androidx.media3.common.util.Util;
import androidx.media3.datasource.TransferListener; import androidx.media3.datasource.TransferListener;
import androidx.media3.exoplayer.analytics.AnalyticsCollector; import androidx.media3.exoplayer.analytics.AnalyticsCollector;
import androidx.media3.exoplayer.analytics.PlayerId;
import androidx.media3.exoplayer.drm.DrmSession; import androidx.media3.exoplayer.drm.DrmSession;
import androidx.media3.exoplayer.drm.DrmSessionEventListener; import androidx.media3.exoplayer.drm.DrmSessionEventListener;
import androidx.media3.exoplayer.source.LoadEventInfo; import androidx.media3.exoplayer.source.LoadEventInfo;
@ -71,6 +72,7 @@ import java.util.Set;
private static final String TAG = "MediaSourceList"; private static final String TAG = "MediaSourceList";
private final PlayerId playerId;
private final List<MediaSourceHolder> mediaSourceHolders; private final List<MediaSourceHolder> mediaSourceHolders;
private final IdentityHashMap<MediaPeriod, MediaSourceHolder> mediaSourceByMediaPeriod; private final IdentityHashMap<MediaPeriod, MediaSourceHolder> mediaSourceByMediaPeriod;
private final Map<Object, MediaSourceHolder> mediaSourceByUid; private final Map<Object, MediaSourceHolder> mediaSourceByUid;
@ -94,11 +96,14 @@ import java.util.Set;
* source events. * source events.
* @param analyticsCollectorHandler The {@link Handler} to call {@link AnalyticsCollector} methods * @param analyticsCollectorHandler The {@link Handler} to call {@link AnalyticsCollector} methods
* on. * on.
* @param playerId The {@link PlayerId} of the player using this list.
*/ */
public MediaSourceList( public MediaSourceList(
MediaSourceListInfoRefreshListener listener, MediaSourceListInfoRefreshListener listener,
@Nullable AnalyticsCollector analyticsCollector, @Nullable AnalyticsCollector analyticsCollector,
Handler analyticsCollectorHandler) { Handler analyticsCollectorHandler,
PlayerId playerId) {
this.playerId = playerId;
mediaSourceListInfoListener = listener; mediaSourceListInfoListener = listener;
shuffleOrder = new DefaultShuffleOrder(0); shuffleOrder = new DefaultShuffleOrder(0);
mediaSourceByMediaPeriod = new IdentityHashMap<>(); mediaSourceByMediaPeriod = new IdentityHashMap<>();
@ -441,7 +446,7 @@ import java.util.Set;
childSources.put(holder, new MediaSourceAndListener(mediaSource, caller, eventListener)); childSources.put(holder, new MediaSourceAndListener(mediaSource, caller, eventListener));
mediaSource.addEventListener(Util.createHandlerForCurrentOrMainLooper(), eventListener); mediaSource.addEventListener(Util.createHandlerForCurrentOrMainLooper(), eventListener);
mediaSource.addDrmEventListener(Util.createHandlerForCurrentOrMainLooper(), eventListener); mediaSource.addDrmEventListener(Util.createHandlerForCurrentOrMainLooper(), eventListener);
mediaSource.prepareSource(caller, mediaTransferListener); mediaSource.prepareSource(caller, mediaTransferListener, playerId);
} }
private void maybeReleaseChildSource(MediaSourceHolder mediaSourceHolder) { private void maybeReleaseChildSource(MediaSourceHolder mediaSourceHolder) {

View File

@ -30,6 +30,7 @@ import androidx.media3.common.TrackGroupArray;
import androidx.media3.common.util.Clock; import androidx.media3.common.util.Clock;
import androidx.media3.common.util.HandlerWrapper; import androidx.media3.common.util.HandlerWrapper;
import androidx.media3.common.util.UnstableApi; import androidx.media3.common.util.UnstableApi;
import androidx.media3.exoplayer.analytics.PlayerId;
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory; import androidx.media3.exoplayer.source.DefaultMediaSourceFactory;
import androidx.media3.exoplayer.source.MediaPeriod; import androidx.media3.exoplayer.source.MediaPeriod;
import androidx.media3.exoplayer.source.MediaSource; import androidx.media3.exoplayer.source.MediaSource;
@ -145,7 +146,8 @@ public final class MetadataRetriever {
case MESSAGE_PREPARE_SOURCE: case MESSAGE_PREPARE_SOURCE:
MediaItem mediaItem = (MediaItem) msg.obj; MediaItem mediaItem = (MediaItem) msg.obj;
mediaSource = mediaSourceFactory.createMediaSource(mediaItem); mediaSource = mediaSourceFactory.createMediaSource(mediaItem);
mediaSource.prepareSource(mediaSourceCaller, /* mediaTransferListener= */ null); mediaSource.prepareSource(
mediaSourceCaller, /* mediaTransferListener= */ null, PlayerId.UNSET);
mediaSourceHandler.sendEmptyMessage(MESSAGE_CHECK_FOR_FAILURE); mediaSourceHandler.sendEmptyMessage(MESSAGE_CHECK_FOR_FAILURE);
return true; return true;
case MESSAGE_CHECK_FOR_FAILURE: case MESSAGE_CHECK_FOR_FAILURE:

View File

@ -41,6 +41,7 @@ import androidx.media3.exoplayer.ExoPlaybackException;
import androidx.media3.exoplayer.Renderer; import androidx.media3.exoplayer.Renderer;
import androidx.media3.exoplayer.RendererCapabilities; import androidx.media3.exoplayer.RendererCapabilities;
import androidx.media3.exoplayer.RenderersFactory; import androidx.media3.exoplayer.RenderersFactory;
import androidx.media3.exoplayer.analytics.PlayerId;
import androidx.media3.exoplayer.audio.AudioRendererEventListener; import androidx.media3.exoplayer.audio.AudioRendererEventListener;
import androidx.media3.exoplayer.drm.DrmSessionManager; import androidx.media3.exoplayer.drm.DrmSessionManager;
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory; import androidx.media3.exoplayer.source.DefaultMediaSourceFactory;
@ -959,7 +960,8 @@ public final class DownloadHelper {
public boolean handleMessage(Message msg) { public boolean handleMessage(Message msg) {
switch (msg.what) { switch (msg.what) {
case MESSAGE_PREPARE_SOURCE: case MESSAGE_PREPARE_SOURCE:
mediaSource.prepareSource(/* caller= */ this, /* mediaTransferListener= */ null); mediaSource.prepareSource(
/* caller= */ this, /* mediaTransferListener= */ null, PlayerId.UNSET);
mediaSourceHandler.sendEmptyMessage(MESSAGE_CHECK_FOR_FAILURE); mediaSourceHandler.sendEmptyMessage(MESSAGE_CHECK_FOR_FAILURE);
return true; return true;
case MESSAGE_CHECK_FOR_FAILURE: case MESSAGE_CHECK_FOR_FAILURE:

View File

@ -15,6 +15,8 @@
*/ */
package androidx.media3.exoplayer.source; package androidx.media3.exoplayer.source;
import static androidx.media3.common.util.Assertions.checkStateNotNull;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -22,6 +24,7 @@ import androidx.media3.common.Timeline;
import androidx.media3.common.util.Assertions; import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.UnstableApi; import androidx.media3.common.util.UnstableApi;
import androidx.media3.datasource.TransferListener; import androidx.media3.datasource.TransferListener;
import androidx.media3.exoplayer.analytics.PlayerId;
import androidx.media3.exoplayer.drm.DrmSessionEventListener; import androidx.media3.exoplayer.drm.DrmSessionEventListener;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
@ -43,6 +46,7 @@ public abstract class BaseMediaSource implements MediaSource {
@Nullable private Looper looper; @Nullable private Looper looper;
@Nullable private Timeline timeline; @Nullable private Timeline timeline;
@Nullable private PlayerId playerId;
public BaseMediaSource() { public BaseMediaSource() {
mediaSourceCallers = new ArrayList<>(/* initialCapacity= */ 1); mediaSourceCallers = new ArrayList<>(/* initialCapacity= */ 1);
@ -53,7 +57,7 @@ public abstract class BaseMediaSource implements MediaSource {
/** /**
* Starts source preparation and enables the source, see {@link #prepareSource(MediaSourceCaller, * Starts source preparation and enables the source, see {@link #prepareSource(MediaSourceCaller,
* TransferListener)}. This method is called at most once until the next call to {@link * TransferListener, PlayerId)}. This method is called at most once until the next call to {@link
* #releaseSourceInternal()}. * #releaseSourceInternal()}.
* *
* @param mediaTransferListener The transfer listener which should be informed of any media data * @param mediaTransferListener The transfer listener which should be informed of any media data
@ -162,6 +166,16 @@ public abstract class BaseMediaSource implements MediaSource {
return !enabledMediaSourceCallers.isEmpty(); return !enabledMediaSourceCallers.isEmpty();
} }
/**
* 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}.
*/
protected final PlayerId getPlayerId() {
return checkStateNotNull(playerId);
}
@Override @Override
public final void addEventListener(Handler handler, MediaSourceEventListener eventListener) { public final void addEventListener(Handler handler, MediaSourceEventListener eventListener) {
Assertions.checkNotNull(handler); Assertions.checkNotNull(handler);
@ -188,9 +202,12 @@ public abstract class BaseMediaSource implements MediaSource {
@Override @Override
public final void prepareSource( public final void prepareSource(
MediaSourceCaller caller, @Nullable TransferListener mediaTransferListener) { MediaSourceCaller caller,
@Nullable TransferListener mediaTransferListener,
PlayerId playerId) {
Looper looper = Looper.myLooper(); Looper looper = Looper.myLooper();
Assertions.checkArgument(this.looper == null || this.looper == looper); Assertions.checkArgument(this.looper == null || this.looper == looper);
this.playerId = playerId;
@Nullable Timeline timeline = this.timeline; @Nullable Timeline timeline = this.timeline;
mediaSourceCallers.add(caller); mediaSourceCallers.add(caller);
if (this.looper == null) { if (this.looper == null) {
@ -228,6 +245,7 @@ public abstract class BaseMediaSource implements MediaSource {
if (mediaSourceCallers.isEmpty()) { if (mediaSourceCallers.isEmpty()) {
looper = null; looper = null;
timeline = null; timeline = null;
playerId = null;
enabledMediaSourceCallers.clear(); enabledMediaSourceCallers.clear();
releaseSourceInternal(); releaseSourceInternal();
} else { } else {

View File

@ -119,7 +119,7 @@ public abstract class CompositeMediaSource<T> extends BaseMediaSource {
childSources.put(id, new MediaSourceAndListener<>(mediaSource, caller, eventListener)); childSources.put(id, new MediaSourceAndListener<>(mediaSource, caller, eventListener));
mediaSource.addEventListener(Assertions.checkNotNull(eventHandler), eventListener); mediaSource.addEventListener(Assertions.checkNotNull(eventHandler), eventListener);
mediaSource.addDrmEventListener(Assertions.checkNotNull(eventHandler), eventListener); mediaSource.addDrmEventListener(Assertions.checkNotNull(eventHandler), eventListener);
mediaSource.prepareSource(caller, mediaTransferListener); mediaSource.prepareSource(caller, mediaTransferListener, getPlayerId());
if (!isEnabled()) { if (!isEnabled()) {
mediaSource.disable(caller); mediaSource.disable(caller);
} }

View File

@ -22,6 +22,7 @@ import androidx.media3.common.Timeline;
import androidx.media3.common.util.UnstableApi; import androidx.media3.common.util.UnstableApi;
import androidx.media3.datasource.TransferListener; import androidx.media3.datasource.TransferListener;
import androidx.media3.exoplayer.ExoPlayer; import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.exoplayer.analytics.PlayerId;
import androidx.media3.exoplayer.drm.DrmSessionEventListener; import androidx.media3.exoplayer.drm.DrmSessionEventListener;
import androidx.media3.exoplayer.upstream.Allocator; import androidx.media3.exoplayer.upstream.Allocator;
import java.io.IOException; import java.io.IOException;
@ -35,7 +36,7 @@ import java.io.IOException;
* provide a new timeline whenever the structure of the media changes. The MediaSource * provide a new timeline whenever the structure of the media changes. The MediaSource
* provides these timelines by calling {@link MediaSourceCaller#onSourceInfoRefreshed} on the * provides these timelines by calling {@link MediaSourceCaller#onSourceInfoRefreshed} on the
* {@link MediaSourceCaller}s passed to {@link #prepareSource(MediaSourceCaller, * {@link MediaSourceCaller}s passed to {@link #prepareSource(MediaSourceCaller,
* TransferListener)}. * TransferListener, PlayerId)}.
* <li>To provide {@link MediaPeriod} instances for the periods in its timeline. MediaPeriods are * <li>To provide {@link MediaPeriod} instances for the periods in its timeline. MediaPeriods are
* obtained by calling {@link #createPeriod(MediaPeriodId, Allocator, long)}, and provide a * obtained by calling {@link #createPeriod(MediaPeriodId, Allocator, long)}, and provide a
* way for the player to load and read the media. * way for the player to load and read the media.
@ -173,6 +174,16 @@ public interface MediaSource {
/** Returns the {@link MediaItem} whose media is provided by the source. */ /** Returns the {@link MediaItem} whose media is provided by the source. */
MediaItem getMediaItem(); MediaItem getMediaItem();
/**
* @deprecated Implement {@link #prepareSource(MediaSourceCaller, TransferListener, PlayerId)}
* instead.
*/
@Deprecated
default void prepareSource(
MediaSourceCaller caller, @Nullable TransferListener mediaTransferListener) {
prepareSource(caller, mediaTransferListener, PlayerId.UNSET);
}
/** /**
* Registers a {@link MediaSourceCaller}. Starts source preparation if needed and enables the * Registers a {@link MediaSourceCaller}. Starts source preparation if needed and enables the
* source for the creation of {@link MediaPeriod MediaPerods}. * source for the creation of {@link MediaPeriod MediaPerods}.
@ -190,15 +201,20 @@ public interface MediaSource {
* transfers. May be null if no listener is available. Note that this listener should be only * 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 * informed of transfers related to the media loads and not of auxiliary loads for manifests
* and other data. * and other data.
* @param playerId The {@link PlayerId} of the player using this media source.
*/ */
void prepareSource(MediaSourceCaller caller, @Nullable TransferListener mediaTransferListener); void prepareSource(
MediaSourceCaller caller,
@Nullable TransferListener mediaTransferListener,
PlayerId playerId);
/** /**
* Throws any pending error encountered while loading or refreshing source information. * Throws any pending error encountered while loading or refreshing source information.
* *
* <p>Should not be called directly from application code. * <p>Should not be called directly from application code.
* *
* <p>Must only be called after {@link #prepareSource(MediaSourceCaller, TransferListener)}. * <p>Must only be called after {@link #prepareSource(MediaSourceCaller, TransferListener,
* PlayerId)}.
*/ */
void maybeThrowSourceInfoRefreshError() throws IOException; void maybeThrowSourceInfoRefreshError() throws IOException;
@ -207,7 +223,8 @@ public interface MediaSource {
* *
* <p>Should not be called directly from application code. * <p>Should not be called directly from application code.
* *
* <p>Must only be called after {@link #prepareSource(MediaSourceCaller, TransferListener)}. * <p>Must only be called after {@link #prepareSource(MediaSourceCaller, TransferListener,
* PlayerId)}.
* *
* @param caller The {@link MediaSourceCaller} enabling the source. * @param caller The {@link MediaSourceCaller} enabling the source.
*/ */

View File

@ -171,7 +171,7 @@ public final class ServerSideInsertedAdsMediaSource extends BaseMediaSource
} }
mediaSource.addEventListener(handler, /* eventListener= */ this); mediaSource.addEventListener(handler, /* eventListener= */ this);
mediaSource.addDrmEventListener(handler, /* eventListener= */ this); mediaSource.addDrmEventListener(handler, /* eventListener= */ this);
mediaSource.prepareSource(/* caller= */ this, mediaTransferListener); mediaSource.prepareSource(/* caller= */ this, mediaTransferListener, getPlayerId());
} }
@Override @Override

View File

@ -29,7 +29,9 @@ import androidx.media3.common.PlaybackParameters;
import androidx.media3.common.Player; import androidx.media3.common.Player;
import androidx.media3.common.Timeline; import androidx.media3.common.Timeline;
import androidx.media3.common.TracksInfo; import androidx.media3.common.TracksInfo;
import androidx.media3.exoplayer.analytics.PlayerId;
import androidx.media3.exoplayer.source.MediaSource.MediaPeriodId; import androidx.media3.exoplayer.source.MediaSource.MediaPeriodId;
import androidx.media3.exoplayer.source.MediaSource.MediaSourceCaller;
import androidx.media3.exoplayer.source.SinglePeriodTimeline; import androidx.media3.exoplayer.source.SinglePeriodTimeline;
import androidx.media3.exoplayer.source.ads.SinglePeriodAdTimeline; import androidx.media3.exoplayer.source.ads.SinglePeriodAdTimeline;
import androidx.media3.exoplayer.trackselection.ExoTrackSelection; import androidx.media3.exoplayer.trackselection.ExoTrackSelection;
@ -84,7 +86,8 @@ public final class MediaPeriodQueueTest {
new MediaSourceList( new MediaSourceList(
mock(MediaSourceList.MediaSourceListInfoRefreshListener.class), mock(MediaSourceList.MediaSourceListInfoRefreshListener.class),
/* analyticsCollector= */ null, /* analyticsCollector= */ null,
new Handler(Looper.getMainLooper())); new Handler(Looper.getMainLooper()),
PlayerId.UNSET);
rendererCapabilities = new RendererCapabilities[0]; rendererCapabilities = new RendererCapabilities[0];
trackSelector = mock(TrackSelector.class); trackSelector = mock(TrackSelector.class);
allocator = mock(Allocator.class); allocator = mock(Allocator.class);
@ -744,7 +747,8 @@ public final class MediaPeriodQueueTest {
new MediaSourceList.MediaSourceHolder(fakeMediaSource, /* useLazyPreparation= */ false); new MediaSourceList.MediaSourceHolder(fakeMediaSource, /* useLazyPreparation= */ false);
mediaSourceList.setMediaSources( mediaSourceList.setMediaSources(
ImmutableList.of(mediaSourceHolder), new FakeShuffleOrder(/* length= */ 1)); ImmutableList.of(mediaSourceHolder), new FakeShuffleOrder(/* length= */ 1));
mediaSourceHolder.mediaSource.prepareSourceInternal(/* mediaTransferListener */ null); mediaSourceHolder.mediaSource.prepareSource(
mock(MediaSourceCaller.class), /* mediaTransferListener */ null, PlayerId.UNSET);
Timeline playlistTimeline = mediaSourceList.createTimeline(); Timeline playlistTimeline = mediaSourceList.createTimeline();
firstPeriodUid = playlistTimeline.getUidOfPeriod(/* periodIndex= */ 0); firstPeriodUid = playlistTimeline.getUidOfPeriod(/* periodIndex= */ 0);

View File

@ -29,6 +29,7 @@ import androidx.media3.common.MediaItem;
import androidx.media3.common.Player; import androidx.media3.common.Player;
import androidx.media3.common.Timeline; import androidx.media3.common.Timeline;
import androidx.media3.common.util.Util; import androidx.media3.common.util.Util;
import androidx.media3.exoplayer.analytics.PlayerId;
import androidx.media3.exoplayer.source.MediaSource; import androidx.media3.exoplayer.source.MediaSource;
import androidx.media3.exoplayer.source.ShuffleOrder; import androidx.media3.exoplayer.source.ShuffleOrder;
import androidx.media3.test.utils.FakeMediaSource; import androidx.media3.test.utils.FakeMediaSource;
@ -57,7 +58,8 @@ public class MediaSourceListTest {
new MediaSourceList( new MediaSourceList(
mock(MediaSourceList.MediaSourceListInfoRefreshListener.class), mock(MediaSourceList.MediaSourceListInfoRefreshListener.class),
/* analyticsCollector= */ null, /* analyticsCollector= */ null,
Util.createHandlerForCurrentOrMainLooper()); Util.createHandlerForCurrentOrMainLooper(),
PlayerId.UNSET);
} }
@Test @Test
@ -95,30 +97,30 @@ public class MediaSourceListTest {
// Verify prepare is called once on prepare. // Verify prepare is called once on prepare.
verify(mockMediaSource1, times(0)) verify(mockMediaSource1, times(0))
.prepareSource( .prepareSource(
any(MediaSource.MediaSourceCaller.class), /* mediaTransferListener= */ isNull()); any(MediaSource.MediaSourceCaller.class), /* mediaTransferListener= */ isNull(), any());
verify(mockMediaSource2, times(0)) verify(mockMediaSource2, times(0))
.prepareSource( .prepareSource(
any(MediaSource.MediaSourceCaller.class), /* mediaTransferListener= */ isNull()); any(MediaSource.MediaSourceCaller.class), /* mediaTransferListener= */ isNull(), any());
mediaSourceList.prepare(/* mediaTransferListener= */ null); mediaSourceList.prepare(/* mediaTransferListener= */ null);
assertThat(mediaSourceList.isPrepared()).isTrue(); assertThat(mediaSourceList.isPrepared()).isTrue();
// Verify prepare is called once on prepare. // Verify prepare is called once on prepare.
verify(mockMediaSource1, times(1)) verify(mockMediaSource1, times(1))
.prepareSource( .prepareSource(
any(MediaSource.MediaSourceCaller.class), /* mediaTransferListener= */ isNull()); any(MediaSource.MediaSourceCaller.class), /* mediaTransferListener= */ isNull(), any());
verify(mockMediaSource2, times(1)) verify(mockMediaSource2, times(1))
.prepareSource( .prepareSource(
any(MediaSource.MediaSourceCaller.class), /* mediaTransferListener= */ isNull()); any(MediaSource.MediaSourceCaller.class), /* mediaTransferListener= */ isNull(), any());
mediaSourceList.release(); mediaSourceList.release();
mediaSourceList.prepare(/* mediaTransferListener= */ null); mediaSourceList.prepare(/* mediaTransferListener= */ null);
// Verify prepare is called a second time on re-prepare. // Verify prepare is called a second time on re-prepare.
verify(mockMediaSource1, times(2)) verify(mockMediaSource1, times(2))
.prepareSource( .prepareSource(
any(MediaSource.MediaSourceCaller.class), /* mediaTransferListener= */ isNull()); any(MediaSource.MediaSourceCaller.class), /* mediaTransferListener= */ isNull(), any());
verify(mockMediaSource2, times(2)) verify(mockMediaSource2, times(2))
.prepareSource( .prepareSource(
any(MediaSource.MediaSourceCaller.class), /* mediaTransferListener= */ isNull()); any(MediaSource.MediaSourceCaller.class), /* mediaTransferListener= */ isNull(), any());
} }
@Test @Test
@ -185,10 +187,10 @@ public class MediaSourceListTest {
// Verify sources are prepared. // Verify sources are prepared.
verify(mockMediaSource1, times(1)) verify(mockMediaSource1, times(1))
.prepareSource( .prepareSource(
any(MediaSource.MediaSourceCaller.class), /* mediaTransferListener= */ isNull()); any(MediaSource.MediaSourceCaller.class), /* mediaTransferListener= */ isNull(), any());
verify(mockMediaSource2, times(1)) verify(mockMediaSource2, times(1))
.prepareSource( .prepareSource(
any(MediaSource.MediaSourceCaller.class), /* mediaTransferListener= */ isNull()); any(MediaSource.MediaSourceCaller.class), /* mediaTransferListener= */ isNull(), any());
// Set media items again. The second holder is re-used. // Set media items again. The second holder is re-used.
MediaSource mockMediaSource3 = mock(MediaSource.class); MediaSource mockMediaSource3 = mock(MediaSource.class);
@ -206,7 +208,7 @@ public class MediaSourceListTest {
assertThat(mediaSources.get(1).isRemoved).isFalse(); assertThat(mediaSources.get(1).isRemoved).isFalse();
verify(mockMediaSource2, times(2)) verify(mockMediaSource2, times(2))
.prepareSource( .prepareSource(
any(MediaSource.MediaSourceCaller.class), /* mediaTransferListener= */ isNull()); any(MediaSource.MediaSourceCaller.class), /* mediaTransferListener= */ isNull(), any());
} }
@Test @Test
@ -225,10 +227,10 @@ public class MediaSourceListTest {
// Verify lazy initialization does not call prepare on sources. // Verify lazy initialization does not call prepare on sources.
verify(mockMediaSource1, times(0)) verify(mockMediaSource1, times(0))
.prepareSource( .prepareSource(
any(MediaSource.MediaSourceCaller.class), /* mediaTransferListener= */ isNull()); any(MediaSource.MediaSourceCaller.class), /* mediaTransferListener= */ isNull(), any());
verify(mockMediaSource2, times(0)) verify(mockMediaSource2, times(0))
.prepareSource( .prepareSource(
any(MediaSource.MediaSourceCaller.class), /* mediaTransferListener= */ isNull()); any(MediaSource.MediaSourceCaller.class), /* mediaTransferListener= */ isNull(), any());
for (int i = 0; i < mediaSources.size(); i++) { for (int i = 0; i < mediaSources.size(); i++) {
assertThat(mediaSources.get(i).firstWindowIndexInChild).isEqualTo(i); assertThat(mediaSources.get(i).firstWindowIndexInChild).isEqualTo(i);
@ -262,10 +264,10 @@ public class MediaSourceListTest {
// Verify prepare is called on sources when added. // Verify prepare is called on sources when added.
verify(mockMediaSource1, times(1)) verify(mockMediaSource1, times(1))
.prepareSource( .prepareSource(
any(MediaSource.MediaSourceCaller.class), /* mediaTransferListener= */ isNull()); any(MediaSource.MediaSourceCaller.class), /* mediaTransferListener= */ isNull(), any());
verify(mockMediaSource2, times(1)) verify(mockMediaSource2, times(1))
.prepareSource( .prepareSource(
any(MediaSource.MediaSourceCaller.class), /* mediaTransferListener= */ isNull()); any(MediaSource.MediaSourceCaller.class), /* mediaTransferListener= */ isNull(), any());
} }
@Test @Test
@ -390,7 +392,7 @@ public class MediaSourceListTest {
new ShuffleOrder.DefaultShuffleOrder(/* length= */ 1)); new ShuffleOrder.DefaultShuffleOrder(/* length= */ 1));
verify(mockMediaSource, times(0)) verify(mockMediaSource, times(0))
.prepareSource( .prepareSource(
any(MediaSource.MediaSourceCaller.class), /* mediaTransferListener= */ isNull()); any(MediaSource.MediaSourceCaller.class), /* mediaTransferListener= */ isNull(), any());
mediaSourceList.release(); mediaSourceList.release();
verify(mockMediaSource, times(0)).releaseSource(any(MediaSource.MediaSourceCaller.class)); verify(mockMediaSource, times(0)).releaseSource(any(MediaSource.MediaSourceCaller.class));
assertThat(mediaSourceHolder.isRemoved).isFalse(); assertThat(mediaSourceHolder.isRemoved).isFalse();
@ -409,7 +411,7 @@ public class MediaSourceListTest {
new ShuffleOrder.DefaultShuffleOrder(/* length= */ 1)); new ShuffleOrder.DefaultShuffleOrder(/* length= */ 1));
verify(mockMediaSource, times(1)) verify(mockMediaSource, times(1))
.prepareSource( .prepareSource(
any(MediaSource.MediaSourceCaller.class), /* mediaTransferListener= */ isNull()); any(MediaSource.MediaSourceCaller.class), /* mediaTransferListener= */ isNull(), any());
mediaSourceList.release(); mediaSourceList.release();
verify(mockMediaSource, times(1)).releaseSource(any(MediaSource.MediaSourceCaller.class)); verify(mockMediaSource, times(1)).releaseSource(any(MediaSource.MediaSourceCaller.class));
assertThat(mediaSourceHolder.isRemoved).isFalse(); assertThat(mediaSourceHolder.isRemoved).isFalse();

View File

@ -24,6 +24,7 @@ import androidx.media3.common.C;
import androidx.media3.common.Player; import androidx.media3.common.Player;
import androidx.media3.common.Timeline; import androidx.media3.common.Timeline;
import androidx.media3.common.util.Util; import androidx.media3.common.util.Util;
import androidx.media3.exoplayer.analytics.PlayerId;
import androidx.media3.exoplayer.source.MediaSource.MediaPeriodId; import androidx.media3.exoplayer.source.MediaSource.MediaPeriodId;
import androidx.media3.exoplayer.source.MediaSource.MediaSourceCaller; import androidx.media3.exoplayer.source.MediaSource.MediaSourceCaller;
import androidx.media3.exoplayer.source.ShuffleOrder.DefaultShuffleOrder; import androidx.media3.exoplayer.source.ShuffleOrder.DefaultShuffleOrder;
@ -644,7 +645,7 @@ public final class ConcatenatingMediaSourceTest {
() -> { () -> {
MediaSourceCaller caller = mock(MediaSourceCaller.class); MediaSourceCaller caller = mock(MediaSourceCaller.class);
mediaSource.addMediaSources(Arrays.asList(createMediaSources(2))); mediaSource.addMediaSources(Arrays.asList(createMediaSources(2)));
mediaSource.prepareSource(caller, /* mediaTransferListener= */ null); mediaSource.prepareSource(caller, /* mediaTransferListener= */ null, PlayerId.UNSET);
mediaSource.moveMediaSource( mediaSource.moveMediaSource(
/* currentIndex= */ 0, /* currentIndex= */ 0,
/* newIndex= */ 1, /* newIndex= */ 1,

View File

@ -31,6 +31,7 @@ import androidx.media3.common.C;
import androidx.media3.common.MediaItem; import androidx.media3.common.MediaItem;
import androidx.media3.common.Timeline; import androidx.media3.common.Timeline;
import androidx.media3.datasource.DataSpec; import androidx.media3.datasource.DataSpec;
import androidx.media3.exoplayer.analytics.PlayerId;
import androidx.media3.exoplayer.source.MediaPeriod; import androidx.media3.exoplayer.source.MediaPeriod;
import androidx.media3.exoplayer.source.MediaSource.MediaPeriodId; import androidx.media3.exoplayer.source.MediaSource.MediaPeriodId;
import androidx.media3.exoplayer.source.MediaSource.MediaSourceCaller; import androidx.media3.exoplayer.source.MediaSource.MediaSourceCaller;
@ -118,7 +119,8 @@ public final class AdsMediaSourceTest {
adMediaSourceFactory, adMediaSourceFactory,
mockAdsLoader, mockAdsLoader,
mockAdViewProvider); mockAdViewProvider);
adsMediaSource.prepareSource(mockMediaSourceCaller, /* mediaTransferListener= */ null); adsMediaSource.prepareSource(
mockMediaSourceCaller, /* mediaTransferListener= */ null, PlayerId.UNSET);
shadowOf(Looper.getMainLooper()).idle(); shadowOf(Looper.getMainLooper()).idle();
verify(mockAdsLoader) verify(mockAdsLoader)
.start( .start(

View File

@ -39,6 +39,7 @@ import androidx.media3.common.Player;
import androidx.media3.common.Timeline; import androidx.media3.common.Timeline;
import androidx.media3.exoplayer.ExoPlayer; import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.exoplayer.analytics.AnalyticsListener; import androidx.media3.exoplayer.analytics.AnalyticsListener;
import androidx.media3.exoplayer.analytics.PlayerId;
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory; import androidx.media3.exoplayer.source.DefaultMediaSourceFactory;
import androidx.media3.test.utils.CapturingRenderersFactory; import androidx.media3.test.utils.CapturingRenderersFactory;
import androidx.media3.test.utils.DumpFileAsserts; import androidx.media3.test.utils.DumpFileAsserts;
@ -105,7 +106,9 @@ public final class ServerSideInsertedAdMediaSourceTest {
mediaSource.setAdPlaybackState(adPlaybackState); mediaSource.setAdPlaybackState(adPlaybackState);
mediaSource.prepareSource( mediaSource.prepareSource(
(source, timeline) -> timelineReference.set(timeline), /* mediaTransferListener= */ null); (source, timeline) -> timelineReference.set(timeline),
/* mediaTransferListener= */ null,
PlayerId.UNSET);
runMainLooperUntil(() -> timelineReference.get() != null); runMainLooperUntil(() -> timelineReference.get() != null);
Timeline timeline = timelineReference.get(); Timeline timeline = timelineReference.get();

View File

@ -30,6 +30,7 @@ import androidx.media3.common.util.Util;
import androidx.media3.datasource.ByteArrayDataSource; import androidx.media3.datasource.ByteArrayDataSource;
import androidx.media3.datasource.DataSource; import androidx.media3.datasource.DataSource;
import androidx.media3.datasource.FileDataSource; import androidx.media3.datasource.FileDataSource;
import androidx.media3.exoplayer.analytics.PlayerId;
import androidx.media3.exoplayer.source.MediaSource; import androidx.media3.exoplayer.source.MediaSource;
import androidx.media3.exoplayer.source.MediaSource.MediaSourceCaller; import androidx.media3.exoplayer.source.MediaSource.MediaSourceCaller;
import androidx.media3.exoplayer.upstream.ParsingLoadable; import androidx.media3.exoplayer.upstream.ParsingLoadable;
@ -484,7 +485,7 @@ public final class DashMediaSourceTest {
countDownLatch.countDown(); countDownLatch.countDown();
} }
}; };
mediaSource.prepareSource(caller, /* mediaTransferListener= */ null); mediaSource.prepareSource(caller, /* mediaTransferListener= */ null, PlayerId.UNSET);
while (!countDownLatch.await(/* timeout= */ 10, MILLISECONDS)) { while (!countDownLatch.await(/* timeout= */ 10, MILLISECONDS)) {
ShadowLooper.idleMainLooper(); ShadowLooper.idleMainLooper();
} }

View File

@ -27,6 +27,7 @@ import androidx.media3.common.StreamKey;
import androidx.media3.common.Timeline; import androidx.media3.common.Timeline;
import androidx.media3.common.util.Util; import androidx.media3.common.util.Util;
import androidx.media3.datasource.DataSource; import androidx.media3.datasource.DataSource;
import androidx.media3.exoplayer.analytics.PlayerId;
import androidx.media3.exoplayer.hls.playlist.HlsMediaPlaylist; import androidx.media3.exoplayer.hls.playlist.HlsMediaPlaylist;
import androidx.media3.exoplayer.hls.playlist.HlsPlaylistParser; import androidx.media3.exoplayer.hls.playlist.HlsPlaylistParser;
import androidx.media3.exoplayer.source.MediaSource; import androidx.media3.exoplayer.source.MediaSource;
@ -752,7 +753,7 @@ public class HlsMediaSourceTest {
List<Timeline> timelines = new ArrayList<>(); List<Timeline> timelines = new ArrayList<>();
MediaSource.MediaSourceCaller mediaSourceCaller = (source, timeline) -> timelines.add(timeline); MediaSource.MediaSourceCaller mediaSourceCaller = (source, timeline) -> timelines.add(timeline);
mediaSource.prepareSource(mediaSourceCaller, null); mediaSource.prepareSource(mediaSourceCaller, /* mediaTransferListener= */ null, PlayerId.UNSET);
runMainLooperUntil(() -> timelines.size() == 1); runMainLooperUntil(() -> timelines.size() == 1);
mediaSource.onPrimaryPlaylistRefreshed(secondPlaylist); mediaSource.onPrimaryPlaylistRefreshed(secondPlaylist);
runMainLooperUntil(() -> timelines.size() == 2); runMainLooperUntil(() -> timelines.size() == 2);
@ -785,7 +786,9 @@ public class HlsMediaSourceTest {
throws TimeoutException { throws TimeoutException {
AtomicReference<Timeline> receivedTimeline = new AtomicReference<>(); AtomicReference<Timeline> receivedTimeline = new AtomicReference<>();
mediaSource.prepareSource( mediaSource.prepareSource(
(source, timeline) -> receivedTimeline.set(timeline), /* mediaTransferListener= */ null); (source, timeline) -> receivedTimeline.set(timeline),
/* mediaTransferListener= */ null,
PlayerId.UNSET);
runMainLooperUntil(() -> receivedTimeline.get() != null); runMainLooperUntil(() -> receivedTimeline.get() != null);
return receivedTimeline.get(); return receivedTimeline.get();
} }

View File

@ -29,6 +29,7 @@ import androidx.media3.common.Timeline;
import androidx.media3.common.util.Assertions; import androidx.media3.common.util.Assertions;
import androidx.media3.common.util.UnstableApi; import androidx.media3.common.util.UnstableApi;
import androidx.media3.common.util.Util; import androidx.media3.common.util.Util;
import androidx.media3.exoplayer.analytics.PlayerId;
import androidx.media3.exoplayer.source.LoadEventInfo; import androidx.media3.exoplayer.source.LoadEventInfo;
import androidx.media3.exoplayer.source.MediaLoadData; import androidx.media3.exoplayer.source.MediaLoadData;
import androidx.media3.exoplayer.source.MediaPeriod; import androidx.media3.exoplayer.source.MediaPeriod;
@ -117,7 +118,8 @@ public class MediaSourceTestRunner {
final IOException[] prepareError = new IOException[1]; final IOException[] prepareError = new IOException[1];
runOnPlaybackThread( runOnPlaybackThread(
() -> { () -> {
mediaSource.prepareSource(mediaSourceListener, /* mediaTransferListener= */ null); mediaSource.prepareSource(
mediaSourceListener, /* mediaTransferListener= */ null, PlayerId.UNSET);
try { try {
// TODO: This only catches errors that are set synchronously in prepareSource. To // TODO: This only catches errors that are set synchronously in prepareSource. To
// capture async errors we'll need to poll maybeThrowSourceInfoRefreshError until the // capture async errors we'll need to poll maybeThrowSourceInfoRefreshError until the

View File

@ -20,6 +20,7 @@ import static com.google.common.truth.Truth.assertThat;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.media3.common.MediaItem; import androidx.media3.common.MediaItem;
import androidx.media3.common.Timeline.Window; import androidx.media3.common.Timeline.Window;
import androidx.media3.exoplayer.analytics.PlayerId;
import androidx.media3.exoplayer.source.MediaSource; import androidx.media3.exoplayer.source.MediaSource;
import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
@ -42,7 +43,8 @@ public class FakeMediaSourceFactoryTest {
int firstWindowIndex = timeline.getFirstWindowIndex(/* shuffleModeEnabled= */ false); int firstWindowIndex = timeline.getFirstWindowIndex(/* shuffleModeEnabled= */ false);
reportedMediaItem.set(timeline.getWindow(firstWindowIndex, new Window()).mediaItem); reportedMediaItem.set(timeline.getWindow(firstWindowIndex, new Window()).mediaItem);
}, },
/* mediaTransferListener= */ null); /* mediaTransferListener= */ null,
PlayerId.UNSET);
assertThat(reportedMediaItem.get()).isSameInstanceAs(mediaItem); assertThat(reportedMediaItem.get()).isSameInstanceAs(mediaItem);
assertThat(mediaSource.getMediaItem()).isSameInstanceAs(mediaItem); assertThat(mediaSource.getMediaItem()).isSameInstanceAs(mediaItem);