blob: dcaa0f16f9f26eb5d3ff9293e4805ca6521e2b35 [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
Vlad Popa1d5f0d52022-12-18 12:21:26 +010023#include "android/media/SoundDoseRecord.h"
Vlad Popa2900c0a2022-10-24 13:38:00 +020024#include <android-base/stringprintf.h>
Vlad Popa1d5f0d52022-12-18 12:21:26 +010025#include <media/AidlConversionCppNdk.h>
26#include <cinttypes>
Vlad Popa2900c0a2022-10-24 13:38:00 +020027#include <time.h>
Vlad Popa4defd0b2022-11-06 14:22:31 +010028#include <utils/Log.h>
Vlad Popa2900c0a2022-10-24 13:38:00 +020029
30namespace android {
31
Vlad Popa1d5f0d52022-12-18 12:21:26 +010032using aidl::android::media::audio::common::AudioDevice;
33using aidl::android::media::audio::common::AudioDeviceAddress;
34
Vlad Popa2900c0a2022-10-24 13:38:00 +020035namespace {
36
37int64_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 Popaf09e93f2022-10-31 16:27:12 +010048sp<audio_utils::MelProcessor> SoundDoseManager::getOrCreateProcessorForDevice(
Vlad Popa4defd0b2022-11-06 14:22:31 +010049 audio_port_handle_t deviceId, audio_io_handle_t streamHandle, uint32_t sampleRate,
50 size_t channelCount, audio_format_t format) {
Vlad Popa2900c0a2022-10-24 13:38:00 +020051 std::lock_guard _l(mLock);
52
Vlad Popa1d5f0d52022-12-18 12:21:26 +010053 if (mHalSoundDose != nullptr) {
54 ALOGW("%s: using HAL MEL computation, no MelProcessor needed.", __func__);
55 return nullptr;
56 }
57
Vlad Popaf09e93f2022-10-31 16:27:12 +010058 auto streamProcessor = mActiveProcessors.find(streamHandle);
59 sp<audio_utils::MelProcessor> processor;
Vlad Popa4defd0b2022-11-06 14:22:31 +010060 if (streamProcessor != mActiveProcessors.end() &&
61 (processor = streamProcessor->second.promote())) {
Vlad Popaeafa0482023-02-23 14:35:56 +010062 ALOGV("%s: found callback for stream id %d", __func__, streamHandle);
Vlad Popa58e72dc2023-02-01 13:18:40 +010063 const auto activeTypeIt = mActiveDeviceTypes.find(deviceId);
64 if (activeTypeIt != mActiveDeviceTypes.end()) {
65 processor->setAttenuation(mMelAttenuationDB[activeTypeIt->second]);
66 }
Vlad Popaf09e93f2022-10-31 16:27:12 +010067 processor->setDeviceId(deviceId);
Vlad Popae3fd1c22022-11-07 21:03:18 +010068 processor->setOutputRs2(mRs2Value);
Vlad Popaf09e93f2022-10-31 16:27:12 +010069 return processor;
Vlad Popa2900c0a2022-10-24 13:38:00 +020070 } else {
Vlad Popaeafa0482023-02-23 14:35:56 +010071 ALOGV("%s: creating new callback for stream id %d", __func__, streamHandle);
Vlad Popa4defd0b2022-11-06 14:22:31 +010072 sp<audio_utils::MelProcessor> melProcessor = sp<audio_utils::MelProcessor>::make(
Vlad Popa3c7a2662023-02-14 20:09:47 +010073 sampleRate, channelCount, format, this, deviceId, mRs2Value);
Vlad Popa58e72dc2023-02-01 13:18:40 +010074 const auto activeTypeIt = mActiveDeviceTypes.find(deviceId);
75 if (activeTypeIt != mActiveDeviceTypes.end()) {
76 melProcessor->setAttenuation(mMelAttenuationDB[activeTypeIt->second]);
77 }
Vlad Popaf09e93f2022-10-31 16:27:12 +010078 mActiveProcessors[streamHandle] = melProcessor;
79 return melProcessor;
Vlad Popa2900c0a2022-10-24 13:38:00 +020080 }
81}
82
Vlad Popa1d5f0d52022-12-18 12:21:26 +010083bool SoundDoseManager::setHalSoundDoseInterface(const std::shared_ptr<ISoundDose>& halSoundDose) {
84 ALOGV("%s", __func__);
85
86 {
87 std::lock_guard _l(mLock);
88
89 mHalSoundDose = halSoundDose;
90 if (halSoundDose == nullptr) {
91 ALOGI("%s: passed ISoundDose object is null, switching to internal CSD", __func__);
92 return false;
93 }
94
95 if (!mHalSoundDose->setOutputRs2(mRs2Value).isOk()) {
96 ALOGW("%s: Cannot set RS2 value for momentary exposure %f",
97 __func__,
98 mRs2Value);
99 }
100
101 // initialize the HAL sound dose callback lazily
102 if (mHalSoundDoseCallback == nullptr) {
103 mHalSoundDoseCallback =
104 ndk::SharedRefBase::make<HalSoundDoseCallback>(this);
105 }
106 }
107
108 auto status = halSoundDose->registerSoundDoseCallback(mHalSoundDoseCallback);
109 if (!status.isOk()) {
110 // Not a warning since this can happen if the callback was registered before
111 ALOGI("%s: Cannot register HAL sound dose callback with status message: %s",
112 __func__,
113 status.getMessage());
114 }
115
116 return true;
117}
118
Vlad Popa4defd0b2022-11-06 14:22:31 +0100119void SoundDoseManager::setOutputRs2(float rs2Value) {
Vlad Popa2900c0a2022-10-24 13:38:00 +0200120 ALOGV("%s", __func__);
Vlad Popaf09e93f2022-10-31 16:27:12 +0100121 std::lock_guard _l(mLock);
122
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100123 if (mHalSoundDose != nullptr) {
124 // using the HAL sound dose interface
Vlad Popa3c3995d2023-01-13 11:10:05 +0100125 if (!mHalSoundDose->setOutputRs2(rs2Value).isOk()) {
126 ALOGE("%s: Cannot set RS2 value for momentary exposure %f", __func__, rs2Value);
127 return;
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100128 }
Vlad Popa3c3995d2023-01-13 11:10:05 +0100129 mRs2Value = rs2Value;
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100130 return;
131 }
Vlad Popae3fd1c22022-11-07 21:03:18 +0100132
Vlad Popaf09e93f2022-10-31 16:27:12 +0100133 for (auto& streamProcessor : mActiveProcessors) {
134 sp<audio_utils::MelProcessor> processor = streamProcessor.second.promote();
135 if (processor != nullptr) {
Vlad Popa3c3995d2023-01-13 11:10:05 +0100136 status_t result = processor->setOutputRs2(rs2Value);
Vlad Popaf09e93f2022-10-31 16:27:12 +0100137 if (result != NO_ERROR) {
Vlad Popa3c3995d2023-01-13 11:10:05 +0100138 ALOGW("%s: could not set RS2 value %f for stream %d", __func__, rs2Value,
Vlad Popaf09e93f2022-10-31 16:27:12 +0100139 streamProcessor.first);
Vlad Popa3c3995d2023-01-13 11:10:05 +0100140 return;
Vlad Popaf09e93f2022-10-31 16:27:12 +0100141 }
Vlad Popa3c3995d2023-01-13 11:10:05 +0100142 mRs2Value = rs2Value;
Vlad Popaf09e93f2022-10-31 16:27:12 +0100143 }
144 }
145}
146
Vlad Popa4defd0b2022-11-06 14:22:31 +0100147void SoundDoseManager::removeStreamProcessor(audio_io_handle_t streamHandle) {
Vlad Popaf09e93f2022-10-31 16:27:12 +0100148 std::lock_guard _l(mLock);
149 auto callbackToRemove = mActiveProcessors.find(streamHandle);
Vlad Popa4defd0b2022-11-06 14:22:31 +0100150 if (callbackToRemove != mActiveProcessors.end()) {
Vlad Popaf09e93f2022-10-31 16:27:12 +0100151 mActiveProcessors.erase(callbackToRemove);
152 }
153}
154
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100155audio_port_handle_t SoundDoseManager::getIdForAudioDevice(const AudioDevice& audioDevice) const {
156 std::lock_guard _l(mLock);
157
158 audio_devices_t type;
159 std::string address;
160 auto result = aidl::android::aidl2legacy_AudioDevice_audio_device(
161 audioDevice, &type, &address);
162 if (result != NO_ERROR) {
163 ALOGE("%s: could not convert from AudioDevice to AudioDeviceTypeAddr", __func__);
164 return AUDIO_PORT_HANDLE_NONE;
165 }
166
167 auto adt = AudioDeviceTypeAddr(type, address);
168 auto deviceIt = mActiveDevices.find(adt);
169 if (deviceIt == mActiveDevices.end()) {
Vlad Popa7e81cea2023-01-19 16:34:16 +0100170 ALOGI("%s: could not find port id for device %s", __func__, adt.toString().c_str());
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100171 return AUDIO_PORT_HANDLE_NONE;
172 }
173 return deviceIt->second;
174}
175
176void SoundDoseManager::mapAddressToDeviceId(const AudioDeviceTypeAddr& adt,
177 const audio_port_handle_t deviceId) {
178 std::lock_guard _l(mLock);
Vlad Popaeafa0482023-02-23 14:35:56 +0100179 ALOGI("%s: map address: %d to device id: %d", __func__, adt.mType, deviceId);
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100180 mActiveDevices[adt] = deviceId;
Vlad Popa58e72dc2023-02-01 13:18:40 +0100181 mActiveDeviceTypes[deviceId] = adt.mType;
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100182}
183
184void SoundDoseManager::clearMapDeviceIdEntries(audio_port_handle_t deviceId) {
185 std::lock_guard _l(mLock);
186 for (auto activeDevice = mActiveDevices.begin(); activeDevice != mActiveDevices.end();) {
187 if (activeDevice->second == deviceId) {
Vlad Popaeafa0482023-02-23 14:35:56 +0100188 ALOGI("%s: clear mapping type: %d to deviceId: %d",
189 __func__, activeDevice->first.mType, deviceId);
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100190 activeDevice = mActiveDevices.erase(activeDevice);
191 continue;
192 }
193 ++activeDevice;
194 }
Vlad Popa58e72dc2023-02-01 13:18:40 +0100195 mActiveDeviceTypes.erase(deviceId);
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100196}
197
198ndk::ScopedAStatus SoundDoseManager::HalSoundDoseCallback::onMomentaryExposureWarning(
199 float in_currentDbA, const AudioDevice& in_audioDevice) {
200 auto soundDoseManager = mSoundDoseManager.promote();
201 if (soundDoseManager == nullptr) {
202 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
203 }
204
Vlad Popa3d6d39d2022-12-21 18:59:16 +0100205 std::shared_ptr<ISoundDose> halSoundDose;
206 soundDoseManager->getHalSoundDose(&halSoundDose);
207 if(halSoundDose == nullptr) {
208 ALOGW("%s: HAL sound dose interface deactivated. Ignoring", __func__);
209 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
210 }
211
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100212 auto id = soundDoseManager->getIdForAudioDevice(in_audioDevice);
213 if (id == AUDIO_PORT_HANDLE_NONE) {
Vlad Popa7e81cea2023-01-19 16:34:16 +0100214 ALOGI("%s: no mapped id for audio device with type %d and address %s",
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100215 __func__, in_audioDevice.type.type,
216 in_audioDevice.address.get<AudioDeviceAddress::id>().c_str());
217 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
218 }
219 soundDoseManager->onMomentaryExposure(in_currentDbA, id);
220
221 return ndk::ScopedAStatus::ok();
222}
223
224ndk::ScopedAStatus SoundDoseManager::HalSoundDoseCallback::onNewMelValues(
225 const ISoundDose::IHalSoundDoseCallback::MelRecord& in_melRecord,
226 const AudioDevice& in_audioDevice) {
227 auto soundDoseManager = mSoundDoseManager.promote();
228 if (soundDoseManager == nullptr) {
229 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
230 }
Vlad Popa3d6d39d2022-12-21 18:59:16 +0100231
232 std::shared_ptr<ISoundDose> halSoundDose;
233 soundDoseManager->getHalSoundDose(&halSoundDose);
234 if(halSoundDose == nullptr) {
235 ALOGW("%s: HAL sound dose interface deactivated. Ignoring", __func__);
236 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
237 }
238
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100239 auto id = soundDoseManager->getIdForAudioDevice(in_audioDevice);
240 if (id == AUDIO_PORT_HANDLE_NONE) {
Vlad Popa7e81cea2023-01-19 16:34:16 +0100241 ALOGI("%s: no mapped id for audio device with type %d and address %s",
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100242 __func__, in_audioDevice.type.type,
243 in_audioDevice.address.get<AudioDeviceAddress::id>().c_str());
244 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
245 }
246 // TODO: introduce timestamp in onNewMelValues callback
247 soundDoseManager->onNewMelValues(in_melRecord.melValues, 0,
248 in_melRecord.melValues.size(), id);
249
250 return ndk::ScopedAStatus::ok();
251}
252
Vlad Popae3fd1c22022-11-07 21:03:18 +0100253void SoundDoseManager::SoundDose::binderDied(__unused const wp<IBinder>& who) {
254 ALOGV("%s", __func__);
255
256 auto soundDoseManager = mSoundDoseManager.promote();
257 if (soundDoseManager != nullptr) {
258 soundDoseManager->resetSoundDose();
259 }
260}
261
262binder::Status SoundDoseManager::SoundDose::setOutputRs2(float value) {
263 ALOGV("%s", __func__);
264 auto soundDoseManager = mSoundDoseManager.promote();
265 if (soundDoseManager != nullptr) {
266 soundDoseManager->setOutputRs2(value);
267 }
268 return binder::Status::ok();
269}
270
271binder::Status SoundDoseManager::SoundDose::resetCsd(
272 float currentCsd, const std::vector<media::SoundDoseRecord>& records) {
273 ALOGV("%s", __func__);
274 auto soundDoseManager = mSoundDoseManager.promote();
275 if (soundDoseManager != nullptr) {
276 soundDoseManager->resetCsd(currentCsd, records);
277 }
278 return binder::Status::ok();
279}
280
Vlad Popa58e72dc2023-02-01 13:18:40 +0100281binder::Status SoundDoseManager::SoundDose::updateAttenuation(float attenuationDB, int device) {
282 ALOGV("%s", __func__);
283 auto soundDoseManager = mSoundDoseManager.promote();
284 if (soundDoseManager != nullptr) {
285 soundDoseManager->updateAttenuation(attenuationDB, static_cast<audio_devices_t>(device));
286 }
287 return binder::Status::ok();
288}
289
Vlad Popa91930462022-12-20 22:42:48 +0100290binder::Status SoundDoseManager::SoundDose::getOutputRs2(float* value) {
291 ALOGV("%s", __func__);
292 auto soundDoseManager = mSoundDoseManager.promote();
293 if (soundDoseManager != nullptr) {
294 std::lock_guard _l(soundDoseManager->mLock);
295 *value = soundDoseManager->mRs2Value;
296 }
297 return binder::Status::ok();
298}
299
300binder::Status SoundDoseManager::SoundDose::getCsd(float* value) {
301 ALOGV("%s", __func__);
302 auto soundDoseManager = mSoundDoseManager.promote();
303 if (soundDoseManager != nullptr) {
304 *value = soundDoseManager->mMelAggregator->getCsd();
305 }
306 return binder::Status::ok();
307}
308
309binder::Status SoundDoseManager::SoundDose::forceUseFrameworkMel(bool useFrameworkMel) {
310 ALOGV("%s", __func__);
311 auto soundDoseManager = mSoundDoseManager.promote();
312 if (soundDoseManager != nullptr) {
313 soundDoseManager->setUseFrameworkMel(useFrameworkMel);
314 }
315 return binder::Status::ok();
316}
317
318binder::Status SoundDoseManager::SoundDose::forceComputeCsdOnAllDevices(
319 bool computeCsdOnAllDevices) {
320 ALOGV("%s", __func__);
321 auto soundDoseManager = mSoundDoseManager.promote();
322 if (soundDoseManager != nullptr) {
323 soundDoseManager->setComputeCsdOnAllDevices(computeCsdOnAllDevices);
324 }
325 return binder::Status::ok();
326}
327
Vlad Popa58e72dc2023-02-01 13:18:40 +0100328void SoundDoseManager::updateAttenuation(float attenuationDB, audio_devices_t deviceType) {
329 std::lock_guard _l(mLock);
Vlad Popaeafa0482023-02-23 14:35:56 +0100330 ALOGV("%s: updating MEL processor attenuation for device type %d to %f",
Vlad Popa58e72dc2023-02-01 13:18:40 +0100331 __func__, deviceType, attenuationDB);
332 mMelAttenuationDB[deviceType] = attenuationDB;
333 for (const auto& mp : mActiveProcessors) {
334 auto melProcessor = mp.second.promote();
335 if (melProcessor != nullptr) {
336 auto deviceId = melProcessor->getDeviceId();
337 if (mActiveDeviceTypes[deviceId] == deviceType) {
Vlad Popaeafa0482023-02-23 14:35:56 +0100338 ALOGV("%s: set attenuation for deviceId %d to %f",
Vlad Popa58e72dc2023-02-01 13:18:40 +0100339 __func__, deviceId, attenuationDB);
340 melProcessor->setAttenuation(attenuationDB);
341 }
342 }
343 }
344}
345
Vlad Popa91930462022-12-20 22:42:48 +0100346void SoundDoseManager::setUseFrameworkMel(bool useFrameworkMel) {
Vlad Popa3d6d39d2022-12-21 18:59:16 +0100347 // invalidate any HAL sound dose interface used
348 setHalSoundDoseInterface(nullptr);
349
Vlad Popa91930462022-12-20 22:42:48 +0100350 std::lock_guard _l(mLock);
351 mUseFrameworkMel = useFrameworkMel;
352}
353
Vlad Popa3d6d39d2022-12-21 18:59:16 +0100354bool SoundDoseManager::forceUseFrameworkMel() const {
Vlad Popa91930462022-12-20 22:42:48 +0100355 std::lock_guard _l(mLock);
356 return mUseFrameworkMel;
357}
358
359void SoundDoseManager::setComputeCsdOnAllDevices(bool computeCsdOnAllDevices) {
360 std::lock_guard _l(mLock);
361 mComputeCsdOnAllDevices = computeCsdOnAllDevices;
362}
363
Vlad Popa3d6d39d2022-12-21 18:59:16 +0100364bool SoundDoseManager::forceComputeCsdOnAllDevices() const {
Vlad Popa91930462022-12-20 22:42:48 +0100365 std::lock_guard _l(mLock);
366 return mComputeCsdOnAllDevices;
367}
368
Vlad Popa3d6d39d2022-12-21 18:59:16 +0100369void SoundDoseManager::getHalSoundDose(std::shared_ptr<ISoundDose>* halSoundDose) const {
370 std::lock_guard _l(mLock);
371 *halSoundDose = mHalSoundDose;
372}
373
Vlad Popae3fd1c22022-11-07 21:03:18 +0100374void SoundDoseManager::resetSoundDose() {
375 std::lock_guard lock(mLock);
376 mSoundDose = nullptr;
377}
378
379void SoundDoseManager::resetCsd(float currentCsd,
380 const std::vector<media::SoundDoseRecord>& records) {
381 std::lock_guard lock(mLock);
382 std::vector<audio_utils::CsdRecord> resetRecords;
383 for (const auto& record : records) {
384 resetRecords.emplace_back(record.timestamp, record.duration, record.value,
385 record.averageMel);
386 }
387
388 mMelAggregator->reset(currentCsd, resetRecords);
389}
390
Vlad Popa4defd0b2022-11-06 14:22:31 +0100391void SoundDoseManager::onNewMelValues(const std::vector<float>& mels, size_t offset, size_t length,
392 audio_port_handle_t deviceId) const {
Vlad Popaf09e93f2022-10-31 16:27:12 +0100393 ALOGV("%s", __func__);
Vlad Popa2900c0a2022-10-24 13:38:00 +0200394
Vlad Popa4defd0b2022-11-06 14:22:31 +0100395 sp<media::ISoundDoseCallback> soundDoseCallback;
396 std::vector<audio_utils::CsdRecord> records;
397 float currentCsd;
398 {
399 std::lock_guard _l(mLock);
Vlad Popa2900c0a2022-10-24 13:38:00 +0200400
Vlad Popa4defd0b2022-11-06 14:22:31 +0100401 int64_t timestampSec = getMonotonicSecond();
402
403 // only for internal callbacks
404 records = mMelAggregator->aggregateAndAddNewMelRecord(audio_utils::MelRecord(
405 deviceId, std::vector<float>(mels.begin() + offset, mels.begin() + offset + length),
406 timestampSec - length));
407
408 currentCsd = mMelAggregator->getCsd();
409 }
410
411 soundDoseCallback = getSoundDoseCallback();
412
413 if (records.size() > 0 && soundDoseCallback != nullptr) {
414 std::vector<media::SoundDoseRecord> newRecordsToReport;
415 for (const auto& record : records) {
416 newRecordsToReport.emplace_back(csdRecordToSoundDoseRecord(record));
417 }
418
419 soundDoseCallback->onNewCsdValue(currentCsd, newRecordsToReport);
420 }
Vlad Popa2900c0a2022-10-24 13:38:00 +0200421}
422
Vlad Popa4defd0b2022-11-06 14:22:31 +0100423sp<media::ISoundDoseCallback> SoundDoseManager::getSoundDoseCallback() const {
424 std::lock_guard _l(mLock);
Vlad Popae3fd1c22022-11-07 21:03:18 +0100425 if (mSoundDose == nullptr) {
426 return nullptr;
427 }
428
429 return mSoundDose->mSoundDoseCallback;
Vlad Popa4defd0b2022-11-06 14:22:31 +0100430}
431
432void SoundDoseManager::onMomentaryExposure(float currentMel, audio_port_handle_t deviceId) const {
433 ALOGV("%s: Momentary exposure for device %d triggered: %f MEL", __func__, deviceId, currentMel);
Vlad Popa63f047e2022-11-05 14:09:19 +0100434
Vlad Popae3fd1c22022-11-07 21:03:18 +0100435 auto soundDoseCallback = getSoundDoseCallback();
Vlad Popa63f047e2022-11-05 14:09:19 +0100436 if (soundDoseCallback != nullptr) {
Vlad Popae3fd1c22022-11-07 21:03:18 +0100437 soundDoseCallback->onMomentaryExposure(currentMel, deviceId);
Vlad Popa63f047e2022-11-05 14:09:19 +0100438 }
439}
440
Vlad Popae3fd1c22022-11-07 21:03:18 +0100441sp<media::ISoundDose> SoundDoseManager::getSoundDoseInterface(
442 const sp<media::ISoundDoseCallback>& callback) {
Vlad Popa63f047e2022-11-05 14:09:19 +0100443 ALOGV("%s: Register ISoundDoseCallback", __func__);
444
445 std::lock_guard _l(mLock);
Vlad Popae3fd1c22022-11-07 21:03:18 +0100446 if (mSoundDose == nullptr) {
447 mSoundDose = sp<SoundDose>::make(this, callback);
448 }
449 return mSoundDose;
Vlad Popaf09e93f2022-10-31 16:27:12 +0100450}
451
Vlad Popa4defd0b2022-11-06 14:22:31 +0100452std::string SoundDoseManager::dump() const {
Vlad Popa2900c0a2022-10-24 13:38:00 +0200453 std::string output;
Vlad Popaf09e93f2022-10-31 16:27:12 +0100454 mMelAggregator->foreachCsd([&output](audio_utils::CsdRecord csdRecord) {
Vlad Popa2900c0a2022-10-24 13:38:00 +0200455 base::StringAppendF(&output,
456 "CSD %f with average MEL %f in interval [%" PRId64 ", %" PRId64 "]",
Vlad Popa4defd0b2022-11-06 14:22:31 +0100457 csdRecord.value, csdRecord.averageMel, csdRecord.timestamp,
Vlad Popa2900c0a2022-10-24 13:38:00 +0200458 csdRecord.timestamp + csdRecord.duration);
459 base::StringAppendF(&output, "\n");
460 });
461
462 base::StringAppendF(&output, "\nCached Mel Records:\n");
Vlad Popaf09e93f2022-10-31 16:27:12 +0100463 mMelAggregator->foreachCachedMel([&output](const audio_utils::MelRecord& melRecord) {
Vlad Popa2900c0a2022-10-24 13:38:00 +0200464 base::StringAppendF(&output, "Continuous MELs for portId=%d, ", melRecord.portId);
465 base::StringAppendF(&output, "starting at timestamp %" PRId64 ": ", melRecord.timestamp);
466
467 for (const auto& mel : melRecord.mels) {
468 base::StringAppendF(&output, "%.2f ", mel);
469 }
470 base::StringAppendF(&output, "\n");
471 });
472
473 return output;
474}
475
476size_t SoundDoseManager::getCachedMelRecordsSize() const {
Vlad Popaf09e93f2022-10-31 16:27:12 +0100477 return mMelAggregator->getCachedMelRecordsSize();
Vlad Popa2900c0a2022-10-24 13:38:00 +0200478}
479
Vlad Popa4defd0b2022-11-06 14:22:31 +0100480media::SoundDoseRecord SoundDoseManager::csdRecordToSoundDoseRecord(
481 const audio_utils::CsdRecord& legacy) {
482 media::SoundDoseRecord soundDoseRecord{};
483 soundDoseRecord.timestamp = legacy.timestamp;
484 soundDoseRecord.duration = legacy.duration;
485 soundDoseRecord.value = legacy.value;
486 soundDoseRecord.averageMel = legacy.averageMel;
487 return soundDoseRecord;
488}
489
Vlad Popa2900c0a2022-10-24 13:38:00 +0200490} // namespace android