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
; /*!< First port for RTP sessions (set in rtp.conf) */
81 static int rtpend
; /*!< Last port for RTP sessions (set in rtp.conf) */
82 static int rtpdebug
; /*!< Are we debugging? */
83 static int rtcpdebug
; /*!< Are we debugging RTCP? */
84 static int rtcpstats
; /*!< Are we debugging RTCP? */
85 static int rtcpinterval
= RTCP_DEFAULT_INTERVALMS
; /*!< Time between rtcp reports in millisecs */
86 static int stundebug
; /*!< 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
;
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 */
110 unsigned char rawdata
[8192 + AST_FRIENDLY_OFFSET
];
111 unsigned int ssrc
; /*!< Synchronization source, RFC 3550, page 10. */
112 unsigned int themssrc
; /*!< Their SSRC */
115 unsigned int lastrxts
;
116 unsigned int lastividtimestamp
;
117 unsigned int lastovidtimestamp
;
118 unsigned int lasteventseqn
;
119 int lastrxseqno
; /*!< Last received sequence number */
120 unsigned short seedrxseqno
; /*!< What sequence number did they start with?*/
121 unsigned int seedrxts
; /*!< What RTP timestamp did they start with? */
122 unsigned int rxcount
; /*!< How many packets have we received? */
123 unsigned int rxoctetcount
; /*!< How many octets have we received? should be rxcount *160*/
124 unsigned int txcount
; /*!< How many packets have we sent? */
125 unsigned int txoctetcount
; /*!< How many octets have we sent? (txcount*160)*/
126 unsigned int cycles
; /*!< Shifted count of sequence number cycles */
127 double rxjitter
; /*!< Interarrival jitter at the moment */
128 double rxtransit
; /*!< Relative transit time for previous packet */
132 int rtptimeout
; /*!< RTP timeout time (negative or zero means disabled, negative value means temporarily disabled) */
133 int rtpholdtimeout
; /*!< RTP timeout when on hold (negative or zero means disabled, negative value means temporarily disabled). */
134 int rtpkeepalive
; /*!< Send RTP comfort noice packets for keepalive */
136 /* DTMF Reception Variables */
138 unsigned int lasteventendseqn
;
140 unsigned int dtmfduration
;
141 /* DTMF Transmission Variables */
142 unsigned int lastdigitts
;
148 struct sockaddr_in us
; /*!< Socket representation of the local endpoint. */
149 struct sockaddr_in them
; /*!< Socket representation of the remote endpoint. */
150 struct timeval rxcore
;
151 struct timeval txcore
;
152 double drxcore
; /*!< The double representation of the first received packet */
153 struct timeval lastrx
; /*!< timeval when we last received a packet */
154 struct timeval dtmfmute
;
155 struct ast_smoother
*smoother
;
157 unsigned short seqno
; /*!< Sequence number, RFC 3550, page 13. */
158 unsigned short rxseqno
;
159 struct sched_context
*sched
;
160 struct io_context
*io
;
162 ast_rtp_callback callback
;
163 ast_mutex_t bridge_lock
;
164 struct rtpPayloadType current_RTP_PT
[MAX_RTP_PT
];
165 int rtp_lookup_code_cache_isAstFormat
; /*!< a cache for the result of rtp_lookup_code(): */
166 int rtp_lookup_code_cache_code
;
167 int rtp_lookup_code_cache_result
;
168 struct ast_rtcp
*rtcp
;
169 struct ast_codec_pref pref
;
170 struct ast_rtp
*bridged
; /*!< Who we are Packet bridged to */
173 /* Forward declarations */
174 static int ast_rtcp_write(void *data
);
175 static void timeval2ntp(struct timeval tv
, unsigned int *msw
, unsigned int *lsw
);
176 static int ast_rtcp_write_sr(void *data
);
177 static int ast_rtcp_write_rr(void *data
);
178 static unsigned int ast_rtcp_calc_interval(struct ast_rtp
*rtp
);
179 static int ast_rtp_senddigit_continuation(struct ast_rtp
*rtp
);
180 int ast_rtp_senddigit_end(struct ast_rtp
*rtp
, char digit
);
182 #define FLAG_3389_WARNING (1 << 0)
183 #define FLAG_NAT_ACTIVE (3 << 1)
184 #define FLAG_NAT_INACTIVE (0 << 1)
185 #define FLAG_NAT_INACTIVE_NOWARN (1 << 1)
186 #define FLAG_HAS_DTMF (1 << 3)
187 #define FLAG_P2P_SENT_MARK (1 << 4)
188 #define FLAG_P2P_NEED_DTMF (1 << 5)
189 #define FLAG_CALLBACK_MODE (1 << 6)
190 #define FLAG_DTMF_COMPENSATE (1 << 7)
191 #define FLAG_HAS_STUN (1 << 8)
194 * \brief Structure defining an RTCP session.
196 * The concept "RTCP session" is not defined in RFC 3550, but since
197 * this structure is analogous to ast_rtp, which tracks a RTP session,
198 * it is logical to think of this as a RTCP session.
200 * RTCP packet is defined on page 9 of RFC 3550.
204 int s
; /*!< Socket */
205 struct sockaddr_in us
; /*!< Socket representation of the local endpoint. */
206 struct sockaddr_in them
; /*!< Socket representation of the remote endpoint. */
207 unsigned int soc
; /*!< What they told us */
208 unsigned int spc
; /*!< What they told us */
209 unsigned int themrxlsr
; /*!< The middle 32 bits of the NTP timestamp in the last received SR*/
210 struct timeval rxlsr
; /*!< Time when we got their last SR */
211 struct timeval txlsr
; /*!< Time when we sent or last SR*/
212 unsigned int expected_prior
; /*!< no. packets in previous interval */
213 unsigned int received_prior
; /*!< no. packets received in previous interval */
214 int schedid
; /*!< Schedid returned from ast_sched_add() to schedule RTCP-transmissions*/
215 unsigned int rr_count
; /*!< number of RRs we've sent, not including report blocks in SR's */
216 unsigned int sr_count
; /*!< number of SRs we've sent */
217 unsigned int lastsrtxcount
; /*!< Transmit packet count when last SR sent */
218 double accumulated_transit
; /*!< accumulated a-dlsr-lsr */
219 double rtt
; /*!< Last reported rtt */
220 unsigned int reported_jitter
; /*!< The contents of their last jitter entry in the RR */
221 unsigned int reported_lost
; /*!< Reported lost packets in their RR */
222 char quality
[AST_MAX_USER_FIELD
];
231 typedef struct { unsigned int id
[4]; } __attribute__((packed
)) stun_trans_id
;
233 /* XXX Maybe stun belongs in another file if it ever has use outside of RTP */
235 unsigned short msgtype
;
236 unsigned short msglen
;
238 unsigned char ies
[0];
239 } __attribute__((packed
));
244 unsigned char value
[0];
245 } __attribute__((packed
));
248 unsigned char unused
;
249 unsigned char family
;
252 } __attribute__((packed
));
254 #define STUN_IGNORE (0)
255 #define STUN_ACCEPT (1)
257 #define STUN_BINDREQ 0x0001
258 #define STUN_BINDRESP 0x0101
259 #define STUN_BINDERR 0x0111
260 #define STUN_SECREQ 0x0002
261 #define STUN_SECRESP 0x0102
262 #define STUN_SECERR 0x0112
264 #define STUN_MAPPED_ADDRESS 0x0001
265 #define STUN_RESPONSE_ADDRESS 0x0002
266 #define STUN_CHANGE_REQUEST 0x0003
267 #define STUN_SOURCE_ADDRESS 0x0004
268 #define STUN_CHANGED_ADDRESS 0x0005
269 #define STUN_USERNAME 0x0006
270 #define STUN_PASSWORD 0x0007
271 #define STUN_MESSAGE_INTEGRITY 0x0008
272 #define STUN_ERROR_CODE 0x0009
273 #define STUN_UNKNOWN_ATTRIBUTES 0x000a
274 #define STUN_REFLECTED_FROM 0x000b
276 static const char *stun_msg2str(int msg
)
280 return "Binding Request";
282 return "Binding Response";
284 return "Binding Error Response";
286 return "Shared Secret Request";
288 return "Shared Secret Response";
290 return "Shared Secret Error Response";
292 return "Non-RFC3489 Message";
295 static const char *stun_attr2str(int msg
)
298 case STUN_MAPPED_ADDRESS
:
299 return "Mapped Address";
300 case STUN_RESPONSE_ADDRESS
:
301 return "Response Address";
302 case STUN_CHANGE_REQUEST
:
303 return "Change Request";
304 case STUN_SOURCE_ADDRESS
:
305 return "Source Address";
306 case STUN_CHANGED_ADDRESS
:
307 return "Changed Address";
312 case STUN_MESSAGE_INTEGRITY
:
313 return "Message Integrity";
314 case STUN_ERROR_CODE
:
316 case STUN_UNKNOWN_ATTRIBUTES
:
317 return "Unknown Attributes";
318 case STUN_REFLECTED_FROM
:
319 return "Reflected From";
321 return "Non-RFC3489 Attribute";
325 const char *username
;
326 const char *password
;
329 static int stun_process_attr(struct stun_state
*state
, struct stun_attr
*attr
)
332 ast_verbose("Found STUN Attribute %s (%04x), length %d\n",
333 stun_attr2str(ntohs(attr
->attr
)), ntohs(attr
->attr
), ntohs(attr
->len
));
334 switch(ntohs(attr
->attr
)) {
336 state
->username
= (const char *) (attr
->value
);
339 state
->password
= (const char *) (attr
->value
);
343 ast_verbose("Ignoring STUN attribute %s (%04x), length %d\n",
344 stun_attr2str(ntohs(attr
->attr
)), ntohs(attr
->attr
), ntohs(attr
->len
));
349 static void append_attr_string(struct stun_attr
**attr
, int attrval
, const char *s
, int *len
, int *left
)
351 int size
= sizeof(**attr
) + strlen(s
);
353 (*attr
)->attr
= htons(attrval
);
354 (*attr
)->len
= htons(strlen(s
));
355 memcpy((*attr
)->value
, s
, strlen(s
));
356 (*attr
) = (struct stun_attr
*)((*attr
)->value
+ strlen(s
));
362 static void append_attr_address(struct stun_attr
**attr
, int attrval
, struct sockaddr_in
*sin
, int *len
, int *left
)
364 int size
= sizeof(**attr
) + 8;
365 struct stun_addr
*addr
;
367 (*attr
)->attr
= htons(attrval
);
368 (*attr
)->len
= htons(8);
369 addr
= (struct stun_addr
*)((*attr
)->value
);
372 addr
->port
= sin
->sin_port
;
373 addr
->addr
= sin
->sin_addr
.s_addr
;
374 (*attr
) = (struct stun_attr
*)((*attr
)->value
+ 8);
380 static int stun_send(int s
, struct sockaddr_in
*dst
, struct stun_header
*resp
)
382 return sendto(s
, resp
, ntohs(resp
->msglen
) + sizeof(*resp
), 0,
383 (struct sockaddr
*)dst
, sizeof(*dst
));
386 static void stun_req_id(struct stun_header
*req
)
390 req
->id
.id
[x
] = ast_random();
393 size_t ast_rtp_alloc_size(void)
395 return sizeof(struct ast_rtp
);
398 void ast_rtp_stun_request(struct ast_rtp
*rtp
, struct sockaddr_in
*suggestion
, const char *username
)
400 struct stun_header
*req
;
401 unsigned char reqdata
[1024];
403 struct stun_attr
*attr
;
405 req
= (struct stun_header
*)reqdata
;
408 reqleft
= sizeof(reqdata
) - sizeof(struct stun_header
);
411 attr
= (struct stun_attr
*)req
->ies
;
413 append_attr_string(&attr
, STUN_USERNAME
, username
, &reqlen
, &reqleft
);
414 req
->msglen
= htons(reqlen
);
415 req
->msgtype
= htons(STUN_BINDREQ
);
416 stun_send(rtp
->s
, suggestion
, req
);
419 static int stun_handle_packet(int s
, struct sockaddr_in
*src
, unsigned char *data
, size_t len
)
421 struct stun_header
*resp
, *hdr
= (struct stun_header
*)data
;
422 struct stun_attr
*attr
;
423 struct stun_state st
;
424 int ret
= STUN_IGNORE
;
425 unsigned char respdata
[1024];
426 int resplen
, respleft
;
428 if (len
< sizeof(struct stun_header
)) {
430 ast_log(LOG_DEBUG
, "Runt STUN packet (only %zd, wanting at least %zd)\n", len
, sizeof(struct stun_header
));
434 ast_verbose("STUN Packet, msg %s (%04x), length: %d\n", stun_msg2str(ntohs(hdr
->msgtype
)), ntohs(hdr
->msgtype
), ntohs(hdr
->msglen
));
435 if (ntohs(hdr
->msglen
) > len
- sizeof(struct stun_header
)) {
437 ast_log(LOG_DEBUG
, "Scrambled STUN packet length (got %d, expecting %zd)\n", ntohs(hdr
->msglen
), len
- sizeof(struct stun_header
));
439 len
= ntohs(hdr
->msglen
);
440 data
+= sizeof(struct stun_header
);
441 memset(&st
, 0, sizeof(st
));
443 if (len
< sizeof(struct stun_attr
)) {
445 ast_log(LOG_DEBUG
, "Runt Attribute (got %zd, expecting %zd)\n", len
, sizeof(struct stun_attr
));
448 attr
= (struct stun_attr
*)data
;
449 if (ntohs(attr
->len
) > len
) {
451 ast_log(LOG_DEBUG
, "Inconsistent Attribute (length %d exceeds remaining msg len %zd)\n", ntohs(attr
->len
), len
);
454 if (stun_process_attr(&st
, attr
)) {
456 ast_log(LOG_DEBUG
, "Failed to handle attribute %s (%04x)\n", stun_attr2str(ntohs(attr
->attr
)), ntohs(attr
->attr
));
459 /* Clear attribute in case previous entry was a string */
461 data
+= ntohs(attr
->len
) + sizeof(struct stun_attr
);
462 len
-= ntohs(attr
->len
) + sizeof(struct stun_attr
);
464 /* Null terminate any string */
466 resp
= (struct stun_header
*)respdata
;
468 respleft
= sizeof(respdata
) - sizeof(struct stun_header
);
472 attr
= (struct stun_attr
*)resp
->ies
;
474 switch(ntohs(hdr
->msgtype
)) {
477 ast_verbose("STUN Bind Request, username: %s\n",
478 st
.username
? st
.username
: "<none>");
480 append_attr_string(&attr
, STUN_USERNAME
, st
.username
, &resplen
, &respleft
);
481 append_attr_address(&attr
, STUN_MAPPED_ADDRESS
, src
, &resplen
, &respleft
);
482 resp
->msglen
= htons(resplen
);
483 resp
->msgtype
= htons(STUN_BINDRESP
);
484 stun_send(s
, src
, resp
);
489 ast_verbose("Dunno what to do with STUN message %04x (%s)\n", ntohs(hdr
->msgtype
), stun_msg2str(ntohs(hdr
->msgtype
)));
495 /*! \brief List of current sessions */
496 static AST_LIST_HEAD_STATIC(protos
, ast_rtp_protocol
);
498 static void timeval2ntp(struct timeval tv
, unsigned int *msw
, unsigned int *lsw
)
500 unsigned int sec
, usec
, frac
;
501 sec
= tv
.tv_sec
+ 2208988800u; /* Sec between 1900 and 1970 */
503 frac
= (usec
<< 12) + (usec
<< 8) - ((usec
* 3650) >> 6);
508 int ast_rtp_fd(struct ast_rtp
*rtp
)
513 int ast_rtcp_fd(struct ast_rtp
*rtp
)
520 unsigned int ast_rtcp_calc_interval(struct ast_rtp
*rtp
)
522 unsigned int interval
;
523 /*! \todo XXX Do a more reasonable calculation on this one
524 * Look in RFC 3550 Section A.7 for an example*/
525 interval
= rtcpinterval
;
529 /* \brief Put RTP timeout timers on hold during another transaction, like T.38 */
530 void ast_rtp_set_rtptimers_onhold(struct ast_rtp
*rtp
)
532 rtp
->rtptimeout
= (-1) * rtp
->rtptimeout
;
533 rtp
->rtpholdtimeout
= (-1) * rtp
->rtpholdtimeout
;
536 /*! \brief Set rtp timeout */
537 void ast_rtp_set_rtptimeout(struct ast_rtp
*rtp
, int timeout
)
539 rtp
->rtptimeout
= timeout
;
542 /*! \brief Set rtp hold timeout */
543 void ast_rtp_set_rtpholdtimeout(struct ast_rtp
*rtp
, int timeout
)
545 rtp
->rtpholdtimeout
= timeout
;
548 /*! \brief set RTP keepalive interval */
549 void ast_rtp_set_rtpkeepalive(struct ast_rtp
*rtp
, int period
)
551 rtp
->rtpkeepalive
= period
;
554 /*! \brief Get rtp timeout */
555 int ast_rtp_get_rtptimeout(struct ast_rtp
*rtp
)
557 if (rtp
->rtptimeout
< 0) /* We're not checking, but remembering the setting (during T.38 transmission) */
559 return rtp
->rtptimeout
;
562 /*! \brief Get rtp hold timeout */
563 int ast_rtp_get_rtpholdtimeout(struct ast_rtp
*rtp
)
565 if (rtp
->rtptimeout
< 0) /* We're not checking, but remembering the setting (during T.38 transmission) */
567 return rtp
->rtpholdtimeout
;
570 /*! \brief Get RTP keepalive interval */
571 int ast_rtp_get_rtpkeepalive(struct ast_rtp
*rtp
)
573 return rtp
->rtpkeepalive
;
576 void ast_rtp_set_data(struct ast_rtp
*rtp
, void *data
)
581 void ast_rtp_set_callback(struct ast_rtp
*rtp
, ast_rtp_callback callback
)
583 rtp
->callback
= callback
;
586 void ast_rtp_setnat(struct ast_rtp
*rtp
, int nat
)
591 void ast_rtp_setdtmf(struct ast_rtp
*rtp
, int dtmf
)
593 ast_set2_flag(rtp
, dtmf
? 1 : 0, FLAG_HAS_DTMF
);
596 void ast_rtp_setdtmfcompensate(struct ast_rtp
*rtp
, int compensate
)
598 ast_set2_flag(rtp
, compensate
? 1 : 0, FLAG_DTMF_COMPENSATE
);
601 void ast_rtp_setstun(struct ast_rtp
*rtp
, int stun_enable
)
603 ast_set2_flag(rtp
, stun_enable
? 1 : 0, FLAG_HAS_STUN
);
606 static struct ast_frame
*send_dtmf(struct ast_rtp
*rtp
, enum ast_frame_type type
)
608 if (((ast_test_flag(rtp
, FLAG_DTMF_COMPENSATE
) && type
== AST_FRAME_DTMF_END
) ||
609 (type
== AST_FRAME_DTMF_BEGIN
)) && ast_tvcmp(ast_tvnow(), rtp
->dtmfmute
) < 0) {
611 ast_log(LOG_DEBUG
, "Ignore potential DTMF echo from '%s'\n", ast_inet_ntoa(rtp
->them
.sin_addr
));
613 rtp
->dtmfduration
= 0;
614 return &ast_null_frame
;
617 ast_log(LOG_DEBUG
, "Sending dtmf: %d (%c), at %s\n", rtp
->resp
, rtp
->resp
, ast_inet_ntoa(rtp
->them
.sin_addr
));
618 if (rtp
->resp
== 'X') {
619 rtp
->f
.frametype
= AST_FRAME_CONTROL
;
620 rtp
->f
.subclass
= AST_CONTROL_FLASH
;
622 rtp
->f
.frametype
= type
;
623 rtp
->f
.subclass
= rtp
->resp
;
633 static inline int rtp_debug_test_addr(struct sockaddr_in
*addr
)
637 if (rtpdebugaddr
.sin_addr
.s_addr
) {
638 if (((ntohs(rtpdebugaddr
.sin_port
) != 0)
639 && (rtpdebugaddr
.sin_port
!= addr
->sin_port
))
640 || (rtpdebugaddr
.sin_addr
.s_addr
!= addr
->sin_addr
.s_addr
))
646 static inline int rtcp_debug_test_addr(struct sockaddr_in
*addr
)
650 if (rtcpdebugaddr
.sin_addr
.s_addr
) {
651 if (((ntohs(rtcpdebugaddr
.sin_port
) != 0)
652 && (rtcpdebugaddr
.sin_port
!= addr
->sin_port
))
653 || (rtcpdebugaddr
.sin_addr
.s_addr
!= addr
->sin_addr
.s_addr
))
660 static struct ast_frame
*process_cisco_dtmf(struct ast_rtp
*rtp
, unsigned char *data
, int len
)
664 struct ast_frame
*f
= NULL
;
665 event
= ntohl(*((unsigned int *)(data
)));
667 if (option_debug
> 2 || rtpdebug
)
668 ast_log(LOG_DEBUG
, "Cisco DTMF Digit: %08x (len = %d)\n", event
, len
);
671 } else if (event
< 11) {
673 } else if (event
< 12) {
675 } else if (event
< 16) {
676 resp
= 'A' + (event
- 12);
677 } else if (event
< 17) {
680 if (rtp
->resp
&& (rtp
->resp
!= resp
)) {
681 f
= send_dtmf(rtp
, AST_FRAME_DTMF_END
);
684 rtp
->dtmfcount
= dtmftimeout
;
689 * \brief Process RTP DTMF and events according to RFC 2833.
691 * RFC 2833 is "RTP Payload for DTMF Digits, Telephony Tones and Telephony Signals".
699 static struct ast_frame
*process_rfc2833(struct ast_rtp
*rtp
, unsigned char *data
, int len
, unsigned int seqno
)
702 unsigned int event_end
;
703 unsigned int duration
;
705 struct ast_frame
*f
= NULL
;
707 /* Figure out event, event end, and duration */
708 event
= ntohl(*((unsigned int *)(data
)));
710 event_end
= ntohl(*((unsigned int *)(data
)));
713 duration
= ntohl(*((unsigned int *)(data
)));
716 /* Print out debug if turned on */
717 if (rtpdebug
|| option_debug
> 2)
718 ast_log(LOG_DEBUG
, "- RTP 2833 Event: %08x (len = %d)\n", event
, len
);
720 /* Figure out what digit was pressed */
723 } else if (event
< 11) {
725 } else if (event
< 12) {
727 } else if (event
< 16) {
728 resp
= 'A' + (event
- 12);
729 } else if (event
< 17) { /* Event 16: Hook flash */
733 if ((!(rtp
->resp
) && (!(event_end
& 0x80))) || (rtp
->resp
&& rtp
->resp
!= resp
)) {
735 if (!ast_test_flag(rtp
, FLAG_DTMF_COMPENSATE
))
736 f
= send_dtmf(rtp
, AST_FRAME_DTMF_BEGIN
);
737 } else if (event_end
& 0x80 && rtp
->lasteventendseqn
!= seqno
&& rtp
->resp
) {
738 f
= send_dtmf(rtp
, AST_FRAME_DTMF_END
);
739 f
->samples
= duration
;
741 rtp
->lasteventendseqn
= seqno
;
742 } else if (ast_test_flag(rtp
, FLAG_DTMF_COMPENSATE
) && event_end
& 0x80 && rtp
->lasteventendseqn
!= seqno
) {
744 f
= send_dtmf(rtp
, AST_FRAME_DTMF_END
);
745 f
->samples
= duration
;
747 rtp
->lasteventendseqn
= seqno
;
750 rtp
->dtmfcount
= dtmftimeout
;
751 rtp
->dtmfduration
= duration
;
757 * \brief Process Comfort Noise RTP.
759 * This is incomplete at the moment.
762 static struct ast_frame
*process_rfc3389(struct ast_rtp
*rtp
, unsigned char *data
, int len
)
764 struct ast_frame
*f
= NULL
;
765 /* Convert comfort noise into audio with various codecs. Unfortunately this doesn't
766 totally help us out becuase we don't have an engine to keep it going and we are not
767 guaranteed to have it every 20ms or anything */
769 ast_log(LOG_DEBUG
, "- RTP 3389 Comfort noise event: Level %d (len = %d)\n", rtp
->lastrxformat
, len
);
771 if (!(ast_test_flag(rtp
, FLAG_3389_WARNING
))) {
772 ast_log(LOG_NOTICE
, "Comfort noise support incomplete in Asterisk (RFC 3389). Please turn off on client if possible. Client IP: %s\n",
773 ast_inet_ntoa(rtp
->them
.sin_addr
));
774 ast_set_flag(rtp
, FLAG_3389_WARNING
);
777 /* Must have at least one byte */
781 rtp
->f
.data
= rtp
->rawdata
+ AST_FRIENDLY_OFFSET
;
782 rtp
->f
.datalen
= len
- 1;
783 rtp
->f
.offset
= AST_FRIENDLY_OFFSET
;
784 memcpy(rtp
->f
.data
, data
+ 1, len
- 1);
790 rtp
->f
.frametype
= AST_FRAME_CNG
;
791 rtp
->f
.subclass
= data
[0] & 0x7f;
792 rtp
->f
.datalen
= len
- 1;
794 rtp
->f
.delivery
.tv_usec
= rtp
->f
.delivery
.tv_sec
= 0;
799 static int rtpread(int *id
, int fd
, short events
, void *cbdata
)
801 struct ast_rtp
*rtp
= cbdata
;
803 f
= ast_rtp_read(rtp
);
806 rtp
->callback(rtp
, f
, rtp
->data
);
811 struct ast_frame
*ast_rtcp_read(struct ast_rtp
*rtp
)
814 int position
, i
, packetwords
;
816 struct sockaddr_in sin
;
817 unsigned int rtcpdata
[8192 + AST_FRIENDLY_OFFSET
];
818 unsigned int *rtcpheader
;
830 struct ast_frame
*f
= &ast_null_frame
;
832 if (!rtp
|| !rtp
->rtcp
)
833 return &ast_null_frame
;
837 res
= recvfrom(rtp
->rtcp
->s
, rtcpdata
+ AST_FRIENDLY_OFFSET
, sizeof(rtcpdata
) - sizeof(unsigned int) * AST_FRIENDLY_OFFSET
,
838 0, (struct sockaddr
*)&sin
, &len
);
839 rtcpheader
= (unsigned int *)(rtcpdata
+ AST_FRIENDLY_OFFSET
);
843 ast_log(LOG_WARNING
, "RTCP Read error: %s\n", strerror(errno
));
846 return &ast_null_frame
;
849 packetwords
= res
/ 4;
852 /* Send to whoever sent to us */
853 if ((rtp
->rtcp
->them
.sin_addr
.s_addr
!= sin
.sin_addr
.s_addr
) ||
854 (rtp
->rtcp
->them
.sin_port
!= sin
.sin_port
)) {
855 memcpy(&rtp
->rtcp
->them
, &sin
, sizeof(rtp
->rtcp
->them
));
856 if (option_debug
|| rtpdebug
)
857 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
));
862 ast_log(LOG_DEBUG
, "Got RTCP report of %d bytes\n", res
);
864 /* Process a compound packet */
866 while (position
< packetwords
) {
868 length
= ntohl(rtcpheader
[i
]);
869 pt
= (length
& 0xff0000) >> 16;
870 rc
= (length
& 0x1f000000) >> 24;
873 if ((i
+ length
) > packetwords
) {
874 ast_log(LOG_WARNING
, "RTCP Read too short\n");
875 return &ast_null_frame
;
878 if (rtcp_debug_test_addr(&sin
)) {
879 ast_verbose("\n\nGot RTCP from %s:%d\n", ast_inet_ntoa(sin
.sin_addr
), ntohs(sin
.sin_port
));
880 ast_verbose("PT: %d(%s)\n", pt
, (pt
== 200) ? "Sender Report" : (pt
== 201) ? "Receiver Report" : (pt
== 192) ? "H.261 FUR" : "Unknown");
881 ast_verbose("Reception reports: %d\n", rc
);
882 ast_verbose("SSRC of sender: %u\n", rtcpheader
[i
+ 1]);
885 i
+= 2; /* Advance past header and ssrc */
889 gettimeofday(&rtp
->rtcp
->rxlsr
,NULL
); /* To be able to populate the dlsr */
890 rtp
->rtcp
->spc
= ntohl(rtcpheader
[i
+3]);
891 rtp
->rtcp
->soc
= ntohl(rtcpheader
[i
+ 4]);
892 rtp
->rtcp
->themrxlsr
= ((ntohl(rtcpheader
[i
]) & 0x0000ffff) << 16) | ((ntohl(rtcpheader
[i
+ 1]) & 0xffff) >> 16); /* Going to LSR in RR*/
894 if (rtcp_debug_test_addr(&sin
)) {
895 ast_verbose("NTP timestamp: %lu.%010lu\n", (unsigned long) ntohl(rtcpheader
[i
]), (unsigned long) ntohl(rtcpheader
[i
+ 1]) * 4096);
896 ast_verbose("RTP timestamp: %lu\n", (unsigned long) ntohl(rtcpheader
[i
+ 2]));
897 ast_verbose("SPC: %lu\tSOC: %lu\n", (unsigned long) ntohl(rtcpheader
[i
+ 3]), (unsigned long) ntohl(rtcpheader
[i
+ 4]));
902 /* Intentional fall through */
904 /* This is the place to calculate RTT */
905 /* Don't handle multiple reception reports (rc > 1) yet */
906 gettimeofday(&now
, NULL
);
907 timeval2ntp(now
, &msw
, &lsw
);
908 /* Use the one we sent them in our SR instead, rtcp->txlsr could have been rewritten if the dlsr is large */
909 if (ntohl(rtcpheader
[i
+ 4])) { /* We must have the LSR */
910 comp
= ((msw
& 0xffff) << 16) | ((lsw
& 0xffff0000) >> 16);
911 a
= (double)((comp
& 0xffff0000) >> 16) + (double)((double)(comp
& 0xffff)/1000000.);
912 lsr
= (double)((ntohl(rtcpheader
[i
+ 4]) & 0xffff0000) >> 16) + (double)((double)(ntohl(rtcpheader
[i
+ 4]) & 0xffff) / 1000000.);
913 dlsr
= (double)(ntohl(rtcpheader
[i
+ 5])/65536.);
914 rtt
= a
- dlsr
- lsr
;
915 rtp
->rtcp
->accumulated_transit
+= rtt
;
916 rtp
->rtcp
->rtt
= rtt
;
917 if (rtp
->rtcp
->maxrtt
<rtt
)
918 rtp
->rtcp
->maxrtt
= rtt
;
919 if (rtp
->rtcp
->minrtt
>rtt
)
920 rtp
->rtcp
->minrtt
= rtt
;
922 rtp
->rtcp
->reported_jitter
= ntohl(rtcpheader
[i
+ 3]);
923 rtp
->rtcp
->reported_lost
= ntohl(rtcpheader
[i
+ 1]) & 0xffffff;
924 if (rtcp_debug_test_addr(&sin
)) {
925 ast_verbose("Fraction lost: %ld\n", (((long) ntohl(rtcpheader
[i
+ 1]) & 0xff000000) >> 24));
926 ast_verbose("Packets lost so far: %d\n", rtp
->rtcp
->reported_lost
);
927 ast_verbose("Highest sequence number: %ld\n", (long) (ntohl(rtcpheader
[i
+ 2]) & 0xffff));
928 ast_verbose("Sequence number cycles: %ld\n", (long) (ntohl(rtcpheader
[i
+ 2]) & 0xffff) >> 16);
929 ast_verbose("Interarrival jitter: %u\n", rtp
->rtcp
->reported_jitter
);
930 ast_verbose("Last SR(our NTP): %lu.%010lu\n",(unsigned long) ntohl(rtcpheader
[i
+ 4]) >> 16,((unsigned long) ntohl(rtcpheader
[i
+ 4]) << 16) * 4096);
931 ast_verbose("DLSR: %4.4f (sec)\n",ntohl(rtcpheader
[i
+ 5])/65536.0);
933 ast_verbose("RTT: %f(sec)\n", rtt
);
937 if (rtcp_debug_test_addr(&sin
))
938 ast_verbose("Received an RTCP Fast Update Request\n");
939 rtp
->f
.frametype
= AST_FRAME_CONTROL
;
940 rtp
->f
.subclass
= AST_CONTROL_VIDUPDATE
;
948 if (rtcp_debug_test_addr(&sin
))
949 ast_verbose("Received an SDES from %s:%d\n", ast_inet_ntoa(rtp
->rtcp
->them
.sin_addr
), ntohs(rtp
->rtcp
->them
.sin_port
));
952 if (rtcp_debug_test_addr(&sin
))
953 ast_verbose("Received a BYE from %s:%d\n", ast_inet_ntoa(rtp
->rtcp
->them
.sin_addr
), ntohs(rtp
->rtcp
->them
.sin_port
));
957 ast_log(LOG_DEBUG
, "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
));
960 position
+= (length
+ 1);
966 static void calc_rxstamp(struct timeval
*tv
, struct ast_rtp
*rtp
, unsigned int timestamp
, int mark
)
975 if ((!rtp
->rxcore
.tv_sec
&& !rtp
->rxcore
.tv_usec
) || mark
) {
976 gettimeofday(&rtp
->rxcore
, NULL
);
977 rtp
->drxcore
= (double) rtp
->rxcore
.tv_sec
+ (double) rtp
->rxcore
.tv_usec
/ 1000000;
978 /* map timestamp to a real time */
979 rtp
->seedrxts
= timestamp
; /* Their RTP timestamp started with this */
980 rtp
->rxcore
.tv_sec
-= timestamp
/ 8000;
981 rtp
->rxcore
.tv_usec
-= (timestamp
% 8000) * 125;
982 /* Round to 0.1ms for nice, pretty timestamps */
983 rtp
->rxcore
.tv_usec
-= rtp
->rxcore
.tv_usec
% 100;
984 if (rtp
->rxcore
.tv_usec
< 0) {
985 /* Adjust appropriately if necessary */
986 rtp
->rxcore
.tv_usec
+= 1000000;
987 rtp
->rxcore
.tv_sec
-= 1;
991 gettimeofday(&now
,NULL
);
992 /* rxcore is the mapping between the RTP timestamp and _our_ real time from gettimeofday() */
993 tv
->tv_sec
= rtp
->rxcore
.tv_sec
+ timestamp
/ 8000;
994 tv
->tv_usec
= rtp
->rxcore
.tv_usec
+ (timestamp
% 8000) * 125;
995 if (tv
->tv_usec
>= 1000000) {
996 tv
->tv_usec
-= 1000000;
999 prog
= (double)((timestamp
-rtp
->seedrxts
)/8000.);
1000 dtv
= (double)rtp
->drxcore
+ (double)(prog
);
1001 current_time
= (double)now
.tv_sec
+ (double)now
.tv_usec
/1000000;
1002 transit
= current_time
- dtv
;
1003 d
= transit
- rtp
->rxtransit
;
1004 rtp
->rxtransit
= transit
;
1007 rtp
->rxjitter
+= (1./16.) * (d
- rtp
->rxjitter
);
1008 if (rtp
->rtcp
&& rtp
->rxjitter
> rtp
->rtcp
->maxrxjitter
)
1009 rtp
->rtcp
->maxrxjitter
= rtp
->rxjitter
;
1010 if (rtp
->rtcp
&& rtp
->rxjitter
< rtp
->rtcp
->minrxjitter
)
1011 rtp
->rtcp
->minrxjitter
= rtp
->rxjitter
;
1014 /*! \brief Perform a Packet2Packet RTP write */
1015 static int bridge_p2p_rtp_write(struct ast_rtp
*rtp
, struct ast_rtp
*bridged
, unsigned int *rtpheader
, int len
, int hdrlen
)
1017 int res
= 0, payload
= 0, bridged_payload
= 0, version
, padding
, mark
, ext
;
1018 struct rtpPayloadType rtpPT
;
1021 /* Get fields from packet */
1022 seqno
= ntohl(rtpheader
[0]);
1023 version
= (seqno
& 0xC0000000) >> 30;
1024 payload
= (seqno
& 0x7f0000) >> 16;
1025 padding
= seqno
& (1 << 29);
1026 mark
= seqno
& (1 << 23);
1027 ext
= seqno
& (1 << 28);
1030 /* Check what the payload value should be */
1031 rtpPT
= ast_rtp_lookup_pt(rtp
, payload
);
1033 /* If the payload is DTMF, and we are listening for DTMF - then feed it into the core */
1034 if (ast_test_flag(rtp
, FLAG_P2P_NEED_DTMF
) && !rtpPT
.isAstFormat
&& rtpPT
.code
== AST_RTP_DTMF
)
1037 /* Otherwise adjust bridged payload to match */
1038 bridged_payload
= ast_rtp_lookup_code(bridged
, rtpPT
.isAstFormat
, rtpPT
.code
);
1040 /* If the mark bit has not been sent yet... do it now */
1041 if (!ast_test_flag(rtp
, FLAG_P2P_SENT_MARK
)) {
1043 ast_set_flag(rtp
, FLAG_P2P_SENT_MARK
);
1046 /* Reconstruct part of the packet */
1047 rtpheader
[0] = htonl((version
<< 30) | (mark
<< 23) | (bridged_payload
<< 16) | (seqno
));
1049 /* Send the packet back out */
1050 res
= sendto(bridged
->s
, (void *)rtpheader
, len
, 0, (struct sockaddr
*)&bridged
->them
, sizeof(bridged
->them
));
1052 if (!bridged
->nat
|| (bridged
->nat
&& (ast_test_flag(bridged
, FLAG_NAT_ACTIVE
) == FLAG_NAT_ACTIVE
))) {
1053 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
));
1054 } else if (((ast_test_flag(bridged
, FLAG_NAT_ACTIVE
) == FLAG_NAT_INACTIVE
) || rtpdebug
) && !ast_test_flag(bridged
, FLAG_NAT_INACTIVE_NOWARN
)) {
1055 if (option_debug
|| rtpdebug
)
1056 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
));
1057 ast_set_flag(bridged
, FLAG_NAT_INACTIVE_NOWARN
);
1060 } else if (rtp_debug_test_addr(&bridged
->them
))
1061 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
);
1066 struct ast_frame
*ast_rtp_read(struct ast_rtp
*rtp
)
1069 struct sockaddr_in sin
;
1080 unsigned int timestamp
;
1081 unsigned int *rtpheader
;
1082 struct rtpPayloadType rtpPT
;
1083 struct ast_rtp
*bridged
= NULL
;
1085 /* If time is up, kill it */
1086 if (rtp
->send_digit
)
1087 ast_rtp_senddigit_continuation(rtp
);
1091 /* Cache where the header will go */
1092 res
= recvfrom(rtp
->s
, rtp
->rawdata
+ AST_FRIENDLY_OFFSET
, sizeof(rtp
->rawdata
) - AST_FRIENDLY_OFFSET
,
1093 0, (struct sockaddr
*)&sin
, &len
);
1095 rtpheader
= (unsigned int *)(rtp
->rawdata
+ AST_FRIENDLY_OFFSET
);
1097 if (errno
!= EAGAIN
)
1098 ast_log(LOG_WARNING
, "RTP Read error: %s\n", strerror(errno
));
1101 return &ast_null_frame
;
1105 ast_log(LOG_WARNING
, "RTP Read too short\n");
1106 return &ast_null_frame
;
1110 seqno
= ntohl(rtpheader
[0]);
1112 /* Check RTP version */
1113 version
= (seqno
& 0xC0000000) >> 30;
1115 if ((stun_handle_packet(rtp
->s
, &sin
, rtp
->rawdata
+ AST_FRIENDLY_OFFSET
, res
) == STUN_ACCEPT
) &&
1116 (!rtp
->them
.sin_port
&& !rtp
->them
.sin_addr
.s_addr
)) {
1117 memcpy(&rtp
->them
, &sin
, sizeof(rtp
->them
));
1119 return &ast_null_frame
;
1122 #if 0 /* Allow to receive RTP stream with closed transmission path */
1123 /* If we don't have the other side's address, then ignore this */
1124 if (!rtp
->them
.sin_addr
.s_addr
|| !rtp
->them
.sin_port
)
1125 return &ast_null_frame
;
1128 /* Send to whoever send to us if NAT is turned on */
1130 if ((rtp
->them
.sin_addr
.s_addr
!= sin
.sin_addr
.s_addr
) ||
1131 (rtp
->them
.sin_port
!= sin
.sin_port
)) {
1134 memcpy(&rtp
->rtcp
->them
, &sin
, sizeof(rtp
->rtcp
->them
));
1135 rtp
->rtcp
->them
.sin_port
= htons(ntohs(rtp
->them
.sin_port
)+1);
1138 ast_set_flag(rtp
, FLAG_NAT_ACTIVE
);
1139 if (option_debug
|| rtpdebug
)
1140 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
));
1144 /* If we are bridged to another RTP stream, send direct */
1145 if ((bridged
= ast_rtp_get_bridged(rtp
)) && !bridge_p2p_rtp_write(rtp
, bridged
, rtpheader
, res
, hdrlen
))
1146 return &ast_null_frame
;
1149 return &ast_null_frame
;
1151 payloadtype
= (seqno
& 0x7f0000) >> 16;
1152 padding
= seqno
& (1 << 29);
1153 mark
= seqno
& (1 << 23);
1154 ext
= seqno
& (1 << 28);
1156 timestamp
= ntohl(rtpheader
[1]);
1157 ssrc
= ntohl(rtpheader
[2]);
1159 if (!mark
&& rtp
->rxssrc
&& rtp
->rxssrc
!= ssrc
) {
1160 if (option_debug
|| rtpdebug
)
1161 ast_log(LOG_DEBUG
, "Forcing Marker bit, because SSRC has changed\n");
1168 /* Remove padding bytes */
1169 res
-= rtp
->rawdata
[AST_FRIENDLY_OFFSET
+ res
- 1];
1173 /* RTP Extension present */
1175 hdrlen
+= (ntohl(rtpheader
[3]) & 0xffff) << 2;
1179 ast_log(LOG_WARNING
, "RTP Read too short (%d, expecting %d)\n", res
, hdrlen
);
1180 return &ast_null_frame
;
1183 rtp
->rxcount
++; /* Only count reasonably valid packets, this'll make the rtcp stats more accurate */
1185 tseqno
= rtp
->lastrxseqno
+1;
1187 if (rtp
->rxcount
==1) {
1188 /* This is the first RTP packet successfully received from source */
1189 rtp
->seedrxseqno
= seqno
;
1192 /* Do not schedule RR if RTCP isn't run */
1193 if (rtp
->rtcp
&& rtp
->rtcp
->them
.sin_addr
.s_addr
&& rtp
->rtcp
->schedid
< 1) {
1194 /* Schedule transmission of Receiver Report */
1195 rtp
->rtcp
->schedid
= ast_sched_add(rtp
->sched
, ast_rtcp_calc_interval(rtp
), ast_rtcp_write
, rtp
);
1198 if (tseqno
> RTP_SEQ_MOD
) { /* if tseqno is greater than RTP_SEQ_MOD it would indicate that the sender cycled */
1199 rtp
->cycles
+= RTP_SEQ_MOD
;
1200 ast_verbose("SEQNO cycled: %u\t%d\n", rtp
->cycles
, seqno
);
1203 rtp
->lastrxseqno
= seqno
;
1205 if (rtp
->themssrc
==0)
1206 rtp
->themssrc
= ntohl(rtpheader
[2]); /* Record their SSRC to put in future RR */
1208 if (rtp_debug_test_addr(&sin
))
1209 ast_verbose("Got RTP packet from %s:%d (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
1210 ast_inet_ntoa(sin
.sin_addr
), ntohs(sin
.sin_port
), payloadtype
, seqno
, timestamp
,res
- hdrlen
);
1212 rtpPT
= ast_rtp_lookup_pt(rtp
, payloadtype
);
1213 if (!rtpPT
.isAstFormat
) {
1214 struct ast_frame
*f
= NULL
;
1216 /* This is special in-band data that's not one of our codecs */
1217 if (rtpPT
.code
== AST_RTP_DTMF
) {
1218 /* It's special -- rfc2833 process it */
1219 if (rtp_debug_test_addr(&sin
)) {
1220 unsigned char *data
;
1222 unsigned int event_end
;
1223 unsigned int duration
;
1224 data
= rtp
->rawdata
+ AST_FRIENDLY_OFFSET
+ hdrlen
;
1225 event
= ntohl(*((unsigned int *)(data
)));
1227 event_end
= ntohl(*((unsigned int *)(data
)));
1230 duration
= ntohl(*((unsigned int *)(data
)));
1232 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
);
1234 f
= process_rfc2833(rtp
, rtp
->rawdata
+ AST_FRIENDLY_OFFSET
+ hdrlen
, res
- hdrlen
, seqno
);
1235 } else if (rtpPT
.code
== AST_RTP_CISCO_DTMF
) {
1236 /* It's really special -- process it the Cisco way */
1237 if (rtp
->lasteventseqn
<= seqno
|| (rtp
->lasteventseqn
>= 65530 && seqno
<= 6)) {
1238 f
= process_cisco_dtmf(rtp
, rtp
->rawdata
+ AST_FRIENDLY_OFFSET
+ hdrlen
, res
- hdrlen
);
1239 rtp
->lasteventseqn
= seqno
;
1241 } else if (rtpPT
.code
== AST_RTP_CN
) {
1243 f
= process_rfc3389(rtp
, rtp
->rawdata
+ AST_FRIENDLY_OFFSET
+ hdrlen
, res
- hdrlen
);
1245 ast_log(LOG_NOTICE
, "Unknown RTP codec %d received from '%s'\n", payloadtype
, ast_inet_ntoa(rtp
->them
.sin_addr
));
1247 return f
? f
: &ast_null_frame
;
1249 rtp
->lastrxformat
= rtp
->f
.subclass
= rtpPT
.code
;
1250 rtp
->f
.frametype
= (rtp
->f
.subclass
< AST_FORMAT_MAX_AUDIO
) ? AST_FRAME_VOICE
: AST_FRAME_VIDEO
;
1253 rtp
->lastrxts
= timestamp
;
1255 rtp
->rxseqno
= seqno
;
1257 /* Record received timestamp as last received now */
1258 rtp
->lastrxts
= timestamp
;
1261 rtp
->f
.datalen
= res
- hdrlen
;
1262 rtp
->f
.data
= rtp
->rawdata
+ hdrlen
+ AST_FRIENDLY_OFFSET
;
1263 rtp
->f
.offset
= hdrlen
+ AST_FRIENDLY_OFFSET
;
1264 if (rtp
->f
.subclass
< AST_FORMAT_MAX_AUDIO
) {
1265 rtp
->f
.samples
= ast_codec_get_samples(&rtp
->f
);
1266 if (rtp
->f
.subclass
== AST_FORMAT_SLINEAR
)
1267 ast_frame_byteswap_be(&rtp
->f
);
1268 calc_rxstamp(&rtp
->f
.delivery
, rtp
, timestamp
, mark
);
1269 /* Add timing data to let ast_generic_bridge() put the frame into a jitterbuf */
1270 rtp
->f
.has_timing_info
= 1;
1271 rtp
->f
.ts
= timestamp
/ 8;
1272 rtp
->f
.len
= rtp
->f
.samples
/ 8;
1273 rtp
->f
.seqno
= seqno
;
1275 /* Video -- samples is # of samples vs. 90000 */
1276 if (!rtp
->lastividtimestamp
)
1277 rtp
->lastividtimestamp
= timestamp
;
1278 rtp
->f
.samples
= timestamp
- rtp
->lastividtimestamp
;
1279 rtp
->lastividtimestamp
= timestamp
;
1280 rtp
->f
.delivery
.tv_sec
= 0;
1281 rtp
->f
.delivery
.tv_usec
= 0;
1283 rtp
->f
.subclass
|= 0x1;
1290 /* The following array defines the MIME Media type (and subtype) for each
1291 of our codecs, or RTP-specific data type. */
1293 struct rtpPayloadType payloadType
;
1297 {{1, AST_FORMAT_G723_1
}, "audio", "G723"},
1298 {{1, AST_FORMAT_GSM
}, "audio", "GSM"},
1299 {{1, AST_FORMAT_ULAW
}, "audio", "PCMU"},
1300 {{1, AST_FORMAT_ALAW
}, "audio", "PCMA"},
1301 {{1, AST_FORMAT_G726
}, "audio", "G726-32"},
1302 {{1, AST_FORMAT_ADPCM
}, "audio", "DVI4"},
1303 {{1, AST_FORMAT_SLINEAR
}, "audio", "L16"},
1304 {{1, AST_FORMAT_LPC10
}, "audio", "LPC"},
1305 {{1, AST_FORMAT_G729A
}, "audio", "G729"},
1306 {{1, AST_FORMAT_SPEEX
}, "audio", "speex"},
1307 {{1, AST_FORMAT_ILBC
}, "audio", "iLBC"},
1308 {{1, AST_FORMAT_G722
}, "audio", "G722"},
1309 {{1, AST_FORMAT_G726_AAL2
}, "audio", "AAL2-G726-32"},
1310 {{0, AST_RTP_DTMF
}, "audio", "telephone-event"},
1311 {{0, AST_RTP_CISCO_DTMF
}, "audio", "cisco-telephone-event"},
1312 {{0, AST_RTP_CN
}, "audio", "CN"},
1313 {{1, AST_FORMAT_JPEG
}, "video", "JPEG"},
1314 {{1, AST_FORMAT_PNG
}, "video", "PNG"},
1315 {{1, AST_FORMAT_H261
}, "video", "H261"},
1316 {{1, AST_FORMAT_H263
}, "video", "H263"},
1317 {{1, AST_FORMAT_H263_PLUS
}, "video", "h263-1998"},
1318 {{1, AST_FORMAT_H264
}, "video", "H264"},
1321 /* Static (i.e., well-known) RTP payload types for our "AST_FORMAT..."s:
1322 also, our own choices for dynamic payload types. This is our master
1323 table for transmission */
1324 static struct rtpPayloadType static_RTP_PT
[MAX_RTP_PT
] = {
1325 [0] = {1, AST_FORMAT_ULAW
},
1326 #ifdef USE_DEPRECATED_G726
1327 [2] = {1, AST_FORMAT_G726
}, /* Technically this is G.721, but if Cisco can do it, so can we... */
1329 [3] = {1, AST_FORMAT_GSM
},
1330 [4] = {1, AST_FORMAT_G723_1
},
1331 [5] = {1, AST_FORMAT_ADPCM
}, /* 8 kHz */
1332 [6] = {1, AST_FORMAT_ADPCM
}, /* 16 kHz */
1333 [7] = {1, AST_FORMAT_LPC10
},
1334 [8] = {1, AST_FORMAT_ALAW
},
1335 [9] = {1, AST_FORMAT_G722
},
1336 [10] = {1, AST_FORMAT_SLINEAR
}, /* 2 channels */
1337 [11] = {1, AST_FORMAT_SLINEAR
}, /* 1 channel */
1338 [13] = {0, AST_RTP_CN
},
1339 [16] = {1, AST_FORMAT_ADPCM
}, /* 11.025 kHz */
1340 [17] = {1, AST_FORMAT_ADPCM
}, /* 22.050 kHz */
1341 [18] = {1, AST_FORMAT_G729A
},
1342 [19] = {0, AST_RTP_CN
}, /* Also used for CN */
1343 [26] = {1, AST_FORMAT_JPEG
},
1344 [31] = {1, AST_FORMAT_H261
},
1345 [34] = {1, AST_FORMAT_H263
},
1346 [103] = {1, AST_FORMAT_H263_PLUS
},
1347 [97] = {1, AST_FORMAT_ILBC
},
1348 [99] = {1, AST_FORMAT_H264
},
1349 [101] = {0, AST_RTP_DTMF
},
1350 [110] = {1, AST_FORMAT_SPEEX
},
1351 [111] = {1, AST_FORMAT_G726
},
1352 [112] = {1, AST_FORMAT_G726_AAL2
},
1353 [121] = {0, AST_RTP_CISCO_DTMF
}, /* Must be type 121 */
1356 void ast_rtp_pt_clear(struct ast_rtp
* rtp
)
1363 ast_mutex_lock(&rtp
->bridge_lock
);
1365 for (i
= 0; i
< MAX_RTP_PT
; ++i
) {
1366 rtp
->current_RTP_PT
[i
].isAstFormat
= 0;
1367 rtp
->current_RTP_PT
[i
].code
= 0;
1370 rtp
->rtp_lookup_code_cache_isAstFormat
= 0;
1371 rtp
->rtp_lookup_code_cache_code
= 0;
1372 rtp
->rtp_lookup_code_cache_result
= 0;
1374 ast_mutex_unlock(&rtp
->bridge_lock
);
1377 void ast_rtp_pt_default(struct ast_rtp
* rtp
)
1381 ast_mutex_lock(&rtp
->bridge_lock
);
1383 /* Initialize to default payload types */
1384 for (i
= 0; i
< MAX_RTP_PT
; ++i
) {
1385 rtp
->current_RTP_PT
[i
].isAstFormat
= static_RTP_PT
[i
].isAstFormat
;
1386 rtp
->current_RTP_PT
[i
].code
= static_RTP_PT
[i
].code
;
1389 rtp
->rtp_lookup_code_cache_isAstFormat
= 0;
1390 rtp
->rtp_lookup_code_cache_code
= 0;
1391 rtp
->rtp_lookup_code_cache_result
= 0;
1393 ast_mutex_unlock(&rtp
->bridge_lock
);
1396 void ast_rtp_pt_copy(struct ast_rtp
*dest
, struct ast_rtp
*src
)
1400 ast_mutex_lock(&dest
->bridge_lock
);
1401 ast_mutex_lock(&src
->bridge_lock
);
1403 for (i
=0; i
< MAX_RTP_PT
; ++i
) {
1404 dest
->current_RTP_PT
[i
].isAstFormat
=
1405 src
->current_RTP_PT
[i
].isAstFormat
;
1406 dest
->current_RTP_PT
[i
].code
=
1407 src
->current_RTP_PT
[i
].code
;
1409 dest
->rtp_lookup_code_cache_isAstFormat
= 0;
1410 dest
->rtp_lookup_code_cache_code
= 0;
1411 dest
->rtp_lookup_code_cache_result
= 0;
1413 ast_mutex_unlock(&src
->bridge_lock
);
1414 ast_mutex_unlock(&dest
->bridge_lock
);
1417 /*! \brief Get channel driver interface structure */
1418 static struct ast_rtp_protocol
*get_proto(struct ast_channel
*chan
)
1420 struct ast_rtp_protocol
*cur
= NULL
;
1422 AST_LIST_LOCK(&protos
);
1423 AST_LIST_TRAVERSE(&protos
, cur
, list
) {
1424 if (cur
->type
== chan
->tech
->type
)
1427 AST_LIST_UNLOCK(&protos
);
1432 int ast_rtp_early_bridge(struct ast_channel
*dest
, struct ast_channel
*src
)
1434 struct ast_rtp
*destp
= NULL
, *srcp
= NULL
; /* Audio RTP Channels */
1435 struct ast_rtp
*vdestp
= NULL
, *vsrcp
= NULL
; /* Video RTP channels */
1436 struct ast_rtp_protocol
*destpr
= NULL
, *srcpr
= NULL
;
1437 enum ast_rtp_get_result audio_dest_res
= AST_RTP_GET_FAILED
, video_dest_res
= AST_RTP_GET_FAILED
;
1438 enum ast_rtp_get_result audio_src_res
= AST_RTP_GET_FAILED
, video_src_res
= AST_RTP_GET_FAILED
;
1439 int srccodec
, nat_active
= 0;
1442 ast_channel_lock(dest
);
1444 while(ast_channel_trylock(src
)) {
1445 ast_channel_unlock(dest
);
1447 ast_channel_lock(dest
);
1451 /* Find channel driver interfaces */
1452 destpr
= get_proto(dest
);
1454 srcpr
= get_proto(src
);
1457 ast_log(LOG_DEBUG
, "Channel '%s' has no RTP, not doing anything\n", dest
->name
);
1458 ast_channel_unlock(dest
);
1460 ast_channel_unlock(src
);
1465 ast_log(LOG_DEBUG
, "Channel '%s' has no RTP, not doing anything\n", src
? src
->name
: "<unspecified>");
1466 ast_channel_unlock(dest
);
1468 ast_channel_unlock(src
);
1472 /* Get audio and video interface (if native bridge is possible) */
1473 audio_dest_res
= destpr
->get_rtp_info(dest
, &destp
);
1474 video_dest_res
= destpr
->get_vrtp_info
? destpr
->get_vrtp_info(dest
, &vdestp
) : AST_RTP_GET_FAILED
;
1476 audio_src_res
= srcpr
->get_rtp_info(src
, &srcp
);
1477 video_src_res
= srcpr
->get_vrtp_info
? srcpr
->get_vrtp_info(src
, &vsrcp
) : AST_RTP_GET_FAILED
;
1480 /* Check if bridge is still possible (In SIP canreinvite=no stops this, like NAT) */
1481 if (audio_dest_res
!= AST_RTP_TRY_NATIVE
) {
1482 /* Somebody doesn't want to play... */
1483 ast_channel_unlock(dest
);
1485 ast_channel_unlock(src
);
1488 if (audio_src_res
== AST_RTP_TRY_NATIVE
&& srcpr
->get_codec
)
1489 srccodec
= srcpr
->get_codec(src
);
1492 /* Consider empty media as non-existant */
1493 if (audio_src_res
== AST_RTP_TRY_NATIVE
&& !srcp
->them
.sin_addr
.s_addr
)
1495 /* If the client has NAT stuff turned on then just safe NAT is active */
1496 if (srcp
&& (srcp
->nat
|| ast_test_flag(srcp
, FLAG_NAT_ACTIVE
)))
1498 /* Bridge media early */
1499 if (destpr
->set_rtp_peer(dest
, srcp
, vsrcp
, srccodec
, nat_active
))
1500 ast_log(LOG_WARNING
, "Channel '%s' failed to setup early bridge to '%s'\n", dest
->name
, src
? src
->name
: "<unspecified>");
1501 ast_channel_unlock(dest
);
1503 ast_channel_unlock(src
);
1505 ast_log(LOG_DEBUG
, "Setting early bridge SDP of '%s' with that of '%s'\n", dest
->name
, src
? src
->name
: "<unspecified>");
1509 int ast_rtp_make_compatible(struct ast_channel
*dest
, struct ast_channel
*src
, int media
)
1511 struct ast_rtp
*destp
= NULL
, *srcp
= NULL
; /* Audio RTP Channels */
1512 struct ast_rtp
*vdestp
= NULL
, *vsrcp
= NULL
; /* Video RTP channels */
1513 struct ast_rtp_protocol
*destpr
= NULL
, *srcpr
= NULL
;
1514 enum ast_rtp_get_result audio_dest_res
= AST_RTP_GET_FAILED
, video_dest_res
= AST_RTP_GET_FAILED
;
1515 enum ast_rtp_get_result audio_src_res
= AST_RTP_GET_FAILED
, video_src_res
= AST_RTP_GET_FAILED
;
1519 ast_channel_lock(dest
);
1520 while(ast_channel_trylock(src
)) {
1521 ast_channel_unlock(dest
);
1523 ast_channel_lock(dest
);
1526 /* Find channel driver interfaces */
1527 if (!(destpr
= get_proto(dest
))) {
1529 ast_log(LOG_DEBUG
, "Channel '%s' has no RTP, not doing anything\n", dest
->name
);
1530 ast_channel_unlock(dest
);
1531 ast_channel_unlock(src
);
1534 if (!(srcpr
= get_proto(src
))) {
1536 ast_log(LOG_DEBUG
, "Channel '%s' has no RTP, not doing anything\n", src
->name
);
1537 ast_channel_unlock(dest
);
1538 ast_channel_unlock(src
);
1542 /* Get audio and video interface (if native bridge is possible) */
1543 audio_dest_res
= destpr
->get_rtp_info(dest
, &destp
);
1544 video_dest_res
= destpr
->get_vrtp_info
? destpr
->get_vrtp_info(dest
, &vdestp
) : AST_RTP_GET_FAILED
;
1545 audio_src_res
= srcpr
->get_rtp_info(src
, &srcp
);
1546 video_src_res
= srcpr
->get_vrtp_info
? srcpr
->get_vrtp_info(src
, &vsrcp
) : AST_RTP_GET_FAILED
;
1548 /* Check if bridge is still possible (In SIP canreinvite=no stops this, like NAT) */
1549 if (audio_dest_res
!= AST_RTP_TRY_NATIVE
|| audio_src_res
!= AST_RTP_TRY_NATIVE
) {
1550 /* Somebody doesn't want to play... */
1551 ast_channel_unlock(dest
);
1552 ast_channel_unlock(src
);
1555 ast_rtp_pt_copy(destp
, srcp
);
1556 if (vdestp
&& vsrcp
)
1557 ast_rtp_pt_copy(vdestp
, vsrcp
);
1558 if (srcpr
->get_codec
)
1559 srccodec
= srcpr
->get_codec(src
);
1564 if (destpr
->set_rtp_peer(dest
, srcp
, vsrcp
, srccodec
, ast_test_flag(srcp
, FLAG_NAT_ACTIVE
)))
1565 ast_log(LOG_WARNING
, "Channel '%s' failed to setup early bridge to '%s'\n", dest
->name
, src
->name
);
1567 ast_channel_unlock(dest
);
1568 ast_channel_unlock(src
);
1570 ast_log(LOG_DEBUG
, "Seeded SDP of '%s' with that of '%s'\n", dest
->name
, src
->name
);
1574 /*! \brief Make a note of a RTP payload type that was seen in a SDP "m=" line.
1575 * By default, use the well-known value for this type (although it may
1576 * still be set to a different value by a subsequent "a=rtpmap:" line)
1578 void ast_rtp_set_m_type(struct ast_rtp
* rtp
, int pt
)
1580 if (pt
< 0 || pt
> MAX_RTP_PT
|| static_RTP_PT
[pt
].code
== 0)
1581 return; /* bogus payload type */
1583 ast_mutex_lock(&rtp
->bridge_lock
);
1584 rtp
->current_RTP_PT
[pt
] = static_RTP_PT
[pt
];
1585 ast_mutex_unlock(&rtp
->bridge_lock
);
1588 /*! \brief Make a note of a RTP payload type (with MIME type) that was seen in
1589 * an SDP "a=rtpmap:" line.
1591 void ast_rtp_set_rtpmap_type(struct ast_rtp
*rtp
, int pt
,
1592 char *mimeType
, char *mimeSubtype
,
1593 enum ast_rtp_options options
)
1597 if (pt
< 0 || pt
> MAX_RTP_PT
)
1598 return; /* bogus payload type */
1600 ast_mutex_lock(&rtp
->bridge_lock
);
1602 for (i
= 0; i
< sizeof(mimeTypes
)/sizeof(mimeTypes
[0]); ++i
) {
1603 if (strcasecmp(mimeSubtype
, mimeTypes
[i
].subtype
) == 0 &&
1604 strcasecmp(mimeType
, mimeTypes
[i
].type
) == 0) {
1605 rtp
->current_RTP_PT
[pt
] = mimeTypes
[i
].payloadType
;
1606 if ((mimeTypes
[i
].payloadType
.code
== AST_FORMAT_G726
) &&
1607 mimeTypes
[i
].payloadType
.isAstFormat
&&
1608 (options
& AST_RTP_OPT_G726_NONSTANDARD
))
1609 rtp
->current_RTP_PT
[pt
].code
= AST_FORMAT_G726_AAL2
;
1614 ast_mutex_unlock(&rtp
->bridge_lock
);
1619 /*! \brief Return the union of all of the codecs that were set by rtp_set...() calls
1620 * They're returned as two distinct sets: AST_FORMATs, and AST_RTPs */
1621 void ast_rtp_get_current_formats(struct ast_rtp
* rtp
,
1622 int* astFormats
, int* nonAstFormats
)
1626 ast_mutex_lock(&rtp
->bridge_lock
);
1628 *astFormats
= *nonAstFormats
= 0;
1629 for (pt
= 0; pt
< MAX_RTP_PT
; ++pt
) {
1630 if (rtp
->current_RTP_PT
[pt
].isAstFormat
) {
1631 *astFormats
|= rtp
->current_RTP_PT
[pt
].code
;
1633 *nonAstFormats
|= rtp
->current_RTP_PT
[pt
].code
;
1637 ast_mutex_unlock(&rtp
->bridge_lock
);
1642 struct rtpPayloadType
ast_rtp_lookup_pt(struct ast_rtp
* rtp
, int pt
)
1644 struct rtpPayloadType result
;
1646 result
.isAstFormat
= result
.code
= 0;
1648 if (pt
< 0 || pt
> MAX_RTP_PT
)
1649 return result
; /* bogus payload type */
1651 /* Start with negotiated codecs */
1652 ast_mutex_lock(&rtp
->bridge_lock
);
1653 result
= rtp
->current_RTP_PT
[pt
];
1654 ast_mutex_unlock(&rtp
->bridge_lock
);
1656 /* If it doesn't exist, check our static RTP type list, just in case */
1658 result
= static_RTP_PT
[pt
];
1663 /*! \brief Looks up an RTP code out of our *static* outbound list */
1664 int ast_rtp_lookup_code(struct ast_rtp
* rtp
, const int isAstFormat
, const int code
)
1668 ast_mutex_lock(&rtp
->bridge_lock
);
1670 if (isAstFormat
== rtp
->rtp_lookup_code_cache_isAstFormat
&&
1671 code
== rtp
->rtp_lookup_code_cache_code
) {
1672 /* Use our cached mapping, to avoid the overhead of the loop below */
1673 pt
= rtp
->rtp_lookup_code_cache_result
;
1674 ast_mutex_unlock(&rtp
->bridge_lock
);
1678 /* Check the dynamic list first */
1679 for (pt
= 0; pt
< MAX_RTP_PT
; ++pt
) {
1680 if (rtp
->current_RTP_PT
[pt
].code
== code
&& rtp
->current_RTP_PT
[pt
].isAstFormat
== isAstFormat
) {
1681 rtp
->rtp_lookup_code_cache_isAstFormat
= isAstFormat
;
1682 rtp
->rtp_lookup_code_cache_code
= code
;
1683 rtp
->rtp_lookup_code_cache_result
= pt
;
1684 ast_mutex_unlock(&rtp
->bridge_lock
);
1689 /* Then the static list */
1690 for (pt
= 0; pt
< MAX_RTP_PT
; ++pt
) {
1691 if (static_RTP_PT
[pt
].code
== code
&& static_RTP_PT
[pt
].isAstFormat
== isAstFormat
) {
1692 rtp
->rtp_lookup_code_cache_isAstFormat
= isAstFormat
;
1693 rtp
->rtp_lookup_code_cache_code
= code
;
1694 rtp
->rtp_lookup_code_cache_result
= pt
;
1695 ast_mutex_unlock(&rtp
->bridge_lock
);
1700 ast_mutex_unlock(&rtp
->bridge_lock
);
1705 const char *ast_rtp_lookup_mime_subtype(const int isAstFormat
, const int code
,
1706 enum ast_rtp_options options
)
1710 for (i
= 0; i
< sizeof(mimeTypes
)/sizeof(mimeTypes
[0]); ++i
) {
1711 if ((mimeTypes
[i
].payloadType
.code
== code
) && (mimeTypes
[i
].payloadType
.isAstFormat
== isAstFormat
)) {
1713 (code
== AST_FORMAT_G726_AAL2
) &&
1714 (options
& AST_RTP_OPT_G726_NONSTANDARD
))
1717 return mimeTypes
[i
].subtype
;
1724 char *ast_rtp_lookup_mime_multiple(char *buf
, size_t size
, const int capability
,
1725 const int isAstFormat
, enum ast_rtp_options options
)
1735 snprintf(end
, size
, "0x%x (", capability
);
1742 for (format
= 1; format
< AST_RTP_MAX
; format
<<= 1) {
1743 if (capability
& format
) {
1744 const char *name
= ast_rtp_lookup_mime_subtype(isAstFormat
, format
, options
);
1746 snprintf(end
, size
, "%s|", name
);
1754 snprintf(start
, size
, "nothing)");
1761 static int rtp_socket(void)
1765 s
= socket(AF_INET
, SOCK_DGRAM
, 0);
1767 flags
= fcntl(s
, F_GETFL
);
1768 fcntl(s
, F_SETFL
, flags
| O_NONBLOCK
);
1771 setsockopt(s
, SOL_SOCKET
, SO_NO_CHECK
, &nochecksums
, sizeof(nochecksums
));
1778 * \brief Initialize a new RTCP session.
1780 * \returns The newly initialized RTCP session.
1782 static struct ast_rtcp
*ast_rtcp_new(void)
1784 struct ast_rtcp
*rtcp
;
1786 if (!(rtcp
= ast_calloc(1, sizeof(*rtcp
))))
1788 rtcp
->s
= rtp_socket();
1789 rtcp
->us
.sin_family
= AF_INET
;
1790 rtcp
->them
.sin_family
= AF_INET
;
1794 ast_log(LOG_WARNING
, "Unable to allocate RTCP socket: %s\n", strerror(errno
));
1802 * \brief Initialize a new RTP structure.
1805 void ast_rtp_new_init(struct ast_rtp
*rtp
)
1807 ast_mutex_init(&rtp
->bridge_lock
);
1809 rtp
->them
.sin_family
= AF_INET
;
1810 rtp
->us
.sin_family
= AF_INET
;
1811 rtp
->ssrc
= ast_random();
1812 rtp
->seqno
= ast_random() & 0xffff;
1813 ast_set_flag(rtp
, FLAG_HAS_DTMF
);
1818 struct ast_rtp
*ast_rtp_new_with_bindaddr(struct sched_context
*sched
, struct io_context
*io
, int rtcpenable
, int callbackmode
, struct in_addr addr
)
1820 struct ast_rtp
*rtp
;
1825 if (!(rtp
= ast_calloc(1, sizeof(*rtp
))))
1828 ast_rtp_new_init(rtp
);
1830 rtp
->s
= rtp_socket();
1833 ast_log(LOG_ERROR
, "Unable to allocate socket: %s\n", strerror(errno
));
1836 if (sched
&& rtcpenable
) {
1838 rtp
->rtcp
= ast_rtcp_new();
1841 /* Select a random port number in the range of possible RTP */
1842 x
= (ast_random() % (rtpend
-rtpstart
)) + rtpstart
;
1844 /* Save it for future references. */
1846 /* Iterate tring to bind that port and incrementing it otherwise untill a port was found or no ports are available. */
1848 /* Must be an even port number by RTP spec */
1849 rtp
->us
.sin_port
= htons(x
);
1850 rtp
->us
.sin_addr
= addr
;
1851 /* If there's rtcp, initialize it as well. */
1853 rtp
->rtcp
->us
.sin_port
= htons(x
+ 1);
1854 rtp
->rtcp
->us
.sin_addr
= addr
;
1856 /* Try to bind it/them. */
1857 if (!(first
= bind(rtp
->s
, (struct sockaddr
*)&rtp
->us
, sizeof(rtp
->us
))) &&
1858 (!rtp
->rtcp
|| !bind(rtp
->rtcp
->s
, (struct sockaddr
*)&rtp
->rtcp
->us
, sizeof(rtp
->rtcp
->us
))))
1861 /* Primary bind succeeded! Gotta recreate it */
1863 rtp
->s
= rtp_socket();
1865 if (errno
!= EADDRINUSE
) {
1866 /* We got an error that wasn't expected, abort! */
1867 ast_log(LOG_ERROR
, "Unexpected bind error: %s\n", strerror(errno
));
1870 close(rtp
->rtcp
->s
);
1876 /* The port was used, increment it (by two). */
1878 /* Did we go over the limit ? */
1880 /* then, start from the begingig. */
1881 x
= (rtpstart
+ 1) & ~1;
1882 /* Check if we reached the place were we started. */
1883 if (x
== startplace
) {
1884 /* If so, there's no ports available. */
1885 ast_log(LOG_ERROR
, "No RTP ports remaining. Can't setup media stream for this call.\n");
1888 close(rtp
->rtcp
->s
);
1898 rtp
->ioid
= ast_io_add(rtp
->io
, rtp
->s
, rtpread
, AST_IO_IN
, rtp
);
1899 ast_set_flag(rtp
, FLAG_CALLBACK_MODE
);
1901 ast_rtp_pt_default(rtp
);
1905 struct ast_rtp
*ast_rtp_new(struct sched_context
*sched
, struct io_context
*io
, int rtcpenable
, int callbackmode
)
1909 memset(&ia
, 0, sizeof(ia
));
1910 return ast_rtp_new_with_bindaddr(sched
, io
, rtcpenable
, callbackmode
, ia
);
1913 int ast_rtp_settos(struct ast_rtp
*rtp
, int tos
)
1917 if ((res
= setsockopt(rtp
->s
, IPPROTO_IP
, IP_TOS
, &tos
, sizeof(tos
))))
1918 ast_log(LOG_WARNING
, "Unable to set TOS to %d\n", tos
);
1922 void ast_rtp_set_peer(struct ast_rtp
*rtp
, struct sockaddr_in
*them
)
1924 rtp
->them
.sin_port
= them
->sin_port
;
1925 rtp
->them
.sin_addr
= them
->sin_addr
;
1927 rtp
->rtcp
->them
.sin_port
= htons(ntohs(them
->sin_port
) + 1);
1928 rtp
->rtcp
->them
.sin_addr
= them
->sin_addr
;
1933 int ast_rtp_get_peer(struct ast_rtp
*rtp
, struct sockaddr_in
*them
)
1935 if ((them
->sin_family
!= AF_INET
) ||
1936 (them
->sin_port
!= rtp
->them
.sin_port
) ||
1937 (them
->sin_addr
.s_addr
!= rtp
->them
.sin_addr
.s_addr
)) {
1938 them
->sin_family
= AF_INET
;
1939 them
->sin_port
= rtp
->them
.sin_port
;
1940 them
->sin_addr
= rtp
->them
.sin_addr
;
1946 void ast_rtp_get_us(struct ast_rtp
*rtp
, struct sockaddr_in
*us
)
1951 struct ast_rtp
*ast_rtp_get_bridged(struct ast_rtp
*rtp
)
1953 struct ast_rtp
*bridged
= NULL
;
1955 ast_mutex_lock(&rtp
->bridge_lock
);
1956 bridged
= rtp
->bridged
;
1957 ast_mutex_unlock(&rtp
->bridge_lock
);
1962 void ast_rtp_stop(struct ast_rtp
*rtp
)
1964 if (rtp
->rtcp
&& rtp
->rtcp
->schedid
> 0) {
1965 ast_sched_del(rtp
->sched
, rtp
->rtcp
->schedid
);
1966 rtp
->rtcp
->schedid
= -1;
1969 memset(&rtp
->them
.sin_addr
, 0, sizeof(rtp
->them
.sin_addr
));
1970 memset(&rtp
->them
.sin_port
, 0, sizeof(rtp
->them
.sin_port
));
1972 memset(&rtp
->rtcp
->them
.sin_addr
, 0, sizeof(rtp
->rtcp
->them
.sin_addr
));
1973 memset(&rtp
->rtcp
->them
.sin_port
, 0, sizeof(rtp
->rtcp
->them
.sin_port
));
1976 ast_clear_flag(rtp
, FLAG_P2P_SENT_MARK
);
1979 void ast_rtp_reset(struct ast_rtp
*rtp
)
1981 memset(&rtp
->rxcore
, 0, sizeof(rtp
->rxcore
));
1982 memset(&rtp
->txcore
, 0, sizeof(rtp
->txcore
));
1983 memset(&rtp
->dtmfmute
, 0, sizeof(rtp
->dtmfmute
));
1985 rtp
->lastdigitts
= 0;
1987 rtp
->lastividtimestamp
= 0;
1988 rtp
->lastovidtimestamp
= 0;
1989 rtp
->lasteventseqn
= 0;
1990 rtp
->lasteventendseqn
= 0;
1991 rtp
->lasttxformat
= 0;
1992 rtp
->lastrxformat
= 0;
1994 rtp
->dtmfduration
= 0;
1999 char *ast_rtp_get_quality(struct ast_rtp
*rtp
)
2003 *themssrc their ssrc
2005 *rxjitter our calculated jitter(rx)
2006 *rxcount no. received packets
2007 *txjitter reported jitter of the other end
2008 *txcount transmitted packets
2009 *rlp remote lost packets
2012 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
);
2014 return rtp
->rtcp
->quality
;
2017 void ast_rtp_destroy(struct ast_rtp
*rtp
)
2019 if (rtcp_debug_test_addr(&rtp
->them
) || rtcpstats
) {
2020 /*Print some info on the call here */
2021 ast_verbose(" RTP-stats\n");
2022 ast_verbose("* Our Receiver:\n");
2023 ast_verbose(" SSRC: %u\n", rtp
->themssrc
);
2024 ast_verbose(" Received packets: %u\n", rtp
->rxcount
);
2025 ast_verbose(" Lost packets: %u\n", rtp
->rtcp
->expected_prior
- rtp
->rtcp
->received_prior
);
2026 ast_verbose(" Jitter: %.4f\n", rtp
->rxjitter
);
2027 ast_verbose(" Transit: %.4f\n", rtp
->rxtransit
);
2028 ast_verbose(" RR-count: %u\n", rtp
->rtcp
->rr_count
);
2029 ast_verbose("* Our Sender:\n");
2030 ast_verbose(" SSRC: %u\n", rtp
->ssrc
);
2031 ast_verbose(" Sent packets: %u\n", rtp
->txcount
);
2032 ast_verbose(" Lost packets: %u\n", rtp
->rtcp
->reported_lost
);
2033 ast_verbose(" Jitter: %u\n", rtp
->rtcp
->reported_jitter
);
2034 ast_verbose(" SR-count: %u\n", rtp
->rtcp
->sr_count
);
2035 ast_verbose(" RTT: %f\n", rtp
->rtcp
->rtt
);
2039 ast_smoother_free(rtp
->smoother
);
2041 ast_io_remove(rtp
->io
, rtp
->ioid
);
2045 if (rtp
->rtcp
->schedid
> 0)
2046 ast_sched_del(rtp
->sched
, rtp
->rtcp
->schedid
);
2047 close(rtp
->rtcp
->s
);
2052 ast_mutex_destroy(&rtp
->bridge_lock
);
2057 static unsigned int calc_txstamp(struct ast_rtp
*rtp
, struct timeval
*delivery
)
2061 if (ast_tvzero(rtp
->txcore
)) {
2062 rtp
->txcore
= ast_tvnow();
2063 /* Round to 20ms for nice, pretty timestamps */
2064 rtp
->txcore
.tv_usec
-= rtp
->txcore
.tv_usec
% 20000;
2066 /* Use previous txcore if available */
2067 t
= (delivery
&& !ast_tvzero(*delivery
)) ? *delivery
: ast_tvnow();
2068 ms
= ast_tvdiff_ms(t
, rtp
->txcore
);
2071 /* Use what we just got for next time */
2073 return (unsigned int) ms
;
2076 /*! \brief Send begin frames for DTMF */
2077 int ast_rtp_senddigit_begin(struct ast_rtp
*rtp
, char digit
)
2079 unsigned int *rtpheader
;
2080 int hdrlen
= 12, res
= 0, i
= 0, payload
= 0;
2083 if ((digit
<= '9') && (digit
>= '0'))
2085 else if (digit
== '*')
2087 else if (digit
== '#')
2089 else if ((digit
>= 'A') && (digit
<= 'D'))
2090 digit
= digit
- 'A' + 12;
2091 else if ((digit
>= 'a') && (digit
<= 'd'))
2092 digit
= digit
- 'a' + 12;
2094 ast_log(LOG_WARNING
, "Don't know how to represent '%c'\n", digit
);
2098 /* If we have no peer, return immediately */
2099 if (!rtp
->them
.sin_addr
.s_addr
|| !rtp
->them
.sin_port
)
2102 payload
= ast_rtp_lookup_code(rtp
, 0, AST_RTP_DTMF
);
2104 rtp
->dtmfmute
= ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
2105 rtp
->send_duration
= 160;
2107 /* Get a pointer to the header */
2108 rtpheader
= (unsigned int *)data
;
2109 rtpheader
[0] = htonl((2 << 30) | (1 << 23) | (payload
<< 16) | (rtp
->seqno
));
2110 rtpheader
[1] = htonl(rtp
->lastdigitts
);
2111 rtpheader
[2] = htonl(rtp
->ssrc
);
2113 for (i
= 0; i
< 2; i
++) {
2114 rtpheader
[3] = htonl((digit
<< 24) | (0xa << 16) | (rtp
->send_duration
));
2115 res
= sendto(rtp
->s
, (void *) rtpheader
, hdrlen
+ 4, 0, (struct sockaddr
*) &rtp
->them
, sizeof(rtp
->them
));
2117 ast_log(LOG_ERROR
, "RTP Transmission error to %s:%d: %s\n",
2118 ast_inet_ntoa(rtp
->them
.sin_addr
),
2119 ntohs(rtp
->them
.sin_port
), strerror(errno
));
2120 if (rtp_debug_test_addr(&rtp
->them
))
2121 ast_verbose("Sent RTP DTMF packet to %s:%d (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
2122 ast_inet_ntoa(rtp
->them
.sin_addr
),
2123 ntohs(rtp
->them
.sin_port
), payload
, rtp
->seqno
, rtp
->lastdigitts
, res
- hdrlen
);
2124 /* Increment sequence number */
2126 /* Increment duration */
2127 rtp
->send_duration
+= 160;
2128 /* Clear marker bit and set seqno */
2129 rtpheader
[0] = htonl((2 << 30) | (payload
<< 16) | (rtp
->seqno
));
2132 /* Since we received a begin, we can safely store the digit and disable any compensation */
2133 rtp
->send_digit
= digit
;
2134 rtp
->send_payload
= payload
;
2139 /*! \brief Send continuation frame for DTMF */
2140 static int ast_rtp_senddigit_continuation(struct ast_rtp
*rtp
)
2142 unsigned int *rtpheader
;
2143 int hdrlen
= 12, res
= 0;
2146 if (!rtp
->them
.sin_addr
.s_addr
|| !rtp
->them
.sin_port
)
2149 /* Setup packet to send */
2150 rtpheader
= (unsigned int *)data
;
2151 rtpheader
[0] = htonl((2 << 30) | (1 << 23) | (rtp
->send_payload
<< 16) | (rtp
->seqno
));
2152 rtpheader
[1] = htonl(rtp
->lastdigitts
);
2153 rtpheader
[2] = htonl(rtp
->ssrc
);
2154 rtpheader
[3] = htonl((rtp
->send_digit
<< 24) | (0xa << 16) | (rtp
->send_duration
));
2155 rtpheader
[0] = htonl((2 << 30) | (rtp
->send_payload
<< 16) | (rtp
->seqno
));
2158 res
= sendto(rtp
->s
, (void *) rtpheader
, hdrlen
+ 4, 0, (struct sockaddr
*) &rtp
->them
, sizeof(rtp
->them
));
2160 ast_log(LOG_ERROR
, "RTP Transmission error to %s:%d: %s\n",
2161 ast_inet_ntoa(rtp
->them
.sin_addr
),
2162 ntohs(rtp
->them
.sin_port
), strerror(errno
));
2163 if (rtp_debug_test_addr(&rtp
->them
))
2164 ast_verbose("Sent RTP DTMF packet to %s:%d (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
2165 ast_inet_ntoa(rtp
->them
.sin_addr
),
2166 ntohs(rtp
->them
.sin_port
), rtp
->send_payload
, rtp
->seqno
, rtp
->lastdigitts
, res
- hdrlen
);
2168 /* Increment sequence number */
2170 /* Increment duration */
2171 rtp
->send_duration
+= 160;
2176 /*! \brief Send end packets for DTMF */
2177 int ast_rtp_senddigit_end(struct ast_rtp
*rtp
, char digit
)
2179 unsigned int *rtpheader
;
2180 int hdrlen
= 12, res
= 0, i
= 0;
2183 /* If no address, then bail out */
2184 if (!rtp
->them
.sin_addr
.s_addr
|| !rtp
->them
.sin_port
)
2187 if ((digit
<= '9') && (digit
>= '0'))
2189 else if (digit
== '*')
2191 else if (digit
== '#')
2193 else if ((digit
>= 'A') && (digit
<= 'D'))
2194 digit
= digit
- 'A' + 12;
2195 else if ((digit
>= 'a') && (digit
<= 'd'))
2196 digit
= digit
- 'a' + 12;
2198 ast_log(LOG_WARNING
, "Don't know how to represent '%c'\n", digit
);
2202 rtp
->dtmfmute
= ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
2204 rtpheader
= (unsigned int *)data
;
2205 rtpheader
[0] = htonl((2 << 30) | (1 << 23) | (rtp
->send_payload
<< 16) | (rtp
->seqno
));
2206 rtpheader
[1] = htonl(rtp
->lastdigitts
);
2207 rtpheader
[2] = htonl(rtp
->ssrc
);
2208 rtpheader
[3] = htonl((digit
<< 24) | (0xa << 16) | (rtp
->send_duration
));
2210 rtpheader
[3] |= htonl((1 << 23));
2211 rtpheader
[0] = htonl((2 << 30) | (rtp
->send_payload
<< 16) | (rtp
->seqno
));
2212 /* Send 3 termination packets */
2213 for (i
= 0; i
< 3; i
++) {
2214 res
= sendto(rtp
->s
, (void *) rtpheader
, hdrlen
+ 4, 0, (struct sockaddr
*) &rtp
->them
, sizeof(rtp
->them
));
2216 ast_log(LOG_ERROR
, "RTP Transmission error to %s:%d: %s\n",
2217 ast_inet_ntoa(rtp
->them
.sin_addr
),
2218 ntohs(rtp
->them
.sin_port
), strerror(errno
));
2219 if (rtp_debug_test_addr(&rtp
->them
))
2220 ast_verbose("Sent RTP DTMF packet to %s:%d (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
2221 ast_inet_ntoa(rtp
->them
.sin_addr
),
2222 ntohs(rtp
->them
.sin_port
), rtp
->send_payload
, rtp
->seqno
, rtp
->lastdigitts
, res
- hdrlen
);
2224 rtp
->send_digit
= 0;
2225 /* Increment lastdigitts */
2226 rtp
->lastdigitts
+= 960;
2232 /*! \brief Public function: Send an H.261 fast update request, some devices need this rather than SIP XML */
2233 int ast_rtcp_send_h261fur(void *data
)
2235 struct ast_rtp
*rtp
= data
;
2238 rtp
->rtcp
->sendfur
= 1;
2239 res
= ast_rtcp_write(data
);
2244 /*! \brief Send RTCP sender's report */
2245 static int ast_rtcp_write_sr(void *data
)
2247 struct ast_rtp
*rtp
= data
;
2251 unsigned int now_lsw
;
2252 unsigned int now_msw
;
2253 unsigned int *rtcpheader
;
2255 unsigned int extended
;
2256 unsigned int expected
;
2257 unsigned int expected_interval
;
2258 unsigned int received_interval
;
2261 struct timeval dlsr
;
2264 /* Commented condition is always not NULL if rtp->rtcp is not NULL */
2265 if (!rtp
|| !rtp
->rtcp
/* || (&rtp->rtcp->them.sin_addr == 0)*/)
2268 if (!rtp
->rtcp
->them
.sin_addr
.s_addr
) { /* This'll stop rtcp for this rtp session */
2269 ast_verbose("RTCP SR transmission error, rtcp halted\n");
2270 if (rtp
->rtcp
->schedid
> 0)
2271 ast_sched_del(rtp
->sched
, rtp
->rtcp
->schedid
);
2272 rtp
->rtcp
->schedid
= -1;
2276 gettimeofday(&now
, NULL
);
2277 timeval2ntp(now
, &now_msw
, &now_lsw
); /* fill thses ones in from utils.c*/
2278 rtcpheader
= (unsigned int *)bdata
;
2279 rtcpheader
[1] = htonl(rtp
->ssrc
); /* Our SSRC */
2280 rtcpheader
[2] = htonl(now_msw
); /* now, MSW. gettimeofday() + SEC_BETWEEN_1900_AND_1970*/
2281 rtcpheader
[3] = htonl(now_lsw
); /* now, LSW */
2282 rtcpheader
[4] = htonl(rtp
->lastts
); /* FIXME shouldn't be that, it should be now */
2283 rtcpheader
[5] = htonl(rtp
->txcount
); /* No. packets sent */
2284 rtcpheader
[6] = htonl(rtp
->txoctetcount
); /* No. bytes sent */
2287 extended
= rtp
->cycles
+ rtp
->lastrxseqno
;
2288 expected
= extended
- rtp
->seedrxseqno
+ 1;
2289 if (rtp
->rxcount
> expected
)
2290 expected
+= rtp
->rxcount
- expected
;
2291 lost
= expected
- rtp
->rxcount
;
2292 expected_interval
= expected
- rtp
->rtcp
->expected_prior
;
2293 rtp
->rtcp
->expected_prior
= expected
;
2294 received_interval
= rtp
->rxcount
- rtp
->rtcp
->received_prior
;
2295 rtp
->rtcp
->received_prior
= rtp
->rxcount
;
2296 lost_interval
= expected_interval
- received_interval
;
2297 if (expected_interval
== 0 || lost_interval
<= 0)
2300 fraction
= (lost_interval
<< 8) / expected_interval
;
2301 timersub(&now
, &rtp
->rtcp
->rxlsr
, &dlsr
);
2302 rtcpheader
[7] = htonl(rtp
->themssrc
);
2303 rtcpheader
[8] = htonl(((fraction
& 0xff) << 24) | (lost
& 0xffffff));
2304 rtcpheader
[9] = htonl((rtp
->cycles
) | ((rtp
->lastrxseqno
& 0xffff)));
2305 rtcpheader
[10] = htonl((unsigned int)rtp
->rxjitter
);
2306 rtcpheader
[11] = htonl(rtp
->rtcp
->themrxlsr
);
2307 rtcpheader
[12] = htonl((((dlsr
.tv_sec
* 1000) + (dlsr
.tv_usec
/ 1000)) * 65536) / 1000);
2310 rtcpheader
[0] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SR
<< 16) | ((len
/4)-1));
2312 if (rtp
->rtcp
->sendfur
) {
2313 rtcpheader
[13] = htonl((2 << 30) | (0 << 24) | (RTCP_PT_FUR
<< 16) | 1);
2314 rtcpheader
[14] = htonl(rtp
->ssrc
); /* Our SSRC */
2316 rtp
->rtcp
->sendfur
= 0;
2319 /* Insert SDES here. Probably should make SDES text equal to mimetypes[code].type (not subtype 'cos */
2320 /* it can change mid call, and SDES can't) */
2321 rtcpheader
[len
/4] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SDES
<< 16) | 2);
2322 rtcpheader
[(len
/4)+1] = htonl(rtp
->ssrc
); /* Our SSRC */
2323 rtcpheader
[(len
/4)+2] = htonl(0x01 << 24); /* Empty for the moment */
2326 res
= sendto(rtp
->rtcp
->s
, (unsigned int *)rtcpheader
, len
, 0, (struct sockaddr
*)&rtp
->rtcp
->them
, sizeof(rtp
->rtcp
->them
));
2328 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
));
2329 if (rtp
->rtcp
->schedid
> 0)
2330 ast_sched_del(rtp
->sched
, rtp
->rtcp
->schedid
);
2331 rtp
->rtcp
->schedid
= -1;
2335 /* FIXME Don't need to get a new one */
2336 gettimeofday(&rtp
->rtcp
->txlsr
, NULL
);
2337 rtp
->rtcp
->sr_count
++;
2339 rtp
->rtcp
->lastsrtxcount
= rtp
->txcount
;
2341 if (rtcp_debug_test_addr(&rtp
->rtcp
->them
)) {
2342 ast_verbose("* Sent RTCP SR to %s:%d\n", ast_inet_ntoa(rtp
->rtcp
->them
.sin_addr
), ntohs(rtp
->rtcp
->them
.sin_port
));
2343 ast_verbose(" Our SSRC: %u\n", rtp
->ssrc
);
2344 ast_verbose(" Sent(NTP): %u.%010u\n", (unsigned int)now
.tv_sec
, (unsigned int)now
.tv_usec
*4096);
2345 ast_verbose(" Sent(RTP): %u\n", rtp
->lastts
);
2346 ast_verbose(" Sent packets: %u\n", rtp
->txcount
);
2347 ast_verbose(" Sent octets: %u\n", rtp
->txoctetcount
);
2348 ast_verbose(" Report block:\n");
2349 ast_verbose(" Fraction lost: %u\n", fraction
);
2350 ast_verbose(" Cumulative loss: %u\n", lost
);
2351 ast_verbose(" IA jitter: %.4f\n", rtp
->rxjitter
);
2352 ast_verbose(" Their last SR: %u\n", rtp
->rtcp
->themrxlsr
);
2353 ast_verbose(" DLSR: %4.4f (sec)\n\n", (double)(ntohl(rtcpheader
[12])/65536.0));
2358 /*! \brief Send RTCP recepient's report */
2359 static int ast_rtcp_write_rr(void *data
)
2361 struct ast_rtp
*rtp
= data
;
2365 unsigned int extended
;
2366 unsigned int expected
;
2367 unsigned int expected_interval
;
2368 unsigned int received_interval
;
2371 unsigned int *rtcpheader
;
2373 struct timeval dlsr
;
2376 if (!rtp
|| !rtp
->rtcp
|| (&rtp
->rtcp
->them
.sin_addr
== 0))
2379 if (!rtp
->rtcp
->them
.sin_addr
.s_addr
) {
2380 ast_log(LOG_ERROR
, "RTCP RR transmission error to, rtcp halted %s\n",strerror(errno
));
2381 if (rtp
->rtcp
->schedid
> 0)
2382 ast_sched_del(rtp
->sched
, rtp
->rtcp
->schedid
);
2383 rtp
->rtcp
->schedid
= -1;
2387 extended
= rtp
->cycles
+ rtp
->lastrxseqno
;
2388 expected
= extended
- rtp
->seedrxseqno
+ 1;
2389 lost
= expected
- rtp
->rxcount
;
2390 expected_interval
= expected
- rtp
->rtcp
->expected_prior
;
2391 rtp
->rtcp
->expected_prior
= expected
;
2392 received_interval
= rtp
->rxcount
- rtp
->rtcp
->received_prior
;
2393 rtp
->rtcp
->received_prior
= rtp
->rxcount
;
2394 lost_interval
= expected_interval
- received_interval
;
2395 if (expected_interval
== 0 || lost_interval
<= 0)
2398 fraction
= (lost_interval
<< 8) / expected_interval
;
2399 gettimeofday(&now
, NULL
);
2400 timersub(&now
, &rtp
->rtcp
->rxlsr
, &dlsr
);
2401 rtcpheader
= (unsigned int *)bdata
;
2402 rtcpheader
[0] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_RR
<< 16) | ((len
/4)-1));
2403 rtcpheader
[1] = htonl(rtp
->ssrc
);
2404 rtcpheader
[2] = htonl(rtp
->themssrc
);
2405 rtcpheader
[3] = htonl(((fraction
& 0xff) << 24) | (lost
& 0xffffff));
2406 rtcpheader
[4] = htonl((rtp
->cycles
) | ((rtp
->lastrxseqno
& 0xffff)));
2407 rtcpheader
[5] = htonl((unsigned int)rtp
->rxjitter
);
2408 rtcpheader
[6] = htonl(rtp
->rtcp
->themrxlsr
);
2409 rtcpheader
[7] = htonl((((dlsr
.tv_sec
* 1000) + (dlsr
.tv_usec
/ 1000)) * 65536) / 1000);
2411 if (rtp
->rtcp
->sendfur
) {
2412 rtcpheader
[8] = htonl((2 << 30) | (0 << 24) | (RTCP_PT_FUR
<< 16) | 1); /* Header from page 36 in RFC 3550 */
2413 rtcpheader
[9] = htonl(rtp
->ssrc
); /* Our SSRC */
2415 rtp
->rtcp
->sendfur
= 0;
2418 /*! \note Insert SDES here. Probably should make SDES text equal to mimetypes[code].type (not subtype 'cos
2419 it can change mid call, and SDES can't) */
2420 rtcpheader
[len
/4] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SDES
<< 16) | 2);
2421 rtcpheader
[(len
/4)+1] = htonl(rtp
->ssrc
); /* Our SSRC */
2422 rtcpheader
[(len
/4)+2] = htonl(0x01 << 24); /* Empty for the moment */
2425 res
= sendto(rtp
->rtcp
->s
, (unsigned int *)rtcpheader
, len
, 0, (struct sockaddr
*)&rtp
->rtcp
->them
, sizeof(rtp
->rtcp
->them
));
2428 ast_log(LOG_ERROR
, "RTCP RR transmission error, rtcp halted: %s\n",strerror(errno
));
2429 /* Remove the scheduler */
2430 if (rtp
->rtcp
->schedid
> 0)
2431 ast_sched_del(rtp
->sched
, rtp
->rtcp
->schedid
);
2432 rtp
->rtcp
->schedid
= -1;
2436 rtp
->rtcp
->rr_count
++;
2438 if (rtcp_debug_test_addr(&rtp
->rtcp
->them
)) {
2439 ast_verbose("\n* Sending RTCP RR to %s:%d\n"
2440 " Our SSRC: %u\nTheir SSRC: %u\niFraction lost: %d\nCumulative loss: %u\n"
2441 " IA jitter: %.4f\n"
2442 " Their last SR: %u\n"
2443 " DLSR: %4.4f (sec)\n\n",
2444 ast_inet_ntoa(rtp
->rtcp
->them
.sin_addr
),
2445 ntohs(rtp
->rtcp
->them
.sin_port
),
2446 rtp
->ssrc
, rtp
->themssrc
, fraction
, lost
,
2448 rtp
->rtcp
->themrxlsr
,
2449 (double)(ntohl(rtcpheader
[7])/65536.0));
2455 /*! \brief Write and RTCP packet to the far end
2456 * \note Decide if we are going to send an SR (with Reception Block) or RR
2457 * RR is sent if we have not sent any rtp packets in the previous interval */
2458 static int ast_rtcp_write(void *data
)
2460 struct ast_rtp
*rtp
= data
;
2463 if (rtp
->txcount
> rtp
->rtcp
->lastsrtxcount
)
2464 res
= ast_rtcp_write_sr(data
);
2466 res
= ast_rtcp_write_rr(data
);
2471 /*! \brief generate comfort noice (CNG) */
2472 int ast_rtp_sendcng(struct ast_rtp
*rtp
, int level
)
2474 unsigned int *rtpheader
;
2479 level
= 127 - (level
& 0x7f);
2480 payload
= ast_rtp_lookup_code(rtp
, 0, AST_RTP_CN
);
2482 /* If we have no peer, return immediately */
2483 if (!rtp
->them
.sin_addr
.s_addr
)
2486 rtp
->dtmfmute
= ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
2488 /* Get a pointer to the header */
2489 rtpheader
= (unsigned int *)data
;
2490 rtpheader
[0] = htonl((2 << 30) | (1 << 23) | (payload
<< 16) | (rtp
->seqno
++));
2491 rtpheader
[1] = htonl(rtp
->lastts
);
2492 rtpheader
[2] = htonl(rtp
->ssrc
);
2494 if (rtp
->them
.sin_port
&& rtp
->them
.sin_addr
.s_addr
) {
2495 res
= sendto(rtp
->s
, (void *)rtpheader
, hdrlen
+ 1, 0, (struct sockaddr
*)&rtp
->them
, sizeof(rtp
->them
));
2497 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
));
2498 if (rtp_debug_test_addr(&rtp
->them
))
2499 ast_verbose("Sent Comfort Noise RTP packet to %s:%d (type %d, seq %d, ts %u, len %d)\n"
2500 , ast_inet_ntoa(rtp
->them
.sin_addr
), ntohs(rtp
->them
.sin_port
), payload
, rtp
->seqno
, rtp
->lastts
,res
- hdrlen
);
2506 static int ast_rtp_raw_write(struct ast_rtp
*rtp
, struct ast_frame
*f
, int codec
)
2508 unsigned char *rtpheader
;
2515 ms
= calc_txstamp(rtp
, &f
->delivery
);
2516 /* Default prediction */
2517 if (f
->subclass
< AST_FORMAT_MAX_AUDIO
) {
2518 pred
= rtp
->lastts
+ f
->samples
;
2520 /* Re-calculate last TS */
2521 rtp
->lastts
= rtp
->lastts
+ ms
* 8;
2522 if (ast_tvzero(f
->delivery
)) {
2523 /* If this isn't an absolute delivery time, Check if it is close to our prediction,
2524 and if so, go with our prediction */
2525 if (abs(rtp
->lastts
- pred
) < MAX_TIMESTAMP_SKEW
)
2528 if (option_debug
> 2)
2529 ast_log(LOG_DEBUG
, "Difference is %d, ms is %d\n", abs(rtp
->lastts
- pred
), ms
);
2534 mark
= f
->subclass
& 0x1;
2535 pred
= rtp
->lastovidtimestamp
+ f
->samples
;
2536 /* Re-calculate last TS */
2537 rtp
->lastts
= rtp
->lastts
+ ms
* 90;
2538 /* If it's close to our prediction, go for it */
2539 if (ast_tvzero(f
->delivery
)) {
2540 if (abs(rtp
->lastts
- pred
) < 7200) {
2542 rtp
->lastovidtimestamp
+= f
->samples
;
2544 if (option_debug
> 2)
2545 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
);
2546 rtp
->lastovidtimestamp
= rtp
->lastts
;
2550 /* If the timestamp for non-digit packets has moved beyond the timestamp
2551 for digits, update the digit timestamp.
2553 if (rtp
->lastts
> rtp
->lastdigitts
)
2554 rtp
->lastdigitts
= rtp
->lastts
;
2556 if (f
->has_timing_info
)
2557 rtp
->lastts
= f
->ts
* 8;
2559 /* Get a pointer to the header */
2560 rtpheader
= (unsigned char *)(f
->data
- hdrlen
);
2562 put_unaligned_uint32(rtpheader
, htonl((2 << 30) | (codec
<< 16) | (rtp
->seqno
) | (mark
<< 23)));
2563 put_unaligned_uint32(rtpheader
+ 4, htonl(rtp
->lastts
));
2564 put_unaligned_uint32(rtpheader
+ 8, htonl(rtp
->ssrc
));
2566 if (rtp
->them
.sin_port
&& rtp
->them
.sin_addr
.s_addr
) {
2567 res
= sendto(rtp
->s
, (void *)rtpheader
, f
->datalen
+ hdrlen
, 0, (struct sockaddr
*)&rtp
->them
, sizeof(rtp
->them
));
2569 if (!rtp
->nat
|| (rtp
->nat
&& (ast_test_flag(rtp
, FLAG_NAT_ACTIVE
) == FLAG_NAT_ACTIVE
))) {
2570 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
));
2571 } else if (((ast_test_flag(rtp
, FLAG_NAT_ACTIVE
) == FLAG_NAT_INACTIVE
) || rtpdebug
) && !ast_test_flag(rtp
, FLAG_NAT_INACTIVE_NOWARN
)) {
2572 /* Only give this error message once if we are not RTP debugging */
2573 if (option_debug
|| rtpdebug
)
2574 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
));
2575 ast_set_flag(rtp
, FLAG_NAT_INACTIVE_NOWARN
);
2579 rtp
->txoctetcount
+=(res
- hdrlen
);
2581 if (rtp
->rtcp
&& rtp
->rtcp
->schedid
< 1)
2582 rtp
->rtcp
->schedid
= ast_sched_add(rtp
->sched
, ast_rtcp_calc_interval(rtp
), ast_rtcp_write
, rtp
);
2585 if (rtp_debug_test_addr(&rtp
->them
))
2586 ast_verbose("Sent RTP packet to %s:%d (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
2587 ast_inet_ntoa(rtp
->them
.sin_addr
), ntohs(rtp
->them
.sin_port
), codec
, rtp
->seqno
, rtp
->lastts
,res
- hdrlen
);
2595 int ast_rtp_codec_setpref(struct ast_rtp
*rtp
, struct ast_codec_pref
*prefs
)
2598 for (x
= 0; x
< 32; x
++) { /* Ugly way */
2599 rtp
->pref
.order
[x
] = prefs
->order
[x
];
2600 rtp
->pref
.framing
[x
] = prefs
->framing
[x
];
2603 ast_smoother_free(rtp
->smoother
);
2604 rtp
->smoother
= NULL
;
2608 struct ast_codec_pref
*ast_rtp_codec_getpref(struct ast_rtp
*rtp
)
2613 int ast_rtp_codec_getformat(int pt
)
2615 if (pt
< 0 || pt
> MAX_RTP_PT
)
2616 return 0; /* bogus payload type */
2618 if (static_RTP_PT
[pt
].isAstFormat
)
2619 return static_RTP_PT
[pt
].code
;
2624 int ast_rtp_write(struct ast_rtp
*rtp
, struct ast_frame
*_f
)
2626 struct ast_frame
*f
;
2632 /* If we have no peer, return immediately */
2633 if (!rtp
->them
.sin_addr
.s_addr
)
2636 /* If there is no data length, return immediately */
2640 /* Make sure we have enough space for RTP header */
2641 if ((_f
->frametype
!= AST_FRAME_VOICE
) && (_f
->frametype
!= AST_FRAME_VIDEO
)) {
2642 ast_log(LOG_WARNING
, "RTP can only send voice and video\n");
2646 subclass
= _f
->subclass
;
2647 if (_f
->frametype
== AST_FRAME_VIDEO
)
2650 codec
= ast_rtp_lookup_code(rtp
, 1, subclass
);
2652 ast_log(LOG_WARNING
, "Don't know how to send format %s packets with RTP\n", ast_getformatname(_f
->subclass
));
2656 if (rtp
->lasttxformat
!= subclass
) {
2657 /* New format, reset the smoother */
2659 ast_log(LOG_DEBUG
, "Ooh, format changed from %s to %s\n", ast_getformatname(rtp
->lasttxformat
), ast_getformatname(subclass
));
2660 rtp
->lasttxformat
= subclass
;
2662 ast_smoother_free(rtp
->smoother
);
2663 rtp
->smoother
= NULL
;
2666 if (!rtp
->smoother
) {
2667 struct ast_format_list fmt
= ast_codec_pref_getsize(&rtp
->pref
, subclass
);
2668 if (fmt
.inc_ms
) { /* if codec parameters is set / avoid division by zero */
2669 if (!(rtp
->smoother
= ast_smoother_new((fmt
.cur_ms
* fmt
.fr_len
) / fmt
.inc_ms
))) {
2670 ast_log(LOG_WARNING
, "Unable to create smoother: format: %d ms: %d len: %d\n", subclass
, fmt
.cur_ms
, ((fmt
.cur_ms
* fmt
.fr_len
) / fmt
.inc_ms
));
2674 ast_smoother_set_flags(rtp
->smoother
, fmt
.flags
);
2676 ast_log(LOG_DEBUG
, "Created smoother: format: %d ms: %d len: %d\n", subclass
, fmt
.cur_ms
, ((fmt
.cur_ms
* fmt
.fr_len
) / fmt
.inc_ms
));
2679 if (rtp
->smoother
) {
2680 if (ast_smoother_test_flag(rtp
->smoother
, AST_SMOOTHER_FLAG_BE
)) {
2681 ast_smoother_feed_be(rtp
->smoother
, _f
);
2683 ast_smoother_feed(rtp
->smoother
, _f
);
2686 while((f
= ast_smoother_read(rtp
->smoother
)))
2687 ast_rtp_raw_write(rtp
, f
, codec
);
2689 /* Don't buffer outgoing frames; send them one-per-packet: */
2690 if (_f
->offset
< hdrlen
) {
2695 ast_rtp_raw_write(rtp
, f
, codec
);
2703 /*! \brief Unregister interface to channel driver */
2704 void ast_rtp_proto_unregister(struct ast_rtp_protocol
*proto
)
2706 AST_LIST_LOCK(&protos
);
2707 AST_LIST_REMOVE(&protos
, proto
, list
);
2708 AST_LIST_UNLOCK(&protos
);
2711 /*! \brief Register interface to channel driver */
2712 int ast_rtp_proto_register(struct ast_rtp_protocol
*proto
)
2714 struct ast_rtp_protocol
*cur
;
2716 AST_LIST_LOCK(&protos
);
2717 AST_LIST_TRAVERSE(&protos
, cur
, list
) {
2718 if (!strcmp(cur
->type
, proto
->type
)) {
2719 ast_log(LOG_WARNING
, "Tried to register same protocol '%s' twice\n", cur
->type
);
2720 AST_LIST_UNLOCK(&protos
);
2724 AST_LIST_INSERT_HEAD(&protos
, proto
, list
);
2725 AST_LIST_UNLOCK(&protos
);
2730 /*! \brief Bridge loop for true native bridge (reinvite) */
2731 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
)
2733 struct ast_frame
*fr
= NULL
;
2734 struct ast_channel
*who
= NULL
, *other
= NULL
, *cs
[3] = {NULL
, };
2735 int oldcodec0
= codec0
, oldcodec1
= codec1
;
2736 struct sockaddr_in ac1
= {0,}, vac1
= {0,}, ac0
= {0,}, vac0
= {0,};
2737 struct sockaddr_in t1
= {0,}, vt1
= {0,}, t0
= {0,}, vt0
= {0,};
2739 /* Set it up so audio goes directly between the two endpoints */
2741 /* Test the first channel */
2742 if (!(pr0
->set_rtp_peer(c0
, p1
, vp1
, codec1
, ast_test_flag(p1
, FLAG_NAT_ACTIVE
)))) {
2743 ast_rtp_get_peer(p1
, &ac1
);
2745 ast_rtp_get_peer(vp1
, &vac1
);
2747 ast_log(LOG_WARNING
, "Channel '%s' failed to talk to '%s'\n", c0
->name
, c1
->name
);
2749 /* Test the second channel */
2750 if (!(pr1
->set_rtp_peer(c1
, p0
, vp0
, codec0
, ast_test_flag(p0
, FLAG_NAT_ACTIVE
)))) {
2751 ast_rtp_get_peer(p0
, &ac0
);
2753 ast_rtp_get_peer(vp0
, &vac0
);
2755 ast_log(LOG_WARNING
, "Channel '%s' failed to talk to '%s'\n", c1
->name
, c0
->name
);
2757 /* Now we can unlock and move into our loop */
2758 ast_channel_unlock(c0
);
2759 ast_channel_unlock(c1
);
2761 /* Throw our channels into the structure and enter the loop */
2766 /* Check if anything changed */
2767 if ((c0
->tech_pvt
!= pvt0
) ||
2768 (c1
->tech_pvt
!= pvt1
) ||
2769 (c0
->masq
|| c0
->masqr
|| c1
->masq
|| c1
->masqr
)) {
2770 ast_log(LOG_DEBUG
, "Oooh, something is weird, backing out\n");
2771 if (c0
->tech_pvt
== pvt0
)
2772 if (pr0
->set_rtp_peer(c0
, NULL
, NULL
, 0, 0))
2773 ast_log(LOG_WARNING
, "Channel '%s' failed to break RTP bridge\n", c0
->name
);
2774 if (c1
->tech_pvt
== pvt1
)
2775 if (pr1
->set_rtp_peer(c1
, NULL
, NULL
, 0, 0))
2776 ast_log(LOG_WARNING
, "Channel '%s' failed to break RTP bridge\n", c1
->name
);
2777 return AST_BRIDGE_RETRY
;
2780 /* Check if they have changed their address */
2781 ast_rtp_get_peer(p1
, &t1
);
2783 ast_rtp_get_peer(vp1
, &vt1
);
2785 codec1
= pr1
->get_codec(c1
);
2786 ast_rtp_get_peer(p0
, &t0
);
2788 ast_rtp_get_peer(vp0
, &vt0
);
2790 codec0
= pr0
->get_codec(c0
);
2791 if ((inaddrcmp(&t1
, &ac1
)) ||
2792 (vp1
&& inaddrcmp(&vt1
, &vac1
)) ||
2793 (codec1
!= oldcodec1
)) {
2794 if (option_debug
> 1) {
2795 ast_log(LOG_DEBUG
, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
2796 c1
->name
, ast_inet_ntoa(t1
.sin_addr
), ntohs(t1
.sin_port
), codec1
);
2797 ast_log(LOG_DEBUG
, "Oooh, '%s' changed end vaddress to %s:%d (format %d)\n",
2798 c1
->name
, ast_inet_ntoa(vt1
.sin_addr
), ntohs(vt1
.sin_port
), codec1
);
2799 ast_log(LOG_DEBUG
, "Oooh, '%s' was %s:%d/(format %d)\n",
2800 c1
->name
, ast_inet_ntoa(ac1
.sin_addr
), ntohs(ac1
.sin_port
), oldcodec1
);
2801 ast_log(LOG_DEBUG
, "Oooh, '%s' was %s:%d/(format %d)\n",
2802 c1
->name
, ast_inet_ntoa(vac1
.sin_addr
), ntohs(vac1
.sin_port
), oldcodec1
);
2804 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
)))
2805 ast_log(LOG_WARNING
, "Channel '%s' failed to update to '%s'\n", c0
->name
, c1
->name
);
2806 memcpy(&ac1
, &t1
, sizeof(ac1
));
2807 memcpy(&vac1
, &vt1
, sizeof(vac1
));
2810 if ((inaddrcmp(&t0
, &ac0
)) ||
2811 (vp0
&& inaddrcmp(&vt0
, &vac0
))) {
2812 if (option_debug
> 1) {
2813 ast_log(LOG_DEBUG
, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
2814 c0
->name
, ast_inet_ntoa(t0
.sin_addr
), ntohs(t0
.sin_port
), codec0
);
2815 ast_log(LOG_DEBUG
, "Oooh, '%s' was %s:%d/(format %d)\n",
2816 c0
->name
, ast_inet_ntoa(ac0
.sin_addr
), ntohs(ac0
.sin_port
), oldcodec0
);
2818 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
)))
2819 ast_log(LOG_WARNING
, "Channel '%s' failed to update to '%s'\n", c1
->name
, c0
->name
);
2820 memcpy(&ac0
, &t0
, sizeof(ac0
));
2821 memcpy(&vac0
, &vt0
, sizeof(vac0
));
2825 /* Wait for frame to come in on the channels */
2826 if (!(who
= ast_waitfor_n(cs
, 2, &timeoutms
))) {
2828 return AST_BRIDGE_RETRY
;
2830 ast_log(LOG_DEBUG
, "Ooh, empty read...\n");
2831 if (ast_check_hangup(c0
) || ast_check_hangup(c1
))
2836 other
= (who
== c0
) ? c1
: c0
;
2837 if (!fr
|| ((fr
->frametype
== AST_FRAME_DTMF
) &&
2838 (((who
== c0
) && (flags
& AST_BRIDGE_DTMF_CHANNEL_0
)) ||
2839 ((who
== c1
) && (flags
& AST_BRIDGE_DTMF_CHANNEL_1
))))) {
2840 /* Break out of bridge */
2844 ast_log(LOG_DEBUG
, "Oooh, got a %s\n", fr
? "digit" : "hangup");
2845 if (c0
->tech_pvt
== pvt0
)
2846 if (pr0
->set_rtp_peer(c0
, NULL
, NULL
, 0, 0))
2847 ast_log(LOG_WARNING
, "Channel '%s' failed to break RTP bridge\n", c0
->name
);
2848 if (c1
->tech_pvt
== pvt1
)
2849 if (pr1
->set_rtp_peer(c1
, NULL
, NULL
, 0, 0))
2850 ast_log(LOG_WARNING
, "Channel '%s' failed to break RTP bridge\n", c1
->name
);
2851 return AST_BRIDGE_COMPLETE
;
2852 } else if ((fr
->frametype
== AST_FRAME_CONTROL
) && !(flags
& AST_BRIDGE_IGNORE_SIGS
)) {
2853 if ((fr
->subclass
== AST_CONTROL_HOLD
) ||
2854 (fr
->subclass
== AST_CONTROL_UNHOLD
) ||
2855 (fr
->subclass
== AST_CONTROL_VIDUPDATE
)) {
2856 ast_indicate(other
, fr
->subclass
);
2861 ast_log(LOG_DEBUG
, "Got a FRAME_CONTROL (%d) frame on channel %s\n", fr
->subclass
, who
->name
);
2862 return AST_BRIDGE_COMPLETE
;
2865 if ((fr
->frametype
== AST_FRAME_DTMF
) ||
2866 (fr
->frametype
== AST_FRAME_VOICE
) ||
2867 (fr
->frametype
== AST_FRAME_VIDEO
)) {
2868 ast_write(other
, fr
);
2878 return AST_BRIDGE_FAILED
;
2881 /*! \brief P2P RTP Callback */
2882 static int p2p_rtp_callback(int *id
, int fd
, short events
, void *cbdata
)
2884 int res
= 0, hdrlen
= 12;
2885 struct sockaddr_in sin
;
2887 unsigned int *header
;
2888 struct ast_rtp
*rtp
= cbdata
, *bridged
= NULL
;
2894 if ((res
= recvfrom(fd
, rtp
->rawdata
+ AST_FRIENDLY_OFFSET
, sizeof(rtp
->rawdata
) - AST_FRIENDLY_OFFSET
, 0, (struct sockaddr
*)&sin
, &len
)) < 0)
2897 header
= (unsigned int *)(rtp
->rawdata
+ AST_FRIENDLY_OFFSET
);
2899 /* If NAT support is turned on, then see if we need to change their address */
2901 ((rtp
->them
.sin_addr
.s_addr
!= sin
.sin_addr
.s_addr
) ||
2902 (rtp
->them
.sin_port
!= sin
.sin_port
))) {
2905 ast_set_flag(rtp
, FLAG_NAT_ACTIVE
);
2906 if (option_debug
|| rtpdebug
)
2907 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
));
2910 /* Write directly out to other RTP stream if bridged */
2911 if ((bridged
= ast_rtp_get_bridged(rtp
)))
2912 bridge_p2p_rtp_write(rtp
, bridged
, header
, res
, hdrlen
);
2917 /*! \brief Helper function to switch a channel and RTP stream into callback mode */
2918 static int p2p_callback_enable(struct ast_channel
*chan
, struct ast_rtp
*rtp
, int *fds
, int **iod
)
2920 /* If we need DTMF, are looking for STUN, or we have no IO structure then we can't do direct callback */
2921 if (ast_test_flag(rtp
, FLAG_P2P_NEED_DTMF
) || ast_test_flag(rtp
, FLAG_HAS_STUN
) || !rtp
->io
)
2924 /* If the RTP structure is already in callback mode, remove it temporarily */
2926 ast_io_remove(rtp
->io
, rtp
->ioid
);
2930 /* Steal the file descriptors from the channel and stash them away */
2931 fds
[0] = chan
->fds
[0];
2934 /* Now, fire up callback mode */
2935 iod
[0] = ast_io_add(rtp
->io
, fds
[0], p2p_rtp_callback
, AST_IO_IN
, rtp
);
2940 /*! \brief Helper function to switch a channel and RTP stream out of callback mode */
2941 static int p2p_callback_disable(struct ast_channel
*chan
, struct ast_rtp
*rtp
, int *fds
, int **iod
)
2943 ast_channel_lock(chan
);
2945 /* Remove the callback from the IO context */
2946 ast_io_remove(rtp
->io
, iod
[0]);
2948 /* Restore file descriptors */
2949 chan
->fds
[0] = fds
[0];
2950 ast_channel_unlock(chan
);
2952 /* Restore callback mode if previously used */
2953 if (ast_test_flag(rtp
, FLAG_CALLBACK_MODE
))
2954 rtp
->ioid
= ast_io_add(rtp
->io
, rtp
->s
, rtpread
, AST_IO_IN
, rtp
);
2959 /*! \brief Helper function that sets what an RTP structure is bridged to */
2960 static void p2p_set_bridge(struct ast_rtp
*rtp0
, struct ast_rtp
*rtp1
)
2962 ast_mutex_lock(&rtp0
->bridge_lock
);
2963 rtp0
->bridged
= rtp1
;
2964 ast_mutex_unlock(&rtp0
->bridge_lock
);
2969 /*! \brief Bridge loop for partial native bridge (packet2packet) */
2970 static enum ast_bridge_result
bridge_p2p_loop(struct ast_channel
*c0
, struct ast_channel
*c1
, struct ast_rtp
*p0
, struct ast_rtp
*p1
, int timeoutms
, int flags
, struct ast_frame
**fo
, struct ast_channel
**rc
, void *pvt0
, void *pvt1
)
2972 struct ast_frame
*fr
= NULL
;
2973 struct ast_channel
*who
= NULL
, *other
= NULL
, *cs
[3] = {NULL
, };
2974 int p0_fds
[2] = {-1, -1}, p1_fds
[2] = {-1, -1};
2975 int *p0_iod
[2] = {NULL
, NULL
}, *p1_iod
[2] = {NULL
, NULL
};
2976 int p0_callback
= 0, p1_callback
= 0;
2977 enum ast_bridge_result res
= AST_BRIDGE_FAILED
;
2979 /* Okay, setup each RTP structure to do P2P forwarding */
2980 ast_clear_flag(p0
, FLAG_P2P_SENT_MARK
);
2981 p2p_set_bridge(p0
, p1
);
2982 ast_clear_flag(p1
, FLAG_P2P_SENT_MARK
);
2983 p2p_set_bridge(p1
, p0
);
2985 /* Activate callback modes if possible */
2986 p0_callback
= p2p_callback_enable(c0
, p0
, &p0_fds
[0], &p0_iod
[0]);
2987 p1_callback
= p2p_callback_enable(c1
, p1
, &p1_fds
[0], &p1_iod
[0]);
2989 /* Now let go of the channel locks and be on our way */
2990 ast_channel_unlock(c0
);
2991 ast_channel_unlock(c1
);
2993 /* Go into a loop forwarding frames until we don't need to anymore */
2998 /* Check if anything changed */
2999 if ((c0
->tech_pvt
!= pvt0
) ||
3000 (c1
->tech_pvt
!= pvt1
) ||
3001 (c0
->masq
|| c0
->masqr
|| c1
->masq
|| c1
->masqr
)) {
3002 ast_log(LOG_DEBUG
, "Oooh, something is weird, backing out\n");
3003 if ((c0
->masq
|| c0
->masqr
) && (fr
= ast_read(c0
)))
3005 if ((c1
->masq
|| c1
->masqr
) && (fr
= ast_read(c1
)))
3007 res
= AST_BRIDGE_RETRY
;
3010 /* Wait on a channel to feed us a frame */
3011 if (!(who
= ast_waitfor_n(cs
, 2, &timeoutms
))) {
3013 res
= AST_BRIDGE_RETRY
;
3017 ast_log(LOG_NOTICE
, "Ooh, empty read...\n");
3018 if (ast_check_hangup(c0
) || ast_check_hangup(c1
))
3022 /* Read in frame from channel */
3024 other
= (who
== c0
) ? c1
: c0
;
3025 /* Dependong on the frame we may need to break out of our bridge */
3026 if (!fr
|| ((fr
->frametype
== AST_FRAME_DTMF
) &&
3027 ((who
== c0
) && (flags
& AST_BRIDGE_DTMF_CHANNEL_0
)) |
3028 ((who
== c1
) && (flags
& AST_BRIDGE_DTMF_CHANNEL_1
)))) {
3029 /* Record received frame and who */
3033 ast_log(LOG_DEBUG
, "Oooh, got a %s\n", fr
? "digit" : "hangup");
3034 res
= AST_BRIDGE_COMPLETE
;
3036 } else if ((fr
->frametype
== AST_FRAME_CONTROL
) && !(flags
& AST_BRIDGE_IGNORE_SIGS
)) {
3037 if ((fr
->subclass
== AST_CONTROL_HOLD
) ||
3038 (fr
->subclass
== AST_CONTROL_UNHOLD
) ||
3039 (fr
->subclass
== AST_CONTROL_VIDUPDATE
)) {
3040 /* If we are going on hold, then break callback mode and P2P bridging */
3041 if (fr
->subclass
== AST_CONTROL_HOLD
) {
3043 p0_callback
= p2p_callback_disable(c0
, p0
, &p0_fds
[0], &p0_iod
[0]);
3045 p1_callback
= p2p_callback_disable(c1
, p1
, &p1_fds
[0], &p1_iod
[0]);
3046 p2p_set_bridge(p0
, NULL
);
3047 p2p_set_bridge(p1
, NULL
);
3048 } else if (fr
->subclass
== AST_CONTROL_UNHOLD
) {
3049 /* If we are off hold, then go back to callback mode and P2P bridging */
3050 ast_clear_flag(p0
, FLAG_P2P_SENT_MARK
);
3051 p2p_set_bridge(p0
, p1
);
3052 ast_clear_flag(p1
, FLAG_P2P_SENT_MARK
);
3053 p2p_set_bridge(p1
, p0
);
3054 p0_callback
= p2p_callback_enable(c0
, p0
, &p0_fds
[0], &p0_iod
[0]);
3055 p1_callback
= p2p_callback_enable(c1
, p1
, &p1_fds
[0], &p1_iod
[0]);
3057 ast_indicate(other
, fr
->subclass
);
3062 ast_log(LOG_DEBUG
, "Got a FRAME_CONTROL (%d) frame on channel %s\n", fr
->subclass
, who
->name
);
3063 res
= AST_BRIDGE_COMPLETE
;
3067 /* If this is a DTMF, voice, or video frame write it to the other channel */
3068 if ((fr
->frametype
== AST_FRAME_DTMF
) ||
3069 (fr
->frametype
== AST_FRAME_VOICE
) ||
3070 (fr
->frametype
== AST_FRAME_VIDEO
)) {
3071 ast_write(other
, fr
);
3081 /* If we are totally avoiding the core, then restore our link to it */
3083 p0_callback
= p2p_callback_disable(c0
, p0
, &p0_fds
[0], &p0_iod
[0]);
3085 p1_callback
= p2p_callback_disable(c1
, p1
, &p1_fds
[0], &p1_iod
[0]);
3087 /* Break out of the direct bridge */
3088 p2p_set_bridge(p0
, NULL
);
3089 p2p_set_bridge(p1
, NULL
);
3094 /*! \brief Bridge calls. If possible and allowed, initiate
3095 re-invite so the peers exchange media directly outside
3097 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
)
3099 struct ast_rtp
*p0
= NULL
, *p1
= NULL
; /* Audio RTP Channels */
3100 struct ast_rtp
*vp0
= NULL
, *vp1
= NULL
; /* Video RTP channels */
3101 struct ast_rtp_protocol
*pr0
= NULL
, *pr1
= NULL
;
3102 enum ast_rtp_get_result audio_p0_res
= AST_RTP_GET_FAILED
, video_p0_res
= AST_RTP_GET_FAILED
;
3103 enum ast_rtp_get_result audio_p1_res
= AST_RTP_GET_FAILED
, video_p1_res
= AST_RTP_GET_FAILED
;
3104 enum ast_bridge_result res
= AST_BRIDGE_FAILED
;
3105 int codec0
= 0, codec1
= 0;
3106 void *pvt0
= NULL
, *pvt1
= NULL
;
3109 ast_channel_lock(c0
);
3110 while(ast_channel_trylock(c1
)) {
3111 ast_channel_unlock(c0
);
3113 ast_channel_lock(c0
);
3116 /* Find channel driver interfaces */
3117 if (!(pr0
= get_proto(c0
))) {
3118 ast_log(LOG_WARNING
, "Can't find native functions for channel '%s'\n", c0
->name
);
3119 ast_channel_unlock(c0
);
3120 ast_channel_unlock(c1
);
3121 return AST_BRIDGE_FAILED
;
3123 if (!(pr1
= get_proto(c1
))) {
3124 ast_log(LOG_WARNING
, "Can't find native functions for channel '%s'\n", c1
->name
);
3125 ast_channel_unlock(c0
);
3126 ast_channel_unlock(c1
);
3127 return AST_BRIDGE_FAILED
;
3130 /* Get channel specific interface structures */
3131 pvt0
= c0
->tech_pvt
;
3132 pvt1
= c1
->tech_pvt
;
3134 /* Get audio and video interface (if native bridge is possible) */
3135 audio_p0_res
= pr0
->get_rtp_info(c0
, &p0
);
3136 video_p0_res
= pr0
->get_vrtp_info
? pr0
->get_vrtp_info(c0
, &vp0
) : AST_RTP_GET_FAILED
;
3137 audio_p1_res
= pr1
->get_rtp_info(c1
, &p1
);
3138 video_p1_res
= pr1
->get_vrtp_info
? pr1
->get_vrtp_info(c1
, &vp1
) : AST_RTP_GET_FAILED
;
3140 /* If we are carrying video, and both sides are not reinviting... then fail the native bridge */
3141 if (video_p0_res
!= AST_RTP_GET_FAILED
&& (audio_p0_res
!= AST_RTP_TRY_NATIVE
|| video_p0_res
!= AST_RTP_TRY_NATIVE
))
3142 audio_p0_res
= AST_RTP_GET_FAILED
;
3143 if (video_p1_res
!= AST_RTP_GET_FAILED
&& (audio_p1_res
!= AST_RTP_TRY_NATIVE
|| video_p1_res
!= AST_RTP_TRY_NATIVE
))
3144 audio_p1_res
= AST_RTP_GET_FAILED
;
3146 /* Check if a bridge is possible (partial/native) */
3147 if (audio_p0_res
== AST_RTP_GET_FAILED
|| audio_p1_res
== AST_RTP_GET_FAILED
) {
3148 /* Somebody doesn't want to play... */
3149 ast_channel_unlock(c0
);
3150 ast_channel_unlock(c1
);
3151 return AST_BRIDGE_FAILED_NOWARN
;
3154 /* If we need to feed DTMF frames into the core then only do a partial native bridge */
3155 if (ast_test_flag(p0
, FLAG_HAS_DTMF
) && (flags
& AST_BRIDGE_DTMF_CHANNEL_0
)) {
3156 ast_set_flag(p0
, FLAG_P2P_NEED_DTMF
);
3157 audio_p0_res
= AST_RTP_TRY_PARTIAL
;
3160 if (ast_test_flag(p1
, FLAG_HAS_DTMF
) && (flags
& AST_BRIDGE_DTMF_CHANNEL_1
)) {
3161 ast_set_flag(p1
, FLAG_P2P_NEED_DTMF
);
3162 audio_p1_res
= AST_RTP_TRY_PARTIAL
;
3165 /* Get codecs from both sides */
3166 codec0
= pr0
->get_codec
? pr0
->get_codec(c0
) : 0;
3167 codec1
= pr1
->get_codec
? pr1
->get_codec(c1
) : 0;
3168 if (codec0
&& codec1
&& !(codec0
& codec1
)) {
3169 /* Hey, we can't do native bridging if both parties speak different codecs */
3171 ast_log(LOG_DEBUG
, "Channel codec0 = %d is not codec1 = %d, cannot native bridge in RTP.\n", codec0
, codec1
);
3172 ast_channel_unlock(c0
);
3173 ast_channel_unlock(c1
);
3174 return AST_BRIDGE_FAILED_NOWARN
;
3177 /* If either side can only do a partial bridge, then don't try for a true native bridge */
3178 if (audio_p0_res
== AST_RTP_TRY_PARTIAL
|| audio_p1_res
== AST_RTP_TRY_PARTIAL
) {
3179 /* In order to do Packet2Packet bridging both sides must be in the same rawread/rawwrite */
3180 if (c0
->rawreadformat
!= c1
->rawwriteformat
|| c1
->rawreadformat
!= c0
->rawwriteformat
) {
3182 ast_log(LOG_DEBUG
, "Cannot packet2packet bridge - raw formats are incompatible\n");
3183 ast_channel_unlock(c0
);
3184 ast_channel_unlock(c1
);
3185 return AST_BRIDGE_FAILED_NOWARN
;
3187 if (option_verbose
> 2)
3188 ast_verbose(VERBOSE_PREFIX_3
"Packet2Packet bridging %s and %s\n", c0
->name
, c1
->name
);
3189 res
= bridge_p2p_loop(c0
, c1
, p0
, p1
, timeoutms
, flags
, fo
, rc
, pvt0
, pvt1
);
3191 if (option_verbose
> 2)
3192 ast_verbose(VERBOSE_PREFIX_3
"Native bridging %s and %s\n", c0
->name
, c1
->name
);
3193 res
= bridge_native_loop(c0
, c1
, p0
, p1
, vp0
, vp1
, pr0
, pr1
, codec0
, codec1
, timeoutms
, flags
, fo
, rc
, pvt0
, pvt1
);
3199 static int rtp_do_debug_ip(int fd
, int argc
, char *argv
[])
3202 struct ast_hostent ahp
;
3207 return RESULT_SHOWUSAGE
;
3209 p
= strstr(arg
, ":");
3215 hp
= ast_gethostbyname(arg
, &ahp
);
3217 return RESULT_SHOWUSAGE
;
3218 rtpdebugaddr
.sin_family
= AF_INET
;
3219 memcpy(&rtpdebugaddr
.sin_addr
, hp
->h_addr
, sizeof(rtpdebugaddr
.sin_addr
));
3220 rtpdebugaddr
.sin_port
= htons(port
);
3222 ast_cli(fd
, "RTP Debugging Enabled for IP: %s\n", ast_inet_ntoa(rtpdebugaddr
.sin_addr
));
3224 ast_cli(fd
, "RTP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(rtpdebugaddr
.sin_addr
), port
);
3226 return RESULT_SUCCESS
;
3229 static int rtcp_do_debug_ip_deprecated(int fd
, int argc
, char *argv
[])
3232 struct ast_hostent ahp
;
3236 return RESULT_SHOWUSAGE
;
3239 p
= strstr(arg
, ":");
3245 hp
= ast_gethostbyname(arg
, &ahp
);
3247 return RESULT_SHOWUSAGE
;
3248 rtcpdebugaddr
.sin_family
= AF_INET
;
3249 memcpy(&rtcpdebugaddr
.sin_addr
, hp
->h_addr
, sizeof(rtcpdebugaddr
.sin_addr
));
3250 rtcpdebugaddr
.sin_port
= htons(port
);
3252 ast_cli(fd
, "RTCP Debugging Enabled for IP: %s\n", ast_inet_ntoa(rtcpdebugaddr
.sin_addr
));
3254 ast_cli(fd
, "RTCP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(rtcpdebugaddr
.sin_addr
), port
);
3256 return RESULT_SUCCESS
;
3259 static int rtcp_do_debug_ip(int fd
, int argc
, char *argv
[])
3262 struct ast_hostent ahp
;
3266 return RESULT_SHOWUSAGE
;
3269 p
= strstr(arg
, ":");
3275 hp
= ast_gethostbyname(arg
, &ahp
);
3277 return RESULT_SHOWUSAGE
;
3278 rtcpdebugaddr
.sin_family
= AF_INET
;
3279 memcpy(&rtcpdebugaddr
.sin_addr
, hp
->h_addr
, sizeof(rtcpdebugaddr
.sin_addr
));
3280 rtcpdebugaddr
.sin_port
= htons(port
);
3282 ast_cli(fd
, "RTCP Debugging Enabled for IP: %s\n", ast_inet_ntoa(rtcpdebugaddr
.sin_addr
));
3284 ast_cli(fd
, "RTCP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(rtcpdebugaddr
.sin_addr
), port
);
3286 return RESULT_SUCCESS
;
3289 static int rtp_do_debug(int fd
, int argc
, char *argv
[])
3293 return RESULT_SHOWUSAGE
;
3294 return rtp_do_debug_ip(fd
, argc
, argv
);
3297 memset(&rtpdebugaddr
,0,sizeof(rtpdebugaddr
));
3298 ast_cli(fd
, "RTP Debugging Enabled\n");
3299 return RESULT_SUCCESS
;
3302 static int rtcp_do_debug_deprecated(int fd
, int argc
, char *argv
[]) {
3305 return RESULT_SHOWUSAGE
;
3306 return rtcp_do_debug_ip_deprecated(fd
, argc
, argv
);
3309 memset(&rtcpdebugaddr
,0,sizeof(rtcpdebugaddr
));
3310 ast_cli(fd
, "RTCP Debugging Enabled\n");
3311 return RESULT_SUCCESS
;
3314 static int rtcp_do_debug(int fd
, int argc
, char *argv
[]) {
3317 return RESULT_SHOWUSAGE
;
3318 return rtcp_do_debug_ip(fd
, argc
, argv
);
3321 memset(&rtcpdebugaddr
,0,sizeof(rtcpdebugaddr
));
3322 ast_cli(fd
, "RTCP Debugging Enabled\n");
3323 return RESULT_SUCCESS
;
3326 static int rtcp_do_stats_deprecated(int fd
, int argc
, char *argv
[]) {
3328 return RESULT_SHOWUSAGE
;
3331 ast_cli(fd
, "RTCP Stats Enabled\n");
3332 return RESULT_SUCCESS
;
3335 static int rtcp_do_stats(int fd
, int argc
, char *argv
[]) {
3337 return RESULT_SHOWUSAGE
;
3340 ast_cli(fd
, "RTCP Stats Enabled\n");
3341 return RESULT_SUCCESS
;
3344 static int rtp_no_debug(int fd
, int argc
, char *argv
[])
3347 return RESULT_SHOWUSAGE
;
3349 ast_cli(fd
,"RTP Debugging Disabled\n");
3350 return RESULT_SUCCESS
;
3353 static int rtcp_no_debug_deprecated(int fd
, int argc
, char *argv
[])
3356 return RESULT_SHOWUSAGE
;
3358 ast_cli(fd
,"RTCP Debugging Disabled\n");
3359 return RESULT_SUCCESS
;
3362 static int rtcp_no_debug(int fd
, int argc
, char *argv
[])
3365 return RESULT_SHOWUSAGE
;
3367 ast_cli(fd
,"RTCP Debugging Disabled\n");
3368 return RESULT_SUCCESS
;
3371 static int rtcp_no_stats_deprecated(int fd
, int argc
, char *argv
[])
3374 return RESULT_SHOWUSAGE
;
3376 ast_cli(fd
,"RTCP Stats Disabled\n");
3377 return RESULT_SUCCESS
;
3380 static int rtcp_no_stats(int fd
, int argc
, char *argv
[])
3383 return RESULT_SHOWUSAGE
;
3385 ast_cli(fd
,"RTCP Stats Disabled\n");
3386 return RESULT_SUCCESS
;
3389 static int stun_do_debug(int fd
, int argc
, char *argv
[])
3392 return RESULT_SHOWUSAGE
;
3395 ast_cli(fd
, "STUN Debugging Enabled\n");
3396 return RESULT_SUCCESS
;
3399 static int stun_no_debug(int fd
, int argc
, char *argv
[])
3402 return RESULT_SHOWUSAGE
;
3404 ast_cli(fd
, "STUN Debugging Disabled\n");
3405 return RESULT_SUCCESS
;
3408 static char debug_usage
[] =
3409 "Usage: rtp debug [ip host[:port]]\n"
3410 " Enable dumping of all RTP packets to and from host.\n";
3412 static char no_debug_usage
[] =
3413 "Usage: rtp debug off\n"
3414 " Disable all RTP debugging\n";
3416 static char stun_debug_usage
[] =
3417 "Usage: stun debug\n"
3418 " Enable STUN (Simple Traversal of UDP through NATs) debugging\n";
3420 static char stun_no_debug_usage
[] =
3421 "Usage: stun debug off\n"
3422 " Disable STUN debugging\n";
3424 static char rtcp_debug_usage
[] =
3425 "Usage: rtcp debug [ip host[:port]]\n"
3426 " Enable dumping of all RTCP packets to and from host.\n";
3428 static char rtcp_no_debug_usage
[] =
3429 "Usage: rtcp debug off\n"
3430 " Disable all RTCP debugging\n";
3432 static char rtcp_stats_usage
[] =
3433 "Usage: rtcp stats\n"
3434 " Enable dumping of RTCP stats.\n";
3436 static char rtcp_no_stats_usage
[] =
3437 "Usage: rtcp stats off\n"
3438 " Disable all RTCP stats\n";
3440 static struct ast_cli_entry cli_rtp_no_debug_deprecated
= {
3441 { "rtp", "no", "debug", NULL
},
3445 static struct ast_cli_entry cli_rtp_rtcp_debug_ip_deprecated
= {
3446 { "rtp", "rtcp", "debug", "ip", NULL
},
3447 rtcp_do_debug_deprecated
, NULL
,
3450 static struct ast_cli_entry cli_rtp_rtcp_debug_deprecated
= {
3451 { "rtp", "rtcp", "debug", NULL
},
3452 rtcp_do_debug_deprecated
, NULL
,
3455 static struct ast_cli_entry cli_rtp_rtcp_no_debug_deprecated
= {
3456 { "rtp", "rtcp", "no", "debug", NULL
},
3457 rtcp_no_debug_deprecated
, NULL
,
3460 static struct ast_cli_entry cli_rtp_rtcp_stats_deprecated
= {
3461 { "rtp", "rtcp", "stats", NULL
},
3462 rtcp_do_stats_deprecated
, NULL
,
3465 static struct ast_cli_entry cli_rtp_rtcp_no_stats_deprecated
= {
3466 { "rtp", "rtcp", "no", "stats", NULL
},
3467 rtcp_no_stats_deprecated
, NULL
,
3470 static struct ast_cli_entry cli_stun_no_debug_deprecated
= {
3471 { "stun", "no", "debug", NULL
},
3472 stun_no_debug
, NULL
,
3475 static struct ast_cli_entry cli_rtp
[] = {
3476 { { "rtp", "debug", "ip", NULL
},
3477 rtp_do_debug
, "Enable RTP debugging on IP",
3480 { { "rtp", "debug", NULL
},
3481 rtp_do_debug
, "Enable RTP debugging",
3484 { { "rtp", "debug", "off", NULL
},
3485 rtp_no_debug
, "Disable RTP debugging",
3486 no_debug_usage
, NULL
, &cli_rtp_no_debug_deprecated
},
3488 { { "rtcp", "debug", "ip", NULL
},
3489 rtcp_do_debug
, "Enable RTCP debugging on IP",
3490 rtcp_debug_usage
, NULL
, &cli_rtp_rtcp_debug_ip_deprecated
},
3492 { { "rtcp", "debug", NULL
},
3493 rtcp_do_debug
, "Enable RTCP debugging",
3494 rtcp_debug_usage
, NULL
, &cli_rtp_rtcp_debug_deprecated
},
3496 { { "rtcp", "debug", "off", NULL
},
3497 rtcp_no_debug
, "Disable RTCP debugging",
3498 rtcp_no_debug_usage
, NULL
, &cli_rtp_rtcp_no_debug_deprecated
},
3500 { { "rtcp", "stats", NULL
},
3501 rtcp_do_stats
, "Enable RTCP stats",
3502 rtcp_stats_usage
, NULL
, &cli_rtp_rtcp_stats_deprecated
},
3504 { { "rtcp", "stats", "off", NULL
},
3505 rtcp_no_stats
, "Disable RTCP stats",
3506 rtcp_no_stats_usage
, NULL
, &cli_rtp_rtcp_no_stats_deprecated
},
3508 { { "stun", "debug", NULL
},
3509 stun_do_debug
, "Enable STUN debugging",
3512 { { "stun", "debug", "off", NULL
},
3513 stun_no_debug
, "Disable STUN debugging",
3514 stun_no_debug_usage
, NULL
, &cli_stun_no_debug_deprecated
},
3517 int ast_rtp_reload(void)
3519 struct ast_config
*cfg
;
3524 dtmftimeout
= DEFAULT_DTMF_TIMEOUT
;
3525 cfg
= ast_config_load("rtp.conf");
3527 if ((s
= ast_variable_retrieve(cfg
, "general", "rtpstart"))) {
3529 if (rtpstart
< 1024)
3531 if (rtpstart
> 65535)
3534 if ((s
= ast_variable_retrieve(cfg
, "general", "rtpend"))) {
3541 if ((s
= ast_variable_retrieve(cfg
, "general", "rtcpinterval"))) {
3542 rtcpinterval
= atoi(s
);
3543 if (rtcpinterval
== 0)
3544 rtcpinterval
= 0; /* Just so we're clear... it's zero */
3545 if (rtcpinterval
< RTCP_MIN_INTERVALMS
)
3546 rtcpinterval
= RTCP_MIN_INTERVALMS
; /* This catches negative numbers too */
3547 if (rtcpinterval
> RTCP_MAX_INTERVALMS
)
3548 rtcpinterval
= RTCP_MAX_INTERVALMS
;
3550 if ((s
= ast_variable_retrieve(cfg
, "general", "rtpchecksums"))) {
3558 ast_log(LOG_WARNING
, "Disabling RTP checksums is not supported on this operating system!\n");
3561 if ((s
= ast_variable_retrieve(cfg
, "general", "dtmftimeout"))) {
3562 dtmftimeout
= atoi(s
);
3563 if ((dtmftimeout
< 0) || (dtmftimeout
> 20000)) {
3564 ast_log(LOG_WARNING
, "DTMF timeout of '%d' outside range, using default of '%d' instead\n",
3565 dtmftimeout
, DEFAULT_DTMF_TIMEOUT
);
3566 dtmftimeout
= DEFAULT_DTMF_TIMEOUT
;
3569 ast_config_destroy(cfg
);
3571 if (rtpstart
>= rtpend
) {
3572 ast_log(LOG_WARNING
, "Unreasonable values for RTP start/end port in rtp.conf\n");
3576 if (option_verbose
> 1)
3577 ast_verbose(VERBOSE_PREFIX_2
"RTP Allocating from port range %d -> %d\n", rtpstart
, rtpend
);
3581 /*! \brief Initialize the RTP system in Asterisk */
3582 void ast_rtp_init(void)
3584 ast_cli_register_multiple(cli_rtp
, sizeof(cli_rtp
) / sizeof(struct ast_cli_entry
));