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>
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33 #include "asterisk/rtp.h"
34 #include "asterisk/frame.h"
35 #include "asterisk/logger.h"
36 #include "asterisk/options.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/acl.h"
39 #include "asterisk/channel.h"
40 #include "asterisk/config.h"
41 #include "asterisk/lock.h"
42 #include "asterisk/utils.h"
43 #include "asterisk/cli.h"
44 #include "asterisk/unaligned.h"
46 #define MAX_TIMESTAMP_SKEW 640
50 static int dtmftimeout = 3000; /* 3000 samples */
52 static int rtpstart = 0;
53 static int rtpend = 0;
54 static int rtpdebug = 0; /* Are we debugging? */
55 static struct sockaddr_in rtpdebugaddr; /* Debug packets to/from this host */
57 static int nochecksums = 0;
60 /* The value of each payload format mapping: */
61 struct rtpPayloadType {
62 int isAstFormat; /* whether the following code is an AST_FORMAT */
66 #define MAX_RTP_PT 256
68 #define FLAG_3389_WARNING (1 << 0)
74 unsigned char rawdata[8192 + AST_FRIENDLY_OFFSET];
77 unsigned int lastdigitts;
78 unsigned int lastrxts;
79 unsigned int lastividtimestamp;
80 unsigned int lastovidtimestamp;
81 unsigned int lasteventseqn;
85 unsigned int dtmfduration;
88 struct sockaddr_in us;
89 struct sockaddr_in them;
90 struct timeval rxcore;
91 struct timeval txcore;
92 struct timeval dtmfmute;
93 struct ast_smoother *smoother;
96 unsigned short rxseqno;
97 struct sched_context *sched;
98 struct io_context *io;
100 ast_rtp_callback callback;
101 struct rtpPayloadType current_RTP_PT[MAX_RTP_PT];
102 int rtp_lookup_code_cache_isAstFormat; /* a cache for the result of rtp_lookup_code(): */
103 int rtp_lookup_code_cache_code;
104 int rtp_lookup_code_cache_result;
105 int rtp_offered_from_local;
106 struct ast_rtcp *rtcp;
111 struct sockaddr_in us;
112 struct sockaddr_in them;
115 static struct ast_rtp_protocol *protos = NULL;
117 int ast_rtp_fd(struct ast_rtp *rtp)
122 int ast_rtcp_fd(struct ast_rtp *rtp)
129 void ast_rtp_set_data(struct ast_rtp *rtp, void *data)
134 void ast_rtp_set_callback(struct ast_rtp *rtp, ast_rtp_callback callback)
136 rtp->callback = callback;
139 void ast_rtp_setnat(struct ast_rtp *rtp, int nat)
144 static struct ast_frame *send_dtmf(struct ast_rtp *rtp)
147 static struct ast_frame null_frame = { AST_FRAME_NULL, };
148 char iabuf[INET_ADDRSTRLEN];
149 gettimeofday(&tv, NULL);
150 if ((tv.tv_sec < rtp->dtmfmute.tv_sec) ||
151 ((tv.tv_sec == rtp->dtmfmute.tv_sec) && (tv.tv_usec < rtp->dtmfmute.tv_usec))) {
153 ast_log(LOG_DEBUG, "Ignore potential DTMF echo from '%s'\n", ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr));
155 rtp->dtmfduration = 0;
159 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));
160 if (rtp->resp == 'X') {
161 rtp->f.frametype = AST_FRAME_CONTROL;
162 rtp->f.subclass = AST_CONTROL_FLASH;
164 rtp->f.frametype = AST_FRAME_DTMF;
165 rtp->f.subclass = rtp->resp;
172 rtp->dtmfduration = 0;
177 static inline int rtp_debug_test_addr(struct sockaddr_in *addr)
181 if (rtpdebugaddr.sin_addr.s_addr) {
182 if (((ntohs(rtpdebugaddr.sin_port) != 0)
183 && (rtpdebugaddr.sin_port != addr->sin_port))
184 || (rtpdebugaddr.sin_addr.s_addr != addr->sin_addr.s_addr))
190 static struct ast_frame *process_cisco_dtmf(struct ast_rtp *rtp, unsigned char *data, int len)
194 struct ast_frame *f = NULL;
195 event = ntohl(*((unsigned int *)(data)));
198 printf("Cisco Digit: %08x (len = %d)\n", event, len);
202 } else if (event < 11) {
204 } else if (event < 12) {
206 } else if (event < 16) {
207 resp = 'A' + (event - 12);
208 } else if (event < 17) {
211 if (rtp->resp && (rtp->resp != resp)) {
215 rtp->dtmfcount = dtmftimeout;
219 /* process_rfc2833: Process RTP DTMF and events according to RFC 2833:
220 "RTP Payload for DTMF Digits, Telephony Tones and Telephony Signals"
222 static struct ast_frame *process_rfc2833(struct ast_rtp *rtp, unsigned char *data, int len)
225 unsigned int event_end;
226 unsigned int duration;
228 struct ast_frame *f = NULL;
229 event = ntohl(*((unsigned int *)(data)));
231 event_end = ntohl(*((unsigned int *)(data)));
234 duration = ntohl(*((unsigned int *)(data)));
237 ast_log(LOG_DEBUG, "- RTP 2833 Event: %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) { /* Event 16: Hook flash */
249 if (rtp->resp && (rtp->resp != resp)) {
251 } else if(event_end & 0x80) {
258 } else if(rtp->dtmfduration && (duration < rtp->dtmfduration)) {
261 if (!(event_end & 0x80))
263 rtp->dtmfcount = dtmftimeout;
264 rtp->dtmfduration = duration;
268 /*--- process_rfc3389: Process Comfort Noise RTP.
269 This is incomplete at the moment.
271 static struct ast_frame *process_rfc3389(struct ast_rtp *rtp, unsigned char *data, int len)
273 struct ast_frame *f = NULL;
274 /* Convert comfort noise into audio with various codecs. Unfortunately this doesn't
275 totally help us out becuase we don't have an engine to keep it going and we are not
276 guaranteed to have it every 20ms or anything */
278 ast_log(LOG_DEBUG, "- RTP 3389 Comfort noise event: Level %d (len = %d)\n", rtp->lastrxformat, len);
279 if (!(rtp->flags & FLAG_3389_WARNING)) {
280 char iabuf[INET_ADDRSTRLEN];
281 ast_log(LOG_NOTICE, "Comfort noise support incomplete in Asterisk (RFC 3389). Please turn off on client if possible. Client IP: %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr));
282 rtp->flags |= FLAG_3389_WARNING;
284 /* Must have at least one byte */
288 rtp->f.data = rtp->rawdata + AST_FRIENDLY_OFFSET;
289 rtp->f.datalen = len - 1;
290 rtp->f.offset = AST_FRIENDLY_OFFSET;
291 memcpy(rtp->f.data, data + 1, len - 1);
297 rtp->f.frametype = AST_FRAME_CNG;
298 rtp->f.subclass = data[0] & 0x7f;
299 rtp->f.datalen = len - 1;
301 rtp->f.delivery.tv_usec = rtp->f.delivery.tv_sec = 0;
306 static int rtpread(int *id, int fd, short events, void *cbdata)
308 struct ast_rtp *rtp = cbdata;
310 f = ast_rtp_read(rtp);
313 rtp->callback(rtp, f, rtp->data);
318 struct ast_frame *ast_rtcp_read(struct ast_rtp *rtp)
320 static struct ast_frame null_frame = { AST_FRAME_NULL, };
324 struct sockaddr_in sin;
325 unsigned int rtcpdata[1024];
326 char iabuf[INET_ADDRSTRLEN];
328 if (!rtp || !rtp->rtcp)
333 res = recvfrom(rtp->rtcp->s, rtcpdata, sizeof(rtcpdata),
334 0, (struct sockaddr *)&sin, &len);
338 ast_log(LOG_WARNING, "RTP Read error: %s\n", strerror(errno));
345 ast_log(LOG_WARNING, "RTP Read too short\n");
350 /* Send to whoever sent to us */
351 if ((rtp->rtcp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
352 (rtp->rtcp->them.sin_port != sin.sin_port)) {
353 memcpy(&rtp->them, &sin, sizeof(rtp->them));
356 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));
360 ast_log(LOG_DEBUG, "Got RTCP report of %d bytes\n", res);
364 static void calc_rxstamp(struct timeval *tv, struct ast_rtp *rtp, unsigned int timestamp, int mark)
366 if ((!rtp->rxcore.tv_sec && !rtp->rxcore.tv_usec) || mark) {
367 gettimeofday(&rtp->rxcore, NULL);
368 rtp->rxcore.tv_sec -= timestamp / 8000;
369 rtp->rxcore.tv_usec -= (timestamp % 8000) * 125;
370 /* Round to 20ms for nice, pretty timestamps */
371 rtp->rxcore.tv_usec -= rtp->rxcore.tv_usec % 20000;
372 if (rtp->rxcore.tv_usec < 0) {
373 /* Adjust appropriately if necessary */
374 rtp->rxcore.tv_usec += 1000000;
375 rtp->rxcore.tv_sec -= 1;
378 tv->tv_sec = rtp->rxcore.tv_sec + timestamp / 8000;
379 tv->tv_usec = rtp->rxcore.tv_usec + (timestamp % 8000) * 125;
380 if (tv->tv_usec >= 1000000) {
381 tv->tv_usec -= 1000000;
386 struct ast_frame *ast_rtp_read(struct ast_rtp *rtp)
389 struct sockaddr_in sin;
399 char iabuf[INET_ADDRSTRLEN];
400 unsigned int timestamp;
401 unsigned int *rtpheader;
402 static struct ast_frame *f, null_frame = { AST_FRAME_NULL, };
403 struct rtpPayloadType rtpPT;
407 /* Cache where the header will go */
408 res = recvfrom(rtp->s, rtp->rawdata + AST_FRIENDLY_OFFSET, sizeof(rtp->rawdata) - AST_FRIENDLY_OFFSET,
409 0, (struct sockaddr *)&sin, &len);
412 rtpheader = (unsigned int *)(rtp->rawdata + AST_FRIENDLY_OFFSET);
415 ast_log(LOG_WARNING, "RTP Read error: %s\n", strerror(errno));
421 ast_log(LOG_WARNING, "RTP Read too short\n");
425 /* Ignore if the other side hasn't been given an address
427 if (!rtp->them.sin_addr.s_addr || !rtp->them.sin_port)
431 /* Send to whoever sent to us */
432 if ((rtp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
433 (rtp->them.sin_port != sin.sin_port)) {
434 memcpy(&rtp->them, &sin, sizeof(rtp->them));
436 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));
441 seqno = ntohl(rtpheader[0]);
443 /* Check RTP version */
444 version = (seqno & 0xC0000000) >> 30;
448 payloadtype = (seqno & 0x7f0000) >> 16;
449 padding = seqno & (1 << 29);
450 mark = seqno & (1 << 23);
451 ext = seqno & (1 << 28);
453 timestamp = ntohl(rtpheader[1]);
456 /* Remove padding bytes */
457 res -= rtp->rawdata[AST_FRIENDLY_OFFSET + res - 1];
461 /* RTP Extension present */
463 hdrlen += (ntohl(rtpheader[3]) & 0xffff) << 2;
467 ast_log(LOG_WARNING, "RTP Read too short (%d, expecting %d)\n", res, hdrlen);
471 if(rtp_debug_test_addr(&sin))
472 ast_verbose("Got RTP packet from %s:%d (type %d, seq %d, ts %d, len %d)\n"
473 , ast_inet_ntoa(iabuf, sizeof(iabuf), sin.sin_addr), ntohs(sin.sin_port), payloadtype, seqno, timestamp,res - hdrlen);
475 rtpPT = ast_rtp_lookup_pt(rtp, payloadtype);
476 if (!rtpPT.isAstFormat) {
477 /* This is special in-band data that's not one of our codecs */
478 if (rtpPT.code == AST_RTP_DTMF) {
479 /* It's special -- rfc2833 process it */
480 if (rtp->lasteventseqn <= seqno || rtp->resp == 0 || (rtp->lasteventseqn >= 65530 && seqno <= 6)) {
481 f = process_rfc2833(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
482 rtp->lasteventseqn = seqno;
489 } else if (rtpPT.code == AST_RTP_CISCO_DTMF) {
490 /* It's really special -- process it the Cisco way */
491 if (rtp->lasteventseqn <= seqno || rtp->resp == 0 || (rtp->lasteventseqn >= 65530 && seqno <= 6)) {
492 f = process_cisco_dtmf(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
493 rtp->lasteventseqn = seqno;
500 } else if (rtpPT.code == AST_RTP_CN) {
502 f = process_rfc3389(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
508 ast_log(LOG_NOTICE, "Unknown RTP codec %d received\n", payloadtype);
512 rtp->f.subclass = rtpPT.code;
513 if (rtp->f.subclass < AST_FORMAT_MAX_AUDIO)
514 rtp->f.frametype = AST_FRAME_VOICE;
516 rtp->f.frametype = AST_FRAME_VIDEO;
517 rtp->lastrxformat = rtp->f.subclass;
520 rtp->lastrxts = timestamp;
523 for (x=rtp->rxseqno + 1; x < seqno; x++) {
524 /* Queue empty frames */
530 rtp->f.src = "RTPMissedFrame";
533 rtp->rxseqno = seqno;
535 if (rtp->dtmfcount) {
537 printf("dtmfcount was %d\n", rtp->dtmfcount);
539 rtp->dtmfcount -= (timestamp - rtp->lastrxts);
540 if (rtp->dtmfcount < 0)
543 if (dtmftimeout != rtp->dtmfcount)
544 printf("dtmfcount is %d\n", rtp->dtmfcount);
547 rtp->lastrxts = timestamp;
549 /* Send any pending DTMF */
550 if (rtp->resp && !rtp->dtmfcount) {
552 ast_log(LOG_DEBUG, "Sending pending DTMF\n");
553 return send_dtmf(rtp);
556 rtp->f.datalen = res - hdrlen;
557 rtp->f.data = rtp->rawdata + hdrlen + AST_FRIENDLY_OFFSET;
558 rtp->f.offset = hdrlen + AST_FRIENDLY_OFFSET;
559 if (rtp->f.subclass < AST_FORMAT_MAX_AUDIO) {
560 rtp->f.samples = ast_codec_get_samples(&rtp->f);
561 if (rtp->f.subclass == AST_FORMAT_SLINEAR)
562 ast_frame_byteswap_be(&rtp->f);
563 calc_rxstamp(&rtp->f.delivery, rtp, timestamp, mark);
565 /* Video -- samples is # of samples vs. 90000 */
566 if (!rtp->lastividtimestamp)
567 rtp->lastividtimestamp = timestamp;
568 rtp->f.samples = timestamp - rtp->lastividtimestamp;
569 rtp->lastividtimestamp = timestamp;
570 rtp->f.delivery.tv_sec = 0;
571 rtp->f.delivery.tv_usec = 0;
573 rtp->f.subclass |= 0x1;
580 /* The following array defines the MIME Media type (and subtype) for each
581 of our codecs, or RTP-specific data type. */
583 struct rtpPayloadType payloadType;
587 {{1, AST_FORMAT_G723_1}, "audio", "G723"},
588 {{1, AST_FORMAT_GSM}, "audio", "GSM"},
589 {{1, AST_FORMAT_ULAW}, "audio", "PCMU"},
590 {{1, AST_FORMAT_ALAW}, "audio", "PCMA"},
591 {{1, AST_FORMAT_G726}, "audio", "G726-32"},
592 {{1, AST_FORMAT_ADPCM}, "audio", "DVI4"},
593 {{1, AST_FORMAT_SLINEAR}, "audio", "L16"},
594 {{1, AST_FORMAT_LPC10}, "audio", "LPC"},
595 {{1, AST_FORMAT_G729A}, "audio", "G729"},
596 {{1, AST_FORMAT_SPEEX}, "audio", "speex"},
597 {{1, AST_FORMAT_ILBC}, "audio", "iLBC"},
598 {{0, AST_RTP_DTMF}, "audio", "telephone-event"},
599 {{0, AST_RTP_CISCO_DTMF}, "audio", "cisco-telephone-event"},
600 {{0, AST_RTP_CN}, "audio", "CN"},
601 {{1, AST_FORMAT_JPEG}, "video", "JPEG"},
602 {{1, AST_FORMAT_PNG}, "video", "PNG"},
603 {{1, AST_FORMAT_H261}, "video", "H261"},
604 {{1, AST_FORMAT_H263}, "video", "H263"},
605 {{1, AST_FORMAT_H263_PLUS}, "video", "h263-1998"},
608 /* Static (i.e., well-known) RTP payload types for our "AST_FORMAT..."s:
609 also, our own choices for dynamic payload types. This is our master
610 table for transmission */
611 static struct rtpPayloadType static_RTP_PT[MAX_RTP_PT] = {
612 [0] = {1, AST_FORMAT_ULAW},
613 #ifdef USE_DEPRECATED_G726
614 [2] = {1, AST_FORMAT_G726}, /* Technically this is G.721, but if Cisco can do it, so can we... */
616 [3] = {1, AST_FORMAT_GSM},
617 [4] = {1, AST_FORMAT_G723_1},
618 [5] = {1, AST_FORMAT_ADPCM}, /* 8 kHz */
619 [6] = {1, AST_FORMAT_ADPCM}, /* 16 kHz */
620 [7] = {1, AST_FORMAT_LPC10},
621 [8] = {1, AST_FORMAT_ALAW},
622 [10] = {1, AST_FORMAT_SLINEAR}, /* 2 channels */
623 [11] = {1, AST_FORMAT_SLINEAR}, /* 1 channel */
624 [13] = {0, AST_RTP_CN},
625 [16] = {1, AST_FORMAT_ADPCM}, /* 11.025 kHz */
626 [17] = {1, AST_FORMAT_ADPCM}, /* 22.050 kHz */
627 [18] = {1, AST_FORMAT_G729A},
628 [19] = {0, AST_RTP_CN}, /* Also used for CN */
629 [26] = {1, AST_FORMAT_JPEG},
630 [31] = {1, AST_FORMAT_H261},
631 [34] = {1, AST_FORMAT_H263},
632 [103] = {1, AST_FORMAT_H263_PLUS},
633 [97] = {1, AST_FORMAT_ILBC},
634 [101] = {0, AST_RTP_DTMF},
635 [110] = {1, AST_FORMAT_SPEEX},
636 [111] = {1, AST_FORMAT_G726},
637 [121] = {0, AST_RTP_CISCO_DTMF}, /* Must be type 121 */
640 void ast_rtp_pt_clear(struct ast_rtp* rtp)
644 for (i = 0; i < MAX_RTP_PT; ++i) {
645 rtp->current_RTP_PT[i].isAstFormat = 0;
646 rtp->current_RTP_PT[i].code = 0;
649 rtp->rtp_lookup_code_cache_isAstFormat = 0;
650 rtp->rtp_lookup_code_cache_code = 0;
651 rtp->rtp_lookup_code_cache_result = 0;
654 void ast_rtp_pt_default(struct ast_rtp* rtp)
658 /* Initialize to default payload types */
659 for (i = 0; i < MAX_RTP_PT; ++i) {
660 rtp->current_RTP_PT[i].isAstFormat = static_RTP_PT[i].isAstFormat;
661 rtp->current_RTP_PT[i].code = static_RTP_PT[i].code;
664 rtp->rtp_lookup_code_cache_isAstFormat = 0;
665 rtp->rtp_lookup_code_cache_code = 0;
666 rtp->rtp_lookup_code_cache_result = 0;
669 /* Make a note of a RTP payload type that was seen in a SDP "m=" line. */
670 /* By default, use the well-known value for this type (although it may */
671 /* still be set to a different value by a subsequent "a=rtpmap:" line): */
672 void ast_rtp_set_m_type(struct ast_rtp* rtp, int pt) {
673 if (pt < 0 || pt > MAX_RTP_PT)
674 return; /* bogus payload type */
676 if (static_RTP_PT[pt].code != 0) {
677 rtp->current_RTP_PT[pt] = static_RTP_PT[pt];
681 /* Make a note of a RTP payload type (with MIME type) that was seen in */
682 /* a SDP "a=rtpmap:" line. */
683 void ast_rtp_set_rtpmap_type(struct ast_rtp* rtp, int pt,
684 char* mimeType, char* mimeSubtype) {
687 if (pt < 0 || pt > MAX_RTP_PT)
688 return; /* bogus payload type */
690 for (i = 0; i < sizeof mimeTypes/sizeof mimeTypes[0]; ++i) {
691 if (strcasecmp(mimeSubtype, mimeTypes[i].subtype) == 0 &&
692 strcasecmp(mimeType, mimeTypes[i].type) == 0) {
693 rtp->current_RTP_PT[pt] = mimeTypes[i].payloadType;
699 /* Return the union of all of the codecs that were set by rtp_set...() calls */
700 /* They're returned as two distinct sets: AST_FORMATs, and AST_RTPs */
701 void ast_rtp_get_current_formats(struct ast_rtp* rtp,
702 int* astFormats, int* nonAstFormats) {
705 *astFormats = *nonAstFormats = 0;
706 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
707 if (rtp->current_RTP_PT[pt].isAstFormat) {
708 *astFormats |= rtp->current_RTP_PT[pt].code;
710 *nonAstFormats |= rtp->current_RTP_PT[pt].code;
715 void ast_rtp_offered_from_local(struct ast_rtp* rtp, int local) {
717 rtp->rtp_offered_from_local = local;
719 ast_log(LOG_WARNING, "rtp structure is null\n");
722 struct rtpPayloadType ast_rtp_lookup_pt(struct ast_rtp* rtp, int pt)
724 struct rtpPayloadType result;
726 result.isAstFormat = result.code = 0;
727 if (pt < 0 || pt > MAX_RTP_PT)
728 return result; /* bogus payload type */
730 /* Start with the negotiated codecs */
731 if (!rtp->rtp_offered_from_local)
732 result = rtp->current_RTP_PT[pt];
734 /* If it doesn't exist, check our static RTP type list, just in case */
736 result = static_RTP_PT[pt];
740 /* Looks up an RTP code out of our *static* outbound list */
741 int ast_rtp_lookup_code(struct ast_rtp* rtp, const int isAstFormat, const int code) {
745 if (isAstFormat == rtp->rtp_lookup_code_cache_isAstFormat &&
746 code == rtp->rtp_lookup_code_cache_code) {
748 /* Use our cached mapping, to avoid the overhead of the loop below */
749 return rtp->rtp_lookup_code_cache_result;
752 /* Check the dynamic list first */
753 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
754 if (rtp->current_RTP_PT[pt].code == code && rtp->current_RTP_PT[pt].isAstFormat == isAstFormat) {
755 rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
756 rtp->rtp_lookup_code_cache_code = code;
757 rtp->rtp_lookup_code_cache_result = pt;
762 /* Then the static list */
763 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
764 if (static_RTP_PT[pt].code == code && static_RTP_PT[pt].isAstFormat == isAstFormat) {
765 rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
766 rtp->rtp_lookup_code_cache_code = code;
767 rtp->rtp_lookup_code_cache_result = pt;
774 char* ast_rtp_lookup_mime_subtype(const int isAstFormat, const int code) {
778 for (i = 0; i < sizeof mimeTypes/sizeof mimeTypes[0]; ++i) {
779 if (mimeTypes[i].payloadType.code == code && mimeTypes[i].payloadType.isAstFormat == isAstFormat) {
780 return mimeTypes[i].subtype;
786 char *ast_rtp_lookup_mime_multiple(char *buf, int size, const int capability, const int isAstFormat)
796 snprintf(end, size, "0x%x (", capability);
803 for (format = 1; format < AST_RTP_MAX; format <<= 1) {
804 if (capability & format) {
805 const char *name = ast_rtp_lookup_mime_subtype(isAstFormat, format);
806 snprintf(end, size, "%s|", name);
814 snprintf(start, size, "nothing)");
821 static int rtp_socket(void)
825 s = socket(AF_INET, SOCK_DGRAM, 0);
827 flags = fcntl(s, F_GETFL);
828 fcntl(s, F_SETFL, flags | O_NONBLOCK);
831 setsockopt(s, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
837 static struct ast_rtcp *ast_rtcp_new(void)
839 struct ast_rtcp *rtcp;
840 rtcp = malloc(sizeof(struct ast_rtcp));
843 memset(rtcp, 0, sizeof(struct ast_rtcp));
844 rtcp->s = rtp_socket();
845 rtcp->us.sin_family = AF_INET;
848 ast_log(LOG_WARNING, "Unable to allocate socket: %s\n", strerror(errno));
854 struct ast_rtp *ast_rtp_new_with_bindaddr(struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode, struct in_addr addr)
860 rtp = malloc(sizeof(struct ast_rtp));
863 memset(rtp, 0, sizeof(struct ast_rtp));
864 rtp->them.sin_family = AF_INET;
865 rtp->us.sin_family = AF_INET;
866 rtp->s = rtp_socket();
868 rtp->seqno = rand() & 0xffff;
871 ast_log(LOG_ERROR, "Unable to allocate socket: %s\n", strerror(errno));
874 if (sched && rtcpenable) {
876 rtp->rtcp = ast_rtcp_new();
878 /* Find us a place */
879 x = (rand() % (rtpend-rtpstart)) + rtpstart;
883 /* Must be an even port number by RTP spec */
884 rtp->us.sin_port = htons(x);
885 rtp->us.sin_addr = addr;
887 rtp->rtcp->us.sin_port = htons(x + 1);
888 if (!(first = bind(rtp->s, (struct sockaddr *)&rtp->us, sizeof(rtp->us))) &&
889 (!rtp->rtcp || !bind(rtp->rtcp->s, (struct sockaddr *)&rtp->rtcp->us, sizeof(rtp->rtcp->us))))
892 /* Primary bind succeeded! Gotta recreate it */
894 rtp->s = rtp_socket();
896 if (errno != EADDRINUSE) {
897 ast_log(LOG_ERROR, "Unexpected bind error: %s\n", strerror(errno));
908 x = (rtpstart + 1) & ~1;
909 if (x == startplace) {
910 ast_log(LOG_ERROR, "No RTP ports remaining. Can't setup media stream for this call.\n");
920 if (io && sched && callbackmode) {
921 /* Operate this one in a callback mode */
924 rtp->ioid = ast_io_add(rtp->io, rtp->s, rtpread, AST_IO_IN, rtp);
926 ast_rtp_pt_default(rtp);
930 struct ast_rtp *ast_rtp_new(struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode)
934 memset(&ia, 0, sizeof(ia));
935 return ast_rtp_new_with_bindaddr(sched, io, rtcpenable, callbackmode, ia);
938 int ast_rtp_settos(struct ast_rtp *rtp, int tos)
942 if ((res = setsockopt(rtp->s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))))
943 ast_log(LOG_WARNING, "Unable to set TOS to %d\n", tos);
947 void ast_rtp_set_peer(struct ast_rtp *rtp, struct sockaddr_in *them)
949 rtp->them.sin_port = them->sin_port;
950 rtp->them.sin_addr = them->sin_addr;
952 rtp->rtcp->them.sin_port = htons(ntohs(them->sin_port) + 1);
953 rtp->rtcp->them.sin_addr = them->sin_addr;
958 void ast_rtp_get_peer(struct ast_rtp *rtp, struct sockaddr_in *them)
960 them->sin_family = AF_INET;
961 them->sin_port = rtp->them.sin_port;
962 them->sin_addr = rtp->them.sin_addr;
965 void ast_rtp_get_us(struct ast_rtp *rtp, struct sockaddr_in *us)
967 memcpy(us, &rtp->us, sizeof(rtp->us));
970 void ast_rtp_stop(struct ast_rtp *rtp)
972 memset(&rtp->them.sin_addr, 0, sizeof(rtp->them.sin_addr));
973 memset(&rtp->them.sin_port, 0, sizeof(rtp->them.sin_port));
975 memset(&rtp->rtcp->them.sin_addr, 0, sizeof(rtp->them.sin_addr));
976 memset(&rtp->rtcp->them.sin_port, 0, sizeof(rtp->them.sin_port));
980 void ast_rtp_reset(struct ast_rtp *rtp)
982 memset(&rtp->rxcore, 0, sizeof(rtp->rxcore));
983 memset(&rtp->txcore, 0, sizeof(rtp->txcore));
984 memset(&rtp->dtmfmute, 0, sizeof(rtp->dtmfmute));
986 rtp->lastdigitts = 0;
988 rtp->lastividtimestamp = 0;
989 rtp->lastovidtimestamp = 0;
990 rtp->lasteventseqn = 0;
991 rtp->lasttxformat = 0;
992 rtp->lastrxformat = 0;
994 rtp->dtmfduration = 0;
999 void ast_rtp_destroy(struct ast_rtp *rtp)
1002 ast_smoother_free(rtp->smoother);
1004 ast_io_remove(rtp->io, rtp->ioid);
1008 close(rtp->rtcp->s);
1014 static unsigned int calc_txstamp(struct ast_rtp *rtp, struct timeval *delivery)
1018 if (!rtp->txcore.tv_sec && !rtp->txcore.tv_usec) {
1019 gettimeofday(&rtp->txcore, NULL);
1020 /* Round to 20ms for nice, pretty timestamps */
1021 rtp->txcore.tv_usec -= rtp->txcore.tv_usec % 20000;
1023 if (delivery && (delivery->tv_sec || delivery->tv_usec)) {
1024 /* Use previous txcore */
1025 ms = (delivery->tv_sec - rtp->txcore.tv_sec) * 1000;
1026 ms += (1000000 + delivery->tv_usec - rtp->txcore.tv_usec) / 1000 - 1000;
1027 rtp->txcore.tv_sec = delivery->tv_sec;
1028 rtp->txcore.tv_usec = delivery->tv_usec;
1030 gettimeofday(&now, NULL);
1031 ms = (now.tv_sec - rtp->txcore.tv_sec) * 1000;
1032 ms += (1000000 + now.tv_usec - rtp->txcore.tv_usec) / 1000 - 1000;
1033 /* Use what we just got for next time */
1034 rtp->txcore.tv_sec = now.tv_sec;
1035 rtp->txcore.tv_usec = now.tv_usec;
1040 int ast_rtp_senddigit(struct ast_rtp *rtp, char digit)
1042 unsigned int *rtpheader;
1048 char iabuf[INET_ADDRSTRLEN];
1050 if ((digit <= '9') && (digit >= '0'))
1052 else if (digit == '*')
1054 else if (digit == '#')
1056 else if ((digit >= 'A') && (digit <= 'D'))
1057 digit = digit - 'A' + 12;
1058 else if ((digit >= 'a') && (digit <= 'd'))
1059 digit = digit - 'a' + 12;
1061 ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
1064 payload = ast_rtp_lookup_code(rtp, 0, AST_RTP_DTMF);
1066 /* If we have no peer, return immediately */
1067 if (!rtp->them.sin_addr.s_addr)
1070 gettimeofday(&rtp->dtmfmute, NULL);
1071 rtp->dtmfmute.tv_usec += (500 * 1000);
1072 if (rtp->dtmfmute.tv_usec > 1000000) {
1073 rtp->dtmfmute.tv_usec -= 1000000;
1074 rtp->dtmfmute.tv_sec += 1;
1077 /* Get a pointer to the header */
1078 rtpheader = (unsigned int *)data;
1079 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno++));
1080 rtpheader[1] = htonl(rtp->lastdigitts);
1081 rtpheader[2] = htonl(rtp->ssrc);
1082 rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (0));
1084 if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
1085 res = sendto(rtp->s, (void *) rtpheader, hdrlen + 4, 0, (struct sockaddr *) &rtp->them, sizeof(rtp->them));
1087 ast_log(LOG_ERROR, "RTP Transmission error to %s:%d: %s\n",
1088 ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr),
1089 ntohs(rtp->them.sin_port), strerror(errno));
1090 if (rtp_debug_test_addr(&rtp->them))
1091 ast_verbose("Sent RTP packet to %s:%d (type %d, seq %d, ts %d, len %d)\n",
1092 ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr),
1093 ntohs(rtp->them.sin_port), payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
1095 /* Clear marker bit and increment seqno */
1096 rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno++));
1097 /* For the last three packets, set the duration and the end bit */
1100 /* No, this is wrong... Do not increment lastdigitts, that's not according
1101 to the RFC, as best we can determine */
1102 rtp->lastdigitts++; /* or else the SPA3000 will click instead of beeping... */
1104 rtpheader[1] = htonl(rtp->lastdigitts);
1105 /* Make duration 800 (100ms) */
1106 rtpheader[3] |= htonl((800));
1107 /* Set the End bit */
1108 rtpheader[3] |= htonl((1 << 23));
1111 /* Increment the digit timestamp by 120ms, to ensure that digits
1112 sent sequentially with no intervening non-digit packets do not
1113 get sent with the same timestamp, and that sequential digits
1114 have some 'dead air' in between them
1116 rtp->lastdigitts += 960;
1120 int ast_rtp_sendcng(struct ast_rtp *rtp, int level)
1122 unsigned int *rtpheader;
1127 char iabuf[INET_ADDRSTRLEN];
1128 level = 127 - (level & 0x7f);
1129 payload = ast_rtp_lookup_code(rtp, 0, AST_RTP_CN);
1131 /* If we have no peer, return immediately */
1132 if (!rtp->them.sin_addr.s_addr)
1135 gettimeofday(&rtp->dtmfmute, NULL);
1136 rtp->dtmfmute.tv_usec += (500 * 1000);
1137 if (rtp->dtmfmute.tv_usec > 1000000) {
1138 rtp->dtmfmute.tv_usec -= 1000000;
1139 rtp->dtmfmute.tv_sec += 1;
1142 /* Get a pointer to the header */
1143 rtpheader = (unsigned int *)data;
1144 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno++));
1145 rtpheader[1] = htonl(rtp->lastts);
1146 rtpheader[2] = htonl(rtp->ssrc);
1148 if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
1149 res = sendto(rtp->s, (void *)rtpheader, hdrlen + 1, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
1151 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));
1152 if(rtp_debug_test_addr(&rtp->them))
1153 ast_verbose("Sent Comfort Noise RTP packet to %s:%d (type %d, seq %d, ts %d, len %d)\n"
1154 , ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr), ntohs(rtp->them.sin_port), payload, rtp->seqno, rtp->lastts,res - hdrlen);
1160 static int ast_rtp_raw_write(struct ast_rtp *rtp, struct ast_frame *f, int codec)
1162 unsigned char *rtpheader;
1163 char iabuf[INET_ADDRSTRLEN];
1170 ms = calc_txstamp(rtp, &f->delivery);
1171 /* Default prediction */
1172 if (f->subclass < AST_FORMAT_MAX_AUDIO) {
1173 pred = rtp->lastts + ast_codec_get_samples(f);
1175 /* Re-calculate last TS */
1176 rtp->lastts = rtp->lastts + ms * 8;
1177 if (!f->delivery.tv_sec && !f->delivery.tv_usec) {
1178 /* If this isn't an absolute delivery time, Check if it is close to our prediction,
1179 and if so, go with our prediction */
1180 if (abs(rtp->lastts - pred) < MAX_TIMESTAMP_SKEW)
1183 if (option_debug > 2)
1184 ast_log(LOG_DEBUG, "Difference is %d, ms is %d\n", abs(rtp->lastts - pred), ms);
1189 mark = f->subclass & 0x1;
1190 pred = rtp->lastovidtimestamp + f->samples;
1191 /* Re-calculate last TS */
1192 rtp->lastts = rtp->lastts + ms * 90;
1193 /* If it's close to our prediction, go for it */
1194 if (!f->delivery.tv_sec && !f->delivery.tv_usec) {
1195 if (abs(rtp->lastts - pred) < 7200) {
1197 rtp->lastovidtimestamp += f->samples;
1199 if (option_debug > 2)
1200 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);
1201 rtp->lastovidtimestamp = rtp->lastts;
1205 /* If the timestamp for non-digit packets has moved beyond the timestamp
1206 for digits, update the digit timestamp.
1208 if (rtp->lastts > rtp->lastdigitts)
1209 rtp->lastdigitts = rtp->lastts;
1211 /* Get a pointer to the header */
1212 rtpheader = (unsigned char *)(f->data - hdrlen);
1214 put_unaligned_uint32(rtpheader, htonl((2 << 30) | (codec << 16) | (rtp->seqno) | (mark << 23)));
1215 put_unaligned_uint32(rtpheader + 4, htonl(rtp->lastts));
1216 put_unaligned_uint32(rtpheader + 8, htonl(rtp->ssrc));
1218 if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
1219 res = sendto(rtp->s, (void *)rtpheader, f->datalen + hdrlen, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
1221 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));
1222 if(rtp_debug_test_addr(&rtp->them))
1223 ast_verbose("Sent RTP packet to %s:%d (type %d, seq %d, ts %d, len %d)\n"
1224 , ast_inet_ntoa(iabuf, sizeof(iabuf), rtp->them.sin_addr), ntohs(rtp->them.sin_port), codec, rtp->seqno, rtp->lastts,res - hdrlen);
1232 int ast_rtp_write(struct ast_rtp *rtp, struct ast_frame *_f)
1234 struct ast_frame *f;
1240 /* If we have no peer, return immediately */
1241 if (!rtp->them.sin_addr.s_addr)
1244 /* If there is no data length, return immediately */
1248 /* Make sure we have enough space for RTP header */
1249 if ((_f->frametype != AST_FRAME_VOICE) && (_f->frametype != AST_FRAME_VIDEO)) {
1250 ast_log(LOG_WARNING, "RTP can only send voice\n");
1254 subclass = _f->subclass;
1255 if (_f->frametype == AST_FRAME_VIDEO)
1258 codec = ast_rtp_lookup_code(rtp, 1, subclass);
1260 ast_log(LOG_WARNING, "Don't know how to send format %s packets with RTP\n", ast_getformatname(_f->subclass));
1264 if (rtp->lasttxformat != subclass) {
1265 /* New format, reset the smoother */
1267 ast_log(LOG_DEBUG, "Ooh, format changed from %s to %s\n", ast_getformatname(rtp->lasttxformat), ast_getformatname(subclass));
1268 rtp->lasttxformat = subclass;
1270 ast_smoother_free(rtp->smoother);
1271 rtp->smoother = NULL;
1276 case AST_FORMAT_SLINEAR:
1277 if (!rtp->smoother) {
1278 rtp->smoother = ast_smoother_new(320);
1280 if (!rtp->smoother) {
1281 ast_log(LOG_WARNING, "Unable to create smoother :(\n");
1284 ast_smoother_feed_be(rtp->smoother, _f);
1286 while((f = ast_smoother_read(rtp->smoother)))
1287 ast_rtp_raw_write(rtp, f, codec);
1289 case AST_FORMAT_ULAW:
1290 case AST_FORMAT_ALAW:
1291 if (!rtp->smoother) {
1292 rtp->smoother = ast_smoother_new(160);
1294 if (!rtp->smoother) {
1295 ast_log(LOG_WARNING, "Unable to create smoother :(\n");
1298 ast_smoother_feed(rtp->smoother, _f);
1300 while((f = ast_smoother_read(rtp->smoother)))
1301 ast_rtp_raw_write(rtp, f, codec);
1303 case AST_FORMAT_ADPCM:
1304 case AST_FORMAT_G726:
1305 if (!rtp->smoother) {
1306 rtp->smoother = ast_smoother_new(80);
1308 if (!rtp->smoother) {
1309 ast_log(LOG_WARNING, "Unable to create smoother :(\n");
1312 ast_smoother_feed(rtp->smoother, _f);
1314 while((f = ast_smoother_read(rtp->smoother)))
1315 ast_rtp_raw_write(rtp, f, codec);
1317 case AST_FORMAT_G729A:
1318 if (!rtp->smoother) {
1319 rtp->smoother = ast_smoother_new(20);
1321 ast_smoother_set_flags(rtp->smoother, AST_SMOOTHER_FLAG_G729);
1323 if (!rtp->smoother) {
1324 ast_log(LOG_WARNING, "Unable to create g729 smoother :(\n");
1327 ast_smoother_feed(rtp->smoother, _f);
1329 while((f = ast_smoother_read(rtp->smoother)))
1330 ast_rtp_raw_write(rtp, f, codec);
1332 case AST_FORMAT_GSM:
1333 if (!rtp->smoother) {
1334 rtp->smoother = ast_smoother_new(33);
1336 if (!rtp->smoother) {
1337 ast_log(LOG_WARNING, "Unable to create GSM smoother :(\n");
1340 ast_smoother_feed(rtp->smoother, _f);
1341 while((f = ast_smoother_read(rtp->smoother)))
1342 ast_rtp_raw_write(rtp, f, codec);
1344 case AST_FORMAT_ILBC:
1345 if (!rtp->smoother) {
1346 rtp->smoother = ast_smoother_new(50);
1348 if (!rtp->smoother) {
1349 ast_log(LOG_WARNING, "Unable to create ILBC smoother :(\n");
1352 ast_smoother_feed(rtp->smoother, _f);
1353 while((f = ast_smoother_read(rtp->smoother)))
1354 ast_rtp_raw_write(rtp, f, codec);
1357 ast_log(LOG_WARNING, "Not sure about sending format %s packets\n", ast_getformatname(subclass));
1358 /* fall through to... */
1359 case AST_FORMAT_H261:
1360 case AST_FORMAT_H263:
1361 case AST_FORMAT_H263_PLUS:
1362 case AST_FORMAT_G723_1:
1363 case AST_FORMAT_LPC10:
1364 case AST_FORMAT_SPEEX:
1365 /* Don't buffer outgoing frames; send them one-per-packet: */
1366 if (_f->offset < hdrlen) {
1371 ast_rtp_raw_write(rtp, f, codec);
1377 void ast_rtp_proto_unregister(struct ast_rtp_protocol *proto)
1379 struct ast_rtp_protocol *cur, *prev;
1386 prev->next = proto->next;
1388 protos = proto->next;
1396 int ast_rtp_proto_register(struct ast_rtp_protocol *proto)
1398 struct ast_rtp_protocol *cur;
1401 if (cur->type == proto->type) {
1402 ast_log(LOG_WARNING, "Tried to register same protocol '%s' twice\n", cur->type);
1407 proto->next = protos;
1412 static struct ast_rtp_protocol *get_proto(struct ast_channel *chan)
1414 struct ast_rtp_protocol *cur;
1417 if (cur->type == chan->type) {
1425 /* ast_rtp_bridge: Bridge calls. If possible and allowed, initiate
1426 re-invite so the peers exchange media directly outside
1428 int ast_rtp_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc)
1430 struct ast_frame *f;
1431 struct ast_channel *who, *cs[3];
1432 struct ast_rtp *p0, *p1;
1433 struct ast_rtp *vp0, *vp1;
1434 struct ast_rtp_protocol *pr0, *pr1;
1435 struct sockaddr_in ac0, ac1;
1436 struct sockaddr_in vac0, vac1;
1437 struct sockaddr_in t0, t1;
1438 struct sockaddr_in vt0, vt1;
1439 char iabuf[INET_ADDRSTRLEN];
1443 int codec0,codec1, oldcodec0, oldcodec1;
1445 memset(&vt0, 0, sizeof(vt0));
1446 memset(&vt1, 0, sizeof(vt1));
1447 memset(&vac0, 0, sizeof(vac0));
1448 memset(&vac1, 0, sizeof(vac1));
1450 /* if need DTMF, cant native bridge */
1451 if (flags & (AST_BRIDGE_DTMF_CHANNEL_0 | AST_BRIDGE_DTMF_CHANNEL_1))
1453 ast_mutex_lock(&c0->lock);
1454 while(ast_mutex_trylock(&c1->lock)) {
1455 ast_mutex_unlock(&c0->lock);
1457 ast_mutex_lock(&c0->lock);
1459 pr0 = get_proto(c0);
1460 pr1 = get_proto(c1);
1462 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c0->name);
1463 ast_mutex_unlock(&c0->lock);
1464 ast_mutex_unlock(&c1->lock);
1468 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c1->name);
1469 ast_mutex_unlock(&c0->lock);
1470 ast_mutex_unlock(&c1->lock);
1473 pvt0 = c0->tech_pvt;
1474 pvt1 = c1->tech_pvt;
1475 p0 = pr0->get_rtp_info(c0);
1476 if (pr0->get_vrtp_info)
1477 vp0 = pr0->get_vrtp_info(c0);
1480 p1 = pr1->get_rtp_info(c1);
1481 if (pr1->get_vrtp_info)
1482 vp1 = pr1->get_vrtp_info(c1);
1486 /* Somebody doesn't want to play... */
1487 ast_mutex_unlock(&c0->lock);
1488 ast_mutex_unlock(&c1->lock);
1492 codec0 = pr0->get_codec(c0);
1496 codec1 = pr1->get_codec(c1);
1499 if (pr0->get_codec && pr1->get_codec) {
1500 /* Hey, we can't do reinvite if both parties speak different codecs */
1501 if (!(codec0 & codec1)) {
1503 ast_log(LOG_DEBUG, "Channel codec0 = %d is not codec1 = %d, cannot native bridge in RTP.\n", codec0, codec1);
1504 ast_mutex_unlock(&c0->lock);
1505 ast_mutex_unlock(&c1->lock);
1509 /* Ok, we should be able to redirect the media. Start with one channel */
1510 if (pr0->set_rtp_peer(c0, p1, vp1, codec1))
1511 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
1513 /* Store RTP peer */
1514 ast_rtp_get_peer(p1, &ac1);
1516 ast_rtp_get_peer(vp1, &vac1);
1518 /* Then test the other channel */
1519 if (pr1->set_rtp_peer(c1, p0, vp0, codec0))
1520 ast_log(LOG_WARNING, "Channel '%s' failed to talk back to '%s'\n", c1->name, c0->name);
1522 /* Store RTP peer */
1523 ast_rtp_get_peer(p0, &ac0);
1525 ast_rtp_get_peer(vp0, &vac0);
1527 ast_mutex_unlock(&c0->lock);
1528 ast_mutex_unlock(&c1->lock);
1535 if ((c0->tech_pvt != pvt0) ||
1536 (c1->tech_pvt != pvt1) ||
1537 (c0->masq || c0->masqr || c1->masq || c1->masqr)) {
1538 ast_log(LOG_DEBUG, "Oooh, something is weird, backing out\n");
1539 if (c0->tech_pvt == pvt0) {
1540 if (pr0->set_rtp_peer(c0, NULL, NULL, 0))
1541 ast_log(LOG_WARNING, "Channel '%s' failed to revert\n", c0->name);
1543 if (c1->tech_pvt == pvt1) {
1544 if (pr1->set_rtp_peer(c1, NULL, NULL, 0))
1545 ast_log(LOG_WARNING, "Channel '%s' failed to revert back\n", c1->name);
1547 /* Tell it to try again later */
1551 ast_rtp_get_peer(p1, &t1);
1552 ast_rtp_get_peer(p0, &t0);
1554 codec0 = pr0->get_codec(c0);
1556 codec1 = pr1->get_codec(c1);
1558 ast_rtp_get_peer(vp1, &vt1);
1560 ast_rtp_get_peer(vp0, &vt0);
1561 if (inaddrcmp(&t1, &ac1) || (vp1 && inaddrcmp(&vt1, &vac1)) || (codec1 != oldcodec1)) {
1563 ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
1564 c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), t1.sin_addr), ntohs(t1.sin_port), codec1);
1565 ast_log(LOG_DEBUG, "Oooh, '%s' changed end vaddress to %s:%d (format %d)\n",
1566 c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), vt1.sin_addr), ntohs(vt1.sin_port), codec1);
1567 ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d/(format %d)\n",
1568 c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), ac1.sin_addr), ntohs(ac1.sin_port), oldcodec1);
1569 ast_log(LOG_DEBUG, "Oooh, '%s' wasv %s:%d/(format %d)\n",
1570 c1->name, ast_inet_ntoa(iabuf, sizeof(iabuf), vac1.sin_addr), ntohs(vac1.sin_port), oldcodec1);
1572 if (pr0->set_rtp_peer(c0, t1.sin_addr.s_addr ? p1 : NULL, vt1.sin_addr.s_addr ? vp1 : NULL, codec1))
1573 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c0->name, c1->name);
1574 memcpy(&ac1, &t1, sizeof(ac1));
1575 memcpy(&vac1, &vt1, sizeof(vac1));
1578 if (inaddrcmp(&t0, &ac0) || (vp0 && inaddrcmp(&vt0, &vac0))) {
1580 ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
1581 c0->name, ast_inet_ntoa(iabuf, sizeof(iabuf), t0.sin_addr), ntohs(t0.sin_port), codec0);
1582 ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d/(format %d)\n",
1583 c0->name, ast_inet_ntoa(iabuf, sizeof(iabuf), ac0.sin_addr), ntohs(ac0.sin_port), oldcodec0);
1585 if (pr1->set_rtp_peer(c1, t0.sin_addr.s_addr ? p0 : NULL, vt0.sin_addr.s_addr ? vp0 : NULL, codec0))
1586 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c1->name, c0->name);
1587 memcpy(&ac0, &t0, sizeof(ac0));
1588 memcpy(&vac0, &vt0, sizeof(vac0));
1591 who = ast_waitfor_n(cs, 2, &to);
1594 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
1595 /* check for hangup / whentohangup */
1596 if (ast_check_hangup(c0) || ast_check_hangup(c1))
1601 if (!f || ((f->frametype == AST_FRAME_DTMF) &&
1602 (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
1603 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))))) {
1607 ast_log(LOG_DEBUG, "Oooh, got a %s\n", f ? "digit" : "hangup");
1608 if ((c0->tech_pvt == pvt0) && (!c0->_softhangup)) {
1609 if (pr0->set_rtp_peer(c0, NULL, NULL, 0))
1610 ast_log(LOG_WARNING, "Channel '%s' failed to revert\n", c0->name);
1612 if ((c1->tech_pvt == pvt1) && (!c1->_softhangup)) {
1613 if (pr1->set_rtp_peer(c1, NULL, NULL, 0))
1614 ast_log(LOG_WARNING, "Channel '%s' failed to revert back\n", c1->name);
1616 /* That's all we needed */
1619 if ((f->frametype == AST_FRAME_DTMF) ||
1620 (f->frametype == AST_FRAME_VOICE) ||
1621 (f->frametype == AST_FRAME_VIDEO)) {
1622 /* Forward voice or DTMF frames if they happen upon us */
1625 } else if (who == c1) {
1631 /* Swap priority not that it's a big deal at this point */
1640 static int rtp_do_debug_ip(int fd, int argc, char *argv[])
1643 struct ast_hostent ahp;
1644 char iabuf[INET_ADDRSTRLEN];
1649 return RESULT_SHOWUSAGE;
1651 p = strstr(arg, ":");
1657 hp = ast_gethostbyname(arg, &ahp);
1659 return RESULT_SHOWUSAGE;
1660 rtpdebugaddr.sin_family = AF_INET;
1661 memcpy(&rtpdebugaddr.sin_addr, hp->h_addr, sizeof(rtpdebugaddr.sin_addr));
1662 rtpdebugaddr.sin_port = htons(port);
1664 ast_cli(fd, "RTP Debugging Enabled for IP: %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), rtpdebugaddr.sin_addr));
1666 ast_cli(fd, "RTP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(iabuf, sizeof(iabuf), rtpdebugaddr.sin_addr), port);
1668 return RESULT_SUCCESS;
1671 static int rtp_do_debug(int fd, int argc, char *argv[])
1675 return RESULT_SHOWUSAGE;
1676 return rtp_do_debug_ip(fd, argc, argv);
1679 memset(&rtpdebugaddr,0,sizeof(rtpdebugaddr));
1680 ast_cli(fd, "RTP Debugging Enabled\n");
1681 return RESULT_SUCCESS;
1684 static int rtp_no_debug(int fd, int argc, char *argv[])
1687 return RESULT_SHOWUSAGE;
1689 ast_cli(fd,"RTP Debugging Disabled\n");
1690 return RESULT_SUCCESS;
1693 static char debug_usage[] =
1694 "Usage: rtp debug [ip host[:port]]\n"
1695 " Enable dumping of all RTP packets to and from host.\n";
1697 static char no_debug_usage[] =
1698 "Usage: rtp no debug\n"
1699 " Disable all RTP debugging\n";
1701 static struct ast_cli_entry cli_debug_ip =
1702 {{ "rtp", "debug", "ip", NULL } , rtp_do_debug, "Enable RTP debugging on IP", debug_usage };
1704 static struct ast_cli_entry cli_debug =
1705 {{ "rtp", "debug", NULL } , rtp_do_debug, "Enable RTP debugging", debug_usage };
1707 static struct ast_cli_entry cli_no_debug =
1708 {{ "rtp", "no", "debug", NULL } , rtp_no_debug, "Disable RTP debugging", no_debug_usage };
1710 void ast_rtp_reload(void)
1712 struct ast_config *cfg;
1717 cfg = ast_config_load("rtp.conf");
1719 if ((s = ast_variable_retrieve(cfg, "general", "rtpstart"))) {
1721 if (rtpstart < 1024)
1723 if (rtpstart > 65535)
1726 if ((s = ast_variable_retrieve(cfg, "general", "rtpend"))) {
1733 if ((s = ast_variable_retrieve(cfg, "general", "rtpchecksums"))) {
1741 ast_log(LOG_WARNING, "Disabling RTP checksums is not supported on this operating system!\n");
1744 ast_config_destroy(cfg);
1746 if (rtpstart >= rtpend) {
1747 ast_log(LOG_WARNING, "Unreasonable values for RTP start/end port in rtp.conf\n");
1751 if (option_verbose > 1)
1752 ast_verbose(VERBOSE_PREFIX_2 "RTP Allocating from port range %d -> %d\n", rtpstart, rtpend);
1756 void ast_rtp_init(void)
1758 ast_cli_register(&cli_debug);
1759 ast_cli_register(&cli_debug_ip);
1760 ast_cli_register(&cli_no_debug);