Skip to content

Commit 946d9f1

Browse files
flammeEric Laurent
authored andcommitted
Add package name when initializing SoundPool.
The package name is useful for permission validation. Currently, package name is required when starting external vibration so that the vibrator service can verify if the app has the permission to start vibration. Bug: 175595599 Test: SoundPool tests in cts Test: play audio-coupled-haptic file using SoundPool. Change-Id: Ibfe414063a303118a5bb393d65ea5dc2323bf722 Merged-In: Ibfe414063a303118a5bb393d65ea5dc2323bf722 (cherry picked from commit de9005bab1751e23342bbc9f0f51213eea8885af)
1 parent 0d094ad commit 946d9f1

7 files changed

Lines changed: 28 additions & 11 deletions

File tree

media/java/android/media/SoundPool.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ private SoundPool(int maxStreams, AudioAttributes attributes) {
149149
super(attributes, AudioPlaybackConfiguration.PLAYER_TYPE_JAM_SOUNDPOOL);
150150

151151
// do native setup
152-
if (native_setup(new WeakReference<SoundPool>(this), maxStreams, attributes) != 0) {
152+
if (native_setup(new WeakReference<SoundPool>(this),
153+
maxStreams, attributes, getCurrentOpPackageName()) != 0) {
153154
throw new RuntimeException("Native setup failed");
154155
}
155156
mAttributes = attributes;
@@ -501,7 +502,7 @@ public void setOnLoadCompleteListener(OnLoadCompleteListener listener) {
501502
private native final int _load(FileDescriptor fd, long offset, long length, int priority);
502503

503504
private native final int native_setup(Object weakRef, int maxStreams,
504-
Object/*AudioAttributes*/ attributes);
505+
@NonNull Object/*AudioAttributes*/ attributes, @NonNull String opPackageName);
505506

506507
private native final int _play(int soundID, float leftVolume, float rightVolume,
507508
int priority, int loop, float rate);

media/jni/soundpool/SoundPool.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ bool checkLoop(int32_t *loop)
8484

8585
} // namespace
8686

87-
SoundPool::SoundPool(int32_t maxStreams, const audio_attributes_t* attributes)
88-
: mStreamManager(maxStreams, kStreamManagerThreads, attributes)
87+
SoundPool::SoundPool(
88+
int32_t maxStreams, const audio_attributes_t* attributes, const std::string& opPackageName)
89+
: mStreamManager(maxStreams, kStreamManagerThreads, attributes, opPackageName)
8990
{
9091
ALOGV("%s(maxStreams=%d, attr={ content_type=%d, usage=%d, flags=0x%x, tags=%s })",
9192
__func__, maxStreams,

media/jni/soundpool/SoundPool.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "SoundManager.h"
2020
#include "StreamManager.h"
2121

22+
#include <string>
23+
2224
namespace android {
2325

2426
/**
@@ -29,7 +31,8 @@ namespace android {
2931
*/
3032
class SoundPool {
3133
public:
32-
SoundPool(int32_t maxStreams, const audio_attributes_t* attributes);
34+
SoundPool(int32_t maxStreams, const audio_attributes_t* attributes,
35+
const std::string& opPackageName = {});
3336
~SoundPool();
3437

3538
// SoundPool Java API support

media/jni/soundpool/Stream.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,9 @@ void Stream::play_l(const std::shared_ptr<Sound>& sound, int32_t nextStreamID,
332332
0 /*default notification frames*/, AUDIO_SESSION_ALLOCATE,
333333
AudioTrack::TRANSFER_DEFAULT,
334334
nullptr /*offloadInfo*/, -1 /*uid*/, -1 /*pid*/,
335-
mStreamManager->getAttributes());
335+
mStreamManager->getAttributes(),
336+
false /*doNotReconnect*/, 1.0f /*maxRequiredSpeed*/,
337+
mStreamManager->getOpPackageName());
336338
// Set caller name so it can be logged in destructor.
337339
// MediaMetricsConstants.h: AMEDIAMETRICS_PROP_CALLERNAME_VALUE_SOUNDPOOL
338340
newTrack->setCallerName("soundpool");

media/jni/soundpool/StreamManager.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,11 @@ int32_t StreamMap::getNextIdForStream(Stream* stream) const {
9898
#pragma clang diagnostic ignored "-Wthread-safety-analysis"
9999

100100
StreamManager::StreamManager(
101-
int32_t streams, size_t threads, const audio_attributes_t* attributes)
101+
int32_t streams, size_t threads, const audio_attributes_t* attributes,
102+
std::string opPackageName)
102103
: StreamMap(streams)
103104
, mAttributes(*attributes)
105+
, mOpPackageName(std::move(opPackageName))
104106
{
105107
ALOGV("%s(%d, %zu, ...)", __func__, streams, threads);
106108
forEach([this](Stream *stream) {

media/jni/soundpool/StreamManager.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <map>
2525
#include <memory>
2626
#include <mutex>
27+
#include <string>
2728
#include <unordered_set>
2829
#include <vector>
2930

@@ -386,7 +387,8 @@ class StreamManager : public StreamMap {
386387
public:
387388
// Note: the SoundPool pointer is only used for stream initialization.
388389
// It is not stored in StreamManager.
389-
StreamManager(int32_t streams, size_t threads, const audio_attributes_t* attributes);
390+
StreamManager(int32_t streams, size_t threads, const audio_attributes_t* attributes,
391+
std::string opPackageName);
390392
~StreamManager();
391393

392394
// Returns positive streamID on success, 0 on failure. This is locked.
@@ -400,6 +402,8 @@ class StreamManager : public StreamMap {
400402

401403
const audio_attributes_t* getAttributes() const { return &mAttributes; }
402404

405+
const std::string& getOpPackageName() const { return mOpPackageName; }
406+
403407
// Moves the stream to the restart queue (called upon BUFFER_END of the static track)
404408
// this is locked internally.
405409
// If activeStreamIDToMatch is nonzero, it will only move to the restart queue
@@ -473,6 +477,8 @@ class StreamManager : public StreamMap {
473477
// The paired stream may be active or restarting.
474478
// No particular order.
475479
std::unordered_set<Stream*> mProcessingStreams GUARDED_BY(mStreamManagerLock);
480+
481+
const std::string mOpPackageName;
476482
};
477483

478484
} // namespace android::soundpool

media/jni/soundpool/android_media_SoundPool.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <utils/Log.h>
2323
#include <jni.h>
2424
#include <nativehelper/JNIHelp.h>
25+
#include <nativehelper/ScopedUtfChars.h>
2526
#include <android_runtime/AndroidRuntime.h>
2627
#include "SoundPool.h"
2728

@@ -181,7 +182,7 @@ static void android_media_callback(SoundPoolEvent event, SoundPool* soundPool, v
181182

182183
static jint
183184
android_media_SoundPool_native_setup(JNIEnv *env, jobject thiz, jobject weakRef,
184-
jint maxChannels, jobject jaa)
185+
jint maxChannels, jobject jaa, jstring opPackageName)
185186
{
186187
if (jaa == nullptr) {
187188
ALOGE("Error creating SoundPool: invalid audio attributes");
@@ -203,7 +204,8 @@ android_media_SoundPool_native_setup(JNIEnv *env, jobject thiz, jobject weakRef,
203204
paa->flags = env->GetIntField(jaa, javaAudioAttrFields.fieldFlags);
204205

205206
ALOGV("android_media_SoundPool_native_setup");
206-
auto *ap = new SoundPool(maxChannels, paa);
207+
ScopedUtfChars opPackageNameStr(env, opPackageName);
208+
auto *ap = new SoundPool(maxChannels, paa, opPackageNameStr.c_str());
207209
if (ap == nullptr) {
208210
return -1;
209211
}
@@ -298,7 +300,7 @@ static JNINativeMethod gMethods[] = {
298300
(void *)android_media_SoundPool_setRate
299301
},
300302
{ "native_setup",
301-
"(Ljava/lang/Object;ILjava/lang/Object;)I",
303+
"(Ljava/lang/Object;ILjava/lang/Object;Ljava/lang/String;)I",
302304
(void*)android_media_SoundPool_native_setup
303305
},
304306
{ "native_release",

0 commit comments

Comments
 (0)