• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "engine_event_callback_napi.h"
16 #include <uv.h>
17 #include "intell_voice_info.h"
18 #include "intell_voice_log.h"
19 #include "intell_voice_common_napi.h"
20 #include "v1_2/intell_voice_engine_types.h"
21 
22 #define LOG_TAG "EngineEventCallbackNapi"
23 
24 using namespace std;
25 using namespace OHOS::IntellVoice;
26 using namespace OHOS::IntellVoiceEngine;
27 
28 namespace OHOS {
29 namespace IntellVoiceNapi {
EngineEventCallbackNapi(napi_env env)30 EngineEventCallbackNapi::EngineEventCallbackNapi(napi_env env) : env_(env)
31 {
32     INTELL_VOICE_LOG_INFO("enter");
33     if (env_ != nullptr) {
34         napi_get_uv_event_loop(env_, &loop_);
35     }
36 }
37 
SaveCallbackReference(napi_value callback)38 void EngineEventCallbackNapi::SaveCallbackReference(napi_value callback)
39 {
40     std::lock_guard<std::mutex> lock(mutex_);
41     for (auto it : callbackRefSet_) {
42         if (IntellVoiceCommonNapi::IsSameCallback(env_, callback, it->ref_)) {
43             INTELL_VOICE_LOG_INFO("the callback already exists");
44             return;
45         }
46     }
47 
48     napi_ref cbRef = nullptr;
49     constexpr int32_t refCount = 1;
50     napi_status status = napi_create_reference(env_, callback, refCount, &cbRef);
51     CHECK_CONDITION_RETURN_VOID(((status != napi_ok) || (cbRef == nullptr)), "creating reference for callback failed");
52     std::shared_ptr<IntellVoiceRef> callbackRef =  std::make_shared<IntellVoiceRef>(env_, cbRef);
53     CHECK_CONDITION_RETURN_VOID((callbackRef == nullptr), "create callback ref failed");
54     callbackRefSet_.insert(callbackRef);
55     INTELL_VOICE_LOG_INFO("save callback reference success, size [%{public}zu]", callbackRefSet_.size());
56 }
57 
RemoveCallbackReference(napi_value callback)58 void EngineEventCallbackNapi::RemoveCallbackReference(napi_value callback)
59 {
60     std::lock_guard<std::mutex> lock(mutex_);
61     for (auto it = callbackRefSet_.begin(); it != callbackRefSet_.end(); ++it) {
62         if (IntellVoiceCommonNapi::IsSameCallback(env_, callback, (*it)->ref_)) {
63             callbackRefSet_.erase(it);
64             INTELL_VOICE_LOG_INFO("remove callback reference success, size [%{public}zu]", callbackRefSet_.size());
65             return;
66         }
67     }
68 
69     INTELL_VOICE_LOG_ERROR("js callback not find");
70 }
71 
RemoveAllCallbackReference()72 void EngineEventCallbackNapi::RemoveAllCallbackReference()
73 {
74     std::lock_guard<std::mutex> lock(mutex_);
75     callbackRefSet_.clear();
76     INTELL_VOICE_LOG_INFO("remove all callback reference");
77 }
78 
GetCbReferenceSetSize()79 uint32_t EngineEventCallbackNapi::GetCbReferenceSetSize()
80 {
81     std::lock_guard<std::mutex> lock(mutex_);
82     return callbackRefSet_.size();
83 }
84 
OnEvent(const IntellVoiceEngineCallBackEvent & event)85 void EngineEventCallbackNapi::OnEvent(const IntellVoiceEngineCallBackEvent &event)
86 {
87     std::lock_guard<std::mutex> lock(mutex_);
88     INTELL_VOICE_LOG_INFO("enter");
89     int32_t eventId = -1;
90     if (event.msgId == HDI::IntelligentVoice::Engine::V1_0::INTELL_VOICE_ENGINE_MSG_RECOGNIZE_COMPLETE) {
91         eventId = INTELLIGENT_VOICE_EVENT_RECOGNIZE_COMPLETE;
92     } else if (event.msgId == static_cast<OHOS::HDI::IntelligentVoice::Engine::V1_0::IntellVoiceEngineMessageType>(
93         OHOS::HDI::IntelligentVoice::Engine::V1_2::INTELL_VOICE_ENGINE_MSG_HEADSET_RECOGNIZE_COMPLETE)) {
94         eventId = INTELLIGENT_VOICE_EVENT_HEADSET_RECOGNIZE_COMPLETE;
95     } else {
96         INTELL_VOICE_LOG_ERROR("error msgId:%{public}d", event.msgId);
97         return;
98     }
99     EngineCallBackInfo cbInfo = {eventId, event.result == 0 ? true : false, event.info};
100     INTELL_VOICE_LOG_INFO("OnEvent EngineCallBackInfo: eventId: %{public}d, isSuccess: %{public}u",
101         cbInfo.eventId, cbInfo.isSuccess);
102 
103     for (auto it : callbackRefSet_) {
104         OnJsCallbackEngineEvent(cbInfo, it);
105     }
106 }
107 
OnJsCallbackEngineEvent(const EngineCallBackInfo & cbInfo,std::shared_ptr<IntellVoiceRef> cbRef)108 void EngineEventCallbackNapi::OnJsCallbackEngineEvent(const EngineCallBackInfo &cbInfo,
109     std::shared_ptr<IntellVoiceRef> cbRef)
110 {
111     CHECK_CONDITION_RETURN_VOID((loop_ == nullptr), "loop is nullptr");
112     uv_work_t *work = new (nothrow) uv_work_t;
113     CHECK_CONDITION_RETURN_VOID((work == nullptr), "Create uv work failed, no memory");
114     work->data = new EngineEventCallbackData { cbInfo, cbRef };
115     CHECK_CONDITION_RETURN_VOID((work->data == nullptr), "Create uv work data failed, no memory");
116 
117     int32_t ret = uv_queue_work_with_qos(loop_, work, [](uv_work_t *work) {}, [](uv_work_t *work, int uvStatus) {
118         std::shared_ptr<EngineEventCallbackData> callbackData(
119             static_cast<EngineEventCallbackData *>(work->data), [work](EngineEventCallbackData *data) {
120                 delete data;
121                 delete work;
122         });
123         CHECK_CONDITION_RETURN_VOID((callbackData == nullptr), "uvCallback is nullptr");
124         CHECK_CONDITION_RETURN_VOID((callbackData->callback == nullptr), "uvCallback callback is nullptr");
125         napi_env env = callbackData->callback->env_;
126         napi_value jsCallback = callbackData->callback->GetRefValue();
127         CHECK_CONDITION_RETURN_VOID((jsCallback == nullptr), "get reference value failed");
128 
129         const size_t argc = ARGC_ONE;
130         napi_value args[argc] = { GetCallBackInfoNapiValue(env, callbackData->cbInfo) };
131         CHECK_CONDITION_RETURN_VOID((args[ARG_INDEX_0] == nullptr), "arg is nullptr");
132         napi_value result = nullptr;
133         napi_status status = napi_call_function(env, nullptr, jsCallback, argc, args, &result);
134         if (status != napi_ok) {
135             INTELL_VOICE_LOG_ERROR("failed to call engine event callback, error: %{public}d", status);
136         }
137     }, uv_qos_default);
138     if (ret != 0) {
139         INTELL_VOICE_LOG_ERROR("failed to execute libuv work queue");
140         delete reinterpret_cast<EngineEventCallbackData*>(work->data);
141         delete work;
142     }
143 }
144 
GetCallBackInfoNapiValue(napi_env env,const EngineCallBackInfo & callbackInfo)145 napi_value EngineEventCallbackNapi::GetCallBackInfoNapiValue(napi_env env, const EngineCallBackInfo &callbackInfo)
146 {
147     napi_value result;
148     napi_status status = napi_create_object(env, &result);
149     if (status != napi_ok || result == nullptr) {
150         INTELL_VOICE_LOG_ERROR("failed to create js callbackInfo, error: %{public}d", status);
151         return nullptr;
152     }
153 
154     napi_set_named_property(env, result, "eventId", SetValue(env, callbackInfo.eventId));
155     napi_set_named_property(env, result, "isSuccess", SetValue(env, callbackInfo.isSuccess));
156     napi_set_named_property(env, result, "context", SetValue(env, callbackInfo.context));
157     return result;
158 }
159 }  // namespace IntellVoiceNapi
160 }  // namespace OHOS
161