2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013 Digium, Inc.
6 * Kinsey Moore <kmoore@digium.com>
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.
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.
19 #ifndef _STASIS_BRIDGING_H
20 #define _STASIS_BRIDGING_H
22 #if defined(__cplusplus) || defined(c_plusplus)
26 #include "asterisk/stringfields.h"
27 #include "asterisk/utils.h"
28 #include "asterisk/lock.h"
29 #include "asterisk/linkedlists.h"
30 #include "asterisk/channel.h"
31 #include "asterisk/bridging.h"
32 #include "asterisk/pbx.h"
35 * \brief Structure that contains a snapshot of information about a bridge
37 struct ast_bridge_snapshot {
38 AST_DECLARE_STRING_FIELDS(
39 /*! Immutable bridge UUID. */
40 AST_STRING_FIELD(uniqueid);
41 /*! Bridge technology that is handling the bridge */
42 AST_STRING_FIELD(technology);
43 /*! Bridge subclass that is handling the bridge */
44 AST_STRING_FIELD(subclass);
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;
61 * \brief Generate a snapshot of the bridge state. This is an ao2 object, so
62 * ao2_cleanup() to deallocate.
64 * \param bridge The bridge from which to generate a snapshot
66 * \retval AO2 refcounted snapshot on success
67 * \retval NULL on error
69 struct ast_bridge_snapshot *ast_bridge_snapshot_create(struct ast_bridge *bridge);
73 * \brief Message type for \ref ast_bridge_snapshot.
75 * \retval Message type for \ref ast_bridge_snapshot.
77 struct stasis_message_type *ast_bridge_snapshot_type(void);
81 * \brief A topic which publishes the events for a particular bridge.
83 * If the given \a bridge is \c NULL, ast_bridge_topic_all() is returned.
85 * \param bridge Bridge for which to get a topic or \c NULL.
87 * \retval Topic for bridge's events.
88 * \retval ast_bridge_topic_all() if \a bridge is \c NULL.
90 struct stasis_topic *ast_bridge_topic(struct ast_bridge *bridge);
94 * \brief A topic which publishes the events for all bridges.
95 * \retval Topic for all bridge events.
97 struct stasis_topic *ast_bridge_topic_all(void);
101 * \brief A caching topic which caches \ref ast_bridge_snapshot messages from
102 * ast_bridge_events_all(void).
104 * \retval Caching topic for all bridge events.
106 struct stasis_caching_topic *ast_bridge_topic_all_cached(void);
110 * \brief Publish the state of a bridge
112 * \param bridge The bridge for which to publish state
114 void ast_bridge_publish_state(struct ast_bridge *bridge);
116 /*! \brief Message representing the merge of two bridges */
117 struct ast_bridge_merge_message {
118 struct ast_bridge_snapshot *from; /*!< Bridge from which channels will be removed during the merge */
119 struct ast_bridge_snapshot *to; /*!< Bridge to which channels will be added during the merge */
124 * \brief Message type for \ref ast_bridge_merge_message.
126 * \retval Message type for \ref ast_bridge_merge_message.
128 struct stasis_message_type *ast_bridge_merge_message_type(void);
132 * \brief Publish a bridge merge
134 * \param to The bridge to which channels are being added
135 * \param from The bridge from which channels are being removed
137 void ast_bridge_publish_merge(struct ast_bridge *to, struct ast_bridge *from);
141 * \brief Blob of data associated with a bridge.
143 * The \c blob is actually a JSON object of structured data. It has a "type" field
144 * which contains the type string describing this blob.
146 struct ast_bridge_blob {
147 /*! Bridge blob is associated with (or NULL for global/all bridges) */
148 struct ast_bridge_snapshot *bridge;
149 /*! Channel blob is associated with (may be NULL for some messages) */
150 struct ast_channel_snapshot *channel;
151 /*! JSON blob of data */
152 struct ast_json *blob;
157 * \brief Message type for \ref channel enter bridge blob messages.
159 * \retval Message type for \ref channel enter bridge blob messages.
161 struct stasis_message_type *ast_channel_entered_bridge_type(void);
165 * \brief Message type for \ref channel leave bridge blob messages.
167 * \retval Message type for \ref channel leave bridge blob messages.
169 struct stasis_message_type *ast_channel_left_bridge_type(void);
173 * \brief Creates a \ref ast_bridge_blob message.
175 * The \a blob JSON object requires a \c "type" field describing the blob. It
176 * should also be treated as immutable and not modified after it is put into the
179 * \param bridge Channel blob is associated with, or NULL for global/all bridges.
180 * \param blob JSON object representing the data.
181 * \return \ref ast_bridge_blob message.
182 * \return \c NULL on error
184 struct stasis_message *ast_bridge_blob_create(struct stasis_message_type *type,
185 struct ast_bridge *bridge,
186 struct ast_channel *chan,
187 struct ast_json *blob);
191 * \brief Publish a bridge channel enter event
193 * \param bridge The bridge a channel entered
194 * \param chan The channel that entered the bridge
196 void ast_bridge_publish_enter(struct ast_bridge *bridge, struct ast_channel *chan);
200 * \brief Publish a bridge channel leave event
202 * \param bridge The bridge a channel left
203 * \param chan The channel that left the bridge
205 void ast_bridge_publish_leave(struct ast_bridge *bridge, struct ast_channel *chan);
208 * \brief Build a JSON object from a \ref ast_bridge_snapshot.
209 * \return JSON object representing bridge snapshot.
210 * \return \c NULL on error
212 struct ast_json *ast_bridge_snapshot_to_json(const struct ast_bridge_snapshot *snapshot);
215 * \brief Pair showing a bridge snapshot and a specific channel snapshot belonging to the bridge
217 struct ast_bridge_channel_snapshot_pair {
218 struct ast_bridge_snapshot *bridge_snapshot;
219 struct ast_channel_snapshot *channel_snapshot;
223 * \brief Pair showing a bridge and a specific channel belonging to the bridge
225 struct ast_bridge_channel_pair {
226 struct ast_bridge *bridge;
227 struct ast_channel *channel;
232 * \brief Message type for \ref ast_blind_transfer_message.
234 * \retval Message type for \ref ast_blind_transfer_message.
236 struct stasis_message_type *ast_blind_transfer_type(void);
239 * \brief Publish a blind transfer event
241 * \param is_external Whether the blind transfer was initiated externally (e.g. via AMI or native protocol)
242 * \param result The success or failure of the transfer
243 * \param to_transferee The bridge between the transferer and transferee plus the transferer channel
244 * \param context The destination context for the blind transfer
245 * \param exten The destination extension for the blind transfer
247 void ast_bridge_publish_blind_transfer(int is_external, enum ast_transfer_result result,
248 struct ast_bridge_channel_pair *to_transferee, const char *context, const char *exten);
250 enum ast_attended_transfer_dest_type {
251 /*! The transfer failed, so there is no appropriate final state */
252 AST_ATTENDED_TRANSFER_DEST_FAIL,
253 /*! The transfer results in a single bridge remaining due to a merge or swap */
254 AST_ATTENDED_TRANSFER_DEST_BRIDGE_MERGE,
255 /*! The transfer results in a channel or bridge running an application */
256 AST_ATTENDED_TRANSFER_DEST_APP,
257 /*! The transfer results in both bridges remaining with a local channel linking them */
258 AST_ATTENDED_TRANSFER_DEST_LINK,
262 * \brief Message representing attended transfer
264 struct ast_attended_transfer_message {
265 /*! Result of the attended transfer */
266 enum ast_transfer_result result;
267 /*! Indicates if the transfer was initiated externally*/
269 /*! Bridge between transferer <-> transferee and the transferer channel in that bridge. May be NULL */
270 struct ast_bridge_channel_snapshot_pair to_transferee;
271 /*! Bridge between transferer <-> transfer target and the transferer channel in that bridge. May be NULL */
272 struct ast_bridge_channel_snapshot_pair to_transfer_target;
273 /*! Indicates the final state of the transfer */
274 enum ast_attended_transfer_dest_type dest_type;
276 /*! ID of the surviving bridge. Applicable for AST_ATTENDED_TRANSFER_DEST_BRIDGE_MERGE */
277 char bridge[AST_UUID_STR_LEN];
278 /*! Destination application of transfer. Applicable for AST_ATTENDED_TRANSFER_DEST_APP */
279 char app[AST_MAX_APP];
280 /*! Pair of local channels linking the bridges. Applicable for AST_ATTENDED_TRANSFER_DEST_LINK */
281 struct ast_channel_snapshot *links[2];
287 * \brief Message type for \ref ast_attended_transfer_message.
289 * \retval Message type for \ref ast_attended_transfer_message.
291 struct stasis_message_type *ast_attended_transfer_type(void);
295 * \brief Publish an attended transfer failure
297 * Publish an \ref ast_attended_transfer_message with the dest_type set to
298 * \c AST_ATTENDED_TRANSFER_DEST_FAIL.
300 * \param is_external Indicates if the transfer was initiated externally
301 * \param result The result of the transfer. Will always be a type of failure.
302 * \param transferee The bridge between the transferer and transferees as well as the transferer channel from that bridge
303 * \param target The bridge between the transferer and transfer targets as well as the transferer channel from that bridge
305 void ast_bridge_publish_attended_transfer_fail(int is_external, enum ast_transfer_result result,
306 struct ast_bridge_channel_pair *transferee, struct ast_bridge_channel_pair *target);
310 * \brief Publish an attended transfer that results in two bridges becoming one.
312 * Publish an \ref ast_attended_transfer_message with the dest_type set to
313 * \c AST_ATTENDED_TRANSFER_DEST_BRIDGE_MERGE. This type of attended transfer results from
314 * having two bridges involved and either
316 * \li Merging the two bridges together
317 * \li Moving a channel from one bridge to the other, thus emptying a bridge
319 * In either case, two bridges enter, one leaves.
321 * \param is_external Indicates if the transfer was initiated externally
322 * \param result The result of the transfer.
323 * \param transferee The bridge between the transferer and transferees as well as the transferer channel from that bridge
324 * \param target The bridge between the transferer and transfer targets as well as the transferer channel from that bridge
325 * \param final_bridge The bridge that the parties end up in. Will be a bridge from the transferee or target pair.
327 void ast_bridge_publish_attended_transfer_bridge_merge(int is_external, enum ast_transfer_result result,
328 struct ast_bridge_channel_pair *transferee, struct ast_bridge_channel_pair *target,
329 struct ast_bridge *final_bridge);
333 * \brief Publish an attended transfer that results in an application being run
335 * Publish an \ref ast_attended_transfer_message with the dest_type set to
336 * \c AST_ATTENDED_TRANSFER_DEST_APP. This occurs when an attended transfer
339 * \li A transferee channel leaving a bridge to run an app
340 * \li A bridge of transferees running an app (via a local channel)
342 * \param is_external Indicates if the transfer was initiated externally
343 * \param result The result of the transfer.
344 * \param transferee The bridge between the transferer and transferees as well as the transferer channel from that bridge
345 * \param target The bridge between the transferer and transfer targets as well as the transferer channel from that bridge
346 * \param dest_app The application that the channel or bridge is running upon transfer completion.
348 void ast_bridge_publish_attended_transfer_app(int is_external, enum ast_transfer_result result,
349 struct ast_bridge_channel_pair *transferee, struct ast_bridge_channel_pair *target,
350 const char *dest_app);
354 * \brief Publish an attended transfer that results in two bridges linked by a local channel
356 * Publish an \ref ast_attended_transfer_message with the dest_type set to
357 * \c AST_ATTENDED_TRANSFER_DEST_LINK. This occurs when two bridges are involved
358 * in an attended transfer, but their properties do not allow for the bridges to
359 * merge or to have channels moved off of the bridge. An example of this occurs when
360 * attempting to transfer a ConfBridge to another bridge.
362 * When this type of transfer occurs, the two bridges continue to exist after the
363 * transfer and a local channel is used to link the two bridges together.
365 * \param is_external Indicates if the transfer was initiated externally
366 * \param result The result of the transfer.
367 * \param transferee The bridge between the transferer and transferees as well as the transferer channel from that bridge
368 * \param target The bridge between the transferer and transfer targets as well as the transferer channel from that bridge
369 * \param locals The local channels linking the bridges together.
371 void ast_bridge_publish_attended_transfer_link(int is_external, enum ast_transfer_result result,
372 struct ast_bridge_channel_pair *transferee, struct ast_bridge_channel_pair *target,
373 struct ast_channel *locals[2]);
376 * \brief Returns the most recent snapshot for the bridge.
378 * The returned pointer is AO2 managed, so ao2_cleanup() when you're done.
380 * \param bridge_id Uniqueid of the bridge for which to get the snapshot.
381 * \return Most recent snapshot. ao2_cleanup() when done.
382 * \return \c NULL if channel isn't in cache.
384 struct ast_bridge_snapshot *ast_bridge_snapshot_get_latest(
385 const char *bridge_id);
388 * \brief Initialize the stasis bridging topic and message types
389 * \retval 0 on success
390 * \retval -1 on failure
392 int ast_stasis_bridging_init(void);
394 #if defined(__cplusplus) || defined(c_plusplus)
398 #endif /* _STASIS_BRIDGING_H */