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 #include "avtranscoder_ffi.h"
16 #include "cj_avtranscoder.h"
17 #include "media_log.h"
18
19 namespace OHOS {
20 namespace Media {
21 using namespace OHOS::FFI;
22
23 extern "C" {
FfiAVTranscoderCreateAVTranscoder(int32_t * errCode)24 int64_t FfiAVTranscoderCreateAVTranscoder(int32_t* errCode)
25 {
26 if (errCode == nullptr) {
27 MEDIA_LOGE("[CJAVTranscoder] errCode is nullptr!");
28 return INVALID_ID;
29 }
30 return CJAVTranscoder::CreateAVTranscoder(errCode);
31 }
32
FfiAVTranscoderPrepare(int64_t id,CAVTransCoderConfig config)33 int32_t FfiAVTranscoderPrepare(int64_t id, CAVTransCoderConfig config)
34 {
35 auto cjAVTranscoder = FFIData::GetData<CJAVTranscoder>(id);
36 if (cjAVTranscoder == nullptr) {
37 MEDIA_LOGE("[CJAVTranscoder] instance is nullptr!");
38 return MSERR_EXT_API9_NO_MEMORY;
39 }
40
41 std::shared_ptr<TransCoder> transCoder = cjAVTranscoder->transCoder_;
42 if (!transCoder) {
43 MEDIA_LOGE("[CJAVTranscoder] transCoder_ is nullptr!");
44 return MSERR_EXT_API9_NO_MEMORY;
45 }
46
47 return cjAVTranscoder->Prepare(transCoder, config);
48 }
49
FfiAVTranscoderStart(int64_t id)50 int32_t FfiAVTranscoderStart(int64_t id)
51 {
52 auto cjAVTranscoder = FFIData::GetData<CJAVTranscoder>(id);
53 if (cjAVTranscoder == nullptr) {
54 MEDIA_LOGE("[CJAVTranscoder] instance is nullptr!");
55 return MSERR_EXT_API9_NO_MEMORY;
56 }
57
58 std::shared_ptr<TransCoder> transCoder = cjAVTranscoder->transCoder_;
59 if (!transCoder) {
60 MEDIA_LOGE("[CJAVTranscoder] transCoder_ is nullptr!");
61 return MSERR_EXT_API9_NO_MEMORY;
62 }
63
64 return cjAVTranscoder->Start(transCoder);
65 }
66
FfiAVTranscoderPause(int64_t id)67 int32_t FfiAVTranscoderPause(int64_t id)
68 {
69 auto cjAVTranscoder = FFIData::GetData<CJAVTranscoder>(id);
70 if (cjAVTranscoder == nullptr) {
71 MEDIA_LOGE("[CJAVTranscoder] instance is nullptr!");
72 return MSERR_EXT_API9_NO_MEMORY;
73 }
74
75 std::shared_ptr<TransCoder> transCoder = cjAVTranscoder->transCoder_;
76 if (!transCoder) {
77 MEDIA_LOGE("[CJAVTranscoder] transCoder_ is nullptr!");
78 return MSERR_EXT_API9_NO_MEMORY;
79 }
80
81 return cjAVTranscoder->Pause(transCoder);
82 }
83
FfiAVTranscoderResume(int64_t id)84 int32_t FfiAVTranscoderResume(int64_t id)
85 {
86 auto cjAVTranscoder = FFIData::GetData<CJAVTranscoder>(id);
87 if (cjAVTranscoder == nullptr) {
88 MEDIA_LOGE("[CJAVTranscoder] instance is nullptr!");
89 return MSERR_EXT_API9_NO_MEMORY;
90 }
91
92 std::shared_ptr<TransCoder> transCoder = cjAVTranscoder->transCoder_;
93 if (!transCoder) {
94 MEDIA_LOGE("[CJAVTranscoder] transCoder_ is nullptr!");
95 return MSERR_EXT_API9_NO_MEMORY;
96 }
97
98 return cjAVTranscoder->Resume(transCoder);
99 }
100
FfiAVTranscoderCancel(int64_t id)101 int32_t FfiAVTranscoderCancel(int64_t id)
102 {
103 auto cjAVTranscoder = FFIData::GetData<CJAVTranscoder>(id);
104 if (cjAVTranscoder == nullptr) {
105 MEDIA_LOGE("[CJAVTranscoder] instance is nullptr!");
106 return MSERR_EXT_API9_NO_MEMORY;
107 }
108
109 std::shared_ptr<TransCoder> transCoder = cjAVTranscoder->transCoder_;
110 if (!transCoder) {
111 MEDIA_LOGE("[CJAVTranscoder] transCoder_ is nullptr!");
112 return MSERR_EXT_API9_NO_MEMORY;
113 }
114
115 return cjAVTranscoder->Cancel(transCoder);
116 }
117
FfiAVTranscoderRelease(int64_t id)118 int32_t FfiAVTranscoderRelease(int64_t id)
119 {
120 auto cjAVTranscoder = FFIData::GetData<CJAVTranscoder>(id);
121 if (cjAVTranscoder == nullptr) {
122 MEDIA_LOGE("[CJAVTranscoder] instance is nullptr!");
123 return MSERR_EXT_API9_NO_MEMORY;
124 }
125
126 std::shared_ptr<TransCoder> transCoder = cjAVTranscoder->transCoder_;
127 if (!transCoder) {
128 MEDIA_LOGE("[CJAVTranscoder] transCoder_ is nullptr!");
129 return MSERR_EXT_API9_NO_MEMORY;
130 }
131
132 return cjAVTranscoder->Release(transCoder);
133 }
134
FfiAVTranscoderGetFdSrc(int64_t id,int32_t * errCode)135 CAVFileDescriptor FfiAVTranscoderGetFdSrc(int64_t id, int32_t* errCode)
136 {
137 if (errCode == nullptr) {
138 MEDIA_LOGE("[CJAVTranscoder] errCode is nullptr!");
139 return CAVFileDescriptor{0};
140 }
141 auto cjAVTranscoder = FFIData::GetData<CJAVTranscoder>(id);
142 if (cjAVTranscoder == nullptr) {
143 *errCode = MSERR_EXT_API9_NO_MEMORY;
144 return CAVFileDescriptor{0};
145 }
146 *errCode = MSERR_EXT_API9_OK;
147 return cjAVTranscoder->GetInputFile();
148 }
149
FfiAVTranscoderSetFdSrc(int64_t id,CAVFileDescriptor fdSrc)150 int32_t FfiAVTranscoderSetFdSrc(int64_t id, CAVFileDescriptor fdSrc)
151 {
152 auto cjAVTranscoder = FFIData::GetData<CJAVTranscoder>(id);
153 if (cjAVTranscoder == nullptr) {
154 return MSERR_EXT_API9_NO_MEMORY;
155 }
156
157 std::shared_ptr<TransCoder> transCoder = cjAVTranscoder->transCoder_;
158 if (!transCoder) {
159 MEDIA_LOGE("[CJAVTranscoder] transCoder_ is nullptr!");
160 return MSERR_EXT_API9_NO_MEMORY;
161 }
162
163 return cjAVTranscoder->SetInputFile(transCoder, fdSrc);
164 }
165
FfiAVTranscoderGetFdDst(int64_t id,int32_t * errCode)166 int32_t FfiAVTranscoderGetFdDst(int64_t id, int32_t* errCode)
167 {
168 if (errCode == nullptr) {
169 MEDIA_LOGE("[CJAVTranscoder] errCode is nullptr!");
170 return 0;
171 }
172 auto cjAVTranscoder = FFIData::GetData<CJAVTranscoder>(id);
173 if (cjAVTranscoder == nullptr) {
174 *errCode = MSERR_EXT_API9_NO_MEMORY;
175 return 0;
176 }
177 *errCode = MSERR_EXT_API9_OK;
178 return cjAVTranscoder->GetOutputFile();
179 }
180
FfiAVTranscoderSetFdDst(int64_t id,int32_t fdDst)181 int32_t FfiAVTranscoderSetFdDst(int64_t id, int32_t fdDst)
182 {
183 auto cjAVTranscoder = FFIData::GetData<CJAVTranscoder>(id);
184 if (cjAVTranscoder == nullptr) {
185 return MSERR_EXT_API9_NO_MEMORY;
186 }
187
188 std::shared_ptr<TransCoder> transCoder = cjAVTranscoder->transCoder_;
189 if (!transCoder) {
190 MEDIA_LOGE("[CJAVTranscoder] transCoder_ is nullptr!");
191 return MSERR_EXT_API9_NO_MEMORY;
192 }
193
194 return cjAVTranscoder->SetOutputFile(transCoder, fdDst);
195 }
196
FfiAVTranscoderOnProgressUpdate(int64_t id,int64_t callbackId)197 int32_t FfiAVTranscoderOnProgressUpdate(int64_t id, int64_t callbackId)
198 {
199 auto cjAVTranscoder = FFIData::GetData<CJAVTranscoder>(id);
200 if (cjAVTranscoder == nullptr) {
201 MEDIA_LOGE("[CJAVTranscoder] instance is nullptr!");
202 return MSERR_EXT_API9_NO_MEMORY;
203 }
204 return cjAVTranscoder->OnProgressUpdate(callbackId);
205 }
206
FfiAVTranscoderOffProgressUpdate(int64_t id)207 int32_t FfiAVTranscoderOffProgressUpdate(int64_t id)
208 {
209 auto cjAVTranscoder = FFIData::GetData<CJAVTranscoder>(id);
210 if (cjAVTranscoder == nullptr) {
211 MEDIA_LOGE("[CJAVTranscoder] instance is nullptr!");
212 return MSERR_EXT_API9_NO_MEMORY;
213 }
214 return cjAVTranscoder->OffProgressUpdate();
215 }
216
FfiAVTranscoderOnComplete(int64_t id,int64_t callbackId)217 int32_t FfiAVTranscoderOnComplete(int64_t id, int64_t callbackId)
218 {
219 auto cjAVTranscoder = FFIData::GetData<CJAVTranscoder>(id);
220 if (cjAVTranscoder == nullptr) {
221 MEDIA_LOGE("[CJAVTranscoder] instance is nullptr!");
222 return MSERR_EXT_API9_NO_MEMORY;
223 }
224 return cjAVTranscoder->OnComplete(callbackId);
225 }
226
FfiAVTranscoderOffComplete(int64_t id)227 int32_t FfiAVTranscoderOffComplete(int64_t id)
228 {
229 auto cjAVTranscoder = FFIData::GetData<CJAVTranscoder>(id);
230 if (cjAVTranscoder == nullptr) {
231 MEDIA_LOGE("[CJAVTranscoder] instance is nullptr!");
232 return MSERR_EXT_API9_NO_MEMORY;
233 }
234 return cjAVTranscoder->OffComplete();
235 }
236
FfiAVTranscoderOnError(int64_t id,int64_t callbackId)237 int32_t FfiAVTranscoderOnError(int64_t id, int64_t callbackId)
238 {
239 auto cjAVTranscoder = FFIData::GetData<CJAVTranscoder>(id);
240 if (cjAVTranscoder == nullptr) {
241 MEDIA_LOGE("[CJAVTranscoder] instance is nullptr!");
242 return MSERR_EXT_API9_NO_MEMORY;
243 }
244 return cjAVTranscoder->OnError(callbackId);
245 }
246
FfiAVTranscoderOffError(int64_t id)247 int32_t FfiAVTranscoderOffError(int64_t id)
248 {
249 auto cjAVTranscoder = FFIData::GetData<CJAVTranscoder>(id);
250 if (cjAVTranscoder == nullptr) {
251 MEDIA_LOGE("[CJAVTranscoder] instance is nullptr!");
252 return MSERR_EXT_API9_NO_MEMORY;
253 }
254 return cjAVTranscoder->OffError();
255 }
256 }
257 }
258 }