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); |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 128 | |
| 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 | |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 138 | sp<ModuleClient> addClient(const sp<ISoundTriggerClient>& client, |
| 139 | const String16& opPackageName); |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 140 | |
| 141 | void detach(const sp<ModuleClient>& moduleClient); |
| 142 | |
| 143 | void onCallbackEvent(const sp<CallbackEvent>& event); |
| 144 | |
| 145 | private: |
| 146 | |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 147 | 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, |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 162 | const sp<ISoundTriggerClient>& client, |
| 163 | const String16& opPackageName); |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 164 | |
| 165 | virtual ~ModuleClient(); |
| 166 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 167 | 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 dooley | 6e189b1 | 2018-11-07 15:44:37 +0100 | [diff] [blame] | 177 | virtual status_t getModelState(sound_model_handle_t handle); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 178 | |
| 179 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 180 | |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 181 | virtual void onFirstRef(); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 182 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 183 | // IBinder::DeathRecipient implementation |
| 184 | virtual void binderDied(const wp<IBinder> &who); |
| 185 | |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 186 | void onCallbackEvent(const sp<CallbackEvent>& event); |
| 187 | |
| 188 | void setCaptureState_l(bool active); |
| 189 | |
| 190 | sp<ISoundTriggerClient> client() const { return mClient; } |
| 191 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 192 | private: |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 193 | |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 194 | mutable Mutex mLock; |
| 195 | wp<Module> mModule; |
| 196 | sp<ISoundTriggerClient> mClient; |
jiabin | 68e0df7 | 2019-03-18 17:55:35 -0700 | [diff] [blame] | 197 | const String16 mOpPackageName; |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 198 | }; // class ModuleClient |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 199 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 200 | class CallbackThread : public Thread { |
| 201 | public: |
| 202 | |
Chih-Hung Hsieh | 85f8a73 | 2016-08-09 14:10:03 -0700 | [diff] [blame] | 203 | explicit CallbackThread(const wp<SoundTriggerHwService>& service); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 204 | |
| 205 | virtual ~CallbackThread(); |
| 206 | |
| 207 | // Thread virtuals |
| 208 | virtual bool threadLoop(); |
| 209 | |
| 210 | // RefBase |
| 211 | virtual void onFirstRef(); |
| 212 | |
| 213 | void exit(); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 214 | void sendCallbackEvent(const sp<CallbackEvent>& event); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 215 | |
| 216 | private: |
| 217 | wp<SoundTriggerHwService> mService; |
| 218 | Condition mCallbackCond; |
| 219 | Mutex mCallbackLock; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 220 | Vector< sp<CallbackEvent> > mEventQueue; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 221 | }; |
| 222 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 223 | static void recognitionCallback(struct sound_trigger_recognition_event *event, void *cookie); |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 224 | sp<IMemory> prepareRecognitionEvent(struct sound_trigger_recognition_event *event); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 225 | void sendRecognitionEvent(struct sound_trigger_recognition_event *event, Module *module); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 226 | |
| 227 | static void soundModelCallback(struct sound_trigger_model_event *event, void *cookie); |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 228 | sp<IMemory> prepareSoundModelEvent(struct sound_trigger_model_event *event); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 229 | void sendSoundModelEvent(struct sound_trigger_model_event *event, Module *module); |
| 230 | |
Chris Thornton | 07405ee | 2017-11-14 20:45:27 -0800 | [diff] [blame] | 231 | 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 Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 235 | |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 236 | void sendCallbackEvent(const sp<CallbackEvent>& event); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 237 | void onCallbackEvent(const sp<CallbackEvent>& event); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 238 | |
| 239 | private: |
| 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 Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 247 | sp<MemoryDealer> mMemoryDealer; |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 248 | Mutex mMemoryDealerLock; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 249 | bool mCaptureState; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 250 | }; |
| 251 | |
| 252 | } // namespace android |
| 253 | |
| 254 | #endif // ANDROID_HARDWARE_SOUNDTRIGGER_HAL_SERVICE_H |