blob: 2dd7166ab7456d29bc7b566736a85705850ec323 [file] [log] [blame]
Eric Laurent27ef4d82016-10-14 15:46:06 -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_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>
Mikhail Naganov0bbc4aa2017-12-22 13:23:08 -080022#include <hardware/sound_trigger.h>
Eric Laurent27ef4d82016-10-14 15:46:06 -070023#include <hidl/Status.h>
Mathias Agopianefc68352017-02-28 16:26:29 -080024#include <stdatomic.h>
Eric Laurent27ef4d82016-10-14 15:46:06 -070025#include <system/sound_trigger.h>
Mikhail Naganov0bbc4aa2017-12-22 13:23:08 -080026#include <utils/KeyedVector.h>
27#include <utils/threads.h>
Eric Laurent27ef4d82016-10-14 15:46:06 -070028
29namespace android {
30namespace hardware {
31namespace soundtrigger {
32namespace V2_0 {
33namespace implementation {
34
35using ::android::hardware::audio::common::V2_0::Uuid;
36using ::android::hardware::soundtrigger::V2_0::ISoundTriggerHwCallback;
37
Eric Laurent27ef4d82016-10-14 15:46:06 -070038class SoundTriggerHalImpl : public ISoundTriggerHw {
Mikhail Naganov0bbc4aa2017-12-22 13:23:08 -080039 public:
40 SoundTriggerHalImpl();
Eric Laurent27ef4d82016-10-14 15:46:06 -070041
Mikhail Naganov0bbc4aa2017-12-22 13:23:08 -080042 // 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,
Eric Laurent27ef4d82016-10-14 15:46:06 -070049 const sp<ISoundTriggerHwCallback>& callback,
Mikhail Naganov0bbc4aa2017-12-22 13:23:08 -080050 ISoundTriggerHwCallback::CallbackCookie cookie,
51 loadPhraseSoundModel_cb _hidl_cb) override;
Eric Laurent27ef4d82016-10-14 15:46:06 -070052
Mikhail Naganov0bbc4aa2017-12-22 13:23:08 -080053 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;
Eric Laurent27ef4d82016-10-14 15:46:06 -070060
Mikhail Naganov0bbc4aa2017-12-22 13:23:08 -080061 // RefBase
62 virtual void onFirstRef();
Eric Laurent27ef4d82016-10-14 15:46:06 -070063
Mikhail Naganov0bbc4aa2017-12-22 13:23:08 -080064 static void soundModelCallback(struct sound_trigger_model_event* halEvent, void* cookie);
65 static void recognitionCallback(struct sound_trigger_recognition_event* halEvent, void* cookie);
Eric Laurent27ef4d82016-10-14 15:46:06 -070066
Mikhail Naganov0bbc4aa2017-12-22 13:23:08 -080067 private:
68 class SoundModelClient : public RefBase {
69 public:
70 SoundModelClient(uint32_t id, sp<ISoundTriggerHwCallback> callback,
71 ISoundTriggerHwCallback::CallbackCookie cookie)
72 : mId(id), mCallback(callback), mCookie(cookie) {}
73 virtual ~SoundModelClient() {}
Eric Laurent27ef4d82016-10-14 15:46:06 -070074
Mikhail Naganov0bbc4aa2017-12-22 13:23:08 -080075 uint32_t mId;
76 sound_model_handle_t mHalHandle;
77 sp<ISoundTriggerHwCallback> mCallback;
78 ISoundTriggerHwCallback::CallbackCookie mCookie;
79 };
Eric Laurent27ef4d82016-10-14 15:46:06 -070080
Mikhail Naganov0bbc4aa2017-12-22 13:23:08 -080081 uint32_t nextUniqueId();
82 void convertUuidFromHal(Uuid* uuid, const sound_trigger_uuid_t* halUuid);
83 void convertUuidToHal(sound_trigger_uuid_t* halUuid, const Uuid* uuid);
84 void convertPropertiesFromHal(ISoundTriggerHw::Properties* properties,
85 const struct sound_trigger_properties* halProperties);
86 void convertTriggerPhraseToHal(struct sound_trigger_phrase* halTriggerPhrase,
87 const ISoundTriggerHw::Phrase* triggerPhrase);
88 // returned HAL sound model must be freed by caller
89 struct sound_trigger_sound_model* convertSoundModelToHal(
90 const ISoundTriggerHw::SoundModel* soundModel);
91 void convertPhraseRecognitionExtraToHal(struct sound_trigger_phrase_recognition_extra* halExtra,
92 const PhraseRecognitionExtra* extra);
93 // returned recognition config must be freed by caller
94 struct sound_trigger_recognition_config* convertRecognitionConfigToHal(
95 const ISoundTriggerHw::RecognitionConfig* config);
Eric Laurent27ef4d82016-10-14 15:46:06 -070096
Mikhail Naganov0bbc4aa2017-12-22 13:23:08 -080097 static void convertSoundModelEventFromHal(ISoundTriggerHwCallback::ModelEvent* event,
98 const struct sound_trigger_model_event* halEvent);
99 static ISoundTriggerHwCallback::RecognitionEvent* convertRecognitionEventFromHal(
100 const struct sound_trigger_recognition_event* halEvent);
101 static void convertPhraseRecognitionExtraFromHal(
102 PhraseRecognitionExtra* extra,
103 const struct sound_trigger_phrase_recognition_extra* halExtra);
Eric Laurent27ef4d82016-10-14 15:46:06 -0700104
Mikhail Naganov0bbc4aa2017-12-22 13:23:08 -0800105 int doLoadSoundModel(const ISoundTriggerHw::SoundModel& soundModel,
106 const sp<ISoundTriggerHwCallback>& callback,
107 ISoundTriggerHwCallback::CallbackCookie cookie, uint32_t* modelId);
Eric Laurent27ef4d82016-10-14 15:46:06 -0700108
Mikhail Naganov0bbc4aa2017-12-22 13:23:08 -0800109 virtual ~SoundTriggerHalImpl();
Eric Laurent27ef4d82016-10-14 15:46:06 -0700110
Mikhail Naganov0bbc4aa2017-12-22 13:23:08 -0800111 const char* mModuleName;
112 struct sound_trigger_hw_device* mHwDevice;
113 volatile atomic_uint_fast32_t mNextModelId;
114 DefaultKeyedVector<int32_t, sp<SoundModelClient> > mClients;
115 Mutex mLock;
Eric Laurent27ef4d82016-10-14 15:46:06 -0700116};
117
Mikhail Naganov0bbc4aa2017-12-22 13:23:08 -0800118extern "C" ISoundTriggerHw* HIDL_FETCH_ISoundTriggerHw(const char* name);
Eric Laurent27ef4d82016-10-14 15:46:06 -0700119
120} // namespace implementation
121} // namespace V2_0
122} // namespace soundtrigger
123} // namespace hardware
124} // namespace android
125
126#endif // ANDROID_HARDWARE_SOUNDTRIGGER_V2_0_IMPLEMENTATION_H