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 ast_assert(bridge_channel->read_format != NULL);
324 ast_assert(bridge_channel->write_format != NULL);
326 /* Restore original formats of the channel as they came in */
327 if (ast_format_cmp(ast_channel_readformat(bridge_channel->chan), bridge_channel->read_format) == AST_FORMAT_CMP_NOT_EQUAL) {
328 ast_debug(1, "Bridge is returning %p(%s) to read format %s\n",
329 bridge_channel, ast_channel_name(bridge_channel->chan),
330 ast_format_get_name(bridge_channel->read_format));
331 if (ast_set_read_format(bridge_channel->chan, bridge_channel->read_format)) {
332 ast_debug(1, "Bridge failed to return %p(%s) to read format %s\n",
333 bridge_channel, ast_channel_name(bridge_channel->chan),
334 ast_format_get_name(bridge_channel->read_format));
337 if (ast_format_cmp(ast_channel_writeformat(bridge_channel->chan), bridge_channel->write_format) == AST_FORMAT_CMP_NOT_EQUAL) {
338 ast_debug(1, "Bridge is returning %p(%s) to write format %s\n",
339 bridge_channel, ast_channel_name(bridge_channel->chan),
340 ast_format_get_name(bridge_channel->write_format));
341 if (ast_set_write_format(bridge_channel->chan, bridge_channel->write_format)) {
342 ast_debug(1, "Bridge failed to return %p(%s) to write format %s\n",
343 bridge_channel, ast_channel_name(bridge_channel->chan),
344 ast_format_get_name(bridge_channel->write_format));
349 struct ast_bridge *ast_bridge_channel_merge_inhibit(struct ast_bridge_channel *bridge_channel, int request)
351 struct ast_bridge *bridge;
353 ast_bridge_channel_lock_bridge(bridge_channel);
354 bridge = bridge_channel->bridge;
356 bridge_merge_inhibit_nolock(bridge, request);
357 ast_bridge_unlock(bridge);
361 void ast_bridge_channel_update_linkedids(struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
363 struct ast_bridge_channel *other;
364 struct ast_bridge *bridge = bridge_channel->bridge;
365 struct ast_channel *oldest_linkedid_chan = bridge_channel->chan;
367 AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
371 oldest_linkedid_chan = ast_channel_internal_oldest_linkedid(
372 oldest_linkedid_chan, other->chan);
375 ast_channel_lock(bridge_channel->chan);
376 ast_channel_internal_copy_linkedid(bridge_channel->chan, oldest_linkedid_chan);
377 ast_channel_unlock(bridge_channel->chan);
378 AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
382 ast_channel_lock(other->chan);
383 ast_channel_internal_copy_linkedid(other->chan, oldest_linkedid_chan);
384 ast_channel_unlock(other->chan);
390 * \brief Set dest's empty peeraccount with the src's non-empty accountcode.
393 * \param dest Channel to update peeraccount.
394 * \param src Channel to get accountcode from.
396 * \note Both channels are already locked.
400 static void channel_fill_empty_peeraccount(struct ast_channel *dest, struct ast_channel *src)
402 if (ast_strlen_zero(ast_channel_peeraccount(dest))
403 && !ast_strlen_zero(ast_channel_accountcode(src))) {
404 ast_debug(1, "Setting channel %s peeraccount with channel %s accountcode '%s'.\n",
405 ast_channel_name(dest),
406 ast_channel_name(src), ast_channel_accountcode(src));
407 ast_channel_peeraccount_set(dest, ast_channel_accountcode(src));
413 * \brief Set dest's empty accountcode with the src's non-empty peeraccount.
416 * \param dest Channel to update accountcode.
417 * \param src Channel to get peeraccount from.
419 * \note Both channels are already locked.
423 static void channel_fill_empty_accountcode(struct ast_channel *dest, struct ast_channel *src)
425 if (ast_strlen_zero(ast_channel_accountcode(dest))
426 && !ast_strlen_zero(ast_channel_peeraccount(src))) {
427 ast_debug(1, "Setting channel %s accountcode with channel %s peeraccount '%s'.\n",
428 ast_channel_name(dest),
429 ast_channel_name(src), ast_channel_peeraccount(src));
430 ast_channel_accountcode_set(dest, ast_channel_peeraccount(src));
436 * \brief Set empty peeraccount and accountcode in a channel from the other channel.
439 * \param c0 First bridge channel to update.
440 * \param c1 Second bridge channel to update.
442 * \note Both channels are already locked.
446 static void channel_set_empty_accountcodes(struct ast_channel *c0, struct ast_channel *c1)
448 /* Set empty peeraccount from the other channel's accountcode. */
449 channel_fill_empty_peeraccount(c0, c1);
450 channel_fill_empty_peeraccount(c1, c0);
452 /* Set empty accountcode from the other channel's peeraccount. */
453 channel_fill_empty_accountcode(c0, c1);
454 channel_fill_empty_accountcode(c1, c0);
459 * \brief Update dest's peeraccount with the src's different accountcode.
462 * \param dest Channel to update peeraccount.
463 * \param src Channel to get accountcode from.
465 * \note Both channels are already locked.
469 static void channel_update_peeraccount(struct ast_channel *dest, struct ast_channel *src)
471 if (strcmp(ast_channel_accountcode(src), ast_channel_peeraccount(dest))) {
472 ast_debug(1, "Changing channel %s peeraccount '%s' to match channel %s accountcode '%s'.\n",
473 ast_channel_name(dest), ast_channel_peeraccount(dest),
474 ast_channel_name(src), ast_channel_accountcode(src));
475 ast_channel_peeraccount_set(dest, ast_channel_accountcode(src));
481 * \brief Update peeraccounts to match the other channel's accountcode.
484 * \param c0 First channel to update.
485 * \param c1 Second channel to update.
487 * \note Both channels are already locked.
491 static void channel_update_peeraccounts(struct ast_channel *c0, struct ast_channel *c1)
493 channel_update_peeraccount(c0, c1);
494 channel_update_peeraccount(c1, c0);
499 * \brief Update channel accountcodes because a channel is joining a bridge.
502 * \param joining Channel joining the bridge.
503 * \param swap Channel being replaced by the joining channel. May be NULL.
505 * \note The bridge must be locked prior to calling this function.
509 static void bridge_channel_update_accountcodes_joining(struct ast_bridge_channel *joining, struct ast_bridge_channel *swap)
511 struct ast_bridge *bridge = joining->bridge;
512 struct ast_bridge_channel *other;
513 unsigned int swap_in_bridge = 0;
514 unsigned int will_be_two_party;
517 * Only update the peeraccount to match if the joining channel
518 * will make it a two party bridge.
520 if (bridge->num_channels <= 2 && swap) {
521 AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
528 will_be_two_party = (1 == bridge->num_channels - swap_in_bridge);
530 AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
534 ast_assert(joining != other);
535 ast_channel_lock_both(joining->chan, other->chan);
536 channel_set_empty_accountcodes(joining->chan, other->chan);
537 if (will_be_two_party) {
538 channel_update_peeraccounts(joining->chan, other->chan);
540 ast_channel_unlock(joining->chan);
541 ast_channel_unlock(other->chan);
547 * \brief Update channel peeraccount codes because a channel has left a bridge.
550 * \param leaving Channel leaving the bridge. (Has already been removed actually)
552 * \note The bridge must be locked prior to calling this function.
556 static void bridge_channel_update_accountcodes_leaving(struct ast_bridge_channel *leaving)
558 struct ast_bridge *bridge = leaving->bridge;
559 struct ast_bridge_channel *first;
560 struct ast_bridge_channel *second;
562 if (bridge->num_channels != 2 || bridge->dissolved) {
566 first = AST_LIST_FIRST(&bridge->channels);
567 second = AST_LIST_LAST(&bridge->channels);
568 ast_assert(first && first != second);
569 ast_channel_lock_both(first->chan, second->chan);
570 channel_set_empty_accountcodes(first->chan, second->chan);
571 channel_update_peeraccounts(first->chan, second->chan);
572 ast_channel_unlock(second->chan);
573 ast_channel_unlock(first->chan);
576 void ast_bridge_channel_update_accountcodes(struct ast_bridge_channel *joining, struct ast_bridge_channel *leaving)
579 bridge_channel_update_accountcodes_joining(joining, leaving);
581 bridge_channel_update_accountcodes_leaving(leaving);
585 void ast_bridge_channel_kick(struct ast_bridge_channel *bridge_channel, int cause)
587 struct ast_bridge_features *features = bridge_channel->features;
588 struct ast_bridge_hook *hook;
589 struct ao2_iterator iter;
591 ast_bridge_channel_lock(bridge_channel);
592 if (bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
593 channel_set_cause(bridge_channel->chan, cause);
596 ast_bridge_channel_unlock(bridge_channel);
598 /* Run any hangup hooks. */
599 iter = ao2_iterator_init(features->other_hooks, 0);
600 for (; (hook = ao2_iterator_next(&iter)); ao2_ref(hook, -1)) {
603 if (hook->type != AST_BRIDGE_HOOK_TYPE_HANGUP) {
606 remove_me = hook->callback(bridge_channel, hook->hook_pvt);
608 ast_debug(1, "Hangup hook %p is being removed from %p(%s)\n",
609 hook, bridge_channel, ast_channel_name(bridge_channel->chan));
610 ao2_unlink(features->other_hooks, hook);
613 ao2_iterator_destroy(&iter);
615 /* Default hangup action. */
616 ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END, cause);
621 * \brief Write an \ref ast_frame onto the bridge channel
624 * \param bridge_channel Which channel to queue the frame onto.
625 * \param frame The frame to write onto the bridge_channel
627 * \retval 0 on success.
628 * \retval -1 on error.
630 static int bridge_channel_write_frame(struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
632 ast_assert(frame->frametype != AST_FRAME_BRIDGE_ACTION_SYNC);
634 ast_bridge_channel_lock_bridge(bridge_channel);
636 * XXX need to implement a deferred write queue for when there
637 * is no peer channel in the bridge (yet or it was kicked).
639 * The tech decides if a frame needs to be pushed back for deferral.
640 * simple_bridge/native_bridge are likely the only techs that will do this.
642 bridge_channel->bridge->technology->write(bridge_channel->bridge, bridge_channel, frame);
644 /* Remember any owed events to the bridge. */
645 switch (frame->frametype) {
646 case AST_FRAME_DTMF_BEGIN:
647 bridge_channel->owed.dtmf_tv = ast_tvnow();
648 bridge_channel->owed.dtmf_digit = frame->subclass.integer;
650 case AST_FRAME_DTMF_END:
651 bridge_channel->owed.dtmf_digit = '\0';
653 case AST_FRAME_CONTROL:
655 * We explicitly will not remember HOLD/UNHOLD frames because
656 * things like attended transfers will handle them.
661 ast_bridge_unlock(bridge_channel->bridge);
664 * Claim successful write to bridge. If deferred frame
665 * support is added, claim successfully deferred.
670 void bridge_channel_settle_owed_events(struct ast_bridge *orig_bridge, struct ast_bridge_channel *bridge_channel)
672 if (bridge_channel->owed.dtmf_digit) {
673 struct ast_frame frame = {
674 .frametype = AST_FRAME_DTMF_END,
675 .subclass.integer = bridge_channel->owed.dtmf_digit,
676 .src = "Bridge channel owed DTMF",
679 frame.len = ast_tvdiff_ms(ast_tvnow(), bridge_channel->owed.dtmf_tv);
680 if (frame.len < option_dtmfminduration) {
681 frame.len = option_dtmfminduration;
683 ast_log(LOG_DTMF, "DTMF end '%c' simulated to bridge %s because %s left. Duration %ld ms.\n",
684 bridge_channel->owed.dtmf_digit, orig_bridge->uniqueid,
685 ast_channel_name(bridge_channel->chan), frame.len);
686 bridge_channel->owed.dtmf_digit = '\0';
687 orig_bridge->technology->write(orig_bridge, NULL, &frame);
693 * \brief Suspend a channel from a bridge.
695 * \param bridge_channel Channel to suspend.
697 * \note This function assumes bridge_channel->bridge is locked.
701 void bridge_channel_internal_suspend_nolock(struct ast_bridge_channel *bridge_channel)
703 bridge_channel->suspended = 1;
704 if (bridge_channel->in_bridge) {
705 --bridge_channel->bridge->num_active;
708 /* Get technology bridge threads off of the channel. */
709 if (bridge_channel->bridge->technology->suspend) {
710 bridge_channel->bridge->technology->suspend(bridge_channel->bridge, bridge_channel);
716 * \brief Suspend a channel from a bridge.
718 * \param bridge_channel Channel to suspend.
722 static void bridge_channel_suspend(struct ast_bridge_channel *bridge_channel)
724 ast_bridge_channel_lock_bridge(bridge_channel);
725 bridge_channel_internal_suspend_nolock(bridge_channel);
726 ast_bridge_unlock(bridge_channel->bridge);
731 * \brief Unsuspend a channel from a bridge.
733 * \param bridge_channel Channel to unsuspend.
735 * \note This function assumes bridge_channel->bridge is locked.
739 void bridge_channel_internal_unsuspend_nolock(struct ast_bridge_channel *bridge_channel)
741 bridge_channel->suspended = 0;
742 if (bridge_channel->in_bridge) {
743 ++bridge_channel->bridge->num_active;
746 /* Wake technology bridge threads to take care of channel again. */
747 if (bridge_channel->bridge->technology->unsuspend) {
748 bridge_channel->bridge->technology->unsuspend(bridge_channel->bridge, bridge_channel);
751 /* Wake suspended channel. */
752 ast_bridge_channel_lock(bridge_channel);
753 ast_cond_signal(&bridge_channel->cond);
754 ast_bridge_channel_unlock(bridge_channel);
759 * \brief Unsuspend a channel from a bridge.
761 * \param bridge_channel Channel to unsuspend.
765 static void bridge_channel_unsuspend(struct ast_bridge_channel *bridge_channel)
767 ast_bridge_channel_lock_bridge(bridge_channel);
768 bridge_channel_internal_unsuspend_nolock(bridge_channel);
769 ast_bridge_unlock(bridge_channel->bridge);
774 * \brief Queue an action frame onto the bridge channel with data.
777 * \param bridge_channel Which channel to queue the frame onto.
778 * \param action Type of bridge action frame.
779 * \param data Frame payload data to pass.
780 * \param datalen Frame payload data length to pass.
782 * \retval 0 on success.
783 * \retval -1 on error.
785 static int bridge_channel_queue_action_data(struct ast_bridge_channel *bridge_channel,
786 enum bridge_channel_action_type action, const void *data, size_t datalen)
788 struct ast_frame frame = {
789 .frametype = AST_FRAME_BRIDGE_ACTION,
790 .subclass.integer = action,
792 .data.ptr = (void *) data,
795 return ast_bridge_channel_queue_frame(bridge_channel, &frame);
800 * \brief Queue an action frame onto the bridge channel with data synchronously.
803 * The function will not return until the queued frame is freed.
805 * \param bridge_channel Which channel to queue the frame onto.
806 * \param action Type of bridge action frame.
807 * \param data Frame payload data to pass.
808 * \param datalen Frame payload data length to pass.
810 * \retval 0 on success.
811 * \retval -1 on error.
813 static int bridge_channel_queue_action_data_sync(struct ast_bridge_channel *bridge_channel,
814 enum bridge_channel_action_type action, const void *data, size_t datalen)
816 struct sync_payload *sync_payload;
817 int sync_payload_len = sizeof(*sync_payload) + datalen;
818 struct bridge_sync sync_struct;
819 struct ast_frame frame = {
820 .frametype = AST_FRAME_BRIDGE_ACTION_SYNC,
821 .subclass.integer = action,
824 /* Make sure we don't end up trying to wait on ourself to deliver the frame */
825 ast_assert(!pthread_equal(pthread_self(), bridge_channel->thread));
827 sync_payload = ast_alloca(sync_payload_len);
828 sync_payload->id = ast_atomic_fetchadd_int(&sync_ids, +1);
829 memcpy(sync_payload->data, data, datalen);
831 frame.datalen = sync_payload_len;
832 frame.data.ptr = sync_payload;
834 bridge_sync_init(&sync_struct, sync_payload->id);
835 if (ast_bridge_channel_queue_frame(bridge_channel, &frame)) {
836 bridge_sync_cleanup(&sync_struct);
840 bridge_sync_wait(&sync_struct);
841 bridge_sync_cleanup(&sync_struct);
846 * \brief Write an action frame onto the bridge channel with data.
849 * \param bridge_channel Which channel to queue the frame onto.
850 * \param action Type of bridge action frame.
851 * \param data Frame payload data to pass.
852 * \param datalen Frame payload data length to pass.
854 * \retval 0 on success.
855 * \retval -1 on error.
857 static int bridge_channel_write_action_data(struct ast_bridge_channel *bridge_channel,
858 enum bridge_channel_action_type action, const void *data, size_t datalen)
860 struct ast_frame frame = {
861 .frametype = AST_FRAME_BRIDGE_ACTION,
862 .subclass.integer = action,
864 .data.ptr = (void *) data,
867 return bridge_channel_write_frame(bridge_channel, &frame);
870 static void bridge_frame_free(struct ast_frame *frame)
872 if (frame->frametype == AST_FRAME_BRIDGE_ACTION_SYNC) {
873 struct sync_payload *sync_payload = frame->data.ptr;
874 struct bridge_sync *sync;
876 AST_RWLIST_RDLOCK(&sync_structs);
877 AST_RWLIST_TRAVERSE(&sync_structs, sync, list) {
878 if (sync->id == sync_payload->id) {
883 bridge_sync_signal(sync);
885 AST_RWLIST_UNLOCK(&sync_structs);
891 int ast_bridge_channel_queue_frame(struct ast_bridge_channel *bridge_channel, struct ast_frame *fr)
893 struct ast_frame *dup;
896 if (bridge_channel->suspended
897 /* Also defer DTMF frames. */
898 && fr->frametype != AST_FRAME_DTMF_BEGIN
899 && fr->frametype != AST_FRAME_DTMF_END
900 && !ast_is_deferrable_frame(fr)) {
901 /* Drop non-deferable frames when suspended. */
904 if (fr->frametype == AST_FRAME_NULL) {
905 /* "Accept" the frame and discard it. */
914 ast_bridge_channel_lock(bridge_channel);
915 if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
916 /* Drop frames on channels leaving the bridge. */
917 ast_bridge_channel_unlock(bridge_channel);
918 bridge_frame_free(dup);
922 AST_LIST_INSERT_TAIL(&bridge_channel->wr_queue, dup, frame_list);
923 if (write(bridge_channel->alert_pipe[1], &nudge, sizeof(nudge)) != sizeof(nudge)) {
924 ast_log(LOG_ERROR, "We couldn't write alert pipe for %p(%s)... something is VERY wrong\n",
925 bridge_channel, ast_channel_name(bridge_channel->chan));
927 ast_bridge_channel_unlock(bridge_channel);
931 int ast_bridge_queue_everyone_else(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
933 struct ast_bridge_channel *cur;
934 int not_written = -1;
936 if (frame->frametype == AST_FRAME_NULL) {
937 /* "Accept" the frame and discard it. */
941 AST_LIST_TRAVERSE(&bridge->channels, cur, entry) {
942 if (cur == bridge_channel) {
945 if (!ast_bridge_channel_queue_frame(cur, frame)) {
952 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)
954 struct ast_frame frame = {
955 .frametype = AST_FRAME_CONTROL,
956 .subclass.integer = control,
958 .data.ptr = (void *) data,
961 return ast_bridge_channel_queue_frame(bridge_channel, &frame);
964 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)
966 struct ast_frame frame = {
967 .frametype = AST_FRAME_CONTROL,
968 .subclass.integer = control,
970 .data.ptr = (void *) data,
973 return bridge_channel_write_frame(bridge_channel, &frame);
976 int ast_bridge_channel_write_hold(struct ast_bridge_channel *bridge_channel, const char *moh_class)
978 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
981 if (!ast_strlen_zero(moh_class)) {
982 datalen = strlen(moh_class) + 1;
984 blob = ast_json_pack("{s: s}",
985 "musicclass", moh_class);
991 ast_channel_publish_cached_blob(bridge_channel->chan, ast_channel_hold_type(), blob);
993 return ast_bridge_channel_write_control_data(bridge_channel, AST_CONTROL_HOLD,
997 int ast_bridge_channel_write_unhold(struct ast_bridge_channel *bridge_channel)
999 ast_channel_publish_cached_blob(bridge_channel->chan, ast_channel_unhold_type(), NULL);
1001 return ast_bridge_channel_write_control_data(bridge_channel, AST_CONTROL_UNHOLD, NULL, 0);
1006 * \brief Helper function to kick off a PBX app on a bridge_channel
1008 static int run_app_helper(struct ast_channel *chan, const char *app_name, const char *app_args)
1012 if (!strcasecmp("Gosub", app_name)) {
1013 ast_app_exec_sub(NULL, chan, app_args, 0);
1014 } else if (!strcasecmp("Macro", app_name)) {
1015 ast_app_exec_macro(NULL, chan, app_args);
1017 struct ast_app *app;
1019 app = pbx_findapp(app_name);
1021 ast_log(LOG_WARNING, "Could not find application (%s)\n", app_name);
1023 struct ast_str *substituted_args = ast_str_create(16);
1025 if (substituted_args) {
1026 ast_str_substitute_variables(&substituted_args, 0, chan, app_args);
1027 res = pbx_exec(chan, app, ast_str_buffer(substituted_args));
1028 ast_free(substituted_args);
1030 ast_log(LOG_WARNING, "Could not substitute application argument variables for %s\n", app_name);
1031 res = pbx_exec(chan, app, app_args);
1038 void ast_bridge_channel_run_app(struct ast_bridge_channel *bridge_channel, const char *app_name, const char *app_args, const char *moh_class)
1041 ast_bridge_channel_write_hold(bridge_channel, moh_class);
1043 if (run_app_helper(bridge_channel->chan, app_name, S_OR(app_args, ""))) {
1044 /* Break the bridge if the app returns non-zero. */
1045 ast_bridge_channel_kick(bridge_channel, AST_CAUSE_NORMAL_CLEARING);
1048 ast_bridge_channel_write_unhold(bridge_channel);
1052 struct bridge_run_app {
1053 /*! Offset into app_name[] where the MOH class name starts. (zero if no MOH) */
1055 /*! Offset into app_name[] where the application argument string starts. (zero if no arguments) */
1056 int app_args_offset;
1057 /*! Application name to run. */
1063 * \brief Handle the run application bridge action.
1066 * \param bridge_channel Which channel to run the application on.
1067 * \param data Action frame data to run the application.
1071 static void bridge_channel_run_app(struct ast_bridge_channel *bridge_channel, struct bridge_run_app *data)
1073 ast_bridge_channel_run_app(bridge_channel, data->app_name,
1074 data->app_args_offset ? &data->app_name[data->app_args_offset] : NULL,
1075 data->moh_offset ? &data->app_name[data->moh_offset] : NULL);
1080 * \brief Marshal an application to be executed on a bridge_channel
1082 static int payload_helper_app(ast_bridge_channel_post_action_data post_it,
1083 struct ast_bridge_channel *bridge_channel, const char *app_name, const char *app_args, const char *moh_class)
1085 struct bridge_run_app *app_data;
1086 size_t len_name = strlen(app_name) + 1;
1087 size_t len_args = ast_strlen_zero(app_args) ? 0 : strlen(app_args) + 1;
1088 size_t len_moh = !moh_class ? 0 : strlen(moh_class) + 1;
1089 size_t len_data = sizeof(*app_data) + len_name + len_args + len_moh;
1091 /* Fill in application run frame data. */
1092 app_data = alloca(len_data);
1093 app_data->app_args_offset = len_args ? len_name : 0;
1094 app_data->moh_offset = len_moh ? len_name + len_args : 0;
1095 strcpy(app_data->app_name, app_name);/* Safe */
1097 strcpy(&app_data->app_name[app_data->app_args_offset], app_args);/* Safe */
1100 strcpy(&app_data->app_name[app_data->moh_offset], moh_class);/* Safe */
1103 return post_it(bridge_channel, BRIDGE_CHANNEL_ACTION_RUN_APP, app_data, len_data);
1106 int ast_bridge_channel_write_app(struct ast_bridge_channel *bridge_channel, const char *app_name, const char *app_args, const char *moh_class)
1108 return payload_helper_app(bridge_channel_write_action_data,
1109 bridge_channel, app_name, app_args, moh_class);
1112 int ast_bridge_channel_queue_app(struct ast_bridge_channel *bridge_channel, const char *app_name, const char *app_args, const char *moh_class)
1114 return payload_helper_app(bridge_channel_queue_action_data,
1115 bridge_channel, app_name, app_args, moh_class);
1118 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)
1121 ast_bridge_channel_write_hold(bridge_channel, moh_class);
1124 custom_play(bridge_channel, playfile);
1126 ast_stream_and_wait(bridge_channel->chan, playfile, AST_DIGIT_NONE);
1129 ast_bridge_channel_write_unhold(bridge_channel);
1133 * It may be necessary to resume music on hold after we finish
1134 * playing the announcment.
1136 if (ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_MOH)) {
1137 const char *latest_musicclass;
1139 ast_channel_lock(bridge_channel->chan);
1140 latest_musicclass = ast_strdupa(ast_channel_latest_musicclass(bridge_channel->chan));
1141 ast_channel_unlock(bridge_channel->chan);
1142 ast_moh_start(bridge_channel->chan, latest_musicclass, NULL);
1146 struct bridge_playfile {
1147 /*! Call this function to play the playfile. (NULL if normal sound file to play) */
1148 ast_bridge_custom_play_fn custom_play;
1149 /*! Offset into playfile[] where the MOH class name starts. (zero if no MOH)*/
1151 /*! Filename to play. */
1157 * \brief Handle the playfile bridge action.
1160 * \param bridge_channel Which channel to play a file on.
1161 * \param payload Action frame payload to play a file.
1165 static void bridge_channel_playfile(struct ast_bridge_channel *bridge_channel, struct bridge_playfile *payload)
1167 ast_bridge_channel_playfile(bridge_channel, payload->custom_play, payload->playfile,
1168 payload->moh_offset ? &payload->playfile[payload->moh_offset] : NULL);
1173 * \brief Marshal a file to be played on a bridge_channel
1175 static int payload_helper_playfile(ast_bridge_channel_post_action_data post_it,
1176 struct ast_bridge_channel *bridge_channel, ast_bridge_custom_play_fn custom_play, const char *playfile, const char *moh_class)
1178 struct bridge_playfile *payload;
1179 size_t len_name = strlen(playfile) + 1;
1180 size_t len_moh = !moh_class ? 0 : strlen(moh_class) + 1;
1181 size_t len_payload = sizeof(*payload) + len_name + len_moh;
1183 /* Fill in play file frame data. */
1184 payload = ast_alloca(len_payload);
1185 payload->custom_play = custom_play;
1186 payload->moh_offset = len_moh ? len_name : 0;
1187 strcpy(payload->playfile, playfile);/* Safe */
1189 strcpy(&payload->playfile[payload->moh_offset], moh_class);/* Safe */
1192 return post_it(bridge_channel, BRIDGE_CHANNEL_ACTION_PLAY_FILE, payload, len_payload);
1195 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)
1197 return payload_helper_playfile(bridge_channel_write_action_data,
1198 bridge_channel, custom_play, playfile, moh_class);
1201 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)
1203 return payload_helper_playfile(bridge_channel_queue_action_data,
1204 bridge_channel, custom_play, playfile, moh_class);
1207 int ast_bridge_channel_queue_playfile_sync(struct ast_bridge_channel *bridge_channel,
1208 ast_bridge_custom_play_fn custom_play, const char *playfile, const char *moh_class)
1210 return payload_helper_playfile(bridge_channel_queue_action_data_sync,
1211 bridge_channel, custom_play, playfile, moh_class);
1214 struct bridge_custom_callback {
1215 /*! Call this function on the bridge channel thread. */
1216 ast_bridge_custom_callback_fn callback;
1217 /*! Size of the payload if it exists. A number otherwise. */
1218 size_t payload_size;
1219 /*! Option flags determining how callback is called. */
1221 /*! Nonzero if the payload exists. */
1222 char payload_exists;
1223 /*! Payload to give to callback. */
1229 * \brief Handle the do custom callback bridge action.
1232 * \param bridge_channel Which channel to call the callback on.
1233 * \param data Action frame data to call the callback.
1237 static void bridge_channel_do_callback(struct ast_bridge_channel *bridge_channel, struct bridge_custom_callback *data)
1239 if (ast_test_flag(data, AST_BRIDGE_CHANNEL_CB_OPTION_MEDIA)) {
1240 bridge_channel_suspend(bridge_channel);
1241 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1243 data->callback(bridge_channel, data->payload_exists ? data->payload : NULL, data->payload_size);
1244 if (ast_test_flag(data, AST_BRIDGE_CHANNEL_CB_OPTION_MEDIA)) {
1245 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1246 bridge_channel_unsuspend(bridge_channel);
1252 * \brief Marshal a custom callback function to be called on a bridge_channel
1254 static int payload_helper_cb(ast_bridge_channel_post_action_data post_it,
1255 struct ast_bridge_channel *bridge_channel,
1256 enum ast_bridge_channel_custom_callback_option flags,
1257 ast_bridge_custom_callback_fn callback, const void *payload, size_t payload_size)
1259 struct bridge_custom_callback *cb_data;
1260 size_t len_data = sizeof(*cb_data) + (payload ? payload_size : 0);
1268 /* Fill in custom callback frame data. */
1269 cb_data = alloca(len_data);
1270 cb_data->callback = callback;
1271 cb_data->payload_size = payload_size;
1272 cb_data->flags = flags;
1273 cb_data->payload_exists = payload && payload_size;
1274 if (cb_data->payload_exists) {
1275 memcpy(cb_data->payload, payload, payload_size);/* Safe */
1278 return post_it(bridge_channel, BRIDGE_CHANNEL_ACTION_CALLBACK, cb_data, len_data);
1281 int ast_bridge_channel_write_callback(struct ast_bridge_channel *bridge_channel,
1282 enum ast_bridge_channel_custom_callback_option flags,
1283 ast_bridge_custom_callback_fn callback, const void *payload, size_t payload_size)
1285 return payload_helper_cb(bridge_channel_write_action_data,
1286 bridge_channel, flags, callback, payload, payload_size);
1289 int ast_bridge_channel_queue_callback(struct ast_bridge_channel *bridge_channel,
1290 enum ast_bridge_channel_custom_callback_option flags,
1291 ast_bridge_custom_callback_fn callback, const void *payload, size_t payload_size)
1293 return payload_helper_cb(bridge_channel_queue_action_data,
1294 bridge_channel, flags, callback, payload, payload_size);
1297 struct bridge_park {
1298 int parker_uuid_offset;
1299 int app_data_offset;
1300 /* buffer used for holding those strings */
1301 char parkee_uuid[0];
1306 * \brief Park a bridge_cahnnel
1308 static void bridge_channel_park(struct ast_bridge_channel *bridge_channel, struct bridge_park *payload)
1310 if (!ast_parking_provider_registered()) {
1311 ast_log(AST_LOG_WARNING, "Unable to park %s: No parking provider loaded!\n",
1312 ast_channel_name(bridge_channel->chan));
1316 if (ast_parking_park_bridge_channel(bridge_channel, payload->parkee_uuid,
1317 &payload->parkee_uuid[payload->parker_uuid_offset],
1318 payload->app_data_offset ? &payload->parkee_uuid[payload->app_data_offset] : NULL)) {
1319 ast_log(AST_LOG_WARNING, "Error occurred while parking %s\n",
1320 ast_channel_name(bridge_channel->chan));
1326 * \brief Marshal a park action onto a bridge_channel
1328 static int payload_helper_park(ast_bridge_channel_post_action_data post_it,
1329 struct ast_bridge_channel *bridge_channel,
1330 const char *parkee_uuid,
1331 const char *parker_uuid,
1332 const char *app_data)
1334 struct bridge_park *payload;
1335 size_t len_parkee_uuid = strlen(parkee_uuid) + 1;
1336 size_t len_parker_uuid = strlen(parker_uuid) + 1;
1337 size_t len_app_data = !app_data ? 0 : strlen(app_data) + 1;
1338 size_t len_payload = sizeof(*payload) + len_parker_uuid + len_parkee_uuid + len_app_data;
1340 payload = alloca(len_payload);
1341 payload->app_data_offset = len_app_data ? len_parkee_uuid + len_parker_uuid : 0;
1342 payload->parker_uuid_offset = len_parkee_uuid;
1343 strcpy(payload->parkee_uuid, parkee_uuid);
1344 strcpy(&payload->parkee_uuid[payload->parker_uuid_offset], parker_uuid);
1346 strcpy(&payload->parkee_uuid[payload->app_data_offset], app_data);
1349 return post_it(bridge_channel, BRIDGE_CHANNEL_ACTION_PARK, payload, len_payload);
1352 int ast_bridge_channel_write_park(struct ast_bridge_channel *bridge_channel, const char *parkee_uuid, const char *parker_uuid, const char *app_data)
1354 return payload_helper_park(bridge_channel_write_action_data,
1355 bridge_channel, parkee_uuid, parker_uuid, app_data);
1360 * \brief Handle bridge channel interval expiration.
1363 * \param bridge_channel Channel to run expired intervals on.
1367 static void bridge_channel_handle_interval(struct ast_bridge_channel *bridge_channel)
1369 struct ast_heap *interval_hooks;
1370 struct ast_bridge_hook_timer *hook;
1371 struct timeval start;
1372 int chan_suspended = 0;
1374 interval_hooks = bridge_channel->features->interval_hooks;
1375 ast_heap_wrlock(interval_hooks);
1376 start = ast_tvnow();
1377 while ((hook = ast_heap_peek(interval_hooks, 1))) {
1379 unsigned int execution_time;
1381 if (ast_tvdiff_ms(hook->timer.trip_time, start) > 0) {
1382 ast_debug(1, "Hook %p on %p(%s) wants to happen in the future, stopping our traversal\n",
1383 hook, bridge_channel, ast_channel_name(bridge_channel->chan));
1387 ast_heap_unlock(interval_hooks);
1390 && ast_test_flag(&hook->timer, AST_BRIDGE_HOOK_TIMER_OPTION_MEDIA)) {
1392 bridge_channel_suspend(bridge_channel);
1393 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1396 ast_debug(1, "Executing hook %p on %p(%s)\n",
1397 hook, bridge_channel, ast_channel_name(bridge_channel->chan));
1398 interval = hook->generic.callback(bridge_channel, hook->generic.hook_pvt);
1400 ast_heap_wrlock(interval_hooks);
1401 if (ast_heap_peek(interval_hooks, hook->timer.heap_index) != hook
1402 || !ast_heap_remove(interval_hooks, hook)) {
1403 /* Interval hook is already removed from the bridge_channel. */
1410 ast_debug(1, "Removed interval hook %p from %p(%s)\n",
1411 hook, bridge_channel, ast_channel_name(bridge_channel->chan));
1416 /* Set new interval for the hook. */
1417 hook->timer.interval = interval;
1420 ast_debug(1, "Updating interval hook %p with interval %u on %p(%s)\n",
1421 hook, hook->timer.interval, bridge_channel,
1422 ast_channel_name(bridge_channel->chan));
1424 /* resetting start */
1425 start = ast_tvnow();
1428 * Resetup the interval hook for the next interval. We may need
1429 * to skip over any missed intervals because the hook was
1430 * delayed or took too long.
1432 execution_time = ast_tvdiff_ms(start, hook->timer.trip_time);
1433 while (hook->timer.interval < execution_time) {
1434 execution_time -= hook->timer.interval;
1436 hook->timer.trip_time = ast_tvadd(start, ast_samp2tv(hook->timer.interval - execution_time, 1000));
1437 hook->timer.seqno = ast_atomic_fetchadd_int((int *) &bridge_channel->features->interval_sequence, +1);
1439 if (ast_heap_push(interval_hooks, hook)) {
1440 /* Could not push the hook back onto the heap. */
1444 ast_heap_unlock(interval_hooks);
1446 if (chan_suspended) {
1447 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1448 bridge_channel_unsuspend(bridge_channel);
1454 * \brief Write a DTMF stream out to a channel
1456 static int bridge_channel_write_dtmf_stream(struct ast_bridge_channel *bridge_channel, const char *dtmf)
1458 return bridge_channel_write_action_data(bridge_channel,
1459 BRIDGE_CHANNEL_ACTION_DTMF_STREAM, dtmf, strlen(dtmf) + 1);
1464 * \brief Indicate to the testsuite a feature was successfully detected.
1466 * Currently, this function only will relay built-in features to the testsuite,
1467 * but it could be modified to detect applicationmap items should the need arise.
1469 * \param chan The channel that activated the feature
1470 * \param dtmf The DTMF sequence entered to activate the feature
1472 static void testsuite_notify_feature_success(struct ast_channel *chan, const char *dtmf)
1474 #ifdef TEST_FRAMEWORK
1475 char *feature = "unknown";
1476 struct ast_featuremap_config *featuremap = ast_get_chan_featuremap_config(chan);
1477 struct ast_features_xfer_config *xfer = ast_get_chan_features_xfer_config(chan);
1480 if (!strcmp(dtmf, featuremap->blindxfer)) {
1481 feature = "blindxfer";
1482 } else if (!strcmp(dtmf, featuremap->atxfer)) {
1484 } else if (!strcmp(dtmf, featuremap->disconnect)) {
1485 feature = "disconnect";
1486 } else if (!strcmp(dtmf, featuremap->automon)) {
1487 feature = "automon";
1488 } else if (!strcmp(dtmf, featuremap->automixmon)) {
1489 feature = "automixmon";
1490 } else if (!strcmp(dtmf, featuremap->parkcall)) {
1491 feature = "parkcall";
1495 if (!strcmp(dtmf, xfer->atxferthreeway)) {
1496 feature = "atxferthreeway";
1500 ao2_cleanup(featuremap);
1503 ast_test_suite_event_notify("FEATURE_DETECTION",
1504 "Result: success\r\n"
1505 "Feature: %s", feature);
1506 #endif /* TEST_FRAMEWORK */
1511 * \brief Internal function that executes a feature on a bridge channel
1512 * \note Neither the bridge nor the bridge_channel locks should be held when entering
1515 static void bridge_channel_feature(struct ast_bridge_channel *bridge_channel, const char *starting_dtmf)
1517 struct ast_bridge_features *features = bridge_channel->features;
1518 struct ast_bridge_hook_dtmf *hook = NULL;
1519 char dtmf[MAXIMUM_DTMF_FEATURE_STRING];
1521 unsigned int digit_timeout;
1522 RAII_VAR(struct ast_features_general_config *, gen_cfg, NULL, ao2_cleanup);
1524 ast_channel_lock(bridge_channel->chan);
1525 gen_cfg = ast_get_chan_features_general_config(bridge_channel->chan);
1527 ast_log(LOG_ERROR, "Unable to retrieve features configuration.\n");
1528 ast_channel_unlock(bridge_channel->chan);
1531 digit_timeout = gen_cfg->featuredigittimeout;
1532 ast_channel_unlock(bridge_channel->chan);
1534 if (ast_strlen_zero(starting_dtmf)) {
1538 ast_copy_string(dtmf, starting_dtmf, sizeof(dtmf));
1539 dtmf_len = strlen(dtmf);
1543 * Check if any feature DTMF hooks match or could match and
1544 * try to collect more DTMF digits.
1550 ast_debug(1, "DTMF feature string on %p(%s) is now '%s'\n",
1551 bridge_channel, ast_channel_name(bridge_channel->chan), dtmf);
1553 /* See if a DTMF feature hook matches or can match */
1554 hook = ao2_find(features->dtmf_hooks, dtmf, OBJ_PARTIAL_KEY);
1556 ast_debug(1, "No DTMF feature hooks on %p(%s) match '%s'\n",
1557 bridge_channel, ast_channel_name(bridge_channel->chan), dtmf);
1560 if (strlen(hook->dtmf.code) == dtmf_len) {
1561 ast_debug(1, "DTMF feature hook %p matched DTMF string '%s' on %p(%s)\n",
1562 hook, dtmf, bridge_channel, ast_channel_name(bridge_channel->chan));
1568 if (ARRAY_LEN(dtmf) - 1 <= dtmf_len) {
1569 /* We have reached the maximum length of a DTMF feature string. */
1574 res = ast_waitfordigit(bridge_channel->chan, digit_timeout);
1576 ast_debug(1, "DTMF feature string collection on %p(%s) timed out\n",
1577 bridge_channel, ast_channel_name(bridge_channel->chan));
1581 ast_debug(1, "DTMF feature string collection failed on %p(%s) for some reason\n",
1582 bridge_channel, ast_channel_name(bridge_channel->chan));
1586 /* Add the new DTMF into the DTMF string so we can do our matching */
1587 dtmf[dtmf_len] = res;
1588 dtmf[++dtmf_len] = '\0';
1594 /* Execute the matched hook on this channel. */
1595 remove_me = hook->generic.callback(bridge_channel, hook->generic.hook_pvt);
1597 ast_debug(1, "DTMF hook %p is being removed from %p(%s)\n",
1598 hook, bridge_channel, ast_channel_name(bridge_channel->chan));
1599 ao2_unlink(features->dtmf_hooks, hook);
1601 testsuite_notify_feature_success(bridge_channel->chan, hook->dtmf.code);
1605 * If we are handing the channel off to an external hook for
1606 * ownership, we are not guaranteed what kind of state it will
1607 * come back in. If the channel hungup, we need to detect that
1608 * here if the hook did not already change the state.
1610 if (bridge_channel->chan && ast_check_hangup_locked(bridge_channel->chan)) {
1611 ast_bridge_channel_kick(bridge_channel, 0);
1614 if (features->dtmf_passthrough) {
1615 /* Stream any collected DTMF to the other channels. */
1616 bridge_channel_write_dtmf_stream(bridge_channel, dtmf);
1618 ast_test_suite_event_notify("FEATURE_DETECTION", "Result: fail");
1624 * \brief Indicate that a bridge_channel is talking
1626 static void bridge_channel_talking(struct ast_bridge_channel *bridge_channel, int talking)
1628 struct ast_bridge_features *features = bridge_channel->features;
1629 struct ast_bridge_hook *hook;
1630 struct ao2_iterator iter;
1632 /* Run any talk detection hooks. */
1633 iter = ao2_iterator_init(features->other_hooks, 0);
1634 for (; (hook = ao2_iterator_next(&iter)); ao2_ref(hook, -1)) {
1636 ast_bridge_talking_indicate_callback talk_cb;
1638 if (hook->type != AST_BRIDGE_HOOK_TYPE_TALK) {
1641 talk_cb = (ast_bridge_talking_indicate_callback) hook->callback;
1642 remove_me = talk_cb(bridge_channel, hook->hook_pvt, talking);
1644 ast_debug(1, "Talk detection hook %p is being removed from %p(%s)\n",
1645 hook, bridge_channel, ast_channel_name(bridge_channel->chan));
1646 ao2_unlink(features->other_hooks, hook);
1649 ao2_iterator_destroy(&iter);
1652 /*! \brief Internal function that plays back DTMF on a bridge channel */
1653 static void bridge_channel_dtmf_stream(struct ast_bridge_channel *bridge_channel, const char *dtmf)
1655 ast_debug(1, "Playing DTMF stream '%s' out to %p(%s)\n",
1656 dtmf, bridge_channel, ast_channel_name(bridge_channel->chan));
1657 ast_dtmf_stream(bridge_channel->chan, NULL, dtmf, 0, 0);
1660 /*! \brief Data specifying where a blind transfer is going to */
1661 struct blind_transfer_data {
1662 char exten[AST_MAX_EXTENSION];
1663 char context[AST_MAX_CONTEXT];
1668 * \brief Execute after bridge actions on a channel when it leaves a bridge
1670 static void after_bridge_move_channel(struct ast_channel *chan_bridged, void *data)
1672 RAII_VAR(struct ast_channel *, chan_target, data, ao2_cleanup);
1673 struct ast_party_connected_line connected_target;
1674 unsigned char connected_line_data[1024];
1677 ast_party_connected_line_init(&connected_target);
1679 ast_channel_lock(chan_target);
1680 ast_party_connected_line_copy(&connected_target, ast_channel_connected(chan_target));
1681 ast_channel_unlock(chan_target);
1682 ast_party_id_reset(&connected_target.priv);
1684 if (ast_channel_move(chan_target, chan_bridged)) {
1685 ast_softhangup(chan_target, AST_SOFTHANGUP_DEV);
1686 ast_party_connected_line_free(&connected_target);
1690 if ((payload_size = ast_connected_line_build_data(connected_line_data,
1691 sizeof(connected_line_data), &connected_target, NULL)) != -1) {
1692 struct ast_control_read_action_payload *frame_payload;
1695 frame_size = payload_size + sizeof(*frame_payload);
1696 frame_payload = ast_alloca(frame_size);
1697 frame_payload->action = AST_FRAME_READ_ACTION_CONNECTED_LINE_MACRO;
1698 frame_payload->payload_size = payload_size;
1699 memcpy(frame_payload->payload, connected_line_data, payload_size);
1700 ast_queue_control_data(chan_target, AST_CONTROL_READ_ACTION, frame_payload, frame_size);
1703 ast_party_connected_line_free(&connected_target);
1708 * \brief Execute logic to cleanup when after bridge fails
1710 static void after_bridge_move_channel_fail(enum ast_bridge_after_cb_reason reason, void *data)
1712 RAII_VAR(struct ast_channel *, chan_target, data, ao2_cleanup);
1714 ast_log(LOG_WARNING, "Unable to complete transfer: %s\n",
1715 ast_bridge_after_cb_reason_string(reason));
1716 ast_softhangup(chan_target, AST_SOFTHANGUP_DEV);
1721 * \brief Perform a blind transfer on a channel in a bridge
1723 static void bridge_channel_blind_transfer(struct ast_bridge_channel *bridge_channel,
1724 struct blind_transfer_data *blind_data)
1726 ast_async_goto(bridge_channel->chan, blind_data->context, blind_data->exten, 1);
1727 ast_bridge_channel_kick(bridge_channel, AST_CAUSE_NORMAL_CLEARING);
1732 * \brief Perform an attended transfer on a channel in a bridge
1734 static void bridge_channel_attended_transfer(struct ast_bridge_channel *bridge_channel,
1735 const char *target_chan_name)
1737 RAII_VAR(struct ast_channel *, chan_target, NULL, ao2_cleanup);
1738 RAII_VAR(struct ast_channel *, chan_bridged, NULL, ao2_cleanup);
1740 chan_target = ast_channel_get_by_name(target_chan_name);
1742 /* Dang, it disappeared somehow */
1743 ast_bridge_channel_kick(bridge_channel, AST_CAUSE_NORMAL_CLEARING);
1747 ast_bridge_channel_lock(bridge_channel);
1748 chan_bridged = bridge_channel->chan;
1749 ast_assert(chan_bridged != NULL);
1750 ao2_ref(chan_bridged, +1);
1751 ast_bridge_channel_unlock(bridge_channel);
1753 if (ast_bridge_set_after_callback(chan_bridged, after_bridge_move_channel,
1754 after_bridge_move_channel_fail, ast_channel_ref(chan_target))) {
1755 ast_softhangup(chan_target, AST_SOFTHANGUP_DEV);
1757 /* Release the ref we tried to pass to ast_bridge_set_after_callback(). */
1758 ast_channel_unref(chan_target);
1760 ast_bridge_channel_kick(bridge_channel, AST_CAUSE_NORMAL_CLEARING);
1765 * \brief Handle bridge channel bridge action frame.
1768 * \param bridge_channel Channel to execute the action on.
1769 * \param action What to do.
1770 * \param data data from the action.
1774 static void bridge_channel_handle_action(struct ast_bridge_channel *bridge_channel,
1775 enum bridge_channel_action_type action, void *data)
1778 case BRIDGE_CHANNEL_ACTION_DTMF_STREAM:
1779 bridge_channel_suspend(bridge_channel);
1780 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1781 bridge_channel_dtmf_stream(bridge_channel, data);
1782 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1783 bridge_channel_unsuspend(bridge_channel);
1785 case BRIDGE_CHANNEL_ACTION_TALKING_START:
1786 case BRIDGE_CHANNEL_ACTION_TALKING_STOP:
1787 bridge_channel_talking(bridge_channel,
1788 action == BRIDGE_CHANNEL_ACTION_TALKING_START);
1790 case BRIDGE_CHANNEL_ACTION_PLAY_FILE:
1791 bridge_channel_suspend(bridge_channel);
1792 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1793 bridge_channel_playfile(bridge_channel, data);
1794 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1795 bridge_channel_unsuspend(bridge_channel);
1797 case BRIDGE_CHANNEL_ACTION_RUN_APP:
1798 bridge_channel_suspend(bridge_channel);
1799 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1800 bridge_channel_run_app(bridge_channel, data);
1801 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1802 bridge_channel_unsuspend(bridge_channel);
1804 case BRIDGE_CHANNEL_ACTION_CALLBACK:
1805 bridge_channel_do_callback(bridge_channel, data);
1807 case BRIDGE_CHANNEL_ACTION_PARK:
1808 bridge_channel_suspend(bridge_channel);
1809 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1810 bridge_channel_park(bridge_channel, data);
1811 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1812 bridge_channel_unsuspend(bridge_channel);
1814 case BRIDGE_CHANNEL_ACTION_BLIND_TRANSFER:
1815 bridge_channel_blind_transfer(bridge_channel, data);
1817 case BRIDGE_CHANNEL_ACTION_ATTENDED_TRANSFER:
1818 bridge_channel_attended_transfer(bridge_channel, data);
1827 * \brief Check if a bridge should dissolve and do it.
1830 * \param bridge_channel Channel causing the check.
1832 * \note On entry, bridge_channel->bridge is already locked.
1836 static void bridge_channel_dissolve_check(struct ast_bridge_channel *bridge_channel)
1838 struct ast_bridge *bridge = bridge_channel->bridge;
1840 if (bridge->dissolved) {
1844 if (!bridge->num_channels
1845 && ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_DISSOLVE_EMPTY)) {
1846 /* Last channel leaving the bridge turns off the lights. */
1847 bridge_dissolve(bridge, ast_channel_hangupcause(bridge_channel->chan));
1851 switch (bridge_channel->state) {
1852 case BRIDGE_CHANNEL_STATE_END:
1853 /* Do we need to dissolve the bridge because this channel hung up? */
1854 if (ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_DISSOLVE_HANGUP)
1855 || (bridge_channel->features->usable
1856 && ast_test_flag(&bridge_channel->features->feature_flags,
1857 AST_BRIDGE_CHANNEL_FLAG_DISSOLVE_HANGUP))) {
1858 bridge_dissolve(bridge, ast_channel_hangupcause(bridge_channel->chan));
1866 if (bridge->num_lonely && bridge->num_lonely == bridge->num_channels) {
1868 * This will start a chain reaction where each channel leaving
1869 * enters this function and causes the next to leave as long as
1870 * there aren't non-lonely channels in the bridge.
1872 ast_bridge_channel_leave_bridge(AST_LIST_FIRST(&bridge->channels),
1873 BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE,
1874 ast_channel_hangupcause(bridge_channel->chan));
1878 void bridge_channel_internal_pull(struct ast_bridge_channel *bridge_channel)
1880 struct ast_bridge *bridge = bridge_channel->bridge;
1882 if (!bridge_channel->in_bridge) {
1885 bridge_channel->in_bridge = 0;
1887 ast_debug(1, "Bridge %s: pulling %p(%s)\n",
1888 bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan));
1890 ast_verb(3, "Channel %s left '%s' %s-bridge <%s>\n",
1891 ast_channel_name(bridge_channel->chan),
1892 bridge->technology->name,
1893 bridge->v_table->name,
1896 if (!bridge_channel->just_joined) {
1897 /* Tell the bridge technology we are leaving so they tear us down */
1898 ast_debug(1, "Bridge %s: %p(%s) is leaving %s technology\n",
1899 bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan),
1900 bridge->technology->name);
1901 if (bridge->technology->leave) {
1902 bridge->technology->leave(bridge, bridge_channel);
1906 /* Remove channel from the bridge */
1907 if (!bridge_channel->suspended) {
1908 --bridge->num_active;
1910 if (ast_test_flag(&bridge_channel->features->feature_flags, AST_BRIDGE_CHANNEL_FLAG_LONELY)) {
1911 --bridge->num_lonely;
1913 --bridge->num_channels;
1914 AST_LIST_REMOVE(&bridge->channels, bridge_channel, entry);
1916 bridge_channel_dissolve_check(bridge_channel);
1917 bridge->v_table->pull(bridge, bridge_channel);
1919 ast_bridge_channel_clear_roles(bridge_channel);
1921 /* If we are not going to be hung up after leaving a bridge, and we were an
1922 * outgoing channel, clear the outgoing flag.
1924 if (ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_OUTGOING)
1925 && (ast_channel_is_leaving_bridge(bridge_channel->chan)
1926 || bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT)) {
1927 ast_debug(2, "Channel %s will survive this bridge; clearing outgoing (dialed) flag\n", ast_channel_name(bridge_channel->chan));
1928 ast_clear_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_OUTGOING);
1931 bridge->reconfigured = 1;
1932 ast_bridge_publish_leave(bridge, bridge_channel->chan);
1935 int bridge_channel_internal_push(struct ast_bridge_channel *bridge_channel)
1937 struct ast_bridge *bridge = bridge_channel->bridge;
1938 struct ast_bridge_channel *swap;
1940 ast_assert(!bridge_channel->in_bridge);
1942 swap = bridge_find_channel(bridge, bridge_channel->swap);
1943 bridge_channel->swap = NULL;
1946 ast_debug(1, "Bridge %s: pushing %p(%s) by swapping with %p(%s)\n",
1947 bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan),
1948 swap, ast_channel_name(swap->chan));
1950 ast_debug(1, "Bridge %s: pushing %p(%s)\n",
1951 bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan));
1954 /* Add channel to the bridge */
1955 if (bridge->dissolved
1956 || bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT
1957 || (swap && swap->state != BRIDGE_CHANNEL_STATE_WAIT)
1958 || bridge->v_table->push(bridge, bridge_channel, swap)
1959 || ast_bridge_channel_establish_roles(bridge_channel)) {
1960 ast_debug(1, "Bridge %s: pushing %p(%s) into bridge failed\n",
1961 bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan));
1964 bridge_channel->in_bridge = 1;
1965 bridge_channel->just_joined = 1;
1966 AST_LIST_INSERT_TAIL(&bridge->channels, bridge_channel, entry);
1967 ++bridge->num_channels;
1968 if (ast_test_flag(&bridge_channel->features->feature_flags, AST_BRIDGE_CHANNEL_FLAG_LONELY)) {
1969 ++bridge->num_lonely;
1971 if (!bridge_channel->suspended) {
1972 ++bridge->num_active;
1975 ast_verb(3, "Channel %s %s%s%s '%s' %s-bridge <%s>\n",
1976 ast_channel_name(bridge_channel->chan),
1977 swap ? "swapped with " : "joined",
1978 swap ? ast_channel_name(swap->chan) : "",
1979 swap ? " into" : "",
1980 bridge->technology->name,
1981 bridge->v_table->name,
1984 ast_bridge_publish_enter(bridge, bridge_channel->chan, swap ? swap->chan : NULL);
1986 ast_bridge_channel_leave_bridge(swap, BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, 0);
1987 bridge_channel_internal_pull(swap);
1990 /* Clear any BLINDTRANSFER and ATTENDEDTRANSFER since the transfer has completed. */
1991 pbx_builtin_setvar_helper(bridge_channel->chan, "BLINDTRANSFER", NULL);
1992 pbx_builtin_setvar_helper(bridge_channel->chan, "ATTENDEDTRANSFER", NULL);
1994 /* Wake up the bridge channel thread to reevaluate any interval timers. */
1995 ast_queue_frame(bridge_channel->chan, &ast_null_frame);
1997 bridge->reconfigured = 1;
2003 * \brief Handle bridge channel control frame action.
2006 * \param bridge_channel Channel to execute the control frame action on.
2007 * \param fr Control frame to handle.
2011 static void bridge_channel_handle_control(struct ast_bridge_channel *bridge_channel, struct ast_frame *fr)
2013 struct ast_channel *chan;
2014 struct ast_option_header *aoh;
2017 chan = bridge_channel->chan;
2018 switch (fr->subclass.integer) {
2019 case AST_CONTROL_REDIRECTING:
2020 is_caller = !ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING);
2021 if (ast_channel_redirecting_sub(NULL, chan, fr, 1) &&
2022 ast_channel_redirecting_macro(NULL, chan, fr, is_caller, 1)) {
2023 ast_indicate_data(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
2026 case AST_CONTROL_CONNECTED_LINE:
2027 is_caller = !ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING);
2028 if (ast_channel_connected_line_sub(NULL, chan, fr, 1) &&
2029 ast_channel_connected_line_macro(NULL, chan, fr, is_caller, 1)) {
2030 ast_indicate_data(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
2033 case AST_CONTROL_OPTION:
2035 * Forward option Requests, but only ones we know are safe These
2036 * are ONLY sent by chan_iax2 and I'm not convinced that they
2037 * are useful. I haven't deleted them entirely because I just am
2038 * not sure of the ramifications of removing them.
2041 if (aoh && aoh->flag == AST_OPTION_FLAG_REQUEST) {
2042 switch (ntohs(aoh->option)) {
2043 case AST_OPTION_TONE_VERIFY:
2044 case AST_OPTION_TDD:
2045 case AST_OPTION_RELAXDTMF:
2046 case AST_OPTION_AUDIO_MODE:
2047 case AST_OPTION_DIGIT_DETECT:
2048 case AST_OPTION_FAX_DETECT:
2049 ast_channel_setoption(chan, ntohs(aoh->option), aoh->data,
2050 fr->datalen - sizeof(*aoh), 0);
2057 case AST_CONTROL_ANSWER:
2058 if (ast_channel_state(chan) != AST_STATE_UP) {
2061 ast_indicate(chan, -1);
2065 ast_indicate_data(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
2072 * \brief Handle bridge channel write frame to channel.
2075 * \param bridge_channel Channel to write outgoing frame.
2079 static void bridge_channel_handle_write(struct ast_bridge_channel *bridge_channel)
2081 struct ast_frame *fr;
2083 struct sync_payload *sync_payload;
2085 ast_bridge_channel_lock(bridge_channel);
2086 if (read(bridge_channel->alert_pipe[0], &nudge, sizeof(nudge)) < 0) {
2087 if (errno != EINTR && errno != EAGAIN) {
2088 ast_log(LOG_WARNING, "read() failed for alert pipe on %p(%s): %s\n",
2089 bridge_channel, ast_channel_name(bridge_channel->chan), strerror(errno));
2092 fr = AST_LIST_REMOVE_HEAD(&bridge_channel->wr_queue, frame_list);
2093 ast_bridge_channel_unlock(bridge_channel);
2097 switch (fr->frametype) {
2098 case AST_FRAME_BRIDGE_ACTION:
2099 bridge_channel_handle_action(bridge_channel, fr->subclass.integer, fr->data.ptr);
2101 case AST_FRAME_BRIDGE_ACTION_SYNC:
2102 sync_payload = fr->data.ptr;
2103 bridge_channel_handle_action(bridge_channel, fr->subclass.integer, sync_payload->data);
2105 case AST_FRAME_CONTROL:
2106 bridge_channel_handle_control(bridge_channel, fr);
2108 case AST_FRAME_NULL:
2111 /* Write the frame to the channel. */
2112 bridge_channel->activity = BRIDGE_CHANNEL_THREAD_SIMPLE;
2113 ast_write(bridge_channel->chan, fr);
2116 bridge_frame_free(fr);
2119 /*! \brief Internal function to handle DTMF from a channel */
2120 static struct ast_frame *bridge_handle_dtmf(struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
2122 struct ast_bridge_features *features = bridge_channel->features;
2123 struct ast_bridge_hook_dtmf *hook;
2126 /* See if this DTMF matches the beginning of any feature hooks. */
2127 dtmf[0] = frame->subclass.integer;
2129 hook = ao2_find(features->dtmf_hooks, dtmf, OBJ_PARTIAL_KEY);
2131 enum ast_frame_type frametype = frame->frametype;
2133 bridge_frame_free(frame);
2138 /* Collect any more needed DTMF to execute a hook. */
2139 bridge_channel_suspend(bridge_channel);
2140 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
2141 switch (frametype) {
2142 case AST_FRAME_DTMF_BEGIN:
2143 bridge_channel_feature(bridge_channel, NULL);
2145 case AST_FRAME_DTMF_END:
2146 bridge_channel_feature(bridge_channel, dtmf);
2149 /* Unexpected frame type. */
2153 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
2154 bridge_channel_unsuspend(bridge_channel);
2155 #ifdef TEST_FRAMEWORK
2156 } else if (frame->frametype == AST_FRAME_DTMF_END) {
2157 /* Only transmit this event on DTMF end or else every DTMF
2158 * press will result in the event being broadcast twice
2160 ast_test_suite_event_notify("FEATURE_DETECTION", "Result: fail");
2170 * \brief Feed notification that a frame is waiting on a channel into the bridging core
2172 * \param bridge_channel Bridge channel the notification was received on
2174 static void bridge_handle_trip(struct ast_bridge_channel *bridge_channel)
2176 struct ast_frame *frame;
2178 if (bridge_channel->features->mute) {
2179 frame = ast_read_noaudio(bridge_channel->chan);
2181 frame = ast_read(bridge_channel->chan);
2185 ast_bridge_channel_kick(bridge_channel, 0);
2188 switch (frame->frametype) {
2189 case AST_FRAME_CONTROL:
2190 switch (frame->subclass.integer) {
2191 case AST_CONTROL_HANGUP:
2192 ast_bridge_channel_kick(bridge_channel, 0);
2193 bridge_frame_free(frame);
2199 case AST_FRAME_DTMF_BEGIN:
2200 case AST_FRAME_DTMF_END:
2201 frame = bridge_handle_dtmf(bridge_channel, frame);
2205 if (!bridge_channel->features->dtmf_passthrough) {
2206 bridge_frame_free(frame);
2214 /* Simply write the frame out to the bridge technology. */
2215 bridge_channel_write_frame(bridge_channel, frame);
2216 bridge_frame_free(frame);
2221 * \brief Determine how long till the next timer interval.
2224 * \param bridge_channel Channel to determine how long can wait.
2226 * \retval ms Number of milliseconds to wait.
2227 * \retval -1 to wait forever.
2229 static int bridge_channel_next_interval(struct ast_bridge_channel *bridge_channel)
2231 struct ast_heap *interval_hooks = bridge_channel->features->interval_hooks;
2232 struct ast_bridge_hook_timer *hook;
2235 ast_heap_wrlock(interval_hooks);
2236 hook = ast_heap_peek(interval_hooks, 1);
2238 ms = ast_tvdiff_ms(hook->timer.trip_time, ast_tvnow());
2240 /* Expire immediately. An interval hook is ready to run. */
2244 /* No hook so wait forever. */
2247 ast_heap_unlock(interval_hooks);
2254 * \brief Wait for something to happen on the bridge channel and handle it.
2257 * \param bridge_channel Channel to wait.
2259 * \note Each channel does writing/reading in their own thread.
2263 static void bridge_channel_wait(struct ast_bridge_channel *bridge_channel)
2267 struct ast_channel *chan;
2269 /* Wait for data to either come from the channel or us to be signaled */
2270 ast_bridge_channel_lock(bridge_channel);
2271 if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
2272 } else if (bridge_channel->suspended) {
2273 /* XXX ASTERISK-21271 the external party use of suspended will go away as will these references because this is the bridge channel thread */
2274 ast_debug(1, "Bridge %s: %p(%s) is going into a signal wait\n",
2275 bridge_channel->bridge->uniqueid, bridge_channel,
2276 ast_channel_name(bridge_channel->chan));
2277 ast_cond_wait(&bridge_channel->cond, ao2_object_get_lockaddr(bridge_channel));
2279 ast_bridge_channel_unlock(bridge_channel);
2281 ms = bridge_channel_next_interval(bridge_channel);
2282 chan = ast_waitfor_nandfds(&bridge_channel->chan, 1,
2283 &bridge_channel->alert_pipe[0], 1, NULL, &outfd, &ms);
2284 if (ast_channel_softhangup_internal_flag(bridge_channel->chan) & AST_SOFTHANGUP_UNBRIDGE) {
2285 ast_channel_clear_softhangup(bridge_channel->chan, AST_SOFTHANGUP_UNBRIDGE);
2286 ast_bridge_channel_lock_bridge(bridge_channel);
2287 bridge_channel->bridge->reconfigured = 1;
2288 bridge_reconfigured(bridge_channel->bridge, 0);
2289 ast_bridge_unlock(bridge_channel->bridge);
2291 ast_bridge_channel_lock(bridge_channel);
2292 bridge_channel->activity = BRIDGE_CHANNEL_THREAD_FRAME;
2293 ast_bridge_channel_unlock(bridge_channel);
2294 if (!bridge_channel->suspended
2295 && bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
2297 bridge_handle_trip(bridge_channel);
2298 } else if (-1 < outfd) {
2299 bridge_channel_handle_write(bridge_channel);
2300 } else if (ms == 0) {
2301 /* An interval expired. */
2302 bridge_channel_handle_interval(bridge_channel);
2305 bridge_channel->activity = BRIDGE_CHANNEL_THREAD_IDLE;
2308 ast_bridge_channel_unlock(bridge_channel);
2313 * \brief Handle bridge channel join/leave event.
2316 * \param bridge_channel Which channel is involved.
2317 * \param type Specified join/leave event.
2321 static void bridge_channel_event_join_leave(struct ast_bridge_channel *bridge_channel, enum ast_bridge_hook_type type)
2323 struct ast_bridge_features *features = bridge_channel->features;
2324 struct ast_bridge_hook *hook;
2325 struct ao2_iterator iter;
2327 /* Run the specified hooks. */
2328 iter = ao2_iterator_init(features->other_hooks, 0);
2329 for (; (hook = ao2_iterator_next(&iter)); ao2_ref(hook, -1)) {
2330 if (hook->type == type) {
2335 /* Found the first specified hook to run. */
2336 bridge_channel_suspend(bridge_channel);
2337 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
2339 if (hook->type == type) {
2340 hook->callback(bridge_channel, hook->hook_pvt);
2341 ao2_unlink(features->other_hooks, hook);
2344 } while ((hook = ao2_iterator_next(&iter)));
2345 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
2346 bridge_channel_unsuspend(bridge_channel);
2348 ao2_iterator_destroy(&iter);
2351 /*! \brief Join a channel to a bridge and handle anything the bridge may want us to do */
2352 int bridge_channel_internal_join(struct ast_bridge_channel *bridge_channel)
2355 struct ast_bridge_features *channel_features;
2357 bridge_channel->read_format = ao2_bump(ast_channel_readformat(bridge_channel->chan));
2358 bridge_channel->write_format = ao2_bump(ast_channel_writeformat(bridge_channel->chan));
2360 ast_debug(1, "Bridge %s: %p(%s) is joining\n",
2361 bridge_channel->bridge->uniqueid,
2362 bridge_channel, ast_channel_name(bridge_channel->chan));
2365 * Directly locking the bridge is safe here because nobody else
2366 * knows about this bridge_channel yet.
2368 ast_bridge_lock(bridge_channel->bridge);
2370 ast_channel_lock(bridge_channel->chan);
2371 /* Make sure we're still good to be put into a bridge */
2372 if (ast_channel_internal_bridge(bridge_channel->chan)
2373 || ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_ZOMBIE)) {
2374 ast_channel_unlock(bridge_channel->chan);
2375 ast_bridge_unlock(bridge_channel->bridge);
2376 ast_debug(1, "Bridge %s: %p(%s) failed to join Bridge\n",
2377 bridge_channel->bridge->uniqueid,
2379 ast_channel_name(bridge_channel->chan));
2382 ast_channel_internal_bridge_set(bridge_channel->chan, bridge_channel->bridge);
2384 /* Attach features requested by the channel */
2385 channel_features = ast_channel_feature_hooks_get(bridge_channel->chan);
2386 if (channel_features) {
2387 ast_bridge_features_merge(bridge_channel->features, channel_features);
2389 ast_channel_unlock(bridge_channel->chan);
2391 /* Add the jitterbuffer if the channel requires it */
2392 ast_jb_enable_for_channel(bridge_channel->chan);
2394 if (!bridge_channel->bridge->callid) {
2395 bridge_channel->bridge->callid = ast_read_threadstorage_callid();
2398 if (bridge_channel_internal_push(bridge_channel)) {
2399 int cause = bridge_channel->bridge->cause;
2401 ast_bridge_unlock(bridge_channel->bridge);
2402 ast_bridge_channel_kick(bridge_channel, cause);
2403 ast_bridge_channel_lock_bridge(bridge_channel);
2404 ast_bridge_features_remove(bridge_channel->features,
2405 AST_BRIDGE_HOOK_REMOVE_ON_PULL);
2406 bridge_channel_dissolve_check(bridge_channel);
2409 bridge_reconfigured(bridge_channel->bridge, !bridge_channel->inhibit_colp);
2411 if (bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
2413 * Indicate a source change since this channel is entering the
2414 * bridge system only if the bridge technology is not MULTIMIX
2415 * capable. The MULTIMIX technology has already done it.
2417 if (!(bridge_channel->bridge->technology->capabilities
2418 & AST_BRIDGE_CAPABILITY_MULTIMIX)) {
2419 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCCHANGE);
2422 ast_bridge_unlock(bridge_channel->bridge);
2423 bridge_channel_event_join_leave(bridge_channel, AST_BRIDGE_HOOK_TYPE_JOIN);
2424 while (bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
2425 /* Wait for something to do. */
2426 bridge_channel_wait(bridge_channel);
2428 bridge_channel_event_join_leave(bridge_channel, AST_BRIDGE_HOOK_TYPE_LEAVE);
2429 ast_bridge_channel_lock_bridge(bridge_channel);
2432 bridge_channel_internal_pull(bridge_channel);
2433 bridge_channel_settle_owed_events(bridge_channel->bridge, bridge_channel);
2434 bridge_reconfigured(bridge_channel->bridge, 1);
2436 ast_bridge_unlock(bridge_channel->bridge);
2438 /* Complete any active hold before exiting the bridge. */
2439 if (ast_channel_hold_state(bridge_channel->chan) == AST_CONTROL_HOLD) {
2440 ast_debug(1, "Channel %s simulating UNHOLD for bridge end.\n",
2441 ast_channel_name(bridge_channel->chan));
2442 ast_indicate(bridge_channel->chan, AST_CONTROL_UNHOLD);
2445 /* Complete any partial DTMF digit before exiting the bridge. */
2446 if (ast_channel_sending_dtmf_digit(bridge_channel->chan)) {
2447 ast_channel_end_dtmf(bridge_channel->chan,
2448 ast_channel_sending_dtmf_digit(bridge_channel->chan),
2449 ast_channel_sending_dtmf_tv(bridge_channel->chan), "bridge end");
2452 /* Indicate a source change since this channel is leaving the bridge system. */
2453 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCCHANGE);
2456 * Wait for any dual redirect to complete.
2458 * Must be done while "still in the bridge" for ast_async_goto()
2461 while (ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_BRIDGE_DUAL_REDIRECT_WAIT)) {
2464 ast_channel_lock(bridge_channel->chan);
2465 ast_channel_internal_bridge_set(bridge_channel->chan, NULL);
2466 ast_channel_unlock(bridge_channel->chan);
2468 ast_bridge_channel_restore_formats(bridge_channel);
2473 int bridge_channel_internal_queue_blind_transfer(struct ast_channel *transferee,
2474 const char *exten, const char *context,
2475 transfer_channel_cb new_channel_cb, void *user_data)
2477 RAII_VAR(struct ast_bridge_channel *, transferee_bridge_channel, NULL, ao2_cleanup);
2478 struct blind_transfer_data blind_data;
2480 ast_channel_lock(transferee);
2481 transferee_bridge_channel = ast_channel_get_bridge_channel(transferee);
2482 ast_channel_unlock(transferee);
2484 if (!transferee_bridge_channel) {
2488 if (new_channel_cb) {
2489 new_channel_cb(transferee, user_data, AST_BRIDGE_TRANSFER_SINGLE_PARTY);
2492 ast_copy_string(blind_data.exten, exten, sizeof(blind_data.exten));
2493 ast_copy_string(blind_data.context, context, sizeof(blind_data.context));
2495 return bridge_channel_queue_action_data(transferee_bridge_channel,
2496 BRIDGE_CHANNEL_ACTION_BLIND_TRANSFER, &blind_data, sizeof(blind_data));
2499 int bridge_channel_internal_queue_attended_transfer(struct ast_channel *transferee,
2500 struct ast_channel *unbridged_chan)
2502 RAII_VAR(struct ast_bridge_channel *, transferee_bridge_channel, NULL, ao2_cleanup);
2503 char unbridged_chan_name[AST_CHANNEL_NAME];
2505 ast_channel_lock(transferee);
2506 transferee_bridge_channel = ast_channel_get_bridge_channel(transferee);
2507 ast_channel_unlock(transferee);
2509 if (!transferee_bridge_channel) {
2513 ast_copy_string(unbridged_chan_name, ast_channel_name(unbridged_chan),
2514 sizeof(unbridged_chan_name));
2516 return bridge_channel_queue_action_data(transferee_bridge_channel,
2517 BRIDGE_CHANNEL_ACTION_ATTENDED_TRANSFER, unbridged_chan_name,
2518 sizeof(unbridged_chan_name));
2521 int bridge_channel_internal_allows_optimization(struct ast_bridge_channel *bridge_channel)
2523 return bridge_channel->in_bridge
2524 && AST_LIST_EMPTY(&bridge_channel->wr_queue);
2529 * \brief Close a pipe.
2532 * \param my_pipe What to close.
2536 static void pipe_close(int *my_pipe)
2538 if (my_pipe[0] > -1) {
2542 if (my_pipe[1] > -1) {
2550 * \brief Initialize a pipe as non-blocking.
2553 * \param my_pipe What to initialize.
2555 * \retval 0 on success.
2556 * \retval -1 on error.
2558 static int pipe_init_nonblock(int *my_pipe)
2564 if (pipe(my_pipe)) {
2565 ast_log(LOG_WARNING, "Can't create pipe! Try increasing max file descriptors with ulimit -n\n");
2568 flags = fcntl(my_pipe[0], F_GETFL);
2569 if (fcntl(my_pipe[0], F_SETFL, flags | O_NONBLOCK) < 0) {
2570 ast_log(LOG_WARNING, "Unable to set read pipe nonblocking! (%d: %s)\n",
2571 errno, strerror(errno));
2574 flags = fcntl(my_pipe[1], F_GETFL);
2575 if (fcntl(my_pipe[1], F_SETFL, flags | O_NONBLOCK) < 0) {
2576 ast_log(LOG_WARNING, "Unable to set write pipe nonblocking! (%d: %s)\n",
2577 errno, strerror(errno));
2583 /* Destroy elements of the bridge channel structure and the bridge channel structure itself */
2584 static void bridge_channel_destroy(void *obj)
2586 struct ast_bridge_channel *bridge_channel = obj;
2587 struct ast_frame *fr;
2589 if (bridge_channel->callid) {
2590 bridge_channel->callid = ast_callid_unref(bridge_channel->callid);
2593 if (bridge_channel->bridge) {
2594 ao2_ref(bridge_channel->bridge, -1);
2595 bridge_channel->bridge = NULL;
2598 /* Flush any unhandled wr_queue frames. */
2599 while ((fr = AST_LIST_REMOVE_HEAD(&bridge_channel->wr_queue, frame_list))) {
2600 bridge_frame_free(fr);
2602 pipe_close(bridge_channel->alert_pipe);
2604 ast_cond_destroy(&bridge_channel->cond);
2606 ao2_cleanup(bridge_channel->write_format);
2607 ao2_cleanup(bridge_channel->read_format);
2610 struct ast_bridge_channel *bridge_channel_internal_alloc(struct ast_bridge *bridge)
2612 struct ast_bridge_channel *bridge_channel;
2614 bridge_channel = ao2_alloc(sizeof(struct ast_bridge_channel), bridge_channel_destroy);
2615 if (!bridge_channel) {
2618 ast_cond_init(&bridge_channel->cond, NULL);
2619 if (pipe_init_nonblock(bridge_channel->alert_pipe)) {
2620 ao2_ref(bridge_channel, -1);
2624 bridge_channel->bridge = bridge;
2625 ao2_ref(bridge_channel->bridge, +1);
2628 return bridge_channel;