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 <RtpService.h>
18 #include <RtpGlobal.h>
19 #include <RtpImpl.h>
20 #include <RtpStack.h>
21 #include <RtpTrace.h>
22 #include <RtpError.h>
23 #include <RtpStackUtil.h>
24 
25 RtpStack* g_pobjRtpStack = nullptr;
26 
addSdesItem(OUT RtcpConfigInfo * pobjRtcpCfgInfo,IN RtpDt_UChar * sdesName,IN RtpDt_UInt32 uiLength)27 RtpDt_Void addSdesItem(
28         OUT RtcpConfigInfo* pobjRtcpCfgInfo, IN RtpDt_UChar* sdesName, IN RtpDt_UInt32 uiLength)
29 {
30     tRTCP_SDES_ITEM stSdesItem;
31     memset(&stSdesItem, RTP_ZERO, sizeof(tRTCP_SDES_ITEM));
32     RtpDt_UInt32 uiIndex = RTP_ZERO;
33     stSdesItem.ucType = RTP_ONE;  // RTCP_SDES_CNAME
34     stSdesItem.ucLength = uiLength;
35     stSdesItem.pValue = sdesName;
36     pobjRtcpCfgInfo->addRtcpSdesItem(&stSdesItem, uiIndex);
37     return;
38 }  // addSdesItem
39 
populateReceiveRtpIndInfo(OUT tRtpSvcIndSt_ReceiveRtpInd * pstRtpIndMsg,IN RtpPacket * pobjRtpPkt)40 RtpDt_Void populateReceiveRtpIndInfo(
41         OUT tRtpSvcIndSt_ReceiveRtpInd* pstRtpIndMsg, IN RtpPacket* pobjRtpPkt)
42 {
43     RtpHeader* pobjRtpHeader = pobjRtpPkt->getRtpHeader();
44     pstRtpIndMsg->bMbit = pobjRtpHeader->getMarker() > 0 ? eRTP_TRUE : eRTP_FALSE;
45     pstRtpIndMsg->dwTimestamp = pobjRtpHeader->getRtpTimestamp();
46     pstRtpIndMsg->dwPayloadType = pobjRtpHeader->getPayloadType();
47     pstRtpIndMsg->dwSeqNum = pobjRtpHeader->getSequenceNumber();
48     pstRtpIndMsg->dwSsrc = pobjRtpHeader->getRtpSsrc();
49 
50     // Header length
51     pstRtpIndMsg->wMsgHdrLen = RTP_FIXED_HDR_LEN;
52     pstRtpIndMsg->wMsgHdrLen += RTP_WORD_SIZE * pobjRtpHeader->getCsrcCount();
53 
54     if (pobjRtpPkt->getExtHeader())
55     {
56         pstRtpIndMsg->wMsgHdrLen += pobjRtpPkt->getExtHeader()->getLength();
57         RtpDt_UChar* pExtHdrBuffer = pobjRtpPkt->getExtHeader()->getBuffer();
58         RtpDt_Int32 uiByte4Data =
59                 RtpOsUtil::Ntohl(*(reinterpret_cast<RtpDt_UInt32*>(pExtHdrBuffer)));
60         pstRtpIndMsg->wDefinedByProfile = uiByte4Data >> 16;
61         pstRtpIndMsg->wExtLen = uiByte4Data & 0x00FF;
62         pstRtpIndMsg->pExtData = pExtHdrBuffer + 4;
63         pstRtpIndMsg->wExtDataSize = pobjRtpPkt->getExtHeader()->getLength() - 4;
64     }
65     else
66     {
67         pstRtpIndMsg->wDefinedByProfile = 0;
68         pstRtpIndMsg->wExtLen = 0;
69         pstRtpIndMsg->pExtData = nullptr;
70         pstRtpIndMsg->wExtDataSize = 0;
71     }
72     // End Header length
73     // play the payload
74     RtpBuffer* pobjRtpPayload = pobjRtpPkt->getRtpPayload();
75     // Header
76 
77     // body
78     if (!pobjRtpPayload)
79     {
80         pstRtpIndMsg->wMsgBodyLen = 0;
81         pstRtpIndMsg->pMsgBody = nullptr;
82         return;
83     }
84 
85     pstRtpIndMsg->wMsgBodyLen = pobjRtpPayload->getLength();
86     pstRtpIndMsg->pMsgBody = reinterpret_cast<RtpDt_UChar*>(pobjRtpPayload->getBuffer());
87 }
88 
populateRcvdReportFromStk(IN std::list<RtcpReportBlock * > & pobjRepBlkList,OUT tRtpSvcRecvReport * pstRcvdReport)89 eRtp_Bool populateRcvdReportFromStk(
90         IN std::list<RtcpReportBlock*>& pobjRepBlkList, OUT tRtpSvcRecvReport* pstRcvdReport)
91 {
92     if (pobjRepBlkList.size() > RTP_ZERO)
93     {
94         // application supports one RR
95         RtcpReportBlock* pobjRepBlkElm = pobjRepBlkList.front();
96         if (pobjRepBlkElm == nullptr)
97         {
98             return eRTP_FALSE;
99         }
100 
101         pstRcvdReport->ssrc = pobjRepBlkElm->getSsrc();
102         pstRcvdReport->fractionLost = pobjRepBlkElm->getFracLost();
103         pstRcvdReport->cumPktsLost = pobjRepBlkElm->getCumNumPktLost();
104         pstRcvdReport->extHighSeqNum = pobjRepBlkElm->getExtHighSeqRcv();
105         pstRcvdReport->jitter = pobjRepBlkElm->getJitter();
106         pstRcvdReport->lsr = pobjRepBlkElm->getLastSR();
107         pstRcvdReport->delayLsr = pobjRepBlkElm->getDelayLastSR();
108 
109         RTP_TRACE_MESSAGE("Received RR info :  [SSRC = %u] [FRAC LOST = %u]", pstRcvdReport->ssrc,
110                 pstRcvdReport->fractionLost);
111 
112         RTP_TRACE_MESSAGE("Received RR info :  [CUM PKTS LOST = %u] [EXT HIGE SEQ NUM = %u]",
113                 pstRcvdReport->cumPktsLost, pstRcvdReport->extHighSeqNum);
114 
115         RTP_TRACE_MESSAGE("Received RR info :  [JITTER = %u] [LSR = %u]", pstRcvdReport->jitter,
116                 pstRcvdReport->lsr);
117 
118         RTP_TRACE_MESSAGE(
119                 "Received RR info :  [DELAY SINCE LSR = %u] ", pstRcvdReport->delayLsr, nullptr);
120     }
121     else
122     {
123         pstRcvdReport->ssrc = RTP_ZERO;
124         pstRcvdReport->fractionLost = RTP_ZERO;
125         pstRcvdReport->cumPktsLost = RTP_ZERO;
126         pstRcvdReport->extHighSeqNum = RTP_ZERO;
127         pstRcvdReport->jitter = RTP_ZERO;
128         pstRcvdReport->lsr = RTP_ZERO;
129         pstRcvdReport->delayLsr = RTP_ZERO;
130     }
131 
132     return eRTP_TRUE;
133 }  // populateRcvdReportFromStk
134 
populateRcvdRrInfoFromStk(IN std::list<RtcpRrPacket * > & pobjRrList,OUT tNotifyReceiveRtcpRrInd * pstRrInfo)135 eRtp_Bool populateRcvdRrInfoFromStk(
136         IN std::list<RtcpRrPacket*>& pobjRrList, OUT tNotifyReceiveRtcpRrInd* pstRrInfo)
137 {
138     if (pobjRrList.empty())
139     {
140         return eRTP_FALSE;
141     }
142     // application supports one RR
143     RtcpRrPacket* pobjRrPkt = pobjRrList.front();
144     if (pobjRrPkt == nullptr)
145     {
146         return eRTP_FALSE;
147     }
148 
149     tRtpSvcRecvReport* pstRcvdReport = &(pstRrInfo->stRecvRpt);
150     std::list<RtcpReportBlock*>& pobjRepBlkList = pobjRrPkt->getReportBlockList();
151     return populateRcvdReportFromStk(pobjRepBlkList, pstRcvdReport);
152 }  // populateRcvdRrInfoFromStk
153 
populateRcvdSrInfoFromStk(IN std::list<RtcpSrPacket * > & pobjSrList,OUT tNotifyReceiveRtcpSrInd * pstSrInfo)154 eRtp_Bool populateRcvdSrInfoFromStk(
155         IN std::list<RtcpSrPacket*>& pobjSrList, OUT tNotifyReceiveRtcpSrInd* pstSrInfo)
156 {
157     if (pobjSrList.empty())
158     {
159         return eRTP_FALSE;
160     }
161     // get SR packet data
162     RtcpSrPacket* pobjSrPkt = pobjSrList.front();
163     if (pobjSrPkt == nullptr)
164     {
165         return eRTP_FALSE;
166     }
167 
168     pstSrInfo->ntpTimestampMsw = pobjSrPkt->getNtpTime()->m_uiNtpHigh32Bits;
169     pstSrInfo->ntpTimestampLsw = pobjSrPkt->getNtpTime()->m_uiNtpLow32Bits;
170     pstSrInfo->rtpTimestamp = pobjSrPkt->getRtpTimestamp();
171     pstSrInfo->sendPktCount = pobjSrPkt->getSendPktCount();
172     pstSrInfo->sendOctCount = pobjSrPkt->getSendOctetCount();
173 
174     RTP_TRACE_MESSAGE("Received SR info :  [NTP High 32 = %u] [NTP LOW 32 = %u]",
175             pstSrInfo->ntpTimestampMsw, pstSrInfo->ntpTimestampLsw);
176 
177     RTP_TRACE_MESSAGE(
178             "Received SR info :  [RTP timestamp = %u] ", pstSrInfo->rtpTimestamp, nullptr);
179 
180     RTP_TRACE_MESSAGE("Received SR info :  [SEND PKT COUNT = %u] [SEND OCTET COUNT = %u]",
181             pstSrInfo->sendPktCount, pstSrInfo->sendOctCount);
182 
183     // populate tRtpSvcRecvReport
184     tRtpSvcRecvReport* pstRcvdReport = &(pstSrInfo->stRecvRpt);
185     RtcpRrPacket* pobjRepBlk = pobjSrPkt->getRrPktInfo();
186     std::list<RtcpReportBlock*>& pobjRepBlkList = pobjRepBlk->getReportBlockList();
187     return populateRcvdReportFromStk(pobjRepBlkList, pstRcvdReport);
188 }
189 
populateRcvdFbInfoFromStk(IN RtcpFbPacket * m_pobjRtcpFbPkt,OUT tRtpSvcIndSt_ReceiveRtcpFeedbackInd * stFbRtcpMsg)190 eRtp_Bool populateRcvdFbInfoFromStk(
191         IN RtcpFbPacket* m_pobjRtcpFbPkt, OUT tRtpSvcIndSt_ReceiveRtcpFeedbackInd* stFbRtcpMsg)
192 {
193     if (m_pobjRtcpFbPkt != nullptr)
194     {
195         stFbRtcpMsg->wPayloadType = m_pobjRtcpFbPkt->getRtcpHdrInfo()->getPacketType();
196         stFbRtcpMsg->wFmt = m_pobjRtcpFbPkt->getRtcpHdrInfo()->getReceptionReportCount();
197         stFbRtcpMsg->dwMediaSsrc = m_pobjRtcpFbPkt->getMediaSsrc();
198         stFbRtcpMsg->wMsgLen = m_pobjRtcpFbPkt->getRtcpHdrInfo()->getLength();
199         if (m_pobjRtcpFbPkt->getFCI() != nullptr)
200         {
201             stFbRtcpMsg->pMsg = m_pobjRtcpFbPkt->getFCI()->getBuffer();
202         }
203         return eRTP_TRUE;
204     }
205 
206     return eRTP_FALSE;
207 }
208 
populateRtpProfile(OUT RtpStackProfile * pobjStackProfile)209 RtpDt_Void populateRtpProfile(OUT RtpStackProfile* pobjStackProfile)
210 {
211     pobjStackProfile->setRtcpBandwidth(RTP_DEF_RTCP_BW_SIZE);
212     pobjStackProfile->setMtuSize(RTP_CONF_MTU_SIZE);
213     pobjStackProfile->setTermNumber(RTP_CONF_SSRC_SEED);
214 }
215 
SetRtpHeaderExtension(IN tRtpSvc_SendRtpPacketParam * pstRtpParam)216 RtpBuffer* SetRtpHeaderExtension(IN tRtpSvc_SendRtpPacketParam* pstRtpParam)
217 {
218     RtpBuffer* pobjXHdr = new RtpBuffer();
219 
220     // HDR extension
221     if (pstRtpParam->bXbit)
222     {
223         const RtpDt_Int32 headerSize = 4;
224         RtpDt_Int32 nBufferSize = headerSize + pstRtpParam->wExtLen * sizeof(int32_t);
225         RtpDt_UChar* pBuf = new RtpDt_UChar[nBufferSize];
226 
227         if (pstRtpParam->wExtLen * sizeof(int32_t) != pstRtpParam->nExtDataSize)
228         {
229             RTP_TRACE_WARNING("SetRtpHeaderExtension invalid data size len[%d], size[%d]",
230                     pstRtpParam->wExtLen, pstRtpParam->nExtDataSize);
231         }
232 
233         // define by profile
234         pBuf[0] = (((unsigned)pstRtpParam->wDefinedByProfile) >> 8) & 0x00ff;
235         pBuf[1] = pstRtpParam->wDefinedByProfile & 0x00ff;
236 
237         // number of the extension data set
238         pBuf[2] = (((unsigned)pstRtpParam->wExtLen) >> 8) & 0x00ff;
239         pBuf[3] = (pstRtpParam->wExtLen) & 0x00ff;
240 
241         memcpy(pBuf + 4, pstRtpParam->pExtData, pstRtpParam->nExtDataSize);
242         pobjXHdr->setBufferInfo(nBufferSize, pBuf);
243     }
244     else
245     {
246         pobjXHdr->setBufferInfo(0, nullptr);
247     }
248 
249     return pobjXHdr;
250 }
251 
GetRtpHeaderExtensionSize(eRtp_Bool bEnableCVO)252 RtpDt_UInt16 GetRtpHeaderExtensionSize(eRtp_Bool bEnableCVO)
253 {
254     if (bEnableCVO)
255         return RTP_CVO_XHDR_LEN;
256 
257     return 0;
258 }
259 
IMS_RtpSvc_Initialize()260 GLOBAL eRtp_Bool IMS_RtpSvc_Initialize()
261 {
262     if (g_pobjRtpStack == nullptr)
263     {
264         g_pobjRtpStack = new RtpStack();
265         if (g_pobjRtpStack == nullptr)
266         {
267             return eRTP_FALSE;
268         }
269 
270         RtpStackProfile* pobjStackProfile = new RtpStackProfile();
271         if (pobjStackProfile == nullptr)
272         {
273             return eRTP_FALSE;
274         }
275 
276         populateRtpProfile(pobjStackProfile);
277         g_pobjRtpStack->setStackProfile(pobjStackProfile);
278     }
279 
280     return eRTP_TRUE;
281 }
282 
IMS_RtpSvc_Deinitialize()283 GLOBAL eRtp_Bool IMS_RtpSvc_Deinitialize()
284 {
285     if (g_pobjRtpStack)
286     {
287         delete g_pobjRtpStack;
288         g_pobjRtpStack = nullptr;
289     }
290 
291     return eRTP_TRUE;
292 }
293 
IMS_RtpSvc_CreateSession(IN RtpDt_Char * szLocalIP,IN RtpDt_UInt32 port,IN RtpDt_Void * pAppData,OUT RtpDt_UInt32 * puSsrc,OUT RTPSESSIONID * hRtpSession)294 GLOBAL eRtp_Bool IMS_RtpSvc_CreateSession(IN RtpDt_Char* szLocalIP, IN RtpDt_UInt32 port,
295         IN RtpDt_Void* pAppData, OUT RtpDt_UInt32* puSsrc, OUT RTPSESSIONID* hRtpSession)
296 {
297     if (g_pobjRtpStack == nullptr || szLocalIP == nullptr)
298     {
299         return eRTP_FALSE;
300     }
301 
302     RtpSession* pobjRtpSession = g_pobjRtpStack->createRtpSession();
303     if (pobjRtpSession == nullptr)
304     {
305         return eRTP_FALSE;
306     }
307 
308     // set ip and port
309     RtpBuffer* pobjTransAddr = new RtpBuffer();
310     RtpDt_UInt32 uiIpLen = strlen(szLocalIP) + 1;
311     RtpDt_UChar* pcIpAddr = new RtpDt_UChar[uiIpLen];
312     memcpy(pcIpAddr, szLocalIP, uiIpLen);
313     pobjTransAddr->setBufferInfo(uiIpLen, pcIpAddr);
314 
315     pobjRtpSession->setRtpTransAddr(pobjTransAddr);
316     pobjRtpSession->setRtpPort((RtpDt_UInt16)port);
317 
318     *puSsrc = pobjRtpSession->getSsrc();
319     *hRtpSession = reinterpret_cast<RtpDt_Void*>(pobjRtpSession);
320 
321     RtpImpl* pobjRtpImpl = new RtpImpl();
322     if (pobjRtpImpl == nullptr)
323     {
324         return eRTP_FALSE;
325     }
326     pobjRtpImpl->setAppdata(pAppData);
327 
328     RtcpConfigInfo* pobjRtcpConfigInfo = new RtcpConfigInfo();
329     addSdesItem(
330             pobjRtcpConfigInfo, reinterpret_cast<RtpDt_UChar*>(szLocalIP), strlen(szLocalIP) + 1);
331 
332     eRTP_STATUS_CODE eInitSta = pobjRtpSession->initSession(pobjRtpImpl, pobjRtcpConfigInfo);
333     if (eInitSta != RTP_SUCCESS)
334     {
335         return eRTP_FALSE;
336     }
337     return eRTP_TRUE;
338 }
339 
IMS_RtpSvc_SetPayload(IN RTPSESSIONID hRtpSession,IN tRtpSvc_SetPayloadParam * pstPayloadInfo,IN eRtp_Bool bEnableXHdr,IN RtpDt_UInt32 nNumOfPayloadParam)340 GLOBAL eRtp_Bool IMS_RtpSvc_SetPayload(IN RTPSESSIONID hRtpSession,
341         IN tRtpSvc_SetPayloadParam* pstPayloadInfo, IN eRtp_Bool bEnableXHdr,
342         IN RtpDt_UInt32 nNumOfPayloadParam)
343 {
344     RtpDt_UInt32 payloadType[RTP_MAX_PAYLOAD_TYPE] = {0};
345 
346     for (RtpDt_UInt32 i = 0; i < nNumOfPayloadParam; i++)
347     {
348         RTP_TRACE_MESSAGE(
349                 "IMS_RtpSvc_SetPayload   payloadtype = %d", pstPayloadInfo[i].payloadType, 0);
350         payloadType[i] = pstPayloadInfo[i].payloadType;
351     }
352     RtpPayloadInfo* pobjlPayloadInfo =
353             new RtpPayloadInfo(payloadType, pstPayloadInfo[0].samplingRate, nNumOfPayloadParam);
354     if (pobjlPayloadInfo == nullptr)
355     {
356         return eRTP_FALSE;
357     }
358 
359     RtpSession* pobjRtpSession = reinterpret_cast<RtpSession*>(hRtpSession);
360 
361     if (g_pobjRtpStack == nullptr ||
362             g_pobjRtpStack->isValidRtpSession(pobjRtpSession) == eRTP_FAILURE)
363     {
364         delete pobjlPayloadInfo;
365         return eRTP_FALSE;
366     }
367 
368     eRTP_STATUS_CODE eInitSta =
369             pobjRtpSession->setPayload(pobjlPayloadInfo, GetRtpHeaderExtensionSize(bEnableXHdr));
370     delete pobjlPayloadInfo;
371     if (eInitSta != RTP_SUCCESS)
372     {
373         return eRTP_FALSE;
374     }
375 
376     return eRTP_TRUE;
377 }
378 
IMS_RtpSvc_SetRTCPInterval(IN RTPSESSIONID hRtpSession,IN RtpDt_UInt32 nInterval)379 GLOBAL eRtp_Bool IMS_RtpSvc_SetRTCPInterval(IN RTPSESSIONID hRtpSession, IN RtpDt_UInt32 nInterval)
380 {
381     if (g_pobjRtpStack == nullptr ||
382             g_pobjRtpStack->isValidRtpSession(reinterpret_cast<RtpSession*>(hRtpSession)) ==
383                     eRTP_FAILURE)
384         return eRTP_FALSE;
385 
386     (reinterpret_cast<RtpSession*>(hRtpSession))->setRTCPTimerValue(nInterval);
387     return eRTP_TRUE;
388 }
389 
IMS_RtpSvc_DeleteSession(IN RTPSESSIONID hRtpSession)390 GLOBAL eRtp_Bool IMS_RtpSvc_DeleteSession(IN RTPSESSIONID hRtpSession)
391 {
392     RtpSession* pobjRtpSession = reinterpret_cast<RtpSession*>(hRtpSession);
393 
394     if (g_pobjRtpStack == nullptr ||
395             g_pobjRtpStack->isValidRtpSession(pobjRtpSession) == eRTP_FAILURE)
396         return eRTP_FALSE;
397 
398     eRTP_STATUS_CODE eDelRtpStrm = g_pobjRtpStack->deleteRtpSession(pobjRtpSession);
399     if (eDelRtpStrm != RTP_SUCCESS)
400     {
401         return eRTP_FALSE;
402     }
403 
404     delete pobjRtpSession;
405     return eRTP_TRUE;
406 }
407 
IMS_RtpSvc_SendRtpPacket(IN RtpServiceListener * pobjRtpServiceListener,IN RTPSESSIONID hRtpSession,IN RtpDt_Char * pBuffer,IN RtpDt_UInt16 wBufferLength,IN tRtpSvc_SendRtpPacketParam * pstRtpParam)408 GLOBAL eRtp_Bool IMS_RtpSvc_SendRtpPacket(IN RtpServiceListener* pobjRtpServiceListener,
409         IN RTPSESSIONID hRtpSession, IN RtpDt_Char* pBuffer, IN RtpDt_UInt16 wBufferLength,
410         IN tRtpSvc_SendRtpPacketParam* pstRtpParam)
411 {
412     RtpSession* pobjRtpSession = reinterpret_cast<RtpSession*>(hRtpSession);
413 
414     if (g_pobjRtpStack == nullptr ||
415             g_pobjRtpStack->isValidRtpSession(pobjRtpSession) == eRTP_FAILURE)
416         return eRTP_FALSE;
417 
418     if (pobjRtpSession->isRtpEnabled() == eRTP_FALSE)
419     {
420         return eRTP_FALSE;
421     }
422 
423     RtpBuffer* pobjRtpPayload = new RtpBuffer();
424     if (pobjRtpPayload == nullptr)
425     {
426         RTP_TRACE_WARNING(
427                 "IMS_RtpSvc_SendRtpPacket pobjRtpPayload - Malloc failed", RTP_ZERO, RTP_ZERO);
428         return eRTP_FALSE;
429     }
430 
431     RtpBuffer* pobjRtpBuf = new RtpBuffer();
432     if (pobjRtpBuf == nullptr)
433     {
434         delete pobjRtpPayload;
435         RTP_TRACE_MESSAGE(
436                 "IMS_RtpSvc_SendRtpPacket pobjRtpBuf - Malloc failed", RTP_ZERO, RTP_ZERO);
437         return eRTP_FALSE;
438     }
439 
440     // Create RTP packet
441     //  1. Set Marker bit
442     eRtp_Bool bMbit = eRTP_FALSE;
443 
444     if (pstRtpParam->bMbit == eRTP_TRUE)
445     {
446         bMbit = eRTP_TRUE;
447     }
448 
449     // 2. Set Payload
450     pobjRtpPayload->setBufferInfo(wBufferLength, reinterpret_cast<RtpDt_UChar*>(pBuffer));
451     eRtp_Bool bUseLastTimestamp = pstRtpParam->bUseLastTimestamp ? eRTP_TRUE : eRTP_FALSE;
452     eRTP_STATUS_CODE eRtpCreateStat = pobjRtpSession->createRtpPacket(pobjRtpPayload, bMbit,
453             pstRtpParam->byPayLoadType, bUseLastTimestamp, pstRtpParam->diffFromLastRtpTimestamp,
454             SetRtpHeaderExtension(pstRtpParam), pobjRtpBuf);
455 
456     // 3. de-init and free the temp variable both in success and failure case
457     pobjRtpPayload->setBufferInfo(RTP_ZERO, nullptr);
458     delete pobjRtpPayload;
459 
460     if (eRtpCreateStat != RTP_SUCCESS)
461     {
462         delete pobjRtpBuf;
463         RTP_TRACE_WARNING(
464                 "IMS_RtpSvc_SendRtpPacket - eRtpCreateStat != RTP_SUCCESS ", RTP_ZERO, RTP_ZERO);
465 
466         return eRTP_FALSE;
467     }
468     // End Create RTP packet
469 
470     if (pobjRtpSession->isRtpEnabled() == eRTP_FALSE)
471     {
472         delete pobjRtpBuf;
473         return eRTP_FALSE;
474     }
475 
476     // dispatch to peer
477     if (pobjRtpServiceListener->OnRtpPacket(pobjRtpBuf->getBuffer(), pobjRtpBuf->getLength()) == -1)
478     {
479         delete pobjRtpBuf;
480         RTP_TRACE_WARNING("On Rtp packet failed ..! OnRtpPacket", RTP_ZERO, RTP_ZERO);
481         return eRTP_FALSE;
482     }
483 
484     delete pobjRtpBuf;
485 
486     return eRTP_TRUE;
487 }
488 
IMS_RtpSvc_ProcRtpPacket(IN RtpServiceListener * pvIRtpSession,IN RTPSESSIONID hRtpSession,IN RtpDt_UChar * pMsg,IN RtpDt_UInt16 uiMsgLength,IN RtpDt_Char * pPeerIp,IN RtpDt_UInt16 uiPeerPort,OUT RtpDt_UInt32 & uiPeerSsrc)489 GLOBAL eRtp_Bool IMS_RtpSvc_ProcRtpPacket(IN RtpServiceListener* pvIRtpSession,
490         IN RTPSESSIONID hRtpSession, IN RtpDt_UChar* pMsg, IN RtpDt_UInt16 uiMsgLength,
491         IN RtpDt_Char* pPeerIp, IN RtpDt_UInt16 uiPeerPort, OUT RtpDt_UInt32& uiPeerSsrc)
492 {
493     tRtpSvc_IndicationFromStack stackInd = RTPSVC_RECEIVE_RTP_IND;
494     RtpSession* pobjRtpSession = reinterpret_cast<RtpSession*>(hRtpSession);
495 
496     if (g_pobjRtpStack == nullptr ||
497             g_pobjRtpStack->isValidRtpSession(pobjRtpSession) == eRTP_FAILURE)
498     {
499         return eRTP_FALSE;
500     }
501 
502     if (pobjRtpSession->isRtpEnabled() == eRTP_FALSE)
503     {
504         return eRTP_FALSE;
505     }
506 
507     RtpPacket* pobjRtpPkt = new RtpPacket();
508     if (pobjRtpPkt == nullptr)
509     {
510         RTP_TRACE_WARNING(
511                 "IMS_RtpSvc_ProcRtpPacket pobjRtpPkt - Malloc failed", RTP_ZERO, RTP_ZERO);
512         return eRTP_FALSE;
513     }
514 
515     RtpBuffer objRtpBuf;
516     objRtpBuf.setBufferInfo(uiMsgLength, pMsg);
517 
518     RtpDt_UInt32 uiTransLen = strlen(reinterpret_cast<const RtpDt_Char*>(pPeerIp));
519     RtpBuffer objRmtAddr;
520     objRmtAddr.setBufferInfo(uiTransLen + 1, reinterpret_cast<RtpDt_UChar*>(pPeerIp));
521 
522     eRTP_STATUS_CODE eStatus =
523             pobjRtpSession->processRcvdRtpPkt(&objRmtAddr, uiPeerPort, &objRtpBuf, pobjRtpPkt);
524     objRtpBuf.setBufferInfo(RTP_ZERO, nullptr);
525     objRmtAddr.setBufferInfo(RTP_ZERO, nullptr);
526     if (eStatus != RTP_SUCCESS)
527     {
528         if (eStatus == RTP_OWN_SSRC_COLLISION)
529             pobjRtpSession->sendRtcpByePacket();
530 
531         RTP_TRACE_WARNING("process packet failed with reason [%d]", eStatus, RTP_ZERO);
532         delete pobjRtpPkt;
533         return eRTP_FALSE;
534     }
535 
536     uiPeerSsrc = pobjRtpPkt->getRtpHeader()->getRtpSsrc();
537 
538     // populate stRtpIndMsg
539     tRtpSvcIndSt_ReceiveRtpInd stRtpIndMsg;
540     stRtpIndMsg.pMsgHdr = pMsg;
541     populateReceiveRtpIndInfo(&stRtpIndMsg, pobjRtpPkt);
542 
543     if (pobjRtpSession->isRtpEnabled() == eRTP_FALSE)
544     {
545         delete pobjRtpPkt;
546         return eRTP_FALSE;
547     }
548 
549     pvIRtpSession->OnPeerInd(stackInd, (RtpDt_Void*)&stRtpIndMsg);
550 
551     delete pobjRtpPkt;
552     return eRTP_TRUE;
553 }
554 
IMS_RtpSvc_SessionEnableRTP(IN RTPSESSIONID rtpSessionId,IN eRtp_Bool bResetSsrc)555 GLOBAL eRtp_Bool IMS_RtpSvc_SessionEnableRTP(IN RTPSESSIONID rtpSessionId, IN eRtp_Bool bResetSsrc)
556 {
557     RtpSession* pobjRtpSession = reinterpret_cast<RtpSession*>(rtpSessionId);
558 
559     if (g_pobjRtpStack == nullptr ||
560             g_pobjRtpStack->isValidRtpSession(pobjRtpSession) == eRTP_FAILURE)
561         return eRTP_FALSE;
562 
563     // generate SSRC
564     RtpDt_UInt32 oldSsrc = pobjRtpSession->getSsrc();
565     if (oldSsrc == 0 || bResetSsrc == eRTP_TRUE)
566     {
567         RtpDt_UInt32 uiSsrc = RtpStackUtil::generateNewSsrc(RTP_CONF_SSRC_SEED);
568         RTP_TRACE_WARNING("[SessionEnableRTP] SSRC changed [%x] -> [%x]", oldSsrc, uiSsrc);
569         pobjRtpSession->setSsrc(uiSsrc);
570     }
571 
572     if (pobjRtpSession->enableRtp() == RTP_SUCCESS)
573         return eRTP_TRUE;
574 
575     return eRTP_FALSE;
576 }
577 
IMS_RtpSvc_SessionDisableRTP(IN RTPSESSIONID rtpSessionId)578 GLOBAL eRtp_Bool IMS_RtpSvc_SessionDisableRTP(IN RTPSESSIONID rtpSessionId)
579 {
580     RtpSession* pobjRtpSession = reinterpret_cast<RtpSession*>(rtpSessionId);
581 
582     if (g_pobjRtpStack == nullptr ||
583             g_pobjRtpStack->isValidRtpSession(pobjRtpSession) == eRTP_FAILURE)
584         return eRTP_FALSE;
585 
586     if (pobjRtpSession->disableRtp() == RTP_SUCCESS)
587         return eRTP_TRUE;
588 
589     return eRTP_FALSE;
590 }
591 
IMS_RtpSvc_SessionEnableRTCP(IN RTPSESSIONID hRtpSession,IN eRtp_Bool enableRTCPBye)592 GLOBAL eRtp_Bool IMS_RtpSvc_SessionEnableRTCP(
593         IN RTPSESSIONID hRtpSession, IN eRtp_Bool enableRTCPBye)
594 {
595     RtpSession* pobjRtpSession = reinterpret_cast<RtpSession*>(hRtpSession);
596 
597     if (g_pobjRtpStack == nullptr ||
598             g_pobjRtpStack->isValidRtpSession(pobjRtpSession) == eRTP_FAILURE)
599         return eRTP_FALSE;
600 
601     eRTP_STATUS_CODE eRtcpStatus = pobjRtpSession->enableRtcp((eRtp_Bool)enableRTCPBye);
602     if (eRtcpStatus != RTP_SUCCESS)
603     {
604         return eRTP_FALSE;
605     }
606     return eRTP_TRUE;
607 }
608 
IMS_RtpSvc_SessionDisableRTCP(IN RTPSESSIONID hRtpSession)609 GLOBAL eRtp_Bool IMS_RtpSvc_SessionDisableRTCP(IN RTPSESSIONID hRtpSession)
610 {
611     RtpSession* pobjRtpSession = reinterpret_cast<RtpSession*>(hRtpSession);
612     eRTP_STATUS_CODE eRtcpStatus = RTP_SUCCESS;
613 
614     if (g_pobjRtpStack == nullptr ||
615             g_pobjRtpStack->isValidRtpSession(pobjRtpSession) == eRTP_FAILURE)
616         return eRTP_FALSE;
617 
618     eRtcpStatus = pobjRtpSession->disableRtcp();
619     if (eRtcpStatus != RTP_SUCCESS)
620     {
621         return eRTP_FALSE;
622     }
623 
624     return eRTP_TRUE;
625 }
626 
IMS_RtpSvc_SendRtcpByePacket(IN RTPSESSIONID hRtpSession)627 GLOBAL eRtp_Bool IMS_RtpSvc_SendRtcpByePacket(IN RTPSESSIONID hRtpSession)
628 {
629     RtpSession* pobjRtpSession = reinterpret_cast<RtpSession*>(hRtpSession);
630 
631     if (g_pobjRtpStack == nullptr ||
632             g_pobjRtpStack->isValidRtpSession(pobjRtpSession) == eRTP_FAILURE)
633         return eRTP_FALSE;
634 
635     pobjRtpSession->sendRtcpByePacket();
636     return eRTP_TRUE;
637 }
638 
IMS_RtpSvc_SendRtcpRtpFbPacket(IN RTPSESSIONID hRtpSession,IN RtpDt_UInt32 uiFbType,IN RtpDt_Char * pcBuff,IN RtpDt_UInt32 uiLen,IN RtpDt_UInt32 uiMediaSsrc)639 GLOBAL eRtp_Bool IMS_RtpSvc_SendRtcpRtpFbPacket(IN RTPSESSIONID hRtpSession,
640         IN RtpDt_UInt32 uiFbType, IN RtpDt_Char* pcBuff, IN RtpDt_UInt32 uiLen,
641         IN RtpDt_UInt32 uiMediaSsrc)
642 {
643     RtpSession* pobjRtpSession = reinterpret_cast<RtpSession*>(hRtpSession);
644     if (g_pobjRtpStack == nullptr ||
645             g_pobjRtpStack->isValidRtpSession(pobjRtpSession) == eRTP_FAILURE)
646         return eRTP_FALSE;
647 
648     pobjRtpSession->sendRtcpRtpFbPacket(uiFbType, pcBuff, uiLen, uiMediaSsrc);
649 
650     return eRTP_TRUE;
651 }
652 
IMS_RtpSvc_SendRtcpPayloadFbPacket(IN RTPSESSIONID hRtpSession,IN RtpDt_UInt32 uiFbType,IN RtpDt_Char * pcBuff,IN RtpDt_UInt32 uiLen,IN RtpDt_UInt32 uiMediaSsrc)653 GLOBAL eRtp_Bool IMS_RtpSvc_SendRtcpPayloadFbPacket(IN RTPSESSIONID hRtpSession,
654         IN RtpDt_UInt32 uiFbType, IN RtpDt_Char* pcBuff, IN RtpDt_UInt32 uiLen,
655         IN RtpDt_UInt32 uiMediaSsrc)
656 {
657     RtpSession* pobjRtpSession = reinterpret_cast<RtpSession*>(hRtpSession);
658     if (g_pobjRtpStack == nullptr ||
659             g_pobjRtpStack->isValidRtpSession(pobjRtpSession) == eRTP_FAILURE)
660         return eRTP_FALSE;
661 
662     pobjRtpSession->sendRtcpPayloadFbPacket(uiFbType, pcBuff, uiLen, uiMediaSsrc);
663 
664     return eRTP_TRUE;
665 }
666 
IMS_RtpSvc_ProcRtcpPacket(IN RtpServiceListener * pobjRtpServiceListener,IN RTPSESSIONID hRtpSession,IN RtpDt_UChar * pMsg,IN RtpDt_UInt16 uiMsgLength,IN RtpDt_Char * pcIpAddr,IN RtpDt_UInt32 uiRtcpPort,OUT RtpDt_UInt32 * uiPeerSsrc)667 GLOBAL eRtp_Bool IMS_RtpSvc_ProcRtcpPacket(IN RtpServiceListener* pobjRtpServiceListener,
668         IN RTPSESSIONID hRtpSession, IN RtpDt_UChar* pMsg, IN RtpDt_UInt16 uiMsgLength,
669         IN RtpDt_Char* pcIpAddr, IN RtpDt_UInt32 uiRtcpPort, OUT RtpDt_UInt32* uiPeerSsrc)
670 {
671     (RtpDt_Void) uiPeerSsrc;
672 
673     RtpSession* pobjRtpSession = reinterpret_cast<RtpSession*>(hRtpSession);
674 
675     if (g_pobjRtpStack == nullptr ||
676             g_pobjRtpStack->isValidRtpSession(pobjRtpSession) == eRTP_FAILURE)
677         return eRTP_FALSE;
678 
679     if (pMsg == nullptr || uiMsgLength == RTP_ZERO || pcIpAddr == nullptr)
680     {
681         return eRTP_FALSE;
682     }
683 
684     RtpBuffer objRmtAddr;
685     objRmtAddr.setBuffer(reinterpret_cast<RtpDt_UChar*>(pcIpAddr));
686     objRmtAddr.setLength(strlen(pcIpAddr) + 1);
687 
688     // decrypt RTCP message
689     RtpBuffer objRtcpBuf;
690     objRtcpBuf.setBufferInfo(uiMsgLength, pMsg);
691 
692     // process RTCP message
693     RtcpPacket objRtcpPkt;
694     eRTP_STATUS_CODE eProcRtcpSta =
695             pobjRtpSession->processRcvdRtcpPkt(&objRmtAddr, uiRtcpPort, &objRtcpBuf, &objRtcpPkt);
696 
697     // clean the data
698     objRtcpBuf.setBufferInfo(RTP_ZERO, nullptr);
699     objRmtAddr.setBufferInfo(RTP_ZERO, nullptr);
700     RtpBuffer objRtpPayload;
701     objRtpPayload.setBufferInfo(RTP_ZERO, nullptr);
702 
703     if (eProcRtcpSta != RTP_SUCCESS)
704     {
705         RTP_TRACE_WARNING("Rtcp packet processing is  failed", RTP_ZERO, RTP_ZERO);
706         return eRTP_FALSE;
707     }
708 
709     // inform to application
710     std::list<RtcpSrPacket*>& pobjSrList = objRtcpPkt.getSrPacketList();
711     if (pobjSrList.size() > RTP_ZERO)
712     {
713         tRtpSvc_IndicationFromStack stackInd = RTPSVC_RECEIVE_RTCP_SR_IND;
714         tNotifyReceiveRtcpSrInd stSrRtcpMsg;
715 
716         if (populateRcvdSrInfoFromStk(pobjSrList, &stSrRtcpMsg) == eRTP_TRUE)
717         {
718             pobjRtpServiceListener->OnPeerInd(stackInd, (RtpDt_Void*)&stSrRtcpMsg);
719         }
720 
721         RtpDt_UInt32 rttd = pobjRtpSession->getRTTD();
722         pobjRtpServiceListener->OnPeerRtcpComponents((RtpDt_Void*)&rttd);
723     }
724     else
725     {
726         std::list<RtcpRrPacket*>& pobjRrList = objRtcpPkt.getRrPacketList();
727         if (pobjRrList.size() > RTP_ZERO)
728         {
729             tRtpSvc_IndicationFromStack stackInd = RTPSVC_RECEIVE_RTCP_RR_IND;
730             tNotifyReceiveRtcpRrInd stRrRtcpMsg;
731 
732             if (populateRcvdRrInfoFromStk(pobjRrList, &stRrRtcpMsg) == eRTP_TRUE)
733             {
734                 pobjRtpServiceListener->OnPeerInd(stackInd, (RtpDt_Void*)&stRrRtcpMsg);
735             }
736 
737             RtpDt_UInt32 rttd = pobjRtpSession->getRTTD();
738             pobjRtpServiceListener->OnPeerRtcpComponents((RtpDt_Void*)&rttd);
739         }
740     }  // end else
741 
742     // process rtcp fb packet and inform to application
743     std::list<RtcpFbPacket*>& pobjFbList = objRtcpPkt.getFbPacketList();
744 
745     for (auto& pobjFbPkt : pobjFbList)
746     {
747         // get Fb packet data
748         if (pobjFbPkt == nullptr)
749         {
750             return eRTP_FALSE;
751         }
752         tRtpSvc_IndicationFromStack stackInd = RTPSVC_RECEIVE_RTCP_FB_IND;
753         if (pobjFbPkt->getRtcpHdrInfo()->getPacketType() == RTCP_PSFB)
754         {
755             stackInd = RTPSVC_RECEIVE_RTCP_PAYLOAD_FB_IND;
756         }
757         tRtpSvcIndSt_ReceiveRtcpFeedbackInd stFbRtcpMsg;
758         if (populateRcvdFbInfoFromStk(pobjFbPkt, &stFbRtcpMsg) == eRTP_TRUE)
759             pobjRtpServiceListener->OnPeerInd(stackInd, (RtpDt_Void*)&stFbRtcpMsg);
760     }  // pobjFbList End
761 
762     return eRTP_TRUE;
763 }
764 
IMS_RtpSvc_SendRtcpXrPacket(IN RTPSESSIONID hRtpSession,IN RtpDt_UChar * m_pBlockBuffer,IN RtpDt_UInt16 nblockLength)765 GLOBAL eRtp_Bool IMS_RtpSvc_SendRtcpXrPacket(
766         IN RTPSESSIONID hRtpSession, IN RtpDt_UChar* m_pBlockBuffer, IN RtpDt_UInt16 nblockLength)
767 {
768     RTP_TRACE_MESSAGE("IMS_RtpSvc_SendRtcpXrPacket", 0, 0);
769 
770     RtpSession* pobjRtpSession = reinterpret_cast<RtpSession*>(hRtpSession);
771     pobjRtpSession->sendRtcpXrPacket(m_pBlockBuffer, nblockLength);
772 
773     return eRTP_TRUE;
774 }
775 
IMS_RtpSvc_SetRtpContext(RTPSESSIONID hRtpSession,RtpDt_UInt32 ssrc,RtpDt_UInt32,RtpDt_UInt16 seqNumber)776 GLOBAL eRtp_Bool IMS_RtpSvc_SetRtpContext(RTPSESSIONID hRtpSession, RtpDt_UInt32 ssrc,
777         RtpDt_UInt32 /*timestamp*/, RtpDt_UInt16 seqNumber)
778 {
779     RTP_TRACE_MESSAGE("IMS_RtpSvc_SetRtpContext. ssrc:%d, sequenceNumber:%d", ssrc, seqNumber);
780     RtpSession* pobjRtpSession = reinterpret_cast<RtpSession*>(hRtpSession);
781     pobjRtpSession->setSequenceNumber(seqNumber);
782     return eRTP_TRUE;
783 }
784 
IMS_RtpSvc_GetRtpContext(RTPSESSIONID hRtpSession,RtpDt_UInt32 & ssrc,RtpDt_UInt32 &,RtpDt_UInt16 & seqNumber)785 GLOBAL eRtp_Bool IMS_RtpSvc_GetRtpContext(RTPSESSIONID hRtpSession, RtpDt_UInt32& ssrc,
786         RtpDt_UInt32& /*timestamp*/, RtpDt_UInt16& seqNumber)
787 {
788     RTP_TRACE_MESSAGE("IMS_RtpSvc_GetRtpContext. ssrc:%d, sequenceNumber:%d", ssrc, seqNumber);
789     RtpSession* pobjRtpSession = reinterpret_cast<RtpSession*>(hRtpSession);
790     ssrc = pobjRtpSession->getSsrc();
791     seqNumber = pobjRtpSession->getSequenceNumber();
792     return eRTP_TRUE;
793 }