blob: 377f2a1faec7d064518b7e8cc97851da9f9e7815 [file] [log] [blame]
Eric Laurentb7a11d82014-04-18 17:40:41 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_HARDWARE_SOUNDTRIGGER_HAL_SERVICE_H
18#define ANDROID_HARDWARE_SOUNDTRIGGER_HAL_SERVICE_H
19
20#include <utils/Vector.h>
21//#include <binder/AppOpsManager.h>
22#include <binder/MemoryDealer.h>
23#include <binder/BinderService.h>
24#include <binder/IAppOpsCallback.h>
25#include <soundtrigger/ISoundTriggerHwService.h>
26#include <soundtrigger/ISoundTrigger.h>
27#include <soundtrigger/ISoundTriggerClient.h>
28#include <system/sound_trigger.h>
29#include <hardware/sound_trigger.h>
30
31namespace android {
32
33class MemoryHeapBase;
34
35class SoundTriggerHwService :
36 public BinderService<SoundTriggerHwService>,
37 public BnSoundTriggerHwService
38{
39 friend class BinderService<SoundTriggerHwService>;
40public:
41 class Module;
42
43 static char const* getServiceName() { return "media.sound_trigger_hw"; }
44
45 SoundTriggerHwService();
46 virtual ~SoundTriggerHwService();
47
48 // ISoundTriggerHwService
49 virtual status_t listModules(struct sound_trigger_module_descriptor *modules,
50 uint32_t *numModules);
51
52 virtual status_t attach(const sound_trigger_module_handle_t handle,
53 const sp<ISoundTriggerClient>& client,
54 sp<ISoundTrigger>& module);
55
56 virtual status_t onTransact(uint32_t code, const Parcel& data,
57 Parcel* reply, uint32_t flags);
58
59 virtual status_t dump(int fd, const Vector<String16>& args);
60
61 class Model : public RefBase {
62 public:
63
64 enum {
65 STATE_IDLE,
66 STATE_ACTIVE
67 };
68
69 Model(sound_model_handle_t handle);
70 ~Model() {}
71
72 sp<IMemory> allocateMemory(size_t size);
73 void deallocateMemory();
74
75 sound_model_handle_t mHandle;
76 int mState;
77 audio_io_handle_t mInputHandle;
78 audio_session_t mCaptureSession;
79 sp<MemoryDealer> mMemoryDealer;
80 };
81
82 class Module : public virtual RefBase,
83 public BnSoundTrigger,
84 public IBinder::DeathRecipient {
85 public:
86
87 Module(const sp<SoundTriggerHwService>& service,
88 sound_trigger_hw_device* hwDevice,
89 sound_trigger_module_descriptor descriptor,
90 const sp<ISoundTriggerClient>& client);
91
92 virtual ~Module();
93
94 virtual void detach();
95
96 virtual status_t loadSoundModel(const sp<IMemory>& modelMemory,
97 sound_model_handle_t *handle);
98
99 virtual status_t unloadSoundModel(sound_model_handle_t handle);
100
101 virtual status_t startRecognition(sound_model_handle_t handle,
102 const sp<IMemory>& dataMemory);
103 virtual status_t stopRecognition(sound_model_handle_t handle);
104
105 virtual status_t dump(int fd, const Vector<String16>& args);
106
107
108 sound_trigger_hw_device *hwDevice() const { return mHwDevice; }
109 struct sound_trigger_module_descriptor descriptor() { return mDescriptor; }
110 void setClient(sp<ISoundTriggerClient> client) { mClient = client; }
111 void clearClient() { mClient.clear(); }
112 sp<ISoundTriggerClient> client() { return mClient; }
113
114 void sendRecognitionEvent(struct sound_trigger_recognition_event *event);
115 void onRecognitionEvent(sp<IMemory> eventMemory);
116
117 sp<Model> getModel(sound_model_handle_t handle);
118
119 // IBinder::DeathRecipient implementation
120 virtual void binderDied(const wp<IBinder> &who);
121
122 private:
123 Mutex mLock;
124 wp<SoundTriggerHwService> mService;
125 struct sound_trigger_hw_device* mHwDevice;
126 struct sound_trigger_module_descriptor mDescriptor;
127 sp<ISoundTriggerClient> mClient;
128 DefaultKeyedVector< sound_model_handle_t, sp<Model> > mModels;
129 }; // class Module
130
131 class RecognitionEvent : public RefBase {
132 public:
133
134 RecognitionEvent(sp<IMemory> eventMemory, wp<Module> module);
135
136 virtual ~RecognitionEvent();
137
138 sp<IMemory> mEventMemory;
139 wp<Module> mModule;
140 };
141
142 class CallbackThread : public Thread {
143 public:
144
145 CallbackThread(const wp<SoundTriggerHwService>& service);
146
147 virtual ~CallbackThread();
148
149 // Thread virtuals
150 virtual bool threadLoop();
151
152 // RefBase
153 virtual void onFirstRef();
154
155 void exit();
156 void sendRecognitionEvent(const sp<RecognitionEvent>& event);
157
158 private:
159 wp<SoundTriggerHwService> mService;
160 Condition mCallbackCond;
161 Mutex mCallbackLock;
162 Vector< sp<RecognitionEvent> > mEventQueue;
163 };
164
165 void detachModule(sp<Module> module);
166
167 static void recognitionCallback(struct sound_trigger_recognition_event *event, void *cookie);
168 void sendRecognitionEvent(const sp<RecognitionEvent>& event);
169 void onRecognitionEvent(const sp<RecognitionEvent>& event);
170
171 static void soundModelCallback(struct sound_trigger_model_event *event, void *cookie);
172
173private:
174
175 virtual void onFirstRef();
176
177 Mutex mServiceLock;
178 volatile int32_t mNextUniqueId;
179 DefaultKeyedVector< sound_trigger_module_handle_t, sp<Module> > mModules;
180 sp<CallbackThread> mCallbackThread;
181};
182
183} // namespace android
184
185#endif // ANDROID_HARDWARE_SOUNDTRIGGER_HAL_SERVICE_H