blob: 4057e14adbec8a84f19ea379158c0aae585ce1d9 [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
jiabin68e0df72019-03-18 17:55:35 -070050 virtual status_t listModules(const String16& opPackageName,
51 struct sound_trigger_module_descriptor *modules,
Eric Laurentb7a11d82014-04-18 17:40:41 -070052 uint32_t *numModules);
53
jiabin68e0df72019-03-18 17:55:35 -070054 virtual status_t attach(const String16& opPackageName,
55 const sound_trigger_module_handle_t handle,
Eric Laurentb7a11d82014-04-18 17:40:41 -070056 const sp<ISoundTriggerClient>& client,
57 sp<ISoundTrigger>& module);
58
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070059 virtual status_t setCaptureState(bool active);
60
Eric Laurentb7a11d82014-04-18 17:40:41 -070061 virtual status_t onTransact(uint32_t code, const Parcel& data,
62 Parcel* reply, uint32_t flags);
63
64 virtual status_t dump(int fd, const Vector<String16>& args);
65
66 class Model : public RefBase {
67 public:
68
69 enum {
70 STATE_IDLE,
71 STATE_ACTIVE
72 };
73
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070074 Model(sound_model_handle_t handle, audio_session_t session, audio_io_handle_t ioHandle,
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -070075 audio_devices_t device, sound_trigger_sound_model_type_t type,
76 sp<ModuleClient>& moduleClient);
Eric Laurentb7a11d82014-04-18 17:40:41 -070077 ~Model() {}
78
Eric Laurentb7a11d82014-04-18 17:40:41 -070079 sound_model_handle_t mHandle;
80 int mState;
Eric Laurentb7a11d82014-04-18 17:40:41 -070081 audio_session_t mCaptureSession;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070082 audio_io_handle_t mCaptureIOHandle;
83 audio_devices_t mCaptureDevice;
84 sound_trigger_sound_model_type_t mType;
85 struct sound_trigger_recognition_config mConfig;
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -070086 sp<ModuleClient> mModuleClient;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070087 };
88
89 class CallbackEvent : public RefBase {
90 public:
91 typedef enum {
92 TYPE_RECOGNITION,
93 TYPE_SOUNDMODEL,
94 TYPE_SERVICE_STATE,
95 } event_type;
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -070096 CallbackEvent(event_type type, sp<IMemory> memory);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070097
98 virtual ~CallbackEvent();
99
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -0700100 void setModule(wp<Module> module) { mModule = module; }
101 void setModuleClient(wp<ModuleClient> moduleClient) { mModuleClient = moduleClient; }
102
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700103 event_type mType;
104 sp<IMemory> mMemory;
105 wp<Module> mModule;
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -0700106 wp<ModuleClient> mModuleClient;
Eric Laurentb7a11d82014-04-18 17:40:41 -0700107 };
108
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -0700109 class Module : public RefBase {
Eric Laurentb7a11d82014-04-18 17:40:41 -0700110 public:
111
112 Module(const sp<SoundTriggerHwService>& service,
Eric Laurent7a544b42016-08-05 19:01:13 -0700113 const sp<SoundTriggerHalInterface>& halInterface,
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -0700114 sound_trigger_module_descriptor descriptor);
Eric Laurentb7a11d82014-04-18 17:40:41 -0700115
116 virtual ~Module();
117
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -0700118 virtual status_t loadSoundModel(const sp<IMemory>& modelMemory,
119 sp<ModuleClient> moduleClient,
120 sound_model_handle_t *handle);
121
122 virtual status_t unloadSoundModel(sound_model_handle_t handle);
123
124 virtual status_t startRecognition(sound_model_handle_t handle,
125 const sp<IMemory>& dataMemory);
126 virtual status_t stopRecognition(sound_model_handle_t handle);
mike dooley6e189b12018-11-07 15:44:37 +0100127 virtual status_t getModelState(sound_model_handle_t handle);
Nicholas Ambur41947e22019-10-01 11:53:40 -0700128 virtual status_t setParameter(sound_model_handle_t handle,
129 sound_trigger_model_parameter_t param,
130 int32_t value);
131 virtual status_t getParameter(sound_model_handle_t handle,
132 sound_trigger_model_parameter_t param,
133 int32_t* value);
134 virtual status_t queryParameter(sound_model_handle_t handle,
135 sound_trigger_model_parameter_t param,
136 sound_trigger_model_parameter_range_t* param_range);
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -0700137
138 sp<SoundTriggerHalInterface> halInterface() const { return mHalInterface; }
139 struct sound_trigger_module_descriptor descriptor() { return mDescriptor; }
140 wp<SoundTriggerHwService> service() const { return mService; }
141 bool isConcurrentCaptureAllowed() const { return mDescriptor.properties.concurrent_capture; }
142
143 sp<Model> getModel(sound_model_handle_t handle);
144
145 void setCaptureState_l(bool active);
146
jiabin68e0df72019-03-18 17:55:35 -0700147 sp<ModuleClient> addClient(const sp<ISoundTriggerClient>& client,
148 const String16& opPackageName);
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -0700149
150 void detach(const sp<ModuleClient>& moduleClient);
151
152 void onCallbackEvent(const sp<CallbackEvent>& event);
153
154 private:
155
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -0700156 Mutex mLock;
157 wp<SoundTriggerHwService> mService;
158 sp<SoundTriggerHalInterface> mHalInterface;
159 struct sound_trigger_module_descriptor mDescriptor;
160 Vector< sp<ModuleClient> > mModuleClients;
161 DefaultKeyedVector< sound_model_handle_t, sp<Model> > mModels;
162 sound_trigger_service_state_t mServiceState;
163 }; // class Module
164
165 class ModuleClient : public virtual RefBase,
166 public BnSoundTrigger,
167 public IBinder::DeathRecipient {
168 public:
169
170 ModuleClient(const sp<Module>& module,
jiabin68e0df72019-03-18 17:55:35 -0700171 const sp<ISoundTriggerClient>& client,
172 const String16& opPackageName);
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -0700173
174 virtual ~ModuleClient();
175
Eric Laurentb7a11d82014-04-18 17:40:41 -0700176 virtual void detach();
177
178 virtual status_t loadSoundModel(const sp<IMemory>& modelMemory,
179 sound_model_handle_t *handle);
180
181 virtual status_t unloadSoundModel(sound_model_handle_t handle);
182
183 virtual status_t startRecognition(sound_model_handle_t handle,
184 const sp<IMemory>& dataMemory);
185 virtual status_t stopRecognition(sound_model_handle_t handle);
mike dooley6e189b12018-11-07 15:44:37 +0100186 virtual status_t getModelState(sound_model_handle_t handle);
Nicholas Ambur41947e22019-10-01 11:53:40 -0700187 virtual status_t setParameter(sound_model_handle_t handle,
188 sound_trigger_model_parameter_t param,
189 int32_t value);
190 virtual status_t getParameter(sound_model_handle_t handle,
191 sound_trigger_model_parameter_t param,
192 int32_t* value);
193 virtual status_t queryParameter(sound_model_handle_t handle,
194 sound_trigger_model_parameter_t param,
195 sound_trigger_model_parameter_range_t* param_range);
Eric Laurentb7a11d82014-04-18 17:40:41 -0700196
197 virtual status_t dump(int fd, const Vector<String16>& args);
198
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -0700199 virtual void onFirstRef();
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700200
Eric Laurentb7a11d82014-04-18 17:40:41 -0700201 // IBinder::DeathRecipient implementation
202 virtual void binderDied(const wp<IBinder> &who);
203
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -0700204 void onCallbackEvent(const sp<CallbackEvent>& event);
205
206 void setCaptureState_l(bool active);
207
208 sp<ISoundTriggerClient> client() const { return mClient; }
209
Eric Laurentb7a11d82014-04-18 17:40:41 -0700210 private:
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700211
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -0700212 mutable Mutex mLock;
213 wp<Module> mModule;
214 sp<ISoundTriggerClient> mClient;
jiabin68e0df72019-03-18 17:55:35 -0700215 const String16 mOpPackageName;
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -0700216 }; // class ModuleClient
Eric Laurentb7a11d82014-04-18 17:40:41 -0700217
Eric Laurentb7a11d82014-04-18 17:40:41 -0700218 class CallbackThread : public Thread {
219 public:
220
Chih-Hung Hsieh85f8a732016-08-09 14:10:03 -0700221 explicit CallbackThread(const wp<SoundTriggerHwService>& service);
Eric Laurentb7a11d82014-04-18 17:40:41 -0700222
223 virtual ~CallbackThread();
224
225 // Thread virtuals
226 virtual bool threadLoop();
227
228 // RefBase
229 virtual void onFirstRef();
230
231 void exit();
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700232 void sendCallbackEvent(const sp<CallbackEvent>& event);
Eric Laurentb7a11d82014-04-18 17:40:41 -0700233
234 private:
235 wp<SoundTriggerHwService> mService;
236 Condition mCallbackCond;
237 Mutex mCallbackLock;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700238 Vector< sp<CallbackEvent> > mEventQueue;
Eric Laurentb7a11d82014-04-18 17:40:41 -0700239 };
240
Eric Laurentb7a11d82014-04-18 17:40:41 -0700241 static void recognitionCallback(struct sound_trigger_recognition_event *event, void *cookie);
Chris Thornton79c56612017-10-25 14:47:44 -0700242 sp<IMemory> prepareRecognitionEvent(struct sound_trigger_recognition_event *event);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700243 void sendRecognitionEvent(struct sound_trigger_recognition_event *event, Module *module);
Eric Laurentb7a11d82014-04-18 17:40:41 -0700244
245 static void soundModelCallback(struct sound_trigger_model_event *event, void *cookie);
Chris Thornton79c56612017-10-25 14:47:44 -0700246 sp<IMemory> prepareSoundModelEvent(struct sound_trigger_model_event *event);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700247 void sendSoundModelEvent(struct sound_trigger_model_event *event, Module *module);
248
Chris Thornton07405ee2017-11-14 20:45:27 -0800249 sp<IMemory> prepareServiceStateEvent(sound_trigger_service_state_t state);
250 void sendServiceStateEvent(sound_trigger_service_state_t state, Module *module);
251 void sendServiceStateEvent(sound_trigger_service_state_t state,
252 ModuleClient *moduleClient);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700253
Chris Thornton79c56612017-10-25 14:47:44 -0700254 void sendCallbackEvent(const sp<CallbackEvent>& event);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700255 void onCallbackEvent(const sp<CallbackEvent>& event);
Eric Laurentb7a11d82014-04-18 17:40:41 -0700256
257private:
258
259 virtual void onFirstRef();
260
261 Mutex mServiceLock;
262 volatile int32_t mNextUniqueId;
263 DefaultKeyedVector< sound_trigger_module_handle_t, sp<Module> > mModules;
264 sp<CallbackThread> mCallbackThread;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700265 sp<MemoryDealer> mMemoryDealer;
Chris Thornton79c56612017-10-25 14:47:44 -0700266 Mutex mMemoryDealerLock;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700267 bool mCaptureState;
Eric Laurentb7a11d82014-04-18 17:40:41 -0700268};
269
270} // namespace android
271
272#endif // ANDROID_HARDWARE_SOUNDTRIGGER_HAL_SERVICE_H