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 #include "task_executor.h" 31 32 #ifdef SUPPORT_WINDOW_MANAGER 33 #include "display_manager_lite.h" 34 using FoldStatus = OHOS::Rosen::FoldStatus; 35 #endif 36 37 namespace OHOS { 38 namespace IntellVoiceEngine { 39 using OHOS::IntellVoiceUtils::StateMsg; 40 using OHOS::IntellVoiceUtils::State; 41 42 class WakeupEngineImpl : private OHOS::IntellVoiceUtils::ModuleStates, private EngineUtil, 43 private WakeupSourceProcess, private OHOS::IntellVoiceUtils::TaskExecutor { 44 public: 45 WakeupEngineImpl(); 46 ~WakeupEngineImpl(); 47 int32_t Handle(const StateMsg &msg); 48 49 private: 50 enum EngineState { 51 IDLE = 0, 52 INITIALIZING = 1, 53 INITIALIZED = 2, 54 RECOGNIZING = 3, 55 RECOGNIZED = 4, 56 READ_CAPTURER = 5, 57 }; 58 59 private: 60 bool SetCallbackInner(); 61 int32_t AttachInner(const IntellVoiceEngineInfo &info); 62 bool StartAudioSource(); 63 void StopAudioSource(); 64 void SetParamOnAudioStart(int32_t uuid); 65 void SetParamOnAudioStop(); 66 bool CreateWakeupSourceStopCallback(); 67 void DestroyWakeupSourceStopCallback(); 68 void OnWakeupEvent(const OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineCallBackEvent &event); 69 void OnInitDone(int32_t result); 70 void OnWakeupRecognition(int32_t result, const std::string &info); 71 void UpdateDspModel(); 72 void SetWakeupModel(); 73 OHOS::AudioStandard::AudioChannel GetWakeupSourceChannel(); 74 void SubscribeSwingEvent(); 75 void UnSubscribeSwingEvent(); 76 #ifdef SUPPORT_WINDOW_MANAGER 77 int32_t GetFoldStatusInfo(bool &isFoldable, FoldStatus &status); 78 void SetFoldStatus(); 79 #endif 80 81 private: 82 bool InitStates(); 83 int32_t HandleGetParam(const StateMsg &msg, State &nextState); 84 int32_t HandleSetParam(const StateMsg &msg, State &nextState); 85 int32_t HandleInit(const StateMsg &msg, State &nextState); 86 int32_t HandleSetListener(const StateMsg &msg, State &nextState); 87 int32_t HandleInitDone(const StateMsg &msg, State &nextState); 88 int32_t HandleStart(const StateMsg &msg, State &nextState); 89 int32_t HandleStop(const StateMsg &msg, State &nextState); 90 int32_t HandleRecognizeComplete(const StateMsg &msg, State &nextState); 91 int32_t HandleReconfirmRecognitionComplete(const StateMsg &msg, State &nextState); 92 int32_t HandleStartCapturer(const StateMsg &msg, State &nextState); 93 int32_t HandleRead(const StateMsg &msg, State &nextState); 94 int32_t HandleStopCapturer(const StateMsg &msg, State &nextState); 95 int32_t HandleGetWakeupPcm(const StateMsg &msg, State &nextState); 96 int32_t HandleRecognizingTimeout(const StateMsg &msg, State &nextState); 97 int32_t HandleResetAdapter(const StateMsg &msg, State &nextState); 98 int32_t HandleRelease(const StateMsg &msg, State &nextState); 99 std::string GetWakeupPhrase(); 100 void ReadBufferCallback(uint8_t *buffer, uint32_t size, bool isEnd); 101 102 private: 103 using EngineUtil::adapter_; 104 bool isPcmFromExternal_ = false; 105 int32_t channels_ = 0; 106 uint32_t channelId_ = 0; 107 uint32_t callerTokenId_ = 0; 108 std::atomic<bool> isSubscribeSwingEvent_ = false; 109 sptr<OHOS::HDI::IntelligentVoice::Engine::V1_0::IIntellVoiceEngineCallback> callback_ = nullptr; 110 std::shared_ptr<WakeupAdapterListener> adapterListener_ = nullptr; 111 std::shared_ptr<WakeupSourceStopCallback> wakeupSourceStopCallback_ = nullptr; 112 std::unique_ptr<AudioSource> audioSource_ = nullptr; 113 OHOS::AudioStandard::AudioCapturerOptions capturerOptions_; 114 #ifdef SUPPORT_WINDOW_MANAGER 115 FoldStatus curFoldStatus_ = FoldStatus::UNKNOWN; 116 #endif 117 }; 118 } 119 } 120 #endif