1 /*
2 * Copyright (C) 2023 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 "native_avmuxer.h"
17 #include "native_avmagic.h"
18 #include "avmuxer.h"
19 #include "avcodec_log.h"
20 #include "avcodec_errors.h"
21
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "NativeAVMuxer"};
24 }
25
26 using namespace OHOS::MediaAVCodec;
27
28 struct AVMuxerObject : public OH_AVMuxer {
AVMuxerObjectAVMuxerObject29 explicit AVMuxerObject(const std::shared_ptr<AVMuxer> &muxer)
30 : OH_AVMuxer(AVMagic::AVCODEC_MAGIC_AVMUXER), muxer_(muxer) {}
31 ~AVMuxerObject() = default;
32
33 const std::shared_ptr<AVMuxer> muxer_;
34 };
35
OH_AVMuxer_Create(int32_t fd,OH_AVOutputFormat format)36 struct OH_AVMuxer *OH_AVMuxer_Create(int32_t fd, OH_AVOutputFormat format)
37 {
38 std::shared_ptr<AVMuxer> avmuxer = AVMuxerFactory::CreateAVMuxer(fd, static_cast<OutputFormat>(format));
39 CHECK_AND_RETURN_RET_LOG(avmuxer != nullptr, nullptr, "create muxer failed!");
40 struct AVMuxerObject *object = new(std::nothrow) AVMuxerObject(avmuxer);
41 return object;
42 }
43
OH_AVMuxer_SetRotation(OH_AVMuxer * muxer,int32_t rotation)44 OH_AVErrCode OH_AVMuxer_SetRotation(OH_AVMuxer *muxer, int32_t rotation)
45 {
46 CHECK_AND_RETURN_RET_LOG(muxer != nullptr, AV_ERR_INVALID_VAL, "input muxer is nullptr!");
47 CHECK_AND_RETURN_RET_LOG(muxer->magic_ == AVMagic::AVCODEC_MAGIC_AVMUXER, AV_ERR_INVALID_VAL, "magic error!");
48
49 struct AVMuxerObject *object = reinterpret_cast<AVMuxerObject *>(muxer);
50 CHECK_AND_RETURN_RET_LOG(object->muxer_ != nullptr, AV_ERR_INVALID_VAL, "muxer_ is nullptr!");
51
52 int32_t ret = object->muxer_->SetRotation(rotation);
53 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
54 "muxer_ SetRotation failed!");
55
56 return AV_ERR_OK;
57 }
58
OH_AVMuxer_AddTrack(OH_AVMuxer * muxer,int32_t * trackIndex,OH_AVFormat * trackFormat)59 OH_AVErrCode OH_AVMuxer_AddTrack(OH_AVMuxer *muxer, int32_t *trackIndex, OH_AVFormat *trackFormat)
60 {
61 CHECK_AND_RETURN_RET_LOG(muxer != nullptr, AV_ERR_INVALID_VAL, "input muxer is nullptr!");
62 CHECK_AND_RETURN_RET_LOG(muxer->magic_ == AVMagic::AVCODEC_MAGIC_AVMUXER, AV_ERR_INVALID_VAL, "magic error!");
63 CHECK_AND_RETURN_RET_LOG(trackIndex != nullptr, AV_ERR_INVALID_VAL, "input track index is nullptr!");
64 CHECK_AND_RETURN_RET_LOG(trackFormat != nullptr, AV_ERR_INVALID_VAL, "input track format is nullptr!");
65 CHECK_AND_RETURN_RET_LOG(trackFormat->magic_ == AVMagic::AVCODEC_MAGIC_FORMAT, AV_ERR_INVALID_VAL, "magic error!");
66
67 struct AVMuxerObject *object = reinterpret_cast<AVMuxerObject *>(muxer);
68 CHECK_AND_RETURN_RET_LOG(object->muxer_ != nullptr, AV_ERR_INVALID_VAL, "muxer_ is nullptr!");
69
70 int32_t ret = object->muxer_->AddTrack(*trackIndex, trackFormat->format_);
71 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
72 "muxer_ AddTrack failed!");
73
74 return AV_ERR_OK;
75 }
76
OH_AVMuxer_Start(OH_AVMuxer * muxer)77 OH_AVErrCode OH_AVMuxer_Start(OH_AVMuxer *muxer)
78 {
79 CHECK_AND_RETURN_RET_LOG(muxer != nullptr, AV_ERR_INVALID_VAL, "input muxer is nullptr!");
80 CHECK_AND_RETURN_RET_LOG(muxer->magic_ == AVMagic::AVCODEC_MAGIC_AVMUXER, AV_ERR_INVALID_VAL, "magic error!");
81
82 struct AVMuxerObject *object = reinterpret_cast<AVMuxerObject *>(muxer);
83 CHECK_AND_RETURN_RET_LOG(object->muxer_ != nullptr, AV_ERR_INVALID_VAL, "muxer_ is nullptr!");
84
85 int32_t ret = object->muxer_->Start();
86 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
87 "muxer_ Start failed!");
88
89 return AV_ERR_OK;
90 }
91
OH_AVMuxer_WriteSample(OH_AVMuxer * muxer,uint32_t trackIndex,OH_AVMemory * sample,OH_AVCodecBufferAttr info)92 OH_AVErrCode OH_AVMuxer_WriteSample(OH_AVMuxer *muxer,
93 uint32_t trackIndex,
94 OH_AVMemory *sample,
95 OH_AVCodecBufferAttr info)
96 {
97 CHECK_AND_RETURN_RET_LOG(muxer != nullptr, AV_ERR_INVALID_VAL, "input muxer is nullptr!");
98 CHECK_AND_RETURN_RET_LOG(muxer->magic_ == AVMagic::AVCODEC_MAGIC_AVMUXER, AV_ERR_INVALID_VAL, "magic error!");
99 CHECK_AND_RETURN_RET_LOG(sample != nullptr, AV_ERR_INVALID_VAL, "Invalid memory");
100 CHECK_AND_RETURN_RET_LOG(info.offset >= 0 && info.size >= 0, AV_ERR_INVALID_VAL, "Invalid memory");
101
102 struct AVMuxerObject *object = reinterpret_cast<AVMuxerObject *>(muxer);
103 CHECK_AND_RETURN_RET_LOG(object->muxer_ != nullptr, AV_ERR_INVALID_VAL, "muxer_ is nullptr!");
104
105 AVCodecBufferInfo sampleInfo;
106 sampleInfo.presentationTimeUs = info.pts;
107 sampleInfo.size = info.size;
108 sampleInfo.offset = info.offset;
109 AVCodecBufferFlag flag = static_cast<AVCodecBufferFlag>(info.flags);
110
111 int32_t ret = object->muxer_->WriteSample(trackIndex, sample->memory_, sampleInfo, flag);
112 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
113 "muxer_ WriteSample failed!");
114
115 return AV_ERR_OK;
116 }
117
OH_AVMuxer_Stop(OH_AVMuxer * muxer)118 OH_AVErrCode OH_AVMuxer_Stop(OH_AVMuxer *muxer)
119 {
120 CHECK_AND_RETURN_RET_LOG(muxer != nullptr, AV_ERR_INVALID_VAL, "input muxer is nullptr!");
121 CHECK_AND_RETURN_RET_LOG(muxer->magic_ == AVMagic::AVCODEC_MAGIC_AVMUXER, AV_ERR_INVALID_VAL, "magic error!");
122
123 struct AVMuxerObject *object = reinterpret_cast<AVMuxerObject *>(muxer);
124 CHECK_AND_RETURN_RET_LOG(object->muxer_ != nullptr, AV_ERR_INVALID_VAL, "muxer_ is nullptr!");
125
126 int32_t ret = object->muxer_->Stop();
127 CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCSErrorToOHAVErrCode(static_cast<AVCodecServiceErrCode>(ret)),
128 "muxer_ Stop failed!");
129
130 return AV_ERR_OK;
131 }
132
OH_AVMuxer_Destroy(OH_AVMuxer * muxer)133 OH_AVErrCode OH_AVMuxer_Destroy(OH_AVMuxer *muxer)
134 {
135 CHECK_AND_RETURN_RET_LOG(muxer != nullptr, AV_ERR_INVALID_VAL, "input muxer is nullptr!");
136 CHECK_AND_RETURN_RET_LOG(muxer->magic_ == AVMagic::AVCODEC_MAGIC_AVMUXER, AV_ERR_INVALID_VAL, "magic error!");
137
138 delete muxer;
139
140 return AV_ERR_OK;
141 }