1 /* 2 * Copyright (c) 2024 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_UTIL_H 16 #define ENGINE_UTIL_H 17 #include <memory> 18 #include <mutex> 19 #include <string> 20 #include <map> 21 #include <vector> 22 #include <ashmem.h> 23 #include "i_adapter_host_manager.h" 24 #include <i_intell_voice_engine_callback.h> 25 #include "v1_0/iintell_voice_engine_adapter.h" 26 #include "v1_0/iintell_voice_engine_callback.h" 27 #include "audio_system_manager.h" 28 29 namespace OHOS { 30 namespace IntellVoiceEngine { 31 using OHOS::HDI::IntelligentVoice::Engine::V1_0::IIntellVoiceEngineCallback; 32 33 enum EngineEvent { 34 NONE = 0, 35 INIT, 36 INIT_DONE, 37 START_RECOGNIZE, 38 STOP_RECOGNIZE, 39 RECOGNIZE_COMPLETE, 40 START_CAPTURER, 41 READ, 42 STOP_CAPTURER, 43 RECOGNIZING_TIMEOUT, 44 RECOGNIZE_COMPLETE_TIMEOUT, 45 READ_CAPTURER_TIMEOUT, 46 SET_LISTENER, 47 SET_PARAM, 48 GET_PARAM, 49 GET_WAKEUP_PCM, 50 RELEASE_ADAPTER, 51 RESET_ADAPTER, 52 RELEASE, 53 }; 54 55 struct SetListenerMsg { SetListenerMsgSetListenerMsg56 explicit SetListenerMsg(sptr<IIntelligentVoiceEngineCallback> cb) : callback(cb) {} 57 sptr<IIntelligentVoiceEngineCallback> callback = nullptr; 58 }; 59 60 struct CapturerData { 61 std::vector<uint8_t> data; 62 }; 63 64 struct StringParam { strParamStringParam65 explicit StringParam(const std::string &str = "") : strParam(str) {} 66 std::string strParam; 67 }; 68 69 class EngineUtil { 70 public: 71 EngineUtil(); 72 ~EngineUtil() = default; CreateAdapterInner(T & mgr,OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineAdapterType type)73 template<typename T> bool CreateAdapterInner(T &mgr, 74 OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineAdapterType type) 75 { 76 desc_.adapterType = type; 77 adapter_ = mgr.CreateEngineAdapter(desc_); 78 if (adapter_ == nullptr) { 79 return false; 80 } 81 return true; 82 } ReleaseAdapterInner(T & mgr)83 template<typename T> void ReleaseAdapterInner(T &mgr) 84 { 85 mgr.ReleaseEngineAdapter(desc_); 86 adapter_ = nullptr; 87 } 88 int32_t SetParameter(const std::string &keyValueList); 89 std::string GetParameter(const std::string &key); 90 int32_t WriteAudio(const uint8_t *buffer, uint32_t size); 91 int32_t Stop(); 92 int32_t GetWakeupPcm(std::vector<uint8_t> &data); 93 int32_t Evaluate(const std::string &word, EvaluationResultInfo &info); 94 bool SetDspFeatures(); 95 std::vector<uint8_t> ReadDspModel(OHOS::HDI::IntelligentVoice::Engine::V1_0::ContentType type); 96 void ProcDspModel(const std::vector<uint8_t> &buffer); 97 void SetLanguage(); 98 void SetArea(); 99 void SetSensibility(); 100 void SelectInputDevice(AudioStandard::DeviceType type); 101 void SetScreenStatus(); 102 103 protected: 104 std::shared_ptr<IAdapterHostManager> adapter_ = nullptr; 105 OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineAdapterDescriptor desc_; 106 107 private: 108 static void WriteBufferFromAshmem(uint8_t *&buffer, uint32_t size, sptr<OHOS::Ashmem> ashmem); 109 }; 110 } 111 } 112 #endif