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>
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32 #include "asterisk/channel.h"
33 #include "asterisk/frame.h"
34 #include "asterisk/module.h"
35 #include "asterisk/rtp_engine.h"
36 #include "asterisk/manager.h"
37 #include "asterisk/options.h"
38 #include "asterisk/astobj2.h"
39 #include "asterisk/pbx.h"
40 #include "asterisk/translate.h"
41 #include "asterisk/netsock2.h"
42 #include "asterisk/_private.h"
43 #include "asterisk/framehook.h"
45 struct ast_srtp_res *res_srtp = NULL;
46 struct ast_srtp_policy_res *res_srtp_policy = NULL;
48 /*! Structure that represents an RTP session (instance) */
49 struct ast_rtp_instance {
50 /*! Engine that is handling this RTP instance */
51 struct ast_rtp_engine *engine;
52 /*! Data unique to the RTP engine */
54 /*! RTP properties that have been set and their value */
55 int properties[AST_RTP_PROPERTY_MAX];
56 /*! Address that we are expecting RTP to come in to */
57 struct ast_sockaddr local_address;
58 /*! Address that we are sending RTP to */
59 struct ast_sockaddr remote_address;
60 /*! Alternate address that we are receiving RTP from */
61 struct ast_sockaddr alt_remote_address;
62 /*! Instance that we are bridged to if doing remote or local bridging */
63 struct ast_rtp_instance *bridged;
64 /*! Payload and packetization information */
65 struct ast_rtp_codecs codecs;
66 /*! RTP timeout time (negative or zero means disabled, negative value means temporarily disabled) */
68 /*! RTP timeout when on hold (negative or zero means disabled, negative value means temporarily disabled). */
70 /*! RTP keepalive interval */
72 /*! Glue currently in use */
73 struct ast_rtp_glue *glue;
74 /*! Channel associated with the instance */
75 struct ast_channel *chan;
76 /*! SRTP info associated with the instance */
77 struct ast_srtp *srtp;
80 /*! List of RTP engines that are currently registered */
81 static AST_RWLIST_HEAD_STATIC(engines, ast_rtp_engine);
83 /*! List of RTP glues */
84 static AST_RWLIST_HEAD_STATIC(glues, ast_rtp_glue);
86 /*! The following array defines the MIME Media type (and subtype) for each
87 of our codecs, or RTP-specific data type. */
88 static struct ast_rtp_mime_type {
89 struct ast_rtp_payload_type payload_type;
92 unsigned int sample_rate;
93 } ast_rtp_mime_types[128]; /* This will Likely not need to grow any time soon. */
94 static ast_rwlock_t mime_types_lock;
95 static int mime_types_len = 0;
98 * \brief Mapping between Asterisk codecs and rtp payload types
100 * Static (i.e., well-known) RTP payload types for our "AST_FORMAT..."s:
101 * also, our own choices for dynamic payload types. This is our master
102 * table for transmission
104 * See http://www.iana.org/assignments/rtp-parameters for a list of
107 static struct ast_rtp_payload_type static_RTP_PT[AST_RTP_MAX_PT];
108 static ast_rwlock_t static_RTP_PT_lock;
110 int ast_rtp_engine_register2(struct ast_rtp_engine *engine, struct ast_module *module)
112 struct ast_rtp_engine *current_engine;
114 /* Perform a sanity check on the engine structure to make sure it has the basics */
115 if (ast_strlen_zero(engine->name) || !engine->new || !engine->destroy || !engine->write || !engine->read) {
116 ast_log(LOG_WARNING, "RTP Engine '%s' failed sanity check so it was not registered.\n", !ast_strlen_zero(engine->name) ? engine->name : "Unknown");
120 /* Link owner module to the RTP engine for reference counting purposes */
121 engine->mod = module;
123 AST_RWLIST_WRLOCK(&engines);
125 /* Ensure that no two modules with the same name are registered at the same time */
126 AST_RWLIST_TRAVERSE(&engines, current_engine, entry) {
127 if (!strcmp(current_engine->name, engine->name)) {
128 ast_log(LOG_WARNING, "An RTP engine with the name '%s' has already been registered.\n", engine->name);
129 AST_RWLIST_UNLOCK(&engines);
134 /* The engine survived our critique. Off to the list it goes to be used */
135 AST_RWLIST_INSERT_TAIL(&engines, engine, entry);
137 AST_RWLIST_UNLOCK(&engines);
139 ast_verb(2, "Registered RTP engine '%s'\n", engine->name);
144 int ast_rtp_engine_unregister(struct ast_rtp_engine *engine)
146 struct ast_rtp_engine *current_engine = NULL;
148 AST_RWLIST_WRLOCK(&engines);
150 if ((current_engine = AST_RWLIST_REMOVE(&engines, engine, entry))) {
151 ast_verb(2, "Unregistered RTP engine '%s'\n", engine->name);
154 AST_RWLIST_UNLOCK(&engines);
156 return current_engine ? 0 : -1;
159 int ast_rtp_glue_register2(struct ast_rtp_glue *glue, struct ast_module *module)
161 struct ast_rtp_glue *current_glue = NULL;
163 if (ast_strlen_zero(glue->type)) {
169 AST_RWLIST_WRLOCK(&glues);
171 AST_RWLIST_TRAVERSE(&glues, current_glue, entry) {
172 if (!strcasecmp(current_glue->type, glue->type)) {
173 ast_log(LOG_WARNING, "RTP glue with the name '%s' has already been registered.\n", glue->type);
174 AST_RWLIST_UNLOCK(&glues);
179 AST_RWLIST_INSERT_TAIL(&glues, glue, entry);
181 AST_RWLIST_UNLOCK(&glues);
183 ast_verb(2, "Registered RTP glue '%s'\n", glue->type);
188 int ast_rtp_glue_unregister(struct ast_rtp_glue *glue)
190 struct ast_rtp_glue *current_glue = NULL;
192 AST_RWLIST_WRLOCK(&glues);
194 if ((current_glue = AST_RWLIST_REMOVE(&glues, glue, entry))) {
195 ast_verb(2, "Unregistered RTP glue '%s'\n", glue->type);
198 AST_RWLIST_UNLOCK(&glues);
200 return current_glue ? 0 : -1;
203 static void instance_destructor(void *obj)
205 struct ast_rtp_instance *instance = obj;
207 /* Pass us off to the engine to destroy */
208 if (instance->data && instance->engine->destroy(instance)) {
209 ast_debug(1, "Engine '%s' failed to destroy RTP instance '%p'\n", instance->engine->name, instance);
213 if (instance->srtp) {
214 res_srtp->destroy(instance->srtp);
217 /* Drop our engine reference */
218 ast_module_unref(instance->engine->mod);
220 ast_debug(1, "Destroyed RTP instance '%p'\n", instance);
223 int ast_rtp_instance_destroy(struct ast_rtp_instance *instance)
225 ao2_ref(instance, -1);
230 struct ast_rtp_instance *ast_rtp_instance_new(const char *engine_name,
231 struct ast_sched_context *sched, const struct ast_sockaddr *sa,
234 struct ast_sockaddr address = {{0,}};
235 struct ast_rtp_instance *instance = NULL;
236 struct ast_rtp_engine *engine = NULL;
238 AST_RWLIST_RDLOCK(&engines);
240 /* If an engine name was specified try to use it or otherwise use the first one registered */
241 if (!ast_strlen_zero(engine_name)) {
242 AST_RWLIST_TRAVERSE(&engines, engine, entry) {
243 if (!strcmp(engine->name, engine_name)) {
248 engine = AST_RWLIST_FIRST(&engines);
251 /* If no engine was actually found bail out now */
253 ast_log(LOG_ERROR, "No RTP engine was found. Do you have one loaded?\n");
254 AST_RWLIST_UNLOCK(&engines);
258 /* Bump up the reference count before we return so the module can not be unloaded */
259 ast_module_ref(engine->mod);
261 AST_RWLIST_UNLOCK(&engines);
263 /* Allocate a new RTP instance */
264 if (!(instance = ao2_alloc(sizeof(*instance), instance_destructor))) {
265 ast_module_unref(engine->mod);
268 instance->engine = engine;
269 ast_sockaddr_copy(&instance->local_address, sa);
270 ast_sockaddr_copy(&address, sa);
272 ast_debug(1, "Using engine '%s' for RTP instance '%p'\n", engine->name, instance);
274 /* And pass it off to the engine to setup */
275 if (instance->engine->new(instance, sched, &address, data)) {
276 ast_debug(1, "Engine '%s' failed to setup RTP instance '%p'\n", engine->name, instance);
277 ao2_ref(instance, -1);
281 ast_debug(1, "RTP instance '%p' is setup and ready to go\n", instance);
286 void ast_rtp_instance_set_data(struct ast_rtp_instance *instance, void *data)
288 instance->data = data;
291 void *ast_rtp_instance_get_data(struct ast_rtp_instance *instance)
293 return instance->data;
296 int ast_rtp_instance_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
298 return instance->engine->write(instance, frame);
301 struct ast_frame *ast_rtp_instance_read(struct ast_rtp_instance *instance, int rtcp)
303 return instance->engine->read(instance, rtcp);
306 int ast_rtp_instance_set_local_address(struct ast_rtp_instance *instance,
307 const struct ast_sockaddr *address)
309 ast_sockaddr_copy(&instance->local_address, address);
313 int ast_rtp_instance_set_remote_address(struct ast_rtp_instance *instance,
314 const struct ast_sockaddr *address)
316 ast_sockaddr_copy(&instance->remote_address, address);
320 if (instance->engine->remote_address_set) {
321 instance->engine->remote_address_set(instance, &instance->remote_address);
327 int ast_rtp_instance_set_alt_remote_address(struct ast_rtp_instance *instance,
328 const struct ast_sockaddr *address)
330 ast_sockaddr_copy(&instance->alt_remote_address, address);
334 if (instance->engine->alt_remote_address_set) {
335 instance->engine->alt_remote_address_set(instance, &instance->alt_remote_address);
341 int ast_rtp_instance_get_and_cmp_local_address(struct ast_rtp_instance *instance,
342 struct ast_sockaddr *address)
344 if (ast_sockaddr_cmp(address, &instance->local_address) != 0) {
345 ast_sockaddr_copy(address, &instance->local_address);
352 void ast_rtp_instance_get_local_address(struct ast_rtp_instance *instance,
353 struct ast_sockaddr *address)
355 ast_sockaddr_copy(address, &instance->local_address);
358 int ast_rtp_instance_get_and_cmp_remote_address(struct ast_rtp_instance *instance,
359 struct ast_sockaddr *address)
361 if (ast_sockaddr_cmp(address, &instance->remote_address) != 0) {
362 ast_sockaddr_copy(address, &instance->remote_address);
369 void ast_rtp_instance_get_remote_address(struct ast_rtp_instance *instance,
370 struct ast_sockaddr *address)
372 ast_sockaddr_copy(address, &instance->remote_address);
375 void ast_rtp_instance_set_extended_prop(struct ast_rtp_instance *instance, int property, void *value)
377 if (instance->engine->extended_prop_set) {
378 instance->engine->extended_prop_set(instance, property, value);
382 void *ast_rtp_instance_get_extended_prop(struct ast_rtp_instance *instance, int property)
384 if (instance->engine->extended_prop_get) {
385 return instance->engine->extended_prop_get(instance, property);
391 void ast_rtp_instance_set_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value)
393 instance->properties[property] = value;
395 if (instance->engine->prop_set) {
396 instance->engine->prop_set(instance, property, value);
400 int ast_rtp_instance_get_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property)
402 return instance->properties[property];
405 struct ast_rtp_codecs *ast_rtp_instance_get_codecs(struct ast_rtp_instance *instance)
407 return &instance->codecs;
410 void ast_rtp_codecs_payloads_clear(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
414 for (i = 0; i < AST_RTP_MAX_PT; i++) {
415 codecs->payloads[i].asterisk_format = 0;
416 codecs->payloads[i].rtp_code = 0;
417 ast_format_clear(&codecs->payloads[i].format);
418 if (instance && instance->engine && instance->engine->payload_set) {
419 instance->engine->payload_set(instance, i, 0, NULL, 0);
424 void ast_rtp_codecs_payloads_default(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
428 ast_rwlock_rdlock(&static_RTP_PT_lock);
429 for (i = 0; i < AST_RTP_MAX_PT; i++) {
430 if (static_RTP_PT[i].rtp_code || static_RTP_PT[i].asterisk_format) {
432 codecs->payloads[i].asterisk_format = static_RTP_PT[i].asterisk_format;
433 codecs->payloads[i].rtp_code = static_RTP_PT[i].rtp_code;
434 ast_format_copy(&codecs->payloads[i].format, &static_RTP_PT[i].format);
435 if (instance && instance->engine && instance->engine->payload_set) {
436 instance->engine->payload_set(instance, i, codecs->payloads[i].asterisk_format, &codecs->payloads[i].format, codecs->payloads[i].rtp_code);
440 ast_rwlock_unlock(&static_RTP_PT_lock);
443 void ast_rtp_codecs_payloads_copy(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
447 for (i = 0; i < AST_RTP_MAX_PT; i++) {
448 if (src->payloads[i].rtp_code || src->payloads[i].asterisk_format) {
449 ast_debug(2, "Copying payload %d from %p to %p\n", i, src, dest);
450 dest->payloads[i].asterisk_format = src->payloads[i].asterisk_format;
451 dest->payloads[i].rtp_code = src->payloads[i].rtp_code;
452 ast_format_copy(&dest->payloads[i].format, &src->payloads[i].format);
453 if (instance && instance->engine && instance->engine->payload_set) {
454 instance->engine->payload_set(instance, i, dest->payloads[i].asterisk_format, &dest->payloads[i].format, dest->payloads[i].rtp_code);
460 void ast_rtp_codecs_payloads_set_m_type(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
463 ast_rwlock_rdlock(&static_RTP_PT_lock);
464 if (payload < 0 || payload >= AST_RTP_MAX_PT || (!static_RTP_PT[payload].rtp_code && !static_RTP_PT[payload].asterisk_format)) {
465 ast_rwlock_unlock(&static_RTP_PT_lock);
469 codecs->payloads[payload].asterisk_format = static_RTP_PT[payload].asterisk_format;
470 codecs->payloads[payload].rtp_code = static_RTP_PT[payload].rtp_code;
471 ast_format_copy(&codecs->payloads[payload].format, &static_RTP_PT[payload].format);
473 ast_debug(1, "Setting payload %d based on m type on %p\n", payload, codecs);
475 if (instance && instance->engine && instance->engine->payload_set) {
476 instance->engine->payload_set(instance, payload, codecs->payloads[payload].asterisk_format, &codecs->payloads[payload].format, codecs->payloads[payload].rtp_code);
478 ast_rwlock_unlock(&static_RTP_PT_lock);
481 int ast_rtp_codecs_payloads_set_rtpmap_type_rate(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int pt,
482 char *mimetype, char *mimesubtype,
483 enum ast_rtp_options options,
484 unsigned int sample_rate)
489 if (pt < 0 || pt >= AST_RTP_MAX_PT)
490 return -1; /* bogus payload type */
492 ast_rwlock_rdlock(&mime_types_lock);
493 for (i = 0; i < mime_types_len; ++i) {
494 const struct ast_rtp_mime_type *t = &ast_rtp_mime_types[i];
496 if (strcasecmp(mimesubtype, t->subtype)) {
500 if (strcasecmp(mimetype, t->type)) {
504 /* if both sample rates have been supplied, and they don't match,
505 * then this not a match; if one has not been supplied, then the
506 * rates are not compared */
507 if (sample_rate && t->sample_rate &&
508 (sample_rate != t->sample_rate)) {
513 codecs->payloads[pt] = t->payload_type;
515 if ((t->payload_type.format.id == AST_FORMAT_G726) && t->payload_type.asterisk_format && (options & AST_RTP_OPT_G726_NONSTANDARD)) {
516 ast_format_set(&codecs->payloads[pt].format, AST_FORMAT_G726_AAL2, 0);
519 if (instance && instance->engine && instance->engine->payload_set) {
520 instance->engine->payload_set(instance, pt, codecs->payloads[i].asterisk_format, &codecs->payloads[i].format, codecs->payloads[i].rtp_code);
525 ast_rwlock_unlock(&mime_types_lock);
527 return (found ? 0 : -2);
530 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)
532 return ast_rtp_codecs_payloads_set_rtpmap_type_rate(codecs, instance, payload, mimetype, mimesubtype, options, 0);
535 void ast_rtp_codecs_payloads_unset(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
537 if (payload < 0 || payload >= AST_RTP_MAX_PT) {
541 ast_debug(2, "Unsetting payload %d on %p\n", payload, codecs);
543 codecs->payloads[payload].asterisk_format = 0;
544 codecs->payloads[payload].rtp_code = 0;
545 ast_format_clear(&codecs->payloads[payload].format);
547 if (instance && instance->engine && instance->engine->payload_set) {
548 instance->engine->payload_set(instance, payload, 0, NULL, 0);
552 struct ast_rtp_payload_type ast_rtp_codecs_payload_lookup(struct ast_rtp_codecs *codecs, int payload)
554 struct ast_rtp_payload_type result = { .asterisk_format = 0, };
556 if (payload < 0 || payload >= AST_RTP_MAX_PT) {
560 result.asterisk_format = codecs->payloads[payload].asterisk_format;
561 result.rtp_code = codecs->payloads[payload].rtp_code;
562 ast_format_copy(&result.format, &codecs->payloads[payload].format);
564 if (!result.rtp_code && !result.asterisk_format) {
565 ast_rwlock_rdlock(&static_RTP_PT_lock);
566 result = static_RTP_PT[payload];
567 ast_rwlock_unlock(&static_RTP_PT_lock);
574 struct ast_format *ast_rtp_codecs_get_payload_format(struct ast_rtp_codecs *codecs, int payload)
576 if (payload < 0 || payload >= AST_RTP_MAX_PT) {
579 if (!codecs->payloads[payload].asterisk_format) {
582 return &codecs->payloads[payload].format;
585 void ast_rtp_codecs_payload_formats(struct ast_rtp_codecs *codecs, struct ast_format_cap *astformats, int *nonastformats)
589 ast_format_cap_remove_all(astformats);
592 for (i = 0; i < AST_RTP_MAX_PT; i++) {
593 if (codecs->payloads[i].rtp_code || codecs->payloads[i].asterisk_format) {
594 ast_debug(1, "Incorporating payload %d on %p\n", i, codecs);
596 if (codecs->payloads[i].asterisk_format) {
597 ast_format_cap_add(astformats, &codecs->payloads[i].format);
599 *nonastformats |= codecs->payloads[i].rtp_code;
604 int ast_rtp_codecs_payload_code(struct ast_rtp_codecs *codecs, int asterisk_format, const struct ast_format *format, int code)
608 for (i = 0; i < AST_RTP_MAX_PT; i++) {
609 if (codecs->payloads[i].asterisk_format && asterisk_format && format &&
610 (ast_format_cmp(format, &codecs->payloads[i].format) != AST_FORMAT_CMP_NOT_EQUAL)) {
612 } else if (!codecs->payloads[i].asterisk_format && !asterisk_format &&
613 (codecs->payloads[i].rtp_code == code)) {
618 ast_rwlock_rdlock(&static_RTP_PT_lock);
619 for (i = 0; i < AST_RTP_MAX_PT; i++) {
620 if (static_RTP_PT[i].asterisk_format && asterisk_format && format &&
621 (ast_format_cmp(format, &static_RTP_PT[i].format) != AST_FORMAT_CMP_NOT_EQUAL)) {
624 } else if (!static_RTP_PT[i].asterisk_format && !asterisk_format &&
625 (static_RTP_PT[i].rtp_code == code)) {
630 ast_rwlock_unlock(&static_RTP_PT_lock);
635 const char *ast_rtp_lookup_mime_subtype2(const int asterisk_format, struct ast_format *format, int code, enum ast_rtp_options options)
638 const char *res = "";
640 ast_rwlock_rdlock(&mime_types_lock);
641 for (i = 0; i < mime_types_len; i++) {
642 if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
643 (ast_format_cmp(format, &ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
644 if ((format->id == AST_FORMAT_G726_AAL2) && (options & AST_RTP_OPT_G726_NONSTANDARD)) {
648 res = ast_rtp_mime_types[i].subtype;
651 } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
652 ast_rtp_mime_types[i].payload_type.rtp_code == code) {
654 res = ast_rtp_mime_types[i].subtype;
658 ast_rwlock_unlock(&mime_types_lock);
663 unsigned int ast_rtp_lookup_sample_rate2(int asterisk_format, struct ast_format *format, int code)
666 unsigned int res = 0;
668 ast_rwlock_rdlock(&mime_types_lock);
669 for (i = 0; i < mime_types_len; ++i) {
670 if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
671 (ast_format_cmp(format, &ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
672 res = ast_rtp_mime_types[i].sample_rate;
674 } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
675 ast_rtp_mime_types[i].payload_type.rtp_code == code) {
676 res = ast_rtp_mime_types[i].sample_rate;
680 ast_rwlock_unlock(&mime_types_lock);
685 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)
694 if (asterisk_format) {
695 struct ast_format tmp_fmt;
696 ast_format_cap_iter_start(ast_format_capability);
697 while (!ast_format_cap_iter_next(ast_format_capability, &tmp_fmt)) {
698 name = ast_rtp_lookup_mime_subtype2(asterisk_format, &tmp_fmt, 0, options);
699 ast_str_append(&buf, 0, "%s|", name);
702 ast_format_cap_iter_end(ast_format_capability);
706 ast_str_append(&buf, 0, "0x%x (", (unsigned int) rtp_capability);
707 for (x = 1; x < AST_RTP_MAX; x <<= 1) {
708 if (rtp_capability & x) {
709 name = ast_rtp_lookup_mime_subtype2(asterisk_format, NULL, x, options);
710 ast_str_append(&buf, 0, "%s|", name);
716 ast_str_append(&buf, 0, "%s", found ? ")" : "nothing)");
718 return ast_str_buffer(buf);
721 void ast_rtp_codecs_packetization_set(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, struct ast_codec_pref *prefs)
723 codecs->pref = *prefs;
725 if (instance && instance->engine->packetization_set) {
726 instance->engine->packetization_set(instance, &instance->codecs.pref);
730 int ast_rtp_instance_dtmf_begin(struct ast_rtp_instance *instance, char digit)
732 return instance->engine->dtmf_begin ? instance->engine->dtmf_begin(instance, digit) : -1;
735 int ast_rtp_instance_dtmf_end(struct ast_rtp_instance *instance, char digit)
737 return instance->engine->dtmf_end ? instance->engine->dtmf_end(instance, digit) : -1;
739 int ast_rtp_instance_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration)
741 return instance->engine->dtmf_end_with_duration ? instance->engine->dtmf_end_with_duration(instance, digit, duration) : -1;
744 int ast_rtp_instance_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode)
746 return (!instance->engine->dtmf_mode_set || instance->engine->dtmf_mode_set(instance, dtmf_mode)) ? -1 : 0;
749 enum ast_rtp_dtmf_mode ast_rtp_instance_dtmf_mode_get(struct ast_rtp_instance *instance)
751 return instance->engine->dtmf_mode_get ? instance->engine->dtmf_mode_get(instance) : 0;
754 void ast_rtp_instance_update_source(struct ast_rtp_instance *instance)
756 if (instance->engine->update_source) {
757 instance->engine->update_source(instance);
761 void ast_rtp_instance_change_source(struct ast_rtp_instance *instance)
763 if (instance->engine->change_source) {
764 instance->engine->change_source(instance);
768 int ast_rtp_instance_set_qos(struct ast_rtp_instance *instance, int tos, int cos, const char *desc)
770 return instance->engine->qos ? instance->engine->qos(instance, tos, cos, desc) : -1;
773 void ast_rtp_instance_stop(struct ast_rtp_instance *instance)
775 if (instance->engine->stop) {
776 instance->engine->stop(instance);
780 int ast_rtp_instance_fd(struct ast_rtp_instance *instance, int rtcp)
782 return instance->engine->fd ? instance->engine->fd(instance, rtcp) : -1;
785 struct ast_rtp_glue *ast_rtp_instance_get_glue(const char *type)
787 struct ast_rtp_glue *glue = NULL;
789 AST_RWLIST_RDLOCK(&glues);
791 AST_RWLIST_TRAVERSE(&glues, glue, entry) {
792 if (!strcasecmp(glue->type, type)) {
797 AST_RWLIST_UNLOCK(&glues);
802 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)
804 enum ast_bridge_result res = AST_BRIDGE_FAILED;
805 struct ast_channel *who = NULL, *other = NULL, *cs[3] = { NULL, };
806 struct ast_frame *fr = NULL;
808 /* Start locally bridging both instances */
809 if (instance0->engine->local_bridge && instance0->engine->local_bridge(instance0, instance1)) {
810 ast_debug(1, "Failed to locally bridge %s to %s, backing out.\n", ast_channel_name(c0), ast_channel_name(c1));
811 ast_channel_unlock(c0);
812 ast_channel_unlock(c1);
813 return AST_BRIDGE_FAILED_NOWARN;
815 if (instance1->engine->local_bridge && instance1->engine->local_bridge(instance1, instance0)) {
816 ast_debug(1, "Failed to locally bridge %s to %s, backing out.\n", ast_channel_name(c1), ast_channel_name(c0));
817 if (instance0->engine->local_bridge) {
818 instance0->engine->local_bridge(instance0, NULL);
820 ast_channel_unlock(c0);
821 ast_channel_unlock(c1);
822 return AST_BRIDGE_FAILED_NOWARN;
825 ast_channel_unlock(c0);
826 ast_channel_unlock(c1);
828 instance0->bridged = instance1;
829 instance1->bridged = instance0;
831 ast_poll_channel_add(c0, c1);
833 /* Hop into a loop waiting for a frame from either channel */
838 /* If the underlying formats have changed force this bridge to break */
839 if ((ast_format_cmp(ast_channel_rawreadformat(c0), ast_channel_rawwriteformat(c1)) == AST_FORMAT_CMP_NOT_EQUAL) ||
840 (ast_format_cmp(ast_channel_rawreadformat(c1), ast_channel_rawwriteformat(c0)) == AST_FORMAT_CMP_NOT_EQUAL)) {
841 ast_debug(1, "rtp-engine-local-bridge: Oooh, formats changed, backing out\n");
842 res = AST_BRIDGE_FAILED_NOWARN;
845 /* Check if anything changed */
846 if ((ast_channel_tech_pvt(c0) != pvt0) ||
847 (ast_channel_tech_pvt(c1) != pvt1) ||
848 (ast_channel_masq(c0) || ast_channel_masqr(c0) || ast_channel_masq(c1) || ast_channel_masqr(c1)) ||
849 (ast_channel_monitor(c0) || ast_channel_audiohooks(c0) || ast_channel_monitor(c1) || ast_channel_audiohooks(c1)) ||
850 (!ast_framehook_list_is_empty(ast_channel_framehooks(c0)) || !ast_framehook_list_is_empty(ast_channel_framehooks(c1)))) {
851 ast_debug(1, "rtp-engine-local-bridge: Oooh, something is weird, backing out\n");
852 /* 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 */
853 if ((ast_channel_masq(c0) || ast_channel_masqr(c0)) && (fr = ast_read(c0))) {
856 if ((ast_channel_masq(c1) || ast_channel_masqr(c1)) && (fr = ast_read(c1))) {
859 res = AST_BRIDGE_RETRY;
862 /* Wait on a channel to feed us a frame */
863 if (!(who = ast_waitfor_n(cs, 2, &timeoutms))) {
865 res = AST_BRIDGE_RETRY;
868 ast_debug(2, "rtp-engine-local-bridge: Ooh, empty read...\n");
869 if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
874 /* Read in frame from channel */
876 other = (who == c0) ? c1 : c0;
877 /* Depending on the frame we may need to break out of our bridge */
878 if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
879 ((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) |
880 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1)))) {
881 /* Record received frame and who */
884 ast_debug(1, "rtp-engine-local-bridge: Ooh, got a %s\n", fr ? "digit" : "hangup");
885 res = AST_BRIDGE_COMPLETE;
887 } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
888 if ((fr->subclass.integer == AST_CONTROL_HOLD) ||
889 (fr->subclass.integer == AST_CONTROL_UNHOLD) ||
890 (fr->subclass.integer == AST_CONTROL_VIDUPDATE) ||
891 (fr->subclass.integer == AST_CONTROL_SRCUPDATE) ||
892 (fr->subclass.integer == AST_CONTROL_T38_PARAMETERS) ||
893 (fr->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER)) {
894 /* If we are going on hold, then break callback mode and P2P bridging */
895 if (fr->subclass.integer == AST_CONTROL_HOLD) {
896 if (instance0->engine->local_bridge) {
897 instance0->engine->local_bridge(instance0, NULL);
899 if (instance1->engine->local_bridge) {
900 instance1->engine->local_bridge(instance1, NULL);
902 instance0->bridged = NULL;
903 instance1->bridged = NULL;
904 } else if (fr->subclass.integer == AST_CONTROL_UNHOLD) {
905 if (instance0->engine->local_bridge) {
906 instance0->engine->local_bridge(instance0, instance1);
908 if (instance1->engine->local_bridge) {
909 instance1->engine->local_bridge(instance1, instance0);
911 instance0->bridged = instance1;
912 instance1->bridged = instance0;
914 /* Since UPDATE_BRIDGE_PEER is only used by the bridging code, don't forward it */
915 if (fr->subclass.integer != AST_CONTROL_UPDATE_RTP_PEER) {
916 ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
919 } else if (fr->subclass.integer == AST_CONTROL_CONNECTED_LINE) {
920 if (ast_channel_connected_line_sub(who, other, fr, 1) &&
921 ast_channel_connected_line_macro(who, other, fr, other == c0, 1)) {
922 ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
925 } else if (fr->subclass.integer == AST_CONTROL_REDIRECTING) {
926 if (ast_channel_redirecting_sub(who, other, fr, 1) &&
927 ast_channel_redirecting_macro(who, other, fr, other == c0, 1)) {
928 ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
934 ast_debug(1, "rtp-engine-local-bridge: Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass.integer, ast_channel_name(who));
935 res = AST_BRIDGE_COMPLETE;
939 if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
940 (fr->frametype == AST_FRAME_DTMF_END) ||
941 (fr->frametype == AST_FRAME_VOICE) ||
942 (fr->frametype == AST_FRAME_VIDEO) ||
943 (fr->frametype == AST_FRAME_IMAGE) ||
944 (fr->frametype == AST_FRAME_HTML) ||
945 (fr->frametype == AST_FRAME_MODEM) ||
946 (fr->frametype == AST_FRAME_TEXT)) {
947 ast_write(other, fr);
958 /* Stop locally bridging both instances */
959 if (instance0->engine->local_bridge) {
960 instance0->engine->local_bridge(instance0, NULL);
962 if (instance1->engine->local_bridge) {
963 instance1->engine->local_bridge(instance1, NULL);
966 instance0->bridged = NULL;
967 instance1->bridged = NULL;
969 ast_poll_channel_del(c0, c1);
974 static enum ast_bridge_result remote_bridge_loop(struct ast_channel *c0,
975 struct ast_channel *c1,
976 struct ast_rtp_instance *instance0,
977 struct ast_rtp_instance *instance1,
978 struct ast_rtp_instance *vinstance0,
979 struct ast_rtp_instance *vinstance1,
980 struct ast_rtp_instance *tinstance0,
981 struct ast_rtp_instance *tinstance1,
982 struct ast_rtp_glue *glue0,
983 struct ast_rtp_glue *glue1,
984 struct ast_format_cap *cap0,
985 struct ast_format_cap *cap1,
988 struct ast_frame **fo,
989 struct ast_channel **rc,
993 enum ast_bridge_result res = AST_BRIDGE_FAILED;
994 struct ast_channel *who = NULL, *other = NULL, *cs[3] = { NULL, };
995 struct ast_format_cap *oldcap0 = ast_format_cap_dup(cap0);
996 struct ast_format_cap *oldcap1 = ast_format_cap_dup(cap1);
997 struct ast_sockaddr ac1 = {{0,}}, vac1 = {{0,}}, tac1 = {{0,}}, ac0 = {{0,}}, vac0 = {{0,}}, tac0 = {{0,}};
998 struct ast_sockaddr t1 = {{0,}}, vt1 = {{0,}}, tt1 = {{0,}}, t0 = {{0,}}, vt0 = {{0,}}, tt0 = {{0,}};
999 struct ast_frame *fr = NULL;
1001 if (!oldcap0 || !oldcap1) {
1002 ast_channel_unlock(c0);
1003 ast_channel_unlock(c1);
1004 goto remote_bridge_cleanup;
1006 /* Test the first channel */
1007 if (!(glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0))) {
1008 ast_rtp_instance_get_remote_address(instance1, &ac1);
1010 ast_rtp_instance_get_remote_address(vinstance1, &vac1);
1013 ast_rtp_instance_get_remote_address(tinstance1, &tac1);
1016 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", ast_channel_name(c0), ast_channel_name(c1));
1019 /* Test the second channel */
1020 if (!(glue1->update_peer(c1, instance0, vinstance0, tinstance0, cap0, 0))) {
1021 ast_rtp_instance_get_remote_address(instance0, &ac0);
1023 ast_rtp_instance_get_remote_address(instance0, &vac0);
1026 ast_rtp_instance_get_remote_address(instance0, &tac0);
1029 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", ast_channel_name(c1), ast_channel_name(c0));
1032 ast_channel_unlock(c0);
1033 ast_channel_unlock(c1);
1035 instance0->bridged = instance1;
1036 instance1->bridged = instance0;
1038 ast_poll_channel_add(c0, c1);
1040 /* Go into a loop handling any stray frames that may come in */
1045 /* Check if anything changed */
1046 if ((ast_channel_tech_pvt(c0) != pvt0) ||
1047 (ast_channel_tech_pvt(c1) != pvt1) ||
1048 (ast_channel_masq(c0) || ast_channel_masqr(c0) || ast_channel_masq(c1) || ast_channel_masqr(c1)) ||
1049 (ast_channel_monitor(c0) || ast_channel_audiohooks(c0) || ast_channel_monitor(c1) || ast_channel_audiohooks(c1)) ||
1050 (!ast_framehook_list_is_empty(ast_channel_framehooks(c0)) || !ast_framehook_list_is_empty(ast_channel_framehooks(c1)))) {
1051 ast_debug(1, "Oooh, something is weird, backing out\n");
1052 res = AST_BRIDGE_RETRY;
1056 /* Check if they have changed their address */
1057 ast_rtp_instance_get_remote_address(instance1, &t1);
1059 ast_rtp_instance_get_remote_address(vinstance1, &vt1);
1062 ast_rtp_instance_get_remote_address(tinstance1, &tt1);
1064 if (glue1->get_codec) {
1065 ast_format_cap_remove_all(cap1);
1066 glue1->get_codec(c1, cap1);
1069 ast_rtp_instance_get_remote_address(instance0, &t0);
1071 ast_rtp_instance_get_remote_address(vinstance0, &vt0);
1074 ast_rtp_instance_get_remote_address(tinstance0, &tt0);
1076 if (glue0->get_codec) {
1077 ast_format_cap_remove_all(cap0);
1078 glue0->get_codec(c0, cap0);
1081 if ((ast_sockaddr_cmp(&t1, &ac1)) ||
1082 (vinstance1 && ast_sockaddr_cmp(&vt1, &vac1)) ||
1083 (tinstance1 && ast_sockaddr_cmp(&tt1, &tac1)) ||
1084 (!ast_format_cap_identical(cap1, oldcap1))) {
1085 char tmp_buf[512] = { 0, };
1086 ast_debug(1, "Oooh, '%s' changed end address to %s (format %s)\n",
1087 ast_channel_name(c1), ast_sockaddr_stringify(&t1),
1088 ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap1));
1089 ast_debug(1, "Oooh, '%s' changed end vaddress to %s (format %s)\n",
1090 ast_channel_name(c1), ast_sockaddr_stringify(&vt1),
1091 ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap1));
1092 ast_debug(1, "Oooh, '%s' changed end taddress to %s (format %s)\n",
1093 ast_channel_name(c1), ast_sockaddr_stringify(&tt1),
1094 ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap1));
1095 ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
1096 ast_channel_name(c1), ast_sockaddr_stringify(&ac1),
1097 ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap1));
1098 ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
1099 ast_channel_name(c1), ast_sockaddr_stringify(&vac1),
1100 ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap1));
1101 ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
1102 ast_channel_name(c1), ast_sockaddr_stringify(&tac1),
1103 ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap1));
1104 if (glue0->update_peer(c0,
1105 ast_sockaddr_isnull(&t1) ? NULL : instance1,
1106 ast_sockaddr_isnull(&vt1) ? NULL : vinstance1,
1107 ast_sockaddr_isnull(&tt1) ? NULL : tinstance1,
1109 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", ast_channel_name(c0), ast_channel_name(c1));
1111 ast_sockaddr_copy(&ac1, &t1);
1112 ast_sockaddr_copy(&vac1, &vt1);
1113 ast_sockaddr_copy(&tac1, &tt1);
1114 ast_format_cap_copy(oldcap1, cap1);
1116 if ((ast_sockaddr_cmp(&t0, &ac0)) ||
1117 (vinstance0 && ast_sockaddr_cmp(&vt0, &vac0)) ||
1118 (tinstance0 && ast_sockaddr_cmp(&tt0, &tac0)) ||
1119 (!ast_format_cap_identical(cap0, oldcap0))) {
1120 char tmp_buf[512] = { 0, };
1121 ast_debug(1, "Oooh, '%s' changed end address to %s (format %s)\n",
1122 ast_channel_name(c0), ast_sockaddr_stringify(&t0),
1123 ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap0));
1124 ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
1125 ast_channel_name(c0), ast_sockaddr_stringify(&ac0),
1126 ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap0));
1127 if (glue1->update_peer(c1, t0.len ? instance0 : NULL,
1128 vt0.len ? vinstance0 : NULL,
1129 tt0.len ? tinstance0 : NULL,
1131 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", ast_channel_name(c1), ast_channel_name(c0));
1133 ast_sockaddr_copy(&ac0, &t0);
1134 ast_sockaddr_copy(&vac0, &vt0);
1135 ast_sockaddr_copy(&tac0, &tt0);
1136 ast_format_cap_copy(oldcap0, cap0);
1139 /* Wait for frame to come in on the channels */
1140 if (!(who = ast_waitfor_n(cs, 2, &timeoutms))) {
1142 res = AST_BRIDGE_RETRY;
1145 ast_debug(1, "Ooh, empty read...\n");
1146 if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
1152 other = (who == c0) ? c1 : c0;
1153 if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
1154 (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
1155 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))))) {
1156 /* Break out of bridge */
1159 ast_debug(1, "Oooh, got a %s\n", fr ? "digit" : "hangup");
1160 res = AST_BRIDGE_COMPLETE;
1162 } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
1163 if ((fr->subclass.integer == AST_CONTROL_HOLD) ||
1164 (fr->subclass.integer == AST_CONTROL_UNHOLD) ||
1165 (fr->subclass.integer == AST_CONTROL_VIDUPDATE) ||
1166 (fr->subclass.integer == AST_CONTROL_SRCUPDATE) ||
1167 (fr->subclass.integer == AST_CONTROL_T38_PARAMETERS) ||
1168 (fr->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER)) {
1169 if (fr->subclass.integer == AST_CONTROL_HOLD) {
1170 /* If we someone went on hold we want the other side to reinvite back to us */
1172 glue1->update_peer(c1, NULL, NULL, NULL, 0, 0);
1174 glue0->update_peer(c0, NULL, NULL, NULL, 0, 0);
1176 } else if (fr->subclass.integer == AST_CONTROL_UNHOLD ||
1177 fr->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER) {
1178 /* If they went off hold they should go back to being direct, or if we have
1179 * been told to force a peer update, go ahead and do it. */
1181 glue1->update_peer(c1, instance0, vinstance0, tinstance0, cap0, 0);
1183 glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0);
1186 /* Update local address information */
1187 ast_rtp_instance_get_remote_address(instance0, &t0);
1188 ast_sockaddr_copy(&ac0, &t0);
1189 ast_rtp_instance_get_remote_address(instance1, &t1);
1190 ast_sockaddr_copy(&ac1, &t1);
1191 /* Update codec information */
1192 if (glue0->get_codec && ast_channel_tech_pvt(c0)) {
1193 ast_format_cap_remove_all(cap0);
1194 ast_format_cap_remove_all(oldcap0);
1195 glue0->get_codec(c0, cap0);
1196 ast_format_cap_append(oldcap0, cap0);
1199 if (glue1->get_codec && ast_channel_tech_pvt(c1)) {
1200 ast_format_cap_remove_all(cap1);
1201 ast_format_cap_remove_all(oldcap1);
1202 glue0->get_codec(c1, cap1);
1203 ast_format_cap_append(oldcap1, cap1);
1205 /* Since UPDATE_BRIDGE_PEER is only used by the bridging code, don't forward it */
1206 if (fr->subclass.integer != AST_CONTROL_UPDATE_RTP_PEER) {
1207 ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
1210 } else if (fr->subclass.integer == AST_CONTROL_CONNECTED_LINE) {
1211 if (ast_channel_connected_line_sub(who, other, fr, 1) &&
1212 ast_channel_connected_line_macro(who, other, fr, other == c0, 1)) {
1213 ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
1216 } else if (fr->subclass.integer == AST_CONTROL_REDIRECTING) {
1217 if (ast_channel_redirecting_sub(who, other, fr, 1) &&
1218 ast_channel_redirecting_macro(who, other, fr, other == c0, 1)) {
1219 ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
1225 ast_debug(1, "Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass.integer, ast_channel_name(who));
1226 res = AST_BRIDGE_COMPLETE;
1227 goto remote_bridge_cleanup;
1230 if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
1231 (fr->frametype == AST_FRAME_DTMF_END) ||
1232 (fr->frametype == AST_FRAME_VOICE) ||
1233 (fr->frametype == AST_FRAME_VIDEO) ||
1234 (fr->frametype == AST_FRAME_IMAGE) ||
1235 (fr->frametype == AST_FRAME_HTML) ||
1236 (fr->frametype == AST_FRAME_MODEM) ||
1237 (fr->frametype == AST_FRAME_TEXT)) {
1238 ast_write(other, fr);
1248 if (ast_test_flag(ast_channel_flags(c0), AST_FLAG_ZOMBIE)) {
1249 ast_debug(1, "Channel '%s' Zombie cleardown from bridge\n", ast_channel_name(c0));
1250 } else if (ast_channel_tech_pvt(c0) != pvt0) {
1251 ast_debug(1, "Channel c0->'%s' pvt changed, in bridge with c1->'%s'\n", ast_channel_name(c0), ast_channel_name(c1));
1252 } else if (glue0 != ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) {
1253 ast_debug(1, "Channel c0->'%s' technology changed, in bridge with c1->'%s'\n", ast_channel_name(c0), ast_channel_name(c1));
1254 } else if (glue0->update_peer(c0, NULL, NULL, NULL, 0, 0)) {
1255 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", ast_channel_name(c0));
1257 if (ast_test_flag(ast_channel_flags(c1), AST_FLAG_ZOMBIE)) {
1258 ast_debug(1, "Channel '%s' Zombie cleardown from bridge\n", ast_channel_name(c1));
1259 } else if (ast_channel_tech_pvt(c1) != pvt1) {
1260 ast_debug(1, "Channel c1->'%s' pvt changed, in bridge with c0->'%s'\n", ast_channel_name(c1), ast_channel_name(c0));
1261 } else if (glue1 != ast_rtp_instance_get_glue(ast_channel_tech(c1)->type)) {
1262 ast_debug(1, "Channel c1->'%s' technology changed, in bridge with c0->'%s'\n", ast_channel_name(c1), ast_channel_name(c0));
1263 } else if (glue1->update_peer(c1, NULL, NULL, NULL, 0, 0)) {
1264 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", ast_channel_name(c1));
1267 instance0->bridged = NULL;
1268 instance1->bridged = NULL;
1270 ast_poll_channel_del(c0, c1);
1272 remote_bridge_cleanup:
1273 ast_format_cap_destroy(oldcap0);
1274 ast_format_cap_destroy(oldcap1);
1280 * \brief Conditionally unref an rtp instance
1282 static void unref_instance_cond(struct ast_rtp_instance **instance)
1285 ao2_ref(*instance, -1);
1290 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)
1292 struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
1293 *vinstance0 = NULL, *vinstance1 = NULL,
1294 *tinstance0 = NULL, *tinstance1 = NULL;
1295 struct ast_rtp_glue *glue0, *glue1;
1296 struct ast_sockaddr addr1 = { {0, }, }, addr2 = { {0, }, };
1297 enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1298 enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1299 enum ast_bridge_result res = AST_BRIDGE_FAILED;
1300 enum ast_rtp_dtmf_mode dmode;
1301 struct ast_format_cap *cap0 = ast_format_cap_alloc_nolock();
1302 struct ast_format_cap *cap1 = ast_format_cap_alloc_nolock();
1303 int unlock_chans = 1;
1305 if (!cap0 || !cap1) {
1310 /* Lock both channels so we can look for the glue that binds them together */
1311 ast_channel_lock(c0);
1312 while (ast_channel_trylock(c1)) {
1313 ast_channel_unlock(c0);
1315 ast_channel_lock(c0);
1318 /* Ensure neither channel got hungup during lock avoidance */
1319 if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
1320 ast_log(LOG_WARNING, "Got hangup while attempting to bridge '%s' and '%s'\n", ast_channel_name(c0), ast_channel_name(c1));
1324 /* Grab glue that binds each channel to something using the RTP engine */
1325 if (!(glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) || !(glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type))) {
1326 ast_debug(1, "Can't find native functions for channel '%s'\n", glue0 ? ast_channel_name(c1) : ast_channel_name(c0));
1330 audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
1331 video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1333 audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
1334 video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1336 /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
1337 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)) {
1338 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1340 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)) {
1341 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1344 /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
1345 if (audio_glue0_res == AST_RTP_GLUE_RESULT_FORBID || audio_glue1_res == AST_RTP_GLUE_RESULT_FORBID) {
1346 res = AST_BRIDGE_FAILED_NOWARN;
1351 /* If address families differ, force a local bridge */
1352 ast_rtp_instance_get_remote_address(instance0, &addr1);
1353 ast_rtp_instance_get_remote_address(instance1, &addr2);
1355 if (addr1.ss.ss_family != addr2.ss.ss_family ||
1356 (ast_sockaddr_is_ipv4_mapped(&addr1) != ast_sockaddr_is_ipv4_mapped(&addr2))) {
1357 audio_glue0_res = AST_RTP_GLUE_RESULT_LOCAL;
1358 audio_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
1361 /* If we need to get DTMF see if we can do it outside of the RTP stream itself */
1362 dmode = ast_rtp_instance_dtmf_mode_get(instance0);
1363 if ((flags & AST_BRIDGE_DTMF_CHANNEL_0) && dmode) {
1364 res = AST_BRIDGE_FAILED_NOWARN;
1367 dmode = ast_rtp_instance_dtmf_mode_get(instance1);
1368 if ((flags & AST_BRIDGE_DTMF_CHANNEL_1) && dmode) {
1369 res = AST_BRIDGE_FAILED_NOWARN;
1373 /* 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 */
1374 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)))) {
1375 res = AST_BRIDGE_FAILED_NOWARN;
1379 /* Make sure that codecs match */
1380 if (glue0->get_codec){
1381 glue0->get_codec(c0, cap0);
1383 if (glue1->get_codec) {
1384 glue1->get_codec(c1, cap1);
1386 if (!ast_format_cap_is_empty(cap0) && !ast_format_cap_is_empty(cap1) && !ast_format_cap_has_joint(cap0, cap1)) {
1387 char tmp0[256] = { 0, };
1388 char tmp1[256] = { 0, };
1389 ast_debug(1, "Channel codec0 = %s is not codec1 = %s, cannot native bridge in RTP.\n",
1390 ast_getformatname_multiple(tmp0, sizeof(tmp0), cap0),
1391 ast_getformatname_multiple(tmp1, sizeof(tmp1), cap1));
1392 res = AST_BRIDGE_FAILED_NOWARN;
1396 instance0->glue = glue0;
1397 instance1->glue = glue1;
1398 instance0->chan = c0;
1399 instance1->chan = c1;
1401 /* Depending on the end result for bridging either do a local bridge or remote bridge */
1402 if (audio_glue0_res == AST_RTP_GLUE_RESULT_LOCAL || audio_glue1_res == AST_RTP_GLUE_RESULT_LOCAL) {
1403 ast_verb(3, "Locally bridging %s and %s\n", ast_channel_name(c0), ast_channel_name(c1));
1404 res = local_bridge_loop(c0, c1, instance0, instance1, timeoutms, flags, fo, rc, ast_channel_tech_pvt(c0), ast_channel_tech_pvt(c1));
1406 ast_verb(3, "Remotely bridging %s and %s\n", ast_channel_name(c0), ast_channel_name(c1));
1407 res = remote_bridge_loop(c0, c1, instance0, instance1, vinstance0, vinstance1,
1408 tinstance0, tinstance1, glue0, glue1, cap0, cap1, timeoutms, flags,
1409 fo, rc, ast_channel_tech_pvt(c0), ast_channel_tech_pvt(c1));
1412 instance0->glue = NULL;
1413 instance1->glue = NULL;
1414 instance0->chan = NULL;
1415 instance1->chan = NULL;
1421 ast_channel_unlock(c0);
1422 ast_channel_unlock(c1);
1424 ast_format_cap_destroy(cap1);
1425 ast_format_cap_destroy(cap0);
1427 unref_instance_cond(&instance0);
1428 unref_instance_cond(&instance1);
1429 unref_instance_cond(&vinstance0);
1430 unref_instance_cond(&vinstance1);
1431 unref_instance_cond(&tinstance0);
1432 unref_instance_cond(&tinstance1);
1437 struct ast_rtp_instance *ast_rtp_instance_get_bridged(struct ast_rtp_instance *instance)
1439 return instance->bridged;
1442 void ast_rtp_instance_early_bridge_make_compatible(struct ast_channel *c0, struct ast_channel *c1)
1444 struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
1445 *vinstance0 = NULL, *vinstance1 = NULL,
1446 *tinstance0 = NULL, *tinstance1 = NULL;
1447 struct ast_rtp_glue *glue0, *glue1;
1448 enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1449 enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1450 struct ast_format_cap *cap0 = ast_format_cap_alloc_nolock();
1451 struct ast_format_cap *cap1 = ast_format_cap_alloc_nolock();
1453 /* Lock both channels so we can look for the glue that binds them together */
1454 ast_channel_lock_both(c0, c1);
1456 if (!cap1 || !cap0) {
1460 /* Grab glue that binds each channel to something using the RTP engine */
1461 if (!(glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) || !(glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type))) {
1462 ast_debug(1, "Can't find native functions for channel '%s'\n", glue0 ? ast_channel_name(c1) : ast_channel_name(c0));
1466 audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
1467 video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1469 audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
1470 video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1472 /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
1473 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)) {
1474 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1476 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)) {
1477 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1479 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) {
1480 glue0->get_codec(c0, cap0);
1482 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) {
1483 glue1->get_codec(c1, cap1);
1486 /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
1487 if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
1491 /* Make sure we have matching codecs */
1492 if (!ast_format_cap_has_joint(cap0, cap1)) {
1496 ast_rtp_codecs_payloads_copy(&instance0->codecs, &instance1->codecs, instance1);
1498 if (vinstance0 && vinstance1) {
1499 ast_rtp_codecs_payloads_copy(&vinstance0->codecs, &vinstance1->codecs, vinstance1);
1501 if (tinstance0 && tinstance1) {
1502 ast_rtp_codecs_payloads_copy(&tinstance0->codecs, &tinstance1->codecs, tinstance1);
1505 if (glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0)) {
1506 ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n",
1507 ast_channel_name(c0), ast_channel_name(c1));
1509 ast_debug(1, "Seeded SDP of '%s' with that of '%s'\n",
1510 ast_channel_name(c0), ast_channel_name(c1));
1514 ast_channel_unlock(c0);
1515 ast_channel_unlock(c1);
1517 ast_format_cap_destroy(cap0);
1518 ast_format_cap_destroy(cap1);
1520 unref_instance_cond(&instance0);
1521 unref_instance_cond(&instance1);
1522 unref_instance_cond(&vinstance0);
1523 unref_instance_cond(&vinstance1);
1524 unref_instance_cond(&tinstance0);
1525 unref_instance_cond(&tinstance1);
1528 int ast_rtp_instance_early_bridge(struct ast_channel *c0, struct ast_channel *c1)
1530 struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
1531 *vinstance0 = NULL, *vinstance1 = NULL,
1532 *tinstance0 = NULL, *tinstance1 = NULL;
1533 struct ast_rtp_glue *glue0, *glue1;
1534 enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1535 enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1536 struct ast_format_cap *cap0 = ast_format_cap_alloc_nolock();
1537 struct ast_format_cap *cap1 = ast_format_cap_alloc_nolock();
1540 /* If there is no second channel just immediately bail out, we are of no use in that scenario */
1542 ast_format_cap_destroy(cap0);
1543 ast_format_cap_destroy(cap1);
1547 /* Lock both channels so we can look for the glue that binds them together */
1548 ast_channel_lock(c0);
1549 while (ast_channel_trylock(c1)) {
1550 ast_channel_unlock(c0);
1552 ast_channel_lock(c0);
1555 if (!cap1 || !cap0) {
1559 /* Grab glue that binds each channel to something using the RTP engine */
1560 if (!(glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) || !(glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type))) {
1561 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", glue0 ? ast_channel_name(c1) : ast_channel_name(c0));
1565 audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
1566 video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1568 audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
1569 video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1571 /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
1572 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)) {
1573 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1575 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)) {
1576 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1578 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) {
1579 glue0->get_codec(c0, cap0);
1581 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) {
1582 glue1->get_codec(c1, cap1);
1585 /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
1586 if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
1590 /* Make sure we have matching codecs */
1591 if (!ast_format_cap_has_joint(cap0, cap1)) {
1595 /* Bridge media early */
1596 if (glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0)) {
1597 ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
1603 ast_channel_unlock(c0);
1604 ast_channel_unlock(c1);
1606 ast_format_cap_destroy(cap0);
1607 ast_format_cap_destroy(cap1);
1609 unref_instance_cond(&instance0);
1610 unref_instance_cond(&instance1);
1611 unref_instance_cond(&vinstance0);
1612 unref_instance_cond(&vinstance1);
1613 unref_instance_cond(&tinstance0);
1614 unref_instance_cond(&tinstance1);
1617 ast_debug(1, "Setting early bridge SDP of '%s' with that of '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
1623 int ast_rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations)
1625 return instance->engine->red_init ? instance->engine->red_init(instance, buffer_time, payloads, generations) : -1;
1628 int ast_rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame)
1630 return instance->engine->red_buffer ? instance->engine->red_buffer(instance, frame) : -1;
1633 int ast_rtp_instance_get_stats(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat)
1635 return instance->engine->get_stat ? instance->engine->get_stat(instance, stats, stat) : -1;
1638 char *ast_rtp_instance_get_quality(struct ast_rtp_instance *instance, enum ast_rtp_instance_stat_field field, char *buf, size_t size)
1640 struct ast_rtp_instance_stats stats = { 0, };
1641 enum ast_rtp_instance_stat stat;
1643 /* Determine what statistics we will need to retrieve based on field passed in */
1644 if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
1645 stat = AST_RTP_INSTANCE_STAT_ALL;
1646 } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
1647 stat = AST_RTP_INSTANCE_STAT_COMBINED_JITTER;
1648 } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
1649 stat = AST_RTP_INSTANCE_STAT_COMBINED_LOSS;
1650 } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
1651 stat = AST_RTP_INSTANCE_STAT_COMBINED_RTT;
1656 /* Attempt to actually retrieve the statistics we need to generate the quality string */
1657 if (ast_rtp_instance_get_stats(instance, &stats, stat)) {
1661 /* Now actually fill the buffer with the good information */
1662 if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
1663 snprintf(buf, size, "ssrc=%i;themssrc=%u;lp=%u;rxjitter=%f;rxcount=%u;txjitter=%f;txcount=%u;rlp=%u;rtt=%f",
1664 stats.local_ssrc, stats.remote_ssrc, stats.rxploss, stats.txjitter, stats.rxcount, stats.rxjitter, stats.txcount, stats.txploss, stats.rtt);
1665 } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
1666 snprintf(buf, size, "minrxjitter=%f;maxrxjitter=%f;avgrxjitter=%f;stdevrxjitter=%f;reported_minjitter=%f;reported_maxjitter=%f;reported_avgjitter=%f;reported_stdevjitter=%f;",
1667 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));
1668 } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
1669 snprintf(buf, size, "minrxlost=%f;maxrxlost=%f;avgrxlost=%f;stdevrxlost=%f;reported_minlost=%f;reported_maxlost=%f;reported_avglost=%f;reported_stdevlost=%f;",
1670 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));
1671 } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
1672 snprintf(buf, size, "minrtt=%f;maxrtt=%f;avgrtt=%f;stdevrtt=%f;", stats.minrtt, stats.maxrtt, stats.normdevrtt, stats.stdevrtt);
1678 void ast_rtp_instance_set_stats_vars(struct ast_channel *chan, struct ast_rtp_instance *instance)
1680 char quality_buf[AST_MAX_USER_FIELD], *quality;
1681 struct ast_channel *bridge = ast_bridged_channel(chan);
1683 if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
1684 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOS", quality);
1686 pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSBRIDGED", quality);
1690 if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER, quality_buf, sizeof(quality_buf)))) {
1691 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSJITTER", quality);
1693 pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSJITTERBRIDGED", quality);
1697 if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS, quality_buf, sizeof(quality_buf)))) {
1698 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSLOSS", quality);
1700 pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSLOSSBRIDGED", quality);
1704 if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT, quality_buf, sizeof(quality_buf)))) {
1705 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSRTT", quality);
1707 pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSRTTBRIDGED", quality);
1712 int ast_rtp_instance_set_read_format(struct ast_rtp_instance *instance, struct ast_format *format)
1714 return instance->engine->set_read_format ? instance->engine->set_read_format(instance, format) : -1;
1717 int ast_rtp_instance_set_write_format(struct ast_rtp_instance *instance, struct ast_format *format)
1719 return instance->engine->set_write_format ? instance->engine->set_write_format(instance, format) : -1;
1722 int ast_rtp_instance_make_compatible(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_channel *peer)
1724 struct ast_rtp_glue *glue;
1725 struct ast_rtp_instance *peer_instance = NULL;
1728 if (!instance->engine->make_compatible) {
1732 ast_channel_lock(peer);
1734 if (!(glue = ast_rtp_instance_get_glue(ast_channel_tech(peer)->type))) {
1735 ast_channel_unlock(peer);
1739 glue->get_rtp_info(peer, &peer_instance);
1741 if (!peer_instance || peer_instance->engine != instance->engine) {
1742 ast_channel_unlock(peer);
1743 ao2_ref(peer_instance, -1);
1744 peer_instance = NULL;
1748 res = instance->engine->make_compatible(chan, instance, peer, peer_instance);
1750 ast_channel_unlock(peer);
1752 ao2_ref(peer_instance, -1);
1753 peer_instance = NULL;
1758 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)
1760 if (instance->engine->available_formats) {
1761 instance->engine->available_formats(instance, to_endpoint, to_asterisk, result);
1762 if (!ast_format_cap_is_empty(result)) {
1767 ast_translate_available_formats(to_endpoint, to_asterisk, result);
1770 int ast_rtp_instance_activate(struct ast_rtp_instance *instance)
1772 return instance->engine->activate ? instance->engine->activate(instance) : 0;
1775 void ast_rtp_instance_stun_request(struct ast_rtp_instance *instance,
1776 struct ast_sockaddr *suggestion,
1777 const char *username)
1779 if (instance->engine->stun_request) {
1780 instance->engine->stun_request(instance, suggestion, username);
1784 void ast_rtp_instance_set_timeout(struct ast_rtp_instance *instance, int timeout)
1786 instance->timeout = timeout;
1789 void ast_rtp_instance_set_hold_timeout(struct ast_rtp_instance *instance, int timeout)
1791 instance->holdtimeout = timeout;
1794 void ast_rtp_instance_set_keepalive(struct ast_rtp_instance *instance, int interval)
1796 instance->keepalive = interval;
1799 int ast_rtp_instance_get_timeout(struct ast_rtp_instance *instance)
1801 return instance->timeout;
1804 int ast_rtp_instance_get_hold_timeout(struct ast_rtp_instance *instance)
1806 return instance->holdtimeout;
1809 int ast_rtp_instance_get_keepalive(struct ast_rtp_instance *instance)
1811 return instance->keepalive;
1814 struct ast_rtp_engine *ast_rtp_instance_get_engine(struct ast_rtp_instance *instance)
1816 return instance->engine;
1819 struct ast_rtp_glue *ast_rtp_instance_get_active_glue(struct ast_rtp_instance *instance)
1821 return instance->glue;
1824 struct ast_channel *ast_rtp_instance_get_chan(struct ast_rtp_instance *instance)
1826 return instance->chan;
1829 int ast_rtp_engine_register_srtp(struct ast_srtp_res *srtp_res, struct ast_srtp_policy_res *policy_res)
1831 if (res_srtp || res_srtp_policy) {
1834 if (!srtp_res || !policy_res) {
1838 res_srtp = srtp_res;
1839 res_srtp_policy = policy_res;
1844 void ast_rtp_engine_unregister_srtp(void)
1847 res_srtp_policy = NULL;
1850 int ast_rtp_engine_srtp_is_registered(void)
1852 return res_srtp && res_srtp_policy;
1855 int ast_rtp_instance_add_srtp_policy(struct ast_rtp_instance *instance, struct ast_srtp_policy *remote_policy, struct ast_srtp_policy *local_policy)
1863 if (!instance->srtp) {
1864 res = res_srtp->create(&instance->srtp, instance, remote_policy);
1866 res = res_srtp->replace(&instance->srtp, instance, remote_policy);
1869 res = res_srtp->add_stream(instance->srtp, local_policy);
1875 struct ast_srtp *ast_rtp_instance_get_srtp(struct ast_rtp_instance *instance)
1877 return instance->srtp;
1880 int ast_rtp_instance_sendcng(struct ast_rtp_instance *instance, int level)
1882 if (instance->engine->sendcng) {
1883 return instance->engine->sendcng(instance, level);
1889 static void set_next_mime_type(const struct ast_format *format, int rtp_code, char *type, char *subtype, unsigned int sample_rate)
1891 int x = mime_types_len;
1892 if (ARRAY_LEN(ast_rtp_mime_types) == mime_types_len) {
1896 ast_rwlock_wrlock(&mime_types_lock);
1898 ast_rtp_mime_types[x].payload_type.asterisk_format = 1;
1899 ast_format_copy(&ast_rtp_mime_types[x].payload_type.format, format);
1901 ast_rtp_mime_types[x].payload_type.rtp_code = rtp_code;
1903 ast_rtp_mime_types[x].type = type;
1904 ast_rtp_mime_types[x].subtype = subtype;
1905 ast_rtp_mime_types[x].sample_rate = sample_rate;
1907 ast_rwlock_unlock(&mime_types_lock);
1910 static void add_static_payload(int map, const struct ast_format *format, int rtp_code)
1913 ast_rwlock_wrlock(&static_RTP_PT_lock);
1915 /* find next available dynamic payload slot */
1916 for (x = 96; x < 127; x++) {
1917 if (!static_RTP_PT[x].asterisk_format && !static_RTP_PT[x].rtp_code) {
1925 ast_log(LOG_WARNING, "No Dynamic RTP mapping avaliable for format %s\n" ,ast_getformatname(format));
1926 ast_rwlock_unlock(&static_RTP_PT_lock);
1931 static_RTP_PT[map].asterisk_format = 1;
1932 ast_format_copy(&static_RTP_PT[map].format, format);
1934 static_RTP_PT[map].rtp_code = rtp_code;
1936 ast_rwlock_unlock(&static_RTP_PT_lock);
1939 int ast_rtp_engine_load_format(const struct ast_format *format)
1941 switch (format->id) {
1942 case AST_FORMAT_SILK:
1943 set_next_mime_type(format, 0, "audio", "SILK", ast_format_rate(format));
1944 add_static_payload(-1, format, 0);
1946 case AST_FORMAT_CELT:
1947 set_next_mime_type(format, 0, "audio", "CELT", ast_format_rate(format));
1948 add_static_payload(-1, format, 0);
1957 int ast_rtp_engine_unload_format(const struct ast_format *format)
1962 ast_rwlock_wrlock(&static_RTP_PT_lock);
1963 /* remove everything pertaining to this format id from the lists */
1964 for (x = 0; x < AST_RTP_MAX_PT; x++) {
1965 if (ast_format_cmp(&static_RTP_PT[x].format, format) == AST_FORMAT_CMP_EQUAL) {
1966 memset(&static_RTP_PT[x], 0, sizeof(struct ast_rtp_payload_type));
1969 ast_rwlock_unlock(&static_RTP_PT_lock);
1972 ast_rwlock_wrlock(&mime_types_lock);
1973 /* rebuild the list skipping the items matching this id */
1974 for (x = 0; x < mime_types_len; x++) {
1975 if (ast_format_cmp(&ast_rtp_mime_types[x].payload_type.format, format) == AST_FORMAT_CMP_EQUAL) {
1978 ast_rtp_mime_types[y] = ast_rtp_mime_types[x];
1982 ast_rwlock_unlock(&mime_types_lock);
1986 int ast_rtp_engine_init()
1988 struct ast_format tmpfmt;
1990 ast_rwlock_init(&mime_types_lock);
1991 ast_rwlock_init(&static_RTP_PT_lock);
1993 /* Define all the RTP mime types available */
1994 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G723_1, 0), 0, "audio", "G723", 8000);
1995 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_GSM, 0), 0, "audio", "GSM", 8000);
1996 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0, "audio", "PCMU", 8000);
1997 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0, "audio", "G711U", 8000);
1998 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), 0, "audio", "PCMA", 8000);
1999 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), 0, "audio", "G711A", 8000);
2000 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G726, 0), 0, "audio", "G726-32", 8000);
2001 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0, "audio", "DVI4", 8000);
2002 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), 0, "audio", "L16", 8000);
2003 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0, "audio", "L16", 16000);
2004 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0, "audio", "L16-256", 16000);
2005 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_LPC10, 0), 0, "audio", "LPC", 8000);
2006 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0, "audio", "G729", 8000);
2007 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0, "audio", "G729A", 8000);
2008 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0, "audio", "G.729", 8000);
2009 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX, 0), 0, "audio", "speex", 8000);
2010 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX16, 0), 0, "audio", "speex", 16000);
2011 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX32, 0), 0, "audio", "speex", 32000);
2012 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ILBC, 0), 0, "audio", "iLBC", 8000);
2013 /* this is the sample rate listed in the RTP profile for the G.722 codec, *NOT* the actual sample rate of the media stream */
2014 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G722, 0), 0, "audio", "G722", 8000);
2015 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G726_AAL2, 0), 0, "audio", "AAL2-G726-32", 8000);
2016 set_next_mime_type(NULL, AST_RTP_DTMF, "audio", "telephone-event", 8000);
2017 set_next_mime_type(NULL, AST_RTP_CISCO_DTMF, "audio", "cisco-telephone-event", 8000);
2018 set_next_mime_type(NULL, AST_RTP_CN, "audio", "CN", 8000);
2019 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_JPEG, 0), 0, "video", "JPEG", 90000);
2020 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_PNG, 0), 0, "video", "PNG", 90000);
2021 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H261, 0), 0, "video", "H261", 90000);
2022 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H263, 0), 0, "video", "H263", 90000);
2023 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0, "video", "h263-1998", 90000);
2024 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H264, 0), 0, "video", "H264", 90000);
2025 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_MP4_VIDEO, 0), 0, "video", "MP4V-ES", 90000);
2026 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_T140RED, 0), 0, "text", "RED", 1000);
2027 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_T140, 0), 0, "text", "T140", 1000);
2028 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SIREN7, 0), 0, "audio", "G7221", 16000);
2029 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SIREN14, 0), 0, "audio", "G7221", 32000);
2030 set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G719, 0), 0, "audio", "G719", 48000);
2032 /* Define the static rtp payload mappings */
2033 add_static_payload(0, ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0);
2034 #ifdef USE_DEPRECATED_G726
2035 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... */
2037 add_static_payload(3, ast_format_set(&tmpfmt, AST_FORMAT_GSM, 0), 0);
2038 add_static_payload(4, ast_format_set(&tmpfmt, AST_FORMAT_G723_1, 0), 0);
2039 add_static_payload(5, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0);/* 8 kHz */
2040 add_static_payload(6, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0); /* 16 kHz */
2041 add_static_payload(7, ast_format_set(&tmpfmt, AST_FORMAT_LPC10, 0), 0);
2042 add_static_payload(8, ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), 0);
2043 add_static_payload(9, ast_format_set(&tmpfmt, AST_FORMAT_G722, 0), 0);
2044 add_static_payload(10, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), 0); /* 2 channels */
2045 add_static_payload(11, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), 0); /* 1 channel */
2046 add_static_payload(13, NULL, AST_RTP_CN);
2047 add_static_payload(16, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0); /* 11.025 kHz */
2048 add_static_payload(17, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0); /* 22.050 kHz */
2049 add_static_payload(18, ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0);
2050 add_static_payload(19, NULL, AST_RTP_CN); /* Also used for CN */
2051 add_static_payload(26, ast_format_set(&tmpfmt, AST_FORMAT_JPEG, 0), 0);
2052 add_static_payload(31, ast_format_set(&tmpfmt, AST_FORMAT_H261, 0), 0);
2053 add_static_payload(34, ast_format_set(&tmpfmt, AST_FORMAT_H263, 0), 0);
2054 add_static_payload(97, ast_format_set(&tmpfmt, AST_FORMAT_ILBC, 0), 0);
2055 add_static_payload(98, ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0);
2056 add_static_payload(99, ast_format_set(&tmpfmt, AST_FORMAT_H264, 0), 0);
2057 add_static_payload(101, NULL, AST_RTP_DTMF);
2058 add_static_payload(102, ast_format_set(&tmpfmt, AST_FORMAT_SIREN7, 0), 0);
2059 add_static_payload(103, ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0);
2060 add_static_payload(104, ast_format_set(&tmpfmt, AST_FORMAT_MP4_VIDEO, 0), 0);
2061 add_static_payload(105, ast_format_set(&tmpfmt, AST_FORMAT_T140RED, 0), 0); /* Real time text chat (with redundancy encoding) */
2062 add_static_payload(106, ast_format_set(&tmpfmt, AST_FORMAT_T140, 0), 0); /* Real time text chat */
2063 add_static_payload(110, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX, 0), 0);
2064 add_static_payload(111, ast_format_set(&tmpfmt, AST_FORMAT_G726, 0), 0);
2065 add_static_payload(112, ast_format_set(&tmpfmt, AST_FORMAT_G726_AAL2, 0), 0);
2066 add_static_payload(115, ast_format_set(&tmpfmt, AST_FORMAT_SIREN14, 0), 0);
2067 add_static_payload(116, ast_format_set(&tmpfmt, AST_FORMAT_G719, 0), 0);
2068 add_static_payload(117, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX16, 0), 0);
2069 add_static_payload(118, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0); /* 16 Khz signed linear */
2070 add_static_payload(119, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX32, 0), 0);
2071 add_static_payload(121, NULL, AST_RTP_CISCO_DTMF); /* Must be type 121 */