• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 #include <utility>
21 
22 #include <stdatomic.h>
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>
29 #include <android/hardware/soundtrigger/2.1/ISoundTriggerHw.h>
30 #include <android/hardware/soundtrigger/2.2/ISoundTriggerHw.h>
31 #include <android/hardware/soundtrigger/2.0/ISoundTriggerHwCallback.h>
32 #include <android/hardware/soundtrigger/2.1/ISoundTriggerHwCallback.h>
33 
34 namespace android {
35 
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;
50 using V2_2_ISoundTriggerHw =
51         ::android::hardware::soundtrigger::V2_2::ISoundTriggerHw;
52 
53 class SoundTriggerHalHidl : public SoundTriggerHalInterface,
54                             public virtual V2_1_ISoundTriggerHwCallback
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 
98         /* Get the current state of a given model.
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.
102          * Only supported for device api versions SOUND_TRIGGER_DEVICE_API_VERSION_1_2 or above.
103          */
104         virtual int getModelState(sound_model_handle_t handle);
105 
106         // ISoundTriggerHwCallback
107         virtual ::android::hardware::Return<void> recognitionCallback(
108                 const V2_0_ISoundTriggerHwCallback::RecognitionEvent& event, CallbackCookie cookie);
109         virtual ::android::hardware::Return<void> phraseRecognitionCallback(
110                 const V2_0_ISoundTriggerHwCallback::PhraseRecognitionEvent& event, int32_t cookie);
111         virtual ::android::hardware::Return<void> soundModelCallback(
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);
119 private:
120         class SoundModel : public RefBase {
121         public:
SoundModel(sound_model_handle_t handle,sound_model_callback_t callback,void * cookie,android::hardware::soundtrigger::V2_0::SoundModelHandle halHandle)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) {}
~SoundModel()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,
143                               const sound_trigger_uuid_t *uuid);
144         void convertUuidFromHal(sound_trigger_uuid_t *uuid,
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);
154         void convertTriggerPhrasesToHal(
155                 hidl_vec<ISoundTriggerHw::Phrase> *halTriggerPhrases,
156                 struct sound_trigger_phrase_sound_model *keyPhraseModel);
157         void convertSoundModelToHal(ISoundTriggerHw::SoundModel *halModel,
158                 const struct sound_trigger_sound_model *soundModel);
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));
169 
170         void convertPhraseRecognitionExtraToHal(
171                 PhraseRecognitionExtra *halExtra,
172                 const struct sound_trigger_phrase_recognition_extra *extra);
173         void convertRecognitionConfigToHal(ISoundTriggerHw::RecognitionConfig *halConfig,
174                 const struct sound_trigger_recognition_config *config);
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));
179 
180         struct sound_trigger_model_event *convertSoundModelEventFromHal(
181                                               const V2_0_ISoundTriggerHwCallback::ModelEvent *halEvent);
182         void convertPhraseRecognitionExtraFromHal(
183                 struct sound_trigger_phrase_recognition_extra *extra,
184                 const PhraseRecognitionExtra *halExtra);
185         struct sound_trigger_phrase_recognition_event* convertPhraseRecognitionEventFromHal(
186                 const V2_0_ISoundTriggerHwCallback::PhraseRecognitionEvent *halPhraseEvent);
187         struct sound_trigger_recognition_event *convertRecognitionEventFromHal(
188                 const V2_0_ISoundTriggerHwCallback::RecognitionEvent *halEvent);
189         void fillRecognitionEventFromHal(
190                 struct sound_trigger_recognition_event *event,
191                 const V2_0_ISoundTriggerHwCallback::RecognitionEvent *halEvent);
192 
193         uint32_t nextUniqueId();
194         sp<ISoundTriggerHw> getService();
195         sp<V2_1_ISoundTriggerHw> toService2_1(const sp<ISoundTriggerHw>& s);
196         sp<V2_2_ISoundTriggerHw> toService2_2(const sp<ISoundTriggerHw>& s);
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
215