Skip to content

Commit d1fc15f

Browse files
toniheicopybara-github
authored andcommitted
Avoid bundling PlayerInfo for in-process calls
PlayerInfo bundling is costly and we can add a shortcut for in-process binder calls where we store the direct object reference in a live Binder object that can be written to the Bundle instead of the individual data fields. #minor-release PiperOrigin-RevId: 572816784
1 parent 4ebe630 commit d1fc15f

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

libraries/session/src/main/java/androidx/media3/session/MediaSessionStub.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1915,9 +1915,13 @@ public void onPlayerInfoChanged(
19151915
if (controllerInterfaceVersion >= 2) {
19161916
PlayerInfo filteredPlayerInfo =
19171917
playerInfo.filterByAvailableCommands(availableCommands, excludeTimeline, excludeTracks);
1918+
Bundle playerInfoBundle =
1919+
iController instanceof MediaControllerStub
1920+
? filteredPlayerInfo.toBundleInProcess()
1921+
: filteredPlayerInfo.toBundle();
19181922
iController.onPlayerInfoChangedWithExclusions(
19191923
sequenceNumber,
1920-
filteredPlayerInfo.toBundle(),
1924+
playerInfoBundle,
19211925
new PlayerInfo.BundlingExclusions(bundlingExclusionsTimeline, bundlingExclusionsTracks)
19221926
.toBundle());
19231927
} else {

libraries/session/src/main/java/androidx/media3/session/PlayerInfo.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
import static androidx.media3.common.Player.STATE_IDLE;
2323
import static androidx.media3.common.Player.TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED;
2424

25+
import android.os.Binder;
2526
import android.os.Bundle;
27+
import android.os.IBinder;
2628
import androidx.annotation.CheckResult;
2729
import androidx.annotation.FloatRange;
2830
import androidx.annotation.Nullable;
@@ -44,6 +46,7 @@
4446
import androidx.media3.common.VideoSize;
4547
import androidx.media3.common.text.CueGroup;
4648
import androidx.media3.common.util.Assertions;
49+
import androidx.media3.common.util.BundleUtil;
4750
import androidx.media3.common.util.UnstableApi;
4851
import androidx.media3.common.util.Util;
4952
import com.google.common.base.Objects;
@@ -824,8 +827,9 @@ private boolean isPlaying(
824827
private static final String FIELD_TRACK_SELECTION_PARAMETERS = Util.intToStringMaxRadix(29);
825828
private static final String FIELD_CURRENT_TRACKS = Util.intToStringMaxRadix(30);
826829
private static final String FIELD_TIMELINE_CHANGE_REASON = Util.intToStringMaxRadix(31);
830+
private static final String FIELD_IN_PROCESS_BINDER = Util.intToStringMaxRadix(32);
827831

828-
// Next field key = 32
832+
// Next field key = 33
829833

830834
/**
831835
* Returns a copy of this player info, filtered by the specified available commands.
@@ -882,6 +886,16 @@ public PlayerInfo filterByAvailableCommands(
882886
return builder.build();
883887
}
884888

889+
/**
890+
* Returns a {@link Bundle} that stores a direct object reference to this class for in-process
891+
* sharing.
892+
*/
893+
public Bundle toBundleInProcess() {
894+
Bundle bundle = new Bundle();
895+
BundleUtil.putBinder(bundle, FIELD_IN_PROCESS_BINDER, new InProcessBinder());
896+
return bundle;
897+
}
898+
885899
@Override
886900
public Bundle toBundle() {
887901
Bundle bundle = new Bundle();
@@ -985,6 +999,10 @@ public Bundle toBundle() {
985999
public static final Creator<PlayerInfo> CREATOR = PlayerInfo::fromBundle;
9861000

9871001
private static PlayerInfo fromBundle(Bundle bundle) {
1002+
@Nullable IBinder inProcessBinder = BundleUtil.getBinder(bundle, FIELD_IN_PROCESS_BINDER);
1003+
if (inProcessBinder instanceof InProcessBinder) {
1004+
return ((InProcessBinder) inProcessBinder).getPlayerInfo();
1005+
}
9881006
@Nullable Bundle playerErrorBundle = bundle.getBundle(FIELD_PLAYBACK_ERROR);
9891007
@Nullable
9901008
PlaybackException playerError =
@@ -1115,4 +1133,10 @@ private static PlayerInfo fromBundle(Bundle bundle) {
11151133
currentTracks,
11161134
trackSelectionParameters);
11171135
}
1136+
1137+
private final class InProcessBinder extends Binder {
1138+
public PlayerInfo getPlayerInfo() {
1139+
return PlayerInfo.this;
1140+
}
1141+
}
11181142
}

0 commit comments

Comments
 (0)