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