2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2007 - 2009, Digium, Inc.
6 * Joshua Colp <jcolp@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.
21 * \brief Bridging Channel API
23 * \author Joshua Colp <jcolp@digium.com>
24 * \author Richard Mudgett <rmudgett@digium.com>
25 * \author Matt Jordan <mjordan@digium.com>
30 <support_level>core</support_level>
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38 #include <semaphore.h>
40 #include "asterisk/heap.h"
41 #include "asterisk/astobj2.h"
42 #include "asterisk/stringfields.h"
43 #include "asterisk/app.h"
44 #include "asterisk/pbx.h"
45 #include "asterisk/channel.h"
46 #include "asterisk/timing.h"
47 #include "asterisk/bridge.h"
48 #include "asterisk/bridge_channel.h"
49 #include "asterisk/bridge_after.h"
50 #include "asterisk/bridge_channel_internal.h"
51 #include "asterisk/bridge_internal.h"
52 #include "asterisk/stasis_bridges.h"
53 #include "asterisk/stasis_channels.h"
54 #include "asterisk/musiconhold.h"
55 #include "asterisk/features_config.h"
56 #include "asterisk/parking.h"
57 #include "asterisk/causes.h"
58 #include "asterisk/test.h"
61 * \brief Used to queue an action frame onto a bridge channel and write an action frame into a bridge.
64 * \param bridge_channel Which channel work with.
65 * \param action Type of bridge action frame.
66 * \param data Frame payload data to pass.
67 * \param datalen Frame payload data length to pass.
69 * \retval 0 on success.
70 * \retval -1 on error.
72 typedef int (*ast_bridge_channel_post_action_data)(struct ast_bridge_channel *bridge_channel, enum bridge_channel_action_type action, const void *data, size_t datalen);
75 * \brief Counter used for assigning synchronous bridge action IDs
80 * \brief Frame payload for synchronous bridge actions.
82 * The payload serves as a wrapper around the actual payload of the
83 * frame, with the addition of an id used to find the associated
87 /*! Unique ID for this synchronous action */
89 /*! Actual frame data to process */
90 unsigned char data[0];
94 * \brief Synchronous bridge action object.
96 * Synchronous bridge actions require the ability for one thread to wait
97 * and for another thread to indicate that the action has completed. This
98 * structure facilitates that goal by providing synchronization structures.
101 /*! Unique ID of this synchronization object. Corresponds with ID in synchronous frame payload */
103 /*! Semaphore used for synchronization */
105 /*! Pointer to next entry in the list */
106 AST_LIST_ENTRY(bridge_sync) list;
110 * \brief List holding active synchronous action objects.
112 static AST_RWLIST_HEAD_STATIC(sync_structs, bridge_sync);
115 * \brief initialize a synchronous bridge object.
117 * This both initializes the structure and adds it to the list of
118 * synchronization structures.
120 * \param sync_struct The synchronization object to initialize.
121 * \param id ID to assign to the synchronization object.
123 static void bridge_sync_init(struct bridge_sync *sync_struct, unsigned int id)
125 memset(sync_struct, 0, sizeof(*sync_struct));
126 sync_struct->id = id;
127 sem_init(&sync_struct->sem, 0, 0);
129 AST_RWLIST_WRLOCK(&sync_structs);
130 AST_RWLIST_INSERT_TAIL(&sync_structs, sync_struct, list);
131 AST_RWLIST_UNLOCK(&sync_structs);
135 * \brief Clean up a syncrhonization bridge object.
137 * This frees fields within the synchronization object and removes
138 * it from the list of active synchronization objects.
140 * Since synchronization objects are stack-allocated, it is vital
141 * that this is called before the synchronization object goes
144 * \param sync_struct Synchronization object to clean up.
146 static void bridge_sync_cleanup(struct bridge_sync *sync_struct)
148 struct bridge_sync *iter;
150 AST_RWLIST_WRLOCK(&sync_structs);
151 AST_LIST_TRAVERSE_SAFE_BEGIN(&sync_structs, iter, list) {
152 if (iter->id == sync_struct->id) {
153 AST_LIST_REMOVE_CURRENT(list);
157 AST_LIST_TRAVERSE_SAFE_END;
158 AST_RWLIST_UNLOCK(&sync_structs);
160 sem_destroy(&sync_struct->sem);
164 * \brief Failsafe for synchronous bridge action waiting.
166 * When waiting for a synchronous bridge action to complete,
167 * if there is a frame resource leak somewhere, it is possible
168 * that we will never get notified that the synchronous action
171 * If a significant amount of time passes, then we will abandon
172 * waiting for the synchrnous bridge action to complete.
174 * This constant represents the number of milliseconds we will
175 * wait for the bridge action to complete.
177 #define PLAYBACK_TIMEOUT (600 * 1000)
180 * \brief Wait for a synchronous bridge action to complete.
182 * \param sync_struct Synchronization object corresponding to the bridge action.
184 static void bridge_sync_wait(struct bridge_sync *sync_struct)
186 struct timeval timeout_val = ast_tvadd(ast_tvnow(), ast_samp2tv(PLAYBACK_TIMEOUT, 1000));
187 struct timespec timeout_spec = {
188 .tv_sec = timeout_val.tv_sec,
189 .tv_nsec = timeout_val.tv_usec * 1000,
192 sem_timedwait(&sync_struct->sem, &timeout_spec);
196 * \brief Signal that waiting for a synchronous bridge action is no longer necessary.
198 * This may occur for several reasons
199 * \li The synchronous bridge action has completed.
200 * \li The bridge channel has been removed from the bridge.
201 * \li The synchronous bridge action could not be queued.
203 * \param sync_struct Synchronization object corresponding to the bridge action.
205 static void bridge_sync_signal(struct bridge_sync *sync_struct)
207 sem_post(&sync_struct->sem);
210 void ast_bridge_channel_lock_bridge(struct ast_bridge_channel *bridge_channel)
212 struct ast_bridge *bridge;
215 /* Safely get the bridge pointer */
216 ast_bridge_channel_lock(bridge_channel);
217 bridge = bridge_channel->bridge;
219 ast_bridge_channel_unlock(bridge_channel);
221 /* Lock the bridge and see if it is still the bridge we need to lock. */
222 ast_bridge_lock(bridge);
223 if (bridge == bridge_channel->bridge) {
227 ast_bridge_unlock(bridge);
232 int ast_bridge_channel_notify_talking(struct ast_bridge_channel *bridge_channel, int started_talking)
234 struct ast_frame action = {
235 .frametype = AST_FRAME_BRIDGE_ACTION,
236 .subclass.integer = started_talking
237 ? BRIDGE_CHANNEL_ACTION_TALKING_START : BRIDGE_CHANNEL_ACTION_TALKING_STOP,
240 return ast_bridge_channel_queue_frame(bridge_channel, &action);
245 * \brief Poke the bridge_channel thread
247 static void bridge_channel_poke(struct ast_bridge_channel *bridge_channel)
249 if (!pthread_equal(pthread_self(), bridge_channel->thread)) {
250 /* Wake up the bridge channel thread. */
251 ast_queue_frame(bridge_channel->chan, &ast_null_frame);
257 * \brief Set actual cause on channel.
260 * \param chan Channel to set cause.
261 * \param cause Cause to set on channel.
262 * If cause <= 0 then use cause on channel if cause still <= 0 use AST_CAUSE_NORMAL_CLEARING.
264 * \return Actual cause set on channel.
266 static int channel_set_cause(struct ast_channel *chan, int cause)
268 ast_channel_lock(chan);
270 cause = ast_channel_hangupcause(chan);
272 cause = AST_CAUSE_NORMAL_CLEARING;
275 ast_channel_hangupcause_set(chan, cause);
276 ast_channel_unlock(chan);
280 void ast_bridge_channel_leave_bridge_nolock(struct ast_bridge_channel *bridge_channel, enum bridge_channel_state new_state, int cause)
282 if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
286 ast_debug(1, "Setting %p(%s) state from:%u to:%u\n",
287 bridge_channel, ast_channel_name(bridge_channel->chan), bridge_channel->state,
290 channel_set_cause(bridge_channel->chan, cause);
292 /* Change the state on the bridge channel */
293 bridge_channel->state = new_state;
295 bridge_channel_poke(bridge_channel);
298 void ast_bridge_channel_leave_bridge(struct ast_bridge_channel *bridge_channel, enum bridge_channel_state new_state, int cause)
300 ast_bridge_channel_lock(bridge_channel);
301 ast_bridge_channel_leave_bridge_nolock(bridge_channel, new_state, cause);
302 ast_bridge_channel_unlock(bridge_channel);
305 struct ast_bridge_channel *ast_bridge_channel_peer(struct ast_bridge_channel *bridge_channel)
307 struct ast_bridge *bridge = bridge_channel->bridge;
308 struct ast_bridge_channel *other = NULL;
310 if (bridge_channel->in_bridge && bridge->num_channels == 2) {
311 AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
312 if (other != bridge_channel) {
321 void ast_bridge_channel_restore_formats(struct ast_bridge_channel *bridge_channel)
323 /* Restore original formats of the channel as they came in */
324 if (ast_format_cmp(ast_channel_readformat(bridge_channel->chan), &bridge_channel->read_format) == AST_FORMAT_CMP_NOT_EQUAL) {
325 ast_debug(1, "Bridge is returning %p(%s) to read format %s\n",
326 bridge_channel, ast_channel_name(bridge_channel->chan),
327 ast_getformatname(&bridge_channel->read_format));
328 if (ast_set_read_format(bridge_channel->chan, &bridge_channel->read_format)) {
329 ast_debug(1, "Bridge failed to return %p(%s) to read format %s\n",
330 bridge_channel, ast_channel_name(bridge_channel->chan),
331 ast_getformatname(&bridge_channel->read_format));
334 if (ast_format_cmp(ast_channel_writeformat(bridge_channel->chan), &bridge_channel->write_format) == AST_FORMAT_CMP_NOT_EQUAL) {
335 ast_debug(1, "Bridge is returning %p(%s) to write format %s\n",
336 bridge_channel, ast_channel_name(bridge_channel->chan),
337 ast_getformatname(&bridge_channel->write_format));
338 if (ast_set_write_format(bridge_channel->chan, &bridge_channel->write_format)) {
339 ast_debug(1, "Bridge failed to return %p(%s) to write format %s\n",
340 bridge_channel, ast_channel_name(bridge_channel->chan),
341 ast_getformatname(&bridge_channel->write_format));
346 struct ast_bridge *ast_bridge_channel_merge_inhibit(struct ast_bridge_channel *bridge_channel, int request)
348 struct ast_bridge *bridge;
350 ast_bridge_channel_lock_bridge(bridge_channel);
351 bridge = bridge_channel->bridge;
353 bridge_merge_inhibit_nolock(bridge, request);
354 ast_bridge_unlock(bridge);
358 void ast_bridge_channel_update_linkedids(struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
360 struct ast_bridge_channel *other = NULL;
361 struct ast_bridge *bridge = bridge_channel->bridge;
362 struct ast_channel *oldest_linkedid_chan = bridge_channel->chan;
364 AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
368 oldest_linkedid_chan = ast_channel_internal_oldest_linkedid(
369 oldest_linkedid_chan, other->chan);
372 ast_channel_lock(bridge_channel->chan);
373 ast_channel_internal_copy_linkedid(bridge_channel->chan,
374 oldest_linkedid_chan);
375 ast_channel_unlock(bridge_channel->chan);
376 AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
380 ast_channel_lock(other->chan);
381 ast_channel_internal_copy_linkedid(other->chan,
382 oldest_linkedid_chan);
383 ast_channel_unlock(other->chan);
387 void ast_bridge_channel_update_accountcodes(struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
389 struct ast_bridge *bridge = bridge_channel->bridge;
390 struct ast_bridge_channel *other = NULL;
392 AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
396 ast_channel_lock_both(bridge_channel->chan, other->chan);
398 if (!ast_strlen_zero(ast_channel_accountcode(bridge_channel->chan)) && ast_strlen_zero(ast_channel_peeraccount(other->chan))) {
399 ast_debug(1, "Setting peeraccount to %s for %s from data on channel %s\n",
400 ast_channel_accountcode(bridge_channel->chan), ast_channel_name(other->chan), ast_channel_name(bridge_channel->chan));
401 ast_channel_peeraccount_set(other->chan, ast_channel_accountcode(bridge_channel->chan));
403 if (!ast_strlen_zero(ast_channel_accountcode(other->chan)) && ast_strlen_zero(ast_channel_peeraccount(bridge_channel->chan))) {
404 ast_debug(1, "Setting peeraccount to %s for %s from data on channel %s\n",
405 ast_channel_accountcode(other->chan), ast_channel_name(bridge_channel->chan), ast_channel_name(other->chan));
406 ast_channel_peeraccount_set(bridge_channel->chan, ast_channel_accountcode(other->chan));
408 if (!ast_strlen_zero(ast_channel_peeraccount(bridge_channel->chan)) && ast_strlen_zero(ast_channel_accountcode(other->chan))) {
409 ast_debug(1, "Setting accountcode to %s for %s from data on channel %s\n",
410 ast_channel_peeraccount(bridge_channel->chan), ast_channel_name(other->chan), ast_channel_name(bridge_channel->chan));
411 ast_channel_accountcode_set(other->chan, ast_channel_peeraccount(bridge_channel->chan));
413 if (!ast_strlen_zero(ast_channel_peeraccount(other->chan)) && ast_strlen_zero(ast_channel_accountcode(bridge_channel->chan))) {
414 ast_debug(1, "Setting accountcode to %s for %s from data on channel %s\n",
415 ast_channel_peeraccount(other->chan), ast_channel_name(bridge_channel->chan), ast_channel_name(other->chan));
416 ast_channel_accountcode_set(bridge_channel->chan, ast_channel_peeraccount(other->chan));
418 if (bridge->num_channels == 2) {
419 if (strcmp(ast_channel_accountcode(bridge_channel->chan), ast_channel_peeraccount(other->chan))) {
420 ast_debug(1, "Changing peeraccount from %s to %s on %s to match channel %s\n",
421 ast_channel_peeraccount(other->chan), ast_channel_peeraccount(bridge_channel->chan), ast_channel_name(other->chan), ast_channel_name(bridge_channel->chan));
422 ast_channel_peeraccount_set(other->chan, ast_channel_accountcode(bridge_channel->chan));
424 if (strcmp(ast_channel_accountcode(other->chan), ast_channel_peeraccount(bridge_channel->chan))) {
425 ast_debug(1, "Changing peeraccount from %s to %s on %s to match channel %s\n",
426 ast_channel_peeraccount(bridge_channel->chan), ast_channel_peeraccount(other->chan), ast_channel_name(bridge_channel->chan), ast_channel_name(other->chan));
427 ast_channel_peeraccount_set(bridge_channel->chan, ast_channel_accountcode(other->chan));
430 ast_channel_unlock(bridge_channel->chan);
431 ast_channel_unlock(other->chan);
435 void ast_bridge_channel_kick(struct ast_bridge_channel *bridge_channel, int cause)
437 struct ast_bridge_features *features = bridge_channel->features;
438 struct ast_bridge_hook *hook;
439 struct ao2_iterator iter;
441 ast_bridge_channel_lock(bridge_channel);
442 if (bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
443 channel_set_cause(bridge_channel->chan, cause);
446 ast_bridge_channel_unlock(bridge_channel);
448 /* Run any hangup hooks. */
449 iter = ao2_iterator_init(features->other_hooks, 0);
450 for (; (hook = ao2_iterator_next(&iter)); ao2_ref(hook, -1)) {
453 if (hook->type != AST_BRIDGE_HOOK_TYPE_HANGUP) {
456 remove_me = hook->callback(bridge_channel, hook->hook_pvt);
458 ast_debug(1, "Hangup hook %p is being removed from %p(%s)\n",
459 hook, bridge_channel, ast_channel_name(bridge_channel->chan));
460 ao2_unlink(features->other_hooks, hook);
463 ao2_iterator_destroy(&iter);
465 /* Default hangup action. */
466 ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END, cause);
471 * \brief Write an \ref ast_frame onto the bridge channel
474 * \param bridge_channel Which channel to queue the frame onto.
475 * \param frame The frame to write onto the bridge_channel
477 * \retval 0 on success.
478 * \retval -1 on error.
480 static int bridge_channel_write_frame(struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
482 ast_assert(frame->frametype != AST_FRAME_BRIDGE_ACTION_SYNC);
484 ast_bridge_channel_lock_bridge(bridge_channel);
486 * XXX need to implement a deferred write queue for when there
487 * is no peer channel in the bridge (yet or it was kicked).
489 * The tech decides if a frame needs to be pushed back for deferral.
490 * simple_bridge/native_bridge are likely the only techs that will do this.
492 bridge_channel->bridge->technology->write(bridge_channel->bridge, bridge_channel, frame);
494 /* Remember any owed events to the bridge. */
495 switch (frame->frametype) {
496 case AST_FRAME_DTMF_BEGIN:
497 bridge_channel->owed.dtmf_tv = ast_tvnow();
498 bridge_channel->owed.dtmf_digit = frame->subclass.integer;
500 case AST_FRAME_DTMF_END:
501 bridge_channel->owed.dtmf_digit = '\0';
503 case AST_FRAME_CONTROL:
505 * We explicitly will not remember HOLD/UNHOLD frames because
506 * things like attended transfers will handle them.
511 ast_bridge_unlock(bridge_channel->bridge);
514 * Claim successful write to bridge. If deferred frame
515 * support is added, claim successfully deferred.
520 void bridge_channel_settle_owed_events(struct ast_bridge *orig_bridge, struct ast_bridge_channel *bridge_channel)
522 if (bridge_channel->owed.dtmf_digit) {
523 struct ast_frame frame = {
524 .frametype = AST_FRAME_DTMF_END,
525 .subclass.integer = bridge_channel->owed.dtmf_digit,
526 .src = "Bridge channel owed DTMF",
529 frame.len = ast_tvdiff_ms(ast_tvnow(), bridge_channel->owed.dtmf_tv);
530 if (frame.len < option_dtmfminduration) {
531 frame.len = option_dtmfminduration;
533 ast_log(LOG_DTMF, "DTMF end '%c' simulated to bridge %s because %s left. Duration %ld ms.\n",
534 bridge_channel->owed.dtmf_digit, orig_bridge->uniqueid,
535 ast_channel_name(bridge_channel->chan), frame.len);
536 bridge_channel->owed.dtmf_digit = '\0';
537 orig_bridge->technology->write(orig_bridge, NULL, &frame);
543 * \brief Suspend a channel from a bridge.
545 * \param bridge_channel Channel to suspend.
547 * \note This function assumes bridge_channel->bridge is locked.
551 void bridge_channel_internal_suspend_nolock(struct ast_bridge_channel *bridge_channel)
553 bridge_channel->suspended = 1;
554 if (bridge_channel->in_bridge) {
555 --bridge_channel->bridge->num_active;
558 /* Get technology bridge threads off of the channel. */
559 if (bridge_channel->bridge->technology->suspend) {
560 bridge_channel->bridge->technology->suspend(bridge_channel->bridge, bridge_channel);
566 * \brief Suspend a channel from a bridge.
568 * \param bridge_channel Channel to suspend.
572 static void bridge_channel_suspend(struct ast_bridge_channel *bridge_channel)
574 ast_bridge_channel_lock_bridge(bridge_channel);
575 bridge_channel_internal_suspend_nolock(bridge_channel);
576 ast_bridge_unlock(bridge_channel->bridge);
581 * \brief Unsuspend a channel from a bridge.
583 * \param bridge_channel Channel to unsuspend.
585 * \note This function assumes bridge_channel->bridge is locked.
589 void bridge_channel_internal_unsuspend_nolock(struct ast_bridge_channel *bridge_channel)
591 bridge_channel->suspended = 0;
592 if (bridge_channel->in_bridge) {
593 ++bridge_channel->bridge->num_active;
596 /* Wake technology bridge threads to take care of channel again. */
597 if (bridge_channel->bridge->technology->unsuspend) {
598 bridge_channel->bridge->technology->unsuspend(bridge_channel->bridge, bridge_channel);
601 /* Wake suspended channel. */
602 ast_bridge_channel_lock(bridge_channel);
603 ast_cond_signal(&bridge_channel->cond);
604 ast_bridge_channel_unlock(bridge_channel);
609 * \brief Unsuspend a channel from a bridge.
611 * \param bridge_channel Channel to unsuspend.
615 static void bridge_channel_unsuspend(struct ast_bridge_channel *bridge_channel)
617 ast_bridge_channel_lock_bridge(bridge_channel);
618 bridge_channel_internal_unsuspend_nolock(bridge_channel);
619 ast_bridge_unlock(bridge_channel->bridge);
624 * \brief Queue an action frame onto the bridge channel with data.
627 * \param bridge_channel Which channel to queue the frame onto.
628 * \param action Type of bridge action frame.
629 * \param data Frame payload data to pass.
630 * \param datalen Frame payload data length to pass.
632 * \retval 0 on success.
633 * \retval -1 on error.
635 static int bridge_channel_queue_action_data(struct ast_bridge_channel *bridge_channel,
636 enum bridge_channel_action_type action, const void *data, size_t datalen)
638 struct ast_frame frame = {
639 .frametype = AST_FRAME_BRIDGE_ACTION,
640 .subclass.integer = action,
642 .data.ptr = (void *) data,
645 return ast_bridge_channel_queue_frame(bridge_channel, &frame);
650 * \brief Queue an action frame onto the bridge channel with data synchronously.
653 * The function will not return until the queued frame is freed.
655 * \param bridge_channel Which channel to queue the frame onto.
656 * \param action Type of bridge action frame.
657 * \param data Frame payload data to pass.
658 * \param datalen Frame payload data length to pass.
660 * \retval 0 on success.
661 * \retval -1 on error.
663 static int bridge_channel_queue_action_data_sync(struct ast_bridge_channel *bridge_channel,
664 enum bridge_channel_action_type action, const void *data, size_t datalen)
666 struct sync_payload *sync_payload;
667 int sync_payload_len = sizeof(*sync_payload) + datalen;
668 struct bridge_sync sync_struct;
669 struct ast_frame frame = {
670 .frametype = AST_FRAME_BRIDGE_ACTION_SYNC,
671 .subclass.integer = action,
674 /* Make sure we don't end up trying to wait on ourself to deliver the frame */
675 ast_assert(!pthread_equal(pthread_self(), bridge_channel->thread));
677 sync_payload = ast_alloca(sync_payload_len);
678 sync_payload->id = ast_atomic_fetchadd_int(&sync_ids, +1);
679 memcpy(sync_payload->data, data, datalen);
681 frame.datalen = sync_payload_len;
682 frame.data.ptr = sync_payload;
684 bridge_sync_init(&sync_struct, sync_payload->id);
685 if (ast_bridge_channel_queue_frame(bridge_channel, &frame)) {
686 bridge_sync_cleanup(&sync_struct);
690 bridge_sync_wait(&sync_struct);
691 bridge_sync_cleanup(&sync_struct);
696 * \brief Write an action frame onto the bridge channel with data.
699 * \param bridge_channel Which channel to queue the frame onto.
700 * \param action Type of bridge action frame.
701 * \param data Frame payload data to pass.
702 * \param datalen Frame payload data length to pass.
704 * \retval 0 on success.
705 * \retval -1 on error.
707 static int bridge_channel_write_action_data(struct ast_bridge_channel *bridge_channel,
708 enum bridge_channel_action_type action, const void *data, size_t datalen)
710 struct ast_frame frame = {
711 .frametype = AST_FRAME_BRIDGE_ACTION,
712 .subclass.integer = action,
714 .data.ptr = (void *) data,
717 return bridge_channel_write_frame(bridge_channel, &frame);
720 static void bridge_frame_free(struct ast_frame *frame)
722 if (frame->frametype == AST_FRAME_BRIDGE_ACTION_SYNC) {
723 struct sync_payload *sync_payload = frame->data.ptr;
724 struct bridge_sync *sync;
726 AST_RWLIST_RDLOCK(&sync_structs);
727 AST_RWLIST_TRAVERSE(&sync_structs, sync, list) {
728 if (sync->id == sync_payload->id) {
733 bridge_sync_signal(sync);
735 AST_RWLIST_UNLOCK(&sync_structs);
741 int ast_bridge_channel_queue_frame(struct ast_bridge_channel *bridge_channel, struct ast_frame *fr)
743 struct ast_frame *dup;
746 if (bridge_channel->suspended
747 /* Also defer DTMF frames. */
748 && fr->frametype != AST_FRAME_DTMF_BEGIN
749 && fr->frametype != AST_FRAME_DTMF_END
750 && !ast_is_deferrable_frame(fr)) {
751 /* Drop non-deferable frames when suspended. */
754 if (fr->frametype == AST_FRAME_NULL) {
755 /* "Accept" the frame and discard it. */
764 ast_bridge_channel_lock(bridge_channel);
765 if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
766 /* Drop frames on channels leaving the bridge. */
767 ast_bridge_channel_unlock(bridge_channel);
768 bridge_frame_free(dup);
772 AST_LIST_INSERT_TAIL(&bridge_channel->wr_queue, dup, frame_list);
773 if (write(bridge_channel->alert_pipe[1], &nudge, sizeof(nudge)) != sizeof(nudge)) {
774 ast_log(LOG_ERROR, "We couldn't write alert pipe for %p(%s)... something is VERY wrong\n",
775 bridge_channel, ast_channel_name(bridge_channel->chan));
777 ast_bridge_channel_unlock(bridge_channel);
781 int ast_bridge_queue_everyone_else(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
783 struct ast_bridge_channel *cur;
784 int not_written = -1;
786 if (frame->frametype == AST_FRAME_NULL) {
787 /* "Accept" the frame and discard it. */
791 AST_LIST_TRAVERSE(&bridge->channels, cur, entry) {
792 if (cur == bridge_channel) {
795 if (!ast_bridge_channel_queue_frame(cur, frame)) {
802 int ast_bridge_channel_queue_control_data(struct ast_bridge_channel *bridge_channel, enum ast_control_frame_type control, const void *data, size_t datalen)
804 struct ast_frame frame = {
805 .frametype = AST_FRAME_CONTROL,
806 .subclass.integer = control,
808 .data.ptr = (void *) data,
811 return ast_bridge_channel_queue_frame(bridge_channel, &frame);
814 int ast_bridge_channel_write_control_data(struct ast_bridge_channel *bridge_channel, enum ast_control_frame_type control, const void *data, size_t datalen)
816 struct ast_frame frame = {
817 .frametype = AST_FRAME_CONTROL,
818 .subclass.integer = control,
820 .data.ptr = (void *) data,
823 return bridge_channel_write_frame(bridge_channel, &frame);
826 int ast_bridge_channel_write_hold(struct ast_bridge_channel *bridge_channel, const char *moh_class)
828 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
831 if (!ast_strlen_zero(moh_class)) {
832 datalen = strlen(moh_class) + 1;
834 blob = ast_json_pack("{s: s}",
835 "musicclass", moh_class);
841 ast_channel_lock(bridge_channel->chan);
842 ast_channel_publish_blob(bridge_channel->chan, ast_channel_hold_type(), blob);
843 ast_channel_unlock(bridge_channel->chan);
844 return ast_bridge_channel_write_control_data(bridge_channel, AST_CONTROL_HOLD,
848 int ast_bridge_channel_write_unhold(struct ast_bridge_channel *bridge_channel)
850 ast_channel_lock(bridge_channel->chan);
851 ast_channel_publish_blob(bridge_channel->chan, ast_channel_unhold_type(), NULL);
852 ast_channel_unlock(bridge_channel->chan);
853 return ast_bridge_channel_write_control_data(bridge_channel, AST_CONTROL_UNHOLD, NULL, 0);
858 * \brief Helper function to kick off a PBX app on a bridge_channel
860 static int run_app_helper(struct ast_channel *chan, const char *app_name, const char *app_args)
864 if (!strcasecmp("Gosub", app_name)) {
865 ast_app_exec_sub(NULL, chan, app_args, 0);
866 } else if (!strcasecmp("Macro", app_name)) {
867 ast_app_exec_macro(NULL, chan, app_args);
871 app = pbx_findapp(app_name);
873 ast_log(LOG_WARNING, "Could not find application (%s)\n", app_name);
875 res = pbx_exec(chan, app, app_args);
881 void ast_bridge_channel_run_app(struct ast_bridge_channel *bridge_channel, const char *app_name, const char *app_args, const char *moh_class)
884 ast_bridge_channel_write_hold(bridge_channel, moh_class);
886 if (run_app_helper(bridge_channel->chan, app_name, S_OR(app_args, ""))) {
887 /* Break the bridge if the app returns non-zero. */
888 ast_bridge_channel_kick(bridge_channel, AST_CAUSE_NORMAL_CLEARING);
891 ast_bridge_channel_write_unhold(bridge_channel);
895 struct bridge_run_app {
896 /*! Offset into app_name[] where the MOH class name starts. (zero if no MOH) */
898 /*! Offset into app_name[] where the application argument string starts. (zero if no arguments) */
900 /*! Application name to run. */
906 * \brief Handle the run application bridge action.
909 * \param bridge_channel Which channel to run the application on.
910 * \param data Action frame data to run the application.
914 static void bridge_channel_run_app(struct ast_bridge_channel *bridge_channel, struct bridge_run_app *data)
916 ast_bridge_channel_run_app(bridge_channel, data->app_name,
917 data->app_args_offset ? &data->app_name[data->app_args_offset] : NULL,
918 data->moh_offset ? &data->app_name[data->moh_offset] : NULL);
923 * \brief Marshal an application to be executed on a bridge_channel
925 static int payload_helper_app(ast_bridge_channel_post_action_data post_it,
926 struct ast_bridge_channel *bridge_channel, const char *app_name, const char *app_args, const char *moh_class)
928 struct bridge_run_app *app_data;
929 size_t len_name = strlen(app_name) + 1;
930 size_t len_args = ast_strlen_zero(app_args) ? 0 : strlen(app_args) + 1;
931 size_t len_moh = !moh_class ? 0 : strlen(moh_class) + 1;
932 size_t len_data = sizeof(*app_data) + len_name + len_args + len_moh;
934 /* Fill in application run frame data. */
935 app_data = alloca(len_data);
936 app_data->app_args_offset = len_args ? len_name : 0;
937 app_data->moh_offset = len_moh ? len_name + len_args : 0;
938 strcpy(app_data->app_name, app_name);/* Safe */
940 strcpy(&app_data->app_name[app_data->app_args_offset], app_args);/* Safe */
943 strcpy(&app_data->app_name[app_data->moh_offset], moh_class);/* Safe */
946 return post_it(bridge_channel, BRIDGE_CHANNEL_ACTION_RUN_APP, app_data, len_data);
949 int ast_bridge_channel_write_app(struct ast_bridge_channel *bridge_channel, const char *app_name, const char *app_args, const char *moh_class)
951 return payload_helper_app(bridge_channel_write_action_data,
952 bridge_channel, app_name, app_args, moh_class);
955 int ast_bridge_channel_queue_app(struct ast_bridge_channel *bridge_channel, const char *app_name, const char *app_args, const char *moh_class)
957 return payload_helper_app(bridge_channel_queue_action_data,
958 bridge_channel, app_name, app_args, moh_class);
961 void ast_bridge_channel_playfile(struct ast_bridge_channel *bridge_channel, ast_bridge_custom_play_fn custom_play, const char *playfile, const char *moh_class)
964 ast_bridge_channel_write_hold(bridge_channel, moh_class);
967 custom_play(bridge_channel, playfile);
969 ast_stream_and_wait(bridge_channel->chan, playfile, AST_DIGIT_NONE);
972 ast_bridge_channel_write_unhold(bridge_channel);
976 * It may be necessary to resume music on hold after we finish
977 * playing the announcment.
979 if (ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_MOH)) {
980 const char *latest_musicclass;
982 ast_channel_lock(bridge_channel->chan);
983 latest_musicclass = ast_strdupa(ast_channel_latest_musicclass(bridge_channel->chan));
984 ast_channel_unlock(bridge_channel->chan);
985 ast_moh_start(bridge_channel->chan, latest_musicclass, NULL);
989 struct bridge_playfile {
990 /*! Call this function to play the playfile. (NULL if normal sound file to play) */
991 ast_bridge_custom_play_fn custom_play;
992 /*! Offset into playfile[] where the MOH class name starts. (zero if no MOH)*/
994 /*! Filename to play. */
1000 * \brief Handle the playfile bridge action.
1003 * \param bridge_channel Which channel to play a file on.
1004 * \param payload Action frame payload to play a file.
1008 static void bridge_channel_playfile(struct ast_bridge_channel *bridge_channel, struct bridge_playfile *payload)
1010 ast_bridge_channel_playfile(bridge_channel, payload->custom_play, payload->playfile,
1011 payload->moh_offset ? &payload->playfile[payload->moh_offset] : NULL);
1016 * \brief Marshal a file to be played on a bridge_channel
1018 static int payload_helper_playfile(ast_bridge_channel_post_action_data post_it,
1019 struct ast_bridge_channel *bridge_channel, ast_bridge_custom_play_fn custom_play, const char *playfile, const char *moh_class)
1021 struct bridge_playfile *payload;
1022 size_t len_name = strlen(playfile) + 1;
1023 size_t len_moh = !moh_class ? 0 : strlen(moh_class) + 1;
1024 size_t len_payload = sizeof(*payload) + len_name + len_moh;
1026 /* Fill in play file frame data. */
1027 payload = ast_alloca(len_payload);
1028 payload->custom_play = custom_play;
1029 payload->moh_offset = len_moh ? len_name : 0;
1030 strcpy(payload->playfile, playfile);/* Safe */
1032 strcpy(&payload->playfile[payload->moh_offset], moh_class);/* Safe */
1035 return post_it(bridge_channel, BRIDGE_CHANNEL_ACTION_PLAY_FILE, payload, len_payload);
1038 int ast_bridge_channel_write_playfile(struct ast_bridge_channel *bridge_channel, ast_bridge_custom_play_fn custom_play, const char *playfile, const char *moh_class)
1040 return payload_helper_playfile(bridge_channel_write_action_data,
1041 bridge_channel, custom_play, playfile, moh_class);
1044 int ast_bridge_channel_queue_playfile(struct ast_bridge_channel *bridge_channel, ast_bridge_custom_play_fn custom_play, const char *playfile, const char *moh_class)
1046 return payload_helper_playfile(bridge_channel_queue_action_data,
1047 bridge_channel, custom_play, playfile, moh_class);
1050 int ast_bridge_channel_queue_playfile_sync(struct ast_bridge_channel *bridge_channel,
1051 ast_bridge_custom_play_fn custom_play, const char *playfile, const char *moh_class)
1053 return payload_helper_playfile(bridge_channel_queue_action_data_sync,
1054 bridge_channel, custom_play, playfile, moh_class);
1057 struct bridge_custom_callback {
1058 /*! Call this function on the bridge channel thread. */
1059 ast_bridge_custom_callback_fn callback;
1060 /*! Size of the payload if it exists. A number otherwise. */
1061 size_t payload_size;
1062 /*! Option flags determining how callback is called. */
1064 /*! Nonzero if the payload exists. */
1065 char payload_exists;
1066 /*! Payload to give to callback. */
1072 * \brief Handle the do custom callback bridge action.
1075 * \param bridge_channel Which channel to call the callback on.
1076 * \param data Action frame data to call the callback.
1080 static void bridge_channel_do_callback(struct ast_bridge_channel *bridge_channel, struct bridge_custom_callback *data)
1082 if (ast_test_flag(data, AST_BRIDGE_CHANNEL_CB_OPTION_MEDIA)) {
1083 bridge_channel_suspend(bridge_channel);
1084 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1086 data->callback(bridge_channel, data->payload_exists ? data->payload : NULL, data->payload_size);
1087 if (ast_test_flag(data, AST_BRIDGE_CHANNEL_CB_OPTION_MEDIA)) {
1088 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1089 bridge_channel_unsuspend(bridge_channel);
1095 * \brief Marshal a custom callback function to be called on a bridge_channel
1097 static int payload_helper_cb(ast_bridge_channel_post_action_data post_it,
1098 struct ast_bridge_channel *bridge_channel,
1099 enum ast_bridge_channel_custom_callback_option flags,
1100 ast_bridge_custom_callback_fn callback, const void *payload, size_t payload_size)
1102 struct bridge_custom_callback *cb_data;
1103 size_t len_data = sizeof(*cb_data) + (payload ? payload_size : 0);
1111 /* Fill in custom callback frame data. */
1112 cb_data = alloca(len_data);
1113 cb_data->callback = callback;
1114 cb_data->payload_size = payload_size;
1115 cb_data->flags = flags;
1116 cb_data->payload_exists = payload && payload_size;
1117 if (cb_data->payload_exists) {
1118 memcpy(cb_data->payload, payload, payload_size);/* Safe */
1121 return post_it(bridge_channel, BRIDGE_CHANNEL_ACTION_CALLBACK, cb_data, len_data);
1124 int ast_bridge_channel_write_callback(struct ast_bridge_channel *bridge_channel,
1125 enum ast_bridge_channel_custom_callback_option flags,
1126 ast_bridge_custom_callback_fn callback, const void *payload, size_t payload_size)
1128 return payload_helper_cb(bridge_channel_write_action_data,
1129 bridge_channel, flags, callback, payload, payload_size);
1132 int ast_bridge_channel_queue_callback(struct ast_bridge_channel *bridge_channel,
1133 enum ast_bridge_channel_custom_callback_option flags,
1134 ast_bridge_custom_callback_fn callback, const void *payload, size_t payload_size)
1136 return payload_helper_cb(bridge_channel_queue_action_data,
1137 bridge_channel, flags, callback, payload, payload_size);
1140 struct bridge_park {
1141 int parker_uuid_offset;
1142 int app_data_offset;
1143 /* buffer used for holding those strings */
1144 char parkee_uuid[0];
1149 * \brief Park a bridge_cahnnel
1151 static void bridge_channel_park(struct ast_bridge_channel *bridge_channel, struct bridge_park *payload)
1153 if (!ast_parking_provider_registered()) {
1154 ast_log(AST_LOG_WARNING, "Unable to park %s: No parking provider loaded!\n",
1155 ast_channel_name(bridge_channel->chan));
1159 if (ast_parking_park_bridge_channel(bridge_channel, payload->parkee_uuid,
1160 &payload->parkee_uuid[payload->parker_uuid_offset],
1161 payload->app_data_offset ? &payload->parkee_uuid[payload->app_data_offset] : NULL)) {
1162 ast_log(AST_LOG_WARNING, "Error occurred while parking %s\n",
1163 ast_channel_name(bridge_channel->chan));
1169 * \brief Marshal a park action onto a bridge_channel
1171 static int payload_helper_park(ast_bridge_channel_post_action_data post_it,
1172 struct ast_bridge_channel *bridge_channel,
1173 const char *parkee_uuid,
1174 const char *parker_uuid,
1175 const char *app_data)
1177 struct bridge_park *payload;
1178 size_t len_parkee_uuid = strlen(parkee_uuid) + 1;
1179 size_t len_parker_uuid = strlen(parker_uuid) + 1;
1180 size_t len_app_data = !app_data ? 0 : strlen(app_data) + 1;
1181 size_t len_payload = sizeof(*payload) + len_parker_uuid + len_parkee_uuid + len_app_data;
1183 payload = alloca(len_payload);
1184 payload->app_data_offset = len_app_data ? len_parkee_uuid + len_parker_uuid : 0;
1185 payload->parker_uuid_offset = len_parkee_uuid;
1186 strcpy(payload->parkee_uuid, parkee_uuid);
1187 strcpy(&payload->parkee_uuid[payload->parker_uuid_offset], parker_uuid);
1189 strcpy(&payload->parkee_uuid[payload->app_data_offset], app_data);
1192 return post_it(bridge_channel, BRIDGE_CHANNEL_ACTION_PARK, payload, len_payload);
1195 int ast_bridge_channel_write_park(struct ast_bridge_channel *bridge_channel, const char *parkee_uuid, const char *parker_uuid, const char *app_data)
1197 return payload_helper_park(bridge_channel_write_action_data,
1198 bridge_channel, parkee_uuid, parker_uuid, app_data);
1203 * \brief Handle bridge channel interval expiration.
1206 * \param bridge_channel Channel to run expired intervals on.
1210 static void bridge_channel_handle_interval(struct ast_bridge_channel *bridge_channel)
1212 struct ast_heap *interval_hooks;
1213 struct ast_bridge_hook_timer *hook;
1214 struct timeval start;
1215 int chan_suspended = 0;
1217 interval_hooks = bridge_channel->features->interval_hooks;
1218 ast_heap_wrlock(interval_hooks);
1219 start = ast_tvnow();
1220 while ((hook = ast_heap_peek(interval_hooks, 1))) {
1222 unsigned int execution_time;
1224 if (ast_tvdiff_ms(hook->timer.trip_time, start) > 0) {
1225 ast_debug(1, "Hook %p on %p(%s) wants to happen in the future, stopping our traversal\n",
1226 hook, bridge_channel, ast_channel_name(bridge_channel->chan));
1230 ast_heap_unlock(interval_hooks);
1233 && ast_test_flag(&hook->timer, AST_BRIDGE_HOOK_TIMER_OPTION_MEDIA)) {
1235 bridge_channel_suspend(bridge_channel);
1236 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1239 ast_debug(1, "Executing hook %p on %p(%s)\n",
1240 hook, bridge_channel, ast_channel_name(bridge_channel->chan));
1241 interval = hook->generic.callback(bridge_channel, hook->generic.hook_pvt);
1243 ast_heap_wrlock(interval_hooks);
1244 if (ast_heap_peek(interval_hooks, hook->timer.heap_index) != hook
1245 || !ast_heap_remove(interval_hooks, hook)) {
1246 /* Interval hook is already removed from the bridge_channel. */
1253 ast_debug(1, "Removed interval hook %p from %p(%s)\n",
1254 hook, bridge_channel, ast_channel_name(bridge_channel->chan));
1259 /* Set new interval for the hook. */
1260 hook->timer.interval = interval;
1263 ast_debug(1, "Updating interval hook %p with interval %u on %p(%s)\n",
1264 hook, hook->timer.interval, bridge_channel,
1265 ast_channel_name(bridge_channel->chan));
1267 /* resetting start */
1268 start = ast_tvnow();
1271 * Resetup the interval hook for the next interval. We may need
1272 * to skip over any missed intervals because the hook was
1273 * delayed or took too long.
1275 execution_time = ast_tvdiff_ms(start, hook->timer.trip_time);
1276 while (hook->timer.interval < execution_time) {
1277 execution_time -= hook->timer.interval;
1279 hook->timer.trip_time = ast_tvadd(start, ast_samp2tv(hook->timer.interval - execution_time, 1000));
1280 hook->timer.seqno = ast_atomic_fetchadd_int((int *) &bridge_channel->features->interval_sequence, +1);
1282 if (ast_heap_push(interval_hooks, hook)) {
1283 /* Could not push the hook back onto the heap. */
1287 ast_heap_unlock(interval_hooks);
1289 if (chan_suspended) {
1290 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1291 bridge_channel_unsuspend(bridge_channel);
1297 * \brief Write a DTMF stream out to a channel
1299 static int bridge_channel_write_dtmf_stream(struct ast_bridge_channel *bridge_channel, const char *dtmf)
1301 return bridge_channel_write_action_data(bridge_channel,
1302 BRIDGE_CHANNEL_ACTION_DTMF_STREAM, dtmf, strlen(dtmf) + 1);
1307 * \brief Indicate to the testsuite a feature was successfully detected.
1309 * Currently, this function only will relay built-in features to the testsuite,
1310 * but it could be modified to detect applicationmap items should the need arise.
1312 * \param chan The channel that activated the feature
1313 * \param dtmf The DTMF sequence entered to activate the feature
1315 static void testsuite_notify_feature_success(struct ast_channel *chan, const char *dtmf)
1317 #ifdef TEST_FRAMEWORK
1318 char *feature = "unknown";
1319 struct ast_featuremap_config *featuremap = ast_get_chan_featuremap_config(chan);
1320 struct ast_features_xfer_config *xfer = ast_get_chan_features_xfer_config(chan);
1323 if (!strcmp(dtmf, featuremap->blindxfer)) {
1324 feature = "blindxfer";
1325 } else if (!strcmp(dtmf, featuremap->atxfer)) {
1327 } else if (!strcmp(dtmf, featuremap->disconnect)) {
1328 feature = "disconnect";
1329 } else if (!strcmp(dtmf, featuremap->automon)) {
1330 feature = "automon";
1331 } else if (!strcmp(dtmf, featuremap->automixmon)) {
1332 feature = "automixmon";
1333 } else if (!strcmp(dtmf, featuremap->parkcall)) {
1334 feature = "parkcall";
1335 } else if (!strcmp(dtmf, xfer->atxferthreeway)) {
1336 feature = "atxferthreeway";
1340 ast_test_suite_event_notify("FEATURE_DETECTION",
1341 "Result: success\r\n"
1342 "Feature: %s", feature);
1343 #endif /* TEST_FRAMEWORK */
1348 * \brief Internal function that executes a feature on a bridge channel
1349 * \note Neither the bridge nor the bridge_channel locks should be held when entering
1352 static void bridge_channel_feature(struct ast_bridge_channel *bridge_channel, const char *starting_dtmf)
1354 struct ast_bridge_features *features = bridge_channel->features;
1355 struct ast_bridge_hook_dtmf *hook = NULL;
1356 char dtmf[MAXIMUM_DTMF_FEATURE_STRING];
1358 unsigned int digit_timeout;
1359 RAII_VAR(struct ast_features_general_config *, gen_cfg, NULL, ao2_cleanup);
1361 ast_channel_lock(bridge_channel->chan);
1362 gen_cfg = ast_get_chan_features_general_config(bridge_channel->chan);
1364 ast_log(LOG_ERROR, "Unable to retrieve features configuration.\n");
1365 ast_channel_unlock(bridge_channel->chan);
1368 digit_timeout = gen_cfg->featuredigittimeout;
1369 ast_channel_unlock(bridge_channel->chan);
1371 if (ast_strlen_zero(starting_dtmf)) {
1375 ast_copy_string(dtmf, starting_dtmf, sizeof(dtmf));
1376 dtmf_len = strlen(dtmf);
1380 * Check if any feature DTMF hooks match or could match and
1381 * try to collect more DTMF digits.
1387 ast_debug(1, "DTMF feature string on %p(%s) is now '%s'\n",
1388 bridge_channel, ast_channel_name(bridge_channel->chan), dtmf);
1390 /* See if a DTMF feature hook matches or can match */
1391 hook = ao2_find(features->dtmf_hooks, dtmf, OBJ_PARTIAL_KEY);
1393 ast_debug(1, "No DTMF feature hooks on %p(%s) match '%s'\n",
1394 bridge_channel, ast_channel_name(bridge_channel->chan), dtmf);
1397 if (strlen(hook->dtmf.code) == dtmf_len) {
1398 ast_debug(1, "DTMF feature hook %p matched DTMF string '%s' on %p(%s)\n",
1399 hook, dtmf, bridge_channel, ast_channel_name(bridge_channel->chan));
1405 if (ARRAY_LEN(dtmf) - 1 <= dtmf_len) {
1406 /* We have reached the maximum length of a DTMF feature string. */
1411 res = ast_waitfordigit(bridge_channel->chan, digit_timeout);
1413 ast_debug(1, "DTMF feature string collection on %p(%s) timed out\n",
1414 bridge_channel, ast_channel_name(bridge_channel->chan));
1418 ast_debug(1, "DTMF feature string collection failed on %p(%s) for some reason\n",
1419 bridge_channel, ast_channel_name(bridge_channel->chan));
1423 /* Add the new DTMF into the DTMF string so we can do our matching */
1424 dtmf[dtmf_len] = res;
1425 dtmf[++dtmf_len] = '\0';
1431 /* Execute the matched hook on this channel. */
1432 remove_me = hook->generic.callback(bridge_channel, hook->generic.hook_pvt);
1434 ast_debug(1, "DTMF hook %p is being removed from %p(%s)\n",
1435 hook, bridge_channel, ast_channel_name(bridge_channel->chan));
1436 ao2_unlink(features->dtmf_hooks, hook);
1438 testsuite_notify_feature_success(bridge_channel->chan, hook->dtmf.code);
1442 * If we are handing the channel off to an external hook for
1443 * ownership, we are not guaranteed what kind of state it will
1444 * come back in. If the channel hungup, we need to detect that
1445 * here if the hook did not already change the state.
1447 if (bridge_channel->chan && ast_check_hangup_locked(bridge_channel->chan)) {
1448 ast_bridge_channel_kick(bridge_channel, 0);
1451 if (features->dtmf_passthrough) {
1452 /* Stream any collected DTMF to the other channels. */
1453 bridge_channel_write_dtmf_stream(bridge_channel, dtmf);
1455 ast_test_suite_event_notify("FEATURE_DETECTION", "Result: fail");
1461 * \brief Indicate that a bridge_channel is talking
1463 static void bridge_channel_talking(struct ast_bridge_channel *bridge_channel, int talking)
1465 struct ast_bridge_features *features = bridge_channel->features;
1466 struct ast_bridge_hook *hook;
1467 struct ao2_iterator iter;
1469 /* Run any talk detection hooks. */
1470 iter = ao2_iterator_init(features->other_hooks, 0);
1471 for (; (hook = ao2_iterator_next(&iter)); ao2_ref(hook, -1)) {
1473 ast_bridge_talking_indicate_callback talk_cb;
1475 if (hook->type != AST_BRIDGE_HOOK_TYPE_TALK) {
1478 talk_cb = (ast_bridge_talking_indicate_callback) hook->callback;
1479 remove_me = talk_cb(bridge_channel, hook->hook_pvt, talking);
1481 ast_debug(1, "Talk detection hook %p is being removed from %p(%s)\n",
1482 hook, bridge_channel, ast_channel_name(bridge_channel->chan));
1483 ao2_unlink(features->other_hooks, hook);
1486 ao2_iterator_destroy(&iter);
1489 /*! \brief Internal function that plays back DTMF on a bridge channel */
1490 static void bridge_channel_dtmf_stream(struct ast_bridge_channel *bridge_channel, const char *dtmf)
1492 ast_debug(1, "Playing DTMF stream '%s' out to %p(%s)\n",
1493 dtmf, bridge_channel, ast_channel_name(bridge_channel->chan));
1494 ast_dtmf_stream(bridge_channel->chan, NULL, dtmf, 0, 0);
1497 /*! \brief Data specifying where a blind transfer is going to */
1498 struct blind_transfer_data {
1499 char exten[AST_MAX_EXTENSION];
1500 char context[AST_MAX_CONTEXT];
1505 * \brief Execute after bridge actions on a channel when it leaves a bridge
1507 static void after_bridge_move_channel(struct ast_channel *chan_bridged, void *data)
1509 RAII_VAR(struct ast_channel *, chan_target, data, ao2_cleanup);
1510 struct ast_party_connected_line connected_target;
1511 unsigned char connected_line_data[1024];
1514 ast_party_connected_line_init(&connected_target);
1516 ast_channel_lock(chan_target);
1517 ast_party_connected_line_copy(&connected_target, ast_channel_connected(chan_target));
1518 ast_channel_unlock(chan_target);
1519 ast_party_id_reset(&connected_target.priv);
1521 if (ast_channel_move(chan_target, chan_bridged)) {
1522 ast_softhangup(chan_target, AST_SOFTHANGUP_DEV);
1523 ast_party_connected_line_free(&connected_target);
1527 if ((payload_size = ast_connected_line_build_data(connected_line_data,
1528 sizeof(connected_line_data), &connected_target, NULL)) != -1) {
1529 struct ast_control_read_action_payload *frame_payload;
1532 frame_size = payload_size + sizeof(*frame_payload);
1533 frame_payload = ast_alloca(frame_size);
1534 frame_payload->action = AST_FRAME_READ_ACTION_CONNECTED_LINE_MACRO;
1535 frame_payload->payload_size = payload_size;
1536 memcpy(frame_payload->payload, connected_line_data, payload_size);
1537 ast_queue_control_data(chan_target, AST_CONTROL_READ_ACTION, frame_payload, frame_size);
1540 ast_party_connected_line_free(&connected_target);
1545 * \brief Execute logic to cleanup when after bridge fails
1547 static void after_bridge_move_channel_fail(enum ast_bridge_after_cb_reason reason, void *data)
1549 RAII_VAR(struct ast_channel *, chan_target, data, ao2_cleanup);
1551 ast_log(LOG_WARNING, "Unable to complete transfer: %s\n",
1552 ast_bridge_after_cb_reason_string(reason));
1553 ast_softhangup(chan_target, AST_SOFTHANGUP_DEV);
1558 * \brief Perform a blind transfer on a channel in a bridge
1560 static void bridge_channel_blind_transfer(struct ast_bridge_channel *bridge_channel,
1561 struct blind_transfer_data *blind_data)
1563 ast_async_goto(bridge_channel->chan, blind_data->context, blind_data->exten, 1);
1564 ast_bridge_channel_kick(bridge_channel, AST_CAUSE_NORMAL_CLEARING);
1569 * \brief Perform an attended transfer on a channel in a bridge
1571 static void bridge_channel_attended_transfer(struct ast_bridge_channel *bridge_channel,
1572 const char *target_chan_name)
1574 RAII_VAR(struct ast_channel *, chan_target, NULL, ao2_cleanup);
1575 RAII_VAR(struct ast_channel *, chan_bridged, NULL, ao2_cleanup);
1577 chan_target = ast_channel_get_by_name(target_chan_name);
1579 /* Dang, it disappeared somehow */
1580 ast_bridge_channel_kick(bridge_channel, AST_CAUSE_NORMAL_CLEARING);
1584 ast_bridge_channel_lock(bridge_channel);
1585 chan_bridged = bridge_channel->chan;
1586 ast_assert(chan_bridged != NULL);
1587 ao2_ref(chan_bridged, +1);
1588 ast_bridge_channel_unlock(bridge_channel);
1590 if (ast_bridge_set_after_callback(chan_bridged, after_bridge_move_channel,
1591 after_bridge_move_channel_fail, ast_channel_ref(chan_target))) {
1592 ast_softhangup(chan_target, AST_SOFTHANGUP_DEV);
1594 /* Release the ref we tried to pass to ast_bridge_set_after_callback(). */
1595 ast_channel_unref(chan_target);
1597 ast_bridge_channel_kick(bridge_channel, AST_CAUSE_NORMAL_CLEARING);
1602 * \brief Handle bridge channel bridge action frame.
1605 * \param bridge_channel Channel to execute the action on.
1606 * \param action What to do.
1607 * \param data data from the action.
1611 static void bridge_channel_handle_action(struct ast_bridge_channel *bridge_channel,
1612 enum bridge_channel_action_type action, void *data)
1615 case BRIDGE_CHANNEL_ACTION_DTMF_STREAM:
1616 bridge_channel_suspend(bridge_channel);
1617 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1618 bridge_channel_dtmf_stream(bridge_channel, data);
1619 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1620 bridge_channel_unsuspend(bridge_channel);
1622 case BRIDGE_CHANNEL_ACTION_TALKING_START:
1623 case BRIDGE_CHANNEL_ACTION_TALKING_STOP:
1624 bridge_channel_talking(bridge_channel,
1625 action == BRIDGE_CHANNEL_ACTION_TALKING_START);
1627 case BRIDGE_CHANNEL_ACTION_PLAY_FILE:
1628 bridge_channel_suspend(bridge_channel);
1629 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1630 bridge_channel_playfile(bridge_channel, data);
1631 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1632 bridge_channel_unsuspend(bridge_channel);
1634 case BRIDGE_CHANNEL_ACTION_RUN_APP:
1635 bridge_channel_suspend(bridge_channel);
1636 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1637 bridge_channel_run_app(bridge_channel, data);
1638 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1639 bridge_channel_unsuspend(bridge_channel);
1641 case BRIDGE_CHANNEL_ACTION_CALLBACK:
1642 bridge_channel_do_callback(bridge_channel, data);
1644 case BRIDGE_CHANNEL_ACTION_PARK:
1645 bridge_channel_suspend(bridge_channel);
1646 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1647 bridge_channel_park(bridge_channel, data);
1648 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1649 bridge_channel_unsuspend(bridge_channel);
1651 case BRIDGE_CHANNEL_ACTION_BLIND_TRANSFER:
1652 bridge_channel_blind_transfer(bridge_channel, data);
1654 case BRIDGE_CHANNEL_ACTION_ATTENDED_TRANSFER:
1655 bridge_channel_attended_transfer(bridge_channel, data);
1664 * \brief Check if a bridge should dissolve and do it.
1667 * \param bridge_channel Channel causing the check.
1669 * \note On entry, bridge_channel->bridge is already locked.
1673 static void bridge_channel_dissolve_check(struct ast_bridge_channel *bridge_channel)
1675 struct ast_bridge *bridge = bridge_channel->bridge;
1677 if (bridge->dissolved) {
1681 if (!bridge->num_channels
1682 && ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_DISSOLVE_EMPTY)) {
1683 /* Last channel leaving the bridge turns off the lights. */
1684 bridge_dissolve(bridge, ast_channel_hangupcause(bridge_channel->chan));
1688 switch (bridge_channel->state) {
1689 case BRIDGE_CHANNEL_STATE_END:
1690 /* Do we need to dissolve the bridge because this channel hung up? */
1691 if (ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_DISSOLVE_HANGUP)
1692 || (bridge_channel->features->usable
1693 && ast_test_flag(&bridge_channel->features->feature_flags,
1694 AST_BRIDGE_CHANNEL_FLAG_DISSOLVE_HANGUP))) {
1695 bridge_dissolve(bridge, ast_channel_hangupcause(bridge_channel->chan));
1703 if (bridge->num_lonely && bridge->num_lonely == bridge->num_channels) {
1705 * This will start a chain reaction where each channel leaving
1706 * enters this function and causes the next to leave as long as
1707 * there aren't non-lonely channels in the bridge.
1709 ast_bridge_channel_leave_bridge(AST_LIST_FIRST(&bridge->channels),
1710 BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE,
1711 ast_channel_hangupcause(bridge_channel->chan));
1715 void bridge_channel_internal_pull(struct ast_bridge_channel *bridge_channel)
1717 struct ast_bridge *bridge = bridge_channel->bridge;
1719 if (!bridge_channel->in_bridge) {
1722 bridge_channel->in_bridge = 0;
1724 ast_debug(1, "Bridge %s: pulling %p(%s)\n",
1725 bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan));
1727 ast_verb(3, "Channel %s left '%s' %s-bridge <%s>\n",
1728 ast_channel_name(bridge_channel->chan),
1729 bridge->technology->name,
1730 bridge->v_table->name,
1733 if (!bridge_channel->just_joined) {
1734 /* Tell the bridge technology we are leaving so they tear us down */
1735 ast_debug(1, "Bridge %s: %p(%s) is leaving %s technology\n",
1736 bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan),
1737 bridge->technology->name);
1738 if (bridge->technology->leave) {
1739 bridge->technology->leave(bridge, bridge_channel);
1743 /* Remove channel from the bridge */
1744 if (!bridge_channel->suspended) {
1745 --bridge->num_active;
1747 if (ast_test_flag(&bridge_channel->features->feature_flags, AST_BRIDGE_CHANNEL_FLAG_LONELY)) {
1748 --bridge->num_lonely;
1750 --bridge->num_channels;
1751 AST_LIST_REMOVE(&bridge->channels, bridge_channel, entry);
1752 bridge->v_table->pull(bridge, bridge_channel);
1754 ast_bridge_channel_clear_roles(bridge_channel);
1756 /* If we are not going to be hung up after leaving a bridge, and we were an
1757 * outgoing channel, clear the outgoing flag.
1759 if (ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_OUTGOING)
1760 && (ast_channel_softhangup_internal_flag(bridge_channel->chan) & (AST_SOFTHANGUP_ASYNCGOTO | AST_SOFTHANGUP_UNBRIDGE)
1761 || bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT)) {
1762 ast_debug(2, "Channel %s will survive this bridge; clearing outgoing (dialed) flag\n", ast_channel_name(bridge_channel->chan));
1763 ast_clear_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_OUTGOING);
1766 bridge_channel_dissolve_check(bridge_channel);
1768 bridge->reconfigured = 1;
1769 ast_bridge_publish_leave(bridge, bridge_channel->chan);
1772 int bridge_channel_internal_push(struct ast_bridge_channel *bridge_channel)
1774 struct ast_bridge *bridge = bridge_channel->bridge;
1775 struct ast_bridge_channel *swap;
1777 ast_assert(!bridge_channel->in_bridge);
1779 swap = bridge_find_channel(bridge, bridge_channel->swap);
1780 bridge_channel->swap = NULL;
1783 ast_debug(1, "Bridge %s: pushing %p(%s) by swapping with %p(%s)\n",
1784 bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan),
1785 swap, ast_channel_name(swap->chan));
1787 ast_debug(1, "Bridge %s: pushing %p(%s)\n",
1788 bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan));
1791 /* Add channel to the bridge */
1792 if (bridge->dissolved
1793 || bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT
1794 || (swap && swap->state != BRIDGE_CHANNEL_STATE_WAIT)
1795 || bridge->v_table->push(bridge, bridge_channel, swap)
1796 || ast_bridge_channel_establish_roles(bridge_channel)) {
1797 ast_debug(1, "Bridge %s: pushing %p(%s) into bridge failed\n",
1798 bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan));
1801 bridge_channel->in_bridge = 1;
1802 bridge_channel->just_joined = 1;
1803 AST_LIST_INSERT_TAIL(&bridge->channels, bridge_channel, entry);
1804 ++bridge->num_channels;
1805 if (ast_test_flag(&bridge_channel->features->feature_flags, AST_BRIDGE_CHANNEL_FLAG_LONELY)) {
1806 ++bridge->num_lonely;
1808 if (!bridge_channel->suspended) {
1809 ++bridge->num_active;
1812 ast_verb(3, "Channel %s %s%s%s '%s' %s-bridge <%s>\n",
1813 ast_channel_name(bridge_channel->chan),
1814 swap ? "swapped with " : "joined",
1815 swap ? ast_channel_name(swap->chan) : "",
1816 swap ? " into" : "",
1817 bridge->technology->name,
1818 bridge->v_table->name,
1821 ast_bridge_publish_enter(bridge, bridge_channel->chan, swap ? swap->chan : NULL);
1823 ast_bridge_channel_leave_bridge(swap, BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, 0);
1824 bridge_channel_internal_pull(swap);
1827 /* Clear any BLINDTRANSFER and ATTENDEDTRANSFER since the transfer has completed. */
1828 pbx_builtin_setvar_helper(bridge_channel->chan, "BLINDTRANSFER", NULL);
1829 pbx_builtin_setvar_helper(bridge_channel->chan, "ATTENDEDTRANSFER", NULL);
1831 /* Wake up the bridge channel thread to reevaluate any interval timers. */
1832 ast_queue_frame(bridge_channel->chan, &ast_null_frame);
1834 bridge->reconfigured = 1;
1840 * \brief Handle bridge channel control frame action.
1843 * \param bridge_channel Channel to execute the control frame action on.
1844 * \param fr Control frame to handle.
1848 static void bridge_channel_handle_control(struct ast_bridge_channel *bridge_channel, struct ast_frame *fr)
1850 struct ast_channel *chan;
1851 struct ast_option_header *aoh;
1854 chan = bridge_channel->chan;
1855 switch (fr->subclass.integer) {
1856 case AST_CONTROL_REDIRECTING:
1857 is_caller = !ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING);
1858 if (ast_channel_redirecting_sub(NULL, chan, fr, 1) &&
1859 ast_channel_redirecting_macro(NULL, chan, fr, is_caller, 1)) {
1860 ast_indicate_data(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
1863 case AST_CONTROL_CONNECTED_LINE:
1864 is_caller = !ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING);
1865 if (ast_channel_connected_line_sub(NULL, chan, fr, 1) &&
1866 ast_channel_connected_line_macro(NULL, chan, fr, is_caller, 1)) {
1867 ast_indicate_data(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
1870 case AST_CONTROL_OPTION:
1872 * Forward option Requests, but only ones we know are safe These
1873 * are ONLY sent by chan_iax2 and I'm not convinced that they
1874 * are useful. I haven't deleted them entirely because I just am
1875 * not sure of the ramifications of removing them.
1878 if (aoh && aoh->flag == AST_OPTION_FLAG_REQUEST) {
1879 switch (ntohs(aoh->option)) {
1880 case AST_OPTION_TONE_VERIFY:
1881 case AST_OPTION_TDD:
1882 case AST_OPTION_RELAXDTMF:
1883 case AST_OPTION_AUDIO_MODE:
1884 case AST_OPTION_DIGIT_DETECT:
1885 case AST_OPTION_FAX_DETECT:
1886 ast_channel_setoption(chan, ntohs(aoh->option), aoh->data,
1887 fr->datalen - sizeof(*aoh), 0);
1894 case AST_CONTROL_ANSWER:
1895 if (ast_channel_state(chan) != AST_STATE_UP) {
1898 ast_indicate(chan, -1);
1902 ast_indicate_data(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
1909 * \brief Handle bridge channel write frame to channel.
1912 * \param bridge_channel Channel to write outgoing frame.
1916 static void bridge_channel_handle_write(struct ast_bridge_channel *bridge_channel)
1918 struct ast_frame *fr;
1920 struct sync_payload *sync_payload;
1922 ast_bridge_channel_lock(bridge_channel);
1923 if (read(bridge_channel->alert_pipe[0], &nudge, sizeof(nudge)) < 0) {
1924 if (errno != EINTR && errno != EAGAIN) {
1925 ast_log(LOG_WARNING, "read() failed for alert pipe on %p(%s): %s\n",
1926 bridge_channel, ast_channel_name(bridge_channel->chan), strerror(errno));
1929 fr = AST_LIST_REMOVE_HEAD(&bridge_channel->wr_queue, frame_list);
1930 ast_bridge_channel_unlock(bridge_channel);
1934 switch (fr->frametype) {
1935 case AST_FRAME_BRIDGE_ACTION:
1936 bridge_channel_handle_action(bridge_channel, fr->subclass.integer, fr->data.ptr);
1938 case AST_FRAME_BRIDGE_ACTION_SYNC:
1939 sync_payload = fr->data.ptr;
1940 bridge_channel_handle_action(bridge_channel, fr->subclass.integer, sync_payload->data);
1942 case AST_FRAME_CONTROL:
1943 bridge_channel_handle_control(bridge_channel, fr);
1945 case AST_FRAME_NULL:
1948 /* Write the frame to the channel. */
1949 bridge_channel->activity = BRIDGE_CHANNEL_THREAD_SIMPLE;
1950 ast_write(bridge_channel->chan, fr);
1953 bridge_frame_free(fr);
1956 /*! \brief Internal function to handle DTMF from a channel */
1957 static struct ast_frame *bridge_handle_dtmf(struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
1959 struct ast_bridge_features *features = bridge_channel->features;
1960 struct ast_bridge_hook_dtmf *hook;
1963 /* See if this DTMF matches the beginning of any feature hooks. */
1964 dtmf[0] = frame->subclass.integer;
1966 hook = ao2_find(features->dtmf_hooks, dtmf, OBJ_PARTIAL_KEY);
1968 enum ast_frame_type frametype = frame->frametype;
1970 bridge_frame_free(frame);
1975 /* Collect any more needed DTMF to execute a hook. */
1976 bridge_channel_suspend(bridge_channel);
1977 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1978 switch (frametype) {
1979 case AST_FRAME_DTMF_BEGIN:
1980 bridge_channel_feature(bridge_channel, NULL);
1982 case AST_FRAME_DTMF_END:
1983 bridge_channel_feature(bridge_channel, dtmf);
1986 /* Unexpected frame type. */
1990 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1991 bridge_channel_unsuspend(bridge_channel);
1992 #ifdef TEST_FRAMEWORK
1993 } else if (frame->frametype == AST_FRAME_DTMF_END) {
1994 /* Only transmit this event on DTMF end or else every DTMF
1995 * press will result in the event being broadcast twice
1997 ast_test_suite_event_notify("FEATURE_DETECTION", "Result: fail");
2007 * \brief Feed notification that a frame is waiting on a channel into the bridging core
2009 * \param bridge_channel Bridge channel the notification was received on
2011 static void bridge_handle_trip(struct ast_bridge_channel *bridge_channel)
2013 struct ast_frame *frame;
2015 if (bridge_channel->features->mute) {
2016 frame = ast_read_noaudio(bridge_channel->chan);
2018 frame = ast_read(bridge_channel->chan);
2022 ast_bridge_channel_kick(bridge_channel, 0);
2025 switch (frame->frametype) {
2026 case AST_FRAME_CONTROL:
2027 switch (frame->subclass.integer) {
2028 case AST_CONTROL_HANGUP:
2029 ast_bridge_channel_kick(bridge_channel, 0);
2030 bridge_frame_free(frame);
2036 case AST_FRAME_DTMF_BEGIN:
2037 case AST_FRAME_DTMF_END:
2038 frame = bridge_handle_dtmf(bridge_channel, frame);
2042 if (!bridge_channel->features->dtmf_passthrough) {
2043 bridge_frame_free(frame);
2051 /* Simply write the frame out to the bridge technology. */
2052 bridge_channel_write_frame(bridge_channel, frame);
2053 bridge_frame_free(frame);
2058 * \brief Determine how long till the next timer interval.
2061 * \param bridge_channel Channel to determine how long can wait.
2063 * \retval ms Number of milliseconds to wait.
2064 * \retval -1 to wait forever.
2066 static int bridge_channel_next_interval(struct ast_bridge_channel *bridge_channel)
2068 struct ast_heap *interval_hooks = bridge_channel->features->interval_hooks;
2069 struct ast_bridge_hook_timer *hook;
2072 ast_heap_wrlock(interval_hooks);
2073 hook = ast_heap_peek(interval_hooks, 1);
2075 ms = ast_tvdiff_ms(hook->timer.trip_time, ast_tvnow());
2077 /* Expire immediately. An interval hook is ready to run. */
2081 /* No hook so wait forever. */
2084 ast_heap_unlock(interval_hooks);
2091 * \brief Wait for something to happen on the bridge channel and handle it.
2094 * \param bridge_channel Channel to wait.
2096 * \note Each channel does writing/reading in their own thread.
2100 static void bridge_channel_wait(struct ast_bridge_channel *bridge_channel)
2104 struct ast_channel *chan;
2106 /* Wait for data to either come from the channel or us to be signaled */
2107 ast_bridge_channel_lock(bridge_channel);
2108 if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
2109 } else if (bridge_channel->suspended) {
2110 /* XXX ASTERISK-21271 the external party use of suspended will go away as will these references because this is the bridge channel thread */
2111 ast_debug(1, "Bridge %s: %p(%s) is going into a signal wait\n",
2112 bridge_channel->bridge->uniqueid, bridge_channel,
2113 ast_channel_name(bridge_channel->chan));
2114 ast_cond_wait(&bridge_channel->cond, ao2_object_get_lockaddr(bridge_channel));
2116 ast_bridge_channel_unlock(bridge_channel);
2118 ms = bridge_channel_next_interval(bridge_channel);
2119 chan = ast_waitfor_nandfds(&bridge_channel->chan, 1,
2120 &bridge_channel->alert_pipe[0], 1, NULL, &outfd, &ms);
2121 if (ast_channel_softhangup_internal_flag(bridge_channel->chan) & AST_SOFTHANGUP_UNBRIDGE) {
2122 ast_channel_clear_softhangup(bridge_channel->chan, AST_SOFTHANGUP_UNBRIDGE);
2123 ast_bridge_channel_lock_bridge(bridge_channel);
2124 bridge_channel->bridge->reconfigured = 1;
2125 bridge_reconfigured(bridge_channel->bridge, 0);
2126 ast_bridge_unlock(bridge_channel->bridge);
2128 ast_bridge_channel_lock(bridge_channel);
2129 bridge_channel->activity = BRIDGE_CHANNEL_THREAD_FRAME;
2130 ast_bridge_channel_unlock(bridge_channel);
2131 if (!bridge_channel->suspended
2132 && bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
2134 bridge_handle_trip(bridge_channel);
2135 } else if (-1 < outfd) {
2136 bridge_channel_handle_write(bridge_channel);
2137 } else if (ms == 0) {
2138 /* An interval expired. */
2139 bridge_channel_handle_interval(bridge_channel);
2142 bridge_channel->activity = BRIDGE_CHANNEL_THREAD_IDLE;
2145 ast_bridge_channel_unlock(bridge_channel);
2150 * \brief Handle bridge channel join/leave event.
2153 * \param bridge_channel Which channel is involved.
2154 * \param type Specified join/leave event.
2158 static void bridge_channel_event_join_leave(struct ast_bridge_channel *bridge_channel, enum ast_bridge_hook_type type)
2160 struct ast_bridge_features *features = bridge_channel->features;
2161 struct ast_bridge_hook *hook;
2162 struct ao2_iterator iter;
2164 /* Run the specified hooks. */
2165 iter = ao2_iterator_init(features->other_hooks, 0);
2166 for (; (hook = ao2_iterator_next(&iter)); ao2_ref(hook, -1)) {
2167 if (hook->type == type) {
2172 /* Found the first specified hook to run. */
2173 bridge_channel_suspend(bridge_channel);
2174 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
2176 if (hook->type == type) {
2177 hook->callback(bridge_channel, hook->hook_pvt);
2178 ao2_unlink(features->other_hooks, hook);
2181 } while ((hook = ao2_iterator_next(&iter)));
2182 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
2183 bridge_channel_unsuspend(bridge_channel);
2185 ao2_iterator_destroy(&iter);
2188 /*! \brief Join a channel to a bridge and handle anything the bridge may want us to do */
2189 int bridge_channel_internal_join(struct ast_bridge_channel *bridge_channel)
2193 ast_format_copy(&bridge_channel->read_format, ast_channel_readformat(bridge_channel->chan));
2194 ast_format_copy(&bridge_channel->write_format, ast_channel_writeformat(bridge_channel->chan));
2196 ast_debug(1, "Bridge %s: %p(%s) is joining\n",
2197 bridge_channel->bridge->uniqueid,
2198 bridge_channel, ast_channel_name(bridge_channel->chan));
2201 * Directly locking the bridge is safe here because nobody else
2202 * knows about this bridge_channel yet.
2204 ast_bridge_lock(bridge_channel->bridge);
2206 /* Make sure we're still good to be put into a bridge */
2207 ast_channel_lock(bridge_channel->chan);
2208 if (ast_channel_internal_bridge(bridge_channel->chan)
2209 || ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_ZOMBIE)) {
2210 ast_channel_unlock(bridge_channel->chan);
2211 ast_bridge_unlock(bridge_channel->bridge);
2212 ast_debug(1, "Bridge %s: %p(%s) failed to join Bridge\n",
2213 bridge_channel->bridge->uniqueid,
2215 ast_channel_name(bridge_channel->chan));
2218 ast_channel_internal_bridge_set(bridge_channel->chan, bridge_channel->bridge);
2219 ast_channel_unlock(bridge_channel->chan);
2221 /* Add the jitterbuffer if the channel requires it */
2222 ast_jb_enable_for_channel(bridge_channel->chan);
2224 if (!bridge_channel->bridge->callid) {
2225 bridge_channel->bridge->callid = ast_read_threadstorage_callid();
2228 if (bridge_channel_internal_push(bridge_channel)) {
2229 int cause = bridge_channel->bridge->cause;
2231 ast_bridge_unlock(bridge_channel->bridge);
2232 ast_bridge_channel_kick(bridge_channel, cause);
2233 ast_bridge_channel_lock_bridge(bridge_channel);
2234 ast_bridge_features_remove(bridge_channel->features,
2235 AST_BRIDGE_HOOK_REMOVE_ON_PULL);
2236 bridge_channel_dissolve_check(bridge_channel);
2239 bridge_reconfigured(bridge_channel->bridge, !bridge_channel->inhibit_colp);
2241 if (bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
2243 * Indicate a source change since this channel is entering the
2244 * bridge system only if the bridge technology is not MULTIMIX
2245 * capable. The MULTIMIX technology has already done it.
2247 if (!(bridge_channel->bridge->technology->capabilities
2248 & AST_BRIDGE_CAPABILITY_MULTIMIX)) {
2249 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCCHANGE);
2252 ast_bridge_unlock(bridge_channel->bridge);
2253 bridge_channel_event_join_leave(bridge_channel, AST_BRIDGE_HOOK_TYPE_JOIN);
2254 while (bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
2255 /* Wait for something to do. */
2256 bridge_channel_wait(bridge_channel);
2258 bridge_channel_event_join_leave(bridge_channel, AST_BRIDGE_HOOK_TYPE_LEAVE);
2259 ast_bridge_channel_lock_bridge(bridge_channel);
2262 bridge_channel_internal_pull(bridge_channel);
2263 bridge_channel_settle_owed_events(bridge_channel->bridge, bridge_channel);
2264 bridge_reconfigured(bridge_channel->bridge, 1);
2266 ast_bridge_unlock(bridge_channel->bridge);
2268 /* Complete any active hold before exiting the bridge. */
2269 if (ast_channel_hold_state(bridge_channel->chan) == AST_CONTROL_HOLD) {
2270 ast_debug(1, "Channel %s simulating UNHOLD for bridge end.\n",
2271 ast_channel_name(bridge_channel->chan));
2272 ast_indicate(bridge_channel->chan, AST_CONTROL_UNHOLD);
2275 /* Complete any partial DTMF digit before exiting the bridge. */
2276 if (ast_channel_sending_dtmf_digit(bridge_channel->chan)) {
2277 ast_channel_end_dtmf(bridge_channel->chan,
2278 ast_channel_sending_dtmf_digit(bridge_channel->chan),
2279 ast_channel_sending_dtmf_tv(bridge_channel->chan), "bridge end");
2282 /* Indicate a source change since this channel is leaving the bridge system. */
2283 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCCHANGE);
2286 * Wait for any dual redirect to complete.
2288 * Must be done while "still in the bridge" for ast_async_goto()
2291 while (ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_BRIDGE_DUAL_REDIRECT_WAIT)) {
2294 ast_channel_lock(bridge_channel->chan);
2295 ast_channel_internal_bridge_set(bridge_channel->chan, NULL);
2296 ast_channel_unlock(bridge_channel->chan);
2298 ast_bridge_channel_restore_formats(bridge_channel);
2303 int bridge_channel_internal_queue_blind_transfer(struct ast_channel *transferee,
2304 const char *exten, const char *context,
2305 transfer_channel_cb new_channel_cb, void *user_data)
2307 RAII_VAR(struct ast_bridge_channel *, transferee_bridge_channel, NULL, ao2_cleanup);
2308 struct blind_transfer_data blind_data;
2310 ast_channel_lock(transferee);
2311 transferee_bridge_channel = ast_channel_get_bridge_channel(transferee);
2312 ast_channel_unlock(transferee);
2314 if (!transferee_bridge_channel) {
2318 if (new_channel_cb) {
2319 new_channel_cb(transferee, user_data, AST_BRIDGE_TRANSFER_SINGLE_PARTY);
2322 ast_copy_string(blind_data.exten, exten, sizeof(blind_data.exten));
2323 ast_copy_string(blind_data.context, context, sizeof(blind_data.context));
2325 return bridge_channel_queue_action_data(transferee_bridge_channel,
2326 BRIDGE_CHANNEL_ACTION_BLIND_TRANSFER, &blind_data, sizeof(blind_data));
2329 int bridge_channel_internal_queue_attended_transfer(struct ast_channel *transferee,
2330 struct ast_channel *unbridged_chan)
2332 RAII_VAR(struct ast_bridge_channel *, transferee_bridge_channel, NULL, ao2_cleanup);
2333 char unbridged_chan_name[AST_CHANNEL_NAME];
2335 ast_channel_lock(transferee);
2336 transferee_bridge_channel = ast_channel_get_bridge_channel(transferee);
2337 ast_channel_unlock(transferee);
2339 if (!transferee_bridge_channel) {
2343 ast_copy_string(unbridged_chan_name, ast_channel_name(unbridged_chan),
2344 sizeof(unbridged_chan_name));
2346 return bridge_channel_queue_action_data(transferee_bridge_channel,
2347 BRIDGE_CHANNEL_ACTION_ATTENDED_TRANSFER, unbridged_chan_name,
2348 sizeof(unbridged_chan_name));
2351 int bridge_channel_internal_allows_optimization(struct ast_bridge_channel *bridge_channel)
2353 return bridge_channel->in_bridge
2354 && AST_LIST_EMPTY(&bridge_channel->wr_queue);
2359 * \brief Close a pipe.
2362 * \param my_pipe What to close.
2366 static void pipe_close(int *my_pipe)
2368 if (my_pipe[0] > -1) {
2372 if (my_pipe[1] > -1) {
2380 * \brief Initialize a pipe as non-blocking.
2383 * \param my_pipe What to initialize.
2385 * \retval 0 on success.
2386 * \retval -1 on error.
2388 static int pipe_init_nonblock(int *my_pipe)
2394 if (pipe(my_pipe)) {
2395 ast_log(LOG_WARNING, "Can't create pipe! Try increasing max file descriptors with ulimit -n\n");
2398 flags = fcntl(my_pipe[0], F_GETFL);
2399 if (fcntl(my_pipe[0], F_SETFL, flags | O_NONBLOCK) < 0) {
2400 ast_log(LOG_WARNING, "Unable to set read pipe nonblocking! (%d: %s)\n",
2401 errno, strerror(errno));
2404 flags = fcntl(my_pipe[1], F_GETFL);
2405 if (fcntl(my_pipe[1], F_SETFL, flags | O_NONBLOCK) < 0) {
2406 ast_log(LOG_WARNING, "Unable to set write pipe nonblocking! (%d: %s)\n",
2407 errno, strerror(errno));
2413 /* Destroy elements of the bridge channel structure and the bridge channel structure itself */
2414 static void bridge_channel_destroy(void *obj)
2416 struct ast_bridge_channel *bridge_channel = obj;
2417 struct ast_frame *fr;
2419 if (bridge_channel->callid) {
2420 bridge_channel->callid = ast_callid_unref(bridge_channel->callid);
2423 if (bridge_channel->bridge) {
2424 ao2_ref(bridge_channel->bridge, -1);
2425 bridge_channel->bridge = NULL;
2428 /* Flush any unhandled wr_queue frames. */
2429 while ((fr = AST_LIST_REMOVE_HEAD(&bridge_channel->wr_queue, frame_list))) {
2430 bridge_frame_free(fr);
2432 pipe_close(bridge_channel->alert_pipe);
2434 ast_cond_destroy(&bridge_channel->cond);
2437 struct ast_bridge_channel *bridge_channel_internal_alloc(struct ast_bridge *bridge)
2439 struct ast_bridge_channel *bridge_channel;
2441 bridge_channel = ao2_alloc(sizeof(struct ast_bridge_channel), bridge_channel_destroy);
2442 if (!bridge_channel) {
2445 ast_cond_init(&bridge_channel->cond, NULL);
2446 if (pipe_init_nonblock(bridge_channel->alert_pipe)) {
2447 ao2_ref(bridge_channel, -1);
2451 bridge_channel->bridge = bridge;
2452 ao2_ref(bridge_channel->bridge, +1);
2455 return bridge_channel;