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; |
| 33 | using aidl::android::media::audio::common::AudioDeviceAddress; |
| 34 | |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 35 | namespace { |
| 36 | |
| 37 | int64_t getMonotonicSecond() { |
| 38 | struct timespec now_ts; |
| 39 | if (clock_gettime(CLOCK_MONOTONIC, &now_ts) != 0) { |
| 40 | ALOGE("%s: cannot get timestamp", __func__); |
| 41 | return -1; |
| 42 | } |
| 43 | return now_ts.tv_sec; |
| 44 | } |
| 45 | |
| 46 | } // namespace |
| 47 | |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 48 | sp<audio_utils::MelProcessor> SoundDoseManager::getOrCreateProcessorForDevice( |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 49 | audio_port_handle_t deviceId, audio_io_handle_t streamHandle, uint32_t sampleRate, |
| 50 | size_t channelCount, audio_format_t format) { |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 51 | std::lock_guard _l(mLock); |
| 52 | |
Vlad Popa | 2a06fca | 2023-02-06 16:45:45 +0100 | [diff] [blame] | 53 | if (mHalSoundDose != nullptr && !mDisableCsd) { |
Vlad Popa | 1c2f7e1 | 2023-03-28 02:08:56 +0200 | [diff] [blame] | 54 | ALOGD("%s: using HAL MEL computation, no MelProcessor needed.", __func__); |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 55 | return nullptr; |
| 56 | } |
| 57 | |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 58 | auto streamProcessor = mActiveProcessors.find(streamHandle); |
Vlad Popa | 1c2f7e1 | 2023-03-28 02:08:56 +0200 | [diff] [blame] | 59 | if (streamProcessor != mActiveProcessors.end()) { |
| 60 | auto processor = streamProcessor->second.promote(); |
| 61 | // if processor is nullptr it means it was removed by the playback |
| 62 | // thread and can be replaced in the mActiveProcessors map |
| 63 | if (processor != nullptr) { |
| 64 | ALOGV("%s: found callback for stream id %d", __func__, streamHandle); |
| 65 | const auto activeTypeIt = mActiveDeviceTypes.find(deviceId); |
| 66 | if (activeTypeIt != mActiveDeviceTypes.end()) { |
| 67 | processor->setAttenuation(mMelAttenuationDB[activeTypeIt->second]); |
| 68 | } |
| 69 | processor->setDeviceId(deviceId); |
| 70 | processor->setOutputRs2UpperBound(mRs2UpperBound); |
| 71 | return processor; |
Vlad Popa | 58e72dc | 2023-02-01 13:18:40 +0100 | [diff] [blame] | 72 | } |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 73 | } |
Vlad Popa | 1c2f7e1 | 2023-03-28 02:08:56 +0200 | [diff] [blame] | 74 | |
| 75 | ALOGV("%s: creating new callback for stream id %d", __func__, streamHandle); |
| 76 | sp<audio_utils::MelProcessor> melProcessor = sp<audio_utils::MelProcessor>::make( |
| 77 | sampleRate, channelCount, format, this, deviceId, mRs2UpperBound); |
| 78 | const auto activeTypeIt = mActiveDeviceTypes.find(deviceId); |
| 79 | if (activeTypeIt != mActiveDeviceTypes.end()) { |
| 80 | melProcessor->setAttenuation(mMelAttenuationDB[activeTypeIt->second]); |
| 81 | } |
| 82 | mActiveProcessors[streamHandle] = melProcessor; |
| 83 | return melProcessor; |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 86 | bool SoundDoseManager::setHalSoundDoseInterface(const std::shared_ptr<ISoundDose>& halSoundDose) { |
| 87 | ALOGV("%s", __func__); |
| 88 | |
| 89 | { |
| 90 | std::lock_guard _l(mLock); |
| 91 | |
| 92 | mHalSoundDose = halSoundDose; |
| 93 | if (halSoundDose == nullptr) { |
| 94 | ALOGI("%s: passed ISoundDose object is null, switching to internal CSD", __func__); |
| 95 | return false; |
| 96 | } |
| 97 | |
Vlad Popa | 4847de1 | 2023-03-16 18:30:08 +0100 | [diff] [blame] | 98 | if (!mHalSoundDose->setOutputRs2UpperBound(mRs2UpperBound).isOk()) { |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 99 | ALOGW("%s: Cannot set RS2 value for momentary exposure %f", |
| 100 | __func__, |
Vlad Popa | 4847de1 | 2023-03-16 18:30:08 +0100 | [diff] [blame] | 101 | mRs2UpperBound); |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // initialize the HAL sound dose callback lazily |
| 105 | if (mHalSoundDoseCallback == nullptr) { |
| 106 | mHalSoundDoseCallback = |
| 107 | ndk::SharedRefBase::make<HalSoundDoseCallback>(this); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | auto status = halSoundDose->registerSoundDoseCallback(mHalSoundDoseCallback); |
| 112 | if (!status.isOk()) { |
| 113 | // Not a warning since this can happen if the callback was registered before |
| 114 | ALOGI("%s: Cannot register HAL sound dose callback with status message: %s", |
| 115 | __func__, |
| 116 | status.getMessage()); |
| 117 | } |
| 118 | |
| 119 | return true; |
| 120 | } |
| 121 | |
Vlad Popa | 4847de1 | 2023-03-16 18:30:08 +0100 | [diff] [blame] | 122 | void SoundDoseManager::setOutputRs2UpperBound(float rs2Value) { |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 123 | ALOGV("%s", __func__); |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 124 | std::lock_guard _l(mLock); |
| 125 | |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 126 | if (mHalSoundDose != nullptr) { |
| 127 | // using the HAL sound dose interface |
Vlad Popa | 4847de1 | 2023-03-16 18:30:08 +0100 | [diff] [blame] | 128 | if (!mHalSoundDose->setOutputRs2UpperBound(rs2Value).isOk()) { |
Vlad Popa | 3c3995d | 2023-01-13 11:10:05 +0100 | [diff] [blame] | 129 | ALOGE("%s: Cannot set RS2 value for momentary exposure %f", __func__, rs2Value); |
| 130 | return; |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 131 | } |
Vlad Popa | 4847de1 | 2023-03-16 18:30:08 +0100 | [diff] [blame] | 132 | mRs2UpperBound = rs2Value; |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 133 | return; |
| 134 | } |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 135 | |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 136 | for (auto& streamProcessor : mActiveProcessors) { |
| 137 | sp<audio_utils::MelProcessor> processor = streamProcessor.second.promote(); |
| 138 | if (processor != nullptr) { |
Vlad Popa | 4847de1 | 2023-03-16 18:30:08 +0100 | [diff] [blame] | 139 | status_t result = processor->setOutputRs2UpperBound(rs2Value); |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 140 | if (result != NO_ERROR) { |
Vlad Popa | 4847de1 | 2023-03-16 18:30:08 +0100 | [diff] [blame] | 141 | 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] | 142 | streamProcessor.first); |
Vlad Popa | 3c3995d | 2023-01-13 11:10:05 +0100 | [diff] [blame] | 143 | return; |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 144 | } |
Vlad Popa | 4847de1 | 2023-03-16 18:30:08 +0100 | [diff] [blame] | 145 | mRs2UpperBound = rs2Value; |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 150 | void SoundDoseManager::removeStreamProcessor(audio_io_handle_t streamHandle) { |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 151 | std::lock_guard _l(mLock); |
| 152 | auto callbackToRemove = mActiveProcessors.find(streamHandle); |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 153 | if (callbackToRemove != mActiveProcessors.end()) { |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 154 | mActiveProcessors.erase(callbackToRemove); |
| 155 | } |
| 156 | } |
| 157 | |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 158 | audio_port_handle_t SoundDoseManager::getIdForAudioDevice(const AudioDevice& audioDevice) const { |
| 159 | std::lock_guard _l(mLock); |
| 160 | |
| 161 | audio_devices_t type; |
| 162 | std::string address; |
| 163 | auto result = aidl::android::aidl2legacy_AudioDevice_audio_device( |
| 164 | audioDevice, &type, &address); |
| 165 | if (result != NO_ERROR) { |
| 166 | ALOGE("%s: could not convert from AudioDevice to AudioDeviceTypeAddr", __func__); |
| 167 | return AUDIO_PORT_HANDLE_NONE; |
| 168 | } |
| 169 | |
| 170 | auto adt = AudioDeviceTypeAddr(type, address); |
| 171 | auto deviceIt = mActiveDevices.find(adt); |
| 172 | if (deviceIt == mActiveDevices.end()) { |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 173 | 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] | 174 | return AUDIO_PORT_HANDLE_NONE; |
| 175 | } |
| 176 | return deviceIt->second; |
| 177 | } |
| 178 | |
| 179 | void SoundDoseManager::mapAddressToDeviceId(const AudioDeviceTypeAddr& adt, |
| 180 | const audio_port_handle_t deviceId) { |
| 181 | std::lock_guard _l(mLock); |
Vlad Popa | eafa048 | 2023-02-23 14:35:56 +0100 | [diff] [blame] | 182 | 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] | 183 | mActiveDevices[adt] = deviceId; |
Vlad Popa | 58e72dc | 2023-02-01 13:18:40 +0100 | [diff] [blame] | 184 | mActiveDeviceTypes[deviceId] = adt.mType; |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | void SoundDoseManager::clearMapDeviceIdEntries(audio_port_handle_t deviceId) { |
| 188 | std::lock_guard _l(mLock); |
| 189 | for (auto activeDevice = mActiveDevices.begin(); activeDevice != mActiveDevices.end();) { |
| 190 | if (activeDevice->second == deviceId) { |
Vlad Popa | eafa048 | 2023-02-23 14:35:56 +0100 | [diff] [blame] | 191 | ALOGI("%s: clear mapping type: %d to deviceId: %d", |
| 192 | __func__, activeDevice->first.mType, deviceId); |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 193 | activeDevice = mActiveDevices.erase(activeDevice); |
| 194 | continue; |
| 195 | } |
| 196 | ++activeDevice; |
| 197 | } |
Vlad Popa | 58e72dc | 2023-02-01 13:18:40 +0100 | [diff] [blame] | 198 | mActiveDeviceTypes.erase(deviceId); |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | ndk::ScopedAStatus SoundDoseManager::HalSoundDoseCallback::onMomentaryExposureWarning( |
| 202 | float in_currentDbA, const AudioDevice& in_audioDevice) { |
| 203 | auto soundDoseManager = mSoundDoseManager.promote(); |
| 204 | if (soundDoseManager == nullptr) { |
| 205 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 206 | } |
| 207 | |
Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 208 | std::shared_ptr<ISoundDose> halSoundDose; |
| 209 | soundDoseManager->getHalSoundDose(&halSoundDose); |
| 210 | if(halSoundDose == nullptr) { |
| 211 | ALOGW("%s: HAL sound dose interface deactivated. Ignoring", __func__); |
| 212 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 213 | } |
| 214 | |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 215 | auto id = soundDoseManager->getIdForAudioDevice(in_audioDevice); |
| 216 | if (id == AUDIO_PORT_HANDLE_NONE) { |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 217 | 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] | 218 | __func__, in_audioDevice.type.type, |
Vlad Popa | 69fbbee | 2023-04-25 12:23:24 +0200 | [diff] [blame^] | 219 | in_audioDevice.address.toString().c_str()); |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 220 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 221 | } |
| 222 | soundDoseManager->onMomentaryExposure(in_currentDbA, id); |
| 223 | |
| 224 | return ndk::ScopedAStatus::ok(); |
| 225 | } |
| 226 | |
| 227 | ndk::ScopedAStatus SoundDoseManager::HalSoundDoseCallback::onNewMelValues( |
| 228 | const ISoundDose::IHalSoundDoseCallback::MelRecord& in_melRecord, |
| 229 | const AudioDevice& in_audioDevice) { |
| 230 | auto soundDoseManager = mSoundDoseManager.promote(); |
| 231 | if (soundDoseManager == nullptr) { |
| 232 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 233 | } |
Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 234 | |
| 235 | std::shared_ptr<ISoundDose> halSoundDose; |
| 236 | soundDoseManager->getHalSoundDose(&halSoundDose); |
| 237 | if(halSoundDose == nullptr) { |
| 238 | ALOGW("%s: HAL sound dose interface deactivated. Ignoring", __func__); |
| 239 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); |
| 240 | } |
| 241 | |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 242 | auto id = soundDoseManager->getIdForAudioDevice(in_audioDevice); |
| 243 | if (id == AUDIO_PORT_HANDLE_NONE) { |
Vlad Popa | 7e81cea | 2023-01-19 16:34:16 +0100 | [diff] [blame] | 244 | 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] | 245 | __func__, in_audioDevice.type.type, |
Vlad Popa | 69fbbee | 2023-04-25 12:23:24 +0200 | [diff] [blame^] | 246 | in_audioDevice.address.toString().c_str()); |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 247 | return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); |
| 248 | } |
| 249 | // TODO: introduce timestamp in onNewMelValues callback |
| 250 | soundDoseManager->onNewMelValues(in_melRecord.melValues, 0, |
| 251 | in_melRecord.melValues.size(), id); |
| 252 | |
| 253 | return ndk::ScopedAStatus::ok(); |
| 254 | } |
| 255 | |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 256 | void SoundDoseManager::SoundDose::binderDied(__unused const wp<IBinder>& who) { |
| 257 | ALOGV("%s", __func__); |
| 258 | |
| 259 | auto soundDoseManager = mSoundDoseManager.promote(); |
| 260 | if (soundDoseManager != nullptr) { |
| 261 | soundDoseManager->resetSoundDose(); |
| 262 | } |
| 263 | } |
| 264 | |
Vlad Popa | 4847de1 | 2023-03-16 18:30:08 +0100 | [diff] [blame] | 265 | binder::Status SoundDoseManager::SoundDose::setOutputRs2UpperBound(float value) { |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 266 | ALOGV("%s", __func__); |
| 267 | auto soundDoseManager = mSoundDoseManager.promote(); |
| 268 | if (soundDoseManager != nullptr) { |
Vlad Popa | 4847de1 | 2023-03-16 18:30:08 +0100 | [diff] [blame] | 269 | soundDoseManager->setOutputRs2UpperBound(value); |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 270 | } |
| 271 | return binder::Status::ok(); |
| 272 | } |
| 273 | |
| 274 | binder::Status SoundDoseManager::SoundDose::resetCsd( |
| 275 | float currentCsd, const std::vector<media::SoundDoseRecord>& records) { |
| 276 | ALOGV("%s", __func__); |
| 277 | auto soundDoseManager = mSoundDoseManager.promote(); |
| 278 | if (soundDoseManager != nullptr) { |
| 279 | soundDoseManager->resetCsd(currentCsd, records); |
| 280 | } |
| 281 | return binder::Status::ok(); |
| 282 | } |
| 283 | |
Vlad Popa | 58e72dc | 2023-02-01 13:18:40 +0100 | [diff] [blame] | 284 | binder::Status SoundDoseManager::SoundDose::updateAttenuation(float attenuationDB, int device) { |
| 285 | ALOGV("%s", __func__); |
| 286 | auto soundDoseManager = mSoundDoseManager.promote(); |
| 287 | if (soundDoseManager != nullptr) { |
| 288 | soundDoseManager->updateAttenuation(attenuationDB, static_cast<audio_devices_t>(device)); |
| 289 | } |
| 290 | return binder::Status::ok(); |
| 291 | } |
| 292 | |
Vlad Popa | 2a06fca | 2023-02-06 16:45:45 +0100 | [diff] [blame] | 293 | binder::Status SoundDoseManager::SoundDose::disableCsd() { |
| 294 | ALOGV("%s", __func__); |
| 295 | auto soundDoseManager = mSoundDoseManager.promote(); |
| 296 | if (soundDoseManager != nullptr) { |
| 297 | soundDoseManager->disableCsd(); |
| 298 | } |
| 299 | return binder::Status::ok(); |
| 300 | } |
| 301 | |
Vlad Popa | 4847de1 | 2023-03-16 18:30:08 +0100 | [diff] [blame] | 302 | binder::Status SoundDoseManager::SoundDose::getOutputRs2UpperBound(float* value) { |
Vlad Popa | 9193046 | 2022-12-20 22:42:48 +0100 | [diff] [blame] | 303 | ALOGV("%s", __func__); |
| 304 | auto soundDoseManager = mSoundDoseManager.promote(); |
| 305 | if (soundDoseManager != nullptr) { |
| 306 | std::lock_guard _l(soundDoseManager->mLock); |
Vlad Popa | 4847de1 | 2023-03-16 18:30:08 +0100 | [diff] [blame] | 307 | *value = soundDoseManager->mRs2UpperBound; |
Vlad Popa | 9193046 | 2022-12-20 22:42:48 +0100 | [diff] [blame] | 308 | } |
| 309 | return binder::Status::ok(); |
| 310 | } |
| 311 | |
| 312 | binder::Status SoundDoseManager::SoundDose::getCsd(float* value) { |
| 313 | ALOGV("%s", __func__); |
| 314 | auto soundDoseManager = mSoundDoseManager.promote(); |
| 315 | if (soundDoseManager != nullptr) { |
| 316 | *value = soundDoseManager->mMelAggregator->getCsd(); |
| 317 | } |
| 318 | return binder::Status::ok(); |
| 319 | } |
| 320 | |
| 321 | binder::Status SoundDoseManager::SoundDose::forceUseFrameworkMel(bool useFrameworkMel) { |
| 322 | ALOGV("%s", __func__); |
| 323 | auto soundDoseManager = mSoundDoseManager.promote(); |
| 324 | if (soundDoseManager != nullptr) { |
| 325 | soundDoseManager->setUseFrameworkMel(useFrameworkMel); |
| 326 | } |
| 327 | return binder::Status::ok(); |
| 328 | } |
| 329 | |
| 330 | binder::Status SoundDoseManager::SoundDose::forceComputeCsdOnAllDevices( |
| 331 | bool computeCsdOnAllDevices) { |
| 332 | ALOGV("%s", __func__); |
| 333 | auto soundDoseManager = mSoundDoseManager.promote(); |
| 334 | if (soundDoseManager != nullptr) { |
| 335 | soundDoseManager->setComputeCsdOnAllDevices(computeCsdOnAllDevices); |
| 336 | } |
| 337 | return binder::Status::ok(); |
| 338 | } |
| 339 | |
Vlad Popa | 95ee3ca | 2023-03-20 19:29:38 +0000 | [diff] [blame] | 340 | binder::Status SoundDoseManager::SoundDose::isSoundDoseHalSupported(bool* value) { |
| 341 | ALOGV("%s", __func__); |
| 342 | *value = false; |
| 343 | auto soundDoseManager = mSoundDoseManager.promote(); |
| 344 | if (soundDoseManager != nullptr) { |
| 345 | *value = soundDoseManager->isSoundDoseHalSupported(); |
| 346 | } |
| 347 | return binder::Status::ok(); |
| 348 | } |
| 349 | |
Vlad Popa | 58e72dc | 2023-02-01 13:18:40 +0100 | [diff] [blame] | 350 | void SoundDoseManager::updateAttenuation(float attenuationDB, audio_devices_t deviceType) { |
| 351 | std::lock_guard _l(mLock); |
Vlad Popa | eafa048 | 2023-02-23 14:35:56 +0100 | [diff] [blame] | 352 | ALOGV("%s: updating MEL processor attenuation for device type %d to %f", |
Vlad Popa | 58e72dc | 2023-02-01 13:18:40 +0100 | [diff] [blame] | 353 | __func__, deviceType, attenuationDB); |
| 354 | mMelAttenuationDB[deviceType] = attenuationDB; |
| 355 | for (const auto& mp : mActiveProcessors) { |
| 356 | auto melProcessor = mp.second.promote(); |
| 357 | if (melProcessor != nullptr) { |
| 358 | auto deviceId = melProcessor->getDeviceId(); |
| 359 | if (mActiveDeviceTypes[deviceId] == deviceType) { |
Vlad Popa | eafa048 | 2023-02-23 14:35:56 +0100 | [diff] [blame] | 360 | ALOGV("%s: set attenuation for deviceId %d to %f", |
Vlad Popa | 58e72dc | 2023-02-01 13:18:40 +0100 | [diff] [blame] | 361 | __func__, deviceId, attenuationDB); |
| 362 | melProcessor->setAttenuation(attenuationDB); |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | |
Vlad Popa | 2a06fca | 2023-02-06 16:45:45 +0100 | [diff] [blame] | 368 | void SoundDoseManager::disableCsd() { |
| 369 | ALOGV("%s", __func__); |
| 370 | |
| 371 | std::lock_guard _l(mLock); |
| 372 | mDisableCsd = true; |
| 373 | |
| 374 | // Normally, there should be no active MelProcessors when this method is called |
| 375 | // We pause however every cached MelProcessor as a defensive mechanism to not |
| 376 | // have unnecessary processing |
| 377 | for (auto& activeEntry : mActiveProcessors) { |
| 378 | auto melProcessor = activeEntry.second.promote(); |
| 379 | if (melProcessor != nullptr) { |
| 380 | melProcessor->pause(); |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | bool SoundDoseManager::isCsdDisabled() { |
| 386 | std::lock_guard _l(mLock); |
| 387 | return mDisableCsd; |
| 388 | } |
| 389 | |
Vlad Popa | 9193046 | 2022-12-20 22:42:48 +0100 | [diff] [blame] | 390 | void SoundDoseManager::setUseFrameworkMel(bool useFrameworkMel) { |
Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 391 | // invalidate any HAL sound dose interface used |
| 392 | setHalSoundDoseInterface(nullptr); |
| 393 | |
Vlad Popa | 9193046 | 2022-12-20 22:42:48 +0100 | [diff] [blame] | 394 | std::lock_guard _l(mLock); |
| 395 | mUseFrameworkMel = useFrameworkMel; |
| 396 | } |
| 397 | |
Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 398 | bool SoundDoseManager::forceUseFrameworkMel() const { |
Vlad Popa | 9193046 | 2022-12-20 22:42:48 +0100 | [diff] [blame] | 399 | std::lock_guard _l(mLock); |
| 400 | return mUseFrameworkMel; |
| 401 | } |
| 402 | |
| 403 | void SoundDoseManager::setComputeCsdOnAllDevices(bool computeCsdOnAllDevices) { |
| 404 | std::lock_guard _l(mLock); |
| 405 | mComputeCsdOnAllDevices = computeCsdOnAllDevices; |
| 406 | } |
| 407 | |
Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 408 | bool SoundDoseManager::forceComputeCsdOnAllDevices() const { |
Vlad Popa | 9193046 | 2022-12-20 22:42:48 +0100 | [diff] [blame] | 409 | std::lock_guard _l(mLock); |
| 410 | return mComputeCsdOnAllDevices; |
| 411 | } |
| 412 | |
Vlad Popa | 95ee3ca | 2023-03-20 19:29:38 +0000 | [diff] [blame] | 413 | bool SoundDoseManager::isSoundDoseHalSupported() const { |
| 414 | if (mDisableCsd) { |
| 415 | return false; |
| 416 | } |
| 417 | |
| 418 | std::shared_ptr<ISoundDose> halSoundDose; |
| 419 | getHalSoundDose(&halSoundDose); |
| 420 | if (mHalSoundDose == nullptr) { |
| 421 | return false; |
| 422 | } |
| 423 | return true; |
| 424 | } |
| 425 | |
Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 426 | void SoundDoseManager::getHalSoundDose(std::shared_ptr<ISoundDose>* halSoundDose) const { |
| 427 | std::lock_guard _l(mLock); |
| 428 | *halSoundDose = mHalSoundDose; |
| 429 | } |
| 430 | |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 431 | void SoundDoseManager::resetSoundDose() { |
| 432 | std::lock_guard lock(mLock); |
| 433 | mSoundDose = nullptr; |
| 434 | } |
| 435 | |
| 436 | void SoundDoseManager::resetCsd(float currentCsd, |
| 437 | const std::vector<media::SoundDoseRecord>& records) { |
| 438 | std::lock_guard lock(mLock); |
| 439 | std::vector<audio_utils::CsdRecord> resetRecords; |
| 440 | for (const auto& record : records) { |
| 441 | resetRecords.emplace_back(record.timestamp, record.duration, record.value, |
| 442 | record.averageMel); |
| 443 | } |
| 444 | |
| 445 | mMelAggregator->reset(currentCsd, resetRecords); |
| 446 | } |
| 447 | |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 448 | void SoundDoseManager::onNewMelValues(const std::vector<float>& mels, size_t offset, size_t length, |
| 449 | audio_port_handle_t deviceId) const { |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 450 | ALOGV("%s", __func__); |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 451 | |
Vlad Popa | 2a06fca | 2023-02-06 16:45:45 +0100 | [diff] [blame] | 452 | |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 453 | sp<media::ISoundDoseCallback> soundDoseCallback; |
| 454 | std::vector<audio_utils::CsdRecord> records; |
| 455 | float currentCsd; |
| 456 | { |
| 457 | std::lock_guard _l(mLock); |
Vlad Popa | 2a06fca | 2023-02-06 16:45:45 +0100 | [diff] [blame] | 458 | if (mDisableCsd) { |
| 459 | return; |
| 460 | } |
| 461 | |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 462 | |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 463 | int64_t timestampSec = getMonotonicSecond(); |
| 464 | |
| 465 | // only for internal callbacks |
| 466 | records = mMelAggregator->aggregateAndAddNewMelRecord(audio_utils::MelRecord( |
| 467 | deviceId, std::vector<float>(mels.begin() + offset, mels.begin() + offset + length), |
| 468 | timestampSec - length)); |
| 469 | |
| 470 | currentCsd = mMelAggregator->getCsd(); |
| 471 | } |
| 472 | |
| 473 | soundDoseCallback = getSoundDoseCallback(); |
| 474 | |
| 475 | if (records.size() > 0 && soundDoseCallback != nullptr) { |
| 476 | std::vector<media::SoundDoseRecord> newRecordsToReport; |
| 477 | for (const auto& record : records) { |
| 478 | newRecordsToReport.emplace_back(csdRecordToSoundDoseRecord(record)); |
| 479 | } |
| 480 | |
| 481 | soundDoseCallback->onNewCsdValue(currentCsd, newRecordsToReport); |
| 482 | } |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 483 | } |
| 484 | |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 485 | sp<media::ISoundDoseCallback> SoundDoseManager::getSoundDoseCallback() const { |
| 486 | std::lock_guard _l(mLock); |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 487 | if (mSoundDose == nullptr) { |
| 488 | return nullptr; |
| 489 | } |
| 490 | |
| 491 | return mSoundDose->mSoundDoseCallback; |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | void SoundDoseManager::onMomentaryExposure(float currentMel, audio_port_handle_t deviceId) const { |
| 495 | 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] | 496 | |
Vlad Popa | 2a06fca | 2023-02-06 16:45:45 +0100 | [diff] [blame] | 497 | { |
| 498 | std::lock_guard _l(mLock); |
| 499 | if (mDisableCsd) { |
| 500 | return; |
| 501 | } |
| 502 | } |
| 503 | |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 504 | auto soundDoseCallback = getSoundDoseCallback(); |
Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 505 | if (soundDoseCallback != nullptr) { |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 506 | soundDoseCallback->onMomentaryExposure(currentMel, deviceId); |
Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 510 | sp<media::ISoundDose> SoundDoseManager::getSoundDoseInterface( |
| 511 | const sp<media::ISoundDoseCallback>& callback) { |
Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 512 | ALOGV("%s: Register ISoundDoseCallback", __func__); |
| 513 | |
| 514 | std::lock_guard _l(mLock); |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 515 | if (mSoundDose == nullptr) { |
| 516 | mSoundDose = sp<SoundDose>::make(this, callback); |
| 517 | } |
| 518 | return mSoundDose; |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 519 | } |
| 520 | |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 521 | std::string SoundDoseManager::dump() const { |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 522 | std::string output; |
Vlad Popa | 2a06fca | 2023-02-06 16:45:45 +0100 | [diff] [blame] | 523 | { |
| 524 | std::lock_guard _l(mLock); |
| 525 | if (mDisableCsd) { |
| 526 | base::StringAppendF(&output, "CSD is disabled"); |
| 527 | return output; |
| 528 | } |
| 529 | } |
| 530 | |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 531 | mMelAggregator->foreachCsd([&output](audio_utils::CsdRecord csdRecord) { |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 532 | base::StringAppendF(&output, |
| 533 | "CSD %f with average MEL %f in interval [%" PRId64 ", %" PRId64 "]", |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 534 | csdRecord.value, csdRecord.averageMel, csdRecord.timestamp, |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 535 | csdRecord.timestamp + csdRecord.duration); |
| 536 | base::StringAppendF(&output, "\n"); |
| 537 | }); |
| 538 | |
| 539 | base::StringAppendF(&output, "\nCached Mel Records:\n"); |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 540 | mMelAggregator->foreachCachedMel([&output](const audio_utils::MelRecord& melRecord) { |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 541 | base::StringAppendF(&output, "Continuous MELs for portId=%d, ", melRecord.portId); |
| 542 | base::StringAppendF(&output, "starting at timestamp %" PRId64 ": ", melRecord.timestamp); |
| 543 | |
| 544 | for (const auto& mel : melRecord.mels) { |
| 545 | base::StringAppendF(&output, "%.2f ", mel); |
| 546 | } |
| 547 | base::StringAppendF(&output, "\n"); |
| 548 | }); |
| 549 | |
| 550 | return output; |
| 551 | } |
| 552 | |
| 553 | size_t SoundDoseManager::getCachedMelRecordsSize() const { |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 554 | return mMelAggregator->getCachedMelRecordsSize(); |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 555 | } |
| 556 | |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 557 | media::SoundDoseRecord SoundDoseManager::csdRecordToSoundDoseRecord( |
| 558 | const audio_utils::CsdRecord& legacy) { |
| 559 | media::SoundDoseRecord soundDoseRecord{}; |
| 560 | soundDoseRecord.timestamp = legacy.timestamp; |
| 561 | soundDoseRecord.duration = legacy.duration; |
| 562 | soundDoseRecord.value = legacy.value; |
| 563 | soundDoseRecord.averageMel = legacy.averageMel; |
| 564 | return soundDoseRecord; |
| 565 | } |
| 566 | |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 567 | } // namespace android |