Wed Jan 27 20:02:15 2016

Asterisk developer's documentation


rtp_engine.c

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

Generated on 27 Jan 2016 for Asterisk - The Open Source Telephony Project by  doxygen 1.6.1