• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 #include "recorder_callback_napi.h"
17 #include <uv.h>
18 #include "media_errors.h"
19 #include "media_log.h"
20 #include "scope_guard.h"
21 
22 namespace {
23     constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_RECORDER, "RecorderCallbackNapi"};
24 }
25 
26 namespace OHOS {
27 namespace Media {
RecorderCallbackNapi(napi_env env,bool isVideo)28 RecorderCallbackNapi::RecorderCallbackNapi(napi_env env, bool isVideo)
29     : env_(env), isVideo_(isVideo)
30 {
31     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this));
32 }
33 
~RecorderCallbackNapi()34 RecorderCallbackNapi::~RecorderCallbackNapi()
35 {
36     MEDIA_LOGD("0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this));
37 }
38 
SaveCallbackReference(const std::string & name,std::weak_ptr<AutoRef> ref)39 void RecorderCallbackNapi::SaveCallbackReference(const std::string &name, std::weak_ptr<AutoRef> ref)
40 {
41     std::lock_guard<std::mutex> lock(mutex_);
42     refMap_[name] = ref;
43 }
44 
ClearCallbackReference()45 void RecorderCallbackNapi::ClearCallbackReference()
46 {
47     std::lock_guard<std::mutex> lock(mutex_);
48     refMap_.clear();
49 }
50 
SendErrorCallback(int32_t errCode)51 void RecorderCallbackNapi::SendErrorCallback(int32_t errCode)
52 {
53     std::lock_guard<std::mutex> lock(mutex_);
54     if (refMap_.find(ERROR_CALLBACK_NAME) == refMap_.end()) {
55         MEDIA_LOGW("can not find error callback!");
56         return;
57     }
58 
59     RecordJsCallback *cb = new(std::nothrow) RecordJsCallback();
60     CHECK_AND_RETURN_LOG(cb != nullptr, "cb is nullptr");
61     cb->autoRef = refMap_.at(ERROR_CALLBACK_NAME);
62     cb->callbackName = ERROR_CALLBACK_NAME;
63     if (isVideo_) {
64         cb->errorMsg = MSExtErrorAPI9ToString(static_cast<MediaServiceExtErrCodeAPI9>(errCode), "", "");
65     } else {
66         cb->errorMsg = MSExtErrorToString(static_cast<MediaServiceExtErrCode>(errCode));
67     }
68     cb->errorCode = errCode;
69     return OnJsErrorCallBack(cb);
70 }
71 
SendStateCallback(const std::string & callbackName)72 void RecorderCallbackNapi::SendStateCallback(const std::string &callbackName)
73 {
74     std::lock_guard<std::mutex> lock(mutex_);
75     if (refMap_.find(callbackName) == refMap_.end()) {
76         MEDIA_LOGW("can not find %{public}s callback!", callbackName.c_str());
77         return;
78     }
79 
80     RecordJsCallback *cb = new(std::nothrow) RecordJsCallback();
81     CHECK_AND_RETURN_LOG(cb != nullptr, "cb is nullptr");
82     cb->autoRef = refMap_.at(callbackName);
83     cb->callbackName = callbackName;
84     return OnJsStateCallBack(cb);
85 }
86 
OnError(RecorderErrorType errorType,int32_t errCode)87 void RecorderCallbackNapi::OnError(RecorderErrorType errorType, int32_t errCode)
88 {
89     MEDIA_LOGD("OnError is called, name: %{public}d, error message: %{public}d", errorType, errCode);
90     if (isVideo_) {
91         MediaServiceExtErrCode err = MSErrorToExtError(static_cast<MediaServiceErrCode>(errCode));
92         return SendErrorCallback(err);
93     } else {
94         MediaServiceExtErrCodeAPI9 err = MSErrorToExtErrorAPI9(static_cast<MediaServiceErrCode>(errCode));
95         return SendErrorCallback(err);
96     }
97 }
98 
OnInfo(int32_t type,int32_t extra)99 void RecorderCallbackNapi::OnInfo(int32_t type, int32_t extra)
100 {
101     MEDIA_LOGD("OnInfo() is called, type: %{public}d, extra: %{public}d", type, extra);
102 }
103 
OnAudioCaptureChange(const AudioRecorderChangeInfo & audioRecorderChangeInfo)104 void RecorderCallbackNapi::OnAudioCaptureChange(const AudioRecorderChangeInfo &audioRecorderChangeInfo)
105 {
106     (void)audioRecorderChangeInfo;
107 }
108 
OnJsStateCallBack(RecordJsCallback * jsCb) const109 void RecorderCallbackNapi::OnJsStateCallBack(RecordJsCallback *jsCb) const
110 {
111     auto task = [event = jsCb]() {
112         std::string request = event->callbackName;
113         do {
114             std::shared_ptr<AutoRef> ref = event->autoRef.lock();
115             CHECK_AND_BREAK_LOG(ref != nullptr, "%{public}s AutoRef is nullptr", request.c_str());
116 
117             napi_handle_scope scope = nullptr;
118             napi_open_handle_scope(ref->env_, &scope);
119             CHECK_AND_BREAK_LOG(scope != nullptr, "%{public}s scope is nullptr", request.c_str());
120             ON_SCOPE_EXIT(0) {
121                 napi_close_handle_scope(ref->env_, scope);
122             };
123 
124             napi_value jsCallback = nullptr;
125             napi_status nstatus = napi_get_reference_value(ref->env_, ref->cb_, &jsCallback);
126             CHECK_AND_BREAK_LOG(nstatus == napi_ok && jsCallback != nullptr, "%{public}s get reference value failed",
127                 request.c_str());
128 
129             napi_value result = nullptr;
130             nstatus = napi_call_function(ref->env_, nullptr, jsCallback, 0, nullptr, &result);
131             CHECK_AND_BREAK_LOG(nstatus == napi_ok, "%{public}s failed to napi call function", request.c_str());
132         } while (0);
133         delete event;
134     };
135     auto ret = napi_send_event(env_, task, napi_eprio_immediate);
136     CHECK_AND_RETURN_LOG(ret == napi_status::napi_ok, "failed to napi_send_event task");
137 }
138 
OnJsErrorCallBack(RecordJsCallback * jsCb) const139 void RecorderCallbackNapi::OnJsErrorCallBack(RecordJsCallback *jsCb) const
140 {
141     auto task = [event = jsCb]() {
142         std::string request = event->callbackName;
143         do {
144             std::shared_ptr<AutoRef> ref = event->autoRef.lock();
145             CHECK_AND_BREAK_LOG(ref != nullptr, "%{public}s AutoRef is nullptr", request.c_str());
146 
147             napi_handle_scope scope = nullptr;
148             napi_open_handle_scope(ref->env_, &scope);
149             CHECK_AND_BREAK_LOG(scope != nullptr, "%{public}s scope is nullptr", request.c_str());
150             ON_SCOPE_EXIT(0) { napi_close_handle_scope(ref->env_, scope); };
151 
152             napi_value jsCallback = nullptr;
153             napi_status nstatus = napi_get_reference_value(ref->env_, ref->cb_, &jsCallback);
154             CHECK_AND_BREAK_LOG(nstatus == napi_ok && jsCallback != nullptr, "%{public}s get reference value failed",
155                 request.c_str());
156 
157             napi_value msgValStr = nullptr;
158             nstatus = napi_create_string_utf8(ref->env_, event->errorMsg.c_str(), NAPI_AUTO_LENGTH, &msgValStr);
159             CHECK_AND_BREAK_LOG(nstatus == napi_ok && msgValStr != nullptr, "%{public}s failed to get error code value",
160                 request.c_str());
161 
162             napi_value args[1] = { nullptr };
163             nstatus = napi_create_error(ref->env_, nullptr, msgValStr, &args[0]);
164             CHECK_AND_BREAK_LOG(nstatus == napi_ok && args[0] != nullptr, "%{public}s failed to create error callback",
165                 request.c_str());
166 
167             nstatus = CommonNapi::FillErrorArgs(ref->env_, event->errorCode, args[0]);
168             CHECK_AND_BREAK_LOG(nstatus == napi_ok, "create error callback failed");
169 
170             // Call back function
171             napi_value result = nullptr;
172             nstatus = napi_call_function(ref->env_, nullptr, jsCallback, 1, args, &result);
173             CHECK_AND_BREAK_LOG(nstatus == napi_ok, "%{public}s failed to napi call function", request.c_str());
174         } while (0);
175         delete event;
176     };
177     auto ret = napi_send_event(env_, task, napi_eprio_immediate);
178     CHECK_AND_RETURN_LOG(ret == napi_status::napi_ok, "failed to napi_send_event task");
179 }
180 } // namespace Media
181 } // namespace OHOS