blob: fb9e39e65f879ad593da0315afb0414401c60376 [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>
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -080029#include <android/hardware/soundtrigger/2.1/ISoundTriggerHw.h>
Michael Dooley67e3d412018-10-16 19:51:16 +000030#include <android/hardware/soundtrigger/2.2/ISoundTriggerHw.h>
Eric Laurent7a544b42016-08-05 19:01:13 -070031#include <android/hardware/soundtrigger/2.0/ISoundTriggerHwCallback.h>
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -080032#include <android/hardware/soundtrigger/2.1/ISoundTriggerHwCallback.h>
Eric Laurent7a544b42016-08-05 19:01:13 -070033
34namespace android {
35
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -080036using ::android::hardware::audio::common::V2_0::Uuid;
37using ::android::hardware::hidl_vec;
38using ::android::hardware::soundtrigger::V2_0::ConfidenceLevel;
39using ::android::hardware::soundtrigger::V2_0::PhraseRecognitionExtra;
40using ::android::hardware::soundtrigger::V2_0::SoundModelType;
41using ::android::hardware::soundtrigger::V2_0::SoundModelHandle;
42using ::android::hardware::soundtrigger::V2_0::ISoundTriggerHw;
43using V2_0_ISoundTriggerHwCallback =
44 ::android::hardware::soundtrigger::V2_0::ISoundTriggerHwCallback;
45using V2_1_ISoundTriggerHw =
46 ::android::hardware::soundtrigger::V2_1::ISoundTriggerHw;
47using V2_1_ISoundTriggerHwCallback =
48 ::android::hardware::soundtrigger::V2_1::ISoundTriggerHwCallback;
49using ::android::hidl::memory::V1_0::IMemory;
Michael Dooley67e3d412018-10-16 19:51:16 +000050using V2_2_ISoundTriggerHw =
51 ::android::hardware::soundtrigger::V2_2::ISoundTriggerHw;
Eric Laurent7a544b42016-08-05 19:01:13 -070052
53class SoundTriggerHalHidl : public SoundTriggerHalInterface,
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -080054 public virtual V2_1_ISoundTriggerHwCallback
Eric Laurent7a544b42016-08-05 19:01:13 -070055
56{
57public:
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 Dooley67e3d412018-10-16 19:51:16 +000098 /* Get the current state of a given model.
mike dooley6e189b12018-11-07 15:44:37 +010099 * 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 Dooley67e3d412018-10-16 19:51:16 +0000102 * Only supported for device api versions SOUND_TRIGGER_DEVICE_API_VERSION_1_2 or above.
103 */
mike dooley6e189b12018-11-07 15:44:37 +0100104 virtual int getModelState(sound_model_handle_t handle);
Michael Dooley67e3d412018-10-16 19:51:16 +0000105
Eric Laurent7a544b42016-08-05 19:01:13 -0700106 // ISoundTriggerHwCallback
107 virtual ::android::hardware::Return<void> recognitionCallback(
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800108 const V2_0_ISoundTriggerHwCallback::RecognitionEvent& event, CallbackCookie cookie);
Eric Laurent7a544b42016-08-05 19:01:13 -0700109 virtual ::android::hardware::Return<void> phraseRecognitionCallback(
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800110 const V2_0_ISoundTriggerHwCallback::PhraseRecognitionEvent& event, int32_t cookie);
Eric Laurent7a544b42016-08-05 19:01:13 -0700111 virtual ::android::hardware::Return<void> soundModelCallback(
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800112 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 Laurent7a544b42016-08-05 19:01:13 -0700119private:
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 Laurentf7854d42016-10-14 15:57:18 -0700143 const sound_trigger_uuid_t *uuid);
144 void convertUuidFromHal(sound_trigger_uuid_t *uuid,
Eric Laurent7a544b42016-08-05 19:01:13 -0700145 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 Naganov6f9f1e22018-01-05 14:09:32 -0800154 void convertTriggerPhrasesToHal(
155 hidl_vec<ISoundTriggerHw::Phrase> *halTriggerPhrases,
156 struct sound_trigger_phrase_sound_model *keyPhraseModel);
157 void convertSoundModelToHal(ISoundTriggerHw::SoundModel *halModel,
Eric Laurent7a544b42016-08-05 19:01:13 -0700158 const struct sound_trigger_sound_model *soundModel);
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800159 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 Laurent7a544b42016-08-05 19:01:13 -0700169
170 void convertPhraseRecognitionExtraToHal(
171 PhraseRecognitionExtra *halExtra,
172 const struct sound_trigger_phrase_recognition_extra *extra);
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800173 void convertRecognitionConfigToHal(ISoundTriggerHw::RecognitionConfig *halConfig,
Eric Laurent7a544b42016-08-05 19:01:13 -0700174 const struct sound_trigger_recognition_config *config);
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800175 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 Laurent7a544b42016-08-05 19:01:13 -0700179
180 struct sound_trigger_model_event *convertSoundModelEventFromHal(
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800181 const V2_0_ISoundTriggerHwCallback::ModelEvent *halEvent);
Eric Laurent7a544b42016-08-05 19:01:13 -0700182 void convertPhraseRecognitionExtraFromHal(
183 struct sound_trigger_phrase_recognition_extra *extra,
184 const PhraseRecognitionExtra *halExtra);
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800185 struct sound_trigger_phrase_recognition_event* convertPhraseRecognitionEventFromHal(
186 const V2_0_ISoundTriggerHwCallback::PhraseRecognitionEvent *halPhraseEvent);
Eric Laurent7a544b42016-08-05 19:01:13 -0700187 struct sound_trigger_recognition_event *convertRecognitionEventFromHal(
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800188 const V2_0_ISoundTriggerHwCallback::RecognitionEvent *halEvent);
189 void fillRecognitionEventFromHal(
190 struct sound_trigger_recognition_event *event,
191 const V2_0_ISoundTriggerHwCallback::RecognitionEvent *halEvent);
Eric Laurent7a544b42016-08-05 19:01:13 -0700192
193 uint32_t nextUniqueId();
194 sp<ISoundTriggerHw> getService();
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800195 sp<V2_1_ISoundTriggerHw> toService2_1(const sp<ISoundTriggerHw>& s);
Michael Dooley67e3d412018-10-16 19:51:16 +0000196 sp<V2_2_ISoundTriggerHw> toService2_2(const sp<ISoundTriggerHw>& s);
Eric Laurent7a544b42016-08-05 19:01:13 -0700197 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