1 /* 2 * Copyright (c) 2023 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 16 #ifndef ENROLL_INTELL_VOICE_ENGINE_CALLBACK_NAPI_H 17 #define ENROLL_INTELL_VOICE_ENGINE_CALLBACK_NAPI_H 18 19 #include <queue> 20 #include <map> 21 #include <uv.h> 22 #include "napi/native_api.h" 23 #include "napi/native_node_api.h" 24 #include "intell_voice_napi_util.h" 25 #include "i_intell_voice_engine_callback.h" 26 #include "intell_voice_napi_queue.h" 27 28 namespace OHOS { 29 namespace IntellVoiceNapi { 30 using OHOS::IntellVoiceEngine::IIntellVoiceEngineEventCallback; 31 using OHOS::IntellVoiceEngine::IntellVoiceEngineCallBackEvent; 32 33 using ProcessWorkFunc = std::function<int32_t(AsyncContext *)>; 34 35 enum EnrollAsyncWorkType { 36 ASYNC_WORK_INIT = 0, 37 ASYNC_WORK_START, 38 ASYNC_WORK_COMMIT, 39 ASYNC_WORK_INVALID, 40 }; 41 42 struct EnrollCallbackInfo { 43 int32_t eventId; 44 int32_t result; 45 std::string context; 46 47 void GetCallBackInfoNapiValue(const napi_env &env, napi_value &out); 48 }; 49 50 class EnrollAsyncContext : public AsyncContext { 51 public: EnrollAsyncContext(napi_env env)52 explicit EnrollAsyncContext(napi_env env) : AsyncContext(env) {}; 53 EnrollAsyncWorkType type = ASYNC_WORK_INVALID; 54 ProcessWorkFunc processWork = nullptr; 55 EnrollCallbackInfo callbackInfo; 56 }; 57 58 class EnrollIntellVoiceEngineCallbackNapi : public IIntellVoiceEngineEventCallback { 59 public: 60 explicit EnrollIntellVoiceEngineCallbackNapi(const napi_env env); 61 virtual ~EnrollIntellVoiceEngineCallbackNapi(); 62 63 void OnEvent(const IntellVoiceEngineCallBackEvent &event) override; 64 void QueueAsyncWork(EnrollAsyncContext *context); 65 void ClearAsyncWork(bool error, const std::string &msg); 66 67 private: 68 int32_t ConvertEventId(EnrollAsyncWorkType type); 69 void OnJsCallBack(EnrollAsyncContext *context); 70 static void UvWorkCallBack(uv_work_t *work, int status); 71 72 private: 73 std::mutex mutex_; 74 napi_env env_ = nullptr; 75 uv_loop_s *loop_ = nullptr; 76 77 std::map<EnrollAsyncWorkType, std::queue<EnrollAsyncContext *>> contextMap_; 78 }; 79 } // namespace IntellVoiceNapi 80 } // namespace OHOS 81 #endif 82