rtpsourcedata.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. This file is a part of JRTPLIB
  3. Copyright (c) 1999-2011 Jori Liesenborgs
  4. Contact: jori.liesenborgs@gmail.com
  5. This library was developed at the Expertise Centre for Digital Media
  6. (http://www.edm.uhasselt.be), a research center of the Hasselt University
  7. (http://www.uhasselt.be). The library is based upon work done for
  8. my thesis at the School for Knowledge Technology (Belgium/The Netherlands).
  9. Permission is hereby granted, free of charge, to any person obtaining a
  10. copy of this software and associated documentation files (the "Software"),
  11. to deal in the Software without restriction, including without limitation
  12. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. and/or sell copies of the Software, and to permit persons to whom the
  14. Software is furnished to do so, subject to the following conditions:
  15. The above copyright notice and this permission notice shall be included
  16. in all copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  23. IN THE SOFTWARE.
  24. */
  25. #include "rtpsourcedata.h"
  26. #include "rtpdefines.h"
  27. #include "rtpaddress.h"
  28. #include "rtpmemorymanager.h"
  29. #if ! (defined(WIN32) || defined(_WIN32_WCE))
  30. #include <netinet/in.h>
  31. #endif // WIN32
  32. #ifdef RTPDEBUG
  33. #include <iostream>
  34. #include <string>
  35. #endif // RTPDEBUG
  36. #include "rtpdebug.h"
  37. #define ACCEPTPACKETCODE \
  38. *accept = true; \
  39. \
  40. sentdata = true; \
  41. packetsreceived++; \
  42. numnewpackets++; \
  43. \
  44. if (pack->GetExtendedSequenceNumber() == 0) \
  45. { \
  46. baseseqnr = 0x0000FFFF; \
  47. numcycles = 0x00010000; \
  48. } \
  49. else \
  50. baseseqnr = pack->GetExtendedSequenceNumber() - 1; \
  51. \
  52. exthighseqnr = baseseqnr + 1; \
  53. prevpacktime = receivetime; \
  54. prevexthighseqnr = baseseqnr; \
  55. savedextseqnr = baseseqnr; \
  56. \
  57. pack->SetExtendedSequenceNumber(exthighseqnr); \
  58. \
  59. prevtimestamp = pack->GetTimestamp(); \
  60. lastmsgtime = prevpacktime; \
  61. if (!ownpacket) /* for own packet, this value is set on an outgoing packet */ \
  62. lastrtptime = prevpacktime;
  63. namespace jrtplib
  64. {
  65. void RTPSourceStats::ProcessPacket(RTPPacket *pack,const RTPTime &receivetime,double tsunit,
  66. bool ownpacket,bool *accept,bool applyprobation,bool *onprobation)
  67. {
  68. // Note that the sequence number in the RTP packet is still just the
  69. // 16 bit number contained in the RTP header
  70. *onprobation = false;
  71. if (!sentdata) // no valid packets received yet
  72. {
  73. #ifdef RTP_SUPPORT_PROBATION
  74. if (applyprobation)
  75. {
  76. bool acceptpack = false;
  77. if (probation)
  78. {
  79. uint16_t pseq;
  80. uint32_t pseq2;
  81. pseq = prevseqnr;
  82. pseq++;
  83. pseq2 = (uint32_t)pseq;
  84. if (pseq2 == pack->GetExtendedSequenceNumber()) // ok, its the next expected packet
  85. {
  86. prevseqnr = (uint16_t)pack->GetExtendedSequenceNumber();
  87. probation--;
  88. if (probation == 0) // probation over
  89. acceptpack = true;
  90. else
  91. *onprobation = true;
  92. }
  93. else // not next packet
  94. {
  95. probation = RTP_PROBATIONCOUNT;
  96. prevseqnr = (uint16_t)pack->GetExtendedSequenceNumber();
  97. *onprobation = true;
  98. }
  99. }
  100. else // first packet received with this SSRC ID, start probation
  101. {
  102. probation = RTP_PROBATIONCOUNT;
  103. prevseqnr = (uint16_t)pack->GetExtendedSequenceNumber();
  104. *onprobation = true;
  105. }
  106. if (acceptpack)
  107. {
  108. ACCEPTPACKETCODE
  109. }
  110. else
  111. {
  112. *accept = false;
  113. lastmsgtime = receivetime;
  114. }
  115. }
  116. else // No probation
  117. {
  118. ACCEPTPACKETCODE
  119. }
  120. #else // No compiled-in probation support
  121. ACCEPTPACKETCODE
  122. #endif // RTP_SUPPORT_PROBATION
  123. }
  124. else // already got packets
  125. {
  126. uint16_t maxseq16;
  127. uint32_t extseqnr;
  128. // Adjust max extended sequence number and set extende seq nr of packet
  129. *accept = true;
  130. packetsreceived++;
  131. numnewpackets++;
  132. maxseq16 = (uint16_t)(exthighseqnr&0x0000FFFF);
  133. if (pack->GetExtendedSequenceNumber() >= maxseq16)
  134. {
  135. extseqnr = numcycles+pack->GetExtendedSequenceNumber();
  136. exthighseqnr = extseqnr;
  137. }
  138. else
  139. {
  140. uint16_t dif1,dif2;
  141. dif1 = ((uint16_t)pack->GetExtendedSequenceNumber());
  142. dif1 -= maxseq16;
  143. dif2 = maxseq16;
  144. dif2 -= ((uint16_t)pack->GetExtendedSequenceNumber());
  145. if (dif1 < dif2)
  146. {
  147. numcycles += 0x00010000;
  148. extseqnr = numcycles+pack->GetExtendedSequenceNumber();
  149. exthighseqnr = extseqnr;
  150. }
  151. else
  152. extseqnr = numcycles+pack->GetExtendedSequenceNumber();
  153. }
  154. pack->SetExtendedSequenceNumber(extseqnr);
  155. // Calculate jitter
  156. if (tsunit > 0)
  157. {
  158. #if 0
  159. RTPTime curtime = receivetime;
  160. double diffts1,diffts2,diff;
  161. curtime -= prevpacktime;
  162. diffts1 = curtime.GetDouble()/tsunit;
  163. diffts2 = (double)pack->GetTimestamp() - (double)prevtimestamp;
  164. diff = diffts1 - diffts2;
  165. if (diff < 0)
  166. diff = -diff;
  167. diff -= djitter;
  168. diff /= 16.0;
  169. djitter += diff;
  170. jitter = (uint32_t)djitter;
  171. #else
  172. RTPTime curtime = receivetime;
  173. double diffts1,diffts2,diff;
  174. uint32_t curts = pack->GetTimestamp();
  175. curtime -= prevpacktime;
  176. diffts1 = curtime.GetDouble()/tsunit;
  177. if (curts > prevtimestamp)
  178. {
  179. uint32_t unsigneddiff = curts - prevtimestamp;
  180. if (unsigneddiff < 0x10000000) // okay, curts realy is larger than prevtimestamp
  181. diffts2 = (double)unsigneddiff;
  182. else
  183. {
  184. // wraparound occurred and curts is actually smaller than prevtimestamp
  185. unsigneddiff = -unsigneddiff; // to get the actual difference (in absolute value)
  186. diffts2 = -((double)unsigneddiff);
  187. }
  188. }
  189. else if (curts < prevtimestamp)
  190. {
  191. uint32_t unsigneddiff = prevtimestamp - curts;
  192. if (unsigneddiff < 0x10000000) // okay, curts really is smaller than prevtimestamp
  193. diffts2 = -((double)unsigneddiff); // negative since we actually need curts-prevtimestamp
  194. else
  195. {
  196. // wraparound occurred and curts is actually larger than prevtimestamp
  197. unsigneddiff = -unsigneddiff; // to get the actual difference (in absolute value)
  198. diffts2 = (double)unsigneddiff;
  199. }
  200. }
  201. else
  202. diffts2 = 0;
  203. diff = diffts1 - diffts2;
  204. if (diff < 0)
  205. diff = -diff;
  206. diff -= djitter;
  207. diff /= 16.0;
  208. djitter += diff;
  209. jitter = (uint32_t)djitter;
  210. #endif
  211. }
  212. else
  213. {
  214. djitter = 0;
  215. jitter = 0;
  216. }
  217. prevpacktime = receivetime;
  218. prevtimestamp = pack->GetTimestamp();
  219. lastmsgtime = prevpacktime;
  220. if (!ownpacket) // for own packet, this value is set on an outgoing packet
  221. lastrtptime = prevpacktime;
  222. }
  223. }
  224. RTPSourceData::RTPSourceData(uint32_t s, RTPMemoryManager *mgr) : RTPMemoryObject(mgr),SDESinf(mgr),byetime(0,0)
  225. {
  226. ssrc = s;
  227. issender = false;
  228. iscsrc = false;
  229. timestampunit = -1;
  230. receivedbye = false;
  231. byereason = 0;
  232. byereasonlen = 0;
  233. rtpaddr = 0;
  234. rtcpaddr = 0;
  235. ownssrc = false;
  236. validated = false;
  237. processedinrtcp = false;
  238. isrtpaddrset = false;
  239. isrtcpaddrset = false;
  240. }
  241. RTPSourceData::~RTPSourceData()
  242. {
  243. FlushPackets();
  244. if (byereason)
  245. RTPDeleteByteArray(byereason,GetMemoryManager());
  246. if (rtpaddr)
  247. RTPDelete(rtpaddr,GetMemoryManager());
  248. if (rtcpaddr)
  249. RTPDelete(rtcpaddr,GetMemoryManager());
  250. }
  251. double RTPSourceData::INF_GetEstimatedTimestampUnit() const
  252. {
  253. if (!SRprevinf.HasInfo())
  254. return -1.0;
  255. RTPTime t1 = RTPTime(SRinf.GetNTPTimestamp());
  256. RTPTime t2 = RTPTime(SRprevinf.GetNTPTimestamp());
  257. if ((t1.GetSeconds() == 0 && t1.GetMicroSeconds() == 0) ||
  258. (t2.GetSeconds() == 0 && t2.GetMicroSeconds() == 0)) // one of the times couldn't be calculated
  259. return -1.0;
  260. if (t1 <= t2)
  261. return -1.0;
  262. t1 -= t2; // get the time difference
  263. uint32_t tsdiff = SRinf.GetRTPTimestamp()-SRprevinf.GetRTPTimestamp();
  264. return (t1.GetDouble()/((double)tsdiff));
  265. }
  266. RTPTime RTPSourceData::INF_GetRoundtripTime() const
  267. {
  268. if (!RRinf.HasInfo())
  269. return RTPTime(0,0);
  270. if (RRinf.GetDelaySinceLastSR() == 0 && RRinf.GetLastSRTimestamp() == 0)
  271. return RTPTime(0,0);
  272. RTPNTPTime recvtime = RRinf.GetReceiveTime().GetNTPTime();
  273. uint32_t rtt = ((recvtime.GetMSW()&0xFFFF)<<16)|((recvtime.GetLSW()>>16)&0xFFFF);
  274. rtt -= RRinf.GetLastSRTimestamp();
  275. rtt -= RRinf.GetDelaySinceLastSR();
  276. double drtt = (((double)rtt)/65536.0);
  277. return RTPTime(drtt);
  278. }
  279. #ifdef RTPDEBUG
  280. void RTPSourceData::Dump()
  281. {
  282. std::cout << "Source data for SSRC: " << ssrc << std::endl;
  283. std::cout << " Active: " << ((IsActive())?"Yes":"No") << std::endl;
  284. std::cout << " Sender: " << ((issender)?"Yes":"No") << std::endl;
  285. std::cout << " CSRC: " << ((iscsrc)?"Yes":"No") << std::endl;
  286. std::cout << " Received bye: " << ((receivedbye)?"Yes":"No") << std::endl;
  287. std::cout << " ProcessedInRTCP: " << ((processedinrtcp)?"Yes":"No") << std::endl;
  288. std::cout << " Timestamp unit: " << timestampunit << std::endl;
  289. std::cout << " RTP address: ";
  290. if (!isrtpaddrset)
  291. std::cout << "Not set" << std::endl;
  292. else
  293. {
  294. if (rtpaddr == 0)
  295. std::cout << "Own session" << std::endl;
  296. else
  297. std::cout << rtpaddr->GetAddressString() << std::endl;
  298. }
  299. std::cout << " RTCP address: ";
  300. if (!isrtcpaddrset)
  301. std::cout << "Not set" << std::endl;
  302. else
  303. {
  304. if (rtcpaddr == 0)
  305. std::cout << "Own session" << std::endl;
  306. else
  307. std::cout << rtcpaddr->GetAddressString() << std::endl;
  308. }
  309. if (SRinf.HasInfo())
  310. {
  311. if (!SRprevinf.HasInfo())
  312. {
  313. std::cout << " SR Info:" << std::endl;
  314. std::cout << " NTP timestamp: " << SRinf.GetNTPTimestamp().GetMSW() << ":" << SRinf.GetNTPTimestamp().GetLSW() << std::endl;
  315. std::cout << " RTP timestamp: " << SRinf.GetRTPTimestamp() << std::endl;
  316. std::cout << " Packet count: " << SRinf.GetPacketCount() << std::endl;
  317. std::cout << " Octet count: " << SRinf.GetByteCount() << std::endl;
  318. std::cout << " Receive time: " << SRinf.GetReceiveTime().GetSeconds() << std::endl;
  319. }
  320. else
  321. {
  322. std::cout << " SR Info:" << std::endl;
  323. std::cout << " NTP timestamp: " << SRinf.GetNTPTimestamp().GetMSW() << ":" << SRinf.GetNTPTimestamp().GetLSW()
  324. << " (" << SRprevinf.GetNTPTimestamp().GetMSW() << ":" << SRprevinf.GetNTPTimestamp().GetLSW() << ")" << std::endl;
  325. std::cout << " RTP timestamp: " << SRinf.GetRTPTimestamp()
  326. << " (" << SRprevinf.GetRTPTimestamp() << ")" << std::endl;
  327. std::cout << " Packet count: " << SRinf.GetPacketCount()
  328. << " (" << SRprevinf.GetPacketCount() << ")" << std::endl;
  329. std::cout << " Octet count: " << SRinf.GetByteCount()
  330. << " (" << SRprevinf.GetByteCount() <<")" << std::endl;
  331. std::cout << " Receive time: " << SRinf.GetReceiveTime().GetSeconds()
  332. << " (" << SRprevinf.GetReceiveTime().GetSeconds() << ")" << std::endl;
  333. }
  334. }
  335. if (RRinf.HasInfo())
  336. {
  337. if (!RRprevinf.HasInfo())
  338. {
  339. std::cout << " RR Info:" << std::endl;
  340. std::cout << " Fraction lost: " << RRinf.GetFractionLost() << std::endl;
  341. std::cout << " Packets lost: " << RRinf.GetPacketsLost() << std::endl;
  342. std::cout << " Ext.High.Seq: " << RRinf.GetExtendedHighestSequenceNumber() << std::endl;
  343. std::cout << " Jitter: " << RRinf.GetJitter() << std::endl;
  344. std::cout << " LSR: " << RRinf.GetLastSRTimestamp() << std::endl;
  345. std::cout << " DLSR: " << RRinf.GetDelaySinceLastSR() << std::endl;
  346. std::cout << " Receive time: " << RRinf.GetReceiveTime().GetSeconds() << std::endl;
  347. }
  348. else
  349. {
  350. std::cout << " RR Info:" << std::endl;
  351. std::cout << " Fraction lost: " << RRinf.GetFractionLost()
  352. << " (" << RRprevinf.GetFractionLost() << ")" << std::endl;
  353. std::cout << " Packets lost: " << RRinf.GetPacketsLost()
  354. << " (" << RRprevinf.GetPacketsLost() << ")" << std::endl;
  355. std::cout << " Ext.High.Seq: " << RRinf.GetExtendedHighestSequenceNumber()
  356. << " (" << RRprevinf.GetExtendedHighestSequenceNumber() << ")" << std::endl;
  357. std::cout << " Jitter: " << RRinf.GetJitter()
  358. << " (" << RRprevinf.GetJitter() << ")" << std::endl;
  359. std::cout << " LSR: " << RRinf.GetLastSRTimestamp()
  360. << " (" << RRprevinf.GetLastSRTimestamp() << ")" << std::endl;
  361. std::cout << " DLSR: " << RRinf.GetDelaySinceLastSR()
  362. << " (" << RRprevinf.GetDelaySinceLastSR() << ")" << std::endl;
  363. std::cout << " Receive time: " << RRinf.GetReceiveTime().GetSeconds()
  364. << " (" << RRprevinf.GetReceiveTime().GetSeconds() <<")" << std::endl;
  365. }
  366. }
  367. std::cout << " Stats:" << std::endl;
  368. std::cout << " Sent data: " << ((stats.HasSentData())?"Yes":"No") << std::endl;
  369. std::cout << " Packets received: " << stats.GetNumPacketsReceived() << std::endl;
  370. std::cout << " Seq. base: " << stats.GetBaseSequenceNumber() << std::endl;
  371. std::cout << " Ext.High.Seq: " << stats.GetExtendedHighestSequenceNumber() << std::endl;
  372. std::cout << " Jitter: " << stats.GetJitter() << std::endl;
  373. std::cout << " New packets: " << stats.GetNumPacketsReceivedInInterval() << std::endl;
  374. std::cout << " Saved seq. nr.: " << stats.GetSavedExtendedSequenceNumber() << std::endl;
  375. std::cout << " RTT: " << INF_GetRoundtripTime().GetDouble() << " seconds" << std::endl;
  376. if (INF_GetEstimatedTimestampUnit() > 0)
  377. std::cout << " Estimated: " << (1.0/INF_GetEstimatedTimestampUnit()) << " samples per second" << std::endl;
  378. std::cout << " SDES Info:" << std::endl;
  379. size_t len;
  380. char str[1024];
  381. uint8_t *val;
  382. if ((val = SDESinf.GetCNAME(&len)) != 0)
  383. {
  384. memcpy(str,val,len);
  385. str[len] = 0;
  386. std::cout << " CNAME: " << std::string(str) << std::endl;
  387. }
  388. if ((val = SDESinf.GetName(&len)) != 0)
  389. {
  390. memcpy(str,val,len);
  391. str[len] = 0;
  392. std::cout << " Name: " << std::string(str) << std::endl;
  393. }
  394. if ((val = SDESinf.GetEMail(&len)) != 0)
  395. {
  396. memcpy(str,val,len);
  397. str[len] = 0;
  398. std::cout << " EMail: " << std::string(str) << std::endl;
  399. }
  400. if ((val = SDESinf.GetPhone(&len)) != 0)
  401. {
  402. memcpy(str,val,len);
  403. str[len] = 0;
  404. std::cout << " phone: " << std::string(str) << std::endl;
  405. }
  406. if ((val = SDESinf.GetLocation(&len)) != 0)
  407. {
  408. memcpy(str,val,len);
  409. str[len] = 0;
  410. std::cout << " Location: " << std::string(str) << std::endl;
  411. }
  412. if ((val = SDESinf.GetTool(&len)) != 0)
  413. {
  414. memcpy(str,val,len);
  415. str[len] = 0;
  416. std::cout << " Tool: " << std::string(str) << std::endl;
  417. }
  418. if ((val = SDESinf.GetNote(&len)) != 0)
  419. {
  420. memcpy(str,val,len);
  421. str[len] = 0;
  422. std::cout << " Note: " << std::string(str) << std::endl;
  423. }
  424. #ifdef RTP_SUPPORT_SDESPRIV
  425. SDESinf.GotoFirstPrivateValue();
  426. uint8_t *pref;
  427. size_t preflen;
  428. while (SDESinf.GetNextPrivateValue(&pref,&preflen,&val,&len))
  429. {
  430. char prefstr[1024];
  431. memcpy(prefstr,pref,preflen);
  432. memcpy(str,val,len);
  433. prefstr[preflen] = 0;
  434. str[len] = 0;
  435. std::cout << " Private: " << std::string(prefstr) << ":" << std::string(str) << std::endl;
  436. }
  437. #endif // RTP_SUPPORT_SDESPRIV
  438. if (byereason)
  439. {
  440. memcpy(str,byereason,byereasonlen);
  441. str[byereasonlen] = 0;
  442. std::cout << " BYE Reason: " << std::string(str) << std::endl;
  443. }
  444. }
  445. #endif // RTPDEBUG
  446. } // end namespace