• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "intell_voice_engine_adapter_impl.h"
16 
17 #include <cinttypes>
18 #include <memory>
19 #include <vector>
20 
21 #include "hdf_base.h"
22 #include "securec.h"
23 #include "intell_voice_log.h"
24 #include "scope_guard.h"
25 
26 #undef HDF_LOG_TAG
27 #define HDF_LOG_TAG "IntellVoiceEngineAdapterImpl"
28 
29 namespace OHOS {
30 namespace IntelligentVoice {
31 namespace Engine {
IntellVoiceEngineAdapterImpl(std::unique_ptr<IEngine> engine)32 IntellVoiceEngineAdapterImpl::IntellVoiceEngineAdapterImpl(std::unique_ptr<IEngine> engine)
33     : engine_(std::move(engine))
34 {}
35 
~IntellVoiceEngineAdapterImpl()36 IntellVoiceEngineAdapterImpl::~IntellVoiceEngineAdapterImpl()
37 {
38     engine_ = nullptr;
39 }
40 
SetCallback(const sptr<IIntellVoiceEngineCallback> & engineCallback)41 int32_t IntellVoiceEngineAdapterImpl::SetCallback(const sptr<IIntellVoiceEngineCallback> &engineCallback)
42 {
43     if (engineCallback == nullptr) {
44         INTELLIGENT_VOICE_LOGE("callback is nullptr");
45         return HDF_ERR_MALLOC_FAIL;
46     }
47 
48     std::shared_ptr<EngineListener> listener = std::make_shared<EngineListener>(engineCallback);
49     if (listener == nullptr) {
50         INTELLIGENT_VOICE_LOGE("listener is nullptr");
51         return HDF_ERR_MALLOC_FAIL;
52     }
53 
54     if (engine_->SetListener(listener) != 0) {
55         INTELLIGENT_VOICE_LOGE("failed to set listener");
56         return HDF_FAILURE;
57     }
58     return HDF_SUCCESS;
59 }
60 
Attach(const IntellVoiceEngineAdapterInfo & info)61 int32_t IntellVoiceEngineAdapterImpl::Attach(const IntellVoiceEngineAdapterInfo &info)
62 {
63     INTELLIGENT_VOICE_LOGD("Attach enter");
64     return engine_->Init(info);
65 }
66 
Detach()67 int32_t IntellVoiceEngineAdapterImpl::Detach()
68 {
69     INTELLIGENT_VOICE_LOGD("Detach enter");
70     return engine_->Release();
71 }
72 
SetParameter(const std::string & keyValueList)73 int32_t IntellVoiceEngineAdapterImpl::SetParameter(const std::string &keyValueList)
74 {
75     INTELLIGENT_VOICE_LOGD("SetParameter enter");
76     return engine_->SetParameter(keyValueList);
77 }
78 
GetParameter(const std::string & keyList,std::string & valueList)79 int32_t IntellVoiceEngineAdapterImpl::GetParameter(const std::string &keyList, std::string &valueList)
80 {
81     INTELLIGENT_VOICE_LOGD("GetParameter enter");
82     return engine_->GetParameter(keyList, [&](const std::string &retStr) { valueList = retStr; });
83 }
84 
Start(const StartInfo & info)85 int32_t IntellVoiceEngineAdapterImpl::Start(const StartInfo &info)
86 {
87     INTELLIGENT_VOICE_LOGD("Start enter");
88     return engine_->Start(info);
89 }
90 
Stop()91 int32_t IntellVoiceEngineAdapterImpl::Stop()
92 {
93     INTELLIGENT_VOICE_LOGD("Stop enter");
94     return engine_->Stop();
95 }
96 
WriteAudio(const std::vector<uint8_t> & buffer)97 int32_t IntellVoiceEngineAdapterImpl::WriteAudio(const std::vector<uint8_t> &buffer)
98 {
99     return engine_->Write(buffer.data(), buffer.size());
100 }
101 
Evaluate(const std::string & word,EvaluationResultInfo & info)102 int32_t IntellVoiceEngineAdapterImpl::Evaluate(const std::string &word, EvaluationResultInfo &info)
103 {
104     return engine_->Evaluate(word, info);
105 }
106 
Read(ContentType type,sptr<Ashmem> & buffer)107 int32_t IntellVoiceEngineAdapterImpl::Read(ContentType type, sptr<Ashmem> &buffer)
108 {
109     INTELLIGENT_VOICE_LOGD("enter");
110     uint8_t *tmp = nullptr;
111     uint32_t size = 0;
112 
113     ReadFileDataInner(type, tmp, size);
114     if (tmp == nullptr) {
115         INTELLIGENT_VOICE_LOGE("tmp buffer is nullptr");
116         return HDF_ERR_INVALID_OBJECT;
117     }
118 
119     ON_SCOPE_EXIT_WITH_NAME(bufferExit)
120     {
121         INTELLIGENT_VOICE_LOGI("now delete buffer");
122         delete[] tmp;
123         tmp = nullptr;
124     };
125 
126     if (size == 0) {
127         INTELLIGENT_VOICE_LOGE("size(%{public}u) is invalid", size);
128         return HDF_ERR_INVALID_OBJECT;
129     }
130 
131     buffer = OHOS::Ashmem::CreateAshmem("ReadContent", size);
132     if (buffer == nullptr) {
133         INTELLIGENT_VOICE_LOGE("ashmem buffer is nullptr, size:%{public}u", size);
134         return HDF_ERR_INVALID_OBJECT;
135     }
136 
137     if (!buffer->MapReadAndWriteAshmem()) {
138         INTELLIGENT_VOICE_LOGE("failed to map and write ashmem");
139         goto ERROR_EXIT;
140     }
141 
142     if (!buffer->WriteToAshmem(tmp, size, 0)) {
143         INTELLIGENT_VOICE_LOGE("failed to write to ashmem");
144         goto ERROR_EXIT;
145     }
146     return HDF_SUCCESS;
147 
148 ERROR_EXIT:
149     buffer->UnmapAshmem();
150     buffer->CloseAshmem();
151     buffer = nullptr;
152     return HDF_FAILURE;
153 }
154 
ReadFileDataInner(ContentType type,uint8_t * & buffer,uint32_t & size)155 int32_t IntellVoiceEngineAdapterImpl::ReadFileDataInner(ContentType type, uint8_t *&buffer, uint32_t &size)
156 {
157     INTELLIGENT_VOICE_LOGD("enter");
158     return engine_->ReadFileData(type, [&](std::shared_ptr<uint8_t> fileData, uint32_t fileSize) {
159         buffer = new (std::nothrow) uint8_t[fileSize];
160         if (buffer == nullptr) {
161             INTELLIGENT_VOICE_LOGE("buffer is nullptr");
162             return;
163         }
164         size = fileSize;
165         (void)memcpy_s(buffer, size, fileData.get(), fileSize);
166     });
167 }
168 
169 
GetWakeupPcm(std::vector<uint8_t> & data)170 int32_t IntellVoiceEngineAdapterImpl::GetWakeupPcm(std::vector<uint8_t> &data)
171 {
172     return engine_->GetWakeupPcm(data);
173 }
174 
EngineListener(const sptr<IIntellVoiceEngineCallback> & cb)175 EngineListener::EngineListener(const sptr<IIntellVoiceEngineCallback> &cb) : cb_(cb)
176 {
177 }
178 
~EngineListener()179 EngineListener::~EngineListener()
180 {
181     cb_ = nullptr;
182 }
183 
OnIntellVoiceEvent(const IntellVoiceEngineCallBackEvent & event)184 void EngineListener::OnIntellVoiceEvent(const IntellVoiceEngineCallBackEvent &event)
185 {
186     if (cb_ == nullptr) {
187         INTELLIGENT_VOICE_LOGE("cb_ is nullptr");
188         return;
189     }
190 
191     cb_->OnIntellVoiceHdiEvent(event);
192 }
193 }
194 }
195 }
196