1 /*
2  * Copyright (c) 2024 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 WAKEUP_ENGINE_IMPL_H
16 #define WAKEUP_ENGINE_IMPL_H
17 
18 #include <memory>
19 #include <string>
20 #include "v1_0/iintell_voice_engine_callback.h"
21 #include "audio_info.h"
22 #include "i_intell_voice_engine.h"
23 #include "state_manager.h"
24 #include "engine_util.h"
25 #include "wakeup_adapter_listener.h"
26 #include "wakeup_source_stop_callback.h"
27 
28 #include "audio_source.h"
29 #include "wakeup_source_process.h"
30 
31 namespace OHOS {
32 namespace IntellVoiceEngine {
33 using OHOS::IntellVoiceUtils::StateMsg;
34 using OHOS::IntellVoiceUtils::State;
35 
36 class WakeupEngineImpl : private OHOS::IntellVoiceUtils::ModuleStates, private EngineUtil,
37     private WakeupSourceProcess {
38 public:
39     WakeupEngineImpl();
40     ~WakeupEngineImpl();
41     int32_t Handle(const StateMsg &msg);
42 
43 private:
44     enum EngineState {
45         IDLE = 0,
46         INITIALIZING = 1,
47         INITIALIZED = 2,
48         RECOGNIZING = 3,
49         RECOGNIZED = 4,
50         READ_CAPTURER = 5,
51     };
52 
53 private:
54     bool SetCallbackInner();
55     int32_t AttachInner(const IntellVoiceEngineInfo &info);
56     bool StartAudioSource();
57     void StopAudioSource();
58     void SetParamOnAudioStart(int32_t uuid);
59     void SetParamOnAudioStop();
60     bool CreateWakeupSourceStopCallback();
61     void DestroyWakeupSourceStopCallback();
62     void OnWakeupEvent(const OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineCallBackEvent &event);
63     void OnInitDone(int32_t result);
64     void OnWakeupRecognition(int32_t result, const std::string &info);
65     void OnWakeupKws3Recognition(int32_t result, const std::string &info);
66     void UpdateDspModel();
67     OHOS::AudioStandard::AudioChannel GetWakeupSourceChannel();
68 
69 private:
70     bool InitStates();
71     int32_t HandleGetParam(const StateMsg &msg, State &nextState);
72     int32_t HandleSetParam(const StateMsg &msg, State &nextState);
73     int32_t HandleInit(const StateMsg &msg, State &nextState);
74     int32_t HandleSetListener(const StateMsg &msg, State &nextState);
75     int32_t HandleInitDone(const StateMsg &msg, State &nextState);
76     int32_t HandleStart(const StateMsg &msg, State &nextState);
77     int32_t HandleStop(const StateMsg &msg, State &nextState);
78     int32_t HandleRecognizeComplete(const StateMsg &msg, State &nextState);
79     int32_t HandleReconfirmRecognitionComplete(const StateMsg &msg, State &nextState);
80     int32_t HandleStartCapturer(const StateMsg &msg, State &nextState);
81     int32_t HandleRead(const StateMsg &msg, State &nextState);
82     int32_t HandleStopCapturer(const StateMsg &msg, State &nextState);
83     int32_t HandleGetWakeupPcm(const StateMsg &msg, State &nextState);
84     int32_t HandleRecognizingTimeout(const StateMsg &msg, State &nextState);
85     int32_t HandleResetAdapter(const StateMsg &msg, State &nextState);
86     int32_t HandleRelease(const StateMsg &msg, State &nextState);
87     std::string GetWakeupPhrase();
88 
89 private:
90     using EngineUtil::adapter_;
91     bool isPcmFromExternal_ = false;
92     int32_t channels_ = 0;
93     uint32_t channelId_ = 0;
94     sptr<OHOS::HDI::IntelligentVoice::Engine::V1_0::IIntellVoiceEngineCallback> callback_ = nullptr;
95     std::shared_ptr<WakeupAdapterListener> adapterListener_ = nullptr;
96     std::shared_ptr<WakeupSourceStopCallback> wakeupSourceStopCallback_ = nullptr;
97     std::unique_ptr<AudioSource> audioSource_ = nullptr;
98     OHOS::AudioStandard::AudioCapturerOptions capturerOptions_;
99 };
100 }
101 }
102 #endif