CHANNEL(callid): Give dialplan access to the callid.
[asterisk/asterisk.git] / main / rtp_engine.c
index 7326adf..931f89d 100644 (file)
                                        and sending this report.</para>
                                </parameter>
                        </syntax>
+                       <see-also>
+                               <ref type="managerEvent">RTCPReceived</ref>
+                       </see-also>
                </managerEventInstance>
        </managerEvent>
        <managerEvent language="en_US" name="RTCPReceived">
                                <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentOctets'])" />
                                <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[contains(@name, 'ReportX')])" />
                        </syntax>
+                       <see-also>
+                               <ref type="managerEvent">RTCPSent</ref>
+                       </see-also>
                </managerEventInstance>
        </managerEvent>
  ***/
 
 #include "asterisk.h"
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
-
-#include <math.h>
-
-#include "asterisk/channel.h"
-#include "asterisk/frame.h"
-#include "asterisk/module.h"
-#include "asterisk/rtp_engine.h"
+#include <math.h>                       /* for sqrt, MAX */
+#include <sched.h>                      /* for sched_yield */
+#include <sys/time.h>                   /* for timeval */
+#include <time.h>                       /* for time_t */
+
+#include "asterisk/_private.h"          /* for ast_rtp_engine_init prototype */
+#include "asterisk/astobj2.h"           /* for ao2_cleanup, ao2_ref, etc */
+#include "asterisk/channel.h"           /* for ast_channel_name, etc */
+#include "asterisk/codec.h"             /* for ast_codec_media_type2str, etc */
+#include "asterisk/format.h"            /* for ast_format_cmp, etc */
+#include "asterisk/format_cache.h"      /* for ast_format_adpcm, etc */
+#include "asterisk/format_cap.h"        /* for ast_format_cap_alloc, etc */
+#include "asterisk/json.h"              /* for ast_json_ref, etc */
+#include "asterisk/linkedlists.h"       /* for ast_rtp_engine::<anonymous>, etc */
+#include "asterisk/lock.h"              /* for ast_rwlock_unlock, etc */
+#include "asterisk/logger.h"            /* for ast_log, ast_debug, etc */
 #include "asterisk/manager.h"
-#include "asterisk/options.h"
-#include "asterisk/astobj2.h"
-#include "asterisk/pbx.h"
-#include "asterisk/translate.h"
-#include "asterisk/netsock2.h"
-#include "asterisk/_private.h"
-#include "asterisk/framehook.h"
-#include "asterisk/stasis.h"
-#include "asterisk/json.h"
-#include "asterisk/stasis_channels.h"
+#include "asterisk/module.h"            /* for ast_module_unref, etc */
+#include "asterisk/netsock2.h"          /* for ast_sockaddr_copy, etc */
+#include "asterisk/options.h"           /* for ast_option_rtpptdynamic */
+#include "asterisk/pbx.h"               /* for pbx_builtin_setvar_helper */
+#include "asterisk/res_srtp.h"          /* for ast_srtp_res */
+#include "asterisk/rtp_engine.h"        /* for ast_rtp_codecs, etc */
+#include "asterisk/stasis.h"            /* for stasis_message_data, etc */
+#include "asterisk/stasis_channels.h"   /* for ast_channel_stage_snapshot, etc */
+#include "asterisk/strings.h"           /* for ast_str_append, etc */
+#include "asterisk/time.h"              /* for ast_tvdiff_ms, ast_tvnow */
+#include "asterisk/translate.h"         /* for ast_translate_available_formats */
+#include "asterisk/utils.h"             /* for ast_free, ast_strdup, etc */
+#include "asterisk/vector.h"            /* for AST_VECTOR_GET, etc */
 
 struct ast_srtp_res *res_srtp = NULL;
 struct ast_srtp_policy_res *res_srtp_policy = NULL;
@@ -188,8 +205,14 @@ struct ast_rtp_instance {
        struct ast_rtp_glue *glue;
        /*! SRTP info associated with the instance */
        struct ast_srtp *srtp;
+       /*! SRTP info dedicated for RTCP associated with the instance */
+       struct ast_srtp *rtcp_srtp;
        /*! Channel unique ID */
        char channel_uniqueid[AST_MAX_UNIQUEID];
+       /*! Time of last packet sent */
+       time_t last_tx;
+       /*! Time of last packet received */
+       time_t last_rx;
 };
 
 /*! List of RTP engines that are currently registered */
@@ -225,14 +248,17 @@ static int mime_types_len = 0;
  * See http://www.iana.org/assignments/rtp-parameters for a list of
  * assigned values
  */
-static struct ast_rtp_payload_type static_RTP_PT[AST_RTP_MAX_PT];
+static struct ast_rtp_payload_type *static_RTP_PT[AST_RTP_MAX_PT];
 static ast_rwlock_t static_RTP_PT_lock;
 
 /*! \brief \ref stasis topic for RTP related messages */
 static struct stasis_topic *rtp_topic;
 
 
-/*! \internal \brief Destructor for \c ast_rtp_payload_type */
+/*!
+ * \internal
+ * \brief Destructor for \c ast_rtp_payload_type
+ */
 static void rtp_payload_type_dtor(void *obj)
 {
        struct ast_rtp_payload_type *payload = obj;
@@ -244,7 +270,8 @@ struct ast_rtp_payload_type *ast_rtp_engine_alloc_payload_type(void)
 {
        struct ast_rtp_payload_type *payload;
 
-       payload = ao2_alloc(sizeof(*payload), rtp_payload_type_dtor);
+       payload = ao2_alloc_options(sizeof(*payload), rtp_payload_type_dtor,
+               AO2_ALLOC_OPT_LOCK_NOLOCK);
 
        return payload;
 }
@@ -356,6 +383,10 @@ static void instance_destructor(void *obj)
                res_srtp->destroy(instance->srtp);
        }
 
+       if (instance->rtcp_srtp) {
+               res_srtp->destroy(instance->rtcp_srtp);
+       }
+
        ast_rtp_codecs_payloads_destroy(&instance->codecs);
 
        /* Drop our engine reference */
@@ -572,22 +603,32 @@ int ast_rtp_codecs_payloads_initialize(struct ast_rtp_codecs *codecs)
 
        codecs->framing = 0;
        ast_rwlock_init(&codecs->codecs_lock);
-       res = AST_VECTOR_INIT(&codecs->payloads, AST_RTP_MAX_PT);
+       res = AST_VECTOR_INIT(&codecs->payload_mapping_rx, AST_RTP_MAX_PT);
+       res |= AST_VECTOR_INIT(&codecs->payload_mapping_tx, AST_RTP_MAX_PT);
+       if (res) {
+               AST_VECTOR_FREE(&codecs->payload_mapping_rx);
+               AST_VECTOR_FREE(&codecs->payload_mapping_tx);
+       }
 
        return res;
 }
 
 void ast_rtp_codecs_payloads_destroy(struct ast_rtp_codecs *codecs)
 {
-       int i;
+       int idx;
+       struct ast_rtp_payload_type *type;
 
-       for (i = 0; i < AST_VECTOR_SIZE(&codecs->payloads); i++) {
-               struct ast_rtp_payload_type *type;
+       for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_rx); ++idx) {
+               type = AST_VECTOR_GET(&codecs->payload_mapping_rx, idx);
+               ao2_t_cleanup(type, "destroying ast_rtp_codec rx mapping");
+       }
+       AST_VECTOR_FREE(&codecs->payload_mapping_rx);
 
-               type = AST_VECTOR_GET(&codecs->payloads, i);
-               ao2_t_cleanup(type, "destroying ast_rtp_codec");
+       for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_tx); ++idx) {
+               type = AST_VECTOR_GET(&codecs->payload_mapping_tx, idx);
+               ao2_t_cleanup(type, "destroying ast_rtp_codec tx mapping");
        }
-       AST_VECTOR_FREE(&codecs->payloads);
+       AST_VECTOR_FREE(&codecs->payload_mapping_tx);
 
        ast_rwlock_destroy(&codecs->codecs_lock);
 }
