Sat Apr 26 2014 22:01:29

Asterisk developer's documentation


bridging.h
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  * \brief Channel Bridging API
00021  * \author Joshua Colp <jcolp@digium.com>
00022  * \ref AstBridging
00023  */
00024 
00025 /*!
00026  * \page AstBridging Channel Bridging API
00027  *
00028  * The purpose of this API is to provide an easy and flexible way to bridge
00029  * channels of different technologies with different features.
00030  *
00031  * Bridging technologies provide the mechanism that do the actual handling
00032  * of frames between channels. They provide capability information, codec information,
00033  * and preference value to assist the bridging core in choosing a bridging technology when
00034  * creating a bridge. Different bridges may use different bridging technologies based on needs
00035  * but once chosen they all operate under the same premise; they receive frames and send frames.
00036  *
00037  * Bridges are a combination of bridging technology, channels, and features. A
00038  * developer creates a new bridge based on what they are currently expecting to do
00039  * with it or what they will do with it in the future. The bridging core determines what
00040  * available bridging technology will best fit the requirements and creates a new bridge.
00041  * Once created, channels can be added to the bridge in a blocking or non-blocking fashion.
00042  *
00043  * Features are such things as channel muting or DTMF based features such as attended transfer,
00044  * blind transfer, and hangup. Feature information must be set at the most granular level, on
00045  * the channel. While you can use features on a global scope the presence of a feature structure
00046  * on the channel will override the global scope. An example would be having the bridge muted
00047  * at global scope and attended transfer enabled on a channel. Since the channel itself is not muted
00048  * it would be able to speak.
00049  *
00050  * Feature hooks allow a developer to tell the bridging core that when a DTMF string
00051  * is received from a channel a callback should be called in their application. For
00052  * example, a conference bridge application may want to provide an IVR to control various
00053  * settings on the conference bridge. This can be accomplished by attaching a feature hook
00054  * that calls an IVR function when a DTMF string is entered.
00055  *
00056  */
00057 
00058 #ifndef _ASTERISK_BRIDGING_H
00059 #define _ASTERISK_BRIDGING_H
00060 
00061 #if defined(__cplusplus) || defined(c_plusplus)
00062 extern "C" {
00063 #endif
00064 
00065 #include "asterisk/bridging_features.h"
00066 #include "asterisk/dsp.h"
00067 
00068 /*! \brief Capabilities for a bridge technology */
00069 enum ast_bridge_capability {
00070    /*! Bridge is only capable of mixing 2 channels */
00071    AST_BRIDGE_CAPABILITY_1TO1MIX = (1 << 1),
00072    /*! Bridge is capable of mixing 2 or more channels */
00073    AST_BRIDGE_CAPABILITY_MULTIMIX = (1 << 2),
00074    /*! Bridge should natively bridge two channels if possible */
00075    AST_BRIDGE_CAPABILITY_NATIVE = (1 << 3),
00076    /*! Bridge should run using the multithreaded model */
00077    AST_BRIDGE_CAPABILITY_MULTITHREADED = (1 << 4),
00078    /*! Bridge should run a central bridge thread */
00079    AST_BRIDGE_CAPABILITY_THREAD = (1 << 5),
00080    /*! Bridge technology can do video mixing (or something along those lines) */
00081    AST_BRIDGE_CAPABILITY_VIDEO = (1 << 6),
00082    /*! Bridge technology can optimize things based on who is talking */
00083    AST_BRIDGE_CAPABILITY_OPTIMIZE = (1 << 7),
00084 };
00085 
00086 /*! \brief State information about a bridged channel */
00087 enum ast_bridge_channel_state {
00088    /*! Waiting for a signal */
00089    AST_BRIDGE_CHANNEL_STATE_WAIT = 0,
00090    /*! Bridged channel has ended itself (it has hung up) */
00091    AST_BRIDGE_CHANNEL_STATE_END,
00092    /*! Bridged channel should be hung up */
00093    AST_BRIDGE_CHANNEL_STATE_HANGUP,
00094    /*! Bridged channel should be removed from the bridge without being hung up */
00095    AST_BRIDGE_CHANNEL_STATE_DEPART,
00096    /*! Bridged channel is executing a feature hook */
00097    AST_BRIDGE_CHANNEL_STATE_FEATURE,
00098    /*! Bridged channel is sending a DTMF stream out */
00099    AST_BRIDGE_CHANNEL_STATE_DTMF,
00100    /*! Bridged channel began talking */
00101    AST_BRIDGE_CHANNEL_STATE_START_TALKING,
00102    /*! Bridged channel has stopped talking */
00103    AST_BRIDGE_CHANNEL_STATE_STOP_TALKING,
00104 };
00105 
00106 /*! \brief Return values for bridge technology write function */
00107 enum ast_bridge_write_result {
00108    /*! Bridge technology wrote out frame fine */
00109    AST_BRIDGE_WRITE_SUCCESS = 0,
00110    /*! Bridge technology attempted to write out the frame but failed */
00111    AST_BRIDGE_WRITE_FAILED,
00112    /*! Bridge technology does not support writing out a frame of this type */
00113    AST_BRIDGE_WRITE_UNSUPPORTED,
00114 };
00115 
00116 struct ast_bridge_technology;
00117 struct ast_bridge;
00118 
00119 /*!
00120  * \brief Structure specific to bridge technologies capable of
00121  * performing talking optimizations.
00122  */
00123 struct ast_bridge_tech_optimizations {
00124    /*! The amount of time in ms that talking must be detected before
00125     *  the dsp determines that talking has occurred */
00126    unsigned int talking_threshold;
00127    /*! The amount of time in ms that silence must be detected before
00128     *  the dsp determines that talking has stopped */
00129    unsigned int silence_threshold;
00130    /*! Whether or not the bridging technology should drop audio
00131     *  detected as silence from the mix. */
00132    unsigned int drop_silence:1;
00133 };
00134 
00135 /*!
00136  * \brief Structure that contains information regarding a channel in a bridge
00137  */
00138 struct ast_bridge_channel {
00139    /*! Lock to protect this data structure */
00140    ast_mutex_t lock;
00141    /*! Condition, used if we want to wake up a thread waiting on the bridged channel */
00142    ast_cond_t cond;
00143    /*! Current bridged channel state */
00144    enum ast_bridge_channel_state state;
00145    /*! Asterisk channel participating in the bridge */
00146    struct ast_channel *chan;
00147    /*! Asterisk channel we are swapping with (if swapping) */
00148    struct ast_channel *swap;
00149    /*! Bridge this channel is participating in */
00150    struct ast_bridge *bridge;
00151    /*! Private information unique to the bridge technology */
00152    void *bridge_pvt;
00153    /*! Thread handling the bridged channel */
00154    pthread_t thread;
00155    /*! Additional file descriptors to look at */
00156    int fds[4];
00157    /*! Bit to indicate whether the channel is suspended from the bridge or not */
00158    unsigned int suspended:1;
00159    /*! Bit to indicate if a imparted channel is allowed to get hungup after leaving the bridge by the bridging core. */
00160    unsigned int allow_impart_hangup:1;
00161    /*! Features structure for features that are specific to this channel */
00162    struct ast_bridge_features *features;
00163    /*! Technology optimization parameters used by bridging technologies capable of
00164     *  optimizing based upon talk detection. */
00165    struct ast_bridge_tech_optimizations tech_args;
00166    /*! Queue of DTMF digits used for DTMF streaming */
00167    char dtmf_stream_q[8];
00168    /*! Call ID associated with bridge channel */
00169    struct ast_callid *callid;
00170    /*! Linked list information */
00171    AST_LIST_ENTRY(ast_bridge_channel) entry;
00172 };
00173 
00174 enum ast_bridge_video_mode_type {
00175    /*! Video is not allowed in the bridge */
00176    AST_BRIDGE_VIDEO_MODE_NONE = 0,
00177    /*! A single user is picked as the only distributed of video across the bridge */
00178    AST_BRIDGE_VIDEO_MODE_SINGLE_SRC,
00179    /*! A single user's video feed is distributed to all bridge channels, but
00180     *  that feed is automatically picked based on who is talking the most. */
00181    AST_BRIDGE_VIDEO_MODE_TALKER_SRC,
00182 };
00183 
00184 /*! This is used for both SINGLE_SRC mode to set what channel
00185  *  should be the current single video feed */
00186 struct ast_bridge_video_single_src_data {
00187    /*! Only accept video coming from this channel */
00188    struct ast_channel *chan_vsrc;
00189 };
00190 
00191 /*! This is used for both SINGLE_SRC_TALKER mode to set what channel
00192  *  should be the current single video feed */
00193 struct ast_bridge_video_talker_src_data {
00194    /*! Only accept video coming from this channel */
00195    struct ast_channel *chan_vsrc;
00196    int average_talking_energy;
00197 
00198    /*! Current talker see's this person */
00199    struct ast_channel *chan_old_vsrc;
00200 };
00201 
00202 struct ast_bridge_video_mode {
00203    enum ast_bridge_video_mode_type mode;
00204    /* Add data for all the video modes here. */
00205    union {
00206       struct ast_bridge_video_single_src_data single_src_data;
00207       struct ast_bridge_video_talker_src_data talker_src_data;
00208    } mode_data;
00209 };
00210 
00211 /*!
00212  * \brief Structure that contains information about a bridge
00213  */
00214 struct ast_bridge {
00215    /*! Number of channels participating in the bridge */
00216    int num;
00217    /*! The video mode this bridge is using */
00218    struct ast_bridge_video_mode video_mode;
00219    /*! The internal sample rate this bridge is mixed at when multiple channels are being mixed.
00220     *  If this value is 0, the bridge technology may auto adjust the internal mixing rate. */
00221    unsigned int internal_sample_rate;
00222    /*! The mixing interval indicates how quickly the bridges internal mixing should occur
00223     * for bridge technologies that mix audio. When set to 0, the bridge tech must choose a
00224     * default interval for itself. */
00225    unsigned int internal_mixing_interval;
00226    /*! Bit to indicate that the bridge thread is waiting on channels in the bridge array */
00227    unsigned int waiting:1;
00228    /*! Bit to indicate the bridge thread should stop */
00229    unsigned int stop:1;
00230    /*! Bit to indicate the bridge thread should refresh itself */
00231    unsigned int refresh:1;
00232    /*! Bridge flags to tweak behavior */
00233    struct ast_flags feature_flags;
00234    /*! Bridge technology that is handling the bridge */
00235    struct ast_bridge_technology *technology;
00236    /*! Private information unique to the bridge technology */
00237    void *bridge_pvt;
00238    /*! Thread running the bridge */
00239    pthread_t thread;
00240    /*! Enabled features information */
00241    struct ast_bridge_features features;
00242    /*! Array of channels that the bridge thread is currently handling */
00243    struct ast_channel **array;
00244    /*! Number of channels in the above array */
00245    size_t array_num;
00246    /*! Number of channels the array can handle */
00247    size_t array_size;
00248    /*! Call ID associated with the bridge */
00249    struct ast_callid *callid;
00250    /*! Linked list of channels participating in the bridge */
00251    AST_LIST_HEAD_NOLOCK(, ast_bridge_channel) channels;
00252    };
00253 
00254 /*! \brief Create a new bridge
00255  *
00256  * \param capabilities The capabilities that we require to be used on the bridge
00257  * \param flags Flags that will alter the behavior of the bridge
00258  *
00259  * \retval a pointer to a new bridge on success
00260  * \retval NULL on failure
00261  *
00262  * Example usage:
00263  *
00264  * \code
00265  * struct ast_bridge *bridge;
00266  * bridge = ast_bridge_new(AST_BRIDGE_CAPABILITY_1TO1MIX, AST_BRIDGE_FLAG_DISSOLVE);
00267  * \endcode
00268  *
00269  * This creates a simple two party bridge that will be destroyed once one of
00270  * the channels hangs up.
00271  */
00272 struct ast_bridge *ast_bridge_new(uint32_t capabilities, int flags);
00273 
00274 /*!
00275  * \brief Lock the bridge.
00276  *
00277  * \param bridge Bridge to lock
00278  *
00279  * \return Nothing
00280  */
00281 #define ast_bridge_lock(bridge)  _ast_bridge_lock(bridge, __FILE__, __PRETTY_FUNCTION__, __LINE__, #bridge)
00282 static inline void _ast_bridge_lock(struct ast_bridge *bridge, const char *file, const char *function, int line, const char *var)
00283 {
00284    __ao2_lock(bridge, AO2_LOCK_REQ_MUTEX, file, function, line, var);
00285 }
00286 
00287 /*!
00288  * \brief Unlock the bridge.
00289  *
00290  * \param bridge Bridge to unlock
00291  *
00292  * \return Nothing
00293  */
00294 #define ast_bridge_unlock(bridge)   _ast_bridge_unlock(bridge, __FILE__, __PRETTY_FUNCTION__, __LINE__, #bridge)
00295 static inline void _ast_bridge_unlock(struct ast_bridge *bridge, const char *file, const char *function, int line, const char *var)
00296 {
00297    __ao2_unlock(bridge, file, function, line, var);
00298 }
00299 
00300 /*! \brief See if it is possible to create a bridge
00301  *
00302  * \param capabilities The capabilities that the bridge will use
00303  *
00304  * \retval 1 if possible
00305  * \retval 0 if not possible
00306  *
00307  * Example usage:
00308  *
00309  * \code
00310  * int possible = ast_bridge_check(AST_BRIDGE_CAPABILITY_1TO1MIX);
00311  * \endcode
00312  *
00313  * This sees if it is possible to create a bridge capable of bridging two channels
00314  * together.
00315  */
00316 int ast_bridge_check(uint32_t capabilities);
00317 
00318 /*! \brief Destroy a bridge
00319  *
00320  * \param bridge Bridge to destroy
00321  *
00322  * \retval 0 on success
00323  * \retval -1 on failure
00324  *
00325  * Example usage:
00326  *
00327  * \code
00328  * ast_bridge_destroy(bridge);
00329  * \endcode
00330  *
00331  * This destroys a bridge that was previously created using ast_bridge_new.
00332  */
00333 int ast_bridge_destroy(struct ast_bridge *bridge);
00334 
00335 /*! \brief Join (blocking) a channel to a bridge
00336  *
00337  * \param bridge Bridge to join
00338  * \param chan Channel to join
00339  * \param swap Channel to swap out if swapping
00340  * \param features Bridge features structure
00341  * \param (Optional) Bridging tech optimization parameters for this channel.
00342  *
00343  * \retval state that channel exited the bridge with
00344  *
00345  * Example usage:
00346  *
00347  * \code
00348  * ast_bridge_join(bridge, chan, NULL, NULL);
00349  * \endcode
00350  *
00351  * This adds a channel pointed to by the chan pointer to the bridge pointed to by
00352  * the bridge pointer. This function will not return until the channel has been
00353  * removed from the bridge, swapped out for another channel, or has hung up.
00354  *
00355  * If this channel will be replacing another channel the other channel can be specified
00356  * in the swap parameter. The other channel will be thrown out of the bridge in an
00357  * atomic fashion.
00358  *
00359  * If channel specific features are enabled a pointer to the features structure
00360  * can be specified in the features parameter.
00361  */
00362 enum ast_bridge_channel_state ast_bridge_join(struct ast_bridge *bridge,
00363    struct ast_channel *chan,
00364    struct ast_channel *swap,
00365    struct ast_bridge_features *features,
00366    struct ast_bridge_tech_optimizations *tech_args);
00367 
00368 /*! \brief Impart (non-blocking) a channel on a bridge
00369  *
00370  * \param bridge Bridge to impart on
00371  * \param chan Channel to impart
00372  * \param swap Channel to swap out if swapping
00373  * \param features Bridge features structure
00374  * \param allow_hangup  Indicates if the bridge thread should manage hanging up of the channel or not.
00375  *
00376  * \retval 0 on success
00377  * \retval -1 on failure
00378  *
00379  * Example usage:
00380  *
00381  * \code
00382  * ast_bridge_impart(bridge, chan, NULL, NULL, 0);
00383  * \endcode
00384  *
00385  * This adds a channel pointed to by the chan pointer to the bridge pointed to by
00386  * the bridge pointer. This function will return immediately and will not wait
00387  * until the channel is no longer part of the bridge.
00388  *
00389  * If this channel will be replacing another channel the other channel can be specified
00390  * in the swap parameter. The other channel will be thrown out of the bridge in an
00391  * atomic fashion.
00392  *
00393  * If channel specific features are enabled a pointer to the features structure
00394  * can be specified in the features parameter.
00395  */
00396 int ast_bridge_impart(struct ast_bridge *bridge, struct ast_channel *chan, struct ast_channel *swap, struct ast_bridge_features *features, int allow_hangup);
00397 
00398 /*! \brief Depart a channel from a bridge
00399  *
00400  * \param bridge Bridge to depart from
00401  * \param chan Channel to depart
00402  *
00403  * \retval 0 on success
00404  * \retval -1 on failure
00405  *
00406  * Example usage:
00407  *
00408  * \code
00409  * ast_bridge_depart(bridge, chan);
00410  * \endcode
00411  *
00412  * This removes the channel pointed to by the chan pointer from the bridge
00413  * pointed to by the bridge pointer and gives control to the calling thread.
00414  * This does not hang up the channel.
00415  *
00416  * \note This API call can only be used on channels that were added to the bridge
00417  *       using the ast_bridge_impart API call.
00418  */
00419 int ast_bridge_depart(struct ast_bridge *bridge, struct ast_channel *chan);
00420 
00421 /*! \brief Remove a channel from a bridge
00422  *
00423  * \param bridge Bridge that the channel is to be removed from
00424  * \param chan Channel to remove
00425  *
00426  * \retval 0 on success
00427  * \retval -1 on failure
00428  *
00429  * Example usage:
00430  *
00431  * \code
00432  * ast_bridge_remove(bridge, chan);
00433  * \endcode
00434  *
00435  * This removes the channel pointed to by the chan pointer from the bridge
00436  * pointed to by the bridge pointer and requests that it be hung up. Control
00437  * over the channel will NOT be given to the calling thread.
00438  *
00439  * \note This API call can be used on channels that were added to the bridge
00440  *       using both ast_bridge_join and ast_bridge_impart.
00441  */
00442 int ast_bridge_remove(struct ast_bridge *bridge, struct ast_channel *chan);
00443 
00444 /*! \brief Merge two bridges together
00445  *
00446  * \param bridge0 First bridge
00447  * \param bridge1 Second bridge
00448  *
00449  * \retval 0 on success
00450  * \retval -1 on failure
00451  *
00452  * Example usage:
00453  *
00454  * \code
00455  * ast_bridge_merge(bridge0, bridge1);
00456  * \endcode
00457  *
00458  * This merges the bridge pointed to by bridge1 with the bridge pointed to by bridge0.
00459  * In reality all of the channels in bridge1 are simply moved to bridge0.
00460  *
00461  * \note The second bridge specified is not destroyed when this operation is
00462  *       completed.
00463  */
00464 int ast_bridge_merge(struct ast_bridge *bridge0, struct ast_bridge *bridge1);
00465 
00466 /*! \brief Suspend a channel temporarily from a bridge
00467  *
00468  * \param bridge Bridge to suspend the channel from
00469  * \param chan Channel to suspend
00470  *
00471  * \retval 0 on success
00472  * \retval -1 on failure
00473  *
00474  * Example usage:
00475  *
00476  * \code
00477  * ast_bridge_suspend(bridge, chan);
00478  * \endcode
00479  *
00480  * This suspends the channel pointed to by chan from the bridge pointed to by bridge temporarily.
00481  * Control of the channel is given to the calling thread. This differs from ast_bridge_depart as
00482  * the channel will not be removed from the bridge.
00483  *
00484  * \note This API call can be used on channels that were added to the bridge
00485  *       using both ast_bridge_join and ast_bridge_impart.
00486  */
00487 int ast_bridge_suspend(struct ast_bridge *bridge, struct ast_channel *chan);
00488 
00489 /*! \brief Unsuspend a channel from a bridge
00490  *
00491  * \param bridge Bridge to unsuspend the channel from
00492  * \param chan Channel to unsuspend
00493  *
00494  * \retval 0 on success
00495  * \retval -1 on failure
00496  *
00497  * Example usage:
00498  *
00499  * \code
00500  * ast_bridge_unsuspend(bridge, chan);
00501  * \endcode
00502  *
00503  * This unsuspends the channel pointed to by chan from the bridge pointed to by bridge.
00504  * The bridge will go back to handling the channel once this function returns.
00505  *
00506  * \note You must not mess with the channel once this function returns.
00507  *       Doing so may result in bad things happening.
00508  */
00509 int ast_bridge_unsuspend(struct ast_bridge *bridge, struct ast_channel *chan);
00510 
00511 /*! \brief Change the state of a bridged channel
00512  *
00513  * \param bridge_channel Channel to change the state on
00514  * \param new_state The new state to place the channel into
00515  *
00516  * Example usage:
00517  *
00518  * \code
00519  * ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_WAIT);
00520  * \endcode
00521  *
00522  * This places the channel pointed to by bridge_channel into the state
00523  * AST_BRIDGE_CHANNEL_STATE_WAIT.
00524  *
00525  * \note This API call is only meant to be used in feature hook callbacks to
00526  *       make sure the channel either hangs up or returns to the bridge.
00527  */
00528 void ast_bridge_change_state(struct ast_bridge_channel *bridge_channel, enum ast_bridge_channel_state new_state);
00529 
00530 /*! \brief Adjust the internal mixing sample rate of a bridge used during
00531  *         multimix mode.
00532  *
00533  * \param bridge_channel Channel to change the sample rate on.
00534  * \param sample rate, the sample rate to change to. If a
00535  *        value of 0 is passed here, the bridge will be free to pick
00536  *        what ever sample rate it chooses.
00537  *
00538  */
00539 void ast_bridge_set_internal_sample_rate(struct ast_bridge *bridge, unsigned int sample_rate);
00540 
00541 /*! \brief Adjust the internal mixing interval of a bridge used during
00542  *         multimix mode.
00543  *
00544  * \param bridge_channel Channel to change the sample rate on.
00545  * \param mixing_interval, the sample rate to change to.  If 0 is set
00546  * the bridge tech is free to choose any mixing interval it uses by default.
00547  */
00548 void ast_bridge_set_mixing_interval(struct ast_bridge *bridge, unsigned int mixing_interval);
00549 
00550 /*!
00551  * \brief Set a bridge to feed a single video source to all participants.
00552  */
00553 void ast_bridge_set_single_src_video_mode(struct ast_bridge *bridge, struct ast_channel *video_src_chan);
00554 
00555 /*!
00556  * \brief Set the bridge to pick the strongest talker supporting
00557  * video as the single source video feed
00558  */
00559 void ast_bridge_set_talker_src_video_mode(struct ast_bridge *bridge);
00560 
00561 /*!
00562  * \brief Update information about talker energy for talker src video mode.
00563  */
00564 void ast_bridge_update_talker_src_video_mode(struct ast_bridge *bridge, struct ast_channel *chan, int talker_energy, int is_keyfame);
00565 
00566 /*!
00567  * \brief Returns the number of video sources currently active in the bridge
00568  */
00569 int ast_bridge_number_video_src(struct ast_bridge *bridge);
00570 
00571 /*!
00572  * \brief Determine if a channel is a video src for the bridge
00573  *
00574  * \retval 0 Not a current video source of the bridge.
00575  * \retval None 0, is a video source of the bridge, The number
00576  *         returned represents the priority this video stream has
00577  *         on the bridge where 1 is the highest priority.
00578  */
00579 int ast_bridge_is_video_src(struct ast_bridge *bridge, struct ast_channel *chan);
00580 
00581 /*!
00582  * \brief remove a channel as a source of video for the bridge.
00583  */
00584 void ast_bridge_remove_video_src(struct ast_bridge *bridge, struct ast_channel *chan);
00585 
00586 #if defined(__cplusplus) || defined(c_plusplus)
00587 }
00588 #endif
00589 
00590 #endif /* _ASTERISK_BRIDGING_H */