Sun Oct 16 2011 08:41:46

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 #include "asterisk.h"
00027 
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 331886 $")
00029 
00030 #include <math.h>
00031 
00032 #include "asterisk/channel.h"
00033 #include "asterisk/frame.h"
00034 #include "asterisk/module.h"
00035 #include "asterisk/rtp_engine.h"
00036 #include "asterisk/manager.h"
00037 #include "asterisk/options.h"
00038 #include "asterisk/astobj2.h"
00039 #include "asterisk/pbx.h"
00040 #include "asterisk/translate.h"
00041 #include "asterisk/netsock2.h"
00042 #include "asterisk/framehook.h"
00043 
00044 struct ast_srtp_res *res_srtp = NULL;
00045 struct ast_srtp_policy_res *res_srtp_policy = NULL;
00046 
00047 /*! Structure that represents an RTP session (instance) */
00048 struct ast_rtp_instance {
00049    /*! Engine that is handling this RTP instance */
00050    struct ast_rtp_engine *engine;
00051    /*! Data unique to the RTP engine */
00052    void *data;
00053    /*! RTP properties that have been set and their value */
00054    int properties[AST_RTP_PROPERTY_MAX];
00055    /*! Address that we are expecting RTP to come in to */
00056    struct ast_sockaddr local_address;
00057    /*! Address that we are sending RTP to */
00058    struct ast_sockaddr remote_address;
00059    /*! Alternate address that we are receiving RTP from */
00060    struct ast_sockaddr alt_remote_address;
00061    /*! Instance that we are bridged to if doing remote or local bridging */
00062    struct ast_rtp_instance *bridged;
00063    /*! Payload and packetization information */
00064    struct ast_rtp_codecs codecs;
00065    /*! RTP timeout time (negative or zero means disabled, negative value means temporarily disabled) */
00066    int timeout;
00067    /*! RTP timeout when on hold (negative or zero means disabled, negative value means temporarily disabled). */
00068    int holdtimeout;
00069    /*! RTP keepalive interval */
00070    int keepalive;
00071    /*! Glue currently in use */
00072    struct ast_rtp_glue *glue;
00073    /*! Channel associated with the instance */
00074    struct ast_channel *chan;
00075    /*! SRTP info associated with the instance */
00076    struct ast_srtp *srtp;
00077 };
00078 
00079 /*! List of RTP engines that are currently registered */
00080 static AST_RWLIST_HEAD_STATIC(engines, ast_rtp_engine);
00081 
00082 /*! List of RTP glues */
00083 static AST_RWLIST_HEAD_STATIC(glues, ast_rtp_glue);
00084 
00085 /*! The following array defines the MIME Media type (and subtype) for each
00086    of our codecs, or RTP-specific data type. */
00087 static const struct ast_rtp_mime_type {
00088    struct ast_rtp_payload_type payload_type;
00089    char *type;
00090    char *subtype;
00091    unsigned int sample_rate;
00092 } ast_rtp_mime_types[] = {
00093    {{1, AST_FORMAT_G723_1}, "audio", "G723", 8000},
00094    {{1, AST_FORMAT_GSM}, "audio", "GSM", 8000},
00095    {{1, AST_FORMAT_ULAW}, "audio", "PCMU", 8000},
00096    {{1, AST_FORMAT_ULAW}, "audio", "G711U", 8000},
00097    {{1, AST_FORMAT_ALAW}, "audio", "PCMA", 8000},
00098    {{1, AST_FORMAT_ALAW}, "audio", "G711A", 8000},
00099    {{1, AST_FORMAT_G726}, "audio", "G726-32", 8000},
00100    {{1, AST_FORMAT_ADPCM}, "audio", "DVI4", 8000},
00101    {{1, AST_FORMAT_SLINEAR}, "audio", "L16", 8000},
00102    {{1, AST_FORMAT_SLINEAR16}, "audio", "L16", 16000},
00103    {{1, AST_FORMAT_LPC10}, "audio", "LPC", 8000},
00104    {{1, AST_FORMAT_G729A}, "audio", "G729", 8000},
00105    {{1, AST_FORMAT_G729A}, "audio", "G729A", 8000},
00106    {{1, AST_FORMAT_G729A}, "audio", "G.729", 8000},
00107    {{1, AST_FORMAT_SPEEX}, "audio", "speex", 8000},
00108    {{1, AST_FORMAT_SPEEX16}, "audio", "speex", 16000},
00109    {{1, AST_FORMAT_ILBC}, "audio", "iLBC", 8000},
00110    /* this is the sample rate listed in the RTP profile for the G.722
00111                  codec, *NOT* the actual sample rate of the media stream
00112    */
00113    {{1, AST_FORMAT_G722}, "audio", "G722", 8000},
00114    {{1, AST_FORMAT_G726_AAL2}, "audio", "AAL2-G726-32", 8000},
00115    {{0, AST_RTP_DTMF}, "audio", "telephone-event", 8000},
00116    {{0, AST_RTP_CISCO_DTMF}, "audio", "cisco-telephone-event", 8000},
00117    {{0, AST_RTP_CN}, "audio", "CN", 8000},
00118    {{1, AST_FORMAT_JPEG}, "video", "JPEG", 90000},
00119    {{1, AST_FORMAT_PNG}, "video", "PNG", 90000},
00120    {{1, AST_FORMAT_H261}, "video", "H261", 90000},
00121    {{1, AST_FORMAT_H263}, "video", "H263", 90000},
00122    {{1, AST_FORMAT_H263_PLUS}, "video", "h263-1998", 90000},
00123    {{1, AST_FORMAT_H264}, "video", "H264", 90000},
00124    {{1, AST_FORMAT_MP4_VIDEO}, "video", "MP4V-ES", 90000},
00125    {{1, AST_FORMAT_T140RED}, "text", "RED", 1000},
00126    {{1, AST_FORMAT_T140}, "text", "T140", 1000},
00127    {{1, AST_FORMAT_SIREN7}, "audio", "G7221", 16000},
00128    {{1, AST_FORMAT_SIREN14}, "audio", "G7221", 32000},
00129    {{1, AST_FORMAT_G719}, "audio", "G719", 48000},
00130 };
00131 
00132 /*!
00133  * \brief Mapping between Asterisk codecs and rtp payload types
00134  *
00135  * Static (i.e., well-known) RTP payload types for our "AST_FORMAT..."s:
00136  * also, our own choices for dynamic payload types.  This is our master
00137  * table for transmission
00138  *
00139  * See http://www.iana.org/assignments/rtp-parameters for a list of
00140  * assigned values
00141  */
00142 static const struct ast_rtp_payload_type static_RTP_PT[AST_RTP_MAX_PT] = {
00143    [0] = {1, AST_FORMAT_ULAW},
00144    #ifdef USE_DEPRECATED_G726
00145    [2] = {1, AST_FORMAT_G726}, /* Technically this is G.721, but if Cisco can do it, so can we... */
00146    #endif
00147    [3] = {1, AST_FORMAT_GSM},
00148    [4] = {1, AST_FORMAT_G723_1},
00149    [5] = {1, AST_FORMAT_ADPCM}, /* 8 kHz */
00150    [6] = {1, AST_FORMAT_ADPCM}, /* 16 kHz */
00151    [7] = {1, AST_FORMAT_LPC10},
00152    [8] = {1, AST_FORMAT_ALAW},
00153    [9] = {1, AST_FORMAT_G722},
00154    [10] = {1, AST_FORMAT_SLINEAR}, /* 2 channels */
00155    [11] = {1, AST_FORMAT_SLINEAR}, /* 1 channel */
00156    [13] = {0, AST_RTP_CN},
00157    [16] = {1, AST_FORMAT_ADPCM}, /* 11.025 kHz */
00158    [17] = {1, AST_FORMAT_ADPCM}, /* 22.050 kHz */
00159    [18] = {1, AST_FORMAT_G729A},
00160    [19] = {0, AST_RTP_CN},         /* Also used for CN */
00161    [26] = {1, AST_FORMAT_JPEG},
00162    [31] = {1, AST_FORMAT_H261},
00163    [34] = {1, AST_FORMAT_H263},
00164    [97] = {1, AST_FORMAT_ILBC},
00165    [98] = {1, AST_FORMAT_H263_PLUS},
00166    [99] = {1, AST_FORMAT_H264},
00167    [101] = {0, AST_RTP_DTMF},
00168    [102] = {1, AST_FORMAT_SIREN7},
00169    [103] = {1, AST_FORMAT_H263_PLUS},
00170    [104] = {1, AST_FORMAT_MP4_VIDEO},
00171    [105] = {1, AST_FORMAT_T140RED},   /* Real time text chat (with redundancy encoding) */
00172    [106] = {1, AST_FORMAT_T140},      /* Real time text chat */
00173    [110] = {1, AST_FORMAT_SPEEX},
00174    [111] = {1, AST_FORMAT_G726},
00175    [112] = {1, AST_FORMAT_G726_AAL2},
00176    [115] = {1, AST_FORMAT_SIREN14},
00177    [116] = {1, AST_FORMAT_G719},
00178    [117] = {1, AST_FORMAT_SPEEX16},
00179    [118] = {1, AST_FORMAT_SLINEAR16}, /* 16 Khz signed linear */
00180    [121] = {0, AST_RTP_CISCO_DTMF},   /* Must be type 121 */
00181 };
00182 
00183 int ast_rtp_engine_register2(struct ast_rtp_engine *engine, struct ast_module *module)
00184 {
00185    struct ast_rtp_engine *current_engine;
00186 
00187    /* Perform a sanity check on the engine structure to make sure it has the basics */
00188    if (ast_strlen_zero(engine->name) || !engine->new || !engine->destroy || !engine->write || !engine->read) {
00189       ast_log(LOG_WARNING, "RTP Engine '%s' failed sanity check so it was not registered.\n", !ast_strlen_zero(engine->name) ? engine->name : "Unknown");
00190       return -1;
00191    }
00192 
00193    /* Link owner module to the RTP engine for reference counting purposes */
00194    engine->mod = module;
00195 
00196    AST_RWLIST_WRLOCK(&engines);
00197 
00198    /* Ensure that no two modules with the same name are registered at the same time */
00199    AST_RWLIST_TRAVERSE(&engines, current_engine, entry) {
00200       if (!strcmp(current_engine->name, engine->name)) {
00201          ast_log(LOG_WARNING, "An RTP engine with the name '%s' has already been registered.\n", engine->name);
00202          AST_RWLIST_UNLOCK(&engines);
00203          return -1;
00204       }
00205    }
00206 
00207    /* The engine survived our critique. Off to the list it goes to be used */
00208    AST_RWLIST_INSERT_TAIL(&engines, engine, entry);
00209 
00210    AST_RWLIST_UNLOCK(&engines);
00211 
00212    ast_verb(2, "Registered RTP engine '%s'\n", engine->name);
00213 
00214    return 0;
00215 }
00216 
00217 int ast_rtp_engine_unregister(struct ast_rtp_engine *engine)
00218 {
00219    struct ast_rtp_engine *current_engine = NULL;
00220 
00221    AST_RWLIST_WRLOCK(&engines);
00222 
00223    if ((current_engine = AST_RWLIST_REMOVE(&engines, engine, entry))) {
00224       ast_verb(2, "Unregistered RTP engine '%s'\n", engine->name);
00225    }
00226 
00227    AST_RWLIST_UNLOCK(&engines);
00228 
00229    return current_engine ? 0 : -1;
00230 }
00231 
00232 int ast_rtp_glue_register2(struct ast_rtp_glue *glue, struct ast_module *module)
00233 {
00234    struct ast_rtp_glue *current_glue = NULL;
00235 
00236    if (ast_strlen_zero(glue->type)) {
00237       return -1;
00238    }
00239 
00240    glue->mod = module;
00241 
00242    AST_RWLIST_WRLOCK(&glues);
00243 
00244    AST_RWLIST_TRAVERSE(&glues, current_glue, entry) {
00245       if (!strcasecmp(current_glue->type, glue->type)) {
00246          ast_log(LOG_WARNING, "RTP glue with the name '%s' has already been registered.\n", glue->type);
00247          AST_RWLIST_UNLOCK(&glues);
00248          return -1;
00249       }
00250    }
00251 
00252    AST_RWLIST_INSERT_TAIL(&glues, glue, entry);
00253 
00254    AST_RWLIST_UNLOCK(&glues);
00255 
00256    ast_verb(2, "Registered RTP glue '%s'\n", glue->type);
00257 
00258    return 0;
00259 }
00260 
00261 int ast_rtp_glue_unregister(struct ast_rtp_glue *glue)
00262 {
00263    struct ast_rtp_glue *current_glue = NULL;
00264 
00265    AST_RWLIST_WRLOCK(&glues);
00266 
00267    if ((current_glue = AST_RWLIST_REMOVE(&glues, glue, entry))) {
00268       ast_verb(2, "Unregistered RTP glue '%s'\n", glue->type);
00269    }
00270 
00271    AST_RWLIST_UNLOCK(&glues);
00272 
00273    return current_glue ? 0 : -1;
00274 }
00275 
00276 static void instance_destructor(void *obj)
00277 {
00278    struct ast_rtp_instance *instance = obj;
00279 
00280    /* Pass us off to the engine to destroy */
00281    if (instance->data && instance->engine->destroy(instance)) {
00282       ast_debug(1, "Engine '%s' failed to destroy RTP instance '%p'\n", instance->engine->name, instance);
00283       return;
00284    }
00285 
00286    if (instance->srtp) {
00287       res_srtp->destroy(instance->srtp);
00288    }
00289 
00290    /* Drop our engine reference */
00291    ast_module_unref(instance->engine->mod);
00292 
00293    ast_debug(1, "Destroyed RTP instance '%p'\n", instance);
00294 }
00295 
00296 int ast_rtp_instance_destroy(struct ast_rtp_instance *instance)
00297 {
00298    ao2_ref(instance, -1);
00299 
00300    return 0;
00301 }
00302 
00303 struct ast_rtp_instance *ast_rtp_instance_new(const char *engine_name,
00304       struct sched_context *sched, const struct ast_sockaddr *sa,
00305       void *data)
00306 {
00307    struct ast_sockaddr address = {{0,}};
00308    struct ast_rtp_instance *instance = NULL;
00309    struct ast_rtp_engine *engine = NULL;
00310 
00311    AST_RWLIST_RDLOCK(&engines);
00312 
00313    /* If an engine name was specified try to use it or otherwise use the first one registered */
00314    if (!ast_strlen_zero(engine_name)) {
00315       AST_RWLIST_TRAVERSE(&engines, engine, entry) {
00316          if (!strcmp(engine->name, engine_name)) {
00317             break;
00318          }
00319       }
00320    } else {
00321       engine = AST_RWLIST_FIRST(&engines);
00322    }
00323 
00324    /* If no engine was actually found bail out now */
00325    if (!engine) {
00326       ast_log(LOG_ERROR, "No RTP engine was found. Do you have one loaded?\n");
00327       AST_RWLIST_UNLOCK(&engines);
00328       return NULL;
00329    }
00330 
00331    /* Bump up the reference count before we return so the module can not be unloaded */
00332    ast_module_ref(engine->mod);
00333 
00334    AST_RWLIST_UNLOCK(&engines);
00335 
00336    /* Allocate a new RTP instance */
00337    if (!(instance = ao2_alloc(sizeof(*instance), instance_destructor))) {
00338       ast_module_unref(engine->mod);
00339       return NULL;
00340    }
00341    instance->engine = engine;
00342    ast_sockaddr_copy(&instance->local_address, sa);
00343    ast_sockaddr_copy(&address, sa);
00344 
00345    ast_debug(1, "Using engine '%s' for RTP instance '%p'\n", engine->name, instance);
00346 
00347    /* And pass it off to the engine to setup */
00348    if (instance->engine->new(instance, sched, &address, data)) {
00349       ast_debug(1, "Engine '%s' failed to setup RTP instance '%p'\n", engine->name, instance);
00350       ao2_ref(instance, -1);
00351       return NULL;
00352    }
00353 
00354    ast_debug(1, "RTP instance '%p' is setup and ready to go\n", instance);
00355 
00356    return instance;
00357 }
00358 
00359 void ast_rtp_instance_set_data(struct ast_rtp_instance *instance, void *data)
00360 {
00361    instance->data = data;
00362 }
00363 
00364 void *ast_rtp_instance_get_data(struct ast_rtp_instance *instance)
00365 {
00366    return instance->data;
00367 }
00368 
00369 int ast_rtp_instance_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
00370 {
00371    return instance->engine->write(instance, frame);
00372 }
00373 
00374 struct ast_frame *ast_rtp_instance_read(struct ast_rtp_instance *instance, int rtcp)
00375 {
00376    return instance->engine->read(instance, rtcp);
00377 }
00378 
00379 int ast_rtp_instance_set_local_address(struct ast_rtp_instance *instance,
00380       const struct ast_sockaddr *address)
00381 {
00382    ast_sockaddr_copy(&instance->local_address, address);
00383    return 0;
00384 }
00385 
00386 int ast_rtp_instance_set_remote_address(struct ast_rtp_instance *instance,
00387       const struct ast_sockaddr *address)
00388 {
00389    ast_sockaddr_copy(&instance->remote_address, address);
00390 
00391    /* moo */
00392 
00393    if (instance->engine->remote_address_set) {
00394       instance->engine->remote_address_set(instance, &instance->remote_address);
00395    }
00396 
00397    return 0;
00398 }
00399 
00400 int ast_rtp_instance_set_alt_remote_address(struct ast_rtp_instance *instance,
00401       const struct ast_sockaddr *address)
00402 {
00403    ast_sockaddr_copy(&instance->alt_remote_address, address);
00404 
00405    /* oink */
00406 
00407    if (instance->engine->alt_remote_address_set) {
00408       instance->engine->alt_remote_address_set(instance, &instance->alt_remote_address);
00409    }
00410 
00411    return 0;
00412 }
00413 
00414 int ast_rtp_instance_get_and_cmp_local_address(struct ast_rtp_instance *instance,
00415       struct ast_sockaddr *address)
00416 {
00417    if (ast_sockaddr_cmp(address, &instance->local_address) != 0) {
00418       ast_sockaddr_copy(address, &instance->local_address);
00419       return 1;
00420    }
00421 
00422    return 0;
00423 }
00424 
00425 void ast_rtp_instance_get_local_address(struct ast_rtp_instance *instance,
00426       struct ast_sockaddr *address)
00427 {
00428    ast_sockaddr_copy(address, &instance->local_address);
00429 }
00430 
00431 int ast_rtp_instance_get_and_cmp_remote_address(struct ast_rtp_instance *instance,
00432       struct ast_sockaddr *address)
00433 {
00434    if (ast_sockaddr_cmp(address, &instance->remote_address) != 0) {
00435       ast_sockaddr_copy(address, &instance->remote_address);
00436       return 1;
00437    }
00438 
00439    return 0;
00440 }
00441 
00442 void ast_rtp_instance_get_remote_address(struct ast_rtp_instance *instance,
00443       struct ast_sockaddr *address)
00444 {
00445    ast_sockaddr_copy(address, &instance->remote_address);
00446 }
00447 
00448 void ast_rtp_instance_set_extended_prop(struct ast_rtp_instance *instance, int property, void *value)
00449 {
00450    if (instance->engine->extended_prop_set) {
00451       instance->engine->extended_prop_set(instance, property, value);
00452    }
00453 }
00454 
00455 void *ast_rtp_instance_get_extended_prop(struct ast_rtp_instance *instance, int property)
00456 {
00457    if (instance->engine->extended_prop_get) {
00458       return instance->engine->extended_prop_get(instance, property);
00459    }
00460 
00461    return NULL;
00462 }
00463 
00464 void ast_rtp_instance_set_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value)
00465 {
00466    instance->properties[property] = value;
00467 
00468    if (instance->engine->prop_set) {
00469       instance->engine->prop_set(instance, property, value);
00470    }
00471 }
00472 
00473 int ast_rtp_instance_get_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property)
00474 {
00475    return instance->properties[property];
00476 }
00477 
00478 struct ast_rtp_codecs *ast_rtp_instance_get_codecs(struct ast_rtp_instance *instance)
00479 {
00480    return &instance->codecs;
00481 }
00482 
00483 void ast_rtp_codecs_payloads_clear(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
00484 {
00485    int i;
00486 
00487    for (i = 0; i < AST_RTP_MAX_PT; i++) {
00488       codecs->payloads[i].asterisk_format = 0;
00489       codecs->payloads[i].code = 0;
00490       if (instance && instance->engine && instance->engine->payload_set) {
00491          instance->engine->payload_set(instance, i, 0, 0);
00492       }
00493    }
00494 }
00495 
00496 void ast_rtp_codecs_payloads_default(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
00497 {
00498    int i;
00499 
00500    for (i = 0; i < AST_RTP_MAX_PT; i++) {
00501       if (static_RTP_PT[i].code) {
00502          codecs->payloads[i].asterisk_format = static_RTP_PT[i].asterisk_format;
00503          codecs->payloads[i].code = static_RTP_PT[i].code;
00504          if (instance && instance->engine && instance->engine->payload_set) {
00505             instance->engine->payload_set(instance, i, codecs->payloads[i].asterisk_format, codecs->payloads[i].code);
00506          }
00507       }
00508    }
00509 }
00510 
00511 void ast_rtp_codecs_payloads_copy(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
00512 {
00513    int i;
00514 
00515    for (i = 0; i < AST_RTP_MAX_PT; i++) {
00516       if (src->payloads[i].code) {
00517          ast_debug(2, "Copying payload %d from %p to %p\n", i, src, dest);
00518          dest->payloads[i].asterisk_format = src->payloads[i].asterisk_format;
00519          dest->payloads[i].code = src->payloads[i].code;
00520          if (instance && instance->engine && instance->engine->payload_set) {
00521             instance->engine->payload_set(instance, i, dest->payloads[i].asterisk_format, dest->payloads[i].code);
00522          }
00523       }
00524    }
00525 }
00526 
00527 void ast_rtp_codecs_payloads_set_m_type(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
00528 {
00529    if (payload < 0 || payload >= AST_RTP_MAX_PT || !static_RTP_PT[payload].code) {
00530       return;
00531    }
00532 
00533    codecs->payloads[payload].asterisk_format = static_RTP_PT[payload].asterisk_format;
00534    codecs->payloads[payload].code = static_RTP_PT[payload].code;
00535 
00536    ast_debug(1, "Setting payload %d based on m type on %p\n", payload, codecs);
00537 
00538    if (instance && instance->engine && instance->engine->payload_set) {
00539       instance->engine->payload_set(instance, payload, codecs->payloads[payload].asterisk_format, codecs->payloads[payload].code);
00540    }
00541 }
00542 
00543 int ast_rtp_codecs_payloads_set_rtpmap_type_rate(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int pt,
00544              char *mimetype, char *mimesubtype,
00545              enum ast_rtp_options options,
00546              unsigned int sample_rate)
00547 {
00548    unsigned int i;
00549    int found = 0;
00550 
00551    if (pt < 0 || pt >= AST_RTP_MAX_PT)
00552       return -1; /* bogus payload type */
00553 
00554    for (i = 0; i < ARRAY_LEN(ast_rtp_mime_types); ++i) {
00555       const struct ast_rtp_mime_type *t = &ast_rtp_mime_types[i];
00556 
00557       if (strcasecmp(mimesubtype, t->subtype)) {
00558          continue;
00559       }
00560 
00561       if (strcasecmp(mimetype, t->type)) {
00562          continue;
00563       }
00564 
00565       /* if both sample rates have been supplied, and they don't match,
00566        * then this not a match; if one has not been supplied, then the
00567        * rates are not compared */
00568       if (sample_rate && t->sample_rate &&
00569           (sample_rate != t->sample_rate)) {
00570          continue;
00571       }
00572 
00573       found = 1;
00574       codecs->payloads[pt] = t->payload_type;
00575 
00576       if ((t->payload_type.code == AST_FORMAT_G726) &&
00577                               t->payload_type.asterisk_format &&
00578           (options & AST_RTP_OPT_G726_NONSTANDARD)) {
00579          codecs->payloads[pt].code = AST_FORMAT_G726_AAL2;
00580       }
00581 
00582       if (instance && instance->engine && instance->engine->payload_set) {
00583          instance->engine->payload_set(instance, pt, codecs->payloads[i].asterisk_format, codecs->payloads[i].code);
00584       }
00585 
00586       break;
00587    }
00588 
00589    return (found ? 0 : -2);
00590 }
00591 
00592 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)
00593 {
00594    return ast_rtp_codecs_payloads_set_rtpmap_type_rate(codecs, instance, payload, mimetype, mimesubtype, options, 0);
00595 }
00596 
00597 void ast_rtp_codecs_payloads_unset(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
00598 {
00599    if (payload < 0 || payload >= AST_RTP_MAX_PT) {
00600       return;
00601    }
00602 
00603    ast_debug(2, "Unsetting payload %d on %p\n", payload, codecs);
00604 
00605    codecs->payloads[payload].asterisk_format = 0;
00606    codecs->payloads[payload].code = 0;
00607 
00608    if (instance && instance->engine && instance->engine->payload_set) {
00609       instance->engine->payload_set(instance, payload, 0, 0);
00610    }
00611 }
00612 
00613 struct ast_rtp_payload_type ast_rtp_codecs_payload_lookup(struct ast_rtp_codecs *codecs, int payload)
00614 {
00615    struct ast_rtp_payload_type result = { .asterisk_format = 0, };
00616 
00617    if (payload < 0 || payload >= AST_RTP_MAX_PT) {
00618       return result;
00619    }
00620 
00621    result.asterisk_format = codecs->payloads[payload].asterisk_format;
00622    result.code = codecs->payloads[payload].code;
00623 
00624    if (!result.code) {
00625       result = static_RTP_PT[payload];
00626    }
00627 
00628    return result;
00629 }
00630 
00631 void ast_rtp_codecs_payload_formats(struct ast_rtp_codecs *codecs, format_t *astformats, int *nonastformats)
00632 {
00633    int i;
00634 
00635    *astformats = *nonastformats = 0;
00636 
00637    for (i = 0; i < AST_RTP_MAX_PT; i++) {
00638       if (codecs->payloads[i].code) {
00639          ast_debug(1, "Incorporating payload %d on %p\n", i, codecs);
00640       }
00641       if (codecs->payloads[i].asterisk_format) {
00642          *astformats |= codecs->payloads[i].code;
00643       } else {
00644          *nonastformats |= codecs->payloads[i].code;
00645       }
00646    }
00647 }
00648 
00649 int ast_rtp_codecs_payload_code(struct ast_rtp_codecs *codecs, const int asterisk_format, const format_t code)
00650 {
00651    int i;
00652 
00653    for (i = 0; i < AST_RTP_MAX_PT; i++) {
00654       if (codecs->payloads[i].asterisk_format == asterisk_format && codecs->payloads[i].code == code) {
00655          return i;
00656       }
00657    }
00658 
00659    for (i = 0; i < AST_RTP_MAX_PT; i++) {
00660       if (static_RTP_PT[i].asterisk_format == asterisk_format && static_RTP_PT[i].code == code) {
00661          return i;
00662       }
00663    }
00664 
00665    return -1;
00666 }
00667 
00668 const char *ast_rtp_lookup_mime_subtype2(const int asterisk_format, const format_t code, enum ast_rtp_options options)
00669 {
00670    int i;
00671 
00672    for (i = 0; i < ARRAY_LEN(ast_rtp_mime_types); i++) {
00673       if (ast_rtp_mime_types[i].payload_type.code == code && ast_rtp_mime_types[i].payload_type.asterisk_format == asterisk_format) {
00674          if (asterisk_format && (code == AST_FORMAT_G726_AAL2) && (options & AST_RTP_OPT_G726_NONSTANDARD)) {
00675             return "G726-32";
00676          } else {
00677             return ast_rtp_mime_types[i].subtype;
00678          }
00679       }
00680    }
00681 
00682    return "";
00683 }
00684 
00685 unsigned int ast_rtp_lookup_sample_rate2(int asterisk_format, format_t code)
00686 {
00687    unsigned int i;
00688 
00689    for (i = 0; i < ARRAY_LEN(ast_rtp_mime_types); ++i) {
00690       if ((ast_rtp_mime_types[i].payload_type.code == code) && (ast_rtp_mime_types[i].payload_type.asterisk_format == asterisk_format)) {
00691          return ast_rtp_mime_types[i].sample_rate;
00692       }
00693    }
00694 
00695    return 0;
00696 }
00697 
00698 char *ast_rtp_lookup_mime_multiple2(struct ast_str *buf, const format_t capability, const int asterisk_format, enum ast_rtp_options options)
00699 {
00700    format_t format;
00701    int found = 0;
00702 
00703    if (!buf) {
00704       return NULL;
00705    }
00706 
00707    ast_str_append(&buf, 0, "0x%llx (", (unsigned long long) capability);
00708 
00709    for (format = 1; format < AST_RTP_MAX; format <<= 1) {
00710       if (capability & format) {
00711          const char *name = ast_rtp_lookup_mime_subtype2(asterisk_format, format, options);
00712          ast_str_append(&buf, 0, "%s|", name);
00713          found = 1;
00714       }
00715    }
00716 
00717    ast_str_append(&buf, 0, "%s", found ? ")" : "nothing)");
00718 
00719    return ast_str_buffer(buf);
00720 }
00721 
00722 void ast_rtp_codecs_packetization_set(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, struct ast_codec_pref *prefs)
00723 {
00724    codecs->pref = *prefs;
00725 
00726    if (instance && instance->engine->packetization_set) {
00727       instance->engine->packetization_set(instance, &instance->codecs.pref);
00728    }
00729 }
00730 
00731 int ast_rtp_instance_dtmf_begin(struct ast_rtp_instance *instance, char digit)
00732 {
00733    return instance->engine->dtmf_begin ? instance->engine->dtmf_begin(instance, digit) : -1;
00734 }
00735 
00736 int ast_rtp_instance_dtmf_end(struct ast_rtp_instance *instance, char digit)
00737 {
00738    return instance->engine->dtmf_end ? instance->engine->dtmf_end(instance, digit) : -1;
00739 }
00740 int ast_rtp_instance_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration)
00741 {
00742    return instance->engine->dtmf_end_with_duration ? instance->engine->dtmf_end_with_duration(instance, digit, duration) : -1;
00743 }
00744 
00745 int ast_rtp_instance_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode)
00746 {
00747    return (!instance->engine->dtmf_mode_set || instance->engine->dtmf_mode_set(instance, dtmf_mode)) ? -1 : 0;
00748 }
00749 
00750 enum ast_rtp_dtmf_mode ast_rtp_instance_dtmf_mode_get(struct ast_rtp_instance *instance)
00751 {
00752    return instance->engine->dtmf_mode_get ? instance->engine->dtmf_mode_get(instance) : 0;
00753 }
00754 
00755 void ast_rtp_instance_update_source(struct ast_rtp_instance *instance)
00756 {
00757    if (instance->engine->update_source) {
00758       instance->engine->update_source(instance);
00759    }
00760 }
00761 
00762 void ast_rtp_instance_change_source(struct ast_rtp_instance *instance)
00763 {
00764    if (instance->engine->change_source) {
00765       instance->engine->change_source(instance);
00766    }
00767 }
00768 
00769 int ast_rtp_instance_set_qos(struct ast_rtp_instance *instance, int tos, int cos, const char *desc)
00770 {
00771    return instance->engine->qos ? instance->engine->qos(instance, tos, cos, desc) : -1;
00772 }
00773 
00774 void ast_rtp_instance_stop(struct ast_rtp_instance *instance)
00775 {
00776    if (instance->engine->stop) {
00777       instance->engine->stop(instance);
00778    }
00779 }
00780 
00781 int ast_rtp_instance_fd(struct ast_rtp_instance *instance, int rtcp)
00782 {
00783    return instance->engine->fd ? instance->engine->fd(instance, rtcp) : -1;
00784 }
00785 
00786 struct ast_rtp_glue *ast_rtp_instance_get_glue(const char *type)
00787 {
00788    struct ast_rtp_glue *glue = NULL;
00789 
00790    AST_RWLIST_RDLOCK(&glues);
00791 
00792    AST_RWLIST_TRAVERSE(&glues, glue, entry) {
00793       if (!strcasecmp(glue->type, type)) {
00794          break;
00795       }
00796    }
00797 
00798    AST_RWLIST_UNLOCK(&glues);
00799 
00800    return glue;
00801 }
00802 
00803 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)
00804 {
00805    enum ast_bridge_result res = AST_BRIDGE_FAILED;
00806    struct ast_channel *who = NULL, *other = NULL, *cs[3] = { NULL, };
00807    struct ast_frame *fr = NULL;
00808 
00809    /* Start locally bridging both instances */
00810    if (instance0->engine->local_bridge && instance0->engine->local_bridge(instance0, instance1)) {
00811       ast_debug(1, "Failed to locally bridge %s to %s, backing out.\n", c0->name, c1->name);
00812       ast_channel_unlock(c0);
00813       ast_channel_unlock(c1);
00814       return AST_BRIDGE_FAILED_NOWARN;
00815    }
00816    if (instance1->engine->local_bridge && instance1->engine->local_bridge(instance1, instance0)) {
00817       ast_debug(1, "Failed to locally bridge %s to %s, backing out.\n", c1->name, c0->name);
00818       if (instance0->engine->local_bridge) {
00819          instance0->engine->local_bridge(instance0, NULL);
00820       }
00821       ast_channel_unlock(c0);
00822       ast_channel_unlock(c1);
00823       return AST_BRIDGE_FAILED_NOWARN;
00824    }
00825 
00826    ast_channel_unlock(c0);
00827    ast_channel_unlock(c1);
00828 
00829    instance0->bridged = instance1;
00830    instance1->bridged = instance0;
00831 
00832    ast_poll_channel_add(c0, c1);
00833 
00834    /* Hop into a loop waiting for a frame from either channel */
00835    cs[0] = c0;
00836    cs[1] = c1;
00837    cs[2] = NULL;
00838    for (;;) {
00839       /* If the underlying formats have changed force this bridge to break */
00840       if ((c0->rawreadformat != c1->rawwriteformat) || (c1->rawreadformat != c0->rawwriteformat)) {
00841          ast_debug(1, "rtp-engine-local-bridge: Oooh, formats changed, backing out\n");
00842          res = AST_BRIDGE_FAILED_NOWARN;
00843          break;
00844       }
00845       /* Check if anything changed */
00846       if ((c0->tech_pvt != pvt0) ||
00847           (c1->tech_pvt != pvt1) ||
00848           (c0->masq || c0->masqr || c1->masq || c1->masqr) ||
00849           (c0->monitor || c0->audiohooks || c1->monitor || c1->audiohooks) ||
00850           (!ast_framehook_list_is_empty(c0->framehooks) || !ast_framehook_list_is_empty(c1->framehooks))) {
00851          ast_debug(1, "rtp-engine-local-bridge: Oooh, something is weird, backing out\n");
00852          /* 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 */
00853          if ((c0->masq || c0->masqr) && (fr = ast_read(c0))) {
00854             ast_frfree(fr);
00855          }
00856          if ((c1->masq || c1->masqr) && (fr = ast_read(c1))) {
00857             ast_frfree(fr);
00858          }
00859          res = AST_BRIDGE_RETRY;
00860          break;
00861       }
00862       /* Wait on a channel to feed us a frame */
00863       if (!(who = ast_waitfor_n(cs, 2, &timeoutms))) {
00864          if (!timeoutms) {
00865             res = AST_BRIDGE_RETRY;
00866             break;
00867          }
00868          ast_debug(2, "rtp-engine-local-bridge: Ooh, empty read...\n");
00869          if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
00870             break;
00871          }
00872          continue;
00873       }
00874       /* Read in frame from channel */
00875       fr = ast_read(who);
00876       other = (who == c0) ? c1 : c0;
00877       /* Depending on the frame we may need to break out of our bridge */
00878       if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
00879              ((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) |
00880              ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1)))) {
00881          /* Record received frame and who */
00882          *fo = fr;
00883          *rc = who;
00884          ast_debug(1, "rtp-engine-local-bridge: Ooh, got a %s\n", fr ? "digit" : "hangup");
00885          res = AST_BRIDGE_COMPLETE;
00886          break;
00887       } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
00888          if ((fr->subclass.integer == AST_CONTROL_HOLD) ||
00889              (fr->subclass.integer == AST_CONTROL_UNHOLD) ||
00890              (fr->subclass.integer == AST_CONTROL_VIDUPDATE) ||
00891              (fr->subclass.integer == AST_CONTROL_SRCUPDATE) ||
00892              (fr->subclass.integer == AST_CONTROL_T38_PARAMETERS)) {
00893             /* If we are going on hold, then break callback mode and P2P bridging */
00894             if (fr->subclass.integer == AST_CONTROL_HOLD) {
00895                if (instance0->engine->local_bridge) {
00896                   instance0->engine->local_bridge(instance0, NULL);
00897                }
00898                if (instance1->engine->local_bridge) {
00899                   instance1->engine->local_bridge(instance1, NULL);
00900                }
00901                instance0->bridged = NULL;
00902                instance1->bridged = NULL;
00903             } else if (fr->subclass.integer == AST_CONTROL_UNHOLD) {
00904                if (instance0->engine->local_bridge) {
00905                   instance0->engine->local_bridge(instance0, instance1);
00906                }
00907                if (instance1->engine->local_bridge) {
00908                   instance1->engine->local_bridge(instance1, instance0);
00909                }
00910                instance0->bridged = instance1;
00911                instance1->bridged = instance0;
00912             }
00913             ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
00914             ast_frfree(fr);
00915          } else if (fr->subclass.integer == AST_CONTROL_CONNECTED_LINE) {
00916             if (ast_channel_connected_line_macro(who, other, fr, other == c0, 1)) {
00917                ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
00918             }
00919             ast_frfree(fr);
00920          } else if (fr->subclass.integer == AST_CONTROL_REDIRECTING) {
00921             if (ast_channel_redirecting_macro(who, other, fr, other == c0, 1)) {
00922                ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
00923             }
00924             ast_frfree(fr);
00925          } else {
00926             *fo = fr;
00927             *rc = who;
00928             ast_debug(1, "rtp-engine-local-bridge: Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass.integer, who->name);
00929             res = AST_BRIDGE_COMPLETE;
00930             break;
00931          }
00932       } else {
00933          if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
00934              (fr->frametype == AST_FRAME_DTMF_END) ||
00935              (fr->frametype == AST_FRAME_VOICE) ||
00936              (fr->frametype == AST_FRAME_VIDEO) ||
00937              (fr->frametype == AST_FRAME_IMAGE) ||
00938              (fr->frametype == AST_FRAME_HTML) ||
00939              (fr->frametype == AST_FRAME_MODEM) ||
00940              (fr->frametype == AST_FRAME_TEXT)) {
00941             ast_write(other, fr);
00942          }
00943 
00944          ast_frfree(fr);
00945       }
00946       /* Swap priority */
00947       cs[2] = cs[0];
00948       cs[0] = cs[1];
00949       cs[1] = cs[2];
00950    }
00951 
00952    /* Stop locally bridging both instances */
00953    if (instance0->engine->local_bridge) {
00954       instance0->engine->local_bridge(instance0, NULL);
00955    }
00956    if (instance1->engine->local_bridge) {
00957       instance1->engine->local_bridge(instance1, NULL);
00958    }
00959 
00960    instance0->bridged = NULL;
00961    instance1->bridged = NULL;
00962 
00963    ast_poll_channel_del(c0, c1);
00964 
00965    return res;
00966 }
00967 
00968 static enum ast_bridge_result remote_bridge_loop(struct ast_channel *c0, struct ast_channel *c1, struct ast_rtp_instance *instance0, struct ast_rtp_instance *instance1,
00969                    struct ast_rtp_instance *vinstance0, struct ast_rtp_instance *vinstance1, struct ast_rtp_instance *tinstance0,
00970                    struct ast_rtp_instance *tinstance1, struct ast_rtp_glue *glue0, struct ast_rtp_glue *glue1, format_t codec0, format_t codec1, int timeoutms,
00971                    int flags, struct ast_frame **fo, struct ast_channel **rc, void *pvt0, void *pvt1)
00972 {
00973    enum ast_bridge_result res = AST_BRIDGE_FAILED;
00974    struct ast_channel *who = NULL, *other = NULL, *cs[3] = { NULL, };
00975    format_t oldcodec0 = codec0, oldcodec1 = codec1;
00976    struct ast_sockaddr ac1 = {{0,}}, vac1 = {{0,}}, tac1 = {{0,}}, ac0 = {{0,}}, vac0 = {{0,}}, tac0 = {{0,}};
00977    struct ast_sockaddr t1 = {{0,}}, vt1 = {{0,}}, tt1 = {{0,}}, t0 = {{0,}}, vt0 = {{0,}}, tt0 = {{0,}};
00978    struct ast_frame *fr = NULL;
00979 
00980    /* Test the first channel */
00981    if (!(glue0->update_peer(c0, instance1, vinstance1, tinstance1, codec1, 0))) {
00982       ast_rtp_instance_get_remote_address(instance1, &ac1);
00983       if (vinstance1) {
00984          ast_rtp_instance_get_remote_address(vinstance1, &vac1);
00985       }
00986       if (tinstance1) {
00987          ast_rtp_instance_get_remote_address(tinstance1, &tac1);
00988       }
00989    } else {
00990       ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c0->name, c1->name);
00991    }
00992 
00993    /* Test the second channel */
00994    if (!(glue1->update_peer(c1, instance0, vinstance0, tinstance0, codec0, 0))) {
00995       ast_rtp_instance_get_remote_address(instance0, &ac0);
00996       if (vinstance0) {
00997          ast_rtp_instance_get_remote_address(instance0, &vac0);
00998       }
00999       if (tinstance0) {
01000          ast_rtp_instance_get_remote_address(instance0, &tac0);
01001       }
01002    } else {
01003       ast_log(LOG_WARNING, "Channel '%s' failed to talk to '%s'\n", c1->name, c0->name);
01004    }
01005 
01006    ast_channel_unlock(c0);
01007    ast_channel_unlock(c1);
01008 
01009    instance0->bridged = instance1;
01010    instance1->bridged = instance0;
01011 
01012    ast_poll_channel_add(c0, c1);
01013 
01014    /* Go into a loop handling any stray frames that may come in */
01015    cs[0] = c0;
01016    cs[1] = c1;
01017    cs[2] = NULL;
01018    for (;;) {
01019       /* Check if anything changed */
01020       if ((c0->tech_pvt != pvt0) ||
01021           (c1->tech_pvt != pvt1) ||
01022           (c0->masq || c0->masqr || c1->masq || c1->masqr) ||
01023           (c0->monitor || c0->audiohooks || c1->monitor || c1->audiohooks) ||
01024           (!ast_framehook_list_is_empty(c0->framehooks) || !ast_framehook_list_is_empty(c1->framehooks))) {
01025          ast_debug(1, "Oooh, something is weird, backing out\n");
01026          res = AST_BRIDGE_RETRY;
01027          break;
01028       }
01029 
01030       /* Check if they have changed their address */
01031       ast_rtp_instance_get_remote_address(instance1, &t1);
01032       if (vinstance1) {
01033          ast_rtp_instance_get_remote_address(vinstance1, &vt1);
01034       }
01035       if (tinstance1) {
01036          ast_rtp_instance_get_remote_address(tinstance1, &tt1);
01037       }
01038       if (glue1->get_codec) {
01039          codec1 = glue1->get_codec(c1);
01040       }
01041 
01042       ast_rtp_instance_get_remote_address(instance0, &t0);
01043       if (vinstance0) {
01044          ast_rtp_instance_get_remote_address(vinstance0, &vt0);
01045       }
01046       if (tinstance0) {
01047          ast_rtp_instance_get_remote_address(tinstance0, &tt0);
01048       }
01049       if (glue0->get_codec) {
01050          codec0 = glue0->get_codec(c0);
01051       }
01052 
01053       if ((ast_sockaddr_cmp(&t1, &ac1)) ||
01054           (vinstance1 && ast_sockaddr_cmp(&vt1, &vac1)) ||
01055           (tinstance1 && ast_sockaddr_cmp(&tt1, &tac1)) ||
01056           (codec1 != oldcodec1)) {
01057          ast_debug(1, "Oooh, '%s' changed end address to %s (format %s)\n",
01058               c1->name, ast_sockaddr_stringify(&t1),
01059               ast_getformatname(codec1));
01060          ast_debug(1, "Oooh, '%s' changed end vaddress to %s (format %s)\n",
01061               c1->name, ast_sockaddr_stringify(&vt1),
01062               ast_getformatname(codec1));
01063          ast_debug(1, "Oooh, '%s' changed end taddress to %s (format %s)\n",
01064               c1->name, ast_sockaddr_stringify(&tt1),
01065               ast_getformatname(codec1));
01066          ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
01067               c1->name, ast_sockaddr_stringify(&ac1),
01068               ast_getformatname(oldcodec1));
01069          ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
01070               c1->name, ast_sockaddr_stringify(&vac1),
01071               ast_getformatname(oldcodec1));
01072          ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
01073               c1->name, ast_sockaddr_stringify(&tac1),
01074               ast_getformatname(oldcodec1));
01075          if (glue0->update_peer(c0,
01076                       ast_sockaddr_isnull(&t1)  ? NULL : instance1,
01077                       ast_sockaddr_isnull(&vt1) ? NULL : vinstance1,
01078                       ast_sockaddr_isnull(&tt1) ? NULL : tinstance1,
01079                       codec1, 0)) {
01080             ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c0->name, c1->name);
01081          }
01082          ast_sockaddr_copy(&ac1, &t1);
01083          ast_sockaddr_copy(&vac1, &vt1);
01084          ast_sockaddr_copy(&tac1, &tt1);
01085          oldcodec1 = codec1;
01086       }
01087       if ((ast_sockaddr_cmp(&t0, &ac0)) ||
01088           (vinstance0 && ast_sockaddr_cmp(&vt0, &vac0)) ||
01089           (tinstance0 && ast_sockaddr_cmp(&tt0, &tac0)) ||
01090           (codec0 != oldcodec0)) {
01091          ast_debug(1, "Oooh, '%s' changed end address to %s (format %s)\n",
01092               c0->name, ast_sockaddr_stringify(&t0),
01093               ast_getformatname(codec0));
01094          ast_debug(1, "Oooh, '%s' was %s/(format %s)\n",
01095               c0->name, ast_sockaddr_stringify(&ac0),
01096               ast_getformatname(oldcodec0));
01097          if (glue1->update_peer(c1, t0.len ? instance0 : NULL,
01098                   vt0.len ? vinstance0 : NULL,
01099                   tt0.len ? tinstance0 : NULL,
01100                   codec0, 0)) {
01101             ast_log(LOG_WARNING, "Channel '%s' failed to update to '%s'\n", c1->name, c0->name);
01102          }
01103          ast_sockaddr_copy(&ac0, &t0);
01104          ast_sockaddr_copy(&vac0, &vt0);
01105          ast_sockaddr_copy(&tac0, &tt0);
01106          oldcodec0 = codec0;
01107       }
01108 
01109       /* Wait for frame to come in on the channels */
01110       if (!(who = ast_waitfor_n(cs, 2, &timeoutms))) {
01111          if (!timeoutms) {
01112             res = AST_BRIDGE_RETRY;
01113             break;
01114          }
01115          ast_debug(1, "Ooh, empty read...\n");
01116          if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
01117             break;
01118          }
01119          continue;
01120       }
01121       fr = ast_read(who);
01122       other = (who == c0) ? c1 : c0;
01123       if (!fr || ((fr->frametype == AST_FRAME_DTMF_BEGIN || fr->frametype == AST_FRAME_DTMF_END) &&
01124              (((who == c0) && (flags & AST_BRIDGE_DTMF_CHANNEL_0)) ||
01125               ((who == c1) && (flags & AST_BRIDGE_DTMF_CHANNEL_1))))) {
01126          /* Break out of bridge */
01127          *fo = fr;
01128          *rc = who;
01129          ast_debug(1, "Oooh, got a %s\n", fr ? "digit" : "hangup");
01130          res = AST_BRIDGE_COMPLETE;
01131          break;
01132       } else if ((fr->frametype == AST_FRAME_CONTROL) && !(flags & AST_BRIDGE_IGNORE_SIGS)) {
01133          if ((fr->subclass.integer == AST_CONTROL_HOLD) ||
01134              (fr->subclass.integer == AST_CONTROL_UNHOLD) ||
01135              (fr->subclass.integer == AST_CONTROL_VIDUPDATE) ||
01136              (fr->subclass.integer == AST_CONTROL_SRCUPDATE) ||
01137              (fr->subclass.integer == AST_CONTROL_T38_PARAMETERS)) {
01138             if (fr->subclass.integer == AST_CONTROL_HOLD) {
01139                /* If we someone went on hold we want the other side to reinvite back to us */
01140                if (who == c0) {
01141                   glue1->update_peer(c1, NULL, NULL, NULL, 0, 0);
01142                } else {
01143                   glue0->update_peer(c0, NULL, NULL, NULL, 0, 0);
01144                }
01145             } else if (fr->subclass.integer == AST_CONTROL_UNHOLD) {
01146                /* If they went off hold they should go back to being direct */
01147                if (who == c0) {
01148                   glue1->update_peer(c1, instance0, vinstance0, tinstance0, codec0, 0);
01149                } else {
01150                   glue0->update_peer(c0, instance1, vinstance1, tinstance1, codec1, 0);
01151                }
01152             }
01153             /* Update local address information */
01154             ast_rtp_instance_get_remote_address(instance0, &t0);
01155             ast_sockaddr_copy(&ac0, &t0);
01156             ast_rtp_instance_get_remote_address(instance1, &t1);
01157             ast_sockaddr_copy(&ac1, &t1);
01158             /* Update codec information */
01159             if (glue0->get_codec && c0->tech_pvt) {
01160                oldcodec0 = codec0 = glue0->get_codec(c0);
01161             }
01162             if (glue1->get_codec && c1->tech_pvt) {
01163                oldcodec1 = codec1 = glue1->get_codec(c1);
01164             }
01165             ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
01166             ast_frfree(fr);
01167          } else if (fr->subclass.integer == AST_CONTROL_CONNECTED_LINE) {
01168             if (ast_channel_connected_line_macro(who, other, fr, other == c0, 1)) {
01169                ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
01170             }
01171             ast_frfree(fr);
01172          } else if (fr->subclass.integer == AST_CONTROL_REDIRECTING) {
01173             if (ast_channel_redirecting_macro(who, other, fr, other == c0, 1)) {
01174                ast_indicate_data(other, fr->subclass.integer, fr->data.ptr, fr->datalen);
01175             }
01176             ast_frfree(fr);
01177          } else {
01178             *fo = fr;
01179             *rc = who;
01180             ast_debug(1, "Got a FRAME_CONTROL (%d) frame on channel %s\n", fr->subclass.integer, who->name);
01181             return AST_BRIDGE_COMPLETE;
01182          }
01183       } else {
01184          if ((fr->frametype == AST_FRAME_DTMF_BEGIN) ||
01185              (fr->frametype == AST_FRAME_DTMF_END) ||
01186              (fr->frametype == AST_FRAME_VOICE) ||
01187              (fr->frametype == AST_FRAME_VIDEO) ||
01188              (fr->frametype == AST_FRAME_IMAGE) ||
01189              (fr->frametype == AST_FRAME_HTML) ||
01190              (fr->frametype == AST_FRAME_MODEM) ||
01191              (fr->frametype == AST_FRAME_TEXT)) {
01192             ast_write(other, fr);
01193          }
01194          ast_frfree(fr);
01195       }
01196       /* Swap priority */
01197       cs[2] = cs[0];
01198       cs[0] = cs[1];
01199       cs[1] = cs[2];
01200    }
01201 
01202    if (ast_test_flag(c0, AST_FLAG_ZOMBIE)) {
01203       ast_debug(1, "Channel '%s' Zombie cleardown from bridge\n", c0->name);
01204    } else if (c0->tech_pvt != pvt0) {
01205       ast_debug(1, "Channel c0->'%s' pvt changed, in bridge with c1->'%s'\n", c0->name, c1->name);
01206    } else if (glue0 != ast_rtp_instance_get_glue(c0->tech->type)) {
01207       ast_debug(1, "Channel c0->'%s' technology changed, in bridge with c1->'%s'\n", c0->name, c1->name);
01208    } else if (glue0->update_peer(c0, NULL, NULL, NULL, 0, 0)) {
01209       ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c0->name);
01210    }
01211    if (ast_test_flag(c1, AST_FLAG_ZOMBIE)) {
01212       ast_debug(1, "Channel '%s' Zombie cleardown from bridge\n", c1->name);
01213    } else if (c1->tech_pvt != pvt1) {
01214       ast_debug(1, "Channel c1->'%s' pvt changed, in bridge with c0->'%s'\n", c1->name, c0->name);
01215    } else if (glue1 != ast_rtp_instance_get_glue(c1->tech->type)) {
01216       ast_debug(1, "Channel c1->'%s' technology changed, in bridge with c0->'%s'\n", c1->name, c0->name);
01217    } else if (glue1->update_peer(c1, NULL, NULL, NULL, 0, 0)) {
01218       ast_log(LOG_WARNING, "Channel '%s' failed to break RTP bridge\n", c1->name);
01219    }
01220 
01221    instance0->bridged = NULL;
01222    instance1->bridged = NULL;
01223 
01224    ast_poll_channel_del(c0, c1);
01225 
01226    return res;
01227 }
01228 
01229 /*!
01230  * \brief Conditionally unref an rtp instance
01231  */
01232 static void unref_instance_cond(struct ast_rtp_instance **instance)
01233 {
01234    if (*instance) {
01235       ao2_ref(*instance, -1);
01236       *instance = NULL;
01237    }
01238 }
01239 
01240 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)
01241 {
01242    struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
01243          *vinstance0 = NULL, *vinstance1 = NULL,
01244          *tinstance0 = NULL, *tinstance1 = NULL;
01245    struct ast_rtp_glue *glue0, *glue1;
01246    struct ast_sockaddr addr1 = { {0, }, }, addr2 = { {0, }, };
01247    enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
01248    enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
01249    enum ast_bridge_result res = AST_BRIDGE_FAILED;
01250    enum ast_rtp_dtmf_mode dmode;
01251    format_t codec0 = 0, codec1 = 0;
01252    int unlock_chans = 1;
01253 
01254    /* Lock both channels so we can look for the glue that binds them together */
01255    ast_channel_lock(c0);
01256    while (ast_channel_trylock(c1)) {
01257       ast_channel_unlock(c0);
01258       usleep(1);
01259       ast_channel_lock(c0);
01260    }
01261 
01262    /* Ensure neither channel got hungup during lock avoidance */
01263    if (ast_check_hangup(c0) || ast_check_hangup(c1)) {
01264       ast_log(LOG_WARNING, "Got hangup while attempting to bridge '%s' and '%s'\n", c0->name, c1->name);
01265       goto done;
01266    }
01267 
01268    /* Grab glue that binds each channel to something using the RTP engine */
01269    if (!(glue0 = ast_rtp_instance_get_glue(c0->tech->type)) || !(glue1 = ast_rtp_instance_get_glue(c1->tech->type))) {
01270       ast_debug(1, "Can't find native functions for channel '%s'\n", glue0 ? c1->name : c0->name);
01271       goto done;
01272    }
01273 
01274    audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
01275    video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
01276 
01277    audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
01278    video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
01279 
01280    /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
01281    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)) {
01282       audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
01283    }
01284    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)) {
01285       audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
01286    }
01287 
01288    /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
01289    if (audio_glue0_res == AST_RTP_GLUE_RESULT_FORBID || audio_glue1_res == AST_RTP_GLUE_RESULT_FORBID) {
01290       res = AST_BRIDGE_FAILED_NOWARN;
01291       goto done;
01292    }
01293 
01294 
01295    /* If address families differ, force a local bridge */
01296    ast_rtp_instance_get_remote_address(instance0, &addr1);
01297    ast_rtp_instance_get_remote_address(instance1, &addr2);
01298 
01299    if (addr1.ss.ss_family != addr2.ss.ss_family ||
01300       (ast_sockaddr_is_ipv4_mapped(&addr1) != ast_sockaddr_is_ipv4_mapped(&addr2))) {
01301       audio_glue0_res = AST_RTP_GLUE_RESULT_LOCAL;
01302       audio_glue1_res = AST_RTP_GLUE_RESULT_LOCAL;
01303    }
01304 
01305    /* If we need to get DTMF see if we can do it outside of the RTP stream itself */
01306    dmode = ast_rtp_instance_dtmf_mode_get(instance0);
01307    if ((flags & AST_BRIDGE_DTMF_CHANNEL_0) && dmode) {
01308       res = AST_BRIDGE_FAILED_NOWARN;
01309       goto done;
01310    }
01311    dmode = ast_rtp_instance_dtmf_mode_get(instance1);
01312    if ((flags & AST_BRIDGE_DTMF_CHANNEL_1) && dmode) {
01313       res = AST_BRIDGE_FAILED_NOWARN;
01314       goto done;
01315    }
01316 
01317    /* 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 */
01318    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)))) {
01319       res = AST_BRIDGE_FAILED_NOWARN;
01320       goto done;
01321    }
01322 
01323    /* Make sure that codecs match */
01324    codec0 = glue0->get_codec ? glue0->get_codec(c0) : 0;
01325    codec1 = glue1->get_codec ? glue1->get_codec(c1) : 0;
01326    if (codec0 && codec1 && !(codec0 & codec1)) {
01327       ast_debug(1, "Channel codec0 = %s is not codec1 = %s, cannot native bridge in RTP.\n", ast_getformatname(codec0), ast_getformatname(codec1));
01328       res = AST_BRIDGE_FAILED_NOWARN;
01329       goto done;
01330    }
01331 
01332    instance0->glue = glue0;
01333    instance1->glue = glue1;
01334    instance0->chan = c0;
01335    instance1->chan = c1;
01336 
01337    /* Depending on the end result for bridging either do a local bridge or remote bridge */
01338    if (audio_glue0_res == AST_RTP_GLUE_RESULT_LOCAL || audio_glue1_res == AST_RTP_GLUE_RESULT_LOCAL) {
01339       ast_verb(3, "Locally bridging %s and %s\n", c0->name, c1->name);
01340       res = local_bridge_loop(c0, c1, instance0, instance1, timeoutms, flags, fo, rc, c0->tech_pvt, c1->tech_pvt);
01341    } else {
01342       ast_verb(3, "Remotely bridging %s and %s\n", c0->name, c1->name);
01343       res = remote_bridge_loop(c0, c1, instance0, instance1, vinstance0, vinstance1,
01344             tinstance0, tinstance1, glue0, glue1, codec0, codec1, timeoutms, flags,
01345             fo, rc, c0->tech_pvt, c1->tech_pvt);
01346    }
01347 
01348    instance0->glue = NULL;
01349    instance1->glue = NULL;
01350    instance0->chan = NULL;
01351    instance1->chan = NULL;
01352 
01353    unlock_chans = 0;
01354 
01355 done:
01356    if (unlock_chans) {
01357       ast_channel_unlock(c0);
01358       ast_channel_unlock(c1);
01359    }
01360 
01361    unref_instance_cond(&instance0);
01362    unref_instance_cond(&instance1);
01363    unref_instance_cond(&vinstance0);
01364    unref_instance_cond(&vinstance1);
01365    unref_instance_cond(&tinstance0);
01366    unref_instance_cond(&tinstance1);
01367 
01368    return res;
01369 }
01370 
01371 struct ast_rtp_instance *ast_rtp_instance_get_bridged(struct ast_rtp_instance *instance)
01372 {
01373    return instance->bridged;
01374 }
01375 
01376 void ast_rtp_instance_early_bridge_make_compatible(struct ast_channel *c0, struct ast_channel *c1)
01377 {
01378    struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
01379       *vinstance0 = NULL, *vinstance1 = NULL,
01380       *tinstance0 = NULL, *tinstance1 = NULL;
01381    struct ast_rtp_glue *glue0, *glue1;
01382    enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
01383    enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
01384    format_t codec0 = 0, codec1 = 0;
01385    int res = 0;
01386 
01387    /* Lock both channels so we can look for the glue that binds them together */
01388    ast_channel_lock(c0);
01389    while (ast_channel_trylock(c1)) {
01390       ast_channel_unlock(c0);
01391       usleep(1);
01392       ast_channel_lock(c0);
01393    }
01394 
01395    /* Grab glue that binds each channel to something using the RTP engine */
01396    if (!(glue0 = ast_rtp_instance_get_glue(c0->tech->type)) || !(glue1 = ast_rtp_instance_get_glue(c1->tech->type))) {
01397       ast_debug(1, "Can't find native functions for channel '%s'\n", glue0 ? c1->name : c0->name);
01398       goto done;
01399    }
01400 
01401    audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
01402    video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
01403 
01404    audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
01405    video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
01406 
01407    /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
01408    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)) {
01409       audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
01410    }
01411    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)) {
01412       audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
01413    }
01414    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) {
01415       codec0 = glue0->get_codec(c0);
01416    }
01417    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) {
01418       codec1 = glue1->get_codec(c1);
01419    }
01420 
01421    /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
01422    if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
01423       goto done;
01424    }
01425 
01426    /* Make sure we have matching codecs */
01427    if (!(codec0 & codec1)) {
01428       goto done;
01429    }
01430 
01431    ast_rtp_codecs_payloads_copy(&instance0->codecs, &instance1->codecs, instance1);
01432 
01433    if (vinstance0 && vinstance1) {
01434       ast_rtp_codecs_payloads_copy(&vinstance0->codecs, &vinstance1->codecs, vinstance1);
01435    }
01436    if (tinstance0 && tinstance1) {
01437       ast_rtp_codecs_payloads_copy(&tinstance0->codecs, &tinstance1->codecs, tinstance1);
01438    }
01439 
01440    res = 0;
01441 
01442 done:
01443    ast_channel_unlock(c0);
01444    ast_channel_unlock(c1);
01445 
01446    unref_instance_cond(&instance0);
01447    unref_instance_cond(&instance1);
01448    unref_instance_cond(&vinstance0);
01449    unref_instance_cond(&vinstance1);
01450    unref_instance_cond(&tinstance0);
01451    unref_instance_cond(&tinstance1);
01452 
01453    if (!res) {
01454       ast_debug(1, "Seeded SDP of '%s' with that of '%s'\n", c0->name, c1 ? c1->name : "<unspecified>");
01455    }
01456 }
01457 
01458 int ast_rtp_instance_early_bridge(struct ast_channel *c0, struct ast_channel *c1)
01459 {
01460    struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
01461          *vinstance0 = NULL, *vinstance1 = NULL,
01462          *tinstance0 = NULL, *tinstance1 = NULL;
01463    struct ast_rtp_glue *glue0, *glue1;
01464    enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
01465    enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
01466    format_t codec0 = 0, codec1 = 0;
01467    int res = 0;
01468 
01469    /* If there is no second channel just immediately bail out, we are of no use in that scenario */
01470    if (!c1) {
01471       return -1;
01472    }
01473 
01474    /* Lock both channels so we can look for the glue that binds them together */
01475    ast_channel_lock(c0);
01476    while (ast_channel_trylock(c1)) {
01477       ast_channel_unlock(c0);
01478       usleep(1);
01479       ast_channel_lock(c0);
01480    }
01481 
01482    /* Grab glue that binds each channel to something using the RTP engine */
01483    if (!(glue0 = ast_rtp_instance_get_glue(c0->tech->type)) || !(glue1 = ast_rtp_instance_get_glue(c1->tech->type))) {
01484       ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", glue0 ? c1->name : c0->name);
01485       goto done;
01486    }
01487 
01488    audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
01489    video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
01490 
01491    audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
01492    video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
01493 
01494    /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
01495    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)) {
01496       audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
01497    }
01498    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)) {
01499       audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
01500    }
01501    if (audio_glue0_res == AST_RTP_GLUE_RESULT_REMOTE && (video_glue0_res == AST_RTP_GLUE_RESULT_FORBID || video_glue0_res == AST_RTP_GLUE_RESULT_REMOTE) && glue0->get_codec(c0)) {
01502       codec0 = glue0->get_codec(c0);
01503    }
01504    if (audio_glue1_res == AST_RTP_GLUE_RESULT_REMOTE && (video_glue1_res == AST_RTP_GLUE_RESULT_FORBID || video_glue1_res == AST_RTP_GLUE_RESULT_REMOTE) && glue1->get_codec(c1)) {
01505       codec1 = glue1->get_codec(c1);
01506    }
01507 
01508    /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
01509    if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
01510       goto done;
01511    }
01512 
01513    /* Make sure we have matching codecs */
01514    if (!(codec0 & codec1)) {
01515       goto done;
01516    }
01517 
01518    /* Bridge media early */
01519    if (glue0->update_peer(c0, instance1, vinstance1, tinstance1, codec1, 0)) {
01520       ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", c0->name, c1 ? c1->name : "<unspecified>");
01521    }
01522 
01523    res = 0;
01524 
01525 done:
01526    ast_channel_unlock(c0);
01527    ast_channel_unlock(c1);
01528 
01529    unref_instance_cond(&instance0);
01530    unref_instance_cond(&instance1);
01531    unref_instance_cond(&vinstance0);
01532    unref_instance_cond(&vinstance1);
01533    unref_instance_cond(&tinstance0);
01534    unref_instance_cond(&tinstance1);
01535 
01536    if (!res) {
01537       ast_debug(1, "Setting early bridge SDP of '%s' with that of '%s'\n", c0->name, c1 ? c1->name : "<unspecified>");
01538    }
01539 
01540    return res;
01541 }
01542 
01543 int ast_rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations)
01544 {
01545    return instance->engine->red_init ? instance->engine->red_init(instance, buffer_time, payloads, generations) : -1;
01546 }
01547 
01548 int ast_rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame)
01549 {
01550    return instance->engine->red_buffer ? instance->engine->red_buffer(instance, frame) : -1;
01551 }
01552 
01553 int ast_rtp_instance_get_stats(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat)
01554 {
01555    return instance->engine->get_stat ? instance->engine->get_stat(instance, stats, stat) : -1;
01556 }
01557 
01558 char *ast_rtp_instance_get_quality(struct ast_rtp_instance *instance, enum ast_rtp_instance_stat_field field, char *buf, size_t size)
01559 {
01560    struct ast_rtp_instance_stats stats = { 0, };
01561    enum ast_rtp_instance_stat stat;
01562 
01563    /* Determine what statistics we will need to retrieve based on field passed in */
01564    if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
01565       stat = AST_RTP_INSTANCE_STAT_ALL;
01566    } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
01567       stat = AST_RTP_INSTANCE_STAT_COMBINED_JITTER;
01568    } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
01569       stat = AST_RTP_INSTANCE_STAT_COMBINED_LOSS;
01570    } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
01571       stat = AST_RTP_INSTANCE_STAT_COMBINED_RTT;
01572    } else {
01573       return NULL;
01574    }
01575 
01576    /* Attempt to actually retrieve the statistics we need to generate the quality string */
01577    if (ast_rtp_instance_get_stats(instance, &stats, stat)) {
01578       return NULL;
01579    }
01580 
01581    /* Now actually fill the buffer with the good information */
01582    if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
01583       snprintf(buf, size, "ssrc=%i;themssrc=%u;lp=%u;rxjitter=%f;rxcount=%u;txjitter=%f;txcount=%u;rlp=%u;rtt=%f",
01584           stats.local_ssrc, stats.remote_ssrc, stats.rxploss, stats.txjitter, stats.rxcount, stats.rxjitter, stats.txcount, stats.txploss, stats.rtt);
01585    } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
01586       snprintf(buf, size, "minrxjitter=%f;maxrxjitter=%f;avgrxjitter=%f;stdevrxjitter=%f;reported_minjitter=%f;reported_maxjitter=%f;reported_avgjitter=%f;reported_stdevjitter=%f;",
01587           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));
01588    } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
01589       snprintf(buf, size, "minrxlost=%f;maxrxlost=%f;avgrxlost=%f;stdevrxlost=%f;reported_minlost=%f;reported_maxlost=%f;reported_avglost=%f;reported_stdevlost=%f;",
01590           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));
01591    } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
01592       snprintf(buf, size, "minrtt=%f;maxrtt=%f;avgrtt=%f;stdevrtt=%f;", stats.minrtt, stats.maxrtt, stats.normdevrtt, stats.stdevrtt);
01593    }
01594 
01595    return buf;
01596 }
01597 
01598 void ast_rtp_instance_set_stats_vars(struct ast_channel *chan, struct ast_rtp_instance *instance)
01599 {
01600    char quality_buf[AST_MAX_USER_FIELD], *quality;
01601    struct ast_channel *bridge = ast_bridged_channel(chan);
01602 
01603    if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY, quality_buf, sizeof(quality_buf)))) {
01604       pbx_builtin_setvar_helper(chan, "RTPAUDIOQOS", quality);
01605       if (bridge) {
01606          pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSBRIDGED", quality);
01607       }
01608    }
01609 
01610    if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER, quality_buf, sizeof(quality_buf)))) {
01611       pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSJITTER", quality);
01612       if (bridge) {
01613          pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSJITTERBRIDGED", quality);
01614       }
01615    }
01616 
01617    if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS, quality_buf, sizeof(quality_buf)))) {
01618       pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSLOSS", quality);
01619       if (bridge) {
01620          pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSLOSSBRIDGED", quality);
01621       }
01622    }
01623 
01624    if ((quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT, quality_buf, sizeof(quality_buf)))) {
01625       pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSRTT", quality);
01626       if (bridge) {
01627          pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSRTTBRIDGED", quality);
01628       }
01629    }
01630 }
01631 
01632 int ast_rtp_instance_set_read_format(struct ast_rtp_instance *instance, format_t format)
01633 {
01634    return instance->engine->set_read_format ? instance->engine->set_read_format(instance, format) : -1;
01635 }
01636 
01637 int ast_rtp_instance_set_write_format(struct ast_rtp_instance *instance, format_t format)
01638 {
01639    return instance->engine->set_write_format ? instance->engine->set_write_format(instance, format) : -1;
01640 }
01641 
01642 int ast_rtp_instance_make_compatible(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_channel *peer)
01643 {
01644    struct ast_rtp_glue *glue;
01645    struct ast_rtp_instance *peer_instance = NULL;
01646    int res = -1;
01647 
01648    if (!instance->engine->make_compatible) {
01649       return -1;
01650    }
01651 
01652    ast_channel_lock(peer);
01653 
01654    if (!(glue = ast_rtp_instance_get_glue(peer->tech->type))) {
01655       ast_channel_unlock(peer);
01656       return -1;
01657    }
01658 
01659    glue->get_rtp_info(peer, &peer_instance);
01660 
01661    if (!peer_instance || peer_instance->engine != instance->engine) {
01662       ast_channel_unlock(peer);
01663       ao2_ref(peer_instance, -1);
01664       peer_instance = NULL;
01665       return -1;
01666    }
01667 
01668    res = instance->engine->make_compatible(chan, instance, peer, peer_instance);
01669 
01670    ast_channel_unlock(peer);
01671 
01672    ao2_ref(peer_instance, -1);
01673    peer_instance = NULL;
01674 
01675    return res;
01676 }
01677 
01678 format_t ast_rtp_instance_available_formats(struct ast_rtp_instance *instance, format_t to_endpoint, format_t to_asterisk)
01679 {
01680    format_t formats;
01681 
01682    if (instance->engine->available_formats && (formats = instance->engine->available_formats(instance, to_endpoint, to_asterisk))) {
01683       return formats;
01684    }
01685 
01686    return ast_translate_available_formats(to_endpoint, to_asterisk);
01687 }
01688 
01689 int ast_rtp_instance_activate(struct ast_rtp_instance *instance)
01690 {
01691    return instance->engine->activate ? instance->engine->activate(instance) : 0;
01692 }
01693 
01694 void ast_rtp_instance_stun_request(struct ast_rtp_instance *instance,
01695                struct ast_sockaddr *suggestion,
01696                const char *username)
01697 {
01698    if (instance->engine->stun_request) {
01699       instance->engine->stun_request(instance, suggestion, username);
01700    }
01701 }
01702 
01703 void ast_rtp_instance_set_timeout(struct ast_rtp_instance *instance, int timeout)
01704 {
01705    instance->timeout = timeout;
01706 }
01707 
01708 void ast_rtp_instance_set_hold_timeout(struct ast_rtp_instance *instance, int timeout)
01709 {
01710    instance->holdtimeout = timeout;
01711 }
01712 
01713 void ast_rtp_instance_set_keepalive(struct ast_rtp_instance *instance, int interval)
01714 {
01715    instance->keepalive = interval;
01716 }
01717 
01718 int ast_rtp_instance_get_timeout(struct ast_rtp_instance *instance)
01719 {
01720    return instance->timeout;
01721 }
01722 
01723 int ast_rtp_instance_get_hold_timeout(struct ast_rtp_instance *instance)
01724 {
01725    return instance->holdtimeout;
01726 }
01727 
01728 int ast_rtp_instance_get_keepalive(struct ast_rtp_instance *instance)
01729 {
01730    return instance->keepalive;
01731 }
01732 
01733 struct ast_rtp_engine *ast_rtp_instance_get_engine(struct ast_rtp_instance *instance)
01734 {
01735    return instance->engine;
01736 }
01737 
01738 struct ast_rtp_glue *ast_rtp_instance_get_active_glue(struct ast_rtp_instance *instance)
01739 {
01740    return instance->glue;
01741 }
01742 
01743 struct ast_channel *ast_rtp_instance_get_chan(struct ast_rtp_instance *instance)
01744 {
01745    return instance->chan;
01746 }
01747 
01748 int ast_rtp_engine_register_srtp(struct ast_srtp_res *srtp_res, struct ast_srtp_policy_res *policy_res)
01749 {
01750    if (res_srtp || res_srtp_policy) {
01751       return -1;
01752    }
01753    if (!srtp_res || !policy_res) {
01754       return -1;
01755    }
01756 
01757    res_srtp = srtp_res;
01758    res_srtp_policy = policy_res;
01759 
01760    return 0;
01761 }
01762 
01763 void ast_rtp_engine_unregister_srtp(void)
01764 {
01765    res_srtp = NULL;
01766    res_srtp_policy = NULL;
01767 }
01768 
01769 int ast_rtp_engine_srtp_is_registered(void)
01770 {
01771    return res_srtp && res_srtp_policy;
01772 }
01773 
01774 int ast_rtp_instance_add_srtp_policy(struct ast_rtp_instance *instance, struct ast_srtp_policy *policy)
01775 {
01776    if (!res_srtp) {
01777       return -1;
01778    }
01779 
01780    if (!instance->srtp) {
01781       return res_srtp->create(&instance->srtp, instance, policy);
01782    } else {
01783       return res_srtp->add_stream(instance->srtp, policy);
01784    }
01785 }
01786 
01787 struct ast_srtp *ast_rtp_instance_get_srtp(struct ast_rtp_instance *instance)
01788 {
01789    return instance->srtp;
01790 }
01791 
01792 int ast_rtp_instance_sendcng(struct ast_rtp_instance *instance, int level)
01793 {
01794    if (instance->engine->sendcng) {
01795       return instance->engine->sendcng(instance, level);
01796    }
01797 
01798    return -1;
01799 }