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