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 #include "adapter_host_manager.h"
16 #include "engine_host_manager.h"
17
18 #include "intell_voice_log.h"
19 #include "intell_voice_util.h"
20
21 #define LOG_TAG "AdapterHostManager"
22
23 using namespace OHOS::IntellVoiceUtils;
24
25 namespace OHOS {
26 namespace IntellVoiceEngine {
~AdapterHostManager()27 AdapterHostManager::~AdapterHostManager()
28 {
29 adapterProxy1_0_ = nullptr;
30 adapterProxy1_2_ = nullptr;
31 }
32
Init(const IntellVoiceEngineAdapterDescriptor & desc)33 bool AdapterHostManager::Init(const IntellVoiceEngineAdapterDescriptor &desc)
34 {
35 INTELL_VOICE_LOG_INFO("enter");
36 const auto &engineHostProxy1_0 = EngineHostManager::GetInstance().GetEngineHostProxy1_0();
37 const auto &engineHostProxy1_2 = EngineHostManager::GetInstance().GetEngineHostProxy1_2();
38 CHECK_CONDITION_RETURN_FALSE(engineHostProxy1_0 == nullptr, "engine host proxy_v1_0 is null");
39
40 if (engineHostProxy1_2 != nullptr) {
41 engineHostProxy1_2->CreateAdapter_V_2(desc, adapterProxy1_2_);
42 if (adapterProxy1_2_ == nullptr) {
43 INTELL_VOICE_LOG_ERROR("engine adapter proxy1_2_ is nullptr");
44 return false;
45 }
46
47 adapterProxy1_0_ = adapterProxy1_2_;
48 return true;
49 }
50
51 engineHostProxy1_0->CreateAdapter(desc, adapterProxy1_0_);
52 if (adapterProxy1_0_ == nullptr) {
53 INTELL_VOICE_LOG_ERROR("engine adapter proxy1_0_ is nullptr");
54 return false;
55 }
56 return true;
57 }
58
SetCallback(const sptr<IIntellVoiceEngineCallback> & engineCallback)59 int32_t AdapterHostManager::SetCallback(const sptr<IIntellVoiceEngineCallback> &engineCallback)
60 {
61 CHECK_CONDITION_RETURN_RET(adapterProxy1_0_ == nullptr, -1, "adapter proxy is null");
62 return adapterProxy1_0_->SetCallback(engineCallback);
63 }
64
Attach(const OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineAdapterInfo & info)65 int32_t AdapterHostManager::Attach(const OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineAdapterInfo &info)
66 {
67 CHECK_CONDITION_RETURN_RET(adapterProxy1_0_ == nullptr, -1, "adapter proxy is null");
68 return adapterProxy1_0_->Attach(info);
69 }
70
Detach()71 int32_t AdapterHostManager::Detach()
72 {
73 CHECK_CONDITION_RETURN_RET(adapterProxy1_0_ == nullptr, -1, "adapter proxy is null");
74 return adapterProxy1_0_->Detach();
75 }
76
SetParameter(const std::string & keyValueList)77 int32_t AdapterHostManager::SetParameter(const std::string &keyValueList)
78 {
79 CHECK_CONDITION_RETURN_RET(adapterProxy1_0_ == nullptr, -1, "adapter proxy is null");
80 return adapterProxy1_0_->SetParameter(keyValueList);
81 }
82
GetParameter(const std::string & keyList,std::string & valueList)83 int32_t AdapterHostManager::GetParameter(const std::string &keyList, std::string &valueList)
84 {
85 CHECK_CONDITION_RETURN_RET(adapterProxy1_0_ == nullptr, -1, "adapter proxy is null");
86 return adapterProxy1_0_->GetParameter(keyList, valueList);
87 }
88
Start(const OHOS::HDI::IntelligentVoice::Engine::V1_0::StartInfo & info)89 int32_t AdapterHostManager::Start(const OHOS::HDI::IntelligentVoice::Engine::V1_0::StartInfo &info)
90 {
91 CHECK_CONDITION_RETURN_RET(adapterProxy1_0_ == nullptr, -1, "adapter proxy is null");
92 return adapterProxy1_0_->Start(info);
93 }
94
Stop()95 int32_t AdapterHostManager::Stop()
96 {
97 CHECK_CONDITION_RETURN_RET(adapterProxy1_0_ == nullptr, -1, "adapter proxy is null");
98 return adapterProxy1_0_->Stop();
99 }
100
WriteAudio(const std::vector<uint8_t> & buffer)101 int32_t AdapterHostManager::WriteAudio(const std::vector<uint8_t> &buffer)
102 {
103 CHECK_CONDITION_RETURN_RET(adapterProxy1_0_ == nullptr, -1, "adapter proxy is null");
104 return adapterProxy1_0_->WriteAudio(buffer);
105 }
106
Read(OHOS::HDI::IntelligentVoice::Engine::V1_0::ContentType type,sptr<Ashmem> & buffer)107 int32_t AdapterHostManager::Read(OHOS::HDI::IntelligentVoice::Engine::V1_0::ContentType type, sptr<Ashmem> &buffer)
108 {
109 CHECK_CONDITION_RETURN_RET(adapterProxy1_0_ == nullptr, -1, "adapter proxy is null");
110 return adapterProxy1_0_->Read(type, buffer);
111 }
112
GetWakeupPcm(std::vector<uint8_t> & data)113 int32_t AdapterHostManager::GetWakeupPcm(std::vector<uint8_t> &data)
114 {
115 CHECK_CONDITION_RETURN_RET(adapterProxy1_2_ == nullptr, -1, "adapter proxy is null");
116 if (adapterProxy1_2_ == nullptr) {
117 return -1;
118 }
119
120 return adapterProxy1_2_->GetWakeupPcm(data);
121 }
122
Evaluate(const std::string & word,EvaluationResultInfo & info)123 int32_t AdapterHostManager::Evaluate(const std::string &word, EvaluationResultInfo &info)
124 {
125 CHECK_CONDITION_RETURN_RET(adapterProxy1_2_ == nullptr, -1, "adapter proxy is null");
126 if (adapterProxy1_2_ == nullptr) {
127 return -1;
128 }
129 return adapterProxy1_2_->Evaluate(word, info);
130 }
131 }
132 }
133