CSL  5.2
CslRtpSession.cpp
Go to the documentation of this file.
1 ///
2 /// CslRtpSession.cpp -- Contains a subclass of RTPSession, which is the core of RTP
3 ///
4 /// This code is common to both RtpSender and RtpReceiver, as these two units have a lot of
5 /// redundancy betwen them.
6 ///
7 
8 #include "CslRtpSession.h"
9 
10 using namespace std;
11 using namespace csl;
12 
13 CslRtpSession::CslRtpSession(unsigned numChans, unsigned bufferFrames)
14  : mRtpBuffer(numChans, bufferFrames), // Default to 1 mono channel being set,
15  mTmpBuffer(1, CGestalt::maxBufferFrames()) // Init the encapsulated buffer size (hard-coded to mono, for now)
16 {
17 // RTPSession::RTPSession();
18  mTmpBuffer.allocateMonoBuffers(); // Allocate the mTmpBuffer buffers
19 }
20 
21 // ---------------------------------------------------------------
23  mTmpBuffer.freeMonoBuffers(); // Free the temp buffer, and delete it
24 }
25 /*
26 // ---------------------------------------------------------------
27 void CslRtpSession::OnPollThreadStep() // If there are packets to read (or it's time to send an RTCP packet)
28 {
29  BeginDataAccess(); // Call this when we access the RTP session, to prevent interference by other access
30 
31  // check incoming packets
32  if (GotoFirstSourceWithData())
33  {
34  do // Do the following for all waiting packets
35  {
36  RTPPacket *pack;
37  RTPSourceData *srcdat;
38 
39  srcdat = GetCurrentSourceInfo();
40 
41  while ((pack = GetNextPacket()) != NULL)
42  {
43  ProcessRTPPacket(*srcdat,*pack); // Process this packet if it exists
44  delete pack;
45  }
46  } while (GotoNextSourceWithData());
47  }
48  EndDataAccess(); // Release the RTP resources to be used elsewhere
49 
50  RTCPCompoundPacketBuilder
51 
52 }
53 */
54 // ---------------------------------------------------------------
55 // OnRTCPCompoundPacket parses the compound packet to display all of the data
56 // available onto the console
57 //
58 void CslRtpSession::OnRTCPCompoundPacket(RTCPCompoundPacket *pack, const RTPTime &receivetime, const RTPAddress *senderaddress)
59 {
60  RTCPPacket * rtcpPacket;
61 
62  // I know there should be a way to do this with polymorphism, but I can't figure it out...
63  RTCPSRPacket * srPacket; RTCPRRPacket * rrPacket; RTCPSDESPacket * sdesPacket;
64  RTCPBYEPacket * byePacket; RTCPAPPPacket * appPacket; RTCPUnknownPacket * unknownPacket;
65 
66  BeginDataAccess();
67  cout << "RTCP Compound Packet (" << pack->GetCompoundPacketLength() << " bytes) \n";
68  unsigned i = 0, chunkCnt = 0;
69  bool avail = false;
70  char * text;
71  pack->GotoFirstPacket(); // Reset the packet iterator
72  while (0 != (rtcpPacket = pack->GetNextPacket())) // For all packets...
73  {
74  i++;
75  cout << " 1: RTCP type " << rtcpPacket->GetPacketType() << " "; // Display the info
76  switch (rtcpPacket->GetPacketType()) {
77  case RTCPPacket::SR:
78  cout << "(SR) ";
79  srPacket = (RTCPSRPacket*) rtcpPacket;
80  cout << "SenderSSRC: " << srPacket->GetSenderSSRC() << " ";
81  cout << "RTPtime: " << srPacket->GetRTPTimestamp() << "\n";
82  cout << " PackCnt: " << srPacket->GetSenderPacketCount() << " ";
83  cout << "OctetCnt: " << srPacket->GetSenderOctetCount() << "\n";
84  for (int j = 0; j < srPacket->GetReceptionReportCount(); j++) {
85  cout << " RR# " << i << ": ";
86  cout << "SSRC: " << srPacket->GetSSRC(i) << " ";
87  cout << "%Lost: " << 100*srPacket->GetFractionLost(i) << " ";
88  cout << "#Lost: " << srPacket->GetLostPacketCount(i) << " ";
89  cout << "HighestSeq: " << srPacket->GetExtendedHighestSequenceNumber(i) << " ";
90  cout << "Jitter: " << srPacket->GetJitter(i) << " ";
91  cout << "LSR: " << srPacket->GetLSR(i) << " ";
92  cout << "DLSR: " << srPacket->GetDLSR(i) << "\n";
93  }
94  break;
95  case RTCPPacket::RR:
96  cout << "(RR) ";
97  rrPacket = (RTCPRRPacket*) rtcpPacket;
98  cout << "SenderSSRC: " << rrPacket->GetSenderSSRC() << " ";
99  cout << "ReportCnt: " << rrPacket->GetReceptionReportCount() << "\n";
100  for (int j = 0; j < rrPacket->GetReceptionReportCount(); j++) {
101  cout << " RR# " << i << ": ";
102  cout << "SSRC: " << rrPacket->GetSSRC(i) << " ";
103  cout << "%Lost: " << 100*rrPacket->GetFractionLost(i) << " ";
104  cout << "#Lost: " << rrPacket->GetLostPacketCount(i) << "\n";
105  cout << " HighestSeq: " << rrPacket->GetExtendedHighestSequenceNumber(i) << " ";
106  cout << "Jitter: " << rrPacket->GetJitter(i) << "\n";
107  cout << " LSR: " << rrPacket->GetLSR(i) << " ";
108  cout << "DLSR: " << rrPacket->GetDLSR(i) << "\n";
109  }
110  break;
111  case RTCPPacket::SDES:
112  sdesPacket = (RTCPSDESPacket*) rtcpPacket;
113  cout << "(SDES) ";
114  cout << sdesPacket->GetChunkCount() << " chunks \n";
115  avail = sdesPacket->GotoFirstChunk();
116  chunkCnt = 0;
117  while (avail) {
118  chunkCnt++;
119  cout << " (Chunk #" << chunkCnt << ") ";
120  cout << " SSRC: " << sdesPacket->GetChunkSSRC() << "\n";
121  avail = sdesPacket->GotoNextChunk();
122  }
123  avail = sdesPacket->GotoFirstItem();
124  chunkCnt = 0;
125  while (avail) {
126  chunkCnt++;
127  cout << " (Item #" << chunkCnt << ") ";
128  cout << " Type: " << sdesPacket->GetItemType() << " ";
129  switch (sdesPacket->GetItemType()) {
130  case RTCPSDESPacket::None: cout << "(None) "; break;
131  case RTCPSDESPacket::CNAME: cout << "(CNAME) "; break;
132  case RTCPSDESPacket::NAME: cout << "(NAME) "; break;
133  case RTCPSDESPacket::EMAIL: cout << "(EMAIL) "; break;
134  case RTCPSDESPacket::PHONE: cout << "(PHONE) "; break;
135  case RTCPSDESPacket::LOC: cout << "(LOC) "; break;
136  case RTCPSDESPacket::TOOL: cout << "(TOOL) "; break;
137  case RTCPSDESPacket::NOTE: cout << "(NOTE) "; break;
138  case RTCPSDESPacket::PRIV: cout << "(PRIV) "; break;
139  case RTCPSDESPacket::Unknown: cout << "(Unknown) "; break;
140  }
141  cout << "(" << sdesPacket->GetItemLength() << " bytes) ";
142  cout << (char *) sdesPacket->GetItemData() << "\n";
143  avail = sdesPacket->GotoNextItem();
144  }
145  break;
146  case RTCPPacket::BYE:
147  cout << "(BYE) \n";
148  break;
149  case RTCPPacket::APP:
150  cout << "(APP) \n";
151  break;
152  case RTCPPacket::Unknown:
153  cout << "(Unknown) \n";
154  break;
155  }
156  EndDataAccess();
157  };
158 }
159 
160 
161 void CslRtpSession::OnRTPPacket(RTPPacket * pack, const RTPTime &receivetime, const RTPAddress *senderaddress)
162 {
163  Interleaver interleaver;
164  // You can inspect the packet and the source's info here
165 // cout << "Packet # " << rtppack.GetExtendedSequenceNumber() << "\n"; // " from SSRC " << srcdat.GetSSRC() << "\n";
166 // cout << rtppack.GetPayloadLength() << " bytes of data with timestamp " << rtppack.GetTimestamp() << "\n";
167 // cout << "With a type of " << rtppack.GetPayloadType() << "\n";
168 
169  BeginDataAccess();
170  unsigned payloadLength = pack->GetPayloadLength() / sizeof(short);
171  RTPTime diffTime(receivetime.CurrentTime()); // packetTime now contains the network latency of this packet
172  diffTime -= receivetime;
173 
174 /* cout << "inf: " << srcdat.SR_Prev_GetRTPTimestamp() << " "
175  << srcdat.SR_GetRTPTimestamp() << " "
176  << srcdat.RR_GetReceiveTime().GetDouble()
177  << srcdat.RR_Prev_GetReceiveTime().GetDouble()
178  << srcdat.INF_GetRoundtripTime().GetDouble() "\n";
179 */
180  if ((pack->GetSequenceNumber() % 100) == 0) {
181  cout << "Net latency: ";
182  cout << diffTime.GetDouble() * 1000. << "ms "
183  // << packetTime.GetNTPTime().GetMSW() << "."
184  // << packetTime.GetNTPTime().GetLSW() << " "
185  << "Seq# " << pack->GetExtendedSequenceNumber() << " "
186  << "Timestamp " << pack->GetTimestamp() << "\n";
187  }
188 
189 
190 // short * payload = (short *) rtppack.GetPayloadData(); // Get the payload data into a short array
191  mTmpBuffer.setSizeOnly(1,payloadLength); // Set the temp buffer's payload length
192 
193  interleaver.deinterleave(mTmpBuffer, (short *) pack->GetPayloadData(), payloadLength,1); // Deinterleave the payload into the buffer
194  EndDataAccess();
195  mRtpBuffer.writeBuffer(mTmpBuffer, 0); // Write the temp Buffer into the Ring Buffer
196 }
197 /*
198 // ---------------------------------------------------------------
199 void CslRtpSession::ProcessRTPPacket(const RTPSourceData &srcdat,const RTPPacket &rtppack)
200 {
201  Interleaver interleaver;
202  // You can inspect the packet and the source's info here
203 // cout << "Packet # " << rtppack.GetExtendedSequenceNumber() << "\n"; // " from SSRC " << srcdat.GetSSRC() << "\n";
204 // cout << rtppack.GetPayloadLength() << " bytes of data with timestamp " << rtppack.GetTimestamp() << "\n";
205 // cout << "With a type of " << rtppack.GetPayloadType() << "\n";
206 
207  RTPTime packetTime(rtppack.GetReceiveTime());
208  RTPTime diffTime(packetTime.CurrentTime()); // packetTime now contains the network latency of this packet
209  diffTime -= packetTime;
210 
211 // cout << "inf: " << srcdat.SR_Prev_GetRTPTimestamp() << " "
212 // << srcdat.SR_GetRTPTimestamp() << " "
213 // << srcdat.RR_GetReceiveTime().GetDouble()
214 // << srcdat.RR_Prev_GetReceiveTime().GetDouble()
215 // << srcdat.INF_GetRoundtripTime().GetDouble() "\n";
216 
217  unsigned payloadLength = rtppack.GetPayloadLength() / sizeof(short);
218  cout << "Net latency: ";
219  cout << diffTime.GetDouble() << "us "
220 // << packetTime.GetNTPTime().GetMSW() << "."
221 // << packetTime.GetNTPTime().GetLSW() << " "
222  << "Seq# " << rtppack.GetSequenceNumber() << " "
223  << "Timestamp " << rtppack.GetTimestamp() << "\n";
224 
225 
226 // short * payload = (short *) rtppack.GetPayloadData(); // Get the payload data into a short array
227  tBuf->setSizeOnly(1,payloadLength); // Set the temp buffer's payload length
228 
229  interleaver.deinterleave(*tBuf, (short *) rtppack.GetPayloadData(), payloadLength,1); // Deinterleave the payload into the buffer
230  buffer->writeBuffer(*tBuf, 0); // Write the temp Buffer into the Ring Buffer
231 
232 } */
233 
234 void CslRtpSession::OnSSRCCollision(RTPSourceData *srcdat,const RTPAddress *senderaddress,bool isrtp)
235 {
236  cout << "SSRC Collision!" << "\n";
237 }
238 void CslRtpSession::OnCNAMECollision(RTPSourceData *srcdat,const RTPAddress *senderaddress,
239  const u_int8_t *cname,size_t cnamelength)
240 {
241  cout << "CNAME Collision: " << (char *) cname << "\n";
242  return;
243 }
244 void CslRtpSession::OnNewSource(RTPSourceData *srcdat)
245 {
246  cout << "New source: SSRC: " << srcdat->GetSSRC() << " ";
247  cout << "RTP: " << srcdat->GetRTPDataAddress() << " ";
248  cout << "RTCP: " << srcdat->GetRTCPDataAddress() << "\n";
249 // if (!srcdat->IsOwnSSRC())
250 // this->AddDestination(srcdat->GetSSRC());
251  return;
252 }
253 void CslRtpSession::OnRemoveSource(RTPSourceData *srcdat)
254 {
255  cout << "Source removed: SSRC: " << srcdat->GetSSRC() << " ";
256  cout << "RTP: " << srcdat->GetRTPDataAddress() << " ";
257  cout << "RTCP: " << srcdat->GetRTCPDataAddress() << "\n";
258  return;
259 }
260 void CslRtpSession::OnTimeout(RTPSourceData *srcdat)
261 {
262  cout << "Timeout: SSRC: " << srcdat->GetSSRC() << " ";
263  cout << "RTP: " << srcdat->GetRTPDataAddress() << " ";
264  cout << "RTCP: " << srcdat->GetRTCPDataAddress() << "\n";
265  return;
266 }
267 void CslRtpSession::OnBYETimeout(RTPSourceData *srcdat)
268 {
269  cout << "BYE Timeout: SSRC: " << srcdat->GetSSRC() << " ";
270  cout << "RTP: " << srcdat->GetRTPDataAddress() << " ";
271  cout << "RTCP: " << srcdat->GetRTCPDataAddress() << "\n";
272  return;
273 }
274 void CslRtpSession::OnAPPPacket(RTCPAPPPacket *apppacket,const RTPTime &receivetime,
275  const RTPAddress *senderaddress)
276 {
277  cout << "APP Packet: SSRC: " << apppacket->GetSSRC() << " ";
278  cout << (char *) apppacket->GetName() << ": ";
279  cout << "(" << apppacket->GetAPPDataLength() << " bytes)\n";
280  return;
281 }
282 void CslRtpSession::OnUnknownPacketType(RTCPPacket *rtcppack,const RTPTime &receivetime,
283  const RTPAddress *senderaddress)
284 {
285  cout << "Unknown Packet Type: ";
286  cout << "(" << rtcppack->GetPacketLength() << " bytes)\n";
287  return;
288 }
289 void CslRtpSession::OnUnknownPacketFormat(RTCPPacket *rtcppack,const RTPTime &receivetime,
290  const RTPAddress *senderaddress)
291 {
292  cout << "Unknown Packet Type: ";
293  cout << "(" << rtcppack->GetPacketLength() << " bytes)\n";
294  return;
295 }
296 void CslRtpSession::OnNoteTimeout(RTPSourceData *srcdat)
297 {
298  cout << "Note Timeout: SSRC: " << srcdat->GetSSRC() << " ";
299  cout << "RTP: " << srcdat->GetRTPDataAddress() << " ";
300  cout << "RTCP: " << srcdat->GetRTCPDataAddress() << "\n";
301  return;
302 }
303 void CslRtpSession::OnBYEPacket(RTPSourceData *srcdat)
304 {
305  cout << "BYE Packet: SSRC: " << srcdat->GetSSRC() << " ";
306  cout << "RTP: " << srcdat->GetRTPDataAddress() << " ";
307  cout << "RTCP: " << srcdat->GetRTCPDataAddress() << "\n";
308  return;
309 }