bridging.c 58 KB

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