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 | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame^] | 20 | #include <android/media/BnSoundDose.h> |
Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 21 | #include <android/media/ISoundDoseCallback.h> |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 22 | #include <audio_utils/MelAggregator.h> |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 23 | #include <audio_utils/MelProcessor.h> |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame^] | 24 | #include <binder/Status.h> |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 25 | #include <mutex> |
| 26 | #include <unordered_map> |
| 27 | |
| 28 | namespace android { |
| 29 | |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 30 | class SoundDoseManager : public audio_utils::MelProcessor::MelCallback { |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 31 | public: |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 32 | /** CSD is computed with a rolling window of 7 days. */ |
| 33 | static constexpr int64_t kCsdWindowSeconds = 604800; // 60s * 60m * 24h * 7d |
| 34 | /** Default RS2 value in dBA as defined in IEC 62368-1 3rd edition. */ |
| 35 | static constexpr float kDefaultRs2Value = 100.f; |
| 36 | |
| 37 | SoundDoseManager() |
| 38 | : mMelAggregator(sp<audio_utils::MelAggregator>::make(kCsdWindowSeconds)), |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 39 | mRs2Value(kDefaultRs2Value){}; |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 40 | |
| 41 | /** |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 42 | * \brief Creates or gets the MelProcessor assigned to the streamHandle |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 43 | * |
| 44 | * \param deviceId id for the devices where the stream is active. |
| 45 | * \param streanHandle handle to the stream |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 46 | * \param sampleRate sample rate for the processor |
| 47 | * \param channelCount number of channels to be processed. |
| 48 | * \param format format of the input samples. |
| 49 | * |
| 50 | * \return MelProcessor assigned to the stream and device id. |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 51 | */ |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 52 | sp<audio_utils::MelProcessor> getOrCreateProcessorForDevice(audio_port_handle_t deviceId, |
| 53 | audio_io_handle_t streamHandle, |
| 54 | uint32_t sampleRate, |
| 55 | size_t channelCount, |
| 56 | audio_format_t format); |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 57 | |
| 58 | /** |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 59 | * \brief Removes stream processor when MEL computation is not needed anymore |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 60 | * |
| 61 | * \param streanHandle handle to the stream |
| 62 | */ |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 63 | void removeStreamProcessor(audio_io_handle_t streamHandle); |
| 64 | |
| 65 | /** |
| 66 | * Sets the output RS2 value for momentary exposure warnings. Must not be |
| 67 | * higher than 100dBA and not lower than 80dBA. |
| 68 | * |
| 69 | * \param rs2Value value to use for momentary exposure |
| 70 | */ |
| 71 | void setOutputRs2(float rs2Value); |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 72 | |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame^] | 73 | /** |
| 74 | * \brief Registers the interface for passing callbacks to the AudioService and gets |
| 75 | * the ISoundDose interface. |
| 76 | * |
| 77 | * \returns the sound dose binder to send commands to the SoundDoseManager |
| 78 | **/ |
| 79 | sp<media::ISoundDose> getSoundDoseInterface(const sp<media::ISoundDoseCallback>& callback); |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 80 | |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame^] | 81 | std::string dump() const; |
Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 82 | |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 83 | // used for testing |
| 84 | size_t getCachedMelRecordsSize() const; |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 85 | |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 86 | /** Method for converting from audio_utils::CsdRecord to media::SoundDoseRecord. */ |
| 87 | static media::SoundDoseRecord csdRecordToSoundDoseRecord(const audio_utils::CsdRecord& legacy); |
| 88 | |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 89 | // ------ Override audio_utils::MelProcessor::MelCallback ------ |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 90 | 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] | 91 | audio_port_handle_t deviceId) const override; |
| 92 | |
| 93 | void onMomentaryExposure(float currentMel, audio_port_handle_t deviceId) const override; |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 94 | |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame^] | 95 | private: |
| 96 | class SoundDose : public media::BnSoundDose, |
| 97 | public IBinder::DeathRecipient { |
| 98 | public: |
| 99 | SoundDose(SoundDoseManager* manager, const sp<media::ISoundDoseCallback>& callback) |
| 100 | : mSoundDoseManager(manager), |
| 101 | mSoundDoseCallback(callback) {}; |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 102 | |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame^] | 103 | /** IBinder::DeathRecipient. Listen to the death of ISoundDoseCallback. */ |
| 104 | virtual void binderDied(const wp<IBinder>& who); |
| 105 | |
| 106 | /** BnSoundDose override */ |
| 107 | binder::Status setOutputRs2(float value) override; |
| 108 | binder::Status resetCsd(float currentCsd, |
| 109 | const std::vector<media::SoundDoseRecord>& records) override; |
| 110 | |
| 111 | wp<SoundDoseManager> mSoundDoseManager; |
| 112 | const sp<media::ISoundDoseCallback> mSoundDoseCallback; |
| 113 | }; |
| 114 | |
| 115 | void resetSoundDose(); |
| 116 | |
| 117 | void resetCsd(float currentCsd, const std::vector<media::SoundDoseRecord>& records); |
| 118 | |
| 119 | sp<media::ISoundDoseCallback> getSoundDoseCallback() const; |
| 120 | |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 121 | mutable std::mutex mLock; |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 122 | |
| 123 | // no need for lock since MelAggregator is thread-safe |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 124 | const sp<audio_utils::MelAggregator> mMelAggregator; |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 125 | |
Vlad Popa | 4defd0b | 2022-11-06 14:22:31 +0100 | [diff] [blame] | 126 | std::unordered_map<audio_io_handle_t, wp<audio_utils::MelProcessor>> mActiveProcessors |
| 127 | GUARDED_BY(mLock); |
Vlad Popa | f09e93f | 2022-10-31 16:27:12 +0100 | [diff] [blame] | 128 | |
| 129 | float mRs2Value GUARDED_BY(mLock); |
Vlad Popa | 63f047e | 2022-11-05 14:09:19 +0100 | [diff] [blame] | 130 | |
Vlad Popa | e3fd1c2 | 2022-11-07 21:03:18 +0100 | [diff] [blame^] | 131 | sp<SoundDose> mSoundDose GUARDED_BY(mLock); |
Vlad Popa | 2900c0a | 2022-10-24 13:38:00 +0200 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | } // namespace android |