blob: c222cd9afec791ee1b57bf182729b192801a7d08 [file] [log] [blame]
Eric Laurentb7a11d82014-04-18 17:40:41 -07001/*
2 * Copyright (C) 2008 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_SERVICE_H
18#define ANDROID_HARDWARE_SOUNDTRIGGER_HAL_SERVICE_H
19
20#include <utils/Vector.h>
21//#include <binder/AppOpsManager.h>
22#include <binder/MemoryDealer.h>
23#include <binder/BinderService.h>
24#include <binder/IAppOpsCallback.h>
25#include <soundtrigger/ISoundTriggerHwService.h>
26#include <soundtrigger/ISoundTrigger.h>
27#include <soundtrigger/ISoundTriggerClient.h>
28#include <system/sound_trigger.h>
Eric Laurent7a544b42016-08-05 19:01:13 -070029#include "SoundTriggerHalInterface.h"
Eric Laurentb7a11d82014-04-18 17:40:41 -070030
31namespace android {
32
33class MemoryHeapBase;
34
35class SoundTriggerHwService :
36 public BinderService<SoundTriggerHwService>,
37 public BnSoundTriggerHwService
38{
39 friend class BinderService<SoundTriggerHwService>;
40public:
41 class Module;
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -070042 class ModuleClient;
Eric Laurentb7a11d82014-04-18 17:40:41 -070043
44 static char const* getServiceName() { return "media.sound_trigger_hw"; }
45
46 SoundTriggerHwService();
47 virtual ~SoundTriggerHwService();
48
49 // ISoundTriggerHwService
50 virtual status_t listModules(struct sound_trigger_module_descriptor *modules,
51 uint32_t *numModules);
52
53 virtual status_t attach(const sound_trigger_module_handle_t handle,
54 const sp<ISoundTriggerClient>& client,
55 sp<ISoundTrigger>& module);
56
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070057 virtual status_t setCaptureState(bool active);
58
Eric Laurentb7a11d82014-04-18 17:40:41 -070059 virtual status_t onTransact(uint32_t code, const Parcel& data,
60 Parcel* reply, uint32_t flags);
61
62 virtual status_t dump(int fd, const Vector<String16>& args);
63
64 class Model : public RefBase {
65 public:
66
67 enum {
68 STATE_IDLE,
69 STATE_ACTIVE
70 };
71
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070072 Model(sound_model_handle_t handle, audio_session_t session, audio_io_handle_t ioHandle,
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -070073 audio_devices_t device, sound_trigger_sound_model_type_t type,
74 sp<ModuleClient>& moduleClient);
Eric Laurentb7a11d82014-04-18 17:40:41 -070075 ~Model() {}
76
Eric Laurentb7a11d82014-04-18 17:40:41 -070077 sound_model_handle_t mHandle;
78 int mState;
Eric Laurentb7a11d82014-04-18 17:40:41 -070079 audio_session_t mCaptureSession;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070080 audio_io_handle_t mCaptureIOHandle;
81 audio_devices_t mCaptureDevice;
82 sound_trigger_sound_model_type_t mType;
83 struct sound_trigger_recognition_config mConfig;
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -070084 sp<ModuleClient> mModuleClient;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070085 };
86
87 class CallbackEvent : public RefBase {
88 public:
89 typedef enum {
90 TYPE_RECOGNITION,
91 TYPE_SOUNDMODEL,
92 TYPE_SERVICE_STATE,
93 } event_type;
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -070094 CallbackEvent(event_type type, sp<IMemory> memory);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070095
96 virtual ~CallbackEvent();
97
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -070098 void setModule(wp<Module> module) { mModule = module; }
99 void setModuleClient(wp<ModuleClient> moduleClient) { mModuleClient = moduleClient; }
100
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700101 event_type mType;
102 sp<IMemory> mMemory;
103 wp<Module> mModule;
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -0700104 wp<ModuleClient> mModuleClient;
Eric Laurentb7a11d82014-04-18 17:40:41 -0700105 };
106
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -0700107 class Module : public RefBase {
Eric Laurentb7a11d82014-04-18 17:40:41 -0700108 public:
109
110 Module(const sp<SoundTriggerHwService>& service,
Eric Laurent7a544b42016-08-05 19:01:13 -0700111 const sp<SoundTriggerHalInterface>& halInterface,
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -0700112 sound_trigger_module_descriptor descriptor);
Eric Laurentb7a11d82014-04-18 17:40:41 -0700113
114 virtual ~Module();
115
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -0700116 virtual status_t loadSoundModel(const sp<IMemory>& modelMemory,
117 sp<ModuleClient> moduleClient,
118 sound_model_handle_t *handle);
119
120 virtual status_t unloadSoundModel(sound_model_handle_t handle);
121
122 virtual status_t startRecognition(sound_model_handle_t handle,
123 const sp<IMemory>& dataMemory);
124 virtual status_t stopRecognition(sound_model_handle_t handle);
Michael Dooley67e3d412018-10-16 19:51:16 +0000125 virtual status_t getModelState(sound_model_handle_t handle,
126 sp<IMemory>& eventMemory);
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -0700127
128 sp<SoundTriggerHalInterface> halInterface() const { return mHalInterface; }
129 struct sound_trigger_module_descriptor descriptor() { return mDescriptor; }
130 wp<SoundTriggerHwService> service() const { return mService; }
131 bool isConcurrentCaptureAllowed() const { return mDescriptor.properties.concurrent_capture; }
132
133 sp<Model> getModel(sound_model_handle_t handle);
134
135 void setCaptureState_l(bool active);
136
137 sp<ModuleClient> addClient(const sp<ISoundTriggerClient>& client);
138
139 void detach(const sp<ModuleClient>& moduleClient);
140
141 void onCallbackEvent(const sp<CallbackEvent>& event);
142
143 private:
144
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -0700145 Mutex mLock;
146 wp<SoundTriggerHwService> mService;
147 sp<SoundTriggerHalInterface> mHalInterface;
148 struct sound_trigger_module_descriptor mDescriptor;
149 Vector< sp<ModuleClient> > mModuleClients;
150 DefaultKeyedVector< sound_model_handle_t, sp<Model> > mModels;
151 sound_trigger_service_state_t mServiceState;
152 }; // class Module
153
154 class ModuleClient : public virtual RefBase,
155 public BnSoundTrigger,
156 public IBinder::DeathRecipient {
157 public:
158
159 ModuleClient(const sp<Module>& module,
160 const sp<ISoundTriggerClient>& client);
161
162 virtual ~ModuleClient();
163
Eric Laurentb7a11d82014-04-18 17:40:41 -0700164 virtual void detach();
165
166 virtual status_t loadSoundModel(const sp<IMemory>& modelMemory,
167 sound_model_handle_t *handle);
168
169 virtual status_t unloadSoundModel(sound_model_handle_t handle);
170
171 virtual status_t startRecognition(sound_model_handle_t handle,
172 const sp<IMemory>& dataMemory);
173 virtual status_t stopRecognition(sound_model_handle_t handle);
Michael Dooley67e3d412018-10-16 19:51:16 +0000174 virtual status_t getModelState(sound_model_handle_t handle,
175 sp<IMemory>& eventMemory);
Eric Laurentb7a11d82014-04-18 17:40:41 -0700176
177 virtual status_t dump(int fd, const Vector<String16>& args);
178
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -0700179 virtual void onFirstRef();
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700180
Eric Laurentb7a11d82014-04-18 17:40:41 -0700181 // IBinder::DeathRecipient implementation
182 virtual void binderDied(const wp<IBinder> &who);
183
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -0700184 void onCallbackEvent(const sp<CallbackEvent>& event);
185
186 void setCaptureState_l(bool active);
187
188 sp<ISoundTriggerClient> client() const { return mClient; }
189
Eric Laurentb7a11d82014-04-18 17:40:41 -0700190 private:
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700191
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -0700192 mutable Mutex mLock;
193 wp<Module> mModule;
194 sp<ISoundTriggerClient> mClient;
195 }; // class ModuleClient
Eric Laurentb7a11d82014-04-18 17:40:41 -0700196
Eric Laurentb7a11d82014-04-18 17:40:41 -0700197 class CallbackThread : public Thread {
198 public:
199
Chih-Hung Hsieh85f8a732016-08-09 14:10:03 -0700200 explicit CallbackThread(const wp<SoundTriggerHwService>& service);
Eric Laurentb7a11d82014-04-18 17:40:41 -0700201
202 virtual ~CallbackThread();
203
204 // Thread virtuals
205 virtual bool threadLoop();
206
207 // RefBase
208 virtual void onFirstRef();
209
210 void exit();
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700211 void sendCallbackEvent(const sp<CallbackEvent>& event);
Eric Laurentb7a11d82014-04-18 17:40:41 -0700212
213 private:
214 wp<SoundTriggerHwService> mService;
215 Condition mCallbackCond;
216 Mutex mCallbackLock;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700217 Vector< sp<CallbackEvent> > mEventQueue;
Eric Laurentb7a11d82014-04-18 17:40:41 -0700218 };
219
Eric Laurentb7a11d82014-04-18 17:40:41 -0700220 static void recognitionCallback(struct sound_trigger_recognition_event *event, void *cookie);
Chris Thornton79c56612017-10-25 14:47:44 -0700221 sp<IMemory> prepareRecognitionEvent(struct sound_trigger_recognition_event *event);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700222 void sendRecognitionEvent(struct sound_trigger_recognition_event *event, Module *module);
Eric Laurentb7a11d82014-04-18 17:40:41 -0700223
224 static void soundModelCallback(struct sound_trigger_model_event *event, void *cookie);
Chris Thornton79c56612017-10-25 14:47:44 -0700225 sp<IMemory> prepareSoundModelEvent(struct sound_trigger_model_event *event);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700226 void sendSoundModelEvent(struct sound_trigger_model_event *event, Module *module);
227
Chris Thornton07405ee2017-11-14 20:45:27 -0800228 sp<IMemory> prepareServiceStateEvent(sound_trigger_service_state_t state);
229 void sendServiceStateEvent(sound_trigger_service_state_t state, Module *module);
230 void sendServiceStateEvent(sound_trigger_service_state_t state,
231 ModuleClient *moduleClient);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700232
Chris Thornton79c56612017-10-25 14:47:44 -0700233 void sendCallbackEvent(const sp<CallbackEvent>& event);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700234 void onCallbackEvent(const sp<CallbackEvent>& event);
Eric Laurentb7a11d82014-04-18 17:40:41 -0700235
236private:
237
238 virtual void onFirstRef();
239
240 Mutex mServiceLock;
241 volatile int32_t mNextUniqueId;
242 DefaultKeyedVector< sound_trigger_module_handle_t, sp<Module> > mModules;
243 sp<CallbackThread> mCallbackThread;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700244 sp<MemoryDealer> mMemoryDealer;
Chris Thornton79c56612017-10-25 14:47:44 -0700245 Mutex mMemoryDealerLock;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700246 bool mCaptureState;
Eric Laurentb7a11d82014-04-18 17:40:41 -0700247};
248
249} // namespace android
250
251#endif // ANDROID_HARDWARE_SOUNDTRIGGER_HAL_SERVICE_H