| 1 | /* |
| 2 | * Copyright (C) 2019 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::SoundManager" |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include "SoundManager.h" |
| 22 | |
| 23 | #include <thread> |
| 24 | |
| 25 | #include "SoundDecoder.h" |
| 26 | |
| 27 | namespace android::soundpool { |
| 28 | |
| 29 | static const size_t kDecoderThreads = std::thread::hardware_concurrency() >= 4 ? 2 : 1; |
| 30 | |
| 31 | SoundManager::SoundManager() |
| 32 | : mDecoder{std::make_unique<SoundDecoder>(this, kDecoderThreads)} |
| 33 | { |
| 34 | ALOGV("%s()", __func__); |
| 35 | } |
| 36 | |
| 37 | SoundManager::~SoundManager() |
| 38 | { |
| 39 | ALOGV("%s()", __func__); |
| 40 | mDecoder->quit(); |
| 41 | |
| 42 | std::lock_guard lock(mSoundManagerLock); |
| 43 | mSounds.clear(); |
| 44 | } |
| 45 | |
| 46 | int32_t SoundManager::load(int fd, int64_t offset, int64_t length, int32_t priority) |
| 47 | { |
| 48 | ALOGV("%s(fd=%d, offset=%lld, length=%lld, priority=%d)", |
| 49 | __func__, fd, (long long)offset, (long long)length, priority); |
| 50 | int32_t soundID; |
| 51 | { |
| 52 | std::lock_guard lock(mSoundManagerLock); |
| 53 | // mNextSoundID is always positive and does not "integer overflow" |
| 54 | do { |
| 55 | mNextSoundID = mNextSoundID == INT32_MAX ? 1 : mNextSoundID + 1; |
| 56 | } while (findSound_l(mNextSoundID) != nullptr); |
| 57 | soundID = mNextSoundID; |
| 58 | auto sound = std::make_shared<Sound>(soundID, fd, offset, length); |
| 59 | mSounds.emplace(soundID, sound); |
| 60 | } |
| 61 | // mDecoder->loadSound() must be called outside of mSoundManagerLock. |
| 62 | // mDecoder->loadSound() may block on mDecoder message queue space; |
| 63 | // the message queue emptying may block on SoundManager::findSound(). |
| 64 | // |
| 65 | // It is theoretically possible that sound loads might decode out-of-order. |
| 66 | mDecoder->loadSound(soundID); |
| 67 | return soundID; |
| 68 | } |
| 69 | |
| 70 | bool SoundManager::unload(int32_t soundID) |
| 71 | { |
| 72 | ALOGV("%s(soundID=%d)", __func__, soundID); |
| 73 | std::lock_guard lock(mSoundManagerLock); |
| 74 | return mSounds.erase(soundID) > 0; // erase() returns number of sounds removed. |
| 75 | } |
| 76 | |
| 77 | std::shared_ptr<Sound> SoundManager::findSound(int32_t soundID) const |
| 78 | { |
| 79 | std::lock_guard lock(mSoundManagerLock); |
| 80 | return findSound_l(soundID); |
| 81 | } |
| 82 | |
| 83 | std::shared_ptr<Sound> SoundManager::findSound_l(int32_t soundID) const |
| 84 | { |
| 85 | auto it = mSounds.find(soundID); |
| 86 | return it != mSounds.end() ? it->second : nullptr; |
| 87 | } |
| 88 | |
| 89 | void SoundManager::setCallback(SoundPool *soundPool, SoundPoolCallback* callback, void* user) |
| 90 | { |
| 91 | mCallbackHandler.setCallback(soundPool, callback, user); |
| 92 | } |
| 93 | |
| 94 | void SoundManager::notify(SoundPoolEvent event) |
| 95 | { |
| 96 | mCallbackHandler.notify(event); |
| 97 | } |
| 98 | |
| 99 | void* SoundManager::getUserData() const |
| 100 | { |
| 101 | return mCallbackHandler.getUserData(); |
| 102 | } |
| 103 | |
| 104 | } // namespace android::soundpool |