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 #include <BaseNode.h>
18 #include <ImsMediaTrace.h>
19 #include <stdlib.h>
20
21 using NODE_ID_PAIR = std::pair<kBaseNodeId, const char*>;
22 static std::vector<NODE_ID_PAIR> vectorNodeId{
23 std::make_pair(kNodeIdUnknown, "NodeUnknown"),
24 std::make_pair(kNodeIdSocketWriter, "SocketWriter"),
25 std::make_pair(kNodeIdSocketReader, "SocketReader"),
26 std::make_pair(kNodeIdRtpEncoder, "RtpEncoder"),
27 std::make_pair(kNodeIdRtpDecoder, "RtpDecoder"),
28 std::make_pair(kNodeIdRtcpEncoder, "RtcpEncoder"),
29 std::make_pair(kNodeIdRtcpDecoder, "RtcpDecoder"),
30 std::make_pair(kNodeIdAudioSource, "AudioSource"),
31 std::make_pair(kNodeIdAudioPlayer, "AudioPlayer"),
32 std::make_pair(kNodeIdDtmfEncoder, "DtmfEncoder"),
33 std::make_pair(kNodeIdDtmfSender, "DtmfSender"),
34 std::make_pair(kNodeIdAudioPayloadEncoder, "AudioPayloadEncoder"),
35 std::make_pair(kNodeIdAudioPayloadDecoder, "AudioPayloadDecoder"),
36 std::make_pair(kNodeIdVideoSource, "VideoSource"),
37 std::make_pair(kNodeIdVideoRenderer, "VideoRenderer"),
38 std::make_pair(kNodeIdVideoPayloadEncoder, "VideoPayloadEncoder"),
39 std::make_pair(kNodeIdVideoPayloadDecoder, "VideoPayloadDecoder"),
40 std::make_pair(kNodeIdTextSource, "TextSource"),
41 std::make_pair(kNodeIdTextRenderer, "TextRenderer"),
42 std::make_pair(kNodeIdTextPayloadEncoder, "TextPayloadEncoder"),
43 std::make_pair(kNodeIdTextPayloadDecoder, "TextPayloadDecoder"),
44 };
45
BaseNode(BaseSessionCallback * callback)46 BaseNode::BaseNode(BaseSessionCallback* callback)
47 {
48 mScheduler = nullptr;
49 mCallback = callback;
50 mNodeState = kNodeStateStopped;
51 mMediaType = IMS_MEDIA_AUDIO;
52 mListFrontNodes.clear();
53 mListRearNodes.clear();
54 }
55
~BaseNode()56 BaseNode::~BaseNode()
57 {
58 ClearDataQueue();
59 mNodeState = kNodeStateStopped;
60 }
61
SetSessionCallback(BaseSessionCallback * callback)62 void BaseNode::SetSessionCallback(BaseSessionCallback* callback)
63 {
64 mCallback = callback;
65 }
66
SetSchedulerCallback(const std::shared_ptr<StreamSchedulerCallback> & callback)67 void BaseNode::SetSchedulerCallback(const std::shared_ptr<StreamSchedulerCallback>& callback)
68 {
69 mScheduler = callback;
70 }
71
ConnectRearNode(BaseNode * pRearNode)72 void BaseNode::ConnectRearNode(BaseNode* pRearNode)
73 {
74 if (pRearNode == nullptr)
75 {
76 return;
77 }
78
79 IMLOGD3("[ConnectRearNode] type[%d] connect [%s] to [%s]", mMediaType, GetNodeName(),
80 pRearNode->GetNodeName());
81 mListRearNodes.push_back(pRearNode);
82 pRearNode->mListFrontNodes.push_back(this);
83 }
84
DisconnectNodes()85 void BaseNode::DisconnectNodes()
86 {
87 while (!mListFrontNodes.empty())
88 {
89 DisconnectFrontNode(mListFrontNodes.back());
90 }
91
92 while (!mListRearNodes.empty())
93 {
94 DisconnectRearNode(mListRearNodes.back());
95 }
96 }
97
ClearDataQueue()98 void BaseNode::ClearDataQueue()
99 {
100 IMLOGD1("[ClearDataQueue] queue size[%d]", mDataQueue.GetCount());
101 mDataQueue.Clear();
102 }
103
GetNodeId()104 kBaseNodeId BaseNode::GetNodeId()
105 {
106 return kNodeIdUnknown;
107 }
108
Prepare()109 bool BaseNode::Prepare()
110 {
111 return RESULT_SUCCESS;
112 }
113
Start()114 ImsMediaResult BaseNode::Start()
115 {
116 if (!IsRunTimeStart())
117 {
118 return RESULT_SUCCESS;
119 }
120 else
121 {
122 IMLOGW0("[Start] Error - base method");
123 return RESULT_NOT_SUPPORTED;
124 }
125 }
126
ProcessStart()127 ImsMediaResult BaseNode::ProcessStart()
128 {
129 IMLOGW0("[ProcessStart] Error - base method");
130 return RESULT_NOT_SUPPORTED;
131 }
132
IsRunTime()133 bool BaseNode::IsRunTime()
134 {
135 return true;
136 }
137
IsRunTimeStart()138 bool BaseNode::IsRunTimeStart()
139 {
140 return true;
141 }
142
SetConfig(void * config)143 void BaseNode::SetConfig(void* config)
144 {
145 (void)config;
146 IMLOGW0("[SetConfig] Error - base method");
147 }
148
IsSameConfig(void * config)149 bool BaseNode::IsSameConfig(void* config)
150 {
151 (void)config;
152 IMLOGW0("[IsSameConfig] Error - base method");
153 return true;
154 }
155
UpdateConfig(void * config)156 ImsMediaResult BaseNode::UpdateConfig(void* config)
157 {
158 // check config items updates
159 bool isUpdateNode = false;
160
161 if (IsSameConfig(config))
162 {
163 IMLOGD0("[UpdateConfig] no update");
164 return RESULT_SUCCESS;
165 }
166 else
167 {
168 isUpdateNode = true;
169 }
170
171 kBaseNodeState prevState = mNodeState;
172
173 if (isUpdateNode && mNodeState == kNodeStateRunning)
174 {
175 Stop();
176 }
177
178 // reset the parameters
179 SetConfig(config);
180
181 if (isUpdateNode && prevState == kNodeStateRunning)
182 {
183 return Start();
184 }
185
186 return RESULT_SUCCESS;
187 }
188
ProcessData()189 void BaseNode::ProcessData()
190 {
191 IMLOGE0("ProcessData] Error - base method");
192 }
193
GetNodeName()194 const char* BaseNode::GetNodeName()
195 {
196 typedef typename std::vector<std::pair<kBaseNodeId, const char*>>::iterator iterator;
197
198 for (iterator it = vectorNodeId.begin(); it != vectorNodeId.end(); ++it)
199 {
200 if (it->first == GetNodeId())
201 {
202 return it->second;
203 }
204 }
205
206 return "NodeUnknown";
207 }
208
SetMediaType(ImsMediaType eType)209 void BaseNode::SetMediaType(ImsMediaType eType)
210 {
211 mMediaType = eType;
212 }
213
GetMediaType()214 ImsMediaType BaseNode::GetMediaType()
215 {
216 return mMediaType;
217 }
218
219 // Graph Interface
GetState()220 kBaseNodeState BaseNode::GetState()
221 {
222 return mNodeState;
223 }
224
SetState(kBaseNodeState state)225 void BaseNode::SetState(kBaseNodeState state)
226 {
227 mNodeState = state;
228 }
229
GetDataCount()230 uint32_t BaseNode::GetDataCount()
231 {
232 return mDataQueue.GetCount();
233 }
234
GetData(ImsMediaSubType * psubtype,uint8_t ** ppData,uint32_t * pnDataSize,uint32_t * pnTimestamp,bool * pbMark,uint32_t * pnSeqNum,ImsMediaSubType * peDataType,uint32_t * arrivalTime)235 bool BaseNode::GetData(ImsMediaSubType* psubtype, uint8_t** ppData, uint32_t* pnDataSize,
236 uint32_t* pnTimestamp, bool* pbMark, uint32_t* pnSeqNum, ImsMediaSubType* peDataType,
237 uint32_t* arrivalTime)
238 {
239 DataEntry* pEntry;
240
241 if (mDataQueue.Get(&pEntry))
242 {
243 if (psubtype)
244 *psubtype = pEntry->subtype;
245 if (ppData)
246 *ppData = pEntry->pbBuffer;
247 if (pnDataSize)
248 *pnDataSize = pEntry->nBufferSize;
249 if (pnTimestamp)
250 *pnTimestamp = pEntry->nTimestamp;
251 if (pbMark)
252 *pbMark = pEntry->bMark;
253 if (pnSeqNum)
254 *pnSeqNum = pEntry->nSeqNum;
255 if (peDataType)
256 *peDataType = pEntry->eDataType;
257 if (arrivalTime)
258 *arrivalTime = pEntry->arrivalTime;
259 return true;
260 }
261 else
262 {
263 if (psubtype)
264 *psubtype = MEDIASUBTYPE_UNDEFINED;
265 if (ppData)
266 *ppData = nullptr;
267 if (pnDataSize)
268 *pnDataSize = 0;
269 if (pnTimestamp)
270 *pnTimestamp = 0;
271 if (pbMark)
272 *pbMark = false;
273 if (pnSeqNum)
274 *pnSeqNum = 0;
275 if (peDataType)
276 *peDataType = MEDIASUBTYPE_UNDEFINED;
277 if (arrivalTime)
278 *arrivalTime = 0;
279 return false;
280 }
281 }
282
AddData(uint8_t * data,uint32_t size,uint32_t timestamp,bool mark,uint32_t seq,ImsMediaSubType subtype,ImsMediaSubType dataType,uint32_t arrivalTime,int32_t index)283 void BaseNode::AddData(uint8_t* data, uint32_t size, uint32_t timestamp, bool mark, uint32_t seq,
284 ImsMediaSubType subtype, ImsMediaSubType dataType, uint32_t arrivalTime, int32_t index)
285 {
286 DataEntry entry = DataEntry();
287 entry.pbBuffer = data;
288 entry.nBufferSize = size;
289 entry.nTimestamp = timestamp;
290 entry.bMark = mark;
291 entry.nSeqNum = seq;
292 entry.eDataType = dataType;
293 entry.subtype = subtype;
294 entry.arrivalTime = arrivalTime;
295 index == -1 ? mDataQueue.Add(&entry) : mDataQueue.InsertAt(index, &entry);
296 }
297
DeleteData()298 void BaseNode::DeleteData()
299 {
300 mDataQueue.Delete();
301 }
302
SendDataToRearNode(ImsMediaSubType subtype,uint8_t * pData,uint32_t nDataSize,uint32_t nTimestamp,bool bMark,uint32_t nSeqNum,ImsMediaSubType nDataType,uint32_t arrivalTime)303 void BaseNode::SendDataToRearNode(ImsMediaSubType subtype, uint8_t* pData, uint32_t nDataSize,
304 uint32_t nTimestamp, bool bMark, uint32_t nSeqNum, ImsMediaSubType nDataType,
305 uint32_t arrivalTime)
306 {
307 bool needRunCount = false;
308
309 for (auto& node : mListRearNodes)
310 {
311 if (node != nullptr && node->GetState() == kNodeStateRunning)
312 {
313 node->OnDataFromFrontNode(
314 subtype, pData, nDataSize, nTimestamp, bMark, nSeqNum, nDataType, arrivalTime);
315
316 if (!node->IsRunTime())
317 {
318 needRunCount = true;
319 }
320 }
321 }
322
323 if (needRunCount && mScheduler != nullptr)
324 {
325 mScheduler->onAwakeScheduler();
326 }
327 }
328
OnDataFromFrontNode(ImsMediaSubType subtype,uint8_t * pData,uint32_t nDataSize,uint32_t nTimestamp,bool bMark,uint32_t nSeqNum,ImsMediaSubType nDataType,uint32_t arrivalTime)329 void BaseNode::OnDataFromFrontNode(ImsMediaSubType subtype, uint8_t* pData, uint32_t nDataSize,
330 uint32_t nTimestamp, bool bMark, uint32_t nSeqNum, ImsMediaSubType nDataType,
331 uint32_t arrivalTime)
332 {
333 DataEntry entry = DataEntry();
334 entry.pbBuffer = pData;
335 entry.nBufferSize = nDataSize;
336 entry.nTimestamp = nTimestamp;
337 entry.bMark = bMark;
338 entry.nSeqNum = nSeqNum;
339 entry.eDataType = nDataType;
340 entry.subtype = subtype;
341 entry.arrivalTime = arrivalTime;
342 mDataQueue.Add(&entry);
343 }
344
DisconnectRearNode(BaseNode * pRearNode)345 void BaseNode::DisconnectRearNode(BaseNode* pRearNode)
346 {
347 if (pRearNode == nullptr)
348 {
349 mListRearNodes.pop_back();
350 return;
351 }
352
353 IMLOGD3("[DisconnectRearNode] type[%d] disconnect [%s] from [%s]", mMediaType, GetNodeName(),
354 pRearNode->GetNodeName());
355
356 mListRearNodes.remove(pRearNode);
357 pRearNode->mListFrontNodes.remove(this);
358 }
359
DisconnectFrontNode(BaseNode * pFrontNode)360 void BaseNode::DisconnectFrontNode(BaseNode* pFrontNode)
361 {
362 if (pFrontNode == nullptr)
363 {
364 mListFrontNodes.pop_back();
365 return;
366 }
367
368 IMLOGD3("[DisconnectFrontNode] type[%d] disconnect [%s] from [%s]", mMediaType,
369 pFrontNode->GetNodeName(), GetNodeName());
370
371 mListFrontNodes.remove(pFrontNode);
372 pFrontNode->mListRearNodes.remove(this);
373 }