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