123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457 |
- /*
- * Asterisk -- An open source telephony toolkit.
- *
- * Copyright (C) 2007 - 2009, Digium, Inc.
- *
- * Joshua Colp <jcolp@digium.com>
- *
- * See http://www.asterisk.org for more information about
- * the Asterisk project. Please do not directly contact
- * any of the maintainers of this project for assistance;
- * the project provides a web site, mailing lists and IRC
- * channels for your use.
- *
- * This program is free software, distributed under the terms of
- * the GNU General Public License Version 2. See the LICENSE file
- * at the top of the source tree.
- */
- /*! \file
- *
- * \brief Bridging Channel API
- *
- * \author Joshua Colp <jcolp@digium.com>
- * \author Richard Mudgett <rmudgett@digium.com>
- * \author Matt Jordan <mjordan@digium.com>
- *
- */
- /*** MODULEINFO
- <support_level>core</support_level>
- ***/
- #include "asterisk.h"
- ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
- #include <signal.h>
- #include <semaphore.h>
- #include "asterisk/heap.h"
- #include "asterisk/astobj2.h"
- #include "asterisk/stringfields.h"
- #include "asterisk/app.h"
- #include "asterisk/pbx.h"
- #include "asterisk/channel.h"
- #include "asterisk/timing.h"
- #include "asterisk/bridge.h"
- #include "asterisk/bridge_channel.h"
- #include "asterisk/bridge_after.h"
- #include "asterisk/bridge_channel_internal.h"
- #include "asterisk/bridge_internal.h"
- #include "asterisk/stasis_bridges.h"
- #include "asterisk/stasis_channels.h"
- #include "asterisk/musiconhold.h"
- #include "asterisk/features_config.h"
- #include "asterisk/parking.h"
- #include "asterisk/causes.h"
- #include "asterisk/test.h"
- /*!
- * \brief Used to queue an action frame onto a bridge channel and write an action frame into a bridge.
- * \since 12.0.0
- *
- * \param bridge_channel Which channel work with.
- * \param action Type of bridge action frame.
- * \param data Frame payload data to pass.
- * \param datalen Frame payload data length to pass.
- *
- * \retval 0 on success.
- * \retval -1 on error.
- */
- typedef int (*ast_bridge_channel_post_action_data)(struct ast_bridge_channel *bridge_channel, enum bridge_channel_action_type action, const void *data, size_t datalen);
- /*!
- * \brief Counter used for assigning synchronous bridge action IDs
- */
- static int sync_ids;
- /*!
- * \brief Frame payload for synchronous bridge actions.
- *
- * The payload serves as a wrapper around the actual payload of the
- * frame, with the addition of an id used to find the associated
- * bridge_sync object.
- */
- struct sync_payload {
- /*! Unique ID for this synchronous action */
- unsigned int id;
- /*! Actual frame data to process */
- unsigned char data[0];
- };
- /*!
- * \brief Synchronous bridge action object.
- *
- * Synchronous bridge actions require the ability for one thread to wait
- * and for another thread to indicate that the action has completed. This
- * structure facilitates that goal by providing synchronization structures.
- */
- struct bridge_sync {
- /*! Unique ID of this synchronization object. Corresponds with ID in synchronous frame payload */
- unsigned int id;
- /*! Semaphore used for synchronization */
- sem_t sem;
- /*! Pointer to next entry in the list */
- AST_LIST_ENTRY(bridge_sync) list;
- };
- /*!
- * \brief List holding active synchronous action objects.
- */
- static AST_RWLIST_HEAD_STATIC(sync_structs, bridge_sync);
- /*!
- * \brief initialize a synchronous bridge object.
- *
- * This both initializes the structure and adds it to the list of
- * synchronization structures.
- *
- * \param sync_struct The synchronization object to initialize.
- * \param id ID to assign to the synchronization object.
- */
- static void bridge_sync_init(struct bridge_sync *sync_struct, unsigned int id)
- {
- memset(sync_struct, 0, sizeof(*sync_struct));
- sync_struct->id = id;
- sem_init(&sync_struct->sem, 0, 0);
- AST_RWLIST_WRLOCK(&sync_structs);
- AST_RWLIST_INSERT_TAIL(&sync_structs, sync_struct, list);
- AST_RWLIST_UNLOCK(&sync_structs);
- }
- /*!
- * \brief Clean up a syncrhonization bridge object.
- *
- * This frees fields within the synchronization object and removes
- * it from the list of active synchronization objects.
- *
- * Since synchronization objects are stack-allocated, it is vital
- * that this is called before the synchronization object goes
- * out of scope.
- *
- * \param sync_struct Synchronization object to clean up.
- */
- static void bridge_sync_cleanup(struct bridge_sync *sync_struct)
- {
- struct bridge_sync *iter;
- AST_RWLIST_WRLOCK(&sync_structs);
- AST_LIST_TRAVERSE_SAFE_BEGIN(&sync_structs, iter, list) {
- if (iter->id == sync_struct->id) {
- AST_LIST_REMOVE_CURRENT(list);
- break;
- }
- }
- AST_LIST_TRAVERSE_SAFE_END;
- AST_RWLIST_UNLOCK(&sync_structs);
- sem_destroy(&sync_struct->sem);
- }
- /*!
- * \brief Failsafe for synchronous bridge action waiting.
- *
- * When waiting for a synchronous bridge action to complete,
- * if there is a frame resource leak somewhere, it is possible
- * that we will never get notified that the synchronous action
- * completed.
- *
- * If a significant amount of time passes, then we will abandon
- * waiting for the synchrnous bridge action to complete.
- *
- * This constant represents the number of milliseconds we will
- * wait for the bridge action to complete.
- */
- #define PLAYBACK_TIMEOUT (600 * 1000)
- /*!
- * \brief Wait for a synchronous bridge action to complete.
- *
- * \param sync_struct Synchronization object corresponding to the bridge action.
- */
- static void bridge_sync_wait(struct bridge_sync *sync_struct)
- {
- struct timeval timeout_val = ast_tvadd(ast_tvnow(), ast_samp2tv(PLAYBACK_TIMEOUT, 1000));
- struct timespec timeout_spec = {
- .tv_sec = timeout_val.tv_sec,
- .tv_nsec = timeout_val.tv_usec * 1000,
- };
- sem_timedwait(&sync_struct->sem, &timeout_spec);
- }
- /*!
- * \brief Signal that waiting for a synchronous bridge action is no longer necessary.
- *
- * This may occur for several reasons
- * \li The synchronous bridge action has completed.
- * \li The bridge channel has been removed from the bridge.
- * \li The synchronous bridge action could not be queued.
- *
- * \param sync_struct Synchronization object corresponding to the bridge action.
- */
- static void bridge_sync_signal(struct bridge_sync *sync_struct)
- {
- sem_post(&sync_struct->sem);
- }
- void ast_bridge_channel_lock_bridge(struct ast_bridge_channel *bridge_channel)
- {
- struct ast_bridge *bridge;
- for (;;) {
- /* Safely get the bridge pointer */
- ast_bridge_channel_lock(bridge_channel);
- bridge = bridge_channel->bridge;
- ao2_ref(bridge, +1);
- ast_bridge_channel_unlock(bridge_channel);
- /* Lock the bridge and see if it is still the bridge we need to lock. */
- ast_bridge_lock(bridge);
- if (bridge == bridge_channel->bridge) {
- ao2_ref(bridge, -1);
- return;
- }
- ast_bridge_unlock(bridge);
- ao2_ref(bridge, -1);
- }
- }
- int ast_bridge_channel_notify_talking(struct ast_bridge_channel *bridge_channel, int started_talking)
- {
- struct ast_frame action = {
- .frametype = AST_FRAME_BRIDGE_ACTION,
- .subclass.integer = started_talking
- ? BRIDGE_CHANNEL_ACTION_TALKING_START : BRIDGE_CHANNEL_ACTION_TALKING_STOP,
- };
- return ast_bridge_channel_queue_frame(bridge_channel, &action);
- }
- /*!
- * \internal
- * \brief Poke the bridge_channel thread
- */
- static void bridge_channel_poke(struct ast_bridge_channel *bridge_channel)
- {
- if (!pthread_equal(pthread_self(), bridge_channel->thread)) {
- /* Wake up the bridge channel thread. */
- ast_queue_frame(bridge_channel->chan, &ast_null_frame);
- }
- }
- /*!
- * \internal
- * \brief Set actual cause on channel.
- * \since 12.0.0
- *
- * \param chan Channel to set cause.
- * \param cause Cause to set on channel.
- * If cause <= 0 then use cause on channel if cause still <= 0 use AST_CAUSE_NORMAL_CLEARING.
- *
- * \return Actual cause set on channel.
- */
- static int channel_set_cause(struct ast_channel *chan, int cause)
- {
- ast_channel_lock(chan);
- if (cause <= 0) {
- cause = ast_channel_hangupcause(chan);
- if (cause <= 0) {
- cause = AST_CAUSE_NORMAL_CLEARING;
- }
- }
- ast_channel_hangupcause_set(chan, cause);
- ast_channel_unlock(chan);
- return cause;
- }
- void ast_bridge_channel_leave_bridge_nolock(struct ast_bridge_channel *bridge_channel, enum bridge_channel_state new_state, int cause)
- {
- if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
- return;
- }
- ast_debug(1, "Setting %p(%s) state from:%d to:%d\n",
- bridge_channel, ast_channel_name(bridge_channel->chan), bridge_channel->state,
- new_state);
- channel_set_cause(bridge_channel->chan, cause);
- /* Change the state on the bridge channel */
- bridge_channel->state = new_state;
- bridge_channel_poke(bridge_channel);
- }
- void ast_bridge_channel_leave_bridge(struct ast_bridge_channel *bridge_channel, enum bridge_channel_state new_state, int cause)
- {
- ast_bridge_channel_lock(bridge_channel);
- ast_bridge_channel_leave_bridge_nolock(bridge_channel, new_state, cause);
- ast_bridge_channel_unlock(bridge_channel);
- }
- struct ast_bridge_channel *ast_bridge_channel_peer(struct ast_bridge_channel *bridge_channel)
- {
- struct ast_bridge *bridge = bridge_channel->bridge;
- struct ast_bridge_channel *other = NULL;
- if (bridge_channel->in_bridge && bridge->num_channels == 2) {
- AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
- if (other != bridge_channel) {
- break;
- }
- }
- }
- return other;
- }
- void ast_bridge_channel_restore_formats(struct ast_bridge_channel *bridge_channel)
- {
- /* Restore original formats of the channel as they came in */
- if (ast_format_cmp(ast_channel_readformat(bridge_channel->chan), &bridge_channel->read_format) == AST_FORMAT_CMP_NOT_EQUAL) {
- ast_debug(1, "Bridge is returning %p(%s) to read format %s\n",
- bridge_channel, ast_channel_name(bridge_channel->chan),
- ast_getformatname(&bridge_channel->read_format));
- if (ast_set_read_format(bridge_channel->chan, &bridge_channel->read_format)) {
- ast_debug(1, "Bridge failed to return %p(%s) to read format %s\n",
- bridge_channel, ast_channel_name(bridge_channel->chan),
- ast_getformatname(&bridge_channel->read_format));
- }
- }
- if (ast_format_cmp(ast_channel_writeformat(bridge_channel->chan), &bridge_channel->write_format) == AST_FORMAT_CMP_NOT_EQUAL) {
- ast_debug(1, "Bridge is returning %p(%s) to write format %s\n",
- bridge_channel, ast_channel_name(bridge_channel->chan),
- ast_getformatname(&bridge_channel->write_format));
- if (ast_set_write_format(bridge_channel->chan, &bridge_channel->write_format)) {
- ast_debug(1, "Bridge failed to return %p(%s) to write format %s\n",
- bridge_channel, ast_channel_name(bridge_channel->chan),
- ast_getformatname(&bridge_channel->write_format));
- }
- }
- }
- struct ast_bridge *ast_bridge_channel_merge_inhibit(struct ast_bridge_channel *bridge_channel, int request)
- {
- struct ast_bridge *bridge;
- ast_bridge_channel_lock_bridge(bridge_channel);
- bridge = bridge_channel->bridge;
- ao2_ref(bridge, +1);
- bridge_merge_inhibit_nolock(bridge, request);
- ast_bridge_unlock(bridge);
- return bridge;
- }
- void ast_bridge_channel_update_linkedids(struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
- {
- struct ast_bridge_channel *other = NULL;
- struct ast_bridge *bridge = bridge_channel->bridge;
- struct ast_channel *oldest_linkedid_chan = bridge_channel->chan;
- AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
- if (other == swap) {
- continue;
- }
- oldest_linkedid_chan = ast_channel_internal_oldest_linkedid(
- oldest_linkedid_chan, other->chan);
- }
- ast_channel_lock(bridge_channel->chan);
- ast_channel_internal_copy_linkedid(bridge_channel->chan,
- oldest_linkedid_chan);
- ast_channel_unlock(bridge_channel->chan);
- AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
- if (other == swap) {
- continue;
- }
- ast_channel_lock(other->chan);
- ast_channel_internal_copy_linkedid(other->chan,
- oldest_linkedid_chan);
- ast_channel_unlock(other->chan);
- }
- }
- void ast_bridge_channel_update_accountcodes(struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
- {
- struct ast_bridge *bridge = bridge_channel->bridge;
- struct ast_bridge_channel *other = NULL;
- AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
- if (other == swap) {
- continue;
- }
- ast_channel_lock_both(bridge_channel->chan, other->chan);
- if (!ast_strlen_zero(ast_channel_accountcode(bridge_channel->chan)) && ast_strlen_zero(ast_channel_peeraccount(other->chan))) {
- ast_debug(1, "Setting peeraccount to %s for %s from data on channel %s\n",
- ast_channel_accountcode(bridge_channel->chan), ast_channel_name(other->chan), ast_channel_name(bridge_channel->chan));
- ast_channel_peeraccount_set(other->chan, ast_channel_accountcode(bridge_channel->chan));
- }
- if (!ast_strlen_zero(ast_channel_accountcode(other->chan)) && ast_strlen_zero(ast_channel_peeraccount(bridge_channel->chan))) {
- ast_debug(1, "Setting peeraccount to %s for %s from data on channel %s\n",
- ast_channel_accountcode(other->chan), ast_channel_name(bridge_channel->chan), ast_channel_name(other->chan));
- ast_channel_peeraccount_set(bridge_channel->chan, ast_channel_accountcode(other->chan));
- }
- if (!ast_strlen_zero(ast_channel_peeraccount(bridge_channel->chan)) && ast_strlen_zero(ast_channel_accountcode(other->chan))) {
- ast_debug(1, "Setting accountcode to %s for %s from data on channel %s\n",
- ast_channel_peeraccount(bridge_channel->chan), ast_channel_name(other->chan), ast_channel_name(bridge_channel->chan));
- ast_channel_accountcode_set(other->chan, ast_channel_peeraccount(bridge_channel->chan));
- }
- if (!ast_strlen_zero(ast_channel_peeraccount(other->chan)) && ast_strlen_zero(ast_channel_accountcode(bridge_channel->chan))) {
- ast_debug(1, "Setting accountcode to %s for %s from data on channel %s\n",
- ast_channel_peeraccount(other->chan), ast_channel_name(bridge_channel->chan), ast_channel_name(other->chan));
- ast_channel_accountcode_set(bridge_channel->chan, ast_channel_peeraccount(other->chan));
- }
- if (bridge->num_channels == 2) {
- if (strcmp(ast_channel_accountcode(bridge_channel->chan), ast_channel_peeraccount(other->chan))) {
- ast_debug(1, "Changing peeraccount from %s to %s on %s to match channel %s\n",
- ast_channel_peeraccount(other->chan), ast_channel_peeraccount(bridge_channel->chan), ast_channel_name(other->chan), ast_channel_name(bridge_channel->chan));
- ast_channel_peeraccount_set(other->chan, ast_channel_accountcode(bridge_channel->chan));
- }
- if (strcmp(ast_channel_accountcode(other->chan), ast_channel_peeraccount(bridge_channel->chan))) {
- ast_debug(1, "Changing peeraccount from %s to %s on %s to match channel %s\n",
- ast_channel_peeraccount(bridge_channel->chan), ast_channel_peeraccount(other->chan), ast_channel_name(bridge_channel->chan), ast_channel_name(other->chan));
- ast_channel_peeraccount_set(bridge_channel->chan, ast_channel_accountcode(other->chan));
- }
- }
- ast_channel_unlock(bridge_channel->chan);
- ast_channel_unlock(other->chan);
- }
- }
- void ast_bridge_channel_kick(struct ast_bridge_channel *bridge_channel, int cause)
- {
- struct ast_bridge_features *features = bridge_channel->features;
- struct ast_bridge_hook *hook;
- struct ao2_iterator iter;
- ast_bridge_channel_lock(bridge_channel);
- if (bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
- channel_set_cause(bridge_channel->chan, cause);
- cause = 0;
- }
- ast_bridge_channel_unlock(bridge_channel);
- /* Run any hangup hooks. */
- iter = ao2_iterator_init(features->other_hooks, 0);
- for (; (hook = ao2_iterator_next(&iter)); ao2_ref(hook, -1)) {
- int remove_me;
- if (hook->type != AST_BRIDGE_HOOK_TYPE_HANGUP) {
- continue;
- }
- remove_me = hook->callback(bridge_channel, hook->hook_pvt);
- if (remove_me) {
- ast_debug(1, "Hangup hook %p is being removed from %p(%s)\n",
- hook, bridge_channel, ast_channel_name(bridge_channel->chan));
- ao2_unlink(features->other_hooks, hook);
- }
- }
- ao2_iterator_destroy(&iter);
- /* Default hangup action. */
- ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END, cause);
- }
- /*!
- * \internal
- * \brief Write an \ref ast_frame onto the bridge channel
- * \since 12.0.0
- *
- * \param bridge_channel Which channel to queue the frame onto.
- * \param frame The frame to write onto the bridge_channel
- *
- * \retval 0 on success.
- * \retval -1 on error.
- */
- static int bridge_channel_write_frame(struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
- {
- ast_assert(frame->frametype != AST_FRAME_BRIDGE_ACTION_SYNC);
- ast_bridge_channel_lock_bridge(bridge_channel);
- /*
- * XXX need to implement a deferred write queue for when there
- * is no peer channel in the bridge (yet or it was kicked).
- *
- * The tech decides if a frame needs to be pushed back for deferral.
- * simple_bridge/native_bridge are likely the only techs that will do this.
- */
- bridge_channel->bridge->technology->write(bridge_channel->bridge, bridge_channel, frame);
- /* Remember any owed events to the bridge. */
- switch (frame->frametype) {
- case AST_FRAME_DTMF_BEGIN:
- bridge_channel->owed.dtmf_tv = ast_tvnow();
- bridge_channel->owed.dtmf_digit = frame->subclass.integer;
- break;
- case AST_FRAME_DTMF_END:
- bridge_channel->owed.dtmf_digit = '\0';
- break;
- case AST_FRAME_CONTROL:
- /*
- * We explicitly will not remember HOLD/UNHOLD frames because
- * things like attended transfers will handle them.
- */
- default:
- break;
- }
- ast_bridge_unlock(bridge_channel->bridge);
- /*
- * Claim successful write to bridge. If deferred frame
- * support is added, claim successfully deferred.
- */
- return 0;
- }
- void bridge_channel_settle_owed_events(struct ast_bridge *orig_bridge, struct ast_bridge_channel *bridge_channel)
- {
- if (bridge_channel->owed.dtmf_digit) {
- struct ast_frame frame = {
- .frametype = AST_FRAME_DTMF_END,
- .subclass.integer = bridge_channel->owed.dtmf_digit,
- .src = "Bridge channel owed DTMF",
- };
- frame.len = ast_tvdiff_ms(ast_tvnow(), bridge_channel->owed.dtmf_tv);
- if (frame.len < option_dtmfminduration) {
- frame.len = option_dtmfminduration;
- }
- ast_log(LOG_DTMF, "DTMF end '%c' simulated to bridge %s because %s left. Duration %ld ms.\n",
- bridge_channel->owed.dtmf_digit, orig_bridge->uniqueid,
- ast_channel_name(bridge_channel->chan), frame.len);
- bridge_channel->owed.dtmf_digit = '\0';
- orig_bridge->technology->write(orig_bridge, NULL, &frame);
- }
- }
- /*!
- * \internal
- * \brief Suspend a channel from a bridge.
- *
- * \param bridge_channel Channel to suspend.
- *
- * \note This function assumes bridge_channel->bridge is locked.
- *
- * \return Nothing
- */
- void bridge_channel_internal_suspend_nolock(struct ast_bridge_channel *bridge_channel)
- {
- bridge_channel->suspended = 1;
- if (bridge_channel->in_bridge) {
- --bridge_channel->bridge->num_active;
- }
- /* Get technology bridge threads off of the channel. */
- if (bridge_channel->bridge->technology->suspend) {
- bridge_channel->bridge->technology->suspend(bridge_channel->bridge, bridge_channel);
- }
- }
- /*!
- * \internal
- * \brief Suspend a channel from a bridge.
- *
- * \param bridge_channel Channel to suspend.
- *
- * \return Nothing
- */
- static void bridge_channel_suspend(struct ast_bridge_channel *bridge_channel)
- {
- ast_bridge_channel_lock_bridge(bridge_channel);
- bridge_channel_internal_suspend_nolock(bridge_channel);
- ast_bridge_unlock(bridge_channel->bridge);
- }
- /*!
- * \internal
- * \brief Unsuspend a channel from a bridge.
- *
- * \param bridge_channel Channel to unsuspend.
- *
- * \note This function assumes bridge_channel->bridge is locked.
- *
- * \return Nothing
- */
- void bridge_channel_internal_unsuspend_nolock(struct ast_bridge_channel *bridge_channel)
- {
- bridge_channel->suspended = 0;
- if (bridge_channel->in_bridge) {
- ++bridge_channel->bridge->num_active;
- }
- /* Wake technology bridge threads to take care of channel again. */
- if (bridge_channel->bridge->technology->unsuspend) {
- bridge_channel->bridge->technology->unsuspend(bridge_channel->bridge, bridge_channel);
- }
- /* Wake suspended channel. */
- ast_bridge_channel_lock(bridge_channel);
- ast_cond_signal(&bridge_channel->cond);
- ast_bridge_channel_unlock(bridge_channel);
- }
- /*!
- * \internal
- * \brief Unsuspend a channel from a bridge.
- *
- * \param bridge_channel Channel to unsuspend.
- *
- * \return Nothing
- */
- static void bridge_channel_unsuspend(struct ast_bridge_channel *bridge_channel)
- {
- ast_bridge_channel_lock_bridge(bridge_channel);
- bridge_channel_internal_unsuspend_nolock(bridge_channel);
- ast_bridge_unlock(bridge_channel->bridge);
- }
- /*!
- * \internal
- * \brief Queue an action frame onto the bridge channel with data.
- * \since 12.0.0
- *
- * \param bridge_channel Which channel to queue the frame onto.
- * \param action Type of bridge action frame.
- * \param data Frame payload data to pass.
- * \param datalen Frame payload data length to pass.
- *
- * \retval 0 on success.
- * \retval -1 on error.
- */
- static int bridge_channel_queue_action_data(struct ast_bridge_channel *bridge_channel,
- enum bridge_channel_action_type action, const void *data, size_t datalen)
- {
- struct ast_frame frame = {
- .frametype = AST_FRAME_BRIDGE_ACTION,
- .subclass.integer = action,
- .datalen = datalen,
- .data.ptr = (void *) data,
- };
- return ast_bridge_channel_queue_frame(bridge_channel, &frame);
- }
- /*!
- * \internal
- * \brief Queue an action frame onto the bridge channel with data synchronously.
- * \since 12.2.0
- *
- * The function will not return until the queued frame is freed.
- *
- * \param bridge_channel Which channel to queue the frame onto.
- * \param action Type of bridge action frame.
- * \param data Frame payload data to pass.
- * \param datalen Frame payload data length to pass.
- *
- * \retval 0 on success.
- * \retval -1 on error.
- */
- static int bridge_channel_queue_action_data_sync(struct ast_bridge_channel *bridge_channel,
- enum bridge_channel_action_type action, const void *data, size_t datalen)
- {
- struct sync_payload *sync_payload;
- int sync_payload_len = sizeof(*sync_payload) + datalen;
- struct bridge_sync sync_struct;
- struct ast_frame frame = {
- .frametype = AST_FRAME_BRIDGE_ACTION_SYNC,
- .subclass.integer = action,
- };
- /* Make sure we don't end up trying to wait on ourself to deliver the frame */
- ast_assert(!pthread_equal(pthread_self(), bridge_channel->thread));
- sync_payload = ast_alloca(sync_payload_len);
- sync_payload->id = ast_atomic_fetchadd_int(&sync_ids, +1);
- memcpy(sync_payload->data, data, datalen);
- frame.datalen = sync_payload_len;
- frame.data.ptr = sync_payload;
- bridge_sync_init(&sync_struct, sync_payload->id);
- if (ast_bridge_channel_queue_frame(bridge_channel, &frame)) {
- bridge_sync_cleanup(&sync_struct);
- return -1;
- }
- bridge_sync_wait(&sync_struct);
- bridge_sync_cleanup(&sync_struct);
- return 0;
- }
- /*!
- * \internal
- * \brief Write an action frame onto the bridge channel with data.
- * \since 12.0.0
- *
- * \param bridge_channel Which channel to queue the frame onto.
- * \param action Type of bridge action frame.
- * \param data Frame payload data to pass.
- * \param datalen Frame payload data length to pass.
- *
- * \retval 0 on success.
- * \retval -1 on error.
- */
- static int bridge_channel_write_action_data(struct ast_bridge_channel *bridge_channel,
- enum bridge_channel_action_type action, const void *data, size_t datalen)
- {
- struct ast_frame frame = {
- .frametype = AST_FRAME_BRIDGE_ACTION,
- .subclass.integer = action,
- .datalen = datalen,
- .data.ptr = (void *) data,
- };
- return bridge_channel_write_frame(bridge_channel, &frame);
- }
- static void bridge_frame_free(struct ast_frame *frame)
- {
- if (frame->frametype == AST_FRAME_BRIDGE_ACTION_SYNC) {
- struct sync_payload *sync_payload = frame->data.ptr;
- struct bridge_sync *sync;
- AST_RWLIST_RDLOCK(&sync_structs);
- AST_RWLIST_TRAVERSE(&sync_structs, sync, list) {
- if (sync->id == sync_payload->id) {
- break;
- }
- }
- if (sync) {
- bridge_sync_signal(sync);
- }
- AST_RWLIST_UNLOCK(&sync_structs);
- }
- ast_frfree(frame);
- }
- int ast_bridge_channel_queue_frame(struct ast_bridge_channel *bridge_channel, struct ast_frame *fr)
- {
- struct ast_frame *dup;
- char nudge = 0;
- if (bridge_channel->suspended
- /* Also defer DTMF frames. */
- && fr->frametype != AST_FRAME_DTMF_BEGIN
- && fr->frametype != AST_FRAME_DTMF_END
- && !ast_is_deferrable_frame(fr)) {
- /* Drop non-deferable frames when suspended. */
- return 0;
- }
- if (fr->frametype == AST_FRAME_NULL) {
- /* "Accept" the frame and discard it. */
- return 0;
- }
- dup = ast_frdup(fr);
- if (!dup) {
- return -1;
- }
- ast_bridge_channel_lock(bridge_channel);
- if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
- /* Drop frames on channels leaving the bridge. */
- ast_bridge_channel_unlock(bridge_channel);
- bridge_frame_free(dup);
- return 0;
- }
- AST_LIST_INSERT_TAIL(&bridge_channel->wr_queue, dup, frame_list);
- if (write(bridge_channel->alert_pipe[1], &nudge, sizeof(nudge)) != sizeof(nudge)) {
- ast_log(LOG_ERROR, "We couldn't write alert pipe for %p(%s)... something is VERY wrong\n",
- bridge_channel, ast_channel_name(bridge_channel->chan));
- }
- ast_bridge_channel_unlock(bridge_channel);
- return 0;
- }
- int ast_bridge_queue_everyone_else(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
- {
- struct ast_bridge_channel *cur;
- int not_written = -1;
- if (frame->frametype == AST_FRAME_NULL) {
- /* "Accept" the frame and discard it. */
- return 0;
- }
- AST_LIST_TRAVERSE(&bridge->channels, cur, entry) {
- if (cur == bridge_channel) {
- continue;
- }
- if (!ast_bridge_channel_queue_frame(cur, frame)) {
- not_written = 0;
- }
- }
- return not_written;
- }
- int ast_bridge_channel_queue_control_data(struct ast_bridge_channel *bridge_channel, enum ast_control_frame_type control, const void *data, size_t datalen)
- {
- struct ast_frame frame = {
- .frametype = AST_FRAME_CONTROL,
- .subclass.integer = control,
- .datalen = datalen,
- .data.ptr = (void *) data,
- };
- return ast_bridge_channel_queue_frame(bridge_channel, &frame);
- }
- int ast_bridge_channel_write_control_data(struct ast_bridge_channel *bridge_channel, enum ast_control_frame_type control, const void *data, size_t datalen)
- {
- struct ast_frame frame = {
- .frametype = AST_FRAME_CONTROL,
- .subclass.integer = control,
- .datalen = datalen,
- .data.ptr = (void *) data,
- };
- return bridge_channel_write_frame(bridge_channel, &frame);
- }
- int ast_bridge_channel_write_hold(struct ast_bridge_channel *bridge_channel, const char *moh_class)
- {
- RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
- size_t datalen;
- if (!ast_strlen_zero(moh_class)) {
- datalen = strlen(moh_class) + 1;
- blob = ast_json_pack("{s: s}",
- "musicclass", moh_class);
- } else {
- moh_class = NULL;
- datalen = 0;
- }
- ast_channel_lock(bridge_channel->chan);
- ast_channel_publish_blob(bridge_channel->chan, ast_channel_hold_type(), blob);
- ast_channel_unlock(bridge_channel->chan);
- return ast_bridge_channel_write_control_data(bridge_channel, AST_CONTROL_HOLD,
- moh_class, datalen);
- }
- int ast_bridge_channel_write_unhold(struct ast_bridge_channel *bridge_channel)
- {
- ast_channel_lock(bridge_channel->chan);
- ast_channel_publish_blob(bridge_channel->chan, ast_channel_unhold_type(), NULL);
- ast_channel_unlock(bridge_channel->chan);
- return ast_bridge_channel_write_control_data(bridge_channel, AST_CONTROL_UNHOLD, NULL, 0);
- }
- /*!
- * \internal
- * \brief Helper function to kick off a PBX app on a bridge_channel
- */
- static int run_app_helper(struct ast_channel *chan, const char *app_name, const char *app_args)
- {
- int res = 0;
- if (!strcasecmp("Gosub", app_name)) {
- ast_app_exec_sub(NULL, chan, app_args, 0);
- } else if (!strcasecmp("Macro", app_name)) {
- ast_app_exec_macro(NULL, chan, app_args);
- } else {
- struct ast_app *app;
- app = pbx_findapp(app_name);
- if (!app) {
- ast_log(LOG_WARNING, "Could not find application (%s)\n", app_name);
- } else {
- res = pbx_exec(chan, app, app_args);
- }
- }
- return res;
- }
- void ast_bridge_channel_run_app(struct ast_bridge_channel *bridge_channel, const char *app_name, const char *app_args, const char *moh_class)
- {
- if (moh_class) {
- ast_bridge_channel_write_hold(bridge_channel, moh_class);
- }
- if (run_app_helper(bridge_channel->chan, app_name, S_OR(app_args, ""))) {
- /* Break the bridge if the app returns non-zero. */
- ast_bridge_channel_kick(bridge_channel, AST_CAUSE_NORMAL_CLEARING);
- }
- if (moh_class) {
- ast_bridge_channel_write_unhold(bridge_channel);
- }
- }
- struct bridge_run_app {
- /*! Offset into app_name[] where the MOH class name starts. (zero if no MOH) */
- int moh_offset;
- /*! Offset into app_name[] where the application argument string starts. (zero if no arguments) */
- int app_args_offset;
- /*! Application name to run. */
- char app_name[0];
- };
- /*!
- * \internal
- * \brief Handle the run application bridge action.
- * \since 12.0.0
- *
- * \param bridge_channel Which channel to run the application on.
- * \param data Action frame data to run the application.
- *
- * \return Nothing
- */
- static void bridge_channel_run_app(struct ast_bridge_channel *bridge_channel, struct bridge_run_app *data)
- {
- ast_bridge_channel_run_app(bridge_channel, data->app_name,
- data->app_args_offset ? &data->app_name[data->app_args_offset] : NULL,
- data->moh_offset ? &data->app_name[data->moh_offset] : NULL);
- }
- /*!
- * \internal
- * \brief Marshal an application to be executed on a bridge_channel
- */
- static int payload_helper_app(ast_bridge_channel_post_action_data post_it,
- struct ast_bridge_channel *bridge_channel, const char *app_name, const char *app_args, const char *moh_class)
- {
- struct bridge_run_app *app_data;
- size_t len_name = strlen(app_name) + 1;
- size_t len_args = ast_strlen_zero(app_args) ? 0 : strlen(app_args) + 1;
- size_t len_moh = !moh_class ? 0 : strlen(moh_class) + 1;
- size_t len_data = sizeof(*app_data) + len_name + len_args + len_moh;
- /* Fill in application run frame data. */
- app_data = alloca(len_data);
- app_data->app_args_offset = len_args ? len_name : 0;
- app_data->moh_offset = len_moh ? len_name + len_args : 0;
- strcpy(app_data->app_name, app_name);/* Safe */
- if (len_args) {
- strcpy(&app_data->app_name[app_data->app_args_offset], app_args);/* Safe */
- }
- if (moh_class) {
- strcpy(&app_data->app_name[app_data->moh_offset], moh_class);/* Safe */
- }
- return post_it(bridge_channel, BRIDGE_CHANNEL_ACTION_RUN_APP, app_data, len_data);
- }
- int ast_bridge_channel_write_app(struct ast_bridge_channel *bridge_channel, const char *app_name, const char *app_args, const char *moh_class)
- {
- return payload_helper_app(bridge_channel_write_action_data,
- bridge_channel, app_name, app_args, moh_class);
- }
- int ast_bridge_channel_queue_app(struct ast_bridge_channel *bridge_channel, const char *app_name, const char *app_args, const char *moh_class)
- {
- return payload_helper_app(bridge_channel_queue_action_data,
- bridge_channel, app_name, app_args, moh_class);
- }
- void ast_bridge_channel_playfile(struct ast_bridge_channel *bridge_channel, ast_bridge_custom_play_fn custom_play, const char *playfile, const char *moh_class)
- {
- if (moh_class) {
- ast_bridge_channel_write_hold(bridge_channel, moh_class);
- }
- if (custom_play) {
- custom_play(bridge_channel, playfile);
- } else {
- ast_stream_and_wait(bridge_channel->chan, playfile, AST_DIGIT_NONE);
- }
- if (moh_class) {
- ast_bridge_channel_write_unhold(bridge_channel);
- }
- /*
- * It may be necessary to resume music on hold after we finish
- * playing the announcment.
- */
- if (ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_MOH)) {
- const char *latest_musicclass;
- ast_channel_lock(bridge_channel->chan);
- latest_musicclass = ast_strdupa(ast_channel_latest_musicclass(bridge_channel->chan));
- ast_channel_unlock(bridge_channel->chan);
- ast_moh_start(bridge_channel->chan, latest_musicclass, NULL);
- }
- }
- struct bridge_playfile {
- /*! Call this function to play the playfile. (NULL if normal sound file to play) */
- ast_bridge_custom_play_fn custom_play;
- /*! Offset into playfile[] where the MOH class name starts. (zero if no MOH)*/
- int moh_offset;
- /*! Filename to play. */
- char playfile[0];
- };
- /*!
- * \internal
- * \brief Handle the playfile bridge action.
- * \since 12.0.0
- *
- * \param bridge_channel Which channel to play a file on.
- * \param payload Action frame payload to play a file.
- *
- * \return Nothing
- */
- static void bridge_channel_playfile(struct ast_bridge_channel *bridge_channel, struct bridge_playfile *payload)
- {
- ast_bridge_channel_playfile(bridge_channel, payload->custom_play, payload->playfile,
- payload->moh_offset ? &payload->playfile[payload->moh_offset] : NULL);
- }
- /*!
- * \internal
- * \brief Marshal a file to be played on a bridge_channel
- */
- static int payload_helper_playfile(ast_bridge_channel_post_action_data post_it,
- struct ast_bridge_channel *bridge_channel, ast_bridge_custom_play_fn custom_play, const char *playfile, const char *moh_class)
- {
- struct bridge_playfile *payload;
- size_t len_name = strlen(playfile) + 1;
- size_t len_moh = !moh_class ? 0 : strlen(moh_class) + 1;
- size_t len_payload = sizeof(*payload) + len_name + len_moh;
- /* Fill in play file frame data. */
- payload = ast_alloca(len_payload);
- payload->custom_play = custom_play;
- payload->moh_offset = len_moh ? len_name : 0;
- strcpy(payload->playfile, playfile);/* Safe */
- if (moh_class) {
- strcpy(&payload->playfile[payload->moh_offset], moh_class);/* Safe */
- }
- return post_it(bridge_channel, BRIDGE_CHANNEL_ACTION_PLAY_FILE, payload, len_payload);
- }
- int ast_bridge_channel_write_playfile(struct ast_bridge_channel *bridge_channel, ast_bridge_custom_play_fn custom_play, const char *playfile, const char *moh_class)
- {
- return payload_helper_playfile(bridge_channel_write_action_data,
- bridge_channel, custom_play, playfile, moh_class);
- }
- int ast_bridge_channel_queue_playfile(struct ast_bridge_channel *bridge_channel, ast_bridge_custom_play_fn custom_play, const char *playfile, const char *moh_class)
- {
- return payload_helper_playfile(bridge_channel_queue_action_data,
- bridge_channel, custom_play, playfile, moh_class);
- }
- int ast_bridge_channel_queue_playfile_sync(struct ast_bridge_channel *bridge_channel,
- ast_bridge_custom_play_fn custom_play, const char *playfile, const char *moh_class)
- {
- return payload_helper_playfile(bridge_channel_queue_action_data_sync,
- bridge_channel, custom_play, playfile, moh_class);
- }
- struct bridge_custom_callback {
- /*! Call this function on the bridge channel thread. */
- ast_bridge_custom_callback_fn callback;
- /*! Size of the payload if it exists. A number otherwise. */
- size_t payload_size;
- /*! Option flags determining how callback is called. */
- unsigned int flags;
- /*! Nonzero if the payload exists. */
- char payload_exists;
- /*! Payload to give to callback. */
- char payload[0];
- };
- /*!
- * \internal
- * \brief Handle the do custom callback bridge action.
- * \since 12.0.0
- *
- * \param bridge_channel Which channel to call the callback on.
- * \param data Action frame data to call the callback.
- *
- * \return Nothing
- */
- static void bridge_channel_do_callback(struct ast_bridge_channel *bridge_channel, struct bridge_custom_callback *data)
- {
- if (ast_test_flag(data, AST_BRIDGE_CHANNEL_CB_OPTION_MEDIA)) {
- bridge_channel_suspend(bridge_channel);
- ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
- }
- data->callback(bridge_channel, data->payload_exists ? data->payload : NULL, data->payload_size);
- if (ast_test_flag(data, AST_BRIDGE_CHANNEL_CB_OPTION_MEDIA)) {
- ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
- bridge_channel_unsuspend(bridge_channel);
- }
- }
- /*!
- * \internal
- * \brief Marshal a custom callback function to be called on a bridge_channel
- */
- static int payload_helper_cb(ast_bridge_channel_post_action_data post_it,
- struct ast_bridge_channel *bridge_channel,
- enum ast_bridge_channel_custom_callback_option flags,
- ast_bridge_custom_callback_fn callback, const void *payload, size_t payload_size)
- {
- struct bridge_custom_callback *cb_data;
- size_t len_data = sizeof(*cb_data) + (payload ? payload_size : 0);
- /* Sanity check. */
- if (!callback) {
- ast_assert(0);
- return -1;
- }
- /* Fill in custom callback frame data. */
- cb_data = alloca(len_data);
- cb_data->callback = callback;
- cb_data->payload_size = payload_size;
- cb_data->flags = flags;
- cb_data->payload_exists = payload && payload_size;
- if (cb_data->payload_exists) {
- memcpy(cb_data->payload, payload, payload_size);/* Safe */
- }
- return post_it(bridge_channel, BRIDGE_CHANNEL_ACTION_CALLBACK, cb_data, len_data);
- }
- int ast_bridge_channel_write_callback(struct ast_bridge_channel *bridge_channel,
- enum ast_bridge_channel_custom_callback_option flags,
- ast_bridge_custom_callback_fn callback, const void *payload, size_t payload_size)
- {
- return payload_helper_cb(bridge_channel_write_action_data,
- bridge_channel, flags, callback, payload, payload_size);
- }
- int ast_bridge_channel_queue_callback(struct ast_bridge_channel *bridge_channel,
- enum ast_bridge_channel_custom_callback_option flags,
- ast_bridge_custom_callback_fn callback, const void *payload, size_t payload_size)
- {
- return payload_helper_cb(bridge_channel_queue_action_data,
- bridge_channel, flags, callback, payload, payload_size);
- }
- struct bridge_park {
- int parker_uuid_offset;
- int app_data_offset;
- /* buffer used for holding those strings */
- char parkee_uuid[0];
- };
- /*!
- * \internal
- * \brief Park a bridge_cahnnel
- */
- static void bridge_channel_park(struct ast_bridge_channel *bridge_channel, struct bridge_park *payload)
- {
- if (!ast_parking_provider_registered()) {
- ast_log(AST_LOG_WARNING, "Unable to park %s: No parking provider loaded!\n",
- ast_channel_name(bridge_channel->chan));
- return;
- }
- if (ast_parking_park_bridge_channel(bridge_channel, payload->parkee_uuid,
- &payload->parkee_uuid[payload->parker_uuid_offset],
- payload->app_data_offset ? &payload->parkee_uuid[payload->app_data_offset] : NULL)) {
- ast_log(AST_LOG_WARNING, "Error occurred while parking %s\n",
- ast_channel_name(bridge_channel->chan));
- }
- }
- /*!
- * \internal
- * \brief Marshal a park action onto a bridge_channel
- */
- static int payload_helper_park(ast_bridge_channel_post_action_data post_it,
- struct ast_bridge_channel *bridge_channel,
- const char *parkee_uuid,
- const char *parker_uuid,
- const char *app_data)
- {
- struct bridge_park *payload;
- size_t len_parkee_uuid = strlen(parkee_uuid) + 1;
- size_t len_parker_uuid = strlen(parker_uuid) + 1;
- size_t len_app_data = !app_data ? 0 : strlen(app_data) + 1;
- size_t len_payload = sizeof(*payload) + len_parker_uuid + len_parkee_uuid + len_app_data;
- payload = alloca(len_payload);
- payload->app_data_offset = len_app_data ? len_parkee_uuid + len_parker_uuid : 0;
- payload->parker_uuid_offset = len_parkee_uuid;
- strcpy(payload->parkee_uuid, parkee_uuid);
- strcpy(&payload->parkee_uuid[payload->parker_uuid_offset], parker_uuid);
- if (app_data) {
- strcpy(&payload->parkee_uuid[payload->app_data_offset], app_data);
- }
- return post_it(bridge_channel, BRIDGE_CHANNEL_ACTION_PARK, payload, len_payload);
- }
- int ast_bridge_channel_write_park(struct ast_bridge_channel *bridge_channel, const char *parkee_uuid, const char *parker_uuid, const char *app_data)
- {
- return payload_helper_park(bridge_channel_write_action_data,
- bridge_channel, parkee_uuid, parker_uuid, app_data);
- }
- /*!
- * \internal
- * \brief Handle bridge channel interval expiration.
- * \since 12.0.0
- *
- * \param bridge_channel Channel to run expired intervals on.
- *
- * \return Nothing
- */
- static void bridge_channel_handle_interval(struct ast_bridge_channel *bridge_channel)
- {
- struct ast_heap *interval_hooks;
- struct ast_bridge_hook_timer *hook;
- struct timeval start;
- int chan_suspended = 0;
- interval_hooks = bridge_channel->features->interval_hooks;
- ast_heap_wrlock(interval_hooks);
- start = ast_tvnow();
- while ((hook = ast_heap_peek(interval_hooks, 1))) {
- int interval;
- unsigned int execution_time;
- if (ast_tvdiff_ms(hook->timer.trip_time, start) > 0) {
- ast_debug(1, "Hook %p on %p(%s) wants to happen in the future, stopping our traversal\n",
- hook, bridge_channel, ast_channel_name(bridge_channel->chan));
- break;
- }
- ao2_ref(hook, +1);
- ast_heap_unlock(interval_hooks);
- if (!chan_suspended
- && ast_test_flag(&hook->timer, AST_BRIDGE_HOOK_TIMER_OPTION_MEDIA)) {
- chan_suspended = 1;
- bridge_channel_suspend(bridge_channel);
- ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
- }
- ast_debug(1, "Executing hook %p on %p(%s)\n",
- hook, bridge_channel, ast_channel_name(bridge_channel->chan));
- interval = hook->generic.callback(bridge_channel, hook->generic.hook_pvt);
- ast_heap_wrlock(interval_hooks);
- if (ast_heap_peek(interval_hooks, hook->timer.heap_index) != hook
- || !ast_heap_remove(interval_hooks, hook)) {
- /* Interval hook is already removed from the bridge_channel. */
- ao2_ref(hook, -1);
- continue;
- }
- ao2_ref(hook, -1);
- if (interval < 0) {
- ast_debug(1, "Removed interval hook %p from %p(%s)\n",
- hook, bridge_channel, ast_channel_name(bridge_channel->chan));
- ao2_ref(hook, -1);
- continue;
- }
- if (interval) {
- /* Set new interval for the hook. */
- hook->timer.interval = interval;
- }
- ast_debug(1, "Updating interval hook %p with interval %u on %p(%s)\n",
- hook, hook->timer.interval, bridge_channel,
- ast_channel_name(bridge_channel->chan));
- /* resetting start */
- start = ast_tvnow();
- /*
- * Resetup the interval hook for the next interval. We may need
- * to skip over any missed intervals because the hook was
- * delayed or took too long.
- */
- execution_time = ast_tvdiff_ms(start, hook->timer.trip_time);
- while (hook->timer.interval < execution_time) {
- execution_time -= hook->timer.interval;
- }
- hook->timer.trip_time = ast_tvadd(start, ast_samp2tv(hook->timer.interval - execution_time, 1000));
- hook->timer.seqno = ast_atomic_fetchadd_int((int *) &bridge_channel->features->interval_sequence, +1);
- if (ast_heap_push(interval_hooks, hook)) {
- /* Could not push the hook back onto the heap. */
- ao2_ref(hook, -1);
- }
- }
- ast_heap_unlock(interval_hooks);
- if (chan_suspended) {
- ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
- bridge_channel_unsuspend(bridge_channel);
- }
- }
- /*!
- * \internal
- * \brief Write a DTMF stream out to a channel
- */
- static int bridge_channel_write_dtmf_stream(struct ast_bridge_channel *bridge_channel, const char *dtmf)
- {
- return bridge_channel_write_action_data(bridge_channel,
- BRIDGE_CHANNEL_ACTION_DTMF_STREAM, dtmf, strlen(dtmf) + 1);
- }
- /*!
- * \internal
- * \brief Indicate to the testsuite a feature was successfully detected.
- *
- * Currently, this function only will relay built-in features to the testsuite,
- * but it could be modified to detect applicationmap items should the need arise.
- *
- * \param chan The channel that activated the feature
- * \param dtmf The DTMF sequence entered to activate the feature
- */
- static void testsuite_notify_feature_success(struct ast_channel *chan, const char *dtmf)
- {
- #ifdef TEST_FRAMEWORK
- char *feature = "unknown";
- struct ast_featuremap_config *featuremap = ast_get_chan_featuremap_config(chan);
- struct ast_features_xfer_config *xfer = ast_get_chan_features_xfer_config(chan);
- if (featuremap) {
- if (!strcmp(dtmf, featuremap->blindxfer)) {
- feature = "blindxfer";
- } else if (!strcmp(dtmf, featuremap->atxfer)) {
- feature = "atxfer";
- } else if (!strcmp(dtmf, featuremap->disconnect)) {
- feature = "disconnect";
- } else if (!strcmp(dtmf, featuremap->automon)) {
- feature = "automon";
- } else if (!strcmp(dtmf, featuremap->automixmon)) {
- feature = "automixmon";
- } else if (!strcmp(dtmf, featuremap->parkcall)) {
- feature = "parkcall";
- } else if (!strcmp(dtmf, xfer->atxferthreeway)) {
- feature = "atxferthreeway";
- }
- }
- ast_test_suite_event_notify("FEATURE_DETECTION",
- "Result: success\r\n"
- "Feature: %s", feature);
- #endif /* TEST_FRAMEWORK */
- }
- /*!
- * \internal
- * \brief Internal function that executes a feature on a bridge channel
- * \note Neither the bridge nor the bridge_channel locks should be held when entering
- * this function.
- */
- static void bridge_channel_feature(struct ast_bridge_channel *bridge_channel, const char *starting_dtmf)
- {
- struct ast_bridge_features *features = bridge_channel->features;
- struct ast_bridge_hook_dtmf *hook = NULL;
- char dtmf[MAXIMUM_DTMF_FEATURE_STRING];
- size_t dtmf_len;
- unsigned int digit_timeout;
- RAII_VAR(struct ast_features_general_config *, gen_cfg, NULL, ao2_cleanup);
- ast_channel_lock(bridge_channel->chan);
- gen_cfg = ast_get_chan_features_general_config(bridge_channel->chan);
- if (!gen_cfg) {
- ast_log(LOG_ERROR, "Unable to retrieve features configuration.\n");
- ast_channel_unlock(bridge_channel->chan);
- return;
- }
- digit_timeout = gen_cfg->featuredigittimeout;
- ast_channel_unlock(bridge_channel->chan);
- if (ast_strlen_zero(starting_dtmf)) {
- dtmf[0] = '\0';
- dtmf_len = 0;
- } else {
- ast_copy_string(dtmf, starting_dtmf, sizeof(dtmf));
- dtmf_len = strlen(dtmf);
- }
- /*
- * Check if any feature DTMF hooks match or could match and
- * try to collect more DTMF digits.
- */
- for (;;) {
- int res;
- if (dtmf_len) {
- ast_debug(1, "DTMF feature string on %p(%s) is now '%s'\n",
- bridge_channel, ast_channel_name(bridge_channel->chan), dtmf);
- /* See if a DTMF feature hook matches or can match */
- hook = ao2_find(features->dtmf_hooks, dtmf, OBJ_PARTIAL_KEY);
- if (!hook) {
- ast_debug(1, "No DTMF feature hooks on %p(%s) match '%s'\n",
- bridge_channel, ast_channel_name(bridge_channel->chan), dtmf);
- break;
- }
- if (strlen(hook->dtmf.code) == dtmf_len) {
- ast_debug(1, "DTMF feature hook %p matched DTMF string '%s' on %p(%s)\n",
- hook, dtmf, bridge_channel, ast_channel_name(bridge_channel->chan));
- break;
- }
- ao2_ref(hook, -1);
- hook = NULL;
- if (ARRAY_LEN(dtmf) - 1 <= dtmf_len) {
- /* We have reached the maximum length of a DTMF feature string. */
- break;
- }
- }
- res = ast_waitfordigit(bridge_channel->chan, digit_timeout);
- if (!res) {
- ast_debug(1, "DTMF feature string collection on %p(%s) timed out\n",
- bridge_channel, ast_channel_name(bridge_channel->chan));
- break;
- }
- if (res < 0) {
- ast_debug(1, "DTMF feature string collection failed on %p(%s) for some reason\n",
- bridge_channel, ast_channel_name(bridge_channel->chan));
- break;
- }
- /* Add the new DTMF into the DTMF string so we can do our matching */
- dtmf[dtmf_len] = res;
- dtmf[++dtmf_len] = '\0';
- }
- if (hook) {
- int remove_me;
- /* Execute the matched hook on this channel. */
- remove_me = hook->generic.callback(bridge_channel, hook->generic.hook_pvt);
- if (remove_me) {
- ast_debug(1, "DTMF hook %p is being removed from %p(%s)\n",
- hook, bridge_channel, ast_channel_name(bridge_channel->chan));
- ao2_unlink(features->dtmf_hooks, hook);
- }
- testsuite_notify_feature_success(bridge_channel->chan, hook->dtmf.code);
- ao2_ref(hook, -1);
- /*
- * If we are handing the channel off to an external hook for
- * ownership, we are not guaranteed what kind of state it will
- * come back in. If the channel hungup, we need to detect that
- * here if the hook did not already change the state.
- */
- if (bridge_channel->chan && ast_check_hangup_locked(bridge_channel->chan)) {
- ast_bridge_channel_kick(bridge_channel, 0);
- }
- } else {
- if (features->dtmf_passthrough) {
- /* Stream any collected DTMF to the other channels. */
- bridge_channel_write_dtmf_stream(bridge_channel, dtmf);
- }
- ast_test_suite_event_notify("FEATURE_DETECTION", "Result: fail");
- }
- }
- /*!
- * \internal
- * \brief Indicate that a bridge_channel is talking
- */
- static void bridge_channel_talking(struct ast_bridge_channel *bridge_channel, int talking)
- {
- struct ast_bridge_features *features = bridge_channel->features;
- struct ast_bridge_hook *hook;
- struct ao2_iterator iter;
- /* Run any talk detection hooks. */
- iter = ao2_iterator_init(features->other_hooks, 0);
- for (; (hook = ao2_iterator_next(&iter)); ao2_ref(hook, -1)) {
- int remove_me;
- ast_bridge_talking_indicate_callback talk_cb;
- if (hook->type != AST_BRIDGE_HOOK_TYPE_TALK) {
- continue;
- }
- talk_cb = (ast_bridge_talking_indicate_callback) hook->callback;
- remove_me = talk_cb(bridge_channel, hook->hook_pvt, talking);
- if (remove_me) {
- ast_debug(1, "Talk detection hook %p is being removed from %p(%s)\n",
- hook, bridge_channel, ast_channel_name(bridge_channel->chan));
- ao2_unlink(features->other_hooks, hook);
- }
- }
- ao2_iterator_destroy(&iter);
- }
- /*! \brief Internal function that plays back DTMF on a bridge channel */
- static void bridge_channel_dtmf_stream(struct ast_bridge_channel *bridge_channel, const char *dtmf)
- {
- ast_debug(1, "Playing DTMF stream '%s' out to %p(%s)\n",
- dtmf, bridge_channel, ast_channel_name(bridge_channel->chan));
- ast_dtmf_stream(bridge_channel->chan, NULL, dtmf, 0, 0);
- }
- /*! \brief Data specifying where a blind transfer is going to */
- struct blind_transfer_data {
- char exten[AST_MAX_EXTENSION];
- char context[AST_MAX_CONTEXT];
- };
- /*!
- * \internal
- * \brief Execute after bridge actions on a channel when it leaves a bridge
- */
- static void after_bridge_move_channel(struct ast_channel *chan_bridged, void *data)
- {
- RAII_VAR(struct ast_channel *, chan_target, data, ao2_cleanup);
- struct ast_party_connected_line connected_target;
- unsigned char connected_line_data[1024];
- int payload_size;
- ast_party_connected_line_init(&connected_target);
- ast_channel_lock(chan_target);
- ast_party_connected_line_copy(&connected_target, ast_channel_connected(chan_target));
- ast_channel_unlock(chan_target);
- ast_party_id_reset(&connected_target.priv);
- if (ast_channel_move(chan_target, chan_bridged)) {
- ast_softhangup(chan_target, AST_SOFTHANGUP_DEV);
- ast_party_connected_line_free(&connected_target);
- return;
- }
- if ((payload_size = ast_connected_line_build_data(connected_line_data,
- sizeof(connected_line_data), &connected_target, NULL)) != -1) {
- struct ast_control_read_action_payload *frame_payload;
- int frame_size;
- frame_size = payload_size + sizeof(*frame_payload);
- frame_payload = ast_alloca(frame_size);
- frame_payload->action = AST_FRAME_READ_ACTION_CONNECTED_LINE_MACRO;
- frame_payload->payload_size = payload_size;
- memcpy(frame_payload->payload, connected_line_data, payload_size);
- ast_queue_control_data(chan_target, AST_CONTROL_READ_ACTION, frame_payload, frame_size);
- }
- ast_party_connected_line_free(&connected_target);
- }
- /*!
- * \internal
- * \brief Execute logic to cleanup when after bridge fails
- */
- static void after_bridge_move_channel_fail(enum ast_bridge_after_cb_reason reason, void *data)
- {
- RAII_VAR(struct ast_channel *, chan_target, data, ao2_cleanup);
- ast_log(LOG_WARNING, "Unable to complete transfer: %s\n",
- ast_bridge_after_cb_reason_string(reason));
- ast_softhangup(chan_target, AST_SOFTHANGUP_DEV);
- }
- /*!
- * \internal
- * \brief Perform a blind transfer on a channel in a bridge
- */
- static void bridge_channel_blind_transfer(struct ast_bridge_channel *bridge_channel,
- struct blind_transfer_data *blind_data)
- {
- ast_async_goto(bridge_channel->chan, blind_data->context, blind_data->exten, 1);
- ast_bridge_channel_kick(bridge_channel, AST_CAUSE_NORMAL_CLEARING);
- }
- /*!
- * \internal
- * \brief Perform an attended transfer on a channel in a bridge
- */
- static void bridge_channel_attended_transfer(struct ast_bridge_channel *bridge_channel,
- const char *target_chan_name)
- {
- RAII_VAR(struct ast_channel *, chan_target, NULL, ao2_cleanup);
- RAII_VAR(struct ast_channel *, chan_bridged, NULL, ao2_cleanup);
- chan_target = ast_channel_get_by_name(target_chan_name);
- if (!chan_target) {
- /* Dang, it disappeared somehow */
- ast_bridge_channel_kick(bridge_channel, AST_CAUSE_NORMAL_CLEARING);
- return;
- }
- ast_bridge_channel_lock(bridge_channel);
- chan_bridged = bridge_channel->chan;
- ast_assert(chan_bridged != NULL);
- ao2_ref(chan_bridged, +1);
- ast_bridge_channel_unlock(bridge_channel);
- if (ast_bridge_set_after_callback(chan_bridged, after_bridge_move_channel,
- after_bridge_move_channel_fail, ast_channel_ref(chan_target))) {
- ast_softhangup(chan_target, AST_SOFTHANGUP_DEV);
- /* Release the ref we tried to pass to ast_bridge_set_after_callback(). */
- ast_channel_unref(chan_target);
- }
- ast_bridge_channel_kick(bridge_channel, AST_CAUSE_NORMAL_CLEARING);
- }
- /*!
- * \internal
- * \brief Handle bridge channel bridge action frame.
- * \since 12.0.0
- *
- * \param bridge_channel Channel to execute the action on.
- * \param action What to do.
- * \param data data from the action.
- *
- * \return Nothing
- */
- static void bridge_channel_handle_action(struct ast_bridge_channel *bridge_channel,
- enum bridge_channel_action_type action, void *data)
- {
- switch (action) {
- case BRIDGE_CHANNEL_ACTION_DTMF_STREAM:
- bridge_channel_suspend(bridge_channel);
- ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
- bridge_channel_dtmf_stream(bridge_channel, data);
- ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
- bridge_channel_unsuspend(bridge_channel);
- break;
- case BRIDGE_CHANNEL_ACTION_TALKING_START:
- case BRIDGE_CHANNEL_ACTION_TALKING_STOP:
- bridge_channel_talking(bridge_channel,
- action == BRIDGE_CHANNEL_ACTION_TALKING_START);
- break;
- case BRIDGE_CHANNEL_ACTION_PLAY_FILE:
- bridge_channel_suspend(bridge_channel);
- ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
- bridge_channel_playfile(bridge_channel, data);
- ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
- bridge_channel_unsuspend(bridge_channel);
- break;
- case BRIDGE_CHANNEL_ACTION_RUN_APP:
- bridge_channel_suspend(bridge_channel);
- ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
- bridge_channel_run_app(bridge_channel, data);
- ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
- bridge_channel_unsuspend(bridge_channel);
- break;
- case BRIDGE_CHANNEL_ACTION_CALLBACK:
- bridge_channel_do_callback(bridge_channel, data);
- break;
- case BRIDGE_CHANNEL_ACTION_PARK:
- bridge_channel_suspend(bridge_channel);
- ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
- bridge_channel_park(bridge_channel, data);
- ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
- bridge_channel_unsuspend(bridge_channel);
- break;
- case BRIDGE_CHANNEL_ACTION_BLIND_TRANSFER:
- bridge_channel_blind_transfer(bridge_channel, data);
- break;
- case BRIDGE_CHANNEL_ACTION_ATTENDED_TRANSFER:
- bridge_channel_attended_transfer(bridge_channel, data);
- break;
- default:
- break;
- }
- }
- /*!
- * \internal
- * \brief Check if a bridge should dissolve and do it.
- * \since 12.0.0
- *
- * \param bridge_channel Channel causing the check.
- *
- * \note On entry, bridge_channel->bridge is already locked.
- *
- * \return Nothing
- */
- static void bridge_channel_dissolve_check(struct ast_bridge_channel *bridge_channel)
- {
- struct ast_bridge *bridge = bridge_channel->bridge;
- if (bridge->dissolved) {
- return;
- }
- if (!bridge->num_channels
- && ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_DISSOLVE_EMPTY)) {
- /* Last channel leaving the bridge turns off the lights. */
- bridge_dissolve(bridge, ast_channel_hangupcause(bridge_channel->chan));
- return;
- }
- switch (bridge_channel->state) {
- case BRIDGE_CHANNEL_STATE_END:
- /* Do we need to dissolve the bridge because this channel hung up? */
- if (ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_DISSOLVE_HANGUP)
- || (bridge_channel->features->usable
- && ast_test_flag(&bridge_channel->features->feature_flags,
- AST_BRIDGE_CHANNEL_FLAG_DISSOLVE_HANGUP))) {
- bridge_dissolve(bridge, ast_channel_hangupcause(bridge_channel->chan));
- return;
- }
- break;
- default:
- break;
- }
- if (bridge->num_lonely && bridge->num_lonely == bridge->num_channels) {
- /*
- * This will start a chain reaction where each channel leaving
- * enters this function and causes the next to leave as long as
- * there aren't non-lonely channels in the bridge.
- */
- ast_bridge_channel_leave_bridge(AST_LIST_FIRST(&bridge->channels),
- BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE,
- ast_channel_hangupcause(bridge_channel->chan));
- }
- }
- void bridge_channel_internal_pull(struct ast_bridge_channel *bridge_channel)
- {
- struct ast_bridge *bridge = bridge_channel->bridge;
- if (!bridge_channel->in_bridge) {
- return;
- }
- bridge_channel->in_bridge = 0;
- ast_debug(1, "Bridge %s: pulling %p(%s)\n",
- bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan));
- ast_verb(3, "Channel %s left '%s' %s-bridge <%s>\n",
- ast_channel_name(bridge_channel->chan),
- bridge->technology->name,
- bridge->v_table->name,
- bridge->uniqueid);
- if (!bridge_channel->just_joined) {
- /* Tell the bridge technology we are leaving so they tear us down */
- ast_debug(1, "Bridge %s: %p(%s) is leaving %s technology\n",
- bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan),
- bridge->technology->name);
- if (bridge->technology->leave) {
- bridge->technology->leave(bridge, bridge_channel);
- }
- }
- /* Remove channel from the bridge */
- if (!bridge_channel->suspended) {
- --bridge->num_active;
- }
- if (ast_test_flag(&bridge_channel->features->feature_flags, AST_BRIDGE_CHANNEL_FLAG_LONELY)) {
- --bridge->num_lonely;
- }
- --bridge->num_channels;
- AST_LIST_REMOVE(&bridge->channels, bridge_channel, entry);
- bridge->v_table->pull(bridge, bridge_channel);
- ast_bridge_channel_clear_roles(bridge_channel);
- /* If we are not going to be hung up after leaving a bridge, and we were an
- * outgoing channel, clear the outgoing flag.
- */
- if (ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_OUTGOING)
- && (ast_channel_softhangup_internal_flag(bridge_channel->chan) & (AST_SOFTHANGUP_ASYNCGOTO | AST_SOFTHANGUP_UNBRIDGE)
- || bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT)) {
- ast_debug(2, "Channel %s will survive this bridge; clearing outgoing (dialed) flag\n", ast_channel_name(bridge_channel->chan));
- ast_clear_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_OUTGOING);
- }
- bridge_channel_dissolve_check(bridge_channel);
- bridge->reconfigured = 1;
- ast_bridge_publish_leave(bridge, bridge_channel->chan);
- }
- int bridge_channel_internal_push(struct ast_bridge_channel *bridge_channel)
- {
- struct ast_bridge *bridge = bridge_channel->bridge;
- struct ast_bridge_channel *swap;
- ast_assert(!bridge_channel->in_bridge);
- swap = bridge_find_channel(bridge, bridge_channel->swap);
- bridge_channel->swap = NULL;
- if (swap) {
- ast_debug(1, "Bridge %s: pushing %p(%s) by swapping with %p(%s)\n",
- bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan),
- swap, ast_channel_name(swap->chan));
- } else {
- ast_debug(1, "Bridge %s: pushing %p(%s)\n",
- bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan));
- }
- /* Add channel to the bridge */
- if (bridge->dissolved
- || bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT
- || (swap && swap->state != BRIDGE_CHANNEL_STATE_WAIT)
- || bridge->v_table->push(bridge, bridge_channel, swap)
- || ast_bridge_channel_establish_roles(bridge_channel)) {
- ast_debug(1, "Bridge %s: pushing %p(%s) into bridge failed\n",
- bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan));
- return -1;
- }
- bridge_channel->in_bridge = 1;
- bridge_channel->just_joined = 1;
- AST_LIST_INSERT_TAIL(&bridge->channels, bridge_channel, entry);
- ++bridge->num_channels;
- if (ast_test_flag(&bridge_channel->features->feature_flags, AST_BRIDGE_CHANNEL_FLAG_LONELY)) {
- ++bridge->num_lonely;
- }
- if (!bridge_channel->suspended) {
- ++bridge->num_active;
- }
- ast_verb(3, "Channel %s %s%s%s '%s' %s-bridge <%s>\n",
- ast_channel_name(bridge_channel->chan),
- swap ? "swapped with " : "joined",
- swap ? ast_channel_name(swap->chan) : "",
- swap ? " into" : "",
- bridge->technology->name,
- bridge->v_table->name,
- bridge->uniqueid);
- ast_bridge_publish_enter(bridge, bridge_channel->chan, swap ? swap->chan : NULL);
- if (swap) {
- ast_bridge_channel_leave_bridge(swap, BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, 0);
- bridge_channel_internal_pull(swap);
- }
- /* Clear any BLINDTRANSFER and ATTENDEDTRANSFER since the transfer has completed. */
- pbx_builtin_setvar_helper(bridge_channel->chan, "BLINDTRANSFER", NULL);
- pbx_builtin_setvar_helper(bridge_channel->chan, "ATTENDEDTRANSFER", NULL);
- /* Wake up the bridge channel thread to reevaluate any interval timers. */
- ast_queue_frame(bridge_channel->chan, &ast_null_frame);
- bridge->reconfigured = 1;
- return 0;
- }
- /*!
- * \internal
- * \brief Handle bridge channel control frame action.
- * \since 12.0.0
- *
- * \param bridge_channel Channel to execute the control frame action on.
- * \param fr Control frame to handle.
- *
- * \return Nothing
- */
- static void bridge_channel_handle_control(struct ast_bridge_channel *bridge_channel, struct ast_frame *fr)
- {
- struct ast_channel *chan;
- struct ast_option_header *aoh;
- int is_caller;
- chan = bridge_channel->chan;
- switch (fr->subclass.integer) {
- case AST_CONTROL_REDIRECTING:
- is_caller = !ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING);
- if (ast_channel_redirecting_sub(NULL, chan, fr, 1) &&
- ast_channel_redirecting_macro(NULL, chan, fr, is_caller, 1)) {
- ast_indicate_data(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
- }
- break;
- case AST_CONTROL_CONNECTED_LINE:
- is_caller = !ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING);
- if (ast_channel_connected_line_sub(NULL, chan, fr, 1) &&
- ast_channel_connected_line_macro(NULL, chan, fr, is_caller, 1)) {
- ast_indicate_data(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
- }
- break;
- case AST_CONTROL_OPTION:
- /*
- * Forward option Requests, but only ones we know are safe These
- * are ONLY sent by chan_iax2 and I'm not convinced that they
- * are useful. I haven't deleted them entirely because I just am
- * not sure of the ramifications of removing them.
- */
- aoh = fr->data.ptr;
- if (aoh && aoh->flag == AST_OPTION_FLAG_REQUEST) {
- switch (ntohs(aoh->option)) {
- case AST_OPTION_TONE_VERIFY:
- case AST_OPTION_TDD:
- case AST_OPTION_RELAXDTMF:
- case AST_OPTION_AUDIO_MODE:
- case AST_OPTION_DIGIT_DETECT:
- case AST_OPTION_FAX_DETECT:
- ast_channel_setoption(chan, ntohs(aoh->option), aoh->data,
- fr->datalen - sizeof(*aoh), 0);
- break;
- default:
- break;
- }
- }
- break;
- case AST_CONTROL_ANSWER:
- if (ast_channel_state(chan) != AST_STATE_UP) {
- ast_answer(chan);
- } else {
- ast_indicate(chan, -1);
- }
- break;
- default:
- ast_indicate_data(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
- break;
- }
- }
- /*!
- * \internal
- * \brief Handle bridge channel write frame to channel.
- * \since 12.0.0
- *
- * \param bridge_channel Channel to write outgoing frame.
- *
- * \return Nothing
- */
- static void bridge_channel_handle_write(struct ast_bridge_channel *bridge_channel)
- {
- struct ast_frame *fr;
- char nudge;
- struct sync_payload *sync_payload;
- ast_bridge_channel_lock(bridge_channel);
- if (read(bridge_channel->alert_pipe[0], &nudge, sizeof(nudge)) < 0) {
- if (errno != EINTR && errno != EAGAIN) {
- ast_log(LOG_WARNING, "read() failed for alert pipe on %p(%s): %s\n",
- bridge_channel, ast_channel_name(bridge_channel->chan), strerror(errno));
- }
- }
- fr = AST_LIST_REMOVE_HEAD(&bridge_channel->wr_queue, frame_list);
- ast_bridge_channel_unlock(bridge_channel);
- if (!fr) {
- return;
- }
- switch (fr->frametype) {
- case AST_FRAME_BRIDGE_ACTION:
- bridge_channel_handle_action(bridge_channel, fr->subclass.integer, fr->data.ptr);
- break;
- case AST_FRAME_BRIDGE_ACTION_SYNC:
- sync_payload = fr->data.ptr;
- bridge_channel_handle_action(bridge_channel, fr->subclass.integer, sync_payload->data);
- break;
- case AST_FRAME_CONTROL:
- bridge_channel_handle_control(bridge_channel, fr);
- break;
- case AST_FRAME_NULL:
- break;
- default:
- /* Write the frame to the channel. */
- bridge_channel->activity = BRIDGE_CHANNEL_THREAD_SIMPLE;
- ast_write(bridge_channel->chan, fr);
- break;
- }
- bridge_frame_free(fr);
- }
- /*! \brief Internal function to handle DTMF from a channel */
- static struct ast_frame *bridge_handle_dtmf(struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
- {
- struct ast_bridge_features *features = bridge_channel->features;
- struct ast_bridge_hook_dtmf *hook;
- char dtmf[2];
- /* See if this DTMF matches the beginning of any feature hooks. */
- dtmf[0] = frame->subclass.integer;
- dtmf[1] = '\0';
- hook = ao2_find(features->dtmf_hooks, dtmf, OBJ_PARTIAL_KEY);
- if (hook) {
- enum ast_frame_type frametype = frame->frametype;
- bridge_frame_free(frame);
- frame = NULL;
- ao2_ref(hook, -1);
- /* Collect any more needed DTMF to execute a hook. */
- bridge_channel_suspend(bridge_channel);
- ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
- switch (frametype) {
- case AST_FRAME_DTMF_BEGIN:
- bridge_channel_feature(bridge_channel, NULL);
- break;
- case AST_FRAME_DTMF_END:
- bridge_channel_feature(bridge_channel, dtmf);
- break;
- default:
- /* Unexpected frame type. */
- ast_assert(0);
- break;
- }
- ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
- bridge_channel_unsuspend(bridge_channel);
- #ifdef TEST_FRAMEWORK
- } else if (frame->frametype == AST_FRAME_DTMF_END) {
- /* Only transmit this event on DTMF end or else every DTMF
- * press will result in the event being broadcast twice
- */
- ast_test_suite_event_notify("FEATURE_DETECTION", "Result: fail");
- #endif
- }
- return frame;
- }
- /*!
- * \internal
- * \brief Feed notification that a frame is waiting on a channel into the bridging core
- *
- * \param bridge_channel Bridge channel the notification was received on
- */
- static void bridge_handle_trip(struct ast_bridge_channel *bridge_channel)
- {
- struct ast_frame *frame;
- if (bridge_channel->features->mute) {
- frame = ast_read_noaudio(bridge_channel->chan);
- } else {
- frame = ast_read(bridge_channel->chan);
- }
- if (!frame) {
- ast_bridge_channel_kick(bridge_channel, 0);
- return;
- }
- switch (frame->frametype) {
- case AST_FRAME_CONTROL:
- switch (frame->subclass.integer) {
- case AST_CONTROL_HANGUP:
- ast_bridge_channel_kick(bridge_channel, 0);
- bridge_frame_free(frame);
- return;
- default:
- break;
- }
- break;
- case AST_FRAME_DTMF_BEGIN:
- case AST_FRAME_DTMF_END:
- frame = bridge_handle_dtmf(bridge_channel, frame);
- if (!frame) {
- return;
- }
- if (!bridge_channel->features->dtmf_passthrough) {
- bridge_frame_free(frame);
- return;
- }
- break;
- default:
- break;
- }
- /* Simply write the frame out to the bridge technology. */
- bridge_channel_write_frame(bridge_channel, frame);
- bridge_frame_free(frame);
- }
- /*!
- * \internal
- * \brief Determine how long till the next timer interval.
- * \since 12.0.0
- *
- * \param bridge_channel Channel to determine how long can wait.
- *
- * \retval ms Number of milliseconds to wait.
- * \retval -1 to wait forever.
- */
- static int bridge_channel_next_interval(struct ast_bridge_channel *bridge_channel)
- {
- struct ast_heap *interval_hooks = bridge_channel->features->interval_hooks;
- struct ast_bridge_hook_timer *hook;
- int ms;
- ast_heap_wrlock(interval_hooks);
- hook = ast_heap_peek(interval_hooks, 1);
- if (hook) {
- ms = ast_tvdiff_ms(hook->timer.trip_time, ast_tvnow());
- if (ms < 0) {
- /* Expire immediately. An interval hook is ready to run. */
- ms = 0;
- }
- } else {
- /* No hook so wait forever. */
- ms = -1;
- }
- ast_heap_unlock(interval_hooks);
- return ms;
- }
- /*!
- * \internal
- * \brief Wait for something to happen on the bridge channel and handle it.
- * \since 12.0.0
- *
- * \param bridge_channel Channel to wait.
- *
- * \note Each channel does writing/reading in their own thread.
- *
- * \return Nothing
- */
- static void bridge_channel_wait(struct ast_bridge_channel *bridge_channel)
- {
- int ms;
- int outfd;
- struct ast_channel *chan;
- /* Wait for data to either come from the channel or us to be signaled */
- ast_bridge_channel_lock(bridge_channel);
- if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
- } else if (bridge_channel->suspended) {
- /* XXX ASTERISK-21271 the external party use of suspended will go away as will these references because this is the bridge channel thread */
- ast_debug(1, "Bridge %s: %p(%s) is going into a signal wait\n",
- bridge_channel->bridge->uniqueid, bridge_channel,
- ast_channel_name(bridge_channel->chan));
- ast_cond_wait(&bridge_channel->cond, ao2_object_get_lockaddr(bridge_channel));
- } else {
- ast_bridge_channel_unlock(bridge_channel);
- outfd = -1;
- ms = bridge_channel_next_interval(bridge_channel);
- chan = ast_waitfor_nandfds(&bridge_channel->chan, 1,
- &bridge_channel->alert_pipe[0], 1, NULL, &outfd, &ms);
- if (ast_channel_softhangup_internal_flag(bridge_channel->chan) & AST_SOFTHANGUP_UNBRIDGE) {
- ast_channel_clear_softhangup(bridge_channel->chan, AST_SOFTHANGUP_UNBRIDGE);
- ast_bridge_channel_lock_bridge(bridge_channel);
- bridge_channel->bridge->reconfigured = 1;
- bridge_reconfigured(bridge_channel->bridge, 0);
- ast_bridge_unlock(bridge_channel->bridge);
- }
- ast_bridge_channel_lock(bridge_channel);
- bridge_channel->activity = BRIDGE_CHANNEL_THREAD_FRAME;
- ast_bridge_channel_unlock(bridge_channel);
- if (!bridge_channel->suspended
- && bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
- if (chan) {
- bridge_handle_trip(bridge_channel);
- } else if (-1 < outfd) {
- bridge_channel_handle_write(bridge_channel);
- } else if (ms == 0) {
- /* An interval expired. */
- bridge_channel_handle_interval(bridge_channel);
- }
- }
- bridge_channel->activity = BRIDGE_CHANNEL_THREAD_IDLE;
- return;
- }
- ast_bridge_channel_unlock(bridge_channel);
- }
- /*!
- * \internal
- * \brief Handle bridge channel join/leave event.
- * \since 12.0.0
- *
- * \param bridge_channel Which channel is involved.
- * \param type Specified join/leave event.
- *
- * \return Nothing
- */
- static void bridge_channel_event_join_leave(struct ast_bridge_channel *bridge_channel, enum ast_bridge_hook_type type)
- {
- struct ast_bridge_features *features = bridge_channel->features;
- struct ast_bridge_hook *hook;
- struct ao2_iterator iter;
- /* Run the specified hooks. */
- iter = ao2_iterator_init(features->other_hooks, 0);
- for (; (hook = ao2_iterator_next(&iter)); ao2_ref(hook, -1)) {
- if (hook->type == type) {
- break;
- }
- }
- if (hook) {
- /* Found the first specified hook to run. */
- bridge_channel_suspend(bridge_channel);
- ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
- do {
- if (hook->type == type) {
- hook->callback(bridge_channel, hook->hook_pvt);
- ao2_unlink(features->other_hooks, hook);
- }
- ao2_ref(hook, -1);
- } while ((hook = ao2_iterator_next(&iter)));
- ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
- bridge_channel_unsuspend(bridge_channel);
- }
- ao2_iterator_destroy(&iter);
- }
- /*! \brief Join a channel to a bridge and handle anything the bridge may want us to do */
- int bridge_channel_internal_join(struct ast_bridge_channel *bridge_channel)
- {
- int res = 0;
- ast_format_copy(&bridge_channel->read_format, ast_channel_readformat(bridge_channel->chan));
- ast_format_copy(&bridge_channel->write_format, ast_channel_writeformat(bridge_channel->chan));
- ast_debug(1, "Bridge %s: %p(%s) is joining\n",
- bridge_channel->bridge->uniqueid,
- bridge_channel, ast_channel_name(bridge_channel->chan));
- /*
- * Directly locking the bridge is safe here because nobody else
- * knows about this bridge_channel yet.
- */
- ast_bridge_lock(bridge_channel->bridge);
- /* Make sure we're still good to be put into a bridge */
- ast_channel_lock(bridge_channel->chan);
- if (ast_channel_internal_bridge(bridge_channel->chan)
- || ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_ZOMBIE)) {
- ast_channel_unlock(bridge_channel->chan);
- ast_bridge_unlock(bridge_channel->bridge);
- ast_debug(1, "Bridge %s: %p(%s) failed to join Bridge\n",
- bridge_channel->bridge->uniqueid,
- bridge_channel,
- ast_channel_name(bridge_channel->chan));
- return -1;
- }
- ast_channel_internal_bridge_set(bridge_channel->chan, bridge_channel->bridge);
- ast_channel_unlock(bridge_channel->chan);
- /* Add the jitterbuffer if the channel requires it */
- ast_jb_enable_for_channel(bridge_channel->chan);
- if (!bridge_channel->bridge->callid) {
- bridge_channel->bridge->callid = ast_read_threadstorage_callid();
- }
- if (bridge_channel_internal_push(bridge_channel)) {
- int cause = bridge_channel->bridge->cause;
- ast_bridge_unlock(bridge_channel->bridge);
- ast_bridge_channel_kick(bridge_channel, cause);
- ast_bridge_channel_lock_bridge(bridge_channel);
- ast_bridge_features_remove(bridge_channel->features,
- AST_BRIDGE_HOOK_REMOVE_ON_PULL);
- bridge_channel_dissolve_check(bridge_channel);
- res = -1;
- }
- bridge_reconfigured(bridge_channel->bridge, !bridge_channel->inhibit_colp);
- if (bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
- /*
- * Indicate a source change since this channel is entering the
- * bridge system only if the bridge technology is not MULTIMIX
- * capable. The MULTIMIX technology has already done it.
- */
- if (!(bridge_channel->bridge->technology->capabilities
- & AST_BRIDGE_CAPABILITY_MULTIMIX)) {
- ast_indicate(bridge_channel->chan, AST_CONTROL_SRCCHANGE);
- }
- ast_bridge_unlock(bridge_channel->bridge);
- bridge_channel_event_join_leave(bridge_channel, AST_BRIDGE_HOOK_TYPE_JOIN);
- while (bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
- /* Wait for something to do. */
- bridge_channel_wait(bridge_channel);
- }
- bridge_channel_event_join_leave(bridge_channel, AST_BRIDGE_HOOK_TYPE_LEAVE);
- ast_bridge_channel_lock_bridge(bridge_channel);
- }
- bridge_channel_internal_pull(bridge_channel);
- bridge_channel_settle_owed_events(bridge_channel->bridge, bridge_channel);
- bridge_reconfigured(bridge_channel->bridge, 1);
- ast_bridge_unlock(bridge_channel->bridge);
- /* Complete any active hold before exiting the bridge. */
- if (ast_channel_hold_state(bridge_channel->chan) == AST_CONTROL_HOLD) {
- ast_debug(1, "Channel %s simulating UNHOLD for bridge end.\n",
- ast_channel_name(bridge_channel->chan));
- ast_indicate(bridge_channel->chan, AST_CONTROL_UNHOLD);
- }
- /* Complete any partial DTMF digit before exiting the bridge. */
- if (ast_channel_sending_dtmf_digit(bridge_channel->chan)) {
- ast_channel_end_dtmf(bridge_channel->chan,
- ast_channel_sending_dtmf_digit(bridge_channel->chan),
- ast_channel_sending_dtmf_tv(bridge_channel->chan), "bridge end");
- }
- /* Indicate a source change since this channel is leaving the bridge system. */
- ast_indicate(bridge_channel->chan, AST_CONTROL_SRCCHANGE);
- /*
- * Wait for any dual redirect to complete.
- *
- * Must be done while "still in the bridge" for ast_async_goto()
- * to work right.
- */
- while (ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_BRIDGE_DUAL_REDIRECT_WAIT)) {
- sched_yield();
- }
- ast_channel_lock(bridge_channel->chan);
- ast_channel_internal_bridge_set(bridge_channel->chan, NULL);
- ast_channel_unlock(bridge_channel->chan);
- ast_bridge_channel_restore_formats(bridge_channel);
- return res;
- }
- int bridge_channel_internal_queue_blind_transfer(struct ast_channel *transferee,
- const char *exten, const char *context,
- transfer_channel_cb new_channel_cb, void *user_data)
- {
- RAII_VAR(struct ast_bridge_channel *, transferee_bridge_channel, NULL, ao2_cleanup);
- struct blind_transfer_data blind_data;
- ast_channel_lock(transferee);
- transferee_bridge_channel = ast_channel_get_bridge_channel(transferee);
- ast_channel_unlock(transferee);
- if (!transferee_bridge_channel) {
- return -1;
- }
- if (new_channel_cb) {
- new_channel_cb(transferee, user_data, AST_BRIDGE_TRANSFER_SINGLE_PARTY);
- }
- ast_copy_string(blind_data.exten, exten, sizeof(blind_data.exten));
- ast_copy_string(blind_data.context, context, sizeof(blind_data.context));
- return bridge_channel_queue_action_data(transferee_bridge_channel,
- BRIDGE_CHANNEL_ACTION_BLIND_TRANSFER, &blind_data, sizeof(blind_data));
- }
- int bridge_channel_internal_queue_attended_transfer(struct ast_channel *transferee,
- struct ast_channel *unbridged_chan)
- {
- RAII_VAR(struct ast_bridge_channel *, transferee_bridge_channel, NULL, ao2_cleanup);
- char unbridged_chan_name[AST_CHANNEL_NAME];
- ast_channel_lock(transferee);
- transferee_bridge_channel = ast_channel_get_bridge_channel(transferee);
- ast_channel_unlock(transferee);
- if (!transferee_bridge_channel) {
- return -1;
- }
- ast_copy_string(unbridged_chan_name, ast_channel_name(unbridged_chan),
- sizeof(unbridged_chan_name));
- return bridge_channel_queue_action_data(transferee_bridge_channel,
- BRIDGE_CHANNEL_ACTION_ATTENDED_TRANSFER, unbridged_chan_name,
- sizeof(unbridged_chan_name));
- }
- int bridge_channel_internal_allows_optimization(struct ast_bridge_channel *bridge_channel)
- {
- return bridge_channel->in_bridge
- && AST_LIST_EMPTY(&bridge_channel->wr_queue);
- }
- /*!
- * \internal
- * \brief Close a pipe.
- * \since 12.0.0
- *
- * \param my_pipe What to close.
- *
- * \return Nothing
- */
- static void pipe_close(int *my_pipe)
- {
- if (my_pipe[0] > -1) {
- close(my_pipe[0]);
- my_pipe[0] = -1;
- }
- if (my_pipe[1] > -1) {
- close(my_pipe[1]);
- my_pipe[1] = -1;
- }
- }
- /*!
- * \internal
- * \brief Initialize a pipe as non-blocking.
- * \since 12.0.0
- *
- * \param my_pipe What to initialize.
- *
- * \retval 0 on success.
- * \retval -1 on error.
- */
- static int pipe_init_nonblock(int *my_pipe)
- {
- int flags;
- my_pipe[0] = -1;
- my_pipe[1] = -1;
- if (pipe(my_pipe)) {
- ast_log(LOG_WARNING, "Can't create pipe! Try increasing max file descriptors with ulimit -n\n");
- return -1;
- }
- flags = fcntl(my_pipe[0], F_GETFL);
- if (fcntl(my_pipe[0], F_SETFL, flags | O_NONBLOCK) < 0) {
- ast_log(LOG_WARNING, "Unable to set read pipe nonblocking! (%d: %s)\n",
- errno, strerror(errno));
- return -1;
- }
- flags = fcntl(my_pipe[1], F_GETFL);
- if (fcntl(my_pipe[1], F_SETFL, flags | O_NONBLOCK) < 0) {
- ast_log(LOG_WARNING, "Unable to set write pipe nonblocking! (%d: %s)\n",
- errno, strerror(errno));
- return -1;
- }
- return 0;
- }
- /* Destroy elements of the bridge channel structure and the bridge channel structure itself */
- static void bridge_channel_destroy(void *obj)
- {
- struct ast_bridge_channel *bridge_channel = obj;
- struct ast_frame *fr;
- if (bridge_channel->callid) {
- bridge_channel->callid = ast_callid_unref(bridge_channel->callid);
- }
- if (bridge_channel->bridge) {
- ao2_ref(bridge_channel->bridge, -1);
- bridge_channel->bridge = NULL;
- }
- /* Flush any unhandled wr_queue frames. */
- while ((fr = AST_LIST_REMOVE_HEAD(&bridge_channel->wr_queue, frame_list))) {
- bridge_frame_free(fr);
- }
- pipe_close(bridge_channel->alert_pipe);
- ast_cond_destroy(&bridge_channel->cond);
- }
- struct ast_bridge_channel *bridge_channel_internal_alloc(struct ast_bridge *bridge)
- {
- struct ast_bridge_channel *bridge_channel;
- bridge_channel = ao2_alloc(sizeof(struct ast_bridge_channel), bridge_channel_destroy);
- if (!bridge_channel) {
- return NULL;
- }
- ast_cond_init(&bridge_channel->cond, NULL);
- if (pipe_init_nonblock(bridge_channel->alert_pipe)) {
- ao2_ref(bridge_channel, -1);
- return NULL;
- }
- if (bridge) {
- bridge_channel->bridge = bridge;
- ao2_ref(bridge_channel->bridge, +1);
- }
- return bridge_channel;
- }
|