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;
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))) {
190 ast_log(LOG_DEBUG, "Ignore potential DTMF echo from '%s'\n", ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr));
192 rtp->dtmfduration = 0;
196 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));
197 if (rtp->resp == 'X') {
198 rtp->f.frametype = AST_FRAME_CONTROL;
199 rtp->f.subclass = AST_CONTROL_FLASH;
201 rtp->f.frametype = AST_FRAME_DTMF;
202 rtp->f.subclass = rtp->resp;
209 rtp->dtmfduration = 0;
214 static inline int rtp_debug_test_addr(struct sockaddr_in *addr)
218 if (rtpdebugaddr.sin_addr.s_addr) {
219 if (((ntohs(rtpdebugaddr.sin_port) != 0)
220 && (rtpdebugaddr.sin_port != addr->sin_port))
221 || (rtpdebugaddr.sin_addr.s_addr != addr->sin_addr.s_addr))
227 static struct ast_frame *process_cisco_dtmf(struct ast_rtp *rtp, unsigned char *data, int len)
231 struct ast_frame *f = NULL;
232 event = ntohl(*((unsigned int *)(data)));
235 printf("Cisco Digit: %08x (len = %d)\n", event, len);
239 } else if (event < 11) {
241 } else if (event < 12) {
243 } else if (event < 16) {
244 resp = 'A' + (event - 12);
245 } else if (event < 17) {
248 if (rtp->resp && (rtp->resp != resp)) {
252 rtp->dtmfcount = dtmftimeout;
256 static struct ast_frame *process_rfc2833(struct ast_rtp *rtp, unsigned char *data, int len)
259 unsigned int event_end;
260 unsigned int duration;
262 struct ast_frame *f = NULL;
263 event = ntohl(*((unsigned int *)(data)));
265 event_end = ntohl(*((unsigned int *)(data)));
268 duration = ntohl(*((unsigned int *)(data)));
271 printf("Event: %08x (len = %d)\n", event, len);
275 } else if (event < 11) {
277 } else if (event < 12) {
279 } else if (event < 16) {
280 resp = 'A' + (event - 12);
281 } else if (event < 17) {
284 if (rtp->resp && (rtp->resp != resp)) {
287 else if(event_end & 0x80)
296 else if(rtp->dtmfduration && (duration < rtp->dtmfduration))
300 if (!(event_end & 0x80))
302 rtp->dtmfcount = dtmftimeout;
303 rtp->dtmfduration = duration;
307 static struct ast_frame *process_rfc3389(struct ast_rtp *rtp, unsigned char *data, int len)
309 struct ast_frame *f = NULL;
310 /* Convert comfort noise into audio with various codecs. Unfortunately this doesn't
311 totally help us out becuase we don't have an engine to keep it going and we are not
312 guaranteed to have it every 20ms or anything */
314 printf("RFC3389: %d bytes, level %d...\n", len, rtp->lastrxformat);
316 if (!(rtp->flags & FLAG_3389_WARNING)) {
317 ast_log(LOG_NOTICE, "RFC3389 support incomplete. Turn off on client if possible\n");
318 rtp->flags |= FLAG_3389_WARNING;
320 /* Must have at least one byte */
324 rtp->f.data = rtp->rawdata + AST_FRIENDLY_OFFSET;
325 rtp->f.datalen = len - 1;
326 rtp->f.offset = AST_FRIENDLY_OFFSET;
327 memcpy(rtp->f.data, data + 1, len - 1);
333 rtp->f.frametype = AST_FRAME_CNG;
334 rtp->f.subclass = data[0] & 0x7f;
335 rtp->f.datalen = len - 1;
337 rtp->f.delivery.tv_usec = rtp->f.delivery.tv_sec = 0;
342 static int rtpread(int *id, int fd, short events, void *cbdata)
344 struct ast_rtp *rtp = cbdata;
346 f = ast_rtp_read(rtp);
349 rtp->callback(rtp, f, rtp->data);
354 struct ast_frame *ast_rtcp_read(struct ast_rtp *rtp)
356 static struct ast_frame null_frame = { AST_FRAME_NULL, };
360 struct sockaddr_in sin;
361 unsigned int rtcpdata[1024];
362 char iabuf[INET_ADDRSTRLEN];
369 res = recvfrom(rtp->rtcp->s, rtcpdata, sizeof(rtcpdata),
370 0, (struct sockaddr *)&sin, &len);
374 ast_log(LOG_NOTICE, "RTP: Received packet with bad UDP checksum\n");
376 ast_log(LOG_WARNING, "RTP Read error: %s\n", strerror(errno));
383 ast_log(LOG_WARNING, "RTP Read too short\n");
388 /* Send to whoever sent to us */
389 if ((rtp->rtcp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
390 (rtp->rtcp->them.sin_port != sin.sin_port)) {
391 memcpy(&rtp->them, &sin, sizeof(rtp->them));
394 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));
398 ast_log(LOG_DEBUG, "Got RTCP report of %d bytes\n", res);
402 static void calc_rxstamp(struct timeval *tv, struct ast_rtp *rtp, unsigned int timestamp, int mark)
404 if ((!rtp->rxcore.tv_sec && !rtp->rxcore.tv_usec) || mark) {
405 gettimeofday(&rtp->rxcore, NULL);
406 rtp->rxcore.tv_sec -= timestamp / 8000;
407 rtp->rxcore.tv_usec -= (timestamp % 8000) * 125;
408 /* Round to 20ms for nice, pretty timestamps */
409 rtp->rxcore.tv_usec -= rtp->rxcore.tv_usec % 20000;
410 if (rtp->rxcore.tv_usec < 0) {
411 /* Adjust appropriately if necessary */
412 rtp->rxcore.tv_usec += 1000000;
413 rtp->rxcore.tv_sec -= 1;
416 tv->tv_sec = rtp->rxcore.tv_sec + timestamp / 8000;
417 tv->tv_usec = rtp->rxcore.tv_usec + (timestamp % 8000) * 125;
418 if (tv->tv_usec >= 1000000) {
419 tv->tv_usec -= 1000000;
424 struct ast_frame *ast_rtp_read(struct ast_rtp *rtp)
427 struct sockaddr_in sin;
436 char iabuf[INET_ADDRSTRLEN];
437 unsigned int timestamp;
438 unsigned int *rtpheader;
439 static struct ast_frame *f, null_frame = { AST_FRAME_NULL, };
440 struct rtpPayloadType rtpPT;
444 /* Cache where the header will go */
445 res = recvfrom(rtp->s, rtp->rawdata + AST_FRIENDLY_OFFSET, sizeof(rtp->rawdata) - AST_FRIENDLY_OFFSET,
446 0, (struct sockaddr *)&sin, &len);
449 rtpheader = (unsigned int *)(rtp->rawdata + AST_FRIENDLY_OFFSET);
452 ast_log(LOG_NOTICE, "RTP: Received packet with bad UDP checksum\n");
454 ast_log(LOG_WARNING, "RTP Read error: %s\n", strerror(errno));
460 ast_log(LOG_WARNING, "RTP Read too short\n");
464 /* Ignore if the other side hasn't been given an address
466 if (!rtp->them.sin_addr.s_addr || !rtp->them.sin_port)
470 /* Send to whoever sent to us */
471 if ((rtp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
472 (rtp->them.sin_port != sin.sin_port)) {
473 memcpy(&rtp->them, &sin, sizeof(rtp->them));
475 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));
480 seqno = ntohl(rtpheader[0]);
482 /* Check RTP version */
483 version = (seqno & 0xC0000000) >> 30;
487 payloadtype = (seqno & 0x7f0000) >> 16;
488 mark = seqno & (1 << 23);
489 ext = seqno & (1 << 28);
491 timestamp = ntohl(rtpheader[1]);
493 /* RTP Extension present */
495 hdrlen += (ntohl(rtpheader[3]) & 0xffff) << 2;
499 ast_log(LOG_WARNING, "RTP Read too short (%d, expecting %d)\n", res, hdrlen);
503 if(rtp_debug_test_addr(&sin))
504 ast_verbose("Got RTP packet from %s:%d (type %d, seq %d, ts %d, len %d)\n"
505 , ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), ntohs(sin.sin_port), payloadtype, seqno, timestamp,res - hdrlen);
507 rtpPT = ast_rtp_lookup_pt(rtp, payloadtype);
508 if (!rtpPT.isAstFormat) {
509 /* This is special in-band data that's not one of our codecs */
510 if (rtpPT.code == AST_RTP_DTMF) {
511 /* It's special -- rfc2833 process it */
512 if (rtp->lasteventseqn <= seqno || rtp->resp == 0 || (rtp->lasteventseqn >= 65530 && seqno <= 6)) {
513 f = process_rfc2833(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
514 rtp->lasteventseqn = seqno;
521 } else if (rtpPT.code == AST_RTP_CISCO_DTMF) {
522 /* It's really special -- process it the Cisco way */
523 if (rtp->lasteventseqn <= seqno || rtp->resp == 0 || (rtp->lasteventseqn >= 65530 && seqno <= 6)) {
524 f = process_cisco_dtmf(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
525 rtp->lasteventseqn = seqno;
532 } else if (rtpPT.code == AST_RTP_CN) {
534 f = process_rfc3389(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
540 ast_log(LOG_NOTICE, "Unknown RTP codec %d received\n", payloadtype);
544 rtp->f.subclass = rtpPT.code;
545 if (rtp->f.subclass < AST_FORMAT_MAX_AUDIO)
546 rtp->f.frametype = AST_FRAME_VOICE;
548 rtp->f.frametype = AST_FRAME_VIDEO;
549 rtp->lastrxformat = rtp->f.subclass;
552 rtp->lastrxts = timestamp;
555 for (x=rtp->rxseqno + 1; x < seqno; x++) {
556 /* Queue empty frames */
562 rtp->f.src = "RTPMissedFrame";
565 rtp->rxseqno = seqno;
567 if (rtp->dtmfcount) {
569 printf("dtmfcount was %d\n", rtp->dtmfcount);
571 rtp->dtmfcount -= (timestamp - rtp->lastrxts);
572 if (rtp->dtmfcount < 0)
575 if (dtmftimeout != rtp->dtmfcount)
576 printf("dtmfcount is %d\n", rtp->dtmfcount);
579 rtp->lastrxts = timestamp;
581 /* Send any pending DTMF */
582 if (rtp->resp && !rtp->dtmfcount) {
584 ast_log(LOG_DEBUG, "Sending pending DTMF\n");
585 return send_dtmf(rtp);
588 rtp->f.datalen = res - hdrlen;
589 rtp->f.data = rtp->rawdata + hdrlen + AST_FRIENDLY_OFFSET;
590 rtp->f.offset = hdrlen + AST_FRIENDLY_OFFSET;
591 if (rtp->f.subclass < AST_FORMAT_MAX_AUDIO) {
592 switch(rtp->f.subclass) {
593 case AST_FORMAT_ULAW:
594 case AST_FORMAT_ALAW:
595 rtp->f.samples = rtp->f.datalen;
597 case AST_FORMAT_SLINEAR:
598 rtp->f.samples = rtp->f.datalen / 2;
601 rtp->f.samples = 160 * (rtp->f.datalen / 33);
603 case AST_FORMAT_ILBC:
604 rtp->f.samples = 240 * (rtp->f.datalen / 50);
606 case AST_FORMAT_ADPCM:
607 case AST_FORMAT_G726:
608 rtp->f.samples = rtp->f.datalen * 2;
610 case AST_FORMAT_G729A:
611 rtp->f.samples = rtp->f.datalen * 8;
613 case AST_FORMAT_G723_1:
614 rtp->f.samples = g723_samples(rtp->f.data, rtp->f.datalen);
616 case AST_FORMAT_SPEEX:
617 /* assumes that the RTP packet contained one Speex frame */
618 rtp->f.samples = 160;
620 case AST_FORMAT_LPC10:
621 rtp->f.samples = 22 * 8;
622 rtp->f.samples += (((char *)(rtp->f.data))[7] & 0x1) * 8;
625 ast_log(LOG_NOTICE, "Unable to calculate samples for format %s\n", ast_getformatname(rtp->f.subclass));
628 calc_rxstamp(&rtp->f.delivery, rtp, timestamp, mark);
630 /* Video -- samples is # of samples vs. 90000 */
631 if (!rtp->lastividtimestamp)
632 rtp->lastividtimestamp = timestamp;
633 rtp->f.samples = timestamp - rtp->lastividtimestamp;
634 rtp->lastividtimestamp = timestamp;
635 rtp->f.delivery.tv_sec = 0;
636 rtp->f.delivery.tv_usec = 0;
638 rtp->f.subclass |= 0x1;
645 /* The following array defines the MIME Media type (and subtype) for each
646 of our codecs, or RTP-specific data type. */
648 struct rtpPayloadType payloadType;
652 {{1, AST_FORMAT_G723_1}, "audio", "G723"},
653 {{1, AST_FORMAT_GSM}, "audio", "GSM"},
654 {{1, AST_FORMAT_ULAW}, "audio", "PCMU"},
655 {{1, AST_FORMAT_ALAW}, "audio", "PCMA"},
656 {{1, AST_FORMAT_G726}, "audio", "G726-32"},
657 {{1, AST_FORMAT_ADPCM}, "audio", "DVI4"},
658 {{1, AST_FORMAT_SLINEAR}, "audio", "L16"},
659 {{1, AST_FORMAT_LPC10}, "audio", "LPC"},
660 {{1, AST_FORMAT_G729A}, "audio", "G729"},
661 {{1, AST_FORMAT_SPEEX}, "audio", "speex"},
662 {{1, AST_FORMAT_ILBC}, "audio", "iLBC"},
663 {{0, AST_RTP_DTMF}, "audio", "telephone-event"},
664 {{0, AST_RTP_CISCO_DTMF}, "audio", "cisco-telephone-event"},
665 {{0, AST_RTP_CN}, "audio", "CN"},
666 {{1, AST_FORMAT_JPEG}, "video", "JPEG"},
667 {{1, AST_FORMAT_PNG}, "video", "PNG"},
668 {{1, AST_FORMAT_H261}, "video", "H261"},
669 {{1, AST_FORMAT_H263}, "video", "H263"},
672 /* Static (i.e., well-known) RTP payload types for our "AST_FORMAT..."s:
673 also, our own choices for dynamic payload types. This is our master
674 table for transmission */
675 static struct rtpPayloadType static_RTP_PT[MAX_RTP_PT] = {
676 [0] = {1, AST_FORMAT_ULAW},
677 [2] = {1, AST_FORMAT_G726}, /* Technically this is G.721, but if Cisco can do it, so can we... */
678 [3] = {1, AST_FORMAT_GSM},
679 [4] = {1, AST_FORMAT_G723_1},
680 [5] = {1, AST_FORMAT_ADPCM}, /* 8 kHz */
681 [6] = {1, AST_FORMAT_ADPCM}, /* 16 kHz */
682 [7] = {1, AST_FORMAT_LPC10},
683 [8] = {1, AST_FORMAT_ALAW},
684 [10] = {1, AST_FORMAT_SLINEAR}, /* 2 channels */
685 [11] = {1, AST_FORMAT_SLINEAR}, /* 1 channel */
686 [13] = {0, AST_RTP_CN},
687 [16] = {1, AST_FORMAT_ADPCM}, /* 11.025 kHz */
688 [17] = {1, AST_FORMAT_ADPCM}, /* 22.050 kHz */
689 [18] = {1, AST_FORMAT_G729A},
690 [19] = {0, AST_RTP_CN}, /* Also used for CN */
691 [26] = {1, AST_FORMAT_JPEG},
692 [31] = {1, AST_FORMAT_H261},
693 [34] = {1, AST_FORMAT_H263},
694 [97] = {1, AST_FORMAT_ILBC},
695 [101] = {0, AST_RTP_DTMF},
696 [110] = {1, AST_FORMAT_SPEEX},
697 [121] = {0, AST_RTP_CISCO_DTMF}, /* Must be type 121 */
700 void ast_rtp_pt_clear(struct ast_rtp* rtp)
704 for (i = 0; i < MAX_RTP_PT; ++i) {
705 rtp->current_RTP_PT[i].isAstFormat = 0;
706 rtp->current_RTP_PT[i].code = 0;
709 rtp->rtp_lookup_code_cache_isAstFormat = 0;
710 rtp->rtp_lookup_code_cache_code = 0;
711 rtp->rtp_lookup_code_cache_result = 0;
714 void ast_rtp_pt_default(struct ast_rtp* rtp)
718 /* Initialize to default payload types */
719 for (i = 0; i < MAX_RTP_PT; ++i) {
720 rtp->current_RTP_PT[i].isAstFormat = static_RTP_PT[i].isAstFormat;
721 rtp->current_RTP_PT[i].code = static_RTP_PT[i].code;
724 rtp->rtp_lookup_code_cache_isAstFormat = 0;
725 rtp->rtp_lookup_code_cache_code = 0;
726 rtp->rtp_lookup_code_cache_result = 0;
729 /* Make a note of a RTP payload type that was seen in a SDP "m=" line. */
730 /* By default, use the well-known value for this type (although it may */
731 /* still be set to a different value by a subsequent "a=rtpmap:" line): */
732 void ast_rtp_set_m_type(struct ast_rtp* rtp, int pt) {
733 if (pt < 0 || pt > MAX_RTP_PT)
734 return; /* bogus payload type */
736 if (static_RTP_PT[pt].code != 0) {
737 rtp->current_RTP_PT[pt] = static_RTP_PT[pt];
741 /* Make a note of a RTP payload type (with MIME type) that was seen in */
742 /* a SDP "a=rtpmap:" line. */
743 void ast_rtp_set_rtpmap_type(struct ast_rtp* rtp, int pt,
744 char* mimeType, char* mimeSubtype) {
747 if (pt < 0 || pt > MAX_RTP_PT)
748 return; /* bogus payload type */
750 for (i = 0; i < sizeof mimeTypes/sizeof mimeTypes[0]; ++i) {
751 if (strcasecmp(mimeSubtype, mimeTypes[i].subtype) == 0 &&
752 strcasecmp(mimeType, mimeTypes[i].type) == 0) {
753 rtp->current_RTP_PT[pt] = mimeTypes[i].payloadType;
759 /* Return the union of all of the codecs that were set by rtp_set...() calls */
760 /* They're returned as two distinct sets: AST_FORMATs, and AST_RTPs */
761 void ast_rtp_get_current_formats(struct ast_rtp* rtp,
762 int* astFormats, int* nonAstFormats) {
765 *astFormats = *nonAstFormats = 0;
766 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
767 if (rtp->current_RTP_PT[pt].isAstFormat) {
768 *astFormats |= rtp->current_RTP_PT[pt].code;
770 *nonAstFormats |= rtp->current_RTP_PT[pt].code;
775 void ast_rtp_offered_from_local(struct ast_rtp* rtp, int local) {
777 rtp->rtp_offered_from_local = local;
779 ast_log(LOG_WARNING, "rtp structure is null\n");
782 struct rtpPayloadType ast_rtp_lookup_pt(struct ast_rtp* rtp, int pt)
784 struct rtpPayloadType result;
786 result.isAstFormat = result.code = 0;
787 if (pt < 0 || pt > MAX_RTP_PT)
788 return result; /* bogus payload type */
790 /* Start with the negotiated codecs */
791 if (!rtp->rtp_offered_from_local)
792 result = rtp->current_RTP_PT[pt];
794 /* If it doesn't exist, check our static RTP type list, just in case */
796 result = static_RTP_PT[pt];
800 /* Looks up an RTP code out of our *static* outbound list */
801 int ast_rtp_lookup_code(struct ast_rtp* rtp, int isAstFormat, int code) {
805 if (isAstFormat == rtp->rtp_lookup_code_cache_isAstFormat &&
806 code == rtp->rtp_lookup_code_cache_code) {
808 /* Use our cached mapping, to avoid the overhead of the loop below */
809 return rtp->rtp_lookup_code_cache_result;
812 /* Check the dynamic list first */
813 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
814 if (rtp->current_RTP_PT[pt].code == code && rtp->current_RTP_PT[pt].isAstFormat == isAstFormat) {
815 rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
816 rtp->rtp_lookup_code_cache_code = code;
817 rtp->rtp_lookup_code_cache_result = pt;
822 /* Then the static list */
823 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
824 if (static_RTP_PT[pt].code == code && static_RTP_PT[pt].isAstFormat == isAstFormat) {
825 rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
826 rtp->rtp_lookup_code_cache_code = code;
827 rtp->rtp_lookup_code_cache_result = pt;
834 char* ast_rtp_lookup_mime_subtype(int isAstFormat, int code) {
838 for (i = 0; i < sizeof mimeTypes/sizeof mimeTypes[0]; ++i) {
839 if (mimeTypes[i].payloadType.code == code && mimeTypes[i].payloadType.isAstFormat == isAstFormat) {
840 return mimeTypes[i].subtype;
846 static int rtp_socket(void)
850 s = socket(AF_INET, SOCK_DGRAM, 0);
852 flags = fcntl(s, F_GETFL);
853 fcntl(s, F_SETFL, flags | O_NONBLOCK);
856 setsockopt(s, SOL_SOCKET, SO_NO_CHECK, &checksums, sizeof(checksums));
863 static struct ast_rtcp *ast_rtcp_new(void)
865 struct ast_rtcp *rtcp;
866 rtcp = malloc(sizeof(struct ast_rtcp));
869 memset(rtcp, 0, sizeof(struct ast_rtcp));
870 rtcp->s = rtp_socket();
871 rtcp->us.sin_family = AF_INET;
874 ast_log(LOG_WARNING, "Unable to allocate socket: %s\n", strerror(errno));
880 struct ast_rtp *ast_rtp_new_with_bindaddr(struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode, struct in_addr addr)
886 rtp = malloc(sizeof(struct ast_rtp));
889 memset(rtp, 0, sizeof(struct ast_rtp));
890 rtp->them.sin_family = AF_INET;
891 rtp->us.sin_family = AF_INET;
892 rtp->s = rtp_socket();
894 rtp->seqno = rand() & 0xffff;
897 ast_log(LOG_ERROR, "Unable to allocate socket: %s\n", strerror(errno));
900 if (sched && rtcpenable) {
902 rtp->rtcp = ast_rtcp_new();
904 /* Find us a place */
905 x = (rand() % (rtpend-rtpstart)) + rtpstart;
909 /* Must be an even port number by RTP spec */
910 rtp->us.sin_port = htons(x);
911 rtp->us.sin_addr = addr;
913 rtp->rtcp->us.sin_port = htons(x + 1);
914 if (!(first = bind(rtp->s, (struct sockaddr *)&rtp->us, sizeof(rtp->us))) &&
915 (!rtp->rtcp || !bind(rtp->rtcp->s, (struct sockaddr *)&rtp->rtcp->us, sizeof(rtp->rtcp->us))))
918 /* Primary bind succeeded! Gotta recreate it */
920 rtp->s = rtp_socket();
922 if (errno != EADDRINUSE) {
923 ast_log(LOG_ERROR, "Unexpected bind error: %s\n", strerror(errno));
934 x = (rtpstart + 1) & ~1;
935 if (x == startplace) {
936 ast_log(LOG_ERROR, "No RTP ports remaining\n");
946 if (io && sched && callbackmode) {
947 /* Operate this one in a callback mode */
950 rtp->ioid = ast_io_add(rtp->io, rtp->s, rtpread, AST_IO_IN, rtp);
952 ast_rtp_pt_default(rtp);
956 struct ast_rtp *ast_rtp_new(struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode)
960 memset(&ia, 0, sizeof(ia));
961 return ast_rtp_new_with_bindaddr(sched, io, rtcpenable, callbackmode, ia);
964 int ast_rtp_settos(struct ast_rtp *rtp, int tos)
968 if ((res = setsockopt(rtp->s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))))
969 ast_log(LOG_WARNING, "Unable to set TOS to %d\n", tos);
973 void ast_rtp_set_peer(struct ast_rtp *rtp, struct sockaddr_in *them)
975 rtp->them.sin_port = them->sin_port;
976 rtp->them.sin_addr = them->sin_addr;
978 rtp->rtcp->them.sin_port = htons(ntohs(them->sin_port) + 1);
979 rtp->rtcp->them.sin_addr = them->sin_addr;
984 void ast_rtp_get_peer(struct ast_rtp *rtp, struct sockaddr_in *them)
986 them->sin_family = AF_INET;
987 them->sin_port = rtp->them.sin_port;
988 them->sin_addr = rtp->them.sin_addr;
991 void ast_rtp_get_us(struct ast_rtp *rtp, struct sockaddr_in *us)
993 memcpy(us, &rtp->us, sizeof(rtp->us));
996 void ast_rtp_stop(struct ast_rtp *rtp)
998 memset(&rtp->them.sin_addr, 0, sizeof(rtp->them.sin_addr));
999 memset(&rtp->them.sin_port, 0, sizeof(rtp->them.sin_port));
1001 memset(&rtp->rtcp->them.sin_addr, 0, sizeof(rtp->them.sin_addr));
1002 memset(&rtp->rtcp->them.sin_port, 0, sizeof(rtp->them.sin_port));
1006 void ast_rtp_reset(struct ast_rtp *rtp)
1008 memset(&rtp->rxcore, 0, sizeof(rtp->rxcore));
1009 memset(&rtp->txcore, 0, sizeof(rtp->txcore));
1010 memset(&rtp->dtmfmute, 0, sizeof(rtp->dtmfmute));
1013 rtp->lastividtimestamp = 0;
1014 rtp->lastovidtimestamp = 0;
1015 rtp->lasteventseqn = 0;
1016 rtp->lasttxformat = 0;
1017 rtp->lastrxformat = 0;
1019 rtp->dtmfduration = 0;
1024 void ast_rtp_destroy(struct ast_rtp *rtp)
1027 ast_smoother_free(rtp->smoother);
1029 ast_io_remove(rtp->io, rtp->ioid);
1033 close(rtp->rtcp->s);
1039 static unsigned int calc_txstamp(struct ast_rtp *rtp, struct timeval *delivery)
1043 if (!rtp->txcore.tv_sec && !rtp->txcore.tv_usec) {
1044 gettimeofday(&rtp->txcore, NULL);
1045 /* Round to 20ms for nice, pretty timestamps */
1046 rtp->txcore.tv_usec -= rtp->txcore.tv_usec % 20000;
1048 if (delivery && (delivery->tv_sec || delivery->tv_usec)) {
1049 /* Use previous txcore */
1050 ms = (delivery->tv_sec - rtp->txcore.tv_sec) * 1000;
1051 ms += (1000000 + delivery->tv_usec - rtp->txcore.tv_usec) / 1000 - 1000;
1052 rtp->txcore.tv_sec = delivery->tv_sec;
1053 rtp->txcore.tv_usec = delivery->tv_usec;
1055 gettimeofday(&now, NULL);
1056 ms = (now.tv_sec - rtp->txcore.tv_sec) * 1000;
1057 ms += (1000000 + now.tv_usec - rtp->txcore.tv_usec) / 1000 - 1000;
1058 /* Use what we just got for next time */
1059 rtp->txcore.tv_sec = now.tv_sec;
1060 rtp->txcore.tv_usec = now.tv_usec;
1065 int ast_rtp_senddigit(struct ast_rtp *rtp, char digit)
1067 unsigned int *rtpheader;
1073 char iabuf[INET_ADDRSTRLEN];
1075 if ((digit <= '9') && (digit >= '0'))
1077 else if (digit == '*')
1079 else if (digit == '#')
1081 else if ((digit >= 'A') && (digit <= 'D'))
1082 digit = digit - 'A' + 12;
1083 else if ((digit >= 'a') && (digit <= 'd'))
1084 digit = digit - 'a' + 12;
1086 ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
1089 payload = ast_rtp_lookup_code(rtp, 0, AST_RTP_DTMF);
1091 /* If we have no peer, return immediately */
1092 if (!rtp->them.sin_addr.s_addr)
1095 gettimeofday(&rtp->dtmfmute, NULL);
1096 rtp->dtmfmute.tv_usec += (500 * 1000);
1097 if (rtp->dtmfmute.tv_usec > 1000000) {
1098 rtp->dtmfmute.tv_usec -= 1000000;
1099 rtp->dtmfmute.tv_sec += 1;
1102 /* Get a pointer to the header */
1103 rtpheader = (unsigned int *)data;
1104 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno++));
1105 rtpheader[1] = htonl(rtp->lastts);
1106 rtpheader[2] = htonl(rtp->ssrc);
1107 rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (0));
1109 if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
1110 res = sendto(rtp->s, (void *)rtpheader, hdrlen + 4, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
1112 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));
1113 if(rtp_debug_test_addr(&rtp->them))
1114 ast_verbose("Sent RTP packet to %s:%d (type %d, seq %d, ts %d, len %d)\n"
1115 , ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr), ntohs(rtp->them.sin_port), payload, rtp->seqno, rtp->lastts,res - hdrlen);
1119 /* Clear marker bit and increment seqno */
1120 rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno++));
1121 /* Make duration 800 (100ms) */
1122 rtpheader[3] |= htonl((800));
1123 /* Set the End bit for the last 3 */
1124 rtpheader[3] |= htonl((1 << 23));
1125 } else if ( x < 5) {
1126 rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno++));
1132 int ast_rtp_sendcng(struct ast_rtp *rtp, int level)
1134 unsigned int *rtpheader;
1139 char iabuf[INET_ADDRSTRLEN];
1140 level = 127 - (level & 0x7f);
1141 payload = ast_rtp_lookup_code(rtp, 0, AST_RTP_CN);
1143 /* If we have no peer, return immediately */
1144 if (!rtp->them.sin_addr.s_addr)
1147 gettimeofday(&rtp->dtmfmute, NULL);
1148 rtp->dtmfmute.tv_usec += (500 * 1000);
1149 if (rtp->dtmfmute.tv_usec > 1000000) {
1150 rtp->dtmfmute.tv_usec -= 1000000;
1151 rtp->dtmfmute.tv_sec += 1;
1154 /* Get a pointer to the header */
1155 rtpheader = (unsigned int *)data;
1156 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno++));
1157 rtpheader[1] = htonl(rtp->lastts);
1158 rtpheader[2] = htonl(rtp->ssrc);
1160 if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
1161 res = sendto(rtp->s, (void *)rtpheader, hdrlen + 1, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
1163 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));
1164 if(rtp_debug_test_addr(&rtp->them))
1165 ast_verbose("Sent Comfort Noise RTP packet to %s:%d (type %d, seq %d, ts %d, len %d)\n"
1166 , ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr), ntohs(rtp->them.sin_port), payload, rtp->seqno, rtp->lastts,res - hdrlen);
1173 static void put_uint32(unsigned char *buf, int i)
1175 unsigned char *c = (unsigned char *)&i;
1177 buf[0] = (i>>24) & 0xff;
1178 buf[1] = (i>>16) & 0xff;
1179 buf[2] = (i>>8) & 0xff;
1183 #define put_uint32(p,v) ((*((unsigned int *)(p))) = (v))
1186 static int ast_rtp_raw_write(struct ast_rtp *rtp, struct ast_frame *f, int codec)
1188 unsigned char *rtpheader;
1189 char iabuf[INET_ADDRSTRLEN];
1196 ms = calc_txstamp(rtp, &f->delivery);
1197 /* Default prediction */
1198 if (f->subclass < AST_FORMAT_MAX_AUDIO) {
1199 pred = rtp->lastts + ms * 8;
1201 switch(f->subclass) {
1202 case AST_FORMAT_ULAW:
1203 case AST_FORMAT_ALAW:
1204 /* If we're within +/- 20ms from when where we
1205 predict we should be, use that */
1206 pred = rtp->lastts + f->datalen;
1208 case AST_FORMAT_ADPCM:
1209 case AST_FORMAT_G726:
1210 /* If we're within +/- 20ms from when where we
1211 predict we should be, use that */
1212 pred = rtp->lastts + f->datalen * 2;
1214 case AST_FORMAT_G729A:
1215 pred = rtp->lastts + f->datalen * 8;
1217 case AST_FORMAT_GSM:
1218 pred = rtp->lastts + (f->datalen * 160 / 33);
1220 case AST_FORMAT_ILBC:
1221 pred = rtp->lastts + (f->datalen * 240 / 50);
1223 case AST_FORMAT_G723_1:
1224 pred = rtp->lastts + g723_samples(f->data, f->datalen);
1226 case AST_FORMAT_SPEEX:
1227 pred = rtp->lastts + 160;
1228 /* assumes that the RTP packet contains one Speex frame */
1230 case AST_FORMAT_LPC10:
1231 /* assumes that the RTP packet contains one LPC10 frame */
1232 pred = rtp->lastts + 22 * 8;
1233 pred += (((char *)(f->data))[7] & 0x1) * 8;
1236 ast_log(LOG_WARNING, "Not sure about timestamp format for codec format %s\n", ast_getformatname(f->subclass));
1238 /* Re-calculate last TS */
1239 rtp->lastts = rtp->lastts + ms * 8;
1240 if (!f->delivery.tv_sec && !f->delivery.tv_usec) {
1241 /* If this isn't an absolute delivery time, Check if it is close to our prediction,
1242 and if so, go with our prediction */
1243 if (abs(rtp->lastts - pred) < MAX_TIMESTAMP_SKEW)
1246 if (option_debug > 2)
1247 ast_log(LOG_DEBUG, "Difference is %d, ms is %d\n", abs(rtp->lastts - pred), ms);
1252 mark = f->subclass & 0x1;
1253 pred = rtp->lastovidtimestamp + f->samples;
1254 /* Re-calculate last TS */
1255 rtp->lastts = rtp->lastts + ms * 90;
1256 /* If it's close to our prediction, go for it */
1257 if (!f->delivery.tv_sec && !f->delivery.tv_usec) {
1258 if (abs(rtp->lastts - pred) < 7200) {
1260 rtp->lastovidtimestamp += f->samples;
1262 if (option_debug > 2)
1263 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);
1264 rtp->lastovidtimestamp = rtp->lastts;
1268 /* Get a pointer to the header */
1269 rtpheader = (unsigned char *)(f->data - hdrlen);
1271 put_uint32(rtpheader, htonl((2 << 30) | (codec << 16) | (rtp->seqno) | (mark << 23)));
1272 put_uint32(rtpheader + 4, htonl(rtp->lastts));
1273 put_uint32(rtpheader + 8, htonl(rtp->ssrc));
1277 if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
1278 res = sendto(rtp->s, (void *)rtpheader, f->datalen + hdrlen, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
1280 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));
1281 if(rtp_debug_test_addr(&rtp->them))
1282 ast_verbose("Sent RTP packet to %s:%d (type %d, seq %d, ts %d, len %d)\n"
1283 , ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr), ntohs(rtp->them.sin_port), codec, rtp->seqno, rtp->lastts,res - hdrlen);
1288 int ast_rtp_write(struct ast_rtp *rtp, struct ast_frame *_f)
1290 struct ast_frame *f;
1296 /* If we have no peer, return immediately */
1297 if (!rtp->them.sin_addr.s_addr)
1300 /* If there is no data length, return immediately */
1304 /* Make sure we have enough space for RTP header */
1305 if ((_f->frametype != AST_FRAME_VOICE) && (_f->frametype != AST_FRAME_VIDEO)) {
1306 ast_log(LOG_WARNING, "RTP can only send voice\n");
1310 subclass = _f->subclass;
1311 if (_f->frametype == AST_FRAME_VIDEO)
1314 codec = ast_rtp_lookup_code(rtp, 1, subclass);
1316 ast_log(LOG_WARNING, "Don't know how to send format %s packets with RTP\n", ast_getformatname(_f->subclass));
1320 if (rtp->lasttxformat != subclass) {
1321 /* New format, reset the smoother */
1323 ast_log(LOG_DEBUG, "Ooh, format changed from %s to %s\n", ast_getformatname(rtp->lasttxformat), ast_getformatname(subclass));
1324 rtp->lasttxformat = subclass;
1326 ast_smoother_free(rtp->smoother);
1327 rtp->smoother = NULL;
1332 case AST_FORMAT_ULAW:
1333 case AST_FORMAT_ALAW:
1334 if (!rtp->smoother) {
1335 rtp->smoother = ast_smoother_new(160);
1337 if (!rtp->smoother) {
1338 ast_log(LOG_WARNING, "Unable to create smoother :(\n");
1341 ast_smoother_feed(rtp->smoother, _f);
1343 while((f = ast_smoother_read(rtp->smoother)))
1344 ast_rtp_raw_write(rtp, f, codec);
1346 case AST_FORMAT_ADPCM:
1347 case AST_FORMAT_G726:
1348 if (!rtp->smoother) {
1349 rtp->smoother = ast_smoother_new(80);
1351 if (!rtp->smoother) {
1352 ast_log(LOG_WARNING, "Unable to create smoother :(\n");
1355 ast_smoother_feed(rtp->smoother, _f);
1357 while((f = ast_smoother_read(rtp->smoother)))
1358 ast_rtp_raw_write(rtp, f, codec);
1360 case AST_FORMAT_G729A:
1361 if (!rtp->smoother) {
1362 rtp->smoother = ast_smoother_new(20);
1364 ast_smoother_set_flags(rtp->smoother, AST_SMOOTHER_FLAG_G729);
1366 if (!rtp->smoother) {
1367 ast_log(LOG_WARNING, "Unable to create g729 smoother :(\n");
1370 ast_smoother_feed(rtp->smoother, _f);
1372 while((f = ast_smoother_read(rtp->smoother)))
1373 ast_rtp_raw_write(rtp, f, codec);
1375 case AST_FORMAT_GSM:
1376 if (!rtp->smoother) {
1377 rtp->smoother = ast_smoother_new(33);
1379 if (!rtp->smoother) {
1380 ast_log(LOG_WARNING, "Unable to create GSM smoother :(\n");
1383 ast_smoother_feed(rtp->smoother, _f);
1384 while((f = ast_smoother_read(rtp->smoother)))
1385 ast_rtp_raw_write(rtp, f, codec);
1387 case AST_FORMAT_ILBC:
1388 if (!rtp->smoother) {
1389 rtp->smoother = ast_smoother_new(50);
1391 if (!rtp->smoother) {
1392 ast_log(LOG_WARNING, "Unable to create ILBC smoother :(\n");
1395 ast_smoother_feed(rtp->smoother, _f);
1396 while((f = ast_smoother_read(rtp->smoother)))
1397 ast_rtp_raw_write(rtp, f, codec);
1400 ast_log(LOG_WARNING, "Not sure about sending format %s packets\n", ast_getformatname(subclass));
1401 /* fall through to... */
1402 case AST_FORMAT_H261:
1403 case AST_FORMAT_H263:
1404 case AST_FORMAT_G723_1:
1405 case AST_FORMAT_LPC10:
1406 case AST_FORMAT_SPEEX:
1407 /* Don't buffer outgoing frames; send them one-per-packet: */
1408 if (_f->offset < hdrlen) {
1413 ast_rtp_raw_write(rtp, f, codec);
1419 void ast_rtp_proto_unregister(struct ast_rtp_protocol *proto)
1421 struct ast_rtp_protocol *cur, *prev;
1428 prev->next = proto->next;
1430 protos = proto->next;
1438 int ast_rtp_proto_register(struct ast_rtp_protocol *proto)
1440 struct ast_rtp_protocol *cur;
1443 if (cur->type == proto->type) {
1444 ast_log(LOG_WARNING, "Tried to register same protocol '%s' twice\n", cur->type);
1449 proto->next = protos;
1454 static struct ast_rtp_protocol *get_proto(struct ast_channel *chan)
1456 struct ast_rtp_protocol *cur;
1459 if (cur->type == chan->type) {
1467 int ast_rtp_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
1469 struct ast_frame *f;
1470 struct ast_channel *who, *cs[3];
1471 struct ast_rtp *p0, *p1;
1472 struct ast_rtp *vp0, *vp1;
1473 struct ast_rtp_protocol *pr0, *pr1;
1474 struct sockaddr_in ac0, ac1;
1475 struct sockaddr_in vac0, vac1;
1476 struct sockaddr_in t0, t1;
1477 struct sockaddr_in vt0, vt1;
1478 char iabuf[INET_ADDRSTRLEN];
1482 int codec0,codec1, oldcodec0, oldcodec1;
1484 memset(&vt0, 0, sizeof(vt0));
1485 memset(&vt1, 0, sizeof(vt1));
1486 memset(&vac0, 0, sizeof(vac0));
1487 memset(&vac1, 0, sizeof(vac1));
1489 /* if need DTMF, cant native bridge */
1490 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
1492 ast_mutex_lock(&c0->lock);
1493 while(ast_mutex_trylock(&c1->lock)) {
1494 ast_mutex_unlock(&c0->lock);
1496 ast_mutex_lock(&c0->lock);
1498 pr0 = get_proto(c0);
1499 pr1 = get_proto(c1);
1501 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c0->name);
1502 ast_mutex_unlock(&c0->lock);
1503 ast_mutex_unlock(&c1->lock);
1507 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c1->name);
1508 ast_mutex_unlock(&c0->lock);
1509 ast_mutex_unlock(&c1->lock);
1512 pvt0 = c0->pvt->pvt;
1513 pvt1 = c1->pvt->pvt;
1514 p0 = pr0->get_rtp_info(c0);
1515 if (pr0->get_vrtp_info)
1516 vp0 = pr0->get_vrtp_info(c0);
1519 p1 = pr1->get_rtp_info(c1);
1520 if (pr1->get_vrtp_info)
1521 vp1 = pr1->get_vrtp_info(c1);
1525 /* Somebody doesn't want to play... */
1526 ast_mutex_unlock(&c0->lock);
1527 ast_mutex_unlock(&c1->lock);
1531 codec0 = pr0->get_codec(c0);
1535 codec1 = pr1->get_codec(c1);
1538 if (pr0->get_codec && pr1->get_codec) {
1539 /* Hey, we can't do reinvite if both parties speak diffrent codecs */
1540 if (!(codec0 & codec1)) {
1541 ast_log(LOG_WARNING, "codec0 = %d is not codec1 = %d, cannot native bridge.\n",codec0,codec1);
1542 ast_mutex_unlock(&c0->lock);
1543 ast_mutex_unlock(&c1->lock);
1547 if (pr0->set_rtp_peer(c0, p1, vp1, codec1))
1548 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
1550 /* Store RTP peer */
1551 ast_rtp_get_peer(p1, &ac1);
1553 ast_rtp_get_peer(vp1, &vac1);
1555 if (pr1->set_rtp_peer(c1, p0, vp0, codec0))
1556 ast_log(LOG_WARNING, "Channel '%s' failed to talk back to '%s'\n", c1->name, c0->name);
1558 /* Store RTP peer */
1559 ast_rtp_get_peer(p0, &ac0);
1561 ast_rtp_get_peer(vp0, &vac0);
1563 ast_mutex_unlock(&c0->lock);
1564 ast_mutex_unlock(&c1->lock);
1571 if ((c0->pvt->pvt != pvt0) ||
1572 (c1->pvt->pvt != pvt1) ||
1573 (c0->masq || c0->masqr || c1->masq || c1->masqr)) {
1574 ast_log(LOG_DEBUG, "Oooh, something is weird, backing out\n");
1575 if (c0->pvt->pvt == pvt0) {
1576 if (pr0->set_rtp_peer(c0, NULL, NULL, 0))
1577 ast_log(LOG_WARNING, "Channel '%s' failed to revert\n", c0->name);
1579 if (c1->pvt->pvt == pvt1) {
1580 if (pr1->set_rtp_peer(c1, NULL, NULL, 0))
1581 ast_log(LOG_WARNING, "Channel '%s' failed to revert back\n", c1->name);
1583 /* Tell it to try again later */
1587 ast_rtp_get_peer(p1, &t1);
1588 ast_rtp_get_peer(p0, &t0);
1590 codec0 = pr0->get_codec(c0);
1592 codec1 = pr1->get_codec(c1);
1594 ast_rtp_get_peer(vp1, &vt1);
1596 ast_rtp_get_peer(vp0, &vt0);
1597 if (inaddrcmp(&t1, &ac1) || (vp1 && inaddrcmp(&vt1, &vac1)) || (codec1 != oldcodec1)) {
1599 ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
1600 c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), t1.sin_addr), ntohs(t1.sin_port), codec1);
1601 ast_log(LOG_DEBUG, "Oooh, '%s' changed end vaddress to %s:%d (format %d)\n",
1602 c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), vt1.sin_addr), ntohs(vt1.sin_port), codec1);
1603 ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d/(format %d)\n",
1604 c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), ac1.sin_addr), ntohs(ac1.sin_port), oldcodec1);
1605 ast_log(LOG_DEBUG, "Oooh, '%s' wasv %s:%d/(format %d)\n",
1606 c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), vac1.sin_addr), ntohs(vac1.sin_port), oldcodec1);
1608 if (pr0->set_rtp_peer(c0, t1.sin_addr.s_addr ? p1 : NULL, vt1.sin_addr.s_addr ? vp1 : NULL, codec1))
1609 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c0->name, c1->name);
1610 memcpy(&ac1, &t1, sizeof(ac1));
1611 memcpy(&vac1, &vt1, sizeof(vac1));
1614 if (inaddrcmp(&t0, &ac0) || (vp0 && inaddrcmp(&vt0, &vac0))) {
1616 ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
1617 c0->name, ast_inet_ntoa(iabuf, sizeof(iabuf), t0.sin_addr), ntohs(t0.sin_port), codec0);
1618 ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d/(format %d)\n",
1619 c0->name, ast_inet_ntoa(iabuf, sizeof(iabuf), ac0.sin_addr), ntohs(ac0.sin_port), oldcodec0);
1621 if (pr1->set_rtp_peer(c1, t0.sin_addr.s_addr ? p0 : NULL, vt0.sin_addr.s_addr ? vp0 : NULL, codec0))
1622 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c1->name, c0->name);
1623 memcpy(&ac0, &t0, sizeof(ac0));
1624 memcpy(&vac0, &vt0, sizeof(vac0));
1627 who = ast_waitfor_n(cs, 2, &to);
1630 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
1631 /* check for hagnup / whentohangup */
1632 if (ast_check_hangup(c0) || ast_check_hangup(c1))
1637 if (!f || ((f->frametype == AST_FRAME_DTMF) &&
1638 (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
1639 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))))) {
1643 ast_log(LOG_DEBUG, "Oooh, got a %s\n", f ? "digit" : "hangup");
1644 if ((c0->pvt->pvt == pvt0) && (!c0->_softhangup)) {
1645 if (pr0->set_rtp_peer(c0, NULL, NULL, 0))
1646 ast_log(LOG_WARNING, "Channel '%s' failed to revert\n", c0->name);
1648 if ((c1->pvt->pvt == pvt1) && (!c1->_softhangup)) {
1649 if (pr1->set_rtp_peer(c1, NULL, NULL, 0))
1650 ast_log(LOG_WARNING, "Channel '%s' failed to revert back\n", c1->name);
1652 /* That's all we needed */
1655 if ((f->frametype == AST_FRAME_DTMF) ||
1656 (f->frametype == AST_FRAME_VOICE) ||
1657 (f->frametype == AST_FRAME_VIDEO)) {
1658 /* Forward voice or DTMF frames if they happen upon us */
1661 } else if (who == c1) {
1667 /* Swap priority not that it's a big deal at this point */
1676 static int rtp_do_debug_ip(int fd, int argc, char *argv[])
1679 struct ast_hostent ahp;
1680 char iabuf[INET_ADDRSTRLEN];
1685 return RESULT_SHOWUSAGE;
1687 p = strstr(arg, ":");
1693 hp = ast_gethostbyname(arg, &ahp);
1695 return RESULT_SHOWUSAGE;
1696 rtpdebugaddr.sin_family = AF_INET;
1697 memcpy(&rtpdebugaddr.sin_addr, hp->h_addr, sizeof(rtpdebugaddr.sin_addr));
1698 rtpdebugaddr.sin_port = htons(port);
1700 ast_cli(fd, "RTP Debugging Enabled for IP: %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), rtpdebugaddr.sin_addr));
1702 ast_cli(fd, "RTP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(iabuf, sizeof(iabuf), rtpdebugaddr.sin_addr), port);
1704 return RESULT_SUCCESS;
1707 static int rtp_do_debug(int fd, int argc, char *argv[])
1711 return RESULT_SHOWUSAGE;
1712 return rtp_do_debug_ip(fd, argc, argv);
1715 memset(&rtpdebugaddr,0,sizeof(rtpdebugaddr));
1716 ast_cli(fd, "RTP Debugging Enabled\n");
1717 return RESULT_SUCCESS;
1720 static int rtp_no_debug(int fd, int argc, char *argv[])
1723 return RESULT_SHOWUSAGE;
1725 ast_cli(fd,"RTP Debugging Disabled\n");
1726 return RESULT_SUCCESS;
1729 static char debug_usage[] =
1730 "Usage: rtp debug [ip host[:port]]\n"
1731 " Enable dumping of all RTP packets to and from host.\n";
1733 static char no_debug_usage[] =
1734 "Usage: rtp no debug\n"
1735 " Disable all RTP debugging\n";
1737 static struct ast_cli_entry cli_debug_ip =
1738 {{ "rtp", "debug", "ip", NULL } , rtp_do_debug, "Enable RTP debugging on IP", debug_usage };
1740 static struct ast_cli_entry cli_debug =
1741 {{ "rtp", "debug", NULL } , rtp_do_debug, "Enable RTP debugging", debug_usage };
1743 static struct ast_cli_entry cli_no_debug =
1744 {{ "rtp", "no", "debug", NULL } , rtp_no_debug, "Disable RTP debugging", no_debug_usage };
1746 void ast_rtp_reload(void)
1748 struct ast_config *cfg;
1756 cfg = ast_config_load("rtp.conf");
1758 if ((s = ast_variable_retrieve(cfg, "general", "rtpstart"))) {
1760 if (rtpstart < 1024)
1762 if (rtpstart > 65535)
1765 if ((s = ast_variable_retrieve(cfg, "general", "rtpend"))) {
1772 if ((s = ast_variable_retrieve(cfg, "general", "rtpchecksums"))) {
1780 ast_log(LOG_WARNING, "Disabling RTP checksums is not supported on this operating system!\n");
1783 ast_config_destroy(cfg);
1785 if (rtpstart >= rtpend) {
1786 ast_log(LOG_WARNING, "Unreasonable values for RTP start in rtp.conf/end\n");
1790 if (option_verbose > 1)
1791 ast_verbose(VERBOSE_PREFIX_2 "RTP Allocating from port range %d -> %d\n", rtpstart, rtpend);
1795 void ast_rtp_init(void)
1797 ast_cli_register(&cli_debug);
1798 ast_cli_register(&cli_debug_ip);
1799 ast_cli_register(&cli_no_debug);