Adjust AGGREGATES_CALLBACKS_WITHIN_TIMEOUT_MS and make it configurable

PiperOrigin-RevId: 745585764
This commit is contained in:
bachinger 2025-04-09 07:45:44 -07:00 committed by Copybara-Service
parent f9617e1f8d
commit 1b3658e357
5 changed files with 98 additions and 16 deletions

View File

@ -63,6 +63,9 @@
instead of `MediaCodec.BufferInfo`. instead of `MediaCodec.BufferInfo`.
* IMA extension: * IMA extension:
* Session: * Session:
* Lower aggregation timeout for platform `MediaSession` callbacks from 500
to 100 milliseconds and add an experimental setter to allow apps to
configure this value.
* UI: * UI:
* Enable `PlayerSurface` to work with `ExoPlayer.setVideoEffects` and * Enable `PlayerSurface` to work with `ExoPlayer.setVideoEffects` and
`CompositionPlayer`. `CompositionPlayer`.

View File

@ -61,6 +61,7 @@ public final class MediaBrowser extends MediaController {
private Looper applicationLooper; private Looper applicationLooper;
private @MonotonicNonNull BitmapLoader bitmapLoader; private @MonotonicNonNull BitmapLoader bitmapLoader;
private int maxCommandsForMediaItems; private int maxCommandsForMediaItems;
private long platformSessionCallbackAggregationTimeoutMs;
/** /**
* Creates a builder for {@link MediaBrowser}. * Creates a builder for {@link MediaBrowser}.
@ -78,6 +79,8 @@ public final class MediaBrowser extends MediaController {
connectionHints = Bundle.EMPTY; connectionHints = Bundle.EMPTY;
listener = new Listener() {}; listener = new Listener() {};
applicationLooper = Util.getCurrentOrMainLooper(); applicationLooper = Util.getCurrentOrMainLooper();
platformSessionCallbackAggregationTimeoutMs =
DEFAULT_PLATFORM_CALLBACK_AGGREGATION_TIMEOUT_MS;
} }
/** /**
@ -156,6 +159,24 @@ public final class MediaBrowser extends MediaController {
return this; return this;
} }
/**
* Sets the timeout after which updates from the platform session callbacks are applied to the
* browser, in milliseconds.
*
* <p>The default is 100ms.
*
* @param platformSessionCallbackAggregationTimeoutMs The timeout, in milliseconds.
* @return The builder to allow chaining.
*/
@UnstableApi
@CanIgnoreReturnValue
public Builder experimentalSetPlatformSessionCallbackAggregationTimeoutMs(
long platformSessionCallbackAggregationTimeoutMs) {
this.platformSessionCallbackAggregationTimeoutMs =
platformSessionCallbackAggregationTimeoutMs;
return this;
}
/** /**
* Builds a {@link MediaBrowser} asynchronously. * Builds a {@link MediaBrowser} asynchronously.
* *
@ -196,7 +217,8 @@ public final class MediaBrowser extends MediaController {
applicationLooper, applicationLooper,
holder, holder,
bitmapLoader, bitmapLoader,
maxCommandsForMediaItems); maxCommandsForMediaItems,
platformSessionCallbackAggregationTimeoutMs);
postOrRun(new Handler(applicationLooper), () -> holder.setController(browser)); postOrRun(new Handler(applicationLooper), () -> holder.setController(browser));
return holder; return holder;
} }
@ -266,7 +288,8 @@ public final class MediaBrowser extends MediaController {
Looper applicationLooper, Looper applicationLooper,
ConnectionCallback connectionCallback, ConnectionCallback connectionCallback,
@Nullable BitmapLoader bitmapLoader, @Nullable BitmapLoader bitmapLoader,
int maxCommandsForMediaItems) { int maxCommandsForMediaItems,
long platformSessionCallbackAggregationTimeoutMs) {
super( super(
context, context,
token, token,
@ -275,7 +298,8 @@ public final class MediaBrowser extends MediaController {
applicationLooper, applicationLooper,
connectionCallback, connectionCallback,
bitmapLoader, bitmapLoader,
maxCommandsForMediaItems); maxCommandsForMediaItems,
platformSessionCallbackAggregationTimeoutMs);
} }
@Override @Override
@ -286,12 +310,19 @@ public final class MediaBrowser extends MediaController {
SessionToken token, SessionToken token,
Bundle connectionHints, Bundle connectionHints,
Looper applicationLooper, Looper applicationLooper,
@Nullable BitmapLoader bitmapLoader) { @Nullable BitmapLoader bitmapLoader,
long platformSessionCallbackAggregationTimeoutMs) {
MediaBrowserImpl impl; MediaBrowserImpl impl;
if (token.isLegacySession()) { if (token.isLegacySession()) {
impl = impl =
new MediaBrowserImplLegacy( new MediaBrowserImplLegacy(
context, this, token, connectionHints, applicationLooper, checkNotNull(bitmapLoader)); context,
this,
token,
connectionHints,
applicationLooper,
checkNotNull(bitmapLoader),
platformSessionCallbackAggregationTimeoutMs);
} else { } else {
impl = new MediaBrowserImplBase(context, this, token, connectionHints, applicationLooper); impl = new MediaBrowserImplBase(context, this, token, connectionHints, applicationLooper);
} }

View File

@ -64,8 +64,16 @@ import org.checkerframework.checker.initialization.qual.UnderInitialization;
SessionToken token, SessionToken token,
Bundle connectionHints, Bundle connectionHints,
Looper applicationLooper, Looper applicationLooper,
BitmapLoader bitmapLoader) { BitmapLoader bitmapLoader,
super(context, instance, token, connectionHints, applicationLooper, bitmapLoader); long platformSessionCallbackAggregationTimeoutMs) {
super(
context,
instance,
token,
connectionHints,
applicationLooper,
bitmapLoader,
platformSessionCallbackAggregationTimeoutMs);
this.instance = instance; this.instance = instance;
commandButtonsForMediaItems = ImmutableMap.of(); commandButtonsForMediaItems = ImmutableMap.of();
} }

View File

@ -201,6 +201,8 @@ public class MediaController implements Player {
"MediaController method is called from a wrong thread." "MediaController method is called from a wrong thread."
+ " See javadoc of MediaController for details."; + " See javadoc of MediaController for details.";
@UnstableApi protected static final long DEFAULT_PLATFORM_CALLBACK_AGGREGATION_TIMEOUT_MS = 100L;
/** A builder for {@link MediaController}. */ /** A builder for {@link MediaController}. */
public static final class Builder { public static final class Builder {
@ -211,6 +213,7 @@ public class MediaController implements Player {
private Looper applicationLooper; private Looper applicationLooper;
private @MonotonicNonNull BitmapLoader bitmapLoader; private @MonotonicNonNull BitmapLoader bitmapLoader;
private int maxCommandsForMediaItems; private int maxCommandsForMediaItems;
private long platformSessionCallbackAggregationTimeoutMs;
/** /**
* Creates a builder for {@link MediaController}. * Creates a builder for {@link MediaController}.
@ -242,6 +245,8 @@ public class MediaController implements Player {
connectionHints = Bundle.EMPTY; connectionHints = Bundle.EMPTY;
listener = new Listener() {}; listener = new Listener() {};
applicationLooper = Util.getCurrentOrMainLooper(); applicationLooper = Util.getCurrentOrMainLooper();
platformSessionCallbackAggregationTimeoutMs =
DEFAULT_PLATFORM_CALLBACK_AGGREGATION_TIMEOUT_MS;
} }
/** /**
@ -320,6 +325,24 @@ public class MediaController implements Player {
return this; return this;
} }
/**
* Sets the timeout after which updates from the platform session callbacks are applied to the
* browser, in milliseconds.
*
* <p>The default is 100ms.
*
* @param platformSessionCallbackAggregationTimeoutMs The timeout, in milliseconds.
* @return tThe builder to allow chaining.
*/
@UnstableApi
@CanIgnoreReturnValue
public Builder experimentalSetPlatformSessionCallbackAggregationTimeoutMs(
long platformSessionCallbackAggregationTimeoutMs) {
this.platformSessionCallbackAggregationTimeoutMs =
platformSessionCallbackAggregationTimeoutMs;
return this;
}
/** /**
* Builds a {@link MediaController} asynchronously. * Builds a {@link MediaController} asynchronously.
* *
@ -361,7 +384,8 @@ public class MediaController implements Player {
applicationLooper, applicationLooper,
holder, holder,
bitmapLoader, bitmapLoader,
maxCommandsForMediaItems); maxCommandsForMediaItems,
platformSessionCallbackAggregationTimeoutMs);
postOrRun(new Handler(applicationLooper), () -> holder.setController(controller)); postOrRun(new Handler(applicationLooper), () -> holder.setController(controller));
return holder; return holder;
} }
@ -553,7 +577,8 @@ public class MediaController implements Player {
Looper applicationLooper, Looper applicationLooper,
ConnectionCallback connectionCallback, ConnectionCallback connectionCallback,
@Nullable BitmapLoader bitmapLoader, @Nullable BitmapLoader bitmapLoader,
int maxCommandsForMediaItems) { int maxCommandsForMediaItems,
long platformSessionCallbackAggregationTimeoutMs) {
checkNotNull(context, "context must not be null"); checkNotNull(context, "context must not be null");
checkNotNull(token, "token must not be null"); checkNotNull(token, "token must not be null");
Log.i( Log.i(
@ -576,7 +601,14 @@ public class MediaController implements Player {
this.connectionCallback = connectionCallback; this.connectionCallback = connectionCallback;
this.maxCommandsForMediaItems = maxCommandsForMediaItems; this.maxCommandsForMediaItems = maxCommandsForMediaItems;
impl = createImpl(context, token, connectionHints, applicationLooper, bitmapLoader); impl =
createImpl(
context,
token,
connectionHints,
applicationLooper,
bitmapLoader,
platformSessionCallbackAggregationTimeoutMs);
impl.connect(); impl.connect();
} }
@ -587,10 +619,17 @@ public class MediaController implements Player {
SessionToken token, SessionToken token,
Bundle connectionHints, Bundle connectionHints,
Looper applicationLooper, Looper applicationLooper,
@Nullable BitmapLoader bitmapLoader) { @Nullable BitmapLoader bitmapLoader,
long platformSessionCallbackAggregationTimeoutMs) {
if (token.isLegacySession()) { if (token.isLegacySession()) {
return new MediaControllerImplLegacy( return new MediaControllerImplLegacy(
context, this, token, connectionHints, applicationLooper, checkNotNull(bitmapLoader)); context,
this,
token,
connectionHints,
applicationLooper,
checkNotNull(bitmapLoader),
platformSessionCallbackAggregationTimeoutMs);
} else { } else {
return new MediaControllerImplBase(context, this, token, connectionHints, applicationLooper); return new MediaControllerImplBase(context, this, token, connectionHints, applicationLooper);
} }

View File

@ -93,8 +93,6 @@ import org.checkerframework.checker.initialization.qual.UnderInitialization;
private static final String TAG = "MCImplLegacy"; private static final String TAG = "MCImplLegacy";
private static final long AGGREGATES_CALLBACKS_WITHIN_TIMEOUT_MS = 500L;
/* package */ final Context context; /* package */ final Context context;
private final MediaController instance; private final MediaController instance;
@ -104,6 +102,7 @@ import org.checkerframework.checker.initialization.qual.UnderInitialization;
private final BitmapLoader bitmapLoader; private final BitmapLoader bitmapLoader;
private final ImmutableList<CommandButton> commandButtonsForMediaItems; private final ImmutableList<CommandButton> commandButtonsForMediaItems;
private final Bundle connectionHints; private final Bundle connectionHints;
private final long platformSessionCallbackAggregationTimeoutMs;
@Nullable private MediaControllerCompat controllerCompat; @Nullable private MediaControllerCompat controllerCompat;
@Nullable private MediaBrowserCompat browserCompat; @Nullable private MediaBrowserCompat browserCompat;
@ -122,7 +121,8 @@ import org.checkerframework.checker.initialization.qual.UnderInitialization;
SessionToken token, SessionToken token,
Bundle connectionHints, Bundle connectionHints,
Looper applicationLooper, Looper applicationLooper,
BitmapLoader bitmapLoader) { BitmapLoader bitmapLoader,
long platformSessionCallbackAggregationTimeoutMs) {
// Initialize default values. // Initialize default values.
legacyPlayerInfo = new LegacyPlayerInfo(); legacyPlayerInfo = new LegacyPlayerInfo();
pendingLegacyPlayerInfo = new LegacyPlayerInfo(); pendingLegacyPlayerInfo = new LegacyPlayerInfo();
@ -140,6 +140,7 @@ import org.checkerframework.checker.initialization.qual.UnderInitialization;
this.token = token; this.token = token;
this.connectionHints = connectionHints; this.connectionHints = connectionHints;
this.bitmapLoader = bitmapLoader; this.bitmapLoader = bitmapLoader;
this.platformSessionCallbackAggregationTimeoutMs = platformSessionCallbackAggregationTimeoutMs;
currentPositionMs = C.TIME_UNSET; currentPositionMs = C.TIME_UNSET;
lastSetPlayWhenReadyCalledTimeMs = C.TIME_UNSET; lastSetPlayWhenReadyCalledTimeMs = C.TIME_UNSET;
// Always empty. Only supported for a MediaBrowser connected to a MediaBrowserServiceCompat. // Always empty. Only supported for a MediaBrowser connected to a MediaBrowserServiceCompat.
@ -1992,7 +1993,7 @@ import org.checkerframework.checker.initialization.qual.UnderInitialization;
return; return;
} }
pendingChangesHandler.sendEmptyMessageDelayed( pendingChangesHandler.sendEmptyMessageDelayed(
MSG_HANDLE_PENDING_UPDATES, AGGREGATES_CALLBACKS_WITHIN_TIMEOUT_MS); MSG_HANDLE_PENDING_UPDATES, platformSessionCallbackAggregationTimeoutMs);
} }
} }