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_detector.h"
16 #include "intell_voice_log.h"
17 #include "trigger_detector_recognition_callback.h"
18 #include "memory_guard.h"
19
20 #define LOG_TAG "TriggerDetector"
21
22 namespace OHOS {
23 namespace IntellVoiceTrigger {
TriggerDetector(int32_t uuid,std::shared_ptr<TriggerService> service,std::shared_ptr<IIntellVoiceTriggerDetectorCallback> callback)24 TriggerDetector::TriggerDetector(int32_t uuid, std::shared_ptr<TriggerService> service,
25 std::shared_ptr<IIntellVoiceTriggerDetectorCallback> callback) : uuid_(uuid), service_(service)
26 {
27 callback_ = std::make_shared<TriggerDetectorRecognitionCallback>(callback);
28 if (callback_ == nullptr) {
29 INTELL_VOICE_LOG_ERROR("callback_ is nullptr");
30 }
31 }
32
~TriggerDetector()33 TriggerDetector::~TriggerDetector()
34 {
35 service_ = nullptr;
36 }
37
StartRecognition()38 bool TriggerDetector::StartRecognition()
39 {
40 OHOS::IntellVoiceUtils::MemoryGuard memoryGuard;
41 if (service_ == nullptr) {
42 INTELL_VOICE_LOG_ERROR("service_ is nullptr");
43 return false;
44 }
45 service_->StartRecognition(uuid_, callback_);
46 return true;
47 }
48
StopRecognition()49 bool TriggerDetector::StopRecognition()
50 {
51 OHOS::IntellVoiceUtils::MemoryGuard memoryGuard;
52 if (service_ == nullptr) {
53 INTELL_VOICE_LOG_ERROR("service_ is nullptr");
54 return false;
55 }
56 service_->StopRecognition(uuid_, callback_);
57 return true;
58 }
59
UnloadTriggerModel()60 void TriggerDetector::UnloadTriggerModel()
61 {
62 OHOS::IntellVoiceUtils::MemoryGuard memoryGuard;
63 if (service_ == nullptr) {
64 INTELL_VOICE_LOG_ERROR("service_ is nullptr");
65 return;
66 }
67
68 service_->UnloadTriggerModel(uuid_);
69 }
70 }
71 }