@@ -595,6 +636,7 @@ void ast_rtp_codecs_payloads_destroy(struct ast_rtp_codecs *codecs)
 void ast_rtp_codecs_payloads_clear(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
 {
        ast_rtp_codecs_payloads_destroy(codecs);
+       ast_rtp_codecs_payloads_initialize(codecs);
 
        if (instance && instance->engine && instance->engine->payload_set) {
                int i;
@@ -602,74 +644,324 @@ void ast_rtp_codecs_payloads_clear(struct ast_rtp_codecs *codecs, struct ast_rtp
                        instance->engine->payload_set(instance, i, 0, NULL, 0);
                }
        }
+}
 
-       ast_rtp_codecs_payloads_initialize(codecs);
+/*!
+ * \internal
+ * \brief Clear the rx primary mapping flag on all other matching mappings.
+ * \since 14.0.0
+ *
+ * \param codecs Codecs that need rx clearing.
+ * \param to_match Payload type object to compare against.
+ *
+ * \note It is assumed that codecs is write locked before calling.
+ *
+ * \return Nothing
+ */
+static void payload_mapping_rx_clear_primary(struct ast_rtp_codecs *codecs, struct ast_rtp_payload_type *to_match)
+{
+       int idx;
+       struct ast_rtp_payload_type *current;
+       struct ast_rtp_payload_type *new_type;
+       struct timeval now;
+
+       if (!to_match->primary_mapping) {
+               return;
+       }
+
+       now = ast_tvnow();
+       for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_rx); ++idx) {
+               current = AST_VECTOR_GET(&codecs->payload_mapping_rx, idx);
+
+               if (!current || current == to_match || !current->primary_mapping) {
+                       continue;
+               }
+               if (current->asterisk_format && to_match->asterisk_format) {
+                       if (ast_format_cmp(current->format, to_match->format) == AST_FORMAT_CMP_NOT_EQUAL) {
+                               continue;
+                       }
+               } else if (!current->asterisk_format && !to_match->asterisk_format) {
+                       if (current->rtp_code != to_match->rtp_code) {
+                               continue;
+                       }
+               } else {
+                       continue;
+               }
+
+               /* Replace current with non-primary marked version */
+               new_type = ast_rtp_engine_alloc_payload_type();
+               if (!new_type) {
+                       continue;
+               }
+               *new_type = *current;
+               new_type->primary_mapping = 0;
+               new_type->when_retired = now;
+               ao2_bump(new_type->format);
+               AST_VECTOR_REPLACE(&codecs->payload_mapping_rx, idx, new_type);
+               ao2_ref(current, -1);
+       }
+}
+
+/*!
+ * \internal
+ * \brief Put the new_type into the rx payload type mapping.
+ * \since 14.0.0
+ *
+ * \param codecs Codecs structure to put new_type into
+ * \param payload type position to replace.
+ * \param new_type RTP payload mapping object to store.
+ *
+ * \note It is assumed that codecs is write locked before calling.
+ *
+ * \return Nothing
+ */
+static void rtp_codecs_payload_replace_rx(struct ast_rtp_codecs *codecs, int payload, struct ast_rtp_payload_type *new_type)
+{
+       ao2_ref(new_type, +1);
+       if (payload < AST_VECTOR_SIZE(&codecs->payload_mapping_rx)) {
+               ao2_t_cleanup(AST_VECTOR_GET(&codecs->payload_mapping_rx, payload),
+                       "cleaning up rx mapping vector element about to be replaced");
+       }
+       AST_VECTOR_REPLACE(&codecs->payload_mapping_rx, payload, new_type);
+
+       payload_mapping_rx_clear_primary(codecs, new_type);
+}
+
+/*!
+ * \internal
+ * \brief Copy the rx payload type mapping to the destination.
+ * \since 14.0.0
+ *
+ * \param src The source codecs structure
+ * \param dest The destination codecs structure that the values from src will be copied to
+ * \param instance Optionally the instance that the dst codecs structure belongs to
+ *
+ * \note It is assumed that src is at least read locked before calling.
+ * \note It is assumed that dest is write locked before calling.
+ *
+ * \return Nothing
+ */
+static void rtp_codecs_payloads_copy_rx(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
+{
+       int idx;
+       struct ast_rtp_payload_type *type;
+
+       for (idx = 0; idx < AST_VECTOR_SIZE(&src->payload_mapping_rx); ++idx) {
+               type = AST_VECTOR_GET(&src->payload_mapping_rx, idx);
+               if (!type) {
+                       continue;
+               }
+
+               ast_debug(2, "Copying rx payload mapping %d (%p) from %p to %p\n",
+                       idx, type, src, dest);
+               rtp_codecs_payload_replace_rx(dest, idx, type);
+
+               if (instance && instance->engine && instance->engine->payload_set) {
+                       instance->engine->payload_set(instance, idx, type->asterisk_format, type->format, type->rtp_code);
+               }
+       }
+}
+
+/*!
+ * \internal
+ * \brief Determine if a type of payload is already present in mappings.
+ * \since 14.0.0
+ *
+ * \param codecs Codecs to be checked for mappings.
+ * \param to_match Payload type object to compare against.
+ *
+ * \note It is assumed that codecs is write locked before calling.
+ *
+ * \retval 0 not found
+ * \retval 1 found
+ */
+static int payload_mapping_tx_is_present(const struct ast_rtp_codecs *codecs, const struct ast_rtp_payload_type *to_match)
+{
+       int idx;
+       struct ast_rtp_payload_type *current;
+
+       for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_tx); ++idx) {
+               current = AST_VECTOR_GET(&codecs->payload_mapping_tx, idx);
+
+               if (!current) {
+                       continue;
+               }
+               if (current == to_match) {
+                       /* The exact object is already in the mapping. */
+                       return 1;
+               }
+               if (current->asterisk_format && to_match->asterisk_format) {
+                       if (ast_format_get_codec_id(current->format) != ast_format_get_codec_id(to_match->format)) {
+                               continue;
+                       } else if (current->payload == to_match->payload) {
+                               return 0;
+                       }
+               } else if (!current->asterisk_format && !to_match->asterisk_format) {
+                       if (current->rtp_code != to_match->rtp_code) {
+                               continue;
+                       }
+               } else {
+                       continue;
+               }
+
+               return 1;
+       }
+
+       return 0;
+}
+
+/*!
+ * \internal
+ * \brief Copy the tx payload type mapping to the destination.
+ * \since 14.0.0
+ *
+ * \param src The source codecs structure
+ * \param dest The destination codecs structure that the values from src will be copied to
+ * \param instance Optionally the instance that the dst codecs structure belongs to
+ *
+ * \note It is assumed that src is at least read locked before calling.
+ * \note It is assumed that dest is write locked before calling.
+ *
+ * \return Nothing
+ */
+static void rtp_codecs_payloads_copy_tx(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
+{
+       int idx;
+       struct ast_rtp_payload_type *type;
+
+       for (idx = 0; idx < AST_VECTOR_SIZE(&src->payload_mapping_tx); ++idx) {
+               type = AST_VECTOR_GET(&src->payload_mapping_tx, idx);
+               if (!type) {
+                       continue;
+               }
+
+               ast_debug(2, "Copying tx payload mapping %d (%p) from %p to %p\n",
+                       idx, type, src, dest);
+               ao2_ref(type, +1);
+               if (idx < AST_VECTOR_SIZE(&dest->payload_mapping_tx)) {
+                       ao2_t_cleanup(AST_VECTOR_GET(&dest->payload_mapping_tx, idx),
+                               "cleaning up tx mapping vector element about to be replaced");
+               }
+               AST_VECTOR_REPLACE(&dest->payload_mapping_tx, idx, type);
+
+               if (instance && instance->engine && instance->engine->payload_set) {
+                       instance->engine->payload_set(instance, idx, type->asterisk_format, type->format, type->rtp_code);
+               }
+       }
 }
 
 void ast_rtp_codecs_payloads_copy(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
 {
-       int i;
+       int idx;
+       struct ast_rtp_payload_type *type;
 
-       ast_rwlock_rdlock(&src->codecs_lock);
        ast_rwlock_wrlock(&dest->codecs_lock);
 
-       for (i = 0; i < AST_VECTOR_SIZE(&src->payloads); i++) {
-               struct ast_rtp_payload_type *type;
+       /* Deadlock avoidance because of held write lock. */
+       while (ast_rwlock_tryrdlock(&src->codecs_lock)) {
+               ast_rwlock_unlock(&dest->codecs_lock);
+               sched_yield();
+               ast_rwlock_wrlock(&dest->codecs_lock);
+       }
+
+       /*
+        * This represents a completely new mapping of what the remote party is
+        * expecting for payloads, so we clear out the entire tx payload mapping
+        * vector and replace it.
+        */
+       for (idx = 0; idx < AST_VECTOR_SIZE(&dest->payload_mapping_tx); ++idx) {
+               type = AST_VECTOR_GET(&dest->payload_mapping_tx, idx);
+               ao2_t_cleanup(type, "destroying ast_rtp_codec tx mapping");
+               AST_VECTOR_REPLACE(&dest->payload_mapping_tx, idx, NULL);
+       }
+
+       rtp_codecs_payloads_copy_rx(src, dest, instance);
+       rtp_codecs_payloads_copy_tx(src, dest, instance);
+       dest->framing = src->framing;
 
-               type = AST_VECTOR_GET(&src->payloads, i);
+       ast_rwlock_unlock(&src->codecs_lock);
+       ast_rwlock_unlock(&dest->codecs_lock);
+}
+
+void ast_rtp_codecs_payloads_xover(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
+{
+       int idx;
+       struct ast_rtp_payload_type *type;
+
+       ast_rwlock_wrlock(&dest->codecs_lock);
+       if (src != dest) {
+               /* Deadlock avoidance because of held write lock. */
+               while (ast_rwlock_tryrdlock(&src->codecs_lock)) {
+                       ast_rwlock_unlock(&dest->codecs_lock);
+                       sched_yield();
+                       ast_rwlock_wrlock(&dest->codecs_lock);
+               }
+       }
+
+       /* Crossover copy payload type tx mapping to rx mapping. */
+       for (idx = 0; idx < AST_VECTOR_SIZE(&src->payload_mapping_tx); ++idx) {
+               type = AST_VECTOR_GET(&src->payload_mapping_tx, idx);
                if (!type) {
                        continue;
                }
-               if (i < AST_VECTOR_SIZE(&dest->payloads)) {
-                       ao2_t_cleanup(AST_VECTOR_GET(&dest->payloads, i), "cleaning up vector element about to be replaced");
-               }
-               ast_debug(2, "Copying payload %d (%p) from %p to %p\n", i, type, src, dest);
-               ao2_bump(type);
-               AST_VECTOR_INSERT(&dest->payloads, i, type);
+
+               /* All tx mapping elements should have the primary flag set. */
+               ast_assert(type->primary_mapping);
+
+               ast_debug(2, "Crossover copying tx to rx payload mapping %d (%p) from %p to %p\n",
+                       idx, type, src, dest);
+               rtp_codecs_payload_replace_rx(dest, idx, type);
 
                if (instance && instance->engine && instance->engine->payload_set) {
-                       instance->engine->payload_set(instance, i, type->asterisk_format, type->format, type->rtp_code);
+                       instance->engine->payload_set(instance, idx, type->asterisk_format, type->format, type->rtp_code);
                }
        }
+
        dest->framing = src->framing;
+
+       if (src != dest) {
+               ast_rwlock_unlock(&src->codecs_lock);
+       }
        ast_rwlock_unlock(&dest->codecs_lock);
-       ast_rwlock_unlock(&src->codecs_lock);
 }
 
 void ast_rtp_codecs_payloads_set_m_type(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
 {
        struct ast_rtp_payload_type *new_type;
 
-       new_type = ast_rtp_engine_alloc_payload_type();
-       if (!new_type) {
+       if (payload < 0 || payload >= AST_RTP_MAX_PT) {
                return;
        }
 
        ast_rwlock_rdlock(&static_RTP_PT_lock);
-       if (payload < 0 || payload >= AST_RTP_MAX_PT) {
-               ast_rwlock_unlock(&static_RTP_PT_lock);
+       new_type = ao2_bump(static_RTP_PT[payload]);
+       ast_rwlock_unlock(&static_RTP_PT_lock);
+       if (!new_type) {
+               ast_debug(1, "Don't have a default tx payload type %d format for m type on %p\n",
+                       payload, codecs);
                return;
        }
 
-       ast_rwlock_wrlock(&codecs->codecs_lock);
-       if (payload < AST_VECTOR_SIZE(&codecs->payloads)) {
-               ao2_t_cleanup(AST_VECTOR_GET(&codecs->payloads, payload), "cleaning up replaced payload type");
-       }
+       ast_debug(1, "Setting tx payload type %d based on m type on %p\n",
+               payload, codecs);
 
-       new_type->asterisk_format = static_RTP_PT[payload].asterisk_format;
-       new_type->rtp_code = static_RTP_PT[payload].rtp_code;
-       new_type->payload = payload;
-       new_type->format = ao2_bump(static_RTP_PT[payload].format);
+       ast_rwlock_wrlock(&codecs->codecs_lock);
 
-       ast_debug(1, "Setting payload %d (%p) based on m type on %p\n", payload, new_type, codecs);
-       AST_VECTOR_INSERT(&codecs->payloads, payload, new_type);
+       if (!payload_mapping_tx_is_present(codecs, new_type)) {
+               if (payload < AST_VECTOR_SIZE(&codecs->payload_mapping_tx)) {
+                       ao2_t_cleanup(AST_VECTOR_GET(&codecs->payload_mapping_tx, payload),
+                               "cleaning up replaced tx payload type");
+               }
+               AST_VECTOR_REPLACE(&codecs->payload_mapping_tx, payload, new_type);
 
-       if (instance && instance->engine && instance->engine->payload_set) {
-               instance->engine->payload_set(instance, payload, new_type->asterisk_format, new_type->format, new_type->rtp_code);
+               if (instance && instance->engine && instance->engine->payload_set) {
+                       instance->engine->payload_set(instance, payload, new_type->asterisk_format, new_type->format, new_type->rtp_code);
+               }
+       } else {
+               ao2_ref(new_type, -1);
        }
 
        ast_rwlock_unlock(&codecs->codecs_lock);
-       ast_rwlock_unlock(&static_RTP_PT_lock);
 }
 
 int ast_rtp_codecs_payloads_set_rtpmap_type_rate(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int pt,
@@ -677,18 +969,18 @@ int ast_rtp_codecs_payloads_set_rtpmap_type_rate(struct ast_rtp_codecs *codecs,
                                 enum ast_rtp_options options,
                                 unsigned int sample_rate)
 {
-       unsigned int i;
+       unsigned int idx;
        int found = 0;
 
-       ast_rwlock_rdlock(&mime_types_lock);
        if (pt < 0 || pt >= AST_RTP_MAX_PT) {
-               ast_rwlock_unlock(&mime_types_lock);
                return -1; /* bogus payload type */
        }
 
+       ast_rwlock_rdlock(&mime_types_lock);
        ast_rwlock_wrlock(&codecs->codecs_lock);
-       for (i = 0; i < mime_types_len; ++i) {
-               const struct ast_rtp_mime_type *t = &ast_rtp_mime_types[i];
+
+       for (idx = 0; idx < mime_types_len; ++idx) {
+               const struct ast_rtp_mime_type *t = &ast_rtp_mime_types[idx];
                struct ast_rtp_payload_type *new_type;
 
                if (strcasecmp(mimesubtype, t->subtype)) {
@@ -714,27 +1006,40 @@ int ast_rtp_codecs_payloads_set_rtpmap_type_rate(struct ast_rtp_codecs *codecs,
                        continue;
                }
 
-               if (pt < AST_VECTOR_SIZE(&codecs->payloads)) {
-                       ao2_t_cleanup(AST_VECTOR_GET(&codecs->payloads, pt), "cleaning up replaced payload type");
-               }
-
-               new_type->payload = pt;
                new_type->asterisk_format = t->payload_type.asterisk_format;
                new_type->rtp_code = t->payload_type.rtp_code;
-               if ((ast_format_cmp(t->payload_type.format, ast_format_g726) == AST_FORMAT_CMP_EQUAL) &&
-                               t->payload_type.asterisk_format && (options & AST_RTP_OPT_G726_NONSTANDARD)) {
-                       new_type->format = ao2_bump(ast_format_g726_aal2);
+               new_type->payload = pt;
+               new_type->primary_mapping = 1;
+               if (t->payload_type.asterisk_format
+                       && ast_format_cmp(t->payload_type.format, ast_format_g726) == AST_FORMAT_CMP_EQUAL
+                       && (options & AST_RTP_OPT_G726_NONSTANDARD)) {
+                       new_type->format = ast_format_g726_aal2;
                } else {
-                       new_type->format = ao2_bump(t->payload_type.format);
+                       new_type->format = t->payload_type.format;
                }
-               AST_VECTOR_INSERT(&codecs->payloads, pt, new_type);
 
-               if (instance && instance->engine && instance->engine->payload_set) {
-                       instance->engine->payload_set(instance, pt, new_type->asterisk_format, new_type->format, new_type->rtp_code);
+               if (new_type->format) {
+                       /* SDP parsing automatically increases the reference count */
+                       new_type->format = ast_format_parse_sdp_fmtp(new_type->format, "");
+               }
+
+               if (!payload_mapping_tx_is_present(codecs, new_type)) {
+                       if (pt < AST_VECTOR_SIZE(&codecs->payload_mapping_tx)) {
+                               ao2_t_cleanup(AST_VECTOR_GET(&codecs->payload_mapping_tx, pt),
+                                       "cleaning up replaced tx payload type");
+                       }
+                       AST_VECTOR_REPLACE(&codecs->payload_mapping_tx, pt, new_type);
+
+                       if (instance && instance->engine && instance->engine->payload_set) {
+                               instance->engine->payload_set(instance, pt, new_type->asterisk_format, new_type->format, new_type->rtp_code);
+                       }
+               } else {
+                       ao2_ref(new_type, -1);
                }
 
                break;
        }
+
        ast_rwlock_unlock(&codecs->codecs_lock);
        ast_rwlock_unlock(&mime_types_lock);
 
@@ -757,10 +1062,11 @@ void ast_rtp_codecs_payloads_unset(struct ast_rtp_codecs *codecs, struct ast_rtp
        ast_debug(2, "Unsetting payload %d on %p\n", payload, codecs);
 
        ast_rwlock_wrlock(&codecs->codecs_lock);
-       if (payload < AST_VECTOR_SIZE(&codecs->payloads)) {
-               type = AST_VECTOR_GET(&codecs->payloads, payload);
+
+       if (payload < AST_VECTOR_SIZE(&codecs->payload_mapping_tx)) {
+               type = AST_VECTOR_GET(&codecs->payload_mapping_tx, payload);
                ao2_cleanup(type);
-               AST_VECTOR_INSERT(&codecs->payloads, payload, NULL);
+               AST_VECTOR_REPLACE(&codecs->payload_mapping_tx, payload, NULL);
        }
 
        if (instance && instance->engine && instance->engine->payload_set) {
@@ -779,22 +1085,15 @@ struct ast_rtp_payload_type *ast_rtp_codecs_get_payload(struct ast_rtp_codecs *c
        }
 
        ast_rwlock_rdlock(&codecs->codecs_lock);
-       if (payload < AST_VECTOR_SIZE(&codecs->payloads)) {
-               type = AST_VECTOR_GET(&codecs->payloads, payload);
+       if (payload < AST_VECTOR_SIZE(&codecs->payload_mapping_rx)) {
+               type = AST_VECTOR_GET(&codecs->payload_mapping_rx, payload);
                ao2_bump(type);
        }
        ast_rwlock_unlock(&codecs->codecs_lock);
 
        if (!type) {
-               type = ast_rtp_engine_alloc_payload_type();
-               if (!type) {
-                       return NULL;
-               }
                ast_rwlock_rdlock(&static_RTP_PT_lock);
-               type->asterisk_format = static_RTP_PT[payload].asterisk_format;
-               type->rtp_code = static_RTP_PT[payload].rtp_code;
-               type->payload = payload;
-               type->format = ao2_bump(static_RTP_PT[payload].format);
+               type = ao2_bump(static_RTP_PT[payload]);
                ast_rwlock_unlock(&static_RTP_PT_lock);
        }
 
@@ -805,16 +1104,28 @@ int ast_rtp_codecs_payload_replace_format(struct ast_rtp_codecs *codecs, int pay
 {
        struct ast_rtp_payload_type *type;
 
-       if (payload < 0 || payload >= AST_RTP_MAX_PT) {
+       if (payload < 0 || payload >= AST_RTP_MAX_PT || !format) {
+               return -1;
+       }
+
+       type = ast_rtp_engine_alloc_payload_type();
+       if (!type) {
                return -1;
        }
+       ao2_ref(format, +1);
+       type->format = format;
+       type->asterisk_format = 1;
+       type->payload = payload;
+       type->primary_mapping = 1;
 
        ast_rwlock_wrlock(&codecs->codecs_lock);
-       if (payload < AST_VECTOR_SIZE(&codecs->payloads)) {
-               type = AST_VECTOR_GET(&codecs->payloads, payload);
-               if (type && type->asterisk_format) {
-                       ao2_replace(type->format, format);
+       if (!payload_mapping_tx_is_present(codecs, type)) {
+               if (payload < AST_VECTOR_SIZE(&codecs->payload_mapping_tx)) {
+                       ao2_cleanup(AST_VECTOR_GET(&codecs->payload_mapping_tx, payload));
                }
+               AST_VECTOR_REPLACE(&codecs->payload_mapping_tx, payload, type);
+       } else {
+               ao2_ref(type, -1);
        }
        ast_rwlock_unlock(&codecs->codecs_lock);
 
@@ -831,8 +1142,8 @@ struct ast_format *ast_rtp_codecs_get_payload_format(struct ast_rtp_codecs *code
        }
 
        ast_rwlock_rdlock(&codecs->codecs_lock);
-       if (payload < AST_VECTOR_SIZE(&codecs->payloads)) {
-               type = AST_VECTOR_GET(&codecs->payloads, payload);
+       if (payload < AST_VECTOR_SIZE(&codecs->payload_mapping_tx)) {
+               type = AST_VECTOR_GET(&codecs->payload_mapping_tx, payload);
                if (type && type->asterisk_format) {
                        format = ao2_bump(type->format);
                }
@@ -866,16 +1177,17 @@ unsigned int ast_rtp_codecs_get_framing(struct ast_rtp_codecs *codecs)
 
 void ast_rtp_codecs_payload_formats(struct ast_rtp_codecs *codecs, struct ast_format_cap *astformats, int *nonastformats)
 {
-       int i;
+       int idx;
 
        ast_format_cap_remove_by_type(astformats, AST_MEDIA_TYPE_UNKNOWN);
        *nonastformats = 0;
 
        ast_rwlock_rdlock(&codecs->codecs_lock);
-       for (i = 0; i < AST_VECTOR_SIZE(&codecs->payloads); i++) {
+
+       for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_tx); ++idx) {
                struct ast_rtp_payload_type *type;
 
-               type = AST_VECTOR_GET(&codecs->payloads, i);
+               type = AST_VECTOR_GET(&codecs->payload_mapping_tx, idx);
                if (!type) {
                        continue;
                }
@@ -886,7 +1198,6 @@ void ast_rtp_codecs_payload_formats(struct ast_rtp_codecs *codecs, struct ast_fo
                        *nonastformats |= type->rtp_code;
                }
        }
-
        if (codecs->framing) {
                ast_format_cap_set_framing(astformats, codecs->framing);
        }
@@ -894,56 +1205,287 @@ void ast_rtp_codecs_payload_formats(struct ast_rtp_codecs *codecs, struct ast_fo
        ast_rwlock_unlock(&codecs->codecs_lock);
 }
 
-int ast_rtp_codecs_payload_code(struct ast_rtp_codecs *codecs, int asterisk_format, const struct ast_format *format, int code)
+/*!
+ * \internal
+ * \brief Find the static payload type mapping for the format.
+ * \since 14.0.0
+ *
+ * \param asterisk_format Non-zero if the given Asterisk format is present
+ * \param format Asterisk format to look for
+ * \param code The non-Asterisk format code to look for
+ *
+ * \note It is assumed that static_RTP_PT_lock is at least read locked before calling.
+ *
+ * \retval Numerical payload type
+ * \retval -1 if not found.
+ */
+static int find_static_payload_type(int asterisk_format, const struct ast_format *format, int code)
+{
+       int idx;
+       int payload = -1;
+
+       if (!asterisk_format) {
+               for (idx = 0; idx < AST_RTP_MAX_PT; ++idx) {
+                       if (static_RTP_PT[idx]
+                               && !static_RTP_PT[idx]->asterisk_format
+                               && static_RTP_PT[idx]->rtp_code == code) {
+                               payload = idx;
+                               break;
+                       }
+               }
+       } else if (format) {
+               for (idx = 0; idx < AST_RTP_MAX_PT; ++idx) {
+                       if (static_RTP_PT[idx]
+                               && static_RTP_PT[idx]->asterisk_format
+                               && ast_format_cmp(format, static_RTP_PT[idx]->format)
+                                       != AST_FORMAT_CMP_NOT_EQUAL) {
+                               payload = idx;
+                               break;
+                       }
+               }
+       }
+
+       return payload;
+}
+
+/*!
+ * \internal
+ * \brief Find the first unused dynamic rx payload type.
+ * \since 14.0.0
+ *
+ * \param codecs Codecs structure to look in
+ *
+ * \note It is assumed that codecs is at least read locked before calling.
+ *
+ * \retval Numerical payload type
+ * \retval -1 if not found.
+ */
+static int rtp_codecs_find_empty_dynamic_rx(struct ast_rtp_codecs *codecs)
 {
        struct ast_rtp_payload_type *type;
-       int i;
+       int idx;
        int payload = -1;
 
-       ast_rwlock_rdlock(&codecs->codecs_lock);
-       for (i = 0; i < AST_VECTOR_SIZE(&codecs->payloads); i++) {
-               type = AST_VECTOR_GET(&codecs->payloads, i);
+       idx = AST_RTP_PT_FIRST_DYNAMIC;
+       for (; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_rx); ++idx) {
+               type = AST_VECTOR_GET(&codecs->payload_mapping_rx, idx);
                if (!type) {
-                       continue;
+                       payload = idx;
+                       break;
                }
+       }
+       return payload;
+}
 
-               if ((asterisk_format && format && ast_format_cmp(format, type->format) == AST_FORMAT_CMP_EQUAL)
-                       || (!asterisk_format && type->rtp_code == code)) {
-                       payload = i;
-                       break;
+/*!
+ * \internal
+ * \brief Find the oldest non-primary dynamic rx payload type.
+ * \since 14.0.0
+ *
+ * \param codecs Codecs structure to look in
+ *
+ * \note It is assumed that codecs is at least read locked before calling.
+ *
+ * \retval Numerical payload type
+ * \retval -1 if not found.
+ */
+static int rtp_codecs_find_non_primary_dynamic_rx(struct ast_rtp_codecs *codecs)
+{
+       struct ast_rtp_payload_type *type;
+       struct timeval oldest;
+       int idx;
+       int payload = -1;
+
+       idx = AST_RTP_PT_FIRST_DYNAMIC;
+       for (; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_rx); ++idx) {
+               type = AST_VECTOR_GET(&codecs->payload_mapping_rx, idx);
+               if (type
+                       && !type->primary_mapping
+                       && (payload == -1
+                               || ast_tvdiff_ms(type->when_retired, oldest) < 0)) {
+                       oldest = type->when_retired;
+                       payload = idx;
                }
        }
+       return payload;
+}
+
+/*!
+ * \internal
+ * \brief Assign a payload type for the rx mapping.
+ * \since 14.0.0
+ *
+ * \param codecs Codecs structure to look in
+ * \param asterisk_format Non-zero if the given Asterisk format is present
+ * \param format Asterisk format to look for
+ * \param code The format to look for
+ *
+ * \note It is assumed that static_RTP_PT_lock is at least read locked before calling.
+ *
+ * \retval Numerical payload type
+ * \retval -1 if could not assign.
+ */
+static int rtp_codecs_assign_payload_code_rx(struct ast_rtp_codecs *codecs, int asterisk_format, struct ast_format *format, int code)
+{
+       int payload;
+       struct ast_rtp_payload_type *new_type;
+
+       payload = find_static_payload_type(asterisk_format, format, code);
+       if (payload < 0) {
+               return payload;
+       }
+
+       new_type = ast_rtp_engine_alloc_payload_type();
+       if (!new_type) {
+               return -1;
+       }
+       new_type->format = ao2_bump(format);
+       new_type->asterisk_format = asterisk_format;
+       new_type->rtp_code = code;
+       new_type->payload = payload;
+       new_type->primary_mapping = 1;
+
+       ast_rwlock_wrlock(&codecs->codecs_lock);
+       if (payload < AST_RTP_PT_FIRST_DYNAMIC
+               || AST_VECTOR_SIZE(&codecs->payload_mapping_rx) <= payload
+               || !AST_VECTOR_GET(&codecs->payload_mapping_rx, payload)) {
+               /*
+                * The payload type is a static assignment
+                * or our default dynamic position is available.
+                */
+               rtp_codecs_payload_replace_rx(codecs, payload, new_type);
+       } else if (-1 < (payload = rtp_codecs_find_empty_dynamic_rx(codecs))
+               || -1 < (payload = rtp_codecs_find_non_primary_dynamic_rx(codecs))) {
+               /*
+                * We found the first available empty dynamic position
+                * or we found a mapping that should no longer be
+                * actively used.
+                */
+               new_type->payload = payload;
+               rtp_codecs_payload_replace_rx(codecs, payload, new_type);
+       } else {
+               /*
+                * There are no empty or non-primary dynamic positions
+                * left.  Sadness.
+                *
+                * I don't think this is really possible.
+                */
+               ast_log(LOG_WARNING, "No dynamic RTP payload type values available!\n");
+       }
        ast_rwlock_unlock(&codecs->codecs_lock);
 
+       ao2_ref(new_type, -1);
+
+       return payload;
+}
+
+int ast_rtp_codecs_payload_code(struct ast_rtp_codecs *codecs, int asterisk_format, struct ast_format *format, int code)
+{
+       struct ast_rtp_payload_type *type;
+       int idx;
+       int payload = -1;
+
+       ast_rwlock_rdlock(&static_RTP_PT_lock);
+       if (!asterisk_format) {
+               ast_rwlock_rdlock(&codecs->codecs_lock);
+               for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_rx); ++idx) {
+                       type = AST_VECTOR_GET(&codecs->payload_mapping_rx, idx);
+                       if (!type) {
+                               continue;
+                       }
+
+                       if (!type->asterisk_format
+                               && type->primary_mapping
+                               && type->rtp_code == code) {
+                               payload = idx;
+                               break;
+                       }
+               }
+               ast_rwlock_unlock(&codecs->codecs_lock);
+       } else if (format) {
+               ast_rwlock_rdlock(&codecs->codecs_lock);
+               for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_rx); ++idx) {
+                       type = AST_VECTOR_GET(&codecs->payload_mapping_rx, idx);
+                       if (!type) {
+                               continue;
+                       }
+
+                       if (type->asterisk_format
+                               && type->primary_mapping
+                               && ast_format_cmp(format, type->format) == AST_FORMAT_CMP_EQUAL) {
+                               payload = idx;
+                               break;
+                       }
+               }
+               ast_rwlock_unlock(&codecs->codecs_lock);
+       }
+
        if (payload < 0) {
-               ast_rwlock_rdlock(&static_RTP_PT_lock);
-               for (i = 0; i < AST_RTP_MAX_PT; i++) {
-                       if (static_RTP_PT[i].asterisk_format && asterisk_format && format &&
-                               (ast_format_cmp(format, static_RTP_PT[i].format) != AST_FORMAT_CMP_NOT_EQUAL)) {
-                               payload = i;
+               payload = rtp_codecs_assign_payload_code_rx(codecs, asterisk_format, format,
+                       code);
+       }
+       ast_rwlock_unlock(&static_RTP_PT_lock);
+
+       return payload;
+}
+
+int ast_rtp_codecs_payload_code_tx(struct ast_rtp_codecs *codecs, int asterisk_format, const struct ast_format *format, int code)
+{
+       struct ast_rtp_payload_type *type;
+       int idx;
+       int payload = -1;
+
+       if (!asterisk_format) {
+               ast_rwlock_rdlock(&codecs->codecs_lock);
+               for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_tx); ++idx) {
+                       type = AST_VECTOR_GET(&codecs->payload_mapping_tx, idx);
+                       if (!type) {
+                               continue;
+                       }
+
+                       if (!type->asterisk_format
+                               && type->rtp_code == code) {
+                               payload = idx;
                                break;
-                       } else if (!static_RTP_PT[i].asterisk_format && !asterisk_format &&
-                               (static_RTP_PT[i].rtp_code == code)) {
-                               payload = i;
+                       }
+               }
+               ast_rwlock_unlock(&codecs->codecs_lock);
+       } else if (format) {
+               ast_rwlock_rdlock(&codecs->codecs_lock);
+               for (idx = 0; idx < AST_VECTOR_SIZE(&codecs->payload_mapping_tx); ++idx) {
+                       type = AST_VECTOR_GET(&codecs->payload_mapping_tx, idx);
+                       if (!type) {
+                               continue;
+                       }
+
+                       if (type->asterisk_format
+                               && ast_format_cmp(format, type->format) == AST_FORMAT_CMP_EQUAL) {
+                               payload = idx;
                                break;
                        }
                }
+               ast_rwlock_unlock(&codecs->codecs_lock);
+       }
+
+       if (payload < 0) {
+               ast_rwlock_rdlock(&static_RTP_PT_lock);
+               payload = find_static_payload_type(asterisk_format, format, code);
                ast_rwlock_unlock(&static_RTP_PT_lock);
        }
 
        return payload;
 }
 
-int ast_rtp_codecs_find_payload_code(struct ast_rtp_codecs *codecs, int code)
+int ast_rtp_codecs_find_payload_code(struct ast_rtp_codecs *codecs, int payload)
 {
        struct ast_rtp_payload_type *type;
        int res = -1;
 
        ast_rwlock_rdlock(&codecs->codecs_lock);
-       if (code < AST_VECTOR_SIZE(&codecs->payloads)) {
-               type = AST_VECTOR_GET(&codecs->payloads, code);
+       if (payload < AST_VECTOR_SIZE(&codecs->payload_mapping_tx)) {
+               type = AST_VECTOR_GET(&codecs->payload_mapping_tx, payload);
                if (type) {
-                       res = type->payload;
+                       res = payload;
                }
        }
        ast_rwlock_unlock(&codecs->codecs_lock);
@@ -951,7 +1493,8 @@ int ast_rtp_codecs_find_payload_code(struct ast_rtp_codecs *codecs, int code)
        return res;
 }
 
-const char *ast_rtp_lookup_mime_subtype2(const int asterisk_format, struct ast_format *format, int code, enum ast_rtp_options options)
+const char *ast_rtp_lookup_mime_subtype2(const int asterisk_format,
+       const struct ast_format *format, int code, enum ast_rtp_options options)
 {
        int i;
        const char *res = "";
@@ -980,7 +1523,8 @@ const char *ast_rtp_lookup_mime_subtype2(const int asterisk_format, struct ast_f
        return res;
 }
 
-unsigned int ast_rtp_lookup_sample_rate2(int asterisk_format, struct ast_format *format, int code)
+unsigned int ast_rtp_lookup_sample_rate2(int asterisk_format,
+       const struct ast_format *format, int code)
 {
        unsigned int i;
        unsigned int res = 0;
@@ -1185,13 +1729,13 @@ void ast_rtp_instance_early_bridge_make_compatible(struct ast_channel *c_dst, st
                goto done;
        }
 
-       ast_rtp_codecs_payloads_copy(&instance_src->codecs, &instance_dst->codecs, instance_dst);
+       ast_rtp_codecs_payloads_xover(&instance_src->codecs, &instance_dst->codecs, instance_dst);
 
        if (vinstance_dst && vinstance_src) {
-               ast_rtp_codecs_payloads_copy(&vinstance_src->codecs, &vinstance_dst->codecs, vinstance_dst);
+               ast_rtp_codecs_payloads_xover(&vinstance_src->codecs, &vinstance_dst->codecs, vinstance_dst);
        }
        if (tinstance_dst && tinstance_src) {
-               ast_rtp_codecs_payloads_copy(&tinstance_src->codecs, &tinstance_dst->codecs, tinstance_dst);
+               ast_rtp_codecs_payloads_xover(&tinstance_src->codecs, &tinstance_dst->codecs, tinstance_dst);
        }
 
        if (glue_dst->update_peer(c_dst, instance_src, vinstance_src, tinstance_src, cap_src, 0)) {
@@ -1357,16 +1901,16 @@ void ast_rtp_instance_set_stats_vars(struct ast_channel *chan, struct ast_rtp_in
 {
        char quality_buf[AST_MAX_USER_FIELD];
        char *quality;
-       struct ast_channel *bridge = ast_channel_bridge_peer(chan);
+       struct ast_channel *bridge;
 
-       ast_channel_lock(chan);
-       ast_channel_stage_snapshot(chan);
-       ast_channel_unlock(chan);
+       bridge = ast_channel_bridge_peer(chan);
        if (bridge) {
-               ast_channel_lock(bridge);
+               ast_channel_lock_both(chan, bridge);
                ast_channel_stage_snapshot(bridge);
-               ast_channel_unlock(bridge);
+       } else {
+               ast_channel_lock(chan);
        }
+       ast_channel_stage_snapshot(chan);
 
        quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY,
                quality_buf, sizeof(quality_buf));
@@ -1404,11 +1948,9 @@ void ast_rtp_instance_set_stats_vars(struct ast_channel *chan, struct ast_rtp_in
                }
        }
 
-       ast_channel_lock(chan);
        ast_channel_stage_snapshot_done(chan);
        ast_channel_unlock(chan);
        if (bridge) {
-               ast_channel_lock(bridge);
                ast_channel_stage_snapshot_done(bridge);
                ast_channel_unlock(bridge);
                ast_channel_unref(bridge);
@@ -1557,29 +2099,38 @@ int ast_rtp_engine_srtp_is_registered(void)
        return res_srtp && res_srtp_policy;
 }
 
-int ast_rtp_instance_add_srtp_policy(struct ast_rtp_instance *instance, struct ast_srtp_policy *remote_policy, struct ast_srtp_policy *local_policy)
+int ast_rtp_instance_add_srtp_policy(struct ast_rtp_instance *instance, struct ast_srtp_policy *remote_policy, struct ast_srtp_policy *local_policy, int rtcp)
 {
        int res = 0;
+       struct ast_srtp **srtp;
 
        if (!res_srtp) {
                return -1;
        }
 
-       if (!instance->srtp) {
-               res = res_srtp->create(&instance->srtp, instance, remote_policy);
+
+       srtp = rtcp ? &instance->rtcp_srtp : &instance->srtp;
+
+       if (!*srtp) {
+               res = res_srtp->create(srtp, instance, remote_policy);
        } else {
-               res = res_srtp->replace(&instance->srtp, instance, remote_policy);
+               res = res_srtp->replace(srtp, instance, remote_policy);
        }
        if (!res) {
-               res = res_srtp->add_stream(instance->srtp, local_policy);
+               res = res_srtp->add_stream(*srtp, local_policy);
        }
 
        return res;
 }
 
-struct ast_srtp *ast_rtp_instance_get_srtp(struct ast_rtp_instance *instance)
+struct ast_srtp *ast_rtp_instance_get_srtp(struct ast_rtp_instance *instance, int rtcp)
 {
-       return instance->srtp;
+       if (rtcp && instance->rtcp_srtp) {
+               return instance->rtcp_srtp;
+       }
+       else {
+               return instance->srtp;
+       }
 }
 
 int ast_rtp_instance_sendcng(struct ast_rtp_instance *instance, int level)
@@ -1623,18 +2174,34 @@ int ast_rtp_dtls_cfg_parse(struct ast_rtp_dtls_cfg *dtls_cfg, const char *name,
                }
        } else if (!strcasecmp(name, "dtlscertfile")) {
                ast_free(dtls_cfg->certfile);
+               if (!ast_strlen_zero(value) && !ast_file_is_readable(value)) {
+                       ast_log(LOG_ERROR, "%s file %s does not exist or is not readable\n", name, value);
+                       return -1;
+               }
                dtls_cfg->certfile = ast_strdup(value);
        } else if (!strcasecmp(name, "dtlsprivatekey")) {
                ast_free(dtls_cfg->pvtfile);
+               if (!ast_strlen_zero(value) && !ast_file_is_readable(value)) {
+                       ast_log(LOG_ERROR, "%s file %s does not exist or is not readable\n", name, value);
+                       return -1;
+               }
                dtls_cfg->pvtfile = ast_strdup(value);
        } else if (!strcasecmp(name, "dtlscipher")) {
                ast_free(dtls_cfg->cipher);
                dtls_cfg->cipher = ast_strdup(value);
        } else if (!strcasecmp(name, "dtlscafile")) {
                ast_free(dtls_cfg->cafile);
+               if (!ast_strlen_zero(value) && !ast_file_is_readable(value)) {
+                       ast_log(LOG_ERROR, "%s file %s does not exist or is not readable\n", name, value);
+                       return -1;
+               }
                dtls_cfg->cafile = ast_strdup(value);
        } else if (!strcasecmp(name, "dtlscapath") || !strcasecmp(name, "dtlscadir")) {
                ast_free(dtls_cfg->capath);
+               if (!ast_strlen_zero(value) && !ast_file_is_readable(value)) {
+                       ast_log(LOG_ERROR, "%s file %s does not exist or is not readable\n", name, value);
+                       return -1;
+               }
                dtls_cfg->capath = ast_strdup(value);
        } else if (!strcasecmp(name, "dtlssetup")) {
                if (!strcasecmp(value, "active")) {
@@ -1659,6 +2226,8 @@ int ast_rtp_dtls_cfg_parse(struct ast_rtp_dtls_cfg *dtls_cfg, const char *name,
 
 void ast_rtp_dtls_cfg_copy(const struct ast_rtp_dtls_cfg *src_cfg, struct ast_rtp_dtls_cfg *dst_cfg)
 {
+       ast_rtp_dtls_cfg_free(dst_cfg);         /* Prevent a double-call leaking memory via ast_strdup */
+
        dst_cfg->enabled = src_cfg->enabled;
        dst_cfg->verify = src_cfg->verify;
        dst_cfg->rekey = src_cfg->rekey;
@@ -1675,20 +2244,15 @@ void ast_rtp_dtls_cfg_copy(const struct ast_rtp_dtls_cfg *src_cfg, struct ast_rt
 void ast_rtp_dtls_cfg_free(struct ast_rtp_dtls_cfg *dtls_cfg)
 {
        ast_free(dtls_cfg->certfile);
+       dtls_cfg->certfile = NULL;
        ast_free(dtls_cfg->pvtfile);
+       dtls_cfg->pvtfile = NULL;
        ast_free(dtls_cfg->cipher);
+       dtls_cfg->cipher = NULL;
        ast_free(dtls_cfg->cafile);
+       dtls_cfg->cafile = NULL;
        ast_free(dtls_cfg->capath);
-}
-
-/*! \internal
- * \brief Small helper routine that cleans up entry i in
- * \c static_RTP_PT.
- */
-static void rtp_engine_static_RTP_PT_cleanup(int i)
-{
-       ao2_cleanup(static_RTP_PT[i].format);
-       memset(&static_RTP_PT[i], 0, sizeof(struct ast_rtp_payload_type));
+       dtls_cfg->capath = NULL;
 }
 
 /*! \internal
@@ -1703,12 +2267,16 @@ static void rtp_engine_mime_type_cleanup(int i)
 
 static void set_next_mime_type(struct ast_format *format, int rtp_code, const char *type, const char *subtype, unsigned int sample_rate)
 {
-       int x = mime_types_len;
-       if (ARRAY_LEN(ast_rtp_mime_types) == mime_types_len) {
+       int x;
+
+       ast_rwlock_wrlock(&mime_types_lock);
+
+       x = mime_types_len;
+       if (ARRAY_LEN(ast_rtp_mime_types) <= x) {
+               ast_rwlock_unlock(&mime_types_lock);
                return;
        }
 
-       ast_rwlock_wrlock(&mime_types_lock);
        /* Make sure any previous value in ast_rtp_mime_types is cleaned up */
        memset(&ast_rtp_mime_types[x], 0, sizeof(struct ast_rtp_mime_type));    
        if (format) {
@@ -1721,49 +2289,109 @@ static void set_next_mime_type(struct ast_format *format, int rtp_code, const ch
        ast_copy_string(ast_rtp_mime_types[x].subtype, subtype, sizeof(ast_rtp_mime_types[x].subtype));
        ast_rtp_mime_types[x].sample_rate = sample_rate;
        mime_types_len++;
+
        ast_rwlock_unlock(&mime_types_lock);
 }
 
 static void add_static_payload(int map, struct ast_format *format, int rtp_code)
 {
        int x;
+       struct ast_rtp_payload_type *type;
+
+       /*
+        * ARRAY_LEN's result is cast to an int so 'map' is not autocast to a size_t,
+        * which if negative would cause an assertion.
+        */
+       ast_assert(map < (int)ARRAY_LEN(static_RTP_PT));
+
        ast_rwlock_wrlock(&static_RTP_PT_lock);
        if (map < 0) {
                /* find next available dynamic payload slot */
-               for (x = 96; x < 127; x++) {
-                       if (!static_RTP_PT[x].asterisk_format && !static_RTP_PT[x].rtp_code) {
+               for (x = AST_RTP_PT_FIRST_DYNAMIC; x < AST_RTP_MAX_PT; ++x) {
+                       if (!static_RTP_PT[x]) {
                                map = x;
                                break;
                        }
                }
-       }
 
-       if (map < 0) {
-               ast_log(LOG_WARNING, "No Dynamic RTP mapping available for format %s\n",
-                       ast_format_get_name(format));
-               ast_rwlock_unlock(&static_RTP_PT_lock);
-               return;
+               /* http://www.iana.org/assignments/rtp-parameters
+                * RFC 3551, Section 3: "[...] applications which need to define more
+                * than 32 dynamic payload types MAY bind codes below 96, in which case
+                * it is RECOMMENDED that unassigned payload type numbers be used
+                * first". Updated by RFC 5761, Section 4: "[...] values in the range
+                * 64-95 MUST NOT be used [to avoid conflicts with RTCP]". Summaries:
+                * https://tools.ietf.org/html/draft-roach-mmusic-unified-plan#section-3.2.1.2
+                * https://tools.ietf.org/html/draft-wu-avtcore-dynamic-pt-usage#section-3
+                */
+               if (map < 0) {
+                       for (x = MAX(ast_option_rtpptdynamic, 35); x <= AST_RTP_PT_LAST_REASSIGN; ++x) {
+                               if (!static_RTP_PT[x]) {
+                                       map = x;
+                                       break;
+                               }
+                       }
+               }
+               /* Yet, reusing mappings below 35 is not supported in Asterisk because
+                * when Compact Headers are activated, no rtpmap is send for those below
+                * 35. If you want to use 35 and below
+                * A) do not use Compact Headers,
+                * B) remove that code in chan_sip/res_pjsip, or
+                * C) add a flag that this RTP Payload Type got reassigned dynamically
+                *    and requires a rtpmap even with Compact Headers enabled.
+                */
+               if (map < 0) {
+                       for (x = MAX(ast_option_rtpptdynamic, 20); x < 35; ++x) {
+                               if (!static_RTP_PT[x]) {
+                                       map = x;
+                                       break;
+                               }
+                       }
+               }
+               if (map < 0) {
+                       for (x = MAX(ast_option_rtpptdynamic, 0); x < 20; ++x) {
+                               if (!static_RTP_PT[x]) {
+                                       map = x;
+                                       break;
+                               }
+                       }
+               }
+
+               if (map < 0) {
+                       if (format) {
+                               ast_log(LOG_WARNING, "No Dynamic RTP mapping available for format %s\n",
+                                       ast_format_get_name(format));
+                       } else {
+                               ast_log(LOG_WARNING, "No Dynamic RTP mapping available for RTP code %d\n",
+                                       rtp_code);
+                       }
+                       ast_rwlock_unlock(&static_RTP_PT_lock);
+                       return;
+               }
        }
 
-       if (format) {
-               static_RTP_PT[map].asterisk_format = 1;
-               static_RTP_PT[map].format = ao2_bump(format);
-       } else {
-               static_RTP_PT[map].rtp_code = rtp_code;
+       type = ast_rtp_engine_alloc_payload_type();
+       if (type) {
+               if (format) {
+                       ao2_ref(format, +1);
+                       type->format = format;
+                       type->asterisk_format = 1;
+               } else {
+                       type->rtp_code = rtp_code;
+               }
+               type->payload = map;
+               type->primary_mapping = 1;
+               ao2_cleanup(static_RTP_PT[map]);
+               static_RTP_PT[map] = type;
        }
        ast_rwlock_unlock(&static_RTP_PT_lock);
 }
 
 int ast_rtp_engine_load_format(struct ast_format *format)
 {
-       char *codec_name = ast_strdupa(ast_format_get_name(format));
-
-       codec_name = ast_str_to_upper(codec_name);
-
        set_next_mime_type(format,
                0,
                ast_codec_media_type2str(ast_format_get_type(format)),
-               codec_name,
+               ast_format_get_codec_name(format),
                ast_format_get_sample_rate(format));
        add_static_payload(-1, format, 0);
 
@@ -1778,8 +2406,10 @@ int ast_rtp_engine_unload_format(struct ast_format *format)
        ast_rwlock_wrlock(&static_RTP_PT_lock);
        /* remove everything pertaining to this format id from the lists */
        for (x = 0; x < AST_RTP_MAX_PT; x++) {
-               if (ast_format_cmp(static_RTP_PT[x].format, format) == AST_FORMAT_CMP_EQUAL) {
-                       rtp_engine_static_RTP_PT_cleanup(x);
+               if (static_RTP_PT[x]
+                       && ast_format_cmp(static_RTP_PT[x]->format, format) == AST_FORMAT_CMP_EQUAL) {
+                       ao2_ref(static_RTP_PT[x], -1);
+                       static_RTP_PT[x] = NULL;
                }
        }
        ast_rwlock_unlock(&static_RTP_PT_lock);
@@ -1791,7 +2421,9 @@ int ast_rtp_engine_unload_format(struct ast_format *format)
                        rtp_engine_mime_type_cleanup(x);
                        continue;
                }
-               ast_rtp_mime_types[y] = ast_rtp_mime_types[x];
+               if (x != y) {
+                       ast_rtp_mime_types[y] = ast_rtp_mime_types[x];
+               }
                y++;
        }
        mime_types_len = y;
@@ -1861,7 +2493,7 @@ static struct ast_manager_event_blob *rtcp_report_to_ami(struct stasis_message *
        if (type == AST_RTP_RTCP_SR) {
                ast_str_append(&packet_string, 0, "SentNTP: %lu.%06lu\r\n",
                        (unsigned long)payload->report->sender_information.ntp_timestamp.tv_sec,
-                       (unsigned long)payload->report->sender_information.ntp_timestamp.tv_usec * 4096);
+                       (unsigned long)payload->report->sender_information.ntp_timestamp.tv_usec);
                ast_str_append(&packet_string, 0, "SentRTP: %u\r\n",
                                payload->report->sender_information.rtp_timestamp);
                ast_str_append(&packet_string, 0, "SentPackets: %u\r\n",
@@ -1947,7 +2579,7 @@ static struct ast_json *rtcp_report_to_json(struct stasis_message *msg,
        if (payload->report->type == AST_RTP_RTCP_SR) {
                char sec[32];
                char usec[32];
-               snprintf(sec, sizeof(sec), "%lu", payload->report->sender_information.ntp_timestamp.tv_sec);
+               snprintf(sec, sizeof(sec), "%lu", (unsigned long)payload->report->sender_information.ntp_timestamp.tv_sec);
                snprintf(usec, sizeof(usec), "%lu", (unsigned long)payload->report->sender_information.ntp_timestamp.tv_usec);
                json_rtcp_sender_info = ast_json_pack("{s: s, s: s, s: i, s: i, s: i}",
                                "ntp_timestamp_sec", sec,
@@ -1960,12 +2592,12 @@ static struct ast_json *rtcp_report_to_json(struct stasis_message *msg,
                }
        }
 
-       json_rtcp_report = ast_json_pack("{s: i, s: i, s: i, s: O, s: O}",
+       json_rtcp_report = ast_json_pack("{s: i, s: i, s: i, s: o, s: o}",
                        "ssrc", payload->report->ssrc,
                        "type", payload->report->type,
                        "report_count", payload->report->reception_report_count,
-                       "sender_information", json_rtcp_sender_info ? json_rtcp_sender_info : ast_json_null(),
-                       "report_blocks", json_rtcp_report_blocks);
+                       "sender_information", json_rtcp_sender_info ? ast_json_ref(json_rtcp_sender_info) : ast_json_ref(ast_json_null()),
+                       "report_blocks", ast_json_ref(json_rtcp_report_blocks));
        if (!json_rtcp_report) {
                return NULL;
        }
@@ -1977,10 +2609,10 @@ static struct ast_json *rtcp_report_to_json(struct stasis_message *msg,
                }
        }
 
-       return ast_json_pack("{s: O, s: O, s: O}",
-               "channel", payload->snapshot ? json_channel : ast_json_null(),
-               "rtcp_report", json_rtcp_report,
-               "blob", payload->blob);
+       return ast_json_pack("{s: o, s: o, s: o}",
+               "channel", payload->snapshot ? ast_json_ref(json_channel) : ast_json_ref(ast_json_null()),
+               "rtcp_report", ast_json_ref(json_rtcp_report),
+               "blob", ast_json_deep_copy(payload->blob));
 }
 
 static void rtp_rtcp_report_dtor(void *obj)
@@ -2066,9 +2698,8 @@ static void rtp_engine_shutdown(void)
 
        ast_rwlock_wrlock(&static_RTP_PT_lock);
        for (x = 0; x < AST_RTP_MAX_PT; x++) {
-               if (static_RTP_PT[x].format) {
-                       rtp_engine_static_RTP_PT_cleanup(x);
-               }
+               ao2_cleanup(static_RTP_PT[x]);
+               static_RTP_PT[x] = NULL;
        }
        ast_rwlock_unlock(&static_RTP_PT_lock);
 
@@ -2078,10 +2709,11 @@ static void rtp_engine_shutdown(void)
                        rtp_engine_mime_type_cleanup(x);
                }
        }
+       mime_types_len = 0;
        ast_rwlock_unlock(&mime_types_lock);
 }
 
-int ast_rtp_engine_init()
+int ast_rtp_engine_init(void)
 {
        ast_rwlock_init(&mime_types_lock);
        ast_rwlock_init(&static_RTP_PT_lock);
@@ -2092,7 +2724,7 @@ int ast_rtp_engine_init()
        }
        STASIS_MESSAGE_TYPE_INIT(ast_rtp_rtcp_sent_type);
        STASIS_MESSAGE_TYPE_INIT(ast_rtp_rtcp_received_type);
-       ast_register_atexit(rtp_engine_shutdown);
+       ast_register_cleanup(rtp_engine_shutdown);
 
        /* Define all the RTP mime types available */
        set_next_mime_type(ast_format_g723, 0, "audio", "G723", 8000);
@@ -2197,3 +2829,23 @@ int ast_rtp_engine_init()
 
        return 0;
 }
+
+time_t ast_rtp_instance_get_last_tx(const struct ast_rtp_instance *rtp)
+{
+       return rtp->last_tx;
+}
+
+void ast_rtp_instance_set_last_tx(struct ast_rtp_instance *rtp, time_t time)
+{
+       rtp->last_tx = time;
+}
+
+time_t ast_rtp_instance_get_last_rx(const struct ast_rtp_instance *rtp)
+{
+       return rtp->last_rx;
+}
+
+void ast_rtp_instance_set_last_rx(struct ast_rtp_instance *rtp, time_t time)
+{
+       rtp->last_rx = time;
+}