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 * author: <rimon.xu@rock-chips.com> and <martin.cheng@rock-chips.com> 17 * date: 2020-04-03 18 * module: RTTaskGraph 19 */ 20 21 #ifndef SRC_RT_TASK_APP_GRAPH_RTTASKGRAPH_H_ 22 #define SRC_RT_TASK_APP_GRAPH_RTTASKGRAPH_H_ 23 24 #include <vector> 25 #include <memory> 26 #include <map> 27 #include <functional> 28 #include <stdarg.h> 29 30 #include "rt_header.h" 31 #include "rt_metadata.h" 32 #include "RTGraphCommon.h" 33 34 typedef enum ExecutorMode { 35 EXECUTOR_THREAD_POOL, 36 EXECUTOR_THREAD_LOCAL, 37 } RTExecutorMode; 38 39 typedef enum GraphCmd { 40 GRAPH_CMD_PREPARE = 100, 41 GRAPH_CMD_START, 42 GRAPH_CMD_STOP, 43 GRAPH_CMD_RESUME, 44 GRAPH_CMD_PAUSE, 45 GRAPH_CMD_FLUSH, 46 // for graph private command 47 GRAPH_CMD_PRIVATE_CMD, 48 // for task node command 49 GRAPH_CMD_TASK_NODE_PRIVATE_CMD, 50 GRAPH_CMD_MAX, 51 } RTGraphCmd; 52 53 class RTInputStreamManager; 54 class RTOutputStreamManager; 55 class RTTaskNode; 56 class RTGraphInputStream; 57 class RTGraphOutputStream; 58 class RTExecutor; 59 class RTScheduler; 60 class RTMediaBuffer; 61 class RTGraphParser; 62 class RTTaskGraphConfig; 63 class RTTaskGraphStat; 64 class RTTaskGraph { 65 public: 66 explicit RTTaskGraph(const char* tagName); 67 explicit RTTaskGraph(UINT64 uid, const char* tagName); 68 virtual ~RTTaskGraph(); 69 70 public: 71 // external common api 72 UINT64 getUid(); 73 std::string getName(); 74 RT_RET autoBuild(const char* config, RT_BOOL isFileType = RT_TRUE); 75 RT_RET prepare(RtMetaData *params = RT_NULL); 76 RT_RET start(); 77 RT_RET stop(RT_BOOL block = RT_TRUE); 78 RT_RET resume(); 79 RT_RET pause(RT_BOOL block = RT_FALSE); 80 RT_RET flush(); 81 RT_RET reset(); 82 RT_RET release(); 83 RT_RET invoke(INT32 cmd, RtMetaData *params); 84 RT_RET waitForObservedOutput(INT64 timeoutUs = -1); 85 RT_RET waitUntilDone(INT64 timeoutUs = -1); 86 RT_RET waitUntilEos(INT64 timeoutUs = -1); 87 RT_RET queryStat(RTTaskGraphStat *stat); 88 RT_RET dump(); 89 90 RTCBHandle observeOutputStream( 91 const std::string streamName, 92 std::function<RT_RET(RTMediaBuffer *)> streamCallback); 93 RT_RET cancelObserveOutputStream(RTCBHandle handle); 94 95 public: 96 // deprecated api 97 RT_RET observeOutputStream(const std::string& streamName, 98 INT32 streamId, 99 std::function<RT_RET(RTMediaBuffer *)> streamCallback); 100 RT_RET cancelObserveOutputStream(INT32 streamId); 101 RT_RET addSubGraph(const char *graphConfigFile); 102 RT_RET removeSubGraph(const char *graphConfigFile); 103 RT_BOOL hasMirror(INT32 nodeId, RT_BOOL nodeOnly = RT_FALSE); 104 RT_RET setExternalExecutor(RTExecutor *executor); 105 106 public: 107 // external link graph api 108 RTLinkHandle addDownGraph(RTTaskGraph *graph, std::string srcName, std::string dstName); 109 RT_RET removeDownGraph(RTLinkHandle handle); 110 RT_RET setGraphOutputStreamAddMode(RTGraphIOStreamMode mode); 111 112 public: 113 // external link node api 114 RTTaskNode* createNode(std::string nodeConfig, std::string streamConfig); 115 RT_RET linkNode(RTTaskNode *srcNode, RTTaskNode *dstNode); 116 template <class T, class... Args> linkNode(T src,T dst,Args...rest)117 RT_RET linkNode(T src, T dst, Args... rest) { 118 return linkMultiNode(src, dst, rest...); 119 } 120 RT_RET unlinkNode(RTTaskNode *srcNode, RTTaskNode *dstNode); 121 122 RT_BOOL hasLinkMode(std::string mode); 123 RT_RET selectLinkMode(std::string mode); 124 template <class T, class... Args> selectLinkMode(T arg1,T arg2,Args...rest)125 RT_RET selectLinkMode(T arg1, T arg2, Args... rest) { 126 return selectMultiMode(arg1, arg2, rest...); 127 } 128 RT_RET clearLinkShips(); 129 130 public: 131 // internal api 132 void sendInterrupt(std::string reason); 133 RT_BOOL unthrottleSources(); 134 135 protected: 136 RT_RET addPacketToInputStream(std::string streamName, INT64 timeoutUs, RTMediaBuffer *packet); 137 RTCBHandle addOutputStreamPoller(const std::string streamName); 138 RT_RET removeOutputStreamPoller(RTCBHandle handle); 139 RT_RET checkInputStreamFull(std::string streamName); onReceiveOutputStreams(RTGraphOutputStream * graphOutputStream)140 virtual RT_RET onReceiveOutputStreams(RTGraphOutputStream *graphOutputStream) { return RT_OK; } 141 142 private: 143 template <class T, class... Args> linkMultiNode(T src,T dst,Args...rest)144 RT_RET linkMultiNode(T src, T dst, Args... rest) { 145 RT_RET ret = linkNode(src, dst); 146 if (ret != RT_OK) { 147 RT_LOGE("src node %d dst node %d link failed", src->getID(), dst->getID()); 148 } else { 149 linkMultiNode(dst, rest...); 150 } 151 return ret; 152 } 153 154 template <class T> linkMultiNode(T end)155 RT_RET linkMultiNode(T end) { 156 return RT_OK; 157 } 158 159 template <class T, class... Args> selectMultiMode(T arg1,T arg2,Args...rest)160 RT_RET selectMultiMode(T arg1, T arg2, Args... rest) { 161 RT_RET ret = selectLinkMode(arg1); 162 if (ret != RT_OK) { 163 RT_LOGE("select mode %s failed", arg1); 164 } else { 165 selectMultiMode(arg2, rest...); 166 } 167 return ret; 168 } 169 170 template <class T> selectMultiMode(T end)171 RT_RET selectMultiMode(T end) { 172 RT_RET ret = selectLinkMode(end); 173 if (ret != RT_OK) { 174 RT_LOGE("select mode %s failed", end); 175 } 176 return ret; 177 } 178 179 private: 180 RT_RET autoLinkSource(); 181 RT_RET autoUnlinkSource(INT32 nodeId); 182 RT_RET prepareForRun(RtMetaData *options); 183 RT_RET prepareNodeForRun(RTTaskNode *node, RtMetaData *options); 184 RT_RET cleanupAfterRun(); 185 void updateThrottledNodes(RTInputStreamManager* stream, bool *streamWasFull); 186 void updateThrottledNodesNoLock(RTInputStreamManager* stream, bool *streamWasFull); 187 RT_RET buildTaskNode(INT32 pipeId, INT32 nodeId, RTGraphParser* nodeParser); 188 RT_RET buildExecutors(RTGraphParser *parser); 189 RT_RET buildNodes(RTGraphParser *parser); 190 RT_RET buildLinkModes(RTGraphParser *parser); 191 RT_RET buildSubGraph(RTGraphParser *graphParser); 192 RT_RET notifyOutputStreams(RTGraphOutputStream *graphOutputStream); 193 RTTaskNode *createNodeByText(const char *graphConfig); 194 std::vector<std::vector<INT32>> parseLinkShip(std::string linkShip); 195 RT_RET linkNode(INT32 srcNodeId, INT32 dstNodeId); 196 RT_RET unlinkNode(INT32 srcNodeId, INT32 dstNodeId); 197 198 RT_RET setupGraphInputStream(std::string streamName); 199 200 protected: 201 UINT64 mGraphUid; 202 std::string mTagName; 203 RT_BOOL mHasError; 204 RtMutex mFullInputStreamsMutex; 205 RtMutex mErrorMutex; 206 RTScheduler *mScheduler; 207 RtMutex *mLock; 208 RTTaskGraphConfig *mGraphConfig; 209 RTGraphIOStreamMode mIoStreamMode; 210 INT32 mThreadNum; 211 std::map<INT32, RTExecutor *> mExecutors; 212 RTExecutor *mExtExecutor; 213 std::map<INT32/* node id */, RTTaskNode *> mNodes; 214 std::map<INT32, std::vector<RTInputStreamManager *>> mFullInputStreams; 215 std::map<INT32/* node id */, RTInputStreamManager *> mInputManagers; 216 std::map<INT32/* node id */, RTOutputStreamManager *> mOutputManagers; 217 // The graph output streams. 218 std::map<INT32, RTGraphOutputStream *> mGraphOutputStreams; 219 std::map<INT32, RTGraphInputStream *> mGraphInputStreams; 220 // Maps graph input streams to their virtual node ids. 221 std::map<std::string, INT32> mGraphInputStreamIds; 222 std::map<std::string, std::string> mLinkShips; 223 std::vector<std::string> mLinkModes; 224 // Compatible with old interfaces 225 std::map<INT32/* old stream id */, std::string> mOutputStreamCompMaps; 226 std::map<INT32/* old stream id */, RTCBHandle> mGraphOutCompMaps; 227 }; 228 229 #endif // SRC_RT_TASK_APP_GRAPH_RTTASKGRAPH_H_ 230