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 /*! \brief See if it is possible to create a bridge 00275 * 00276 * \param capabilities The capabilities that the bridge will use 00277 * 00278 * \retval 1 if possible 00279 * \retval 0 if not possible 00280 * 00281 * Example usage: 00282 * 00283 * \code 00284 * int possible = ast_bridge_check(AST_BRIDGE_CAPABILITY_1TO1MIX); 00285 * \endcode 00286 * 00287 * This sees if it is possible to create a bridge capable of bridging two channels 00288 * together. 00289 */ 00290 int ast_bridge_check(uint32_t capabilities); 00291 00292 /*! \brief Destroy a bridge 00293 * 00294 * \param bridge Bridge to destroy 00295 * 00296 * \retval 0 on success 00297 * \retval -1 on failure 00298 * 00299 * Example usage: 00300 * 00301 * \code 00302 * ast_bridge_destroy(bridge); 00303 * \endcode 00304 * 00305 * This destroys a bridge that was previously created using ast_bridge_new. 00306 */ 00307 int ast_bridge_destroy(struct ast_bridge *bridge); 00308 00309 /*! \brief Join (blocking) a channel to a bridge 00310 * 00311 * \param bridge Bridge to join 00312 * \param chan Channel to join 00313 * \param swap Channel to swap out if swapping 00314 * \param features Bridge features structure 00315 * \param (Optional) Bridging tech optimization parameters for this channel. 00316 * 00317 * \retval state that channel exited the bridge with 00318 * 00319 * Example usage: 00320 * 00321 * \code 00322 * ast_bridge_join(bridge, chan, NULL, NULL); 00323 * \endcode 00324 * 00325 * This adds a channel pointed to by the chan pointer to the bridge pointed to by 00326 * the bridge pointer. This function will not return until the channel has been 00327 * removed from the bridge, swapped out for another channel, or has hung up. 00328 * 00329 * If this channel will be replacing another channel the other channel can be specified 00330 * in the swap parameter. The other channel will be thrown out of the bridge in an 00331 * atomic fashion. 00332 * 00333 * If channel specific features are enabled a pointer to the features structure 00334 * can be specified in the features parameter. 00335 */ 00336 enum ast_bridge_channel_state ast_bridge_join(struct ast_bridge *bridge, 00337 struct ast_channel *chan, 00338 struct ast_channel *swap, 00339 struct ast_bridge_features *features, 00340 struct ast_bridge_tech_optimizations *tech_args); 00341 00342 /*! \brief Impart (non-blocking) a channel on a bridge 00343 * 00344 * \param bridge Bridge to impart on 00345 * \param chan Channel to impart 00346 * \param swap Channel to swap out if swapping 00347 * \param features Bridge features structure 00348 * \param allow_hangup Indicates if the bridge thread should manage hanging up of the channel or not. 00349 * 00350 * \retval 0 on success 00351 * \retval -1 on failure 00352 * 00353 * Example usage: 00354 * 00355 * \code 00356 * ast_bridge_impart(bridge, chan, NULL, NULL, 0); 00357 * \endcode 00358 * 00359 * This adds a channel pointed to by the chan pointer to the bridge pointed to by 00360 * the bridge pointer. This function will return immediately and will not wait 00361 * until the channel is no longer part of the bridge. 00362 * 00363 * If this channel will be replacing another channel the other channel can be specified 00364 * in the swap parameter. The other channel will be thrown out of the bridge in an 00365 * atomic fashion. 00366 * 00367 * If channel specific features are enabled a pointer to the features structure 00368 * can be specified in the features parameter. 00369 */ 00370 int ast_bridge_impart(struct ast_bridge *bridge, struct ast_channel *chan, struct ast_channel *swap, struct ast_bridge_features *features, int allow_hangup); 00371 00372 /*! \brief Depart a channel from a bridge 00373 * 00374 * \param bridge Bridge to depart from 00375 * \param chan Channel to depart 00376 * 00377 * \retval 0 on success 00378 * \retval -1 on failure 00379 * 00380 * Example usage: 00381 * 00382 * \code 00383 * ast_bridge_depart(bridge, chan); 00384 * \endcode 00385 * 00386 * This removes the channel pointed to by the chan pointer from the bridge 00387 * pointed to by the bridge pointer and gives control to the calling thread. 00388 * This does not hang up the channel. 00389 * 00390 * \note This API call can only be used on channels that were added to the bridge 00391 * using the ast_bridge_impart API call. 00392 */ 00393 int ast_bridge_depart(struct ast_bridge *bridge, struct ast_channel *chan); 00394 00395 /*! \brief Remove a channel from a bridge 00396 * 00397 * \param bridge Bridge that the channel is to be removed from 00398 * \param chan Channel to remove 00399 * 00400 * \retval 0 on success 00401 * \retval -1 on failure 00402 * 00403 * Example usage: 00404 * 00405 * \code 00406 * ast_bridge_remove(bridge, chan); 00407 * \endcode 00408 * 00409 * This removes the channel pointed to by the chan pointer from the bridge 00410 * pointed to by the bridge pointer and requests that it be hung up. Control 00411 * over the channel will NOT be given to the calling thread. 00412 * 00413 * \note This API call can be used on channels that were added to the bridge 00414 * using both ast_bridge_join and ast_bridge_impart. 00415 */ 00416 int ast_bridge_remove(struct ast_bridge *bridge, struct ast_channel *chan); 00417 00418 /*! \brief Merge two bridges together 00419 * 00420 * \param bridge0 First bridge 00421 * \param bridge1 Second bridge 00422 * 00423 * \retval 0 on success 00424 * \retval -1 on failure 00425 * 00426 * Example usage: 00427 * 00428 * \code 00429 * ast_bridge_merge(bridge0, bridge1); 00430 * \endcode 00431 * 00432 * This merges the bridge pointed to by bridge1 with the bridge pointed to by bridge0. 00433 * In reality all of the channels in bridge1 are simply moved to bridge0. 00434 * 00435 * \note The second bridge specified is not destroyed when this operation is 00436 * completed. 00437 */ 00438 int ast_bridge_merge(struct ast_bridge *bridge0, struct ast_bridge *bridge1); 00439 00440 /*! \brief Suspend a channel temporarily from a bridge 00441 * 00442 * \param bridge Bridge to suspend the channel from 00443 * \param chan Channel to suspend 00444 * 00445 * \retval 0 on success 00446 * \retval -1 on failure 00447 * 00448 * Example usage: 00449 * 00450 * \code 00451 * ast_bridge_suspend(bridge, chan); 00452 * \endcode 00453 * 00454 * This suspends the channel pointed to by chan from the bridge pointed to by bridge temporarily. 00455 * Control of the channel is given to the calling thread. This differs from ast_bridge_depart as 00456 * the channel will not be removed from the bridge. 00457 * 00458 * \note This API call can be used on channels that were added to the bridge 00459 * using both ast_bridge_join and ast_bridge_impart. 00460 */ 00461 int ast_bridge_suspend(struct ast_bridge *bridge, struct ast_channel *chan); 00462 00463 /*! \brief Unsuspend a channel from a bridge 00464 * 00465 * \param bridge Bridge to unsuspend the channel from 00466 * \param chan Channel to unsuspend 00467 * 00468 * \retval 0 on success 00469 * \retval -1 on failure 00470 * 00471 * Example usage: 00472 * 00473 * \code 00474 * ast_bridge_unsuspend(bridge, chan); 00475 * \endcode 00476 * 00477 * This unsuspends the channel pointed to by chan from the bridge pointed to by bridge. 00478 * The bridge will go back to handling the channel once this function returns. 00479 * 00480 * \note You must not mess with the channel once this function returns. 00481 * Doing so may result in bad things happening. 00482 */ 00483 int ast_bridge_unsuspend(struct ast_bridge *bridge, struct ast_channel *chan); 00484 00485 /*! \brief Change the state of a bridged channel 00486 * 00487 * \param bridge_channel Channel to change the state on 00488 * \param new_state The new state to place the channel into 00489 * 00490 * Example usage: 00491 * 00492 * \code 00493 * ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_WAIT); 00494 * \endcode 00495 * 00496 * This places the channel pointed to by bridge_channel into the state 00497 * AST_BRIDGE_CHANNEL_STATE_WAIT. 00498 * 00499 * \note This API call is only meant to be used in feature hook callbacks to 00500 * make sure the channel either hangs up or returns to the bridge. 00501 */ 00502 void ast_bridge_change_state(struct ast_bridge_channel *bridge_channel, enum ast_bridge_channel_state new_state); 00503 00504 /*! \brief Adjust the internal mixing sample rate of a bridge used during 00505 * multimix mode. 00506 * 00507 * \param bridge_channel Channel to change the sample rate on. 00508 * \param sample rate, the sample rate to change to. If a 00509 * value of 0 is passed here, the bridge will be free to pick 00510 * what ever sample rate it chooses. 00511 * 00512 */ 00513 void ast_bridge_set_internal_sample_rate(struct ast_bridge *bridge, unsigned int sample_rate); 00514 00515 /*! \brief Adjust the internal mixing interval of a bridge used during 00516 * multimix mode. 00517 * 00518 * \param bridge_channel Channel to change the sample rate on. 00519 * \param mixing_interval, the sample rate to change to. If 0 is set 00520 * the bridge tech is free to choose any mixing interval it uses by default. 00521 */ 00522 void ast_bridge_set_mixing_interval(struct ast_bridge *bridge, unsigned int mixing_interval); 00523 00524 /*! 00525 * \brief Set a bridge to feed a single video source to all participants. 00526 */ 00527 void ast_bridge_set_single_src_video_mode(struct ast_bridge *bridge, struct ast_channel *video_src_chan); 00528 00529 /*! 00530 * \brief Set the bridge to pick the strongest talker supporting 00531 * video as the single source video feed 00532 */ 00533 void ast_bridge_set_talker_src_video_mode(struct ast_bridge *bridge); 00534 00535 /*! 00536 * \brief Update information about talker energy for talker src video mode. 00537 */ 00538 void ast_bridge_update_talker_src_video_mode(struct ast_bridge *bridge, struct ast_channel *chan, int talker_energy, int is_keyfame); 00539 00540 /*! 00541 * \brief Returns the number of video sources currently active in the bridge 00542 */ 00543 int ast_bridge_number_video_src(struct ast_bridge *bridge); 00544 00545 /*! 00546 * \brief Determine if a channel is a video src for the bridge 00547 * 00548 * \retval 0 Not a current video source of the bridge. 00549 * \retval None 0, is a video source of the bridge, The number 00550 * returned represents the priority this video stream has 00551 * on the bridge where 1 is the highest priority. 00552 */ 00553 int ast_bridge_is_video_src(struct ast_bridge *bridge, struct ast_channel *chan); 00554 00555 /*! 00556 * \brief remove a channel as a source of video for the bridge. 00557 */ 00558 void ast_bridge_remove_video_src(struct ast_bridge *bridge, struct ast_channel *chan); 00559 00560 #if defined(__cplusplus) || defined(c_plusplus) 00561 } 00562 #endif 00563 00564 #endif /* _ASTERISK_BRIDGING_H */
1.5.6