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