| Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 29 | #include "SoundTriggerHalInterface.h" | 
| Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 30 |  | 
|  | 31 | namespace android { | 
|  | 32 |  | 
|  | 33 | class MemoryHeapBase; | 
|  | 34 |  | 
|  | 35 | class SoundTriggerHwService : | 
|  | 36 | public BinderService<SoundTriggerHwService>, | 
|  | 37 | public BnSoundTriggerHwService | 
|  | 38 | { | 
|  | 39 | friend class BinderService<SoundTriggerHwService>; | 
|  | 40 | public: | 
|  | 41 | class Module; | 
| Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 42 | class ModuleClient; | 
| Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 43 |  | 
|  | 44 | static char const* getServiceName() { return "media.sound_trigger_hw"; } | 
|  | 45 |  | 
|  | 46 | SoundTriggerHwService(); | 
|  | 47 | virtual             ~SoundTriggerHwService(); | 
|  | 48 |  | 
|  | 49 | // ISoundTriggerHwService | 
| jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 50 | virtual status_t listModules(const String16& opPackageName, | 
|  | 51 | struct sound_trigger_module_descriptor *modules, | 
| Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 52 | uint32_t *numModules); | 
|  | 53 |  | 
| jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 54 | virtual status_t attach(const String16& opPackageName, | 
|  | 55 | const sound_trigger_module_handle_t handle, | 
| Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 56 | const sp<ISoundTriggerClient>& client, | 
|  | 57 | sp<ISoundTrigger>& module); | 
|  | 58 |  | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 59 | virtual status_t setCaptureState(bool active); | 
|  | 60 |  | 
| Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 61 | 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 Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 74 | Model(sound_model_handle_t handle, audio_session_t session, audio_io_handle_t ioHandle, | 
| Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 75 | audio_devices_t device, sound_trigger_sound_model_type_t type, | 
|  | 76 | sp<ModuleClient>& moduleClient); | 
| Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 77 | ~Model() {} | 
|  | 78 |  | 
| Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 79 | sound_model_handle_t    mHandle; | 
|  | 80 | int                     mState; | 
| Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 81 | audio_session_t         mCaptureSession; | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 82 | 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 George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 86 | sp<ModuleClient>        mModuleClient; | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 87 | }; | 
|  | 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 George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 96 | CallbackEvent(event_type type, sp<IMemory> memory); | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 97 |  | 
|  | 98 | virtual             ~CallbackEvent(); | 
|  | 99 |  | 
| Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 100 | void setModule(wp<Module> module) { mModule = module; } | 
|  | 101 | void setModuleClient(wp<ModuleClient> moduleClient) { mModuleClient = moduleClient; } | 
|  | 102 |  | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 103 | event_type mType; | 
|  | 104 | sp<IMemory> mMemory; | 
|  | 105 | wp<Module> mModule; | 
| Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 106 | wp<ModuleClient> mModuleClient; | 
| Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 107 | }; | 
|  | 108 |  | 
| Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 109 | class Module : public RefBase { | 
| Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 110 | public: | 
|  | 111 |  | 
|  | 112 | Module(const sp<SoundTriggerHwService>& service, | 
| Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 113 | const sp<SoundTriggerHalInterface>& halInterface, | 
| Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 114 | sound_trigger_module_descriptor descriptor); | 
| Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 115 |  | 
|  | 116 | virtual ~Module(); | 
|  | 117 |  | 
| Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 118 | 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 dooley | 6e189b1 | 2018-11-07 15:44:37 +0100 | [diff] [blame] | 127 | virtual status_t getModelState(sound_model_handle_t handle); | 
| Nicholas Ambur | 41947e2 | 2019-10-01 11:53:40 -0700 | [diff] [blame] | 128 | 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 George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 137 |  | 
|  | 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 |  | 
| jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 147 | sp<ModuleClient> addClient(const sp<ISoundTriggerClient>& client, | 
|  | 148 | const String16& opPackageName); | 
| Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 149 |  | 
|  | 150 | void detach(const sp<ModuleClient>& moduleClient); | 
|  | 151 |  | 
|  | 152 | void onCallbackEvent(const sp<CallbackEvent>& event); | 
|  | 153 |  | 
|  | 154 | private: | 
|  | 155 |  | 
| Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 156 | 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, | 
| jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 171 | const sp<ISoundTriggerClient>& client, | 
|  | 172 | const String16& opPackageName); | 
| Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 173 |  | 
|  | 174 | virtual ~ModuleClient(); | 
|  | 175 |  | 
| Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 176 | 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 dooley | 6e189b1 | 2018-11-07 15:44:37 +0100 | [diff] [blame] | 186 | virtual status_t getModelState(sound_model_handle_t handle); | 
| Nicholas Ambur | 41947e2 | 2019-10-01 11:53:40 -0700 | [diff] [blame] | 187 | 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 Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 196 |  | 
|  | 197 | virtual status_t dump(int fd, const Vector<String16>& args); | 
|  | 198 |  | 
| Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 199 | virtual void onFirstRef(); | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 200 |  | 
| Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 201 | // IBinder::DeathRecipient implementation | 
|  | 202 | virtual void        binderDied(const wp<IBinder> &who); | 
|  | 203 |  | 
| Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 204 | void onCallbackEvent(const sp<CallbackEvent>& event); | 
|  | 205 |  | 
|  | 206 | void setCaptureState_l(bool active); | 
|  | 207 |  | 
|  | 208 | sp<ISoundTriggerClient> client() const { return mClient; } | 
|  | 209 |  | 
| Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 210 | private: | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 211 |  | 
| Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 212 | mutable Mutex               mLock; | 
|  | 213 | wp<Module>                  mModule; | 
|  | 214 | sp<ISoundTriggerClient>     mClient; | 
| jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 215 | const String16              mOpPackageName; | 
| Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 216 | }; // class ModuleClient | 
| Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 217 |  | 
| Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 218 | class CallbackThread : public Thread { | 
|  | 219 | public: | 
|  | 220 |  | 
| Chih-Hung Hsieh | 85f8a73 | 2016-08-09 14:10:03 -0700 | [diff] [blame] | 221 | explicit CallbackThread(const wp<SoundTriggerHwService>& service); | 
| Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 222 |  | 
|  | 223 | virtual             ~CallbackThread(); | 
|  | 224 |  | 
|  | 225 | // Thread virtuals | 
|  | 226 | virtual bool        threadLoop(); | 
|  | 227 |  | 
|  | 228 | // RefBase | 
|  | 229 | virtual void        onFirstRef(); | 
|  | 230 |  | 
|  | 231 | void        exit(); | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 232 | void        sendCallbackEvent(const sp<CallbackEvent>& event); | 
| Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 233 |  | 
|  | 234 | private: | 
|  | 235 | wp<SoundTriggerHwService>   mService; | 
|  | 236 | Condition                   mCallbackCond; | 
|  | 237 | Mutex                       mCallbackLock; | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 238 | Vector< sp<CallbackEvent> > mEventQueue; | 
| Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 239 | }; | 
|  | 240 |  | 
| Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 241 | static void recognitionCallback(struct sound_trigger_recognition_event *event, void *cookie); | 
| Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 242 | sp<IMemory> prepareRecognitionEvent(struct sound_trigger_recognition_event *event); | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 243 | void sendRecognitionEvent(struct sound_trigger_recognition_event *event, Module *module); | 
| Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 244 |  | 
|  | 245 | static void soundModelCallback(struct sound_trigger_model_event *event, void *cookie); | 
| Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 246 | sp<IMemory> prepareSoundModelEvent(struct sound_trigger_model_event *event); | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 247 | void sendSoundModelEvent(struct sound_trigger_model_event *event, Module *module); | 
|  | 248 |  | 
| Chris Thornton | 07405ee | 2017-11-14 20:45:27 -0800 | [diff] [blame] | 249 | 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 Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 253 |  | 
| Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 254 | void sendCallbackEvent(const sp<CallbackEvent>& event); | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 255 | void onCallbackEvent(const sp<CallbackEvent>& event); | 
| Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 256 |  | 
|  | 257 | private: | 
|  | 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 Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 265 | sp<MemoryDealer>    mMemoryDealer; | 
| Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 266 | Mutex               mMemoryDealerLock; | 
| Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 267 | bool                mCaptureState; | 
| Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 268 | }; | 
|  | 269 |  | 
|  | 270 | } // namespace android | 
|  | 271 |  | 
|  | 272 | #endif // ANDROID_HARDWARE_SOUNDTRIGGER_HAL_SERVICE_H |