Home | History | Annotate | Download | only in soundtrigger
      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 "SoundTriggerHalInterface.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     class ModuleClient;
     43 
     44     static char const* getServiceName() { return "media.sound_trigger_hw"; }
     45 
     46                         SoundTriggerHwService();
     47     virtual             ~SoundTriggerHwService();
     48 
     49     // ISoundTriggerHwService
     50     virtual status_t listModules(const String16& opPackageName,
     51                                  struct sound_trigger_module_descriptor *modules,
     52                                  uint32_t *numModules);
     53 
     54     virtual status_t attach(const String16& opPackageName,
     55                             const sound_trigger_module_handle_t handle,
     56                             const sp<ISoundTriggerClient>& client,
     57                             sp<ISoundTrigger>& module);
     58 
     59     virtual status_t setCaptureState(bool active);
     60 
     61     virtual status_t    onTransact(uint32_t code, const Parcel& data,
     62                                    Parcel* reply, uint32_t flags);
     63 
     64     virtual status_t    dump(int fd, const Vector<String16>& args);
     65 
     66     class Model : public RefBase {
     67      public:
     68 
     69         enum {
     70             STATE_IDLE,
     71             STATE_ACTIVE
     72         };
     73 
     74         Model(sound_model_handle_t handle, audio_session_t session, audio_io_handle_t ioHandle,
     75               audio_devices_t device, sound_trigger_sound_model_type_t type,
     76               sp<ModuleClient>& moduleClient);
     77         ~Model() {}
     78 
     79         sound_model_handle_t    mHandle;
     80         int                     mState;
     81         audio_session_t         mCaptureSession;
     82         audio_io_handle_t       mCaptureIOHandle;
     83         audio_devices_t         mCaptureDevice;
     84         sound_trigger_sound_model_type_t mType;
     85         struct sound_trigger_recognition_config mConfig;
     86         sp<ModuleClient>        mModuleClient;
     87     };
     88 
     89     class CallbackEvent : public RefBase {
     90     public:
     91         typedef enum {
     92             TYPE_RECOGNITION,
     93             TYPE_SOUNDMODEL,
     94             TYPE_SERVICE_STATE,
     95         } event_type;
     96         CallbackEvent(event_type type, sp<IMemory> memory);
     97 
     98         virtual             ~CallbackEvent();
     99 
    100         void setModule(wp<Module> module) { mModule = module; }
    101         void setModuleClient(wp<ModuleClient> moduleClient) { mModuleClient = moduleClient; }
    102 
    103         event_type mType;
    104         sp<IMemory> mMemory;
    105         wp<Module> mModule;
    106         wp<ModuleClient> mModuleClient;
    107     };
    108 
    109     class Module : public RefBase {
    110     public:
    111 
    112        Module(const sp<SoundTriggerHwService>& service,
    113               const sp<SoundTriggerHalInterface>& halInterface,
    114               sound_trigger_module_descriptor descriptor);
    115 
    116        virtual ~Module();
    117 
    118        virtual status_t loadSoundModel(const sp<IMemory>& modelMemory,
    119                                        sp<ModuleClient> moduleClient,
    120                                        sound_model_handle_t *handle);
    121 
    122        virtual status_t unloadSoundModel(sound_model_handle_t handle);
    123 
    124        virtual status_t startRecognition(sound_model_handle_t handle,
    125                                          const sp<IMemory>& dataMemory);
    126        virtual status_t stopRecognition(sound_model_handle_t handle);
    127        virtual status_t getModelState(sound_model_handle_t handle);
    128 
    129        sp<SoundTriggerHalInterface> halInterface() const { return mHalInterface; }
    130        struct sound_trigger_module_descriptor descriptor() { return mDescriptor; }
    131        wp<SoundTriggerHwService> service() const { return mService; }
    132        bool isConcurrentCaptureAllowed() const { return mDescriptor.properties.concurrent_capture; }
    133 
    134        sp<Model> getModel(sound_model_handle_t handle);
    135 
    136        void setCaptureState_l(bool active);
    137 
    138        sp<ModuleClient> addClient(const sp<ISoundTriggerClient>& client,
    139                                   const String16& opPackageName);
    140 
    141        void detach(const sp<ModuleClient>& moduleClient);
    142 
    143        void onCallbackEvent(const sp<CallbackEvent>& event);
    144 
    145     private:
    146 
    147         Mutex                                  mLock;
    148         wp<SoundTriggerHwService>              mService;
    149         sp<SoundTriggerHalInterface>           mHalInterface;
    150         struct sound_trigger_module_descriptor mDescriptor;
    151         Vector< sp<ModuleClient> >             mModuleClients;
    152         DefaultKeyedVector< sound_model_handle_t, sp<Model> >     mModels;
    153         sound_trigger_service_state_t          mServiceState;
    154     }; // class Module
    155 
    156     class ModuleClient : public virtual RefBase,
    157                          public BnSoundTrigger,
    158                          public IBinder::DeathRecipient {
    159     public:
    160 
    161        ModuleClient(const sp<Module>& module,
    162               const sp<ISoundTriggerClient>& client,
    163               const String16& opPackageName);
    164 
    165        virtual ~ModuleClient();
    166 
    167        virtual void detach();
    168 
    169        virtual status_t loadSoundModel(const sp<IMemory>& modelMemory,
    170                                        sound_model_handle_t *handle);
    171 
    172        virtual status_t unloadSoundModel(sound_model_handle_t handle);
    173 
    174        virtual status_t startRecognition(sound_model_handle_t handle,
    175                                          const sp<IMemory>& dataMemory);
    176        virtual status_t stopRecognition(sound_model_handle_t handle);
    177        virtual status_t getModelState(sound_model_handle_t handle);
    178 
    179        virtual status_t dump(int fd, const Vector<String16>& args);
    180 
    181        virtual void onFirstRef();
    182 
    183        // IBinder::DeathRecipient implementation
    184        virtual void        binderDied(const wp<IBinder> &who);
    185 
    186        void onCallbackEvent(const sp<CallbackEvent>& event);
    187 
    188        void setCaptureState_l(bool active);
    189 
    190        sp<ISoundTriggerClient> client() const { return mClient; }
    191 
    192     private:
    193 
    194         mutable Mutex               mLock;
    195         wp<Module>                  mModule;
    196         sp<ISoundTriggerClient>     mClient;
    197         const String16              mOpPackageName;
    198     }; // class ModuleClient
    199 
    200     class CallbackThread : public Thread {
    201     public:
    202 
    203         explicit CallbackThread(const wp<SoundTriggerHwService>& service);
    204 
    205         virtual             ~CallbackThread();
    206 
    207         // Thread virtuals
    208         virtual bool        threadLoop();
    209 
    210         // RefBase
    211         virtual void        onFirstRef();
    212 
    213                 void        exit();
    214                 void        sendCallbackEvent(const sp<CallbackEvent>& event);
    215 
    216     private:
    217         wp<SoundTriggerHwService>   mService;
    218         Condition                   mCallbackCond;
    219         Mutex                       mCallbackLock;
    220         Vector< sp<CallbackEvent> > mEventQueue;
    221     };
    222 
    223     static void recognitionCallback(struct sound_trigger_recognition_event *event, void *cookie);
    224            sp<IMemory> prepareRecognitionEvent(struct sound_trigger_recognition_event *event);
    225            void sendRecognitionEvent(struct sound_trigger_recognition_event *event, Module *module);
    226 
    227     static void soundModelCallback(struct sound_trigger_model_event *event, void *cookie);
    228            sp<IMemory> prepareSoundModelEvent(struct sound_trigger_model_event *event);
    229            void sendSoundModelEvent(struct sound_trigger_model_event *event, Module *module);
    230 
    231            sp<IMemory> prepareServiceStateEvent(sound_trigger_service_state_t state);
    232            void sendServiceStateEvent(sound_trigger_service_state_t state, Module *module);
    233            void sendServiceStateEvent(sound_trigger_service_state_t state,
    234                                       ModuleClient *moduleClient);
    235 
    236            void sendCallbackEvent(const sp<CallbackEvent>& event);
    237            void onCallbackEvent(const sp<CallbackEvent>& event);
    238 
    239 private:
    240 
    241     virtual void onFirstRef();
    242 
    243     Mutex               mServiceLock;
    244     volatile int32_t    mNextUniqueId;
    245     DefaultKeyedVector< sound_trigger_module_handle_t, sp<Module> >     mModules;
    246     sp<CallbackThread>  mCallbackThread;
    247     sp<MemoryDealer>    mMemoryDealer;
    248     Mutex               mMemoryDealerLock;
    249     bool                mCaptureState;
    250 };
    251 
    252 } // namespace android
    253 
    254 #endif // ANDROID_HARDWARE_SOUNDTRIGGER_HAL_SERVICE_H
    255