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 #include "avtranscoder_callback.h"
16 #include <uv.h>
17 #include "media_errors.h"
18 #include "scope_guard.h"
19 #include "media_log.h"
20
21 namespace {
22 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_PLAYER, "AVTransCoderCallback"};
23 }
24
25 namespace OHOS {
26 namespace Media {
AVTransCoderCallback(napi_env env)27 AVTransCoderCallback::AVTransCoderCallback(napi_env env) : env_(env)
28 {
29 MEDIA_LOGI("0x%{public}06" PRIXPTR "Instances create", FAKE_POINTER(this));
30 }
31
~AVTransCoderCallback()32 AVTransCoderCallback::~AVTransCoderCallback()
33 {
34 MEDIA_LOGI("0x%{public}06" PRIXPTR "Instances destroy", FAKE_POINTER(this));
35 }
36
SaveCallbackReference(const std::string & name,std::weak_ptr<AutoRef> ref)37 void AVTransCoderCallback::SaveCallbackReference(const std::string &name, std::weak_ptr<AutoRef> ref)
38 {
39 std::lock_guard<std::mutex> lock(mutex_);
40 refMap_[name] = ref;
41 MEDIA_LOGI("Set callback type: %{public}s", name.c_str());
42 }
43
CancelCallbackReference(const std::string & name)44 void AVTransCoderCallback::CancelCallbackReference(const std::string &name)
45 {
46 std::lock_guard<std::mutex> lock(mutex_);
47 auto iter = refMap_.find(name);
48 if (iter != refMap_.end()) {
49 refMap_.erase(iter);
50 }
51 MEDIA_LOGI("Cancel callback type: %{public}s", name.c_str());
52 }
53
ClearCallbackReference()54 void AVTransCoderCallback::ClearCallbackReference()
55 {
56 std::lock_guard<std::mutex> lock(mutex_);
57 refMap_.clear();
58 MEDIA_LOGI("ClearCallback!");
59 }
60
SendErrorCallback(MediaServiceExtErrCodeAPI9 errCode,const std::string & msg)61 void AVTransCoderCallback::SendErrorCallback(MediaServiceExtErrCodeAPI9 errCode, const std::string &msg)
62 {
63 std::string message = MSExtAVErrorToString(errCode) + msg;
64 MEDIA_LOGE("SendErrorCallback:errorCode %{public}d, errorMsg %{public}s", errCode, message.c_str());
65 std::lock_guard<std::mutex> lock(mutex_);
66 if (refMap_.find(AVTransCoderEvent::EVENT_ERROR) == refMap_.end()) {
67 MEDIA_LOGW("can not find error callback!");
68 return;
69 }
70
71 AVTransCoderJsCallback *cb = new(std::nothrow) AVTransCoderJsCallback();
72 CHECK_AND_RETURN_LOG(cb != nullptr, "cb is nullptr");
73 cb->autoRef = refMap_.at(AVTransCoderEvent::EVENT_ERROR);
74 cb->callbackName = AVTransCoderEvent::EVENT_ERROR;
75 cb->errorCode = errCode;
76 cb->errorMsg = message;
77 return OnJsErrorCallBack(cb);
78 }
79
SendStateCallback(const std::string & state,const StateChangeReason & reason)80 void AVTransCoderCallback::SendStateCallback(const std::string &state, const StateChangeReason &reason)
81 {
82 std::lock_guard<std::mutex> lock(mutex_);
83 MEDIA_LOGI("StateChange, currentState: %{public}s to state: %{public}s", currentState_.c_str(), state.c_str());
84 currentState_ = state;
85 }
86
SendCompleteCallback()87 void AVTransCoderCallback::SendCompleteCallback()
88 {
89 std::lock_guard<std::mutex> lock(mutex_);
90 if (refMap_.find(AVTransCoderEvent::EVENT_COMPLETE) == refMap_.end()) {
91 MEDIA_LOGW("can not find statechange callback!");
92 return;
93 }
94
95 AVTransCoderJsCallback *cb = new(std::nothrow) AVTransCoderJsCallback();
96 CHECK_AND_RETURN_LOG(cb != nullptr, "cb is nullptr");
97 cb->autoRef = refMap_.at(AVTransCoderEvent::EVENT_COMPLETE);
98 cb->callbackName = AVTransCoderEvent::EVENT_COMPLETE;
99 return OnJsCompleteCallBack(cb);
100 }
101
SendProgressUpdateCallback(int32_t progress)102 void AVTransCoderCallback::SendProgressUpdateCallback(int32_t progress)
103 {
104 std::lock_guard<std::mutex> lock(mutex_);
105 if (refMap_.find(AVTransCoderEvent::EVENT_PROGRESS_UPDATE) == refMap_.end()) {
106 MEDIA_LOGW("can not find progressupdate callback!");
107 return;
108 }
109 AVTransCoderJsCallback *cb = new(std::nothrow) AVTransCoderJsCallback();
110 CHECK_AND_RETURN_LOG(cb != nullptr, "cb is nullptr");
111 cb->autoRef = refMap_.at(AVTransCoderEvent::EVENT_PROGRESS_UPDATE);
112 cb->callbackName = AVTransCoderEvent::EVENT_PROGRESS_UPDATE;
113 cb->progress = progress;
114 return OnJsProgressUpdateCallback(cb);
115 }
116
OnError(int32_t errCode,const std::string & errorMsg)117 void AVTransCoderCallback::OnError(int32_t errCode, const std::string &errorMsg)
118 {
119 MEDIA_LOGE("AVTransCoderCallback::OnError: %{public}d, %{public}s", errCode, errorMsg.c_str());
120 MediaServiceExtErrCodeAPI9 errorCodeApi9 = MSErrorToExtErrorAPI9(static_cast<MediaServiceErrCode>(errCode));
121 SendErrorCallback(errorCodeApi9, errorMsg);
122 SendStateCallback(AVTransCoderState::STATE_ERROR, StateChangeReason::BACKGROUND);
123 }
124
OnInfo(int32_t type,int32_t extra)125 void AVTransCoderCallback::OnInfo(int32_t type, int32_t extra)
126 {
127 if (type == TransCoderOnInfoType::INFO_TYPE_TRANSCODER_COMPLETED) {
128 SendCompleteCallback();
129 } else if (type == TransCoderOnInfoType::INFO_TYPE_PROGRESS_UPDATE) {
130 SendProgressUpdateCallback(extra);
131 }
132 }
133
OnJsCompleteCallBack(AVTransCoderJsCallback * jsCb) const134 void AVTransCoderCallback::OnJsCompleteCallBack(AVTransCoderJsCallback *jsCb) const
135 {
136 auto task = [event = jsCb]() {
137 CHECK_AND_RETURN_LOG(event != nullptr, "jsCb is nullptr");
138 std::string request = event->callbackName;
139 MEDIA_LOGI("uv_queue_work_with_qos start, state changes to %{public}s", event->state.c_str());
140 do {
141 std::shared_ptr<AutoRef> ref = event->autoRef.lock();
142 CHECK_AND_BREAK_LOG(ref != nullptr, "%{public}s AutoRef is nullptr", request.c_str());
143
144 napi_handle_scope scope = nullptr;
145 napi_open_handle_scope(ref->env_, &scope);
146 CHECK_AND_BREAK_LOG(scope != nullptr, "%{public}s scope is nullptr", request.c_str());
147 ON_SCOPE_EXIT(0) {
148 napi_close_handle_scope(ref->env_, scope);
149 };
150
151 napi_value jsCallback = nullptr;
152 napi_status nstatus = napi_get_reference_value(ref->env_, ref->cb_, &jsCallback);
153 CHECK_AND_BREAK_LOG(nstatus == napi_ok && jsCallback != nullptr, "%{public}s get reference value fail",
154 request.c_str());
155
156 napi_value args[2] = { nullptr };
157
158 const size_t argCount = 0;
159 napi_value result = nullptr;
160 nstatus = napi_call_function(ref->env_, nullptr, jsCallback, argCount, args, &result);
161 CHECK_AND_BREAK_LOG(nstatus == napi_ok, "%{public}s fail to napi call function", request.c_str());
162 } while (0);
163 delete event;
164 };
165
166 auto ret = napi_send_event(env_, task, napi_eprio_immediate);
167 if (ret != napi_status::napi_ok) {
168 MEDIA_LOGE("Failed to SendEvent, ret = %{public}d", ret);
169 delete jsCb;
170 }
171 }
172
OnJsProgressUpdateCallback(AVTransCoderJsCallback * jsCb) const173 void AVTransCoderCallback::OnJsProgressUpdateCallback(AVTransCoderJsCallback *jsCb) const
174 {
175 auto task = [event = jsCb]() {
176 CHECK_AND_RETURN_LOG(event != nullptr, "jsCb is nullptr");
177 std::string request = event->callbackName;
178 do {
179 std::shared_ptr<AutoRef> ref = event->autoRef.lock();
180 CHECK_AND_BREAK_LOG(ref != nullptr, "%{public}s AutoRef is nullptr", request.c_str());
181
182 napi_handle_scope scope = nullptr;
183 napi_open_handle_scope(ref->env_, &scope);
184 CHECK_AND_BREAK_LOG(scope != nullptr, "%{public}s scope is nullptr", request.c_str());
185 ON_SCOPE_EXIT(0) {
186 napi_close_handle_scope(ref->env_, scope);
187 };
188
189 napi_value jsCallback = nullptr;
190 napi_status nstatus = napi_get_reference_value(ref->env_, ref->cb_, &jsCallback);
191 CHECK_AND_BREAK_LOG(nstatus == napi_ok && jsCallback != nullptr, "%{public}s get reference value fail",
192 request.c_str());
193
194 napi_value args[1] = { nullptr };
195 nstatus = napi_create_int32(ref->env_, event->progress, &args[0]);
196 CHECK_AND_BREAK_LOG(nstatus == napi_ok && args[0] != nullptr,
197 "%{public}s fail to create callback", request.c_str());
198
199 const size_t argCount = 1;
200 napi_value result = nullptr;
201 nstatus = napi_call_function(ref->env_, nullptr, jsCallback, argCount, args, &result);
202 CHECK_AND_BREAK_LOG(nstatus == napi_ok, "%{public}s fail to napi call function", request.c_str());
203 } while (0);
204 delete event;
205 };
206
207 auto ret = napi_send_event(env_, task, napi_eprio_immediate);
208 if (ret != napi_status::napi_ok) {
209 MEDIA_LOGE("Failed to SendEvent, ret = %{public}d", ret);
210 delete jsCb;
211 }
212 }
213
GetState()214 std::string AVTransCoderCallback::GetState()
215 {
216 std::lock_guard<std::mutex> lock(mutex_);
217 return currentState_;
218 }
219
OnJsErrorCallBack(AVTransCoderJsCallback * jsCb) const220 void AVTransCoderCallback::OnJsErrorCallBack(AVTransCoderJsCallback *jsCb) const
221 {
222 auto task = [event = jsCb]() {
223 CHECK_AND_RETURN_LOG(event != nullptr, "jsCb is nullptr");
224 std::string request = event->callbackName;
225 MEDIA_LOGI("uv_queue_work_with_qos start, errorcode:%{public}d , errormessage:%{public}s:",
226 event->errorCode, event->errorMsg.c_str());
227 do {
228 std::shared_ptr<AutoRef> ref = event->autoRef.lock();
229 CHECK_AND_BREAK_LOG(ref != nullptr, "%{public}s AutoRef is nullptr", request.c_str());
230
231 napi_handle_scope scope = nullptr;
232 napi_open_handle_scope(ref->env_, &scope);
233 CHECK_AND_BREAK_LOG(scope != nullptr, "%{public}s scope is nullptr", request.c_str());
234 ON_SCOPE_EXIT(0) {
235 napi_close_handle_scope(ref->env_, scope);
236 };
237
238 napi_value jsCallback = nullptr;
239 napi_status nstatus = napi_get_reference_value(ref->env_, ref->cb_, &jsCallback);
240 CHECK_AND_BREAK_LOG(nstatus == napi_ok && jsCallback != nullptr, "%{public}s get reference value fail",
241 request.c_str());
242
243 napi_value msgValStr = nullptr;
244 nstatus = napi_create_string_utf8(ref->env_, event->errorMsg.c_str(), NAPI_AUTO_LENGTH, &msgValStr);
245 CHECK_AND_BREAK_LOG(nstatus == napi_ok && msgValStr != nullptr, "create error message str fail");
246
247 napi_value args[1] = { nullptr };
248 nstatus = napi_create_error(ref->env_, nullptr, msgValStr, &args[0]);
249 CHECK_AND_BREAK_LOG(nstatus == napi_ok && args[0] != nullptr, "create error callback fail");
250
251 nstatus = CommonNapi::FillErrorArgs(ref->env_, event->errorCode, args[0]);
252 CHECK_AND_BREAK_LOG(nstatus == napi_ok, "create error callback fail");
253
254 // Call back function
255 napi_value result = nullptr;
256 nstatus = napi_call_function(ref->env_, nullptr, jsCallback, 1, args, &result);
257 CHECK_AND_BREAK_LOG(nstatus == napi_ok, "%{public}s fail to napi call function", request.c_str());
258 } while (0);
259 delete event;
260 };
261
262 auto ret = napi_send_event(env_, task, napi_eprio_immediate);
263 if (ret != napi_status::napi_ok) {
264 MEDIA_LOGE("Failed to SendEvent, ret = %{public}d", ret);
265 delete jsCb;
266 }
267 }
268 } // namespace Media
269 } // namespace OHOS