| Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 1 | /* | 
|  | 2 | ** | 
|  | 3 | ** Copyright 2022, The Android Open Source Project | 
|  | 4 | ** | 
|  | 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 6 | ** you may not use this file except in compliance with the License. | 
|  | 7 | ** You may obtain a copy of the License at | 
|  | 8 | ** | 
|  | 9 | **     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 10 | ** | 
|  | 11 | ** Unless required by applicable law or agreed to in writing, software | 
|  | 12 | ** distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 14 | ** See the License for the specific language governing permissions and | 
|  | 15 | ** limitations under the License. | 
|  | 16 | */ | 
|  | 17 |  | 
|  | 18 | // #define LOG_NDEBUG 0 | 
|  | 19 | #define LOG_TAG "SoundDoseManager" | 
|  | 20 |  | 
|  | 21 | #include "SoundDoseManager.h" | 
|  | 22 |  | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 23 | #include "android/media/SoundDoseRecord.h" | 
| Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 24 | #include <android-base/stringprintf.h> | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 25 | #include <media/AidlConversionCppNdk.h> | 
|  | 26 | #include <cinttypes> | 
| Vlad Popa | 4847de1 | 2023-03-16 18:30:08 +0100 | [diff] [blame] | 27 | #include <ctime> | 
| Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 28 | #include <utils/Log.h> | 
| Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 29 |  | 
|  | 30 | namespace android { | 
|  | 31 |  | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 32 | using aidl::android::media::audio::common::AudioDevice; | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 33 |  | 
| Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 34 | namespace { | 
|  | 35 |  | 
|  | 36 | int64_t getMonotonicSecond() { | 
|  | 37 | struct timespec now_ts; | 
|  | 38 | if (clock_gettime(CLOCK_MONOTONIC, &now_ts) != 0) { | 
|  | 39 | ALOGE("%s: cannot get timestamp", __func__); | 
|  | 40 | return -1; | 
|  | 41 | } | 
|  | 42 | return now_ts.tv_sec; | 
|  | 43 | } | 
|  | 44 |  | 
|  | 45 | }  // namespace | 
|  | 46 |  | 
| Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 47 | sp<audio_utils::MelProcessor> SoundDoseManager::getOrCreateProcessorForDevice( | 
| Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 48 | audio_port_handle_t deviceId, audio_io_handle_t streamHandle, uint32_t sampleRate, | 
|  | 49 | size_t channelCount, audio_format_t format) { | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 50 | const std::lock_guard _l(mLock); | 
| Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 51 |  | 
| Vlad Popa | 63c48dd | 2023-08-21 19:52:14 -0700 | [diff] [blame] | 52 | if (mHalSoundDose.size() > 0 && mEnabledCsd) { | 
| Vlad Popa | 1c2f7e1 | 2023-03-28 02:08:56 +0200 | [diff] [blame] | 53 | ALOGD("%s: using HAL MEL computation, no MelProcessor needed.", __func__); | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 54 | return nullptr; | 
|  | 55 | } | 
|  | 56 |  | 
| Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 57 | auto streamProcessor = mActiveProcessors.find(streamHandle); | 
| Vlad Popa | 1c2f7e1 | 2023-03-28 02:08:56 +0200 | [diff] [blame] | 58 | if (streamProcessor != mActiveProcessors.end()) { | 
|  | 59 | auto processor = streamProcessor->second.promote(); | 
|  | 60 | // if processor is nullptr it means it was removed by the playback | 
|  | 61 | // thread and can be replaced in the mActiveProcessors map | 
|  | 62 | if (processor != nullptr) { | 
|  | 63 | ALOGV("%s: found callback for stream id %d", __func__, streamHandle); | 
|  | 64 | const auto activeTypeIt = mActiveDeviceTypes.find(deviceId); | 
|  | 65 | if (activeTypeIt != mActiveDeviceTypes.end()) { | 
|  | 66 | processor->setAttenuation(mMelAttenuationDB[activeTypeIt->second]); | 
|  | 67 | } | 
|  | 68 | processor->setDeviceId(deviceId); | 
|  | 69 | processor->setOutputRs2UpperBound(mRs2UpperBound); | 
|  | 70 | return processor; | 
| Vlad Popa | 58e72dc | 2023-02-01 13:18:40 +0100 | [diff] [blame] | 71 | } | 
| Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 72 | } | 
| Vlad Popa | 1c2f7e1 | 2023-03-28 02:08:56 +0200 | [diff] [blame] | 73 |  | 
|  | 74 | ALOGV("%s: creating new callback for stream id %d", __func__, streamHandle); | 
|  | 75 | sp<audio_utils::MelProcessor> melProcessor = sp<audio_utils::MelProcessor>::make( | 
|  | 76 | sampleRate, channelCount, format, this, deviceId, mRs2UpperBound); | 
|  | 77 | const auto activeTypeIt = mActiveDeviceTypes.find(deviceId); | 
|  | 78 | if (activeTypeIt != mActiveDeviceTypes.end()) { | 
|  | 79 | melProcessor->setAttenuation(mMelAttenuationDB[activeTypeIt->second]); | 
|  | 80 | } | 
|  | 81 | mActiveProcessors[streamHandle] = melProcessor; | 
|  | 82 | return melProcessor; | 
| Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 83 | } | 
|  | 84 |  | 
| Vlad Popa | 63c48dd | 2023-08-21 19:52:14 -0700 | [diff] [blame] | 85 | bool SoundDoseManager::setHalSoundDoseInterface(const std::string &module, | 
|  | 86 | const std::shared_ptr<ISoundDose> &halSoundDose) { | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 87 | ALOGV("%s", __func__); | 
|  | 88 |  | 
| Vlad Popa | 63c48dd | 2023-08-21 19:52:14 -0700 | [diff] [blame] | 89 | if (halSoundDose == nullptr) { | 
|  | 90 | ALOGI("%s: passed ISoundDose object is null", __func__); | 
|  | 91 | return false; | 
|  | 92 | } | 
|  | 93 |  | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 94 | std::shared_ptr<HalSoundDoseCallback> halSoundDoseCallback; | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 95 | { | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 96 | const std::lock_guard _l(mLock); | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 97 |  | 
| Vlad Popa | 63c48dd | 2023-08-21 19:52:14 -0700 | [diff] [blame] | 98 | if (mHalSoundDose.find(module) != mHalSoundDose.end()) { | 
|  | 99 | ALOGW("%s: Module %s already has a sound dose HAL assigned, skipping", __func__, | 
|  | 100 | module.c_str()); | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 101 | return false; | 
|  | 102 | } | 
| Vlad Popa | 63c48dd | 2023-08-21 19:52:14 -0700 | [diff] [blame] | 103 | mHalSoundDose[module] = halSoundDose; | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 104 |  | 
| Vlad Popa | 63c48dd | 2023-08-21 19:52:14 -0700 | [diff] [blame] | 105 | if (!halSoundDose->setOutputRs2UpperBound(mRs2UpperBound).isOk()) { | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 106 | ALOGW("%s: Cannot set RS2 value for momentary exposure %f", | 
|  | 107 | __func__, | 
| Vlad Popa | 4847de1 | 2023-03-16 18:30:08 +0100 | [diff] [blame] | 108 | mRs2UpperBound); | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 109 | } | 
|  | 110 |  | 
|  | 111 | // initialize the HAL sound dose callback lazily | 
|  | 112 | if (mHalSoundDoseCallback == nullptr) { | 
|  | 113 | mHalSoundDoseCallback = | 
|  | 114 | ndk::SharedRefBase::make<HalSoundDoseCallback>(this); | 
|  | 115 | } | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 116 | halSoundDoseCallback = mHalSoundDoseCallback; | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 117 | } | 
|  | 118 |  | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 119 | auto status = halSoundDose->registerSoundDoseCallback(halSoundDoseCallback); | 
|  | 120 |  | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 121 | if (!status.isOk()) { | 
|  | 122 | // Not a warning since this can happen if the callback was registered before | 
|  | 123 | ALOGI("%s: Cannot register HAL sound dose callback with status message: %s", | 
|  | 124 | __func__, | 
|  | 125 | status.getMessage()); | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | return true; | 
|  | 129 | } | 
|  | 130 |  | 
| Vlad Popa | 63c48dd | 2023-08-21 19:52:14 -0700 | [diff] [blame] | 131 | void SoundDoseManager::resetHalSoundDoseInterfaces() { | 
|  | 132 | ALOGV("%s", __func__); | 
|  | 133 |  | 
|  | 134 | const std::lock_guard _l(mLock); | 
|  | 135 | mHalSoundDose.clear(); | 
|  | 136 | } | 
|  | 137 |  | 
| Vlad Popa | 4847de1 | 2023-03-16 18:30:08 +0100 | [diff] [blame] | 138 | void SoundDoseManager::setOutputRs2UpperBound(float rs2Value) { | 
| Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 139 | ALOGV("%s", __func__); | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 140 | const std::lock_guard _l(mLock); | 
| Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 141 |  | 
| Vlad Popa | 63c48dd | 2023-08-21 19:52:14 -0700 | [diff] [blame] | 142 | if (mHalSoundDose.size() > 0) { | 
|  | 143 | for (auto& halSoundDose : mHalSoundDose) { | 
|  | 144 | // using the HAL sound dose interface | 
|  | 145 | if (!halSoundDose.second->setOutputRs2UpperBound(rs2Value).isOk()) { | 
|  | 146 | ALOGE("%s: Cannot set RS2 value for momentary exposure %f", __func__, rs2Value); | 
|  | 147 | continue; | 
|  | 148 | } | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 149 | } | 
| Vlad Popa | 63c48dd | 2023-08-21 19:52:14 -0700 | [diff] [blame] | 150 |  | 
| Vlad Popa | 4847de1 | 2023-03-16 18:30:08 +0100 | [diff] [blame] | 151 | mRs2UpperBound = rs2Value; | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 152 | return; | 
|  | 153 | } | 
| Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 154 |  | 
| Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 155 | for (auto& streamProcessor : mActiveProcessors) { | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 156 | const sp<audio_utils::MelProcessor> processor = streamProcessor.second.promote(); | 
| Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 157 | if (processor != nullptr) { | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 158 | const status_t result = processor->setOutputRs2UpperBound(rs2Value); | 
| Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 159 | if (result != NO_ERROR) { | 
| Vlad Popa | 4847de1 | 2023-03-16 18:30:08 +0100 | [diff] [blame] | 160 | ALOGW("%s: could not set RS2 upper bound %f for stream %d", __func__, rs2Value, | 
| Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 161 | streamProcessor.first); | 
| Vlad Popa | 3c3995d | 2023-01-13 11:10:05 +0100 | [diff] [blame] | 162 | return; | 
| Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 163 | } | 
| Vlad Popa | 4847de1 | 2023-03-16 18:30:08 +0100 | [diff] [blame] | 164 | mRs2UpperBound = rs2Value; | 
| Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 165 | } | 
|  | 166 | } | 
|  | 167 | } | 
|  | 168 |  | 
| Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 169 | void SoundDoseManager::removeStreamProcessor(audio_io_handle_t streamHandle) { | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 170 | const std::lock_guard _l(mLock); | 
| Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 171 | auto callbackToRemove = mActiveProcessors.find(streamHandle); | 
| Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 172 | if (callbackToRemove != mActiveProcessors.end()) { | 
| Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 173 | mActiveProcessors.erase(callbackToRemove); | 
|  | 174 | } | 
|  | 175 | } | 
|  | 176 |  | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 177 | audio_port_handle_t SoundDoseManager::getIdForAudioDevice(const AudioDevice& audioDevice) const { | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 178 | const std::lock_guard _l(mLock); | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 179 |  | 
|  | 180 | audio_devices_t type; | 
|  | 181 | std::string address; | 
|  | 182 | auto result = aidl::android::aidl2legacy_AudioDevice_audio_device( | 
|  | 183 | audioDevice, &type, &address); | 
|  | 184 | if (result != NO_ERROR) { | 
|  | 185 | ALOGE("%s: could not convert from AudioDevice to AudioDeviceTypeAddr", __func__); | 
|  | 186 | return AUDIO_PORT_HANDLE_NONE; | 
|  | 187 | } | 
|  | 188 |  | 
|  | 189 | auto adt = AudioDeviceTypeAddr(type, address); | 
|  | 190 | auto deviceIt = mActiveDevices.find(adt); | 
|  | 191 | if (deviceIt == mActiveDevices.end()) { | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 192 | ALOGI("%s: could not find port id for device %s", __func__, adt.toString().c_str()); | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 193 | return AUDIO_PORT_HANDLE_NONE; | 
|  | 194 | } | 
|  | 195 | return deviceIt->second; | 
|  | 196 | } | 
|  | 197 |  | 
|  | 198 | void SoundDoseManager::mapAddressToDeviceId(const AudioDeviceTypeAddr& adt, | 
|  | 199 | const audio_port_handle_t deviceId) { | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 200 | const std::lock_guard _l(mLock); | 
| Vlad Popa | eafa048 | 2023-02-23 14:35:56 +0100 | [diff] [blame] | 201 | ALOGI("%s: map address: %d to device id: %d", __func__, adt.mType, deviceId); | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 202 | mActiveDevices[adt] = deviceId; | 
| Vlad Popa | 58e72dc | 2023-02-01 13:18:40 +0100 | [diff] [blame] | 203 | mActiveDeviceTypes[deviceId] = adt.mType; | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 204 | } | 
|  | 205 |  | 
|  | 206 | void SoundDoseManager::clearMapDeviceIdEntries(audio_port_handle_t deviceId) { | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 207 | const std::lock_guard _l(mLock); | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 208 | for (auto activeDevice = mActiveDevices.begin(); activeDevice != mActiveDevices.end();) { | 
|  | 209 | if (activeDevice->second == deviceId) { | 
| Vlad Popa | eafa048 | 2023-02-23 14:35:56 +0100 | [diff] [blame] | 210 | ALOGI("%s: clear mapping type: %d to deviceId: %d", | 
|  | 211 | __func__, activeDevice->first.mType, deviceId); | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 212 | activeDevice = mActiveDevices.erase(activeDevice); | 
|  | 213 | continue; | 
|  | 214 | } | 
|  | 215 | ++activeDevice; | 
|  | 216 | } | 
| Vlad Popa | 58e72dc | 2023-02-01 13:18:40 +0100 | [diff] [blame] | 217 | mActiveDeviceTypes.erase(deviceId); | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 218 | } | 
|  | 219 |  | 
|  | 220 | ndk::ScopedAStatus SoundDoseManager::HalSoundDoseCallback::onMomentaryExposureWarning( | 
|  | 221 | float in_currentDbA, const AudioDevice& in_audioDevice) { | 
| Vlad Popa | 63c48dd | 2023-08-21 19:52:14 -0700 | [diff] [blame] | 222 | sp<SoundDoseManager> soundDoseManager; | 
|  | 223 | { | 
|  | 224 | const std::lock_guard _l(mCbLock); | 
|  | 225 | soundDoseManager = mSoundDoseManager.promote(); | 
|  | 226 | if (soundDoseManager == nullptr) { | 
|  | 227 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); | 
|  | 228 | } | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 229 | } | 
|  | 230 |  | 
| Vlad Popa | 63c48dd | 2023-08-21 19:52:14 -0700 | [diff] [blame] | 231 | if (!soundDoseManager->useHalSoundDose()) { | 
| Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 232 | ALOGW("%s: HAL sound dose interface deactivated. Ignoring", __func__); | 
|  | 233 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); | 
|  | 234 | } | 
|  | 235 |  | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 236 | auto id = soundDoseManager->getIdForAudioDevice(in_audioDevice); | 
|  | 237 | if (id == AUDIO_PORT_HANDLE_NONE) { | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 238 | ALOGI("%s: no mapped id for audio device with type %d and address %s", | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 239 | __func__, in_audioDevice.type.type, | 
| Vlad Popa | 69fbbee | 2023-04-25 12:23:24 +0200 | [diff] [blame] | 240 | in_audioDevice.address.toString().c_str()); | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 241 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); | 
|  | 242 | } | 
|  | 243 | soundDoseManager->onMomentaryExposure(in_currentDbA, id); | 
|  | 244 |  | 
|  | 245 | return ndk::ScopedAStatus::ok(); | 
|  | 246 | } | 
|  | 247 |  | 
|  | 248 | ndk::ScopedAStatus SoundDoseManager::HalSoundDoseCallback::onNewMelValues( | 
|  | 249 | const ISoundDose::IHalSoundDoseCallback::MelRecord& in_melRecord, | 
|  | 250 | const AudioDevice& in_audioDevice) { | 
| Vlad Popa | 63c48dd | 2023-08-21 19:52:14 -0700 | [diff] [blame] | 251 | sp<SoundDoseManager> soundDoseManager; | 
|  | 252 | { | 
|  | 253 | const std::lock_guard _l(mCbLock); | 
|  | 254 | soundDoseManager = mSoundDoseManager.promote(); | 
|  | 255 | if (soundDoseManager == nullptr) { | 
|  | 256 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); | 
|  | 257 | } | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 258 | } | 
| Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 259 |  | 
| Vlad Popa | 63c48dd | 2023-08-21 19:52:14 -0700 | [diff] [blame] | 260 | if (!soundDoseManager->useHalSoundDose()) { | 
| Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 261 | ALOGW("%s: HAL sound dose interface deactivated. Ignoring", __func__); | 
|  | 262 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); | 
|  | 263 | } | 
|  | 264 |  | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 265 | auto id = soundDoseManager->getIdForAudioDevice(in_audioDevice); | 
|  | 266 | if (id == AUDIO_PORT_HANDLE_NONE) { | 
| Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 267 | ALOGI("%s: no mapped id for audio device with type %d and address %s", | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 268 | __func__, in_audioDevice.type.type, | 
| Vlad Popa | 69fbbee | 2023-04-25 12:23:24 +0200 | [diff] [blame] | 269 | in_audioDevice.address.toString().c_str()); | 
| Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 270 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); | 
|  | 271 | } | 
|  | 272 | // TODO: introduce timestamp in onNewMelValues callback | 
|  | 273 | soundDoseManager->onNewMelValues(in_melRecord.melValues, 0, | 
|  | 274 | in_melRecord.melValues.size(), id); | 
|  | 275 |  | 
|  | 276 | return ndk::ScopedAStatus::ok(); | 
|  | 277 | } | 
|  | 278 |  | 
| Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 279 | void SoundDoseManager::SoundDose::binderDied(__unused const wp<IBinder>& who) { | 
|  | 280 | ALOGV("%s", __func__); | 
|  | 281 |  | 
|  | 282 | auto soundDoseManager = mSoundDoseManager.promote(); | 
|  | 283 | if (soundDoseManager != nullptr) { | 
|  | 284 | soundDoseManager->resetSoundDose(); | 
|  | 285 | } | 
|  | 286 | } | 
|  | 287 |  | 
| Vlad Popa | 4847de1 | 2023-03-16 18:30:08 +0100 | [diff] [blame] | 288 | binder::Status SoundDoseManager::SoundDose::setOutputRs2UpperBound(float value) { | 
| Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 289 | ALOGV("%s", __func__); | 
|  | 290 | auto soundDoseManager = mSoundDoseManager.promote(); | 
|  | 291 | if (soundDoseManager != nullptr) { | 
| Vlad Popa | 4847de1 | 2023-03-16 18:30:08 +0100 | [diff] [blame] | 292 | soundDoseManager->setOutputRs2UpperBound(value); | 
| Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 293 | } | 
|  | 294 | return binder::Status::ok(); | 
|  | 295 | } | 
|  | 296 |  | 
|  | 297 | binder::Status SoundDoseManager::SoundDose::resetCsd( | 
|  | 298 | float currentCsd, const std::vector<media::SoundDoseRecord>& records) { | 
|  | 299 | ALOGV("%s", __func__); | 
|  | 300 | auto soundDoseManager = mSoundDoseManager.promote(); | 
|  | 301 | if (soundDoseManager != nullptr) { | 
|  | 302 | soundDoseManager->resetCsd(currentCsd, records); | 
|  | 303 | } | 
|  | 304 | return binder::Status::ok(); | 
|  | 305 | } | 
|  | 306 |  | 
| Vlad Popa | 58e72dc | 2023-02-01 13:18:40 +0100 | [diff] [blame] | 307 | binder::Status SoundDoseManager::SoundDose::updateAttenuation(float attenuationDB, int device) { | 
|  | 308 | ALOGV("%s", __func__); | 
|  | 309 | auto soundDoseManager = mSoundDoseManager.promote(); | 
|  | 310 | if (soundDoseManager != nullptr) { | 
|  | 311 | soundDoseManager->updateAttenuation(attenuationDB, static_cast<audio_devices_t>(device)); | 
|  | 312 | } | 
|  | 313 | return binder::Status::ok(); | 
|  | 314 | } | 
|  | 315 |  | 
| Vlad Popa | 617bbf0 | 2023-04-24 19:10:36 +0200 | [diff] [blame] | 316 | binder::Status SoundDoseManager::SoundDose::setCsdEnabled(bool enabled) { | 
| Vlad Popa | 2a06fca | 2023-02-06 16:45:45 +0100 | [diff] [blame] | 317 | ALOGV("%s", __func__); | 
|  | 318 | auto soundDoseManager = mSoundDoseManager.promote(); | 
|  | 319 | if (soundDoseManager != nullptr) { | 
| Vlad Popa | 617bbf0 | 2023-04-24 19:10:36 +0200 | [diff] [blame] | 320 | soundDoseManager->setCsdEnabled(enabled); | 
| Vlad Popa | 2a06fca | 2023-02-06 16:45:45 +0100 | [diff] [blame] | 321 | } | 
|  | 322 | return binder::Status::ok(); | 
|  | 323 | } | 
|  | 324 |  | 
| Vlad Popa | 197faf8 | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 325 | binder::Status SoundDoseManager::SoundDose::initCachedAudioDeviceCategories( | 
|  | 326 | const std::vector<media::ISoundDose::AudioDeviceCategory>& btDeviceCategories) { | 
|  | 327 | ALOGV("%s", __func__); | 
|  | 328 | auto soundDoseManager = mSoundDoseManager.promote(); | 
|  | 329 | if (soundDoseManager != nullptr) { | 
|  | 330 | soundDoseManager->initCachedAudioDeviceCategories(btDeviceCategories); | 
|  | 331 | } | 
|  | 332 | return binder::Status::ok(); | 
|  | 333 | } | 
|  | 334 | binder::Status SoundDoseManager::SoundDose::setAudioDeviceCategory( | 
|  | 335 | const media::ISoundDose::AudioDeviceCategory& btAudioDevice) { | 
|  | 336 | ALOGV("%s", __func__); | 
|  | 337 | auto soundDoseManager = mSoundDoseManager.promote(); | 
|  | 338 | if (soundDoseManager != nullptr) { | 
|  | 339 | soundDoseManager->setAudioDeviceCategory(btAudioDevice); | 
|  | 340 | } | 
|  | 341 | return binder::Status::ok(); | 
|  | 342 | } | 
|  | 343 |  | 
| Vlad Popa | 4847de1 | 2023-03-16 18:30:08 +0100 | [diff] [blame] | 344 | binder::Status SoundDoseManager::SoundDose::getOutputRs2UpperBound(float* value) { | 
| Vlad Popa | 9193046 | 2022-12-20 22:42:48 +0100 | [diff] [blame] | 345 | ALOGV("%s", __func__); | 
|  | 346 | auto soundDoseManager = mSoundDoseManager.promote(); | 
|  | 347 | if (soundDoseManager != nullptr) { | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 348 | const std::lock_guard _l(soundDoseManager->mLock); | 
| Vlad Popa | 4847de1 | 2023-03-16 18:30:08 +0100 | [diff] [blame] | 349 | *value = soundDoseManager->mRs2UpperBound; | 
| Vlad Popa | 9193046 | 2022-12-20 22:42:48 +0100 | [diff] [blame] | 350 | } | 
|  | 351 | return binder::Status::ok(); | 
|  | 352 | } | 
|  | 353 |  | 
|  | 354 | binder::Status SoundDoseManager::SoundDose::getCsd(float* value) { | 
|  | 355 | ALOGV("%s", __func__); | 
|  | 356 | auto soundDoseManager = mSoundDoseManager.promote(); | 
|  | 357 | if (soundDoseManager != nullptr) { | 
|  | 358 | *value = soundDoseManager->mMelAggregator->getCsd(); | 
|  | 359 | } | 
|  | 360 | return binder::Status::ok(); | 
|  | 361 | } | 
|  | 362 |  | 
|  | 363 | binder::Status SoundDoseManager::SoundDose::forceUseFrameworkMel(bool useFrameworkMel) { | 
|  | 364 | ALOGV("%s", __func__); | 
|  | 365 | auto soundDoseManager = mSoundDoseManager.promote(); | 
|  | 366 | if (soundDoseManager != nullptr) { | 
|  | 367 | soundDoseManager->setUseFrameworkMel(useFrameworkMel); | 
|  | 368 | } | 
|  | 369 | return binder::Status::ok(); | 
|  | 370 | } | 
|  | 371 |  | 
|  | 372 | binder::Status SoundDoseManager::SoundDose::forceComputeCsdOnAllDevices( | 
|  | 373 | bool computeCsdOnAllDevices) { | 
|  | 374 | ALOGV("%s", __func__); | 
|  | 375 | auto soundDoseManager = mSoundDoseManager.promote(); | 
|  | 376 | if (soundDoseManager != nullptr) { | 
|  | 377 | soundDoseManager->setComputeCsdOnAllDevices(computeCsdOnAllDevices); | 
|  | 378 | } | 
|  | 379 | return binder::Status::ok(); | 
|  | 380 | } | 
|  | 381 |  | 
| Vlad Popa | 95ee3ca | 2023-03-20 19:29:38 +0000 | [diff] [blame] | 382 | binder::Status SoundDoseManager::SoundDose::isSoundDoseHalSupported(bool* value) { | 
|  | 383 | ALOGV("%s", __func__); | 
|  | 384 | *value = false; | 
|  | 385 | auto soundDoseManager = mSoundDoseManager.promote(); | 
|  | 386 | if (soundDoseManager != nullptr) { | 
|  | 387 | *value = soundDoseManager->isSoundDoseHalSupported(); | 
|  | 388 | } | 
|  | 389 | return binder::Status::ok(); | 
|  | 390 | } | 
|  | 391 |  | 
| Vlad Popa | 58e72dc | 2023-02-01 13:18:40 +0100 | [diff] [blame] | 392 | void SoundDoseManager::updateAttenuation(float attenuationDB, audio_devices_t deviceType) { | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 393 | const std::lock_guard _l(mLock); | 
| Vlad Popa | eafa048 | 2023-02-23 14:35:56 +0100 | [diff] [blame] | 394 | ALOGV("%s: updating MEL processor attenuation for device type %d to %f", | 
| Vlad Popa | 58e72dc | 2023-02-01 13:18:40 +0100 | [diff] [blame] | 395 | __func__, deviceType, attenuationDB); | 
|  | 396 | mMelAttenuationDB[deviceType] = attenuationDB; | 
|  | 397 | for (const auto& mp : mActiveProcessors) { | 
|  | 398 | auto melProcessor = mp.second.promote(); | 
|  | 399 | if (melProcessor != nullptr) { | 
|  | 400 | auto deviceId = melProcessor->getDeviceId(); | 
| Vlad Popa | 197faf8 | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 401 | const auto deviceTypeIt = mActiveDeviceTypes.find(deviceId); | 
|  | 402 | if (deviceTypeIt != mActiveDeviceTypes.end() && | 
|  | 403 | deviceTypeIt->second == deviceType) { | 
| Vlad Popa | eafa048 | 2023-02-23 14:35:56 +0100 | [diff] [blame] | 404 | ALOGV("%s: set attenuation for deviceId %d to %f", | 
| Vlad Popa | 58e72dc | 2023-02-01 13:18:40 +0100 | [diff] [blame] | 405 | __func__, deviceId, attenuationDB); | 
|  | 406 | melProcessor->setAttenuation(attenuationDB); | 
|  | 407 | } | 
|  | 408 | } | 
|  | 409 | } | 
|  | 410 | } | 
|  | 411 |  | 
| Vlad Popa | 617bbf0 | 2023-04-24 19:10:36 +0200 | [diff] [blame] | 412 | void SoundDoseManager::setCsdEnabled(bool enabled) { | 
| Vlad Popa | 2a06fca | 2023-02-06 16:45:45 +0100 | [diff] [blame] | 413 | ALOGV("%s",  __func__); | 
|  | 414 |  | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 415 | const std::lock_guard _l(mLock); | 
| Vlad Popa | 617bbf0 | 2023-04-24 19:10:36 +0200 | [diff] [blame] | 416 | mEnabledCsd = enabled; | 
| Vlad Popa | 2a06fca | 2023-02-06 16:45:45 +0100 | [diff] [blame] | 417 |  | 
| Vlad Popa | 2a06fca | 2023-02-06 16:45:45 +0100 | [diff] [blame] | 418 | for (auto& activeEntry : mActiveProcessors) { | 
|  | 419 | auto melProcessor = activeEntry.second.promote(); | 
|  | 420 | if (melProcessor != nullptr) { | 
| Vlad Popa | 617bbf0 | 2023-04-24 19:10:36 +0200 | [diff] [blame] | 421 | if (enabled) { | 
|  | 422 | melProcessor->resume(); | 
|  | 423 | } else { | 
|  | 424 | melProcessor->pause(); | 
|  | 425 | } | 
| Vlad Popa | 2a06fca | 2023-02-06 16:45:45 +0100 | [diff] [blame] | 426 | } | 
|  | 427 | } | 
|  | 428 | } | 
|  | 429 |  | 
| Vlad Popa | 617bbf0 | 2023-04-24 19:10:36 +0200 | [diff] [blame] | 430 | bool SoundDoseManager::isCsdEnabled() { | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 431 | const std::lock_guard _l(mLock); | 
| Vlad Popa | 617bbf0 | 2023-04-24 19:10:36 +0200 | [diff] [blame] | 432 | return mEnabledCsd; | 
| Vlad Popa | 2a06fca | 2023-02-06 16:45:45 +0100 | [diff] [blame] | 433 | } | 
|  | 434 |  | 
| Vlad Popa | 197faf8 | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 435 | void SoundDoseManager::initCachedAudioDeviceCategories( | 
|  | 436 | const std::vector<media::ISoundDose::AudioDeviceCategory>& deviceCategories) { | 
|  | 437 | ALOGV("%s", __func__); | 
|  | 438 | { | 
|  | 439 | const std::lock_guard _l(mLock); | 
|  | 440 | mBluetoothDevicesWithCsd.clear(); | 
|  | 441 | } | 
|  | 442 | for (const auto& btDeviceCategory : deviceCategories) { | 
|  | 443 | setAudioDeviceCategory(btDeviceCategory); | 
|  | 444 | } | 
|  | 445 | } | 
|  | 446 |  | 
|  | 447 | void SoundDoseManager::setAudioDeviceCategory( | 
|  | 448 | const media::ISoundDose::AudioDeviceCategory& audioDevice) { | 
|  | 449 | ALOGV("%s: set BT audio device type with address %s to headphone %d", __func__, | 
|  | 450 | audioDevice.address.c_str(), audioDevice.csdCompatible); | 
|  | 451 |  | 
|  | 452 | std::vector<audio_port_handle_t> devicesToStart; | 
|  | 453 | std::vector<audio_port_handle_t> devicesToStop; | 
|  | 454 | { | 
|  | 455 | const std::lock_guard _l(mLock); | 
|  | 456 | const auto deviceIt = mBluetoothDevicesWithCsd.find( | 
|  | 457 | std::make_pair(audioDevice.address, | 
|  | 458 | static_cast<audio_devices_t>(audioDevice.internalAudioType))); | 
|  | 459 | if (deviceIt != mBluetoothDevicesWithCsd.end()) { | 
|  | 460 | deviceIt->second = audioDevice.csdCompatible; | 
|  | 461 | } else { | 
|  | 462 | mBluetoothDevicesWithCsd.emplace( | 
|  | 463 | std::make_pair(audioDevice.address, | 
|  | 464 | static_cast<audio_devices_t>(audioDevice.internalAudioType)), | 
|  | 465 | audioDevice.csdCompatible); | 
|  | 466 | } | 
|  | 467 |  | 
|  | 468 | for (const auto &activeDevice: mActiveDevices) { | 
|  | 469 | if (activeDevice.first.address() == audioDevice.address && | 
|  | 470 | activeDevice.first.mType == | 
|  | 471 | static_cast<audio_devices_t>(audioDevice.internalAudioType)) { | 
|  | 472 | if (audioDevice.csdCompatible) { | 
|  | 473 | devicesToStart.push_back(activeDevice.second); | 
|  | 474 | } else { | 
|  | 475 | devicesToStop.push_back(activeDevice.second); | 
|  | 476 | } | 
|  | 477 | } | 
|  | 478 | } | 
|  | 479 | } | 
|  | 480 |  | 
|  | 481 | for (const auto& deviceToStart : devicesToStart) { | 
|  | 482 | mMelReporterCallback->startMelComputationForDeviceId(deviceToStart); | 
|  | 483 | } | 
|  | 484 | for (const auto& deviceToStop : devicesToStop) { | 
|  | 485 | mMelReporterCallback->stopMelComputationForDeviceId(deviceToStop); | 
|  | 486 | } | 
|  | 487 | } | 
|  | 488 |  | 
|  | 489 | bool SoundDoseManager::shouldComputeCsdForDeviceType(audio_devices_t device) { | 
|  | 490 | if (!isCsdEnabled()) { | 
|  | 491 | ALOGV("%s csd is disabled", __func__); | 
|  | 492 | return false; | 
|  | 493 | } | 
|  | 494 | if (forceComputeCsdOnAllDevices()) { | 
|  | 495 | return true; | 
|  | 496 | } | 
|  | 497 |  | 
|  | 498 | switch (device) { | 
|  | 499 | case AUDIO_DEVICE_OUT_WIRED_HEADSET: | 
|  | 500 | case AUDIO_DEVICE_OUT_WIRED_HEADPHONE: | 
| Vlad Popa | ac6389f | 2023-07-31 17:33:25 -0700 | [diff] [blame] | 501 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP: | 
| Vlad Popa | 197faf8 | 2023-07-27 18:27:59 -0700 | [diff] [blame] | 502 | case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES: | 
|  | 503 | case AUDIO_DEVICE_OUT_USB_HEADSET: | 
|  | 504 | case AUDIO_DEVICE_OUT_BLE_HEADSET: | 
|  | 505 | case AUDIO_DEVICE_OUT_BLE_BROADCAST: | 
|  | 506 | return true; | 
|  | 507 | default: | 
|  | 508 | return false; | 
|  | 509 | } | 
|  | 510 | } | 
|  | 511 |  | 
|  | 512 | bool SoundDoseManager::shouldComputeCsdForDeviceWithAddress(const audio_devices_t type, | 
|  | 513 | const std::string& deviceAddress) { | 
|  | 514 | if (!isCsdEnabled()) { | 
|  | 515 | ALOGV("%s csd is disabled", __func__); | 
|  | 516 | return false; | 
|  | 517 | } | 
|  | 518 | if (forceComputeCsdOnAllDevices()) { | 
|  | 519 | return true; | 
|  | 520 | } | 
|  | 521 |  | 
|  | 522 | if (!audio_is_ble_out_device(type) && !audio_is_a2dp_device(type)) { | 
|  | 523 | return shouldComputeCsdForDeviceType(type); | 
|  | 524 | } | 
|  | 525 |  | 
|  | 526 | const std::lock_guard _l(mLock); | 
|  | 527 | const auto deviceIt = mBluetoothDevicesWithCsd.find(std::make_pair(deviceAddress, type)); | 
|  | 528 | return deviceIt != mBluetoothDevicesWithCsd.end() && deviceIt->second; | 
|  | 529 | } | 
|  | 530 |  | 
| Vlad Popa | 9193046 | 2022-12-20 22:42:48 +0100 | [diff] [blame] | 531 | void SoundDoseManager::setUseFrameworkMel(bool useFrameworkMel) { | 
| Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 532 | // invalidate any HAL sound dose interface used | 
| Vlad Popa | 63c48dd | 2023-08-21 19:52:14 -0700 | [diff] [blame] | 533 | resetHalSoundDoseInterfaces(); | 
| Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 534 |  | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 535 | const std::lock_guard _l(mLock); | 
| Vlad Popa | 9193046 | 2022-12-20 22:42:48 +0100 | [diff] [blame] | 536 | mUseFrameworkMel = useFrameworkMel; | 
|  | 537 | } | 
|  | 538 |  | 
| Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 539 | bool SoundDoseManager::forceUseFrameworkMel() const { | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 540 | const std::lock_guard _l(mLock); | 
| Vlad Popa | 9193046 | 2022-12-20 22:42:48 +0100 | [diff] [blame] | 541 | return mUseFrameworkMel; | 
|  | 542 | } | 
|  | 543 |  | 
|  | 544 | void SoundDoseManager::setComputeCsdOnAllDevices(bool computeCsdOnAllDevices) { | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 545 | const std::lock_guard _l(mLock); | 
| Vlad Popa | 9193046 | 2022-12-20 22:42:48 +0100 | [diff] [blame] | 546 | mComputeCsdOnAllDevices = computeCsdOnAllDevices; | 
|  | 547 | } | 
|  | 548 |  | 
| Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 549 | bool SoundDoseManager::forceComputeCsdOnAllDevices() const { | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 550 | const std::lock_guard _l(mLock); | 
| Vlad Popa | 9193046 | 2022-12-20 22:42:48 +0100 | [diff] [blame] | 551 | return mComputeCsdOnAllDevices; | 
|  | 552 | } | 
|  | 553 |  | 
| Vlad Popa | 95ee3ca | 2023-03-20 19:29:38 +0000 | [diff] [blame] | 554 | bool SoundDoseManager::isSoundDoseHalSupported() const { | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 555 | { | 
|  | 556 | const std::lock_guard _l(mLock); | 
|  | 557 | if (!mEnabledCsd) return false; | 
| Vlad Popa | 95ee3ca | 2023-03-20 19:29:38 +0000 | [diff] [blame] | 558 | } | 
|  | 559 |  | 
| Vlad Popa | 63c48dd | 2023-08-21 19:52:14 -0700 | [diff] [blame] | 560 | return useHalSoundDose(); | 
| Vlad Popa | 95ee3ca | 2023-03-20 19:29:38 +0000 | [diff] [blame] | 561 | } | 
|  | 562 |  | 
| Vlad Popa | 63c48dd | 2023-08-21 19:52:14 -0700 | [diff] [blame] | 563 | bool SoundDoseManager::useHalSoundDose() const { | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 564 | const std::lock_guard _l(mLock); | 
| Vlad Popa | 63c48dd | 2023-08-21 19:52:14 -0700 | [diff] [blame] | 565 | return mHalSoundDose.size() > 0; | 
| Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 566 | } | 
|  | 567 |  | 
| Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 568 | void SoundDoseManager::resetSoundDose() { | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 569 | const std::lock_guard lock(mLock); | 
| Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 570 | mSoundDose = nullptr; | 
|  | 571 | } | 
|  | 572 |  | 
|  | 573 | void SoundDoseManager::resetCsd(float currentCsd, | 
|  | 574 | const std::vector<media::SoundDoseRecord>& records) { | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 575 | const std::lock_guard lock(mLock); | 
| Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 576 | std::vector<audio_utils::CsdRecord> resetRecords; | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 577 | resetRecords.reserve(records.size()); | 
| Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 578 | for (const auto& record : records) { | 
|  | 579 | resetRecords.emplace_back(record.timestamp, record.duration, record.value, | 
|  | 580 | record.averageMel); | 
|  | 581 | } | 
|  | 582 |  | 
|  | 583 | mMelAggregator->reset(currentCsd, resetRecords); | 
|  | 584 | } | 
|  | 585 |  | 
| Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 586 | void SoundDoseManager::onNewMelValues(const std::vector<float>& mels, size_t offset, size_t length, | 
|  | 587 | audio_port_handle_t deviceId) const { | 
| Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 588 | ALOGV("%s", __func__); | 
| Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 589 |  | 
| Vlad Popa | 2a06fca | 2023-02-06 16:45:45 +0100 | [diff] [blame] | 590 |  | 
| Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 591 | sp<media::ISoundDoseCallback> soundDoseCallback; | 
|  | 592 | std::vector<audio_utils::CsdRecord> records; | 
|  | 593 | float currentCsd; | 
|  | 594 | { | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 595 | const std::lock_guard _l(mLock); | 
| Vlad Popa | 617bbf0 | 2023-04-24 19:10:36 +0200 | [diff] [blame] | 596 | if (!mEnabledCsd) { | 
| Vlad Popa | 2a06fca | 2023-02-06 16:45:45 +0100 | [diff] [blame] | 597 | return; | 
|  | 598 | } | 
|  | 599 |  | 
| Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 600 |  | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 601 | const int64_t timestampSec = getMonotonicSecond(); | 
| Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 602 |  | 
|  | 603 | // only for internal callbacks | 
|  | 604 | records = mMelAggregator->aggregateAndAddNewMelRecord(audio_utils::MelRecord( | 
|  | 605 | deviceId, std::vector<float>(mels.begin() + offset, mels.begin() + offset + length), | 
|  | 606 | timestampSec - length)); | 
|  | 607 |  | 
|  | 608 | currentCsd = mMelAggregator->getCsd(); | 
|  | 609 | } | 
|  | 610 |  | 
|  | 611 | soundDoseCallback = getSoundDoseCallback(); | 
|  | 612 |  | 
|  | 613 | if (records.size() > 0 && soundDoseCallback != nullptr) { | 
|  | 614 | std::vector<media::SoundDoseRecord> newRecordsToReport; | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 615 | newRecordsToReport.resize(records.size()); | 
| Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 616 | for (const auto& record : records) { | 
|  | 617 | newRecordsToReport.emplace_back(csdRecordToSoundDoseRecord(record)); | 
|  | 618 | } | 
|  | 619 |  | 
|  | 620 | soundDoseCallback->onNewCsdValue(currentCsd, newRecordsToReport); | 
|  | 621 | } | 
| Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 622 | } | 
|  | 623 |  | 
| Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 624 | sp<media::ISoundDoseCallback> SoundDoseManager::getSoundDoseCallback() const { | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 625 | const std::lock_guard _l(mLock); | 
| Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 626 | if (mSoundDose == nullptr) { | 
|  | 627 | return nullptr; | 
|  | 628 | } | 
|  | 629 |  | 
|  | 630 | return mSoundDose->mSoundDoseCallback; | 
| Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 631 | } | 
|  | 632 |  | 
|  | 633 | void SoundDoseManager::onMomentaryExposure(float currentMel, audio_port_handle_t deviceId) const { | 
|  | 634 | ALOGV("%s: Momentary exposure for device %d triggered: %f MEL", __func__, deviceId, currentMel); | 
| Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 635 |  | 
| Vlad Popa | 2a06fca | 2023-02-06 16:45:45 +0100 | [diff] [blame] | 636 | { | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 637 | const std::lock_guard _l(mLock); | 
| Vlad Popa | 617bbf0 | 2023-04-24 19:10:36 +0200 | [diff] [blame] | 638 | if (!mEnabledCsd) { | 
| Vlad Popa | 2a06fca | 2023-02-06 16:45:45 +0100 | [diff] [blame] | 639 | return; | 
|  | 640 | } | 
|  | 641 | } | 
|  | 642 |  | 
| Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 643 | auto soundDoseCallback = getSoundDoseCallback(); | 
| Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 644 | if (soundDoseCallback != nullptr) { | 
| Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 645 | soundDoseCallback->onMomentaryExposure(currentMel, deviceId); | 
| Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 646 | } | 
|  | 647 | } | 
|  | 648 |  | 
| Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 649 | sp<media::ISoundDose> SoundDoseManager::getSoundDoseInterface( | 
|  | 650 | const sp<media::ISoundDoseCallback>& callback) { | 
| Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 651 | ALOGV("%s: Register ISoundDoseCallback", __func__); | 
|  | 652 |  | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 653 | const std::lock_guard _l(mLock); | 
| Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 654 | if (mSoundDose == nullptr) { | 
|  | 655 | mSoundDose = sp<SoundDose>::make(this, callback); | 
|  | 656 | } | 
|  | 657 | return mSoundDose; | 
| Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 658 | } | 
|  | 659 |  | 
| Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 660 | std::string SoundDoseManager::dump() const { | 
| Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 661 | std::string output; | 
| Vlad Popa | 2a06fca | 2023-02-06 16:45:45 +0100 | [diff] [blame] | 662 | { | 
| Vlad Popa | ffcacdc | 2023-06-10 00:24:35 +0200 | [diff] [blame] | 663 | const std::lock_guard _l(mLock); | 
| Vlad Popa | 617bbf0 | 2023-04-24 19:10:36 +0200 | [diff] [blame] | 664 | if (!mEnabledCsd) { | 
| Vlad Popa | 2a06fca | 2023-02-06 16:45:45 +0100 | [diff] [blame] | 665 | base::StringAppendF(&output, "CSD is disabled"); | 
|  | 666 | return output; | 
|  | 667 | } | 
|  | 668 | } | 
|  | 669 |  | 
| Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 670 | mMelAggregator->foreachCsd([&output](audio_utils::CsdRecord csdRecord) { | 
| Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 671 | base::StringAppendF(&output, | 
|  | 672 | "CSD %f with average MEL %f in interval [%" PRId64 ", %" PRId64 "]", | 
| Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 673 | csdRecord.value, csdRecord.averageMel, csdRecord.timestamp, | 
| Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 674 | csdRecord.timestamp + csdRecord.duration); | 
|  | 675 | base::StringAppendF(&output, "\n"); | 
|  | 676 | }); | 
|  | 677 |  | 
|  | 678 | base::StringAppendF(&output, "\nCached Mel Records:\n"); | 
| Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 679 | mMelAggregator->foreachCachedMel([&output](const audio_utils::MelRecord& melRecord) { | 
| Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 680 | base::StringAppendF(&output, "Continuous MELs for portId=%d, ", melRecord.portId); | 
|  | 681 | base::StringAppendF(&output, "starting at timestamp %" PRId64 ": ", melRecord.timestamp); | 
|  | 682 |  | 
|  | 683 | for (const auto& mel : melRecord.mels) { | 
|  | 684 | base::StringAppendF(&output, "%.2f ", mel); | 
|  | 685 | } | 
|  | 686 | base::StringAppendF(&output, "\n"); | 
|  | 687 | }); | 
|  | 688 |  | 
|  | 689 | return output; | 
|  | 690 | } | 
|  | 691 |  | 
|  | 692 | size_t SoundDoseManager::getCachedMelRecordsSize() const { | 
| Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 693 | return mMelAggregator->getCachedMelRecordsSize(); | 
| Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 694 | } | 
|  | 695 |  | 
| Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 696 | media::SoundDoseRecord SoundDoseManager::csdRecordToSoundDoseRecord( | 
|  | 697 | const audio_utils::CsdRecord& legacy) { | 
|  | 698 | media::SoundDoseRecord soundDoseRecord{}; | 
|  | 699 | soundDoseRecord.timestamp = legacy.timestamp; | 
|  | 700 | soundDoseRecord.duration = legacy.duration; | 
|  | 701 | soundDoseRecord.value = legacy.value; | 
|  | 702 | soundDoseRecord.averageMel = legacy.averageMel; | 
|  | 703 | return soundDoseRecord; | 
|  | 704 | } | 
|  | 705 |  | 
| Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 706 | }  // namespace android |