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 |
| 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 Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 57 | virtual status_t setCaptureState(bool active); |
| 58 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 59 | 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 Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 72 | 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] | 73 | audio_devices_t device, sound_trigger_sound_model_type_t type, |
| 74 | sp<ModuleClient>& moduleClient); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 75 | ~Model() {} |
| 76 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 77 | sound_model_handle_t mHandle; |
| 78 | int mState; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 79 | audio_session_t mCaptureSession; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 80 | 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 George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 84 | sp<ModuleClient> mModuleClient; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 85 | }; |
| 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 George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 94 | CallbackEvent(event_type type, sp<IMemory> memory); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 95 | |
| 96 | virtual ~CallbackEvent(); |
| 97 | |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 98 | void setModule(wp<Module> module) { mModule = module; } |
| 99 | void setModuleClient(wp<ModuleClient> moduleClient) { mModuleClient = moduleClient; } |
| 100 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 101 | event_type mType; |
| 102 | sp<IMemory> mMemory; |
| 103 | wp<Module> mModule; |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 104 | wp<ModuleClient> mModuleClient; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 107 | class Module : public RefBase { |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 108 | public: |
| 109 | |
| 110 | Module(const sp<SoundTriggerHwService>& service, |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 111 | const sp<SoundTriggerHalInterface>& halInterface, |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 112 | sound_trigger_module_descriptor descriptor); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 113 | |
| 114 | virtual ~Module(); |
| 115 | |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 116 | 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); |
mike dooley | 6e189b1 | 2018-11-07 15:44:37 +0100 | [diff] [blame^] | 125 | virtual status_t getModelState(sound_model_handle_t handle); |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 126 | |
| 127 | sp<SoundTriggerHalInterface> halInterface() const { return mHalInterface; } |
| 128 | struct sound_trigger_module_descriptor descriptor() { return mDescriptor; } |
| 129 | wp<SoundTriggerHwService> service() const { return mService; } |
| 130 | bool isConcurrentCaptureAllowed() const { return mDescriptor.properties.concurrent_capture; } |
| 131 | |
| 132 | sp<Model> getModel(sound_model_handle_t handle); |
| 133 | |
| 134 | void setCaptureState_l(bool active); |
| 135 | |
| 136 | sp<ModuleClient> addClient(const sp<ISoundTriggerClient>& client); |
| 137 | |
| 138 | void detach(const sp<ModuleClient>& moduleClient); |
| 139 | |
| 140 | void onCallbackEvent(const sp<CallbackEvent>& event); |
| 141 | |
| 142 | private: |
| 143 | |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 144 | Mutex mLock; |
| 145 | wp<SoundTriggerHwService> mService; |
| 146 | sp<SoundTriggerHalInterface> mHalInterface; |
| 147 | struct sound_trigger_module_descriptor mDescriptor; |
| 148 | Vector< sp<ModuleClient> > mModuleClients; |
| 149 | DefaultKeyedVector< sound_model_handle_t, sp<Model> > mModels; |
| 150 | sound_trigger_service_state_t mServiceState; |
| 151 | }; // class Module |
| 152 | |
| 153 | class ModuleClient : public virtual RefBase, |
| 154 | public BnSoundTrigger, |
| 155 | public IBinder::DeathRecipient { |
| 156 | public: |
| 157 | |
| 158 | ModuleClient(const sp<Module>& module, |
| 159 | const sp<ISoundTriggerClient>& client); |
| 160 | |
| 161 | virtual ~ModuleClient(); |
| 162 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 163 | virtual void detach(); |
| 164 | |
| 165 | virtual status_t loadSoundModel(const sp<IMemory>& modelMemory, |
| 166 | sound_model_handle_t *handle); |
| 167 | |
| 168 | virtual status_t unloadSoundModel(sound_model_handle_t handle); |
| 169 | |
| 170 | virtual status_t startRecognition(sound_model_handle_t handle, |
| 171 | const sp<IMemory>& dataMemory); |
| 172 | virtual status_t stopRecognition(sound_model_handle_t handle); |
mike dooley | 6e189b1 | 2018-11-07 15:44:37 +0100 | [diff] [blame^] | 173 | virtual status_t getModelState(sound_model_handle_t handle); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 174 | |
| 175 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 176 | |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 177 | virtual void onFirstRef(); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 178 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 179 | // IBinder::DeathRecipient implementation |
| 180 | virtual void binderDied(const wp<IBinder> &who); |
| 181 | |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 182 | void onCallbackEvent(const sp<CallbackEvent>& event); |
| 183 | |
| 184 | void setCaptureState_l(bool active); |
| 185 | |
| 186 | sp<ISoundTriggerClient> client() const { return mClient; } |
| 187 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 188 | private: |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 189 | |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 190 | mutable Mutex mLock; |
| 191 | wp<Module> mModule; |
| 192 | sp<ISoundTriggerClient> mClient; |
| 193 | }; // class ModuleClient |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 194 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 195 | class CallbackThread : public Thread { |
| 196 | public: |
| 197 | |
Chih-Hung Hsieh | 85f8a73 | 2016-08-09 14:10:03 -0700 | [diff] [blame] | 198 | explicit CallbackThread(const wp<SoundTriggerHwService>& service); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 199 | |
| 200 | virtual ~CallbackThread(); |
| 201 | |
| 202 | // Thread virtuals |
| 203 | virtual bool threadLoop(); |
| 204 | |
| 205 | // RefBase |
| 206 | virtual void onFirstRef(); |
| 207 | |
| 208 | void exit(); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 209 | void sendCallbackEvent(const sp<CallbackEvent>& event); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 210 | |
| 211 | private: |
| 212 | wp<SoundTriggerHwService> mService; |
| 213 | Condition mCallbackCond; |
| 214 | Mutex mCallbackLock; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 215 | Vector< sp<CallbackEvent> > mEventQueue; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 216 | }; |
| 217 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 218 | static void recognitionCallback(struct sound_trigger_recognition_event *event, void *cookie); |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 219 | sp<IMemory> prepareRecognitionEvent(struct sound_trigger_recognition_event *event); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 220 | void sendRecognitionEvent(struct sound_trigger_recognition_event *event, Module *module); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 221 | |
| 222 | static void soundModelCallback(struct sound_trigger_model_event *event, void *cookie); |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 223 | sp<IMemory> prepareSoundModelEvent(struct sound_trigger_model_event *event); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 224 | void sendSoundModelEvent(struct sound_trigger_model_event *event, Module *module); |
| 225 | |
Chris Thornton | 07405ee | 2017-11-14 20:45:27 -0800 | [diff] [blame] | 226 | sp<IMemory> prepareServiceStateEvent(sound_trigger_service_state_t state); |
| 227 | void sendServiceStateEvent(sound_trigger_service_state_t state, Module *module); |
| 228 | void sendServiceStateEvent(sound_trigger_service_state_t state, |
| 229 | ModuleClient *moduleClient); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 230 | |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 231 | void sendCallbackEvent(const sp<CallbackEvent>& event); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 232 | void onCallbackEvent(const sp<CallbackEvent>& event); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 233 | |
| 234 | private: |
| 235 | |
| 236 | virtual void onFirstRef(); |
| 237 | |
| 238 | Mutex mServiceLock; |
| 239 | volatile int32_t mNextUniqueId; |
| 240 | DefaultKeyedVector< sound_trigger_module_handle_t, sp<Module> > mModules; |
| 241 | sp<CallbackThread> mCallbackThread; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 242 | sp<MemoryDealer> mMemoryDealer; |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 243 | Mutex mMemoryDealerLock; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 244 | bool mCaptureState; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | } // namespace android |
| 248 | |
| 249 | #endif // ANDROID_HARDWARE_SOUNDTRIGGER_HAL_SERVICE_H |