1 /** 2 * Copyright (C) 2022 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 BASE_NODE_H 18 #define BASE_NODE_H 19 20 #include <stdint.h> 21 #include <ImsMediaDataQueue.h> 22 #include <BaseSessionCallback.h> 23 #include <StreamSchedulerCallback.h> 24 25 #define MAX_AUDIO_PAYLOAD_SIZE (1500) 26 #define MAX_FRAME_IN_PACKET ((MAX_AUDIO_PAYLOAD_SIZE - 1) / 32) 27 28 enum kBaseNodeState 29 { 30 /* the state after stop method called normally*/ 31 kNodeStateStopped, 32 /* the state after start without error*/ 33 kNodeStateRunning, 34 }; 35 36 enum kBaseNodeId 37 { 38 kNodeIdUnknown, 39 // for socket 40 kNodeIdSocketWriter, 41 kNodeIdSocketReader, 42 // for rtp 43 kNodeIdRtpEncoder, 44 kNodeIdRtpDecoder, 45 // for rtcp 46 kNodeIdRtcpEncoder, 47 kNodeIdRtcpDecoder, 48 // for Audio 49 kNodeIdAudioSource, 50 kNodeIdAudioPlayer, 51 kNodeIdDtmfEncoder, 52 kNodeIdAudioPayloadEncoder, 53 kNodeIdAudioPayloadDecoder, 54 // for Video 55 kNodeIdVideoSource, 56 kNodeIdVideoRenderer, 57 kNodeIdVideoPayloadEncoder, 58 kNodeIdVideoPayloadDecoder, 59 // for Text 60 kNodeIdTextSource, 61 kNodeIdTextRenderer, 62 kNodeIdTextPayloadEncoder, 63 kNodeIdTextPayloadDecoder, 64 }; 65 66 /** 67 * @brief BaseNode object 68 * 69 */ 70 class BaseNode 71 { 72 public: 73 BaseNode(BaseSessionCallback* callback = nullptr); 74 virtual ~BaseNode(); 75 /** 76 * @brief Sets the BaseSession callback listener 77 * 78 * @param callback the callback instance 79 */ 80 void SetSessionCallback(BaseSessionCallback* callback); 81 82 /** 83 * @brief Sets the session scheduler callback listener 84 * 85 * @param callback the instance of callback listener 86 */ 87 void SetSchedulerCallback(std::shared_ptr<StreamSchedulerCallback>& callback); 88 89 /** 90 * @brief Connects a node to rear to this node. It makes to pass the processed data to next node 91 * 92 * @param pRearNode The instance of node to connect to next node 93 */ 94 void ConnectRearNode(BaseNode* pRearNode); 95 96 /** 97 * @brief Disconnect nodes connected to rear and front 98 */ 99 void DisconnectNodes(); 100 101 /** 102 * @brief Empty the data queue 103 * 104 */ 105 void ClearDataQueue(); 106 107 /** 108 * @brief Gets the node id to identify the IAudioSourceNoce 109 * 110 * @return BaseNodeID The node id 111 */ 112 virtual kBaseNodeId GetNodeId(); 113 114 /** 115 * @brief Prepare the node before start it 116 * 117 * @return bool Returns true when it starts well without error 118 */ 119 virtual bool Prepare(); 120 121 /** 122 * @brief Starts to run the node with the configuration already set by the SetConfig method 123 * 124 * @return ImsMediaResult return RESULT_SUCCESS when it starts well without error 125 */ 126 virtual ImsMediaResult Start(); 127 128 /** 129 * @brief Starts to run node with the configuration already set by the SetConfig method in 130 * scheduler thread 131 * 132 * @return ImsMediaResult return RESULT_SUCCESS when it starts well without error 133 */ 134 virtual ImsMediaResult ProcessStart(); 135 136 /** 137 * @brief Stops the node operation 138 * 139 */ 140 virtual void Stop() = 0; 141 142 /** 143 * @brief Checks the node processes data in main thread. 144 */ 145 virtual bool IsRunTime() = 0; 146 147 /** 148 * @brief Checks the node to start in main thread 149 */ 150 virtual bool IsRunTimeStart(); 151 152 /** 153 * @brief Checks the node is initial node of data source 154 * 155 * @return true It is a source node 156 * @return false It is not a source node 157 */ 158 virtual bool IsSourceNode() = 0; 159 160 /** 161 * @brief Sets the config to delivers the parameter to use in the node 162 * 163 * @param config Sets the Audio/Video/TextConfig. 164 */ 165 virtual void SetConfig(void* config); 166 167 /** 168 * @brief Compares the config with the member valuables in the node 169 * 170 * @param config Audio/Video/TextConfig to compare 171 * @return true The member valuables in the config is same with the member valuables in the node 172 * @return false There is at least one member valuables not same with config 173 */ 174 virtual bool IsSameConfig(void* config); 175 176 /** 177 * @brief Updates the node member valuable and re start the running operation with the given 178 * config. 179 * 180 * @param config The Audio/Video/TextConfig to update 181 * @return ImsMediaResult Returns RETURN_SUCCESS when the update succeed 182 */ 183 virtual ImsMediaResult UpdateConfig(void* config); 184 185 /** 186 * @brief This method is invoked by the thread created in StreamScheduler 187 * 188 */ 189 virtual void ProcessData(); 190 191 /** 192 * @brief Gets the node name with char types 193 * 194 * @return const char* The node name 195 */ 196 virtual const char* GetNodeName(); 197 198 /** 199 * @brief Sets the media type 200 * 201 * @param eType the types can be Audio/Video/Text. Check the type definition. 202 */ 203 virtual void SetMediaType(ImsMediaType eType); 204 205 /** 206 * @brief Gets the media type 207 * 208 * @return ImsMediaType the types of the node 209 */ 210 virtual ImsMediaType GetMediaType(); 211 212 /** 213 * @brief Gets the state of the node 214 * 215 * @return kBaseNodeState The returning node states is running or stopped. 216 */ 217 virtual kBaseNodeState GetState(); 218 219 virtual void SetState(kBaseNodeState state); 220 /** 221 * @brief Gets the number of data stored in this node 222 * 223 * @return uint32_t The data count 224 */ 225 virtual uint32_t GetDataCount(); 226 227 /** 228 * @brief Gets one data stored in front of data queue in the node 229 * 230 * @param subtype The subtype of data stored in the queue. It can be various subtype according 231 * to the characteristics of the given data 232 * @param data The data buffer 233 * @param dataSize The size of data 234 * @param timestamp The timestamp of data, it can be milliseconds unit or rtp timestamp unit 235 * @param mark It is true when the data has marker bit set 236 * @param seq The sequence number of data. it is 0 when there is no valid sequence number set 237 * @param dataType The additional data type for the video frames 238 * @param arrivalTime The arrival time of the packet 239 * @return true Succeeds to gets the valid data 240 * @return false Fails to gets the valid data 241 */ 242 virtual bool GetData(ImsMediaSubType* subtype, uint8_t** data, uint32_t* dataSize, 243 uint32_t* timestamp, bool* mark, uint32_t* seq, ImsMediaSubType* dataType = nullptr, 244 uint32_t* arrivalTime = nullptr); 245 246 /** 247 * @brief This method is to add data frame to the queue in the node 248 * 249 * @param data The data buffer 250 * @param size The size of data 251 * @param timestamp The timestamp of data, it can be milliseconds unit or rtp timestamp unit 252 * @param mark It is true when the data has marker bit set 253 * @param seq The sequence number of data. it is 0 when there is no valid sequence number set 254 * @param subtype The subtype of data stored in the queue. It can be various subtype according 255 * to the characteristics of the given data 256 * @param dataType The additional data type for the video frames 257 * @param arrivalTime The arrival time of the packet 258 * @param index The index of the queue to add, if it is not set, add the frame to the end of 259 * the queue 260 */ 261 virtual void AddData(uint8_t* data, uint32_t size, uint32_t timestamp, bool mark, uint32_t seq, 262 ImsMediaSubType subtype = ImsMediaSubType::MEDIASUBTYPE_UNDEFINED, 263 ImsMediaSubType dataType = ImsMediaSubType::MEDIASUBTYPE_UNDEFINED, 264 uint32_t arrivalTime = 0, int32_t index = -1); 265 266 /** 267 * @brief Deletes the data stored in the front of the data queue 268 * 269 */ 270 virtual void DeleteData(); 271 272 /** 273 * @brief Sends processed data to next node 274 * 275 * @param subtype The subtype of data stored in the queue. It can be various subtype according 276 * to the characteristics of the given data 277 * @param data The data buffer 278 * @param dataSize The size of data 279 * @param timestamp The timestamp of data, it can be milliseconds unit or rtp timestamp unit 280 * @param mark It is true when the data has marker bit set 281 * @param seq The sequence number of data. it is 0 when there is no valid sequence number set 282 * @param dataType The additional data type for the video frames 283 * @param arrivalTime The arrival time of the packet in milliseconds unit 284 */ 285 virtual void SendDataToRearNode(ImsMediaSubType subtype, uint8_t* data, uint32_t dataSize, 286 uint32_t timestamp, bool mark, uint32_t seq, 287 ImsMediaSubType nDataType = ImsMediaSubType::MEDIASUBTYPE_UNDEFINED, 288 uint32_t arrivalTime = 0); 289 290 /** 291 * @brief This method is invoked when the front node calls SendDataToRearNode to pass the 292 * processed data to next connected rear node 293 * 294 * @param subtype The subtype of data stored in the queue. It can be various subtype according 295 * to the characteristics of the given data 296 * @param data The data buffer 297 * @param dataSize The size of data 298 * @param timestamp The timestamp of data, it can be milliseconds unit or rtp timestamp unit 299 * @param mark It is true when the data has marker bit set 300 * @param seq The sequence number of data. it is 0 when there is no valid sequence number set 301 * @param dataType The additional data type for the video frames 302 * @param arrivalTime The arrival time of the packet 303 */ 304 virtual void OnDataFromFrontNode(ImsMediaSubType subtype, uint8_t* pData, uint32_t nDataSize, 305 uint32_t timestamp, bool mark, uint32_t nSeqNum, 306 ImsMediaSubType nDataType = ImsMediaSubType::MEDIASUBTYPE_UNDEFINED, 307 uint32_t arrivalTime = 0); 308 309 protected: 310 /** 311 * @brief Disconnects the front node from this node. 312 * 313 * @param pFrontNode The instance of node to disconnect 314 */ 315 void DisconnectFrontNode(BaseNode* pFrontNode); 316 317 /** 318 * @brief Disconnects the rear node from this node. 319 * 320 * @param pRearNode The instance of node to disconnect. 321 */ 322 void DisconnectRearNode(BaseNode* pRearNode); 323 324 std::shared_ptr<StreamSchedulerCallback> mScheduler; 325 BaseSessionCallback* mCallback; 326 kBaseNodeState mNodeState; 327 ImsMediaDataQueue mDataQueue; 328 std::list<BaseNode*> mListFrontNodes; 329 std::list<BaseNode*> mListRearNodes; 330 ImsMediaType mMediaType; 331 }; 332 333 #endif