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
;
93 /* Uncomment this to enable more intense native bridging, but note: this is currently buggy */
94 /* #define P2P_INTENSE */
97 * \brief Structure representing a RTP session.
99 * 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 [...]"
102 /*! \brief The value of each payload format mapping: */
103 struct rtpPayloadType
{
104 int isAstFormat
; /*!< whether the following code is an AST_FORMAT */
109 /*! \brief RTP session description */
113 unsigned char rawdata
[8192 + AST_FRIENDLY_OFFSET
];
114 unsigned int ssrc
; /*!< Synchronization source, RFC 3550, page 10. */
115 unsigned int themssrc
; /*!< Their SSRC */
118 unsigned int lastrxts
;
119 unsigned int lastividtimestamp
;
120 unsigned int lastovidtimestamp
;
121 unsigned int lasteventseqn
;
122 int lastrxseqno
; /*!< Last received sequence number */
123 unsigned short seedrxseqno
; /*!< What sequence number did they start with?*/
124 unsigned int seedrxts
; /*!< What RTP timestamp did they start with? */
125 unsigned int rxcount
; /*!< How many packets have we received? */
126 unsigned int rxoctetcount
; /*!< How many octets have we received? should be rxcount *160*/
127 unsigned int txcount
; /*!< How many packets have we sent? */
128 unsigned int txoctetcount
; /*!< How many octets have we sent? (txcount*160)*/
129 unsigned int cycles
; /*!< Shifted count of sequence number cycles */
130 double rxjitter
; /*!< Interarrival jitter at the moment */
131 double rxtransit
; /*!< Relative transit time for previous packet */
135 int rtptimeout
; /*!< RTP timeout time (negative or zero means disabled, negative value means temporarily disabled) */
136 int rtpholdtimeout
; /*!< RTP timeout when on hold (negative or zero means disabled, negative value means temporarily disabled). */
137 int rtpkeepalive
; /*!< Send RTP comfort noice packets for keepalive */
139 /* DTMF Reception Variables */
141 unsigned int lastevent
;
143 unsigned int dtmfsamples
;
144 /* DTMF Transmission Variables */
145 unsigned int lastdigitts
;
146 char sending_digit
; /*!< boolean - are we sending digits */
147 char send_digit
; /*!< digit we are sending */
152 struct sockaddr_in us
; /*!< Socket representation of the local endpoint. */
153 struct sockaddr_in them
; /*!< Socket representation of the remote endpoint. */
154 struct timeval rxcore
;
155 struct timeval txcore
;
156 double drxcore
; /*!< The double representation of the first received packet */
157 struct timeval lastrx
; /*!< timeval when we last received a packet */
158 struct timeval dtmfmute
;
159 struct ast_smoother
*smoother
;
161 unsigned short seqno
; /*!< Sequence number, RFC 3550, page 13. */
162 unsigned short rxseqno
;
163 struct sched_context
*sched
;
164 struct io_context
*io
;
166 ast_rtp_callback callback
;
167 ast_mutex_t bridge_lock
;
168 struct rtpPayloadType current_RTP_PT
[MAX_RTP_PT
];
169 int rtp_lookup_code_cache_isAstFormat
; /*!< a cache for the result of rtp_lookup_code(): */
170 int rtp_lookup_code_cache_code
;
171 int rtp_lookup_code_cache_result
;
172 struct ast_rtcp
*rtcp
;
173 struct ast_codec_pref pref
;
174 struct ast_rtp
*bridged
; /*!< Who we are Packet bridged to */
175 int set_marker_bit
:1; /*!< Whether to set the marker bit or not */
178 /* Forward declarations */
179 static int ast_rtcp_write(const void *data
);
180 static void timeval2ntp(struct timeval tv
, unsigned int *msw
, unsigned int *lsw
);
181 static int ast_rtcp_write_sr(const void *data
);
182 static int ast_rtcp_write_rr(const void *data
);
183 static unsigned int ast_rtcp_calc_interval(struct ast_rtp
*rtp
);
184 static int ast_rtp_senddigit_continuation(struct ast_rtp
*rtp
);
185 int ast_rtp_senddigit_end(struct ast_rtp
*rtp
, char digit
);
187 #define FLAG_3389_WARNING (1 << 0)
188 #define FLAG_NAT_ACTIVE (3 << 1)
189 #define FLAG_NAT_INACTIVE (0 << 1)
190 #define FLAG_NAT_INACTIVE_NOWARN (1 << 1)
191 #define FLAG_HAS_DTMF (1 << 3)
192 #define FLAG_P2P_SENT_MARK (1 << 4)
193 #define FLAG_P2P_NEED_DTMF (1 << 5)
194 #define FLAG_CALLBACK_MODE (1 << 6)
195 #define FLAG_DTMF_COMPENSATE (1 << 7)
196 #define FLAG_HAS_STUN (1 << 8)
199 * \brief Structure defining an RTCP session.
201 * The concept "RTCP session" is not defined in RFC 3550, but since
202 * this structure is analogous to ast_rtp, which tracks a RTP session,
203 * it is logical to think of this as a RTCP session.
205 * RTCP packet is defined on page 9 of RFC 3550.
209 int s
; /*!< Socket */
210 struct sockaddr_in us
; /*!< Socket representation of the local endpoint. */
211 struct sockaddr_in them
; /*!< Socket representation of the remote endpoint. */
212 unsigned int soc
; /*!< What they told us */
213 unsigned int spc
; /*!< What they told us */
214 unsigned int themrxlsr
; /*!< The middle 32 bits of the NTP timestamp in the last received SR*/
215 struct timeval rxlsr
; /*!< Time when we got their last SR */
216 struct timeval txlsr
; /*!< Time when we sent or last SR*/
217 unsigned int expected_prior
; /*!< no. packets in previous interval */
218 unsigned int received_prior
; /*!< no. packets received in previous interval */
219 int schedid
; /*!< Schedid returned from ast_sched_add() to schedule RTCP-transmissions*/
220 unsigned int rr_count
; /*!< number of RRs we've sent, not including report blocks in SR's */
221 unsigned int sr_count
; /*!< number of SRs we've sent */
222 unsigned int lastsrtxcount
; /*!< Transmit packet count when last SR sent */
223 double accumulated_transit
; /*!< accumulated a-dlsr-lsr */
224 double rtt
; /*!< Last reported rtt */
225 unsigned int reported_jitter
; /*!< The contents of their last jitter entry in the RR */
226 unsigned int reported_lost
; /*!< Reported lost packets in their RR */
227 char quality
[AST_MAX_USER_FIELD
];
236 typedef struct { unsigned int id
[4]; } __attribute__((packed
)) stun_trans_id
;
238 /* XXX Maybe stun belongs in another file if it ever has use outside of RTP */
240 unsigned short msgtype
;
241 unsigned short msglen
;
243 unsigned char ies
[0];
244 } __attribute__((packed
));
249 unsigned char value
[0];
250 } __attribute__((packed
));
253 unsigned char unused
;
254 unsigned char family
;
257 } __attribute__((packed
));
259 #define STUN_IGNORE (0)
260 #define STUN_ACCEPT (1)
262 #define STUN_BINDREQ 0x0001
263 #define STUN_BINDRESP 0x0101
264 #define STUN_BINDERR 0x0111
265 #define STUN_SECREQ 0x0002
266 #define STUN_SECRESP 0x0102
267 #define STUN_SECERR 0x0112
269 #define STUN_MAPPED_ADDRESS 0x0001
270 #define STUN_RESPONSE_ADDRESS 0x0002
271 #define STUN_CHANGE_REQUEST 0x0003
272 #define STUN_SOURCE_ADDRESS 0x0004
273 #define STUN_CHANGED_ADDRESS 0x0005
274 #define STUN_USERNAME 0x0006
275 #define STUN_PASSWORD 0x0007
276 #define STUN_MESSAGE_INTEGRITY 0x0008
277 #define STUN_ERROR_CODE 0x0009
278 #define STUN_UNKNOWN_ATTRIBUTES 0x000a
279 #define STUN_REFLECTED_FROM 0x000b
281 static const char *stun_msg2str(int msg
)
285 return "Binding Request";
287 return "Binding Response";
289 return "Binding Error Response";
291 return "Shared Secret Request";
293 return "Shared Secret Response";
295 return "Shared Secret Error Response";
297 return "Non-RFC3489 Message";
300 static const char *stun_attr2str(int msg
)
303 case STUN_MAPPED_ADDRESS
:
304 return "Mapped Address";
305 case STUN_RESPONSE_ADDRESS
:
306 return "Response Address";
307 case STUN_CHANGE_REQUEST
:
308 return "Change Request";
309 case STUN_SOURCE_ADDRESS
:
310 return "Source Address";
311 case STUN_CHANGED_ADDRESS
:
312 return "Changed Address";
317 case STUN_MESSAGE_INTEGRITY
:
318 return "Message Integrity";
319 case STUN_ERROR_CODE
:
321 case STUN_UNKNOWN_ATTRIBUTES
:
322 return "Unknown Attributes";
323 case STUN_REFLECTED_FROM
:
324 return "Reflected From";
326 return "Non-RFC3489 Attribute";
330 const char *username
;
331 const char *password
;
334 static int stun_process_attr(struct stun_state
*state
, struct stun_attr
*attr
)
337 ast_verbose("Found STUN Attribute %s (%04x), length %d\n",
338 stun_attr2str(ntohs(attr
->attr
)), ntohs(attr
->attr
), ntohs(attr
->len
));
339 switch(ntohs(attr
->attr
)) {
341 state
->username
= (const char *) (attr
->value
);
344 state
->password
= (const char *) (attr
->value
);
348 ast_verbose("Ignoring STUN attribute %s (%04x), length %d\n",
349 stun_attr2str(ntohs(attr
->attr
)), ntohs(attr
->attr
), ntohs(attr
->len
));
354 static void append_attr_string(struct stun_attr
**attr
, int attrval
, const char *s
, int *len
, int *left
)
356 int size
= sizeof(**attr
) + strlen(s
);
358 (*attr
)->attr
= htons(attrval
);
359 (*attr
)->len
= htons(strlen(s
));
360 memcpy((*attr
)->value
, s
, strlen(s
));
361 (*attr
) = (struct stun_attr
*)((*attr
)->value
+ strlen(s
));
367 static void append_attr_address(struct stun_attr
**attr
, int attrval
, struct sockaddr_in
*sin
, int *len
, int *left
)
369 int size
= sizeof(**attr
) + 8;
370 struct stun_addr
*addr
;
372 (*attr
)->attr
= htons(attrval
);
373 (*attr
)->len
= htons(8);
374 addr
= (struct stun_addr
*)((*attr
)->value
);
377 addr
->port
= sin
->sin_port
;
378 addr
->addr
= sin
->sin_addr
.s_addr
;
379 (*attr
) = (struct stun_attr
*)((*attr
)->value
+ 8);
385 static int stun_send(int s
, struct sockaddr_in
*dst
, struct stun_header
*resp
)
387 return sendto(s
, resp
, ntohs(resp
->msglen
) + sizeof(*resp
), 0,
388 (struct sockaddr
*)dst
, sizeof(*dst
));
391 static void stun_req_id(struct stun_header
*req
)
395 req
->id
.id
[x
] = ast_random();
398 size_t ast_rtp_alloc_size(void)
400 return sizeof(struct ast_rtp
);
403 void ast_rtp_stun_request(struct ast_rtp
*rtp
, struct sockaddr_in
*suggestion
, const char *username
)
405 struct stun_header
*req
;
406 unsigned char reqdata
[1024];
408 struct stun_attr
*attr
;
410 req
= (struct stun_header
*)reqdata
;
413 reqleft
= sizeof(reqdata
) - sizeof(struct stun_header
);
416 attr
= (struct stun_attr
*)req
->ies
;
418 append_attr_string(&attr
, STUN_USERNAME
, username
, &reqlen
, &reqleft
);
419 req
->msglen
= htons(reqlen
);
420 req
->msgtype
= htons(STUN_BINDREQ
);
421 stun_send(rtp
->s
, suggestion
, req
);
424 static int stun_handle_packet(int s
, struct sockaddr_in
*src
, unsigned char *data
, size_t len
)
426 struct stun_header
*resp
, *hdr
= (struct stun_header
*)data
;
427 struct stun_attr
*attr
;
428 struct stun_state st
;
429 int ret
= STUN_IGNORE
;
430 unsigned char respdata
[1024];
431 int resplen
, respleft
;
433 if (len
< sizeof(struct stun_header
)) {
435 ast_log(LOG_DEBUG
, "Runt STUN packet (only %zd, wanting at least %zd)\n", len
, sizeof(struct stun_header
));
439 ast_verbose("STUN Packet, msg %s (%04x), length: %d\n", stun_msg2str(ntohs(hdr
->msgtype
)), ntohs(hdr
->msgtype
), ntohs(hdr
->msglen
));
440 if (ntohs(hdr
->msglen
) > len
- sizeof(struct stun_header
)) {
442 ast_log(LOG_DEBUG
, "Scrambled STUN packet length (got %d, expecting %zd)\n", ntohs(hdr
->msglen
), len
- sizeof(struct stun_header
));
444 len
= ntohs(hdr
->msglen
);
445 data
+= sizeof(struct stun_header
);
446 memset(&st
, 0, sizeof(st
));
448 if (len
< sizeof(struct stun_attr
)) {
450 ast_log(LOG_DEBUG
, "Runt Attribute (got %zd, expecting %zd)\n", len
, sizeof(struct stun_attr
));
453 attr
= (struct stun_attr
*)data
;
454 if ((ntohs(attr
->len
) + sizeof(struct stun_attr
)) > len
) {
456 ast_log(LOG_DEBUG
, "Inconsistent Attribute (length %d exceeds remaining msg len %d)\n", (int) (ntohs(attr
->len
) + sizeof(struct stun_attr
)), (int) len
);
459 if (stun_process_attr(&st
, attr
)) {
461 ast_log(LOG_DEBUG
, "Failed to handle attribute %s (%04x)\n", stun_attr2str(ntohs(attr
->attr
)), ntohs(attr
->attr
));
464 /* Clear attribute in case previous entry was a string */
466 data
+= ntohs(attr
->len
) + sizeof(struct stun_attr
);
467 len
-= ntohs(attr
->len
) + sizeof(struct stun_attr
);
469 /* Null terminate any string */
471 resp
= (struct stun_header
*)respdata
;
473 respleft
= sizeof(respdata
) - sizeof(struct stun_header
);
477 attr
= (struct stun_attr
*)resp
->ies
;
479 switch(ntohs(hdr
->msgtype
)) {
482 ast_verbose("STUN Bind Request, username: %s\n",
483 st
.username
? st
.username
: "<none>");
485 append_attr_string(&attr
, STUN_USERNAME
, st
.username
, &resplen
, &respleft
);
486 append_attr_address(&attr
, STUN_MAPPED_ADDRESS
, src
, &resplen
, &respleft
);
487 resp
->msglen
= htons(resplen
);
488 resp
->msgtype
= htons(STUN_BINDRESP
);
489 stun_send(s
, src
, resp
);
494 ast_verbose("Dunno what to do with STUN message %04x (%s)\n", ntohs(hdr
->msgtype
), stun_msg2str(ntohs(hdr
->msgtype
)));
500 /*! \brief List of current sessions */
501 static AST_LIST_HEAD_STATIC(protos
, ast_rtp_protocol
);
503 static void timeval2ntp(struct timeval tv
, unsigned int *msw
, unsigned int *lsw
)
505 unsigned int sec
, usec
, frac
;
506 sec
= tv
.tv_sec
+ 2208988800u; /* Sec between 1900 and 1970 */
508 frac
= (usec
<< 12) + (usec
<< 8) - ((usec
* 3650) >> 6);
513 int ast_rtp_fd(struct ast_rtp
*rtp
)
518 int ast_rtcp_fd(struct ast_rtp
*rtp
)
525 unsigned int ast_rtcp_calc_interval(struct ast_rtp
*rtp
)
527 unsigned int interval
;
528 /*! \todo XXX Do a more reasonable calculation on this one
529 * Look in RFC 3550 Section A.7 for an example*/
530 interval
= rtcpinterval
;
534 /* \brief Put RTP timeout timers on hold during another transaction, like T.38 */
535 void ast_rtp_set_rtptimers_onhold(struct ast_rtp
*rtp
)
537 rtp
->rtptimeout
= (-1) * rtp
->rtptimeout
;
538 rtp
->rtpholdtimeout
= (-1) * rtp
->rtpholdtimeout
;
541 /*! \brief Set rtp timeout */
542 void ast_rtp_set_rtptimeout(struct ast_rtp
*rtp
, int timeout
)
544 rtp
->rtptimeout
= timeout
;
547 /*! \brief Set rtp hold timeout */
548 void ast_rtp_set_rtpholdtimeout(struct ast_rtp
*rtp
, int timeout
)
550 rtp
->rtpholdtimeout
= timeout
;
553 /*! \brief set RTP keepalive interval */
554 void ast_rtp_set_rtpkeepalive(struct ast_rtp
*rtp
, int period
)
556 rtp
->rtpkeepalive
= period
;
559 /*! \brief Get rtp timeout */
560 int ast_rtp_get_rtptimeout(struct ast_rtp
*rtp
)
562 if (rtp
->rtptimeout
< 0) /* We're not checking, but remembering the setting (during T.38 transmission) */
564 return rtp
->rtptimeout
;
567 /*! \brief Get rtp hold timeout */
568 int ast_rtp_get_rtpholdtimeout(struct ast_rtp
*rtp
)
570 if (rtp
->rtptimeout
< 0) /* We're not checking, but remembering the setting (during T.38 transmission) */
572 return rtp
->rtpholdtimeout
;
575 /*! \brief Get RTP keepalive interval */
576 int ast_rtp_get_rtpkeepalive(struct ast_rtp
*rtp
)
578 return rtp
->rtpkeepalive
;
581 void ast_rtp_set_data(struct ast_rtp
*rtp
, void *data
)
586 void ast_rtp_set_callback(struct ast_rtp
*rtp
, ast_rtp_callback callback
)
588 rtp
->callback
= callback
;
591 void ast_rtp_setnat(struct ast_rtp
*rtp
, int nat
)
596 int ast_rtp_getnat(struct ast_rtp
*rtp
)
598 return ast_test_flag(rtp
, FLAG_NAT_ACTIVE
);
601 void ast_rtp_setdtmf(struct ast_rtp
*rtp
, int dtmf
)
603 ast_set2_flag(rtp
, dtmf
? 1 : 0, FLAG_HAS_DTMF
);
606 void ast_rtp_setdtmfcompensate(struct ast_rtp
*rtp
, int compensate
)
608 ast_set2_flag(rtp
, compensate
? 1 : 0, FLAG_DTMF_COMPENSATE
);
611 void ast_rtp_setstun(struct ast_rtp
*rtp
, int stun_enable
)
613 ast_set2_flag(rtp
, stun_enable
? 1 : 0, FLAG_HAS_STUN
);
616 static struct ast_frame
*send_dtmf(struct ast_rtp
*rtp
, enum ast_frame_type type
)
618 if (((ast_test_flag(rtp
, FLAG_DTMF_COMPENSATE
) && type
== AST_FRAME_DTMF_END
) ||
619 (type
== AST_FRAME_DTMF_BEGIN
)) && ast_tvcmp(ast_tvnow(), rtp
->dtmfmute
) < 0) {
621 ast_log(LOG_DEBUG
, "Ignore potential DTMF echo from '%s'\n", ast_inet_ntoa(rtp
->them
.sin_addr
));
623 rtp
->dtmfsamples
= 0;
624 return &ast_null_frame
;
627 ast_log(LOG_DEBUG
, "Sending dtmf: %d (%c), at %s\n", rtp
->resp
, rtp
->resp
, ast_inet_ntoa(rtp
->them
.sin_addr
));
628 if (rtp
->resp
== 'X') {
629 rtp
->f
.frametype
= AST_FRAME_CONTROL
;
630 rtp
->f
.subclass
= AST_CONTROL_FLASH
;
632 rtp
->f
.frametype
= type
;
633 rtp
->f
.subclass
= rtp
->resp
;
643 static inline int rtp_debug_test_addr(struct sockaddr_in
*addr
)
647 if (rtpdebugaddr
.sin_addr
.s_addr
) {
648 if (((ntohs(rtpdebugaddr
.sin_port
) != 0)
649 && (rtpdebugaddr
.sin_port
!= addr
->sin_port
))
650 || (rtpdebugaddr
.sin_addr
.s_addr
!= addr
->sin_addr
.s_addr
))
656 static inline int rtcp_debug_test_addr(struct sockaddr_in
*addr
)
660 if (rtcpdebugaddr
.sin_addr
.s_addr
) {
661 if (((ntohs(rtcpdebugaddr
.sin_port
) != 0)
662 && (rtcpdebugaddr
.sin_port
!= addr
->sin_port
))
663 || (rtcpdebugaddr
.sin_addr
.s_addr
!= addr
->sin_addr
.s_addr
))
670 static struct ast_frame
*process_cisco_dtmf(struct ast_rtp
*rtp
, unsigned char *data
, int len
)
674 struct ast_frame
*f
= NULL
;
675 event
= ntohl(*((unsigned int *)(data
)));
677 if (option_debug
> 2 || rtpdebug
)
678 ast_log(LOG_DEBUG
, "Cisco DTMF Digit: %08x (len = %d)\n", event
, len
);
681 } else if (event
< 11) {
683 } else if (event
< 12) {
685 } else if (event
< 16) {
686 resp
= 'A' + (event
- 12);
687 } else if (event
< 17) {
690 if (rtp
->resp
&& (rtp
->resp
!= resp
)) {
691 f
= send_dtmf(rtp
, AST_FRAME_DTMF_END
);
694 rtp
->dtmfcount
= dtmftimeout
;
699 * \brief Process RTP DTMF and events according to RFC 2833.
701 * RFC 2833 is "RTP Payload for DTMF Digits, Telephony Tones and Telephony Signals".
709 static struct ast_frame
*process_rfc2833(struct ast_rtp
*rtp
, unsigned char *data
, int len
, unsigned int seqno
, unsigned int timestamp
)
712 unsigned int event_end
;
713 unsigned int samples
;
715 struct ast_frame
*f
= NULL
;
717 /* Figure out event, event end, and samples */
718 event
= ntohl(*((unsigned int *)(data
)));
720 event_end
= ntohl(*((unsigned int *)(data
)));
723 samples
= ntohl(*((unsigned int *)(data
)));
726 /* Print out debug if turned on */
727 if (rtpdebug
|| option_debug
> 2)
728 ast_log(LOG_DEBUG
, "- RTP 2833 Event: %08x (len = %d)\n", event
, len
);
730 /* Figure out what digit was pressed */
733 } else if (event
< 11) {
735 } else if (event
< 12) {
737 } else if (event
< 16) {
738 resp
= 'A' + (event
- 12);
739 } else if (event
< 17) { /* Event 16: Hook flash */
742 /* Not a supported event */
743 ast_log(LOG_DEBUG
, "Ignoring RTP 2833 Event: %08x. Not a DTMF Digit.\n", event
);
744 return &ast_null_frame
;
747 if (ast_test_flag(rtp
, FLAG_DTMF_COMPENSATE
)) {
748 if ((rtp
->lastevent
!= timestamp
) || (rtp
->resp
&& rtp
->resp
!= resp
)) {
750 f
= send_dtmf(rtp
, AST_FRAME_DTMF_END
);
752 rtp
->lastevent
= timestamp
;
755 if ((!(rtp
->resp
) && (!(event_end
& 0x80))) || (rtp
->resp
&& rtp
->resp
!= resp
)) {
757 f
= send_dtmf(rtp
, AST_FRAME_DTMF_BEGIN
);
758 } else if ((event_end
& 0x80) && (rtp
->lastevent
!= seqno
) && rtp
->resp
) {
759 f
= send_dtmf(rtp
, AST_FRAME_DTMF_END
);
760 f
->len
= ast_tvdiff_ms(ast_samp2tv(samples
, 8000), ast_tv(0, 0)); /* XXX hard coded 8kHz */
762 rtp
->lastevent
= seqno
;
766 rtp
->dtmfcount
= dtmftimeout
;
767 rtp
->dtmfsamples
= samples
;
773 * \brief Process Comfort Noise RTP.
775 * This is incomplete at the moment.
778 static struct ast_frame
*process_rfc3389(struct ast_rtp
*rtp
, unsigned char *data
, int len
)
780 struct ast_frame
*f
= NULL
;
781 /* Convert comfort noise into audio with various codecs. Unfortunately this doesn't
782 totally help us out becuase we don't have an engine to keep it going and we are not
783 guaranteed to have it every 20ms or anything */
785 ast_log(LOG_DEBUG
, "- RTP 3389 Comfort noise event: Level %d (len = %d)\n", rtp
->lastrxformat
, len
);
787 if (!(ast_test_flag(rtp
, FLAG_3389_WARNING
))) {
788 ast_log(LOG_NOTICE
, "Comfort noise support incomplete in Asterisk (RFC 3389). Please turn off on client if possible. Client IP: %s\n",
789 ast_inet_ntoa(rtp
->them
.sin_addr
));
790 ast_set_flag(rtp
, FLAG_3389_WARNING
);
793 /* Must have at least one byte */
797 rtp
->f
.data
= rtp
->rawdata
+ AST_FRIENDLY_OFFSET
;
798 rtp
->f
.datalen
= len
- 1;
799 rtp
->f
.offset
= AST_FRIENDLY_OFFSET
;
800 memcpy(rtp
->f
.data
, data
+ 1, len
- 1);
806 rtp
->f
.frametype
= AST_FRAME_CNG
;
807 rtp
->f
.subclass
= data
[0] & 0x7f;
808 rtp
->f
.datalen
= len
- 1;
810 rtp
->f
.delivery
.tv_usec
= rtp
->f
.delivery
.tv_sec
= 0;
815 static int rtpread(int *id
, int fd
, short events
, void *cbdata
)
817 struct ast_rtp
*rtp
= cbdata
;
819 f
= ast_rtp_read(rtp
);
822 rtp
->callback(rtp
, f
, rtp
->data
);
827 struct ast_frame
*ast_rtcp_read(struct ast_rtp
*rtp
)
830 int position
, i
, packetwords
;
832 struct sockaddr_in sin
;
833 unsigned int rtcpdata
[8192 + AST_FRIENDLY_OFFSET
];
834 unsigned int *rtcpheader
;
846 struct ast_frame
*f
= &ast_null_frame
;
848 if (!rtp
|| !rtp
->rtcp
)
849 return &ast_null_frame
;
853 res
= recvfrom(rtp
->rtcp
->s
, rtcpdata
+ AST_FRIENDLY_OFFSET
, sizeof(rtcpdata
) - sizeof(unsigned int) * AST_FRIENDLY_OFFSET
,
854 0, (struct sockaddr
*)&sin
, &len
);
855 rtcpheader
= (unsigned int *)(rtcpdata
+ AST_FRIENDLY_OFFSET
);
860 if (errno
!= EAGAIN
) {
861 ast_log(LOG_WARNING
, "RTCP Read error: %s. Hanging up.\n", strerror(errno
));
864 return &ast_null_frame
;
867 packetwords
= res
/ 4;
870 /* Send to whoever sent to us */
871 if ((rtp
->rtcp
->them
.sin_addr
.s_addr
!= sin
.sin_addr
.s_addr
) ||
872 (rtp
->rtcp
->them
.sin_port
!= sin
.sin_port
)) {
873 memcpy(&rtp
->rtcp
->them
, &sin
, sizeof(rtp
->rtcp
->them
));
874 if (option_debug
|| rtpdebug
)
875 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
));
880 ast_log(LOG_DEBUG
, "Got RTCP report of %d bytes\n", res
);
882 /* Process a compound packet */
884 while (position
< packetwords
) {
886 length
= ntohl(rtcpheader
[i
]);
887 pt
= (length
& 0xff0000) >> 16;
888 rc
= (length
& 0x1f000000) >> 24;
891 if ((i
+ length
) > packetwords
) {
892 ast_log(LOG_WARNING
, "RTCP Read too short\n");
893 return &ast_null_frame
;
896 if (rtcp_debug_test_addr(&sin
)) {
897 ast_verbose("\n\nGot RTCP from %s:%d\n", ast_inet_ntoa(sin
.sin_addr
), ntohs(sin
.sin_port
));
898 ast_verbose("PT: %d(%s)\n", pt
, (pt
== 200) ? "Sender Report" : (pt
== 201) ? "Receiver Report" : (pt
== 192) ? "H.261 FUR" : "Unknown");
899 ast_verbose("Reception reports: %d\n", rc
);
900 ast_verbose("SSRC of sender: %u\n", rtcpheader
[i
+ 1]);
903 i
+= 2; /* Advance past header and ssrc */
907 gettimeofday(&rtp
->rtcp
->rxlsr
,NULL
); /* To be able to populate the dlsr */
908 rtp
->rtcp
->spc
= ntohl(rtcpheader
[i
+3]);
909 rtp
->rtcp
->soc
= ntohl(rtcpheader
[i
+ 4]);
910 rtp
->rtcp
->themrxlsr
= ((ntohl(rtcpheader
[i
]) & 0x0000ffff) << 16) | ((ntohl(rtcpheader
[i
+ 1]) & 0xffff0000) >> 16); /* Going to LSR in RR*/
912 if (rtcp_debug_test_addr(&sin
)) {
913 ast_verbose("NTP timestamp: %lu.%010lu\n", (unsigned long) ntohl(rtcpheader
[i
]), (unsigned long) ntohl(rtcpheader
[i
+ 1]) * 4096);
914 ast_verbose("RTP timestamp: %lu\n", (unsigned long) ntohl(rtcpheader
[i
+ 2]));
915 ast_verbose("SPC: %lu\tSOC: %lu\n", (unsigned long) ntohl(rtcpheader
[i
+ 3]), (unsigned long) ntohl(rtcpheader
[i
+ 4]));
920 /* Intentional fall through */
922 /* Don't handle multiple reception reports (rc > 1) yet */
923 /* Calculate RTT per RFC */
924 gettimeofday(&now
, NULL
);
925 timeval2ntp(now
, &msw
, &lsw
);
926 if (ntohl(rtcpheader
[i
+ 4]) && ntohl(rtcpheader
[i
+ 5])) { /* We must have the LSR && DLSR */
927 comp
= ((msw
& 0xffff) << 16) | ((lsw
& 0xffff0000) >> 16);
928 lsr
= ntohl(rtcpheader
[i
+ 4]);
929 dlsr
= ntohl(rtcpheader
[i
+ 5]);
930 rtt
= comp
- lsr
- dlsr
;
932 /* Convert end to end delay to usec (keeping the calculation in 64bit space)
933 sess->ee_delay = (eedelay * 1000) / 65536; */
935 rtt
= (rtt
* 1000000) >> 16;
937 rtt
= (rtt
* 1000) >> 16;
941 rttsec
= rtt
/ 1000.;
943 if (comp
- dlsr
>= lsr
) {
944 rtp
->rtcp
->accumulated_transit
+= rttsec
;
945 rtp
->rtcp
->rtt
= rttsec
;
946 if (rtp
->rtcp
->maxrtt
<rttsec
)
947 rtp
->rtcp
->maxrtt
= rttsec
;
948 if (rtp
->rtcp
->minrtt
>rttsec
)
949 rtp
->rtcp
->minrtt
= rttsec
;
950 } else if (rtcp_debug_test_addr(&sin
)) {
951 ast_verbose("Internal RTCP NTP clock skew detected: "
952 "lsr=%u, now=%u, dlsr=%u (%d:%03dms), "
954 lsr
, comp
, dlsr
, dlsr
/ 65536,
955 (dlsr
% 65536) * 1000 / 65536,
956 dlsr
- (comp
- lsr
));
960 rtp
->rtcp
->reported_jitter
= ntohl(rtcpheader
[i
+ 3]);
961 rtp
->rtcp
->reported_lost
= ntohl(rtcpheader
[i
+ 1]) & 0xffffff;
962 if (rtcp_debug_test_addr(&sin
)) {
963 ast_verbose(" Fraction lost: %ld\n", (((long) ntohl(rtcpheader
[i
+ 1]) & 0xff000000) >> 24));
964 ast_verbose(" Packets lost so far: %d\n", rtp
->rtcp
->reported_lost
);
965 ast_verbose(" Highest sequence number: %ld\n", (long) (ntohl(rtcpheader
[i
+ 2]) & 0xffff));
966 ast_verbose(" Sequence number cycles: %ld\n", (long) (ntohl(rtcpheader
[i
+ 2]) & 0xffff) >> 16);
967 ast_verbose(" Interarrival jitter: %u\n", rtp
->rtcp
->reported_jitter
);
968 ast_verbose(" Last SR(our NTP): %lu.%010lu\n",(unsigned long) ntohl(rtcpheader
[i
+ 4]) >> 16,((unsigned long) ntohl(rtcpheader
[i
+ 4]) << 16) * 4096);
969 ast_verbose(" DLSR: %4.4f (sec)\n",ntohl(rtcpheader
[i
+ 5])/65536.0);
971 ast_verbose(" RTT: %lu(sec)\n", (unsigned long) rtt
);
975 if (rtcp_debug_test_addr(&sin
))
976 ast_verbose("Received an RTCP Fast Update Request\n");
977 rtp
->f
.frametype
= AST_FRAME_CONTROL
;
978 rtp
->f
.subclass
= AST_CONTROL_VIDUPDATE
;
986 if (rtcp_debug_test_addr(&sin
))
987 ast_verbose("Received an SDES from %s:%d\n", ast_inet_ntoa(rtp
->rtcp
->them
.sin_addr
), ntohs(rtp
->rtcp
->them
.sin_port
));
990 if (rtcp_debug_test_addr(&sin
))
991 ast_verbose("Received a BYE from %s:%d\n", ast_inet_ntoa(rtp
->rtcp
->them
.sin_addr
), ntohs(rtp
->rtcp
->them
.sin_port
));
995 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
));
998 position
+= (length
+ 1);
1004 static void calc_rxstamp(struct timeval
*tv
, struct ast_rtp
*rtp
, unsigned int timestamp
, int mark
)
1008 double current_time
;
1013 if ((!rtp
->rxcore
.tv_sec
&& !rtp
->rxcore
.tv_usec
) || mark
) {
1014 gettimeofday(&rtp
->rxcore
, NULL
);
1015 rtp
->drxcore
= (double) rtp
->rxcore
.tv_sec
+ (double) rtp
->rxcore
.tv_usec
/ 1000000;
1016 /* map timestamp to a real time */
1017 rtp
->seedrxts
= timestamp
; /* Their RTP timestamp started with this */
1018 rtp
->rxcore
.tv_sec
-= timestamp
/ 8000;
1019 rtp
->rxcore
.tv_usec
-= (timestamp
% 8000) * 125;
1020 /* Round to 0.1ms for nice, pretty timestamps */
1021 rtp
->rxcore
.tv_usec
-= rtp
->rxcore
.tv_usec
% 100;
1022 if (rtp
->rxcore
.tv_usec
< 0) {
1023 /* Adjust appropriately if necessary */
1024 rtp
->rxcore
.tv_usec
+= 1000000;
1025 rtp
->rxcore
.tv_sec
-= 1;
1029 gettimeofday(&now
,NULL
);
1030 /* rxcore is the mapping between the RTP timestamp and _our_ real time from gettimeofday() */
1031 tv
->tv_sec
= rtp
->rxcore
.tv_sec
+ timestamp
/ 8000;
1032 tv
->tv_usec
= rtp
->rxcore
.tv_usec
+ (timestamp
% 8000) * 125;
1033 if (tv
->tv_usec
>= 1000000) {
1034 tv
->tv_usec
-= 1000000;
1037 prog
= (double)((timestamp
-rtp
->seedrxts
)/8000.);
1038 dtv
= (double)rtp
->drxcore
+ (double)(prog
);
1039 current_time
= (double)now
.tv_sec
+ (double)now
.tv_usec
/1000000;
1040 transit
= current_time
- dtv
;
1041 d
= transit
- rtp
->rxtransit
;
1042 rtp
->rxtransit
= transit
;
1045 rtp
->rxjitter
+= (1./16.) * (d
- rtp
->rxjitter
);
1046 if (rtp
->rtcp
&& rtp
->rxjitter
> rtp
->rtcp
->maxrxjitter
)
1047 rtp
->rtcp
->maxrxjitter
= rtp
->rxjitter
;
1048 if (rtp
->rtcp
&& rtp
->rxjitter
< rtp
->rtcp
->minrxjitter
)
1049 rtp
->rtcp
->minrxjitter
= rtp
->rxjitter
;
1052 /*! \brief Perform a Packet2Packet RTP write */
1053 static int bridge_p2p_rtp_write(struct ast_rtp
*rtp
, struct ast_rtp
*bridged
, unsigned int *rtpheader
, int len
, int hdrlen
)
1055 int res
= 0, payload
= 0, bridged_payload
= 0, mark
;
1056 struct rtpPayloadType rtpPT
;
1057 int reconstruct
= ntohl(rtpheader
[0]);
1059 /* Get fields from packet */
1060 payload
= (reconstruct
& 0x7f0000) >> 16;
1061 mark
= (((reconstruct
& 0x800000) >> 23) != 0);
1063 /* Check what the payload value should be */
1064 rtpPT
= ast_rtp_lookup_pt(rtp
, payload
);
1066 /* If the payload coming in is not one of the negotiated ones then send it to the core, this will cause formats to change and the bridge to break */
1067 if (!bridged
->current_RTP_PT
[payload
].code
)
1070 /* If the payload is DTMF, and we are listening for DTMF - then feed it into the core */
1071 if (ast_test_flag(rtp
, FLAG_P2P_NEED_DTMF
) && !rtpPT
.isAstFormat
&& rtpPT
.code
== AST_RTP_DTMF
)
1074 /* Otherwise adjust bridged payload to match */
1075 bridged_payload
= ast_rtp_lookup_code(bridged
, rtpPT
.isAstFormat
, rtpPT
.code
);
1077 /* If the mark bit has not been sent yet... do it now */
1078 if (!ast_test_flag(rtp
, FLAG_P2P_SENT_MARK
)) {
1080 ast_set_flag(rtp
, FLAG_P2P_SENT_MARK
);
1083 /* Reconstruct part of the packet */
1084 reconstruct
&= 0xFF80FFFF;
1085 reconstruct
|= (bridged_payload
<< 16);
1086 reconstruct
|= (mark
<< 23);
1087 rtpheader
[0] = htonl(reconstruct
);
1089 /* Send the packet back out */
1090 res
= sendto(bridged
->s
, (void *)rtpheader
, len
, 0, (struct sockaddr
*)&bridged
->them
, sizeof(bridged
->them
));
1092 if (!bridged
->nat
|| (bridged
->nat
&& (ast_test_flag(bridged
, FLAG_NAT_ACTIVE
) == FLAG_NAT_ACTIVE
))) {
1093 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
));
1094 } else if (((ast_test_flag(bridged
, FLAG_NAT_ACTIVE
) == FLAG_NAT_INACTIVE
) || rtpdebug
) && !ast_test_flag(bridged
, FLAG_NAT_INACTIVE_NOWARN
)) {
1095 if (option_debug
|| rtpdebug
)
1096 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
));
1097 ast_set_flag(bridged
, FLAG_NAT_INACTIVE_NOWARN
);
1100 } else if (rtp_debug_test_addr(&bridged
->them
))
1101 ast_verbose("Sent RTP P2P packet to %s:%u (type %-2.2d, len %-6.6u)\n", ast_inet_ntoa(bridged
->them
.sin_addr
), ntohs(bridged
->them
.sin_port
), bridged_payload
, len
- hdrlen
);
1106 struct ast_frame
*ast_rtp_read(struct ast_rtp
*rtp
)
1109 struct sockaddr_in sin
;
1120 unsigned int timestamp
;
1121 unsigned int *rtpheader
;
1122 struct rtpPayloadType rtpPT
;
1123 struct ast_rtp
*bridged
= NULL
;
1125 /* If time is up, kill it */
1126 if (rtp
->sending_digit
)
1127 ast_rtp_senddigit_continuation(rtp
);
1131 /* Cache where the header will go */
1132 res
= recvfrom(rtp
->s
, rtp
->rawdata
+ AST_FRIENDLY_OFFSET
, sizeof(rtp
->rawdata
) - AST_FRIENDLY_OFFSET
,
1133 0, (struct sockaddr
*)&sin
, &len
);
1135 rtpheader
= (unsigned int *)(rtp
->rawdata
+ AST_FRIENDLY_OFFSET
);
1139 if (errno
!= EAGAIN
) {
1140 ast_log(LOG_WARNING
, "RTP Read error: %s. Hanging up.\n", strerror(errno
));
1143 return &ast_null_frame
;
1147 ast_log(LOG_WARNING
, "RTP Read too short\n");
1148 return &ast_null_frame
;
1152 seqno
= ntohl(rtpheader
[0]);
1154 /* Check RTP version */
1155 version
= (seqno
& 0xC0000000) >> 30;
1157 if ((stun_handle_packet(rtp
->s
, &sin
, rtp
->rawdata
+ AST_FRIENDLY_OFFSET
, res
) == STUN_ACCEPT
) &&
1158 (!rtp
->them
.sin_port
&& !rtp
->them
.sin_addr
.s_addr
)) {
1159 memcpy(&rtp
->them
, &sin
, sizeof(rtp
->them
));
1161 return &ast_null_frame
;
1164 #if 0 /* Allow to receive RTP stream with closed transmission path */
1165 /* If we don't have the other side's address, then ignore this */
1166 if (!rtp
->them
.sin_addr
.s_addr
|| !rtp
->them
.sin_port
)
1167 return &ast_null_frame
;
1170 /* Send to whoever send to us if NAT is turned on */
1172 if ((rtp
->them
.sin_addr
.s_addr
!= sin
.sin_addr
.s_addr
) ||
1173 (rtp
->them
.sin_port
!= sin
.sin_port
)) {
1176 memcpy(&rtp
->rtcp
->them
, &sin
, sizeof(rtp
->rtcp
->them
));
1177 rtp
->rtcp
->them
.sin_port
= htons(ntohs(rtp
->them
.sin_port
)+1);
1180 ast_set_flag(rtp
, FLAG_NAT_ACTIVE
);
1181 if (option_debug
|| rtpdebug
)
1182 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
));
1186 /* If we are bridged to another RTP stream, send direct */
1187 if ((bridged
= ast_rtp_get_bridged(rtp
)) && !bridge_p2p_rtp_write(rtp
, bridged
, rtpheader
, res
, hdrlen
))
1188 return &ast_null_frame
;
1191 return &ast_null_frame
;
1193 payloadtype
= (seqno
& 0x7f0000) >> 16;
1194 padding
= seqno
& (1 << 29);
1195 mark
= seqno
& (1 << 23);
1196 ext
= seqno
& (1 << 28);
1197 cc
= (seqno
& 0xF000000) >> 24;
1199 timestamp
= ntohl(rtpheader
[1]);
1200 ssrc
= ntohl(rtpheader
[2]);
1202 if (!mark
&& rtp
->rxssrc
&& rtp
->rxssrc
!= ssrc
) {
1203 if (option_debug
|| rtpdebug
)
1204 ast_log(LOG_DEBUG
, "Forcing Marker bit, because SSRC has changed\n");
1211 /* Remove padding bytes */
1212 res
-= rtp
->rawdata
[AST_FRIENDLY_OFFSET
+ res
- 1];
1216 /* CSRC fields present */
1221 /* RTP Extension present */
1222 hdrlen
+= (ntohl(rtpheader
[hdrlen
/4]) & 0xffff) << 2;
1227 ast_log(LOG_WARNING
, "RTP Read too short (%d, expecting %d)\n", res
, hdrlen
);
1228 return &ast_null_frame
;
1231 rtp
->rxcount
++; /* Only count reasonably valid packets, this'll make the rtcp stats more accurate */
1233 if (rtp
->rxcount
==1) {
1234 /* This is the first RTP packet successfully received from source */
1235 rtp
->seedrxseqno
= seqno
;
1238 /* Do not schedule RR if RTCP isn't run */
1239 if (rtp
->rtcp
&& rtp
->rtcp
->them
.sin_addr
.s_addr
&& rtp
->rtcp
->schedid
< 1) {
1240 /* Schedule transmission of Receiver Report */
1241 rtp
->rtcp
->schedid
= ast_sched_add(rtp
->sched
, ast_rtcp_calc_interval(rtp
), ast_rtcp_write
, rtp
);
1243 if ( (int)rtp
->lastrxseqno
- (int)seqno
> 100) /* if so it would indicate that the sender cycled; allow for misordering */
1244 rtp
->cycles
+= RTP_SEQ_MOD
;
1246 rtp
->lastrxseqno
= seqno
;
1248 if (rtp
->themssrc
==0)
1249 rtp
->themssrc
= ntohl(rtpheader
[2]); /* Record their SSRC to put in future RR */
1251 if (rtp_debug_test_addr(&sin
))
1252 ast_verbose("Got RTP packet from %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
1253 ast_inet_ntoa(sin
.sin_addr
), ntohs(sin
.sin_port
), payloadtype
, seqno
, timestamp
,res
- hdrlen
);
1255 rtpPT
= ast_rtp_lookup_pt(rtp
, payloadtype
);
1256 if (!rtpPT
.isAstFormat
) {
1257 struct ast_frame
*f
= NULL
;
1259 /* This is special in-band data that's not one of our codecs */
1260 if (rtpPT
.code
== AST_RTP_DTMF
) {
1261 /* It's special -- rfc2833 process it */
1262 if (rtp_debug_test_addr(&sin
)) {
1263 unsigned char *data
;
1265 unsigned int event_end
;
1266 unsigned int duration
;
1267 data
= rtp
->rawdata
+ AST_FRIENDLY_OFFSET
+ hdrlen
;
1268 event
= ntohl(*((unsigned int *)(data
)));
1270 event_end
= ntohl(*((unsigned int *)(data
)));
1273 duration
= ntohl(*((unsigned int *)(data
)));
1275 ast_verbose("Got RTP RFC2833 from %s:%u (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
);
1277 f
= process_rfc2833(rtp
, rtp
->rawdata
+ AST_FRIENDLY_OFFSET
+ hdrlen
, res
- hdrlen
, seqno
, timestamp
);
1278 } else if (rtpPT
.code
== AST_RTP_CISCO_DTMF
) {
1279 /* It's really special -- process it the Cisco way */
1280 if (rtp
->lastevent
<= seqno
|| (rtp
->lastevent
>= 65530 && seqno
<= 6)) {
1281 f
= process_cisco_dtmf(rtp
, rtp
->rawdata
+ AST_FRIENDLY_OFFSET
+ hdrlen
, res
- hdrlen
);
1282 rtp
->lastevent
= seqno
;
1284 } else if (rtpPT
.code
== AST_RTP_CN
) {
1286 f
= process_rfc3389(rtp
, rtp
->rawdata
+ AST_FRIENDLY_OFFSET
+ hdrlen
, res
- hdrlen
);
1288 ast_log(LOG_NOTICE
, "Unknown RTP codec %d received from '%s'\n", payloadtype
, ast_inet_ntoa(rtp
->them
.sin_addr
));
1290 return f
? f
: &ast_null_frame
;
1292 rtp
->lastrxformat
= rtp
->f
.subclass
= rtpPT
.code
;
1293 rtp
->f
.frametype
= (rtp
->f
.subclass
< AST_FORMAT_MAX_AUDIO
) ? AST_FRAME_VOICE
: AST_FRAME_VIDEO
;
1296 rtp
->lastrxts
= timestamp
;
1298 rtp
->rxseqno
= seqno
;
1300 /* Record received timestamp as last received now */
1301 rtp
->lastrxts
= timestamp
;
1304 rtp
->f
.datalen
= res
- hdrlen
;
1305 rtp
->f
.data
= rtp
->rawdata
+ hdrlen
+ AST_FRIENDLY_OFFSET
;
1306 rtp
->f
.offset
= hdrlen
+ AST_FRIENDLY_OFFSET
;
1307 rtp
->f
.seqno
= seqno
;
1308 if (rtp
->f
.subclass
< AST_FORMAT_MAX_AUDIO
) {
1309 rtp
->f
.samples
= ast_codec_get_samples(&rtp
->f
);
1310 if (rtp
->f
.subclass
== AST_FORMAT_SLINEAR
)
1311 ast_frame_byteswap_be(&rtp
->f
);
1312 calc_rxstamp(&rtp
->f
.delivery
, rtp
, timestamp
, mark
);
1313 /* Add timing data to let ast_generic_bridge() put the frame into a jitterbuf */
1314 ast_set_flag(&rtp
->f
, AST_FRFLAG_HAS_TIMING_INFO
);
1315 rtp
->f
.ts
= timestamp
/ 8;
1316 rtp
->f
.len
= rtp
->f
.samples
/ ( (ast_format_rate(rtp
->f
.subclass
) == 16000) ? 16 : 8 );
1318 /* Video -- samples is # of samples vs. 90000 */
1319 if (!rtp
->lastividtimestamp
)
1320 rtp
->lastividtimestamp
= timestamp
;
1321 rtp
->f
.samples
= timestamp
- rtp
->lastividtimestamp
;
1322 rtp
->lastividtimestamp
= timestamp
;
1323 rtp
->f
.delivery
.tv_sec
= 0;
1324 rtp
->f
.delivery
.tv_usec
= 0;
1326 rtp
->f
.subclass
|= 0x1;
1333 /* The following array defines the MIME Media type (and subtype) for each
1334 of our codecs, or RTP-specific data type. */
1336 struct rtpPayloadType payloadType
;
1340 {{1, AST_FORMAT_G723_1
}, "audio", "G723"},
1341 {{1, AST_FORMAT_GSM
}, "audio", "GSM"},
1342 {{1, AST_FORMAT_ULAW
}, "audio", "PCMU"},
1343 {{1, AST_FORMAT_ULAW
}, "audio", "G711U"},
1344 {{1, AST_FORMAT_ALAW
}, "audio", "PCMA"},
1345 {{1, AST_FORMAT_ALAW
}, "audio", "G711A"},
1346 {{1, AST_FORMAT_G726
}, "audio", "G726-32"},
1347 {{1, AST_FORMAT_ADPCM
}, "audio", "DVI4"},
1348 {{1, AST_FORMAT_SLINEAR
}, "audio", "L16"},
1349 {{1, AST_FORMAT_LPC10
}, "audio", "LPC"},
1350 {{1, AST_FORMAT_G729A
}, "audio", "G729"},
1351 {{1, AST_FORMAT_G729A
}, "audio", "G729A"},
1352 {{1, AST_FORMAT_SPEEX
}, "audio", "speex"},
1353 {{1, AST_FORMAT_ILBC
}, "audio", "iLBC"},
1354 {{1, AST_FORMAT_G722
}, "audio", "G722"},
1355 {{1, AST_FORMAT_G726_AAL2
}, "audio", "AAL2-G726-32"},
1356 {{0, AST_RTP_DTMF
}, "audio", "telephone-event"},
1357 {{0, AST_RTP_CISCO_DTMF
}, "audio", "cisco-telephone-event"},
1358 {{0, AST_RTP_CN
}, "audio", "CN"},
1359 {{1, AST_FORMAT_JPEG
}, "video", "JPEG"},
1360 {{1, AST_FORMAT_PNG
}, "video", "PNG"},
1361 {{1, AST_FORMAT_H261
}, "video", "H261"},
1362 {{1, AST_FORMAT_H263
}, "video", "H263"},
1363 {{1, AST_FORMAT_H263_PLUS
}, "video", "h263-1998"},
1364 {{1, AST_FORMAT_H264
}, "video", "H264"},
1367 /* Static (i.e., well-known) RTP payload types for our "AST_FORMAT..."s:
1368 also, our own choices for dynamic payload types. This is our master
1369 table for transmission */
1370 static struct rtpPayloadType static_RTP_PT
[MAX_RTP_PT
] = {
1371 [0] = {1, AST_FORMAT_ULAW
},
1372 #ifdef USE_DEPRECATED_G726
1373 [2] = {1, AST_FORMAT_G726
}, /* Technically this is G.721, but if Cisco can do it, so can we... */
1375 [3] = {1, AST_FORMAT_GSM
},
1376 [4] = {1, AST_FORMAT_G723_1
},
1377 [5] = {1, AST_FORMAT_ADPCM
}, /* 8 kHz */
1378 [6] = {1, AST_FORMAT_ADPCM
}, /* 16 kHz */
1379 [7] = {1, AST_FORMAT_LPC10
},
1380 [8] = {1, AST_FORMAT_ALAW
},
1381 [9] = {1, AST_FORMAT_G722
},
1382 [10] = {1, AST_FORMAT_SLINEAR
}, /* 2 channels */
1383 [11] = {1, AST_FORMAT_SLINEAR
}, /* 1 channel */
1384 [13] = {0, AST_RTP_CN
},
1385 [16] = {1, AST_FORMAT_ADPCM
}, /* 11.025 kHz */
1386 [17] = {1, AST_FORMAT_ADPCM
}, /* 22.050 kHz */
1387 [18] = {1, AST_FORMAT_G729A
},
1388 [19] = {0, AST_RTP_CN
}, /* Also used for CN */
1389 [26] = {1, AST_FORMAT_JPEG
},
1390 [31] = {1, AST_FORMAT_H261
},
1391 [34] = {1, AST_FORMAT_H263
},
1392 [103] = {1, AST_FORMAT_H263_PLUS
},
1393 [97] = {1, AST_FORMAT_ILBC
},
1394 [99] = {1, AST_FORMAT_H264
},
1395 [101] = {0, AST_RTP_DTMF
},
1396 [110] = {1, AST_FORMAT_SPEEX
},
1397 [111] = {1, AST_FORMAT_G726
},
1398 [112] = {1, AST_FORMAT_G726_AAL2
},
1399 [121] = {0, AST_RTP_CISCO_DTMF
}, /* Must be type 121 */
1402 void ast_rtp_pt_clear(struct ast_rtp
* rtp
)
1409 ast_mutex_lock(&rtp
->bridge_lock
);
1411 for (i
= 0; i
< MAX_RTP_PT
; ++i
) {
1412 rtp
->current_RTP_PT
[i
].isAstFormat
= 0;
1413 rtp
->current_RTP_PT
[i
].code
= 0;
1416 rtp
->rtp_lookup_code_cache_isAstFormat
= 0;
1417 rtp
->rtp_lookup_code_cache_code
= 0;
1418 rtp
->rtp_lookup_code_cache_result
= 0;
1420 ast_mutex_unlock(&rtp
->bridge_lock
);
1423 void ast_rtp_pt_default(struct ast_rtp
* rtp
)
1427 ast_mutex_lock(&rtp
->bridge_lock
);
1429 /* Initialize to default payload types */
1430 for (i
= 0; i
< MAX_RTP_PT
; ++i
) {
1431 rtp
->current_RTP_PT
[i
].isAstFormat
= static_RTP_PT
[i
].isAstFormat
;
1432 rtp
->current_RTP_PT
[i
].code
= static_RTP_PT
[i
].code
;
1435 rtp
->rtp_lookup_code_cache_isAstFormat
= 0;
1436 rtp
->rtp_lookup_code_cache_code
= 0;
1437 rtp
->rtp_lookup_code_cache_result
= 0;
1439 ast_mutex_unlock(&rtp
->bridge_lock
);
1442 void ast_rtp_pt_copy(struct ast_rtp
*dest
, struct ast_rtp
*src
)
1446 ast_mutex_lock(&dest
->bridge_lock
);
1447 ast_mutex_lock(&src
->bridge_lock
);
1449 for (i
=0; i
< MAX_RTP_PT
; ++i
) {
1450 dest
->current_RTP_PT
[i
].isAstFormat
=
1451 src
->current_RTP_PT
[i
].isAstFormat
;
1452 dest
->current_RTP_PT
[i
].code
=
1453 src
->current_RTP_PT
[i
].code
;
1455 dest
->rtp_lookup_code_cache_isAstFormat
= 0;
1456 dest
->rtp_lookup_code_cache_code
= 0;
1457 dest
->rtp_lookup_code_cache_result
= 0;
1459 ast_mutex_unlock(&src
->bridge_lock
);
1460 ast_mutex_unlock(&dest
->bridge_lock
);
1463 /*! \brief Get channel driver interface structure */
1464 static struct ast_rtp_protocol
*get_proto(struct ast_channel
*chan
)
1466 struct ast_rtp_protocol
*cur
= NULL
;
1468 AST_LIST_LOCK(&protos
);
1469 AST_LIST_TRAVERSE(&protos
, cur
, list
) {
1470 if (cur
->type
== chan
->tech
->type
)
1473 AST_LIST_UNLOCK(&protos
);
1478 int ast_rtp_early_bridge(struct ast_channel
*dest
, struct ast_channel
*src
)
1480 struct ast_rtp
*destp
= NULL
, *srcp
= NULL
; /* Audio RTP Channels */
1481 struct ast_rtp
*vdestp
= NULL
, *vsrcp
= NULL
; /* Video RTP channels */
1482 struct ast_rtp_protocol
*destpr
= NULL
, *srcpr
= NULL
;
1483 enum ast_rtp_get_result audio_dest_res
= AST_RTP_GET_FAILED
, video_dest_res
= AST_RTP_GET_FAILED
;
1484 enum ast_rtp_get_result audio_src_res
= AST_RTP_GET_FAILED
, video_src_res
= AST_RTP_GET_FAILED
;
1485 int srccodec
, destcodec
, nat_active
= 0;
1488 ast_channel_lock(dest
);
1490 while(ast_channel_trylock(src
)) {
1491 ast_channel_unlock(dest
);
1493 ast_channel_lock(dest
);
1497 /* Find channel driver interfaces */
1498 destpr
= get_proto(dest
);
1500 srcpr
= get_proto(src
);
1503 ast_log(LOG_DEBUG
, "Channel '%s' has no RTP, not doing anything\n", dest
->name
);
1504 ast_channel_unlock(dest
);
1506 ast_channel_unlock(src
);
1511 ast_log(LOG_DEBUG
, "Channel '%s' has no RTP, not doing anything\n", src
? src
->name
: "<unspecified>");
1512 ast_channel_unlock(dest
);
1514 ast_channel_unlock(src
);
1518 /* Get audio and video interface (if native bridge is possible) */
1519 audio_dest_res
= destpr
->get_rtp_info(dest
, &destp
);
1520 video_dest_res
= destpr
->get_vrtp_info
? destpr
->get_vrtp_info(dest
, &vdestp
) : AST_RTP_GET_FAILED
;
1522 audio_src_res
= srcpr
->get_rtp_info(src
, &srcp
);
1523 video_src_res
= srcpr
->get_vrtp_info
? srcpr
->get_vrtp_info(src
, &vsrcp
) : AST_RTP_GET_FAILED
;
1526 /* Check if bridge is still possible (In SIP canreinvite=no stops this, like NAT) */
1527 if (audio_dest_res
!= AST_RTP_TRY_NATIVE
) {
1528 /* Somebody doesn't want to play... */
1529 ast_channel_unlock(dest
);
1531 ast_channel_unlock(src
);
1534 if (audio_src_res
== AST_RTP_TRY_NATIVE
&& srcpr
->get_codec
)
1535 srccodec
= srcpr
->get_codec(src
);
1538 if (audio_dest_res
== AST_RTP_TRY_NATIVE
&& destpr
->get_codec
)
1539 destcodec
= destpr
->get_codec(dest
);
1542 /* Ensure we have at least one matching codec */
1543 if (!(srccodec
& destcodec
)) {
1544 ast_channel_unlock(dest
);
1546 ast_channel_unlock(src
);
1549 /* Consider empty media as non-existant */
1550 if (audio_src_res
== AST_RTP_TRY_NATIVE
&& !srcp
->them
.sin_addr
.s_addr
)
1552 /* If the client has NAT stuff turned on then just safe NAT is active */
1553 if (srcp
&& (srcp
->nat
|| ast_test_flag(srcp
, FLAG_NAT_ACTIVE
)))
1555 /* Bridge media early */
1556 if (destpr
->set_rtp_peer(dest
, srcp
, vsrcp
, srccodec
, nat_active
))
1557 ast_log(LOG_WARNING
, "Channel '%s' failed to setup early bridge to '%s'\n", dest
->name
, src
? src
->name
: "<unspecified>");
1558 ast_channel_unlock(dest
);
1560 ast_channel_unlock(src
);
1562 ast_log(LOG_DEBUG
, "Setting early bridge SDP of '%s' with that of '%s'\n", dest
->name
, src
? src
->name
: "<unspecified>");
1566 int ast_rtp_make_compatible(struct ast_channel
*dest
, struct ast_channel
*src
, int media
)
1568 struct ast_rtp
*destp
= NULL
, *srcp
= NULL
; /* Audio RTP Channels */
1569 struct ast_rtp
*vdestp
= NULL
, *vsrcp
= NULL
; /* Video RTP channels */
1570 struct ast_rtp_protocol
*destpr
= NULL
, *srcpr
= NULL
;
1571 enum ast_rtp_get_result audio_dest_res
= AST_RTP_GET_FAILED
, video_dest_res
= AST_RTP_GET_FAILED
;
1572 enum ast_rtp_get_result audio_src_res
= AST_RTP_GET_FAILED
, video_src_res
= AST_RTP_GET_FAILED
;
1573 int srccodec
, destcodec
;
1576 ast_channel_lock(dest
);
1577 while(ast_channel_trylock(src
)) {
1578 ast_channel_unlock(dest
);
1580 ast_channel_lock(dest
);
1583 /* Find channel driver interfaces */
1584 if (!(destpr
= get_proto(dest
))) {
1586 ast_log(LOG_DEBUG
, "Channel '%s' has no RTP, not doing anything\n", dest
->name
);
1587 ast_channel_unlock(dest
);
1588 ast_channel_unlock(src
);
1591 if (!(srcpr
= get_proto(src
))) {
1593 ast_log(LOG_DEBUG
, "Channel '%s' has no RTP, not doing anything\n", src
->name
);
1594 ast_channel_unlock(dest
);
1595 ast_channel_unlock(src
);
1599 /* Get audio and video interface (if native bridge is possible) */
1600 audio_dest_res
= destpr
->get_rtp_info(dest
, &destp
);
1601 video_dest_res
= destpr
->get_vrtp_info
? destpr
->get_vrtp_info(dest
, &vdestp
) : AST_RTP_GET_FAILED
;
1602 audio_src_res
= srcpr
->get_rtp_info(src
, &srcp
);
1603 video_src_res
= srcpr
->get_vrtp_info
? srcpr
->get_vrtp_info(src
, &vsrcp
) : AST_RTP_GET_FAILED
;
1605 /* Ensure we have at least one matching codec */
1606 if (srcpr
->get_codec
)
1607 srccodec
= srcpr
->get_codec(src
);
1610 if (destpr
->get_codec
)
1611 destcodec
= destpr
->get_codec(dest
);
1615 /* Check if bridge is still possible (In SIP canreinvite=no stops this, like NAT) */
1616 if (audio_dest_res
!= AST_RTP_TRY_NATIVE
|| audio_src_res
!= AST_RTP_TRY_NATIVE
|| !(srccodec
& destcodec
)) {
1617 /* Somebody doesn't want to play... */
1618 ast_channel_unlock(dest
);
1619 ast_channel_unlock(src
);
1622 ast_rtp_pt_copy(destp
, srcp
);
1623 if (vdestp
&& vsrcp
)
1624 ast_rtp_pt_copy(vdestp
, vsrcp
);
1627 if (destpr
->set_rtp_peer(dest
, srcp
, vsrcp
, srccodec
, ast_test_flag(srcp
, FLAG_NAT_ACTIVE
)))
1628 ast_log(LOG_WARNING
, "Channel '%s' failed to setup early bridge to '%s'\n", dest
->name
, src
->name
);
1630 ast_channel_unlock(dest
);
1631 ast_channel_unlock(src
);
1633 ast_log(LOG_DEBUG
, "Seeded SDP of '%s' with that of '%s'\n", dest
->name
, src
->name
);
1637 /*! \brief Make a note of a RTP payload type that was seen in a SDP "m=" line.
1638 * By default, use the well-known value for this type (although it may
1639 * still be set to a different value by a subsequent "a=rtpmap:" line)
1641 void ast_rtp_set_m_type(struct ast_rtp
* rtp
, int pt
)
1643 if (pt
< 0 || pt
> MAX_RTP_PT
|| static_RTP_PT
[pt
].code
== 0)
1644 return; /* bogus payload type */
1646 ast_mutex_lock(&rtp
->bridge_lock
);
1647 rtp
->current_RTP_PT
[pt
] = static_RTP_PT
[pt
];
1648 ast_mutex_unlock(&rtp
->bridge_lock
);
1651 /*! \brief remove setting from payload type list if the rtpmap header indicates
1652 an unknown media type */
1653 void ast_rtp_unset_m_type(struct ast_rtp
* rtp
, int pt
)
1655 ast_mutex_lock(&rtp
->bridge_lock
);
1656 rtp
->current_RTP_PT
[pt
].isAstFormat
= 0;
1657 rtp
->current_RTP_PT
[pt
].code
= 0;
1658 ast_mutex_unlock(&rtp
->bridge_lock
);
1661 /*! \brief Make a note of a RTP payload type (with MIME type) that was seen in
1662 * an SDP "a=rtpmap:" line.
1663 * \return 0 if the MIME type was found and set, -1 if it wasn't found
1665 int ast_rtp_set_rtpmap_type(struct ast_rtp
*rtp
, int pt
,
1666 char *mimeType
, char *mimeSubtype
,
1667 enum ast_rtp_options options
)
1672 if (pt
< 0 || pt
> MAX_RTP_PT
)
1673 return -1; /* bogus payload type */
1675 ast_mutex_lock(&rtp
->bridge_lock
);
1677 for (i
= 0; i
< sizeof(mimeTypes
)/sizeof(mimeTypes
[0]); ++i
) {
1678 if (strcasecmp(mimeSubtype
, mimeTypes
[i
].subtype
) == 0 &&
1679 strcasecmp(mimeType
, mimeTypes
[i
].type
) == 0) {
1681 rtp
->current_RTP_PT
[pt
] = mimeTypes
[i
].payloadType
;
1682 if ((mimeTypes
[i
].payloadType
.code
== AST_FORMAT_G726
) &&
1683 mimeTypes
[i
].payloadType
.isAstFormat
&&
1684 (options
& AST_RTP_OPT_G726_NONSTANDARD
))
1685 rtp
->current_RTP_PT
[pt
].code
= AST_FORMAT_G726_AAL2
;
1690 ast_mutex_unlock(&rtp
->bridge_lock
);
1692 return (found
? 0 : -1);
1695 /*! \brief Return the union of all of the codecs that were set by rtp_set...() calls
1696 * They're returned as two distinct sets: AST_FORMATs, and AST_RTPs */
1697 void ast_rtp_get_current_formats(struct ast_rtp
* rtp
,
1698 int* astFormats
, int* nonAstFormats
)
1702 ast_mutex_lock(&rtp
->bridge_lock
);
1704 *astFormats
= *nonAstFormats
= 0;
1705 for (pt
= 0; pt
< MAX_RTP_PT
; ++pt
) {
1706 if (rtp
->current_RTP_PT
[pt
].isAstFormat
) {
1707 *astFormats
|= rtp
->current_RTP_PT
[pt
].code
;
1709 *nonAstFormats
|= rtp
->current_RTP_PT
[pt
].code
;
1713 ast_mutex_unlock(&rtp
->bridge_lock
);
1718 struct rtpPayloadType
ast_rtp_lookup_pt(struct ast_rtp
* rtp
, int pt
)
1720 struct rtpPayloadType result
;
1722 result
.isAstFormat
= result
.code
= 0;
1724 if (pt
< 0 || pt
> MAX_RTP_PT
)
1725 return result
; /* bogus payload type */
1727 /* Start with negotiated codecs */
1728 ast_mutex_lock(&rtp
->bridge_lock
);
1729 result
= rtp
->current_RTP_PT
[pt
];
1730 ast_mutex_unlock(&rtp
->bridge_lock
);
1732 /* If it doesn't exist, check our static RTP type list, just in case */
1734 result
= static_RTP_PT
[pt
];
1739 /*! \brief Looks up an RTP code out of our *static* outbound list */
1740 int ast_rtp_lookup_code(struct ast_rtp
* rtp
, const int isAstFormat
, const int code
)
1744 ast_mutex_lock(&rtp
->bridge_lock
);
1746 if (isAstFormat
== rtp
->rtp_lookup_code_cache_isAstFormat
&&
1747 code
== rtp
->rtp_lookup_code_cache_code
) {
1748 /* Use our cached mapping, to avoid the overhead of the loop below */
1749 pt
= rtp
->rtp_lookup_code_cache_result
;
1750 ast_mutex_unlock(&rtp
->bridge_lock
);
1754 /* Check the dynamic list first */
1755 for (pt
= 0; pt
< MAX_RTP_PT
; ++pt
) {
1756 if (rtp
->current_RTP_PT
[pt
].code
== code
&& rtp
->current_RTP_PT
[pt
].isAstFormat
== isAstFormat
) {
1757 rtp
->rtp_lookup_code_cache_isAstFormat
= isAstFormat
;
1758 rtp
->rtp_lookup_code_cache_code
= code
;
1759 rtp
->rtp_lookup_code_cache_result
= pt
;
1760 ast_mutex_unlock(&rtp
->bridge_lock
);
1765 /* Then the static list */
1766 for (pt
= 0; pt
< MAX_RTP_PT
; ++pt
) {
1767 if (static_RTP_PT
[pt
].code
== code
&& static_RTP_PT
[pt
].isAstFormat
== isAstFormat
) {
1768 rtp
->rtp_lookup_code_cache_isAstFormat
= isAstFormat
;
1769 rtp
->rtp_lookup_code_cache_code
= code
;
1770 rtp
->rtp_lookup_code_cache_result
= pt
;
1771 ast_mutex_unlock(&rtp
->bridge_lock
);
1776 ast_mutex_unlock(&rtp
->bridge_lock
);
1781 const char *ast_rtp_lookup_mime_subtype(const int isAstFormat
, const int code
,
1782 enum ast_rtp_options options
)
1786 for (i
= 0; i
< sizeof(mimeTypes
)/sizeof(mimeTypes
[0]); ++i
) {
1787 if ((mimeTypes
[i
].payloadType
.code
== code
) && (mimeTypes
[i
].payloadType
.isAstFormat
== isAstFormat
)) {
1789 (code
== AST_FORMAT_G726_AAL2
) &&
1790 (options
& AST_RTP_OPT_G726_NONSTANDARD
))
1793 return mimeTypes
[i
].subtype
;
1800 char *ast_rtp_lookup_mime_multiple(char *buf
, size_t size
, const int capability
,
1801 const int isAstFormat
, enum ast_rtp_options options
)
1811 snprintf(end
, size
, "0x%x (", capability
);
1818 for (format
= 1; format
< AST_RTP_MAX
; format
<<= 1) {
1819 if (capability
& format
) {
1820 const char *name
= ast_rtp_lookup_mime_subtype(isAstFormat
, format
, options
);
1822 snprintf(end
, size
, "%s|", name
);
1830 snprintf(start
, size
, "nothing)");
1837 static int rtp_socket(void)
1841 s
= socket(AF_INET
, SOCK_DGRAM
, 0);
1843 flags
= fcntl(s
, F_GETFL
);
1844 fcntl(s
, F_SETFL
, flags
| O_NONBLOCK
);
1847 setsockopt(s
, SOL_SOCKET
, SO_NO_CHECK
, &nochecksums
, sizeof(nochecksums
));
1854 * \brief Initialize a new RTCP session.
1856 * \returns The newly initialized RTCP session.
1858 static struct ast_rtcp
*ast_rtcp_new(void)
1860 struct ast_rtcp
*rtcp
;
1862 if (!(rtcp
= ast_calloc(1, sizeof(*rtcp
))))
1864 rtcp
->s
= rtp_socket();
1865 rtcp
->us
.sin_family
= AF_INET
;
1866 rtcp
->them
.sin_family
= AF_INET
;
1870 ast_log(LOG_WARNING
, "Unable to allocate RTCP socket: %s\n", strerror(errno
));
1878 * \brief Initialize a new RTP structure.
1881 void ast_rtp_new_init(struct ast_rtp
*rtp
)
1883 ast_mutex_init(&rtp
->bridge_lock
);
1885 rtp
->them
.sin_family
= AF_INET
;
1886 rtp
->us
.sin_family
= AF_INET
;
1887 rtp
->ssrc
= ast_random();
1888 rtp
->seqno
= ast_random() & 0xffff;
1889 ast_set_flag(rtp
, FLAG_HAS_DTMF
);
1894 struct ast_rtp
*ast_rtp_new_with_bindaddr(struct sched_context
*sched
, struct io_context
*io
, int rtcpenable
, int callbackmode
, struct in_addr addr
)
1896 struct ast_rtp
*rtp
;
1901 if (!(rtp
= ast_calloc(1, sizeof(*rtp
))))
1904 ast_rtp_new_init(rtp
);
1906 rtp
->s
= rtp_socket();
1909 ast_log(LOG_ERROR
, "Unable to allocate socket: %s\n", strerror(errno
));
1912 if (sched
&& rtcpenable
) {
1914 rtp
->rtcp
= ast_rtcp_new();
1917 /* Select a random port number in the range of possible RTP */
1918 x
= (ast_random() % (rtpend
-rtpstart
)) + rtpstart
;
1920 /* Save it for future references. */
1922 /* Iterate tring to bind that port and incrementing it otherwise untill a port was found or no ports are available. */
1924 /* Must be an even port number by RTP spec */
1925 rtp
->us
.sin_port
= htons(x
);
1926 rtp
->us
.sin_addr
= addr
;
1927 /* If there's rtcp, initialize it as well. */
1929 rtp
->rtcp
->us
.sin_port
= htons(x
+ 1);
1930 rtp
->rtcp
->us
.sin_addr
= addr
;
1932 /* Try to bind it/them. */
1933 if (!(first
= bind(rtp
->s
, (struct sockaddr
*)&rtp
->us
, sizeof(rtp
->us
))) &&
1934 (!rtp
->rtcp
|| !bind(rtp
->rtcp
->s
, (struct sockaddr
*)&rtp
->rtcp
->us
, sizeof(rtp
->rtcp
->us
))))
1937 /* Primary bind succeeded! Gotta recreate it */
1939 rtp
->s
= rtp_socket();
1941 if (errno
!= EADDRINUSE
) {
1942 /* We got an error that wasn't expected, abort! */
1943 ast_log(LOG_ERROR
, "Unexpected bind error: %s\n", strerror(errno
));
1946 close(rtp
->rtcp
->s
);
1952 /* The port was used, increment it (by two). */
1954 /* Did we go over the limit ? */
1956 /* then, start from the begingig. */
1957 x
= (rtpstart
+ 1) & ~1;
1958 /* Check if we reached the place were we started. */
1959 if (x
== startplace
) {
1960 /* If so, there's no ports available. */
1961 ast_log(LOG_ERROR
, "No RTP ports remaining. Can't setup media stream for this call.\n");
1964 close(rtp
->rtcp
->s
);
1974 rtp
->ioid
= ast_io_add(rtp
->io
, rtp
->s
, rtpread
, AST_IO_IN
, rtp
);
1975 ast_set_flag(rtp
, FLAG_CALLBACK_MODE
);
1977 ast_rtp_pt_default(rtp
);
1981 struct ast_rtp
*ast_rtp_new(struct sched_context
*sched
, struct io_context
*io
, int rtcpenable
, int callbackmode
)
1985 memset(&ia
, 0, sizeof(ia
));
1986 return ast_rtp_new_with_bindaddr(sched
, io
, rtcpenable
, callbackmode
, ia
);
1989 int ast_rtp_settos(struct ast_rtp
*rtp
, int tos
)
1993 if ((res
= setsockopt(rtp
->s
, IPPROTO_IP
, IP_TOS
, &tos
, sizeof(tos
))))
1994 ast_log(LOG_WARNING
, "Unable to set TOS to %d\n", tos
);
1998 void ast_rtp_new_source(struct ast_rtp
*rtp
)
2000 rtp
->set_marker_bit
= 1;
2001 rtp
->ssrc
= ast_random();
2005 void ast_rtp_set_peer(struct ast_rtp
*rtp
, struct sockaddr_in
*them
)
2007 rtp
->them
.sin_port
= them
->sin_port
;
2008 rtp
->them
.sin_addr
= them
->sin_addr
;
2010 rtp
->rtcp
->them
.sin_port
= htons(ntohs(them
->sin_port
) + 1);
2011 rtp
->rtcp
->them
.sin_addr
= them
->sin_addr
;
2016 int ast_rtp_get_peer(struct ast_rtp
*rtp
, struct sockaddr_in
*them
)
2018 if ((them
->sin_family
!= AF_INET
) ||
2019 (them
->sin_port
!= rtp
->them
.sin_port
) ||
2020 (them
->sin_addr
.s_addr
!= rtp
->them
.sin_addr
.s_addr
)) {
2021 them
->sin_family
= AF_INET
;
2022 them
->sin_port
= rtp
->them
.sin_port
;
2023 them
->sin_addr
= rtp
->them
.sin_addr
;
2029 void ast_rtp_get_us(struct ast_rtp
*rtp
, struct sockaddr_in
*us
)
2034 struct ast_rtp
*ast_rtp_get_bridged(struct ast_rtp
*rtp
)
2036 struct ast_rtp
*bridged
= NULL
;
2038 ast_mutex_lock(&rtp
->bridge_lock
);
2039 bridged
= rtp
->bridged
;
2040 ast_mutex_unlock(&rtp
->bridge_lock
);
2045 void ast_rtp_stop(struct ast_rtp
*rtp
)
2047 AST_SCHED_DEL(rtp
->sched
, rtp
->rtcp
->schedid
);
2049 memset(&rtp
->them
.sin_addr
, 0, sizeof(rtp
->them
.sin_addr
));
2050 memset(&rtp
->them
.sin_port
, 0, sizeof(rtp
->them
.sin_port
));
2052 memset(&rtp
->rtcp
->them
.sin_addr
, 0, sizeof(rtp
->rtcp
->them
.sin_addr
));
2053 memset(&rtp
->rtcp
->them
.sin_port
, 0, sizeof(rtp
->rtcp
->them
.sin_port
));
2056 ast_clear_flag(rtp
, FLAG_P2P_SENT_MARK
);
2059 void ast_rtp_reset(struct ast_rtp
*rtp
)
2061 memset(&rtp
->rxcore
, 0, sizeof(rtp
->rxcore
));
2062 memset(&rtp
->txcore
, 0, sizeof(rtp
->txcore
));
2063 memset(&rtp
->dtmfmute
, 0, sizeof(rtp
->dtmfmute
));
2065 rtp
->lastdigitts
= 0;
2067 rtp
->lastividtimestamp
= 0;
2068 rtp
->lastovidtimestamp
= 0;
2069 rtp
->lasteventseqn
= 0;
2071 rtp
->lasttxformat
= 0;
2072 rtp
->lastrxformat
= 0;
2074 rtp
->dtmfsamples
= 0;
2079 char *ast_rtp_get_quality(struct ast_rtp
*rtp
, struct ast_rtp_quality
*qual
)
2083 *themssrc their ssrc
2085 *rxjitter our calculated jitter(rx)
2086 *rxcount no. received packets
2087 *txjitter reported jitter of the other end
2088 *txcount transmitted packets
2089 *rlp remote lost packets
2090 *rtt round trip time
2094 qual
->local_ssrc
= rtp
->ssrc
;
2095 qual
->local_jitter
= rtp
->rxjitter
;
2096 qual
->local_count
= rtp
->rxcount
;
2097 qual
->remote_ssrc
= rtp
->themssrc
;
2098 qual
->remote_count
= rtp
->txcount
;
2100 qual
->local_lostpackets
= rtp
->rtcp
->expected_prior
- rtp
->rtcp
->received_prior
;
2101 qual
->remote_lostpackets
= rtp
->rtcp
->reported_lost
;
2102 qual
->remote_jitter
= rtp
->rtcp
->reported_jitter
/ 65536.0;
2103 qual
->rtt
= rtp
->rtcp
->rtt
;
2107 snprintf(rtp
->rtcp
->quality
, sizeof(rtp
->rtcp
->quality
),
2108 "ssrc=%u;themssrc=%u;lp=%u;rxjitter=%f;rxcount=%u;txjitter=%f;txcount=%u;rlp=%u;rtt=%f",
2111 rtp
->rtcp
->expected_prior
- rtp
->rtcp
->received_prior
,
2114 (double)rtp
->rtcp
->reported_jitter
/ 65536.0,
2116 rtp
->rtcp
->reported_lost
,
2118 return rtp
->rtcp
->quality
;
2120 return "<Unknown> - RTP/RTCP has already been destroyed";
2123 void ast_rtp_destroy(struct ast_rtp
*rtp
)
2125 if (rtcp_debug_test_addr(&rtp
->them
) || rtcpstats
) {
2126 /*Print some info on the call here */
2127 ast_verbose(" RTP-stats\n");
2128 ast_verbose("* Our Receiver:\n");
2129 ast_verbose(" SSRC: %u\n", rtp
->themssrc
);
2130 ast_verbose(" Received packets: %u\n", rtp
->rxcount
);
2131 ast_verbose(" Lost packets: %u\n", rtp
->rtcp
->expected_prior
- rtp
->rtcp
->received_prior
);
2132 ast_verbose(" Jitter: %.4f\n", rtp
->rxjitter
);
2133 ast_verbose(" Transit: %.4f\n", rtp
->rxtransit
);
2134 ast_verbose(" RR-count: %u\n", rtp
->rtcp
->rr_count
);
2135 ast_verbose("* Our Sender:\n");
2136 ast_verbose(" SSRC: %u\n", rtp
->ssrc
);
2137 ast_verbose(" Sent packets: %u\n", rtp
->txcount
);
2138 ast_verbose(" Lost packets: %u\n", rtp
->rtcp
->reported_lost
);
2139 ast_verbose(" Jitter: %u\n", rtp
->rtcp
->reported_jitter
/ (unsigned int)65536.0);
2140 ast_verbose(" SR-count: %u\n", rtp
->rtcp
->sr_count
);
2141 ast_verbose(" RTT: %f\n", rtp
->rtcp
->rtt
);
2145 ast_smoother_free(rtp
->smoother
);
2147 ast_io_remove(rtp
->io
, rtp
->ioid
);
2151 AST_SCHED_DEL(rtp
->sched
, rtp
->rtcp
->schedid
);
2152 close(rtp
->rtcp
->s
);
2157 ast_mutex_destroy(&rtp
->bridge_lock
);
2162 static unsigned int calc_txstamp(struct ast_rtp
*rtp
, struct timeval
*delivery
)
2166 if (ast_tvzero(rtp
->txcore
)) {
2167 rtp
->txcore
= ast_tvnow();
2168 /* Round to 20ms for nice, pretty timestamps */
2169 rtp
->txcore
.tv_usec
-= rtp
->txcore
.tv_usec
% 20000;
2171 /* Use previous txcore if available */
2172 t
= (delivery
&& !ast_tvzero(*delivery
)) ? *delivery
: ast_tvnow();
2173 ms
= ast_tvdiff_ms(t
, rtp
->txcore
);
2176 /* Use what we just got for next time */
2178 return (unsigned int) ms
;
2181 /*! \brief Send begin frames for DTMF */
2182 int ast_rtp_senddigit_begin(struct ast_rtp
*rtp
, char digit
)
2184 unsigned int *rtpheader
;
2185 int hdrlen
= 12, res
= 0, i
= 0, payload
= 0;
2188 if ((digit
<= '9') && (digit
>= '0'))
2190 else if (digit
== '*')
2192 else if (digit
== '#')
2194 else if ((digit
>= 'A') && (digit
<= 'D'))
2195 digit
= digit
- 'A' + 12;
2196 else if ((digit
>= 'a') && (digit
<= 'd'))
2197 digit
= digit
- 'a' + 12;
2199 ast_log(LOG_WARNING
, "Don't know how to represent '%c'\n", digit
);
2203 /* If we have no peer, return immediately */
2204 if (!rtp
->them
.sin_addr
.s_addr
|| !rtp
->them
.sin_port
)
2207 payload
= ast_rtp_lookup_code(rtp
, 0, AST_RTP_DTMF
);
2209 rtp
->dtmfmute
= ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
2210 rtp
->send_duration
= 160;
2212 /* Get a pointer to the header */
2213 rtpheader
= (unsigned int *)data
;
2214 rtpheader
[0] = htonl((2 << 30) | (1 << 23) | (payload
<< 16) | (rtp
->seqno
));
2215 rtpheader
[1] = htonl(rtp
->lastdigitts
);
2216 rtpheader
[2] = htonl(rtp
->ssrc
);
2218 for (i
= 0; i
< 2; i
++) {
2219 rtpheader
[3] = htonl((digit
<< 24) | (0xa << 16) | (rtp
->send_duration
));
2220 res
= sendto(rtp
->s
, (void *) rtpheader
, hdrlen
+ 4, 0, (struct sockaddr
*) &rtp
->them
, sizeof(rtp
->them
));
2222 ast_log(LOG_ERROR
, "RTP Transmission error to %s:%u: %s\n",
2223 ast_inet_ntoa(rtp
->them
.sin_addr
),
2224 ntohs(rtp
->them
.sin_port
), strerror(errno
));
2225 if (rtp_debug_test_addr(&rtp
->them
))
2226 ast_verbose("Sent RTP DTMF packet to %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
2227 ast_inet_ntoa(rtp
->them
.sin_addr
),
2228 ntohs(rtp
->them
.sin_port
), payload
, rtp
->seqno
, rtp
->lastdigitts
, res
- hdrlen
);
2229 /* Increment sequence number */
2231 /* Increment duration */
2232 rtp
->send_duration
+= 160;
2233 /* Clear marker bit and set seqno */
2234 rtpheader
[0] = htonl((2 << 30) | (payload
<< 16) | (rtp
->seqno
));
2237 /* Since we received a begin, we can safely store the digit and disable any compensation */
2238 rtp
->sending_digit
= 1;
2239 rtp
->send_digit
= digit
;
2240 rtp
->send_payload
= payload
;
2245 /*! \brief Send continuation frame for DTMF */
2246 static int ast_rtp_senddigit_continuation(struct ast_rtp
*rtp
)
2248 unsigned int *rtpheader
;
2249 int hdrlen
= 12, res
= 0;
2252 if (!rtp
->them
.sin_addr
.s_addr
|| !rtp
->them
.sin_port
)
2255 /* Setup packet to send */
2256 rtpheader
= (unsigned int *)data
;
2257 rtpheader
[0] = htonl((2 << 30) | (1 << 23) | (rtp
->send_payload
<< 16) | (rtp
->seqno
));
2258 rtpheader
[1] = htonl(rtp
->lastdigitts
);
2259 rtpheader
[2] = htonl(rtp
->ssrc
);
2260 rtpheader
[3] = htonl((rtp
->send_digit
<< 24) | (0xa << 16) | (rtp
->send_duration
));
2261 rtpheader
[0] = htonl((2 << 30) | (rtp
->send_payload
<< 16) | (rtp
->seqno
));
2264 res
= sendto(rtp
->s
, (void *) rtpheader
, hdrlen
+ 4, 0, (struct sockaddr
*) &rtp
->them
, sizeof(rtp
->them
));
2266 ast_log(LOG_ERROR
, "RTP Transmission error to %s:%d: %s\n",
2267 ast_inet_ntoa(rtp
->them
.sin_addr
),
2268 ntohs(rtp
->them
.sin_port
), strerror(errno
));
2269 if (rtp_debug_test_addr(&rtp
->them
))
2270 ast_verbose("Sent RTP DTMF packet to %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
2271 ast_inet_ntoa(rtp
->them
.sin_addr
),
2272 ntohs(rtp
->them
.sin_port
), rtp
->send_payload
, rtp
->seqno
, rtp
->lastdigitts
, res
- hdrlen
);
2274 /* Increment sequence number */
2276 /* Increment duration */
2277 rtp
->send_duration
+= 160;
2282 /*! \brief Send end packets for DTMF */
2283 int ast_rtp_senddigit_end(struct ast_rtp
*rtp
, char digit
)
2285 unsigned int *rtpheader
;
2286 int hdrlen
= 12, res
= 0, i
= 0;
2289 /* If no address, then bail out */
2290 if (!rtp
->them
.sin_addr
.s_addr
|| !rtp
->them
.sin_port
)
2293 if ((digit
<= '9') && (digit
>= '0'))
2295 else if (digit
== '*')
2297 else if (digit
== '#')
2299 else if ((digit
>= 'A') && (digit
<= 'D'))
2300 digit
= digit
- 'A' + 12;
2301 else if ((digit
>= 'a') && (digit
<= 'd'))
2302 digit
= digit
- 'a' + 12;
2304 ast_log(LOG_WARNING
, "Don't know how to represent '%c'\n", digit
);
2308 rtp
->dtmfmute
= ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
2310 rtpheader
= (unsigned int *)data
;
2311 rtpheader
[0] = htonl((2 << 30) | (1 << 23) | (rtp
->send_payload
<< 16) | (rtp
->seqno
));
2312 rtpheader
[1] = htonl(rtp
->lastdigitts
);
2313 rtpheader
[2] = htonl(rtp
->ssrc
);
2314 rtpheader
[3] = htonl((digit
<< 24) | (0xa << 16) | (rtp
->send_duration
));
2316 rtpheader
[3] |= htonl((1 << 23));
2317 rtpheader
[0] = htonl((2 << 30) | (rtp
->send_payload
<< 16) | (rtp
->seqno
));
2318 /* Send 3 termination packets */
2319 for (i
= 0; i
< 3; i
++) {
2320 res
= sendto(rtp
->s
, (void *) rtpheader
, hdrlen
+ 4, 0, (struct sockaddr
*) &rtp
->them
, sizeof(rtp
->them
));
2322 ast_log(LOG_ERROR
, "RTP Transmission error to %s:%d: %s\n",
2323 ast_inet_ntoa(rtp
->them
.sin_addr
),
2324 ntohs(rtp
->them
.sin_port
), strerror(errno
));
2325 if (rtp_debug_test_addr(&rtp
->them
))
2326 ast_verbose("Sent RTP DTMF packet to %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
2327 ast_inet_ntoa(rtp
->them
.sin_addr
),
2328 ntohs(rtp
->them
.sin_port
), rtp
->send_payload
, rtp
->seqno
, rtp
->lastdigitts
, res
- hdrlen
);
2330 rtp
->sending_digit
= 0;
2331 rtp
->send_digit
= 0;
2332 /* Increment lastdigitts */
2333 rtp
->lastdigitts
+= 960;
2339 /*! \brief Public function: Send an H.261 fast update request, some devices need this rather than SIP XML */
2340 int ast_rtcp_send_h261fur(void *data
)
2342 struct ast_rtp
*rtp
= data
;
2345 rtp
->rtcp
->sendfur
= 1;
2346 res
= ast_rtcp_write(data
);
2351 /*! \brief Send RTCP sender's report */
2352 static int ast_rtcp_write_sr(const void *data
)
2354 struct ast_rtp
*rtp
= (struct ast_rtp
*)data
;
2358 unsigned int now_lsw
;
2359 unsigned int now_msw
;
2360 unsigned int *rtcpheader
;
2362 unsigned int extended
;
2363 unsigned int expected
;
2364 unsigned int expected_interval
;
2365 unsigned int received_interval
;
2368 struct timeval dlsr
;
2371 /* Commented condition is always not NULL if rtp->rtcp is not NULL */
2372 if (!rtp
|| !rtp
->rtcp
/* || (&rtp->rtcp->them.sin_addr == 0)*/)
2375 if (!rtp
->rtcp
->them
.sin_addr
.s_addr
) { /* This'll stop rtcp for this rtp session */
2376 ast_verbose("RTCP SR transmission error, rtcp halted\n");
2377 AST_SCHED_DEL(rtp
->sched
, rtp
->rtcp
->schedid
);
2381 gettimeofday(&now
, NULL
);
2382 timeval2ntp(now
, &now_msw
, &now_lsw
); /* fill thses ones in from utils.c*/
2383 rtcpheader
= (unsigned int *)bdata
;
2384 rtcpheader
[1] = htonl(rtp
->ssrc
); /* Our SSRC */
2385 rtcpheader
[2] = htonl(now_msw
); /* now, MSW. gettimeofday() + SEC_BETWEEN_1900_AND_1970*/
2386 rtcpheader
[3] = htonl(now_lsw
); /* now, LSW */
2387 rtcpheader
[4] = htonl(rtp
->lastts
); /* FIXME shouldn't be that, it should be now */
2388 rtcpheader
[5] = htonl(rtp
->txcount
); /* No. packets sent */
2389 rtcpheader
[6] = htonl(rtp
->txoctetcount
); /* No. bytes sent */
2392 extended
= rtp
->cycles
+ rtp
->lastrxseqno
;
2393 expected
= extended
- rtp
->seedrxseqno
+ 1;
2394 if (rtp
->rxcount
> expected
)
2395 expected
+= rtp
->rxcount
- expected
;
2396 lost
= expected
- rtp
->rxcount
;
2397 expected_interval
= expected
- rtp
->rtcp
->expected_prior
;
2398 rtp
->rtcp
->expected_prior
= expected
;
2399 received_interval
= rtp
->rxcount
- rtp
->rtcp
->received_prior
;
2400 rtp
->rtcp
->received_prior
= rtp
->rxcount
;
2401 lost_interval
= expected_interval
- received_interval
;
2402 if (expected_interval
== 0 || lost_interval
<= 0)
2405 fraction
= (lost_interval
<< 8) / expected_interval
;
2406 timersub(&now
, &rtp
->rtcp
->rxlsr
, &dlsr
);
2407 rtcpheader
[7] = htonl(rtp
->themssrc
);
2408 rtcpheader
[8] = htonl(((fraction
& 0xff) << 24) | (lost
& 0xffffff));
2409 rtcpheader
[9] = htonl((rtp
->cycles
) | ((rtp
->lastrxseqno
& 0xffff)));
2410 rtcpheader
[10] = htonl((unsigned int)(rtp
->rxjitter
* 65536.));
2411 rtcpheader
[11] = htonl(rtp
->rtcp
->themrxlsr
);
2412 rtcpheader
[12] = htonl((((dlsr
.tv_sec
* 1000) + (dlsr
.tv_usec
/ 1000)) * 65536) / 1000);
2415 rtcpheader
[0] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SR
<< 16) | ((len
/4)-1));
2417 if (rtp
->rtcp
->sendfur
) {
2418 rtcpheader
[13] = htonl((2 << 30) | (0 << 24) | (RTCP_PT_FUR
<< 16) | 1);
2419 rtcpheader
[14] = htonl(rtp
->ssrc
); /* Our SSRC */
2421 rtp
->rtcp
->sendfur
= 0;
2424 /* Insert SDES here. Probably should make SDES text equal to mimetypes[code].type (not subtype 'cos */
2425 /* it can change mid call, and SDES can't) */
2426 rtcpheader
[len
/4] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SDES
<< 16) | 2);
2427 rtcpheader
[(len
/4)+1] = htonl(rtp
->ssrc
); /* Our SSRC */
2428 rtcpheader
[(len
/4)+2] = htonl(0x01 << 24); /* Empty for the moment */
2431 res
= sendto(rtp
->rtcp
->s
, (unsigned int *)rtcpheader
, len
, 0, (struct sockaddr
*)&rtp
->rtcp
->them
, sizeof(rtp
->rtcp
->them
));
2433 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
));
2434 AST_SCHED_DEL(rtp
->sched
, rtp
->rtcp
->schedid
);
2438 /* FIXME Don't need to get a new one */
2439 gettimeofday(&rtp
->rtcp
->txlsr
, NULL
);
2440 rtp
->rtcp
->sr_count
++;
2442 rtp
->rtcp
->lastsrtxcount
= rtp
->txcount
;
2444 if (rtcp_debug_test_addr(&rtp
->rtcp
->them
)) {
2445 ast_verbose("* Sent RTCP SR to %s:%d\n", ast_inet_ntoa(rtp
->rtcp
->them
.sin_addr
), ntohs(rtp
->rtcp
->them
.sin_port
));
2446 ast_verbose(" Our SSRC: %u\n", rtp
->ssrc
);
2447 ast_verbose(" Sent(NTP): %u.%010u\n", (unsigned int)now
.tv_sec
, (unsigned int)now
.tv_usec
*4096);
2448 ast_verbose(" Sent(RTP): %u\n", rtp
->lastts
);
2449 ast_verbose(" Sent packets: %u\n", rtp
->txcount
);
2450 ast_verbose(" Sent octets: %u\n", rtp
->txoctetcount
);
2451 ast_verbose(" Report block:\n");
2452 ast_verbose(" Fraction lost: %u\n", fraction
);
2453 ast_verbose(" Cumulative loss: %u\n", lost
);
2454 ast_verbose(" IA jitter: %.4f\n", rtp
->rxjitter
);
2455 ast_verbose(" Their last SR: %u\n", rtp
->rtcp
->themrxlsr
);
2456 ast_verbose(" DLSR: %4.4f (sec)\n\n", (double)(ntohl(rtcpheader
[12])/65536.0));
2461 /*! \brief Send RTCP recepient's report */
2462 static int ast_rtcp_write_rr(const void *data
)
2464 struct ast_rtp
*rtp
= (struct ast_rtp
*)data
;
2468 unsigned int extended
;
2469 unsigned int expected
;
2470 unsigned int expected_interval
;
2471 unsigned int received_interval
;
2474 unsigned int *rtcpheader
;
2476 struct timeval dlsr
;
2479 if (!rtp
|| !rtp
->rtcp
|| (&rtp
->rtcp
->them
.sin_addr
== 0))
2482 if (!rtp
->rtcp
->them
.sin_addr
.s_addr
) {
2483 ast_log(LOG_ERROR
, "RTCP RR transmission error, rtcp halted\n");
2484 AST_SCHED_DEL(rtp
->sched
, rtp
->rtcp
->schedid
);
2488 extended
= rtp
->cycles
+ rtp
->lastrxseqno
;
2489 expected
= extended
- rtp
->seedrxseqno
+ 1;
2490 lost
= expected
- rtp
->rxcount
;
2491 expected_interval
= expected
- rtp
->rtcp
->expected_prior
;
2492 rtp
->rtcp
->expected_prior
= expected
;
2493 received_interval
= rtp
->rxcount
- rtp
->rtcp
->received_prior
;
2494 rtp
->rtcp
->received_prior
= rtp
->rxcount
;
2495 lost_interval
= expected_interval
- received_interval
;
2496 if (expected_interval
== 0 || lost_interval
<= 0)
2499 fraction
= (lost_interval
<< 8) / expected_interval
;
2500 gettimeofday(&now
, NULL
);
2501 timersub(&now
, &rtp
->rtcp
->rxlsr
, &dlsr
);
2502 rtcpheader
= (unsigned int *)bdata
;
2503 rtcpheader
[0] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_RR
<< 16) | ((len
/4)-1));
2504 rtcpheader
[1] = htonl(rtp
->ssrc
);
2505 rtcpheader
[2] = htonl(rtp
->themssrc
);
2506 rtcpheader
[3] = htonl(((fraction
& 0xff) << 24) | (lost
& 0xffffff));
2507 rtcpheader
[4] = htonl((rtp
->cycles
) | ((rtp
->lastrxseqno
& 0xffff)));
2508 rtcpheader
[5] = htonl((unsigned int)(rtp
->rxjitter
* 65536.));
2509 rtcpheader
[6] = htonl(rtp
->rtcp
->themrxlsr
);
2510 rtcpheader
[7] = htonl((((dlsr
.tv_sec
* 1000) + (dlsr
.tv_usec
/ 1000)) * 65536) / 1000);
2512 if (rtp
->rtcp
->sendfur
) {
2513 rtcpheader
[8] = htonl((2 << 30) | (0 << 24) | (RTCP_PT_FUR
<< 16) | 1); /* Header from page 36 in RFC 3550 */
2514 rtcpheader
[9] = htonl(rtp
->ssrc
); /* Our SSRC */
2516 rtp
->rtcp
->sendfur
= 0;
2519 /*! \note Insert SDES here. Probably should make SDES text equal to mimetypes[code].type (not subtype 'cos
2520 it can change mid call, and SDES can't) */
2521 rtcpheader
[len
/4] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SDES
<< 16) | 2);
2522 rtcpheader
[(len
/4)+1] = htonl(rtp
->ssrc
); /* Our SSRC */
2523 rtcpheader
[(len
/4)+2] = htonl(0x01 << 24); /* Empty for the moment */
2526 res
= sendto(rtp
->rtcp
->s
, (unsigned int *)rtcpheader
, len
, 0, (struct sockaddr
*)&rtp
->rtcp
->them
, sizeof(rtp
->rtcp
->them
));
2529 ast_log(LOG_ERROR
, "RTCP RR transmission error, rtcp halted: %s\n",strerror(errno
));
2530 /* Remove the scheduler */
2531 AST_SCHED_DEL(rtp
->sched
, rtp
->rtcp
->schedid
);
2535 rtp
->rtcp
->rr_count
++;
2537 if (rtcp_debug_test_addr(&rtp
->rtcp
->them
)) {
2538 ast_verbose("\n* Sending RTCP RR to %s:%d\n"
2539 " Our SSRC: %u\nTheir SSRC: %u\niFraction lost: %d\nCumulative loss: %u\n"
2540 " IA jitter: %.4f\n"
2541 " Their last SR: %u\n"
2542 " DLSR: %4.4f (sec)\n\n",
2543 ast_inet_ntoa(rtp
->rtcp
->them
.sin_addr
),
2544 ntohs(rtp
->rtcp
->them
.sin_port
),
2545 rtp
->ssrc
, rtp
->themssrc
, fraction
, lost
,
2547 rtp
->rtcp
->themrxlsr
,
2548 (double)(ntohl(rtcpheader
[7])/65536.0));
2554 /*! \brief Write and RTCP packet to the far end
2555 * \note Decide if we are going to send an SR (with Reception Block) or RR
2556 * RR is sent if we have not sent any rtp packets in the previous interval */
2557 static int ast_rtcp_write(const void *data
)
2559 struct ast_rtp
*rtp
= (struct ast_rtp
*)data
;
2562 if (!rtp
|| !rtp
->rtcp
)
2565 if (rtp
->txcount
> rtp
->rtcp
->lastsrtxcount
)
2566 res
= ast_rtcp_write_sr(data
);
2568 res
= ast_rtcp_write_rr(data
);
2573 /*! \brief generate comfort noice (CNG) */
2574 int ast_rtp_sendcng(struct ast_rtp
*rtp
, int level
)
2576 unsigned int *rtpheader
;
2581 level
= 127 - (level
& 0x7f);
2582 payload
= ast_rtp_lookup_code(rtp
, 0, AST_RTP_CN
);
2584 /* If we have no peer, return immediately */
2585 if (!rtp
->them
.sin_addr
.s_addr
)
2588 rtp
->dtmfmute
= ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
2590 /* Get a pointer to the header */
2591 rtpheader
= (unsigned int *)data
;
2592 rtpheader
[0] = htonl((2 << 30) | (1 << 23) | (payload
<< 16) | (rtp
->seqno
++));
2593 rtpheader
[1] = htonl(rtp
->lastts
);
2594 rtpheader
[2] = htonl(rtp
->ssrc
);
2596 if (rtp
->them
.sin_port
&& rtp
->them
.sin_addr
.s_addr
) {
2597 res
= sendto(rtp
->s
, (void *)rtpheader
, hdrlen
+ 1, 0, (struct sockaddr
*)&rtp
->them
, sizeof(rtp
->them
));
2599 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
));
2600 if (rtp_debug_test_addr(&rtp
->them
))
2601 ast_verbose("Sent Comfort Noise RTP packet to %s:%u (type %d, seq %u, ts %u, len %d)\n"
2602 , ast_inet_ntoa(rtp
->them
.sin_addr
), ntohs(rtp
->them
.sin_port
), payload
, rtp
->seqno
, rtp
->lastts
,res
- hdrlen
);
2608 static int ast_rtp_raw_write(struct ast_rtp
*rtp
, struct ast_frame
*f
, int codec
)
2610 unsigned char *rtpheader
;
2617 ms
= calc_txstamp(rtp
, &f
->delivery
);
2618 /* Default prediction */
2619 if (f
->subclass
< AST_FORMAT_MAX_AUDIO
) {
2620 pred
= rtp
->lastts
+ f
->samples
;
2622 /* Re-calculate last TS */
2623 rtp
->lastts
= rtp
->lastts
+ ms
* 8;
2624 if (ast_tvzero(f
->delivery
)) {
2625 /* If this isn't an absolute delivery time, Check if it is close to our prediction,
2626 and if so, go with our prediction */
2627 if (abs(rtp
->lastts
- pred
) < MAX_TIMESTAMP_SKEW
)
2630 if (option_debug
> 2)
2631 ast_log(LOG_DEBUG
, "Difference is %d, ms is %d\n", abs(rtp
->lastts
- pred
), ms
);
2636 mark
= f
->subclass
& 0x1;
2637 pred
= rtp
->lastovidtimestamp
+ f
->samples
;
2638 /* Re-calculate last TS */
2639 rtp
->lastts
= rtp
->lastts
+ ms
* 90;
2640 /* If it's close to our prediction, go for it */
2641 if (ast_tvzero(f
->delivery
)) {
2642 if (abs(rtp
->lastts
- pred
) < 7200) {
2644 rtp
->lastovidtimestamp
+= f
->samples
;
2646 if (option_debug
> 2)
2647 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
);
2648 rtp
->lastovidtimestamp
= rtp
->lastts
;
2653 /* If we have been explicitly told to set the marker bit do so */
2654 if (rtp
->set_marker_bit
) {
2656 rtp
->set_marker_bit
= 0;
2659 /* If the timestamp for non-digit packets has moved beyond the timestamp
2660 for digits, update the digit timestamp.
2662 if (rtp
->lastts
> rtp
->lastdigitts
)
2663 rtp
->lastdigitts
= rtp
->lastts
;
2665 if (ast_test_flag(f
, AST_FRFLAG_HAS_TIMING_INFO
))
2666 rtp
->lastts
= f
->ts
* 8;
2668 /* Get a pointer to the header */
2669 rtpheader
= (unsigned char *)(f
->data
- hdrlen
);
2671 put_unaligned_uint32(rtpheader
, htonl((2 << 30) | (codec
<< 16) | (rtp
->seqno
) | (mark
<< 23)));
2672 put_unaligned_uint32(rtpheader
+ 4, htonl(rtp
->lastts
));
2673 put_unaligned_uint32(rtpheader
+ 8, htonl(rtp
->ssrc
));
2675 if (rtp
->them
.sin_port
&& rtp
->them
.sin_addr
.s_addr
) {
2676 res
= sendto(rtp
->s
, (void *)rtpheader
, f
->datalen
+ hdrlen
, 0, (struct sockaddr
*)&rtp
->them
, sizeof(rtp
->them
));
2678 if (!rtp
->nat
|| (rtp
->nat
&& (ast_test_flag(rtp
, FLAG_NAT_ACTIVE
) == FLAG_NAT_ACTIVE
))) {
2679 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
));
2680 } else if (((ast_test_flag(rtp
, FLAG_NAT_ACTIVE
) == FLAG_NAT_INACTIVE
) || rtpdebug
) && !ast_test_flag(rtp
, FLAG_NAT_INACTIVE_NOWARN
)) {
2681 /* Only give this error message once if we are not RTP debugging */
2682 if (option_debug
|| rtpdebug
)
2683 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
));
2684 ast_set_flag(rtp
, FLAG_NAT_INACTIVE_NOWARN
);
2688 rtp
->txoctetcount
+=(res
- hdrlen
);
2690 if (rtp
->rtcp
&& rtp
->rtcp
->schedid
< 1)
2691 rtp
->rtcp
->schedid
= ast_sched_add(rtp
->sched
, ast_rtcp_calc_interval(rtp
), ast_rtcp_write
, rtp
);
2694 if (rtp_debug_test_addr(&rtp
->them
))
2695 ast_verbose("Sent RTP packet to %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
2696 ast_inet_ntoa(rtp
->them
.sin_addr
), ntohs(rtp
->them
.sin_port
), codec
, rtp
->seqno
, rtp
->lastts
,res
- hdrlen
);
2704 int ast_rtp_codec_setpref(struct ast_rtp
*rtp
, struct ast_codec_pref
*prefs
)
2707 for (x
= 0; x
< 32; x
++) { /* Ugly way */
2708 rtp
->pref
.order
[x
] = prefs
->order
[x
];
2709 rtp
->pref
.framing
[x
] = prefs
->framing
[x
];
2712 ast_smoother_free(rtp
->smoother
);
2713 rtp
->smoother
= NULL
;
2717 struct ast_codec_pref
*ast_rtp_codec_getpref(struct ast_rtp
*rtp
)
2722 int ast_rtp_codec_getformat(int pt
)
2724 if (pt
< 0 || pt
> MAX_RTP_PT
)
2725 return 0; /* bogus payload type */
2727 if (static_RTP_PT
[pt
].isAstFormat
)
2728 return static_RTP_PT
[pt
].code
;
2733 int ast_rtp_write(struct ast_rtp
*rtp
, struct ast_frame
*_f
)
2735 struct ast_frame
*f
;
2741 /* If we have no peer, return immediately */
2742 if (!rtp
->them
.sin_addr
.s_addr
)
2745 /* If there is no data length, return immediately */
2749 /* Make sure we have enough space for RTP header */
2750 if ((_f
->frametype
!= AST_FRAME_VOICE
) && (_f
->frametype
!= AST_FRAME_VIDEO
)) {
2751 ast_log(LOG_WARNING
, "RTP can only send voice and video\n");
2755 subclass
= _f
->subclass
;
2756 if (_f
->frametype
== AST_FRAME_VIDEO
)
2759 codec
= ast_rtp_lookup_code(rtp
, 1, subclass
);
2761 ast_log(LOG_WARNING
, "Don't know how to send format %s packets with RTP\n", ast_getformatname(_f
->subclass
));
2765 if (rtp
->lasttxformat
!= subclass
) {
2766 /* New format, reset the smoother */
2768 ast_log(LOG_DEBUG
, "Ooh, format changed from %s to %s\n", ast_getformatname(rtp
->lasttxformat
), ast_getformatname(subclass
));
2769 rtp
->lasttxformat
= subclass
;
2771 ast_smoother_free(rtp
->smoother
);
2772 rtp
->smoother
= NULL
;
2775 if (!rtp
->smoother
&& subclass
!= AST_FORMAT_SPEEX
&& subclass
!= AST_FORMAT_G723_1
) {
2776 struct ast_format_list fmt
= ast_codec_pref_getsize(&rtp
->pref
, subclass
);
2777 if (fmt
.inc_ms
) { /* if codec parameters is set / avoid division by zero */
2778 if (!(rtp
->smoother
= ast_smoother_new((fmt
.cur_ms
* fmt
.fr_len
) / fmt
.inc_ms
))) {
2779 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
));
2783 ast_smoother_set_flags(rtp
->smoother
, fmt
.flags
);
2785 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
));
2788 if (rtp
->smoother
) {
2789 if (ast_smoother_test_flag(rtp
->smoother
, AST_SMOOTHER_FLAG_BE
)) {
2790 ast_smoother_feed_be(rtp
->smoother
, _f
);
2792 ast_smoother_feed(rtp
->smoother
, _f
);
2795 while((f
= ast_smoother_read(rtp
->smoother
)) && (f
->data
))
2796 ast_rtp_raw_write(rtp
, f
, codec
);
2798 /* Don't buffer outgoing frames; send them one-per-packet: */
2799 if (_f
->offset
< hdrlen
) {
2805 ast_rtp_raw_write(rtp
, f
, codec
);
2813 /*! \brief Unregister interface to channel driver */
2814 void ast_rtp_proto_unregister(struct ast_rtp_protocol
*proto
)
2816 AST_LIST_LOCK(&protos
);
2817 AST_LIST_REMOVE(&protos
, proto
, list
);
2818 AST_LIST_UNLOCK(&protos
);
2821 /*! \brief Register interface to channel driver */
2822 int ast_rtp_proto_register(struct ast_rtp_protocol
*proto
)
2824 struct ast_rtp_protocol
*cur
;
2826 AST_LIST_LOCK(&protos
);
2827 AST_LIST_TRAVERSE(&protos
, cur
, list
) {
2828 if (!strcmp(cur
->type
, proto
->type
)) {
2829 ast_log(LOG_WARNING
, "Tried to register same protocol '%s' twice\n", cur
->type
);
2830 AST_LIST_UNLOCK(&protos
);
2834 AST_LIST_INSERT_HEAD(&protos
, proto
, list
);
2835 AST_LIST_UNLOCK(&protos
);
2840 /*! \brief Bridge loop for true native bridge (reinvite) */
2841 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
)
2843 struct ast_frame
*fr
= NULL
;
2844 struct ast_channel
*who
= NULL
, *other
= NULL
, *cs
[3] = {NULL
, };
2845 int oldcodec0
= codec0
, oldcodec1
= codec1
;
2846 struct sockaddr_in ac1
= {0,}, vac1
= {0,}, ac0
= {0,}, vac0
= {0,};
2847 struct sockaddr_in t1
= {0,}, vt1
= {0,}, t0
= {0,}, vt0
= {0,};
2849 /* Set it up so audio goes directly between the two endpoints */
2851 /* Test the first channel */
2852 if (!(pr0
->set_rtp_peer(c0
, p1
, vp1
, codec1
, ast_test_flag(p1
, FLAG_NAT_ACTIVE
)))) {
2853 ast_rtp_get_peer(p1
, &ac1
);
2855 ast_rtp_get_peer(vp1
, &vac1
);
2857 ast_log(LOG_WARNING
, "Channel '%s' failed to talk to '%s'\n", c0
->name
, c1
->name
);
2859 /* Test the second channel */
2860 if (!(pr1
->set_rtp_peer(c1
, p0
, vp0
, codec0
, ast_test_flag(p0
, FLAG_NAT_ACTIVE
)))) {
2861 ast_rtp_get_peer(p0
, &ac0
);
2863 ast_rtp_get_peer(vp0
, &vac0
);
2865 ast_log(LOG_WARNING
, "Channel '%s' failed to talk to '%s'\n", c1
->name
, c0
->name
);
2867 /* Now we can unlock and move into our loop */
2868 ast_channel_unlock(c0
);
2869 ast_channel_unlock(c1
);
2871 /* Throw our channels into the structure and enter the loop */
2876 /* Check if anything changed */
2877 if ((c0
->tech_pvt
!= pvt0
) ||
2878 (c1
->tech_pvt
!= pvt1
) ||
2879 (c0
->masq
|| c0
->masqr
|| c1
->masq
|| c1
->masqr
) ||
2880 (c0
->monitor
|| c0
->audiohooks
|| c1
->monitor
|| c1
->audiohooks
)) {
2881 ast_log(LOG_DEBUG
, "Oooh, something is weird, backing out\n");
2882 if (c0
->tech_pvt
== pvt0
)
2883 if (pr0
->set_rtp_peer(c0
, NULL
, NULL
, 0, 0))
2884 ast_log(LOG_WARNING
, "Channel '%s' failed to break RTP bridge\n", c0
->name
);
2885 if (c1
->tech_pvt
== pvt1
)
2886 if (pr1
->set_rtp_peer(c1
, NULL
, NULL
, 0, 0))
2887 ast_log(LOG_WARNING
, "Channel '%s' failed to break RTP bridge\n", c1
->name
);
2888 return AST_BRIDGE_RETRY
;
2891 /* Check if they have changed their address */
2892 ast_rtp_get_peer(p1
, &t1
);
2894 ast_rtp_get_peer(vp1
, &vt1
);
2896 codec1
= pr1
->get_codec(c1
);
2897 ast_rtp_get_peer(p0
, &t0
);
2899 ast_rtp_get_peer(vp0
, &vt0
);
2901 codec0
= pr0
->get_codec(c0
);
2902 if ((inaddrcmp(&t1
, &ac1
)) ||
2903 (vp1
&& inaddrcmp(&vt1
, &vac1
)) ||
2904 (codec1
!= oldcodec1
)) {
2905 if (option_debug
> 1) {
2906 ast_log(LOG_DEBUG
, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
2907 c1
->name
, ast_inet_ntoa(t1
.sin_addr
), ntohs(t1
.sin_port
), codec1
);
2908 ast_log(LOG_DEBUG
, "Oooh, '%s' changed end vaddress to %s:%d (format %d)\n",
2909 c1
->name
, ast_inet_ntoa(vt1
.sin_addr
), ntohs(vt1
.sin_port
), codec1
);
2910 ast_log(LOG_DEBUG
, "Oooh, '%s' was %s:%d/(format %d)\n",
2911 c1
->name
, ast_inet_ntoa(ac1
.sin_addr
), ntohs(ac1
.sin_port
), oldcodec1
);
2912 ast_log(LOG_DEBUG
, "Oooh, '%s' was %s:%d/(format %d)\n",
2913 c1
->name
, ast_inet_ntoa(vac1
.sin_addr
), ntohs(vac1
.sin_port
), oldcodec1
);
2915 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
)))
2916 ast_log(LOG_WARNING
, "Channel '%s' failed to update to '%s'\n", c0
->name
, c1
->name
);
2917 memcpy(&ac1
, &t1
, sizeof(ac1
));
2918 memcpy(&vac1
, &vt1
, sizeof(vac1
));
2921 if ((inaddrcmp(&t0
, &ac0
)) ||
2922 (vp0
&& inaddrcmp(&vt0
, &vac0
))) {
2923 if (option_debug
> 1) {
2924 ast_log(LOG_DEBUG
, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
2925 c0
->name
, ast_inet_ntoa(t0
.sin_addr
), ntohs(t0
.sin_port
), codec0
);
2926 ast_log(LOG_DEBUG
, "Oooh, '%s' was %s:%d/(format %d)\n",
2927 c0
->name
, ast_inet_ntoa(ac0
.sin_addr
), ntohs(ac0
.sin_port
), oldcodec0
);
2929 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
)))
2930 ast_log(LOG_WARNING
, "Channel '%s' failed to update to '%s'\n", c1
->name
, c0
->name
);
2931 memcpy(&ac0
, &t0
, sizeof(ac0
));
2932 memcpy(&vac0
, &vt0
, sizeof(vac0
));
2936 /* Wait for frame to come in on the channels */
2937 if (!(who
= ast_waitfor_n(cs
, 2, &timeoutms
))) {
2939 if (pr0
->set_rtp_peer(c0
, NULL
, NULL
, 0, 0))
2940 ast_log(LOG_WARNING
, "Channel '%s' failed to break RTP bridge\n", c0
->name
);
2941 if (pr1
->set_rtp_peer(c1
, NULL
, NULL
, 0, 0))
2942 ast_log(LOG_WARNING
, "Channel '%s' failed to break RTP bridge\n", c1
->name
);
2943 return AST_BRIDGE_RETRY
;
2946 ast_log(LOG_DEBUG
, "Ooh, empty read...\n");
2947 if (ast_check_hangup(c0
) || ast_check_hangup(c1
))
2952 other
= (who
== c0
) ? c1
: c0
;
2953 if (!fr
|| ((fr
->frametype
== AST_FRAME_DTMF_BEGIN
|| fr
->frametype
== AST_FRAME_DTMF_END
) &&
2954 (((who
== c0
) && (flags
& AST_BRIDGE_DTMF_CHANNEL_0
)) ||
2955 ((who
== c1
) && (flags
& AST_BRIDGE_DTMF_CHANNEL_1
))))) {
2956 /* Break out of bridge */
2960 ast_log(LOG_DEBUG
, "Oooh, got a %s\n", fr
? "digit" : "hangup");
2961 if (c0
->tech_pvt
== pvt0
)
2962 if (pr0
->set_rtp_peer(c0
, NULL
, NULL
, 0, 0))
2963 ast_log(LOG_WARNING
, "Channel '%s' failed to break RTP bridge\n", c0
->name
);
2964 if (c1
->tech_pvt
== pvt1
)
2965 if (pr1
->set_rtp_peer(c1
, NULL
, NULL
, 0, 0))
2966 ast_log(LOG_WARNING
, "Channel '%s' failed to break RTP bridge\n", c1
->name
);
2967 return AST_BRIDGE_COMPLETE
;
2968 } else if ((fr
->frametype
== AST_FRAME_CONTROL
) && !(flags
& AST_BRIDGE_IGNORE_SIGS
)) {
2969 if ((fr
->subclass
== AST_CONTROL_HOLD
) ||
2970 (fr
->subclass
== AST_CONTROL_UNHOLD
) ||
2971 (fr
->subclass
== AST_CONTROL_VIDUPDATE
)) {
2972 if (fr
->subclass
== AST_CONTROL_HOLD
) {
2973 /* If we someone went on hold we want the other side to reinvite back to us */
2975 pr1
->set_rtp_peer(c1
, NULL
, NULL
, 0, 0);
2977 pr0
->set_rtp_peer(c0
, NULL
, NULL
, 0, 0);
2978 } else if (fr
->subclass
== AST_CONTROL_UNHOLD
) {
2979 /* If they went off hold they should go back to being direct */
2981 pr1
->set_rtp_peer(c1
, p0
, vp0
, codec0
, ast_test_flag(p0
, FLAG_NAT_ACTIVE
));
2983 pr0
->set_rtp_peer(c0
, p1
, vp1
, codec1
, ast_test_flag(p1
, FLAG_NAT_ACTIVE
));
2985 /* Update local address information */
2986 ast_rtp_get_peer(p0
, &t0
);
2987 memcpy(&ac0
, &t0
, sizeof(ac0
));
2988 ast_rtp_get_peer(p1
, &t1
);
2989 memcpy(&ac1
, &t1
, sizeof(ac1
));
2990 /* Update codec information */
2991 if (pr0
->get_codec
&& c0
->tech_pvt
)
2992 oldcodec0
= codec0
= pr0
->get_codec(c0
);
2993 if (pr1
->get_codec
&& c1
->tech_pvt
)
2994 oldcodec1
= codec1
= pr1
->get_codec(c1
);
2995 ast_indicate_data(other
, fr
->subclass
, fr
->data
, fr
->datalen
);
3000 ast_log(LOG_DEBUG
, "Got a FRAME_CONTROL (%d) frame on channel %s\n", fr
->subclass
, who
->name
);
3001 return AST_BRIDGE_COMPLETE
;
3004 if ((fr
->frametype
== AST_FRAME_DTMF_BEGIN
) ||
3005 (fr
->frametype
== AST_FRAME_DTMF_END
) ||
3006 (fr
->frametype
== AST_FRAME_VOICE
) ||
3007 (fr
->frametype
== AST_FRAME_VIDEO
) ||
3008 (fr
->frametype
== AST_FRAME_IMAGE
) ||
3009 (fr
->frametype
== AST_FRAME_HTML
) ||
3010 (fr
->frametype
== AST_FRAME_MODEM
) ||
3011 (fr
->frametype
== AST_FRAME_TEXT
)) {
3012 ast_write(other
, fr
);
3022 if (pr0
->set_rtp_peer(c0
, NULL
, NULL
, 0, 0))
3023 ast_log(LOG_WARNING
, "Channel '%s' failed to break RTP bridge\n", c0
->name
);
3024 if (pr1
->set_rtp_peer(c1
, NULL
, NULL
, 0, 0))
3025 ast_log(LOG_WARNING
, "Channel '%s' failed to break RTP bridge\n", c1
->name
);
3027 return AST_BRIDGE_FAILED
;
3030 /*! \brief P2P RTP Callback */
3032 static int p2p_rtp_callback(int *id
, int fd
, short events
, void *cbdata
)
3034 int res
= 0, hdrlen
= 12;
3035 struct sockaddr_in sin
;
3037 unsigned int *header
;
3038 struct ast_rtp
*rtp
= cbdata
, *bridged
= NULL
;
3044 if ((res
= recvfrom(fd
, rtp
->rawdata
+ AST_FRIENDLY_OFFSET
, sizeof(rtp
->rawdata
) - AST_FRIENDLY_OFFSET
, 0, (struct sockaddr
*)&sin
, &len
)) < 0)
3047 header
= (unsigned int *)(rtp
->rawdata
+ AST_FRIENDLY_OFFSET
);
3049 /* If NAT support is turned on, then see if we need to change their address */
3051 ((rtp
->them
.sin_addr
.s_addr
!= sin
.sin_addr
.s_addr
) ||
3052 (rtp
->them
.sin_port
!= sin
.sin_port
))) {
3055 ast_set_flag(rtp
, FLAG_NAT_ACTIVE
);
3056 if (option_debug
|| rtpdebug
)
3057 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
));
3060 /* Write directly out to other RTP stream if bridged */
3061 if ((bridged
= ast_rtp_get_bridged(rtp
)))
3062 bridge_p2p_rtp_write(rtp
, bridged
, header
, res
, hdrlen
);
3067 /*! \brief Helper function to switch a channel and RTP stream into callback mode */
3068 static int p2p_callback_enable(struct ast_channel
*chan
, struct ast_rtp
*rtp
, int *fds
, int **iod
)
3070 /* If we need DTMF, are looking for STUN, or we have no IO structure then we can't do direct callback */
3071 if (ast_test_flag(rtp
, FLAG_P2P_NEED_DTMF
) || ast_test_flag(rtp
, FLAG_HAS_STUN
) || !rtp
->io
)
3074 /* If the RTP structure is already in callback mode, remove it temporarily */
3076 ast_io_remove(rtp
->io
, rtp
->ioid
);
3080 /* Steal the file descriptors from the channel and stash them away */
3081 fds
[0] = chan
->fds
[0];
3084 /* Now, fire up callback mode */
3085 iod
[0] = ast_io_add(rtp
->io
, fds
[0], p2p_rtp_callback
, AST_IO_IN
, rtp
);
3090 static int p2p_callback_enable(struct ast_channel
*chan
, struct ast_rtp
*rtp
, int *fds
, int **iod
)
3096 /*! \brief Helper function to switch a channel and RTP stream out of callback mode */
3097 static int p2p_callback_disable(struct ast_channel
*chan
, struct ast_rtp
*rtp
, int *fds
, int **iod
)
3099 ast_channel_lock(chan
);
3101 /* Remove the callback from the IO context */
3102 ast_io_remove(rtp
->io
, iod
[0]);
3104 /* Restore file descriptors */
3105 chan
->fds
[0] = fds
[0];
3106 ast_channel_unlock(chan
);
3108 /* Restore callback mode if previously used */
3109 if (ast_test_flag(rtp
, FLAG_CALLBACK_MODE
))
3110 rtp
->ioid
= ast_io_add(rtp
->io
, rtp
->s
, rtpread
, AST_IO_IN
, rtp
);
3115 /*! \brief Helper function that sets what an RTP structure is bridged to */
3116 static void p2p_set_bridge(struct ast_rtp
*rtp0
, struct ast_rtp
*rtp1
)
3118 ast_mutex_lock(&rtp0
->bridge_lock
);
3119 rtp0
->bridged
= rtp1
;
3120 ast_mutex_unlock(&rtp0
->bridge_lock
);
3125 /*! \brief Bridge loop for partial native bridge (packet2packet) */
3126 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
)
3128 struct ast_frame
*fr
= NULL
;
3129 struct ast_channel
*who
= NULL
, *other
= NULL
, *cs
[3] = {NULL
, };
3130 int p0_fds
[2] = {-1, -1}, p1_fds
[2] = {-1, -1};
3131 int *p0_iod
[2] = {NULL
, NULL
}, *p1_iod
[2] = {NULL
, NULL
};
3132 int p0_callback
= 0, p1_callback
= 0;
3133 enum ast_bridge_result res
= AST_BRIDGE_FAILED
;
3135 /* Okay, setup each RTP structure to do P2P forwarding */
3136 ast_clear_flag(p0
, FLAG_P2P_SENT_MARK
);
3137 p2p_set_bridge(p0
, p1
);
3138 ast_clear_flag(p1
, FLAG_P2P_SENT_MARK
);
3139 p2p_set_bridge(p1
, p0
);
3141 /* Activate callback modes if possible */
3142 p0_callback
= p2p_callback_enable(c0
, p0
, &p0_fds
[0], &p0_iod
[0]);
3143 p1_callback
= p2p_callback_enable(c1
, p1
, &p1_fds
[0], &p1_iod
[0]);
3145 /* Now let go of the channel locks and be on our way */
3146 ast_channel_unlock(c0
);
3147 ast_channel_unlock(c1
);
3149 /* Go into a loop forwarding frames until we don't need to anymore */
3154 /* If the underlying formats have changed force this bridge to break */
3155 if ((c0
->rawreadformat
!= c1
->rawwriteformat
) || (c1
->rawreadformat
!= c0
->rawwriteformat
)) {
3156 ast_log(LOG_DEBUG
, "Oooh, formats changed, backing out\n");
3157 res
= AST_BRIDGE_FAILED_NOWARN
;
3160 /* Check if anything changed */
3161 if ((c0
->tech_pvt
!= pvt0
) ||
3162 (c1
->tech_pvt
!= pvt1
) ||
3163 (c0
->masq
|| c0
->masqr
|| c1
->masq
|| c1
->masqr
) ||
3164 (c0
->monitor
|| c0
->audiohooks
|| c1
->monitor
|| c1
->audiohooks
)) {
3165 ast_log(LOG_DEBUG
, "Oooh, something is weird, backing out\n");
3166 if ((c0
->masq
|| c0
->masqr
) && (fr
= ast_read(c0
)))
3168 if ((c1
->masq
|| c1
->masqr
) && (fr
= ast_read(c1
)))
3170 res
= AST_BRIDGE_RETRY
;
3173 /* Wait on a channel to feed us a frame */
3174 if (!(who
= ast_waitfor_n(cs
, 2, &timeoutms
))) {
3176 res
= AST_BRIDGE_RETRY
;
3180 ast_log(LOG_NOTICE
, "Ooh, empty read...\n");
3181 if (ast_check_hangup(c0
) || ast_check_hangup(c1
))
3185 /* Read in frame from channel */
3187 other
= (who
== c0
) ? c1
: c0
;
3188 /* Dependong on the frame we may need to break out of our bridge */
3189 if (!fr
|| ((fr
->frametype
== AST_FRAME_DTMF_BEGIN
|| fr
->frametype
== AST_FRAME_DTMF_END
) &&
3190 ((who
== c0
) && (flags
& AST_BRIDGE_DTMF_CHANNEL_0
)) |
3191 ((who
== c1
) && (flags
& AST_BRIDGE_DTMF_CHANNEL_1
)))) {
3192 /* Record received frame and who */
3196 ast_log(LOG_DEBUG
, "Oooh, got a %s\n", fr
? "digit" : "hangup");
3197 res
= AST_BRIDGE_COMPLETE
;
3199 } else if ((fr
->frametype
== AST_FRAME_CONTROL
) && !(flags
& AST_BRIDGE_IGNORE_SIGS
)) {
3200 if ((fr
->subclass
== AST_CONTROL_HOLD
) ||
3201 (fr
->subclass
== AST_CONTROL_UNHOLD
) ||
3202 (fr
->subclass
== AST_CONTROL_VIDUPDATE
)) {
3203 /* If we are going on hold, then break callback mode and P2P bridging */
3204 if (fr
->subclass
== AST_CONTROL_HOLD
) {
3206 p0_callback
= p2p_callback_disable(c0
, p0
, &p0_fds
[0], &p0_iod
[0]);
3208 p1_callback
= p2p_callback_disable(c1
, p1
, &p1_fds
[0], &p1_iod
[0]);
3209 p2p_set_bridge(p0
, NULL
);
3210 p2p_set_bridge(p1
, NULL
);
3211 } else if (fr
->subclass
== AST_CONTROL_UNHOLD
) {
3212 /* If we are off hold, then go back to callback mode and P2P bridging */
3213 ast_clear_flag(p0
, FLAG_P2P_SENT_MARK
);
3214 p2p_set_bridge(p0
, p1
);
3215 ast_clear_flag(p1
, FLAG_P2P_SENT_MARK
);
3216 p2p_set_bridge(p1
, p0
);
3217 p0_callback
= p2p_callback_enable(c0
, p0
, &p0_fds
[0], &p0_iod
[0]);
3218 p1_callback
= p2p_callback_enable(c1
, p1
, &p1_fds
[0], &p1_iod
[0]);
3220 ast_indicate_data(other
, fr
->subclass
, fr
->data
, fr
->datalen
);
3225 ast_log(LOG_DEBUG
, "Got a FRAME_CONTROL (%d) frame on channel %s\n", fr
->subclass
, who
->name
);
3226 res
= AST_BRIDGE_COMPLETE
;
3230 if ((fr
->frametype
== AST_FRAME_DTMF_BEGIN
) ||
3231 (fr
->frametype
== AST_FRAME_DTMF_END
) ||
3232 (fr
->frametype
== AST_FRAME_VOICE
) ||
3233 (fr
->frametype
== AST_FRAME_VIDEO
) ||
3234 (fr
->frametype
== AST_FRAME_IMAGE
) ||
3235 (fr
->frametype
== AST_FRAME_HTML
) ||
3236 (fr
->frametype
== AST_FRAME_MODEM
) ||
3237 (fr
->frametype
== AST_FRAME_TEXT
)) {
3238 ast_write(other
, fr
);
3249 /* If we are totally avoiding the core, then restore our link to it */
3251 p0_callback
= p2p_callback_disable(c0
, p0
, &p0_fds
[0], &p0_iod
[0]);
3253 p1_callback
= p2p_callback_disable(c1
, p1
, &p1_fds
[0], &p1_iod
[0]);
3255 /* Break out of the direct bridge */
3256 p2p_set_bridge(p0
, NULL
);
3257 p2p_set_bridge(p1
, NULL
);
3262 /*! \brief Bridge calls. If possible and allowed, initiate
3263 re-invite so the peers exchange media directly outside
3265 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
)
3267 struct ast_rtp
*p0
= NULL
, *p1
= NULL
; /* Audio RTP Channels */
3268 struct ast_rtp
*vp0
= NULL
, *vp1
= NULL
; /* Video RTP channels */
3269 struct ast_rtp_protocol
*pr0
= NULL
, *pr1
= NULL
;
3270 enum ast_rtp_get_result audio_p0_res
= AST_RTP_GET_FAILED
, video_p0_res
= AST_RTP_GET_FAILED
;
3271 enum ast_rtp_get_result audio_p1_res
= AST_RTP_GET_FAILED
, video_p1_res
= AST_RTP_GET_FAILED
;
3272 enum ast_bridge_result res
= AST_BRIDGE_FAILED
;
3273 int codec0
= 0, codec1
= 0;
3274 void *pvt0
= NULL
, *pvt1
= NULL
;
3277 ast_channel_lock(c0
);
3278 while(ast_channel_trylock(c1
)) {
3279 ast_channel_unlock(c0
);
3281 ast_channel_lock(c0
);
3284 /* Ensure neither channel got hungup during lock avoidance */
3285 if (ast_check_hangup(c0
) || ast_check_hangup(c1
)) {
3286 ast_log(LOG_WARNING
, "Got hangup while attempting to bridge '%s' and '%s'\n", c0
->name
, c1
->name
);
3287 ast_channel_unlock(c0
);
3288 ast_channel_unlock(c1
);
3289 return AST_BRIDGE_FAILED
;
3292 /* Find channel driver interfaces */
3293 if (!(pr0
= get_proto(c0
))) {
3294 ast_log(LOG_WARNING
, "Can't find native functions for channel '%s'\n", c0
->name
);
3295 ast_channel_unlock(c0
);
3296 ast_channel_unlock(c1
);
3297 return AST_BRIDGE_FAILED
;
3299 if (!(pr1
= get_proto(c1
))) {
3300 ast_log(LOG_WARNING
, "Can't find native functions for channel '%s'\n", c1
->name
);
3301 ast_channel_unlock(c0
);
3302 ast_channel_unlock(c1
);
3303 return AST_BRIDGE_FAILED
;
3306 /* Get channel specific interface structures */
3307 pvt0
= c0
->tech_pvt
;
3308 pvt1
= c1
->tech_pvt
;
3310 /* Get audio and video interface (if native bridge is possible) */
3311 audio_p0_res
= pr0
->get_rtp_info(c0
, &p0
);
3312 video_p0_res
= pr0
->get_vrtp_info
? pr0
->get_vrtp_info(c0
, &vp0
) : AST_RTP_GET_FAILED
;
3313 audio_p1_res
= pr1
->get_rtp_info(c1
, &p1
);
3314 video_p1_res
= pr1
->get_vrtp_info
? pr1
->get_vrtp_info(c1
, &vp1
) : AST_RTP_GET_FAILED
;
3316 /* If we are carrying video, and both sides are not reinviting... then fail the native bridge */
3317 if (video_p0_res
!= AST_RTP_GET_FAILED
&& (audio_p0_res
!= AST_RTP_TRY_NATIVE
|| video_p0_res
!= AST_RTP_TRY_NATIVE
))
3318 audio_p0_res
= AST_RTP_GET_FAILED
;
3319 if (video_p1_res
!= AST_RTP_GET_FAILED
&& (audio_p1_res
!= AST_RTP_TRY_NATIVE
|| video_p1_res
!= AST_RTP_TRY_NATIVE
))
3320 audio_p1_res
= AST_RTP_GET_FAILED
;
3322 /* Check if a bridge is possible (partial/native) */
3323 if (audio_p0_res
== AST_RTP_GET_FAILED
|| audio_p1_res
== AST_RTP_GET_FAILED
) {
3324 /* Somebody doesn't want to play... */
3325 ast_channel_unlock(c0
);
3326 ast_channel_unlock(c1
);
3327 return AST_BRIDGE_FAILED_NOWARN
;
3330 /* If we need to feed DTMF frames into the core then only do a partial native bridge */
3331 if (ast_test_flag(p0
, FLAG_HAS_DTMF
) && (flags
& AST_BRIDGE_DTMF_CHANNEL_0
)) {
3332 ast_set_flag(p0
, FLAG_P2P_NEED_DTMF
);
3333 audio_p0_res
= AST_RTP_TRY_PARTIAL
;
3336 if (ast_test_flag(p1
, FLAG_HAS_DTMF
) && (flags
& AST_BRIDGE_DTMF_CHANNEL_1
)) {
3337 ast_set_flag(p1
, FLAG_P2P_NEED_DTMF
);
3338 audio_p1_res
= AST_RTP_TRY_PARTIAL
;
3341 /* If both sides are not using the same method of DTMF transmission
3342 * (ie: one is RFC2833, other is INFO... then we can not do direct media.
3343 * --------------------------------------------------
3344 * | DTMF Mode | HAS_DTMF | Accepts Begin Frames |
3345 * |-----------|------------|-----------------------|
3346 * | Inband | False | True |
3347 * | RFC2833 | True | True |
3348 * | SIP INFO | False | False |
3349 * --------------------------------------------------
3350 * However, if DTMF from both channels is being monitored by the core, then
3351 * we can still do packet-to-packet bridging, because passing through the
3352 * core will handle DTMF mode translation.
3354 if ( (ast_test_flag(p0
, FLAG_HAS_DTMF
) != ast_test_flag(p1
, FLAG_HAS_DTMF
)) ||
3355 (!c0
->tech
->send_digit_begin
!= !c1
->tech
->send_digit_begin
)) {
3356 if (!ast_test_flag(p0
, FLAG_P2P_NEED_DTMF
) || !ast_test_flag(p1
, FLAG_P2P_NEED_DTMF
)) {
3357 ast_channel_unlock(c0
);
3358 ast_channel_unlock(c1
);
3359 return AST_BRIDGE_FAILED_NOWARN
;
3361 audio_p0_res
= AST_RTP_TRY_PARTIAL
;
3362 audio_p1_res
= AST_RTP_TRY_PARTIAL
;
3365 /* If the core will need to compensate and the P2P bridge will need to feed up DTMF frames then we can not reliably do so yet, so do not P2P bridge */
3366 if ((audio_p0_res
== AST_RTP_TRY_PARTIAL
&& ast_test_flag(p0
, FLAG_P2P_NEED_DTMF
) && ast_test_flag(p0
, FLAG_DTMF_COMPENSATE
)) ||
3367 (audio_p1_res
== AST_RTP_TRY_PARTIAL
&& ast_test_flag(p1
, FLAG_P2P_NEED_DTMF
) && ast_test_flag(p1
, FLAG_DTMF_COMPENSATE
))) {
3368 ast_channel_unlock(c0
);
3369 ast_channel_unlock(c1
);
3370 return AST_BRIDGE_FAILED_NOWARN
;
3373 /* Get codecs from both sides */
3374 codec0
= pr0
->get_codec
? pr0
->get_codec(c0
) : 0;
3375 codec1
= pr1
->get_codec
? pr1
->get_codec(c1
) : 0;
3376 if (codec0
&& codec1
&& !(codec0
& codec1
)) {
3377 /* Hey, we can't do native bridging if both parties speak different codecs */
3379 ast_log(LOG_DEBUG
, "Channel codec0 = %d is not codec1 = %d, cannot native bridge in RTP.\n", codec0
, codec1
);
3380 ast_channel_unlock(c0
);
3381 ast_channel_unlock(c1
);
3382 return AST_BRIDGE_FAILED_NOWARN
;
3385 /* If either side can only do a partial bridge, then don't try for a true native bridge */
3386 if (audio_p0_res
== AST_RTP_TRY_PARTIAL
|| audio_p1_res
== AST_RTP_TRY_PARTIAL
) {
3387 struct ast_format_list fmt0
, fmt1
;
3389 /* In order to do Packet2Packet bridging both sides must be in the same rawread/rawwrite */
3390 if (c0
->rawreadformat
!= c1
->rawwriteformat
|| c1
->rawreadformat
!= c0
->rawwriteformat
) {
3392 ast_log(LOG_DEBUG
, "Cannot packet2packet bridge - raw formats are incompatible\n");
3393 ast_channel_unlock(c0
);
3394 ast_channel_unlock(c1
);
3395 return AST_BRIDGE_FAILED_NOWARN
;
3397 /* They must also be using the same packetization */
3398 fmt0
= ast_codec_pref_getsize(&p0
->pref
, c0
->rawreadformat
);
3399 fmt1
= ast_codec_pref_getsize(&p1
->pref
, c1
->rawreadformat
);
3400 if (fmt0
.cur_ms
!= fmt1
.cur_ms
) {
3402 ast_log(LOG_DEBUG
, "Cannot packet2packet bridge - packetization settings prevent it\n");
3403 ast_channel_unlock(c0
);
3404 ast_channel_unlock(c1
);
3405 return AST_BRIDGE_FAILED_NOWARN
;
3408 if (option_verbose
> 2)
3409 ast_verbose(VERBOSE_PREFIX_3
"Packet2Packet bridging %s and %s\n", c0
->name
, c1
->name
);
3410 res
= bridge_p2p_loop(c0
, c1
, p0
, p1
, timeoutms
, flags
, fo
, rc
, pvt0
, pvt1
);
3412 if (option_verbose
> 2)
3413 ast_verbose(VERBOSE_PREFIX_3
"Native bridging %s and %s\n", c0
->name
, c1
->name
);
3414 res
= bridge_native_loop(c0
, c1
, p0
, p1
, vp0
, vp1
, pr0
, pr1
, codec0
, codec1
, timeoutms
, flags
, fo
, rc
, pvt0
, pvt1
);
3420 static int rtp_do_debug_ip(int fd
, int argc
, char *argv
[])
3423 struct ast_hostent ahp
;
3428 return RESULT_SHOWUSAGE
;
3430 p
= strstr(arg
, ":");
3436 hp
= ast_gethostbyname(arg
, &ahp
);
3438 return RESULT_SHOWUSAGE
;
3439 rtpdebugaddr
.sin_family
= AF_INET
;
3440 memcpy(&rtpdebugaddr
.sin_addr
, hp
->h_addr
, sizeof(rtpdebugaddr
.sin_addr
));
3441 rtpdebugaddr
.sin_port
= htons(port
);
3443 ast_cli(fd
, "RTP Debugging Enabled for IP: %s\n", ast_inet_ntoa(rtpdebugaddr
.sin_addr
));
3445 ast_cli(fd
, "RTP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(rtpdebugaddr
.sin_addr
), port
);
3447 return RESULT_SUCCESS
;
3450 static int rtcp_do_debug_ip_deprecated(int fd
, int argc
, char *argv
[])
3453 struct ast_hostent ahp
;
3457 return RESULT_SHOWUSAGE
;
3460 p
= strstr(arg
, ":");
3466 hp
= ast_gethostbyname(arg
, &ahp
);
3468 return RESULT_SHOWUSAGE
;
3469 rtcpdebugaddr
.sin_family
= AF_INET
;
3470 memcpy(&rtcpdebugaddr
.sin_addr
, hp
->h_addr
, sizeof(rtcpdebugaddr
.sin_addr
));
3471 rtcpdebugaddr
.sin_port
= htons(port
);
3473 ast_cli(fd
, "RTCP Debugging Enabled for IP: %s\n", ast_inet_ntoa(rtcpdebugaddr
.sin_addr
));
3475 ast_cli(fd
, "RTCP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(rtcpdebugaddr
.sin_addr
), port
);
3477 return RESULT_SUCCESS
;
3480 static int rtcp_do_debug_ip(int fd
, int argc
, char *argv
[])
3483 struct ast_hostent ahp
;
3487 return RESULT_SHOWUSAGE
;
3490 p
= strstr(arg
, ":");
3496 hp
= ast_gethostbyname(arg
, &ahp
);
3498 return RESULT_SHOWUSAGE
;
3499 rtcpdebugaddr
.sin_family
= AF_INET
;
3500 memcpy(&rtcpdebugaddr
.sin_addr
, hp
->h_addr
, sizeof(rtcpdebugaddr
.sin_addr
));
3501 rtcpdebugaddr
.sin_port
= htons(port
);
3503 ast_cli(fd
, "RTCP Debugging Enabled for IP: %s\n", ast_inet_ntoa(rtcpdebugaddr
.sin_addr
));
3505 ast_cli(fd
, "RTCP Debugging Enabled for IP: %s:%d\n", ast_inet_ntoa(rtcpdebugaddr
.sin_addr
), port
);
3507 return RESULT_SUCCESS
;
3510 static int rtp_do_debug(int fd
, int argc
, char *argv
[])
3514 return RESULT_SHOWUSAGE
;
3515 return rtp_do_debug_ip(fd
, argc
, argv
);
3518 memset(&rtpdebugaddr
,0,sizeof(rtpdebugaddr
));
3519 ast_cli(fd
, "RTP Debugging Enabled\n");
3520 return RESULT_SUCCESS
;
3523 static int rtcp_do_debug_deprecated(int fd
, int argc
, char *argv
[]) {
3526 return RESULT_SHOWUSAGE
;
3527 return rtcp_do_debug_ip_deprecated(fd
, argc
, argv
);
3530 memset(&rtcpdebugaddr
,0,sizeof(rtcpdebugaddr
));
3531 ast_cli(fd
, "RTCP Debugging Enabled\n");
3532 return RESULT_SUCCESS
;
3535 static int rtcp_do_debug(int fd
, int argc
, char *argv
[]) {
3538 return RESULT_SHOWUSAGE
;
3539 return rtcp_do_debug_ip(fd
, argc
, argv
);
3542 memset(&rtcpdebugaddr
,0,sizeof(rtcpdebugaddr
));
3543 ast_cli(fd
, "RTCP Debugging Enabled\n");
3544 return RESULT_SUCCESS
;
3547 static int rtcp_do_stats_deprecated(int fd
, int argc
, char *argv
[]) {
3549 return RESULT_SHOWUSAGE
;
3552 ast_cli(fd
, "RTCP Stats Enabled\n");
3553 return RESULT_SUCCESS
;
3556 static int rtcp_do_stats(int fd
, int argc
, char *argv
[]) {
3558 return RESULT_SHOWUSAGE
;
3561 ast_cli(fd
, "RTCP Stats Enabled\n");
3562 return RESULT_SUCCESS
;
3565 static int rtp_no_debug(int fd
, int argc
, char *argv
[])
3568 return RESULT_SHOWUSAGE
;
3570 ast_cli(fd
,"RTP Debugging Disabled\n");
3571 return RESULT_SUCCESS
;
3574 static int rtcp_no_debug_deprecated(int fd
, int argc
, char *argv
[])
3577 return RESULT_SHOWUSAGE
;
3579 ast_cli(fd
,"RTCP Debugging Disabled\n");
3580 return RESULT_SUCCESS
;
3583 static int rtcp_no_debug(int fd
, int argc
, char *argv
[])
3586 return RESULT_SHOWUSAGE
;
3588 ast_cli(fd
,"RTCP Debugging Disabled\n");
3589 return RESULT_SUCCESS
;
3592 static int rtcp_no_stats_deprecated(int fd
, int argc
, char *argv
[])
3595 return RESULT_SHOWUSAGE
;
3597 ast_cli(fd
,"RTCP Stats Disabled\n");
3598 return RESULT_SUCCESS
;
3601 static int rtcp_no_stats(int fd
, int argc
, char *argv
[])
3604 return RESULT_SHOWUSAGE
;
3606 ast_cli(fd
,"RTCP Stats Disabled\n");
3607 return RESULT_SUCCESS
;
3610 static int stun_do_debug(int fd
, int argc
, char *argv
[])
3613 return RESULT_SHOWUSAGE
;
3616 ast_cli(fd
, "STUN Debugging Enabled\n");
3617 return RESULT_SUCCESS
;
3620 static int stun_no_debug(int fd
, int argc
, char *argv
[])
3623 return RESULT_SHOWUSAGE
;
3625 ast_cli(fd
, "STUN Debugging Disabled\n");
3626 return RESULT_SUCCESS
;
3629 static char debug_usage
[] =
3630 "Usage: rtp debug [ip host[:port]]\n"
3631 " Enable dumping of all RTP packets to and from host.\n";
3633 static char no_debug_usage
[] =
3634 "Usage: rtp debug off\n"
3635 " Disable all RTP debugging\n";
3637 static char stun_debug_usage
[] =
3638 "Usage: stun debug\n"
3639 " Enable STUN (Simple Traversal of UDP through NATs) debugging\n";
3641 static char stun_no_debug_usage
[] =
3642 "Usage: stun debug off\n"
3643 " Disable STUN debugging\n";
3645 static char rtcp_debug_usage
[] =
3646 "Usage: rtcp debug [ip host[:port]]\n"
3647 " Enable dumping of all RTCP packets to and from host.\n";
3649 static char rtcp_no_debug_usage
[] =
3650 "Usage: rtcp debug off\n"
3651 " Disable all RTCP debugging\n";
3653 static char rtcp_stats_usage
[] =
3654 "Usage: rtcp stats\n"
3655 " Enable dumping of RTCP stats.\n";
3657 static char rtcp_no_stats_usage
[] =
3658 "Usage: rtcp stats off\n"
3659 " Disable all RTCP stats\n";
3661 static struct ast_cli_entry cli_rtp_no_debug_deprecated
= {
3662 { "rtp", "no", "debug", NULL
},
3666 static struct ast_cli_entry cli_rtp_rtcp_debug_ip_deprecated
= {
3667 { "rtp", "rtcp", "debug", "ip", NULL
},
3668 rtcp_do_debug_deprecated
, NULL
,
3671 static struct ast_cli_entry cli_rtp_rtcp_debug_deprecated
= {
3672 { "rtp", "rtcp", "debug", NULL
},
3673 rtcp_do_debug_deprecated
, NULL
,
3676 static struct ast_cli_entry cli_rtp_rtcp_no_debug_deprecated
= {
3677 { "rtp", "rtcp", "no", "debug", NULL
},
3678 rtcp_no_debug_deprecated
, NULL
,
3681 static struct ast_cli_entry cli_rtp_rtcp_stats_deprecated
= {
3682 { "rtp", "rtcp", "stats", NULL
},
3683 rtcp_do_stats_deprecated
, NULL
,
3686 static struct ast_cli_entry cli_rtp_rtcp_no_stats_deprecated
= {
3687 { "rtp", "rtcp", "no", "stats", NULL
},
3688 rtcp_no_stats_deprecated
, NULL
,
3691 static struct ast_cli_entry cli_stun_no_debug_deprecated
= {
3692 { "stun", "no", "debug", NULL
},
3693 stun_no_debug
, NULL
,
3696 static struct ast_cli_entry cli_rtp
[] = {
3697 { { "rtp", "debug", "ip", NULL
},
3698 rtp_do_debug
, "Enable RTP debugging on IP",
3701 { { "rtp", "debug", NULL
},
3702 rtp_do_debug
, "Enable RTP debugging",
3705 { { "rtp", "debug", "off", NULL
},
3706 rtp_no_debug
, "Disable RTP debugging",
3707 no_debug_usage
, NULL
, &cli_rtp_no_debug_deprecated
},
3709 { { "rtcp", "debug", "ip", NULL
},
3710 rtcp_do_debug
, "Enable RTCP debugging on IP",
3711 rtcp_debug_usage
, NULL
, &cli_rtp_rtcp_debug_ip_deprecated
},
3713 { { "rtcp", "debug", NULL
},
3714 rtcp_do_debug
, "Enable RTCP debugging",
3715 rtcp_debug_usage
, NULL
, &cli_rtp_rtcp_debug_deprecated
},
3717 { { "rtcp", "debug", "off", NULL
},
3718 rtcp_no_debug
, "Disable RTCP debugging",
3719 rtcp_no_debug_usage
, NULL
, &cli_rtp_rtcp_no_debug_deprecated
},
3721 { { "rtcp", "stats", NULL
},
3722 rtcp_do_stats
, "Enable RTCP stats",
3723 rtcp_stats_usage
, NULL
, &cli_rtp_rtcp_stats_deprecated
},
3725 { { "rtcp", "stats", "off", NULL
},
3726 rtcp_no_stats
, "Disable RTCP stats",
3727 rtcp_no_stats_usage
, NULL
, &cli_rtp_rtcp_no_stats_deprecated
},
3729 { { "stun", "debug", NULL
},
3730 stun_do_debug
, "Enable STUN debugging",
3733 { { "stun", "debug", "off", NULL
},
3734 stun_no_debug
, "Disable STUN debugging",
3735 stun_no_debug_usage
, NULL
, &cli_stun_no_debug_deprecated
},
3738 int ast_rtp_reload(void)
3740 struct ast_config
*cfg
;
3745 dtmftimeout
= DEFAULT_DTMF_TIMEOUT
;
3746 cfg
= ast_config_load("rtp.conf");
3748 if ((s
= ast_variable_retrieve(cfg
, "general", "rtpstart"))) {
3750 if (rtpstart
< 1024)
3752 if (rtpstart
> 65535)
3755 if ((s
= ast_variable_retrieve(cfg
, "general", "rtpend"))) {
3762 if ((s
= ast_variable_retrieve(cfg
, "general", "rtcpinterval"))) {
3763 rtcpinterval
= atoi(s
);
3764 if (rtcpinterval
== 0)
3765 rtcpinterval
= 0; /* Just so we're clear... it's zero */
3766 if (rtcpinterval
< RTCP_MIN_INTERVALMS
)
3767 rtcpinterval
= RTCP_MIN_INTERVALMS
; /* This catches negative numbers too */
3768 if (rtcpinterval
> RTCP_MAX_INTERVALMS
)
3769 rtcpinterval
= RTCP_MAX_INTERVALMS
;
3771 if ((s
= ast_variable_retrieve(cfg
, "general", "rtpchecksums"))) {
3779 ast_log(LOG_WARNING
, "Disabling RTP checksums is not supported on this operating system!\n");
3782 if ((s
= ast_variable_retrieve(cfg
, "general", "dtmftimeout"))) {
3783 dtmftimeout
= atoi(s
);
3784 if ((dtmftimeout
< 0) || (dtmftimeout
> 20000)) {
3785 ast_log(LOG_WARNING
, "DTMF timeout of '%d' outside range, using default of '%d' instead\n",
3786 dtmftimeout
, DEFAULT_DTMF_TIMEOUT
);
3787 dtmftimeout
= DEFAULT_DTMF_TIMEOUT
;
3790 ast_config_destroy(cfg
);
3792 if (rtpstart
>= rtpend
) {
3793 ast_log(LOG_WARNING
, "Unreasonable values for RTP start/end port in rtp.conf\n");
3797 if (option_verbose
> 1)
3798 ast_verbose(VERBOSE_PREFIX_2
"RTP Allocating from port range %d -> %d\n", rtpstart
, rtpend
);
3802 /*! \brief Initialize the RTP system in Asterisk */
3803 void ast_rtp_init(void)
3805 ast_cli_register_multiple(cli_rtp
, sizeof(cli_rtp
) / sizeof(struct ast_cli_entry
));