Wed Jan 8 2020 09:49:42

Asterisk developer's documentation


bridging.c
Go to the documentation of this file.
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 Channel Bridging API
22  *
23  * \author Joshua Colp <jcolp@digium.com>
24  */
25 
26 /*** MODULEINFO
27  <support_level>core</support_level>
28  ***/
29 
30 #include "asterisk.h"
31 
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 413586 $")
33 
34 #include <signal.h>
35 
36 #include "asterisk/logger.h"
37 #include "asterisk/channel.h"
38 #include "asterisk/options.h"
39 #include "asterisk/utils.h"
40 #include "asterisk/lock.h"
41 #include "asterisk/linkedlists.h"
42 #include "asterisk/bridging.h"
44 #include "asterisk/app.h"
45 #include "asterisk/file.h"
46 #include "asterisk/module.h"
47 #include "asterisk/astobj2.h"
48 
50 
51 /* Initial starting point for the bridge array of channels */
52 #define BRIDGE_ARRAY_START 128
53 
54 /* Grow rate of bridge array of channels */
55 #define BRIDGE_ARRAY_GROW 32
56 
57 /*! Default DTMF keys for built in features */
59 
60 /*! Function handlers for the built in features */
62 
63 int __ast_bridge_technology_register(struct ast_bridge_technology *technology, struct ast_module *module)
64 {
65  struct ast_bridge_technology *current = NULL;
66 
67  /* Perform a sanity check to make sure the bridge technology conforms to our needed requirements */
68  if (ast_strlen_zero(technology->name) || !technology->capabilities || !technology->write) {
69  ast_log(LOG_WARNING, "Bridge technology %s failed registration sanity check.\n", technology->name);
70  return -1;
71  }
72 
74 
75  /* Look for duplicate bridge technology already using this name, or already registered */
76  AST_RWLIST_TRAVERSE(&bridge_technologies, current, entry) {
77  if ((!strcasecmp(current->name, technology->name)) || (current == technology)) {
78  ast_log(LOG_WARNING, "A bridge technology of %s already claims to exist in our world.\n", technology->name);
80  return -1;
81  }
82  }
83 
84  /* Copy module pointer so reference counting can keep the module from unloading */
85  technology->mod = module;
86 
87  /* Insert our new bridge technology into the list and print out a pretty message */
88  AST_RWLIST_INSERT_TAIL(&bridge_technologies, technology, entry);
89 
91 
92  if (option_verbose > 1) {
93  ast_verbose(VERBOSE_PREFIX_2 "Registered bridge technology %s\n", technology->name);
94  }
95 
96  return 0;
97 }
98 
100 {
101  struct ast_bridge_technology *current = NULL;
102 
104 
105  /* Ensure the bridge technology is registered before removing it */
107  if (current == technology) {
109  if (option_verbose > 1) {
110  ast_verbose(VERBOSE_PREFIX_2 "Unregistered bridge technology %s\n", technology->name);
111  }
112  break;
113  }
114  }
116 
118 
119  return current ? 0 : -1;
120 }
121 
122 void ast_bridge_change_state(struct ast_bridge_channel *bridge_channel, enum ast_bridge_channel_state new_state)
123 {
124  /* Change the state on the bridge channel */
125  bridge_channel->state = new_state;
126 
127  /* Only poke the channel's thread if it is not us */
128  if (!pthread_equal(pthread_self(), bridge_channel->thread)) {
129  pthread_kill(bridge_channel->thread, SIGURG);
130  ast_mutex_lock(&bridge_channel->lock);
131  ast_cond_signal(&bridge_channel->cond);
132  ast_mutex_unlock(&bridge_channel->lock);
133  }
134 
135  return;
136 }
137 
138 /*! \brief Helper function to poke the bridge thread */
139 static void bridge_poke(struct ast_bridge *bridge)
140 {
141  /* Poke the thread just in case */
142  if (bridge->thread != AST_PTHREADT_NULL && bridge->thread != AST_PTHREADT_STOP) {
143  pthread_kill(bridge->thread, SIGURG);
144  }
145 
146  return;
147 }
148 
149 /*! \brief Helper function to add a channel to the bridge array
150  *
151  * \note This function assumes the bridge is locked.
152  */
153 static void bridge_array_add(struct ast_bridge *bridge, struct ast_channel *chan)
154 {
155  /* We have to make sure the bridge thread is not using the bridge array before messing with it */
156  while (bridge->waiting) {
157  bridge_poke(bridge);
158  sched_yield();
159  }
160 
161  bridge->array[bridge->array_num++] = chan;
162 
163  ast_debug(1, "Added channel %s(%p) to bridge array on %p, new count is %d\n", chan->name, chan, bridge, (int)bridge->array_num);
164 
165  /* If the next addition of a channel will exceed our array size grow it out */
166  if (bridge->array_num == bridge->array_size) {
167  struct ast_channel **tmp;
168  ast_debug(1, "Growing bridge array on %p from %d to %d\n", bridge, (int)bridge->array_size, (int)bridge->array_size + BRIDGE_ARRAY_GROW);
169  if (!(tmp = ast_realloc(bridge->array, (bridge->array_size + BRIDGE_ARRAY_GROW) * sizeof(struct ast_channel *)))) {
170  ast_log(LOG_ERROR, "Failed to allocate more space for another channel on bridge '%p', this is not going to end well\n", bridge);
171  return;
172  }
173  bridge->array = tmp;
174  bridge->array_size += BRIDGE_ARRAY_GROW;
175  }
176 
177  return;
178 }
179 
180 /*! \brief Helper function to remove a channel from the bridge array
181  *
182  * \note This function assumes the bridge is locked.
183  */
184 static void bridge_array_remove(struct ast_bridge *bridge, struct ast_channel *chan)
185 {
186  int i;
187 
188  /* We have to make sure the bridge thread is not using the bridge array before messing with it */
189  while (bridge->waiting) {
190  bridge_poke(bridge);
191  sched_yield();
192  }
193 
194  for (i = 0; i < bridge->array_num; i++) {
195  if (bridge->array[i] == chan) {
196  bridge->array[i] = (bridge->array[(bridge->array_num - 1)] != chan ? bridge->array[(bridge->array_num - 1)] : NULL);
197  bridge->array[(bridge->array_num - 1)] = NULL;
198  bridge->array_num--;
199  ast_debug(1, "Removed channel %p from bridge array on %p, new count is %d\n", chan, bridge, (int)bridge->array_num);
200  break;
201  }
202  }
203 
204  return;
205 }
206 
207 /*! \brief Helper function to find a bridge channel given a channel */
209 {
210  struct ast_bridge_channel *bridge_channel = NULL;
211 
212  AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
213  if (bridge_channel->chan == chan) {
214  break;
215  }
216  }
217 
218  return bridge_channel;
219 }
220 
221 /*! \brief Internal function to see whether a bridge should dissolve, and if so do it */
222 static void bridge_check_dissolve(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
223 {
224  struct ast_bridge_channel *bridge_channel2 = NULL;
225 
226  if (!ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_DISSOLVE) && (!bridge_channel->features || !bridge_channel->features->usable || !ast_test_flag(&bridge_channel->features->feature_flags, AST_BRIDGE_FLAG_DISSOLVE))) {
227  return;
228  }
229 
230  ast_debug(1, "Dissolving bridge %p\n", bridge);
231 
232  AST_LIST_TRAVERSE(&bridge->channels, bridge_channel2, entry) {
233  if (bridge_channel2->state != AST_BRIDGE_CHANNEL_STATE_END && bridge_channel2->state != AST_BRIDGE_CHANNEL_STATE_DEPART) {
235  }
236  }
237 
238  return;
239 }
240 
241 /*! \brief Internal function to handle DTMF from a channel */
242 static struct ast_frame *bridge_handle_dtmf(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
243 {
244  struct ast_bridge_features *features = (bridge_channel->features ? bridge_channel->features : &bridge->features);
245  struct ast_bridge_features_hook *hook = NULL;
246 
247  /* If the features structure we grabbed is not usable immediately return the frame */
248  if (!features->usable) {
249  return frame;
250  }
251 
252  /* 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 */
253  AST_LIST_TRAVERSE(&features->hooks, hook, entry) {
254  if (hook->dtmf[0] == frame->subclass.integer) {
255  ast_frfree(frame);
256  frame = NULL;
258  break;
259  }
260  }
261 
262  return frame;
263 }
264 
265 /*! \brief Internal function used to determine whether a control frame should be dropped or not */
266 static int bridge_drop_control_frame(int subclass)
267 {
268  switch (subclass) {
269  case AST_CONTROL_ANSWER:
270  case -1:
271  return 1;
272  default:
273  return 0;
274  }
275 }
276 
277 void ast_bridge_handle_trip(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_channel *chan, int outfd)
278 {
279  /* If no bridge channel has been provided and the actual channel has been provided find it */
280  if (chan && !bridge_channel) {
281  bridge_channel = find_bridge_channel(bridge, chan);
282  }
283 
284  /* If a bridge channel with actual channel is present read a frame and handle it */
285  if (chan && bridge_channel) {
286  struct ast_frame *frame = (((bridge->features.mute) || (bridge_channel->features && bridge_channel->features->mute)) ? ast_read_noaudio(chan) : ast_read(chan));
287 
288  /* This is pretty simple... see if they hung up */
289  if (!frame || (frame->frametype == AST_FRAME_CONTROL && frame->subclass.integer == AST_CONTROL_HANGUP)) {
290  /* Signal the thread that is handling the bridged channel that it should be ended */
292  } else if (frame->frametype == AST_FRAME_CONTROL && bridge_drop_control_frame(frame->subclass.integer)) {
293  ast_debug(1, "Dropping control frame from bridge channel %p\n", bridge_channel);
294  } else {
295  if (frame->frametype == AST_FRAME_DTMF_BEGIN) {
296  frame = bridge_handle_dtmf(bridge, bridge_channel, frame);
297  }
298  /* Simply write the frame out to the bridge technology if it still exists */
299  if (frame) {
300  bridge->technology->write(bridge, bridge_channel, frame);
301  }
302  }
303 
304  if (frame) {
305  ast_frfree(frame);
306  }
307  return;
308  }
309 
310  /* If a file descriptor actually tripped pass it off to the bridge technology */
311  if (outfd > -1 && bridge->technology->fd) {
312  bridge->technology->fd(bridge, bridge_channel, outfd);
313  return;
314  }
315 
316  /* If all else fails just poke the bridge */
317  if (bridge->technology->poke && bridge_channel) {
318  bridge->technology->poke(bridge, bridge_channel);
319  return;
320  }
321 
322  return;
323 }
324 
325 /*! \brief Generic thread loop, TODO: Rethink this/improve it */
326 static int generic_thread_loop(struct ast_bridge *bridge)
327 {
328  while (!bridge->stop && !bridge->refresh && bridge->array_num) {
329  struct ast_channel *winner = NULL;
330  int to = -1;
331 
332  /* Move channels around for priority reasons if we have more than one channel in our array */
333  if (bridge->array_num > 1) {
334  struct ast_channel *first = bridge->array[0];
335  memmove(bridge->array, bridge->array + 1, sizeof(struct ast_channel *) * (bridge->array_num - 1));
336  bridge->array[(bridge->array_num - 1)] = first;
337  }
338 
339  /* Wait on the channels */
340  bridge->waiting = 1;
341  ao2_unlock(bridge);
342  winner = ast_waitfor_n(bridge->array, (int)bridge->array_num, &to);
343  bridge->waiting = 0;
344  ao2_lock(bridge);
345 
346  /* Process whatever they did */
347  ast_bridge_handle_trip(bridge, NULL, winner, -1);
348  }
349 
350  return 0;
351 }
352 
353 /*! \brief Bridge thread function */
354 static void *bridge_thread(void *data)
355 {
356  struct ast_bridge *bridge = data;
357  int res = 0;
358 
359  ao2_lock(bridge);
360 
361  ast_debug(1, "Started bridge thread for %p\n", bridge);
362 
363  /* Loop around until we are told to stop */
364  while (!bridge->stop && bridge->array_num && !res) {
365  /* In case the refresh bit was set simply set it back to off */
366  bridge->refresh = 0;
367 
368  ast_debug(1, "Launching bridge thread function %p for bridge %p\n", (bridge->technology->thread ? bridge->technology->thread : &generic_thread_loop), bridge);
369 
370  /* Execute the appropriate thread function. If the technology does not provide one we use the generic one */
371  res = (bridge->technology->thread ? bridge->technology->thread(bridge) : generic_thread_loop(bridge));
372  }
373 
374  ast_debug(1, "Ending bridge thread for %p\n", bridge);
375 
376  /* Indicate the bridge thread is no longer active */
377  bridge->thread = AST_PTHREADT_NULL;
378  ao2_unlock(bridge);
379 
380  ao2_ref(bridge, -1);
381 
382  return NULL;
383 }
384 
385 /*! \brief Helper function used to find the "best" bridge technology given a specified capabilities */
387 {
388  struct ast_bridge_technology *current = NULL, *best = NULL;
389 
391  AST_RWLIST_TRAVERSE(&bridge_technologies, current, entry) {
392  ast_debug(1, "Bridge technology %s has capabilities %d and we want %u\n", current->name, current->capabilities, capabilities);
393  if (current->suspended) {
394  ast_debug(1, "Bridge technology %s is suspended. Skipping.\n", current->name);
395  continue;
396  }
397  if (!(current->capabilities & capabilities)) {
398  ast_debug(1, "Bridge technology %s does not have the capabilities we need.\n", current->name);
399  continue;
400  }
401  if (best && best->preference < current->preference) {
402  ast_debug(1, "Bridge technology %s has preference %u while %s has preference %u. Skipping.\n", current->name, current->preference, best->name, best->preference);
403  continue;
404  }
405  best = current;
406  }
407 
408  if (best) {
409  /* Increment it's module reference count if present so it does not get unloaded while in use */
410  if (best->mod) {
411  ast_module_ref(best->mod);
412  }
413  ast_debug(1, "Chose bridge technology %s\n", best->name);
414  }
415 
417 
418  return best;
419 }
420 
421 static void destroy_bridge(void *obj)
422 {
423  struct ast_bridge *bridge = obj;
424 
425  ast_debug(1, "Actually destroying bridge %p, nobody wants it anymore\n", bridge);
426 
427  /* Pass off the bridge to the technology to destroy if needed */
428  if (bridge->technology->destroy) {
429  ast_debug(1, "Giving bridge technology %s the bridge structure %p to destroy\n", bridge->technology->name, bridge);
430  if (bridge->technology->destroy(bridge)) {
431  ast_debug(1, "Bridge technology %s failed to destroy bridge structure %p... trying our best\n", bridge->technology->name, bridge);
432  }
433  }
434 
435  /* We are no longer using the bridge technology so decrement the module reference count on it */
436  if (bridge->technology->mod) {
437  ast_module_unref(bridge->technology->mod);
438  }
439 
440  /* Last but not least clean up the features configuration */
442 
443  /* Drop the array of channels */
444  ast_free(bridge->array);
445 
446  return;
447 }
448 
450 {
451  struct ast_bridge *bridge = NULL;
452  struct ast_bridge_technology *bridge_technology = NULL;
453 
454  /* If we need to be a smart bridge see if we can move between 1to1 and multimix bridges */
455  if (flags & AST_BRIDGE_FLAG_SMART) {
456  struct ast_bridge *other_bridge;
457 
458  if (!(other_bridge = ast_bridge_new((capabilities & AST_BRIDGE_CAPABILITY_1TO1MIX) ? AST_BRIDGE_CAPABILITY_MULTIMIX : AST_BRIDGE_CAPABILITY_1TO1MIX, 0))) {
459  return NULL;
460  }
461 
462  ast_bridge_destroy(other_bridge);
463  }
464 
465  /* If capabilities were provided use our helper function to find the "best" bridge technology, otherwise we can
466  * just look for the most basic capability needed, single 1to1 mixing. */
467  bridge_technology = (capabilities ? find_best_technology(capabilities) : find_best_technology(AST_BRIDGE_CAPABILITY_1TO1MIX));
468 
469  /* If no bridge technology was found we can't possibly do bridging so fail creation of the bridge */
470  if (!bridge_technology) {
471  ast_debug(1, "Failed to find a bridge technology to satisfy capabilities %u\n", capabilities);
472  return NULL;
473  }
474 
475  /* We have everything we need to create this bridge... so allocate the memory, link things together, and fire her up! */
476  if (!(bridge = ao2_alloc(sizeof(*bridge), destroy_bridge))) {
477  return NULL;
478  }
479 
480  bridge->technology = bridge_technology;
481  bridge->thread = AST_PTHREADT_NULL;
482 
483  /* Create an array of pointers for the channels that will be joining us */
484  bridge->array = ast_calloc(BRIDGE_ARRAY_START, sizeof(struct ast_channel*));
485  bridge->array_size = BRIDGE_ARRAY_START;
486 
487  ast_set_flag(&bridge->feature_flags, flags);
488 
489  /* Pass off the bridge to the technology to manipulate if needed */
490  if (bridge->technology->create) {
491  ast_debug(1, "Giving bridge technology %s the bridge structure %p to setup\n", bridge->technology->name, bridge);
492  if (bridge->technology->create(bridge)) {
493  ast_debug(1, "Bridge technology %s failed to setup bridge structure %p\n", bridge->technology->name, bridge);
494  ao2_ref(bridge, -1);
495  bridge = NULL;
496  }
497  }
498 
499  return bridge;
500 }
501 
503 {
504  struct ast_bridge_technology *bridge_technology = NULL;
505 
506  if (!(bridge_technology = find_best_technology(capabilities))) {
507  return 0;
508  }
509 
510  ast_module_unref(bridge_technology->mod);
511 
512  return 1;
513 }
514 
515 int ast_bridge_destroy(struct ast_bridge *bridge)
516 {
517  struct ast_bridge_channel *bridge_channel = NULL;
518 
519  ao2_lock(bridge);
520 
521  if (bridge->thread != AST_PTHREADT_NULL) {
522  pthread_t thread = bridge->thread;
523  bridge->stop = 1;
524  bridge_poke(bridge);
525  ao2_unlock(bridge);
526  pthread_join(thread, NULL);
527  ao2_lock(bridge);
528  }
529 
530  ast_debug(1, "Telling all channels in bridge %p to end and leave the party\n", bridge);
531 
532  /* Drop every bridged channel, the last one will cause the bridge thread (if it exists) to exit */
533  AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
535  }
536 
537  ao2_unlock(bridge);
538 
539  ao2_ref(bridge, -1);
540 
541  return 0;
542 }
543 
544 static int bridge_make_compatible(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
545 {
546  format_t formats[2] = {bridge_channel->chan->readformat, bridge_channel->chan->writeformat};
547 
548  /* Are the formats currently in use something ths bridge can handle? */
549  if (!(bridge->technology->formats & bridge_channel->chan->readformat)) {
550  format_t best_format = ast_best_codec(bridge->technology->formats);
551 
552  /* Read format is a no go... */
553  if (option_debug) {
554  char codec_buf[512];
555  ast_debug(1, "Bridge technology %s wants to read any of formats %s but channel has %s\n", bridge->technology->name,
556  ast_getformatname_multiple(codec_buf, sizeof(codec_buf), bridge->technology->formats),
557  ast_getformatname(formats[0]));
558  }
559  /* Switch read format to the best one chosen */
560  if (ast_set_read_format(bridge_channel->chan, best_format)) {
561  ast_log(LOG_WARNING, "Failed to set channel %s to read format %s\n", bridge_channel->chan->name, ast_getformatname(best_format));
562  return -1;
563  }
564  ast_debug(1, "Bridge %p put channel %s into read format %s\n", bridge, bridge_channel->chan->name, ast_getformatname(best_format));
565  } else {
566  ast_debug(1, "Bridge %p is happy that channel %s already has read format %s\n", bridge, bridge_channel->chan->name, ast_getformatname(formats[0]));
567  }
568 
569  if (!(bridge->technology->formats & formats[1])) {
570  int best_format = ast_best_codec(bridge->technology->formats);
571 
572  /* Write format is a no go... */
573  if (option_debug) {
574  char codec_buf[512];
575  ast_debug(1, "Bridge technology %s wants to write any of formats %s but channel has %s\n", bridge->technology->name,
576  ast_getformatname_multiple(codec_buf, sizeof(codec_buf), bridge->technology->formats),
577  ast_getformatname(formats[1]));
578  }
579  /* Switch write format to the best one chosen */
580  if (ast_set_write_format(bridge_channel->chan, best_format)) {
581  ast_log(LOG_WARNING, "Failed to set channel %s to write format %s\n", bridge_channel->chan->name, ast_getformatname(best_format));
582  return -1;
583  }
584  ast_debug(1, "Bridge %p put channel %s into write format %s\n", bridge, bridge_channel->chan->name, ast_getformatname(best_format));
585  } else {
586  ast_debug(1, "Bridge %p is happy that channel %s already has write format %s\n", bridge, bridge_channel->chan->name, ast_getformatname(formats[1]));
587  }
588 
589  return 0;
590 }
591 
592 /*! \brief Perform the smart bridge operation. Basically sees if a new bridge technology should be used instead of the current one. */
593 static int smart_bridge_operation(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, int count)
594 {
595  enum ast_bridge_capability new_capabilities = 0;
596  struct ast_bridge_technology *new_technology = NULL, *old_technology = bridge->technology;
597  struct ast_bridge temp_bridge = {
598  .technology = bridge->technology,
599  .bridge_pvt = bridge->bridge_pvt,
600  };
601  struct ast_bridge_channel *bridge_channel2 = NULL;
602 
603  /* Based on current feature determine whether we want to change bridge technologies or not */
605  if (count <= 2) {
606  ast_debug(1, "Bridge %p channel count (%d) is within limits for bridge technology %s, not performing smart bridge operation.\n", bridge, count, bridge->technology->name);
607  return 0;
608  }
609  new_capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX;
611  if (count > 2) {
612  ast_debug(1, "Bridge %p channel count (%d) is within limits for bridge technology %s, not performing smart bridge operation.\n", bridge, count, bridge->technology->name);
613  return 0;
614  }
615  new_capabilities = AST_BRIDGE_CAPABILITY_1TO1MIX;
616  }
617 
618  if (!new_capabilities) {
619  ast_debug(1, "Bridge '%p' has no new capabilities, not performing smart bridge operation.\n", bridge);
620  return 0;
621  }
622 
623  /* Attempt to find a new bridge technology to satisfy the capabilities */
624  if (!(new_technology = find_best_technology(new_capabilities))) {
625  ast_debug(1, "Smart bridge operation was unable to find new bridge technology with capabilities %u to satisfy bridge %p\n", new_capabilities, bridge);
626  return -1;
627  }
628 
629  ast_debug(1, "Performing smart bridge operation on bridge %p, moving from bridge technology %s to %s\n", bridge, old_technology->name, new_technology->name);
630 
631  /* If a thread is currently executing for the current technology tell it to stop */
632  if (bridge->thread != AST_PTHREADT_NULL) {
633  /* If the new bridge technology also needs a thread simply tell the bridge thread to refresh itself. This has the benefit of not incurring the cost/time of tearing down and bringing up a new thread. */
634  if (new_technology->capabilities & AST_BRIDGE_CAPABILITY_THREAD) {
635  ast_debug(1, "Telling current bridge thread for bridge %p to refresh\n", bridge);
636  bridge->refresh = 1;
637  bridge_poke(bridge);
638  } else {
639  pthread_t bridge_thread = bridge->thread;
640  ast_debug(1, "Telling current bridge thread for bridge %p to stop\n", bridge);
641  bridge->stop = 1;
642  bridge_poke(bridge);
643  ao2_unlock(bridge);
644  pthread_join(bridge_thread, NULL);
645  ao2_lock(bridge);
646  }
647  }
648 
649  /* Since we are soon going to pass this bridge to a new technology we need to NULL out the bridge_pvt pointer but don't worry as it still exists in temp_bridge, ditto for the old technology */
650  bridge->bridge_pvt = NULL;
651  bridge->technology = new_technology;
652 
653  /* Pass the bridge to the new bridge technology so it can set it up */
654  if (new_technology->create) {
655  ast_debug(1, "Giving bridge technology %s the bridge structure %p to setup\n", new_technology->name, bridge);
656  if (new_technology->create(bridge)) {
657  ast_debug(1, "Bridge technology %s failed to setup bridge structure %p\n", new_technology->name, bridge);
658  }
659  }
660 
661  /* Move existing channels over to the new technology, while taking them away from the old one */
662  AST_LIST_TRAVERSE(&bridge->channels, bridge_channel2, entry) {
663  /* Skip over channel that initiated the smart bridge operation */
664  if (bridge_channel == bridge_channel2) {
665  continue;
666  }
667 
668  /* First we part them from the old technology */
669  if (old_technology->leave) {
670  ast_debug(1, "Giving bridge technology %s notification that %p is leaving bridge %p (really %p)\n", old_technology->name, bridge_channel2, &temp_bridge, bridge);
671  if (old_technology->leave(&temp_bridge, bridge_channel2)) {
672  ast_debug(1, "Bridge technology %s failed to allow %p (really %p) to leave bridge %p\n", old_technology->name, bridge_channel2, &temp_bridge, bridge);
673  }
674  }
675 
676  /* Second we make them compatible again with the bridge */
677  bridge_make_compatible(bridge, bridge_channel2);
678 
679  /* Third we join them to the new technology */
680  if (new_technology->join) {
681  ast_debug(1, "Giving bridge technology %s notification that %p is joining bridge %p\n", new_technology->name, bridge_channel2, bridge);
682  if (new_technology->join(bridge, bridge_channel2)) {
683  ast_debug(1, "Bridge technology %s failed to join %p to bridge %p\n", new_technology->name, bridge_channel2, bridge);
684  }
685  }
686 
687  /* Fourth we tell them to wake up so they become aware that they above has happened */
688  pthread_kill(bridge_channel2->thread, SIGURG);
689  ast_mutex_lock(&bridge_channel2->lock);
690  ast_cond_signal(&bridge_channel2->cond);
691  ast_mutex_unlock(&bridge_channel2->lock);
692  }
693 
694  /* Now that all the channels have been moved over we need to get rid of all the information the old technology may have left around */
695  if (old_technology->destroy) {
696  ast_debug(1, "Giving bridge technology %s the bridge structure %p (really %p) to destroy\n", old_technology->name, &temp_bridge, bridge);
697  if (old_technology->destroy(&temp_bridge)) {
698  ast_debug(1, "Bridge technology %s failed to destroy bridge structure %p (really %p)... some memory may have leaked\n", old_technology->name, &temp_bridge, bridge);
699  }
700  }
701 
702  /* Finally if the old technology has module referencing remove our reference, we are no longer going to use it */
703  if (old_technology->mod) {
704  ast_module_unref(old_technology->mod);
705  }
706 
707  return 0;
708 }
709 
710 /*! \brief Run in a multithreaded model. Each joined channel does writing/reading in their own thread. TODO: Improve */
712 {
713  int fds[4] = { -1, }, nfds = 0, i = 0, outfd = -1, ms = -1;
714  struct ast_channel *chan = NULL;
715 
716  /* Add any file descriptors we may want to monitor */
717  if (bridge_channel->bridge->technology->fd) {
718  for (i = 0; i < 4; i ++) {
719  if (bridge_channel->fds[i] >= 0) {
720  fds[nfds++] = bridge_channel->fds[i];
721  }
722  }
723  }
724 
725  ao2_unlock(bridge_channel->bridge);
726 
727  /* Wait for data to either come from the channel or us to be signalled */
728  if (!bridge_channel->suspended) {
729  ast_debug(1, "Going into a multithreaded waitfor for bridge channel %p of bridge %p\n", bridge_channel, bridge_channel->bridge);
730  chan = ast_waitfor_nandfds(&bridge_channel->chan, 1, fds, nfds, NULL, &outfd, &ms);
731  } else {
732  ast_mutex_lock(&bridge_channel->lock);
733  ast_debug(1, "Going into a multithreaded signal wait for bridge channel %p of bridge %p\n", bridge_channel, bridge_channel->bridge);
734  ast_cond_wait(&bridge_channel->cond, &bridge_channel->lock);
735  ast_mutex_unlock(&bridge_channel->lock);
736  }
737 
738  ao2_lock(bridge_channel->bridge);
739 
740  if (!bridge_channel->suspended) {
741  ast_bridge_handle_trip(bridge_channel->bridge, bridge_channel, chan, outfd);
742  }
743 
744  return bridge_channel->state;
745 }
746 
747 /*! \brief Run in a singlethreaded model. Each joined channel yields itself to the main bridge thread. TODO: Improve */
749 {
750  ao2_unlock(bridge_channel->bridge);
751  ast_mutex_lock(&bridge_channel->lock);
752  if (bridge_channel->state == AST_BRIDGE_CHANNEL_STATE_WAIT) {
753  ast_debug(1, "Going into a single threaded signal wait for bridge channel %p of bridge %p\n", bridge_channel, bridge_channel->bridge);
754  ast_cond_wait(&bridge_channel->cond, &bridge_channel->lock);
755  }
756  ast_mutex_unlock(&bridge_channel->lock);
757  ao2_lock(bridge_channel->bridge);
758 
759  return bridge_channel->state;
760 }
761 
762 /*! \brief Internal function that suspends a channel from a bridge */
763 static void bridge_channel_suspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
764 {
765  bridge_channel->suspended = 1;
766 
767  bridge_array_remove(bridge, bridge_channel->chan);
768 
769  if (bridge->technology->suspend) {
770  bridge->technology->suspend(bridge, bridge_channel);
771  }
772 
773  return;
774 }
775 
776 /*! \brief Internal function that unsuspends a channel from a bridge */
777 static void bridge_channel_unsuspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
778 {
779  bridge_channel->suspended =0;
780 
781  bridge_array_add(bridge, bridge_channel->chan);
782 
783  if (bridge->technology->unsuspend) {
784  bridge->technology->unsuspend(bridge, bridge_channel);
785  }
786 
787  return;
788 }
789 
790 /*! \brief Internal function that executes a feature on a bridge channel */
791 static void bridge_channel_feature(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
792 {
793  struct ast_bridge_features *features = (bridge_channel->features ? bridge_channel->features : &bridge->features);
794  struct ast_bridge_features_hook *hook = NULL;
796  int look_for_dtmf = 1, dtmf_len = 0;
797 
798  /* 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 */
799  ast_set_flag(bridge_channel->chan, AST_FLAG_END_DTMF_ONLY);
800 
801  /* Wait for DTMF on the channel and put it into a buffer. If the buffer matches any feature hook execute the hook. */
802  while (look_for_dtmf) {
803  int res = ast_waitfordigit(bridge_channel->chan, 3000);
804 
805  /* If the above timed out simply exit */
806  if (!res) {
807  ast_debug(1, "DTMF feature string collection on bridge channel %p timed out\n", bridge_channel);
808  break;
809  } else if (res < 0) {
810  ast_debug(1, "DTMF feature string collection failed on bridge channel %p for some reason\n", bridge_channel);
811  break;
812  }
813 
814  /* Add the above DTMF into the DTMF string so we can do our matching */
815  dtmf[dtmf_len++] = res;
816 
817  ast_debug(1, "DTMF feature string on bridge channel %p is now '%s'\n", bridge_channel, dtmf);
818 
819  /* Assume that we do not want to look for DTMF any longer */
820  look_for_dtmf = 0;
821 
822  /* See if a DTMF feature hook matches or can match */
823  AST_LIST_TRAVERSE(&features->hooks, hook, entry) {
824  /* If this hook matches just break out now */
825  if (!strcmp(hook->dtmf, dtmf)) {
826  ast_debug(1, "DTMF feature hook %p matched DTMF string '%s' on bridge channel %p\n", hook, dtmf, bridge_channel);
827  break;
828  } else if (!strncmp(hook->dtmf, dtmf, dtmf_len)) {
829  ast_debug(1, "DTMF feature hook %p can match DTMF string '%s', it wants '%s', on bridge channel %p\n", hook, dtmf, hook->dtmf, bridge_channel);
830  look_for_dtmf = 1;
831  } else {
832  ast_debug(1, "DTMF feature hook %p does not match DTMF string '%s', it wants '%s', on bridge channel %p\n", hook, dtmf, hook->dtmf, bridge_channel);
833  }
834  }
835 
836  /* If we have reached the maximum length of a DTMF feature string bail out */
837  if (dtmf_len == MAXIMUM_DTMF_FEATURE_STRING) {
838  break;
839  }
840  }
841 
842  /* Since we are done bringing DTMF in return to using both begin and end frames */
843  ast_clear_flag(bridge_channel->chan, AST_FLAG_END_DTMF_ONLY);
844 
845  /* If a hook was actually matched execute it on this channel, otherwise stream up the DTMF to the other channels */
846  if (hook) {
847  hook->callback(bridge, bridge_channel, hook->hook_pvt);
848  } else {
849  ast_bridge_dtmf_stream(bridge, dtmf, bridge_channel->chan);
851  }
852 
853  return;
854 }
855 
856 /*! \brief Internal function that plays back DTMF on a bridge channel */
857 static void bridge_channel_dtmf_stream(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
858 {
859  char dtmf_q[8] = "";
860 
861  ast_copy_string(dtmf_q, bridge_channel->dtmf_stream_q, sizeof(dtmf_q));
862  bridge_channel->dtmf_stream_q[0] = '\0';
863 
864  ast_debug(1, "Playing DTMF stream '%s' out to bridge channel %p\n", dtmf_q, bridge_channel);
865  ast_dtmf_stream(bridge_channel->chan, NULL, dtmf_q, 250, 0);
866 
868 
869  return;
870 }
871 
872 /*! \brief Join a channel to a bridge and handle anything the bridge may want us to do */
874 {
875  int formats[2] = { bridge_channel->chan->readformat, bridge_channel->chan->writeformat };
877 
878  /* Record the thread that will be the owner of us */
879  bridge_channel->thread = pthread_self();
880 
881  ast_debug(1, "Joining bridge channel %p to bridge %p\n", bridge_channel, bridge_channel->bridge);
882 
883  ao2_lock(bridge_channel->bridge);
884 
885  state = bridge_channel->state;
886 
887  /* Add channel into the bridge */
888  AST_LIST_INSERT_TAIL(&bridge_channel->bridge->channels, bridge_channel, entry);
889  bridge_channel->bridge->num++;
890 
891  bridge_array_add(bridge_channel->bridge, bridge_channel->chan);
892 
893  if (bridge_channel->swap) {
894  struct ast_bridge_channel *bridge_channel2 = NULL;
895 
896  /* If we are performing a swap operation we do not need to execute the smart bridge operation as the actual number of channels involved will not have changed, we just need to tell the other channel to leave */
897  if ((bridge_channel2 = find_bridge_channel(bridge_channel->bridge, bridge_channel->swap))) {
898  ast_debug(1, "Swapping bridge channel %p out from bridge %p so bridge channel %p can slip in\n", bridge_channel2, bridge_channel->bridge, bridge_channel);
900  }
901 
902  bridge_channel->swap = NULL;
903  } else if (ast_test_flag(&bridge_channel->bridge->feature_flags, AST_BRIDGE_FLAG_SMART)) {
904  /* Perform the smart bridge operation, basically see if we need to move around between technologies */
905  smart_bridge_operation(bridge_channel->bridge, bridge_channel, bridge_channel->bridge->num);
906  }
907 
908  /* Make the channel compatible with the bridge */
909  bridge_make_compatible(bridge_channel->bridge, bridge_channel);
910 
911  /* Tell the bridge technology we are joining so they set us up */
912  if (bridge_channel->bridge->technology->join) {
913  ast_debug(1, "Giving bridge technology %s notification that %p is joining bridge %p\n", bridge_channel->bridge->technology->name, bridge_channel, bridge_channel->bridge);
914  if (bridge_channel->bridge->technology->join(bridge_channel->bridge, bridge_channel)) {
915  ast_debug(1, "Bridge technology %s failed to join %p to bridge %p\n", bridge_channel->bridge->technology->name, bridge_channel, bridge_channel->bridge);
916  }
917  }
918 
919  /* Actually execute the respective threading model, and keep our bridge thread alive */
920  while (bridge_channel->state == AST_BRIDGE_CHANNEL_STATE_WAIT) {
921  /* Update bridge pointer on channel */
922  bridge_channel->chan->bridge = bridge_channel->bridge;
923  /* If the technology requires a thread and one is not running, start it up */
924  if (bridge_channel->bridge->thread == AST_PTHREADT_NULL && (bridge_channel->bridge->technology->capabilities & AST_BRIDGE_CAPABILITY_THREAD)) {
925  bridge_channel->bridge->stop = 0;
926  ast_debug(1, "Starting a bridge thread for bridge %p\n", bridge_channel->bridge);
927  ao2_ref(bridge_channel->bridge, +1);
928  if (ast_pthread_create(&bridge_channel->bridge->thread, NULL, bridge_thread, bridge_channel->bridge)) {
929  ast_debug(1, "Failed to create a bridge thread for bridge %p, giving it another go.\n", bridge_channel->bridge);
930  ao2_ref(bridge_channel->bridge, -1);
931  continue;
932  }
933  }
934  /* Execute the threading model */
936  /* Depending on the above state see what we need to do */
937  if (state == AST_BRIDGE_CHANNEL_STATE_FEATURE) {
938  bridge_channel_suspend(bridge_channel->bridge, bridge_channel);
939  bridge_channel_feature(bridge_channel->bridge, bridge_channel);
940  bridge_channel_unsuspend(bridge_channel->bridge, bridge_channel);
941  } else if (state == AST_BRIDGE_CHANNEL_STATE_DTMF) {
942  bridge_channel_suspend(bridge_channel->bridge, bridge_channel);
943  bridge_channel_dtmf_stream(bridge_channel->bridge, bridge_channel);
944  bridge_channel_unsuspend(bridge_channel->bridge, bridge_channel);
945  }
946  }
947 
948  bridge_channel->chan->bridge = NULL;
949 
950  /* See if we need to dissolve the bridge itself if they hung up */
951  if (bridge_channel->state == AST_BRIDGE_CHANNEL_STATE_END) {
952  bridge_check_dissolve(bridge_channel->bridge, bridge_channel);
953  }
954 
955  /* Tell the bridge technology we are leaving so they tear us down */
956  if (bridge_channel->bridge->technology->leave) {
957  ast_debug(1, "Giving bridge technology %s notification that %p is leaving bridge %p\n", bridge_channel->bridge->technology->name, bridge_channel, bridge_channel->bridge);
958  if (bridge_channel->bridge->technology->leave(bridge_channel->bridge, bridge_channel)) {
959  ast_debug(1, "Bridge technology %s failed to leave %p from bridge %p\n", bridge_channel->bridge->technology->name, bridge_channel, bridge_channel->bridge);
960  }
961  }
962 
963  /* Remove channel from the bridge */
964  bridge_channel->bridge->num--;
965  AST_LIST_REMOVE(&bridge_channel->bridge->channels, bridge_channel, entry);
966 
967  bridge_array_remove(bridge_channel->bridge, bridge_channel->chan);
968 
969  /* Perform the smart bridge operation if needed since a channel has left */
970  if (ast_test_flag(&bridge_channel->bridge->feature_flags, AST_BRIDGE_FLAG_SMART)) {
971  smart_bridge_operation(bridge_channel->bridge, NULL, bridge_channel->bridge->num);
972  }
973 
974  ao2_unlock(bridge_channel->bridge);
975 
976  /* Restore original formats of the channel as they came in */
977  if (bridge_channel->chan->readformat != formats[0]) {
978  ast_debug(1, "Bridge is returning %p to read format %s(%d)\n", bridge_channel, ast_getformatname(formats[0]), formats[0]);
979  if (ast_set_read_format(bridge_channel->chan, formats[0])) {
980  ast_debug(1, "Bridge failed to return channel %p to read format %s(%d)\n", bridge_channel, ast_getformatname(formats[0]), formats[0]);
981  }
982  }
983  if (bridge_channel->chan->writeformat != formats[1]) {
984  ast_debug(1, "Bridge is returning %p to write format %s(%d)\n", bridge_channel, ast_getformatname(formats[1]), formats[1]);
985  if (ast_set_write_format(bridge_channel->chan, formats[1])) {
986  ast_debug(1, "Bridge failed to return channel %p to write format %s(%d)\n", bridge_channel, ast_getformatname(formats[1]), formats[1]);
987  }
988  }
989 
990  return bridge_channel->state;
991 }
992 
994 {
995  struct ast_bridge_channel bridge_channel = {
996  .chan = chan,
997  .swap = swap,
998  .bridge = bridge,
999  .features = features,
1000  };
1002 
1003  /* Initialize various other elements of the bridge channel structure that we can't do above */
1004  ast_mutex_init(&bridge_channel.lock);
1005  ast_cond_init(&bridge_channel.cond, NULL);
1006 
1007  ao2_ref(bridge_channel.bridge, +1);
1008 
1009  state = bridge_channel_join(&bridge_channel);
1010 
1011  ao2_ref(bridge_channel.bridge, -1);
1012 
1013  /* Destroy some elements of the bridge channel structure above */
1014  ast_mutex_destroy(&bridge_channel.lock);
1015  ast_cond_destroy(&bridge_channel.cond);
1016 
1017  return state;
1018 }
1019 
1020 /*! \brief Thread responsible for imparted bridged channels */
1021 static void *bridge_channel_thread(void *data)
1022 {
1023  struct ast_bridge_channel *bridge_channel = data;
1025 
1026  state = bridge_channel_join(bridge_channel);
1027 
1028  ao2_ref(bridge_channel->bridge, -1);
1029 
1030  /* If no other thread is going to take the channel then hang it up, or else we would have to service it until something else came along */
1032  ast_hangup(bridge_channel->chan);
1033  }
1034 
1035  /* Destroy elements of the bridge channel structure and the bridge channel structure itself */
1036  ast_mutex_destroy(&bridge_channel->lock);
1037  ast_cond_destroy(&bridge_channel->cond);
1038  ast_free(bridge_channel);
1039 
1040  return NULL;
1041 }
1042 
1044 {
1045  struct ast_bridge_channel *bridge_channel = NULL;
1046 
1047  /* Try to allocate a structure for the bridge channel */
1048  if (!(bridge_channel = ast_calloc(1, sizeof(*bridge_channel)))) {
1049  return -1;
1050  }
1051 
1052  /* Setup various parameters */
1053  bridge_channel->chan = chan;
1054  bridge_channel->swap = swap;
1055  bridge_channel->bridge = bridge;
1056  bridge_channel->features = features;
1057 
1058  /* Initialize our mutex lock and condition */
1059  ast_mutex_init(&bridge_channel->lock);
1060  ast_cond_init(&bridge_channel->cond, NULL);
1061 
1062  /* Bump up the reference count on the bridge, it'll get decremented later */
1063  ao2_ref(bridge, +1);
1064 
1065  /* Actually create the thread that will handle the channel */
1066  if (ast_pthread_create(&bridge_channel->thread, NULL, bridge_channel_thread, bridge_channel)) {
1067  ao2_ref(bridge, -1);
1068  ast_cond_destroy(&bridge_channel->cond);
1069  ast_mutex_destroy(&bridge_channel->lock);
1070  ast_free(bridge_channel);
1071  return -1;
1072  }
1073 
1074  return 0;
1075 }
1076 
1078 {
1079  struct ast_bridge_channel *bridge_channel = NULL;
1080  pthread_t thread;
1081 
1082  ao2_lock(bridge);
1083 
1084  /* Try to find the channel that we want to depart */
1085  if (!(bridge_channel = find_bridge_channel(bridge, chan))) {
1086  ao2_unlock(bridge);
1087  return -1;
1088  }
1089 
1091  thread = bridge_channel->thread;
1092 
1093  ao2_unlock(bridge);
1094 
1095  pthread_join(thread, NULL);
1096 
1097  return 0;
1098 }
1099 
1101 {
1102  struct ast_bridge_channel *bridge_channel = NULL;
1103 
1104  ao2_lock(bridge);
1105 
1106  /* Try to find the channel that we want to remove */
1107  if (!(bridge_channel = find_bridge_channel(bridge, chan))) {
1108  ao2_unlock(bridge);
1109  return -1;
1110  }
1111 
1113 
1114  ao2_unlock(bridge);
1115 
1116  return 0;
1117 }
1118 
1119 int ast_bridge_merge(struct ast_bridge *bridge0, struct ast_bridge *bridge1)
1120 {
1121  struct ast_bridge_channel *bridge_channel = NULL;
1122 
1123  ao2_lock(bridge0);
1124  ao2_lock(bridge1);
1125 
1126  /* If the first bridge currently has 2 channels and is not capable of becoming a multimixing bridge we can not merge */
1127  if ((bridge0->num + bridge1->num) > 2 && (!(bridge0->technology->capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX) && !ast_test_flag(&bridge0->feature_flags, AST_BRIDGE_FLAG_SMART))) {
1128  ao2_unlock(bridge1);
1129  ao2_unlock(bridge0);
1130  ast_debug(1, "Can't merge bridge %p into bridge %p, multimix is needed and it could not be acquired.\n", bridge1, bridge0);
1131  return -1;
1132  }
1133 
1134  ast_debug(1, "Merging channels from bridge %p into bridge %p\n", bridge1, bridge0);
1135 
1136  /* Perform smart bridge operation on bridge we are merging into so it can change bridge technology if needed */
1137  if (smart_bridge_operation(bridge0, NULL, bridge0->num + bridge1->num)) {
1138  ao2_unlock(bridge1);
1139  ao2_unlock(bridge0);
1140  ast_debug(1, "Can't merge bridge %p into bridge %p, tried to perform smart bridge operation and failed.\n", bridge1, bridge0);
1141  return -1;
1142  }
1143 
1144  /* If a thread is currently executing on bridge1 tell it to stop */
1145  if (bridge1->thread) {
1146  ast_debug(1, "Telling bridge thread on bridge %p to stop as it is being merged into %p\n", bridge1, bridge0);
1147  bridge1->thread = AST_PTHREADT_STOP;
1148  }
1149 
1150  /* Move channels from bridge1 over to bridge0 */
1151  while ((bridge_channel = AST_LIST_REMOVE_HEAD(&bridge1->channels, entry))) {
1152  /* Tell the technology handling bridge1 that the bridge channel is leaving */
1153  if (bridge1->technology->leave) {
1154  ast_debug(1, "Giving bridge technology %s notification that %p is leaving bridge %p\n", bridge1->technology->name, bridge_channel, bridge1);
1155  if (bridge1->technology->leave(bridge1, bridge_channel)) {
1156  ast_debug(1, "Bridge technology %s failed to allow %p to leave bridge %p\n", bridge1->technology->name, bridge_channel, bridge1);
1157  }
1158  }
1159 
1160  /* Drop channel count and reference count on the bridge they are leaving */
1161  bridge1->num--;
1162  ao2_ref(bridge1, -1);
1163 
1164  bridge_array_remove(bridge1, bridge_channel->chan);
1165 
1166  /* Now add them into the bridge they are joining, increase channel count, and bump up reference count */
1167  bridge_channel->bridge = bridge0;
1168  AST_LIST_INSERT_TAIL(&bridge0->channels, bridge_channel, entry);
1169  bridge0->num++;
1170  ao2_ref(bridge0, +1);
1171 
1172  bridge_array_add(bridge0, bridge_channel->chan);
1173 
1174  /* Make the channel compatible with the new bridge it is joining or else formats would go amuck */
1175  bridge_make_compatible(bridge0, bridge_channel);
1176 
1177  /* Tell the technology handling bridge0 that the bridge channel is joining */
1178  if (bridge0->technology->join) {
1179  ast_debug(1, "Giving bridge technology %s notification that %p is joining bridge %p\n", bridge0->technology->name, bridge_channel, bridge0);
1180  if (bridge0->technology->join(bridge0, bridge_channel)) {
1181  ast_debug(1, "Bridge technology %s failed to join %p to bridge %p\n", bridge0->technology->name, bridge_channel, bridge0);
1182  }
1183  }
1184 
1185  /* Poke the bridge channel, this will cause it to wake up and execute the proper threading model for the new bridge it is in */
1186  pthread_kill(bridge_channel->thread, SIGURG);
1187  ast_mutex_lock(&bridge_channel->lock);
1188  ast_cond_signal(&bridge_channel->cond);
1189  ast_mutex_unlock(&bridge_channel->lock);
1190  }
1191 
1192  ast_debug(1, "Merged channels from bridge %p into bridge %p\n", bridge1, bridge0);
1193 
1194  ao2_unlock(bridge1);
1195  ao2_unlock(bridge0);
1196 
1197  return 0;
1198 }
1199 
1201 {
1202  struct ast_bridge_channel *bridge_channel;
1203 
1204  ao2_lock(bridge);
1205 
1206  if (!(bridge_channel = find_bridge_channel(bridge, chan))) {
1207  ao2_unlock(bridge);
1208  return -1;
1209  }
1210 
1211  bridge_channel_suspend(bridge, bridge_channel);
1212 
1213  ao2_unlock(bridge);
1214 
1215  return 0;
1216 }
1217 
1219 {
1220  struct ast_bridge_channel *bridge_channel;
1221 
1222  ao2_lock(bridge);
1223 
1224  if (!(bridge_channel = find_bridge_channel(bridge, chan))) {
1225  ao2_unlock(bridge);
1226  return -1;
1227  }
1228 
1229  bridge_channel_unsuspend(bridge, bridge_channel);
1230 
1231  ao2_unlock(bridge);
1232 
1233  return 0;
1234 }
1235 
1237 {
1238  technology->suspended = 1;
1239  return;
1240 }
1241 
1243 {
1244  technology->suspended = 0;
1245  return;
1246 }
1247 
1249 {
1250  if (builtin_features_handlers[feature]) {
1251  return -1;
1252  }
1253 
1254  if (!ast_strlen_zero(dtmf)) {
1255  ast_copy_string(builtin_features_dtmf[feature], dtmf, sizeof(builtin_features_dtmf[feature]));
1256  }
1257 
1258  builtin_features_handlers[feature] = callback;
1259 
1260  return 0;
1261 }
1262 
1264 {
1265  if (!builtin_features_handlers[feature]) {
1266  return -1;
1267  }
1268 
1269  builtin_features_handlers[feature] = NULL;
1270 
1271  return 0;
1272 }
1273 
1274 int ast_bridge_features_hook(struct ast_bridge_features *features, const char *dtmf, ast_bridge_features_hook_callback callback, void *hook_pvt)
1275 {
1276  struct ast_bridge_features_hook *hook = NULL;
1277 
1278  /* Allocate new memory and setup it's various variables */
1279  if (!(hook = ast_calloc(1, sizeof(*hook)))) {
1280  return -1;
1281  }
1282 
1283  ast_copy_string(hook->dtmf, dtmf, sizeof(hook->dtmf));
1284  hook->callback = callback;
1285  hook->hook_pvt = hook_pvt;
1286 
1287  /* Once done we add it onto the list. Now it will be picked up when DTMF is used */
1288  AST_LIST_INSERT_TAIL(&features->hooks, hook, entry);
1289 
1290  features->usable = 1;
1291 
1292  return 0;
1293 }
1294 
1295 int ast_bridge_features_enable(struct ast_bridge_features *features, enum ast_bridge_builtin_feature feature, const char *dtmf, void *config)
1296 {
1297  /* If no alternate DTMF stream was provided use the default one */
1298  if (ast_strlen_zero(dtmf)) {
1299  dtmf = builtin_features_dtmf[feature];
1300  /* If no DTMF is still available (ie: it has been disabled) then error out now */
1301  if (ast_strlen_zero(dtmf)) {
1302  ast_debug(1, "Failed to enable built in feature %u on %p, no DTMF string is available for it.\n", feature, features);
1303  return -1;
1304  }
1305  }
1306 
1307  if (!builtin_features_handlers[feature]) {
1308  return -1;
1309  }
1310 
1311  /* The rest is basically pretty easy. We create another hook using the built in feature's callback and DTMF, easy as pie. */
1312  return ast_bridge_features_hook(features, dtmf, builtin_features_handlers[feature], config);
1313 }
1314 
1316 {
1317  ast_set_flag(&features->feature_flags, flag);
1318  features->usable = 1;
1319  return 0;
1320 }
1321 
1323 {
1324  /* Zero out the structure */
1325  memset(features, 0, sizeof(*features));
1326 
1327  /* Initialize the hooks list, just in case */
1328  AST_LIST_HEAD_INIT_NOLOCK(&features->hooks);
1329 
1330  return 0;
1331 }
1332 
1334 {
1335  struct ast_bridge_features_hook *hook = NULL;
1336 
1337  /* This is relatively simple, hooks are kept as a list on the features structure so we just pop them off and free them */
1338  while ((hook = AST_LIST_REMOVE_HEAD(&features->hooks, entry))) {
1339  ast_free(hook);
1340  }
1341 
1342  return 0;
1343 }
1344 
1345 int ast_bridge_dtmf_stream(struct ast_bridge *bridge, const char *dtmf, struct ast_channel *chan)
1346 {
1347  struct ast_bridge_channel *bridge_channel = NULL;
1348 
1349  ao2_lock(bridge);
1350 
1351  AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
1352  if (bridge_channel->chan == chan) {
1353  continue;
1354  }
1355  ast_copy_string(bridge_channel->dtmf_stream_q, dtmf, sizeof(bridge_channel->dtmf_stream_q));
1357  }
1358 
1359  ao2_unlock(bridge);
1360 
1361  return 0;
1362 }
int(* ast_bridge_features_hook_callback)(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
Features hook callback type.
static void bridge_channel_unsuspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
Internal function that unsuspends a channel from a bridge.
Definition: bridging.c:777
ast_bridge_capability
Capabilities for a bridge technology.
Definition: bridging.h:68
struct ast_channel * ast_waitfor_n(struct ast_channel **chan, int n, int *ms)
Waits for input on a group of channels Wait for input on an array of channels for a given # of millis...
Definition: channel.c:3534
static void destroy_bridge(void *obj)
Definition: bridging.c:421
union ast_frame_subclass subclass
Definition: frame.h:146
int ast_hangup(struct ast_channel *chan)
Hang up a channel.
Definition: channel.c:2804
enum sip_cc_notify_state state
Definition: chan_sip.c:842
pthread_t thread
Definition: app_meetme.c:962
Main Channel structure associated with a channel.
Definition: channel.h:742
char dtmf_stream_q[8]
Definition: bridging.h:141
static int generic_thread_loop(struct ast_bridge *bridge)
Generic thread loop, TODO: Rethink this/improve it.
Definition: bridging.c:326
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
struct ast_flags feature_flags
Definition: bridging.h:159
static void bridge_channel_dtmf_stream(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
Internal function that plays back DTMF on a bridge channel.
Definition: bridging.c:857
struct ast_bridge_features * features
Definition: bridging.h:139
static int bridge_make_compatible(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
Definition: bridging.c:544
static const char config[]
Definition: cdr_csv.c:57
void ast_module_unref(struct ast_module *)
Definition: loader.c:1312
#define AST_RWLIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a read/write list of specified type, statically initialized...
Definition: linkedlists.h:332
pthread_t thread
Definition: bridging.h:133
int ast_bridge_check(enum ast_bridge_capability capabilities)
See if it is possible to create a bridge.
Definition: bridging.c:502
format_t writeformat
Definition: channel.h:854
enum ast_bridge_channel_state ast_bridge_join(struct ast_bridge *bridge, struct ast_channel *chan, struct ast_channel *swap, struct ast_bridge_features *features)
Join (blocking) a channel to a bridge.
Definition: bridging.c:993
Structure that contains features information.
#define ast_test_flag(p, flag)
Definition: utils.h:63
int option_debug
Definition: asterisk.c:182
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
void ast_bridge_technology_suspend(struct ast_bridge_technology *technology)
Suspend a bridge technology from consideration.
Definition: bridging.c:1236
#define ast_set_flag(p, flag)
Definition: utils.h:70
#define LOG_WARNING
Definition: logger.h:144
unsigned int waiting
Definition: bridging.h:153
static struct aji_capabilities * capabilities
Definition: res_jabber.c:393
void ast_verbose(const char *fmt,...)
Definition: logger.c:1568
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
void(* suspend)(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
unsigned int suspended
Definition: bridging.h:137
struct ast_frame * ast_read(struct ast_channel *chan)
Reads a frame.
Definition: channel.c:4383
struct ast_bridge * ast_bridge_new(enum ast_bridge_capability capabilities, int flags)
Create a new bridge.
Definition: bridging.c:449
int ast_bridge_features_enable(struct ast_bridge_features *features, enum ast_bridge_builtin_feature feature, const char *dtmf, void *config)
Enable a built in feature on a bridge features structure.
Definition: bridging.c:1295
int ast_bridge_depart(struct ast_bridge *bridge, struct ast_channel *chan)
Depart a channel from a bridge.
Definition: bridging.c:1077
int option_verbose
Definition: asterisk.c:181
static void * bridge_channel_thread(void *data)
Thread responsible for imparted bridged channels.
Definition: bridging.c:1021
int ast_bridge_features_init(struct ast_bridge_features *features)
Initialize bridge features structure.
Definition: bridging.c:1322
#define ast_cond_wait(cond, mutex)
Definition: lock.h:171
#define ast_cond_init(cond, attr)
Definition: lock.h:167
format_t ast_best_codec(format_t fmts)
Pick the best audio codec.
Definition: channel.c:1062
int ast_bridge_destroy(struct ast_bridge *bridge)
Destroy a bridge.
Definition: bridging.c:515
static int bridge_drop_control_frame(int subclass)
Internal function used to determine whether a control frame should be dropped or not.
Definition: bridging.c:266
#define ast_mutex_lock(a)
Definition: lock.h:155
#define ao2_unlock(a)
Definition: astobj2.h:497
int ast_bridge_features_unregister(enum ast_bridge_builtin_feature feature)
Unregister a handler for a built in feature.
Definition: bridging.c:1263
int(* poke)(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h.
static int smart_bridge_operation(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, int count)
Perform the smart bridge operation. Basically sees if a new bridge technology should be used instead ...
Definition: bridging.c:593
const char * data
Definition: channel.h:755
#define AST_LIST_REMOVE(head, elm, field)
Removes a specific entry from a list.
Definition: linkedlists.h:841
#define ast_cond_signal(cond)
Definition: lock.h:169
struct ast_channel * ast_waitfor_nandfds(struct ast_channel **chan, int n, int *fds, int nfds, int *exception, int *outfd, int *ms)
Waits for activity on a group of channels.
Definition: channel.c:3188
void(* unsuspend)(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
struct ast_bridge * bridge
Definition: bridging.h:129
static void * bridge_thread(void *data)
Bridge thread function.
Definition: bridging.c:354
struct ast_bridge_features features
Definition: bridging.h:167
char dtmf[MAXIMUM_DTMF_FEATURE_STRING]
Utility functions.
int __ast_bridge_technology_register(struct ast_bridge_technology *technology, struct ast_module *mod)
Register a bridge technology for use.
Definition: bridging.c:63
static void bridge_array_add(struct ast_bridge *bridge, struct ast_channel *chan)
Helper function to add a channel to the bridge array.
Definition: bridging.c:153
int ast_bridge_features_hook(struct ast_bridge_features *features, const char *dtmf, ast_bridge_features_hook_callback callback, void *hook_pvt)
Attach a custom hook to a bridge features structure.
Definition: bridging.c:1274
int ast_set_write_format(struct ast_channel *chan, format_t format)
Sets write format on channel chan Set write format for channel to whichever component of &quot;format&quot; is ...
Definition: channel.c:5307
struct ast_bridge_technology * technology
Definition: bridging.h:161
struct ast_flags feature_flags
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
size_t array_size
Definition: bridging.h:173
int(* join)(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
int(* create)(struct ast_bridge *bridge)
size_t array_num
Definition: bridging.h:171
int ast_set_read_format(struct ast_channel *chan, format_t format)
Sets read format on channel chan Set read format for channel to whichever component of &quot;format&quot; is be...
Definition: channel.c:5301
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
void ast_bridge_change_state(struct ast_bridge_channel *bridge_channel, enum ast_bridge_channel_state new_state)
Change the state of a bridged channel.
Definition: bridging.c:122
void ast_bridge_technology_unsuspend(struct ast_bridge_technology *technology)
Unsuspend a bridge technology.
Definition: bridging.c:1242
unsigned int refresh
Definition: bridging.h:157
int ast_bridge_impart(struct ast_bridge *bridge, struct ast_channel *chan, struct ast_channel *swap, struct ast_bridge_features *features)
Impart (non-blocking) a channel on a bridge.
Definition: bridging.c:1043
General Asterisk PBX channel definitions.
#define VERBOSE_PREFIX_2
Definition: logger.h:42
int ast_bridge_merge(struct ast_bridge *bridge0, struct ast_bridge *bridge1)
Merge two bridges together.
Definition: bridging.c:1119
#define AST_PTHREADT_NULL
Definition: lock.h:65
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
static void bridge_poke(struct ast_bridge *bridge)
Helper function to poke the bridge thread.
Definition: bridging.c:139
Channel Bridging API.
#define BRIDGE_ARRAY_START
Definition: bridging.c:52
enum ast_bridge_preference preference
Channel Bridging API.
#define AST_RWLIST_TRAVERSE
Definition: linkedlists.h:493
int(* thread)(struct ast_bridge *bridge)
#define ao2_ref(o, delta)
Definition: astobj2.h:472
#define ao2_lock(a)
Definition: astobj2.h:488
#define AST_RWLIST_REMOVE_CURRENT
Definition: linkedlists.h:565
static struct ast_bridge_technology * find_best_technology(enum ast_bridge_capability capabilities)
Helper function used to find the &quot;best&quot; bridge technology given a specified capabilities.
Definition: bridging.c:386
unsigned int stop
Definition: bridging.h:155
A set of macros to manage forward-linked lists.
static struct ast_bridge_channel * find_bridge_channel(struct ast_bridge *bridge, struct ast_channel *chan)
Helper function to find a bridge channel given a channel.
Definition: bridging.c:208
int ast_bridge_suspend(struct ast_bridge *bridge, struct ast_channel *chan)
Suspend a channel temporarily from a bridge.
Definition: bridging.c:1200
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Definition: linkedlists.h:818
#define MAXIMUM_DTMF_FEATURE_STRING
Maximum length of a DTMF feature string.
static enum ast_bridge_channel_state bridge_channel_join_singlethreaded(struct ast_bridge_channel *bridge_channel)
Run in a singlethreaded model. Each joined channel yields itself to the main bridge thread...
Definition: bridging.c:748
int ast_bridge_technology_unregister(struct ast_bridge_technology *technology)
Unregister a bridge technology from use.
Definition: bridging.c:99
static void bridge_check_dissolve(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
Internal function to see whether a bridge should dissolve, and if so do it.
Definition: bridging.c:222
#define AST_RWLIST_TRAVERSE_SAFE_BEGIN
Definition: linkedlists.h:542
void ast_bridge_handle_trip(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_channel *chan, int outfd)
Feed notification that a frame is waiting on a channel into the bridging core.
Definition: bridging.c:277
ast_cond_t cond
Definition: bridging.h:121
static void bridge_channel_suspend(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
Internal function that suspends a channel from a bridge.
Definition: bridging.c:763
Structure that contains information about a bridge.
Definition: bridging.h:149
char * ast_getformatname(format_t format)
Get the name of a format.
Definition: frame.c:578
#define LOG_ERROR
Definition: logger.h:155
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:716
int64_t format_t
Definition: frame_defs.h:32
static void bridge_channel_feature(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
Internal function that executes a feature on a bridge channel.
Definition: bridging.c:791
int ast_bridge_remove(struct ast_bridge *bridge, struct ast_channel *chan)
Remove a channel from a bridge.
Definition: bridging.c:1100
struct ast_channel ** array
Definition: bridging.h:169
pthread_t thread
Definition: bridging.h:165
struct ast_module * mod
Definition: file.c:65
const ast_string_field name
Definition: channel.h:787
struct sla_ringing_trunk * first
Definition: app_meetme.c:965
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define ast_cond_destroy(cond)
Definition: lock.h:168
int(* destroy)(struct ast_bridge *bridge)
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:430
struct ast_bridge * bridge
Definition: channel.h:865
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:490
#define ast_free(a)
Definition: astmm.h:97
#define ast_pthread_create(a, b, c, d)
Definition: utils.h:418
static enum ast_bridge_channel_state bridge_channel_join(struct ast_bridge_channel *bridge_channel)
Join a channel to a bridge and handle anything the bridge may want us to do.
Definition: bridging.c:873
struct ast_channel * swap
Definition: bridging.h:127
#define ast_clear_flag(p, flag)
Definition: utils.h:77
enum ast_bridge_write_result(* write)(struct ast_bridge *bridge, struct ast_bridge_channel *bridged_channel, struct ast_frame *frame)
Support for logging to various files, console and syslog Configuration in file logger.conf.
static struct ast_frame * bridge_handle_dtmf(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
Internal function to handle DTMF from a channel.
Definition: bridging.c:242
int ast_waitfordigit(struct ast_channel *c, int ms)
Waits for a digit.
Definition: channel.c:3552
static void * builtin_features_handlers[AST_BRIDGE_BUILTIN_END]
Definition: bridging.c:61
int(* fd)(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, int fd)
#define AST_RWLIST_INSERT_TAIL
Definition: linkedlists.h:726
#define AST_LIST_HEAD_INIT_NOLOCK(head)
Initializes a list head structure.
Definition: linkedlists.h:666
struct ast_channel * chan
Definition: bridging.h:125
Structure that contains information regarding a channel in a bridge.
Definition: bridging.h:117
static void bridge_array_remove(struct ast_bridge *bridge, struct ast_channel *chan)
Helper function to remove a channel from the bridge array.
Definition: bridging.c:184
format_t readformat
Definition: channel.h:853
#define ast_calloc(a, b)
Definition: astmm.h:82
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
static enum ast_bridge_channel_state bridge_channel_join_multithreaded(struct ast_bridge_channel *bridge_channel)
Run in a multithreaded model. Each joined channel does writing/reading in their own thread...
Definition: bridging.c:711
ast_bridge_feature_flags
Flags used for bridge features.
ast_bridge_features_hook_callback callback
Structure that is the essence of a bridge technology.
#define ast_realloc(a, b)
Definition: astmm.h:103
#define AST_PTHREADT_STOP
Definition: lock.h:66
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
char * ast_getformatname_multiple(char *buf, size_t size, format_t format)
Get the names of a set of formats.
Definition: frame.c:591
Data structure associated with a single frame of data.
Definition: frame.h:142
Options provided by main asterisk program.
ast_bridge_channel_state
State information about a bridged channel.
Definition: bridging.h:86
ast_mutex_t lock
Definition: bridging.h:119
Structure that is the essence of a features hook.
enum ast_frame_type frametype
Definition: frame.h:144
#define ast_mutex_init(pmutex)
Definition: lock.h:152
#define ast_frfree(fr)
Definition: frame.h:583
#define BRIDGE_ARRAY_GROW
Definition: bridging.c:55
int ast_dtmf_stream(struct ast_channel *chan, struct ast_channel *peer, const char *digits, int between, unsigned int duration)
Send DTMF to a channel.
Definition: app.c:501
#define ast_mutex_destroy(a)
Definition: lock.h:154
int ast_bridge_features_register(enum ast_bridge_builtin_feature feature, ast_bridge_features_hook_callback callback, const char *dtmf)
Register a handler for a built in feature.
Definition: bridging.c:1248
static char builtin_features_dtmf[AST_BRIDGE_BUILTIN_END][MAXIMUM_DTMF_FEATURE_STRING]
Definition: bridging.c:58
int(* leave)(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
Asterisk module definitions.
enum ast_bridge_channel_state state
Definition: bridging.h:123
int ast_bridge_dtmf_stream(struct ast_bridge *bridge, const char *dtmf, struct ast_channel *chan)
Play a DTMF stream into a bridge, optionally not to a given channel.
Definition: bridging.c:1345
struct ast_frame * ast_read_noaudio(struct ast_channel *chan)
Reads a frame, returning AST_FRAME_NULL frame if audio.
Definition: channel.c:4388
int ast_bridge_features_cleanup(struct ast_bridge_features *features)
Clean up the contents of a bridge features structure.
Definition: bridging.c:1333
#define AST_RWLIST_TRAVERSE_SAFE_END
Definition: linkedlists.h:602
void * bridge_pvt
Definition: bridging.h:163
int ast_bridge_unsuspend(struct ast_bridge *bridge, struct ast_channel *chan)
Unsuspend a channel from a bridge.
Definition: bridging.c:1218
ast_bridge_builtin_feature
Built in features.
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
#define ast_mutex_unlock(a)
Definition: lock.h:156
int ast_bridge_features_set_flag(struct ast_bridge_features *features, enum ast_bridge_feature_flags flag)
Set a flag on a bridge features structure.
Definition: bridging.c:1315
struct ast_module * ast_module_ref(struct ast_module *)
Definition: loader.c:1300