8261a421a5f42058d044b1d2535c779d2df8aadb
[asterisk/asterisk.git] / main / bridging_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 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36
37 #include <signal.h>
38
39 #include "asterisk/heap.h"
40 #include "asterisk/astobj2.h"
41 #include "asterisk/stringfields.h"
42 #include "asterisk/app.h"
43 #include "asterisk/pbx.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/timing.h"
46 #include "asterisk/bridging.h"
47 #include "asterisk/bridging_channel.h"
48 #include "asterisk/bridging_channel_internal.h"
49 #include "asterisk/bridging_internal.h"
50 #include "asterisk/stasis_bridging.h"
51 #include "asterisk/stasis_channels.h"
52 #include "asterisk/musiconhold.h"
53 #include "asterisk/features_config.h"
54 #include "asterisk/parking.h"
55
56
57 struct ast_bridge *bridge_channel_merge_inhibit(struct ast_bridge_channel *bridge_channel, int request)
58 {
59         struct ast_bridge *bridge;
60
61         ast_bridge_channel_lock_bridge(bridge_channel);
62         bridge = bridge_channel->bridge;
63         ao2_ref(bridge, +1);
64         bridge_merge_inhibit_nolock(bridge, request);
65         ast_bridge_unlock(bridge);
66         return bridge;
67 }
68
69 void ast_bridge_channel_lock_bridge(struct ast_bridge_channel *bridge_channel)
70 {
71         struct ast_bridge *bridge;
72
73         for (;;) {
74                 /* Safely get the bridge pointer */
75                 ast_bridge_channel_lock(bridge_channel);
76                 bridge = bridge_channel->bridge;
77                 ao2_ref(bridge, +1);
78                 ast_bridge_channel_unlock(bridge_channel);
79
80                 /* Lock the bridge and see if it is still the bridge we need to lock. */
81                 ast_bridge_lock(bridge);
82                 if (bridge == bridge_channel->bridge) {
83                         ao2_ref(bridge, -1);
84                         return;
85                 }
86                 ast_bridge_unlock(bridge);
87                 ao2_ref(bridge, -1);
88         }
89 }
90
91 static void bridge_channel_poke(struct ast_bridge_channel *bridge_channel)
92 {
93         if (!pthread_equal(pthread_self(), bridge_channel->thread)) {
94                 while (bridge_channel->waiting) {
95                         pthread_kill(bridge_channel->thread, SIGURG);
96                         sched_yield();
97                 }
98         }
99 }
100
101 void ast_bridge_change_state_nolock(struct ast_bridge_channel *bridge_channel, enum ast_bridge_channel_state new_state)
102 {
103 /* BUGBUG need cause code for the bridge_channel leaving the bridge. */
104         if (bridge_channel->state != AST_BRIDGE_CHANNEL_STATE_WAIT) {
105                 return;
106         }
107
108         ast_debug(1, "Setting %p(%s) state from:%d to:%d\n",
109                 bridge_channel, ast_channel_name(bridge_channel->chan), bridge_channel->state,
110                 new_state);
111
112         /* Change the state on the bridge channel */
113         bridge_channel->state = new_state;
114
115         bridge_channel_poke(bridge_channel);
116 }
117
118 void ast_bridge_change_state(struct ast_bridge_channel *bridge_channel, enum ast_bridge_channel_state new_state)
119 {
120         ast_bridge_channel_lock(bridge_channel);
121         ast_bridge_change_state_nolock(bridge_channel, new_state);
122         ast_bridge_channel_unlock(bridge_channel);
123 }
124
125 void bridge_channel_update_accountcodes(struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
126 {
127         struct ast_bridge *bridge = bridge_channel->bridge;
128         struct ast_bridge_channel *other = NULL;
129
130         AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
131                 if (other == swap) {
132                         continue;
133                 }
134
135                 if (!ast_strlen_zero(ast_channel_accountcode(bridge_channel->chan)) && ast_strlen_zero(ast_channel_peeraccount(other->chan))) {
136                         ast_debug(1, "Setting peeraccount to %s for %s from data on channel %s\n",
137                                         ast_channel_accountcode(bridge_channel->chan), ast_channel_name(other->chan), ast_channel_name(bridge_channel->chan));
138                         ast_channel_peeraccount_set(other->chan, ast_channel_accountcode(bridge_channel->chan));
139                 }
140                 if (!ast_strlen_zero(ast_channel_accountcode(other->chan)) && ast_strlen_zero(ast_channel_peeraccount(bridge_channel->chan))) {
141                         ast_debug(1, "Setting peeraccount to %s for %s from data on channel %s\n",
142                                         ast_channel_accountcode(other->chan), ast_channel_name(bridge_channel->chan), ast_channel_name(other->chan));
143                         ast_channel_peeraccount_set(bridge_channel->chan, ast_channel_accountcode(other->chan));
144                 }
145                 if (!ast_strlen_zero(ast_channel_peeraccount(bridge_channel->chan)) && ast_strlen_zero(ast_channel_accountcode(other->chan))) {
146                         ast_debug(1, "Setting accountcode to %s for %s from data on channel %s\n",
147                                         ast_channel_peeraccount(bridge_channel->chan), ast_channel_name(other->chan), ast_channel_name(bridge_channel->chan));
148                         ast_channel_accountcode_set(other->chan, ast_channel_peeraccount(bridge_channel->chan));
149                 }
150                 if (!ast_strlen_zero(ast_channel_peeraccount(other->chan)) && ast_strlen_zero(ast_channel_accountcode(bridge_channel->chan))) {
151                         ast_debug(1, "Setting accountcode to %s for %s from data on channel %s\n",
152                                         ast_channel_peeraccount(other->chan), ast_channel_name(bridge_channel->chan), ast_channel_name(other->chan));
153                         ast_channel_accountcode_set(bridge_channel->chan, ast_channel_peeraccount(other->chan));
154                 }
155                 if (bridge->num_channels == 2) {
156                         if (strcmp(ast_channel_accountcode(bridge_channel->chan), ast_channel_peeraccount(other->chan))) {
157                                 ast_debug(1, "Changing peeraccount from %s to %s on %s to match channel %s\n",
158                                                 ast_channel_peeraccount(other->chan), ast_channel_peeraccount(bridge_channel->chan), ast_channel_name(other->chan), ast_channel_name(bridge_channel->chan));
159                                 ast_channel_peeraccount_set(other->chan, ast_channel_accountcode(bridge_channel->chan));
160                         }
161                         if (strcmp(ast_channel_accountcode(other->chan), ast_channel_peeraccount(bridge_channel->chan))) {
162                                 ast_debug(1, "Changing peeraccount from %s to %s on %s to match channel %s\n",
163                                                 ast_channel_peeraccount(bridge_channel->chan), ast_channel_peeraccount(other->chan), ast_channel_name(bridge_channel->chan), ast_channel_name(other->chan));
164                                 ast_channel_peeraccount_set(bridge_channel->chan, ast_channel_accountcode(other->chan));
165                         }
166                 }
167         }
168 }
169
170 void bridge_channel_update_linkedids(struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
171 {
172         struct ast_bridge_channel *other = NULL;
173         struct ast_bridge *bridge = bridge_channel->bridge;
174         const char *oldest_linkedid = ast_channel_linkedid(bridge_channel->chan);
175
176         AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
177                 if (other == swap) {
178                         continue;
179                 }
180                 oldest_linkedid = ast_channel_oldest_linkedid(oldest_linkedid, ast_channel_linkedid(other->chan));
181         }
182
183         if (ast_strlen_zero(oldest_linkedid)) {
184                 return;
185         }
186
187         ast_channel_linkedid_set(bridge_channel->chan, oldest_linkedid);
188         AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
189                 if (other == swap) {
190                         continue;
191                 }
192                 ast_channel_linkedid_set(other->chan, oldest_linkedid);
193         }
194 }
195
196 int ast_bridge_channel_queue_frame(struct ast_bridge_channel *bridge_channel, struct ast_frame *fr)
197 {
198         struct ast_frame *dup;
199         char nudge = 0;
200
201         if (bridge_channel->suspended
202                 /* Also defer DTMF frames. */
203                 && fr->frametype != AST_FRAME_DTMF_BEGIN
204                 && fr->frametype != AST_FRAME_DTMF_END
205                 && !ast_is_deferrable_frame(fr)) {
206                 /* Drop non-deferable frames when suspended. */
207                 return 0;
208         }
209         if (fr->frametype == AST_FRAME_NULL) {
210                 /* "Accept" the frame and discard it. */
211                 return 0;
212         }
213
214         dup = ast_frdup(fr);
215         if (!dup) {
216                 return -1;
217         }
218
219         ast_bridge_channel_lock(bridge_channel);
220         if (bridge_channel->state != AST_BRIDGE_CHANNEL_STATE_WAIT) {
221                 /* Drop frames on channels leaving the bridge. */
222                 ast_bridge_channel_unlock(bridge_channel);
223                 ast_frfree(dup);
224                 return 0;
225         }
226
227         AST_LIST_INSERT_TAIL(&bridge_channel->wr_queue, dup, frame_list);
228         if (write(bridge_channel->alert_pipe[1], &nudge, sizeof(nudge)) != sizeof(nudge)) {
229                 ast_log(LOG_ERROR, "We couldn't write alert pipe for %p(%s)... something is VERY wrong\n",
230                         bridge_channel, ast_channel_name(bridge_channel->chan));
231         }
232         ast_bridge_channel_unlock(bridge_channel);
233         return 0;
234 }
235
236 int ast_bridge_channel_queue_action_data(struct ast_bridge_channel *bridge_channel, enum ast_bridge_action_type action, const void *data, size_t datalen)
237 {
238         struct ast_frame frame = {
239                 .frametype = AST_FRAME_BRIDGE_ACTION,
240                 .subclass.integer = action,
241                 .datalen = datalen,
242                 .data.ptr = (void *) data,
243         };
244
245         return ast_bridge_channel_queue_frame(bridge_channel, &frame);
246 }
247
248 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)
249 {
250         struct ast_frame frame = {
251                 .frametype = AST_FRAME_CONTROL,
252                 .subclass.integer = control,
253                 .datalen = datalen,
254                 .data.ptr = (void *) data,
255         };
256
257         return ast_bridge_channel_queue_frame(bridge_channel, &frame);
258 }
259
260 int ast_bridge_queue_everyone_else(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
261 {
262         struct ast_bridge_channel *cur;
263         int not_written = -1;
264
265         if (frame->frametype == AST_FRAME_NULL) {
266                 /* "Accept" the frame and discard it. */
267                 return 0;
268         }
269
270         AST_LIST_TRAVERSE(&bridge->channels, cur, entry) {
271                 if (cur == bridge_channel) {
272                         continue;
273                 }
274                 if (!ast_bridge_channel_queue_frame(cur, frame)) {
275                         not_written = 0;
276                 }
277         }
278         return not_written;
279 }
280
281 void ast_bridge_channel_restore_formats(struct ast_bridge_channel *bridge_channel)
282 {
283         /* Restore original formats of the channel as they came in */
284         if (ast_format_cmp(ast_channel_readformat(bridge_channel->chan), &bridge_channel->read_format) == AST_FORMAT_CMP_NOT_EQUAL) {
285                 ast_debug(1, "Bridge is returning %p(%s) to read format %s\n",
286                         bridge_channel, ast_channel_name(bridge_channel->chan),
287                         ast_getformatname(&bridge_channel->read_format));
288                 if (ast_set_read_format(bridge_channel->chan, &bridge_channel->read_format)) {
289                         ast_debug(1, "Bridge failed to return %p(%s) to read format %s\n",
290                                 bridge_channel, ast_channel_name(bridge_channel->chan),
291                                 ast_getformatname(&bridge_channel->read_format));
292                 }
293         }
294         if (ast_format_cmp(ast_channel_writeformat(bridge_channel->chan), &bridge_channel->write_format) == AST_FORMAT_CMP_NOT_EQUAL) {
295                 ast_debug(1, "Bridge is returning %p(%s) to write format %s\n",
296                         bridge_channel, ast_channel_name(bridge_channel->chan),
297                         ast_getformatname(&bridge_channel->write_format));
298                 if (ast_set_write_format(bridge_channel->chan, &bridge_channel->write_format)) {
299                         ast_debug(1, "Bridge failed to return %p(%s) to write format %s\n",
300                                 bridge_channel, ast_channel_name(bridge_channel->chan),
301                                 ast_getformatname(&bridge_channel->write_format));
302                 }
303         }
304 }
305
306 static int bridge_channel_write_frame(struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
307 {
308         ast_bridge_channel_lock_bridge(bridge_channel);
309 /*
310  * BUGBUG need to implement a deferred write queue for when there is no peer channel in the bridge (yet or it was kicked).
311  *
312  * The tech decides if a frame needs to be pushed back for deferral.
313  * simple_bridge/native_bridge are likely the only techs that will do this.
314  */
315         bridge_channel->bridge->technology->write(bridge_channel->bridge, bridge_channel, frame);
316         ast_bridge_unlock(bridge_channel->bridge);
317
318         /*
319          * Claim successful write to bridge.  If deferred frame
320          * support is added, claim successfully deferred.
321          */
322         return 0;
323 }
324
325 int ast_bridge_channel_write_action_data(struct ast_bridge_channel *bridge_channel, enum ast_bridge_action_type action, const void *data, size_t datalen)
326 {
327         struct ast_frame frame = {
328                 .frametype = AST_FRAME_BRIDGE_ACTION,
329                 .subclass.integer = action,
330                 .datalen = datalen,
331                 .data.ptr = (void *) data,
332         };
333
334         return bridge_channel_write_frame(bridge_channel, &frame);
335 }
336
337 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)
338 {
339         struct ast_frame frame = {
340                 .frametype = AST_FRAME_CONTROL,
341                 .subclass.integer = control,
342                 .datalen = datalen,
343                 .data.ptr = (void *) data,
344         };
345
346         return bridge_channel_write_frame(bridge_channel, &frame);
347 }
348
349 int ast_bridge_channel_write_hold(struct ast_bridge_channel *bridge_channel, const char *moh_class)
350 {
351         RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
352         size_t datalen;
353
354         if (!ast_strlen_zero(moh_class)) {
355                 datalen = strlen(moh_class) + 1;
356
357                 blob = ast_json_pack("{s: s}",
358                         "musicclass", moh_class);
359         } else {
360                 moh_class = NULL;
361                 datalen = 0;
362         }
363
364         ast_channel_publish_blob(bridge_channel->chan, ast_channel_hold_type(), blob);
365         return ast_bridge_channel_write_control_data(bridge_channel, AST_CONTROL_HOLD,
366                 moh_class, datalen);
367 }
368
369 int ast_bridge_channel_write_unhold(struct ast_bridge_channel *bridge_channel)
370 {
371         ast_channel_publish_blob(bridge_channel->chan, ast_channel_unhold_type(), NULL);
372         return ast_bridge_channel_write_control_data(bridge_channel, AST_CONTROL_UNHOLD, NULL, 0);
373 }
374
375 static int run_app_helper(struct ast_channel *chan, const char *app_name, const char *app_args)
376 {
377         int res = 0;
378
379         if (!strcasecmp("Gosub", app_name)) {
380                 ast_app_exec_sub(NULL, chan, app_args, 0);
381         } else if (!strcasecmp("Macro", app_name)) {
382                 ast_app_exec_macro(NULL, chan, app_args);
383         } else {
384                 struct ast_app *app;
385
386                 app = pbx_findapp(app_name);
387                 if (!app) {
388                         ast_log(LOG_WARNING, "Could not find application (%s)\n", app_name);
389                 } else {
390                         res = pbx_exec(chan, app, app_args);
391                 }
392         }
393         return res;
394 }
395
396 /*!
397 * \internal
398 * \brief Handle bridge hangup event.
399 * \since 12.0.0
400 *
401 * \param bridge_channel Which channel is hanging up.
402 *
403 * \return Nothing
404 */
405 static void bridge_channel_handle_hangup(struct ast_bridge_channel *bridge_channel)
406 {
407         struct ast_bridge_features *features = bridge_channel->features;
408         struct ast_bridge_hook *hook;
409         struct ao2_iterator iter;
410
411         /* Run any hangup hooks. */
412         iter = ao2_iterator_init(features->hangup_hooks, 0);
413         for (; (hook = ao2_iterator_next(&iter)); ao2_ref(hook, -1)) {
414                 int remove_me;
415
416                 remove_me = hook->callback(bridge_channel->bridge, bridge_channel, hook->hook_pvt);
417                 if (remove_me) {
418                         ast_debug(1, "Hangup hook %p is being removed from %p(%s)\n",
419                                 hook, bridge_channel, ast_channel_name(bridge_channel->chan));
420                         ao2_unlink(features->hangup_hooks, hook);
421                 }
422         }
423         ao2_iterator_destroy(&iter);
424
425         /* Default hangup action. */
426         ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_END);
427 }
428
429 void ast_bridge_channel_run_app(struct ast_bridge_channel *bridge_channel, const char *app_name, const char *app_args, const char *moh_class)
430 {
431         if (moh_class) {
432                 ast_bridge_channel_write_hold(bridge_channel, moh_class);
433         }
434         if (run_app_helper(bridge_channel->chan, app_name, S_OR(app_args, ""))) {
435                 /* Break the bridge if the app returns non-zero. */
436                 bridge_channel_handle_hangup(bridge_channel);
437         }
438         if (moh_class) {
439                 ast_bridge_channel_write_unhold(bridge_channel);
440         }
441 }
442
443
444 struct bridge_run_app {
445         /*! Offset into app_name[] where the MOH class name starts.  (zero if no MOH) */
446         int moh_offset;
447         /*! Offset into app_name[] where the application argument string starts. (zero if no arguments) */
448         int app_args_offset;
449         /*! Application name to run. */
450         char app_name[0];
451 };
452
453 /*!
454  * \internal
455  * \brief Handle the run application bridge action.
456  * \since 12.0.0
457  *
458  * \param bridge_channel Which channel to run the application on.
459  * \param data Action frame data to run the application.
460  *
461  * \return Nothing
462  */
463 static void bridge_channel_run_app(struct ast_bridge_channel *bridge_channel, struct bridge_run_app *data)
464 {
465         ast_bridge_channel_run_app(bridge_channel, data->app_name,
466                 data->app_args_offset ? &data->app_name[data->app_args_offset] : NULL,
467                 data->moh_offset ? &data->app_name[data->moh_offset] : NULL);
468 }
469
470 static int payload_helper_app(ast_bridge_channel_post_action_data post_it,
471         struct ast_bridge_channel *bridge_channel, const char *app_name, const char *app_args, const char *moh_class)
472 {
473         struct bridge_run_app *app_data;
474         size_t len_name = strlen(app_name) + 1;
475         size_t len_args = ast_strlen_zero(app_args) ? 0 : strlen(app_args) + 1;
476         size_t len_moh = !moh_class ? 0 : strlen(moh_class) + 1;
477         size_t len_data = sizeof(*app_data) + len_name + len_args + len_moh;
478
479         /* Fill in application run frame data. */
480         app_data = alloca(len_data);
481         app_data->app_args_offset = len_args ? len_name : 0;
482         app_data->moh_offset = len_moh ? len_name + len_args : 0;
483         strcpy(app_data->app_name, app_name);/* Safe */
484         if (len_args) {
485                 strcpy(&app_data->app_name[app_data->app_args_offset], app_args);/* Safe */
486         }
487         if (moh_class) {
488                 strcpy(&app_data->app_name[app_data->moh_offset], moh_class);/* Safe */
489         }
490
491         return post_it(bridge_channel, AST_BRIDGE_ACTION_RUN_APP, app_data, len_data);
492 }
493
494 int ast_bridge_channel_write_app(struct ast_bridge_channel *bridge_channel, const char *app_name, const char *app_args, const char *moh_class)
495 {
496         return payload_helper_app(ast_bridge_channel_write_action_data,
497                 bridge_channel, app_name, app_args, moh_class);
498 }
499
500 int ast_bridge_channel_queue_app(struct ast_bridge_channel *bridge_channel, const char *app_name, const char *app_args, const char *moh_class)
501 {
502         return payload_helper_app(ast_bridge_channel_queue_action_data,
503                 bridge_channel, app_name, app_args, moh_class);
504 }
505
506 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)
507 {
508         if (moh_class) {
509                 ast_bridge_channel_write_hold(bridge_channel, moh_class);
510         }
511         if (custom_play) {
512                 custom_play(bridge_channel, playfile);
513         } else {
514                 ast_stream_and_wait(bridge_channel->chan, playfile, AST_DIGIT_NONE);
515         }
516         if (moh_class) {
517                 ast_bridge_channel_write_unhold(bridge_channel);
518         }
519
520         /*
521          * It may be necessary to resume music on hold after we finish
522          * playing the announcment.
523          *
524          * XXX We have no idea what MOH class was in use before playing
525          * the file. This method also fails to restore ringing indications.
526          * the proposed solution is to create a resume_entertainment callback
527          * for the bridge technology and execute it here.
528          */
529         if (ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_MOH)) {
530                 ast_moh_start(bridge_channel->chan, NULL, NULL);
531         }
532 }
533
534 struct bridge_playfile {
535         /*! Call this function to play the playfile. (NULL if normal sound file to play) */
536         ast_bridge_custom_play_fn custom_play;
537         /*! Offset into playfile[] where the MOH class name starts.  (zero if no MOH)*/
538         int moh_offset;
539         /*! Filename to play. */
540         char playfile[0];
541 };
542
543 /*!
544  * \internal
545  * \brief Handle the playfile bridge action.
546  * \since 12.0.0
547  *
548  * \param bridge_channel Which channel to play a file on.
549  * \param payload Action frame payload to play a file.
550  *
551  * \return Nothing
552  */
553 static void bridge_channel_playfile(struct ast_bridge_channel *bridge_channel, struct bridge_playfile *payload)
554 {
555         ast_bridge_channel_playfile(bridge_channel, payload->custom_play, payload->playfile,
556                 payload->moh_offset ? &payload->playfile[payload->moh_offset] : NULL);
557 }
558
559 static int payload_helper_playfile(ast_bridge_channel_post_action_data post_it,
560         struct ast_bridge_channel *bridge_channel, ast_bridge_custom_play_fn custom_play, const char *playfile, const char *moh_class)
561 {
562         struct bridge_playfile *payload;
563         size_t len_name = strlen(playfile) + 1;
564         size_t len_moh = !moh_class ? 0 : strlen(moh_class) + 1;
565         size_t len_payload = sizeof(*payload) + len_name + len_moh;
566
567         /* Fill in play file frame data. */
568         payload = alloca(len_payload);
569         payload->custom_play = custom_play;
570         payload->moh_offset = len_moh ? len_name : 0;
571         strcpy(payload->playfile, playfile);/* Safe */
572         if (moh_class) {
573                 strcpy(&payload->playfile[payload->moh_offset], moh_class);/* Safe */
574         }
575
576         return post_it(bridge_channel, AST_BRIDGE_ACTION_PLAY_FILE, payload, len_payload);
577 }
578
579 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)
580 {
581         return payload_helper_playfile(ast_bridge_channel_write_action_data,
582                 bridge_channel, custom_play, playfile, moh_class);
583 }
584
585 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)
586 {
587         return payload_helper_playfile(ast_bridge_channel_queue_action_data,
588                 bridge_channel, custom_play, playfile, moh_class);
589 }
590
591 struct bridge_custom_callback {
592         /*! Call this function on the bridge channel thread. */
593         ast_bridge_custom_callback_fn callback;
594         /*! Size of the payload if it exists.  A number otherwise. */
595         size_t payload_size;
596         /*! Nonzero if the payload exists. */
597         char payload_exists;
598         /*! Payload to give to callback. */
599         char payload[0];
600 };
601
602 /*!
603  * \internal
604  * \brief Handle the do custom callback bridge action.
605  * \since 12.0.0
606  *
607  * \param bridge_channel Which channel to run the application on.
608  * \param data Action frame data to run the application.
609  *
610  * \return Nothing
611  */
612 static void bridge_channel_do_callback(struct ast_bridge_channel *bridge_channel, struct bridge_custom_callback *data)
613 {
614         data->callback(bridge_channel, data->payload_exists ? data->payload : NULL, data->payload_size);
615 }
616
617 static int payload_helper_cb(ast_bridge_channel_post_action_data post_it,
618         struct ast_bridge_channel *bridge_channel, ast_bridge_custom_callback_fn callback, const void *payload, size_t payload_size)
619 {
620         struct bridge_custom_callback *cb_data;
621         size_t len_data = sizeof(*cb_data) + (payload ? payload_size : 0);
622
623         /* Sanity check. */
624         if (!callback) {
625                 ast_assert(0);
626                 return -1;
627         }
628
629         /* Fill in custom callback frame data. */
630         cb_data = alloca(len_data);
631         cb_data->callback = callback;
632         cb_data->payload_size = payload_size;
633         cb_data->payload_exists = payload && payload_size;
634         if (cb_data->payload_exists) {
635                 memcpy(cb_data->payload, payload, payload_size);/* Safe */
636         }
637
638         return post_it(bridge_channel, AST_BRIDGE_ACTION_CALLBACK, cb_data, len_data);
639 }
640
641 int ast_bridge_channel_write_callback(struct ast_bridge_channel *bridge_channel, ast_bridge_custom_callback_fn callback, const void *payload, size_t payload_size)
642 {
643         return payload_helper_cb(ast_bridge_channel_write_action_data,
644                 bridge_channel, callback, payload, payload_size);
645 }
646
647 int ast_bridge_channel_queue_callback(struct ast_bridge_channel *bridge_channel, ast_bridge_custom_callback_fn callback, const void *payload, size_t payload_size)
648 {
649         return payload_helper_cb(ast_bridge_channel_queue_action_data,
650                 bridge_channel, callback, payload, payload_size);
651 }
652
653 struct bridge_park {
654         int parker_uuid_offset;
655         int app_data_offset;
656         /* buffer used for holding those strings */
657         char parkee_uuid[0];
658 };
659
660 static void bridge_channel_park(struct ast_bridge_channel *bridge_channel, struct bridge_park *payload)
661 {
662         ast_bridge_channel_park(bridge_channel, payload->parkee_uuid,
663                 &payload->parkee_uuid[payload->parker_uuid_offset],
664                 payload->app_data_offset ? &payload->parkee_uuid[payload->app_data_offset] : NULL);
665 }
666
667 static int payload_helper_park(ast_bridge_channel_post_action_data post_it,
668         struct ast_bridge_channel *bridge_channel,
669         const char *parkee_uuid,
670         const char *parker_uuid,
671         const char *app_data)
672 {
673         struct bridge_park *payload;
674         size_t len_parkee_uuid = strlen(parkee_uuid) + 1;
675         size_t len_parker_uuid = strlen(parker_uuid) + 1;
676         size_t len_app_data = !app_data ? 0 : strlen(app_data) + 1;
677         size_t len_payload = sizeof(*payload) + len_parker_uuid + len_parkee_uuid + len_app_data;
678
679         payload = alloca(len_payload);
680         payload->app_data_offset = len_app_data ? len_parkee_uuid + len_parker_uuid : 0;
681         payload->parker_uuid_offset = len_parkee_uuid;
682         strcpy(payload->parkee_uuid, parkee_uuid);
683         strcpy(&payload->parkee_uuid[payload->parker_uuid_offset], parker_uuid);
684         if (app_data) {
685                 strcpy(&payload->parkee_uuid[payload->app_data_offset], app_data);
686         }
687
688         return post_it(bridge_channel, AST_BRIDGE_ACTION_PARK, payload, len_payload);
689 }
690
691 int ast_bridge_channel_write_park(struct ast_bridge_channel *bridge_channel, const char *parkee_uuid, const char *parker_uuid, const char *app_data)
692 {
693         return payload_helper_park(ast_bridge_channel_write_action_data,
694                 bridge_channel, parkee_uuid, parker_uuid, app_data);
695 }
696
697 static int bridge_channel_interval_ready(struct ast_bridge_channel *bridge_channel)
698 {
699         struct ast_bridge_features *features = bridge_channel->features;
700         struct ast_bridge_hook *hook;
701         int ready;
702
703         ast_heap_wrlock(features->interval_hooks);
704         hook = ast_heap_peek(features->interval_hooks, 1);
705         ready = hook && ast_tvdiff_ms(hook->parms.timer.trip_time, ast_tvnow()) <= 0;
706         ast_heap_unlock(features->interval_hooks);
707
708         return ready;
709 }
710
711 int ast_bridge_notify_talking(struct ast_bridge_channel *bridge_channel, int started_talking)
712 {
713         struct ast_frame action = {
714                 .frametype = AST_FRAME_BRIDGE_ACTION,
715                 .subclass.integer = started_talking
716                         ? AST_BRIDGE_ACTION_TALKING_START : AST_BRIDGE_ACTION_TALKING_STOP,
717         };
718
719         return ast_bridge_channel_queue_frame(bridge_channel, &action);
720 }
721
722 struct ast_bridge_channel *ast_bridge_channel_peer(struct ast_bridge_channel *bridge_channel)
723 {
724         struct ast_bridge *bridge = bridge_channel->bridge;
725         struct ast_bridge_channel *other = NULL;
726
727         if (bridge_channel->in_bridge && bridge->num_channels == 2) {
728                 AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
729                         if (other != bridge_channel) {
730                                 break;
731                         }
732                 }
733         }
734
735         return other;
736 }
737
738 struct ast_bridge_channel *bridge_find_channel(struct ast_bridge *bridge, struct ast_channel *chan)
739 {
740         struct ast_bridge_channel *bridge_channel;
741
742         AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
743                 if (bridge_channel->chan == chan) {
744                         break;
745                 }
746         }
747
748         return bridge_channel;
749 }
750
751 /*!
752  * \internal
753  * \brief Suspend a channel from a bridge.
754  *
755  * \param bridge_channel Channel to suspend.
756  *
757  * \note This function assumes bridge_channel->bridge is locked.
758  *
759  * \return Nothing
760  */
761 void bridge_channel_suspend_nolock(struct ast_bridge_channel *bridge_channel)
762 {
763         bridge_channel->suspended = 1;
764         if (bridge_channel->in_bridge) {
765                 --bridge_channel->bridge->num_active;
766         }
767
768         /* Get technology bridge threads off of the channel. */
769         if (bridge_channel->bridge->technology->suspend) {
770                 bridge_channel->bridge->technology->suspend(bridge_channel->bridge, bridge_channel);
771         }
772 }
773
774 /*!
775  * \internal
776  * \brief Suspend a channel from a bridge.
777  *
778  * \param bridge_channel Channel to suspend.
779  *
780  * \return Nothing
781  */
782 static void bridge_channel_suspend(struct ast_bridge_channel *bridge_channel)
783 {
784         ast_bridge_channel_lock_bridge(bridge_channel);
785         bridge_channel_suspend_nolock(bridge_channel);
786         ast_bridge_unlock(bridge_channel->bridge);
787 }
788
789 /*!
790  * \internal
791  * \brief Unsuspend a channel from a bridge.
792  *
793  * \param bridge_channel Channel to unsuspend.
794  *
795  * \note This function assumes bridge_channel->bridge is locked.
796  *
797  * \return Nothing
798  */
799 void bridge_channel_unsuspend_nolock(struct ast_bridge_channel *bridge_channel)
800 {
801         bridge_channel->suspended = 0;
802         if (bridge_channel->in_bridge) {
803                 ++bridge_channel->bridge->num_active;
804         }
805
806         /* Wake technology bridge threads to take care of channel again. */
807         if (bridge_channel->bridge->technology->unsuspend) {
808                 bridge_channel->bridge->technology->unsuspend(bridge_channel->bridge, bridge_channel);
809         }
810
811         /* Wake suspended channel. */
812         ast_bridge_channel_lock(bridge_channel);
813         ast_cond_signal(&bridge_channel->cond);
814         ast_bridge_channel_unlock(bridge_channel);
815 }
816
817 /*!
818  * \internal
819  * \brief Unsuspend a channel from a bridge.
820  *
821  * \param bridge_channel Channel to unsuspend.
822  *
823  * \return Nothing
824  */
825 static void bridge_channel_unsuspend(struct ast_bridge_channel *bridge_channel)
826 {
827         ast_bridge_channel_lock_bridge(bridge_channel);
828         bridge_channel_unsuspend_nolock(bridge_channel);
829         ast_bridge_unlock(bridge_channel->bridge);
830 }
831
832 /*! \brief Internal function that activates interval hooks on a bridge channel */
833 static void bridge_channel_interval(struct ast_bridge_channel *bridge_channel)
834 {
835         struct ast_bridge_hook *hook;
836         struct timeval start;
837
838         ast_heap_wrlock(bridge_channel->features->interval_hooks);
839         start = ast_tvnow();
840         while ((hook = ast_heap_peek(bridge_channel->features->interval_hooks, 1))) {
841                 int interval;
842                 unsigned int execution_time;
843
844                 if (ast_tvdiff_ms(hook->parms.timer.trip_time, start) > 0) {
845                         ast_debug(1, "Hook %p on %p(%s) wants to happen in the future, stopping our traversal\n",
846                                 hook, bridge_channel, ast_channel_name(bridge_channel->chan));
847                         break;
848                 }
849                 ao2_ref(hook, +1);
850                 ast_heap_unlock(bridge_channel->features->interval_hooks);
851
852                 ast_debug(1, "Executing hook %p on %p(%s)\n",
853                         hook, bridge_channel, ast_channel_name(bridge_channel->chan));
854                 interval = hook->callback(bridge_channel->bridge, bridge_channel, hook->hook_pvt);
855
856                 ast_heap_wrlock(bridge_channel->features->interval_hooks);
857                 if (ast_heap_peek(bridge_channel->features->interval_hooks,
858                         hook->parms.timer.heap_index) != hook
859                         || !ast_heap_remove(bridge_channel->features->interval_hooks, hook)) {
860                         /* Interval hook is already removed from the bridge_channel. */
861                         ao2_ref(hook, -1);
862                         continue;
863                 }
864                 ao2_ref(hook, -1);
865
866                 if (interval < 0) {
867                         ast_debug(1, "Removed interval hook %p from %p(%s)\n",
868                                 hook, bridge_channel, ast_channel_name(bridge_channel->chan));
869                         ao2_ref(hook, -1);
870                         continue;
871                 }
872                 if (interval) {
873                         /* Set new interval for the hook. */
874                         hook->parms.timer.interval = interval;
875                 }
876
877                 ast_debug(1, "Updating interval hook %p with interval %u on %p(%s)\n",
878                         hook, hook->parms.timer.interval, bridge_channel,
879                         ast_channel_name(bridge_channel->chan));
880
881                 /* resetting start */
882                 start = ast_tvnow();
883
884                 /*
885                  * Resetup the interval hook for the next interval.  We may need
886                  * to skip over any missed intervals because the hook was
887                  * delayed or took too long.
888                  */
889                 execution_time = ast_tvdiff_ms(start, hook->parms.timer.trip_time);
890                 while (hook->parms.timer.interval < execution_time) {
891                         execution_time -= hook->parms.timer.interval;
892                 }
893                 hook->parms.timer.trip_time = ast_tvadd(start, ast_samp2tv(hook->parms.timer.interval - execution_time, 1000));
894                 hook->parms.timer.seqno = ast_atomic_fetchadd_int((int *) &bridge_channel->features->interval_sequence, +1);
895
896                 if (ast_heap_push(bridge_channel->features->interval_hooks, hook)) {
897                         /* Could not push the hook back onto the heap. */
898                         ao2_ref(hook, -1);
899                 }
900         }
901         ast_heap_unlock(bridge_channel->features->interval_hooks);
902 }
903
904 static int bridge_channel_write_dtmf_stream(struct ast_bridge_channel *bridge_channel, const char *dtmf)
905 {
906         return ast_bridge_channel_write_action_data(bridge_channel,
907                 AST_BRIDGE_ACTION_DTMF_STREAM, dtmf, strlen(dtmf) + 1);
908 }
909
910 /*!
911  * \brief Internal function that executes a feature on a bridge channel
912  * \note Neither the bridge nor the bridge_channel locks should be held when entering
913  * this function.
914  */
915 static void bridge_channel_feature(struct ast_bridge_channel *bridge_channel)
916 {
917         struct ast_bridge_features *features = bridge_channel->features;
918         struct ast_bridge_hook *hook = NULL;
919         char dtmf[MAXIMUM_DTMF_FEATURE_STRING] = "";
920         size_t dtmf_len = 0;
921         unsigned int digit_timeout;
922         RAII_VAR(struct ast_features_general_config *, gen_cfg, NULL, ao2_cleanup);
923
924         ast_channel_lock(bridge_channel->chan);
925         gen_cfg = ast_get_chan_features_general_config(bridge_channel->chan);
926         if (!gen_cfg) {
927                 ast_log(LOG_ERROR, "Unable to retrieve features configuration.\n");
928                 ast_channel_unlock(bridge_channel->chan);
929                 return;
930         }
931         digit_timeout = gen_cfg->featuredigittimeout;
932         ast_channel_unlock(bridge_channel->chan);
933
934         /* The channel is now under our control and we don't really want any begin frames to do our DTMF matching so disable 'em at the core level */
935         ast_set_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_END_DTMF_ONLY);
936
937         /* Wait for DTMF on the channel and put it into a buffer. If the buffer matches any feature hook execute the hook. */
938         do {
939                 int res;
940
941                 /* If the above timed out simply exit */
942                 res = ast_waitfordigit(bridge_channel->chan, digit_timeout);
943                 if (!res) {
944                         ast_debug(1, "DTMF feature string collection on %p(%s) timed out\n",
945                                 bridge_channel, ast_channel_name(bridge_channel->chan));
946                         break;
947                 }
948                 if (res < 0) {
949                         ast_debug(1, "DTMF feature string collection failed on %p(%s) for some reason\n",
950                                 bridge_channel, ast_channel_name(bridge_channel->chan));
951                         break;
952                 }
953
954 /* BUGBUG need to record the duration of DTMF digits so when the string is played back, they are reproduced. */
955                 /* Add the above DTMF into the DTMF string so we can do our matching */
956                 dtmf[dtmf_len++] = res;
957                 ast_debug(1, "DTMF feature string on %p(%s) is now '%s'\n",
958                         bridge_channel, ast_channel_name(bridge_channel->chan), dtmf);
959
960                 /* See if a DTMF feature hook matches or can match */
961                 hook = ao2_find(features->dtmf_hooks, dtmf, OBJ_PARTIAL_KEY);
962                 if (!hook) {
963                         ast_debug(1, "No DTMF feature hooks on %p(%s) match '%s'\n",
964                                 bridge_channel, ast_channel_name(bridge_channel->chan), dtmf);
965                         break;
966                 }
967                 if (strlen(hook->parms.dtmf.code) == dtmf_len) {
968                         ast_debug(1, "DTMF feature hook %p matched DTMF string '%s' on %p(%s)\n",
969                                 hook, dtmf, bridge_channel, ast_channel_name(bridge_channel->chan));
970                         break;
971                 }
972                 ao2_ref(hook, -1);
973                 hook = NULL;
974
975                 /* Stop if we have reached the maximum length of a DTMF feature string. */
976         } while (dtmf_len < ARRAY_LEN(dtmf) - 1);
977
978         /* Since we are done bringing DTMF in return to using both begin and end frames */
979         ast_clear_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_END_DTMF_ONLY);
980
981         /* If a hook was actually matched execute it on this channel, otherwise stream up the DTMF to the other channels */
982         if (hook) {
983                 int remove_me;
984
985                 remove_me = hook->callback(bridge_channel->bridge, bridge_channel, hook->hook_pvt);
986                 if (remove_me) {
987                         ast_debug(1, "DTMF hook %p is being removed from %p(%s)\n",
988                                 hook, bridge_channel, ast_channel_name(bridge_channel->chan));
989                         ao2_unlink(features->dtmf_hooks, hook);
990                 }
991                 ao2_ref(hook, -1);
992
993                 /*
994                  * If we are handing the channel off to an external hook for
995                  * ownership, we are not guaranteed what kind of state it will
996                  * come back in.  If the channel hungup, we need to detect that
997                  * here if the hook did not already change the state.
998                  */
999                 if (bridge_channel->chan && ast_check_hangup_locked(bridge_channel->chan)) {
1000                         bridge_channel_handle_hangup(bridge_channel);
1001                 }
1002         } else if (features->dtmf_passthrough) {
1003                 bridge_channel_write_dtmf_stream(bridge_channel, dtmf);
1004         }
1005 }
1006
1007 static void bridge_channel_talking(struct ast_bridge_channel *bridge_channel, int talking)
1008 {
1009         struct ast_bridge_features *features = bridge_channel->features;
1010
1011         if (features->talker_cb) {
1012                 features->talker_cb(bridge_channel, features->talker_pvt_data, talking);
1013         }
1014 }
1015
1016 /*! \brief Internal function that plays back DTMF on a bridge channel */
1017 static void bridge_channel_dtmf_stream(struct ast_bridge_channel *bridge_channel, const char *dtmf)
1018 {
1019         ast_debug(1, "Playing DTMF stream '%s' out to %p(%s)\n",
1020                 dtmf, bridge_channel, ast_channel_name(bridge_channel->chan));
1021         ast_dtmf_stream(bridge_channel->chan, NULL, dtmf, 0, 0);
1022 }
1023
1024 static void bridge_channel_blind_transfer(struct ast_bridge_channel *bridge_channel,
1025                 struct blind_transfer_data *blind_data)
1026 {
1027         ast_async_goto(bridge_channel->chan, blind_data->context, blind_data->exten, 1);
1028         bridge_channel_handle_hangup(bridge_channel);
1029 }
1030
1031 static void after_bridge_move_channel(struct ast_channel *chan_bridged, void *data)
1032 {
1033         RAII_VAR(struct ast_channel *, chan_target, data, ao2_cleanup);
1034         struct ast_party_connected_line connected_target;
1035         unsigned char connected_line_data[1024];
1036         int payload_size;
1037
1038         ast_party_connected_line_init(&connected_target);
1039
1040         ast_channel_lock(chan_target);
1041         ast_party_connected_line_copy(&connected_target, ast_channel_connected(chan_target));
1042         ast_channel_unlock(chan_target);
1043         ast_party_id_reset(&connected_target.priv);
1044
1045         if (ast_channel_move(chan_target, chan_bridged)) {
1046                 ast_softhangup(chan_target, AST_SOFTHANGUP_DEV);
1047                 ast_party_connected_line_free(&connected_target);
1048                 return;
1049         }
1050
1051         if ((payload_size = ast_connected_line_build_data(connected_line_data,
1052                 sizeof(connected_line_data), &connected_target, NULL)) != -1) {
1053                 struct ast_control_read_action_payload *frame_payload;
1054                 int frame_size;
1055
1056                 frame_size = payload_size + sizeof(*frame_payload);
1057                 frame_payload = ast_alloca(frame_size);
1058                 frame_payload->action = AST_FRAME_READ_ACTION_CONNECTED_LINE_MACRO;
1059                 frame_payload->payload_size = payload_size;
1060                 memcpy(frame_payload->payload, connected_line_data, payload_size);
1061                 ast_queue_control_data(chan_target, AST_CONTROL_READ_ACTION, frame_payload, frame_size);
1062         }
1063
1064         ast_party_connected_line_free(&connected_target);
1065 }
1066
1067 static void after_bridge_move_channel_fail(enum ast_after_bridge_cb_reason reason, void *data)
1068 {
1069         RAII_VAR(struct ast_channel *, chan_target, data, ao2_cleanup);
1070
1071         ast_log(LOG_WARNING, "Unable to complete transfer: %s\n",
1072                 ast_after_bridge_cb_reason_string(reason));
1073         ast_softhangup(chan_target, AST_SOFTHANGUP_DEV);
1074 }
1075
1076 static void bridge_channel_attended_transfer(struct ast_bridge_channel *bridge_channel,
1077                 const char *target_chan_name)
1078 {
1079         RAII_VAR(struct ast_channel *, chan_target, NULL, ao2_cleanup);
1080         RAII_VAR(struct ast_channel *, chan_bridged, NULL, ao2_cleanup);
1081
1082         chan_target = ast_channel_get_by_name(target_chan_name);
1083         if (!chan_target) {
1084                 /* Dang, it disappeared somehow */
1085                 bridge_channel_handle_hangup(bridge_channel);
1086                 return;
1087         }
1088
1089         ast_bridge_channel_lock(bridge_channel);
1090         chan_bridged = bridge_channel->chan;
1091         ast_assert(chan_bridged != NULL);
1092         ao2_ref(chan_bridged, +1);
1093         ast_bridge_channel_unlock(bridge_channel);
1094
1095         if (ast_after_bridge_callback_set(chan_bridged, after_bridge_move_channel,
1096                 after_bridge_move_channel_fail, ast_channel_ref(chan_target))) {
1097                 ast_softhangup(chan_target, AST_SOFTHANGUP_DEV);
1098
1099                 /* Release the ref we tried to pass to ast_after_bridge_callback_set(). */
1100                 ast_channel_unref(chan_target);
1101         }
1102         bridge_channel_handle_hangup(bridge_channel);
1103 }
1104
1105 /*!
1106  * \internal
1107  * \brief Handle bridge channel bridge action frame.
1108  * \since 12.0.0
1109  *
1110  * \param bridge_channel Channel to execute the action on.
1111  * \param action What to do.
1112  *
1113  * \return Nothing
1114  */
1115 static void bridge_channel_handle_action(struct ast_bridge_channel *bridge_channel, struct ast_frame *action)
1116 {
1117         switch (action->subclass.integer) {
1118         case AST_BRIDGE_ACTION_INTERVAL:
1119                 bridge_channel_suspend(bridge_channel);
1120                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1121                 bridge_channel_interval(bridge_channel);
1122                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1123                 bridge_channel_unsuspend(bridge_channel);
1124                 break;
1125         case AST_BRIDGE_ACTION_FEATURE:
1126                 bridge_channel_suspend(bridge_channel);
1127                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1128                 bridge_channel_feature(bridge_channel);
1129                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1130                 bridge_channel_unsuspend(bridge_channel);
1131                 break;
1132         case AST_BRIDGE_ACTION_DTMF_STREAM:
1133                 bridge_channel_suspend(bridge_channel);
1134                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1135                 bridge_channel_dtmf_stream(bridge_channel, action->data.ptr);
1136                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1137                 bridge_channel_unsuspend(bridge_channel);
1138                 break;
1139         case AST_BRIDGE_ACTION_TALKING_START:
1140         case AST_BRIDGE_ACTION_TALKING_STOP:
1141                 bridge_channel_talking(bridge_channel,
1142                         action->subclass.integer == AST_BRIDGE_ACTION_TALKING_START);
1143                 break;
1144         case AST_BRIDGE_ACTION_PLAY_FILE:
1145                 bridge_channel_suspend(bridge_channel);
1146                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1147                 bridge_channel_playfile(bridge_channel, action->data.ptr);
1148                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1149                 bridge_channel_unsuspend(bridge_channel);
1150                 break;
1151         case AST_BRIDGE_ACTION_RUN_APP:
1152                 bridge_channel_suspend(bridge_channel);
1153                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1154                 bridge_channel_run_app(bridge_channel, action->data.ptr);
1155                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1156                 bridge_channel_unsuspend(bridge_channel);
1157                 break;
1158         case AST_BRIDGE_ACTION_CALLBACK:
1159                 bridge_channel_suspend(bridge_channel);
1160                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1161                 bridge_channel_do_callback(bridge_channel, action->data.ptr);
1162                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1163                 bridge_channel_unsuspend(bridge_channel);
1164                 break;
1165         case AST_BRIDGE_ACTION_PARK:
1166                 bridge_channel_suspend(bridge_channel);
1167                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1168                 bridge_channel_park(bridge_channel, action->data.ptr);
1169                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1170                 bridge_channel_unsuspend(bridge_channel);
1171                 break;
1172         case AST_BRIDGE_ACTION_BLIND_TRANSFER:
1173                 bridge_channel_blind_transfer(bridge_channel, action->data.ptr);
1174                 break;
1175         case AST_BRIDGE_ACTION_ATTENDED_TRANSFER:
1176                 bridge_channel_attended_transfer(bridge_channel, action->data.ptr);
1177                 break;
1178         default:
1179                 break;
1180         }
1181 }
1182
1183 /*!
1184  * \internal
1185  * \brief Check if a bridge should dissolve and do it.
1186  * \since 12.0.0
1187  *
1188  * \param bridge_channel Channel causing the check.
1189  *
1190  * \note On entry, bridge_channel->bridge is already locked.
1191  *
1192  * \return Nothing
1193  */
1194 static void bridge_dissolve_check(struct ast_bridge_channel *bridge_channel)
1195 {
1196         struct ast_bridge *bridge = bridge_channel->bridge;
1197
1198         if (bridge->dissolved) {
1199                 return;
1200         }
1201
1202         if (!bridge->num_channels
1203                 && ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_DISSOLVE_EMPTY)) {
1204                 /* Last channel leaving the bridge turns off the lights. */
1205                 bridge_dissolve(bridge);
1206                 return;
1207         }
1208
1209         switch (bridge_channel->state) {
1210         case AST_BRIDGE_CHANNEL_STATE_END:
1211                 /* Do we need to dissolve the bridge because this channel hung up? */
1212                 if (ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_DISSOLVE_HANGUP)
1213                         || (bridge_channel->features->usable
1214                                 && ast_test_flag(&bridge_channel->features->feature_flags,
1215                                         AST_BRIDGE_CHANNEL_FLAG_DISSOLVE_HANGUP))) {
1216                         bridge_dissolve(bridge);
1217                         return;
1218                 }
1219                 break;
1220         default:
1221                 break;
1222         }
1223 /* BUGBUG need to implement AST_BRIDGE_CHANNEL_FLAG_LONELY support here */
1224 }
1225
1226 /*!
1227  * \internal
1228  * \brief Pull the bridge channel out of its current bridge.
1229  * \since 12.0.0
1230  *
1231  * \param bridge_channel Channel to pull.
1232  *
1233  * \note On entry, bridge_channel->bridge is already locked.
1234  *
1235  * \return Nothing
1236  */
1237 void bridge_channel_pull(struct ast_bridge_channel *bridge_channel)
1238 {
1239         struct ast_bridge *bridge = bridge_channel->bridge;
1240
1241         if (!bridge_channel->in_bridge) {
1242                 return;
1243         }
1244         bridge_channel->in_bridge = 0;
1245
1246         ast_debug(1, "Bridge %s: pulling %p(%s)\n",
1247                 bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan));
1248
1249         ast_verb(3, "Channel %s left '%s' %s-bridge <%s>\n",
1250                 ast_channel_name(bridge_channel->chan),
1251                 bridge->technology->name,
1252                 bridge->v_table->name,
1253                 bridge->uniqueid);
1254
1255 /* BUGBUG This is where incoming HOLD/UNHOLD memory should write UNHOLD into bridge. (if not local optimizing) */
1256 /* BUGBUG This is where incoming DTMF begin/end memory should write DTMF end into bridge. (if not local optimizing) */
1257         if (!bridge_channel->just_joined) {
1258                 /* Tell the bridge technology we are leaving so they tear us down */
1259                 ast_debug(1, "Bridge %s: %p(%s) is leaving %s technology\n",
1260                         bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan),
1261                         bridge->technology->name);
1262                 if (bridge->technology->leave) {
1263                         bridge->technology->leave(bridge, bridge_channel);
1264                 }
1265         }
1266
1267         /* Remove channel from the bridge */
1268         if (!bridge_channel->suspended) {
1269                 --bridge->num_active;
1270         }
1271         --bridge->num_channels;
1272         AST_LIST_REMOVE(&bridge->channels, bridge_channel, entry);
1273         bridge->v_table->pull(bridge, bridge_channel);
1274
1275         ast_bridge_channel_clear_roles(bridge_channel);
1276
1277         /* If we are not going to be hung up after leaving a bridge, and we were an
1278          * outgoing channel, clear the outgoing flag.
1279          */
1280         if (ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_OUTGOING)
1281                         && (ast_channel_softhangup_internal_flag(bridge_channel->chan) &
1282                                 (AST_SOFTHANGUP_ASYNCGOTO | AST_SOFTHANGUP_UNBRIDGE))) {
1283                 ast_clear_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_OUTGOING);
1284         }
1285
1286         bridge_dissolve_check(bridge_channel);
1287
1288         bridge->reconfigured = 1;
1289         ast_bridge_publish_leave(bridge, bridge_channel->chan);
1290 }
1291
1292 /*!
1293  * \internal
1294  * \brief Push the bridge channel into its specified bridge.
1295  * \since 12.0.0
1296  *
1297  * \param bridge_channel Channel to push.
1298  *
1299  * \note On entry, bridge_channel->bridge is already locked.
1300  *
1301  * \retval 0 on success.
1302  * \retval -1 on failure.  The channel did not get pushed.
1303  */
1304 int bridge_channel_push(struct ast_bridge_channel *bridge_channel)
1305 {
1306         struct ast_bridge *bridge = bridge_channel->bridge;
1307         struct ast_bridge_channel *swap;
1308
1309         ast_assert(!bridge_channel->in_bridge);
1310
1311         swap = bridge_find_channel(bridge, bridge_channel->swap);
1312         bridge_channel->swap = NULL;
1313
1314         if (swap) {
1315                 ast_debug(1, "Bridge %s: pushing %p(%s) by swapping with %p(%s)\n",
1316                         bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan),
1317                         swap, ast_channel_name(swap->chan));
1318         } else {
1319                 ast_debug(1, "Bridge %s: pushing %p(%s)\n",
1320                         bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan));
1321         }
1322
1323         /* Add channel to the bridge */
1324         if (bridge->dissolved
1325                 || bridge_channel->state != AST_BRIDGE_CHANNEL_STATE_WAIT
1326                 || (swap && swap->state != AST_BRIDGE_CHANNEL_STATE_WAIT)
1327                 || bridge->v_table->push(bridge, bridge_channel, swap)
1328                 || ast_bridge_channel_establish_roles(bridge_channel)) {
1329                 ast_debug(1, "Bridge %s: pushing %p(%s) into bridge failed\n",
1330                         bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan));
1331                 ast_bridge_features_remove(bridge_channel->features, AST_BRIDGE_HOOK_REMOVE_ON_PULL);
1332                 return -1;
1333         }
1334         bridge_channel->in_bridge = 1;
1335         bridge_channel->just_joined = 1;
1336         AST_LIST_INSERT_TAIL(&bridge->channels, bridge_channel, entry);
1337         ++bridge->num_channels;
1338         if (!bridge_channel->suspended) {
1339                 ++bridge->num_active;
1340         }
1341
1342         ast_verb(3, "Channel %s %s%s%s '%s' %s-bridge <%s>\n",
1343                 ast_channel_name(bridge_channel->chan),
1344                 swap ? "swapped with " : "joined",
1345                 swap ? ast_channel_name(swap->chan) : "",
1346                 swap ? " into" : "",
1347                 bridge->technology->name,
1348                 bridge->v_table->name,
1349                 bridge->uniqueid);
1350
1351         ast_bridge_publish_enter(bridge, bridge_channel->chan);
1352         if (swap) {
1353                 ast_bridge_change_state(swap, AST_BRIDGE_CHANNEL_STATE_HANGUP);
1354                 bridge_channel_pull(swap);
1355         }
1356
1357         /* Clear any BLINDTRANSFER and ATTENDEDTRANSFER since the transfer has completed. */
1358         pbx_builtin_setvar_helper(bridge_channel->chan, "BLINDTRANSFER", NULL);
1359         pbx_builtin_setvar_helper(bridge_channel->chan, "ATTENDEDTRANSFER", NULL);
1360
1361         bridge->reconfigured = 1;
1362         return 0;
1363 }
1364
1365 /*!
1366  * \internal
1367  * \brief Handle bridge channel control frame action.
1368  * \since 12.0.0
1369  *
1370  * \param bridge_channel Channel to execute the control frame action on.
1371  * \param fr Control frame to handle.
1372  *
1373  * \return Nothing
1374  */
1375 static void bridge_channel_handle_control(struct ast_bridge_channel *bridge_channel, struct ast_frame *fr)
1376 {
1377         struct ast_channel *chan;
1378         struct ast_option_header *aoh;
1379         int is_caller;
1380         int intercept_failed;
1381
1382         chan = bridge_channel->chan;
1383         switch (fr->subclass.integer) {
1384         case AST_CONTROL_REDIRECTING:
1385                 is_caller = !ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING);
1386                 bridge_channel_suspend(bridge_channel);
1387                 intercept_failed = ast_channel_redirecting_sub(NULL, chan, fr, 1)
1388                         && ast_channel_redirecting_macro(NULL, chan, fr, is_caller, 1);
1389                 bridge_channel_unsuspend(bridge_channel);
1390                 if (intercept_failed) {
1391                         ast_indicate_data(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
1392                 }
1393                 break;
1394         case AST_CONTROL_CONNECTED_LINE:
1395                 is_caller = !ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING);
1396                 bridge_channel_suspend(bridge_channel);
1397                 intercept_failed = ast_channel_connected_line_sub(NULL, chan, fr, 1)
1398                         && ast_channel_connected_line_macro(NULL, chan, fr, is_caller, 1);
1399                 bridge_channel_unsuspend(bridge_channel);
1400                 if (intercept_failed) {
1401                         ast_indicate_data(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
1402                 }
1403                 break;
1404         case AST_CONTROL_HOLD:
1405         case AST_CONTROL_UNHOLD:
1406 /*
1407  * BUGBUG bridge_channels should remember sending/receiving an outstanding HOLD to/from the bridge
1408  *
1409  * When the sending channel is pulled from the bridge it needs to write into the bridge an UNHOLD before being pulled.
1410  * When the receiving channel is pulled from the bridge it needs to generate its own UNHOLD.
1411  * Something similar needs to be done for DTMF begin/end.
1412  */
1413         case AST_CONTROL_VIDUPDATE:
1414         case AST_CONTROL_SRCUPDATE:
1415         case AST_CONTROL_SRCCHANGE:
1416         case AST_CONTROL_T38_PARAMETERS:
1417 /* BUGBUG may have to do something with a jitter buffer for these. */
1418                 ast_indicate_data(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
1419                 break;
1420         case AST_CONTROL_OPTION:
1421                 /*
1422                  * Forward option Requests, but only ones we know are safe These
1423                  * are ONLY sent by chan_iax2 and I'm not convinced that they
1424                  * are useful. I haven't deleted them entirely because I just am
1425                  * not sure of the ramifications of removing them.
1426                  */
1427                 aoh = fr->data.ptr;
1428                 if (aoh && aoh->flag == AST_OPTION_FLAG_REQUEST) {
1429                         switch (ntohs(aoh->option)) {
1430                         case AST_OPTION_TONE_VERIFY:
1431                         case AST_OPTION_TDD:
1432                         case AST_OPTION_RELAXDTMF:
1433                         case AST_OPTION_AUDIO_MODE:
1434                         case AST_OPTION_DIGIT_DETECT:
1435                         case AST_OPTION_FAX_DETECT:
1436                                 ast_channel_setoption(chan, ntohs(aoh->option), aoh->data,
1437                                         fr->datalen - sizeof(*aoh), 0);
1438                                 break;
1439                         default:
1440                                 break;
1441                         }
1442                 }
1443                 break;
1444         case AST_CONTROL_ANSWER:
1445                 if (ast_channel_state(chan) != AST_STATE_UP) {
1446                         ast_answer(chan);
1447                 } else {
1448                         ast_indicate(chan, -1);
1449                 }
1450                 break;
1451         default:
1452                 ast_indicate_data(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
1453                 break;
1454         }
1455 }
1456
1457 /*!
1458  * \internal
1459  * \brief Handle bridge channel write frame to channel.
1460  * \since 12.0.0
1461  *
1462  * \param bridge_channel Channel to write outgoing frame.
1463  *
1464  * \return Nothing
1465  */
1466 static void bridge_channel_handle_write(struct ast_bridge_channel *bridge_channel)
1467 {
1468         struct ast_frame *fr;
1469         char nudge;
1470
1471         ast_bridge_channel_lock(bridge_channel);
1472         if (read(bridge_channel->alert_pipe[0], &nudge, sizeof(nudge)) < 0) {
1473                 if (errno != EINTR && errno != EAGAIN) {
1474                         ast_log(LOG_WARNING, "read() failed for alert pipe on %p(%s): %s\n",
1475                                 bridge_channel, ast_channel_name(bridge_channel->chan), strerror(errno));
1476                 }
1477         }
1478         fr = AST_LIST_REMOVE_HEAD(&bridge_channel->wr_queue, frame_list);
1479         ast_bridge_channel_unlock(bridge_channel);
1480         if (!fr) {
1481                 return;
1482         }
1483         switch (fr->frametype) {
1484         case AST_FRAME_BRIDGE_ACTION:
1485                 bridge_channel_handle_action(bridge_channel, fr);
1486                 break;
1487         case AST_FRAME_CONTROL:
1488                 bridge_channel_handle_control(bridge_channel, fr);
1489                 break;
1490         case AST_FRAME_NULL:
1491                 break;
1492         default:
1493                 /* Write the frame to the channel. */
1494                 bridge_channel->activity = AST_BRIDGE_CHANNEL_THREAD_SIMPLE;
1495                 ast_write(bridge_channel->chan, fr);
1496                 break;
1497         }
1498         ast_frfree(fr);
1499 }
1500
1501 /*!
1502  * \internal
1503  * \brief Handle bridge channel interval expiration.
1504  * \since 12.0.0
1505  *
1506  * \param bridge_channel Channel to check interval on.
1507  *
1508  * \return Nothing
1509  */
1510 static void bridge_channel_handle_interval(struct ast_bridge_channel *bridge_channel)
1511 {
1512         struct ast_timer *interval_timer;
1513
1514         interval_timer = bridge_channel->features->interval_timer;
1515         if (interval_timer) {
1516                 if (ast_wait_for_input(ast_timer_fd(interval_timer), 0) == 1) {
1517                         ast_timer_ack(interval_timer, 1);
1518                         if (bridge_channel_interval_ready(bridge_channel)) {
1519 /* BUGBUG since this is now only run by the channel thread, there is no need to queue the action once this intervals become a first class wait item in bridge_channel_wait(). */
1520                                 struct ast_frame interval_action = {
1521                                         .frametype = AST_FRAME_BRIDGE_ACTION,
1522                                         .subclass.integer = AST_BRIDGE_ACTION_INTERVAL,
1523                                 };
1524
1525                                 ast_bridge_channel_queue_frame(bridge_channel, &interval_action);
1526                         }
1527                 }
1528         }
1529 }
1530
1531 /*! \brief Internal function to handle DTMF from a channel */
1532 static struct ast_frame *bridge_handle_dtmf(struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
1533 {
1534         struct ast_bridge_features *features = bridge_channel->features;
1535         struct ast_bridge_hook *hook;
1536         char dtmf[2];
1537
1538 /* BUGBUG the feature hook matching needs to be done here.  Any matching feature hook needs to be queued onto the bridge_channel.  Also the feature hook digit timeout needs to be handled. */
1539 /* BUGBUG the AMI atxfer action just sends DTMF end events to initiate DTMF atxfer and dial the extension.  Another reason the DTMF hook matching needs rework. */
1540         /* See if this DTMF matches the beginnings of any feature hooks, if so we switch to the feature state to either execute the feature or collect more DTMF */
1541         dtmf[0] = frame->subclass.integer;
1542         dtmf[1] = '\0';
1543         hook = ao2_find(features->dtmf_hooks, dtmf, OBJ_PARTIAL_KEY);
1544         if (hook) {
1545                 struct ast_frame action = {
1546                         .frametype = AST_FRAME_BRIDGE_ACTION,
1547                         .subclass.integer = AST_BRIDGE_ACTION_FEATURE,
1548                 };
1549
1550                 ast_frfree(frame);
1551                 frame = NULL;
1552                 ast_bridge_channel_queue_frame(bridge_channel, &action);
1553                 ao2_ref(hook, -1);
1554         }
1555
1556         return frame;
1557 }
1558
1559
1560 /*!
1561  * \internal
1562  * \brief Feed notification that a frame is waiting on a channel into the bridging core
1563  *
1564  * \param bridge_channel Bridge channel the notification was received on
1565  */
1566 static void bridge_handle_trip(struct ast_bridge_channel *bridge_channel)
1567 {
1568         struct ast_frame *frame;
1569
1570         if (bridge_channel->features->mute) {
1571                 frame = ast_read_noaudio(bridge_channel->chan);
1572         } else {
1573                 frame = ast_read(bridge_channel->chan);
1574         }
1575
1576         if (!frame) {
1577                 bridge_channel_handle_hangup(bridge_channel);
1578                 return;
1579         }
1580         switch (frame->frametype) {
1581         case AST_FRAME_CONTROL:
1582                 switch (frame->subclass.integer) {
1583                 case AST_CONTROL_HANGUP:
1584                         bridge_channel_handle_hangup(bridge_channel);
1585                         ast_frfree(frame);
1586                         return;
1587 /* BUGBUG This is where incoming HOLD/UNHOLD memory should register.  Write UNHOLD into bridge when this channel is pulled. */
1588                 default:
1589                         break;
1590                 }
1591                 break;
1592         case AST_FRAME_DTMF_BEGIN:
1593                 frame = bridge_handle_dtmf(bridge_channel, frame);
1594                 if (!frame) {
1595                         return;
1596                 }
1597                 /* Fall through */
1598         case AST_FRAME_DTMF_END:
1599                 if (!bridge_channel->features->dtmf_passthrough) {
1600                         ast_frfree(frame);
1601                         return;
1602                 }
1603 /* BUGBUG This is where incoming DTMF begin/end memory should register.  Write DTMF end into bridge when this channel is pulled. */
1604                 break;
1605         default:
1606                 break;
1607         }
1608
1609         /* Simply write the frame out to the bridge technology. */
1610 /* BUGBUG The tech is where AST_CONTROL_ANSWER hook should go. (early bridge) */
1611 /* BUGBUG The tech is where incoming BUSY/CONGESTION hangup should happen? (early bridge) */
1612         bridge_channel_write_frame(bridge_channel, frame);
1613         ast_frfree(frame);
1614 }
1615
1616 /*!
1617  * \internal
1618  * \brief Wait for something to happen on the bridge channel and handle it.
1619  * \since 12.0.0
1620  *
1621  * \param bridge_channel Channel to wait.
1622  *
1623  * \note Each channel does writing/reading in their own thread.
1624  *
1625  * \return Nothing
1626  */
1627 static void bridge_channel_wait(struct ast_bridge_channel *bridge_channel)
1628 {
1629         int ms = -1;
1630         int outfd;
1631         struct ast_channel *chan;
1632
1633         /* Wait for data to either come from the channel or us to be signaled */
1634         ast_bridge_channel_lock(bridge_channel);
1635         if (bridge_channel->state != AST_BRIDGE_CHANNEL_STATE_WAIT) {
1636         } else if (bridge_channel->suspended) {
1637 /* BUGBUG the external party use of suspended will go away as will these references because this is the bridge channel thread */
1638                 ast_debug(1, "Bridge %s: %p(%s) is going into a signal wait\n",
1639                         bridge_channel->bridge->uniqueid, bridge_channel,
1640                         ast_channel_name(bridge_channel->chan));
1641                 ast_cond_wait(&bridge_channel->cond, ao2_object_get_lockaddr(bridge_channel));
1642         } else {
1643                 ast_debug(10, "Bridge %s: %p(%s) is going into a waitfor\n",
1644                         bridge_channel->bridge->uniqueid, bridge_channel,
1645                         ast_channel_name(bridge_channel->chan));
1646                 bridge_channel->waiting = 1;
1647                 ast_bridge_channel_unlock(bridge_channel);
1648                 outfd = -1;
1649 /* BUGBUG need to make the next expiring active interval setup ms timeout rather than holding up the chan reads. */
1650                 chan = ast_waitfor_nandfds(&bridge_channel->chan, 1,
1651                         &bridge_channel->alert_pipe[0], 1, NULL, &outfd, &ms);
1652                 bridge_channel->waiting = 0;
1653                 if (ast_channel_softhangup_internal_flag(bridge_channel->chan) & AST_SOFTHANGUP_UNBRIDGE) {
1654                         ast_channel_clear_softhangup(bridge_channel->chan, AST_SOFTHANGUP_UNBRIDGE);
1655                         ast_bridge_channel_lock_bridge(bridge_channel);
1656                         bridge_channel->bridge->reconfigured = 1;
1657                         bridge_reconfigured(bridge_channel->bridge, 0);
1658                         ast_bridge_unlock(bridge_channel->bridge);
1659                 }
1660                 ast_bridge_channel_lock(bridge_channel);
1661                 bridge_channel->activity = AST_BRIDGE_CHANNEL_THREAD_FRAME;
1662                 ast_bridge_channel_unlock(bridge_channel);
1663                 if (!bridge_channel->suspended
1664                         && bridge_channel->state == AST_BRIDGE_CHANNEL_STATE_WAIT) {
1665                         if (chan) {
1666                                 bridge_channel_handle_interval(bridge_channel);
1667                                 bridge_handle_trip(bridge_channel);
1668                         } else if (-1 < outfd) {
1669                                 bridge_channel_handle_write(bridge_channel);
1670                         }
1671                 }
1672                 bridge_channel->activity = AST_BRIDGE_CHANNEL_THREAD_IDLE;
1673                 return;
1674         }
1675         ast_bridge_channel_unlock(bridge_channel);
1676 }
1677
1678 /*!
1679  * \internal
1680  * \brief Handle bridge channel join event.
1681  * \since 12.0.0
1682  *
1683  * \param bridge_channel Which channel is joining.
1684  *
1685  * \return Nothing
1686  */
1687 static void bridge_channel_handle_join(struct ast_bridge_channel *bridge_channel)
1688 {
1689         struct ast_bridge_features *features = bridge_channel->features;
1690         struct ast_bridge_hook *hook;
1691         struct ao2_iterator iter;
1692
1693         /* Run any join hooks. */
1694         iter = ao2_iterator_init(features->join_hooks, AO2_ITERATOR_UNLINK);
1695         hook = ao2_iterator_next(&iter);
1696         if (hook) {
1697                 bridge_channel_suspend(bridge_channel);
1698                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1699                 do {
1700                         hook->callback(bridge_channel->bridge, bridge_channel, hook->hook_pvt);
1701                         ao2_ref(hook, -1);
1702                 } while ((hook = ao2_iterator_next(&iter)));
1703                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1704                 bridge_channel_unsuspend(bridge_channel);
1705         }
1706         ao2_iterator_destroy(&iter);
1707 }
1708
1709 /*!
1710  * \internal
1711  * \brief Handle bridge channel leave event.
1712  * \since 12.0.0
1713  *
1714  * \param bridge_channel Which channel is leaving.
1715  *
1716  * \return Nothing
1717  */
1718 static void bridge_channel_handle_leave(struct ast_bridge_channel *bridge_channel)
1719 {
1720         struct ast_bridge_features *features = bridge_channel->features;
1721         struct ast_bridge_hook *hook;
1722         struct ao2_iterator iter;
1723
1724         /* Run any leave hooks. */
1725         iter = ao2_iterator_init(features->leave_hooks, AO2_ITERATOR_UNLINK);
1726         hook = ao2_iterator_next(&iter);
1727         if (hook) {
1728                 bridge_channel_suspend(bridge_channel);
1729                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1730                 do {
1731                         hook->callback(bridge_channel->bridge, bridge_channel, hook->hook_pvt);
1732                         ao2_ref(hook, -1);
1733                 } while ((hook = ao2_iterator_next(&iter)));
1734                 ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
1735                 bridge_channel_unsuspend(bridge_channel);
1736         }
1737         ao2_iterator_destroy(&iter);
1738 }
1739
1740 /*! \brief Join a channel to a bridge and handle anything the bridge may want us to do */
1741 void bridge_channel_join(struct ast_bridge_channel *bridge_channel)
1742 {
1743         ast_format_copy(&bridge_channel->read_format, ast_channel_readformat(bridge_channel->chan));
1744         ast_format_copy(&bridge_channel->write_format, ast_channel_writeformat(bridge_channel->chan));
1745
1746         ast_debug(1, "Bridge %s: %p(%s) is joining\n",
1747                 bridge_channel->bridge->uniqueid,
1748                 bridge_channel, ast_channel_name(bridge_channel->chan));
1749
1750         /*
1751          * Get "in the bridge" before pushing the channel for any
1752          * masquerades on the channel to happen before bridging.
1753          */
1754         ast_channel_lock(bridge_channel->chan);
1755         ast_channel_internal_bridge_set(bridge_channel->chan, bridge_channel->bridge);
1756         ast_channel_unlock(bridge_channel->chan);
1757
1758         /* Add the jitterbuffer if the channel requires it */
1759         ast_jb_enable_for_channel(bridge_channel->chan);
1760
1761         /*
1762          * Directly locking the bridge is safe here because nobody else
1763          * knows about this bridge_channel yet.
1764          */
1765         ast_bridge_lock(bridge_channel->bridge);
1766
1767         if (!bridge_channel->bridge->callid) {
1768                 bridge_channel->bridge->callid = ast_read_threadstorage_callid();
1769         }
1770
1771         if (bridge_channel_push(bridge_channel)) {
1772                 ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_HANGUP);
1773         }
1774         bridge_reconfigured(bridge_channel->bridge, 1);
1775
1776         if (bridge_channel->state == AST_BRIDGE_CHANNEL_STATE_WAIT) {
1777                 /*
1778                  * Indicate a source change since this channel is entering the
1779                  * bridge system only if the bridge technology is not MULTIMIX
1780                  * capable.  The MULTIMIX technology has already done it.
1781                  */
1782                 if (!(bridge_channel->bridge->technology->capabilities
1783                         & AST_BRIDGE_CAPABILITY_MULTIMIX)) {
1784                         ast_indicate(bridge_channel->chan, AST_CONTROL_SRCCHANGE);
1785                 }
1786
1787                 ast_bridge_unlock(bridge_channel->bridge);
1788                 bridge_channel_handle_join(bridge_channel);
1789                 while (bridge_channel->state == AST_BRIDGE_CHANNEL_STATE_WAIT) {
1790                         /* Wait for something to do. */
1791                         bridge_channel_wait(bridge_channel);
1792                 }
1793                 bridge_channel_handle_leave(bridge_channel);
1794                 ast_bridge_channel_lock_bridge(bridge_channel);
1795         }
1796
1797         bridge_channel_pull(bridge_channel);
1798         bridge_reconfigured(bridge_channel->bridge, 1);
1799
1800         ast_bridge_unlock(bridge_channel->bridge);
1801
1802         /* Indicate a source change since this channel is leaving the bridge system. */
1803         ast_indicate(bridge_channel->chan, AST_CONTROL_SRCCHANGE);
1804
1805 /* BUGBUG Revisit in regards to moving channels between bridges and local channel optimization. */
1806 /* BUGBUG This is where outgoing HOLD/UNHOLD memory should write UNHOLD to channel. */
1807         /* Complete any partial DTMF digit before exiting the bridge. */
1808         if (ast_channel_sending_dtmf_digit(bridge_channel->chan)) {
1809                 ast_channel_end_dtmf(bridge_channel->chan,
1810                         ast_channel_sending_dtmf_digit(bridge_channel->chan),
1811                         ast_channel_sending_dtmf_tv(bridge_channel->chan), "bridge end");
1812         }
1813
1814         /*
1815          * Wait for any dual redirect to complete.
1816          *
1817          * Must be done while "still in the bridge" for ast_async_goto()
1818          * to work right.
1819          */
1820         while (ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_BRIDGE_DUAL_REDIRECT_WAIT)) {
1821                 sched_yield();
1822         }
1823         ast_channel_lock(bridge_channel->chan);
1824         ast_channel_internal_bridge_set(bridge_channel->chan, NULL);
1825         ast_channel_unlock(bridge_channel->chan);
1826
1827         ast_bridge_channel_restore_formats(bridge_channel);
1828 }
1829
1830 void ast_bridge_channel_leave_bridge(struct ast_bridge_channel *bridge_channel)
1831 {
1832         ast_bridge_change_state(bridge_channel, AST_BRIDGE_CHANNEL_STATE_END);
1833 }
1834
1835 void ast_bridge_channel_leave_bridge_nolock(struct ast_bridge_channel *bridge_channel)
1836 {
1837         ast_bridge_change_state_nolock(bridge_channel, AST_BRIDGE_CHANNEL_STATE_END);
1838 }