1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef SERVICE_MANAGER_H 16 #define SERVICE_MANAGER_H 17 #include <mutex> 18 #include <map> 19 #include <atomic> 20 #include "engine_base.h" 21 #include "trigger_detector.h" 22 #include "switch_observer.h" 23 #include "switch_provider.h" 24 #include "history_info_mgr.h" 25 #include "intell_voice_engine_arbitration.h" 26 #include "intell_voice_death_recipient.h" 27 #include "update_engine_controller.h" 28 #include "data_operation_callback.h" 29 #include "i_intell_voice_update_callback.h" 30 #include "task_executor.h" 31 #include "engine_util.h" 32 33 namespace OHOS { 34 namespace IntellVoiceEngine { 35 constexpr int32_t VOICE_WAKEUP_MODEL_UUID = 1; 36 constexpr int32_t PROXIMAL_WAKEUP_MODEL_UUID = 2; 37 const std::string WAKEUP_KEY = "intell_voice_trigger_enabled"; 38 const std::string WHISPER_KEY = "intell_voice_trigger_whisper"; 39 const std::string IMPROVE_KEY = "intell_voice_improve_enabled"; 40 const std::string SHORTWORD_KEY = "intell_voice_trigger_shortword"; 41 42 class IntellVoiceServiceManager : private IntellVoiceEngineArbitration, 43 private UpdateEngineController, 44 private OHOS::IntellVoiceUtils::TaskExecutor { 45 public: 46 ~IntellVoiceServiceManager(); 47 static std::unique_ptr<IntellVoiceServiceManager> &GetInstance(); SetEnrollResult(IntellVoiceEngineType type,bool result)48 static void SetEnrollResult(IntellVoiceEngineType type, bool result) 49 { 50 if ((type < INTELL_VOICE_ENROLL) || (type >= ENGINE_TYPE_BUT)) { 51 return; 52 } 53 54 g_enrollResult[type].store(result); 55 } GetEnrollResult(IntellVoiceEngineType type)56 static bool GetEnrollResult(IntellVoiceEngineType type) 57 { 58 if ((type < INTELL_VOICE_ENROLL) || (type >= ENGINE_TYPE_BUT)) { 59 return false; 60 } 61 62 return g_enrollResult[type].load(); 63 } 64 65 sptr<IIntellVoiceEngine> HandleCreateEngine(IntellVoiceEngineType type); 66 int32_t HandleReleaseEngine(IntellVoiceEngineType type); 67 void HandleSilenceUpdate(); 68 int32_t HandleCloneUpdate(const std::string &wakeupInfo, const sptr<IRemoteObject> &object); 69 void HandleSwitchOn(bool isAsync, int32_t uuid, bool needUpdateAdapter); 70 void HandleSwitchOff(bool isAsync, int32_t uuid); 71 void HandleCloseWakeupSource(); 72 void HandleUnloadIntellVoiceService(bool isAsync); 73 bool HandleOnIdle(); 74 void HandleServiceStop(); 75 void HandleHeadsetHostDie(); 76 77 void ProcBreathModel(); 78 void CreateSwitchProvider(); 79 void ReleaseSwitchProvider(); 80 bool StartDetection(int32_t uuid); 81 void StopDetection(int32_t uuid); 82 bool QuerySwitchStatus(const std::string &key); 83 84 bool RegisterProxyDeathRecipient(IntellVoiceEngineType type, const sptr<IRemoteObject> &object); 85 bool DeregisterProxyDeathRecipient(IntellVoiceEngineType type); 86 87 using UpdateEngineController::OnUpdateComplete; 88 using TaskExecutor::AddAsyncTask; 89 using TaskExecutor::AddSyncTask; 90 using TaskExecutor::StopThread; 91 92 int32_t SetParameter(const std::string &keyValueList); 93 std::string GetParameter(const std::string &key); 94 std::string GetWakeupCapability(); 95 int32_t GetWakeupSourceFilesList(std::vector<std::string>& cloneFiles); 96 int32_t GetWakeupSourceFile(const std::string &filePath, std::vector<uint8_t> &buffer); 97 int32_t SendWakeupFile(const std::string &filePath, const std::vector<uint8_t> &buffer); 98 int32_t ClearUserData(); 99 void SetDspSensibility(const std::string &sensibility); 100 void OnTriggerConnectServiceStart(); 101 102 private: 103 IntellVoiceServiceManager(); IsSwitchKeyValid(const std::string & key)104 static bool IsSwitchKeyValid(const std::string &key) 105 { 106 if ((key == WAKEUP_KEY) || (key == WHISPER_KEY) || (key == IMPROVE_KEY) || (key == SHORTWORD_KEY)) { 107 return true; 108 } 109 return false; 110 } 111 QuerySwitchByUuid(int32_t uuid)112 bool QuerySwitchByUuid(int32_t uuid) 113 { 114 if (uuid == VOICE_WAKEUP_MODEL_UUID) { 115 return QuerySwitchStatus(WAKEUP_KEY); 116 } 117 118 if (uuid == PROXIMAL_WAKEUP_MODEL_UUID) { 119 return QuerySwitchStatus(WHISPER_KEY); 120 } 121 122 return false; 123 } 124 sptr<IIntellVoiceEngine> CreateEngine(IntellVoiceEngineType type, const std::string ¶m = ""); 125 int32_t ReleaseEngine(IntellVoiceEngineType type); 126 void OnSwitchChange(const std::string &switchKey); 127 void OnDetected(int32_t uuid); 128 void CreateDetector(int32_t uuid); 129 130 sptr<IIntellVoiceEngine> CreateEngineInner(IntellVoiceEngineType type, const std::string ¶m = ""); 131 int32_t ReleaseEngineInner(IntellVoiceEngineType type); 132 bool CreateOrResetWakeupEngine(); 133 bool IsEngineExist(IntellVoiceEngineType type); 134 int32_t ServiceStopProc(); 135 136 void ReleaseUpdateEngine() override; 137 bool CreateUpdateEngine(const std::string ¶m) override; 138 void HandleUpdateComplete(UpdateState result, const std::string ¶m) override; 139 void HandleUpdateRetry() override; 140 int32_t SilenceUpdate(); 141 int32_t CloneUpdate(const std::string &wakeupInfo, const sptr<IRemoteObject> &object); 142 void RegisterObserver(const std::string &switchKey); 143 void SetImproveParam(sptr<EngineBase> engine); 144 void CreateAndStartServiceObject(int32_t uuid, bool needResetAdapter); 145 void ReleaseServiceObject(int32_t uuid); 146 int32_t SwitchOnProc(int32_t uuid, bool needUpdateAdapter); 147 int32_t SwitchOffProc(int32_t uuid); 148 bool IsNeedToUnloadService(); 149 int32_t UnloadIntellVoiceService(); 150 151 private: 152 static std::unique_ptr<IntellVoiceServiceManager> g_intellVoiceServiceMgr; 153 static std::atomic<bool> g_enrollResult[ENGINE_TYPE_BUT]; 154 static std::vector<int32_t> g_defaultDspSentenceThresholds; 155 std::mutex deathMutex_; 156 std::mutex detectorMutex_; 157 std::mutex switchMutex_; 158 std::map<IntellVoiceEngineType, sptr<EngineBase>> engines_; 159 std::map<int32_t, std::shared_ptr<IntellVoiceTrigger::TriggerDetector>> detector_; 160 std::map<const std::string, sptr<SwitchObserver>> switchObserver_; 161 IntellVoiceUtils::UniqueProductType<SwitchProvider> switchProvider_ = 162 IntellVoiceUtils::UniqueProductType<SwitchProvider> {nullptr, nullptr}; 163 std::map<IntellVoiceEngineType, sptr<IntellVoiceUtils::IntellVoiceDeathRecipient>> proxyDeathRecipient_; 164 std::map<IntellVoiceEngineType, sptr<IRemoteObject>> deathRecipientObj_; 165 }; 166 } // namespace IntellVoice 167 } // namespace OHOS 168 #endif 169