1 /*
2  * Copyright 2018, 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 
17 #ifndef C2_OMX_NODE_H_
18 #define C2_OMX_NODE_H_
19 
20 #include <atomic>
21 
22 #include <android/IOMXBufferSource.h>
23 #include <codec2/hidl/client.h>
24 #include <media/stagefright/foundation/Mutexed.h>
25 #include <media/IOMX.h>
26 #include <media/OMXBuffer.h>
27 
28 namespace android {
29 
30 /**
31  * IOmxNode implementation around codec 2.0 component, only to be used in
32  * IGraphicBufferSource::configure. Only subset of IOmxNode API is implemented
33  * and others are left as stub. As a result, one cannot expect this IOmxNode
34  * to work in any other usage than IGraphicBufferSource.
35  */
36 struct C2OMXNode : public BnOMXNode {
37     explicit C2OMXNode(const std::shared_ptr<Codec2Client::Component> &comp);
38     ~C2OMXNode() override = default;
39 
40     // IOMXNode
41     status_t freeNode() override;
42     status_t sendCommand(OMX_COMMANDTYPE cmd, OMX_S32 param) override;
43     status_t getParameter(
44             OMX_INDEXTYPE index, void *params, size_t size) override;
45     status_t setParameter(
46             OMX_INDEXTYPE index, const void *params, size_t size) override;
47     status_t getConfig(
48             OMX_INDEXTYPE index, void *params, size_t size) override;
49     status_t setConfig(
50             OMX_INDEXTYPE index, const void *params, size_t size) override;
51     status_t setPortMode(OMX_U32 port_index, IOMX::PortMode mode) override;
52     status_t prepareForAdaptivePlayback(
53             OMX_U32 portIndex, OMX_BOOL enable,
54             OMX_U32 maxFrameWidth, OMX_U32 maxFrameHeight) override;
55     status_t configureVideoTunnelMode(
56             OMX_U32 portIndex, OMX_BOOL tunneled,
57             OMX_U32 audioHwSync, native_handle_t **sidebandHandle) override;
58     status_t getGraphicBufferUsage(
59             OMX_U32 port_index, OMX_U32* usage) override;
60     status_t setInputSurface(
61             const sp<IOMXBufferSource> &bufferSource) override;
62     status_t allocateSecureBuffer(
63             OMX_U32 port_index, size_t size, buffer_id *buffer,
64             void **buffer_data, sp<NativeHandle> *native_handle) override;
65     status_t useBuffer(
66             OMX_U32 port_index, const OMXBuffer &omxBuf, buffer_id *buffer) override;
67     status_t freeBuffer(
68             OMX_U32 port_index, buffer_id buffer) override;
69     status_t fillBuffer(
70             buffer_id buffer, const OMXBuffer &omxBuf, int fenceFd) override;
71     status_t emptyBuffer(
72             buffer_id buffer, const OMXBuffer &omxBuf,
73             OMX_U32 flags, OMX_TICKS timestamp, int fenceFd) override;
74     status_t getExtensionIndex(
75             const char *parameter_name,
76             OMX_INDEXTYPE *index) override;
77     status_t dispatchMessage(const omx_message &msg) override;
78 
79     /**
80      * Returns underlying IOMXBufferSource object.
81      */
82     sp<IOMXBufferSource> getSource();
83 
84     /**
85      * Configure the frame size.
86      */
87     void setFrameSize(uint32_t width, uint32_t height);
88 
89     /**
90      * Clean up work item reference.
91      *
92      * \param index input work index
93      */
94     void onInputBufferDone(c2_cntr64_t index);
95 
96 private:
97     std::weak_ptr<Codec2Client::Component> mComp;
98     sp<IOMXBufferSource> mBufferSource;
99     std::shared_ptr<C2Allocator> mAllocator;
100     std::atomic_uint64_t mFrameIndex;
101     uint32_t mWidth;
102     uint32_t mHeight;
103     uint64_t mUsage;
104 
105     // WORKAROUND: timestamp adjustment
106 
107     // if >0: this is the max timestamp gap, if <0: this is -1 times the fixed timestamp gap
108     // if 0: no timestamp adjustment is made
109     // note that C2OMXNode can be recycled between encoding sessions.
110     int32_t mAdjustTimestampGapUs;
111     bool mFirstInputFrame; // true for first input
112     c2_cntr64_t mPrevInputTimestamp; // input timestamp for previous frame
113     c2_cntr64_t mPrevCodecTimestamp; // adjusted (codec) timestamp for previous frame
114 
115     Mutexed<std::map<uint64_t, buffer_id>> mBufferIdsInUse;
116 
117     class QueueThread;
118     sp<QueueThread> mQueueThread;
119 };
120 
121 }  // namespace android
122 
123 #endif  // C2_OMX_NODE_H_
124