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 | |
| 23 | #include <android-base/stringprintf.h> |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 24 | #include <time.h> |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame^] | 25 | #include <utils/Log.h> |
| 26 | #include <cinttypes> |
| 27 | #include "android/media/SoundDoseRecord.h" |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | namespace { |
| 32 | |
| 33 | int64_t getMonotonicSecond() { |
| 34 | struct timespec now_ts; |
| 35 | if (clock_gettime(CLOCK_MONOTONIC, &now_ts) != 0) { |
| 36 | ALOGE("%s: cannot get timestamp", __func__); |
| 37 | return -1; |
| 38 | } |
| 39 | return now_ts.tv_sec; |
| 40 | } |
| 41 | |
| 42 | } // namespace |
| 43 | |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 44 | sp<audio_utils::MelProcessor> SoundDoseManager::getOrCreateProcessorForDevice( |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame^] | 45 | audio_port_handle_t deviceId, audio_io_handle_t streamHandle, uint32_t sampleRate, |
| 46 | size_t channelCount, audio_format_t format) { |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 47 | std::lock_guard _l(mLock); |
| 48 | |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 49 | auto streamProcessor = mActiveProcessors.find(streamHandle); |
| 50 | sp<audio_utils::MelProcessor> processor; |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame^] | 51 | if (streamProcessor != mActiveProcessors.end() && |
| 52 | (processor = streamProcessor->second.promote())) { |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 53 | ALOGV("%s: found callback for stream %d", __func__, streamHandle); |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 54 | processor->setDeviceId(deviceId); |
| 55 | return processor; |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 56 | } else { |
| 57 | ALOGV("%s: creating new callback for device %d", __func__, streamHandle); |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame^] | 58 | sp<audio_utils::MelProcessor> melProcessor = sp<audio_utils::MelProcessor>::make( |
| 59 | sampleRate, channelCount, format, *this, deviceId, mRs2Value); |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 60 | mActiveProcessors[streamHandle] = melProcessor; |
| 61 | return melProcessor; |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame^] | 65 | void SoundDoseManager::setOutputRs2(float rs2Value) { |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 66 | ALOGV("%s", __func__); |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 67 | std::lock_guard _l(mLock); |
| 68 | |
| 69 | for (auto& streamProcessor : mActiveProcessors) { |
| 70 | sp<audio_utils::MelProcessor> processor = streamProcessor.second.promote(); |
| 71 | if (processor != nullptr) { |
| 72 | status_t result = processor->setOutputRs2(rs2Value); |
| 73 | if (result != NO_ERROR) { |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame^] | 74 | ALOGW("%s: could not set RS2 value %f for stream %d", __func__, rs2Value, |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 75 | streamProcessor.first); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame^] | 81 | void SoundDoseManager::removeStreamProcessor(audio_io_handle_t streamHandle) { |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 82 | std::lock_guard _l(mLock); |
| 83 | auto callbackToRemove = mActiveProcessors.find(streamHandle); |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame^] | 84 | if (callbackToRemove != mActiveProcessors.end()) { |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 85 | mActiveProcessors.erase(callbackToRemove); |
| 86 | } |
| 87 | } |
| 88 | |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame^] | 89 | void SoundDoseManager::onNewMelValues(const std::vector<float>& mels, size_t offset, size_t length, |
| 90 | audio_port_handle_t deviceId) const { |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 91 | ALOGV("%s", __func__); |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 92 | |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame^] | 93 | sp<media::ISoundDoseCallback> soundDoseCallback; |
| 94 | std::vector<audio_utils::CsdRecord> records; |
| 95 | float currentCsd; |
| 96 | { |
| 97 | std::lock_guard _l(mLock); |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 98 | |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame^] | 99 | int64_t timestampSec = getMonotonicSecond(); |
| 100 | |
| 101 | // only for internal callbacks |
| 102 | records = mMelAggregator->aggregateAndAddNewMelRecord(audio_utils::MelRecord( |
| 103 | deviceId, std::vector<float>(mels.begin() + offset, mels.begin() + offset + length), |
| 104 | timestampSec - length)); |
| 105 | |
| 106 | currentCsd = mMelAggregator->getCsd(); |
| 107 | } |
| 108 | |
| 109 | soundDoseCallback = getSoundDoseCallback(); |
| 110 | |
| 111 | if (records.size() > 0 && soundDoseCallback != nullptr) { |
| 112 | std::vector<media::SoundDoseRecord> newRecordsToReport; |
| 113 | for (const auto& record : records) { |
| 114 | newRecordsToReport.emplace_back(csdRecordToSoundDoseRecord(record)); |
| 115 | } |
| 116 | |
| 117 | soundDoseCallback->onNewCsdValue(currentCsd, newRecordsToReport); |
| 118 | } |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame^] | 121 | sp<media::ISoundDoseCallback> SoundDoseManager::getSoundDoseCallback() const { |
| 122 | std::lock_guard _l(mLock); |
| 123 | return mSoundDoseCallback; |
| 124 | } |
| 125 | |
| 126 | void SoundDoseManager::onMomentaryExposure(float currentMel, audio_port_handle_t deviceId) const { |
| 127 | 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] | 128 | |
| 129 | sp<media::ISoundDoseCallback> soundDoseCallback; |
| 130 | { |
| 131 | std::lock_guard _l(mLock); |
| 132 | soundDoseCallback = mSoundDoseCallback; |
| 133 | } |
| 134 | |
| 135 | if (soundDoseCallback != nullptr) { |
| 136 | mSoundDoseCallback->onMomentaryExposure(currentMel, deviceId); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | void SoundDoseManager::registerSoundDoseCallback(const sp<media::ISoundDoseCallback>& callback) { |
| 141 | ALOGV("%s: Register ISoundDoseCallback", __func__); |
| 142 | |
| 143 | std::lock_guard _l(mLock); |
| 144 | mSoundDoseCallback = callback; |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 145 | } |
| 146 | |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame^] | 147 | std::string SoundDoseManager::dump() const { |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 148 | std::string output; |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 149 | mMelAggregator->foreachCsd([&output](audio_utils::CsdRecord csdRecord) { |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 150 | base::StringAppendF(&output, |
| 151 | "CSD %f with average MEL %f in interval [%" PRId64 ", %" PRId64 "]", |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame^] | 152 | csdRecord.value, csdRecord.averageMel, csdRecord.timestamp, |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 153 | csdRecord.timestamp + csdRecord.duration); |
| 154 | base::StringAppendF(&output, "\n"); |
| 155 | }); |
| 156 | |
| 157 | base::StringAppendF(&output, "\nCached Mel Records:\n"); |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 158 | mMelAggregator->foreachCachedMel([&output](const audio_utils::MelRecord& melRecord) { |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 159 | base::StringAppendF(&output, "Continuous MELs for portId=%d, ", melRecord.portId); |
| 160 | base::StringAppendF(&output, "starting at timestamp %" PRId64 ": ", melRecord.timestamp); |
| 161 | |
| 162 | for (const auto& mel : melRecord.mels) { |
| 163 | base::StringAppendF(&output, "%.2f ", mel); |
| 164 | } |
| 165 | base::StringAppendF(&output, "\n"); |
| 166 | }); |
| 167 | |
| 168 | return output; |
| 169 | } |
| 170 | |
| 171 | size_t SoundDoseManager::getCachedMelRecordsSize() const { |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 172 | return mMelAggregator->getCachedMelRecordsSize(); |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 173 | } |
| 174 | |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame^] | 175 | media::SoundDoseRecord SoundDoseManager::csdRecordToSoundDoseRecord( |
| 176 | const audio_utils::CsdRecord& legacy) { |
| 177 | media::SoundDoseRecord soundDoseRecord{}; |
| 178 | soundDoseRecord.timestamp = legacy.timestamp; |
| 179 | soundDoseRecord.duration = legacy.duration; |
| 180 | soundDoseRecord.value = legacy.value; |
| 181 | soundDoseRecord.averageMel = legacy.averageMel; |
| 182 | return soundDoseRecord; |
| 183 | } |
| 184 | |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 185 | } // namespace android |