blob: 754b5693cba7740898b8919a441e9eeb09f01f04 [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#pragma once
19
Vlad Popa63f047e2022-11-05 14:09:19 +010020#include <android/media/ISoundDoseCallback.h>
Vlad Popa2900c0a2022-10-24 13:38:00 +020021#include <audio_utils/MelProcessor.h>
22#include <audio_utils/MelAggregator.h>
23#include <mutex>
24#include <unordered_map>
Vlad Popaf09e93f2022-10-31 16:27:12 +010025#include <utils/Errors.h>
Vlad Popa2900c0a2022-10-24 13:38:00 +020026
27namespace android {
28
Vlad Popaf09e93f2022-10-31 16:27:12 +010029class SoundDoseManager : public audio_utils::MelProcessor::MelCallback {
Vlad Popa2900c0a2022-10-24 13:38:00 +020030public:
Vlad Popaf09e93f2022-10-31 16:27:12 +010031 /** CSD is computed with a rolling window of 7 days. */
32 static constexpr int64_t kCsdWindowSeconds = 604800; // 60s * 60m * 24h * 7d
33 /** Default RS2 value in dBA as defined in IEC 62368-1 3rd edition. */
34 static constexpr float kDefaultRs2Value = 100.f;
35
36 SoundDoseManager()
37 : mMelAggregator(sp<audio_utils::MelAggregator>::make(kCsdWindowSeconds)),
38 mRs2Value(kDefaultRs2Value) {};
Vlad Popa2900c0a2022-10-24 13:38:00 +020039
40 /**
Vlad Popaf09e93f2022-10-31 16:27:12 +010041 * \brief Creates or gets the MelProcessor assigned to the streamHandle
Vlad Popa2900c0a2022-10-24 13:38:00 +020042 *
43 * \param deviceId id for the devices where the stream is active.
44 * \param streanHandle handle to the stream
Vlad Popaf09e93f2022-10-31 16:27:12 +010045 * \param sampleRate sample rate for the processor
46 * \param channelCount number of channels to be processed.
47 * \param format format of the input samples.
48 *
49 * \return MelProcessor assigned to the stream and device id.
Vlad Popa2900c0a2022-10-24 13:38:00 +020050 */
Vlad Popaf09e93f2022-10-31 16:27:12 +010051 sp<audio_utils::MelProcessor> getOrCreateProcessorForDevice(
Vlad Popa2900c0a2022-10-24 13:38:00 +020052 audio_port_handle_t deviceId,
Vlad Popaf09e93f2022-10-31 16:27:12 +010053 audio_io_handle_t streamHandle,
54 uint32_t sampleRate,
55 size_t channelCount,
56 audio_format_t format);
Vlad Popa2900c0a2022-10-24 13:38:00 +020057
58 /**
Vlad Popaf09e93f2022-10-31 16:27:12 +010059 * \brief Removes stream processor when MEL computation is not needed anymore
Vlad Popa2900c0a2022-10-24 13:38:00 +020060 *
61 * \param streanHandle handle to the stream
62 */
Vlad Popaf09e93f2022-10-31 16:27:12 +010063 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 Popa2900c0a2022-10-24 13:38:00 +020072
73 std::string dump() const;
74
Vlad Popa63f047e2022-11-05 14:09:19 +010075 /** \brief Registers the interface for passing callbacks to the AudioService. */
76 void registerSoundDoseCallback(const sp<media::ISoundDoseCallback>& callback);
77
Vlad Popa2900c0a2022-10-24 13:38:00 +020078 // used for testing
79 size_t getCachedMelRecordsSize() const;
Vlad Popaf09e93f2022-10-31 16:27:12 +010080
81 // ------ Override audio_utils::MelProcessor::MelCallback ------
82 void onNewMelValues(const std::vector<float>& mels,
83 size_t offset,
84 size_t length,
85 audio_port_handle_t deviceId) const override;
86
87 void onMomentaryExposure(float currentMel, audio_port_handle_t deviceId) const override;
Vlad Popa2900c0a2022-10-24 13:38:00 +020088private:
Vlad Popaf09e93f2022-10-31 16:27:12 +010089 mutable std::mutex mLock;
Vlad Popa2900c0a2022-10-24 13:38:00 +020090
91 // no need for lock since MelAggregator is thread-safe
Vlad Popaf09e93f2022-10-31 16:27:12 +010092 const sp<audio_utils::MelAggregator> mMelAggregator;
Vlad Popa2900c0a2022-10-24 13:38:00 +020093
Vlad Popaf09e93f2022-10-31 16:27:12 +010094 std::unordered_map<audio_io_handle_t,
95 wp<audio_utils::MelProcessor>> mActiveProcessors GUARDED_BY(mLock);
96
97 float mRs2Value GUARDED_BY(mLock);
Vlad Popa63f047e2022-11-05 14:09:19 +010098
99 sp<media::ISoundDoseCallback> mSoundDoseCallback GUARDED_BY(mLock);
Vlad Popa2900c0a2022-10-24 13:38:00 +0200100};
101
102} // namespace android