SoundTriggerHalImpl.h revision 27ef4d872d9f6bbbc62d03b807cae694a3c6e2e8
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_V2_0_IMPLEMENTATION_H
18#define ANDROID_HARDWARE_SOUNDTRIGGER_V2_0_IMPLEMENTATION_H
19
20#include <android/hardware/soundtrigger/2.0/ISoundTriggerHw.h>
21#include <android/hardware/soundtrigger/2.0/ISoundTriggerHwCallback.h>
22#include <hidl/Status.h>
23#include <utils/threads.h>
24#include <utils/KeyedVector.h>
25#include <system/sound_trigger.h>
26#include <hardware/sound_trigger.h>
27
28namespace android {
29namespace hardware {
30namespace soundtrigger {
31namespace V2_0 {
32namespace implementation {
33
34using ::android::hardware::audio::common::V2_0::Uuid;
35using ::android::hardware::soundtrigger::V2_0::ISoundTriggerHwCallback;
36
37
38class SoundTriggerHalImpl : public ISoundTriggerHw {
39public:
40        explicit SoundTriggerHalImpl(const char *moduleName = NULL);
41
42        // Methods from ::android::hardware::soundtrigger::V2_0::ISoundTriggerHw follow.
43        Return<void> getProperties(getProperties_cb _hidl_cb)  override;
44        Return<void> loadSoundModel(const ISoundTriggerHw::SoundModel& soundModel,
45                                    const sp<ISoundTriggerHwCallback>& callback,
46                                    ISoundTriggerHwCallback::CallbackCookie cookie,
47                                    loadSoundModel_cb _hidl_cb)  override;
48        Return<void> loadPhraseSoundModel(const ISoundTriggerHw::PhraseSoundModel& soundModel,
49                                    const sp<ISoundTriggerHwCallback>& callback,
50                                    ISoundTriggerHwCallback::CallbackCookie cookie,
51                                    loadPhraseSoundModel_cb _hidl_cb)  override;
52
53        Return<int32_t> unloadSoundModel(SoundModelHandle modelHandle)  override;
54        Return<int32_t> startRecognition(SoundModelHandle modelHandle,
55                                      const ISoundTriggerHw::RecognitionConfig& config,
56                                      const sp<ISoundTriggerHwCallback>& callback,
57                                      ISoundTriggerHwCallback::CallbackCookie cookie)  override;
58        Return<int32_t> stopRecognition(SoundModelHandle modelHandle)  override;
59        Return<int32_t> stopAllRecognitions()  override;
60
61        // RefBase
62        virtual     void        onFirstRef();
63
64        static void soundModelCallback(struct sound_trigger_model_event *halEvent,
65                                       void *cookie);
66        static void recognitionCallback(struct sound_trigger_recognition_event *halEvent,
67                                        void *cookie);
68
69private:
70
71        class SoundModelClient : public RefBase {
72        public:
73            SoundModelClient(uint32_t id, sp<ISoundTriggerHwCallback> callback,
74                             ISoundTriggerHwCallback::CallbackCookie cookie)
75                : mId(id), mCallback(callback), mCookie(cookie) {}
76            virtual ~SoundModelClient() {}
77
78            uint32_t mId;
79            sound_model_handle_t mHalHandle;
80            sp<ISoundTriggerHwCallback> mCallback;
81            ISoundTriggerHwCallback::CallbackCookie mCookie;
82        };
83
84        uint32_t nextUniqueId();
85        void convertUuidFromHal(Uuid *uuid,
86                                const sound_trigger_uuid_t *halUuid);
87        void convertUuidToHal(sound_trigger_uuid_t *halUuid,
88                              const Uuid *uuid);
89        void convertPropertiesFromHal(ISoundTriggerHw::Properties *properties,
90                                      const struct sound_trigger_properties *halProperties);
91        void convertTriggerPhraseToHal(struct sound_trigger_phrase *halTriggerPhrase,
92                                       const ISoundTriggerHw::Phrase *triggerPhrase);
93        // returned HAL sound model must be freed by caller
94        struct sound_trigger_sound_model *convertSoundModelToHal(
95                    const ISoundTriggerHw::SoundModel *soundModel);
96        void convertPhraseRecognitionExtraToHal(
97                struct sound_trigger_phrase_recognition_extra *halExtra,
98                const PhraseRecognitionExtra *extra);
99        // returned recognition config must be freed by caller
100        struct sound_trigger_recognition_config *convertRecognitionConfigToHal(
101                const ISoundTriggerHw::RecognitionConfig *config);
102
103
104        static void convertSoundModelEventFromHal(ISoundTriggerHwCallback::ModelEvent *event,
105                                            const struct sound_trigger_model_event *halEvent);
106        static ISoundTriggerHwCallback::RecognitionEvent *convertRecognitionEventFromHal(
107                                            const struct sound_trigger_recognition_event *halEvent);
108        static void convertPhraseRecognitionExtraFromHal(PhraseRecognitionExtra *extra,
109                                    const struct sound_trigger_phrase_recognition_extra *halExtra);
110
111        int doLoadSoundModel(const ISoundTriggerHw::SoundModel& soundModel,
112                             const sp<ISoundTriggerHwCallback>& callback,
113                             ISoundTriggerHwCallback::CallbackCookie cookie,
114                             uint32_t *modelId);
115
116        virtual             ~SoundTriggerHalImpl();
117
118        const char *                                        mModuleName;
119        struct sound_trigger_hw_device*                     mHwDevice;
120        volatile atomic_uint_fast32_t                       mNextModelId;
121        DefaultKeyedVector<int32_t, sp<SoundModelClient> >  mClients;
122        Mutex                                               mLock;
123};
124
125extern "C" ISoundTriggerHw *HIDL_FETCH_ISoundTriggerHw(const char *name);
126
127}  // namespace implementation
128}  // namespace V2_0
129}  // namespace soundtrigger
130}  // namespace hardware
131}  // namespace android
132
133#endif  // ANDROID_HARDWARE_SOUNDTRIGGER_V2_0_IMPLEMENTATION_H
134
135