Sat Apr 26 2014 22:01:41

Asterisk developer's documentation


rtp_engine.c
Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2008, Digium, Inc.
00005  *
00006  * Joshua Colp <jcolp@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  *
00021  * \brief Pluggable RTP Architecture
00022  *
00023  * \author Joshua Colp <jcolp@digium.com>
00024  */
00025 
00026 /*** MODULEINFO
00027    <support_level>core</support_level>
00028  ***/
00029 
00030 #include "asterisk.h"
00031 
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 409524 $")
00033 
00034 #include <math.h>
00035 
00036 #include "asterisk/channel.h"
00037 #include "asterisk/frame.h"
00038 #include "asterisk/module.h"
00039 #include "asterisk/rtp_engine.h"
00040 #include "asterisk/manager.h"
00041 #include "asterisk/options.h"
00042 #include "asterisk/astobj2.h"
00043 #include "asterisk/pbx.h"
00044 #include "asterisk/translate.h"
00045 #include "asterisk/netsock2.h"
00046 #include "asterisk/_private.h"
00047 #include "asterisk/framehook.h"
00048 
00049 struct ast_srtp_res *res_srtp = NULL;
00050 struct ast_srtp_policy_res *res_srtp_policy = NULL;
00051 
00052 /*! Structure that represents an RTP session (instance) */
00053 struct ast_rtp_instance {
00054    /*! Engine that is handling this RTP instance */
00055    struct ast_rtp_engine *engine;
00056    /*! Data unique to the RTP engine */
00057    void *data;
00058    /*! RTP properties that have been set and their value */
00059    int properties[AST_RTP_PROPERTY_MAX];
00060    /*! Address that we are expecting RTP to come in to */
00061    struct ast_sockaddr local_address;
00062    /*! Address that we are sending RTP to */
00063    struct ast_sockaddr remote_address;
00064    /*! Alternate address that we are receiving RTP from */
00065    struct ast_sockaddr alt_remote_address;
00066    /*! Instance that we are bridged to if doing remote or local bridging */
00067    struct ast_rtp_instance *bridged;
00068    /*! Payload and packetization information */
00069    struct ast_rtp_codecs codecs;
00070    /*! RTP timeout time (negative or zero means disabled, negative value means temporarily disabled) */
00071    int timeout;
00072    /*! RTP timeout when on hold (negative or zero means disabled, negative value means temporarily disabled). */
00073    int holdtimeout;
00074    /*! RTP keepalive interval */
00075    int keepalive;
00076    /*! Glue currently in use */
00077    struct ast_rtp_glue *glue;
00078    /*! Channel associated with the instance */
00079    struct ast_channel *chan;
00080    /*! SRTP info associated with the instance */
00081    struct ast_srtp *srtp;
00082 };
00083 
00084 /*! List of RTP engines that are currently registered */
00085 static AST_RWLIST_HEAD_STATIC(engines, ast_rtp_engine);
00086 
00087 /*! List of RTP glues */
00088 static AST_RWLIST_HEAD_STATIC(glues, ast_rtp_glue);
00089 
00090 /*! The following array defines the MIME Media type (and subtype) for each
00091    of our codecs, or RTP-specific data type. */
00092 static struct ast_rtp_mime_type {
00093    struct ast_rtp_payload_type payload_type;
00094    char *type;
00095    char *subtype;
00096    unsigned int sample_rate;
00097 } ast_rtp_mime_types[128]; /* This will Likely not need to grow any time soon. */
00098 static ast_rwlock_t mime_types_lock;
00099 static int mime_types_len = 0;
00100 
00101 /*!
00102  * \brief Mapping between Asterisk codecs and rtp payload types
00103  *
00104  * Static (i.e., well-known) RTP payload types for our "AST_FORMAT..."s:
00105  * also, our own choices for dynamic payload types.  This is our master
00106  * table for transmission
00107  *
00108  * See http://www.iana.org/assignments/rtp-parameters for a list of
00109  * assigned values
00110  */
00111 static struct ast_rtp_payload_type static_RTP_PT[AST_RTP_MAX_PT];
00112 static ast_rwlock_t static_RTP_PT_lock;
00113 
00114 int ast_rtp_engine_register2(struct ast_rtp_engine *engine, struct ast_module *module)
00115 {
00116    struct ast_rtp_engine *current_engine;
00117 
00118    /* Perform a sanity check on the engine structure to make sure it has the basics */
00119    if (ast_strlen_zero(engine->name) || !engine->new || !engine->destroy || !engine->write || !engine->read) {
00120       ast_log(LOG_WARNING, "RTP Engine '%s' failed sanity check so it was not registered.\n", !ast_strlen_zero(engine->name) ? engine->name : "Unknown");
00121       return -1;
00122    }
00123 
00124    /* Link owner module to the RTP engine for reference counting purposes */
00125    engine->mod = module;
00126 
00127    AST_RWLIST_WRLOCK(&engines);
00128 
00129    /* Ensure that no two modules with the same name are registered at the same time */
00130    AST_RWLIST_TRAVERSE(&engines, current_engine, entry) {
00131       if (!strcmp(current_engine->name, engine->name)) {
00132          ast_log(LOG_WARNING, "An RTP engine with the name '%s' has already been registered.\n", engine->name);
00133          AST_RWLIST_UNLOCK(&engines);
00134          return -1;
00135       }
00136    }
00137 
00138    /* The engine survived our critique. Off to the list it goes to be used */
00139    AST_RWLIST_INSERT_TAIL(&engines, engine, entry);
00140 
00141    AST_RWLIST_UNLOCK(&engines);
00142 
00143    ast_verb(2, "Registered RTP engine '%s'\n", engine->name);
00144 
00145    return 0;
00146 }
00147 
00148 int ast_rtp_engine_unregister(struct ast_rtp_engine *engine)
00149 {
00150    struct ast_rtp_engine *current_engine = NULL;
00151 
00152    AST_RWLIST_WRLOCK(&engines);
00153 
00154    if ((current_engine = AST_RWLIST_REMOVE(&engines, engine, entry))) {
00155       ast_verb(2, "Unregistered RTP engine '%s'\n", engine->name);
00156    }
00157 
00158    AST_RWLIST_UNLOCK(&engines);
00159 
00160    return current_engine ? 0 : -1;
00161 }
00162 
00163 int ast_rtp_glue_register2(struct ast_rtp_glue *glue, struct ast_module *module)
00164 {
00165    struct ast_rtp_glue *current_glue = NULL;
00166 
00167    if (ast_strlen_zero(glue->type)) {
00168       return -1;
00169    }
00170 
00171    glue->mod = module;
00172 
00173    AST_RWLIST_WRLOCK(&glues);
00174 
00175    AST_RWLIST_TRAVERSE(&glues, current_glue, entry) {
00176       if (!strcasecmp(current_glue->type, glue->type)) {
00177          ast_log(LOG_WARNING, "RTP glue with the name '%s' has already been registered.\n", glue->type);
00178          AST_RWLIST_UNLOCK(&glues);
00179          return -1;
00180       }
00181    }
00182 
00183    AST_RWLIST_INSERT_TAIL(&glues, glue, entry);
00184 
00185    AST_RWLIST_UNLOCK(&glues);
00186 
00187    ast_verb(2, "Registered RTP glue '%s'\n", glue->type);
00188 
00189    return 0;
00190 }
00191 
00192 int ast_rtp_glue_unregister(struct ast_rtp_glue *glue)
00193 {
00194    struct ast_rtp_glue *current_glue = NULL;
00195 
00196    AST_RWLIST_WRLOCK(&glues);
00197 
00198    if ((current_glue = AST_RWLIST_REMOVE(&glues, glue, entry))) {
00199       ast_verb(2, "Unregistered RTP glue '%s'\n", glue->type);
00200    }
00201 
00202    AST_RWLIST_UNLOCK(&glues);
00203 
00204    return current_glue ? 0 : -1;
00205 }
00206 
00207 static void instance_destructor(void *obj)
00208 {
00209    struct ast_rtp_instance *instance = obj;
00210 
00211    /* Pass us off to the engine to destroy */
00212    if (instance->data && instance->engine->destroy(instance)) {
00213       ast_debug(1, "Engine '%s' failed to destroy RTP instance '%p'\n", instance->engine->name, instance);
00214       return;
00215    }
00216 
00217    if (instance->srtp) {
00218       res_srtp->destroy(instance->srtp);
00219    }
00220 
00221    ast_rtp_codecs_payloads_destroy(&instance->codecs);
00222 
00223    /* Drop our engine reference */
00224    ast_module_unref(instance->engine->mod);
00225 
00226    ast_debug(1, "Destroyed RTP instance '%p'\n", instance);
00227 }
00228 
00229 int ast_rtp_instance_destroy(struct ast_rtp_instance *instance)
00230 {
00231    ao2_ref(instance, -1);
00232 
00233    return 0;
00234 }
00235 
00236 struct ast_rtp_instance *ast_rtp_instance_new(const char *engine_name,
00237       struct ast_sched_context *sched, const struct ast_sockaddr *sa,
00238       void *data)
00239 {
00240    struct ast_sockaddr address = {{0,}};
00241    struct ast_rtp_instance *instance = NULL;
00242    struct ast_rtp_engine *engine = NULL;
00243 
00244    AST_RWLIST_RDLOCK(&engines);
00245 
00246    /* If an engine name was specified try to use it or otherwise use the first one registered */
00247    if (!ast_strlen_zero(engine_name)) {
00248       AST_RWLIST_TRAVERSE(&engines, engine, entry) {
00249          if (!strcmp(engine->name, engine_name)) {
00250             break;
00251          }
00252       }
00253    } else {
00254       engine = AST_RWLIST_FIRST(&engines);
00255    }
00256 
00257    /* If no engine was actually found bail out now */
00258    if (!engine) {
00259       ast_log(LOG_ERROR, "No RTP engine was found. Do you have one loaded?\n");
00260       AST_RWLIST_UNLOCK(&engines);
00261       return NULL;
00262    }
00263 
00264    /* Bump up the reference count before we return so the module can not be unloaded */
00265    ast_module_ref(engine->mod);
00266 
00267    AST_RWLIST_UNLOCK(&engines);
00268 
00269    /* Allocate a new RTP instance */
00270    if (!(instance = ao2_alloc(sizeof(*instance), instance_destructor))) {
00271       ast_module_unref(engine->mod);
00272       return NULL;
00273    }
00274    instance->engine = engine;
00275    ast_sockaddr_copy(&instance->local_address, sa);
00276    ast_sockaddr_copy(&address, sa);
00277 
00278    if (ast_rtp_codecs_payloads_initialize(&instance->codecs)) {
00279       ao2_ref(instance, -1);
00280       return NULL;
00281    }
00282 
00283    ast_debug(1, "Using engine '%s' for RTP instance '%p'\n", engine->name, instance);
00284 
00285    /* And pass it off to the engine to setup */
00286    if (instance->engine->new(instance, sched, &address, data)) {
00287       ast_debug(1, "Engine '%s' failed to setup RTP instance '%p'\n", engine->name, instance);
00288       ao2_ref(instance, -1);
00289       return NULL;
00290    }
00291 
00292    ast_debug(1, "RTP instance '%p' is setup and ready to go\n", instance);
00293 
00294    return instance;
00295 }
00296 
00297 void ast_rtp_instance_set_data(struct ast_rtp_instance *instance, void *data)
00298 {
00299    instance->data = data;
00300 }
00301 
00302 void *ast_rtp_instance_get_data(struct ast_rtp_instance *instance)
00303 {
00304    return instance->data;
00305 }
00306 
00307 int ast_rtp_instance_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
00308 {
00309    return instance->engine->write(instance, frame);
00310 }
00311 
00312 struct ast_frame *ast_rtp_instance_read(struct ast_rtp_instance *instance, int rtcp)
00313 {
00314    return instance->engine->read(instance, rtcp);
00315 }
00316 
00317 int ast_rtp_instance_set_local_address(struct ast_rtp_instance *instance,
00318       const struct ast_sockaddr *address)
00319 {
00320    ast_sockaddr_copy(&instance->local_address, address);
00321    return 0;
00322 }
00323 
00324 int ast_rtp_instance_set_remote_address(struct ast_rtp_instance *instance,
00325       const struct ast_sockaddr *address)
00326 {
00327    ast_sockaddr_copy(&instance->remote_address, address);
00328 
00329    /* moo */
00330 
00331    if (instance->engine->remote_address_set) {
00332       instance->engine->remote_address_set(instance, &instance->remote_address);
00333    }
00334 
00335    return 0;
00336 }
00337 
00338 int ast_rtp_instance_set_alt_remote_address(struct ast_rtp_instance *instance,
00339       const struct ast_sockaddr *address)
00340 {
00341    ast_sockaddr_copy(&instance->alt_remote_address, address);
00342 
00343    /* oink */
00344 
00345    if (instance->engine->alt_remote_address_set) {
00346       instance->engine->alt_remote_address_set(instance, &instance->alt_remote_address);
00347    }
00348 
00349    return 0;
00350 }
00351 
00352 int ast_rtp_instance_get_and_cmp_local_address(struct ast_rtp_instance *instance,
00353       struct ast_sockaddr *address)
00354 {
00355    if (ast_sockaddr_cmp(address, &instance->local_address) != 0) {
00356       ast_sockaddr_copy(address, &instance->local_address);
00357       return 1;
00358    }
00359 
00360    return 0;
00361 }
00362 
00363 void ast_rtp_instance_get_local_address(struct ast_rtp_instance *instance,
00364       struct ast_sockaddr *address)
00365 {
00366    ast_sockaddr_copy(address, &instance->local_address);
00367 }
00368 
00369 int ast_rtp_instance_get_and_cmp_remote_address(struct ast_rtp_instance *instance,
00370       struct ast_sockaddr *address)
00371 {
00372    if (ast_sockaddr_cmp(address, &instance->remote_address) != 0) {
00373       ast_sockaddr_copy(address, &instance->remote_address);
00374       return 1;
00375    }
00376 
00377    return 0;
00378 }
00379 
00380 void ast_rtp_instance_get_remote_address(struct ast_rtp_instance *instance,
00381       struct ast_sockaddr *address)
00382 {
00383    ast_sockaddr_copy(address, &instance->remote_address);
00384 }
00385 
00386 void ast_rtp_instance_set_extended_prop(struct ast_rtp_instance *instance, int property, void *value)
00387 {
00388    if (instance->engine->extended_prop_set) {
00389       instance->engine->extended_prop_set(instance, property, value);
00390    }
00391 }
00392 
00393 void *ast_rtp_instance_get_extended_prop(struct ast_rtp_instance *instance, int property)
00394 {
00395    if (instance->engine->extended_prop_get) {
00396       return instance->engine->extended_prop_get(instance, property);
00397    }
00398 
00399    return NULL;
00400 }
00401 
00402 void ast_rtp_instance_set_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value)
00403 {
00404    instance->properties[property] = value;
00405 
00406    if (instance->engine->prop_set) {
00407       instance->engine->prop_set(instance, property, value);
00408    }
00409 }
00410 
00411 int ast_rtp_instance_get_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property)
00412 {
00413    return instance->properties[property];
00414 }
00415 
00416 struct ast_rtp_codecs *ast_rtp_instance_get_codecs(struct ast_rtp_instance *instance)
00417 {
00418    return &instance->codecs;
00419 }
00420 
00421 static int rtp_payload_type_hash(const void *obj, const int flags)
00422 {
00423    const struct ast_rtp_payload_type *type = obj;
00424    const int *payload = obj;
00425 
00426    return (flags & OBJ_KEY) ? *payload : type->payload;
00427 }
00428 
00429 static int rtp_payload_type_cmp(void *obj, void *arg, int flags)
00430 {
00431    struct ast_rtp_payload_type *type1 = obj, *type2 = arg;
00432    const int *payload = arg;
00433 
00434    return (type1->payload == (OBJ_KEY ? *payload : type2->payload)) ? CMP_MATCH | CMP_STOP : 0;
00435 }
00436 
00437 int ast_rtp_codecs_payloads_initialize(struct ast_rtp_codecs *codecs)
00438 {
00439    if (!(codecs->payloads = ao2_container_alloc(AST_RTP_MAX_PT, rtp_payload_type_hash, rtp_payload_type_cmp))) {
00440       return -1;
00441    }
00442 
00443    return 0;
00444 }
00445 
00446 void ast_rtp_codecs_payloads_destroy(struct ast_rtp_codecs *codecs)
00447 {
00448    ao2_cleanup(codecs->payloads);
00449 }
00450 
00451 void ast_rtp_codecs_payloads_clear(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
00452 {
00453    ast_rtp_codecs_payloads_destroy(codecs);
00454 
00455    if (instance && instance->engine && instance->engine->payload_set) {
00456       int i;
00457       for (i = 0; i < AST_RTP_MAX_PT; i++) {
00458          instance->engine->payload_set(instance, i, 0, NULL, 0);
00459       }
00460    }
00461 
00462    ast_rtp_codecs_payloads_initialize(codecs);
00463 }
00464 
00465 void ast_rtp_codecs_payloads_default(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
00466 {
00467    int i;
00468 
00469    ast_rwlock_rdlock(&static_RTP_PT_lock);
00470    for (i = 0; i < AST_RTP_MAX_PT; i++) {
00471       if (static_RTP_PT[i].rtp_code || static_RTP_PT[i].asterisk_format) {
00472          struct ast_rtp_payload_type *type;
00473 
00474          if (!(type = ao2_alloc(sizeof(*type), NULL))) {
00475             /* Unfortunately if this occurs the payloads container will not contain all possible default payloads
00476              * but we err on the side of doing what we can in the hopes that the extreme memory conditions which
00477              * caused this to occur will go away.
00478              */
00479             continue;
00480          }
00481 
00482          type->payload = i;
00483          type->asterisk_format = static_RTP_PT[i].asterisk_format;
00484          type->rtp_code = static_RTP_PT[i].rtp_code;
00485          ast_format_copy(&type->format, &static_RTP_PT[i].format);
00486 
00487          ao2_link_flags(codecs->payloads, type, OBJ_NOLOCK);
00488 
00489          if (instance && instance->engine && instance->engine->payload_set) {
00490             instance->engine->payload_set(instance, i, type->asterisk_format, &type->format, type->rtp_code);
00491          }
00492 
00493          ao2_ref(type, -1);
00494       }
00495    }
00496    ast_rwlock_unlock(&static_RTP_PT_lock);
00497 }
00498 
00499 void ast_rtp_codecs_payloads_copy(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
00500 {
00501    int i;
00502    struct ast_rtp_payload_type *type;
00503 
00504    for (i = 0; i < AST_RTP_MAX_PT; i++) {
00505       struct ast_rtp_payload_type *new_type;
00506 
00507       if (!(type = ao2_find(src->payloads, &i, OBJ_KEY | OBJ_NOLOCK))) {
00508          continue;
00509       }
00510 
00511       if (!(new_type = ao2_alloc(sizeof(*new_type), NULL))) {
00512          continue;
00513       }
00514 
00515       ast_debug(2, "Copying payload %d from %p to %p\n", i, src, dest);
00516 
00517       new_type->payload = i;
00518       *new_type = *type;
00519 
00520       ao2_link_flags(dest->payloads, new_type, OBJ_NOLOCK);
00521 
00522       ao2_ref(new_type, -1);
00523 
00524       if (instance && instance->engine && instance->engine->payload_set) {
00525          instance->engine->payload_set(instance, i, type->asterisk_format, &type->format, type->rtp_code);
00526       }
00527 
00528       ao2_ref(type, -1);
00529    }
00530 }
00531 
00532 void ast_rtp_codecs_payloads_set_m_type(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
00533 {
00534    struct ast_rtp_payload_type *type;
00535 
00536    ast_rwlock_rdlock(&static_RTP_PT_lock);
00537 
00538    if (payload < 0 || payload >= AST_RTP_MAX_PT) {
00539       ast_rwlock_unlock(&static_RTP_PT_lock);
00540       return;
00541    }
00542 
00543    if (!(type = ao2_find(codecs->payloads, &payload, OBJ_KEY | OBJ_NOLOCK))) {
00544       if (!(type = ao2_alloc(sizeof(*type), NULL))) {
00545          ast_rwlock_unlock(&static_RTP_PT_lock);
00546          return;
00547       }
00548       type->payload = payload;
00549       ao2_link_flags(codecs->payloads, type, OBJ_NOLOCK);
00550    }
00551 
00552    type->asterisk_format = static_RTP_PT[payload].asterisk_format;
00553    type->rtp_code = static_RTP_PT[payload].rtp_code;
00554    type->payload = payload;
00555    ast_format_copy(&type->format, &static_RTP_PT[payload].format);
00556 
00557    ast_debug(1, "Setting payload %d based on m type on %p\n", payload, codecs);
00558 
00559    if (instance && instance->engine && instance->engine->payload_set) {
00560       instance->engine->payload_set(instance, payload, type->asterisk_format, &type->format, type->rtp_code);
00561    }
00562 
00563    ao2_ref(type, -1);
00564 
00565    ast_rwlock_unlock(&static_RTP_PT_lock);
00566 }
00567 
00568 int ast_rtp_codecs_payloads_set_rtpmap_type_rate(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int pt,
00569              char *mimetype, char *mimesubtype,
00570              enum ast_rtp_options options,
00571              unsigned int sample_rate)
00572 {
00573    unsigned int i;
00574    int found = 0;
00575 
00576    if (pt < 0 || pt >= AST_RTP_MAX_PT)
00577       return -1; /* bogus payload type */
00578 
00579    ast_rwlock_rdlock(&mime_types_lock);
00580    for (i = 0; i < mime_types_len; ++i) {
00581       const struct ast_rtp_mime_type *t = &ast_rtp_mime_types[i];
00582       struct ast_rtp_payload_type *type;
00583 
00584       if (strcasecmp(mimesubtype, t->subtype)) {
00585          continue;
00586       }
00587 
00588       if (strcasecmp(mimetype, t->type)) {
00589          continue;
00590       }
00591 
00592       /* if both sample rates have been supplied, and they don't match,
00593        * then this not a match; if one has not been supplied, then the
00594        * rates are not compared */
00595       if (sample_rate && t->sample_rate &&
00596           (sample_rate != t->sample_rate)) {
00597          continue;
00598       }
00599 
00600       found = 1;
00601 
00602       if (!(type = ao2_find(codecs->payloads, &pt, OBJ_KEY | OBJ_NOLOCK))) {
00603          if (!(type = ao2_alloc(sizeof(*type), NULL))) {
00604             continue;
00605          }
00606          type->payload = pt;
00607          ao2_link_flags(codecs->payloads, type, OBJ_NOLOCK);
00608       }
00609 
00610       *type = t->payload_type;
00611       type->payload = pt;
00612 
00613       if ((t->payload_type.format.id == AST_FORMAT_G726) && t->payload_type.asterisk_format && (options & AST_RTP_OPT_G726_NONSTANDARD)) {
00614          ast_format_set(&type->format, AST_FORMAT_G726_AAL2, 0);
00615       }
00616 
00617       if (instance && instance->engine && instance->engine->payload_set) {
00618          instance->engine->payload_set(instance, pt, type->asterisk_format, &type->format, type->rtp_code);
00619       }
00620 
00621       ao2_ref(type, -1);
00622 
00623       break;
00624    }
00625    ast_rwlock_unlock(&mime_types_lock);
00626 
00627    return (found ? 0 : -2);
00628 }
00629 
00630 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)
00631 {
00632    return ast_rtp_codecs_payloads_set_rtpmap_type_rate(codecs, instance, payload, mimetype, mimesubtype, options, 0);
00633 }
00634 
00635 void ast_rtp_codecs_payloads_unset(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
00636 {
00637    if (payload < 0 || payload >= AST_RTP_MAX_PT) {
00638       return;
00639    }
00640 
00641    ast_debug(2, "Unsetting payload %d on %p\n", payload, codecs);
00642 
00643    ao2_find(codecs->payloads, &payload, OBJ_KEY | OBJ_NOLOCK | OBJ_NODATA | OBJ_UNLINK);
00644 
00645    if (instance && instance->engine && instance->engine->payload_set) {
00646       instance->engine->payload_set(instance, payload, 0, NULL, 0);
00647    }
00648 }
00649 
00650 struct ast_rtp_payload_type ast_rtp_codecs_payload_lookup(struct ast_rtp_codecs *codecs, int payload)
00651 {
00652    struct ast_rtp_payload_type result = { .asterisk_format = 0, }, *type;
00653 
00654    if (payload < 0 || payload >= AST_RTP_MAX_PT) {
00655       return result;
00656    }
00657 
00658    if ((type = ao2_find(codecs->payloads, &payload, OBJ_KEY | OBJ_NOLOCK))) {
00659       result = *type;
00660       ao2_ref(type, -1);
00661    }
00662 
00663    if (!result.rtp_code && !result.asterisk_format) {
00664       ast_rwlock_rdlock(&static_RTP_PT_lock);
00665       result = static_RTP_PT[payload];
00666       ast_rwlock_unlock(&static_RTP_PT_lock);
00667    }
00668 
00669    return result;
00670 }
00671 
00672 
00673 struct ast_format *ast_rtp_codecs_get_payload_format(struct ast_rtp_codecs *codecs, int payload)
00674 {
00675    struct ast_rtp_payload_type *type;
00676    struct ast_format *format;
00677 
00678    if (payload < 0 || payload >= AST_RTP_MAX_PT) {
00679       return NULL;
00680    }
00681 
00682    if (!(type = ao2_find(codecs->payloads, &payload, OBJ_KEY | OBJ_NOLOCK))) {
00683       return NULL;
00684    }
00685 
00686    format = type->asterisk_format ? &type->format : NULL;
00687 
00688    ao2_ref(type, -1);
00689 
00690    return format;
00691 }
00692 
00693 static int rtp_payload_type_add_ast(void *obj, void *arg, int flags)
00694 {
00695    struct ast_rtp_payload_type *type = obj;
00696    struct ast_format_cap *astformats = arg;
00697 
00698    if (type->asterisk_format) {
00699       ast_format_cap_add(astformats, &type->format);
00700    }
00701 
00702    return 0;
00703 }
00704 
00705 static int rtp_payload_type_add_nonast(void *obj, void *arg, int flags)
00706 {
00707    struct ast_rtp_payload_type *type = obj;
00708    int *nonastformats = arg;
00709 
00710    if (!type->asterisk_format) {
00711       *nonastformats |= type->rtp_code;
00712    }
00713 
00714    return 0;
00715 }
00716 
00717 void ast_rtp_codecs_payload_formats(struct ast_rtp_codecs *codecs, struct ast_format_cap *astformats, int *nonastformats)
00718 {
00719    ast_format_cap_remove_all(astformats);
00720    *nonastformats = 0;
00721 
00722    ao2_callback(codecs->payloads, OBJ_NODATA | OBJ_MULTIPLE | OBJ_NOLOCK, rtp_payload_type_add_ast, astformats);
00723    ao2_callback(codecs->payloads, OBJ_NODATA | OBJ_MULTIPLE | OBJ_NOLOCK, rtp_payload_type_add_nonast, nonastformats);
00724 }
00725 
00726 static int rtp_payload_type_find_format(void *obj, void *arg, int flags)
00727 {
00728    struct ast_rtp_payload_type *type = obj;
00729    struct ast_format *format = arg;
00730 
00731    return (type->asterisk_format && (ast_format_cmp(&type->format, format) != AST_FORMAT_CMP_NOT_EQUAL)) ? CMP_MATCH | CMP_STOP : 0;
00732 }
00733 
00734 static int rtp_payload_type_find_nonast_format(void *obj, void *arg, int flags)
00735 {
00736    struct ast_rtp_payload_type *type = obj;
00737    int *rtp_code = arg;
00738 
00739    return ((!type->asterisk_format && (type->rtp_code == *rtp_code)) ? CMP_MATCH | CMP_STOP : 0);
00740 }
00741 
00742 int ast_rtp_codecs_payload_code(struct ast_rtp_codecs *codecs, int asterisk_format, const struct ast_format *format, int code)
00743 {
00744    struct ast_rtp_payload_type *type;
00745    int i, res = -1;
00746 
00747    if (asterisk_format && format && (type = ao2_callback(codecs->payloads, OBJ_NOLOCK, rtp_payload_type_find_format, (void*)format))) {
00748       res = type->payload;
00749       ao2_ref(type, -1);
00750       return res;
00751    } else if (!asterisk_format && (type = ao2_callback(codecs->payloads, OBJ_NOLOCK, rtp_payload_type_find_nonast_format, (void*)&code))) {
00752       res = type->payload;
00753       ao2_ref(type, -1);
00754       return res;
00755    }
00756 
00757    ast_rwlock_rdlock(&static_RTP_PT_lock);
00758    for (i = 0; i < AST_RTP_MAX_PT; i++) {
00759       if (static_RTP_PT[i].asterisk_format && asterisk_format && format &&
00760          (ast_format_cmp(format, &static_RTP_PT[i].format) != AST_FORMAT_CMP_NOT_EQUAL)) {
00761          res = i;
00762          break;
00763       } else if (!static_RTP_PT[i].asterisk_format && !asterisk_format &&
00764          (static_RTP_PT[i].rtp_code == code)) {
00765          res = i;
00766          break;
00767       }
00768    }
00769    ast_rwlock_unlock(&static_RTP_PT_lock);
00770 
00771    return res;
00772 }
00773 int ast_rtp_codecs_find_payload_code(struct ast_rtp_codecs *codecs, int code)
00774 {
00775    struct ast_rtp_payload_type *type;
00776    int res = -1;
00777 
00778    /* Search the payload type in the codecs passed */
00779    if ((type = ao2_find(codecs->payloads, &code, OBJ_NOLOCK | OBJ_KEY)))
00780    {
00781       res = type->payload;
00782       ao2_ref(type, -1);
00783       return res;
00784    }
00785 
00786    return res;
00787 }
00788 const char *ast_rtp_lookup_mime_subtype2(const int asterisk_format, struct ast_format *format, int code, enum ast_rtp_options options)
00789 {
00790    int i;
00791    const char *res = "";
00792 
00793    ast_rwlock_rdlock(&mime_types_lock);
00794    for (i = 0; i < mime_types_len; i++) {
00795       if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
00796          (ast_format_cmp(format, &ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
00797          if ((format->id == AST_FORMAT_G726_AAL2) && (options & AST_RTP_OPT_G726_NONSTANDARD)) {
00798             res = "G726-32";
00799             break;
00800          } else {
00801             res = ast_rtp_mime_types[i].subtype;
00802             break;
00803          }
00804       } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
00805          ast_rtp_mime_types[i].payload_type.rtp_code == code) {
00806 
00807          res = ast_rtp_mime_types[i].subtype;
00808          break;
00809       }
00810    }
00811    ast_rwlock_unlock(&mime_types_lock);
00812 
00813    return res;
00814 }
00815 
00816 unsigned int ast_rtp_lookup_sample_rate2(int asterisk_format, struct ast_format *format, int code)
00817 {
00818    unsigned int i;
00819    unsigned int res = 0;
00820 
00821    ast_rwlock_rdlock(&mime_types_lock);
00822    for (i = 0; i < mime_types_len; ++i) {
00823       if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
00824          (ast_format_cmp(format, &ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
00825          res = ast_rtp_mime_types[i].sample_rate;
00826          break;
00827       } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
00828          ast_rtp_mime_types[i].payload_type.rtp_code == code) {
00829          res = ast_rtp_mime_types[i].sample_rate;
00830          break;
00831       }
00832    }
00833    ast_rwlock_unlock(&mime_types_lock);
00834 
00835    return res;
00836 }
00837 
00838 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)
00839 {
00840    int found = 0;
00841    const char *name;
00842    if (!buf) {
00843       return NULL;
00844    }
00845 
00846 
00847    if (asterisk_format) {
00848       struct ast_format tmp_fmt;
00849       ast_format_cap_iter_start(ast_format_capability);
00850       while (!ast_format_cap_iter_next(ast_format_capability, &tmp_fmt)) {
00851          name = ast_rtp_lookup_mime_subtype2(asterisk_format, &tmp_fmt, 0, options);
00852          ast_str_append(&buf, 0, "%s|", name);
00853          found = 1;
00854       }
00855       ast_format_cap_iter_end(ast_format_capability);
00856 
00857    } else {
00858       int x;
00859       ast_str_append(&buf, 0, "0x%x (", (unsigned int) rtp_capability);
00860       for (x = 1; x <= AST_RTP_MAX; x <<= 1) {
00861          if (rtp_capability & x) {
00862             name = ast_rtp_lookup_mime_subtype2(asterisk_format, NULL, x, options);
00863             ast_str_append(&buf, 0, "%s|", name);
00864             found = 1;
00865          }
00866       }
00867    }
00868 
00869    ast_str_append(&buf, 0, "%s", found ? ")" : "nothing)");
00870 
00871    return ast_str_buffer(buf);
00872 }
00873 
00874 void ast_rtp_codecs_packetization_set(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, struct ast_codec_pref *prefs)
00875 {
00876    codecs->pref = *prefs;
00877 
00878    if (instance && instance->engine->packetization_set) {
00879       instance->engine->packetization_set(instance, &instance->codecs.pref);
00880    }
00881 }
00882 
00883 int ast_rtp_instance_dtmf_begin(struct ast_rtp_instance *instance, char digit)
00884 {
00885    return instance->engine->dtmf_begin ? instance->engine->dtmf_begin(instance, digit) : -1;
00886 }
00887 
00888 int ast_rtp_instance_dtmf_end(struct ast_rtp_instance *instance, char digit)
00889 {
00890    return instance->engine->dtmf_end ? instance->engine->dtmf_end(instance, digit) : -1;
00891 }
00892 int ast_rtp_instance_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration)
00893 {
00894    return instance->engine->dtmf_end_with_duration ? instance->engine->dtmf_end_with_duration(instance, digit, duration) : -1;
00895 }
00896 
00897 int ast_rtp_instance_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode)
00898 {
00899    return (!instance->engine->dtmf_mode_set || instance->engine->dtmf_mode_set(instance, dtmf_mode)) ? -1 : 0;
00900 }
00901 
00902 enum ast_rtp_dtmf_mode ast_rtp_instance_dtmf_mode_get(struct ast_rtp_instance *instance)
00903 {
00904    return instance->engine->dtmf_mode_get ? instance->engine->dtmf_mode_get(instance) : 0;
00905 }
00906 
00907 void ast_rtp_instance_update_source(struct ast_rtp_instance *instance)
00908 {
00909    if (instance->engine->update_source) {
00910       instance->engine->update_source(instance);
00911    }
00912 }
00913 
00914 void ast_rtp_instance_change_source(struct ast_rtp_instance *instance)
00915 {
00916    if (instance->engine->change_source) {
00917       instance->engine->change_source(instance);
00918    }
00919 }
00920 
00921 int ast_rtp_instance_set_qos(struct ast_rtp_instance *instance, int tos, int cos, const char *desc)
00922 {
00923    return instance->engine->qos ? instance->engine->qos(instance, tos, cos, desc) : -1;
00924 }
00925 
00926 void ast_rtp_instance_stop(struct ast_rtp_instance *instance)
00927 {
00928    if (instance->engine->stop) {
00929       instance->engine->stop(instance);
00930    }
00931 }
00932 
00933 int ast_rtp_instance_fd(struct ast_rtp_instance *instance, int rtcp)
00934 {
00935    return instance->engine->fd ? instance->engine->fd(instance, rtcp) : -1;
00936 }
00937 
00938 struct ast_rtp_glue *ast_rtp_instance_get_glue(const char *type)
00939 {
00940    struct ast_rtp_glue *glue = NULL;
00941 
00942    AST_RWLIST_RDLOCK(&glues);
00943 
00944    AST_RWLIST_TRAVERSE(&glues, glue, entry) {
00945       if (!strcasecmp(glue->type, type)) {
00946          break;
00947       }
00948    }
00949 
00950    AST_RWLIST_UNLOCK(&glues);
00951 
00952    return glue;
00953 }
00954 
00955 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)
00956 {
00957    enum ast_bridge_result res = AST_BRIDGE_FAILED;
00958    struct ast_channel *who = NULL, *other = NULL, *cs[3] = { NULL, };
00959    struct ast_frame *fr = NULL;
00960    struct timeval start;
00961 
00962    /* Start locally bridging both instances */
00963    if (instance0->engine->local_bridge && instance0->engine->local_bridge(instance0, instance1)) {
00964       ast_debug(1, "Failed to locally bridge %s to %s, backing out.\n", ast_channel_name(c0), ast_channel_name(c1));
00965       ast_channel_unlock(c0);
00966       ast_channel_unlock(c1);
00967       return AST_BRIDGE_FAILED_NOWARN;
00968    }
00969    if (instance1->engine->local_bridge && instance1->engine->local_bridge(instance1, instance0)) {
00970       ast_debug(1, "Failed to locally bridge %s to %s, backing out.\n", ast_channel_name(c1), ast_channel_name(c0));
00971       if (instance0->engine->local_bridge) {
00972          instance0->engine->local_bridge(instance0, NULL);
00973       }
00974       ast_channel_unlock(c0);
00975       ast_channel_unlock(c1);
00976       return AST_BRIDGE_FAILED_NOWARN;
00977    }
00978 
00979    ast_channel_unlock(c0);
00980    ast_channel_unlock(c1);
00981 
00982    instance0->bridged = instance1;
00983    instance1->bridged = instance0;
00984 
00985    ast_poll_channel_add(c0, c1);
00986 
00987    /* Hop into a loop waiting for a frame from either channel */
00988    cs[0] = c0;
00989    cs[1] = c1;
00990    cs[2] = NULL;
00991    start = ast_tvnow();
00992    for (;;) {
00993       int ms;
00994       /* If the underlying formats have changed force this bridge to break */
00995       if ((ast_format_cmp(ast_channel_rawreadformat(c0), ast_channel_rawwriteformat(c1)) == AST_FORMAT_CMP_NOT_EQUAL) ||
00996          (ast_format_cmp(ast_channel_rawreadformat(c1), ast_channel_rawwriteformat(c0)) == AST_FORMAT_CMP_NOT_EQUAL)) {
00997          ast_debug(1, "rtp-engine-local-bridge: Oooh, formats changed, backing out\n");
00998          res = AST_BRIDGE_FAILED_NOWARN;
00999          break;
01000       }
01001       /* Check if anything changed */
01002       if ((ast_channel_tech_pvt(c0) != pvt0) ||
01003           (ast_channel_tech_pvt(c1) != pvt1) ||
01004           (ast_channel_masq(c0) || ast_channel_masqr(c0) || ast_channel_masq(c1) || ast_channel_masqr(c1)) ||
01005           (ast_channel_monitor(c0) || ast_channel_audiohooks(c0) || ast_channel_monitor(c1) || ast_channel_audiohooks(c1)) ||
01006           (!ast_framehook_list_is_empty(ast_channel_framehooks(c0)) || !ast_framehook_list_is_empty(ast_channel_framehooks(c1)))) {
01007          ast_debug(1, "rtp-engine-local-bridge: Oooh, something is weird, backing out\n");
01008          /* 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 */
01009          if ((ast_channel_masq(c0) || ast_channel_masqr(c0)) && (fr = ast_read(c0))) {
01010             ast_frfree(fr);
01011          }
01012          if ((ast_channel_masq(c1) || ast_channel_masqr(c1)) && (fr = ast_read(c1))) {
01013             ast_frfree(fr);
01014          }
01015          res = AST_BRIDGE_RETRY;
01016          break;
01017       }
01018       /* Wait on a channel to feed us a frame */
01019       ms = ast_remaining_ms(start, timeoutms);
01020       if (!(who = ast_waitfor_n(cs, 2, &ms))) {
01021          if (!ms) {
01022             res = AST_BRIDGE_RETRY;
01023             break;
01024          }
01025          ast_debug(2, "rtp-engine-local-bridge: Ooh, empty read...\n");
01026          if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
01027             break;
01028          }
01029          continue;
01030       }
01031       /* Read in frame from channel */
01032       fr = ast_read(who);
01033       other = (who == c0) ? c1 : c0;
01034       /* Depending on the frame we may need to break out of our bridge */
01035       if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
01036              ((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) |
01037              ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1)))) {
01038          /* Record received frame and who */
01039          *fo = fr;
01040          *rc = who;
01041          ast_debug(1, "rtp-engine-local-bridge: Ooh, got a %s\n", fr ? "digit" : "hangup");
01042          res = AST_BRIDGE_COMPLETE;
01043          break;
01044       } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
01045          if ((fr->subclass.integer == AST_CONTROL_HOLD) ||
01046              (fr->subclass.integer == AST_CONTROL_UNHOLD) ||
01047              (fr->subclass.integer == AST_CONTROL_VIDUPDATE) ||
01048              (fr->subclass.integer == AST_CONTROL_SRCUPDATE) ||
01049              (fr->subclass.integer == AST_CONTROL_T38_PARAMETERS) ||
01050              (fr->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER)) {
01051             /* If we are going on hold, then break callback mode and P2P bridging */
01052             if (fr->subclass.integer == AST_CONTROL_HOLD) {
01053                if (instance0->engine->local_bridge) {
01054                   instance0->engine->local_bridge(instance0, NULL);
01055                }
01056                if (instance1->engine->local_bridge) {
01057                   instance1->engine->local_bridge(instance1, NULL);
01058                }
01059                instance0->bridged = NULL;
01060                instance1->bridged = NULL;
01061             } else if (fr->subclass.integer == AST_CONTROL_UNHOLD) {
01062                if (instance0->engine->local_bridge) {
01063                   instance0->engine->local_bridge(instance0, instance1);
01064                }
01065                if (instance1->engine->local_bridge) {
01066                   instance1->engine->local_bridge(instance1, instance0);
01067                }
01068                instance0->bridged = instance1;
01069                instance1->bridged = instance0;
01070             }
01071             /* Since UPDATE_BRIDGE_PEER is only used by the bridging code, don't forward it */
01072             if (fr->subclass.integer != AST_CONTROL_UPDATE_RTP_PEER) {
01073                ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
01074             }
01075             ast_frfree(fr);
01076          } else if (fr->subclass.integer == AST_CONTROL_CONNECTED_LINE) {
01077             if (ast_channel_connected_line_sub(who, other, fr, 1) &&
01078                ast_channel_connected_line_macro(who, other, fr, other == c0, 1)) {
01079                ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
01080             }
01081             ast_frfree(fr);
01082          } else if (fr->subclass.integer == AST_CONTROL_REDIRECTING) {
01083             if (ast_channel_redirecting_sub(who, other, fr, 1) &&
01084                ast_channel_redirecting_macro(who, other, fr, other == c0, 1)) {
01085                ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
01086             }
01087             ast_frfree(fr);
01088          } else if (fr->subclass.integer == AST_CONTROL_PVT_CAUSE_CODE) {
01089             ast_channel_hangupcause_hash_set(other, fr->data.ptr, fr->datalen);
01090             ast_frfree(fr);
01091          } else {
01092             *fo = fr;
01093             *rc = who;
01094             ast_debug(1, "rtp-engine-local-bridge: Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass.integer, ast_channel_name(who));
01095             res = AST_BRIDGE_COMPLETE;
01096             break;
01097          }
01098       } else {
01099          if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
01100              (fr->frametype == AST_FRAME_DTMF_END) ||
01101              (fr->frametype == AST_FRAME_VOICE) ||
01102              (fr->frametype == AST_FRAME_VIDEO) ||
01103              (fr->frametype == AST_FRAME_IMAGE) ||
01104              (fr->frametype == AST_FRAME_HTML) ||
01105              (fr->frametype == AST_FRAME_MODEM) ||
01106              (fr->frametype == AST_FRAME_TEXT)) {
01107             ast_write(other, fr);
01108          }
01109 
01110          ast_frfree(fr);
01111       }
01112       /* Swap priority */
01113       cs[2] = cs[0];
01114       cs[0] = cs[1];
01115       cs[1] = cs[2];
01116    }
01117 
01118    /* Stop locally bridging both instances */
01119    if (instance0->engine->local_bridge) {
01120       instance0->engine->local_bridge(instance0, NULL);
01121    }
01122    if (instance1->engine->local_bridge) {
01123       instance1->engine->local_bridge(instance1, NULL);
01124    }
01125 
01126    instance0->bridged = NULL;
01127    instance1->bridged = NULL;
01128 
01129    ast_poll_channel_del(c0, c1);
01130 
01131    return res;
01132 }
01133 
01134 static enum ast_bridge_result remote_bridge_loop(struct ast_channel *c0,
01135    struct ast_channel *c1,
01136    struct ast_rtp_instance *instance0,
01137    struct ast_rtp_instance *instance1,
01138    struct ast_rtp_instance *vinstance0,
01139    struct ast_rtp_instance *vinstance1,
01140    struct ast_rtp_instance *tinstance0,
01141    struct ast_rtp_instance *tinstance1,
01142    struct ast_rtp_glue *glue0,
01143    struct ast_rtp_glue *glue1,
01144    struct ast_format_cap *cap0,
01145    struct ast_format_cap *cap1,
01146    int timeoutms,
01147    int flags,
01148    struct ast_frame **fo,
01149    struct ast_channel **rc,
01150    void *pvt0,
01151    void *pvt1)
01152 {
01153    enum ast_bridge_result res = AST_BRIDGE_FAILED;
01154    struct ast_channel *who = NULL, *other = NULL, *cs[3] = { NULL, };
01155    struct ast_format_cap *oldcap0 = ast_format_cap_dup(cap0);
01156    struct ast_format_cap *oldcap1 = ast_format_cap_dup(cap1);
01157    struct ast_sockaddr ac1 = {{0,}}, vac1 = {{0,}}, tac1 = {{0,}}, ac0 = {{0,}}, vac0 = {{0,}}, tac0 = {{0,}};
01158    struct ast_sockaddr t1 = {{0,}}, vt1 = {{0,}}, tt1 = {{0,}}, t0 = {{0,}}, vt0 = {{0,}}, tt0 = {{0,}};
01159    struct ast_frame *fr = NULL;
01160    struct timeval start;
01161 
01162    if (!oldcap0 || !oldcap1) {
01163       ast_channel_unlock(c0);
01164       ast_channel_unlock(c1);
01165       goto remote_bridge_cleanup;
01166    }
01167    /* Test the first channel */
01168    if (!(glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0))) {
01169       ast_rtp_instance_get_remote_address(instance1, &ac1);
01170       if (vinstance1) {
01171          ast_rtp_instance_get_remote_address(vinstance1, &vac1);
01172       }
01173       if (tinstance1) {
01174          ast_rtp_instance_get_remote_address(tinstance1, &tac1);
01175       }
01176    } else {
01177       ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", ast_channel_name(c0), ast_channel_name(c1));
01178    }
01179 
01180    /* Test the second channel */
01181    if (!(glue1->update_peer(c1, instance0, vinstance0, tinstance0, cap0, 0))) {
01182       ast_rtp_instance_get_remote_address(instance0, &ac0);
01183       if (vinstance0) {
01184          ast_rtp_instance_get_remote_address(instance0, &vac0);
01185       }
01186       if (tinstance0) {
01187          ast_rtp_instance_get_remote_address(instance0, &tac0);
01188       }
01189    } else {
01190       ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", ast_channel_name(c1), ast_channel_name(c0));
01191    }
01192 
01193    ast_channel_unlock(c0);
01194    ast_channel_unlock(c1);
01195 
01196    instance0->bridged = instance1;
01197    instance1->bridged = instance0;
01198 
01199    ast_poll_channel_add(c0, c1);
01200 
01201    /* Go into a loop handling any stray frames that may come in */
01202    cs[0] = c0;
01203    cs[1] = c1;
01204    cs[2] = NULL;
01205    start = ast_tvnow();
01206    for (;;) {
01207       int ms;
01208       /* Check if anything changed */
01209       if ((ast_channel_tech_pvt(c0) != pvt0) ||
01210           (ast_channel_tech_pvt(c1) != pvt1) ||
01211           (ast_channel_masq(c0) || ast_channel_masqr(c0) || ast_channel_masq(c1) || ast_channel_masqr(c1)) ||
01212           (ast_channel_monitor(c0) || ast_channel_audiohooks(c0) || ast_channel_monitor(c1) || ast_channel_audiohooks(c1)) ||
01213           (!ast_framehook_list_is_empty(ast_channel_framehooks(c0)) || !ast_framehook_list_is_empty(ast_channel_framehooks(c1)))) {
01214          ast_debug(1, "Oooh, something is weird, backing out\n");
01215          res = AST_BRIDGE_RETRY;
01216          break;
01217       }
01218 
01219       /* Check if they have changed their address */
01220       ast_rtp_instance_get_remote_address(instance1, &t1);
01221       if (vinstance1) {
01222          ast_rtp_instance_get_remote_address(vinstance1, &vt1);
01223       }
01224       if (tinstance1) {
01225          ast_rtp_instance_get_remote_address(tinstance1, &tt1);
01226       }
01227       ast_channel_lock(c1);
01228       if (glue1->get_codec && ast_channel_tech_pvt(c1)) {
01229          ast_format_cap_remove_all(cap1);
01230          glue1->get_codec(c1, cap1);
01231       }
01232       ast_channel_unlock(c1);
01233 
01234       ast_rtp_instance_get_remote_address(instance0, &t0);
01235       if (vinstance0) {
01236          ast_rtp_instance_get_remote_address(vinstance0, &vt0);
01237       }
01238       if (tinstance0) {
01239          ast_rtp_instance_get_remote_address(tinstance0, &tt0);
01240       }
01241       ast_channel_lock(c0);
01242       if (glue0->get_codec && ast_channel_tech_pvt(c0)) {
01243          ast_format_cap_remove_all(cap0);
01244          glue0->get_codec(c0, cap0);
01245       }
01246       ast_channel_unlock(c0);
01247 
01248       if ((ast_sockaddr_cmp(&t1, &ac1)) ||
01249           (vinstance1 && ast_sockaddr_cmp(&vt1, &vac1)) ||
01250           (tinstance1 && ast_sockaddr_cmp(&tt1, &tac1)) ||
01251           (!ast_format_cap_identical(cap1, oldcap1))) {
01252          char tmp_buf[512] = { 0, };
01253          ast_debug(1, "Oooh, '%s' changed end address to %s (format %s)\n",
01254               ast_channel_name(c1), ast_sockaddr_stringify(&t1),
01255               ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap1));
01256          ast_debug(1, "Oooh, '%s' changed end vaddress to %s (format %s)\n",
01257               ast_channel_name(c1), ast_sockaddr_stringify(&vt1),
01258               ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap1));
01259          ast_debug(1, "Oooh, '%s' changed end taddress to %s (format %s)\n",
01260               ast_channel_name(c1), ast_sockaddr_stringify(&tt1),
01261               ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap1));
01262          ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
01263               ast_channel_name(c1), ast_sockaddr_stringify(&ac1),
01264               ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap1));
01265          ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
01266               ast_channel_name(c1), ast_sockaddr_stringify(&vac1),
01267               ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap1));
01268          ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
01269               ast_channel_name(c1), ast_sockaddr_stringify(&tac1),
01270               ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap1));
01271          if (glue0->update_peer(c0,
01272                       ast_sockaddr_isnull(&t1)  ? NULL : instance1,
01273                       ast_sockaddr_isnull(&vt1) ? NULL : vinstance1,
01274                       ast_sockaddr_isnull(&tt1) ? NULL : tinstance1,
01275                       cap1, 0)) {
01276             ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", ast_channel_name(c0), ast_channel_name(c1));
01277          }
01278          ast_sockaddr_copy(&ac1, &t1);
01279          ast_sockaddr_copy(&vac1, &vt1);
01280          ast_sockaddr_copy(&tac1, &tt1);
01281          ast_format_cap_copy(oldcap1, cap1);
01282       }
01283       if ((ast_sockaddr_cmp(&t0, &ac0)) ||
01284           (vinstance0 && ast_sockaddr_cmp(&vt0, &vac0)) ||
01285           (tinstance0 && ast_sockaddr_cmp(&tt0, &tac0)) ||
01286           (!ast_format_cap_identical(cap0, oldcap0))) {
01287          char tmp_buf[512] = { 0, };
01288          ast_debug(1, "Oooh, '%s' changed end address to %s (format %s)\n",
01289               ast_channel_name(c0), ast_sockaddr_stringify(&t0),
01290               ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap0));
01291          ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
01292               ast_channel_name(c0), ast_sockaddr_stringify(&ac0),
01293               ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap0));
01294          if (glue1->update_peer(c1, t0.len ? instance0 : NULL,
01295                   vt0.len ? vinstance0 : NULL,
01296                   tt0.len ? tinstance0 : NULL,
01297                   cap0, 0)) {
01298             ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", ast_channel_name(c1), ast_channel_name(c0));
01299          }
01300          ast_sockaddr_copy(&ac0, &t0);
01301          ast_sockaddr_copy(&vac0, &vt0);
01302          ast_sockaddr_copy(&tac0, &tt0);
01303          ast_format_cap_copy(oldcap0, cap0);
01304       }
01305 
01306       ms = ast_remaining_ms(start, timeoutms);
01307       /* Wait for frame to come in on the channels */
01308       if (!(who = ast_waitfor_n(cs, 2, &ms))) {
01309          if (!ms) {
01310             res = AST_BRIDGE_RETRY;
01311             break;
01312          }
01313          ast_debug(1, "Ooh, empty read...\n");
01314          if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
01315             break;
01316          }
01317          continue;
01318       }
01319       fr = ast_read(who);
01320       other = (who == c0) ? c1 : c0;
01321       if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
01322              (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
01323               ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))))) {
01324          /* Break out of bridge */
01325          *fo = fr;
01326          *rc = who;
01327          ast_debug(1, "Oooh, got a %s\n", fr ? "digit" : "hangup");
01328          res = AST_BRIDGE_COMPLETE;
01329          break;
01330       } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
01331          if ((fr->subclass.integer == AST_CONTROL_HOLD) ||
01332              (fr->subclass.integer == AST_CONTROL_UNHOLD) ||
01333              (fr->subclass.integer == AST_CONTROL_VIDUPDATE) ||
01334              (fr->subclass.integer == AST_CONTROL_SRCUPDATE) ||
01335              (fr->subclass.integer == AST_CONTROL_T38_PARAMETERS) ||
01336             (fr->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER)) {
01337             if (fr->subclass.integer == AST_CONTROL_HOLD) {
01338                /* If we someone went on hold we want the other side to reinvite back to us */
01339                if (who == c0) {
01340                   glue1->update_peer(c1, NULL, NULL, NULL, 0, 0);
01341                } else {
01342                   glue0->update_peer(c0, NULL, NULL, NULL, 0, 0);
01343                }
01344             } else if (fr->subclass.integer == AST_CONTROL_UNHOLD ||
01345                fr->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER) {
01346                /* If they went off hold they should go back to being direct, or if we have
01347                 * been told to force a peer update, go ahead and do it. */
01348                if (who == c0) {
01349                   glue1->update_peer(c1, instance0, vinstance0, tinstance0, cap0, 0);
01350                } else {
01351                   glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0);
01352                }
01353             }
01354             /* Update local address information */
01355             ast_rtp_instance_get_remote_address(instance0, &t0);
01356             ast_sockaddr_copy(&ac0, &t0);
01357             ast_rtp_instance_get_remote_address(instance1, &t1);
01358             ast_sockaddr_copy(&ac1, &t1);
01359             /* Update codec information */
01360             ast_channel_lock(c0);
01361             if (glue0->get_codec && ast_channel_tech_pvt(c0)) {
01362                ast_format_cap_remove_all(cap0);
01363                ast_format_cap_remove_all(oldcap0);
01364                glue0->get_codec(c0, cap0);
01365                ast_format_cap_append(oldcap0, cap0);
01366 
01367             }
01368             ast_channel_unlock(c0);
01369             ast_channel_lock(c1);
01370             if (glue1->get_codec && ast_channel_tech_pvt(c1)) {
01371                ast_format_cap_remove_all(cap1);
01372                ast_format_cap_remove_all(oldcap1);
01373                glue1->get_codec(c1, cap1);
01374                ast_format_cap_append(oldcap1, cap1);
01375             }
01376             ast_channel_unlock(c1);
01377             /* Since UPDATE_BRIDGE_PEER is only used by the bridging code, don't forward it */
01378             if (fr->subclass.integer != AST_CONTROL_UPDATE_RTP_PEER) {
01379                ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
01380             }
01381             ast_frfree(fr);
01382          } else if (fr->subclass.integer == AST_CONTROL_CONNECTED_LINE) {
01383             if (ast_channel_connected_line_sub(who, other, fr, 1) &&
01384                ast_channel_connected_line_macro(who, other, fr, other == c0, 1)) {
01385                ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
01386             }
01387             ast_frfree(fr);
01388          } else if (fr->subclass.integer == AST_CONTROL_REDIRECTING) {
01389             if (ast_channel_redirecting_sub(who, other, fr, 1) &&
01390                ast_channel_redirecting_macro(who, other, fr, other == c0, 1)) {
01391                ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
01392             }
01393             ast_frfree(fr);
01394          } else if (fr->subclass.integer == AST_CONTROL_PVT_CAUSE_CODE) {
01395             ast_channel_hangupcause_hash_set(other, fr->data.ptr, fr->datalen);
01396             ast_frfree(fr);
01397          } else {
01398             *fo = fr;
01399             *rc = who;
01400             ast_debug(1, "Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass.integer, ast_channel_name(who));
01401             res = AST_BRIDGE_COMPLETE;
01402             break;
01403          }
01404       } else {
01405          if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
01406              (fr->frametype == AST_FRAME_DTMF_END) ||
01407              (fr->frametype == AST_FRAME_VOICE) ||
01408              (fr->frametype == AST_FRAME_VIDEO) ||
01409              (fr->frametype == AST_FRAME_IMAGE) ||
01410              (fr->frametype == AST_FRAME_HTML) ||
01411              (fr->frametype == AST_FRAME_MODEM) ||
01412              (fr->frametype == AST_FRAME_TEXT)) {
01413             ast_write(other, fr);
01414          }
01415          ast_frfree(fr);
01416       }
01417       /* Swap priority */
01418       cs[2] = cs[0];
01419       cs[0] = cs[1];
01420       cs[1] = cs[2];
01421    }
01422 
01423    if (ast_test_flag(ast_channel_flags(c0), AST_FLAG_ZOMBIE)) {
01424       ast_debug(1, "Channel '%s' Zombie cleardown from bridge\n", ast_channel_name(c0));
01425    } else if (ast_channel_tech_pvt(c0) != pvt0) {
01426       ast_debug(1, "Channel c0->'%s' pvt changed, in bridge with c1->'%s'\n", ast_channel_name(c0), ast_channel_name(c1));
01427    } else if (glue0 != ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) {
01428       ast_debug(1, "Channel c0->'%s' technology changed, in bridge with c1->'%s'\n", ast_channel_name(c0), ast_channel_name(c1));
01429    } else if (glue0->update_peer(c0, NULL, NULL, NULL, 0, 0)) {
01430       ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", ast_channel_name(c0));
01431    }
01432    if (ast_test_flag(ast_channel_flags(c1), AST_FLAG_ZOMBIE)) {
01433       ast_debug(1, "Channel '%s' Zombie cleardown from bridge\n", ast_channel_name(c1));
01434    } else if (ast_channel_tech_pvt(c1) != pvt1) {
01435       ast_debug(1, "Channel c1->'%s' pvt changed, in bridge with c0->'%s'\n", ast_channel_name(c1), ast_channel_name(c0));
01436    } else if (glue1 != ast_rtp_instance_get_glue(ast_channel_tech(c1)->type)) {
01437       ast_debug(1, "Channel c1->'%s' technology changed, in bridge with c0->'%s'\n", ast_channel_name(c1), ast_channel_name(c0));
01438    } else if (glue1->update_peer(c1, NULL, NULL, NULL, 0, 0)) {
01439       ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", ast_channel_name(c1));
01440    }
01441 
01442    instance0->bridged = NULL;
01443    instance1->bridged = NULL;
01444 
01445    ast_poll_channel_del(c0, c1);
01446 
01447 remote_bridge_cleanup:
01448    ast_format_cap_destroy(oldcap0);
01449    ast_format_cap_destroy(oldcap1);
01450 
01451    return res;
01452 }
01453 
01454 /*!
01455  * \brief Conditionally unref an rtp instance
01456  */
01457 static void unref_instance_cond(struct ast_rtp_instance **instance)
01458 {
01459    if (*instance) {
01460       ao2_ref(*instance, -1);
01461       *instance = NULL;
01462    }
01463 }
01464 
01465 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)
01466 {
01467    struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
01468          *vinstance0 = NULL, *vinstance1 = NULL,
01469          *tinstance0 = NULL, *tinstance1 = NULL;
01470    struct ast_rtp_glue *glue0, *glue1;
01471    struct ast_sockaddr addr1 = { {0, }, }, addr2 = { {0, }, };
01472    enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
01473    enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
01474    enum ast_bridge_result res = AST_BRIDGE_FAILED;
01475    enum ast_rtp_dtmf_mode dmode;
01476    struct ast_format_cap *cap0 = ast_format_cap_alloc_nolock();
01477    struct ast_format_cap *cap1 = ast_format_cap_alloc_nolock();
01478    int unlock_chans = 1;
01479    int read_ptime0, read_ptime1, write_ptime0, write_ptime1;
01480 
01481    if (!cap0 || !cap1) {
01482       unlock_chans = 0;
01483       goto done;
01484    }
01485 
01486    /* Lock both channels so we can look for the glue that binds them together */
01487    ast_channel_lock(c0);
01488    while (ast_channel_trylock(c1)) {
01489       ast_channel_unlock(c0);
01490       usleep(1);
01491       ast_channel_lock(c0);
01492    }
01493 
01494    /* Ensure neither channel got hungup during lock avoidance */
01495    if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
01496       ast_log(LOG_WARNING, "Got hangup while attempting to bridge '%s' and '%s'\n", ast_channel_name(c0), ast_channel_name(c1));
01497       goto done;
01498    }
01499 
01500    /* Grab glue that binds each channel to something using the RTP engine */
01501    if (!(glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) || !(glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type))) {
01502       ast_debug(1, "Can't find native functions for channel '%s'\n", glue0 ? ast_channel_name(c1) : ast_channel_name(c0));
01503       goto done;
01504    }
01505 
01506    audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
01507    video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
01508 
01509    audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
01510    video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
01511 
01512    /* If the channels are of the same technology, they might have limitations on remote bridging */
01513    if (ast_channel_tech(c0) == ast_channel_tech(c1)) {
01514       if (audio_glue0_res == audio_glue1_res && audio_glue1_res == AST_RTP_GLUE_RESULT_REMOTE) {
01515          if (glue0->allow_rtp_remote && !(glue0->allow_rtp_remote(c0, c1))) {
01516             /* If the allow_rtp_remote indicates that remote isn't allowed, revert to local bridge */
01517             audio_glue0_res = audio_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
01518          }
01519       }
01520       if (video_glue0_res == video_glue1_res && video_glue1_res == AST_RTP_GLUE_RESULT_REMOTE) {
01521          if (glue0->allow_vrtp_remote && !(glue0->allow_vrtp_remote(c0, c1))) {
01522             /* if the allow_vrtp_remote indicates that remote isn't allowed, revert to local bridge */
01523             video_glue0_res = video_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
01524          }
01525       }
01526    }
01527 
01528    /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
01529    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)) {
01530       audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
01531    }
01532    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)) {
01533       audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
01534    }
01535 
01536    /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
01537    if (audio_glue0_res == AST_RTP_GLUE_RESULT_FORBID || audio_glue1_res == AST_RTP_GLUE_RESULT_FORBID) {
01538       res = AST_BRIDGE_FAILED_NOWARN;
01539       goto done;
01540    }
01541 
01542 
01543    /* If address families differ, force a local bridge */
01544    ast_rtp_instance_get_remote_address(instance0, &addr1);
01545    ast_rtp_instance_get_remote_address(instance1, &addr2);
01546 
01547    if (addr1.ss.ss_family != addr2.ss.ss_family ||
01548       (ast_sockaddr_is_ipv4_mapped(&addr1) != ast_sockaddr_is_ipv4_mapped(&addr2))) {
01549       audio_glue0_res = AST_RTP_GLUE_RESULT_LOCAL;
01550       audio_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
01551    }
01552 
01553    /* If we need to get DTMF see if we can do it outside of the RTP stream itself */
01554    dmode = ast_rtp_instance_dtmf_mode_get(instance0);
01555    if ((flags & AST_BRIDGE_DTMF_CHANNEL_0) && dmode) {
01556       res = AST_BRIDGE_FAILED_NOWARN;
01557       goto done;
01558    }
01559    dmode = ast_rtp_instance_dtmf_mode_get(instance1);
01560    if ((flags & AST_BRIDGE_DTMF_CHANNEL_1) && dmode) {
01561       res = AST_BRIDGE_FAILED_NOWARN;
01562       goto done;
01563    }
01564 
01565    /* 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 */
01566    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)))) {
01567       res = AST_BRIDGE_FAILED_NOWARN;
01568       goto done;
01569    }
01570 
01571    /* Make sure that codecs match */
01572    if (glue0->get_codec){
01573       glue0->get_codec(c0, cap0);
01574    }
01575    if (glue1->get_codec) {
01576       glue1->get_codec(c1, cap1);
01577    }
01578    if (!ast_format_cap_is_empty(cap0) && !ast_format_cap_is_empty(cap1) && !ast_format_cap_has_joint(cap0, cap1)) {
01579       char tmp0[256] = { 0, };
01580       char tmp1[256] = { 0, };
01581       ast_debug(1, "Channel codec0 = %s is not codec1 = %s, cannot native bridge in RTP.\n",
01582          ast_getformatname_multiple(tmp0, sizeof(tmp0), cap0),
01583          ast_getformatname_multiple(tmp1, sizeof(tmp1), cap1));
01584       res = AST_BRIDGE_FAILED_NOWARN;
01585       goto done;
01586    }
01587 
01588    read_ptime0 = (ast_codec_pref_getsize(&instance0->codecs.pref, ast_channel_rawreadformat(c0))).cur_ms;
01589    read_ptime1 = (ast_codec_pref_getsize(&instance1->codecs.pref, ast_channel_rawreadformat(c1))).cur_ms;
01590    write_ptime0 = (ast_codec_pref_getsize(&instance0->codecs.pref, ast_channel_rawwriteformat(c0))).cur_ms;
01591    write_ptime1 = (ast_codec_pref_getsize(&instance1->codecs.pref, ast_channel_rawwriteformat(c1))).cur_ms;
01592 
01593    if (read_ptime0 != write_ptime1 || read_ptime1 != write_ptime0) {
01594       ast_debug(1, "Packetization differs between RTP streams (%d != %d or %d != %d). Cannot native bridge in RTP\n",
01595             read_ptime0, write_ptime1, read_ptime1, write_ptime0);
01596       res = AST_BRIDGE_FAILED_NOWARN;
01597       goto done;
01598    }
01599 
01600    instance0->glue = glue0;
01601    instance1->glue = glue1;
01602    instance0->chan = c0;
01603    instance1->chan = c1;
01604 
01605    /* Depending on the end result for bridging either do a local bridge or remote bridge */
01606    if (audio_glue0_res == AST_RTP_GLUE_RESULT_LOCAL || audio_glue1_res == AST_RTP_GLUE_RESULT_LOCAL) {
01607       ast_verb(3, "Locally bridging %s and %s\n", ast_channel_name(c0), ast_channel_name(c1));
01608       res = local_bridge_loop(c0, c1, instance0, instance1, timeoutms, flags, fo, rc, ast_channel_tech_pvt(c0), ast_channel_tech_pvt(c1));
01609    } else {
01610       ast_verb(3, "Remotely bridging %s and %s\n", ast_channel_name(c0), ast_channel_name(c1));
01611       res = remote_bridge_loop(c0, c1, instance0, instance1, vinstance0, vinstance1,
01612             tinstance0, tinstance1, glue0, glue1, cap0, cap1, timeoutms, flags,
01613             fo, rc, ast_channel_tech_pvt(c0), ast_channel_tech_pvt(c1));
01614    }
01615 
01616    instance0->glue = NULL;
01617    instance1->glue = NULL;
01618    instance0->chan = NULL;
01619    instance1->chan = NULL;
01620 
01621    unlock_chans = 0;
01622 
01623 done:
01624    if (unlock_chans) {
01625       ast_channel_unlock(c0);
01626       ast_channel_unlock(c1);
01627    }
01628    ast_format_cap_destroy(cap1);
01629    ast_format_cap_destroy(cap0);
01630 
01631    unref_instance_cond(&instance0);
01632    unref_instance_cond(&instance1);
01633    unref_instance_cond(&vinstance0);
01634    unref_instance_cond(&vinstance1);
01635    unref_instance_cond(&tinstance0);
01636    unref_instance_cond(&tinstance1);
01637 
01638    return res;
01639 }
01640 
01641 struct ast_rtp_instance *ast_rtp_instance_get_bridged(struct ast_rtp_instance *instance)
01642 {
01643    return instance->bridged;
01644 }
01645 
01646 void ast_rtp_instance_early_bridge_make_compatible(struct ast_channel *c_dst, struct ast_channel *c_src)
01647 {
01648    struct ast_rtp_instance *instance_dst = NULL, *instance_src = NULL,
01649       *vinstance_dst = NULL, *vinstance_src = NULL,
01650       *tinstance_dst = NULL, *tinstance_src = NULL;
01651    struct ast_rtp_glue *glue_dst, *glue_src;
01652    enum ast_rtp_glue_result audio_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID, video_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID;
01653    enum ast_rtp_glue_result audio_glue_src_res = AST_RTP_GLUE_RESULT_FORBID, video_glue_src_res = AST_RTP_GLUE_RESULT_FORBID;
01654    struct ast_format_cap *cap_dst = ast_format_cap_alloc_nolock();
01655    struct ast_format_cap *cap_src = ast_format_cap_alloc_nolock();
01656 
01657    /* Lock both channels so we can look for the glue that binds them together */
01658    ast_channel_lock_both(c_dst, c_src);
01659 
01660    if (!cap_src || !cap_dst) {
01661       goto done;
01662    }
01663 
01664    /* Grab glue that binds each channel to something using the RTP engine */
01665    if (!(glue_dst = ast_rtp_instance_get_glue(ast_channel_tech(c_dst)->type)) || !(glue_src = ast_rtp_instance_get_glue(ast_channel_tech(c_src)->type))) {
01666       ast_debug(1, "Can't find native functions for channel '%s'\n", glue_dst ? ast_channel_name(c_src) : ast_channel_name(c_dst));
01667       goto done;
01668    }
01669 
01670    audio_glue_dst_res = glue_dst->get_rtp_info(c_dst, &instance_dst);
01671    video_glue_dst_res = glue_dst->get_vrtp_info ? glue_dst->get_vrtp_info(c_dst, &vinstance_dst) : AST_RTP_GLUE_RESULT_FORBID;
01672 
01673    audio_glue_src_res = glue_src->get_rtp_info(c_src, &instance_src);
01674    video_glue_src_res = glue_src->get_vrtp_info ? glue_src->get_vrtp_info(c_src, &vinstance_src) : AST_RTP_GLUE_RESULT_FORBID;
01675 
01676    /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
01677    if (video_glue_dst_res != AST_RTP_GLUE_RESULT_FORBID && (audio_glue_dst_res != AST_RTP_GLUE_RESULT_REMOTE || video_glue_dst_res != AST_RTP_GLUE_RESULT_REMOTE)) {
01678       audio_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID;
01679    }
01680    if (video_glue_src_res != AST_RTP_GLUE_RESULT_FORBID && (audio_glue_src_res != AST_RTP_GLUE_RESULT_REMOTE || video_glue_src_res != AST_RTP_GLUE_RESULT_REMOTE)) {
01681       audio_glue_src_res = AST_RTP_GLUE_RESULT_FORBID;
01682    }
01683    if (audio_glue_dst_res == AST_RTP_GLUE_RESULT_REMOTE && (video_glue_dst_res == AST_RTP_GLUE_RESULT_FORBID || video_glue_dst_res == AST_RTP_GLUE_RESULT_REMOTE) && glue_dst->get_codec) {
01684       glue_dst->get_codec(c_dst, cap_dst);
01685    }
01686    if (audio_glue_src_res == AST_RTP_GLUE_RESULT_REMOTE && (video_glue_src_res == AST_RTP_GLUE_RESULT_FORBID || video_glue_src_res == AST_RTP_GLUE_RESULT_REMOTE) && glue_src->get_codec) {
01687       glue_src->get_codec(c_src, cap_src);
01688    }
01689 
01690    /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
01691    if (audio_glue_dst_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue_src_res != AST_RTP_GLUE_RESULT_REMOTE) {
01692       goto done;
01693    }
01694 
01695    /* Make sure we have matching codecs */
01696    if (!ast_format_cap_has_joint(cap_dst, cap_src)) {
01697       goto done;
01698    }
01699 
01700    ast_rtp_codecs_payloads_copy(&instance_src->codecs, &instance_dst->codecs, instance_dst);
01701 
01702    if (vinstance_dst && vinstance_src) {
01703       ast_rtp_codecs_payloads_copy(&vinstance_src->codecs, &vinstance_dst->codecs, vinstance_dst);
01704    }
01705    if (tinstance_dst && tinstance_src) {
01706       ast_rtp_codecs_payloads_copy(&tinstance_src->codecs, &tinstance_dst->codecs, tinstance_dst);
01707    }
01708 
01709    if (glue_dst->update_peer(c_dst, instance_src, vinstance_src, tinstance_src, cap_src, 0)) {
01710       ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n",
01711          ast_channel_name(c_dst), ast_channel_name(c_src));
01712    } else {
01713       ast_debug(1, "Seeded SDP of '%s' with that of '%s'\n",
01714          ast_channel_name(c_dst), ast_channel_name(c_src));
01715    }
01716 
01717 done:
01718    ast_channel_unlock(c_dst);
01719    ast_channel_unlock(c_src);
01720 
01721    ast_format_cap_destroy(cap_dst);
01722    ast_format_cap_destroy(cap_src);
01723 
01724    unref_instance_cond(&instance_dst);
01725    unref_instance_cond(&instance_src);
01726    unref_instance_cond(&vinstance_dst);
01727    unref_instance_cond(&vinstance_src);
01728    unref_instance_cond(&tinstance_dst);
01729    unref_instance_cond(&tinstance_src);
01730 }
01731 
01732 int ast_rtp_instance_early_bridge(struct ast_channel *c0, struct ast_channel *c1)
01733 {
01734    struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
01735          *vinstance0 = NULL, *vinstance1 = NULL,
01736          *tinstance0 = NULL, *tinstance1 = NULL;
01737    struct ast_rtp_glue *glue0, *glue1;
01738    enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
01739    enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
01740    struct ast_format_cap *cap0 = ast_format_cap_alloc_nolock();
01741    struct ast_format_cap *cap1 = ast_format_cap_alloc_nolock();
01742    int res = 0;
01743 
01744    /* If there is no second channel just immediately bail out, we are of no use in that scenario */
01745    if (!c1) {
01746       ast_format_cap_destroy(cap0);
01747       ast_format_cap_destroy(cap1);
01748       return -1;
01749    }
01750 
01751    /* Lock both channels so we can look for the glue that binds them together */
01752    ast_channel_lock(c0);
01753    while (ast_channel_trylock(c1)) {
01754       ast_channel_unlock(c0);
01755       usleep(1);
01756       ast_channel_lock(c0);
01757    }
01758 
01759    if (!cap1 || !cap0) {
01760       goto done;
01761    }
01762 
01763    /* Grab glue that binds each channel to something using the RTP engine */
01764    if (!(glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) || !(glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type))) {
01765       ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", glue0 ? ast_channel_name(c1) : ast_channel_name(c0));
01766       goto done;
01767    }
01768 
01769    audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
01770    video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
01771 
01772    audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
01773    video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
01774 
01775    /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
01776    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)) {
01777       audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
01778    }
01779    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)) {
01780       audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
01781    }
01782    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) {
01783       glue0->get_codec(c0, cap0);
01784    }
01785    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) {
01786       glue1->get_codec(c1, cap1);
01787    }
01788 
01789    /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
01790    if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
01791       goto done;
01792    }
01793 
01794    /* Make sure we have matching codecs */
01795    if (!ast_format_cap_has_joint(cap0, cap1)) {
01796       goto done;
01797    }
01798 
01799    /* Bridge media early */
01800    if (glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0)) {
01801       ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
01802    }
01803 
01804    res = 0;
01805 
01806 done:
01807    ast_channel_unlock(c0);
01808    ast_channel_unlock(c1);
01809 
01810    ast_format_cap_destroy(cap0);
01811    ast_format_cap_destroy(cap1);
01812 
01813    unref_instance_cond(&instance0);
01814    unref_instance_cond(&instance1);
01815    unref_instance_cond(&vinstance0);
01816    unref_instance_cond(&vinstance1);
01817    unref_instance_cond(&tinstance0);
01818    unref_instance_cond(&tinstance1);
01819 
01820    if (!res) {
01821       ast_debug(1, "Setting early bridge SDP of '%s' with that of '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
01822    }
01823 
01824    return res;
01825 }
01826 
01827 int ast_rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations)
01828 {
01829    return instance->engine->red_init ? instance->engine->red_init(instance, buffer_time, payloads, generations) : -1;
01830 }
01831 
01832 int ast_rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame)
01833 {
01834    return instance->engine->red_buffer ? instance->engine->red_buffer(instance, frame) : -1;
01835 }
01836 
01837 int ast_rtp_instance_get_stats(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat)
01838 {
01839    return instance->engine->get_stat ? instance->engine->get_stat(instance, stats, stat) : -1;
01840 }
01841 
01842 char *ast_rtp_instance_get_quality(struct ast_rtp_instance *instance, enum ast_rtp_instance_stat_field field, char *buf, size_t size)
01843 {
01844    struct ast_rtp_instance_stats stats = { 0, };
01845    enum ast_rtp_instance_stat stat;
01846 
01847    /* Determine what statistics we will need to retrieve based on field passed in */
01848    if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
01849       stat = AST_RTP_INSTANCE_STAT_ALL;
01850    } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
01851       stat = AST_RTP_INSTANCE_STAT_COMBINED_JITTER;
01852    } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
01853       stat = AST_RTP_INSTANCE_STAT_COMBINED_LOSS;
01854    } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
01855       stat = AST_RTP_INSTANCE_STAT_COMBINED_RTT;
01856    } else {
01857       return NULL;
01858    }
01859 
01860    /* Attempt to actually retrieve the statistics we need to generate the quality string */
01861    if (ast_rtp_instance_get_stats(instance, &stats, stat)) {
01862       return NULL;
01863    }
01864 
01865    /* Now actually fill the buffer with the good information */
01866    if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
01867       snprintf(buf, size, "ssrc=%i;themssrc=%u;lp=%u;rxjitter=%f;rxcount=%u;txjitter=%f;txcount=%u;rlp=%u;rtt=%f",
01868           stats.local_ssrc, stats.remote_ssrc, stats.rxploss, stats.rxjitter, stats.rxcount, stats.txjitter, stats.txcount, stats.txploss, stats.rtt);
01869    } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
01870       snprintf(buf, size, "minrxjitter=%f;maxrxjitter=%f;avgrxjitter=%f;stdevrxjitter=%f;reported_minjitter=%f;reported_maxjitter=%f;reported_avgjitter=%f;reported_stdevjitter=%f;",
01871           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));
01872    } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
01873       snprintf(buf, size, "minrxlost=%f;maxrxlost=%f;avgrxlost=%f;stdevrxlost=%f;reported_minlost=%f;reported_maxlost=%f;reported_avglost=%f;reported_stdevlost=%f;",
01874           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));
01875    } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
01876       snprintf(buf, size, "minrtt=%f;maxrtt=%f;avgrtt=%f;stdevrtt=%f;", stats.minrtt, stats.maxrtt, stats.normdevrtt, stats.stdevrtt);
01877    }
01878 
01879    return buf;
01880 }
01881 
01882 void ast_rtp_instance_set_stats_vars(struct ast_channel *chan, struct ast_rtp_instance *instance)
01883 {
01884    char quality_buf[AST_MAX_USER_FIELD], *quality;
01885    struct ast_channel *bridge = ast_bridged_channel(chan);
01886 
01887    if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
01888       pbx_builtin_setvar_helper(chan, "RTPAUDIOQOS", quality);
01889       if (bridge) {
01890          pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSBRIDGED", quality);
01891       }
01892    }
01893 
01894    if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER, quality_buf, sizeof(quality_buf)))) {
01895       pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSJITTER", quality);
01896       if (bridge) {
01897          pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSJITTERBRIDGED", quality);
01898       }
01899    }
01900 
01901    if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS, quality_buf, sizeof(quality_buf)))) {
01902       pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSLOSS", quality);
01903       if (bridge) {
01904          pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSLOSSBRIDGED", quality);
01905       }
01906    }
01907 
01908    if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT, quality_buf, sizeof(quality_buf)))) {
01909       pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSRTT", quality);
01910       if (bridge) {
01911          pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSRTTBRIDGED", quality);
01912       }
01913    }
01914 }
01915 
01916 int ast_rtp_instance_set_read_format(struct ast_rtp_instance *instance, struct ast_format *format)
01917 {
01918    return instance->engine->set_read_format ? instance->engine->set_read_format(instance, format) : -1;
01919 }
01920 
01921 int ast_rtp_instance_set_write_format(struct ast_rtp_instance *instance, struct ast_format *format)
01922 {
01923    return instance->engine->set_write_format ? instance->engine->set_write_format(instance, format) : -1;
01924 }
01925 
01926 int ast_rtp_instance_make_compatible(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_channel *peer)
01927 {
01928    struct ast_rtp_glue *glue;
01929    struct ast_rtp_instance *peer_instance = NULL;
01930    int res = -1;
01931 
01932    if (!instance->engine->make_compatible) {
01933       return -1;
01934    }
01935 
01936    ast_channel_lock(peer);
01937 
01938    if (!(glue = ast_rtp_instance_get_glue(ast_channel_tech(peer)->type))) {
01939       ast_channel_unlock(peer);
01940       return -1;
01941    }
01942 
01943    glue->get_rtp_info(peer, &peer_instance);
01944    if (!peer_instance) {
01945       ast_log(LOG_ERROR, "Unable to get_rtp_info for peer type %s\n", glue->type);
01946       ast_channel_unlock(peer);
01947       return -1;
01948    }
01949    if (peer_instance->engine != instance->engine) {
01950       ast_log(LOG_ERROR, "Peer engine mismatch for type %s\n", glue->type);
01951       ast_channel_unlock(peer);
01952       ao2_ref(peer_instance, -1);
01953       return -1;
01954    }
01955 
01956    res = instance->engine->make_compatible(chan, instance, peer, peer_instance);
01957 
01958    ast_channel_unlock(peer);
01959 
01960    ao2_ref(peer_instance, -1);
01961    peer_instance = NULL;
01962 
01963    return res;
01964 }
01965 
01966 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)
01967 {
01968    if (instance->engine->available_formats) {
01969       instance->engine->available_formats(instance, to_endpoint, to_asterisk, result);
01970       if (!ast_format_cap_is_empty(result)) {
01971          return;
01972       }
01973    }
01974 
01975    ast_translate_available_formats(to_endpoint, to_asterisk, result);
01976 }
01977 
01978 int ast_rtp_instance_activate(struct ast_rtp_instance *instance)
01979 {
01980    return instance->engine->activate ? instance->engine->activate(instance) : 0;
01981 }
01982 
01983 void ast_rtp_instance_stun_request(struct ast_rtp_instance *instance,
01984                struct ast_sockaddr *suggestion,
01985                const char *username)
01986 {
01987    if (instance->engine->stun_request) {
01988       instance->engine->stun_request(instance, suggestion, username);
01989    }
01990 }
01991 
01992 void ast_rtp_instance_set_timeout(struct ast_rtp_instance *instance, int timeout)
01993 {
01994    instance->timeout = timeout;
01995 }
01996 
01997 void ast_rtp_instance_set_hold_timeout(struct ast_rtp_instance *instance, int timeout)
01998 {
01999    instance->holdtimeout = timeout;
02000 }
02001 
02002 void ast_rtp_instance_set_keepalive(struct ast_rtp_instance *instance, int interval)
02003 {
02004    instance->keepalive = interval;
02005 }
02006 
02007 int ast_rtp_instance_get_timeout(struct ast_rtp_instance *instance)
02008 {
02009    return instance->timeout;
02010 }
02011 
02012 int ast_rtp_instance_get_hold_timeout(struct ast_rtp_instance *instance)
02013 {
02014    return instance->holdtimeout;
02015 }
02016 
02017 int ast_rtp_instance_get_keepalive(struct ast_rtp_instance *instance)
02018 {
02019    return instance->keepalive;
02020 }
02021 
02022 struct ast_rtp_engine *ast_rtp_instance_get_engine(struct ast_rtp_instance *instance)
02023 {
02024    return instance->engine;
02025 }
02026 
02027 struct ast_rtp_glue *ast_rtp_instance_get_active_glue(struct ast_rtp_instance *instance)
02028 {
02029    return instance->glue;
02030 }
02031 
02032 struct ast_channel *ast_rtp_instance_get_chan(struct ast_rtp_instance *instance)
02033 {
02034    return instance->chan;
02035 }
02036 
02037 int ast_rtp_engine_register_srtp(struct ast_srtp_res *srtp_res, struct ast_srtp_policy_res *policy_res)
02038 {
02039    if (res_srtp || res_srtp_policy) {
02040       return -1;
02041    }
02042    if (!srtp_res || !policy_res) {
02043       return -1;
02044    }
02045 
02046    res_srtp = srtp_res;
02047    res_srtp_policy = policy_res;
02048 
02049    return 0;
02050 }
02051 
02052 void ast_rtp_engine_unregister_srtp(void)
02053 {
02054    res_srtp = NULL;
02055    res_srtp_policy = NULL;
02056 }
02057 
02058 int ast_rtp_engine_srtp_is_registered(void)
02059 {
02060    return res_srtp && res_srtp_policy;
02061 }
02062 
02063 int ast_rtp_instance_add_srtp_policy(struct ast_rtp_instance *instance, struct ast_srtp_policy *remote_policy, struct ast_srtp_policy *local_policy)
02064 {
02065    int res = 0;
02066 
02067    if (!res_srtp) {
02068       return -1;
02069    }
02070 
02071    if (!instance->srtp) {
02072       res = res_srtp->create(&instance->srtp, instance, remote_policy);
02073    } else {
02074       res = res_srtp->replace(&instance->srtp, instance, remote_policy);
02075    }
02076    if (!res) {
02077       res = res_srtp->add_stream(instance->srtp, local_policy);
02078    }
02079 
02080    return res;
02081 }
02082 
02083 struct ast_srtp *ast_rtp_instance_get_srtp(struct ast_rtp_instance *instance)
02084 {
02085    return instance->srtp;
02086 }
02087 
02088 int ast_rtp_instance_sendcng(struct ast_rtp_instance *instance, int level)
02089 {
02090    if (instance->engine->sendcng) {
02091       return instance->engine->sendcng(instance, level);
02092    }
02093 
02094    return -1;
02095 }
02096 
02097 struct ast_rtp_engine_ice *ast_rtp_instance_get_ice(struct ast_rtp_instance *instance)
02098 {
02099    return instance->engine->ice;
02100 }
02101 
02102 struct ast_rtp_engine_dtls *ast_rtp_instance_get_dtls(struct ast_rtp_instance *instance)
02103 {
02104    return instance->engine->dtls;
02105 }
02106 
02107 int ast_rtp_dtls_cfg_parse(struct ast_rtp_dtls_cfg *dtls_cfg, const char *name, const char *value)
02108 {
02109    if (!strcasecmp(name, "dtlsenable")) {
02110       dtls_cfg->enabled = ast_true(value) ? 1 : 0;
02111    } else if (!strcasecmp(name, "dtlsverify")) {
02112       dtls_cfg->verify = ast_true(value) ? 1 : 0;
02113    } else if (!strcasecmp(name, "dtlsrekey")) {
02114       if (sscanf(value, "%30u", &dtls_cfg->rekey) != 1) {
02115          return -1;
02116       }
02117    } else if (!strcasecmp(name, "dtlscertfile")) {
02118       ast_free(dtls_cfg->certfile);
02119       dtls_cfg->certfile = ast_strdup(value);
02120    } else if (!strcasecmp(name, "dtlsprivatekey")) {
02121       ast_free(dtls_cfg->pvtfile);
02122       dtls_cfg->pvtfile = ast_strdup(value);
02123    } else if (!strcasecmp(name, "dtlscipher")) {
02124       ast_free(dtls_cfg->cipher);
02125       dtls_cfg->cipher = ast_strdup(value);
02126    } else if (!strcasecmp(name, "dtlscafile")) {
02127       ast_free(dtls_cfg->cafile);
02128       dtls_cfg->cafile = ast_strdup(value);
02129    } else if (!strcasecmp(name, "dtlscapath") || !strcasecmp(name, "dtlscadir")) {
02130       ast_free(dtls_cfg->capath);
02131       dtls_cfg->capath = ast_strdup(value);
02132    } else if (!strcasecmp(name, "dtlssetup")) {
02133       if (!strcasecmp(value, "active")) {
02134          dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_ACTIVE;
02135       } else if (!strcasecmp(value, "passive")) {
02136          dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_PASSIVE;
02137       } else if (!strcasecmp(value, "actpass")) {
02138          dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_ACTPASS;
02139       }
02140    } else {
02141       return -1;
02142    }
02143 
02144    return 0;
02145 }
02146 
02147 void ast_rtp_dtls_cfg_copy(const struct ast_rtp_dtls_cfg *src_cfg, struct ast_rtp_dtls_cfg *dst_cfg)
02148 {
02149    dst_cfg->enabled = src_cfg->enabled;
02150    dst_cfg->verify = src_cfg->verify;
02151    dst_cfg->rekey = src_cfg->rekey;
02152    dst_cfg->suite = src_cfg->suite;
02153    dst_cfg->certfile = ast_strdup(src_cfg->certfile);
02154    dst_cfg->pvtfile = ast_strdup(src_cfg->pvtfile);
02155    dst_cfg->cipher = ast_strdup(src_cfg->cipher);
02156    dst_cfg->cafile = ast_strdup(src_cfg->cafile);
02157    dst_cfg->capath = ast_strdup(src_cfg->capath);
02158    dst_cfg->default_setup = src_cfg->default_setup;
02159 }
02160 
02161 void ast_rtp_dtls_cfg_free(struct ast_rtp_dtls_cfg *dtls_cfg)
02162 {
02163    ast_free(dtls_cfg->certfile);
02164    ast_free(dtls_cfg->pvtfile);
02165    ast_free(dtls_cfg->cipher);
02166    ast_free(dtls_cfg->cafile);
02167    ast_free(dtls_cfg->capath);
02168 }
02169 
02170 static void set_next_mime_type(const struct ast_format *format, int rtp_code, char *type, char *subtype, unsigned int sample_rate)
02171 {
02172    int x = mime_types_len;
02173    if (ARRAY_LEN(ast_rtp_mime_types) == mime_types_len) {
02174       return;
02175    }
02176 
02177    ast_rwlock_wrlock(&mime_types_lock);
02178    if (format) {
02179       ast_rtp_mime_types[x].payload_type.asterisk_format = 1;
02180       ast_format_copy(&ast_rtp_mime_types[x].payload_type.format, format);
02181    } else {
02182       ast_rtp_mime_types[x].payload_type.rtp_code = rtp_code;
02183    }
02184    ast_rtp_mime_types[x].type = type;
02185    ast_rtp_mime_types[x].subtype = subtype;
02186    ast_rtp_mime_types[x].sample_rate = sample_rate;
02187    mime_types_len++;
02188    ast_rwlock_unlock(&mime_types_lock);
02189 }
02190 
02191 static void add_static_payload(int map, const struct ast_format *format, int rtp_code)
02192 {
02193    int x;
02194    ast_rwlock_wrlock(&static_RTP_PT_lock);
02195    if (map < 0) {
02196       /* find next available dynamic payload slot */
02197       for (x = 96; x < 127; x++) {
02198          if (!static_RTP_PT[x].asterisk_format && !static_RTP_PT[x].rtp_code) {
02199             map = x;
02200             break;
02201          }
02202       }
02203    }
02204 
02205    if (map < 0) {
02206       ast_log(LOG_WARNING, "No Dynamic RTP mapping available for format %s\n" ,ast_getformatname(format));
02207       ast_rwlock_unlock(&static_RTP_PT_lock);
02208       return;
02209    }
02210 
02211    if (format) {
02212       static_RTP_PT[map].asterisk_format = 1;
02213       ast_format_copy(&static_RTP_PT[map].format, format);
02214    } else {
02215       static_RTP_PT[map].rtp_code = rtp_code;
02216    }
02217    ast_rwlock_unlock(&static_RTP_PT_lock);
02218 }
02219 
02220 int ast_rtp_engine_load_format(const struct ast_format *format)
02221 {
02222    switch (format->id) {
02223    case AST_FORMAT_SILK:
02224       set_next_mime_type(format, 0, "audio", "SILK", ast_format_rate(format));
02225       add_static_payload(-1, format, 0);
02226       break;
02227    case AST_FORMAT_CELT:
02228       set_next_mime_type(format, 0, "audio", "CELT", ast_format_rate(format));
02229       add_static_payload(-1, format, 0);
02230       break;
02231    default:
02232       break;
02233    }
02234 
02235    return 0;
02236 }
02237 
02238 int ast_rtp_engine_unload_format(const struct ast_format *format)
02239 {
02240    int x;
02241    int y = 0;
02242 
02243    ast_rwlock_wrlock(&static_RTP_PT_lock);
02244    /* remove everything pertaining to this format id from the lists */
02245    for (x = 0; x < AST_RTP_MAX_PT; x++) {
02246       if (ast_format_cmp(&static_RTP_PT[x].format, format) == AST_FORMAT_CMP_EQUAL) {
02247          memset(&static_RTP_PT[x], 0, sizeof(struct ast_rtp_payload_type));
02248       }
02249    }
02250    ast_rwlock_unlock(&static_RTP_PT_lock);
02251 
02252 
02253    ast_rwlock_wrlock(&mime_types_lock);
02254    /* rebuild the list skipping the items matching this id */
02255    for (x = 0; x < mime_types_len; x++) {
02256       if (ast_format_cmp(&ast_rtp_mime_types[x].payload_type.format, format) == AST_FORMAT_CMP_EQUAL) {
02257          continue;
02258       }
02259       ast_rtp_mime_types[y] = ast_rtp_mime_types[x];
02260       y++;
02261    }
02262    mime_types_len = y;
02263    ast_rwlock_unlock(&mime_types_lock);
02264    return 0;
02265 }
02266 
02267 int ast_rtp_engine_init()
02268 {
02269    struct ast_format tmpfmt;
02270 
02271    ast_rwlock_init(&mime_types_lock);
02272    ast_rwlock_init(&static_RTP_PT_lock);
02273 
02274    /* Define all the RTP mime types available */
02275    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G723_1, 0), 0, "audio", "G723", 8000);
02276    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_GSM, 0), 0, "audio", "GSM", 8000);
02277    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0, "audio", "PCMU", 8000);
02278    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0, "audio", "G711U", 8000);
02279    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), 0, "audio", "PCMA", 8000);
02280    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), 0, "audio", "G711A", 8000);
02281    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G726, 0), 0, "audio", "G726-32", 8000);
02282    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0, "audio", "DVI4", 8000);
02283    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), 0, "audio", "L16", 8000);
02284    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0, "audio", "L16", 16000);
02285    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0, "audio", "L16-256", 16000);
02286    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_LPC10, 0), 0, "audio", "LPC", 8000);
02287    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0, "audio", "G729", 8000);
02288    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0, "audio", "G729A", 8000);
02289    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0, "audio", "G.729", 8000);
02290    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX, 0), 0, "audio", "speex", 8000);
02291    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX16, 0), 0,  "audio", "speex", 16000);
02292    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX32, 0), 0,  "audio", "speex", 32000);
02293    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ILBC, 0), 0, "audio", "iLBC", 8000);
02294    /* this is the sample rate listed in the RTP profile for the G.722 codec, *NOT* the actual sample rate of the media stream */
02295    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G722, 0), 0, "audio", "G722", 8000);
02296    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G726_AAL2, 0), 0, "audio", "AAL2-G726-32", 8000);
02297    set_next_mime_type(NULL, AST_RTP_DTMF, "audio", "telephone-event", 8000);
02298    set_next_mime_type(NULL, AST_RTP_CISCO_DTMF, "audio", "cisco-telephone-event", 8000);
02299    set_next_mime_type(NULL, AST_RTP_CN, "audio", "CN", 8000);
02300    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_JPEG, 0), 0, "video", "JPEG", 90000);
02301    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_PNG, 0), 0, "video", "PNG", 90000);
02302    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H261, 0), 0, "video", "H261", 90000);
02303    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H263, 0), 0, "video", "H263", 90000);
02304    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0, "video", "h263-1998", 90000);
02305    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H264, 0), 0, "video", "H264", 90000);
02306    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_MP4_VIDEO, 0), 0, "video", "MP4V-ES", 90000);
02307    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_T140RED, 0), 0, "text", "RED", 1000);
02308    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_T140, 0), 0, "text", "T140", 1000);
02309    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SIREN7, 0), 0, "audio", "G7221", 16000);
02310    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SIREN14, 0), 0, "audio", "G7221", 32000);
02311    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G719, 0), 0, "audio", "G719", 48000);
02312 
02313    /* Define the static rtp payload mappings */
02314    add_static_payload(0, ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0);
02315    #ifdef USE_DEPRECATED_G726
02316    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... */
02317    #endif
02318    add_static_payload(3, ast_format_set(&tmpfmt, AST_FORMAT_GSM, 0), 0);
02319    add_static_payload(4, ast_format_set(&tmpfmt, AST_FORMAT_G723_1, 0), 0);
02320    add_static_payload(5, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0);/* 8 kHz */
02321    add_static_payload(6, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0); /* 16 kHz */
02322    add_static_payload(7, ast_format_set(&tmpfmt, AST_FORMAT_LPC10, 0), 0);
02323    add_static_payload(8, ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), 0);
02324    add_static_payload(9, ast_format_set(&tmpfmt, AST_FORMAT_G722, 0), 0);
02325    add_static_payload(10, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), 0); /* 2 channels */
02326    add_static_payload(11, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), 0); /* 1 channel */
02327    add_static_payload(13, NULL, AST_RTP_CN);
02328    add_static_payload(16, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0); /* 11.025 kHz */
02329    add_static_payload(17, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0); /* 22.050 kHz */
02330    add_static_payload(18, ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0);
02331    add_static_payload(19, NULL, AST_RTP_CN);         /* Also used for CN */
02332    add_static_payload(26, ast_format_set(&tmpfmt, AST_FORMAT_JPEG, 0), 0);
02333    add_static_payload(31, ast_format_set(&tmpfmt, AST_FORMAT_H261, 0), 0);
02334    add_static_payload(34, ast_format_set(&tmpfmt, AST_FORMAT_H263, 0), 0);
02335    add_static_payload(97, ast_format_set(&tmpfmt, AST_FORMAT_ILBC, 0), 0);
02336    add_static_payload(98, ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0);
02337    add_static_payload(99, ast_format_set(&tmpfmt, AST_FORMAT_H264, 0), 0);
02338    add_static_payload(101, NULL, AST_RTP_DTMF);
02339    add_static_payload(102, ast_format_set(&tmpfmt, AST_FORMAT_SIREN7, 0), 0);
02340    add_static_payload(103, ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0);
02341    add_static_payload(104, ast_format_set(&tmpfmt, AST_FORMAT_MP4_VIDEO, 0), 0);
02342    add_static_payload(105, ast_format_set(&tmpfmt, AST_FORMAT_T140RED, 0), 0);   /* Real time text chat (with redundancy encoding) */
02343    add_static_payload(106, ast_format_set(&tmpfmt, AST_FORMAT_T140, 0), 0);     /* Real time text chat */
02344    add_static_payload(110, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX, 0), 0);
02345    add_static_payload(111, ast_format_set(&tmpfmt, AST_FORMAT_G726, 0), 0);
02346    add_static_payload(112, ast_format_set(&tmpfmt, AST_FORMAT_G726_AAL2, 0), 0);
02347    add_static_payload(115, ast_format_set(&tmpfmt, AST_FORMAT_SIREN14, 0), 0);
02348    add_static_payload(116, ast_format_set(&tmpfmt, AST_FORMAT_G719, 0), 0);
02349    add_static_payload(117, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX16, 0), 0);
02350    add_static_payload(118, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0); /* 16 Khz signed linear */
02351    add_static_payload(119, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX32, 0), 0);
02352    add_static_payload(121, NULL, AST_RTP_CISCO_DTMF);   /* Must be type 121 */
02353 
02354    return 0;
02355 }