Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | #define LOG_TAG "SoundTriggerHwService" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <pthread.h> |
| 24 | |
Eric Laurent | 8ba53d8 | 2014-08-01 23:15:05 +0000 | [diff] [blame] | 25 | #include <system/sound_trigger.h> |
| 26 | #include <cutils/atomic.h> |
| 27 | #include <cutils/properties.h> |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 28 | #include <hardware/hardware.h> |
| 29 | #include <media/AudioSystem.h> |
Andy Hung | ab7ef30 | 2018-05-15 19:35:29 -0700 | [diff] [blame] | 30 | #include <mediautils/ServiceUtilities.h> |
Eric Laurent | 8ba53d8 | 2014-08-01 23:15:05 +0000 | [diff] [blame] | 31 | #include <utils/Errors.h> |
| 32 | #include <utils/Log.h> |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 33 | #include <binder/IServiceManager.h> |
| 34 | #include <binder/MemoryBase.h> |
| 35 | #include <binder/MemoryHeapBase.h> |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 36 | #include <system/sound_trigger.h> |
Eric Laurent | 8ba53d8 | 2014-08-01 23:15:05 +0000 | [diff] [blame] | 37 | #include "SoundTriggerHwService.h" |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 38 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 39 | #ifdef SOUND_TRIGGER_USE_STUB_MODULE |
| 40 | #define HW_MODULE_PREFIX "stub" |
| 41 | #else |
| 42 | #define HW_MODULE_PREFIX "primary" |
| 43 | #endif |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 44 | namespace android { |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 45 | |
| 46 | SoundTriggerHwService::SoundTriggerHwService() |
| 47 | : BnSoundTriggerHwService(), |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 48 | mNextUniqueId(1), |
| 49 | mMemoryDealer(new MemoryDealer(1024 * 1024, "SoundTriggerHwService")), |
| 50 | mCaptureState(false) |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 51 | { |
| 52 | } |
| 53 | |
| 54 | void SoundTriggerHwService::onFirstRef() |
| 55 | { |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 56 | int rc; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 57 | |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 58 | sp<SoundTriggerHalInterface> halInterface = |
| 59 | SoundTriggerHalInterface::connectModule(HW_MODULE_PREFIX); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 60 | |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 61 | if (halInterface == 0) { |
| 62 | ALOGW("could not connect to HAL"); |
| 63 | return; |
| 64 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 65 | sound_trigger_module_descriptor descriptor; |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 66 | rc = halInterface->getProperties(&descriptor.properties); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 67 | if (rc != 0) { |
| 68 | ALOGE("could not read implementation properties"); |
| 69 | return; |
| 70 | } |
| 71 | descriptor.handle = |
| 72 | (sound_trigger_module_handle_t)android_atomic_inc(&mNextUniqueId); |
| 73 | ALOGI("loaded default module %s, handle %d", descriptor.properties.description, |
| 74 | descriptor.handle); |
| 75 | |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 76 | sp<Module> module = new Module(this, halInterface, descriptor); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 77 | mModules.add(descriptor.handle, module); |
| 78 | mCallbackThread = new CallbackThread(this); |
| 79 | } |
| 80 | |
| 81 | SoundTriggerHwService::~SoundTriggerHwService() |
| 82 | { |
| 83 | if (mCallbackThread != 0) { |
| 84 | mCallbackThread->exit(); |
| 85 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | status_t SoundTriggerHwService::listModules(struct sound_trigger_module_descriptor *modules, |
| 89 | uint32_t *numModules) |
| 90 | { |
| 91 | ALOGV("listModules"); |
Eric Laurent | 7504b9e | 2017-08-15 18:17:26 -0700 | [diff] [blame] | 92 | if (!captureHotwordAllowed(IPCThreadState::self()->getCallingPid(), |
| 93 | IPCThreadState::self()->getCallingUid())) { |
Eric Laurent | 8ba53d8 | 2014-08-01 23:15:05 +0000 | [diff] [blame] | 94 | return PERMISSION_DENIED; |
| 95 | } |
| 96 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 97 | AutoMutex lock(mServiceLock); |
| 98 | if (numModules == NULL || (*numModules != 0 && modules == NULL)) { |
| 99 | return BAD_VALUE; |
| 100 | } |
| 101 | size_t maxModules = *numModules; |
| 102 | *numModules = mModules.size(); |
| 103 | for (size_t i = 0; i < mModules.size() && i < maxModules; i++) { |
| 104 | modules[i] = mModules.valueAt(i)->descriptor(); |
| 105 | } |
| 106 | return NO_ERROR; |
| 107 | } |
| 108 | |
| 109 | status_t SoundTriggerHwService::attach(const sound_trigger_module_handle_t handle, |
| 110 | const sp<ISoundTriggerClient>& client, |
| 111 | sp<ISoundTrigger>& moduleInterface) |
| 112 | { |
| 113 | ALOGV("attach module %d", handle); |
Eric Laurent | 7504b9e | 2017-08-15 18:17:26 -0700 | [diff] [blame] | 114 | if (!captureHotwordAllowed(IPCThreadState::self()->getCallingPid(), |
| 115 | IPCThreadState::self()->getCallingUid())) { |
Eric Laurent | 8ba53d8 | 2014-08-01 23:15:05 +0000 | [diff] [blame] | 116 | return PERMISSION_DENIED; |
| 117 | } |
| 118 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 119 | AutoMutex lock(mServiceLock); |
| 120 | moduleInterface.clear(); |
| 121 | if (client == 0) { |
| 122 | return BAD_VALUE; |
| 123 | } |
| 124 | ssize_t index = mModules.indexOfKey(handle); |
| 125 | if (index < 0) { |
| 126 | return BAD_VALUE; |
| 127 | } |
| 128 | sp<Module> module = mModules.valueAt(index); |
| 129 | |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 130 | sp<ModuleClient> moduleClient = module->addClient(client); |
| 131 | if (moduleClient == 0) { |
| 132 | return NO_INIT; |
| 133 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 134 | |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 135 | moduleClient->setCaptureState_l(mCaptureState); |
| 136 | moduleInterface = moduleClient; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 137 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 138 | return NO_ERROR; |
| 139 | } |
| 140 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 141 | status_t SoundTriggerHwService::setCaptureState(bool active) |
| 142 | { |
| 143 | ALOGV("setCaptureState %d", active); |
| 144 | AutoMutex lock(mServiceLock); |
| 145 | mCaptureState = active; |
| 146 | for (size_t i = 0; i < mModules.size(); i++) { |
| 147 | mModules.valueAt(i)->setCaptureState_l(active); |
| 148 | } |
| 149 | return NO_ERROR; |
| 150 | } |
| 151 | |
| 152 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 153 | static const int kDumpLockRetries = 50; |
| 154 | static const int kDumpLockSleep = 60000; |
| 155 | |
| 156 | static bool tryLock(Mutex& mutex) |
| 157 | { |
| 158 | bool locked = false; |
| 159 | for (int i = 0; i < kDumpLockRetries; ++i) { |
| 160 | if (mutex.tryLock() == NO_ERROR) { |
| 161 | locked = true; |
| 162 | break; |
| 163 | } |
| 164 | usleep(kDumpLockSleep); |
| 165 | } |
| 166 | return locked; |
| 167 | } |
| 168 | |
| 169 | status_t SoundTriggerHwService::dump(int fd, const Vector<String16>& args __unused) { |
| 170 | String8 result; |
| 171 | if (checkCallingPermission(String16("android.permission.DUMP")) == false) { |
| 172 | result.appendFormat("Permission Denial: can't dump SoundTriggerHwService"); |
| 173 | write(fd, result.string(), result.size()); |
| 174 | } else { |
| 175 | bool locked = tryLock(mServiceLock); |
| 176 | // failed to lock - SoundTriggerHwService is probably deadlocked |
| 177 | if (!locked) { |
| 178 | result.append("SoundTriggerHwService may be deadlocked\n"); |
| 179 | write(fd, result.string(), result.size()); |
| 180 | } |
| 181 | |
| 182 | if (locked) mServiceLock.unlock(); |
| 183 | } |
| 184 | return NO_ERROR; |
| 185 | } |
| 186 | |
| 187 | status_t SoundTriggerHwService::onTransact( |
| 188 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) { |
| 189 | return BnSoundTriggerHwService::onTransact(code, data, reply, flags); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | // static |
| 194 | void SoundTriggerHwService::recognitionCallback(struct sound_trigger_recognition_event *event, |
| 195 | void *cookie) |
| 196 | { |
| 197 | Module *module = (Module *)cookie; |
| 198 | if (module == NULL) { |
| 199 | return; |
| 200 | } |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 201 | sp<SoundTriggerHwService> service = module->service().promote(); |
| 202 | if (service == 0) { |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | service->sendRecognitionEvent(event, module); |
| 207 | } |
| 208 | |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 209 | sp<IMemory> SoundTriggerHwService::prepareRecognitionEvent( |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 210 | struct sound_trigger_recognition_event *event) |
| 211 | { |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 212 | AutoMutex lock(mMemoryDealerLock); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 213 | sp<IMemory> eventMemory; |
| 214 | |
| 215 | //sanitize event |
| 216 | switch (event->type) { |
| 217 | case SOUND_MODEL_TYPE_KEYPHRASE: |
| 218 | ALOGW_IF(event->data_size != 0 && event->data_offset != |
| 219 | sizeof(struct sound_trigger_phrase_recognition_event), |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 220 | "prepareRecognitionEvent(): invalid data offset %u for keyphrase event type", |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 221 | event->data_offset); |
| 222 | event->data_offset = sizeof(struct sound_trigger_phrase_recognition_event); |
| 223 | break; |
Ryan Bavetta | 00a727c | 2016-01-26 21:56:19 -0800 | [diff] [blame] | 224 | case SOUND_MODEL_TYPE_GENERIC: |
| 225 | ALOGW_IF(event->data_size != 0 && event->data_offset != |
| 226 | sizeof(struct sound_trigger_generic_recognition_event), |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 227 | "prepareRecognitionEvent(): invalid data offset %u for generic event type", |
Ryan Bavetta | 00a727c | 2016-01-26 21:56:19 -0800 | [diff] [blame] | 228 | event->data_offset); |
| 229 | event->data_offset = sizeof(struct sound_trigger_generic_recognition_event); |
| 230 | break; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 231 | case SOUND_MODEL_TYPE_UNKNOWN: |
| 232 | ALOGW_IF(event->data_size != 0 && event->data_offset != |
| 233 | sizeof(struct sound_trigger_recognition_event), |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 234 | "prepareRecognitionEvent(): invalid data offset %u for unknown event type", |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 235 | event->data_offset); |
| 236 | event->data_offset = sizeof(struct sound_trigger_recognition_event); |
| 237 | break; |
| 238 | default: |
Eric Laurent | 886561f | 2014-08-28 19:45:37 -0700 | [diff] [blame] | 239 | return eventMemory; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | size_t size = event->data_offset + event->data_size; |
| 243 | eventMemory = mMemoryDealer->allocate(size); |
| 244 | if (eventMemory == 0 || eventMemory->pointer() == NULL) { |
| 245 | eventMemory.clear(); |
| 246 | return eventMemory; |
| 247 | } |
| 248 | memcpy(eventMemory->pointer(), event, size); |
| 249 | |
| 250 | return eventMemory; |
| 251 | } |
| 252 | |
| 253 | void SoundTriggerHwService::sendRecognitionEvent(struct sound_trigger_recognition_event *event, |
| 254 | Module *module) |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 255 | { |
| 256 | if (module == NULL) { |
| 257 | return; |
| 258 | } |
| 259 | sp<IMemory> eventMemory = prepareRecognitionEvent(event); |
| 260 | if (eventMemory == 0) { |
| 261 | return; |
| 262 | } |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 263 | |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 264 | sp<CallbackEvent> callbackEvent = new CallbackEvent(CallbackEvent::TYPE_RECOGNITION, |
| 265 | eventMemory); |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 266 | callbackEvent->setModule(module); |
| 267 | sendCallbackEvent(callbackEvent); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | // static |
| 271 | void SoundTriggerHwService::soundModelCallback(struct sound_trigger_model_event *event, |
| 272 | void *cookie) |
| 273 | { |
| 274 | Module *module = (Module *)cookie; |
| 275 | if (module == NULL) { |
| 276 | return; |
| 277 | } |
| 278 | sp<SoundTriggerHwService> service = module->service().promote(); |
| 279 | if (service == 0) { |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | service->sendSoundModelEvent(event, module); |
| 284 | } |
| 285 | |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 286 | sp<IMemory> SoundTriggerHwService::prepareSoundModelEvent(struct sound_trigger_model_event *event) |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 287 | { |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 288 | AutoMutex lock(mMemoryDealerLock); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 289 | sp<IMemory> eventMemory; |
| 290 | |
| 291 | size_t size = event->data_offset + event->data_size; |
| 292 | eventMemory = mMemoryDealer->allocate(size); |
| 293 | if (eventMemory == 0 || eventMemory->pointer() == NULL) { |
| 294 | eventMemory.clear(); |
| 295 | return eventMemory; |
| 296 | } |
| 297 | memcpy(eventMemory->pointer(), event, size); |
| 298 | |
| 299 | return eventMemory; |
| 300 | } |
| 301 | |
| 302 | void SoundTriggerHwService::sendSoundModelEvent(struct sound_trigger_model_event *event, |
| 303 | Module *module) |
| 304 | { |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 305 | sp<IMemory> eventMemory = prepareSoundModelEvent(event); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 306 | if (eventMemory == 0) { |
| 307 | return; |
| 308 | } |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 309 | sp<CallbackEvent> callbackEvent = new CallbackEvent(CallbackEvent::TYPE_SOUNDMODEL, |
| 310 | eventMemory); |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 311 | callbackEvent->setModule(module); |
| 312 | sendCallbackEvent(callbackEvent); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | |
Chris Thornton | 07405ee | 2017-11-14 20:45:27 -0800 | [diff] [blame] | 316 | sp<IMemory> SoundTriggerHwService::prepareServiceStateEvent(sound_trigger_service_state_t state) |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 317 | { |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 318 | AutoMutex lock(mMemoryDealerLock); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 319 | sp<IMemory> eventMemory; |
| 320 | |
| 321 | size_t size = sizeof(sound_trigger_service_state_t); |
| 322 | eventMemory = mMemoryDealer->allocate(size); |
| 323 | if (eventMemory == 0 || eventMemory->pointer() == NULL) { |
| 324 | eventMemory.clear(); |
| 325 | return eventMemory; |
| 326 | } |
| 327 | *((sound_trigger_service_state_t *)eventMemory->pointer()) = state; |
| 328 | return eventMemory; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Chris Thornton | 07405ee | 2017-11-14 20:45:27 -0800 | [diff] [blame] | 331 | void SoundTriggerHwService::sendServiceStateEvent(sound_trigger_service_state_t state, |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 332 | Module *module) |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 333 | { |
Chris Thornton | 07405ee | 2017-11-14 20:45:27 -0800 | [diff] [blame] | 334 | sp<IMemory> eventMemory = prepareServiceStateEvent(state); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 335 | if (eventMemory == 0) { |
| 336 | return; |
| 337 | } |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 338 | sp<CallbackEvent> callbackEvent = new CallbackEvent(CallbackEvent::TYPE_SERVICE_STATE, |
| 339 | eventMemory); |
Chris Thornton | 07405ee | 2017-11-14 20:45:27 -0800 | [diff] [blame] | 340 | callbackEvent->setModule(module); |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 341 | sendCallbackEvent(callbackEvent); |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Chris Thornton | 07405ee | 2017-11-14 20:45:27 -0800 | [diff] [blame] | 344 | void SoundTriggerHwService::sendServiceStateEvent(sound_trigger_service_state_t state, |
| 345 | ModuleClient *moduleClient) |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 346 | { |
Chris Thornton | 07405ee | 2017-11-14 20:45:27 -0800 | [diff] [blame] | 347 | sp<IMemory> eventMemory = prepareServiceStateEvent(state); |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 348 | if (eventMemory == 0) { |
| 349 | return; |
| 350 | } |
| 351 | sp<CallbackEvent> callbackEvent = new CallbackEvent(CallbackEvent::TYPE_SERVICE_STATE, |
| 352 | eventMemory); |
| 353 | callbackEvent->setModuleClient(moduleClient); |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 354 | sendCallbackEvent(callbackEvent); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 357 | void SoundTriggerHwService::sendCallbackEvent(const sp<CallbackEvent>& event) |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 358 | { |
| 359 | mCallbackThread->sendCallbackEvent(event); |
| 360 | } |
| 361 | |
| 362 | void SoundTriggerHwService::onCallbackEvent(const sp<CallbackEvent>& event) |
| 363 | { |
| 364 | ALOGV("onCallbackEvent"); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 365 | sp<Module> module; |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 366 | sp<ModuleClient> moduleClient; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 367 | { |
| 368 | AutoMutex lock(mServiceLock); |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 369 | //CallbackEvent is either for Module or ModuleClient |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 370 | module = event->mModule.promote(); |
| 371 | if (module == 0) { |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 372 | moduleClient = event->mModuleClient.promote(); |
| 373 | if (moduleClient == 0) { |
| 374 | return; |
| 375 | } |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 376 | } else { |
| 377 | // Sanity check on this being a Module we know about. |
| 378 | bool foundModule = false; |
| 379 | for (size_t i = 0; i < mModules.size(); i++) { |
| 380 | if (mModules.valueAt(i).get() == module.get()) { |
| 381 | foundModule = true; |
| 382 | break; |
| 383 | } |
| 384 | } |
| 385 | if (!foundModule) { |
| 386 | ALOGE("onCallbackEvent for unknown module"); |
| 387 | return; |
| 388 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 389 | } |
| 390 | } |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 391 | if (module != 0) { |
| 392 | ALOGV("onCallbackEvent for module"); |
| 393 | module->onCallbackEvent(event); |
| 394 | } else if (moduleClient != 0) { |
| 395 | ALOGV("onCallbackEvent for moduleClient"); |
| 396 | moduleClient->onCallbackEvent(event); |
| 397 | } |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 398 | { |
| 399 | AutoMutex lock(mServiceLock); |
| 400 | // clear now to execute with mServiceLock locked |
| 401 | event->mMemory.clear(); |
| 402 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | #undef LOG_TAG |
| 406 | #define LOG_TAG "SoundTriggerHwService::CallbackThread" |
| 407 | |
| 408 | SoundTriggerHwService::CallbackThread::CallbackThread(const wp<SoundTriggerHwService>& service) |
| 409 | : mService(service) |
| 410 | { |
| 411 | } |
| 412 | |
| 413 | SoundTriggerHwService::CallbackThread::~CallbackThread() |
| 414 | { |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 415 | while (!mEventQueue.isEmpty()) { |
| 416 | mEventQueue[0]->mMemory.clear(); |
| 417 | mEventQueue.removeAt(0); |
| 418 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | void SoundTriggerHwService::CallbackThread::onFirstRef() |
| 422 | { |
| 423 | run("soundTrigger cbk", ANDROID_PRIORITY_URGENT_AUDIO); |
| 424 | } |
| 425 | |
| 426 | bool SoundTriggerHwService::CallbackThread::threadLoop() |
| 427 | { |
| 428 | while (!exitPending()) { |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 429 | sp<CallbackEvent> event; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 430 | sp<SoundTriggerHwService> service; |
| 431 | { |
| 432 | Mutex::Autolock _l(mCallbackLock); |
| 433 | while (mEventQueue.isEmpty() && !exitPending()) { |
| 434 | ALOGV("CallbackThread::threadLoop() sleep"); |
| 435 | mCallbackCond.wait(mCallbackLock); |
| 436 | ALOGV("CallbackThread::threadLoop() wake up"); |
| 437 | } |
| 438 | if (exitPending()) { |
| 439 | break; |
| 440 | } |
| 441 | event = mEventQueue[0]; |
| 442 | mEventQueue.removeAt(0); |
| 443 | service = mService.promote(); |
| 444 | } |
| 445 | if (service != 0) { |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 446 | service->onCallbackEvent(event); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | return false; |
| 450 | } |
| 451 | |
| 452 | void SoundTriggerHwService::CallbackThread::exit() |
| 453 | { |
| 454 | Mutex::Autolock _l(mCallbackLock); |
| 455 | requestExit(); |
| 456 | mCallbackCond.broadcast(); |
| 457 | } |
| 458 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 459 | void SoundTriggerHwService::CallbackThread::sendCallbackEvent( |
| 460 | const sp<SoundTriggerHwService::CallbackEvent>& event) |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 461 | { |
| 462 | AutoMutex lock(mCallbackLock); |
| 463 | mEventQueue.add(event); |
| 464 | mCallbackCond.signal(); |
| 465 | } |
| 466 | |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 467 | SoundTriggerHwService::CallbackEvent::CallbackEvent(event_type type, sp<IMemory> memory) |
| 468 | : mType(type), mMemory(memory) |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 469 | { |
| 470 | } |
| 471 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 472 | SoundTriggerHwService::CallbackEvent::~CallbackEvent() |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 473 | { |
| 474 | } |
| 475 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 476 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 477 | #undef LOG_TAG |
| 478 | #define LOG_TAG "SoundTriggerHwService::Module" |
| 479 | |
| 480 | SoundTriggerHwService::Module::Module(const sp<SoundTriggerHwService>& service, |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 481 | const sp<SoundTriggerHalInterface>& halInterface, |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 482 | sound_trigger_module_descriptor descriptor) |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 483 | : mService(service), mHalInterface(halInterface), mDescriptor(descriptor), |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 484 | mServiceState(SOUND_TRIGGER_STATE_NO_INIT) |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 485 | { |
| 486 | } |
| 487 | |
| 488 | SoundTriggerHwService::Module::~Module() { |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 489 | mModuleClients.clear(); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 490 | } |
| 491 | |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 492 | sp<SoundTriggerHwService::ModuleClient> |
| 493 | SoundTriggerHwService::Module::addClient(const sp<ISoundTriggerClient>& client) |
| 494 | { |
| 495 | AutoMutex lock(mLock); |
| 496 | sp<ModuleClient> moduleClient; |
| 497 | |
| 498 | for (size_t i = 0; i < mModuleClients.size(); i++) { |
| 499 | if (mModuleClients[i]->client() == client) { |
| 500 | // Client already present, reuse client |
| 501 | return moduleClient; |
| 502 | } |
| 503 | } |
| 504 | moduleClient = new ModuleClient(this, client); |
| 505 | |
| 506 | ALOGV("addClient() client %p", moduleClient.get()); |
| 507 | mModuleClients.add(moduleClient); |
| 508 | |
| 509 | return moduleClient; |
| 510 | } |
| 511 | |
| 512 | void SoundTriggerHwService::Module::detach(const sp<ModuleClient>& moduleClient) |
| 513 | { |
| 514 | ALOGV("Module::detach()"); |
Eric Laurent | 338e8ba | 2017-10-05 10:58:38 -0700 | [diff] [blame] | 515 | Vector<audio_session_t> releasedSessions; |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 516 | |
Eric Laurent | 338e8ba | 2017-10-05 10:58:38 -0700 | [diff] [blame] | 517 | { |
| 518 | AutoMutex lock(mLock); |
| 519 | ssize_t index = -1; |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 520 | |
Eric Laurent | 338e8ba | 2017-10-05 10:58:38 -0700 | [diff] [blame] | 521 | for (size_t i = 0; i < mModuleClients.size(); i++) { |
| 522 | if (mModuleClients[i] == moduleClient) { |
| 523 | index = i; |
| 524 | break; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 525 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 526 | } |
Eric Laurent | 338e8ba | 2017-10-05 10:58:38 -0700 | [diff] [blame] | 527 | if (index == -1) { |
| 528 | return; |
| 529 | } |
| 530 | |
| 531 | ALOGV("remove client %p", moduleClient.get()); |
| 532 | mModuleClients.removeAt(index); |
| 533 | |
| 534 | // Iterate in reverse order as models are removed from list inside the loop. |
| 535 | for (size_t i = mModels.size(); i > 0; i--) { |
| 536 | sp<Model> model = mModels.valueAt(i - 1); |
| 537 | if (moduleClient == model->mModuleClient) { |
| 538 | mModels.removeItemsAt(i - 1); |
| 539 | ALOGV("detach() unloading model %d", model->mHandle); |
| 540 | if (mHalInterface != 0) { |
| 541 | if (model->mState == Model::STATE_ACTIVE) { |
| 542 | mHalInterface->stopRecognition(model->mHandle); |
| 543 | } |
| 544 | mHalInterface->unloadSoundModel(model->mHandle); |
| 545 | } |
| 546 | releasedSessions.add(model->mCaptureSession); |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | for (size_t i = 0; i < releasedSessions.size(); i++) { |
| 552 | // do not call AudioSystem methods with mLock held |
| 553 | AudioSystem::releaseSoundTriggerSession(releasedSessions[i]); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 554 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | status_t SoundTriggerHwService::Module::loadSoundModel(const sp<IMemory>& modelMemory, |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 558 | sp<ModuleClient> moduleClient, |
| 559 | sound_model_handle_t *handle) |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 560 | { |
| 561 | ALOGV("loadSoundModel() handle"); |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 562 | if (mHalInterface == 0) { |
| 563 | return NO_INIT; |
| 564 | } |
Eric Laurent | 9b11c02 | 2018-06-06 19:19:22 -0700 | [diff] [blame] | 565 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 566 | struct sound_trigger_sound_model *sound_model = |
| 567 | (struct sound_trigger_sound_model *)modelMemory->pointer(); |
| 568 | |
Eric Laurent | bb00d8f | 2016-08-17 06:19:32 -0700 | [diff] [blame] | 569 | size_t structSize; |
| 570 | if (sound_model->type == SOUND_MODEL_TYPE_KEYPHRASE) { |
| 571 | structSize = sizeof(struct sound_trigger_phrase_sound_model); |
| 572 | } else { |
| 573 | structSize = sizeof(struct sound_trigger_sound_model); |
| 574 | } |
| 575 | |
| 576 | if (sound_model->data_offset < structSize || |
| 577 | sound_model->data_size > (UINT_MAX - sound_model->data_offset) || |
| 578 | modelMemory->size() < sound_model->data_offset || |
| 579 | sound_model->data_size > (modelMemory->size() - sound_model->data_offset)) { |
| 580 | android_errorWriteLog(0x534e4554, "30148546"); |
| 581 | ALOGE("loadSoundModel() data_size is too big"); |
| 582 | return BAD_VALUE; |
| 583 | } |
| 584 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 585 | audio_session_t session; |
| 586 | audio_io_handle_t ioHandle; |
| 587 | audio_devices_t device; |
Eric Laurent | 338e8ba | 2017-10-05 10:58:38 -0700 | [diff] [blame] | 588 | // do not call AudioSystem methods with mLock held |
| 589 | status_t status = AudioSystem::acquireSoundTriggerSession(&session, &ioHandle, &device); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 590 | if (status != NO_ERROR) { |
| 591 | return status; |
| 592 | } |
| 593 | |
Eric Laurent | 338e8ba | 2017-10-05 10:58:38 -0700 | [diff] [blame] | 594 | { |
| 595 | AutoMutex lock(mLock); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 596 | |
Eric Laurent | 338e8ba | 2017-10-05 10:58:38 -0700 | [diff] [blame] | 597 | if (mModels.size() >= mDescriptor.properties.max_sound_models) { |
| 598 | ALOGW("loadSoundModel(): Not loading, max number of models (%d) would be exceeded", |
| 599 | mDescriptor.properties.max_sound_models); |
| 600 | status = INVALID_OPERATION; |
| 601 | goto exit; |
| 602 | } |
| 603 | |
| 604 | status = mHalInterface->loadSoundModel(sound_model, |
| 605 | SoundTriggerHwService::soundModelCallback, |
| 606 | this, handle); |
| 607 | if (status != NO_ERROR) { |
| 608 | goto exit; |
| 609 | } |
| 610 | |
| 611 | sp<Model> model = new Model(*handle, session, ioHandle, device, sound_model->type, |
| 612 | moduleClient); |
| 613 | mModels.replaceValueFor(*handle, model); |
| 614 | } |
| 615 | exit: |
| 616 | if (status != NO_ERROR) { |
| 617 | // do not call AudioSystem methods with mLock held |
| 618 | AudioSystem::releaseSoundTriggerSession(session); |
| 619 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 620 | return status; |
| 621 | } |
| 622 | |
| 623 | status_t SoundTriggerHwService::Module::unloadSoundModel(sound_model_handle_t handle) |
| 624 | { |
| 625 | ALOGV("unloadSoundModel() model handle %d", handle); |
Eric Laurent | 338e8ba | 2017-10-05 10:58:38 -0700 | [diff] [blame] | 626 | status_t status; |
| 627 | audio_session_t session; |
Eric Laurent | 02eb47c | 2014-11-20 10:10:20 -0800 | [diff] [blame] | 628 | |
Eric Laurent | 338e8ba | 2017-10-05 10:58:38 -0700 | [diff] [blame] | 629 | { |
| 630 | AutoMutex lock(mLock); |
| 631 | if (mHalInterface == 0) { |
| 632 | return NO_INIT; |
| 633 | } |
| 634 | ssize_t index = mModels.indexOfKey(handle); |
| 635 | if (index < 0) { |
| 636 | return BAD_VALUE; |
| 637 | } |
| 638 | sp<Model> model = mModels.valueAt(index); |
| 639 | mModels.removeItem(handle); |
| 640 | if (model->mState == Model::STATE_ACTIVE) { |
| 641 | mHalInterface->stopRecognition(model->mHandle); |
| 642 | model->mState = Model::STATE_IDLE; |
| 643 | } |
| 644 | status = mHalInterface->unloadSoundModel(handle); |
| 645 | session = model->mCaptureSession; |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 646 | } |
Eric Laurent | 338e8ba | 2017-10-05 10:58:38 -0700 | [diff] [blame] | 647 | // do not call AudioSystem methods with mLock held |
| 648 | AudioSystem::releaseSoundTriggerSession(session); |
| 649 | return status; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | status_t SoundTriggerHwService::Module::startRecognition(sound_model_handle_t handle, |
Eric Laurent | 0832b2d | 2014-07-06 16:17:25 -0700 | [diff] [blame] | 653 | const sp<IMemory>& dataMemory) |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 654 | { |
| 655 | ALOGV("startRecognition() model handle %d", handle); |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 656 | if (mHalInterface == 0) { |
| 657 | return NO_INIT; |
| 658 | } |
Eric Laurent | bb00d8f | 2016-08-17 06:19:32 -0700 | [diff] [blame] | 659 | |
| 660 | struct sound_trigger_recognition_config *config = |
| 661 | (struct sound_trigger_recognition_config *)dataMemory->pointer(); |
| 662 | |
| 663 | if (config->data_offset < sizeof(struct sound_trigger_recognition_config) || |
| 664 | config->data_size > (UINT_MAX - config->data_offset) || |
| 665 | dataMemory->size() < config->data_offset || |
| 666 | config->data_size > (dataMemory->size() - config->data_offset)) { |
| 667 | ALOGE("startRecognition() data_size is too big"); |
| 668 | return BAD_VALUE; |
| 669 | } |
| 670 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 671 | AutoMutex lock(mLock); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 672 | if (mServiceState == SOUND_TRIGGER_STATE_DISABLED) { |
| 673 | return INVALID_OPERATION; |
| 674 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 675 | sp<Model> model = getModel(handle); |
| 676 | if (model == 0) { |
| 677 | return BAD_VALUE; |
| 678 | } |
| 679 | |
| 680 | if (model->mState == Model::STATE_ACTIVE) { |
| 681 | return INVALID_OPERATION; |
| 682 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 683 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 684 | |
| 685 | //TODO: get capture handle and device from audio policy service |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 686 | config->capture_handle = model->mCaptureIOHandle; |
| 687 | config->capture_device = model->mCaptureDevice; |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 688 | status_t status = mHalInterface->startRecognition(handle, config, |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 689 | SoundTriggerHwService::recognitionCallback, |
Eric Laurent | 0832b2d | 2014-07-06 16:17:25 -0700 | [diff] [blame] | 690 | this); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 691 | |
| 692 | if (status == NO_ERROR) { |
| 693 | model->mState = Model::STATE_ACTIVE; |
| 694 | model->mConfig = *config; |
| 695 | } |
| 696 | |
| 697 | return status; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | status_t SoundTriggerHwService::Module::stopRecognition(sound_model_handle_t handle) |
| 701 | { |
| 702 | ALOGV("stopRecognition() model handle %d", handle); |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 703 | if (mHalInterface == 0) { |
| 704 | return NO_INIT; |
| 705 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 706 | AutoMutex lock(mLock); |
| 707 | sp<Model> model = getModel(handle); |
| 708 | if (model == 0) { |
| 709 | return BAD_VALUE; |
| 710 | } |
| 711 | |
| 712 | if (model->mState != Model::STATE_ACTIVE) { |
| 713 | return INVALID_OPERATION; |
| 714 | } |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 715 | mHalInterface->stopRecognition(handle); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 716 | model->mState = Model::STATE_IDLE; |
| 717 | return NO_ERROR; |
| 718 | } |
| 719 | |
mike dooley | 6e189b1 | 2018-11-07 15:44:37 +0100 | [diff] [blame] | 720 | status_t SoundTriggerHwService::Module::getModelState(sound_model_handle_t handle) |
Michael Dooley | 67e3d41 | 2018-10-16 19:51:16 +0000 | [diff] [blame] | 721 | { |
| 722 | ALOGV("getModelState() model handle %d", handle); |
| 723 | if (mHalInterface == 0) { |
| 724 | return NO_INIT; |
| 725 | } |
| 726 | AutoMutex lock(mLock); |
| 727 | sp<Model> model = getModel(handle); |
| 728 | if (model == 0) { |
| 729 | return BAD_VALUE; |
| 730 | } |
| 731 | |
| 732 | if (model->mState != Model::STATE_ACTIVE) { |
| 733 | return INVALID_OPERATION; |
| 734 | } |
| 735 | |
mike dooley | 6e189b1 | 2018-11-07 15:44:37 +0100 | [diff] [blame] | 736 | return mHalInterface->getModelState(handle); |
Michael Dooley | 67e3d41 | 2018-10-16 19:51:16 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 739 | void SoundTriggerHwService::Module::onCallbackEvent(const sp<CallbackEvent>& event) |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 740 | { |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 741 | ALOGV("onCallbackEvent type %d", event->mType); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 742 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 743 | sp<IMemory> eventMemory = event->mMemory; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 744 | |
| 745 | if (eventMemory == 0 || eventMemory->pointer() == NULL) { |
| 746 | return; |
| 747 | } |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 748 | if (mModuleClients.isEmpty()) { |
| 749 | ALOGI("%s no clients", __func__); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 750 | return; |
| 751 | } |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 752 | |
Chris Thornton | 02b7421 | 2017-11-06 14:45:30 -0800 | [diff] [blame] | 753 | Vector< sp<ModuleClient> > clients; |
| 754 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 755 | switch (event->mType) { |
| 756 | case CallbackEvent::TYPE_RECOGNITION: { |
| 757 | struct sound_trigger_recognition_event *recognitionEvent = |
| 758 | (struct sound_trigger_recognition_event *)eventMemory->pointer(); |
Eric Laurent | 886561f | 2014-08-28 19:45:37 -0700 | [diff] [blame] | 759 | { |
| 760 | AutoMutex lock(mLock); |
| 761 | sp<Model> model = getModel(recognitionEvent->model); |
| 762 | if (model == 0) { |
| 763 | ALOGW("%s model == 0", __func__); |
| 764 | return; |
| 765 | } |
| 766 | if (model->mState != Model::STATE_ACTIVE) { |
| 767 | ALOGV("onCallbackEvent model->mState %d != Model::STATE_ACTIVE", model->mState); |
| 768 | return; |
| 769 | } |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 770 | |
Eric Laurent | 886561f | 2014-08-28 19:45:37 -0700 | [diff] [blame] | 771 | recognitionEvent->capture_session = model->mCaptureSession; |
mike dooley | 6e189b1 | 2018-11-07 15:44:37 +0100 | [diff] [blame] | 772 | // Don't reset the model state if this recognition event is a get-state response |
| 773 | if (recognitionEvent->status != RECOGNITION_STATUS_GET_STATE_RESPONSE) { |
| 774 | model->mState = Model::STATE_IDLE; |
| 775 | } |
Chris Thornton | 02b7421 | 2017-11-06 14:45:30 -0800 | [diff] [blame] | 776 | clients.add(model->mModuleClient); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 777 | } |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 778 | } break; |
| 779 | case CallbackEvent::TYPE_SOUNDMODEL: { |
| 780 | struct sound_trigger_model_event *soundmodelEvent = |
| 781 | (struct sound_trigger_model_event *)eventMemory->pointer(); |
Eric Laurent | 886561f | 2014-08-28 19:45:37 -0700 | [diff] [blame] | 782 | { |
| 783 | AutoMutex lock(mLock); |
| 784 | sp<Model> model = getModel(soundmodelEvent->model); |
| 785 | if (model == 0) { |
| 786 | ALOGW("%s model == 0", __func__); |
| 787 | return; |
| 788 | } |
Chris Thornton | 02b7421 | 2017-11-06 14:45:30 -0800 | [diff] [blame] | 789 | clients.add(model->mModuleClient); |
Eric Laurent | 886561f | 2014-08-28 19:45:37 -0700 | [diff] [blame] | 790 | } |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 791 | } break; |
| 792 | case CallbackEvent::TYPE_SERVICE_STATE: { |
Eric Laurent | 886561f | 2014-08-28 19:45:37 -0700 | [diff] [blame] | 793 | { |
| 794 | AutoMutex lock(mLock); |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 795 | for (size_t i = 0; i < mModuleClients.size(); i++) { |
| 796 | if (mModuleClients[i] != 0) { |
Chris Thornton | 02b7421 | 2017-11-06 14:45:30 -0800 | [diff] [blame] | 797 | clients.add(mModuleClients[i]); |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 798 | } |
| 799 | } |
Eric Laurent | 886561f | 2014-08-28 19:45:37 -0700 | [diff] [blame] | 800 | } |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 801 | } break; |
| 802 | default: |
| 803 | LOG_ALWAYS_FATAL("onCallbackEvent unknown event type %d", event->mType); |
| 804 | } |
Chris Thornton | 02b7421 | 2017-11-06 14:45:30 -0800 | [diff] [blame] | 805 | |
| 806 | for (size_t i = 0; i < clients.size(); i++) { |
| 807 | clients[i]->onCallbackEvent(event); |
| 808 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | sp<SoundTriggerHwService::Model> SoundTriggerHwService::Module::getModel( |
| 812 | sound_model_handle_t handle) |
| 813 | { |
| 814 | sp<Model> model; |
| 815 | ssize_t index = mModels.indexOfKey(handle); |
| 816 | if (index >= 0) { |
| 817 | model = mModels.valueAt(index); |
| 818 | } |
| 819 | return model; |
| 820 | } |
| 821 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 822 | // Called with mServiceLock held |
| 823 | void SoundTriggerHwService::Module::setCaptureState_l(bool active) |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 824 | { |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 825 | ALOGV("Module::setCaptureState_l %d", active); |
| 826 | sp<SoundTriggerHwService> service; |
| 827 | sound_trigger_service_state_t state; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 828 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 829 | Vector< sp<IMemory> > events; |
| 830 | { |
| 831 | AutoMutex lock(mLock); |
| 832 | state = (active && !mDescriptor.properties.concurrent_capture) ? |
| 833 | SOUND_TRIGGER_STATE_DISABLED : SOUND_TRIGGER_STATE_ENABLED; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 834 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 835 | if (state == mServiceState) { |
| 836 | return; |
| 837 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 838 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 839 | mServiceState = state; |
| 840 | |
| 841 | service = mService.promote(); |
| 842 | if (service == 0) { |
| 843 | return; |
| 844 | } |
| 845 | |
| 846 | if (state == SOUND_TRIGGER_STATE_ENABLED) { |
| 847 | goto exit; |
| 848 | } |
| 849 | |
Chris Thornton | efcf16c | 2016-03-27 17:13:28 -0700 | [diff] [blame] | 850 | const bool supports_stop_all = |
Chris Thornton | de22f8a | 2017-08-29 16:46:37 -0700 | [diff] [blame] | 851 | (mHalInterface != 0) && (mHalInterface->stopAllRecognitions() != -ENOSYS); |
Chris Thornton | efcf16c | 2016-03-27 17:13:28 -0700 | [diff] [blame] | 852 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 853 | for (size_t i = 0; i < mModels.size(); i++) { |
| 854 | sp<Model> model = mModels.valueAt(i); |
| 855 | if (model->mState == Model::STATE_ACTIVE) { |
Eric Laurent | 7a544b4 | 2016-08-05 19:01:13 -0700 | [diff] [blame] | 856 | if (mHalInterface != 0 && !supports_stop_all) { |
| 857 | mHalInterface->stopRecognition(model->mHandle); |
Chris Thornton | efcf16c | 2016-03-27 17:13:28 -0700 | [diff] [blame] | 858 | } |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 859 | // keep model in ACTIVE state so that event is processed by onCallbackEvent() |
Ryan Bavetta | 00a727c | 2016-01-26 21:56:19 -0800 | [diff] [blame] | 860 | if (model->mType == SOUND_MODEL_TYPE_KEYPHRASE) { |
| 861 | struct sound_trigger_phrase_recognition_event event; |
| 862 | memset(&event, 0, sizeof(struct sound_trigger_phrase_recognition_event)); |
| 863 | event.num_phrases = model->mConfig.num_phrases; |
| 864 | for (size_t i = 0; i < event.num_phrases; i++) { |
| 865 | event.phrase_extras[i] = model->mConfig.phrases[i]; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 866 | } |
Ryan Bavetta | 00a727c | 2016-01-26 21:56:19 -0800 | [diff] [blame] | 867 | event.common.status = RECOGNITION_STATUS_ABORT; |
| 868 | event.common.type = model->mType; |
| 869 | event.common.model = model->mHandle; |
| 870 | event.common.data_size = 0; |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 871 | sp<IMemory> eventMemory = service->prepareRecognitionEvent(&event.common); |
Ryan Bavetta | 00a727c | 2016-01-26 21:56:19 -0800 | [diff] [blame] | 872 | if (eventMemory != 0) { |
| 873 | events.add(eventMemory); |
| 874 | } |
| 875 | } else if (model->mType == SOUND_MODEL_TYPE_GENERIC) { |
| 876 | struct sound_trigger_generic_recognition_event event; |
| 877 | memset(&event, 0, sizeof(struct sound_trigger_generic_recognition_event)); |
| 878 | event.common.status = RECOGNITION_STATUS_ABORT; |
| 879 | event.common.type = model->mType; |
| 880 | event.common.model = model->mHandle; |
| 881 | event.common.data_size = 0; |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 882 | sp<IMemory> eventMemory = service->prepareRecognitionEvent(&event.common); |
Ryan Bavetta | 00a727c | 2016-01-26 21:56:19 -0800 | [diff] [blame] | 883 | if (eventMemory != 0) { |
| 884 | events.add(eventMemory); |
| 885 | } |
Ryan Bavetta | 9609a91 | 2016-01-28 19:22:29 -0800 | [diff] [blame] | 886 | } else if (model->mType == SOUND_MODEL_TYPE_UNKNOWN) { |
| 887 | struct sound_trigger_phrase_recognition_event event; |
| 888 | memset(&event, 0, sizeof(struct sound_trigger_phrase_recognition_event)); |
| 889 | event.common.status = RECOGNITION_STATUS_ABORT; |
| 890 | event.common.type = model->mType; |
| 891 | event.common.model = model->mHandle; |
| 892 | event.common.data_size = 0; |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 893 | sp<IMemory> eventMemory = service->prepareRecognitionEvent(&event.common); |
Ryan Bavetta | 9609a91 | 2016-01-28 19:22:29 -0800 | [diff] [blame] | 894 | if (eventMemory != 0) { |
| 895 | events.add(eventMemory); |
| 896 | } |
Ryan Bavetta | 00a727c | 2016-01-26 21:56:19 -0800 | [diff] [blame] | 897 | } else { |
| 898 | goto exit; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 902 | } |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 903 | |
| 904 | for (size_t i = 0; i < events.size(); i++) { |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 905 | sp<CallbackEvent> callbackEvent = new CallbackEvent(CallbackEvent::TYPE_RECOGNITION, |
| 906 | events[i]); |
| 907 | callbackEvent->setModule(this); |
Chris Thornton | 79c5661 | 2017-10-25 14:47:44 -0700 | [diff] [blame] | 908 | service->sendCallbackEvent(callbackEvent); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | exit: |
Chris Thornton | 07405ee | 2017-11-14 20:45:27 -0800 | [diff] [blame] | 912 | service->sendServiceStateEvent(state, this); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 913 | } |
| 914 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 915 | |
| 916 | SoundTriggerHwService::Model::Model(sound_model_handle_t handle, audio_session_t session, |
| 917 | audio_io_handle_t ioHandle, audio_devices_t device, |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 918 | sound_trigger_sound_model_type_t type, |
| 919 | sp<ModuleClient>& moduleClient) : |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame] | 920 | mHandle(handle), mState(STATE_IDLE), mCaptureSession(session), |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 921 | mCaptureIOHandle(ioHandle), mCaptureDevice(device), mType(type), |
| 922 | mModuleClient(moduleClient) |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 923 | { |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 924 | } |
| 925 | |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 926 | #undef LOG_TAG |
| 927 | #define LOG_TAG "SoundTriggerHwService::ModuleClient" |
| 928 | |
| 929 | SoundTriggerHwService::ModuleClient::ModuleClient(const sp<Module>& module, |
| 930 | const sp<ISoundTriggerClient>& client) |
| 931 | : mModule(module), mClient(client) |
| 932 | { |
| 933 | } |
| 934 | |
| 935 | void SoundTriggerHwService::ModuleClient::onFirstRef() |
| 936 | { |
Chris Thornton | c8a9f4a | 2017-02-06 18:31:42 -0800 | [diff] [blame] | 937 | sp<IBinder> binder = IInterface::asBinder(mClient); |
| 938 | if (binder != 0) { |
| 939 | binder->linkToDeath(this); |
| 940 | } |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | SoundTriggerHwService::ModuleClient::~ModuleClient() |
| 944 | { |
| 945 | } |
| 946 | |
| 947 | status_t SoundTriggerHwService::ModuleClient::dump(int fd __unused, |
| 948 | const Vector<String16>& args __unused) { |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 949 | String8 result; |
| 950 | return NO_ERROR; |
| 951 | } |
| 952 | |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 953 | void SoundTriggerHwService::ModuleClient::detach() { |
| 954 | ALOGV("detach()"); |
Eric Laurent | 7504b9e | 2017-08-15 18:17:26 -0700 | [diff] [blame] | 955 | if (!captureHotwordAllowed(IPCThreadState::self()->getCallingPid(), |
| 956 | IPCThreadState::self()->getCallingUid())) { |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 957 | return; |
| 958 | } |
| 959 | |
| 960 | { |
| 961 | AutoMutex lock(mLock); |
| 962 | if (mClient != 0) { |
| 963 | IInterface::asBinder(mClient)->unlinkToDeath(this); |
| 964 | mClient.clear(); |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | sp<Module> module = mModule.promote(); |
| 969 | if (module == 0) { |
| 970 | return; |
| 971 | } |
| 972 | module->detach(this); |
| 973 | } |
| 974 | |
| 975 | status_t SoundTriggerHwService::ModuleClient::loadSoundModel(const sp<IMemory>& modelMemory, |
| 976 | sound_model_handle_t *handle) |
| 977 | { |
| 978 | ALOGV("loadSoundModel() handle"); |
Eric Laurent | 7504b9e | 2017-08-15 18:17:26 -0700 | [diff] [blame] | 979 | if (!captureHotwordAllowed(IPCThreadState::self()->getCallingPid(), |
| 980 | IPCThreadState::self()->getCallingUid())) { |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 981 | return PERMISSION_DENIED; |
| 982 | } |
Eric Laurent | 9b11c02 | 2018-06-06 19:19:22 -0700 | [diff] [blame] | 983 | if (checkIMemory(modelMemory) != NO_ERROR) { |
| 984 | return BAD_VALUE; |
| 985 | } |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 986 | |
| 987 | sp<Module> module = mModule.promote(); |
| 988 | if (module == 0) { |
| 989 | return NO_INIT; |
| 990 | } |
| 991 | return module->loadSoundModel(modelMemory, this, handle); |
| 992 | } |
| 993 | |
| 994 | status_t SoundTriggerHwService::ModuleClient::unloadSoundModel(sound_model_handle_t handle) |
| 995 | { |
| 996 | ALOGV("unloadSoundModel() model handle %d", handle); |
Eric Laurent | 7504b9e | 2017-08-15 18:17:26 -0700 | [diff] [blame] | 997 | if (!captureHotwordAllowed(IPCThreadState::self()->getCallingPid(), |
| 998 | IPCThreadState::self()->getCallingUid())) { |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 999 | return PERMISSION_DENIED; |
| 1000 | } |
| 1001 | |
| 1002 | sp<Module> module = mModule.promote(); |
| 1003 | if (module == 0) { |
| 1004 | return NO_INIT; |
| 1005 | } |
| 1006 | return module->unloadSoundModel(handle); |
| 1007 | } |
| 1008 | |
| 1009 | status_t SoundTriggerHwService::ModuleClient::startRecognition(sound_model_handle_t handle, |
| 1010 | const sp<IMemory>& dataMemory) |
| 1011 | { |
| 1012 | ALOGV("startRecognition() model handle %d", handle); |
Eric Laurent | 7504b9e | 2017-08-15 18:17:26 -0700 | [diff] [blame] | 1013 | if (!captureHotwordAllowed(IPCThreadState::self()->getCallingPid(), |
| 1014 | IPCThreadState::self()->getCallingUid())) { |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 1015 | return PERMISSION_DENIED; |
| 1016 | } |
Eric Laurent | 9b11c02 | 2018-06-06 19:19:22 -0700 | [diff] [blame] | 1017 | if (checkIMemory(dataMemory) != NO_ERROR) { |
| 1018 | return BAD_VALUE; |
| 1019 | } |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 1020 | |
| 1021 | sp<Module> module = mModule.promote(); |
| 1022 | if (module == 0) { |
| 1023 | return NO_INIT; |
| 1024 | } |
| 1025 | return module->startRecognition(handle, dataMemory); |
| 1026 | } |
| 1027 | |
| 1028 | status_t SoundTriggerHwService::ModuleClient::stopRecognition(sound_model_handle_t handle) |
| 1029 | { |
| 1030 | ALOGV("stopRecognition() model handle %d", handle); |
Eric Laurent | 7504b9e | 2017-08-15 18:17:26 -0700 | [diff] [blame] | 1031 | if (!captureHotwordAllowed(IPCThreadState::self()->getCallingPid(), |
| 1032 | IPCThreadState::self()->getCallingUid())) { |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 1033 | return PERMISSION_DENIED; |
| 1034 | } |
| 1035 | |
| 1036 | sp<Module> module = mModule.promote(); |
| 1037 | if (module == 0) { |
| 1038 | return NO_INIT; |
| 1039 | } |
| 1040 | return module->stopRecognition(handle); |
| 1041 | } |
| 1042 | |
mike dooley | 6e189b1 | 2018-11-07 15:44:37 +0100 | [diff] [blame] | 1043 | status_t SoundTriggerHwService::ModuleClient::getModelState(sound_model_handle_t handle) |
Michael Dooley | 67e3d41 | 2018-10-16 19:51:16 +0000 | [diff] [blame] | 1044 | { |
| 1045 | ALOGV("getModelState() model handle %d", handle); |
| 1046 | if (!captureHotwordAllowed(IPCThreadState::self()->getCallingPid(), |
| 1047 | IPCThreadState::self()->getCallingUid())) { |
| 1048 | return PERMISSION_DENIED; |
| 1049 | } |
| 1050 | |
| 1051 | sp<Module> module = mModule.promote(); |
| 1052 | if (module == 0) { |
| 1053 | return NO_INIT; |
| 1054 | } |
mike dooley | 6e189b1 | 2018-11-07 15:44:37 +0100 | [diff] [blame] | 1055 | return module->getModelState(handle); |
Michael Dooley | 67e3d41 | 2018-10-16 19:51:16 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 1058 | void SoundTriggerHwService::ModuleClient::setCaptureState_l(bool active) |
| 1059 | { |
| 1060 | ALOGV("ModuleClient::setCaptureState_l %d", active); |
| 1061 | sp<SoundTriggerHwService> service; |
| 1062 | sound_trigger_service_state_t state; |
| 1063 | |
| 1064 | sp<Module> module = mModule.promote(); |
| 1065 | if (module == 0) { |
| 1066 | return; |
| 1067 | } |
| 1068 | { |
| 1069 | AutoMutex lock(mLock); |
| 1070 | state = (active && !module->isConcurrentCaptureAllowed()) ? |
| 1071 | SOUND_TRIGGER_STATE_DISABLED : SOUND_TRIGGER_STATE_ENABLED; |
| 1072 | |
| 1073 | service = module->service().promote(); |
| 1074 | if (service == 0) { |
| 1075 | return; |
| 1076 | } |
| 1077 | } |
Chris Thornton | 07405ee | 2017-11-14 20:45:27 -0800 | [diff] [blame] | 1078 | service->sendServiceStateEvent(state, this); |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | void SoundTriggerHwService::ModuleClient::onCallbackEvent(const sp<CallbackEvent>& event) |
| 1082 | { |
| 1083 | ALOGV("ModuleClient onCallbackEvent type %d", event->mType); |
| 1084 | |
| 1085 | sp<IMemory> eventMemory = event->mMemory; |
| 1086 | |
| 1087 | if (eventMemory == 0 || eventMemory->pointer() == NULL) { |
| 1088 | return; |
| 1089 | } |
| 1090 | |
Chris Thornton | 02b7421 | 2017-11-06 14:45:30 -0800 | [diff] [blame] | 1091 | sp<ISoundTriggerClient> client; |
| 1092 | { |
| 1093 | AutoMutex lock(mLock); |
| 1094 | client = mClient; |
| 1095 | } |
| 1096 | |
| 1097 | if (client != 0) { |
| 1098 | switch (event->mType) { |
| 1099 | case CallbackEvent::TYPE_RECOGNITION: { |
| 1100 | client->onRecognitionEvent(eventMemory); |
| 1101 | } break; |
| 1102 | case CallbackEvent::TYPE_SOUNDMODEL: { |
| 1103 | client->onSoundModelEvent(eventMemory); |
| 1104 | } break; |
| 1105 | case CallbackEvent::TYPE_SERVICE_STATE: { |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 1106 | client->onServiceStateChange(eventMemory); |
Chris Thornton | 02b7421 | 2017-11-06 14:45:30 -0800 | [diff] [blame] | 1107 | } break; |
| 1108 | default: |
| 1109 | LOG_ALWAYS_FATAL("onCallbackEvent unknown event type %d", event->mType); |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 1110 | } |
Haynes Mathew George | e52c500 | 2016-10-12 17:27:18 -0700 | [diff] [blame] | 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | void SoundTriggerHwService::ModuleClient::binderDied( |
| 1115 | const wp<IBinder> &who __unused) { |
| 1116 | ALOGW("client binder died for client %p", this); |
| 1117 | detach(); |
| 1118 | } |
| 1119 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 1120 | }; // namespace android |