bridging.c 59 KB

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