bridge.h 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2007 - 2009, 2013 Digium, Inc.
  5. *
  6. * Richard Mudgett <rmudgett@digium.com>
  7. * Joshua Colp <jcolp@digium.com>
  8. *
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2. See the LICENSE file
  17. * at the top of the source tree.
  18. */
  19. /*!
  20. * \file
  21. * \brief Bridging API
  22. *
  23. * \author Richard Mudgett <rmudgett@digium.com>
  24. * \author Joshua Colp <jcolp@digium.com>
  25. * \ref AstBridging
  26. *
  27. * See Also:
  28. * \arg \ref AstCREDITS
  29. */
  30. /*!
  31. * \page AstBridging Bridging API
  32. *
  33. * The purpose of this API is to provide an easy and flexible way to bridge
  34. * channels of different technologies with different features.
  35. *
  36. * Bridging technologies provide the mechanism that do the actual handling
  37. * of frames between channels. They provide capability information, codec information,
  38. * and preference value to assist the bridging core in choosing a bridging technology when
  39. * creating a bridge. Different bridges may use different bridging technologies based on needs
  40. * but once chosen they all operate under the same premise; they receive frames and send frames.
  41. *
  42. * Bridges are a combination of bridging technology, channels, and features. A
  43. * developer creates a new bridge based on what they are currently expecting to do
  44. * with it or what they will do with it in the future. The bridging core determines what
  45. * available bridging technology will best fit the requirements and creates a new bridge.
  46. * Once created, channels can be added to the bridge in a blocking or non-blocking fashion.
  47. *
  48. * Features are such things as channel muting or DTMF based features such as attended transfer,
  49. * blind transfer, and hangup. Feature information must be set at the most granular level, on
  50. * the channel. While you can use features on a global scope the presence of a feature structure
  51. * on the channel will override the global scope. An example would be having the bridge muted
  52. * at global scope and attended transfer enabled on a channel. Since the channel itself is not muted
  53. * it would be able to speak.
  54. *
  55. * Feature hooks allow a developer to tell the bridging core that when a DTMF string
  56. * is received from a channel a callback should be called in their application. For
  57. * example, a conference bridge application may want to provide an IVR to control various
  58. * settings on the conference bridge. This can be accomplished by attaching a feature hook
  59. * that calls an IVR function when a DTMF string is entered.
  60. *
  61. */
  62. #ifndef _ASTERISK_BRIDGING_H
  63. #define _ASTERISK_BRIDGING_H
  64. #if defined(__cplusplus) || defined(c_plusplus)
  65. extern "C" {
  66. #endif
  67. #include "asterisk/bridge_features.h"
  68. #include "asterisk/bridge_channel.h"
  69. #include "asterisk/bridge_roles.h"
  70. #include "asterisk/dsp.h"
  71. #include "asterisk/uuid.h"
  72. struct ast_bridge_technology;
  73. struct ast_bridge;
  74. struct ast_bridge_tech_optimizations;
  75. /*! \brief Capabilities for a bridge technology */
  76. enum ast_bridge_capability {
  77. /*! Bridge technology can service calls on hold. */
  78. AST_BRIDGE_CAPABILITY_HOLDING = (1 << 0),
  79. /*! Bridge waits for channel to answer. Passes early media. (XXX Not supported yet) */
  80. AST_BRIDGE_CAPABILITY_EARLY = (1 << 1),
  81. /*! Bridge is capable of natively bridging two channels. (Smart bridge only) */
  82. AST_BRIDGE_CAPABILITY_NATIVE = (1 << 2),
  83. /*! Bridge is capable of mixing at most two channels. (Smart bridgeable) */
  84. AST_BRIDGE_CAPABILITY_1TO1MIX = (1 << 3),
  85. /*! Bridge is capable of mixing an arbitrary number of channels. (Smart bridgeable) */
  86. AST_BRIDGE_CAPABILITY_MULTIMIX = (1 << 4),
  87. };
  88. /*! \brief Video source modes */
  89. enum ast_bridge_video_mode_type {
  90. /*! Video is not allowed in the bridge */
  91. AST_BRIDGE_VIDEO_MODE_NONE = 0,
  92. /*! A single user is picked as the only distributed of video across the bridge */
  93. AST_BRIDGE_VIDEO_MODE_SINGLE_SRC,
  94. /*! A single user's video feed is distributed to all bridge channels, but
  95. * that feed is automatically picked based on who is talking the most. */
  96. AST_BRIDGE_VIDEO_MODE_TALKER_SRC,
  97. };
  98. /*! \brief This is used for both SINGLE_SRC mode to set what channel
  99. * should be the current single video feed */
  100. struct ast_bridge_video_single_src_data {
  101. /*! Only accept video coming from this channel */
  102. struct ast_channel *chan_vsrc;
  103. };
  104. /*! \brief This is used for both SINGLE_SRC_TALKER mode to set what channel
  105. * should be the current single video feed */
  106. struct ast_bridge_video_talker_src_data {
  107. /*! Only accept video coming from this channel */
  108. struct ast_channel *chan_vsrc;
  109. int average_talking_energy;
  110. /*! Current talker see's this person */
  111. struct ast_channel *chan_old_vsrc;
  112. };
  113. /*! \brief Data structure that defines a video source mode */
  114. struct ast_bridge_video_mode {
  115. enum ast_bridge_video_mode_type mode;
  116. /* Add data for all the video modes here. */
  117. union {
  118. struct ast_bridge_video_single_src_data single_src_data;
  119. struct ast_bridge_video_talker_src_data talker_src_data;
  120. } mode_data;
  121. };
  122. /*!
  123. * \brief Destroy the bridge.
  124. *
  125. * \param self Bridge to operate upon.
  126. *
  127. * \return Nothing
  128. */
  129. typedef void (*ast_bridge_destructor_fn)(struct ast_bridge *self);
  130. /*!
  131. * \brief The bridge is being dissolved.
  132. *
  133. * \param self Bridge to operate upon.
  134. *
  135. * \details
  136. * The bridge is being dissolved. Remove any external
  137. * references to the bridge so it can be destroyed.
  138. *
  139. * \note On entry, self must NOT be locked.
  140. *
  141. * \return Nothing
  142. */
  143. typedef void (*ast_bridge_dissolving_fn)(struct ast_bridge *self);
  144. /*!
  145. * \brief Push this channel into the bridge.
  146. *
  147. * \param self Bridge to operate upon.
  148. * \param bridge_channel Bridge channel to push.
  149. * \param swap Bridge channel to swap places with if not NULL.
  150. *
  151. * \details
  152. * Setup any channel hooks controlled by the bridge. Allocate
  153. * bridge_channel->bridge_pvt and initialize any resources put
  154. * in bridge_channel->bridge_pvt if needed. If there is a swap
  155. * channel, use it as a guide to setting up the bridge_channel.
  156. *
  157. * \note On entry, self is already locked.
  158. *
  159. * \retval 0 on success.
  160. * \retval -1 on failure. The channel did not get pushed.
  161. */
  162. typedef int (*ast_bridge_push_channel_fn)(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap);
  163. /*!
  164. * \brief Pull this channel from the bridge.
  165. *
  166. * \param self Bridge to operate upon.
  167. * \param bridge_channel Bridge channel to pull.
  168. *
  169. * \details
  170. * Remove any channel hooks controlled by the bridge. Release
  171. * any resources held by bridge_channel->bridge_pvt and release
  172. * bridge_channel->bridge_pvt.
  173. *
  174. * \note On entry, self is already locked.
  175. *
  176. * \return Nothing
  177. */
  178. typedef void (*ast_bridge_pull_channel_fn)(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel);
  179. /*!
  180. * \brief Notify the bridge that this channel was just masqueraded.
  181. *
  182. * \param self Bridge to operate upon.
  183. * \param bridge_channel Bridge channel that was masqueraded.
  184. *
  185. * \details
  186. * A masquerade just happened to this channel. The bridge needs
  187. * to re-evaluate this a channel in the bridge.
  188. *
  189. * \note On entry, self is already locked.
  190. *
  191. * \return Nothing
  192. */
  193. typedef void (*ast_bridge_notify_masquerade_fn)(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel);
  194. /*!
  195. * \brief Get the merge priority of this bridge.
  196. *
  197. * \param self Bridge to operate upon.
  198. *
  199. * \note On entry, self is already locked.
  200. *
  201. * \return Merge priority
  202. */
  203. typedef int (*ast_bridge_merge_priority_fn)(struct ast_bridge *self);
  204. /*!
  205. * \brief Bridge virtual methods table definition.
  206. *
  207. * \note Any changes to this struct must be reflected in
  208. * bridge_alloc() validity checking.
  209. */
  210. struct ast_bridge_methods {
  211. /*! Bridge class name for log messages. */
  212. const char *name;
  213. /*! Destroy the bridge. */
  214. ast_bridge_destructor_fn destroy;
  215. /*! The bridge is being dissolved. Remove any references to the bridge. */
  216. ast_bridge_dissolving_fn dissolving;
  217. /*! Push the bridge channel into the bridge. */
  218. ast_bridge_push_channel_fn push;
  219. /*! Pull the bridge channel from the bridge. */
  220. ast_bridge_pull_channel_fn pull;
  221. /*! Notify the bridge of a masquerade with the channel. */
  222. ast_bridge_notify_masquerade_fn notify_masquerade;
  223. /*! Get the bridge merge priority. */
  224. ast_bridge_merge_priority_fn get_merge_priority;
  225. /*! Peek at swap channel before it can hang up, prior to push. */
  226. ast_bridge_push_channel_fn push_peek;
  227. };
  228. /*! Softmix technology parameters. */
  229. struct ast_bridge_softmix {
  230. /*! The video mode softmix is using */
  231. struct ast_bridge_video_mode video_mode;
  232. /*!
  233. * \brief The internal sample rate softmix uses to mix channels.
  234. *
  235. * \note If this value is 0, the sofmix may auto adjust the mixing rate.
  236. */
  237. unsigned int internal_sample_rate;
  238. /*!
  239. * \brief The mixing interval indicates how quickly softmix
  240. * mixing should occur to mix audio.
  241. *
  242. * \note When set to 0, softmix must choose a default interval
  243. * for itself.
  244. */
  245. unsigned int internal_mixing_interval;
  246. };
  247. /*!
  248. * \brief Structure that contains information about a bridge
  249. */
  250. struct ast_bridge {
  251. /*! Bridge virtual method table. */
  252. const struct ast_bridge_methods *v_table;
  253. /*! "Personality" currently exhibited by bridge subclass */
  254. void *personality;
  255. /*! Bridge technology that is handling the bridge */
  256. struct ast_bridge_technology *technology;
  257. /*! Private information unique to the bridge technology */
  258. void *tech_pvt;
  259. /*! Per-bridge topics */
  260. struct stasis_cp_single *topics;
  261. /*! Call ID associated with the bridge */
  262. struct ast_callid *callid;
  263. /*! Linked list of channels participating in the bridge */
  264. AST_LIST_HEAD_NOLOCK(, ast_bridge_channel) channels;
  265. /*! Queue of actions to perform on the bridge. */
  266. AST_LIST_HEAD_NOLOCK(, ast_frame) action_queue;
  267. /*! Softmix technology parameters. */
  268. struct ast_bridge_softmix softmix;
  269. /*! Bridge flags to tweak behavior */
  270. struct ast_flags feature_flags;
  271. /*! Allowed bridge technology capabilities when AST_BRIDGE_FLAG_SMART enabled. */
  272. uint32_t allowed_capabilities;
  273. /*! Number of channels participating in the bridge */
  274. unsigned int num_channels;
  275. /*! Number of active channels in the bridge. */
  276. unsigned int num_active;
  277. /*! Number of channels with AST_BRIDGE_CHANNEL_FLAG_LONELY in the bridge. */
  278. unsigned int num_lonely;
  279. /*!
  280. * \brief Count of the active temporary requests to inhibit bridge merges.
  281. * Zero if merges are allowed.
  282. *
  283. * \note Temporary as in try again in a moment.
  284. */
  285. unsigned int inhibit_merge;
  286. /*! Cause code of the dissolved bridge. */
  287. int cause;
  288. /*! TRUE if the bridge was reconfigured. */
  289. unsigned int reconfigured:1;
  290. /*! TRUE if the bridge has been dissolved. Any channel that now tries to join is immediately ejected. */
  291. unsigned int dissolved:1;
  292. /*! TRUE if the bridge construction was completed. */
  293. unsigned int construction_completed:1;
  294. AST_DECLARE_STRING_FIELDS(
  295. /*! Immutable name of the creator for the bridge */
  296. AST_STRING_FIELD(creator);
  297. /*! Immutable name given to the bridge by its creator */
  298. AST_STRING_FIELD(name);
  299. /*! Immutable bridge UUID. */
  300. AST_STRING_FIELD(uniqueid);
  301. );
  302. };
  303. /*! \brief Bridge base class virtual method table. */
  304. extern struct ast_bridge_methods ast_bridge_base_v_table;
  305. /*!
  306. * \brief Create a new base class bridge
  307. *
  308. * \param capabilities The capabilities that we require to be used on the bridge
  309. * \param flags Flags that will alter the behavior of the bridge
  310. * \param creator Entity that created the bridge (optional)
  311. * \param name Name given to the bridge by its creator (optional, requires named creator)
  312. * \param id Unique ID given to the bridge by its creator (optional)
  313. *
  314. * \retval a pointer to a new bridge on success
  315. * \retval NULL on failure
  316. *
  317. * Example usage:
  318. *
  319. * \code
  320. * struct ast_bridge *bridge;
  321. * bridge = ast_bridge_base_new(AST_BRIDGE_CAPABILITY_1TO1MIX, AST_BRIDGE_FLAG_DISSOLVE_HANGUP);
  322. * \endcode
  323. *
  324. * This creates a no frills two party bridge that will be
  325. * destroyed once one of the channels hangs up.
  326. */
  327. struct ast_bridge *ast_bridge_base_new(uint32_t capabilities, unsigned int flags, const char *creator, const char *name, const char *id);
  328. /*!
  329. * \brief Try locking the bridge.
  330. *
  331. * \param bridge Bridge to try locking
  332. *
  333. * \retval 0 on success.
  334. * \retval non-zero on error.
  335. */
  336. #define ast_bridge_trylock(bridge) _ast_bridge_trylock(bridge, __FILE__, __PRETTY_FUNCTION__, __LINE__, #bridge)
  337. static inline int _ast_bridge_trylock(struct ast_bridge *bridge, const char *file, const char *function, int line, const char *var)
  338. {
  339. return __ao2_trylock(bridge, AO2_LOCK_REQ_MUTEX, file, function, line, var);
  340. }
  341. /*!
  342. * \brief Lock the bridge.
  343. *
  344. * \param bridge Bridge to lock
  345. *
  346. * \return Nothing
  347. */
  348. #define ast_bridge_lock(bridge) _ast_bridge_lock(bridge, __FILE__, __PRETTY_FUNCTION__, __LINE__, #bridge)
  349. static inline void _ast_bridge_lock(struct ast_bridge *bridge, const char *file, const char *function, int line, const char *var)
  350. {
  351. __ao2_lock(bridge, AO2_LOCK_REQ_MUTEX, file, function, line, var);
  352. }
  353. /*!
  354. * \brief Unlock the bridge.
  355. *
  356. * \param bridge Bridge to unlock
  357. *
  358. * \return Nothing
  359. */
  360. #define ast_bridge_unlock(bridge) _ast_bridge_unlock(bridge, __FILE__, __PRETTY_FUNCTION__, __LINE__, #bridge)
  361. static inline void _ast_bridge_unlock(struct ast_bridge *bridge, const char *file, const char *function, int line, const char *var)
  362. {
  363. __ao2_unlock(bridge, file, function, line, var);
  364. }
  365. /*! \brief Lock two bridges. */
  366. #define ast_bridge_lock_both(bridge1, bridge2) \
  367. do { \
  368. for (;;) { \
  369. ast_bridge_lock(bridge1); \
  370. if (!ast_bridge_trylock(bridge2)) { \
  371. break; \
  372. } \
  373. ast_bridge_unlock(bridge1); \
  374. sched_yield(); \
  375. } \
  376. } while (0)
  377. /*!
  378. * \brief Destroy a bridge
  379. *
  380. * \param bridge Bridge to destroy
  381. * \param cause Cause of bridge being destroyed. (If cause <= 0 then use AST_CAUSE_NORMAL_CLEARING)
  382. *
  383. * \retval 0 on success
  384. * \retval -1 on failure
  385. *
  386. * Example usage:
  387. *
  388. * \code
  389. * ast_bridge_destroy(bridge, AST_CAUSE_NORMAL_CLEARING);
  390. * \endcode
  391. *
  392. * This destroys a bridge that was previously created.
  393. *
  394. * \note
  395. * While this function will kick all channels out of the bridge, channels that
  396. * were added to the bridge using ast_bridge_impart() with the flag
  397. * AST_BRIDGE_IMPART_CHAN_DEPARTABLE set must have ast_bridge_depart() called
  398. * on them.
  399. */
  400. int ast_bridge_destroy(struct ast_bridge *bridge, int cause);
  401. /*!
  402. * \brief Notify bridging that this channel was just masqueraded.
  403. * \since 12.0.0
  404. *
  405. * \param chan Channel just involved in a masquerade
  406. *
  407. * \return Nothing
  408. */
  409. void ast_bridge_notify_masquerade(struct ast_channel *chan);
  410. enum ast_bridge_join_flags {
  411. /*! The bridge reference is being passed by the caller. */
  412. AST_BRIDGE_JOIN_PASS_REFERENCE = (1 << 0),
  413. /*! The initial bridge join does not cause a COLP exchange. */
  414. AST_BRIDGE_JOIN_INHIBIT_JOIN_COLP = (1 << 1),
  415. };
  416. /*!
  417. * \brief Join a channel to a bridge (blocking)
  418. *
  419. * \param bridge Bridge to join
  420. * \param chan Channel to join
  421. * \param swap Channel to swap out if swapping (A channel reference is stolen.)
  422. * \param features Bridge features structure
  423. * \param tech_args Optional Bridging tech optimization parameters for this channel.
  424. * \param flags defined by enum ast_bridge_join_flags.
  425. *
  426. * \note The passed in swap channel is always unreffed on return. It is not a
  427. * good idea to access the swap channel on return or for the caller to keep a
  428. * reference to it.
  429. *
  430. * \note Absolutely _NO_ locks should be held before calling
  431. * this function since it blocks.
  432. *
  433. * \retval 0 if the channel successfully joined the bridge before it exited.
  434. * \retval -1 if the channel failed to join the bridge
  435. *
  436. * Example usage:
  437. *
  438. * \code
  439. * ast_bridge_join(bridge, chan, NULL, NULL, NULL, AST_BRIDGE_JOIN_PASS_REFERENCE);
  440. * \endcode
  441. *
  442. * This adds a channel pointed to by the chan pointer to the bridge pointed to by
  443. * the bridge pointer. This function will not return until the channel has been
  444. * removed from the bridge, swapped out for another channel, or has hung up.
  445. *
  446. * If this channel will be replacing another channel the other channel can be specified
  447. * in the swap parameter. The other channel will be thrown out of the bridge in an
  448. * atomic fashion.
  449. *
  450. * If channel specific features are enabled a pointer to the features structure
  451. * can be specified in the features parameter.
  452. */
  453. int ast_bridge_join(struct ast_bridge *bridge,
  454. struct ast_channel *chan,
  455. struct ast_channel *swap,
  456. struct ast_bridge_features *features,
  457. struct ast_bridge_tech_optimizations *tech_args,
  458. enum ast_bridge_join_flags flags);
  459. enum ast_bridge_impart_flags {
  460. /*! Field describing what the caller can do with the channel after it is imparted. */
  461. AST_BRIDGE_IMPART_CHAN_MASK = (1 << 0),
  462. /*! The caller wants to reclaim the channel using ast_bridge_depart(). */
  463. AST_BRIDGE_IMPART_CHAN_DEPARTABLE = (0 << 0),
  464. /*! The caller is passing channel control entirely to the bridging system. */
  465. AST_BRIDGE_IMPART_CHAN_INDEPENDENT = (1 << 0),
  466. /*! The initial bridge join does not cause a COLP exchange. */
  467. AST_BRIDGE_IMPART_INHIBIT_JOIN_COLP = (1 << 1),
  468. };
  469. /*!
  470. * \brief Impart a channel to a bridge (non-blocking)
  471. *
  472. * \param bridge Bridge to impart on
  473. * \param chan Channel to impart (The channel reference is stolen if impart successful.)
  474. * \param swap Channel to swap out if swapping. NULL if not swapping.
  475. * \param features Bridge features structure.
  476. * \param flags defined by enum ast_bridge_impart_flags.
  477. *
  478. * \note The given bridge must be unlocked when calling this function.
  479. *
  480. * \note The features parameter must be NULL or obtained by
  481. * ast_bridge_features_new(). You must not dereference features
  482. * after calling even if the call fails.
  483. *
  484. * \note chan is locked by this function.
  485. *
  486. * \retval 0 on success
  487. * \retval -1 on failure (Caller still has ownership of chan)
  488. *
  489. * Example usage:
  490. *
  491. * \code
  492. * ast_bridge_impart(bridge, chan, NULL, NULL, AST_BRIDGE_IMPART_CHAN_INDEPENDENT);
  493. * \endcode
  494. *
  495. * \details
  496. * This adds a channel pointed to by the chan pointer to the
  497. * bridge pointed to by the bridge pointer. This function will
  498. * return immediately and will not wait until the channel is no
  499. * longer part of the bridge.
  500. *
  501. * If this channel will be replacing another channel the other
  502. * channel can be specified in the swap parameter. The other
  503. * channel will be thrown out of the bridge in an atomic
  504. * fashion.
  505. *
  506. * If channel specific features are enabled, a pointer to the
  507. * features structure can be specified in the features
  508. * parameter.
  509. *
  510. * \note If you impart a channel with
  511. * AST_BRIDGE_IMPART_CHAN_DEPARTABLE you MUST
  512. * ast_bridge_depart() the channel if this call succeeds. The
  513. * bridge channel thread is created join-able. The implication
  514. * is that the channel is special and will not behave like a
  515. * normal channel.
  516. *
  517. * \note If you impart a channel with
  518. * AST_BRIDGE_IMPART_CHAN_INDEPENDENT you must not
  519. * ast_bridge_depart() the channel. The bridge channel thread
  520. * is created non-join-able. The channel must be treated as if
  521. * it were placed into the bridge by ast_bridge_join().
  522. * Channels placed into a bridge by ast_bridge_join() are
  523. * removed by a third party using ast_bridge_remove().
  524. */
  525. int ast_bridge_impart(struct ast_bridge *bridge,
  526. struct ast_channel *chan,
  527. struct ast_channel *swap,
  528. struct ast_bridge_features *features,
  529. enum ast_bridge_impart_flags flags) attribute_warn_unused_result;
  530. /*!
  531. * \brief Depart a channel from a bridge
  532. *
  533. * \param chan Channel to depart
  534. *
  535. * \note chan is locked by this function.
  536. *
  537. * \retval 0 on success
  538. * \retval -1 on failure
  539. *
  540. * Example usage:
  541. *
  542. * \code
  543. * ast_bridge_depart(chan);
  544. * \endcode
  545. *
  546. * This removes the channel pointed to by the chan pointer from any bridge
  547. * it may be in and gives control to the calling thread.
  548. * This does not hang up the channel.
  549. *
  550. * \note This API call can only be used on channels that were added to the bridge
  551. * using the ast_bridge_impart API call with the AST_BRIDGE_IMPART_CHAN_DEPARTABLE
  552. * flag.
  553. */
  554. int ast_bridge_depart(struct ast_channel *chan);
  555. /*!
  556. * \brief Remove a channel from a bridge
  557. *
  558. * \param bridge Bridge that the channel is to be removed from
  559. * \param chan Channel to remove
  560. *
  561. * \retval 0 on success
  562. * \retval -1 on failure
  563. *
  564. * Example usage:
  565. *
  566. * \code
  567. * ast_bridge_remove(bridge, chan);
  568. * \endcode
  569. *
  570. * This removes the channel pointed to by the chan pointer from the bridge
  571. * pointed to by the bridge pointer and requests that it be hung up. Control
  572. * over the channel will NOT be given to the calling thread.
  573. *
  574. * \note This API call can be used on channels that were added to the bridge
  575. * using both ast_bridge_join and ast_bridge_impart.
  576. */
  577. int ast_bridge_remove(struct ast_bridge *bridge, struct ast_channel *chan);
  578. /*!
  579. * \brief Kick a channel from a bridge
  580. *
  581. * \param bridge Bridge that the channel is to be kicked from
  582. * \param chan Channel to kick
  583. *
  584. * \retval 0 on success
  585. * \retval -1 on failure
  586. *
  587. * Example usage:
  588. *
  589. * \code
  590. * ast_bridge_kick(bridge, chan);
  591. * \endcode
  592. *
  593. * \details
  594. * This kicks the channel pointed to by the chan pointer from
  595. * the bridge pointed to by the bridge pointer and requests that
  596. * it be hung up. Control over the channel will NOT be given to
  597. * the calling thread.
  598. *
  599. * \note The functional difference between ast_bridge_kick() and
  600. * ast_bridge_remove() is that the bridge may dissolve as a
  601. * result of the channel being kicked.
  602. *
  603. * \note This API call can be used on channels that were added
  604. * to the bridge using both ast_bridge_join and
  605. * ast_bridge_impart.
  606. */
  607. int ast_bridge_kick(struct ast_bridge *bridge, struct ast_channel *chan);
  608. /*!
  609. * \brief Merge two bridges together
  610. *
  611. * \param dst_bridge Destination bridge of merge.
  612. * \param src_bridge Source bridge of merge.
  613. * \param merge_best_direction TRUE if don't care about which bridge merges into the other.
  614. * \param kick_me Array of channels to kick from the bridges.
  615. * \param num_kick Number of channels in the kick_me array.
  616. *
  617. * \note Absolutely _NO_ bridge or channel locks should be held
  618. * before calling this function.
  619. *
  620. * \retval 0 on success
  621. * \retval -1 on failure
  622. *
  623. * Example usage:
  624. *
  625. * \code
  626. * ast_bridge_merge(dst_bridge, src_bridge, 0, NULL, 0);
  627. * \endcode
  628. *
  629. * This moves the channels in src_bridge into the bridge pointed
  630. * to by dst_bridge.
  631. */
  632. int ast_bridge_merge(struct ast_bridge *dst_bridge, struct ast_bridge *src_bridge, int merge_best_direction, struct ast_channel **kick_me, unsigned int num_kick);
  633. /*!
  634. * \brief Move a channel from one bridge to another.
  635. * \since 12.0.0
  636. *
  637. * \param dst_bridge Destination bridge of bridge channel move.
  638. * \param src_bridge Source bridge of bridge channel move.
  639. * \param chan Channel to move.
  640. * \param swap Channel to replace in dst_bridge.
  641. * \param attempt_recovery TRUE if failure attempts to push channel back into original bridge.
  642. *
  643. * \note Absolutely _NO_ bridge or channel locks should be held
  644. * before calling this function.
  645. *
  646. * \retval 0 on success.
  647. * \retval -1 on failure.
  648. */
  649. int ast_bridge_move(struct ast_bridge *dst_bridge, struct ast_bridge *src_bridge, struct ast_channel *chan, struct ast_channel *swap, int attempt_recovery);
  650. /*!
  651. * \brief Adjust the bridge merge inhibit request count.
  652. * \since 12.0.0
  653. *
  654. * \param bridge What to operate on.
  655. * \param request Inhibit request increment.
  656. * (Positive to add requests. Negative to remove requests.)
  657. *
  658. * \return Nothing
  659. */
  660. void ast_bridge_merge_inhibit(struct ast_bridge *bridge, int request);
  661. /*!
  662. * \brief Suspend a channel temporarily from a bridge
  663. *
  664. * \param bridge Bridge to suspend the channel from
  665. * \param chan Channel to suspend
  666. *
  667. * \retval 0 on success
  668. * \retval -1 on failure
  669. *
  670. * Example usage:
  671. *
  672. * \code
  673. * ast_bridge_suspend(bridge, chan);
  674. * \endcode
  675. *
  676. * This suspends the channel pointed to by chan from the bridge pointed to by bridge temporarily.
  677. * Control of the channel is given to the calling thread. This differs from ast_bridge_depart as
  678. * the channel will not be removed from the bridge.
  679. *
  680. * \note This API call can be used on channels that were added to the bridge
  681. * using both ast_bridge_join and ast_bridge_impart.
  682. */
  683. int ast_bridge_suspend(struct ast_bridge *bridge, struct ast_channel *chan);
  684. /*!
  685. * \brief Unsuspend a channel from a bridge
  686. *
  687. * \param bridge Bridge to unsuspend the channel from
  688. * \param chan Channel to unsuspend
  689. *
  690. * \retval 0 on success
  691. * \retval -1 on failure
  692. *
  693. * Example usage:
  694. *
  695. * \code
  696. * ast_bridge_unsuspend(bridge, chan);
  697. * \endcode
  698. *
  699. * This unsuspends the channel pointed to by chan from the bridge pointed to by bridge.
  700. * The bridge will go back to handling the channel once this function returns.
  701. *
  702. * \note You must not mess with the channel once this function returns.
  703. * Doing so may result in bad things happening.
  704. */
  705. int ast_bridge_unsuspend(struct ast_bridge *bridge, struct ast_channel *chan);
  706. /*!
  707. * \brief Sets BRIDGECHANNEL and BRIDGEPVTCALLID for a channel
  708. *
  709. * \pre chan must be locked before calling
  710. *
  711. * \param name channel name of the bridged peer
  712. * \param pvtid Private CallID of the bridged peer
  713. *
  714. * \return nothing
  715. */
  716. void ast_bridge_vars_set(struct ast_channel *chan, const char *name, const char *pvtid);
  717. struct ast_unreal_pvt;
  718. /*!
  719. * \brief Check and optimize out the unreal channels between bridges.
  720. * \since 12.0.0
  721. *
  722. * \param chan Unreal channel writing a frame into the channel driver.
  723. * \param peer Other unreal channel in the pair.
  724. * \param pvt Private data provided by an implementation of the unreal driver that
  725. * contains the callbacks that should be called when optimization begins/ends
  726. *
  727. * \note It is assumed that chan is already locked.
  728. *
  729. * \retval 0 if unreal channels were not optimized out.
  730. * \retval non-zero if unreal channels were optimized out.
  731. */
  732. int ast_bridge_unreal_optimize_out(struct ast_channel *chan, struct ast_channel *peer, struct ast_unreal_pvt *pvt);
  733. /*!
  734. * \brief Tells, if optimization is allowed, how the optimization would be performed
  735. */
  736. enum ast_bridge_optimization {
  737. /*! Optimization would swap peer into the chan_bridge */
  738. AST_BRIDGE_OPTIMIZE_SWAP_TO_CHAN_BRIDGE,
  739. /*! Optimization would swap chan into the peer_bridge */
  740. AST_BRIDGE_OPTIMIZE_SWAP_TO_PEER_BRIDGE,
  741. /*! Optimization would merge peer_bridge into chan_bridge */
  742. AST_BRIDGE_OPTIMIZE_MERGE_TO_CHAN_BRIDGE,
  743. /*! Optimization would merge chan_bridge into peer_bridge */
  744. AST_BRIDGE_OPTIMIZE_MERGE_TO_PEER_BRIDGE,
  745. /*! Optimization is not permitted on one or both bridges */
  746. AST_BRIDGE_OPTIMIZE_PROHIBITED,
  747. };
  748. /*!
  749. * \brief Determine if bridges allow for optimization to occur betweem them
  750. * \since 12.0.0
  751. *
  752. * \param chan_bridge First bridge being tested
  753. * \param peer_bridge Second bridge being tested
  754. *
  755. * This determines if two bridges allow for unreal channel optimization
  756. * to occur between them. The function does not require for unreal channels
  757. * to already be in the bridges when called.
  758. *
  759. * \note It is assumed that both bridges are locked prior to calling this function
  760. *
  761. * \note A return other than AST_BRIDGE_OPTIMIZE_PROHIBITED does not guarantee
  762. * that an optimization attempt will succeed. However, a return of
  763. * AST_BRIDGE_OPTIMIZE_PROHIBITED guarantees that an optimization attempt will
  764. * never succeed.
  765. *
  766. * \returns Optimization allowability for the bridges
  767. */
  768. enum ast_bridge_optimization ast_bridges_allow_optimization(struct ast_bridge *chan_bridge,
  769. struct ast_bridge *peer_bridge);
  770. /*!
  771. * \brief Put an action onto the specified bridge.
  772. * \since 12.0.0
  773. *
  774. * \param bridge What to queue the action on.
  775. * \param action What to do.
  776. *
  777. * \retval 0 on success.
  778. * \retval -1 on error.
  779. *
  780. * \note This API call is meant for internal bridging operations.
  781. */
  782. int ast_bridge_queue_action(struct ast_bridge *bridge, struct ast_frame *action);
  783. /*!
  784. * \brief Queue the given frame to everyone else.
  785. * \since 12.0.0
  786. *
  787. * \param bridge What bridge to distribute frame.
  788. * \param bridge_channel Channel to optionally not pass frame to. (NULL to pass to everyone)
  789. * \param frame Frame to pass.
  790. *
  791. * \note This is intended to be called by bridge hooks and
  792. * bridge technologies.
  793. *
  794. * \retval 0 Frame written to at least one channel.
  795. * \retval -1 Frame written to no channels.
  796. */
  797. int ast_bridge_queue_everyone_else(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame);
  798. /*!
  799. * \brief Adjust the internal mixing sample rate of a bridge
  800. * used during multimix mode.
  801. *
  802. * \param bridge Channel to change the sample rate on.
  803. * \param sample_rate the sample rate to change to. If a
  804. * value of 0 is passed here, the bridge will be free to pick
  805. * what ever sample rate it chooses.
  806. *
  807. */
  808. void ast_bridge_set_internal_sample_rate(struct ast_bridge *bridge, unsigned int sample_rate);
  809. /*!
  810. * \brief Adjust the internal mixing interval of a bridge used
  811. * during multimix mode.
  812. *
  813. * \param bridge Channel to change the sample rate on.
  814. * \param mixing_interval the sample rate to change to. If 0 is set
  815. * the bridge tech is free to choose any mixing interval it uses by default.
  816. */
  817. void ast_bridge_set_mixing_interval(struct ast_bridge *bridge, unsigned int mixing_interval);
  818. /*!
  819. * \brief Set a bridge to feed a single video source to all participants.
  820. */
  821. void ast_bridge_set_single_src_video_mode(struct ast_bridge *bridge, struct ast_channel *video_src_chan);
  822. /*!
  823. * \brief Set the bridge to pick the strongest talker supporting
  824. * video as the single source video feed
  825. */
  826. void ast_bridge_set_talker_src_video_mode(struct ast_bridge *bridge);
  827. /*!
  828. * \brief Update information about talker energy for talker src video mode.
  829. */
  830. void ast_bridge_update_talker_src_video_mode(struct ast_bridge *bridge, struct ast_channel *chan, int talker_energy, int is_keyfame);
  831. /*!
  832. * \brief Returns the number of video sources currently active in the bridge
  833. */
  834. int ast_bridge_number_video_src(struct ast_bridge *bridge);
  835. /*!
  836. * \brief Determine if a channel is a video src for the bridge
  837. *
  838. * \retval 0 Not a current video source of the bridge.
  839. * \retval None 0, is a video source of the bridge, The number
  840. * returned represents the priority this video stream has
  841. * on the bridge where 1 is the highest priority.
  842. */
  843. int ast_bridge_is_video_src(struct ast_bridge *bridge, struct ast_channel *chan);
  844. /*!
  845. * \brief remove a channel as a source of video for the bridge.
  846. */
  847. void ast_bridge_remove_video_src(struct ast_bridge *bridge, struct ast_channel *chan);
  848. /*!
  849. * \brief Converts an enum representation of a bridge video mode to string
  850. *
  851. * \param video_mode The video mode
  852. *
  853. * \retval A string representation of \c video_mode
  854. */
  855. const char *ast_bridge_video_mode_to_string(enum ast_bridge_video_mode_type video_mode);
  856. /*!
  857. * \brief Acquire the channel's bridge for transfer purposes.
  858. * \since 13.21.0
  859. *
  860. * \param chan Channel involved in a transfer.
  861. *
  862. * \return The bridge the channel is in or NULL if it either isn't
  863. * in a bridge or should not be considered to be in a bridge.
  864. */
  865. struct ast_bridge *ast_bridge_transfer_acquire_bridge(struct ast_channel *chan);
  866. enum ast_transfer_result {
  867. /*! The transfer completed successfully */
  868. AST_BRIDGE_TRANSFER_SUCCESS,
  869. /*! A bridge involved does not permit transferring */
  870. AST_BRIDGE_TRANSFER_NOT_PERMITTED,
  871. /*! The current bridge setup makes transferring an invalid operation */
  872. AST_BRIDGE_TRANSFER_INVALID,
  873. /*! The transfer operation failed for a miscellaneous reason */
  874. AST_BRIDGE_TRANSFER_FAIL,
  875. };
  876. enum ast_transfer_type {
  877. /*! Transfer of a single party */
  878. AST_BRIDGE_TRANSFER_SINGLE_PARTY,
  879. /*! Transfer of multiple parties */
  880. AST_BRIDGE_TRANSFER_MULTI_PARTY,
  881. };
  882. /*!
  883. * \brief AO2 object that wraps data for transfer_channel_cb
  884. */
  885. struct transfer_channel_data {
  886. void *data; /*! Data to be used by the transfer_channel_cb -- note that this
  887. * pointer is going to be pointing to something on the stack, so
  888. * it must not be used at any point after returning from the
  889. * transfer_channel_cb. */
  890. int completed; /*! Initially 0, This will be set to 1 by either the transfer
  891. * code or by transfer code hooks (e.g. parking) when the
  892. * transfer is completed and any remaining actions have taken
  893. * place (e.g. parking announcements). It will never be reset
  894. * to 0. This is used for deferring progress for channel
  895. * drivers that support deferred progress. */
  896. };
  897. /*!
  898. * \brief Callback function type called during blind transfers
  899. *
  900. * A caller of ast_bridge_transfer_blind() may wish to set data on
  901. * the channel that ends up running dialplan. For instance, it may
  902. * be useful to set channel variables on the channel.
  903. *
  904. * \param chan The involved channel
  905. * \param user_data User-provided data needed in the callback
  906. * \param transfer_type The type of transfer being completed
  907. */
  908. typedef void (*transfer_channel_cb)(struct ast_channel *chan, struct transfer_channel_data *user_data,
  909. enum ast_transfer_type transfer_type);
  910. /*!
  911. * \brief Blind transfer target to the extension and context provided
  912. *
  913. * The channel given is bridged to one or multiple channels. Depending on
  914. * the bridge and the number of participants, the entire bridge could be
  915. * transferred to the given destination, or a single channel may be redirected.
  916. *
  917. * Callers may also provide a callback to be called on the channel that will
  918. * be running dialplan. The user data passed into ast_bridge_transfer_blind
  919. * will be given as the argument to the callback to be interpreted as desired.
  920. * This callback is guaranteed to be called in the same thread as
  921. * ast_bridge_transfer_blind() and before ast_bridge_transfer_blind() returns.
  922. *
  923. * \note Absolutely _NO_ channel locks should be held before
  924. * calling this function.
  925. *
  926. * \param is_external Indicates that transfer was initiated externally
  927. * \param transferer The channel performing the blind transfer
  928. * \param exten The dialplan extension to send the call to
  929. * \param context The dialplan context to send the call to
  930. * \param new_channel_cb A callback to be called on the channel that will
  931. * be executing dialplan
  932. * \param user_data Argument for new_channel_cb
  933. * \return The success or failure result of the blind transfer
  934. */
  935. enum ast_transfer_result ast_bridge_transfer_blind(int is_external,
  936. struct ast_channel *transferer, const char *exten, const char *context,
  937. transfer_channel_cb new_channel_cb, void *user_data);
  938. /*!
  939. * \brief Attended transfer
  940. *
  941. * The two channels are both transferer channels. The first is the channel
  942. * that is bridged to the transferee (or if unbridged, the 'first' call of
  943. * the transfer). The second is the channel that is bridged to the transfer
  944. * target (or if unbridged, the 'second' call of the transfer).
  945. *
  946. * \note Absolutely _NO_ channel locks should be held before
  947. * calling this function.
  948. *
  949. * \param to_transferee Transferer channel on initial call (presumably bridged to transferee)
  950. * \param to_transfer_target Transferer channel on consultation call (presumably bridged to transfer target)
  951. * \return The success or failure of the attended transfer
  952. */
  953. enum ast_transfer_result ast_bridge_transfer_attended(struct ast_channel *to_transferee,
  954. struct ast_channel *to_transfer_target);
  955. /*!
  956. * \brief Set the relevant transfer variables for a single channel
  957. *
  958. * Sets either the ATTENDEDTRANSFER or BLINDTRANSFER variable for a channel while clearing
  959. * the opposite.
  960. *
  961. * \param chan Channel the variable is being set for
  962. * \param value Value the variable is being set to
  963. * \param is_attended false set BLINDTRANSFER and unset ATTENDEDTRANSFER
  964. * true set ATTENDEDTRANSFER and unset BLINDTRANSFER
  965. */
  966. void ast_bridge_set_transfer_variables(struct ast_channel *chan, const char *value, int is_attended);
  967. /*!
  968. * \brief Get a container of all channels in the bridge
  969. * \since 12.0.0
  970. *
  971. * \param bridge The bridge which is already locked.
  972. *
  973. * \retval NULL Failed to create container
  974. * \retval non-NULL Container of channels in the bridge
  975. */
  976. struct ao2_container *ast_bridge_peers_nolock(struct ast_bridge *bridge);
  977. /*!
  978. * \brief Get a container of all channels in the bridge
  979. * \since 12.0.0
  980. *
  981. * \param bridge The bridge
  982. *
  983. * \note The returned container is a snapshot of channels in the
  984. * bridge when called.
  985. *
  986. * \retval NULL Failed to create container
  987. * \retval non-NULL Container of channels in the bridge
  988. */
  989. struct ao2_container *ast_bridge_peers(struct ast_bridge *bridge);
  990. /*!
  991. * \brief Get the channel's bridge peer only if the bridge is two-party.
  992. * \since 12.0.0
  993. *
  994. * \param bridge The bridge which is already locked.
  995. * \param chan Channel desiring the bridge peer channel.
  996. *
  997. * \note The returned peer channel is the current peer in the
  998. * bridge when called.
  999. *
  1000. * \retval NULL Channel not in a bridge or the bridge is not two-party.
  1001. * \retval non-NULL Reffed peer channel at time of calling.
  1002. */
  1003. struct ast_channel *ast_bridge_peer_nolock(struct ast_bridge *bridge, struct ast_channel *chan);
  1004. /*!
  1005. * \brief Get the channel's bridge peer only if the bridge is two-party.
  1006. * \since 12.0.0
  1007. *
  1008. * \param bridge The bridge
  1009. * \param chan Channel desiring the bridge peer channel.
  1010. *
  1011. * \note The returned peer channel is the current peer in the
  1012. * bridge when called.
  1013. *
  1014. * \retval NULL Channel not in a bridge or the bridge is not two-party.
  1015. * \retval non-NULL Reffed peer channel at time of calling.
  1016. */
  1017. struct ast_channel *ast_bridge_peer(struct ast_bridge *bridge, struct ast_channel *chan);
  1018. /*!
  1019. * \brief Remove marked bridge channel feature hooks.
  1020. * \since 12.0.0
  1021. *
  1022. * \param features Bridge features structure
  1023. * \param flags Determinator for whether hook is removed.
  1024. *
  1025. * \return Nothing
  1026. */
  1027. void ast_bridge_features_remove(struct ast_bridge_features *features, enum ast_bridge_hook_remove_flags flags);
  1028. /*!
  1029. * \brief Find bridge by id
  1030. * \since 12.0.0
  1031. *
  1032. * \param bridge_id Bridge identifier
  1033. *
  1034. * \return NULL bridge not found
  1035. * \return non-NULL reference to bridge
  1036. */
  1037. struct ast_bridge *ast_bridge_find_by_id(const char *bridge_id);
  1038. #if defined(__cplusplus) || defined(c_plusplus)
  1039. }
  1040. #endif
  1041. #endif /* _ASTERISK_BRIDGING_H */