Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 31 | namespace android { |
| 32 | |
| 33 | class MemoryHeapBase; |
| 34 | |
| 35 | class SoundTriggerHwService : |
| 36 | public BinderService<SoundTriggerHwService>, |
| 37 | public BnSoundTriggerHwService |
| 38 | { |
| 39 | friend class BinderService<SoundTriggerHwService>; |
| 40 | public: |
| 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 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame^] | 56 | virtual status_t setCaptureState(bool active); |
| 57 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 58 | virtual status_t onTransact(uint32_t code, const Parcel& data, |
| 59 | Parcel* reply, uint32_t flags); |
| 60 | |
| 61 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 62 | |
| 63 | class Model : public RefBase { |
| 64 | public: |
| 65 | |
| 66 | enum { |
| 67 | STATE_IDLE, |
| 68 | STATE_ACTIVE |
| 69 | }; |
| 70 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame^] | 71 | Model(sound_model_handle_t handle, audio_session_t session, audio_io_handle_t ioHandle, |
| 72 | audio_devices_t device, sound_trigger_sound_model_type_t type); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 73 | ~Model() {} |
| 74 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 75 | sound_model_handle_t mHandle; |
| 76 | int mState; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 77 | audio_session_t mCaptureSession; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame^] | 78 | audio_io_handle_t mCaptureIOHandle; |
| 79 | audio_devices_t mCaptureDevice; |
| 80 | sound_trigger_sound_model_type_t mType; |
| 81 | struct sound_trigger_recognition_config mConfig; |
| 82 | }; |
| 83 | |
| 84 | class CallbackEvent : public RefBase { |
| 85 | public: |
| 86 | typedef enum { |
| 87 | TYPE_RECOGNITION, |
| 88 | TYPE_SOUNDMODEL, |
| 89 | TYPE_SERVICE_STATE, |
| 90 | } event_type; |
| 91 | CallbackEvent(event_type type, sp<IMemory> memory, wp<Module> module); |
| 92 | |
| 93 | virtual ~CallbackEvent(); |
| 94 | |
| 95 | event_type mType; |
| 96 | sp<IMemory> mMemory; |
| 97 | wp<Module> mModule; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | class Module : public virtual RefBase, |
| 101 | public BnSoundTrigger, |
| 102 | public IBinder::DeathRecipient { |
| 103 | public: |
| 104 | |
| 105 | Module(const sp<SoundTriggerHwService>& service, |
| 106 | sound_trigger_hw_device* hwDevice, |
| 107 | sound_trigger_module_descriptor descriptor, |
| 108 | const sp<ISoundTriggerClient>& client); |
| 109 | |
| 110 | virtual ~Module(); |
| 111 | |
| 112 | virtual void detach(); |
| 113 | |
| 114 | virtual status_t loadSoundModel(const sp<IMemory>& modelMemory, |
| 115 | sound_model_handle_t *handle); |
| 116 | |
| 117 | virtual status_t unloadSoundModel(sound_model_handle_t handle); |
| 118 | |
| 119 | virtual status_t startRecognition(sound_model_handle_t handle, |
| 120 | const sp<IMemory>& dataMemory); |
| 121 | virtual status_t stopRecognition(sound_model_handle_t handle); |
| 122 | |
| 123 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 124 | |
| 125 | |
| 126 | sound_trigger_hw_device *hwDevice() const { return mHwDevice; } |
| 127 | struct sound_trigger_module_descriptor descriptor() { return mDescriptor; } |
| 128 | void setClient(sp<ISoundTriggerClient> client) { mClient = client; } |
| 129 | void clearClient() { mClient.clear(); } |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame^] | 130 | sp<ISoundTriggerClient> client() const { return mClient; } |
| 131 | wp<SoundTriggerHwService> service() const { return mService; } |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 132 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame^] | 133 | void onCallbackEvent(const sp<CallbackEvent>& event); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 134 | |
| 135 | sp<Model> getModel(sound_model_handle_t handle); |
| 136 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame^] | 137 | void setCaptureState_l(bool active); |
| 138 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 139 | // IBinder::DeathRecipient implementation |
| 140 | virtual void binderDied(const wp<IBinder> &who); |
| 141 | |
| 142 | private: |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame^] | 143 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 144 | Mutex mLock; |
| 145 | wp<SoundTriggerHwService> mService; |
| 146 | struct sound_trigger_hw_device* mHwDevice; |
| 147 | struct sound_trigger_module_descriptor mDescriptor; |
| 148 | sp<ISoundTriggerClient> mClient; |
| 149 | DefaultKeyedVector< sound_model_handle_t, sp<Model> > mModels; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame^] | 150 | sound_trigger_service_state_t mServiceState; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 151 | }; // class Module |
| 152 | |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 153 | class CallbackThread : public Thread { |
| 154 | public: |
| 155 | |
| 156 | CallbackThread(const wp<SoundTriggerHwService>& service); |
| 157 | |
| 158 | virtual ~CallbackThread(); |
| 159 | |
| 160 | // Thread virtuals |
| 161 | virtual bool threadLoop(); |
| 162 | |
| 163 | // RefBase |
| 164 | virtual void onFirstRef(); |
| 165 | |
| 166 | void exit(); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame^] | 167 | void sendCallbackEvent(const sp<CallbackEvent>& event); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 168 | |
| 169 | private: |
| 170 | wp<SoundTriggerHwService> mService; |
| 171 | Condition mCallbackCond; |
| 172 | Mutex mCallbackLock; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame^] | 173 | Vector< sp<CallbackEvent> > mEventQueue; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 174 | }; |
| 175 | |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame^] | 176 | void detachModule(sp<Module> module); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 177 | |
| 178 | static void recognitionCallback(struct sound_trigger_recognition_event *event, void *cookie); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame^] | 179 | sp<IMemory> prepareRecognitionEvent_l(struct sound_trigger_recognition_event *event); |
| 180 | void sendRecognitionEvent(struct sound_trigger_recognition_event *event, Module *module); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 181 | |
| 182 | static void soundModelCallback(struct sound_trigger_model_event *event, void *cookie); |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame^] | 183 | sp<IMemory> prepareSoundModelEvent_l(struct sound_trigger_model_event *event); |
| 184 | void sendSoundModelEvent(struct sound_trigger_model_event *event, Module *module); |
| 185 | |
| 186 | sp<IMemory> prepareServiceStateEvent_l(sound_trigger_service_state_t state); |
| 187 | void sendServiceStateEvent_l(sound_trigger_service_state_t state, Module *module); |
| 188 | |
| 189 | void sendCallbackEvent_l(const sp<CallbackEvent>& event); |
| 190 | void onCallbackEvent(const sp<CallbackEvent>& event); |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 191 | |
| 192 | private: |
| 193 | |
| 194 | virtual void onFirstRef(); |
| 195 | |
| 196 | Mutex mServiceLock; |
| 197 | volatile int32_t mNextUniqueId; |
| 198 | DefaultKeyedVector< sound_trigger_module_handle_t, sp<Module> > mModules; |
| 199 | sp<CallbackThread> mCallbackThread; |
Eric Laurent | df3dc7e | 2014-07-27 18:39:40 -0700 | [diff] [blame^] | 200 | sp<MemoryDealer> mMemoryDealer; |
| 201 | bool mCaptureState; |
Eric Laurent | b7a11d8 | 2014-04-18 17:40:41 -0700 | [diff] [blame] | 202 | }; |
| 203 | |
| 204 | } // namespace android |
| 205 | |
| 206 | #endif // ANDROID_HARDWARE_SOUNDTRIGGER_HAL_SERVICE_H |