• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 ONLY_FIRST_WAKEUP_ENGINE_IMPL_H
16 #define ONLY_FIRST_WAKEUP_ENGINE_IMPL_H
17 
18 #include <memory>
19 #include <string>
20 #include "i_intell_voice_engine.h"
21 #include "state_manager.h"
22 #include "wakeup_source_stop_callback.h"
23 #include "audio_source.h"
24 #include "wakeup_source_process.h"
25 
26 namespace OHOS {
27 namespace IntellVoiceEngine {
28 using OHOS::IntellVoiceUtils::StateMsg;
29 using OHOS::IntellVoiceUtils::State;
30 
31 enum EngineEvent {
32     INIT = 0,
33     INIT_DONE,
34     START_RECOGNIZE,
35     START_CAPTURER,
36     READ,
37     STOP_CAPTURER,
38     READ_CAPTURER_TIMEOUT,
39     GET_PARAM,
40     RELEASE,
41     RECOGNIZE_COMPLETE_TIMEOUT,
42     RECORD_START,
43 };
44 
45 struct CapturerData {
46     std::vector<uint8_t> data;
47 };
48 
49 struct StringParam {
strParamStringParam50     explicit StringParam(const std::string &str = "") : strParam(str) {}
51     std::string strParam;
52 };
53 
54 class OnlyFirstWakeupEngineImpl : private OHOS::IntellVoiceUtils::ModuleStates, private WakeupSourceProcess {
55 public:
56     OnlyFirstWakeupEngineImpl();
57     ~OnlyFirstWakeupEngineImpl();
58     int32_t Handle(const StateMsg &msg);
59 
60 private:
61     enum EngineState {
62         IDLE = 0,
63         INITIALIZED,
64         RECOGNIZED,
65         READ_CAPTURER,
66     };
67 
68 private:
69     bool StartAudioSource();
70     void StopAudioSource();
71     bool CreateWakeupSourceStopCallback();
72     void DestroyWakeupSourceStopCallback();
73     void OnInitDone(int32_t result);
74 
75 private:
76     bool InitStates();
77     int32_t HandleGetParam(const StateMsg &msg, State &nextState);
78     int32_t HandleInit(const StateMsg &msg, State &nextState);
79     int32_t HandleStart(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 HandleRelease(const StateMsg &msg, State &nextState);
84     void ReadBufferCallback(uint8_t *buffer, uint32_t size, bool isEnd);
85     int32_t HandleRecordStart(const StateMsg &msg, State &nextState);
86 
87 private:
88     int32_t channels_ = 0;
89     uint32_t channelId_ = 0;
90     std::shared_ptr<WakeupSourceStopCallback> wakeupSourceStopCallback_ = nullptr;
91     std::unique_ptr<AudioSource> audioSource_ = nullptr;
92     OHOS::AudioStandard::AudioCapturerOptions capturerOptions_;
93     int32_t recordStart_ = -1;
94 };
95 }
96 }
97 #endif