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