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/netsock.h"
57 #include "asterisk/cli.h"
58 #include "asterisk/manager.h"
59 #include "asterisk/unaligned.h"
60 #include "asterisk/utils.h"
62 #define MAX_TIMESTAMP_SKEW 640
64 #define RTP_SEQ_MOD (1<<16) /*!< A sequence number can't be more than 16 bits */
65 #define RTCP_DEFAULT_INTERVALMS 5000 /*!< Default milli-seconds between RTCP reports we send */
66 #define RTCP_MIN_INTERVALMS 500 /*!< Min milli-seconds between RTCP reports we send */
67 #define RTCP_MAX_INTERVALMS 60000 /*!< Max milli-seconds between RTCP reports we send */
69 #define RTCP_PT_FUR 192
70 #define RTCP_PT_SR 200
71 #define RTCP_PT_RR 201
72 #define RTCP_PT_SDES 202
73 #define RTCP_PT_BYE 203
74 #define RTCP_PT_APP 204
78 #define DEFAULT_DTMF_TIMEOUT 3000 /*!< samples */
80 static int dtmftimeout = DEFAULT_DTMF_TIMEOUT;
82 static int rtpstart; /*!< First port for RTP sessions (set in rtp.conf) */
83 static int rtpend; /*!< Last port for RTP sessions (set in rtp.conf) */
84 static int rtpdebug; /*!< Are we debugging? */
85 static int rtcpdebug; /*!< Are we debugging RTCP? */
86 static int rtcpstats; /*!< Are we debugging RTCP? */
87 static int rtcpinterval = RTCP_DEFAULT_INTERVALMS; /*!< Time between rtcp reports in millisecs */
88 static int stundebug; /*!< Are we debugging stun? */
89 static struct sockaddr_in rtpdebugaddr; /*!< Debug packets to/from this host */
90 static struct sockaddr_in rtcpdebugaddr; /*!< Debug RTCP packets to/from this host */
92 static int nochecksums;
95 /* Uncomment this to enable more intense native bridging, but note: this is currently buggy */
96 /* #define P2P_INTENSE */
99 * \brief Structure representing a RTP session.
101 * 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 [...]"
104 /*! \brief The value of each payload format mapping: */
105 struct rtpPayloadType {
106 int isAstFormat; /*!< whether the following code is an AST_FORMAT */
111 /*! \brief RTP session description */
115 unsigned char rawdata[8192 + AST_FRIENDLY_OFFSET];
116 unsigned int ssrc; /*!< Synchronization source, RFC 3550, page 10. */
117 unsigned int themssrc; /*!< Their SSRC */
120 unsigned int lastrxts;
121 unsigned int lastividtimestamp;
122 unsigned int lastovidtimestamp;
123 unsigned int lastitexttimestamp;
124 unsigned int lastotexttimestamp;
125 unsigned int lasteventseqn;
126 int lastrxseqno; /*!< Last received sequence number */
127 unsigned short seedrxseqno; /*!< What sequence number did they start with?*/
128 unsigned int seedrxts; /*!< What RTP timestamp did they start with? */
129 unsigned int rxcount; /*!< How many packets have we received? */
130 unsigned int rxoctetcount; /*!< How many octets have we received? should be rxcount *160*/
131 unsigned int txcount; /*!< How many packets have we sent? */
132 unsigned int txoctetcount; /*!< How many octets have we sent? (txcount*160)*/
133 unsigned int cycles; /*!< Shifted count of sequence number cycles */
134 double rxjitter; /*!< Interarrival jitter at the moment */
135 double rxtransit; /*!< Relative transit time for previous packet */
139 int rtptimeout; /*!< RTP timeout time (negative or zero means disabled, negative value means temporarily disabled) */
140 int rtpholdtimeout; /*!< RTP timeout when on hold (negative or zero means disabled, negative value means temporarily disabled). */
141 int rtpkeepalive; /*!< Send RTP comfort noice packets for keepalive */
143 /* DTMF Reception Variables */
145 unsigned int lastevent;
147 unsigned int dtmfsamples;
148 /* DTMF Transmission Variables */
149 unsigned int lastdigitts;
150 char sending_digit; /*!< boolean - are we sending digits */
151 char send_digit; /*!< digit we are sending */
156 struct sockaddr_in us; /*!< Socket representation of the local endpoint. */
157 struct sockaddr_in them; /*!< Socket representation of the remote endpoint. */
158 struct timeval rxcore;
159 struct timeval txcore;
160 double drxcore; /*!< The double representation of the first received packet */
161 struct timeval lastrx; /*!< timeval when we last received a packet */
162 struct timeval dtmfmute;
163 struct ast_smoother *smoother;
165 unsigned short seqno; /*!< Sequence number, RFC 3550, page 13. */
166 unsigned short rxseqno;
167 struct sched_context *sched;
168 struct io_context *io;
170 ast_rtp_callback callback;
172 ast_mutex_t bridge_lock;
174 struct rtpPayloadType current_RTP_PT[MAX_RTP_PT];
175 int rtp_lookup_code_cache_isAstFormat; /*!< a cache for the result of rtp_lookup_code(): */
176 int rtp_lookup_code_cache_code;
177 int rtp_lookup_code_cache_result;
178 struct ast_rtcp *rtcp;
179 struct ast_codec_pref pref;
180 struct ast_rtp *bridged; /*!< Who we are Packet bridged to */
183 /* Forward declarations */
184 static int ast_rtcp_write(void *data);
185 static void timeval2ntp(struct timeval tv, unsigned int *msw, unsigned int *lsw);
186 static int ast_rtcp_write_sr(void *data);
187 static int ast_rtcp_write_rr(void *data);
188 static unsigned int ast_rtcp_calc_interval(struct ast_rtp *rtp);
189 static int ast_rtp_senddigit_continuation(struct ast_rtp *rtp);
190 int ast_rtp_senddigit_end(struct ast_rtp *rtp, char digit);
192 #define FLAG_3389_WARNING (1 << 0)
193 #define FLAG_NAT_ACTIVE (3 << 1)
194 #define FLAG_NAT_INACTIVE (0 << 1)
195 #define FLAG_NAT_INACTIVE_NOWARN (1 << 1)
196 #define FLAG_HAS_DTMF (1 << 3)
197 #define FLAG_P2P_SENT_MARK (1 << 4)
198 #define FLAG_P2P_NEED_DTMF (1 << 5)
199 #define FLAG_CALLBACK_MODE (1 << 6)
200 #define FLAG_DTMF_COMPENSATE (1 << 7)
201 #define FLAG_HAS_STUN (1 << 8)
204 * \brief Structure defining an RTCP session.
206 * The concept "RTCP session" is not defined in RFC 3550, but since
207 * this structure is analogous to ast_rtp, which tracks a RTP session,
208 * it is logical to think of this as a RTCP session.
210 * RTCP packet is defined on page 9 of RFC 3550.
214 int s; /*!< Socket */
215 struct sockaddr_in us; /*!< Socket representation of the local endpoint. */
216 struct sockaddr_in them; /*!< Socket representation of the remote endpoint. */
217 unsigned int soc; /*!< What they told us */
218 unsigned int spc; /*!< What they told us */
219 unsigned int themrxlsr; /*!< The middle 32 bits of the NTP timestamp in the last received SR*/
220 struct timeval rxlsr; /*!< Time when we got their last SR */
221 struct timeval txlsr; /*!< Time when we sent or last SR*/
222 unsigned int expected_prior; /*!< no. packets in previous interval */
223 unsigned int received_prior; /*!< no. packets received in previous interval */
224 int schedid; /*!< Schedid returned from ast_sched_add() to schedule RTCP-transmissions*/
225 unsigned int rr_count; /*!< number of RRs we've sent, not including report blocks in SR's */
226 unsigned int sr_count; /*!< number of SRs we've sent */
227 unsigned int lastsrtxcount; /*!< Transmit packet count when last SR sent */
228 double accumulated_transit; /*!< accumulated a-dlsr-lsr */
229 double rtt; /*!< Last reported rtt */
230 unsigned int reported_jitter; /*!< The contents of their last jitter entry in the RR */
231 unsigned int reported_lost; /*!< Reported lost packets in their RR */
232 char quality[AST_MAX_USER_FIELD];
241 * \brief STUN support code
243 * This code provides some support for doing STUN transactions.
244 * Eventually it should be moved elsewhere as other protocols
245 * than RTP can benefit from it - e.g. SIP.
246 * STUN is described in RFC3489 and it is based on the exchange
247 * of UDP packets between a client and one or more servers to
248 * determine the externally visible address (and port) of the client
249 * once it has gone through the NAT boxes that connect it to the
251 * The simplest request packet is just the header defined in
252 * struct stun_header, and from the response we may just look at
253 * one attribute, STUN_MAPPED_ADDRESS, that we find in the response.
254 * By doing more transactions with different server addresses we
255 * may determine more about the behaviour of the NAT boxes, of
256 * course - the details are in the RFC.
258 * All STUN packets start with a simple header made of a type,
259 * length (excluding the header) and a 16-byte random transaction id.
260 * Following the header we may have zero or more attributes, each
261 * structured as a type, length and a value (whose format depends
262 * on the type, but often contains addresses).
263 * Of course all fields are in network format.
266 typedef struct { unsigned int id[4]; } __attribute__((packed)) stun_trans_id;
269 unsigned short msgtype;
270 unsigned short msglen;
272 unsigned char ies[0];
273 } __attribute__((packed));
278 unsigned char value[0];
279 } __attribute__((packed));
282 * The format normally used for addresses carried by STUN messages.
285 unsigned char unused;
286 unsigned char family;
289 } __attribute__((packed));
291 #define STUN_IGNORE (0)
292 #define STUN_ACCEPT (1)
294 /*! \brief STUN message types
295 * 'BIND' refers to transactions used to determine the externally
296 * visible addresses. 'SEC' refers to transactions used to establish
297 * a session key for subsequent requests.
298 * 'SEC' functionality is not supported here.
301 #define STUN_BINDREQ 0x0001
302 #define STUN_BINDRESP 0x0101
303 #define STUN_BINDERR 0x0111
304 #define STUN_SECREQ 0x0002
305 #define STUN_SECRESP 0x0102
306 #define STUN_SECERR 0x0112
308 /*! \brief Basic attribute types in stun messages.
309 * Messages can also contain custom attributes (codes above 0x7fff)
311 #define STUN_MAPPED_ADDRESS 0x0001
312 #define STUN_RESPONSE_ADDRESS 0x0002
313 #define STUN_CHANGE_REQUEST 0x0003
314 #define STUN_SOURCE_ADDRESS 0x0004
315 #define STUN_CHANGED_ADDRESS 0x0005
316 #define STUN_USERNAME 0x0006
317 #define STUN_PASSWORD 0x0007
318 #define STUN_MESSAGE_INTEGRITY 0x0008
319 #define STUN_ERROR_CODE 0x0009
320 #define STUN_UNKNOWN_ATTRIBUTES 0x000a
321 #define STUN_REFLECTED_FROM 0x000b
323 /*! \brief helper function to print message names */
324 static const char *stun_msg2str(int msg)
328 return "Binding Request";
330 return "Binding Response";
332 return "Binding Error Response";
334 return "Shared Secret Request";
336 return "Shared Secret Response";
338 return "Shared Secret Error Response";
340 return "Non-RFC3489 Message";
343 /*! \brief helper function to print attribute names */
344 static const char *stun_attr2str(int msg)
347 case STUN_MAPPED_ADDRESS:
348 return "Mapped Address";
349 case STUN_RESPONSE_ADDRESS:
350 return "Response Address";
351 case STUN_CHANGE_REQUEST:
352 return "Change Request";
353 case STUN_SOURCE_ADDRESS:
354 return "Source Address";
355 case STUN_CHANGED_ADDRESS:
356 return "Changed Address";
361 case STUN_MESSAGE_INTEGRITY:
362 return "Message Integrity";
363 case STUN_ERROR_CODE:
365 case STUN_UNKNOWN_ATTRIBUTES:
366 return "Unknown Attributes";
367 case STUN_REFLECTED_FROM:
368 return "Reflected From";
370 return "Non-RFC3489 Attribute";
373 /*! \brief here we store credentials extracted from a message */
375 const char *username;
376 const char *password;
379 static int stun_process_attr(struct stun_state *state, struct stun_attr *attr)
382 ast_verbose("Found STUN Attribute %s (%04x), length %d\n",
383 stun_attr2str(ntohs(attr->attr)), ntohs(attr->attr), ntohs(attr->len));
384 switch (ntohs(attr->attr)) {
386 state->username = (const char *) (attr->value);
389 state->password = (const char *) (attr->value);
393 ast_verbose("Ignoring STUN attribute %s (%04x), length %d\n",
394 stun_attr2str(ntohs(attr->attr)), ntohs(attr->attr), ntohs(attr->len));
399 /*! \brief append a string to an STUN message */
400 static void append_attr_string(struct stun_attr **attr, int attrval, const char *s, int *len, int *left)
402 int size = sizeof(**attr) + strlen(s);
404 (*attr)->attr = htons(attrval);
405 (*attr)->len = htons(strlen(s));
406 memcpy((*attr)->value, s, strlen(s));
407 (*attr) = (struct stun_attr *)((*attr)->value + strlen(s));
413 /*! \brief append an address to an STUN message */
414 static void append_attr_address(struct stun_attr **attr, int attrval, struct sockaddr_in *sin, int *len, int *left)
416 int size = sizeof(**attr) + 8;
417 struct stun_addr *addr;
419 (*attr)->attr = htons(attrval);
420 (*attr)->len = htons(8);
421 addr = (struct stun_addr *)((*attr)->value);
424 addr->port = sin->sin_port;
425 addr->addr = sin->sin_addr.s_addr;
426 (*attr) = (struct stun_attr *)((*attr)->value + 8);
432 /*! \brief wrapper to send an STUN message */
433 static int stun_send(int s, struct sockaddr_in *dst, struct stun_header *resp)
435 return sendto(s, resp, ntohs(resp->msglen) + sizeof(*resp), 0,
436 (struct sockaddr *)dst, sizeof(*dst));
439 /*! \brief helper function to generate a random request id */
440 static void stun_req_id(struct stun_header *req)
443 for (x = 0; x < 4; x++)
444 req->id.id[x] = ast_random();
447 size_t ast_rtp_alloc_size(void)
449 return sizeof(struct ast_rtp);
452 /*! \brief callback type to be invoked on stun responses. */
453 typedef int (stun_cb_f)(struct stun_attr *attr, void *arg);
455 /*! \brief handle an incoming STUN message.
457 * Do some basic sanity checks on packet size and content,
458 * try to extract a bit of information, and possibly reply.
459 * At the moment this only processes BIND requests, and returns
460 * the externally visible address of the request.
461 * If a callback is specified, invoke it with the attribute.
463 static int stun_handle_packet(int s, struct sockaddr_in *src,
464 unsigned char *data, size_t len, stun_cb_f *stun_cb, void *arg)
466 struct stun_header *hdr = (struct stun_header *)data;
467 struct stun_attr *attr;
468 struct stun_state st;
469 int ret = STUN_IGNORE;
472 /* On entry, 'len' is the length of the udp payload. After the
473 * initial checks it becomes the size of unprocessed options,
474 * while 'data' is advanced accordingly.
476 if (len < sizeof(struct stun_header)) {
477 ast_debug(1, "Runt STUN packet (only %d, wanting at least %d)\n", (int) len, (int) sizeof(struct stun_header));
480 len -= sizeof(struct stun_header);
481 data += sizeof(struct stun_header);
482 x = ntohs(hdr->msglen); /* len as advertised in the message */
484 ast_verbose("STUN Packet, msg %s (%04x), length: %d\n", stun_msg2str(ntohs(hdr->msgtype)), ntohs(hdr->msgtype), x);
486 ast_debug(1, "Scrambled STUN packet length (got %d, expecting %d)\n", x, (int)len);
489 memset(&st, 0, sizeof(st));
491 if (len < sizeof(struct stun_attr)) {
492 ast_debug(1, "Runt Attribute (got %d, expecting %d)\n", (int)len, (int) sizeof(struct stun_attr));
495 attr = (struct stun_attr *)data;
496 /* compute total attribute length */
497 x = ntohs(attr->len) + sizeof(struct stun_attr);
499 ast_debug(1, "Inconsistent Attribute (length %d exceeds remaining msg len %d)\n", x, (int)len);
504 if (stun_process_attr(&st, attr)) {
505 ast_debug(1, "Failed to handle attribute %s (%04x)\n", stun_attr2str(ntohs(attr->attr)), ntohs(attr->attr));
508 /* Clear attribute id: in case previous entry was a string,
509 * this will act as the terminator for the string.
515 /* Null terminate any string.
516 * XXX NOTE, we write past the size of the buffer passed by the
517 * caller, so this is potentially dangerous. The only thing that
518 * saves us is that usually we read the incoming message in a
519 * much larger buffer in the struct ast_rtp
523 /* Now prepare to generate a reply, which at the moment is done
524 * only for properly formed (len == 0) STUN_BINDREQ messages.
527 unsigned char respdata[1024];
528 struct stun_header *resp = (struct stun_header *)respdata;
529 int resplen = 0; /* len excluding header */
530 int respleft = sizeof(respdata) - sizeof(struct stun_header);
535 attr = (struct stun_attr *)resp->ies;
536 switch (ntohs(hdr->msgtype)) {
539 ast_verbose("STUN Bind Request, username: %s\n",
540 st.username ? st.username : "<none>");
542 append_attr_string(&attr, STUN_USERNAME, st.username, &resplen, &respleft);
543 append_attr_address(&attr, STUN_MAPPED_ADDRESS, src, &resplen, &respleft);
544 resp->msglen = htons(resplen);
545 resp->msgtype = htons(STUN_BINDRESP);
546 stun_send(s, src, resp);
551 ast_verbose("Dunno what to do with STUN message %04x (%s)\n", ntohs(hdr->msgtype), stun_msg2str(ntohs(hdr->msgtype)));
557 /*! \brief Extract the STUN_MAPPED_ADDRESS from the stun response.
558 * This is used as a callback for stun_handle_response
559 * when called from ast_stun_request.
561 static int stun_get_mapped(struct stun_attr *attr, void *arg)
563 struct stun_addr *addr = (struct stun_addr *)(attr + 1);
564 struct sockaddr_in *sa = (struct sockaddr_in *)arg;
566 if (ntohs(attr->attr) != STUN_MAPPED_ADDRESS || ntohs(attr->len) != 8)
567 return 1; /* not us. */
568 sa->sin_port = addr->port;
569 sa->sin_addr.s_addr = addr->addr;
573 /*! \brief Generic STUN request
574 * Send a generic stun request to the server specified,
575 * possibly waiting for a reply and filling the 'reply' field with
576 * the externally visible address. Note that in this case the request
578 * (Note, the interface may change slightly in the future).
580 * \param s the socket used to send the request
581 * \param dst the address of the STUN server
582 * \param username if non null, add the username in the request
583 * \param answer if non null, the function waits for a response and
584 * puts here the externally visible address.
585 * \return 0 on success, other values on error.
587 int ast_stun_request(int s, struct sockaddr_in *dst,
588 const char *username, struct sockaddr_in *answer)
590 struct stun_header *req;
591 unsigned char reqdata[1024];
593 struct stun_attr *attr;
597 req = (struct stun_header *)reqdata;
600 reqleft = sizeof(reqdata) - sizeof(struct stun_header);
603 attr = (struct stun_attr *)req->ies;
605 append_attr_string(&attr, STUN_USERNAME, username, &reqlen, &reqleft);
606 req->msglen = htons(reqlen);
607 req->msgtype = htons(STUN_BINDREQ);
608 for (retry = 0; retry < 3; retry++) { /* XXX make retries configurable */
609 /* send request, possibly wait for reply */
610 unsigned char reply_buf[1024];
612 struct timeval to = { 3, 0 }; /* timeout, make it configurable */
613 struct sockaddr_in src;
616 res = stun_send(s, dst, req);
618 ast_log(LOG_WARNING, "ast_stun_request send #%d failed error %d, retry\n",
626 res = ast_select(s + 1, &rfds, NULL, NULL, &to);
627 if (res <= 0) /* timeout or error */
629 bzero(&src, sizeof(src));
630 srclen = sizeof(src);
631 /* XXX pass -1 in the size, because stun_handle_packet might
632 * write past the end of the buffer.
634 res = recvfrom(s, reply_buf, sizeof(reply_buf) - 1,
635 0, (struct sockaddr *)&src, &srclen);
637 ast_log(LOG_WARNING, "ast_stun_request recvfrom #%d failed error %d, retry\n",
641 bzero(answer, sizeof(struct sockaddr_in));
642 stun_handle_packet(s, &src, reply_buf, res,
643 stun_get_mapped, answer);
644 res = 0; /* signal regular exit */
650 /*! \brief send a STUN BIND request to the given destination.
651 * Optionally, add a username if specified.
653 void ast_rtp_stun_request(struct ast_rtp *rtp, struct sockaddr_in *suggestion, const char *username)
655 ast_stun_request(rtp->s, suggestion, username, NULL);
658 /*! \brief List of current sessions */
659 static AST_RWLIST_HEAD_STATIC(protos, ast_rtp_protocol);
661 static void timeval2ntp(struct timeval tv, unsigned int *msw, unsigned int *lsw)
663 unsigned int sec, usec, frac;
664 sec = tv.tv_sec + 2208988800u; /* Sec between 1900 and 1970 */
666 frac = (usec << 12) + (usec << 8) - ((usec * 3650) >> 6);
671 int ast_rtp_fd(struct ast_rtp *rtp)
676 int ast_rtcp_fd(struct ast_rtp *rtp)
683 unsigned int ast_rtcp_calc_interval(struct ast_rtp *rtp)
685 unsigned int interval;
686 /*! \todo XXX Do a more reasonable calculation on this one
687 * Look in RFC 3550 Section A.7 for an example*/
688 interval = rtcpinterval;
692 /* \brief Put RTP timeout timers on hold during another transaction, like T.38 */
693 void ast_rtp_set_rtptimers_onhold(struct ast_rtp *rtp)
695 rtp->rtptimeout = (-1) * rtp->rtptimeout;
696 rtp->rtpholdtimeout = (-1) * rtp->rtpholdtimeout;
699 /*! \brief Set rtp timeout */
700 void ast_rtp_set_rtptimeout(struct ast_rtp *rtp, int timeout)
702 rtp->rtptimeout = timeout;
705 /*! \brief Set rtp hold timeout */
706 void ast_rtp_set_rtpholdtimeout(struct ast_rtp *rtp, int timeout)
708 rtp->rtpholdtimeout = timeout;
711 /*! \brief set RTP keepalive interval */
712 void ast_rtp_set_rtpkeepalive(struct ast_rtp *rtp, int period)
714 rtp->rtpkeepalive = period;
717 /*! \brief Get rtp timeout */
718 int ast_rtp_get_rtptimeout(struct ast_rtp *rtp)
720 if (rtp->rtptimeout < 0) /* We're not checking, but remembering the setting (during T.38 transmission) */
722 return rtp->rtptimeout;
725 /*! \brief Get rtp hold timeout */
726 int ast_rtp_get_rtpholdtimeout(struct ast_rtp *rtp)
728 if (rtp->rtptimeout < 0) /* We're not checking, but remembering the setting (during T.38 transmission) */
730 return rtp->rtpholdtimeout;
733 /*! \brief Get RTP keepalive interval */
734 int ast_rtp_get_rtpkeepalive(struct ast_rtp *rtp)
736 return rtp->rtpkeepalive;
739 void ast_rtp_set_data(struct ast_rtp *rtp, void *data)
744 void ast_rtp_set_callback(struct ast_rtp *rtp, ast_rtp_callback callback)
746 rtp->callback = callback;
749 void ast_rtp_setnat(struct ast_rtp *rtp, int nat)
754 int ast_rtp_getnat(struct ast_rtp *rtp)
756 return ast_test_flag(rtp, FLAG_NAT_ACTIVE);
759 void ast_rtp_setdtmf(struct ast_rtp *rtp, int dtmf)
761 ast_set2_flag(rtp, dtmf ? 1 : 0, FLAG_HAS_DTMF);
764 void ast_rtp_setdtmfcompensate(struct ast_rtp *rtp, int compensate)
766 ast_set2_flag(rtp, compensate ? 1 : 0, FLAG_DTMF_COMPENSATE);
769 void ast_rtp_setstun(struct ast_rtp *rtp, int stun_enable)
771 ast_set2_flag(rtp, stun_enable ? 1 : 0, FLAG_HAS_STUN);
774 static void rtp_bridge_lock(struct ast_rtp *rtp)
777 ast_mutex_lock(&rtp->bridge_lock);
782 static void rtp_bridge_unlock(struct ast_rtp *rtp)
785 ast_mutex_unlock(&rtp->bridge_lock);
790 static struct ast_frame *send_dtmf(struct ast_rtp *rtp, enum ast_frame_type type)
792 if (((ast_test_flag(rtp, FLAG_DTMF_COMPENSATE) && type == AST_FRAME_DTMF_END) ||
793 (type == AST_FRAME_DTMF_BEGIN)) && ast_tvcmp(ast_tvnow(), rtp->dtmfmute) < 0) {
794 ast_debug(1, "Ignore potential DTMF echo from '%s'\n", ast_inet_ntoa(rtp->them.sin_addr));
796 rtp->dtmfsamples = 0;
797 return &ast_null_frame;
799 ast_debug(1, "Sending dtmf: %d (%c), at %s\n", rtp->resp, rtp->resp, ast_inet_ntoa(rtp->them.sin_addr));
800 if (rtp->resp == 'X') {
801 rtp->f.frametype = AST_FRAME_CONTROL;
802 rtp->f.subclass = AST_CONTROL_FLASH;
804 rtp->f.frametype = type;
805 rtp->f.subclass = rtp->resp;
815 static inline int rtp_debug_test_addr(struct sockaddr_in *addr)
819 if (rtpdebugaddr.sin_addr.s_addr) {
820 if (((ntohs(rtpdebugaddr.sin_port) != 0)
821 && (rtpdebugaddr.sin_port != addr->sin_port))
822 || (rtpdebugaddr.sin_addr.s_addr != addr->sin_addr.s_addr))
828 static inline int rtcp_debug_test_addr(struct sockaddr_in *addr)
832 if (rtcpdebugaddr.sin_addr.s_addr) {
833 if (((ntohs(rtcpdebugaddr.sin_port) != 0)
834 && (rtcpdebugaddr.sin_port != addr->sin_port))
835 || (rtcpdebugaddr.sin_addr.s_addr != addr->sin_addr.s_addr))
842 static struct ast_frame *process_cisco_dtmf(struct ast_rtp *rtp, unsigned char *data, int len)
846 struct ast_frame *f = NULL;
851 /* We should have at least 4 bytes in RTP data */
855 /* The format of Cisco RTP DTMF packet looks like next:
856 +0 - sequence number of DTMF RTP packet (begins from 1,
859 +1 (bit 0) - flaps by different DTMF digits delimited by audio
860 or repeated digit without audio???
861 +2 (+4,+6,...) - power level? (rises from 0 to 32 at begin of tone
862 then falls to 0 at its end)
863 +3 (+5,+7,...) - detected DTMF digit (0..9,*,#,A-D,...)
864 Repeated DTMF information (bytes 4/5, 6/7) is history shifted right
865 by each new packet and thus provides some redudancy.
867 Sample of Cisco RTP DTMF packet is (all data in hex):
868 19 07 00 02 12 02 20 02
869 showing end of DTMF digit '2'.
872 27 07 00 02 0A 02 20 02
873 28 06 20 02 00 02 0A 02
874 shows begin of new digit '2' with very short pause (20 ms) after
875 previous digit '2'. Bit +1.0 flips at begin of new digit.
877 Cisco RTP DTMF packets comes as replacement of audio RTP packets
878 so its uses the same sequencing and timestamping rules as replaced
879 audio packets. Repeat interval of DTMF packets is 20 ms and not rely
880 on audio framing parameters. Marker bit isn't used within stream of
881 DTMFs nor audio stream coming immediately after DTMF stream. Timestamps
882 are not sequential at borders between DTMF and audio streams,
888 event = data[3] & 0x1f;
890 if (option_debug > 2 || rtpdebug)
891 ast_debug(0, "Cisco DTMF Digit: %02x (len=%d, seq=%d, flags=%02x, power=%d, history count=%d)\n", event, len, seq, flags, power, (len - 4) / 2);
894 } else if (event < 11) {
896 } else if (event < 12) {
898 } else if (event < 16) {
899 resp = 'A' + (event - 12);
900 } else if (event < 17) {
903 if ((!rtp->resp && power) || (rtp->resp && (rtp->resp != resp))) {
905 /* Why we should care on DTMF compensation at reception? */
906 if (!ast_test_flag(rtp, FLAG_DTMF_COMPENSATE)) {
907 f = send_dtmf(rtp, AST_FRAME_DTMF_BEGIN);
908 rtp->dtmfsamples = 0;
910 } else if ((rtp->resp == resp) && !power) {
911 f = send_dtmf(rtp, AST_FRAME_DTMF_END);
912 f->samples = rtp->dtmfsamples * 8;
914 } else if (rtp->resp == resp)
915 rtp->dtmfsamples += 20 * 8;
916 rtp->dtmfcount = dtmftimeout;
921 * \brief Process RTP DTMF and events according to RFC 2833.
923 * RFC 2833 is "RTP Payload for DTMF Digits, Telephony Tones and Telephony Signals".
932 static struct ast_frame *process_rfc2833(struct ast_rtp *rtp, unsigned char *data, int len, unsigned int seqno, unsigned int timestamp)
935 unsigned int event_end;
936 unsigned int samples;
938 struct ast_frame *f = NULL;
940 /* Figure out event, event end, and samples */
941 event = ntohl(*((unsigned int *)(data)));
943 event_end = ntohl(*((unsigned int *)(data)));
946 samples = ntohl(*((unsigned int *)(data)));
949 /* Print out debug if turned on */
950 if (rtpdebug || option_debug > 2)
951 ast_debug(0, "- RTP 2833 Event: %08x (len = %d)\n", event, len);
953 /* Figure out what digit was pressed */
956 } else if (event < 11) {
958 } else if (event < 12) {
960 } else if (event < 16) {
961 resp = 'A' + (event - 12);
962 } else if (event < 17) { /* Event 16: Hook flash */
966 if (ast_test_flag(rtp, FLAG_DTMF_COMPENSATE)) {
967 if ((rtp->lastevent != timestamp) || (rtp->resp && rtp->resp != resp)) {
969 f = send_dtmf(rtp, AST_FRAME_DTMF_END);
971 rtp->lastevent = timestamp;
974 if ((!(rtp->resp) && (!(event_end & 0x80))) || (rtp->resp && rtp->resp != resp)) {
976 f = send_dtmf(rtp, AST_FRAME_DTMF_BEGIN);
977 } else if ((event_end & 0x80) && (rtp->lastevent != seqno) && rtp->resp) {
978 f = send_dtmf(rtp, AST_FRAME_DTMF_END);
979 f->len = ast_tvdiff_ms(ast_samp2tv(samples, 8000), ast_tv(0, 0)); /* XXX hard coded 8kHz */
981 rtp->lastevent = seqno;
985 rtp->dtmfcount = dtmftimeout;
986 rtp->dtmfsamples = samples;
992 * \brief Process Comfort Noise RTP.
994 * This is incomplete at the moment.
997 static struct ast_frame *process_rfc3389(struct ast_rtp *rtp, unsigned char *data, int len)
999 struct ast_frame *f = NULL;
1000 /* Convert comfort noise into audio with various codecs. Unfortunately this doesn't
1001 totally help us out becuase we don't have an engine to keep it going and we are not
1002 guaranteed to have it every 20ms or anything */
1004 ast_debug(0, "- RTP 3389 Comfort noise event: Level %d (len = %d)\n", rtp->lastrxformat, len);
1006 if (!(ast_test_flag(rtp, FLAG_3389_WARNING))) {
1007 ast_log(LOG_NOTICE, "Comfort noise support incomplete in Asterisk (RFC 3389). Please turn off on client if possible. Client IP: %s\n",
1008 ast_inet_ntoa(rtp->them.sin_addr));
1009 ast_set_flag(rtp, FLAG_3389_WARNING);
1012 /* Must have at least one byte */
1016 rtp->f.data = rtp->rawdata + AST_FRIENDLY_OFFSET;
1017 rtp->f.datalen = len - 1;
1018 rtp->f.offset = AST_FRIENDLY_OFFSET;
1019 memcpy(rtp->f.data, data + 1, len - 1);
1025 rtp->f.frametype = AST_FRAME_CNG;
1026 rtp->f.subclass = data[0] & 0x7f;
1027 rtp->f.datalen = len - 1;
1029 rtp->f.delivery.tv_usec = rtp->f.delivery.tv_sec = 0;
1034 static int rtpread(int *id, int fd, short events, void *cbdata)
1036 struct ast_rtp *rtp = cbdata;
1037 struct ast_frame *f;
1038 f = ast_rtp_read(rtp);
1041 rtp->callback(rtp, f, rtp->data);
1046 struct ast_frame *ast_rtcp_read(struct ast_rtp *rtp)
1049 int position, i, packetwords;
1051 struct sockaddr_in sin;
1052 unsigned int rtcpdata[8192 + AST_FRIENDLY_OFFSET];
1053 unsigned int *rtcpheader;
1056 unsigned int length;
1065 struct ast_frame *f = &ast_null_frame;
1067 if (!rtp || !rtp->rtcp)
1068 return &ast_null_frame;
1072 res = recvfrom(rtp->rtcp->s, rtcpdata + AST_FRIENDLY_OFFSET, sizeof(rtcpdata) - sizeof(unsigned int) * AST_FRIENDLY_OFFSET,
1073 0, (struct sockaddr *)&sin, &len);
1074 rtcpheader = (unsigned int *)(rtcpdata + AST_FRIENDLY_OFFSET);
1079 if (errno != EAGAIN) {
1080 ast_log(LOG_WARNING, "RTCP Read error: %s. Hanging up.\n", strerror(errno));
1083 return &ast_null_frame;
1086 packetwords = res / 4;
1089 /* Send to whoever sent to us */
1090 if ((rtp->rtcp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
1091 (rtp->rtcp->them.sin_port != sin.sin_port)) {
1092 memcpy(&rtp->rtcp->them, &sin, sizeof(rtp->rtcp->them));
1093 if (option_debug || rtpdebug)
1094 ast_debug(0, "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));
1098 ast_debug(1, "Got RTCP report of %d bytes\n", res);
1100 /* Process a compound packet */
1102 while (position < packetwords) {
1104 length = ntohl(rtcpheader[i]);
1105 pt = (length & 0xff0000) >> 16;
1106 rc = (length & 0x1f000000) >> 24;
1109 if ((i + length) > packetwords) {
1110 ast_log(LOG_WARNING, "RTCP Read too short\n");
1111 return &ast_null_frame;
1114 if (rtcp_debug_test_addr(&sin)) {
1115 ast_verbose("\n\nGot RTCP from %s:%d\n", ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port));
1116 ast_verbose("PT: %d(%s)\n", pt, (pt == 200) ? "Sender Report" : (pt == 201) ? "Receiver Report" : (pt == 192) ? "H.261 FUR" : "Unknown");
1117 ast_verbose("Reception reports: %d\n", rc);
1118 ast_verbose("SSRC of sender: %u\n", rtcpheader[i + 1]);
1121 i += 2; /* Advance past header and ssrc */
1125 gettimeofday(&rtp->rtcp->rxlsr,NULL); /* To be able to populate the dlsr */
1126 rtp->rtcp->spc = ntohl(rtcpheader[i+3]);
1127 rtp->rtcp->soc = ntohl(rtcpheader[i + 4]);
1128 rtp->rtcp->themrxlsr = ((ntohl(rtcpheader[i]) & 0x0000ffff) << 16) | ((ntohl(rtcpheader[i + 1]) & 0xffff0000) >> 16); /* Going to LSR in RR*/
1130 if (rtcp_debug_test_addr(&sin)) {
1131 ast_verbose("NTP timestamp: %lu.%010lu\n", (unsigned long) ntohl(rtcpheader[i]), (unsigned long) ntohl(rtcpheader[i + 1]) * 4096);
1132 ast_verbose("RTP timestamp: %lu\n", (unsigned long) ntohl(rtcpheader[i + 2]));
1133 ast_verbose("SPC: %lu\tSOC: %lu\n", (unsigned long) ntohl(rtcpheader[i + 3]), (unsigned long) ntohl(rtcpheader[i + 4]));
1138 /* Intentional fall through */
1140 /* Don't handle multiple reception reports (rc > 1) yet */
1141 /* Calculate RTT per RFC */
1142 gettimeofday(&now, NULL);
1143 timeval2ntp(now, &msw, &lsw);
1144 if (ntohl(rtcpheader[i + 4]) && ntohl(rtcpheader[i + 5])) { /* We must have the LSR && DLSR */
1145 comp = ((msw & 0xffff) << 16) | ((lsw & 0xffff0000) >> 16);
1146 lsr = ntohl(rtcpheader[i + 4]);
1147 dlsr = ntohl(rtcpheader[i + 5]);
1148 rtt = comp - lsr - dlsr;
1150 /* Convert end to end delay to usec (keeping the calculation in 64bit space)
1151 sess->ee_delay = (eedelay * 1000) / 65536; */
1153 rtt = (rtt * 1000000) >> 16;
1155 rtt = (rtt * 1000) >> 16;
1159 rttsec = rtt / 1000.;
1161 if (comp - dlsr >= lsr) {
1162 rtp->rtcp->accumulated_transit += rttsec;
1163 rtp->rtcp->rtt = rttsec;
1164 if (rtp->rtcp->maxrtt<rttsec)
1165 rtp->rtcp->maxrtt = rttsec;
1166 if (rtp->rtcp->minrtt>rttsec)
1167 rtp->rtcp->minrtt = rttsec;
1168 } else if (rtcp_debug_test_addr(&sin)) {
1169 ast_verbose("Internal RTCP NTP clock skew detected: "
1170 "lsr=%u, now=%u, dlsr=%u (%d:%03dms), "
1172 lsr, comp, dlsr, dlsr / 65536,
1173 (dlsr % 65536) * 1000 / 65536,
1174 dlsr - (comp - lsr));
1178 rtp->rtcp->reported_jitter = ntohl(rtcpheader[i + 3]);
1179 rtp->rtcp->reported_lost = ntohl(rtcpheader[i + 1]) & 0xffffff;
1180 if (rtcp_debug_test_addr(&sin)) {
1181 ast_verbose(" Fraction lost: %ld\n", (((long) ntohl(rtcpheader[i + 1]) & 0xff000000) >> 24));
1182 ast_verbose(" Packets lost so far: %d\n", rtp->rtcp->reported_lost);
1183 ast_verbose(" Highest sequence number: %ld\n", (long) (ntohl(rtcpheader[i + 2]) & 0xffff));
1184 ast_verbose(" Sequence number cycles: %ld\n", (long) (ntohl(rtcpheader[i + 2]) & 0xffff) >> 16);
1185 ast_verbose(" Interarrival jitter: %u\n", rtp->rtcp->reported_jitter);
1186 ast_verbose(" Last SR(our NTP): %lu.%010lu\n",(unsigned long) ntohl(rtcpheader[i + 4]) >> 16,((unsigned long) ntohl(rtcpheader[i + 4]) << 16) * 4096);
1187 ast_verbose(" DLSR: %4.4f (sec)\n",ntohl(rtcpheader[i + 5])/65536.0);
1189 ast_verbose(" RTT: %lu(sec)\n", (unsigned long) rtt);
1192 manager_event(EVENT_FLAG_REPORTING, "RTCPReceived", "From %s:%d\r\n"
1194 "ReceptionReports: %d\r\n"
1195 "SenderSSRC: %u\r\n"
1196 "FractionLost: %ld\r\n"
1197 "PacketsLost: %d\r\n"
1198 "HighestSequence: %ld\r\n"
1199 "SequenceNumberCycles: %ld\r\n"
1201 "LastSR: %lu.%010lu\r\n"
1202 "DLSR: %4.4f(sec)\r\n"
1203 "RTT: %llu(sec)\r\n",
1204 ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port),
1205 pt, (pt == 200) ? "Sender Report" : (pt == 201) ? "Receiver Report" : (pt == 192) ? "H.261 FUR" : "Unknown",
1208 (((long) ntohl(rtcpheader[i + 1]) & 0xff000000) >> 24),
1209 rtp->rtcp->reported_lost,
1210 (long) (ntohl(rtcpheader[i + 2]) & 0xffff),
1211 (long) (ntohl(rtcpheader[i + 2]) & 0xffff) >> 16,
1212 rtp->rtcp->reported_jitter,
1213 (unsigned long) ntohl(rtcpheader[i + 4]) >> 16, ((unsigned long) ntohl(rtcpheader[i + 4]) << 16) * 4096,
1214 ntohl(rtcpheader[i + 5])/65536.0,
1215 (unsigned long long)rtt);
1217 manager_event(EVENT_FLAG_REPORTING, "RTCPReceived", "From %s:%d\r\n"
1219 "ReceptionReports: %d\r\n"
1220 "SenderSSRC: %u\r\n"
1221 "FractionLost: %ld\r\n"
1222 "PacketsLost: %d\r\n"
1223 "HighestSequence: %ld\r\n"
1224 "SequenceNumberCycles: %ld\r\n"
1226 "LastSR: %lu.%010lu\r\n"
1227 "DLSR: %4.4f(sec)\r\n",
1228 ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port),
1229 pt, (pt == 200) ? "Sender Report" : (pt == 201) ? "Receiver Report" : (pt == 192) ? "H.261 FUR" : "Unknown",
1232 (((long) ntohl(rtcpheader[i + 1]) & 0xff000000) >> 24),
1233 rtp->rtcp->reported_lost,
1234 (long) (ntohl(rtcpheader[i + 2]) & 0xffff),
1235 (long) (ntohl(rtcpheader[i + 2]) & 0xffff) >> 16,
1236 rtp->rtcp->reported_jitter,
1237 (unsigned long) ntohl(rtcpheader[i + 4]) >> 16,
1238 ((unsigned long) ntohl(rtcpheader[i + 4]) << 16) * 4096,
1239 ntohl(rtcpheader[i + 5])/65536.0);
1243 if (rtcp_debug_test_addr(&sin))
1244 ast_verbose("Received an RTCP Fast Update Request\n");
1245 rtp->f.frametype = AST_FRAME_CONTROL;
1246 rtp->f.subclass = AST_CONTROL_VIDUPDATE;
1254 if (rtcp_debug_test_addr(&sin))
1255 ast_verbose("Received an SDES from %s:%d\n", ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
1258 if (rtcp_debug_test_addr(&sin))
1259 ast_verbose("Received a BYE from %s:%d\n", ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
1262 ast_debug(1, "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));
1265 position += (length + 1);
1271 static void calc_rxstamp(struct timeval *tv, struct ast_rtp *rtp, unsigned int timestamp, int mark)
1275 double current_time;
1280 if ((!rtp->rxcore.tv_sec && !rtp->rxcore.tv_usec) || mark) {
1281 gettimeofday(&rtp->rxcore, NULL);
1282 rtp->drxcore = (double) rtp->rxcore.tv_sec + (double) rtp->rxcore.tv_usec / 1000000;
1283 /* map timestamp to a real time */
1284 rtp->seedrxts = timestamp; /* Their RTP timestamp started with this */
1285 rtp->rxcore.tv_sec -= timestamp / 8000;
1286 rtp->rxcore.tv_usec -= (timestamp % 8000) * 125;
1287 /* Round to 0.1ms for nice, pretty timestamps */
1288 rtp->rxcore.tv_usec -= rtp->rxcore.tv_usec % 100;
1289 if (rtp->rxcore.tv_usec < 0) {
1290 /* Adjust appropriately if necessary */
1291 rtp->rxcore.tv_usec += 1000000;
1292 rtp->rxcore.tv_sec -= 1;
1296 gettimeofday(&now,NULL);
1297 /* rxcore is the mapping between the RTP timestamp and _our_ real time from gettimeofday() */
1298 tv->tv_sec = rtp->rxcore.tv_sec + timestamp / 8000;
1299 tv->tv_usec = rtp->rxcore.tv_usec + (timestamp % 8000) * 125;
1300 if (tv->tv_usec >= 1000000) {
1301 tv->tv_usec -= 1000000;
1304 prog = (double)((timestamp-rtp->seedrxts)/8000.);
1305 dtv = (double)rtp->drxcore + (double)(prog);
1306 current_time = (double)now.tv_sec + (double)now.tv_usec/1000000;
1307 transit = current_time - dtv;
1308 d = transit - rtp->rxtransit;
1309 rtp->rxtransit = transit;
1312 rtp->rxjitter += (1./16.) * (d - rtp->rxjitter);
1313 if (rtp->rtcp && rtp->rxjitter > rtp->rtcp->maxrxjitter)
1314 rtp->rtcp->maxrxjitter = rtp->rxjitter;
1315 if (rtp->rtcp && rtp->rxjitter < rtp->rtcp->minrxjitter)
1316 rtp->rtcp->minrxjitter = rtp->rxjitter;
1319 /*! \brief Perform a Packet2Packet RTP write */
1320 static int bridge_p2p_rtp_write(struct ast_rtp *rtp, struct ast_rtp *bridged, unsigned int *rtpheader, int len, int hdrlen)
1322 int res = 0, payload = 0, bridged_payload = 0, mark;
1323 struct rtpPayloadType rtpPT;
1324 int reconstruct = ntohl(rtpheader[0]);
1326 /* Get fields from packet */
1327 payload = (reconstruct & 0x7f0000) >> 16;
1328 mark = (((reconstruct & 0x800000) >> 23) != 0);
1330 /* Check what the payload value should be */
1331 rtpPT = ast_rtp_lookup_pt(rtp, payload);
1333 /* If the payload is DTMF, and we are listening for DTMF - then feed it into the core */
1334 if (ast_test_flag(rtp, FLAG_P2P_NEED_DTMF) && !rtpPT.isAstFormat && rtpPT.code == AST_RTP_DTMF)
1337 /* Otherwise adjust bridged payload to match */
1338 bridged_payload = ast_rtp_lookup_code(bridged, rtpPT.isAstFormat, rtpPT.code);
1340 /* If the mark bit has not been sent yet... do it now */
1341 if (!ast_test_flag(rtp, FLAG_P2P_SENT_MARK)) {
1343 ast_set_flag(rtp, FLAG_P2P_SENT_MARK);
1346 /* Reconstruct part of the packet */
1347 reconstruct &= 0xFF80FFFF;
1348 reconstruct |= (bridged_payload << 16);
1349 reconstruct |= (mark << 23);
1350 rtpheader[0] = htonl(reconstruct);
1352 /* Send the packet back out */
1353 res = sendto(bridged->s, (void *)rtpheader, len, 0, (struct sockaddr *)&bridged->them, sizeof(bridged->them));
1355 if (!bridged->nat || (bridged->nat && (ast_test_flag(bridged, FLAG_NAT_ACTIVE) == FLAG_NAT_ACTIVE))) {
1356 ast_debug(1, "RTP Transmission error of packet to %s:%d: %s\n", ast_inet_ntoa(bridged->them.sin_addr), ntohs(bridged->them.sin_port), strerror(errno));
1357 } else if (((ast_test_flag(bridged, FLAG_NAT_ACTIVE) == FLAG_NAT_INACTIVE) || rtpdebug) && !ast_test_flag(bridged, FLAG_NAT_INACTIVE_NOWARN)) {
1358 if (option_debug || rtpdebug)
1359 ast_debug(0, "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));
1360 ast_set_flag(bridged, FLAG_NAT_INACTIVE_NOWARN);
1363 } else if (rtp_debug_test_addr(&bridged->them))
1364 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);
1369 struct ast_frame *ast_rtp_read(struct ast_rtp *rtp)
1372 struct sockaddr_in sin;
1383 unsigned int timestamp;
1384 unsigned int *rtpheader;
1385 struct rtpPayloadType rtpPT;
1386 struct ast_rtp *bridged = NULL;
1388 /* If time is up, kill it */
1389 if (rtp->sending_digit)
1390 ast_rtp_senddigit_continuation(rtp);
1394 /* Cache where the header will go */
1395 res = recvfrom(rtp->s, rtp->rawdata + AST_FRIENDLY_OFFSET, sizeof(rtp->rawdata) - AST_FRIENDLY_OFFSET,
1396 0, (struct sockaddr *)&sin, &len);
1398 rtpheader = (unsigned int *)(rtp->rawdata + AST_FRIENDLY_OFFSET);
1402 if (errno != EAGAIN) {
1403 ast_log(LOG_WARNING, "RTP Read error: %s. Hanging up.\n", strerror(errno));
1406 return &ast_null_frame;
1410 ast_log(LOG_WARNING, "RTP Read too short\n");
1411 return &ast_null_frame;
1415 seqno = ntohl(rtpheader[0]);
1417 /* Check RTP version */
1418 version = (seqno & 0xC0000000) >> 30;
1420 /* If the two high bits are 0, this might be a
1421 * STUN message, so process it. stun_handle_packet()
1422 * answers to requests, and it returns STUN_ACCEPT
1423 * if the request is valid.
1425 if ((stun_handle_packet(rtp->s, &sin, rtp->rawdata + AST_FRIENDLY_OFFSET, res, NULL, NULL) == STUN_ACCEPT) &&
1426 (!rtp->them.sin_port && !rtp->them.sin_addr.s_addr)) {
1427 memcpy(&rtp->them, &sin, sizeof(rtp->them));
1429 return &ast_null_frame;
1432 #if 0 /* Allow to receive RTP stream with closed transmission path */
1433 /* If we don't have the other side's address, then ignore this */
1434 if (!rtp->them.sin_addr.s_addr || !rtp->them.sin_port)
1435 return &ast_null_frame;
1438 /* Send to whoever send to us if NAT is turned on */
1440 if ((rtp->them.sin_addr.s_addr != sin.sin_addr.s_addr) ||
1441 (rtp->them.sin_port != sin.sin_port)) {
1444 memcpy(&rtp->rtcp->them, &sin, sizeof(rtp->rtcp->them));
1445 rtp->rtcp->them.sin_port = htons(ntohs(rtp->them.sin_port)+1);
1448 ast_set_flag(rtp, FLAG_NAT_ACTIVE);
1449 if (option_debug || rtpdebug)
1450 ast_debug(0, "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));
1454 /* If we are bridged to another RTP stream, send direct */
1455 if ((bridged = ast_rtp_get_bridged(rtp)) && !bridge_p2p_rtp_write(rtp, bridged, rtpheader, res, hdrlen))
1456 return &ast_null_frame;
1459 return &ast_null_frame;
1461 payloadtype = (seqno & 0x7f0000) >> 16;
1462 padding = seqno & (1 << 29);
1463 mark = seqno & (1 << 23);
1464 ext = seqno & (1 << 28);
1465 cc = (seqno & 0xF000000) >> 24;
1467 timestamp = ntohl(rtpheader[1]);
1468 ssrc = ntohl(rtpheader[2]);
1470 if (!mark && rtp->rxssrc && rtp->rxssrc != ssrc) {
1471 if (option_debug || rtpdebug)
1472 ast_debug(0, "Forcing Marker bit, because SSRC has changed\n");
1479 /* Remove padding bytes */
1480 res -= rtp->rawdata[AST_FRIENDLY_OFFSET + res - 1];
1484 /* CSRC fields present */
1489 /* RTP Extension present */
1490 hdrlen += (ntohl(rtpheader[hdrlen/4]) & 0xffff) << 2;
1494 profile = (ntohl(rtpheader[3]) & 0xffff0000) >> 16;
1495 if (profile == 0x505a)
1496 ast_debug(1, "Found Zfone extension in RTP stream - zrtp - not supported.\n");
1498 ast_debug(1, "Found unknown RTP Extensions %x\n", profile);
1503 ast_log(LOG_WARNING, "RTP Read too short (%d, expecting %d)\n", res, hdrlen);
1504 return &ast_null_frame;
1507 rtp->rxcount++; /* Only count reasonably valid packets, this'll make the rtcp stats more accurate */
1509 if (rtp->rxcount==1) {
1510 /* This is the first RTP packet successfully received from source */
1511 rtp->seedrxseqno = seqno;
1514 /* Do not schedule RR if RTCP isn't run */
1515 if (rtp->rtcp && rtp->rtcp->them.sin_addr.s_addr && rtp->rtcp->schedid < 1) {
1516 /* Schedule transmission of Receiver Report */
1517 rtp->rtcp->schedid = ast_sched_add(rtp->sched, ast_rtcp_calc_interval(rtp), ast_rtcp_write, rtp);
1519 if ( (int)rtp->lastrxseqno - (int)seqno > 100) /* if so it would indicate that the sender cycled; allow for misordering */
1520 rtp->cycles += RTP_SEQ_MOD;
1522 rtp->lastrxseqno = seqno;
1524 if (rtp->themssrc==0)
1525 rtp->themssrc = ntohl(rtpheader[2]); /* Record their SSRC to put in future RR */
1527 if (rtp_debug_test_addr(&sin))
1528 ast_verbose("Got RTP packet from %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
1529 ast_inet_ntoa(sin.sin_addr), ntohs(sin.sin_port), payloadtype, seqno, timestamp,res - hdrlen);
1531 rtpPT = ast_rtp_lookup_pt(rtp, payloadtype);
1532 if (!rtpPT.isAstFormat) {
1533 struct ast_frame *f = NULL;
1535 /* This is special in-band data that's not one of our codecs */
1536 if (rtpPT.code == AST_RTP_DTMF) {
1537 /* It's special -- rfc2833 process it */
1538 if (rtp_debug_test_addr(&sin)) {
1539 unsigned char *data;
1541 unsigned int event_end;
1542 unsigned int duration;
1543 data = rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen;
1544 event = ntohl(*((unsigned int *)(data)));
1546 event_end = ntohl(*((unsigned int *)(data)));
1549 duration = ntohl(*((unsigned int *)(data)));
1551 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);
1553 f = process_rfc2833(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen, seqno, timestamp);
1554 } else if (rtpPT.code == AST_RTP_CISCO_DTMF) {
1555 /* It's really special -- process it the Cisco way */
1556 if (rtp->lastevent <= seqno || (rtp->lastevent >= 65530 && seqno <= 6)) {
1557 f = process_cisco_dtmf(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
1558 rtp->lastevent = seqno;
1560 } else if (rtpPT.code == AST_RTP_CN) {
1562 f = process_rfc3389(rtp, rtp->rawdata + AST_FRIENDLY_OFFSET + hdrlen, res - hdrlen);
1564 ast_log(LOG_NOTICE, "Unknown RTP codec %d received from '%s'\n", payloadtype, ast_inet_ntoa(rtp->them.sin_addr));
1566 return f ? f : &ast_null_frame;
1568 rtp->lastrxformat = rtp->f.subclass = rtpPT.code;
1569 rtp->f.frametype = (rtp->f.subclass < AST_FORMAT_MAX_AUDIO) ? AST_FRAME_VOICE : (rtp->f.subclass < AST_FORMAT_MAX_VIDEO) ? AST_FRAME_VIDEO : AST_FRAME_TEXT;
1572 rtp->lastrxts = timestamp;
1574 rtp->rxseqno = seqno;
1576 /* Record received timestamp as last received now */
1577 rtp->lastrxts = timestamp;
1580 rtp->f.datalen = res - hdrlen;
1581 rtp->f.data = rtp->rawdata + hdrlen + AST_FRIENDLY_OFFSET;
1582 rtp->f.offset = hdrlen + AST_FRIENDLY_OFFSET;
1583 rtp->f.seqno = seqno;
1584 if (rtp->f.subclass < AST_FORMAT_MAX_AUDIO) {
1585 rtp->f.samples = ast_codec_get_samples(&rtp->f);
1586 if (rtp->f.subclass == AST_FORMAT_SLINEAR)
1587 ast_frame_byteswap_be(&rtp->f);
1588 calc_rxstamp(&rtp->f.delivery, rtp, timestamp, mark);
1589 /* Add timing data to let ast_generic_bridge() put the frame into a jitterbuf */
1590 rtp->f.has_timing_info = 1;
1591 rtp->f.ts = timestamp / 8;
1592 rtp->f.len = rtp->f.samples / 8;
1593 } else if(rtp->f.subclass < AST_FORMAT_MAX_VIDEO) {
1594 /* Video -- samples is # of samples vs. 90000 */
1595 if (!rtp->lastividtimestamp)
1596 rtp->lastividtimestamp = timestamp;
1597 rtp->f.samples = timestamp - rtp->lastividtimestamp;
1598 rtp->lastividtimestamp = timestamp;
1599 rtp->f.delivery.tv_sec = 0;
1600 rtp->f.delivery.tv_usec = 0;
1602 rtp->f.subclass |= 0x1;
1604 /* TEXT -- samples is # of samples vs. 1000 */
1605 if (!rtp->lastitexttimestamp)
1606 rtp->lastitexttimestamp = timestamp;
1607 rtp->f.samples = timestamp - rtp->lastitexttimestamp;
1608 rtp->lastitexttimestamp = timestamp;
1609 rtp->f.delivery.tv_sec = 0;
1610 rtp->f.delivery.tv_usec = 0;
1616 /* The following array defines the MIME Media type (and subtype) for each
1617 of our codecs, or RTP-specific data type. */
1619 struct rtpPayloadType payloadType;
1623 {{1, AST_FORMAT_G723_1}, "audio", "G723"},
1624 {{1, AST_FORMAT_GSM}, "audio", "GSM"},
1625 {{1, AST_FORMAT_ULAW}, "audio", "PCMU"},
1626 {{1, AST_FORMAT_ALAW}, "audio", "PCMA"},
1627 {{1, AST_FORMAT_G726}, "audio", "G726-32"},
1628 {{1, AST_FORMAT_ADPCM}, "audio", "DVI4"},
1629 {{1, AST_FORMAT_SLINEAR}, "audio", "L16"},
1630 {{1, AST_FORMAT_LPC10}, "audio", "LPC"},
1631 {{1, AST_FORMAT_G729A}, "audio", "G729"},
1632 {{1, AST_FORMAT_SPEEX}, "audio", "speex"},
1633 {{1, AST_FORMAT_ILBC}, "audio", "iLBC"},
1634 {{1, AST_FORMAT_G722}, "audio", "G722"},
1635 {{1, AST_FORMAT_G726_AAL2}, "audio", "AAL2-G726-32"},
1636 {{0, AST_RTP_DTMF}, "audio", "telephone-event"},
1637 {{0, AST_RTP_CISCO_DTMF}, "audio", "cisco-telephone-event"},
1638 {{0, AST_RTP_CN}, "audio", "CN"},
1639 {{1, AST_FORMAT_JPEG}, "video", "JPEG"},
1640 {{1, AST_FORMAT_PNG}, "video", "PNG"},
1641 {{1, AST_FORMAT_H261}, "video", "H261"},
1642 {{1, AST_FORMAT_H263}, "video", "H263"},
1643 {{1, AST_FORMAT_H263_PLUS}, "video", "h263-1998"},
1644 {{1, AST_FORMAT_H264}, "video", "H264"},
1645 {{1, AST_FORMAT_MP4_VIDEO}, "video", "MP4V-ES"},
1646 {{1, AST_FORMAT_T140}, "text", "T140"},
1649 /* Static (i.e., well-known) RTP payload types for our "AST_FORMAT..."s:
1650 also, our own choices for dynamic payload types. This is our master
1651 table for transmission */
1652 static struct rtpPayloadType static_RTP_PT[MAX_RTP_PT] = {
1653 [0] = {1, AST_FORMAT_ULAW},
1654 #ifdef USE_DEPRECATED_G726
1655 [2] = {1, AST_FORMAT_G726}, /* Technically this is G.721, but if Cisco can do it, so can we... */
1657 [3] = {1, AST_FORMAT_GSM},
1658 [4] = {1, AST_FORMAT_G723_1},
1659 [5] = {1, AST_FORMAT_ADPCM}, /* 8 kHz */
1660 [6] = {1, AST_FORMAT_ADPCM}, /* 16 kHz */
1661 [7] = {1, AST_FORMAT_LPC10},
1662 [8] = {1, AST_FORMAT_ALAW},
1663 [9] = {1, AST_FORMAT_G722},
1664 [10] = {1, AST_FORMAT_SLINEAR}, /* 2 channels */
1665 [11] = {1, AST_FORMAT_SLINEAR}, /* 1 channel */
1666 [13] = {0, AST_RTP_CN},
1667 [16] = {1, AST_FORMAT_ADPCM}, /* 11.025 kHz */
1668 [17] = {1, AST_FORMAT_ADPCM}, /* 22.050 kHz */
1669 [18] = {1, AST_FORMAT_G729A},
1670 [19] = {0, AST_RTP_CN}, /* Also used for CN */
1671 [26] = {1, AST_FORMAT_JPEG},
1672 [31] = {1, AST_FORMAT_H261},
1673 [34] = {1, AST_FORMAT_H263},
1674 [97] = {1, AST_FORMAT_ILBC},
1675 [98] = {1, AST_FORMAT_H263_PLUS},
1676 [99] = {1, AST_FORMAT_H264},
1677 [101] = {0, AST_RTP_DTMF},
1678 [102] = {1, AST_FORMAT_T140}, /* Real time text chat */
1679 [103] = {1, AST_FORMAT_H263_PLUS},
1680 [104] = {1, AST_FORMAT_MP4_VIDEO},
1681 [110] = {1, AST_FORMAT_SPEEX},
1682 [111] = {1, AST_FORMAT_G726},
1683 [112] = {1, AST_FORMAT_G726_AAL2},
1684 [121] = {0, AST_RTP_CISCO_DTMF}, /* Must be type 121 */
1687 void ast_rtp_pt_clear(struct ast_rtp* rtp)
1694 rtp_bridge_lock(rtp);
1696 for (i = 0; i < MAX_RTP_PT; ++i) {
1697 rtp->current_RTP_PT[i].isAstFormat = 0;
1698 rtp->current_RTP_PT[i].code = 0;
1701 rtp->rtp_lookup_code_cache_isAstFormat = 0;
1702 rtp->rtp_lookup_code_cache_code = 0;
1703 rtp->rtp_lookup_code_cache_result = 0;
1705 rtp_bridge_unlock(rtp);
1708 void ast_rtp_pt_default(struct ast_rtp* rtp)
1712 rtp_bridge_lock(rtp);
1714 /* Initialize to default payload types */
1715 for (i = 0; i < MAX_RTP_PT; ++i) {
1716 rtp->current_RTP_PT[i].isAstFormat = static_RTP_PT[i].isAstFormat;
1717 rtp->current_RTP_PT[i].code = static_RTP_PT[i].code;
1720 rtp->rtp_lookup_code_cache_isAstFormat = 0;
1721 rtp->rtp_lookup_code_cache_code = 0;
1722 rtp->rtp_lookup_code_cache_result = 0;
1724 rtp_bridge_unlock(rtp);
1727 void ast_rtp_pt_copy(struct ast_rtp *dest, struct ast_rtp *src)
1731 rtp_bridge_lock(dest);
1732 rtp_bridge_lock(src);
1734 for (i=0; i < MAX_RTP_PT; ++i) {
1735 dest->current_RTP_PT[i].isAstFormat =
1736 src->current_RTP_PT[i].isAstFormat;
1737 dest->current_RTP_PT[i].code =
1738 src->current_RTP_PT[i].code;
1740 dest->rtp_lookup_code_cache_isAstFormat = 0;
1741 dest->rtp_lookup_code_cache_code = 0;
1742 dest->rtp_lookup_code_cache_result = 0;
1744 rtp_bridge_unlock(src);
1745 rtp_bridge_unlock(dest);
1748 /*! \brief Get channel driver interface structure */
1749 static struct ast_rtp_protocol *get_proto(struct ast_channel *chan)
1751 struct ast_rtp_protocol *cur = NULL;
1753 AST_RWLIST_RDLOCK(&protos);
1754 AST_RWLIST_TRAVERSE(&protos, cur, list) {
1755 if (cur->type == chan->tech->type)
1758 AST_RWLIST_UNLOCK(&protos);
1763 int ast_rtp_early_bridge(struct ast_channel *c0, struct ast_channel *c1)
1765 // dest = c0, src = c1
1766 struct ast_rtp *destp = NULL, *srcp = NULL; /* Audio RTP Channels */
1767 struct ast_rtp *vdestp = NULL, *vsrcp = NULL; /* Video RTP channels */
1768 struct ast_rtp *tdestp = NULL, *tsrcp = NULL; /* Text RTP channels */
1769 struct ast_rtp_protocol *destpr = NULL, *srcpr = NULL;
1770 enum ast_rtp_get_result audio_dest_res = AST_RTP_GET_FAILED, video_dest_res = AST_RTP_GET_FAILED, text_dest_res = AST_RTP_GET_FAILED;
1771 enum ast_rtp_get_result audio_src_res = AST_RTP_GET_FAILED, video_src_res = AST_RTP_GET_FAILED, text_src_res = AST_RTP_GET_FAILED;
1772 int srccodec, destcodec, nat_active = 0;
1775 ast_channel_lock(c0);
1777 while (ast_channel_trylock(c1)) {
1778 ast_channel_unlock(c0);
1780 ast_channel_lock(c0);
1784 /* Find channel driver interfaces */
1785 destpr = get_proto(c0);
1787 srcpr = get_proto(c1);
1789 ast_debug(1, "Channel '%s' has no RTP, not doing anything\n", c0->name);
1790 ast_channel_unlock(c0);
1792 ast_channel_unlock(c1);
1796 ast_debug(1, "Channel '%s' has no RTP, not doing anything\n", c1 ? c1->name : "<unspecified>");
1797 ast_channel_unlock(c0);
1799 ast_channel_unlock(c1);
1803 /* Get audio, video and text interface (if native bridge is possible) */
1804 audio_dest_res = destpr->get_rtp_info(c0, &destp);
1805 video_dest_res = destpr->get_vrtp_info ? destpr->get_vrtp_info(c0, &vdestp) : AST_RTP_GET_FAILED;
1806 text_dest_res = destpr->get_trtp_info ? destpr->get_trtp_info(c0, &tdestp) : AST_RTP_GET_FAILED;
1808 audio_src_res = srcpr->get_rtp_info(c1, &srcp);
1809 video_src_res = srcpr->get_vrtp_info ? srcpr->get_vrtp_info(c1, &vsrcp) : AST_RTP_GET_FAILED;
1810 text_src_res = srcpr->get_trtp_info ? srcpr->get_trtp_info(c1, &tsrcp) : AST_RTP_GET_FAILED;
1813 /* Check if bridge is still possible (In SIP canreinvite=no stops this, like NAT) */
1814 if (audio_dest_res != AST_RTP_TRY_NATIVE) {
1815 /* Somebody doesn't want to play... */
1816 ast_channel_unlock(c0);
1818 ast_channel_unlock(c1);
1821 if (audio_src_res == AST_RTP_TRY_NATIVE && srcpr->get_codec)
1822 srccodec = srcpr->get_codec(c1);
1825 if (audio_dest_res == AST_RTP_TRY_NATIVE && destpr->get_codec)
1826 destcodec = destpr->get_codec(c0);
1829 /* Ensure we have at least one matching codec */
1830 if (!(srccodec & destcodec)) {
1831 ast_channel_unlock(c0);
1833 ast_channel_unlock(c1);
1836 /* Consider empty media as non-existant */
1837 if (audio_src_res == AST_RTP_TRY_NATIVE && !srcp->them.sin_addr.s_addr)
1839 if (srcp && (srcp->nat || ast_test_flag(srcp, FLAG_NAT_ACTIVE)))
1841 /* Bridge media early */
1842 if (destpr->set_rtp_peer(c0, srcp, vsrcp, tsrcp, srccodec, nat_active))
1843 ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", c0->name, c1 ? c1->name : "<unspecified>");
1844 ast_channel_unlock(c0);
1846 ast_channel_unlock(c1);
1847 ast_debug(1, "Setting early bridge SDP of '%s' with that of '%s'\n", c0->name, c1 ? c1->name : "<unspecified>");
1851 int ast_rtp_make_compatible(struct ast_channel *dest, struct ast_channel *src, int media)
1853 struct ast_rtp *destp = NULL, *srcp = NULL; /* Audio RTP Channels */
1854 struct ast_rtp *vdestp = NULL, *vsrcp = NULL; /* Video RTP channels */
1855 struct ast_rtp *tdestp = NULL, *tsrcp = NULL; /* Text RTP channels */
1856 struct ast_rtp_protocol *destpr = NULL, *srcpr = NULL;
1857 enum ast_rtp_get_result audio_dest_res = AST_RTP_GET_FAILED, video_dest_res = AST_RTP_GET_FAILED, text_dest_res = AST_RTP_GET_FAILED;
1858 enum ast_rtp_get_result audio_src_res = AST_RTP_GET_FAILED, video_src_res = AST_RTP_GET_FAILED, text_src_res = AST_RTP_GET_FAILED;
1859 int srccodec, destcodec;
1862 ast_channel_lock(dest);
1863 while (ast_channel_trylock(src)) {
1864 ast_channel_unlock(dest);
1866 ast_channel_lock(dest);
1869 /* Find channel driver interfaces */
1870 if (!(destpr = get_proto(dest))) {
1871 ast_debug(1, "Channel '%s' has no RTP, not doing anything\n", dest->name);
1872 ast_channel_unlock(dest);
1873 ast_channel_unlock(src);
1876 if (!(srcpr = get_proto(src))) {
1877 ast_debug(1, "Channel '%s' has no RTP, not doing anything\n", src->name);
1878 ast_channel_unlock(dest);
1879 ast_channel_unlock(src);
1883 /* Get audio and video interface (if native bridge is possible) */
1884 audio_dest_res = destpr->get_rtp_info(dest, &destp);
1885 video_dest_res = destpr->get_vrtp_info ? destpr->get_vrtp_info(dest, &vdestp) : AST_RTP_GET_FAILED;
1886 text_dest_res = destpr->get_trtp_info ? destpr->get_trtp_info(dest, &tdestp) : AST_RTP_GET_FAILED;
1887 audio_src_res = srcpr->get_rtp_info(src, &srcp);
1888 video_src_res = srcpr->get_vrtp_info ? srcpr->get_vrtp_info(src, &vsrcp) : AST_RTP_GET_FAILED;
1889 text_src_res = srcpr->get_trtp_info ? srcpr->get_trtp_info(src, &tsrcp) : AST_RTP_GET_FAILED;
1891 /* Ensure we have at least one matching codec */
1892 if (srcpr->get_codec)
1893 srccodec = srcpr->get_codec(src);
1896 if (destpr->get_codec)
1897 destcodec = destpr->get_codec(dest);
1901 /* Check if bridge is still possible (In SIP canreinvite=no stops this, like NAT) */
1902 if (audio_dest_res != AST_RTP_TRY_NATIVE || audio_src_res != AST_RTP_TRY_NATIVE || !(srccodec & destcodec)) {
1903 /* Somebody doesn't want to play... */
1904 ast_channel_unlock(dest);
1905 ast_channel_unlock(src);
1908 ast_rtp_pt_copy(destp, srcp);
1909 if (vdestp && vsrcp)
1910 ast_rtp_pt_copy(vdestp, vsrcp);
1911 if (tdestp && tsrcp)
1912 ast_rtp_pt_copy(tdestp, tsrcp);
1915 if (destpr->set_rtp_peer(dest, srcp, vsrcp, tsrcp, srccodec, ast_test_flag(srcp, FLAG_NAT_ACTIVE)))
1916 ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", dest->name, src->name);
1918 ast_channel_unlock(dest);
1919 ast_channel_unlock(src);
1920 ast_debug(1, "Seeded SDP of '%s' with that of '%s'\n", dest->name, src->name);
1924 /*! \brief Make a note of a RTP payload type that was seen in a SDP "m=" line.
1925 * By default, use the well-known value for this type (although it may
1926 * still be set to a different value by a subsequent "a=rtpmap:" line)
1928 void ast_rtp_set_m_type(struct ast_rtp* rtp, int pt)
1930 if (pt < 0 || pt > MAX_RTP_PT || static_RTP_PT[pt].code == 0)
1931 return; /* bogus payload type */
1933 rtp_bridge_lock(rtp);
1934 rtp->current_RTP_PT[pt] = static_RTP_PT[pt];
1935 rtp_bridge_unlock(rtp);
1938 /*! \brief Make a note of a RTP payload type (with MIME type) that was seen in
1939 * an SDP "a=rtpmap:" line.
1941 void ast_rtp_set_rtpmap_type(struct ast_rtp *rtp, int pt,
1942 char *mimeType, char *mimeSubtype,
1943 enum ast_rtp_options options)
1947 if (pt < 0 || pt > MAX_RTP_PT)
1948 return; /* bogus payload type */
1950 rtp_bridge_lock(rtp);
1952 for (i = 0; i < sizeof(mimeTypes)/sizeof(mimeTypes[0]); ++i) {
1953 if (strcasecmp(mimeSubtype, mimeTypes[i].subtype) == 0 &&
1954 strcasecmp(mimeType, mimeTypes[i].type) == 0) {
1955 rtp->current_RTP_PT[pt] = mimeTypes[i].payloadType;
1956 if ((mimeTypes[i].payloadType.code == AST_FORMAT_G726) &&
1957 mimeTypes[i].payloadType.isAstFormat &&
1958 (options & AST_RTP_OPT_G726_NONSTANDARD))
1959 rtp->current_RTP_PT[pt].code = AST_FORMAT_G726_AAL2;
1964 rtp_bridge_unlock(rtp);
1969 /*! \brief Return the union of all of the codecs that were set by rtp_set...() calls
1970 * They're returned as two distinct sets: AST_FORMATs, and AST_RTPs */
1971 void ast_rtp_get_current_formats(struct ast_rtp* rtp,
1972 int* astFormats, int* nonAstFormats)
1976 rtp_bridge_lock(rtp);
1978 *astFormats = *nonAstFormats = 0;
1979 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
1980 if (rtp->current_RTP_PT[pt].isAstFormat) {
1981 *astFormats |= rtp->current_RTP_PT[pt].code;
1983 *nonAstFormats |= rtp->current_RTP_PT[pt].code;
1987 rtp_bridge_unlock(rtp);
1992 struct rtpPayloadType ast_rtp_lookup_pt(struct ast_rtp* rtp, int pt)
1994 struct rtpPayloadType result;
1996 result.isAstFormat = result.code = 0;
1998 if (pt < 0 || pt > MAX_RTP_PT)
1999 return result; /* bogus payload type */
2001 /* Start with negotiated codecs */
2002 rtp_bridge_lock(rtp);
2003 result = rtp->current_RTP_PT[pt];
2004 rtp_bridge_unlock(rtp);
2006 /* If it doesn't exist, check our static RTP type list, just in case */
2008 result = static_RTP_PT[pt];
2013 /*! \brief Looks up an RTP code out of our *static* outbound list */
2014 int ast_rtp_lookup_code(struct ast_rtp* rtp, const int isAstFormat, const int code)
2018 rtp_bridge_lock(rtp);
2020 if (isAstFormat == rtp->rtp_lookup_code_cache_isAstFormat &&
2021 code == rtp->rtp_lookup_code_cache_code) {
2022 /* Use our cached mapping, to avoid the overhead of the loop below */
2023 pt = rtp->rtp_lookup_code_cache_result;
2024 rtp_bridge_unlock(rtp);
2028 /* Check the dynamic list first */
2029 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
2030 if (rtp->current_RTP_PT[pt].code == code && rtp->current_RTP_PT[pt].isAstFormat == isAstFormat) {
2031 rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
2032 rtp->rtp_lookup_code_cache_code = code;
2033 rtp->rtp_lookup_code_cache_result = pt;
2034 rtp_bridge_unlock(rtp);
2039 /* Then the static list */
2040 for (pt = 0; pt < MAX_RTP_PT; ++pt) {
2041 if (static_RTP_PT[pt].code == code && static_RTP_PT[pt].isAstFormat == isAstFormat) {
2042 rtp->rtp_lookup_code_cache_isAstFormat = isAstFormat;
2043 rtp->rtp_lookup_code_cache_code = code;
2044 rtp->rtp_lookup_code_cache_result = pt;
2045 rtp_bridge_unlock(rtp);
2050 rtp_bridge_unlock(rtp);
2055 const char *ast_rtp_lookup_mime_subtype(const int isAstFormat, const int code,
2056 enum ast_rtp_options options)
2060 for (i = 0; i < sizeof(mimeTypes)/sizeof(mimeTypes[0]); ++i) {
2061 if ((mimeTypes[i].payloadType.code == code) && (mimeTypes[i].payloadType.isAstFormat == isAstFormat)) {
2063 (code == AST_FORMAT_G726_AAL2) &&
2064 (options & AST_RTP_OPT_G726_NONSTANDARD))
2067 return mimeTypes[i].subtype;
2074 char *ast_rtp_lookup_mime_multiple(char *buf, size_t size, const int capability,
2075 const int isAstFormat, enum ast_rtp_options options)
2085 snprintf(end, size, "0x%x (", capability);
2092 for (format = 1; format < AST_RTP_MAX; format <<= 1) {
2093 if (capability & format) {
2094 const char *name = ast_rtp_lookup_mime_subtype(isAstFormat, format, options);
2096 snprintf(end, size, "%s|", name);
2104 snprintf(start, size, "nothing)");
2111 /*! \brief Open RTP or RTCP socket for a session.
2112 * Print a message on failure.
2114 static int rtp_socket(const char *type)
2116 int s = socket(AF_INET, SOCK_DGRAM, 0);
2120 ast_log(LOG_WARNING, "Unable to allocate %s socket: %s\n", type, strerror(errno));
2122 long flags = fcntl(s, F_GETFL);
2123 fcntl(s, F_SETFL, flags | O_NONBLOCK);
2126 setsockopt(s, SOL_SOCKET, SO_NO_CHECK, &nochecksums, sizeof(nochecksums));
2133 * \brief Initialize a new RTCP session.
2135 * \returns The newly initialized RTCP session.
2137 static struct ast_rtcp *ast_rtcp_new(void)
2139 struct ast_rtcp *rtcp;
2141 if (!(rtcp = ast_calloc(1, sizeof(*rtcp))))
2143 rtcp->s = rtp_socket("RTCP");
2144 rtcp->us.sin_family = AF_INET;
2145 rtcp->them.sin_family = AF_INET;
2156 * \brief Initialize a new RTP structure.
2159 void ast_rtp_new_init(struct ast_rtp *rtp)
2162 ast_mutex_init(&rtp->bridge_lock);
2165 rtp->them.sin_family = AF_INET;
2166 rtp->us.sin_family = AF_INET;
2167 rtp->ssrc = ast_random();
2168 rtp->seqno = ast_random() & 0xffff;
2169 ast_set_flag(rtp, FLAG_HAS_DTMF);
2174 struct ast_rtp *ast_rtp_new_with_bindaddr(struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode, struct in_addr addr)
2176 struct ast_rtp *rtp;
2180 if (!(rtp = ast_calloc(1, sizeof(*rtp))))
2183 ast_rtp_new_init(rtp);
2185 rtp->s = rtp_socket("RTP");
2188 if (sched && rtcpenable) {
2190 rtp->rtcp = ast_rtcp_new();
2194 * Try to bind the RTP port, x, and possibly the RTCP port, x+1 as well.
2195 * Start from a random (even, by RTP spec) port number, and
2196 * iterate until success or no ports are available.
2197 * Note that the requirement of RTP port being even, or RTCP being the
2198 * next one, cannot be enforced in presence of a NAT box because the
2199 * mapping is not under our control.
2201 x = (ast_random() % (rtpend-rtpstart)) + rtpstart;
2202 x = x & ~1; /* make it an even number */
2203 startplace = x; /* remember the starting point */
2204 /* this is constant across the loop */
2205 rtp->us.sin_addr = addr;
2207 rtp->rtcp->us.sin_addr = addr;
2209 rtp->us.sin_port = htons(x);
2210 if (!bind(rtp->s, (struct sockaddr *)&rtp->us, sizeof(rtp->us))) {
2211 /* bind succeeded, if no rtcp then we are done */
2214 /* have rtcp, try to bind it */
2215 rtp->rtcp->us.sin_port = htons(x + 1);
2216 if (!bind(rtp->rtcp->s, (struct sockaddr *)&rtp->rtcp->us, sizeof(rtp->rtcp->us)))
2217 break; /* success again, we are really done */
2219 * RTCP bind failed, so close and recreate the
2220 * already bound RTP socket for the next round.
2223 rtp->s = rtp_socket("RTP");
2228 * If we get here, there was an error in one of the bind()
2229 * calls, so make sure it is nothing unexpected.
2231 if (errno != EADDRINUSE) {
2232 /* We got an error that wasn't expected, abort! */
2233 ast_log(LOG_ERROR, "Unexpected bind error: %s\n", strerror(errno));
2237 * One of the ports is in use. For the next iteration,
2238 * increment by two and handle wraparound.
2239 * If we reach the starting point, then declare failure.
2243 x = (rtpstart + 1) & ~1;
2244 if (x == startplace) {
2245 ast_log(LOG_ERROR, "No RTP ports remaining. Can't setup media stream for this call.\n");
2252 rtp->ioid = ast_io_add(rtp->io, rtp->s, rtpread, AST_IO_IN, rtp);
2253 ast_set_flag(rtp, FLAG_CALLBACK_MODE);
2255 ast_rtp_pt_default(rtp);
2262 close(rtp->rtcp->s);
2263 ast_free(rtp->rtcp);
2269 struct ast_rtp *ast_rtp_new(struct sched_context *sched, struct io_context *io, int rtcpenable, int callbackmode)
2273 memset(&ia, 0, sizeof(ia));
2274 return ast_rtp_new_with_bindaddr(sched, io, rtcpenable, callbackmode, ia);
2277 int ast_rtp_setqos(struct ast_rtp *rtp, int tos, int cos)
2279 return ast_netsock_set_qos(rtp->s, tos, cos);
2282 void ast_rtp_set_peer(struct ast_rtp *rtp, struct sockaddr_in *them)
2284 rtp->them.sin_port = them->sin_port;
2285 rtp->them.sin_addr = them->sin_addr;
2287 rtp->rtcp->them.sin_port = htons(ntohs(them->sin_port) + 1);
2288 rtp->rtcp->them.sin_addr = them->sin_addr;
2293 int ast_rtp_get_peer(struct ast_rtp *rtp, struct sockaddr_in *them)
2295 if ((them->sin_family != AF_INET) ||
2296 (them->sin_port != rtp->them.sin_port) ||
2297 (them->sin_addr.s_addr != rtp->them.sin_addr.s_addr)) {
2298 them->sin_family = AF_INET;
2299 them->sin_port = rtp->them.sin_port;
2300 them->sin_addr = rtp->them.sin_addr;
2306 void ast_rtp_get_us(struct ast_rtp *rtp, struct sockaddr_in *us)
2311 struct ast_rtp *ast_rtp_get_bridged(struct ast_rtp *rtp)
2313 struct ast_rtp *bridged = NULL;
2315 rtp_bridge_lock(rtp);
2316 bridged = rtp->bridged;
2317 rtp_bridge_unlock(rtp);
2322 void ast_rtp_stop(struct ast_rtp *rtp)
2324 if (rtp->rtcp && rtp->rtcp->schedid > 0) {
2325 ast_sched_del(rtp->sched, rtp->rtcp->schedid);
2326 rtp->rtcp->schedid = -1;
2329 memset(&rtp->them.sin_addr, 0, sizeof(rtp->them.sin_addr));
2330 memset(&rtp->them.sin_port, 0, sizeof(rtp->them.sin_port));
2332 memset(&rtp->rtcp->them.sin_addr, 0, sizeof(rtp->rtcp->them.sin_addr));
2333 memset(&rtp->rtcp->them.sin_port, 0, sizeof(rtp->rtcp->them.sin_port));
2336 ast_clear_flag(rtp, FLAG_P2P_SENT_MARK);
2339 void ast_rtp_reset(struct ast_rtp *rtp)
2341 memset(&rtp->rxcore, 0, sizeof(rtp->rxcore));
2342 memset(&rtp->txcore, 0, sizeof(rtp->txcore));
2343 memset(&rtp->dtmfmute, 0, sizeof(rtp->dtmfmute));
2345 rtp->lastdigitts = 0;
2347 rtp->lastividtimestamp = 0;
2348 rtp->lastovidtimestamp = 0;
2349 rtp->lastitexttimestamp = 0;
2350 rtp->lastotexttimestamp = 0;
2351 rtp->lasteventseqn = 0;
2353 rtp->lasttxformat = 0;
2354 rtp->lastrxformat = 0;
2356 rtp->dtmfsamples = 0;
2361 char *ast_rtp_get_quality(struct ast_rtp *rtp, struct ast_rtp_quality *qual)
2365 *themssrc their ssrc
2367 *rxjitter our calculated jitter(rx)
2368 *rxcount no. received packets
2369 *txjitter reported jitter of the other end
2370 *txcount transmitted packets
2371 *rlp remote lost packets
2372 *rtt round trip time
2376 qual->local_ssrc = rtp->ssrc;
2377 qual->local_lostpackets = rtp->rtcp->expected_prior - rtp->rtcp->received_prior;
2378 qual->local_jitter = rtp->rxjitter;
2379 qual->local_count = rtp->rxcount;
2380 qual->remote_ssrc = rtp->themssrc;
2381 qual->remote_lostpackets = rtp->rtcp->reported_lost;
2382 qual->remote_jitter = rtp->rtcp->reported_jitter / 65536.0;
2383 qual->remote_count = rtp->txcount;
2384 qual->rtt = rtp->rtcp->rtt;
2386 snprintf(rtp->rtcp->quality, sizeof(rtp->rtcp->quality), "ssrc=%u;themssrc=%u;lp=%u;rxjitter=%f;rxcount=%u;txjitter=%f;txcount=%u;rlp=%u;rtt=%f", rtp->ssrc, rtp->themssrc, rtp->rtcp->expected_prior - rtp->rtcp->received_prior, rtp->rxjitter, rtp->rxcount, (double)rtp->rtcp->reported_jitter/65536., rtp->txcount, rtp->rtcp->reported_lost, rtp->rtcp->rtt);
2388 return rtp->rtcp->quality;
2391 void ast_rtp_destroy(struct ast_rtp *rtp)
2393 if (rtcp_debug_test_addr(&rtp->them) || rtcpstats) {
2394 /*Print some info on the call here */
2395 ast_verbose(" RTP-stats\n");
2396 ast_verbose("* Our Receiver:\n");
2397 ast_verbose(" SSRC: %u\n", rtp->themssrc);
2398 ast_verbose(" Received packets: %u\n", rtp->rxcount);
2399 ast_verbose(" Lost packets: %u\n", rtp->rtcp->expected_prior - rtp->rtcp->received_prior);
2400 ast_verbose(" Jitter: %.4f\n", rtp->rxjitter);
2401 ast_verbose(" Transit: %.4f\n", rtp->rxtransit);
2402 ast_verbose(" RR-count: %u\n", rtp->rtcp->rr_count);
2403 ast_verbose("* Our Sender:\n");
2404 ast_verbose(" SSRC: %u\n", rtp->ssrc);
2405 ast_verbose(" Sent packets: %u\n", rtp->txcount);
2406 ast_verbose(" Lost packets: %u\n", rtp->rtcp->reported_lost);
2407 ast_verbose(" Jitter: %u\n", rtp->rtcp->reported_jitter);
2408 ast_verbose(" SR-count: %u\n", rtp->rtcp->sr_count);
2409 ast_verbose(" RTT: %f\n", rtp->rtcp->rtt);
2412 manager_event(EVENT_FLAG_REPORTING, "RTPReceiverStat", "SSRC: %u\r\n"
2413 "ReceivedPackets: %u\r\n"
2414 "LostPackets: %u\r\n"
2420 rtp->rtcp->expected_prior - rtp->rtcp->received_prior,
2423 rtp->rtcp->rr_count);
2424 manager_event(EVENT_FLAG_REPORTING, "RTPSenderStat", "SSRC: %u\r\n"
2425 "SentPackets: %u\r\n"
2426 "LostPackets: %u\r\n"
2432 rtp->rtcp->reported_lost,
2433 rtp->rtcp->reported_jitter,
2434 rtp->rtcp->sr_count,
2437 ast_smoother_free(rtp->smoother);
2439 ast_io_remove(rtp->io, rtp->ioid);
2443 if (rtp->rtcp->schedid > 0)
2444 ast_sched_del(rtp->sched, rtp->rtcp->schedid);
2445 close(rtp->rtcp->s);
2446 ast_free(rtp->rtcp);
2450 ast_mutex_destroy(&rtp->bridge_lock);
2455 static unsigned int calc_txstamp(struct ast_rtp *rtp, struct timeval *delivery)
2459 if (ast_tvzero(rtp->txcore)) {
2460 rtp->txcore = ast_tvnow();
2461 /* Round to 20ms for nice, pretty timestamps */
2462 rtp->txcore.tv_usec -= rtp->txcore.tv_usec % 20000;
2464 /* Use previous txcore if available */
2465 t = (delivery && !ast_tvzero(*delivery)) ? *delivery : ast_tvnow();
2466 ms = ast_tvdiff_ms(t, rtp->txcore);
2469 /* Use what we just got for next time */
2471 return (unsigned int) ms;
2474 /*! \brief Send begin frames for DTMF */
2475 int ast_rtp_senddigit_begin(struct ast_rtp *rtp, char digit)
2477 unsigned int *rtpheader;
2478 int hdrlen = 12, res = 0, i = 0, payload = 0;
2481 if ((digit <= '9') && (digit >= '0'))
2483 else if (digit == '*')
2485 else if (digit == '#')
2487 else if ((digit >= 'A') && (digit <= 'D'))
2488 digit = digit - 'A' + 12;
2489 else if ((digit >= 'a') && (digit <= 'd'))
2490 digit = digit - 'a' + 12;
2492 ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
2496 /* If we have no peer, return immediately */
2497 if (!rtp->them.sin_addr.s_addr || !rtp->them.sin_port)
2500 payload = ast_rtp_lookup_code(rtp, 0, AST_RTP_DTMF);
2502 rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
2503 rtp->send_duration = 160;
2505 /* Get a pointer to the header */
2506 rtpheader = (unsigned int *)data;
2507 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno));
2508 rtpheader[1] = htonl(rtp->lastdigitts);
2509 rtpheader[2] = htonl(rtp->ssrc);
2511 for (i = 0; i < 2; i++) {
2512 rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration));
2513 res = sendto(rtp->s, (void *) rtpheader, hdrlen + 4, 0, (struct sockaddr *) &rtp->them, sizeof(rtp->them));
2515 ast_log(LOG_ERROR, "RTP Transmission error to %s:%u: %s\n",
2516 ast_inet_ntoa(rtp->them.sin_addr),
2517 ntohs(rtp->them.sin_port), strerror(errno));
2518 if (rtp_debug_test_addr(&rtp->them))
2519 ast_verbose("Sent RTP DTMF packet to %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
2520 ast_inet_ntoa(rtp->them.sin_addr),
2521 ntohs(rtp->them.sin_port), payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
2522 /* Increment sequence number */
2524 /* Increment duration */
2525 rtp->send_duration += 160;
2526 /* Clear marker bit and set seqno */
2527 rtpheader[0] = htonl((2 << 30) | (payload << 16) | (rtp->seqno));
2530 /* Since we received a begin, we can safely store the digit and disable any compensation */
2531 rtp->sending_digit = 1;
2532 rtp->send_digit = digit;
2533 rtp->send_payload = payload;
2538 /*! \brief Send continuation frame for DTMF */
2539 static int ast_rtp_senddigit_continuation(struct ast_rtp *rtp)
2541 unsigned int *rtpheader;
2542 int hdrlen = 12, res = 0;
2545 if (!rtp->them.sin_addr.s_addr || !rtp->them.sin_port)
2548 /* Setup packet to send */
2549 rtpheader = (unsigned int *)data;
2550 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (rtp->send_payload << 16) | (rtp->seqno));
2551 rtpheader[1] = htonl(rtp->lastdigitts);
2552 rtpheader[2] = htonl(rtp->ssrc);
2553 rtpheader[3] = htonl((rtp->send_digit << 24) | (0xa << 16) | (rtp->send_duration));
2554 rtpheader[0] = htonl((2 << 30) | (rtp->send_payload << 16) | (rtp->seqno));
2557 res = sendto(rtp->s, (void *) rtpheader, hdrlen + 4, 0, (struct sockaddr *) &rtp->them, sizeof(rtp->them));
2559 ast_log(LOG_ERROR, "RTP Transmission error to %s:%d: %s\n",
2560 ast_inet_ntoa(rtp->them.sin_addr),
2561 ntohs(rtp->them.sin_port), strerror(errno));
2562 if (rtp_debug_test_addr(&rtp->them))
2563 ast_verbose("Sent RTP DTMF packet to %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
2564 ast_inet_ntoa(rtp->them.sin_addr),
2565 ntohs(rtp->them.sin_port), rtp->send_payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
2567 /* Increment sequence number */
2569 /* Increment duration */
2570 rtp->send_duration += 160;
2575 /*! \brief Send end packets for DTMF */
2576 int ast_rtp_senddigit_end(struct ast_rtp *rtp, char digit)
2578 unsigned int *rtpheader;
2579 int hdrlen = 12, res = 0, i = 0;
2582 /* If no address, then bail out */
2583 if (!rtp->them.sin_addr.s_addr || !rtp->them.sin_port)
2586 if ((digit <= '9') && (digit >= '0'))
2588 else if (digit == '*')
2590 else if (digit == '#')
2592 else if ((digit >= 'A') && (digit <= 'D'))
2593 digit = digit - 'A' + 12;
2594 else if ((digit >= 'a') && (digit <= 'd'))
2595 digit = digit - 'a' + 12;
2597 ast_log(LOG_WARNING, "Don't know how to represent '%c'\n", digit);
2601 rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
2603 rtpheader = (unsigned int *)data;
2604 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (rtp->send_payload << 16) | (rtp->seqno));
2605 rtpheader[1] = htonl(rtp->lastdigitts);
2606 rtpheader[2] = htonl(rtp->ssrc);
2607 rtpheader[3] = htonl((digit << 24) | (0xa << 16) | (rtp->send_duration));
2609 rtpheader[3] |= htonl((1 << 23));
2610 rtpheader[0] = htonl((2 << 30) | (rtp->send_payload << 16) | (rtp->seqno));
2611 /* Send 3 termination packets */
2612 for (i = 0; i < 3; i++) {
2613 res = sendto(rtp->s, (void *) rtpheader, hdrlen + 4, 0, (struct sockaddr *) &rtp->them, sizeof(rtp->them));
2615 ast_log(LOG_ERROR, "RTP Transmission error to %s:%d: %s\n",
2616 ast_inet_ntoa(rtp->them.sin_addr),
2617 ntohs(rtp->them.sin_port), strerror(errno));
2618 if (rtp_debug_test_addr(&rtp->them))
2619 ast_verbose("Sent RTP DTMF packet to %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
2620 ast_inet_ntoa(rtp->them.sin_addr),
2621 ntohs(rtp->them.sin_port), rtp->send_payload, rtp->seqno, rtp->lastdigitts, res - hdrlen);
2623 rtp->sending_digit = 0;
2624 rtp->send_digit = 0;
2625 /* Increment lastdigitts */
2626 rtp->lastdigitts += 960;
2632 /*! \brief Public function: Send an H.261 fast update request, some devices need this rather than SIP XML */
2633 int ast_rtcp_send_h261fur(void *data)
2635 struct ast_rtp *rtp = data;
2638 rtp->rtcp->sendfur = 1;
2639 res = ast_rtcp_write(data);
2644 /*! \brief Send RTCP sender's report */
2645 static int ast_rtcp_write_sr(void *data)
2647 struct ast_rtp *rtp = data;
2651 unsigned int now_lsw;
2652 unsigned int now_msw;
2653 unsigned int *rtcpheader;
2655 unsigned int extended;
2656 unsigned int expected;
2657 unsigned int expected_interval;
2658 unsigned int received_interval;
2661 struct timeval dlsr;
2664 /* Commented condition is always not NULL if rtp->rtcp is not NULL */
2665 if (!rtp || !rtp->rtcp/* || (&rtp->rtcp->them.sin_addr == 0)*/)
2668 if (!rtp->rtcp->them.sin_addr.s_addr) { /* This'll stop rtcp for this rtp session */
2669 ast_verbose("RTCP SR transmission error, rtcp halted\n");
2670 if (rtp->rtcp->schedid > 0)
2671 ast_sched_del(rtp->sched, rtp->rtcp->schedid);
2672 rtp->rtcp->schedid = -1;
2676 gettimeofday(&now, NULL);
2677 timeval2ntp(now, &now_msw, &now_lsw); /* fill thses ones in from utils.c*/
2678 rtcpheader = (unsigned int *)bdata;
2679 rtcpheader[1] = htonl(rtp->ssrc); /* Our SSRC */
2680 rtcpheader[2] = htonl(now_msw); /* now, MSW. gettimeofday() + SEC_BETWEEN_1900_AND_1970*/
2681 rtcpheader[3] = htonl(now_lsw); /* now, LSW */
2682 rtcpheader[4] = htonl(rtp->lastts); /* FIXME shouldn't be that, it should be now */
2683 rtcpheader[5] = htonl(rtp->txcount); /* No. packets sent */
2684 rtcpheader[6] = htonl(rtp->txoctetcount); /* No. bytes sent */
2687 extended = rtp->cycles + rtp->lastrxseqno;
2688 expected = extended - rtp->seedrxseqno + 1;
2689 if (rtp->rxcount > expected)
2690 expected += rtp->rxcount - expected;
2691 lost = expected - rtp->rxcount;
2692 expected_interval = expected - rtp->rtcp->expected_prior;
2693 rtp->rtcp->expected_prior = expected;
2694 received_interval = rtp->rxcount - rtp->rtcp->received_prior;
2695 rtp->rtcp->received_prior = rtp->rxcount;
2696 lost_interval = expected_interval - received_interval;
2697 if (expected_interval == 0 || lost_interval <= 0)
2700 fraction = (lost_interval << 8) / expected_interval;
2701 timersub(&now, &rtp->rtcp->rxlsr, &dlsr);
2702 rtcpheader[7] = htonl(rtp->themssrc);
2703 rtcpheader[8] = htonl(((fraction & 0xff) << 24) | (lost & 0xffffff));
2704 rtcpheader[9] = htonl((rtp->cycles) | ((rtp->lastrxseqno & 0xffff)));
2705 rtcpheader[10] = htonl((unsigned int)(rtp->rxjitter * 65536.));
2706 rtcpheader[11] = htonl(rtp->rtcp->themrxlsr);
2707 rtcpheader[12] = htonl((((dlsr.tv_sec * 1000) + (dlsr.tv_usec / 1000)) * 65536) / 1000);
2710 rtcpheader[0] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SR << 16) | ((len/4)-1));
2712 if (rtp->rtcp->sendfur) {
2713 rtcpheader[13] = htonl((2 << 30) | (0 << 24) | (RTCP_PT_FUR << 16) | 1);
2714 rtcpheader[14] = htonl(rtp->ssrc); /* Our SSRC */
2716 rtp->rtcp->sendfur = 0;
2719 /* Insert SDES here. Probably should make SDES text equal to mimetypes[code].type (not subtype 'cos */
2720 /* it can change mid call, and SDES can't) */
2721 rtcpheader[len/4] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SDES << 16) | 2);
2722 rtcpheader[(len/4)+1] = htonl(rtp->ssrc); /* Our SSRC */
2723 rtcpheader[(len/4)+2] = htonl(0x01 << 24); /* Empty for the moment */
2726 res = sendto(rtp->rtcp->s, (unsigned int *)rtcpheader, len, 0, (struct sockaddr *)&rtp->rtcp->them, sizeof(rtp->rtcp->them));
2728 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));
2729 if (rtp->rtcp->schedid > 0)
2730 ast_sched_del(rtp->sched, rtp->rtcp->schedid);
2731 rtp->rtcp->schedid = -1;
2735 /* FIXME Don't need to get a new one */
2736 gettimeofday(&rtp->rtcp->txlsr, NULL);
2737 rtp->rtcp->sr_count++;
2739 rtp->rtcp->lastsrtxcount = rtp->txcount;
2741 if (rtcp_debug_test_addr(&rtp->rtcp->them)) {
2742 ast_verbose("* Sent RTCP SR to %s:%d\n", ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port));
2743 ast_verbose(" Our SSRC: %u\n", rtp->ssrc);
2744 ast_verbose(" Sent(NTP): %u.%010u\n", (unsigned int)now.tv_sec, (unsigned int)now.tv_usec*4096);
2745 ast_verbose(" Sent(RTP): %u\n", rtp->lastts);
2746 ast_verbose(" Sent packets: %u\n", rtp->txcount);
2747 ast_verbose(" Sent octets: %u\n", rtp->txoctetcount);
2748 ast_verbose(" Report block:\n");
2749 ast_verbose(" Fraction lost: %u\n", fraction);
2750 ast_verbose(" Cumulative loss: %u\n", lost);
2751 ast_verbose(" IA jitter: %.4f\n", rtp->rxjitter);
2752 ast_verbose(" Their last SR: %u\n", rtp->rtcp->themrxlsr);
2753 ast_verbose(" DLSR: %4.4f (sec)\n\n", (double)(ntohl(rtcpheader[12])/65536.0));
2755 manager_event(EVENT_FLAG_REPORTING, "RTCPSent", "To %s:%d\r\n"
2757 "SentNTP: %u.%010u\r\n"
2759 "SentPackets: %u\r\n"
2760 "SentOctets: %u\r\n"
2762 "FractionLost: %u\r\n"
2763 "CumulativeLoss: %u\r\n"
2764 "IAJitter: %.4f\r\n"
2765 "TheirLastSR: %u\r\n"
2766 "DLSR: %4.4f (sec)\r\n",
2767 ast_inet_ntoa(rtp->rtcp->them.sin_addr), ntohs(rtp->rtcp->them.sin_port),
2769 (unsigned int)now.tv_sec, (unsigned int)now.tv_usec*4096,
2776 rtp->rtcp->themrxlsr,
2777 (double)(ntohl(rtcpheader[12])/65536.0));
2781 /*! \brief Send RTCP recipient's report */
2782 static int ast_rtcp_write_rr(void *data)
2784 struct ast_rtp *rtp = data;
2788 unsigned int extended;
2789 unsigned int expected;
2790 unsigned int expected_interval;
2791 unsigned int received_interval;
2794 unsigned int *rtcpheader;
2796 struct timeval dlsr;
2799 if (!rtp || !rtp->rtcp || (&rtp->rtcp->them.sin_addr == 0))
2802 if (!rtp->rtcp->them.sin_addr.s_addr) {
2803 ast_log(LOG_ERROR, "RTCP RR transmission error, rtcp halted\n");
2804 if (rtp->rtcp->schedid > 0)
2805 ast_sched_del(rtp->sched, rtp->rtcp->schedid);
2806 rtp->rtcp->schedid = -1;
2810 extended = rtp->cycles + rtp->lastrxseqno;
2811 expected = extended - rtp->seedrxseqno + 1;
2812 lost = expected - rtp->rxcount;
2813 expected_interval = expected - rtp->rtcp->expected_prior;
2814 rtp->rtcp->expected_prior = expected;
2815 received_interval = rtp->rxcount - rtp->rtcp->received_prior;
2816 rtp->rtcp->received_prior = rtp->rxcount;
2817 lost_interval = expected_interval - received_interval;
2818 if (expected_interval == 0 || lost_interval <= 0)
2821 fraction = (lost_interval << 8) / expected_interval;
2822 gettimeofday(&now, NULL);
2823 timersub(&now, &rtp->rtcp->rxlsr, &dlsr);
2824 rtcpheader = (unsigned int *)bdata;
2825 rtcpheader[0] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_RR << 16) | ((len/4)-1));
2826 rtcpheader[1] = htonl(rtp->ssrc);
2827 rtcpheader[2] = htonl(rtp->themssrc);
2828 rtcpheader[3] = htonl(((fraction & 0xff) << 24) | (lost & 0xffffff));
2829 rtcpheader[4] = htonl((rtp->cycles) | ((rtp->lastrxseqno & 0xffff)));
2830 rtcpheader[5] = htonl((unsigned int)(rtp->rxjitter * 65536.));
2831 rtcpheader[6] = htonl(rtp->rtcp->themrxlsr);
2832 rtcpheader[7] = htonl((((dlsr.tv_sec * 1000) + (dlsr.tv_usec / 1000)) * 65536) / 1000);
2834 if (rtp->rtcp->sendfur) {
2835 rtcpheader[8] = htonl((2 << 30) | (0 << 24) | (RTCP_PT_FUR << 16) | 1); /* Header from page 36 in RFC 3550 */
2836 rtcpheader[9] = htonl(rtp->ssrc); /* Our SSRC */
2838 rtp->rtcp->sendfur = 0;
2841 /*! \note Insert SDES here. Probably should make SDES text equal to mimetypes[code].type (not subtype 'cos
2842 it can change mid call, and SDES can't) */
2843 rtcpheader[len/4] = htonl((2 << 30) | (1 << 24) | (RTCP_PT_SDES << 16) | 2);
2844 rtcpheader[(len/4)+1] = htonl(rtp->ssrc); /* Our SSRC */
2845 rtcpheader[(len/4)+2] = htonl(0x01 << 24); /* Empty for the moment */
2848 res = sendto(rtp->rtcp->s, (unsigned int *)rtcpheader, len, 0, (struct sockaddr *)&rtp->rtcp->them, sizeof(rtp->rtcp->them));
2851 ast_log(LOG_ERROR, "RTCP RR transmission error, rtcp halted: %s\n",strerror(errno));
2852 /* Remove the scheduler */
2853 if (rtp->rtcp->schedid > 0)
2854 ast_sched_del(rtp->sched, rtp->rtcp->schedid);
2855 rtp->rtcp->schedid = -1;
2859 rtp->rtcp->rr_count++;
2861 if (rtcp_debug_test_addr(&rtp->rtcp->them)) {
2862 ast_verbose("\n* Sending RTCP RR to %s:%d\n"
2863 " Our SSRC: %u\nTheir SSRC: %u\niFraction lost: %d\nCumulative loss: %u\n"
2864 " IA jitter: %.4f\n"
2865 " Their last SR: %u\n"
2866 " DLSR: %4.4f (sec)\n\n",
2867 ast_inet_ntoa(rtp->rtcp->them.sin_addr),
2868 ntohs(rtp->rtcp->them.sin_port),
2869 rtp->ssrc, rtp->themssrc, fraction, lost,
2871 rtp->rtcp->themrxlsr,
2872 (double)(ntohl(rtcpheader[7])/65536.0));
2878 /*! \brief Write and RTCP packet to the far end
2879 * \note Decide if we are going to send an SR (with Reception Block) or RR
2880 * RR is sent if we have not sent any rtp packets in the previous interval */
2881 static int ast_rtcp_write(void *data)
2883 struct ast_rtp *rtp = data;
2886 if (!rtp || !rtp->rtcp)
2889 if (rtp->txcount > rtp->rtcp->lastsrtxcount)
2890 res = ast_rtcp_write_sr(data);
2892 res = ast_rtcp_write_rr(data);
2897 /*! \brief generate comfort noice (CNG) */
2898 int ast_rtp_sendcng(struct ast_rtp *rtp, int level)
2900 unsigned int *rtpheader;
2905 level = 127 - (level & 0x7f);
2906 payload = ast_rtp_lookup_code(rtp, 0, AST_RTP_CN);
2908 /* If we have no peer, return immediately */
2909 if (!rtp->them.sin_addr.s_addr)
2912 rtp->dtmfmute = ast_tvadd(ast_tvnow(), ast_tv(0, 500000));
2914 /* Get a pointer to the header */
2915 rtpheader = (unsigned int *)data;
2916 rtpheader[0] = htonl((2 << 30) | (1 << 23) | (payload << 16) | (rtp->seqno++));
2917 rtpheader[1] = htonl(rtp->lastts);
2918 rtpheader[2] = htonl(rtp->ssrc);
2920 if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
2921 res = sendto(rtp->s, (void *)rtpheader, hdrlen + 1, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
2923 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));
2924 if (rtp_debug_test_addr(&rtp->them))
2925 ast_verbose("Sent Comfort Noise RTP packet to %s:%u (type %d, seq %u, ts %u, len %d)\n"
2926 , ast_inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port), payload, rtp->seqno, rtp->lastts,res - hdrlen);
2932 /*! \brief Write RTP packet with audio or video media frames into UDP packet */
2933 static int ast_rtp_raw_write(struct ast_rtp *rtp, struct ast_frame *f, int codec)
2935 unsigned char *rtpheader;
2942 ms = calc_txstamp(rtp, &f->delivery);
2943 /* Default prediction */
2944 if (f->subclass < AST_FORMAT_MAX_AUDIO) {
2945 pred = rtp->lastts + f->samples;
2947 /* Re-calculate last TS */
2948 rtp->lastts = rtp->lastts + ms * 8;
2949 if (ast_tvzero(f->delivery)) {
2950 /* If this isn't an absolute delivery time, Check if it is close to our prediction,
2951 and if so, go with our prediction */
2952 if (abs(rtp->lastts - pred) < MAX_TIMESTAMP_SKEW)
2955 ast_debug(3, "Difference is %d, ms is %d\n", abs(rtp->lastts - pred), ms);
2959 } else if(f->subclass < AST_FORMAT_MAX_VIDEO) {
2960 mark = f->subclass & 0x1;
2961 pred = rtp->lastovidtimestamp + f->samples;
2962 /* Re-calculate last TS */
2963 rtp->lastts = rtp->lastts + ms * 90;
2964 /* If it's close to our prediction, go for it */
2965 if (ast_tvzero(f->delivery)) {
2966 if (abs(rtp->lastts - pred) < 7200) {
2968 rtp->lastovidtimestamp += f->samples;
2970 ast_debug(3, "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);
2971 rtp->lastovidtimestamp = rtp->lastts;
2975 pred = rtp->lastotexttimestamp + f->samples;
2976 /* Re-calculate last TS */
2977 rtp->lastts = rtp->lastts + ms * 90;
2978 /* If it's close to our prediction, go for it */
2979 if (ast_tvzero(f->delivery)) {
2980 if (abs(rtp->lastts - pred) < 7200) {
2982 rtp->lastotexttimestamp += f->samples;
2984 ast_debug(3, "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);
2985 rtp->lastotexttimestamp = rtp->lastts;
2989 /* If the timestamp for non-digit packets has moved beyond the timestamp
2990 for digits, update the digit timestamp.
2992 if (rtp->lastts > rtp->lastdigitts)
2993 rtp->lastdigitts = rtp->lastts;
2995 if (f->has_timing_info)
2996 rtp->lastts = f->ts * 8;
2998 /* Get a pointer to the header */
2999 rtpheader = (unsigned char *)(f->data - hdrlen);
3001 put_unaligned_uint32(rtpheader, htonl((2 << 30) | (codec << 16) | (rtp->seqno) | (mark << 23)));
3002 put_unaligned_uint32(rtpheader + 4, htonl(rtp->lastts));
3003 put_unaligned_uint32(rtpheader + 8, htonl(rtp->ssrc));
3005 if (rtp->them.sin_port && rtp->them.sin_addr.s_addr) {
3006 res = sendto(rtp->s, (void *)rtpheader, f->datalen + hdrlen, 0, (struct sockaddr *)&rtp->them, sizeof(rtp->them));
3008 if (!rtp->nat || (rtp->nat && (ast_test_flag(rtp, FLAG_NAT_ACTIVE) == FLAG_NAT_ACTIVE))) {
3009 ast_debug(1, "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));
3010 } else if (((ast_test_flag(rtp, FLAG_NAT_ACTIVE) == FLAG_NAT_INACTIVE) || rtpdebug) && !ast_test_flag(rtp, FLAG_NAT_INACTIVE_NOWARN)) {
3011 /* Only give this error message once if we are not RTP debugging */
3012 if (option_debug || rtpdebug)
3013 ast_debug(0, "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));
3014 ast_set_flag(rtp, FLAG_NAT_INACTIVE_NOWARN);
3018 rtp->txoctetcount +=(res - hdrlen);
3020 if (rtp->rtcp && rtp->rtcp->schedid < 1)
3021 rtp->rtcp->schedid = ast_sched_add(rtp->sched, ast_rtcp_calc_interval(rtp), ast_rtcp_write, rtp);
3024 if (rtp_debug_test_addr(&rtp->them))
3025 ast_verbose("Sent RTP packet to %s:%u (type %-2.2d, seq %-6.6u, ts %-6.6u, len %-6.6u)\n",
3026 ast_inet_ntoa(rtp->them.sin_addr), ntohs(rtp->them.sin_port), codec, rtp->seqno, rtp->lastts,res - hdrlen);
3034 int ast_rtp_codec_setpref(struct ast_rtp *rtp, struct ast_codec_pref *prefs)
3037 for (x = 0; x < 32; x++) { /* Ugly way */
3038 rtp->pref.order[x] = prefs->order[x];
3039 rtp->pref.framing[x] = prefs->framing[x];
3042 ast_smoother_free(rtp->smoother);
3043 rtp->smoother = NULL;
3047 struct ast_codec_pref *ast_rtp_codec_getpref(struct ast_rtp *rtp)
3052 int ast_rtp_codec_getformat(int pt)
3054 if (pt < 0 || pt > MAX_RTP_PT)
3055 return 0; /* bogus payload type */
3057 if (static_RTP_PT[pt].isAstFormat)
3058 return static_RTP_PT[pt].code;
3063 int ast_rtp_write(struct ast_rtp *rtp, struct ast_frame *_f)
3065 struct ast_frame *f;
3071 /* If we have no peer, return immediately */
3072 if (!rtp->them.sin_addr.s_addr)
3075 /* If there is no data length, return immediately */
3079 /* Make sure we have enough space for RTP header */
3080 if ((_f->frametype != AST_FRAME_VOICE) && (_f->frametype != AST_FRAME_VIDEO) && (_f->frametype != AST_FRAME_TEXT)) {
3081 ast_log(LOG_WARNING, "RTP can only send voice, video and text\n");
3085 /* The bottom bit of a video subclass contains the marker bit */
3086 subclass = _f->subclass;
3087 if (_f->frametype == AST_FRAME_VIDEO)
3090 codec = ast_rtp_lookup_code(rtp, 1, subclass);
3092 ast_log(LOG_WARNING, "Don't know how to send format %s packets with RTP\n", ast_getformatname(_f->subclass));
3096 if (rtp->lasttxformat != subclass) {
3097 /* New format, reset the smoother */
3098 ast_debug(1, "Ooh, format changed from %s to %s\n", ast_getformatname(rtp->lasttxformat), ast_getformatname(subclass));
3099 rtp->lasttxformat = subclass;
3101 ast_smoother_free(rtp->smoother);
3102 rtp->smoother = NULL;
3105 if (!rtp->smoother && subclass != AST_FORMAT_SPEEX) {
3106 struct ast_format_list fmt = ast_codec_pref_getsize(&rtp->pref, subclass);
3107 if (fmt.inc_ms) { /* if codec parameters is set / avoid division by zero */
3108 if (!(rtp->smoother = ast_smoother_new((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms))) {
3109 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));
3113 ast_smoother_set_flags(rtp->smoother, fmt.flags);
3114 ast_debug(1, "Created smoother: format: %d ms: %d len: %d\n", subclass, fmt.cur_ms, ((fmt.cur_ms * fmt.fr_len) / fmt.inc_ms));
3117 if (rtp->smoother) {
3118 if (ast_smoother_test_flag(rtp->smoother, AST_SMOOTHER_FLAG_BE)) {
3119 ast_smoother_feed_be(rtp->smoother, _f);
3121 ast_smoother_feed(rtp->smoother, _f);
3124 while ((f = ast_smoother_read(rtp->smoother)) && (f->data))
3125 ast_rtp_raw_write(rtp, f, codec);
3127 /* Don't buffer outgoing frames; send them one-per-packet: */
3128 if (_f->offset < hdrlen)
3129 f = ast_frdup(_f); /*! \bug XXX this might never be free'd. Why do we do this? */
3133 ast_rtp_raw_write(rtp, f, codec);
3141 /*! \brief Unregister interface to channel driver */
3142 void ast_rtp_proto_unregister(struct ast_rtp_protocol *proto)
3144 AST_RWLIST_WRLOCK(&protos);
3145 AST_RWLIST_REMOVE(&protos, proto, list);<