aaudio: indicate client UID and PID to audio flinger

Implement correct indication of client UID and PID to audio flinger
for AAudio MMAP streams in both exclusive mode and shared mode.
- Add start/stop client methods on MMAP streams used only when the MMAP
stream is in AAudio service and carries a mix of shared streams.
- Add "In Service'" indication from "client" side to AAudioServiceStreamMMAP
so that the behavior can be adapted accordingly.
- Modify logic on audio flinger side with regard to mmap tracks and
audio HAL stream activity:
  - use same audio session for all clients on a same stream to match
  audio policy logic to share same direct output stream for clients on same
  session. This is also more consistent with current volume and effect
  handling as all MMAP  clients sharing the same output stream have the
  same volume and use case.
  - start/stop the HAL when the stream is started/stopped with the initial client
  handle (returned when the stream is opened) but do not create a track.
  AAudioService implementation will always send an additional start command before
  first client starts and a stop command after last client stops,
  in both shared and exclusive mode.
  - start/stop a track only if the start/stop stream command is received
  with a handle different from the initial handle.
- Allow more than one active client from the same UID on a MMAP input in audio policy.

Bug: 62950008
Test: verify playback and capture in mmap mode
Merged-In: I86151bbb637ff172d2fd5f813056eab13a7bcd3c
Change-Id: I86151bbb637ff172d2fd5f813056eab13a7bcd3c
diff --git a/services/oboeservice/AAudioServiceEndpoint.cpp b/services/oboeservice/AAudioServiceEndpoint.cpp
index b519829..5895974 100644
--- a/services/oboeservice/AAudioServiceEndpoint.cpp
+++ b/services/oboeservice/AAudioServiceEndpoint.cpp
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#define LOG_TAG "AAudioService"
+#define LOG_TAG "AAudioServiceEndpoint"
 //#define LOG_NDEBUG 0
 #include <utils/Log.h>
 
@@ -94,7 +94,7 @@
 }
 
 aaudio_result_t AAudioServiceEndpoint::close() {
-    return getStreamInternal()->close();
+     return getStreamInternal()->close();
 }
 
 // TODO, maybe use an interface to reduce exposure
@@ -113,26 +113,25 @@
 
 aaudio_result_t AAudioServiceEndpoint::startStream(sp<AAudioServiceStreamShared> sharedStream) {
     // TODO use real-time technique to avoid mutex, eg. atomic command FIFO
+    aaudio_result_t result = AAUDIO_OK;
     std::lock_guard<std::mutex> lock(mLockStreams);
-    mRunningStreams.push_back(sharedStream);
-    if (mRunningStreams.size() == 1) {
+    if (++mRunningStreams == 1) {
+        result = getStreamInternal()->requestStart();
         startSharingThread_l();
     }
-    return AAUDIO_OK;
+    return result;
 }
 
 aaudio_result_t AAudioServiceEndpoint::stopStream(sp<AAudioServiceStreamShared> sharedStream) {
     int numRunningStreams = 0;
     {
         std::lock_guard<std::mutex> lock(mLockStreams);
-        mRunningStreams.erase(
-                std::remove(mRunningStreams.begin(), mRunningStreams.end(), sharedStream),
-                mRunningStreams.end());
-        numRunningStreams = mRunningStreams.size();
+        numRunningStreams = --mRunningStreams;
     }
     if (numRunningStreams == 0) {
         // Don't call this under a lock because the callbackLoop also uses the lock.
         stopSharingThread();
+        getStreamInternal()->requestStop();
     }
     return AAUDIO_OK;
 }
@@ -163,11 +162,8 @@
 
 void AAudioServiceEndpoint::disconnectRegisteredStreams() {
     std::lock_guard<std::mutex> lock(mLockStreams);
-    for(auto baseStream : mRunningStreams) {
-        baseStream->onStop();
-    }
-    mRunningStreams.clear();
     for(auto sharedStream : mRegisteredStreams) {
+        sharedStream->stop();
         sharedStream->disconnect();
     }
     mRegisteredStreams.clear();
@@ -189,3 +185,4 @@
 
     return true;
 }
+