• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <RtpHeader.h>
18 #include <RtpTrace.h>
19 #include <RtpError.h>
20 
RtpHeader()21 RtpHeader::RtpHeader() :
22         m_ucVersion(RTP_ZERO),
23         m_ucPadding(RTP_ZERO),
24         m_ucExtension(RTP_ZERO),
25         m_ucCsrcCount(RTP_ZERO),
26         m_uiCsrcList(std::list<RtpDt_UInt32>()),
27         m_ucMarker(RTP_ZERO),
28         m_ucPayloadType(RTP_ZERO),
29         m_usSequenceNumber(RTP_ZERO),
30         m_uiTimestamp(RTP_ZERO),
31         m_uiSsrc(RTP_ZERO)
32 
33 {
34 }
35 
~RtpHeader()36 RtpHeader::~RtpHeader()
37 {
38     m_uiCsrcList.clear();
39 }
40 
setVersion(IN RtpDt_UChar ucVersion)41 RtpDt_Void RtpHeader::setVersion(IN RtpDt_UChar ucVersion)
42 {
43     m_ucVersion = ucVersion;
44 }
45 
getVersion()46 RtpDt_UChar RtpHeader::getVersion()
47 {
48     return m_ucVersion;
49 }
50 
setPadding()51 RtpDt_Void RtpHeader::setPadding()
52 {
53     m_ucPadding = RTP_ONE;
54 }
55 
getPadding()56 RtpDt_UChar RtpHeader::getPadding()
57 {
58     return m_ucPadding;
59 }
60 
setExtension(RtpDt_UChar ext)61 RtpDt_Void RtpHeader::setExtension(RtpDt_UChar ext)
62 {
63     m_ucExtension = ext;
64 }
65 
getExtension()66 RtpDt_UChar RtpHeader::getExtension()
67 {
68     return m_ucExtension;
69 }
70 
setCsrcCount(IN RtpDt_UChar ucCsrcCount)71 RtpDt_Void RtpHeader::setCsrcCount(IN RtpDt_UChar ucCsrcCount)
72 {
73     m_ucCsrcCount = ucCsrcCount;
74 }
75 
getCsrcCount()76 RtpDt_UChar RtpHeader::getCsrcCount()
77 {
78     return m_ucCsrcCount;
79 }
80 
getCsrcList()81 std::list<RtpDt_UInt32>& RtpHeader::getCsrcList()
82 {
83     return m_uiCsrcList;
84 }
85 
addElementToCsrcList(IN RtpDt_UInt32 uiCsrc)86 RtpDt_Void RtpHeader::addElementToCsrcList(IN RtpDt_UInt32 uiCsrc)
87 {
88     // append uiCsrc into list.
89     m_uiCsrcList.push_back(uiCsrc);
90     RTP_TRACE_MESSAGE("CsrcList[%d] = %d", m_uiCsrcList.size(), uiCsrc);
91     return;
92 }
93 
setMarker()94 RtpDt_Void RtpHeader::setMarker()
95 {
96     m_ucMarker = RTP_ONE;
97 }
98 
getMarker()99 RtpDt_UChar RtpHeader::getMarker()
100 {
101     return m_ucMarker;
102 }
103 
setPayloadType(IN RtpDt_UChar ucPayloadType)104 RtpDt_Void RtpHeader::setPayloadType(IN RtpDt_UChar ucPayloadType)
105 {
106     m_ucPayloadType = ucPayloadType;
107 }
108 
getPayloadType()109 RtpDt_UChar RtpHeader::getPayloadType()
110 {
111     return m_ucPayloadType;
112 }
113 
setSequenceNumber(IN RtpDt_UInt16 usSequenceNumber)114 RtpDt_Void RtpHeader::setSequenceNumber(IN RtpDt_UInt16 usSequenceNumber)
115 {
116     m_usSequenceNumber = usSequenceNumber;
117 }
118 
getSequenceNumber()119 RtpDt_UInt16 RtpHeader::getSequenceNumber()
120 {
121     return m_usSequenceNumber;
122 }
123 
setRtpTimestamp(IN RtpDt_UInt32 uiTimestamp)124 RtpDt_Void RtpHeader::setRtpTimestamp(IN RtpDt_UInt32 uiTimestamp)
125 {
126     m_uiTimestamp = uiTimestamp;
127 }
128 
getRtpTimestamp()129 RtpDt_UInt32 RtpHeader::getRtpTimestamp()
130 {
131     return m_uiTimestamp;
132 }
133 
setRtpSsrc(IN RtpDt_UInt32 uiSsrc)134 RtpDt_Void RtpHeader::setRtpSsrc(IN RtpDt_UInt32 uiSsrc)
135 {
136     m_uiSsrc = uiSsrc;
137 }
138 
getRtpSsrc()139 RtpDt_UInt32 RtpHeader::getRtpSsrc()
140 {
141     return m_uiSsrc;
142 }
143 
formHeader(IN RtpBuffer * pobjRtpPktBuf)144 eRtp_Bool RtpHeader::formHeader(IN RtpBuffer* pobjRtpPktBuf)
145 {
146     RtpDt_UInt16 usTmpData = RTP_ZERO;
147     RtpDt_UInt16 usUtlData = RTP_ZERO;
148 
149     // get RtpBuffer data
150     RtpDt_UChar* pucRtpHeaderBuffer = pobjRtpPktBuf->getBuffer();
151 
152     // version 2 bits
153     RTP_FORM_HDR_UTL(usUtlData, m_ucVersion, RTP_VER_SHIFT_VAL, usTmpData);
154 
155     // padding 1 bit
156     RTP_FORM_HDR_UTL(usUtlData, m_ucPadding, RTP_PAD_SHIFT_VAL, usTmpData);
157 
158     // extension 1 bit
159     RTP_FORM_HDR_UTL(usUtlData, m_ucExtension, RTP_EXT_SHIFT_VAL, usTmpData);
160 
161     // CC. CSRC count 4 bits.
162     RTP_FORM_HDR_UTL(usUtlData, m_ucCsrcCount, RTP_CC_SHIFT_VAL, usTmpData);
163 
164     // Marker. 1 bit
165     RTP_FORM_HDR_UTL(usUtlData, m_ucMarker, RTP_MARK_SHIFT_VAL, usTmpData);
166 
167     // payload type. 7 bits
168     RTP_FORM_HDR_UTL(usUtlData, m_ucPayloadType, RTP_PLTYPE_SHIFT_VAL, usTmpData);
169 
170     RtpDt_UInt32 uiByte4Data = usTmpData;
171     uiByte4Data = uiByte4Data << RTP_SIXTEEN;
172 
173     // sequence number. 16 bits
174     uiByte4Data = uiByte4Data | m_usSequenceNumber;
175 
176     *(reinterpret_cast<RtpDt_UInt32*>(pucRtpHeaderBuffer)) = RtpOsUtil::Ntohl(uiByte4Data);
177     pucRtpHeaderBuffer = pucRtpHeaderBuffer + RTP_WORD_SIZE;
178 
179     // time stamp
180     *(reinterpret_cast<RtpDt_UInt32*>(pucRtpHeaderBuffer)) = RtpOsUtil::Ntohl(m_uiTimestamp);
181     pucRtpHeaderBuffer = pucRtpHeaderBuffer + RTP_WORD_SIZE;
182 
183     // ssrc
184     *(reinterpret_cast<RtpDt_UInt32*>(pucRtpHeaderBuffer)) = RtpOsUtil::Ntohl(m_uiSsrc);
185     pucRtpHeaderBuffer = pucRtpHeaderBuffer + RTP_WORD_SIZE;
186 
187     RtpDt_UInt32 uiBufLen = RTP_FIXED_HDR_LEN;
188 
189     // csrc list
190     for (auto csrc : m_uiCsrcList)
191     {
192         *(reinterpret_cast<RtpDt_UInt32*>(pucRtpHeaderBuffer)) = RtpOsUtil::Ntohl(csrc);
193         pucRtpHeaderBuffer = pucRtpHeaderBuffer + RTP_WORD_SIZE;
194     }
195     RtpDt_UInt16 usSize = m_uiCsrcList.size();
196 
197     uiBufLen += (RTP_WORD_SIZE * usSize);
198 
199     pobjRtpPktBuf->setLength(uiBufLen);
200 
201     return eRTP_TRUE;
202 }
203 
decodeHeader(IN RtpBuffer * pobjRtpPktBuf,OUT RtpDt_UInt32 & uiBufPos)204 eRtp_Bool RtpHeader::decodeHeader(IN RtpBuffer* pobjRtpPktBuf, OUT RtpDt_UInt32& uiBufPos)
205 {
206     // get RtpBuffer data
207     RtpDt_UChar* pucRtpHeaderBuffer = pobjRtpPktBuf->getBuffer();
208     RtpDt_UInt32 uiRtpBufferLength = pobjRtpPktBuf->getLength();
209 
210     if (uiRtpBufferLength < RTP_FIXED_HDR_LEN)
211     {
212         RTP_TRACE_ERROR("Invalid Rtp packet: Expected minimum Rtp packet length[%d], Received[%d]",
213                 RTP_FIXED_HDR_LEN, uiRtpBufferLength);
214         return eRTP_FALSE;
215     }
216 
217     RtpDt_UInt32 uiByte4Data =
218             RtpOsUtil::Ntohl(*(reinterpret_cast<RtpDt_UInt32*>(pucRtpHeaderBuffer)));
219     pucRtpHeaderBuffer = pucRtpHeaderBuffer + RTP_FOUR;
220     uiBufPos = uiBufPos + RTP_WORD_SIZE;
221 
222     // version 2 bits
223     RtpDt_UInt16 usUtl2Data = (RtpDt_UInt16)(uiByte4Data >> RTP_SIXTEEN);
224     m_ucVersion = (RtpDt_UChar)(usUtl2Data >> RTP_VER_SHIFT_VAL);
225 
226     // padding 1 bit
227     m_ucPadding = (RtpDt_UChar)((usUtl2Data >> RTP_PAD_SHIFT_VAL) & RTP_HEX_1_BIT_MAX);
228 
229     // extension 1 bit
230     m_ucExtension = (RtpDt_UChar)((usUtl2Data >> RTP_EXT_SHIFT_VAL) & RTP_HEX_1_BIT_MAX);
231 
232     // CC. CSRC count 4 bits.
233     m_ucCsrcCount = (RtpDt_UChar)((usUtl2Data >> RTP_CC_SHIFT_VAL) & RTP_HEX_4_BIT_MAX);
234 
235     // Marker. 1 bit
236     usUtl2Data = usUtl2Data & RTP_HEX_8_BIT_MAX;
237     m_ucMarker = (RtpDt_UChar)((usUtl2Data >> RTP_MARK_SHIFT_VAL) & RTP_HEX_1_BIT_MAX);
238 
239     // payload type. 7 bits
240     m_ucPayloadType = (RtpDt_UChar)((usUtl2Data)&RTP_HEX_7_BIT_MAX);
241 
242     // sequence number. 16 bits
243     m_usSequenceNumber = (RtpDt_UInt16)(uiByte4Data & RTP_HEX_16_BIT_MAX);
244 
245     // timestamp
246     m_uiTimestamp = RtpOsUtil::Ntohl(*(reinterpret_cast<RtpDt_UInt32*>(pucRtpHeaderBuffer)));
247     pucRtpHeaderBuffer = pucRtpHeaderBuffer + RTP_FOUR;
248     uiBufPos = uiBufPos + RTP_WORD_SIZE;
249 
250     // Synchronization source
251     m_uiSsrc = RtpOsUtil::Ntohl(*(reinterpret_cast<RtpDt_UInt32*>(pucRtpHeaderBuffer)));
252     pucRtpHeaderBuffer = pucRtpHeaderBuffer + RTP_FOUR;
253     uiBufPos = uiBufPos + RTP_WORD_SIZE;
254 
255     if (m_ucCsrcCount != 0)
256     {
257         RtpDt_UInt32 uiExpectedLength = RTP_FIXED_HDR_LEN + (m_ucCsrcCount * RTP_WORD_SIZE);
258         if (uiRtpBufferLength < uiExpectedLength)
259         {
260             RTP_TRACE_ERROR(
261                     "Invalid Rtp packet: Expected minimum Rtp packet length[%d], but received[%d]",
262                     uiExpectedLength, uiRtpBufferLength);
263             return eRTP_FALSE;
264         }
265 
266         // csrc list
267         for (RtpDt_UInt32 usCsrcIdx = RTP_ZERO; usCsrcIdx < m_ucCsrcCount; usCsrcIdx++)
268         {
269             addElementToCsrcList(
270                     RtpOsUtil::Ntohl(*(reinterpret_cast<RtpDt_UInt32*>(pucRtpHeaderBuffer))));
271             pucRtpHeaderBuffer = pucRtpHeaderBuffer + RTP_FOUR;
272             uiBufPos = uiBufPos + RTP_WORD_SIZE;
273         }
274     }
275 
276     return eRTP_TRUE;
277 }
278