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 | |
| 20 | #include <audio_utils/MelProcessor.h> |
| 21 | #include <audio_utils/MelAggregator.h> |
| 22 | #include <mutex> |
| 23 | #include <unordered_map> |
| 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | /** CSD is computed with a rolling window of 7 days. */ |
| 28 | constexpr int64_t kCsdWindowSeconds = 604800; // 60 * 60 * 24 * 7 |
| 29 | |
| 30 | class SoundDoseManager { |
| 31 | public: |
| 32 | SoundDoseManager() : mMelAggregator(kCsdWindowSeconds) {}; |
| 33 | |
| 34 | /** |
| 35 | * \brief Creates or gets the callback assigned to the streamHandle |
| 36 | * |
| 37 | * \param deviceId id for the devices where the stream is active. |
| 38 | * \param streanHandle handle to the stream |
| 39 | */ |
| 40 | sp<audio_utils::MelProcessor::MelCallback> getOrCreateCallbackForDevice( |
| 41 | audio_port_handle_t deviceId, |
| 42 | audio_io_handle_t streamHandle); |
| 43 | |
| 44 | /** |
| 45 | * \brief Removes stream callback when MEL computation is not needed anymore |
| 46 | * |
| 47 | * \param streanHandle handle to the stream |
| 48 | */ |
| 49 | void removeStreamCallback(audio_io_handle_t streamHandle); |
| 50 | |
| 51 | std::string dump() const; |
| 52 | |
| 53 | // used for testing |
| 54 | size_t getCachedMelRecordsSize() const; |
| 55 | private: |
| 56 | /** |
| 57 | * An implementation of the MelProcessor::MelCallback that is assigned to a |
| 58 | * specific device. |
| 59 | */ |
| 60 | class Callback : public audio_utils::MelProcessor::MelCallback { |
| 61 | public: |
| 62 | Callback(SoundDoseManager& soundDoseManager, audio_port_handle_t deviceHandle) |
| 63 | : mSoundDoseManager(soundDoseManager), mDeviceHandle(deviceHandle) {} |
| 64 | |
| 65 | void onNewMelValues(const std::vector<float>& mels, |
| 66 | size_t offset, |
| 67 | size_t length) const override; |
| 68 | |
| 69 | SoundDoseManager& mSoundDoseManager; |
| 70 | audio_port_handle_t mDeviceHandle; |
| 71 | }; |
| 72 | |
| 73 | // no need for lock since MelAggregator is thread-safe |
| 74 | audio_utils::MelAggregator mMelAggregator; |
| 75 | |
| 76 | std::mutex mLock; |
| 77 | std::unordered_map<audio_io_handle_t, sp<Callback>> mActiveCallbacks GUARDED_BY(mLock); |
| 78 | }; |
| 79 | |
| 80 | } // namespace android |