• 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 "codec/common/codec_encoder.h"
17 #include "codec/util/codec_util.h"
18 #include "media_log.h"
19 
20 namespace OHOS {
21 namespace Media {
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_VIDEOEDITOR, "VideoEditorEncode"};
24 }
25 
CodecEncoder(uint64_t id,std::string logTag)26 CodecEncoder::CodecEncoder(uint64_t id, std::string logTag)
27     : id_(id), logTag_(logTag + "-" + std::to_string(id))
28 {
29 }
30 
~CodecEncoder()31 CodecEncoder::~CodecEncoder()
32 {
33 }
34 
GetAVCodecAsyncCallback()35 OH_AVCodecAsyncCallback CodecEncoder::GetAVCodecAsyncCallback()
36 {
37     auto CodecOnErrorImpl = [](OH_AVCodec* codec, int32_t errorCode, void* context) {
38         auto error = static_cast<OH_AVErrCode>(errorCode);
39         CodecEncoder* encoder = static_cast<CodecEncoder*>(context);
40         if (encoder == nullptr) {
41             MEDIA_LOGE("encoder CodecOnError, context is nullptr, error: %{public}d(%{public}s).", error,
42                 CodecUtil::GetCodecErrorStr(error));
43             return;
44         }
45         encoder->CodecOnErrorInner(codec, errorCode);
46     };
47     auto CodecOnStreamChangedImpl = [](OH_AVCodec*, OH_AVFormat* format, void* context) {
48         CodecEncoder* encoder = static_cast<CodecEncoder*>(context);
49         if (encoder == nullptr) {
50             MEDIA_LOGE("encoder CodecOnStreamChanged, context is nullptr.");
51             return;
52         }
53         encoder->CodecOnStreamChangedInner(format);
54     };
55     auto CodecOnNeedInputDataImpl = [](OH_AVCodec* codec, uint32_t index, OH_AVMemory* data, void* context) {
56         CodecEncoder* encoder = static_cast<CodecEncoder*>(context);
57         if (encoder == nullptr) {
58             MEDIA_LOGE("encoder CodecOnNeedInputData, context is nullptr.");
59             return;
60         }
61         encoder->CodecOnNeedInputDataInner(codec, index, data);
62     };
63     auto CodecOnNewOutputDataImpl = [](OH_AVCodec* codec, uint32_t index, OH_AVMemory* data, OH_AVCodecBufferAttr* attr,
64         void* context) {
65         CodecEncoder* encoder = static_cast<CodecEncoder*>(context);
66         if (encoder == nullptr) {
67             MEDIA_LOGE("encoder CodecOnNewOutputData, context is nullptr.");
68             return;
69         }
70         encoder->CodecOnNewOutputDataInner(codec, index, data, attr);
71     };
72     // 设置回调
73     return { CodecOnErrorImpl, CodecOnStreamChangedImpl, CodecOnNeedInputDataImpl, CodecOnNewOutputDataImpl };
74 }
75 } // namespace Media
76 } // namespace OHOS