blob: b0aa5d653f06e1b7ef4f6d8eb53f53c8301a57d9 [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 Popae3fd1c22022-11-07 21:03:18 +010020#include <android/media/BnSoundDose.h>
Vlad Popa63f047e2022-11-05 14:09:19 +010021#include <android/media/ISoundDoseCallback.h>
Vlad Popa2900c0a2022-10-24 13:38:00 +020022#include <audio_utils/MelAggregator.h>
Vlad Popa4defd0b2022-11-06 14:22:31 +010023#include <audio_utils/MelProcessor.h>
Vlad Popae3fd1c22022-11-07 21:03:18 +010024#include <binder/Status.h>
Vlad Popa2900c0a2022-10-24 13:38:00 +020025#include <mutex>
26#include <unordered_map>
27
28namespace android {
29
Vlad Popaf09e93f2022-10-31 16:27:12 +010030class SoundDoseManager : public audio_utils::MelProcessor::MelCallback {
Vlad Popa4defd0b2022-11-06 14:22:31 +010031 public:
Vlad Popaf09e93f2022-10-31 16:27:12 +010032 /** 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 Popa4defd0b2022-11-06 14:22:31 +010039 mRs2Value(kDefaultRs2Value){};
Vlad Popa2900c0a2022-10-24 13:38:00 +020040
41 /**
Vlad Popaf09e93f2022-10-31 16:27:12 +010042 * \brief Creates or gets the MelProcessor assigned to the streamHandle
Vlad Popa2900c0a2022-10-24 13:38:00 +020043 *
44 * \param deviceId id for the devices where the stream is active.
45 * \param streanHandle handle to the stream
Vlad Popaf09e93f2022-10-31 16:27:12 +010046 * \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 Popa2900c0a2022-10-24 13:38:00 +020051 */
Vlad Popa4defd0b2022-11-06 14:22:31 +010052 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 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
Vlad Popae3fd1c22022-11-07 21:03:18 +010073 /**
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 Popa2900c0a2022-10-24 13:38:00 +020080
Vlad Popae3fd1c22022-11-07 21:03:18 +010081 std::string dump() const;
Vlad Popa63f047e2022-11-05 14:09:19 +010082
Vlad Popa2900c0a2022-10-24 13:38:00 +020083 // used for testing
84 size_t getCachedMelRecordsSize() const;
Vlad Popaf09e93f2022-10-31 16:27:12 +010085
Vlad Popa4defd0b2022-11-06 14:22:31 +010086 /** Method for converting from audio_utils::CsdRecord to media::SoundDoseRecord. */
87 static media::SoundDoseRecord csdRecordToSoundDoseRecord(const audio_utils::CsdRecord& legacy);
88
Vlad Popaf09e93f2022-10-31 16:27:12 +010089 // ------ Override audio_utils::MelProcessor::MelCallback ------
Vlad Popa4defd0b2022-11-06 14:22:31 +010090 void onNewMelValues(const std::vector<float>& mels, size_t offset, size_t length,
Vlad Popaf09e93f2022-10-31 16:27:12 +010091 audio_port_handle_t deviceId) const override;
92
93 void onMomentaryExposure(float currentMel, audio_port_handle_t deviceId) const override;
Vlad Popa4defd0b2022-11-06 14:22:31 +010094
Vlad Popae3fd1c22022-11-07 21:03:18 +010095private:
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 Popa4defd0b2022-11-06 14:22:31 +0100102
Vlad Popae3fd1c22022-11-07 21:03:18 +0100103 /** 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 Popaf09e93f2022-10-31 16:27:12 +0100121 mutable std::mutex mLock;
Vlad Popa2900c0a2022-10-24 13:38:00 +0200122
123 // no need for lock since MelAggregator is thread-safe
Vlad Popaf09e93f2022-10-31 16:27:12 +0100124 const sp<audio_utils::MelAggregator> mMelAggregator;
Vlad Popa2900c0a2022-10-24 13:38:00 +0200125
Vlad Popa4defd0b2022-11-06 14:22:31 +0100126 std::unordered_map<audio_io_handle_t, wp<audio_utils::MelProcessor>> mActiveProcessors
127 GUARDED_BY(mLock);
Vlad Popaf09e93f2022-10-31 16:27:12 +0100128
129 float mRs2Value GUARDED_BY(mLock);
Vlad Popa63f047e2022-11-05 14:09:19 +0100130
Vlad Popae3fd1c22022-11-07 21:03:18 +0100131 sp<SoundDose> mSoundDose GUARDED_BY(mLock);
Vlad Popa2900c0a2022-10-24 13:38:00 +0200132};
133
134} // namespace android