ARI: Add ability to raise arbitrary User Events
[asterisk/asterisk.git] / main / rtp_engine.c
index 4861387..5174b9c 100644 (file)
 
 /*** MODULEINFO
        <support_level>core</support_level>
+***/
+
+/*** DOCUMENTATION
+       <managerEvent language="en_US" name="RTCPSent">
+               <managerEventInstance class="EVENT_FLAG_REPORTING">
+                       <synopsis>Raised when an RTCP packet is sent.</synopsis>
+                       <syntax>
+                               <channel_snapshot/>
+                               <parameter name="SSRC">
+                                       <para>The SSRC identifier for our stream</para>
+                               </parameter>
+                               <parameter name="PT">
+                                       <para>The type of packet for this RTCP report.</para>
+                                       <enumlist>
+                                               <enum name="200(SR)"/>
+                                               <enum name="201(RR)"/>
+                                       </enumlist>
+                               </parameter>
+                               <parameter name="To">
+                                       <para>The address the report is sent to.</para>
+                               </parameter>
+                               <parameter name="ReportCount">
+                                       <para>The number of reports that were sent.</para>
+                                       <para>The report count determines the number of ReportX headers in
+                                       the message. The X for each set of report headers will range from 0 to
+                                       <literal>ReportCount - 1</literal>.</para>
+                               </parameter>
+                               <parameter name="SentNTP" required="false">
+                                       <para>The time the sender generated the report. Only valid when
+                                       PT is <literal>200(SR)</literal>.</para>
+                               </parameter>
+                               <parameter name="SentRTP" required="false">
+                                       <para>The sender's last RTP timestamp. Only valid when PT is
+                                       <literal>200(SR)</literal>.</para>
+                               </parameter>
+                               <parameter name="SentPackets" required="false">
+                                       <para>The number of packets the sender has sent. Only valid when PT
+                                       is <literal>200(SR)</literal>.</para>
+                               </parameter>
+                               <parameter name="SentOctets" required="false">
+                                       <para>The number of bytes the sender has sent. Only valid when PT is
+                                       <literal>200(SR)</literal>.</para>
+                               </parameter>
+                               <parameter name="ReportXSourceSSRC">
+                                       <para>The SSRC for the source of this report block.</para>
+                               </parameter>
+                               <parameter name="ReportXFractionLost">
+                                       <para>The fraction of RTP data packets from <literal>ReportXSourceSSRC</literal>
+                                       lost since the previous SR or RR report was sent.</para>
+                               </parameter>
+                               <parameter name="ReportXCumulativeLost">
+                                       <para>The total number of RTP data packets from <literal>ReportXSourceSSRC</literal>
+                                       lost since the beginning of reception.</para>
+                               </parameter>
+                               <parameter name="ReportXHighestSequence">
+                                       <para>The highest sequence number received in an RTP data packet from
+                                       <literal>ReportXSourceSSRC</literal>.</para>
+                               </parameter>
+                               <parameter name="ReportXSequenceNumberCycles">
+                                       <para>The number of sequence number cycles seen for the RTP data
+                                       received from <literal>ReportXSourceSSRC</literal>.</para>
+                               </parameter>
+                               <parameter name="ReportXIAJitter">
+                                       <para>An estimate of the statistical variance of the RTP data packet
+                                       interarrival time, measured in timestamp units.</para>
+                               </parameter>
+                               <parameter name="ReportXLSR">
+                                       <para>The last SR timestamp received from <literal>ReportXSourceSSRC</literal>.
+                                       If no SR has been received from <literal>ReportXSourceSSRC</literal>,
+                                       then 0.</para>
+                               </parameter>
+                               <parameter name="ReportXDLSR">
+                                       <para>The delay, expressed in units of 1/65536 seconds, between
+                                       receiving the last SR packet from <literal>ReportXSourceSSRC</literal>
+                                       and sending this report.</para>
+                               </parameter>
+                       </syntax>
+               </managerEventInstance>
+       </managerEvent>
+       <managerEvent language="en_US" name="RTCPReceived">
+               <managerEventInstance class="EVENT_FLAG_REPORTING">
+                       <synopsis>Raised when an RTCP packet is received.</synopsis>
+                       <syntax>
+                               <channel_snapshot/>
+                               <parameter name="SSRC">
+                                       <para>The SSRC identifier for the remote system</para>
+                               </parameter>
+                               <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='PT'])" />
+                               <parameter name="From">
+                                       <para>The address the report was received from.</para>
+                               </parameter>
+                               <parameter name="RTT">
+                                       <para>Calculated Round-Trip Time in seconds</para>
+                               </parameter>
+                               <parameter name="ReportCount">
+                                       <para>The number of reports that were received.</para>
+                                       <para>The report count determines the number of ReportX headers in
+                                       the message. The X for each set of report headers will range from 0 to
+                                       <literal>ReportCount - 1</literal>.</para>
+                               </parameter>
+                               <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentNTP'])" />
+                               <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentRTP'])" />
+                               <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentPackets'])" />
+                               <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>
+               </managerEventInstance>
+       </managerEvent>
  ***/
 
 #include "asterisk.h"
@@ -45,6 +153,9 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #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"
 
 struct ast_srtp_res *res_srtp = NULL;
 struct ast_srtp_policy_res *res_srtp_policy = NULL;
