bridge_channel.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 2007 - 2009, 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 Bridging Channel API
00022  *
00023  * \author Joshua Colp <jcolp@digium.com>
00024  * \author Richard Mudgett <rmudgett@digium.com>
00025  * \author Matt Jordan <mjordan@digium.com>
00026  *
00027  */
00028 
00029 /*** MODULEINFO
00030    <support_level>core</support_level>
00031  ***/
00032 
00033 #include "asterisk.h"
00034 
00035 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 434217 $")
00036 
00037 #include <signal.h>
00038 
00039 #include "asterisk/heap.h"
00040 #include "asterisk/astobj2.h"
00041 #include "asterisk/stringfields.h"
00042 #include "asterisk/app.h"
00043 #include "asterisk/pbx.h"
00044 #include "asterisk/channel.h"
00045 #include "asterisk/timing.h"
00046 #include "asterisk/bridge.h"
00047 #include "asterisk/bridge_channel.h"
00048 #include "asterisk/bridge_after.h"
00049 #include "asterisk/bridge_channel_internal.h"
00050 #include "asterisk/bridge_internal.h"
00051 #include "asterisk/stasis_bridges.h"
00052 #include "asterisk/stasis_channels.h"
00053 #include "asterisk/musiconhold.h"
00054 #include "asterisk/features_config.h"
00055 #include "asterisk/parking.h"
00056 #include "asterisk/causes.h"
00057 #include "asterisk/test.h"
00058 #include "asterisk/sem.h"
00059 
00060 /*!
00061  * \brief Used to queue an action frame onto a bridge channel and write an action frame into a bridge.
00062  * \since 12.0.0
00063  *
00064  * \param bridge_channel Which channel work with.
00065  * \param action Type of bridge action frame.
00066  * \param data Frame payload data to pass.
00067  * \param datalen Frame payload data length to pass.
00068  *
00069  * \retval 0 on success.
00070  * \retval -1 on error.
00071  */
00072 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);
00073 
00074 /*!
00075  * \brief Counter used for assigning synchronous bridge action IDs
00076  */
00077 static int sync_ids;
00078 
00079 /*!
00080  * \brief Frame payload for synchronous bridge actions.
00081  *
00082  * The payload serves as a wrapper around the actual payload of the
00083  * frame, with the addition of an id used to find the associated
00084  * bridge_sync object.
00085  */
00086 struct sync_payload {
00087    /*! Unique ID for this synchronous action */
00088    unsigned int id;
00089    /*! Actual frame data to process */
00090    unsigned char data[0];
00091 };
00092 
00093 /*!
00094  * \brief Synchronous bridge action object.
00095  *
00096  * Synchronous bridge actions require the ability for one thread to wait
00097  * and for another thread to indicate that the action has completed. This
00098  * structure facilitates that goal by providing synchronization structures.
00099  */
00100 struct bridge_sync {
00101    /*! Unique ID of this synchronization object. Corresponds with ID in synchronous frame payload */
00102    unsigned int id;
00103    /*! Semaphore used for synchronization */
00104    struct ast_sem sem;
00105    /*! Pointer to next entry in the list */
00106    AST_LIST_ENTRY(bridge_sync) list;
00107 };
00108 
00109 /*!
00110  * \brief List holding active synchronous action objects.
00111  */
00112 static AST_RWLIST_HEAD_STATIC(sync_structs, bridge_sync);
00113 
00114 /*!
00115  * \brief initialize a synchronous bridge object.
00116  *
00117  * This both initializes the structure and adds it to the list of
00118  * synchronization structures.
00119  *
00120  * \param sync_struct The synchronization object to initialize.
00121  * \param id ID to assign to the synchronization object.
00122  */
00123 static void bridge_sync_init(struct bridge_sync *sync_struct, unsigned int id)
00124 {
00125    memset(sync_struct, 0, sizeof(*sync_struct));
00126    sync_struct->id = id;
00127    ast_sem_init(&sync_struct->sem, 0, 0);
00128 
00129    AST_RWLIST_WRLOCK(&sync_structs);
00130    AST_RWLIST_INSERT_TAIL(&sync_structs, sync_struct, list);
00131    AST_RWLIST_UNLOCK(&sync_structs);
00132 }
00133 
00134 /*!
00135  * \brief Clean up a syncrhonization bridge object.
00136  *
00137  * This frees fields within the synchronization object and removes
00138  * it from the list of active synchronization objects.
00139  *
00140  * Since synchronization objects are stack-allocated, it is vital
00141  * that this is called before the synchronization object goes
00142  * out of scope.
00143  *
00144  * \param sync_struct Synchronization object to clean up.
00145  */
00146 static void bridge_sync_cleanup(struct bridge_sync *sync_struct)
00147 {
00148    struct bridge_sync *iter;
00149 
00150    AST_RWLIST_WRLOCK(&sync_structs);
00151    AST_LIST_TRAVERSE_SAFE_BEGIN(&sync_structs, iter, list) {
00152       if (iter->id == sync_struct->id) {
00153          AST_LIST_REMOVE_CURRENT(list);
00154          break;
00155       }
00156    }
00157    AST_LIST_TRAVERSE_SAFE_END;
00158    AST_RWLIST_UNLOCK(&sync_structs);
00159 
00160    ast_sem_destroy(&sync_struct->sem);
00161 }
00162 
00163 /*!
00164  * \brief Failsafe for synchronous bridge action waiting.
00165  *
00166  * When waiting for a synchronous bridge action to complete,
00167  * if there is a frame resource leak somewhere, it is possible
00168  * that we will never get notified that the synchronous action
00169  * completed.
00170  *
00171  * If a significant amount of time passes, then we will abandon
00172  * waiting for the synchrnous bridge action to complete.
00173  *
00174  * This constant represents the number of milliseconds we will
00175  * wait for the bridge action to complete.
00176  */
00177 #define PLAYBACK_TIMEOUT (600 * 1000)
00178 
00179 /*!
00180  * \brief Wait for a synchronous bridge action to complete.
00181  *
00182  * \param sync_struct Synchronization object corresponding to the bridge action.
00183  */
00184 static void bridge_sync_wait(struct bridge_sync *sync_struct)
00185 {
00186    struct timeval timeout_val = ast_tvadd(ast_tvnow(), ast_samp2tv(PLAYBACK_TIMEOUT, 1000));
00187    struct timespec timeout_spec = {
00188       .tv_sec = timeout_val.tv_sec,
00189       .tv_nsec = timeout_val.tv_usec * 1000,
00190    };
00191 
00192    ast_sem_timedwait(&sync_struct->sem, &timeout_spec);
00193 }
00194 
00195 /*!
00196  * \brief Signal that waiting for a synchronous bridge action is no longer necessary.
00197  *
00198  * This may occur for several reasons
00199  * \li The synchronous bridge action has completed.
00200  * \li The bridge channel has been removed from the bridge.
00201  * \li The synchronous bridge action could not be queued.
00202  *
00203  * \param sync_struct Synchronization object corresponding to the bridge action.
00204  */
00205 static void bridge_sync_signal(struct bridge_sync *sync_struct)
00206 {
00207    ast_sem_post(&sync_struct->sem);
00208 }
00209 
00210 void ast_bridge_channel_lock_bridge(struct ast_bridge_channel *bridge_channel)
00211 {
00212    struct ast_bridge *bridge;
00213 
00214    for (;;) {
00215       /* Safely get the bridge pointer */
00216       ast_bridge_channel_lock(bridge_channel);
00217       bridge = bridge_channel->bridge;
00218       ao2_ref(bridge, +1);
00219       ast_bridge_channel_unlock(bridge_channel);
00220 
00221       /* Lock the bridge and see if it is still the bridge we need to lock. */
00222       ast_bridge_lock(bridge);
00223       if (bridge == bridge_channel->bridge) {
00224          ao2_ref(bridge, -1);
00225          return;
00226       }
00227       ast_bridge_unlock(bridge);
00228       ao2_ref(bridge, -1);
00229    }
00230 }
00231 
00232 int ast_bridge_channel_notify_talking(struct ast_bridge_channel *bridge_channel, int started_talking)
00233 {
00234    struct ast_frame action = {
00235       .frametype = AST_FRAME_BRIDGE_ACTION,
00236       .subclass.integer = started_talking
00237          ? BRIDGE_CHANNEL_ACTION_TALKING_START : BRIDGE_CHANNEL_ACTION_TALKING_STOP,
00238    };
00239 
00240    return ast_bridge_channel_queue_frame(bridge_channel, &action);
00241 }
00242 
00243 /*!
00244  * \internal
00245  * \brief Poke the bridge_channel thread
00246  */
00247 static void bridge_channel_poke(struct ast_bridge_channel *bridge_channel)
00248 {
00249    if (!pthread_equal(pthread_self(), bridge_channel->thread)) {
00250       /* Wake up the bridge channel thread. */
00251       ast_queue_frame(bridge_channel->chan, &ast_null_frame);
00252    }
00253 }
00254 
00255 /*!
00256  * \internal
00257  * \brief Set actual cause on channel.
00258  * \since 12.0.0
00259  *
00260  * \param chan Channel to set cause.
00261  * \param cause Cause to set on channel.
00262  *   If cause <= 0 then use cause on channel if cause still <= 0 use AST_CAUSE_NORMAL_CLEARING.
00263  *
00264  * \return Actual cause set on channel.
00265  */
00266 static int channel_set_cause(struct ast_channel *chan, int cause)
00267 {
00268    ast_channel_lock(chan);
00269    if (cause <= 0) {
00270       cause = ast_channel_hangupcause(chan);
00271       if (cause <= 0) {
00272          cause = AST_CAUSE_NORMAL_CLEARING;
00273       }
00274    }
00275    ast_channel_hangupcause_set(chan, cause);
00276    ast_channel_unlock(chan);
00277    return cause;
00278 }
00279 
00280 void ast_bridge_channel_leave_bridge_nolock(struct ast_bridge_channel *bridge_channel, enum bridge_channel_state new_state, int cause)
00281 {
00282    if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
00283       return;
00284    }
00285 
00286    ast_debug(1, "Setting %p(%s) state from:%u to:%u\n",
00287       bridge_channel, ast_channel_name(bridge_channel->chan), bridge_channel->state,
00288       new_state);
00289 
00290    channel_set_cause(bridge_channel->chan, cause);
00291 
00292    /* Change the state on the bridge channel */
00293    bridge_channel->state = new_state;
00294 
00295    bridge_channel_poke(bridge_channel);
00296 }
00297 
00298 void ast_bridge_channel_leave_bridge(struct ast_bridge_channel *bridge_channel, enum bridge_channel_state new_state, int cause)
00299 {
00300    ast_bridge_channel_lock(bridge_channel);
00301    ast_bridge_channel_leave_bridge_nolock(bridge_channel, new_state, cause);
00302    ast_bridge_channel_unlock(bridge_channel);
00303 }
00304 
00305 struct ast_bridge_channel *ast_bridge_channel_peer(struct ast_bridge_channel *bridge_channel)
00306 {
00307    struct ast_bridge *bridge = bridge_channel->bridge;
00308    struct ast_bridge_channel *other = NULL;
00309 
00310    if (bridge_channel->in_bridge && bridge->num_channels == 2) {
00311       AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
00312          if (other != bridge_channel) {
00313             break;
00314          }
00315       }
00316    }
00317 
00318    return other;
00319 }
00320 
00321 void ast_bridge_channel_restore_formats(struct ast_bridge_channel *bridge_channel)
00322 {
00323    ast_assert(bridge_channel->read_format != NULL);
00324    ast_assert(bridge_channel->write_format != NULL);
00325 
00326    ast_channel_lock(bridge_channel->chan);
00327 
00328    /* Restore original formats of the channel as they came in */
00329    if (ast_format_cmp(ast_channel_readformat(bridge_channel->chan), bridge_channel->read_format) == AST_FORMAT_CMP_NOT_EQUAL) {
00330       ast_debug(1, "Bridge is returning %p(%s) to read format %s\n",
00331          bridge_channel, ast_channel_name(bridge_channel->chan),
00332          ast_format_get_name(bridge_channel->read_format));
00333       if (ast_set_read_format(bridge_channel->chan, bridge_channel->read_format)) {
00334          ast_debug(1, "Bridge failed to return %p(%s) to read format %s\n",
00335             bridge_channel, ast_channel_name(bridge_channel->chan),
00336             ast_format_get_name(bridge_channel->read_format));
00337       }
00338    }
00339    if (ast_format_cmp(ast_channel_writeformat(bridge_channel->chan), bridge_channel->write_format) == AST_FORMAT_CMP_NOT_EQUAL) {
00340       ast_debug(1, "Bridge is returning %p(%s) to write format %s\n",
00341          bridge_channel, ast_channel_name(bridge_channel->chan),
00342          ast_format_get_name(bridge_channel->write_format));
00343       if (ast_set_write_format(bridge_channel->chan, bridge_channel->write_format)) {
00344          ast_debug(1, "Bridge failed to return %p(%s) to write format %s\n",
00345             bridge_channel, ast_channel_name(bridge_channel->chan),
00346             ast_format_get_name(bridge_channel->write_format));
00347       }
00348    }
00349 
00350    ast_channel_unlock(bridge_channel->chan);
00351 }
00352 
00353 struct ast_bridge *ast_bridge_channel_merge_inhibit(struct ast_bridge_channel *bridge_channel, int request)
00354 {
00355    struct ast_bridge *bridge;
00356 
00357    ast_bridge_channel_lock_bridge(bridge_channel);
00358    bridge = bridge_channel->bridge;
00359    ao2_ref(bridge, +1);
00360    bridge_merge_inhibit_nolock(bridge, request);
00361    ast_bridge_unlock(bridge);
00362    return bridge;
00363 }
00364 
00365 void ast_bridge_channel_update_linkedids(struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
00366 {
00367    struct ast_bridge_channel *other;
00368    struct ast_bridge *bridge = bridge_channel->bridge;
00369    struct ast_channel *oldest_linkedid_chan = bridge_channel->chan;
00370 
00371    AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
00372       if (other == swap) {
00373          continue;
00374       }
00375       oldest_linkedid_chan = ast_channel_internal_oldest_linkedid(
00376          oldest_linkedid_chan, other->chan);
00377    }
00378 
00379    ast_channel_lock(bridge_channel->chan);
00380    ast_channel_internal_copy_linkedid(bridge_channel->chan, oldest_linkedid_chan);
00381    ast_channel_unlock(bridge_channel->chan);
00382    AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
00383       if (other == swap) {
00384          continue;
00385       }
00386       ast_channel_lock(other->chan);
00387       ast_channel_internal_copy_linkedid(other->chan, oldest_linkedid_chan);
00388       ast_channel_unlock(other->chan);
00389    }
00390 }
00391 
00392 /*!
00393  * \internal
00394  * \brief Set dest's empty peeraccount with the src's non-empty accountcode.
00395  * \since 12.5.0
00396  *
00397  * \param dest Channel to update peeraccount.
00398  * \param src Channel to get accountcode from.
00399  *
00400  * \note Both channels are already locked.
00401  *
00402  * \return Nothing
00403  */
00404 static void channel_fill_empty_peeraccount(struct ast_channel *dest, struct ast_channel *src)
00405 {
00406    if (ast_strlen_zero(ast_channel_peeraccount(dest))
00407       && !ast_strlen_zero(ast_channel_accountcode(src))) {
00408       ast_debug(1, "Setting channel %s peeraccount with channel %s accountcode '%s'.\n",
00409          ast_channel_name(dest),
00410          ast_channel_name(src), ast_channel_accountcode(src));
00411       ast_channel_peeraccount_set(dest, ast_channel_accountcode(src));
00412    }
00413 }
00414 
00415 /*!
00416  * \internal
00417  * \brief Set dest's empty accountcode with the src's non-empty peeraccount.
00418  * \since 12.5.0
00419  *
00420  * \param dest Channel to update accountcode.
00421  * \param src Channel to get peeraccount from.
00422  *
00423  * \note Both channels are already locked.
00424  *
00425  * \return Nothing
00426  */
00427 static void channel_fill_empty_accountcode(struct ast_channel *dest, struct ast_channel *src)
00428 {
00429    if (ast_strlen_zero(ast_channel_accountcode(dest))
00430       && !ast_strlen_zero(ast_channel_peeraccount(src))) {
00431       ast_debug(1, "Setting channel %s accountcode with channel %s peeraccount '%s'.\n",
00432          ast_channel_name(dest),
00433          ast_channel_name(src), ast_channel_peeraccount(src));
00434       ast_channel_accountcode_set(dest, ast_channel_peeraccount(src));
00435    }
00436 }
00437 
00438 /*!
00439  * \internal
00440  * \brief Set empty peeraccount and accountcode in a channel from the other channel.
00441  * \since 12.5.0
00442  *
00443  * \param c0 First bridge channel to update.
00444  * \param c1 Second bridge channel to update.
00445  *
00446  * \note Both channels are already locked.
00447  *
00448  * \return Nothing
00449  */
00450 static void channel_set_empty_accountcodes(struct ast_channel *c0, struct ast_channel *c1)
00451 {
00452    /* Set empty peeraccount from the other channel's accountcode. */
00453    channel_fill_empty_peeraccount(c0, c1);
00454    channel_fill_empty_peeraccount(c1, c0);
00455 
00456    /* Set empty accountcode from the other channel's peeraccount. */
00457    channel_fill_empty_accountcode(c0, c1);
00458    channel_fill_empty_accountcode(c1, c0);
00459 }
00460 
00461 /*!
00462  * \internal
00463  * \brief Update dest's peeraccount with the src's different accountcode.
00464  * \since 12.5.0
00465  *
00466  * \param dest Channel to update peeraccount.
00467  * \param src Channel to get accountcode from.
00468  *
00469  * \note Both channels are already locked.
00470  *
00471  * \return Nothing
00472  */
00473 static void channel_update_peeraccount(struct ast_channel *dest, struct ast_channel *src)
00474 {
00475    if (strcmp(ast_channel_accountcode(src), ast_channel_peeraccount(dest))) {
00476       ast_debug(1, "Changing channel %s peeraccount '%s' to match channel %s accountcode '%s'.\n",
00477          ast_channel_name(dest), ast_channel_peeraccount(dest),
00478          ast_channel_name(src), ast_channel_accountcode(src));
00479       ast_channel_peeraccount_set(dest, ast_channel_accountcode(src));
00480    }
00481 }
00482 
00483 /*!
00484  * \internal
00485  * \brief Update peeraccounts to match the other channel's accountcode.
00486  * \since 12.5.0
00487  *
00488  * \param c0 First channel to update.
00489  * \param c1 Second channel to update.
00490  *
00491  * \note Both channels are already locked.
00492  *
00493  * \return Nothing
00494  */
00495 static void channel_update_peeraccounts(struct ast_channel *c0, struct ast_channel *c1)
00496 {
00497    channel_update_peeraccount(c0, c1);
00498    channel_update_peeraccount(c1, c0);
00499 }
00500 
00501 /*!
00502  * \internal
00503  * \brief Update channel accountcodes because a channel is joining a bridge.
00504  * \since 12.5.0
00505  *
00506  * \param joining Channel joining the bridge.
00507  * \param swap Channel being replaced by the joining channel.  May be NULL.
00508  *
00509  * \note The bridge must be locked prior to calling this function.
00510  *
00511  * \return Nothing
00512  */
00513 static void bridge_channel_update_accountcodes_joining(struct ast_bridge_channel *joining, struct ast_bridge_channel *swap)
00514 {
00515    struct ast_bridge *bridge = joining->bridge;
00516    struct ast_bridge_channel *other;
00517    unsigned int swap_in_bridge = 0;
00518    unsigned int will_be_two_party;
00519 
00520    /*
00521     * Only update the peeraccount to match if the joining channel
00522     * will make it a two party bridge.
00523     */
00524    if (bridge->num_channels <= 2 && swap) {
00525       AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
00526          if (other == swap) {
00527             swap_in_bridge = 1;
00528             break;
00529          }
00530       }
00531    }
00532    will_be_two_party = (1 == bridge->num_channels - swap_in_bridge);
00533 
00534    AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
00535       if (other == swap) {
00536          continue;
00537       }
00538       ast_assert(joining != other);
00539       ast_channel_lock_both(joining->chan, other->chan);
00540       channel_set_empty_accountcodes(joining->chan, other->chan);
00541       if (will_be_two_party) {
00542          channel_update_peeraccounts(joining->chan, other->chan);
00543       }
00544       ast_channel_unlock(joining->chan);
00545       ast_channel_unlock(other->chan);
00546    }
00547 }
00548 
00549 /*!
00550  * \internal
00551  * \brief Update channel peeraccount codes because a channel has left a bridge.
00552  * \since 12.5.0
00553  *
00554  * \param leaving Channel leaving the bridge. (Has already been removed actually)
00555  *
00556  * \note The bridge must be locked prior to calling this function.
00557  *
00558  * \return Nothing
00559  */
00560 static void bridge_channel_update_accountcodes_leaving(struct ast_bridge_channel *leaving)
00561 {
00562    struct ast_bridge *bridge = leaving->bridge;
00563    struct ast_bridge_channel *first;
00564    struct ast_bridge_channel *second;
00565 
00566    if (bridge->num_channels != 2 || bridge->dissolved) {
00567       return;
00568    }
00569 
00570    first = AST_LIST_FIRST(&bridge->channels);
00571    second = AST_LIST_LAST(&bridge->channels);
00572    ast_assert(first && first != second);
00573    ast_channel_lock_both(first->chan, second->chan);
00574    channel_set_empty_accountcodes(first->chan, second->chan);
00575    channel_update_peeraccounts(first->chan, second->chan);
00576    ast_channel_unlock(second->chan);
00577    ast_channel_unlock(first->chan);
00578 }
00579 
00580 void ast_bridge_channel_update_accountcodes(struct ast_bridge_channel *joining, struct ast_bridge_channel *leaving)
00581 {
00582    if (joining) {
00583       bridge_channel_update_accountcodes_joining(joining, leaving);
00584    } else {
00585       bridge_channel_update_accountcodes_leaving(leaving);
00586    }
00587 }
00588 
00589 void ast_bridge_channel_kick(struct ast_bridge_channel *bridge_channel, int cause)
00590 {
00591    struct ast_bridge_features *features = bridge_channel->features;
00592    struct ast_bridge_hook *hook;
00593    struct ao2_iterator iter;
00594 
00595    ast_bridge_channel_lock(bridge_channel);
00596    if (bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
00597       channel_set_cause(bridge_channel->chan, cause);
00598       cause = 0;
00599    }
00600    ast_bridge_channel_unlock(bridge_channel);
00601 
00602    /* Run any hangup hooks. */
00603    iter = ao2_iterator_init(features->other_hooks, 0);
00604    for (; (hook = ao2_iterator_next(&iter)); ao2_ref(hook, -1)) {
00605       int remove_me;
00606 
00607       if (hook->type != AST_BRIDGE_HOOK_TYPE_HANGUP) {
00608          continue;
00609       }
00610       remove_me = hook->callback(bridge_channel, hook->hook_pvt);
00611       if (remove_me) {
00612          ast_debug(1, "Hangup hook %p is being removed from %p(%s)\n",
00613             hook, bridge_channel, ast_channel_name(bridge_channel->chan));
00614          ao2_unlink(features->other_hooks, hook);
00615       }
00616    }
00617    ao2_iterator_destroy(&iter);
00618 
00619    /* Default hangup action. */
00620    ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END, cause);
00621 }
00622 
00623 /*!
00624  * \internal
00625  * \brief Write an \ref ast_frame onto the bridge channel
00626  * \since 12.0.0
00627  *
00628  * \param bridge_channel Which channel to queue the frame onto.
00629  * \param frame The frame to write onto the bridge_channel
00630  *
00631  * \retval 0 on success.
00632  * \retval -1 on error.
00633  */
00634 static int bridge_channel_write_frame(struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
00635 {
00636    ast_assert(frame->frametype != AST_FRAME_BRIDGE_ACTION_SYNC);
00637 
00638    ast_bridge_channel_lock_bridge(bridge_channel);
00639 /*
00640  * XXX need to implement a deferred write queue for when there
00641  * is no peer channel in the bridge (yet or it was kicked).
00642  *
00643  * The tech decides if a frame needs to be pushed back for deferral.
00644  * simple_bridge/native_bridge are likely the only techs that will do this.
00645  */
00646    bridge_channel->bridge->technology->write(bridge_channel->bridge, bridge_channel, frame);
00647 
00648    /* Remember any owed events to the bridge. */
00649    switch (frame->frametype) {
00650    case AST_FRAME_DTMF_BEGIN:
00651       bridge_channel->owed.dtmf_tv = ast_tvnow();
00652       bridge_channel->owed.dtmf_digit = frame->subclass.integer;
00653       break;
00654    case AST_FRAME_DTMF_END:
00655       bridge_channel->owed.dtmf_digit = '\0';
00656       break;
00657    case AST_FRAME_CONTROL:
00658       /*
00659        * We explicitly will not remember HOLD/UNHOLD frames because
00660        * things like attended transfers will handle them.
00661        */
00662    default:
00663       break;
00664    }
00665    ast_bridge_unlock(bridge_channel->bridge);
00666 
00667    /*
00668     * Claim successful write to bridge.  If deferred frame
00669     * support is added, claim successfully deferred.
00670     */
00671    return 0;
00672 }
00673 
00674 void bridge_channel_settle_owed_events(struct ast_bridge *orig_bridge, struct ast_bridge_channel *bridge_channel)
00675 {
00676    if (bridge_channel->owed.dtmf_digit) {
00677       struct ast_frame frame = {
00678          .frametype = AST_FRAME_DTMF_END,
00679          .subclass.integer = bridge_channel->owed.dtmf_digit,
00680          .src = "Bridge channel owed DTMF",
00681       };
00682 
00683       frame.len = ast_tvdiff_ms(ast_tvnow(), bridge_channel->owed.dtmf_tv);
00684       if (frame.len < option_dtmfminduration) {
00685          frame.len = option_dtmfminduration;
00686       }
00687       ast_log(LOG_DTMF, "DTMF end '%c' simulated to bridge %s because %s left.  Duration %ld ms.\n",
00688          bridge_channel->owed.dtmf_digit, orig_bridge->uniqueid,
00689          ast_channel_name(bridge_channel->chan), frame.len);
00690       bridge_channel->owed.dtmf_digit = '\0';
00691       orig_bridge->technology->write(orig_bridge, NULL, &frame);
00692    }
00693 }
00694 
00695 /*!
00696  * \internal
00697  * \brief Suspend a channel from a bridge.
00698  *
00699  * \param bridge_channel Channel to suspend.
00700  *
00701  * \note This function assumes bridge_channel->bridge is locked.
00702  *
00703  * \return Nothing
00704  */
00705 void bridge_channel_internal_suspend_nolock(struct ast_bridge_channel *bridge_channel)
00706 {
00707    bridge_channel->suspended = 1;
00708    if (bridge_channel->in_bridge) {
00709       --bridge_channel->bridge->num_active;
00710    }
00711 
00712    /* Get technology bridge threads off of the channel. */
00713    if (bridge_channel->bridge->technology->suspend) {
00714       bridge_channel->bridge->technology->suspend(bridge_channel->bridge, bridge_channel);
00715    }
00716 }
00717 
00718 /*!
00719  * \internal
00720  * \brief Suspend a channel from a bridge.
00721  *
00722  * \param bridge_channel Channel to suspend.
00723  *
00724  * \return Nothing
00725  */
00726 static void bridge_channel_suspend(struct ast_bridge_channel *bridge_channel)
00727 {
00728    ast_bridge_channel_lock_bridge(bridge_channel);
00729    bridge_channel_internal_suspend_nolock(bridge_channel);
00730    ast_bridge_unlock(bridge_channel->bridge);
00731 }
00732 
00733 /*!
00734  * \internal
00735  * \brief Unsuspend a channel from a bridge.
00736  *
00737  * \param bridge_channel Channel to unsuspend.
00738  *
00739  * \note This function assumes bridge_channel->bridge is locked.
00740  *
00741  * \return Nothing
00742  */
00743 void bridge_channel_internal_unsuspend_nolock(struct ast_bridge_channel *bridge_channel)
00744 {
00745    bridge_channel->suspended = 0;
00746    if (bridge_channel->in_bridge) {
00747       ++bridge_channel->bridge->num_active;
00748    }
00749 
00750    /* Wake technology bridge threads to take care of channel again. */
00751    if (bridge_channel->bridge->technology->unsuspend) {
00752       bridge_channel->bridge->technology->unsuspend(bridge_channel->bridge, bridge_channel);
00753    }
00754 
00755    /* Wake suspended channel. */
00756    ast_bridge_channel_lock(bridge_channel);
00757    ast_cond_signal(&bridge_channel->cond);
00758    ast_bridge_channel_unlock(bridge_channel);
00759 }
00760 
00761 /*!
00762  * \internal
00763  * \brief Unsuspend a channel from a bridge.
00764  *
00765  * \param bridge_channel Channel to unsuspend.
00766  *
00767  * \return Nothing
00768  */
00769 static void bridge_channel_unsuspend(struct ast_bridge_channel *bridge_channel)
00770 {
00771    ast_bridge_channel_lock_bridge(bridge_channel);
00772    bridge_channel_internal_unsuspend_nolock(bridge_channel);
00773    ast_bridge_unlock(bridge_channel->bridge);
00774 }
00775 
00776 /*!
00777  * \internal
00778  * \brief Queue an action frame onto the bridge channel with data.
00779  * \since 12.0.0
00780  *
00781  * \param bridge_channel Which channel to queue the frame onto.
00782  * \param action Type of bridge action frame.
00783  * \param data Frame payload data to pass.
00784  * \param datalen Frame payload data length to pass.
00785  *
00786  * \retval 0 on success.
00787  * \retval -1 on error.
00788  */
00789 static int bridge_channel_queue_action_data(struct ast_bridge_channel *bridge_channel,
00790    enum bridge_channel_action_type action, const void *data, size_t datalen)
00791 {
00792    struct ast_frame frame = {
00793       .frametype = AST_FRAME_BRIDGE_ACTION,
00794       .subclass.integer = action,
00795       .datalen = datalen,
00796       .data.ptr = (void *) data,
00797    };
00798 
00799    return ast_bridge_channel_queue_frame(bridge_channel, &frame);
00800 }
00801 
00802 /*!
00803  * \internal
00804  * \brief Queue an action frame onto the bridge channel with data synchronously.
00805  * \since 12.2.0
00806  *
00807  * The function will not return until the queued frame is freed.
00808  *
00809  * \param bridge_channel Which channel to queue the frame onto.
00810  * \param action Type of bridge action frame.
00811  * \param data Frame payload data to pass.
00812  * \param datalen Frame payload data length to pass.
00813  *
00814  * \retval 0 on success.
00815  * \retval -1 on error.
00816  */
00817 static int bridge_channel_queue_action_data_sync(struct ast_bridge_channel *bridge_channel,
00818    enum bridge_channel_action_type action, const void *data, size_t datalen)
00819 {
00820    struct sync_payload *sync_payload;
00821    int sync_payload_len = sizeof(*sync_payload) + datalen;
00822    struct bridge_sync sync_struct;
00823    struct ast_frame frame = {
00824       .frametype = AST_FRAME_BRIDGE_ACTION_SYNC,
00825       .subclass.integer = action,
00826    };
00827 
00828    /* Make sure we don't end up trying to wait on ourself to deliver the frame */
00829    ast_assert(!pthread_equal(pthread_self(), bridge_channel->thread));
00830 
00831    sync_payload = ast_alloca(sync_payload_len);
00832    sync_payload->id = ast_atomic_fetchadd_int(&sync_ids, +1);
00833    memcpy(sync_payload->data, data, datalen);
00834 
00835    frame.datalen = sync_payload_len;
00836    frame.data.ptr = sync_payload;
00837 
00838    bridge_sync_init(&sync_struct, sync_payload->id);
00839    if (ast_bridge_channel_queue_frame(bridge_channel, &frame)) {
00840       bridge_sync_cleanup(&sync_struct);
00841       return -1;
00842    }
00843 
00844    bridge_sync_wait(&sync_struct);
00845    bridge_sync_cleanup(&sync_struct);
00846    return 0;
00847 }
00848 /*!
00849  * \internal
00850  * \brief Write an action frame onto the bridge channel with data.
00851  * \since 12.0.0
00852  *
00853  * \param bridge_channel Which channel to queue the frame onto.
00854  * \param action Type of bridge action frame.
00855  * \param data Frame payload data to pass.
00856  * \param datalen Frame payload data length to pass.
00857  *
00858  * \retval 0 on success.
00859  * \retval -1 on error.
00860  */
00861 static int bridge_channel_write_action_data(struct ast_bridge_channel *bridge_channel,
00862    enum bridge_channel_action_type action, const void *data, size_t datalen)
00863 {
00864    struct ast_frame frame = {
00865       .frametype = AST_FRAME_BRIDGE_ACTION,
00866       .subclass.integer = action,
00867       .datalen = datalen,
00868       .data.ptr = (void *) data,
00869    };
00870 
00871    return bridge_channel_write_frame(bridge_channel, &frame);
00872 }
00873 
00874 static void bridge_frame_free(struct ast_frame *frame)
00875 {
00876    if (frame->frametype == AST_FRAME_BRIDGE_ACTION_SYNC) {
00877       struct sync_payload *sync_payload = frame->data.ptr;
00878       struct bridge_sync *sync;
00879 
00880       AST_RWLIST_RDLOCK(&sync_structs);
00881       AST_RWLIST_TRAVERSE(&sync_structs, sync, list) {
00882          if (sync->id == sync_payload->id) {
00883             break;
00884          }
00885       }
00886       if (sync) {
00887          bridge_sync_signal(sync);
00888       }
00889       AST_RWLIST_UNLOCK(&sync_structs);
00890    }
00891 
00892    ast_frfree(frame);
00893 }
00894 
00895 int ast_bridge_channel_queue_frame(struct ast_bridge_channel *bridge_channel, struct ast_frame *fr)
00896 {
00897    struct ast_frame *dup;
00898    char nudge = 0;
00899 
00900    if (bridge_channel->suspended
00901       /* Also defer DTMF frames. */
00902       && fr->frametype != AST_FRAME_DTMF_BEGIN
00903       && fr->frametype != AST_FRAME_DTMF_END
00904       && !ast_is_deferrable_frame(fr)) {
00905       /* Drop non-deferable frames when suspended. */
00906       return 0;
00907    }
00908    if (fr->frametype == AST_FRAME_NULL) {
00909       /* "Accept" the frame and discard it. */
00910       return 0;
00911    }
00912 
00913    dup = ast_frdup(fr);
00914    if (!dup) {
00915       return -1;
00916    }
00917 
00918    ast_bridge_channel_lock(bridge_channel);
00919    if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
00920       /* Drop frames on channels leaving the bridge. */
00921       ast_bridge_channel_unlock(bridge_channel);
00922       bridge_frame_free(dup);
00923       return 0;
00924    }
00925 
00926    AST_LIST_INSERT_TAIL(&bridge_channel->wr_queue, dup, frame_list);
00927    if (write(bridge_channel->alert_pipe[1], &nudge, sizeof(nudge)) != sizeof(nudge)) {
00928       ast_log(LOG_ERROR, "We couldn't write alert pipe for %p(%s)... something is VERY wrong\n",
00929          bridge_channel, ast_channel_name(bridge_channel->chan));
00930    }
00931    ast_bridge_channel_unlock(bridge_channel);
00932    return 0;
00933 }
00934 
00935 int ast_bridge_queue_everyone_else(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
00936 {
00937    struct ast_bridge_channel *cur;
00938    int not_written = -1;
00939 
00940    if (frame->frametype == AST_FRAME_NULL) {
00941       /* "Accept" the frame and discard it. */
00942       return 0;
00943    }
00944 
00945    AST_LIST_TRAVERSE(&bridge->channels, cur, entry) {
00946       if (cur == bridge_channel) {
00947          continue;
00948       }
00949       if (!ast_bridge_channel_queue_frame(cur, frame)) {
00950          not_written = 0;
00951       }
00952    }
00953    return not_written;
00954 }
00955 
00956 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)
00957 {
00958    struct ast_frame frame = {
00959       .frametype = AST_FRAME_CONTROL,
00960       .subclass.integer = control,
00961       .datalen = datalen,
00962       .data.ptr = (void *) data,
00963    };
00964 
00965    return ast_bridge_channel_queue_frame(bridge_channel, &frame);
00966 }
00967 
00968 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)
00969 {
00970    struct ast_frame frame = {
00971       .frametype = AST_FRAME_CONTROL,
00972       .subclass.integer = control,
00973       .datalen = datalen,
00974       .data.ptr = (void *) data,
00975    };
00976 
00977    return bridge_channel_write_frame(bridge_channel, &frame);
00978 }
00979 
00980 int ast_bridge_channel_write_hold(struct ast_bridge_channel *bridge_channel, const char *moh_class)
00981 {
00982    struct ast_json *blob;
00983    int res;
00984    size_t datalen;
00985 
00986    if (!ast_strlen_zero(moh_class)) {
00987       datalen = strlen(moh_class) + 1;
00988 
00989       blob = ast_json_pack("{s: s}",
00990          "musicclass", moh_class);
00991    } else {
00992       moh_class = NULL;
00993       datalen = 0;
00994       blob = NULL;
00995    }
00996 
00997    ast_channel_publish_cached_blob(bridge_channel->chan, ast_channel_hold_type(), blob);
00998 
00999    res = ast_bridge_channel_write_control_data(bridge_channel, AST_CONTROL_HOLD,
01000       moh_class, datalen);
01001 
01002    ast_json_unref(blob);
01003    return res;
01004 }
01005 
01006 int ast_bridge_channel_write_unhold(struct ast_bridge_channel *bridge_channel)
01007 {
01008    ast_channel_publish_cached_blob(bridge_channel->chan, ast_channel_unhold_type(), NULL);
01009 
01010    return ast_bridge_channel_write_control_data(bridge_channel, AST_CONTROL_UNHOLD, NULL, 0);
01011 }
01012 
01013 /*!
01014  * \internal
01015  * \brief Helper function to kick off a PBX app on a bridge_channel
01016  */
01017 static int run_app_helper(struct ast_channel *chan, const char *app_name, const char *app_args)
01018 {
01019    int res = 0;
01020 
01021    if (!strcasecmp("Gosub", app_name)) {
01022       ast_app_exec_sub(NULL, chan, app_args, 0);
01023    } else if (!strcasecmp("Macro", app_name)) {
01024       ast_app_exec_macro(NULL, chan, app_args);
01025    } else {
01026       struct ast_app *app;
01027 
01028       app = pbx_findapp(app_name);
01029       if (!app) {
01030          ast_log(LOG_WARNING, "Could not find application (%s)\n", app_name);
01031       } else {
01032          struct ast_str *substituted_args = ast_str_create(16);
01033 
01034          if (substituted_args) {
01035             ast_str_substitute_variables(&substituted_args, 0, chan, app_args);
01036             res = pbx_exec(chan, app, ast_str_buffer(substituted_args));
01037             ast_free(substituted_args);
01038          } else {
01039             ast_log(LOG_WARNING, "Could not substitute application argument variables for %s\n", app_name);
01040             res = pbx_exec(chan, app, app_args);
01041          }
01042       }
01043    }
01044    return res;
01045 }
01046 
01047 void ast_bridge_channel_run_app(struct ast_bridge_channel *bridge_channel, const char *app_name, const char *app_args, const char *moh_class)
01048 {
01049    if (moh_class) {
01050       ast_bridge_channel_write_hold(bridge_channel, moh_class);
01051    }
01052    if (run_app_helper(bridge_channel->chan, app_name, S_OR(app_args, ""))) {
01053       /* Break the bridge if the app returns non-zero. */
01054       ast_bridge_channel_kick(bridge_channel, AST_CAUSE_NORMAL_CLEARING);
01055    }
01056    if (moh_class) {
01057       ast_bridge_channel_write_unhold(bridge_channel);
01058    }
01059 }
01060 
01061 struct bridge_run_app {
01062    /*! Offset into app_name[] where the MOH class name starts.  (zero if no MOH) */
01063    int moh_offset;
01064    /*! Offset into app_name[] where the application argument string starts. (zero if no arguments) */
01065    int app_args_offset;
01066    /*! Application name to run. */
01067    char app_name[0];
01068 };
01069 
01070 /*!
01071  * \internal
01072  * \brief Handle the run application bridge action.
01073  * \since 12.0.0
01074  *
01075  * \param bridge_channel Which channel to run the application on.
01076  * \param data Action frame data to run the application.
01077  *
01078  * \return Nothing
01079  */
01080 static void bridge_channel_run_app(struct ast_bridge_channel *bridge_channel, struct bridge_run_app *data)
01081 {
01082    ast_bridge_channel_run_app(bridge_channel, data->app_name,
01083       data->app_args_offset ? &data->app_name[data->app_args_offset] : NULL,
01084       data->moh_offset ? &data->app_name[data->moh_offset] : NULL);
01085 }
01086 
01087 /*!
01088  * \internal
01089  * \brief Marshal an application to be executed on a bridge_channel
01090  */
01091 static int payload_helper_app(ast_bridge_channel_post_action_data post_it,
01092    struct ast_bridge_channel *bridge_channel, const char *app_name, const char *app_args, const char *moh_class)
01093 {
01094    struct bridge_run_app *app_data;
01095    size_t len_name = strlen(app_name) + 1;
01096    size_t len_args = ast_strlen_zero(app_args) ? 0 : strlen(app_args) + 1;
01097    size_t len_moh = !moh_class ? 0 : strlen(moh_class) + 1;
01098    size_t len_data = sizeof(*app_data) + len_name + len_args + len_moh;
01099 
01100    /* Fill in application run frame data. */
01101    app_data = alloca(len_data);
01102    app_data->app_args_offset = len_args ? len_name : 0;
01103    app_data->moh_offset = len_moh ? len_name + len_args : 0;
01104    strcpy(app_data->app_name, app_name);/* Safe */
01105    if (len_args) {
01106       strcpy(&app_data->app_name[app_data->app_args_offset], app_args);/* Safe */
01107    }
01108    if (moh_class) {
01109       strcpy(&app_data->app_name[app_data->moh_offset], moh_class);/* Safe */
01110    }
01111 
01112    return post_it(bridge_channel, BRIDGE_CHANNEL_ACTION_RUN_APP, app_data, len_data);
01113 }
01114 
01115 int ast_bridge_channel_write_app(struct ast_bridge_channel *bridge_channel, const char *app_name, const char *app_args, const char *moh_class)
01116 {
01117    return payload_helper_app(bridge_channel_write_action_data,
01118       bridge_channel, app_name, app_args, moh_class);
01119 }
01120 
01121 int ast_bridge_channel_queue_app(struct ast_bridge_channel *bridge_channel, const char *app_name, const char *app_args, const char *moh_class)
01122 {
01123    return payload_helper_app(bridge_channel_queue_action_data,
01124       bridge_channel, app_name, app_args, moh_class);
01125 }
01126 
01127 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)
01128 {
01129    if (moh_class) {
01130       ast_bridge_channel_write_hold(bridge_channel, moh_class);
01131    }
01132    if (custom_play) {
01133       custom_play(bridge_channel, playfile);
01134    } else {
01135       ast_stream_and_wait(bridge_channel->chan, playfile, AST_DIGIT_NONE);
01136    }
01137    if (moh_class) {
01138       ast_bridge_channel_write_unhold(bridge_channel);
01139    }
01140 
01141    /*
01142     * It may be necessary to resume music on hold after we finish
01143     * playing the announcment.
01144     */
01145    if (ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_MOH)) {
01146       const char *latest_musicclass;
01147 
01148       ast_channel_lock(bridge_channel->chan);
01149       latest_musicclass = ast_strdupa(ast_channel_latest_musicclass(bridge_channel->chan));
01150       ast_channel_unlock(bridge_channel->chan);
01151       ast_moh_start(bridge_channel->chan, latest_musicclass, NULL);
01152    }
01153 }
01154 
01155 struct bridge_playfile {
01156    /*! Call this function to play the playfile. (NULL if normal sound file to play) */
01157    ast_bridge_custom_play_fn custom_play;
01158    /*! Offset into playfile[] where the MOH class name starts.  (zero if no MOH)*/
01159    int moh_offset;
01160    /*! Filename to play. */
01161    char playfile[0];
01162 };
01163 
01164 /*!
01165  * \internal
01166  * \brief Handle the playfile bridge action.
01167  * \since 12.0.0
01168  *
01169  * \param bridge_channel Which channel to play a file on.
01170  * \param payload Action frame payload to play a file.
01171  *
01172  * \return Nothing
01173  */
01174 static void bridge_channel_playfile(struct ast_bridge_channel *bridge_channel, struct bridge_playfile *payload)
01175 {
01176    ast_bridge_channel_playfile(bridge_channel, payload->custom_play, payload->playfile,
01177       payload->moh_offset ? &payload->playfile[payload->moh_offset] : NULL);
01178 }
01179 
01180 /*!
01181  * \internal
01182  * \brief Marshal a file to be played on a bridge_channel
01183  */
01184 static int payload_helper_playfile(ast_bridge_channel_post_action_data post_it,
01185    struct ast_bridge_channel *bridge_channel, ast_bridge_custom_play_fn custom_play, const char *playfile, const char *moh_class)
01186 {
01187    struct bridge_playfile *payload;
01188    size_t len_name = strlen(playfile) + 1;
01189    size_t len_moh = !moh_class ? 0 : strlen(moh_class) + 1;
01190    size_t len_payload = sizeof(*payload) + len_name + len_moh;
01191 
01192    /* Fill in play file frame data. */
01193    payload = ast_alloca(len_payload);
01194    payload->custom_play = custom_play;
01195    payload->moh_offset = len_moh ? len_name : 0;
01196    strcpy(payload->playfile, playfile);/* Safe */
01197    if (moh_class) {
01198       strcpy(&payload->playfile[payload->moh_offset], moh_class);/* Safe */
01199    }
01200 
01201    return post_it(bridge_channel, BRIDGE_CHANNEL_ACTION_PLAY_FILE, payload, len_payload);
01202 }
01203 
01204 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)
01205 {
01206    return payload_helper_playfile(bridge_channel_write_action_data,
01207       bridge_channel, custom_play, playfile, moh_class);
01208 }
01209 
01210 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)
01211 {
01212    return payload_helper_playfile(bridge_channel_queue_action_data,
01213       bridge_channel, custom_play, playfile, moh_class);
01214 }
01215 
01216 int ast_bridge_channel_queue_playfile_sync(struct ast_bridge_channel *bridge_channel,
01217       ast_bridge_custom_play_fn custom_play, const char *playfile, const char *moh_class)
01218 {
01219    return payload_helper_playfile(bridge_channel_queue_action_data_sync,
01220       bridge_channel, custom_play, playfile, moh_class);
01221 }
01222 
01223 struct bridge_custom_callback {
01224    /*! Call this function on the bridge channel thread. */
01225    ast_bridge_custom_callback_fn callback;
01226    /*! Size of the payload if it exists.  A number otherwise. */
01227    size_t payload_size;
01228    /*! Option flags determining how callback is called. */
01229    unsigned int flags;
01230    /*! Nonzero if the payload exists. */
01231    char payload_exists;
01232    /*! Payload to give to callback. */
01233    char payload[0];
01234 };
01235 
01236 /*!
01237  * \internal
01238  * \brief Handle the do custom callback bridge action.
01239  * \since 12.0.0
01240  *
01241  * \param bridge_channel Which channel to call the callback on.
01242  * \param data Action frame data to call the callback.
01243  *
01244  * \return Nothing
01245  */
01246 static void bridge_channel_do_callback(struct ast_bridge_channel *bridge_channel, struct bridge_custom_callback *data)
01247 {
01248    if (ast_test_flag(data, AST_BRIDGE_CHANNEL_CB_OPTION_MEDIA)) {
01249       bridge_channel_suspend(bridge_channel);
01250       ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
01251    }
01252    data->callback(bridge_channel, data->payload_exists ? data->payload : NULL, data->payload_size);
01253    if (ast_test_flag(data, AST_BRIDGE_CHANNEL_CB_OPTION_MEDIA)) {
01254       ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
01255       bridge_channel_unsuspend(bridge_channel);
01256    }
01257 }
01258 
01259 /*!
01260  * \internal
01261  * \brief Marshal a custom callback function to be called on a bridge_channel
01262  */
01263 static int payload_helper_cb(ast_bridge_channel_post_action_data post_it,
01264    struct ast_bridge_channel *bridge_channel,
01265    enum ast_bridge_channel_custom_callback_option flags,
01266    ast_bridge_custom_callback_fn callback, const void *payload, size_t payload_size)
01267 {
01268    struct bridge_custom_callback *cb_data;
01269    size_t len_data = sizeof(*cb_data) + (payload ? payload_size : 0);
01270 
01271    /* Sanity check. */
01272    if (!callback) {
01273       ast_assert(0);
01274       return -1;
01275    }
01276 
01277    /* Fill in custom callback frame data. */
01278    cb_data = alloca(len_data);
01279    cb_data->callback = callback;
01280    cb_data->payload_size = payload_size;
01281    cb_data->flags = flags;
01282    cb_data->payload_exists = payload && payload_size;
01283    if (cb_data->payload_exists) {
01284       memcpy(cb_data->payload, payload, payload_size);/* Safe */
01285    }
01286 
01287    return post_it(bridge_channel, BRIDGE_CHANNEL_ACTION_CALLBACK, cb_data, len_data);
01288 }
01289 
01290 int ast_bridge_channel_write_callback(struct ast_bridge_channel *bridge_channel,
01291    enum ast_bridge_channel_custom_callback_option flags,
01292    ast_bridge_custom_callback_fn callback, const void *payload, size_t payload_size)
01293 {
01294    return payload_helper_cb(bridge_channel_write_action_data,
01295       bridge_channel, flags, callback, payload, payload_size);
01296 }
01297 
01298 int ast_bridge_channel_queue_callback(struct ast_bridge_channel *bridge_channel,
01299    enum ast_bridge_channel_custom_callback_option flags,
01300    ast_bridge_custom_callback_fn callback, const void *payload, size_t payload_size)
01301 {
01302    return payload_helper_cb(bridge_channel_queue_action_data,
01303       bridge_channel, flags, callback, payload, payload_size);
01304 }
01305 
01306 struct bridge_park {
01307    int parker_uuid_offset;
01308    int app_data_offset;
01309    /* buffer used for holding those strings */
01310    char parkee_uuid[0];
01311 };
01312 
01313 /*!
01314  * \internal
01315  * \brief Park a bridge_cahnnel
01316  */
01317 static void bridge_channel_park(struct ast_bridge_channel *bridge_channel, struct bridge_park *payload)
01318 {
01319    if (!ast_parking_provider_registered()) {
01320       ast_log(AST_LOG_WARNING, "Unable to park %s: No parking provider loaded!\n",
01321          ast_channel_name(bridge_channel->chan));
01322       return;
01323    }
01324 
01325    if (ast_parking_park_bridge_channel(bridge_channel, payload->parkee_uuid,
01326       &payload->parkee_uuid[payload->parker_uuid_offset],
01327       payload->app_data_offset ? &payload->parkee_uuid[payload->app_data_offset] : NULL)) {
01328       ast_log(AST_LOG_WARNING, "Error occurred while parking %s\n",
01329          ast_channel_name(bridge_channel->chan));
01330    }
01331 }
01332 
01333 /*!
01334  * \internal
01335  * \brief Marshal a park action onto a bridge_channel
01336  */
01337 static int payload_helper_park(ast_bridge_channel_post_action_data post_it,
01338    struct ast_bridge_channel *bridge_channel,
01339    const char *parkee_uuid,
01340    const char *parker_uuid,
01341    const char *app_data)
01342 {
01343    struct bridge_park *payload;
01344    size_t len_parkee_uuid = strlen(parkee_uuid) + 1;
01345    size_t len_parker_uuid = strlen(parker_uuid) + 1;
01346    size_t len_app_data = !app_data ? 0 : strlen(app_data) + 1;
01347    size_t len_payload = sizeof(*payload) + len_parker_uuid + len_parkee_uuid + len_app_data;
01348 
01349    payload = alloca(len_payload);
01350    payload->app_data_offset = len_app_data ? len_parkee_uuid + len_parker_uuid : 0;
01351    payload->parker_uuid_offset = len_parkee_uuid;
01352    strcpy(payload->parkee_uuid, parkee_uuid);
01353    strcpy(&payload->parkee_uuid[payload->parker_uuid_offset], parker_uuid);
01354    if (app_data) {
01355       strcpy(&payload->parkee_uuid[payload->app_data_offset], app_data);
01356    }
01357 
01358    return post_it(bridge_channel, BRIDGE_CHANNEL_ACTION_PARK, payload, len_payload);
01359 }
01360 
01361 int ast_bridge_channel_write_park(struct ast_bridge_channel *bridge_channel, const char *parkee_uuid, const char *parker_uuid, const char *app_data)
01362 {
01363    return payload_helper_park(bridge_channel_write_action_data,
01364       bridge_channel, parkee_uuid, parker_uuid, app_data);
01365 }
01366 
01367 /*!
01368  * \internal
01369  * \brief Handle bridge channel interval expiration.
01370  * \since 12.0.0
01371  *
01372  * \param bridge_channel Channel to run expired intervals on.
01373  *
01374  * \return Nothing
01375  */
01376 static void bridge_channel_handle_interval(struct ast_bridge_channel *bridge_channel)
01377 {
01378    struct ast_heap *interval_hooks;
01379    struct ast_bridge_hook_timer *hook;
01380    struct timeval start;
01381    int chan_suspended = 0;
01382 
01383    interval_hooks = bridge_channel->features->interval_hooks;
01384    ast_heap_wrlock(interval_hooks);
01385    start = ast_tvnow();
01386    while ((hook = ast_heap_peek(interval_hooks, 1))) {
01387       int interval;
01388       unsigned int execution_time;
01389 
01390       if (ast_tvdiff_ms(hook->timer.trip_time, start) > 0) {
01391          ast_debug(1, "Hook %p on %p(%s) wants to happen in the future, stopping our traversal\n",
01392             hook, bridge_channel, ast_channel_name(bridge_channel->chan));
01393          break;
01394       }
01395       ao2_ref(hook, +1);
01396       ast_heap_unlock(interval_hooks);
01397 
01398       if (!chan_suspended
01399          && ast_test_flag(&hook->timer, AST_BRIDGE_HOOK_TIMER_OPTION_MEDIA)) {
01400          chan_suspended = 1;
01401          bridge_channel_suspend(bridge_channel);
01402          ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
01403       }
01404 
01405       ast_debug(1, "Executing hook %p on %p(%s)\n",
01406          hook, bridge_channel, ast_channel_name(bridge_channel->chan));
01407       interval = hook->generic.callback(bridge_channel, hook->generic.hook_pvt);
01408 
01409       ast_heap_wrlock(interval_hooks);
01410       if (ast_heap_peek(interval_hooks, hook->timer.heap_index) != hook
01411          || !ast_heap_remove(interval_hooks, hook)) {
01412          /* Interval hook is already removed from the bridge_channel. */
01413          ao2_ref(hook, -1);
01414          continue;
01415       }
01416       ao2_ref(hook, -1);
01417 
01418       if (interval < 0) {
01419          ast_debug(1, "Removed interval hook %p from %p(%s)\n",
01420             hook, bridge_channel, ast_channel_name(bridge_channel->chan));
01421          ao2_ref(hook, -1);
01422          continue;
01423       }
01424       if (interval) {
01425          /* Set new interval for the hook. */
01426          hook->timer.interval = interval;
01427       }
01428 
01429       ast_debug(1, "Updating interval hook %p with interval %u on %p(%s)\n",
01430          hook, hook->timer.interval, bridge_channel,
01431          ast_channel_name(bridge_channel->chan));
01432 
01433       /* resetting start */
01434       start = ast_tvnow();
01435 
01436       /*
01437        * Resetup the interval hook for the next interval.  We may need
01438        * to skip over any missed intervals because the hook was
01439        * delayed or took too long.
01440        */
01441       execution_time = ast_tvdiff_ms(start, hook->timer.trip_time);
01442       while (hook->timer.interval < execution_time) {
01443          execution_time -= hook->timer.interval;
01444       }
01445       hook->timer.trip_time = ast_tvadd(start, ast_samp2tv(hook->timer.interval - execution_time, 1000));
01446       hook->timer.seqno = ast_atomic_fetchadd_int((int *) &bridge_channel->features->interval_sequence, +1);
01447 
01448       if (ast_heap_push(interval_hooks, hook)) {
01449          /* Could not push the hook back onto the heap. */
01450          ao2_ref(hook, -1);
01451       }
01452    }
01453    ast_heap_unlock(interval_hooks);
01454 
01455    if (chan_suspended) {
01456       ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
01457       bridge_channel_unsuspend(bridge_channel);
01458    }
01459 }
01460 
01461 /*!
01462  * \internal
01463  * \brief Write a DTMF stream out to a channel
01464  */
01465 static int bridge_channel_write_dtmf_stream(struct ast_bridge_channel *bridge_channel, const char *dtmf)
01466 {
01467    return bridge_channel_write_action_data(bridge_channel,
01468       BRIDGE_CHANNEL_ACTION_DTMF_STREAM, dtmf, strlen(dtmf) + 1);
01469 }
01470 
01471 /*!
01472  * \internal
01473  * \brief Indicate to the testsuite a feature was successfully detected.
01474  *
01475  * Currently, this function only will relay built-in features to the testsuite,
01476  * but it could be modified to detect applicationmap items should the need arise.
01477  *
01478  * \param chan The channel that activated the feature
01479  * \param dtmf The DTMF sequence entered to activate the feature
01480  */
01481 static void testsuite_notify_feature_success(struct ast_channel *chan, const char *dtmf)
01482 {
01483 #ifdef TEST_FRAMEWORK
01484    char *feature = "unknown";
01485    struct ast_featuremap_config *featuremap = ast_get_chan_featuremap_config(chan);
01486    struct ast_features_xfer_config *xfer = ast_get_chan_features_xfer_config(chan);
01487 
01488    if (featuremap) {
01489       if (!strcmp(dtmf, featuremap->blindxfer)) {
01490          feature = "blindxfer";
01491       } else if (!strcmp(dtmf, featuremap->atxfer)) {
01492          feature = "atxfer";
01493       } else if (!strcmp(dtmf, featuremap->disconnect)) {
01494          feature = "disconnect";
01495       } else if (!strcmp(dtmf, featuremap->automon)) {
01496          feature = "automon";
01497       } else if (!strcmp(dtmf, featuremap->automixmon)) {
01498          feature = "automixmon";
01499       } else if (!strcmp(dtmf, featuremap->parkcall)) {
01500          feature = "parkcall";
01501       }
01502    }
01503    if (xfer) {
01504       if (!strcmp(dtmf, xfer->atxferthreeway)) {
01505          feature = "atxferthreeway";
01506       }
01507    }
01508 
01509    ao2_cleanup(featuremap);
01510    ao2_cleanup(xfer);
01511 
01512    ast_test_suite_event_notify("FEATURE_DETECTION",
01513          "Result: success\r\n"
01514          "Feature: %s", feature);
01515 #endif /* TEST_FRAMEWORK */
01516 }
01517 
01518 static int bridge_channel_feature_digit_add(
01519    struct ast_bridge_channel *bridge_channel, int digit, size_t dtmf_len)
01520 {
01521    if (dtmf_len < ARRAY_LEN(bridge_channel->dtmf_hook_state.collected) - 1) {
01522       /* Add the new digit to the DTMF string so we can do our matching */
01523       bridge_channel->dtmf_hook_state.collected[dtmf_len++] = digit;
01524       bridge_channel->dtmf_hook_state.collected[dtmf_len] = '\0';
01525 
01526       ast_debug(1, "DTMF feature string on %p(%s) is now '%s'\n",
01527            bridge_channel, ast_channel_name(bridge_channel->chan),
01528            bridge_channel->dtmf_hook_state.collected);
01529    }
01530 
01531    return dtmf_len;
01532 }
01533 
01534 static unsigned int bridge_channel_feature_digit_timeout(struct ast_bridge_channel *bridge_channel)
01535 {
01536    unsigned int digit_timeout;
01537    struct ast_features_general_config *gen_cfg;
01538 
01539    /* Determine interdigit timeout */
01540    ast_channel_lock(bridge_channel->chan);
01541    gen_cfg = ast_get_chan_features_general_config(bridge_channel->chan);
01542    ast_channel_unlock(bridge_channel->chan);
01543 
01544    if (!gen_cfg) {
01545       ast_log(LOG_ERROR, "Unable to retrieve features configuration.\n");
01546       return 3000; /* Pick a reasonable failsafe timeout in ms */
01547    }
01548 
01549    digit_timeout = gen_cfg->featuredigittimeout;
01550    ao2_ref(gen_cfg, -1);
01551 
01552    return digit_timeout;
01553 }
01554 
01555 void ast_bridge_channel_feature_digit_add(struct ast_bridge_channel *bridge_channel, int digit)
01556 {
01557    if (digit) {
01558       bridge_channel_feature_digit_add(
01559          bridge_channel, digit, strlen(bridge_channel->dtmf_hook_state.collected));
01560    }
01561 }
01562 
01563 void ast_bridge_channel_feature_digit(struct ast_bridge_channel *bridge_channel, int digit)
01564 {
01565    struct ast_bridge_features *features = bridge_channel->features;
01566    struct ast_bridge_hook_dtmf *hook = NULL;
01567    size_t dtmf_len;
01568 
01569    struct sanity_check_of_dtmf_size {
01570       char check[1 / (ARRAY_LEN(bridge_channel->dtmf_hook_state.collected) == ARRAY_LEN(hook->dtmf.code))];
01571    };
01572 
01573    dtmf_len = strlen(bridge_channel->dtmf_hook_state.collected);
01574    if (!dtmf_len && !digit) {
01575       /* Nothing to do */
01576       return;
01577    }
01578 
01579    if (digit) {
01580       dtmf_len = bridge_channel_feature_digit_add(bridge_channel, digit, dtmf_len);
01581    }
01582 
01583    while (digit) {
01584       /* See if a DTMF feature hook matches or can match */
01585       hook = ao2_find(features->dtmf_hooks, bridge_channel->dtmf_hook_state.collected,
01586          OBJ_SEARCH_PARTIAL_KEY);
01587       if (!hook) {
01588          ast_debug(1, "No DTMF feature hooks on %p(%s) match '%s'\n",
01589             bridge_channel, ast_channel_name(bridge_channel->chan),
01590             bridge_channel->dtmf_hook_state.collected);
01591          break;
01592       } else if (dtmf_len != strlen(hook->dtmf.code)) {
01593          unsigned int digit_timeout;
01594          /* Need more digits to match */
01595          ao2_ref(hook, -1);
01596          digit_timeout = bridge_channel_feature_digit_timeout(bridge_channel);
01597          bridge_channel->dtmf_hook_state.interdigit_timeout =
01598             ast_tvadd(ast_tvnow(), ast_samp2tv(digit_timeout, 1000));
01599          return;
01600       } else {
01601          int remove_me;
01602          int already_suspended;
01603 
01604          ast_debug(1, "DTMF feature hook %p matched DTMF string '%s' on %p(%s)\n",
01605             hook, bridge_channel->dtmf_hook_state.collected, bridge_channel,
01606             ast_channel_name(bridge_channel->chan));
01607 
01608          /*
01609           * Clear the collected digits before executing the hook
01610           * in case the hook starts another sequence.
01611           */
01612          bridge_channel->dtmf_hook_state.collected[0] = '\0';
01613 
01614          ast_bridge_channel_lock_bridge(bridge_channel);
01615          already_suspended = bridge_channel->suspended;
01616          if (!already_suspended) {
01617             bridge_channel_internal_suspend_nolock(bridge_channel);
01618          }
01619          ast_bridge_unlock(bridge_channel->bridge);
01620          ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
01621 
01622          /* Execute the matched hook on this channel. */
01623          remove_me = hook->generic.callback(bridge_channel, hook->generic.hook_pvt);
01624          if (remove_me) {
01625             ast_debug(1, "DTMF hook %p is being removed from %p(%s)\n",
01626                hook, bridge_channel, ast_channel_name(bridge_channel->chan));
01627             ao2_unlink(features->dtmf_hooks, hook);
01628          }
01629          testsuite_notify_feature_success(bridge_channel->chan, hook->dtmf.code);
01630          ao2_ref(hook, -1);
01631 
01632          ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
01633          if (!already_suspended) {
01634             bridge_channel_unsuspend(bridge_channel);
01635          }
01636 
01637          /*
01638           * If we are handing the channel off to an external hook for
01639           * ownership, we are not guaranteed what kind of state it will
01640           * come back in.  If the channel hungup, we need to detect that
01641           * here if the hook did not already change the state.
01642           */
01643          if (bridge_channel->chan && ast_check_hangup_locked(bridge_channel->chan)) {
01644             ast_bridge_channel_kick(bridge_channel, 0);
01645             bridge_channel->dtmf_hook_state.collected[0] = '\0';
01646             return;
01647          }
01648 
01649          /* if there is dtmf that has been collected then loop back through,
01650             but set digit to -1 so it doesn't try to do an add since the dtmf
01651             is already in the buffer */
01652          dtmf_len = strlen(bridge_channel->dtmf_hook_state.collected);
01653          if (!dtmf_len) {
01654             return;
01655          }
01656       }
01657    }
01658 
01659    if (!digit) {
01660       ast_debug(1, "DTMF feature string collection on %p(%s) timed out\n",
01661          bridge_channel, ast_channel_name(bridge_channel->chan));
01662    }
01663 
01664    /* Timeout or DTMF digit didn't allow a match with any hooks. */
01665    if (features->dtmf_passthrough) {
01666       /* Stream the collected DTMF to the other channels. */
01667       bridge_channel_write_dtmf_stream(bridge_channel,
01668          bridge_channel->dtmf_hook_state.collected);
01669    }
01670    bridge_channel->dtmf_hook_state.collected[0] = '\0';
01671 
01672    ast_test_suite_event_notify("FEATURE_DETECTION", "Result: fail");
01673 }
01674 
01675 /*!
01676  * \internal
01677  * \brief Handle bridge channel DTMF feature timeout expiration.
01678  * \since 12.8.0
01679  *
01680  * \param bridge_channel Channel to check expired interdigit timer on.
01681  *
01682  * \return Nothing
01683  */
01684 static void bridge_channel_handle_feature_timeout(struct ast_bridge_channel *bridge_channel)
01685 {
01686    if (!bridge_channel->dtmf_hook_state.collected[0]
01687       || 0 < ast_tvdiff_ms(bridge_channel->dtmf_hook_state.interdigit_timeout,
01688          ast_tvnow())) {
01689       /* Not within a sequence or not timed out. */
01690       return;
01691    }
01692 
01693    ast_bridge_channel_feature_digit(bridge_channel, 0);
01694 }
01695 
01696 /*!
01697  * \internal
01698  * \brief Indicate that a bridge_channel is talking
01699  */
01700 static void bridge_channel_talking(struct ast_bridge_channel *bridge_channel, int talking)
01701 {
01702    struct ast_bridge_features *features = bridge_channel->features;
01703    struct ast_bridge_hook *hook;
01704    struct ao2_iterator iter;
01705 
01706    /* Run any talk detection hooks. */
01707    iter = ao2_iterator_init(features->other_hooks, 0);
01708    for (; (hook = ao2_iterator_next(&iter)); ao2_ref(hook, -1)) {
01709       int remove_me;
01710       ast_bridge_talking_indicate_callback talk_cb;
01711 
01712       if (hook->type != AST_BRIDGE_HOOK_TYPE_TALK) {
01713          continue;
01714       }
01715       talk_cb = (ast_bridge_talking_indicate_callback) hook->callback;
01716       remove_me = talk_cb(bridge_channel, hook->hook_pvt, talking);
01717       if (remove_me) {
01718          ast_debug(1, "Talk detection hook %p is being removed from %p(%s)\n",
01719             hook, bridge_channel, ast_channel_name(bridge_channel->chan));
01720          ao2_unlink(features->other_hooks, hook);
01721       }
01722    }
01723    ao2_iterator_destroy(&iter);
01724 }
01725 
01726 /*! \brief Internal function that plays back DTMF on a bridge channel */
01727 static void bridge_channel_dtmf_stream(struct ast_bridge_channel *bridge_channel, const char *dtmf)
01728 {
01729    ast_debug(1, "Playing DTMF stream '%s' out to %p(%s)\n",
01730       dtmf, bridge_channel, ast_channel_name(bridge_channel->chan));
01731    ast_dtmf_stream(bridge_channel->chan, NULL, dtmf, 0, 0);
01732 }
01733 
01734 /*! \brief Data specifying where a blind transfer is going to */
01735 struct blind_transfer_data {
01736    char exten[AST_MAX_EXTENSION];
01737    char context[AST_MAX_CONTEXT];
01738 };
01739 
01740 /*!
01741  * \internal
01742  * \brief Execute after bridge actions on a channel when it leaves a bridge
01743  */
01744 static void after_bridge_move_channel(struct ast_channel *chan_bridged, void *data)
01745 {
01746    RAII_VAR(struct ast_channel *, chan_target, data, ao2_cleanup);
01747    struct ast_party_connected_line connected_target;
01748    unsigned char connected_line_data[1024];
01749    int payload_size;
01750 
01751    ast_party_connected_line_init(&connected_target);
01752 
01753    ast_channel_lock(chan_target);
01754    ast_party_connected_line_copy(&connected_target, ast_channel_connected(chan_target));
01755    ast_channel_unlock(chan_target);
01756    ast_party_id_reset(&connected_target.priv);
01757 
01758    if (ast_channel_move(chan_target, chan_bridged)) {
01759       ast_softhangup(chan_target, AST_SOFTHANGUP_DEV);
01760       ast_party_connected_line_free(&connected_target);
01761       return;
01762    }
01763 
01764    if ((payload_size = ast_connected_line_build_data(connected_line_data,
01765       sizeof(connected_line_data), &connected_target, NULL)) != -1) {
01766       struct ast_control_read_action_payload *frame_payload;
01767       int frame_size;
01768 
01769       frame_size = payload_size + sizeof(*frame_payload);
01770       frame_payload = ast_alloca(frame_size);
01771       frame_payload->action = AST_FRAME_READ_ACTION_CONNECTED_LINE_MACRO;
01772       frame_payload->payload_size = payload_size;
01773       memcpy(frame_payload->payload, connected_line_data, payload_size);
01774       ast_queue_control_data(chan_target, AST_CONTROL_READ_ACTION, frame_payload, frame_size);
01775    }
01776 
01777    ast_party_connected_line_free(&connected_target);
01778 }
01779 
01780 /*!
01781  * \internal
01782  * \brief Execute logic to cleanup when after bridge fails
01783  */
01784 static void after_bridge_move_channel_fail(enum ast_bridge_after_cb_reason reason, void *data)
01785 {
01786    RAII_VAR(struct ast_channel *, chan_target, data, ao2_cleanup);
01787 
01788    ast_log(LOG_WARNING, "Unable to complete transfer: %s\n",
01789       ast_bridge_after_cb_reason_string(reason));
01790    ast_softhangup(chan_target, AST_SOFTHANGUP_DEV);
01791 }
01792 
01793 /*!
01794  * \internal
01795  * \brief Perform a blind transfer on a channel in a bridge
01796  */
01797 static void bridge_channel_blind_transfer(struct ast_bridge_channel *bridge_channel,
01798       struct blind_transfer_data *blind_data)
01799 {
01800    ast_async_goto(bridge_channel->chan, blind_data->context, blind_data->exten, 1);
01801    ast_bridge_channel_kick(bridge_channel, AST_CAUSE_NORMAL_CLEARING);
01802 }
01803 
01804 /*!
01805  * \internal
01806  * \brief Perform an attended transfer on a channel in a bridge
01807  */
01808 static void bridge_channel_attended_transfer(struct ast_bridge_channel *bridge_channel,
01809       const char *target_chan_name)
01810 {
01811    RAII_VAR(struct ast_channel *, chan_target, NULL, ao2_cleanup);
01812    RAII_VAR(struct ast_channel *, chan_bridged, NULL, ao2_cleanup);
01813 
01814    chan_target = ast_channel_get_by_name(target_chan_name);
01815    if (!chan_target) {
01816       /* Dang, it disappeared somehow */
01817       ast_bridge_channel_kick(bridge_channel, AST_CAUSE_NORMAL_CLEARING);
01818       return;
01819    }
01820 
01821    ast_bridge_channel_lock(bridge_channel);
01822    chan_bridged = bridge_channel->chan;
01823    ast_assert(chan_bridged != NULL);
01824    ao2_ref(chan_bridged, +1);
01825    ast_bridge_channel_unlock(bridge_channel);
01826 
01827    if (ast_bridge_set_after_callback(chan_bridged, after_bridge_move_channel,
01828       after_bridge_move_channel_fail, ast_channel_ref(chan_target))) {
01829       ast_softhangup(chan_target, AST_SOFTHANGUP_DEV);
01830 
01831       /* Release the ref we tried to pass to ast_bridge_set_after_callback(). */
01832       ast_channel_unref(chan_target);
01833    }
01834    ast_bridge_channel_kick(bridge_channel, AST_CAUSE_NORMAL_CLEARING);
01835 }
01836 
01837 /*!
01838  * \internal
01839  * \brief Handle bridge channel bridge action frame.
01840  * \since 12.0.0
01841  *
01842  * \param bridge_channel Channel to execute the action on.
01843  * \param action What to do.
01844  * \param data data from the action.
01845  *
01846  * \return Nothing
01847  */
01848 static void bridge_channel_handle_action(struct ast_bridge_channel *bridge_channel,
01849    enum bridge_channel_action_type action, void *data)
01850 {
01851    switch (action) {
01852    case BRIDGE_CHANNEL_ACTION_DTMF_STREAM:
01853       bridge_channel_suspend(bridge_channel);
01854       ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
01855       bridge_channel_dtmf_stream(bridge_channel, data);
01856       ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
01857       bridge_channel_unsuspend(bridge_channel);
01858       break;
01859    case BRIDGE_CHANNEL_ACTION_TALKING_START:
01860    case BRIDGE_CHANNEL_ACTION_TALKING_STOP:
01861       bridge_channel_talking(bridge_channel,
01862          action == BRIDGE_CHANNEL_ACTION_TALKING_START);
01863       break;
01864    case BRIDGE_CHANNEL_ACTION_PLAY_FILE:
01865       bridge_channel_suspend(bridge_channel);
01866       ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
01867       bridge_channel_playfile(bridge_channel, data);
01868       ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
01869       bridge_channel_unsuspend(bridge_channel);
01870       break;
01871    case BRIDGE_CHANNEL_ACTION_RUN_APP:
01872       bridge_channel_suspend(bridge_channel);
01873       ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
01874       bridge_channel_run_app(bridge_channel, data);
01875       ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
01876       bridge_channel_unsuspend(bridge_channel);
01877       break;
01878    case BRIDGE_CHANNEL_ACTION_CALLBACK:
01879       bridge_channel_do_callback(bridge_channel, data);
01880       break;
01881    case BRIDGE_CHANNEL_ACTION_PARK:
01882       bridge_channel_suspend(bridge_channel);
01883       ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
01884       bridge_channel_park(bridge_channel, data);
01885       ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
01886       bridge_channel_unsuspend(bridge_channel);
01887       break;
01888    case BRIDGE_CHANNEL_ACTION_BLIND_TRANSFER:
01889       bridge_channel_blind_transfer(bridge_channel, data);
01890       break;
01891    case BRIDGE_CHANNEL_ACTION_ATTENDED_TRANSFER:
01892       bridge_channel_attended_transfer(bridge_channel, data);
01893       break;
01894    default:
01895       break;
01896    }
01897 }
01898 
01899 /*!
01900  * \internal
01901  * \brief Check if a bridge should dissolve and do it.
01902  * \since 12.0.0
01903  *
01904  * \param bridge_channel Channel causing the check.
01905  *
01906  * \note On entry, bridge_channel->bridge is already locked.
01907  *
01908  * \return Nothing
01909  */
01910 static void bridge_channel_dissolve_check(struct ast_bridge_channel *bridge_channel)
01911 {
01912    struct ast_bridge *bridge = bridge_channel->bridge;
01913 
01914    if (bridge->dissolved) {
01915       return;
01916    }
01917 
01918    if (!bridge->num_channels
01919       && ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_DISSOLVE_EMPTY)) {
01920       /* Last channel leaving the bridge turns off the lights. */
01921       bridge_dissolve(bridge, ast_channel_hangupcause(bridge_channel->chan));
01922       return;
01923    }
01924 
01925    switch (bridge_channel->state) {
01926    case BRIDGE_CHANNEL_STATE_END:
01927       /* Do we need to dissolve the bridge because this channel hung up? */
01928       if (ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_DISSOLVE_HANGUP)
01929          || (bridge_channel->features->usable
01930             && ast_test_flag(&bridge_channel->features->feature_flags,
01931                AST_BRIDGE_CHANNEL_FLAG_DISSOLVE_HANGUP))) {
01932          bridge_dissolve(bridge, ast_channel_hangupcause(bridge_channel->chan));
01933          return;
01934       }
01935       break;
01936    default:
01937       break;
01938    }
01939 
01940    if (bridge->num_lonely && bridge->num_lonely == bridge->num_channels) {
01941       /*
01942        * This will start a chain reaction where each channel leaving
01943        * enters this function and causes the next to leave as long as
01944        * there aren't non-lonely channels in the bridge.
01945        */
01946       ast_bridge_channel_leave_bridge(AST_LIST_FIRST(&bridge->channels),
01947          BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE,
01948          ast_channel_hangupcause(bridge_channel->chan));
01949    }
01950 }
01951 
01952 void bridge_channel_internal_pull(struct ast_bridge_channel *bridge_channel)
01953 {
01954    struct ast_bridge *bridge = bridge_channel->bridge;
01955 
01956    if (!bridge_channel->in_bridge) {
01957       return;
01958    }
01959    bridge_channel->in_bridge = 0;
01960 
01961    ast_debug(1, "Bridge %s: pulling %p(%s)\n",
01962       bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan));
01963 
01964    ast_verb(3, "Channel %s left '%s' %s-bridge <%s>\n",
01965       ast_channel_name(bridge_channel->chan),
01966       bridge->technology->name,
01967       bridge->v_table->name,
01968       bridge->uniqueid);
01969 
01970    if (!bridge_channel->just_joined) {
01971       /* Tell the bridge technology we are leaving so they tear us down */
01972       ast_debug(1, "Bridge %s: %p(%s) is leaving %s technology\n",
01973          bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan),
01974          bridge->technology->name);
01975       if (bridge->technology->leave) {
01976          bridge->technology->leave(bridge, bridge_channel);
01977       }
01978    }
01979 
01980    /* Remove channel from the bridge */
01981    if (!bridge_channel->suspended) {
01982       --bridge->num_active;
01983    }
01984    if (ast_test_flag(&bridge_channel->features->feature_flags, AST_BRIDGE_CHANNEL_FLAG_LONELY)) {
01985       --bridge->num_lonely;
01986    }
01987    --bridge->num_channels;
01988    AST_LIST_REMOVE(&bridge->channels, bridge_channel, entry);
01989 
01990    bridge_channel_dissolve_check(bridge_channel);
01991    bridge->v_table->pull(bridge, bridge_channel);
01992 
01993    ast_bridge_channel_clear_roles(bridge_channel);
01994 
01995    /* If we are not going to be hung up after leaving a bridge, and we were an
01996     * outgoing channel, clear the outgoing flag.
01997     */
01998    if (ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_OUTGOING)
01999        && (ast_channel_is_leaving_bridge(bridge_channel->chan)
02000            || bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT)) {
02001       ast_debug(2, "Channel %s will survive this bridge; clearing outgoing (dialed) flag\n", ast_channel_name(bridge_channel->chan));
02002       ast_clear_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_OUTGOING);
02003    }
02004 
02005    bridge->reconfigured = 1;
02006    ast_bridge_publish_leave(bridge, bridge_channel->chan);
02007 }
02008 
02009 int bridge_channel_internal_push(struct ast_bridge_channel *bridge_channel)
02010 {
02011    struct ast_bridge *bridge = bridge_channel->bridge;
02012    struct ast_bridge_channel *swap;
02013 
02014    ast_assert(!bridge_channel->in_bridge);
02015 
02016    swap = bridge_find_channel(bridge, bridge_channel->swap);
02017    bridge_channel->swap = NULL;
02018 
02019    if (swap) {
02020       ast_debug(1, "Bridge %s: pushing %p(%s) by swapping with %p(%s)\n",
02021          bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan),
02022          swap, ast_channel_name(swap->chan));
02023    } else {
02024       ast_debug(1, "Bridge %s: pushing %p(%s)\n",
02025          bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan));
02026    }
02027 
02028    /* Add channel to the bridge */
02029    if (bridge->dissolved
02030       || bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT
02031       || (swap && swap->state != BRIDGE_CHANNEL_STATE_WAIT)
02032       || bridge->v_table->push(bridge, bridge_channel, swap)
02033       || ast_bridge_channel_establish_roles(bridge_channel)) {
02034       ast_debug(1, "Bridge %s: pushing %p(%s) into bridge failed\n",
02035          bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan));
02036       return -1;
02037    }
02038 
02039    if (swap) {
02040       int dissolve = ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_DISSOLVE_EMPTY);
02041 
02042       /* This flag is cleared so the act of this channel leaving does not cause it to dissolve if need be */
02043       ast_clear_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_DISSOLVE_EMPTY);
02044 
02045       ast_bridge_channel_leave_bridge(swap, BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, 0);
02046       bridge_channel_internal_pull(swap);
02047 
02048       ast_set2_flag(&bridge->feature_flags, dissolve, AST_BRIDGE_FLAG_DISSOLVE_EMPTY);
02049    }
02050 
02051    bridge_channel->in_bridge = 1;
02052    bridge_channel->just_joined = 1;
02053    AST_LIST_INSERT_TAIL(&bridge->channels, bridge_channel, entry);
02054    ++bridge->num_channels;
02055    if (ast_test_flag(&bridge_channel->features->feature_flags, AST_BRIDGE_CHANNEL_FLAG_LONELY)) {
02056       ++bridge->num_lonely;
02057    }
02058    if (!bridge_channel->suspended) {
02059       ++bridge->num_active;
02060    }
02061 
02062    ast_verb(3, "Channel %s %s%s%s '%s' %s-bridge <%s>\n",
02063       ast_channel_name(bridge_channel->chan),
02064       swap ? "swapped with " : "joined",
02065       swap ? ast_channel_name(swap->chan) : "",
02066       swap ? " into" : "",
02067       bridge->technology->name,
02068       bridge->v_table->name,
02069       bridge->uniqueid);
02070 
02071    ast_bridge_publish_enter(bridge, bridge_channel->chan, swap ? swap->chan : NULL);
02072 
02073    /* Clear any BLINDTRANSFER and ATTENDEDTRANSFER since the transfer has completed. */
02074    pbx_builtin_setvar_helper(bridge_channel->chan, "BLINDTRANSFER", NULL);
02075    pbx_builtin_setvar_helper(bridge_channel->chan, "ATTENDEDTRANSFER", NULL);
02076 
02077    /* Wake up the bridge channel thread to reevaluate any interval timers. */
02078    ast_queue_frame(bridge_channel->chan, &ast_null_frame);
02079 
02080    bridge->reconfigured = 1;
02081    return 0;
02082 }
02083 
02084 /*!
02085  * \internal
02086  * \brief Handle bridge channel control frame action.
02087  * \since 12.0.0
02088  *
02089  * \param bridge_channel Channel to execute the control frame action on.
02090  * \param fr Control frame to handle.
02091  *
02092  * \return Nothing
02093  */
02094 static void bridge_channel_handle_control(struct ast_bridge_channel *bridge_channel, struct ast_frame *fr)
02095 {
02096    struct ast_channel *chan;
02097    struct ast_option_header *aoh;
02098    int is_caller;
02099 
02100    chan = bridge_channel->chan;
02101    switch (fr->subclass.integer) {
02102    case AST_CONTROL_REDIRECTING:
02103       is_caller = !ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING);
02104       if (ast_channel_redirecting_sub(NULL, chan, fr, 1) &&
02105          ast_channel_redirecting_macro(NULL, chan, fr, is_caller, 1)) {
02106          ast_indicate_data(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
02107       }
02108       break;
02109    case AST_CONTROL_CONNECTED_LINE:
02110       is_caller = !ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING);
02111       if (ast_channel_connected_line_sub(NULL, chan, fr, 1) &&
02112          ast_channel_connected_line_macro(NULL, chan, fr, is_caller, 1)) {
02113          ast_indicate_data(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
02114       }
02115       break;
02116    case AST_CONTROL_OPTION:
02117       /*
02118        * Forward option Requests, but only ones we know are safe These
02119        * are ONLY sent by chan_iax2 and I'm not convinced that they
02120        * are useful. I haven't deleted them entirely because I just am
02121        * not sure of the ramifications of removing them.
02122        */
02123       aoh = fr->data.ptr;
02124       if (aoh && aoh->flag == AST_OPTION_FLAG_REQUEST) {
02125          switch (ntohs(aoh->option)) {
02126          case AST_OPTION_TONE_VERIFY:
02127          case AST_OPTION_TDD:
02128          case AST_OPTION_RELAXDTMF:
02129          case AST_OPTION_AUDIO_MODE:
02130          case AST_OPTION_DIGIT_DETECT:
02131          case AST_OPTION_FAX_DETECT:
02132             ast_channel_setoption(chan, ntohs(aoh->option), aoh->data,
02133                fr->datalen - sizeof(*aoh), 0);
02134             break;
02135          default:
02136             break;
02137          }
02138       }
02139       break;
02140    case AST_CONTROL_ANSWER:
02141       if (ast_channel_state(chan) != AST_STATE_UP) {
02142          ast_answer(chan);
02143       } else {
02144          ast_indicate(chan, -1);
02145       }
02146       break;
02147    case AST_CONTROL_MASQUERADE_NOTIFY:
02148       /* Should never happen. */
02149       ast_assert(0);
02150       break;
02151    default:
02152       ast_indicate_data(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
02153       break;
02154    }
02155 }
02156 
02157 /*!
02158  * \internal
02159  * \param bridge_channel Channel to read wr_queue alert pipe.
02160  *
02161  * \return Nothing
02162  */
02163 static void bridge_channel_read_wr_queue_alert(struct ast_bridge_channel *bridge_channel)
02164 {
02165    char nudge;
02166 
02167    if (read(bridge_channel->alert_pipe[0], &nudge, sizeof(nudge)) < 0) {
02168       if (errno != EINTR && errno != EAGAIN) {
02169          ast_log(LOG_WARNING, "read() failed for alert pipe on %p(%s): %s\n",
02170             bridge_channel, ast_channel_name(bridge_channel->chan),
02171             strerror(errno));
02172       }
02173    }
02174 }
02175 
02176 /*!
02177  * \internal
02178  * \brief Handle bridge channel write frame to channel.
02179  * \since 12.0.0
02180  *
02181  * \param bridge_channel Channel to write outgoing frame.
02182  *
02183  * \return Nothing
02184  */
02185 static void bridge_channel_handle_write(struct ast_bridge_channel *bridge_channel)
02186 {
02187    struct ast_frame *fr;
02188    struct sync_payload *sync_payload;
02189 
02190    ast_bridge_channel_lock(bridge_channel);
02191 
02192    /* It's not good to have unbalanced frames and alert_pipe alerts. */
02193    ast_assert(!AST_LIST_EMPTY(&bridge_channel->wr_queue));
02194    if (AST_LIST_EMPTY(&bridge_channel->wr_queue)) {
02195       /* No frame, flush the alert pipe of excess alerts. */
02196       ast_log(LOG_WARNING, "Weird.  No frame from bridge for %s to process?\n",
02197          ast_channel_name(bridge_channel->chan));
02198       bridge_channel_read_wr_queue_alert(bridge_channel);
02199       ast_bridge_channel_unlock(bridge_channel);
02200       return;
02201    }
02202 
02203    AST_LIST_TRAVERSE_SAFE_BEGIN(&bridge_channel->wr_queue, fr, frame_list) {
02204       if (bridge_channel->dtmf_hook_state.collected[0]) {
02205          switch (fr->frametype) {
02206          case AST_FRAME_BRIDGE_ACTION:
02207          case AST_FRAME_BRIDGE_ACTION_SYNC:
02208             /* Defer processing these frames while DTMF is collected. */
02209             continue;
02210          default:
02211             break;
02212          }
02213       }
02214       bridge_channel_read_wr_queue_alert(bridge_channel);
02215       AST_LIST_REMOVE_CURRENT(frame_list);
02216       break;
02217    }
02218    AST_LIST_TRAVERSE_SAFE_END;
02219 
02220    ast_bridge_channel_unlock(bridge_channel);
02221    if (!fr) {
02222       /*
02223        * Wait some to reduce CPU usage from a tight loop
02224        * without any wait because we only have deferred
02225        * frames in the wr_queue.
02226        */
02227       usleep(1);
02228       return;
02229    }
02230 
02231    switch (fr->frametype) {
02232    case AST_FRAME_BRIDGE_ACTION:
02233       bridge_channel_handle_action(bridge_channel, fr->subclass.integer, fr->data.ptr);
02234       break;
02235    case AST_FRAME_BRIDGE_ACTION_SYNC:
02236       sync_payload = fr->data.ptr;
02237       bridge_channel_handle_action(bridge_channel, fr->subclass.integer, sync_payload->data);
02238       break;
02239    case AST_FRAME_CONTROL:
02240       bridge_channel_handle_control(bridge_channel, fr);
02241       break;
02242    case AST_FRAME_NULL:
02243       break;
02244    default:
02245       /* Write the frame to the channel. */
02246       bridge_channel->activity = BRIDGE_CHANNEL_THREAD_SIMPLE;
02247       ast_write(bridge_channel->chan, fr);
02248       break;
02249    }
02250    bridge_frame_free(fr);
02251 }
02252 
02253 /*! \brief Internal function to handle DTMF from a channel */
02254 static struct ast_frame *bridge_handle_dtmf(struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
02255 {
02256    struct ast_bridge_features *features = bridge_channel->features;
02257    struct ast_bridge_hook_dtmf *hook = NULL;
02258    char dtmf[2];
02259 
02260    /*
02261     * See if we are already matching a DTMF feature hook sequence or
02262     * if this DTMF matches the beginning of any DTMF feature hooks.
02263     */
02264    dtmf[0] = frame->subclass.integer;
02265    dtmf[1] = '\0';
02266    if (bridge_channel->dtmf_hook_state.collected[0]
02267       || (hook = ao2_find(features->dtmf_hooks, dtmf, OBJ_SEARCH_PARTIAL_KEY))) {
02268       enum ast_frame_type frametype = frame->frametype;
02269 
02270       bridge_frame_free(frame);
02271       frame = NULL;
02272 
02273       ao2_cleanup(hook);
02274 
02275       switch (frametype) {
02276       case AST_FRAME_DTMF_BEGIN:
02277          /* Just eat the frame. */
02278          break;
02279       case AST_FRAME_DTMF_END:
02280          ast_bridge_channel_feature_digit(bridge_channel, dtmf[0]);
02281          break;
02282       default:
02283          /* Unexpected frame type. */
02284          ast_assert(0);
02285          break;
02286       }
02287 #ifdef TEST_FRAMEWORK
02288    } else if (frame->frametype == AST_FRAME_DTMF_END) {
02289       /* Only transmit this event on DTMF end or else every DTMF
02290        * press will result in the event being broadcast twice
02291        */
02292       ast_test_suite_event_notify("FEATURE_DETECTION", "Result: fail");
02293 #endif
02294    }
02295 
02296    return frame;
02297 }
02298 
02299 
02300 /*!
02301  * \internal
02302  * \brief Feed notification that a frame is waiting on a channel into the bridging core
02303  *
02304  * \param bridge_channel Bridge channel the notification was received on
02305  */
02306 static void bridge_handle_trip(struct ast_bridge_channel *bridge_channel)
02307 {
02308    struct ast_frame *frame;
02309 
02310    if (bridge_channel->features->mute) {
02311       frame = ast_read_noaudio(bridge_channel->chan);
02312    } else {
02313       frame = ast_read(bridge_channel->chan);
02314    }
02315 
02316    if (!frame) {
02317       ast_bridge_channel_kick(bridge_channel, 0);
02318       return;
02319    }
02320    switch (frame->frametype) {
02321    case AST_FRAME_CONTROL:
02322       switch (frame->subclass.integer) {
02323       case AST_CONTROL_HANGUP:
02324          ast_bridge_channel_kick(bridge_channel, 0);
02325          bridge_frame_free(frame);
02326          return;
02327       default:
02328          break;
02329       }
02330       break;
02331    case AST_FRAME_DTMF_BEGIN:
02332    case AST_FRAME_DTMF_END:
02333       frame = bridge_handle_dtmf(bridge_channel, frame);
02334       if (!frame) {
02335          return;
02336       }
02337       if (!bridge_channel->features->dtmf_passthrough) {
02338          bridge_frame_free(frame);
02339          return;
02340       }
02341       break;
02342    default:
02343       break;
02344    }
02345 
02346    /* Simply write the frame out to the bridge technology. */
02347    bridge_channel_write_frame(bridge_channel, frame);
02348    bridge_frame_free(frame);
02349 }
02350 
02351 /*!
02352  * \internal
02353  * \brief Determine how long till the next timer interval.
02354  * \since 12.0.0
02355  *
02356  * \param bridge_channel Channel to determine how long can wait.
02357  *
02358  * \retval ms Number of milliseconds to wait.
02359  * \retval -1 to wait forever.
02360  */
02361 static int bridge_channel_next_interval(struct ast_bridge_channel *bridge_channel)
02362 {
02363    struct ast_heap *interval_hooks = bridge_channel->features->interval_hooks;
02364    struct ast_bridge_hook_timer *hook;
02365    int ms;
02366 
02367    ast_heap_wrlock(interval_hooks);
02368    hook = ast_heap_peek(interval_hooks, 1);
02369    if (hook) {
02370       ms = ast_tvdiff_ms(hook->timer.trip_time, ast_tvnow());
02371       if (ms < 0) {
02372          /* Expire immediately.  An interval hook is ready to run. */
02373          ms = 0;
02374       }
02375    } else {
02376       /* No hook so wait forever. */
02377       ms = -1;
02378    }
02379    ast_heap_unlock(interval_hooks);
02380 
02381    return ms;
02382 }
02383 
02384 /*!
02385  * \internal
02386  * \brief Determine how long till the DTMF interdigit timeout.
02387  * \since 12.8.0
02388  *
02389  * \param bridge_channel Channel to determine how long can wait.
02390  *
02391  * \retval ms Number of milliseconds to wait.
02392  * \retval -1 to wait forever.
02393  */
02394 static int bridge_channel_feature_timeout(struct ast_bridge_channel *bridge_channel)
02395 {
02396    int ms;
02397 
02398    if (bridge_channel->dtmf_hook_state.collected[0]) {
02399       ms = ast_tvdiff_ms(bridge_channel->dtmf_hook_state.interdigit_timeout,
02400          ast_tvnow());
02401       if (ms < 0) {
02402          /* Expire immediately. */
02403          ms = 0;
02404       }
02405    } else {
02406       /* Timer is not active so wait forever. */
02407       ms = -1;
02408    }
02409 
02410    return ms;
02411 }
02412 
02413 /*!
02414  * \internal
02415  * \brief Determine how long till a timeout.
02416  * \since 12.8.0
02417  *
02418  * \param bridge_channel Channel to determine how long can wait.
02419  *
02420  * \retval ms Number of milliseconds to wait.
02421  * \retval -1 to wait forever.
02422  */
02423 static int bridge_channel_next_timeout(struct ast_bridge_channel *bridge_channel)
02424 {
02425    int ms_interval;
02426    int ms;
02427 
02428    ms_interval = bridge_channel_next_interval(bridge_channel);
02429    ms = bridge_channel_feature_timeout(bridge_channel);
02430    if (ms < 0 || (0 <= ms_interval && ms_interval < ms)) {
02431       /* Interval hook timeout is next. */
02432       ms = ms_interval;
02433    }
02434 
02435    return ms;
02436 }
02437 
02438 /*!
02439  * \internal
02440  * \brief Wait for something to happen on the bridge channel and handle it.
02441  * \since 12.0.0
02442  *
02443  * \param bridge_channel Channel to wait.
02444  *
02445  * \note Each channel does writing/reading in their own thread.
02446  *
02447  * \return Nothing
02448  */
02449 static void bridge_channel_wait(struct ast_bridge_channel *bridge_channel)
02450 {
02451    int ms;
02452    int outfd;
02453    struct ast_channel *chan;
02454 
02455    /* Wait for data to either come from the channel or us to be signaled */
02456    ast_bridge_channel_lock(bridge_channel);
02457    if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
02458    } else if (bridge_channel->suspended) {
02459 /* XXX ASTERISK-21271 the external party use of suspended will go away as will these references because this is the bridge channel thread */
02460       ast_debug(1, "Bridge %s: %p(%s) is going into a signal wait\n",
02461          bridge_channel->bridge->uniqueid, bridge_channel,
02462          ast_channel_name(bridge_channel->chan));
02463       ast_cond_wait(&bridge_channel->cond, ao2_object_get_lockaddr(bridge_channel));
02464    } else {
02465       ast_bridge_channel_unlock(bridge_channel);
02466       outfd = -1;
02467       ms = bridge_channel_next_timeout(bridge_channel);
02468       chan = ast_waitfor_nandfds(&bridge_channel->chan, 1,
02469          &bridge_channel->alert_pipe[0], 1, NULL, &outfd, &ms);
02470       if (ast_channel_unbridged(bridge_channel->chan)) {
02471          ast_channel_set_unbridged(bridge_channel->chan, 0);
02472          ast_bridge_channel_lock_bridge(bridge_channel);
02473          bridge_channel->bridge->reconfigured = 1;
02474          bridge_reconfigured(bridge_channel->bridge, 0);
02475          ast_bridge_unlock(bridge_channel->bridge);
02476       }
02477       ast_bridge_channel_lock(bridge_channel);
02478       bridge_channel->activity = BRIDGE_CHANNEL_THREAD_FRAME;
02479       ast_bridge_channel_unlock(bridge_channel);
02480       if (!bridge_channel->suspended
02481          && bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
02482          if (chan) {
02483             bridge_handle_trip(bridge_channel);
02484          } else if (ms == 0) {
02485             /* An interdigit timeout or interval expired. */
02486             bridge_channel_handle_feature_timeout(bridge_channel);
02487             bridge_channel_handle_interval(bridge_channel);
02488          } else if (-1 < outfd) {
02489             /*
02490              * Must do this after checking timeouts or may have
02491              * an infinite loop due to deferring write queue
02492              * actions while trying to match DTMF feature hooks.
02493              */
02494             bridge_channel_handle_write(bridge_channel);
02495          }
02496       }
02497       bridge_channel->activity = BRIDGE_CHANNEL_THREAD_IDLE;
02498       return;
02499    }
02500    ast_bridge_channel_unlock(bridge_channel);
02501 }
02502 
02503 /*!
02504  * \internal
02505  * \brief Handle bridge channel join/leave event.
02506  * \since 12.0.0
02507  *
02508  * \param bridge_channel Which channel is involved.
02509  * \param type Specified join/leave event.
02510  *
02511  * \return Nothing
02512  */
02513 static void bridge_channel_event_join_leave(struct ast_bridge_channel *bridge_channel, enum ast_bridge_hook_type type)
02514 {
02515    struct ast_bridge_features *features = bridge_channel->features;
02516    struct ast_bridge_hook *hook;
02517    struct ao2_iterator iter;
02518 
02519    /* Run the specified hooks. */
02520    iter = ao2_iterator_init(features->other_hooks, 0);
02521    for (; (hook = ao2_iterator_next(&iter)); ao2_ref(hook, -1)) {
02522       if (hook->type == type) {
02523          break;
02524       }
02525    }
02526    if (hook) {
02527       /* Found the first specified hook to run. */
02528       bridge_channel_suspend(bridge_channel);
02529       ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
02530       do {
02531          if (hook->type == type) {
02532             hook->callback(bridge_channel, hook->hook_pvt);
02533             ao2_unlink(features->other_hooks, hook);
02534          }
02535          ao2_ref(hook, -1);
02536       } while ((hook = ao2_iterator_next(&iter)));
02537       ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
02538       bridge_channel_unsuspend(bridge_channel);
02539    }
02540    ao2_iterator_destroy(&iter);
02541 }
02542 
02543 int bridge_channel_internal_join(struct ast_bridge_channel *bridge_channel)
02544 {
02545    int res = 0;
02546    struct ast_bridge_features *channel_features;
02547    struct ast_channel *swap;
02548 
02549    ast_debug(1, "Bridge %s: %p(%s) is joining\n",
02550       bridge_channel->bridge->uniqueid,
02551       bridge_channel, ast_channel_name(bridge_channel->chan));
02552 
02553    /*
02554     * Directly locking the bridge is safe here because nobody else
02555     * knows about this bridge_channel yet.
02556     */
02557    ast_bridge_lock(bridge_channel->bridge);
02558 
02559    ast_channel_lock(bridge_channel->chan);
02560 
02561    bridge_channel->read_format = ao2_bump(ast_channel_readformat(bridge_channel->chan));
02562    bridge_channel->write_format = ao2_bump(ast_channel_writeformat(bridge_channel->chan));
02563 
02564    /* Make sure we're still good to be put into a bridge */
02565    if (ast_channel_internal_bridge(bridge_channel->chan)
02566       || ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_ZOMBIE)) {
02567       ast_channel_unlock(bridge_channel->chan);
02568       ast_bridge_unlock(bridge_channel->bridge);
02569       ast_debug(1, "Bridge %s: %p(%s) failed to join Bridge\n",
02570          bridge_channel->bridge->uniqueid,
02571          bridge_channel,
02572          ast_channel_name(bridge_channel->chan));
02573       return -1;
02574    }
02575    ast_channel_internal_bridge_set(bridge_channel->chan, bridge_channel->bridge);
02576 
02577    /* Attach features requested by the channel */
02578    channel_features = ast_channel_feature_hooks_get(bridge_channel->chan);
02579    if (channel_features) {
02580       ast_bridge_features_merge(bridge_channel->features, channel_features);
02581    }
02582    ast_channel_unlock(bridge_channel->chan);
02583 
02584    /* Add the jitterbuffer if the channel requires it */
02585    ast_jb_enable_for_channel(bridge_channel->chan);
02586 
02587    if (!bridge_channel->bridge->callid) {
02588       bridge_channel->bridge->callid = ast_read_threadstorage_callid();
02589    }
02590 
02591    /* Take the swap channel ref from the bridge_channel struct. */
02592    swap = bridge_channel->swap;
02593 
02594    if (bridge_channel_internal_push(bridge_channel)) {
02595       int cause = bridge_channel->bridge->cause;
02596 
02597       ast_bridge_unlock(bridge_channel->bridge);
02598       ast_bridge_channel_kick(bridge_channel, cause);
02599       ast_bridge_channel_lock_bridge(bridge_channel);
02600       ast_bridge_features_remove(bridge_channel->features,
02601          AST_BRIDGE_HOOK_REMOVE_ON_PULL);
02602       bridge_channel_dissolve_check(bridge_channel);
02603       res = -1;
02604    }
02605    bridge_reconfigured(bridge_channel->bridge, !bridge_channel->inhibit_colp);
02606 
02607    if (bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
02608       /*
02609        * Indicate a source change since this channel is entering the
02610        * bridge system only if the bridge technology is not MULTIMIX
02611        * capable.  The MULTIMIX technology has already done it.
02612        */
02613       if (!(bridge_channel->bridge->technology->capabilities
02614          & AST_BRIDGE_CAPABILITY_MULTIMIX)) {
02615          ast_indicate(bridge_channel->chan, AST_CONTROL_SRCCHANGE);
02616       }
02617 
02618       ast_bridge_unlock(bridge_channel->bridge);
02619 
02620       /* Must release any swap ref after unlocking the bridge. */
02621       ao2_t_cleanup(swap, "Bridge push with swap successful");
02622       swap = NULL;
02623 
02624       bridge_channel_event_join_leave(bridge_channel, AST_BRIDGE_HOOK_TYPE_JOIN);
02625 
02626       while (bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
02627          /* Wait for something to do. */
02628          bridge_channel_wait(bridge_channel);
02629       }
02630 
02631       /* Force a timeout on any accumulated DTMF hook digits. */
02632       ast_bridge_channel_feature_digit(bridge_channel, 0);
02633 
02634       bridge_channel_event_join_leave(bridge_channel, AST_BRIDGE_HOOK_TYPE_LEAVE);
02635       ast_bridge_channel_lock_bridge(bridge_channel);
02636    }
02637 
02638    bridge_channel_internal_pull(bridge_channel);
02639    bridge_channel_settle_owed_events(bridge_channel->bridge, bridge_channel);
02640    bridge_reconfigured(bridge_channel->bridge, 1);
02641 
02642    ast_bridge_unlock(bridge_channel->bridge);
02643 
02644    /* Must release any swap ref after unlocking the bridge. */
02645    ao2_t_cleanup(swap, "Bridge push with swap failed or exited immediately");
02646 
02647    /* Complete any active hold before exiting the bridge. */
02648    if (ast_channel_hold_state(bridge_channel->chan) == AST_CONTROL_HOLD) {
02649       ast_debug(1, "Channel %s simulating UNHOLD for bridge end.\n",
02650          ast_channel_name(bridge_channel->chan));
02651       ast_indicate(bridge_channel->chan, AST_CONTROL_UNHOLD);
02652    }
02653 
02654    /* Complete any partial DTMF digit before exiting the bridge. */
02655    if (ast_channel_sending_dtmf_digit(bridge_channel->chan)) {
02656       ast_channel_end_dtmf(bridge_channel->chan,
02657          ast_channel_sending_dtmf_digit(bridge_channel->chan),
02658          ast_channel_sending_dtmf_tv(bridge_channel->chan), "bridge end");
02659    }
02660 
02661    /* Indicate a source change since this channel is leaving the bridge system. */
02662    ast_indicate(bridge_channel->chan, AST_CONTROL_SRCCHANGE);
02663 
02664    /*
02665     * Wait for any dual redirect to complete.
02666     *
02667     * Must be done while "still in the bridge" for ast_async_goto()
02668     * to work right.
02669     */
02670    while (ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_BRIDGE_DUAL_REDIRECT_WAIT)) {
02671       sched_yield();
02672    }
02673    ast_channel_lock(bridge_channel->chan);
02674    ast_channel_internal_bridge_set(bridge_channel->chan, NULL);
02675    ast_channel_unlock(bridge_channel->chan);
02676 
02677    ast_bridge_channel_restore_formats(bridge_channel);
02678 
02679    return res;
02680 }
02681 
02682 int bridge_channel_internal_queue_blind_transfer(struct ast_channel *transferee,
02683       const char *exten, const char *context,
02684       transfer_channel_cb new_channel_cb, void *user_data)
02685 {
02686    RAII_VAR(struct ast_bridge_channel *, transferee_bridge_channel, NULL, ao2_cleanup);
02687    struct blind_transfer_data blind_data;
02688 
02689    ast_channel_lock(transferee);
02690    transferee_bridge_channel = ast_channel_get_bridge_channel(transferee);
02691    ast_channel_unlock(transferee);
02692 
02693    if (!transferee_bridge_channel) {
02694       return -1;
02695    }
02696 
02697    if (new_channel_cb) {
02698       new_channel_cb(transferee, user_data, AST_BRIDGE_TRANSFER_SINGLE_PARTY);
02699    }
02700 
02701    ast_copy_string(blind_data.exten, exten, sizeof(blind_data.exten));
02702    ast_copy_string(blind_data.context, context, sizeof(blind_data.context));
02703 
02704    return bridge_channel_queue_action_data(transferee_bridge_channel,
02705       BRIDGE_CHANNEL_ACTION_BLIND_TRANSFER, &blind_data, sizeof(blind_data));
02706 }
02707 
02708 int bridge_channel_internal_queue_attended_transfer(struct ast_channel *transferee,
02709       struct ast_channel *unbridged_chan)
02710 {
02711    RAII_VAR(struct ast_bridge_channel *, transferee_bridge_channel, NULL, ao2_cleanup);
02712    char unbridged_chan_name[AST_CHANNEL_NAME];
02713 
02714    ast_channel_lock(transferee);
02715    transferee_bridge_channel = ast_channel_get_bridge_channel(transferee);
02716    ast_channel_unlock(transferee);
02717 
02718    if (!transferee_bridge_channel) {
02719       return -1;
02720    }
02721 
02722    ast_copy_string(unbridged_chan_name, ast_channel_name(unbridged_chan),
02723       sizeof(unbridged_chan_name));
02724 
02725    return bridge_channel_queue_action_data(transferee_bridge_channel,
02726       BRIDGE_CHANNEL_ACTION_ATTENDED_TRANSFER, unbridged_chan_name,
02727       sizeof(unbridged_chan_name));
02728 }
02729 
02730 int bridge_channel_internal_allows_optimization(struct ast_bridge_channel *bridge_channel)
02731 {
02732    return bridge_channel->in_bridge
02733       && AST_LIST_EMPTY(&bridge_channel->wr_queue);
02734 }
02735 
02736 /*!
02737  * \internal
02738  * \brief Close a pipe.
02739  * \since 12.0.0
02740  *
02741  * \param my_pipe What to close.
02742  *
02743  * \return Nothing
02744  */
02745 static void pipe_close(int *my_pipe)
02746 {
02747    if (my_pipe[0] > -1) {
02748       close(my_pipe[0]);
02749       my_pipe[0] = -1;
02750    }
02751    if (my_pipe[1] > -1) {
02752       close(my_pipe[1]);
02753       my_pipe[1] = -1;
02754    }
02755 }
02756 
02757 /*!
02758  * \internal
02759  * \brief Initialize a pipe as non-blocking.
02760  * \since 12.0.0
02761  *
02762  * \param my_pipe What to initialize.
02763  *
02764  * \retval 0 on success.
02765  * \retval -1 on error.
02766  */
02767 static int pipe_init_nonblock(int *my_pipe)
02768 {
02769    int flags;
02770 
02771    my_pipe[0] = -1;
02772    my_pipe[1] = -1;
02773    if (pipe(my_pipe)) {
02774       ast_log(LOG_WARNING, "Can't create pipe! Try increasing max file descriptors with ulimit -n\n");
02775       return -1;
02776    }
02777    flags = fcntl(my_pipe[0], F_GETFL);
02778    if (fcntl(my_pipe[0], F_SETFL, flags | O_NONBLOCK) < 0) {
02779       ast_log(LOG_WARNING, "Unable to set read pipe nonblocking! (%d: %s)\n",
02780          errno, strerror(errno));
02781       return -1;
02782    }
02783    flags = fcntl(my_pipe[1], F_GETFL);
02784    if (fcntl(my_pipe[1], F_SETFL, flags | O_NONBLOCK) < 0) {
02785       ast_log(LOG_WARNING, "Unable to set write pipe nonblocking! (%d: %s)\n",
02786          errno, strerror(errno));
02787       return -1;
02788    }
02789    return 0;
02790 }
02791 
02792 /* Destroy elements of the bridge channel structure and the bridge channel structure itself */
02793 static void bridge_channel_destroy(void *obj)
02794 {
02795    struct ast_bridge_channel *bridge_channel = obj;
02796    struct ast_frame *fr;
02797 
02798    if (bridge_channel->callid) {
02799       bridge_channel->callid = 0;
02800    }
02801 
02802    if (bridge_channel->bridge) {
02803       ao2_ref(bridge_channel->bridge, -1);
02804       bridge_channel->bridge = NULL;
02805    }
02806 
02807    /* Flush any unhandled wr_queue frames. */
02808    while ((fr = AST_LIST_REMOVE_HEAD(&bridge_channel->wr_queue, frame_list))) {
02809       bridge_frame_free(fr);
02810    }
02811    pipe_close(bridge_channel->alert_pipe);
02812 
02813    ast_cond_destroy(&bridge_channel->cond);
02814 
02815    ao2_cleanup(bridge_channel->write_format);
02816    ao2_cleanup(bridge_channel->read_format);
02817 }
02818 
02819 struct ast_bridge_channel *bridge_channel_internal_alloc(struct ast_bridge *bridge)
02820 {
02821    struct ast_bridge_channel *bridge_channel;
02822 
02823    bridge_channel = ao2_alloc(sizeof(struct ast_bridge_channel), bridge_channel_destroy);
02824    if (!bridge_channel) {
02825       return NULL;
02826    }
02827    ast_cond_init(&bridge_channel->cond, NULL);
02828    if (pipe_init_nonblock(bridge_channel->alert_pipe)) {
02829       ao2_ref(bridge_channel, -1);
02830       return NULL;
02831    }
02832    if (bridge) {
02833       bridge_channel->bridge = bridge;
02834       ao2_ref(bridge_channel->bridge, +1);
02835    }
02836 
02837    return bridge_channel;
02838 }

Generated on Thu Apr 16 06:27:16 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6