Merge "res_pjsip/res_pjsip_callerid: NULL check on caller id name string"
[asterisk/asterisk.git] / res / res_pjsip_sdp_rtp.c
index bc8c748..97e365c 100644 (file)
@@ -38,8 +38,7 @@
 #include <pjmedia.h>
 #include <pjlib.h>
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
-
+#include "asterisk/utils.h"
 #include "asterisk/module.h"
 #include "asterisk/format.h"
 #include "asterisk/format_cap.h"
@@ -50,6 +49,8 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/sched.h"
 #include "asterisk/acl.h"
 #include "asterisk/sdp_srtp.h"
+#include "asterisk/dsp.h"
+#include "asterisk/linkedlists.h"       /* for AST_LIST_NEXT */
 
 #include "asterisk/res_pjsip.h"
 #include "asterisk/res_pjsip_session.h"
@@ -57,11 +58,8 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 /*! \brief Scheduler for RTCP purposes */
 static struct ast_sched_context *sched;
 
-/*! \brief Address for IPv4 RTP */
-static struct ast_sockaddr address_ipv4;
-
-/*! \brief Address for IPv6 RTP */
-static struct ast_sockaddr address_ipv6;
+/*! \brief Address for RTP */
+static struct ast_sockaddr address_rtp;
 
 static const char STR_AUDIO[] = "audio";
 static const int FD_AUDIO = 0;
@@ -89,7 +87,8 @@ static int media_type_to_fdno(enum ast_media_type media_type)
        case AST_MEDIA_TYPE_VIDEO: return FD_VIDEO;
        case AST_MEDIA_TYPE_TEXT:
        case AST_MEDIA_TYPE_UNKNOWN:
-       case AST_MEDIA_TYPE_IMAGE: break;
+       case AST_MEDIA_TYPE_IMAGE:
+       case AST_MEDIA_TYPE_END: break;
        }
        return -1;
 }
@@ -106,24 +105,148 @@ static void format_cap_only_type(struct ast_format_cap *caps, enum ast_media_typ
        }
 }
 
+static int send_keepalive(const void *data)
+{
+       struct ast_sip_session_media *session_media = (struct ast_sip_session_media *) data;
+       struct ast_rtp_instance *rtp = session_media->rtp;
+       int keepalive;
+       time_t interval;
+       int send_keepalive;
+
+       if (!rtp) {
+               return 0;
+       }
+
+       keepalive = ast_rtp_instance_get_keepalive(rtp);
+
+       if (!ast_sockaddr_isnull(&session_media->direct_media_addr)) {
+               ast_debug(3, "Not sending RTP keepalive on RTP instance %p since direct media is in use\n", rtp);
+               return keepalive * 1000;
+       }
+
+       interval = time(NULL) - ast_rtp_instance_get_last_tx(rtp);
+       send_keepalive = interval >= keepalive;
+
+       ast_debug(3, "It has been %d seconds since RTP was last sent on instance %p. %sending keepalive\n",
+                       (int) interval, rtp, send_keepalive ? "S" : "Not s");
+
+       if (send_keepalive) {
+               ast_rtp_instance_sendcng(rtp, 0);
+               return keepalive * 1000;
+       }
+
+       return (keepalive - interval) * 1000;
+}
+
+/*! \brief Check whether RTP is being received or not */
+static int rtp_check_timeout(const void *data)
+{
+       struct ast_sip_session_media *session_media = (struct ast_sip_session_media *)data;
+       struct ast_rtp_instance *rtp = session_media->rtp;
+       int elapsed;
+       struct ast_channel *chan;
+
+       if (!rtp) {
+               return 0;
+       }
+
+       elapsed = time(NULL) - ast_rtp_instance_get_last_rx(rtp);
+       if (elapsed < ast_rtp_instance_get_timeout(rtp)) {
+               return (ast_rtp_instance_get_timeout(rtp) - elapsed) * 1000;
+       }
+
+       chan = ast_channel_get_by_name(ast_rtp_instance_get_channel_id(rtp));
+       if (!chan) {
+               return 0;
+       }
+
+       ast_log(LOG_NOTICE, "Disconnecting channel '%s' for lack of RTP activity in %d seconds\n",
+               ast_channel_name(chan), elapsed);
+
+       ast_channel_lock(chan);
+       ast_channel_hangupcause_set(chan, AST_CAUSE_REQUESTED_CHAN_UNAVAIL);
+       ast_channel_unlock(chan);
+
+       ast_softhangup(chan, AST_SOFTHANGUP_DEV);
+       ast_channel_unref(chan);
+
+       return 0;
+}
+
+/*!
+ * \brief Enable RTCP on an RTP session.
+ */
+static void enable_rtcp(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
+       const struct pjmedia_sdp_media *remote_media)
+{
+       enum ast_rtp_instance_rtcp rtcp_type;
+
+       if (session->endpoint->media.rtcp_mux && session_media->remote_rtcp_mux) {
+               rtcp_type = AST_RTP_INSTANCE_RTCP_MUX;
+       } else {
+               rtcp_type = AST_RTP_INSTANCE_RTCP_STANDARD;
+       }
+
+       ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_RTCP, rtcp_type);
+}
+
 /*! \brief Internal function which creates an RTP instance */