@@ -61,8 +172,6 @@ struct ast_rtp_instance {
        struct ast_sockaddr local_address;
        /*! Address that we are sending RTP to */
        struct ast_sockaddr remote_address;
-       /*! Alternate address that we are receiving RTP from */
-       struct ast_sockaddr alt_remote_address;
        /*! Instance that we are bridged to if doing remote or local bridging */
        struct ast_rtp_instance *bridged;
        /*! Payload and packetization information */
@@ -75,10 +184,10 @@ struct ast_rtp_instance {
        int keepalive;
        /*! Glue currently in use */
        struct ast_rtp_glue *glue;
-       /*! Channel associated with the instance */
-       struct ast_channel *chan;
        /*! SRTP info associated with the instance */
        struct ast_srtp *srtp;
+       /*! Channel unique ID */
+       char channel_uniqueid[AST_MAX_UNIQUEID];
 };
 
 /*! List of RTP engines that are currently registered */
@@ -111,6 +220,9 @@ static int mime_types_len = 0;
 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;
+
 int ast_rtp_engine_register2(struct ast_rtp_engine *engine, struct ast_module *module)
 {
        struct ast_rtp_engine *current_engine;
@@ -294,6 +406,16 @@ struct ast_rtp_instance *ast_rtp_instance_new(const char *engine_name,
        return instance;
 }
 
+const char *ast_rtp_instance_get_channel_id(struct ast_rtp_instance *instance)
+{
+       return instance->channel_uniqueid;
+}
+
+void ast_rtp_instance_set_channel_id(struct ast_rtp_instance *instance, const char *uniqueid)
+{
+       ast_copy_string(instance->channel_uniqueid, uniqueid, sizeof(instance->channel_uniqueid));
+}
+
 void ast_rtp_instance_set_data(struct ast_rtp_instance *instance, void *data)
 {
        instance->data = data;
@@ -335,20 +457,6 @@ int ast_rtp_instance_set_remote_address(struct ast_rtp_instance *instance,
        return 0;
 }
 
-int ast_rtp_instance_set_alt_remote_address(struct ast_rtp_instance *instance,
-               const struct ast_sockaddr *address)
-{
-       ast_sockaddr_copy(&instance->alt_remote_address, address);
-
-       /* oink */
-
-       if (instance->engine->alt_remote_address_set) {
-               instance->engine->alt_remote_address_set(instance, &instance->alt_remote_address);
-       }
-
-       return 0;
-}
-
 int ast_rtp_instance_get_and_cmp_local_address(struct ast_rtp_instance *instance,
                struct ast_sockaddr *address)
 {
@@ -731,6 +839,14 @@ static int rtp_payload_type_find_format(void *obj, void *arg, int flags)
        return (type->asterisk_format && (ast_format_cmp(&type->format, format) != AST_FORMAT_CMP_NOT_EQUAL)) ? CMP_MATCH | CMP_STOP : 0;
 }
 
+static int rtp_payload_type_find_nonast_format(void *obj, void *arg, int flags)
+{
+       struct ast_rtp_payload_type *type = obj;
+       int *rtp_code = arg;
+
+       return ((!type->asterisk_format && (type->rtp_code == *rtp_code)) ? CMP_MATCH | CMP_STOP : 0);
+}
+
 int ast_rtp_codecs_payload_code(struct ast_rtp_codecs *codecs, int asterisk_format, const struct ast_format *format, int code)
 {
        struct ast_rtp_payload_type *type;
@@ -740,7 +856,7 @@ int ast_rtp_codecs_payload_code(struct ast_rtp_codecs *codecs, int asterisk_form
                res = type->payload;
                ao2_ref(type, -1);
                return res;
-       } else if (!asterisk_format && (type = ao2_find(codecs->payloads, &code, OBJ_NOLOCK | OBJ_KEY))) {
+       } else if (!asterisk_format && (type = ao2_callback(codecs->payloads, OBJ_NOLOCK, rtp_payload_type_find_nonast_format, (void*)&code))) {
                res = type->payload;
                ao2_ref(type, -1);
                return res;
@@ -762,7 +878,21 @@ int ast_rtp_codecs_payload_code(struct ast_rtp_codecs *codecs, int asterisk_form
 
        return res;
 }
+int ast_rtp_codecs_find_payload_code(struct ast_rtp_codecs *codecs, int code)
+{
+       struct ast_rtp_payload_type *type;
+       int res = -1;
+
+       /* Search the payload type in the codecs passed */
+       if ((type = ao2_find(codecs->payloads, &code, OBJ_NOLOCK | OBJ_KEY)))
+       {
+               res = type->payload;
+               ao2_ref(type, -1);
+               return res;
+       }
 
+       return res;
+}
 const char *ast_rtp_lookup_mime_subtype2(const int asterisk_format, struct ast_format *format, int code, enum ast_rtp_options options)
 {
        int i;
@@ -835,7 +965,7 @@ char *ast_rtp_lookup_mime_multiple2(struct ast_str *buf, struct ast_format_cap *
        } else {
                int x;
                ast_str_append(&buf, 0, "0x%x (", (unsigned int) rtp_capability);
-               for (x = 1; x < AST_RTP_MAX; x <<= 1) {
+               for (x = 1; x <= AST_RTP_MAX; x <<= 1) {
                        if (rtp_capability & x) {
                                name = ast_rtp_lookup_mime_subtype2(asterisk_format, NULL, x, options);
                                ast_str_append(&buf, 0, "%s|", name);
@@ -930,489 +1060,6 @@ struct ast_rtp_glue *ast_rtp_instance_get_glue(const char *type)
        return glue;
 }
 
-static enum ast_bridge_result local_bridge_loop(struct ast_channel *c0, struct ast_channel *c1, struct ast_rtp_instance *instance0, struct ast_rtp_instance *instance1, int timeoutms, int flags, struct ast_frame **fo, struct ast_channel **rc, void *pvt0, void *pvt1)
-{
-       enum ast_bridge_result res = AST_BRIDGE_FAILED;
-       struct ast_channel *who = NULL, *other = NULL, *cs[3] = { NULL, };
-       struct ast_frame *fr = NULL;
-
-       /* Start locally bridging both instances */
-       if (instance0->engine->local_bridge && instance0->engine->local_bridge(instance0, instance1)) {
-               ast_debug(1, "Failed to locally bridge %s to %s, backing out.\n", ast_channel_name(c0), ast_channel_name(c1));
-               ast_channel_unlock(c0);
-               ast_channel_unlock(c1);
-               return AST_BRIDGE_FAILED_NOWARN;
-       }
-       if (instance1->engine->local_bridge && instance1->engine->local_bridge(instance1, instance0)) {
-               ast_debug(1, "Failed to locally bridge %s to %s, backing out.\n", ast_channel_name(c1), ast_channel_name(c0));
-               if (instance0->engine->local_bridge) {
-                       instance0->engine->local_bridge(instance0, NULL);
-               }
-               ast_channel_unlock(c0);
-               ast_channel_unlock(c1);
-               return AST_BRIDGE_FAILED_NOWARN;
-       }
-
-       ast_channel_unlock(c0);
-       ast_channel_unlock(c1);
-
-       instance0->bridged = instance1;
-       instance1->bridged = instance0;
-
-       ast_poll_channel_add(c0, c1);
-
-       /* Hop into a loop waiting for a frame from either channel */
-       cs[0] = c0;
-       cs[1] = c1;
-       cs[2] = NULL;
-       for (;;) {
-               /* If the underlying formats have changed force this bridge to break */
-               if ((ast_format_cmp(ast_channel_rawreadformat(c0), ast_channel_rawwriteformat(c1)) == AST_FORMAT_CMP_NOT_EQUAL) ||
-                       (ast_format_cmp(ast_channel_rawreadformat(c1), ast_channel_rawwriteformat(c0)) == AST_FORMAT_CMP_NOT_EQUAL)) {
-                       ast_debug(1, "rtp-engine-local-bridge: Oooh, formats changed, backing out\n");
-                       res = AST_BRIDGE_FAILED_NOWARN;
-                       break;
-               }
-               /* Check if anything changed */
-               if ((ast_channel_tech_pvt(c0) != pvt0) ||
-                   (ast_channel_tech_pvt(c1) != pvt1) ||
-                   (ast_channel_masq(c0) || ast_channel_masqr(c0) || ast_channel_masq(c1) || ast_channel_masqr(c1)) ||
-                   (ast_channel_monitor(c0) || ast_channel_audiohooks(c0) || ast_channel_monitor(c1) || ast_channel_audiohooks(c1)) ||
-                   (!ast_framehook_list_is_empty(ast_channel_framehooks(c0)) || !ast_framehook_list_is_empty(ast_channel_framehooks(c1)))) {
-                       ast_debug(1, "rtp-engine-local-bridge: Oooh, something is weird, backing out\n");
-                       /* If a masquerade needs to happen we have to try to read in a frame so that it actually happens. Without this we risk being called again and going into a loop */
-                       if ((ast_channel_masq(c0) || ast_channel_masqr(c0)) && (fr = ast_read(c0))) {
-                               ast_frfree(fr);
-                       }
-                       if ((ast_channel_masq(c1) || ast_channel_masqr(c1)) && (fr = ast_read(c1))) {
-                               ast_frfree(fr);
-                       }
-                       res = AST_BRIDGE_RETRY;
-                       break;
-               }
-               /* Wait on a channel to feed us a frame */
-               if (!(who = ast_waitfor_n(cs, 2, &timeoutms))) {
-                       if (!timeoutms) {
-                               res = AST_BRIDGE_RETRY;
-                               break;
-                       }
-                       ast_debug(2, "rtp-engine-local-bridge: Ooh, empty read...\n");
-                       if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
-                               break;
-                       }
-                       continue;
-               }
-               /* Read in frame from channel */
-               fr = ast_read(who);
-               other = (who == c0) ? c1 : c0;
-               /* Depending on the frame we may need to break out of our bridge */
-               if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
-                           ((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) |
-                           ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1)))) {
-                       /* Record received frame and who */
-                       *fo = fr;
-                       *rc = who;
-                       ast_debug(1, "rtp-engine-local-bridge: Ooh, got a %s\n", fr ? "digit" : "hangup");
-                       res = AST_BRIDGE_COMPLETE;
-                       break;
-               } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
-                       if ((fr->subclass.integer == AST_CONTROL_HOLD) ||
-                           (fr->subclass.integer == AST_CONTROL_UNHOLD) ||
-                           (fr->subclass.integer == AST_CONTROL_VIDUPDATE) ||
-                           (fr->subclass.integer == AST_CONTROL_SRCUPDATE) ||
-                           (fr->subclass.integer == AST_CONTROL_T38_PARAMETERS) ||
-                           (fr->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER)) {
-                               /* If we are going on hold, then break callback mode and P2P bridging */
-                               if (fr->subclass.integer == AST_CONTROL_HOLD) {
-                                       if (instance0->engine->local_bridge) {
-                                               instance0->engine->local_bridge(instance0, NULL);
-                                       }
-                                       if (instance1->engine->local_bridge) {
-                                               instance1->engine->local_bridge(instance1, NULL);
-                                       }
-                                       instance0->bridged = NULL;
-                                       instance1->bridged = NULL;
-                               } else if (fr->subclass.integer == AST_CONTROL_UNHOLD) {
-                                       if (instance0->engine->local_bridge) {
-                                               instance0->engine->local_bridge(instance0, instance1);
-                                       }
-                                       if (instance1->engine->local_bridge) {
-                                               instance1->engine->local_bridge(instance1, instance0);
-                                       }
-                                       instance0->bridged = instance1;
-                                       instance1->bridged = instance0;
-                               }
-                               /* Since UPDATE_BRIDGE_PEER is only used by the bridging code, don't forward it */
-                               if (fr->subclass.integer != AST_CONTROL_UPDATE_RTP_PEER) {
-                                       ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
-                               }
-                               ast_frfree(fr);
-                       } else if (fr->subclass.integer == AST_CONTROL_CONNECTED_LINE) {
-                               if (ast_channel_connected_line_sub(who, other, fr, 1) &&
-                                       ast_channel_connected_line_macro(who, other, fr, other == c0, 1)) {
-                                       ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
-                               }
-                               ast_frfree(fr);
-                       } else if (fr->subclass.integer == AST_CONTROL_REDIRECTING) {
-                               if (ast_channel_redirecting_sub(who, other, fr, 1) &&
-                                       ast_channel_redirecting_macro(who, other, fr, other == c0, 1)) {
-                                       ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
-                               }
-                               ast_frfree(fr);
-                       } else if (fr->subclass.integer == AST_CONTROL_PVT_CAUSE_CODE) {
-                               ast_channel_hangupcause_hash_set(other, fr->data.ptr, fr->datalen);
-                               ast_frfree(fr);
-                       } else {
-                               *fo = fr;
-                               *rc = who;
-                               ast_debug(1, "rtp-engine-local-bridge: Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass.integer, ast_channel_name(who));
-                               res = AST_BRIDGE_COMPLETE;
-                               break;
-                       }
-               } else {
-                       if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
-                           (fr->frametype == AST_FRAME_DTMF_END) ||
-                           (fr->frametype == AST_FRAME_VOICE) ||
-                           (fr->frametype == AST_FRAME_VIDEO) ||
-                           (fr->frametype == AST_FRAME_IMAGE) ||
-                           (fr->frametype == AST_FRAME_HTML) ||
-                           (fr->frametype == AST_FRAME_MODEM) ||
-                           (fr->frametype == AST_FRAME_TEXT)) {
-                               ast_write(other, fr);
-                       }
-
-                       ast_frfree(fr);
-               }
-               /* Swap priority */
-               cs[2] = cs[0];
-               cs[0] = cs[1];
-               cs[1] = cs[2];
-       }
-
-       /* Stop locally bridging both instances */
-       if (instance0->engine->local_bridge) {
-               instance0->engine->local_bridge(instance0, NULL);
-       }
-       if (instance1->engine->local_bridge) {
-               instance1->engine->local_bridge(instance1, NULL);
-       }
-
-       instance0->bridged = NULL;
-       instance1->bridged = NULL;
-
-       ast_poll_channel_del(c0, c1);
-
-       return res;
-}
-
-static enum ast_bridge_result remote_bridge_loop(struct ast_channel *c0,
-       struct ast_channel *c1,
-       struct ast_rtp_instance *instance0,
-       struct ast_rtp_instance *instance1,
-       struct ast_rtp_instance *vinstance0,
-       struct ast_rtp_instance *vinstance1,
-       struct ast_rtp_instance *tinstance0,
-       struct ast_rtp_instance *tinstance1,
-       struct ast_rtp_glue *glue0,
-       struct ast_rtp_glue *glue1,
-       struct ast_format_cap *cap0,
-       struct ast_format_cap *cap1,
-       int timeoutms,
-       int flags,
-       struct ast_frame **fo,
-       struct ast_channel **rc,
-       void *pvt0,
-       void *pvt1)
-{
-       enum ast_bridge_result res = AST_BRIDGE_FAILED;
-       struct ast_channel *who = NULL, *other = NULL, *cs[3] = { NULL, };
-       struct ast_format_cap *oldcap0 = ast_format_cap_dup(cap0);
-       struct ast_format_cap *oldcap1 = ast_format_cap_dup(cap1);
-       struct ast_sockaddr ac1 = {{0,}}, vac1 = {{0,}}, tac1 = {{0,}}, ac0 = {{0,}}, vac0 = {{0,}}, tac0 = {{0,}};
-       struct ast_sockaddr t1 = {{0,}}, vt1 = {{0,}}, tt1 = {{0,}}, t0 = {{0,}}, vt0 = {{0,}}, tt0 = {{0,}};
-       struct ast_frame *fr = NULL;
-
-       if (!oldcap0 || !oldcap1) {
-               ast_channel_unlock(c0);
-               ast_channel_unlock(c1);
-               goto remote_bridge_cleanup;
-       }
-       /* Test the first channel */
-       if (!(glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0))) {
-               ast_rtp_instance_get_remote_address(instance1, &ac1);
-               if (vinstance1) {
-                       ast_rtp_instance_get_remote_address(vinstance1, &vac1);
-               }
-               if (tinstance1) {
-                       ast_rtp_instance_get_remote_address(tinstance1, &tac1);
-               }
-       } else {
-               ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", ast_channel_name(c0), ast_channel_name(c1));
-       }
-
-       /* Test the second channel */
-       if (!(glue1->update_peer(c1, instance0, vinstance0, tinstance0, cap0, 0))) {
-               ast_rtp_instance_get_remote_address(instance0, &ac0);
-               if (vinstance0) {
-                       ast_rtp_instance_get_remote_address(instance0, &vac0);
-               }
-               if (tinstance0) {
-                       ast_rtp_instance_get_remote_address(instance0, &tac0);
-               }
-       } else {
-               ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", ast_channel_name(c1), ast_channel_name(c0));
-       }
-
-       ast_channel_unlock(c0);
-       ast_channel_unlock(c1);
-
-       instance0->bridged = instance1;
-       instance1->bridged = instance0;
-
-       ast_poll_channel_add(c0, c1);
-
-       /* Go into a loop handling any stray frames that may come in */
-       cs[0] = c0;
-       cs[1] = c1;
-       cs[2] = NULL;
-       for (;;) {
-               /* Check if anything changed */
-               if ((ast_channel_tech_pvt(c0) != pvt0) ||
-                   (ast_channel_tech_pvt(c1) != pvt1) ||
-                   (ast_channel_masq(c0) || ast_channel_masqr(c0) || ast_channel_masq(c1) || ast_channel_masqr(c1)) ||
-                   (ast_channel_monitor(c0) || ast_channel_audiohooks(c0) || ast_channel_monitor(c1) || ast_channel_audiohooks(c1)) ||
-                   (!ast_framehook_list_is_empty(ast_channel_framehooks(c0)) || !ast_framehook_list_is_empty(ast_channel_framehooks(c1)))) {
-                       ast_debug(1, "Oooh, something is weird, backing out\n");
-                       res = AST_BRIDGE_RETRY;
-                       break;
-               }
-
-               /* Check if they have changed their address */
-               ast_rtp_instance_get_remote_address(instance1, &t1);
-               if (vinstance1) {
-                       ast_rtp_instance_get_remote_address(vinstance1, &vt1);
-               }
-               if (tinstance1) {
-                       ast_rtp_instance_get_remote_address(tinstance1, &tt1);
-               }
-               if (glue1->get_codec) {
-                       ast_format_cap_remove_all(cap1);
-                       glue1->get_codec(c1, cap1);
-               }
-
-               ast_rtp_instance_get_remote_address(instance0, &t0);
-               if (vinstance0) {
-                       ast_rtp_instance_get_remote_address(vinstance0, &vt0);
-               }
-               if (tinstance0) {
-                       ast_rtp_instance_get_remote_address(tinstance0, &tt0);
-               }
-               if (glue0->get_codec) {
-                       ast_format_cap_remove_all(cap0);
-                       glue0->get_codec(c0, cap0);
-               }
-
-               if ((ast_sockaddr_cmp(&t1, &ac1)) ||
-                   (vinstance1 && ast_sockaddr_cmp(&vt1, &vac1)) ||
-                   (tinstance1 && ast_sockaddr_cmp(&tt1, &tac1)) ||
-                   (!ast_format_cap_identical(cap1, oldcap1))) {
-                       char tmp_buf[512] = { 0, };
-                       ast_debug(1, "Oooh, '%s' changed end address to %s (format %s)\n",
-                                 ast_channel_name(c1), ast_sockaddr_stringify(&t1),
-                                 ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap1));
-                       ast_debug(1, "Oooh, '%s' changed end vaddress to %s (format %s)\n",
-                                 ast_channel_name(c1), ast_sockaddr_stringify(&vt1),
-                                 ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap1));
-                       ast_debug(1, "Oooh, '%s' changed end taddress to %s (format %s)\n",
-                                 ast_channel_name(c1), ast_sockaddr_stringify(&tt1),
-                                 ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap1));
-                       ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
-                                 ast_channel_name(c1), ast_sockaddr_stringify(&ac1),
-                                 ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap1));
-                       ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
-                                 ast_channel_name(c1), ast_sockaddr_stringify(&vac1),
-                                 ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap1));
-                       ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
-                                 ast_channel_name(c1), ast_sockaddr_stringify(&tac1),
-                                 ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap1));
-                       if (glue0->update_peer(c0,
-                                              ast_sockaddr_isnull(&t1)  ? NULL : instance1,
-                                              ast_sockaddr_isnull(&vt1) ? NULL : vinstance1,
-                                              ast_sockaddr_isnull(&tt1) ? NULL : tinstance1,
-                                              cap1, 0)) {
-                               ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", ast_channel_name(c0), ast_channel_name(c1));
-                       }
-                       ast_sockaddr_copy(&ac1, &t1);
-                       ast_sockaddr_copy(&vac1, &vt1);
-                       ast_sockaddr_copy(&tac1, &tt1);
-                       ast_format_cap_copy(oldcap1, cap1);
-               }
-               if ((ast_sockaddr_cmp(&t0, &ac0)) ||
-                   (vinstance0 && ast_sockaddr_cmp(&vt0, &vac0)) ||
-                   (tinstance0 && ast_sockaddr_cmp(&tt0, &tac0)) ||
-                   (!ast_format_cap_identical(cap0, oldcap0))) {
-                       char tmp_buf[512] = { 0, };
-                       ast_debug(1, "Oooh, '%s' changed end address to %s (format %s)\n",
-                                 ast_channel_name(c0), ast_sockaddr_stringify(&t0),
-                                 ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap0));
-                       ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
-                                 ast_channel_name(c0), ast_sockaddr_stringify(&ac0),
-                                 ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap0));
-                       if (glue1->update_peer(c1, t0.len ? instance0 : NULL,
-                                               vt0.len ? vinstance0 : NULL,
-                                               tt0.len ? tinstance0 : NULL,
-                                               cap0, 0)) {
-                               ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", ast_channel_name(c1), ast_channel_name(c0));
-                       }
-                       ast_sockaddr_copy(&ac0, &t0);
-                       ast_sockaddr_copy(&vac0, &vt0);
-                       ast_sockaddr_copy(&tac0, &tt0);
-                       ast_format_cap_copy(oldcap0, cap0);
-               }
-
-               /* Wait for frame to come in on the channels */
-               if (!(who = ast_waitfor_n(cs, 2, &timeoutms))) {
-                       if (!timeoutms) {
-                               res = AST_BRIDGE_RETRY;
-                               break;
-                       }
-                       ast_debug(1, "Ooh, empty read...\n");
-                       if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
-                               break;
-                       }
-                       continue;
-               }
-               fr = ast_read(who);
-               other = (who == c0) ? c1 : c0;
-               if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
-                           (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
-                            ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))))) {
-                       /* Break out of bridge */
-                       *fo = fr;
-                       *rc = who;
-                       ast_debug(1, "Oooh, got a %s\n", fr ? "digit" : "hangup");
-                       res = AST_BRIDGE_COMPLETE;
-                       break;
-               } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
-                       if ((fr->subclass.integer == AST_CONTROL_HOLD) ||
-                           (fr->subclass.integer == AST_CONTROL_UNHOLD) ||
-                           (fr->subclass.integer == AST_CONTROL_VIDUPDATE) ||
-                           (fr->subclass.integer == AST_CONTROL_SRCUPDATE) ||
-                           (fr->subclass.integer == AST_CONTROL_T38_PARAMETERS) ||
-                               (fr->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER)) {
-                               if (fr->subclass.integer == AST_CONTROL_HOLD) {
-                                       /* If we someone went on hold we want the other side to reinvite back to us */
-                                       if (who == c0) {
-                                               glue1->update_peer(c1, NULL, NULL, NULL, 0, 0);
-                                       } else {
-                                               glue0->update_peer(c0, NULL, NULL, NULL, 0, 0);
-                                       }
-                               } else if (fr->subclass.integer == AST_CONTROL_UNHOLD ||
-                                       fr->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER) {
-                                       /* If they went off hold they should go back to being direct, or if we have
-                                        * been told to force a peer update, go ahead and do it. */
-                                       if (who == c0) {
-                                               glue1->update_peer(c1, instance0, vinstance0, tinstance0, cap0, 0);
-                                       } else {
-                                               glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0);
-                                       }
-                               }
-                               /* Update local address information */
-                               ast_rtp_instance_get_remote_address(instance0, &t0);
-                               ast_sockaddr_copy(&ac0, &t0);
-                               ast_rtp_instance_get_remote_address(instance1, &t1);
-                               ast_sockaddr_copy(&ac1, &t1);
-                               /* Update codec information */
-                               if (glue0->get_codec && ast_channel_tech_pvt(c0)) {
-                                       ast_format_cap_remove_all(cap0);
-                                       ast_format_cap_remove_all(oldcap0);
-                                       glue0->get_codec(c0, cap0);
-                                       ast_format_cap_append(oldcap0, cap0);
-
-                               }
-                               if (glue1->get_codec && ast_channel_tech_pvt(c1)) {
-                                       ast_format_cap_remove_all(cap1);
-                                       ast_format_cap_remove_all(oldcap1);
-                                       glue1->get_codec(c1, cap1);
-                                       ast_format_cap_append(oldcap1, cap1);
-                               }
-                               /* Since UPDATE_BRIDGE_PEER is only used by the bridging code, don't forward it */
-                               if (fr->subclass.integer != AST_CONTROL_UPDATE_RTP_PEER) {
-                                       ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
-                               }
-                               ast_frfree(fr);
-                       } else if (fr->subclass.integer == AST_CONTROL_CONNECTED_LINE) {
-                               if (ast_channel_connected_line_sub(who, other, fr, 1) &&
-                                       ast_channel_connected_line_macro(who, other, fr, other == c0, 1)) {
-                                       ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
-                               }
-                               ast_frfree(fr);
-                       } else if (fr->subclass.integer == AST_CONTROL_REDIRECTING) {
-                               if (ast_channel_redirecting_sub(who, other, fr, 1) &&
-                                       ast_channel_redirecting_macro(who, other, fr, other == c0, 1)) {
-                                       ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
-                               }
-                               ast_frfree(fr);
-                       } else if (fr->subclass.integer == AST_CONTROL_PVT_CAUSE_CODE) {
-                               ast_channel_hangupcause_hash_set(other, fr->data.ptr, fr->datalen);
-                               ast_frfree(fr);
-                       } else {
-                               *fo = fr;
-                               *rc = who;
-                               ast_debug(1, "Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass.integer, ast_channel_name(who));
-                               res = AST_BRIDGE_COMPLETE;
-                               goto remote_bridge_cleanup;
-                       }
-               } else {
-                       if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
-                           (fr->frametype == AST_FRAME_DTMF_END) ||
-                           (fr->frametype == AST_FRAME_VOICE) ||
-                           (fr->frametype == AST_FRAME_VIDEO) ||
-                           (fr->frametype == AST_FRAME_IMAGE) ||
-                           (fr->frametype == AST_FRAME_HTML) ||
-                           (fr->frametype == AST_FRAME_MODEM) ||
-                           (fr->frametype == AST_FRAME_TEXT)) {
-                               ast_write(other, fr);
-                       }
-                       ast_frfree(fr);
-               }
-               /* Swap priority */
-               cs[2] = cs[0];
-               cs[0] = cs[1];
-               cs[1] = cs[2];
-       }
-
-       if (ast_test_flag(ast_channel_flags(c0), AST_FLAG_ZOMBIE)) {
-               ast_debug(1, "Channel '%s' Zombie cleardown from bridge\n", ast_channel_name(c0));
-       } else if (ast_channel_tech_pvt(c0) != pvt0) {
-               ast_debug(1, "Channel c0->'%s' pvt changed, in bridge with c1->'%s'\n", ast_channel_name(c0), ast_channel_name(c1));
-       } else if (glue0 != ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) {
-               ast_debug(1, "Channel c0->'%s' technology changed, in bridge with c1->'%s'\n", ast_channel_name(c0), ast_channel_name(c1));
-       } else if (glue0->update_peer(c0, NULL, NULL, NULL, 0, 0)) {
-               ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", ast_channel_name(c0));
-       }
-       if (ast_test_flag(ast_channel_flags(c1), AST_FLAG_ZOMBIE)) {
-               ast_debug(1, "Channel '%s' Zombie cleardown from bridge\n", ast_channel_name(c1));
-       } else if (ast_channel_tech_pvt(c1) != pvt1) {
-               ast_debug(1, "Channel c1->'%s' pvt changed, in bridge with c0->'%s'\n", ast_channel_name(c1), ast_channel_name(c0));
-       } else if (glue1 != ast_rtp_instance_get_glue(ast_channel_tech(c1)->type)) {
-               ast_debug(1, "Channel c1->'%s' technology changed, in bridge with c0->'%s'\n", ast_channel_name(c1), ast_channel_name(c0));
-       } else if (glue1->update_peer(c1, NULL, NULL, NULL, 0, 0)) {
-               ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", ast_channel_name(c1));
-       }
-
-       instance0->bridged = NULL;
-       instance1->bridged = NULL;
-
-       ast_poll_channel_del(c0, c1);
-
-remote_bridge_cleanup:
-       ast_format_cap_destroy(oldcap0);
-       ast_format_cap_destroy(oldcap1);
-
-       return res;
-}
-
 /*!
  * \brief Conditionally unref an rtp instance
  */
