• 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 <stdatomic.h>
21 #include <utils/RefBase.h>
22 #include <utils/KeyedVector.h>
23 #include <utils/Vector.h>
24 #include <utils/threads.h>
25 #include "SoundTriggerHalInterface.h"
26 #include <android/hardware/soundtrigger/2.0/types.h>
27 #include <android/hardware/soundtrigger/2.0/ISoundTriggerHw.h>
28 #include <android/hardware/soundtrigger/2.0/ISoundTriggerHwCallback.h>
29 
30 namespace android {
31 
32 using android::hardware::audio::common::V2_0::Uuid;
33 using android::hardware::soundtrigger::V2_0::ConfidenceLevel;
34 using android::hardware::soundtrigger::V2_0::PhraseRecognitionExtra;
35 using android::hardware::soundtrigger::V2_0::SoundModelType;
36 using android::hardware::soundtrigger::V2_0::SoundModelHandle;
37 using android::hardware::soundtrigger::V2_0::ISoundTriggerHw;
38 using android::hardware::soundtrigger::V2_0::ISoundTriggerHwCallback;
39 
40 class SoundTriggerHalHidl : public SoundTriggerHalInterface,
41                             public virtual ISoundTriggerHwCallback
42 
43 {
44 public:
45         virtual int getProperties(struct sound_trigger_properties *properties);
46 
47         /*
48          * Load a sound model. Once loaded, recognition of this model can be started and stopped.
49          * Only one active recognition per model at a time. The SoundTrigger service will handle
50          * concurrent recognition requests by different users/applications on the same model.
51          * The implementation returns a unique handle used by other functions (unload_sound_model(),
52          * start_recognition(), etc...
53          */
54         virtual int loadSoundModel(struct sound_trigger_sound_model *sound_model,
55                                 sound_model_callback_t callback,
56                                 void *cookie,
57                                 sound_model_handle_t *handle);
58 
59         /*
60          * Unload a sound model. A sound model can be unloaded to make room for a new one to overcome
61          * implementation limitations.
62          */
63         virtual int unloadSoundModel(sound_model_handle_t handle);
64 
65         /* Start recognition on a given model. Only one recognition active at a time per model.
66          * Once recognition succeeds of fails, the callback is called.
67          * TODO: group recognition configuration parameters into one struct and add key phrase options.
68          */
69         virtual int startRecognition(sound_model_handle_t handle,
70                                  const struct sound_trigger_recognition_config *config,
71                                  recognition_callback_t callback,
72                                  void *cookie);
73 
74         /* Stop recognition on a given model.
75          * The implementation does not have to call the callback when stopped via this method.
76          */
77         virtual int stopRecognition(sound_model_handle_t handle);
78 
79         /* Stop recognition on all models.
80          * Only supported for device api versions SOUND_TRIGGER_DEVICE_API_VERSION_1_1 or above.
81          * If no implementation is provided, stop_recognition will be called for each running model.
82          */
83         virtual int stopAllRecognitions();
84 
85         // ISoundTriggerHwCallback
86         virtual ::android::hardware::Return<void> recognitionCallback(
87                 const ISoundTriggerHwCallback::RecognitionEvent& event, CallbackCookie cookie);
88         virtual ::android::hardware::Return<void> phraseRecognitionCallback(
89                 const ISoundTriggerHwCallback::PhraseRecognitionEvent& event, int32_t cookie);
90         virtual ::android::hardware::Return<void> soundModelCallback(
91                 const ISoundTriggerHwCallback::ModelEvent& event, CallbackCookie cookie);
92 private:
93         class SoundModel : public RefBase {
94         public:
SoundModel(sound_model_handle_t handle,sound_model_callback_t callback,void * cookie,android::hardware::soundtrigger::V2_0::SoundModelHandle halHandle)95             SoundModel(sound_model_handle_t handle, sound_model_callback_t callback,
96                        void *cookie, android::hardware::soundtrigger::V2_0::SoundModelHandle halHandle)
97                  : mHandle(handle), mHalHandle(halHandle),
98                    mSoundModelCallback(callback), mSoundModelCookie(cookie),
99                    mRecognitionCallback(NULL), mRecognitionCookie(NULL) {}
~SoundModel()100             ~SoundModel() {}
101 
102             sound_model_handle_t   mHandle;
103             android::hardware::soundtrigger::V2_0::SoundModelHandle mHalHandle;
104             sound_model_callback_t mSoundModelCallback;
105             void *                 mSoundModelCookie;
106             recognition_callback_t mRecognitionCallback;
107             void *                 mRecognitionCookie;
108         };
109 
110         friend class SoundTriggerHalInterface;
111 
112         explicit SoundTriggerHalHidl(const char *moduleName = NULL);
113         virtual  ~SoundTriggerHalHidl();
114 
115         void convertUuidToHal(Uuid *halUuid,
116                               const sound_trigger_uuid_t *uuid);
117         void convertUuidFromHal(sound_trigger_uuid_t *uuid,
118                                 const Uuid *halUuid);
119 
120         void convertPropertiesFromHal(
121                 struct sound_trigger_properties *properties,
122                 const ISoundTriggerHw::Properties *halProperties);
123 
124         void convertTriggerPhraseToHal(
125                 ISoundTriggerHw::Phrase *halTriggerPhrase,
126                 const struct sound_trigger_phrase *triggerPhrase);
127         ISoundTriggerHw::SoundModel *convertSoundModelToHal(
128                 const struct sound_trigger_sound_model *soundModel);
129 
130         void convertPhraseRecognitionExtraToHal(
131                 PhraseRecognitionExtra *halExtra,
132                 const struct sound_trigger_phrase_recognition_extra *extra);
133         ISoundTriggerHw::RecognitionConfig *convertRecognitionConfigToHal(
134                 const struct sound_trigger_recognition_config *config);
135 
136         struct sound_trigger_model_event *convertSoundModelEventFromHal(
137                                               const ISoundTriggerHwCallback::ModelEvent *halEvent);
138         void convertPhraseRecognitionExtraFromHal(
139                 struct sound_trigger_phrase_recognition_extra *extra,
140                 const PhraseRecognitionExtra *halExtra);
141         struct sound_trigger_recognition_event *convertRecognitionEventFromHal(
142                 const ISoundTriggerHwCallback::RecognitionEvent *halEvent);
143 
144         uint32_t nextUniqueId();
145         sp<ISoundTriggerHw> getService();
146         sp<SoundModel> getModel(sound_model_handle_t handle);
147         sp<SoundModel> removeModel(sound_model_handle_t handle);
148 
149         static pthread_once_t sOnceControl;
150         static void sOnceInit();
151 
152         Mutex mLock;
153         Mutex mHalLock;
154         const char *mModuleName;
155         volatile atomic_uint_fast32_t  mNextUniqueId;
156         // Effect chains without a valid thread
157         DefaultKeyedVector< sound_model_handle_t , sp<SoundModel> > mSoundModels;
158         sp<::android::hardware::soundtrigger::V2_0::ISoundTriggerHw> mISoundTrigger;
159 };
160 
161 } // namespace android
162 
163 #endif // ANDROID_HARDWARE_SOUNDTRIGGER_HAL_HIDL_H
164