• 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 #ifndef INTELL_VOICE_TRIGGER_HELPER_H
16 #define INTELL_VOICE_TRIGGER_HELPER_H
17 
18 #include <cstdint>
19 #include <map>
20 #include "msg_handle_thread.h"
21 #include "trigger_base_type.h"
22 #include "i_intell_voice_trigger_recognition_callback.h"
23 
24 #include "i_intell_voice_trigger_connector_module.h"
25 #include "i_intell_voice_trigger_connector_callback.h"
26 #include "trigger_connector_common_type.h"
27 
28 namespace OHOS {
29 namespace IntellVoiceTrigger {
30 enum ModelState { MODEL_NOTLOADED, MODEL_LOADED, MODEL_STARTED, MODEL_STATE_BUT };
31 
32 class TriggerModelData {
33 public:
34     explicit TriggerModelData(int32_t uuid);
35     ~TriggerModelData();
36     void SetCallback(std::shared_ptr<IIntellVoiceTriggerRecognitionCallback> callback);
37     std::shared_ptr<IIntellVoiceTriggerRecognitionCallback> GetCallback();
38     void SetModel(std::shared_ptr<GenericTriggerModel> model);
39     std::shared_ptr<GenericTriggerModel> GetModel();
40     void SetState(ModelState state);
41     ModelState GetState() const;
42     void SetModelHandle(int32_t handle);
43     int32_t GetModelHandle() const;
44 
45     bool SameModel(std::shared_ptr<GenericTriggerModel> model);
46     void Clear();
47 
48 public:
49     int32_t uuid_ = -1;
50 
51 private:
52     ModelState state_ = MODEL_NOTLOADED;
53     std::shared_ptr<GenericTriggerModel> model_ = nullptr;
54     std::shared_ptr<IIntellVoiceTriggerRecognitionCallback> callback_ = nullptr;
55     int32_t modelHandle_ = 0;
56 };
57 
58 class TriggerHelper : public IIntellVoiceTriggerConnectorCallback,
59     public std::enable_shared_from_this<TriggerHelper> {
60 public:
61     ~TriggerHelper();
62 
63     static std::shared_ptr<TriggerHelper> Create();
64 
65     int32_t StartGenericRecognition(int32_t uuid, std::shared_ptr<GenericTriggerModel> model,
66         std::shared_ptr<IIntellVoiceTriggerRecognitionCallback> callback);
67     int32_t StopGenericRecognition(int32_t uuid, std::shared_ptr<IIntellVoiceTriggerRecognitionCallback> callback);
68     void UnloadGenericTriggerModel(int32_t uuid);
69     std::shared_ptr<TriggerModelData> GetTriggerModelData(int32_t uuid);
70 
71 private:
72     TriggerHelper();
73     void GetModule();
74     int32_t InitRecognition(std::shared_ptr<TriggerModelData> modelData, bool unload);
75     int32_t StartRecognition(std::shared_ptr<TriggerModelData> modelData);
76     int32_t StopRecognition(std::shared_ptr<TriggerModelData> modelData);
77     int32_t LoadModel(std::shared_ptr<TriggerModelData> modelData);
78     int32_t UnloadModel(std::shared_ptr<TriggerModelData> modelData);
79 
80     void OnRecognition(int32_t modelHandle, const IntellVoiceRecognitionEvent &event) override;
81 
82 private:
83     std::mutex mutex_;
84 
85     std::map<int32_t, std::shared_ptr<TriggerModelData>> modelDataMap_;
86     std::shared_ptr<IIntellVoiceTriggerConnectorModule> module_ = nullptr;
87     std::vector<TriggerConnectorModuleDesc> moduleDesc_;
88 };
89 }  // namespace IntellVoiceTrigger
90 }  // namespace OHOS
91 #endif