1 /*
2 * Copyright (c) 2025 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 "cj_avtranscoder_callback.h"
17 #include "media_core.h"
18 #include "scope_guard.h"
19 #include "cj_lambda.h"
20
21 namespace OHOS {
22 namespace Media {
23
24 std::unordered_map<CJAVTranscoderEvent, const char *, EnumClassHash> EVENT2CSTR = {
25 {CJAVTranscoderEvent::EVENT_PROGRESS_UPDATE, "progressUpdate"},
26 {CJAVTranscoderEvent::EVENT_COMPLETE, "complete"},
27 {CJAVTranscoderEvent::EVENT_ERROR, "error"}
28 };
29
CJAVTranscoderCallback()30 CJAVTranscoderCallback::CJAVTranscoderCallback()
31 {
32 MEDIA_LOGI("0x%{public}06" PRIXPTR "Instances create", FAKE_POINTER(this));
33 }
34
~CJAVTranscoderCallback()35 CJAVTranscoderCallback::~CJAVTranscoderCallback()
36 {
37 MEDIA_LOGI("0x%{public}06" PRIXPTR "Instances destroy", FAKE_POINTER(this));
38 }
39
SaveCallbackReference(CJAVTranscoderEvent event,int64_t callbackId)40 void CJAVTranscoderCallback::SaveCallbackReference(CJAVTranscoderEvent event, int64_t callbackId)
41 {
42 std::lock_guard<std::mutex> lock(mutex_);
43 if (event == CJAVTranscoderEvent::EVENT_PROGRESS_UPDATE) {
44 auto func = reinterpret_cast<void (*)(int32_t)>(callbackId);
45 onprogressfunc = [lambda = CJLambda::Create(func)](int32_t progress) {
46 lambda(progress);
47 };
48 } else if (event == CJAVTranscoderEvent::EVENT_COMPLETE) {
49 auto func = reinterpret_cast<void (*)(void)>(callbackId);
50 oncompletefunc = [lambda = CJLambda::Create(func)]() {
51 lambda();
52 };
53 } else if (event == CJAVTranscoderEvent::EVENT_ERROR) {
54 auto func = reinterpret_cast<void (*)(int32_t, const char*)>(callbackId);
55 onerrorfunc = [lambda = CJLambda::Create(func)](int32_t errCode, const char* msg) {
56 lambda(errCode, msg);
57 };
58 }
59
60 if (EVENT2CSTR.find(event) != EVENT2CSTR.end()) {
61 MEDIA_LOGI("Set callback type: %{public}s", EVENT2CSTR.at(event));
62 } else {
63 MEDIA_LOGW("event %{public}d is not in EVENT2CSTR", static_cast<int32_t>(event));
64 }
65 }
66
CancelCallbackReference(CJAVTranscoderEvent event)67 void CJAVTranscoderCallback::CancelCallbackReference(CJAVTranscoderEvent event)
68 {
69 std::lock_guard<std::mutex> lock(mutex_);
70 if (event == CJAVTranscoderEvent::EVENT_PROGRESS_UPDATE) {
71 onprogressfunc = nullptr;
72 } else if (event == CJAVTranscoderEvent::EVENT_COMPLETE) {
73 oncompletefunc = nullptr;
74 } else if (event == CJAVTranscoderEvent::EVENT_ERROR) {
75 onerrorfunc = nullptr;
76 }
77
78 if (EVENT2CSTR.find(event) != EVENT2CSTR.end()) {
79 MEDIA_LOGI("Cancel callback type: %{public}s", EVENT2CSTR.at(event));
80 } else {
81 MEDIA_LOGW("event %{public}d is not in EVENT2CSTR", static_cast<int32_t>(event));
82 }
83 }
84
ClearCallbackReference()85 void CJAVTranscoderCallback::ClearCallbackReference()
86 {
87 std::lock_guard<std::mutex> lock(mutex_);
88 onprogressfunc = nullptr;
89 oncompletefunc = nullptr;
90 onerrorfunc = nullptr;
91 MEDIA_LOGI("ClearCallback!");
92 }
93
SendErrorCallback(int32_t errCode,const std::string & msg)94 void CJAVTranscoderCallback::SendErrorCallback(int32_t errCode, const std::string &msg)
95 {
96 std::lock_guard<std::mutex> lock(mutex_);
97 if (onerrorfunc == nullptr) {
98 MEDIA_LOGW("can not find error callback!");
99 return;
100 }
101 onerrorfunc(errCode, msg.c_str());
102 }
103
SendStateCallback(CjAVTransCoderState state,const StateChangeReason & reason)104 void CJAVTranscoderCallback::SendStateCallback(CjAVTransCoderState state, const StateChangeReason &reason)
105 {
106 std::lock_guard<std::mutex> lock(mutex_);
107 currentState_ = state;
108 }
109
SendCompleteCallback()110 void CJAVTranscoderCallback::SendCompleteCallback()
111 {
112 std::lock_guard<std::mutex> lock(mutex_);
113 if (oncompletefunc == nullptr) {
114 MEDIA_LOGW("can not find complete callback!");
115 return;
116 }
117 oncompletefunc();
118 }
119
SendProgressUpdateCallback(int32_t progress)120 void CJAVTranscoderCallback::SendProgressUpdateCallback(int32_t progress)
121 {
122 std::lock_guard<std::mutex> lock(mutex_);
123 if (onprogressfunc == nullptr) {
124 MEDIA_LOGW("can not find progress callback!");
125 return;
126 }
127 onprogressfunc(progress);
128 }
129
GetState()130 CjAVTransCoderState CJAVTranscoderCallback::GetState()
131 {
132 std::lock_guard<std::mutex> lock(mutex_);
133 return currentState_;
134 }
135
OnError(int32_t errCode,const std::string & errorMsg)136 void CJAVTranscoderCallback::OnError(int32_t errCode, const std::string &errorMsg)
137 {
138 MediaServiceExtErrCodeAPI9 exErr = MSErrorToExtErrorAPI9(static_cast<MediaServiceErrCode>(errCode));
139 MEDIA_LOGE("CJAVTranscoderCallback::OnError: %{public}d, %{public}s", exErr, errorMsg.c_str());
140 SendErrorCallback(exErr, errorMsg);
141 SendStateCallback(CjAVTransCoderState::STATE_ERROR, StateChangeReason::BACKGROUND);
142 }
143
OnInfo(int32_t type,int32_t extra)144 void CJAVTranscoderCallback::OnInfo(int32_t type, int32_t extra)
145 {
146 if (type == TransCoderOnInfoType::INFO_TYPE_TRANSCODER_COMPLETED) {
147 SendCompleteCallback();
148 } else if (type == TransCoderOnInfoType::INFO_TYPE_PROGRESS_UPDATE) {
149 SendProgressUpdateCallback(extra);
150 }
151 }
152 }
153 }
154