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
16 #include "wakeup_intell_voice_engine.h"
17 #include "i_intell_voice_engine.h"
18 #include "intell_voice_manager.h"
19 #include "intell_voice_log.h"
20
21 #define LOG_TAG "WakeupIntellVoiceEngine"
22
23 using namespace std;
24 using namespace OHOS::IntellVoiceEngine;
25
26 namespace OHOS {
27 namespace IntellVoice {
WakeupIntellVoiceEngine(const WakeupIntelligentVoiceEngineDescriptor & descriptor,IntellVoiceEngineType type)28 WakeupIntellVoiceEngine::WakeupIntellVoiceEngine(const WakeupIntelligentVoiceEngineDescriptor &descriptor,
29 IntellVoiceEngineType type)
30 {
31 INTELL_VOICE_LOG_INFO("enter");
32
33 descriptor_ = make_unique<WakeupIntelligentVoiceEngineDescriptor>();
34 descriptor_->needReconfirm = descriptor.needReconfirm;
35 descriptor_->wakeupPhrase = descriptor.wakeupPhrase;
36 auto mgr = IntellVoiceManager::GetInstance();
37 if (mgr != nullptr) {
38 result_ = mgr->CreateIntellVoiceEngine(type, engine_);
39 if (result_ != INTELLIGENT_VOICE_SUCCESS) {
40 INTELL_VOICE_LOG_ERROR("create wakeup engine failed, ret:%{public}d", result_);
41 }
42 } else {
43 INTELL_VOICE_LOG_ERROR("mgr is nullptr");
44 result_ = INTELLIGENT_VOICE_SYSTEM_ERROR;
45 }
46 }
47
~WakeupIntellVoiceEngine()48 WakeupIntellVoiceEngine::~WakeupIntellVoiceEngine()
49 {
50 INTELL_VOICE_LOG_INFO("enter");
51 }
52
SetSensibility(const int32_t & sensibility)53 int32_t WakeupIntellVoiceEngine::SetSensibility(const int32_t &sensibility)
54 {
55 INTELL_VOICE_LOG_INFO("enter");
56 if (engine_ == nullptr) {
57 INTELL_VOICE_LOG_ERROR("engine is null");
58 return -1;
59 }
60 string keyValueList = "Sensibility=" + to_string(sensibility);
61 return engine_->SetParameter(keyValueList);
62 }
63
SetWakeupHapInfo(const WakeupHapInfo & info)64 int32_t WakeupIntellVoiceEngine::SetWakeupHapInfo(const WakeupHapInfo &info)
65 {
66 INTELL_VOICE_LOG_INFO("enter");
67 int32_t ret;
68 if (engine_ == nullptr) {
69 INTELL_VOICE_LOG_ERROR("engine is null");
70 return -1;
71 }
72 ret = engine_->SetParameter("wakeup_bundle_name=" + info.bundleName);
73 ret += engine_->SetParameter("wakeup_ability_name=" + info.abilityName);
74 return ret;
75 }
76
SetParameter(const string & key,const string & value)77 int32_t WakeupIntellVoiceEngine::SetParameter(const string &key, const string &value)
78 {
79 INTELL_VOICE_LOG_INFO("enter");
80 if (engine_ == nullptr) {
81 INTELL_VOICE_LOG_ERROR("engine is null");
82 return -1;
83 }
84 string keyValueList = key + "=" + value;
85 return engine_->SetParameter(keyValueList);
86 }
87
GetParameter(const std::string & key)88 std::string WakeupIntellVoiceEngine::GetParameter(const std::string &key)
89 {
90 INTELL_VOICE_LOG_INFO("enter");
91 if (engine_ == nullptr) {
92 INTELL_VOICE_LOG_ERROR("engine is null");
93 return "";
94 }
95 return engine_->GetParameter(key);
96 }
97
Release()98 int32_t WakeupIntellVoiceEngine::Release()
99 {
100 INTELL_VOICE_LOG_INFO("enter");
101 return 0;
102 }
103
SetCallback(shared_ptr<IIntellVoiceEngineEventCallback> callback)104 int32_t WakeupIntellVoiceEngine::SetCallback(shared_ptr<IIntellVoiceEngineEventCallback> callback)
105 {
106 INTELL_VOICE_LOG_INFO("enter");
107 if (engine_ == nullptr) {
108 INTELL_VOICE_LOG_ERROR("engine is null");
109 return -1;
110 }
111
112 callback_ = sptr<EngineCallbackInner>(new (std::nothrow) EngineCallbackInner(callback));
113 if (callback_ == nullptr) {
114 INTELL_VOICE_LOG_ERROR("callback_ is nullptr");
115 return -1;
116 }
117 engine_->SetCallback(callback_->AsObject());
118 return 0;
119 }
120
StartCapturer(int32_t channels)121 int32_t WakeupIntellVoiceEngine::StartCapturer(int32_t channels)
122 {
123 INTELL_VOICE_LOG_INFO("enter");
124 if (engine_ == nullptr) {
125 INTELL_VOICE_LOG_ERROR("engine is null");
126 return -1;
127 }
128 return engine_->StartCapturer(channels);
129 }
130
Read(std::vector<uint8_t> & data)131 int32_t WakeupIntellVoiceEngine::Read(std::vector<uint8_t> &data)
132 {
133 INTELL_VOICE_LOG_INFO("enter");
134 CHECK_CONDITION_RETURN_RET(engine_ == nullptr, -1, "engine is null");
135
136 return engine_->Read(data);
137 }
138
StopCapturer()139 int32_t WakeupIntellVoiceEngine::StopCapturer()
140 {
141 INTELL_VOICE_LOG_INFO("enter");
142 CHECK_CONDITION_RETURN_RET(engine_ == nullptr, -1, "engine is null");
143
144 return engine_->StopCapturer();
145 }
146
GetWakeupPcm(std::vector<uint8_t> & data)147 int32_t WakeupIntellVoiceEngine::GetWakeupPcm(std::vector<uint8_t> &data)
148 {
149 INTELL_VOICE_LOG_INFO("enter");
150 CHECK_CONDITION_RETURN_RET(engine_ == nullptr, -1, "engine is null");
151
152 return engine_->GetWakeupPcm(data);
153 }
154
NotifyHeadsetWakeEvent()155 int32_t WakeupIntellVoiceEngine::NotifyHeadsetWakeEvent()
156 {
157 INTELL_VOICE_LOG_INFO("enter");
158 if (engine_ == nullptr) {
159 INTELL_VOICE_LOG_ERROR("engine is null");
160 return -1;
161 }
162 return engine_->NotifyHeadsetWakeEvent();
163 }
164
NotifyHeadsetHostEvent(int32_t event)165 int32_t WakeupIntellVoiceEngine::NotifyHeadsetHostEvent(int32_t event)
166 {
167 INTELL_VOICE_LOG_INFO("enter, event:%{public}d", event);
168 if (engine_ == nullptr) {
169 INTELL_VOICE_LOG_ERROR("engine is null");
170 return -1;
171 }
172 return engine_->NotifyHeadsetHostEvent(static_cast<OHOS::IntellVoiceEngine::HeadsetHostEventType>(event));
173 }
174 }
175 }