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 "video_processing_callback_native.h"
17
18 #include "vpe_log.h"
19
20 using namespace OHOS::Media::VideoProcessingEngine;
21
BindOnError(OH_VideoProcessingCallback_OnError onError)22 VideoProcessing_ErrorCode VideoProcessingCallbackNative::BindOnError(OH_VideoProcessingCallback_OnError onError)
23 {
24 CHECK_AND_RETURN_RET_LOG(onError != nullptr, VIDEO_PROCESSING_ERROR_INVALID_PARAMETER, "onError is null!");
25 return BindFunction([this, &onError]() { onError_ = onError; });
26 }
27
BindOnState(OH_VideoProcessingCallback_OnState onState)28 VideoProcessing_ErrorCode VideoProcessingCallbackNative::BindOnState(OH_VideoProcessingCallback_OnState onState)
29 {
30 CHECK_AND_RETURN_RET_LOG(onState != nullptr, VIDEO_PROCESSING_ERROR_INVALID_PARAMETER, "onState is null!");
31 return BindFunction([this, &onState]() { onState_ = onState; });
32 }
33
BindOnNewOutputBuffer(OH_VideoProcessingCallback_OnNewOutputBuffer onNewOutputBuffer)34 VideoProcessing_ErrorCode VideoProcessingCallbackNative::BindOnNewOutputBuffer(
35 OH_VideoProcessingCallback_OnNewOutputBuffer onNewOutputBuffer)
36 {
37 CHECK_AND_RETURN_RET_LOG(onNewOutputBuffer != nullptr, VIDEO_PROCESSING_ERROR_INVALID_PARAMETER,
38 "onNewOutputBuffer is null!");
39 return BindFunction([this, &onNewOutputBuffer]() { onNewOutputBuffer_ = onNewOutputBuffer; });
40 }
41
IsValid() const42 bool VideoProcessingCallbackNative::IsValid() const
43 {
44 return isValid_.load();
45 }
46
IsModifiable() const47 bool VideoProcessingCallbackNative::IsModifiable() const
48 {
49 return isModifiable_.load();
50 }
51
LockModifiers()52 void VideoProcessingCallbackNative::LockModifiers()
53 {
54 std::lock_guard<std::mutex> lock(lock_);
55 isModifiable_ = false;
56 }
57
UnlockModifiers()58 void VideoProcessingCallbackNative::UnlockModifiers()
59 {
60 std::lock_guard<std::mutex> lock(lock_);
61 isModifiable_ = true;
62 }
63
HasOnNewOutputBuffer() const64 bool VideoProcessingCallbackNative::HasOnNewOutputBuffer() const
65 {
66 std::lock_guard<std::mutex> lock(lock_);
67 return onNewOutputBuffer_ != nullptr;
68 }
69
OnError(OH_VideoProcessing * instance,VideoProcessing_ErrorCode errorCode,void * userData)70 void VideoProcessingCallbackNative::OnError(OH_VideoProcessing* instance, VideoProcessing_ErrorCode errorCode,
71 void* userData)
72 {
73 if (onError_ == nullptr) {
74 VPE_LOGD("onError_ is null!");
75 return;
76 }
77 onError_(instance, errorCode, userData);
78 }
79
OnState(OH_VideoProcessing * instance,VideoProcessing_State state,void * userData)80 void VideoProcessingCallbackNative::OnState(OH_VideoProcessing* instance, VideoProcessing_State state, void* userData)
81 {
82 if (onState_ == nullptr) {
83 VPE_LOGD("onState_ is null!");
84 return;
85 }
86 onState_(instance, state, userData);
87 }
88
OnNewOutputBuffer(OH_VideoProcessing * instance,uint32_t index,void * userData)89 void VideoProcessingCallbackNative::OnNewOutputBuffer(OH_VideoProcessing* instance, uint32_t index, void* userData)
90 {
91 if (onNewOutputBuffer_ == nullptr) {
92 VPE_LOGD("onNewOutputBuffer_ is null!");
93 return;
94 }
95 onNewOutputBuffer_(instance, index, userData);
96 }
97
BindFunction(std::function<void ()> && functionBinder)98 VideoProcessing_ErrorCode VideoProcessingCallbackNative::BindFunction(std::function<void()>&& functionBinder)
99 {
100 if (!isModifiable_.load()) {
101 return VIDEO_PROCESSING_ERROR_PROCESS_FAILED;
102 }
103 std::lock_guard<std::mutex> lock(lock_);
104 functionBinder();
105 if (!isValid_.load()) {
106 isValid_ = true;
107 }
108 return VIDEO_PROCESSING_SUCCESS;
109 }
110