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 HEADSET_WAKEUP_ENGINE_IMPL_H
16 #define HEADSET_WAKEUP_ENGINE_IMPL_H
17 
18 #include <memory>
19 #include <string>
20 #include <atomic>
21 #include "v1_0/iintell_voice_engine_callback.h"
22 #include "audio_info.h"
23 #include "intell_voice_generic_factory.h"
24 #include "i_intell_voice_engine.h"
25 #include "state_manager.h"
26 #include "engine_util.h"
27 #include "wakeup_adapter_listener.h"
28 
29 #include "headset_wakeup_wrapper.h"
30 #include "wakeup_source_process.h"
31 
32 namespace OHOS {
33 namespace IntellVoiceEngine {
34 using OHOS::IntellVoiceUtils::StateMsg;
35 using OHOS::IntellVoiceUtils::State;
36 
37 class HeadsetWakeupEngineImpl : private OHOS::IntellVoiceUtils::ModuleStates, private EngineUtil,
38     private WakeupSourceProcess, private HeadsetWakeupWrapper {
39 public:
40     HeadsetWakeupEngineImpl();
41     ~HeadsetWakeupEngineImpl();
42     bool Init();
43     int32_t Handle(const StateMsg &msg);
44     using HeadsetWakeupWrapper::GetHeadsetAwakeState;
45 
46 private:
47     enum EngineState {
48         IDLE = 0,
49         INITIALIZING = 1,
50         INITIALIZED = 2,
51         RECOGNIZING = 3,
52         RECOGNIZED = 4,
53         READ_CAPTURER = 5,
54     };
55 
56 private:
57     bool SetCallbackInner();
58     int32_t AttachInner(const IntellVoiceEngineInfo &info);
59     void OnWakeupEvent(const OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineCallBackEvent &event);
60     void OnInitDone(int32_t result);
61     void OnWakeupRecognition(int32_t result, const std::string &info);
62     bool StartAudioSource();
63     void StopAudioSource();
64     void ReadThread();
65 
66 private:
67     bool InitStates();
68     int32_t HandleSetParam(const StateMsg &msg, State &nextState);
69     int32_t HandleInit(const StateMsg &msg, State &nextState);
70     int32_t HandleSetListener(const StateMsg &msg, State &nextState);
71     int32_t HandleInitDone(const StateMsg &msg, State &nextState);
72     int32_t HandleStart(const StateMsg &msg, State &nextState);
73     int32_t HandleStop(const StateMsg &msg, State &nextState);
74     int32_t HandleRecognizeComplete(const StateMsg &msg, State &nextState);
75     int32_t HandleStartCapturer(const StateMsg &msg, State &nextState);
76     int32_t HandleRead(const StateMsg &msg, State &nextState);
77     int32_t HandleStopCapturer(const StateMsg &msg, State &nextState);
78     int32_t HandleRecognizingTimeout(const StateMsg &msg, State &nextState);
79     int32_t HandleResetAdapter(const StateMsg &msg, State &nextState);
80     int32_t HandleRelease(const StateMsg &msg, State &nextState);
81 
82 private:
83     using EngineUtil::adapter_;
84     int32_t channels_ = 0;
85     std::atomic<bool> isReading_ = false;
86     std::thread readThread_;
87     sptr<OHOS::HDI::IntelligentVoice::Engine::V1_0::IIntellVoiceEngineCallback> callback_ = nullptr;
88     std::shared_ptr<WakeupAdapterListener> adapterListener_ = nullptr;
89     friend class OHOS::IntellVoiceUtils::UniquePtrFactory<HeadsetWakeupEngineImpl>;
90 };
91 }
92 }
93 #endif