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 <RtpStackUtil.h>
18
RtpStackUtil()19 RtpStackUtil::RtpStackUtil() {}
20
~RtpStackUtil()21 RtpStackUtil::~RtpStackUtil() {}
22
getSequenceNumber(IN RtpDt_UChar * pucRtpHdrBuf)23 RtpDt_UInt16 RtpStackUtil::getSequenceNumber(IN RtpDt_UChar* pucRtpHdrBuf)
24 {
25 if (pucRtpHdrBuf == nullptr)
26 {
27 return RTP_ZERO;
28 }
29
30 RtpDt_UInt32 uiByte4Data = RTP_ZERO;
31 RtpDt_UInt16 usSeqNum = RTP_ZERO;
32
33 uiByte4Data = RtpOsUtil::Ntohl(*(reinterpret_cast<RtpDt_UInt32*>(pucRtpHdrBuf)));
34 usSeqNum = (RtpDt_UInt16)(uiByte4Data & RTP_HEX_16_BIT_MAX);
35
36 return usSeqNum;
37 }
38
getRtpSsrc(IN RtpDt_UChar * pucRtpBuf)39 RtpDt_UInt32 RtpStackUtil::getRtpSsrc(IN RtpDt_UChar* pucRtpBuf)
40 {
41 if (pucRtpBuf == nullptr)
42 {
43 return RTP_ZERO;
44 }
45
46 RtpDt_UInt32 uiByte4Data = RTP_ZERO;
47 pucRtpBuf = pucRtpBuf + RTP_EIGHT;
48
49 uiByte4Data = RtpOsUtil::Ntohl(*(reinterpret_cast<RtpDt_UInt32*>(pucRtpBuf)));
50 return uiByte4Data;
51 }
52
getRtcpSsrc(IN RtpDt_UChar * pucRtcpBuf)53 RtpDt_UInt32 RtpStackUtil::getRtcpSsrc(IN RtpDt_UChar* pucRtcpBuf)
54 {
55 if (pucRtcpBuf == nullptr)
56 {
57 return RTP_ZERO;
58 }
59 pucRtcpBuf = pucRtcpBuf + RTP_WORD_SIZE;
60
61 RtpDt_UInt32 uiByte4Data = RtpOsUtil::Ntohl(*(reinterpret_cast<RtpDt_UInt32*>(pucRtcpBuf)));
62 return uiByte4Data;
63 }
64
generateNewSsrc(IN RtpDt_UInt32 uiTermNum)65 RtpDt_UInt32 RtpStackUtil::generateNewSsrc(IN RtpDt_UInt32 uiTermNum)
66 {
67 RtpDt_UInt32 uiTmpRand = RTP_ZERO;
68
69 uiTmpRand = RtpOsUtil::Rand();
70 uiTmpRand = uiTmpRand << RTP_EIGHT;
71 uiTmpRand = uiTmpRand & RTP_SSRC_GEN_UTL;
72 uiTmpRand = uiTmpRand | uiTermNum;
73
74 return uiTmpRand;
75 }
76
getMidFourOctets(IN tRTP_NTP_TIME * pstNtpTs)77 RtpDt_UInt32 RtpStackUtil::getMidFourOctets(IN tRTP_NTP_TIME* pstNtpTs)
78 {
79 if (pstNtpTs == nullptr)
80 {
81 return RTP_ZERO;
82 }
83
84 RtpDt_UInt32 uiNtpTs = pstNtpTs->m_uiNtpHigh32Bits;
85 uiNtpTs = uiNtpTs << RTP_BYTE2_BIT_SIZE;
86 RtpDt_UInt32 uiNtpLowTs = pstNtpTs->m_uiNtpLow32Bits;
87 uiNtpLowTs = uiNtpLowTs >> RTP_BYTE2_BIT_SIZE;
88 uiNtpTs = uiNtpTs | uiNtpLowTs;
89 return uiNtpTs;
90 }
91
calcRtpTimestamp(IN RtpDt_UInt32 uiPrevRtpTs,IN tRTP_NTP_TIME * pstCurNtpTs,IN tRTP_NTP_TIME * pstPrevNtpTs,IN RtpDt_UInt32 uiSamplingRate)92 RtpDt_UInt32 RtpStackUtil::calcRtpTimestamp(IN RtpDt_UInt32 uiPrevRtpTs,
93 IN tRTP_NTP_TIME* pstCurNtpTs, IN tRTP_NTP_TIME* pstPrevNtpTs,
94 IN RtpDt_UInt32 uiSamplingRate)
95 {
96 if (pstCurNtpTs == nullptr || pstPrevNtpTs == nullptr)
97 {
98 return RTP_ZERO;
99 }
100
101 RtpDt_Int32 iTimeDiffHigh32Bits = RTP_ZERO;
102 RtpDt_Int32 iTimeDiffLow32Bits = RTP_ZERO;
103
104 if ((RTP_ZERO != pstPrevNtpTs->m_uiNtpHigh32Bits) ||
105 (RTP_ZERO != pstPrevNtpTs->m_uiNtpLow32Bits))
106 {
107 iTimeDiffHigh32Bits = pstCurNtpTs->m_uiNtpHigh32Bits - pstPrevNtpTs->m_uiNtpHigh32Bits;
108 iTimeDiffLow32Bits = (pstCurNtpTs->m_uiNtpLow32Bits / 4294UL) -
109 (pstPrevNtpTs->m_uiNtpLow32Bits / 4294UL);
110 }
111 else
112 {
113 iTimeDiffHigh32Bits = RTP_ZERO;
114 iTimeDiffLow32Bits = RTP_ZERO;
115 }
116
117 // calc iTimeDiff in millisec
118 RtpDt_Int32 iTimeDiff = (iTimeDiffHigh32Bits * 1000 * 1000) + iTimeDiffLow32Bits;
119
120 /* the time diff high bit is in seconds and
121 the time diff low bit is in micro seconds */
122
123 RtpDt_UInt32 uiNewRtpTs = RTP_ZERO;
124
125 if (RTP_ZERO == iTimeDiff)
126 {
127 uiNewRtpTs = uiPrevRtpTs;
128 }
129 else
130 {
131 RtpDt_Int32 temp = uiSamplingRate / 1000;
132 uiNewRtpTs = uiPrevRtpTs + (temp * iTimeDiff / 1000);
133 }
134 return uiNewRtpTs;
135 }
136