• 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 "intell_voice_log.h"
23 #include "securec.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 
Read(ContentType type,sptr<Ashmem> & buffer)102 int32_t IntellVoiceEngineAdapterImpl::Read(ContentType type, sptr<Ashmem> &buffer)
103 {
104     INTELLIGENT_VOICE_LOGD("enter");
105     uint8_t *tmp = nullptr;
106     uint32_t size = 0;
107 
108     ReadFileDataInner(type, tmp, size);
109     if (tmp == nullptr) {
110         INTELLIGENT_VOICE_LOGE("tmp buffer is nullptr");
111         return HDF_ERR_INVALID_OBJECT;
112     }
113 
114     ON_SCOPE_EXIT_WITH_NAME(bufferExit)
115     {
116         INTELLIGENT_VOICE_LOGI("now delete buffer");
117         delete[] tmp;
118         tmp = nullptr;
119     };
120 
121     if (size == 0) {
122         INTELLIGENT_VOICE_LOGE("size(%{public}u) is invalid", size);
123         return HDF_ERR_INVALID_OBJECT;
124     }
125 
126     buffer = OHOS::Ashmem::CreateAshmem("ReadContent", size);
127     if (buffer == nullptr) {
128         INTELLIGENT_VOICE_LOGE("ashmem buffer is nullptr, size:%{public}u", size);
129         return HDF_ERR_INVALID_OBJECT;
130     }
131 
132     if (!buffer->MapReadAndWriteAshmem()) {
133         INTELLIGENT_VOICE_LOGE("failed to map and write ashmem");
134         return HDF_FAILURE;
135     }
136 
137     if (!buffer->WriteToAshmem(tmp, size, 0)) {
138         INTELLIGENT_VOICE_LOGE("failed to write to ashmem");
139         return HDF_FAILURE;
140     }
141     return HDF_SUCCESS;
142 }
143 
ReadFileDataInner(ContentType type,uint8_t * & buffer,uint32_t & size)144 int32_t IntellVoiceEngineAdapterImpl::ReadFileDataInner(ContentType type, uint8_t *&buffer, uint32_t &size)
145 {
146     INTELLIGENT_VOICE_LOGD("enter");
147     return engine_->ReadFileData(type, [&](std::shared_ptr<uint8_t> fileData, uint32_t fileSize) {
148         buffer = new (std::nothrow) uint8_t[fileSize];
149         if (buffer == nullptr) {
150             INTELLIGENT_VOICE_LOGE("buffer is nullptr");
151             return;
152         }
153         size = fileSize;
154         (void)memcpy_s(buffer, size, fileData.get(), fileSize);
155     });
156 }
157 
EngineListener(const sptr<IIntellVoiceEngineCallback> & cb)158 EngineListener::EngineListener(const sptr<IIntellVoiceEngineCallback> &cb) : cb_(cb)
159 {
160 }
161 
~EngineListener()162 EngineListener::~EngineListener()
163 {
164     cb_ = nullptr;
165 }
166 
OnIntellVoiceEvent(const IntellVoiceEngineCallBackEvent & event)167 void EngineListener::OnIntellVoiceEvent(const IntellVoiceEngineCallBackEvent &event)
168 {
169     if (cb_ == nullptr) {
170         INTELLIGENT_VOICE_LOGE("cb_ is nullptr");
171         return;
172     }
173 
174     cb_->OnIntellVoiceHdiEvent(event);
175 }
176 }
177 }
178 }