xref: /OK3568_Linux_fs/external/rockit/tgi/sdk/include/RTTaskNodeBase.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright 2020 Rockchip Electronics Co. LTD
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 SRC_RT_TASK_TASK_GRAPH_RTTASKNODEBASE_H_
18 #define SRC_RT_TASK_TASK_GRAPH_RTTASKNODEBASE_H_
19 
20 #include "string.h"
21 #include <map>
22 #include <functional>
23 
24 #include "rt_header.h"
25 
26 #include "RTTaskNodeContext.h"
27 #include "RTTaskNodeContextManager.h"
28 #include "RTTaskNodeOptions.h"
29 #include "RTTaskNodeFactory.h"
30 #include "RTNodeCommon.h"
31 #include "RTStreamInfo.h"
32 
33 typedef struct _RTTaskNodeInfo {
34     INT32 nodeId;
35     std::string nodeName;
36 } RTTaskNodeInfo;
37 
38 class RTBufferListener;
39 class RTInputStreamHandler;
40 class RTInputStreamManager;
41 class RTOutputStreamHandler;
42 class RTOutputStreamManager;
43 class RTSchedulerQueue;
44 class RTMediaBufferPool;
45 class RTTaskNodeStat;
46 class RTTaskNodeBase {
47  public:
48     RTTaskNodeBase();
49     virtual ~RTTaskNodeBase();
50 
51     RT_RET              prepareForRun(RtMetaData* options,
52                                       std::function<void()> sourceNodeOpenedCallback,
53                                       std::function<void(RTTaskNodeContext *)> scheduleCallback);
54 
55     RT_RET              cleanupAfterRun();
getID()56     INT32               getID() { return mNodeId; }
getOptions()57     RtMetaData*         getOptions() { return mOptions; }
58     RT_BOOL             isSource() const;
59     RT_BOOL             isSink() const;
60     RT_BOOL             hasMirror(RT_BOOL nodeOnly = RT_FALSE) const;
61     INT32               numInputStreams() const;
62     INT32               numOutputStreams() const;
63 
64     RT_RET              processNode(RTTaskNodeContext *packet);
65     void                nodeOpened();
66     bool                closed();
67     RT_RET              flush();
68     RT_RET              reset();
69     RT_BOOL             isDone();
70     RT_RET              closeNode();
71     RT_RET              closeInputStreams();
72     RT_RET              closeOutputStreams();
73 
74     void                setMaxInputStreamQueueSize(INT32 maxQueueSize);
75     void                setMaxBatchPrcoessSize(INT32 maxBatchSize);
76     void                setQueueSizeCallbacks(
77                          std::function<void(RTInputStreamManager*, bool*)> becomesFullCallback,
78                          std::function<void(RTInputStreamManager*, bool*)> becomesNotFullCallback,
79                          std::function<void(RTInputStreamManager*, bool*)> becomesEmptyCallback);
80 
initialize(INT32 node_id,RTTaskNodeInfo * nodeInfo)81     RT_RET              initialize(INT32 node_id, RTTaskNodeInfo *nodeInfo) { return RT_OK; }
82     RT_RET              initialize(
83                          INT32 nodeId,
84                          RTTaskNodeInfo        *nodeInfo,
85                          RTInputStreamManager  *inputManager,
86                          RTOutputStreamManager *outputManager);
87     RT_RET              initialize(INT32 nodeId,
88                          RTTaskNodeInfo *nodeInfo,
89                          const std::vector<RTInputStreamManager *> &inputManagers,
90                          const std::vector<RTOutputStreamManager *> &outputManagers);
91 
92     RT_RET              addStreamSource(
93                          INT32 streamId, RTOutputStreamManager *lastOutputManager);
94     RT_RET              addStreamSource(RTOutputStreamManager *lastOutputManager);
95     RT_RET              removeStreamSource(RTOutputStreamManager *outputManager);
getStreamSource()96     const std::vector<INT32> &getStreamSource() { return mSourceNodes; }
97 
98     void                checkIfBecameReady();
99 
100     RT_BOOL             tryToBeginScheduling();
101     void                schedulingLoop();
102     void                endScheduling();
103 
104     // Returns a pointer to the default calculator context that is used for
105     // sequential execution. A source node should always reuse its default
106     // calculator context.
getDefaultNodeContext()107     RTTaskNodeContext*  getDefaultNodeContext() const {
108         return mNodeContextManager.getDefaultNodeContext();
109     }
110 
getSchedulerQueue()111     RTSchedulerQueue*   getSchedulerQueue() const {
112         return mSchedulerQueue;
113     }
114 
115     // Sets the scheduler queue the node is assigned to.
setSchedulerQueue(RTSchedulerQueue * queue)116     void                setSchedulerQueue(RTSchedulerQueue* queue) {
117         mSchedulerQueue = queue;
118     }
119 
120     RT_RET              sendInterrupt(std::string reason);
121     RT_RET              cancelInterrupt(std::string reason);
122     RT_RET              dump();
123 
124     RT_RET              attachOutStreamPool(RTMediaBufferPool *pool, std::string streamType = RT_NONE);
125     RT_RET              detachOutStreamPool(std::string streamType = RT_NONE);
126 
127     void                setComponentUid(INT32 componentUid);
128     INT32               getComponentUid();
129 
130  public:
131     virtual RT_RET      open(RTTaskNodeContext *context) = 0;
132     virtual RT_RET      process(RTTaskNodeContext *context) = 0;
133     virtual RT_RET      close(RTTaskNodeContext *context) = 0;
134     virtual RT_RET      invoke(RtMetaData *meta);
135     virtual RT_RET      queryStat(RTTaskNodeStat *stat);
queryStatInternal(RTTaskNodeStat * nodeStat)136     virtual RT_RET      queryStatInternal(RTTaskNodeStat *nodeStat) { return RT_ERR_UNSUPPORT; }
137 
138  protected:
initSupportOptions()139     virtual RT_RET      initSupportOptions() { return RT_OK; }
invokeInternal(RtMetaData * meta)140     virtual RT_RET      invokeInternal(RtMetaData *meta) { return RT_ERR_UNSUPPORT; }
141 
142  private:
143     RT_RET              addInputStreams(RTInputStreamManager *inputManager);
144     RT_RET              addOutputStreams(RTOutputStreamManager *outputManager);
145     RT_RET              prepareOptionsForRun();
146     RT_RET              processPassThrough(RTTaskNodeContext *nodeContext);
147 
148  protected:
149     INT32 mNodeId;
150     RTInputStreamHandler  *mInputHandler;
151     RTOutputStreamHandler *mOutputHandler;
152 
153     RtMutex mMutex;
154 
155     INT32 mCurrentInScheduling = 0;
156     INT32 mMaxInScheduling = 1;
157     // SchedulingState incidates the current state of the node scheduling process.
158     // There are four possible transitions:
159     // (a) From kIdle to kScheduling.
160     // Any thread that makes this transition becomes the scheduling thread and
161     // will be responsible for preparing and scheduling all possible invocations.
162     // (b) From kScheduling to kSchedulingPending.
163     // Any thread, except the scheduling thread, can make this transition.
164     // kSchedulingPending indicates that some recent changes require the
165     // scheduling thread to recheck the node readiness after current scheduling
166     // iteration.
167     // (c) From kSchedulingPending to kScheduling.
168     // Made by the scheduling thread to indicate that it has already caught up
169     // with all the recent changes that can affect node readiness.
170     // (d) From kScheduling to kIdle. Made by the scheduling thread when there is
171     // no more scheduling work to be done.
172     enum RTSchedulingState {
173         kIdle = 0,        //
174         kScheduling = 1,  //
175         kSchedulingPending = 2
176     };
177     RTSchedulingState mSchedulingState = kIdle;
178     RTTaskNodeContextManager mNodeContextManager;
179     RtMetaData              *mOptions;
180     std::function<void()> mSourceNodeOpenedCallback;
181     // The status of the current Calculator that this CalculatorNode
182     // is wrapping.  kStateActive is currently used only for source nodes.
183     enum RTNodeStatus {
184         kStateUninitialized = 0,
185         kStatePrepared = 1,
186         kStateOpened = 2,
187         kStateActive = 3,
188         kStateClosed = 4
189     };
190     RTNodeStatus mStatus = kStateUninitialized;
191     RTSchedulerQueue *mSchedulerQueue = NULL;
192     std::map<std::string/* name */, RTTaskNodeOption> mSupportOptions;
193     std::vector<INT32> mSourceNodes;
194     std::vector<RTStreamInfo *> mInputStreamInfos;
195     std::vector<RTStreamInfo *> mOutputStreamInfos;
196     RT_BOOL                     mIsPassThrough;
197     RtReference                *mProcRef;
198     INT32                       mComponentUid;
199 };
200 
201 #endif  // SRC_RT_TASK_TASK_GRAPH_RTTASKNODEBASE_H_
202