Thu Oct 11 06:33:55 2012

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: 374414 $")
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 int ast_rtp_codecs_payload_code(struct ast_rtp_codecs *codecs, int asterisk_format, const struct ast_format *format, int code)
00735 {
00736    struct ast_rtp_payload_type *type;
00737    int i, res = -1;
00738 
00739    if (asterisk_format && format && (type = ao2_callback(codecs->payloads, OBJ_NOLOCK, rtp_payload_type_find_format, (void*)format))) {
00740       res = type->payload;
00741       ao2_ref(type, -1);
00742       return res;
00743    } else if (!asterisk_format && (type = ao2_find(codecs->payloads, &code, OBJ_NOLOCK | OBJ_KEY))) {
00744       res = type->payload;
00745       ao2_ref(type, -1);
00746       return res;
00747    }
00748 
00749    ast_rwlock_rdlock(&static_RTP_PT_lock);
00750    for (i = 0; i < AST_RTP_MAX_PT; i++) {
00751       if (static_RTP_PT[i].asterisk_format && asterisk_format && format &&
00752          (ast_format_cmp(format, &static_RTP_PT[i].format) != AST_FORMAT_CMP_NOT_EQUAL)) {
00753          res = i;
00754          break;
00755       } else if (!static_RTP_PT[i].asterisk_format && !asterisk_format &&
00756          (static_RTP_PT[i].rtp_code == code)) {
00757          res = i;
00758          break;
00759       }
00760    }
00761    ast_rwlock_unlock(&static_RTP_PT_lock);
00762 
00763    return res;
00764 }
00765 int ast_rtp_codecs_find_payload_code(struct ast_rtp_codecs *codecs, int code)
00766 {
00767    struct ast_rtp_payload_type *type;
00768    int res = -1;
00769 
00770    /* Search the payload type in the codecs passed */
00771    if ((type = ao2_find(codecs->payloads, &code, OBJ_NOLOCK | OBJ_KEY)))
00772    {
00773       res = type->payload;
00774       ao2_ref(type, -1);
00775       return res;
00776    }
00777 
00778    return res;
00779 }
00780 const char *ast_rtp_lookup_mime_subtype2(const int asterisk_format, struct ast_format *format, int code, enum ast_rtp_options options)
00781 {
00782    int i;
00783    const char *res = "";
00784 
00785    ast_rwlock_rdlock(&mime_types_lock);
00786    for (i = 0; i < mime_types_len; i++) {
00787       if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
00788          (ast_format_cmp(format, &ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
00789          if ((format->id == AST_FORMAT_G726_AAL2) && (options & AST_RTP_OPT_G726_NONSTANDARD)) {
00790             res = "G726-32";
00791             break;
00792          } else {
00793             res = ast_rtp_mime_types[i].subtype;
00794             break;
00795          }
00796       } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
00797          ast_rtp_mime_types[i].payload_type.rtp_code == code) {
00798 
00799          res = ast_rtp_mime_types[i].subtype;
00800          break;
00801       }
00802    }
00803    ast_rwlock_unlock(&mime_types_lock);
00804 
00805    return res;
00806 }
00807 
00808 unsigned int ast_rtp_lookup_sample_rate2(int asterisk_format, struct ast_format *format, int code)
00809 {
00810    unsigned int i;
00811    unsigned int res = 0;
00812 
00813    ast_rwlock_rdlock(&mime_types_lock);
00814    for (i = 0; i < mime_types_len; ++i) {
00815       if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
00816          (ast_format_cmp(format, &ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
00817          res = ast_rtp_mime_types[i].sample_rate;
00818          break;
00819       } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
00820          ast_rtp_mime_types[i].payload_type.rtp_code == code) {
00821          res = ast_rtp_mime_types[i].sample_rate;
00822          break;
00823       }
00824    }
00825    ast_rwlock_unlock(&mime_types_lock);
00826 
00827    return res;
00828 }
00829 
00830 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)
00831 {
00832    int found = 0;
00833    const char *name;
00834    if (!buf) {
00835       return NULL;
00836    }
00837 
00838 
00839    if (asterisk_format) {
00840       struct ast_format tmp_fmt;
00841       ast_format_cap_iter_start(ast_format_capability);
00842       while (!ast_format_cap_iter_next(ast_format_capability, &tmp_fmt)) {
00843          name = ast_rtp_lookup_mime_subtype2(asterisk_format, &tmp_fmt, 0, options);
00844          ast_str_append(&buf, 0, "%s|", name);
00845          found = 1;
00846       }
00847       ast_format_cap_iter_end(ast_format_capability);
00848 
00849    } else {
00850       int x;
00851       ast_str_append(&buf, 0, "0x%x (", (unsigned int) rtp_capability);
00852       for (x = 1; x < AST_RTP_MAX; x <<= 1) {
00853          if (rtp_capability & x) {
00854             name = ast_rtp_lookup_mime_subtype2(asterisk_format, NULL, x, options);
00855             ast_str_append(&buf, 0, "%s|", name);
00856             found = 1;
00857          }
00858       }
00859    }
00860 
00861    ast_str_append(&buf, 0, "%s", found ? ")" : "nothing)");
00862 
00863    return ast_str_buffer(buf);
00864 }
00865 
00866 void ast_rtp_codecs_packetization_set(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, struct ast_codec_pref *prefs)
00867 {
00868    codecs->pref = *prefs;
00869 
00870    if (instance && instance->engine->packetization_set) {
00871       instance->engine->packetization_set(instance, &instance->codecs.pref);
00872    }
00873 }
00874 
00875 int ast_rtp_instance_dtmf_begin(struct ast_rtp_instance *instance, char digit)
00876 {
00877    return instance->engine->dtmf_begin ? instance->engine->dtmf_begin(instance, digit) : -1;
00878 }
00879 
00880 int ast_rtp_instance_dtmf_end(struct ast_rtp_instance *instance, char digit)
00881 {
00882    return instance->engine->dtmf_end ? instance->engine->dtmf_end(instance, digit) : -1;
00883 }
00884 int ast_rtp_instance_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration)
00885 {
00886    return instance->engine->dtmf_end_with_duration ? instance->engine->dtmf_end_with_duration(instance, digit, duration) : -1;
00887 }
00888 
00889 int ast_rtp_instance_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode)
00890 {
00891    return (!instance->engine->dtmf_mode_set || instance->engine->dtmf_mode_set(instance, dtmf_mode)) ? -1 : 0;
00892 }
00893 
00894 enum ast_rtp_dtmf_mode ast_rtp_instance_dtmf_mode_get(struct ast_rtp_instance *instance)
00895 {
00896    return instance->engine->dtmf_mode_get ? instance->engine->dtmf_mode_get(instance) : 0;
00897 }
00898 
00899 void ast_rtp_instance_update_source(struct ast_rtp_instance *instance)
00900 {
00901    if (instance->engine->update_source) {
00902       instance->engine->update_source(instance);
00903    }
00904 }
00905 
00906 void ast_rtp_instance_change_source(struct ast_rtp_instance *instance)
00907 {
00908    if (instance->engine->change_source) {
00909       instance->engine->change_source(instance);
00910    }
00911 }
00912 
00913 int ast_rtp_instance_set_qos(struct ast_rtp_instance *instance, int tos, int cos, const char *desc)
00914 {
00915    return instance->engine->qos ? instance->engine->qos(instance, tos, cos, desc) : -1;
00916 }
00917 
00918 void ast_rtp_instance_stop(struct ast_rtp_instance *instance)
00919 {
00920    if (instance->engine->stop) {
00921       instance->engine->stop(instance);
00922    }
00923 }
00924 
00925 int ast_rtp_instance_fd(struct ast_rtp_instance *instance, int rtcp)
00926 {
00927    return instance->engine->fd ? instance->engine->fd(instance, rtcp) : -1;
00928 }
00929 
00930 struct ast_rtp_glue *ast_rtp_instance_get_glue(const char *type)
00931 {
00932    struct ast_rtp_glue *glue = NULL;
00933 
00934    AST_RWLIST_RDLOCK(&glues);
00935 
00936    AST_RWLIST_TRAVERSE(&glues, glue, entry) {
00937       if (!strcasecmp(glue->type, type)) {
00938          break;
00939       }
00940    }
00941 
00942    AST_RWLIST_UNLOCK(&glues);
00943 
00944    return glue;
00945 }
00946 
00947 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)
00948 {
00949    enum ast_bridge_result res = AST_BRIDGE_FAILED;
00950    struct ast_channel *who = NULL, *other = NULL, *cs[3] = { NULL, };
00951    struct ast_frame *fr = NULL;
00952 
00953    /* Start locally bridging both instances */
00954    if (instance0->engine->local_bridge && instance0->engine->local_bridge(instance0, instance1)) {
00955       ast_debug(1, "Failed to locally bridge %s to %s, backing out.\n", ast_channel_name(c0), ast_channel_name(c1));
00956       ast_channel_unlock(c0);
00957       ast_channel_unlock(c1);
00958       return AST_BRIDGE_FAILED_NOWARN;
00959    }
00960    if (instance1->engine->local_bridge && instance1->engine->local_bridge(instance1, instance0)) {
00961       ast_debug(1, "Failed to locally bridge %s to %s, backing out.\n", ast_channel_name(c1), ast_channel_name(c0));
00962       if (instance0->engine->local_bridge) {
00963          instance0->engine->local_bridge(instance0, NULL);
00964       }
00965       ast_channel_unlock(c0);
00966       ast_channel_unlock(c1);
00967       return AST_BRIDGE_FAILED_NOWARN;
00968    }
00969 
00970    ast_channel_unlock(c0);
00971    ast_channel_unlock(c1);
00972 
00973    instance0->bridged = instance1;
00974    instance1->bridged = instance0;
00975 
00976    ast_poll_channel_add(c0, c1);
00977 
00978    /* Hop into a loop waiting for a frame from either channel */
00979    cs[0] = c0;
00980    cs[1] = c1;
00981    cs[2] = NULL;
00982    for (;;) {
00983       /* If the underlying formats have changed force this bridge to break */
00984       if ((ast_format_cmp(ast_channel_rawreadformat(c0), ast_channel_rawwriteformat(c1)) == AST_FORMAT_CMP_NOT_EQUAL) ||
00985          (ast_format_cmp(ast_channel_rawreadformat(c1), ast_channel_rawwriteformat(c0)) == AST_FORMAT_CMP_NOT_EQUAL)) {
00986          ast_debug(1, "rtp-engine-local-bridge: Oooh, formats changed, backing out\n");
00987          res = AST_BRIDGE_FAILED_NOWARN;
00988          break;
00989       }
00990       /* Check if anything changed */
00991       if ((ast_channel_tech_pvt(c0) != pvt0) ||
00992           (ast_channel_tech_pvt(c1) != pvt1) ||
00993           (ast_channel_masq(c0) || ast_channel_masqr(c0) || ast_channel_masq(c1) || ast_channel_masqr(c1)) ||
00994           (ast_channel_monitor(c0) || ast_channel_audiohooks(c0) || ast_channel_monitor(c1) || ast_channel_audiohooks(c1)) ||
00995           (!ast_framehook_list_is_empty(ast_channel_framehooks(c0)) || !ast_framehook_list_is_empty(ast_channel_framehooks(c1)))) {
00996          ast_debug(1, "rtp-engine-local-bridge: Oooh, something is weird, backing out\n");
00997          /* 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 */
00998          if ((ast_channel_masq(c0) || ast_channel_masqr(c0)) && (fr = ast_read(c0))) {
00999             ast_frfree(fr);
01000          }
01001          if ((ast_channel_masq(c1) || ast_channel_masqr(c1)) && (fr = ast_read(c1))) {
01002             ast_frfree(fr);
01003          }
01004          res = AST_BRIDGE_RETRY;
01005          break;
01006       }
01007       /* Wait on a channel to feed us a frame */
01008       if (!(who = ast_waitfor_n(cs, 2, &timeoutms))) {
01009          if (!timeoutms) {
01010             res = AST_BRIDGE_RETRY;
01011             break;
01012          }
01013          ast_debug(2, "rtp-engine-local-bridge: Ooh, empty read...\n");
01014          if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
01015             break;
01016          }
01017          continue;
01018       }
01019       /* Read in frame from channel */
01020       fr = ast_read(who);
01021       other = (who == c0) ? c1 : c0;
01022       /* Depending on the frame we may need to break out of our bridge */
01023       if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
01024              ((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) |
01025              ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1)))) {
01026          /* Record received frame and who */
01027          *fo = fr;
01028          *rc = who;
01029          ast_debug(1, "rtp-engine-local-bridge: Ooh, got a %s\n", fr ? "digit" : "hangup");
01030          res = AST_BRIDGE_COMPLETE;
01031          break;
01032       } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
01033          if ((fr->subclass.integer == AST_CONTROL_HOLD) ||
01034              (fr->subclass.integer == AST_CONTROL_UNHOLD) ||
01035              (fr->subclass.integer == AST_CONTROL_VIDUPDATE) ||
01036              (fr->subclass.integer == AST_CONTROL_SRCUPDATE) ||
01037              (fr->subclass.integer == AST_CONTROL_T38_PARAMETERS) ||
01038              (fr->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER)) {
01039             /* If we are going on hold, then break callback mode and P2P bridging */
01040             if (fr->subclass.integer == AST_CONTROL_HOLD) {
01041                if (instance0->engine->local_bridge) {
01042                   instance0->engine->local_bridge(instance0, NULL);
01043                }
01044                if (instance1->engine->local_bridge) {
01045                   instance1->engine->local_bridge(instance1, NULL);
01046                }
01047                instance0->bridged = NULL;
01048                instance1->bridged = NULL;
01049             } else if (fr->subclass.integer == AST_CONTROL_UNHOLD) {
01050                if (instance0->engine->local_bridge) {
01051                   instance0->engine->local_bridge(instance0, instance1);
01052                }
01053                if (instance1->engine->local_bridge) {
01054                   instance1->engine->local_bridge(instance1, instance0);
01055                }
01056                instance0->bridged = instance1;
01057                instance1->bridged = instance0;
01058             }
01059             /* Since UPDATE_BRIDGE_PEER is only used by the bridging code, don't forward it */
01060             if (fr->subclass.integer != AST_CONTROL_UPDATE_RTP_PEER) {
01061                ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
01062             }
01063             ast_frfree(fr);
01064          } else if (fr->subclass.integer == AST_CONTROL_CONNECTED_LINE) {
01065             if (ast_channel_connected_line_sub(who, other, fr, 1) &&
01066                ast_channel_connected_line_macro(who, other, fr, other == c0, 1)) {
01067                ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
01068             }
01069             ast_frfree(fr);
01070          } else if (fr->subclass.integer == AST_CONTROL_REDIRECTING) {
01071             if (ast_channel_redirecting_sub(who, other, fr, 1) &&
01072                ast_channel_redirecting_macro(who, other, fr, other == c0, 1)) {
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_PVT_CAUSE_CODE) {
01077             ast_channel_hangupcause_hash_set(other, fr->data.ptr, fr->datalen);
01078             ast_frfree(fr);
01079          } else {
01080             *fo = fr;
01081             *rc = who;
01082             ast_debug(1, "rtp-engine-local-bridge: Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass.integer, ast_channel_name(who));
01083             res = AST_BRIDGE_COMPLETE;
01084             break;
01085          }
01086       } else {
01087          if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
01088              (fr->frametype == AST_FRAME_DTMF_END) ||
01089              (fr->frametype == AST_FRAME_VOICE) ||
01090              (fr->frametype == AST_FRAME_VIDEO) ||
01091              (fr->frametype == AST_FRAME_IMAGE) ||
01092              (fr->frametype == AST_FRAME_HTML) ||
01093              (fr->frametype == AST_FRAME_MODEM) ||
01094              (fr->frametype == AST_FRAME_TEXT)) {
01095             ast_write(other, fr);
01096          }
01097 
01098          ast_frfree(fr);
01099       }
01100       /* Swap priority */
01101       cs[2] = cs[0];
01102       cs[0] = cs[1];
01103       cs[1] = cs[2];
01104    }
01105 
01106    /* Stop locally bridging both instances */
01107    if (instance0->engine->local_bridge) {
01108       instance0->engine->local_bridge(instance0, NULL);
01109    }
01110    if (instance1->engine->local_bridge) {
01111       instance1->engine->local_bridge(instance1, NULL);
01112    }
01113 
01114    instance0->bridged = NULL;
01115    instance1->bridged = NULL;
01116 
01117    ast_poll_channel_del(c0, c1);
01118 
01119    return res;
01120 }
01121 
01122 static enum ast_bridge_result remote_bridge_loop(struct ast_channel *c0,
01123    struct ast_channel *c1,
01124    struct ast_rtp_instance *instance0,
01125    struct ast_rtp_instance *instance1,
01126    struct ast_rtp_instance *vinstance0,
01127    struct ast_rtp_instance *vinstance1,
01128    struct ast_rtp_instance *tinstance0,
01129    struct ast_rtp_instance *tinstance1,
01130    struct ast_rtp_glue *glue0,
01131    struct ast_rtp_glue *glue1,
01132    struct ast_format_cap *cap0,
01133    struct ast_format_cap *cap1,
01134    int timeoutms,
01135    int flags,
01136    struct ast_frame **fo,
01137    struct ast_channel **rc,
01138    void *pvt0,
01139    void *pvt1)
01140 {
01141    enum ast_bridge_result res = AST_BRIDGE_FAILED;
01142    struct ast_channel *who = NULL, *other = NULL, *cs[3] = { NULL, };
01143    struct ast_format_cap *oldcap0 = ast_format_cap_dup(cap0);
01144    struct ast_format_cap *oldcap1 = ast_format_cap_dup(cap1);
01145    struct ast_sockaddr ac1 = {{0,}}, vac1 = {{0,}}, tac1 = {{0,}}, ac0 = {{0,}}, vac0 = {{0,}}, tac0 = {{0,}};
01146    struct ast_sockaddr t1 = {{0,}}, vt1 = {{0,}}, tt1 = {{0,}}, t0 = {{0,}}, vt0 = {{0,}}, tt0 = {{0,}};
01147    struct ast_frame *fr = NULL;
01148 
01149    if (!oldcap0 || !oldcap1) {
01150       ast_channel_unlock(c0);
01151       ast_channel_unlock(c1);
01152       goto remote_bridge_cleanup;
01153    }
01154    /* Test the first channel */
01155    if (!(glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0))) {
01156       ast_rtp_instance_get_remote_address(instance1, &ac1);
01157       if (vinstance1) {
01158          ast_rtp_instance_get_remote_address(vinstance1, &vac1);
01159       }
01160       if (tinstance1) {
01161          ast_rtp_instance_get_remote_address(tinstance1, &tac1);
01162       }
01163    } else {
01164       ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", ast_channel_name(c0), ast_channel_name(c1));
01165    }
01166 
01167    /* Test the second channel */
01168    if (!(glue1->update_peer(c1, instance0, vinstance0, tinstance0, cap0, 0))) {
01169       ast_rtp_instance_get_remote_address(instance0, &ac0);
01170       if (vinstance0) {
01171          ast_rtp_instance_get_remote_address(instance0, &vac0);
01172       }
01173       if (tinstance0) {
01174          ast_rtp_instance_get_remote_address(instance0, &tac0);
01175       }
01176    } else {
01177       ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", ast_channel_name(c1), ast_channel_name(c0));
01178    }
01179 
01180    ast_channel_unlock(c0);
01181    ast_channel_unlock(c1);
01182 
01183    instance0->bridged = instance1;
01184    instance1->bridged = instance0;
01185 
01186    ast_poll_channel_add(c0, c1);
01187 
01188    /* Go into a loop handling any stray frames that may come in */
01189    cs[0] = c0;
01190    cs[1] = c1;
01191    cs[2] = NULL;
01192    for (;;) {
01193       /* Check if anything changed */
01194       if ((ast_channel_tech_pvt(c0) != pvt0) ||
01195           (ast_channel_tech_pvt(c1) != pvt1) ||
01196           (ast_channel_masq(c0) || ast_channel_masqr(c0) || ast_channel_masq(c1) || ast_channel_masqr(c1)) ||
01197           (ast_channel_monitor(c0) || ast_channel_audiohooks(c0) || ast_channel_monitor(c1) || ast_channel_audiohooks(c1)) ||
01198           (!ast_framehook_list_is_empty(ast_channel_framehooks(c0)) || !ast_framehook_list_is_empty(ast_channel_framehooks(c1)))) {
01199          ast_debug(1, "Oooh, something is weird, backing out\n");
01200          res = AST_BRIDGE_RETRY;
01201          break;
01202       }
01203 
01204       /* Check if they have changed their address */
01205       ast_rtp_instance_get_remote_address(instance1, &t1);
01206       if (vinstance1) {
01207          ast_rtp_instance_get_remote_address(vinstance1, &vt1);
01208       }
01209       if (tinstance1) {
01210          ast_rtp_instance_get_remote_address(tinstance1, &tt1);
01211       }
01212       if (glue1->get_codec) {
01213          ast_format_cap_remove_all(cap1);
01214          glue1->get_codec(c1, cap1);
01215       }
01216 
01217       ast_rtp_instance_get_remote_address(instance0, &t0);
01218       if (vinstance0) {
01219          ast_rtp_instance_get_remote_address(vinstance0, &vt0);
01220       }
01221       if (tinstance0) {
01222          ast_rtp_instance_get_remote_address(tinstance0, &tt0);
01223       }
01224       if (glue0->get_codec) {
01225          ast_format_cap_remove_all(cap0);
01226          glue0->get_codec(c0, cap0);
01227       }
01228 
01229       if ((ast_sockaddr_cmp(&t1, &ac1)) ||
01230           (vinstance1 && ast_sockaddr_cmp(&vt1, &vac1)) ||
01231           (tinstance1 && ast_sockaddr_cmp(&tt1, &tac1)) ||
01232           (!ast_format_cap_identical(cap1, oldcap1))) {
01233          char tmp_buf[512] = { 0, };
01234          ast_debug(1, "Oooh, '%s' changed end address to %s (format %s)\n",
01235               ast_channel_name(c1), ast_sockaddr_stringify(&t1),
01236               ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap1));
01237          ast_debug(1, "Oooh, '%s' changed end vaddress to %s (format %s)\n",
01238               ast_channel_name(c1), ast_sockaddr_stringify(&vt1),
01239               ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap1));
01240          ast_debug(1, "Oooh, '%s' changed end taddress to %s (format %s)\n",
01241               ast_channel_name(c1), ast_sockaddr_stringify(&tt1),
01242               ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap1));
01243          ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
01244               ast_channel_name(c1), ast_sockaddr_stringify(&ac1),
01245               ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap1));
01246          ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
01247               ast_channel_name(c1), ast_sockaddr_stringify(&vac1),
01248               ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap1));
01249          ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
01250               ast_channel_name(c1), ast_sockaddr_stringify(&tac1),
01251               ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap1));
01252          if (glue0->update_peer(c0,
01253                       ast_sockaddr_isnull(&t1)  ? NULL : instance1,
01254                       ast_sockaddr_isnull(&vt1) ? NULL : vinstance1,
01255                       ast_sockaddr_isnull(&tt1) ? NULL : tinstance1,
01256                       cap1, 0)) {
01257             ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", ast_channel_name(c0), ast_channel_name(c1));
01258          }
01259          ast_sockaddr_copy(&ac1, &t1);
01260          ast_sockaddr_copy(&vac1, &vt1);
01261          ast_sockaddr_copy(&tac1, &tt1);
01262          ast_format_cap_copy(oldcap1, cap1);
01263       }
01264       if ((ast_sockaddr_cmp(&t0, &ac0)) ||
01265           (vinstance0 && ast_sockaddr_cmp(&vt0, &vac0)) ||
01266           (tinstance0 && ast_sockaddr_cmp(&tt0, &tac0)) ||
01267           (!ast_format_cap_identical(cap0, oldcap0))) {
01268          char tmp_buf[512] = { 0, };
01269          ast_debug(1, "Oooh, '%s' changed end address to %s (format %s)\n",
01270               ast_channel_name(c0), ast_sockaddr_stringify(&t0),
01271               ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), cap0));
01272          ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
01273               ast_channel_name(c0), ast_sockaddr_stringify(&ac0),
01274               ast_getformatname_multiple(tmp_buf, sizeof(tmp_buf), oldcap0));
01275          if (glue1->update_peer(c1, t0.len ? instance0 : NULL,
01276                   vt0.len ? vinstance0 : NULL,
01277                   tt0.len ? tinstance0 : NULL,
01278                   cap0, 0)) {
01279             ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", ast_channel_name(c1), ast_channel_name(c0));
01280          }
01281          ast_sockaddr_copy(&ac0, &t0);
01282          ast_sockaddr_copy(&vac0, &vt0);
01283          ast_sockaddr_copy(&tac0, &tt0);
01284          ast_format_cap_copy(oldcap0, cap0);
01285       }
01286 
01287       /* Wait for frame to come in on the channels */
01288       if (!(who = ast_waitfor_n(cs, 2, &timeoutms))) {
01289          if (!timeoutms) {
01290             res = AST_BRIDGE_RETRY;
01291             break;
01292          }
01293          ast_debug(1, "Ooh, empty read...\n");
01294          if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
01295             break;
01296          }
01297          continue;
01298       }
01299       fr = ast_read(who);
01300       other = (who == c0) ? c1 : c0;
01301       if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
01302              (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
01303               ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))))) {
01304          /* Break out of bridge */
01305          *fo = fr;
01306          *rc = who;
01307          ast_debug(1, "Oooh, got a %s\n", fr ? "digit" : "hangup");
01308          res = AST_BRIDGE_COMPLETE;
01309          break;
01310       } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
01311          if ((fr->subclass.integer == AST_CONTROL_HOLD) ||
01312              (fr->subclass.integer == AST_CONTROL_UNHOLD) ||
01313              (fr->subclass.integer == AST_CONTROL_VIDUPDATE) ||
01314              (fr->subclass.integer == AST_CONTROL_SRCUPDATE) ||
01315              (fr->subclass.integer == AST_CONTROL_T38_PARAMETERS) ||
01316             (fr->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER)) {
01317             if (fr->subclass.integer == AST_CONTROL_HOLD) {
01318                /* If we someone went on hold we want the other side to reinvite back to us */
01319                if (who == c0) {
01320                   glue1->update_peer(c1, NULL, NULL, NULL, 0, 0);
01321                } else {
01322                   glue0->update_peer(c0, NULL, NULL, NULL, 0, 0);
01323                }
01324             } else if (fr->subclass.integer == AST_CONTROL_UNHOLD ||
01325                fr->subclass.integer == AST_CONTROL_UPDATE_RTP_PEER) {
01326                /* If they went off hold they should go back to being direct, or if we have
01327                 * been told to force a peer update, go ahead and do it. */
01328                if (who == c0) {
01329                   glue1->update_peer(c1, instance0, vinstance0, tinstance0, cap0, 0);
01330                } else {
01331                   glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0);
01332                }
01333             }
01334             /* Update local address information */
01335             ast_rtp_instance_get_remote_address(instance0, &t0);
01336             ast_sockaddr_copy(&ac0, &t0);
01337             ast_rtp_instance_get_remote_address(instance1, &t1);
01338             ast_sockaddr_copy(&ac1, &t1);
01339             /* Update codec information */
01340             if (glue0->get_codec && ast_channel_tech_pvt(c0)) {
01341                ast_format_cap_remove_all(cap0);
01342                ast_format_cap_remove_all(oldcap0);
01343                glue0->get_codec(c0, cap0);
01344                ast_format_cap_append(oldcap0, cap0);
01345 
01346             }
01347             if (glue1->get_codec && ast_channel_tech_pvt(c1)) {
01348                ast_format_cap_remove_all(cap1);
01349                ast_format_cap_remove_all(oldcap1);
01350                glue1->get_codec(c1, cap1);
01351                ast_format_cap_append(oldcap1, cap1);
01352             }
01353             /* Since UPDATE_BRIDGE_PEER is only used by the bridging code, don't forward it */
01354             if (fr->subclass.integer != AST_CONTROL_UPDATE_RTP_PEER) {
01355                ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
01356             }
01357             ast_frfree(fr);
01358          } else if (fr->subclass.integer == AST_CONTROL_CONNECTED_LINE) {
01359             if (ast_channel_connected_line_sub(who, other, fr, 1) &&
01360                ast_channel_connected_line_macro(who, other, fr, other == c0, 1)) {
01361                ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
01362             }
01363             ast_frfree(fr);
01364          } else if (fr->subclass.integer == AST_CONTROL_REDIRECTING) {
01365             if (ast_channel_redirecting_sub(who, other, fr, 1) &&
01366                ast_channel_redirecting_macro(who, other, fr, other == c0, 1)) {
01367                ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
01368             }
01369             ast_frfree(fr);
01370          } else if (fr->subclass.integer == AST_CONTROL_PVT_CAUSE_CODE) {
01371             ast_channel_hangupcause_hash_set(other, fr->data.ptr, fr->datalen);
01372             ast_frfree(fr);
01373          } else {
01374             *fo = fr;
01375             *rc = who;
01376             ast_debug(1, "Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass.integer, ast_channel_name(who));
01377             res = AST_BRIDGE_COMPLETE;
01378             goto remote_bridge_cleanup;
01379          }
01380       } else {
01381          if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
01382              (fr->frametype == AST_FRAME_DTMF_END) ||
01383              (fr->frametype == AST_FRAME_VOICE) ||
01384              (fr->frametype == AST_FRAME_VIDEO) ||
01385              (fr->frametype == AST_FRAME_IMAGE) ||
01386              (fr->frametype == AST_FRAME_HTML) ||
01387              (fr->frametype == AST_FRAME_MODEM) ||
01388              (fr->frametype == AST_FRAME_TEXT)) {
01389             ast_write(other, fr);
01390          }
01391          ast_frfree(fr);
01392       }
01393       /* Swap priority */
01394       cs[2] = cs[0];
01395       cs[0] = cs[1];
01396       cs[1] = cs[2];
01397    }
01398 
01399    if (ast_test_flag(ast_channel_flags(c0), AST_FLAG_ZOMBIE)) {
01400       ast_debug(1, "Channel '%s' Zombie cleardown from bridge\n", ast_channel_name(c0));
01401    } else if (ast_channel_tech_pvt(c0) != pvt0) {
01402       ast_debug(1, "Channel c0->'%s' pvt changed, in bridge with c1->'%s'\n", ast_channel_name(c0), ast_channel_name(c1));
01403    } else if (glue0 != ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) {
01404       ast_debug(1, "Channel c0->'%s' technology changed, in bridge with c1->'%s'\n", ast_channel_name(c0), ast_channel_name(c1));
01405    } else if (glue0->update_peer(c0, NULL, NULL, NULL, 0, 0)) {
01406       ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", ast_channel_name(c0));
01407    }
01408    if (ast_test_flag(ast_channel_flags(c1), AST_FLAG_ZOMBIE)) {
01409       ast_debug(1, "Channel '%s' Zombie cleardown from bridge\n", ast_channel_name(c1));
01410    } else if (ast_channel_tech_pvt(c1) != pvt1) {
01411       ast_debug(1, "Channel c1->'%s' pvt changed, in bridge with c0->'%s'\n", ast_channel_name(c1), ast_channel_name(c0));
01412    } else if (glue1 != ast_rtp_instance_get_glue(ast_channel_tech(c1)->type)) {
01413       ast_debug(1, "Channel c1->'%s' technology changed, in bridge with c0->'%s'\n", ast_channel_name(c1), ast_channel_name(c0));
01414    } else if (glue1->update_peer(c1, NULL, NULL, NULL, 0, 0)) {
01415       ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", ast_channel_name(c1));
01416    }
01417 
01418    instance0->bridged = NULL;
01419    instance1->bridged = NULL;
01420 
01421    ast_poll_channel_del(c0, c1);
01422 
01423 remote_bridge_cleanup:
01424    ast_format_cap_destroy(oldcap0);
01425    ast_format_cap_destroy(oldcap1);
01426 
01427    return res;
01428 }
01429 
01430 /*!
01431  * \brief Conditionally unref an rtp instance
01432  */
01433 static void unref_instance_cond(struct ast_rtp_instance **instance)
01434 {
01435    if (*instance) {
01436       ao2_ref(*instance, -1);
01437       *instance = NULL;
01438    }
01439 }
01440 
01441 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)
01442 {
01443    struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
01444          *vinstance0 = NULL, *vinstance1 = NULL,
01445          *tinstance0 = NULL, *tinstance1 = NULL;
01446    struct ast_rtp_glue *glue0, *glue1;
01447    struct ast_sockaddr addr1 = { {0, }, }, addr2 = { {0, }, };
01448    enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
01449    enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
01450    enum ast_bridge_result res = AST_BRIDGE_FAILED;
01451    enum ast_rtp_dtmf_mode dmode;
01452    struct ast_format_cap *cap0 = ast_format_cap_alloc_nolock();
01453    struct ast_format_cap *cap1 = ast_format_cap_alloc_nolock();
01454    int unlock_chans = 1;
01455 
01456    if (!cap0 || !cap1) {
01457       unlock_chans = 0;
01458       goto done;
01459    }
01460 
01461    /* Lock both channels so we can look for the glue that binds them together */
01462    ast_channel_lock(c0);
01463    while (ast_channel_trylock(c1)) {
01464       ast_channel_unlock(c0);
01465       usleep(1);
01466       ast_channel_lock(c0);
01467    }
01468 
01469    /* Ensure neither channel got hungup during lock avoidance */
01470    if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
01471       ast_log(LOG_WARNING, "Got hangup while attempting to bridge '%s' and '%s'\n", ast_channel_name(c0), ast_channel_name(c1));
01472       goto done;
01473    }
01474 
01475    /* Grab glue that binds each channel to something using the RTP engine */
01476    if (!(glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) || !(glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type))) {
01477       ast_debug(1, "Can't find native functions for channel '%s'\n", glue0 ? ast_channel_name(c1) : ast_channel_name(c0));
01478       goto done;
01479    }
01480 
01481    audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
01482    video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
01483 
01484    audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
01485    video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
01486 
01487    /* Apply any limitations on direct media bridging that may be present */
01488    if (audio_glue0_res == audio_glue1_res && audio_glue1_res == AST_RTP_GLUE_RESULT_REMOTE) {
01489       if (glue0->allow_rtp_remote && !(glue0->allow_rtp_remote(c0, instance1))) {
01490          /* If the allow_rtp_remote indicates that remote isn't allowed, revert to local bridge */
01491          audio_glue0_res = audio_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
01492       } else if (glue1->allow_rtp_remote && !(glue1->allow_rtp_remote(c1, instance0))) {
01493          audio_glue0_res = audio_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
01494       }
01495    }
01496    if (video_glue0_res == video_glue1_res && video_glue1_res == AST_RTP_GLUE_RESULT_REMOTE) {
01497       if (glue0->allow_vrtp_remote && !(glue0->allow_vrtp_remote(c0, instance1))) {
01498          /* if the allow_vrtp_remote indicates that remote isn't allowed, revert to local bridge */
01499          video_glue0_res = video_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
01500       } else if (glue1->allow_vrtp_remote && !(glue1->allow_vrtp_remote(c1, instance0))) {
01501          video_glue0_res = video_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
01502       }
01503    }
01504 
01505    /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
01506    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)) {
01507       audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
01508    }
01509    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)) {
01510       audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
01511    }
01512 
01513    /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
01514    if (audio_glue0_res == AST_RTP_GLUE_RESULT_FORBID || audio_glue1_res == AST_RTP_GLUE_RESULT_FORBID) {
01515       res = AST_BRIDGE_FAILED_NOWARN;
01516       goto done;
01517    }
01518 
01519 
01520    /* If address families differ, force a local bridge */
01521    ast_rtp_instance_get_remote_address(instance0, &addr1);
01522    ast_rtp_instance_get_remote_address(instance1, &addr2);
01523 
01524    if (addr1.ss.ss_family != addr2.ss.ss_family ||
01525       (ast_sockaddr_is_ipv4_mapped(&addr1) != ast_sockaddr_is_ipv4_mapped(&addr2))) {
01526       audio_glue0_res = AST_RTP_GLUE_RESULT_LOCAL;
01527       audio_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
01528    }
01529 
01530    /* If we need to get DTMF see if we can do it outside of the RTP stream itself */
01531    dmode = ast_rtp_instance_dtmf_mode_get(instance0);
01532    if ((flags & AST_BRIDGE_DTMF_CHANNEL_0) && dmode) {
01533       res = AST_BRIDGE_FAILED_NOWARN;
01534       goto done;
01535    }
01536    dmode = ast_rtp_instance_dtmf_mode_get(instance1);
01537    if ((flags & AST_BRIDGE_DTMF_CHANNEL_1) && dmode) {
01538       res = AST_BRIDGE_FAILED_NOWARN;
01539       goto done;
01540    }
01541 
01542    /* 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 */
01543    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)))) {
01544       res = AST_BRIDGE_FAILED_NOWARN;
01545       goto done;
01546    }
01547 
01548    /* Make sure that codecs match */
01549    if (glue0->get_codec){
01550       glue0->get_codec(c0, cap0);
01551    }
01552    if (glue1->get_codec) {
01553       glue1->get_codec(c1, cap1);
01554    }
01555    if (!ast_format_cap_is_empty(cap0) && !ast_format_cap_is_empty(cap1) && !ast_format_cap_has_joint(cap0, cap1)) {
01556       char tmp0[256] = { 0, };
01557       char tmp1[256] = { 0, };
01558       ast_debug(1, "Channel codec0 = %s is not codec1 = %s, cannot native bridge in RTP.\n",
01559          ast_getformatname_multiple(tmp0, sizeof(tmp0), cap0),
01560          ast_getformatname_multiple(tmp1, sizeof(tmp1), cap1));
01561       res = AST_BRIDGE_FAILED_NOWARN;
01562       goto done;
01563    }
01564 
01565    instance0->glue = glue0;
01566    instance1->glue = glue1;
01567    instance0->chan = c0;
01568    instance1->chan = c1;
01569 
01570    /* Depending on the end result for bridging either do a local bridge or remote bridge */
01571    if (audio_glue0_res == AST_RTP_GLUE_RESULT_LOCAL || audio_glue1_res == AST_RTP_GLUE_RESULT_LOCAL) {
01572       ast_verb(3, "Locally bridging %s and %s\n", ast_channel_name(c0), ast_channel_name(c1));
01573       res = local_bridge_loop(c0, c1, instance0, instance1, timeoutms, flags, fo, rc, ast_channel_tech_pvt(c0), ast_channel_tech_pvt(c1));
01574    } else {
01575       ast_verb(3, "Remotely bridging %s and %s\n", ast_channel_name(c0), ast_channel_name(c1));
01576       res = remote_bridge_loop(c0, c1, instance0, instance1, vinstance0, vinstance1,
01577             tinstance0, tinstance1, glue0, glue1, cap0, cap1, timeoutms, flags,
01578             fo, rc, ast_channel_tech_pvt(c0), ast_channel_tech_pvt(c1));
01579    }
01580 
01581    instance0->glue = NULL;
01582    instance1->glue = NULL;
01583    instance0->chan = NULL;
01584    instance1->chan = NULL;
01585 
01586    unlock_chans = 0;
01587 
01588 done:
01589    if (unlock_chans) {
01590       ast_channel_unlock(c0);
01591       ast_channel_unlock(c1);
01592    }
01593    ast_format_cap_destroy(cap1);
01594    ast_format_cap_destroy(cap0);
01595 
01596    unref_instance_cond(&instance0);
01597    unref_instance_cond(&instance1);
01598    unref_instance_cond(&vinstance0);
01599    unref_instance_cond(&vinstance1);
01600    unref_instance_cond(&tinstance0);
01601    unref_instance_cond(&tinstance1);
01602 
01603    return res;
01604 }
01605 
01606 struct ast_rtp_instance *ast_rtp_instance_get_bridged(struct ast_rtp_instance *instance)
01607 {
01608    return instance->bridged;
01609 }
01610 
01611 void ast_rtp_instance_early_bridge_make_compatible(struct ast_channel *c0, struct ast_channel *c1)
01612 {
01613    struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
01614       *vinstance0 = NULL, *vinstance1 = NULL,
01615       *tinstance0 = NULL, *tinstance1 = NULL;
01616    struct ast_rtp_glue *glue0, *glue1;
01617    enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
01618    enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
01619    struct ast_format_cap *cap0 = ast_format_cap_alloc_nolock();
01620    struct ast_format_cap *cap1 = ast_format_cap_alloc_nolock();
01621 
01622    /* Lock both channels so we can look for the glue that binds them together */
01623    ast_channel_lock_both(c0, c1);
01624 
01625    if (!cap1 || !cap0) {
01626       goto done;
01627    }
01628 
01629    /* Grab glue that binds each channel to something using the RTP engine */
01630    if (!(glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) || !(glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type))) {
01631       ast_debug(1, "Can't find native functions for channel '%s'\n", glue0 ? ast_channel_name(c1) : ast_channel_name(c0));
01632       goto done;
01633    }
01634 
01635    audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
01636    video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
01637 
01638    audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
01639    video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
01640 
01641    /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
01642    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)) {
01643       audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
01644    }
01645    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)) {
01646       audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
01647    }
01648    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) {
01649       glue0->get_codec(c0, cap0);
01650    }
01651    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) {
01652       glue1->get_codec(c1, cap1);
01653    }
01654 
01655    /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
01656    if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
01657       goto done;
01658    }
01659 
01660    /* Make sure we have matching codecs */
01661    if (!ast_format_cap_has_joint(cap0, cap1)) {
01662       goto done;
01663    }
01664 
01665    ast_rtp_codecs_payloads_copy(&instance0->codecs, &instance1->codecs, instance1);
01666 
01667    if (vinstance0 && vinstance1) {
01668       ast_rtp_codecs_payloads_copy(&vinstance0->codecs, &vinstance1->codecs, vinstance1);
01669    }
01670    if (tinstance0 && tinstance1) {
01671       ast_rtp_codecs_payloads_copy(&tinstance0->codecs, &tinstance1->codecs, tinstance1);
01672    }
01673 
01674    if (glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0)) {
01675       ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n",
01676          ast_channel_name(c0), ast_channel_name(c1));
01677    } else {
01678       ast_debug(1, "Seeded SDP of '%s' with that of '%s'\n",
01679          ast_channel_name(c0), ast_channel_name(c1));
01680    }
01681 
01682 done:
01683    ast_channel_unlock(c0);
01684    ast_channel_unlock(c1);
01685 
01686    ast_format_cap_destroy(cap0);
01687    ast_format_cap_destroy(cap1);
01688 
01689    unref_instance_cond(&instance0);
01690    unref_instance_cond(&instance1);
01691    unref_instance_cond(&vinstance0);
01692    unref_instance_cond(&vinstance1);
01693    unref_instance_cond(&tinstance0);
01694    unref_instance_cond(&tinstance1);
01695 }
01696 
01697 int ast_rtp_instance_early_bridge(struct ast_channel *c0, struct ast_channel *c1)
01698 {
01699    struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
01700          *vinstance0 = NULL, *vinstance1 = NULL,
01701          *tinstance0 = NULL, *tinstance1 = NULL;
01702    struct ast_rtp_glue *glue0, *glue1;
01703    enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
01704    enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
01705    struct ast_format_cap *cap0 = ast_format_cap_alloc_nolock();
01706    struct ast_format_cap *cap1 = ast_format_cap_alloc_nolock();
01707    int res = 0;
01708 
01709    /* If there is no second channel just immediately bail out, we are of no use in that scenario */
01710    if (!c1) {
01711       ast_format_cap_destroy(cap0);
01712       ast_format_cap_destroy(cap1);
01713       return -1;
01714    }
01715 
01716    /* Lock both channels so we can look for the glue that binds them together */
01717    ast_channel_lock(c0);
01718    while (ast_channel_trylock(c1)) {
01719       ast_channel_unlock(c0);
01720       usleep(1);
01721       ast_channel_lock(c0);
01722    }
01723 
01724    if (!cap1 || !cap0) {
01725       goto done;
01726    }
01727 
01728    /* Grab glue that binds each channel to something using the RTP engine */
01729    if (!(glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) || !(glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type))) {
01730       ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", glue0 ? ast_channel_name(c1) : ast_channel_name(c0));
01731       goto done;
01732    }
01733 
01734    audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
01735    video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
01736 
01737    audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
01738    video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
01739 
01740    /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
01741    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)) {
01742       audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
01743    }
01744    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)) {
01745       audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
01746    }
01747    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) {
01748       glue0->get_codec(c0, cap0);
01749    }
01750    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) {
01751       glue1->get_codec(c1, cap1);
01752    }
01753 
01754    /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
01755    if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
01756       goto done;
01757    }
01758 
01759    /* Make sure we have matching codecs */
01760    if (!ast_format_cap_has_joint(cap0, cap1)) {
01761       goto done;
01762    }
01763 
01764    /* Bridge media early */
01765    if (glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0)) {
01766       ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
01767    }
01768 
01769    res = 0;
01770 
01771 done:
01772    ast_channel_unlock(c0);
01773    ast_channel_unlock(c1);
01774 
01775    ast_format_cap_destroy(cap0);
01776    ast_format_cap_destroy(cap1);
01777 
01778    unref_instance_cond(&instance0);
01779    unref_instance_cond(&instance1);
01780    unref_instance_cond(&vinstance0);
01781    unref_instance_cond(&vinstance1);
01782    unref_instance_cond(&tinstance0);
01783    unref_instance_cond(&tinstance1);
01784 
01785    if (!res) {
01786       ast_debug(1, "Setting early bridge SDP of '%s' with that of '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
01787    }
01788 
01789    return res;
01790 }
01791 
01792 int ast_rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations)
01793 {
01794    return instance->engine->red_init ? instance->engine->red_init(instance, buffer_time, payloads, generations) : -1;
01795 }
01796 
01797 int ast_rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame)
01798 {
01799    return instance->engine->red_buffer ? instance->engine->red_buffer(instance, frame) : -1;
01800 }
01801 
01802 int ast_rtp_instance_get_stats(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat)
01803 {
01804    return instance->engine->get_stat ? instance->engine->get_stat(instance, stats, stat) : -1;
01805 }
01806 
01807 char *ast_rtp_instance_get_quality(struct ast_rtp_instance *instance, enum ast_rtp_instance_stat_field field, char *buf, size_t size)
01808 {
01809    struct ast_rtp_instance_stats stats = { 0, };
01810    enum ast_rtp_instance_stat stat;
01811 
01812    /* Determine what statistics we will need to retrieve based on field passed in */
01813    if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
01814       stat = AST_RTP_INSTANCE_STAT_ALL;
01815    } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
01816       stat = AST_RTP_INSTANCE_STAT_COMBINED_JITTER;
01817    } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
01818       stat = AST_RTP_INSTANCE_STAT_COMBINED_LOSS;
01819    } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
01820       stat = AST_RTP_INSTANCE_STAT_COMBINED_RTT;
01821    } else {
01822       return NULL;
01823    }
01824 
01825    /* Attempt to actually retrieve the statistics we need to generate the quality string */
01826    if (ast_rtp_instance_get_stats(instance, &stats, stat)) {
01827       return NULL;
01828    }
01829 
01830    /* Now actually fill the buffer with the good information */
01831    if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
01832       snprintf(buf, size, "ssrc=%i;themssrc=%u;lp=%u;rxjitter=%f;rxcount=%u;txjitter=%f;txcount=%u;rlp=%u;rtt=%f",
01833           stats.local_ssrc, stats.remote_ssrc, stats.rxploss, stats.txjitter, stats.rxcount, stats.rxjitter, stats.txcount, stats.txploss, stats.rtt);
01834    } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
01835       snprintf(buf, size, "minrxjitter=%f;maxrxjitter=%f;avgrxjitter=%f;stdevrxjitter=%f;reported_minjitter=%f;reported_maxjitter=%f;reported_avgjitter=%f;reported_stdevjitter=%f;",
01836           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));
01837    } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
01838       snprintf(buf, size, "minrxlost=%f;maxrxlost=%f;avgrxlost=%f;stdevrxlost=%f;reported_minlost=%f;reported_maxlost=%f;reported_avglost=%f;reported_stdevlost=%f;",
01839           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));
01840    } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
01841       snprintf(buf, size, "minrtt=%f;maxrtt=%f;avgrtt=%f;stdevrtt=%f;", stats.minrtt, stats.maxrtt, stats.normdevrtt, stats.stdevrtt);
01842    }
01843 
01844    return buf;
01845 }
01846 
01847 void ast_rtp_instance_set_stats_vars(struct ast_channel *chan, struct ast_rtp_instance *instance)
01848 {
01849    char quality_buf[AST_MAX_USER_FIELD], *quality;
01850    struct ast_channel *bridge = ast_bridged_channel(chan);
01851 
01852    if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
01853       pbx_builtin_setvar_helper(chan, "RTPAUDIOQOS", quality);
01854       if (bridge) {
01855          pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSBRIDGED", quality);
01856       }
01857    }
01858 
01859    if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER, quality_buf, sizeof(quality_buf)))) {
01860       pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSJITTER", quality);
01861       if (bridge) {
01862          pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSJITTERBRIDGED", quality);
01863       }
01864    }
01865 
01866    if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS, quality_buf, sizeof(quality_buf)))) {
01867       pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSLOSS", quality);
01868       if (bridge) {
01869          pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSLOSSBRIDGED", quality);
01870       }
01871    }
01872 
01873    if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT, quality_buf, sizeof(quality_buf)))) {
01874       pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSRTT", quality);
01875       if (bridge) {
01876          pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSRTTBRIDGED", quality);
01877       }
01878    }
01879 }
01880 
01881 int ast_rtp_instance_set_read_format(struct ast_rtp_instance *instance, struct ast_format *format)
01882 {
01883    return instance->engine->set_read_format ? instance->engine->set_read_format(instance, format) : -1;
01884 }
01885 
01886 int ast_rtp_instance_set_write_format(struct ast_rtp_instance *instance, struct ast_format *format)
01887 {
01888    return instance->engine->set_write_format ? instance->engine->set_write_format(instance, format) : -1;
01889 }
01890 
01891 int ast_rtp_instance_make_compatible(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_channel *peer)
01892 {
01893    struct ast_rtp_glue *glue;
01894    struct ast_rtp_instance *peer_instance = NULL;
01895    int res = -1;
01896 
01897    if (!instance->engine->make_compatible) {
01898       return -1;
01899    }
01900 
01901    ast_channel_lock(peer);
01902 
01903    if (!(glue = ast_rtp_instance_get_glue(ast_channel_tech(peer)->type))) {
01904       ast_channel_unlock(peer);
01905       return -1;
01906    }
01907 
01908    glue->get_rtp_info(peer, &peer_instance);
01909 
01910    if (!peer_instance || peer_instance->engine != instance->engine) {
01911       ast_channel_unlock(peer);
01912       ao2_ref(peer_instance, -1);
01913       peer_instance = NULL;
01914       return -1;
01915    }
01916 
01917    res = instance->engine->make_compatible(chan, instance, peer, peer_instance);
01918 
01919    ast_channel_unlock(peer);
01920 
01921    ao2_ref(peer_instance, -1);
01922    peer_instance = NULL;
01923 
01924    return res;
01925 }
01926 
01927 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)
01928 {
01929    if (instance->engine->available_formats) {
01930       instance->engine->available_formats(instance, to_endpoint, to_asterisk, result);
01931       if (!ast_format_cap_is_empty(result)) {
01932          return;
01933       }
01934    }
01935 
01936    ast_translate_available_formats(to_endpoint, to_asterisk, result);
01937 }
01938 
01939 int ast_rtp_instance_activate(struct ast_rtp_instance *instance)
01940 {
01941    return instance->engine->activate ? instance->engine->activate(instance) : 0;
01942 }
01943 
01944 void ast_rtp_instance_stun_request(struct ast_rtp_instance *instance,
01945                struct ast_sockaddr *suggestion,
01946                const char *username)
01947 {
01948    if (instance->engine->stun_request) {
01949       instance->engine->stun_request(instance, suggestion, username);
01950    }
01951 }
01952 
01953 void ast_rtp_instance_set_timeout(struct ast_rtp_instance *instance, int timeout)
01954 {
01955    instance->timeout = timeout;
01956 }
01957 
01958 void ast_rtp_instance_set_hold_timeout(struct ast_rtp_instance *instance, int timeout)
01959 {
01960    instance->holdtimeout = timeout;
01961 }
01962 
01963 void ast_rtp_instance_set_keepalive(struct ast_rtp_instance *instance, int interval)
01964 {
01965    instance->keepalive = interval;
01966 }
01967 
01968 int ast_rtp_instance_get_timeout(struct ast_rtp_instance *instance)
01969 {
01970    return instance->timeout;
01971 }
01972 
01973 int ast_rtp_instance_get_hold_timeout(struct ast_rtp_instance *instance)
01974 {
01975    return instance->holdtimeout;
01976 }
01977 
01978 int ast_rtp_instance_get_keepalive(struct ast_rtp_instance *instance)
01979 {
01980    return instance->keepalive;
01981 }
01982 
01983 struct ast_rtp_engine *ast_rtp_instance_get_engine(struct ast_rtp_instance *instance)
01984 {
01985    return instance->engine;
01986 }
01987 
01988 struct ast_rtp_glue *ast_rtp_instance_get_active_glue(struct ast_rtp_instance *instance)
01989 {
01990    return instance->glue;
01991 }
01992 
01993 struct ast_channel *ast_rtp_instance_get_chan(struct ast_rtp_instance *instance)
01994 {
01995    return instance->chan;
01996 }
01997 
01998 int ast_rtp_engine_register_srtp(struct ast_srtp_res *srtp_res, struct ast_srtp_policy_res *policy_res)
01999 {
02000    if (res_srtp || res_srtp_policy) {
02001       return -1;
02002    }
02003    if (!srtp_res || !policy_res) {
02004       return -1;
02005    }
02006 
02007    res_srtp = srtp_res;
02008    res_srtp_policy = policy_res;
02009 
02010    return 0;
02011 }
02012 
02013 void ast_rtp_engine_unregister_srtp(void)
02014 {
02015    res_srtp = NULL;
02016    res_srtp_policy = NULL;
02017 }
02018 
02019 int ast_rtp_engine_srtp_is_registered(void)
02020 {
02021    return res_srtp && res_srtp_policy;
02022 }
02023 
02024 int ast_rtp_instance_add_srtp_policy(struct ast_rtp_instance *instance, struct ast_srtp_policy *remote_policy, struct ast_srtp_policy *local_policy)
02025 {
02026    int res = 0;
02027 
02028    if (!res_srtp) {
02029       return -1;
02030    }
02031 
02032    if (!instance->srtp) {
02033       res = res_srtp->create(&instance->srtp, instance, remote_policy);
02034    } else {
02035       res = res_srtp->replace(&instance->srtp, instance, remote_policy);
02036    }
02037    if (!res) {
02038       res = res_srtp->add_stream(instance->srtp, local_policy);
02039    }
02040 
02041    return res;
02042 }
02043 
02044 struct ast_srtp *ast_rtp_instance_get_srtp(struct ast_rtp_instance *instance)
02045 {
02046    return instance->srtp;
02047 }
02048 
02049 int ast_rtp_instance_sendcng(struct ast_rtp_instance *instance, int level)
02050 {
02051    if (instance->engine->sendcng) {
02052       return instance->engine->sendcng(instance, level);
02053    }
02054 
02055    return -1;
02056 }
02057 
02058 struct ast_rtp_engine_ice *ast_rtp_instance_get_ice(struct ast_rtp_instance *instance)
02059 {
02060    return instance->engine->ice;
02061 }
02062 
02063 struct ast_rtp_engine_dtls *ast_rtp_instance_get_dtls(struct ast_rtp_instance *instance)
02064 {
02065    return instance->engine->dtls;
02066 }
02067 
02068 int ast_rtp_dtls_cfg_parse(struct ast_rtp_dtls_cfg *dtls_cfg, const char *name, const char *value)
02069 {
02070    if (!strcasecmp(name, "dtlsenable")) {
02071       dtls_cfg->enabled = ast_true(value) ? 1 : 0;
02072    } else if (!strcasecmp(name, "dtlsverify")) {
02073       dtls_cfg->verify = ast_true(value) ? 1 : 0;
02074    } else if (!strcasecmp(name, "dtlsrekey")) {
02075       if (sscanf(value, "%30u", &dtls_cfg->rekey) != 1) {
02076          return -1;
02077       }
02078    } else if (!strcasecmp(name, "dtlscertfile")) {
02079       ast_free(dtls_cfg->certfile);
02080       dtls_cfg->certfile = ast_strdup(value);
02081    } else if (!strcasecmp(name, "dtlsprivatekey")) {
02082       ast_free(dtls_cfg->pvtfile);
02083       dtls_cfg->pvtfile = ast_strdup(value);
02084    } else if (!strcasecmp(name, "dtlscipher")) {
02085       ast_free(dtls_cfg->cipher);
02086       dtls_cfg->cipher = ast_strdup(value);
02087    } else if (!strcasecmp(name, "dtlscafile")) {
02088       ast_free(dtls_cfg->cafile);
02089       dtls_cfg->cafile = ast_strdup(value);
02090    } else if (!strcasecmp(name, "dtlscapath") || !strcasecmp(name, "dtlscadir")) {
02091       ast_free(dtls_cfg->capath);
02092       dtls_cfg->capath = ast_strdup(value);
02093    } else if (!strcasecmp(name, "dtlssetup")) {
02094       if (!strcasecmp(value, "active")) {
02095          dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_ACTIVE;
02096       } else if (!strcasecmp(value, "passive")) {
02097          dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_PASSIVE;
02098       } else if (!strcasecmp(value, "actpass")) {
02099          dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_ACTPASS;
02100       }
02101    } else {
02102       return -1;
02103    }
02104 
02105    return 0;
02106 }
02107 
02108 void ast_rtp_dtls_cfg_copy(const struct ast_rtp_dtls_cfg *src_cfg, struct ast_rtp_dtls_cfg *dst_cfg)
02109 {
02110    dst_cfg->enabled = src_cfg->enabled;
02111    dst_cfg->verify = src_cfg->verify;
02112    dst_cfg->rekey = src_cfg->rekey;
02113    dst_cfg->suite = src_cfg->suite;
02114    dst_cfg->certfile = ast_strdup(src_cfg->certfile);
02115    dst_cfg->pvtfile = ast_strdup(src_cfg->pvtfile);
02116    dst_cfg->cipher = ast_strdup(src_cfg->cipher);
02117    dst_cfg->cafile = ast_strdup(src_cfg->cafile);
02118    dst_cfg->capath = ast_strdup(src_cfg->capath);
02119    dst_cfg->default_setup = src_cfg->default_setup;
02120 }
02121 
02122 void ast_rtp_dtls_cfg_free(struct ast_rtp_dtls_cfg *dtls_cfg)
02123 {
02124    ast_free(dtls_cfg->certfile);
02125    ast_free(dtls_cfg->pvtfile);
02126    ast_free(dtls_cfg->cipher);
02127    ast_free(dtls_cfg->cafile);
02128    ast_free(dtls_cfg->capath);
02129 }
02130 
02131 static void set_next_mime_type(const struct ast_format *format, int rtp_code, char *type, char *subtype, unsigned int sample_rate)
02132 {
02133    int x = mime_types_len;
02134    if (ARRAY_LEN(ast_rtp_mime_types) == mime_types_len) {
02135       return;
02136    }
02137 
02138    ast_rwlock_wrlock(&mime_types_lock);
02139    if (format) {
02140       ast_rtp_mime_types[x].payload_type.asterisk_format = 1;
02141       ast_format_copy(&ast_rtp_mime_types[x].payload_type.format, format);
02142    } else {
02143       ast_rtp_mime_types[x].payload_type.rtp_code = rtp_code;
02144    }
02145    ast_rtp_mime_types[x].type = type;
02146    ast_rtp_mime_types[x].subtype = subtype;
02147    ast_rtp_mime_types[x].sample_rate = sample_rate;
02148    mime_types_len++;
02149    ast_rwlock_unlock(&mime_types_lock);
02150 }
02151 
02152 static void add_static_payload(int map, const struct ast_format *format, int rtp_code)
02153 {
02154    int x;
02155    ast_rwlock_wrlock(&static_RTP_PT_lock);
02156    if (map < 0) {
02157       /* find next available dynamic payload slot */
02158       for (x = 96; x < 127; x++) {
02159          if (!static_RTP_PT[x].asterisk_format && !static_RTP_PT[x].rtp_code) {
02160             map = x;
02161             break;
02162          }
02163       }
02164    }
02165 
02166    if (map < 0) {
02167       ast_log(LOG_WARNING, "No Dynamic RTP mapping avaliable for format %s\n" ,ast_getformatname(format));
02168       ast_rwlock_unlock(&static_RTP_PT_lock);
02169       return;
02170    }
02171 
02172    if (format) {
02173       static_RTP_PT[map].asterisk_format = 1;
02174       ast_format_copy(&static_RTP_PT[map].format, format);
02175    } else {
02176       static_RTP_PT[map].rtp_code = rtp_code;
02177    }
02178    ast_rwlock_unlock(&static_RTP_PT_lock);
02179 }
02180 
02181 int ast_rtp_engine_load_format(const struct ast_format *format)
02182 {
02183    switch (format->id) {
02184    case AST_FORMAT_SILK:
02185       set_next_mime_type(format, 0, "audio", "SILK", ast_format_rate(format));
02186       add_static_payload(-1, format, 0);
02187       break;
02188    case AST_FORMAT_CELT:
02189       set_next_mime_type(format, 0, "audio", "CELT", ast_format_rate(format));
02190       add_static_payload(-1, format, 0);
02191       break;
02192    default:
02193       break;
02194    }
02195 
02196    return 0;
02197 }
02198 
02199 int ast_rtp_engine_unload_format(const struct ast_format *format)
02200 {
02201    int x;
02202    int y = 0;
02203 
02204    ast_rwlock_wrlock(&static_RTP_PT_lock);
02205    /* remove everything pertaining to this format id from the lists */
02206    for (x = 0; x < AST_RTP_MAX_PT; x++) {
02207       if (ast_format_cmp(&static_RTP_PT[x].format, format) == AST_FORMAT_CMP_EQUAL) {
02208          memset(&static_RTP_PT[x], 0, sizeof(struct ast_rtp_payload_type));
02209       }
02210    }
02211    ast_rwlock_unlock(&static_RTP_PT_lock);
02212 
02213 
02214    ast_rwlock_wrlock(&mime_types_lock);
02215    /* rebuild the list skipping the items matching this id */
02216    for (x = 0; x < mime_types_len; x++) {
02217       if (ast_format_cmp(&ast_rtp_mime_types[x].payload_type.format, format) == AST_FORMAT_CMP_EQUAL) {
02218          continue;
02219       }
02220       ast_rtp_mime_types[y] = ast_rtp_mime_types[x];
02221       y++;
02222    }
02223    mime_types_len = y;
02224    ast_rwlock_unlock(&mime_types_lock);
02225    return 0;
02226 }
02227 
02228 int ast_rtp_engine_init()
02229 {
02230    struct ast_format tmpfmt;
02231 
02232    ast_rwlock_init(&mime_types_lock);
02233    ast_rwlock_init(&static_RTP_PT_lock);
02234 
02235    /* Define all the RTP mime types available */
02236    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G723_1, 0), 0, "audio", "G723", 8000);
02237    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_GSM, 0), 0, "audio", "GSM", 8000);
02238    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0, "audio", "PCMU", 8000);
02239    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0, "audio", "G711U", 8000);
02240    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), 0, "audio", "PCMA", 8000);
02241    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), 0, "audio", "G711A", 8000);
02242    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G726, 0), 0, "audio", "G726-32", 8000);
02243    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0, "audio", "DVI4", 8000);
02244    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), 0, "audio", "L16", 8000);
02245    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0, "audio", "L16", 16000);
02246    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0, "audio", "L16-256", 16000);
02247    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_LPC10, 0), 0, "audio", "LPC", 8000);
02248    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0, "audio", "G729", 8000);
02249    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0, "audio", "G729A", 8000);
02250    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0, "audio", "G.729", 8000);
02251    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX, 0), 0, "audio", "speex", 8000);
02252    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX16, 0), 0,  "audio", "speex", 16000);
02253    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SPEEX32, 0), 0,  "audio", "speex", 32000);
02254    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_ILBC, 0), 0, "audio", "iLBC", 8000);
02255    /* this is the sample rate listed in the RTP profile for the G.722 codec, *NOT* the actual sample rate of the media stream */
02256    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G722, 0), 0, "audio", "G722", 8000);
02257    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G726_AAL2, 0), 0, "audio", "AAL2-G726-32", 8000);
02258    set_next_mime_type(NULL, AST_RTP_DTMF, "audio", "telephone-event", 8000);
02259    set_next_mime_type(NULL, AST_RTP_CISCO_DTMF, "audio", "cisco-telephone-event", 8000);
02260    set_next_mime_type(NULL, AST_RTP_CN, "audio", "CN", 8000);
02261    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_JPEG, 0), 0, "video", "JPEG", 90000);
02262    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_PNG, 0), 0, "video", "PNG", 90000);
02263    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H261, 0), 0, "video", "H261", 90000);
02264    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H263, 0), 0, "video", "H263", 90000);
02265    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0, "video", "h263-1998", 90000);
02266    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_H264, 0), 0, "video", "H264", 90000);
02267    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_MP4_VIDEO, 0), 0, "video", "MP4V-ES", 90000);
02268    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_T140RED, 0), 0, "text", "RED", 1000);
02269    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_T140, 0), 0, "text", "T140", 1000);
02270    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SIREN7, 0), 0, "audio", "G7221", 16000);
02271    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_SIREN14, 0), 0, "audio", "G7221", 32000);
02272    set_next_mime_type(ast_format_set(&tmpfmt, AST_FORMAT_G719, 0), 0, "audio", "G719", 48000);
02273 
02274    /* Define the static rtp payload mappings */
02275    add_static_payload(0, ast_format_set(&tmpfmt, AST_FORMAT_ULAW, 0), 0);
02276    #ifdef USE_DEPRECATED_G726
02277    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... */
02278    #endif
02279    add_static_payload(3, ast_format_set(&tmpfmt, AST_FORMAT_GSM, 0), 0);
02280    add_static_payload(4, ast_format_set(&tmpfmt, AST_FORMAT_G723_1, 0), 0);
02281    add_static_payload(5, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0);/* 8 kHz */
02282    add_static_payload(6, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0); /* 16 kHz */
02283    add_static_payload(7, ast_format_set(&tmpfmt, AST_FORMAT_LPC10, 0), 0);
02284    add_static_payload(8, ast_format_set(&tmpfmt, AST_FORMAT_ALAW, 0), 0);
02285    add_static_payload(9, ast_format_set(&tmpfmt, AST_FORMAT_G722, 0), 0);
02286    add_static_payload(10, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), 0); /* 2 channels */
02287    add_static_payload(11, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0), 0); /* 1 channel */
02288    add_static_payload(13, NULL, AST_RTP_CN);
02289    add_static_payload(16, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0); /* 11.025 kHz */
02290    add_static_payload(17, ast_format_set(&tmpfmt, AST_FORMAT_ADPCM, 0), 0); /* 22.050 kHz */
02291    add_static_payload(18, ast_format_set(&tmpfmt, AST_FORMAT_G729A, 0), 0);
02292    add_static_payload(19, NULL, AST_RTP_CN);         /* Also used for CN */
02293    add_static_payload(26, ast_format_set(&tmpfmt, AST_FORMAT_JPEG, 0), 0);
02294    add_static_payload(31, ast_format_set(&tmpfmt, AST_FORMAT_H261, 0), 0);
02295    add_static_payload(34, ast_format_set(&tmpfmt, AST_FORMAT_H263, 0), 0);
02296    add_static_payload(97, ast_format_set(&tmpfmt, AST_FORMAT_ILBC, 0), 0);
02297    add_static_payload(98, ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0);
02298    add_static_payload(99, ast_format_set(&tmpfmt, AST_FORMAT_H264, 0), 0);
02299    add_static_payload(101, NULL, AST_RTP_DTMF);
02300    add_static_payload(102, ast_format_set(&tmpfmt, AST_FORMAT_SIREN7, 0), 0);
02301    add_static_payload(103, ast_format_set(&tmpfmt, AST_FORMAT_H263_PLUS, 0), 0);
02302    add_static_payload(104, ast_format_set(&tmpfmt, AST_FORMAT_MP4_VIDEO, 0), 0);
02303    add_static_payload(105, ast_format_set(&tmpfmt, AST_FORMAT_T140RED, 0), 0);   /* Real time text chat (with redundancy encoding) */
02304    add_static_payload(106, ast_format_set(&tmpfmt, AST_FORMAT_T140, 0), 0);     /* Real time text chat */
02305    add_static_payload(110, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX, 0), 0);
02306    add_static_payload(111, ast_format_set(&tmpfmt, AST_FORMAT_G726, 0), 0);
02307    add_static_payload(112, ast_format_set(&tmpfmt, AST_FORMAT_G726_AAL2, 0), 0);
02308    add_static_payload(115, ast_format_set(&tmpfmt, AST_FORMAT_SIREN14, 0), 0);
02309    add_static_payload(116, ast_format_set(&tmpfmt, AST_FORMAT_G719, 0), 0);
02310    add_static_payload(117, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX16, 0), 0);
02311    add_static_payload(118, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0), 0); /* 16 Khz signed linear */
02312    add_static_payload(119, ast_format_set(&tmpfmt, AST_FORMAT_SPEEX32, 0), 0);
02313    add_static_payload(121, NULL, AST_RTP_CISCO_DTMF);   /* Must be type 121 */
02314 
02315    return 0;
02316 }

Generated on Thu Oct 11 06:33:55 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6