Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #ifndef ANDROID_HARDWARE_SOUNDTRIGGER_HAL_HIDL_H |
| 18 | #define ANDROID_HARDWARE_SOUNDTRIGGER_HAL_HIDL_H |
| 19 | |
Mikhail Naganov | 6f9f1e2 | 2018-01-05 14:09:32 -0800 | [diff] [blame] | 20 | #include <utility> |
| 21 | |
Mathias Agopian | 05d19b0 | 2017-02-28 16:28:19 -0800 | [diff] [blame] | 22 | #include <stdatomic.h> |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 23 | #include <utils/RefBase.h> |
| 24 | #include <utils/KeyedVector.h> |
| 25 | #include <utils/Vector.h> |
| 26 | #include <utils/threads.h> |
| 27 | #include "SoundTriggerHalInterface.h" |
| 28 | #include <android/hardware/soundtrigger/2.0/types.h> |
Mikhail Naganov | 6f9f1e2 | 2018-01-05 14:09:32 -0800 | [diff] [blame] | 29 | #include <android/hardware/soundtrigger/2.1/ISoundTriggerHw.h> |
Michael Dooley | 67e3d41 | 2018-10-16 19:51:16 +0000 | [diff] [blame] | 30 | #include <android/hardware/soundtrigger/2.2/ISoundTriggerHw.h> |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 31 | #include <android/hardware/soundtrigger/2.0/ISoundTriggerHwCallback.h> |
Mikhail Naganov | 6f9f1e2 | 2018-01-05 14:09:32 -0800 | [diff] [blame] | 32 | #include <android/hardware/soundtrigger/2.1/ISoundTriggerHwCallback.h> |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 33 | |
| 34 | namespace android { |
| 35 | |
Mikhail Naganov | 6f9f1e2 | 2018-01-05 14:09:32 -0800 | [diff] [blame] | 36 | using ::android::hardware::audio::common::V2_0::Uuid; |
| 37 | using ::android::hardware::hidl_vec; |
| 38 | using ::android::hardware::soundtrigger::V2_0::ConfidenceLevel; |
| 39 | using ::android::hardware::soundtrigger::V2_0::PhraseRecognitionExtra; |
| 40 | using ::android::hardware::soundtrigger::V2_0::SoundModelType; |
| 41 | using ::android::hardware::soundtrigger::V2_0::SoundModelHandle; |
| 42 | using ::android::hardware::soundtrigger::V2_0::ISoundTriggerHw; |
| 43 | using V2_0_ISoundTriggerHwCallback = |
| 44 | ::android::hardware::soundtrigger::V2_0::ISoundTriggerHwCallback; |
| 45 | using V2_1_ISoundTriggerHw = |
| 46 | ::android::hardware::soundtrigger::V2_1::ISoundTriggerHw; |
| 47 | using V2_1_ISoundTriggerHwCallback = |
| 48 | ::android::hardware::soundtrigger::V2_1::ISoundTriggerHwCallback; |
| 49 | using ::android::hidl::memory::V1_0::IMemory; |
Michael Dooley | 67e3d41 | 2018-10-16 19:51:16 +0000 | [diff] [blame] | 50 | using V2_2_ISoundTriggerHw = |
| 51 | ::android::hardware::soundtrigger::V2_2::ISoundTriggerHw; |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 52 | |
| 53 | class SoundTriggerHalHidl : public SoundTriggerHalInterface, |
Mikhail Naganov | 6f9f1e2 | 2018-01-05 14:09:32 -0800 | [diff] [blame] | 54 | public virtual V2_1_ISoundTriggerHwCallback |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 55 | |
| 56 | { |
| 57 | public: |
| 58 | virtual int getProperties(struct sound_trigger_properties *properties); |
| 59 | |
| 60 | /* |
| 61 | * Load a sound model. Once loaded, recognition of this model can be started and stopped. |
| 62 | * Only one active recognition per model at a time. The SoundTrigger service will handle |
| 63 | * concurrent recognition requests by different users/applications on the same model. |
| 64 | * The implementation returns a unique handle used by other functions (unload_sound_model(), |
| 65 | * start_recognition(), etc... |
| 66 | */ |
| 67 | virtual int loadSoundModel(struct sound_trigger_sound_model *sound_model, |
| 68 | sound_model_callback_t callback, |
| 69 | void *cookie, |
| 70 | sound_model_handle_t *handle); |
| 71 | |
| 72 | /* |
| 73 | * Unload a sound model. A sound model can be unloaded to make room for a new one to overcome |
| 74 | * implementation limitations. |
| 75 | */ |
| 76 | virtual int unloadSoundModel(sound_model_handle_t handle); |
| 77 | |
| 78 | /* Start recognition on a given model. Only one recognition active at a time per model. |
| 79 | * Once recognition succeeds of fails, the callback is called. |
| 80 | * TODO: group recognition configuration parameters into one struct and add key phrase options. |
| 81 | */ |
| 82 | virtual int startRecognition(sound_model_handle_t handle, |
| 83 | const struct sound_trigger_recognition_config *config, |
| 84 | recognition_callback_t callback, |
| 85 | void *cookie); |
| 86 | |
| 87 | /* Stop recognition on a given model. |
| 88 | * The implementation does not have to call the callback when stopped via this method. |
| 89 | */ |
| 90 | virtual int stopRecognition(sound_model_handle_t handle); |
| 91 | |
| 92 | /* Stop recognition on all models. |
| 93 | * Only supported for device api versions SOUND_TRIGGER_DEVICE_API_VERSION_1_1 or above. |
| 94 | * If no implementation is provided, stop_recognition will be called for each running model. |
| 95 | */ |
| 96 | virtual int stopAllRecognitions(); |
| 97 | |
Michael Dooley | 67e3d41 | 2018-10-16 19:51:16 +0000 | [diff] [blame] | 98 | /* Get the current state of a given model. |
mike dooley | 6e189b1 | 2018-11-07 15:44:37 +0100 | [diff] [blame] | 99 | * Returns 0 or an error code. If successful the state will be returned asynchronously |
| 100 | * via a recognition event in the callback method that was registered in the |
| 101 | * startRecognition() method. |
Michael Dooley | 67e3d41 | 2018-10-16 19:51:16 +0000 | [diff] [blame] | 102 | * Only supported for device api versions SOUND_TRIGGER_DEVICE_API_VERSION_1_2 or above. |
| 103 | */ |
mike dooley | 6e189b1 | 2018-11-07 15:44:37 +0100 | [diff] [blame] | 104 | virtual int getModelState(sound_model_handle_t handle); |
Michael Dooley | 67e3d41 | 2018-10-16 19:51:16 +0000 | [diff] [blame] | 105 | |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 106 | // ISoundTriggerHwCallback |
| 107 | virtual ::android::hardware::Return<void> recognitionCallback( |
Mikhail Naganov | 6f9f1e2 | 2018-01-05 14:09:32 -0800 | [diff] [blame] | 108 | const V2_0_ISoundTriggerHwCallback::RecognitionEvent& event, CallbackCookie cookie); |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 109 | virtual ::android::hardware::Return<void> phraseRecognitionCallback( |
Mikhail Naganov | 6f9f1e2 | 2018-01-05 14:09:32 -0800 | [diff] [blame] | 110 | const V2_0_ISoundTriggerHwCallback::PhraseRecognitionEvent& event, int32_t cookie); |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 111 | virtual ::android::hardware::Return<void> soundModelCallback( |
Mikhail Naganov | 6f9f1e2 | 2018-01-05 14:09:32 -0800 | [diff] [blame] | 112 | const V2_0_ISoundTriggerHwCallback::ModelEvent& event, CallbackCookie cookie); |
| 113 | virtual ::android::hardware::Return<void> recognitionCallback_2_1( |
| 114 | const RecognitionEvent& event, CallbackCookie cookie); |
| 115 | virtual ::android::hardware::Return<void> phraseRecognitionCallback_2_1( |
| 116 | const PhraseRecognitionEvent& event, int32_t cookie); |
| 117 | virtual ::android::hardware::Return<void> soundModelCallback_2_1( |
| 118 | const ModelEvent& event, CallbackCookie cookie); |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 119 | private: |
| 120 | class SoundModel : public RefBase { |
| 121 | public: |
| 122 | SoundModel(sound_model_handle_t handle, sound_model_callback_t callback, |
| 123 | void *cookie, android::hardware::soundtrigger::V2_0::SoundModelHandle halHandle) |
| 124 | : mHandle(handle), mHalHandle(halHandle), |
| 125 | mSoundModelCallback(callback), mSoundModelCookie(cookie), |
| 126 | mRecognitionCallback(NULL), mRecognitionCookie(NULL) {} |
| 127 | ~SoundModel() {} |
| 128 | |
| 129 | sound_model_handle_t mHandle; |
| 130 | android::hardware::soundtrigger::V2_0::SoundModelHandle mHalHandle; |
| 131 | sound_model_callback_t mSoundModelCallback; |
| 132 | void * mSoundModelCookie; |
| 133 | recognition_callback_t mRecognitionCallback; |
| 134 | void * mRecognitionCookie; |
| 135 | }; |
| 136 | |
| 137 | friend class SoundTriggerHalInterface; |
| 138 | |
| 139 | explicit SoundTriggerHalHidl(const char *moduleName = NULL); |
| 140 | virtual ~SoundTriggerHalHidl(); |
| 141 | |
| 142 | void convertUuidToHal(Uuid *halUuid, |
Eric Laurent | f7854d4 | 2016-10-14 15:57:18 -0700 | [diff] [blame] | 143 | const sound_trigger_uuid_t *uuid); |
| 144 | void convertUuidFromHal(sound_trigger_uuid_t *uuid, |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 145 | const Uuid *halUuid); |
| 146 | |
| 147 | void convertPropertiesFromHal( |
| 148 | struct sound_trigger_properties *properties, |
| 149 | const ISoundTriggerHw::Properties *halProperties); |
| 150 | |
| 151 | void convertTriggerPhraseToHal( |
| 152 | ISoundTriggerHw::Phrase *halTriggerPhrase, |
| 153 | const struct sound_trigger_phrase *triggerPhrase); |
Mikhail Naganov | 6f9f1e2 | 2018-01-05 14:09:32 -0800 | [diff] [blame] | 154 | void convertTriggerPhrasesToHal( |
| 155 | hidl_vec<ISoundTriggerHw::Phrase> *halTriggerPhrases, |
| 156 | struct sound_trigger_phrase_sound_model *keyPhraseModel); |
| 157 | void convertSoundModelToHal(ISoundTriggerHw::SoundModel *halModel, |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 158 | const struct sound_trigger_sound_model *soundModel); |
Mikhail Naganov | 6f9f1e2 | 2018-01-05 14:09:32 -0800 | [diff] [blame] | 159 | std::pair<bool, sp<IMemory>> convertSoundModelToHal( |
| 160 | V2_1_ISoundTriggerHw::SoundModel *halModel, |
| 161 | const struct sound_trigger_sound_model *soundModel) |
| 162 | __attribute__((warn_unused_result)); |
| 163 | void convertPhraseSoundModelToHal(ISoundTriggerHw::PhraseSoundModel *halKeyPhraseModel, |
| 164 | const struct sound_trigger_sound_model *soundModel); |
| 165 | std::pair<bool, sp<IMemory>> convertPhraseSoundModelToHal( |
| 166 | V2_1_ISoundTriggerHw::PhraseSoundModel *halKeyPhraseModel, |
| 167 | const struct sound_trigger_sound_model *soundModel) |
| 168 | __attribute__((warn_unused_result)); |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 169 | |
| 170 | void convertPhraseRecognitionExtraToHal( |
| 171 | PhraseRecognitionExtra *halExtra, |
| 172 | const struct sound_trigger_phrase_recognition_extra *extra); |
Mikhail Naganov | 6f9f1e2 | 2018-01-05 14:09:32 -0800 | [diff] [blame] | 173 | void convertRecognitionConfigToHal(ISoundTriggerHw::RecognitionConfig *halConfig, |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 174 | const struct sound_trigger_recognition_config *config); |
Mikhail Naganov | 6f9f1e2 | 2018-01-05 14:09:32 -0800 | [diff] [blame] | 175 | std::pair<bool, sp<IMemory>> convertRecognitionConfigToHal( |
| 176 | V2_1_ISoundTriggerHw::RecognitionConfig *halConfig, |
| 177 | const struct sound_trigger_recognition_config *config) |
| 178 | __attribute__((warn_unused_result)); |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 179 | |
| 180 | struct sound_trigger_model_event *convertSoundModelEventFromHal( |
Mikhail Naganov | 6f9f1e2 | 2018-01-05 14:09:32 -0800 | [diff] [blame] | 181 | const V2_0_ISoundTriggerHwCallback::ModelEvent *halEvent); |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 182 | void convertPhraseRecognitionExtraFromHal( |
| 183 | struct sound_trigger_phrase_recognition_extra *extra, |
| 184 | const PhraseRecognitionExtra *halExtra); |
Mikhail Naganov | 6f9f1e2 | 2018-01-05 14:09:32 -0800 | [diff] [blame] | 185 | struct sound_trigger_phrase_recognition_event* convertPhraseRecognitionEventFromHal( |
| 186 | const V2_0_ISoundTriggerHwCallback::PhraseRecognitionEvent *halPhraseEvent); |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 187 | struct sound_trigger_recognition_event *convertRecognitionEventFromHal( |
Mikhail Naganov | 6f9f1e2 | 2018-01-05 14:09:32 -0800 | [diff] [blame] | 188 | const V2_0_ISoundTriggerHwCallback::RecognitionEvent *halEvent); |
| 189 | void fillRecognitionEventFromHal( |
| 190 | struct sound_trigger_recognition_event *event, |
| 191 | const V2_0_ISoundTriggerHwCallback::RecognitionEvent *halEvent); |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 192 | |
| 193 | uint32_t nextUniqueId(); |
| 194 | sp<ISoundTriggerHw> getService(); |
Mikhail Naganov | 6f9f1e2 | 2018-01-05 14:09:32 -0800 | [diff] [blame] | 195 | sp<V2_1_ISoundTriggerHw> toService2_1(const sp<ISoundTriggerHw>& s); |
Michael Dooley | 67e3d41 | 2018-10-16 19:51:16 +0000 | [diff] [blame] | 196 | sp<V2_2_ISoundTriggerHw> toService2_2(const sp<ISoundTriggerHw>& s); |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 197 | sp<SoundModel> getModel(sound_model_handle_t handle); |
| 198 | sp<SoundModel> removeModel(sound_model_handle_t handle); |
| 199 | |
| 200 | static pthread_once_t sOnceControl; |
| 201 | static void sOnceInit(); |
| 202 | |
| 203 | Mutex mLock; |
| 204 | Mutex mHalLock; |
| 205 | const char *mModuleName; |
| 206 | volatile atomic_uint_fast32_t mNextUniqueId; |
| 207 | // Effect chains without a valid thread |
| 208 | DefaultKeyedVector< sound_model_handle_t , sp<SoundModel> > mSoundModels; |
| 209 | sp<::android::hardware::soundtrigger::V2_0::ISoundTriggerHw> mISoundTrigger; |
| 210 | }; |
| 211 | |
| 212 | } // namespace android |
| 213 | |
| 214 | #endif // ANDROID_HARDWARE_SOUNDTRIGGER_HAL_HIDL_H |