@@ -1424,258 +1071,100 @@ static void unref_instance_cond(struct ast_rtp_instance **instance)
        }
 }
 
-enum ast_bridge_result ast_rtp_instance_bridge(struct ast_channel *c0, struct ast_channel *c1, int flags, struct ast_frame **fo, struct ast_channel **rc, int timeoutms)
+struct ast_rtp_instance *ast_rtp_instance_get_bridged(struct ast_rtp_instance *instance)
 {
-       struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
-                       *vinstance0 = NULL, *vinstance1 = NULL,
-                       *tinstance0 = NULL, *tinstance1 = NULL;
-       struct ast_rtp_glue *glue0, *glue1;
-       struct ast_sockaddr addr1 = { {0, }, }, addr2 = { {0, }, };
-       enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
-       enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
-       enum ast_bridge_result res = AST_BRIDGE_FAILED;
-       enum ast_rtp_dtmf_mode dmode;
-       struct ast_format_cap *cap0 = ast_format_cap_alloc_nolock();
-       struct ast_format_cap *cap1 = ast_format_cap_alloc_nolock();
-       int unlock_chans = 1;
-
-       if (!cap0 || !cap1) {
-               unlock_chans = 0;
-               goto done;
-       }
-
-       /* Lock both channels so we can look for the glue that binds them together */
-       ast_channel_lock(c0);
-       while (ast_channel_trylock(c1)) {
-               ast_channel_unlock(c0);
-               usleep(1);
-               ast_channel_lock(c0);
-       }
-
-       /* Ensure neither channel got hungup during lock avoidance */
-       if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
-               ast_log(LOG_WARNING, "Got hangup while attempting to bridge '%s' and '%s'\n", ast_channel_name(c0), ast_channel_name(c1));
-               goto done;
-       }
-
-       /* Grab glue that binds each channel to something using the RTP engine */
-       if (!(glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) || !(glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type))) {
-               ast_debug(1, "Can't find native functions for channel '%s'\n", glue0 ? ast_channel_name(c1) : ast_channel_name(c0));
-               goto done;
-       }
-
-       audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
-       video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
-
-       audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
-       video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
-
-       /* If the channels are of the same technology, they might have limitations on remote bridging */
-       if (ast_channel_tech(c0) == ast_channel_tech(c1)) {
-               if (audio_glue0_res == audio_glue1_res && audio_glue1_res == AST_RTP_GLUE_RESULT_REMOTE) {
-                       if (glue0->allow_rtp_remote && !(glue0->allow_rtp_remote(c0, c1))) {
-                               /* If the allow_rtp_remote indicates that remote isn't allowed, revert to local bridge */
-                               audio_glue0_res = audio_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
-                       }
-               }
-               if (video_glue0_res == video_glue1_res && video_glue1_res == AST_RTP_GLUE_RESULT_REMOTE) {
-                       if (glue0->allow_vrtp_remote && !(glue0->allow_vrtp_remote(c0, c1))) {
-                               /* if the allow_vrtp_remote indicates that remote isn't allowed, revert to local bridge */
-                               video_glue0_res = video_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
-                       }
-               }
-       }
-
-       /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
-       if (video_glue0_res != AST_RTP_GLUE_RESULT_FORBID && (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || video_glue0_res != AST_RTP_GLUE_RESULT_REMOTE)) {
-               audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
-       }
-       if (video_glue1_res != AST_RTP_GLUE_RESULT_FORBID && (audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE || video_glue1_res != AST_RTP_GLUE_RESULT_REMOTE)) {
-               audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
-       }
-
-       /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
-       if (audio_glue0_res == AST_RTP_GLUE_RESULT_FORBID || audio_glue1_res == AST_RTP_GLUE_RESULT_FORBID) {
-               res = AST_BRIDGE_FAILED_NOWARN;
-               goto done;
-       }
-
-
-       /* If address families differ, force a local bridge */
-       ast_rtp_instance_get_remote_address(instance0, &addr1);
-       ast_rtp_instance_get_remote_address(instance1, &addr2);
-
-       if (addr1.ss.ss_family != addr2.ss.ss_family ||
-          (ast_sockaddr_is_ipv4_mapped(&addr1) != ast_sockaddr_is_ipv4_mapped(&addr2))) {
-               audio_glue0_res = AST_RTP_GLUE_RESULT_LOCAL;
-               audio_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
-       }
-
-       /* If we need to get DTMF see if we can do it outside of the RTP stream itself */
-       dmode = ast_rtp_instance_dtmf_mode_get(instance0);
-       if ((flags & AST_BRIDGE_DTMF_CHANNEL_0) && dmode) {
-               res = AST_BRIDGE_FAILED_NOWARN;
-               goto done;
-       }
-       dmode = ast_rtp_instance_dtmf_mode_get(instance1);
-       if ((flags & AST_BRIDGE_DTMF_CHANNEL_1) && dmode) {
-               res = AST_BRIDGE_FAILED_NOWARN;
-               goto done;
-       }
-
-       /* If we have gotten to a local bridge make sure that both sides have the same local bridge callback and that they are DTMF compatible */
-       if ((audio_glue0_res == AST_RTP_GLUE_RESULT_LOCAL || audio_glue1_res == AST_RTP_GLUE_RESULT_LOCAL) && ((instance0->engine->local_bridge != instance1->engine->local_bridge) || (instance0->engine->dtmf_compatible && !instance0->engine->dtmf_compatible(c0, instance0, c1, instance1)))) {
-               res = AST_BRIDGE_FAILED_NOWARN;
-               goto done;
-       }
-
-       /* Make sure that codecs match */
-       if (glue0->get_codec){
-               glue0->get_codec(c0, cap0);
-       }
-       if (glue1->get_codec) {
-               glue1->get_codec(c1, cap1);
-       }
-       if (!ast_format_cap_is_empty(cap0) && !ast_format_cap_is_empty(cap1) && !ast_format_cap_has_joint(cap0, cap1)) {
-               char tmp0[256] = { 0, };
-               char tmp1[256] = { 0, };
-               ast_debug(1, "Channel codec0 = %s is not codec1 = %s, cannot native bridge in RTP.\n",
-                       ast_getformatname_multiple(tmp0, sizeof(tmp0), cap0),
-                       ast_getformatname_multiple(tmp1, sizeof(tmp1), cap1));
-               res = AST_BRIDGE_FAILED_NOWARN;
-               goto done;
-       }
-
-       instance0->glue = glue0;
-       instance1->glue = glue1;
-       instance0->chan = c0;
-       instance1->chan = c1;
-
-       /* Depending on the end result for bridging either do a local bridge or remote bridge */
-       if (audio_glue0_res == AST_RTP_GLUE_RESULT_LOCAL || audio_glue1_res == AST_RTP_GLUE_RESULT_LOCAL) {
-               ast_verb(3, "Locally bridging %s and %s\n", ast_channel_name(c0), ast_channel_name(c1));
-               res = local_bridge_loop(c0, c1, instance0, instance1, timeoutms, flags, fo, rc, ast_channel_tech_pvt(c0), ast_channel_tech_pvt(c1));
-       } else {
-               ast_verb(3, "Remotely bridging %s and %s\n", ast_channel_name(c0), ast_channel_name(c1));
-               res = remote_bridge_loop(c0, c1, instance0, instance1, vinstance0, vinstance1,
-                               tinstance0, tinstance1, glue0, glue1, cap0, cap1, timeoutms, flags,
-                               fo, rc, ast_channel_tech_pvt(c0), ast_channel_tech_pvt(c1));
-       }
-
-       instance0->glue = NULL;
-       instance1->glue = NULL;
-       instance0->chan = NULL;
-       instance1->chan = NULL;
-
-       unlock_chans = 0;
-
-done:
-       if (unlock_chans) {
-               ast_channel_unlock(c0);
-               ast_channel_unlock(c1);
-       }
-       ast_format_cap_destroy(cap1);
-       ast_format_cap_destroy(cap0);
-
-       unref_instance_cond(&instance0);
-       unref_instance_cond(&instance1);
-       unref_instance_cond(&vinstance0);
-       unref_instance_cond(&vinstance1);
-       unref_instance_cond(&tinstance0);
-       unref_instance_cond(&tinstance1);
-
-       return res;
+       return instance->bridged;
 }
 
