blob: 9a95e8aed195921b15ce139f2ec84710ba1475ee [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/MelAggregator.h>
Vlad Popa4defd0b2022-11-06 14:22:31 +010022#include <audio_utils/MelProcessor.h>
23#include <utils/Errors.h>
Vlad Popa2900c0a2022-10-24 13:38:00 +020024#include <mutex>
25#include <unordered_map>
26
27namespace android {
28
Vlad Popaf09e93f2022-10-31 16:27:12 +010029class SoundDoseManager : public audio_utils::MelProcessor::MelCallback {
Vlad Popa4defd0b2022-11-06 14:22:31 +010030 public:
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)),
Vlad Popa4defd0b2022-11-06 14:22:31 +010038 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 Popa4defd0b2022-11-06 14:22:31 +010051 sp<audio_utils::MelProcessor> getOrCreateProcessorForDevice(audio_port_handle_t deviceId,
52 audio_io_handle_t streamHandle,
53 uint32_t sampleRate,
54 size_t channelCount,
55 audio_format_t format);
Vlad Popa2900c0a2022-10-24 13:38:00 +020056
57 /**
Vlad Popaf09e93f2022-10-31 16:27:12 +010058 * \brief Removes stream processor when MEL computation is not needed anymore
Vlad Popa2900c0a2022-10-24 13:38:00 +020059 *
60 * \param streanHandle handle to the stream
61 */
Vlad Popaf09e93f2022-10-31 16:27:12 +010062 void removeStreamProcessor(audio_io_handle_t streamHandle);
63
64 /**
65 * Sets the output RS2 value for momentary exposure warnings. Must not be
66 * higher than 100dBA and not lower than 80dBA.
67 *
68 * \param rs2Value value to use for momentary exposure
69 */
70 void setOutputRs2(float rs2Value);
Vlad Popa2900c0a2022-10-24 13:38:00 +020071
72 std::string dump() const;
73
Vlad Popa63f047e2022-11-05 14:09:19 +010074 /** \brief Registers the interface for passing callbacks to the AudioService. */
75 void registerSoundDoseCallback(const sp<media::ISoundDoseCallback>& callback);
76
Vlad Popa2900c0a2022-10-24 13:38:00 +020077 // used for testing
78 size_t getCachedMelRecordsSize() const;
Vlad Popaf09e93f2022-10-31 16:27:12 +010079
Vlad Popa4defd0b2022-11-06 14:22:31 +010080 /** Method for converting from audio_utils::CsdRecord to media::SoundDoseRecord. */
81 static media::SoundDoseRecord csdRecordToSoundDoseRecord(const audio_utils::CsdRecord& legacy);
82
Vlad Popaf09e93f2022-10-31 16:27:12 +010083 // ------ Override audio_utils::MelProcessor::MelCallback ------
Vlad Popa4defd0b2022-11-06 14:22:31 +010084 void onNewMelValues(const std::vector<float>& mels, size_t offset, size_t length,
Vlad Popaf09e93f2022-10-31 16:27:12 +010085 audio_port_handle_t deviceId) const override;
86
87 void onMomentaryExposure(float currentMel, audio_port_handle_t deviceId) const override;
Vlad Popa4defd0b2022-11-06 14:22:31 +010088
89 private:
90 sp<media::ISoundDoseCallback> getSoundDoseCallback() const;
91
Vlad Popaf09e93f2022-10-31 16:27:12 +010092 mutable std::mutex mLock;
Vlad Popa2900c0a2022-10-24 13:38:00 +020093
94 // no need for lock since MelAggregator is thread-safe
Vlad Popaf09e93f2022-10-31 16:27:12 +010095 const sp<audio_utils::MelAggregator> mMelAggregator;
Vlad Popa2900c0a2022-10-24 13:38:00 +020096
Vlad Popa4defd0b2022-11-06 14:22:31 +010097 std::unordered_map<audio_io_handle_t, wp<audio_utils::MelProcessor>> mActiveProcessors
98 GUARDED_BY(mLock);
Vlad Popaf09e93f2022-10-31 16:27:12 +010099
100 float mRs2Value GUARDED_BY(mLock);
Vlad Popa63f047e2022-11-05 14:09:19 +0100101
102 sp<media::ISoundDoseCallback> mSoundDoseCallback GUARDED_BY(mLock);
Vlad Popa2900c0a2022-10-24 13:38:00 +0200103};
104
105} // namespace android