-static int create_rtp(struct ast_sip_session *session, struct ast_sip_session_media *session_media, unsigned int ipv6)
+static int create_rtp(struct ast_sip_session *session, struct ast_sip_session_media *session_media)
 {
        struct ast_rtp_engine_ice *ice;
+       struct ast_sockaddr temp_media_address;
+       struct ast_sockaddr *media_address =  &address_rtp;
+
+       if (session->endpoint->media.bind_rtp_to_media_address && !ast_strlen_zero(session->endpoint->media.address)) {
+               if (ast_sockaddr_parse(&temp_media_address, session->endpoint->media.address, 0)) {
+                       ast_debug(1, "Endpoint %s: Binding RTP media to %s\n",
+                               ast_sorcery_object_get_id(session->endpoint),
+                               session->endpoint->media.address);
+                       media_address = &temp_media_address;
+               } else {
+                       ast_debug(1, "Endpoint %s: RTP media address invalid: %s\n",
+                               ast_sorcery_object_get_id(session->endpoint),
+                               session->endpoint->media.address);
+               }
+       } else {
+               struct ast_sip_transport *transport;
+
+               transport = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "transport",
+                       session->endpoint->transport);
+               if (transport) {
+                       struct ast_sip_transport_state *trans_state;
+
+                       trans_state = ast_sip_get_transport_state(ast_sorcery_object_get_id(transport));
+                       if (trans_state) {
+                               char hoststr[PJ_INET6_ADDRSTRLEN];
+
+                               pj_sockaddr_print(&trans_state->host, hoststr, sizeof(hoststr), 0);
+                               if (ast_sockaddr_parse(&temp_media_address, hoststr, 0)) {
+                                       ast_debug(1, "Transport %s bound to %s: Using it for RTP media.\n",
+                                               session->endpoint->transport, hoststr);
+                                       media_address = &temp_media_address;
+                               } else {
+                                       ast_debug(1, "Transport %s bound to %s: Invalid for RTP media.\n",
+                                               session->endpoint->transport, hoststr);
+                               }
+                               ao2_ref(trans_state, -1);
+                       }
+                       ao2_ref(transport, -1);
+               }
+       }
 
-       if (!(session_media->rtp = ast_rtp_instance_new(session->endpoint->media.rtp.engine, sched, ipv6 ? &address_ipv6 : &address_ipv4, NULL))) {
+       if (!(session_media->rtp = ast_rtp_instance_new(session->endpoint->media.rtp.engine, sched, media_address, NULL))) {
                ast_log(LOG_ERROR, "Unable to create RTP instance using RTP engine '%s'\n", session->endpoint->media.rtp.engine);
                return -1;
        }
 
-       ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_RTCP, 1);
        ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_NAT, session->endpoint->media.rtp.symmetric);
 
        if (!session->endpoint->media.rtp.ice_support && (ice = ast_rtp_instance_get_ice(session_media->rtp))) {
                ice->stop(session_media->rtp);
        }
 
-       if (session->endpoint->dtmf == AST_SIP_DTMF_RFC_4733) {
+       if (session->endpoint->dtmf == AST_SIP_DTMF_RFC_4733 || session->endpoint->dtmf == AST_SIP_DTMF_AUTO) {
                ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_RFC2833);
                ast_rtp_instance_set_prop(session_media->rtp, AST_RTP_PROPERTY_DTMF, 1);
        } else if (session->endpoint->dtmf == AST_SIP_DTMF_INBAND) {
@@ -131,7 +254,7 @@ static int create_rtp(struct ast_sip_session *session, struct ast_sip_session_me
        }
 
        if (!strcmp(session_media->stream_type, STR_AUDIO) &&
-                       (session->endpoint->media.tos_audio || session->endpoint->media.cos_video)) {
+                       (session->endpoint->media.tos_audio || session->endpoint->media.cos_audio)) {
                ast_rtp_instance_set_qos(session_media->rtp, session->endpoint->media.tos_audio,
                                session->endpoint->media.cos_audio, "SIP RTP Audio");
        } else if (!strcmp(session_media->stream_type, STR_VIDEO) &&
@@ -140,19 +263,24 @@ static int create_rtp(struct ast_sip_session *session, struct ast_sip_session_me
                                session->endpoint->media.cos_video, "SIP RTP Video");
        }
 
+       ast_rtp_instance_set_last_rx(session_media->rtp, time(NULL));
+
        return 0;
 }
 
