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