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 "engine_base.h"
16
17 #include "intell_voice_log.h"
18 #include "string_util.h"
19
20 using namespace OHOS::IntellVoiceUtils;
21 using namespace OHOS::HDI::IntelligentVoice::Engine::V1_0;
22 #define LOG_TAG "EngineBase"
23
24 namespace OHOS {
25 namespace IntellVoiceEngine {
EngineBase()26 EngineBase::EngineBase()
27 {
28 INTELL_VOICE_LOG_INFO("constructor");
29 desc_.adapterType = ADAPTER_TYPE_BUT;
30 }
31
SetParameter(const std::string & keyValueList)32 int32_t EngineBase::SetParameter(const std::string &keyValueList)
33 {
34 std::lock_guard<std::mutex> lock(mutex_);
35 if (adapter_ == nullptr) {
36 INTELL_VOICE_LOG_ERROR("adapter is nullptr");
37 return -1;
38 }
39 return adapter_->SetParameter(keyValueList);
40 }
41
GetParameter(const std::string & key)42 std::string EngineBase::GetParameter(const std::string &key)
43 {
44 std::lock_guard<std::mutex> lock(mutex_);
45 if (adapter_ == nullptr) {
46 INTELL_VOICE_LOG_ERROR("adapter is nullptr");
47 return "";
48 }
49
50 std::string value;
51 adapter_->GetParameter(key, value);
52 return value;
53 }
54
WriteAudio(const uint8_t * buffer,uint32_t size)55 int32_t EngineBase::WriteAudio(const uint8_t *buffer, uint32_t size)
56 {
57 std::lock_guard<std::mutex> lock(mutex_);
58 if (adapter_ == nullptr) {
59 INTELL_VOICE_LOG_ERROR("adapter is nullptr");
60 return -1;
61 }
62 std::vector<uint8_t> audioBuff(&buffer[0], &buffer[size]);
63 return adapter_->WriteAudio(audioBuff);
64 }
65
Stop()66 int32_t EngineBase::Stop()
67 {
68 std::lock_guard<std::mutex> lock(mutex_);
69 if (adapter_ == nullptr) {
70 INTELL_VOICE_LOG_ERROR("adapter is nullptr");
71 return -1;
72 }
73 return adapter_->Stop();
74 }
75
SplitStringToKVPair(const std::string & inputStr,std::map<std::string,std::string> & kvpairs)76 void EngineBase::SplitStringToKVPair(const std::string &inputStr, std::map<std::string, std::string> &kvpairs)
77 {
78 std::vector<std::string> paramsList;
79 StringUtil::Split(inputStr, ";", paramsList);
80 for (auto &it : paramsList) {
81 std::string key;
82 std::string value;
83 if (StringUtil::SplitLineToPair(it, key, value)) {
84 kvpairs[key] = value;
85 INTELL_VOICE_LOG_INFO("key:%{public}s, value:%{public}s", key.c_str(), value.c_str());
86 }
87 }
88 }
89 }
90 }