-struct ast_rtp_instance *ast_rtp_instance_get_bridged(struct ast_rtp_instance *instance)
+void ast_rtp_instance_set_bridged(struct ast_rtp_instance *instance, struct ast_rtp_instance *bridged)
 {
-       return instance->bridged;
+       instance->bridged = bridged;
 }
 
-void ast_rtp_instance_early_bridge_make_compatible(struct ast_channel *c0, struct ast_channel *c1)
+void ast_rtp_instance_early_bridge_make_compatible(struct ast_channel *c_dst, struct ast_channel *c_src)
 {
-       struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
-               *vinstance0 = NULL, *vinstance1 = NULL,
-               *tinstance0 = NULL, *tinstance1 = NULL;
-       struct ast_rtp_glue *glue0, *glue1;
-       enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
-       enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
-       struct ast_format_cap *cap0 = ast_format_cap_alloc_nolock();
-       struct ast_format_cap *cap1 = ast_format_cap_alloc_nolock();
+       struct ast_rtp_instance *instance_dst = NULL, *instance_src = NULL,
+               *vinstance_dst = NULL, *vinstance_src = NULL,
+               *tinstance_dst = NULL, *tinstance_src = NULL;
+       struct ast_rtp_glue *glue_dst, *glue_src;
+       enum ast_rtp_glue_result audio_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID, video_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID;
+       enum ast_rtp_glue_result audio_glue_src_res = AST_RTP_GLUE_RESULT_FORBID, video_glue_src_res = AST_RTP_GLUE_RESULT_FORBID;
+       struct ast_format_cap *cap_dst = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
+       struct ast_format_cap *cap_src = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
 
        /* Lock both channels so we can look for the glue that binds them together */
-       ast_channel_lock_both(c0, c1);
+       ast_channel_lock_both(c_dst, c_src);
 
-       if (!cap1 || !cap0) {
+       if (!cap_src || !cap_dst) {
                goto done;
        }
 
        /* Grab glue that binds each channel to something using the RTP engine */
-       if (!(glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) || !(glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type))) {
-               ast_debug(1, "Can't find native functions for channel '%s'\n", glue0 ? ast_channel_name(c1) : ast_channel_name(c0));
+       if (!(glue_dst = ast_rtp_instance_get_glue(ast_channel_tech(c_dst)->type)) || !(glue_src = ast_rtp_instance_get_glue(ast_channel_tech(c_src)->type))) {
+               ast_debug(1, "Can't find native functions for channel '%s'\n", glue_dst ? ast_channel_name(c_src) : ast_channel_name(c_dst));
                goto done;
        }
 
-       audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
-       video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
+       audio_glue_dst_res = glue_dst->get_rtp_info(c_dst, &instance_dst);
+       video_glue_dst_res = glue_dst->get_vrtp_info ? glue_dst->get_vrtp_info(c_dst, &vinstance_dst) : AST_RTP_GLUE_RESULT_FORBID;
 
-       audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
-       video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
+       audio_glue_src_res = glue_src->get_rtp_info(c_src, &instance_src);
+       video_glue_src_res = glue_src->get_vrtp_info ? glue_src->get_vrtp_info(c_src, &vinstance_src) : AST_RTP_GLUE_RESULT_FORBID;
 
        /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
-       if (video_glue0_res != AST_RTP_GLUE_RESULT_FORBID && (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || video_glue0_res != AST_RTP_GLUE_RESULT_REMOTE)) {
-               audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
+       if (video_glue_dst_res != AST_RTP_GLUE_RESULT_FORBID && (audio_glue_dst_res != AST_RTP_GLUE_RESULT_REMOTE || video_glue_dst_res != AST_RTP_GLUE_RESULT_REMOTE)) {
+               audio_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID;
        }
-       if (video_glue1_res != AST_RTP_GLUE_RESULT_FORBID && (audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE || video_glue1_res != AST_RTP_GLUE_RESULT_REMOTE)) {
-               audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
+       if (video_glue_src_res != AST_RTP_GLUE_RESULT_FORBID && (audio_glue_src_res != AST_RTP_GLUE_RESULT_REMOTE || video_glue_src_res != AST_RTP_GLUE_RESULT_REMOTE)) {
+               audio_glue_src_res = AST_RTP_GLUE_RESULT_FORBID;
        }
-       if (audio_glue0_res == AST_RTP_GLUE_RESULT_REMOTE && (video_glue0_res == AST_RTP_GLUE_RESULT_FORBID || video_glue0_res == AST_RTP_GLUE_RESULT_REMOTE) && glue0->get_codec) {
-               glue0->get_codec(c0, cap0);
+       if (audio_glue_dst_res == AST_RTP_GLUE_RESULT_REMOTE && (video_glue_dst_res == AST_RTP_GLUE_RESULT_FORBID || video_glue_dst_res == AST_RTP_GLUE_RESULT_REMOTE) && glue_dst->get_codec) {
+               glue_dst->get_codec(c_dst, cap_dst);
        }
-       if (audio_glue1_res == AST_RTP_GLUE_RESULT_REMOTE && (video_glue1_res == AST_RTP_GLUE_RESULT_FORBID || video_glue1_res == AST_RTP_GLUE_RESULT_REMOTE) && glue1->get_codec) {
-               glue1->get_codec(c1, cap1);
+       if (audio_glue_src_res == AST_RTP_GLUE_RESULT_REMOTE && (video_glue_src_res == AST_RTP_GLUE_RESULT_FORBID || video_glue_src_res == AST_RTP_GLUE_RESULT_REMOTE) && glue_src->get_codec) {
+               glue_src->get_codec(c_src, cap_src);
        }
 
        /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
-       if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
+       if (audio_glue_dst_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue_src_res != AST_RTP_GLUE_RESULT_REMOTE) {
                goto done;
        }
 
        /* Make sure we have matching codecs */
-       if (!ast_format_cap_has_joint(cap0, cap1)) {
+       if (!ast_format_cap_has_joint(cap_dst, cap_src)) {
                goto done;
        }
 
-       ast_rtp_codecs_payloads_copy(&instance0->codecs, &instance1->codecs, instance1);
+       ast_rtp_codecs_payloads_copy(&instance_src->codecs, &instance_dst->codecs, instance_dst);
 
-       if (vinstance0 && vinstance1) {
-               ast_rtp_codecs_payloads_copy(&vinstance0->codecs, &vinstance1->codecs, vinstance1);
+       if (vinstance_dst && vinstance_src) {
+               ast_rtp_codecs_payloads_copy(&vinstance_src->codecs, &vinstance_dst->codecs, vinstance_dst);
        }
-       if (tinstance0 && tinstance1) {
-               ast_rtp_codecs_payloads_copy(&tinstance0->codecs, &tinstance1->codecs, tinstance1);
+       if (tinstance_dst && tinstance_src) {
+               ast_rtp_codecs_payloads_copy(&tinstance_src->codecs, &tinstance_dst->codecs, tinstance_dst);
        }
 
-       if (glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0)) {
+       if (glue_dst->update_peer(c_dst, instance_src, vinstance_src, tinstance_src, cap_src, 0)) {
                ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n",
-                       ast_channel_name(c0), ast_channel_name(c1));
+                       ast_channel_name(c_dst), ast_channel_name(c_src));
        } else {
                ast_debug(1, "Seeded SDP of '%s' with that of '%s'\n",
-                       ast_channel_name(c0), ast_channel_name(c1));
+                       ast_channel_name(c_dst), ast_channel_name(c_src));
        }
 
 done:
-       ast_channel_unlock(c0);
-       ast_channel_unlock(c1);
+       ast_channel_unlock(c_dst);
+       ast_channel_unlock(c_src);
 
-       ast_format_cap_destroy(cap0);
-       ast_format_cap_destroy(cap1);
+       ast_format_cap_destroy(cap_dst);
+       ast_format_cap_destroy(cap_src);
 
-       unref_instance_cond(&instance0);
-       unref_instance_cond(&instance1);
-       unref_instance_cond(&vinstance0);
-       unref_instance_cond(&vinstance1);
-       unref_instance_cond(&tinstance0);
-       unref_instance_cond(&tinstance1);
+       unref_instance_cond(&instance_dst);
+       unref_instance_cond(&instance_src);
+       unref_instance_cond(&vinstance_dst);
+       unref_instance_cond(&vinstance_src);
+       unref_instance_cond(&tinstance_dst);
+       unref_instance_cond(&tinstance_src);
 }
 
 int ast_rtp_instance_early_bridge(struct ast_channel *c0, struct ast_channel *c1)
@@ -1686,28 +1175,18 @@ int ast_rtp_instance_early_bridge(struct ast_channel *c0, struct ast_channel *c1
        struct ast_rtp_glue *glue0, *glue1;
        enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
        enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
-       struct ast_format_cap *cap0 = ast_format_cap_alloc_nolock();
-       struct ast_format_cap *cap1 = ast_format_cap_alloc_nolock();
-       int res = 0;
+       struct ast_format_cap *cap0 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
+       struct ast_format_cap *cap1 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
 
        /* If there is no second channel just immediately bail out, we are of no use in that scenario */
-       if (!c1) {
+       if (!c1 || !cap1 || !cap0) {
                ast_format_cap_destroy(cap0);
                ast_format_cap_destroy(cap1);
                return -1;
        }
 
        /* Lock both channels so we can look for the glue that binds them together */
-       ast_channel_lock(c0);
-       while (ast_channel_trylock(c1)) {
-               ast_channel_unlock(c0);
-               usleep(1);
-               ast_channel_lock(c0);
-       }
-
-       if (!cap1 || !cap0) {
-               goto done;
-       }
+       ast_channel_lock_both(c0, c1);
 
        /* Grab glue that binds each channel to something using the RTP engine */
        if (!(glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) || !(glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type))) {
@@ -1750,8 +1229,6 @@ int ast_rtp_instance_early_bridge(struct ast_channel *c0, struct ast_channel *c1
                ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
        }
 
-       res = 0;
-
 done:
        ast_channel_unlock(c0);
        ast_channel_unlock(c1);
@@ -1766,11 +1243,9 @@ done:
        unref_instance_cond(&tinstance0);
        unref_instance_cond(&tinstance1);
 
-       if (!res) {
-               ast_debug(1, "Setting early bridge SDP of '%s' with that of '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
-       }
+       ast_debug(1, "Setting early bridge SDP of '%s' with that of '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
 
-       return res;
+       return 0;
 }
 
 int ast_rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations)
@@ -1813,8 +1288,8 @@ char *ast_rtp_instance_get_quality(struct ast_rtp_instance *instance, enum ast_r
 
        /* Now actually fill the buffer with the good information */
        if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
-               snprintf(buf, size, "ssrc=%i;themssrc=%u;lp=%u;rxjitter=%f;rxcount=%u;txjitter=%f;txcount=%u;rlp=%u;rtt=%f",
-                        stats.local_ssrc, stats.remote_ssrc, stats.rxploss, stats.txjitter, stats.rxcount, stats.rxjitter, stats.txcount, stats.txploss, stats.rtt);
+               snprintf(buf, size, "ssrc=%u;themssrc=%u;lp=%u;rxjitter=%f;rxcount=%u;txjitter=%f;txcount=%u;rlp=%u;rtt=%f",
+                        stats.local_ssrc, stats.remote_ssrc, stats.rxploss, stats.rxjitter, stats.rxcount, stats.txjitter, stats.txcount, stats.txploss, stats.rtt);
        } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
                snprintf(buf, size, "minrxjitter=%f;maxrxjitter=%f;avgrxjitter=%f;stdevrxjitter=%f;reported_minjitter=%f;reported_maxjitter=%f;reported_avgjitter=%f;reported_stdevjitter=%f;",
                         stats.local_minjitter, stats.local_maxjitter, stats.local_normdevjitter, sqrt(stats.local_stdevjitter), stats.remote_minjitter, stats.remote_maxjitter, stats.remote_normdevjitter, sqrt(stats.remote_stdevjitter));
@@ -1830,36 +1305,64 @@ char *ast_rtp_instance_get_quality(struct ast_rtp_instance *instance, enum ast_r
 
 void ast_rtp_instance_set_stats_vars(struct ast_channel *chan, struct ast_rtp_instance *instance)
 {
-       char quality_buf[AST_MAX_USER_FIELD], *quality;
-       struct ast_channel *bridge = ast_bridged_channel(chan);
+       char quality_buf[AST_MAX_USER_FIELD];
+       char *quality;
+       struct ast_channel *bridge = ast_channel_bridge_peer(chan);
+
+       ast_channel_lock(chan);
+       ast_channel_stage_snapshot(chan);
+       ast_channel_unlock(chan);
+       if (bridge) {
+               ast_channel_lock(bridge);
+               ast_channel_stage_snapshot(bridge);
+               ast_channel_unlock(bridge);
+       }
 
-       if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
+       quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY,
+               quality_buf, sizeof(quality_buf));
+       if (quality) {
                pbx_builtin_setvar_helper(chan, "RTPAUDIOQOS", quality);
                if (bridge) {
                        pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSBRIDGED", quality);
                }
        }
 
-       if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER, quality_buf, sizeof(quality_buf)))) {
+       quality = ast_rtp_instance_get_quality(instance,
+               AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER, quality_buf, sizeof(quality_buf));
+       if (quality) {
                pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSJITTER", quality);
                if (bridge) {
                        pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSJITTERBRIDGED", quality);
                }
        }
 
-       if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS, quality_buf, sizeof(quality_buf)))) {
+       quality = ast_rtp_instance_get_quality(instance,
+               AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS, quality_buf, sizeof(quality_buf));
+       if (quality) {
                pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSLOSS", quality);
                if (bridge) {
                        pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSLOSSBRIDGED", quality);
                }
        }
 
-       if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT, quality_buf, sizeof(quality_buf)))) {
+       quality = ast_rtp_instance_get_quality(instance,
+               AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT, quality_buf, sizeof(quality_buf));
+       if (quality) {
                pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSRTT", quality);
                if (bridge) {
                        pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSRTTBRIDGED", quality);
                }
        }
+
+       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);
+       }
 }
 
 int ast_rtp_instance_set_read_format(struct ast_rtp_instance *instance, struct ast_format *format)
