Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 50363967 authored by Santiago Seifert's avatar Santiago Seifert
Browse files

Implement in-session transfers for system media sessions

Bug: b/362507305
Test: atest CtsMediaBetterTogetherTestCases:android.media.router.cts.SystemMediaRoutingTest
Flag: com.android.media.flags.enable_mirroring_in_media_router_2
Change-Id: I90aeeea6263b4c9ec4a8eaa44de8dd504e2c89c7
parent f120014b
Loading
Loading
Loading
Loading
+15 −3
Original line number Diff line number Diff line
@@ -503,7 +503,10 @@ public abstract class MediaRoute2ProviderService extends Service {

        String sessionId = sessionInfo.getId();
        synchronized (mSessionLock) {
            if (mSessionInfos.containsKey(sessionId)) {
            var mediaStreams = mOngoingMediaStreams.get(sessionId);
            if (Flags.enableMirroringInMediaRouter2() && mediaStreams != null) {
                mediaStreams.mSessionInfo = sessionInfo;
            } else if (mSessionInfos.containsKey(sessionId)) {
                mSessionInfos.put(sessionId, sessionInfo);
            } else {
                Log.w(TAG, "notifySessionUpdated: Ignoring unknown session info.");
@@ -836,6 +839,9 @@ public abstract class MediaRoute2ProviderService extends Service {
        List<RoutingSessionInfo> sessions;
        synchronized (mSessionLock) {
            sessions = new ArrayList<>(mSessionInfos.values());
            if (Flags.enableMirroringInMediaRouter2()) {
                mOngoingMediaStreams.values().forEach(it -> sessions.add(it.mSessionInfo));
            }
        }

        try {
@@ -888,7 +894,13 @@ public abstract class MediaRoute2ProviderService extends Service {
                Log.w(TAG, description + ": Ignoring empty sessionId from system service.");
                return false;
            }
            if (getSessionInfo(sessionId) == null) {
            boolean idMatchesSystemSession = false;
            if (Flags.enableMirroringInMediaRouter2()) {
                synchronized (mSessionLock) {
                    idMatchesSystemSession = mOngoingMediaStreams.containsKey(sessionId);
                }
            }
            if (!idMatchesSystemSession && getSessionInfo(sessionId) == null) {
                Log.w(TAG, description + ": Ignoring unknown session from system service. "
                        + "sessionId=" + sessionId);
                return false;
@@ -1079,8 +1091,8 @@ public abstract class MediaRoute2ProviderService extends Service {
         *
         * @hide
         */
        @GuardedBy("MediaRoute2ProviderService.this.mSessionLock")
        @NonNull
        // Access guarded by mSessionsLock, but it's not convenient to enforce through @GuardedBy.
        private RoutingSessionInfo mSessionInfo;

        // TODO: b/380431086: Add the video equivalent.
+10 −0
Original line number Diff line number Diff line
@@ -545,6 +545,16 @@ final class MediaRoute2ProviderServiceProxy extends MediaRoute2Provider {
            for (RoutingSessionInfo session : sessions) {
                if (session == null) continue;
                session = assignProviderIdForSession(session);

                if (Flags.enableMirroringInMediaRouter2()) {
                    var systemSessionCallback =
                            mSystemSessionCallbacks.get(session.getOriginalId());
                    if (systemSessionCallback != null) {
                        systemSessionCallback.onSessionUpdate(session);
                        continue;
                    }
                }

                int sourceIndex = findSessionByIdLocked(session);
                if (sourceIndex < 0) {
                    mSessionInfos.add(targetIndex++, session);
+28 −21
Original line number Diff line number Diff line
@@ -128,8 +128,20 @@ import java.util.stream.Stream;
                                targetProviderProxyId, existingSession.getProviderId())) {
                    // The currently selected route and target route both belong to the same
                    // provider. We tell the provider to handle the transfer.
                    targetProviderProxyRecord.requestTransfer(
                            existingSession.getOriginalId(), serviceTargetRoute);
                    if (serviceTargetRoute == null) {
                        notifyRequestFailed(
                                requestId, MediaRoute2ProviderService.REASON_ROUTE_NOT_AVAILABLE);
                    } else {
                        targetProviderProxyRecord.mProxy.transferToRoute(
                                requestId,
                                clientUserHandle,
                                clientPackageName,
                                existingSession.getOriginalId(),
                                targetProviderProxyRecord.mNewOriginalIdToSourceOriginalIdMap.get(
                                        routeOriginalId),
                                transferReason);
                    }
                    return;
                } else {
                    // The target route is handled by a provider other than the target one. We need
                    // to release the existing session.
@@ -429,11 +441,6 @@ import java.util.stream.Stream;
            }
        }

        public void requestTransfer(String sessionId, MediaRoute2Info targetRoute) {
            // TODO: Map the target route to the source route original id.
            throw new UnsupportedOperationException("TODO Implement");
        }

        public void releaseSession(long requestId, String originalSessionId) {
            mProxy.releaseSession(requestId, originalSessionId);
        }
@@ -491,7 +498,7 @@ import java.util.stream.Stream;
                    () -> {
                        if (mSessionRecord != null) {
                            mSessionRecord.onSessionUpdate(sessionInfo);
                        }
                        } else {
                            SystemMediaSessionRecord systemMediaSessionRecord =
                                    new SystemMediaSessionRecord(mProviderId, sessionInfo);
                            RoutingSessionInfo translatedSession;
@@ -503,6 +510,7 @@ import java.util.stream.Stream;
                                translatedSession = systemMediaSessionRecord.mTranslatedSessionInfo;
                            }
                            onSessionOverrideUpdated(translatedSession);
                        }
                    });
        }

@@ -546,7 +554,6 @@ import java.util.stream.Stream;
         * The same as {@link #mSourceSessionInfo}, except ids are {@link #asSystemRouteId system
         * provider ids}.
         */
        @GuardedBy("SystemMediaRoute2Provider2.this.mLock")
        @NonNull
        private RoutingSessionInfo mTranslatedSessionInfo;

@@ -559,10 +566,10 @@ import java.util.stream.Stream;

        @Override
        public void onSessionUpdate(@NonNull RoutingSessionInfo sessionInfo) {
            RoutingSessionInfo translatedSessionInfo = mTranslatedSessionInfo;
            RoutingSessionInfo translatedSessionInfo = asSystemProviderSession(sessionInfo);
            synchronized (mLock) {
                mSourceSessionInfo = sessionInfo;
                mTranslatedSessionInfo = asSystemProviderSession(sessionInfo);
                mTranslatedSessionInfo = translatedSessionInfo;
            }
            onSessionOverrideUpdated(translatedSessionInfo);
        }