Merge "res_srtp: Add support for libsrtp2"
[asterisk/asterisk.git] / main / bridge_channel.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2007 - 2009, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Bridging Channel API
22  *
23  * \author Joshua Colp <jcolp@digium.com>
24  * \author Richard Mudgett <rmudgett@digium.com>
25  * \author Matt Jordan <mjordan@digium.com>
26  *
27  */
28
29 /*** MODULEINFO
30         <support_level>core</support_level>
31  ***/
32
33 #include "asterisk.h"
34
35 #include <signal.h>
36
37 #include "asterisk/heap.h"
38 #include "asterisk/alertpipe.h"
39 #include "asterisk/astobj2.h"
40 #include "asterisk/stringfields.h"
41 #include "asterisk/app.h"
42 #include "asterisk/pbx.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/timing.h"
45 #include "asterisk/bridge.h"
46 #include "asterisk/bridge_channel.h"
47 #include "asterisk/bridge_after.h"
48 #include "asterisk/bridge_channel_internal.h"
49 #include "asterisk/bridge_internal.h"
50 #include "asterisk/stasis_bridges.h"
51 #include "asterisk/stasis_channels.h"
52 #include "asterisk/musiconhold.h"
53 #include "asterisk/features_config.h"
54 #include "asterisk/parking.h"
55 #include "asterisk/causes.h"
56 #include "asterisk/test.h"
57 #include "asterisk/sem.h"
58 #include "asterisk/stream.h"
59
60 /*!
61  * \brief Used to queue an action frame onto a bridge channel and write an action frame into a bridge.
62  * \since 12.0.0
63  *
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.
68  *
69  * \retval 0 on success.
70  * \retval -1 on error.
71  */
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);
73
74 /*!
75  * \brief Counter used for assigning synchronous bridge action IDs
76  */
77 static int sync_ids;
78
79 /*!
80  * \brief Frame payload for synchronous bridge actions.
81  *
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
84  * bridge_sync object.
85  */
86 struct sync_payload {
87         /*! Unique ID for this synchronous action */
88         unsigned int id;
89         /*! Actual frame data to process */
90         unsigned char data[0];
91 };
92
93 /*!
94  * \brief Synchronous bridge action object.
95  *
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.
99  */
100 struct bridge_sync {
101         /*! Unique ID of this synchronization object. Corresponds with ID in synchronous frame payload */
102         unsigned int id;
103         /*! Semaphore used for synchronization */
104         struct ast_sem sem;
105         /*! Pointer to next entry in the list */
106         AST_LIST_ENTRY(bridge_sync) list;
107 };
108
109 /*!
110  * \brief List holding active synchronous action objects.
111  */
112 static AST_RWLIST_HEAD_STATIC(sync_structs, bridge_sync);
113
114 /*!
115  * \brief initialize a synchronous bridge object.
116  *
117  * This both initializes the structure and adds it to the list of
118  * synchronization structures.
119  *
120  * \param sync_struct The synchronization object to initialize.
121  * \param id ID to assign to the synchronization object.
122  */
123 static void bridge_sync_init(struct bridge_sync *sync_struct, unsigned int id)
124 {
125         memset(sync_struct, 0, sizeof(*sync_struct));
126         sync_struct->id = id;
127         ast_sem_init(&sync_struct->sem, 0, 0);
128
129         AST_RWLIST_WRLOCK(&sync_structs);
130         AST_RWLIST_INSERT_TAIL(&sync_structs, sync_struct, list);
131         AST_RWLIST_UNLOCK(&sync_structs);
132 }
133
134 /*!
135  * \brief Clean up a syncrhonization bridge object.
136  *
137  * This frees fields within the synchronization object and removes
138  * it from the list of active synchronization objects.
139  *
140  * Since synchronization objects are stack-allocated, it is vital
141  * that this is called before the synchronization object goes
142  * out of scope.
143  *
144  * \param sync_struct Synchronization object to clean up.
145  */
146 static void bridge_sync_cleanup(struct bridge_sync *sync_struct)
147 {
148         struct bridge_sync *iter;
149
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);
154                         break;
155                 }
156         }
157         AST_LIST_TRAVERSE_SAFE_END;
158         AST_RWLIST_UNLOCK(&sync_structs);
159
160         ast_sem_destroy(&sync_struct->sem);
161 }
162
163 /*!
164  * \brief Failsafe for synchronous bridge action waiting.
165  *
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
169  * completed.
170  *
171  * If a significant amount of time passes, then we will abandon
172  * waiting for the synchrnous bridge action to complete.
173  *
174  * This constant represents the number of milliseconds we will
175  * wait for the bridge action to complete.
176  */
177 #define PLAYBACK_TIMEOUT (600 * 1000)
178
179 /*!
180  * \brief Wait for a synchronous bridge action to complete.
181  *
182  * \param sync_struct Synchronization object corresponding to the bridge action.
183  */
184 static void bridge_sync_wait(struct bridge_sync *sync_struct)
185 {
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,
190         };
191
192         ast_sem_timedwait(&sync_struct->sem, &timeout_spec);
193 }
194
195 /*!
196  * \brief Signal that waiting for a synchronous bridge action is no longer necessary.
197  *
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.
202  *
203  * \param sync_struct Synchronization object corresponding to the bridge action.
204  */
205 static void bridge_sync_signal(struct bridge_sync *sync_struct)
206 {
207         ast_sem_post(&sync_struct->sem);
208 }
209
210 void ast_bridge_channel_lock_bridge(struct ast_bridge_channel *bridge_channel)
211 {
212         struct ast_bridge *bridge;
213
214         for (;;) {
215                 /* Safely get the bridge pointer */
216                 ast_bridge_channel_lock(bridge_channel);
217                 bridge = bridge_channel->bridge;
218                 ao2_ref(bridge, +1);
219                 ast_bridge_channel_unlock(bridge_channel);
220
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) {
224                         ao2_ref(bridge, -1);
225                         return;
226                 }
227                 ast_bridge_unlock(bridge);
228                 ao2_ref(bridge, -1);
229         }
230 }
231
232 int ast_bridge_channel_notify_talking(struct ast_bridge_channel *bridge_channel, int started_talking)
233 {
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,
238         };
239
240         return ast_bridge_channel_queue_frame(bridge_channel, &action);
241 }
242
243 /*!
244  * \internal
245  * \brief Poke the bridge_channel thread
246  */
247 static void bridge_channel_poke(struct ast_bridge_channel *bridge_channel)
248 {
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);
252         }
253 }
254
255 /*!
256  * \internal
257  * \brief Set actual cause on channel.
258  * \since 12.0.0
259  *
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.
263  *
264  * \return Actual cause set on channel.
265  */
266 static int channel_set_cause(struct ast_channel *chan, int cause)
267 {
268         ast_channel_lock(chan);
269         if (cause <= 0) {
270                 cause = ast_channel_hangupcause(chan);
271                 if (cause <= 0) {
272                         cause = AST_CAUSE_NORMAL_CLEARING;
273                 }
274         }
275         ast_channel_hangupcause_set(chan, cause);
276         ast_channel_unlock(chan);
277         return cause;
278 }
279
280 void ast_bridge_channel_leave_bridge_nolock(struct ast_bridge_channel *bridge_channel, enum bridge_channel_state new_state, int cause)
281 {
282         if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
283                 return;
284         }
285
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,
288                 new_state);
289
290         channel_set_cause(bridge_channel->chan, cause);
291
292         ast_channel_lock(bridge_channel->chan);
293         ast_bridge_vars_set(bridge_channel->chan, NULL, NULL);
294         ast_channel_unlock(bridge_channel->chan);
295
296         /* Change the state on the bridge channel */
297         bridge_channel->state = new_state;
298
299         bridge_channel_poke(bridge_channel);
300 }
301
302 void ast_bridge_channel_leave_bridge(struct ast_bridge_channel *bridge_channel, enum bridge_channel_state new_state, int cause)
303 {
304         ast_bridge_channel_lock(bridge_channel);
305         ast_bridge_channel_leave_bridge_nolock(bridge_channel, new_state, cause);
306         ast_bridge_channel_unlock(bridge_channel);
307 }
308
309 struct ast_bridge_channel *ast_bridge_channel_peer(struct ast_bridge_channel *bridge_channel)
310 {
311         struct ast_bridge *bridge = bridge_channel->bridge;
312         struct ast_bridge_channel *other = NULL;
313
314         if (bridge_channel->in_bridge && bridge->num_channels == 2) {
315                 AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
316                         if (other != bridge_channel) {
317                                 break;
318                         }
319                 }
320         }
321
322         return other;
323 }
324
325 void ast_bridge_channel_restore_formats(struct ast_bridge_channel *bridge_channel)
326 {
327         ast_assert(bridge_channel->read_format != NULL);
328         ast_assert(bridge_channel->write_format != NULL);
329
330         ast_channel_lock(bridge_channel->chan);
331
332         /* Restore original formats of the channel as they came in */
333         if (ast_format_cmp(ast_channel_readformat(bridge_channel->chan), bridge_channel->read_format) == AST_FORMAT_CMP_NOT_EQUAL) {
334                 ast_debug(1, "Bridge is returning %p(%s) to read format %s\n",
335                         bridge_channel, ast_channel_name(bridge_channel->chan),
336                         ast_format_get_name(bridge_channel->read_format));
337                 if (ast_set_read_format(bridge_channel->chan, bridge_channel->read_format)) {
338                         ast_debug(1, "Bridge failed to return %p(%s) to read format %s\n",
339                                 bridge_channel, ast_channel_name(bridge_channel->chan),
340                                 ast_format_get_name(bridge_channel->read_format));
341                 }
342         }
343         if (ast_format_cmp(ast_channel_writeformat(bridge_channel->chan), bridge_channel->write_format) == AST_FORMAT_CMP_NOT_EQUAL) {
344                 ast_debug(1, "Bridge is returning %p(%s) to write format %s\n",
345                         bridge_channel, ast_channel_name(bridge_channel->chan),
346                         ast_format_get_name(bridge_channel->write_format));
347                 if (ast_set_write_format(bridge_channel->chan, bridge_channel->write_format)) {
348                         ast_debug(1, "Bridge failed to return %p(%s) to write format %s\n",
349                                 bridge_channel, ast_channel_name(bridge_channel->chan),
350                                 ast_format_get_name(bridge_channel->write_format));
351                 }
352         }
353
354         ast_channel_unlock(bridge_channel->chan);
355 }
356
357 struct ast_bridge *ast_bridge_channel_merge_inhibit(struct ast_bridge_channel *bridge_channel, int request)
358 {
359         struct ast_bridge *bridge;
360
361         ast_bridge_channel_lock_bridge(bridge_channel);
362         bridge = bridge_channel->bridge;
363         ao2_ref(bridge, +1);
364         bridge_merge_inhibit_nolock(bridge, request);
365         ast_bridge_unlock(bridge);
366         return bridge;
367 }
368
369 void ast_bridge_channel_update_linkedids(struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
370 {
371         struct ast_bridge_channel *other;
372         struct ast_bridge *bridge = bridge_channel->bridge;
373         struct ast_channel *oldest_linkedid_chan = bridge_channel->chan;
374
375         AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
376                 if (other == swap) {
377                         continue;
378                 }
379                 oldest_linkedid_chan = ast_channel_internal_oldest_linkedid(
380                         oldest_linkedid_chan, other->chan);
381         }
382
383         ast_channel_lock(bridge_channel->chan);
384         ast_channel_internal_copy_linkedid(bridge_channel->chan, oldest_linkedid_chan);
385         ast_channel_unlock(bridge_channel->chan);
386         AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
387                 if (other == swap) {
388                         continue;
389                 }
390                 ast_channel_lock(other->chan);
391                 ast_channel_internal_copy_linkedid(other->chan, oldest_linkedid_chan);
392                 ast_channel_unlock(other->chan);
393         }
394 }
395
396 /*!
397  * \internal
398  * \brief Set dest's empty peeraccount with the src's non-empty accountcode.
399  * \since 12.5.0
400  *
401  * \param dest Channel to update peeraccount.
402  * \param src Channel to get accountcode from.
403  *
404  * \note Both channels are already locked.
405  *
406  * \return Nothing
407  */
408 static void channel_fill_empty_peeraccount(struct ast_channel *dest, struct ast_channel *src)
409 {
410         if (ast_strlen_zero(ast_channel_peeraccount(dest))
411                 && !ast_strlen_zero(ast_channel_accountcode(src))) {
412                 ast_debug(1, "Setting channel %s peeraccount with channel %s accountcode '%s'.\n",
413                         ast_channel_name(dest),
414                         ast_channel_name(src), ast_channel_accountcode(src));
415                 ast_channel_peeraccount_set(dest, ast_channel_accountcode(src));
416         }
417 }
418
419 /*!
420  * \internal
421  * \brief Set dest's empty accountcode with the src's non-empty peeraccount.
422  * \since 12.5.0
423  *
424  * \param dest Channel to update accountcode.
425  * \param src Channel to get peeraccount from.
426  *
427  * \note Both channels are already locked.
428  *
429  * \return Nothing
430  */
431 static void channel_fill_empty_accountcode(struct ast_channel *dest, struct ast_channel *src)
432 {
433         if (ast_strlen_zero(ast_channel_accountcode(dest))
434                 && !ast_strlen_zero(ast_channel_peeraccount(src))) {
435                 ast_debug(1, "Setting channel %s accountcode with channel %s peeraccount '%s'.\n",
436                         ast_channel_name(dest),
437                         ast_channel_name(src), ast_channel_peeraccount(src));
438                 ast_channel_accountcode_set(dest, ast_channel_peeraccount(src));
439         }
440 }
441
442 /*!
443  * \internal
444  * \brief Set empty peeraccount and accountcode in a channel from the other channel.
445  * \since 12.5.0
446  *
447  * \param c0 First bridge channel to update.
448  * \param c1 Second bridge channel to update.
449  *
450  * \note Both channels are already locked.
451  *
452  * \return Nothing
453  */
454 static void channel_set_empty_accountcodes(struct ast_channel *c0, struct ast_channel *c1)
455 {
456         /* Set empty peeraccount from the other channel's accountcode. */
457         channel_fill_empty_peeraccount(c0, c1);
458         channel_fill_empty_peeraccount(c1, c0);
459
460         /* Set empty accountcode from the other channel's peeraccount. */
461         channel_fill_empty_accountcode(c0, c1);
462         channel_fill_empty_accountcode(c1, c0);
463 }
464
465 /*!
466  * \internal
467  * \brief Update dest's peeraccount with the src's different accountcode.
468  * \since 12.5.0
469  *
470  * \param dest Channel to update peeraccount.
471  * \param src Channel to get accountcode from.
472  *
473  * \note Both channels are already locked.
474  *
475  * \return Nothing
476  */
477 static void channel_update_peeraccount(struct ast_channel *dest, struct ast_channel *src)
478 {
479         if (strcmp(ast_channel_accountcode(src), ast_channel_peeraccount(dest))) {
480                 ast_debug(1, "Changing channel %s peeraccount '%s' to match channel %s accountcode '%s'.\n",
481                         ast_channel_name(dest), ast_channel_peeraccount(dest),
482                         ast_channel_name(src), ast_channel_accountcode(src));
483                 ast_channel_peeraccount_set(dest, ast_channel_accountcode(src));
484         }
485 }
486
487 /*!
488  * \internal
489  * \brief Update peeraccounts to match the other channel's accountcode.
490  * \since 12.5.0
491  *
492  * \param c0 First channel to update.
493  * \param c1 Second channel to update.
494  *
495  * \note Both channels are already locked.
496  *
497  * \return Nothing
498  */
499 static void channel_update_peeraccounts(struct ast_channel *c0, struct ast_channel *c1)
500 {
501         channel_update_peeraccount(c0, c1);
502         channel_update_peeraccount(c1, c0);
503 }
504
505 /*!
506  * \internal
507  * \brief Update channel accountcodes because a channel is joining a bridge.
508  * \since 12.5.0
509  *
510  * \param joining Channel joining the bridge.
511  * \param swap Channel being replaced by the joining channel.  May be NULL.
512  *
513  * \note The bridge must be locked prior to calling this function.
514  *
515  * \return Nothing
516  */
517 static void bridge_channel_update_accountcodes_joining(struct ast_bridge_channel *joining, struct ast_bridge_channel *swap)
518 {
519         struct ast_bridge *bridge = joining->bridge;
520         struct ast_bridge_channel *other;
521         unsigned int swap_in_bridge = 0;
522         unsigned int will_be_two_party;
523
524         /*
525          * Only update the peeraccount to match if the joining channel
526          * will make it a two party bridge.
527          */
528         if (bridge->num_channels <= 2 && swap) {
529                 AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
530                         if (other == swap) {
531                                 swap_in_bridge = 1;
532                                 break;
533                         }
534                 }
535         }
536         will_be_two_party = (1 == bridge->num_channels - swap_in_bridge);
537
538         AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
539                 if (other == swap) {
540                         continue;
541                 }
542                 ast_assert(joining != other);
543                 ast_channel_lock_both(joining->chan, other->chan);
544                 channel_set_empty_accountcodes(joining->chan, other->chan);
545                 if (will_be_two_party) {
546                         channel_update_peeraccounts(joining->chan, other->chan);
547                 }
548                 ast_channel_unlock(joining->chan);
549                 ast_channel_unlock(other->chan);
550         }
551 }
552
553 /*!
554  * \internal
555  * \brief Update channel peeraccount codes because a channel has left a bridge.
556  * \since 12.5.0
557  *
558  * \param leaving Channel leaving the bridge. (Has already been removed actually)
559  *
560  * \note The bridge must be locked prior to calling this function.
561  *
562  * \return Nothing
563  */
564 static void bridge_channel_update_accountcodes_leaving(struct ast_bridge_channel *leaving)
565 {
566         struct ast_bridge *bridge = leaving->bridge;
567         struct ast_bridge_channel *first;
568         struct ast_bridge_channel *second;
569
570         if (bridge->num_channels != 2 || bridge->dissolved) {
571                 return;
572         }
573
574         first = AST_LIST_FIRST(&bridge->channels);
575         second = AST_LIST_LAST(&bridge->channels);
576         ast_assert(first && first != second);
577         ast_channel_lock_both(first->chan, second->chan);
578         channel_set_empty_accountcodes(first->chan, second->chan);
579         channel_update_peeraccounts(first->chan, second->chan);
580         ast_channel_unlock(second->chan);
581         ast_channel_unlock(first->chan);
582 }
583
584 void ast_bridge_channel_update_accountcodes(struct ast_bridge_channel *joining, struct ast_bridge_channel *leaving)
585 {
586         if (joining) {
587                 bridge_channel_update_accountcodes_joining(joining, leaving);
588         } else {
589                 bridge_channel_update_accountcodes_leaving(leaving);
590         }
591 }
592
593 void ast_bridge_channel_kick(struct ast_bridge_channel *bridge_channel, int cause)
594 {
595         struct ast_bridge_features *features = bridge_channel->features;
596         struct ast_bridge_hook *hook;
597         struct ao2_iterator iter;
598
599         ast_bridge_channel_lock(bridge_channel);
600         if (bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
601                 channel_set_cause(bridge_channel->chan, cause);
602                 cause = 0;
603         }
604         ast_bridge_channel_unlock(bridge_channel);
605
606         /* Run any hangup hooks. */
607         iter = ao2_iterator_init(features->other_hooks, 0);
608         for (; (hook = ao2_iterator_next(&iter)); ao2_ref(hook, -1)) {
609                 int remove_me;
610
611                 if (hook->type != AST_BRIDGE_HOOK_TYPE_HANGUP) {
612                         continue;
613                 }
614                 remove_me = hook->callback(bridge_channel, hook->hook_pvt);
615                 if (remove_me) {
616                         ast_debug(1, "Hangup hook %p is being removed from %p(%s)\n",
617                                 hook, bridge_channel, ast_channel_name(bridge_channel->chan));
618                         ao2_unlink(features->other_hooks, hook);
619                 }
620         }
621         ao2_iterator_destroy(&iter);
622
623         /* Default hangup action. */
624         ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END, cause);
625 }
626
627 /*!
628  * \internal
629  * \brief Write an \ref ast_frame onto the bridge channel
630  * \since 12.0.0
631  *
632  * \param bridge_channel Which channel to queue the frame onto.
633  * \param frame The frame to write onto the bridge_channel
634  *
635  * \retval 0 on success.
636  * \retval -1 on error.
637  */
638 static int bridge_channel_write_frame(struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
639 {
640         const struct ast_control_t38_parameters *t38_parameters;
641
642         ast_assert(frame->frametype != AST_FRAME_BRIDGE_ACTION_SYNC);
643
644         ast_bridge_channel_lock_bridge(bridge_channel);
645 /*
646  * XXX need to implement a deferred write queue for when there
647  * is no peer channel in the bridge (yet or it was kicked).
648  *
649  * The tech decides if a frame needs to be pushed back for deferral.
650  * simple_bridge/native_bridge are likely the only techs that will do this.
651  */
652         bridge_channel->bridge->technology->write(bridge_channel->bridge, bridge_channel, frame);
653
654         /* Remember any owed events to the bridge. */
655         switch (frame->frametype) {
656         case AST_FRAME_DTMF_BEGIN:
657                 bridge_channel->owed.dtmf_tv = ast_tvnow();
658                 bridge_channel->owed.dtmf_digit = frame->subclass.integer;
659                 break;
660         case AST_FRAME_DTMF_END:
661                 bridge_channel->owed.dtmf_digit = '\0';
662                 break;
663         case AST_FRAME_CONTROL:
664                 /*
665                  * We explicitly will not remember HOLD/UNHOLD frames because
666                  * things like attended transfers will handle them.
667                  */
668                 switch (frame->subclass.integer) {
669                 case AST_CONTROL_T38_PARAMETERS:
670                         t38_parameters = frame->data.ptr;
671                         switch (t38_parameters->request_response) {
672                         case AST_T38_REQUEST_NEGOTIATE:
673                         case AST_T38_NEGOTIATED:
674                                 bridge_channel->owed.t38_terminate = 1;
675                                 break;
676                         case AST_T38_REQUEST_TERMINATE:
677                         case AST_T38_TERMINATED:
678                         case AST_T38_REFUSED:
679                                 bridge_channel->owed.t38_terminate = 0;
680                                 break;
681                         default:
682                                 break;
683                         }
684                         break;
685                 default:
686                         break;
687                 }
688                 break;
689         default:
690                 break;
691         }
692         ast_bridge_unlock(bridge_channel->bridge);
693
694         /*
695          * Claim successful write to bridge.  If deferred frame
696          * support is added, claim successfully deferred.
697          */
698         return 0;
699 }
700
701 /*!
702  * \internal
703  * \brief Cancel owed events by the channel to the bridge.
704  * \since 13.8.0
705  *
706  * \param bridge_channel Channel that owes events to the bridge.
707  *
708  * \note On entry, the bridge_channel->bridge is already locked.
709  *
710  * \return Nothing
711  */
712 static void bridge_channel_cancel_owed_events(struct ast_bridge_channel *bridge_channel)
713 {
714         bridge_channel->owed.dtmf_digit = '\0';
715         bridge_channel->owed.t38_terminate = 0;
716 }
717
718 void bridge_channel_settle_owed_events(struct ast_bridge *orig_bridge, struct ast_bridge_channel *bridge_channel)
719 {
720         if (bridge_channel->owed.dtmf_digit) {
721                 struct ast_frame frame = {
722                         .frametype = AST_FRAME_DTMF_END,
723                         .subclass.integer = bridge_channel->owed.dtmf_digit,
724                         .src = "Bridge channel owed DTMF",
725                 };
726
727                 frame.len = ast_tvdiff_ms(ast_tvnow(), bridge_channel->owed.dtmf_tv);
728                 if (frame.len < option_dtmfminduration) {
729                         frame.len = option_dtmfminduration;
730                 }
731                 ast_log(LOG_DTMF, "DTMF end '%c' simulated to bridge %s because %s left.  Duration %ld ms.\n",
732                         bridge_channel->owed.dtmf_digit, orig_bridge->uniqueid,
733                         ast_channel_name(bridge_channel->chan), frame.len);
734                 bridge_channel->owed.dtmf_digit = '\0';
735                 orig_bridge->technology->write(orig_bridge, NULL, &frame);
736         }
737         if (bridge_channel->owed.t38_terminate) {
738                 struct ast_control_t38_parameters t38_parameters = {
739                         .request_response = AST_T38_TERMINATED,
740                 };
741                 struct ast_frame frame = {
742                         .frametype = AST_FRAME_CONTROL,
743                         .subclass.integer = AST_CONTROL_T38_PARAMETERS,
744                         .data.ptr = &t38_parameters,
745                         .datalen = sizeof(t38_parameters),
746                         .src = "Bridge channel owed T.38 terminate",
747                 };
748
749                 ast_debug(1, "T.38 terminate simulated to bridge %s because %s left.\n",
750                         orig_bridge->uniqueid, ast_channel_name(bridge_channel->chan));
751                 bridge_channel->owed.t38_terminate = 0;
752                 orig_bridge->technology->write(orig_bridge, NULL, &frame);
753         }
754 }
755
756 /*!
757  * \internal
758  * \brief Suspend a channel from a bridge.
759  *
760  * \param bridge_channel Channel to suspend.
761  *
762  * \note This function assumes bridge_channel->bridge is locked.
763  *
764  * \return Nothing
765  */
766 void bridge_channel_internal_suspend_nolock(struct ast_bridge_channel *bridge_channel)
767 {
768         bridge_channel->suspended = 1;
769         if (bridge_channel->in_bridge) {
770                 --bridge_channel->bridge->num_active;
771         }
772
773         /* Get technology bridge threads off of the channel. */
774         if (bridge_channel->bridge->technology->suspend) {
775                 bridge_channel->bridge->technology->suspend(bridge_channel->bridge, bridge_channel);
776         }
777 }
778
779 /*!
780  * \internal
781  * \brief Suspend a channel from a bridge.
782  *
783  * \param bridge_channel Channel to suspend.
784  *
785  * \return Nothing
786  */
787 static void bridge_channel_suspend(struct ast_bridge_channel *bridge_channel)
788 {
789         ast_bridge_channel_lock_bridge(bridge_channel);
790         bridge_channel_internal_suspend_nolock(bridge_channel);
791         ast_bridge_unlock(bridge_channel->bridge);
792 }
793
794 /*!
795  * \internal
796  * \brief Unsuspend a channel from a bridge.
797  *
798  * \param bridge_channel Channel to unsuspend.
799  *
800  * \note This function assumes bridge_channel->bridge is locked.
801  *
802  * \return Nothing
803  */
804 void bridge_channel_internal_unsuspend_nolock(struct ast_bridge_channel *bridge_channel)
805 {
806         bridge_channel->suspended = 0;
807         if (bridge_channel->in_bridge) {
808                 ++bridge_channel->bridge->num_active;
809         }
810
811         /* Wake technology bridge threads to take care of channel again. */
812         if (bridge_channel->bridge->technology->unsuspend) {
813                 bridge_channel->bridge->technology->unsuspend(bridge_channel->bridge, bridge_channel);
814         }
815
816         /* Wake suspended channel. */
817         ast_bridge_channel_lock(bridge_channel);
818         ast_cond_signal(&bridge_channel->cond);
819         ast_bridge_channel_unlock(bridge_channel);
820 }
821
822 /*!
823  * \internal
824  * \brief Unsuspend a channel from a bridge.
825  *
826  * \param bridge_channel Channel to unsuspend.
827  *
828  * \return Nothing
829  */
830 static void bridge_channel_unsuspend(struct ast_bridge_channel *bridge_channel)
831 {
832         ast_bridge_channel_lock_bridge(bridge_channel);
833         bridge_channel_internal_unsuspend_nolock(bridge_channel);
834         ast_bridge_unlock(bridge_channel->bridge);
835 }
836
837 /*!
838  * \internal
839  * \brief Queue an action frame onto the bridge channel with data.
840  * \since 12.0.0
841  *
842  * \param bridge_channel Which channel to queue the frame onto.
843  * \param action Type of bridge action frame.
844  * \param data Frame payload data to pass.
845  * \param datalen Frame payload data length to pass.
846  *
847  * \retval 0 on success.
848  * \retval -1 on error.
849  */
850 static int bridge_channel_queue_action_data(struct ast_bridge_channel *bridge_channel,
851         enum bridge_channel_action_type action, const void *data, size_t datalen)
852 {
853         struct ast_frame frame = {
854                 .frametype = AST_FRAME_BRIDGE_ACTION,
855                 .subclass.integer = action,
856                 .datalen = datalen,
857                 .data.ptr = (void *) data,
858         };
859
860         return ast_bridge_channel_queue_frame(bridge_channel, &frame);
861 }
862
863 /*!
864  * \internal
865  * \brief Queue an action frame onto the bridge channel with data synchronously.
866  * \since 12.2.0
867  *
868  * The function will not return until the queued frame is freed.
869  *
870  * \param bridge_channel Which channel to queue the frame onto.
871  * \param action Type of bridge action frame.
872  * \param data Frame payload data to pass.
873  * \param datalen Frame payload data length to pass.
874  *
875  * \retval 0 on success.
876  * \retval -1 on error.
877  */
878 static int bridge_channel_queue_action_data_sync(struct ast_bridge_channel *bridge_channel,
879         enum bridge_channel_action_type action, const void *data, size_t datalen)
880 {
881         struct sync_payload *sync_payload;
882         int sync_payload_len = sizeof(*sync_payload) + datalen;
883         struct bridge_sync sync_struct;
884         struct ast_frame frame = {
885                 .frametype = AST_FRAME_BRIDGE_ACTION_SYNC,
886                 .subclass.integer = action,
887         };
888
889         /* Make sure we don't end up trying to wait on ourself to deliver the frame */
890         ast_assert(!pthread_equal(pthread_self(), bridge_channel->thread));
891
892         sync_payload = ast_alloca(sync_payload_len);
893         sync_payload->id = ast_atomic_fetchadd_int(&sync_ids, +1);
894         memcpy(sync_payload->data, data, datalen);
895
896         frame.datalen = sync_payload_len;
897         frame.data.ptr = sync_payload;
898
899         bridge_sync_init(&sync_struct, sync_payload->id);
900         if (ast_bridge_channel_queue_frame(bridge_channel, &frame)) {
901                 bridge_sync_cleanup(&sync_struct);
902                 return -1;
903         }
904
905         bridge_sync_wait(&sync_struct);
906         bridge_sync_cleanup(&sync_struct);
907         return 0;
908 }
909 /*!
910  * \internal
911  * \brief Write an action frame onto the bridge channel with data.
912  * \since 12.0.0
913  *
914  * \param bridge_channel Which channel to queue the frame onto.
915  * \param action Type of bridge action frame.
916  * \param data Frame payload data to pass.
917  * \param datalen Frame payload data length to pass.
918  *
919  * \retval 0 on success.
920  * \retval -1 on error.
921  */
922 static int bridge_channel_write_action_data(struct ast_bridge_channel *bridge_channel,
923         enum bridge_channel_action_type action, const void *data, size_t datalen)
924 {
925         struct ast_frame frame = {
926                 .frametype = AST_FRAME_BRIDGE_ACTION,
927                 .subclass.integer = action,
928                 .datalen = datalen,
929                 .data.ptr = (void *) data,
930         };
931
932         return bridge_channel_write_frame(bridge_channel, &frame);
933 }
934
935 static void bridge_frame_free(struct ast_frame *frame)
936 {
937         if (frame->frametype == AST_FRAME_BRIDGE_ACTION_SYNC) {
938                 struct sync_payload *sync_payload = frame->data.ptr;
939                 struct bridge_sync *sync;
940
941                 AST_RWLIST_RDLOCK(&sync_structs);
942                 AST_RWLIST_TRAVERSE(&sync_structs, sync, list) {
943                         if (sync->id == sync_payload->id) {
944                                 break;
945                         }
946                 }
947                 if (sync) {
948                         bridge_sync_signal(sync);
949                 }
950                 AST_RWLIST_UNLOCK(&sync_structs);
951         }
952
953         ast_frfree(frame);
954 }
955
956 int ast_bridge_channel_queue_frame(struct ast_bridge_channel *bridge_channel, struct ast_frame *fr)
957 {
958         struct ast_frame *dup;
959
960         if (bridge_channel->suspended
961                 /* Also defer DTMF frames. */
962                 && fr->frametype != AST_FRAME_DTMF_BEGIN
963                 && fr->frametype != AST_FRAME_DTMF_END
964                 && !ast_is_deferrable_frame(fr)) {
965                 /* Drop non-deferable frames when suspended. */
966                 return 0;
967         }
968         if (fr->frametype == AST_FRAME_NULL) {
969                 /* "Accept" the frame and discard it. */
970                 return 0;
971         }
972
973         dup = ast_frdup(fr);
974         if (!dup) {
975                 return -1;
976         }
977
978         ast_bridge_channel_lock(bridge_channel);
979         if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
980                 /* Drop frames on channels leaving the bridge. */
981                 ast_bridge_channel_unlock(bridge_channel);
982                 bridge_frame_free(dup);
983                 return 0;
984         }
985
986         if (ast_channel_is_multistream(bridge_channel->chan) &&
987             (fr->frametype == AST_FRAME_IMAGE || fr->frametype == AST_FRAME_TEXT ||
988              fr->frametype == AST_FRAME_VIDEO || fr->frametype == AST_FRAME_VOICE)) {
989                 /* Media frames need to be mapped to an appropriate write stream */
990                 dup->stream_num = AST_VECTOR_GET(
991                         &bridge_channel->stream_map.to_bridge, fr->stream_num);
992         } else {
993                 dup->stream_num = -1;
994         }
995
996         AST_LIST_INSERT_TAIL(&bridge_channel->wr_queue, dup, frame_list);
997         if (ast_alertpipe_write(bridge_channel->alert_pipe)) {
998                 ast_log(LOG_ERROR, "We couldn't write alert pipe for %p(%s)... something is VERY wrong\n",
999                         bridge_channel, ast_channel_name(bridge_channel->chan));
1000         }
1001         ast_bridge_channel_unlock(bridge_channel);
1002         return 0;
1003 }
1004
1005 int ast_bridge_queue_everyone_else(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
1006 {
1007         struct ast_bridge_channel *cur;
1008         int not_written = -1;
1009
1010         if (frame->frametype == AST_FRAME_NULL) {
1011                 /* "Accept" the frame and discard it. */
1012                 return 0;
1013         }
1014
1015         AST_LIST_TRAVERSE(&bridge->channels, cur, entry) {
1016                 if (cur == bridge_channel) {
1017                         continue;
1018                 }
1019                 if (!ast_bridge_channel_queue_frame(cur, frame)) {
1020                         not_written = 0;
1021                 }
1022         }
1023         return not_written;
1024 }
1025
1026 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)
1027 {
1028         struct ast_frame frame = {
1029                 .frametype = AST_FRAME_CONTROL,
1030                 .subclass.integer = control,
1031                 .datalen = datalen,
1032                 .data.ptr = (void *) data,
1033         };
1034
1035         return ast_bridge_channel_queue_frame(bridge_channel, &frame);
1036 }
1037
1038 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)
1039 {
1040         struct ast_frame frame = {
1041                 .frametype = AST_FRAME_CONTROL,
1042                 .subclass.integer = control,
1043                 .datalen = datalen,
1044                 .data.ptr = (void *) data,
1045         };
1046
1047         return bridge_channel_write_frame(bridge_channel, &frame);
1048 }
1049
1050 int ast_bridge_channel_write_hold(struct ast_bridge_channel *bridge_channel, const char *moh_class)
1051 {
1052         struct ast_json *blob;
1053         int res;
1054         size_t datalen;
1055
1056         if (!ast_strlen_zero(moh_class)) {
1057                 datalen = strlen(moh_class) + 1;
1058
1059                 blob = ast_json_pack("{s: s}",
1060                         "musicclass", moh_class);
1061         } else {
1062                 moh_class = NULL;
1063                 datalen = 0;
1064                 blob = NULL;
1065         }
1066
1067         ast_channel_publish_cached_blob(bridge_channel->chan, ast_channel_hold_type(), blob);
1068
1069         res = ast_bridge_channel_write_control_data(bridge_channel, AST_CONTROL_HOLD,
1070                 moh_class, datalen);
1071
1072         ast_json_unref(blob);
1073         return res;
1074 }
1075
1076 int ast_bridge_channel_write_unhold(struct ast_bridge_channel *bridge_channel)
1077 {
1078         ast_channel_publish_cached_blob(bridge_channel->chan, ast_channel_unhold_type(), NULL);
1079
1080         return ast_bridge_channel_write_control_data(bridge_channel, AST_CONTROL_UNHOLD, NULL, 0);
1081 }
1082
1083 /*!
1084  * \internal
1085  * \brief Helper function to kick off a PBX app on a bridge_channel
1086  */
1087 static int run_app_helper(struct ast_channel *chan, const char *app_name, const char *app_args)
1088 {
1089         int res = 0;
1090
1091         if (!strcasecmp("Gosub", app_name)) {
1092                 ast_app_exec_sub(NULL, chan, app_args, 0);
1093         } else if (!strcasecmp("Macro", app_name)) {
1094                 ast_app_exec_macro(NULL, chan, app_args);
1095         } else {
1096                 struct ast_app *app;
1097
1098                 app = pbx_findapp(app_name);
1099                 if (!app) {
1100                         ast_log(LOG_WARNING, "Could not find application (%s)\n", app_name);
1101                 } else {
1102                         struct ast_str *substituted_args = ast_str_create(16);
1103
1104                         if (substituted_args) {
1105                                 ast_str_substitute_variables(&substituted_args, 0, chan, app_args);
1106                                 res = pbx_exec(chan, app, ast_str_buffer(substituted_args));
1107                                 ast_free(substituted_args);
1108                         } else {
1109                                 ast_log(LOG_WARNING, "Could not substitute application argument variables for %s\n", app_name);
1110                                 res = pbx_exec(chan, app, app_args);
1111                         }
1112                 }
1113         }
1114         return res;
1115 }
1116
1117 void ast_bridge_channel_run_app(struct ast_bridge_channel *bridge_channel, const char *app_name, const char *app_args, const char *moh_class)
1118 {
1119         if (moh_class) {
1120                 ast_bridge_channel_write_hold(bridge_channel, moh_class);
1121         }
1122         if (run_app_helper(bridge_channel->chan, app_name, S_OR(app_args, ""))) {
1123                 /* Break the bridge if the app returns non-zero. */
1124                 ast_bridge_channel_kick(bridge_channel, AST_CAUSE_NORMAL_CLEARING);
1125         }
1126         if (moh_class) {
1127                 ast_bridge_channel_write_unhold(bridge_channel);
1128         }
1129 }
1130
1131 struct bridge_run_app {
1132         /*! Offset into app_name[] where the MOH class name starts.  (zero if no MOH) */
1133         int moh_offset;
1134         /*! Offset into app_name[] where the application argument string starts. (zero if no arguments) */
1135         int app_args_offset;
1136         /*! Application name to run. */
1137         char app_name[0];
1138 };
1139
1140 /*!
1141  * \internal
1142  * \brief Handle the run application bridge action.
1143  * \since 12.0.0
1144  *
1145  * \param bridge_channel Which channel to run the application on.
1146  * \param data Action frame data to run the application.
1147  *
1148  * \return Nothing
1149  */
1150 static void bridge_channel_run_app(struct ast_bridge_channel *bridge_channel, struct bridge_run_app *data)
1151 {
1152         ast_bridge_channel_run_app(bridge_channel, data->app_name,
1153                 data->app_args_offset ? &data->app_name[data->app_args_offset] : NULL,
1154                 data->moh_offset ? &data->app_name[data->moh_offset] : NULL);
1155 }
1156
1157 /*!
1158  * \internal
1159  * \brief Marshal an application to be executed on a bridge_channel
1160  */
1161 static int payload_helper_app(ast_bridge_channel_post_action_data post_it,
1162         struct ast_bridge_channel *bridge_channel, const char *app_name, const char *app_args, const char *moh_class)
1163 {
1164         struct bridge_run_app *app_data;
1165         size_t len_name = strlen(app_name) + 1;
1166         size_t len_args = ast_strlen_zero(app_args) ? 0 : strlen(app_args) + 1;
1167         size_t len_moh = !moh_class ? 0 : strlen(moh_class) + 1;
1168         size_t len_data = sizeof(*app_data) + len_name + len_args + len_moh;
1169
1170         /* Fill in application run frame data. */
1171         app_data = alloca(len_data);
1172         app_data->app_args_offset = len_args ? len_name : 0;
1173         app_data->moh_offset = len_moh ? len_name + len_args : 0;
1174         strcpy(app_data->app_name, app_name);/* Safe */
1175         if (len_args) {
1176                 strcpy(&app_data->app_name[app_data->app_args_offset], app_args);/* Safe */
1177         }
1178         if (moh_class) {
1179                 strcpy(&app_data->app_name[app_data->moh_offset], moh_class);/* Safe */
1180         }
1181
1182         return post_it(bridge_channel, BRIDGE_CHANNEL_ACTION_RUN_APP, app_data, len_data);
1183 }
1184
1185 int ast_bridge_channel_write_app(struct ast_bridge_channel *bridge_channel, const char *app_name, const char *app_args, const char *moh_class)
1186 {
1187         return payload_helper_app(bridge_channel_write_action_data,
1188                 bridge_channel, app_name, app_args, moh_class);
1189 }
1190
1191 int ast_bridge_channel_queue_app(struct ast_bridge_channel *bridge_channel, const char *app_name, const char *app_args, const char *moh_class)
1192 {
1193         return payload_helper_app(bridge_channel_queue_action_data,
1194                 bridge_channel, app_name, app_args, moh_class);
1195 }
1196
1197 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)
1198 {
1199         if (moh_class) {
1200                 ast_bridge_channel_write_hold(bridge_channel, moh_class);
1201         }
1202         if (custom_play) {
1203                 custom_play(bridge_channel, playfile);
1204         } else {
1205                 ast_stream_and_wait(bridge_channel->chan, playfile, AST_DIGIT_NONE);
1206         }
1207         if (moh_class) {
1208                 ast_bridge_channel_write_unhold(bridge_channel);
1209         }
1210
1211         /*
1212          * It may be necessary to resume music on hold after we finish
1213          * playing the announcment.
1214          */
1215         if (ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_MOH)) {
1216                 const char *latest_musicclass;
1217
1218                 ast_channel_lock(bridge_channel->chan);
1219                 latest_musicclass = ast_strdupa(ast_channel_latest_musicclass(bridge_channel->chan));
1220                 ast_channel_unlock(bridge_channel->chan);
1221                 ast_moh_start(bridge_channel->chan, latest_musicclass, NULL);
1222         }
1223 }
1224
1225 struct bridge_playfile {
1226         /*! Call this function to play the playfile. (NULL if normal sound file to play) */
1227         ast_bridge_custom_play_fn custom_play;
1228         /*! Offset into playfile[] where the MOH class name starts.  (zero if no MOH)*/
1229         int moh_offset;
1230         /*! Filename to play. */
1231         char playfile[0];
1232 };
1233
1234 /*!
1235  * \internal
1236  * \brief Handle the playfile bridge action.
1237  * \since 12.0.0
1238  *
1239  * \param bridge_channel Which channel to play a file on.
1240  * \param payload Action frame payload to play a file.
1241  *
1242  * \return Nothing
1243  */
1244 static void bridge_channel_playfile(struct ast_bridge_channel *bridge_channel, struct bridge_playfile *payload)
1245 {
1246         ast_bridge_channel_playfile(bridge_channel, payload->custom_play, payload->playfile,
1247                 payload->moh_offset ? &payload->playfile[payload->moh_offset] : NULL);
1248 }
1249
1250 /*!
1251  * \internal
1252  * \brief Marshal a file to be played on a bridge_channel
1253  */
1254 static int payload_helper_playfile(ast_bridge_channel_post_action_data post_it,
1255         struct ast_bridge_channel *bridge_channel, ast_bridge_custom_play_fn custom_play, const char *playfile, const char *moh_class)
1256 {
1257         struct bridge_playfile *payload;
1258         size_t len_name = strlen(playfile) + 1;
1259         size_t len_moh = !moh_class ? 0 : strlen(moh_class) + 1;
1260         size_t len_payload = sizeof(*payload) + len_name + len_moh;
1261
1262         /* Fill in play file frame data. */
1263         payload = ast_alloca(len_payload);
1264         payload->custom_play = custom_play;
1265         payload->moh_offset = len_moh ? len_name : 0;
1266         strcpy(payload->playfile, playfile);/* Safe */
1267         if (moh_class) {
1268                 strcpy(&payload->playfile[payload->moh_offset], moh_class);/* Safe */
1269         }
1270
1271         return post_it(bridge_channel, BRIDGE_CHANNEL_ACTION_PLAY_FILE, payload, len_payload);
1272 }
1273
1274 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)
1275 {
1276         return payload_helper_playfile(bridge_channel_write_action_data,
1277                 bridge_channel, custom_play, playfile, moh_class);
1278 }
1279
1280 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)
1281 {
1282         return payload_helper_playfile(bridge_channel_queue_action_data,
1283                 bridge_channel, custom_play, playfile, moh_class);
1284 }
1285
1286 int ast_bridge_channel_queue_playfile_sync(struct ast_bridge_channel *bridge_channel,
1287                 ast_bridge_custom_play_fn custom_play, const char *playfile, const char *moh_class)
1288 {
1289         return payload_helper_playfile(bridge_channel_queue_action_data_sync,
1290                 bridge_channel, custom_play, playfile, moh_class);
1291 }
1292
1293 struct bridge_custom_callback {
1294         /*! Call this function on the bridge channel thread. */
1295         ast_bridge_custom_callback_fn callback;
1296         /*! Size of the payload if it exists.  A number otherwise. */
1297         size_t payload_size;
1298         /*! Option flags determining how callback is called. */
1299         unsigned int flags;
1300         /*! Nonzero if the payload exists. */
1301         char payload_exists;
1302         /*! Payload to give to callback. */
1303         char payload[0];
1304 };
1305
1306 /*!
1307  * \internal
1308  * \brief Handle the do custom callback bridge action.
1309  * \since 12.0.0
1310  *
1311  * \param bridge_channel Which channel to call the callback on.
1312  * \param data Action frame data to call the callback.
1313  *
1314  * \return Nothing
1315  */
1316 static void bridge_channel_do_callback(struct ast_bridge_channel *bridge_channel, struct bridge_custom_callback *data)
1317 {
1318         if (ast_test_flag(data, AST_BRIDGE_CHANNEL_CB_OPTION_MEDIA)) {
1319                 bridge_channel_suspend(bridge_channel);
1320                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1321         }
1322         data->callback(bridge_channel, data->payload_exists ? data->payload : NULL, data->payload_size);
1323         if (ast_test_flag(data, AST_BRIDGE_CHANNEL_CB_OPTION_MEDIA)) {
1324                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1325                 bridge_channel_unsuspend(bridge_channel);
1326         }
1327 }
1328
1329 /*!
1330  * \internal
1331  * \brief Marshal a custom callback function to be called on a bridge_channel
1332  */
1333 static int payload_helper_cb(ast_bridge_channel_post_action_data post_it,
1334         struct ast_bridge_channel *bridge_channel,
1335         enum ast_bridge_channel_custom_callback_option flags,
1336         ast_bridge_custom_callback_fn callback, const void *payload, size_t payload_size)
1337 {
1338         struct bridge_custom_callback *cb_data;
1339         size_t len_data = sizeof(*cb_data) + (payload ? payload_size : 0);
1340
1341         /* Sanity check. */
1342         if (!callback) {
1343                 ast_assert(0);
1344                 return -1;
1345         }
1346
1347         /* Fill in custom callback frame data. */
1348         cb_data = alloca(len_data);
1349         cb_data->callback = callback;
1350         cb_data->payload_size = payload_size;
1351         cb_data->flags = flags;
1352         cb_data->payload_exists = payload && payload_size;
1353         if (cb_data->payload_exists) {
1354                 memcpy(cb_data->payload, payload, payload_size);/* Safe */
1355         }
1356
1357         return post_it(bridge_channel, BRIDGE_CHANNEL_ACTION_CALLBACK, cb_data, len_data);
1358 }
1359
1360 int ast_bridge_channel_write_callback(struct ast_bridge_channel *bridge_channel,
1361         enum ast_bridge_channel_custom_callback_option flags,
1362         ast_bridge_custom_callback_fn callback, const void *payload, size_t payload_size)
1363 {
1364         return payload_helper_cb(bridge_channel_write_action_data,
1365                 bridge_channel, flags, callback, payload, payload_size);
1366 }
1367
1368 int ast_bridge_channel_queue_callback(struct ast_bridge_channel *bridge_channel,
1369         enum ast_bridge_channel_custom_callback_option flags,
1370         ast_bridge_custom_callback_fn callback, const void *payload, size_t payload_size)
1371 {
1372         return payload_helper_cb(bridge_channel_queue_action_data,
1373                 bridge_channel, flags, callback, payload, payload_size);
1374 }
1375
1376 struct bridge_park {
1377         int parker_uuid_offset;
1378         int app_data_offset;
1379         /* buffer used for holding those strings */
1380         char parkee_uuid[0];
1381 };
1382
1383 /*!
1384  * \internal
1385  * \brief Park a bridge_cahnnel
1386  */
1387 static void bridge_channel_park(struct ast_bridge_channel *bridge_channel, struct bridge_park *payload)
1388 {
1389         if (!ast_parking_provider_registered()) {
1390                 ast_log(AST_LOG_WARNING, "Unable to park %s: No parking provider loaded!\n",
1391                         ast_channel_name(bridge_channel->chan));
1392                 return;
1393         }
1394
1395         if (ast_parking_park_bridge_channel(bridge_channel, payload->parkee_uuid,
1396                 &payload->parkee_uuid[payload->parker_uuid_offset],
1397                 payload->app_data_offset ? &payload->parkee_uuid[payload->app_data_offset] : NULL)) {
1398                 ast_log(AST_LOG_WARNING, "Error occurred while parking %s\n",
1399                         ast_channel_name(bridge_channel->chan));
1400         }
1401 }
1402
1403 /*!
1404  * \internal
1405  * \brief Marshal a park action onto a bridge_channel
1406  */
1407 static int payload_helper_park(ast_bridge_channel_post_action_data post_it,
1408         struct ast_bridge_channel *bridge_channel,
1409         const char *parkee_uuid,
1410         const char *parker_uuid,
1411         const char *app_data)
1412 {
1413         struct bridge_park *payload;
1414         size_t len_parkee_uuid = strlen(parkee_uuid) + 1;
1415         size_t len_parker_uuid = strlen(parker_uuid) + 1;
1416         size_t len_app_data = !app_data ? 0 : strlen(app_data) + 1;
1417         size_t len_payload = sizeof(*payload) + len_parker_uuid + len_parkee_uuid + len_app_data;
1418
1419         payload = alloca(len_payload);
1420         payload->app_data_offset = len_app_data ? len_parkee_uuid + len_parker_uuid : 0;
1421         payload->parker_uuid_offset = len_parkee_uuid;
1422         strcpy(payload->parkee_uuid, parkee_uuid);
1423         strcpy(&payload->parkee_uuid[payload->parker_uuid_offset], parker_uuid);
1424         if (app_data) {
1425                 strcpy(&payload->parkee_uuid[payload->app_data_offset], app_data);
1426         }
1427
1428         return post_it(bridge_channel, BRIDGE_CHANNEL_ACTION_PARK, payload, len_payload);
1429 }
1430
1431 int ast_bridge_channel_write_park(struct ast_bridge_channel *bridge_channel, const char *parkee_uuid, const char *parker_uuid, const char *app_data)
1432 {
1433         return payload_helper_park(bridge_channel_write_action_data,
1434                 bridge_channel, parkee_uuid, parker_uuid, app_data);
1435 }
1436
1437 /*!
1438  * \internal
1439  * \brief Handle bridge channel interval expiration.
1440  * \since 12.0.0
1441  *
1442  * \param bridge_channel Channel to run expired intervals on.
1443  *
1444  * \return Nothing
1445  */
1446 static void bridge_channel_handle_interval(struct ast_bridge_channel *bridge_channel)
1447 {
1448         struct ast_heap *interval_hooks;
1449         struct ast_bridge_hook_timer *hook;
1450         struct timeval start;
1451         int chan_suspended = 0;
1452
1453         interval_hooks = bridge_channel->features->interval_hooks;
1454         ast_heap_wrlock(interval_hooks);
1455         start = ast_tvnow();
1456         while ((hook = ast_heap_peek(interval_hooks, 1))) {
1457                 int interval;
1458                 unsigned int execution_time;
1459
1460                 if (ast_tvdiff_ms(hook->timer.trip_time, start) > 0) {
1461                         ast_debug(1, "Hook %p on %p(%s) wants to happen in the future, stopping our traversal\n",
1462                                 hook, bridge_channel, ast_channel_name(bridge_channel->chan));
1463                         break;
1464                 }
1465                 ao2_ref(hook, +1);
1466                 ast_heap_unlock(interval_hooks);
1467
1468                 if (!chan_suspended
1469                         && ast_test_flag(&hook->timer, AST_BRIDGE_HOOK_TIMER_OPTION_MEDIA)) {
1470                         chan_suspended = 1;
1471                         bridge_channel_suspend(bridge_channel);
1472                         ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1473                 }
1474
1475                 ast_debug(1, "Executing hook %p on %p(%s)\n",
1476                         hook, bridge_channel, ast_channel_name(bridge_channel->chan));
1477                 interval = hook->generic.callback(bridge_channel, hook->generic.hook_pvt);
1478
1479                 ast_heap_wrlock(interval_hooks);
1480                 if (ast_heap_peek(interval_hooks, hook->timer.heap_index) != hook
1481                         || !ast_heap_remove(interval_hooks, hook)) {
1482                         /* Interval hook is already removed from the bridge_channel. */
1483                         ao2_ref(hook, -1);
1484                         continue;
1485                 }
1486                 ao2_ref(hook, -1);
1487
1488                 if (interval < 0) {
1489                         ast_debug(1, "Removed interval hook %p from %p(%s)\n",
1490                                 hook, bridge_channel, ast_channel_name(bridge_channel->chan));
1491                         ao2_ref(hook, -1);
1492                         continue;
1493                 }
1494                 if (interval) {
1495                         /* Set new interval for the hook. */
1496                         hook->timer.interval = interval;
1497                 }
1498
1499                 ast_debug(1, "Updating interval hook %p with interval %u on %p(%s)\n",
1500                         hook, hook->timer.interval, bridge_channel,
1501                         ast_channel_name(bridge_channel->chan));
1502
1503                 /* resetting start */
1504                 start = ast_tvnow();
1505
1506                 /*
1507                  * Resetup the interval hook for the next interval.  We may need
1508                  * to skip over any missed intervals because the hook was
1509                  * delayed or took too long.
1510                  */
1511                 execution_time = ast_tvdiff_ms(start, hook->timer.trip_time);
1512                 while (hook->timer.interval < execution_time) {
1513                         execution_time -= hook->timer.interval;
1514                 }
1515                 hook->timer.trip_time = ast_tvadd(start, ast_samp2tv(hook->timer.interval - execution_time, 1000));
1516                 hook->timer.seqno = ast_atomic_fetchadd_int((int *) &bridge_channel->features->interval_sequence, +1);
1517
1518                 if (ast_heap_push(interval_hooks, hook)) {
1519                         /* Could not push the hook back onto the heap. */
1520                         ao2_ref(hook, -1);
1521                 }
1522         }
1523         ast_heap_unlock(interval_hooks);
1524
1525         if (chan_suspended) {
1526                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1527                 bridge_channel_unsuspend(bridge_channel);
1528         }
1529 }
1530
1531 /*!
1532  * \internal
1533  * \brief Write a DTMF stream out to a channel
1534  */
1535 static int bridge_channel_write_dtmf_stream(struct ast_bridge_channel *bridge_channel, const char *dtmf)
1536 {
1537         return bridge_channel_write_action_data(bridge_channel,
1538                 BRIDGE_CHANNEL_ACTION_DTMF_STREAM, dtmf, strlen(dtmf) + 1);
1539 }
1540
1541 /*!
1542  * \internal
1543  * \brief Indicate to the testsuite a feature was successfully detected.
1544  *
1545  * Currently, this function only will relay built-in features to the testsuite,
1546  * but it could be modified to detect applicationmap items should the need arise.
1547  *
1548  * \param chan The channel that activated the feature
1549  * \param dtmf The DTMF sequence entered to activate the feature
1550  */
1551 static void testsuite_notify_feature_success(struct ast_channel *chan, const char *dtmf)
1552 {
1553 #ifdef TEST_FRAMEWORK
1554         char *feature = "unknown";
1555         struct ast_featuremap_config *featuremap;
1556         struct ast_features_xfer_config *xfer;
1557
1558         ast_channel_lock(chan);
1559         featuremap = ast_get_chan_featuremap_config(chan);
1560         xfer = ast_get_chan_features_xfer_config(chan);
1561         ast_channel_unlock(chan);
1562
1563         if (featuremap) {
1564                 if (!strcmp(dtmf, featuremap->blindxfer)) {
1565                         feature = "blindxfer";
1566                 } else if (!strcmp(dtmf, featuremap->atxfer)) {
1567                         feature = "atxfer";
1568                 } else if (!strcmp(dtmf, featuremap->disconnect)) {
1569                         feature = "disconnect";
1570                 } else if (!strcmp(dtmf, featuremap->automon)) {
1571                         feature = "automon";
1572                 } else if (!strcmp(dtmf, featuremap->automixmon)) {
1573                         feature = "automixmon";
1574                 } else if (!strcmp(dtmf, featuremap->parkcall)) {
1575                         feature = "parkcall";
1576                 }
1577         }
1578         if (xfer) {
1579                 if (!strcmp(dtmf, xfer->atxferthreeway)) {
1580                         feature = "atxferthreeway";
1581                 }
1582         }
1583
1584         ao2_cleanup(featuremap);
1585         ao2_cleanup(xfer);
1586
1587         ast_test_suite_event_notify("FEATURE_DETECTION",
1588                         "Result: success\r\n"
1589                         "Feature: %s", feature);
1590 #endif /* TEST_FRAMEWORK */
1591 }
1592
1593 static int bridge_channel_feature_digit_add(
1594         struct ast_bridge_channel *bridge_channel, int digit, size_t dtmf_len)
1595 {
1596         if (dtmf_len < ARRAY_LEN(bridge_channel->dtmf_hook_state.collected) - 1) {
1597                 /* Add the new digit to the DTMF string so we can do our matching */
1598                 bridge_channel->dtmf_hook_state.collected[dtmf_len++] = digit;
1599                 bridge_channel->dtmf_hook_state.collected[dtmf_len] = '\0';
1600
1601                 ast_debug(1, "DTMF feature string on %p(%s) is now '%s'\n",
1602                           bridge_channel, ast_channel_name(bridge_channel->chan),
1603                           bridge_channel->dtmf_hook_state.collected);
1604         }
1605
1606         return dtmf_len;
1607 }
1608
1609 static unsigned int bridge_channel_feature_digit_timeout(struct ast_bridge_channel *bridge_channel)
1610 {
1611         unsigned int digit_timeout;
1612         struct ast_features_general_config *gen_cfg;
1613
1614         /* Determine interdigit timeout */
1615         ast_channel_lock(bridge_channel->chan);
1616         gen_cfg = ast_get_chan_features_general_config(bridge_channel->chan);
1617         ast_channel_unlock(bridge_channel->chan);
1618
1619         if (!gen_cfg) {
1620                 ast_log(LOG_ERROR, "Unable to retrieve features configuration.\n");
1621                 return 3000; /* Pick a reasonable failsafe timeout in ms */
1622         }
1623
1624         digit_timeout = gen_cfg->featuredigittimeout;
1625         ao2_ref(gen_cfg, -1);
1626
1627         return digit_timeout;
1628 }
1629
1630 void ast_bridge_channel_feature_digit_add(struct ast_bridge_channel *bridge_channel, int digit)
1631 {
1632         if (digit) {
1633                 bridge_channel_feature_digit_add(
1634                         bridge_channel, digit, strlen(bridge_channel->dtmf_hook_state.collected));
1635         }
1636 }
1637
1638 void ast_bridge_channel_feature_digit(struct ast_bridge_channel *bridge_channel, int digit)
1639 {
1640         struct ast_bridge_features *features = bridge_channel->features;
1641         struct ast_bridge_hook_dtmf *hook = NULL;
1642         size_t dtmf_len;
1643
1644         struct sanity_check_of_dtmf_size {
1645                 char check[1 / (ARRAY_LEN(bridge_channel->dtmf_hook_state.collected) == ARRAY_LEN(hook->dtmf.code))];
1646         };
1647
1648         dtmf_len = strlen(bridge_channel->dtmf_hook_state.collected);
1649         if (!dtmf_len && !digit) {
1650                 /* Nothing to do */
1651                 return;
1652         }
1653
1654         if (digit) {
1655                 dtmf_len = bridge_channel_feature_digit_add(bridge_channel, digit, dtmf_len);
1656         }
1657
1658         while (digit) {
1659                 /* See if a DTMF feature hook matches or can match */
1660                 hook = ao2_find(features->dtmf_hooks, bridge_channel->dtmf_hook_state.collected,
1661                         OBJ_SEARCH_PARTIAL_KEY);
1662                 if (!hook) {
1663                         ast_debug(1, "No DTMF feature hooks on %p(%s) match '%s'\n",
1664                                 bridge_channel, ast_channel_name(bridge_channel->chan),
1665                                 bridge_channel->dtmf_hook_state.collected);
1666                         break;
1667                 } else if (dtmf_len != strlen(hook->dtmf.code)) {
1668                         unsigned int digit_timeout;
1669                         /* Need more digits to match */
1670                         ao2_ref(hook, -1);
1671                         digit_timeout = bridge_channel_feature_digit_timeout(bridge_channel);
1672                         bridge_channel->dtmf_hook_state.interdigit_timeout =
1673                                 ast_tvadd(ast_tvnow(), ast_samp2tv(digit_timeout, 1000));
1674                         return;
1675                 } else {
1676                         int remove_me;
1677                         int already_suspended;
1678
1679                         ast_debug(1, "DTMF feature hook %p matched DTMF string '%s' on %p(%s)\n",
1680                                 hook, bridge_channel->dtmf_hook_state.collected, bridge_channel,
1681                                 ast_channel_name(bridge_channel->chan));
1682
1683                         /*
1684                          * Clear the collected digits before executing the hook
1685                          * in case the hook starts another sequence.
1686                          */
1687                         bridge_channel->dtmf_hook_state.collected[0] = '\0';
1688
1689                         ast_bridge_channel_lock_bridge(bridge_channel);
1690                         already_suspended = bridge_channel->suspended;
1691                         if (!already_suspended) {
1692                                 bridge_channel_internal_suspend_nolock(bridge_channel);
1693                         }
1694                         ast_bridge_unlock(bridge_channel->bridge);
1695                         ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1696
1697                         /* Execute the matched hook on this channel. */
1698                         remove_me = hook->generic.callback(bridge_channel, hook->generic.hook_pvt);
1699                         if (remove_me) {
1700                                 ast_debug(1, "DTMF hook %p is being removed from %p(%s)\n",
1701                                         hook, bridge_channel, ast_channel_name(bridge_channel->chan));
1702                                 ao2_unlink(features->dtmf_hooks, hook);
1703                         }
1704                         testsuite_notify_feature_success(bridge_channel->chan, hook->dtmf.code);
1705                         ao2_ref(hook, -1);
1706
1707                         ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1708                         if (!already_suspended) {
1709                                 bridge_channel_unsuspend(bridge_channel);
1710                         }
1711
1712                         /*
1713                          * If we are handing the channel off to an external hook for
1714                          * ownership, we are not guaranteed what kind of state it will
1715                          * come back in.  If the channel hungup, we need to detect that
1716                          * here if the hook did not already change the state.
1717                          */
1718                         if (bridge_channel->chan && ast_check_hangup_locked(bridge_channel->chan)) {
1719                                 ast_bridge_channel_kick(bridge_channel, 0);
1720                                 bridge_channel->dtmf_hook_state.collected[0] = '\0';
1721                                 return;
1722                         }
1723
1724                         /* if there is dtmf that has been collected then loop back through,
1725                            but set digit to -1 so it doesn't try to do an add since the dtmf
1726                            is already in the buffer */
1727                         dtmf_len = strlen(bridge_channel->dtmf_hook_state.collected);
1728                         if (!dtmf_len) {
1729                                 return;
1730                         }
1731                 }
1732         }
1733
1734         if (!digit) {
1735                 ast_debug(1, "DTMF feature string collection on %p(%s) timed out\n",
1736                         bridge_channel, ast_channel_name(bridge_channel->chan));
1737         }
1738
1739         /* Timeout or DTMF digit didn't allow a match with any hooks. */
1740         if (features->dtmf_passthrough) {
1741                 /* Stream the collected DTMF to the other channels. */
1742                 bridge_channel_write_dtmf_stream(bridge_channel,
1743                         bridge_channel->dtmf_hook_state.collected);
1744         }
1745         bridge_channel->dtmf_hook_state.collected[0] = '\0';
1746
1747         ast_test_suite_event_notify("FEATURE_DETECTION", "Result: fail");
1748 }
1749
1750 /*!
1751  * \internal
1752  * \brief Handle bridge channel DTMF feature timeout expiration.
1753  * \since 12.8.0
1754  *
1755  * \param bridge_channel Channel to check expired interdigit timer on.
1756  *
1757  * \return Nothing
1758  */
1759 static void bridge_channel_handle_feature_timeout(struct ast_bridge_channel *bridge_channel)
1760 {
1761         if (!bridge_channel->dtmf_hook_state.collected[0]
1762                 || 0 < ast_tvdiff_ms(bridge_channel->dtmf_hook_state.interdigit_timeout,
1763                         ast_tvnow())) {
1764                 /* Not within a sequence or not timed out. */
1765                 return;
1766         }
1767
1768         ast_bridge_channel_feature_digit(bridge_channel, 0);
1769 }
1770
1771 /*!
1772  * \internal
1773  * \brief Indicate that a bridge_channel is talking
1774  */
1775 static void bridge_channel_talking(struct ast_bridge_channel *bridge_channel, int talking)
1776 {
1777         struct ast_bridge_features *features = bridge_channel->features;
1778         struct ast_bridge_hook *hook;
1779         struct ao2_iterator iter;
1780
1781         /* Run any talk detection hooks. */
1782         iter = ao2_iterator_init(features->other_hooks, 0);
1783         for (; (hook = ao2_iterator_next(&iter)); ao2_ref(hook, -1)) {
1784                 int remove_me;
1785                 ast_bridge_talking_indicate_callback talk_cb;
1786
1787                 if (hook->type != AST_BRIDGE_HOOK_TYPE_TALK) {
1788                         continue;
1789                 }
1790                 talk_cb = (ast_bridge_talking_indicate_callback) hook->callback;
1791                 remove_me = talk_cb(bridge_channel, hook->hook_pvt, talking);
1792                 if (remove_me) {
1793                         ast_debug(1, "Talk detection hook %p is being removed from %p(%s)\n",
1794                                 hook, bridge_channel, ast_channel_name(bridge_channel->chan));
1795                         ao2_unlink(features->other_hooks, hook);
1796                 }
1797         }
1798         ao2_iterator_destroy(&iter);
1799 }
1800
1801 /*! \brief Internal function that plays back DTMF on a bridge channel */
1802 static void bridge_channel_dtmf_stream(struct ast_bridge_channel *bridge_channel, const char *dtmf)
1803 {
1804         ast_debug(1, "Playing DTMF stream '%s' out to %p(%s)\n",
1805                 dtmf, bridge_channel, ast_channel_name(bridge_channel->chan));
1806         ast_dtmf_stream(bridge_channel->chan, NULL, dtmf, 0, 0);
1807 }
1808
1809 /*! \brief Data specifying where a blind transfer is going to */
1810 struct blind_transfer_data {
1811         char exten[AST_MAX_EXTENSION];
1812         char context[AST_MAX_CONTEXT];
1813 };
1814
1815 /*!
1816  * \internal
1817  * \brief Execute after bridge actions on a channel when it leaves a bridge
1818  */
1819 static void after_bridge_move_channel(struct ast_channel *chan_bridged, void *data)
1820 {
1821         RAII_VAR(struct ast_channel *, chan_target, data, ao2_cleanup);
1822         struct ast_party_connected_line connected_target;
1823         unsigned char connected_line_data[1024];
1824         int payload_size;
1825
1826         ast_party_connected_line_init(&connected_target);
1827
1828         ast_channel_lock(chan_target);
1829         ast_party_connected_line_copy(&connected_target, ast_channel_connected(chan_target));
1830         ast_channel_unlock(chan_target);
1831         ast_party_id_reset(&connected_target.priv);
1832
1833         if (ast_channel_move(chan_target, chan_bridged)) {
1834                 ast_softhangup(chan_target, AST_SOFTHANGUP_DEV);
1835                 ast_party_connected_line_free(&connected_target);
1836                 return;
1837         }
1838
1839         /* The ast_channel_move function will end up updating the connected line information
1840          * on chan_target to the value we have here, but will not inform it. To ensure that
1841          * AST_FRAME_READ_ACTION_CONNECTED_LINE_MACRO is executed we wipe it away here. If
1842          * we don't do this then the change will be considered redundant, since the connected
1843          * line information is already there (despite the channel not being told).
1844          */
1845         ast_channel_lock(chan_target);
1846         ast_party_connected_line_free(ast_channel_connected_indicated(chan_target));
1847         ast_party_connected_line_init(ast_channel_connected_indicated(chan_target));
1848         ast_channel_unlock(chan_target);
1849
1850         if ((payload_size = ast_connected_line_build_data(connected_line_data,
1851                 sizeof(connected_line_data), &connected_target, NULL)) != -1) {
1852                 struct ast_control_read_action_payload *frame_payload;
1853                 int frame_size;
1854
1855                 frame_size = payload_size + sizeof(*frame_payload);
1856                 frame_payload = ast_alloca(frame_size);
1857                 frame_payload->action = AST_FRAME_READ_ACTION_CONNECTED_LINE_MACRO;
1858                 frame_payload->payload_size = payload_size;
1859                 memcpy(frame_payload->payload, connected_line_data, payload_size);
1860                 ast_queue_control_data(chan_target, AST_CONTROL_READ_ACTION, frame_payload, frame_size);
1861         }
1862
1863         /* A connected line update is queued so that if chan_target is remotely involved with
1864          * anything (such as dialing a channel) the other channel(s) will be informed of the
1865          * new channel they are involved with.
1866          */
1867         ast_channel_lock(chan_target);
1868         ast_connected_line_copy_from_caller(&connected_target, ast_channel_caller(chan_target));
1869         ast_channel_queue_connected_line_update(chan_target, &connected_target, NULL);
1870         ast_channel_unlock(chan_target);
1871
1872         ast_party_connected_line_free(&connected_target);
1873 }
1874
1875 /*!
1876  * \internal
1877  * \brief Execute logic to cleanup when after bridge fails
1878  */
1879 static void after_bridge_move_channel_fail(enum ast_bridge_after_cb_reason reason, void *data)
1880 {
1881         RAII_VAR(struct ast_channel *, chan_target, data, ao2_cleanup);
1882
1883         ast_log(LOG_WARNING, "Unable to complete transfer: %s\n",
1884                 ast_bridge_after_cb_reason_string(reason));
1885         ast_softhangup(chan_target, AST_SOFTHANGUP_DEV);
1886 }
1887
1888 /*!
1889  * \internal
1890  * \brief Perform a blind transfer on a channel in a bridge
1891  */
1892 static void bridge_channel_blind_transfer(struct ast_bridge_channel *bridge_channel,
1893                 struct blind_transfer_data *blind_data)
1894 {
1895         ast_async_goto(bridge_channel->chan, blind_data->context, blind_data->exten, 1);
1896         ast_bridge_channel_kick(bridge_channel, AST_CAUSE_NORMAL_CLEARING);
1897 }
1898
1899 /*!
1900  * \internal
1901  * \brief Perform an attended transfer on a channel in a bridge
1902  */
1903 static void bridge_channel_attended_transfer(struct ast_bridge_channel *bridge_channel,
1904                 const char *target_chan_name)
1905 {
1906         RAII_VAR(struct ast_channel *, chan_target, NULL, ao2_cleanup);
1907         RAII_VAR(struct ast_channel *, chan_bridged, NULL, ao2_cleanup);
1908
1909         chan_target = ast_channel_get_by_name(target_chan_name);
1910         if (!chan_target) {
1911                 /* Dang, it disappeared somehow */
1912                 ast_bridge_channel_kick(bridge_channel, AST_CAUSE_NORMAL_CLEARING);
1913                 return;
1914         }
1915
1916         ast_bridge_channel_lock(bridge_channel);
1917         chan_bridged = bridge_channel->chan;
1918         ast_assert(chan_bridged != NULL);
1919         ao2_ref(chan_bridged, +1);
1920         ast_bridge_channel_unlock(bridge_channel);
1921
1922         if (ast_bridge_set_after_callback(chan_bridged, after_bridge_move_channel,
1923                 after_bridge_move_channel_fail, ast_channel_ref(chan_target))) {
1924                 ast_softhangup(chan_target, AST_SOFTHANGUP_DEV);
1925
1926                 /* Release the ref we tried to pass to ast_bridge_set_after_callback(). */
1927                 ast_channel_unref(chan_target);
1928         }
1929         ast_bridge_channel_kick(bridge_channel, AST_CAUSE_NORMAL_CLEARING);
1930 }
1931
1932 /*!
1933  * \internal
1934  * \brief Handle bridge channel bridge action frame.
1935  * \since 12.0.0
1936  *
1937  * \param bridge_channel Channel to execute the action on.
1938  * \param action What to do.
1939  * \param data data from the action.
1940  *
1941  * \return Nothing
1942  */
1943 static void bridge_channel_handle_action(struct ast_bridge_channel *bridge_channel,
1944         enum bridge_channel_action_type action, void *data)
1945 {
1946         switch (action) {
1947         case BRIDGE_CHANNEL_ACTION_DTMF_STREAM:
1948                 bridge_channel_suspend(bridge_channel);
1949                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1950                 bridge_channel_dtmf_stream(bridge_channel, data);
1951                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1952                 bridge_channel_unsuspend(bridge_channel);
1953                 break;
1954         case BRIDGE_CHANNEL_ACTION_TALKING_START:
1955         case BRIDGE_CHANNEL_ACTION_TALKING_STOP:
1956                 bridge_channel_talking(bridge_channel,
1957                         action == BRIDGE_CHANNEL_ACTION_TALKING_START);
1958                 break;
1959         case BRIDGE_CHANNEL_ACTION_PLAY_FILE:
1960                 bridge_channel_suspend(bridge_channel);
1961                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1962                 bridge_channel_playfile(bridge_channel, data);
1963                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1964                 bridge_channel_unsuspend(bridge_channel);
1965                 break;
1966         case BRIDGE_CHANNEL_ACTION_RUN_APP:
1967                 bridge_channel_suspend(bridge_channel);
1968                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1969                 bridge_channel_run_app(bridge_channel, data);
1970                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1971                 bridge_channel_unsuspend(bridge_channel);
1972                 break;
1973         case BRIDGE_CHANNEL_ACTION_CALLBACK:
1974                 bridge_channel_do_callback(bridge_channel, data);
1975                 break;
1976         case BRIDGE_CHANNEL_ACTION_PARK:
1977                 bridge_channel_suspend(bridge_channel);
1978                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1979                 bridge_channel_park(bridge_channel, data);
1980                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1981                 bridge_channel_unsuspend(bridge_channel);
1982                 break;
1983         case BRIDGE_CHANNEL_ACTION_BLIND_TRANSFER:
1984                 bridge_channel_blind_transfer(bridge_channel, data);
1985                 break;
1986         case BRIDGE_CHANNEL_ACTION_ATTENDED_TRANSFER:
1987                 bridge_channel_attended_transfer(bridge_channel, data);
1988                 break;
1989         default:
1990                 break;
1991         }
1992
1993         /* While invoking an action it is possible for the channel to be hung up. So
1994          * that the bridge respects this we check here and if hung up kick it out.
1995          */
1996         if (bridge_channel->chan && ast_check_hangup_locked(bridge_channel->chan)) {
1997                 ast_bridge_channel_kick(bridge_channel, 0);
1998         }
1999 }
2000
2001 /*!
2002  * \internal
2003  * \brief Check if a bridge should dissolve and do it.
2004  * \since 12.0.0
2005  *
2006  * \param bridge_channel Channel causing the check.
2007  *
2008  * \note On entry, bridge_channel->bridge is already locked.
2009  *
2010  * \return Nothing
2011  */
2012 static void bridge_channel_dissolve_check(struct ast_bridge_channel *bridge_channel)
2013 {
2014         struct ast_bridge *bridge = bridge_channel->bridge;
2015
2016         if (bridge->dissolved) {
2017                 return;
2018         }
2019
2020         if (!bridge->num_channels
2021                 && ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_DISSOLVE_EMPTY)) {
2022                 /* Last channel leaving the bridge turns off the lights. */
2023                 bridge_dissolve(bridge, ast_channel_hangupcause(bridge_channel->chan));
2024                 return;
2025         }
2026
2027         switch (bridge_channel->state) {
2028         case BRIDGE_CHANNEL_STATE_END:
2029                 /* Do we need to dissolve the bridge because this channel hung up? */
2030                 if (ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_DISSOLVE_HANGUP)
2031                         || (bridge_channel->features->usable
2032                                 && ast_test_flag(&bridge_channel->features->feature_flags,
2033                                         AST_BRIDGE_CHANNEL_FLAG_DISSOLVE_HANGUP))) {
2034                         bridge_dissolve(bridge, ast_channel_hangupcause(bridge_channel->chan));
2035                         return;
2036                 }
2037                 break;
2038         default:
2039                 break;
2040         }
2041
2042         if (bridge->num_lonely && bridge->num_lonely == bridge->num_channels) {
2043                 /*
2044                  * This will start a chain reaction where each channel leaving
2045                  * enters this function and causes the next to leave as long as
2046                  * there aren't non-lonely channels in the bridge.
2047                  */
2048                 ast_bridge_channel_leave_bridge(AST_LIST_FIRST(&bridge->channels),
2049                         BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE,
2050                         ast_channel_hangupcause(bridge_channel->chan));
2051         }
2052 }
2053
2054 void bridge_channel_internal_pull(struct ast_bridge_channel *bridge_channel)
2055 {
2056         struct ast_bridge *bridge = bridge_channel->bridge;
2057
2058         if (!bridge_channel->in_bridge) {
2059                 return;
2060         }
2061         bridge_channel->in_bridge = 0;
2062
2063         ast_debug(1, "Bridge %s: pulling %p(%s)\n",
2064                 bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan));
2065
2066         ast_verb(3, "Channel %s left '%s' %s-bridge <%s>\n",
2067                 ast_channel_name(bridge_channel->chan),
2068                 bridge->technology->name,
2069                 bridge->v_table->name,
2070                 bridge->uniqueid);
2071
2072         if (!bridge_channel->just_joined) {
2073                 /* Tell the bridge technology we are leaving so they tear us down */
2074                 ast_debug(1, "Bridge %s: %p(%s) is leaving %s technology\n",
2075                         bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan),
2076                         bridge->technology->name);
2077                 if (bridge->technology->leave) {
2078                         bridge->technology->leave(bridge, bridge_channel);
2079                 }
2080         }
2081
2082         /* Remove channel from the bridge */
2083         if (!bridge_channel->suspended) {
2084                 --bridge->num_active;
2085         }
2086         if (ast_test_flag(&bridge_channel->features->feature_flags, AST_BRIDGE_CHANNEL_FLAG_LONELY)) {
2087                 --bridge->num_lonely;
2088         }
2089         --bridge->num_channels;
2090         AST_LIST_REMOVE(&bridge->channels, bridge_channel, entry);
2091
2092         bridge_channel_dissolve_check(bridge_channel);
2093         bridge->v_table->pull(bridge, bridge_channel);
2094
2095         ast_bridge_channel_clear_roles(bridge_channel);
2096
2097         /* If we are not going to be hung up after leaving a bridge, and we were an
2098          * outgoing channel, clear the outgoing flag.
2099          */
2100         if (ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_OUTGOING)
2101             && (ast_channel_is_leaving_bridge(bridge_channel->chan)
2102                 || bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT)) {
2103                 ast_debug(2, "Channel %s will survive this bridge; clearing outgoing (dialed) flag\n", ast_channel_name(bridge_channel->chan));
2104                 ast_channel_clear_flag(bridge_channel->chan, AST_FLAG_OUTGOING);
2105         }
2106
2107         bridge->reconfigured = 1;
2108         ast_bridge_publish_leave(bridge, bridge_channel->chan);
2109 }
2110
2111 int bridge_channel_internal_push_full(struct ast_bridge_channel *bridge_channel, int optimized)
2112 {
2113         struct ast_bridge *bridge = bridge_channel->bridge;
2114         struct ast_bridge_channel *swap;
2115
2116         ast_assert(!bridge_channel->in_bridge);
2117
2118         swap = bridge_find_channel(bridge, bridge_channel->swap);
2119         bridge_channel->swap = NULL;
2120
2121         if (swap) {
2122                 ast_debug(1, "Bridge %s: pushing %p(%s) by swapping with %p(%s)\n",
2123                         bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan),
2124                         swap, ast_channel_name(swap->chan));
2125         } else {
2126                 ast_debug(1, "Bridge %s: pushing %p(%s)\n",
2127                         bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan));
2128         }
2129
2130         /* Add channel to the bridge */
2131         if (bridge->dissolved
2132                 || bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT
2133                 || (swap && swap->state != BRIDGE_CHANNEL_STATE_WAIT)
2134                 || bridge->v_table->push(bridge, bridge_channel, swap)) {
2135                 ast_debug(1, "Bridge %s: pushing %p(%s) into bridge failed\n",
2136                         bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan));
2137                 return -1;
2138         }
2139
2140         ast_bridge_channel_establish_roles(bridge_channel);
2141
2142         if (swap) {
2143                 int dissolve = ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_DISSOLVE_EMPTY);
2144
2145                 /* This flag is cleared so the act of this channel leaving does not cause it to dissolve if need be */
2146                 ast_clear_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_DISSOLVE_EMPTY);
2147
2148                 if (optimized) {
2149                         bridge_channel_cancel_owed_events(swap);
2150                 }
2151                 ast_bridge_channel_leave_bridge(swap, BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, 0);
2152                 bridge_channel_internal_pull(swap);
2153
2154                 ast_set2_flag(&bridge->feature_flags, dissolve, AST_BRIDGE_FLAG_DISSOLVE_EMPTY);
2155         }
2156
2157         bridge_channel->in_bridge = 1;
2158         bridge_channel->just_joined = 1;
2159         AST_LIST_INSERT_TAIL(&bridge->channels, bridge_channel, entry);
2160         ++bridge->num_channels;
2161         if (ast_test_flag(&bridge_channel->features->feature_flags, AST_BRIDGE_CHANNEL_FLAG_LONELY)) {
2162                 ++bridge->num_lonely;
2163         }
2164         if (!bridge_channel->suspended) {
2165                 ++bridge->num_active;
2166         }
2167
2168         ast_verb(3, "Channel %s %s%s%s '%s' %s-bridge <%s>\n",
2169                 ast_channel_name(bridge_channel->chan),
2170                 swap ? "swapped with " : "joined",
2171                 swap ? ast_channel_name(swap->chan) : "",
2172                 swap ? " into" : "",
2173                 bridge->technology->name,
2174                 bridge->v_table->name,
2175                 bridge->uniqueid);
2176
2177         ast_bridge_publish_enter(bridge, bridge_channel->chan, swap ? swap->chan : NULL);
2178
2179         /* Clear any BLINDTRANSFER,ATTENDEDTRANSFER and FORWARDERNAME since the transfer has completed. */
2180         pbx_builtin_setvar_helper(bridge_channel->chan, "BLINDTRANSFER", NULL);
2181         pbx_builtin_setvar_helper(bridge_channel->chan, "ATTENDEDTRANSFER", NULL);
2182         pbx_builtin_setvar_helper(bridge_channel->chan, "FORWARDERNAME", NULL);
2183
2184         /* Wake up the bridge channel thread to reevaluate any interval timers. */
2185         ast_queue_frame(bridge_channel->chan, &ast_null_frame);
2186
2187         bridge->reconfigured = 1;
2188         return 0;
2189 }
2190
2191 int bridge_channel_internal_push(struct ast_bridge_channel *bridge_channel)
2192 {
2193         return bridge_channel_internal_push_full(bridge_channel, 0);
2194 }
2195
2196 /*!
2197  * \internal
2198  * \brief Handle bridge channel control frame action.
2199  * \since 12.0.0
2200  *
2201  * \param bridge_channel Channel to execute the control frame action on.
2202  * \param fr Control frame to handle.
2203  *
2204  * \return Nothing
2205  */
2206 static void bridge_channel_handle_control(struct ast_bridge_channel *bridge_channel, struct ast_frame *fr)
2207 {
2208         struct ast_channel *chan;
2209         struct ast_option_header *aoh;
2210         int is_caller;
2211
2212         chan = bridge_channel->chan;
2213         switch (fr->subclass.integer) {
2214         case AST_CONTROL_REDIRECTING:
2215                 is_caller = !ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING);
2216                 if (ast_channel_redirecting_sub(NULL, chan, fr, 1) &&
2217                         ast_channel_redirecting_macro(NULL, chan, fr, is_caller, 1)) {
2218                         ast_indicate_data(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
2219                 }
2220                 break;
2221         case AST_CONTROL_CONNECTED_LINE:
2222                 is_caller = !ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING);
2223                 if (ast_channel_connected_line_sub(NULL, chan, fr, 1) &&
2224                         ast_channel_connected_line_macro(NULL, chan, fr, is_caller, 1)) {
2225                         ast_indicate_data(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
2226                 }
2227                 break;
2228         case AST_CONTROL_OPTION:
2229                 /*
2230                  * Forward option Requests, but only ones we know are safe These
2231                  * are ONLY sent by chan_iax2 and I'm not convinced that they
2232                  * are useful. I haven't deleted them entirely because I just am
2233                  * not sure of the ramifications of removing them.
2234                  */
2235                 aoh = fr->data.ptr;
2236                 if (aoh && aoh->flag == AST_OPTION_FLAG_REQUEST) {
2237                         switch (ntohs(aoh->option)) {
2238                         case AST_OPTION_TONE_VERIFY:
2239                         case AST_OPTION_TDD:
2240                         case AST_OPTION_RELAXDTMF:
2241                         case AST_OPTION_AUDIO_MODE:
2242                         case AST_OPTION_DIGIT_DETECT:
2243                         case AST_OPTION_FAX_DETECT:
2244                                 ast_channel_setoption(chan, ntohs(aoh->option), aoh->data,
2245                                         fr->datalen - sizeof(*aoh), 0);
2246                                 break;
2247                         default:
2248                                 break;
2249                         }
2250                 }
2251                 break;
2252         case AST_CONTROL_ANSWER:
2253                 if (ast_channel_state(chan) != AST_STATE_UP) {
2254                         ast_answer(chan);
2255                 } else {
2256                         ast_indicate(chan, -1);
2257                 }
2258                 break;
2259         case AST_CONTROL_MASQUERADE_NOTIFY:
2260                 /* Should never happen. */
2261                 ast_assert(0);
2262                 break;
2263         case AST_CONTROL_STREAM_TOPOLOGY_CHANGED:
2264                 ast_indicate_data(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
2265                 break;
2266         default:
2267                 ast_indicate_data(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
2268                 break;
2269         }
2270 }
2271
2272 /*!
2273  * \internal
2274  * \brief Handle bridge channel write frame to channel.
2275  * \since 12.0.0
2276  *
2277  * \param bridge_channel Channel to write outgoing frame.
2278  *
2279  * \return Nothing
2280  */
2281 static void bridge_channel_handle_write(struct ast_bridge_channel *bridge_channel)
2282 {
2283         struct ast_frame *fr;
2284         struct sync_payload *sync_payload;
2285         int num;
2286
2287         ast_bridge_channel_lock(bridge_channel);
2288
2289         /* It's not good to have unbalanced frames and alert_pipe alerts. */
2290         ast_assert(!AST_LIST_EMPTY(&bridge_channel->wr_queue));
2291         if (AST_LIST_EMPTY(&bridge_channel->wr_queue)) {
2292                 /* No frame, flush the alert pipe of excess alerts. */
2293                 ast_log(LOG_WARNING, "Weird.  No frame from bridge for %s to process?\n",
2294                         ast_channel_name(bridge_channel->chan));
2295                 ast_alertpipe_read(bridge_channel->alert_pipe);
2296                 ast_bridge_channel_unlock(bridge_channel);
2297                 return;
2298         }
2299
2300         AST_LIST_TRAVERSE_SAFE_BEGIN(&bridge_channel->wr_queue, fr, frame_list) {
2301                 if (bridge_channel->dtmf_hook_state.collected[0]) {
2302                         switch (fr->frametype) {
2303                         case AST_FRAME_BRIDGE_ACTION:
2304                         case AST_FRAME_BRIDGE_ACTION_SYNC:
2305                                 /* Defer processing these frames while DTMF is collected. */
2306                                 continue;
2307                         default:
2308                                 break;
2309                         }
2310                 }
2311                 ast_alertpipe_read(bridge_channel->alert_pipe);
2312                 AST_LIST_REMOVE_CURRENT(frame_list);
2313                 break;
2314         }
2315         AST_LIST_TRAVERSE_SAFE_END;
2316
2317         ast_bridge_channel_unlock(bridge_channel);
2318         if (!fr) {
2319                 /*
2320                  * Wait some to reduce CPU usage from a tight loop
2321                  * without any wait because we only have deferred
2322                  * frames in the wr_queue.
2323                  */
2324                 usleep(1);
2325                 return;
2326         }
2327
2328         switch (fr->frametype) {
2329         case AST_FRAME_BRIDGE_ACTION:
2330                 bridge_channel_handle_action(bridge_channel, fr->subclass.integer, fr->data.ptr);
2331                 break;
2332         case AST_FRAME_BRIDGE_ACTION_SYNC:
2333                 sync_payload = fr->data.ptr;
2334                 bridge_channel_handle_action(bridge_channel, fr->subclass.integer, sync_payload->data);
2335                 break;
2336         case AST_FRAME_CONTROL:
2337                 bridge_channel_handle_control(bridge_channel, fr);
2338                 break;
2339         case AST_FRAME_NULL:
2340                 break;
2341         default:
2342                 if (fr->stream_num >= (int)AST_VECTOR_SIZE(&bridge_channel->stream_map.to_channel)) {
2343                         /* Nowhere to write to, so drop it */
2344                         break;
2345                 }
2346
2347                 /* Find what stream number to write to for the channel */
2348                 num = fr->stream_num < 0 ? -1 :
2349                         AST_VECTOR_GET(&bridge_channel->stream_map.to_channel, fr->stream_num);
2350
2351                 /* Write the frame to the channel. */
2352                 bridge_channel->activity = BRIDGE_CHANNEL_THREAD_SIMPLE;
2353                 ast_write_stream(bridge_channel->chan, num, fr);
2354                 break;
2355         }
2356         bridge_frame_free(fr);
2357 }
2358
2359 /*! \brief Internal function to handle DTMF from a channel */
2360 static struct ast_frame *bridge_handle_dtmf(struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
2361 {
2362         struct ast_bridge_features *features = bridge_channel->features;
2363         struct ast_bridge_hook_dtmf *hook = NULL;
2364         char dtmf[2];
2365
2366         /*
2367          * See if we are already matching a DTMF feature hook sequence or
2368          * if this DTMF matches the beginning of any DTMF feature hooks.
2369          */
2370         dtmf[0] = frame->subclass.integer;
2371         dtmf[1] = '\0';
2372         if (bridge_channel->dtmf_hook_state.collected[0]
2373                 || (hook = ao2_find(features->dtmf_hooks, dtmf, OBJ_SEARCH_PARTIAL_KEY))) {
2374                 enum ast_frame_type frametype = frame->frametype;
2375
2376                 bridge_frame_free(frame);
2377                 frame = NULL;
2378
2379                 ao2_cleanup(hook);
2380
2381                 switch (frametype) {
2382                 case AST_FRAME_DTMF_BEGIN:
2383                         /* Just eat the frame. */
2384                         break;
2385                 case AST_FRAME_DTMF_END:
2386                         ast_bridge_channel_feature_digit(bridge_channel, dtmf[0]);
2387                         break;
2388                 default:
2389                         /* Unexpected frame type. */
2390                         ast_assert(0);
2391                         break;
2392                 }
2393 #ifdef TEST_FRAMEWORK
2394         } else if (frame->frametype == AST_FRAME_DTMF_END) {
2395                 /* Only transmit this event on DTMF end or else every DTMF
2396                  * press will result in the event being broadcast twice
2397                  */
2398                 ast_test_suite_event_notify("FEATURE_DETECTION", "Result: fail");
2399 #endif
2400         }
2401
2402         return frame;
2403 }
2404
2405 static const char *controls[] = {
2406         [AST_CONTROL_RINGING] = "RINGING",
2407         [AST_CONTROL_PROCEEDING] = "PROCEEDING",
2408         [AST_CONTROL_PROGRESS] = "PROGRESS",
2409         [AST_CONTROL_BUSY] = "BUSY",
2410         [AST_CONTROL_CONGESTION] = "CONGESTION",
2411         [AST_CONTROL_ANSWER] = "ANSWER",
2412 };
2413
2414 /*!
2415  * \internal
2416  * \brief Feed notification that a frame is waiting on a channel into the bridging core
2417  *
2418  * \param bridge_channel Bridge channel the notification was received on
2419  */
2420 static void bridge_handle_trip(struct ast_bridge_channel *bridge_channel)
2421 {
2422         struct ast_frame *frame;
2423
2424         if (!ast_strlen_zero(ast_channel_call_forward(bridge_channel->chan))) {
2425                 /* TODO If early bridging is ever used by anything other than ARI,
2426                  * it's important that we actually attempt to handle the call forward
2427                  * attempt, as well as expand features on a bridge channel to allow/disallow
2428                  * call forwarding. For now, all we do is raise an event, showing that
2429                  * a call forward is being attempted.
2430                  */
2431                 ast_channel_publish_dial_forward(NULL, bridge_channel->chan, NULL, NULL, "CANCEL",
2432                         ast_channel_call_forward(bridge_channel->chan));
2433         }
2434
2435         if (bridge_channel->features->mute) {
2436                 frame = ast_read_noaudio(bridge_channel->chan);
2437         } else {
2438                 frame = ast_read(bridge_channel->chan);
2439         }
2440
2441         if (!frame) {
2442                 ast_bridge_channel_kick(bridge_channel, 0);
2443                 return;
2444         }
2445         switch (frame->frametype) {
2446         case AST_FRAME_CONTROL:
2447                 switch (frame->subclass.integer) {
2448                 case AST_CONTROL_CONGESTION:
2449                 case AST_CONTROL_BUSY:
2450                         ast_channel_publish_dial(NULL, bridge_channel->chan, NULL, controls[frame->subclass.integer]);
2451                         break;
2452                 case AST_CONTROL_HANGUP:
2453                         ast_bridge_channel_kick(bridge_channel, 0);
2454                         bridge_frame_free(frame);
2455                         return;
2456                 case AST_CONTROL_RINGING:
2457                 case AST_CONTROL_PROGRESS:
2458                 case AST_CONTROL_PROCEEDING:
2459                 case AST_CONTROL_ANSWER:
2460                         ast_channel_publish_dial(NULL, bridge_channel->chan, NULL, controls[frame->subclass.integer]);
2461                         break;
2462                 case AST_CONTROL_STREAM_TOPOLOGY_REQUEST_CHANGE:
2463                         if (bridge_channel->bridge->technology->stream_topology_request_change &&
2464                             bridge_channel->bridge->technology->stream_topology_request_change(
2465                                     bridge_channel->bridge, bridge_channel)) {
2466                                 /* Topology change was denied so drop frame */
2467                                 bridge_frame_free(frame);
2468                                 return;
2469                         }
2470                         break;
2471                 case AST_CONTROL_STREAM_TOPOLOGY_CHANGED:
2472                         /*
2473                          * If a stream topology has changed then the bridge_channel's
2474                          * media mapping needs to be updated.
2475                          */
2476                         ast_bridge_channel_stream_map(bridge_channel);
2477
2478                         if (bridge_channel->bridge->technology->stream_topology_changed) {
2479                                 bridge_channel->bridge->technology->stream_topology_changed(
2480                                         bridge_channel->bridge, bridge_channel);
2481                         }
2482                         break;
2483                 default:
2484                         break;
2485                 }
2486                 break;
2487         case AST_FRAME_DTMF_BEGIN:
2488         case AST_FRAME_DTMF_END:
2489                 frame = bridge_handle_dtmf(bridge_channel, frame);
2490                 if (!frame) {
2491                         return;
2492                 }
2493                 if (!bridge_channel->features->dtmf_passthrough) {
2494                         bridge_frame_free(frame);
2495                         return;
2496                 }
2497                 break;
2498         default:
2499                 break;
2500         }
2501
2502         /* Simply write the frame out to the bridge technology. */
2503         bridge_channel_write_frame(bridge_channel, frame);
2504         bridge_frame_free(frame);
2505 }
2506
2507 /*!
2508  * \internal
2509  * \brief Determine how long till the next timer interval.
2510  * \since 12.0.0
2511  *
2512  * \param bridge_channel Channel to determine how long can wait.
2513  *
2514  * \retval ms Number of milliseconds to wait.
2515  * \retval -1 to wait forever.
2516  */
2517 static int bridge_channel_next_interval(struct ast_bridge_channel *bridge_channel)
2518 {
2519         struct ast_heap *interval_hooks = bridge_channel->features->interval_hooks;
2520         struct ast_bridge_hook_timer *hook;
2521         int ms;
2522
2523         ast_heap_wrlock(interval_hooks);
2524         hook = ast_heap_peek(interval_hooks, 1);
2525         if (hook) {
2526                 ms = ast_tvdiff_ms(hook->timer.trip_time, ast_tvnow());
2527                 if (ms < 0) {
2528                         /* Expire immediately.  An interval hook is ready to run. */
2529                         ms = 0;
2530                 }
2531         } else {
2532                 /* No hook so wait forever. */
2533                 ms = -1;
2534         }
2535         ast_heap_unlock(interval_hooks);
2536
2537         return ms;
2538 }
2539
2540 /*!
2541  * \internal
2542  * \brief Determine how long till the DTMF interdigit timeout.
2543  * \since 12.8.0
2544  *
2545  * \param bridge_channel Channel to determine how long can wait.
2546  *
2547  * \retval ms Number of milliseconds to wait.
2548  * \retval -1 to wait forever.
2549  */
2550 static int bridge_channel_feature_timeout(struct ast_bridge_channel *bridge_channel)
2551 {
2552         int ms;
2553
2554         if (bridge_channel->dtmf_hook_state.collected[0]) {
2555                 ms = ast_tvdiff_ms(bridge_channel->dtmf_hook_state.interdigit_timeout,
2556                         ast_tvnow());
2557                 if (ms < 0) {
2558                         /* Expire immediately. */
2559                         ms = 0;
2560                 }
2561         } else {
2562                 /* Timer is not active so wait forever. */
2563                 ms = -1;
2564         }
2565
2566         return ms;
2567 }
2568
2569 /*!
2570  * \internal
2571  * \brief Determine how long till a timeout.
2572  * \since 12.8.0
2573  *
2574  * \param bridge_channel Channel to determine how long can wait.
2575  *
2576  * \retval ms Number of milliseconds to wait.
2577  * \retval -1 to wait forever.
2578  */
2579 static int bridge_channel_next_timeout(struct ast_bridge_channel *bridge_channel)
2580 {
2581         int ms_interval;
2582         int ms;
2583
2584         ms_interval = bridge_channel_next_interval(bridge_channel);
2585         ms = bridge_channel_feature_timeout(bridge_channel);
2586         if (ms < 0 || (0 <= ms_interval && ms_interval < ms)) {
2587                 /* Interval hook timeout is next. */
2588                 ms = ms_interval;
2589         }
2590
2591         return ms;
2592 }
2593
2594 /*!
2595  * \internal
2596  * \brief Wait for something to happen on the bridge channel and handle it.
2597  * \since 12.0.0
2598  *
2599  * \param bridge_channel Channel to wait.
2600  *
2601  * \note Each channel does writing/reading in their own thread.
2602  *
2603  * \return Nothing
2604  */
2605 static void bridge_channel_wait(struct ast_bridge_channel *bridge_channel)
2606 {
2607         int ms;
2608         int outfd;
2609         struct ast_channel *chan;
2610
2611         /* Wait for data to either come from the channel or us to be signaled */
2612         ast_bridge_channel_lock(bridge_channel);
2613         if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
2614         } else if (bridge_channel->suspended) {
2615 /* XXX ASTERISK-21271 the external party use of suspended will go away as will these references because this is the bridge channel thread */
2616                 ast_debug(1, "Bridge %s: %p(%s) is going into a signal wait\n",
2617                         bridge_channel->bridge->uniqueid, bridge_channel,
2618                         ast_channel_name(bridge_channel->chan));
2619                 ast_cond_wait(&bridge_channel->cond, ao2_object_get_lockaddr(bridge_channel));
2620         } else {
2621                 ast_bridge_channel_unlock(bridge_channel);
2622                 outfd = -1;
2623                 ms = bridge_channel_next_timeout(bridge_channel);
2624                 chan = ast_waitfor_nandfds(&bridge_channel->chan, 1,
2625                         &bridge_channel->alert_pipe[0], 1, NULL, &outfd, &ms);
2626                 if (ast_channel_unbridged(bridge_channel->chan)) {
2627                         ast_channel_set_unbridged(bridge_channel->chan, 0);
2628                         ast_bridge_channel_lock_bridge(bridge_channel);
2629                         bridge_channel->bridge->reconfigured = 1;
2630                         bridge_reconfigured(bridge_channel->bridge, 0);
2631                         ast_bridge_unlock(bridge_channel->bridge);
2632                 }
2633                 ast_bridge_channel_lock(bridge_channel);
2634                 bridge_channel->activity = BRIDGE_CHANNEL_THREAD_FRAME;
2635                 ast_bridge_channel_unlock(bridge_channel);
2636                 if (!bridge_channel->suspended
2637                         && bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
2638                         if (chan) {
2639                                 bridge_handle_trip(bridge_channel);
2640                         } else if (ms == 0) {
2641                                 /* An interdigit timeout or interval expired. */
2642                                 bridge_channel_handle_feature_timeout(bridge_channel);
2643                                 bridge_channel_handle_interval(bridge_channel);
2644                         } else if (-1 < outfd) {
2645                                 /*
2646                                  * Must do this after checking timeouts or may have
2647                                  * an infinite loop due to deferring write queue
2648                                  * actions while trying to match DTMF feature hooks.
2649                                  */
2650                                 bridge_channel_handle_write(bridge_channel);
2651                         }
2652                 }
2653                 bridge_channel->activity = BRIDGE_CHANNEL_THREAD_IDLE;
2654                 return;
2655         }
2656         ast_bridge_channel_unlock(bridge_channel);
2657 }
2658
2659 /*!
2660  * \internal
2661  * \brief Handle bridge channel join/leave event.
2662  * \since 12.0.0
2663  *
2664  * \param bridge_channel Which channel is involved.
2665  * \param type Specified join/leave event.
2666  *
2667  * \return Nothing
2668  */
2669 static void bridge_channel_event_join_leave(struct ast_bridge_channel *bridge_channel, enum ast_bridge_hook_type type)
2670 {
2671         struct ast_bridge_features *features = bridge_channel->features;
2672         struct ast_bridge_hook *hook;
2673         struct ao2_iterator iter;
2674
2675         /* Run the specified hooks. */
2676         iter = ao2_iterator_init(features->other_hooks, 0);
2677         for (; (hook = ao2_iterator_next(&iter)); ao2_ref(hook, -1)) {
2678                 if (hook->type == type) {
2679                         break;
2680                 }
2681         }
2682         if (hook) {
2683                 /* Found the first specified hook to run. */
2684                 bridge_channel_suspend(bridge_channel);
2685                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
2686                 do {
2687                         if (hook->type == type) {
2688                                 hook->callback(bridge_channel, hook->hook_pvt);
2689                                 ao2_unlink(features->other_hooks, hook);
2690                         }
2691                         ao2_ref(hook, -1);
2692                 } while ((hook = ao2_iterator_next(&iter)));
2693                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
2694                 bridge_channel_unsuspend(bridge_channel);
2695         }
2696         ao2_iterator_destroy(&iter);
2697 }
2698
2699 int bridge_channel_internal_join(struct ast_bridge_channel *bridge_channel)
2700 {
2701         int res = 0;
2702         struct ast_bridge_features *channel_features;
2703         struct ast_channel *swap;
2704
2705         ast_debug(1, "Bridge %s: %p(%s) is joining\n",
2706                 bridge_channel->bridge->uniqueid,
2707                 bridge_channel, ast_channel_name(bridge_channel->chan));
2708
2709         /*
2710          * Directly locking the bridge is safe here because nobody else
2711          * knows about this bridge_channel yet.
2712          */
2713         ast_bridge_lock(bridge_channel->bridge);
2714
2715         ast_channel_lock(bridge_channel->chan);
2716
2717         bridge_channel->read_format = ao2_bump(ast_channel_readformat(bridge_channel->chan));
2718         bridge_channel->write_format = ao2_bump(ast_channel_writeformat(bridge_channel->chan));
2719
2720         /* Make sure we're still good to be put into a bridge */
2721         if (ast_channel_internal_bridge(bridge_channel->chan)
2722                 || ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_ZOMBIE)) {
2723                 ast_channel_unlock(bridge_channel->chan);
2724                 ast_bridge_unlock(bridge_channel->bridge);
2725                 ast_debug(1, "Bridge %s: %p(%s) failed to join Bridge\n",
2726                         bridge_channel->bridge->uniqueid,
2727                         bridge_channel,
2728                         ast_channel_name(bridge_channel->chan));
2729                 return -1;
2730         }
2731         ast_channel_internal_bridge_set(bridge_channel->chan, bridge_channel->bridge);
2732
2733         /* Attach features requested by the channel */
2734         channel_features = ast_channel_feature_hooks_get(bridge_channel->chan);
2735         if (channel_features) {
2736                 ast_bridge_features_merge(bridge_channel->features, channel_features);
2737         }
2738         ast_channel_unlock(bridge_channel->chan);
2739
2740         /* Add the jitterbuffer if the channel requires it */
2741         ast_jb_enable_for_channel(bridge_channel->chan);
2742
2743         if (!bridge_channel->bridge->callid) {
2744                 bridge_channel->bridge->callid = ast_read_threadstorage_callid();
2745         }
2746
2747         /* Take the swap channel ref from the bridge_channel struct. */
2748         swap = bridge_channel->swap;
2749
2750         if (bridge_channel_internal_push(bridge_channel)) {
2751                 int cause = bridge_channel->bridge->cause;
2752
2753                 ast_bridge_unlock(bridge_channel->bridge);
2754                 ast_bridge_channel_kick(bridge_channel, cause);
2755                 ast_bridge_channel_lock_bridge(bridge_channel);
2756                 ast_bridge_features_remove(bridge_channel->features,
2757                         AST_BRIDGE_HOOK_REMOVE_ON_PULL);
2758                 bridge_channel_dissolve_check(bridge_channel);
2759                 res = -1;
2760         }
2761         bridge_reconfigured(bridge_channel->bridge, !bridge_channel->inhibit_colp);
2762
2763         if (bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
2764                 /*
2765                  * Indicate a source change since this channel is entering the
2766                  * bridge system only if the bridge technology is not MULTIMIX
2767                  * capable.  The MULTIMIX technology has already done it.
2768                  */
2769                 if (!(bridge_channel->bridge->technology->capabilities
2770                         & AST_BRIDGE_CAPABILITY_MULTIMIX)) {
2771                         ast_indicate(bridge_channel->chan, AST_CONTROL_SRCCHANGE);
2772                 }
2773
2774                 bridge_channel_impart_signal(bridge_channel->chan);
2775                 ast_bridge_unlock(bridge_channel->bridge);
2776
2777                 /* Must release any swap ref after unlocking the bridge. */
2778                 ao2_t_cleanup(swap, "Bridge push with swap successful");
2779                 swap = NULL;
2780
2781                 bridge_channel_event_join_leave(bridge_channel, AST_BRIDGE_HOOK_TYPE_JOIN);
2782
2783                 while (bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
2784                         /* Wait for something to do. */
2785                         bridge_channel_wait(bridge_channel);
2786                 }
2787
2788                 /* Force a timeout on any accumulated DTMF hook digits. */
2789                 ast_bridge_channel_feature_digit(bridge_channel, 0);
2790
2791                 bridge_channel_event_join_leave(bridge_channel, AST_BRIDGE_HOOK_TYPE_LEAVE);
2792                 ast_bridge_channel_lock_bridge(bridge_channel);
2793         }
2794
2795         bridge_channel_internal_pull(bridge_channel);
2796         bridge_channel_settle_owed_events(bridge_channel->bridge, bridge_channel);
2797         bridge_reconfigured(bridge_channel->bridge, 1);
2798
2799         /* Remove ourselves if we are the video source */
2800         ast_bridge_remove_video_src(bridge_channel->bridge, bridge_channel->chan);
2801
2802         ast_bridge_unlock(bridge_channel->bridge);
2803
2804         /* Must release any swap ref after unlocking the bridge. */
2805         ao2_t_cleanup(swap, "Bridge push with swap failed or exited immediately");
2806
2807         /* Complete any active hold before exiting the bridge. */
2808         if (ast_channel_hold_state(bridge_channel->chan) == AST_CONTROL_HOLD) {
2809                 ast_debug(1, "Channel %s simulating UNHOLD for bridge end.\n",
2810                         ast_channel_name(bridge_channel->chan));
2811                 ast_indicate(bridge_channel->chan, AST_CONTROL_UNHOLD);
2812         }
2813
2814         /* Complete any partial DTMF digit before exiting the bridge. */
2815         if (ast_channel_sending_dtmf_digit(bridge_channel->chan)) {
2816                 ast_channel_end_dtmf(bridge_channel->chan,
2817                         ast_channel_sending_dtmf_digit(bridge_channel->chan),
2818                         ast_channel_sending_dtmf_tv(bridge_channel->chan), "bridge end");
2819         }
2820
2821         /* Complete any T.38 session before exiting the bridge. */
2822         if (ast_channel_is_t38_active(bridge_channel->chan)) {
2823                 struct ast_control_t38_parameters t38_parameters = {
2824                         .request_response = AST_T38_TERMINATED,
2825                 };
2826
2827                 ast_debug(1, "Channel %s simulating T.38 terminate for bridge end.\n",
2828                         ast_channel_name(bridge_channel->chan));
2829                 ast_indicate_data(bridge_channel->chan, AST_CONTROL_T38_PARAMETERS,
2830                         &t38_parameters, sizeof(t38_parameters));
2831         }
2832
2833         /* Indicate a source change since this channel is leaving the bridge system. */
2834         ast_indicate(bridge_channel->chan, AST_CONTROL_SRCCHANGE);
2835
2836         /*
2837          * Wait for any dual redirect to complete.
2838          *
2839          * Must be done while "still in the bridge" for ast_async_goto()
2840          * to work right.
2841          */
2842         while (ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_BRIDGE_DUAL_REDIRECT_WAIT)) {
2843                 sched_yield();
2844         }
2845         ast_channel_lock(bridge_channel->chan);
2846         ast_channel_internal_bridge_set(bridge_channel->chan, NULL);
2847         ast_channel_unlock(bridge_channel->chan);
2848
2849         ast_bridge_channel_restore_formats(bridge_channel);
2850
2851         return res;
2852 }
2853
2854 int bridge_channel_internal_queue_blind_transfer(struct ast_channel *transferee,
2855                 const char *exten, const char *context,
2856                 transfer_channel_cb new_channel_cb, void *user_data)
2857 {
2858         RAII_VAR(struct ast_bridge_channel *, transferee_bridge_channel, NULL, ao2_cleanup);
2859         struct blind_transfer_data blind_data;
2860
2861         ast_channel_lock(transferee);
2862         transferee_bridge_channel = ast_channel_get_bridge_channel(transferee);
2863         ast_channel_unlock(transferee);
2864
2865         if (!transferee_bridge_channel) {
2866                 return -1;
2867         }
2868
2869         if (new_channel_cb) {
2870                 new_channel_cb(transferee, user_data, AST_BRIDGE_TRANSFER_SINGLE_PARTY);
2871         }
2872
2873         ast_copy_string(blind_data.exten, exten, sizeof(blind_data.exten));
2874         ast_copy_string(blind_data.context, context, sizeof(blind_data.context));
2875
2876         return bridge_channel_queue_action_data(transferee_bridge_channel,
2877                 BRIDGE_CHANNEL_ACTION_BLIND_TRANSFER, &blind_data, sizeof(blind_data));
2878 }
2879
2880 int bridge_channel_internal_queue_attended_transfer(struct ast_channel *transferee,
2881                 struct ast_channel *unbridged_chan)
2882 {
2883         RAII_VAR(struct ast_bridge_channel *, transferee_bridge_channel, NULL, ao2_cleanup);
2884         char unbridged_chan_name[AST_CHANNEL_NAME];
2885
2886         ast_channel_lock(transferee);
2887         transferee_bridge_channel = ast_channel_get_bridge_channel(transferee);
2888         ast_channel_unlock(transferee);
2889
2890         if (!transferee_bridge_channel) {
2891                 return -1;
2892         }
2893
2894         ast_copy_string(unbridged_chan_name, ast_channel_name(unbridged_chan),
2895                 sizeof(unbridged_chan_name));
2896
2897         return bridge_channel_queue_action_data(transferee_bridge_channel,
2898                 BRIDGE_CHANNEL_ACTION_ATTENDED_TRANSFER, unbridged_chan_name,
2899                 sizeof(unbridged_chan_name));
2900 }
2901
2902 int bridge_channel_internal_allows_optimization(struct ast_bridge_channel *bridge_channel)
2903 {
2904         return bridge_channel->in_bridge
2905                 && AST_LIST_EMPTY(&bridge_channel->wr_queue);
2906 }
2907
2908 /* Destroy elements of the bridge channel structure and the bridge channel structure itself */
2909 static void bridge_channel_destroy(void *obj)
2910 {
2911         struct ast_bridge_channel *bridge_channel = obj;
2912         struct ast_frame *fr;
2913
2914         if (bridge_channel->callid) {
2915                 bridge_channel->callid = 0;
2916         }
2917
2918         if (bridge_channel->bridge) {
2919                 ao2_ref(bridge_channel->bridge, -1);
2920                 bridge_channel->bridge = NULL;
2921         }
2922
2923         /* Flush any unhandled wr_queue frames. */
2924         while ((fr = AST_LIST_REMOVE_HEAD(&bridge_channel->wr_queue, frame_list))) {
2925                 bridge_frame_free(fr);
2926         }
2927         ast_alertpipe_close(bridge_channel->alert_pipe);
2928
2929         ast_cond_destroy(&bridge_channel->cond);
2930
2931         ao2_cleanup(bridge_channel->write_format);
2932         ao2_cleanup(bridge_channel->read_format);
2933
2934         AST_VECTOR_FREE(&bridge_channel->stream_map.to_bridge);
2935         AST_VECTOR_FREE(&bridge_channel->stream_map.to_channel);
2936 }
2937
2938 struct ast_bridge_channel *bridge_channel_internal_alloc(struct ast_bridge *bridge)
2939 {
2940         struct ast_bridge_channel *bridge_channel;
2941
2942         bridge_channel = ao2_alloc(sizeof(struct ast_bridge_channel), bridge_channel_destroy);
2943         if (!bridge_channel) {
2944                 return NULL;
2945         }
2946         ast_cond_init(&bridge_channel->cond, NULL);
2947         if (ast_alertpipe_init(bridge_channel->alert_pipe)) {
2948                 ao2_ref(bridge_channel, -1);
2949                 return NULL;
2950         }
2951         if (bridge) {
2952                 bridge_channel->bridge = bridge;
2953                 ao2_ref(bridge_channel->bridge, +1);
2954         }
2955
2956         /* The stream_map is initialized later - see ast_bridge_channel_stream_map */
2957
2958         return bridge_channel;
2959 }
2960
2961 void ast_bridge_channel_stream_map(struct ast_bridge_channel *bridge_channel)
2962 {
2963         ast_stream_topology_map(ast_channel_get_stream_topology(bridge_channel->chan),
2964                 &bridge_channel->bridge->media_types, &bridge_channel->stream_map.to_bridge,
2965                 &bridge_channel->stream_map.to_channel);
2966 }