2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
22 * \brief Supports RTP and RTCP with Symmetric RTP support for NAT traversal.
24 * \author Mark Spencer <markster@digium.com>
26 * \note RTP is defined in RFC 3550.
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
40 #include <netinet/in.h>
42 #include <sys/socket.h>
43 #include <arpa/inet.h>
46 #include "asterisk/rtp.h"
47 #include "asterisk/frame.h"
48 #include "asterisk/logger.h"
49 #include "asterisk/options.h"
50 #include "asterisk/channel.h"
51 #include "asterisk/acl.h"
52 #include "asterisk/channel.h"
53 #include "asterisk/config.h"
54 #include "asterisk/lock.h"
55 #include "asterisk/utils.h"
56 #include "asterisk/cli.h"
57 #include "asterisk/unaligned.h"
58 #include "asterisk/utils.h"
60 #define MAX_TIMESTAMP_SKEW 640
62 #define RTP_SEQ_MOD (1<<16) /*!< A sequence number can't be more than 16 bits */
63 #define RTCP_DEFAULT_INTERVALMS 5000 /*!< Default milli-seconds between RTCP reports we send */
64 #define RTCP_MIN_INTERVALMS 500 /*!< Min milli-seconds between RTCP reports we send */
65 #define RTCP_MAX_INTERVALMS 60000 /*!< Max milli-seconds between RTCP reports we send */
67 #define RTCP_PT_FUR 192
68 #define RTCP_PT_SR 200
69 #define RTCP_PT_RR 201
70 #define RTCP_PT_SDES 202
71 #define RTCP_PT_BYE 203
72 #define RTCP_PT_APP 204
76 #define DEFAULT_DTMF_TIMEOUT 3000 /*!< samples */
78 static int dtmftimeout = DEFAULT_DTMF_TIMEOUT;
80 static int rtpstart = 0; /*!< First port for RTP sessions (set in rtp.conf) */
81 static int rtpend = 0; /*!< Last port for RTP sessions (set in rtp.conf) */
82 static int rtpdebug = 0; /*!< Are we debugging? */
83 static int rtcpdebug = 0; /*!< Are we debugging RTCP? */
84 static int rtcpstats = 0; /*!< Are we debugging RTCP? */
85 static int rtcpinterval = RTCP_DEFAULT_INTERVALMS; /*!< Time between rtcp reports in millisecs */
86 static int stundebug = 0; /*!< Are we debugging stun? */
87 static struct sockaddr_in rtpdebugaddr; /*!< Debug packets to/from this host */
88 static struct sockaddr_in rtcpdebugaddr; /*!< Debug RTCP packets to/from this host */
90 static int nochecksums = 0;
94 * \brief Structure representing a RTP session.
96 * RTP session is defined on page 9 of RFC 3550: "An association among a set of participants communicating with RTP. A participant may be involved in multiple RTP sessions at the same time [...]"
99 /*! \brief The value of each payload format mapping: */
100 struct rtpPayloadType {
101 int isAstFormat; /*!< whether the following code is an AST_FORMAT */
106 /*! \brief RTP session description */
111 unsigned char rawdata[8192 + AST_FRIENDLY_OFFSET];
112 unsigned int ssrc; /*!< Synchronization source, RFC 3550, page 10. */
113 unsigned int themssrc; /*!< Their SSRC */
116 unsigned int lastdigitts;
117 unsigned int lastrxts;
118 unsigned int lastividtimestamp;
119 unsigned int lastovidtimestamp;
120 unsigned int lasteventseqn;
121 int lastrxseqno; /*!< Last received sequence number */
122 unsigned short seedrxseqno; /*!< What sequence number did they start with?*/
123 unsigned int seedrxts; /*!< What RTP timestamp did they start with? */
124 unsigned int rxcount; /*!< How many packets have we received? */
125 unsigned int rxoctetcount; /*!< How many octets have we received? should be rxcount *160*/
126 unsigned int txcount; /*!< How many packets have we sent? */
127 unsigned int txoctetcount; /*!< How many octets have we sent? (txcount*160)*/
128 unsigned int cycles; /*!< Shifted count of sequence number cycles */
129 double rxjitter; /*!< Interarrival jitter at the moment */
130 double rxtransit; /*!< Relative transit time for previous packet */
131 unsigned int lasteventendseqn;
135 unsigned int dtmfduration;
138 struct sockaddr_in us; /*!< Socket representation of the local endpoint. */
139 struct sockaddr_in them; /*!< Socket representation of the remote endpoint. */
140 struct timeval rxcore;
141 struct timeval txcore;
142 double drxcore; /*!< The double representation of the first received packet */
143 struct timeval lastrx; /*!< timeval when we last received a packet */
144 struct timeval dtmfmute;
145 struct ast_smoother *smoother;
147 unsigned short seqno; /*!< Sequence number, RFC 3550, page 13. */
148 unsigned short rxseqno;
149 struct sched_context *sched;
150 struct io_context *io;
152 ast_rtp_callback callback;
153 struct rtpPayloadType current_RTP_PT[MAX_RTP_PT];
154 int rtp_lookup_code_cache_isAstFormat; /*!< a cache for the result of rtp_lookup_code(): */
155 int rtp_lookup_code_cache_code;
156 int rtp_lookup_code_cache_result;
157 struct ast_rtcp *rtcp;
158 struct ast_rtp *bridged; /*!< Who we are Packet briged to */
161 /* Forward declarations */
162 static int ast_rtcp_write(void *data);
163 static void timeval2ntp(struct timeval tv, unsigned int *msw, unsigned int *lsw);
164 static int ast_rtcp_write_sr(void *data);
165 static int ast_rtcp_write_rr(void *data);
166 static unsigned int ast_rtcp_calc_interval(struct ast_rtp *rtp);
167 static int bridge_p2p_rtcp_write(struct ast_rtp *rtp, unsigned int *rtcpheader, int len);
169 #define FLAG_3389_WARNING (1 << 0)
170 #define FLAG_NAT_ACTIVE (3 << 1)
171 #define FLAG_NAT_INACTIVE (0 << 1)
172 #define FLAG_NAT_INACTIVE_NOWARN (1 << 1)
173 #define FLAG_HAS_DTMF (1 << 3)
174 #define FLAG_P2P_SENT_MARK (1 << 4)
175 #define FLAG_P2P_NEED_DTMF (1 << 5)
176 #define FLAG_CALLBACK_MODE (1 << 6)
179 * \brief Structure defining an RTCP session.
181 * The concept "RTCP session" is not defined in RFC 3550, but since
182 * this structure is analogous to ast_rtp, which tracks a RTP session,
183 * it is logical to think of this as a RTCP session.
185 * RTCP packet is defined on page 9 of RFC 3550.
189 int s; /*!< Socket */
190 struct sockaddr_in us; /*!< Socket representation of the local endpoint. */
191 struct sockaddr_in them; /*!< Socket representation of the remote endpoint. */
192 unsigned int soc; /*!< What they told us */
193 unsigned int spc; /*!< What they told us */
194 unsigned int themrxlsr; /*!< The middle 32 bits of the NTP timestamp in the last received SR*/
195 struct timeval rxlsr; /*!< Time when we got their last SR */
196 struct timeval txlsr; /*!< Time when we sent or last SR*/
197 unsigned int expected_prior; /*!< no. packets in previous interval */
198 unsigned int received_prior; /*!< no. packets received in previous interval */
199 int schedid; /*!< Schedid returned from ast_sched_add() to schedule RTCP-transmissions*/
200 unsigned int rr_count; /*!< number of RRs we've sent, not including report blocks in SR's */
201 unsigned int sr_count; /*!< number of SRs we've sent */
202 unsigned int lastsrtxcount; /*!< Transmit packet count when last SR sent */
203 double accumulated_transit; /*!< accumulated a-dlsr-lsr */
204 double rtt; /*!< Last reported rtt */
205 unsigned int reported_jitter; /*!< The contents of their last jitter entry in the RR */
206 unsigned int reported_lost; /*!< Reported lost packets in their RR */
207 char quality[AST_MAX_USER_FIELD];
216 typedef struct { unsigned int id[4]; } __attribute__((packed)) stun_trans_id;
218 /* XXX Maybe stun belongs in another file if it ever has use outside of RTP */
220 unsigned short msgtype;
221 unsigned short msglen;
223 unsigned char ies[0];
224 } __attribute__((packed));
229 unsigned char value[0];
230 } __attribute__((packed));
233 unsigned char unused;
234 unsigned char family;
237 } __attribute__((packed));
239 #define STUN_IGNORE (0)
240 #define STUN_ACCEPT (1)
242 #define STUN_BINDREQ 0x0001
243 #define STUN_BINDRESP 0x0101
244 #define STUN_BINDERR 0x0111
245 #define STUN_SECREQ 0x0002
246 #define STUN_SECRESP 0x0102
247 #define STUN_SECERR 0x0112
249 #define STUN_MAPPED_ADDRESS 0x0001
250 #define STUN_RESPONSE_ADDRESS 0x0002
251 #define STUN_CHANGE_REQUEST 0x0003
252 #define STUN_SOURCE_ADDRESS 0x0004
253 #define STUN_CHANGED_ADDRESS 0x0005
254 #define STUN_USERNAME 0x0006
255 #define STUN_PASSWORD 0x0007
256 #define STUN_MESSAGE_INTEGRITY 0x0008
257 #define STUN_ERROR_CODE 0x0009
258 #define STUN_UNKNOWN_ATTRIBUTES 0x000a
259 #define STUN_REFLECTED_FROM 0x000b
261 static const char *stun_msg2str(int msg)
265 return "Binding Request";
267 return "Binding Response";
269 return "Binding Error Response";
271 return "Shared Secret Request";
273 return "Shared Secret Response";
275 return "Shared Secret Error Response";
277 return "Non-RFC3489 Message";
280 static const char *stun_attr2str(int msg)
283 case STUN_MAPPED_ADDRESS:
284 return "Mapped Address";
285 case STUN_RESPONSE_ADDRESS:
286 return "Response Address";
287 case STUN_CHANGE_REQUEST:
288 return "Change Request";
289 case STUN_SOURCE_ADDRESS:
290 return "Source Address";
291 case STUN_CHANGED_ADDRESS:
292 return "Changed Address";
297 case STUN_MESSAGE_INTEGRITY:
298 return "Message Integrity";
299 case STUN_ERROR_CODE:
301 case STUN_UNKNOWN_ATTRIBUTES:
302 return "Unknown Attributes";
303 case STUN_REFLECTED_FROM:
304 return "Reflected From";
306 return "Non-RFC3489 Attribute";
310 const char *username;
311 const char *password;
314 static int stun_process_attr(struct stun_state *state, struct stun_attr *attr)
317 ast_verbose("Found STUN Attribute %s (%04x), length %d\n",
318 stun_attr2str(ntohs(attr->attr)), ntohs(attr->attr), ntohs(attr->len));
319 switch(ntohs(attr->attr)) {
321 state->username = (const char *) (attr->value);
324 state->password = (const char *) (attr->value);
328 ast_verbose("Ignoring STUN attribute %s (%04x), length %d\n",
329 stun_attr2str(ntohs(attr->attr)), ntohs(attr->attr), ntohs(attr->len));
334 static void append_attr_string(struct stun_attr **attr, int attrval, const char *s, int *len, int *left)
336 int size = sizeof(**attr) + strlen(s);
338 (*attr)->attr = htons(attrval);
339 (*attr)->len = htons(strlen(s));
340 memcpy((*attr)->value, s, strlen(s));
341 (*attr) = (struct stun_attr *)((*attr)->value + strlen(s));
347 static void append_attr_address(struct stun_attr **attr, int attrval, struct sockaddr_in *sin, int *len, int *left)
349 int size = sizeof(**attr) + 8;
350 struct stun_addr *addr;
352 (*attr)->attr = htons(attrval);
353 (*attr)->len = htons(8);
354 addr = (struct stun_addr *)((*attr)->value);
357 addr->port = sin->sin_port;
358 addr->addr = sin->sin_addr.s_addr;
359 (*attr) = (struct stun_attr *)((*attr)->value + 8);
365 static int stun_send(int s, struct sockaddr_in *dst, struct stun_header *resp)
367 return sendto(s, resp, ntohs(resp->msglen) + sizeof(*resp), 0,
368 (struct sockaddr *)dst, sizeof(*dst));
371 static void stun_req_id(struct stun_header *req)
375 req->id.id[x] = ast_random();
378 size_t ast_rtp_alloc_size(void)
380 return sizeof(struct ast_rtp);
383 void ast_rtp_stun_request(struct ast_rtp *rtp, struct sockaddr_in *suggestion, const char *username)
385 struct stun_header *req;
386 unsigned char reqdata[1024];
388 struct stun_attr *attr;
390 req = (struct stun_header *)reqdata;
393 reqleft = sizeof(reqdata) - sizeof(struct stun_header);
396 attr = (struct stun_attr *)req->ies;
398 append_attr_string(&attr, STUN_USERNAME, username, &reqlen, &reqleft);
399 req->msglen = htons(reqlen);
400 req->msgtype = htons(STUN_BINDREQ);
401 stun_send(rtp->s, suggestion, req);
404 static int stun_handle_packet(int s, struct sockaddr_in *src, unsigned char *data, size_t len)
406 struct stun_header *resp, *hdr = (struct stun_header *)data;
407 struct stun_attr *attr;
408 struct stun_state st;
409 int ret = STUN_IGNORE;
410 unsigned char respdata[1024];
411 int resplen, respleft;
413 if (len < sizeof(struct stun_header)) {
415 ast_log(LOG_DEBUG, "Runt STUN packet (only %zd, wanting at least %zd)\n", len, sizeof(struct stun_header));
419 ast_verbose("STUN Packet, msg %s (%04x), length: %d\n", stun_msg2str(ntohs(hdr->msgtype)), ntohs(hdr->msgtype), ntohs(hdr->msglen));
420 if (ntohs(hdr->msglen) > len - sizeof(struct stun_header)) {
422 ast_log(LOG_DEBUG, "Scrambled STUN packet length (got %d, expecting %zd)\n", ntohs(hdr->msglen), len - sizeof(struct stun_header));
424 len = ntohs(hdr->msglen);
425 data += sizeof(struct stun_header);
426 memset(&st, 0, sizeof(st));
428 if (len < sizeof(struct stun_attr)) {
430 ast_log(LOG_DEBUG, "Runt Attribute (got %zd, expecting %zd)\n", len, sizeof(struct stun_attr));
433 attr = (struct stun_attr *)data;
434 if (ntohs(attr->len) > len) {
436 ast_log(LOG_DEBUG, "Inconsistent Attribute (length %d exceeds remaining msg len %zd)\n", ntohs(attr->len), len);
439 if (stun_process_attr(&st, attr)) {
441 ast_log(LOG_DEBUG, "Failed to handle attribute %s (%04x)\n", stun_attr2str(ntohs(attr->attr)), ntohs(attr->attr));
444 /* Clear attribute in case previous entry was a string */
446 data += ntohs(attr->len) + sizeof(struct stun_attr);
447 len -= ntohs(attr->len) + sizeof(struct stun_attr);
449 /* Null terminate any string */
451 resp = (struct stun_header *)respdata;
453 respleft = sizeof(respdata) - sizeof(struct stun_header);
457 attr = (struct stun_attr *)resp->ies;
459 switch(ntohs(hdr->msgtype)) {
462 ast_verbose("STUN Bind Request, username: %s\n",
463 st.username ? st.username : "<none>");
465 append_attr_string(&attr, STUN_USERNAME, st.username, &resplen, &respleft);
466 append_attr_address(&attr, STUN_MAPPED_ADDRESS, src, &resplen, &respleft);
467 resp->msglen = htons(resplen);
468 resp->msgtype = htons(STUN_BINDRESP);
469 stun_send(s, src, resp);
474 ast_verbose("Dunno what to do with STUN message %04x (%s)\n", ntohs(hdr->msgtype), stun_msg2str(ntohs(hdr->msgtype)));
480 /*! \brief List of current sessions */
481 static AST_LIST_HEAD_STATIC(protos, ast_rtp_protocol);
483 static void timeval2ntp(struct timeval tv, unsigned int *msw, unsigned int *lsw)
485 unsigned int sec, usec, frac;
486 sec = tv.tv_sec + 2208988800u; /* Sec between 1900 and 1970 */
488 frac = (usec << 12) + (usec << 8) - ((usec * 3650) >> 6);
493 int ast_rtp_fd(struct ast_rtp *rtp)
498 int ast_rtcp_fd(struct ast_rtp *rtp)
505 unsigned int ast_rtcp_calc_interval(struct ast_rtp *rtp)
507 unsigned int interval;
508 /*! \todo XXX Do a more reasonable calculation on this one
509 * Look in RFC 3550 Section A.7 for an example*/
510 interval = rtcpinterval;
514 void ast_rtp_set_data(struct ast_rtp *rtp, void *data)
519 void ast_rtp_set_callback(struct ast_rtp *rtp, ast_rtp_callback callback)
521 rtp->callback = callback;
524 void ast_rtp_setnat(struct ast_rtp *rtp, int nat)
529 void ast_rtp_setdtmf(struct ast_rtp *rtp, int dtmf)
531 ast_set2_flag(rtp, dtmf ? 1 : 0, FLAG_HAS_DTMF);
534 static struct ast_frame *send_dtmf(struct ast_rtp *rtp)
536 if (ast_tvcmp(ast_tvnow(), rtp->dtmfmute) < 0) {
538 ast_log(LOG_DEBUG, "Ignore potential DTMF echo from '%s'\n", ast_inet_ntoa(rtp->them.sin_addr));
540 rtp->dtmfduration = 0;
541 return &ast_null_frame;
544 ast_log(LOG_DEBUG, "Sending dtmf: %d (%c), at %s\n", rtp->resp, rtp->resp, ast_inet_ntoa(rtp->them.sin_addr));
545 if (rtp->resp == 'X') {
546 rtp->f.frametype = AST_FRAME_CONTROL;
547 rtp->f.subclass = AST_CONTROL_FLASH;
549 rtp->f.frametype = AST_FRAME_DTMF;
550 rtp->f.subclass = rtp->resp;
557 rtp->dtmfduration = 0;
562 static inline int rtp_debug_test_addr(struct sockaddr_in *addr)
566 if (rtpdebugaddr.sin_addr.s_addr) {
567 if (((ntohs(rtpdebugaddr.sin_port) != 0)
568 && (rtpdebugaddr.sin_port != addr->sin_port))
569 || (rtpdebugaddr.sin_addr.s_addr != addr->sin_addr.s_addr))
575 static inline int rtcp_debug_test_addr(struct sockaddr_in *addr)
579 if (rtcpdebugaddr.sin_addr.s_addr) {
580 if (((ntohs(rtcpdebugaddr.sin_port) != 0)
581 && (rtcpdebugaddr.sin_port != addr->sin_port))
582 || (rtcpdebugaddr.sin_addr.s_addr != addr->sin_addr.s_addr))
589 static struct ast_frame *process_cisco_dtmf(struct ast_rtp *rtp, unsigned char *data, int len)
593 struct ast_frame *f = NULL;
594 event = ntohl(*((unsigned int *)(data)));
596 if (option_debug > 2 || rtpdebug)
597 ast_log(LOG_DEBUG, "Cisco DTMF Digit: %08x (len = %d)\n", event, len);
600 } else if (event < 11) {
602 } else if (event < 12) {
604 } else if (event < 16) {
605 resp = 'A' + (event - 12);
606 } else if (event < 17) {
609 if (rtp->resp && (rtp->resp != resp)) {
613 rtp->dtmfcount = dtmftimeout;
618 * \brief Process RTP DTMF and events according to RFC 2833.
620 * RFC 2833 is "RTP Payload for DTMF Digits, Telephony Tones and Telephony Signals".
628 static struct ast_frame *process_rfc2833(struct ast_rtp *rtp, unsigned char *data, int len, unsigned int seqno)
631 unsigned int event_end;
632 unsigned int duration;
634 struct ast_frame *f = NULL;
636 event = ntohl(*((unsigned int *)(data)));
638 event_end = ntohl(*((unsigned int *)(data)));
641 duration = ntohl(*((unsigned int *)(data)));
643 if (rtpdebug || option_debug > 2)
644 ast_log(LOG_DEBUG, "- RTP 2833 Event: %08x (len = %d)\n", event, len);
647 } else if (event < 11) {
649 } else if (event < 12) {
651 } else if (event < 16) {
652 resp = 'A' + (event - 12);
653 } else if (event < 17) { /* Event 16: Hook flash */
656 if (rtp->resp && (rtp->resp != resp)) {
658 } else if (event_end & 0x80) {
660 if (rtp->lasteventendseqn != seqno) {
662 rtp->lasteventendseqn = seqno;
668 } else if (rtp->resp && rtp->dtmfduration && (duration < rtp->dtmfduration)) {
671 if (!(event_end & 0x80))
673 rtp->dtmfcount = dtmftimeout;
674 rtp->dtmfduration = duration;
679 * \brief Process Comfort Noise RTP.
681 * This is incomplete at the moment.
684 static struct ast_frame *process_rfc3389(struct ast_rtp *rtp, unsigned char *data, int len)
686 struct ast_frame *f = NULL;
687 /* Convert comfort noise into audio with various codecs. Unfortunately this doesn't
688 totally help us out becuase we don't have an engine to keep it going and we are not
689 guaranteed to have it every 20ms or anything */
691 ast_log(LOG_DEBUG, "- RTP 3389 Comfort noise event: Level %d (len = %d)\n", rtp->lastrxformat, len);
693 if (!(ast_test_flag(rtp, FLAG_3389_WARNING))) {
694 ast_log(LOG_NOTICE, "Comfort noise support incomplete in Asterisk (RFC 3389). Please turn off on client if possible. Client IP: %s\n",
695 ast_inet_ntoa(rtp->them.sin_addr));
696 ast_set_flag(rtp, FLAG_3389_WARNING);
699 /* Must have at least one byte */
703 rtp->f.data = rtp->rawdata + AST_FRIENDLY_OFFSET;
704 rtp->f.datalen = len - 1;
705 rtp->f.offset = AST_FRIENDLY_OFFSET;
706 memcpy(rtp->f.data, data + 1, len - 1);
712 rtp->f.frametype = AST_FRAME_CNG;
713 rtp->f.subclass = data[0] & 0x7f;
714 rtp->f.datalen = len - 1;
716 rtp->f.delivery.tv_usec = rtp->f.delivery.tv_sec = 0;
721 static int rtpread(int *id, int fd, short events, void *cbdata)
723 struct ast_rtp *rtp = cbdata;
725 f = ast_rtp_read(rtp);
728 rtp->callback(rtp, f, rtp->data);
733 struct ast_frame *ast_rtcp_read(struct ast_rtp *rtp)
736 int position, i, packetwords;
738 struct sockaddr_in sin;
739 unsigned int rtcpdata[8192 + AST_FRIENDLY_OFFSET];
740 unsigned int *rtcpheader;
752 struct ast_frame *f = &ast_null_frame;
754 if (!rtp || !rtp->rtcp)
755 return &ast_null_frame;
759 res = recvfrom(rtp->rtcp->s, rtcpdata + AST_FRIENDLY_OFFSET, sizeof(rtcpdata) - sizeof(unsigned int) * AST_FRIENDLY_OFFSET,
760 0, (struct sockaddr *)&sin, &len);
761 rtcpheader = (unsigned int *)(rtcpdata + AST_FRIENDLY_OFFSET);
765 ast_log(LOG_WARNING, "RTCP Read error: %s\n", strerror(errno));
768 return &ast_null_frame;
771 packetwords = res / 4;
774 /* Send to whoever sent to us */
775 if ((rtp->rtcp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
776 (rtp->rtcp->them.sin_port != sin.sin_port)) {
777 memcpy(&rtp->rtcp->them, &sin, sizeof(rtp->rtcp->them));
778 if (option_debug || rtpdebug)
779 ast_log(LOG_DEBUG, "RTCP NAT: Got RTCP from other end. Now sending to address %s:%d\n", ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
783 /* If we are P2P bridged to another RTP stream, send it directly over */
784 if (ast_rtp_get_bridged(rtp) && !bridge_p2p_rtcp_write(rtp, rtcpheader, res))
785 return &ast_null_frame;
788 ast_log(LOG_DEBUG, "Got RTCP report of %d bytes\n", res);
790 /* Process a compound packet */
792 while (position < packetwords) {
794 length = ntohl(rtcpheader[i]);
795 pt = (length & 0xff0000) >> 16;
796 rc = (length & 0x1f000000) >> 24;
799 if ((i + length) > packetwords) {
800 ast_log(LOG_WARNING, "RTCP Read too short\n");
801 return &ast_null_frame;
804 if (rtcp_debug_test_addr(&sin)) {
805 ast_verbose("\n\nGot RTCP from %s:%d\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
806 ast_verbose("PT: %d(%s)\n", pt, (pt == 200) ? "Sender Report" : (pt == 201) ? "Receiver Report" : (pt == 192) ? "H.261 FUR" : "Unknown");
807 ast_verbose("Reception reports: %d\n", rc);
808 ast_verbose("SSRC of sender: %u\n", rtcpheader[i + 1]);
811 i += 2; /* Advance past header and ssrc */
815 gettimeofday(&rtp->rtcp->rxlsr,NULL); /* To be able to populate the dlsr */
816 rtp->rtcp->spc = ntohl(rtcpheader[i+3]);
817 rtp->rtcp->soc = ntohl(rtcpheader[i + 4]);
818 rtp->rtcp->themrxlsr = ((ntohl(rtcpheader[i]) & 0x0000ffff) << 16) | ((ntohl(rtcpheader[i + 1]) & 0xffff) >> 16); /* Going to LSR in RR*/
820 if (rtcp_debug_test_addr(&sin)) {
821 ast_verbose("NTP timestamp: %lu.%010lu\n", (unsigned long) ntohl(rtcpheader[i]), (unsigned long) ntohl(rtcpheader[i + 1]) * 4096);
822 ast_verbose("RTP timestamp: %lu\n", (unsigned long) ntohl(rtcpheader[i + 2]));
823 ast_verbose("SPC: %lu\tSOC: %lu\n", (unsigned long) ntohl(rtcpheader[i + 3]), (unsigned long) ntohl(rtcpheader[i + 4]));
828 /* Intentional fall through */
830 /* This is the place to calculate RTT */
831 /* Don't handle multiple reception reports (rc > 1) yet */
832 gettimeofday(&now, NULL);
833 timeval2ntp(now, &msw, &lsw);
834 /* Use the one we sent them in our SR instead, rtcp->txlsr could have been rewritten if the dlsr is large */
835 if (ntohl(rtcpheader[i + 4])) { /* We must have the LSR */
836 comp = ((msw & 0xffff) << 16) | ((lsw & 0xffff0000) >> 16);
837 a = (double)((comp & 0xffff0000) >> 16) + (double)((double)(comp & 0xffff)/1000000.);
838 lsr = (double)((ntohl(rtcpheader[i + 4]) & 0xffff0000) >> 16) + (double)((double)(ntohl(rtcpheader[i + 4]) & 0xffff) / 1000000.);
839 dlsr = (double)(ntohl(rtcpheader[i + 5])/65536.);
840 rtt = a - dlsr - lsr;
841 rtp->rtcp->accumulated_transit += rtt;
842 rtp->rtcp->rtt = rtt;
843 if (rtp->rtcp->maxrtt<rtt)
844 rtp->rtcp->maxrtt = rtt;
845 if (rtp->rtcp->minrtt>rtt)
846 rtp->rtcp->minrtt = rtt;
848 rtp->rtcp->reported_jitter = ntohl(rtcpheader[i + 3]);
849 rtp->rtcp->reported_lost = ntohl(rtcpheader[i + 1]) & 0xffffff;
850 if (rtcp_debug_test_addr(&sin)) {
851 ast_verbose("Fraction lost: %ld\n", (((long) ntohl(rtcpheader[i + 1]) & 0xff000000) >> 24));
852 ast_verbose("Packets lost so far: %d\n", rtp->rtcp->reported_lost);
853 ast_verbose("Highest sequence number: %ld\n", (long) (ntohl(rtcpheader[i + 2]) & 0xffff));
854 ast_verbose("Sequence number cycles: %ld\n", (long) (ntohl(rtcpheader[i + 2]) & 0xffff) >> 16);
855 ast_verbose("Interarrival jitter: %u\n", rtp->rtcp->reported_jitter);
856 ast_verbose("Last SR(our NTP): %lu.%010lu\n",(unsigned long) ntohl(rtcpheader[i + 4]) >> 16,((unsigned long) ntohl(rtcpheader[i + 4]) << 16) * 4096);
857 ast_verbose("DLSR: %4.4f (sec)\n",ntohl(rtcpheader[i + 5])/65536.0);
859 ast_verbose("RTT: %f(sec)\n", rtt);
863 if (rtcp_debug_test_addr(&sin))
864 ast_verbose("Received an RTCP Fast Update Request\n");
865 rtp->f.frametype = AST_FRAME_CONTROL;
866 rtp->f.subclass = AST_CONTROL_VIDUPDATE;
874 if (rtcp_debug_test_addr(&sin))
875 ast_verbose("Received an SDES from %s:%d\n", ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
878 if (rtcp_debug_test_addr(&sin))
879 ast_verbose("Received a BYE from %s:%d\n", ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
882 ast_log(LOG_NOTICE, "Unknown RTCP packet (pt=%d) received from %s:%d\n", pt, ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
885 position += (length + 1);
891 static void calc_rxstamp(struct timeval *tv, struct ast_rtp *rtp, unsigned int timestamp, int mark)
900 if ((!rtp->rxcore.tv_sec && !rtp->rxcore.tv_usec) || mark) {
901 gettimeofday(&rtp->rxcore, NULL);
902 rtp->drxcore = (double) rtp->rxcore.tv_sec + (double) rtp->rxcore.tv_usec / 1000000;
903 /* map timestamp to a real time */
904 rtp->seedrxts = timestamp; /* Their RTP timestamp started with this */
905 rtp->rxcore.tv_sec -= timestamp / 8000;
906 rtp->rxcore.tv_usec -= (timestamp % 8000) * 125;
907 /* Round to 0.1ms for nice, pretty timestamps */
908 rtp->rxcore.tv_usec -= rtp->rxcore.tv_usec % 100;
909 if (rtp->rxcore.tv_usec < 0) {
910 /* Adjust appropriately if necessary */
911 rtp->rxcore.tv_usec += 1000000;
912 rtp->rxcore.tv_sec -= 1;
916 gettimeofday(&now,NULL);
917 /* rxcore is the mapping between the RTP timestamp and _our_ real time from gettimeofday() */
918 tv->tv_sec = rtp->rxcore.tv_sec + timestamp / 8000;
919 tv->tv_usec = rtp->rxcore.tv_usec + (timestamp % 8000) * 125;
920 if (tv->tv_usec >= 1000000) {
921 tv->tv_usec -= 1000000;
924 prog = (double)((timestamp-rtp->seedrxts)/8000.);
925 dtv = (double)rtp->drxcore + (double)(prog);
926 current_time = (double)now.tv_sec + (double)now.tv_usec/1000000;
927 transit = current_time - dtv;
928 d = transit - rtp->rxtransit;
929 rtp->rxtransit = transit;
932 rtp->rxjitter += (1./16.) * (d - rtp->rxjitter);
933 if (rtp->rxjitter > rtp->rtcp->maxrxjitter)
934 rtp->rtcp->maxrxjitter = rtp->rxjitter;
935 if (rtp->rxjitter < rtp->rtcp->minrxjitter)
936 rtp->rtcp->minrxjitter = rtp->rxjitter;
939 /*! \brief Perform a Packet2Packet RTCP write */
940 static int bridge_p2p_rtcp_write(struct ast_rtp *rtp, unsigned int *rtcpheader, int len)
942 struct ast_rtp *bridged = ast_rtp_get_bridged(rtp);
945 /* If RTCP is not present on the bridged RTP session, then ignore this */
949 /* Send the data out */
950 res = sendto(bridged->rtcp->s, (void *)rtcpheader, len, 0, (struct sockaddr *)&bridged->rtcp->them, sizeof(bridged->rtcp->them));
952 if (!bridged->nat || (bridged->nat && (ast_test_flag(bridged, FLAG_NAT_ACTIVE) == FLAG_NAT_ACTIVE)))
953 ast_log(LOG_DEBUG, "RTCP Transmission error of packet to %s:%d: %s\n", ast_inet_ntoa(bridged->rtcp->them.sin_addr), ntohs(bridged->rtcp->them.sin_port), strerror(errno));
954 else if ((((ast_test_flag(bridged, FLAG_NAT_ACTIVE) == FLAG_NAT_INACTIVE) || rtpdebug)) && (option_debug || rtpdebug))
955 ast_log(LOG_DEBUG, "RTCP NAT: Can't write RTCP to private address %s:%d, waiting for other end to send first...\n", ast_inet_ntoa(bridged->rtcp->them.sin_addr), ntohs(bridged->rtcp->them.sin_port));
956 } else if (rtp_debug_test_addr(&bridged->rtcp->them))
957 ast_verbose("Sent RTCP P2P packet to %s:%d (len %-6.6u)\n", ast_inet_ntoa(bridged->rtcp->them.sin_addr), ntohs(bridged->rtcp->them.sin_port), len);
962 /*! \brief Perform a Packet2Packet RTP write */
963 static int bridge_p2p_rtp_write(struct ast_rtp *rtp, unsigned int *rtpheader, int len, int hdrlen)
965 struct ast_rtp *bridged = ast_rtp_get_bridged(rtp);
966 int res = 0, payload = 0, bridged_payload = 0, version, padding, mark, ext;
967 struct rtpPayloadType rtpPT;
970 /* Get fields from packet */
971 seqno = ntohl(rtpheader[0]);
972 version = (seqno & 0xC0000000) >> 30;
973 payload = (seqno & 0x7f0000) >> 16;
974 padding = seqno & (1 << 29);
975 mark = seqno & (1 << 23);
976 ext = seqno & (1 << 28);
979 /* Check what the payload value should be */
980 rtpPT = ast_rtp_lookup_pt(rtp, payload);
982 /* If the payload is DTMF, and we are listening for DTMF - then feed it into the core */
983 if (ast_test_flag(rtp, FLAG_P2P_NEED_DTMF) && !rtpPT.isAstFormat && rtpPT.code == AST_RTP_DTMF)
986 /* Otherwise adjust bridged payload to match */
987 bridged_payload = ast_rtp_lookup_code(bridged, rtpPT.isAstFormat, rtpPT.code);
989 /* If the mark bit has not been sent yet... do it now */
990 if (!ast_test_flag(rtp, FLAG_P2P_SENT_MARK)) {
992 ast_set_flag(rtp, FLAG_P2P_SENT_MARK);
995 /* Reconstruct part of the packet */
996 rtpheader[0] = htonl((version << 30) | (mark << 23) | (bridged_payload << 16) | (seqno));
998 /* Send the packet back out */
999 res = sendto(bridged->s, (void *)rtpheader, len, 0, (struct sockaddr *)&bridged->them, sizeof(bridged->them));
1001 if (!bridged->nat || (bridged->nat && (ast_test_flag(bridged, FLAG_NAT_ACTIVE) == FLAG_NAT_ACTIVE))) {
1002 ast_log(LOG_DEBUG, "RTP Transmission error of packet to %s:%d: %s\n", ast_inet_ntoa(bridged->them.sin_addr), ntohs(bridged->them.sin_port), strerror(errno));
1003 } else if (((ast_test_flag(bridged, FLAG_NAT_ACTIVE) == FLAG_NAT_INACTIVE) || rtpdebug) && !ast_test_flag(bridged, FLAG_NAT_INACTIVE_NOWARN)) {
1004 if (option_debug || rtpdebug)
1005 ast_log(LOG_DEBUG, "RTP NAT: Can't write RTP to private address %s:%d, waiting for other end to send audio...\n", ast_inet_ntoa(bridged->them.sin_addr), ntohs(bridged->them.sin_port));
1006 ast_set_flag(bridged, FLAG_NAT_INACTIVE_NOWARN);
1009 } else if (rtp_debug_test_addr(&bridged->them))
1010 ast_verbose("Sent RTP P2P packet to %s:%d (type %-2.2d, len %-6.6u)\n", ast_inet_ntoa(bridged->them.sin_addr), ntohs(bridged->them.sin_port), bridged_payload, len - hdrlen);
1015 struct ast_frame *ast_rtp_read(struct ast_rtp *rtp)
1018 struct sockaddr_in sin;
1029 unsigned int timestamp;
1030 unsigned int *rtpheader;
1031 struct rtpPayloadType rtpPT;
1035 /* Cache where the header will go */
1036 res = recvfrom(rtp->s, rtp->rawdata + AST_FRIENDLY_OFFSET, sizeof(rtp->rawdata) - AST_FRIENDLY_OFFSET,
1037 0, (struct sockaddr *)&sin, &len);
1039 rtpheader = (unsigned int *)(rtp->rawdata + AST_FRIENDLY_OFFSET);
1041 if (errno != EAGAIN)
1042 ast_log(LOG_WARNING, "RTP Read error: %s\n", strerror(errno));
1045 return &ast_null_frame;
1049 ast_log(LOG_WARNING, "RTP Read too short\n");
1050 return &ast_null_frame;
1054 seqno = ntohl(rtpheader[0]);
1056 /* Check RTP version */
1057 version = (seqno & 0xC0000000) >> 30;
1059 if ((stun_handle_packet(rtp->s, &sin, rtp->rawdata + AST_FRIENDLY_OFFSET, res) == STUN_ACCEPT) &&
1060 (!rtp->them.sin_port && !rtp->them.sin_addr.s_addr)) {
1061 memcpy(&rtp->them, &sin, sizeof(rtp->them));
1063 return &ast_null_frame;
1066 /* If we don't have the other side's address, then ignore this */
1067 if (!rtp->them.sin_addr.s_addr || !rtp->them.sin_port)
1068 return &ast_null_frame;
1070 /* Send to whoever send to us if NAT is turned on */
1072 if ((rtp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
1073 (rtp->them.sin_port != sin.sin_port)) {
1076 memcpy(&rtp->rtcp->them, &sin, sizeof(rtp->rtcp->them));
1077 rtp->rtcp->them.sin_port = htons(ntohs(rtp->them.sin_port)+1);
1080 ast_set_flag(rtp, FLAG_NAT_ACTIVE);
1081 if (option_debug || rtpdebug)
1082 ast_log(LOG_DEBUG, "RTP NAT: Got audio from other end. Now sending to address %s:%d\n", ast_inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port));
1086 /* If we are bridged to another RTP stream, send direct */
1087 if (ast_rtp_get_bridged(rtp) && !bridge_p2p_rtp_write(rtp, rtpheader, res, hdrlen))
1088 return &ast_null_frame;
1091 return &ast_null_frame;
1093 payloadtype = (seqno & 0x7f0000) >> 16;
1094 padding = seqno & (1 << 29);
1095 mark = seqno & (1 << 23);
1096 ext = seqno & (1 << 28);
1098 timestamp = ntohl(rtpheader[1]);
1099 ssrc = ntohl(rtpheader[2]);
1101 if (!mark && rtp->rxssrc && rtp->rxssrc != ssrc) {
1102 if (option_debug || rtpdebug)
1103 ast_log(LOG_DEBUG, "Forcing Marker bit, because SSRC has changed\n");
1110 /* Remove padding bytes */
1111 res -= rtp->rawdata[AST_FRIENDLY_OFFSET + res - 1];
1115 /* RTP Extension present */
1117 hdrlen += (ntohl(rtpheader[3]) & 0xffff) << 2;
1121 ast_log(LOG_WARNING, "RTP Read too short (%d, expecting %d)\n", res, hdrlen);
1122 return &ast_null_frame;
1125 rtp->rxcount++; /* Only count reasonably valid packets, this'll make the rtcp stats more accurate */
1127 tseqno = rtp->lastrxseqno +1;
1129 if (rtp->rxcount==1) {
1130 /* This is the first RTP packet successfully received from source */
1131 rtp->seedrxseqno = seqno;
1134 if (rtp->rtcp && rtp->rtcp->schedid < 1) {
1135 /* Schedule transmission of Receiver Report */
1136 rtp->rtcp->schedid = ast_sched_add(rtp->sched, ast_rtcp_calc_interval(rtp), ast_rtcp_write, rtp);
1139 if (tseqno > RTP_SEQ_MOD) { /* if tseqno is greater than RTP_SEQ_MOD it would indicate that the sender cycled */
1140 rtp->cycles += RTP_SEQ_MOD;
1141 ast_verbose("SEQNO cycled: %u\t%d\n", rtp->cycles, seqno);
1144 rtp->lastrxseqno = seqno;
1146 if (rtp->themssrc==0)
1147 rtp->themssrc = ntohl(rtpheader[2]); /* Record their SSRC to put in future RR */
1149 if (rtp_debug_test_addr(&sin))
1150 ast_verbose("Got RTP packet from %s:%d (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
1151 ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), payloadtype, seqno, timestamp,res - hdrlen);
1153 rtpPT = ast_rtp_lookup_pt(rtp, payloadtype);
1154 if (!rtpPT.isAstFormat) {
1155 struct ast_frame *f = NULL;
1157 /* This is special in-band data that's not one of our codecs */
1158 if (rtpPT.code == AST_RTP_DTMF) {
1159 /* It's special -- rfc2833 process it */
1160 if (rtp_debug_test_addr(&sin)) {
1161 unsigned char *data;
1163 unsigned int event_end;
1164 unsigned int duration;
1165 data = rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen;
1166 event = ntohl(*((unsigned int *)(data)));
1168 event_end = ntohl(*((unsigned int *)(data)));
1171 duration = ntohl(*((unsigned int *)(data)));
1173 ast_verbose("Got RTP RFC2833 from %s:%d (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u, mark %d, event %08x, end %d, duration %-5.5d) \n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), payloadtype, seqno, timestamp, res - hdrlen, (mark?1:0), event, ((event_end & 0x80)?1:0), duration);
1175 if (rtp->lasteventseqn <= seqno || rtp->resp == 0 || (rtp->lasteventseqn >= 65530 && seqno <= 6)) {
1176 f = process_rfc2833(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen, seqno);
1177 rtp->lasteventseqn = seqno;
1179 } else if (rtpPT.code == AST_RTP_CISCO_DTMF) {
1180 /* It's really special -- process it the Cisco way */
1181 if (rtp->lasteventseqn <= seqno || rtp->resp == 0 || (rtp->lasteventseqn >= 65530 && seqno <= 6)) {
1182 f = process_cisco_dtmf(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
1183 rtp->lasteventseqn = seqno;
1185 } else if (rtpPT.code == AST_RTP_CN) {
1187 f = process_rfc3389(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
1189 ast_log(LOG_NOTICE, "Unknown RTP codec %d received from '%s'\n", payloadtype, ast_inet_ntoa(rtp->them.sin_addr));
1191 return f ? f : &ast_null_frame;
1193 rtp->lastrxformat = rtp->f.subclass = rtpPT.code;
1194 rtp->f.frametype = (rtp->f.subclass < AST_FORMAT_MAX_AUDIO) ? AST_FRAME_VOICE : AST_FRAME_VIDEO;
1197 rtp->lastrxts = timestamp;
1199 rtp->rxseqno = seqno;
1201 if (rtp->dtmfcount) {
1203 printf("dtmfcount was %d\n", rtp->dtmfcount);
1205 rtp->dtmfcount -= (timestamp - rtp->lastrxts);
1206 if (rtp->dtmfcount < 0)
1209 if (dtmftimeout != rtp->dtmfcount)
1210 printf("dtmfcount is %d\n", rtp->dtmfcount);
1213 rtp->lastrxts = timestamp;
1215 /* Send any pending DTMF */
1216 if (rtp->resp && !rtp->dtmfcount) {
1218 ast_log(LOG_DEBUG, "Sending pending DTMF\n");
1219 return send_dtmf(rtp);
1222 rtp->f.datalen = res - hdrlen;
1223 rtp->f.data = rtp->rawdata + hdrlen + AST_FRIENDLY_OFFSET;
1224 rtp->f.offset = hdrlen + AST_FRIENDLY_OFFSET;
1225 if (rtp->f.subclass < AST_FORMAT_MAX_AUDIO) {
1226 rtp->f.samples = ast_codec_get_samples(&rtp->f);
1227 if (rtp->f.subclass == AST_FORMAT_SLINEAR)
1228 ast_frame_byteswap_be(&rtp->f);
1229 calc_rxstamp(&rtp->f.delivery, rtp, timestamp, mark);
1230 /* Add timing data to let ast_generic_bridge() put the frame into a jitterbuf */
1231 rtp->f.has_timing_info = 1;
1232 rtp->f.ts = timestamp / 8;
1233 rtp->f.len = rtp->f.samples / 8;
1234 rtp->f.seqno = seqno;
1236 /* Video -- samples is # of samples vs. 90000 */
1237 if (!rtp->lastividtimestamp)
1238 rtp->lastividtimestamp = timestamp;
1239 rtp->f.samples = timestamp - rtp->lastividtimestamp;
1240 rtp->lastividtimestamp = timestamp;
1241 rtp->f.delivery.tv_sec = 0;
1242 rtp->f.delivery.tv_usec = 0;
1244 rtp->f.subclass |= 0x1;
1251 /* The following array defines the MIME Media type (and subtype) for each
1252 of our codecs, or RTP-specific data type. */
1254 struct rtpPayloadType payloadType;
1258 {{1, AST_FORMAT_G723_1}, "audio", "G723"},
1259 {{1, AST_FORMAT_GSM}, "audio", "GSM"},
1260 {{1, AST_FORMAT_ULAW}, "audio", "PCMU"},
1261 {{1, AST_FORMAT_ALAW}, "audio", "PCMA"},
1262 {{1, AST_FORMAT_G726}, "audio", "G726-32"},
1263 {{1, AST_FORMAT_ADPCM}, "audio", "DVI4"},
1264 {{1, AST_FORMAT_SLINEAR}, "audio", "L16"},
1265 {{1, AST_FORMAT_LPC10}, "audio", "LPC"},
1266 {{1, AST_FORMAT_G729A}, "audio", "G729"},
1267 {{1, AST_FORMAT_SPEEX}, "audio", "speex"},
1268 {{1, AST_FORMAT_ILBC}, "audio", "iLBC"},
1269 {{1, AST_FORMAT_G726_AAL2}, "audio", "AAL2-G726-32"},
1270 {{0, AST_RTP_DTMF}, "audio", "telephone-event"},
1271 {{0, AST_RTP_CISCO_DTMF}, "audio", "cisco-telephone-event"},
1272 {{0, AST_RTP_CN}, "audio", "CN"},
1273 {{1, AST_FORMAT_JPEG}, "video", "JPEG"},
1274 {{1, AST_FORMAT_PNG}, "video", "PNG"},
1275 {{1, AST_FORMAT_H261}, "video", "H261"},
1276 {{1, AST_FORMAT_H263}, "video", "H263"},
1277 {{1, AST_FORMAT_H263_PLUS}, "video", "h263-1998"},
1278 {{1, AST_FORMAT_H264}, "video", "H264"},
1281 /* Static (i.e., well-known) RTP payload types for our "AST_FORMAT..."s:
1282 also, our own choices for dynamic payload types. This is our master
1283 table for transmission */
1284 static struct rtpPayloadType static_RTP_PT[MAX_RTP_PT] = {
1285 [0] = {1, AST_FORMAT_ULAW},
1286 #ifdef USE_DEPRECATED_G726
1287 [2] = {1, AST_FORMAT_G726}, /* Technically this is G.721, but if Cisco can do it, so can we... */
1289 [3] = {1, AST_FORMAT_GSM},
1290 [4] = {1, AST_FORMAT_G723_1},
1291 [5] = {1, AST_FORMAT_ADPCM}, /* 8 kHz */
1292 [6] = {1, AST_FORMAT_ADPCM}, /* 16 kHz */
1293 [7] = {1, AST_FORMAT_LPC10},
1294 [8] = {1, AST_FORMAT_ALAW},
1295 [10] = {1, AST_FORMAT_SLINEAR}, /* 2 channels */
1296 [11] = {1, AST_FORMAT_SLINEAR}, /* 1 channel */
1297 [13] = {0, AST_RTP_CN},
1298 [16] = {1, AST_FORMAT_ADPCM}, /* 11.025 kHz */
1299 [17] = {1, AST_FORMAT_ADPCM}, /* 22.050 kHz */
1300 [18] = {1, AST_FORMAT_G729A},
1301 [19] = {0, AST_RTP_CN}, /* Also used for CN */
1302 [26] = {1, AST_FORMAT_JPEG},
1303 [31] = {1, AST_FORMAT_H261},
1304 [34] = {1, AST_FORMAT_H263},
1305 [103] = {1, AST_FORMAT_H263_PLUS},
1306 [97] = {1, AST_FORMAT_ILBC},
1307 [99] = {1, AST_FORMAT_H264},
1308 [101] = {0, AST_RTP_DTMF},
1309 [110] = {1, AST_FORMAT_SPEEX},
1310 [111] = {1, AST_FORMAT_G726},
1311 [112] = {1, AST_FORMAT_G726_AAL2},
1312 [121] = {0, AST_RTP_CISCO_DTMF}, /* Must be type 121 */
1315 void ast_rtp_pt_clear(struct ast_rtp* rtp)
1321 for (i = 0; i < MAX_RTP_PT; ++i) {
1322 rtp->current_RTP_PT[i].isAstFormat = 0;
1323 rtp->current_RTP_PT[i].code = 0;
1326 rtp->rtp_lookup_code_cache_isAstFormat = 0;
1327 rtp->rtp_lookup_code_cache_code = 0;
1328 rtp->rtp_lookup_code_cache_result = 0;
1331 void ast_rtp_pt_default(struct ast_rtp* rtp)
1335 /* Initialize to default payload types */
1336 for (i = 0; i < MAX_RTP_PT; ++i) {
1337 rtp->current_RTP_PT[i].isAstFormat = static_RTP_PT[i].isAstFormat;
1338 rtp->current_RTP_PT[i].code = static_RTP_PT[i].code;
1341 rtp->rtp_lookup_code_cache_isAstFormat = 0;
1342 rtp->rtp_lookup_code_cache_code = 0;
1343 rtp->rtp_lookup_code_cache_result = 0;
1346 void ast_rtp_pt_copy(struct ast_rtp *dest, const struct ast_rtp *src)
1350 for (i=0; i < MAX_RTP_PT; ++i) {
1351 dest->current_RTP_PT[i].isAstFormat =
1352 src->current_RTP_PT[i].isAstFormat;
1353 dest->current_RTP_PT[i].code =
1354 src->current_RTP_PT[i].code;
1356 dest->rtp_lookup_code_cache_isAstFormat = 0;
1357 dest->rtp_lookup_code_cache_code = 0;
1358 dest->rtp_lookup_code_cache_result = 0;
1361 /*! \brief Get channel driver interface structure */
1362 static struct ast_rtp_protocol *get_proto(struct ast_channel *chan)
1364 struct ast_rtp_protocol *cur = NULL;
1366 AST_LIST_LOCK(&protos);
1367 AST_LIST_TRAVERSE(&protos, cur, list) {
1368 if (cur->type == chan->tech->type)
1371 AST_LIST_UNLOCK(&protos);
1376 int ast_rtp_early_bridge(struct ast_channel *dest, struct ast_channel *src)
1378 struct ast_rtp *destp = NULL, *srcp = NULL; /* Audio RTP Channels */
1379 struct ast_rtp *vdestp = NULL, *vsrcp = NULL; /* Video RTP channels */
1380 struct ast_rtp_protocol *destpr = NULL, *srcpr = NULL;
1381 enum ast_rtp_get_result audio_dest_res = AST_RTP_GET_FAILED, video_dest_res = AST_RTP_GET_FAILED;
1382 enum ast_rtp_get_result audio_src_res = AST_RTP_GET_FAILED, video_src_res = AST_RTP_GET_FAILED;
1386 ast_channel_lock(dest);
1388 while(ast_channel_trylock(src)) {
1389 ast_channel_unlock(dest);
1391 ast_channel_lock(dest);
1395 /* Find channel driver interfaces */
1396 destpr = get_proto(dest);
1398 srcpr = get_proto(src);
1401 ast_log(LOG_DEBUG, "Channel '%s' has no RTP, not doing anything\n", dest->name);
1402 ast_channel_unlock(dest);
1404 ast_channel_unlock(src);
1409 ast_log(LOG_DEBUG, "Channel '%s' has no RTP, not doing anything\n", src ? src->name : "<unspecified>");
1410 ast_channel_unlock(dest);
1412 ast_channel_unlock(src);
1416 /* Get audio and video interface (if native bridge is possible) */
1417 audio_dest_res = destpr->get_rtp_info(dest, &destp);
1418 video_dest_res = destpr->get_vrtp_info ? destpr->get_vrtp_info(dest, &vdestp) : AST_RTP_GET_FAILED;
1420 audio_src_res = srcpr->get_rtp_info(src, &srcp);
1421 video_src_res = srcpr->get_vrtp_info ? srcpr->get_vrtp_info(src, &vsrcp) : AST_RTP_GET_FAILED;
1424 /* Check if bridge is still possible (In SIP canreinvite=no stops this, like NAT) */
1425 if (audio_dest_res != AST_RTP_TRY_NATIVE) {
1426 /* Somebody doesn't want to play... */
1427 ast_channel_unlock(dest);
1429 ast_channel_unlock(src);
1432 if (audio_src_res == AST_RTP_TRY_NATIVE && srcpr->get_codec)
1433 srccodec = srcpr->get_codec(src);
1436 /* Consider empty media as non-existant */
1437 if (audio_src_res == AST_RTP_TRY_NATIVE && !srcp->them.sin_addr.s_addr)
1439 /* Bridge media early */
1440 if (destpr->set_rtp_peer(dest, srcp, vsrcp, srccodec, srcp ? ast_test_flag(srcp, FLAG_NAT_ACTIVE) : 0))
1441 ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", dest->name, src ? src->name : "<unspecified>");
1442 ast_channel_unlock(dest);
1444 ast_channel_unlock(src);
1446 ast_log(LOG_DEBUG, "Setting early bridge SDP of '%s' with that of '%s'\n", dest->name, src ? src->name : "<unspecified>");
1450 int ast_rtp_make_compatible(struct ast_channel *dest, struct ast_channel *src, int media)
1452 struct ast_rtp *destp = NULL, *srcp = NULL; /* Audio RTP Channels */
1453 struct ast_rtp *vdestp = NULL, *vsrcp = NULL; /* Video RTP channels */
1454 struct ast_rtp_protocol *destpr = NULL, *srcpr = NULL;
1455 enum ast_rtp_get_result audio_dest_res = AST_RTP_GET_FAILED, video_dest_res = AST_RTP_GET_FAILED;
1456 enum ast_rtp_get_result audio_src_res = AST_RTP_GET_FAILED, video_src_res = AST_RTP_GET_FAILED;
1460 ast_channel_lock(dest);
1461 while(ast_channel_trylock(src)) {
1462 ast_channel_unlock(dest);
1464 ast_channel_lock(dest);
1467 /* Find channel driver interfaces */
1468 if (!(destpr = get_proto(dest))) {
1470 ast_log(LOG_DEBUG, "Channel '%s' has no RTP, not doing anything\n", dest->name);
1471 ast_channel_unlock(dest);
1472 ast_channel_unlock(src);
1475 if (!(srcpr = get_proto(src))) {
1477 ast_log(LOG_DEBUG, "Channel '%s' has no RTP, not doing anything\n", src->name);
1478 ast_channel_unlock(dest);
1479 ast_channel_unlock(src);
1483 /* Get audio and video interface (if native bridge is possible) */
1484 audio_dest_res = destpr->get_rtp_info(dest, &destp);
1485 video_dest_res = destpr->get_vrtp_info ? destpr->get_vrtp_info(dest, &vdestp) : AST_RTP_GET_FAILED;
1486 audio_src_res = srcpr->get_rtp_info(src, &srcp);
1487 video_src_res = srcpr->get_vrtp_info ? srcpr->get_vrtp_info(src, &vsrcp) : AST_RTP_GET_FAILED;
1489 /* Check if bridge is still possible (In SIP canreinvite=no stops this, like NAT) */
1490 if (audio_dest_res != AST_RTP_TRY_NATIVE || audio_src_res != AST_RTP_TRY_NATIVE) {
1491 /* Somebody doesn't want to play... */
1492 ast_channel_unlock(dest);
1493 ast_channel_unlock(src);
1496 ast_rtp_pt_copy(destp, srcp);
1497 if (vdestp && vsrcp)
1498 ast_rtp_pt_copy(vdestp, vsrcp);
1499 if (srcpr->get_codec)
1500 srccodec = srcpr->get_codec(src);
1505 if (destpr->set_rtp_peer(dest, srcp, vsrcp, srccodec, ast_test_flag(srcp, FLAG_NAT_ACTIVE)))
1506 ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", dest->name, src->name);
1508 ast_channel_unlock(dest);
1509 ast_channel_unlock(src);
1511 ast_log(LOG_DEBUG, "Seeded SDP of '%s' with that of '%s'\n", dest->name, src->name);
1515 /*! \brief Make a note of a RTP payload type that was seen in a SDP "m=" line.
1516 * By default, use the well-known value for this type (although it may
1517 * still be set to a different value by a subsequent "a=rtpmap:" line)
1519 void ast_rtp_set_m_type(struct ast_rtp* rtp, int pt)
1521 if (pt < 0 || pt > MAX_RTP_PT)
1522 return; /* bogus payload type */
1524 if (static_RTP_PT[pt].code != 0)
1525 rtp->current_RTP_PT[pt] = static_RTP_PT[pt];
1528 /*! \brief Make a note of a RTP payload type (with MIME type) that was seen in
1529 * an SDP "a=rtpmap:" line.
1531 void ast_rtp_set_rtpmap_type(struct ast_rtp *rtp, int pt,
1532 char *mimeType, char *mimeSubtype,
1533 enum ast_rtp_options options)
1537 if (pt < 0 || pt > MAX_RTP_PT)
1538 return; /* bogus payload type */
1540 for (i = 0; i < sizeof(mimeTypes)/sizeof(mimeTypes[0]); ++i) {
1541 if (strcasecmp(mimeSubtype, mimeTypes[i].subtype) == 0 &&
1542 strcasecmp(mimeType, mimeTypes[i].type) == 0) {
1543 rtp->current_RTP_PT[pt] = mimeTypes[i].payloadType;
1544 if ((mimeTypes[i].payloadType.code == AST_FORMAT_G726) &&
1545 mimeTypes[i].payloadType.isAstFormat &&
1546 (options & AST_RTP_OPT_G726_NONSTANDARD))
1547 rtp->current_RTP_PT[pt].code = AST_FORMAT_G726_AAL2;
1553 /*! \brief Return the union of all of the codecs that were set by rtp_set...() calls
1554 * They're returned as two distinct sets: AST_FORMATs, and AST_RTPs */
1555 void ast_rtp_get_current_formats(struct ast_rtp* rtp,
1556 int* astFormats, int* nonAstFormats) {
1559 *astFormats = *nonAstFormats = 0;
1560 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
1561 if (rtp->current_RTP_PT[pt].isAstFormat) {
1562 *astFormats |= rtp->current_RTP_PT[pt].code;
1564 *nonAstFormats |= rtp->current_RTP_PT[pt].code;
1569 struct rtpPayloadType ast_rtp_lookup_pt(struct ast_rtp* rtp, int pt)
1571 struct rtpPayloadType result;
1573 result.isAstFormat = result.code = 0;
1574 if (pt < 0 || pt > MAX_RTP_PT)
1575 return result; /* bogus payload type */
1577 /* Start with negotiated codecs */
1578 result = rtp->current_RTP_PT[pt];
1580 /* If it doesn't exist, check our static RTP type list, just in case */
1582 result = static_RTP_PT[pt];
1586 /*! \brief Looks up an RTP code out of our *static* outbound list */
1587 int ast_rtp_lookup_code(struct ast_rtp* rtp, const int isAstFormat, const int code) {
1591 if (isAstFormat == rtp->rtp_lookup_code_cache_isAstFormat &&
1592 code == rtp->rtp_lookup_code_cache_code) {
1594 /* Use our cached mapping, to avoid the overhead of the loop below */
1595 return rtp->rtp_lookup_code_cache_result;
1598 /* Check the dynamic list first */
1599 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
1600 if (rtp->current_RTP_PT[pt].code == code && rtp->current_RTP_PT[pt].isAstFormat == isAstFormat) {
1601 rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
1602 rtp->rtp_lookup_code_cache_code = code;
1603 rtp->rtp_lookup_code_cache_result = pt;
1608 /* Then the static list */
1609 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
1610 if (static_RTP_PT[pt].code == code && static_RTP_PT[pt].isAstFormat == isAstFormat) {
1611 rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
1612 rtp->rtp_lookup_code_cache_code = code;
1613 rtp->rtp_lookup_code_cache_result = pt;
1620 const char *ast_rtp_lookup_mime_subtype(const int isAstFormat, const int code,
1621 enum ast_rtp_options options)
1625 for (i = 0; i < sizeof(mimeTypes)/sizeof(mimeTypes[0]); ++i) {
1626 if ((mimeTypes[i].payloadType.code == code) && (mimeTypes[i].payloadType.isAstFormat == isAstFormat)) {
1628 (code == AST_FORMAT_G726_AAL2) &&
1629 (options & AST_RTP_OPT_G726_NONSTANDARD))
1630 return "AAL2-G726-32";
1632 return mimeTypes[i].subtype;
1639 char *ast_rtp_lookup_mime_multiple(char *buf, size_t size, const int capability,
1640 const int isAstFormat, enum ast_rtp_options options)
1650 snprintf(end, size, "0x%x (", capability);
1657 for (format = 1; format < AST_RTP_MAX; format <<= 1) {
1658 if (capability & format) {
1659 const char *name = ast_rtp_lookup_mime_subtype(isAstFormat, format, options);
1661 snprintf(end, size, "%s|", name);
1669 snprintf(start, size, "nothing)");
1676 static int rtp_socket(void)
1680 s = socket(AF_INET, SOCK_DGRAM, 0);
1682 flags = fcntl(s, F_GETFL);
1683 fcntl(s, F_SETFL, flags | O_NONBLOCK);
1686 setsockopt(s, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
1693 * \brief Initialize a new RTCP session.
1695 * \returns The newly initialized RTCP session.
1697 static struct ast_rtcp *ast_rtcp_new(void)
1699 struct ast_rtcp *rtcp;
1701 if (!(rtcp = ast_calloc(1, sizeof(*rtcp))))
1703 rtcp->s = rtp_socket();
1704 rtcp->us.sin_family = AF_INET;
1705 rtcp->them.sin_family = AF_INET;
1709 ast_log(LOG_WARNING, "Unable to allocate RTCP socket: %s\n", strerror(errno));
1716 struct ast_rtp *ast_rtp_new_with_bindaddr(struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode, struct in_addr addr)
1718 struct ast_rtp *rtp;
1723 if (!(rtp = ast_calloc(1, sizeof(*rtp))))
1725 rtp->them.sin_family = AF_INET;
1726 rtp->us.sin_family = AF_INET;
1727 rtp->s = rtp_socket();
1728 rtp->ssrc = ast_random();
1729 rtp->seqno = ast_random() & 0xffff;
1730 ast_set_flag(rtp, FLAG_HAS_DTMF);
1733 ast_log(LOG_ERROR, "Unable to allocate socket: %s\n", strerror(errno));
1736 if (sched && rtcpenable) {
1738 rtp->rtcp = ast_rtcp_new();
1741 /* Select a random port number in the range of possible RTP */
1742 x = (ast_random() % (rtpend-rtpstart)) + rtpstart;
1744 /* Save it for future references. */
1746 /* Iterate tring to bind that port and incrementing it otherwise untill a port was found or no ports are available. */
1748 /* Must be an even port number by RTP spec */
1749 rtp->us.sin_port = htons(x);
1750 rtp->us.sin_addr = addr;
1751 /* If there's rtcp, initialize it as well. */
1753 rtp->rtcp->us.sin_port = htons(x + 1);
1754 /* Try to bind it/them. */
1755 if (!(first = bind(rtp->s, (struct sockaddr *)&rtp->us, sizeof(rtp->us))) &&
1756 (!rtp->rtcp || !bind(rtp->rtcp->s, (struct sockaddr *)&rtp->rtcp->us, sizeof(rtp->rtcp->us))))
1759 /* Primary bind succeeded! Gotta recreate it */
1761 rtp->s = rtp_socket();
1763 if (errno != EADDRINUSE) {
1764 /* We got an error that wasn't expected, abort! */
1765 ast_log(LOG_ERROR, "Unexpected bind error: %s\n", strerror(errno));
1768 close(rtp->rtcp->s);
1774 /* The port was used, increment it (by two). */
1776 /* Did we go over the limit ? */
1778 /* then, start from the begingig. */
1779 x = (rtpstart + 1) & ~1;
1780 /* Check if we reached the place were we started. */
1781 if (x == startplace) {
1782 /* If so, there's no ports available. */
1783 ast_log(LOG_ERROR, "No RTP ports remaining. Can't setup media stream for this call.\n");
1786 close(rtp->rtcp->s);
1796 rtp->ioid = ast_io_add(rtp->io, rtp->s, rtpread, AST_IO_IN, rtp);
1797 ast_set_flag(rtp, FLAG_CALLBACK_MODE);
1799 ast_rtp_pt_default(rtp);
1803 struct ast_rtp *ast_rtp_new(struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode)
1807 memset(&ia, 0, sizeof(ia));
1808 return ast_rtp_new_with_bindaddr(sched, io, rtcpenable, callbackmode, ia);
1811 int ast_rtp_settos(struct ast_rtp *rtp, int tos)
1815 if ((res = setsockopt(rtp->s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos))))
1816 ast_log(LOG_WARNING, "Unable to set TOS to %d\n", tos);
1820 void ast_rtp_set_peer(struct ast_rtp *rtp, struct sockaddr_in *them)
1822 rtp->them.sin_port = them->sin_port;
1823 rtp->them.sin_addr = them->sin_addr;
1825 rtp->rtcp->them.sin_port = htons(ntohs(them->sin_port) + 1);
1826 rtp->rtcp->them.sin_addr = them->sin_addr;
1831 int ast_rtp_get_peer(struct ast_rtp *rtp, struct sockaddr_in *them)
1833 if ((them->sin_family != AF_INET) ||
1834 (them->sin_port != rtp->them.sin_port) ||
1835 (them->sin_addr.s_addr != rtp->them.sin_addr.s_addr)) {
1836 them->sin_family = AF_INET;
1837 them->sin_port = rtp->them.sin_port;
1838 them->sin_addr = rtp->them.sin_addr;
1844 void ast_rtp_get_us(struct ast_rtp *rtp, struct sockaddr_in *us)
1849 struct ast_rtp *ast_rtp_get_bridged(struct ast_rtp *rtp)
1851 return rtp->bridged;
1854 void ast_rtp_stop(struct ast_rtp *rtp)
1856 if (rtp->rtcp && rtp->rtcp->schedid > 0) {
1857 ast_sched_del(rtp->sched, rtp->rtcp->schedid);
1858 rtp->rtcp->schedid = -1;
1861 memset(&rtp->them.sin_addr, 0, sizeof(rtp->them.sin_addr));
1862 memset(&rtp->them.sin_port, 0, sizeof(rtp->them.sin_port));
1864 memset(&rtp->rtcp->them.sin_addr, 0, sizeof(rtp->rtcp->them.sin_addr));
1865 memset(&rtp->rtcp->them.sin_port, 0, sizeof(rtp->rtcp->them.sin_port));
1868 ast_clear_flag(rtp, FLAG_P2P_SENT_MARK);
1871 void ast_rtp_reset(struct ast_rtp *rtp)
1873 memset(&rtp->rxcore, 0, sizeof(rtp->rxcore));
1874 memset(&rtp->txcore, 0, sizeof(rtp->txcore));
1875 memset(&rtp->dtmfmute, 0, sizeof(rtp->dtmfmute));
1877 rtp->lastdigitts = 0;
1879 rtp->lastividtimestamp = 0;
1880 rtp->lastovidtimestamp = 0;
1881 rtp->lasteventseqn = 0;
1882 rtp->lasteventendseqn = 0;
1883 rtp->lasttxformat = 0;
1884 rtp->lastrxformat = 0;
1886 rtp->dtmfduration = 0;
1891 char *ast_rtp_get_quality(struct ast_rtp *rtp)
1895 *themssrc their ssrc
1897 *rxjitter our calculated jitter(rx)
1898 *rxcount no. received packets
1899 *txjitter reported jitter of the other end
1900 *txcount transmitted packets
1901 *rlp remote lost packets
1904 snprintf(rtp->rtcp->quality, sizeof(rtp->rtcp->quality), "ssrc=%u;themssrc=%u;lp=%u;rxjitter=%f;rxcount=%u;txjitter=%f;txcount=%u;rlp=%u;rtt=%f", rtp->ssrc, rtp->themssrc, rtp->rtcp->expected_prior - rtp->rtcp->received_prior, rtp->rxjitter, rtp->rxcount, (double)rtp->rtcp->reported_jitter/65536., rtp->txcount, rtp->rtcp->reported_lost, rtp->rtcp->rtt);
1906 return rtp->rtcp->quality;
1909 void ast_rtp_destroy(struct ast_rtp *rtp)
1911 if (rtcp_debug_test_addr(&rtp->them) || rtcpstats) {
1912 /*Print some info on the call here */
1913 ast_verbose(" RTP-stats\n");
1914 ast_verbose("* Our Receiver:\n");
1915 ast_verbose(" SSRC: %u\n", rtp->themssrc);
1916 ast_verbose(" Received packets: %u\n", rtp->rxcount);
1917 ast_verbose(" Lost packets: %u\n", rtp->rtcp->expected_prior - rtp->rtcp->received_prior);
1918 ast_verbose(" Jitter: %.4f\n", rtp->rxjitter);
1919 ast_verbose(" Transit: %.4f\n", rtp->rxtransit);
1920 ast_verbose(" RR-count: %u\n", rtp->rtcp->rr_count);
1921 ast_verbose("* Our Sender:\n");
1922 ast_verbose(" SSRC: %u\n", rtp->ssrc);
1923 ast_verbose(" Sent packets: %u\n", rtp->txcount);
1924 ast_verbose(" Lost packets: %u\n", rtp->rtcp->reported_lost);
1925 ast_verbose(" Jitter: %u\n", rtp->rtcp->reported_jitter);
1926 ast_verbose(" SR-count: %u\n", rtp->rtcp->sr_count);
1927 ast_verbose(" RTT: %f\n", rtp->rtcp->rtt);
1931 ast_smoother_free(rtp->smoother);
1933 ast_io_remove(rtp->io, rtp->ioid);
1937 if (rtp->rtcp->schedid > 0)
1938 ast_sched_del(rtp->sched, rtp->rtcp->schedid);
1939 close(rtp->rtcp->s);
1946 static unsigned int calc_txstamp(struct ast_rtp *rtp, struct timeval *delivery)
1950 if (ast_tvzero(rtp->txcore)) {
1951 rtp->txcore = ast_tvnow();
1952 /* Round to 20ms for nice, pretty timestamps */
1953 rtp->txcore.tv_usec -= rtp->txcore.tv_usec % 20000;
1955 /* Use previous txcore if available */
1956 t = (delivery && !ast_tvzero(*delivery)) ? *delivery : ast_tvnow();
1957 ms = ast_tvdiff_ms(t, rtp->txcore);
1960 /* Use what we just got for next time */
1962 return (unsigned int) ms;
1965 int ast_rtp_senddigit(struct ast_rtp *rtp, char digit)
1967 unsigned int *rtpheader;
1974 if ((digit <= '9') && (digit >= '0'))
1976 else if (digit == '*')
1978 else if (digit == '#')
1980 else if ((digit >= 'A') && (digit <= 'D'))
1981 digit = digit - 'A' + 12;
1982 else if ((digit >= 'a') && (digit <= 'd'))
1983 digit = digit - 'a' + 12;
1985 ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
1988 payload = ast_rtp_lookup_code(rtp, 0, AST_RTP_DTMF);
1990 /* If we have no peer, return immediately */
1991 if (!rtp->them.sin_addr.s_addr)
1994 rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
1996 /* Get a pointer to the header */
1997 rtpheader = (unsigned int *)data;
1998 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno));
1999 rtpheader[1] = htonl(rtp->lastdigitts);
2000 rtpheader[2] = htonl(rtp->ssrc);
2001 rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (0));
2002 for (x = 0; x < 6; x++) {
2003 if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
2004 res = sendto(rtp->s, (void *) rtpheader, hdrlen + 4, 0, (struct sockaddr *) &rtp->them, sizeof(rtp->them));
2006 ast_log(LOG_ERROR, "RTP Transmission error to %s:%d: %s\n",
2007 ast_inet_ntoa(rtp->them.sin_addr),
2008 ntohs(rtp->them.sin_port), strerror(errno));
2009 if (rtp_debug_test_addr(&rtp->them))
2010 ast_verbose("Sent RTP DTMF packet to %s:%d (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
2011 ast_inet_ntoa(rtp->them.sin_addr),
2012 ntohs(rtp->them.sin_port), payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
2014 /* Sequence number of last two end packets does not get incremented */
2017 /* Clear marker bit and set seqno */
2018 rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno));
2019 /* For the last three packets, set the duration and the end bit */
2022 /* No, this is wrong... Do not increment lastdigitts, that's not according
2023 to the RFC, as best we can determine */
2024 rtp->lastdigitts++; /* or else the SPA3000 will click instead of beeping... */
2025 rtpheader[1] = htonl(rtp->lastdigitts);
2027 /* Make duration 800 (100ms) */
2028 rtpheader[3] |= htonl((800));
2029 /* Set the End bit */
2030 rtpheader[3] |= htonl((1 << 23));
2033 /*! \note Increment the digit timestamp by 120ms, to ensure that digits
2034 sent sequentially with no intervening non-digit packets do not
2035 get sent with the same timestamp, and that sequential digits
2036 have some 'dead air' in between them
2038 rtp->lastdigitts += 960;
2039 /* Increment the sequence number to reflect the last packet
2046 /* \brief Public function: Send an H.261 fast update request, some devices need this rather than SIP XML */
2047 int ast_rtcp_send_h261fur(void *data)
2049 struct ast_rtp *rtp = data;
2052 rtp->rtcp->sendfur = 1;
2053 res = ast_rtcp_write(data);
2058 /*! \brief Send RTCP sender's report */
2059 static int ast_rtcp_write_sr(void *data)
2061 struct ast_rtp *rtp = data;
2065 unsigned int now_lsw;
2066 unsigned int now_msw;
2067 unsigned int *rtcpheader;
2069 unsigned int extended;
2070 unsigned int expected;
2071 unsigned int expected_interval;
2072 unsigned int received_interval;
2075 struct timeval dlsr;
2078 if (!rtp || !rtp->rtcp || (&rtp->rtcp->them.sin_addr == 0))
2081 if (!rtp->rtcp->them.sin_addr.s_addr) { /* This'll stop rtcp for this rtp session */
2082 ast_verbose("RTCP SR transmission error, rtcp halted %s\n",strerror(errno));
2083 if (rtp->rtcp->schedid > 0)
2084 ast_sched_del(rtp->sched, rtp->rtcp->schedid);
2085 rtp->rtcp->schedid = -1;
2089 gettimeofday(&now, NULL);
2090 timeval2ntp(now, &now_msw, &now_lsw); /* fill thses ones in from utils.c*/
2091 rtcpheader = (unsigned int *)bdata;
2092 rtcpheader[1] = htonl(rtp->ssrc); /* Our SSRC */
2093 rtcpheader[2] = htonl(now_msw); /* now, MSW. gettimeofday() + SEC_BETWEEN_1900_AND_1970*/
2094 rtcpheader[3] = htonl(now_lsw); /* now, LSW */
2095 rtcpheader[4] = htonl(rtp->lastts); /* FIXME shouldn't be that, it should be now */
2096 rtcpheader[5] = htonl(rtp->txcount); /* No. packets sent */
2097 rtcpheader[6] = htonl(rtp->txoctetcount); /* No. bytes sent */
2100 extended = rtp->cycles + rtp->lastrxseqno;
2101 expected = extended - rtp->seedrxseqno + 1;
2102 if (rtp->rxcount > expected)
2103 expected += rtp->rxcount - expected;
2104 lost = expected - rtp->rxcount;
2105 expected_interval = expected - rtp->rtcp->expected_prior;
2106 rtp->rtcp->expected_prior = expected;
2107 received_interval = rtp->rxcount - rtp->rtcp->received_prior;
2108 rtp->rtcp->received_prior = rtp->rxcount;
2109 lost_interval = expected_interval - received_interval;
2110 if (expected_interval == 0 || lost_interval <= 0)
2113 fraction = (lost_interval << 8) / expected_interval;
2114 timersub(&now, &rtp->rtcp->rxlsr, &dlsr);
2115 rtcpheader[7] = htonl(rtp->themssrc);
2116 rtcpheader[8] = htonl(((fraction & 0xff) << 24) | (lost & 0xffffff));
2117 rtcpheader[9] = htonl((rtp->cycles) | ((rtp->lastrxseqno & 0xffff)));
2118 rtcpheader[10] = htonl((unsigned int)rtp->rxjitter);
2119 rtcpheader[11] = htonl(rtp->rtcp->themrxlsr);
2120 rtcpheader[12] = htonl((((dlsr.tv_sec * 1000) + (dlsr.tv_usec / 1000)) * 65536) / 1000);
2123 rtcpheader[0] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SR << 16) | ((len/4)-1));
2125 if (rtp->rtcp->sendfur) {
2126 rtcpheader[13] = htonl((2 << 30) | (0 << 24) | (RTCP_PT_FUR << 16) | 1);
2127 rtcpheader[14] = htonl(rtp->ssrc); /* Our SSRC */
2129 rtp->rtcp->sendfur = 0;
2132 /* Insert SDES here. Probably should make SDES text equal to mimetypes[code].type (not subtype 'cos */
2133 /* it can change mid call, and SDES can't) */
2134 rtcpheader[len/4] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SDES << 16) | 2);
2135 rtcpheader[(len/4)+1] = htonl(rtp->ssrc); /* Our SSRC */
2136 rtcpheader[(len/4)+2] = htonl(0x01 << 24); /* Empty for the moment */
2139 res = sendto(rtp->rtcp->s, (unsigned int *)rtcpheader, len, 0, (struct sockaddr *)&rtp->rtcp->them, sizeof(rtp->rtcp->them));
2141 ast_log(LOG_ERROR, "RTCP SR transmission error to %s:%d, rtcp halted %s\n",ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port), strerror(errno));
2142 if (rtp->rtcp->schedid > 0)
2143 ast_sched_del(rtp->sched, rtp->rtcp->schedid);
2144 rtp->rtcp->schedid = -1;
2148 /* FIXME Don't need to get a new one */
2149 gettimeofday(&rtp->rtcp->txlsr, NULL);
2150 rtp->rtcp->sr_count++;
2152 rtp->rtcp->lastsrtxcount = rtp->txcount;
2154 if (rtcp_debug_test_addr(&rtp->rtcp->them)) {
2155 ast_verbose("* Sent RTCP SR to %s:%d\n", ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
2156 ast_verbose(" Our SSRC: %u\n", rtp->ssrc);
2157 ast_verbose(" Sent(NTP): %u.%010u\n", (unsigned int)now.tv_sec, (unsigned int)now.tv_usec*4096);
2158 ast_verbose(" Sent(RTP): %u\n", rtp->lastts);
2159 ast_verbose(" Sent packets: %u\n", rtp->txcount);
2160 ast_verbose(" Sent octets: %u\n", rtp->txoctetcount);
2161 ast_verbose(" Report block:\n");
2162 ast_verbose(" Fraction lost: %u\n", fraction);
2163 ast_verbose(" Cumulative loss: %u\n", lost);
2164 ast_verbose(" IA jitter: %.4f\n", rtp->rxjitter);
2165 ast_verbose(" Their last SR: %u\n", rtp->rtcp->themrxlsr);
2166 ast_verbose(" DLSR: %4.4f (sec)\n\n", (double)(ntohl(rtcpheader[12])/65536.0));
2171 /*! \brief Send RTCP recepient's report */
2172 static int ast_rtcp_write_rr(void *data)
2174 struct ast_rtp *rtp = data;
2178 unsigned int extended;
2179 unsigned int expected;
2180 unsigned int expected_interval;
2181 unsigned int received_interval;
2184 unsigned int *rtcpheader;
2186 struct timeval dlsr;
2189 if (!rtp || !rtp->rtcp || (&rtp->rtcp->them.sin_addr == 0))
2192 if (!rtp->rtcp->them.sin_addr.s_addr) {
2193 ast_log(LOG_ERROR, "RTCP RR transmission error to, rtcp halted %s\n",strerror(errno));
2194 if (rtp->rtcp->schedid > 0)
2195 ast_sched_del(rtp->sched, rtp->rtcp->schedid);
2196 rtp->rtcp->schedid = -1;
2200 extended = rtp->cycles + rtp->lastrxseqno;
2201 expected = extended - rtp->seedrxseqno + 1;
2202 lost = expected - rtp->rxcount;
2203 expected_interval = expected - rtp->rtcp->expected_prior;
2204 rtp->rtcp->expected_prior = expected;
2205 received_interval = rtp->rxcount - rtp->rtcp->received_prior;
2206 rtp->rtcp->received_prior = rtp->rxcount;
2207 lost_interval = expected_interval - received_interval;
2208 if (expected_interval == 0 || lost_interval <= 0)
2211 fraction = (lost_interval << 8) / expected_interval;
2212 gettimeofday(&now, NULL);
2213 timersub(&now, &rtp->rtcp->rxlsr, &dlsr);
2214 rtcpheader = (unsigned int *)bdata;
2215 rtcpheader[0] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_RR << 16) | ((len/4)-1));
2216 rtcpheader[1] = htonl(rtp->ssrc);
2217 rtcpheader[2] = htonl(rtp->themssrc);
2218 rtcpheader[3] = htonl(((fraction & 0xff) << 24) | (lost & 0xffffff));
2219 rtcpheader[4] = htonl((rtp->cycles) | ((rtp->lastrxseqno & 0xffff)));
2220 rtcpheader[5] = htonl((unsigned int)rtp->rxjitter);
2221 rtcpheader[6] = htonl(rtp->rtcp->themrxlsr);
2222 rtcpheader[7] = htonl((((dlsr.tv_sec * 1000) + (dlsr.tv_usec / 1000)) * 65536) / 1000);
2224 if (rtp->rtcp->sendfur) {
2225 rtcpheader[8] = htonl((2 << 30) | (0 << 24) | (RTCP_PT_FUR << 16) | 1); /* Header from page 36 in RFC 3550 */
2226 rtcpheader[9] = htonl(rtp->ssrc); /* Our SSRC */
2228 rtp->rtcp->sendfur = 0;
2231 /*! \note Insert SDES here. Probably should make SDES text equal to mimetypes[code].type (not subtype 'cos
2232 it can change mid call, and SDES can't) */
2233 rtcpheader[len/4] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SDES << 16) | 2);
2234 rtcpheader[(len/4)+1] = htonl(rtp->ssrc); /* Our SSRC */
2235 rtcpheader[(len/4)+2] = htonl(0x01 << 24); /* Empty for the moment */
2238 res = sendto(rtp->rtcp->s, (unsigned int *)rtcpheader, len, 0, (struct sockaddr *)&rtp->rtcp->them, sizeof(rtp->rtcp->them));
2241 ast_log(LOG_ERROR, "RTCP RR transmission error, rtcp halted: %s\n",strerror(errno));
2242 /* Remove the scheduler */
2243 if (rtp->rtcp->schedid > 0)
2244 ast_sched_del(rtp->sched, rtp->rtcp->schedid);
2245 rtp->rtcp->schedid = -1;
2249 rtp->rtcp->rr_count++;
2251 if (rtcp_debug_test_addr(&rtp->rtcp->them)) {
2252 ast_verbose("\n* Sending RTCP RR to %s:%d\n"
2253 " Our SSRC: %u\nTheir SSRC: %u\niFraction lost: %d\nCumulative loss: %u\n"
2254 " IA jitter: %.4f\n"
2255 " Their last SR: %u\n"
2256 " DLSR: %4.4f (sec)\n\n",
2257 ast_inet_ntoa(rtp->rtcp->them.sin_addr),
2258 ntohs(rtp->rtcp->them.sin_port),
2259 rtp->ssrc, rtp->themssrc, fraction, lost,
2261 rtp->rtcp->themrxlsr,
2262 (double)(ntohl(rtcpheader[7])/65536.0));
2268 /*! \brief Write and RTCP packet to the far end
2269 * \note Decide if we are going to send an SR (with Reception Block) or RR
2270 * RR is sent if we have not sent any rtp packets in the previous interval */
2271 static int ast_rtcp_write(void *data)
2273 struct ast_rtp *rtp = data;
2276 if (rtp->txcount > rtp->rtcp->lastsrtxcount)
2277 res = ast_rtcp_write_sr(data);
2279 res = ast_rtcp_write_rr(data);
2284 /*! \brief generate comfort noice (CNG) */
2285 int ast_rtp_sendcng(struct ast_rtp *rtp, int level)
2287 unsigned int *rtpheader;
2292 level = 127 - (level & 0x7f);
2293 payload = ast_rtp_lookup_code(rtp, 0, AST_RTP_CN);
2295 /* If we have no peer, return immediately */
2296 if (!rtp->them.sin_addr.s_addr)
2299 rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
2301 /* Get a pointer to the header */
2302 rtpheader = (unsigned int *)data;
2303 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno++));
2304 rtpheader[1] = htonl(rtp->lastts);
2305 rtpheader[2] = htonl(rtp->ssrc);
2307 if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
2308 res = sendto(rtp->s, (void *)rtpheader, hdrlen + 1, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
2310 ast_log(LOG_ERROR, "RTP Comfort Noise Transmission error to %s:%d: %s\n", ast_inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port), strerror(errno));
2311 if (rtp_debug_test_addr(&rtp->them))
2312 ast_verbose("Sent Comfort Noise RTP packet to %s:%d (type %d, seq %d, ts %u, len %d)\n"
2313 , ast_inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port), payload, rtp->seqno, rtp->lastts,res - hdrlen);
2319 static int ast_rtp_raw_write(struct ast_rtp *rtp, struct ast_frame *f, int codec)
2321 unsigned char *rtpheader;
2328 ms = calc_txstamp(rtp, &f->delivery);
2329 /* Default prediction */
2330 if (f->subclass < AST_FORMAT_MAX_AUDIO) {
2331 pred = rtp->lastts + f->samples;
2333 /* Re-calculate last TS */
2334 rtp->lastts = rtp->lastts + ms * 8;
2335 if (ast_tvzero(f->delivery)) {
2336 /* If this isn't an absolute delivery time, Check if it is close to our prediction,
2337 and if so, go with our prediction */
2338 if (abs(rtp->lastts - pred) < MAX_TIMESTAMP_SKEW)
2341 if (option_debug > 2)
2342 ast_log(LOG_DEBUG, "Difference is %d, ms is %d\n", abs(rtp->lastts - pred), ms);
2347 mark = f->subclass & 0x1;
2348 pred = rtp->lastovidtimestamp + f->samples;
2349 /* Re-calculate last TS */
2350 rtp->lastts = rtp->lastts + ms * 90;
2351 /* If it's close to our prediction, go for it */
2352 if (ast_tvzero(f->delivery)) {
2353 if (abs(rtp->lastts - pred) < 7200) {
2355 rtp->lastovidtimestamp += f->samples;
2357 if (option_debug > 2)
2358 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);
2359 rtp->lastovidtimestamp = rtp->lastts;
2363 /* If the timestamp for non-digit packets has moved beyond the timestamp
2364 for digits, update the digit timestamp.
2366 if (rtp->lastts > rtp->lastdigitts)
2367 rtp->lastdigitts = rtp->lastts;
2369 if (f->has_timing_info)
2370 rtp->lastts = f->ts * 8;
2372 /* Get a pointer to the header */
2373 rtpheader = (unsigned char *)(f->data - hdrlen);
2375 put_unaligned_uint32(rtpheader, htonl((2 << 30) | (codec << 16) | (rtp->seqno) | (mark << 23)));
2376 put_unaligned_uint32(rtpheader + 4, htonl(rtp->lastts));
2377 put_unaligned_uint32(rtpheader + 8, htonl(rtp->ssrc));
2379 if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
2380 res = sendto(rtp->s, (void *)rtpheader, f->datalen + hdrlen, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
2382 if (!rtp->nat || (rtp->nat && (ast_test_flag(rtp, FLAG_NAT_ACTIVE) == FLAG_NAT_ACTIVE))) {
2383 ast_log(LOG_DEBUG, "RTP Transmission error of packet %d to %s:%d: %s\n", rtp->seqno, ast_inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port), strerror(errno));
2384 } else if (((ast_test_flag(rtp, FLAG_NAT_ACTIVE) == FLAG_NAT_INACTIVE) || rtpdebug) && !ast_test_flag(rtp, FLAG_NAT_INACTIVE_NOWARN)) {
2385 /* Only give this error message once if we are not RTP debugging */
2386 if (option_debug || rtpdebug)
2387 ast_log(LOG_DEBUG, "RTP NAT: Can't write RTP to private address %s:%d, waiting for other end to send audio...\n", ast_inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port));
2388 ast_set_flag(rtp, FLAG_NAT_INACTIVE_NOWARN);
2392 rtp->txoctetcount +=(res - hdrlen);
2394 if (rtp->rtcp && rtp->rtcp->schedid < 1)
2395 rtp->rtcp->schedid = ast_sched_add(rtp->sched, ast_rtcp_calc_interval(rtp), ast_rtcp_write, rtp);
2398 if (rtp_debug_test_addr(&rtp->them))
2399 ast_verbose("Sent RTP packet to %s:%d (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
2400 ast_inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port), codec, rtp->seqno, rtp->lastts,res - hdrlen);
2408 int ast_rtp_write(struct ast_rtp *rtp, struct ast_frame *_f)
2410 struct ast_frame *f;
2416 /* If we have no peer, return immediately */
2417 if (!rtp->them.sin_addr.s_addr)
2420 /* If there is no data length, return immediately */
2424 /* Make sure we have enough space for RTP header */
2425 if ((_f->frametype != AST_FRAME_VOICE) && (_f->frametype != AST_FRAME_VIDEO)) {
2426 ast_log(LOG_WARNING, "RTP can only send voice and video\n");
2430 subclass = _f->subclass;
2431 if (_f->frametype == AST_FRAME_VIDEO)
2434 codec = ast_rtp_lookup_code(rtp, 1, subclass);
2436 ast_log(LOG_WARNING, "Don't know how to send format %s packets with RTP\n", ast_getformatname(_f->subclass));
2440 if (rtp->lasttxformat != subclass) {
2441 /* New format, reset the smoother */
2443 ast_log(LOG_DEBUG, "Ooh, format changed from %s to %s\n", ast_getformatname(rtp->lasttxformat), ast_getformatname(subclass));
2444 rtp->lasttxformat = subclass;
2446 ast_smoother_free(rtp->smoother);
2447 rtp->smoother = NULL;
2452 case AST_FORMAT_SLINEAR:
2453 if (!rtp->smoother) {
2454 rtp->smoother = ast_smoother_new(320);
2456 if (!rtp->smoother) {
2457 ast_log(LOG_WARNING, "Unable to create smoother :(\n");
2460 ast_smoother_feed_be(rtp->smoother, _f);
2462 while((f = ast_smoother_read(rtp->smoother)))
2463 ast_rtp_raw_write(rtp, f, codec);
2465 case AST_FORMAT_ULAW:
2466 case AST_FORMAT_ALAW:
2467 if (!rtp->smoother) {
2468 rtp->smoother = ast_smoother_new(160);
2470 if (!rtp->smoother) {
2471 ast_log(LOG_WARNING, "Unable to create smoother :(\n");
2474 ast_smoother_feed(rtp->smoother, _f);
2476 while((f = ast_smoother_read(rtp->smoother)))
2477 ast_rtp_raw_write(rtp, f, codec);
2479 case AST_FORMAT_ADPCM:
2480 case AST_FORMAT_G726:
2481 case AST_FORMAT_G726_AAL2:
2482 if (!rtp->smoother) {
2483 rtp->smoother = ast_smoother_new(80);
2485 if (!rtp->smoother) {
2486 ast_log(LOG_WARNING, "Unable to create smoother :(\n");
2489 ast_smoother_feed(rtp->smoother, _f);
2491 while((f = ast_smoother_read(rtp->smoother)))
2492 ast_rtp_raw_write(rtp, f, codec);
2494 case AST_FORMAT_G729A:
2495 if (!rtp->smoother) {
2496 rtp->smoother = ast_smoother_new(20);
2498 ast_smoother_set_flags(rtp->smoother, AST_SMOOTHER_FLAG_G729);
2500 if (!rtp->smoother) {
2501 ast_log(LOG_WARNING, "Unable to create g729 smoother :(\n");
2504 ast_smoother_feed(rtp->smoother, _f);
2506 while((f = ast_smoother_read(rtp->smoother)))
2507 ast_rtp_raw_write(rtp, f, codec);
2509 case AST_FORMAT_GSM:
2510 if (!rtp->smoother) {
2511 rtp->smoother = ast_smoother_new(33);
2513 if (!rtp->smoother) {
2514 ast_log(LOG_WARNING, "Unable to create GSM smoother :(\n");
2517 ast_smoother_feed(rtp->smoother, _f);
2518 while((f = ast_smoother_read(rtp->smoother)))
2519 ast_rtp_raw_write(rtp, f, codec);
2521 case AST_FORMAT_ILBC:
2522 if (!rtp->smoother) {
2523 rtp->smoother = ast_smoother_new(50);
2525 if (!rtp->smoother) {
2526 ast_log(LOG_WARNING, "Unable to create ILBC smoother :(\n");
2529 ast_smoother_feed(rtp->smoother, _f);
2530 while((f = ast_smoother_read(rtp->smoother)))
2531 ast_rtp_raw_write(rtp, f, codec);
2534 ast_log(LOG_WARNING, "Not sure about sending format %s packets\n", ast_getformatname(subclass));
2535 /* fall through to... */
2536 case AST_FORMAT_H261:
2537 case AST_FORMAT_H263:
2538 case AST_FORMAT_H263_PLUS:
2539 case AST_FORMAT_H264:
2540 case AST_FORMAT_G723_1:
2541 case AST_FORMAT_LPC10:
2542 case AST_FORMAT_SPEEX:
2543 /* Don't buffer outgoing frames; send them one-per-packet: */
2544 if (_f->offset < hdrlen) {
2549 ast_rtp_raw_write(rtp, f, codec);
2555 /*! \brief Unregister interface to channel driver */
2556 void ast_rtp_proto_unregister(struct ast_rtp_protocol *proto)
2558 AST_LIST_LOCK(&protos);
2559 AST_LIST_REMOVE(&protos, proto, list);
2560 AST_LIST_UNLOCK(&protos);
2563 /*! \brief Register interface to channel driver */
2564 int ast_rtp_proto_register(struct ast_rtp_protocol *proto)
2566 struct ast_rtp_protocol *cur;
2568 AST_LIST_LOCK(&protos);
2569 AST_LIST_TRAVERSE(&protos, cur, list) {
2570 if (!strcmp(cur->type, proto->type)) {
2571 ast_log(LOG_WARNING, "Tried to register same protocol '%s' twice\n", cur->type);
2572 AST_LIST_UNLOCK(&protos);
2576 AST_LIST_INSERT_HEAD(&protos, proto, list);
2577 AST_LIST_UNLOCK(&protos);
2582 /*! \brief Bridge loop for true native bridge (reinvite) */
2583 static enum ast_bridge_result bridge_native_loop(struct ast_channel *c0, struct ast_channel *c1, struct ast_rtp *p0, struct ast_rtp *p1, struct ast_rtp *vp0, struct ast_rtp *vp1, struct ast_rtp_protocol *pr0, struct ast_rtp_protocol *pr1, int codec0, int codec1, int timeoutms, int flags, struct ast_frame **fo, struct ast_channel **rc, void *pvt0, void *pvt1)
2585 struct ast_frame *fr = NULL;
2586 struct ast_channel *who = NULL, *other = NULL, *cs[3] = {NULL, };
2587 int oldcodec0 = codec0, oldcodec1 = codec1;
2588 struct sockaddr_in ac1 = {0,}, vac1 = {0,}, ac0 = {0,}, vac0 = {0,};
2589 struct sockaddr_in t1 = {0,}, vt1 = {0,}, t0 = {0,}, vt0 = {0,};
2591 /* Set it up so audio goes directly between the two endpoints */
2593 /* Test the first channel */
2594 if (!(pr0->set_rtp_peer(c0, p1, vp1, codec1, ast_test_flag(p1, FLAG_NAT_ACTIVE)))) {
2595 ast_rtp_get_peer(p1, &ac1);
2597 ast_rtp_get_peer(vp1, &vac1);
2599 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
2601 /* Test the second channel */
2602 if (!(pr1->set_rtp_peer(c1, p0, vp0, codec0, ast_test_flag(p0, FLAG_NAT_ACTIVE)))) {
2603 ast_rtp_get_peer(p0, &ac0);
2605 ast_rtp_get_peer(vp0, &vac0);
2607 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c1->name, c0->name);
2609 /* Now we can unlock and move into our loop */
2610 ast_channel_unlock(c0);
2611 ast_channel_unlock(c1);
2613 /* Throw our channels into the structure and enter the loop */
2618 /* Check if anything changed */
2619 if ((c0->tech_pvt != pvt0) ||
2620 (c1->tech_pvt != pvt1) ||
2621 (c0->masq || c0->masqr || c1->masq || c1->masqr)) {
2622 ast_log(LOG_DEBUG, "Oooh, something is weird, backing out\n");
2623 if (c0->tech_pvt == pvt0)
2624 if (pr0->set_rtp_peer(c0, NULL, NULL, 0, 0))
2625 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c0->name);
2626 if (c1->tech_pvt == pvt1)
2627 if (pr1->set_rtp_peer(c1, NULL, NULL, 0, 0))
2628 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c1->name);
2629 return AST_BRIDGE_RETRY;
2632 /* Check if they have changed their address */
2633 ast_rtp_get_peer(p1, &t1);
2635 ast_rtp_get_peer(vp1, &vt1);
2637 codec1 = pr1->get_codec(c1);
2638 ast_rtp_get_peer(p0, &t0);
2640 ast_rtp_get_peer(vp0, &vt0);
2642 codec0 = pr0->get_codec(c0);
2643 if ((inaddrcmp(&t1, &ac1)) ||
2644 (vp1 && inaddrcmp(&vt1, &vac1)) ||
2645 (codec1 != oldcodec1)) {
2646 if (option_debug > 1) {
2647 ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
2648 c1->name, ast_inet_ntoa(t1.sin_addr), ntohs(t1.sin_port), codec1);
2649 ast_log(LOG_DEBUG, "Oooh, '%s' changed end vaddress to %s:%d (format %d)\n",
2650 c1->name, ast_inet_ntoa(vt1.sin_addr), ntohs(vt1.sin_port), codec1);
2651 ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d/(format %d)\n",
2652 c1->name, ast_inet_ntoa(ac1.sin_addr), ntohs(ac1.sin_port), oldcodec1);
2653 ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d/(format %d)\n",
2654 c1->name, ast_inet_ntoa(vac1.sin_addr), ntohs(vac1.sin_port), oldcodec1);
2656 if (pr0->set_rtp_peer(c0, t1.sin_addr.s_addr ? p1 : NULL, vt1.sin_addr.s_addr ? vp1 : NULL, codec1, ast_test_flag(p1, FLAG_NAT_ACTIVE)))
2657 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c0->name, c1->name);
2658 memcpy(&ac1, &t1, sizeof(ac1));
2659 memcpy(&vac1, &vt1, sizeof(vac1));
2662 if ((inaddrcmp(&t0, &ac0)) ||
2663 (vp0 && inaddrcmp(&vt0, &vac0))) {
2664 if (option_debug > 1) {
2665 ast_log(LOG_DEBUG, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
2666 c0->name, ast_inet_ntoa(t0.sin_addr), ntohs(t0.sin_port), codec0);
2667 ast_log(LOG_DEBUG, "Oooh, '%s' was %s:%d/(format %d)\n",
2668 c0->name, ast_inet_ntoa(ac0.sin_addr), ntohs(ac0.sin_port), oldcodec0);
2670 if (pr1->set_rtp_peer(c1, t0.sin_addr.s_addr ? p0 : NULL, vt0.sin_addr.s_addr ? vp0 : NULL, codec0, ast_test_flag(p0, FLAG_NAT_ACTIVE)))
2671 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c1->name, c0->name);
2672 memcpy(&ac0, &t0, sizeof(ac0));
2673 memcpy(&vac0, &vt0, sizeof(vac0));
2677 /* Wait for frame to come in on the channels */
2678 if (!(who = ast_waitfor_n(cs, 2, &timeoutms))) {
2680 return AST_BRIDGE_RETRY;
2682 ast_log(LOG_DEBUG, "Ooh, empty read...\n");
2683 if (ast_check_hangup(c0) || ast_check_hangup(c1))
2688 other = (who == c0) ? c1 : c0;
2689 if (!fr || ((fr->frametype == AST_FRAME_DTMF) &&
2690 (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
2691 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))))) {
2692 /* Break out of bridge */
2696 ast_log(LOG_DEBUG, "Oooh, got a %s\n", fr ? "digit" : "hangup");
2697 if (c0->tech_pvt == pvt0)
2698 if (pr0->set_rtp_peer(c0, NULL, NULL, 0, 0))
2699 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c0->name);
2700 if (c1->tech_pvt == pvt1)
2701 if (pr1->set_rtp_peer(c1, NULL, NULL, 0, 0))
2702 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c1->name);
2703 return AST_BRIDGE_COMPLETE;
2704 } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
2705 if ((fr->subclass == AST_CONTROL_HOLD) ||
2706 (fr->subclass == AST_CONTROL_UNHOLD) ||
2707 (fr->subclass == AST_CONTROL_VIDUPDATE)) {
2708 ast_indicate(other, fr->subclass);
2713 ast_log(LOG_DEBUG, "Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass, who->name);
2714 return AST_BRIDGE_COMPLETE;
2717 if ((fr->frametype == AST_FRAME_DTMF) ||
2718 (fr->frametype == AST_FRAME_VOICE) ||
2719 (fr->frametype == AST_FRAME_VIDEO)) {
2720 ast_write(other, fr);
2730 return AST_BRIDGE_FAILED;
2733 /*! \brief P2P RTP/RTCP Callback */
2734 static int p2p_rtp_callback(int *id, int fd, short events, void *cbdata)
2736 int res = 0, hdrlen = 12;
2737 struct sockaddr_in sin;
2739 unsigned int *header;
2740 struct ast_rtp *rtp = cbdata;
2741 int is_rtp = 0, is_rtcp = 0;
2747 if ((res = recvfrom(fd, rtp->rawdata + AST_FRIENDLY_OFFSET, sizeof(rtp->rawdata) - AST_FRIENDLY_OFFSET, 0, (struct sockaddr *)&sin, &len)) < 0)
2750 header = (unsigned int *)(rtp->rawdata + AST_FRIENDLY_OFFSET);
2752 /* Determine what this file descriptor is for */
2755 else if (rtp->rtcp && rtp->rtcp->s == fd)
2758 /* If NAT support is turned on, then see if we need to change their address */
2760 /* If this is for RTP, check that - if it's for RTCP, check that */
2762 if ((rtp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
2763 (rtp->them.sin_port != sin.sin_port)) {
2766 ast_set_flag(rtp, FLAG_NAT_ACTIVE);
2767 if (option_debug || rtpdebug)
2768 ast_log(LOG_DEBUG, "P2P RTP NAT: Got audio from other end. Now sending to address %s:%d\n", ast_inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port));
2770 } else if (is_rtcp) {
2771 if ((rtp->rtcp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
2772 (rtp->rtcp->them.sin_port != sin.sin_port)) {
2773 rtp->rtcp->them = sin;
2774 if (option_debug || rtpdebug)
2775 ast_log(LOG_DEBUG, "P2P RTCP NAT: Got RTCP from other end. Now sending to address %s:%d\n", ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
2780 /* If this came from the RTP stream, write out via RTP - if it's RTCP, write out via RTCP */
2782 bridge_p2p_rtp_write(rtp, header, res, hdrlen);
2784 bridge_p2p_rtcp_write(rtp, header, res);
2789 /*! \brief Helper function to switch a channel and RTP stream into callback mode */
2790 static int p2p_callback_enable(struct ast_channel *chan, struct ast_rtp *rtp, int *fds, int **iod)
2792 /* If we need DTMF or we have no IO structure, then we can't do direct callback */
2793 if (ast_test_flag(rtp, FLAG_P2P_NEED_DTMF) || !rtp->io)
2796 /* If the RTP structure is already in callback mode, remove it temporarily */
2798 ast_io_remove(rtp->io, rtp->ioid);
2802 /* Steal the file descriptors from the channel and stash them away */
2803 fds[0] = chan->fds[0];
2804 fds[1] = chan->fds[1];
2808 /* Now, fire up callback mode */
2809 iod[0] = ast_io_add(rtp->io, fds[0], p2p_rtp_callback, AST_IO_IN, rtp);
2811 iod[1] = ast_io_add(rtp->io, fds[1], p2p_rtp_callback, AST_IO_IN, rtp);
2816 /*! \brief Helper function to switch a channel and RTP stream out of callback mode */
2817 static int p2p_callback_disable(struct ast_channel *chan, struct ast_rtp *rtp, int *fds, int **iod)
2819 ast_channel_lock(chan);
2820 /* Remove the callback from the IO context */
2821 ast_io_remove(rtp->io, iod[0]);
2822 ast_io_remove(rtp->io, iod[1]);
2823 /* Restore file descriptors */
2824 chan->fds[0] = fds[0];
2825 chan->fds[1] = fds[1];
2826 ast_channel_unlock(chan);
2827 /* Restore callback mode if previously used */
2828 if (ast_test_flag(rtp, FLAG_CALLBACK_MODE))
2829 rtp->ioid = ast_io_add(rtp->io, rtp->s, rtpread, AST_IO_IN, rtp);
2833 /*! \brief Bridge loop for partial native bridge (packet2packet) */
2834 static enum ast_bridge_result bridge_p2p_loop(struct ast_channel *c0, struct ast_channel *c1, struct ast_rtp *p0, struct ast_rtp *p1, struct ast_rtp *vp0, struct ast_rtp *vp1, int timeoutms, int flags, struct ast_frame **fo, struct ast_channel **rc, void *pvt0, void *pvt1)
2836 struct ast_frame *fr = NULL;
2837 struct ast_channel *who = NULL, *other = NULL, *cs[3] = {NULL, };
2838 int p0_fds[2] = {-1, -1}, p1_fds[2] = {-1, -1};
2839 int *p0_iod[2] = {NULL, }, *p1_iod[2] = {NULL, };
2840 int p0_callback = 0, p1_callback = 0;
2841 enum ast_bridge_result res = AST_BRIDGE_FAILED;
2843 /* Okay, setup each RTP structure to do P2P forwarding */
2844 ast_clear_flag(p0, FLAG_P2P_SENT_MARK);
2846 ast_clear_flag(p1, FLAG_P2P_SENT_MARK);
2849 ast_clear_flag(vp0, FLAG_P2P_SENT_MARK);
2851 ast_clear_flag(vp1, FLAG_P2P_SENT_MARK);
2855 /* Activate callback modes if possible */
2856 p0_callback = p2p_callback_enable(c0, p0, &p0_fds[0], &p0_iod[0]);
2857 p1_callback = p2p_callback_enable(c1, p1, &p1_fds[0], &p1_iod[0]);
2859 /* Now let go of the channel locks and be on our way */
2860 ast_channel_unlock(c0);
2861 ast_channel_unlock(c1);
2863 /* Go into a loop forwarding frames until we don't need to anymore */
2868 /* Check if anything changed */
2869 if ((c0->tech_pvt != pvt0) ||
2870 (c1->tech_pvt != pvt1) ||
2871 (c0->masq || c0->masqr || c1->masq || c1->masqr)) {
2872 ast_log(LOG_DEBUG, "Oooh, something is weird, backing out\n");
2873 res = AST_BRIDGE_RETRY;
2876 /* Wait on a channel to feed us a frame */
2877 if (!(who = ast_waitfor_n(cs, 2, &timeoutms))) {
2879 res = AST_BRIDGE_RETRY;
2883 ast_log(LOG_NOTICE, "Ooh, empty read...\n");
2884 if (ast_check_hangup(c0) || ast_check_hangup(c1))
2888 /* Read in frame from channel */
2890 other = (who == c0) ? c1 : c0;
2891 /* Dependong on the frame we may need to break out of our bridge */
2892 if (!fr || ((fr->frametype == AST_FRAME_DTMF) &&
2893 ((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) |
2894 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1)))) {
2895 /* Record received frame and who */
2899 ast_log(LOG_DEBUG, "Oooh, got a %s\n", fr ? "digit" : "hangup");
2900 /* Break out of the bridge */
2904 vp0->bridged = NULL;
2905 vp1->bridged = NULL;
2907 res = AST_BRIDGE_COMPLETE;
2909 } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
2910 if ((fr->subclass == AST_CONTROL_HOLD) ||
2911 (fr->subclass == AST_CONTROL_UNHOLD) ||
2912 (fr->subclass == AST_CONTROL_VIDUPDATE)) {
2913 /* If we are going on hold, then break callback mode */
2914 if (fr->subclass == AST_CONTROL_HOLD) {
2916 p0_callback = p2p_callback_disable(c0, p0, &p0_fds[0], &p0_iod[0]);
2918 p1_callback = p2p_callback_disable(c1, p1, &p1_fds[0], &p1_iod[0]);
2919 } else if (fr->subclass == AST_CONTROL_UNHOLD) {
2920 /* If we are off hold, then go back to callback mode */
2921 p0_callback = p2p_callback_enable(c0, p0, &p0_fds[0], &p0_iod[0]);
2922 p1_callback = p2p_callback_enable(c1, p1, &p1_fds[0], &p1_iod[0]);
2924 ast_indicate(other, fr->subclass);
2929 ast_log(LOG_DEBUG, "Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass, who->name);
2930 res = AST_BRIDGE_COMPLETE;
2934 /* If this is a DTMF, voice, or video frame write it to the other channel */
2935 if ((fr->frametype == AST_FRAME_DTMF) ||
2936 (fr->frametype == AST_FRAME_VOICE) ||
2937 (fr->frametype == AST_FRAME_VIDEO)) {
2938 ast_write(other, fr);
2948 /* If we are totally avoiding the core, then restore our link to it */
2950 p0_callback = p2p_callback_disable(c0, p0, &p0_fds[0], &p0_iod[0]);
2952 p1_callback = p2p_callback_disable(c1, p1, &p1_fds[0], &p1_iod[0]);
2957 /*! \brief Bridge calls. If possible and allowed, initiate
2958 re-invite so the peers exchange media directly outside
2960 enum ast_bridge_result ast_rtp_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc, int timeoutms)
2962 struct ast_rtp *p0 = NULL, *p1 = NULL; /* Audio RTP Channels */
2963 struct ast_rtp *vp0 = NULL, *vp1 = NULL; /* Video RTP channels */
2964 struct ast_rtp_protocol *pr0 = NULL, *pr1 = NULL;
2965 enum ast_rtp_get_result audio_p0_res = AST_RTP_GET_FAILED, video_p0_res = AST_RTP_GET_FAILED;
2966 enum ast_rtp_get_result audio_p1_res = AST_RTP_GET_FAILED, video_p1_res = AST_RTP_GET_FAILED;
2967 enum ast_bridge_result res = AST_BRIDGE_FAILED;
2968 int codec0 = 0, codec1 = 0;
2969 void *pvt0 = NULL, *pvt1 = NULL;
2972 ast_channel_lock(c0);
2973 while(ast_channel_trylock(c1)) {
2974 ast_channel_unlock(c0);
2976 ast_channel_lock(c0);
2979 /* Find channel driver interfaces */
2980 if (!(pr0 = get_proto(c0))) {
2981 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c0->name);
2982 ast_channel_unlock(c0);
2983 ast_channel_unlock(c1);
2984 return AST_BRIDGE_FAILED;
2986 if (!(pr1 = get_proto(c1))) {
2987 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", c1->name);
2988 ast_channel_unlock(c0);
2989 ast_channel_unlock(c1);
2990 return AST_BRIDGE_FAILED;
2993 /* Get channel specific interface structures */
2994 pvt0 = c0->tech_pvt;
2995 pvt1 = c1->tech_pvt;
2997 /* Get audio and video interface (if native bridge is possible) */
2998 audio_p0_res = pr0->get_rtp_info(c0, &p0);
2999 video_p0_res = pr0->get_vrtp_info ? pr0->get_vrtp_info(c0, &vp0) : AST_RTP_GET_FAILED;
3000 audio_p1_res = pr1->get_rtp_info(c1, &p1);
3001 video_p1_res = pr1->get_vrtp_info ? pr1->get_vrtp_info(c1, &vp1) : AST_RTP_GET_FAILED;
3003 /* Check if bridge is still possible (In SIP canreinvite=no stops this, like NAT) */
3004 if (audio_p0_res == AST_RTP_GET_FAILED || audio_p1_res == AST_RTP_GET_FAILED) {
3005 /* Somebody doesn't want to play... */
3006 ast_channel_unlock(c0);
3007 ast_channel_unlock(c1);
3008 return AST_BRIDGE_FAILED_NOWARN;
3011 /* If we need to feed DTMF frames into the core then only do a partial native bridge */
3012 if (ast_test_flag(p0, FLAG_HAS_DTMF) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) {
3013 ast_set_flag(p0, FLAG_P2P_NEED_DTMF);
3014 audio_p0_res = AST_RTP_TRY_PARTIAL;
3017 if (ast_test_flag(p1, FLAG_HAS_DTMF) && (flags & AST_BRIDGE_DTMF_CHANNEL_1)) {
3018 ast_set_flag(p1, FLAG_P2P_NEED_DTMF);
3019 audio_p1_res = AST_RTP_TRY_PARTIAL;
3022 /* Get codecs from both sides */
3023 codec0 = pr0->get_codec ? pr0->get_codec(c0) : 0;
3024 codec1 = pr1->get_codec ? pr1->get_codec(c1) : 0;
3025 if (pr0->get_codec && pr1->get_codec) {
3026 /* Hey, we can't do native bridging if both parties speak different codecs */
3027 if (!(codec0 & codec1)) {
3029 ast_log(LOG_DEBUG, "Channel codec0 = %d is not codec1 = %d, cannot native bridge in RTP.\n", codec0, codec1);
3030 ast_channel_unlock(c0);
3031 ast_channel_unlock(c1);
3032 return AST_BRIDGE_FAILED_NOWARN;
3036 /* If either side can only do a partial bridge, then don't try for a true native bridge */
3037 if (audio_p0_res == AST_RTP_TRY_PARTIAL || audio_p1_res == AST_RTP_TRY_PARTIAL) {
3038 if (option_verbose > 2)
3039 ast_verbose(VERBOSE_PREFIX_3 "Packet2Packet bridging %s and %s\n", c0->name, c1->name);
3040 res = bridge_p2p_loop(c0, c1, p0, p1, vp0, vp1, timeoutms, flags, fo, rc, pvt0, pvt1);
3042 if (option_verbose > 2)
3043 ast_verbose(VERBOSE_PREFIX_3 "Native bridging %s and %s\n", c0->name, c1->name);
3044 res = bridge_native_loop(c0, c1, p0, p1, vp0, vp1, pr0, pr1, codec0, codec1, timeoutms, flags, fo, rc, pvt0, pvt1);
3050 static int rtp_do_debug_ip(int fd, int argc, char *argv[])
3053 struct ast_hostent ahp;
3058 return RESULT_SHOWUSAGE;
3060 p = strstr(arg, ":");
3066 hp = ast_gethostbyname(arg, &ahp);
3068 return RESULT_SHOWUSAGE;
3069 rtpdebugaddr.sin_family = AF_INET;
3070 memcpy(&rtpdebugaddr.sin_addr, hp->h_addr, sizeof(rtpdebugaddr.sin_addr));
3071 rtpdebugaddr.sin_port = htons(port);
3073 ast_cli(fd, "RTP Debugging Enabled for IP: %s\n", ast_inet_ntoa(rtpdebugaddr.sin_addr));
3075 ast_cli(fd, "RTP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(rtpdebugaddr.sin_addr), port);
3077 return RESULT_SUCCESS;
3080 static int rtcp_do_debug_ip(int fd, int argc, char *argv[])
3083 struct ast_hostent ahp;
3087 return RESULT_SHOWUSAGE;
3090 p = strstr(arg, ":");
3096 hp = ast_gethostbyname(arg, &ahp);
3098 return RESULT_SHOWUSAGE;
3099 rtcpdebugaddr.sin_family = AF_INET;
3100 memcpy(&rtcpdebugaddr.sin_addr, hp->h_addr, sizeof(rtcpdebugaddr.sin_addr));
3101 rtcpdebugaddr.sin_port = htons(port);
3103 ast_cli(fd, "RTCP Debugging Enabled for IP: %s\n", ast_inet_ntoa(rtcpdebugaddr.sin_addr));
3105 ast_cli(fd, "RTCP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(rtcpdebugaddr.sin_addr), port);
3107 return RESULT_SUCCESS;
3110 static int rtp_do_debug(int fd, int argc, char *argv[])
3114 return RESULT_SHOWUSAGE;
3115 return rtp_do_debug_ip(fd, argc, argv);
3118 memset(&rtpdebugaddr,0,sizeof(rtpdebugaddr));
3119 ast_cli(fd, "RTP Debugging Enabled\n");
3120 return RESULT_SUCCESS;
3123 static int rtcp_do_debug(int fd, int argc, char *argv[]) {
3126 return RESULT_SHOWUSAGE;
3127 return rtcp_do_debug_ip(fd, argc, argv);
3130 memset(&rtcpdebugaddr,0,sizeof(rtcpdebugaddr));
3131 ast_cli(fd, "RTCP Debugging Enabled\n");
3132 return RESULT_SUCCESS;
3135 static int rtcp_do_stats(int fd, int argc, char *argv[]) {
3137 return RESULT_SHOWUSAGE;
3140 ast_cli(fd, "RTCP Stats Enabled\n");
3141 return RESULT_SUCCESS;
3144 static int rtp_no_debug(int fd, int argc, char *argv[])
3147 return RESULT_SHOWUSAGE;
3149 ast_cli(fd,"RTP Debugging Disabled\n");
3150 return RESULT_SUCCESS;
3153 static int rtcp_no_debug(int fd, int argc, char *argv[])