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 - 2005, 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;
90 struct sockaddr_in us;
91 struct sockaddr_in them;
92 struct timeval rxcore;
93 struct timeval txcore;
94 struct timeval dtmfmute;
95 struct ast_smoother *smoother;
98 unsigned short rxseqno;
99 struct sched_context *sched;
100 struct io_context *io;
102 ast_rtp_callback callback;
103 struct rtpPayloadType current_RTP_PT[MAX_RTP_PT];
104 int rtp_lookup_code_cache_isAstFormat; /* a cache for the result of rtp_lookup_code(): */
105 int rtp_lookup_code_cache_code;
106 int rtp_lookup_code_cache_result;
107 int rtp_offered_from_local;
108 struct ast_rtcp *rtcp;
113 struct sockaddr_in us;
114 struct sockaddr_in them;
117 static struct ast_rtp_protocol *protos = NULL;
119 int ast_rtp_fd(struct ast_rtp *rtp)
124 int ast_rtcp_fd(struct ast_rtp *rtp)
131 static int g723_len(unsigned char buf)
133 switch(buf & TYPE_MASK) {
147 ast_log(LOG_WARNING, "Badly encoded frame (%d)\n", buf & TYPE_MASK);
152 static int g723_samples(unsigned char *buf, int maxlen)
157 while(pos < maxlen) {
158 res = g723_len(buf[pos]);
167 void ast_rtp_set_data(struct ast_rtp *rtp, void *data)
172 void ast_rtp_set_callback(struct ast_rtp *rtp, ast_rtp_callback callback)
174 rtp->callback = callback;
177 void ast_rtp_setnat(struct ast_rtp *rtp, int nat)
182 static struct ast_frame *send_dtmf(struct ast_rtp *rtp)
185 static struct ast_frame null_frame = { AST_FRAME_NULL, };
186 char iabuf[INET_ADDRSTRLEN];
187 gettimeofday(&tv, NULL);
188 if ((tv.tv_sec < rtp->dtmfmute.tv_sec) ||
189 ((tv.tv_sec == rtp->dtmfmute.tv_sec) && (tv.tv_usec < rtp->dtmfmute.tv_usec))) {
191 ast_log(LOG_DEBUG, "Ignore potential DTMF echo from '%s'\n", ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr));
193 rtp->dtmfduration = 0;
197 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));
198 if (rtp->resp == 'X') {
199 rtp->f.frametype = AST_FRAME_CONTROL;
200 rtp->f.subclass = AST_CONTROL_FLASH;
202 rtp->f.frametype = AST_FRAME_DTMF;
203 rtp->f.subclass = rtp->resp;
210 rtp->dtmfduration = 0;
215 static inline int rtp_debug_test_addr(struct sockaddr_in *addr)
219 if (rtpdebugaddr.sin_addr.s_addr) {
220 if (((ntohs(rtpdebugaddr.sin_port) != 0)
221 && (rtpdebugaddr.sin_port != addr->sin_port))
222 || (rtpdebugaddr.sin_addr.s_addr != addr->sin_addr.s_addr))
228 static struct ast_frame *process_cisco_dtmf(struct ast_rtp *rtp, unsigned char *data, int len)
232 struct ast_frame *f = NULL;
233 event = ntohl(*((unsigned int *)(data)));
236 printf("Cisco Digit: %08x (len = %d)\n", event, len);
240 } else if (event < 11) {
242 } else if (event < 12) {
244 } else if (event < 16) {
245 resp = 'A' + (event - 12);
246 } else if (event < 17) {
249 if (rtp->resp && (rtp->resp != resp)) {
253 rtp->dtmfcount = dtmftimeout;
257 static struct ast_frame *process_rfc2833(struct ast_rtp *rtp, unsigned char *data, int len)
260 unsigned int event_end;
261 unsigned int duration;
263 struct ast_frame *f = NULL;
264 event = ntohl(*((unsigned int *)(data)));
266 event_end = ntohl(*((unsigned int *)(data)));
269 duration = ntohl(*((unsigned int *)(data)));
272 printf("Event: %08x (len = %d)\n", event, len);
276 } else if (event < 11) {
278 } else if (event < 12) {
280 } else if (event < 16) {
281 resp = 'A' + (event - 12);
282 } else if (event < 17) {
285 if (rtp->resp && (rtp->resp != resp)) {
288 else if(event_end & 0x80)
297 else if(rtp->dtmfduration && (duration < rtp->dtmfduration))
301 if (!(event_end & 0x80))
303 rtp->dtmfcount = dtmftimeout;
304 rtp->dtmfduration = duration;
308 static struct ast_frame *process_rfc3389(struct ast_rtp *rtp, unsigned char *data, int len)
310 struct ast_frame *f = NULL;
311 /* Convert comfort noise into audio with various codecs. Unfortunately this doesn't
312 totally help us out becuase we don't have an engine to keep it going and we are not
313 guaranteed to have it every 20ms or anything */
315 printf("RFC3389: %d bytes, level %d...\n", len, rtp->lastrxformat);
317 if (!(rtp->flags & FLAG_3389_WARNING)) {
318 ast_log(LOG_NOTICE, "RFC3389 support incomplete. Turn off on client if possible\n");
319 rtp->flags |= FLAG_3389_WARNING;
321 /* Must have at least one byte */
325 rtp->f.data = rtp->rawdata + AST_FRIENDLY_OFFSET;
326 rtp->f.datalen = len - 1;
327 rtp->f.offset = AST_FRIENDLY_OFFSET;
328 memcpy(rtp->f.data, data + 1, len - 1);
334 rtp->f.frametype = AST_FRAME_CNG;
335 rtp->f.subclass = data[0] & 0x7f;
336 rtp->f.datalen = len - 1;
338 rtp->f.delivery.tv_usec = rtp->f.delivery.tv_sec = 0;
343 static int rtpread(int *id, int fd, short events, void *cbdata)
345 struct ast_rtp *rtp = cbdata;
347 f = ast_rtp_read(rtp);
350 rtp->callback(rtp, f, rtp->data);
355 struct ast_frame *ast_rtcp_read(struct ast_rtp *rtp)
357 static struct ast_frame null_frame = { AST_FRAME_NULL, };
361 struct sockaddr_in sin;
362 unsigned int rtcpdata[1024];
363 char iabuf[INET_ADDRSTRLEN];
370 res = recvfrom(rtp->rtcp->s, rtcpdata, sizeof(rtcpdata),
371 0, (struct sockaddr *)&sin, &len);
375 ast_log(LOG_NOTICE, "RTP: Received packet with bad UDP checksum\n");
377 ast_log(LOG_WARNING, "RTP Read error: %s\n", strerror(errno));
384 ast_log(LOG_WARNING, "RTP Read too short\n");
389 /* Send to whoever sent to us */
390 if ((rtp->rtcp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
391 (rtp->rtcp->them.sin_port != sin.sin_port)) {
392 memcpy(&rtp->them, &sin, sizeof(rtp->them));
395 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));
399 ast_log(LOG_DEBUG, "Got RTCP report of %d bytes\n", res);
403 static void calc_rxstamp(struct timeval *tv, struct ast_rtp *rtp, unsigned int timestamp, int mark)
405 if ((!rtp->rxcore.tv_sec && !rtp->rxcore.tv_usec) || mark) {
406 gettimeofday(&rtp->rxcore, NULL);
407 rtp->rxcore.tv_sec -= timestamp / 8000;
408 rtp->rxcore.tv_usec -= (timestamp % 8000) * 125;
409 /* Round to 20ms for nice, pretty timestamps */
410 rtp->rxcore.tv_usec -= rtp->rxcore.tv_usec % 20000;
411 if (rtp->rxcore.tv_usec < 0) {
412 /* Adjust appropriately if necessary */
413 rtp->rxcore.tv_usec += 1000000;
414 rtp->rxcore.tv_sec -= 1;
417 tv->tv_sec = rtp->rxcore.tv_sec + timestamp / 8000;
418 tv->tv_usec = rtp->rxcore.tv_usec + (timestamp % 8000) * 125;
419 if (tv->tv_usec >= 1000000) {
420 tv->tv_usec -= 1000000;
425 struct ast_frame *ast_rtp_read(struct ast_rtp *rtp)
428 struct sockaddr_in sin;
437 char iabuf[INET_ADDRSTRLEN];
438 unsigned int timestamp;
439 unsigned int *rtpheader;
440 static struct ast_frame *f, null_frame = { AST_FRAME_NULL, };
441 struct rtpPayloadType rtpPT;
445 /* Cache where the header will go */
446 res = recvfrom(rtp->s, rtp->rawdata + AST_FRIENDLY_OFFSET, sizeof(rtp->rawdata) - AST_FRIENDLY_OFFSET,
447 0, (struct sockaddr *)&sin, &len);
450 rtpheader = (unsigned int *)(rtp->rawdata + AST_FRIENDLY_OFFSET);
453 ast_log(LOG_NOTICE, "RTP: Received packet with bad UDP checksum\n");
455 ast_log(LOG_WARNING, "RTP Read error: %s\n", strerror(errno));
461 ast_log(LOG_WARNING, "RTP Read too short\n");
465 /* Ignore if the other side hasn't been given an address
467 if (!rtp->them.sin_addr.s_addr || !rtp->them.sin_port)
471 /* Send to whoever sent to us */
472 if ((rtp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
473 (rtp->them.sin_port != sin.sin_port)) {
474 memcpy(&rtp->them, &sin, sizeof(rtp->them));
476 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));
481 seqno = ntohl(rtpheader[0]);
483 /* Check RTP version */
484 version = (seqno & 0xC0000000) >> 30;
488 payloadtype = (seqno & 0x7f0000) >> 16;
489 mark = seqno & (1 << 23);
490 ext = seqno & (1 << 28);
492 timestamp = ntohl(rtpheader[1]);
494 /* RTP Extension present */
496 hdrlen += (ntohl(rtpheader[3]) & 0xffff) << 2;
500 ast_log(LOG_WARNING, "RTP Read too short (%d, expecting %d)\n", res, hdrlen);
504 if(rtp_debug_test_addr(&sin))
505 ast_verbose("Got RTP packet from %s:%d (type %d, seq %d, ts %d, len %d)\n"
506 , ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), ntohs(sin.sin_port), payloadtype, seqno, timestamp,res - hdrlen);
508 rtpPT = ast_rtp_lookup_pt(rtp, payloadtype);
509 if (!rtpPT.isAstFormat) {
510 /* This is special in-band data that's not one of our codecs */
511 if (rtpPT.code == AST_RTP_DTMF) {
512 /* It's special -- rfc2833 process it */
513 if (rtp->lasteventseqn <= seqno || rtp->resp == 0 || (rtp->lasteventseqn >= 65530 && seqno <= 6)) {
514 f = process_rfc2833(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
515 rtp->lasteventseqn = seqno;
522 } else if (rtpPT.code == AST_RTP_CISCO_DTMF) {
523 /* It's really special -- process it the Cisco way */
524 if (rtp->lasteventseqn <= seqno || rtp->resp == 0 || (rtp->lasteventseqn >= 65530 && seqno <= 6)) {
525 f = process_cisco_dtmf(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
526 rtp->lasteventseqn = seqno;
533 } else if (rtpPT.code == AST_RTP_CN) {
535 f = process_rfc3389(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
541 ast_log(LOG_NOTICE, "Unknown RTP codec %d received\n", payloadtype);
545 rtp->f.subclass = rtpPT.code;
546 if (rtp->f.subclass < AST_FORMAT_MAX_AUDIO)
547 rtp->f.frametype = AST_FRAME_VOICE;
549 rtp->f.frametype = AST_FRAME_VIDEO;
550 rtp->lastrxformat = rtp->f.subclass;
553 rtp->lastrxts = timestamp;
556 for (x=rtp->rxseqno + 1; x < seqno; x++) {
557 /* Queue empty frames */
563 rtp->f.src = "RTPMissedFrame";
566 rtp->rxseqno = seqno;
568 if (rtp->dtmfcount) {
570 printf("dtmfcount was %d\n", rtp->dtmfcount);
572 rtp->dtmfcount -= (timestamp - rtp->lastrxts);
573 if (rtp->dtmfcount < 0)
576 if (dtmftimeout != rtp->dtmfcount)
577 printf("dtmfcount is %d\n", rtp->dtmfcount);
580 rtp->lastrxts = timestamp;
582 /* Send any pending DTMF */
583 if (rtp->resp && !rtp->dtmfcount) {
585 ast_log(LOG_DEBUG, "Sending pending DTMF\n");
586 return send_dtmf(rtp);
589 rtp->f.datalen = res - hdrlen;
590 rtp->f.data = rtp->rawdata + hdrlen + AST_FRIENDLY_OFFSET;
591 rtp->f.offset = hdrlen + AST_FRIENDLY_OFFSET;
592 if (rtp->f.subclass < AST_FORMAT_MAX_AUDIO) {
593 switch(rtp->f.subclass) {
594 case AST_FORMAT_ULAW:
595 case AST_FORMAT_ALAW:
596 rtp->f.samples = rtp->f.datalen;
598 case AST_FORMAT_SLINEAR:
599 rtp->f.samples = rtp->f.datalen / 2;
602 rtp->f.samples = 160 * (rtp->f.datalen / 33);
604 case AST_FORMAT_ILBC:
605 rtp->f.samples = 240 * (rtp->f.datalen / 50);
607 case AST_FORMAT_ADPCM:
608 case AST_FORMAT_G726:
609 rtp->f.samples = rtp->f.datalen * 2;
611 case AST_FORMAT_G729A:
612 rtp->f.samples = rtp->f.datalen * 8;
614 case AST_FORMAT_G723_1:
615 rtp->f.samples = g723_samples(rtp->f.data, rtp->f.datalen);
617 case AST_FORMAT_SPEEX:
618 /* assumes that the RTP packet contained one Speex frame */
619 rtp->f.samples = 160;
621 case AST_FORMAT_LPC10:
622 rtp->f.samples = 22 * 8;
623 rtp->f.samples += (((char *)(rtp->f.data))[7] & 0x1) * 8;
626 ast_log(LOG_NOTICE, "Unable to calculate samples for format %s\n", ast_getformatname(rtp->f.subclass));
629 calc_rxstamp(&rtp->f.delivery, rtp, timestamp, mark);
631 /* Video -- samples is # of samples vs. 90000 */
632 if (!rtp->lastividtimestamp)
633 rtp->lastividtimestamp = timestamp;
634 rtp->f.samples = timestamp - rtp->lastividtimestamp;
635 rtp->lastividtimestamp = timestamp;
636 rtp->f.delivery.tv_sec = 0;
637 rtp->f.delivery.tv_usec = 0;
639 rtp->f.subclass |= 0x1;
646 /* The following array defines the MIME Media type (and subtype) for each
647 of our codecs, or RTP-specific data type. */
649 struct rtpPayloadType payloadType;
653 {{1, AST_FORMAT_G723_1}, "audio", "G723"},
654 {{1, AST_FORMAT_GSM}, "audio", "GSM"},
655 {{1, AST_FORMAT_ULAW}, "audio", "PCMU"},
656 {{1, AST_FORMAT_ALAW}, "audio", "PCMA"},
657 {{1, AST_FORMAT_G726}, "audio", "G726-32"},
658 {{1, AST_FORMAT_ADPCM}, "audio", "DVI4"},
659 {{1, AST_FORMAT_SLINEAR}, "audio", "L16"},
660 {{1, AST_FORMAT_LPC10}, "audio", "LPC"},
661 {{1, AST_FORMAT_G729A}, "audio", "G729"},
662 {{1, AST_FORMAT_SPEEX}, "audio", "speex"},
663 {{1, AST_FORMAT_ILBC}, "audio", "iLBC"},
664 {{0, AST_RTP_DTMF}, "audio", "telephone-event"},
665 {{0, AST_RTP_CISCO_DTMF}, "audio", "cisco-telephone-event"},
666 {{0, AST_RTP_CN}, "audio", "CN"},
667 {{1, AST_FORMAT_JPEG}, "video", "JPEG"},
668 {{1, AST_FORMAT_PNG}, "video", "PNG"},
669 {{1, AST_FORMAT_H261}, "video", "H261"},
670 {{1, AST_FORMAT_H263}, "video", "H263"},
673 /* Static (i.e., well-known) RTP payload types for our "AST_FORMAT..."s:
674 also, our own choices for dynamic payload types. This is our master
675 table for transmission */
676 static struct rtpPayloadType static_RTP_PT[MAX_RTP_PT] = {
677 [0] = {1, AST_FORMAT_ULAW},
678 [2] = {1, AST_FORMAT_G726}, /* Technically this is G.721, but if Cisco can do it, so can we... */
679 [3] = {1, AST_FORMAT_GSM},
680 [4] = {1, AST_FORMAT_G723_1},
681 [5] = {1, AST_FORMAT_ADPCM}, /* 8 kHz */
682 [6] = {1, AST_FORMAT_ADPCM}, /* 16 kHz */
683 [7] = {1, AST_FORMAT_LPC10},
684 [8] = {1, AST_FORMAT_ALAW},
685 [10] = {1, AST_FORMAT_SLINEAR}, /* 2 channels */
686 [11] = {1, AST_FORMAT_SLINEAR}, /* 1 channel */
687 [13] = {0, AST_RTP_CN},
688 [16] = {1, AST_FORMAT_ADPCM}, /* 11.025 kHz */
689 [17] = {1, AST_FORMAT_ADPCM}, /* 22.050 kHz */
690 [18] = {1, AST_FORMAT_G729A},
691 [19] = {0, AST_RTP_CN}, /* Also used for CN */
692 [26] = {1, AST_FORMAT_JPEG},
693 [31] = {1, AST_FORMAT_H261},
694 [34] = {1, AST_FORMAT_H263},
695 [97] = {1, AST_FORMAT_ILBC},
696 [101] = {0, AST_RTP_DTMF},
697 [110] = {1, AST_FORMAT_SPEEX},
698 [121] = {0, AST_RTP_CISCO_DTMF}, /* Must be type 121 */
701 void ast_rtp_pt_clear(struct ast_rtp* rtp)
705 for (i = 0; i < MAX_RTP_PT; ++i) {
706 rtp->current_RTP_PT[i].isAstFormat = 0;
707 rtp->current_RTP_PT[i].code = 0;
710 rtp->rtp_lookup_code_cache_isAstFormat = 0;
711 rtp->rtp_lookup_code_cache_code = 0;
712 rtp->rtp_lookup_code_cache_result = 0;
715 void ast_rtp_pt_default(struct ast_rtp* rtp)
719 /* Initialize to default payload types */
720 for (i = 0; i < MAX_RTP_PT; ++i) {
721 rtp->current_RTP_PT[i].isAstFormat = static_RTP_PT[i].isAstFormat;
722 rtp->current_RTP_PT[i].code = static_RTP_PT[i].code;
725 rtp->rtp_lookup_code_cache_isAstFormat = 0;
726 rtp->rtp_lookup_code_cache_code = 0;
727 rtp->rtp_lookup_code_cache_result = 0;
730 /* Make a note of a RTP payload type that was seen in a SDP "m=" line. */
731 /* By default, use the well-known value for this type (although it may */
732 /* still be set to a different value by a subsequent "a=rtpmap:" line): */
733 void ast_rtp_set_m_type(struct ast_rtp* rtp, int pt) {
734 if (pt < 0 || pt > MAX_RTP_PT)
735 return; /* bogus payload type */
737 if (static_RTP_PT[pt].code != 0) {
738 rtp->current_RTP_PT[pt] = static_RTP_PT[pt];
742 /* Make a note of a RTP payload type (with MIME type) that was seen in */
743 /* a SDP "a=rtpmap:" line. */
744 void ast_rtp_set_rtpmap_type(struct ast_rtp* rtp, int pt,
745 char* mimeType, char* mimeSubtype) {
748 if (pt < 0 || pt > MAX_RTP_PT)
749 return; /* bogus payload type */
751 for (i = 0; i < sizeof mimeTypes/sizeof mimeTypes[0]; ++i) {
752 if (strcasecmp(mimeSubtype, mimeTypes[i].subtype) == 0 &&
753 strcasecmp(mimeType, mimeTypes[i].type) == 0) {
754 rtp->current_RTP_PT[pt] = mimeTypes[i].payloadType;
760 /* Return the union of all of the codecs that were set by rtp_set...() calls */
761 /* They're returned as two distinct sets: AST_FORMATs, and AST_RTPs */
762 void ast_rtp_get_current_formats(struct ast_rtp* rtp,
763 int* astFormats, int* nonAstFormats) {
766 *astFormats = *nonAstFormats = 0;
767 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
768 if (rtp->current_RTP_PT[pt].isAstFormat) {
769 *astFormats |= rtp->current_RTP_PT[pt].code;
771 *nonAstFormats |= rtp->current_RTP_PT[pt].code;
776 void ast_rtp_offered_from_local(struct ast_rtp* rtp, int local) {
778 rtp->rtp_offered_from_local = local;
780 ast_log(LOG_WARNING, "rtp structure is null\n");
783 struct rtpPayloadType ast_rtp_lookup_pt(struct ast_rtp* rtp, int pt)
785 struct rtpPayloadType result;
787 result.isAstFormat = result.code = 0;
788 if (pt < 0 || pt > MAX_RTP_PT)
789 return result; /* bogus payload type */
791 /* Start with the negotiated codecs */
792 if (!rtp->rtp_offered_from_local)
793 result = rtp->current_RTP_PT[pt];
795 /* If it doesn't exist, check our static RTP type list, just in case */
797 result = static_RTP_PT[pt];
801 /* Looks up an RTP code out of our *static* outbound list */
802 int ast_rtp_lookup_code(struct ast_rtp* rtp, int isAstFormat, int code) {
806 if (isAstFormat == rtp->rtp_lookup_code_cache_isAstFormat &&
807 code == rtp->rtp_lookup_code_cache_code) {
809 /* Use our cached mapping, to avoid the overhead of the loop below */
810 return rtp->rtp_lookup_code_cache_result;
813 /* Check the dynamic list first */
814 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
815 if (rtp->current_RTP_PT[pt].code == code && rtp->current_RTP_PT[pt].isAstFormat == isAstFormat) {
816 rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
817 rtp->rtp_lookup_code_cache_code = code;
818 rtp->rtp_lookup_code_cache_result = pt;
823 /* Then the static list */
824 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
825 if (static_RTP_PT[pt].code == code && static_RTP_PT[pt].isAstFormat == isAstFormat) {
826 rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
827 rtp->rtp_lookup_code_cache_code = code;
828 rtp->rtp_lookup_code_cache_result = pt;
835 char* ast_rtp_lookup_mime_subtype(int isAstFormat, int code) {
839 for (i = 0; i < sizeof mimeTypes/sizeof mimeTypes[0]; ++i) {
840 if (mimeTypes[i].payloadType.code == code && mimeTypes[i].payloadType.isAstFormat == isAstFormat) {
841 return mimeTypes[i].subtype;
847 static int rtp_socket(void)
851 s = socket(AF_INET, SOCK_DGRAM, 0);
853 flags = fcntl(s, F_GETFL);
854 fcntl(s, F_SETFL, flags | O_NONBLOCK);
857 setsockopt(s, SOL_SOCKET, SO_NO_CHECK, &checksums, sizeof(checksums));
864 static struct ast_rtcp *ast_rtcp_new(void)
866 struct ast_rtcp *rtcp;
867 rtcp = malloc(sizeof(struct ast_rtcp));
870 memset(rtcp, 0, sizeof(struct ast_rtcp));
871 rtcp->s = rtp_socket();
872 rtcp->us.sin_family = AF_INET;
875 ast_log(LOG_WARNING, "Unable to allocate socket: %s\n", strerror(errno));
881 struct ast_rtp *ast_rtp_new_with_bindaddr(struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode, struct in_addr addr)
887 rtp = malloc(sizeof(struct ast_rtp));
890 memset(rtp, 0, sizeof(struct ast_rtp));
891 rtp->them.sin_family = AF_INET;
892 rtp->us.sin_family = AF_INET;
893 rtp->s = rtp_socket();
895 rtp->seqno = rand() & 0xffff;
898 ast_log(LOG_ERROR, "Unable to allocate socket: %s\n", strerror(errno));
901 if (sched && rtcpenable) {
903 rtp->rtcp = ast_rtcp_new();
905 /* Find us a place */
906 x = (rand() % (rtpend-rtpstart)) + rtpstart;
910 /* Must be an even port number by RTP spec */
911 rtp->us.sin_port = htons(x);
912 rtp->us.sin_addr = addr;
914 rtp->rtcp->us.sin_port = htons(x + 1);
915 if (!(first = bind(rtp->s, (struct sockaddr *)&rtp->us, sizeof(rtp->us))) &&
916 (!rtp->rtcp || !bind(rtp->rtcp->s, (struct sockaddr *)&rtp->rtcp->us, sizeof(rtp->rtcp->us))))
919 /* Primary bind succeeded! Gotta recreate it */
921 rtp->s = rtp_socket();
923 if (errno != EADDRINUSE) {
924 ast_log(LOG_ERROR, "Unexpected bind error: %s\n", strerror(errno));
935 x = (rtpstart + 1) & ~1;
936 if (x == startplace) {
937 ast_log(LOG_ERROR, "No RTP ports remaining\n");
947 if (io && sched && callbackmode) {
948 /* Operate this one in a callback mode */
951 rtp->ioid = ast_io_add(rtp->io, rtp->s, rtpread, AST_IO_IN, rtp);
953 ast_rtp_pt_default(rtp);
957 struct ast_rtp *ast_rtp_new(struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode)
961 memset(&ia, 0, sizeof(ia));
962 return ast_rtp_new_with_bindaddr(sched, io, rtcpenable, callbackmode, ia);
965 int ast_rtp_settos(struct ast_rtp *rtp, int tos)
969 if ((res = setsockopt(rtp->s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))))
970 ast_log(LOG_WARNING, "Unable to set TOS to %d\n", tos);
974 void ast_rtp_set_peer(struct ast_rtp *rtp, struct sockaddr_in *them)
976 rtp->them.sin_port = them->sin_port;
977 rtp->them.sin_addr = them->sin_addr;
979 rtp->rtcp->them.sin_port = htons(ntohs(them->sin_port) + 1);
980 rtp->rtcp->them.sin_addr = them->sin_addr;
985 void ast_rtp_get_peer(struct ast_rtp *rtp, struct sockaddr_in *them)
987 them->sin_family = AF_INET;
988 them->sin_port = rtp->them.sin_port;
989 them->sin_addr = rtp->them.sin_addr;
992 void ast_rtp_get_us(struct ast_rtp *rtp, struct sockaddr_in *us)
994 memcpy(us, &rtp->us, sizeof(rtp->us));
997 void ast_rtp_stop(struct ast_rtp *rtp)
999 memset(&rtp->them.sin_addr, 0, sizeof(rtp->them.sin_addr));
1000 memset(&rtp->them.sin_port, 0, sizeof(rtp->them.sin_port));
1002 memset(&rtp->rtcp->them.sin_addr, 0, sizeof(rtp->them.sin_addr));
1003 memset(&rtp->rtcp->them.sin_port, 0, sizeof(rtp->them.sin_port));
1007 void ast_rtp_reset(struct ast_rtp *rtp)
1009 memset(&rtp->rxcore, 0, sizeof(rtp->rxcore));
1010 memset(&rtp->txcore, 0, sizeof(rtp->txcore));
1011 memset(&rtp->dtmfmute, 0, sizeof(rtp->dtmfmute));
1014 rtp->digitssent = 0;
1015 rtp->lastividtimestamp = 0;
1016 rtp->lastovidtimestamp = 0;
1017 rtp->lasteventseqn = 0;
1018 rtp->lasttxformat = 0;
1019 rtp->lastrxformat = 0;
1021 rtp->dtmfduration = 0;
1026 void ast_rtp_destroy(struct ast_rtp *rtp)
1029 ast_smoother_free(rtp->smoother);
1031 ast_io_remove(rtp->io, rtp->ioid);
1035 close(rtp->rtcp->s);
1041 static unsigned int calc_txstamp(struct ast_rtp *rtp, struct timeval *delivery)
1045 if (!rtp->txcore.tv_sec && !rtp->txcore.tv_usec) {
1046 gettimeofday(&rtp->txcore, NULL);
1047 /* Round to 20ms for nice, pretty timestamps */
1048 rtp->txcore.tv_usec -= rtp->txcore.tv_usec % 20000;
1050 if (delivery && (delivery->tv_sec || delivery->tv_usec)) {
1051 /* Use previous txcore */
1052 ms = (delivery->tv_sec - rtp->txcore.tv_sec) * 1000;
1053 ms += (1000000 + delivery->tv_usec - rtp->txcore.tv_usec) / 1000 - 1000;
1054 rtp->txcore.tv_sec = delivery->tv_sec;
1055 rtp->txcore.tv_usec = delivery->tv_usec;
1057 gettimeofday(&now, NULL);
1058 ms = (now.tv_sec - rtp->txcore.tv_sec) * 1000;
1059 ms += (1000000 + now.tv_usec - rtp->txcore.tv_usec) / 1000 - 1000;
1060 /* Use what we just got for next time */
1061 rtp->txcore.tv_sec = now.tv_sec;
1062 rtp->txcore.tv_usec = now.tv_usec;
1067 int ast_rtp_senddigit(struct ast_rtp *rtp, char digit)
1069 unsigned int *rtpheader;
1075 char iabuf[INET_ADDRSTRLEN];
1077 if ((digit <= '9') && (digit >= '0'))
1079 else if (digit == '*')
1081 else if (digit == '#')
1083 else if ((digit >= 'A') && (digit <= 'D'))
1084 digit = digit - 'A' + 12;
1085 else if ((digit >= 'a') && (digit <= 'd'))
1086 digit = digit - 'a' + 12;
1088 ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
1091 payload = ast_rtp_lookup_code(rtp, 0, AST_RTP_DTMF);
1093 /* If we have no peer, return immediately */
1094 if (!rtp->them.sin_addr.s_addr)
1097 gettimeofday(&rtp->dtmfmute, NULL);
1098 rtp->dtmfmute.tv_usec += (500 * 1000);
1099 if (rtp->dtmfmute.tv_usec > 1000000) {
1100 rtp->dtmfmute.tv_usec -= 1000000;
1101 rtp->dtmfmute.tv_sec += 1;
1104 /* Get a pointer to the header */
1105 rtpheader = (unsigned int *)data;
1106 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | rtp->seqno);
1107 rtpheader[1] = htonl(rtp->lastts + (rtp->digitssent * 160));
1108 rtpheader[2] = htonl(rtp->ssrc);
1109 rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (0));
1115 if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
1116 res = sendto(rtp->s, (void *)rtpheader, hdrlen + 4, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
1118 ast_log(LOG_ERROR, "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));
1119 if(rtp_debug_test_addr(&rtp->them))
1120 ast_verbose("Sent RTP packet to %s:%d (type %d, seq %d, ts %d, len %d)\n"
1121 , ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr), ntohs(rtp->them.sin_port), payload, rtp->seqno, rtp->lastts,res - hdrlen);
1125 /* Clear marker bit and increment seqno */
1126 rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno++));
1127 /* Make duration 800 (100ms) */
1128 rtpheader[3] |= htonl((800));
1129 /* Set the End bit for the last 3 */
1130 rtpheader[3] |= htonl((1 << 23));
1131 } else if ( x < 5) {
1132 rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno++));
1138 int ast_rtp_sendcng(struct ast_rtp *rtp, int level)
1140 unsigned int *rtpheader;
1145 char iabuf[INET_ADDRSTRLEN];
1146 level = 127 - (level & 0x7f);
1147 payload = ast_rtp_lookup_code(rtp, 0, AST_RTP_CN);
1149 /* If we have no peer, return immediately */
1150 if (!rtp->them.sin_addr.s_addr)
1153 gettimeofday(&rtp->dtmfmute, NULL);
1154 rtp->dtmfmute.tv_usec += (500 * 1000);
1155 if (rtp->dtmfmute.tv_usec > 1000000) {
1156 rtp->dtmfmute.tv_usec -= 1000000;
1157 rtp->dtmfmute.tv_sec += 1;
1160 /* Get a pointer to the header */
1161 rtpheader = (unsigned int *)data;
1162 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno++));
1163 rtpheader[1] = htonl(rtp->lastts);
1164 rtpheader[2] = htonl(rtp->ssrc);
1166 if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
1167 res = sendto(rtp->s, (void *)rtpheader, hdrlen + 1, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
1169 ast_log(LOG_ERROR, "RTP Comfort Noise Transmission error to %s:%d: %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr), ntohs(rtp->them.sin_port), strerror(errno));
1170 if(rtp_debug_test_addr(&rtp->them))
1171 ast_verbose("Sent Comfort Noise RTP packet to %s:%d (type %d, seq %d, ts %d, len %d)\n"
1172 , ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr), ntohs(rtp->them.sin_port), payload, rtp->seqno, rtp->lastts,res - hdrlen);
1179 static void put_uint32(unsigned char *buf, int i)
1181 unsigned char *c = (unsigned char *)&i;
1183 buf[0] = (i>>24) & 0xff;
1184 buf[1] = (i>>16) & 0xff;
1185 buf[2] = (i>>8) & 0xff;
1189 #define put_uint32(p,v) ((*((unsigned int *)(p))) = (v))
1192 static int ast_rtp_raw_write(struct ast_rtp *rtp, struct ast_frame *f, int codec)
1194 unsigned char *rtpheader;
1195 char iabuf[INET_ADDRSTRLEN];
1202 ms = calc_txstamp(rtp, &f->delivery);
1203 /* Default prediction */
1204 if (f->subclass < AST_FORMAT_MAX_AUDIO) {
1205 pred = rtp->lastts + ms * 8;
1207 switch(f->subclass) {
1208 case AST_FORMAT_ULAW:
1209 case AST_FORMAT_ALAW:
1210 /* If we're within +/- 20ms from when where we
1211 predict we should be, use that */
1212 pred = rtp->lastts + f->datalen;
1214 case AST_FORMAT_ADPCM:
1215 case AST_FORMAT_G726:
1216 /* If we're within +/- 20ms from when where we
1217 predict we should be, use that */
1218 pred = rtp->lastts + f->datalen * 2;
1220 case AST_FORMAT_G729A:
1221 pred = rtp->lastts + f->datalen * 8;
1223 case AST_FORMAT_GSM:
1224 pred = rtp->lastts + (f->datalen * 160 / 33);
1226 case AST_FORMAT_ILBC:
1227 pred = rtp->lastts + (f->datalen * 240 / 50);
1229 case AST_FORMAT_G723_1:
1230 pred = rtp->lastts + g723_samples(f->data, f->datalen);
1232 case AST_FORMAT_SPEEX:
1233 pred = rtp->lastts + 160;
1234 /* assumes that the RTP packet contains one Speex frame */
1236 case AST_FORMAT_LPC10:
1237 /* assumes that the RTP packet contains one LPC10 frame */
1238 pred = rtp->lastts + 22 * 8;
1239 pred += (((char *)(f->data))[7] & 0x1) * 8;
1242 ast_log(LOG_WARNING, "Not sure about timestamp format for codec format %s\n", ast_getformatname(f->subclass));
1244 /* Re-calculate last TS */
1245 rtp->lastts = rtp->lastts + ms * 8;
1246 if (!f->delivery.tv_sec && !f->delivery.tv_usec) {
1247 /* If this isn't an absolute delivery time, Check if it is close to our prediction,
1248 and if so, go with our prediction */
1249 if (abs(rtp->lastts - pred) < MAX_TIMESTAMP_SKEW)
1252 if (option_debug > 2)
1253 ast_log(LOG_DEBUG, "Difference is %d, ms is %d\n", abs(rtp->lastts - pred), ms);
1258 mark = f->subclass & 0x1;
1259 pred = rtp->lastovidtimestamp + f->samples;
1260 /* Re-calculate last TS */
1261 rtp->lastts = rtp->lastts + ms * 90;
1262 /* If it's close to our prediction, go for it */
1263 if (!f->delivery.tv_sec && !f->delivery.tv_usec) {
1264 if (abs(rtp->lastts - pred) < 7200) {
1266 rtp->lastovidtimestamp += f->samples;
1268 if (option_debug > 2)
1269 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);
1270 rtp->lastovidtimestamp = rtp->lastts;
1274 /* Get a pointer to the header */
1275 rtpheader = (unsigned char *)(f->data - hdrlen);
1277 put_uint32(rtpheader, htonl((2 << 30) | (codec << 16) | (rtp->seqno) | (mark << 23)));
1278 put_uint32(rtpheader + 4, htonl(rtp->lastts));
1279 put_uint32(rtpheader + 8, htonl(rtp->ssrc));
1281 rtp->digitssent = 0;
1284 if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
1285 res = sendto(rtp->s, (void *)rtpheader, f->datalen + hdrlen, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
1287 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));
1288 if(rtp_debug_test_addr(&rtp->them))
1289 ast_verbose("Sent RTP packet to %s:%d (type %d, seq %d, ts %d, len %d)\n"
1290 , ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr), ntohs(rtp->them.sin_port), codec, rtp->seqno, rtp->lastts,res - hdrlen);
1295 int ast_rtp_write(struct ast_rtp *rtp, struct ast_frame *_f)
1297 struct ast_frame *f;
1303 /* If we have no peer, return immediately */
1304 if (!rtp->them.sin_addr.s_addr)
1307 /* If there is no data length, return immediately */
1311 /* Make sure we have enough space for RTP header */
1312 if ((_f->frametype != AST_FRAME_VOICE) && (_f->frametype != AST_FRAME_VIDEO)) {
1313 ast_log(LOG_WARNING, "RTP can only send voice\n");
1317 subclass = _f->subclass;
1318 if (_f->frametype == AST_FRAME_VIDEO)
1321 codec = ast_rtp_lookup_code(rtp, 1, subclass);
1323 ast_log(LOG_WARNING, "Don't know how to send format %s packets with RTP\n", ast_getformatname(_f->subclass));
1327 if (rtp->lasttxformat != subclass) {
1328 /* New format, reset the smoother */
1330 ast_log(LOG_DEBUG, "Ooh, format changed from %s to %s\n", ast_getformatname(rtp->lasttxformat), ast_getformatname(subclass));
1331 rtp->lasttxformat = subclass;
1333 ast_smoother_free(rtp->smoother);
1334 rtp->smoother = NULL;
1339 case AST_FORMAT_ULAW:
1340 case AST_FORMAT_ALAW:
1341 if (!rtp->smoother) {
1342 rtp->smoother = ast_smoother_new(160);
1344 if (!rtp->smoother) {
1345 ast_log(LOG_WARNING, "Unable to create smoother :(\n");
1348 ast_smoother_feed(rtp->smoother, _f);
1350 while((f = ast_smoother_read(rtp->smoother)))
1351 ast_rtp_raw_write(rtp, f, codec);
1353 case AST_FORMAT_ADPCM:
1354 case AST_FORMAT_G726:
1355 if (!rtp->smoother) {
1356 rtp->smoother = ast_smoother_new(80);
1358 if (!rtp->smoother) {
1359 ast_log(LOG_WARNING, "Unable to create smoother :(\n");
1362 ast_smoother_feed(rtp->smoother, _f);
1364 while((f = ast_smoother_read(rtp->smoother)))
1365 ast_rtp_raw_write(rtp, f, codec);
1367 case AST_FORMAT_G729A:
1368 if (!rtp->smoother) {
1369 rtp->smoother = ast_smoother_new(20);
1371 ast_smoother_set_flags(rtp->smoother, AST_SMOOTHER_FLAG_G729);
1373 if (!rtp->smoother) {
1374 ast_log(LOG_WARNING, "Unable to create g729 smoother :(\n");
1377 ast_smoother_feed(rtp->smoother, _f);
1379 while((f = ast_smoother_read(rtp->smoother)))
1380 ast_rtp_raw_write(rtp, f, codec);
1382 case AST_FORMAT_GSM:
1383 if (!rtp->smoother) {
1384 rtp->smoother = ast_smoother_new(33);
1386 if (!rtp->smoother) {
1387 ast_log(LOG_WARNING, "Unable to create GSM smoother :(\n");
1390 ast_smoother_feed(rtp->smoother, _f);
1391 while((f = ast_smoother_read(rtp->smoother)))
1392 ast_rtp_raw_write(rtp, f, codec);
1394 case AST_FORMAT_ILBC:
1395 if (!rtp->smoother) {
1396 rtp->smoother = ast_smoother_new(50);
1398 if (!rtp->smoother) {
1399 ast_log(LOG_WARNING, "Unable to create ILBC smoother :(\n");
1402 ast_smoother_feed(rtp->smoother, _f);
1403 while((f = ast_smoother_read(rtp->smoother)))
1404 ast_rtp_raw_write(rtp, f, codec);
1407 ast_log(LOG_WARNING, "Not sure about sending format %s packets\n", ast_getformatname(subclass));
1408 /* fall through to... */
1409 case AST_FORMAT_H261:
1410 case AST_FORMAT_H263:
1411 case AST_FORMAT_G723_1:
1412 case AST_FORMAT_LPC10:
1413 case AST_FORMAT_SPEEX:
1414 /* Don't buffer outgoing frames; send them one-per-packet: */
1415 if (_f->offset < hdrlen) {
1420 ast_rtp_raw_write(rtp, f, codec);
1426 void ast_rtp_proto_unregister(struct ast_rtp_protocol *proto)
1428 struct ast_rtp_protocol *cur, *prev;
1435 prev->next = proto->next;
1437 protos = proto->next;
1445 int ast_rtp_proto_register(struct ast_rtp_protocol *proto)
1447 struct ast_rtp_protocol *cur;
1450 if (cur->type == proto->type) {
1451 ast_log(LOG_WARNING, "Tried to register same protocol '%s' twice\n", cur->type);
1456 proto->next = protos;
1461 static struct ast_rtp_protocol *get_proto(struct ast_channel *chan)
1463 struct ast_rtp_protocol *cur;
1466 if (cur->type == chan->type) {
1474 int ast_rtp_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
1476 struct ast_frame *f;
1477 struct ast_channel *who, *cs[3];
1478 struct ast_rtp *p0, *p1;
1479 struct ast_rtp *vp0, *vp1;
1480 struct ast_rtp_protocol *pr0, *pr1;
1481 struct sockaddr_in ac0, ac1;
1482 struct sockaddr_in vac0, vac1;
1483 struct sockaddr_in t0, t1;
1484 struct sockaddr_in vt0, vt1;
1485 char iabuf[INET_ADDRSTRLEN];
1489 int codec0,codec1, oldcodec0, oldcodec1;
1491 memset(&vt0, 0, sizeof(vt0));
1492 memset(&vt1, 0, sizeof(vt1));
1493 memset(&vac0, 0, sizeof(vac0));
1494 memset(&vac1, 0, sizeof(vac1));
1496 /* if need DTMF, cant native bridge */
1497 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
1499 ast_mutex_lock(&c0->lock);
1500 while(ast_mutex_trylock(&c1->lock)) {
1501 ast_mutex_unlock(&c0->lock);
1503 ast_mutex_lock(&c0->lock);
1505 pr0 = get_proto(c0);
1506 pr1 = get_proto(c1);
1508 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c0->name);
1509 ast_mutex_unlock(&c0->lock);
1510 ast_mutex_unlock(&c1->lock);
1514 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c1->name);
1515 ast_mutex_unlock(&c0->lock);
1516 ast_mutex_unlock(&c1->lock);
1519 pvt0 = c0->pvt->pvt;
1520 pvt1 = c1->pvt->pvt;
1521 p0 = pr0->get_rtp_info(c0);
1522 if (pr0->get_vrtp_info)
1523 vp0 = pr0->get_vrtp_info(c0);
1526 p1 = pr1->get_rtp_info(c1);
1527 if (pr1->get_vrtp_info)
1528 vp1 = pr1->get_vrtp_info(c1);
1532 /* Somebody doesn't want to play... */
1533 ast_mutex_unlock(&c0->lock);
1534 ast_mutex_unlock(&c1->lock);
1538 codec0 = pr0->get_codec(c0);
1542 codec1 = pr1->get_codec(c1);
1545 if (pr0->get_codec && pr1->get_codec) {
1546 /* Hey, we can't do reinvite if both parties speak diffrent codecs */
1547 if (!(codec0 & codec1)) {
1548 ast_log(LOG_WARNING, "codec0 = %d is not codec1 = %d, cannot native bridge.\n",codec0,codec1);
1549 ast_mutex_unlock(&c0->lock);
1550 ast_mutex_unlock(&c1->lock);
1554 if (pr0->set_rtp_peer(c0, p1, vp1, codec1))
1555 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
1557 /* Store RTP peer */
1558 ast_rtp_get_peer(p1, &ac1);
1560 ast_rtp_get_peer(vp1, &vac1);
1562 if (pr1->set_rtp_peer(c1, p0, vp0, codec0))
1563 ast_log(LOG_WARNING, "Channel '%s' failed to talk back to '%s'\n", c1->name, c0->name);
1565 /* Store RTP peer */
1566 ast_rtp_get_peer(p0, &ac0);
1568 ast_rtp_get_peer(vp0, &vac0);
1570 ast_mutex_unlock(&c0->lock);
1571 ast_mutex_unlock(&c1->lock);
1578 if ((c0->pvt->pvt != pvt0) ||
1579 (c1->pvt->pvt != pvt1) ||
1580 (c0->masq || c0->masqr || c1->masq || c1->masqr)) {
1581 ast_log(LOG_DEBUG, "Oooh, something is weird, backing out\n");
1582 if (c0->pvt->pvt == pvt0) {
1583 if (pr0->set_rtp_peer(c0, NULL, NULL, 0))
1584 ast_log(LOG_WARNING, "Channel '%s' failed to revert\n", c0->name);
1586 if (c1->pvt->pvt == pvt1) {
1587 if (pr1->set_rtp_peer(c1, NULL, NULL, 0))
1588 ast_log(LOG_WARNING, "Channel '%s' failed to revert back\n", c1->name);
1590 /* Tell it to try again later */
1594 ast_rtp_get_peer(p1, &t1);
1595 ast_rtp_get_peer(p0, &t0);
1597 codec0 = pr0->get_codec(c0);
1599 codec1 = pr1->get_codec(c1);
1601 ast_rtp_get_peer(vp1, &vt1);
1603 ast_rtp_get_peer(vp0, &vt0);
1604 if (inaddrcmp(&t1, &ac1) || (vp1 && inaddrcmp(&vt1, &vac1)) || (codec1 != oldcodec1)) {
1606 ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
1607 c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), t1.sin_addr), ntohs(t1.sin_port), codec1);
1608 ast_log(LOG_DEBUG, "Oooh, '%s' changed end vaddress to %s:%d (format %d)\n",
1609 c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), vt1.sin_addr), ntohs(vt1.sin_port), codec1);
1610 ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d/(format %d)\n",
1611 c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), ac1.sin_addr), ntohs(ac1.sin_port), oldcodec1);
1612 ast_log(LOG_DEBUG, "Oooh, '%s' wasv %s:%d/(format %d)\n",
1613 c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), vac1.sin_addr), ntohs(vac1.sin_port), oldcodec1);
1615 if (pr0->set_rtp_peer(c0, t1.sin_addr.s_addr ? p1 : NULL, vt1.sin_addr.s_addr ? vp1 : NULL, codec1))
1616 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c0->name, c1->name);
1617 memcpy(&ac1, &t1, sizeof(ac1));
1618 memcpy(&vac1, &vt1, sizeof(vac1));
1621 if (inaddrcmp(&t0, &ac0) || (vp0 && inaddrcmp(&vt0, &vac0))) {
1623 ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
1624 c0->name, ast_inet_ntoa(iabuf, sizeof(iabuf), t0.sin_addr), ntohs(t0.sin_port), codec0);
1625 ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d/(format %d)\n",
1626 c0->name, ast_inet_ntoa(iabuf, sizeof(iabuf), ac0.sin_addr), ntohs(ac0.sin_port), oldcodec0);
1628 if (pr1->set_rtp_peer(c1, t0.sin_addr.s_addr ? p0 : NULL, vt0.sin_addr.s_addr ? vp0 : NULL, codec0))
1629 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c1->name, c0->name);
1630 memcpy(&ac0, &t0, sizeof(ac0));
1631 memcpy(&vac0, &vt0, sizeof(vac0));
1634 who = ast_waitfor_n(cs, 2, &to);
1637 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
1638 /* check for hagnup / whentohangup */
1639 if (ast_check_hangup(c0) || ast_check_hangup(c1))
1644 if (!f || ((f->frametype == AST_FRAME_DTMF) &&
1645 (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
1646 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))))) {
1650 ast_log(LOG_DEBUG, "Oooh, got a %s\n", f ? "digit" : "hangup");
1651 if ((c0->pvt->pvt == pvt0) && (!c0->_softhangup)) {
1652 if (pr0->set_rtp_peer(c0, NULL, NULL, 0))
1653 ast_log(LOG_WARNING, "Channel '%s' failed to revert\n", c0->name);
1655 if ((c1->pvt->pvt == pvt1) && (!c1->_softhangup)) {
1656 if (pr1->set_rtp_peer(c1, NULL, NULL, 0))
1657 ast_log(LOG_WARNING, "Channel '%s' failed to revert back\n", c1->name);
1659 /* That's all we needed */
1662 if ((f->frametype == AST_FRAME_DTMF) ||
1663 (f->frametype == AST_FRAME_VOICE) ||
1664 (f->frametype == AST_FRAME_VIDEO)) {
1665 /* Forward voice or DTMF frames if they happen upon us */
1668 } else if (who == c1) {
1674 /* Swap priority not that it's a big deal at this point */
1683 static int rtp_do_debug_ip(int fd, int argc, char *argv[])
1686 struct ast_hostent ahp;
1687 char iabuf[INET_ADDRSTRLEN];
1692 return RESULT_SHOWUSAGE;
1694 p = strstr(arg, ":");
1700 hp = ast_gethostbyname(arg, &ahp);
1702 return RESULT_SHOWUSAGE;
1703 rtpdebugaddr.sin_family = AF_INET;
1704 memcpy(&rtpdebugaddr.sin_addr, hp->h_addr, sizeof(rtpdebugaddr.sin_addr));
1705 rtpdebugaddr.sin_port = htons(port);
1707 ast_cli(fd, "RTP Debugging Enabled for IP: %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), rtpdebugaddr.sin_addr));
1709 ast_cli(fd, "RTP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(iabuf, sizeof(iabuf), rtpdebugaddr.sin_addr), port);
1711 return RESULT_SUCCESS;
1714 static int rtp_do_debug(int fd, int argc, char *argv[])
1718 return RESULT_SHOWUSAGE;
1719 return rtp_do_debug_ip(fd, argc, argv);
1722 memset(&rtpdebugaddr,0,sizeof(rtpdebugaddr));
1723 ast_cli(fd, "RTP Debugging Enabled\n");
1724 return RESULT_SUCCESS;
1727 static int rtp_no_debug(int fd, int argc, char *argv[])
1730 return RESULT_SHOWUSAGE;
1732 ast_cli(fd,"RTP Debugging Disabled\n");
1733 return RESULT_SUCCESS;
1736 static char debug_usage[] =
1737 "Usage: rtp debug [ip host[:port]]\n"
1738 " Enable dumping of all RTP packets to and from host.\n";
1740 static char no_debug_usage[] =
1741 "Usage: rtp no debug\n"
1742 " Disable all RTP debugging\n";
1744 static struct ast_cli_entry cli_debug_ip =
1745 {{ "rtp", "debug", "ip", NULL } , rtp_do_debug, "Enable RTP debugging on IP", debug_usage };
1747 static struct ast_cli_entry cli_debug =
1748 {{ "rtp", "debug", NULL } , rtp_do_debug, "Enable RTP debugging", debug_usage };
1750 static struct ast_cli_entry cli_no_debug =
1751 {{ "rtp", "no", "debug", NULL } , rtp_no_debug, "Disable RTP debugging", no_debug_usage };
1753 void ast_rtp_reload(void)
1755 struct ast_config *cfg;
1763 cfg = ast_config_load("rtp.conf");
1765 if ((s = ast_variable_retrieve(cfg, "general", "rtpstart"))) {
1767 if (rtpstart < 1024)
1769 if (rtpstart > 65535)
1772 if ((s = ast_variable_retrieve(cfg, "general", "rtpend"))) {
1779 if ((s = ast_variable_retrieve(cfg, "general", "rtpchecksums"))) {
1787 ast_log(LOG_WARNING, "Disabling RTP checksums is not supported on this operating system!\n");
1790 ast_config_destroy(cfg);
1792 if (rtpstart >= rtpend) {
1793 ast_log(LOG_WARNING, "Unreasonable values for RTP start in rtp.conf/end\n");
1797 if (option_verbose > 1)
1798 ast_verbose(VERBOSE_PREFIX_2 "RTP Allocating from port range %d -> %d\n", rtpstart, rtpend);
1802 void ast_rtp_init(void)
1804 ast_cli_register(&cli_debug);
1805 ast_cli_register(&cli_debug_ip);
1806 ast_cli_register(&cli_no_debug);