summaryrefslogtreecommitdiff
path: root/service/src/com/android/telephony/imsmedia/lib/libimsmedia/protocol/rtp/core/RtcpSrPacket.cpp
blob: 3dd1c99c7e755867aa09e5855c4d846a1da489bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/**
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <RtcpSrPacket.h>
#include <RtpTrace.h>

/*********************************************************
 * Function name        : RtcpSrPacket
 * Description          : Constructor
 * Return type          : None
 * Argument             : None
 * Preconditions        : None
 * Side Effects            : None
 ********************************************************/
RtcpSrPacket::RtcpSrPacket() :
        m_uiRtpTimestamp(RTP_ZERO),
        m_uiSendPktCount(RTP_ZERO),
        m_uiSendOctCount(RTP_ZERO)
{
    memset(&m_stNtpTimestamp, RTP_ZERO, sizeof(tRTP_NTP_TIME));
}

/*********************************************************
 * Function name        : ~RtcpSrPacket
 * Description          : Destructor
 * Return type          : None
 * Argument             : None
 * Preconditions        : None
 * Side Effects            : None
 ********************************************************/
RtcpSrPacket::~RtcpSrPacket() {}

RtpDt_Void RtcpSrPacket::setRtcpHdrInfo(RtcpHeader& rtcpHeader)
{
    RtcpHeader* pobjRtcpHdr = m_objRrPkt.getRtcpHdrInfo();
    *pobjRtcpHdr = rtcpHeader;
}

RtcpHeader* RtcpSrPacket::getRtcpHdrInfo()
{
    return m_objRrPkt.getRtcpHdrInfo();
}

/*********************************************************
 * Function name        : getRrPktInfo
 * Description          : get method for m_objRrPkt
 * Return type          : RtcpRrPacket*
 * Argument             : None
 * Preconditions        : None
 * Side Effects            : None
 ********************************************************/
RtcpRrPacket* RtcpSrPacket::getRrPktInfo()
{
    return &m_objRrPkt;
}

/*********************************************************
 * Function name        : getNtpTime
 * Description          : get method for m_stNtpTimestamp
 * Return type          : tRTP_NTP_TIME*
 * Argument             : None
 * Preconditions        : None
 * Side Effects            : None
 ********************************************************/
tRTP_NTP_TIME* RtcpSrPacket::getNtpTime()
{
    return &m_stNtpTimestamp;
}

/*********************************************************
 * Function name        : setRtpTimestamp
 * Description          : set method for m_uiRtpTimestamp
 * Return type          : RtpDt_Void
 * Argument             : RtpDt_UInt32 : In
 *                            RTP timestamp
 * Preconditions        : None
 * Side Effects            : None
 ********************************************************/
RtpDt_Void RtcpSrPacket::setRtpTimestamp(IN RtpDt_UInt32 uiRtpTimestamp)
{
    m_uiRtpTimestamp = uiRtpTimestamp;
}

/*********************************************************
 * Function name        : getRtpTimestamp
 * Description          : get method for m_uiRtpTimestamp
 * Return type          : RtpDt_UInt32
 * Argument             : None
 * Preconditions        : None
 * Side Effects            : None
 ********************************************************/
RtpDt_UInt32 RtcpSrPacket::getRtpTimestamp()
{
    return m_uiRtpTimestamp;
}

/*********************************************************
 * Function name        : setSendPktCount
 * Description          : set method for m_uiSendPktCount
 * Return type          : RtpDt_Void
 * Argument             : RtpDt_UInt32 : In
 * Preconditions        : None
 * Side Effects            : None
 ********************************************************/
RtpDt_Void RtcpSrPacket::setSendPktCount(IN RtpDt_UInt32 uiPktCount)
{
    m_uiSendPktCount = uiPktCount;
}

/*********************************************************
 * Function name        : getSendPktCount
 * Description          : get method for m_uiSendPktCount
 * Return type          : RtpDt_UInt32
 * Argument             : None
 * Preconditions        : None
 * Side Effects            : None
 ********************************************************/
RtpDt_UInt32 RtcpSrPacket::getSendPktCount()
{
    return m_uiSendPktCount;
}

/*********************************************************
 * Function name        : setSendOctetCount
 * Description          : set method for m_uiSendOctCount
 * Return type          : RtpDt_Void
 * Argument             : RtpDt_UInt32 : In
 * Preconditions        : None
 * Side Effects            : None
 ********************************************************/
RtpDt_Void RtcpSrPacket::setSendOctetCount(IN RtpDt_UInt32 uiOctetCount)
{
    m_uiSendOctCount = uiOctetCount;
}

/*********************************************************
 * Function name        : getSendOctetCount
 * Description          : get method for m_uiSendOctCount
 * Return type          : RtpDt_UInt32
 * Argument             : None
 * Preconditions        : None
 * Side Effects            : None
 ********************************************************/
RtpDt_UInt32 RtcpSrPacket::getSendOctetCount()
{
    return m_uiSendOctCount;
}

