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