/* * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "dynamic_controller.h" #include "avcodec_errors.h" namespace OHOS { namespace MediaAVCodec { namespace PostProcessing { DynamicController::~DynamicController() { if (instance_ != nullptr) { AVCODEC_LOGW("Instance is not correctly destroyed."); } UnloadInterfacesImpl(); } bool DynamicController::LoadInterfacesImpl() { if (ready_) { AVCODEC_LOGD("Controller is ready."); return true; } if (interface_.Load()) { ready_ = true; return true; } return false; } void DynamicController::UnloadInterfacesImpl() { interface_.Unload(); ready_ = false; } bool DynamicController::IsColorSpaceConversionSupportedImpl(const CapabilityInfo& input, const CapabilityInfo& output) { auto inputPtr = static_cast(&input); auto outputPtr = static_cast(&output); return interface_.Invoke(inputPtr, outputPtr); } int32_t DynamicController::CreateImpl() { instance_ = static_cast(interface_.Invoke()); CHECK_AND_RETURN_RET_LOG(instance_ != nullptr, AVCS_ERR_UNKNOWN, "Create VPE instance failed."); return AVCS_ERR_OK; } void DynamicController::DestroyImpl() { interface_.Invoke(instance_); instance_ = nullptr; } int32_t DynamicController::SetCallbackImpl(void* callback, void* userData) { auto ret = interface_.Invoke(instance_, callback, userData); CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Set callback for video processing failed."); return AVCS_ERR_OK; } int32_t DynamicController::SetOutputSurfaceImpl(sptr surface) { void* sf = static_cast(&surface); auto ret = interface_.Invoke(instance_, sf); CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Set output surface for video processing failed."); surface->UnRegisterReleaseListener(); surface->RegisterReleaseListener([this](sptr &buffer) -> GSError { return OnProducerBufferReleased(buffer); }); return AVCS_ERR_OK; } int32_t DynamicController::CreateInputSurfaceImpl(sptr& surface) { void* sf = static_cast(&surface); auto ret = interface_.Invoke(instance_, sf); CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK && surface != nullptr, AVCS_ERR_UNKNOWN, "Create input surface for video processing failed."); return AVCS_ERR_OK; } int32_t DynamicController::SetParameterImpl(Media::Format& parameter) { void* parameterPtr = static_cast(¶meter); auto ret = interface_.Invoke(instance_, parameterPtr); CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Set parameter for VPE failed."); return AVCS_ERR_OK; } int32_t DynamicController::GetParameterImpl(Media::Format& parameter) { void* parameterPtr = static_cast(¶meter); auto ret = interface_.Invoke(instance_, parameterPtr); CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Get parameter for VPE failed."); return AVCS_ERR_OK; } int32_t DynamicController::ConfigureImpl(Media::Format& config) { void* configPtr = static_cast(&config); auto ret = interface_.Invoke(instance_, configPtr); CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Configure video processing failed."); return AVCS_ERR_OK; } int32_t DynamicController::PrepareImpl() { auto ret = interface_.Invoke(instance_); CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Prepare video processing failed."); return AVCS_ERR_OK; } int32_t DynamicController::StartImpl() { auto ret = interface_.Invoke(instance_); CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Start video processing failed."); return AVCS_ERR_OK; } int32_t DynamicController::StopImpl() { auto ret = interface_.Invoke(instance_); CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Stop video processing failed."); return AVCS_ERR_OK; } int32_t DynamicController::FlushImpl() { auto ret = interface_.Invoke(instance_); CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Flush video processing failed."); return AVCS_ERR_OK; } int32_t DynamicController::GetOutputFormatImpl(Media::Format &format) { void *formatPtr = static_cast(&format); auto ret = interface_.Invoke(instance_, formatPtr); CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "GetOutputFormat video processing failed."); return AVCS_ERR_OK; } int32_t DynamicController::ResetImpl() { auto ret = interface_.Invoke(instance_); CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Reset video processing failed."); return AVCS_ERR_OK; } int32_t DynamicController::ReleaseImpl() { auto ret = interface_.Invoke(instance_); CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Release video processing failed."); return AVCS_ERR_OK; } int32_t DynamicController::ReleaseOutputBufferImpl(uint32_t index, bool render) { auto ret = interface_.Invoke(instance_, index, render); CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Release output buffer of video processing failed."); return AVCS_ERR_OK; } GSError DynamicController::OnProducerBufferReleased([[maybe_unused]] sptr &buffer) { auto ret = interface_.Invoke(instance_); return static_cast(ret); } } // namespace PostProcessing } // namespace MediaAVCodec } // namespace OHOS