/*********************************************************
 * Function name        : decodeSrPacket
 * Description          : Decodes and stores the information of the RTCP SR packet
 * Return type          : eRtp_Bool : eRTP_SUCCESS on successful decoding
 * Argument             : RtpDt_UChar* : In
 * Argument                : RtpDt_UInt32 : In
 *                            RTCP SR packet length
 * Preconditions        : None
 * Side Effects            : None
 ********************************************************/
eRTP_STATUS_CODE RtcpSrPacket::decodeSrPacket(
        IN RtpDt_UChar* pucSrPktBuf, IN RtpDt_UInt16 usSrPktLen, IN RtpDt_UInt16 usExtHdrLen)
{
    /*
            0                   1                   2                   3
            0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
           +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    header |V=2|P|    RC   |   PT=SR=200   |             length            |
           +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
           |                         SSRC of sender                        |
           +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
    sender |              NTP timestamp, most significant word             |
    info   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
           |             NTP timestamp, least significant word             |
           +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
           |                         RTP timestamp                         |
           +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
           |                     sender's packet count                     |
           +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
           |                      sender's octet count                     |
           +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
    report |                 SSRC_1 (SSRC of first source)                 |
    block  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      1    | fraction lost |       cumulative number of packets lost       |
           +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
           |           extended highest sequence number received           |
           +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
           |                      interarrival jitter                      |
           +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
           |                         last SR (LSR)                         |
           +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
           |                   delay since last SR (DLSR)                  |
           +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
    report |                 SSRC_2 (SSRC of second source)                |
    block  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      2    :                               ...                             :
           +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
           |                  profile-specific extensions                  |
           +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

    */

    if (pucSrPktBuf == nullptr || usSrPktLen < RTCP_SR_PACKET_LENGTH)
        return RTP_FAILURE;

    // NTP timestamp most significant word
    m_stNtpTimestamp.m_uiNtpHigh32Bits =
            RtpOsUtil::Ntohl(*(reinterpret_cast<RtpDt_UInt32*>(pucSrPktBuf)));
    pucSrPktBuf = pucSrPktBuf + RTP_WORD_SIZE;
    usSrPktLen = usSrPktLen - RTP_WORD_SIZE;

    // NTP timestamp least significant word
    m_stNtpTimestamp.m_uiNtpLow32Bits =
            RtpOsUtil::Ntohl(*(reinterpret_cast<RtpDt_UInt32*>(pucSrPktBuf)));
    pucSrPktBuf = pucSrPktBuf + RTP_WORD_SIZE;
    usSrPktLen = usSrPktLen - RTP_WORD_SIZE;

    // RTP timestamp
    m_uiRtpTimestamp = RtpOsUtil::Ntohl(*(reinterpret_cast<RtpDt_UInt32*>(pucSrPktBuf)));
    pucSrPktBuf = pucSrPktBuf + RTP_WORD_SIZE;
    usSrPktLen = usSrPktLen - RTP_WORD_SIZE;

    // sender's packet count
    m_uiSendPktCount = RtpOsUtil::Ntohl(*(reinterpret_cast<RtpDt_UInt32*>(pucSrPktBuf)));
    pucSrPktBuf = pucSrPktBuf + RTP_WORD_SIZE;
    usSrPktLen = usSrPktLen - RTP_WORD_SIZE;

    // sender's octet count
    m_uiSendOctCount = RtpOsUtil::Ntohl(*(reinterpret_cast<RtpDt_UInt32*>(pucSrPktBuf)));
    pucSrPktBuf = pucSrPktBuf + RTP_WORD_SIZE;
    usSrPktLen = usSrPktLen - RTP_WORD_SIZE;

    // decode report block
    eRTP_STATUS_CODE eDecodeRes = RTP_FAILURE;
    eDecodeRes = m_objRrPkt.decodeRrPacket(pucSrPktBuf, usSrPktLen, usExtHdrLen);
    if (eDecodeRes != RTP_SUCCESS)
    {
        RTP_TRACE_WARNING(
                "RtcpPacket::decodeRtcpPacket, RR packet Decoding Error[%d]", eDecodeRes, RTP_ZERO);
        return eDecodeRes;
    }

    return RTP_SUCCESS;
}  // decodeSrPacket

/*********************************************************
 * Function name        : formSrPacket
 * Description          : Performs the encoding of the RTCP SR packet.
 * Return type          : eRtp_Bool : eRTP_SUCCESS on successful encoding
 * Argument             : RtpBuffer* : Out
 * Preconditions        : None
 * Side Effects            : None
 ********************************************************/
