Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 1 | /* |
| 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 | #pragma once |
| 19 | |
Vlad Popa | debe0a7 | 2022-12-28 16:55:13 +0100 | [diff] [blame^] | 20 | #include <aidl/android/hardware/audio/core/sounddose/ISoundDose.h> |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 21 | #include <aidl/android/media/audio/common/AudioDevice.h> |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 22 | #include <android/media/BnSoundDose.h> |
Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 23 | #include <android/media/ISoundDoseCallback.h> |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 24 | #include <media/AudioDeviceTypeAddr.h> |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 25 | #include <audio_utils/MelAggregator.h> |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 26 | #include <audio_utils/MelProcessor.h> |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 27 | #include <binder/Status.h> |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 28 | #include <mutex> |
| 29 | #include <unordered_map> |
| 30 | |
| 31 | namespace android { |
| 32 | |
Vlad Popa | debe0a7 | 2022-12-28 16:55:13 +0100 | [diff] [blame^] | 33 | using aidl::android::hardware::audio::core::sounddose::ISoundDose; |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 34 | |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 35 | class SoundDoseManager : public audio_utils::MelProcessor::MelCallback { |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 36 | public: |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 37 | /** CSD is computed with a rolling window of 7 days. */ |
| 38 | static constexpr int64_t kCsdWindowSeconds = 604800; // 60s * 60m * 24h * 7d |
| 39 | /** Default RS2 value in dBA as defined in IEC 62368-1 3rd edition. */ |
| 40 | static constexpr float kDefaultRs2Value = 100.f; |
| 41 | |
| 42 | SoundDoseManager() |
| 43 | : mMelAggregator(sp<audio_utils::MelAggregator>::make(kCsdWindowSeconds)), |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 44 | mRs2Value(kDefaultRs2Value) {}; |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 45 | |
| 46 | /** |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 47 | * \brief Creates or gets the MelProcessor assigned to the streamHandle |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 48 | * |
| 49 | * \param deviceId id for the devices where the stream is active. |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 50 | * \param streamHandle handle to the stream |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 51 | * \param sampleRate sample rate for the processor |
| 52 | * \param channelCount number of channels to be processed. |
| 53 | * \param format format of the input samples. |
| 54 | * |
| 55 | * \return MelProcessor assigned to the stream and device id. |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 56 | */ |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 57 | sp<audio_utils::MelProcessor> getOrCreateProcessorForDevice(audio_port_handle_t deviceId, |
| 58 | audio_io_handle_t streamHandle, |
| 59 | uint32_t sampleRate, |
| 60 | size_t channelCount, |
| 61 | audio_format_t format); |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 62 | |
| 63 | /** |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 64 | * \brief Removes stream processor when MEL computation is not needed anymore |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 65 | * |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 66 | * \param streamHandle handle to the stream |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 67 | */ |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 68 | void removeStreamProcessor(audio_io_handle_t streamHandle); |
| 69 | |
| 70 | /** |
| 71 | * Sets the output RS2 value for momentary exposure warnings. Must not be |
| 72 | * higher than 100dBA and not lower than 80dBA. |
| 73 | * |
| 74 | * \param rs2Value value to use for momentary exposure |
| 75 | */ |
| 76 | void setOutputRs2(float rs2Value); |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 77 | |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 78 | /** |
| 79 | * \brief Registers the interface for passing callbacks to the AudioService and gets |
| 80 | * the ISoundDose interface. |
| 81 | * |
| 82 | * \returns the sound dose binder to send commands to the SoundDoseManager |
| 83 | **/ |
| 84 | sp<media::ISoundDose> getSoundDoseInterface(const sp<media::ISoundDoseCallback>& callback); |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 85 | |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 86 | /** |
| 87 | * Sets the HAL sound dose interface to use for the MEL computation. Use nullptr |
| 88 | * for using the internal MEL computation. |
| 89 | * |
| 90 | * @return true if setting the HAL sound dose value was successful, false otherwise. |
| 91 | */ |
| 92 | bool setHalSoundDoseInterface(const std::shared_ptr<ISoundDose>& halSoundDose); |
| 93 | |
| 94 | /** Returns the cached audio port id from the active devices. */ |
| 95 | audio_port_handle_t getIdForAudioDevice( |
| 96 | const aidl::android::media::audio::common::AudioDevice& audioDevice) const; |
| 97 | |
| 98 | /** Caches mapping between address and device port id. */ |
| 99 | void mapAddressToDeviceId(const AudioDeviceTypeAddr& adt, |
| 100 | const audio_port_handle_t deviceId); |
| 101 | |
| 102 | /** Clear all map entries with passed audio_port_handle_t. */ |
| 103 | void clearMapDeviceIdEntries(audio_port_handle_t deviceId); |
| 104 | |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 105 | std::string dump() const; |
Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 106 | |
Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 107 | // used for testing only |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 108 | size_t getCachedMelRecordsSize() const; |
Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 109 | bool forceUseFrameworkMel() const; |
| 110 | bool forceComputeCsdOnAllDevices() const; |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 111 | |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 112 | /** Method for converting from audio_utils::CsdRecord to media::SoundDoseRecord. */ |
| 113 | static media::SoundDoseRecord csdRecordToSoundDoseRecord(const audio_utils::CsdRecord& legacy); |
| 114 | |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 115 | // ------ Override audio_utils::MelProcessor::MelCallback ------ |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 116 | void onNewMelValues(const std::vector<float>& mels, size_t offset, size_t length, |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 117 | audio_port_handle_t deviceId) const override; |
| 118 | |
| 119 | void onMomentaryExposure(float currentMel, audio_port_handle_t deviceId) const override; |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 120 | |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 121 | private: |
| 122 | class SoundDose : public media::BnSoundDose, |
| 123 | public IBinder::DeathRecipient { |
| 124 | public: |
| 125 | SoundDose(SoundDoseManager* manager, const sp<media::ISoundDoseCallback>& callback) |
| 126 | : mSoundDoseManager(manager), |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 127 | mSoundDoseCallback(callback) {} |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 128 | |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 129 | /** IBinder::DeathRecipient. Listen to the death of ISoundDoseCallback. */ |
| 130 | virtual void binderDied(const wp<IBinder>& who); |
| 131 | |
| 132 | /** BnSoundDose override */ |
| 133 | binder::Status setOutputRs2(float value) override; |
| 134 | binder::Status resetCsd(float currentCsd, |
| 135 | const std::vector<media::SoundDoseRecord>& records) override; |
Vlad Popa | 9193046 | 2022-12-20 22:42:48 +0100 | [diff] [blame] | 136 | binder::Status getOutputRs2(float* value); |
| 137 | binder::Status getCsd(float* value); |
| 138 | binder::Status forceUseFrameworkMel(bool useFrameworkMel); |
| 139 | binder::Status forceComputeCsdOnAllDevices(bool computeCsdOnAllDevices); |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 140 | |
| 141 | wp<SoundDoseManager> mSoundDoseManager; |
| 142 | const sp<media::ISoundDoseCallback> mSoundDoseCallback; |
| 143 | }; |
| 144 | |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 145 | class HalSoundDoseCallback : public ISoundDose::BnHalSoundDoseCallback { |
| 146 | public: |
| 147 | explicit HalSoundDoseCallback(SoundDoseManager* manager) |
| 148 | : mSoundDoseManager(manager) {} |
| 149 | |
| 150 | ndk::ScopedAStatus onMomentaryExposureWarning( |
| 151 | float in_currentDbA, |
| 152 | const aidl::android::media::audio::common::AudioDevice& in_audioDevice) override; |
| 153 | ndk::ScopedAStatus onNewMelValues( |
| 154 | const ISoundDose::IHalSoundDoseCallback::MelRecord& in_melRecord, |
| 155 | const aidl::android::media::audio::common::AudioDevice& in_audioDevice) override; |
| 156 | |
| 157 | wp<SoundDoseManager> mSoundDoseManager; |
| 158 | }; |
| 159 | |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 160 | void resetSoundDose(); |
| 161 | |
| 162 | void resetCsd(float currentCsd, const std::vector<media::SoundDoseRecord>& records); |
| 163 | |
| 164 | sp<media::ISoundDoseCallback> getSoundDoseCallback() const; |
Vlad Popa | 9193046 | 2022-12-20 22:42:48 +0100 | [diff] [blame] | 165 | |
| 166 | void setUseFrameworkMel(bool useFrameworkMel); |
| 167 | void setComputeCsdOnAllDevices(bool computeCsdOnAllDevices); |
Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 168 | /** Returns the HAL sound dose interface or null if internal MEL computation is used. */ |
| 169 | void getHalSoundDose(std::shared_ptr<ISoundDose>* halSoundDose) const; |
Vlad Popa | 9193046 | 2022-12-20 22:42:48 +0100 | [diff] [blame] | 170 | |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 171 | mutable std::mutex mLock; |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 172 | |
| 173 | // no need for lock since MelAggregator is thread-safe |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 174 | const sp<audio_utils::MelAggregator> mMelAggregator; |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 175 | |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 176 | std::unordered_map<audio_io_handle_t, wp<audio_utils::MelProcessor>> mActiveProcessors |
| 177 | GUARDED_BY(mLock); |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 178 | |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 179 | // map active device address and type to device id |
| 180 | std::map<AudioDeviceTypeAddr, audio_port_handle_t> mActiveDevices GUARDED_BY(mLock); |
| 181 | |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 182 | float mRs2Value GUARDED_BY(mLock); |
Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 183 | |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame] | 184 | sp<SoundDose> mSoundDose GUARDED_BY(mLock); |
Vlad Popa | 9193046 | 2022-12-20 22:42:48 +0100 | [diff] [blame] | 185 | |
Vlad Popa | 1d5f0d5 | 2022-12-18 12:21:26 +0100 | [diff] [blame] | 186 | std::shared_ptr<ISoundDose> mHalSoundDose GUARDED_BY(mLock); |
| 187 | std::shared_ptr<HalSoundDoseCallback> mHalSoundDoseCallback GUARDED_BY(mLock); |
| 188 | |
Vlad Popa | 3d6d39d | 2022-12-21 18:59:16 +0100 | [diff] [blame] | 189 | bool mUseFrameworkMel GUARDED_BY(mLock) = false; |
| 190 | bool mComputeCsdOnAllDevices GUARDED_BY(mLock) = false; |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | } // namespace android |