@@ -1890,11 +1393,15 @@ int ast_rtp_instance_make_compatible(struct ast_channel *chan, struct ast_rtp_in
        }
 
        glue->get_rtp_info(peer, &peer_instance);
-
-       if (!peer_instance || peer_instance->engine != instance->engine) {
+       if (!peer_instance) {
+               ast_log(LOG_ERROR, "Unable to get_rtp_info for peer type %s\n", glue->type);
+               ast_channel_unlock(peer);
+               return -1;
+       }
+       if (peer_instance->engine != instance->engine) {
+               ast_log(LOG_ERROR, "Peer engine mismatch for type %s\n", glue->type);
                ast_channel_unlock(peer);
                ao2_ref(peer_instance, -1);
-               peer_instance = NULL;
                return -1;
        }
 
@@ -1974,11 +1481,6 @@ struct ast_rtp_glue *ast_rtp_instance_get_active_glue(struct ast_rtp_instance *i
        return instance->glue;
 }
 
-struct ast_channel *ast_rtp_instance_get_chan(struct ast_rtp_instance *instance)
-{
-       return instance->chan;
-}
-
 int ast_rtp_engine_register_srtp(struct ast_srtp_res *srtp_res, struct ast_srtp_policy_res *policy_res)
 {
        if (res_srtp || res_srtp_policy) {
@@ -2044,6 +1546,74 @@ struct ast_rtp_engine_ice *ast_rtp_instance_get_ice(struct ast_rtp_instance *ins
        return instance->engine->ice;
 }
 
+struct ast_rtp_engine_dtls *ast_rtp_instance_get_dtls(struct ast_rtp_instance *instance)
+{
+       return instance->engine->dtls;
+}
+
+int ast_rtp_dtls_cfg_parse(struct ast_rtp_dtls_cfg *dtls_cfg, const char *name, const char *value)
+{
+       if (!strcasecmp(name, "dtlsenable")) {
+               dtls_cfg->enabled = ast_true(value) ? 1 : 0;
+       } else if (!strcasecmp(name, "dtlsverify")) {
+               dtls_cfg->verify = ast_true(value) ? 1 : 0;
+       } else if (!strcasecmp(name, "dtlsrekey")) {
+               if (sscanf(value, "%30u", &dtls_cfg->rekey) != 1) {
+                       return -1;
+               }
+       } else if (!strcasecmp(name, "dtlscertfile")) {
+               ast_free(dtls_cfg->certfile);
+               dtls_cfg->certfile = ast_strdup(value);
+       } else if (!strcasecmp(name, "dtlsprivatekey")) {
+               ast_free(dtls_cfg->pvtfile);
+               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);
+               dtls_cfg->cafile = ast_strdup(value);
+       } else if (!strcasecmp(name, "dtlscapath") || !strcasecmp(name, "dtlscadir")) {
+               ast_free(dtls_cfg->capath);
+               dtls_cfg->capath = ast_strdup(value);
+       } else if (!strcasecmp(name, "dtlssetup")) {
+               if (!strcasecmp(value, "active")) {
+                       dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_ACTIVE;
+               } else if (!strcasecmp(value, "passive")) {
+                       dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_PASSIVE;
+               } else if (!strcasecmp(value, "actpass")) {
+                       dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_ACTPASS;
+               }
+       } else {
+               return -1;
+       }
+
+       return 0;
+}
+
+void ast_rtp_dtls_cfg_copy(const struct ast_rtp_dtls_cfg *src_cfg, struct ast_rtp_dtls_cfg *dst_cfg)
+{
+       dst_cfg->enabled = src_cfg->enabled;
+       dst_cfg->verify = src_cfg->verify;
+       dst_cfg->rekey = src_cfg->rekey;
+       dst_cfg->suite = src_cfg->suite;
+       dst_cfg->certfile = ast_strdup(src_cfg->certfile);
+       dst_cfg->pvtfile = ast_strdup(src_cfg->pvtfile);
+       dst_cfg->cipher = ast_strdup(src_cfg->cipher);
+       dst_cfg->cafile = ast_strdup(src_cfg->cafile);
+       dst_cfg->capath = ast_strdup(src_cfg->capath);
+       dst_cfg->default_setup = src_cfg->default_setup;
+}
+
+void ast_rtp_dtls_cfg_free(struct ast_rtp_dtls_cfg *dtls_cfg)
+{
+       ast_free(dtls_cfg->certfile);
+       ast_free(dtls_cfg->pvtfile);
+       ast_free(dtls_cfg->cipher);
+       ast_free(dtls_cfg->cafile);
+       ast_free(dtls_cfg->capath);
+}
+
 static void set_next_mime_type(const struct ast_format *format, int rtp_code, char *type, char *subtype, unsigned int sample_rate)
 {
        int x = mime_types_len;
@@ -2080,7 +1650,7 @@ static void add_static_payload(int map, const struct ast_format *format, int rtp
        }
 
        if (map < 0) {
-               ast_log(LOG_WARNING, "No Dynamic RTP mapping avaliable for format %s\n" ,ast_getformatname(format));
+               ast_log(LOG_WARNING, "No Dynamic RTP mapping available for format %s\n" ,ast_getformatname(format));
                ast_rwlock_unlock(&static_RTP_PT_lock);
                return;
        }
@@ -2141,6 +1711,260 @@ int ast_rtp_engine_unload_format(const struct ast_format *format)
        return 0;
 }
 
+/*!
+ * \internal
+ * \brief \ref stasis message payload for RTCP messages
+ */
+struct rtcp_message_payload {
+       struct ast_channel_snapshot *snapshot;  /*< The channel snapshot, if available */
+       struct ast_rtp_rtcp_report *report;     /*< The RTCP report */
+       struct ast_json *blob;                  /*< Extra JSON data to publish */
+};
+
+static void rtcp_message_payload_dtor(void *obj)
+{
+       struct rtcp_message_payload *payload = obj;
+
+       ao2_cleanup(payload->report);
+       ao2_cleanup(payload->snapshot);
+       ast_json_unref(payload->blob);
+}
+
+static struct ast_manager_event_blob *rtcp_report_to_ami(struct stasis_message *msg)
+{
+       struct rtcp_message_payload *payload = stasis_message_data(msg);
+       RAII_VAR(struct ast_str *, channel_string, NULL, ast_free);
+       RAII_VAR(struct ast_str *, packet_string, ast_str_create(512), ast_free);
+       unsigned int ssrc = payload->report->ssrc;
+       unsigned int type = payload->report->type;
+       unsigned int report_count = payload->report->reception_report_count;
+       int i;
+
+       if (!packet_string) {
+               return NULL;
+       }
+
+       if (payload->snapshot) {
+               channel_string = ast_manager_build_channel_state_string(payload->snapshot);
+               if (!channel_string) {
+                       return NULL;
+               }
+       }
+
+       if (payload->blob) {
+               /* Optional data */
+               struct ast_json *to = ast_json_object_get(payload->blob, "to");
+               struct ast_json *from = ast_json_object_get(payload->blob, "from");
+               struct ast_json *rtt = ast_json_object_get(payload->blob, "rtt");
+               if (to) {
+                       ast_str_append(&packet_string, 0, "To: %s\r\n", ast_json_string_get(to));
+               }
+               if (from) {
+                       ast_str_append(&packet_string, 0, "From: %s\r\n", ast_json_string_get(from));
+               }
+               if (rtt) {
+                       ast_str_append(&packet_string, 0, "RTT: %4.4f\r\n", ast_json_real_get(rtt));
+               }
+       }
+
+       ast_str_append(&packet_string, 0, "SSRC: 0x%.8x\r\n", ssrc);
+       ast_str_append(&packet_string, 0, "PT: %u(%s)\r\n", type, type== AST_RTP_RTCP_SR ? "SR" : "RR");
+       ast_str_append(&packet_string, 0, "ReportCount: %u\r\n", report_count);
+       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);
+               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",
+                               payload->report->sender_information.packet_count);
+               ast_str_append(&packet_string, 0, "SentOctets: %u\r\n",
+                               payload->report->sender_information.octet_count);
+       }
+
+       for (i = 0; i < report_count; i++) {
+               RAII_VAR(struct ast_str *, report_string, NULL, ast_free);
+
+               if (!payload->report->report_block[i]) {
+                       break;
+               }
+
+               report_string = ast_str_create(256);
+               if (!report_string) {
+                       return NULL;
+               }
+
+               ast_str_append(&report_string, 0, "Report%dSourceSSRC: 0x%.8x\r\n",
+                               i, payload->report->report_block[i]->source_ssrc);
+               ast_str_append(&report_string, 0, "Report%dFractionLost: %d\r\n",
+                               i, payload->report->report_block[i]->lost_count.fraction);
+               ast_str_append(&report_string, 0, "Report%dCumulativeLost: %u\r\n",
+                               i, payload->report->report_block[i]->lost_count.packets);
+               ast_str_append(&report_string, 0, "Report%dHighestSequence: %u\r\n",
+                               i, payload->report->report_block[i]->highest_seq_no & 0xffff);
+               ast_str_append(&report_string, 0, "Report%dSequenceNumberCycles: %u\r\n",
+                               i, payload->report->report_block[i]->highest_seq_no >> 16);
+               ast_str_append(&report_string, 0, "Report%dIAJitter: %u\r\n",
+                               i, payload->report->report_block[i]->ia_jitter);
+               ast_str_append(&report_string, 0, "Report%dLSR: %u\r\n",
+                               i, payload->report->report_block[i]->lsr);
+               ast_str_append(&report_string, 0, "Report%dDLSR: %4.4f\r\n",
+                               i, ((double)payload->report->report_block[i]->dlsr) / 65536);
+               ast_str_append(&packet_string, 0, "%s", ast_str_buffer(report_string));
+       }
+
+       return ast_manager_event_blob_create(EVENT_FLAG_REPORTING,
+               stasis_message_type(msg) == ast_rtp_rtcp_received_type() ? "RTCPReceived" : "RTCPSent",
+               "%s%s",
+               AS_OR(channel_string, ""),
+               ast_str_buffer(packet_string));
+}
+
+static struct ast_json *rtcp_report_to_json(struct stasis_message *msg,
+       const struct stasis_message_sanitizer *sanitize)
+{
+       struct rtcp_message_payload *payload = stasis_message_data(msg);
+       RAII_VAR(struct ast_json *, json_rtcp_report, NULL, ast_json_unref);
+       RAII_VAR(struct ast_json *, json_rtcp_report_blocks, NULL, ast_json_unref);
+       RAII_VAR(struct ast_json *, json_rtcp_sender_info, NULL, ast_json_unref);
+       RAII_VAR(struct ast_json *, json_channel, NULL, ast_json_unref);
+       int i;
+
+       json_rtcp_report_blocks = ast_json_array_create();
+       if (!json_rtcp_report_blocks) {
+               return NULL;
+       }
+
+       for (i = 0; i < payload->report->reception_report_count; i++) {
+               struct ast_json *json_report_block;
+               json_report_block = ast_json_pack("{s: i, s: i, s: i, s: i, s: i, s: i, s: i}",
+                               "source_ssrc", payload->report->report_block[i]->source_ssrc,
+                               "fraction_lost", payload->report->report_block[i]->lost_count.fraction,
+                               "packets_lost", payload->report->report_block[i]->lost_count.packets,
+                               "highest_seq_no", payload->report->report_block[i]->highest_seq_no,
+                               "ia_jitter", payload->report->report_block[i]->ia_jitter,
+                               "lsr", payload->report->report_block[i]->lsr,
+                               "dlsr", payload->report->report_block[i]->dlsr);
+               if (!json_report_block) {
+                       return NULL;
+               }
+
+               if (ast_json_array_append(json_rtcp_report_blocks, json_report_block)) {
+                       return NULL;
+               }
+       }
+
+       if (payload->report->type == AST_RTP_RTCP_SR) {
+               json_rtcp_sender_info = ast_json_pack("{s: i, s: i, s: i, s: i, s: i}",
+                               "ntp_timestamp_sec", payload->report->sender_information.ntp_timestamp.tv_sec,
+                               "ntp_timestamp_usec", payload->report->sender_information.ntp_timestamp.tv_usec,
+                               "rtp_timestamp", payload->report->sender_information.rtp_timestamp,
+                               "packets", payload->report->sender_information.packet_count,
+                               "octets", payload->report->sender_information.octet_count);
+               if (!json_rtcp_sender_info) {
+                       return NULL;
+               }
+       }
+
+       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);
+       if (!json_rtcp_report) {
+               return NULL;
+       }
+
+       if (payload->snapshot) {
+               json_channel = ast_channel_snapshot_to_json(payload->snapshot, sanitize);
+               if (!json_channel) {
+                       return NULL;
+               }
+       }
+
+       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);
+}
+
+static void rtp_rtcp_report_dtor(void *obj)
+{
+       int i;
+       struct ast_rtp_rtcp_report *rtcp_report = obj;
+
+       for (i = 0; i < rtcp_report->reception_report_count; i++) {
+               ast_free(rtcp_report->report_block[i]);
+       }
+}
+
+struct ast_rtp_rtcp_report *ast_rtp_rtcp_report_alloc(unsigned int report_blocks)
+{
+       struct ast_rtp_rtcp_report *rtcp_report;
+
+       /* Size of object is sizeof the report + the number of report_blocks * sizeof pointer */
+       rtcp_report = ao2_alloc((sizeof(*rtcp_report) + report_blocks * sizeof(struct ast_rtp_rtcp_report_block *)),
+               rtp_rtcp_report_dtor);
+
+       return rtcp_report;
+}
+
+void ast_rtp_publish_rtcp_message(struct ast_rtp_instance *rtp,
+               struct stasis_message_type *message_type,
+               struct ast_rtp_rtcp_report *report,
+               struct ast_json *blob)
+{
+       RAII_VAR(struct rtcp_message_payload *, payload, NULL, ao2_cleanup);
+       RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
+
+       payload = ao2_alloc(sizeof(*payload), rtcp_message_payload_dtor);
+       if (!payload || !report) {
+               return;
+       }
+
+       if (!ast_strlen_zero(rtp->channel_uniqueid)) {
+               payload->snapshot = ast_channel_snapshot_get_latest(rtp->channel_uniqueid);
+       }
+       if (blob) {
+               payload->blob = blob;
+               ast_json_ref(blob);
+       }
+       ao2_ref(report, +1);
+       payload->report = report;
+
+       message = stasis_message_create(message_type, payload);
+       if (!message) {
+               return;
+       }
+
+       stasis_publish(ast_rtp_topic(), message);
+}
+
+/*!
+ * @{ \brief Define RTCP/RTP message types.
+ */
+STASIS_MESSAGE_TYPE_DEFN(ast_rtp_rtcp_sent_type,
+               .to_ami = rtcp_report_to_ami,
+               .to_json = rtcp_report_to_json,);
+STASIS_MESSAGE_TYPE_DEFN(ast_rtp_rtcp_received_type,
+               .to_ami = rtcp_report_to_ami,
+               .to_json = rtcp_report_to_json,);
+/*! @} */
+
+struct stasis_topic *ast_rtp_topic(void)
+{
+       return rtp_topic;
+}
+
+static void rtp_engine_shutdown(void)
+{
+       ao2_cleanup(rtp_topic);
+       rtp_topic = NULL;
+       STASIS_MESSAGE_TYPE_CLEANUP(ast_rtp_rtcp_received_type);
+       STASIS_MESSAGE_TYPE_CLEANUP(ast_rtp_rtcp_sent_type);
+}
+
 int ast_rtp_engine_init()
 {
        struct ast_format tmpfmt;
@@ -2148,6 +1972,14 @@ int ast_rtp_engine_init()
        ast_rwlock_init(&mime_types_lock);
        ast_rwlock_init(&static_RTP_PT_lock);
 
+       rtp_topic = stasis_topic_create("rtp_topic");
+       if (!rtp_topic) {
+               return -1;
+       }
+       STASIS_MESSAGE_TYPE_INIT(ast_rtp_rtcp_sent_type);
+       STASIS_MESSAGE_TYPE_INIT(ast_rtp_rtcp_received_type);
+       ast_register_atexit(rtp_engine_shutdown);
+
        /* Define all the RTP mime types available */
        set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G723_1, 0), 0, "audio", "G723", 8000);
        set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_GSM, 0), 0, "audio", "GSM", 8000);
