blob: 43ad6112b8d519727a3b74ba075d3c809804772f [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);
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -0700128
129 sp<SoundTriggerHalInterface> halInterface() const { return mHalInterface; }
130 struct sound_trigger_module_descriptor descriptor() { return mDescriptor; }
131 wp<SoundTriggerHwService> service() const { return mService; }
132 bool isConcurrentCaptureAllowed() const { return mDescriptor.properties.concurrent_capture; }
133
134 sp<Model> getModel(sound_model_handle_t handle);
135
136 void setCaptureState_l(bool active);
137
jiabin68e0df72019-03-18 17:55:35 -0700138 sp<ModuleClient> addClient(const sp<ISoundTriggerClient>& client,
139 const String16& opPackageName);
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -0700140
141 void detach(const sp<ModuleClient>& moduleClient);
142
143 void onCallbackEvent(const sp<CallbackEvent>& event);
144
145 private:
146
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -0700147 Mutex mLock;
148 wp<SoundTriggerHwService> mService;
149 sp<SoundTriggerHalInterface> mHalInterface;
150 struct sound_trigger_module_descriptor mDescriptor;
151 Vector< sp<ModuleClient> > mModuleClients;
152 DefaultKeyedVector< sound_model_handle_t, sp<Model> > mModels;
153 sound_trigger_service_state_t mServiceState;
154 }; // class Module
155
156 class ModuleClient : public virtual RefBase,
157 public BnSoundTrigger,
158 public IBinder::DeathRecipient {
159 public:
160
161 ModuleClient(const sp<Module>& module,
jiabin68e0df72019-03-18 17:55:35 -0700162 const sp<ISoundTriggerClient>& client,
163 const String16& opPackageName);
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -0700164
165 virtual ~ModuleClient();
166
Eric Laurentb7a11d82014-04-18 17:40:41 -0700167 virtual void detach();
168
169 virtual status_t loadSoundModel(const sp<IMemory>& modelMemory,
170 sound_model_handle_t *handle);
171
172 virtual status_t unloadSoundModel(sound_model_handle_t handle);
173
174 virtual status_t startRecognition(sound_model_handle_t handle,
175 const sp<IMemory>& dataMemory);
176 virtual status_t stopRecognition(sound_model_handle_t handle);
mike dooley6e189b12018-11-07 15:44:37 +0100177 virtual status_t getModelState(sound_model_handle_t handle);
Eric Laurentb7a11d82014-04-18 17:40:41 -0700178
179 virtual status_t dump(int fd, const Vector<String16>& args);
180
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -0700181 virtual void onFirstRef();
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700182
Eric Laurentb7a11d82014-04-18 17:40:41 -0700183 // IBinder::DeathRecipient implementation
184 virtual void binderDied(const wp<IBinder> &who);
185
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -0700186 void onCallbackEvent(const sp<CallbackEvent>& event);
187
188 void setCaptureState_l(bool active);
189
190 sp<ISoundTriggerClient> client() const { return mClient; }
191
Eric Laurentb7a11d82014-04-18 17:40:41 -0700192 private:
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700193
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -0700194 mutable Mutex mLock;
195 wp<Module> mModule;
196 sp<ISoundTriggerClient> mClient;
jiabin68e0df72019-03-18 17:55:35 -0700197 const String16 mOpPackageName;
Haynes Mathew Georgee52c5002016-10-12 17:27:18 -0700198 }; // class ModuleClient
Eric Laurentb7a11d82014-04-18 17:40:41 -0700199
Eric Laurentb7a11d82014-04-18 17:40:41 -0700200 class CallbackThread : public Thread {
201 public:
202
Chih-Hung Hsieh85f8a732016-08-09 14:10:03 -0700203 explicit CallbackThread(const wp<SoundTriggerHwService>& service);
Eric Laurentb7a11d82014-04-18 17:40:41 -0700204
205 virtual ~CallbackThread();
206
207 // Thread virtuals
208 virtual bool threadLoop();
209
210 // RefBase
211 virtual void onFirstRef();
212
213 void exit();
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700214 void sendCallbackEvent(const sp<CallbackEvent>& event);
Eric Laurentb7a11d82014-04-18 17:40:41 -0700215
216 private:
217 wp<SoundTriggerHwService> mService;
218 Condition mCallbackCond;
219 Mutex mCallbackLock;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700220 Vector< sp<CallbackEvent> > mEventQueue;
Eric Laurentb7a11d82014-04-18 17:40:41 -0700221 };
222
Eric Laurentb7a11d82014-04-18 17:40:41 -0700223 static void recognitionCallback(struct sound_trigger_recognition_event *event, void *cookie);
Chris Thornton79c56612017-10-25 14:47:44 -0700224 sp<IMemory> prepareRecognitionEvent(struct sound_trigger_recognition_event *event);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700225 void sendRecognitionEvent(struct sound_trigger_recognition_event *event, Module *module);
Eric Laurentb7a11d82014-04-18 17:40:41 -0700226
227 static void soundModelCallback(struct sound_trigger_model_event *event, void *cookie);
Chris Thornton79c56612017-10-25 14:47:44 -0700228 sp<IMemory> prepareSoundModelEvent(struct sound_trigger_model_event *event);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700229 void sendSoundModelEvent(struct sound_trigger_model_event *event, Module *module);
230
Chris Thornton07405ee2017-11-14 20:45:27 -0800231 sp<IMemory> prepareServiceStateEvent(sound_trigger_service_state_t state);
232 void sendServiceStateEvent(sound_trigger_service_state_t state, Module *module);
233 void sendServiceStateEvent(sound_trigger_service_state_t state,
234 ModuleClient *moduleClient);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700235
Chris Thornton79c56612017-10-25 14:47:44 -0700236 void sendCallbackEvent(const sp<CallbackEvent>& event);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700237 void onCallbackEvent(const sp<CallbackEvent>& event);
Eric Laurentb7a11d82014-04-18 17:40:41 -0700238
239private:
240
241 virtual void onFirstRef();
242
243 Mutex mServiceLock;
244 volatile int32_t mNextUniqueId;
245 DefaultKeyedVector< sound_trigger_module_handle_t, sp<Module> > mModules;
246 sp<CallbackThread> mCallbackThread;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700247 sp<MemoryDealer> mMemoryDealer;
Chris Thornton79c56612017-10-25 14:47:44 -0700248 Mutex mMemoryDealerLock;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700249 bool mCaptureState;
Eric Laurentb7a11d82014-04-18 17:40:41 -0700250};
251
252} // namespace android
253
254#endif // ANDROID_HARDWARE_SOUNDTRIGGER_HAL_SERVICE_H