Wed Apr 6 11:29:47 2011

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

Generated on Wed Apr 6 11:29:47 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7