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