Skip to content

Commit

Permalink
Pass player ID to methods of LoadControl
Browse files Browse the repository at this point in the history
The old methods are deprecated and are called from the new
method for backwards compatibility of custom implementations.

'DefaultLoadControl' is unchanged, but `ExoPlayerImplInternal`
already calls the new methods passing in the `PlayerId`,

PiperOrigin-RevId: 613197190
  • Loading branch information
marcbaechinger authored and copybara-github committed Mar 6, 2024
1 parent a90a704 commit a0a4087
Show file tree
Hide file tree
Showing 10 changed files with 320 additions and 76 deletions.
2 changes: 2 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
`DefaultPreloadManager` which uses `PreloadMediaSource` to preload media
samples of the sources into memory, and uses an integer `rankingData`
that indicates the index of an item on the UI.
* Add `PlayerId` to most methods of `LoadControl` to enable `LoadControl`
implementations to support multiple players.
* Transformer:
* Add `audioConversionProcess` and `videoConversionProcess` to
`ExportResult` indicating how the respective track in the output file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import androidx.media3.exoplayer.analytics.AnalyticsCollector;
import androidx.media3.exoplayer.analytics.AnalyticsListener;
import androidx.media3.exoplayer.analytics.DefaultAnalyticsCollector;
import androidx.media3.exoplayer.analytics.PlayerId;
import androidx.media3.exoplayer.audio.AudioSink;
import androidx.media3.exoplayer.audio.MediaCodecAudioRenderer;
import androidx.media3.exoplayer.image.ImageOutput;
Expand Down Expand Up @@ -478,6 +479,7 @@ final class Builder {
@Nullable /* package */ Looper playbackLooper;
/* package */ boolean buildCalled;
/* package */ boolean suppressPlaybackOnUnsuitableOutput;
/* package */ String playerName;

/**
* Creates a builder.
Expand Down Expand Up @@ -678,6 +680,7 @@ private Builder(
releaseTimeoutMs = DEFAULT_RELEASE_TIMEOUT_MS;
detachSurfaceTimeoutMs = DEFAULT_DETACH_SURFACE_TIMEOUT_MS;
usePlatformDiagnostics = true;
playerName = "";
}

/**
Expand Down Expand Up @@ -1187,6 +1190,24 @@ public Builder setPlaybackLooper(Looper playbackLooper) {
return this;
}

/**
* Sets the player name that is included in the {@link PlayerId} for informational purpose to
* recognize the player by its {@link PlayerId}.
*
* <p>The default is an empty string.
*
* @param playerName A name for the player in the {@link PlayerId}.
* @return This builder.
* @throws IllegalStateException If {@link #build()} has already been called.
*/
@CanIgnoreReturnValue
@UnstableApi
public Builder setName(String playerName) {
checkState(!buildCalled);
this.playerName = playerName;
return this;
}

/**
* Builds an {@link ExoPlayer} instance.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,12 @@ public ExoPlayerImpl(ExoPlayer.Builder builder, @Nullable Player wrappingPlayer)
analyticsCollector.setPlayer(this.wrappingPlayer, applicationLooper);
PlayerId playerId =
Util.SDK_INT < 31
? new PlayerId()
? new PlayerId(builder.playerName)
: Api31.registerMediaMetricsListener(
applicationContext, /* player= */ this, builder.usePlatformDiagnostics);
applicationContext,
/* player= */ this,
builder.usePlatformDiagnostics,
builder.playerName);
internalPlayer =
new ExoPlayerImplInternal(
renderers,
Expand Down Expand Up @@ -3386,16 +3389,16 @@ private Api31() {}

@DoNotInline
public static PlayerId registerMediaMetricsListener(
Context context, ExoPlayerImpl player, boolean usePlatformDiagnostics) {
Context context, ExoPlayerImpl player, boolean usePlatformDiagnostics, String playerName) {
@Nullable MediaMetricsListener listener = MediaMetricsListener.create(context);
if (listener == null) {
Log.w(TAG, "MediaMetricsService unavailable.");
return new PlayerId(LogSessionId.LOG_SESSION_ID_NONE);
return new PlayerId(LogSessionId.LOG_SESSION_ID_NONE, playerName);
}
if (usePlatformDiagnostics) {
player.addAnalyticsListener(listener);
}
return new PlayerId(listener.getLogSessionId());
return new PlayerId(listener.getLogSessionId(), playerName);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ public interface PlaybackInfoUpdateListener {
private final MediaSourceList mediaSourceList;
private final LivePlaybackSpeedControl livePlaybackSpeedControl;
private final long releaseTimeoutMs;
private final PlayerId playerId;

@SuppressWarnings("unused")
private SeekParameters seekParameters;
Expand Down Expand Up @@ -266,11 +267,12 @@ public ExoPlayerImplInternal(
this.setForegroundModeTimeoutMs = releaseTimeoutMs;
this.pauseAtEndOfWindow = pauseAtEndOfWindow;
this.clock = clock;
this.playerId = playerId;

playbackMaybeBecameStuckAtMs = C.TIME_UNSET;
lastRebufferRealtimeMs = C.TIME_UNSET;
backBufferDurationUs = loadControl.getBackBufferDurationUs();
retainBackBufferFromKeyframe = loadControl.retainBackBufferFromKeyframe();
backBufferDurationUs = loadControl.getBackBufferDurationUs(playerId);
retainBackBufferFromKeyframe = loadControl.retainBackBufferFromKeyframe(playerId);

playbackInfo = PlaybackInfo.createDummy(emptyTrackSelectorResult);
playbackInfoUpdate = new PlaybackInfoUpdate(playbackInfo);
Expand Down Expand Up @@ -772,7 +774,7 @@ private void prepareInternal() {
/* resetPosition= */ false,
/* releaseMediaSourceList= */ false,
/* resetError= */ true);
loadControl.onPrepared();
loadControl.onPrepared(playerId);
setState(playbackInfo.timeline.isEmpty() ? Player.STATE_ENDED : Player.STATE_BUFFERING);
mediaSourceList.prepare(bandwidthMeter.getTransferListener());
handler.sendEmptyMessage(MSG_DO_SOME_WORK);
Expand Down Expand Up @@ -1473,7 +1475,7 @@ private void stopInternal(boolean forceResetRenderers, boolean acknowledgeStop)
/* releaseMediaSourceList= */ true,
/* resetError= */ false);
playbackInfoUpdate.incrementPendingOperationAcks(acknowledgeStop ? 1 : 0);
loadControl.onStopped();
loadControl.onStopped(playerId);
setState(Player.STATE_IDLE);
}

Expand All @@ -1485,7 +1487,7 @@ private void releaseInternal() {
/* releaseMediaSourceList= */ true,
/* resetError= */ false);
releaseRenderers();
loadControl.onReleased();
loadControl.onReleased(playerId);
setState(Player.STATE_IDLE);
} finally {
if (internalPlaybackThread != null) {
Expand Down Expand Up @@ -1938,6 +1940,7 @@ private boolean shouldTransitionToReadyState(boolean renderersReadyOrEnded) {
return isBufferedToEnd
|| isAdPendingPreparation
|| loadControl.shouldStartPlayback(
playerId,
playbackInfo.timeline,
playingPeriodHolder.info.id,
getTotalBufferedDurationUs(),
Expand Down Expand Up @@ -2522,7 +2525,12 @@ private boolean shouldContinueLoading() {
- loadingPeriodHolder.info.startPositionUs;
boolean shouldContinueLoading =
loadControl.shouldContinueLoading(
playbackPositionUs, bufferedDurationUs, mediaClock.getPlaybackParameters().speed);
playerId,
playbackInfo.timeline,
loadingPeriodHolder.info.id,
playbackPositionUs,
bufferedDurationUs,
mediaClock.getPlaybackParameters().speed);
if (!shouldContinueLoading
&& bufferedDurationUs < PLAYBACK_BUFFER_EMPTY_THRESHOLD_US
&& (backBufferDurationUs > 0 || retainBackBufferFromKeyframe)) {
Expand All @@ -2534,7 +2542,12 @@ private boolean shouldContinueLoading() {
.discardBuffer(playbackInfo.positionUs, /* toKeyframe= */ false);
shouldContinueLoading =
loadControl.shouldContinueLoading(
playbackPositionUs, bufferedDurationUs, mediaClock.getPlaybackParameters().speed);
playerId,
playbackInfo.timeline,
loadingPeriodHolder.info.id,
playbackPositionUs,
bufferedDurationUs,
mediaClock.getPlaybackParameters().speed);
}
return shouldContinueLoading;
}
Expand Down Expand Up @@ -2759,6 +2772,7 @@ private void updateLoadControlTrackSelection(
TrackGroupArray trackGroups,
TrackSelectorResult trackSelectorResult) {
loadControl.onTracksSelected(
playerId,
playbackInfo.timeline,
mediaPeriodId,
renderers,
Expand Down
Loading

0 comments on commit a0a4087

Please sign in to comment.