• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     /**
97      * Returns dataspace information from GraphicBufferSource.
98      */
99     android_dataspace getDataspace();
100 
101     /**
102      * Sets priority of the queue thread.
103      */
104     void setPriority(int priority);
105 
106 private:
107     std::weak_ptr<Codec2Client::Component> mComp;
108     sp<IOMXBufferSource> mBufferSource;
109     std::shared_ptr<C2Allocator> mAllocator;
110     std::atomic_uint64_t mFrameIndex;
111     uint32_t mWidth;
112     uint32_t mHeight;
113     uint64_t mUsage;
114     Mutexed<android_dataspace> mDataspace;
115 
116     // WORKAROUND: timestamp adjustment
117 
118     // if >0: this is the max timestamp gap, if <0: this is -1 times the fixed timestamp gap
119     // if 0: no timestamp adjustment is made
120     // note that C2OMXNode can be recycled between encoding sessions.
121     int32_t mAdjustTimestampGapUs;
122     bool mFirstInputFrame; // true for first input
123     c2_cntr64_t mPrevInputTimestamp; // input timestamp for previous frame
124     c2_cntr64_t mPrevCodecTimestamp; // adjusted (codec) timestamp for previous frame
125 
126     Mutexed<std::map<uint64_t, buffer_id>> mBufferIdsInUse;
127 
128     class QueueThread;
129     sp<QueueThread> mQueueThread;
130 };
131 
132 }  // namespace android
133 
134 #endif  // C2_OMX_NODE_H_
135