blob: 25878d09425d7245ef7215efba3338cdbd2e7bf5 [file] [log] [blame]
Eric Laurent7a544b42016-08-05 19:01:13 -07001/*
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 Naganov6f9f1e22018-01-05 14:09:32 -080020#include <utility>
21
Mathias Agopian05d19b02017-02-28 16:28:19 -080022#include <stdatomic.h>
Eric Laurent7a544b42016-08-05 19:01:13 -070023#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 Ambur41947e22019-10-01 11:53:40 -070029#include <android/hardware/soundtrigger/2.3/types.h>
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -080030#include <android/hardware/soundtrigger/2.1/ISoundTriggerHw.h>
Michael Dooley67e3d412018-10-16 19:51:16 +000031#include <android/hardware/soundtrigger/2.2/ISoundTriggerHw.h>
Nicholas Ambur41947e22019-10-01 11:53:40 -070032#include <android/hardware/soundtrigger/2.3/ISoundTriggerHw.h>
Eric Laurent7a544b42016-08-05 19:01:13 -070033#include <android/hardware/soundtrigger/2.0/ISoundTriggerHwCallback.h>
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -080034#include <android/hardware/soundtrigger/2.1/ISoundTriggerHwCallback.h>
Eric Laurent7a544b42016-08-05 19:01:13 -070035
36namespace android {
37
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -080038using ::android::hardware::audio::common::V2_0::Uuid;
39using ::android::hardware::hidl_vec;
40using ::android::hardware::soundtrigger::V2_0::ConfidenceLevel;
41using ::android::hardware::soundtrigger::V2_0::PhraseRecognitionExtra;
42using ::android::hardware::soundtrigger::V2_0::SoundModelType;
43using ::android::hardware::soundtrigger::V2_0::SoundModelHandle;
44using ::android::hardware::soundtrigger::V2_0::ISoundTriggerHw;
45using V2_0_ISoundTriggerHwCallback =
46 ::android::hardware::soundtrigger::V2_0::ISoundTriggerHwCallback;
47using V2_1_ISoundTriggerHw =
48 ::android::hardware::soundtrigger::V2_1::ISoundTriggerHw;
49using V2_1_ISoundTriggerHwCallback =
50 ::android::hardware::soundtrigger::V2_1::ISoundTriggerHwCallback;
51using ::android::hidl::memory::V1_0::IMemory;
Michael Dooley67e3d412018-10-16 19:51:16 +000052using V2_2_ISoundTriggerHw =
53 ::android::hardware::soundtrigger::V2_2::ISoundTriggerHw;
Nicholas Ambur41947e22019-10-01 11:53:40 -070054using V2_3_ISoundTriggerHw =
55 ::android::hardware::soundtrigger::V2_3::ISoundTriggerHw;
56using V2_3_ModelParameter =
57 ::android::hardware::soundtrigger::V2_3::ModelParameter;
58using V2_3_OptionalModelParameterRange =
59 ::android::hardware::soundtrigger::V2_3::OptionalModelParameterRange;
Eric Laurent7a544b42016-08-05 19:01:13 -070060
61class SoundTriggerHalHidl : public SoundTriggerHalInterface,
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -080062 public virtual V2_1_ISoundTriggerHwCallback
Eric Laurent7a544b42016-08-05 19:01:13 -070063
64{
65public:
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 Dooley67e3d412018-10-16 19:51:16 +0000106 /* Get the current state of a given model.
mike dooley6e189b12018-11-07 15:44:37 +0100107 * 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 Dooley67e3d412018-10-16 19:51:16 +0000110 * Only supported for device api versions SOUND_TRIGGER_DEVICE_API_VERSION_1_2 or above.
111 */
mike dooley6e189b12018-11-07 15:44:37 +0100112 virtual int getModelState(sound_model_handle_t handle);
Michael Dooley67e3d412018-10-16 19:51:16 +0000113
Nicholas Ambur41947e22019-10-01 11:53:40 -0700114 /* 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 Laurent7a544b42016-08-05 19:01:13 -0700142 // ISoundTriggerHwCallback
143 virtual ::android::hardware::Return<void> recognitionCallback(
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800144 const V2_0_ISoundTriggerHwCallback::RecognitionEvent& event, CallbackCookie cookie);
Eric Laurent7a544b42016-08-05 19:01:13 -0700145 virtual ::android::hardware::Return<void> phraseRecognitionCallback(
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800146 const V2_0_ISoundTriggerHwCallback::PhraseRecognitionEvent& event, int32_t cookie);
Eric Laurent7a544b42016-08-05 19:01:13 -0700147 virtual ::android::hardware::Return<void> soundModelCallback(
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800148 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 Laurent7a544b42016-08-05 19:01:13 -0700155private:
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 Laurentf7854d42016-10-14 15:57:18 -0700179 const sound_trigger_uuid_t *uuid);
180 void convertUuidFromHal(sound_trigger_uuid_t *uuid,
Eric Laurent7a544b42016-08-05 19:01:13 -0700181 const Uuid *halUuid);
182
183 void convertPropertiesFromHal(
184 struct sound_trigger_properties *properties,
185 const ISoundTriggerHw::Properties *halProperties);
Nicholas Ambur41947e22019-10-01 11:53:40 -0700186 static void convertModelParameterToHal(V2_3_ModelParameter* halParam,
187 sound_trigger_model_parameter_t param);
Eric Laurent7a544b42016-08-05 19:01:13 -0700188
189 void convertTriggerPhraseToHal(
190 ISoundTriggerHw::Phrase *halTriggerPhrase,
191 const struct sound_trigger_phrase *triggerPhrase);
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800192 void convertTriggerPhrasesToHal(
193 hidl_vec<ISoundTriggerHw::Phrase> *halTriggerPhrases,
194 struct sound_trigger_phrase_sound_model *keyPhraseModel);
195 void convertSoundModelToHal(ISoundTriggerHw::SoundModel *halModel,
Eric Laurent7a544b42016-08-05 19:01:13 -0700196 const struct sound_trigger_sound_model *soundModel);
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800197 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 Laurent7a544b42016-08-05 19:01:13 -0700207
208 void convertPhraseRecognitionExtraToHal(
209 PhraseRecognitionExtra *halExtra,
210 const struct sound_trigger_phrase_recognition_extra *extra);
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800211 void convertRecognitionConfigToHal(ISoundTriggerHw::RecognitionConfig *halConfig,
Eric Laurent7a544b42016-08-05 19:01:13 -0700212 const struct sound_trigger_recognition_config *config);
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800213 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 Laurent7a544b42016-08-05 19:01:13 -0700217
218 struct sound_trigger_model_event *convertSoundModelEventFromHal(
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800219 const V2_0_ISoundTriggerHwCallback::ModelEvent *halEvent);
Eric Laurent7a544b42016-08-05 19:01:13 -0700220 void convertPhraseRecognitionExtraFromHal(
221 struct sound_trigger_phrase_recognition_extra *extra,
222 const PhraseRecognitionExtra *halExtra);
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800223 struct sound_trigger_phrase_recognition_event* convertPhraseRecognitionEventFromHal(
224 const V2_0_ISoundTriggerHwCallback::PhraseRecognitionEvent *halPhraseEvent);
Eric Laurent7a544b42016-08-05 19:01:13 -0700225 struct sound_trigger_recognition_event *convertRecognitionEventFromHal(
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800226 const V2_0_ISoundTriggerHwCallback::RecognitionEvent *halEvent);
227 void fillRecognitionEventFromHal(
228 struct sound_trigger_recognition_event *event,
229 const V2_0_ISoundTriggerHwCallback::RecognitionEvent *halEvent);
Eric Laurent7a544b42016-08-05 19:01:13 -0700230
231 uint32_t nextUniqueId();
232 sp<ISoundTriggerHw> getService();
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800233 sp<V2_1_ISoundTriggerHw> toService2_1(const sp<ISoundTriggerHw>& s);
Michael Dooley67e3d412018-10-16 19:51:16 +0000234 sp<V2_2_ISoundTriggerHw> toService2_2(const sp<ISoundTriggerHw>& s);
Nicholas Ambur41947e22019-10-01 11:53:40 -0700235 sp<V2_3_ISoundTriggerHw> toService2_3(const sp<ISoundTriggerHw>& s);
Eric Laurent7a544b42016-08-05 19:01:13 -0700236 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