SoundPool: Assign the java piid to the AudioTrack

This allows to send native playback notifications for SoundPool players.

Test: dumpsys audio
Bug: 235521198
Change-Id: I7e743df4a16e0d8dae9f15f21fb4e8fa01aa4465
diff --git a/media/jni/soundpool/SoundPool.cpp b/media/jni/soundpool/SoundPool.cpp
index a2826cb..9f32a83 100644
--- a/media/jni/soundpool/SoundPool.cpp
+++ b/media/jni/soundpool/SoundPool.cpp
@@ -115,7 +115,7 @@
 }
 
 int32_t SoundPool::play(int32_t soundID, float leftVolume, float rightVolume,
-        int32_t priority, int32_t loop, float rate)
+        int32_t priority, int32_t loop, float rate, int32_t playerIId)
 {
     ALOGV("%s(soundID=%d, leftVolume=%f, rightVolume=%f, priority=%d, loop=%d, rate=%f)",
             __func__, soundID, leftVolume, rightVolume, priority, loop, rate);
@@ -136,8 +136,9 @@
     }
 
     const int32_t streamID = mStreamManager.queueForPlay(
-            sound, soundID, leftVolume, rightVolume, priority, loop, rate);
+            sound, soundID, leftVolume, rightVolume, priority, loop, rate, playerIId);
     ALOGV("%s returned %d", __func__, streamID);
+
     return streamID;
 }
 
diff --git a/media/jni/soundpool/SoundPool.h b/media/jni/soundpool/SoundPool.h
index 6bb971b..efc358d 100644
--- a/media/jni/soundpool/SoundPool.h
+++ b/media/jni/soundpool/SoundPool.h
@@ -39,7 +39,7 @@
     int32_t load(int fd, int64_t offset, int64_t length, int32_t priority);
     bool unload(int32_t soundID);
     int32_t play(int32_t soundID, float leftVolume, float rightVolume, int32_t priority,
-            int32_t loop, float rate);
+            int32_t loop, float rate, int32_t playerIId = PLAYER_PIID_INVALID);
     void pause(int32_t streamID);
     void autoPause();
     void resume(int32_t streamID);
diff --git a/media/jni/soundpool/Stream.cpp b/media/jni/soundpool/Stream.cpp
index 9ed8770..4194a22 100644
--- a/media/jni/soundpool/Stream.cpp
+++ b/media/jni/soundpool/Stream.cpp
@@ -229,7 +229,7 @@
    return mStreamManager->getPairStream(this);
 }
 
