rtp.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Real-time Protocol Support
  5. * Supports RTP and RTCP with Symmetric RTP support for NAT
  6. * traversal
  7. *
  8. * Copyright (C) 1999-2004, Digium, Inc.
  9. *
  10. * Mark Spencer <markster@digium.com>
  11. *
  12. * This program is free software, distributed under the terms of
  13. * the GNU General Public License
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <sys/time.h>
  19. #include <signal.h>
  20. #include <errno.h>
  21. #include <unistd.h>
  22. #include <netinet/in.h>
  23. #include <sys/time.h>
  24. #include <sys/socket.h>
  25. #include <arpa/inet.h>
  26. #include <fcntl.h>
  27. #include <asterisk/rtp.h>
  28. #include <asterisk/frame.h>
  29. #include <asterisk/logger.h>
  30. #include <asterisk/options.h>
  31. #include <asterisk/channel.h>
  32. #include <asterisk/acl.h>
  33. #include <asterisk/channel.h>
  34. #include <asterisk/channel_pvt.h>
  35. #include <asterisk/config.h>
  36. #include <asterisk/lock.h>
  37. #include <asterisk/utils.h>
  38. #define MAX_TIMESTAMP_SKEW 640
  39. #define RTP_MTU 1200
  40. #define TYPE_HIGH 0x0
  41. #define TYPE_LOW 0x1
  42. #define TYPE_SILENCE 0x2
  43. #define TYPE_DONTSEND 0x3
  44. #define TYPE_MASK 0x3
  45. static int dtmftimeout = 3000; /* 3000 samples */
  46. static int rtpstart = 0;
  47. static int rtpend = 0;
  48. #ifdef SO_NO_CHECK
  49. static int nochecksums = 0;
  50. #endif
  51. /* The value of each payload format mapping: */
  52. struct rtpPayloadType {
  53. int isAstFormat; /* whether the following code is an AST_FORMAT */
  54. int code;
  55. };
  56. #define MAX_RTP_PT 256
  57. #define FLAG_3389_WARNING (1 << 0)
  58. struct ast_rtp {
  59. int s;
  60. char resp;
  61. struct ast_frame f;
  62. unsigned char rawdata[8192 + AST_FRIENDLY_OFFSET];
  63. unsigned int ssrc;
  64. unsigned int lastts;
  65. unsigned int lastrxts;
  66. unsigned int lastividtimestamp;
  67. unsigned int lastovidtimestamp;
  68. unsigned int lasteventseqn;
  69. int lasttxformat;
  70. int lastrxformat;
  71. int dtmfcount;
  72. unsigned int dtmfduration;
  73. int nat;
  74. int flags;
  75. struct sockaddr_in us;
  76. struct sockaddr_in them;
  77. struct timeval rxcore;
  78. struct timeval txcore;
  79. struct timeval dtmfmute;
  80. struct ast_smoother *smoother;
  81. int *ioid;
  82. unsigned short seqno;
  83. struct sched_context *sched;
  84. struct io_context *io;
  85. void *data;
  86. ast_rtp_callback callback;
  87. struct rtpPayloadType current_RTP_PT[MAX_RTP_PT];
  88. int rtp_lookup_code_cache_isAstFormat; /* a cache for the result of rtp_lookup_code(): */
  89. int rtp_lookup_code_cache_code;
  90. int rtp_lookup_code_cache_result;
  91. int rtp_offered_from_local;
  92. struct ast_rtcp *rtcp;
  93. };
  94. struct ast_rtcp {
  95. int s; /* Socket */
  96. struct sockaddr_in us;
  97. struct sockaddr_in them;
  98. };
  99. static struct ast_rtp_protocol *protos = NULL;
  100. int ast_rtp_fd(struct ast_rtp *rtp)
  101. {
  102. return rtp->s;
  103. }
  104. int ast_rtcp_fd(struct ast_rtp *rtp)
  105. {
  106. if (rtp->rtcp)
  107. return rtp->rtcp->s;
  108. return -1;
  109. }
  110. static int g723_len(unsigned char buf)
  111. {
  112. switch(buf & TYPE_MASK) {
  113. case TYPE_DONTSEND:
  114. return 0;
  115. break;
  116. case TYPE_SILENCE:
  117. return 4;
  118. break;
  119. case TYPE_HIGH:
  120. return 24;
  121. break;
  122. case TYPE_LOW:
  123. return 20;
  124. break;
  125. default:
  126. ast_log(LOG_WARNING, "Badly encoded frame (%d)\n", buf & TYPE_MASK);
  127. }
  128. return -1;
  129. }
  130. static int g723_samples(unsigned char *buf, int maxlen)
  131. {
  132. int pos = 0;
  133. int samples = 0;
  134. int res;
  135. while(pos < maxlen) {
  136. res = g723_len(buf[pos]);
  137. if (res <= 0)
  138. break;
  139. samples += 240;
  140. pos += res;
  141. }
  142. return samples;
  143. }
  144. void ast_rtp_set_data(struct ast_rtp *rtp, void *data)
  145. {
  146. rtp->data = data;
  147. }
  148. void ast_rtp_set_callback(struct ast_rtp *rtp, ast_rtp_callback callback)
  149. {
  150. rtp->callback = callback;
  151. }
  152. void ast_rtp_setnat(struct ast_rtp *rtp, int nat)
  153. {
  154. rtp->nat = nat;
  155. }
  156. static struct ast_frame *send_dtmf(struct ast_rtp *rtp)
  157. {
  158. struct timeval tv;
  159. static struct ast_frame null_frame = { AST_FRAME_NULL, };
  160. char iabuf[INET_ADDRSTRLEN];
  161. gettimeofday(&tv, NULL);
  162. if ((tv.tv_sec < rtp->dtmfmute.tv_sec) ||
  163. ((tv.tv_sec == rtp->dtmfmute.tv_sec) && (tv.tv_usec < rtp->dtmfmute.tv_usec))) {
  164. ast_log(LOG_DEBUG, "Ignore potential DTMF echo from '%s'\n", ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr));
  165. rtp->resp = 0;
  166. rtp->dtmfduration = 0;
  167. return &null_frame;
  168. }
  169. ast_log(LOG_DEBUG, "Sending dtmf: %d (%c), at %s\n", rtp->resp, rtp->resp, ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr));
  170. if (rtp->resp == 'X') {
  171. rtp->f.frametype = AST_FRAME_CONTROL;
  172. rtp->f.subclass = AST_CONTROL_FLASH;
  173. } else {
  174. rtp->f.frametype = AST_FRAME_DTMF;
  175. rtp->f.subclass = rtp->resp;
  176. }
  177. rtp->f.datalen = 0;
  178. rtp->f.samples = 0;
  179. rtp->f.mallocd = 0;
  180. rtp->f.src = "RTP";
  181. rtp->resp = 0;
  182. rtp->dtmfduration = 0;
  183. return &rtp->f;
  184. }
  185. static struct ast_frame *process_cisco_dtmf(struct ast_rtp *rtp, unsigned char *data, int len)
  186. {
  187. unsigned int event;
  188. char resp = 0;
  189. struct ast_frame *f = NULL;
  190. event = ntohl(*((unsigned int *)(data)));
  191. event &= 0x001F;
  192. #if 0
  193. printf("Cisco Digit: %08x (len = %d)\n", event, len);
  194. #endif
  195. if (event < 10) {
  196. resp = '0' + event;
  197. } else if (event < 11) {
  198. resp = '*';
  199. } else if (event < 12) {
  200. resp = '#';
  201. } else if (event < 16) {
  202. resp = 'A' + (event - 12);
  203. } else if (event < 17) {
  204. resp = 'X';
  205. }
  206. if (rtp->resp && (rtp->resp != resp)) {
  207. f = send_dtmf(rtp);
  208. }
  209. rtp->resp = resp;
  210. rtp->dtmfcount = dtmftimeout;
  211. return f;
  212. }
  213. static struct ast_frame *process_rfc2833(struct ast_rtp *rtp, unsigned char *data, int len)
  214. {
  215. unsigned int event;
  216. unsigned int event_end;
  217. unsigned int duration;
  218. char resp = 0;
  219. struct ast_frame *f = NULL;
  220. event = ntohl(*((unsigned int *)(data)));
  221. event >>= 24;
  222. event_end = ntohl(*((unsigned int *)(data)));
  223. event_end <<= 8;
  224. event_end >>= 24;
  225. duration = ntohl(*((unsigned int *)(data)));
  226. duration &= 0xFFFF;
  227. #if 0
  228. printf("Event: %08x (len = %d)\n", event, len);
  229. #endif
  230. if (event < 10) {
  231. resp = '0' + event;
  232. } else if (event < 11) {
  233. resp = '*';
  234. } else if (event < 12) {
  235. resp = '#';
  236. } else if (event < 16) {
  237. resp = 'A' + (event - 12);
  238. } else if (event < 17) {
  239. resp = 'X';
  240. }
  241. if (rtp->resp && (rtp->resp != resp)) {
  242. f = send_dtmf(rtp);
  243. }
  244. else if(event_end & 0x80)
  245. {
  246. if (rtp->resp) {
  247. f = send_dtmf(rtp);
  248. rtp->resp = 0;
  249. }
  250. resp = 0;
  251. duration = 0;
  252. }
  253. else if(rtp->dtmfduration && (duration < rtp->dtmfduration))
  254. {
  255. f = send_dtmf(rtp);
  256. }
  257. if (!(event_end & 0x80))
  258. rtp->resp = resp;
  259. rtp->dtmfcount = dtmftimeout;
  260. rtp->dtmfduration = duration;
  261. return f;
  262. }
  263. static struct ast_frame *process_rfc3389(struct ast_rtp *rtp, unsigned char *data, int len)
  264. {
  265. struct ast_frame *f = NULL;
  266. /* Convert comfort noise into audio with various codecs. Unfortunately this doesn't
  267. totally help us out becuase we don't have an engine to keep it going and we are not
  268. guaranteed to have it every 20ms or anything */
  269. #if 1
  270. printf("RFC3389: %d bytes, level %d...\n", len, rtp->lastrxformat);
  271. #endif
  272. if (!(rtp->flags & FLAG_3389_WARNING)) {
  273. ast_log(LOG_NOTICE, "RFC3389 support incomplete. Turn off on client if possible\n");
  274. rtp->flags |= FLAG_3389_WARNING;
  275. }
  276. /* Must have at least one byte */
  277. if (!len)
  278. return NULL;
  279. if (len < 24) {
  280. rtp->f.data = rtp->rawdata + AST_FRIENDLY_OFFSET;
  281. rtp->f.datalen = len - 1;
  282. rtp->f.offset = AST_FRIENDLY_OFFSET;
  283. memcpy(rtp->f.data, data + 1, len - 1);
  284. } else {
  285. rtp->f.data = NULL;
  286. rtp->f.offset = 0;
  287. rtp->f.datalen = 0;
  288. }
  289. rtp->f.frametype = AST_FRAME_CNG;
  290. rtp->f.subclass = data[0] & 0x7f;
  291. rtp->f.datalen = len - 1;
  292. rtp->f.samples = 0;
  293. rtp->f.delivery.tv_usec = rtp->f.delivery.tv_sec = 0;
  294. f = &rtp->f;
  295. return f;
  296. }
  297. static int rtpread(int *id, int fd, short events, void *cbdata)
  298. {
  299. struct ast_rtp *rtp = cbdata;
  300. struct ast_frame *f;
  301. f = ast_rtp_read(rtp);
  302. if (f) {
  303. if (rtp->callback)
  304. rtp->callback(rtp, f, rtp->data);
  305. }
  306. return 1;
  307. }
  308. struct ast_frame *ast_rtcp_read(struct ast_rtp *rtp)
  309. {
  310. static struct ast_frame null_frame = { AST_FRAME_NULL, };
  311. int len;
  312. int hdrlen = 8;
  313. int res;
  314. struct sockaddr_in sin;
  315. unsigned int rtcpdata[1024];
  316. char iabuf[INET_ADDRSTRLEN];
  317. if (!rtp || !rtp->rtcp)
  318. return &null_frame;
  319. len = sizeof(sin);
  320. res = recvfrom(rtp->rtcp->s, rtcpdata, sizeof(rtcpdata),
  321. 0, (struct sockaddr *)&sin, &len);
  322. if (res < 0) {
  323. if (errno != EAGAIN)
  324. ast_log(LOG_WARNING, "RTP Read error: %s\n", strerror(errno));
  325. if (errno == EBADF)
  326. CRASH;
  327. return &null_frame;
  328. }
  329. if (res < hdrlen) {
  330. ast_log(LOG_WARNING, "RTP Read too short\n");
  331. return &null_frame;
  332. }
  333. if (rtp->nat) {
  334. /* Send to whoever sent to us */
  335. if ((rtp->rtcp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
  336. (rtp->rtcp->them.sin_port != sin.sin_port)) {
  337. memcpy(&rtp->rtcp->them, &sin, sizeof(rtp->rtcp->them));
  338. ast_log(LOG_DEBUG, "RTP NAT: Using address %s:%d\n", ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
  339. }
  340. }
  341. if (option_debug)
  342. ast_log(LOG_DEBUG, "Got RTCP report of %d bytes\n", res);
  343. return &null_frame;
  344. }
  345. static void calc_rxstamp(struct timeval *tv, struct ast_rtp *rtp, unsigned int timestamp, int mark)
  346. {
  347. if ((!rtp->rxcore.tv_sec && !rtp->rxcore.tv_usec) || mark) {
  348. gettimeofday(&rtp->rxcore, NULL);
  349. rtp->rxcore.tv_sec -= timestamp / 8000;
  350. rtp->rxcore.tv_usec -= (timestamp % 8000) * 125;
  351. /* Round to 20ms for nice, pretty timestamps */
  352. rtp->rxcore.tv_usec -= rtp->rxcore.tv_usec % 20000;
  353. if (rtp->rxcore.tv_usec < 0) {
  354. /* Adjust appropriately if necessary */
  355. rtp->rxcore.tv_usec += 1000000;
  356. rtp->rxcore.tv_sec -= 1;
  357. }
  358. }
  359. tv->tv_sec = rtp->rxcore.tv_sec + timestamp / 8000;
  360. tv->tv_usec = rtp->rxcore.tv_usec + (timestamp % 8000) * 125;
  361. if (tv->tv_usec >= 1000000) {
  362. tv->tv_usec -= 1000000;
  363. tv->tv_sec += 1;
  364. }
  365. }
  366. struct ast_frame *ast_rtp_read(struct ast_rtp *rtp)
  367. {
  368. int res;
  369. struct sockaddr_in sin;
  370. int len;
  371. unsigned int seqno;
  372. int version;
  373. int payloadtype;
  374. int hdrlen = 12;
  375. int padding;
  376. int mark;
  377. int ext;
  378. char iabuf[INET_ADDRSTRLEN];
  379. unsigned int timestamp;
  380. unsigned int *rtpheader;
  381. static struct ast_frame *f, null_frame = { AST_FRAME_NULL, };
  382. struct rtpPayloadType rtpPT;
  383. len = sizeof(sin);
  384. /* Cache where the header will go */
  385. res = recvfrom(rtp->s, rtp->rawdata + AST_FRIENDLY_OFFSET, sizeof(rtp->rawdata) - AST_FRIENDLY_OFFSET,
  386. 0, (struct sockaddr *)&sin, &len);
  387. rtpheader = (unsigned int *)(rtp->rawdata + AST_FRIENDLY_OFFSET);
  388. if (res < 0) {
  389. if (errno != EAGAIN)
  390. ast_log(LOG_WARNING, "RTP Read error: %s\n", strerror(errno));
  391. if (errno == EBADF)
  392. CRASH;
  393. return &null_frame;
  394. }
  395. if (res < hdrlen) {
  396. ast_log(LOG_WARNING, "RTP Read too short\n");
  397. return &null_frame;
  398. }
  399. /* Ignore if the other side hasn't been given an address
  400. yet. */
  401. if (!rtp->them.sin_addr.s_addr || !rtp->them.sin_port)
  402. return &null_frame;
  403. if (rtp->nat) {
  404. /* Send to whoever sent to us */
  405. if ((rtp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
  406. (rtp->them.sin_port != sin.sin_port)) {
  407. memcpy(&rtp->them, &sin, sizeof(rtp->them));
  408. ast_log(LOG_DEBUG, "RTP NAT: Using address %s:%d\n", ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr), ntohs(rtp->them.sin_port));
  409. }
  410. }
  411. /* Get fields */
  412. seqno = ntohl(rtpheader[0]);
  413. /* Check RTP version */
  414. version = (seqno & 0xC0000000) >> 30;
  415. if (version != 2)
  416. return &null_frame;
  417. payloadtype = (seqno & 0x7f0000) >> 16;
  418. padding = seqno & (1 << 29);
  419. mark = seqno & (1 << 23);
  420. ext = seqno & (1 << 28);
  421. seqno &= 0xffff;
  422. timestamp = ntohl(rtpheader[1]);
  423. if (padding) {
  424. /* Remove padding bytes */
  425. res -= rtp->rawdata[AST_FRIENDLY_OFFSET + res - 1];
  426. }
  427. if (ext) {
  428. /* RTP Extension present */
  429. hdrlen += 4;
  430. hdrlen += (ntohl(rtpheader[3]) & 0xffff) << 2;
  431. }
  432. if (res < hdrlen) {
  433. ast_log(LOG_WARNING, "RTP Read too short (%d, expecting %d)\n", res, hdrlen);
  434. return &null_frame;
  435. }
  436. #if 0
  437. printf("Got RTP packet from %s:%d (type %d, seq %d, ts %d, len = %d)\n", ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), ntohs(sin.sin_port), payloadtype, seqno, timestamp,res - hdrlen);
  438. #endif
  439. rtpPT = ast_rtp_lookup_pt(rtp, payloadtype);
  440. if (!rtpPT.isAstFormat) {
  441. /* This is special in-band data that's not one of our codecs */
  442. if (rtpPT.code == AST_RTP_DTMF) {
  443. /* It's special -- rfc2833 process it */
  444. if (rtp->lasteventseqn <= seqno || rtp->resp == 0 || (rtp->lasteventseqn >= 65530 && seqno <= 6)) {
  445. f = process_rfc2833(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
  446. rtp->lasteventseqn = seqno;
  447. } else f = NULL;
  448. if (f) return f; else return &null_frame;
  449. } else if (rtpPT.code == AST_RTP_CISCO_DTMF) {
  450. /* It's really special -- process it the Cisco way */
  451. if (rtp->lasteventseqn <= seqno || rtp->resp == 0 || (rtp->lasteventseqn >= 65530 && seqno <= 6)) {
  452. f = process_cisco_dtmf(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
  453. rtp->lasteventseqn = seqno;
  454. } else f = NULL;
  455. if (f) return f; else return &null_frame;
  456. } else if (rtpPT.code == AST_RTP_CN) {
  457. /* Comfort Noise */
  458. f = process_rfc3389(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
  459. if (f) return f; else return &null_frame;
  460. } else {
  461. ast_log(LOG_NOTICE, "Unknown RTP codec %d received\n", payloadtype);
  462. return &null_frame;
  463. }
  464. }
  465. rtp->f.subclass = rtpPT.code;
  466. if (rtp->f.subclass < AST_FORMAT_MAX_AUDIO)
  467. rtp->f.frametype = AST_FRAME_VOICE;
  468. else
  469. rtp->f.frametype = AST_FRAME_VIDEO;
  470. rtp->lastrxformat = rtp->f.subclass;
  471. if (!rtp->lastrxts)
  472. rtp->lastrxts = timestamp;
  473. if (rtp->dtmfcount) {
  474. #if 0
  475. printf("dtmfcount was %d\n", rtp->dtmfcount);
  476. #endif
  477. rtp->dtmfcount -= (timestamp - rtp->lastrxts);
  478. if (rtp->dtmfcount < 0)
  479. rtp->dtmfcount = 0;
  480. #if 0
  481. if (dtmftimeout != rtp->dtmfcount)
  482. printf("dtmfcount is %d\n", rtp->dtmfcount);
  483. #endif
  484. }
  485. rtp->lastrxts = timestamp;
  486. /* Send any pending DTMF */
  487. if (rtp->resp && !rtp->dtmfcount) {
  488. ast_log(LOG_DEBUG, "Sending pending DTMF\n");
  489. return send_dtmf(rtp);
  490. }
  491. rtp->f.mallocd = 0;
  492. rtp->f.datalen = res - hdrlen;
  493. rtp->f.data = rtp->rawdata + hdrlen + AST_FRIENDLY_OFFSET;
  494. rtp->f.offset = hdrlen + AST_FRIENDLY_OFFSET;
  495. if (rtp->f.subclass < AST_FORMAT_MAX_AUDIO) {
  496. switch(rtp->f.subclass) {
  497. case AST_FORMAT_ULAW:
  498. case AST_FORMAT_ALAW:
  499. rtp->f.samples = rtp->f.datalen;
  500. break;
  501. case AST_FORMAT_SLINEAR:
  502. rtp->f.samples = rtp->f.datalen / 2;
  503. ast_frame_byteswap_be(&rtp->f);
  504. break;
  505. case AST_FORMAT_GSM:
  506. rtp->f.samples = 160 * (rtp->f.datalen / 33);
  507. break;
  508. case AST_FORMAT_ILBC:
  509. rtp->f.samples = 240 * (rtp->f.datalen / 50);
  510. break;
  511. case AST_FORMAT_ADPCM:
  512. case AST_FORMAT_G726:
  513. rtp->f.samples = rtp->f.datalen * 2;
  514. break;
  515. case AST_FORMAT_G729A:
  516. rtp->f.samples = rtp->f.datalen * 8;
  517. break;
  518. case AST_FORMAT_G723_1:
  519. rtp->f.samples = g723_samples(rtp->f.data, rtp->f.datalen);
  520. break;
  521. case AST_FORMAT_SPEEX:
  522. /* assumes that the RTP packet contained one Speex frame */
  523. rtp->f.samples = 160;
  524. break;
  525. case AST_FORMAT_LPC10:
  526. rtp->f.samples = 22 * 8;
  527. rtp->f.samples += (((char *)(rtp->f.data))[7] & 0x1) * 8;
  528. break;
  529. default:
  530. ast_log(LOG_NOTICE, "Unable to calculate samples for format %s\n", ast_getformatname(rtp->f.subclass));
  531. break;
  532. }
  533. calc_rxstamp(&rtp->f.delivery, rtp, timestamp, mark);
  534. } else {
  535. /* Video -- samples is # of samples vs. 90000 */
  536. if (!rtp->lastividtimestamp)
  537. rtp->lastividtimestamp = timestamp;
  538. rtp->f.samples = timestamp - rtp->lastividtimestamp;
  539. rtp->lastividtimestamp = timestamp;
  540. rtp->f.delivery.tv_sec = 0;
  541. rtp->f.delivery.tv_usec = 0;
  542. if (mark)
  543. rtp->f.subclass |= 0x1;
  544. }
  545. rtp->f.src = "RTP";
  546. return &rtp->f;
  547. }
  548. /* The following array defines the MIME Media type (and subtype) for each
  549. of our codecs, or RTP-specific data type. */
  550. static struct {
  551. struct rtpPayloadType payloadType;
  552. char* type;
  553. char* subtype;
  554. } mimeTypes[] = {
  555. {{1, AST_FORMAT_G723_1}, "audio", "G723"},
  556. {{1, AST_FORMAT_GSM}, "audio", "GSM"},
  557. {{1, AST_FORMAT_ULAW}, "audio", "PCMU"},
  558. {{1, AST_FORMAT_ALAW}, "audio", "PCMA"},
  559. {{1, AST_FORMAT_G726}, "audio", "G726-32"},
  560. {{1, AST_FORMAT_ADPCM}, "audio", "DVI4"},
  561. {{1, AST_FORMAT_SLINEAR}, "audio", "L16"},
  562. {{1, AST_FORMAT_LPC10}, "audio", "LPC"},
  563. {{1, AST_FORMAT_G729A}, "audio", "G729"},
  564. {{1, AST_FORMAT_SPEEX}, "audio", "speex"},
  565. {{1, AST_FORMAT_ILBC}, "audio", "iLBC"},
  566. {{0, AST_RTP_DTMF}, "audio", "telephone-event"},
  567. {{0, AST_RTP_CISCO_DTMF}, "audio", "cisco-telephone-event"},
  568. {{0, AST_RTP_CN}, "audio", "CN"},
  569. {{1, AST_FORMAT_JPEG}, "video", "JPEG"},
  570. {{1, AST_FORMAT_PNG}, "video", "PNG"},
  571. {{1, AST_FORMAT_H261}, "video", "H261"},
  572. {{1, AST_FORMAT_H263}, "video", "H263"},
  573. };
  574. /* Static (i.e., well-known) RTP payload types for our "AST_FORMAT..."s:
  575. also, our own choices for dynamic payload types. This is our master
  576. table for transmission */
  577. static struct rtpPayloadType static_RTP_PT[MAX_RTP_PT] = {
  578. [0] = {1, AST_FORMAT_ULAW},
  579. #ifdef USE_DEPRECATED_G726
  580. [2] = {1, AST_FORMAT_G726}, /* Technically this is G.721, but if Cisco can do it, so can we... */
  581. #endif
  582. [3] = {1, AST_FORMAT_GSM},
  583. [4] = {1, AST_FORMAT_G723_1},
  584. [5] = {1, AST_FORMAT_ADPCM}, /* 8 kHz */
  585. [6] = {1, AST_FORMAT_ADPCM}, /* 16 kHz */
  586. [7] = {1, AST_FORMAT_LPC10},
  587. [8] = {1, AST_FORMAT_ALAW},
  588. [10] = {1, AST_FORMAT_SLINEAR}, /* 2 channels */
  589. [11] = {1, AST_FORMAT_SLINEAR}, /* 1 channel */
  590. [13] = {0, AST_RTP_CN},
  591. [16] = {1, AST_FORMAT_ADPCM}, /* 11.025 kHz */
  592. [17] = {1, AST_FORMAT_ADPCM}, /* 22.050 kHz */
  593. [18] = {1, AST_FORMAT_G729A},
  594. [19] = {0, AST_RTP_CN}, /* Also used for CN */
  595. [26] = {1, AST_FORMAT_JPEG},
  596. [31] = {1, AST_FORMAT_H261},
  597. [34] = {1, AST_FORMAT_H263},
  598. [97] = {1, AST_FORMAT_ILBC},
  599. [101] = {0, AST_RTP_DTMF},
  600. [110] = {1, AST_FORMAT_SPEEX},
  601. [111] = {1, AST_FORMAT_G726},
  602. [121] = {0, AST_RTP_CISCO_DTMF}, /* Must be type 121 */
  603. };
  604. void ast_rtp_pt_clear(struct ast_rtp* rtp)
  605. {
  606. int i;
  607. for (i = 0; i < MAX_RTP_PT; ++i) {
  608. rtp->current_RTP_PT[i].isAstFormat = 0;
  609. rtp->current_RTP_PT[i].code = 0;
  610. }
  611. rtp->rtp_lookup_code_cache_isAstFormat = 0;
  612. rtp->rtp_lookup_code_cache_code = 0;
  613. rtp->rtp_lookup_code_cache_result = 0;
  614. }
  615. void ast_rtp_pt_default(struct ast_rtp* rtp)
  616. {
  617. int i;
  618. /* Initialize to default payload types */
  619. for (i = 0; i < MAX_RTP_PT; ++i) {
  620. rtp->current_RTP_PT[i].isAstFormat = static_RTP_PT[i].isAstFormat;
  621. rtp->current_RTP_PT[i].code = static_RTP_PT[i].code;
  622. }
  623. rtp->rtp_lookup_code_cache_isAstFormat = 0;
  624. rtp->rtp_lookup_code_cache_code = 0;
  625. rtp->rtp_lookup_code_cache_result = 0;
  626. }
  627. /* Make a note of a RTP payload type that was seen in a SDP "m=" line. */
  628. /* By default, use the well-known value for this type (although it may */
  629. /* still be set to a different value by a subsequent "a=rtpmap:" line): */
  630. void ast_rtp_set_m_type(struct ast_rtp* rtp, int pt) {
  631. if (pt < 0 || pt > MAX_RTP_PT) return; /* bogus payload type */
  632. if (static_RTP_PT[pt].code != 0) {
  633. rtp->current_RTP_PT[pt] = static_RTP_PT[pt];
  634. }
  635. }
  636. /* Make a note of a RTP payload type (with MIME type) that was seen in */
  637. /* a SDP "a=rtpmap:" line. */
  638. void ast_rtp_set_rtpmap_type(struct ast_rtp* rtp, int pt,
  639. char* mimeType, char* mimeSubtype) {
  640. int i;
  641. if (pt < 0 || pt > MAX_RTP_PT) return; /* bogus payload type */
  642. for (i = 0; i < sizeof mimeTypes/sizeof mimeTypes[0]; ++i) {
  643. if (strcasecmp(mimeSubtype, mimeTypes[i].subtype) == 0 &&
  644. strcasecmp(mimeType, mimeTypes[i].type) == 0) {
  645. rtp->current_RTP_PT[pt] = mimeTypes[i].payloadType;
  646. return;
  647. }
  648. }
  649. }
  650. /* Return the union of all of the codecs that were set by rtp_set...() calls */
  651. /* They're returned as two distinct sets: AST_FORMATs, and AST_RTPs */
  652. void ast_rtp_get_current_formats(struct ast_rtp* rtp,
  653. int* astFormats, int* nonAstFormats) {
  654. int pt;
  655. *astFormats = *nonAstFormats = 0;
  656. for (pt = 0; pt < MAX_RTP_PT; ++pt) {
  657. if (rtp->current_RTP_PT[pt].isAstFormat) {
  658. *astFormats |= rtp->current_RTP_PT[pt].code;
  659. } else {
  660. *nonAstFormats |= rtp->current_RTP_PT[pt].code;
  661. }
  662. }
  663. }
  664. void ast_rtp_offered_from_local(struct ast_rtp* rtp, int local) {
  665. if (rtp)
  666. rtp->rtp_offered_from_local = local;
  667. else
  668. ast_log(LOG_WARNING, "rtp structure is null\n");
  669. }
  670. struct rtpPayloadType ast_rtp_lookup_pt(struct ast_rtp* rtp, int pt)
  671. {
  672. struct rtpPayloadType result;
  673. result.isAstFormat = result.code = 0;
  674. if (pt < 0 || pt > MAX_RTP_PT) {
  675. return result; /* bogus payload type */
  676. }
  677. /* Start with the negotiated codecs */
  678. if (!rtp->rtp_offered_from_local)
  679. result = rtp->current_RTP_PT[pt];
  680. /* If it doesn't exist, check our static RTP type list, just in case */
  681. if (!result.code)
  682. result = static_RTP_PT[pt];
  683. return result;
  684. }
  685. /* Looks up an RTP code out of our *static* outbound list */
  686. int ast_rtp_lookup_code(struct ast_rtp* rtp, const int isAstFormat, const int code) {
  687. int pt;
  688. if (isAstFormat == rtp->rtp_lookup_code_cache_isAstFormat &&
  689. code == rtp->rtp_lookup_code_cache_code) {
  690. /* Use our cached mapping, to avoid the overhead of the loop below */
  691. return rtp->rtp_lookup_code_cache_result;
  692. }
  693. /* Check the dynamic list first */
  694. for (pt = 0; pt < MAX_RTP_PT; ++pt) {
  695. if (rtp->current_RTP_PT[pt].code == code &&
  696. rtp->current_RTP_PT[pt].isAstFormat == isAstFormat) {
  697. rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
  698. rtp->rtp_lookup_code_cache_code = code;
  699. rtp->rtp_lookup_code_cache_result = pt;
  700. return pt;
  701. }
  702. }
  703. /* Then the static list */
  704. for (pt = 0; pt < MAX_RTP_PT; ++pt) {
  705. if (static_RTP_PT[pt].code == code &&
  706. static_RTP_PT[pt].isAstFormat == isAstFormat) {
  707. rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
  708. rtp->rtp_lookup_code_cache_code = code;
  709. rtp->rtp_lookup_code_cache_result = pt;
  710. return pt;
  711. }
  712. }
  713. return -1;
  714. }
  715. char* ast_rtp_lookup_mime_subtype(const int isAstFormat, const int code) {
  716. int i;
  717. for (i = 0; i < sizeof mimeTypes/sizeof mimeTypes[0]; ++i) {
  718. if (mimeTypes[i].payloadType.code == code &&
  719. mimeTypes[i].payloadType.isAstFormat == isAstFormat) {
  720. return mimeTypes[i].subtype;
  721. }
  722. }
  723. return "";
  724. }
  725. char *ast_rtp_lookup_mime_multiple(char *buf, int size, const int capability, const int isAstFormat)
  726. {
  727. int format;
  728. unsigned len;
  729. char *end = buf;
  730. char *start = buf;
  731. if (!buf || !size)
  732. return NULL;
  733. snprintf(end, size, "0x%x (", capability);
  734. len = strlen(end);
  735. end += len;
  736. size -= len;
  737. start = end;
  738. for (format = 1; format < AST_RTP_MAX; format <<= 1) {
  739. if (capability & format) {
  740. const char *name = ast_rtp_lookup_mime_subtype(isAstFormat, format);
  741. snprintf(end, size, "%s|", name);
  742. len = strlen(end);
  743. end += len;
  744. size -= len;
  745. }
  746. }
  747. if (start == end)
  748. snprintf(start, size, "nothing)");
  749. else if (size > 1)
  750. *(end -1) = ')';
  751. return buf;
  752. }
  753. static int rtp_socket(void)
  754. {
  755. int s;
  756. long flags;
  757. s = socket(AF_INET, SOCK_DGRAM, 0);
  758. if (s > -1) {
  759. flags = fcntl(s, F_GETFL);
  760. fcntl(s, F_SETFL, flags | O_NONBLOCK);
  761. #ifdef SO_NO_CHECK
  762. if (nochecksums)
  763. setsockopt(s, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
  764. #endif
  765. }
  766. return s;
  767. }
  768. static struct ast_rtcp *ast_rtcp_new(void)
  769. {
  770. struct ast_rtcp *rtcp;
  771. rtcp = malloc(sizeof(struct ast_rtcp));
  772. if (!rtcp)
  773. return NULL;
  774. memset(rtcp, 0, sizeof(struct ast_rtcp));
  775. rtcp->s = rtp_socket();
  776. rtcp->us.sin_family = AF_INET;
  777. if (rtcp->s < 0) {
  778. free(rtcp);
  779. ast_log(LOG_WARNING, "Unable to allocate socket: %s\n", strerror(errno));
  780. return NULL;
  781. }
  782. return rtcp;
  783. }
  784. struct ast_rtp *ast_rtp_new_with_bindaddr(struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode, struct in_addr addr)
  785. {
  786. struct ast_rtp *rtp;
  787. int x;
  788. int first;
  789. int startplace;
  790. rtp = malloc(sizeof(struct ast_rtp));
  791. if (!rtp)
  792. return NULL;
  793. memset(rtp, 0, sizeof(struct ast_rtp));
  794. rtp->them.sin_family = AF_INET;
  795. rtp->us.sin_family = AF_INET;
  796. rtp->s = rtp_socket();
  797. rtp->ssrc = rand();
  798. rtp->seqno = rand() & 0xffff;
  799. if (rtp->s < 0) {
  800. free(rtp);
  801. ast_log(LOG_WARNING, "Unable to allocate socket: %s\n", strerror(errno));
  802. return NULL;
  803. }
  804. if (sched && rtcpenable) {
  805. rtp->sched = sched;
  806. rtp->rtcp = ast_rtcp_new();
  807. }
  808. /* Find us a place */
  809. x = (rand() % (rtpend-rtpstart)) + rtpstart;
  810. x = x & ~1;
  811. startplace = x;
  812. for (;;) {
  813. /* Must be an even port number by RTP spec */
  814. rtp->us.sin_port = htons(x);
  815. rtp->us.sin_addr = addr;
  816. if (rtp->rtcp)
  817. rtp->rtcp->us.sin_port = htons(x + 1);
  818. if (!(first = bind(rtp->s, (struct sockaddr *)&rtp->us, sizeof(rtp->us))) &&
  819. (!rtp->rtcp || !bind(rtp->rtcp->s, (struct sockaddr *)&rtp->rtcp->us, sizeof(rtp->rtcp->us))))
  820. break;
  821. if (!first) {
  822. /* Primary bind succeeded! Gotta recreate it */
  823. close(rtp->s);
  824. rtp->s = rtp_socket();
  825. }
  826. if (errno != EADDRINUSE) {
  827. ast_log(LOG_WARNING, "Unexpected bind error: %s\n", strerror(errno));
  828. close(rtp->s);
  829. if (rtp->rtcp) {
  830. close(rtp->rtcp->s);
  831. free(rtp->rtcp);
  832. }
  833. free(rtp);
  834. return NULL;
  835. }
  836. x += 2;
  837. if (x > rtpend)
  838. x = (rtpstart + 1) & ~1;
  839. if (x == startplace) {
  840. ast_log(LOG_WARNING, "No RTP ports remaining\n");
  841. close(rtp->s);
  842. if (rtp->rtcp) {
  843. close(rtp->rtcp->s);
  844. free(rtp->rtcp);
  845. }
  846. free(rtp);
  847. return NULL;
  848. }
  849. }
  850. if (io && sched && callbackmode) {
  851. /* Operate this one in a callback mode */
  852. rtp->sched = sched;
  853. rtp->io = io;
  854. rtp->ioid = ast_io_add(rtp->io, rtp->s, rtpread, AST_IO_IN, rtp);
  855. }
  856. ast_rtp_pt_default(rtp);
  857. return rtp;
  858. }
  859. struct ast_rtp *ast_rtp_new(struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode)
  860. {
  861. struct in_addr ia;
  862. memset(&ia, 0, sizeof(ia));
  863. return ast_rtp_new_with_bindaddr(sched, io, rtcpenable, callbackmode, ia);
  864. }
  865. int ast_rtp_settos(struct ast_rtp *rtp, int tos)
  866. {
  867. int res;
  868. if ((res = setsockopt(rtp->s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))))
  869. ast_log(LOG_WARNING, "Unable to set TOS to %d\n", tos);
  870. return res;
  871. }
  872. void ast_rtp_set_peer(struct ast_rtp *rtp, struct sockaddr_in *them)
  873. {
  874. rtp->them.sin_port = them->sin_port;
  875. rtp->them.sin_addr = them->sin_addr;
  876. if (rtp->rtcp) {
  877. rtp->rtcp->them.sin_port = htons(ntohs(them->sin_port) + 1);
  878. rtp->rtcp->them.sin_addr = them->sin_addr;
  879. }
  880. }
  881. void ast_rtp_get_peer(struct ast_rtp *rtp, struct sockaddr_in *them)
  882. {
  883. them->sin_family = AF_INET;
  884. them->sin_port = rtp->them.sin_port;
  885. them->sin_addr = rtp->them.sin_addr;
  886. }
  887. void ast_rtp_get_us(struct ast_rtp *rtp, struct sockaddr_in *us)
  888. {
  889. memcpy(us, &rtp->us, sizeof(rtp->us));
  890. }
  891. void ast_rtp_stop(struct ast_rtp *rtp)
  892. {
  893. memset(&rtp->them.sin_addr, 0, sizeof(rtp->them.sin_addr));
  894. memset(&rtp->them.sin_port, 0, sizeof(rtp->them.sin_port));
  895. if (rtp->rtcp) {
  896. memset(&rtp->rtcp->them.sin_addr, 0, sizeof(rtp->them.sin_addr));
  897. memset(&rtp->rtcp->them.sin_port, 0, sizeof(rtp->them.sin_port));
  898. }
  899. }
  900. void ast_rtp_destroy(struct ast_rtp *rtp)
  901. {
  902. if (rtp->smoother)
  903. ast_smoother_free(rtp->smoother);
  904. if (rtp->ioid)
  905. ast_io_remove(rtp->io, rtp->ioid);
  906. if (rtp->s > -1)
  907. close(rtp->s);
  908. if (rtp->rtcp) {
  909. close(rtp->rtcp->s);
  910. free(rtp->rtcp);
  911. }
  912. free(rtp);
  913. }
  914. static unsigned int calc_txstamp(struct ast_rtp *rtp, struct timeval *delivery)
  915. {
  916. struct timeval now;
  917. unsigned int ms;
  918. if (!rtp->txcore.tv_sec && !rtp->txcore.tv_usec) {
  919. gettimeofday(&rtp->txcore, NULL);
  920. /* Round to 20ms for nice, pretty timestamps */
  921. rtp->txcore.tv_usec -= rtp->txcore.tv_usec % 20000;
  922. }
  923. if (delivery && (delivery->tv_sec || delivery->tv_usec)) {
  924. /* Use previous txcore */
  925. ms = (delivery->tv_sec - rtp->txcore.tv_sec) * 1000;
  926. ms += (1000000 + delivery->tv_usec - rtp->txcore.tv_usec) / 1000 - 1000;
  927. rtp->txcore.tv_sec = delivery->tv_sec;
  928. rtp->txcore.tv_usec = delivery->tv_usec;
  929. } else {
  930. gettimeofday(&now, NULL);
  931. ms = (now.tv_sec - rtp->txcore.tv_sec) * 1000;
  932. ms += (1000000 + now.tv_usec - rtp->txcore.tv_usec) / 1000 - 1000;
  933. /* Use what we just got for next time */
  934. rtp->txcore.tv_sec = now.tv_sec;
  935. rtp->txcore.tv_usec = now.tv_usec;
  936. }
  937. return ms;
  938. }
  939. int ast_rtp_senddigit(struct ast_rtp *rtp, char digit)
  940. {
  941. unsigned int *rtpheader;
  942. int hdrlen = 12;
  943. int res;
  944. int x;
  945. int payload;
  946. char data[256];
  947. char iabuf[INET_ADDRSTRLEN];
  948. if ((digit <= '9') && (digit >= '0'))
  949. digit -= '0';
  950. else if (digit == '*')
  951. digit = 10;
  952. else if (digit == '#')
  953. digit = 11;
  954. else if ((digit >= 'A') && (digit <= 'D'))
  955. digit = digit - 'A' + 12;
  956. else if ((digit >= 'a') && (digit <= 'd'))
  957. digit = digit - 'a' + 12;
  958. else {
  959. ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
  960. return -1;
  961. }
  962. payload = ast_rtp_lookup_code(rtp, 0, AST_RTP_DTMF);
  963. /* If we have no peer, return immediately */
  964. if (!rtp->them.sin_addr.s_addr)
  965. return 0;
  966. gettimeofday(&rtp->dtmfmute, NULL);
  967. rtp->dtmfmute.tv_usec += (500 * 1000);
  968. if (rtp->dtmfmute.tv_usec > 1000000) {
  969. rtp->dtmfmute.tv_usec -= 1000000;
  970. rtp->dtmfmute.tv_sec += 1;
  971. }
  972. /* Get a pointer to the header */
  973. rtpheader = (unsigned int *)data;
  974. rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno++));
  975. rtpheader[1] = htonl(rtp->lastts);
  976. rtpheader[2] = htonl(rtp->ssrc);
  977. rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (0));
  978. for (x=0;x<6;x++) {
  979. if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
  980. res = sendto(rtp->s, (void *)rtpheader, hdrlen + 4, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
  981. if (res <0)
  982. ast_log(LOG_NOTICE, "RTP Transmission error to %s:%d: %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr), ntohs(rtp->them.sin_port), strerror(errno));
  983. #if 0
  984. printf("Sent %d bytes of RTP data to %s:%d\n", res, ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr), ntohs(rtp->them.sin_port));
  985. #endif
  986. }
  987. if (x == 2) {
  988. /* Clear marker bit and increment seqno */
  989. rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno++));
  990. /* Make duration 800 (100ms) */
  991. rtpheader[3] |= htonl((800));
  992. /* Set the End bit for the last 3 */
  993. rtpheader[3] |= htonl((1 << 23));
  994. } else if ( x < 5) {
  995. rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno++));
  996. }
  997. }
  998. return 0;
  999. }
  1000. static int ast_rtp_raw_write(struct ast_rtp *rtp, struct ast_frame *f, int codec)
  1001. {
  1002. unsigned int *rtpheader;
  1003. char iabuf[INET_ADDRSTRLEN];
  1004. int hdrlen = 12;
  1005. int res;
  1006. int ms;
  1007. int pred;
  1008. int mark = 0;
  1009. ms = calc_txstamp(rtp, &f->delivery);
  1010. /* Default prediction */
  1011. if (f->subclass < AST_FORMAT_MAX_AUDIO) {
  1012. pred = rtp->lastts + ms * 8;
  1013. switch(f->subclass) {
  1014. case AST_FORMAT_ULAW:
  1015. case AST_FORMAT_ALAW:
  1016. /* If we're within +/- 20ms from when where we
  1017. predict we should be, use that */
  1018. pred = rtp->lastts + f->datalen;
  1019. break;
  1020. case AST_FORMAT_ADPCM:
  1021. case AST_FORMAT_G726:
  1022. /* If we're within +/- 20ms from when where we
  1023. predict we should be, use that */
  1024. pred = rtp->lastts + f->datalen * 2;
  1025. break;
  1026. case AST_FORMAT_G729A:
  1027. pred = rtp->lastts + f->datalen * 8;
  1028. break;
  1029. case AST_FORMAT_GSM:
  1030. pred = rtp->lastts + (f->datalen * 160 / 33);
  1031. break;
  1032. case AST_FORMAT_ILBC:
  1033. pred = rtp->lastts + (f->datalen * 240 / 50);
  1034. break;
  1035. case AST_FORMAT_G723_1:
  1036. pred = rtp->lastts + g723_samples(f->data, f->datalen);
  1037. break;
  1038. case AST_FORMAT_SPEEX:
  1039. pred = rtp->lastts + 160;
  1040. /* assumes that the RTP packet contains one Speex frame */
  1041. break;
  1042. case AST_FORMAT_LPC10:
  1043. /* assumes that the RTP packet contains one LPC10 frame */
  1044. pred = rtp->lastts + 22 * 8;
  1045. pred += (((char *)(f->data))[7] & 0x1) * 8;
  1046. break;
  1047. default:
  1048. ast_log(LOG_WARNING, "Not sure about timestamp format for codec format %s\n", ast_getformatname(f->subclass));
  1049. }
  1050. /* Re-calculate last TS */
  1051. rtp->lastts = rtp->lastts + ms * 8;
  1052. if (!f->delivery.tv_sec && !f->delivery.tv_usec) {
  1053. /* If this isn't an absolute delivery time, Check if it is close to our prediction,
  1054. and if so, go with our prediction */
  1055. if (abs(rtp->lastts - pred) < MAX_TIMESTAMP_SKEW)
  1056. rtp->lastts = pred;
  1057. else {
  1058. ast_log(LOG_DEBUG, "Difference is %d, ms is %d\n", abs(rtp->lastts - pred), ms);
  1059. mark = 1;
  1060. }
  1061. }
  1062. } else {
  1063. mark = f->subclass & 0x1;
  1064. pred = rtp->lastovidtimestamp + f->samples;
  1065. /* Re-calculate last TS */
  1066. rtp->lastts = rtp->lastts + ms * 90;
  1067. /* If it's close to our prediction, go for it */
  1068. if (!f->delivery.tv_sec && !f->delivery.tv_usec) {
  1069. if (abs(rtp->lastts - pred) < 7200) {
  1070. rtp->lastts = pred;
  1071. rtp->lastovidtimestamp += f->samples;
  1072. } else {
  1073. ast_log(LOG_DEBUG, "Difference is %d, ms is %d (%d), pred/ts/samples %d/%d/%d\n", abs(rtp->lastts - pred), ms, ms * 90, rtp->lastts, pred, f->samples);
  1074. rtp->lastovidtimestamp = rtp->lastts;
  1075. }
  1076. }
  1077. }
  1078. /* Get a pointer to the header */
  1079. rtpheader = (unsigned int *)(f->data - hdrlen);
  1080. rtpheader[0] = htonl((2 << 30) | (codec << 16) | (rtp->seqno++) | (mark << 23));
  1081. rtpheader[1] = htonl(rtp->lastts);
  1082. rtpheader[2] = htonl(rtp->ssrc);
  1083. if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
  1084. res = sendto(rtp->s, (void *)rtpheader, f->datalen + hdrlen, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
  1085. if (res <0)
  1086. ast_log(LOG_NOTICE, "RTP Transmission error to %s:%d: %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr), ntohs(rtp->them.sin_port), strerror(errno));
  1087. #if 0
  1088. printf("Sent %d bytes of RTP data to %s:%d\n", res, ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr), ntohs(rtp->them.sin_port));
  1089. #endif
  1090. }
  1091. return 0;
  1092. }
  1093. int ast_rtp_write(struct ast_rtp *rtp, struct ast_frame *_f)
  1094. {
  1095. struct ast_frame *f;
  1096. int codec;
  1097. int hdrlen = 12;
  1098. int subclass;
  1099. /* If we have no peer, return immediately */
  1100. if (!rtp->them.sin_addr.s_addr)
  1101. return 0;
  1102. /* If there is no data length, return immediately */
  1103. if (!_f->datalen)
  1104. return 0;
  1105. /* Make sure we have enough space for RTP header */
  1106. if ((_f->frametype != AST_FRAME_VOICE) && (_f->frametype != AST_FRAME_VIDEO)) {
  1107. ast_log(LOG_WARNING, "RTP can only send voice\n");
  1108. return -1;
  1109. }
  1110. subclass = _f->subclass;
  1111. if (_f->frametype == AST_FRAME_VIDEO)
  1112. subclass &= ~0x1;
  1113. codec = ast_rtp_lookup_code(rtp, 1, subclass);
  1114. if (codec < 0) {
  1115. ast_log(LOG_WARNING, "Don't know how to send format %s packets with RTP\n", ast_getformatname(_f->subclass));
  1116. return -1;
  1117. }
  1118. if (rtp->lasttxformat != subclass) {
  1119. /* New format, reset the smoother */
  1120. ast_log(LOG_DEBUG, "Ooh, format changed from %s to %s\n", ast_getformatname(rtp->lasttxformat), ast_getformatname(subclass));
  1121. rtp->lasttxformat = subclass;
  1122. if (rtp->smoother)
  1123. ast_smoother_free(rtp->smoother);
  1124. rtp->smoother = NULL;
  1125. }
  1126. switch(subclass) {
  1127. case AST_FORMAT_SLINEAR:
  1128. if (!rtp->smoother) {
  1129. rtp->smoother = ast_smoother_new(320);
  1130. }
  1131. if (!rtp->smoother) {
  1132. ast_log(LOG_WARNING, "Unable to create smoother :(\n");
  1133. return -1;
  1134. }
  1135. ast_smoother_feed_be(rtp->smoother, _f);
  1136. while((f = ast_smoother_read(rtp->smoother)))
  1137. ast_rtp_raw_write(rtp, f, codec);
  1138. break;
  1139. case AST_FORMAT_ULAW:
  1140. case AST_FORMAT_ALAW:
  1141. if (!rtp->smoother) {
  1142. rtp->smoother = ast_smoother_new(160);
  1143. }
  1144. if (!rtp->smoother) {
  1145. ast_log(LOG_WARNING, "Unable to create smoother :(\n");
  1146. return -1;
  1147. }
  1148. ast_smoother_feed(rtp->smoother, _f);
  1149. while((f = ast_smoother_read(rtp->smoother)))
  1150. ast_rtp_raw_write(rtp, f, codec);
  1151. break;
  1152. case AST_FORMAT_ADPCM:
  1153. case AST_FORMAT_G726:
  1154. if (!rtp->smoother) {
  1155. rtp->smoother = ast_smoother_new(80);
  1156. }
  1157. if (!rtp->smoother) {
  1158. ast_log(LOG_WARNING, "Unable to create smoother :(\n");
  1159. return -1;
  1160. }
  1161. ast_smoother_feed(rtp->smoother, _f);
  1162. while((f = ast_smoother_read(rtp->smoother)))
  1163. ast_rtp_raw_write(rtp, f, codec);
  1164. break;
  1165. case AST_FORMAT_G729A:
  1166. if (!rtp->smoother) {
  1167. rtp->smoother = ast_smoother_new(20);
  1168. if (rtp->smoother)
  1169. ast_smoother_set_flags(rtp->smoother, AST_SMOOTHER_FLAG_G729);
  1170. }
  1171. if (!rtp->smoother) {
  1172. ast_log(LOG_WARNING, "Unable to create g729 smoother :(\n");
  1173. return -1;
  1174. }
  1175. ast_smoother_feed(rtp->smoother, _f);
  1176. while((f = ast_smoother_read(rtp->smoother)))
  1177. ast_rtp_raw_write(rtp, f, codec);
  1178. break;
  1179. case AST_FORMAT_GSM:
  1180. if (!rtp->smoother) {
  1181. rtp->smoother = ast_smoother_new(33);
  1182. }
  1183. if (!rtp->smoother) {
  1184. ast_log(LOG_WARNING, "Unable to create GSM smoother :(\n");
  1185. return -1;
  1186. }
  1187. ast_smoother_feed(rtp->smoother, _f);
  1188. while((f = ast_smoother_read(rtp->smoother)))
  1189. ast_rtp_raw_write(rtp, f, codec);
  1190. break;
  1191. case AST_FORMAT_ILBC:
  1192. if (!rtp->smoother) {
  1193. rtp->smoother = ast_smoother_new(50);
  1194. }
  1195. if (!rtp->smoother) {
  1196. ast_log(LOG_WARNING, "Unable to create ILBC smoother :(\n");
  1197. return -1;
  1198. }
  1199. ast_smoother_feed(rtp->smoother, _f);
  1200. while((f = ast_smoother_read(rtp->smoother)))
  1201. ast_rtp_raw_write(rtp, f, codec);
  1202. break;
  1203. default:
  1204. ast_log(LOG_WARNING, "Not sure about sending format %s packets\n", ast_getformatname(subclass));
  1205. /* fall through to... */
  1206. case AST_FORMAT_H261:
  1207. case AST_FORMAT_H263:
  1208. case AST_FORMAT_G723_1:
  1209. case AST_FORMAT_LPC10:
  1210. case AST_FORMAT_SPEEX:
  1211. /* Don't buffer outgoing frames; send them one-per-packet: */
  1212. if (_f->offset < hdrlen) {
  1213. f = ast_frdup(_f);
  1214. } else {
  1215. f = _f;
  1216. }
  1217. ast_rtp_raw_write(rtp, f, codec);
  1218. }
  1219. return 0;
  1220. }
  1221. void ast_rtp_proto_unregister(struct ast_rtp_protocol *proto)
  1222. {
  1223. struct ast_rtp_protocol *cur, *prev;
  1224. cur = protos;
  1225. prev = NULL;
  1226. while(cur) {
  1227. if (cur == proto) {
  1228. if (prev)
  1229. prev->next = proto->next;
  1230. else
  1231. protos = proto->next;
  1232. return;
  1233. }
  1234. prev = cur;
  1235. cur = cur->next;
  1236. }
  1237. }
  1238. int ast_rtp_proto_register(struct ast_rtp_protocol *proto)
  1239. {
  1240. struct ast_rtp_protocol *cur;
  1241. cur = protos;
  1242. while(cur) {
  1243. if (cur->type == proto->type) {
  1244. ast_log(LOG_WARNING, "Tried to register same protocol '%s' twice\n", cur->type);
  1245. return -1;
  1246. }
  1247. cur = cur->next;
  1248. }
  1249. proto->next = protos;
  1250. protos = proto;
  1251. return 0;
  1252. }
  1253. static struct ast_rtp_protocol *get_proto(struct ast_channel *chan)
  1254. {
  1255. struct ast_rtp_protocol *cur;
  1256. cur = protos;
  1257. while(cur) {
  1258. if (cur->type == chan->type) {
  1259. return cur;
  1260. }
  1261. cur = cur->next;
  1262. }
  1263. return NULL;
  1264. }
  1265. int ast_rtp_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
  1266. {
  1267. struct ast_frame *f;
  1268. struct ast_channel *who, *cs[3];
  1269. struct ast_rtp *p0, *p1;
  1270. struct ast_rtp *vp0, *vp1;
  1271. struct ast_rtp_protocol *pr0, *pr1;
  1272. struct sockaddr_in ac0, ac1;
  1273. struct sockaddr_in vac0, vac1;
  1274. struct sockaddr_in t0, t1;
  1275. struct sockaddr_in vt0, vt1;
  1276. char iabuf[INET_ADDRSTRLEN];
  1277. void *pvt0, *pvt1;
  1278. int to;
  1279. int codec0,codec1, oldcodec0, oldcodec1;
  1280. memset(&vt0, 0, sizeof(vt0));
  1281. memset(&vt1, 0, sizeof(vt1));
  1282. memset(&vac0, 0, sizeof(vac0));
  1283. memset(&vac1, 0, sizeof(vac1));
  1284. /* if need DTMF, cant native bridge */
  1285. if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
  1286. return -2;
  1287. ast_mutex_lock(&c0->lock);
  1288. while(ast_mutex_trylock(&c1->lock)) {
  1289. ast_mutex_unlock(&c0->lock);
  1290. usleep(1);
  1291. ast_mutex_lock(&c0->lock);
  1292. }
  1293. pr0 = get_proto(c0);
  1294. pr1 = get_proto(c1);
  1295. if (!pr0) {
  1296. ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c0->name);
  1297. ast_mutex_unlock(&c0->lock);
  1298. ast_mutex_unlock(&c1->lock);
  1299. return -1;
  1300. }
  1301. if (!pr1) {
  1302. ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c1->name);
  1303. ast_mutex_unlock(&c0->lock);
  1304. ast_mutex_unlock(&c1->lock);
  1305. return -1;
  1306. }
  1307. pvt0 = c0->pvt->pvt;
  1308. pvt1 = c1->pvt->pvt;
  1309. p0 = pr0->get_rtp_info(c0);
  1310. if (pr0->get_vrtp_info)
  1311. vp0 = pr0->get_vrtp_info(c0);
  1312. else
  1313. vp0 = NULL;
  1314. p1 = pr1->get_rtp_info(c1);
  1315. if (pr1->get_vrtp_info)
  1316. vp1 = pr1->get_vrtp_info(c1);
  1317. else
  1318. vp1 = NULL;
  1319. if (!p0 || !p1) {
  1320. /* Somebody doesn't want to play... */
  1321. ast_mutex_unlock(&c0->lock);
  1322. ast_mutex_unlock(&c1->lock);
  1323. return -2;
  1324. }
  1325. if (pr0->get_codec)
  1326. codec0 = pr0->get_codec(c0);
  1327. else
  1328. codec0 = 0;
  1329. if (pr1->get_codec)
  1330. codec1 = pr1->get_codec(c1);
  1331. else
  1332. codec1 = 0;
  1333. if (pr0->get_codec && pr1->get_codec) {
  1334. /* Hey, we can't do reinvite if both parties speak diffrent codecs */
  1335. if (!(codec0 & codec1)) {
  1336. ast_log(LOG_WARNING, "codec0 = %d is not codec1 = %d, cannot native bridge.\n",codec0,codec1);
  1337. ast_mutex_unlock(&c0->lock);
  1338. ast_mutex_unlock(&c1->lock);
  1339. return -2;
  1340. }
  1341. }
  1342. if (pr0->set_rtp_peer(c0, p1, vp1, codec1))
  1343. ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
  1344. else {
  1345. /* Store RTP peer */
  1346. ast_rtp_get_peer(p1, &ac1);
  1347. if (vp1)
  1348. ast_rtp_get_peer(vp1, &vac1);
  1349. }
  1350. if (pr1->set_rtp_peer(c1, p0, vp0, codec0))
  1351. ast_log(LOG_WARNING, "Channel '%s' failed to talk back to '%s'\n", c1->name, c0->name);
  1352. else {
  1353. /* Store RTP peer */
  1354. ast_rtp_get_peer(p0, &ac0);
  1355. if (vp0)
  1356. ast_rtp_get_peer(vp0, &vac0);
  1357. }
  1358. ast_mutex_unlock(&c0->lock);
  1359. ast_mutex_unlock(&c1->lock);
  1360. cs[0] = c0;
  1361. cs[1] = c1;
  1362. cs[2] = NULL;
  1363. oldcodec0 = codec0;
  1364. oldcodec1 = codec1;
  1365. for (;;) {
  1366. if ((c0->pvt->pvt != pvt0) ||
  1367. (c1->pvt->pvt != pvt1) ||
  1368. (c0->masq || c0->masqr || c1->masq || c1->masqr)) {
  1369. ast_log(LOG_DEBUG, "Oooh, something is weird, backing out\n");
  1370. if (c0->pvt->pvt == pvt0) {
  1371. if (pr0->set_rtp_peer(c0, NULL, NULL, 0))
  1372. ast_log(LOG_WARNING, "Channel '%s' failed to revert\n", c0->name);
  1373. }
  1374. if (c1->pvt->pvt == pvt1) {
  1375. if (pr1->set_rtp_peer(c1, NULL, NULL, 0))
  1376. ast_log(LOG_WARNING, "Channel '%s' failed to revert back\n", c1->name);
  1377. }
  1378. /* Tell it to try again later */
  1379. return -3;
  1380. }
  1381. to = -1;
  1382. ast_rtp_get_peer(p1, &t1);
  1383. ast_rtp_get_peer(p0, &t0);
  1384. if (pr0->get_codec)
  1385. codec0 = pr0->get_codec(c0);
  1386. if (pr1->get_codec)
  1387. codec1 = pr1->get_codec(c1);
  1388. if (vp1)
  1389. ast_rtp_get_peer(vp1, &vt1);
  1390. if (vp0)
  1391. ast_rtp_get_peer(vp0, &vt0);
  1392. if (inaddrcmp(&t1, &ac1) || (vp1 && inaddrcmp(&vt1, &vac1)) || (codec1 != oldcodec1)) {
  1393. ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
  1394. c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), t1.sin_addr), ntohs(t1.sin_port), codec1);
  1395. ast_log(LOG_DEBUG, "Oooh, '%s' changed end vaddress to %s:%d (format %d)\n",
  1396. c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), vt1.sin_addr), ntohs(vt1.sin_port), codec1);
  1397. ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d/(format %d)\n",
  1398. c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), ac1.sin_addr), ntohs(ac1.sin_port), oldcodec1);
  1399. ast_log(LOG_DEBUG, "Oooh, '%s' wasv %s:%d/(format %d)\n",
  1400. c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), vac1.sin_addr), ntohs(vac1.sin_port), oldcodec1);
  1401. if (pr0->set_rtp_peer(c0, t1.sin_addr.s_addr ? p1 : NULL, vt1.sin_addr.s_addr ? vp1 : NULL, codec1))
  1402. ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c0->name, c1->name);
  1403. memcpy(&ac1, &t1, sizeof(ac1));
  1404. memcpy(&vac1, &vt1, sizeof(vac1));
  1405. oldcodec1 = codec1;
  1406. }
  1407. if (inaddrcmp(&t0, &ac0) || (vp0 && inaddrcmp(&vt0, &vac0))) {
  1408. ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
  1409. c0->name, ast_inet_ntoa(iabuf, sizeof(iabuf), t0.sin_addr), ntohs(t0.sin_port), codec0);
  1410. ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d/(format %d)\n",
  1411. c0->name, ast_inet_ntoa(iabuf, sizeof(iabuf), ac0.sin_addr), ntohs(ac0.sin_port), oldcodec0);
  1412. if (pr1->set_rtp_peer(c1, t0.sin_addr.s_addr ? p0 : NULL, vt0.sin_addr.s_addr ? vp0 : NULL, codec0))
  1413. ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c1->name, c0->name);
  1414. memcpy(&ac0, &t0, sizeof(ac0));
  1415. memcpy(&vac0, &vt0, sizeof(vac0));
  1416. oldcodec0 = codec0;
  1417. }
  1418. who = ast_waitfor_n(cs, 2, &to);
  1419. if (!who) {
  1420. ast_log(LOG_DEBUG, "Ooh, empty read...\n");
  1421. /* check for hagnup / whentohangup */
  1422. if (ast_check_hangup(c0) || ast_check_hangup(c1))
  1423. break;
  1424. continue;
  1425. }
  1426. f = ast_read(who);
  1427. if (!f || ((f->frametype == AST_FRAME_DTMF) &&
  1428. (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
  1429. ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))))) {
  1430. *fo = f;
  1431. *rc = who;
  1432. ast_log(LOG_DEBUG, "Oooh, got a %s\n", f ? "digit" : "hangup");
  1433. if ((c0->pvt->pvt == pvt0) && (!c0->_softhangup)) {
  1434. if (pr0->set_rtp_peer(c0, NULL, NULL, 0))
  1435. ast_log(LOG_WARNING, "Channel '%s' failed to revert\n", c0->name);
  1436. }
  1437. if ((c1->pvt->pvt == pvt1) && (!c1->_softhangup)) {
  1438. if (pr1->set_rtp_peer(c1, NULL, NULL, 0))
  1439. ast_log(LOG_WARNING, "Channel '%s' failed to revert back\n", c1->name);
  1440. }
  1441. /* That's all we needed */
  1442. return 0;
  1443. } else {
  1444. if ((f->frametype == AST_FRAME_DTMF) ||
  1445. (f->frametype == AST_FRAME_VOICE) ||
  1446. (f->frametype == AST_FRAME_VIDEO)) {
  1447. /* Forward voice or DTMF frames if they happen upon us */
  1448. if (who == c0) {
  1449. ast_write(c1, f);
  1450. } else if (who == c1) {
  1451. ast_write(c0, f);
  1452. }
  1453. }
  1454. ast_frfree(f);
  1455. }
  1456. /* Swap priority not that it's a big deal at this point */
  1457. cs[2] = cs[0];
  1458. cs[0] = cs[1];
  1459. cs[1] = cs[2];
  1460. }
  1461. return -1;
  1462. }
  1463. void ast_rtp_reload(void)
  1464. {
  1465. struct ast_config *cfg;
  1466. char *s;
  1467. rtpstart = 5000;
  1468. rtpend = 31000;
  1469. cfg = ast_load("rtp.conf");
  1470. if (cfg) {
  1471. if ((s = ast_variable_retrieve(cfg, "general", "rtpstart"))) {
  1472. rtpstart = atoi(s);
  1473. if (rtpstart < 1024)
  1474. rtpstart = 1024;
  1475. if (rtpstart > 65535)
  1476. rtpstart = 65535;
  1477. }
  1478. if ((s = ast_variable_retrieve(cfg, "general", "rtpend"))) {
  1479. rtpend = atoi(s);
  1480. if (rtpend < 1024)
  1481. rtpend = 1024;
  1482. if (rtpend > 65535)
  1483. rtpend = 65535;
  1484. }
  1485. if ((s = ast_variable_retrieve(cfg, "general", "rtpchecksums"))) {
  1486. #ifdef SO_NO_CHECK
  1487. if (ast_false(s))
  1488. nochecksums = 1;
  1489. else
  1490. nochecksums = 0;
  1491. #else
  1492. if (ast_false(s))
  1493. ast_log(LOG_WARNING, "Disabling RTP checksums is not supported on this operating system!\n");
  1494. #endif
  1495. }
  1496. ast_destroy(cfg);
  1497. }
  1498. if (rtpstart >= rtpend) {
  1499. ast_log(LOG_WARNING, "Unreasonable values for RTP start/end\n");
  1500. rtpstart = 5000;
  1501. rtpend = 31000;
  1502. }
  1503. if (option_verbose > 1)
  1504. ast_verbose(VERBOSE_PREFIX_2 "RTP Allocating from port range %d -> %d\n", rtpstart, rtpend);
  1505. }
  1506. void ast_rtp_init(void)
  1507. {
  1508. ast_rtp_reload();
  1509. }