blob: 1c75119a95553e38637a04068e5acebb5d9607fe [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#if !defined(BACKEND_NDK)
24#define BACKEND_NDK
25#endif
26
27#include "android/media/SoundDoseRecord.h"
Vlad Popa2900c0a2022-10-24 13:38:00 +020028#include <android-base/stringprintf.h>
Vlad Popa1d5f0d52022-12-18 12:21:26 +010029#include <media/AidlConversionCppNdk.h>
30#include <cinttypes>
Vlad Popa2900c0a2022-10-24 13:38:00 +020031#include <time.h>
Vlad Popa4defd0b2022-11-06 14:22:31 +010032#include <utils/Log.h>
Vlad Popa2900c0a2022-10-24 13:38:00 +020033
34namespace android {
35
Vlad Popa1d5f0d52022-12-18 12:21:26 +010036using aidl::android::media::audio::common::AudioDevice;
37using aidl::android::media::audio::common::AudioDeviceAddress;
38
Vlad Popa2900c0a2022-10-24 13:38:00 +020039namespace {
40
41int64_t getMonotonicSecond() {
42 struct timespec now_ts;
43 if (clock_gettime(CLOCK_MONOTONIC, &now_ts) != 0) {
44 ALOGE("%s: cannot get timestamp", __func__);
45 return -1;
46 }
47 return now_ts.tv_sec;
48}
49
50} // namespace
51
Vlad Popaf09e93f2022-10-31 16:27:12 +010052sp<audio_utils::MelProcessor> SoundDoseManager::getOrCreateProcessorForDevice(
Vlad Popa4defd0b2022-11-06 14:22:31 +010053 audio_port_handle_t deviceId, audio_io_handle_t streamHandle, uint32_t sampleRate,
54 size_t channelCount, audio_format_t format) {
Vlad Popa2900c0a2022-10-24 13:38:00 +020055 std::lock_guard _l(mLock);
56
Vlad Popa1d5f0d52022-12-18 12:21:26 +010057 if (mHalSoundDose != nullptr) {
58 ALOGW("%s: using HAL MEL computation, no MelProcessor needed.", __func__);
59 return nullptr;
60 }
61
Vlad Popaf09e93f2022-10-31 16:27:12 +010062 auto streamProcessor = mActiveProcessors.find(streamHandle);
63 sp<audio_utils::MelProcessor> processor;
Vlad Popa4defd0b2022-11-06 14:22:31 +010064 if (streamProcessor != mActiveProcessors.end() &&
65 (processor = streamProcessor->second.promote())) {
Vlad Popa2900c0a2022-10-24 13:38:00 +020066 ALOGV("%s: found callback for stream %d", __func__, streamHandle);
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 {
71 ALOGV("%s: creating new callback for device %d", __func__, streamHandle);
Vlad Popa4defd0b2022-11-06 14:22:31 +010072 sp<audio_utils::MelProcessor> melProcessor = sp<audio_utils::MelProcessor>::make(
73 sampleRate, channelCount, format, *this, deviceId, mRs2Value);
Vlad Popaf09e93f2022-10-31 16:27:12 +010074 mActiveProcessors[streamHandle] = melProcessor;
75 return melProcessor;
Vlad Popa2900c0a2022-10-24 13:38:00 +020076 }
77}
78
Vlad Popa1d5f0d52022-12-18 12:21:26 +010079bool SoundDoseManager::setHalSoundDoseInterface(const std::shared_ptr<ISoundDose>& halSoundDose) {
80 ALOGV("%s", __func__);
81
82 {
83 std::lock_guard _l(mLock);
84
85 mHalSoundDose = halSoundDose;
86 if (halSoundDose == nullptr) {
87 ALOGI("%s: passed ISoundDose object is null, switching to internal CSD", __func__);
88 return false;
89 }
90
91 if (!mHalSoundDose->setOutputRs2(mRs2Value).isOk()) {
92 ALOGW("%s: Cannot set RS2 value for momentary exposure %f",
93 __func__,
94 mRs2Value);
95 }
96
97 // initialize the HAL sound dose callback lazily
98 if (mHalSoundDoseCallback == nullptr) {
99 mHalSoundDoseCallback =
100 ndk::SharedRefBase::make<HalSoundDoseCallback>(this);
101 }
102 }
103
104 auto status = halSoundDose->registerSoundDoseCallback(mHalSoundDoseCallback);
105 if (!status.isOk()) {
106 // Not a warning since this can happen if the callback was registered before
107 ALOGI("%s: Cannot register HAL sound dose callback with status message: %s",
108 __func__,
109 status.getMessage());
110 }
111
112 return true;
113}
114
Vlad Popa4defd0b2022-11-06 14:22:31 +0100115void SoundDoseManager::setOutputRs2(float rs2Value) {
Vlad Popa2900c0a2022-10-24 13:38:00 +0200116 ALOGV("%s", __func__);
Vlad Popaf09e93f2022-10-31 16:27:12 +0100117 std::lock_guard _l(mLock);
118
Vlad Popae3fd1c22022-11-07 21:03:18 +0100119 mRs2Value = rs2Value;
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100120 if (mHalSoundDose != nullptr) {
121 // using the HAL sound dose interface
122 if (!mHalSoundDose->setOutputRs2(mRs2Value).isOk()) {
123 ALOGE("%s: Cannot set RS2 value for momentary exposure %f", __func__, mRs2Value);
124 }
125 return;
126 }
Vlad Popae3fd1c22022-11-07 21:03:18 +0100127
Vlad Popaf09e93f2022-10-31 16:27:12 +0100128 for (auto& streamProcessor : mActiveProcessors) {
129 sp<audio_utils::MelProcessor> processor = streamProcessor.second.promote();
130 if (processor != nullptr) {
Vlad Popae3fd1c22022-11-07 21:03:18 +0100131 status_t result = processor->setOutputRs2(mRs2Value);
Vlad Popaf09e93f2022-10-31 16:27:12 +0100132 if (result != NO_ERROR) {
Vlad Popae3fd1c22022-11-07 21:03:18 +0100133 ALOGW("%s: could not set RS2 value %f for stream %d", __func__, mRs2Value,
Vlad Popaf09e93f2022-10-31 16:27:12 +0100134 streamProcessor.first);
135 }
136 }
137 }
138}
139
Vlad Popa4defd0b2022-11-06 14:22:31 +0100140void SoundDoseManager::removeStreamProcessor(audio_io_handle_t streamHandle) {
Vlad Popaf09e93f2022-10-31 16:27:12 +0100141 std::lock_guard _l(mLock);
142 auto callbackToRemove = mActiveProcessors.find(streamHandle);
Vlad Popa4defd0b2022-11-06 14:22:31 +0100143 if (callbackToRemove != mActiveProcessors.end()) {
Vlad Popaf09e93f2022-10-31 16:27:12 +0100144 mActiveProcessors.erase(callbackToRemove);
145 }
146}
147
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100148audio_port_handle_t SoundDoseManager::getIdForAudioDevice(const AudioDevice& audioDevice) const {
149 std::lock_guard _l(mLock);
150
151 audio_devices_t type;
152 std::string address;
153 auto result = aidl::android::aidl2legacy_AudioDevice_audio_device(
154 audioDevice, &type, &address);
155 if (result != NO_ERROR) {
156 ALOGE("%s: could not convert from AudioDevice to AudioDeviceTypeAddr", __func__);
157 return AUDIO_PORT_HANDLE_NONE;
158 }
159
160 auto adt = AudioDeviceTypeAddr(type, address);
161 auto deviceIt = mActiveDevices.find(adt);
162 if (deviceIt == mActiveDevices.end()) {
Vlad Popa7e81cea2023-01-19 16:34:16 +0100163 ALOGI("%s: could not find port id for device %s", __func__, adt.toString().c_str());
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100164 return AUDIO_PORT_HANDLE_NONE;
165 }
166 return deviceIt->second;
167}
168
169void SoundDoseManager::mapAddressToDeviceId(const AudioDeviceTypeAddr& adt,
170 const audio_port_handle_t deviceId) {
171 std::lock_guard _l(mLock);
172 ALOGI("%s: map address: %s to device id: %d", __func__, adt.toString().c_str(), deviceId);
173 mActiveDevices[adt] = deviceId;
174}
175
176void SoundDoseManager::clearMapDeviceIdEntries(audio_port_handle_t deviceId) {
177 std::lock_guard _l(mLock);
178 for (auto activeDevice = mActiveDevices.begin(); activeDevice != mActiveDevices.end();) {
179 if (activeDevice->second == deviceId) {
180 ALOGI("%s: clear mapping addr: %s to deviceId: %d",
181 __func__, activeDevice->first.toString().c_str(), deviceId);
182 activeDevice = mActiveDevices.erase(activeDevice);
183 continue;
184 }
185 ++activeDevice;
186 }
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100187}
188
189ndk::ScopedAStatus SoundDoseManager::HalSoundDoseCallback::onMomentaryExposureWarning(
190 float in_currentDbA, const AudioDevice& in_audioDevice) {
191 auto soundDoseManager = mSoundDoseManager.promote();
192 if (soundDoseManager == nullptr) {
193 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
194 }
195
Vlad Popa3d6d39d2022-12-21 18:59:16 +0100196 std::shared_ptr<ISoundDose> halSoundDose;
197 soundDoseManager->getHalSoundDose(&halSoundDose);
198 if(halSoundDose == nullptr) {
199 ALOGW("%s: HAL sound dose interface deactivated. Ignoring", __func__);
200 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
201 }
202
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100203 auto id = soundDoseManager->getIdForAudioDevice(in_audioDevice);
204 if (id == AUDIO_PORT_HANDLE_NONE) {
Vlad Popa7e81cea2023-01-19 16:34:16 +0100205 ALOGI("%s: no mapped id for audio device with type %d and address %s",
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100206 __func__, in_audioDevice.type.type,
207 in_audioDevice.address.get<AudioDeviceAddress::id>().c_str());
208 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
209 }
210 soundDoseManager->onMomentaryExposure(in_currentDbA, id);
211
212 return ndk::ScopedAStatus::ok();
213}
214
215ndk::ScopedAStatus SoundDoseManager::HalSoundDoseCallback::onNewMelValues(
216 const ISoundDose::IHalSoundDoseCallback::MelRecord& in_melRecord,
217 const AudioDevice& in_audioDevice) {
218 auto soundDoseManager = mSoundDoseManager.promote();
219 if (soundDoseManager == nullptr) {
220 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
221 }
Vlad Popa3d6d39d2022-12-21 18:59:16 +0100222
223 std::shared_ptr<ISoundDose> halSoundDose;
224 soundDoseManager->getHalSoundDose(&halSoundDose);
225 if(halSoundDose == nullptr) {
226 ALOGW("%s: HAL sound dose interface deactivated. Ignoring", __func__);
227 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
228 }
229
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100230 auto id = soundDoseManager->getIdForAudioDevice(in_audioDevice);
231 if (id == AUDIO_PORT_HANDLE_NONE) {
Vlad Popa7e81cea2023-01-19 16:34:16 +0100232 ALOGI("%s: no mapped id for audio device with type %d and address %s",
Vlad Popa1d5f0d52022-12-18 12:21:26 +0100233 __func__, in_audioDevice.type.type,
234 in_audioDevice.address.get<AudioDeviceAddress::id>().c_str());
235 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
236 }
237 // TODO: introduce timestamp in onNewMelValues callback
238 soundDoseManager->onNewMelValues(in_melRecord.melValues, 0,
239 in_melRecord.melValues.size(), id);
240
241 return ndk::ScopedAStatus::ok();
242}
243
Vlad Popae3fd1c22022-11-07 21:03:18 +0100244void SoundDoseManager::SoundDose::binderDied(__unused const wp<IBinder>& who) {
245 ALOGV("%s", __func__);
246
247 auto soundDoseManager = mSoundDoseManager.promote();
248 if (soundDoseManager != nullptr) {
249 soundDoseManager->resetSoundDose();
250 }
251}
252
253binder::Status SoundDoseManager::SoundDose::setOutputRs2(float value) {
254 ALOGV("%s", __func__);
255 auto soundDoseManager = mSoundDoseManager.promote();
256 if (soundDoseManager != nullptr) {
257 soundDoseManager->setOutputRs2(value);
258 }
259 return binder::Status::ok();
260}
261
262binder::Status SoundDoseManager::SoundDose::resetCsd(
263 float currentCsd, const std::vector<media::SoundDoseRecord>& records) {
264 ALOGV("%s", __func__);
265 auto soundDoseManager = mSoundDoseManager.promote();
266 if (soundDoseManager != nullptr) {
267 soundDoseManager->resetCsd(currentCsd, records);
268 }
269 return binder::Status::ok();
270}
271
Vlad Popa91930462022-12-20 22:42:48 +0100272binder::Status SoundDoseManager::SoundDose::getOutputRs2(float* value) {
273 ALOGV("%s", __func__);
274 auto soundDoseManager = mSoundDoseManager.promote();
275 if (soundDoseManager != nullptr) {
276 std::lock_guard _l(soundDoseManager->mLock);
277 *value = soundDoseManager->mRs2Value;
278 }
279 return binder::Status::ok();
280}
281
282binder::Status SoundDoseManager::SoundDose::getCsd(float* value) {
283 ALOGV("%s", __func__);
284 auto soundDoseManager = mSoundDoseManager.promote();
285 if (soundDoseManager != nullptr) {
286 *value = soundDoseManager->mMelAggregator->getCsd();
287 }
288 return binder::Status::ok();
289}
290
291binder::Status SoundDoseManager::SoundDose::forceUseFrameworkMel(bool useFrameworkMel) {
292 ALOGV("%s", __func__);
293 auto soundDoseManager = mSoundDoseManager.promote();
294 if (soundDoseManager != nullptr) {
295 soundDoseManager->setUseFrameworkMel(useFrameworkMel);
296 }
297 return binder::Status::ok();
298}
299
300binder::Status SoundDoseManager::SoundDose::forceComputeCsdOnAllDevices(
301 bool computeCsdOnAllDevices) {
302 ALOGV("%s", __func__);
303 auto soundDoseManager = mSoundDoseManager.promote();
304 if (soundDoseManager != nullptr) {
305 soundDoseManager->setComputeCsdOnAllDevices(computeCsdOnAllDevices);
306 }
307 return binder::Status::ok();
308}
309
310void SoundDoseManager::setUseFrameworkMel(bool useFrameworkMel) {
Vlad Popa3d6d39d2022-12-21 18:59:16 +0100311 // invalidate any HAL sound dose interface used
312 setHalSoundDoseInterface(nullptr);
313
Vlad Popa91930462022-12-20 22:42:48 +0100314 std::lock_guard _l(mLock);
315 mUseFrameworkMel = useFrameworkMel;
316}
317
Vlad Popa3d6d39d2022-12-21 18:59:16 +0100318bool SoundDoseManager::forceUseFrameworkMel() const {
Vlad Popa91930462022-12-20 22:42:48 +0100319 std::lock_guard _l(mLock);
320 return mUseFrameworkMel;
321}
322
323void SoundDoseManager::setComputeCsdOnAllDevices(bool computeCsdOnAllDevices) {
324 std::lock_guard _l(mLock);
325 mComputeCsdOnAllDevices = computeCsdOnAllDevices;
326}
327
Vlad Popa3d6d39d2022-12-21 18:59:16 +0100328bool SoundDoseManager::forceComputeCsdOnAllDevices() const {
Vlad Popa91930462022-12-20 22:42:48 +0100329 std::lock_guard _l(mLock);
330 return mComputeCsdOnAllDevices;
331}
332
Vlad Popa3d6d39d2022-12-21 18:59:16 +0100333void SoundDoseManager::getHalSoundDose(std::shared_ptr<ISoundDose>* halSoundDose) const {
334 std::lock_guard _l(mLock);
335 *halSoundDose = mHalSoundDose;
336}
337
Vlad Popae3fd1c22022-11-07 21:03:18 +0100338void SoundDoseManager::resetSoundDose() {
339 std::lock_guard lock(mLock);
340 mSoundDose = nullptr;
341}
342
343void SoundDoseManager::resetCsd(float currentCsd,
344 const std::vector<media::SoundDoseRecord>& records) {
345 std::lock_guard lock(mLock);
346 std::vector<audio_utils::CsdRecord> resetRecords;
347 for (const auto& record : records) {
348 resetRecords.emplace_back(record.timestamp, record.duration, record.value,
349 record.averageMel);
350 }
351
352 mMelAggregator->reset(currentCsd, resetRecords);
353}
354
Vlad Popa4defd0b2022-11-06 14:22:31 +0100355void SoundDoseManager::onNewMelValues(const std::vector<float>& mels, size_t offset, size_t length,
356 audio_port_handle_t deviceId) const {
Vlad Popaf09e93f2022-10-31 16:27:12 +0100357 ALOGV("%s", __func__);
Vlad Popa2900c0a2022-10-24 13:38:00 +0200358
Vlad Popa4defd0b2022-11-06 14:22:31 +0100359 sp<media::ISoundDoseCallback> soundDoseCallback;
360 std::vector<audio_utils::CsdRecord> records;
361 float currentCsd;
362 {
363 std::lock_guard _l(mLock);
Vlad Popa2900c0a2022-10-24 13:38:00 +0200364
Vlad Popa4defd0b2022-11-06 14:22:31 +0100365 int64_t timestampSec = getMonotonicSecond();
366
367 // only for internal callbacks
368 records = mMelAggregator->aggregateAndAddNewMelRecord(audio_utils::MelRecord(
369 deviceId, std::vector<float>(mels.begin() + offset, mels.begin() + offset + length),
370 timestampSec - length));
371
372 currentCsd = mMelAggregator->getCsd();
373 }
374
375 soundDoseCallback = getSoundDoseCallback();
376
377 if (records.size() > 0 && soundDoseCallback != nullptr) {
378 std::vector<media::SoundDoseRecord> newRecordsToReport;
379 for (const auto& record : records) {
380 newRecordsToReport.emplace_back(csdRecordToSoundDoseRecord(record));
381 }
382
383 soundDoseCallback->onNewCsdValue(currentCsd, newRecordsToReport);
384 }
Vlad Popa2900c0a2022-10-24 13:38:00 +0200385}
386
Vlad Popa4defd0b2022-11-06 14:22:31 +0100387sp<media::ISoundDoseCallback> SoundDoseManager::getSoundDoseCallback() const {
388 std::lock_guard _l(mLock);
Vlad Popae3fd1c22022-11-07 21:03:18 +0100389 if (mSoundDose == nullptr) {
390 return nullptr;
391 }
392
393 return mSoundDose->mSoundDoseCallback;
Vlad Popa4defd0b2022-11-06 14:22:31 +0100394}
395
396void SoundDoseManager::onMomentaryExposure(float currentMel, audio_port_handle_t deviceId) const {
397 ALOGV("%s: Momentary exposure for device %d triggered: %f MEL", __func__, deviceId, currentMel);
Vlad Popa63f047e2022-11-05 14:09:19 +0100398
Vlad Popae3fd1c22022-11-07 21:03:18 +0100399 auto soundDoseCallback = getSoundDoseCallback();
Vlad Popa63f047e2022-11-05 14:09:19 +0100400 if (soundDoseCallback != nullptr) {
Vlad Popae3fd1c22022-11-07 21:03:18 +0100401 soundDoseCallback->onMomentaryExposure(currentMel, deviceId);
Vlad Popa63f047e2022-11-05 14:09:19 +0100402 }
403}
404
Vlad Popae3fd1c22022-11-07 21:03:18 +0100405sp<media::ISoundDose> SoundDoseManager::getSoundDoseInterface(
406 const sp<media::ISoundDoseCallback>& callback) {
Vlad Popa63f047e2022-11-05 14:09:19 +0100407 ALOGV("%s: Register ISoundDoseCallback", __func__);
408
409 std::lock_guard _l(mLock);
Vlad Popae3fd1c22022-11-07 21:03:18 +0100410 if (mSoundDose == nullptr) {
411 mSoundDose = sp<SoundDose>::make(this, callback);
412 }
413 return mSoundDose;
Vlad Popaf09e93f2022-10-31 16:27:12 +0100414}
415
Vlad Popa4defd0b2022-11-06 14:22:31 +0100416std::string SoundDoseManager::dump() const {
Vlad Popa2900c0a2022-10-24 13:38:00 +0200417 std::string output;
Vlad Popaf09e93f2022-10-31 16:27:12 +0100418 mMelAggregator->foreachCsd([&output](audio_utils::CsdRecord csdRecord) {
Vlad Popa2900c0a2022-10-24 13:38:00 +0200419 base::StringAppendF(&output,
420 "CSD %f with average MEL %f in interval [%" PRId64 ", %" PRId64 "]",
Vlad Popa4defd0b2022-11-06 14:22:31 +0100421 csdRecord.value, csdRecord.averageMel, csdRecord.timestamp,
Vlad Popa2900c0a2022-10-24 13:38:00 +0200422 csdRecord.timestamp + csdRecord.duration);
423 base::StringAppendF(&output, "\n");
424 });
425
426 base::StringAppendF(&output, "\nCached Mel Records:\n");
Vlad Popaf09e93f2022-10-31 16:27:12 +0100427 mMelAggregator->foreachCachedMel([&output](const audio_utils::MelRecord& melRecord) {
Vlad Popa2900c0a2022-10-24 13:38:00 +0200428 base::StringAppendF(&output, "Continuous MELs for portId=%d, ", melRecord.portId);
429 base::StringAppendF(&output, "starting at timestamp %" PRId64 ": ", melRecord.timestamp);
430
431 for (const auto& mel : melRecord.mels) {
432 base::StringAppendF(&output, "%.2f ", mel);
433 }
434 base::StringAppendF(&output, "\n");
435 });
436
437 return output;
438}
439
440size_t SoundDoseManager::getCachedMelRecordsSize() const {
Vlad Popaf09e93f2022-10-31 16:27:12 +0100441 return mMelAggregator->getCachedMelRecordsSize();
Vlad Popa2900c0a2022-10-24 13:38:00 +0200442}
443
Vlad Popa4defd0b2022-11-06 14:22:31 +0100444media::SoundDoseRecord SoundDoseManager::csdRecordToSoundDoseRecord(
445 const audio_utils::CsdRecord& legacy) {
446 media::SoundDoseRecord soundDoseRecord{};
447 soundDoseRecord.timestamp = legacy.timestamp;
448 soundDoseRecord.duration = legacy.duration;
449 soundDoseRecord.value = legacy.value;
450 soundDoseRecord.averageMel = legacy.averageMel;
451 return soundDoseRecord;
452}
453
Vlad Popa2900c0a2022-10-24 13:38:00 +0200454} // namespace android