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 ENGINE_MANAGER_H 16 #define ENGINE_MANAGER_H 17 #include <mutex> 18 #include <map> 19 #include <atomic> 20 #include "engine_base.h" 21 #include "intell_voice_engine_arbitration.h" 22 #include "intell_voice_death_recipient.h" 23 #include "update_engine_controller.h" 24 #include "task_executor.h" 25 #include "intell_voice_definitions.h" 26 #include "i_intell_voice_service.h" 27 28 namespace OHOS { 29 namespace IntellVoiceEngine { 30 class IntellVoiceEngineManager : private IntellVoiceEngineArbitration, 31 private UpdateEngineController { 32 public: 33 IntellVoiceEngineManager(); 34 ~IntellVoiceEngineManager(); 35 static bool GetScreenOff(); 36 void SetScreenOff(bool value); SetEnrollResult(IntellVoiceEngineType type,bool result)37 static void SetEnrollResult(IntellVoiceEngineType type, bool result) 38 { 39 if ((type < INTELL_VOICE_ENROLL) || (type >= ENGINE_TYPE_BUT)) { 40 return; 41 } 42 43 g_enrollResult[type].store(result); 44 } GetEnrollResult(IntellVoiceEngineType type)45 static bool GetEnrollResult(IntellVoiceEngineType type) 46 { 47 if ((type < INTELL_VOICE_ENROLL) || (type >= ENGINE_TYPE_BUT)) { 48 return false; 49 } 50 51 return g_enrollResult[type].load(); 52 } 53 54 bool AnyEngineExist(const std::vector<IntellVoiceEngineType> &types); 55 bool RegisterProxyDeathRecipient(IntellVoiceEngineType type, const sptr<IRemoteObject> &object); 56 bool DeregisterProxyDeathRecipient(IntellVoiceEngineType type); 57 58 int32_t GetUploadFiles(int numMax, std::vector<UploadFilesFromHdi> &files); 59 int32_t SetParameter(const std::string &sensibility); 60 std::string GetParameter(const std::string &key); 61 int32_t GetWakeupSourceFilesList(std::vector<std::string> &cloneFiles); 62 int32_t GetWakeupSourceFile(const std::string &filePath, std::vector<uint8_t> &buffer); 63 int32_t SendWakeupFile(const std::string &filePath, const std::vector<uint8_t> &buffer); 64 std::string GetDspSensibility(const std::string &sensibility, const std::string &dspFeature, 65 const std::string &configPath); 66 67 void ClearUserDataInner(); 68 void HeadsetHostDie(); 69 bool IsNeedUpdateComplete(int32_t result, const std::string ¶m); 70 bool IsNeedUpdateRetry(); 71 void EngineOnDetected(int32_t uuid); 72 void ClearWakeupEngineCb(); 73 bool CreateOrResetWakeupEngine(); 74 bool IsEngineExist(IntellVoiceEngineType type); 75 sptr<IIntellVoiceEngine> CreateEngine(IntellVoiceEngineType type, 76 const std::string ¶m = "", bool reEnroll = false); 77 int32_t ReleaseEngineInner(IntellVoiceEngineType type); 78 int32_t SilenceUpdate(); 79 int32_t WhisperVprUpdate(bool reEnroll = false); 80 int32_t CloneUpdate(const std::string &wakeupInfo, const sptr<IRemoteObject> &object); 81 int32_t ServiceStopProc(); 82 void SetDspSensibility(const std::string &sensibility); 83 void OnServiceStart(); 84 void OnServiceStop(); 85 86 private: 87 88 sptr<IIntellVoiceEngine> CreateEngineInner(IntellVoiceEngineType type, 89 const std::string ¶m = "", bool reEnroll = false); 90 void ReleaseUpdateEngine() override; 91 bool CreateUpdateEngine(const std::string ¶m, bool reEnroll = false) override; 92 void LoadIntellVoiceHost(); 93 void UnloadIntellVoiceHost(); 94 95 private: 96 static std::mutex instanceMutex_; 97 static std::shared_ptr<IntellVoiceEngineManager> instance_; 98 static std::atomic<bool> g_enrollResult[ENGINE_TYPE_BUT]; 99 static std::atomic<bool> screenoff_; 100 std::mutex deathMutex_; 101 std::map<IntellVoiceEngineType, sptr<EngineBase>> engines_; 102 std::map<IntellVoiceEngineType, sptr<IntellVoiceUtils::IntellVoiceDeathRecipient>> proxyDeathRecipient_; 103 std::map<IntellVoiceEngineType, sptr<IRemoteObject>> deathRecipientObj_; 104 }; 105 } // namespace IntellVoice 106 } // namespace OHOS 107 #endif 108