blob: 60404dcd9c773d4a8403ac86717b4306da57f915 [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
20#include <utils/RefBase.h>
21#include <utils/KeyedVector.h>
22#include <utils/Vector.h>
23#include <utils/threads.h>
24#include "SoundTriggerHalInterface.h"
25#include <android/hardware/soundtrigger/2.0/types.h>
26#include <android/hardware/soundtrigger/2.0/ISoundTriggerHw.h>
27#include <android/hardware/soundtrigger/2.0/ISoundTriggerHwCallback.h>
28#include <android/hardware/soundtrigger/2.0/BnSoundTriggerHwCallback.h>
29
30namespace android {
31
32using android::hardware::audio::common::V2_0::Uuid;
33using android::hardware::soundtrigger::V2_0::ConfidenceLevel;
34using android::hardware::soundtrigger::V2_0::PhraseRecognitionExtra;
35using android::hardware::soundtrigger::V2_0::SoundModelType;
36using android::hardware::soundtrigger::V2_0::SoundModelHandle;
37using android::hardware::soundtrigger::V2_0::ISoundTriggerHw;
38using android::hardware::soundtrigger::V2_0::ISoundTriggerHwCallback;
39
40class SoundTriggerHalHidl : public SoundTriggerHalInterface,
41 public virtual ISoundTriggerHwCallback
42
43{
44public:
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 // RefBase
86 virtual void onFirstRef();
87
88 // ISoundTriggerHwCallback
89 virtual ::android::hardware::Return<void> recognitionCallback(
90 const ISoundTriggerHwCallback::RecognitionEvent& event, CallbackCookie cookie);
91 virtual ::android::hardware::Return<void> phraseRecognitionCallback(
92 const ISoundTriggerHwCallback::PhraseRecognitionEvent& event, int32_t cookie);
93 virtual ::android::hardware::Return<void> soundModelCallback(
94 const ISoundTriggerHwCallback::ModelEvent& event, CallbackCookie cookie);
95private:
96 class SoundModel : public RefBase {
97 public:
98 SoundModel(sound_model_handle_t handle, sound_model_callback_t callback,
99 void *cookie, android::hardware::soundtrigger::V2_0::SoundModelHandle halHandle)
100 : mHandle(handle), mHalHandle(halHandle),
101 mSoundModelCallback(callback), mSoundModelCookie(cookie),
102 mRecognitionCallback(NULL), mRecognitionCookie(NULL) {}
103 ~SoundModel() {}
104
105 sound_model_handle_t mHandle;
106 android::hardware::soundtrigger::V2_0::SoundModelHandle mHalHandle;
107 sound_model_callback_t mSoundModelCallback;
108 void * mSoundModelCookie;
109 recognition_callback_t mRecognitionCallback;
110 void * mRecognitionCookie;
111 };
112
113 friend class SoundTriggerHalInterface;
114
115 explicit SoundTriggerHalHidl(const char *moduleName = NULL);
116 virtual ~SoundTriggerHalHidl();
117
118 void convertUuidToHal(Uuid *halUuid,
Eric Laurentf7854d42016-10-14 15:57:18 -0700119 const sound_trigger_uuid_t *uuid);
120 void convertUuidFromHal(sound_trigger_uuid_t *uuid,
Eric Laurent7a544b42016-08-05 19:01:13 -0700121 const Uuid *halUuid);
122
123 void convertPropertiesFromHal(
124 struct sound_trigger_properties *properties,
125 const ISoundTriggerHw::Properties *halProperties);
126
127 void convertTriggerPhraseToHal(
128 ISoundTriggerHw::Phrase *halTriggerPhrase,
129 const struct sound_trigger_phrase *triggerPhrase);
130 ISoundTriggerHw::SoundModel *convertSoundModelToHal(
131 const struct sound_trigger_sound_model *soundModel);
132
133 void convertPhraseRecognitionExtraToHal(
134 PhraseRecognitionExtra *halExtra,
135 const struct sound_trigger_phrase_recognition_extra *extra);
136 ISoundTriggerHw::RecognitionConfig *convertRecognitionConfigToHal(
137 const struct sound_trigger_recognition_config *config);
138
139 struct sound_trigger_model_event *convertSoundModelEventFromHal(
140 const ISoundTriggerHwCallback::ModelEvent *halEvent);
141 void convertPhraseRecognitionExtraFromHal(
142 struct sound_trigger_phrase_recognition_extra *extra,
143 const PhraseRecognitionExtra *halExtra);
144 struct sound_trigger_recognition_event *convertRecognitionEventFromHal(
145 const ISoundTriggerHwCallback::RecognitionEvent *halEvent);
146
147 uint32_t nextUniqueId();
148 sp<ISoundTriggerHw> getService();
149 void clearService();
150 sp<SoundModel> getModel(sound_model_handle_t handle);
151 sp<SoundModel> removeModel(sound_model_handle_t handle);
152
153 static pthread_once_t sOnceControl;
154 static void sOnceInit();
155
156 Mutex mLock;
157 Mutex mHalLock;
158 const char *mModuleName;
159 volatile atomic_uint_fast32_t mNextUniqueId;
160 // Effect chains without a valid thread
161 DefaultKeyedVector< sound_model_handle_t , sp<SoundModel> > mSoundModels;
162 sp<::android::hardware::soundtrigger::V2_0::ISoundTriggerHw> mISoundTrigger;
163};
164
165} // namespace android
166
167#endif // ANDROID_HARDWARE_SOUNDTRIGGER_HAL_HIDL_H