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 <map>
17 #include <mutex>
18 #include <optional>
19 #include <string_view>
20
21 #include "avtranscoder.h"
22 #include "media_log.h"
23 #include "media_errors.h"
24 #include "native_player_magic.h"
25 #include "transcoder.h"
26
27 namespace {
28 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_PLAYER, "NativeAVTranscoder"};
29 constexpr uint32_t ERROR_EXT_API_MAP_LENGTH = 23;
30 }
31
32 using namespace OHOS;
33 using namespace OHOS::Media;
34 using namespace OHOS::MediaAVCodec;
35
36 constexpr int32_t AVTRANSCODER_DEFAULT_AUDIO_BIT_RATE = INT32_MAX;
37 constexpr int32_t AVTRANSCODER_DEFAULT_VIDEO_BIT_RATE = -1;
38 constexpr int32_t AVTRANSCODER_DEFAULT_FRAME_HEIGHT = -1;
39 constexpr int32_t AVTRANSCODER_DEFAULT_FRAME_WIDTH = -1;
40 constexpr int32_t AVTRANSCODER_DEFAULT_FD = -1;
41 constexpr int64_t AVTRANSCODER_DEFAULT_OFFSET = -1;
42 constexpr int64_t AVTRANSCODER_DEFAULT_LENGTH = -1;
43 constexpr int32_t MIN_PROGRESS = 0;
44 constexpr int32_t MAX_PROGRESS = 100;
45
46 namespace AVTranscoderOpts {
47 const std::string PREPARE = "Prepare";
48 const std::string START = "Start";
49 const std::string CANCEL = "Cancel";
50 const std::string RESUME = "Resume";
51 const std::string PAUSE = "Pause";
52 const std::string RELEASE = "Release";
53 }
54
55 namespace NativeTranscoderState {
56 const std::string IDLE = "idle";
57 const std::string PREPARED = "prepared";
58 const std::string STARTED = "started";
59 const std::string PAUSED = "paused";
60 const std::string CANCELLED = "cancelled";
61 const std::string COMPLETED = "completed";
62 const std::string RELEASED = "released";
63 const std::string ERROR = "error";
64 }
65
66
67 static const std::map<OH_AVTranscoder_State, std::string> STATE_MAP = {
68 {OH_AVTranscoder_State::AVTRANSCODER_STARTED, NativeTranscoderState::STARTED},
69 {OH_AVTranscoder_State::AVTRANSCODER_PREPARED, NativeTranscoderState::PREPARED},
70 {OH_AVTranscoder_State::AVTRANSCODER_PAUSED, NativeTranscoderState::PAUSED},
71 {OH_AVTranscoder_State::AVTRANSCODER_CANCELLED, NativeTranscoderState::CANCELLED},
72 {OH_AVTranscoder_State::AVTRANSCODER_COMPLETED, NativeTranscoderState::COMPLETED},
73 };
74
75 static const std::map<std::string, std::vector<std::string>> STATE_LIST = {
76 {NativeTranscoderState::IDLE, {
77 AVTranscoderOpts::PREPARE,
78 AVTranscoderOpts::RELEASE,
79 }},
80 {NativeTranscoderState::PREPARED, {
81 AVTranscoderOpts::START,
82 AVTranscoderOpts::RELEASE,
83 }},
84 {NativeTranscoderState::STARTED, {
85 AVTranscoderOpts::CANCEL,
86 AVTranscoderOpts::PAUSE,
87 AVTranscoderOpts::RELEASE,
88 AVTranscoderOpts::START,
89 AVTranscoderOpts::RESUME,
90 }},
91 {NativeTranscoderState::PAUSED, {
92 AVTranscoderOpts::START,
93 AVTranscoderOpts::PAUSE,
94 AVTranscoderOpts::RESUME,
95 AVTranscoderOpts::CANCEL,
96 AVTranscoderOpts::RELEASE,
97 }},
98 {NativeTranscoderState::CANCELLED, {
99 AVTranscoderOpts::RELEASE,
100 }},
101 {NativeTranscoderState::COMPLETED, {
102 AVTranscoderOpts::RELEASE,
103 }},
104 {NativeTranscoderState::RELEASED, {
105 AVTranscoderOpts::RELEASE,
106 }},
107 {NativeTranscoderState::ERROR, {
108 AVTranscoderOpts::RELEASE,
109 }},
110 };
111
112 const std::map<std::string, std::vector<std::string>> STATE_CTRL = {
113 {NativeTranscoderState::IDLE, {}},
114 {NativeTranscoderState::PREPARED, {}},
115 {NativeTranscoderState::STARTED, {
116 AVTranscoderOpts::START,
117 AVTranscoderOpts::RESUME
118 }},
119 {NativeTranscoderState::PAUSED, {
120 AVTranscoderOpts::PAUSE
121 }},
122 {NativeTranscoderState::CANCELLED, {
123 AVTranscoderOpts::CANCEL
124 }},
125 {NativeTranscoderState::RELEASED, {
126 AVTranscoderOpts::RELEASE
127 }},
128 {NativeTranscoderState::COMPLETED, {}},
129 {NativeTranscoderState::ERROR, {}},
130 };
131
132 static const std::map<std::string_view, VideoCodecFormat> MIME_STR_TO_VIDEO_CODEC_FORMAT = {
133 { CodecMimeType::VIDEO_AVC, VideoCodecFormat::H264 },
134 { CodecMimeType::VIDEO_HEVC, VideoCodecFormat::H265 },
135 { "", VideoCodecFormat::VIDEO_DEFAULT },
136 };
137
138 static const std::map<std::string_view, AudioCodecFormat> MIME_STR_TO_AUDIO_CODEC_FORMAT = {
139 { CodecMimeType::AUDIO_AAC, AudioCodecFormat::AAC_LC },
140 { "", AudioCodecFormat::AUDIO_DEFAULT },
141 };
142
143 static const std::map<OH_AVOutputFormat, OutputFormatType> AV_OUTPUT_FORMAT_TO_OUTPUT_FORMAT_TYPE = {
144 { OH_AVOutputFormat::AV_OUTPUT_FORMAT_MPEG_4, OutputFormatType::FORMAT_MPEG_4 },
145 { OH_AVOutputFormat::AV_OUTPUT_FORMAT_M4A, OutputFormatType::FORMAT_M4A },
146 };
147
148 typedef struct TranscoderExtErrCodeAPIConvert {
149 MediaServiceExtErrCodeAPI9 extErrCodeAPI;
150 OH_AVErrCode avErrorCode;
151 } TranscoderExtErrCodeAPIConvert;
152
153 static const TranscoderExtErrCodeAPIConvert ERROR_EXT_API_MAP[ERROR_EXT_API_MAP_LENGTH] = {
154 {MSERR_EXT_API9_OK, AV_ERR_OK},
155 {MSERR_EXT_API9_NO_PERMISSION, AV_ERR_OPERATE_NOT_PERMIT},
156 {MSERR_EXT_API9_PERMISSION_DENIED, AV_ERR_OPERATE_NOT_PERMIT},
157 {MSERR_EXT_API9_INVALID_PARAMETER, AV_ERR_INVALID_VAL},
158 {MSERR_EXT_API9_UNSUPPORT_CAPABILITY, AV_ERR_UNSUPPORT},
159 {MSERR_EXT_API9_NO_MEMORY, AV_ERR_NO_MEMORY},
160 {MSERR_EXT_API9_OPERATE_NOT_PERMIT, AV_ERR_OPERATE_NOT_PERMIT},
161 {MSERR_EXT_API9_IO, AV_ERR_IO},
162 {MSERR_EXT_API9_TIMEOUT, AV_ERR_TIMEOUT},
163 {MSERR_EXT_API9_SERVICE_DIED, AV_ERR_SERVICE_DIED},
164 {MSERR_EXT_API9_UNSUPPORT_FORMAT, AV_ERR_UNSUPPORT},
165 {MSERR_EXT_API9_AUDIO_INTERRUPTED, AV_ERR_OPERATE_NOT_PERMIT},
166 {MSERR_EXT_API14_IO_CANNOT_FIND_HOST, AV_ERR_IO_CANNOT_FIND_HOST},
167 {MSERR_EXT_API14_IO_CONNECTION_TIMEOUT, AV_ERR_IO_CONNECTION_TIMEOUT},
168 {MSERR_EXT_API14_IO_NETWORK_ABNORMAL, AV_ERR_IO_NETWORK_ABNORMAL},
169 {MSERR_EXT_API14_IO_NETWORK_UNAVAILABLE, AV_ERR_IO_NETWORK_UNAVAILABLE},
170 {MSERR_EXT_API14_IO_NO_PERMISSION, AV_ERR_IO_NO_PERMISSION},
171 {MSERR_EXT_API14_IO_NETWORK_ACCESS_DENIED, AV_ERR_IO_NETWORK_ACCESS_DENIED},
172 {MSERR_EXT_API14_IO_RESOURE_NOT_FOUND, AV_ERR_IO_RESOURCE_NOT_FOUND},
173 {MSERR_EXT_API14_IO_SSL_CLIENT_CERT_NEEDED, AV_ERR_IO_SSL_CLIENT_CERT_NEEDED},
174 {MSERR_EXT_API14_IO_SSL_CONNECT_FAIL, AV_ERR_IO_SSL_CONNECT_FAIL},
175 {MSERR_EXT_API14_IO_SSL_SERVER_CERT_UNTRUSTED, AV_ERR_IO_SSL_SERVER_CERT_UNTRUSTED},
176 {MSERR_EXT_API14_IO_UNSUPPORTTED_REQUEST, AV_ERR_IO_UNSUPPORTED_REQUEST},
177 };
178
ExtErrCodeAPIToAVErrCode(MediaServiceExtErrCodeAPI9 errorCode)179 static OH_AVErrCode ExtErrCodeAPIToAVErrCode(MediaServiceExtErrCodeAPI9 errorCode)
180 {
181 for (uint32_t i = 0; i < ERROR_EXT_API_MAP_LENGTH; i++) {
182 if (ERROR_EXT_API_MAP[i].extErrCodeAPI == errorCode) {
183 return ERROR_EXT_API_MAP[i].avErrorCode;
184 }
185 }
186 return AV_ERR_UNKNOWN;
187 }
188
IsValidState(OH_AVTranscoder_State state)189 static std::optional<std::string> IsValidState(OH_AVTranscoder_State state)
190 {
191 std::optional<std::string> res{ std::nullopt };
192 auto stateMapIt = STATE_MAP.find(state);
193 CHECK_AND_RETURN_RET_LOG(stateMapIt != STATE_MAP.end(), res,
194 "IsValidState is called, current OH_AVTranscoder_State is invalid!");
195 auto stateListIt = STATE_LIST.find(stateMapIt->second);
196 CHECK_AND_RETURN_RET_LOG(stateListIt != STATE_LIST.end(), res,
197 "IsValidState is called, current Native state is invalid!");
198 return stateListIt->first;
199 }
200
GetOHAVErrCode(int32_t errCode,const std::string & errorMsg)201 static OH_AVErrCode GetOHAVErrCode(int32_t errCode, const std::string &errorMsg)
202 {
203 MediaServiceErrCode serviceErrCode = static_cast<MediaServiceErrCode>(errCode);
204 MediaServiceExtErrCodeAPI9 extErrCodeAPI = MSErrorToExtErrorAPI9(serviceErrCode);
205 OH_AVErrCode avErrorCode = ExtErrCodeAPIToAVErrCode(extErrCodeAPI);
206 std::string errorMsgExt = MSExtAVErrorToString(extErrCodeAPI) + " " + MSErrorToString(serviceErrCode);
207 MEDIA_LOGE("%{public}s check failed! errCode: %{public}d, errMsg: %{public}s", errorMsg.c_str(), avErrorCode,
208 errorMsgExt.c_str());
209 return avErrorCode;
210 }
211
212 class NativeAVTranscoderOnStateChangeCallback {
213 public:
NativeAVTranscoderOnStateChangeCallback(OH_AVTranscoder_OnStateChange callback,void * userData)214 NativeAVTranscoderOnStateChangeCallback(OH_AVTranscoder_OnStateChange callback, void* userData)
215 : callback_(callback), userData_(userData) {}
216 virtual ~NativeAVTranscoderOnStateChangeCallback() = default;
217
OnStateChangeCallback(OH_AVTranscoder * transcoder,OH_AVTranscoder_State state)218 void OnStateChangeCallback(OH_AVTranscoder *transcoder, OH_AVTranscoder_State state)
219 {
220 CHECK_AND_RETURN(transcoder != nullptr && callback_ != nullptr);
221 callback_(transcoder, state, userData_);
222 }
223
224 private:
225 OH_AVTranscoder_OnStateChange callback_ = nullptr;
226 void *userData_ = nullptr;
227 };
228
229 class NativeAVTranscoderOnErrorCallback {
230 public:
NativeAVTranscoderOnErrorCallback(OH_AVTranscoder_OnError callback,void * userData)231 NativeAVTranscoderOnErrorCallback(OH_AVTranscoder_OnError callback, void* userData)
232 : callback_(callback), userData_(userData) {}
233 virtual ~NativeAVTranscoderOnErrorCallback() = default;
234
OnErrorCallback(OH_AVTranscoder * transcoder,int32_t errorCode,const char * errorMsg)235 void OnErrorCallback(OH_AVTranscoder *transcoder, int32_t errorCode, const char *errorMsg)
236 {
237 CHECK_AND_RETURN(transcoder != nullptr && callback_ != nullptr);
238 callback_(transcoder, errorCode, errorMsg, userData_);
239 }
240
241 private:
242 OH_AVTranscoder_OnError callback_ = nullptr;
243 void *userData_ = nullptr;
244 };
245
246 class NativeAVTranscoderOnProgressUpdateCallback {
247 public:
NativeAVTranscoderOnProgressUpdateCallback(OH_AVTranscoder_OnProgressUpdate callback,void * userData)248 NativeAVTranscoderOnProgressUpdateCallback(OH_AVTranscoder_OnProgressUpdate callback, void* userData)
249 : callback_(callback), userData_(userData) {}
250 virtual ~NativeAVTranscoderOnProgressUpdateCallback() = default;
251
OnProgressUpdateCallback(OH_AVTranscoder * transcoder,int progress)252 void OnProgressUpdateCallback(OH_AVTranscoder *transcoder, int progress)
253 {
254 CHECK_AND_RETURN(transcoder != nullptr && callback_ != nullptr);
255 CHECK_AND_RETURN(progress >= MIN_PROGRESS && progress <= MAX_PROGRESS);
256 callback_(transcoder, progress, userData_);
257 }
258
259 private:
260 OH_AVTranscoder_OnProgressUpdate callback_ = nullptr;
261 void *userData_ = nullptr;
262 };
263
264 class NativeAVTranscoderCallback : public TransCoderCallback {
265 public:
266 explicit NativeAVTranscoderCallback(OH_AVTranscoder *transcoder);
267 virtual ~NativeAVTranscoderCallback() = default;
268
269 OH_AVErrCode SetOnStateChangeCallback(OH_AVTranscoder_OnStateChange callback, void *userData);
270 OH_AVErrCode SetOnErrorCallback(OH_AVTranscoder_OnError callback, void *userData);
271 OH_AVErrCode SetOnProgressUpdateCallback(OH_AVTranscoder_OnProgressUpdate callback, void *userData);
272 OH_AVErrCode IsValidOpt(const std::string &opt);
273 OH_AVErrCode IsRepeatOpt(const std::string &opt);
274 void OnStateChangeCallback(OH_AVTranscoder_State state);
275 void OnProgressUpdateCallback(int32_t progress);
276
277 protected:
278 void OnError(int32_t errCode, const std::string &errorMsg) override;
279 void OnInfo(int32_t type, int32_t extra) override;
280
281 private:
282 std::mutex mutex_;
283 OH_AVTranscoder *transcoder_ = nullptr;
284 std::shared_ptr<NativeAVTranscoderOnErrorCallback> errorCallback_ = nullptr;
285 std::shared_ptr<NativeAVTranscoderOnStateChangeCallback> stateChangeCallback_ = nullptr;
286 std::shared_ptr<NativeAVTranscoderOnProgressUpdateCallback> progressUpdateCallback_ = nullptr;
287
288 std::string state_ = NativeTranscoderState::IDLE;
289 int32_t progress_ = MIN_PROGRESS;
290 };
291
292 struct NativeAVTranscoderConfig : public OH_AVTranscoder_Config {
293 NativeAVTranscoderConfig() = default;
294 ~NativeAVTranscoderConfig() = default;
295
296 int32_t srcFd = AVTRANSCODER_DEFAULT_FD;
297 int64_t srcOffset = AVTRANSCODER_DEFAULT_OFFSET;
298 int64_t length = AVTRANSCODER_DEFAULT_LENGTH;
299 int32_t dstFd = AVTRANSCODER_DEFAULT_FD;
300 AudioCodecFormat audioCodecFormat = AudioCodecFormat::AUDIO_CODEC_FORMAT_BUTT;
301 int32_t audioBitrate = AVTRANSCODER_DEFAULT_AUDIO_BIT_RATE;
302 OutputFormatType fileFormat = OutputFormatType::FORMAT_DEFAULT;
303 VideoCodecFormat videoCodecFormat = VideoCodecFormat::VIDEO_DEFAULT;
304 int32_t videoBitrate = AVTRANSCODER_DEFAULT_VIDEO_BIT_RATE;
305 int32_t videoFrameWidth = AVTRANSCODER_DEFAULT_FRAME_HEIGHT;
306 int32_t videoFrameHeight = AVTRANSCODER_DEFAULT_FRAME_WIDTH;
307 bool enableBFrame = false;
308 };
309
310 struct NativeAVTranscoder : public OH_AVTranscoder {
311 explicit NativeAVTranscoder(const std::shared_ptr<TransCoder> &transcoder);
312 ~NativeAVTranscoder() = default;
313
314 OH_AVErrCode AVTranscoderConfiguration(NativeAVTranscoderConfig *config);
315 OH_AVErrCode CheckStateMachine(const std::string &opt);
316 OH_AVErrCode CheckRepeatOptions(const std::string &opt);
317 OH_AVErrCode OnStateChange(OH_AVTranscoder_State state);
318 OH_AVErrCode SetTranscoderCallback();
319
320 std::shared_ptr<TransCoderCallback> transcoderCb_ = nullptr;
321 const std::shared_ptr<TransCoder> transcoder_ = nullptr;
322 };
323
NativeAVTranscoderCallback(OH_AVTranscoder * transcoder)324 NativeAVTranscoderCallback::NativeAVTranscoderCallback(OH_AVTranscoder *transcoder)
325 : transcoder_(transcoder)
326 {
327 MEDIA_LOGI("0x%{public}06" PRIXPTR " NativeAVTranscoderCallback create", FAKE_POINTER(this));
328 }
329
SetOnErrorCallback(OH_AVTranscoder_OnError callback,void * userData)330 OH_AVErrCode NativeAVTranscoderCallback::SetOnErrorCallback(OH_AVTranscoder_OnError callback, void *userData)
331 {
332 std::lock_guard<std::mutex> lock(mutex_);
333 if (callback != nullptr) {
334 NativeAVTranscoderOnErrorCallback *errorCallback =
335 new (std::nothrow) NativeAVTranscoderOnErrorCallback(callback, userData);
336 CHECK_AND_RETURN_RET_LOG(errorCallback != nullptr, AV_ERR_NO_MEMORY, "errorCallback is nullptr!");
337 errorCallback_ = std::shared_ptr<NativeAVTranscoderOnErrorCallback>(errorCallback);
338 } else {
339 errorCallback_ = nullptr;
340 }
341 return AV_ERR_OK;
342 }
343
SetOnStateChangeCallback(OH_AVTranscoder_OnStateChange callback,void * userData)344 OH_AVErrCode NativeAVTranscoderCallback::SetOnStateChangeCallback(OH_AVTranscoder_OnStateChange callback,
345 void *userData)
346 {
347 std::lock_guard<std::mutex> lock(mutex_);
348 if (callback != nullptr) {
349 NativeAVTranscoderOnStateChangeCallback *stateChangeCallback =
350 new (std::nothrow) NativeAVTranscoderOnStateChangeCallback(callback, userData);
351 CHECK_AND_RETURN_RET_LOG(stateChangeCallback != nullptr, AV_ERR_NO_MEMORY, "stateChangeCallback is nullptr!");
352 stateChangeCallback_ = std::shared_ptr<NativeAVTranscoderOnStateChangeCallback>(stateChangeCallback);
353 } else {
354 stateChangeCallback_ = nullptr;
355 }
356 return AV_ERR_OK;
357 }
358
SetOnProgressUpdateCallback(OH_AVTranscoder_OnProgressUpdate callback,void * userData)359 OH_AVErrCode NativeAVTranscoderCallback::SetOnProgressUpdateCallback(OH_AVTranscoder_OnProgressUpdate callback,
360 void *userData)
361 {
362 std::lock_guard<std::mutex> lock(mutex_);
363 if (callback != nullptr) {
364 NativeAVTranscoderOnProgressUpdateCallback *progressUpdateCallback =
365 new (std::nothrow) NativeAVTranscoderOnProgressUpdateCallback(callback, userData);
366 CHECK_AND_RETURN_RET_LOG(progressUpdateCallback != nullptr, AV_ERR_NO_MEMORY,
367 "progressUpdateCallback is nullptr!");
368 progressUpdateCallback_ = std::shared_ptr<NativeAVTranscoderOnProgressUpdateCallback>(progressUpdateCallback);
369 } else {
370 progressUpdateCallback_ = nullptr;
371 }
372 return AV_ERR_OK;
373 }
374
IsValidOpt(const std::string & opt)375 OH_AVErrCode NativeAVTranscoderCallback::IsValidOpt(const std::string &opt)
376 {
377 auto currentState = STATE_LIST.find(state_);
378 CHECK_AND_RETURN_RET_LOG(currentState != STATE_LIST.end(), AV_ERR_INVALID_VAL,
379 "IsValidOpt is called, current state is invalid!");
380
381 const auto& allowableOpts = currentState->second;
382 CHECK_AND_RETURN_RET_LOG(
383 std::find(allowableOpts.begin(), allowableOpts.end(), opt) != allowableOpts.end(),
384 AV_ERR_OPERATE_NOT_PERMIT,
385 "IsValidOpt is called, current state does not allow this opt! current state is %{public}s,\
386 opt is %{public}s", state_.c_str(), opt.c_str());
387 return AV_ERR_OK;
388 }
389
IsRepeatOpt(const std::string & opt)390 OH_AVErrCode NativeAVTranscoderCallback::IsRepeatOpt(const std::string &opt)
391 {
392 auto currentState = STATE_LIST.find(state_);
393 CHECK_AND_RETURN_RET_LOG(currentState != STATE_LIST.end(), AV_ERR_INVALID_VAL,
394 "IsRepeatOpt is called, current state is invalid!");
395
396 auto currentCtrlState = STATE_CTRL.find(state_);
397 CHECK_AND_RETURN_RET_LOG(currentCtrlState != STATE_CTRL.end(), AV_ERR_INVALID_VAL,
398 "IsRepeatOpt is called, current state is invalid!");
399
400 const auto& repeatOpts = currentCtrlState->second;
401 CHECK_AND_RETURN_RET_LOG(
402 std::find(repeatOpts.begin(), repeatOpts.end(), opt) == repeatOpts.end(),
403 AV_ERR_OPERATE_NOT_PERMIT,
404 "IsRepeatOpt is called, please do not repeat the %{public}s operation!", opt.c_str());
405 return AV_ERR_OK;
406 }
407
OnError(int32_t errCode,const std::string & errorMsg)408 void NativeAVTranscoderCallback::OnError(int32_t errCode, const std::string &errorMsg)
409 {
410 MEDIA_LOGE("NativeAVTranscoderCallback::OnError: %{public}d, %{public}s", errCode, errorMsg.c_str());
411 std::unique_lock<std::mutex> lock(mutex_);
412 CHECK_AND_RETURN_LOG(transcoder_ != nullptr,
413 "NativeAVTranscoderCallback::OnError is called, transcoder_ is nullptr!");
414 state_ = NativeTranscoderState::ERROR;
415 lock.unlock();
416 if (errorCallback_ != nullptr) {
417 MediaServiceExtErrCodeAPI9 extErrCodeAPI =
418 MSErrorToExtErrorAPI9(static_cast<MediaServiceErrCode>(errCode));
419 int32_t avErrorCode = ExtErrCodeAPIToAVErrCode(extErrCodeAPI);
420 std::string errorMsgExt = MSExtAVErrorToString(extErrCodeAPI) + errorMsg;
421 errorCallback_->OnErrorCallback(transcoder_, avErrorCode, errorMsgExt.c_str());
422 }
423 }
424
OnInfo(int32_t type,int32_t extra)425 void NativeAVTranscoderCallback::OnInfo(int32_t type, int32_t extra)
426 {
427 MEDIA_LOGI("NativeAVTranscoderCallback::OnInfo enter.");
428 CHECK_AND_RETURN_LOG(transcoder_ != nullptr,
429 "NativeAVTranscoderCallback::OnInfo is called, transcoder_ is nullptr!");
430
431 if (type == TransCoderOnInfoType::INFO_TYPE_TRANSCODER_COMPLETED) {
432 OnStateChangeCallback(OH_AVTranscoder_State::AVTRANSCODER_COMPLETED);
433 } else if (type == TransCoderOnInfoType::INFO_TYPE_PROGRESS_UPDATE) {
434 OnProgressUpdateCallback(extra);
435 }
436 }
437
OnStateChangeCallback(OH_AVTranscoder_State state)438 void NativeAVTranscoderCallback::OnStateChangeCallback(OH_AVTranscoder_State state)
439 {
440 MEDIA_LOGI("NativeAVTranscoderCallback::OnStateChangeCallback enter.");
441 std::unique_lock<std::mutex> lock(mutex_);
442 CHECK_AND_RETURN_LOG(transcoder_ != nullptr,
443 "OnStateChangeCallback is called, transcoder_ is nullptr!");
444 CHECK_AND_RETURN_LOG(state_ != NativeTranscoderState::ERROR,
445 "OnStateChangeCallback is called, current state is ERROR, only can execute release!");
446 auto nativeState = IsValidState(state);
447 CHECK_AND_RETURN_LOG(nativeState != std::nullopt,
448 "OnStateChangeCallback is called, state is invalid!");
449 state_ = nativeState.value();
450 lock.unlock();
451 if (stateChangeCallback_ != nullptr) {
452 stateChangeCallback_->OnStateChangeCallback(transcoder_, state);
453 }
454 }
455
OnProgressUpdateCallback(int32_t progress)456 void NativeAVTranscoderCallback::OnProgressUpdateCallback(int32_t progress)
457 {
458 MEDIA_LOGI("NativeAVTranscoderCallback::OnProgressUpdateCallback enter.");
459 std::unique_lock<std::mutex> lock(mutex_);
460 CHECK_AND_RETURN_LOG(transcoder_ != nullptr,
461 "OnProgressUpdateCallback is called, transcoder_ is nullptr!");
462 CHECK_AND_RETURN_LOG(progress >= MIN_PROGRESS && progress <= MAX_PROGRESS,
463 "OnProgressUpdateCallback is called, progress is invalid!");
464 progress_ = progress;
465 lock.unlock();
466 if (progressUpdateCallback_ != nullptr) {
467 progressUpdateCallback_->OnProgressUpdateCallback(transcoder_, progress);
468 }
469 }
470
AVTranscoderConfiguration(NativeAVTranscoderConfig * config)471 OH_AVErrCode NativeAVTranscoder::AVTranscoderConfiguration(NativeAVTranscoderConfig *config)
472 {
473 MEDIA_LOGI("NativeAVTranscoder::AVTranscoderConfiguration enter.");
474 CHECK_AND_RETURN_RET_LOG(config != nullptr, AV_ERR_INVALID_VAL,
475 "AVTranscoderConfiguration is called, config is nullptr!");
476 CHECK_AND_RETURN_RET_LOG(transcoder_ != nullptr, AV_ERR_OPERATE_NOT_PERMIT,
477 "AVTranscoderConfiguration is called, transcoder_ is nullptr!");
478
479 int32_t errorCode {MSERR_OK};
480
481 errorCode = transcoder_->SetInputFile(config->srcFd, config->srcOffset, config->length);
482 CHECK_AND_RETURN_RET_LOG(errorCode == MSERR_OK, GetOHAVErrCode(errorCode, "SetInputFile"),
483 "AVTranscoderConfiguration is called, SetInputFile check failed, srcFd:%{public}" PRId32
484 ", srcOffset:%{public}" PRId64 ", srcLength:%{public}" PRId64, config->srcFd, config->srcOffset,
485 config->length);
486
487 errorCode = transcoder_->SetOutputFile(config->dstFd);
488 CHECK_AND_RETURN_RET_LOG(errorCode == MSERR_OK, GetOHAVErrCode(errorCode, "SetOutputFile"),
489 "AVTranscoderConfiguration is called, SetOutputFile check failed, dstFd:%{public}d", config->dstFd);
490
491 errorCode = transcoder_->SetOutputFormat(config->fileFormat);
492 CHECK_AND_RETURN_RET_LOG(errorCode == MSERR_OK, GetOHAVErrCode(errorCode, "SetOutputFormat"),
493 "AVTranscoderConfiguration is called, SetOutputFormat check failed");
494
495 errorCode = transcoder_->SetAudioEncoder(config->audioCodecFormat);
496 CHECK_AND_RETURN_RET_LOG(errorCode == MSERR_OK, GetOHAVErrCode(errorCode, "SetAudioEncoder"),
497 "AVTranscoderConfiguration is called, SetAudioEncoder check failed");
498
499 errorCode = transcoder_->SetAudioEncodingBitRate(config->audioBitrate);
500 CHECK_AND_RETURN_RET_LOG(errorCode == MSERR_OK, GetOHAVErrCode(errorCode, "SetAudioEncodingBitRate"),
501 "AVTranscoderConfiguration is called, SetAudioEncodingBitRate check failed, audioBitrate:%{public}d",
502 config->audioBitrate);
503
504 errorCode = transcoder_->SetVideoEncoder(config->videoCodecFormat);
505 CHECK_AND_RETURN_RET_LOG(errorCode == MSERR_OK, GetOHAVErrCode(errorCode, "SetVideoEncoder"),
506 "AVTranscoderConfiguration is called, SetVideoEncoder check failed");
507
508 errorCode = transcoder_->SetVideoSize(config->videoFrameWidth, config->videoFrameHeight);
509 CHECK_AND_RETURN_RET_LOG(errorCode == MSERR_OK, GetOHAVErrCode(errorCode, "SetVideoSize"),
510 "AVTranscoderConfiguration is called, SetVideoSize check failed, videoFrameWidth:%{public}d,\
511 videoFrameHeight:%{public}d", config->videoFrameWidth, config->videoFrameHeight);
512
513 errorCode = transcoder_->SetVideoEncodingBitRate(config->videoBitrate);
514 CHECK_AND_RETURN_RET_LOG(errorCode == MSERR_OK, GetOHAVErrCode(errorCode, "SetVideoEncodingBitRate"),
515 "AVTranscoderConfiguration is called, SetVideoEncodingBitRate check failed, videoFrameWidth:%{public}d",
516 config->videoBitrate);
517
518 errorCode = transcoder_->SetEnableBFrame(config->enableBFrame);
519 CHECK_AND_RETURN_RET_LOG(errorCode == MSERR_OK, GetOHAVErrCode(errorCode, "SetEnableBFrame"),
520 "AVTranscoderConfiguration is called, SetEnableBFrame check failed, enableBFrame:%{public}d",
521 static_cast<int32_t>(config->enableBFrame));
522
523 return AV_ERR_OK;
524 }
525
NativeAVTranscoder(const std::shared_ptr<TransCoder> & transcoder)526 NativeAVTranscoder::NativeAVTranscoder(const std::shared_ptr<TransCoder> &transcoder)
527 : transcoder_(transcoder)
528 {
529 MEDIA_LOGI("0x%{public}06" PRIXPTR " NativeAVTranscoder create", FAKE_POINTER(this));
530 transcoderCb_ = std::make_shared<NativeAVTranscoderCallback>(this);
531 if (transcoder_ != nullptr && transcoderCb_ != nullptr) {
532 int32_t ret = transcoder_->SetTransCoderCallback(transcoderCb_);
533 if (ret != MSERR_OK) {
534 MEDIA_LOGE("NativeAVTranscoder constructor failed, ret%{public}d.",
535 GetOHAVErrCode(ret, "NativeAVTranscoderConstruct"));
536 }
537 }
538 }
539
SetTranscoderCallback()540 OH_AVErrCode NativeAVTranscoder::SetTranscoderCallback()
541 {
542 CHECK_AND_RETURN_RET_LOG(transcoder_ != nullptr, AV_ERR_INVALID_VAL,
543 "NativeAVTranscoder::SetTranscoderCallback is called, transcoder_ is nullptr!");
544 NativeAVTranscoderCallback *newCallback =
545 new (std::nothrow) NativeAVTranscoderCallback(this);
546 CHECK_AND_RETURN_RET_LOG(newCallback != nullptr, AV_ERR_NO_MEMORY,
547 "NativeAVTranscoder::SetTranscoderCallback is called, transcoderCb_ construct failed!");
548 transcoderCb_ = std::shared_ptr<NativeAVTranscoderCallback>(newCallback);
549 CHECK_AND_RETURN_RET_LOG(transcoderCb_ != nullptr, AV_ERR_NO_MEMORY,
550 "NativeAVTranscoder::SetTranscoderCallback is called, transcoderCb_ construct failed!");
551 int32_t ret = transcoder_->SetTransCoderCallback(transcoderCb_);
552 CHECK_AND_RETURN_RET_NOLOG(ret == MSERR_OK, GetOHAVErrCode(ret, "SetTransCoderCallback"));
553 return AV_ERR_OK;
554 }
555
CheckStateMachine(const std::string & opt)556 OH_AVErrCode NativeAVTranscoder::CheckStateMachine(const std::string &opt)
557 {
558 CHECK_AND_RETURN_RET_LOG(transcoder_ != nullptr && transcoderCb_ != nullptr,
559 AV_ERR_OPERATE_NOT_PERMIT, "CheckStateMachine is called, transcoder_ or transcoderCb_ is nullptr!");
560
561 NativeAVTranscoderCallback* nativeCallback = reinterpret_cast<NativeAVTranscoderCallback*>(transcoderCb_.get());
562 CHECK_AND_RETURN_RET_LOG(nativeCallback != nullptr,
563 AV_ERR_OPERATE_NOT_PERMIT, "CheckStateMachine is called, nativeCallback is nullptr!");
564
565 OH_AVErrCode isValidOpt = nativeCallback->IsValidOpt(opt);
566 CHECK_AND_RETURN_RET_LOG(isValidOpt == AV_ERR_OK, isValidOpt,
567 "CheckStateMachine is called, opt is invalid!");
568
569 return AV_ERR_OK;
570 }
571
CheckRepeatOptions(const std::string & opt)572 OH_AVErrCode NativeAVTranscoder::CheckRepeatOptions(const std::string &opt)
573 {
574 CHECK_AND_RETURN_RET_LOG(transcoder_ != nullptr && transcoderCb_ != nullptr,
575 AV_ERR_OPERATE_NOT_PERMIT, "CheckRepeatOptions is called, transcoder_ or transcoderCb_ is nullptr!");
576
577 NativeAVTranscoderCallback* nativeCallback = reinterpret_cast<NativeAVTranscoderCallback*>(transcoderCb_.get());
578 CHECK_AND_RETURN_RET_LOG(nativeCallback != nullptr,
579 AV_ERR_OPERATE_NOT_PERMIT, "CheckRepeatOptions is called, nativeCallback is nullptr!");
580
581 OH_AVErrCode isValidOpt = nativeCallback->IsRepeatOpt(opt);
582 CHECK_AND_RETURN_RET_LOG(isValidOpt == AV_ERR_OK, isValidOpt,
583 "CheckRepeatOptions is called, opt is repeat option!");
584
585 return AV_ERR_OK;
586 }
587
OnStateChange(OH_AVTranscoder_State state)588 OH_AVErrCode NativeAVTranscoder::OnStateChange(OH_AVTranscoder_State state)
589 {
590 CHECK_AND_RETURN_RET_LOG(transcoder_ != nullptr && transcoderCb_ != nullptr, AV_ERR_INVALID_VAL,
591 "OnStateChange is called, transcoder_ or transcoderCb_ is nullptr!");
592
593 NativeAVTranscoderCallback* nativeCallback = reinterpret_cast<NativeAVTranscoderCallback*>(transcoderCb_.get());
594 CHECK_AND_RETURN_RET_LOG(nativeCallback != nullptr, AV_ERR_INVALID_VAL,
595 "OnStateChange is called, nativeCallback is nullptr!");
596
597 CHECK_AND_RETURN_RET_LOG(IsValidState(state) != std::nullopt, AV_ERR_INVALID_VAL,
598 "OnStateChange is called, current state is invalid!");
599
600 nativeCallback->OnStateChangeCallback(state);
601 return AV_ERR_OK;
602 }
603
OH_AVTranscoderConfig_Create()604 OH_AVTranscoder_Config *OH_AVTranscoderConfig_Create()
605 {
606 MEDIA_LOGI("OH_AVTranscoderConfig_Create enter.");
607 NativeAVTranscoderConfig *config = new (std::nothrow) NativeAVTranscoderConfig();
608 CHECK_AND_RETURN_RET_LOG(config != nullptr, nullptr,
609 "OH_AVTranscoderConfig_Create is called, config is nullptr!");
610 return config;
611 }
612
OH_AVTranscoderConfig_Release(OH_AVTranscoder_Config * config)613 OH_AVErrCode OH_AVTranscoderConfig_Release(OH_AVTranscoder_Config* config)
614 {
615 MEDIA_LOGI("OH_AVTranscoderConfig_Release enter.");
616 CHECK_AND_RETURN_RET_LOG(config != nullptr, AV_ERR_INVALID_VAL,
617 "OH_AVTranscoderConfig_Release is called, config is nullptr!");
618
619 delete config;
620 config = nullptr;
621 return AV_ERR_OK;
622 }
623
OH_AVTranscoderConfig_SetSrcFD(OH_AVTranscoder_Config * config,int32_t srcFd,int64_t srcOffset,int64_t length)624 OH_AVErrCode OH_AVTranscoderConfig_SetSrcFD(
625 OH_AVTranscoder_Config *config, int32_t srcFd, int64_t srcOffset, int64_t length)
626 {
627 MEDIA_LOGI("OH_AVTranscoderConfig_SetSrcFD enter.");
628 CHECK_AND_RETURN_RET_LOG(config != nullptr, AV_ERR_INVALID_VAL,
629 "OH_AVTranscoderConfig_SetSrcFD is called, config is nullptr!");
630
631 NativeAVTranscoderConfig* nativeConfig = reinterpret_cast<NativeAVTranscoderConfig*>(config);
632 CHECK_AND_RETURN_RET_LOG(nativeConfig != nullptr, AV_ERR_INVALID_VAL,
633 "OH_AVTranscoderConfig_SetSrcFD is called, nativeConfig is nullptr!");
634
635 CHECK_AND_RETURN_RET_LOG(srcFd >= 0, AV_ERR_INVALID_VAL,
636 "OH_AVTranscoderConfig_SetSrcFD is called, srcFd is less than zero!");
637
638 CHECK_AND_RETURN_RET_LOG(srcOffset >= 0, AV_ERR_INVALID_VAL,
639 "OH_AVTranscoderConfig_SetSrcFD is called, srcOffset is less than zero!");
640
641 CHECK_AND_RETURN_RET_LOG(length >= 0, AV_ERR_INVALID_VAL,
642 "OH_AVTranscoderConfig_SetSrcFD is called, length is less than zero!");
643
644 nativeConfig->srcFd = srcFd;
645 nativeConfig->srcOffset = srcOffset;
646 nativeConfig->length = length;
647 return AV_ERR_OK;
648 }
649
OH_AVTranscoderConfig_SetDstFD(OH_AVTranscoder_Config * config,int32_t dstFd)650 OH_AVErrCode OH_AVTranscoderConfig_SetDstFD(OH_AVTranscoder_Config *config, int32_t dstFd)
651 {
652 MEDIA_LOGI("OH_AVTranscoderConfig_SetDstFD enter.");
653 CHECK_AND_RETURN_RET_LOG(config != nullptr, AV_ERR_INVALID_VAL,
654 "OH_AVTranscoderConfig_SetDstFD is called, config is nullptr!");
655
656 NativeAVTranscoderConfig* nativeConfig = reinterpret_cast<NativeAVTranscoderConfig*>(config);
657 CHECK_AND_RETURN_RET_LOG(nativeConfig != nullptr, AV_ERR_INVALID_VAL,
658 "OH_AVTranscoderConfig_SetDstFD is called, nativeConfig is nullptr!");
659
660 CHECK_AND_RETURN_RET_LOG(dstFd >= 0, AV_ERR_INVALID_VAL,
661 "OH_AVTranscoderConfig_SetDstFD is called, dstFd is less than zero!");
662
663 nativeConfig->dstFd = dstFd;
664 return AV_ERR_OK;
665 }
666
OH_AVTranscoderConfig_SetDstVideoType(OH_AVTranscoder_Config * config,const char * mimeType)667 OH_AVErrCode OH_AVTranscoderConfig_SetDstVideoType(OH_AVTranscoder_Config *config, const char *mimeType)
668 {
669 MEDIA_LOGI("OH_AVTranscoderConfig_SetDstVideoType enter.");
670 CHECK_AND_RETURN_RET_LOG(config != nullptr && mimeType != nullptr, AV_ERR_INVALID_VAL,
671 "OH_AVTranscoderConfig_SetDstVideoType is called, config or mimeType is nullptr!");
672
673 NativeAVTranscoderConfig* nativeConfig = reinterpret_cast<NativeAVTranscoderConfig*>(config);
674 CHECK_AND_RETURN_RET_LOG(nativeConfig != nullptr, AV_ERR_INVALID_VAL,
675 "OH_AVTranscoderConfig_SetDstVideoType is called, nativeConfig is nullptr!");
676
677 auto currentMimeType = MIME_STR_TO_VIDEO_CODEC_FORMAT.find(mimeType);
678 CHECK_AND_RETURN_RET_LOG(currentMimeType != MIME_STR_TO_VIDEO_CODEC_FORMAT.end(),
679 AV_ERR_INVALID_VAL,
680 "OH_AVTranscoderConfig_SetDstVideoType is called, invalid mime type!");
681
682 nativeConfig->videoCodecFormat = currentMimeType->second;
683 return AV_ERR_OK;
684 }
685
OH_AVTranscoderConfig_SetDstAudioType(OH_AVTranscoder_Config * config,const char * mimeType)686 OH_AVErrCode OH_AVTranscoderConfig_SetDstAudioType(OH_AVTranscoder_Config *config, const char *mimeType)
687 {
688 MEDIA_LOGI("OH_AVTranscoderConfig_SetDstAudioType enter.");
689 CHECK_AND_RETURN_RET_LOG(config != nullptr && mimeType != nullptr, AV_ERR_INVALID_VAL,
690 "OH_AVTranscoderConfig_SetDstAudioType is called, config or mimeType is nullptr!");
691
692 NativeAVTranscoderConfig* nativeConfig = reinterpret_cast<NativeAVTranscoderConfig*>(config);
693 CHECK_AND_RETURN_RET_LOG(nativeConfig != nullptr, AV_ERR_INVALID_VAL,
694 "OH_AVTranscoderConfig_SetDstAudioType is called, nativeConfig is nullptr!");
695
696 auto currentMimeType = MIME_STR_TO_AUDIO_CODEC_FORMAT.find(mimeType);
697 CHECK_AND_RETURN_RET_LOG(currentMimeType != MIME_STR_TO_AUDIO_CODEC_FORMAT.end(),
698 AV_ERR_INVALID_VAL,
699 "OH_AVTranscoderConfig_SetDstAudioType is called, invalid mime type!");
700
701 nativeConfig->audioCodecFormat = currentMimeType->second;
702 return AV_ERR_OK;
703 }
704
OH_AVTranscoderConfig_SetDstFileType(OH_AVTranscoder_Config * config,OH_AVOutputFormat mimeType)705 OH_AVErrCode OH_AVTranscoderConfig_SetDstFileType(OH_AVTranscoder_Config *config, OH_AVOutputFormat mimeType)
706 {
707 MEDIA_LOGI("OH_AVTranscoderConfig_SetDstFileType enter.");
708 CHECK_AND_RETURN_RET_LOG(config != nullptr, AV_ERR_INVALID_VAL,
709 "OH_AVTranscoderConfig_SetDstFileType is called, config is nullptr!");
710
711 NativeAVTranscoderConfig* nativeConfig = reinterpret_cast<NativeAVTranscoderConfig*>(config);
712 CHECK_AND_RETURN_RET_LOG(nativeConfig != nullptr, AV_ERR_INVALID_VAL,
713 "OH_AVTranscoderConfig_SetDstFileType is called, nativeConfig is nullptr!");
714
715 auto currentOutputFormat = AV_OUTPUT_FORMAT_TO_OUTPUT_FORMAT_TYPE.find(mimeType);
716 CHECK_AND_RETURN_RET_LOG(currentOutputFormat != AV_OUTPUT_FORMAT_TO_OUTPUT_FORMAT_TYPE.end(),
717 AV_ERR_INVALID_VAL,
718 "OH_AVTranscoderConfig_SetDstFileType is called, invalid mime type!");
719
720 nativeConfig->fileFormat = currentOutputFormat->second;
721 return AV_ERR_OK;
722 }
723
OH_AVTranscoderConfig_SetDstAudioBitrate(OH_AVTranscoder_Config * config,int32_t bitrate)724 OH_AVErrCode OH_AVTranscoderConfig_SetDstAudioBitrate(OH_AVTranscoder_Config *config, int32_t bitrate)
725 {
726 MEDIA_LOGI("OH_AVTranscoderConfig_SetDstAudioBitrate enter.");
727 CHECK_AND_RETURN_RET_LOG(config != nullptr, AV_ERR_INVALID_VAL,
728 "OH_AVTranscoderConfig_SetDstAudioBitrate is called, config is nullptr!");
729
730 NativeAVTranscoderConfig* nativeConfig = reinterpret_cast<NativeAVTranscoderConfig*>(config);
731 CHECK_AND_RETURN_RET_LOG(nativeConfig != nullptr, AV_ERR_INVALID_VAL,
732 "OH_AVTranscoderConfig_SetDstAudioBitrate is called, nativeConfig is nullptr!");
733
734 CHECK_AND_RETURN_RET_LOG(bitrate > 0, AV_ERR_INVALID_VAL,
735 "OH_AVTranscoderConfig_SetDstAudioBitrate is called, bitrate is less than or equal to zero!");
736
737 nativeConfig->audioBitrate = bitrate;
738 return AV_ERR_OK;
739 }
740
OH_AVTranscoderConfig_SetDstVideoBitrate(OH_AVTranscoder_Config * config,int32_t bitrate)741 OH_AVErrCode OH_AVTranscoderConfig_SetDstVideoBitrate(OH_AVTranscoder_Config *config, int32_t bitrate)
742 {
743 MEDIA_LOGI("OH_AVTranscoderConfig_SetDstVideoBitrate enter.");
744 CHECK_AND_RETURN_RET_LOG(config != nullptr, AV_ERR_INVALID_VAL,
745 "OH_AVTranscoderConfig_SetDstVideoBitrate is called, config is nullptr!");
746
747 NativeAVTranscoderConfig* nativeConfig = reinterpret_cast<NativeAVTranscoderConfig*>(config);
748 CHECK_AND_RETURN_RET_LOG(nativeConfig != nullptr, AV_ERR_INVALID_VAL,
749 "OH_AVTranscoderConfig_SetDstVideoBitrate is called, nativeConfig is nullptr!");
750
751 CHECK_AND_RETURN_RET_LOG(bitrate > 0, AV_ERR_INVALID_VAL,
752 "OH_AVTranscoderConfig_SetDstVideoBitrate is called, bitrate is less than or equal to zero!");
753
754 nativeConfig->videoBitrate = bitrate;
755 return AV_ERR_OK;
756 }
757
OH_AVTranscoderConfig_SetDstVideoResolution(OH_AVTranscoder_Config * config,int32_t width,int32_t height)758 OH_AVErrCode OH_AVTranscoderConfig_SetDstVideoResolution(OH_AVTranscoder_Config *config, int32_t width, int32_t height)
759 {
760 MEDIA_LOGI("OH_AVTranscoderConfig_SetDstVideoResolution enter.");
761 CHECK_AND_RETURN_RET_LOG(config != nullptr, AV_ERR_INVALID_VAL,
762 "OH_AVTranscoderConfig_SetDstVideoResolution is called, config is nullptr!");
763
764 NativeAVTranscoderConfig* nativeConfig = reinterpret_cast<NativeAVTranscoderConfig*>(config);
765 CHECK_AND_RETURN_RET_LOG(nativeConfig != nullptr, AV_ERR_INVALID_VAL,
766 "OH_AVTranscoderConfig_SetDstVideoResolution is called, nativeConfig is nullptr!");
767
768 CHECK_AND_RETURN_RET_LOG(width > 0, AV_ERR_INVALID_VAL,
769 "OH_AVTranscoderConfig_SetDstVideoResolution is called, width is less than or equal to zero!");
770
771 CHECK_AND_RETURN_RET_LOG(height > 0, AV_ERR_INVALID_VAL,
772 "OH_AVTranscoderConfig_SetDstVideoResolution is called, height is less than or equal to zero!");
773
774 nativeConfig->videoFrameHeight = height;
775 nativeConfig->videoFrameWidth = width;
776 return AV_ERR_OK;
777 }
778
OH_AVTranscoder_Create()779 OH_AVTranscoder *OH_AVTranscoder_Create()
780 {
781 MEDIA_LOGI("OH_AVTranscoder_Create enter.");
782 std::shared_ptr<TransCoder> transcoder = TransCoderFactory::CreateTransCoder();
783 CHECK_AND_RETURN_RET_LOG(transcoder != nullptr, nullptr,
784 "OH_AVTranscoder_Create is called, TransCoderFactory::CreateTransCoder failed!");
785
786 NativeAVTranscoder *nativeTranscoder =
787 new (std::nothrow) NativeAVTranscoder(transcoder);
788 CHECK_AND_RETURN_RET_LOG(nativeTranscoder != nullptr, nullptr,
789 "OH_AVTRanscoder_Create is called, NativeAVTranscoder construct failed!");
790
791 return nativeTranscoder;
792 }
793
OH_AVTranscoder_Prepare(OH_AVTranscoder * transcoder,OH_AVTranscoder_Config * config)794 OH_AVErrCode OH_AVTranscoder_Prepare(OH_AVTranscoder *transcoder, OH_AVTranscoder_Config *config)
795 {
796 MEDIA_LOGI("OH_AVTranscoder_Prepare enter.");
797 CHECK_AND_RETURN_RET_LOG(transcoder != nullptr && config != nullptr, AV_ERR_INVALID_VAL,
798 "OH_AVTranscoder_Prepare is called, transcoder is nullptr or config is nullptr!");
799
800 NativeAVTranscoderConfig *nativeConfig = reinterpret_cast<NativeAVTranscoderConfig*>(config);
801 CHECK_AND_RETURN_RET_LOG(nativeConfig != nullptr, AV_ERR_INVALID_VAL,
802 "OH_AVTranscoder_Prepare is called, nativeConfig is nullptr!");
803
804 NativeAVTranscoder *nativeTranscoder = reinterpret_cast<NativeAVTranscoder*>(transcoder);
805 CHECK_AND_RETURN_RET_LOG(nativeTranscoder != nullptr, AV_ERR_INVALID_VAL,
806 "OH_AVTranscoder_Prepare is called, nativeTranscoder is nullptr!");
807
808 CHECK_AND_RETURN_RET_LOG(nativeTranscoder->CheckStateMachine(AVTranscoderOpts::PREPARE) == AV_ERR_OK,
809 AV_ERR_OPERATE_NOT_PERMIT, "OH_AVTranscoder_Prepare is called, CheckStateMachine failed!");
810
811 OH_AVErrCode errCode = nativeTranscoder->AVTranscoderConfiguration(nativeConfig);
812 CHECK_AND_RETURN_RET_LOG(errCode == AV_ERR_OK,
813 ((void)nativeTranscoder->transcoder_->Cancel(), errCode),
814 "OH_AVTranscoder_Prepare is called, AVTranscoderConfiguration failed!");
815
816 int32_t errPrepareCode = nativeTranscoder->transcoder_->Prepare();
817 CHECK_AND_RETURN_RET_LOG(errPrepareCode == MSERR_OK,
818 ((void)nativeTranscoder->transcoder_->Cancel(), GetOHAVErrCode(errPrepareCode, "Prepare")),
819 "OH_AVTranscoder_Prepare is called, Prepare failed!");
820
821 OH_AVErrCode avErrCode = nativeTranscoder->OnStateChange(OH_AVTranscoder_State::AVTRANSCODER_PREPARED);
822 CHECK_AND_RETURN_RET_LOG(avErrCode == AV_ERR_OK, avErrCode,
823 "OH_AVTranscoder_Prepare is called, OnStateChange failed!");
824 return AV_ERR_OK;
825 }
826
OH_AVTranscoder_Start(OH_AVTranscoder * transcoder)827 OH_AVErrCode OH_AVTranscoder_Start(OH_AVTranscoder *transcoder)
828 {
829 MEDIA_LOGI("OH_AVTranscoder_Start enter.");
830 CHECK_AND_RETURN_RET_LOG(transcoder != nullptr, AV_ERR_INVALID_VAL,
831 "OH_AVTranscoder_Start is called, transcoder is nullptr!");
832
833 NativeAVTranscoder *nativeTranscoder = reinterpret_cast<NativeAVTranscoder*>(transcoder);
834 CHECK_AND_RETURN_RET_LOG(nativeTranscoder != nullptr, AV_ERR_INVALID_VAL,
835 "OH_AVTranscoder_Start is called, nativeTranscoder is nullptr!");
836
837 CHECK_AND_RETURN_RET_LOG(nativeTranscoder->CheckStateMachine(AVTranscoderOpts::START) == AV_ERR_OK,
838 AV_ERR_OPERATE_NOT_PERMIT, "OH_AVTranscoder_Start is called, CheckStateMachine failed!");
839
840 CHECK_AND_RETURN_RET_LOG(nativeTranscoder->CheckRepeatOptions(AVTranscoderOpts::START) == AV_ERR_OK,
841 AV_ERR_OK, "OH_AVTranscoder_Start is called, Start operation was repeated!");
842
843 int32_t errCode = nativeTranscoder->transcoder_->Start();
844 CHECK_AND_RETURN_RET_LOG(errCode == MSERR_OK, GetOHAVErrCode(errCode, "Start"),
845 "OH_AVTranscoder_Start is called, Start failed!");
846
847 OH_AVErrCode avErrCode = nativeTranscoder->OnStateChange(OH_AVTranscoder_State::AVTRANSCODER_STARTED);
848 CHECK_AND_RETURN_RET_LOG(avErrCode == AV_ERR_OK, avErrCode,
849 "OH_AVTranscoder_Start is called, OnStateChange failed!");
850 return AV_ERR_OK;
851 }
852
OH_AVTranscoder_Pause(OH_AVTranscoder * transcoder)853 OH_AVErrCode OH_AVTranscoder_Pause(OH_AVTranscoder *transcoder)
854 {
855 MEDIA_LOGI("OH_AVTranscoder_Pause enter.");
856 CHECK_AND_RETURN_RET_LOG(transcoder != nullptr, AV_ERR_INVALID_VAL,
857 "OH_AVTranscoder_Pause is called, transcoder is nullptr!");
858
859 NativeAVTranscoder *nativeTranscoder = reinterpret_cast<NativeAVTranscoder*>(transcoder);
860 CHECK_AND_RETURN_RET_LOG(nativeTranscoder != nullptr, AV_ERR_INVALID_VAL,
861 "OH_AVTranscoder_Pause is called, nativeTranscoder is nullptr!");
862
863 CHECK_AND_RETURN_RET_LOG(nativeTranscoder->CheckStateMachine(AVTranscoderOpts::PAUSE) == AV_ERR_OK,
864 AV_ERR_OPERATE_NOT_PERMIT, "OH_AVTranscoder_Pause is called, CheckStateMachine failed!");
865
866 CHECK_AND_RETURN_RET_LOG(nativeTranscoder->CheckRepeatOptions(AVTranscoderOpts::PAUSE) == AV_ERR_OK,
867 AV_ERR_OK, "OH_AVTranscoder_Pause is called, Pause operation was repeated!");
868
869 int32_t errCode = nativeTranscoder->transcoder_->Pause();
870 CHECK_AND_RETURN_RET_LOG(errCode == MSERR_OK, GetOHAVErrCode(errCode, "Pause"),
871 "OH_AVTranscoder_Pause is called, Pause failed!");
872
873 OH_AVErrCode avErrCode = nativeTranscoder->OnStateChange(OH_AVTranscoder_State::AVTRANSCODER_PAUSED);
874 CHECK_AND_RETURN_RET_LOG(avErrCode == AV_ERR_OK, avErrCode,
875 "OH_AVTranscoder_Pause is called, OnStateChange failed!");
876 return AV_ERR_OK;
877 }
878
OH_AVTranscoder_Resume(OH_AVTranscoder * transcoder)879 OH_AVErrCode OH_AVTranscoder_Resume(OH_AVTranscoder *transcoder)
880 {
881 MEDIA_LOGI("OH_AVTranscoder_Resume enter.");
882 CHECK_AND_RETURN_RET_LOG(transcoder != nullptr, AV_ERR_INVALID_VAL,
883 "OH_AVTranscoder_Resume is called, transcoder is nullptr!");
884
885 NativeAVTranscoder *nativeTranscoder = reinterpret_cast<NativeAVTranscoder*>(transcoder);
886 CHECK_AND_RETURN_RET_LOG(nativeTranscoder != nullptr, AV_ERR_INVALID_VAL,
887 "OH_AVTranscoder_Resume is called, nativeTranscoder is nullptr!");
888
889 CHECK_AND_RETURN_RET_LOG(nativeTranscoder->CheckStateMachine(AVTranscoderOpts::RESUME) == AV_ERR_OK,
890 AV_ERR_OPERATE_NOT_PERMIT, "OH_AVTranscoder_Resume is called, CheckStateMachine failed!");
891
892 CHECK_AND_RETURN_RET_LOG(nativeTranscoder->CheckRepeatOptions(AVTranscoderOpts::RESUME) == AV_ERR_OK,
893 AV_ERR_OK, "OH_AVTranscoder_Resume is called, Resume operation was repeated!");
894
895 int32_t errCode = nativeTranscoder->transcoder_->Resume();
896 CHECK_AND_RETURN_RET_LOG(errCode == MSERR_OK, GetOHAVErrCode(errCode, "Resume"),
897 "OH_AVTranscoder_Resume is called, Resume failed!");
898
899 OH_AVErrCode avErrCode = nativeTranscoder->OnStateChange(OH_AVTranscoder_State::AVTRANSCODER_STARTED);
900 CHECK_AND_RETURN_RET_LOG(avErrCode == AV_ERR_OK, avErrCode,
901 "OH_AVTranscoder_Resume is called, OnStateChange failed!");
902 return AV_ERR_OK;
903 }
904
OH_AVTranscoder_Cancel(OH_AVTranscoder * transcoder)905 OH_AVErrCode OH_AVTranscoder_Cancel(OH_AVTranscoder *transcoder)
906 {
907 MEDIA_LOGI("OH_AVTranscoder_Cancel enter.");
908 CHECK_AND_RETURN_RET_LOG(transcoder != nullptr, AV_ERR_INVALID_VAL,
909 "OH_AVTranscoder_Cancel is called, transcoder is nullptr!");
910
911 NativeAVTranscoder *nativeTranscoder = reinterpret_cast<NativeAVTranscoder*>(transcoder);
912 CHECK_AND_RETURN_RET_LOG(nativeTranscoder != nullptr, AV_ERR_INVALID_VAL,
913 "OH_AVTranscoder_Cancel is called, nativeTranscoder is nullptr!");
914
915 CHECK_AND_RETURN_RET_LOG(nativeTranscoder->CheckStateMachine(AVTranscoderOpts::CANCEL) == AV_ERR_OK,
916 AV_ERR_OPERATE_NOT_PERMIT, "OH_AVTranscoder_Cancel is called, CheckStateMachine failed!");
917
918 CHECK_AND_RETURN_RET_LOG(nativeTranscoder->CheckRepeatOptions(AVTranscoderOpts::CANCEL) == AV_ERR_OK,
919 AV_ERR_OK, "OH_AVTranscoder_Cancel is called, Cancel operation was repeated!");
920
921 int32_t errCode = nativeTranscoder->transcoder_->Cancel();
922 CHECK_AND_RETURN_RET_LOG(errCode == MSERR_OK, GetOHAVErrCode(errCode, "Cancel"),
923 "OH_AVTranscoder_Cancel is called, Cancel failed!");
924
925 OH_AVErrCode avErrCode = nativeTranscoder->OnStateChange(OH_AVTranscoder_State::AVTRANSCODER_CANCELLED);
926 CHECK_AND_RETURN_RET_LOG(avErrCode == AV_ERR_OK, avErrCode,
927 "OH_AVTranscoder_Cancel is called, OnStateChange failed!");
928 return AV_ERR_OK;
929 }
930
OH_AVTranscoder_Release(OH_AVTranscoder * transcoder)931 OH_AVErrCode OH_AVTranscoder_Release(OH_AVTranscoder *transcoder)
932 {
933 MEDIA_LOGI("OH_AVTranscoder_Release enter.");
934 CHECK_AND_RETURN_RET_LOG(transcoder != nullptr, AV_ERR_INVALID_VAL,
935 "OH_AVTranscoder_Release is called, transcoder is originally nullptr!");
936
937 NativeAVTranscoder *nativeTranscoder = reinterpret_cast<NativeAVTranscoder*>(transcoder);
938 CHECK_AND_RETURN_RET_LOG(nativeTranscoder != nullptr, AV_ERR_INVALID_VAL,
939 "OH_AVTranscoder_Release is called, nativeTranscoder is nullptr!");
940
941 CHECK_AND_RETURN_RET_LOG(nativeTranscoder->CheckStateMachine(AVTranscoderOpts::RELEASE) == AV_ERR_OK,
942 AV_ERR_OPERATE_NOT_PERMIT, "OH_AVTranscoder_Release is called, CheckStateMachine failed!");
943
944 CHECK_AND_RETURN_RET_LOG(nativeTranscoder->CheckRepeatOptions(AVTranscoderOpts::RELEASE) == AV_ERR_OK,
945 AV_ERR_OK, "OH_AVTranscoder_Release is called, Release operation was repeated!");
946
947 int32_t errCode = nativeTranscoder->transcoder_->Release();
948 CHECK_AND_RETURN_RET_LOG(errCode == MSERR_OK, GetOHAVErrCode(errCode, "Release"),
949 "OH_AVTranscoder_Release is called, Release failed!");
950
951 delete transcoder;
952 transcoder = nullptr;
953 return AV_ERR_OK;
954 }
955
OH_AVTranscoder_SetStateCallback(OH_AVTranscoder * transcoder,OH_AVTranscoder_OnStateChange callback,void * userData)956 OH_AVErrCode OH_AVTranscoder_SetStateCallback(
957 OH_AVTranscoder *transcoder, OH_AVTranscoder_OnStateChange callback, void *userData)
958 {
959 MEDIA_LOGI("OH_AVTranscoder_SetStateCallback enter.");
960 CHECK_AND_RETURN_RET_LOG(transcoder != nullptr && callback != nullptr, AV_ERR_INVALID_VAL,
961 "OH_AVTranscoder_SetStateCallback is called, transcoder or callback is nullptr!");
962
963 NativeAVTranscoder *nativeTranscoder = reinterpret_cast<NativeAVTranscoder*>(transcoder);
964 CHECK_AND_RETURN_RET_LOG(nativeTranscoder != nullptr, AV_ERR_INVALID_VAL,
965 "OH_AVTranscoder_SetStateCallback is called, nativeTranscoder is nullptr!");
966
967 CHECK_AND_RETURN_RET_LOG(nativeTranscoder->transcoder_ != nullptr, AV_ERR_INVALID_VAL,
968 "OH_AVTranscoder_SetStateCallback is called, nativeTranscoder::transcoder_ is nullptr!");
969
970 if (nativeTranscoder->transcoderCb_ == nullptr) {
971 OH_AVErrCode ret = nativeTranscoder->SetTranscoderCallback();
972 CHECK_AND_RETURN_RET_LOG(ret == AV_ERR_OK, ret,
973 "OH_AVTranscoder_SetStateCallback is called, SetTranscoderCallback failed!");
974 }
975
976 NativeAVTranscoderCallback *nativeTranscoderCallback =
977 reinterpret_cast<NativeAVTranscoderCallback*>(nativeTranscoder->transcoderCb_.get());
978 CHECK_AND_RETURN_RET_LOG(nativeTranscoderCallback != nullptr, AV_ERR_INVALID_VAL,
979 "OH_AVTranscoder_SetStateCallback is called, nativeTranscoderCallback is nullptr!");
980
981 return nativeTranscoderCallback->SetOnStateChangeCallback(callback, userData);
982 }
983
OH_AVTranscoder_SetErrorCallback(OH_AVTranscoder * transcoder,OH_AVTranscoder_OnError callback,void * userData)984 OH_AVErrCode OH_AVTranscoder_SetErrorCallback(
985 OH_AVTranscoder *transcoder, OH_AVTranscoder_OnError callback, void *userData)
986 {
987 MEDIA_LOGI("OH_AVTranscoder_SetErrorCallback enter.");
988 CHECK_AND_RETURN_RET_LOG(transcoder != nullptr && callback != nullptr, AV_ERR_INVALID_VAL,
989 "OH_AVTranscoder_SetErrorCallback is called, transcoder or callback is nullptr!");
990
991 NativeAVTranscoder *nativeTranscoder = reinterpret_cast<NativeAVTranscoder*>(transcoder);
992 CHECK_AND_RETURN_RET_LOG(nativeTranscoder != nullptr, AV_ERR_INVALID_VAL,
993 "OH_AVTranscoder_SetErrorCallback is called, nativeTranscoder is nullptr!");
994
995 CHECK_AND_RETURN_RET_LOG(nativeTranscoder->transcoder_ != nullptr, AV_ERR_INVALID_VAL,
996 "OH_AVTranscoder_SetErrorCallback is called, nativeTranscoder::transcoder_ is nullptr!");
997
998 if (nativeTranscoder->transcoderCb_ == nullptr) {
999 OH_AVErrCode ret = nativeTranscoder->SetTranscoderCallback();
1000 CHECK_AND_RETURN_RET_LOG(ret == AV_ERR_OK, ret,
1001 "OH_AVTranscoder_SetErrorCallback is called, SetTranscoderCallback failed!");
1002 }
1003
1004 NativeAVTranscoderCallback *nativeTranscoderCallback =
1005 reinterpret_cast<NativeAVTranscoderCallback*>(nativeTranscoder->transcoderCb_.get());
1006 CHECK_AND_RETURN_RET_LOG(nativeTranscoderCallback != nullptr, AV_ERR_INVALID_VAL,
1007 "OH_AVTranscoder_SetErrorCallback is called, nativeTranscoderCallback is nullptr!");
1008
1009 return nativeTranscoderCallback->SetOnErrorCallback(callback, userData);
1010 }
1011
OH_AVTranscoder_SetProgressUpdateCallback(OH_AVTranscoder * transcoder,OH_AVTranscoder_OnProgressUpdate callback,void * userData)1012 OH_AVErrCode OH_AVTranscoder_SetProgressUpdateCallback(
1013 OH_AVTranscoder *transcoder, OH_AVTranscoder_OnProgressUpdate callback, void *userData)
1014 {
1015 MEDIA_LOGI("OH_AVTranscoder_SetProgressUpdateCallback enter.");
1016 CHECK_AND_RETURN_RET_LOG(transcoder != nullptr && callback != nullptr, AV_ERR_INVALID_VAL,
1017 "OH_AVTranscoder_SetProgressUpdateCallback is called, transcoder or callback is nullptr!");
1018
1019 NativeAVTranscoder *nativeTranscoder = reinterpret_cast<NativeAVTranscoder*>(transcoder);
1020 CHECK_AND_RETURN_RET_LOG(nativeTranscoder != nullptr, AV_ERR_INVALID_VAL,
1021 "OH_AVTranscoder_SetProgressUpdateCallback is called, nativeTranscoder is nullptr!");
1022
1023 CHECK_AND_RETURN_RET_LOG(nativeTranscoder->transcoder_ != nullptr, AV_ERR_INVALID_VAL,
1024 "OH_AVTranscoder_SetProgressUpdateCallback is called, transcoder_ is nullptr!");
1025
1026 if (nativeTranscoder->transcoderCb_ == nullptr) {
1027 OH_AVErrCode ret = nativeTranscoder->SetTranscoderCallback();
1028 CHECK_AND_RETURN_RET_LOG(ret == AV_ERR_OK, ret,
1029 "OH_AVTranscoder_SetProgressUpdateCallback is called, SetTranscoderCallback failed!");
1030 }
1031
1032 NativeAVTranscoderCallback *nativeTranscoderCallback =
1033 reinterpret_cast<NativeAVTranscoderCallback*>(nativeTranscoder->transcoderCb_.get());
1034 CHECK_AND_RETURN_RET_LOG(nativeTranscoderCallback != nullptr, AV_ERR_INVALID_VAL,
1035 "OH_AVTranscoder_SetProgressUpdateCallback is called, nativeTranscoderCallback is nullptr!");
1036
1037 return nativeTranscoderCallback->SetOnProgressUpdateCallback(callback, userData);
1038 }
1039
OH_AVTranscoderConfig_EnableBFrame(OH_AVTranscoder_Config * config,bool enabled)1040 OH_AVErrCode OH_AVTranscoderConfig_EnableBFrame(OH_AVTranscoder_Config *config, bool enabled)
1041 {
1042 MEDIA_LOGI("OH_AVTranscoderConfig_EnableBFrame enter.");
1043 CHECK_AND_RETURN_RET_LOG(config != nullptr, AV_ERR_INVALID_VAL,
1044 "OH_AVTranscoderConfig_EnableBFrame is called, config is nullptr!");
1045
1046 NativeAVTranscoderConfig* nativeConfig = reinterpret_cast<NativeAVTranscoderConfig*>(config);
1047 CHECK_AND_RETURN_RET_LOG(nativeConfig != nullptr, AV_ERR_INVALID_VAL,
1048 "OH_AVTranscoderConfig_EnableBFrame is called, nativeConfig is nullptr!");
1049
1050 nativeConfig->enableBFrame = enabled;
1051
1052 return AV_ERR_OK;
1053 }