blob: 8f37bed5ca2cfec295b41611123b62cb1af04c05 [file] [log] [blame]
Eric Laurentb7a11d82014-04-18 17:40:41 -07001/*
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 Laurent8ba53d82014-08-01 23:15:05 +000025#include <system/sound_trigger.h>
26#include <cutils/atomic.h>
27#include <cutils/properties.h>
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070028#include <hardware/hardware.h>
29#include <media/AudioSystem.h>
Eric Laurent8ba53d82014-08-01 23:15:05 +000030#include <utils/Errors.h>
31#include <utils/Log.h>
Eric Laurentb7a11d82014-04-18 17:40:41 -070032#include <binder/IServiceManager.h>
33#include <binder/MemoryBase.h>
34#include <binder/MemoryHeapBase.h>
Eric Laurent936c84a2014-07-17 10:26:04 -070035#include <hardware/sound_trigger.h>
Eric Laurent8ba53d82014-08-01 23:15:05 +000036#include <ServiceUtilities.h>
37#include "SoundTriggerHwService.h"
Eric Laurentb7a11d82014-04-18 17:40:41 -070038
39namespace android {
40
41#ifdef SOUND_TRIGGER_USE_STUB_MODULE
42#define HW_MODULE_PREFIX "stub"
43#else
44#define HW_MODULE_PREFIX "primary"
45#endif
46
47SoundTriggerHwService::SoundTriggerHwService()
48 : BnSoundTriggerHwService(),
Eric Laurentdf3dc7e2014-07-27 18:39:40 -070049 mNextUniqueId(1),
50 mMemoryDealer(new MemoryDealer(1024 * 1024, "SoundTriggerHwService")),
51 mCaptureState(false)
Eric Laurentb7a11d82014-04-18 17:40:41 -070052{
53}
54
55void SoundTriggerHwService::onFirstRef()
56{
57 const hw_module_t *mod;
58 int rc;
59 sound_trigger_hw_device *dev;
60
61 rc = hw_get_module_by_class(SOUND_TRIGGER_HARDWARE_MODULE_ID, HW_MODULE_PREFIX, &mod);
62 if (rc != 0) {
63 ALOGE("couldn't load sound trigger module %s.%s (%s)",
Ryan Bavetta9609a912016-01-28 19:22:29 -080064 SOUND_TRIGGER_HARDWARE_MODULE_ID, HW_MODULE_PREFIX, strerror(-rc));
Eric Laurentb7a11d82014-04-18 17:40:41 -070065 return;
66 }
67 rc = sound_trigger_hw_device_open(mod, &dev);
68 if (rc != 0) {
69 ALOGE("couldn't open sound trigger hw device in %s.%s (%s)",
Ryan Bavetta9609a912016-01-28 19:22:29 -080070 SOUND_TRIGGER_HARDWARE_MODULE_ID, HW_MODULE_PREFIX, strerror(-rc));
Eric Laurentb7a11d82014-04-18 17:40:41 -070071 return;
72 }
Chris Thorntonefcf16c2016-03-27 17:13:28 -070073 if (dev->common.version < SOUND_TRIGGER_DEVICE_API_VERSION_1_0 ||
74 dev->common.version > SOUND_TRIGGER_DEVICE_API_VERSION_CURRENT) {
Eric Laurentb7a11d82014-04-18 17:40:41 -070075 ALOGE("wrong sound trigger hw device version %04x", dev->common.version);
76 return;
77 }
78
79 sound_trigger_module_descriptor descriptor;
80 rc = dev->get_properties(dev, &descriptor.properties);
81 if (rc != 0) {
82 ALOGE("could not read implementation properties");
83 return;
84 }
85 descriptor.handle =
86 (sound_trigger_module_handle_t)android_atomic_inc(&mNextUniqueId);
87 ALOGI("loaded default module %s, handle %d", descriptor.properties.description,
88 descriptor.handle);
89
90 sp<ISoundTriggerClient> client;
91 sp<Module> module = new Module(this, dev, descriptor, client);
92 mModules.add(descriptor.handle, module);
93 mCallbackThread = new CallbackThread(this);
94}
95
96SoundTriggerHwService::~SoundTriggerHwService()
97{
98 if (mCallbackThread != 0) {
99 mCallbackThread->exit();
100 }
101 for (size_t i = 0; i < mModules.size(); i++) {
102 sound_trigger_hw_device_close(mModules.valueAt(i)->hwDevice());
103 }
104}
105
106status_t SoundTriggerHwService::listModules(struct sound_trigger_module_descriptor *modules,
107 uint32_t *numModules)
108{
109 ALOGV("listModules");
Eric Laurent8ba53d82014-08-01 23:15:05 +0000110 if (!captureHotwordAllowed()) {
111 return PERMISSION_DENIED;
112 }
113
Eric Laurentb7a11d82014-04-18 17:40:41 -0700114 AutoMutex lock(mServiceLock);
115 if (numModules == NULL || (*numModules != 0 && modules == NULL)) {
116 return BAD_VALUE;
117 }
118 size_t maxModules = *numModules;
119 *numModules = mModules.size();
120 for (size_t i = 0; i < mModules.size() && i < maxModules; i++) {
121 modules[i] = mModules.valueAt(i)->descriptor();
122 }
123 return NO_ERROR;
124}
125
126status_t SoundTriggerHwService::attach(const sound_trigger_module_handle_t handle,
127 const sp<ISoundTriggerClient>& client,
128 sp<ISoundTrigger>& moduleInterface)
129{
130 ALOGV("attach module %d", handle);
Eric Laurent8ba53d82014-08-01 23:15:05 +0000131 if (!captureHotwordAllowed()) {
132 return PERMISSION_DENIED;
133 }
134
Eric Laurentb7a11d82014-04-18 17:40:41 -0700135 AutoMutex lock(mServiceLock);
136 moduleInterface.clear();
137 if (client == 0) {
138 return BAD_VALUE;
139 }
140 ssize_t index = mModules.indexOfKey(handle);
141 if (index < 0) {
142 return BAD_VALUE;
143 }
144 sp<Module> module = mModules.valueAt(index);
145
146 module->setClient(client);
Marco Nelissenf8880202014-11-14 07:58:25 -0800147 IInterface::asBinder(client)->linkToDeath(module);
Eric Laurentb7a11d82014-04-18 17:40:41 -0700148 moduleInterface = module;
149
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700150 module->setCaptureState_l(mCaptureState);
151
Eric Laurentb7a11d82014-04-18 17:40:41 -0700152 return NO_ERROR;
153}
154
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700155status_t SoundTriggerHwService::setCaptureState(bool active)
156{
157 ALOGV("setCaptureState %d", active);
158 AutoMutex lock(mServiceLock);
159 mCaptureState = active;
160 for (size_t i = 0; i < mModules.size(); i++) {
161 mModules.valueAt(i)->setCaptureState_l(active);
162 }
163 return NO_ERROR;
164}
165
166
167void SoundTriggerHwService::detachModule(sp<Module> module)
168{
Eric Laurent936c84a2014-07-17 10:26:04 -0700169 ALOGV("detachModule");
Eric Laurent8ba53d82014-08-01 23:15:05 +0000170 AutoMutex lock(mServiceLock);
Eric Laurentb7a11d82014-04-18 17:40:41 -0700171 module->clearClient();
172}
173
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700174
Eric Laurentb7a11d82014-04-18 17:40:41 -0700175static const int kDumpLockRetries = 50;
176static const int kDumpLockSleep = 60000;
177
178static bool tryLock(Mutex& mutex)
179{
180 bool locked = false;
181 for (int i = 0; i < kDumpLockRetries; ++i) {
182 if (mutex.tryLock() == NO_ERROR) {
183 locked = true;
184 break;
185 }
186 usleep(kDumpLockSleep);
187 }
188 return locked;
189}
190
191status_t SoundTriggerHwService::dump(int fd, const Vector<String16>& args __unused) {
192 String8 result;
193 if (checkCallingPermission(String16("android.permission.DUMP")) == false) {
194 result.appendFormat("Permission Denial: can't dump SoundTriggerHwService");
195 write(fd, result.string(), result.size());
196 } else {
197 bool locked = tryLock(mServiceLock);
198 // failed to lock - SoundTriggerHwService is probably deadlocked
199 if (!locked) {
200 result.append("SoundTriggerHwService may be deadlocked\n");
201 write(fd, result.string(), result.size());
202 }
203
204 if (locked) mServiceLock.unlock();
205 }
206 return NO_ERROR;
207}
208
209status_t SoundTriggerHwService::onTransact(
210 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) {
211 return BnSoundTriggerHwService::onTransact(code, data, reply, flags);
212}
213
214
215// static
216void SoundTriggerHwService::recognitionCallback(struct sound_trigger_recognition_event *event,
217 void *cookie)
218{
219 Module *module = (Module *)cookie;
220 if (module == NULL) {
221 return;
222 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700223 sp<SoundTriggerHwService> service = module->service().promote();
224 if (service == 0) {
225 return;
226 }
227
228 service->sendRecognitionEvent(event, module);
229}
230
231sp<IMemory> SoundTriggerHwService::prepareRecognitionEvent_l(
232 struct sound_trigger_recognition_event *event)
233{
234 sp<IMemory> eventMemory;
235
236 //sanitize event
237 switch (event->type) {
238 case SOUND_MODEL_TYPE_KEYPHRASE:
239 ALOGW_IF(event->data_size != 0 && event->data_offset !=
240 sizeof(struct sound_trigger_phrase_recognition_event),
241 "prepareRecognitionEvent_l(): invalid data offset %u for keyphrase event type",
242 event->data_offset);
243 event->data_offset = sizeof(struct sound_trigger_phrase_recognition_event);
244 break;
Ryan Bavetta00a727c2016-01-26 21:56:19 -0800245 case SOUND_MODEL_TYPE_GENERIC:
246 ALOGW_IF(event->data_size != 0 && event->data_offset !=
247 sizeof(struct sound_trigger_generic_recognition_event),
248 "prepareRecognitionEvent_l(): invalid data offset %u for generic event type",
249 event->data_offset);
250 event->data_offset = sizeof(struct sound_trigger_generic_recognition_event);
251 break;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700252 case SOUND_MODEL_TYPE_UNKNOWN:
253 ALOGW_IF(event->data_size != 0 && event->data_offset !=
254 sizeof(struct sound_trigger_recognition_event),
255 "prepareRecognitionEvent_l(): invalid data offset %u for unknown event type",
256 event->data_offset);
257 event->data_offset = sizeof(struct sound_trigger_recognition_event);
258 break;
259 default:
Eric Laurent886561f2014-08-28 19:45:37 -0700260 return eventMemory;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700261 }
262
263 size_t size = event->data_offset + event->data_size;
264 eventMemory = mMemoryDealer->allocate(size);
265 if (eventMemory == 0 || eventMemory->pointer() == NULL) {
266 eventMemory.clear();
267 return eventMemory;
268 }
269 memcpy(eventMemory->pointer(), event, size);
270
271 return eventMemory;
272}
273
274void SoundTriggerHwService::sendRecognitionEvent(struct sound_trigger_recognition_event *event,
275 Module *module)
276 {
277 AutoMutex lock(mServiceLock);
278 if (module == NULL) {
279 return;
280 }
281 sp<IMemory> eventMemory = prepareRecognitionEvent_l(event);
282 if (eventMemory == 0) {
283 return;
284 }
285 sp<Module> strongModule;
286 for (size_t i = 0; i < mModules.size(); i++) {
287 if (mModules.valueAt(i).get() == module) {
288 strongModule = mModules.valueAt(i);
289 break;
290 }
291 }
292 if (strongModule == 0) {
293 return;
294 }
295
296 sendCallbackEvent_l(new CallbackEvent(CallbackEvent::TYPE_RECOGNITION,
297 eventMemory, strongModule));
298}
299
300// static
301void SoundTriggerHwService::soundModelCallback(struct sound_trigger_model_event *event,
302 void *cookie)
303{
304 Module *module = (Module *)cookie;
305 if (module == NULL) {
306 return;
307 }
308 sp<SoundTriggerHwService> service = module->service().promote();
309 if (service == 0) {
310 return;
311 }
312
313 service->sendSoundModelEvent(event, module);
314}
315
316sp<IMemory> SoundTriggerHwService::prepareSoundModelEvent_l(struct sound_trigger_model_event *event)
317{
318 sp<IMemory> eventMemory;
319
320 size_t size = event->data_offset + event->data_size;
321 eventMemory = mMemoryDealer->allocate(size);
322 if (eventMemory == 0 || eventMemory->pointer() == NULL) {
323 eventMemory.clear();
324 return eventMemory;
325 }
326 memcpy(eventMemory->pointer(), event, size);
327
328 return eventMemory;
329}
330
331void SoundTriggerHwService::sendSoundModelEvent(struct sound_trigger_model_event *event,
332 Module *module)
333{
334 AutoMutex lock(mServiceLock);
335 sp<IMemory> eventMemory = prepareSoundModelEvent_l(event);
336 if (eventMemory == 0) {
337 return;
338 }
339 sp<Module> strongModule;
340 for (size_t i = 0; i < mModules.size(); i++) {
341 if (mModules.valueAt(i).get() == module) {
342 strongModule = mModules.valueAt(i);
343 break;
344 }
345 }
346 if (strongModule == 0) {
347 return;
348 }
349 sendCallbackEvent_l(new CallbackEvent(CallbackEvent::TYPE_SOUNDMODEL,
350 eventMemory, strongModule));
Eric Laurentb7a11d82014-04-18 17:40:41 -0700351}
352
353
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700354sp<IMemory> SoundTriggerHwService::prepareServiceStateEvent_l(sound_trigger_service_state_t state)
Eric Laurentb7a11d82014-04-18 17:40:41 -0700355{
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700356 sp<IMemory> eventMemory;
357
358 size_t size = sizeof(sound_trigger_service_state_t);
359 eventMemory = mMemoryDealer->allocate(size);
360 if (eventMemory == 0 || eventMemory->pointer() == NULL) {
361 eventMemory.clear();
362 return eventMemory;
363 }
364 *((sound_trigger_service_state_t *)eventMemory->pointer()) = state;
365 return eventMemory;
Eric Laurentb7a11d82014-04-18 17:40:41 -0700366}
367
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700368// call with mServiceLock held
369void SoundTriggerHwService::sendServiceStateEvent_l(sound_trigger_service_state_t state,
370 Module *module)
Eric Laurentb7a11d82014-04-18 17:40:41 -0700371{
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700372 sp<IMemory> eventMemory = prepareServiceStateEvent_l(state);
373 if (eventMemory == 0) {
374 return;
375 }
376 sp<Module> strongModule;
377 for (size_t i = 0; i < mModules.size(); i++) {
378 if (mModules.valueAt(i).get() == module) {
379 strongModule = mModules.valueAt(i);
380 break;
381 }
382 }
383 if (strongModule == 0) {
384 return;
385 }
386 sendCallbackEvent_l(new CallbackEvent(CallbackEvent::TYPE_SERVICE_STATE,
387 eventMemory, strongModule));
388}
389
390// call with mServiceLock held
391void SoundTriggerHwService::sendCallbackEvent_l(const sp<CallbackEvent>& event)
392{
393 mCallbackThread->sendCallbackEvent(event);
394}
395
396void SoundTriggerHwService::onCallbackEvent(const sp<CallbackEvent>& event)
397{
398 ALOGV("onCallbackEvent");
Eric Laurentb7a11d82014-04-18 17:40:41 -0700399 sp<Module> module;
400 {
401 AutoMutex lock(mServiceLock);
402 module = event->mModule.promote();
403 if (module == 0) {
404 return;
405 }
406 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700407 module->onCallbackEvent(event);
408 {
409 AutoMutex lock(mServiceLock);
410 // clear now to execute with mServiceLock locked
411 event->mMemory.clear();
412 }
Eric Laurentb7a11d82014-04-18 17:40:41 -0700413}
414
415#undef LOG_TAG
416#define LOG_TAG "SoundTriggerHwService::CallbackThread"
417
418SoundTriggerHwService::CallbackThread::CallbackThread(const wp<SoundTriggerHwService>& service)
419 : mService(service)
420{
421}
422
423SoundTriggerHwService::CallbackThread::~CallbackThread()
424{
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700425 while (!mEventQueue.isEmpty()) {
426 mEventQueue[0]->mMemory.clear();
427 mEventQueue.removeAt(0);
428 }
Eric Laurentb7a11d82014-04-18 17:40:41 -0700429}
430
431void SoundTriggerHwService::CallbackThread::onFirstRef()
432{
433 run("soundTrigger cbk", ANDROID_PRIORITY_URGENT_AUDIO);
434}
435
436bool SoundTriggerHwService::CallbackThread::threadLoop()
437{
438 while (!exitPending()) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700439 sp<CallbackEvent> event;
Eric Laurentb7a11d82014-04-18 17:40:41 -0700440 sp<SoundTriggerHwService> service;
441 {
442 Mutex::Autolock _l(mCallbackLock);
443 while (mEventQueue.isEmpty() && !exitPending()) {
444 ALOGV("CallbackThread::threadLoop() sleep");
445 mCallbackCond.wait(mCallbackLock);
446 ALOGV("CallbackThread::threadLoop() wake up");
447 }
448 if (exitPending()) {
449 break;
450 }
451 event = mEventQueue[0];
452 mEventQueue.removeAt(0);
453 service = mService.promote();
454 }
455 if (service != 0) {
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700456 service->onCallbackEvent(event);
Eric Laurentb7a11d82014-04-18 17:40:41 -0700457 }
458 }
459 return false;
460}
461
462void SoundTriggerHwService::CallbackThread::exit()
463{
464 Mutex::Autolock _l(mCallbackLock);
465 requestExit();
466 mCallbackCond.broadcast();
467}
468
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700469void SoundTriggerHwService::CallbackThread::sendCallbackEvent(
470 const sp<SoundTriggerHwService::CallbackEvent>& event)
Eric Laurentb7a11d82014-04-18 17:40:41 -0700471{
472 AutoMutex lock(mCallbackLock);
473 mEventQueue.add(event);
474 mCallbackCond.signal();
475}
476
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700477SoundTriggerHwService::CallbackEvent::CallbackEvent(event_type type, sp<IMemory> memory,
478 wp<Module> module)
479 : mType(type), mMemory(memory), mModule(module)
Eric Laurentb7a11d82014-04-18 17:40:41 -0700480{
481}
482
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700483SoundTriggerHwService::CallbackEvent::~CallbackEvent()
Eric Laurentb7a11d82014-04-18 17:40:41 -0700484{
485}
486
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700487
Eric Laurentb7a11d82014-04-18 17:40:41 -0700488#undef LOG_TAG
489#define LOG_TAG "SoundTriggerHwService::Module"
490
491SoundTriggerHwService::Module::Module(const sp<SoundTriggerHwService>& service,
492 sound_trigger_hw_device* hwDevice,
493 sound_trigger_module_descriptor descriptor,
494 const sp<ISoundTriggerClient>& client)
495 : mService(service), mHwDevice(hwDevice), mDescriptor(descriptor),
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700496 mClient(client), mServiceState(SOUND_TRIGGER_STATE_NO_INIT)
Eric Laurentb7a11d82014-04-18 17:40:41 -0700497{
498}
499
500SoundTriggerHwService::Module::~Module() {
501}
502
503void SoundTriggerHwService::Module::detach() {
504 ALOGV("detach()");
Eric Laurent8ba53d82014-08-01 23:15:05 +0000505 if (!captureHotwordAllowed()) {
506 return;
507 }
Eric Laurentb7a11d82014-04-18 17:40:41 -0700508 {
509 AutoMutex lock(mLock);
510 for (size_t i = 0; i < mModels.size(); i++) {
511 sp<Model> model = mModels.valueAt(i);
512 ALOGV("detach() unloading model %d", model->mHandle);
513 if (model->mState == Model::STATE_ACTIVE) {
514 mHwDevice->stop_recognition(mHwDevice, model->mHandle);
Eric Laurentb7a11d82014-04-18 17:40:41 -0700515 }
516 mHwDevice->unload_sound_model(mHwDevice, model->mHandle);
517 }
518 mModels.clear();
519 }
520 if (mClient != 0) {
Marco Nelissenf8880202014-11-14 07:58:25 -0800521 IInterface::asBinder(mClient)->unlinkToDeath(this);
Eric Laurentb7a11d82014-04-18 17:40:41 -0700522 }
523 sp<SoundTriggerHwService> service = mService.promote();
524 if (service == 0) {
525 return;
526 }
527 service->detachModule(this);
528}
529
530status_t SoundTriggerHwService::Module::loadSoundModel(const sp<IMemory>& modelMemory,
531 sound_model_handle_t *handle)
532{
533 ALOGV("loadSoundModel() handle");
Eric Laurent8ba53d82014-08-01 23:15:05 +0000534 if (!captureHotwordAllowed()) {
535 return PERMISSION_DENIED;
536 }
Eric Laurentb7a11d82014-04-18 17:40:41 -0700537
538 if (modelMemory == 0 || modelMemory->pointer() == NULL) {
539 ALOGE("loadSoundModel() modelMemory is 0 or has NULL pointer()");
540 return BAD_VALUE;
541 }
542 struct sound_trigger_sound_model *sound_model =
543 (struct sound_trigger_sound_model *)modelMemory->pointer();
544
545 AutoMutex lock(mLock);
Eric Laurent02eb47c2014-11-20 10:10:20 -0800546
547 if (mModels.size() >= mDescriptor.properties.max_sound_models) {
Ryan Bavetta2c561202016-02-16 14:17:31 -0800548 /* Make space for a keyphrase sound model by first trying to swap out a previously loaded
549 * keyphrase sound model, or if needed, another sound model. This decision would optimally
550 * happen in SoundTriggerHelper, but is happening here because state tracking isn't good
551 * enough in SoundTriggerHelper to ensure that state is consistent between it and the HAL,
552 * nor does sufficient error handling exist to recover from inconsistencies.
553 * Once that exists:
554 * TODO: we should return an error instead of unloading a previous sound model here.
555 */
Eric Laurent02eb47c2014-11-20 10:10:20 -0800556 if (mModels.size() == 0) {
557 return INVALID_OPERATION;
558 }
Ryan Bavetta2c561202016-02-16 14:17:31 -0800559 if (sound_model->type == SOUND_MODEL_TYPE_KEYPHRASE) {
560 ALOGW("loadSoundModel() max number of models exceeded %d making room for a new one",
561 mDescriptor.properties.max_sound_models);
562 sound_model_handle_t unload_handle = mModels.valueAt(0)->mHandle;
563 for (size_t i = 0; i < mModels.size(); i++) {
564 if (mModels.valueAt(i)->mType == SOUND_MODEL_TYPE_KEYPHRASE) {
565 unload_handle = mModels.keyAt(i);
566 break;
567 }
568 }
569 unloadSoundModel_l(unload_handle);
570 } else {
571 ALOGW("loadSoundModel(): Not loading, max number of models (%d) would be exceeded",
572 mDescriptor.properties.max_sound_models);
573 return INVALID_OPERATION;
574 }
Eric Laurent02eb47c2014-11-20 10:10:20 -0800575 }
576
Ryan Bavetta2c561202016-02-16 14:17:31 -0800577 status_t status = mHwDevice->load_sound_model(mHwDevice, sound_model,
Eric Laurentb7a11d82014-04-18 17:40:41 -0700578 SoundTriggerHwService::soundModelCallback,
Ryan Bavetta2c561202016-02-16 14:17:31 -0800579 this, handle);
580
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700581 if (status != NO_ERROR) {
582 return status;
Eric Laurentb7a11d82014-04-18 17:40:41 -0700583 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700584 audio_session_t session;
585 audio_io_handle_t ioHandle;
586 audio_devices_t device;
587
588 status = AudioSystem::acquireSoundTriggerSession(&session, &ioHandle, &device);
589 if (status != NO_ERROR) {
590 return status;
591 }
592
593 sp<Model> model = new Model(*handle, session, ioHandle, device, sound_model->type);
594 mModels.replaceValueFor(*handle, model);
Eric Laurentb7a11d82014-04-18 17:40:41 -0700595
596 return status;
597}
598
599status_t SoundTriggerHwService::Module::unloadSoundModel(sound_model_handle_t handle)
600{
601 ALOGV("unloadSoundModel() model handle %d", handle);
Eric Laurent8ba53d82014-08-01 23:15:05 +0000602 if (!captureHotwordAllowed()) {
603 return PERMISSION_DENIED;
604 }
Eric Laurentb7a11d82014-04-18 17:40:41 -0700605
606 AutoMutex lock(mLock);
Eric Laurent02eb47c2014-11-20 10:10:20 -0800607 return unloadSoundModel_l(handle);
608}
609
610status_t SoundTriggerHwService::Module::unloadSoundModel_l(sound_model_handle_t handle)
611{
Eric Laurentb7a11d82014-04-18 17:40:41 -0700612 ssize_t index = mModels.indexOfKey(handle);
613 if (index < 0) {
614 return BAD_VALUE;
615 }
Eric Laurent1a1cba82014-06-09 16:20:05 -0700616 sp<Model> model = mModels.valueAt(index);
Eric Laurentb7a11d82014-04-18 17:40:41 -0700617 mModels.removeItem(handle);
Eric Laurent1a1cba82014-06-09 16:20:05 -0700618 if (model->mState == Model::STATE_ACTIVE) {
619 mHwDevice->stop_recognition(mHwDevice, model->mHandle);
Eric Laurent02eb47c2014-11-20 10:10:20 -0800620 model->mState = Model::STATE_IDLE;
Eric Laurent1a1cba82014-06-09 16:20:05 -0700621 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700622 AudioSystem::releaseSoundTriggerSession(model->mCaptureSession);
Eric Laurentb7a11d82014-04-18 17:40:41 -0700623 return mHwDevice->unload_sound_model(mHwDevice, handle);
624}
625
626status_t SoundTriggerHwService::Module::startRecognition(sound_model_handle_t handle,
Eric Laurent0832b2d2014-07-06 16:17:25 -0700627 const sp<IMemory>& dataMemory)
Eric Laurentb7a11d82014-04-18 17:40:41 -0700628{
629 ALOGV("startRecognition() model handle %d", handle);
Eric Laurent8ba53d82014-08-01 23:15:05 +0000630 if (!captureHotwordAllowed()) {
631 return PERMISSION_DENIED;
632 }
Eric Laurentb7a11d82014-04-18 17:40:41 -0700633
634 if (dataMemory != 0 && dataMemory->pointer() == NULL) {
635 ALOGE("startRecognition() dataMemory is non-0 but has NULL pointer()");
636 return BAD_VALUE;
637
638 }
639 AutoMutex lock(mLock);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700640 if (mServiceState == SOUND_TRIGGER_STATE_DISABLED) {
641 return INVALID_OPERATION;
642 }
Eric Laurentb7a11d82014-04-18 17:40:41 -0700643 sp<Model> model = getModel(handle);
644 if (model == 0) {
645 return BAD_VALUE;
646 }
Eric Laurent0832b2d2014-07-06 16:17:25 -0700647 if ((dataMemory == 0) ||
648 (dataMemory->size() < sizeof(struct sound_trigger_recognition_config))) {
649 return BAD_VALUE;
650 }
Eric Laurentb7a11d82014-04-18 17:40:41 -0700651
652 if (model->mState == Model::STATE_ACTIVE) {
653 return INVALID_OPERATION;
654 }
Eric Laurentb7a11d82014-04-18 17:40:41 -0700655
Eric Laurent0832b2d2014-07-06 16:17:25 -0700656 struct sound_trigger_recognition_config *config =
657 (struct sound_trigger_recognition_config *)dataMemory->pointer();
Eric Laurentb7a11d82014-04-18 17:40:41 -0700658
659 //TODO: get capture handle and device from audio policy service
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700660 config->capture_handle = model->mCaptureIOHandle;
661 config->capture_device = model->mCaptureDevice;
662 status_t status = mHwDevice->start_recognition(mHwDevice, handle, config,
Eric Laurentb7a11d82014-04-18 17:40:41 -0700663 SoundTriggerHwService::recognitionCallback,
Eric Laurent0832b2d2014-07-06 16:17:25 -0700664 this);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700665
666 if (status == NO_ERROR) {
667 model->mState = Model::STATE_ACTIVE;
668 model->mConfig = *config;
669 }
670
671 return status;
Eric Laurentb7a11d82014-04-18 17:40:41 -0700672}
673
674status_t SoundTriggerHwService::Module::stopRecognition(sound_model_handle_t handle)
675{
676 ALOGV("stopRecognition() model handle %d", handle);
Eric Laurent8ba53d82014-08-01 23:15:05 +0000677 if (!captureHotwordAllowed()) {
678 return PERMISSION_DENIED;
679 }
Eric Laurentb7a11d82014-04-18 17:40:41 -0700680
681 AutoMutex lock(mLock);
682 sp<Model> model = getModel(handle);
683 if (model == 0) {
684 return BAD_VALUE;
685 }
686
687 if (model->mState != Model::STATE_ACTIVE) {
688 return INVALID_OPERATION;
689 }
690 mHwDevice->stop_recognition(mHwDevice, handle);
Eric Laurentb7a11d82014-04-18 17:40:41 -0700691 model->mState = Model::STATE_IDLE;
692 return NO_ERROR;
693}
694
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700695
696void SoundTriggerHwService::Module::onCallbackEvent(const sp<CallbackEvent>& event)
Eric Laurentb7a11d82014-04-18 17:40:41 -0700697{
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700698 ALOGV("onCallbackEvent type %d", event->mType);
Eric Laurentb7a11d82014-04-18 17:40:41 -0700699
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700700 sp<IMemory> eventMemory = event->mMemory;
Eric Laurentb7a11d82014-04-18 17:40:41 -0700701
702 if (eventMemory == 0 || eventMemory->pointer() == NULL) {
703 return;
704 }
Eric Laurentb7a11d82014-04-18 17:40:41 -0700705 if (mClient == 0) {
706 ALOGI("%s mClient == 0", __func__);
707 return;
708 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700709
710 switch (event->mType) {
711 case CallbackEvent::TYPE_RECOGNITION: {
712 struct sound_trigger_recognition_event *recognitionEvent =
713 (struct sound_trigger_recognition_event *)eventMemory->pointer();
Eric Laurent886561f2014-08-28 19:45:37 -0700714 sp<ISoundTriggerClient> client;
715 {
716 AutoMutex lock(mLock);
717 sp<Model> model = getModel(recognitionEvent->model);
718 if (model == 0) {
719 ALOGW("%s model == 0", __func__);
720 return;
721 }
722 if (model->mState != Model::STATE_ACTIVE) {
723 ALOGV("onCallbackEvent model->mState %d != Model::STATE_ACTIVE", model->mState);
724 return;
725 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700726
Eric Laurent886561f2014-08-28 19:45:37 -0700727 recognitionEvent->capture_session = model->mCaptureSession;
728 model->mState = Model::STATE_IDLE;
729 client = mClient;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700730 }
Eric Laurent886561f2014-08-28 19:45:37 -0700731 if (client != 0) {
732 client->onRecognitionEvent(eventMemory);
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700733 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700734 } break;
735 case CallbackEvent::TYPE_SOUNDMODEL: {
736 struct sound_trigger_model_event *soundmodelEvent =
737 (struct sound_trigger_model_event *)eventMemory->pointer();
Eric Laurent886561f2014-08-28 19:45:37 -0700738 sp<ISoundTriggerClient> client;
739 {
740 AutoMutex lock(mLock);
741 sp<Model> model = getModel(soundmodelEvent->model);
742 if (model == 0) {
743 ALOGW("%s model == 0", __func__);
744 return;
745 }
746 client = mClient;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700747 }
Eric Laurent886561f2014-08-28 19:45:37 -0700748 if (client != 0) {
749 client->onSoundModelEvent(eventMemory);
750 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700751 } break;
752 case CallbackEvent::TYPE_SERVICE_STATE: {
Eric Laurent886561f2014-08-28 19:45:37 -0700753 sp<ISoundTriggerClient> client;
754 {
755 AutoMutex lock(mLock);
756 client = mClient;
757 }
758 if (client != 0) {
759 client->onServiceStateChange(eventMemory);
760 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700761 } break;
762 default:
763 LOG_ALWAYS_FATAL("onCallbackEvent unknown event type %d", event->mType);
764 }
Eric Laurentb7a11d82014-04-18 17:40:41 -0700765}
766
767sp<SoundTriggerHwService::Model> SoundTriggerHwService::Module::getModel(
768 sound_model_handle_t handle)
769{
770 sp<Model> model;
771 ssize_t index = mModels.indexOfKey(handle);
772 if (index >= 0) {
773 model = mModels.valueAt(index);
774 }
775 return model;
776}
777
778void SoundTriggerHwService::Module::binderDied(
779 const wp<IBinder> &who __unused) {
780 ALOGW("client binder died for module %d", mDescriptor.handle);
781 detach();
782}
783
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700784// Called with mServiceLock held
785void SoundTriggerHwService::Module::setCaptureState_l(bool active)
Eric Laurentb7a11d82014-04-18 17:40:41 -0700786{
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700787 ALOGV("Module::setCaptureState_l %d", active);
788 sp<SoundTriggerHwService> service;
789 sound_trigger_service_state_t state;
Eric Laurentb7a11d82014-04-18 17:40:41 -0700790
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700791 Vector< sp<IMemory> > events;
792 {
793 AutoMutex lock(mLock);
794 state = (active && !mDescriptor.properties.concurrent_capture) ?
795 SOUND_TRIGGER_STATE_DISABLED : SOUND_TRIGGER_STATE_ENABLED;
Eric Laurentb7a11d82014-04-18 17:40:41 -0700796
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700797 if (state == mServiceState) {
798 return;
799 }
Eric Laurentb7a11d82014-04-18 17:40:41 -0700800
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700801 mServiceState = state;
802
803 service = mService.promote();
804 if (service == 0) {
805 return;
806 }
807
808 if (state == SOUND_TRIGGER_STATE_ENABLED) {
809 goto exit;
810 }
811
Chris Thorntonefcf16c2016-03-27 17:13:28 -0700812 const bool supports_stop_all =
813 (mHwDevice->common.version >= SOUND_TRIGGER_DEVICE_API_VERSION_1_1 &&
814 mHwDevice->stop_all_recognitions);
815
816 if (supports_stop_all) {
817 mHwDevice->stop_all_recognitions(mHwDevice);
818 }
819
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700820 for (size_t i = 0; i < mModels.size(); i++) {
821 sp<Model> model = mModels.valueAt(i);
822 if (model->mState == Model::STATE_ACTIVE) {
Chris Thorntonefcf16c2016-03-27 17:13:28 -0700823 if (!supports_stop_all) {
824 mHwDevice->stop_recognition(mHwDevice, model->mHandle);
825 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700826 // keep model in ACTIVE state so that event is processed by onCallbackEvent()
Ryan Bavetta00a727c2016-01-26 21:56:19 -0800827 if (model->mType == SOUND_MODEL_TYPE_KEYPHRASE) {
828 struct sound_trigger_phrase_recognition_event event;
829 memset(&event, 0, sizeof(struct sound_trigger_phrase_recognition_event));
830 event.num_phrases = model->mConfig.num_phrases;
831 for (size_t i = 0; i < event.num_phrases; i++) {
832 event.phrase_extras[i] = model->mConfig.phrases[i];
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700833 }
Ryan Bavetta00a727c2016-01-26 21:56:19 -0800834 event.common.status = RECOGNITION_STATUS_ABORT;
835 event.common.type = model->mType;
836 event.common.model = model->mHandle;
837 event.common.data_size = 0;
838 sp<IMemory> eventMemory = service->prepareRecognitionEvent_l(&event.common);
839 if (eventMemory != 0) {
840 events.add(eventMemory);
841 }
842 } else if (model->mType == SOUND_MODEL_TYPE_GENERIC) {
843 struct sound_trigger_generic_recognition_event event;
844 memset(&event, 0, sizeof(struct sound_trigger_generic_recognition_event));
845 event.common.status = RECOGNITION_STATUS_ABORT;
846 event.common.type = model->mType;
847 event.common.model = model->mHandle;
848 event.common.data_size = 0;
849 sp<IMemory> eventMemory = service->prepareRecognitionEvent_l(&event.common);
850 if (eventMemory != 0) {
851 events.add(eventMemory);
852 }
Ryan Bavetta9609a912016-01-28 19:22:29 -0800853 } else if (model->mType == SOUND_MODEL_TYPE_UNKNOWN) {
854 struct sound_trigger_phrase_recognition_event event;
855 memset(&event, 0, sizeof(struct sound_trigger_phrase_recognition_event));
856 event.common.status = RECOGNITION_STATUS_ABORT;
857 event.common.type = model->mType;
858 event.common.model = model->mHandle;
859 event.common.data_size = 0;
860 sp<IMemory> eventMemory = service->prepareRecognitionEvent_l(&event.common);
861 if (eventMemory != 0) {
862 events.add(eventMemory);
863 }
Ryan Bavetta00a727c2016-01-26 21:56:19 -0800864 } else {
865 goto exit;
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700866 }
867 }
868 }
Eric Laurentb7a11d82014-04-18 17:40:41 -0700869 }
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700870
871 for (size_t i = 0; i < events.size(); i++) {
872 service->sendCallbackEvent_l(new CallbackEvent(CallbackEvent::TYPE_RECOGNITION, events[i],
873 this));
874 }
875
876exit:
877 service->sendServiceStateEvent_l(state, this);
Eric Laurentb7a11d82014-04-18 17:40:41 -0700878}
879
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700880
881SoundTriggerHwService::Model::Model(sound_model_handle_t handle, audio_session_t session,
882 audio_io_handle_t ioHandle, audio_devices_t device,
883 sound_trigger_sound_model_type_t type) :
884 mHandle(handle), mState(STATE_IDLE), mCaptureSession(session),
885 mCaptureIOHandle(ioHandle), mCaptureDevice(device), mType(type)
Eric Laurentb7a11d82014-04-18 17:40:41 -0700886{
Eric Laurentdf3dc7e2014-07-27 18:39:40 -0700887
Eric Laurentb7a11d82014-04-18 17:40:41 -0700888}
889
890status_t SoundTriggerHwService::Module::dump(int fd __unused,
891 const Vector<String16>& args __unused) {
892 String8 result;
893 return NO_ERROR;
894}
895
896}; // namespace android