2 * Asterisk -- A telephony toolkit for Linux.
4 * Real-time Protocol Support
5 * Supports RTP and RTCP with Symmetric RTP support for NAT
8 * Copyright (C) 1999-2004, Digium, Inc.
10 * Mark Spencer <markster@digium.com>
12 * This program is free software, distributed under the terms of
13 * the GNU General Public License
23 #include <netinet/in.h>
25 #include <sys/socket.h>
26 #include <arpa/inet.h>
29 #include <asterisk/rtp.h>
30 #include <asterisk/frame.h>
31 #include <asterisk/logger.h>
32 #include <asterisk/options.h>
33 #include <asterisk/channel.h>
34 #include <asterisk/acl.h>
35 #include <asterisk/channel.h>
36 #include <asterisk/channel_pvt.h>
37 #include <asterisk/config.h>
38 #include <asterisk/lock.h>
39 #include <asterisk/utils.h>
40 #include <asterisk/cli.h>
42 #define MAX_TIMESTAMP_SKEW 640
48 #define TYPE_SILENCE 0x2
49 #define TYPE_DONTSEND 0x3
52 static int dtmftimeout = 3000; /* 3000 samples */
54 static int rtpstart = 0;
55 static int rtpend = 0;
56 static int rtpdebug = 0; /* Are we debugging? */
57 static struct sockaddr_in rtpdebugaddr; /* Debug packets to/from this host */
59 static int checksums = 1;
62 /* The value of each payload format mapping: */
63 struct rtpPayloadType {
64 int isAstFormat; /* whether the following code is an AST_FORMAT */
68 #define MAX_RTP_PT 256
70 #define FLAG_3389_WARNING (1 << 0)
76 unsigned char rawdata[8192 + AST_FRIENDLY_OFFSET];
79 unsigned int lastrxts;
80 unsigned int lastividtimestamp;
81 unsigned int lastovidtimestamp;
82 unsigned int lasteventseqn;
86 unsigned int dtmfduration;
89 struct sockaddr_in us;
90 struct sockaddr_in them;
91 struct timeval rxcore;
92 struct timeval txcore;
93 struct timeval dtmfmute;
94 struct ast_smoother *smoother;
97 unsigned short rxseqno;
98 struct sched_context *sched;
99 struct io_context *io;
101 ast_rtp_callback callback;
102 struct rtpPayloadType current_RTP_PT[MAX_RTP_PT];
103 int rtp_lookup_code_cache_isAstFormat; /* a cache for the result of rtp_lookup_code(): */
104 int rtp_lookup_code_cache_code;
105 int rtp_lookup_code_cache_result;
106 int rtp_offered_from_local;
107 struct ast_rtcp *rtcp;
112 struct sockaddr_in us;
113 struct sockaddr_in them;
116 static struct ast_rtp_protocol *protos = NULL;
118 int ast_rtp_fd(struct ast_rtp *rtp)
123 int ast_rtcp_fd(struct ast_rtp *rtp)
130 static int g723_len(unsigned char buf)
132 switch(buf & TYPE_MASK) {
146 ast_log(LOG_WARNING, "Badly encoded frame (%d)\n", buf & TYPE_MASK);
151 static int g723_samples(unsigned char *buf, int maxlen)
156 while(pos < maxlen) {
157 res = g723_len(buf[pos]);
166 void ast_rtp_set_data(struct ast_rtp *rtp, void *data)
171 void ast_rtp_set_callback(struct ast_rtp *rtp, ast_rtp_callback callback)
173 rtp->callback = callback;
176 void ast_rtp_setnat(struct ast_rtp *rtp, int nat)
181 static struct ast_frame *send_dtmf(struct ast_rtp *rtp)
184 static struct ast_frame null_frame = { AST_FRAME_NULL, };
185 char iabuf[INET_ADDRSTRLEN];
186 gettimeofday(&tv, NULL);
187 if ((tv.tv_sec < rtp->dtmfmute.tv_sec) ||
188 ((tv.tv_sec == rtp->dtmfmute.tv_sec) && (tv.tv_usec < rtp->dtmfmute.tv_usec))) {
189 ast_log(LOG_DEBUG, "Ignore potential DTMF echo from '%s'\n", ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr));
191 rtp->dtmfduration = 0;
194 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));
195 if (rtp->resp == 'X') {
196 rtp->f.frametype = AST_FRAME_CONTROL;
197 rtp->f.subclass = AST_CONTROL_FLASH;
199 rtp->f.frametype = AST_FRAME_DTMF;
200 rtp->f.subclass = rtp->resp;
207 rtp->dtmfduration = 0;
212 static inline int rtp_debug_test_addr(struct sockaddr_in *addr)
216 if (rtpdebugaddr.sin_addr.s_addr) {
217 if (((ntohs(rtpdebugaddr.sin_port) != 0)
218 && (rtpdebugaddr.sin_port != addr->sin_port))
219 || (rtpdebugaddr.sin_addr.s_addr != addr->sin_addr.s_addr))
225 static struct ast_frame *process_cisco_dtmf(struct ast_rtp *rtp, unsigned char *data, int len)
229 struct ast_frame *f = NULL;
230 event = ntohl(*((unsigned int *)(data)));
233 printf("Cisco Digit: %08x (len = %d)\n", event, len);
237 } else if (event < 11) {
239 } else if (event < 12) {
241 } else if (event < 16) {
242 resp = 'A' + (event - 12);
243 } else if (event < 17) {
246 if (rtp->resp && (rtp->resp != resp)) {
250 rtp->dtmfcount = dtmftimeout;
254 static struct ast_frame *process_rfc2833(struct ast_rtp *rtp, unsigned char *data, int len)
257 unsigned int event_end;
258 unsigned int duration;
260 struct ast_frame *f = NULL;
261 event = ntohl(*((unsigned int *)(data)));
263 event_end = ntohl(*((unsigned int *)(data)));
266 duration = ntohl(*((unsigned int *)(data)));
269 printf("Event: %08x (len = %d)\n", event, len);
273 } else if (event < 11) {
275 } else if (event < 12) {
277 } else if (event < 16) {
278 resp = 'A' + (event - 12);
279 } else if (event < 17) {
282 if (rtp->resp && (rtp->resp != resp)) {
285 else if(event_end & 0x80)
294 else if(rtp->dtmfduration && (duration < rtp->dtmfduration))
298 if (!(event_end & 0x80))
300 rtp->dtmfcount = dtmftimeout;
301 rtp->dtmfduration = duration;
305 static struct ast_frame *process_rfc3389(struct ast_rtp *rtp, unsigned char *data, int len)
307 struct ast_frame *f = NULL;
308 /* Convert comfort noise into audio with various codecs. Unfortunately this doesn't
309 totally help us out becuase we don't have an engine to keep it going and we are not
310 guaranteed to have it every 20ms or anything */
312 printf("RFC3389: %d bytes, level %d...\n", len, rtp->lastrxformat);
314 if (!(rtp->flags & FLAG_3389_WARNING)) {
315 ast_log(LOG_NOTICE, "RFC3389 support incomplete. Turn off on client if possible\n");
316 rtp->flags |= FLAG_3389_WARNING;
318 /* Must have at least one byte */
322 rtp->f.data = rtp->rawdata + AST_FRIENDLY_OFFSET;
323 rtp->f.datalen = len - 1;
324 rtp->f.offset = AST_FRIENDLY_OFFSET;
325 memcpy(rtp->f.data, data + 1, len - 1);
331 rtp->f.frametype = AST_FRAME_CNG;
332 rtp->f.subclass = data[0] & 0x7f;
333 rtp->f.datalen = len - 1;
335 rtp->f.delivery.tv_usec = rtp->f.delivery.tv_sec = 0;
340 static int rtpread(int *id, int fd, short events, void *cbdata)
342 struct ast_rtp *rtp = cbdata;
344 f = ast_rtp_read(rtp);
347 rtp->callback(rtp, f, rtp->data);
352 struct ast_frame *ast_rtcp_read(struct ast_rtp *rtp)
354 static struct ast_frame null_frame = { AST_FRAME_NULL, };
358 struct sockaddr_in sin;
359 unsigned int rtcpdata[1024];
360 char iabuf[INET_ADDRSTRLEN];
367 res = recvfrom(rtp->rtcp->s, rtcpdata, sizeof(rtcpdata),
368 0, (struct sockaddr *)&sin, &len);
372 ast_log(LOG_NOTICE, "RTP: Received packet with bad UDP checksum\n");
374 ast_log(LOG_WARNING, "RTP Read error: %s\n", strerror(errno));
381 ast_log(LOG_WARNING, "RTP Read too short\n");
386 /* Send to whoever sent to us */
387 if ((rtp->rtcp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
388 (rtp->rtcp->them.sin_port != sin.sin_port)) {
389 memcpy(&rtp->them, &sin, sizeof(rtp->them));
391 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));
395 ast_log(LOG_DEBUG, "Got RTCP report of %d bytes\n", res);
399 static void calc_rxstamp(struct timeval *tv, struct ast_rtp *rtp, unsigned int timestamp, int mark)
401 if ((!rtp->rxcore.tv_sec && !rtp->rxcore.tv_usec) || mark) {
402 gettimeofday(&rtp->rxcore, NULL);
403 rtp->rxcore.tv_sec -= timestamp / 8000;
404 rtp->rxcore.tv_usec -= (timestamp % 8000) * 125;
405 /* Round to 20ms for nice, pretty timestamps */
406 rtp->rxcore.tv_usec -= rtp->rxcore.tv_usec % 20000;
407 if (rtp->rxcore.tv_usec < 0) {
408 /* Adjust appropriately if necessary */
409 rtp->rxcore.tv_usec += 1000000;
410 rtp->rxcore.tv_sec -= 1;
413 tv->tv_sec = rtp->rxcore.tv_sec + timestamp / 8000;
414 tv->tv_usec = rtp->rxcore.tv_usec + (timestamp % 8000) * 125;
415 if (tv->tv_usec >= 1000000) {
416 tv->tv_usec -= 1000000;
421 struct ast_frame *ast_rtp_read(struct ast_rtp *rtp)
424 struct sockaddr_in sin;
432 char iabuf[INET_ADDRSTRLEN];
433 unsigned int timestamp;
434 unsigned int *rtpheader;
435 static struct ast_frame *f, null_frame = { AST_FRAME_NULL, };
436 struct rtpPayloadType rtpPT;
440 /* Cache where the header will go */
441 res = recvfrom(rtp->s, rtp->rawdata + AST_FRIENDLY_OFFSET, sizeof(rtp->rawdata) - AST_FRIENDLY_OFFSET,
442 0, (struct sockaddr *)&sin, &len);
445 rtpheader = (unsigned int *)(rtp->rawdata + AST_FRIENDLY_OFFSET);
448 ast_log(LOG_NOTICE, "RTP: Received packet with bad UDP checksum\n");
450 ast_log(LOG_WARNING, "RTP Read error: %s\n", strerror(errno));
456 ast_log(LOG_WARNING, "RTP Read too short\n");
460 /* Ignore if the other side hasn't been given an address
462 if (!rtp->them.sin_addr.s_addr || !rtp->them.sin_port)
466 /* Send to whoever sent to us */
467 if ((rtp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
468 (rtp->them.sin_port != sin.sin_port)) {
469 memcpy(&rtp->them, &sin, sizeof(rtp->them));
471 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));
476 seqno = ntohl(rtpheader[0]);
477 payloadtype = (seqno & 0x7f0000) >> 16;
478 mark = seqno & (1 << 23);
479 ext = seqno & (1 << 28);
481 timestamp = ntohl(rtpheader[1]);
483 /* RTP Extension present */
485 hdrlen += (ntohl(rtpheader[3]) & 0xffff) << 2;
489 ast_log(LOG_WARNING, "RTP Read too short (%d, expecting %d)\n", res, hdrlen);
493 if(rtp_debug_test_addr(&sin))
494 ast_verbose("Got RTP packet from %s:%d (type %d, seq %d, ts %d, len %d)\n"
495 , ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), ntohs(sin.sin_port), payloadtype, seqno, timestamp,res - hdrlen);
497 rtpPT = ast_rtp_lookup_pt(rtp, payloadtype);
498 if (!rtpPT.isAstFormat) {
499 /* This is special in-band data that's not one of our codecs */
500 if (rtpPT.code == AST_RTP_DTMF) {
501 /* It's special -- rfc2833 process it */
502 if (rtp->lasteventseqn <= seqno || rtp->resp == 0 || (rtp->lasteventseqn >= 65530 && seqno <= 6)) {
503 f = process_rfc2833(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
504 rtp->lasteventseqn = seqno;
506 if (f) return f; else return &null_frame;
507 } else if (rtpPT.code == AST_RTP_CISCO_DTMF) {
508 /* It's really special -- process it the Cisco way */
509 if (rtp->lasteventseqn <= seqno || rtp->resp == 0 || (rtp->lasteventseqn >= 65530 && seqno <= 6)) {
510 f = process_cisco_dtmf(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
511 rtp->lasteventseqn = seqno;
513 if (f) return f; else return &null_frame;
514 } else if (rtpPT.code == AST_RTP_CN) {
516 f = process_rfc3389(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
517 if (f) return f; else return &null_frame;
519 ast_log(LOG_NOTICE, "Unknown RTP codec %d received\n", payloadtype);
523 rtp->f.subclass = rtpPT.code;
524 if (rtp->f.subclass < AST_FORMAT_MAX_AUDIO)
525 rtp->f.frametype = AST_FRAME_VOICE;
527 rtp->f.frametype = AST_FRAME_VIDEO;
528 rtp->lastrxformat = rtp->f.subclass;
531 rtp->lastrxts = timestamp;
534 for (x=rtp->rxseqno + 1; x < seqno; x++) {
535 /* Queue empty frames */
541 rtp->f.src = "RTPMissedFrame";
544 rtp->rxseqno = seqno;
546 if (rtp->dtmfcount) {
548 printf("dtmfcount was %d\n", rtp->dtmfcount);
550 rtp->dtmfcount -= (timestamp - rtp->lastrxts);
551 if (rtp->dtmfcount < 0)
554 if (dtmftimeout != rtp->dtmfcount)
555 printf("dtmfcount is %d\n", rtp->dtmfcount);
558 rtp->lastrxts = timestamp;
560 /* Send any pending DTMF */
561 if (rtp->resp && !rtp->dtmfcount) {
562 ast_log(LOG_DEBUG, "Sending pending DTMF\n");
563 return send_dtmf(rtp);
566 rtp->f.datalen = res - hdrlen;
567 rtp->f.data = rtp->rawdata + hdrlen + AST_FRIENDLY_OFFSET;
568 rtp->f.offset = hdrlen + AST_FRIENDLY_OFFSET;
569 if (rtp->f.subclass < AST_FORMAT_MAX_AUDIO) {
570 switch(rtp->f.subclass) {
571 case AST_FORMAT_ULAW:
572 case AST_FORMAT_ALAW:
573 rtp->f.samples = rtp->f.datalen;
575 case AST_FORMAT_SLINEAR:
576 rtp->f.samples = rtp->f.datalen / 2;
579 rtp->f.samples = 160 * (rtp->f.datalen / 33);
581 case AST_FORMAT_ILBC:
582 rtp->f.samples = 240 * (rtp->f.datalen / 50);
584 case AST_FORMAT_ADPCM:
585 case AST_FORMAT_G726:
586 rtp->f.samples = rtp->f.datalen * 2;
588 case AST_FORMAT_G729A:
589 rtp->f.samples = rtp->f.datalen * 8;
591 case AST_FORMAT_G723_1:
592 rtp->f.samples = g723_samples(rtp->f.data, rtp->f.datalen);
594 case AST_FORMAT_SPEEX:
595 /* assumes that the RTP packet contained one Speex frame */
596 rtp->f.samples = 160;
598 case AST_FORMAT_LPC10:
599 rtp->f.samples = 22 * 8;
600 rtp->f.samples += (((char *)(rtp->f.data))[7] & 0x1) * 8;
603 ast_log(LOG_NOTICE, "Unable to calculate samples for format %s\n", ast_getformatname(rtp->f.subclass));
606 calc_rxstamp(&rtp->f.delivery, rtp, timestamp, mark);
608 /* Video -- samples is # of samples vs. 90000 */
609 if (!rtp->lastividtimestamp)
610 rtp->lastividtimestamp = timestamp;
611 rtp->f.samples = timestamp - rtp->lastividtimestamp;
612 rtp->lastividtimestamp = timestamp;
613 rtp->f.delivery.tv_sec = 0;
614 rtp->f.delivery.tv_usec = 0;
616 rtp->f.subclass |= 0x1;
623 /* The following array defines the MIME Media type (and subtype) for each
624 of our codecs, or RTP-specific data type. */
626 struct rtpPayloadType payloadType;
630 {{1, AST_FORMAT_G723_1}, "audio", "G723"},
631 {{1, AST_FORMAT_GSM}, "audio", "GSM"},
632 {{1, AST_FORMAT_ULAW}, "audio", "PCMU"},
633 {{1, AST_FORMAT_ALAW}, "audio", "PCMA"},
634 {{1, AST_FORMAT_G726}, "audio", "G726-32"},
635 {{1, AST_FORMAT_ADPCM}, "audio", "DVI4"},
636 {{1, AST_FORMAT_SLINEAR}, "audio", "L16"},
637 {{1, AST_FORMAT_LPC10}, "audio", "LPC"},
638 {{1, AST_FORMAT_G729A}, "audio", "G729"},
639 {{1, AST_FORMAT_SPEEX}, "audio", "speex"},
640 {{1, AST_FORMAT_ILBC}, "audio", "iLBC"},
641 {{0, AST_RTP_DTMF}, "audio", "telephone-event"},
642 {{0, AST_RTP_CISCO_DTMF}, "audio", "cisco-telephone-event"},
643 {{0, AST_RTP_CN}, "audio", "CN"},
644 {{1, AST_FORMAT_JPEG}, "video", "JPEG"},
645 {{1, AST_FORMAT_PNG}, "video", "PNG"},
646 {{1, AST_FORMAT_H261}, "video", "H261"},
647 {{1, AST_FORMAT_H263}, "video", "H263"},
650 /* Static (i.e., well-known) RTP payload types for our "AST_FORMAT..."s:
651 also, our own choices for dynamic payload types. This is our master
652 table for transmission */
653 static struct rtpPayloadType static_RTP_PT[MAX_RTP_PT] = {
654 [0] = {1, AST_FORMAT_ULAW},
655 [2] = {1, AST_FORMAT_G726}, /* Technically this is G.721, but if Cisco can do it, so can we... */
656 [3] = {1, AST_FORMAT_GSM},
657 [4] = {1, AST_FORMAT_G723_1},
658 [5] = {1, AST_FORMAT_ADPCM}, /* 8 kHz */
659 [6] = {1, AST_FORMAT_ADPCM}, /* 16 kHz */
660 [7] = {1, AST_FORMAT_LPC10},
661 [8] = {1, AST_FORMAT_ALAW},
662 [10] = {1, AST_FORMAT_SLINEAR}, /* 2 channels */
663 [11] = {1, AST_FORMAT_SLINEAR}, /* 1 channel */
664 [13] = {0, AST_RTP_CN},
665 [16] = {1, AST_FORMAT_ADPCM}, /* 11.025 kHz */
666 [17] = {1, AST_FORMAT_ADPCM}, /* 22.050 kHz */
667 [18] = {1, AST_FORMAT_G729A},
668 [19] = {0, AST_RTP_CN}, /* Also used for CN */
669 [26] = {1, AST_FORMAT_JPEG},
670 [31] = {1, AST_FORMAT_H261},
671 [34] = {1, AST_FORMAT_H263},
672 [97] = {1, AST_FORMAT_ILBC},
673 [101] = {0, AST_RTP_DTMF},
674 [110] = {1, AST_FORMAT_SPEEX},
675 [121] = {0, AST_RTP_CISCO_DTMF}, /* Must be type 121 */
678 void ast_rtp_pt_clear(struct ast_rtp* rtp)
682 for (i = 0; i < MAX_RTP_PT; ++i) {
683 rtp->current_RTP_PT[i].isAstFormat = 0;
684 rtp->current_RTP_PT[i].code = 0;
687 rtp->rtp_lookup_code_cache_isAstFormat = 0;
688 rtp->rtp_lookup_code_cache_code = 0;
689 rtp->rtp_lookup_code_cache_result = 0;
692 void ast_rtp_pt_default(struct ast_rtp* rtp)
695 /* Initialize to default payload types */
696 for (i = 0; i < MAX_RTP_PT; ++i) {
697 rtp->current_RTP_PT[i].isAstFormat = static_RTP_PT[i].isAstFormat;
698 rtp->current_RTP_PT[i].code = static_RTP_PT[i].code;
701 rtp->rtp_lookup_code_cache_isAstFormat = 0;
702 rtp->rtp_lookup_code_cache_code = 0;
703 rtp->rtp_lookup_code_cache_result = 0;
706 /* Make a note of a RTP payload type that was seen in a SDP "m=" line. */
707 /* By default, use the well-known value for this type (although it may */
708 /* still be set to a different value by a subsequent "a=rtpmap:" line): */
709 void ast_rtp_set_m_type(struct ast_rtp* rtp, int pt) {
710 if (pt < 0 || pt > MAX_RTP_PT) return; /* bogus payload type */
712 if (static_RTP_PT[pt].code != 0) {
713 rtp->current_RTP_PT[pt] = static_RTP_PT[pt];
717 /* Make a note of a RTP payload type (with MIME type) that was seen in */
718 /* a SDP "a=rtpmap:" line. */
719 void ast_rtp_set_rtpmap_type(struct ast_rtp* rtp, int pt,
720 char* mimeType, char* mimeSubtype) {
723 if (pt < 0 || pt > MAX_RTP_PT) return; /* bogus payload type */
725 for (i = 0; i < sizeof mimeTypes/sizeof mimeTypes[0]; ++i) {
726 if (strcasecmp(mimeSubtype, mimeTypes[i].subtype) == 0 &&
727 strcasecmp(mimeType, mimeTypes[i].type) == 0) {
728 rtp->current_RTP_PT[pt] = mimeTypes[i].payloadType;
734 /* Return the union of all of the codecs that were set by rtp_set...() calls */
735 /* They're returned as two distinct sets: AST_FORMATs, and AST_RTPs */
736 void ast_rtp_get_current_formats(struct ast_rtp* rtp,
737 int* astFormats, int* nonAstFormats) {
740 *astFormats = *nonAstFormats = 0;
741 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
742 if (rtp->current_RTP_PT[pt].isAstFormat) {
743 *astFormats |= rtp->current_RTP_PT[pt].code;
745 *nonAstFormats |= rtp->current_RTP_PT[pt].code;
750 void ast_rtp_offered_from_local(struct ast_rtp* rtp, int local) {
752 rtp->rtp_offered_from_local = local;
754 ast_log(LOG_WARNING, "rtp structure is null\n");
757 struct rtpPayloadType ast_rtp_lookup_pt(struct ast_rtp* rtp, int pt)
759 struct rtpPayloadType result;
761 result.isAstFormat = result.code = 0;
762 if (pt < 0 || pt > MAX_RTP_PT) {
763 return result; /* bogus payload type */
765 /* Start with the negotiated codecs */
766 if (!rtp->rtp_offered_from_local)
767 result = rtp->current_RTP_PT[pt];
768 /* If it doesn't exist, check our static RTP type list, just in case */
770 result = static_RTP_PT[pt];
774 /* Looks up an RTP code out of our *static* outbound list */
775 int ast_rtp_lookup_code(struct ast_rtp* rtp, int isAstFormat, int code) {
779 if (isAstFormat == rtp->rtp_lookup_code_cache_isAstFormat &&
780 code == rtp->rtp_lookup_code_cache_code) {
781 /* Use our cached mapping, to avoid the overhead of the loop below */
782 return rtp->rtp_lookup_code_cache_result;
785 /* Check the dynamic list first */
786 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
787 if (rtp->current_RTP_PT[pt].code == code &&
788 rtp->current_RTP_PT[pt].isAstFormat == isAstFormat) {
789 rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
790 rtp->rtp_lookup_code_cache_code = code;
791 rtp->rtp_lookup_code_cache_result = pt;
796 /* Then the static list */
797 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
798 if (static_RTP_PT[pt].code == code &&
799 static_RTP_PT[pt].isAstFormat == isAstFormat) {
800 rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
801 rtp->rtp_lookup_code_cache_code = code;
802 rtp->rtp_lookup_code_cache_result = pt;
809 char* ast_rtp_lookup_mime_subtype(int isAstFormat, int code) {
812 for (i = 0; i < sizeof mimeTypes/sizeof mimeTypes[0]; ++i) {
813 if (mimeTypes[i].payloadType.code == code &&
814 mimeTypes[i].payloadType.isAstFormat == isAstFormat) {
815 return mimeTypes[i].subtype;
821 static int rtp_socket(void)
825 s = socket(AF_INET, SOCK_DGRAM, 0);
827 flags = fcntl(s, F_GETFL);
828 fcntl(s, F_SETFL, flags | O_NONBLOCK);
831 setsockopt(s, SOL_SOCKET, SO_NO_CHECK, &checksums, sizeof(checksums));
838 static struct ast_rtcp *ast_rtcp_new(void)
840 struct ast_rtcp *rtcp;
841 rtcp = malloc(sizeof(struct ast_rtcp));
844 memset(rtcp, 0, sizeof(struct ast_rtcp));
845 rtcp->s = rtp_socket();
846 rtcp->us.sin_family = AF_INET;
849 ast_log(LOG_WARNING, "Unable to allocate socket: %s\n", strerror(errno));
855 struct ast_rtp *ast_rtp_new_with_bindaddr(struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode, struct in_addr addr)
861 rtp = malloc(sizeof(struct ast_rtp));
864 memset(rtp, 0, sizeof(struct ast_rtp));
865 rtp->them.sin_family = AF_INET;
866 rtp->us.sin_family = AF_INET;
867 rtp->s = rtp_socket();
869 rtp->seqno = rand() & 0xffff;
872 ast_log(LOG_WARNING, "Unable to allocate socket: %s\n", strerror(errno));
875 if (sched && rtcpenable) {
877 rtp->rtcp = ast_rtcp_new();
879 /* Find us a place */
880 x = (rand() % (rtpend-rtpstart)) + rtpstart;
884 /* Must be an even port number by RTP spec */
885 rtp->us.sin_port = htons(x);
886 rtp->us.sin_addr = addr;
888 rtp->rtcp->us.sin_port = htons(x + 1);
889 if (!(first = bind(rtp->s, (struct sockaddr *)&rtp->us, sizeof(rtp->us))) &&
890 (!rtp->rtcp || !bind(rtp->rtcp->s, (struct sockaddr *)&rtp->rtcp->us, sizeof(rtp->rtcp->us))))
893 /* Primary bind succeeded! Gotta recreate it */
895 rtp->s = rtp_socket();
897 if (errno != EADDRINUSE) {
898 ast_log(LOG_WARNING, "Unexpected bind error: %s\n", strerror(errno));
909 x = (rtpstart + 1) & ~1;
910 if (x == startplace) {
911 ast_log(LOG_WARNING, "No RTP ports remaining\n");
921 if (io && sched && callbackmode) {
922 /* Operate this one in a callback mode */
925 rtp->ioid = ast_io_add(rtp->io, rtp->s, rtpread, AST_IO_IN, rtp);
927 ast_rtp_pt_default(rtp);
931 struct ast_rtp *ast_rtp_new(struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode)
934 memset(&ia, 0, sizeof(ia));
935 return ast_rtp_new_with_bindaddr(sched, io, rtcpenable, callbackmode, ia);
938 int ast_rtp_settos(struct ast_rtp *rtp, int tos)
941 if ((res = setsockopt(rtp->s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))))
942 ast_log(LOG_WARNING, "Unable to set TOS to %d\n", tos);
946 void ast_rtp_set_peer(struct ast_rtp *rtp, struct sockaddr_in *them)
948 rtp->them.sin_port = them->sin_port;
949 rtp->them.sin_addr = them->sin_addr;
951 rtp->rtcp->them.sin_port = htons(ntohs(them->sin_port) + 1);
952 rtp->rtcp->them.sin_addr = them->sin_addr;
957 void ast_rtp_get_peer(struct ast_rtp *rtp, struct sockaddr_in *them)
959 them->sin_family = AF_INET;
960 them->sin_port = rtp->them.sin_port;
961 them->sin_addr = rtp->them.sin_addr;
964 void ast_rtp_get_us(struct ast_rtp *rtp, struct sockaddr_in *us)
966 memcpy(us, &rtp->us, sizeof(rtp->us));
969 void ast_rtp_stop(struct ast_rtp *rtp)
971 memset(&rtp->them.sin_addr, 0, sizeof(rtp->them.sin_addr));
972 memset(&rtp->them.sin_port, 0, sizeof(rtp->them.sin_port));
974 memset(&rtp->rtcp->them.sin_addr, 0, sizeof(rtp->them.sin_addr));
975 memset(&rtp->rtcp->them.sin_port, 0, sizeof(rtp->them.sin_port));
979 void ast_rtp_reset(struct ast_rtp *rtp)
981 memset(&rtp->rxcore, 0, sizeof(rtp->rxcore));
982 memset(&rtp->txcore, 0, sizeof(rtp->txcore));
983 memset(&rtp->dtmfmute, 0, sizeof(rtp->dtmfmute));
986 rtp->lastividtimestamp = 0;
987 rtp->lastovidtimestamp = 0;
988 rtp->lasteventseqn = 0;
989 rtp->lasttxformat = 0;
990 rtp->lastrxformat = 0;
992 rtp->dtmfduration = 0;
997 void ast_rtp_destroy(struct ast_rtp *rtp)
1000 ast_smoother_free(rtp->smoother);
1002 ast_io_remove(rtp->io, rtp->ioid);
1006 close(rtp->rtcp->s);
1012 static unsigned int calc_txstamp(struct ast_rtp *rtp, struct timeval *delivery)
1016 if (!rtp->txcore.tv_sec && !rtp->txcore.tv_usec) {
1017 gettimeofday(&rtp->txcore, NULL);
1018 /* Round to 20ms for nice, pretty timestamps */
1019 rtp->txcore.tv_usec -= rtp->txcore.tv_usec % 20000;
1021 if (delivery && (delivery->tv_sec || delivery->tv_usec)) {
1022 /* Use previous txcore */
1023 ms = (delivery->tv_sec - rtp->txcore.tv_sec) * 1000;
1024 ms += (1000000 + delivery->tv_usec - rtp->txcore.tv_usec) / 1000 - 1000;
1025 rtp->txcore.tv_sec = delivery->tv_sec;
1026 rtp->txcore.tv_usec = delivery->tv_usec;
1028 gettimeofday(&now, NULL);
1029 ms = (now.tv_sec - rtp->txcore.tv_sec) * 1000;
1030 ms += (1000000 + now.tv_usec - rtp->txcore.tv_usec) / 1000 - 1000;
1031 /* Use what we just got for next time */
1032 rtp->txcore.tv_sec = now.tv_sec;
1033 rtp->txcore.tv_usec = now.tv_usec;
1038 int ast_rtp_senddigit(struct ast_rtp *rtp, char digit)
1040 unsigned int *rtpheader;
1046 char iabuf[INET_ADDRSTRLEN];
1048 if ((digit <= '9') && (digit >= '0'))
1050 else if (digit == '*')
1052 else if (digit == '#')
1054 else if ((digit >= 'A') && (digit <= 'D'))
1055 digit = digit - 'A' + 12;
1056 else if ((digit >= 'a') && (digit <= 'd'))
1057 digit = digit - 'a' + 12;
1059 ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
1062 payload = ast_rtp_lookup_code(rtp, 0, AST_RTP_DTMF);
1064 /* If we have no peer, return immediately */
1065 if (!rtp->them.sin_addr.s_addr)
1068 gettimeofday(&rtp->dtmfmute, NULL);
1069 rtp->dtmfmute.tv_usec += (500 * 1000);
1070 if (rtp->dtmfmute.tv_usec > 1000000) {
1071 rtp->dtmfmute.tv_usec -= 1000000;
1072 rtp->dtmfmute.tv_sec += 1;
1075 /* Get a pointer to the header */
1076 rtpheader = (unsigned int *)data;
1077 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno++));
1078 rtpheader[1] = htonl(rtp->lastts);
1079 rtpheader[2] = htonl(rtp->ssrc);
1080 rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (0));
1082 if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
1083 res = sendto(rtp->s, (void *)rtpheader, hdrlen + 4, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
1085 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));
1086 if(rtp_debug_test_addr(&rtp->them))
1087 ast_verbose("Sent RTP packet to %s:%d (type %d, seq %d, ts %d, len %d)\n"
1088 , ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr), ntohs(rtp->them.sin_port), payload, rtp->seqno, rtp->lastts,res - hdrlen);
1092 /* Clear marker bit and increment seqno */
1093 rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno++));
1094 /* Make duration 800 (100ms) */
1095 rtpheader[3] |= htonl((800));
1096 /* Set the End bit for the last 3 */
1097 rtpheader[3] |= htonl((1 << 23));
1098 } else if ( x < 3) {
1099 rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno++));
1106 static void put_uint32(unsigned char *buf, int i)
1108 unsigned char *c = (unsigned char *)&i;
1110 buf[0] = (i>>24) & 0xff;
1111 buf[1] = (i>>16) & 0xff;
1112 buf[2] = (i>>8) & 0xff;
1116 #define put_uint32(p,v) ((*((unsigned int *)(p))) = (v))
1119 static int ast_rtp_raw_write(struct ast_rtp *rtp, struct ast_frame *f, int codec)
1121 unsigned char *rtpheader;
1122 char iabuf[INET_ADDRSTRLEN];
1129 ms = calc_txstamp(rtp, &f->delivery);
1130 /* Default prediction */
1131 if (f->subclass < AST_FORMAT_MAX_AUDIO) {
1132 pred = rtp->lastts + ms * 8;
1134 switch(f->subclass) {
1135 case AST_FORMAT_ULAW:
1136 case AST_FORMAT_ALAW:
1137 /* If we're within +/- 20ms from when where we
1138 predict we should be, use that */
1139 pred = rtp->lastts + f->datalen;
1141 case AST_FORMAT_ADPCM:
1142 case AST_FORMAT_G726:
1143 /* If we're within +/- 20ms from when where we
1144 predict we should be, use that */
1145 pred = rtp->lastts + f->datalen * 2;
1147 case AST_FORMAT_G729A:
1148 pred = rtp->lastts + f->datalen * 8;
1150 case AST_FORMAT_GSM:
1151 pred = rtp->lastts + (f->datalen * 160 / 33);
1153 case AST_FORMAT_ILBC:
1154 pred = rtp->lastts + (f->datalen * 240 / 50);
1156 case AST_FORMAT_G723_1:
1157 pred = rtp->lastts + g723_samples(f->data, f->datalen);
1159 case AST_FORMAT_SPEEX:
1160 pred = rtp->lastts + 160;
1161 /* assumes that the RTP packet contains one Speex frame */
1163 case AST_FORMAT_LPC10:
1164 /* assumes that the RTP packet contains one LPC10 frame */
1165 pred = rtp->lastts + 22 * 8;
1166 pred += (((char *)(f->data))[7] & 0x1) * 8;
1169 ast_log(LOG_WARNING, "Not sure about timestamp format for codec format %s\n", ast_getformatname(f->subclass));
1171 /* Re-calculate last TS */
1172 rtp->lastts = rtp->lastts + ms * 8;
1173 if (!f->delivery.tv_sec && !f->delivery.tv_usec) {
1174 /* If this isn't an absolute delivery time, Check if it is close to our prediction,
1175 and if so, go with our prediction */
1176 if (abs(rtp->lastts - pred) < MAX_TIMESTAMP_SKEW)
1179 ast_log(LOG_DEBUG, "Difference is %d, ms is %d\n", abs(rtp->lastts - pred), ms);
1184 mark = f->subclass & 0x1;
1185 pred = rtp->lastovidtimestamp + f->samples;
1186 /* Re-calculate last TS */
1187 rtp->lastts = rtp->lastts + ms * 90;
1188 /* If it's close to our prediction, go for it */
1189 if (!f->delivery.tv_sec && !f->delivery.tv_usec) {
1190 if (abs(rtp->lastts - pred) < 7200) {
1192 rtp->lastovidtimestamp += f->samples;
1194 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);
1195 rtp->lastovidtimestamp = rtp->lastts;
1199 /* Get a pointer to the header */
1200 rtpheader = (unsigned char *)(f->data - hdrlen);
1202 put_uint32(rtpheader, htonl((2 << 30) | (codec << 16) | (rtp->seqno) | (mark << 23)));
1203 put_uint32(rtpheader + 4, htonl(rtp->lastts));
1204 put_uint32(rtpheader + 8, htonl(rtp->ssrc));
1208 if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
1209 res = sendto(rtp->s, (void *)rtpheader, f->datalen + hdrlen, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
1211 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));
1212 if(rtp_debug_test_addr(&rtp->them))
1213 ast_verbose("Sent RTP packet to %s:%d (type %d, seq %d, ts %d, len %d)\n"
1214 , ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr), ntohs(rtp->them.sin_port), codec, rtp->seqno, rtp->lastts,res - hdrlen);
1219 int ast_rtp_write(struct ast_rtp *rtp, struct ast_frame *_f)
1221 struct ast_frame *f;
1227 /* If we have no peer, return immediately */
1228 if (!rtp->them.sin_addr.s_addr)
1231 /* If there is no data length, return immediately */
1235 /* Make sure we have enough space for RTP header */
1236 if ((_f->frametype != AST_FRAME_VOICE) && (_f->frametype != AST_FRAME_VIDEO)) {
1237 ast_log(LOG_WARNING, "RTP can only send voice\n");
1241 subclass = _f->subclass;
1242 if (_f->frametype == AST_FRAME_VIDEO)
1245 codec = ast_rtp_lookup_code(rtp, 1, subclass);
1247 ast_log(LOG_WARNING, "Don't know how to send format %s packets with RTP\n", ast_getformatname(_f->subclass));
1251 if (rtp->lasttxformat != subclass) {
1252 /* New format, reset the smoother */
1253 ast_log(LOG_DEBUG, "Ooh, format changed from %s to %s\n", ast_getformatname(rtp->lasttxformat), ast_getformatname(subclass));
1254 rtp->lasttxformat = subclass;
1256 ast_smoother_free(rtp->smoother);
1257 rtp->smoother = NULL;
1262 case AST_FORMAT_ULAW:
1263 case AST_FORMAT_ALAW:
1264 if (!rtp->smoother) {
1265 rtp->smoother = ast_smoother_new(160);
1267 if (!rtp->smoother) {
1268 ast_log(LOG_WARNING, "Unable to create smoother :(\n");
1271 ast_smoother_feed(rtp->smoother, _f);
1273 while((f = ast_smoother_read(rtp->smoother)))
1274 ast_rtp_raw_write(rtp, f, codec);
1276 case AST_FORMAT_ADPCM:
1277 case AST_FORMAT_G726:
1278 if (!rtp->smoother) {
1279 rtp->smoother = ast_smoother_new(80);
1281 if (!rtp->smoother) {
1282 ast_log(LOG_WARNING, "Unable to create smoother :(\n");
1285 ast_smoother_feed(rtp->smoother, _f);
1287 while((f = ast_smoother_read(rtp->smoother)))
1288 ast_rtp_raw_write(rtp, f, codec);
1290 case AST_FORMAT_G729A:
1291 if (!rtp->smoother) {
1292 rtp->smoother = ast_smoother_new(20);
1294 ast_smoother_set_flags(rtp->smoother, AST_SMOOTHER_FLAG_G729);
1296 if (!rtp->smoother) {
1297 ast_log(LOG_WARNING, "Unable to create g729 smoother :(\n");
1300 ast_smoother_feed(rtp->smoother, _f);
1302 while((f = ast_smoother_read(rtp->smoother)))
1303 ast_rtp_raw_write(rtp, f, codec);
1305 case AST_FORMAT_GSM:
1306 if (!rtp->smoother) {
1307 rtp->smoother = ast_smoother_new(33);
1309 if (!rtp->smoother) {
1310 ast_log(LOG_WARNING, "Unable to create GSM smoother :(\n");
1313 ast_smoother_feed(rtp->smoother, _f);
1314 while((f = ast_smoother_read(rtp->smoother)))
1315 ast_rtp_raw_write(rtp, f, codec);
1317 case AST_FORMAT_ILBC:
1318 if (!rtp->smoother) {
1319 rtp->smoother = ast_smoother_new(50);
1321 if (!rtp->smoother) {
1322 ast_log(LOG_WARNING, "Unable to create ILBC smoother :(\n");
1325 ast_smoother_feed(rtp->smoother, _f);
1326 while((f = ast_smoother_read(rtp->smoother)))
1327 ast_rtp_raw_write(rtp, f, codec);
1330 ast_log(LOG_WARNING, "Not sure about sending format %s packets\n", ast_getformatname(subclass));
1331 /* fall through to... */
1332 case AST_FORMAT_H261:
1333 case AST_FORMAT_H263:
1334 case AST_FORMAT_G723_1:
1335 case AST_FORMAT_LPC10:
1336 case AST_FORMAT_SPEEX:
1337 /* Don't buffer outgoing frames; send them one-per-packet: */
1338 if (_f->offset < hdrlen) {
1343 ast_rtp_raw_write(rtp, f, codec);
1349 void ast_rtp_proto_unregister(struct ast_rtp_protocol *proto)
1351 struct ast_rtp_protocol *cur, *prev;
1357 prev->next = proto->next;
1359 protos = proto->next;
1367 int ast_rtp_proto_register(struct ast_rtp_protocol *proto)
1369 struct ast_rtp_protocol *cur;
1372 if (cur->type == proto->type) {
1373 ast_log(LOG_WARNING, "Tried to register same protocol '%s' twice\n", cur->type);
1378 proto->next = protos;
1383 static struct ast_rtp_protocol *get_proto(struct ast_channel *chan)
1385 struct ast_rtp_protocol *cur;
1388 if (cur->type == chan->type) {
1396 int ast_rtp_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
1398 struct ast_frame *f;
1399 struct ast_channel *who, *cs[3];
1400 struct ast_rtp *p0, *p1;
1401 struct ast_rtp *vp0, *vp1;
1402 struct ast_rtp_protocol *pr0, *pr1;
1403 struct sockaddr_in ac0, ac1;
1404 struct sockaddr_in vac0, vac1;
1405 struct sockaddr_in t0, t1;
1406 struct sockaddr_in vt0, vt1;
1407 char iabuf[INET_ADDRSTRLEN];
1411 int codec0,codec1, oldcodec0, oldcodec1;
1413 memset(&vt0, 0, sizeof(vt0));
1414 memset(&vt1, 0, sizeof(vt1));
1415 memset(&vac0, 0, sizeof(vac0));
1416 memset(&vac1, 0, sizeof(vac1));
1418 /* if need DTMF, cant native bridge */
1419 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
1421 ast_mutex_lock(&c0->lock);
1422 while(ast_mutex_trylock(&c1->lock)) {
1423 ast_mutex_unlock(&c0->lock);
1425 ast_mutex_lock(&c0->lock);
1427 pr0 = get_proto(c0);
1428 pr1 = get_proto(c1);
1430 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c0->name);
1431 ast_mutex_unlock(&c0->lock);
1432 ast_mutex_unlock(&c1->lock);
1436 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c1->name);
1437 ast_mutex_unlock(&c0->lock);
1438 ast_mutex_unlock(&c1->lock);
1441 pvt0 = c0->pvt->pvt;
1442 pvt1 = c1->pvt->pvt;
1443 p0 = pr0->get_rtp_info(c0);
1444 if (pr0->get_vrtp_info)
1445 vp0 = pr0->get_vrtp_info(c0);
1448 p1 = pr1->get_rtp_info(c1);
1449 if (pr1->get_vrtp_info)
1450 vp1 = pr1->get_vrtp_info(c1);
1454 /* Somebody doesn't want to play... */
1455 ast_mutex_unlock(&c0->lock);
1456 ast_mutex_unlock(&c1->lock);
1460 codec0 = pr0->get_codec(c0);
1464 codec1 = pr1->get_codec(c1);
1467 if (pr0->get_codec && pr1->get_codec) {
1468 /* Hey, we can't do reinvite if both parties speak diffrent codecs */
1469 if (!(codec0 & codec1)) {
1470 ast_log(LOG_WARNING, "codec0 = %d is not codec1 = %d, cannot native bridge.\n",codec0,codec1);
1471 ast_mutex_unlock(&c0->lock);
1472 ast_mutex_unlock(&c1->lock);
1476 if (pr0->set_rtp_peer(c0, p1, vp1, codec1))
1477 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
1479 /* Store RTP peer */
1480 ast_rtp_get_peer(p1, &ac1);
1482 ast_rtp_get_peer(vp1, &vac1);
1484 if (pr1->set_rtp_peer(c1, p0, vp0, codec0))
1485 ast_log(LOG_WARNING, "Channel '%s' failed to talk back to '%s'\n", c1->name, c0->name);
1487 /* Store RTP peer */
1488 ast_rtp_get_peer(p0, &ac0);
1490 ast_rtp_get_peer(vp0, &vac0);
1492 ast_mutex_unlock(&c0->lock);
1493 ast_mutex_unlock(&c1->lock);
1500 if ((c0->pvt->pvt != pvt0) ||
1501 (c1->pvt->pvt != pvt1) ||
1502 (c0->masq || c0->masqr || c1->masq || c1->masqr)) {
1503 ast_log(LOG_DEBUG, "Oooh, something is weird, backing out\n");
1504 if (c0->pvt->pvt == pvt0) {
1505 if (pr0->set_rtp_peer(c0, NULL, NULL, 0))
1506 ast_log(LOG_WARNING, "Channel '%s' failed to revert\n", c0->name);
1508 if (c1->pvt->pvt == pvt1) {
1509 if (pr1->set_rtp_peer(c1, NULL, NULL, 0))
1510 ast_log(LOG_WARNING, "Channel '%s' failed to revert back\n", c1->name);
1512 /* Tell it to try again later */
1516 ast_rtp_get_peer(p1, &t1);
1517 ast_rtp_get_peer(p0, &t0);
1519 codec0 = pr0->get_codec(c0);
1521 codec1 = pr1->get_codec(c1);
1523 ast_rtp_get_peer(vp1, &vt1);
1525 ast_rtp_get_peer(vp0, &vt0);
1526 if (inaddrcmp(&t1, &ac1) || (vp1 && inaddrcmp(&vt1, &vac1)) || (codec1 != oldcodec1)) {
1527 ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
1528 c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), t1.sin_addr), ntohs(t1.sin_port), codec1);
1529 ast_log(LOG_DEBUG, "Oooh, '%s' changed end vaddress to %s:%d (format %d)\n",
1530 c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), vt1.sin_addr), ntohs(vt1.sin_port), codec1);
1531 ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d/(format %d)\n",
1532 c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), ac1.sin_addr), ntohs(ac1.sin_port), oldcodec1);
1533 ast_log(LOG_DEBUG, "Oooh, '%s' wasv %s:%d/(format %d)\n",
1534 c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), vac1.sin_addr), ntohs(vac1.sin_port), oldcodec1);
1535 if (pr0->set_rtp_peer(c0, t1.sin_addr.s_addr ? p1 : NULL, vt1.sin_addr.s_addr ? vp1 : NULL, codec1))
1536 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c0->name, c1->name);
1537 memcpy(&ac1, &t1, sizeof(ac1));
1538 memcpy(&vac1, &vt1, sizeof(vac1));
1541 if (inaddrcmp(&t0, &ac0) || (vp0 && inaddrcmp(&vt0, &vac0))) {
1542 ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
1543 c0->name, ast_inet_ntoa(iabuf, sizeof(iabuf), t0.sin_addr), ntohs(t0.sin_port), codec0);
1544 ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d/(format %d)\n",
1545 c0->name, ast_inet_ntoa(iabuf, sizeof(iabuf), ac0.sin_addr), ntohs(ac0.sin_port), oldcodec0);
1546 if (pr1->set_rtp_peer(c1, t0.sin_addr.s_addr ? p0 : NULL, vt0.sin_addr.s_addr ? vp0 : NULL, codec0))
1547 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c1->name, c0->name);
1548 memcpy(&ac0, &t0, sizeof(ac0));
1549 memcpy(&vac0, &vt0, sizeof(vac0));
1552 who = ast_waitfor_n(cs, 2, &to);
1554 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
1555 /* check for hagnup / whentohangup */
1556 if (ast_check_hangup(c0) || ast_check_hangup(c1))
1561 if (!f || ((f->frametype == AST_FRAME_DTMF) &&
1562 (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
1563 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))))) {
1566 ast_log(LOG_DEBUG, "Oooh, got a %s\n", f ? "digit" : "hangup");
1567 if ((c0->pvt->pvt == pvt0) && (!c0->_softhangup)) {
1568 if (pr0->set_rtp_peer(c0, NULL, NULL, 0))
1569 ast_log(LOG_WARNING, "Channel '%s' failed to revert\n", c0->name);
1571 if ((c1->pvt->pvt == pvt1) && (!c1->_softhangup)) {
1572 if (pr1->set_rtp_peer(c1, NULL, NULL, 0))
1573 ast_log(LOG_WARNING, "Channel '%s' failed to revert back\n", c1->name);
1575 /* That's all we needed */
1578 if ((f->frametype == AST_FRAME_DTMF) ||
1579 (f->frametype == AST_FRAME_VOICE) ||
1580 (f->frametype == AST_FRAME_VIDEO)) {
1581 /* Forward voice or DTMF frames if they happen upon us */
1584 } else if (who == c1) {
1590 /* Swap priority not that it's a big deal at this point */
1599 static int rtp_do_debug_ip(int fd, int argc, char *argv[])
1602 struct ast_hostent ahp;
1603 char iabuf[INET_ADDRSTRLEN];
1607 return RESULT_SHOWUSAGE;
1609 p = strstr(arg, ":");
1615 hp = ast_gethostbyname(arg, &ahp);
1617 return RESULT_SHOWUSAGE;
1618 rtpdebugaddr.sin_family = AF_INET;
1619 memcpy(&rtpdebugaddr.sin_addr, hp->h_addr, sizeof(rtpdebugaddr.sin_addr));
1620 rtpdebugaddr.sin_port = htons(port);
1622 ast_cli(fd, "RTP Debugging Enabled for IP: %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), rtpdebugaddr.sin_addr));
1624 ast_cli(fd, "RTP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(iabuf, sizeof(iabuf), rtpdebugaddr.sin_addr), port);
1626 return RESULT_SUCCESS;
1629 static int rtp_do_debug(int fd, int argc, char *argv[])
1633 return RESULT_SHOWUSAGE;
1634 return rtp_do_debug_ip(fd, argc, argv);
1637 memset(&rtpdebugaddr,0,sizeof(rtpdebugaddr));
1638 ast_cli(fd, "RTP Debugging Enabled\n");
1639 return RESULT_SUCCESS;
1642 static int rtp_no_debug(int fd, int argc, char *argv[])
1645 return RESULT_SHOWUSAGE;
1647 ast_cli(fd,"RTP Debugging Disabled\n");
1648 return RESULT_SUCCESS;
1651 static char debug_usage[] =
1652 "Usage: rtp debug [ip host[:port]]\n"
1653 " Enable dumping of all RTP packets to and from host.\n";
1654 static char no_debug_usage[] =
1655 "Usage: rtp no debug\n"
1656 " Disable all RTP debugging\n";
1658 static struct ast_cli_entry cli_debug_ip =
1659 {{ "rtp", "debug", "ip", NULL } , rtp_do_debug, "Enable RTP debugging on IP", debug_usage };
1661 static struct ast_cli_entry cli_debug =
1662 {{ "rtp", "debug", NULL } , rtp_do_debug, "Enable RTP debugging", debug_usage };
1664 static struct ast_cli_entry cli_no_debug =
1665 {{ "rtp", "no", "debug", NULL } , rtp_no_debug, "Disable RTP debugging", no_debug_usage };
1667 void ast_rtp_reload(void)
1669 struct ast_config *cfg;
1676 cfg = ast_load("rtp.conf");
1678 if ((s = ast_variable_retrieve(cfg, "general", "rtpstart"))) {
1680 if (rtpstart < 1024)
1682 if (rtpstart > 65535)
1685 if ((s = ast_variable_retrieve(cfg, "general", "rtpend"))) {
1692 if ((s = ast_variable_retrieve(cfg, "general", "rtpchecksums"))) {
1700 ast_log(LOG_WARNING, "Disabling RTP checksums is not supported on this operating system!\n");
1705 if (rtpstart >= rtpend) {
1706 ast_log(LOG_WARNING, "Unreasonable values for RTP start/end\n");
1710 if (option_verbose > 1)
1711 ast_verbose(VERBOSE_PREFIX_2 "RTP Allocating from port range %d -> %d\n", rtpstart, rtpend);
1715 void ast_rtp_init(void)
1717 ast_cli_register(&cli_debug);
1718 ast_cli_register(&cli_debug_ip);
1719 ast_cli_register(&cli_no_debug);