stasis_bridges.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013 Digium, Inc.
  5. *
  6. * Kinsey Moore <kmoore@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. #ifndef _STASIS_BRIDGING_H
  19. #define _STASIS_BRIDGING_H
  20. #if defined(__cplusplus) || defined(c_plusplus)
  21. extern "C" {
  22. #endif
  23. #include "asterisk/stringfields.h"
  24. #include "asterisk/utils.h"
  25. #include "asterisk/lock.h"
  26. #include "asterisk/linkedlists.h"
  27. #include "asterisk/channel.h"
  28. #include "asterisk/bridge.h"
  29. #include "asterisk/pbx.h"
  30. /*!
  31. * \brief Structure that contains a snapshot of information about a bridge
  32. */
  33. struct ast_bridge_snapshot {
  34. AST_DECLARE_STRING_FIELDS(
  35. /*! Immutable bridge UUID. */
  36. AST_STRING_FIELD(uniqueid);
  37. /*! Bridge technology that is handling the bridge */
  38. AST_STRING_FIELD(technology);
  39. /*! Bridge subclass that is handling the bridge */
  40. AST_STRING_FIELD(subclass);
  41. /*! Creator of the bridge */
  42. AST_STRING_FIELD(creator);
  43. /*! Name given to the bridge by its creator */
  44. AST_STRING_FIELD(name);
  45. );
  46. /*! AO2 container of bare channel uniqueid strings participating in the bridge.
  47. * Allocated from ast_str_container_alloc() */
  48. struct ao2_container *channels;
  49. /*! Bridge flags to tweak behavior */
  50. struct ast_flags feature_flags;
  51. /*! Bridge capabilities */
  52. uint32_t capabilities;
  53. /*! Number of channels participating in the bridge */
  54. unsigned int num_channels;
  55. /*! Number of active channels in the bridge. */
  56. unsigned int num_active;
  57. /*! The video mode of the bridge */
  58. enum ast_bridge_video_mode_type video_mode;
  59. /*! Unique ID of the channel providing video, if one exists */
  60. AST_STRING_FIELD_EXTENDED(video_source_id);
  61. };
  62. /*!
  63. * \since 12
  64. * \brief Generate a snapshot of the bridge state. This is an ao2 object, so
  65. * ao2_cleanup() to deallocate.
  66. *
  67. * \pre Bridge is locked
  68. *
  69. * \param bridge The bridge from which to generate a snapshot
  70. *
  71. * \retval AO2 refcounted snapshot on success
  72. * \retval NULL on error
  73. */
  74. struct ast_bridge_snapshot *ast_bridge_snapshot_create(struct ast_bridge *bridge);
  75. /*!
  76. * \since 12
  77. * \brief Message type for \ref ast_bridge_snapshot.
  78. *
  79. * \retval Message type for \ref ast_bridge_snapshot.
  80. */
  81. struct stasis_message_type *ast_bridge_snapshot_type(void);
  82. /*!
  83. * \since 12
  84. * \brief A topic which publishes the events for a particular bridge.
  85. *
  86. * If the given \a bridge is \c NULL, ast_bridge_topic_all() is returned.
  87. *
  88. * \param bridge Bridge for which to get a topic or \c NULL.
  89. *
  90. * \retval Topic for bridge's events.
  91. * \retval ast_bridge_topic_all() if \a bridge is \c NULL.
  92. */
  93. struct stasis_topic *ast_bridge_topic(struct ast_bridge *bridge);
  94. /*!
  95. * \since 12
  96. * \brief A topic which publishes the events for a particular bridge.
  97. *
  98. * \ref ast_bridge_snapshot messages are replaced with stasis_cache_update
  99. * messages.
  100. *
  101. * If the given \a bridge is \c NULL, ast_bridge_topic_all_cached() is returned.
  102. *
  103. * \param bridge Bridge for which to get a topic or \c NULL.
  104. *
  105. * \retval Topic for bridge's events.
  106. * \retval ast_bridge_topic_all() if \a bridge is \c NULL.
  107. */
  108. struct stasis_topic *ast_bridge_topic_cached(struct ast_bridge *bridge);
  109. /*!
  110. * \since 12
  111. * \brief A topic which publishes the events for all bridges.
  112. * \retval Topic for all bridge events.
  113. */
  114. struct stasis_topic *ast_bridge_topic_all(void);
  115. /*!
  116. * \since 12
  117. * \brief A caching topic which caches \ref ast_bridge_snapshot messages from
  118. * ast_bridge_events_all(void).
  119. *
  120. * \retval Caching topic for all bridge events.
  121. */
  122. struct stasis_topic *ast_bridge_topic_all_cached(void);
  123. /*!
  124. * \since 12
  125. * \brief Backend cache for ast_bridge_topic_all_cached().
  126. * \retval Cache of \ref ast_bridge_snapshot.
  127. */
  128. struct stasis_cache *ast_bridge_cache(void);
  129. /*!
  130. * \since 12
  131. * \brief Publish the state of a bridge
  132. *
  133. * \pre Bridge is locked
  134. *
  135. * \param bridge The bridge for which to publish state
  136. */
  137. void ast_bridge_publish_state(struct ast_bridge *bridge);
  138. /*! \brief Message representing the merge of two bridges */
  139. struct ast_bridge_merge_message {
  140. struct ast_bridge_snapshot *from; /*!< Bridge from which channels will be removed during the merge */
  141. struct ast_bridge_snapshot *to; /*!< Bridge to which channels will be added during the merge */
  142. };
  143. /*!
  144. * \since 12
  145. * \brief Message type for \ref ast_bridge_merge_message.
  146. *
  147. * \retval Message type for \ref ast_bridge_merge_message.
  148. */
  149. struct stasis_message_type *ast_bridge_merge_message_type(void);
  150. /*!
  151. * \since 12
  152. * \brief Publish a bridge merge
  153. *
  154. * \pre Bridges involved are locked
  155. *
  156. * \param to The bridge to which channels are being added
  157. * \param from The bridge from which channels are being removed
  158. */
  159. void ast_bridge_publish_merge(struct ast_bridge *to, struct ast_bridge *from);
  160. /*!
  161. * \since 12
  162. * \brief Blob of data associated with a bridge.
  163. *
  164. * The \c blob is actually a JSON object of structured data. It has a "type" field
  165. * which contains the type string describing this blob.
  166. */
  167. struct ast_bridge_blob {
  168. /*! Bridge blob is associated with (or NULL for global/all bridges) */
  169. struct ast_bridge_snapshot *bridge;
  170. /*! Channel blob is associated with (may be NULL for some messages) */
  171. struct ast_channel_snapshot *channel;
  172. /*! JSON blob of data */
  173. struct ast_json *blob;
  174. };
  175. /*!
  176. * \since 12
  177. * \brief Message type for \ref channel enter bridge blob messages.
  178. *
  179. * \retval Message type for \ref channel enter bridge blob messages.
  180. */
  181. struct stasis_message_type *ast_channel_entered_bridge_type(void);
  182. /*!
  183. * \since 12
  184. * \brief Message type for \ref channel leave bridge blob messages.
  185. *
  186. * \retval Message type for \ref channel leave bridge blob messages.
  187. */
  188. struct stasis_message_type *ast_channel_left_bridge_type(void);
  189. /*!
  190. * \since 12
  191. * \brief Creates a \ref ast_bridge_blob message.
  192. *
  193. * The \a blob JSON object requires a \c "type" field describing the blob. It
  194. * should also be treated as immutable and not modified after it is put into the
  195. * message.
  196. *
  197. * \pre bridge is locked.
  198. * \pre No channels are locked.
  199. *
  200. * \param bridge Channel blob is associated with, or NULL for global/all bridges.
  201. * \param blob JSON object representing the data.
  202. * \return \ref ast_bridge_blob message.
  203. * \return \c NULL on error
  204. */
  205. struct stasis_message *ast_bridge_blob_create(struct stasis_message_type *type,
  206. struct ast_bridge *bridge,
  207. struct ast_channel *chan,
  208. struct ast_json *blob);
  209. /*!
  210. * \since 12
  211. * \brief Publish a bridge channel enter event
  212. *
  213. * \pre bridge is locked.
  214. * \pre No channels are locked.
  215. *
  216. * \param bridge The bridge a channel entered
  217. * \param chan The channel that entered the bridge
  218. * \param swap The channel being swapped out of the bridge
  219. */
  220. void ast_bridge_publish_enter(struct ast_bridge *bridge, struct ast_channel *chan,
  221. struct ast_channel *swap);
  222. /*!
  223. * \since 12
  224. * \brief Publish a bridge channel leave event
  225. *
  226. * \pre bridge is locked.
  227. * \pre No channels are locked.
  228. *
  229. * \param bridge The bridge a channel left
  230. * \param chan The channel that left the bridge
  231. */
  232. void ast_bridge_publish_leave(struct ast_bridge *bridge, struct ast_channel *chan);
  233. /*!
  234. * \brief Build a JSON object from a \ref ast_bridge_snapshot.
  235. *
  236. * \param snapshot The bridge snapshot to convert to JSON
  237. * \param sanitize The message sanitizer to use on the snapshot
  238. *
  239. * \return JSON object representing bridge snapshot.
  240. * \return \c NULL on error
  241. */
  242. struct ast_json *ast_bridge_snapshot_to_json(const struct ast_bridge_snapshot *snapshot,
  243. const struct stasis_message_sanitizer *sanitize);
  244. /*!
  245. * \brief Pair showing a bridge snapshot and a specific channel snapshot belonging to the bridge
  246. */
  247. struct ast_bridge_channel_snapshot_pair {
  248. struct ast_bridge_snapshot *bridge_snapshot;
  249. struct ast_channel_snapshot *channel_snapshot;
  250. };
  251. /*!
  252. * \since 12
  253. * \brief Message type for \ref ast_blind_transfer_message.
  254. *
  255. * \retval Message type for \ref ast_blind_transfer_message.
  256. */
  257. struct stasis_message_type *ast_blind_transfer_type(void);
  258. /*!
  259. * \brief Message published during a blind transfer
  260. */
  261. struct ast_blind_transfer_message {
  262. /*! Result of the transfer */
  263. enum ast_transfer_result result;
  264. /*! True if the transfer was initiated by an external source (i.e. not DTMF-initiated) */
  265. int is_external;
  266. /*! The transferring channel */
  267. struct ast_channel_snapshot *transferer;
  268. /*! The bridge between the transferer and the transferee */
  269. struct ast_bridge_snapshot *bridge;
  270. /*! Destination context */
  271. char context[AST_MAX_CONTEXT];
  272. /*! Destination extension */
  273. char exten[AST_MAX_EXTENSION];
  274. /*! Transferee channel. NULL if there were multiple transferee channels */
  275. struct ast_channel_snapshot *transferee;
  276. /*! The channel replacing the transferer when multiple parties are being transferred */
  277. struct ast_channel_snapshot *replace_channel;
  278. };
  279. /*!
  280. * \brief Create a blind transfer message to be published
  281. *
  282. * \param is_external Whether the blind transfer was initiated externally (e.g. via AMI or native protocol)
  283. * \param transferer The transferer's channel that is bridged to the transferee
  284. * \param bridge The bridge the transferer and transferee are in
  285. * \param context The destination context for the blind transfer
  286. * \param exten The destination extension for the blind transfer
  287. *
  288. * \retval NULL Failure to allocate or create snapshots
  289. * \retval non-NULL The created blind transfer message
  290. */
  291. struct ast_blind_transfer_message *ast_blind_transfer_message_create(int is_external,
  292. struct ast_channel *transferer, const char *exten, const char *context);
  293. /*!
  294. * \brief Publish a blind transfer event
  295. *
  296. * \pre Bridges involved are locked. Channels involved are not locked.
  297. *
  298. * \param is_external Whether the blind transfer was initiated externally (e.g. via AMI or native protocol)
  299. * \param result The success or failure of the transfer
  300. * \param to_transferee The bridge between the transferer and transferee plus the transferer channel
  301. * \param context The destination context for the blind transfer
  302. * \param exten The destination extension for the blind transfer
  303. * \param transferee_channel If a single channel is being transferred, this is it. If
  304. * multiple parties are being transferred, this is NULL.
  305. * \param replace_channel If multiple parties are being transferred or the transfer
  306. * cannot reach across the bridge due to bridge flags, this is
  307. * the channel connecting their bridge to the destination.
  308. */
  309. void ast_bridge_publish_blind_transfer(struct ast_blind_transfer_message *transfer_message);
  310. enum ast_attended_transfer_dest_type {
  311. /*! The transfer failed, so there is no appropriate final state */
  312. AST_ATTENDED_TRANSFER_DEST_FAIL,
  313. /*! The transfer results in a single bridge remaining due to a merge or swap */
  314. AST_ATTENDED_TRANSFER_DEST_BRIDGE_MERGE,
  315. /*! The transfer results in a channel or bridge running an application */
  316. AST_ATTENDED_TRANSFER_DEST_APP,
  317. /*! The transfer results in a channel or bridge running an application via a local channel */
  318. AST_ATTENDED_TRANSFER_DEST_LOCAL_APP,
  319. /*! The transfer results in both bridges remaining with a local channel linking them */
  320. AST_ATTENDED_TRANSFER_DEST_LINK,
  321. /*! The transfer results in a threeway call between transferer, transferee, and transfer target */
  322. AST_ATTENDED_TRANSFER_DEST_THREEWAY,
  323. };
  324. /*!
  325. * \brief Message representing attended transfer
  326. */
  327. struct ast_attended_transfer_message {
  328. /*! Result of the attended transfer */
  329. enum ast_transfer_result result;
  330. /*! Indicates if the transfer was initiated externally*/
  331. int is_external;
  332. /*! Bridge between transferer <-> transferee and the transferer channel in that bridge. May be NULL */
  333. struct ast_bridge_channel_snapshot_pair to_transferee;
  334. /*! Bridge between transferer <-> transfer target and the transferer channel in that bridge. May be NULL */
  335. struct ast_bridge_channel_snapshot_pair to_transfer_target;
  336. /*! Local channel connecting transferee bridge to application */
  337. struct ast_channel_snapshot *replace_channel;
  338. /*! Transferee channel. Will be NULL if there were multiple channels transferred. */
  339. struct ast_channel_snapshot *transferee;
  340. /*! Transfer target channel. Will be NULL if there were multiple channels targeted. */
  341. struct ast_channel_snapshot *target;
  342. /*! Indicates the final state of the transfer */
  343. enum ast_attended_transfer_dest_type dest_type;
  344. union {
  345. /*! ID of the surviving bridge. Applicable for AST_ATTENDED_TRANSFER_DEST_BRIDGE_MERGE */
  346. char bridge[AST_UUID_STR_LEN];
  347. /*! Destination application of transfer. Applicable for AST_ATTENDED_TRANSFER_DEST_APP */
  348. char app[AST_MAX_APP];
  349. /*! Pair of local channels linking the bridges. Applicable for AST_ATTENDED_TRANSFER_DEST_LINK */
  350. struct ast_channel_snapshot *links[2];
  351. /*! Transferer channel and bridge that survived the transition to a threeway call. Applicable for AST_ATTENDED_TRANSFER_DEST_THREEWAY */
  352. struct ast_bridge_channel_snapshot_pair threeway;
  353. } dest;
  354. };
  355. /*!
  356. * \brief Create an Attended transfer message to be published.
  357. *
  358. * The parameters to this function are the basic necessities in order to create the
  359. * initial attended transfer message.
  360. *
  361. * The transferee and transfer_target parameters are optional. If not provided, then this
  362. * function will attempt to determine who the transferee and transfer target are based on
  363. * the input transferer channels and bridges. You typically will not need to provide an
  364. * explicit transferee and transfer target channel unless your attended transfer is implemented
  365. * in a strange way.
  366. *
  367. * \param is_external Non-zero if the transfer was initiated by a native channel driver protocol.
  368. * \param to_transferee The transferer channel that is bridged to the transferee channel.
  369. * \param transferee_bridge The bridge between the transferer and transferee. May be NULL.
  370. * \param to_transfer_target The transferer channel that is bridged to the transfer target.
  371. * \param target_bridge The bridge between the transferer and transfer target. May be NULL.
  372. * \param transferee The channel that is being transferred. Optional.
  373. * \param transfer_target The channel that is being transferred to. Optional.
  374. *
  375. * \retval NULL Failure to allocate or create snapshots
  376. * \retval non-NULL The created attended transfer message
  377. */
  378. struct ast_attended_transfer_message *ast_attended_transfer_message_create(
  379. int is_external, struct ast_channel *to_transferee, struct ast_bridge *transferee_bridge,
  380. struct ast_channel *to_transfer_target, struct ast_bridge *target_bridge,
  381. struct ast_channel *transferee, struct ast_channel *transfer_target);
  382. /*!
  383. * \brief Add details for a bridge merge to an attended transfer message.
  384. *
  385. * If the transfer is accomplished by a bridge merge (or swap optimization), then this should
  386. * be called on the created attended transfer message to have the appropriate details added on.
  387. *
  388. * \param transfer_msg The transfer message to add details to
  389. * \param final_bridge The bridge where the surviving parties reside
  390. *
  391. * \retval 0 Success
  392. * \retval -1 Failure
  393. */
  394. int ast_attended_transfer_message_add_merge(struct ast_attended_transfer_message *transfer_msg,
  395. struct ast_bridge *final_bridge);
  396. /*!
  397. * \brief Add details for an attended transfer that was resolved as a three-way call
  398. *
  399. * If the transfer results in a three-way call between the transferer, the transferee, and the
  400. * transfer target, then this should be called in order to add appropriate details to the
  401. * transfer message to be published.
  402. *
  403. * \param transfer_msg The message to add details to
  404. * \param survivor_channel The transferer channel that exists in the three-way call
  405. * \param survivor_bridge The bridge where the three-way call takes place.
  406. *
  407. * \retval 0 Success
  408. * \retval -1 Failure
  409. */
  410. int ast_attended_transfer_message_add_threeway(struct ast_attended_transfer_message *transfer_msg,
  411. struct ast_channel *survivor_channel, struct ast_bridge *survivor_bridge);
  412. /*!
  413. * \brief Add details for an attended transfer to an application
  414. *
  415. * If the transfer is sending one or more parties into an application, then this should be called
  416. * to add appropriate details to the transfer message being published.
  417. *
  418. * \param transfer_msg The message to add details to
  419. * \param app The name of the application that the parties are being transferred to
  420. * \param replace_channel The local channel that is in the bridge and running the application
  421. *
  422. * \retval 0 Success
  423. * \retval -1 Failure
  424. */
  425. int ast_attended_transfer_message_add_app(struct ast_attended_transfer_message *transfer_msg,
  426. const char *app, struct ast_channel *replace_channel);
  427. /*!
  428. * \brief Add details for an attended transfer that has a link between bridges.
  429. *
  430. * An attended transfer may be accomplished by linking two bridges together with local channels.
  431. * If this is how the transfer is to be completed, call this function in order to fill in details
  432. * about the transfer.
  433. *
  434. * \param transfer_msg The message to add details to.
  435. * \param locals An array of local channel halves that each are in one of the involved bridges.
  436. *
  437. * \retval 0 Success
  438. * \retval -1 Failure
  439. */
  440. int ast_attended_transfer_message_add_link(struct ast_attended_transfer_message *transfer_msg,
  441. struct ast_channel *locals[2]);
  442. /*!
  443. * \brief Publish an attended transfer
  444. *
  445. * \param transfer_msg The transfer message to publish
  446. */
  447. void ast_bridge_publish_attended_transfer(struct ast_attended_transfer_message *transfer_msg);
  448. /*!
  449. * \since 12
  450. * \brief Message type for \ref ast_attended_transfer_message.
  451. *
  452. * \retval Message type for \ref ast_attended_transfer_message.
  453. */
  454. struct stasis_message_type *ast_attended_transfer_type(void);
  455. /*!
  456. * \brief Returns the most recent snapshot for the bridge.
  457. *
  458. * The returned pointer is AO2 managed, so ao2_cleanup() when you're done.
  459. *
  460. * \param bridge_id Uniqueid of the bridge for which to get the snapshot.
  461. * \return Most recent snapshot. ao2_cleanup() when done.
  462. * \return \c NULL if channel isn't in cache.
  463. */
  464. struct ast_bridge_snapshot *ast_bridge_snapshot_get_latest(
  465. const char *bridge_id);
  466. /*!
  467. * \internal
  468. * \brief Initialize the topics for a single bridge.
  469. * \return 0 on success.
  470. * \return Non-zero on error.
  471. */
  472. int bridge_topics_init(struct ast_bridge *bridge);
  473. /*!
  474. * \internal
  475. * \brief Initialize the stasis bridging topic and message types
  476. * \retval 0 on success
  477. * \retval -1 on failure
  478. */
  479. int ast_stasis_bridging_init(void);
  480. #if defined(__cplusplus) || defined(c_plusplus)
  481. }
  482. #endif
  483. #endif /* _STASIS_BRIDGING_H */