-static void get_codecs(struct ast_sip_session *session, const struct pjmedia_sdp_media *stream, struct ast_rtp_codecs *codecs)
+static void get_codecs(struct ast_sip_session *session, const struct pjmedia_sdp_media *stream, struct ast_rtp_codecs *codecs,
+       struct ast_sip_session_media *session_media)
 {
        pjmedia_sdp_attr *attr;
        pjmedia_sdp_rtpmap *rtpmap;
        pjmedia_sdp_fmtp fmtp;
        struct ast_format *format;
-       int i, num = 0;
+       int i, num = 0, tel_event = 0;
        char name[256];
        char media[20];
        char fmt_param[256];
+       enum ast_rtp_options options = session->endpoint->media.g726_non_standard ?
+               AST_RTP_OPT_G726_NONSTANDARD : 0;
 
        ast_rtp_codecs_payloads_initialize(codecs);
 
@@ -171,16 +299,24 @@ static void get_codecs(struct ast_sip_session *session, const struct pjmedia_sdp
                }
 
                ast_copy_pj_str(name, &rtpmap->enc_name, sizeof(name));
+               if (strcmp(name, "telephone-event") == 0) {
+                       tel_event++;
+               }
+
                ast_copy_pj_str(media, (pj_str_t*)&stream->desc.media, sizeof(media));
-               ast_rtp_codecs_payloads_set_rtpmap_type_rate(codecs, NULL, pj_strtoul(&stream->desc.fmt[i]),
-                                                            media, name, 0, rtpmap->clock_rate);
+               ast_rtp_codecs_payloads_set_rtpmap_type_rate(codecs, NULL,
+                       pj_strtoul(&stream->desc.fmt[i]), media, name, options, rtpmap->clock_rate);
                /* Look for an optional associated fmtp attribute */
                if (!(attr = pjmedia_sdp_media_find_attr2(stream, "fmtp", &rtpmap->pt))) {
                        continue;
                }
 
                if ((pjmedia_sdp_attr_get_fmtp(attr, &fmtp)) == PJ_SUCCESS) {
-                       sscanf(pj_strbuf(&fmtp.fmt), "%d", &num);
+                       ast_copy_pj_str(fmt_param, &fmtp.fmt, sizeof(fmt_param));
+                       if (sscanf(fmt_param, "%30d", &num) != 1) {
+                               continue;
+                       }
+
                        if ((format = ast_rtp_codecs_get_payload_format(codecs, num))) {
                                struct ast_format *format_parsed;
 
@@ -196,7 +332,9 @@ static void get_codecs(struct ast_sip_session *session, const struct pjmedia_sdp
                        }
                }
        }
-
+       if (!tel_event && (session->endpoint->dtmf == AST_SIP_DTMF_AUTO)) {
+               ast_rtp_instance_dtmf_mode_set(session_media->rtp, AST_RTP_DTMF_MODE_INBAND);
+       }
        /* Get the packetization, if it exists */
        if ((attr = pjmedia_sdp_media_find_attr2(stream, "ptime", NULL))) {
                unsigned long framing = pj_strtoul(pj_strltrim(&attr->value));
@@ -206,8 +344,10 @@ static void get_codecs(struct ast_sip_session *session, const struct pjmedia_sdp
        }
 }
 
-static int set_caps(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
-                   const struct pjmedia_sdp_media *stream)
+static int set_caps(struct ast_sip_session *session,
+       struct ast_sip_session_media *session_media,
+       const struct pjmedia_sdp_media *stream,
+       int is_offer)
 {
        RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
        RAII_VAR(struct ast_format_cap *, peer, NULL, ao2_cleanup);
@@ -217,6 +357,7 @@ static int set_caps(struct ast_sip_session *session, struct ast_sip_session_medi
        int fmts = 0;
        int direct_media_enabled = !ast_sockaddr_isnull(&session_media->direct_media_addr) &&
                ast_format_cap_count(session->direct_media_cap);
+       int dsp_features = 0;
 
        if (!(caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT)) ||
            !(peer = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT)) ||
@@ -234,14 +375,14 @@ static int set_caps(struct ast_sip_session *session, struct ast_sip_session_medi
        }
 
        /* get the capabilities on the peer */
-       get_codecs(session, stream, &codecs);
+       get_codecs(session, stream, &codecs,  session_media);
        ast_rtp_codecs_payload_formats(&codecs, peer, &fmts);
 
        /* get the joint capabilities between peer and endpoint */
        ast_format_cap_get_compatible(caps, peer, joint);
        if (!ast_format_cap_count(joint)) {
-               struct ast_str *usbuf = ast_str_alloca(64);
-               struct ast_str *thembuf = ast_str_alloca(64);
+               struct ast_str *usbuf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
+               struct ast_str *thembuf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
 
                ast_rtp_codecs_payloads_destroy(&codecs);
                ast_log(LOG_NOTICE, "No joint capabilities for '%s' media stream between our configuration(%s) and incoming SDP(%s)\n",
@@ -251,62 +392,89 @@ static int set_caps(struct ast_sip_session *session, struct ast_sip_session_medi
                return -1;
        }
 
+       if (is_offer) {
+               /*
+                * Setup rx payload type mapping to prefer the mapping
+                * from the peer that the RFC says we SHOULD use.
+                */
+               ast_rtp_codecs_payloads_xover(&codecs, &codecs, NULL);
+       }
        ast_rtp_codecs_payloads_copy(&codecs, ast_rtp_instance_get_codecs(session_media->rtp),
-                                    session_media->rtp);
+               session_media->rtp);
 
        ast_format_cap_append_from_cap(session->req_caps, joint, AST_MEDIA_TYPE_UNKNOWN);
 
        if (session->channel) {
-               struct ast_format *fmt;
-
                ast_channel_lock(session->channel);
                ast_format_cap_remove_by_type(caps, AST_MEDIA_TYPE_UNKNOWN);
-               ast_format_cap_append_from_cap(caps, ast_channel_nativeformats(session->channel), AST_MEDIA_TYPE_UNKNOWN);
+               ast_format_cap_append_from_cap(caps, ast_channel_nativeformats(session->channel),
+                       AST_MEDIA_TYPE_UNKNOWN);
                ast_format_cap_remove_by_type(caps, media_type);
-
-               /*
-                * XXX Historically we picked the "best" joint format to use
-                * and stuck with it.  It would be nice to just append the
-                * determined joint media capabilities to give translation
-                * more formats to choose from when necessary.  Unfortunately,
-                * there are some areas of the system where this doesn't work
-                * very well. (The softmix bridge in particular is reluctant
-                * to pick higher fidelity formats and has a problem with
-                * asymmetric sample rates.)
-                */
-               fmt = ast_format_cap_get_format(joint, 0);
-               ast_format_cap_append(caps, fmt, 0);
-
+               if (session->endpoint->preferred_codec_only){
+                       struct ast_format *preferred_fmt = ast_format_cap_get_format(joint, 0);
+                       ast_format_cap_append(caps, preferred_fmt, 0);
+                       ao2_ref(preferred_fmt, -1);
+               } else {
+                       ast_format_cap_append_from_cap(caps, joint, media_type);
+               }
                /*
                 * Apply the new formats to the channel, potentially changing
                 * raw read/write formats and translation path while doing so.
                 */
                ast_channel_nativeformats_set(session->channel, caps);
-               ast_set_read_format(session->channel, ast_channel_readformat(session->channel));
-               ast_set_write_format(session->channel, ast_channel_writeformat(session->channel));
-               ast_channel_unlock(session->channel);
+               if (media_type == AST_MEDIA_TYPE_AUDIO) {
+                       ast_set_read_format(session->channel, ast_channel_readformat(session->channel));
+                       ast_set_write_format(session->channel, ast_channel_writeformat(session->channel));
+               }
+               if ((session->endpoint->dtmf == AST_SIP_DTMF_AUTO)
+                   && (ast_rtp_instance_dtmf_mode_get(session_media->rtp) == AST_RTP_DTMF_MODE_RFC2833)
+                   && (session->dsp)) {
+                       dsp_features = ast_dsp_get_features(session->dsp);
+                       dsp_features &= ~DSP_FEATURE_DIGIT_DETECT;
+                       if (dsp_features) {
+                               ast_dsp_set_features(session->dsp, dsp_features);
+                       } else {
+                               ast_dsp_free(session->dsp);
+                               session->dsp = NULL;
+                       }
+               }
+
+               if (ast_channel_is_bridged(session->channel)) {
+                       ast_channel_set_unbridged_nolock(session->channel, 1);
+               }
 
-               ao2_ref(fmt, -1);
+               ast_channel_unlock(session->channel);
        }
 
        ast_rtp_codecs_payloads_destroy(&codecs);
        return 0;
 }
 
-static pjmedia_sdp_attr* generate_rtpmap_attr(pjmedia_sdp_media *media, pj_pool_t *pool, int rtp_code,
-                                             int asterisk_format, struct ast_format *format, int code)
+static pjmedia_sdp_attr* generate_rtpmap_attr(struct ast_sip_session *session, pjmedia_sdp_media *media, pj_pool_t *pool,
+                                             int rtp_code, int asterisk_format, struct ast_format *format, int code)
 {
+       extern pj_bool_t pjsip_use_compact_form;
        pjmedia_sdp_rtpmap rtpmap;
        pjmedia_sdp_attr *attr = NULL;
        char tmp[64];
+       enum ast_rtp_options options = session->endpoint->media.g726_non_standard ?
+               AST_RTP_OPT_G726_NONSTANDARD : 0;
 
        snprintf(tmp, sizeof(tmp), "%d", rtp_code);
        pj_strdup2(pool, &media->desc.fmt[media->desc.fmt_count++], tmp);
+
+       if (rtp_code <= AST_RTP_PT_LAST_STATIC && pjsip_use_compact_form) {
+               return NULL;
+       }
+
        rtpmap.pt = media->desc.fmt[media->desc.fmt_count - 1];
        rtpmap.clock_rate = ast_rtp_lookup_sample_rate2(asterisk_format, format, code);
-       pj_strdup2(pool, &rtpmap.enc_name, ast_rtp_lookup_mime_subtype2(asterisk_format, format, code, 0));
-       rtpmap.param.slen = 0;
-       rtpmap.param.ptr = NULL;
+       pj_strdup2(pool, &rtpmap.enc_name, ast_rtp_lookup_mime_subtype2(asterisk_format, format, code, options));
+       if (!pj_stricmp2(&rtpmap.enc_name, "opus")) {
+               pj_cstr(&rtpmap.param, "2");
+       } else {
+               pj_cstr(&rtpmap.param, NULL);
+       }
 
        pjmedia_sdp_rtpmap_to_attr(pool, &rtpmap, &attr);
 
@@ -328,7 +496,7 @@ static pjmedia_sdp_attr* generate_fmtp_attr(pj_pool_t *pool, struct ast_format *
                *++tmp = '\0';
                /* ast...generate gives us everything, just need value */
                tmp = strchr(ast_str_buffer(fmtp0), ':');
-               if (tmp && tmp + 1) {
+               if (tmp && tmp[1] != '\0') {
                        fmtp1 = pj_str(tmp + 1);
                } else {
                        fmtp1 = pj_str(ast_str_buffer(fmtp0));
@@ -396,6 +564,7 @@ static void add_ice_to_stream(struct ast_sip_session *session, struct ast_sip_se
        }
 
        ao2_iterator_destroy(&it_candidates);
+       ao2_ref(candidates, -1);
 }
 
 /*! \brief Function which processes ICE attributes in an audio stream */
@@ -459,6 +628,13 @@ static void process_ice_attributes(struct ast_sip_session *session, struct ast_s
                        continue;
                }
 
+               if (session->endpoint->media.rtcp_mux && session_media->remote_rtcp_mux && candidate.id > 1) {
+                       /* Remote side may have offered RTP and RTCP candidates. However, if we're using RTCP MUX,
+                        * then we should ignore RTCP candidates.
+                        */
+                       continue;
+               }
+
                candidate.foundation = foundation;
                candidate.transport = transport;
 
@@ -519,6 +695,9 @@ static enum ast_sip_session_media_encryption get_media_encryption_type(pj_str_t
 
        *optimistic = 0;
 
+       if (!transport_str) {
+               return AST_SIP_MEDIA_TRANSPORT_INVALID;
+       }
        if (strstr(transport_str, "UDP/TLS")) {
                return AST_SIP_MEDIA_ENCRYPT_DTLS;
        } else if (strstr(transport_str, "SAVP")) {
@@ -627,7 +806,7 @@ static void apply_dtls_attrib(struct ast_sip_session_media *session_media,
        struct ast_rtp_engine_dtls *dtls = ast_rtp_instance_get_dtls(session_media->rtp);
        pj_str_t *value;
 
-       if (!attr->value.ptr) {
+       if (!attr->value.ptr || !dtls) {
                return;
        }
 
@@ -752,12 +931,32 @@ static int setup_media_encryption(struct ast_sip_session *session,
        return 0;
 }
 
+static void set_ice_components(struct ast_sip_session *session, struct ast_sip_session_media *session_media)
+{
+       struct ast_rtp_engine_ice *ice;
+
+       ast_assert(session_media->rtp != NULL);
+
+       ice = ast_rtp_instance_get_ice(session_media->rtp);
+       if (!session->endpoint->media.rtp.ice_support || !ice) {
+               return;
+       }
+
+       if (session->endpoint->media.rtcp_mux && session_media->remote_rtcp_mux) {
+               /* We both support RTCP mux. Only one ICE component necessary */
+               ice->change_components(session_media->rtp, 1);
+       } else {
+               /* They either don't support RTCP mux or we don't know if they do yet. */
+               ice->change_components(session_media->rtp, 2);
+       }
+}
+
 /*! \brief Function which negotiates an incoming media stream */
 static int negotiate_incoming_sdp_stream(struct ast_sip_session *session, struct ast_sip_session_media *session_media,
                                         const struct pjmedia_sdp_session *sdp, const struct pjmedia_sdp_media *stream)
 {
        char host[NI_MAXHOST];
-       RAII_VAR(struct ast_sockaddr *, addrs, NULL, ast_free_ptr);
+       RAII_VAR(struct ast_sockaddr *, addrs, NULL, ast_free);
        enum ast_media_type media_type = stream_to_media_type(session_media->stream_type);
        enum ast_sip_session_media_encryption encryption = AST_SIP_MEDIA_ENCRYPT_NONE;
        int res;
@@ -792,15 +991,22 @@ static int negotiate_incoming_sdp_stream(struct ast_sip_session *session, struct
        }
 
        /* Using the connection information create an appropriate RTP instance */
-       if (!session_media->rtp && create_rtp(session, session_media, ast_sockaddr_is_ipv6(addrs))) {
+       if (!session_media->rtp && create_rtp(session, session_media)) {
                return -1;
        }
 
+       session_media->remote_rtcp_mux = (pjmedia_sdp_media_find_attr2(stream, "rtcp-mux", NULL) != NULL);
+       set_ice_components(session, session_media);
+
+       enable_rtcp(session, session_media, stream);
+
        res = setup_media_encryption(session, session_media, sdp, stream);
        if (res) {
-               if (!session->endpoint->media.rtp.encryption_optimistic) {
+               if (!session->endpoint->media.rtp.encryption_optimistic ||
+                       !pj_strncmp2(&stream->desc.transport, "RTP/SAVP", 8)) {
                        /* If optimistic encryption is disabled and crypto should have been enabled
-                        * but was not this session must fail.
+                        * but was not this session must fail. This must also fail if crypto was
+                        * required in the offer but could not be set up.
                         */
                        return -1;
                }
@@ -818,7 +1024,7 @@ static int negotiate_incoming_sdp_stream(struct ast_sip_session *session, struct
                pj_strdup(session->inv_session->pool, &session_media->transport, &stream->desc.transport);
        }
 
-       if (set_caps(session, session_media, stream)) {
+       if (set_caps(session, session_media, stream, 1)) {
                return 0;
        }
        return 1;
@@ -833,6 +1039,7 @@ static int add_crypto_to_stream(struct ast_sip_session *session,
        enum ast_rtp_dtls_hash hash;
        const char *crypto_attribute;
        struct ast_rtp_engine_dtls *dtls;
+       struct ast_sdp_srtp *tmp;
        static const pj_str_t STR_NEW = { "new", 3 };
        static const pj_str_t STR_EXISTING = { "existing", 8 };
        static const pj_str_t STR_ACTIVE = { "active", 6 };
@@ -852,16 +1059,22 @@ static int add_crypto_to_stream(struct ast_sip_session *session,
                        }
                }
 
-               crypto_attribute = ast_sdp_srtp_get_attrib(session_media->srtp,
-                       0 /* DTLS running? No */,
-                       session->endpoint->media.rtp.srtp_tag_32 /* 32 byte tag length? */);
-               if (!crypto_attribute) {
-                       /* No crypto attribute to add, bad news */
-                       return -1;
-               }
+               tmp = session_media->srtp;
+
+               do {
+                       crypto_attribute = ast_sdp_srtp_get_attrib(tmp,
+                               0 /* DTLS running? No */,
+                               session->endpoint->media.rtp.srtp_tag_32 /* 32 byte tag length? */);
+                       if (!crypto_attribute) {
+                               /* No crypto attribute to add, bad news */
+                               return -1;
+                       }
+
+                       attr = pjmedia_sdp_attr_create(pool, "crypto",
+                               pj_cstr(&stmp, crypto_attribute));
+                       media->attr[media->attr_count++] = attr;
+               } while ((tmp = AST_LIST_NEXT(tmp, sdp_srtp_list)));
 
-               attr = pjmedia_sdp_attr_create(pool, "crypto", pj_cstr(&stmp, crypto_attribute));
-               media->attr[media->attr_count++] = attr;
                break;
        case AST_SIP_MEDIA_ENCRYPT_DTLS:
                if (setup_dtls_srtp(session, session_media)) {
@@ -941,13 +1154,13 @@ static int create_outgoing_sdp_stream(struct ast_sip_session *session, struct as
        static const pj_str_t STR_SENDRECV = { "sendrecv", 8 };
        static const pj_str_t STR_SENDONLY = { "sendonly", 8 };
        pjmedia_sdp_media *media;
-       char hostip[PJ_INET6_ADDRSTRLEN+2];
+       const char *hostip = NULL;
        struct ast_sockaddr addr;
        char tmp[512];
        pj_str_t stmp;
        pjmedia_sdp_attr *attr;
        int index = 0;
-       int noncodec = (session->endpoint->dtmf == AST_SIP_DTMF_RFC_4733) ? AST_RTP_DTMF : 0;
+       int noncodec = (session->endpoint->dtmf == AST_SIP_DTMF_RFC_4733 || session->endpoint->dtmf == AST_SIP_DTMF_AUTO) ? AST_RTP_DTMF : 0;
        int min_packet_size = 0, max_packet_size = 0;
        int rtp_code;
        RAII_VAR(struct ast_format_cap *, caps, NULL, ao2_cleanup);
@@ -961,10 +1174,13 @@ static int create_outgoing_sdp_stream(struct ast_sip_session *session, struct as
            (!use_override_prefs && !ast_format_cap_has_type(session->endpoint->media.codecs, media_type))) {
                /* If no type formats are configured don't add a stream */
                return 0;
-       } else if (!session_media->rtp && create_rtp(session, session_media, session->endpoint->media.rtp.ipv6)) {
+       } else if (!session_media->rtp && create_rtp(session, session_media)) {
                return -1;
        }
 
+       set_ice_components(session, session_media);
+       enable_rtcp(session, session_media, NULL);
+
        if (!(media = pj_pool_zalloc(pool, sizeof(struct pjmedia_sdp_media))) ||
                !(media->conn = pj_pool_zalloc(pool, sizeof(struct pjmedia_sdp_conn)))) {
                return -1;
@@ -989,21 +1205,32 @@ static int create_outgoing_sdp_stream(struct ast_sip_session *session, struct as
 
        /* Add connection level details */
        if (direct_media_enabled) {
-               ast_copy_string(hostip, ast_sockaddr_stringify_fmt(&session_media->direct_media_addr, AST_SOCKADDR_STR_ADDR), sizeof(hostip));
+               hostip = ast_sockaddr_stringify_fmt(&session_media->direct_media_addr, AST_SOCKADDR_STR_ADDR);
        } else if (ast_strlen_zero(session->endpoint->media.address)) {
-               pj_sockaddr localaddr;
-
-               if (pj_gethostip(session->endpoint->media.rtp.ipv6 ? pj_AF_INET6() : pj_AF_INET(), &localaddr)) {
-                       return -1;
-               }
-               pj_sockaddr_print(&localaddr, hostip, sizeof(hostip), 2);
+               hostip = ast_sip_get_host_ip_string(session->endpoint->media.rtp.ipv6 ? pj_AF_INET6() : pj_AF_INET());
        } else {
-               ast_copy_string(hostip, session->endpoint->media.address, sizeof(hostip));
+               hostip = session->endpoint->media.address;
+       }
+
+       if (ast_strlen_zero(hostip)) {
+               ast_log(LOG_ERROR, "No local host IP available for stream %s\n", session_media->stream_type);
+               return -1;
        }
 
        media->conn->net_type = STR_IN;
-       media->conn->addr_type = session->endpoint->media.rtp.ipv6 ? STR_IP6 : STR_IP4;
+       /* Assume that the connection will use IPv4 until proven otherwise */
+       media->conn->addr_type = STR_IP4;
        pj_strdup2(pool, &media->conn->addr, hostip);
+
+       if (!ast_strlen_zero(session->endpoint->media.address)) {
+               pj_sockaddr ip;
+
+               if ((pj_sockaddr_parse(pj_AF_UNSPEC(), 0, &media->conn->addr, &ip) == PJ_SUCCESS) &&
+                       (ip.addr.sa_family == pj_AF_INET6())) {
+                       media->conn->addr_type = STR_IP6;
+               }
+       }
+
        ast_rtp_instance_get_local_address(session_media->rtp, &addr);
        media->desc.port = direct_media_enabled ? ast_sockaddr_port(&session_media->direct_media_addr) : (pj_uint16_t) ast_sockaddr_port(&addr);
        media->desc.port_count = 1;
@@ -1039,11 +1266,9 @@ static int create_outgoing_sdp_stream(struct ast_sip_session *session, struct as
                        continue;
                }
 
-               if (!(attr = generate_rtpmap_attr(media, pool, rtp_code, 1, format, 0))) {
-                       ao2_ref(format, -1);
-                       continue;
+               if ((attr = generate_rtpmap_attr(session, media, pool, rtp_code, 1, format, 0))) {
+                       media->attr[media->attr_count++] = attr;
                }
-               media->attr[media->attr_count++] = attr;
 
                if ((attr = generate_fmtp_attr(pool, format, rtp_code))) {
                        media->attr[media->attr_count++] = attr;
@@ -1054,27 +1279,37 @@ static int create_outgoing_sdp_stream(struct ast_sip_session *session, struct as
                        max_packet_size = ast_format_get_maximum_ms(format);
                }
                ao2_ref(format, -1);
+
+               if (media->desc.fmt_count == PJMEDIA_MAX_SDP_FMT) {
+                       break;
+               }
        }
 
        /* Add non-codec formats */
-       if (media_type != AST_MEDIA_TYPE_VIDEO) {
+       if (media_type != AST_MEDIA_TYPE_VIDEO && media->desc.fmt_count < PJMEDIA_MAX_SDP_FMT) {
                for (index = 1LL; index <= AST_RTP_MAX; index <<= 1) {
-                       if (!(noncodec & index) || (rtp_code = ast_rtp_codecs_payload_code(ast_rtp_instance_get_codecs(session_media->rtp),
-                                                                                          0, NULL, index)) == -1) {
+                       if (!(noncodec & index)) {
                                continue;
                        }
-
-                       if (!(attr = generate_rtpmap_attr(media, pool, rtp_code, 0, NULL, index))) {
+                       rtp_code = ast_rtp_codecs_payload_code(
+                               ast_rtp_instance_get_codecs(session_media->rtp), 0, NULL, index);
+                       if (rtp_code == -1) {
                                continue;
                        }
 
-                       media->attr[media->attr_count++] = attr;
+                       if ((attr = generate_rtpmap_attr(session, media, pool, rtp_code, 0, NULL, index))) {
+                               media->attr[media->attr_count++] = attr;
+                       }
 
                        if (index == AST_RTP_DTMF) {
                                snprintf(tmp, sizeof(tmp), "%d 0-16", rtp_code);
                                attr = pjmedia_sdp_attr_create(pool, "fmtp", pj_cstr(&stmp, tmp));
                                media->attr[media->attr_count++] = attr;
                        }
+
+                       if (media->desc.fmt_count == PJMEDIA_MAX_SDP_FMT) {
+                               break;
+                       }
                }
        }
 
@@ -1105,6 +1340,12 @@ static int create_outgoing_sdp_stream(struct ast_sip_session *session, struct as
        attr->name = !session_media->locally_held ? STR_SENDRECV : STR_SENDONLY;
        media->attr[media->attr_count++] = attr;
 
+       /* If we've got rtcp-mux enabled, just unconditionally offer it in all SDPs */
+       if (session->endpoint->media.rtcp_mux) {
+               attr = pjmedia_sdp_attr_create(pool, "rtcp-mux", NULL);
+               pjmedia_sdp_attr_add(&media->attr_count, media->attr, attr);
+       }
+
        /* Add the media stream to the SDP */
        sdp->media[sdp->media_count++] = media;
 
@@ -1115,7 +1356,7 @@ static int apply_negotiated_sdp_stream(struct ast_sip_session *session, struct a
                                       const struct pjmedia_sdp_session *local, const struct pjmedia_sdp_media *local_stream,
                                       const struct pjmedia_sdp_session *remote, const struct pjmedia_sdp_media *remote_stream)
 {
-       RAII_VAR(struct ast_sockaddr *, addrs, NULL, ast_free_ptr);
+       RAII_VAR(struct ast_sockaddr *, addrs, NULL, ast_free);
        enum ast_media_type media_type = stream_to_media_type(session_media->stream_type);
        char host[NI_MAXHOST];
        int fdno, res;
@@ -1135,10 +1376,15 @@ static int apply_negotiated_sdp_stream(struct ast_sip_session *session, struct a
        }
 
        /* Create an RTP instance if need be */
-       if (!session_media->rtp && create_rtp(session, session_media, session->endpoint->media.rtp.ipv6)) {
+       if (!session_media->rtp && create_rtp(session, session_media)) {
                return -1;
        }
 
+       session_media->remote_rtcp_mux = (pjmedia_sdp_media_find_attr2(remote_stream, "rtcp-mux", NULL) != NULL);
+       set_ice_components(session, session_media);
+
+       enable_rtcp(session, session_media, remote_stream);
+
        res = setup_media_encryption(session, session_media, remote, remote_stream);
        if (!session->endpoint->media.rtp.encryption_optimistic && res) {
                /* If optimistic encryption is disabled and crypto should have been enabled but was not
@@ -1162,7 +1408,7 @@ static int apply_negotiated_sdp_stream(struct ast_sip_session *session, struct a
        /* Apply connection information to the RTP instance */
        ast_sockaddr_set_port(addrs, remote_stream->desc.port);
        ast_rtp_instance_set_remote_address(session_media->rtp, addrs);
-       if (set_caps(session, session_media, local_stream)) {
+       if (set_caps(session, session_media, remote_stream, 0)) {
                return 1;
        }
 
@@ -1170,7 +1416,9 @@ static int apply_negotiated_sdp_stream(struct ast_sip_session *session, struct a
                return -1;
        }
        ast_channel_set_fd(session->channel, fdno, ast_rtp_instance_fd(session_media->rtp, 0));
-       ast_channel_set_fd(session->channel, fdno + 1, ast_rtp_instance_fd(session_media->rtp, 1));
+       if (!session->endpoint->media.rtcp_mux || !session_media->remote_rtcp_mux) {
+               ast_channel_set_fd(session->channel, fdno + 1, ast_rtp_instance_fd(session_media->rtp, 1));
+       }
 
        /* If ICE support is enabled find all the needed attributes */
        process_ice_attributes(session, session_media, remote, remote_stream);
@@ -1211,17 +1459,52 @@ static int apply_negotiated_sdp_stream(struct ast_sip_session *session, struct a
        /* This purposely resets the encryption to the configured in case it gets added later */
        session_media->encryption = session->endpoint->media.rtp.encryption;
 
+       if (session->endpoint->media.rtp.keepalive > 0 &&
+                       stream_to_media_type(session_media->stream_type) == AST_MEDIA_TYPE_AUDIO) {
+               ast_rtp_instance_set_keepalive(session_media->rtp, session->endpoint->media.rtp.keepalive);
+               /* Schedule the initial keepalive early in case this is being used to punch holes through
+                * a NAT. This way there won't be an awkward delay before media starts flowing in some
+                * scenarios.
+                */
+               AST_SCHED_DEL(sched, session_media->keepalive_sched_id);
+               session_media->keepalive_sched_id = ast_sched_add_variable(sched, 500, send_keepalive,
+                       session_media, 1);
+       }
+
+       /* As the channel lock is not held during this process the scheduled item won't block if
+        * it is hanging up the channel at the same point we are applying this negotiated SDP.
+        */
+       AST_SCHED_DEL(sched, session_media->timeout_sched_id);
+
+       /* Due to the fact that we only ever have one scheduled timeout item for when we are both
+        * off hold and on hold we don't need to store the two timeouts differently on the RTP
+        * instance itself.
+        */
+       ast_rtp_instance_set_timeout(session_media->rtp, 0);
+       if (session->endpoint->media.rtp.timeout && !session_media->remotely_held) {
+               ast_rtp_instance_set_timeout(session_media->rtp, session->endpoint->media.rtp.timeout);
+       } else if (session->endpoint->media.rtp.timeout_hold && session_media->remotely_held) {
+               ast_rtp_instance_set_timeout(session_media->rtp, session->endpoint->media.rtp.timeout_hold);
+       }
+
+       if (ast_rtp_instance_get_timeout(session_media->rtp)) {
+               session_media->timeout_sched_id = ast_sched_add_variable(sched,
+                       ast_rtp_instance_get_timeout(session_media->rtp) * 1000, rtp_check_timeout,
+                       session_media, 1);
+       }
+
        return 1;
 }
 
 /*! \brief Function which updates the media stream with external media address, if applicable */
 static void change_outgoing_sdp_stream_media_address(pjsip_tx_data *tdata, struct pjmedia_sdp_media *stream, struct ast_sip_transport *transport)
 {
+       RAII_VAR(struct ast_sip_transport_state *, transport_state, ast_sip_get_transport_state(ast_sorcery_object_get_id(transport)), ao2_cleanup);
        char host[NI_MAXHOST];
        struct ast_sockaddr addr = { { 0, } };
 
        /* If the stream has been rejected there will be no connection line */
-       if (!stream->conn) {
+       if (!stream->conn || !transport_state) {
                return;
        }
 
@@ -1229,18 +1512,31 @@ static void change_outgoing_sdp_stream_media_address(pjsip_tx_data *tdata, struc
        ast_sockaddr_parse(&addr, host, PARSE_PORT_FORBID);
 
        /* Is the address within the SDP inside the same network? */
-       if (ast_apply_ha(transport->localnet, &addr) == AST_SENSE_ALLOW) {
+       if (transport_state->localnet
+               && ast_apply_ha(transport_state->localnet, &addr) == AST_SENSE_ALLOW) {
                return;
        }
-
+       ast_debug(5, "Setting media address to %s\n", transport->external_media_address);
        pj_strdup2(tdata->pool, &stream->conn->addr, transport->external_media_address);
 }
 
+/*! \brief Function which stops the RTP instance */
+static void stream_stop(struct ast_sip_session_media *session_media)
+{
+       if (!session_media->rtp) {
+               return;
+       }
+
+       AST_SCHED_DEL(sched, session_media->keepalive_sched_id);
+       AST_SCHED_DEL(sched, session_media->timeout_sched_id);
+       ast_rtp_instance_stop(session_media->rtp);
+}
+
 /*! \brief Function which destroys the RTP instance when session ends */
 static void stream_destroy(struct ast_sip_session_media *session_media)
 {
        if (session_media->rtp) {
-               ast_rtp_instance_stop(session_media->rtp);
+               stream_stop(session_media);
                ast_rtp_instance_destroy(session_media->rtp);
        }
        session_media->rtp = NULL;
@@ -1253,6 +1549,7 @@ static struct ast_sip_session_sdp_handler audio_sdp_handler = {
        .create_outgoing_sdp_stream = create_outgoing_sdp_stream,
        .apply_negotiated_sdp_stream = apply_negotiated_sdp_stream,
        .change_outgoing_sdp_stream_media_address = change_outgoing_sdp_stream_media_address,
+       .stream_stop = stream_stop,
        .stream_destroy = stream_destroy,
 };
 
@@ -1263,20 +1560,24 @@ static struct ast_sip_session_sdp_handler video_sdp_handler = {
        .create_outgoing_sdp_stream = create_outgoing_sdp_stream,
        .apply_negotiated_sdp_stream = apply_negotiated_sdp_stream,
        .change_outgoing_sdp_stream_media_address = change_outgoing_sdp_stream_media_address,
+       .stream_stop = stream_stop,
        .stream_destroy = stream_destroy,
 };
 
 static int video_info_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
 {
-       struct pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
+       struct pjsip_transaction *tsx;
        pjsip_tx_data *tdata;
 
-       if (!ast_sip_is_content_type(&rdata->msg_info.msg->body->content_type,
-                                    "application",
-                                    "media_control+xml")) {
+       if (!session->channel
+               || !ast_sip_is_content_type(&rdata->msg_info.msg->body->content_type,
+                       "application",
+                       "media_control+xml")) {
                return 0;
        }
 
+       tsx = pjsip_rdata_get_tsx(rdata);
+
        ast_queue_control(session->channel, AST_CONTROL_VIDUPDATE);
 
        if (pjsip_dlg_create_response(session->inv_session->dlg, rdata, 200, NULL, &tdata) == PJ_SUCCESS) {
@@ -1319,8 +1620,11 @@ static int load_module(void)
 {
        CHECK_PJSIP_SESSION_MODULE_LOADED();
 
-       ast_sockaddr_parse(&address_ipv4, "0.0.0.0", 0);
-       ast_sockaddr_parse(&address_ipv6, "::", 0);
+       if (ast_check_ipv6()) {
+               ast_sockaddr_parse(&address_rtp, "::", 0);
+       } else {
+               ast_sockaddr_parse(&address_rtp, "0.0.0.0", 0);
+       }
 
        if (!(sched = ast_sched_context_create())) {
                ast_log(LOG_ERROR, "Unable to create scheduler context.\n");
@@ -1348,12 +1652,12 @@ static int load_module(void)
 end:
        unload_module();
 
-       return AST_MODULE_LOAD_FAILURE;
+       return AST_MODULE_LOAD_DECLINE;
 }
 
 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP SDP RTP/AVP stream handler",
-               .support_level = AST_MODULE_SUPPORT_CORE,
-               .load = load_module,
-               .unload = unload_module,
-               .load_pri = AST_MODPRI_CHANNEL_DRIVER,
-       );
+       .support_level = AST_MODULE_SUPPORT_CORE,
+       .load = load_module,
+       .unload = unload_module,
+       .load_pri = AST_MODPRI_CHANNEL_DRIVER,
+);