-Stream* Stream::playPairStream(std::vector<std::any>& garbage) {
+Stream* Stream::playPairStream(std::vector<std::any>& garbage, int32_t playerIId) {
     Stream* pairStream = getPairStream();
     LOG_ALWAYS_FATAL_IF(pairStream == nullptr, "No pair stream!");
     {
@@ -260,7 +260,7 @@
         const int pairState = pairStream->mState;
         pairStream->play_l(pairStream->mSound, pairStream->mStreamID,
                 pairStream->mLeftVolume, pairStream->mRightVolume, pairStream->mPriority,
-                pairStream->mLoop, pairStream->mRate, garbage);
+                pairStream->mLoop, pairStream->mRate, garbage, playerIId);
         if (pairStream->mState == IDLE) {
             return nullptr; // AudioTrack error
         }
@@ -274,12 +274,12 @@
 
 void Stream::play_l(const std::shared_ptr<Sound>& sound, int32_t nextStreamID,
         float leftVolume, float rightVolume, int32_t priority, int32_t loop, float rate,
-        std::vector<std::any>& garbage)
+        std::vector<std::any>& garbage, int32_t playerIId)
 {
     ALOGV("%s(%p)(soundID=%d, streamID=%d, leftVolume=%f, rightVolume=%f,"
-            " priority=%d, loop=%d, rate=%f)",
+            " priority=%d, loop=%d, rate=%f, playerIId=%d)",
             __func__, this, sound->getSoundID(), nextStreamID, leftVolume, rightVolume,
-            priority, loop, rate);
+            priority, loop, rate, playerIId);
 
     // initialize track
     const audio_stream_type_t streamType =
@@ -340,6 +340,10 @@
         // MediaMetricsConstants.h: AMEDIAMETRICS_PROP_CALLERNAME_VALUE_SOUNDPOOL
         mAudioTrack->setCallerName("soundpool");
 
+        if (playerIId != PLAYER_PIID_INVALID) {
+            mAudioTrack->setPlayerIId(playerIId);
+        }
+
         if (status_t status = mAudioTrack->initCheck();
             status != NO_ERROR) {
             ALOGE("%s: error %d creating AudioTrack", __func__, status);
@@ -379,6 +383,7 @@
     std::lock_guard lock(mLock);
     return static_cast<int>(mAudioTrack ? mStreamID : getPairStream()->mStreamID);
 }
+
 size_t Stream::StreamCallback::onMoreData(const AudioTrack::Buffer&) {
     ALOGW("%s streamID %d Unexpected EVENT_MORE_DATA for static track",
             __func__, mStream->getCorrespondingStreamID());
diff --git a/media/jni/soundpool/Stream.h b/media/jni/soundpool/Stream.h
index 0054eec..6c9ef2e 100644
--- a/media/jni/soundpool/Stream.h
+++ b/media/jni/soundpool/Stream.h
@@ -93,7 +93,8 @@
 
     // returns the pair stream if successful, nullptr otherwise.
     // garbage is used to release tracks and data outside of any lock.
-    Stream* playPairStream(std::vector<std::any>& garbage);
+    Stream* playPairStream(std::vector<std::any>& garbage,
+                           int32_t playerIId = PLAYER_PIID_INVALID);
 
     // These parameters are explicitly checked in the SoundPool class
     // so never deviate from the Java API specified values.
@@ -157,7 +158,7 @@
     // garbage is used to release tracks and data outside of any lock.
     void play_l(const std::shared_ptr<Sound>& sound, int streamID,
             float leftVolume, float rightVolume, int priority, int loop, float rate,
-            std::vector<std::any>& garbage) REQUIRES(mLock);
+            std::vector<std::any>& garbage, int playerIId) REQUIRES(mLock);
     void stop_l() REQUIRES(mLock);
     void setVolume_l(float leftVolume, float rightVolume) REQUIRES(mLock);
 
diff --git a/media/jni/soundpool/StreamManager.cpp b/media/jni/soundpool/StreamManager.cpp
index 487a696..acd4bad 100644
--- a/media/jni/soundpool/StreamManager.cpp
+++ b/media/jni/soundpool/StreamManager.cpp
@@ -151,10 +151,13 @@
 
 int32_t StreamManager::queueForPlay(const std::shared_ptr<Sound> &sound,
         int32_t soundID, float leftVolume, float rightVolume,
-        int32_t priority, int32_t loop, float rate)
+        int32_t priority, int32_t loop, float rate, int32_t playerIId)
 {
-    ALOGV("%s(sound=%p, soundID=%d, leftVolume=%f, rightVolume=%f, priority=%d, loop=%d, rate=%f)",
-            __func__, sound.get(), soundID, leftVolume, rightVolume, priority, loop, rate);
+    ALOGV(
+        "%s(sound=%p, soundID=%d, leftVolume=%f, rightVolume=%f, priority=%d, loop=%d, rate=%f,"
+        " playerIId=%d)", __func__, sound.get(), soundID, leftVolume, rightVolume, priority,
+        loop, rate, playerIId);
+
     bool launchThread = false;
     int32_t streamID = 0;
     std::vector<std::any> garbage;
@@ -244,7 +247,7 @@
             removeFromQueues_l(newStream);
             mProcessingStreams.emplace(newStream);
             lock.unlock();
-            if (Stream* nextStream = newStream->playPairStream(garbage)) {
+            if (Stream* nextStream = newStream->playPairStream(garbage, playerIId)) {
                 lock.lock();
                 ALOGV("%s: starting streamID:%d", __func__, nextStream->getStreamID());
                 addToActiveQueue_l(nextStream);
diff --git a/media/jni/soundpool/StreamManager.h b/media/jni/soundpool/StreamManager.h
index ec65b0c..adbab4b 100644
--- a/media/jni/soundpool/StreamManager.h
+++ b/media/jni/soundpool/StreamManager.h
@@ -394,7 +394,7 @@
     // Returns positive streamID on success, 0 on failure.  This is locked.
     int32_t queueForPlay(const std::shared_ptr<Sound> &sound,
             int32_t soundID, float leftVolume, float rightVolume,
-            int32_t priority, int32_t loop, float rate)
+            int32_t priority, int32_t loop, float rate, int32_t playerIId)
             NO_THREAD_SAFETY_ANALYSIS; // uses unique_lock
 
     ///////////////////////////////////////////////////////////////////////
diff --git a/media/jni/soundpool/android_media_SoundPool.cpp b/media/jni/soundpool/android_media_SoundPool.cpp
index 5264772..25040a9 100644
--- a/media/jni/soundpool/android_media_SoundPool.cpp
+++ b/media/jni/soundpool/android_media_SoundPool.cpp
@@ -364,12 +364,19 @@
 static jint
 android_media_SoundPool_play(JNIEnv *env, jobject thiz, jint sampleID,
         jfloat leftVolume, jfloat rightVolume, jint priority, jint loop,
-        jfloat rate)
+        jfloat rate, jint playerIId)
 {
     ALOGV("android_media_SoundPool_play\n");
     auto soundPool = getSoundPool(env, thiz);
     if (soundPool == nullptr) return 0;
-    return (jint) soundPool->play(sampleID, leftVolume, rightVolume, priority, loop, rate);
+
+    return (jint) soundPool->play(sampleID,
+                                  leftVolume,
+                                  rightVolume,
+                                  priority,
+                                  loop,
+                                  rate,
+                                  playerIId);
 }
 
 static void
@@ -563,7 +570,7 @@
         (void *)android_media_SoundPool_unload
     },
     {   "_play",
-        "(IFFIIF)I",
+        "(IFFIIFI)I",
         (void *)android_media_SoundPool_play
     },
     {   "pause",