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"
41 /*! Structure that represents an RTP session (instance) */
42 struct ast_rtp_instance {
43 /*! Engine that is handling this RTP instance */
44 struct ast_rtp_engine *engine;
45 /*! Data unique to the RTP engine */
47 /*! RTP properties that have been set and their value */
48 int properties[AST_RTP_PROPERTY_MAX];
49 /*! Address that we are expecting RTP to come in to */
50 struct sockaddr_in local_address;
51 /*! Address that we are sending RTP to */
52 struct sockaddr_in remote_address;
53 /*! Instance that we are bridged to if doing remote or local bridging */
54 struct ast_rtp_instance *bridged;
55 /*! Payload and packetization information */
56 struct ast_rtp_codecs codecs;
57 /*! RTP timeout time (negative or zero means disabled, negative value means temporarily disabled) */
59 /*! RTP timeout when on hold (negative or zero means disabled, negative value means temporarily disabled). */
61 /*! DTMF mode in use */
62 enum ast_rtp_dtmf_mode dtmf_mode;
65 /*! List of RTP engines that are currently registered */
66 static AST_RWLIST_HEAD_STATIC(engines, ast_rtp_engine);
68 /*! List of RTP glues */
69 static AST_RWLIST_HEAD_STATIC(glues, ast_rtp_glue);
71 /*! The following array defines the MIME Media type (and subtype) for each
72 of our codecs, or RTP-specific data type. */
73 static const struct ast_rtp_mime_type {
74 struct ast_rtp_payload_type payload_type;
77 unsigned int sample_rate;
78 } ast_rtp_mime_types[] = {
79 {{1, AST_FORMAT_G723_1}, "audio", "G723", 8000},
80 {{1, AST_FORMAT_GSM}, "audio", "GSM", 8000},
81 {{1, AST_FORMAT_ULAW}, "audio", "PCMU", 8000},
82 {{1, AST_FORMAT_ULAW}, "audio", "G711U", 8000},
83 {{1, AST_FORMAT_ALAW}, "audio", "PCMA", 8000},
84 {{1, AST_FORMAT_ALAW}, "audio", "G711A", 8000},
85 {{1, AST_FORMAT_G726}, "audio", "G726-32", 8000},
86 {{1, AST_FORMAT_ADPCM}, "audio", "DVI4", 8000},
87 {{1, AST_FORMAT_SLINEAR}, "audio", "L16", 8000},
88 {{1, AST_FORMAT_LPC10}, "audio", "LPC", 8000},
89 {{1, AST_FORMAT_G729A}, "audio", "G729", 8000},
90 {{1, AST_FORMAT_G729A}, "audio", "G729A", 8000},
91 {{1, AST_FORMAT_G729A}, "audio", "G.729", 8000},
92 {{1, AST_FORMAT_SPEEX}, "audio", "speex", 8000},
93 {{1, AST_FORMAT_ILBC}, "audio", "iLBC", 8000},
94 /* this is the sample rate listed in the RTP profile for the G.722
95 codec, *NOT* the actual sample rate of the media stream
97 {{1, AST_FORMAT_G722}, "audio", "G722", 8000},
98 {{1, AST_FORMAT_G726_AAL2}, "audio", "AAL2-G726-32", 8000},
99 {{0, AST_RTP_DTMF}, "audio", "telephone-event", 8000},
100 {{0, AST_RTP_CISCO_DTMF}, "audio", "cisco-telephone-event", 8000},
101 {{0, AST_RTP_CN}, "audio", "CN", 8000},
102 {{1, AST_FORMAT_JPEG}, "video", "JPEG", 90000},
103 {{1, AST_FORMAT_PNG}, "video", "PNG", 90000},
104 {{1, AST_FORMAT_H261}, "video", "H261", 90000},
105 {{1, AST_FORMAT_H263}, "video", "H263", 90000},
106 {{1, AST_FORMAT_H263_PLUS}, "video", "h263-1998", 90000},
107 {{1, AST_FORMAT_H264}, "video", "H264", 90000},
108 {{1, AST_FORMAT_MP4_VIDEO}, "video", "MP4V-ES", 90000},
109 {{1, AST_FORMAT_T140RED}, "text", "RED", 1000},
110 {{1, AST_FORMAT_T140}, "text", "T140", 1000},
111 {{1, AST_FORMAT_SIREN7}, "audio", "G7221", 16000},
112 {{1, AST_FORMAT_SIREN14}, "audio", "G7221", 32000},
116 * \brief Mapping between Asterisk codecs and rtp payload types
118 * Static (i.e., well-known) RTP payload types for our "AST_FORMAT..."s:
119 * also, our own choices for dynamic payload types. This is our master
120 * table for transmission
122 * See http://www.iana.org/assignments/rtp-parameters for a list of
125 static const struct ast_rtp_payload_type static_RTP_PT[AST_RTP_MAX_PT] = {
126 [0] = {1, AST_FORMAT_ULAW},
127 #ifdef USE_DEPRECATED_G726
128 [2] = {1, AST_FORMAT_G726}, /* Technically this is G.721, but if Cisco can do it, so can we... */
130 [3] = {1, AST_FORMAT_GSM},
131 [4] = {1, AST_FORMAT_G723_1},
132 [5] = {1, AST_FORMAT_ADPCM}, /* 8 kHz */
133 [6] = {1, AST_FORMAT_ADPCM}, /* 16 kHz */
134 [7] = {1, AST_FORMAT_LPC10},
135 [8] = {1, AST_FORMAT_ALAW},
136 [9] = {1, AST_FORMAT_G722},
137 [10] = {1, AST_FORMAT_SLINEAR}, /* 2 channels */
138 [11] = {1, AST_FORMAT_SLINEAR}, /* 1 channel */
139 [13] = {0, AST_RTP_CN},
140 [16] = {1, AST_FORMAT_ADPCM}, /* 11.025 kHz */
141 [17] = {1, AST_FORMAT_ADPCM}, /* 22.050 kHz */
142 [18] = {1, AST_FORMAT_G729A},
143 [19] = {0, AST_RTP_CN}, /* Also used for CN */
144 [26] = {1, AST_FORMAT_JPEG},
145 [31] = {1, AST_FORMAT_H261},
146 [34] = {1, AST_FORMAT_H263},
147 [97] = {1, AST_FORMAT_ILBC},
148 [98] = {1, AST_FORMAT_H263_PLUS},
149 [99] = {1, AST_FORMAT_H264},
150 [101] = {0, AST_RTP_DTMF},
151 [102] = {1, AST_FORMAT_SIREN7},
152 [103] = {1, AST_FORMAT_H263_PLUS},
153 [104] = {1, AST_FORMAT_MP4_VIDEO},
154 [105] = {1, AST_FORMAT_T140RED}, /* Real time text chat (with redundancy encoding) */
155 [106] = {1, AST_FORMAT_T140}, /* Real time text chat */
156 [110] = {1, AST_FORMAT_SPEEX},
157 [111] = {1, AST_FORMAT_G726},
158 [112] = {1, AST_FORMAT_G726_AAL2},
159 [115] = {1, AST_FORMAT_SIREN14},
160 [121] = {0, AST_RTP_CISCO_DTMF}, /* Must be type 121 */
163 int ast_rtp_engine_register2(struct ast_rtp_engine *engine, struct ast_module *module)
165 struct ast_rtp_engine *current_engine;
167 /* Perform a sanity check on the engine structure to make sure it has the basics */
168 if (ast_strlen_zero(engine->name) || !engine->new || !engine->destroy || !engine->write || !engine->read) {
169 ast_log(LOG_WARNING, "RTP Engine '%s' failed sanity check so it was not registered.\n", !ast_strlen_zero(engine->name) ? engine->name : "Unknown");
173 /* Link owner module to the RTP engine for reference counting purposes */
174 engine->mod = module;
176 AST_RWLIST_WRLOCK(&engines);
178 /* Ensure that no two modules with the same name are registered at the same time */
179 AST_RWLIST_TRAVERSE(&engines, current_engine, entry) {
180 if (!strcmp(current_engine->name, engine->name)) {
181 ast_log(LOG_WARNING, "An RTP engine with the name '%s' has already been registered.\n", engine->name);
182 AST_RWLIST_UNLOCK(&engines);
187 /* The engine survived our critique. Off to the list it goes to be used */
188 AST_RWLIST_INSERT_TAIL(&engines, engine, entry);
190 AST_RWLIST_UNLOCK(&engines);
192 ast_verb(2, "Registered RTP engine '%s'\n", engine->name);
197 int ast_rtp_engine_unregister(struct ast_rtp_engine *engine)
199 struct ast_rtp_engine *current_engine = NULL;
201 AST_RWLIST_WRLOCK(&engines);
203 if ((current_engine = AST_RWLIST_REMOVE(&engines, engine, entry))) {
204 ast_verb(2, "Unregistered RTP engine '%s'\n", engine->name);
207 AST_RWLIST_UNLOCK(&engines);
209 return current_engine ? 0 : -1;
212 int ast_rtp_glue_register2(struct ast_rtp_glue *glue, struct ast_module *module)
214 struct ast_rtp_glue *current_glue = NULL;
216 if (ast_strlen_zero(glue->type)) {
222 AST_RWLIST_WRLOCK(&glues);
224 AST_RWLIST_TRAVERSE(&glues, current_glue, entry) {
225 if (!strcasecmp(current_glue->type, glue->type)) {
226 ast_log(LOG_WARNING, "RTP glue with the name '%s' has already been registered.\n", glue->type);
227 AST_RWLIST_UNLOCK(&glues);
232 AST_RWLIST_INSERT_TAIL(&glues, glue, entry);
234 AST_RWLIST_UNLOCK(&glues);
236 ast_verb(2, "Registered RTP glue '%s'\n", glue->type);
241 int ast_rtp_glue_unregister(struct ast_rtp_glue *glue)
243 struct ast_rtp_glue *current_glue = NULL;
245 AST_RWLIST_WRLOCK(&glues);
247 if ((current_glue = AST_RWLIST_REMOVE(&glues, glue, entry))) {
248 ast_verb(2, "Unregistered RTP glue '%s'\n", glue->type);
251 AST_RWLIST_UNLOCK(&glues);
253 return current_glue ? 0 : -1;
256 static void instance_destructor(void *obj)
258 struct ast_rtp_instance *instance = obj;
260 /* Pass us off to the engine to destroy */
261 if (instance->data && instance->engine->destroy(instance)) {
262 ast_debug(1, "Engine '%s' failed to destroy RTP instance '%p'\n", instance->engine->name, instance);
266 /* Drop our engine reference */
267 ast_module_unref(instance->engine->mod);
269 ast_debug(1, "Destroyed RTP instance '%p'\n", instance);
272 int ast_rtp_instance_destroy(struct ast_rtp_instance *instance)
274 ao2_ref(instance, -1);
279 struct ast_rtp_instance *ast_rtp_instance_new(const char *engine_name, struct sched_context *sched, struct sockaddr_in *sin, void *data)
281 struct ast_rtp_instance *instance = NULL;
282 struct ast_rtp_engine *engine = NULL;
284 AST_RWLIST_RDLOCK(&engines);
286 /* If an engine name was specified try to use it or otherwise use the first one registered */
287 if (!ast_strlen_zero(engine_name)) {
288 AST_RWLIST_TRAVERSE(&engines, engine, entry) {
289 if (!strcmp(engine->name, engine_name)) {
294 engine = AST_RWLIST_FIRST(&engines);
297 /* If no engine was actually found bail out now */
299 ast_log(LOG_ERROR, "No RTP engine was found. Do you have one loaded?\n");
300 AST_RWLIST_UNLOCK(&engines);
304 /* Bump up the reference count before we return so the module can not be unloaded */
305 ast_module_ref(engine->mod);
307 AST_RWLIST_UNLOCK(&engines);
309 /* Allocate a new RTP instance */
310 if (!(instance = ao2_alloc(sizeof(*instance), instance_destructor))) {
311 ast_module_unref(engine->mod);
314 instance->engine = engine;
315 instance->local_address.sin_family = AF_INET;
316 instance->local_address.sin_addr = sin->sin_addr;
317 instance->remote_address.sin_family = AF_INET;
319 ast_debug(1, "Using engine '%s' for RTP instance '%p'\n", engine->name, instance);
321 /* And pass it off to the engine to setup */
322 if (instance->engine->new(instance, sched, sin, data)) {
323 ast_debug(1, "Engine '%s' failed to setup RTP instance '%p'\n", engine->name, instance);
324 ao2_ref(instance, -1);
328 ast_debug(1, "RTP instance '%p' is setup and ready to go\n", instance);
333 void ast_rtp_instance_set_data(struct ast_rtp_instance *instance, void *data)
335 instance->data = data;
338 void *ast_rtp_instance_get_data(struct ast_rtp_instance *instance)
340 return instance->data;
343 int ast_rtp_instance_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
345 return instance->engine->write(instance, frame);
348 struct ast_frame *ast_rtp_instance_read(struct ast_rtp_instance *instance, int rtcp)
350 return instance->engine->read(instance, rtcp);
353 int ast_rtp_instance_set_local_address(struct ast_rtp_instance *instance, struct sockaddr_in *address)
355 instance->local_address.sin_addr = address->sin_addr;
356 instance->local_address.sin_port = address->sin_port;
360 int ast_rtp_instance_set_remote_address(struct ast_rtp_instance *instance, struct sockaddr_in *address)
362 if (&instance->remote_address != address) {
363 instance->remote_address.sin_addr = address->sin_addr;
364 instance->remote_address.sin_port = address->sin_port;
369 if (instance->engine->remote_address_set) {
370 instance->engine->remote_address_set(instance, &instance->remote_address);
376 int ast_rtp_instance_get_local_address(struct ast_rtp_instance *instance, struct sockaddr_in *address)
378 if ((address->sin_family != AF_INET) ||
379 (address->sin_port != instance->local_address.sin_port) ||
380 (address->sin_addr.s_addr != instance->local_address.sin_addr.s_addr)) {
381 memcpy(address, &instance->local_address, sizeof(*address));
388 int ast_rtp_instance_get_remote_address(struct ast_rtp_instance *instance, struct sockaddr_in *address)
390 if ((address->sin_family != AF_INET) ||
391 (address->sin_port != instance->remote_address.sin_port) ||
392 (address->sin_addr.s_addr != instance->remote_address.sin_addr.s_addr)) {
393 memcpy(address, &instance->remote_address, sizeof(*address));
400 void ast_rtp_instance_set_extended_prop(struct ast_rtp_instance *instance, int property, void *value)
402 if (instance->engine->extended_prop_set) {
403 instance->engine->extended_prop_set(instance, property, value);
407 void *ast_rtp_instance_get_extended_prop(struct ast_rtp_instance *instance, int property)
409 if (instance->engine->extended_prop_get) {
410 return instance->engine->extended_prop_get(instance, property);
416 void ast_rtp_instance_set_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value)
418 instance->properties[property] = value;
420 if (instance->engine->prop_set) {
421 instance->engine->prop_set(instance, property, value);
425 int ast_rtp_instance_get_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property)
427 return instance->properties[property];
430 struct ast_rtp_codecs *ast_rtp_instance_get_codecs(struct ast_rtp_instance *instance)
432 return &instance->codecs;
435 void ast_rtp_codecs_payloads_clear(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
439 for (i = 0; i < AST_RTP_MAX_PT; i++) {
440 ast_debug(2, "Clearing payload %d on %p\n", i, codecs);
441 codecs->payloads[i].asterisk_format = 0;
442 codecs->payloads[i].code = 0;
443 if (instance && instance->engine && instance->engine->payload_set) {
444 instance->engine->payload_set(instance, i, 0, 0);
449 void ast_rtp_codecs_payloads_default(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
453 for (i = 0; i < AST_RTP_MAX_PT; i++) {
454 if (static_RTP_PT[i].code) {
455 ast_debug(2, "Set default payload %d on %p\n", i, codecs);
456 codecs->payloads[i].asterisk_format = static_RTP_PT[i].asterisk_format;
457 codecs->payloads[i].code = static_RTP_PT[i].code;
458 if (instance && instance->engine && instance->engine->payload_set) {
459 instance->engine->payload_set(instance, i, codecs->payloads[i].asterisk_format, codecs->payloads[i].code);
465 void ast_rtp_codecs_payloads_copy(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
469 for (i = 0; i < AST_RTP_MAX_PT; i++) {
470 if (src->payloads[i].code) {
471 ast_debug(2, "Copying payload %d from %p to %p\n", i, src, dest);
472 dest->payloads[i].asterisk_format = src->payloads[i].asterisk_format;
473 dest->payloads[i].code = src->payloads[i].code;
474 if (instance && instance->engine && instance->engine->payload_set) {
475 instance->engine->payload_set(instance, i, dest->payloads[i].asterisk_format, dest->payloads[i].code);
481 void ast_rtp_codecs_payloads_set_m_type(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
483 if (payload < 0 || payload > AST_RTP_MAX_PT || !static_RTP_PT[payload].code) {
487 codecs->payloads[payload].asterisk_format = static_RTP_PT[payload].asterisk_format;
488 codecs->payloads[payload].code = static_RTP_PT[payload].code;
490 ast_debug(1, "Setting payload %d based on m type on %p\n", payload, codecs);
492 if (instance && instance->engine && instance->engine->payload_set) {
493 instance->engine->payload_set(instance, payload, codecs->payloads[payload].asterisk_format, codecs->payloads[payload].code);
497 int ast_rtp_codecs_payloads_set_rtpmap_type_rate(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int pt,
498 char *mimetype, char *mimesubtype,
499 enum ast_rtp_options options,
500 unsigned int sample_rate)
505 if (pt < 0 || pt > AST_RTP_MAX_PT)
506 return -1; /* bogus payload type */
508 for (i = 0; i < ARRAY_LEN(ast_rtp_mime_types); ++i) {
509 const struct ast_rtp_mime_type *t = &ast_rtp_mime_types[i];
511 if (strcasecmp(mimesubtype, t->subtype)) {
515 if (strcasecmp(mimetype, t->type)) {
519 /* if both sample rates have been supplied, and they don't match,
520 then this not a match; if one has not been supplied, then the
521 rates are not compared */
522 if (sample_rate && t->sample_rate &&
523 (sample_rate != t->sample_rate)) {
528 codecs->payloads[pt] = t->payload_type;
530 if ((t->payload_type.code == AST_FORMAT_G726) &&
531 t->payload_type.asterisk_format &&
532 (options & AST_RTP_OPT_G726_NONSTANDARD)) {
533 codecs->payloads[pt].code = AST_FORMAT_G726_AAL2;
536 if (instance && instance->engine && instance->engine->payload_set) {
537 instance->engine->payload_set(instance, pt, codecs->payloads[i].asterisk_format, codecs->payloads[i].code);
543 return (found ? 0 : -2);
546 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)
548 return ast_rtp_codecs_payloads_set_rtpmap_type_rate(codecs, instance, payload, mimetype, mimesubtype, options, 0);
551 void ast_rtp_codecs_payloads_unset(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
553 if (payload < 0 || payload > AST_RTP_MAX_PT) {
557 ast_debug(2, "Unsetting payload %d on %p\n", payload, codecs);
559 codecs->payloads[payload].asterisk_format = 0;
560 codecs->payloads[payload].code = 0;
562 if (instance && instance->engine && instance->engine->payload_set) {
563 instance->engine->payload_set(instance, payload, 0, 0);
567 struct ast_rtp_payload_type ast_rtp_codecs_payload_lookup(struct ast_rtp_codecs *codecs, int payload)
569 struct ast_rtp_payload_type result = { .asterisk_format = 0, };
571 if (payload < 0 || payload > AST_RTP_MAX_PT) {
575 result.asterisk_format = codecs->payloads[payload].asterisk_format;
576 result.code = codecs->payloads[payload].code;
579 result = static_RTP_PT[payload];
585 void ast_rtp_codecs_payload_formats(struct ast_rtp_codecs *codecs, int *astformats, int *nonastformats)
589 *astformats = *nonastformats = 0;
591 for (i = 0; i < AST_RTP_MAX_PT; i++) {
592 if (codecs->payloads[i].code) {
593 ast_debug(1, "Incorporating payload %d on %p\n", i, codecs);
595 if (codecs->payloads[i].asterisk_format) {
596 *astformats |= codecs->payloads[i].code;
598 *nonastformats |= codecs->payloads[i].code;
603 int ast_rtp_codecs_payload_code(struct ast_rtp_codecs *codecs, const int asterisk_format, const int code)
607 for (i = 0; i < AST_RTP_MAX_PT; i++) {
608 if (codecs->payloads[i].asterisk_format == asterisk_format && codecs->payloads[i].code == code) {
609 ast_debug(2, "Found code %d at payload %d on %p\n", code, i, codecs);
614 for (i = 0; i < AST_RTP_MAX_PT; i++) {
615 if (static_RTP_PT[i].asterisk_format == asterisk_format && static_RTP_PT[i].code == code) {
623 const char *ast_rtp_lookup_mime_subtype2(const int asterisk_format, const int code, enum ast_rtp_options options)
627 for (i = 0; i < ARRAY_LEN(ast_rtp_mime_types); i++) {
628 if (ast_rtp_mime_types[i].payload_type.code == code && ast_rtp_mime_types[i].payload_type.asterisk_format == asterisk_format) {
629 if (asterisk_format && (code == AST_FORMAT_G726_AAL2) && (options & AST_RTP_OPT_G726_NONSTANDARD)) {
632 return ast_rtp_mime_types[i].subtype;
640 unsigned int ast_rtp_lookup_sample_rate2(int asterisk_format, int code)
644 for (i = 0; i < ARRAY_LEN(ast_rtp_mime_types); ++i) {
645 if ((ast_rtp_mime_types[i].payload_type.code == code) && (ast_rtp_mime_types[i].payload_type.asterisk_format == asterisk_format)) {
646 return ast_rtp_mime_types[i].sample_rate;
653 char *ast_rtp_lookup_mime_multiple2(struct ast_str *buf, const int capability, const int asterisk_format, enum ast_rtp_options options)
655 int format, found = 0;
661 ast_str_append(&buf, 0, "0x%x (", capability);
663 for (format = 1; format < AST_RTP_MAX; format <<= 1) {
664 if (capability & format) {
665 const char *name = ast_rtp_lookup_mime_subtype2(asterisk_format, format, options);
666 ast_str_append(&buf, 0, "%s|", name);
671 ast_str_append(&buf, 0, "%s", found ? ")" : "nothing)");
673 return ast_str_buffer(buf);
676 void ast_rtp_codecs_packetization_set(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, struct ast_codec_pref *prefs)
678 codecs->pref = *prefs;
680 if (instance && instance->engine->packetization_set) {
681 instance->engine->packetization_set(instance, &instance->codecs.pref);
685 int ast_rtp_instance_dtmf_begin(struct ast_rtp_instance *instance, char digit)
687 return instance->engine->dtmf_begin ? instance->engine->dtmf_begin(instance, digit) : -1;
690 int ast_rtp_instance_dtmf_end(struct ast_rtp_instance *instance, char digit)
692 return instance->engine->dtmf_end ? instance->engine->dtmf_end(instance, digit) : -1;
695 int ast_rtp_instance_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode)
697 if (!instance->engine->dtmf_mode_set || instance->engine->dtmf_mode_set(instance, dtmf_mode)) {
701 instance->dtmf_mode = dtmf_mode;
706 enum ast_rtp_dtmf_mode ast_rtp_instance_dtmf_mode_get(struct ast_rtp_instance *instance)
708 return instance->dtmf_mode;
711 void ast_rtp_instance_new_source(struct ast_rtp_instance *instance)
713 if (instance->engine->new_source) {
714 instance->engine->new_source(instance);
718 int ast_rtp_instance_set_qos(struct ast_rtp_instance *instance, int tos, int cos, const char *desc)
720 return instance->engine->qos ? instance->engine->qos(instance, tos, cos, desc) : -1;
723 void ast_rtp_instance_stop(struct ast_rtp_instance *instance)
725 if (instance->engine->stop) {
726 instance->engine->stop(instance);
730 int ast_rtp_instance_fd(struct ast_rtp_instance *instance, int rtcp)
732 return instance->engine->fd ? instance->engine->fd(instance, rtcp) : -1;
735 struct ast_rtp_glue *ast_rtp_instance_get_glue(const char *type)
737 struct ast_rtp_glue *glue = NULL;
739 AST_RWLIST_RDLOCK(&glues);
741 AST_RWLIST_TRAVERSE(&glues, glue, entry) {
742 if (!strcasecmp(glue->type, type)) {
747 AST_RWLIST_UNLOCK(&glues);
752 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)
754 enum ast_bridge_result res = AST_BRIDGE_FAILED;
755 struct ast_channel *who = NULL, *other = NULL, *cs[3] = { NULL, };
756 struct ast_frame *fr = NULL;
758 /* Start locally bridging both instances */
759 if (instance0->engine->local_bridge && instance0->engine->local_bridge(instance0, instance1)) {
760 ast_debug(1, "Failed to locally bridge %s to %s, backing out.\n", c0->name, c1->name);
761 ast_channel_unlock(c0);
762 ast_channel_unlock(c1);
763 return AST_BRIDGE_FAILED_NOWARN;
765 if (instance1->engine->local_bridge && instance1->engine->local_bridge(instance1, instance0)) {
766 ast_debug(1, "Failed to locally bridge %s to %s, backing out.\n", c1->name, c0->name);
767 if (instance0->engine->local_bridge) {
768 instance0->engine->local_bridge(instance0, NULL);
770 ast_channel_unlock(c0);
771 ast_channel_unlock(c1);
772 return AST_BRIDGE_FAILED_NOWARN;
775 ast_channel_unlock(c0);
776 ast_channel_unlock(c1);
778 instance0->bridged = instance1;
779 instance1->bridged = instance0;
781 ast_poll_channel_add(c0, c1);
783 /* Hop into a loop waiting for a frame from either channel */
788 /* If the underlying formats have changed force this bridge to break */
789 if ((c0->rawreadformat != c1->rawwriteformat) || (c1->rawreadformat != c0->rawwriteformat)) {
790 ast_debug(1, "rtp-engine-local-bridge: Oooh, formats changed, backing out\n");
791 res = AST_BRIDGE_FAILED_NOWARN;
794 /* Check if anything changed */
795 if ((c0->tech_pvt != pvt0) ||
796 (c1->tech_pvt != pvt1) ||
797 (c0->masq || c0->masqr || c1->masq || c1->masqr) ||
798 (c0->monitor || c0->audiohooks || c1->monitor || c1->audiohooks)) {
799 ast_debug(1, "rtp-engine-local-bridge: Oooh, something is weird, backing out\n");
800 /* 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 */
801 if ((c0->masq || c0->masqr) && (fr = ast_read(c0))) {
804 if ((c1->masq || c1->masqr) && (fr = ast_read(c1))) {
807 res = AST_BRIDGE_RETRY;
810 /* Wait on a channel to feed us a frame */
811 if (!(who = ast_waitfor_n(cs, 2, &timeoutms))) {
813 res = AST_BRIDGE_RETRY;
816 ast_debug(2, "rtp-engine-local-bridge: Ooh, empty read...\n");
817 if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
822 /* Read in frame from channel */
824 other = (who == c0) ? c1 : c0;
825 /* Depending on the frame we may need to break out of our bridge */
826 if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
827 ((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) |
828 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1)))) {
829 /* Record received frame and who */
832 ast_debug(1, "rtp-engine-local-bridge: Ooh, got a %s\n", fr ? "digit" : "hangup");
833 res = AST_BRIDGE_COMPLETE;
835 } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
836 if ((fr->subclass == AST_CONTROL_HOLD) ||
837 (fr->subclass == AST_CONTROL_UNHOLD) ||
838 (fr->subclass == AST_CONTROL_VIDUPDATE) ||
839 (fr->subclass == AST_CONTROL_T38) ||
840 (fr->subclass == AST_CONTROL_SRCUPDATE)) {
841 /* If we are going on hold, then break callback mode and P2P bridging */
842 if (fr->subclass == AST_CONTROL_HOLD) {
843 if (instance0->engine->local_bridge) {
844 instance0->engine->local_bridge(instance0, NULL);
846 if (instance1->engine->local_bridge) {
847 instance1->engine->local_bridge(instance1, NULL);
849 instance0->bridged = NULL;
850 instance1->bridged = NULL;
851 } else if (fr->subclass == AST_CONTROL_UNHOLD) {
852 if (instance0->engine->local_bridge) {
853 instance0->engine->local_bridge(instance0, instance1);
855 if (instance1->engine->local_bridge) {
856 instance1->engine->local_bridge(instance1, instance0);
858 instance0->bridged = instance1;
859 instance1->bridged = instance0;
861 ast_indicate_data(other, fr->subclass, fr->data.ptr, fr->datalen);
866 ast_debug(1, "rtp-engine-local-bridge: Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass, who->name);
867 res = AST_BRIDGE_COMPLETE;
871 if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
872 (fr->frametype == AST_FRAME_DTMF_END) ||
873 (fr->frametype == AST_FRAME_VOICE) ||
874 (fr->frametype == AST_FRAME_VIDEO) ||
875 (fr->frametype == AST_FRAME_IMAGE) ||
876 (fr->frametype == AST_FRAME_HTML) ||
877 (fr->frametype == AST_FRAME_MODEM) ||
878 (fr->frametype == AST_FRAME_TEXT)) {
879 ast_write(other, fr);
890 /* Stop locally bridging both instances */
891 if (instance0->engine->local_bridge) {
892 instance0->engine->local_bridge(instance0, NULL);
894 if (instance1->engine->local_bridge) {
895 instance1->engine->local_bridge(instance1, NULL);
898 instance0->bridged = NULL;
899 instance1->bridged = NULL;
901 ast_poll_channel_del(c0, c1);
906 static enum ast_bridge_result remote_bridge_loop(struct ast_channel *c0, struct ast_channel *c1, struct ast_rtp_instance *instance0, struct ast_rtp_instance *instance1,
907 struct ast_rtp_instance *vinstance0, struct ast_rtp_instance *vinstance1, struct ast_rtp_instance *tinstance0,
908 struct ast_rtp_instance *tinstance1, struct ast_rtp_glue *glue0, struct ast_rtp_glue *glue1, int codec0, int codec1, int timeoutms,
909 int flags, struct ast_frame **fo, struct ast_channel **rc, void *pvt0, void *pvt1)
911 enum ast_bridge_result res = AST_BRIDGE_FAILED;
912 struct ast_channel *who = NULL, *other = NULL, *cs[3] = { NULL, };
913 int oldcodec0 = codec0, oldcodec1 = codec1;
914 struct sockaddr_in ac1 = {0,}, vac1 = {0,}, tac1 = {0,}, ac0 = {0,}, vac0 = {0,}, tac0 = {0,};
915 struct sockaddr_in t1 = {0,}, vt1 = {0,}, tt1 = {0,}, t0 = {0,}, vt0 = {0,}, tt0 = {0,};
916 struct ast_frame *fr = NULL;
918 /* Test the first channel */
919 if (!(glue0->update_peer(c0, instance1, vinstance1, tinstance1, codec1, 0))) {
920 ast_rtp_instance_get_remote_address(instance1, &ac1);
922 ast_rtp_instance_get_remote_address(vinstance1, &vac1);
925 ast_rtp_instance_get_remote_address(tinstance1, &tac1);
928 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
931 /* Test the second channel */
932 if (!(glue1->update_peer(c1, instance0, vinstance0, tinstance0, codec0, 0))) {
933 ast_rtp_instance_get_remote_address(instance0, &ac0);
935 ast_rtp_instance_get_remote_address(instance0, &vac0);
938 ast_rtp_instance_get_remote_address(instance0, &tac0);
941 ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c1->name, c0->name);
944 ast_channel_unlock(c0);
945 ast_channel_unlock(c1);
947 instance0->bridged = instance1;
948 instance1->bridged = instance0;
950 ast_poll_channel_add(c0, c1);
952 /* Go into a loop handling any stray frames that may come in */
957 /* Check if anything changed */
958 if ((c0->tech_pvt != pvt0) ||
959 (c1->tech_pvt != pvt1) ||
960 (c0->masq || c0->masqr || c1->masq || c1->masqr) ||
961 (c0->monitor || c0->audiohooks || c1->monitor || c1->audiohooks)) {
962 ast_debug(1, "Oooh, something is weird, backing out\n");
963 res = AST_BRIDGE_RETRY;
967 /* Check if they have changed their address */
968 ast_rtp_instance_get_remote_address(instance1, &t1);
970 ast_rtp_instance_get_remote_address(vinstance1, &vt1);
973 ast_rtp_instance_get_remote_address(tinstance1, &tt1);
975 if (glue1->get_codec) {
976 codec1 = glue1->get_codec(c1);
979 ast_rtp_instance_get_remote_address(instance0, &t0);
981 ast_rtp_instance_get_remote_address(vinstance0, &vt0);
984 ast_rtp_instance_get_remote_address(tinstance0, &tt0);
986 if (glue0->get_codec) {
987 codec0 = glue0->get_codec(c0);
990 if ((inaddrcmp(&t1, &ac1)) ||
991 (vinstance1 && inaddrcmp(&vt1, &vac1)) ||
992 (tinstance1 && inaddrcmp(&tt1, &tac1)) ||
993 (codec1 != oldcodec1)) {
994 ast_debug(1, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
995 c1->name, ast_inet_ntoa(t1.sin_addr), ntohs(t1.sin_port), codec1);
996 ast_debug(1, "Oooh, '%s' changed end vaddress to %s:%d (format %d)\n",
997 c1->name, ast_inet_ntoa(vt1.sin_addr), ntohs(vt1.sin_port), codec1);
998 ast_debug(1, "Oooh, '%s' changed end taddress to %s:%d (format %d)\n",
999 c1->name, ast_inet_ntoa(tt1.sin_addr), ntohs(tt1.sin_port), codec1);
1000 ast_debug(1, "Oooh, '%s' was %s:%d/(format %d)\n",
1001 c1->name, ast_inet_ntoa(ac1.sin_addr), ntohs(ac1.sin_port), oldcodec1);
1002 ast_debug(1, "Oooh, '%s' was %s:%d/(format %d)\n",
1003 c1->name, ast_inet_ntoa(vac1.sin_addr), ntohs(vac1.sin_port), oldcodec1);
1004 ast_debug(1, "Oooh, '%s' was %s:%d/(format %d)\n",
1005 c1->name, ast_inet_ntoa(tac1.sin_addr), ntohs(tac1.sin_port), oldcodec1);
1006 if (glue0->update_peer(c0, t1.sin_addr.s_addr ? instance1 : NULL, vt1.sin_addr.s_addr ? vinstance1 : NULL, tt1.sin_addr.s_addr ? tinstance1 : NULL, codec1, 0)) {
1007 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c0->name, c1->name);
1009 memcpy(&ac1, &t1, sizeof(ac1));
1010 memcpy(&vac1, &vt1, sizeof(vac1));
1011 memcpy(&tac1, &tt1, sizeof(tac1));
1014 if ((inaddrcmp(&t0, &ac0)) ||
1015 (vinstance0 && inaddrcmp(&vt0, &vac0)) ||
1016 (tinstance0 && inaddrcmp(&tt0, &tac0)) ||
1017 (codec0 != oldcodec0)) {
1018 ast_debug(1, "Oooh, '%s' changed end address to %s:%d (format %d)\n",
1019 c0->name, ast_inet_ntoa(t0.sin_addr), ntohs(t0.sin_port), codec0);
1020 ast_debug(1, "Oooh, '%s' was %s:%d/(format %d)\n",
1021 c0->name, ast_inet_ntoa(ac0.sin_addr), ntohs(ac0.sin_port), oldcodec0);
1022 if (glue1->update_peer(c1, t0.sin_addr.s_addr ? instance0 : NULL, vt0.sin_addr.s_addr ? vinstance0 : NULL, tt0.sin_addr.s_addr ? tinstance0 : NULL, codec0, 0)) {
1023 ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c1->name, c0->name);
1025 memcpy(&ac0, &t0, sizeof(ac0));
1026 memcpy(&vac0, &vt0, sizeof(vac0));
1027 memcpy(&tac0, &tt0, sizeof(tac0));
1031 /* Wait for frame to come in on the channels */
1032 if (!(who = ast_waitfor_n(cs, 2, &timeoutms))) {
1034 res = AST_BRIDGE_RETRY;
1037 ast_debug(1, "Ooh, empty read...\n");
1038 if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
1044 other = (who == c0) ? c1 : c0;
1045 if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
1046 (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
1047 ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))))) {
1048 /* Break out of bridge */
1051 ast_debug(1, "Oooh, got a %s\n", fr ? "digit" : "hangup");
1052 res = AST_BRIDGE_COMPLETE;
1054 } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
1055 if ((fr->subclass == AST_CONTROL_HOLD) ||
1056 (fr->subclass == AST_CONTROL_UNHOLD) ||
1057 (fr->subclass == AST_CONTROL_VIDUPDATE) ||
1058 (fr->subclass == AST_CONTROL_T38) ||
1059 (fr->subclass == AST_CONTROL_SRCUPDATE)) {
1060 if (fr->subclass == AST_CONTROL_HOLD) {
1061 /* If we someone went on hold we want the other side to reinvite back to us */
1063 glue1->update_peer(c1, NULL, NULL, NULL, 0, 0);
1065 glue0->update_peer(c0, NULL, NULL, NULL, 0, 0);
1067 } else if (fr->subclass == AST_CONTROL_UNHOLD) {
1068 /* If they went off hold they should go back to being direct */
1070 glue1->update_peer(c1, instance0, vinstance0, tinstance0, codec0, 0);
1072 glue0->update_peer(c0, instance1, vinstance1, tinstance1, codec1, 0);
1075 /* Update local address information */
1076 ast_rtp_instance_get_remote_address(instance0, &t0);
1077 memcpy(&ac0, &t0, sizeof(ac0));
1078 ast_rtp_instance_get_remote_address(instance1, &t1);
1079 memcpy(&ac1, &t1, sizeof(ac1));
1080 /* Update codec information */
1081 if (glue0->get_codec && c0->tech_pvt) {
1082 oldcodec0 = codec0 = glue0->get_codec(c0);
1084 if (glue1->get_codec && c1->tech_pvt) {
1085 oldcodec1 = codec1 = glue1->get_codec(c1);
1087 ast_indicate_data(other, fr->subclass, fr->data.ptr, fr->datalen);
1092 ast_debug(1, "Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass, who->name);
1093 return AST_BRIDGE_COMPLETE;
1096 if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
1097 (fr->frametype == AST_FRAME_DTMF_END) ||
1098 (fr->frametype == AST_FRAME_VOICE) ||
1099 (fr->frametype == AST_FRAME_VIDEO) ||
1100 (fr->frametype == AST_FRAME_IMAGE) ||
1101 (fr->frametype == AST_FRAME_HTML) ||
1102 (fr->frametype == AST_FRAME_MODEM) ||
1103 (fr->frametype == AST_FRAME_TEXT)) {
1104 ast_write(other, fr);
1114 if (glue0->update_peer(c0, NULL, NULL, NULL, 0, 0)) {
1115 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c0->name);
1117 if (glue1->update_peer(c1, NULL, NULL, NULL, 0, 0)) {
1118 ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c1->name);
1121 instance0->bridged = NULL;
1122 instance1->bridged = NULL;
1124 ast_poll_channel_del(c0, c1);
1130 * \brief Conditionally unref an rtp instance
1132 static void unref_instance_cond(struct ast_rtp_instance **instance)
1135 ao2_ref(*instance, -1);
1140 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)
1142 struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
1143 *vinstance0 = NULL, *vinstance1 = NULL,
1144 *tinstance0 = NULL, *tinstance1 = NULL;
1145 struct ast_rtp_glue *glue0, *glue1;
1146 enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID, text_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1147 enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID, text_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1148 enum ast_bridge_result res = AST_BRIDGE_FAILED;
1149 int codec0 = 0, codec1 = 0;
1150 int unlock_chans = 1;
1152 /* Lock both channels so we can look for the glue that binds them together */
1153 ast_channel_lock(c0);
1154 while (ast_channel_trylock(c1)) {
1155 ast_channel_unlock(c0);
1157 ast_channel_lock(c0);
1160 /* Ensure neither channel got hungup during lock avoidance */
1161 if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
1162 ast_log(LOG_WARNING, "Got hangup while attempting to bridge '%s' and '%s'\n", c0->name, c1->name);
1166 /* Grab glue that binds each channel to something using the RTP engine */
1167 if (!(glue0 = ast_rtp_instance_get_glue(c0->tech->type)) || !(glue1 = ast_rtp_instance_get_glue(c1->tech->type))) {
1168 ast_debug(1, "Can't find native functions for channel '%s'\n", glue0 ? c1->name : c0->name);
1172 audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
1173 video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1174 text_glue0_res = glue0->get_trtp_info ? glue0->get_trtp_info(c0, &tinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1176 audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
1177 video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1178 text_glue1_res = glue1->get_trtp_info ? glue1->get_trtp_info(c1, &tinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1180 /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
1181 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)) {
1182 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1184 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)) {
1185 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1188 /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
1189 if (audio_glue0_res == AST_RTP_GLUE_RESULT_FORBID || audio_glue1_res == AST_RTP_GLUE_RESULT_FORBID) {
1190 res = AST_BRIDGE_FAILED_NOWARN;
1194 /* If we need to get DTMF see if we can do it outside of the RTP stream itself */
1195 if ((flags & AST_BRIDGE_DTMF_CHANNEL_0) && instance0->properties[AST_RTP_PROPERTY_DTMF]) {
1196 res = AST_BRIDGE_FAILED_NOWARN;
1199 if ((flags & AST_BRIDGE_DTMF_CHANNEL_1) && instance1->properties[AST_RTP_PROPERTY_DTMF]) {
1200 res = AST_BRIDGE_FAILED_NOWARN;
1204 /* 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 */
1205 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)))) {
1206 res = AST_BRIDGE_FAILED_NOWARN;
1210 /* Make sure that codecs match */
1211 codec0 = glue0->get_codec ? glue0->get_codec(c0) : 0;
1212 codec1 = glue1->get_codec ? glue1->get_codec(c1) : 0;
1213 if (codec0 && codec1 && !(codec0 & codec1)) {
1214 ast_debug(1, "Channel codec0 = %d is not codec1 = %d, cannot native bridge in RTP.\n", codec0, codec1);
1215 res = AST_BRIDGE_FAILED_NOWARN;
1219 /* Depending on the end result for bridging either do a local bridge or remote bridge */
1220 if (audio_glue0_res == AST_RTP_GLUE_RESULT_LOCAL || audio_glue1_res == AST_RTP_GLUE_RESULT_LOCAL) {
1221 ast_verbose(VERBOSE_PREFIX_3 "Locally bridging %s and %s\n", c0->name, c1->name);
1222 res = local_bridge_loop(c0, c1, instance0, instance1, timeoutms, flags, fo, rc, c0->tech_pvt, c1->tech_pvt);
1224 ast_verbose(VERBOSE_PREFIX_3 "Remotely bridging %s and %s\n", c0->name, c1->name);
1225 res = remote_bridge_loop(c0, c1, instance0, instance1, vinstance0, vinstance1,
1226 tinstance0, tinstance1, glue0, glue1, codec0, codec1, timeoutms, flags,
1227 fo, rc, c0->tech_pvt, c1->tech_pvt);
1234 ast_channel_unlock(c0);
1235 ast_channel_unlock(c1);
1238 unref_instance_cond(&instance0);
1239 unref_instance_cond(&instance1);
1240 unref_instance_cond(&vinstance0);
1241 unref_instance_cond(&vinstance1);
1242 unref_instance_cond(&tinstance0);
1243 unref_instance_cond(&tinstance1);
1248 struct ast_rtp_instance *ast_rtp_instance_get_bridged(struct ast_rtp_instance *instance)
1250 return instance->bridged;
1253 void ast_rtp_instance_early_bridge_make_compatible(struct ast_channel *c0, struct ast_channel *c1)
1255 struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
1256 *vinstance0 = NULL, *vinstance1 = NULL,
1257 *tinstance0 = NULL, *tinstance1 = NULL;
1258 struct ast_rtp_glue *glue0, *glue1;
1259 enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID, text_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1260 enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID, text_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1261 int codec0 = 0, codec1 = 0;
1264 /* Lock both channels so we can look for the glue that binds them together */
1265 ast_channel_lock(c0);
1266 while (ast_channel_trylock(c1)) {
1267 ast_channel_unlock(c0);
1269 ast_channel_lock(c0);
1272 /* Grab glue that binds each channel to something using the RTP engine */
1273 if (!(glue0 = ast_rtp_instance_get_glue(c0->tech->type)) || !(glue1 = ast_rtp_instance_get_glue(c1->tech->type))) {
1274 ast_debug(1, "Can't find native functions for channel '%s'\n", glue0 ? c1->name : c0->name);
1278 audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
1279 video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1280 text_glue0_res = glue0->get_trtp_info ? glue0->get_trtp_info(c0, &tinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1282 audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
1283 video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1284 text_glue1_res = glue1->get_trtp_info ? glue1->get_trtp_info(c1, &tinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1286 /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
1287 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)) {
1288 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1290 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)) {
1291 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1293 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(c0)) {
1294 codec0 = glue0->get_codec(c0);
1296 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(c1)) {
1297 codec1 = glue1->get_codec(c1);
1300 /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
1301 if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
1305 /* Make sure we have matching codecs */
1306 if (!(codec0 & codec1)) {
1310 ast_rtp_codecs_payloads_copy(&instance0->codecs, &instance1->codecs, instance1);
1312 if (vinstance0 && vinstance1) {
1313 ast_rtp_codecs_payloads_copy(&vinstance0->codecs, &vinstance1->codecs, vinstance1);
1315 if (tinstance0 && tinstance1) {
1316 ast_rtp_codecs_payloads_copy(&tinstance0->codecs, &tinstance1->codecs, tinstance1);
1322 ast_channel_unlock(c0);
1323 ast_channel_unlock(c1);
1325 unref_instance_cond(&instance0);
1326 unref_instance_cond(&instance1);
1327 unref_instance_cond(&vinstance0);
1328 unref_instance_cond(&vinstance1);
1329 unref_instance_cond(&tinstance0);
1330 unref_instance_cond(&tinstance1);
1333 ast_debug(1, "Seeded SDP of '%s' with that of '%s'\n", c0->name, c1 ? c1->name : "<unspecified>");
1337 int ast_rtp_instance_early_bridge(struct ast_channel *c0, struct ast_channel *c1)
1339 struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
1340 *vinstance0 = NULL, *vinstance1 = NULL,
1341 *tinstance0 = NULL, *tinstance1 = NULL;
1342 struct ast_rtp_glue *glue0, *glue1;
1343 enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID, text_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1344 enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID, text_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1345 int codec0 = 0, codec1 = 0;
1348 /* If there is no second channel just immediately bail out, we are of no use in that scenario */
1353 /* Lock both channels so we can look for the glue that binds them together */
1354 ast_channel_lock(c0);
1355 while (ast_channel_trylock(c1)) {
1356 ast_channel_unlock(c0);
1358 ast_channel_lock(c0);
1361 /* Grab glue that binds each channel to something using the RTP engine */
1362 if (!(glue0 = ast_rtp_instance_get_glue(c0->tech->type)) || !(glue1 = ast_rtp_instance_get_glue(c1->tech->type))) {
1363 ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", glue0 ? c1->name : c0->name);
1367 audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
1368 video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1369 text_glue0_res = glue0->get_trtp_info ? glue0->get_trtp_info(c0, &tinstance0) : AST_RTP_GLUE_RESULT_FORBID;
1371 audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
1372 video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1373 text_glue1_res = glue1->get_trtp_info ? glue1->get_trtp_info(c1, &tinstance1) : AST_RTP_GLUE_RESULT_FORBID;
1375 /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
1376 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)) {
1377 audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
1379 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)) {
1380 audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
1382 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(c0)) {
1383 codec0 = glue0->get_codec(c0);
1385 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(c1)) {
1386 codec1 = glue1->get_codec(c1);
1389 /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
1390 if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
1394 /* Make sure we have matching codecs */
1395 if (!(codec0 & codec1)) {
1399 /* Bridge media early */
1400 if (glue0->update_peer(c0, instance1, vinstance1, tinstance1, codec1, 0)) {
1401 ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", c0->name, c1 ? c1->name : "<unspecified>");
1407 ast_channel_unlock(c0);
1408 ast_channel_unlock(c1);
1410 unref_instance_cond(&instance0);
1411 unref_instance_cond(&instance1);
1412 unref_instance_cond(&vinstance0);
1413 unref_instance_cond(&vinstance1);
1414 unref_instance_cond(&tinstance0);
1415 unref_instance_cond(&tinstance1);
1418 ast_debug(1, "Setting early bridge SDP of '%s' with that of '%s'\n", c0->name, c1 ? c1->name : "<unspecified>");
1424 int ast_rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations)
1426 return instance->engine->red_init ? instance->engine->red_init(instance, buffer_time, payloads, generations) : -1;
1429 int ast_rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame)
1431 return instance->engine->red_buffer ? instance->engine->red_buffer(instance, frame) : -1;
1434 int ast_rtp_instance_get_stats(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat)
1436 return instance->engine->get_stat ? instance->engine->get_stat(instance, stats, stat) : -1;
1439 char *ast_rtp_instance_get_quality(struct ast_rtp_instance *instance, enum ast_rtp_instance_stat_field field, char *buf, size_t size)
1441 struct ast_rtp_instance_stats stats = { 0, };
1442 enum ast_rtp_instance_stat stat;
1444 /* Determine what statistics we will need to retrieve based on field passed in */
1445 if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
1446 stat = AST_RTP_INSTANCE_STAT_ALL;
1447 } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
1448 stat = AST_RTP_INSTANCE_STAT_COMBINED_JITTER;
1449 } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
1450 stat = AST_RTP_INSTANCE_STAT_COMBINED_LOSS;
1451 } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
1452 stat = AST_RTP_INSTANCE_STAT_COMBINED_RTT;
1457 /* Attempt to actually retrieve the statistics we need to generate the quality string */
1458 if (ast_rtp_instance_get_stats(instance, &stats, stat)) {
1462 /* Now actually fill the buffer with the good information */
1463 if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
1464 snprintf(buf, size, "ssrc=%i;themssrc=%u;lp=%u;rxjitter=%u;rxcount=%u;txjitter=%u;txcount=%u;rlp=%u;rtt=%u",
1465 stats.local_ssrc, stats.remote_ssrc, stats.rxploss, stats.txjitter, stats.rxcount, stats.rxjitter, stats.txcount, stats.txploss, stats.rtt);
1466 } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
1467 snprintf(buf, size, "minrxjitter=%f;maxrxjitter=%f;avgrxjitter=%f;stdevrxjitter=%f;reported_minjitter=%f;reported_maxjitter=%f;reported_avgjitter=%f;reported_stdevjitter=%f;",
1468 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));
1469 } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
1470 snprintf(buf, size, "minrxlost=%f;maxrxlost=%f;avgrxlost=%f;stdevrxlost=%f;reported_minlost=%f;reported_maxlost=%f;reported_avglost=%f;reported_stdevlost=%f;",
1471 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));
1472 } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
1473 snprintf(buf, size, "minrtt=%f;maxrtt=%f;avgrtt=%f;stdevrtt=%f;", stats.minrtt, stats.maxrtt, stats.normdevrtt, stats.stdevrtt);
1479 void ast_rtp_instance_set_stats_vars(struct ast_channel *chan, struct ast_rtp_instance *instance)
1481 char quality_buf[AST_MAX_USER_FIELD], *quality;
1482 struct ast_channel *bridge = ast_bridged_channel(chan);
1484 if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
1485 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOS", quality);
1487 pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSBRIDGED", quality);
1491 if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER, quality_buf, sizeof(quality_buf)))) {
1492 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSJITTER", quality);
1494 pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSJITTERBRIDGED", quality);
1498 if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS, quality_buf, sizeof(quality_buf)))) {
1499 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSLOSS", quality);
1501 pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSLOSSBRIDGED", quality);
1505 if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT, quality_buf, sizeof(quality_buf)))) {
1506 pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSRTT", quality);
1508 pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSRTTBRIDGED", quality);
1513 int ast_rtp_instance_set_read_format(struct ast_rtp_instance *instance, int format)
1515 return instance->engine->set_read_format ? instance->engine->set_read_format(instance, format) : -1;
1518 int ast_rtp_instance_set_write_format(struct ast_rtp_instance *instance, int format)
1520 return instance->engine->set_write_format ? instance->engine->set_write_format(instance, format) : -1;
1523 int ast_rtp_instance_make_compatible(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_channel *peer)
1525 struct ast_rtp_glue *glue;
1526 struct ast_rtp_instance *peer_instance = NULL;
1529 if (!instance->engine->make_compatible) {
1533 ast_channel_lock(peer);
1535 if (!(glue = ast_rtp_instance_get_glue(peer->tech->type))) {
1536 ast_channel_unlock(peer);
1540 glue->get_rtp_info(peer, &peer_instance);
1542 if (!peer_instance || peer_instance->engine != instance->engine) {
1543 ast_channel_unlock(peer);
1544 ao2_ref(peer_instance, -1);
1545 peer_instance = NULL;
1549 res = instance->engine->make_compatible(chan, instance, peer, peer_instance);
1551 ast_channel_unlock(peer);
1553 ao2_ref(peer_instance, -1);
1554 peer_instance = NULL;
1559 int ast_rtp_instance_activate(struct ast_rtp_instance *instance)
1561 return instance->engine->activate ? instance->engine->activate(instance) : 0;
1564 void ast_rtp_instance_stun_request(struct ast_rtp_instance *instance, struct sockaddr_in *suggestion, const char *username)
1566 if (instance->engine->stun_request) {
1567 instance->engine->stun_request(instance, suggestion, username);
1571 void ast_rtp_instance_set_timeout(struct ast_rtp_instance *instance, int timeout)
1573 instance->timeout = timeout;
1576 void ast_rtp_instance_set_hold_timeout(struct ast_rtp_instance *instance, int timeout)
1578 instance->holdtimeout = timeout;
1581 int ast_rtp_instance_get_timeout(struct ast_rtp_instance *instance)
1583 return instance->timeout;
1586 int ast_rtp_instance_get_hold_timeout(struct ast_rtp_instance *instance)
1588 return instance->holdtimeout;