• 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 "dynamic_controller.h"
17 #include "avcodec_errors.h"
18 
19 namespace OHOS {
20 namespace MediaAVCodec {
21 namespace PostProcessing {
22 
~DynamicController()23 DynamicController::~DynamicController()
24 {
25     if (instance_ != nullptr) {
26         AVCODEC_LOGW("Instance is not correctly destroyed.");
27     }
28 
29     UnloadInterfacesImpl();
30 }
31 
LoadInterfacesImpl()32 bool DynamicController::LoadInterfacesImpl()
33 {
34     if (ready_) {
35         AVCODEC_LOGD("Controller is ready.");
36         return true;
37     }
38     if (interface_.Load()) {
39         ready_ = true;
40         return true;
41     }
42     return false;
43 }
44 
UnloadInterfacesImpl()45 void DynamicController::UnloadInterfacesImpl()
46 {
47     interface_.Unload();
48     ready_ = false;
49 }
50 
IsColorSpaceConversionSupportedImpl(const CapabilityInfo & input,const CapabilityInfo & output)51 bool DynamicController::IsColorSpaceConversionSupportedImpl(const CapabilityInfo& input, const CapabilityInfo& output)
52 {
53     auto inputPtr = static_cast<const void*>(&input);
54     auto outputPtr = static_cast<const void*>(&output);
55     return interface_.Invoke<DynamicInterfaceName::IS_COLORSPACE_CONVERSION_SUPPORTED>(inputPtr, outputPtr);
56 }
57 
CreateImpl()58 int32_t DynamicController::CreateImpl()
59 {
60     instance_ = static_cast<DynamicColorSpaceConverterHandle*>(interface_.Invoke<DynamicInterfaceName::CREATE>());
61     CHECK_AND_RETURN_RET_LOG(instance_ != nullptr, AVCS_ERR_UNKNOWN, "Create VPE instance failed.");
62     return AVCS_ERR_OK;
63 }
64 
DestroyImpl()65 void DynamicController::DestroyImpl()
66 {
67     interface_.Invoke<DynamicInterfaceName::DESTROY>(instance_);
68     instance_ = nullptr;
69 }
70 
SetCallbackImpl(void * callback,void * userData)71 int32_t DynamicController::SetCallbackImpl(void* callback, void* userData)
72 {
73     auto ret = interface_.Invoke<DynamicInterfaceName::SET_CALLBACK>(instance_, callback, userData);
74     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN,
75         "Set callback for video processing failed.");
76     return AVCS_ERR_OK;
77 }
78 
SetOutputSurfaceImpl(sptr<Surface> surface)79 int32_t DynamicController::SetOutputSurfaceImpl(sptr<Surface> surface)
80 {
81     void* sf = static_cast<void*>(&surface);
82     auto ret = interface_.Invoke<DynamicInterfaceName::SET_OUTPUT_SURFACE>(instance_, sf);
83     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN,
84         "Set output surface for video processing failed.");
85     surface->UnRegisterReleaseListener();
86     surface->RegisterReleaseListener([this](sptr<SurfaceBuffer> &buffer) -> GSError {
87         return OnProducerBufferReleased(buffer);
88     });
89     return AVCS_ERR_OK;
90 }
91 
CreateInputSurfaceImpl(sptr<Surface> & surface)92 int32_t DynamicController::CreateInputSurfaceImpl(sptr<Surface>& surface)
93 {
94     void* sf = static_cast<void*>(&surface);
95     auto ret = interface_.Invoke<DynamicInterfaceName::CREATE_INPUT_SURFACE>(instance_, sf);
96     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK && surface != nullptr, AVCS_ERR_UNKNOWN,
97         "Create input surface for video processing failed.");
98     return AVCS_ERR_OK;
99 }
100 
ConfigureImpl(Media::Format & config)101 int32_t DynamicController::ConfigureImpl(Media::Format& config)
102 {
103     void* configPtr = static_cast<void*>(&config);
104     auto ret = interface_.Invoke<DynamicInterfaceName::CONFIGURE>(instance_, configPtr);
105     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Configure video processing failed.");
106     return AVCS_ERR_OK;
107 }
108 
PrepareImpl()109 int32_t DynamicController::PrepareImpl()
110 {
111     auto ret = interface_.Invoke<DynamicInterfaceName::PREPARE>(instance_);
112     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Prepare video processing failed.");
113     return AVCS_ERR_OK;
114 }
115 
StartImpl()116 int32_t DynamicController::StartImpl()
117 {
118     auto ret = interface_.Invoke<DynamicInterfaceName::START>(instance_);
119     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Start video processing failed.");
120     return AVCS_ERR_OK;
121 }
122 
StopImpl()123 int32_t DynamicController::StopImpl()
124 {
125     auto ret = interface_.Invoke<DynamicInterfaceName::STOP>(instance_);
126     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Stop video processing failed.");
127     return AVCS_ERR_OK;
128 }
129 
FlushImpl()130 int32_t DynamicController::FlushImpl()
131 {
132     auto ret = interface_.Invoke<DynamicInterfaceName::FLUSH>(instance_);
133     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Flush video processing failed.");
134     return AVCS_ERR_OK;
135 }
136 
GetOutputFormatImpl(Media::Format & format)137 int32_t DynamicController::GetOutputFormatImpl(Media::Format &format)
138 {
139     void *formatPtr = static_cast<void *>(&format);
140     auto ret = interface_.Invoke<DynamicInterfaceName::GET_OUTPUT_FORMAT>(instance_, formatPtr);
141     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN,
142                              "GetOutputFormat video processing failed.");
143     return AVCS_ERR_OK;
144 }
145 
ResetImpl()146 int32_t DynamicController::ResetImpl()
147 {
148     auto ret = interface_.Invoke<DynamicInterfaceName::RESET>(instance_);
149     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Reset video processing failed.");
150     return AVCS_ERR_OK;
151 }
152 
ReleaseImpl()153 int32_t DynamicController::ReleaseImpl()
154 {
155     auto ret = interface_.Invoke<DynamicInterfaceName::RELEASE>(instance_);
156     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Release video processing failed.");
157     return AVCS_ERR_OK;
158 }
159 
ReleaseOutputBufferImpl(uint32_t index,bool render)160 int32_t DynamicController::ReleaseOutputBufferImpl(uint32_t index, bool render)
161 {
162     auto ret = interface_.Invoke<DynamicInterfaceName::RELEASE_OUPUT_BUFFER>(instance_, index, render);
163     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN,
164         "Release output buffer of video processing failed.");
165     return AVCS_ERR_OK;
166 }
167 
OnProducerBufferReleased(sptr<SurfaceBuffer> & buffer)168 GSError DynamicController::OnProducerBufferReleased([[maybe_unused]] sptr<SurfaceBuffer> &buffer)
169 {
170     auto ret = interface_.Invoke<DynamicInterfaceName::ON_PRODUCER_BUFFER_RELEASED>(instance_);
171     return static_cast<GSError>(ret);
172 }
173 
NotifyEosImpl()174 int32_t DynamicController::NotifyEosImpl()
175 {
176     auto ret = interface_.Invoke<DynamicInterfaceName::NOTIFY_EOS>(instance_);
177     CHECK_AND_RETURN_RET_LOG(ret == AVCS_ERR_OK, AVCS_ERR_UNKNOWN, "Notify EOS for video processing failed.");
178     return AVCS_ERR_OK;
179 }
180 } // namespace PostProcessing
181 } // namespace MediaAVCodec
182 } // namespace OHOS