blob: f94ab1e1e4400ba375ca4c4d6aa749984ff93d5b [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 Popa4847de12023-03-16 18:30:08 +010027#include <ctime>
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 Popa2a06fca2023-02-06 16:45:45 +010053 if (mHalSoundDose != nullptr && !mDisableCsd) {
Vlad Popa1c2f7e12023-03-28 02:08:56 +020054 ALOGD("%s: using HAL MEL computation, no MelProcessor needed.", __func__);
Vlad Popa1d5f0d52022-12-18 12:21:26 +010055 return nullptr;
56 }
57
Vlad Popaf09e93f2022-10-31 16:27:12 +010058 auto streamProcessor = mActiveProcessors.find(streamHandle);
Vlad Popa1c2f7e12023-03-28 02:08:56 +020059 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 Popa58e72dc2023-02-01 13:18:40 +010072 }
Vlad Popa2900c0a2022-10-24 13:38:00 +020073 }
Vlad Popa1c2f7e12023-03-28 02:08:56 +020074
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 Popa2900c0a2022-10-24 13:38:00 +020084}
85
Vlad Popa1d5f0d52022-12-18 12:21:26 +010086bool 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 Popa4847de12023-03-16 18:30:08 +010098 if (!mHalSoundDose->setOutputRs2UpperBound(mRs2UpperBound).isOk()) {
Vlad Popa1d5f0d52022-12-18 12:21:26 +010099 ALOGW("%s: Cannot set RS2 value for momentary exposure %f",
100 __func__,
Vlad Popa4847de12023-03-16 18:30:08 +0100101 mRs2UpperBound);
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100102 }
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 Popa4847de12023-03-16 18:30:08 +0100122void SoundDoseManager::setOutputRs2UpperBound(float rs2Value) {
Vlad Popa2900c0a2022-10-24 13:38:00 +0200123 ALOGV("%s", __func__);
Vlad Popaf09e93f2022-10-31 16:27:12 +0100124 std::lock_guard _l(mLock);
125
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100126 if (mHalSoundDose != nullptr) {
127 // using the HAL sound dose interface
Vlad Popa4847de12023-03-16 18:30:08 +0100128 if (!mHalSoundDose->setOutputRs2UpperBound(rs2Value).isOk()) {
Vlad Popa3c3995d2023-01-13 11:10:05 +0100129 ALOGE("%s: Cannot set RS2 value for momentary exposure %f", __func__, rs2Value);
130 return;
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100131 }
Vlad Popa4847de12023-03-16 18:30:08 +0100132 mRs2UpperBound = rs2Value;
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100133 return;
134 }
Vlad Popae3fd1c22022-11-07 21:03:18 +0100135
Vlad Popaf09e93f2022-10-31 16:27:12 +0100136 for (auto& streamProcessor : mActiveProcessors) {
137 sp<audio_utils::MelProcessor> processor = streamProcessor.second.promote();
138 if (processor != nullptr) {
Vlad Popa4847de12023-03-16 18:30:08 +0100139 status_t result = processor->setOutputRs2UpperBound(rs2Value);
Vlad Popaf09e93f2022-10-31 16:27:12 +0100140 if (result != NO_ERROR) {
Vlad Popa4847de12023-03-16 18:30:08 +0100141 ALOGW("%s: could not set RS2 upper bound %f for stream %d", __func__, rs2Value,
Vlad Popaf09e93f2022-10-31 16:27:12 +0100142 streamProcessor.first);
Vlad Popa3c3995d2023-01-13 11:10:05 +0100143 return;
Vlad Popaf09e93f2022-10-31 16:27:12 +0100144 }
Vlad Popa4847de12023-03-16 18:30:08 +0100145 mRs2UpperBound = rs2Value;
Vlad Popaf09e93f2022-10-31 16:27:12 +0100146 }
147 }
148}
149
Vlad Popa4defd0b2022-11-06 14:22:31 +0100150void SoundDoseManager::removeStreamProcessor(audio_io_handle_t streamHandle) {
Vlad Popaf09e93f2022-10-31 16:27:12 +0100151 std::lock_guard _l(mLock);
152 auto callbackToRemove = mActiveProcessors.find(streamHandle);
Vlad Popa4defd0b2022-11-06 14:22:31 +0100153 if (callbackToRemove != mActiveProcessors.end()) {
Vlad Popaf09e93f2022-10-31 16:27:12 +0100154 mActiveProcessors.erase(callbackToRemove);
155 }
156}
157
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100158audio_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 Popa7e81cea2023-01-19 16:34:16 +0100173 ALOGI("%s: could not find port id for device %s", __func__, adt.toString().c_str());
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100174 return AUDIO_PORT_HANDLE_NONE;
175 }
176 return deviceIt->second;
177}
178
179void SoundDoseManager::mapAddressToDeviceId(const AudioDeviceTypeAddr& adt,
180 const audio_port_handle_t deviceId) {
181 std::lock_guard _l(mLock);
Vlad Popaeafa0482023-02-23 14:35:56 +0100182 ALOGI("%s: map address: %d to device id: %d", __func__, adt.mType, deviceId);
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100183 mActiveDevices[adt] = deviceId;
Vlad Popa58e72dc2023-02-01 13:18:40 +0100184 mActiveDeviceTypes[deviceId] = adt.mType;
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100185}
186
187void 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 Popaeafa0482023-02-23 14:35:56 +0100191 ALOGI("%s: clear mapping type: %d to deviceId: %d",
192 __func__, activeDevice->first.mType, deviceId);
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100193 activeDevice = mActiveDevices.erase(activeDevice);
194 continue;
195 }
196 ++activeDevice;
197 }
Vlad Popa58e72dc2023-02-01 13:18:40 +0100198 mActiveDeviceTypes.erase(deviceId);
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100199}
200
201ndk::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 Popa3d6d39d2022-12-21 18:59:16 +0100208 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 Popa1d5f0d52022-12-18 12:21:26 +0100215 auto id = soundDoseManager->getIdForAudioDevice(in_audioDevice);
216 if (id == AUDIO_PORT_HANDLE_NONE) {
Vlad Popa7e81cea2023-01-19 16:34:16 +0100217 ALOGI("%s: no mapped id for audio device with type %d and address %s",
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100218 __func__, in_audioDevice.type.type,
Vlad Popa69fbbee2023-04-25 12:23:24 +0200219 in_audioDevice.address.toString().c_str());
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100220 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
221 }
222 soundDoseManager->onMomentaryExposure(in_currentDbA, id);
223
224 return ndk::ScopedAStatus::ok();
225}
226
227ndk::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 Popa3d6d39d2022-12-21 18:59:16 +0100234
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 Popa1d5f0d52022-12-18 12:21:26 +0100242 auto id = soundDoseManager->getIdForAudioDevice(in_audioDevice);
243 if (id == AUDIO_PORT_HANDLE_NONE) {
Vlad Popa7e81cea2023-01-19 16:34:16 +0100244 ALOGI("%s: no mapped id for audio device with type %d and address %s",
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100245 __func__, in_audioDevice.type.type,
Vlad Popa69fbbee2023-04-25 12:23:24 +0200246 in_audioDevice.address.toString().c_str());
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100247 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 Popae3fd1c22022-11-07 21:03:18 +0100256void 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 Popa4847de12023-03-16 18:30:08 +0100265binder::Status SoundDoseManager::SoundDose::setOutputRs2UpperBound(float value) {
Vlad Popae3fd1c22022-11-07 21:03:18 +0100266 ALOGV("%s", __func__);
267 auto soundDoseManager = mSoundDoseManager.promote();
268 if (soundDoseManager != nullptr) {
Vlad Popa4847de12023-03-16 18:30:08 +0100269 soundDoseManager->setOutputRs2UpperBound(value);
Vlad Popae3fd1c22022-11-07 21:03:18 +0100270 }
271 return binder::Status::ok();
272}
273
274binder::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 Popa58e72dc2023-02-01 13:18:40 +0100284binder::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 Popa2a06fca2023-02-06 16:45:45 +0100293binder::Status SoundDoseManager::SoundDose::disableCsd() {
294 ALOGV("%s", __func__);
295 auto soundDoseManager = mSoundDoseManager.promote();
296 if (soundDoseManager != nullptr) {
297 soundDoseManager->disableCsd();
298 }
299 return binder::Status::ok();
300}
301
Vlad Popa4847de12023-03-16 18:30:08 +0100302binder::Status SoundDoseManager::SoundDose::getOutputRs2UpperBound(float* value) {
Vlad Popa91930462022-12-20 22:42:48 +0100303 ALOGV("%s", __func__);
304 auto soundDoseManager = mSoundDoseManager.promote();
305 if (soundDoseManager != nullptr) {
306 std::lock_guard _l(soundDoseManager->mLock);
Vlad Popa4847de12023-03-16 18:30:08 +0100307 *value = soundDoseManager->mRs2UpperBound;
Vlad Popa91930462022-12-20 22:42:48 +0100308 }
309 return binder::Status::ok();
310}
311
312binder::Status SoundDoseManager::SoundDose::getCsd(float* value) {
313 ALOGV("%s", __func__);
314 auto soundDoseManager = mSoundDoseManager.promote();
315 if (soundDoseManager != nullptr) {
316 *value = soundDoseManager->mMelAggregator->getCsd();
317 }
318 return binder::Status::ok();
319}
320
321binder::Status SoundDoseManager::SoundDose::forceUseFrameworkMel(bool useFrameworkMel) {
322 ALOGV("%s", __func__);
323 auto soundDoseManager = mSoundDoseManager.promote();
324 if (soundDoseManager != nullptr) {
325 soundDoseManager->setUseFrameworkMel(useFrameworkMel);
326 }
327 return binder::Status::ok();
328}
329
330binder::Status SoundDoseManager::SoundDose::forceComputeCsdOnAllDevices(
331 bool computeCsdOnAllDevices) {
332 ALOGV("%s", __func__);
333 auto soundDoseManager = mSoundDoseManager.promote();
334 if (soundDoseManager != nullptr) {
335 soundDoseManager->setComputeCsdOnAllDevices(computeCsdOnAllDevices);
336 }
337 return binder::Status::ok();
338}
339
Vlad Popa95ee3ca2023-03-20 19:29:38 +0000340binder::Status SoundDoseManager::SoundDose::isSoundDoseHalSupported(bool* value) {
341 ALOGV("%s", __func__);
342 *value = false;
343 auto soundDoseManager = mSoundDoseManager.promote();
344 if (soundDoseManager != nullptr) {
345 *value = soundDoseManager->isSoundDoseHalSupported();
346 }
347 return binder::Status::ok();
348}
349
Vlad Popa58e72dc2023-02-01 13:18:40 +0100350void SoundDoseManager::updateAttenuation(float attenuationDB, audio_devices_t deviceType) {
351 std::lock_guard _l(mLock);
Vlad Popaeafa0482023-02-23 14:35:56 +0100352 ALOGV("%s: updating MEL processor attenuation for device type %d to %f",
Vlad Popa58e72dc2023-02-01 13:18:40 +0100353 __func__, deviceType, attenuationDB);
354 mMelAttenuationDB[deviceType] = attenuationDB;
355 for (const auto& mp : mActiveProcessors) {
356 auto melProcessor = mp.second.promote();
357 if (melProcessor != nullptr) {
358 auto deviceId = melProcessor->getDeviceId();
359 if (mActiveDeviceTypes[deviceId] == deviceType) {
Vlad Popaeafa0482023-02-23 14:35:56 +0100360 ALOGV("%s: set attenuation for deviceId %d to %f",
Vlad Popa58e72dc2023-02-01 13:18:40 +0100361 __func__, deviceId, attenuationDB);
362 melProcessor->setAttenuation(attenuationDB);
363 }
364 }
365 }
366}
367
Vlad Popa2a06fca2023-02-06 16:45:45 +0100368void SoundDoseManager::disableCsd() {
369 ALOGV("%s", __func__);
370
371 std::lock_guard _l(mLock);
372 mDisableCsd = true;
373
374 // Normally, there should be no active MelProcessors when this method is called
375 // We pause however every cached MelProcessor as a defensive mechanism to not
376 // have unnecessary processing
377 for (auto& activeEntry : mActiveProcessors) {
378 auto melProcessor = activeEntry.second.promote();
379 if (melProcessor != nullptr) {
380 melProcessor->pause();
381 }
382 }
383}
384
385bool SoundDoseManager::isCsdDisabled() {
386 std::lock_guard _l(mLock);
387 return mDisableCsd;
388}
389
Vlad Popa91930462022-12-20 22:42:48 +0100390void SoundDoseManager::setUseFrameworkMel(bool useFrameworkMel) {
Vlad Popa3d6d39d2022-12-21 18:59:16 +0100391 // invalidate any HAL sound dose interface used
392 setHalSoundDoseInterface(nullptr);
393
Vlad Popa91930462022-12-20 22:42:48 +0100394 std::lock_guard _l(mLock);
395 mUseFrameworkMel = useFrameworkMel;
396}
397
Vlad Popa3d6d39d2022-12-21 18:59:16 +0100398bool SoundDoseManager::forceUseFrameworkMel() const {
Vlad Popa91930462022-12-20 22:42:48 +0100399 std::lock_guard _l(mLock);
400 return mUseFrameworkMel;
401}
402
403void SoundDoseManager::setComputeCsdOnAllDevices(bool computeCsdOnAllDevices) {
404 std::lock_guard _l(mLock);
405 mComputeCsdOnAllDevices = computeCsdOnAllDevices;
406}
407
Vlad Popa3d6d39d2022-12-21 18:59:16 +0100408bool SoundDoseManager::forceComputeCsdOnAllDevices() const {
Vlad Popa91930462022-12-20 22:42:48 +0100409 std::lock_guard _l(mLock);
410 return mComputeCsdOnAllDevices;
411}
412
Vlad Popa95ee3ca2023-03-20 19:29:38 +0000413bool SoundDoseManager::isSoundDoseHalSupported() const {
414 if (mDisableCsd) {
415 return false;
416 }
417
418 std::shared_ptr<ISoundDose> halSoundDose;
419 getHalSoundDose(&halSoundDose);
420 if (mHalSoundDose == nullptr) {
421 return false;
422 }
423 return true;
424}
425
Vlad Popa3d6d39d2022-12-21 18:59:16 +0100426void SoundDoseManager::getHalSoundDose(std::shared_ptr<ISoundDose>* halSoundDose) const {
427 std::lock_guard _l(mLock);
428 *halSoundDose = mHalSoundDose;
429}
430
Vlad Popae3fd1c22022-11-07 21:03:18 +0100431void SoundDoseManager::resetSoundDose() {
432 std::lock_guard lock(mLock);
433 mSoundDose = nullptr;
434}
435
436void SoundDoseManager::resetCsd(float currentCsd,
437 const std::vector<media::SoundDoseRecord>& records) {
438 std::lock_guard lock(mLock);
439 std::vector<audio_utils::CsdRecord> resetRecords;
440 for (const auto& record : records) {
441 resetRecords.emplace_back(record.timestamp, record.duration, record.value,
442 record.averageMel);
443 }
444
445 mMelAggregator->reset(currentCsd, resetRecords);
446}
447
Vlad Popa4defd0b2022-11-06 14:22:31 +0100448void SoundDoseManager::onNewMelValues(const std::vector<float>& mels, size_t offset, size_t length,
449 audio_port_handle_t deviceId) const {
Vlad Popaf09e93f2022-10-31 16:27:12 +0100450 ALOGV("%s", __func__);
Vlad Popa2900c0a2022-10-24 13:38:00 +0200451
Vlad Popa2a06fca2023-02-06 16:45:45 +0100452
Vlad Popa4defd0b2022-11-06 14:22:31 +0100453 sp<media::ISoundDoseCallback> soundDoseCallback;
454 std::vector<audio_utils::CsdRecord> records;
455 float currentCsd;
456 {
457 std::lock_guard _l(mLock);
Vlad Popa2a06fca2023-02-06 16:45:45 +0100458 if (mDisableCsd) {
459 return;
460 }
461
Vlad Popa2900c0a2022-10-24 13:38:00 +0200462
Vlad Popa4defd0b2022-11-06 14:22:31 +0100463 int64_t timestampSec = getMonotonicSecond();
464
465 // only for internal callbacks
466 records = mMelAggregator->aggregateAndAddNewMelRecord(audio_utils::MelRecord(
467 deviceId, std::vector<float>(mels.begin() + offset, mels.begin() + offset + length),
468 timestampSec - length));
469
470 currentCsd = mMelAggregator->getCsd();
471 }
472
473 soundDoseCallback = getSoundDoseCallback();
474
475 if (records.size() > 0 && soundDoseCallback != nullptr) {
476 std::vector<media::SoundDoseRecord> newRecordsToReport;
477 for (const auto& record : records) {
478 newRecordsToReport.emplace_back(csdRecordToSoundDoseRecord(record));
479 }
480
481 soundDoseCallback->onNewCsdValue(currentCsd, newRecordsToReport);
482 }
Vlad Popa2900c0a2022-10-24 13:38:00 +0200483}
484
Vlad Popa4defd0b2022-11-06 14:22:31 +0100485sp<media::ISoundDoseCallback> SoundDoseManager::getSoundDoseCallback() const {
486 std::lock_guard _l(mLock);
Vlad Popae3fd1c22022-11-07 21:03:18 +0100487 if (mSoundDose == nullptr) {
488 return nullptr;
489 }
490
491 return mSoundDose->mSoundDoseCallback;
Vlad Popa4defd0b2022-11-06 14:22:31 +0100492}
493
494void SoundDoseManager::onMomentaryExposure(float currentMel, audio_port_handle_t deviceId) const {
495 ALOGV("%s: Momentary exposure for device %d triggered: %f MEL", __func__, deviceId, currentMel);
Vlad Popa63f047e2022-11-05 14:09:19 +0100496
Vlad Popa2a06fca2023-02-06 16:45:45 +0100497 {
498 std::lock_guard _l(mLock);
499 if (mDisableCsd) {
500 return;
501 }
502 }
503
Vlad Popae3fd1c22022-11-07 21:03:18 +0100504 auto soundDoseCallback = getSoundDoseCallback();
Vlad Popa63f047e2022-11-05 14:09:19 +0100505 if (soundDoseCallback != nullptr) {
Vlad Popae3fd1c22022-11-07 21:03:18 +0100506 soundDoseCallback->onMomentaryExposure(currentMel, deviceId);
Vlad Popa63f047e2022-11-05 14:09:19 +0100507 }
508}
509
Vlad Popae3fd1c22022-11-07 21:03:18 +0100510sp<media::ISoundDose> SoundDoseManager::getSoundDoseInterface(
511 const sp<media::ISoundDoseCallback>& callback) {
Vlad Popa63f047e2022-11-05 14:09:19 +0100512 ALOGV("%s: Register ISoundDoseCallback", __func__);
513
514 std::lock_guard _l(mLock);
Vlad Popae3fd1c22022-11-07 21:03:18 +0100515 if (mSoundDose == nullptr) {
516 mSoundDose = sp<SoundDose>::make(this, callback);
517 }
518 return mSoundDose;
Vlad Popaf09e93f2022-10-31 16:27:12 +0100519}
520
Vlad Popa4defd0b2022-11-06 14:22:31 +0100521std::string SoundDoseManager::dump() const {
Vlad Popa2900c0a2022-10-24 13:38:00 +0200522 std::string output;
Vlad Popa2a06fca2023-02-06 16:45:45 +0100523 {
524 std::lock_guard _l(mLock);
525 if (mDisableCsd) {
526 base::StringAppendF(&output, "CSD is disabled");
527 return output;
528 }
529 }
530
Vlad Popaf09e93f2022-10-31 16:27:12 +0100531 mMelAggregator->foreachCsd([&output](audio_utils::CsdRecord csdRecord) {
Vlad Popa2900c0a2022-10-24 13:38:00 +0200532 base::StringAppendF(&output,
533 "CSD %f with average MEL %f in interval [%" PRId64 ", %" PRId64 "]",
Vlad Popa4defd0b2022-11-06 14:22:31 +0100534 csdRecord.value, csdRecord.averageMel, csdRecord.timestamp,
Vlad Popa2900c0a2022-10-24 13:38:00 +0200535 csdRecord.timestamp + csdRecord.duration);
536 base::StringAppendF(&output, "\n");
537 });
538
539 base::StringAppendF(&output, "\nCached Mel Records:\n");
Vlad Popaf09e93f2022-10-31 16:27:12 +0100540 mMelAggregator->foreachCachedMel([&output](const audio_utils::MelRecord& melRecord) {
Vlad Popa2900c0a2022-10-24 13:38:00 +0200541 base::StringAppendF(&output, "Continuous MELs for portId=%d, ", melRecord.portId);
542 base::StringAppendF(&output, "starting at timestamp %" PRId64 ": ", melRecord.timestamp);
543
544 for (const auto& mel : melRecord.mels) {
545 base::StringAppendF(&output, "%.2f ", mel);
546 }
547 base::StringAppendF(&output, "\n");
548 });
549
550 return output;
551}
552
553size_t SoundDoseManager::getCachedMelRecordsSize() const {
Vlad Popaf09e93f2022-10-31 16:27:12 +0100554 return mMelAggregator->getCachedMelRecordsSize();
Vlad Popa2900c0a2022-10-24 13:38:00 +0200555}
556
Vlad Popa4defd0b2022-11-06 14:22:31 +0100557media::SoundDoseRecord SoundDoseManager::csdRecordToSoundDoseRecord(
558 const audio_utils::CsdRecord& legacy) {
559 media::SoundDoseRecord soundDoseRecord{};
560 soundDoseRecord.timestamp = legacy.timestamp;
561 soundDoseRecord.duration = legacy.duration;
562 soundDoseRecord.value = legacy.value;
563 soundDoseRecord.averageMel = legacy.averageMel;
564 return soundDoseRecord;
565}
566
Vlad Popa2900c0a2022-10-24 13:38:00 +0200567} // namespace android