rtpinternalsourcedata.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 "rtpinternalsourcedata.h"
  26. #include "rtppacket.h"
  27. #include <string.h>
  28. #include "rtpdebug.h"
  29. #define RTPINTERNALSOURCEDATA_MAXPROBATIONPACKETS 32
  30. namespace jrtplib
  31. {
  32. RTPInternalSourceData::RTPInternalSourceData(uint32_t ssrc,RTPSources::ProbationType probtype,RTPMemoryManager *mgr):RTPSourceData(ssrc,mgr)
  33. {
  34. #ifdef RTP_SUPPORT_PROBATION
  35. probationtype = probtype;
  36. #endif // RTP_SUPPORT_PROBATION
  37. }
  38. RTPInternalSourceData::~RTPInternalSourceData()
  39. {
  40. }
  41. // The following function should delete rtppack if necessary
  42. int RTPInternalSourceData::ProcessRTPPacket(RTPPacket *rtppack,const RTPTime &receivetime,bool *stored)
  43. {
  44. bool accept,onprobation,applyprobation;
  45. double tsunit;
  46. *stored = false;
  47. if (timestampunit < 0)
  48. tsunit = INF_GetEstimatedTimestampUnit();
  49. else
  50. tsunit = timestampunit;
  51. #ifdef RTP_SUPPORT_PROBATION
  52. if (validated) // If the source is our own process, we can already be validated. No
  53. applyprobation = false; // probation should be applied in that case.
  54. else
  55. {
  56. if (probationtype == RTPSources::NoProbation)
  57. applyprobation = false;
  58. else
  59. applyprobation = true;
  60. }
  61. #else
  62. applyprobation = false;
  63. #endif // RTP_SUPPORT_PROBATION
  64. stats.ProcessPacket(rtppack,receivetime,tsunit,ownssrc,&accept,applyprobation,&onprobation);
  65. #ifdef RTP_SUPPORT_PROBATION
  66. switch (probationtype)
  67. {
  68. case RTPSources::ProbationStore:
  69. if (!(onprobation || accept))
  70. return 0;
  71. if (accept)
  72. validated = true;
  73. break;
  74. case RTPSources::ProbationDiscard:
  75. case RTPSources::NoProbation:
  76. if (!accept)
  77. return 0;
  78. validated = true;
  79. break;
  80. default:
  81. return ERR_RTP_INTERNALSOURCEDATA_INVALIDPROBATIONTYPE;
  82. }
  83. #else
  84. if (!accept)
  85. return 0;
  86. validated = true;
  87. #endif // RTP_SUPPORT_PROBATION;
  88. if (validated && !ownssrc) // for own ssrc these variables depend on the outgoing packets, not on the incoming
  89. issender = true;
  90. // Now, we can place the packet in the queue
  91. if (packetlist.empty())
  92. {
  93. *stored = true;
  94. packetlist.push_back(rtppack);
  95. return 0;
  96. }
  97. if (!validated) // still on probation
  98. {
  99. // Make sure that we don't buffer too much packets to avoid wasting memory
  100. // on a bad source. Delete the packet in the queue with the lowest sequence
  101. // number.
  102. if (packetlist.size() == RTPINTERNALSOURCEDATA_MAXPROBATIONPACKETS)
  103. {
  104. RTPPacket *p = *(packetlist.begin());
  105. packetlist.pop_front();
  106. RTPDelete(p,GetMemoryManager());
  107. }
  108. }
  109. // find the right position to insert the packet
  110. std::list<RTPPacket*>::iterator it,start;
  111. bool done = false;
  112. uint32_t newseqnr = rtppack->GetExtendedSequenceNumber();
  113. it = packetlist.end();
  114. --it;
  115. start = packetlist.begin();
  116. while (!done)
  117. {
  118. RTPPacket *p;
  119. uint32_t seqnr;
  120. p = *it;
  121. seqnr = p->GetExtendedSequenceNumber();
  122. if (seqnr > newseqnr)
  123. {
  124. if (it != start)
  125. --it;
  126. else // we're at the start of the list
  127. {
  128. *stored = true;
  129. done = true;
  130. packetlist.push_front(rtppack);
  131. }
  132. }
  133. else if (seqnr < newseqnr) // insert after this packet
  134. {
  135. ++it;
  136. packetlist.insert(it,rtppack);
  137. done = true;
  138. *stored = true;
  139. }
  140. else // they're equal !! Drop packet
  141. {
  142. done = true;
  143. }
  144. }
  145. return 0;
  146. }
  147. int RTPInternalSourceData::ProcessSDESItem(uint8_t sdesid,const uint8_t *data,size_t itemlen,const RTPTime &receivetime,bool *cnamecollis)
  148. {
  149. *cnamecollis = false;
  150. stats.SetLastMessageTime(receivetime);
  151. switch(sdesid)
  152. {
  153. case RTCP_SDES_ID_CNAME:
  154. {
  155. size_t curlen;
  156. uint8_t *oldcname;
  157. // NOTE: we're going to make sure that the CNAME is only set once.
  158. oldcname = SDESinf.GetCNAME(&curlen);
  159. if (curlen == 0)
  160. {
  161. // if CNAME is set, the source is validated
  162. SDESinf.SetCNAME(data,itemlen);
  163. validated = true;
  164. }
  165. else // check if this CNAME is equal to the one that is already present
  166. {
  167. if (curlen != itemlen)
  168. *cnamecollis = true;
  169. else
  170. {
  171. if (memcmp(data,oldcname,itemlen) != 0)
  172. *cnamecollis = true;
  173. }
  174. }
  175. }
  176. break;
  177. case RTCP_SDES_ID_NAME:
  178. {
  179. uint8_t *oldname;
  180. size_t oldlen;
  181. oldname = SDESinf.GetName(&oldlen);
  182. if (oldlen == 0) // Name not set
  183. return SDESinf.SetName(data,itemlen);
  184. }
  185. break;
  186. case RTCP_SDES_ID_EMAIL:
  187. {
  188. uint8_t *oldemail;
  189. size_t oldlen;
  190. oldemail = SDESinf.GetEMail(&oldlen);
  191. if (oldlen == 0)
  192. return SDESinf.SetEMail(data,itemlen);
  193. }
  194. break;
  195. case RTCP_SDES_ID_PHONE:
  196. return SDESinf.SetPhone(data,itemlen);
  197. case RTCP_SDES_ID_LOCATION:
  198. return SDESinf.SetLocation(data,itemlen);
  199. case RTCP_SDES_ID_TOOL:
  200. {
  201. uint8_t *oldtool;
  202. size_t oldlen;
  203. oldtool = SDESinf.GetTool(&oldlen);
  204. if (oldlen == 0)
  205. return SDESinf.SetTool(data,itemlen);
  206. }
  207. break;
  208. case RTCP_SDES_ID_NOTE:
  209. stats.SetLastNoteTime(receivetime);
  210. return SDESinf.SetNote(data,itemlen);
  211. }
  212. return 0;
  213. }
  214. #ifdef RTP_SUPPORT_SDESPRIV
  215. int RTPInternalSourceData::ProcessPrivateSDESItem(const uint8_t *prefix,size_t prefixlen,const uint8_t *value,size_t valuelen,const RTPTime &receivetime)
  216. {
  217. int status;
  218. stats.SetLastMessageTime(receivetime);
  219. status = SDESinf.SetPrivateValue(prefix,prefixlen,value,valuelen);
  220. if (status == ERR_RTP_SDES_MAXPRIVITEMS)
  221. return 0; // don't stop processing just because the number of items is full
  222. return status;
  223. }
  224. #endif // RTP_SUPPORT_SDESPRIV
  225. int RTPInternalSourceData::ProcessBYEPacket(const uint8_t *reason,size_t reasonlen,const RTPTime &receivetime)
  226. {
  227. if (byereason)
  228. {
  229. RTPDeleteByteArray(byereason,GetMemoryManager());
  230. byereason = 0;
  231. byereasonlen = 0;
  232. }
  233. byetime = receivetime;
  234. byereason = RTPNew(GetMemoryManager(),RTPMEM_TYPE_BUFFER_RTCPBYEREASON) uint8_t[reasonlen];
  235. if (byereason == 0)
  236. return ERR_RTP_OUTOFMEM;
  237. memcpy(byereason,reason,reasonlen);
  238. byereasonlen = reasonlen;
  239. receivedbye = true;
  240. stats.SetLastMessageTime(receivetime);
  241. return 0;
  242. }
  243. } // end namespace