blob: 0b44ae09129bd232f7a18630b994c41585abc3b0 [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>
Eric Laurent7a544b42016-08-05 19:01:13 -070030#include <android/hardware/soundtrigger/2.0/ISoundTriggerHwCallback.h>
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -080031#include <android/hardware/soundtrigger/2.1/ISoundTriggerHwCallback.h>
Eric Laurent7a544b42016-08-05 19:01:13 -070032
33namespace android {
34
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -080035using ::android::hardware::audio::common::V2_0::Uuid;
36using ::android::hardware::hidl_vec;
37using ::android::hardware::soundtrigger::V2_0::ConfidenceLevel;
38using ::android::hardware::soundtrigger::V2_0::PhraseRecognitionExtra;
39using ::android::hardware::soundtrigger::V2_0::SoundModelType;
40using ::android::hardware::soundtrigger::V2_0::SoundModelHandle;
41using ::android::hardware::soundtrigger::V2_0::ISoundTriggerHw;
42using V2_0_ISoundTriggerHwCallback =
43 ::android::hardware::soundtrigger::V2_0::ISoundTriggerHwCallback;
44using V2_1_ISoundTriggerHw =
45 ::android::hardware::soundtrigger::V2_1::ISoundTriggerHw;
46using V2_1_ISoundTriggerHwCallback =
47 ::android::hardware::soundtrigger::V2_1::ISoundTriggerHwCallback;
48using ::android::hidl::memory::V1_0::IMemory;
Eric Laurent7a544b42016-08-05 19:01:13 -070049
50class SoundTriggerHalHidl : public SoundTriggerHalInterface,
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -080051 public virtual V2_1_ISoundTriggerHwCallback
Eric Laurent7a544b42016-08-05 19:01:13 -070052
53{
54public:
55 virtual int getProperties(struct sound_trigger_properties *properties);
56
57 /*
58 * Load a sound model. Once loaded, recognition of this model can be started and stopped.
59 * Only one active recognition per model at a time. The SoundTrigger service will handle
60 * concurrent recognition requests by different users/applications on the same model.
61 * The implementation returns a unique handle used by other functions (unload_sound_model(),
62 * start_recognition(), etc...
63 */
64 virtual int loadSoundModel(struct sound_trigger_sound_model *sound_model,
65 sound_model_callback_t callback,
66 void *cookie,
67 sound_model_handle_t *handle);
68
69 /*
70 * Unload a sound model. A sound model can be unloaded to make room for a new one to overcome
71 * implementation limitations.
72 */
73 virtual int unloadSoundModel(sound_model_handle_t handle);
74
75 /* Start recognition on a given model. Only one recognition active at a time per model.
76 * Once recognition succeeds of fails, the callback is called.
77 * TODO: group recognition configuration parameters into one struct and add key phrase options.
78 */
79 virtual int startRecognition(sound_model_handle_t handle,
80 const struct sound_trigger_recognition_config *config,
81 recognition_callback_t callback,
82 void *cookie);
83
84 /* Stop recognition on a given model.
85 * The implementation does not have to call the callback when stopped via this method.
86 */
87 virtual int stopRecognition(sound_model_handle_t handle);
88
89 /* Stop recognition on all models.
90 * Only supported for device api versions SOUND_TRIGGER_DEVICE_API_VERSION_1_1 or above.
91 * If no implementation is provided, stop_recognition will be called for each running model.
92 */
93 virtual int stopAllRecognitions();
94
Eric Laurent7a544b42016-08-05 19:01:13 -070095 // ISoundTriggerHwCallback
96 virtual ::android::hardware::Return<void> recognitionCallback(
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -080097 const V2_0_ISoundTriggerHwCallback::RecognitionEvent& event, CallbackCookie cookie);
Eric Laurent7a544b42016-08-05 19:01:13 -070098 virtual ::android::hardware::Return<void> phraseRecognitionCallback(
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -080099 const V2_0_ISoundTriggerHwCallback::PhraseRecognitionEvent& event, int32_t cookie);
Eric Laurent7a544b42016-08-05 19:01:13 -0700100 virtual ::android::hardware::Return<void> soundModelCallback(
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800101 const V2_0_ISoundTriggerHwCallback::ModelEvent& event, CallbackCookie cookie);
102 virtual ::android::hardware::Return<void> recognitionCallback_2_1(
103 const RecognitionEvent& event, CallbackCookie cookie);
104 virtual ::android::hardware::Return<void> phraseRecognitionCallback_2_1(
105 const PhraseRecognitionEvent& event, int32_t cookie);
106 virtual ::android::hardware::Return<void> soundModelCallback_2_1(
107 const ModelEvent& event, CallbackCookie cookie);
Eric Laurent7a544b42016-08-05 19:01:13 -0700108private:
109 class SoundModel : public RefBase {
110 public:
111 SoundModel(sound_model_handle_t handle, sound_model_callback_t callback,
112 void *cookie, android::hardware::soundtrigger::V2_0::SoundModelHandle halHandle)
113 : mHandle(handle), mHalHandle(halHandle),
114 mSoundModelCallback(callback), mSoundModelCookie(cookie),
115 mRecognitionCallback(NULL), mRecognitionCookie(NULL) {}
116 ~SoundModel() {}
117
118 sound_model_handle_t mHandle;
119 android::hardware::soundtrigger::V2_0::SoundModelHandle mHalHandle;
120 sound_model_callback_t mSoundModelCallback;
121 void * mSoundModelCookie;
122 recognition_callback_t mRecognitionCallback;
123 void * mRecognitionCookie;
124 };
125
126 friend class SoundTriggerHalInterface;
127
128 explicit SoundTriggerHalHidl(const char *moduleName = NULL);
129 virtual ~SoundTriggerHalHidl();
130
131 void convertUuidToHal(Uuid *halUuid,
Eric Laurentf7854d42016-10-14 15:57:18 -0700132 const sound_trigger_uuid_t *uuid);
133 void convertUuidFromHal(sound_trigger_uuid_t *uuid,
Eric Laurent7a544b42016-08-05 19:01:13 -0700134 const Uuid *halUuid);
135
136 void convertPropertiesFromHal(
137 struct sound_trigger_properties *properties,
138 const ISoundTriggerHw::Properties *halProperties);
139
140 void convertTriggerPhraseToHal(
141 ISoundTriggerHw::Phrase *halTriggerPhrase,
142 const struct sound_trigger_phrase *triggerPhrase);
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800143 void convertTriggerPhrasesToHal(
144 hidl_vec<ISoundTriggerHw::Phrase> *halTriggerPhrases,
145 struct sound_trigger_phrase_sound_model *keyPhraseModel);
146 void convertSoundModelToHal(ISoundTriggerHw::SoundModel *halModel,
Eric Laurent7a544b42016-08-05 19:01:13 -0700147 const struct sound_trigger_sound_model *soundModel);
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800148 std::pair<bool, sp<IMemory>> convertSoundModelToHal(
149 V2_1_ISoundTriggerHw::SoundModel *halModel,
150 const struct sound_trigger_sound_model *soundModel)
151 __attribute__((warn_unused_result));
152 void convertPhraseSoundModelToHal(ISoundTriggerHw::PhraseSoundModel *halKeyPhraseModel,
153 const struct sound_trigger_sound_model *soundModel);
154 std::pair<bool, sp<IMemory>> convertPhraseSoundModelToHal(
155 V2_1_ISoundTriggerHw::PhraseSoundModel *halKeyPhraseModel,
156 const struct sound_trigger_sound_model *soundModel)
157 __attribute__((warn_unused_result));
Eric Laurent7a544b42016-08-05 19:01:13 -0700158
159 void convertPhraseRecognitionExtraToHal(
160 PhraseRecognitionExtra *halExtra,
161 const struct sound_trigger_phrase_recognition_extra *extra);
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800162 void convertRecognitionConfigToHal(ISoundTriggerHw::RecognitionConfig *halConfig,
Eric Laurent7a544b42016-08-05 19:01:13 -0700163 const struct sound_trigger_recognition_config *config);
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800164 std::pair<bool, sp<IMemory>> convertRecognitionConfigToHal(
165 V2_1_ISoundTriggerHw::RecognitionConfig *halConfig,
166 const struct sound_trigger_recognition_config *config)
167 __attribute__((warn_unused_result));
Eric Laurent7a544b42016-08-05 19:01:13 -0700168
169 struct sound_trigger_model_event *convertSoundModelEventFromHal(
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800170 const V2_0_ISoundTriggerHwCallback::ModelEvent *halEvent);
Eric Laurent7a544b42016-08-05 19:01:13 -0700171 void convertPhraseRecognitionExtraFromHal(
172 struct sound_trigger_phrase_recognition_extra *extra,
173 const PhraseRecognitionExtra *halExtra);
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800174 struct sound_trigger_phrase_recognition_event* convertPhraseRecognitionEventFromHal(
175 const V2_0_ISoundTriggerHwCallback::PhraseRecognitionEvent *halPhraseEvent);
Eric Laurent7a544b42016-08-05 19:01:13 -0700176 struct sound_trigger_recognition_event *convertRecognitionEventFromHal(
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800177 const V2_0_ISoundTriggerHwCallback::RecognitionEvent *halEvent);
178 void fillRecognitionEventFromHal(
179 struct sound_trigger_recognition_event *event,
180 const V2_0_ISoundTriggerHwCallback::RecognitionEvent *halEvent);
Eric Laurent7a544b42016-08-05 19:01:13 -0700181
182 uint32_t nextUniqueId();
183 sp<ISoundTriggerHw> getService();
Mikhail Naganov6f9f1e22018-01-05 14:09:32 -0800184 sp<V2_1_ISoundTriggerHw> toService2_1(const sp<ISoundTriggerHw>& s);
Eric Laurent7a544b42016-08-05 19:01:13 -0700185 sp<SoundModel> getModel(sound_model_handle_t handle);
186 sp<SoundModel> removeModel(sound_model_handle_t handle);
187
188 static pthread_once_t sOnceControl;
189 static void sOnceInit();
190
191 Mutex mLock;
192 Mutex mHalLock;
193 const char *mModuleName;
194 volatile atomic_uint_fast32_t mNextUniqueId;
195 // Effect chains without a valid thread
196 DefaultKeyedVector< sound_model_handle_t , sp<SoundModel> > mSoundModels;
197 sp<::android::hardware::soundtrigger::V2_0::ISoundTriggerHw> mISoundTrigger;
198};
199
200} // namespace android
201
202#endif // ANDROID_HARDWARE_SOUNDTRIGGER_HAL_HIDL_H