@@ -2178,7 +2010,7 @@ int ast_rtp_engine_init()
        set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_PNG, 0), 0, "video", "PNG", 90000);
        set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H261, 0), 0, "video", "H261", 90000);
        set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H263, 0), 0, "video", "H263", 90000);
-       set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0, "video", "h263-1998", 90000);
+       set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0, "video", "H263-1998", 90000);
        set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H264, 0), 0, "video", "H264", 90000);
        set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_MP4_VIDEO, 0), 0, "video", "MP4V-ES", 90000);
        set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_T140RED, 0), 0, "text", "RED", 1000);
@@ -2186,6 +2018,9 @@ int ast_rtp_engine_init()
        set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SIREN7, 0), 0, "audio", "G7221", 16000);
        set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SIREN14, 0), 0, "audio", "G7221", 32000);
        set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G719, 0), 0, "audio", "G719", 48000);
+       /* Opus and VP8 */
+       set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_OPUS, 0), 0,  "audio", "opus", 48000);
+       set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_VP8, 0), 0,  "video", "VP8", 90000);
 
        /* Define the static rtp payload mappings */
        add_static_payload(0, ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0);
@@ -2227,6 +2062,9 @@ int ast_rtp_engine_init()
        add_static_payload(118, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0); /* 16 Khz signed linear */
        add_static_payload(119, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX32, 0), 0);
        add_static_payload(121, NULL, AST_RTP_CISCO_DTMF);   /* Must be type 121 */
+       /* Opus and VP8 */
+       add_static_payload(100, ast_format_set(&tmpfmt, AST_FORMAT_VP8, 0), 0);
+       add_static_payload(107, ast_format_set(&tmpfmt, AST_FORMAT_OPUS, 0), 0);
 
        return 0;
 }