1da853ecaSopenharmony_ci/* 2da853ecaSopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd. 3da853ecaSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4da853ecaSopenharmony_ci * you may not use this file except in compliance with the License. 5da853ecaSopenharmony_ci * You may obtain a copy of the License at 6da853ecaSopenharmony_ci * 7da853ecaSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8da853ecaSopenharmony_ci * 9da853ecaSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10da853ecaSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11da853ecaSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12da853ecaSopenharmony_ci * See the License for the specific language governing permissions and 13da853ecaSopenharmony_ci * limitations under the License. 14da853ecaSopenharmony_ci */ 15da853ecaSopenharmony_ci 16da853ecaSopenharmony_ci#include "avmuxer_impl.h" 17da853ecaSopenharmony_ci#include <unistd.h> 18da853ecaSopenharmony_ci#include <fcntl.h> 19da853ecaSopenharmony_ci#include "securec.h" 20da853ecaSopenharmony_ci#include "avcodec_trace.h" 21da853ecaSopenharmony_ci#include "avcodec_log.h" 22da853ecaSopenharmony_ci#include "avcodec_errors.h" 23da853ecaSopenharmony_ci 24da853ecaSopenharmony_cinamespace { 25da853ecaSopenharmony_ciconstexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_MUXER, "AVMuxerImpl"}; 26da853ecaSopenharmony_ci} 27da853ecaSopenharmony_ci 28da853ecaSopenharmony_cinamespace OHOS { 29da853ecaSopenharmony_cinamespace MediaAVCodec { 30da853ecaSopenharmony_cistd::shared_ptr<AVMuxer> AVMuxerFactory::CreateAVMuxer(int32_t fd, Plugins::OutputFormat format) 31da853ecaSopenharmony_ci{ 32da853ecaSopenharmony_ci AVCODEC_SYNC_TRACE; 33da853ecaSopenharmony_ci CHECK_AND_RETURN_RET_LOG(fd >= 0, nullptr, "fd %{public}d is error!", fd); 34da853ecaSopenharmony_ci uint32_t fdPermission = static_cast<uint32_t>(fcntl(fd, F_GETFL, 0)); 35da853ecaSopenharmony_ci CHECK_AND_RETURN_RET_LOG((fdPermission & O_WRONLY) == O_WRONLY || (fdPermission & O_RDWR) == O_RDWR, 36da853ecaSopenharmony_ci nullptr, "No permission to write fd."); 37da853ecaSopenharmony_ci CHECK_AND_RETURN_RET_LOG(lseek(fd, 0, SEEK_CUR) != -1, nullptr, "The fd is not seekable"); 38da853ecaSopenharmony_ci 39da853ecaSopenharmony_ci std::shared_ptr<AVMuxerImpl> impl = std::make_shared<AVMuxerImpl>(); 40da853ecaSopenharmony_ci 41da853ecaSopenharmony_ci int32_t ret = impl->Init(fd, format); 42da853ecaSopenharmony_ci CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, nullptr, "Init avmuxer implementation failed"); 43da853ecaSopenharmony_ci return impl; 44da853ecaSopenharmony_ci} 45da853ecaSopenharmony_ci 46da853ecaSopenharmony_ciAVMuxerImpl::AVMuxerImpl() 47da853ecaSopenharmony_ci{ 48da853ecaSopenharmony_ci AVCODEC_LOGD("AVMuxerImpl:0x%{public}06" PRIXPTR " Instances create", FAKE_POINTER(this)); 49da853ecaSopenharmony_ci} 50da853ecaSopenharmony_ci 51da853ecaSopenharmony_ciAVMuxerImpl::~AVMuxerImpl() 52da853ecaSopenharmony_ci{ 53da853ecaSopenharmony_ci AVCODEC_LOGD("AVMuxerImpl:0x%{public}06" PRIXPTR " Instances destroy", FAKE_POINTER(this)); 54da853ecaSopenharmony_ci} 55da853ecaSopenharmony_ci 56da853ecaSopenharmony_ciint32_t AVMuxerImpl::Init(int32_t fd, Plugins::OutputFormat format) 57da853ecaSopenharmony_ci{ 58da853ecaSopenharmony_ci AVCODEC_SYNC_TRACE; 59da853ecaSopenharmony_ci muxerEngine_ = std::make_shared<Media::MediaMuxer>(getuid(), getprocpid()); 60da853ecaSopenharmony_ci CHECK_AND_RETURN_RET_LOG(muxerEngine_ != nullptr, AVCS_ERR_NO_MEMORY, "Create AVMuxer Engine failed"); 61da853ecaSopenharmony_ci return StatusConvert(muxerEngine_->Init(fd, format)); 62da853ecaSopenharmony_ci} 63da853ecaSopenharmony_ci 64da853ecaSopenharmony_ciint32_t AVMuxerImpl::SetParameter(const std::shared_ptr<Meta> ¶m) 65da853ecaSopenharmony_ci{ 66da853ecaSopenharmony_ci AVCODEC_SYNC_TRACE; 67da853ecaSopenharmony_ci CHECK_AND_RETURN_RET_LOG(muxerEngine_ != nullptr, AVCS_ERR_INVALID_OPERATION, "AVMuxer Engine does not exist"); 68da853ecaSopenharmony_ci CHECK_AND_RETURN_RET_LOG(param != nullptr, AVCS_ERR_INVALID_VAL, "Invalid parameter"); 69da853ecaSopenharmony_ci return StatusConvert(muxerEngine_->SetParameter(param)); 70da853ecaSopenharmony_ci} 71da853ecaSopenharmony_ci 72da853ecaSopenharmony_ciint32_t AVMuxerImpl::SetUserMeta(const std::shared_ptr<Meta> &userMeta) 73da853ecaSopenharmony_ci{ 74da853ecaSopenharmony_ci AVCODEC_SYNC_TRACE; 75da853ecaSopenharmony_ci CHECK_AND_RETURN_RET_LOG(muxerEngine_ != nullptr, AVCS_ERR_INVALID_OPERATION, "AVMuxer Engine does not exist"); 76da853ecaSopenharmony_ci CHECK_AND_RETURN_RET_LOG(userMeta != nullptr, AVCS_ERR_INVALID_VAL, "Invalid parameter"); 77da853ecaSopenharmony_ci return StatusConvert(muxerEngine_->SetUserMeta(userMeta)); 78da853ecaSopenharmony_ci} 79da853ecaSopenharmony_ci 80da853ecaSopenharmony_ciint32_t AVMuxerImpl::AddTrack(int32_t &trackIndex, const std::shared_ptr<Meta> &trackDesc) 81da853ecaSopenharmony_ci{ 82da853ecaSopenharmony_ci AVCODEC_SYNC_TRACE; 83da853ecaSopenharmony_ci CHECK_AND_RETURN_RET_LOG(muxerEngine_ != nullptr, AVCS_ERR_INVALID_OPERATION, "AVMuxer Engine does not exist"); 84da853ecaSopenharmony_ci CHECK_AND_RETURN_RET_LOG(trackDesc != nullptr, AVCS_ERR_INVALID_VAL, "Invalid track format"); 85da853ecaSopenharmony_ci return StatusConvert(muxerEngine_->AddTrack(trackIndex, trackDesc)); 86da853ecaSopenharmony_ci} 87da853ecaSopenharmony_ci 88da853ecaSopenharmony_cisptr<AVBufferQueueProducer> AVMuxerImpl::GetInputBufferQueue(uint32_t trackIndex) 89da853ecaSopenharmony_ci{ 90da853ecaSopenharmony_ci AVCODEC_SYNC_TRACE; 91da853ecaSopenharmony_ci CHECK_AND_RETURN_RET_LOG(muxerEngine_ != nullptr, nullptr, "AVMuxer Engine does not exist"); 92da853ecaSopenharmony_ci return muxerEngine_->GetInputBufferQueue(trackIndex); 93da853ecaSopenharmony_ci} 94da853ecaSopenharmony_ci 95da853ecaSopenharmony_ciint32_t AVMuxerImpl::Start() 96da853ecaSopenharmony_ci{ 97da853ecaSopenharmony_ci AVCODEC_SYNC_TRACE; 98da853ecaSopenharmony_ci CHECK_AND_RETURN_RET_LOG(muxerEngine_ != nullptr, AVCS_ERR_INVALID_OPERATION, "AVMuxer Engine does not exist"); 99da853ecaSopenharmony_ci return StatusConvert(muxerEngine_->Start()); 100da853ecaSopenharmony_ci} 101da853ecaSopenharmony_ci 102da853ecaSopenharmony_ciint32_t AVMuxerImpl::WriteSample(uint32_t trackIndex, const std::shared_ptr<AVBuffer> &sample) 103da853ecaSopenharmony_ci{ 104da853ecaSopenharmony_ci AVCODEC_SYNC_TRACE; 105da853ecaSopenharmony_ci CHECK_AND_RETURN_RET_LOG(muxerEngine_ != nullptr, AVCS_ERR_INVALID_OPERATION, "AVMuxer Engine does not exist"); 106da853ecaSopenharmony_ci CHECK_AND_RETURN_RET_LOG(sample != nullptr && sample->memory_ != nullptr && 107da853ecaSopenharmony_ci sample->memory_->GetSize() >= 0, AVCS_ERR_INVALID_VAL, "Invalid memory"); 108da853ecaSopenharmony_ci return StatusConvert(muxerEngine_->WriteSample(trackIndex, sample)); 109da853ecaSopenharmony_ci} 110da853ecaSopenharmony_ci 111da853ecaSopenharmony_ciint32_t AVMuxerImpl::Stop() 112da853ecaSopenharmony_ci{ 113da853ecaSopenharmony_ci AVCODEC_SYNC_TRACE; 114da853ecaSopenharmony_ci CHECK_AND_RETURN_RET_LOG(muxerEngine_ != nullptr, AVCS_ERR_INVALID_OPERATION, "AVMuxer Engine does not exist"); 115da853ecaSopenharmony_ci return StatusConvert(muxerEngine_->Stop()); 116da853ecaSopenharmony_ci} 117da853ecaSopenharmony_ci 118da853ecaSopenharmony_ciint32_t AVMuxerImpl::StatusConvert(Media::Status status) 119da853ecaSopenharmony_ci{ 120da853ecaSopenharmony_ci const static std::unordered_map<Media::Status, int32_t> table = { 121da853ecaSopenharmony_ci {Status::END_OF_STREAM, AVCodecServiceErrCode::AVCS_ERR_OK}, 122da853ecaSopenharmony_ci {Status::OK, AVCodecServiceErrCode::AVCS_ERR_OK}, 123da853ecaSopenharmony_ci {Status::NO_ERROR, AVCodecServiceErrCode::AVCS_ERR_OK}, 124da853ecaSopenharmony_ci {Status::ERROR_UNKNOWN, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN}, 125da853ecaSopenharmony_ci {Status::ERROR_PLUGIN_ALREADY_EXISTS, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN}, 126da853ecaSopenharmony_ci {Status::ERROR_INCOMPATIBLE_VERSION, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN}, 127da853ecaSopenharmony_ci {Status::ERROR_NO_MEMORY, AVCodecServiceErrCode::AVCS_ERR_NO_MEMORY}, 128da853ecaSopenharmony_ci {Status::ERROR_WRONG_STATE, AVCodecServiceErrCode::AVCS_ERR_INVALID_OPERATION}, 129da853ecaSopenharmony_ci {Status::ERROR_UNIMPLEMENTED, AVCodecServiceErrCode::AVCS_ERR_UNSUPPORT}, 130da853ecaSopenharmony_ci {Status::ERROR_INVALID_PARAMETER, AVCodecServiceErrCode::AVCS_ERR_INVALID_VAL}, 131da853ecaSopenharmony_ci {Status::ERROR_INVALID_DATA, AVCodecServiceErrCode::AVCS_ERR_INVALID_VAL}, 132da853ecaSopenharmony_ci {Status::ERROR_MISMATCHED_TYPE, AVCodecServiceErrCode::AVCS_ERR_INVALID_VAL}, 133da853ecaSopenharmony_ci {Status::ERROR_TIMED_OUT, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN}, 134da853ecaSopenharmony_ci {Status::ERROR_UNSUPPORTED_FORMAT, AVCodecServiceErrCode::AVCS_ERR_UNSUPPORT_FILE_TYPE}, 135da853ecaSopenharmony_ci {Status::ERROR_NOT_ENOUGH_DATA, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN}, 136da853ecaSopenharmony_ci {Status::ERROR_NOT_EXISTED, AVCodecServiceErrCode::AVCS_ERR_OPEN_FILE_FAILED}, 137da853ecaSopenharmony_ci {Status::ERROR_AGAIN, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN}, 138da853ecaSopenharmony_ci {Status::ERROR_PERMISSION_DENIED, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN}, 139da853ecaSopenharmony_ci {Status::ERROR_NULL_POINTER, AVCodecServiceErrCode::AVCS_ERR_INVALID_VAL}, 140da853ecaSopenharmony_ci {Status::ERROR_INVALID_OPERATION, AVCodecServiceErrCode::AVCS_ERR_INVALID_OPERATION}, 141da853ecaSopenharmony_ci {Status::ERROR_CLIENT, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN}, 142da853ecaSopenharmony_ci {Status::ERROR_SERVER, AVCodecServiceErrCode::AVCS_ERR_UNKNOWN}, 143da853ecaSopenharmony_ci {Status::ERROR_DELAY_READY, AVCodecServiceErrCode::AVCS_ERR_OK}, 144da853ecaSopenharmony_ci {Status::ERROR_INVALID_BUFFER_SIZE, AVCodecServiceErrCode::AVCS_ERR_INVALID_VAL}, 145da853ecaSopenharmony_ci }; 146da853ecaSopenharmony_ci auto ite = table.find(status); 147da853ecaSopenharmony_ci if (ite == table.end()) { 148da853ecaSopenharmony_ci return AVCodecServiceErrCode::AVCS_ERR_UNKNOWN; 149da853ecaSopenharmony_ci } 150da853ecaSopenharmony_ci return ite->second; 151da853ecaSopenharmony_ci} 152da853ecaSopenharmony_ci} // namespace MediaAVCodec 153da853ecaSopenharmony_ci} // namespace OHOS