1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <mutex> 20 21 #include <aidl/android/hardware/audio/core/sounddose/BnSoundDose.h> 22 #include <aidl/android/media/audio/common/AudioDevice.h> 23 #include <aidl/android/media/audio/common/AudioFormatDescription.h> 24 #include <audio_utils/MelProcessor.h> 25 #include <audio_utils/mutex.h> 26 27 namespace aidl::android::hardware::audio::core::sounddose { 28 29 // Interface used for processing the data received by a stream. 30 class StreamDataProcessorInterface { 31 public: 32 virtual ~StreamDataProcessorInterface() = default; 33 34 virtual void startDataProcessor( 35 uint32_t samplerate, uint32_t channelCount, 36 const ::aidl::android::media::audio::common::AudioFormatDescription& format) = 0; 37 virtual void setAudioDevice( 38 const ::aidl::android::media::audio::common::AudioDevice& audioDevice) = 0; 39 virtual void process(const void* buffer, size_t size) = 0; 40 }; 41 42 class SoundDose final : public BnSoundDose, public StreamDataProcessorInterface { 43 public: SoundDose()44 SoundDose() : mMelCallback(::android::sp<MelCallback>::make(this)){}; 45 46 // -------------------------------------- BnSoundDose ------------------------------------------ 47 ndk::ScopedAStatus setOutputRs2UpperBound(float in_rs2ValueDbA) override; 48 ndk::ScopedAStatus getOutputRs2UpperBound(float* _aidl_return) override; 49 ndk::ScopedAStatus registerSoundDoseCallback( 50 const std::shared_ptr<ISoundDose::IHalSoundDoseCallback>& in_callback) override; 51 52 // ----------------------------- StreamDataProcessorInterface ---------------------------------- 53 void setAudioDevice( 54 const ::aidl::android::media::audio::common::AudioDevice& audioDevice) override; 55 void startDataProcessor( 56 uint32_t samplerate, uint32_t channelCount, 57 const ::aidl::android::media::audio::common::AudioFormatDescription& format) override; 58 void process(const void* buffer, size_t size) override; 59 60 private: 61 class MelCallback : public ::android::audio_utils::MelProcessor::MelCallback { 62 public: MelCallback(SoundDose * soundDose)63 explicit MelCallback(SoundDose* soundDose) : mSoundDose(*soundDose) {} 64 65 // ------------------------------------ MelCallback ---------------------------------------- 66 void onNewMelValues(const std::vector<float>& mels, size_t offset, size_t length, 67 audio_port_handle_t deviceId, bool attenuated) const override; 68 void onMomentaryExposure(float currentMel, audio_port_handle_t deviceId) const override; 69 70 SoundDose& mSoundDose; // must outlive MelCallback, not owning 71 }; 72 73 void onNewMelValues(const std::vector<float>& mels, size_t offset, size_t length, 74 audio_port_handle_t deviceId) const; 75 void onMomentaryExposure(float currentMel, audio_port_handle_t deviceId) const; 76 77 mutable ::android::audio_utils::mutex mCbMutex; 78 std::shared_ptr<ISoundDose::IHalSoundDoseCallback> mCallback GUARDED_BY(mCbMutex); 79 std::optional<::aidl::android::media::audio::common::AudioDevice> mAudioDevice 80 GUARDED_BY(mCbMutex); 81 mutable ::android::audio_utils::mutex mMutex; 82 float mRs2Value GUARDED_BY(mMutex) = DEFAULT_MAX_RS2; 83 ::android::sp<::android::audio_utils::MelProcessor> mMelProcessor GUARDED_BY(mMutex); 84 ::android::sp<MelCallback> mMelCallback GUARDED_BY(mMutex); 85 }; 86 87 } // namespace aidl::android::hardware::audio::core::sounddose 88