eRTP_STATUS_CODE RtcpSrPacket::formSrPacket(OUT RtpBuffer* pobjRtcpPktBuf)
{
    /*
        0                   1                   2                   3
        0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
header |V=2|P|    RC   |   PT=SR=200   |             length            |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                         SSRC of sender                        |
       +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
sender |              NTP timestamp, most significant word             |
info   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |             NTP timestamp, least significant word             |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                         RTP timestamp                         |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                     sender's packet count                     |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                      sender's octet count                     |
       +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
report |                 SSRC_1 (SSRC of first source)                 |
block  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  1    | fraction lost |       cumulative number of packets lost       |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |           extended highest sequence number received           |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                      interarrival jitter                      |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                         last SR (LSR)                         |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       |                   delay since last SR (DLSR)                  |
       +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
report |                 SSRC_2 (SSRC of second source)                |
block  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2    :                               ...                             :
       +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
       |                  profile-specific extensions                  |
       +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

    */

    RTP_TRACE_MESSAGE("formSrPacket", 0, 0);
    RtpDt_UInt32 uiCurPos = pobjRtcpPktBuf->getLength();
    RtpDt_UChar* pucBuffer = pobjRtcpPktBuf->getBuffer();
    uiCurPos = uiCurPos + RTCP_FIXED_HDR_LEN;
    pucBuffer = pucBuffer + RTCP_FIXED_HDR_LEN;

    // get RTCP header information
    RtcpHeader* pobjRtcpHdr = m_objRrPkt.getRtcpHdrInfo();

    // encode m_stNtpTimestamp
    *(reinterpret_cast<RtpDt_UInt32*>(pucBuffer)) =
            RtpOsUtil::Ntohl(m_stNtpTimestamp.m_uiNtpHigh32Bits);
    pucBuffer = pucBuffer + RTP_WORD_SIZE;
    uiCurPos = uiCurPos + RTP_WORD_SIZE;

    *(reinterpret_cast<RtpDt_UInt32*>(pucBuffer)) =
            RtpOsUtil::Ntohl(m_stNtpTimestamp.m_uiNtpLow32Bits);
    pucBuffer = pucBuffer + RTP_WORD_SIZE;
    uiCurPos = uiCurPos + RTP_WORD_SIZE;

    // encode m_uiRtpTimestamp
    *(reinterpret_cast<RtpDt_UInt32*>(pucBuffer)) = RtpOsUtil::Ntohl(m_uiRtpTimestamp);
    pucBuffer = pucBuffer + RTP_WORD_SIZE;
    uiCurPos = uiCurPos + RTP_WORD_SIZE;

    // encode m_uiSendPktCount
    *(reinterpret_cast<RtpDt_UInt32*>(pucBuffer)) = RtpOsUtil::Ntohl(m_uiSendPktCount);
    pucBuffer = pucBuffer + RTP_WORD_SIZE;
    uiCurPos = uiCurPos + RTP_WORD_SIZE;

    // encode m_uiSendOctCount
    *(reinterpret_cast<RtpDt_UInt32*>(pucBuffer)) = RtpOsUtil::Ntohl(m_uiSendOctCount);
    uiCurPos = uiCurPos + RTP_WORD_SIZE;

    // encode report blocks
    eRTP_STATUS_CODE eEncodeRes = RTP_FAILURE;

    pobjRtcpPktBuf->setLength(uiCurPos);
    eEncodeRes = m_objRrPkt.formRrPacket(pobjRtcpPktBuf, eRTP_FALSE);
    if (eEncodeRes != RTP_SUCCESS)
    {
        RTP_TRACE_WARNING("[formSrPacket], Report Block Encoding Error", RTP_ZERO, RTP_ZERO);
        return eEncodeRes;
    }

    // get length of the SR packet
    RtpDt_UInt32 uiSrPktLen = pobjRtcpPktBuf->getLength();
#ifdef ENABLE_PADDING
    pucBuffer = pobjRtcpPktBuf->getBuffer();
    pucBuffer = pucBuffer + uiSrPktLen;
    RtpDt_UInt32 uiPadLen = uiSrPktLen % RTP_WORD_SIZE;
    if (uiPadLen != RTP_ZERO)
    {
        uiSrPktLen = uiSrPktLen + uiPadLen;
        uiPadLen = RTP_WORD_SIZE - uiPadLen;
        memset(pucBuffer, RTP_ZERO, uiPadLen);
        pucBuffer = pucBuffer + uiPadLen;
        pucBuffer = pucBuffer - RTP_ONE;
        *(reinterpret_cast<RtpDt_UChar*>(pucBuffer)) = (RtpDt_UChar)uiPadLen;

        // set pad bit in header
        pobjRtcpHdr->setPadding();
        // set length in header
        pobjRtcpHdr->setLength(uiSrPktLen);
    }
    else
#endif
    {
        // set length in header
        pobjRtcpHdr->setLength(uiSrPktLen);
    }

    // set compound RTCP packet position to ZERO.
    pobjRtcpPktBuf->setLength(RTP_ZERO);
    // form rtcp header
    pobjRtcpHdr->formRtcpHeader(pobjRtcpPktBuf);
    // set the actual position of the RTCP compound packet
    pobjRtcpPktBuf->setLength(uiSrPktLen);

    return RTP_SUCCESS;
}  // formSrPacket