blob: 3505a8544ad81dbb0be87cbebe59b4f85c870c45 [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 Popa617bbf02023-04-24 19:10:36 +020053 if (mHalSoundDose != nullptr && mEnabledCsd) {
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,
219 in_audioDevice.address.get<AudioDeviceAddress::id>().c_str());
220 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,
246 in_audioDevice.address.get<AudioDeviceAddress::id>().c_str());
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 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 Popa617bbf02023-04-24 19:10:36 +0200293binder::Status SoundDoseManager::SoundDose::setCsdEnabled(bool enabled) {
Vlad Popa2a06fca2023-02-06 16:45:45 +0100294 ALOGV("%s", __func__);
295 auto soundDoseManager = mSoundDoseManager.promote();
296 if (soundDoseManager != nullptr) {
Vlad Popa617bbf02023-04-24 19:10:36 +0200297 soundDoseManager->setCsdEnabled(enabled);
Vlad Popa2a06fca2023-02-06 16:45:45 +0100298 }
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 Popa617bbf02023-04-24 19:10:36 +0200368void SoundDoseManager::setCsdEnabled(bool enabled) {
Vlad Popa2a06fca2023-02-06 16:45:45 +0100369 ALOGV("%s", __func__);
370
371 std::lock_guard _l(mLock);
Vlad Popa617bbf02023-04-24 19:10:36 +0200372 mEnabledCsd = enabled;
Vlad Popa2a06fca2023-02-06 16:45:45 +0100373
Vlad Popa2a06fca2023-02-06 16:45:45 +0100374 for (auto& activeEntry : mActiveProcessors) {
375 auto melProcessor = activeEntry.second.promote();
376 if (melProcessor != nullptr) {
Vlad Popa617bbf02023-04-24 19:10:36 +0200377 if (enabled) {
378 melProcessor->resume();
379 } else {
380 melProcessor->pause();
381 }
Vlad Popa2a06fca2023-02-06 16:45:45 +0100382 }
383 }
384}
385
Vlad Popa617bbf02023-04-24 19:10:36 +0200386bool SoundDoseManager::isCsdEnabled() {
Vlad Popa2a06fca2023-02-06 16:45:45 +0100387 std::lock_guard _l(mLock);
Vlad Popa617bbf02023-04-24 19:10:36 +0200388 return mEnabledCsd;
Vlad Popa2a06fca2023-02-06 16:45:45 +0100389}
390
Vlad Popa91930462022-12-20 22:42:48 +0100391void SoundDoseManager::setUseFrameworkMel(bool useFrameworkMel) {
Vlad Popa3d6d39d2022-12-21 18:59:16 +0100392 // invalidate any HAL sound dose interface used
393 setHalSoundDoseInterface(nullptr);
394
Vlad Popa91930462022-12-20 22:42:48 +0100395 std::lock_guard _l(mLock);
396 mUseFrameworkMel = useFrameworkMel;
397}
398
Vlad Popa3d6d39d2022-12-21 18:59:16 +0100399bool SoundDoseManager::forceUseFrameworkMel() const {
Vlad Popa91930462022-12-20 22:42:48 +0100400 std::lock_guard _l(mLock);
401 return mUseFrameworkMel;
402}
403
404void SoundDoseManager::setComputeCsdOnAllDevices(bool computeCsdOnAllDevices) {
405 std::lock_guard _l(mLock);
406 mComputeCsdOnAllDevices = computeCsdOnAllDevices;
407}
408
Vlad Popa3d6d39d2022-12-21 18:59:16 +0100409bool SoundDoseManager::forceComputeCsdOnAllDevices() const {
Vlad Popa91930462022-12-20 22:42:48 +0100410 std::lock_guard _l(mLock);
411 return mComputeCsdOnAllDevices;
412}
413
Vlad Popa95ee3ca2023-03-20 19:29:38 +0000414bool SoundDoseManager::isSoundDoseHalSupported() const {
Vlad Popa617bbf02023-04-24 19:10:36 +0200415 if (!mEnabledCsd) {
Vlad Popa95ee3ca2023-03-20 19:29:38 +0000416 return false;
417 }
418
419 std::shared_ptr<ISoundDose> halSoundDose;
420 getHalSoundDose(&halSoundDose);
421 if (mHalSoundDose == nullptr) {
422 return false;
423 }
424 return true;
425}
426
Vlad Popa3d6d39d2022-12-21 18:59:16 +0100427void SoundDoseManager::getHalSoundDose(std::shared_ptr<ISoundDose>* halSoundDose) const {
428 std::lock_guard _l(mLock);
429 *halSoundDose = mHalSoundDose;
430}
431
Vlad Popae3fd1c22022-11-07 21:03:18 +0100432void SoundDoseManager::resetSoundDose() {
433 std::lock_guard lock(mLock);
434 mSoundDose = nullptr;
435}
436
437void SoundDoseManager::resetCsd(float currentCsd,
438 const std::vector<media::SoundDoseRecord>& records) {
439 std::lock_guard lock(mLock);
440 std::vector<audio_utils::CsdRecord> resetRecords;
441 for (const auto& record : records) {
442 resetRecords.emplace_back(record.timestamp, record.duration, record.value,
443 record.averageMel);
444 }
445
446 mMelAggregator->reset(currentCsd, resetRecords);
447}
448
Vlad Popa4defd0b2022-11-06 14:22:31 +0100449void SoundDoseManager::onNewMelValues(const std::vector<float>& mels, size_t offset, size_t length,
450 audio_port_handle_t deviceId) const {
Vlad Popaf09e93f2022-10-31 16:27:12 +0100451 ALOGV("%s", __func__);
Vlad Popa2900c0a2022-10-24 13:38:00 +0200452
Vlad Popa2a06fca2023-02-06 16:45:45 +0100453
Vlad Popa4defd0b2022-11-06 14:22:31 +0100454 sp<media::ISoundDoseCallback> soundDoseCallback;
455 std::vector<audio_utils::CsdRecord> records;
456 float currentCsd;
457 {
458 std::lock_guard _l(mLock);
Vlad Popa617bbf02023-04-24 19:10:36 +0200459 if (!mEnabledCsd) {
Vlad Popa2a06fca2023-02-06 16:45:45 +0100460 return;
461 }
462
Vlad Popa2900c0a2022-10-24 13:38:00 +0200463
Vlad Popa4defd0b2022-11-06 14:22:31 +0100464 int64_t timestampSec = getMonotonicSecond();
465
466 // only for internal callbacks
467 records = mMelAggregator->aggregateAndAddNewMelRecord(audio_utils::MelRecord(
468 deviceId, std::vector<float>(mels.begin() + offset, mels.begin() + offset + length),
469 timestampSec - length));
470
471 currentCsd = mMelAggregator->getCsd();
472 }
473
474 soundDoseCallback = getSoundDoseCallback();
475
476 if (records.size() > 0 && soundDoseCallback != nullptr) {
477 std::vector<media::SoundDoseRecord> newRecordsToReport;
478 for (const auto& record : records) {
479 newRecordsToReport.emplace_back(csdRecordToSoundDoseRecord(record));
480 }
481
482 soundDoseCallback->onNewCsdValue(currentCsd, newRecordsToReport);
483 }
Vlad Popa2900c0a2022-10-24 13:38:00 +0200484}
485
Vlad Popa4defd0b2022-11-06 14:22:31 +0100486sp<media::ISoundDoseCallback> SoundDoseManager::getSoundDoseCallback() const {
487 std::lock_guard _l(mLock);
Vlad Popae3fd1c22022-11-07 21:03:18 +0100488 if (mSoundDose == nullptr) {
489 return nullptr;
490 }
491
492 return mSoundDose->mSoundDoseCallback;
Vlad Popa4defd0b2022-11-06 14:22:31 +0100493}
494
495void SoundDoseManager::onMomentaryExposure(float currentMel, audio_port_handle_t deviceId) const {
496 ALOGV("%s: Momentary exposure for device %d triggered: %f MEL", __func__, deviceId, currentMel);
Vlad Popa63f047e2022-11-05 14:09:19 +0100497
Vlad Popa2a06fca2023-02-06 16:45:45 +0100498 {
499 std::lock_guard _l(mLock);
Vlad Popa617bbf02023-04-24 19:10:36 +0200500 if (!mEnabledCsd) {
Vlad Popa2a06fca2023-02-06 16:45:45 +0100501 return;
502 }
503 }
504
Vlad Popae3fd1c22022-11-07 21:03:18 +0100505 auto soundDoseCallback = getSoundDoseCallback();
Vlad Popa63f047e2022-11-05 14:09:19 +0100506 if (soundDoseCallback != nullptr) {
Vlad Popae3fd1c22022-11-07 21:03:18 +0100507 soundDoseCallback->onMomentaryExposure(currentMel, deviceId);
Vlad Popa63f047e2022-11-05 14:09:19 +0100508 }
509}
510
Vlad Popae3fd1c22022-11-07 21:03:18 +0100511sp<media::ISoundDose> SoundDoseManager::getSoundDoseInterface(
512 const sp<media::ISoundDoseCallback>& callback) {
Vlad Popa63f047e2022-11-05 14:09:19 +0100513 ALOGV("%s: Register ISoundDoseCallback", __func__);
514
515 std::lock_guard _l(mLock);
Vlad Popae3fd1c22022-11-07 21:03:18 +0100516 if (mSoundDose == nullptr) {
517 mSoundDose = sp<SoundDose>::make(this, callback);
518 }
519 return mSoundDose;
Vlad Popaf09e93f2022-10-31 16:27:12 +0100520}
521
Vlad Popa4defd0b2022-11-06 14:22:31 +0100522std::string SoundDoseManager::dump() const {
Vlad Popa2900c0a2022-10-24 13:38:00 +0200523 std::string output;
Vlad Popa2a06fca2023-02-06 16:45:45 +0100524 {
525 std::lock_guard _l(mLock);
Vlad Popa617bbf02023-04-24 19:10:36 +0200526 if (!mEnabledCsd) {
Vlad Popa2a06fca2023-02-06 16:45:45 +0100527 base::StringAppendF(&output, "CSD is disabled");
528 return output;
529 }
530 }
531
Vlad Popaf09e93f2022-10-31 16:27:12 +0100532 mMelAggregator->foreachCsd([&output](audio_utils::CsdRecord csdRecord) {
Vlad Popa2900c0a2022-10-24 13:38:00 +0200533 base::StringAppendF(&output,
534 "CSD %f with average MEL %f in interval [%" PRId64 ", %" PRId64 "]",
Vlad Popa4defd0b2022-11-06 14:22:31 +0100535 csdRecord.value, csdRecord.averageMel, csdRecord.timestamp,
Vlad Popa2900c0a2022-10-24 13:38:00 +0200536 csdRecord.timestamp + csdRecord.duration);
537 base::StringAppendF(&output, "\n");
538 });
539
540 base::StringAppendF(&output, "\nCached Mel Records:\n");
Vlad Popaf09e93f2022-10-31 16:27:12 +0100541 mMelAggregator->foreachCachedMel([&output](const audio_utils::MelRecord& melRecord) {
Vlad Popa2900c0a2022-10-24 13:38:00 +0200542 base::StringAppendF(&output, "Continuous MELs for portId=%d, ", melRecord.portId);
543 base::StringAppendF(&output, "starting at timestamp %" PRId64 ": ", melRecord.timestamp);
544
545 for (const auto& mel : melRecord.mels) {
546 base::StringAppendF(&output, "%.2f ", mel);
547 }
548 base::StringAppendF(&output, "\n");
549 });
550
551 return output;
552}
553
554size_t SoundDoseManager::getCachedMelRecordsSize() const {
Vlad Popaf09e93f2022-10-31 16:27:12 +0100555 return mMelAggregator->getCachedMelRecordsSize();
Vlad Popa2900c0a2022-10-24 13:38:00 +0200556}
557
Vlad Popa4defd0b2022-11-06 14:22:31 +0100558media::SoundDoseRecord SoundDoseManager::csdRecordToSoundDoseRecord(
559 const audio_utils::CsdRecord& legacy) {
560 media::SoundDoseRecord soundDoseRecord{};
561 soundDoseRecord.timestamp = legacy.timestamp;
562 soundDoseRecord.duration = legacy.duration;
563 soundDoseRecord.value = legacy.value;
564 soundDoseRecord.averageMel = legacy.averageMel;
565 return soundDoseRecord;
566}
567
Vlad Popa2900c0a2022-10-24 13:38:00 +0200568} // namespace android