2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2008, Digium, Inc.
6 * Joshua Colp <jcolp@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Pluggable RTP Architecture
23 * \author Joshua Colp <jcolp@digium.com>
27 <support_level>core</support_level>
31 <managerEvent language="en_US" name="RTCPSent">
32 <managerEventInstance class="EVENT_FLAG_REPORTING">
33 <synopsis>Raised when an RTCP packet is sent.</synopsis>
36 <parameter name="SSRC">
37 <para>The SSRC identifier for our stream</para>
40 <para>The type of packet for this RTCP report.</para>
42 <enum name="200(SR)"/>
43 <enum name="201(RR)"/>
47 <para>The address the report is sent to.</para>
49 <parameter name="ReportCount">
50 <para>The number of reports that were sent.</para>
51 <para>The report count determines the number of ReportX headers in
52 the message. The X for each set of report headers will range from 0 to
53 <literal>ReportCount - 1</literal>.</para>
55 <parameter name="SentNTP" required="false">
56 <para>The time the sender generated the report. Only valid when
57 PT is <literal>200(SR)</literal>.</para>
59 <parameter name="SentRTP" required="false">
60 <para>The sender's last RTP timestamp. Only valid when PT is
61 <literal>200(SR)</literal>.</para>
63 <parameter name="SentPackets" required="false">
64 <para>The number of packets the sender has sent. Only valid when PT
65 is <literal>200(SR)</literal>.</para>
67 <parameter name="SentOctets" required="false">
68 <para>The number of bytes the sender has sent. Only valid when PT is
69 <literal>200(SR)</literal>.</para>
71 <parameter name="ReportXSourceSSRC">
72 <para>The SSRC for the source of this report block.</para>
74 <parameter name="ReportXFractionLost">
75 <para>The fraction of RTP data packets from <literal>ReportXSourceSSRC</literal>
76 lost since the previous SR or RR report was sent.</para>
78 <parameter name="ReportXCumulativeLost">
79 <para>The total number of RTP data packets from <literal>ReportXSourceSSRC</literal>
80 lost since the beginning of reception.</para>
82 <parameter name="ReportXHighestSequence">
83 <para>The highest sequence number received in an RTP data packet from
84 <literal>ReportXSourceSSRC</literal>.</para>
86 <parameter name="ReportXSequenceNumberCycles">
87 <para>The number of sequence number cycles seen for the RTP data
88 received from <literal>ReportXSourceSSRC</literal>.</para>
90 <parameter name="ReportXIAJitter">
91 <para>An estimate of the statistical variance of the RTP data packet
92 interarrival time, measured in timestamp units.</para>
94 <parameter name="ReportXLSR">
95 <para>The last SR timestamp received from <literal>ReportXSourceSSRC</literal>.
96 If no SR has been received from <literal>ReportXSourceSSRC</literal>,
99 <parameter name="ReportXDLSR">
100 <para>The delay, expressed in units of 1/65536 seconds, between
101 receiving the last SR packet from <literal>ReportXSourceSSRC</literal>
102 and sending this report.</para>
105 </managerEventInstance>
107 <managerEvent language="en_US" name="RTCPReceived">
108 <managerEventInstance class="EVENT_FLAG_REPORTING">
109 <synopsis>Raised when an RTCP packet is received.</synopsis>
112 <parameter name="SSRC">
113 <para>The SSRC identifier for the remote system</para>
115 <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='PT'])" />
116 <parameter name="From">
117 <para>The address the report was received from.</para>
119 <parameter name="RTT">
120 <para>Calculated Round-Trip Time in seconds</para>
122 <parameter name="ReportCount">
123 <para>The number of reports that were received.</para>
124 <para>The report count determines the number of ReportX headers in
125 the message. The X for each set of report headers will range from 0 to
126 <literal>ReportCount - 1</literal>.</para>
128 <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentNTP'])" />
129 <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentRTP'])" />
130 <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentPackets'])" />
131 <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentOctets'])" />
132 <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[contains(@name, 'ReportX')])" />
134 </managerEventInstance>
138 #include "asterisk.h"
140 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
144 #include "asterisk/channel.h"
145 #include "asterisk/frame.h"
146 #include "asterisk/module.h"
147 #include "asterisk/rtp_engine.h"
148 #include "asterisk/manager.h"
149 #include "asterisk/options.h"
150 #include "asterisk/astobj2.h"
151 #include "asterisk/pbx.h"
152 #include "asterisk/translate.h"
153 #include "asterisk/netsock2.h"
154 #include "asterisk/_private.h"
155 #include "asterisk/framehook.h"
156 #include "asterisk/stasis.h"
157 #include "asterisk/json.h"
158 #include "asterisk/stasis_channels.h"
160 struct ast_srtp_res *res_srtp = NULL;
161 struct ast_srtp_policy_res *res_srtp_policy = NULL;
163 /*! Structure that represents an RTP session (instance) */
164 struct ast_rtp_instance {
165 /*! Engine that is handling this RTP instance */
166 struct ast_rtp_engine *engine;
167 /*! Data unique to the RTP engine */
169 /*! RTP properties that have been set and their value */
170 int properties[AST_RTP_PROPERTY_MAX];
171 /*! Address that we are expecting RTP to come in to */
172 struct ast_sockaddr local_address;
173 /*! Address that we are sending RTP to */
174 struct ast_sockaddr remote_address;
175 /*! Instance that we are bridged to if doing remote or local bridging */
176 struct ast_rtp_instance *bridged;
177 /*! Payload and packetization information */
178 struct ast_rtp_codecs codecs;
179 /*! RTP timeout time (negative or zero means disabled, negative value means temporarily disabled) */
181 /*! RTP timeout when on hold (negative or zero means disabled, negative value means temporarily disabled). */
183 /*! RTP keepalive interval */
185 /*! Glue currently in use */
186 struct ast_rtp_glue *glue;
187 /*! SRTP info associated with the instance */
188 struct ast_srtp *srtp;
189 /*! Channel unique ID */
190 char channel_uniqueid[AST_MAX_UNIQUEID];
193 /*! List of RTP engines that are currently registered */
194 static AST_RWLIST_HEAD_STATIC(engines, ast_rtp_engine);
196 /*! List of RTP glues */
197 static AST_RWLIST_HEAD_STATIC(glues, ast_rtp_glue);
199 /*! The following array defines the MIME Media type (and subtype) for each
200 of our codecs, or RTP-specific data type. */
201 static struct ast_rtp_mime_type {
202 struct ast_rtp_payload_type payload_type;
205 unsigned int sample_rate;
206 } ast_rtp_mime_types[128]; /* This will Likely not need to grow any time soon. */
207 static ast_rwlock_t mime_types_lock;
208 static int mime_types_len = 0;
211 * \brief Mapping between Asterisk codecs and rtp payload types
213 * Static (i.e., well-known) RTP payload types for our "AST_FORMAT..."s:
214 * also, our own choices for dynamic payload types. This is our master
215 * table for transmission
217 * See http://www.iana.org/assignments/rtp-parameters for a list of
220 static struct ast_rtp_payload_type static_RTP_PT[AST_RTP_MAX_PT];
221 static ast_rwlock_t static_RTP_PT_lock;
223 /*! \brief \ref stasis topic for RTP related messages */
224 static struct stasis_topic *rtp_topic;
226 int ast_rtp_engine_register2(struct ast_rtp_engine *engine, struct ast_module *module)
228 struct ast_rtp_engine *current_engine;
230 /* Perform a sanity check on the engine structure to make sure it has the basics */
231 if (ast_strlen_zero(engine->name) || !engine->new || !engine->destroy || !engine->write || !engine->read) {
232 ast_log(LOG_WARNING, "RTP Engine '%s' failed sanity check so it was not registered.\n", !ast_strlen_zero(engine->name) ? engine->name : "Unknown");
236 /* Link owner module to the RTP engine for reference counting purposes */
237 engine->mod = module;
239 AST_RWLIST_WRLOCK(&engines);
241 /* Ensure that no two modules with the same name are registered at the same time */
242 AST_RWLIST_TRAVERSE(&engines, current_engine, entry) {
243 if (!strcmp(current_engine->name, engine->name)) {
244 ast_log(LOG_WARNING, "An RTP engine with the name '%s' has already been registered.\n", engine->name);
245 AST_RWLIST_UNLOCK(&engines);
250 /* The engine survived our critique. Off to the list it goes to be used */
251 AST_RWLIST_INSERT_TAIL(&engines, engine, entry);
253 AST_RWLIST_UNLOCK(&engines);
255 ast_verb(2, "Registered RTP engine '%s'\n", engine->name);
260 int ast_rtp_engine_unregister(struct ast_rtp_engine *engine)
262 struct ast_rtp_engine *current_engine = NULL;
264 AST_RWLIST_WRLOCK(&engines);
266 if ((current_engine = AST_RWLIST_REMOVE(&engines, engine, entry))) {
267 ast_verb(2, "Unregistered RTP engine '%s'\n", engine->name);
270 AST_RWLIST_UNLOCK(&engines);
272 return current_engine ? 0 : -1;
275 int ast_rtp_glue_register2(struct ast_rtp_glue *glue, struct ast_module *module)
277 struct ast_rtp_glue *current_glue = NULL;
279 if (ast_strlen_zero(glue->type)) {
285 AST_RWLIST_WRLOCK(&glues);
287 AST_RWLIST_TRAVERSE(&glues, current_glue, entry) {
288 if (!strcasecmp(current_glue->type, glue->type)) {
289 ast_log(LOG_WARNING, "RTP glue with the name '%s' has already been registered.\n", glue->type);
290 AST_RWLIST_UNLOCK(&glues);
295 AST_RWLIST_INSERT_TAIL(&glues, glue, entry);
297 AST_RWLIST_UNLOCK(&glues);
299 ast_verb(2, "Registered RTP glue '%s'\n", glue->type);
304 int ast_rtp_glue_unregister(struct ast_rtp_glue *glue)
306 struct ast_rtp_glue *current_glue = NULL;
308 AST_RWLIST_WRLOCK(&glues);
310 if ((current_glue = AST_RWLIST_REMOVE(&glues, glue, entry))) {
311 ast_verb(2, "Unregistered RTP glue '%s'\n", glue->type);
314 AST_RWLIST_UNLOCK(&glues);
316 return current_glue ? 0 : -1;
319 static void instance_destructor(void *obj)
321 struct ast_rtp_instance *instance = obj;
323 /* Pass us off to the engine to destroy */
324 if (instance->data && instance->engine->destroy(instance)) {
325 ast_debug(1, "Engine '%s' failed to destroy RTP instance '%p'\n", instance->engine->name, instance);
329 if (instance->srtp) {
330 res_srtp->destroy(instance->srtp);
333 ast_rtp_codecs_payloads_destroy(&instance->codecs);
335 /* Drop our engine reference */
336 ast_module_unref(instance->engine->mod);
338 ast_debug(1, "Destroyed RTP instance '%p'\n", instance);
341 int ast_rtp_instance_destroy(struct ast_rtp_instance *instance)
343 ao2_ref(instance, -1);
348 struct ast_rtp_instance *ast_rtp_instance_new(const char *engine_name,
349 struct ast_sched_context *sched, const struct ast_sockaddr *sa,
352 struct ast_sockaddr address = {{0,}};
353 struct ast_rtp_instance *instance = NULL;
354 struct ast_rtp_engine *engine = NULL;
356 AST_RWLIST_RDLOCK(&engines);
358 /* If an engine name was specified try to use it or otherwise use the first one registered */
359 if (!ast_strlen_zero(engine_name)) {
360 AST_RWLIST_TRAVERSE(&engines, engine, entry) {
361 if (!strcmp(engine->name, engine_name)) {
366 engine = AST_RWLIST_FIRST(&engines);
369 /* If no engine was actually found bail out now */
371 ast_log(LOG_ERROR, "No RTP engine was found. Do you have one loaded?\n");
372 AST_RWLIST_UNLOCK(&engines);
376 /* Bump up the reference count before we return so the module can not be unloaded */
377 ast_module_ref(engine->mod);
379 AST_RWLIST_UNLOCK(&engines);
381 /* Allocate a new RTP instance */
382 if (!(instance = ao2_alloc(sizeof(*instance), instance_destructor))) {
383 ast_module_unref(engine->mod);
386 instance->engine = engine;
387 ast_sockaddr_copy(&instance->local_address, sa);
388 ast_sockaddr_copy(&address, sa);
390 if (ast_rtp_codecs_payloads_initialize(&instance->codecs)) {
391 ao2_ref(instance, -1);
395 ast_debug(1, "Using engine '%s' for RTP instance '%p'\n", engine->name, instance);
397 /* And pass it off to the engine to setup */
398 if (instance->engine->new(instance, sched, &address, data)) {
399 ast_debug(1, "Engine '%s' failed to setup RTP instance '%p'\n", engine->name, instance);
400 ao2_ref(instance, -1);
404 ast_debug(1, "RTP instance '%p' is setup and ready to go\n", instance);
409 const char *ast_rtp_instance_get_channel_id(struct ast_rtp_instance *instance)
411 return instance->channel_uniqueid;
414 void ast_rtp_instance_set_channel_id(struct ast_rtp_instance *instance, const char *uniqueid)
416 ast_copy_string(instance->channel_uniqueid, uniqueid, sizeof(instance->channel_uniqueid));
419 void ast_rtp_instance_set_data(struct ast_rtp_instance *instance, void *data)
421 instance->data = data;
424 void *ast_rtp_instance_get_data(struct ast_rtp_instance *instance)
426 return instance->data;
429 int ast_rtp_instance_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
431 return instance->engine->write(instance, frame);
434 struct ast_frame *ast_rtp_instance_read(struct ast_rtp_instance *instance, int rtcp)
436 return instance->engine->read(instance, rtcp);
439 int ast_rtp_instance_set_local_address(struct ast_rtp_instance *instance,
440 const struct ast_sockaddr *address)
442 ast_sockaddr_copy(&instance->local_address, address);
446 int ast_rtp_instance_set_remote_address(struct ast_rtp_instance *instance,
447 const struct ast_sockaddr *address)
449 ast_sockaddr_copy(&instance->remote_address, address);
453 if (instance->engine->remote_address_set) {
454 instance->engine->remote_address_set(instance, &instance->remote_address);
460 int ast_rtp_instance_get_and_cmp_local_address(struct ast_rtp_instance *instance,
461 struct ast_sockaddr *address)
463 if (ast_sockaddr_cmp(address, &instance->local_address) != 0) {
464 ast_sockaddr_copy(address, &instance->local_address);
471 void ast_rtp_instance_get_local_address(struct ast_rtp_instance *instance,
472 struct ast_sockaddr *address)
474 ast_sockaddr_copy(address, &instance->local_address);
477 int ast_rtp_instance_get_and_cmp_remote_address(struct ast_rtp_instance *instance,
478 struct ast_sockaddr *address)
480 if (ast_sockaddr_cmp(address, &instance->remote_address) != 0) {
481 ast_sockaddr_copy(address, &instance->remote_address);
488 void ast_rtp_instance_get_remote_address(struct ast_rtp_instance *instance,
489 struct ast_sockaddr *address)
491 ast_sockaddr_copy(address, &instance->remote_address);
494 void ast_rtp_instance_set_extended_prop(struct ast_rtp_instance *instance, int property, void *value)
496 if (instance->engine->extended_prop_set) {
497 instance->engine->extended_prop_set(instance, property, value);
501 void *ast_rtp_instance_get_extended_prop(struct ast_rtp_instance *instance, int property)
503 if (instance->engine->extended_prop_get) {
504 return instance->engine->extended_prop_get(instance, property);
510 void ast_rtp_instance_set_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value)
512 instance->properties[property] = value;
514 if (instance->engine->prop_set) {
515 instance->engine->prop_set(instance, property, value);
519 int ast_rtp_instance_get_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property)
521 return instance->properties[property];
524 struct ast_rtp_codecs *ast_rtp_instance_get_codecs(struct ast_rtp_instance *instance)
526 return &instance->codecs;
529 static int rtp_payload_type_hash(const void *obj, const int flags)
531 const struct ast_rtp_payload_type *type = obj;
532 const int *payload = obj;
534 return (flags & OBJ_KEY) ? *payload : type->payload;
537 static int rtp_payload_type_cmp(void *obj, void *arg, int flags)
539 struct ast_rtp_payload_type *type1 = obj, *type2 = arg;
540 const int *payload = arg;
542 return (type1->payload == (OBJ_KEY ? *payload : type2->payload)) ? CMP_MATCH | CMP_STOP : 0;
545 int ast_rtp_codecs_payloads_initialize(struct ast_rtp_codecs *codecs)
547 if (!(codecs->payloads = ao2_container_alloc(AST_RTP_MAX_PT, rtp_payload_type_hash, rtp_payload_type_cmp))) {
554 void ast_rtp_codecs_payloads_destroy(struct ast_rtp_codecs *codecs)
556 ao2_cleanup(codecs->payloads);
559 void ast_rtp_codecs_payloads_clear(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
561 ast_rtp_codecs_payloads_destroy(codecs);
563 if (instance && instance->engine && instance->engine->payload_set) {
565 for (i = 0; i < AST_RTP_MAX_PT; i++) {
566 instance->engine->payload_set(instance, i, 0, NULL, 0);
570 ast_rtp_codecs_payloads_initialize(codecs);
573 void ast_rtp_codecs_payloads_default(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
577 ast_rwlock_rdlock(&static_RTP_PT_lock);
578 for (i = 0; i < AST_RTP_MAX_PT; i++) {
579 if (static_RTP_PT[i].rtp_code || static_RTP_PT[i].asterisk_format) {
580 struct ast_rtp_payload_type *type;
582 if (!(type = ao2_alloc(sizeof(*type), NULL))) {
583 /* Unfortunately if this occurs the payloads container will not contain all possible default payloads
584 * but we err on the side of doing what we can in the hopes that the extreme memory conditions which
585 * caused this to occur will go away.
591 type->asterisk_format = static_RTP_PT[i].asterisk_format;
592 type->rtp_code = static_RTP_PT[i].rtp_code;
593 ast_format_copy(&type->format, &static_RTP_PT[i].format);
595 ao2_link_flags(codecs->payloads, type, OBJ_NOLOCK);
597 if (instance && instance->engine && instance->engine->payload_set) {
598 instance->engine->payload_set(instance, i, type->asterisk_format, &type->format, type->rtp_code);
604 ast_rwlock_unlock(&static_RTP_PT_lock);
607 void ast_rtp_codecs_payloads_copy(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
610 struct ast_rtp_payload_type *type;
612 for (i = 0; i < AST_RTP_MAX_PT; i++) {
613 struct ast_rtp_payload_type *new_type;
615 if (!(type = ao2_find(src->payloads, &i, OBJ_KEY | OBJ_NOLOCK))) {
619 if (!(new_type = ao2_alloc(sizeof(*new_type), NULL))) {
623 ast_debug(2, "Copying payload %d from %p to %p\n", i, src, dest);
625 new_type->payload = i;
628 ao2_link_flags(dest->payloads, new_type, OBJ_NOLOCK);
630 ao2_ref(new_type, -1);
632 if (instance && instance->engine && instance->engine->payload_set) {
633 instance->engine->payload_set(instance, i, type->asterisk_format, &type->format, type->rtp_code);
640 void ast_rtp_codecs_payloads_set_m_type(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
642 struct ast_rtp_payload_type *type;
644 ast_rwlock_rdlock(&static_RTP_PT_lock);
646 if (payload < 0 || payload >= AST_RTP_MAX_PT) {
647 ast_rwlock_unlock(&static_RTP_PT_lock);
651 if (!(type = ao2_find(codecs->payloads, &payload, OBJ_KEY | OBJ_NOLOCK))) {
652 if (!(type = ao2_alloc(sizeof(*type), NULL))) {
653 ast_rwlock_unlock(&static_RTP_PT_lock);
656 type->payload = payload;
657 ao2_link_flags(codecs->payloads, type, OBJ_NOLOCK);
660 type->asterisk_format = static_RTP_PT[payload].asterisk_format;
661 type->rtp_code = static_RTP_PT[payload].rtp_code;
662 type->payload = payload;
663 ast_format_copy(&type->format, &static_RTP_PT[payload].format);
665 ast_debug(1, "Setting payload %d based on m type on %p\n", payload, codecs);
667 if (instance && instance->engine && instance->engine->payload_set) {
668 instance->engine->payload_set(instance, payload, type->asterisk_format, &type->format, type->rtp_code);
673 ast_rwlock_unlock(&static_RTP_PT_lock);
676 int ast_rtp_codecs_payloads_set_rtpmap_type_rate(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int pt,
677 char *mimetype, char *mimesubtype,
678 enum ast_rtp_options options,
679 unsigned int sample_rate)
684 if (pt < 0 || pt >= AST_RTP_MAX_PT)
685 return -1; /* bogus payload type */
687 ast_rwlock_rdlock(&mime_types_lock);
688 for (i = 0; i < mime_types_len; ++i) {
689 const struct ast_rtp_mime_type *t = &ast_rtp_mime_types[i];
690 struct ast_rtp_payload_type *type;
692 if (strcasecmp(mimesubtype, t->subtype)) {
696 if (strcasecmp(mimetype, t->type)) {
700 /* if both sample rates have been supplied, and they don't match,
701 * then this not a match; if one has not been supplied, then the
702 * rates are not compared */
703 if (sample_rate && t->sample_rate &&
704 (sample_rate != t->sample_rate)) {
710 if (!(type = ao2_find(codecs->payloads, &pt, OBJ_KEY | OBJ_NOLOCK))) {
711 if (!(type = ao2_alloc(sizeof(*type), NULL))) {
715 ao2_link_flags(codecs->payloads, type, OBJ_NOLOCK);
718 *type = t->payload_type;
721 if ((t->payload_type.format.id == AST_FORMAT_G726) && t->payload_type.asterisk_format && (options & AST_RTP_OPT_G726_NONSTANDARD)) {
722 ast_format_set(&type->format, AST_FORMAT_G726_AAL2, 0);
725 if (instance && instance->engine && instance->engine->payload_set) {
726 instance->engine->payload_set(instance, pt, type->asterisk_format, &type->format, type->rtp_code);
733 ast_rwlock_unlock(&mime_types_lock);
735 return (found ? 0 : -2);
738 int ast_rtp_codecs_payloads_set_rtpmap_type(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload, char *mimetype, char *mimesubtype, enum ast_rtp_options options)
740 return ast_rtp_codecs_payloads_set_rtpmap_type_rate(codecs, instance, payload, mimetype, mimesubtype, options, 0);
743 void ast_rtp_codecs_payloads_unset(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
745 if (payload < 0 || payload >= AST_RTP_MAX_PT) {
749 ast_debug(2, "Unsetting payload %d on %p\n", payload, codecs);
751 ao2_find(codecs->payloads, &payload, OBJ_KEY | OBJ_NOLOCK | OBJ_NODATA | OBJ_UNLINK);
753 if (instance && instance->engine && instance->engine->payload_set) {
754 instance->engine->payload_set(instance, payload, 0, NULL, 0);
758 struct ast_rtp_payload_type ast_rtp_codecs_payload_lookup(struct ast_rtp_codecs *codecs, int payload)
760 struct ast_rtp_payload_type result = { .asterisk_format = 0, }, *type;
762 if (payload < 0 || payload >= AST_RTP_MAX_PT) {
766 if ((type = ao2_find(codecs->payloads, &payload, OBJ_KEY | OBJ_NOLOCK))) {
771 if (!result.rtp_code && !result.asterisk_format) {
772 ast_rwlock_rdlock(&static_RTP_PT_lock);
773 result = static_RTP_PT[payload];
774 ast_rwlock_unlock(&static_RTP_PT_lock);
781 struct ast_format *ast_rtp_codecs_get_payload_format(struct ast_rtp_codecs *codecs, int payload)
783 struct ast_rtp_payload_type *type;
784 struct ast_format *format;
786 if (payload < 0 || payload >= AST_RTP_MAX_PT) {
790 if (!(type = ao2_find(codecs->payloads, &payload, OBJ_KEY | OBJ_NOLOCK))) {
794 format = type->asterisk_format ? &type->format : NULL;
801 static int rtp_payload_type_add_ast(void *obj, void *arg, int flags)
803 struct ast_rtp_payload_type *type = obj;
804 struct ast_format_cap *astformats = arg;
806 if (type->asterisk_format) {
807 ast_format_cap_add(astformats, &type->format);
813 static int rtp_payload_type_add_nonast(void *obj, void *arg, int flags)
815 struct ast_rtp_payload_type *type = obj;
816 int *nonastformats = arg;
818 if (!type->asterisk_format) {
819 *nonastformats |= type->rtp_code;
825 void ast_rtp_codecs_payload_formats(struct ast_rtp_codecs *codecs, struct ast_format_cap *astformats, int *nonastformats)
827 ast_format_cap_remove_all(astformats);
830 ao2_callback(codecs->payloads, OBJ_NODATA | OBJ_MULTIPLE | OBJ_NOLOCK, rtp_payload_type_add_ast, astformats);
831 ao2_callback(codecs->payloads, OBJ_NODATA | OBJ_MULTIPLE | OBJ_NOLOCK, rtp_payload_type_add_nonast, nonastformats);
834 static int rtp_payload_type_find_format(void *obj, void *arg, int flags)
836 struct ast_rtp_payload_type *type = obj;
837 struct ast_format *format = arg;
839 return (type->asterisk_format && (ast_format_cmp(&type->format, format) != AST_FORMAT_CMP_NOT_EQUAL)) ? CMP_MATCH | CMP_STOP : 0;
842 static int rtp_payload_type_find_nonast_format(void *obj, void *arg, int flags)
844 struct ast_rtp_payload_type *type = obj;
847 return ((!type->asterisk_format && (type->rtp_code == *rtp_code)) ? CMP_MATCH | CMP_STOP : 0);
850 int ast_rtp_codecs_payload_code(struct ast_rtp_codecs *codecs, int asterisk_format, const struct ast_format *format, int code)
852 struct ast_rtp_payload_type *type;
855 if (asterisk_format && format && (type = ao2_callback(codecs->payloads, OBJ_NOLOCK, rtp_payload_type_find_format, (void*)format))) {
859 } else if (!asterisk_format && (type = ao2_callback(codecs->payloads, OBJ_NOLOCK, rtp_payload_type_find_nonast_format, (void*)&code))) {
865 ast_rwlock_rdlock(&static_RTP_PT_lock);
866 for (i = 0; i < AST_RTP_MAX_PT; i++) {
867 if (static_RTP_PT[i].asterisk_format && asterisk_format && format &&
868 (ast_format_cmp(format, &static_RTP_PT[i].format) != AST_FORMAT_CMP_NOT_EQUAL)) {
871 } else if (!static_RTP_PT[i].asterisk_format && !asterisk_format &&
872 (static_RTP_PT[i].rtp_code == code)) {
877 ast_rwlock_unlock(&static_RTP_PT_lock);
881 int ast_rtp_codecs_find_payload_code(struct ast_rtp_codecs *codecs, int code)
883 struct ast_rtp_payload_type *type;
886 /* Search the payload type in the codecs passed */
887 if ((type = ao2_find(codecs->payloads, &code, OBJ_NOLOCK | OBJ_KEY)))
896 const char *ast_rtp_lookup_mime_subtype2(const int asterisk_format, struct ast_format *format, int code, enum ast_rtp_options options)
899 const char *res = "";
901 ast_rwlock_rdlock(&mime_types_lock);
902 for (i = 0; i < mime_types_len; i++) {
903 if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
904 (ast_format_cmp(format, &ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
905 if ((format->id == AST_FORMAT_G726_AAL2) && (options & AST_RTP_OPT_G726_NONSTANDARD)) {
909 res = ast_rtp_mime_types[i].subtype;
912 } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
913 ast_rtp_mime_types[i].payload_type.rtp_code == code) {
915 res = ast_rtp_mime_types[i].subtype;
919 ast_rwlock_unlock(&mime_types_lock);
924 unsigned int ast_rtp_lookup_sample_rate2(int asterisk_format, struct ast_format *format, int code)
927 unsigned int res = 0;
929 ast_rwlock_rdlock(&mime_types_lock);
930 for (i = 0; i < mime_types_len; ++i) {
931 if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
932 (ast_format_cmp(format, &ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
933 res = ast_rtp_mime_types[i].sample_rate;
935 } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
936 ast_rtp_mime_types[i].payload_type.rtp_code == code) {
937 res = ast_rtp_mime_types[i].sample_rate;
941 ast_rwlock_unlock(&mime_types_lock);
946 char *ast_rtp_lookup_mime_multiple2(struct ast_str *buf, struct ast_format_cap *ast_format_capability, int rtp_capability, const int asterisk_format, enum ast_rtp_options options)
955 if (asterisk_format) {
956 struct ast_format tmp_fmt;
957 ast_format_cap_iter_start(ast_format_capability);
958 while (!ast_format_cap_iter_next(ast_format_capability, &tmp_fmt)) {
959 name = ast_rtp_lookup_mime_subtype2(asterisk_format, &tmp_fmt, 0, options);
960 ast_str_append(&buf, 0, "%s|", name);
963 ast_format_cap_iter_end(ast_format_capability);
967 ast_str_append(&buf, 0, "0x%x (", (unsigned int) rtp_capability);
968 for (x = 1; x <= AST_RTP_MAX; x <<= 1) {
969 if (rtp_capability & x) {
970 name = ast_rtp_lookup_mime_subtype2(asterisk_format, NULL, x, options);
971 ast_str_append(&buf, 0, "%s|", name);
977 ast_str_append(&buf, 0, "%s", found ? ")" : "nothing)");
979 return ast_str_buffer(buf);
982 void ast_rtp_codecs_packetization_set(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, struct ast_codec_pref *prefs)
984 codecs->pref = *prefs;
986 if (instance && instance->engine->packetization_set) {
987 instance->engine->packetization_set(instance, &instance->codecs.pref);
991 int ast_rtp_instance_dtmf_begin(struct ast_rtp_instance *instance, char digit)
993 return instance->engine->dtmf_begin ? instance->engine->dtmf_begin(instance, digit) : -1;
996 int ast_rtp_instance_dtmf_end(struct ast_rtp_instance *instance, char digit)
998 return instance->engine->dtmf_end ? instance->engine->dtmf_end(instance, digit) : -1;
1000 int ast_rtp_instance_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration)
1002 return instance->engine->dtmf_end_with_duration ? instance->engine->dtmf_end_with_duration(instance, digit, duration) : -1;
1005 int ast_rtp_instance_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode)
1007 return (!instance->engine->dtmf_mode_set || instance->engine->dtmf_mode_set(instance, dtmf_mode)) ? -1 : 0;
1010 enum ast_rtp_dtmf_mode ast_rtp_instance_dtmf_mode_get(struct ast_rtp_instance *instance)
1012 return instance->engine->dtmf_mode_get ? instance->engine->dtmf_mode_get(instance) : 0;
1015 void ast_rtp_instance_update_source(struct ast_rtp_instance *instance)
1017 if (instance->engine->update_source) {
1018 instance->engine->update_source(instance);
1022 void ast_rtp_instance_change_source(struct ast_rtp_instance *instance)
1024 if (instance->engine->change_source) {
1025 instance->engine->change_source(instance);
1029 int ast_rtp_instance_set_qos(struct ast_rtp_instance *instance, int tos, int cos, const char *desc)
1031 return instance->engine->qos ? instance->engine->qos(instance, tos, cos, desc) : -1;
1034 void ast_rtp_instance_stop(struct ast_rtp_instance *instance)
1036 if (instance->engine->stop) {
1037 instance->engine->stop(instance);
1041 int ast_rtp_instance_fd(struct ast_rtp_instance *instance, int rtcp)
1043 return instance->engine->fd ? instance->engine->fd(instance, rtcp) : -1;
1046 struct ast_rtp_glue *ast_rtp_instance_get_glue(const char *type)
1048 struct ast_rtp_glue *glue = NULL;
1050 AST_RWLIST_RDLOCK(&glues);
1052 AST_RWLIST_TRAVERSE(&glues, glue, entry) {
1053 if (!strcasecmp(glue->type, type)) {
1058 AST_RWLIST_UNLOCK(&glues);
1064 * \brief Conditionally unref an rtp instance
1066 static void unref_instance_cond(struct ast_rtp_instance **instance)
1069 ao2_ref(*instance, -1);
1074 struct ast_rtp_instance *ast_rtp_instance_get_bridged(struct ast_rtp_instance *instance)
1076 return instance->bridged;
1079 void ast_rtp_instance_set_bridged(struct ast_rtp_instance *instance, struct ast_rtp_instance *bridged)
1081 instance->bridged = bridged;
1084 void ast_rtp_instance_early_bridge_make_compatible(struct ast_channel *c_dst, struct ast_channel *c_src)
1086 struct ast_rtp_instance *instance_dst = NULL, *instance_src = NULL,
1087 *vinstance_dst = NULL, *vinstance_src = NULL,
1088 *tinstance_dst = NULL, *tinstance_src = NULL;
1089 struct ast_rtp_glue *glue_dst, *glue_src;
1090 enum ast_rtp_glue_result audio_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID, video_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID;
1091 enum ast_rtp_glue_result audio_glue_src_res = AST_RTP_GLUE_RESULT_FORBID, video_glue_src_res = AST_RTP_GLUE_RESULT_FORBID;
1092 struct ast_format_cap *cap_dst = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
1093 struct ast_format_cap *cap_src = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
1095 /* Lock both channels so we can look for the glue that binds them together */
1096 ast_channel_lock_both(c_dst, c_src);
1098 if (!cap_src || !cap_dst) {
1102 /* Grab glue that binds each channel to something using the RTP engine */
1103 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))) {
1104 ast_debug(1, "Can't find native functions for channel '%s'\n", glue_dst ? ast_channel_name(c_src) : ast_channel_name(c_dst));
1108 audio_glue_dst_res = glue_dst->get_rtp_info(c_dst, &instance_dst);
1109 video_glue_dst_res = glue_dst->get_vrtp_info ? glue_dst->get_vrtp_info(c_dst, &vinstance_dst) : AST_RTP_GLUE_RESULT_FORBID;
1111 audio_glue_src_res = glue_src->get_rtp_info(c_src, &instance_src);
1112 video_glue_src_res = glue_src->get_vrtp_info ? glue_src->get_vrtp_info(c_src, &vinstance_src) : AST_RTP_GLUE_RESULT_FORBID;
1114 /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
1115 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)) {
1116 audio_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID;
1118 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)) {
1119 audio_glue_src_res = AST_RTP_GLUE_RESULT_FORBID;
1121 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) {
1122 glue_dst->get_codec(c_dst, cap_dst);
1124 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) {
1125 glue_src->get_codec(c_src, cap_src);
1128 /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
1129 if (audio_glue_dst_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue_src_res != AST_RTP_GLUE_RESULT_REMOTE) {
1133 /* Make sure we have matching codecs */
1134 if (!ast_format_cap_has_joint(cap_dst, cap_src)) {
1138 ast_rtp_codecs_payloads_copy(&instance_src->codecs, &instance_dst->codecs, instance_dst);
1140 if (vinstance_dst && vinstance_src) {
1141 ast_rtp_codecs_payloads_copy(&vinstance_src->codecs, &vinstance_dst->codecs, vinstance_dst);
1143 if (tinstance_dst && tinstance_src) {
1144 ast_rtp_codecs_payloads_copy(&tinstance_src->codecs, &tinstance_dst->codecs, tinstance_dst);
1147 if (glue_dst->update_peer(c_dst, instance_src, vinstance_src, tinstance_src, cap_src, 0)) {
1148 ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n",
1149 ast_channel_name(c_dst), ast_channel_name(c_src));
1151 ast_debug(1, "Seeded SDP of '%s' with that of '%s'\n",
1152 ast_channel_name(c_dst), ast_channel_name(c_src));
1156 ast_channel_unlock(c_dst);
1157 ast_channel_unlock(c_src);
1159 ast_format_cap_destroy(cap_dst);
1160 ast_format_cap_destroy(cap_src);
1162 unref_instance_cond(&instance_dst);
1163 unref_instance_cond(&instance_src);
1164 unref_instance_cond(&vinstance_dst);
1165 unref_instance_cond(&vinstance_src);
1166 unref_instance_cond(&tinstance_dst);
1167 unref_instance_cond(&tinstance_src);
1170 int ast_rtp_instance_early_bridge(struct ast_channel *c0, struct ast_channel *c1)
1172 struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
1173 *vinstance0 = NULL, *vinstance1 = NULL,
1174 *tinstance0 = NULL, *tinstance1 = NULL;
1175 struct ast_rtp_glue *glue0, *glue1;
1176 enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1177 enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1178 struct ast_format_cap *cap0 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
1179 struct ast_format_cap *cap1 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK);
1181 /* If there is no second channel just immediately bail out, we are of no use in that scenario */
1182 if (!c1 || !cap1 || !cap0) {
1183 ast_format_cap_destroy(cap0);
1184 ast_format_cap_destroy(cap1);
1188 /* Lock both channels so we can look for the glue that binds them together */
1189 ast_channel_lock_both(c0, c1);
1191 /* Grab glue that binds each channel to something using the RTP engine */
1192 if (!(glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) || !(glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type))) {
1193 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", glue0 ? ast_channel_name(c1) : ast_channel_name(c0));
1197 audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
1198 video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1200 audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
1201 video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1203 /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
1204 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)) {
1205 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1207 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)) {
1208 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1210 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) {
1211 glue0->get_codec(c0, cap0);
1213 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) {
1214 glue1->get_codec(c1, cap1);
1217 /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
1218 if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
1222 /* Make sure we have matching codecs */
1223 if (!ast_format_cap_has_joint(cap0, cap1)) {
1227 /* Bridge media early */
1228 if (glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0)) {
1229 ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
1233 ast_channel_unlock(c0);
1234 ast_channel_unlock(c1);
1236 ast_format_cap_destroy(cap0);
1237 ast_format_cap_destroy(cap1);
1239 unref_instance_cond(&instance0);
1240 unref_instance_cond(&instance1);
1241 unref_instance_cond(&vinstance0);
1242 unref_instance_cond(&vinstance1);
1243 unref_instance_cond(&tinstance0);
1244 unref_instance_cond(&tinstance1);
1246 ast_debug(1, "Setting early bridge SDP of '%s' with that of '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
1251 int ast_rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations)
1253 return instance->engine->red_init ? instance->engine->red_init(instance, buffer_time, payloads, generations) : -1;
1256 int ast_rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame)
1258 return instance->engine->red_buffer ? instance->engine->red_buffer(instance, frame) : -1;
1261 int ast_rtp_instance_get_stats(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat)
1263 return instance->engine->get_stat ? instance->engine->get_stat(instance, stats, stat) : -1;
1266 char *ast_rtp_instance_get_quality(struct ast_rtp_instance *instance, enum ast_rtp_instance_stat_field field, char *buf, size_t size)
1268 struct ast_rtp_instance_stats stats = { 0, };
1269 enum ast_rtp_instance_stat stat;
1271 /* Determine what statistics we will need to retrieve based on field passed in */
1272 if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
1273 stat = AST_RTP_INSTANCE_STAT_ALL;
1274 } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
1275 stat = AST_RTP_INSTANCE_STAT_COMBINED_JITTER;
1276 } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
1277 stat = AST_RTP_INSTANCE_STAT_COMBINED_LOSS;
1278 } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
1279 stat = AST_RTP_INSTANCE_STAT_COMBINED_RTT;
1284 /* Attempt to actually retrieve the statistics we need to generate the quality string */
1285 if (ast_rtp_instance_get_stats(instance, &stats, stat)) {
1289 /* Now actually fill the buffer with the good information */
1290 if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
1291 snprintf(buf, size, "ssrc=%i;themssrc=%u;lp=%u;rxjitter=%f;rxcount=%u;txjitter=%f;txcount=%u;rlp=%u;rtt=%f",
1292 stats.local_ssrc, stats.remote_ssrc, stats.rxploss, stats.rxjitter, stats.rxcount, stats.txjitter, stats.txcount, stats.txploss, stats.rtt);
1293 } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
1294 snprintf(buf, size, "minrxjitter=%f;maxrxjitter=%f;avgrxjitter=%f;stdevrxjitter=%f;reported_minjitter=%f;reported_maxjitter=%f;reported_avgjitter=%f;reported_stdevjitter=%f;",
1295 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));
1296 } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
1297 snprintf(buf, size, "minrxlost=%f;maxrxlost=%f;avgrxlost=%f;stdevrxlost=%f;reported_minlost=%f;reported_maxlost=%f;reported_avglost=%f;reported_stdevlost=%f;",
1298 stats.local_minrxploss, stats.local_maxrxploss, stats.local_normdevrxploss, sqrt(stats.local_stdevrxploss), stats.remote_minrxploss, stats.remote_maxrxploss, stats.remote_normdevrxploss, sqrt(stats.remote_stdevrxploss));
1299 } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
1300 snprintf(buf, size, "minrtt=%f;maxrtt=%f;avgrtt=%f;stdevrtt=%f;", stats.minrtt, stats.maxrtt, stats.normdevrtt, stats.stdevrtt);
1306 void ast_rtp_instance_set_stats_vars(struct ast_channel *chan, struct ast_rtp_instance *instance)
1308 char quality_buf[AST_MAX_USER_FIELD], *quality;
1309 RAII_VAR(struct ast_channel *, bridge, ast_channel_bridge_peer(chan), ast_channel_cleanup);
1311 if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
1312 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOS", quality);
1314 pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSBRIDGED", quality);
1318 if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER, quality_buf, sizeof(quality_buf)))) {
1319 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSJITTER", quality);
1321 pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSJITTERBRIDGED", quality);
1325 if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS, quality_buf, sizeof(quality_buf)))) {
1326 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSLOSS", quality);
1328 pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSLOSSBRIDGED", quality);
1332 if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT, quality_buf, sizeof(quality_buf)))) {
1333 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSRTT", quality);
1335 pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSRTTBRIDGED", quality);
1340 int ast_rtp_instance_set_read_format(struct ast_rtp_instance *instance, struct ast_format *format)
1342 return instance->engine->set_read_format ? instance->engine->set_read_format(instance, format) : -1;
1345 int ast_rtp_instance_set_write_format(struct ast_rtp_instance *instance, struct ast_format *format)
1347 return instance->engine->set_write_format ? instance->engine->set_write_format(instance, format) : -1;
1350 int ast_rtp_instance_make_compatible(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_channel *peer)
1352 struct ast_rtp_glue *glue;
1353 struct ast_rtp_instance *peer_instance = NULL;
1356 if (!instance->engine->make_compatible) {
1360 ast_channel_lock(peer);
1362 if (!(glue = ast_rtp_instance_get_glue(ast_channel_tech(peer)->type))) {
1363 ast_channel_unlock(peer);
1367 glue->get_rtp_info(peer, &peer_instance);
1368 if (!peer_instance) {
1369 ast_log(LOG_ERROR, "Unable to get_rtp_info for peer type %s\n", glue->type);
1370 ast_channel_unlock(peer);
1373 if (peer_instance->engine != instance->engine) {
1374 ast_log(LOG_ERROR, "Peer engine mismatch for type %s\n", glue->type);
1375 ast_channel_unlock(peer);
1376 ao2_ref(peer_instance, -1);
1380 res = instance->engine->make_compatible(chan, instance, peer, peer_instance);
1382 ast_channel_unlock(peer);
1384 ao2_ref(peer_instance, -1);
1385 peer_instance = NULL;
1390 void ast_rtp_instance_available_formats(struct ast_rtp_instance *instance, struct ast_format_cap *to_endpoint, struct ast_format_cap *to_asterisk, struct ast_format_cap *result)
1392 if (instance->engine->available_formats) {
1393 instance->engine->available_formats(instance, to_endpoint, to_asterisk, result);
1394 if (!ast_format_cap_is_empty(result)) {
1399 ast_translate_available_formats(to_endpoint, to_asterisk, result);
1402 int ast_rtp_instance_activate(struct ast_rtp_instance *instance)
1404 return instance->engine->activate ? instance->engine->activate(instance) : 0;
1407 void ast_rtp_instance_stun_request(struct ast_rtp_instance *instance,
1408 struct ast_sockaddr *suggestion,
1409 const char *username)
1411 if (instance->engine->stun_request) {
1412 instance->engine->stun_request(instance, suggestion, username);
1416 void ast_rtp_instance_set_timeout(struct ast_rtp_instance *instance, int timeout)
1418 instance->timeout = timeout;
1421 void ast_rtp_instance_set_hold_timeout(struct ast_rtp_instance *instance, int timeout)
1423 instance->holdtimeout = timeout;
1426 void ast_rtp_instance_set_keepalive(struct ast_rtp_instance *instance, int interval)
1428 instance->keepalive = interval;
1431 int ast_rtp_instance_get_timeout(struct ast_rtp_instance *instance)
1433 return instance->timeout;
1436 int ast_rtp_instance_get_hold_timeout(struct ast_rtp_instance *instance)
1438 return instance->holdtimeout;
1441 int ast_rtp_instance_get_keepalive(struct ast_rtp_instance *instance)
1443 return instance->keepalive;
1446 struct ast_rtp_engine *ast_rtp_instance_get_engine(struct ast_rtp_instance *instance)
1448 return instance->engine;
1451 struct ast_rtp_glue *ast_rtp_instance_get_active_glue(struct ast_rtp_instance *instance)
1453 return instance->glue;
1456 int ast_rtp_engine_register_srtp(struct ast_srtp_res *srtp_res, struct ast_srtp_policy_res *policy_res)
1458 if (res_srtp || res_srtp_policy) {
1461 if (!srtp_res || !policy_res) {
1465 res_srtp = srtp_res;
1466 res_srtp_policy = policy_res;
1471 void ast_rtp_engine_unregister_srtp(void)
1474 res_srtp_policy = NULL;
1477 int ast_rtp_engine_srtp_is_registered(void)
1479 return res_srtp && res_srtp_policy;
1482 int ast_rtp_instance_add_srtp_policy(struct ast_rtp_instance *instance, struct ast_srtp_policy *remote_policy, struct ast_srtp_policy *local_policy)
1490 if (!instance->srtp) {
1491 res = res_srtp->create(&instance->srtp, instance, remote_policy);
1493 res = res_srtp->replace(&instance->srtp, instance, remote_policy);
1496 res = res_srtp->add_stream(instance->srtp, local_policy);
1502 struct ast_srtp *ast_rtp_instance_get_srtp(struct ast_rtp_instance *instance)
1504 return instance->srtp;
1507 int ast_rtp_instance_sendcng(struct ast_rtp_instance *instance, int level)
1509 if (instance->engine->sendcng) {
1510 return instance->engine->sendcng(instance, level);
1516 struct ast_rtp_engine_ice *ast_rtp_instance_get_ice(struct ast_rtp_instance *instance)
1518 return instance->engine->ice;
1521 struct ast_rtp_engine_dtls *ast_rtp_instance_get_dtls(struct ast_rtp_instance *instance)
1523 return instance->engine->dtls;
1526 int ast_rtp_dtls_cfg_parse(struct ast_rtp_dtls_cfg *dtls_cfg, const char *name, const char *value)
1528 if (!strcasecmp(name, "dtlsenable")) {
1529 dtls_cfg->enabled = ast_true(value) ? 1 : 0;
1530 } else if (!strcasecmp(name, "dtlsverify")) {
1531 dtls_cfg->verify = ast_true(value) ? 1 : 0;
1532 } else if (!strcasecmp(name, "dtlsrekey")) {
1533 if (sscanf(value, "%30u", &dtls_cfg->rekey) != 1) {
1536 } else if (!strcasecmp(name, "dtlscertfile")) {
1537 ast_free(dtls_cfg->certfile);
1538 dtls_cfg->certfile = ast_strdup(value);
1539 } else if (!strcasecmp(name, "dtlsprivatekey")) {
1540 ast_free(dtls_cfg->pvtfile);
1541 dtls_cfg->pvtfile = ast_strdup(value);
1542 } else if (!strcasecmp(name, "dtlscipher")) {
1543 ast_free(dtls_cfg->cipher);
1544 dtls_cfg->cipher = ast_strdup(value);
1545 } else if (!strcasecmp(name, "dtlscafile")) {
1546 ast_free(dtls_cfg->cafile);
1547 dtls_cfg->cafile = ast_strdup(value);
1548 } else if (!strcasecmp(name, "dtlscapath") || !strcasecmp(name, "dtlscadir")) {
1549 ast_free(dtls_cfg->capath);
1550 dtls_cfg->capath = ast_strdup(value);
1551 } else if (!strcasecmp(name, "dtlssetup")) {
1552 if (!strcasecmp(value, "active")) {
1553 dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_ACTIVE;
1554 } else if (!strcasecmp(value, "passive")) {
1555 dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_PASSIVE;
1556 } else if (!strcasecmp(value, "actpass")) {
1557 dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_ACTPASS;
1566 void ast_rtp_dtls_cfg_copy(const struct ast_rtp_dtls_cfg *src_cfg, struct ast_rtp_dtls_cfg *dst_cfg)
1568 dst_cfg->enabled = src_cfg->enabled;
1569 dst_cfg->verify = src_cfg->verify;
1570 dst_cfg->rekey = src_cfg->rekey;
1571 dst_cfg->suite = src_cfg->suite;
1572 dst_cfg->certfile = ast_strdup(src_cfg->certfile);
1573 dst_cfg->pvtfile = ast_strdup(src_cfg->pvtfile);
1574 dst_cfg->cipher = ast_strdup(src_cfg->cipher);
1575 dst_cfg->cafile = ast_strdup(src_cfg->cafile);
1576 dst_cfg->capath = ast_strdup(src_cfg->capath);
1577 dst_cfg->default_setup = src_cfg->default_setup;
1580 void ast_rtp_dtls_cfg_free(struct ast_rtp_dtls_cfg *dtls_cfg)
1582 ast_free(dtls_cfg->certfile);
1583 ast_free(dtls_cfg->pvtfile);
1584 ast_free(dtls_cfg->cipher);
1585 ast_free(dtls_cfg->cafile);
1586 ast_free(dtls_cfg->capath);
1589 static void set_next_mime_type(const struct ast_format *format, int rtp_code, char *type, char *subtype, unsigned int sample_rate)
1591 int x = mime_types_len;
1592 if (ARRAY_LEN(ast_rtp_mime_types) == mime_types_len) {
1596 ast_rwlock_wrlock(&mime_types_lock);
1598 ast_rtp_mime_types[x].payload_type.asterisk_format = 1;
1599 ast_format_copy(&ast_rtp_mime_types[x].payload_type.format, format);
1601 ast_rtp_mime_types[x].payload_type.rtp_code = rtp_code;
1603 ast_rtp_mime_types[x].type = type;
1604 ast_rtp_mime_types[x].subtype = subtype;
1605 ast_rtp_mime_types[x].sample_rate = sample_rate;
1607 ast_rwlock_unlock(&mime_types_lock);
1610 static void add_static_payload(int map, const struct ast_format *format, int rtp_code)
1613 ast_rwlock_wrlock(&static_RTP_PT_lock);
1615 /* find next available dynamic payload slot */
1616 for (x = 96; x < 127; x++) {
1617 if (!static_RTP_PT[x].asterisk_format && !static_RTP_PT[x].rtp_code) {
1625 ast_log(LOG_WARNING, "No Dynamic RTP mapping available for format %s\n" ,ast_getformatname(format));
1626 ast_rwlock_unlock(&static_RTP_PT_lock);
1631 static_RTP_PT[map].asterisk_format = 1;
1632 ast_format_copy(&static_RTP_PT[map].format, format);
1634 static_RTP_PT[map].rtp_code = rtp_code;
1636 ast_rwlock_unlock(&static_RTP_PT_lock);
1639 int ast_rtp_engine_load_format(const struct ast_format *format)
1641 switch (format->id) {
1642 case AST_FORMAT_SILK:
1643 set_next_mime_type(format, 0, "audio", "SILK", ast_format_rate(format));
1644 add_static_payload(-1, format, 0);
1646 case AST_FORMAT_CELT:
1647 set_next_mime_type(format, 0, "audio", "CELT", ast_format_rate(format));
1648 add_static_payload(-1, format, 0);
1657 int ast_rtp_engine_unload_format(const struct ast_format *format)
1662 ast_rwlock_wrlock(&static_RTP_PT_lock);
1663 /* remove everything pertaining to this format id from the lists */
1664 for (x = 0; x < AST_RTP_MAX_PT; x++) {
1665 if (ast_format_cmp(&static_RTP_PT[x].format, format) == AST_FORMAT_CMP_EQUAL) {
1666 memset(&static_RTP_PT[x], 0, sizeof(struct ast_rtp_payload_type));
1669 ast_rwlock_unlock(&static_RTP_PT_lock);
1672 ast_rwlock_wrlock(&mime_types_lock);
1673 /* rebuild the list skipping the items matching this id */
1674 for (x = 0; x < mime_types_len; x++) {
1675 if (ast_format_cmp(&ast_rtp_mime_types[x].payload_type.format, format) == AST_FORMAT_CMP_EQUAL) {
1678 ast_rtp_mime_types[y] = ast_rtp_mime_types[x];
1682 ast_rwlock_unlock(&mime_types_lock);
1688 * \brief \ref stasis message payload for RTCP messages
1690 struct rtcp_message_payload {
1691 struct ast_channel_snapshot *snapshot; /*< The channel snapshot, if available */
1692 struct ast_rtp_rtcp_report *report; /*< The RTCP report */
1693 struct ast_json *blob; /*< Extra JSON data to publish */
1696 static void rtcp_message_payload_dtor(void *obj)
1698 struct rtcp_message_payload *payload = obj;
1700 ao2_cleanup(payload->report);
1701 ao2_cleanup(payload->snapshot);
1702 ast_json_unref(payload->blob);
1705 static struct ast_manager_event_blob *rtcp_report_to_ami(struct stasis_message *msg)
1707 struct rtcp_message_payload *payload = stasis_message_data(msg);
1708 RAII_VAR(struct ast_str *, channel_string, NULL, ast_free);
1709 RAII_VAR(struct ast_str *, packet_string, ast_str_create(512), ast_free);
1710 unsigned int ssrc = payload->report->ssrc;
1711 unsigned int type = payload->report->type;
1712 unsigned int report_count = payload->report->reception_report_count;
1715 if (!packet_string) {
1719 if (payload->snapshot) {
1720 channel_string = ast_manager_build_channel_state_string(payload->snapshot);
1721 if (!channel_string) {
1726 if (payload->blob) {
1728 struct ast_json *to = ast_json_object_get(payload->blob, "to");
1729 struct ast_json *from = ast_json_object_get(payload->blob, "from");
1730 struct ast_json *rtt = ast_json_object_get(payload->blob, "rtt");
1732 ast_str_append(&packet_string, 0, "To: %s\r\n", ast_json_string_get(to));
1735 ast_str_append(&packet_string, 0, "From: %s\r\n", ast_json_string_get(from));
1738 ast_str_append(&packet_string, 0, "RTT: %4.4f\r\n", ast_json_real_get(rtt));
1742 ast_str_append(&packet_string, 0, "SSRC: 0x%.8x\r\n", ssrc);
1743 ast_str_append(&packet_string, 0, "PT: %u(%s)\r\n", type, type== AST_RTP_RTCP_SR ? "SR" : "RR");
1744 ast_str_append(&packet_string, 0, "ReportCount: %u\r\n", report_count);
1745 if (type == AST_RTP_RTCP_SR) {
1746 ast_str_append(&packet_string, 0, "SentNTP: %lu.%06lu\r\n",
1747 (unsigned long)payload->report->sender_information.ntp_timestamp.tv_sec,
1748 (unsigned long)payload->report->sender_information.ntp_timestamp.tv_usec * 4096);
1749 ast_str_append(&packet_string, 0, "SentRTP: %u\r\n",
1750 payload->report->sender_information.rtp_timestamp);
1751 ast_str_append(&packet_string, 0, "SentPackets: %u\r\n",
1752 payload->report->sender_information.packet_count);
1753 ast_str_append(&packet_string, 0, "SentOctets: %u\r\n",
1754 payload->report->sender_information.octet_count);
1757 for (i = 0; i < report_count; i++) {
1758 RAII_VAR(struct ast_str *, report_string, NULL, ast_free);
1760 if (!payload->report->report_block[i]) {
1764 report_string = ast_str_create(256);
1765 if (!report_string) {
1769 ast_str_append(&report_string, 0, "Report%dSourceSSRC: 0x%.8x\r\n",
1770 i, payload->report->report_block[i]->source_ssrc);
1771 ast_str_append(&report_string, 0, "Report%dFractionLost: %u\r\n",
1772 i, payload->report->report_block[i]->lost_count.fraction);
1773 ast_str_append(&report_string, 0, "Report%dCumulativeLost: %u\r\n",
1774 i, payload->report->report_block[i]->lost_count.packets);
1775 ast_str_append(&report_string, 0, "Report%dHighestSequence: %u\r\n",
1776 i, payload->report->report_block[i]->highest_seq_no & 0xffff);
1777 ast_str_append(&report_string, 0, "Report%dSequenceNumberCycles: %u\r\n",
1778 i, payload->report->report_block[i]->highest_seq_no >> 16);
1779 ast_str_append(&report_string, 0, "Report%dIAJitter: %u\r\n",
1780 i, payload->report->report_block[i]->ia_jitter);
1781 ast_str_append(&report_string, 0, "Report%dLSR: %u\r\n",
1782 i, payload->report->report_block[i]->lsr);
1783 ast_str_append(&report_string, 0, "Report%dDLSR: %4.4f\r\n",
1784 i, ((double)payload->report->report_block[i]->dlsr) / 65536);
1785 ast_str_append(&packet_string, 0, "%s", ast_str_buffer(report_string));
1788 return ast_manager_event_blob_create(EVENT_FLAG_REPORTING,
1789 stasis_message_type(msg) == ast_rtp_rtcp_received_type() ? "RTCPReceived" : "RTCPSent",
1791 AS_OR(channel_string, ""),
1792 ast_str_buffer(packet_string));
1795 static struct ast_json *rtcp_report_to_json(struct stasis_message *msg,
1796 const struct stasis_message_sanitizer *sanitize)
1798 struct rtcp_message_payload *payload = stasis_message_data(msg);
1799 RAII_VAR(struct ast_json *, json_rtcp_report, NULL, ast_json_unref);
1800 RAII_VAR(struct ast_json *, json_rtcp_report_blocks, NULL, ast_json_unref);
1801 RAII_VAR(struct ast_json *, json_rtcp_sender_info, NULL, ast_json_unref);
1802 RAII_VAR(struct ast_json *, json_channel, NULL, ast_json_unref);
1805 json_rtcp_report_blocks = ast_json_array_create();
1806 if (!json_rtcp_report_blocks) {
1810 for (i = 0; i < payload->report->reception_report_count; i++) {
1811 struct ast_json *json_report_block;
1812 json_report_block = ast_json_pack("{s: i, s: i, s: i, s: i, s: i, s: i, s: i}",
1813 "source_ssrc", payload->report->report_block[i]->source_ssrc,
1814 "fraction_lost", payload->report->report_block[i]->lost_count.fraction,
1815 "packets_lost", payload->report->report_block[i]->lost_count.packets,
1816 "highest_seq_no", payload->report->report_block[i]->highest_seq_no,
1817 "ia_jitter", payload->report->report_block[i]->ia_jitter,
1818 "lsr", payload->report->report_block[i]->lsr,
1819 "dlsr", payload->report->report_block[i]->dlsr);
1820 if (!json_report_block) {
1824 if (ast_json_array_append(json_rtcp_report_blocks, json_report_block)) {
1829 if (payload->report->type == AST_RTP_RTCP_SR) {
1830 json_rtcp_sender_info = ast_json_pack("{s: i, s: i, s: i, s: i, s: i}",
1831 "ntp_timestamp_sec", payload->report->sender_information.ntp_timestamp.tv_sec,
1832 "ntp_timestamp_usec", payload->report->sender_information.ntp_timestamp.tv_usec,
1833 "rtp_timestamp", payload->report->sender_information.rtp_timestamp,
1834 "packets", payload->report->sender_information.packet_count,
1835 "octets", payload->report->sender_information.octet_count);
1836 if (!json_rtcp_sender_info) {
1841 json_rtcp_report = ast_json_pack("{s: i, s: i, s: i, s: O, s: O}",
1842 "ssrc", payload->report->ssrc,
1843 "type", payload->report->type,
1844 "report_count", payload->report->reception_report_count,
1845 "sender_information", json_rtcp_sender_info ? json_rtcp_sender_info : ast_json_null(),
1846 "report_blocks", json_rtcp_report_blocks);
1847 if (!json_rtcp_report) {
1851 if (payload->snapshot) {
1852 json_channel = ast_channel_snapshot_to_json(payload->snapshot, sanitize);
1853 if (!json_channel) {
1858 return ast_json_pack("{s: O, s: O, s: O}",
1859 "channel", payload->snapshot ? json_channel : ast_json_null(),
1860 "rtcp_report", json_rtcp_report,
1861 "blob", payload->blob);
1864 static void rtp_rtcp_report_dtor(void *obj)
1867 struct ast_rtp_rtcp_report *rtcp_report = obj;
1869 for (i = 0; i < rtcp_report->reception_report_count; i++) {
1870 ast_free(rtcp_report->report_block[i]);
1874 struct ast_rtp_rtcp_report *ast_rtp_rtcp_report_alloc(unsigned int report_blocks)
1876 struct ast_rtp_rtcp_report *rtcp_report;
1878 /* Size of object is sizeof the report + the number of report_blocks * sizeof pointer */
1879 rtcp_report = ao2_alloc((sizeof(*rtcp_report) + report_blocks * sizeof(struct ast_rtp_rtcp_report_block *)),
1880 rtp_rtcp_report_dtor);
1885 void ast_rtp_publish_rtcp_message(struct ast_rtp_instance *rtp,
1886 struct stasis_message_type *message_type,
1887 struct ast_rtp_rtcp_report *report,
1888 struct ast_json *blob)
1890 RAII_VAR(struct rtcp_message_payload *, payload,
1891 ao2_alloc(sizeof(*payload), rtcp_message_payload_dtor), ao2_cleanup);
1892 RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
1893 RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
1895 if (!payload || !report) {
1899 if (!ast_strlen_zero(rtp->channel_uniqueid)) {
1900 snapshot = ast_channel_snapshot_get_latest(rtp->channel_uniqueid);
1902 ao2_ref(snapshot, +1);
1910 payload->snapshot = snapshot;
1911 payload->blob = blob;
1912 payload->report = report;
1914 message = stasis_message_create(message_type, payload);
1919 stasis_publish(ast_rtp_topic(), message);
1923 * @{ \brief Define RTCP/RTP message types.
1925 STASIS_MESSAGE_TYPE_DEFN(ast_rtp_rtcp_sent_type,
1926 .to_ami = rtcp_report_to_ami,
1927 .to_json = rtcp_report_to_json,);
1928 STASIS_MESSAGE_TYPE_DEFN(ast_rtp_rtcp_received_type,
1929 .to_ami = rtcp_report_to_ami,
1930 .to_json = rtcp_report_to_json,);
1933 struct stasis_topic *ast_rtp_topic(void)
1938 static void rtp_engine_shutdown(void)
1940 ao2_cleanup(rtp_topic);
1942 STASIS_MESSAGE_TYPE_CLEANUP(ast_rtp_rtcp_received_type);
1943 STASIS_MESSAGE_TYPE_CLEANUP(ast_rtp_rtcp_sent_type);
1946 int ast_rtp_engine_init()
1948 struct ast_format tmpfmt;
1950 ast_rwlock_init(&mime_types_lock);
1951 ast_rwlock_init(&static_RTP_PT_lock);
1953 rtp_topic = stasis_topic_create("rtp_topic");
1957 STASIS_MESSAGE_TYPE_INIT(ast_rtp_rtcp_sent_type);
1958 STASIS_MESSAGE_TYPE_INIT(ast_rtp_rtcp_received_type);
1959 ast_register_atexit(rtp_engine_shutdown);
1961 /* Define all the RTP mime types available */
1962 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G723_1, 0), 0, "audio", "G723", 8000);
1963 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_GSM, 0), 0, "audio", "GSM", 8000);
1964 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0, "audio", "PCMU", 8000);
1965 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0, "audio", "G711U", 8000);
1966 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), 0, "audio", "PCMA", 8000);
1967 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), 0, "audio", "G711A", 8000);
1968 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G726, 0), 0, "audio", "G726-32", 8000);
1969 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0, "audio", "DVI4", 8000);
1970 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), 0, "audio", "L16", 8000);
1971 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0, "audio", "L16", 16000);
1972 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0, "audio", "L16-256", 16000);
1973 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_LPC10, 0), 0, "audio", "LPC", 8000);
1974 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0, "audio", "G729", 8000);
1975 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0, "audio", "G729A", 8000);
1976 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0, "audio", "G.729", 8000);
1977 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX, 0), 0, "audio", "speex", 8000);
1978 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX16, 0), 0, "audio", "speex", 16000);
1979 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX32, 0), 0, "audio", "speex", 32000);
1980 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ILBC, 0), 0, "audio", "iLBC", 8000);
1981 /* this is the sample rate listed in the RTP profile for the G.722 codec, *NOT* the actual sample rate of the media stream */
1982 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G722, 0), 0, "audio", "G722", 8000);
1983 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G726_AAL2, 0), 0, "audio", "AAL2-G726-32", 8000);
1984 set_next_mime_type(NULL, AST_RTP_DTMF, "audio", "telephone-event", 8000);
1985 set_next_mime_type(NULL, AST_RTP_CISCO_DTMF, "audio", "cisco-telephone-event", 8000);
1986 set_next_mime_type(NULL, AST_RTP_CN, "audio", "CN", 8000);
1987 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_JPEG, 0), 0, "video", "JPEG", 90000);
1988 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_PNG, 0), 0, "video", "PNG", 90000);
1989 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H261, 0), 0, "video", "H261", 90000);
1990 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H263, 0), 0, "video", "H263", 90000);
1991 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0, "video", "h263-1998", 90000);
1992 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H264, 0), 0, "video", "H264", 90000);
1993 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_MP4_VIDEO, 0), 0, "video", "MP4V-ES", 90000);
1994 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_T140RED, 0), 0, "text", "RED", 1000);
1995 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_T140, 0), 0, "text", "T140", 1000);
1996 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SIREN7, 0), 0, "audio", "G7221", 16000);
1997 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SIREN14, 0), 0, "audio", "G7221", 32000);
1998 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G719, 0), 0, "audio", "G719", 48000);
2000 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_OPUS, 0), 0, "audio", "opus", 48000);
2001 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_VP8, 0), 0, "video", "VP8", 90000);
2003 /* Define the static rtp payload mappings */
2004 add_static_payload(0, ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0);
2005 #ifdef USE_DEPRECATED_G726
2006 add_static_payload(2, ast_format_set(&tmpfmt, AST_FORMAT_G726, 0), 0);/* Technically this is G.721, but if Cisco can do it, so can we... */
2008 add_static_payload(3, ast_format_set(&tmpfmt, AST_FORMAT_GSM, 0), 0);
2009 add_static_payload(4, ast_format_set(&tmpfmt, AST_FORMAT_G723_1, 0), 0);
2010 add_static_payload(5, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0);/* 8 kHz */
2011 add_static_payload(6, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0); /* 16 kHz */
2012 add_static_payload(7, ast_format_set(&tmpfmt, AST_FORMAT_LPC10, 0), 0);
2013 add_static_payload(8, ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), 0);
2014 add_static_payload(9, ast_format_set(&tmpfmt, AST_FORMAT_G722, 0), 0);
2015 add_static_payload(10, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), 0); /* 2 channels */
2016 add_static_payload(11, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), 0); /* 1 channel */
2017 add_static_payload(13, NULL, AST_RTP_CN);
2018 add_static_payload(16, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0); /* 11.025 kHz */
2019 add_static_payload(17, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0); /* 22.050 kHz */
2020 add_static_payload(18, ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0);
2021 add_static_payload(19, NULL, AST_RTP_CN); /* Also used for CN */
2022 add_static_payload(26, ast_format_set(&tmpfmt, AST_FORMAT_JPEG, 0), 0);
2023 add_static_payload(31, ast_format_set(&tmpfmt, AST_FORMAT_H261, 0), 0);
2024 add_static_payload(34, ast_format_set(&tmpfmt, AST_FORMAT_H263, 0), 0);
2025 add_static_payload(97, ast_format_set(&tmpfmt, AST_FORMAT_ILBC, 0), 0);
2026 add_static_payload(98, ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0);
2027 add_static_payload(99, ast_format_set(&tmpfmt, AST_FORMAT_H264, 0), 0);
2028 add_static_payload(101, NULL, AST_RTP_DTMF);
2029 add_static_payload(102, ast_format_set(&tmpfmt, AST_FORMAT_SIREN7, 0), 0);
2030 add_static_payload(103, ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0);
2031 add_static_payload(104, ast_format_set(&tmpfmt, AST_FORMAT_MP4_VIDEO, 0), 0);
2032 add_static_payload(105, ast_format_set(&tmpfmt, AST_FORMAT_T140RED, 0), 0); /* Real time text chat (with redundancy encoding) */
2033 add_static_payload(106, ast_format_set(&tmpfmt, AST_FORMAT_T140, 0), 0); /* Real time text chat */
2034 add_static_payload(110, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX, 0), 0);
2035 add_static_payload(111, ast_format_set(&tmpfmt, AST_FORMAT_G726, 0), 0);
2036 add_static_payload(112, ast_format_set(&tmpfmt, AST_FORMAT_G726_AAL2, 0), 0);
2037 add_static_payload(115, ast_format_set(&tmpfmt, AST_FORMAT_SIREN14, 0), 0);
2038 add_static_payload(116, ast_format_set(&tmpfmt, AST_FORMAT_G719, 0), 0);
2039 add_static_payload(117, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX16, 0), 0);
2040 add_static_payload(118, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0); /* 16 Khz signed linear */
2041 add_static_payload(119, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX32, 0), 0);
2042 add_static_payload(121, NULL, AST_RTP_CISCO_DTMF); /* Must be type 121 */
2044 add_static_payload(100, ast_format_set(&tmpfmt, AST_FORMAT_VP8, 0), 0);
2045 add_static_payload(107, ast_format_set(&tmpfmt, AST_FORMAT_OPUS, 0), 0);