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/config.h>
37 #include <asterisk/lock.h>
38 #include <asterisk/utils.h>
39 #include <asterisk/cli.h>
40 #include <asterisk/unaligned.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 nochecksums = 0;
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 lastdigitts;
80 unsigned int lastrxts;
81 unsigned int lastividtimestamp;
82 unsigned int lastovidtimestamp;
83 unsigned int lasteventseqn;
87 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;
600 ast_frame_byteswap_be(&rtp->f);
603 rtp->f.samples = 160 * (rtp->f.datalen / 33);
605 case AST_FORMAT_ILBC:
606 rtp->f.samples = 240 * (rtp->f.datalen / 50);
608 case AST_FORMAT_ADPCM:
609 case AST_FORMAT_G726:
610 rtp->f.samples = rtp->f.datalen * 2;
612 case AST_FORMAT_G729A:
613 rtp->f.samples = rtp->f.datalen * 8;
615 case AST_FORMAT_G723_1:
616 rtp->f.samples = g723_samples(rtp->f.data, rtp->f.datalen);
618 case AST_FORMAT_SPEEX:
619 /* assumes that the RTP packet contained one Speex frame */
620 rtp->f.samples = 160;
622 case AST_FORMAT_LPC10:
623 rtp->f.samples = 22 * 8;
624 rtp->f.samples += (((char *)(rtp->f.data))[7] & 0x1) * 8;
627 ast_log(LOG_NOTICE, "Unable to calculate samples for format %s\n", ast_getformatname(rtp->f.subclass));
630 calc_rxstamp(&rtp->f.delivery, rtp, timestamp, mark);
632 /* Video -- samples is # of samples vs. 90000 */
633 if (!rtp->lastividtimestamp)
634 rtp->lastividtimestamp = timestamp;
635 rtp->f.samples = timestamp - rtp->lastividtimestamp;
636 rtp->lastividtimestamp = timestamp;
637 rtp->f.delivery.tv_sec = 0;
638 rtp->f.delivery.tv_usec = 0;
640 rtp->f.subclass |= 0x1;
647 /* The following array defines the MIME Media type (and subtype) for each
648 of our codecs, or RTP-specific data type. */
650 struct rtpPayloadType payloadType;
654 {{1, AST_FORMAT_G723_1}, "audio", "G723"},
655 {{1, AST_FORMAT_GSM}, "audio", "GSM"},
656 {{1, AST_FORMAT_ULAW}, "audio", "PCMU"},
657 {{1, AST_FORMAT_ALAW}, "audio", "PCMA"},
658 {{1, AST_FORMAT_G726}, "audio", "G726-32"},
659 {{1, AST_FORMAT_ADPCM}, "audio", "DVI4"},
660 {{1, AST_FORMAT_SLINEAR}, "audio", "L16"},
661 {{1, AST_FORMAT_LPC10}, "audio", "LPC"},
662 {{1, AST_FORMAT_G729A}, "audio", "G729"},
663 {{1, AST_FORMAT_SPEEX}, "audio", "speex"},
664 {{1, AST_FORMAT_ILBC}, "audio", "iLBC"},
665 {{0, AST_RTP_DTMF}, "audio", "telephone-event"},
666 {{0, AST_RTP_CISCO_DTMF}, "audio", "cisco-telephone-event"},
667 {{0, AST_RTP_CN}, "audio", "CN"},
668 {{1, AST_FORMAT_JPEG}, "video", "JPEG"},
669 {{1, AST_FORMAT_PNG}, "video", "PNG"},
670 {{1, AST_FORMAT_H261}, "video", "H261"},
671 {{1, AST_FORMAT_H263}, "video", "H263"},
672 {{1, AST_FORMAT_H263_PLUS}, "video", "h263-1998"},
675 /* Static (i.e., well-known) RTP payload types for our "AST_FORMAT..."s:
676 also, our own choices for dynamic payload types. This is our master
677 table for transmission */
678 static struct rtpPayloadType static_RTP_PT[MAX_RTP_PT] = {
679 [0] = {1, AST_FORMAT_ULAW},
680 #ifdef USE_DEPRECATED_G726
681 [2] = {1, AST_FORMAT_G726}, /* Technically this is G.721, but if Cisco can do it, so can we... */
683 [3] = {1, AST_FORMAT_GSM},
684 [4] = {1, AST_FORMAT_G723_1},
685 [5] = {1, AST_FORMAT_ADPCM}, /* 8 kHz */
686 [6] = {1, AST_FORMAT_ADPCM}, /* 16 kHz */
687 [7] = {1, AST_FORMAT_LPC10},
688 [8] = {1, AST_FORMAT_ALAW},
689 [10] = {1, AST_FORMAT_SLINEAR}, /* 2 channels */
690 [11] = {1, AST_FORMAT_SLINEAR}, /* 1 channel */
691 [13] = {0, AST_RTP_CN},
692 [16] = {1, AST_FORMAT_ADPCM}, /* 11.025 kHz */
693 [17] = {1, AST_FORMAT_ADPCM}, /* 22.050 kHz */
694 [18] = {1, AST_FORMAT_G729A},
695 [19] = {0, AST_RTP_CN}, /* Also used for CN */
696 [26] = {1, AST_FORMAT_JPEG},
697 [31] = {1, AST_FORMAT_H261},
698 [34] = {1, AST_FORMAT_H263},
699 [103] = {1, AST_FORMAT_H263_PLUS},
700 [97] = {1, AST_FORMAT_ILBC},
701 [101] = {0, AST_RTP_DTMF},
702 [110] = {1, AST_FORMAT_SPEEX},
703 [111] = {1, AST_FORMAT_G726},
704 [121] = {0, AST_RTP_CISCO_DTMF}, /* Must be type 121 */
707 void ast_rtp_pt_clear(struct ast_rtp* rtp)
711 for (i = 0; i < MAX_RTP_PT; ++i) {
712 rtp->current_RTP_PT[i].isAstFormat = 0;
713 rtp->current_RTP_PT[i].code = 0;
716 rtp->rtp_lookup_code_cache_isAstFormat = 0;
717 rtp->rtp_lookup_code_cache_code = 0;
718 rtp->rtp_lookup_code_cache_result = 0;
721 void ast_rtp_pt_default(struct ast_rtp* rtp)
725 /* Initialize to default payload types */
726 for (i = 0; i < MAX_RTP_PT; ++i) {
727 rtp->current_RTP_PT[i].isAstFormat = static_RTP_PT[i].isAstFormat;
728 rtp->current_RTP_PT[i].code = static_RTP_PT[i].code;
731 rtp->rtp_lookup_code_cache_isAstFormat = 0;
732 rtp->rtp_lookup_code_cache_code = 0;
733 rtp->rtp_lookup_code_cache_result = 0;
736 /* Make a note of a RTP payload type that was seen in a SDP "m=" line. */
737 /* By default, use the well-known value for this type (although it may */
738 /* still be set to a different value by a subsequent "a=rtpmap:" line): */
739 void ast_rtp_set_m_type(struct ast_rtp* rtp, int pt) {
740 if (pt < 0 || pt > MAX_RTP_PT)
741 return; /* bogus payload type */
743 if (static_RTP_PT[pt].code != 0) {
744 rtp->current_RTP_PT[pt] = static_RTP_PT[pt];
748 /* Make a note of a RTP payload type (with MIME type) that was seen in */
749 /* a SDP "a=rtpmap:" line. */
750 void ast_rtp_set_rtpmap_type(struct ast_rtp* rtp, int pt,
751 char* mimeType, char* mimeSubtype) {
754 if (pt < 0 || pt > MAX_RTP_PT)
755 return; /* bogus payload type */
757 for (i = 0; i < sizeof mimeTypes/sizeof mimeTypes[0]; ++i) {
758 if (strcasecmp(mimeSubtype, mimeTypes[i].subtype) == 0 &&
759 strcasecmp(mimeType, mimeTypes[i].type) == 0) {
760 rtp->current_RTP_PT[pt] = mimeTypes[i].payloadType;
766 /* Return the union of all of the codecs that were set by rtp_set...() calls */
767 /* They're returned as two distinct sets: AST_FORMATs, and AST_RTPs */
768 void ast_rtp_get_current_formats(struct ast_rtp* rtp,
769 int* astFormats, int* nonAstFormats) {
772 *astFormats = *nonAstFormats = 0;
773 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
774 if (rtp->current_RTP_PT[pt].isAstFormat) {
775 *astFormats |= rtp->current_RTP_PT[pt].code;
777 *nonAstFormats |= rtp->current_RTP_PT[pt].code;
782 void ast_rtp_offered_from_local(struct ast_rtp* rtp, int local) {
784 rtp->rtp_offered_from_local = local;
786 ast_log(LOG_WARNING, "rtp structure is null\n");
789 struct rtpPayloadType ast_rtp_lookup_pt(struct ast_rtp* rtp, int pt)
791 struct rtpPayloadType result;
793 result.isAstFormat = result.code = 0;
794 if (pt < 0 || pt > MAX_RTP_PT)
795 return result; /* bogus payload type */
797 /* Start with the negotiated codecs */
798 if (!rtp->rtp_offered_from_local)
799 result = rtp->current_RTP_PT[pt];
801 /* If it doesn't exist, check our static RTP type list, just in case */
803 result = static_RTP_PT[pt];
807 /* Looks up an RTP code out of our *static* outbound list */
808 int ast_rtp_lookup_code(struct ast_rtp* rtp, const int isAstFormat, const int code) {
812 if (isAstFormat == rtp->rtp_lookup_code_cache_isAstFormat &&
813 code == rtp->rtp_lookup_code_cache_code) {
815 /* Use our cached mapping, to avoid the overhead of the loop below */
816 return rtp->rtp_lookup_code_cache_result;
819 /* Check the dynamic list first */
820 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
821 if (rtp->current_RTP_PT[pt].code == code && rtp->current_RTP_PT[pt].isAstFormat == isAstFormat) {
822 rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
823 rtp->rtp_lookup_code_cache_code = code;
824 rtp->rtp_lookup_code_cache_result = pt;
829 /* Then the static list */
830 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
831 if (static_RTP_PT[pt].code == code && static_RTP_PT[pt].isAstFormat == isAstFormat) {
832 rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
833 rtp->rtp_lookup_code_cache_code = code;
834 rtp->rtp_lookup_code_cache_result = pt;
841 char* ast_rtp_lookup_mime_subtype(const int isAstFormat, const int code) {
845 for (i = 0; i < sizeof mimeTypes/sizeof mimeTypes[0]; ++i) {
846 if (mimeTypes[i].payloadType.code == code && mimeTypes[i].payloadType.isAstFormat == isAstFormat) {
847 return mimeTypes[i].subtype;
853 char *ast_rtp_lookup_mime_multiple(char *buf, int size, const int capability, const int isAstFormat)
860 snprintf(buf, size, "0x%x (", capability);
862 for (format = 1; format < AST_RTP_MAX; format <<= 1) {
863 if (capability & format) {
864 const char *name = ast_rtp_lookup_mime_subtype(isAstFormat, format);
865 snprintf(buf + strlen(buf), size - strlen(buf), "%s|", name);
868 if (!ast_strlen_zero(buf))
869 buf[strlen(buf)] = ')';
874 static int rtp_socket(void)
878 s = socket(AF_INET, SOCK_DGRAM, 0);
880 flags = fcntl(s, F_GETFL);
881 fcntl(s, F_SETFL, flags | O_NONBLOCK);
884 setsockopt(s, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
890 static struct ast_rtcp *ast_rtcp_new(void)
892 struct ast_rtcp *rtcp;
893 rtcp = malloc(sizeof(struct ast_rtcp));
896 memset(rtcp, 0, sizeof(struct ast_rtcp));
897 rtcp->s = rtp_socket();
898 rtcp->us.sin_family = AF_INET;
901 ast_log(LOG_WARNING, "Unable to allocate socket: %s\n", strerror(errno));
907 struct ast_rtp *ast_rtp_new_with_bindaddr(struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode, struct in_addr addr)
913 rtp = malloc(sizeof(struct ast_rtp));
916 memset(rtp, 0, sizeof(struct ast_rtp));
917 rtp->them.sin_family = AF_INET;
918 rtp->us.sin_family = AF_INET;
919 rtp->s = rtp_socket();
921 rtp->seqno = rand() & 0xffff;
924 ast_log(LOG_ERROR, "Unable to allocate socket: %s\n", strerror(errno));
927 if (sched && rtcpenable) {
929 rtp->rtcp = ast_rtcp_new();
931 /* Find us a place */
932 x = (rand() % (rtpend-rtpstart)) + rtpstart;
936 /* Must be an even port number by RTP spec */
937 rtp->us.sin_port = htons(x);
938 rtp->us.sin_addr = addr;
940 rtp->rtcp->us.sin_port = htons(x + 1);
941 if (!(first = bind(rtp->s, (struct sockaddr *)&rtp->us, sizeof(rtp->us))) &&
942 (!rtp->rtcp || !bind(rtp->rtcp->s, (struct sockaddr *)&rtp->rtcp->us, sizeof(rtp->rtcp->us))))
945 /* Primary bind succeeded! Gotta recreate it */
947 rtp->s = rtp_socket();
949 if (errno != EADDRINUSE) {
950 ast_log(LOG_ERROR, "Unexpected bind error: %s\n", strerror(errno));
961 x = (rtpstart + 1) & ~1;
962 if (x == startplace) {
963 ast_log(LOG_ERROR, "No RTP ports remaining\n");
973 if (io && sched && callbackmode) {
974 /* Operate this one in a callback mode */
977 rtp->ioid = ast_io_add(rtp->io, rtp->s, rtpread, AST_IO_IN, rtp);
979 ast_rtp_pt_default(rtp);
983 struct ast_rtp *ast_rtp_new(struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode)
987 memset(&ia, 0, sizeof(ia));
988 return ast_rtp_new_with_bindaddr(sched, io, rtcpenable, callbackmode, ia);
991 int ast_rtp_settos(struct ast_rtp *rtp, int tos)
995 if ((res = setsockopt(rtp->s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))))
996 ast_log(LOG_WARNING, "Unable to set TOS to %d\n", tos);
1000 void ast_rtp_set_peer(struct ast_rtp *rtp, struct sockaddr_in *them)
1002 rtp->them.sin_port = them->sin_port;
1003 rtp->them.sin_addr = them->sin_addr;
1005 rtp->rtcp->them.sin_port = htons(ntohs(them->sin_port) + 1);
1006 rtp->rtcp->them.sin_addr = them->sin_addr;
1011 void ast_rtp_get_peer(struct ast_rtp *rtp, struct sockaddr_in *them)
1013 them->sin_family = AF_INET;
1014 them->sin_port = rtp->them.sin_port;
1015 them->sin_addr = rtp->them.sin_addr;
1018 void ast_rtp_get_us(struct ast_rtp *rtp, struct sockaddr_in *us)
1020 memcpy(us, &rtp->us, sizeof(rtp->us));
1023 void ast_rtp_stop(struct ast_rtp *rtp)
1025 memset(&rtp->them.sin_addr, 0, sizeof(rtp->them.sin_addr));
1026 memset(&rtp->them.sin_port, 0, sizeof(rtp->them.sin_port));
1028 memset(&rtp->rtcp->them.sin_addr, 0, sizeof(rtp->them.sin_addr));
1029 memset(&rtp->rtcp->them.sin_port, 0, sizeof(rtp->them.sin_port));
1033 void ast_rtp_reset(struct ast_rtp *rtp)
1035 memset(&rtp->rxcore, 0, sizeof(rtp->rxcore));
1036 memset(&rtp->txcore, 0, sizeof(rtp->txcore));
1037 memset(&rtp->dtmfmute, 0, sizeof(rtp->dtmfmute));
1039 rtp->lastdigitts = 0;
1041 rtp->lastividtimestamp = 0;
1042 rtp->lastovidtimestamp = 0;
1043 rtp->lasteventseqn = 0;
1044 rtp->lasttxformat = 0;
1045 rtp->lastrxformat = 0;
1047 rtp->dtmfduration = 0;
1052 void ast_rtp_destroy(struct ast_rtp *rtp)
1055 ast_smoother_free(rtp->smoother);
1057 ast_io_remove(rtp->io, rtp->ioid);
1061 close(rtp->rtcp->s);
1067 static unsigned int calc_txstamp(struct ast_rtp *rtp, struct timeval *delivery)
1071 if (!rtp->txcore.tv_sec && !rtp->txcore.tv_usec) {
1072 gettimeofday(&rtp->txcore, NULL);
1073 /* Round to 20ms for nice, pretty timestamps */
1074 rtp->txcore.tv_usec -= rtp->txcore.tv_usec % 20000;
1076 if (delivery && (delivery->tv_sec || delivery->tv_usec)) {
1077 /* Use previous txcore */
1078 ms = (delivery->tv_sec - rtp->txcore.tv_sec) * 1000;
1079 ms += (1000000 + delivery->tv_usec - rtp->txcore.tv_usec) / 1000 - 1000;
1080 rtp->txcore.tv_sec = delivery->tv_sec;
1081 rtp->txcore.tv_usec = delivery->tv_usec;
1083 gettimeofday(&now, NULL);
1084 ms = (now.tv_sec - rtp->txcore.tv_sec) * 1000;
1085 ms += (1000000 + now.tv_usec - rtp->txcore.tv_usec) / 1000 - 1000;
1086 /* Use what we just got for next time */
1087 rtp->txcore.tv_sec = now.tv_sec;
1088 rtp->txcore.tv_usec = now.tv_usec;
1093 int ast_rtp_senddigit(struct ast_rtp *rtp, char digit)
1095 unsigned int *rtpheader;
1101 char iabuf[INET_ADDRSTRLEN];
1103 if ((digit <= '9') && (digit >= '0'))
1105 else if (digit == '*')
1107 else if (digit == '#')
1109 else if ((digit >= 'A') && (digit <= 'D'))
1110 digit = digit - 'A' + 12;
1111 else if ((digit >= 'a') && (digit <= 'd'))
1112 digit = digit - 'a' + 12;
1114 ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
1117 payload = ast_rtp_lookup_code(rtp, 0, AST_RTP_DTMF);
1119 /* If we have no peer, return immediately */
1120 if (!rtp->them.sin_addr.s_addr)
1123 gettimeofday(&rtp->dtmfmute, NULL);
1124 rtp->dtmfmute.tv_usec += (500 * 1000);
1125 if (rtp->dtmfmute.tv_usec > 1000000) {
1126 rtp->dtmfmute.tv_usec -= 1000000;
1127 rtp->dtmfmute.tv_sec += 1;
1130 /* Get a pointer to the header */
1131 rtpheader = (unsigned int *)data;
1132 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno++));
1133 rtpheader[1] = htonl(rtp->lastdigitts);
1134 rtpheader[2] = htonl(rtp->ssrc);
1135 rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (0));
1137 if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
1138 res = sendto(rtp->s, (void *)rtpheader, hdrlen + 4, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
1140 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));
1141 if(rtp_debug_test_addr(&rtp->them))
1142 ast_verbose("Sent RTP packet to %s:%d (type %d, seq %d, ts %d, len %d)\n"
1143 , ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr), ntohs(rtp->them.sin_port), payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
1146 /* Clear marker bit and increment seqno */
1147 rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno++));
1148 /* For the last three packets, set the duration and the end bit */
1150 /* Make duration 800 (100ms) */
1151 rtpheader[3] |= htonl((800));
1152 /* Set the End bit for the last 3 */
1153 rtpheader[3] |= htonl((1 << 23));
1156 /* Increment the digit timestamp by 100ms, to ensure that digits
1157 sent sequentially with no intervening non-digit packets do not
1158 get sent with the same timestamp.
1160 rtp->lastdigitts += 800;
1164 int ast_rtp_sendcng(struct ast_rtp *rtp, int level)
1166 unsigned int *rtpheader;
1171 char iabuf[INET_ADDRSTRLEN];
1172 level = 127 - (level & 0x7f);
1173 payload = ast_rtp_lookup_code(rtp, 0, AST_RTP_CN);
1175 /* If we have no peer, return immediately */
1176 if (!rtp->them.sin_addr.s_addr)
1179 gettimeofday(&rtp->dtmfmute, NULL);
1180 rtp->dtmfmute.tv_usec += (500 * 1000);
1181 if (rtp->dtmfmute.tv_usec > 1000000) {
1182 rtp->dtmfmute.tv_usec -= 1000000;
1183 rtp->dtmfmute.tv_sec += 1;
1186 /* Get a pointer to the header */
1187 rtpheader = (unsigned int *)data;
1188 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno++));
1189 rtpheader[1] = htonl(rtp->lastts);
1190 rtpheader[2] = htonl(rtp->ssrc);
1192 if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
1193 res = sendto(rtp->s, (void *)rtpheader, hdrlen + 1, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
1195 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));
1196 if(rtp_debug_test_addr(&rtp->them))
1197 ast_verbose("Sent Comfort Noise RTP packet to %s:%d (type %d, seq %d, ts %d, len %d)\n"
1198 , ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr), ntohs(rtp->them.sin_port), payload, rtp->seqno, rtp->lastts,res - hdrlen);
1204 static int ast_rtp_raw_write(struct ast_rtp *rtp, struct ast_frame *f, int codec)
1206 unsigned char *rtpheader;
1207 char iabuf[INET_ADDRSTRLEN];
1214 ms = calc_txstamp(rtp, &f->delivery);
1215 /* Default prediction */
1216 if (f->subclass < AST_FORMAT_MAX_AUDIO) {
1217 pred = rtp->lastts + ms * 8;
1219 switch(f->subclass) {
1220 case AST_FORMAT_ULAW:
1221 case AST_FORMAT_ALAW:
1222 /* If we're within +/- 20ms from when where we
1223 predict we should be, use that */
1224 pred = rtp->lastts + f->datalen;
1226 case AST_FORMAT_ADPCM:
1227 case AST_FORMAT_G726:
1228 /* If we're within +/- 20ms from when where we
1229 predict we should be, use that */
1230 pred = rtp->lastts + f->datalen * 2;
1232 case AST_FORMAT_G729A:
1233 pred = rtp->lastts + f->datalen * 8;
1235 case AST_FORMAT_GSM:
1236 pred = rtp->lastts + (f->datalen * 160 / 33);
1238 case AST_FORMAT_ILBC:
1239 pred = rtp->lastts + (f->datalen * 240 / 50);
1241 case AST_FORMAT_G723_1:
1242 pred = rtp->lastts + g723_samples(f->data, f->datalen);
1244 case AST_FORMAT_SPEEX:
1245 pred = rtp->lastts + 160;
1246 /* assumes that the RTP packet contains one Speex frame */
1248 case AST_FORMAT_LPC10:
1249 /* assumes that the RTP packet contains one LPC10 frame */
1250 pred = rtp->lastts + 22 * 8;
1251 pred += (((char *)(f->data))[7] & 0x1) * 8;
1254 ast_log(LOG_WARNING, "Not sure about timestamp format for codec format %s\n", ast_getformatname(f->subclass));
1256 /* Re-calculate last TS */
1257 rtp->lastts = rtp->lastts + ms * 8;
1258 if (!f->delivery.tv_sec && !f->delivery.tv_usec) {
1259 /* If this isn't an absolute delivery time, Check if it is close to our prediction,
1260 and if so, go with our prediction */
1261 if (abs(rtp->lastts - pred) < MAX_TIMESTAMP_SKEW)
1264 if (option_debug > 2)
1265 ast_log(LOG_DEBUG, "Difference is %d, ms is %d\n", abs(rtp->lastts - pred), ms);
1270 mark = f->subclass & 0x1;
1271 pred = rtp->lastovidtimestamp + f->samples;
1272 /* Re-calculate last TS */
1273 rtp->lastts = rtp->lastts + ms * 90;
1274 /* If it's close to our prediction, go for it */
1275 if (!f->delivery.tv_sec && !f->delivery.tv_usec) {
1276 if (abs(rtp->lastts - pred) < 7200) {
1278 rtp->lastovidtimestamp += f->samples;
1280 if (option_debug > 2)
1281 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);
1282 rtp->lastovidtimestamp = rtp->lastts;
1286 /* If the timestamp for non-digit packets has moved beyond the timestamp
1287 for digits, update the digit timestamp.
1289 if (rtp->lastts > rtp->lastdigitts)
1290 rtp->lastdigitts = rtp->lastts;
1292 /* Get a pointer to the header */
1293 rtpheader = (unsigned char *)(f->data - hdrlen);
1295 put_unaligned_uint32(rtpheader, htonl((2 << 30) | (codec << 16) | (rtp->seqno) | (mark << 23)));
1296 put_unaligned_uint32(rtpheader + 4, htonl(rtp->lastts));
1297 put_unaligned_uint32(rtpheader + 8, htonl(rtp->ssrc));
1299 if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
1300 res = sendto(rtp->s, (void *)rtpheader, f->datalen + hdrlen, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
1302 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));
1303 if(rtp_debug_test_addr(&rtp->them))
1304 ast_verbose("Sent RTP packet to %s:%d (type %d, seq %d, ts %d, len %d)\n"
1305 , ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr), ntohs(rtp->them.sin_port), codec, rtp->seqno, rtp->lastts,res - hdrlen);
1313 int ast_rtp_write(struct ast_rtp *rtp, struct ast_frame *_f)
1315 struct ast_frame *f;
1321 /* If we have no peer, return immediately */
1322 if (!rtp->them.sin_addr.s_addr)
1325 /* If there is no data length, return immediately */
1329 /* Make sure we have enough space for RTP header */
1330 if ((_f->frametype != AST_FRAME_VOICE) && (_f->frametype != AST_FRAME_VIDEO)) {
1331 ast_log(LOG_WARNING, "RTP can only send voice\n");
1335 subclass = _f->subclass;
1336 if (_f->frametype == AST_FRAME_VIDEO)
1339 codec = ast_rtp_lookup_code(rtp, 1, subclass);
1341 ast_log(LOG_WARNING, "Don't know how to send format %s packets with RTP\n", ast_getformatname(_f->subclass));
1345 if (rtp->lasttxformat != subclass) {
1346 /* New format, reset the smoother */
1348 ast_log(LOG_DEBUG, "Ooh, format changed from %s to %s\n", ast_getformatname(rtp->lasttxformat), ast_getformatname(subclass));
1349 rtp->lasttxformat = subclass;
1351 ast_smoother_free(rtp->smoother);
1352 rtp->smoother = NULL;
1357 case AST_FORMAT_SLINEAR:
1358 if (!rtp->smoother) {
1359 rtp->smoother = ast_smoother_new(320);
1361 if (!rtp->smoother) {
1362 ast_log(LOG_WARNING, "Unable to create smoother :(\n");
1365 ast_smoother_feed_be(rtp->smoother, _f);
1367 while((f = ast_smoother_read(rtp->smoother)))
1368 ast_rtp_raw_write(rtp, f, codec);
1370 case AST_FORMAT_ULAW:
1371 case AST_FORMAT_ALAW:
1372 if (!rtp->smoother) {
1373 rtp->smoother = ast_smoother_new(160);
1375 if (!rtp->smoother) {
1376 ast_log(LOG_WARNING, "Unable to create smoother :(\n");
1379 ast_smoother_feed(rtp->smoother, _f);
1381 while((f = ast_smoother_read(rtp->smoother)))
1382 ast_rtp_raw_write(rtp, f, codec);
1384 case AST_FORMAT_ADPCM:
1385 case AST_FORMAT_G726:
1386 if (!rtp->smoother) {
1387 rtp->smoother = ast_smoother_new(80);
1389 if (!rtp->smoother) {
1390 ast_log(LOG_WARNING, "Unable to create smoother :(\n");
1393 ast_smoother_feed(rtp->smoother, _f);
1395 while((f = ast_smoother_read(rtp->smoother)))
1396 ast_rtp_raw_write(rtp, f, codec);
1398 case AST_FORMAT_G729A:
1399 if (!rtp->smoother) {
1400 rtp->smoother = ast_smoother_new(20);
1402 ast_smoother_set_flags(rtp->smoother, AST_SMOOTHER_FLAG_G729);
1404 if (!rtp->smoother) {
1405 ast_log(LOG_WARNING, "Unable to create g729 smoother :(\n");
1408 ast_smoother_feed(rtp->smoother, _f);
1410 while((f = ast_smoother_read(rtp->smoother)))
1411 ast_rtp_raw_write(rtp, f, codec);
1413 case AST_FORMAT_GSM:
1414 if (!rtp->smoother) {
1415 rtp->smoother = ast_smoother_new(33);
1417 if (!rtp->smoother) {
1418 ast_log(LOG_WARNING, "Unable to create GSM smoother :(\n");
1421 ast_smoother_feed(rtp->smoother, _f);
1422 while((f = ast_smoother_read(rtp->smoother)))
1423 ast_rtp_raw_write(rtp, f, codec);
1425 case AST_FORMAT_ILBC:
1426 if (!rtp->smoother) {
1427 rtp->smoother = ast_smoother_new(50);
1429 if (!rtp->smoother) {
1430 ast_log(LOG_WARNING, "Unable to create ILBC smoother :(\n");
1433 ast_smoother_feed(rtp->smoother, _f);
1434 while((f = ast_smoother_read(rtp->smoother)))
1435 ast_rtp_raw_write(rtp, f, codec);
1438 ast_log(LOG_WARNING, "Not sure about sending format %s packets\n", ast_getformatname(subclass));
1439 /* fall through to... */
1440 case AST_FORMAT_H261:
1441 case AST_FORMAT_H263:
1442 case AST_FORMAT_H263_PLUS:
1443 case AST_FORMAT_G723_1:
1444 case AST_FORMAT_LPC10:
1445 case AST_FORMAT_SPEEX:
1446 /* Don't buffer outgoing frames; send them one-per-packet: */
1447 if (_f->offset < hdrlen) {
1452 ast_rtp_raw_write(rtp, f, codec);
1458 void ast_rtp_proto_unregister(struct ast_rtp_protocol *proto)
1460 struct ast_rtp_protocol *cur, *prev;
1467 prev->next = proto->next;
1469 protos = proto->next;
1477 int ast_rtp_proto_register(struct ast_rtp_protocol *proto)
1479 struct ast_rtp_protocol *cur;
1482 if (cur->type == proto->type) {
1483 ast_log(LOG_WARNING, "Tried to register same protocol '%s' twice\n", cur->type);
1488 proto->next = protos;
1493 static struct ast_rtp_protocol *get_proto(struct ast_channel *chan)
1495 struct ast_rtp_protocol *cur;
1498 if (cur->type == chan->type) {
1506 int ast_rtp_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
1508 struct ast_frame *f;
1509 struct ast_channel *who, *cs[3];
1510 struct ast_rtp *p0, *p1;
1511 struct ast_rtp *vp0, *vp1;
1512 struct ast_rtp_protocol *pr0, *pr1;
1513 struct sockaddr_in ac0, ac1;
1514 struct sockaddr_in vac0, vac1;
1515 struct sockaddr_in t0, t1;
1516 struct sockaddr_in vt0, vt1;
1517 char iabuf[INET_ADDRSTRLEN];
1521 int codec0,codec1, oldcodec0, oldcodec1;
1523 memset(&vt0, 0, sizeof(vt0));
1524 memset(&vt1, 0, sizeof(vt1));
1525 memset(&vac0, 0, sizeof(vac0));
1526 memset(&vac1, 0, sizeof(vac1));
1528 /* if need DTMF, cant native bridge */
1529 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
1531 ast_mutex_lock(&c0->lock);
1532 while(ast_mutex_trylock(&c1->lock)) {
1533 ast_mutex_unlock(&c0->lock);
1535 ast_mutex_lock(&c0->lock);
1537 pr0 = get_proto(c0);
1538 pr1 = get_proto(c1);
1540 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c0->name);
1541 ast_mutex_unlock(&c0->lock);
1542 ast_mutex_unlock(&c1->lock);
1546 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c1->name);
1547 ast_mutex_unlock(&c0->lock);
1548 ast_mutex_unlock(&c1->lock);
1551 pvt0 = c0->tech_pvt;
1552 pvt1 = c1->tech_pvt;
1553 p0 = pr0->get_rtp_info(c0);
1554 if (pr0->get_vrtp_info)
1555 vp0 = pr0->get_vrtp_info(c0);
1558 p1 = pr1->get_rtp_info(c1);
1559 if (pr1->get_vrtp_info)
1560 vp1 = pr1->get_vrtp_info(c1);
1564 /* Somebody doesn't want to play... */
1565 ast_mutex_unlock(&c0->lock);
1566 ast_mutex_unlock(&c1->lock);
1570 codec0 = pr0->get_codec(c0);
1574 codec1 = pr1->get_codec(c1);
1577 if (pr0->get_codec && pr1->get_codec) {
1578 /* Hey, we can't do reinvite if both parties speak diffrent codecs */
1579 if (!(codec0 & codec1)) {
1580 ast_log(LOG_WARNING, "codec0 = %d is not codec1 = %d, cannot native bridge.\n",codec0,codec1);
1581 ast_mutex_unlock(&c0->lock);
1582 ast_mutex_unlock(&c1->lock);
1586 if (pr0->set_rtp_peer(c0, p1, vp1, codec1))
1587 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
1589 /* Store RTP peer */
1590 ast_rtp_get_peer(p1, &ac1);
1592 ast_rtp_get_peer(vp1, &vac1);
1594 if (pr1->set_rtp_peer(c1, p0, vp0, codec0))
1595 ast_log(LOG_WARNING, "Channel '%s' failed to talk back to '%s'\n", c1->name, c0->name);
1597 /* Store RTP peer */
1598 ast_rtp_get_peer(p0, &ac0);
1600 ast_rtp_get_peer(vp0, &vac0);
1602 ast_mutex_unlock(&c0->lock);
1603 ast_mutex_unlock(&c1->lock);
1610 if ((c0->tech_pvt != pvt0) ||
1611 (c1->tech_pvt != pvt1) ||
1612 (c0->masq || c0->masqr || c1->masq || c1->masqr)) {
1613 ast_log(LOG_DEBUG, "Oooh, something is weird, backing out\n");
1614 if (c0->tech_pvt == pvt0) {
1615 if (pr0->set_rtp_peer(c0, NULL, NULL, 0))
1616 ast_log(LOG_WARNING, "Channel '%s' failed to revert\n", c0->name);
1618 if (c1->tech_pvt == pvt1) {
1619 if (pr1->set_rtp_peer(c1, NULL, NULL, 0))
1620 ast_log(LOG_WARNING, "Channel '%s' failed to revert back\n", c1->name);
1622 /* Tell it to try again later */
1626 ast_rtp_get_peer(p1, &t1);
1627 ast_rtp_get_peer(p0, &t0);
1629 codec0 = pr0->get_codec(c0);
1631 codec1 = pr1->get_codec(c1);
1633 ast_rtp_get_peer(vp1, &vt1);
1635 ast_rtp_get_peer(vp0, &vt0);
1636 if (inaddrcmp(&t1, &ac1) || (vp1 && inaddrcmp(&vt1, &vac1)) || (codec1 != oldcodec1)) {
1638 ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
1639 c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), t1.sin_addr), ntohs(t1.sin_port), codec1);
1640 ast_log(LOG_DEBUG, "Oooh, '%s' changed end vaddress to %s:%d (format %d)\n",
1641 c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), vt1.sin_addr), ntohs(vt1.sin_port), codec1);
1642 ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d/(format %d)\n",
1643 c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), ac1.sin_addr), ntohs(ac1.sin_port), oldcodec1);
1644 ast_log(LOG_DEBUG, "Oooh, '%s' wasv %s:%d/(format %d)\n",
1645 c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), vac1.sin_addr), ntohs(vac1.sin_port), oldcodec1);
1647 if (pr0->set_rtp_peer(c0, t1.sin_addr.s_addr ? p1 : NULL, vt1.sin_addr.s_addr ? vp1 : NULL, codec1))
1648 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c0->name, c1->name);
1649 memcpy(&ac1, &t1, sizeof(ac1));
1650 memcpy(&vac1, &vt1, sizeof(vac1));
1653 if (inaddrcmp(&t0, &ac0) || (vp0 && inaddrcmp(&vt0, &vac0))) {
1655 ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
1656 c0->name, ast_inet_ntoa(iabuf, sizeof(iabuf), t0.sin_addr), ntohs(t0.sin_port), codec0);
1657 ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d/(format %d)\n",
1658 c0->name, ast_inet_ntoa(iabuf, sizeof(iabuf), ac0.sin_addr), ntohs(ac0.sin_port), oldcodec0);
1660 if (pr1->set_rtp_peer(c1, t0.sin_addr.s_addr ? p0 : NULL, vt0.sin_addr.s_addr ? vp0 : NULL, codec0))
1661 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c1->name, c0->name);
1662 memcpy(&ac0, &t0, sizeof(ac0));
1663 memcpy(&vac0, &vt0, sizeof(vac0));
1666 who = ast_waitfor_n(cs, 2, &to);
1669 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
1670 /* check for hagnup / whentohangup */
1671 if (ast_check_hangup(c0) || ast_check_hangup(c1))
1676 if (!f || ((f->frametype == AST_FRAME_DTMF) &&
1677 (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
1678 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))))) {
1682 ast_log(LOG_DEBUG, "Oooh, got a %s\n", f ? "digit" : "hangup");
1683 if ((c0->tech_pvt == pvt0) && (!c0->_softhangup)) {
1684 if (pr0->set_rtp_peer(c0, NULL, NULL, 0))
1685 ast_log(LOG_WARNING, "Channel '%s' failed to revert\n", c0->name);
1687 if ((c1->tech_pvt == pvt1) && (!c1->_softhangup)) {
1688 if (pr1->set_rtp_peer(c1, NULL, NULL, 0))
1689 ast_log(LOG_WARNING, "Channel '%s' failed to revert back\n", c1->name);
1691 /* That's all we needed */
1694 if ((f->frametype == AST_FRAME_DTMF) ||
1695 (f->frametype == AST_FRAME_VOICE) ||
1696 (f->frametype == AST_FRAME_VIDEO)) {
1697 /* Forward voice or DTMF frames if they happen upon us */
1700 } else if (who == c1) {
1706 /* Swap priority not that it's a big deal at this point */
1715 static int rtp_do_debug_ip(int fd, int argc, char *argv[])
1718 struct ast_hostent ahp;
1719 char iabuf[INET_ADDRSTRLEN];
1724 return RESULT_SHOWUSAGE;
1726 p = strstr(arg, ":");
1732 hp = ast_gethostbyname(arg, &ahp);
1734 return RESULT_SHOWUSAGE;
1735 rtpdebugaddr.sin_family = AF_INET;
1736 memcpy(&rtpdebugaddr.sin_addr, hp->h_addr, sizeof(rtpdebugaddr.sin_addr));
1737 rtpdebugaddr.sin_port = htons(port);
1739 ast_cli(fd, "RTP Debugging Enabled for IP: %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), rtpdebugaddr.sin_addr));
1741 ast_cli(fd, "RTP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(iabuf, sizeof(iabuf), rtpdebugaddr.sin_addr), port);
1743 return RESULT_SUCCESS;
1746 static int rtp_do_debug(int fd, int argc, char *argv[])
1750 return RESULT_SHOWUSAGE;
1751 return rtp_do_debug_ip(fd, argc, argv);
1754 memset(&rtpdebugaddr,0,sizeof(rtpdebugaddr));
1755 ast_cli(fd, "RTP Debugging Enabled\n");
1756 return RESULT_SUCCESS;
1759 static int rtp_no_debug(int fd, int argc, char *argv[])
1762 return RESULT_SHOWUSAGE;
1764 ast_cli(fd,"RTP Debugging Disabled\n");
1765 return RESULT_SUCCESS;
1768 static char debug_usage[] =
1769 "Usage: rtp debug [ip host[:port]]\n"
1770 " Enable dumping of all RTP packets to and from host.\n";
1772 static char no_debug_usage[] =
1773 "Usage: rtp no debug\n"
1774 " Disable all RTP debugging\n";
1776 static struct ast_cli_entry cli_debug_ip =
1777 {{ "rtp", "debug", "ip", NULL } , rtp_do_debug, "Enable RTP debugging on IP", debug_usage };
1779 static struct ast_cli_entry cli_debug =
1780 {{ "rtp", "debug", NULL } , rtp_do_debug, "Enable RTP debugging", debug_usage };
1782 static struct ast_cli_entry cli_no_debug =
1783 {{ "rtp", "no", "debug", NULL } , rtp_no_debug, "Disable RTP debugging", no_debug_usage };
1785 void ast_rtp_reload(void)
1787 struct ast_config *cfg;
1792 cfg = ast_config_load("rtp.conf");
1794 if ((s = ast_variable_retrieve(cfg, "general", "rtpstart"))) {
1796 if (rtpstart < 1024)
1798 if (rtpstart > 65535)
1801 if ((s = ast_variable_retrieve(cfg, "general", "rtpend"))) {
1808 if ((s = ast_variable_retrieve(cfg, "general", "rtpchecksums"))) {
1816 ast_log(LOG_WARNING, "Disabling RTP checksums is not supported on this operating system!\n");
1819 ast_config_destroy(cfg);
1821 if (rtpstart >= rtpend) {
1822 ast_log(LOG_WARNING, "Unreasonable values for RTP start in rtp.conf/end\n");
1826 if (option_verbose > 1)
1827 ast_verbose(VERBOSE_PREFIX_2 "RTP Allocating from port range %d -> %d\n", rtpstart, rtpend);
1831 void ast_rtp_init(void)
1833 ast_cli_register(&cli_debug);
1834 ast_cli_register(&cli_debug_ip);
1835 ast_cli_register(&cli_no_debug);