blob: 46f310c370622cd2c16510ded8ced2d8fba9b993 [file] [log] [blame]
Vlad Popa2900c0a2022-10-24 13:38:00 +02001/*
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 Popa2900c0a2022-10-24 13:38:00 +020024#include <time.h>
Vlad Popa4defd0b2022-11-06 14:22:31 +010025#include <utils/Log.h>
26#include <cinttypes>
27#include "android/media/SoundDoseRecord.h"
Vlad Popa2900c0a2022-10-24 13:38:00 +020028
29namespace android {
30
31namespace {
32
33int64_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 Popaf09e93f2022-10-31 16:27:12 +010044sp<audio_utils::MelProcessor> SoundDoseManager::getOrCreateProcessorForDevice(
Vlad Popa4defd0b2022-11-06 14:22:31 +010045 audio_port_handle_t deviceId, audio_io_handle_t streamHandle, uint32_t sampleRate,
46 size_t channelCount, audio_format_t format) {
Vlad Popa2900c0a2022-10-24 13:38:00 +020047 std::lock_guard _l(mLock);
48
Vlad Popaf09e93f2022-10-31 16:27:12 +010049 auto streamProcessor = mActiveProcessors.find(streamHandle);
50 sp<audio_utils::MelProcessor> processor;
Vlad Popa4defd0b2022-11-06 14:22:31 +010051 if (streamProcessor != mActiveProcessors.end() &&
52 (processor = streamProcessor->second.promote())) {
Vlad Popa2900c0a2022-10-24 13:38:00 +020053 ALOGV("%s: found callback for stream %d", __func__, streamHandle);
Vlad Popaf09e93f2022-10-31 16:27:12 +010054 processor->setDeviceId(deviceId);
Vlad Popae3fd1c22022-11-07 21:03:18 +010055 processor->setOutputRs2(mRs2Value);
Vlad Popaf09e93f2022-10-31 16:27:12 +010056 return processor;
Vlad Popa2900c0a2022-10-24 13:38:00 +020057 } else {
58 ALOGV("%s: creating new callback for device %d", __func__, streamHandle);
Vlad Popa4defd0b2022-11-06 14:22:31 +010059 sp<audio_utils::MelProcessor> melProcessor = sp<audio_utils::MelProcessor>::make(
60 sampleRate, channelCount, format, *this, deviceId, mRs2Value);
Vlad Popaf09e93f2022-10-31 16:27:12 +010061 mActiveProcessors[streamHandle] = melProcessor;
62 return melProcessor;
Vlad Popa2900c0a2022-10-24 13:38:00 +020063 }
64}
65
Vlad Popa4defd0b2022-11-06 14:22:31 +010066void SoundDoseManager::setOutputRs2(float rs2Value) {
Vlad Popa2900c0a2022-10-24 13:38:00 +020067 ALOGV("%s", __func__);
Vlad Popaf09e93f2022-10-31 16:27:12 +010068 std::lock_guard _l(mLock);
69
Vlad Popae3fd1c22022-11-07 21:03:18 +010070 mRs2Value = rs2Value;
71
Vlad Popaf09e93f2022-10-31 16:27:12 +010072 for (auto& streamProcessor : mActiveProcessors) {
73 sp<audio_utils::MelProcessor> processor = streamProcessor.second.promote();
74 if (processor != nullptr) {
Vlad Popae3fd1c22022-11-07 21:03:18 +010075 status_t result = processor->setOutputRs2(mRs2Value);
Vlad Popaf09e93f2022-10-31 16:27:12 +010076 if (result != NO_ERROR) {
Vlad Popae3fd1c22022-11-07 21:03:18 +010077 ALOGW("%s: could not set RS2 value %f for stream %d", __func__, mRs2Value,
Vlad Popaf09e93f2022-10-31 16:27:12 +010078 streamProcessor.first);
79 }
80 }
81 }
82}
83
Vlad Popa4defd0b2022-11-06 14:22:31 +010084void SoundDoseManager::removeStreamProcessor(audio_io_handle_t streamHandle) {
Vlad Popaf09e93f2022-10-31 16:27:12 +010085 std::lock_guard _l(mLock);
86 auto callbackToRemove = mActiveProcessors.find(streamHandle);
Vlad Popa4defd0b2022-11-06 14:22:31 +010087 if (callbackToRemove != mActiveProcessors.end()) {
Vlad Popaf09e93f2022-10-31 16:27:12 +010088 mActiveProcessors.erase(callbackToRemove);
89 }
90}
91
Vlad Popae3fd1c22022-11-07 21:03:18 +010092void SoundDoseManager::SoundDose::binderDied(__unused const wp<IBinder>& who) {
93 ALOGV("%s", __func__);
94
95 auto soundDoseManager = mSoundDoseManager.promote();
96 if (soundDoseManager != nullptr) {
97 soundDoseManager->resetSoundDose();
98 }
99}
100
101binder::Status SoundDoseManager::SoundDose::setOutputRs2(float value) {
102 ALOGV("%s", __func__);
103 auto soundDoseManager = mSoundDoseManager.promote();
104 if (soundDoseManager != nullptr) {
105 soundDoseManager->setOutputRs2(value);
106 }
107 return binder::Status::ok();
108}
109
110binder::Status SoundDoseManager::SoundDose::resetCsd(
111 float currentCsd, const std::vector<media::SoundDoseRecord>& records) {
112 ALOGV("%s", __func__);
113 auto soundDoseManager = mSoundDoseManager.promote();
114 if (soundDoseManager != nullptr) {
115 soundDoseManager->resetCsd(currentCsd, records);
116 }
117 return binder::Status::ok();
118}
119
120void SoundDoseManager::resetSoundDose() {
121 std::lock_guard lock(mLock);
122 mSoundDose = nullptr;
123}
124
125void SoundDoseManager::resetCsd(float currentCsd,
126 const std::vector<media::SoundDoseRecord>& records) {
127 std::lock_guard lock(mLock);
128 std::vector<audio_utils::CsdRecord> resetRecords;
129 for (const auto& record : records) {
130 resetRecords.emplace_back(record.timestamp, record.duration, record.value,
131 record.averageMel);
132 }
133
134 mMelAggregator->reset(currentCsd, resetRecords);
135}
136
Vlad Popa4defd0b2022-11-06 14:22:31 +0100137void SoundDoseManager::onNewMelValues(const std::vector<float>& mels, size_t offset, size_t length,
138 audio_port_handle_t deviceId) const {
Vlad Popaf09e93f2022-10-31 16:27:12 +0100139 ALOGV("%s", __func__);
Vlad Popa2900c0a2022-10-24 13:38:00 +0200140
Vlad Popa4defd0b2022-11-06 14:22:31 +0100141 sp<media::ISoundDoseCallback> soundDoseCallback;
142 std::vector<audio_utils::CsdRecord> records;
143 float currentCsd;
144 {
145 std::lock_guard _l(mLock);
Vlad Popa2900c0a2022-10-24 13:38:00 +0200146
Vlad Popa4defd0b2022-11-06 14:22:31 +0100147 int64_t timestampSec = getMonotonicSecond();
148
149 // only for internal callbacks
150 records = mMelAggregator->aggregateAndAddNewMelRecord(audio_utils::MelRecord(
151 deviceId, std::vector<float>(mels.begin() + offset, mels.begin() + offset + length),
152 timestampSec - length));
153
154 currentCsd = mMelAggregator->getCsd();
155 }
156
157 soundDoseCallback = getSoundDoseCallback();
158
159 if (records.size() > 0 && soundDoseCallback != nullptr) {
160 std::vector<media::SoundDoseRecord> newRecordsToReport;
161 for (const auto& record : records) {
162 newRecordsToReport.emplace_back(csdRecordToSoundDoseRecord(record));
163 }
164
165 soundDoseCallback->onNewCsdValue(currentCsd, newRecordsToReport);
166 }
Vlad Popa2900c0a2022-10-24 13:38:00 +0200167}
168
Vlad Popa4defd0b2022-11-06 14:22:31 +0100169sp<media::ISoundDoseCallback> SoundDoseManager::getSoundDoseCallback() const {
170 std::lock_guard _l(mLock);
Vlad Popae3fd1c22022-11-07 21:03:18 +0100171 if (mSoundDose == nullptr) {
172 return nullptr;
173 }
174
175 return mSoundDose->mSoundDoseCallback;
Vlad Popa4defd0b2022-11-06 14:22:31 +0100176}
177
178void SoundDoseManager::onMomentaryExposure(float currentMel, audio_port_handle_t deviceId) const {
179 ALOGV("%s: Momentary exposure for device %d triggered: %f MEL", __func__, deviceId, currentMel);
Vlad Popa63f047e2022-11-05 14:09:19 +0100180
Vlad Popae3fd1c22022-11-07 21:03:18 +0100181 auto soundDoseCallback = getSoundDoseCallback();
Vlad Popa63f047e2022-11-05 14:09:19 +0100182 if (soundDoseCallback != nullptr) {
Vlad Popae3fd1c22022-11-07 21:03:18 +0100183 soundDoseCallback->onMomentaryExposure(currentMel, deviceId);
Vlad Popa63f047e2022-11-05 14:09:19 +0100184 }
185}
186
Vlad Popae3fd1c22022-11-07 21:03:18 +0100187sp<media::ISoundDose> SoundDoseManager::getSoundDoseInterface(
188 const sp<media::ISoundDoseCallback>& callback) {
Vlad Popa63f047e2022-11-05 14:09:19 +0100189 ALOGV("%s: Register ISoundDoseCallback", __func__);
190
191 std::lock_guard _l(mLock);
Vlad Popae3fd1c22022-11-07 21:03:18 +0100192 if (mSoundDose == nullptr) {
193 mSoundDose = sp<SoundDose>::make(this, callback);
194 }
195 return mSoundDose;
Vlad Popaf09e93f2022-10-31 16:27:12 +0100196}
197
Vlad Popa4defd0b2022-11-06 14:22:31 +0100198std::string SoundDoseManager::dump() const {
Vlad Popa2900c0a2022-10-24 13:38:00 +0200199 std::string output;
Vlad Popaf09e93f2022-10-31 16:27:12 +0100200 mMelAggregator->foreachCsd([&output](audio_utils::CsdRecord csdRecord) {
Vlad Popa2900c0a2022-10-24 13:38:00 +0200201 base::StringAppendF(&output,
202 "CSD %f with average MEL %f in interval [%" PRId64 ", %" PRId64 "]",
Vlad Popa4defd0b2022-11-06 14:22:31 +0100203 csdRecord.value, csdRecord.averageMel, csdRecord.timestamp,
Vlad Popa2900c0a2022-10-24 13:38:00 +0200204 csdRecord.timestamp + csdRecord.duration);
205 base::StringAppendF(&output, "\n");
206 });
207
208 base::StringAppendF(&output, "\nCached Mel Records:\n");
Vlad Popaf09e93f2022-10-31 16:27:12 +0100209 mMelAggregator->foreachCachedMel([&output](const audio_utils::MelRecord& melRecord) {
Vlad Popa2900c0a2022-10-24 13:38:00 +0200210 base::StringAppendF(&output, "Continuous MELs for portId=%d, ", melRecord.portId);
211 base::StringAppendF(&output, "starting at timestamp %" PRId64 ": ", melRecord.timestamp);
212
213 for (const auto& mel : melRecord.mels) {
214 base::StringAppendF(&output, "%.2f ", mel);
215 }
216 base::StringAppendF(&output, "\n");
217 });
218
219 return output;
220}
221
222size_t SoundDoseManager::getCachedMelRecordsSize() const {
Vlad Popaf09e93f2022-10-31 16:27:12 +0100223 return mMelAggregator->getCachedMelRecordsSize();
Vlad Popa2900c0a2022-10-24 13:38:00 +0200224}
225
Vlad Popa4defd0b2022-11-06 14:22:31 +0100226media::SoundDoseRecord SoundDoseManager::csdRecordToSoundDoseRecord(
227 const audio_utils::CsdRecord& legacy) {
228 media::SoundDoseRecord soundDoseRecord{};
229 soundDoseRecord.timestamp = legacy.timestamp;
230 soundDoseRecord.duration = legacy.duration;
231 soundDoseRecord.value = legacy.value;
232 soundDoseRecord.averageMel = legacy.averageMel;
233 return soundDoseRecord;
234}
235
Vlad Popa2900c0a2022-10-24 13:38:00 +0200236} // namespace android