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