audio policy: optimize preferred device selection

Several optimizations to avoid redundant track invalidation
when a preferred device is specified for an AudioTrack.
1) in AudioTrack: only invalidate the track if the preferred device
   differs from the actual device. If the track is not active,
   immediately restore it instead of waiting for next start.
2) in AudioPolicyManager: avoid invalidating the track in startSource() and
   stopSource() if another active client with the same preferred device
   exists.

Also fix a problem in getOutput() preventing MediaPlayer to open a sink
for call assistant use case.

Bug: 272560885
Test: atest RoutingTest
Change-Id: Ia3fb06eaf2e292d2795d588226c7672b7ab6722f
diff --git a/services/audiopolicy/managerdefault/AudioPolicyManager.cpp b/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
index 8d00a3d..f625fdb 100644
--- a/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
+++ b/services/audiopolicy/managerdefault/AudioPolicyManager.cpp
@@ -2088,8 +2088,7 @@
     outputDesc->setClientActive(client, true);
 
     if (client->hasPreferredDevice(true)) {
-        if (outputDesc->clientsList(true /*activeOnly*/).size() == 1 &&
-                client->isPreferredDeviceForExclusiveUse()) {
+        if (outputDesc->sameExclusivePreferredDevicesCount() > 0) {
             // Preferred device may be exclusive, use only if no other active clients on this output
             devices = DeviceVector(
                         mAvailableOutputDevices.getDeviceFromId(client->preferredDeviceId()));
@@ -2293,7 +2292,8 @@
             }
         }
         bool forceDeviceUpdate = false;
-        if (client->hasPreferredDevice(true)) {
+        if (client->hasPreferredDevice(true) &&
+                outputDesc->sameExclusivePreferredDevicesCount() < 2) {
             checkStrategyRoute(client->strategy(), AUDIO_IO_HANDLE_NONE);
             forceDeviceUpdate = true;
         }