• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "avscreen_capture_callback.h"
17 #include "scope_guard.h"
18 #include "media_log.h"
19 
20 namespace {
21     constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_SCREENCAPTURE, "AVScreenCaptureCallback"};
22 }
23 
24 namespace OHOS {
25 namespace Media {
AVScreenCaptureCallback(napi_env env)26 AVScreenCaptureCallback::AVScreenCaptureCallback(napi_env env) : env_(env)
27 {
28     MEDIA_LOGI("0x%{public}06" PRIXPTR "Instances create", FAKE_POINTER(this));
29 }
30 
~AVScreenCaptureCallback()31 AVScreenCaptureCallback::~AVScreenCaptureCallback()
32 {
33     MEDIA_LOGI("0x%{public}06" PRIXPTR "Instances destroy", FAKE_POINTER(this));
34 }
35 
SendErrorCallback(int32_t errCode,const std::string & msg)36 void AVScreenCaptureCallback::SendErrorCallback(int32_t errCode, const std::string &msg)
37 {
38     std::lock_guard<std::mutex> lock(mutex_);
39     if (refMap_.find(AVScreenCaptureEvent::EVENT_ERROR) == refMap_.end()) {
40         MEDIA_LOGW("can not find error callback!");
41         return;
42     }
43 
44     AVScreenCaptureJsCallback *cb = new(std::nothrow) AVScreenCaptureJsCallback();
45     CHECK_AND_RETURN_LOG(cb != nullptr, "cb is nullptr");
46     cb->autoRef = refMap_.at(AVScreenCaptureEvent::EVENT_ERROR);
47     cb->callbackName = AVScreenCaptureEvent::EVENT_ERROR;
48     cb->errorCode = errCode;
49     cb->errorMsg = msg;
50     return OnJsErrorCallBack(cb);
51 }
52 
SendStateCallback(AVScreenCaptureStateCode stateCode)53 void AVScreenCaptureCallback::SendStateCallback(AVScreenCaptureStateCode stateCode)
54 {
55     std::lock_guard<std::mutex> lock(mutex_);
56     if (refMap_.find(AVScreenCaptureEvent::EVENT_STATE_CHANGE) == refMap_.end()) {
57         MEDIA_LOGW("can not find stateChange callback!");
58         return;
59     }
60 
61     AVScreenCaptureJsCallback *cb = new(std::nothrow) AVScreenCaptureJsCallback();
62     CHECK_AND_RETURN_LOG(cb != nullptr, "cb is nullptr");
63     cb->autoRef = refMap_.at(AVScreenCaptureEvent::EVENT_STATE_CHANGE);
64     cb->callbackName = AVScreenCaptureEvent::EVENT_STATE_CHANGE;
65     cb->stateCode = stateCode;
66     return OnJsStateChangeCallBack(cb);
67 }
68 
SaveCallbackReference(const std::string & name,std::weak_ptr<AutoRef> ref)69 void AVScreenCaptureCallback::SaveCallbackReference(const std::string &name, std::weak_ptr<AutoRef> ref)
70 {
71     std::lock_guard<std::mutex> lock(mutex_);
72     refMap_[name] = ref;
73     MEDIA_LOGI("Set callback type: %{public}s", name.c_str());
74 }
75 
CancelCallbackReference(const std::string & name)76 void AVScreenCaptureCallback::CancelCallbackReference(const std::string &name)
77 {
78     std::lock_guard<std::mutex> lock(mutex_);
79     auto iter = refMap_.find(name);
80     if (iter != refMap_.end()) {
81         refMap_.erase(iter);
82     }
83     MEDIA_LOGI("Cancel callback type: %{public}s", name.c_str());
84 }
85 
ClearCallbackReference()86 void AVScreenCaptureCallback::ClearCallbackReference()
87 {
88     std::lock_guard<std::mutex> lock(mutex_);
89     refMap_.clear();
90     MEDIA_LOGI("ClearCallback!");
91 }
92 
OnError(ScreenCaptureErrorType errorType,int32_t errorCode)93 void AVScreenCaptureCallback::OnError(ScreenCaptureErrorType errorType, int32_t errorCode)
94 {
95     MEDIA_LOGI("OnError is called, name: %{public}d, error message: %{public}d", errorType, errorCode);
96     SendErrorCallback(errorCode, "Screen capture failed.");
97 }
98 
OnStateChange(AVScreenCaptureStateCode stateCode)99 void AVScreenCaptureCallback::OnStateChange(AVScreenCaptureStateCode stateCode)
100 {
101     MEDIA_LOGI("OnStateChange() is called, stateCode: %{public}d", stateCode);
102     SendStateCallback(stateCode);
103 }
104 
OnJsErrorCallBack(AVScreenCaptureJsCallback * jsCb) const105 void AVScreenCaptureCallback::OnJsErrorCallBack(AVScreenCaptureJsCallback *jsCb) const
106 {
107     ON_SCOPE_EXIT(0) {
108         delete jsCb;
109     };
110 
111     auto task = [jsCb]() {
112         std::string request = jsCb->callbackName;
113         MEDIA_LOGI("OnJsErrorCallBack task start, errorcode:%{public}d , errormessage:%{public}s:",
114             jsCb->errorCode, jsCb->errorMsg.c_str());
115         do {
116             std::shared_ptr<AutoRef> ref = jsCb->autoRef.lock();
117             CHECK_AND_BREAK_LOG(ref != nullptr, "%{public}s AutoRef is nullptr", request.c_str());
118 
119             napi_handle_scope scope = nullptr;
120             napi_open_handle_scope(ref->env_, &scope);
121             CHECK_AND_BREAK_LOG(scope != nullptr, "%{public}s scope is nullptr", request.c_str());
122             ON_SCOPE_EXIT(0) {
123                 napi_close_handle_scope(ref->env_, scope);
124             };
125 
126             napi_value jsCallback = nullptr;
127             napi_status nstatus = napi_get_reference_value(ref->env_, ref->cb_, &jsCallback);
128             CHECK_AND_BREAK_LOG(nstatus == napi_ok && jsCallback != nullptr, "%{public}s get reference value fail",
129                 request.c_str());
130 
131             napi_value msgValStr = nullptr;
132             nstatus = napi_create_string_utf8(ref->env_, jsCb->errorMsg.c_str(), NAPI_AUTO_LENGTH, &msgValStr);
133             CHECK_AND_BREAK_LOG(nstatus == napi_ok && msgValStr != nullptr, "create error message str fail");
134 
135             napi_value args[1] = { nullptr };
136             nstatus = napi_create_error(ref->env_, nullptr, msgValStr, &args[0]);
137             CHECK_AND_BREAK_LOG(nstatus == napi_ok && args[0] != nullptr, "create error callback fail");
138 
139             nstatus = CommonNapi::FillErrorArgs(ref->env_, jsCb->errorCode, args[0]);
140             CHECK_AND_BREAK_LOG(nstatus == napi_ok, "create error callback fail");
141 
142             // Call back function
143             napi_value result = nullptr;
144             nstatus = napi_call_function(ref->env_, nullptr, jsCallback, 1, args, &result);
145             CHECK_AND_BREAK_LOG(nstatus == napi_ok, "%{public}s fail to napi call function", request.c_str());
146         } while (0);
147         delete jsCb;
148     };
149     CHECK_AND_RETURN_LOG(napi_send_event(env_, task, napi_eprio_immediate) == napi_status::napi_ok,
150         "OnJsErrorCallBack napi_send_event failed");
151 
152     CANCEL_SCOPE_EXIT_GUARD(0);
153 }
154 
OnJsStateChangeCallBack(AVScreenCaptureJsCallback * jsCb) const155 void AVScreenCaptureCallback::OnJsStateChangeCallBack(AVScreenCaptureJsCallback *jsCb) const
156 {
157     ON_SCOPE_EXIT(0) {
158         delete jsCb;
159     };
160 
161     auto task = [jsCb]() {
162         std::string request = jsCb->callbackName;
163         do {
164             std::shared_ptr<AutoRef> ref = jsCb->autoRef.lock();
165             CHECK_AND_BREAK_LOG(ref != nullptr, "%{public}s AutoRef is nullptr", request.c_str());
166 
167             napi_handle_scope scope = nullptr;
168             napi_open_handle_scope(ref->env_, &scope);
169             CHECK_AND_BREAK_LOG(scope != nullptr, "%{public}s scope is nullptr", request.c_str());
170             ON_SCOPE_EXIT(0) {
171                 napi_close_handle_scope(ref->env_, scope);
172             };
173 
174             napi_value jsCallback = nullptr;
175             napi_status nstatus = napi_get_reference_value(ref->env_, ref->cb_, &jsCallback);
176             CHECK_AND_BREAK_LOG(nstatus == napi_ok && jsCallback != nullptr, "%{public}s get reference value fail",
177                 request.c_str());
178 
179             napi_value args[1] = { nullptr };
180             nstatus = napi_create_int32(ref->env_, jsCb->stateCode, &args[0]);
181             CHECK_AND_BREAK_LOG(nstatus == napi_ok && args[0] != nullptr,
182                 "%{public}s fail to create callback", request.c_str());
183 
184             const size_t argCount = 1;
185             napi_value result = nullptr;
186             nstatus = napi_call_function(ref->env_, nullptr, jsCallback, argCount, args, &result);
187             CHECK_AND_BREAK_LOG(nstatus == napi_ok, "%{public}s fail to napi call function", request.c_str());
188         } while (0);
189         delete jsCb;
190     };
191     CHECK_AND_RETURN_LOG(napi_send_event(env_, task, napi_eprio_immediate) == napi_status::napi_ok,
192         "OnJsStateChangeCallBack napi_send_event failed");
193 
194     CANCEL_SCOPE_EXIT_GUARD(0);
195 }
196 
197 } // namespace Media
198 } // namespace OHOS