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 #include "wakeup_source_process.h" 29 #include "task_executor.h" 30 31 namespace OHOS { 32 namespace IntellVoiceEngine { 33 using OHOS::IntellVoiceUtils::StateMsg; 34 using OHOS::IntellVoiceUtils::State; 35 36 class HeadsetWakeupEngineImpl : private OHOS::IntellVoiceUtils::ModuleStates, private EngineUtil, 37 private WakeupSourceProcess, private OHOS::IntellVoiceUtils::TaskExecutor { 38 public: 39 HeadsetWakeupEngineImpl(); 40 ~HeadsetWakeupEngineImpl(); 41 bool Init(); 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 void OnWakeupEvent(const OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineCallBackEvent &event); 58 void OnInitDone(int32_t result); 59 void OnWakeupRecognition(int32_t result, const std::string &info); 60 bool StartAudioSource(); 61 void StopAudioSource(); 62 void ReadThread(); 63 64 private: 65 bool InitStates(); 66 int32_t HandleSetParam(const StateMsg &msg, State &nextState); 67 int32_t HandleInit(const StateMsg &msg, State &nextState); 68 int32_t HandleSetListener(const StateMsg &msg, State &nextState); 69 int32_t HandleInitDone(const StateMsg &msg, State &nextState); 70 int32_t HandleStart(const StateMsg &msg, State &nextState); 71 int32_t HandleStop(const StateMsg &msg, State &nextState); 72 int32_t HandleRecognizeComplete(const StateMsg &msg, State &nextState); 73 int32_t HandleStartCapturer(const StateMsg &msg, State &nextState); 74 int32_t HandleRead(const StateMsg &msg, State &nextState); 75 int32_t HandleStopCapturer(const StateMsg &msg, State &nextState); 76 int32_t HandleRecognizingTimeout(const StateMsg &msg, State &nextState); 77 int32_t HandleResetAdapter(const StateMsg &msg, State &nextState); 78 int32_t HandleRelease(const StateMsg &msg, State &nextState); 79 80 private: 81 using EngineUtil::adapter_; 82 int32_t channels_ = 0; 83 std::atomic<bool> isReading_ = false; 84 std::thread readThread_; 85 sptr<OHOS::HDI::IntelligentVoice::Engine::V1_0::IIntellVoiceEngineCallback> callback_ = nullptr; 86 std::shared_ptr<WakeupAdapterListener> adapterListener_ = nullptr; 87 friend class OHOS::IntellVoiceUtils::UniquePtrFactory<HeadsetWakeupEngineImpl>; 88 }; 89 } 90 } 91 #endif