• 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 "trigger_manager.h"
16 #include "trigger_service.h"
17 #include "trigger_detector.h"
18 #include "intell_voice_log.h"
19 #include "memory_guard.h"
20 #include "trigger_detector_callback.h"
21 
22 #define LOG_TAG "TriggerManager"
23 
24 namespace OHOS {
25 namespace IntellVoiceTrigger {
TriggerManager()26 TriggerManager::TriggerManager()
27 {
28     OHOS::IntellVoiceUtils::MemoryGuard memoryGuard;
29     service_ = std::make_shared<TriggerService>();
30     if (service_ == nullptr) {
31         INTELL_VOICE_LOG_ERROR("service_ is nullptr");
32     }
33 }
34 
~TriggerManager()35 TriggerManager::~TriggerManager()
36 {
37     service_ = nullptr;
38 }
39 
UpdateModel(std::vector<uint8_t> buffer,int32_t uuid,TriggerModelType type)40 void TriggerManager::UpdateModel(std::vector<uint8_t> buffer, int32_t uuid, TriggerModelType type)
41 {
42     if (service_ == nullptr) {
43         INTELL_VOICE_LOG_ERROR("service_ is nullptr");
44         return;
45     }
46     std::shared_ptr<GenericTriggerModel> model = std::make_shared<GenericTriggerModel>(uuid,
47         TriggerModel::TriggerModelVersion::MODLE_VERSION_2, type);
48     if (model == nullptr) {
49         INTELL_VOICE_LOG_ERROR("model is null");
50         return;
51     }
52     model->SetData(buffer);
53     service_->UpdateGenericTriggerModel(model);
54 }
55 
DeleteModel(int32_t uuid)56 void TriggerManager::DeleteModel(int32_t uuid)
57 {
58     if (service_ == nullptr) {
59         INTELL_VOICE_LOG_ERROR("service_ is nullptr");
60         return;
61     }
62     service_->DeleteGenericTriggerModel(uuid);
63 }
64 
IsModelExist(int32_t uuid)65 bool TriggerManager::IsModelExist(int32_t uuid)
66 {
67     if (service_ == nullptr) {
68         INTELL_VOICE_LOG_ERROR("service_ is nullptr");
69         return false;
70     }
71     if (service_->GetGenericTriggerModel(uuid) == nullptr) {
72         return false;
73     }
74     return true;
75 }
76 
GetModel(int32_t uuid)77 std::shared_ptr<GenericTriggerModel> TriggerManager::GetModel(int32_t uuid)
78 {
79     if (service_ == nullptr) {
80         INTELL_VOICE_LOG_ERROR("service_ is nullptr");
81         return nullptr;
82     }
83     return service_->GetGenericTriggerModel(uuid);
84 }
85 
ReleaseTriggerDetector(int32_t uuid)86 void TriggerManager::ReleaseTriggerDetector(int32_t uuid)
87 {
88     std::lock_guard<std::mutex> lock(detectorMutex_);
89     OHOS::IntellVoiceUtils::MemoryGuard memoryGuard;
90     auto it = detectors_.find(uuid);
91     if (it != detectors_.end()) {
92         if (it->second != nullptr) {
93             it->second->UnloadTriggerModel();
94         }
95         detectors_.erase(it);
96     }
97 }
98 
SetParameter(const std::string & key,const std::string & value)99 int32_t TriggerManager::SetParameter(const std::string &key, const std::string &value)
100 {
101     if (service_ == nullptr) {
102         INTELL_VOICE_LOG_ERROR("service_ is nullptr");
103         return -1;
104     }
105     return service_->SetParameter(key, value);
106 }
107 
GetParameter(const std::string & key)108 std::string TriggerManager::GetParameter(const std::string &key)
109 {
110     if (service_ == nullptr) {
111         INTELL_VOICE_LOG_ERROR("service_ is nullptr");
112         return "";
113     }
114     return service_->GetParameter(key);
115 }
116 
117 
AttachTelephonyObserver()118 void TriggerManager::AttachTelephonyObserver()
119 {
120 #ifdef SUPPORT_TELEPHONY_SERVICE
121     if (service_ == nullptr) {
122         INTELL_VOICE_LOG_ERROR("service_ is nullptr");
123         return;
124     }
125     return service_->AttachTelephonyObserver();
126 #endif
127 }
128 
DetachTelephonyObserver()129 void TriggerManager::DetachTelephonyObserver()
130 {
131 #ifdef SUPPORT_TELEPHONY_SERVICE
132 
133     if (service_ == nullptr) {
134         INTELL_VOICE_LOG_ERROR("service_ is nullptr");
135         return;
136     }
137     return service_->DetachTelephonyObserver();
138 #endif
139 }
140 
141 
AttachAudioCaptureListener()142 void TriggerManager::AttachAudioCaptureListener()
143 {
144     if (service_ == nullptr) {
145         INTELL_VOICE_LOG_ERROR("service_ is nullptr");
146         return;
147     }
148     return service_->AttachAudioCaptureListener();
149 }
150 
DetachAudioCaptureListener()151 void TriggerManager::DetachAudioCaptureListener()
152 {
153     if (service_ == nullptr) {
154         INTELL_VOICE_LOG_ERROR("service_ is nullptr");
155         return;
156     }
157     return service_->DetachAudioCaptureListener();
158 }
159 
AttachAudioRendererEventListener()160 void TriggerManager::AttachAudioRendererEventListener()
161 {
162     if (service_ == nullptr) {
163         INTELL_VOICE_LOG_ERROR("service_ is nullptr");
164         return;
165     }
166     return service_->AttachAudioRendererEventListener();
167 }
168 
DetachAudioRendererEventListener()169 void TriggerManager::DetachAudioRendererEventListener()
170 {
171     if (service_ == nullptr) {
172         INTELL_VOICE_LOG_ERROR("service_ is nullptr");
173         return;
174     }
175     return service_->DetachAudioRendererEventListener();
176 }
177 
AttachHibernateObserver()178 void TriggerManager::AttachHibernateObserver()
179 {
180     if (service_ == nullptr) {
181         INTELL_VOICE_LOG_ERROR("service_ is nullptr");
182         return;
183     }
184     return service_->AttachHibernateObserver();
185 }
186 
DetachHibernateObserver()187 void TriggerManager::DetachHibernateObserver()
188 {
189     if (service_ == nullptr) {
190         INTELL_VOICE_LOG_ERROR("service_ is nullptr");
191         return;
192     }
193     return service_->DetachHibernateObserver();
194 }
195 
StartDetection(int32_t uuid)196 int32_t TriggerManager::StartDetection(int32_t uuid)
197 {
198     std::lock_guard<std::mutex> lock(detectorMutex_);
199     if ((detectors_.count(uuid) == 0) || (detectors_[uuid] == nullptr)) {
200         INTELL_VOICE_LOG_INFO("detector is not existed, uuid:%{public}d", uuid);
201         return -1;
202     }
203 
204     if (service_->GetParameter("audio_hal_status") == "true") {
205         INTELL_VOICE_LOG_INFO("audio hal is ready");
206         detectors_[uuid]->StartRecognition();
207         return 1;
208     }
209     return 0;
210 }
211 
StopDetection(int32_t uuid)212 void TriggerManager::StopDetection(int32_t uuid)
213 {
214     std::lock_guard<std::mutex> lock(detectorMutex_);
215     if ((detectors_.count(uuid) == 0) || (detectors_[uuid] == nullptr)) {
216         INTELL_VOICE_LOG_INFO("detector is not existed, uuid:%{public}d", uuid);
217         return;
218     }
219     detectors_[uuid]->StopRecognition();
220 }
221 
CreateDetector(int32_t uuid,std::function<void ()> onDetected)222 void TriggerManager::CreateDetector(int32_t uuid, std::function<void()> onDetected)
223 {
224     std::lock_guard<std::mutex> lock(detectorMutex_);
225     if (detectors_.count(uuid) != 0 && detectors_[uuid] != nullptr) {
226         INTELL_VOICE_LOG_INFO("detector is already existed, no need to create, uuid:%{public}d", uuid);
227         return;
228     }
229 
230     auto cb = std::make_shared<TriggerDetectorCallback>(onDetected);
231     if (cb == nullptr) {
232         INTELL_VOICE_LOG_ERROR("cb is nullptr");
233         return;
234     }
235 
236     OHOS::IntellVoiceUtils::MemoryGuard memoryGuard;
237     std::shared_ptr<TriggerDetector> detector = std::make_shared<TriggerDetector>(uuid, service_, cb);
238     if (detector == nullptr) {
239         INTELL_VOICE_LOG_ERROR("detector is nullptr");
240         return;
241     }
242 
243     detectors_[uuid] = detector;
244 }
245 
OnServiceStart()246 void TriggerManager::OnServiceStart()
247 {
248 }
249 
OnServiceStop()250 void TriggerManager::OnServiceStop()
251 {
252     DetachTelephonyObserver();
253     DetachAudioCaptureListener();
254     DetachAudioRendererEventListener();
255     DetachHibernateObserver();
256 }
257 
258 
OnTelephonyStateRegistryServiceChange(bool isAdded)259 void TriggerManager::OnTelephonyStateRegistryServiceChange(bool isAdded)
260 {
261 #ifdef SUPPORT_TELEPHONY_SERVICE
262     if (isAdded) {
263         INTELL_VOICE_LOG_INFO("telephony state registry service is added");
264         AttachTelephonyObserver();
265     } else {
266         INTELL_VOICE_LOG_INFO("telephony state registry service is removed");
267     }
268 #endif
269 }
270 
OnAudioDistributedServiceChange(bool isAdded)271 void TriggerManager::OnAudioDistributedServiceChange(bool isAdded)
272 {
273     if (isAdded) {
274         INTELL_VOICE_LOG_INFO("audio distributed service is added");
275         AttachAudioCaptureListener();
276     } else {
277         INTELL_VOICE_LOG_INFO("audio distributed service is removed");
278     }
279 }
280 
OnAudioPolicyServiceChange(bool isAdded)281 void TriggerManager::OnAudioPolicyServiceChange(bool isAdded)
282 {
283     if (isAdded) {
284         INTELL_VOICE_LOG_INFO("audio policy service is added");
285         AttachAudioRendererEventListener();
286     } else {
287         INTELL_VOICE_LOG_INFO("audio policy service is removed");
288     }
289 }
290 
OnPowerManagerServiceChange(bool isAdded)291 void TriggerManager::OnPowerManagerServiceChange(bool isAdded)
292 {
293     if (isAdded) {
294         INTELL_VOICE_LOG_INFO("power manager service is added");
295         AttachHibernateObserver();
296     } else {
297         INTELL_VOICE_LOG_INFO("power manager service is removed");
298     }
299 }
300 }  // namespace IntellVoiceTrigger
301 }  // namespace OHOS
302