7777import androidx .media3 .common .Tracks ;
7878import androidx .media3 .common .VideoSize ;
7979import androidx .media3 .common .text .CueGroup ;
80+ import androidx .media3 .common .util .BackgroundExecutor ;
8081import androidx .media3 .common .util .BundleCollectionUtil ;
8182import androidx .media3 .common .util .Clock ;
83+ import androidx .media3 .common .util .Consumer ;
8284import androidx .media3 .common .util .ListenerSet ;
8385import androidx .media3 .common .util .Log ;
8486import androidx .media3 .common .util .Size ;
@@ -210,17 +212,19 @@ public MediaControllerImplBase(
210212
211213 @ Override
212214 public void connect (@ UnderInitialization MediaControllerImplBase this ) {
213- boolean connectionRequested ;
215+ Consumer <Boolean > onConnectionRequested =
216+ connectionRequested -> {
217+ if (!connectionRequested ) {
218+ getInstance ().runOnApplicationLooper (getInstance ()::release );
219+ }
220+ };
214221 if (this .token .getType () == SessionToken .TYPE_SESSION ) {
215222 // Session
216223 serviceConnection = null ;
217- connectionRequested = requestConnectToSession (connectionHints );
224+ requestConnectToSession (connectionHints , onConnectionRequested );
218225 } else {
219226 serviceConnection = new SessionServiceConnection (connectionHints );
220- connectionRequested = requestConnectToService ();
221- }
222- if (!connectionRequested ) {
223- getInstance ().runOnApplicationLooper (getInstance ()::release );
227+ requestConnectToService (onConnectionRequested );
224228 }
225229 }
226230
@@ -2618,7 +2622,7 @@ private void notifyPlayerInfoListenersWithReasons(
26182622 listeners .flushEvents ();
26192623 }
26202624
2621- private boolean requestConnectToService () {
2625+ private void requestConnectToService (Consumer < Boolean > onConnectionRequested ) {
26222626 int flags =
26232627 SDK_INT >= 29
26242628 ? Context .BIND_AUTO_CREATE | Context .BIND_INCLUDE_CAPABILITIES
@@ -2642,34 +2646,49 @@ private boolean requestConnectToService() {
26422646 // If a service wants to keep running, it should be either foreground service or
26432647 // bound service. But there had been request for the feature for system apps
26442648 // and using bindService() will be better fit with it.
2645- try {
2646- if (context .bindService (intent , serviceConnection , flags )) {
2647- return true ;
2648- }
2649- Log .w (TAG , "bind to " + token + " failed" );
2650- } catch (SecurityException e ) {
2651- Log .w (TAG , "bind to " + token + " not allowed" , e );
2652- }
2653- return false ;
2654- }
2655-
2656- private boolean requestConnectToSession (Bundle connectionHints ) {
2657- IMediaSession iSession =
2658- IMediaSession .Stub .asInterface ((IBinder ) checkStateNotNull (token .getBinder ()));
2659- int seq = sequencedFutureManager .obtainNextSequenceNumber ();
2660- ConnectionRequest request =
2661- new ConnectionRequest (
2662- context .getPackageName (),
2663- Process .myPid (),
2664- connectionHints ,
2665- instance .getMaxCommandsForMediaItems ());
2666- try {
2667- iSession .connect (controllerStub , seq , request .toBundle ());
2668- } catch (RemoteException e ) {
2669- Log .w (TAG , "Failed to call connection request." , e );
2670- return false ;
2671- }
2672- return true ;
2649+ BackgroundExecutor .get ()
2650+ .execute (
2651+ () -> {
2652+ try {
2653+ if (context .bindService (intent , serviceConnection , flags )) {
2654+ onConnectionRequested .accept (true );
2655+ return ;
2656+ }
2657+ Log .w (TAG , "bind to " + token + " failed" );
2658+ } catch (SecurityException e ) {
2659+ Log .w (TAG , "bind to " + token + " not allowed" , e );
2660+ }
2661+ onConnectionRequested .accept (false );
2662+ });
2663+ }
2664+
2665+ private void requestConnectToSession (
2666+ Bundle connectionHints , Consumer <Boolean > onConnectionRequested ) {
2667+ // If the session is not remote, it will answer swiftly. In order to support creating a media
2668+ // controller in the media session service onCreate, connect in-place if not remote.
2669+ (token .getBinder () instanceof MediaSessionStub
2670+ ? MoreExecutors .directExecutor ()
2671+ : BackgroundExecutor .get ())
2672+ .execute (
2673+ () -> {
2674+ IMediaSession iSession =
2675+ IMediaSession .Stub .asInterface ((IBinder ) checkStateNotNull (token .getBinder ()));
2676+ int seq = sequencedFutureManager .obtainNextSequenceNumber ();
2677+ ConnectionRequest request =
2678+ new ConnectionRequest (
2679+ context .getPackageName (),
2680+ Process .myPid (),
2681+ connectionHints ,
2682+ instance .getMaxCommandsForMediaItems ());
2683+ try {
2684+ iSession .connect (controllerStub , seq , request .toBundle ());
2685+ } catch (RemoteException e ) {
2686+ Log .w (TAG , "Failed to call connection request." , e );
2687+ onConnectionRequested .accept (false );
2688+ return ;
2689+ }
2690+ onConnectionRequested .accept (true );
2691+ });
26732692 }
26742693
26752694 private void clearSurfacesAndCallbacks () {
0 commit comments