blob: e43acd0d5b01413634129633d6d14177a169ae29 [file] [log] [blame]
Eric Laurentb7a11d82014-04-18 17:40:41 -07001/*
2**
3** Copyright (C) 2014, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9** http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#define LOG_TAG "SoundTrigger"
19//#define LOG_NDEBUG 0
20
21#include <utils/Log.h>
22#include <utils/threads.h>
23#include <binder/IPCThreadState.h>
24#include <binder/IServiceManager.h>
25#include <binder/IMemory.h>
26
27#include <soundtrigger/SoundTrigger.h>
28#include <soundtrigger/ISoundTrigger.h>
29#include <soundtrigger/ISoundTriggerHwService.h>
30#include <soundtrigger/ISoundTriggerClient.h>
31#include <soundtrigger/SoundTriggerCallback.h>
32
33namespace android {
34
35namespace {
36 sp<ISoundTriggerHwService> gSoundTriggerHwService;
37 const int kSoundTriggerHwServicePollDelay = 500000; // 0.5s
38 const char* kSoundTriggerHwServiceName = "media.sound_trigger_hw";
39 Mutex gLock;
40
41 class DeathNotifier : public IBinder::DeathRecipient
42 {
43 public:
44 DeathNotifier() {
45 }
46
47 virtual void binderDied(const wp<IBinder>& who __unused) {
48 ALOGV("binderDied");
49 Mutex::Autolock _l(gLock);
50 gSoundTriggerHwService.clear();
51 ALOGW("Sound trigger service died!");
52 }
53 };
54
55 sp<DeathNotifier> gDeathNotifier;
56}; // namespace anonymous
57
58const sp<ISoundTriggerHwService>& SoundTrigger::getSoundTriggerHwService()
59{
60 Mutex::Autolock _l(gLock);
61 if (gSoundTriggerHwService.get() == 0) {
62 sp<IServiceManager> sm = defaultServiceManager();
63 sp<IBinder> binder;
64 do {
65 binder = sm->getService(String16(kSoundTriggerHwServiceName));
66 if (binder != 0) {
67 break;
68 }
69 ALOGW("SoundTriggerHwService not published, waiting...");
70 usleep(kSoundTriggerHwServicePollDelay);
71 } while(true);
72 if (gDeathNotifier == NULL) {
73 gDeathNotifier = new DeathNotifier();
74 }
75 binder->linkToDeath(gDeathNotifier);
76 gSoundTriggerHwService = interface_cast<ISoundTriggerHwService>(binder);
77 }
78 ALOGE_IF(gSoundTriggerHwService == 0, "no SoundTriggerHwService!?");
79 return gSoundTriggerHwService;
80}
81
82// Static methods
83status_t SoundTrigger::listModules(struct sound_trigger_module_descriptor *modules,
84 uint32_t *numModules)
85{
86 ALOGV("listModules()");
87 const sp<ISoundTriggerHwService>& service = getSoundTriggerHwService();
88 if (service == 0) {
89 return NO_INIT;
90 }
91 return service->listModules(modules, numModules);
92}
93
94sp<SoundTrigger> SoundTrigger::attach(const sound_trigger_module_handle_t module,
95 const sp<SoundTriggerCallback>& callback)
96{
97 ALOGV("attach()");
98 sp<SoundTrigger> soundTrigger;
99 const sp<ISoundTriggerHwService>& service = getSoundTriggerHwService();
100 if (service == 0) {
101 return soundTrigger;
102 }
103 soundTrigger = new SoundTrigger(module, callback);
104 status_t status = service->attach(module, soundTrigger, soundTrigger->mISoundTrigger);
105
106 if (status == NO_ERROR && soundTrigger->mISoundTrigger != 0) {
107 soundTrigger->mISoundTrigger->asBinder()->linkToDeath(soundTrigger);
108 } else {
109 ALOGW("Error %d connecting to sound trigger service", status);
110 soundTrigger.clear();
111 }
112 return soundTrigger;
113}
114
115
116// SoundTrigger
117SoundTrigger::SoundTrigger(sound_trigger_module_handle_t module,
118 const sp<SoundTriggerCallback>& callback)
119 : mModule(module), mCallback(callback)
120{
121}
122
123SoundTrigger::~SoundTrigger()
124{
125 if (mISoundTrigger != 0) {
126 mISoundTrigger->detach();
127 }
128}
129
130
131void SoundTrigger::detach() {
132 ALOGV("detach()");
133 Mutex::Autolock _l(mLock);
134 mCallback.clear();
135 if (mISoundTrigger != 0) {
136 mISoundTrigger->detach();
137 mISoundTrigger->asBinder()->unlinkToDeath(this);
138 mISoundTrigger = 0;
139 }
140}
141
142status_t SoundTrigger::loadSoundModel(const sp<IMemory>& modelMemory,
143 sound_model_handle_t *handle)
144{
145 Mutex::Autolock _l(mLock);
146 if (mISoundTrigger == 0) {
147 return NO_INIT;
148 }
149
150 return mISoundTrigger->loadSoundModel(modelMemory, handle);
151}
152
153status_t SoundTrigger::unloadSoundModel(sound_model_handle_t handle)
154{
155 Mutex::Autolock _l(mLock);
156 if (mISoundTrigger == 0) {
157 return NO_INIT;
158 }
159 return mISoundTrigger->unloadSoundModel(handle);
160}
161
162status_t SoundTrigger::startRecognition(sound_model_handle_t handle,
163 const sp<IMemory>& dataMemory)
164{
165 Mutex::Autolock _l(mLock);
166 if (mISoundTrigger == 0) {
167 return NO_INIT;
168 }
169 return mISoundTrigger->startRecognition(handle, dataMemory);
170}
171
172status_t SoundTrigger::stopRecognition(sound_model_handle_t handle)
173{
174 Mutex::Autolock _l(mLock);
175 if (mISoundTrigger == 0) {
176 return NO_INIT;
177 }
178 return mISoundTrigger->stopRecognition(handle);
179}
180
181// BpSoundTriggerClient
182void SoundTrigger::onRecognitionEvent(const sp<IMemory>& eventMemory)
183{
184 Mutex::Autolock _l(mLock);
185 if (eventMemory == 0 || eventMemory->pointer() == NULL) {
186 return;
187 }
188
189 if (mCallback != 0) {
190 mCallback->onRecognitionEvent(
191 (struct sound_trigger_recognition_event *)eventMemory->pointer());
192 }
193}
194
195
196//IBinder::DeathRecipient
197void SoundTrigger::binderDied(const wp<IBinder>& who __unused) {
198 Mutex::Autolock _l(mLock);
199 ALOGW("SoundTrigger server binder Died ");
200 mISoundTrigger = 0;
201 if (mCallback != 0) {
202 mCallback->onServiceDied();
203 }
204}
205
206status_t SoundTrigger::stringToGuid(const char *str, sound_trigger_uuid_t *guid)
207{
208 if (str == NULL || guid == NULL) {
209 return BAD_VALUE;
210 }
211
212 int tmp[10];
213
214 if (sscanf(str, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
215 tmp, tmp+1, tmp+2, tmp+3, tmp+4, tmp+5, tmp+6, tmp+7, tmp+8, tmp+9) < 10) {
216 return BAD_VALUE;
217 }
218 guid->timeLow = (uint32_t)tmp[0];
219 guid->timeMid = (uint16_t)tmp[1];
220 guid->timeHiAndVersion = (uint16_t)tmp[2];
221 guid->clockSeq = (uint16_t)tmp[3];
222 guid->node[0] = (uint8_t)tmp[4];
223 guid->node[1] = (uint8_t)tmp[5];
224 guid->node[2] = (uint8_t)tmp[6];
225 guid->node[3] = (uint8_t)tmp[7];
226 guid->node[4] = (uint8_t)tmp[8];
227 guid->node[5] = (uint8_t)tmp[9];
228
229 return NO_ERROR;
230}
231
232status_t SoundTrigger::guidToString(const sound_trigger_uuid_t *guid, char *str, size_t maxLen)
233{
234 if (guid == NULL || str == NULL) {
235 return BAD_VALUE;
236 }
237
238 snprintf(str, maxLen, "%08x-%04x-%04x-%04x-%02x%02x%02x%02x%02x%02x",
239 guid->timeLow,
240 guid->timeMid,
241 guid->timeHiAndVersion,
242 guid->clockSeq,
243 guid->node[0],
244 guid->node[1],
245 guid->node[2],
246 guid->node[3],
247 guid->node[4],
248 guid->node[5]);
249
250 return NO_ERROR;
251}
252
253}; // namespace android