1 /*
2  * Copyright 2024, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 //#define LOG_NDEBUG 0
17 #define LOG_TAG "C2AidlNode"
18 #include <log/log.h>
19 #include <private/android/AHardwareBufferHelpers.h>
20 
21 #include <media/stagefright/MediaErrors.h>
22 #include <media/stagefright/aidlpersistentsurface/wrapper/Conversion.h>
23 
24 #include "C2NodeImpl.h"
25 #include "C2AidlNode.h"
26 
27 namespace android {
28 
29 using ::aidl::android::media::IAidlBufferSource;
30 using ::aidl::android::media::IAidlNode;
31 
32 // Conversion
33 using ::android::media::aidl_conversion::toAidlStatus;
34 
C2AidlNode(const std::shared_ptr<Codec2Client::Component> & comp)35 C2AidlNode::C2AidlNode(const std::shared_ptr<Codec2Client::Component> &comp)
36     : mImpl(new C2NodeImpl(comp, true)) {}
37 
38 // aidl ndk interfaces
freeNode()39 ::ndk::ScopedAStatus C2AidlNode::freeNode() {
40     return toAidlStatus(mImpl->freeNode());
41 }
42 
getConsumerUsage(int64_t * _aidl_return)43 ::ndk::ScopedAStatus C2AidlNode::getConsumerUsage(int64_t* _aidl_return) {
44     uint64_t usage;
45     mImpl->getConsumerUsageBits(&usage);
46     *_aidl_return = usage;
47     return ::ndk::ScopedAStatus::ok();
48 }
49 
getInputBufferParams(IAidlNode::InputBufferParams * _aidl_return)50 ::ndk::ScopedAStatus C2AidlNode::getInputBufferParams(IAidlNode::InputBufferParams* _aidl_return) {
51     mImpl->getInputBufferParams(_aidl_return);
52     return ::ndk::ScopedAStatus::ok();
53 }
54 
setConsumerUsage(int64_t usage)55 ::ndk::ScopedAStatus C2AidlNode::setConsumerUsage(int64_t usage) {
56     mImpl->setConsumerUsageBits(static_cast<uint64_t>(usage));
57     return ::ndk::ScopedAStatus::ok();
58 }
59 
setAdjustTimestampGapUs(int32_t gapUs)60 ::ndk::ScopedAStatus C2AidlNode::setAdjustTimestampGapUs(int32_t gapUs) {
61     mImpl->setAdjustTimestampGapUs(gapUs);
62     return ::ndk::ScopedAStatus::ok();
63 }
64 
setInputSurface(const std::shared_ptr<IAidlBufferSource> & bufferSource)65 ::ndk::ScopedAStatus C2AidlNode::setInputSurface(
66         const std::shared_ptr<IAidlBufferSource>& bufferSource) {
67     return toAidlStatus(mImpl->setAidlInputSurface(bufferSource));
68 }
69 
submitBuffer(int32_t buffer,const std::optional<::aidl::android::hardware::HardwareBuffer> & hBuffer,int32_t flags,int64_t timestamp,const::ndk::ScopedFileDescriptor & fence)70 ::ndk::ScopedAStatus C2AidlNode::submitBuffer(
71         int32_t buffer,
72         const std::optional<::aidl::android::hardware::HardwareBuffer>& hBuffer,
73         int32_t flags, int64_t timestamp, const ::ndk::ScopedFileDescriptor& fence) {
74     sp<GraphicBuffer> gBuf;
75     AHardwareBuffer *ahwb = nullptr;
76     if (hBuffer.has_value()) {
77         ahwb = hBuffer.value().get();
78     }
79 
80     if (ahwb) {
81         gBuf = AHardwareBuffer_to_GraphicBuffer(ahwb);
82     }
83     return toAidlStatus(mImpl->submitBuffer(
84             buffer, gBuf, flags, timestamp, ::dup(fence.get())));
85 }
86 
onDataSpaceChanged(int32_t dataSpace,int32_t aspects,int32_t pixelFormat)87 ::ndk::ScopedAStatus C2AidlNode::onDataSpaceChanged(
88         int32_t dataSpace,
89         int32_t aspects,
90         int32_t pixelFormat) {
91     // NOTE: legacy codes passed aspects, but they didn't used.
92     (void)aspects;
93 
94     return toAidlStatus(mImpl->onDataspaceChanged(
95             static_cast<uint32_t>(dataSpace),
96             static_cast<uint32_t>(pixelFormat)));
97 }
98 
99 // cpp interface
100 
getSource()101 std::shared_ptr<IAidlBufferSource> C2AidlNode::getSource() {
102     return mImpl->getAidlSource();
103 }
104 
setFrameSize(uint32_t width,uint32_t height)105 void C2AidlNode::setFrameSize(uint32_t width, uint32_t height) {
106     return mImpl->setFrameSize(width, height);
107 }
108 
onInputBufferDone(c2_cntr64_t index)109 void C2AidlNode::onInputBufferDone(c2_cntr64_t index) {
110     return mImpl->onInputBufferDone(index);
111 }
112 
onInputBufferEmptied()113 void C2AidlNode::onInputBufferEmptied() {
114     return mImpl->onInputBufferEmptied();
115 }
116 
getDataspace()117 android_dataspace C2AidlNode::getDataspace() {
118     return mImpl->getDataspace();
119 }
120 
getPixelFormat()121 uint32_t C2AidlNode::getPixelFormat() {
122     return mImpl->getPixelFormat();
123 }
124 
setPriority(int priority)125 void C2AidlNode::setPriority(int priority) {
126     return mImpl->setPriority(priority);
127 }
128 
129 }  // namespace android
130