SoundTriggerHwService.cpp 36 KB

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