SDRAngel  4.11.5
Developer docs for <a href="https://github.com/f4exb/sdrangel">SDRangel<\a>, an Open Source Qt5 / OpenGL 3.0+ SDR and signal analyzer frontend to various hardware.
rtcpcompoundpacket.cpp
Go to the documentation of this file.
1 /*
2 
3  This file is a part of JRTPLIB
4  Copyright (c) 1999-2017 Jori Liesenborgs
5 
6  Contact: jori.liesenborgs@gmail.com
7 
8  This library was developed at the Expertise Centre for Digital Media
9  (http://www.edm.uhasselt.be), a research center of the Hasselt University
10  (http://www.uhasselt.be). The library is based upon work done for
11  my thesis at the School for Knowledge Technology (Belgium/The Netherlands).
12 
13  Permission is hereby granted, free of charge, to any person obtaining a
14  copy of this software and associated documentation files (the "Software"),
15  to deal in the Software without restriction, including without limitation
16  the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  and/or sell copies of the Software, and to permit persons to whom the
18  Software is furnished to do so, subject to the following conditions:
19 
20  The above copyright notice and this permission notice shall be included
21  in all copies or substantial portions of the Software.
22 
23  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29  IN THE SOFTWARE.
30 
31  */
32 
33 #include "rtcpcompoundpacket.h"
34 #include "rtprawpacket.h"
35 #include "rtperrors.h"
36 #include "rtpstructs.h"
37 #include "rtpdefines.h"
38 #include "rtcpsrpacket.h"
39 #include "rtcprrpacket.h"
40 #include "rtcpsdespacket.h"
41 #include "rtcpbyepacket.h"
42 #include "rtcpapppacket.h"
43 #include "rtcpunknownpacket.h"
44 
45 namespace qrtplib
46 {
47 
49 {
50  compoundpacket = 0;
52  error = 0;
53 
54  if (rawpack.IsRTP())
55  {
57  return;
58  }
59 
60  uint8_t *data = rawpack.GetData();
61  std::size_t datalen = rawpack.GetDataLength();
62 
63  error = ParseData(data, datalen);
64 
65  if (error < 0) {
66  return;
67  }
68 
69  compoundpacket = rawpack.GetData();
71 
72  rtcppackit = rtcppacklist.begin();
73 }
74 
75 RTCPCompoundPacket::RTCPCompoundPacket(uint8_t *packet, std::size_t packetlen)
76 {
77  compoundpacket = 0;
79 
80  error = ParseData(packet, packetlen);
81 
82  if (error < 0) {
83  return;
84  }
85 
86  compoundpacket = packet;
87  compoundpacketlength = packetlen;
88 
89  rtcppackit = rtcppacklist.begin();
90 }
91 
93 {
94  compoundpacket = 0;
96  error = 0;
97 }
98 
99 int RTCPCompoundPacket::ParseData(uint8_t *data, std::size_t datalen)
100 {
101  bool first;
102 
103  if (datalen < sizeof(RTCPCommonHeader))
105 
106  first = true;
107 
108  do
109  {
110  RTCPCommonHeader *rtcphdr;
111  std::size_t length;
112 
113  rtcphdr = (RTCPCommonHeader *) data;
114  if (rtcphdr->version != RTP_VERSION) // check version
115  {
116  ClearPacketList();
118  }
119  if (first)
120  {
121  // Check if first packet is SR or RR
122 
123  first = false;
124  if (!(rtcphdr->packettype == RTP_RTCPTYPE_SR || rtcphdr->packettype == RTP_RTCPTYPE_RR))
125  {
126  ClearPacketList();
128  }
129  }
130 
131  length = (std::size_t) m_endian.qToHost(rtcphdr->length);
132  length++;
133  length *= sizeof(uint32_t);
134 
135  if (length > datalen) // invalid length field
136  {
137  ClearPacketList();
139  }
140 
141  if (rtcphdr->padding)
142  {
143  // check if it's the last packet
144  if (length != datalen)
145  {
146  ClearPacketList();
148  }
149  }
150 
151  RTCPPacket *p;
152 
153  switch (rtcphdr->packettype)
154  {
155  case RTP_RTCPTYPE_SR:
156  p = new RTCPSRPacket(data, length);
157  break;
158  case RTP_RTCPTYPE_RR:
159  p = new RTCPRRPacket(data, length);
160  break;
161  case RTP_RTCPTYPE_SDES:
162  p = new RTCPSDESPacket(data, length);
163  break;
164  case RTP_RTCPTYPE_BYE:
165  p = new RTCPBYEPacket(data, length);
166  break;
167  case RTP_RTCPTYPE_APP:
168  p = new RTCPAPPPacket(data, length);
169  break;
170  default:
171  p = new RTCPUnknownPacket(data, length);
172  }
173 
174  rtcppacklist.push_back(p);
175 
176  datalen -= length;
177  data += length;
178  } while (datalen >= (std::size_t) sizeof(RTCPCommonHeader));
179 
180  if (datalen != 0) // some remaining bytes
181  {
182  ClearPacketList();
184  }
185  return 0;
186 }
187 
189 {
190  ClearPacketList();
191 }
192 
194 {
195  std::list<RTCPPacket *>::const_iterator it;
196 
197  for (it = rtcppacklist.begin(); it != rtcppacklist.end(); it++) {
198  delete *it;
199  }
200 
201  rtcppacklist.clear();
202  rtcppackit = rtcppacklist.begin();
203 }
204 
205 } // end namespace
206 
#define RTP_RTCPTYPE_BYE
Definition: rtpdefines.h:53
T qToHost(const T &x) const
Definition: rtpendian.h:27
unsigned int uint32_t
Definition: rtptypes_win.h:46
#define RTP_RTCPTYPE_RR
Definition: rtpdefines.h:51
std::list< RTCPPacket * > rtcppacklist
unsigned char uint8_t
Definition: rtptypes_win.h:42
#define RTP_RTCPTYPE_SR
Definition: rtpdefines.h:50
std::list< RTCPPacket * >::const_iterator rtcppackit
std::size_t GetDataLength() const
Definition: rtprawpacket.h:70
int ParseData(uint8_t *packet, std::size_t len)
#define RTP_VERSION
Definition: rtpdefines.h:37
#define RTP_RTCPTYPE_APP
Definition: rtpdefines.h:54
bool IsRTP() const
Definition: rtprawpacket.h:88
#define RTP_RTCPTYPE_SDES
Definition: rtpdefines.h:52
#define ERR_RTP_RTCPCOMPOUND_INVALIDPACKET
Definition: rtperrors.h:82