| 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "SoundPool" |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include <algorithm> |
| 22 | #include <thread> |
| 23 | |
| 24 | #include "SoundPool.h" |
| 25 | |
| 26 | namespace android |
| 27 | { |
| 28 | |
| 29 | // kManagerThreads = 1 historically. |
| 30 | // Not really necessary to have more than one, but it does speed things up by about |
| 31 | // 25% having 2 threads instead of 1 when playing many sounds. Having many threads |
| 32 | // could starve other AudioFlinger clients with SoundPool activity. It may also cause |
| 33 | // issues with app loading, e.g. Camera. |
| 34 | static const size_t kStreamManagerThreads = std::thread::hardware_concurrency() >= 4 ? 2 : 1; |
| 35 | |
| 36 | // kUseApiLock = true prior to R. |
| 37 | // Set to true to prevent multiple users access internal to the SoundPool API. |
| 38 | // Set to false to make the SoundPool methods weakly consistent. When set to false, |
| 39 | // only AutoPause and AutoResume are locked, which are the only two methods that |
| 40 | // require API level locking for consistency. |
| 41 | static constexpr bool kUseApiLock = false; |
| 42 | |
| 43 | namespace { |
| 44 | // Check input arguments to SoundPool - return "true" to reject request. |
| 45 | |
| 46 | bool checkVolume(float *leftVolume, float *rightVolume) |
| 47 | { |
| 48 | if (*leftVolume != std::clamp(*leftVolume, 0.f, 1.f) || |
| 49 | *rightVolume != std::clamp(*rightVolume, 0.f, 1.f)) { |
| 50 | ALOGI("volume l=%f r=%f out of (0.f, 1.f) bounds, using 1.f", *leftVolume, *rightVolume); |
| 51 | // for backward compatibility use 1.f. |
| 52 | *leftVolume = *rightVolume = 1.f; |
| 53 | } |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | bool checkRate(float *rate) |
| 58 | { |
| 59 | if (*rate != std::clamp(*rate, 0.125f, 8.f)) { |
| 60 | ALOGI("rate %f out of (0.125f, 8.f) bounds, clamping", *rate); |
| 61 | // for backward compatibility just clamp |
| 62 | *rate = std::clamp(*rate, 0.125f, 8.f); |
| 63 | } |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | bool checkPriority(int32_t *priority) |
| 68 | { |
| 69 | if (*priority < 0) { |
| 70 | ALOGI("negative priority %d, should be >= 0.", *priority); |
| 71 | // for backward compatibility, ignore. |
| 72 | } |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | bool checkLoop(int32_t *loop) |
| 77 | { |
| 78 | if (*loop < -1) { |
| 79 | ALOGI("loop %d, should be >= -1", *loop); |
| 80 | *loop = -1; |
| 81 | } |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | } // namespace |
| 86 | |
| 87 | SoundPool::SoundPool( |
| 88 | int32_t maxStreams, const audio_attributes_t& attributes, |
| 89 | const std::string& opPackageName) |
| 90 | : mStreamManager(maxStreams, kStreamManagerThreads, attributes, opPackageName) |
| 91 | { |
| 92 | ALOGV("%s(maxStreams=%d, attr={ content_type=%d, usage=%d, flags=0x%x, tags=%s })", |
| 93 | __func__, maxStreams, |
| 94 | attributes.content_type, attributes.usage, attributes.flags, attributes.tags); |
| 95 | } |
| 96 | |
| 97 | SoundPool::~SoundPool() |
| 98 | { |
| 99 | ALOGV("%s()", __func__); |
| 100 | } |
| 101 | |
| 102 | int32_t SoundPool::load(int fd, int64_t offset, int64_t length, int32_t priority) |
| 103 | { |
| 104 | ALOGV("%s(fd=%d, offset=%lld, length=%lld, priority=%d)", |
| 105 | __func__, fd, (long long)offset, (long long)length, priority); |
| 106 | auto apiLock = kUseApiLock ? std::make_unique<std::lock_guard<std::mutex>>(mApiLock) : nullptr; |
| 107 | return mSoundManager.load(fd, offset, length, priority); |
| 108 | } |
| 109 | |
| 110 | bool SoundPool::unload(int32_t soundID) |
| 111 | { |
| 112 | ALOGV("%s(%d)", __func__, soundID); |
| 113 | auto apiLock = kUseApiLock ? std::make_unique<std::lock_guard<std::mutex>>(mApiLock) : nullptr; |
| 114 | return mSoundManager.unload(soundID); |
| 115 | } |
| 116 | |
| 117 | int32_t SoundPool::play(int32_t soundID, float leftVolume, float rightVolume, |
| 118 | int32_t priority, int32_t loop, float rate) |
| 119 | { |
| 120 | ALOGV("%s(soundID=%d, leftVolume=%f, rightVolume=%f, priority=%d, loop=%d, rate=%f)", |
| 121 | __func__, soundID, leftVolume, rightVolume, priority, loop, rate); |
| 122 | |
| 123 | // New for R: check arguments to ensure track can be created. |
| 124 | // If SoundPool defers the creation of the AudioTrack to the StreamManager thread, |
| 125 | // the failure to create may not be visible to the caller, so this precheck is needed. |
| 126 | if (checkVolume(&leftVolume, &rightVolume) |
| 127 | || checkPriority(&priority) |
| 128 | || checkLoop(&loop) |
| 129 | || checkRate(&rate)) return 0; |
| 130 | |
| 131 | auto apiLock = kUseApiLock ? std::make_unique<std::lock_guard<std::mutex>>(mApiLock) : nullptr; |
| 132 | const std::shared_ptr<soundpool::Sound> sound = mSoundManager.findSound(soundID); |
| 133 | if (sound == nullptr || sound->getState() != soundpool::Sound::READY) { |
| 134 | ALOGW("%s soundID %d not READY", __func__, soundID); |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | const int32_t streamID = mStreamManager.queueForPlay( |
| 139 | sound, soundID, leftVolume, rightVolume, priority, loop, rate); |
| 140 | ALOGV("%s returned %d", __func__, streamID); |
| 141 | return streamID; |
| 142 | } |
| 143 | |
| 144 | void SoundPool::autoPause() |
| 145 | { |
| 146 | ALOGV("%s()", __func__); |
| 147 | auto apiLock = std::make_unique<std::lock_guard<std::mutex>>(mApiLock); |
| 148 | mStreamManager.forEach([](soundpool::Stream *stream) { stream->autoPause(); }); |
| 149 | } |
| 150 | |
| 151 | void SoundPool::autoResume() |
| 152 | { |
| 153 | ALOGV("%s()", __func__); |
| 154 | auto apiLock = std::make_unique<std::lock_guard<std::mutex>>(mApiLock); |
| 155 | mStreamManager.forEach([](soundpool::Stream *stream) { stream->autoResume(); }); |
| 156 | } |
| 157 | |
| 158 | void SoundPool::mute(bool muting) |
| 159 | { |
| 160 | ALOGV("%s(%d)", __func__, muting); |
| 161 | auto apiLock = std::make_unique<std::lock_guard<std::mutex>>(mApiLock); |
| 162 | mStreamManager.forEach([=](soundpool::Stream *stream) { stream->mute(muting); }); |
| 163 | } |
| 164 | |
| 165 | void SoundPool::pause(int32_t streamID) |
| 166 | { |
| 167 | ALOGV("%s(%d)", __func__, streamID); |
| 168 | auto apiLock = kUseApiLock ? std::make_unique<std::lock_guard<std::mutex>>(mApiLock) : nullptr; |
| 169 | if (soundpool::Stream* stream = mStreamManager.findStream(streamID)) { |
| 170 | stream->pause(streamID); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void SoundPool::resume(int32_t streamID) |
| 175 | { |
| 176 | ALOGV("%s(%d)", __func__, streamID); |
| 177 | auto apiLock = kUseApiLock ? std::make_unique<std::lock_guard<std::mutex>>(mApiLock) : nullptr; |
| 178 | if (soundpool::Stream* stream = mStreamManager.findStream(streamID)) { |
| 179 | stream->resume(streamID); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | void SoundPool::stop(int32_t streamID) |
| 184 | { |
| 185 | ALOGV("%s(%d)", __func__, streamID); |
| 186 | auto apiLock = kUseApiLock ? std::make_unique<std::lock_guard<std::mutex>>(mApiLock) : nullptr; |
| 187 | soundpool::Stream* stream = mStreamManager.findStream(streamID); |
| 188 | if (stream != nullptr && stream->requestStop(streamID)) { |
| 189 | mStreamManager.moveToRestartQueue(stream, streamID); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | void SoundPool::setVolume(int32_t streamID, float leftVolume, float rightVolume) |
| 194 | { |
| 195 | ALOGV("%s(%d, %f %f)", __func__, streamID, leftVolume, rightVolume); |
| 196 | if (checkVolume(&leftVolume, &rightVolume)) return; |
| 197 | auto apiLock = kUseApiLock ? std::make_unique<std::lock_guard<std::mutex>>(mApiLock) : nullptr; |
| 198 | if (soundpool::Stream* stream = mStreamManager.findStream(streamID)) { |
| 199 | stream->setVolume(streamID, leftVolume, rightVolume); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | void SoundPool::setPriority(int32_t streamID, int32_t priority) |
| 204 | { |
| 205 | ALOGV("%s(%d, %d)", __func__, streamID, priority); |
| 206 | if (checkPriority(&priority)) return; |
| 207 | auto apiLock = kUseApiLock ? std::make_unique<std::lock_guard<std::mutex>>(mApiLock) : nullptr; |
| 208 | if (soundpool::Stream* stream = mStreamManager.findStream(streamID)) { |
| 209 | stream->setPriority(streamID, priority); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | void SoundPool::setLoop(int32_t streamID, int32_t loop) |
| 214 | { |
| 215 | ALOGV("%s(%d, %d)", __func__, streamID, loop); |
| 216 | if (checkLoop(&loop)) return; |
| 217 | auto apiLock = kUseApiLock ? std::make_unique<std::lock_guard<std::mutex>>(mApiLock) : nullptr; |
| 218 | if (soundpool::Stream* stream = mStreamManager.findStream(streamID)) { |
| 219 | stream->setLoop(streamID, loop); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | void SoundPool::setRate(int32_t streamID, float rate) |
| 224 | { |
| 225 | ALOGV("%s(%d, %f)", __func__, streamID, rate); |
| 226 | if (checkRate(&rate)) return; |
| 227 | auto apiLock = kUseApiLock ? std::make_unique<std::lock_guard<std::mutex>>(mApiLock) : nullptr; |
| 228 | if (soundpool::Stream* stream = mStreamManager.findStream(streamID)) { |
| 229 | stream->setRate(streamID, rate); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | void SoundPool::setCallback(SoundPoolCallback* callback, void* user) |
| 234 | { |
| 235 | ALOGV("%s(%p, %p)", __func__, callback, user); |
| 236 | auto apiLock = kUseApiLock ? std::make_unique<std::lock_guard<std::mutex>>(mApiLock) : nullptr; |
| 237 | mSoundManager.setCallback(this, callback, user); |
| 238 | } |
| 239 | |
| 240 | void* SoundPool::getUserData() const |
| 241 | { |
| 242 | ALOGV("%s()", __func__); |
| 243 | auto apiLock = kUseApiLock ? std::make_unique<std::lock_guard<std::mutex>>(mApiLock) : nullptr; |
| 244 | return mSoundManager.getUserData(); |
| 245 | } |
| 246 | |
| 247 | } // end namespace android |