bridge_channel.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457
  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 Bridging Channel API
  21. *
  22. * \author Joshua Colp <jcolp@digium.com>
  23. * \author Richard Mudgett <rmudgett@digium.com>
  24. * \author Matt Jordan <mjordan@digium.com>
  25. *
  26. */
  27. /*** MODULEINFO
  28. <support_level>core</support_level>
  29. ***/
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include <signal.h>
  33. #include <semaphore.h>
  34. #include "asterisk/heap.h"
  35. #include "asterisk/astobj2.h"
  36. #include "asterisk/stringfields.h"
  37. #include "asterisk/app.h"
  38. #include "asterisk/pbx.h"
  39. #include "asterisk/channel.h"
  40. #include "asterisk/timing.h"
  41. #include "asterisk/bridge.h"
  42. #include "asterisk/bridge_channel.h"
  43. #include "asterisk/bridge_after.h"
  44. #include "asterisk/bridge_channel_internal.h"
  45. #include "asterisk/bridge_internal.h"
  46. #include "asterisk/stasis_bridges.h"
  47. #include "asterisk/stasis_channels.h"
  48. #include "asterisk/musiconhold.h"
  49. #include "asterisk/features_config.h"
  50. #include "asterisk/parking.h"
  51. #include "asterisk/causes.h"
  52. #include "asterisk/test.h"
  53. /*!
  54. * \brief Used to queue an action frame onto a bridge channel and write an action frame into a bridge.
  55. * \since 12.0.0
  56. *
  57. * \param bridge_channel Which channel work with.
  58. * \param action Type of bridge action frame.
  59. * \param data Frame payload data to pass.
  60. * \param datalen Frame payload data length to pass.
  61. *
  62. * \retval 0 on success.
  63. * \retval -1 on error.
  64. */
  65. typedef int (*ast_bridge_channel_post_action_data)(struct ast_bridge_channel *bridge_channel, enum bridge_channel_action_type action, const void *data, size_t datalen);
  66. /*!
  67. * \brief Counter used for assigning synchronous bridge action IDs
  68. */
  69. static int sync_ids;
  70. /*!
  71. * \brief Frame payload for synchronous bridge actions.
  72. *
  73. * The payload serves as a wrapper around the actual payload of the
  74. * frame, with the addition of an id used to find the associated
  75. * bridge_sync object.
  76. */
  77. struct sync_payload {
  78. /*! Unique ID for this synchronous action */
  79. unsigned int id;
  80. /*! Actual frame data to process */
  81. unsigned char data[0];
  82. };
  83. /*!
  84. * \brief Synchronous bridge action object.
  85. *
  86. * Synchronous bridge actions require the ability for one thread to wait
  87. * and for another thread to indicate that the action has completed. This
  88. * structure facilitates that goal by providing synchronization structures.
  89. */
  90. struct bridge_sync {
  91. /*! Unique ID of this synchronization object. Corresponds with ID in synchronous frame payload */
  92. unsigned int id;
  93. /*! Semaphore used for synchronization */
  94. sem_t sem;
  95. /*! Pointer to next entry in the list */
  96. AST_LIST_ENTRY(bridge_sync) list;
  97. };
  98. /*!
  99. * \brief List holding active synchronous action objects.
  100. */
  101. static AST_RWLIST_HEAD_STATIC(sync_structs, bridge_sync);
  102. /*!
  103. * \brief initialize a synchronous bridge object.
  104. *
  105. * This both initializes the structure and adds it to the list of
  106. * synchronization structures.
  107. *
  108. * \param sync_struct The synchronization object to initialize.
  109. * \param id ID to assign to the synchronization object.
  110. */
  111. static void bridge_sync_init(struct bridge_sync *sync_struct, unsigned int id)
  112. {
  113. memset(sync_struct, 0, sizeof(*sync_struct));
  114. sync_struct->id = id;
  115. sem_init(&sync_struct->sem, 0, 0);
  116. AST_RWLIST_WRLOCK(&sync_structs);
  117. AST_RWLIST_INSERT_TAIL(&sync_structs, sync_struct, list);
  118. AST_RWLIST_UNLOCK(&sync_structs);
  119. }
  120. /*!
  121. * \brief Clean up a syncrhonization bridge object.
  122. *
  123. * This frees fields within the synchronization object and removes
  124. * it from the list of active synchronization objects.
  125. *
  126. * Since synchronization objects are stack-allocated, it is vital
  127. * that this is called before the synchronization object goes
  128. * out of scope.
  129. *
  130. * \param sync_struct Synchronization object to clean up.
  131. */
  132. static void bridge_sync_cleanup(struct bridge_sync *sync_struct)
  133. {
  134. struct bridge_sync *iter;
  135. AST_RWLIST_WRLOCK(&sync_structs);
  136. AST_LIST_TRAVERSE_SAFE_BEGIN(&sync_structs, iter, list) {
  137. if (iter->id == sync_struct->id) {
  138. AST_LIST_REMOVE_CURRENT(list);
  139. break;
  140. }
  141. }
  142. AST_LIST_TRAVERSE_SAFE_END;
  143. AST_RWLIST_UNLOCK(&sync_structs);
  144. sem_destroy(&sync_struct->sem);
  145. }
  146. /*!
  147. * \brief Failsafe for synchronous bridge action waiting.
  148. *
  149. * When waiting for a synchronous bridge action to complete,
  150. * if there is a frame resource leak somewhere, it is possible
  151. * that we will never get notified that the synchronous action
  152. * completed.
  153. *
  154. * If a significant amount of time passes, then we will abandon
  155. * waiting for the synchrnous bridge action to complete.
  156. *
  157. * This constant represents the number of milliseconds we will
  158. * wait for the bridge action to complete.
  159. */
  160. #define PLAYBACK_TIMEOUT (600 * 1000)
  161. /*!
  162. * \brief Wait for a synchronous bridge action to complete.
  163. *
  164. * \param sync_struct Synchronization object corresponding to the bridge action.
  165. */
  166. static void bridge_sync_wait(struct bridge_sync *sync_struct)
  167. {
  168. struct timeval timeout_val = ast_tvadd(ast_tvnow(), ast_samp2tv(PLAYBACK_TIMEOUT, 1000));
  169. struct timespec timeout_spec = {
  170. .tv_sec = timeout_val.tv_sec,
  171. .tv_nsec = timeout_val.tv_usec * 1000,
  172. };
  173. sem_timedwait(&sync_struct->sem, &timeout_spec);
  174. }
  175. /*!
  176. * \brief Signal that waiting for a synchronous bridge action is no longer necessary.
  177. *
  178. * This may occur for several reasons
  179. * \li The synchronous bridge action has completed.
  180. * \li The bridge channel has been removed from the bridge.
  181. * \li The synchronous bridge action could not be queued.
  182. *
  183. * \param sync_struct Synchronization object corresponding to the bridge action.
  184. */
  185. static void bridge_sync_signal(struct bridge_sync *sync_struct)
  186. {
  187. sem_post(&sync_struct->sem);
  188. }
  189. void ast_bridge_channel_lock_bridge(struct ast_bridge_channel *bridge_channel)
  190. {
  191. struct ast_bridge *bridge;
  192. for (;;) {
  193. /* Safely get the bridge pointer */
  194. ast_bridge_channel_lock(bridge_channel);
  195. bridge = bridge_channel->bridge;
  196. ao2_ref(bridge, +1);
  197. ast_bridge_channel_unlock(bridge_channel);
  198. /* Lock the bridge and see if it is still the bridge we need to lock. */
  199. ast_bridge_lock(bridge);
  200. if (bridge == bridge_channel->bridge) {
  201. ao2_ref(bridge, -1);
  202. return;
  203. }
  204. ast_bridge_unlock(bridge);
  205. ao2_ref(bridge, -1);
  206. }
  207. }
  208. int ast_bridge_channel_notify_talking(struct ast_bridge_channel *bridge_channel, int started_talking)
  209. {
  210. struct ast_frame action = {
  211. .frametype = AST_FRAME_BRIDGE_ACTION,
  212. .subclass.integer = started_talking
  213. ? BRIDGE_CHANNEL_ACTION_TALKING_START : BRIDGE_CHANNEL_ACTION_TALKING_STOP,
  214. };
  215. return ast_bridge_channel_queue_frame(bridge_channel, &action);
  216. }
  217. /*!
  218. * \internal
  219. * \brief Poke the bridge_channel thread
  220. */
  221. static void bridge_channel_poke(struct ast_bridge_channel *bridge_channel)
  222. {
  223. if (!pthread_equal(pthread_self(), bridge_channel->thread)) {
  224. /* Wake up the bridge channel thread. */
  225. ast_queue_frame(bridge_channel->chan, &ast_null_frame);
  226. }
  227. }
  228. /*!
  229. * \internal
  230. * \brief Set actual cause on channel.
  231. * \since 12.0.0
  232. *
  233. * \param chan Channel to set cause.
  234. * \param cause Cause to set on channel.
  235. * If cause <= 0 then use cause on channel if cause still <= 0 use AST_CAUSE_NORMAL_CLEARING.
  236. *
  237. * \return Actual cause set on channel.
  238. */
  239. static int channel_set_cause(struct ast_channel *chan, int cause)
  240. {
  241. ast_channel_lock(chan);
  242. if (cause <= 0) {
  243. cause = ast_channel_hangupcause(chan);
  244. if (cause <= 0) {
  245. cause = AST_CAUSE_NORMAL_CLEARING;
  246. }
  247. }
  248. ast_channel_hangupcause_set(chan, cause);
  249. ast_channel_unlock(chan);
  250. return cause;
  251. }
  252. void ast_bridge_channel_leave_bridge_nolock(struct ast_bridge_channel *bridge_channel, enum bridge_channel_state new_state, int cause)
  253. {
  254. if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
  255. return;
  256. }
  257. ast_debug(1, "Setting %p(%s) state from:%d to:%d\n",
  258. bridge_channel, ast_channel_name(bridge_channel->chan), bridge_channel->state,
  259. new_state);
  260. channel_set_cause(bridge_channel->chan, cause);
  261. /* Change the state on the bridge channel */
  262. bridge_channel->state = new_state;
  263. bridge_channel_poke(bridge_channel);
  264. }
  265. void ast_bridge_channel_leave_bridge(struct ast_bridge_channel *bridge_channel, enum bridge_channel_state new_state, int cause)
  266. {
  267. ast_bridge_channel_lock(bridge_channel);
  268. ast_bridge_channel_leave_bridge_nolock(bridge_channel, new_state, cause);
  269. ast_bridge_channel_unlock(bridge_channel);
  270. }
  271. struct ast_bridge_channel *ast_bridge_channel_peer(struct ast_bridge_channel *bridge_channel)
  272. {
  273. struct ast_bridge *bridge = bridge_channel->bridge;
  274. struct ast_bridge_channel *other = NULL;
  275. if (bridge_channel->in_bridge && bridge->num_channels == 2) {
  276. AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
  277. if (other != bridge_channel) {
  278. break;
  279. }
  280. }
  281. }
  282. return other;
  283. }
  284. void ast_bridge_channel_restore_formats(struct ast_bridge_channel *bridge_channel)
  285. {
  286. /* Restore original formats of the channel as they came in */
  287. if (ast_format_cmp(ast_channel_readformat(bridge_channel->chan), &bridge_channel->read_format) == AST_FORMAT_CMP_NOT_EQUAL) {
  288. ast_debug(1, "Bridge is returning %p(%s) to read format %s\n",
  289. bridge_channel, ast_channel_name(bridge_channel->chan),
  290. ast_getformatname(&bridge_channel->read_format));
  291. if (ast_set_read_format(bridge_channel->chan, &bridge_channel->read_format)) {
  292. ast_debug(1, "Bridge failed to return %p(%s) to read format %s\n",
  293. bridge_channel, ast_channel_name(bridge_channel->chan),
  294. ast_getformatname(&bridge_channel->read_format));
  295. }
  296. }
  297. if (ast_format_cmp(ast_channel_writeformat(bridge_channel->chan), &bridge_channel->write_format) == AST_FORMAT_CMP_NOT_EQUAL) {
  298. ast_debug(1, "Bridge is returning %p(%s) to write format %s\n",
  299. bridge_channel, ast_channel_name(bridge_channel->chan),
  300. ast_getformatname(&bridge_channel->write_format));
  301. if (ast_set_write_format(bridge_channel->chan, &bridge_channel->write_format)) {
  302. ast_debug(1, "Bridge failed to return %p(%s) to write format %s\n",
  303. bridge_channel, ast_channel_name(bridge_channel->chan),
  304. ast_getformatname(&bridge_channel->write_format));
  305. }
  306. }
  307. }
  308. struct ast_bridge *ast_bridge_channel_merge_inhibit(struct ast_bridge_channel *bridge_channel, int request)
  309. {
  310. struct ast_bridge *bridge;
  311. ast_bridge_channel_lock_bridge(bridge_channel);
  312. bridge = bridge_channel->bridge;
  313. ao2_ref(bridge, +1);
  314. bridge_merge_inhibit_nolock(bridge, request);
  315. ast_bridge_unlock(bridge);
  316. return bridge;
  317. }
  318. void ast_bridge_channel_update_linkedids(struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
  319. {
  320. struct ast_bridge_channel *other = NULL;
  321. struct ast_bridge *bridge = bridge_channel->bridge;
  322. struct ast_channel *oldest_linkedid_chan = bridge_channel->chan;
  323. AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
  324. if (other == swap) {
  325. continue;
  326. }
  327. oldest_linkedid_chan = ast_channel_internal_oldest_linkedid(
  328. oldest_linkedid_chan, other->chan);
  329. }
  330. ast_channel_lock(bridge_channel->chan);
  331. ast_channel_internal_copy_linkedid(bridge_channel->chan,
  332. oldest_linkedid_chan);
  333. ast_channel_unlock(bridge_channel->chan);
  334. AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
  335. if (other == swap) {
  336. continue;
  337. }
  338. ast_channel_lock(other->chan);
  339. ast_channel_internal_copy_linkedid(other->chan,
  340. oldest_linkedid_chan);
  341. ast_channel_unlock(other->chan);
  342. }
  343. }
  344. void ast_bridge_channel_update_accountcodes(struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
  345. {
  346. struct ast_bridge *bridge = bridge_channel->bridge;
  347. struct ast_bridge_channel *other = NULL;
  348. AST_LIST_TRAVERSE(&bridge->channels, other, entry) {
  349. if (other == swap) {
  350. continue;
  351. }
  352. ast_channel_lock_both(bridge_channel->chan, other->chan);
  353. if (!ast_strlen_zero(ast_channel_accountcode(bridge_channel->chan)) && ast_strlen_zero(ast_channel_peeraccount(other->chan))) {
  354. ast_debug(1, "Setting peeraccount to %s for %s from data on channel %s\n",
  355. ast_channel_accountcode(bridge_channel->chan), ast_channel_name(other->chan), ast_channel_name(bridge_channel->chan));
  356. ast_channel_peeraccount_set(other->chan, ast_channel_accountcode(bridge_channel->chan));
  357. }
  358. if (!ast_strlen_zero(ast_channel_accountcode(other->chan)) && ast_strlen_zero(ast_channel_peeraccount(bridge_channel->chan))) {
  359. ast_debug(1, "Setting peeraccount to %s for %s from data on channel %s\n",
  360. ast_channel_accountcode(other->chan), ast_channel_name(bridge_channel->chan), ast_channel_name(other->chan));
  361. ast_channel_peeraccount_set(bridge_channel->chan, ast_channel_accountcode(other->chan));
  362. }
  363. if (!ast_strlen_zero(ast_channel_peeraccount(bridge_channel->chan)) && ast_strlen_zero(ast_channel_accountcode(other->chan))) {
  364. ast_debug(1, "Setting accountcode to %s for %s from data on channel %s\n",
  365. ast_channel_peeraccount(bridge_channel->chan), ast_channel_name(other->chan), ast_channel_name(bridge_channel->chan));
  366. ast_channel_accountcode_set(other->chan, ast_channel_peeraccount(bridge_channel->chan));
  367. }
  368. if (!ast_strlen_zero(ast_channel_peeraccount(other->chan)) && ast_strlen_zero(ast_channel_accountcode(bridge_channel->chan))) {
  369. ast_debug(1, "Setting accountcode to %s for %s from data on channel %s\n",
  370. ast_channel_peeraccount(other->chan), ast_channel_name(bridge_channel->chan), ast_channel_name(other->chan));
  371. ast_channel_accountcode_set(bridge_channel->chan, ast_channel_peeraccount(other->chan));
  372. }
  373. if (bridge->num_channels == 2) {
  374. if (strcmp(ast_channel_accountcode(bridge_channel->chan), ast_channel_peeraccount(other->chan))) {
  375. ast_debug(1, "Changing peeraccount from %s to %s on %s to match channel %s\n",
  376. ast_channel_peeraccount(other->chan), ast_channel_peeraccount(bridge_channel->chan), ast_channel_name(other->chan), ast_channel_name(bridge_channel->chan));
  377. ast_channel_peeraccount_set(other->chan, ast_channel_accountcode(bridge_channel->chan));
  378. }
  379. if (strcmp(ast_channel_accountcode(other->chan), ast_channel_peeraccount(bridge_channel->chan))) {
  380. ast_debug(1, "Changing peeraccount from %s to %s on %s to match channel %s\n",
  381. ast_channel_peeraccount(bridge_channel->chan), ast_channel_peeraccount(other->chan), ast_channel_name(bridge_channel->chan), ast_channel_name(other->chan));
  382. ast_channel_peeraccount_set(bridge_channel->chan, ast_channel_accountcode(other->chan));
  383. }
  384. }
  385. ast_channel_unlock(bridge_channel->chan);
  386. ast_channel_unlock(other->chan);
  387. }
  388. }
  389. void ast_bridge_channel_kick(struct ast_bridge_channel *bridge_channel, int cause)
  390. {
  391. struct ast_bridge_features *features = bridge_channel->features;
  392. struct ast_bridge_hook *hook;
  393. struct ao2_iterator iter;
  394. ast_bridge_channel_lock(bridge_channel);
  395. if (bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
  396. channel_set_cause(bridge_channel->chan, cause);
  397. cause = 0;
  398. }
  399. ast_bridge_channel_unlock(bridge_channel);
  400. /* Run any hangup hooks. */
  401. iter = ao2_iterator_init(features->other_hooks, 0);
  402. for (; (hook = ao2_iterator_next(&iter)); ao2_ref(hook, -1)) {
  403. int remove_me;
  404. if (hook->type != AST_BRIDGE_HOOK_TYPE_HANGUP) {
  405. continue;
  406. }
  407. remove_me = hook->callback(bridge_channel, hook->hook_pvt);
  408. if (remove_me) {
  409. ast_debug(1, "Hangup hook %p is being removed from %p(%s)\n",
  410. hook, bridge_channel, ast_channel_name(bridge_channel->chan));
  411. ao2_unlink(features->other_hooks, hook);
  412. }
  413. }
  414. ao2_iterator_destroy(&iter);
  415. /* Default hangup action. */
  416. ast_bridge_channel_leave_bridge(bridge_channel, BRIDGE_CHANNEL_STATE_END, cause);
  417. }
  418. /*!
  419. * \internal
  420. * \brief Write an \ref ast_frame onto the bridge channel
  421. * \since 12.0.0
  422. *
  423. * \param bridge_channel Which channel to queue the frame onto.
  424. * \param frame The frame to write onto the bridge_channel
  425. *
  426. * \retval 0 on success.
  427. * \retval -1 on error.
  428. */
  429. static int bridge_channel_write_frame(struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
  430. {
  431. ast_assert(frame->frametype != AST_FRAME_BRIDGE_ACTION_SYNC);
  432. ast_bridge_channel_lock_bridge(bridge_channel);
  433. /*
  434. * XXX need to implement a deferred write queue for when there
  435. * is no peer channel in the bridge (yet or it was kicked).
  436. *
  437. * The tech decides if a frame needs to be pushed back for deferral.
  438. * simple_bridge/native_bridge are likely the only techs that will do this.
  439. */
  440. bridge_channel->bridge->technology->write(bridge_channel->bridge, bridge_channel, frame);
  441. /* Remember any owed events to the bridge. */
  442. switch (frame->frametype) {
  443. case AST_FRAME_DTMF_BEGIN:
  444. bridge_channel->owed.dtmf_tv = ast_tvnow();
  445. bridge_channel->owed.dtmf_digit = frame->subclass.integer;
  446. break;
  447. case AST_FRAME_DTMF_END:
  448. bridge_channel->owed.dtmf_digit = '\0';
  449. break;
  450. case AST_FRAME_CONTROL:
  451. /*
  452. * We explicitly will not remember HOLD/UNHOLD frames because
  453. * things like attended transfers will handle them.
  454. */
  455. default:
  456. break;
  457. }
  458. ast_bridge_unlock(bridge_channel->bridge);
  459. /*
  460. * Claim successful write to bridge. If deferred frame
  461. * support is added, claim successfully deferred.
  462. */
  463. return 0;
  464. }
  465. void bridge_channel_settle_owed_events(struct ast_bridge *orig_bridge, struct ast_bridge_channel *bridge_channel)
  466. {
  467. if (bridge_channel->owed.dtmf_digit) {
  468. struct ast_frame frame = {
  469. .frametype = AST_FRAME_DTMF_END,
  470. .subclass.integer = bridge_channel->owed.dtmf_digit,
  471. .src = "Bridge channel owed DTMF",
  472. };
  473. frame.len = ast_tvdiff_ms(ast_tvnow(), bridge_channel->owed.dtmf_tv);
  474. if (frame.len < option_dtmfminduration) {
  475. frame.len = option_dtmfminduration;
  476. }
  477. ast_log(LOG_DTMF, "DTMF end '%c' simulated to bridge %s because %s left. Duration %ld ms.\n",
  478. bridge_channel->owed.dtmf_digit, orig_bridge->uniqueid,
  479. ast_channel_name(bridge_channel->chan), frame.len);
  480. bridge_channel->owed.dtmf_digit = '\0';
  481. orig_bridge->technology->write(orig_bridge, NULL, &frame);
  482. }
  483. }
  484. /*!
  485. * \internal
  486. * \brief Suspend a channel from a bridge.
  487. *
  488. * \param bridge_channel Channel to suspend.
  489. *
  490. * \note This function assumes bridge_channel->bridge is locked.
  491. *
  492. * \return Nothing
  493. */
  494. void bridge_channel_internal_suspend_nolock(struct ast_bridge_channel *bridge_channel)
  495. {
  496. bridge_channel->suspended = 1;
  497. if (bridge_channel->in_bridge) {
  498. --bridge_channel->bridge->num_active;
  499. }
  500. /* Get technology bridge threads off of the channel. */
  501. if (bridge_channel->bridge->technology->suspend) {
  502. bridge_channel->bridge->technology->suspend(bridge_channel->bridge, bridge_channel);
  503. }
  504. }
  505. /*!
  506. * \internal
  507. * \brief Suspend a channel from a bridge.
  508. *
  509. * \param bridge_channel Channel to suspend.
  510. *
  511. * \return Nothing
  512. */
  513. static void bridge_channel_suspend(struct ast_bridge_channel *bridge_channel)
  514. {
  515. ast_bridge_channel_lock_bridge(bridge_channel);
  516. bridge_channel_internal_suspend_nolock(bridge_channel);
  517. ast_bridge_unlock(bridge_channel->bridge);
  518. }
  519. /*!
  520. * \internal
  521. * \brief Unsuspend a channel from a bridge.
  522. *
  523. * \param bridge_channel Channel to unsuspend.
  524. *
  525. * \note This function assumes bridge_channel->bridge is locked.
  526. *
  527. * \return Nothing
  528. */
  529. void bridge_channel_internal_unsuspend_nolock(struct ast_bridge_channel *bridge_channel)
  530. {
  531. bridge_channel->suspended = 0;
  532. if (bridge_channel->in_bridge) {
  533. ++bridge_channel->bridge->num_active;
  534. }
  535. /* Wake technology bridge threads to take care of channel again. */
  536. if (bridge_channel->bridge->technology->unsuspend) {
  537. bridge_channel->bridge->technology->unsuspend(bridge_channel->bridge, bridge_channel);
  538. }
  539. /* Wake suspended channel. */
  540. ast_bridge_channel_lock(bridge_channel);
  541. ast_cond_signal(&bridge_channel->cond);
  542. ast_bridge_channel_unlock(bridge_channel);
  543. }
  544. /*!
  545. * \internal
  546. * \brief Unsuspend a channel from a bridge.
  547. *
  548. * \param bridge_channel Channel to unsuspend.
  549. *
  550. * \return Nothing
  551. */
  552. static void bridge_channel_unsuspend(struct ast_bridge_channel *bridge_channel)
  553. {
  554. ast_bridge_channel_lock_bridge(bridge_channel);
  555. bridge_channel_internal_unsuspend_nolock(bridge_channel);
  556. ast_bridge_unlock(bridge_channel->bridge);
  557. }
  558. /*!
  559. * \internal
  560. * \brief Queue an action frame onto the bridge channel with data.
  561. * \since 12.0.0
  562. *
  563. * \param bridge_channel Which channel to queue the frame onto.
  564. * \param action Type of bridge action frame.
  565. * \param data Frame payload data to pass.
  566. * \param datalen Frame payload data length to pass.
  567. *
  568. * \retval 0 on success.
  569. * \retval -1 on error.
  570. */
  571. static int bridge_channel_queue_action_data(struct ast_bridge_channel *bridge_channel,
  572. enum bridge_channel_action_type action, const void *data, size_t datalen)
  573. {
  574. struct ast_frame frame = {
  575. .frametype = AST_FRAME_BRIDGE_ACTION,
  576. .subclass.integer = action,
  577. .datalen = datalen,
  578. .data.ptr = (void *) data,
  579. };
  580. return ast_bridge_channel_queue_frame(bridge_channel, &frame);
  581. }
  582. /*!
  583. * \internal
  584. * \brief Queue an action frame onto the bridge channel with data synchronously.
  585. * \since 12.2.0
  586. *
  587. * The function will not return until the queued frame is freed.
  588. *
  589. * \param bridge_channel Which channel to queue the frame onto.
  590. * \param action Type of bridge action frame.
  591. * \param data Frame payload data to pass.
  592. * \param datalen Frame payload data length to pass.
  593. *
  594. * \retval 0 on success.
  595. * \retval -1 on error.
  596. */
  597. static int bridge_channel_queue_action_data_sync(struct ast_bridge_channel *bridge_channel,
  598. enum bridge_channel_action_type action, const void *data, size_t datalen)
  599. {
  600. struct sync_payload *sync_payload;
  601. int sync_payload_len = sizeof(*sync_payload) + datalen;
  602. struct bridge_sync sync_struct;
  603. struct ast_frame frame = {
  604. .frametype = AST_FRAME_BRIDGE_ACTION_SYNC,
  605. .subclass.integer = action,
  606. };
  607. /* Make sure we don't end up trying to wait on ourself to deliver the frame */
  608. ast_assert(!pthread_equal(pthread_self(), bridge_channel->thread));
  609. sync_payload = ast_alloca(sync_payload_len);
  610. sync_payload->id = ast_atomic_fetchadd_int(&sync_ids, +1);
  611. memcpy(sync_payload->data, data, datalen);
  612. frame.datalen = sync_payload_len;
  613. frame.data.ptr = sync_payload;
  614. bridge_sync_init(&sync_struct, sync_payload->id);
  615. if (ast_bridge_channel_queue_frame(bridge_channel, &frame)) {
  616. bridge_sync_cleanup(&sync_struct);
  617. return -1;
  618. }
  619. bridge_sync_wait(&sync_struct);
  620. bridge_sync_cleanup(&sync_struct);
  621. return 0;
  622. }
  623. /*!
  624. * \internal
  625. * \brief Write an action frame onto the bridge channel with data.
  626. * \since 12.0.0
  627. *
  628. * \param bridge_channel Which channel to queue the frame onto.
  629. * \param action Type of bridge action frame.
  630. * \param data Frame payload data to pass.
  631. * \param datalen Frame payload data length to pass.
  632. *
  633. * \retval 0 on success.
  634. * \retval -1 on error.
  635. */
  636. static int bridge_channel_write_action_data(struct ast_bridge_channel *bridge_channel,
  637. enum bridge_channel_action_type action, const void *data, size_t datalen)
  638. {
  639. struct ast_frame frame = {
  640. .frametype = AST_FRAME_BRIDGE_ACTION,
  641. .subclass.integer = action,
  642. .datalen = datalen,
  643. .data.ptr = (void *) data,
  644. };
  645. return bridge_channel_write_frame(bridge_channel, &frame);
  646. }
  647. static void bridge_frame_free(struct ast_frame *frame)
  648. {
  649. if (frame->frametype == AST_FRAME_BRIDGE_ACTION_SYNC) {
  650. struct sync_payload *sync_payload = frame->data.ptr;
  651. struct bridge_sync *sync;
  652. AST_RWLIST_RDLOCK(&sync_structs);
  653. AST_RWLIST_TRAVERSE(&sync_structs, sync, list) {
  654. if (sync->id == sync_payload->id) {
  655. break;
  656. }
  657. }
  658. if (sync) {
  659. bridge_sync_signal(sync);
  660. }
  661. AST_RWLIST_UNLOCK(&sync_structs);
  662. }
  663. ast_frfree(frame);
  664. }
  665. int ast_bridge_channel_queue_frame(struct ast_bridge_channel *bridge_channel, struct ast_frame *fr)
  666. {
  667. struct ast_frame *dup;
  668. char nudge = 0;
  669. if (bridge_channel->suspended
  670. /* Also defer DTMF frames. */
  671. && fr->frametype != AST_FRAME_DTMF_BEGIN
  672. && fr->frametype != AST_FRAME_DTMF_END
  673. && !ast_is_deferrable_frame(fr)) {
  674. /* Drop non-deferable frames when suspended. */
  675. return 0;
  676. }
  677. if (fr->frametype == AST_FRAME_NULL) {
  678. /* "Accept" the frame and discard it. */
  679. return 0;
  680. }
  681. dup = ast_frdup(fr);
  682. if (!dup) {
  683. return -1;
  684. }
  685. ast_bridge_channel_lock(bridge_channel);
  686. if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
  687. /* Drop frames on channels leaving the bridge. */
  688. ast_bridge_channel_unlock(bridge_channel);
  689. bridge_frame_free(dup);
  690. return 0;
  691. }
  692. AST_LIST_INSERT_TAIL(&bridge_channel->wr_queue, dup, frame_list);
  693. if (write(bridge_channel->alert_pipe[1], &nudge, sizeof(nudge)) != sizeof(nudge)) {
  694. ast_log(LOG_ERROR, "We couldn't write alert pipe for %p(%s)... something is VERY wrong\n",
  695. bridge_channel, ast_channel_name(bridge_channel->chan));
  696. }
  697. ast_bridge_channel_unlock(bridge_channel);
  698. return 0;
  699. }
  700. int ast_bridge_queue_everyone_else(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
  701. {
  702. struct ast_bridge_channel *cur;
  703. int not_written = -1;
  704. if (frame->frametype == AST_FRAME_NULL) {
  705. /* "Accept" the frame and discard it. */
  706. return 0;
  707. }
  708. AST_LIST_TRAVERSE(&bridge->channels, cur, entry) {
  709. if (cur == bridge_channel) {
  710. continue;
  711. }
  712. if (!ast_bridge_channel_queue_frame(cur, frame)) {
  713. not_written = 0;
  714. }
  715. }
  716. return not_written;
  717. }
  718. int ast_bridge_channel_queue_control_data(struct ast_bridge_channel *bridge_channel, enum ast_control_frame_type control, const void *data, size_t datalen)
  719. {
  720. struct ast_frame frame = {
  721. .frametype = AST_FRAME_CONTROL,
  722. .subclass.integer = control,
  723. .datalen = datalen,
  724. .data.ptr = (void *) data,
  725. };
  726. return ast_bridge_channel_queue_frame(bridge_channel, &frame);
  727. }
  728. int ast_bridge_channel_write_control_data(struct ast_bridge_channel *bridge_channel, enum ast_control_frame_type control, const void *data, size_t datalen)
  729. {
  730. struct ast_frame frame = {
  731. .frametype = AST_FRAME_CONTROL,
  732. .subclass.integer = control,
  733. .datalen = datalen,
  734. .data.ptr = (void *) data,
  735. };
  736. return bridge_channel_write_frame(bridge_channel, &frame);
  737. }
  738. int ast_bridge_channel_write_hold(struct ast_bridge_channel *bridge_channel, const char *moh_class)
  739. {
  740. RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
  741. size_t datalen;
  742. if (!ast_strlen_zero(moh_class)) {
  743. datalen = strlen(moh_class) + 1;
  744. blob = ast_json_pack("{s: s}",
  745. "musicclass", moh_class);
  746. } else {
  747. moh_class = NULL;
  748. datalen = 0;
  749. }
  750. ast_channel_lock(bridge_channel->chan);
  751. ast_channel_publish_blob(bridge_channel->chan, ast_channel_hold_type(), blob);
  752. ast_channel_unlock(bridge_channel->chan);
  753. return ast_bridge_channel_write_control_data(bridge_channel, AST_CONTROL_HOLD,
  754. moh_class, datalen);
  755. }
  756. int ast_bridge_channel_write_unhold(struct ast_bridge_channel *bridge_channel)
  757. {
  758. ast_channel_lock(bridge_channel->chan);
  759. ast_channel_publish_blob(bridge_channel->chan, ast_channel_unhold_type(), NULL);
  760. ast_channel_unlock(bridge_channel->chan);
  761. return ast_bridge_channel_write_control_data(bridge_channel, AST_CONTROL_UNHOLD, NULL, 0);
  762. }
  763. /*!
  764. * \internal
  765. * \brief Helper function to kick off a PBX app on a bridge_channel
  766. */
  767. static int run_app_helper(struct ast_channel *chan, const char *app_name, const char *app_args)
  768. {
  769. int res = 0;
  770. if (!strcasecmp("Gosub", app_name)) {
  771. ast_app_exec_sub(NULL, chan, app_args, 0);
  772. } else if (!strcasecmp("Macro", app_name)) {
  773. ast_app_exec_macro(NULL, chan, app_args);
  774. } else {
  775. struct ast_app *app;
  776. app = pbx_findapp(app_name);
  777. if (!app) {
  778. ast_log(LOG_WARNING, "Could not find application (%s)\n", app_name);
  779. } else {
  780. res = pbx_exec(chan, app, app_args);
  781. }
  782. }
  783. return res;
  784. }
  785. void ast_bridge_channel_run_app(struct ast_bridge_channel *bridge_channel, const char *app_name, const char *app_args, const char *moh_class)
  786. {
  787. if (moh_class) {
  788. ast_bridge_channel_write_hold(bridge_channel, moh_class);
  789. }
  790. if (run_app_helper(bridge_channel->chan, app_name, S_OR(app_args, ""))) {
  791. /* Break the bridge if the app returns non-zero. */
  792. ast_bridge_channel_kick(bridge_channel, AST_CAUSE_NORMAL_CLEARING);
  793. }
  794. if (moh_class) {
  795. ast_bridge_channel_write_unhold(bridge_channel);
  796. }
  797. }
  798. struct bridge_run_app {
  799. /*! Offset into app_name[] where the MOH class name starts. (zero if no MOH) */
  800. int moh_offset;
  801. /*! Offset into app_name[] where the application argument string starts. (zero if no arguments) */
  802. int app_args_offset;
  803. /*! Application name to run. */
  804. char app_name[0];
  805. };
  806. /*!
  807. * \internal
  808. * \brief Handle the run application bridge action.
  809. * \since 12.0.0
  810. *
  811. * \param bridge_channel Which channel to run the application on.
  812. * \param data Action frame data to run the application.
  813. *
  814. * \return Nothing
  815. */
  816. static void bridge_channel_run_app(struct ast_bridge_channel *bridge_channel, struct bridge_run_app *data)
  817. {
  818. ast_bridge_channel_run_app(bridge_channel, data->app_name,
  819. data->app_args_offset ? &data->app_name[data->app_args_offset] : NULL,
  820. data->moh_offset ? &data->app_name[data->moh_offset] : NULL);
  821. }
  822. /*!
  823. * \internal
  824. * \brief Marshal an application to be executed on a bridge_channel
  825. */
  826. static int payload_helper_app(ast_bridge_channel_post_action_data post_it,
  827. struct ast_bridge_channel *bridge_channel, const char *app_name, const char *app_args, const char *moh_class)
  828. {
  829. struct bridge_run_app *app_data;
  830. size_t len_name = strlen(app_name) + 1;
  831. size_t len_args = ast_strlen_zero(app_args) ? 0 : strlen(app_args) + 1;
  832. size_t len_moh = !moh_class ? 0 : strlen(moh_class) + 1;
  833. size_t len_data = sizeof(*app_data) + len_name + len_args + len_moh;
  834. /* Fill in application run frame data. */
  835. app_data = alloca(len_data);
  836. app_data->app_args_offset = len_args ? len_name : 0;
  837. app_data->moh_offset = len_moh ? len_name + len_args : 0;
  838. strcpy(app_data->app_name, app_name);/* Safe */
  839. if (len_args) {
  840. strcpy(&app_data->app_name[app_data->app_args_offset], app_args);/* Safe */
  841. }
  842. if (moh_class) {
  843. strcpy(&app_data->app_name[app_data->moh_offset], moh_class);/* Safe */
  844. }
  845. return post_it(bridge_channel, BRIDGE_CHANNEL_ACTION_RUN_APP, app_data, len_data);
  846. }
  847. int ast_bridge_channel_write_app(struct ast_bridge_channel *bridge_channel, const char *app_name, const char *app_args, const char *moh_class)
  848. {
  849. return payload_helper_app(bridge_channel_write_action_data,
  850. bridge_channel, app_name, app_args, moh_class);
  851. }
  852. int ast_bridge_channel_queue_app(struct ast_bridge_channel *bridge_channel, const char *app_name, const char *app_args, const char *moh_class)
  853. {
  854. return payload_helper_app(bridge_channel_queue_action_data,
  855. bridge_channel, app_name, app_args, moh_class);
  856. }
  857. void ast_bridge_channel_playfile(struct ast_bridge_channel *bridge_channel, ast_bridge_custom_play_fn custom_play, const char *playfile, const char *moh_class)
  858. {
  859. if (moh_class) {
  860. ast_bridge_channel_write_hold(bridge_channel, moh_class);
  861. }
  862. if (custom_play) {
  863. custom_play(bridge_channel, playfile);
  864. } else {
  865. ast_stream_and_wait(bridge_channel->chan, playfile, AST_DIGIT_NONE);
  866. }
  867. if (moh_class) {
  868. ast_bridge_channel_write_unhold(bridge_channel);
  869. }
  870. /*
  871. * It may be necessary to resume music on hold after we finish
  872. * playing the announcment.
  873. */
  874. if (ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_MOH)) {
  875. const char *latest_musicclass;
  876. ast_channel_lock(bridge_channel->chan);
  877. latest_musicclass = ast_strdupa(ast_channel_latest_musicclass(bridge_channel->chan));
  878. ast_channel_unlock(bridge_channel->chan);
  879. ast_moh_start(bridge_channel->chan, latest_musicclass, NULL);
  880. }
  881. }
  882. struct bridge_playfile {
  883. /*! Call this function to play the playfile. (NULL if normal sound file to play) */
  884. ast_bridge_custom_play_fn custom_play;
  885. /*! Offset into playfile[] where the MOH class name starts. (zero if no MOH)*/
  886. int moh_offset;
  887. /*! Filename to play. */
  888. char playfile[0];
  889. };
  890. /*!
  891. * \internal
  892. * \brief Handle the playfile bridge action.
  893. * \since 12.0.0
  894. *
  895. * \param bridge_channel Which channel to play a file on.
  896. * \param payload Action frame payload to play a file.
  897. *
  898. * \return Nothing
  899. */
  900. static void bridge_channel_playfile(struct ast_bridge_channel *bridge_channel, struct bridge_playfile *payload)
  901. {
  902. ast_bridge_channel_playfile(bridge_channel, payload->custom_play, payload->playfile,
  903. payload->moh_offset ? &payload->playfile[payload->moh_offset] : NULL);
  904. }
  905. /*!
  906. * \internal
  907. * \brief Marshal a file to be played on a bridge_channel
  908. */
  909. static int payload_helper_playfile(ast_bridge_channel_post_action_data post_it,
  910. struct ast_bridge_channel *bridge_channel, ast_bridge_custom_play_fn custom_play, const char *playfile, const char *moh_class)
  911. {
  912. struct bridge_playfile *payload;
  913. size_t len_name = strlen(playfile) + 1;
  914. size_t len_moh = !moh_class ? 0 : strlen(moh_class) + 1;
  915. size_t len_payload = sizeof(*payload) + len_name + len_moh;
  916. /* Fill in play file frame data. */
  917. payload = ast_alloca(len_payload);
  918. payload->custom_play = custom_play;
  919. payload->moh_offset = len_moh ? len_name : 0;
  920. strcpy(payload->playfile, playfile);/* Safe */
  921. if (moh_class) {
  922. strcpy(&payload->playfile[payload->moh_offset], moh_class);/* Safe */
  923. }
  924. return post_it(bridge_channel, BRIDGE_CHANNEL_ACTION_PLAY_FILE, payload, len_payload);
  925. }
  926. int ast_bridge_channel_write_playfile(struct ast_bridge_channel *bridge_channel, ast_bridge_custom_play_fn custom_play, const char *playfile, const char *moh_class)
  927. {
  928. return payload_helper_playfile(bridge_channel_write_action_data,
  929. bridge_channel, custom_play, playfile, moh_class);
  930. }
  931. int ast_bridge_channel_queue_playfile(struct ast_bridge_channel *bridge_channel, ast_bridge_custom_play_fn custom_play, const char *playfile, const char *moh_class)
  932. {
  933. return payload_helper_playfile(bridge_channel_queue_action_data,
  934. bridge_channel, custom_play, playfile, moh_class);
  935. }
  936. int ast_bridge_channel_queue_playfile_sync(struct ast_bridge_channel *bridge_channel,
  937. ast_bridge_custom_play_fn custom_play, const char *playfile, const char *moh_class)
  938. {
  939. return payload_helper_playfile(bridge_channel_queue_action_data_sync,
  940. bridge_channel, custom_play, playfile, moh_class);
  941. }
  942. struct bridge_custom_callback {
  943. /*! Call this function on the bridge channel thread. */
  944. ast_bridge_custom_callback_fn callback;
  945. /*! Size of the payload if it exists. A number otherwise. */
  946. size_t payload_size;
  947. /*! Option flags determining how callback is called. */
  948. unsigned int flags;
  949. /*! Nonzero if the payload exists. */
  950. char payload_exists;
  951. /*! Payload to give to callback. */
  952. char payload[0];
  953. };
  954. /*!
  955. * \internal
  956. * \brief Handle the do custom callback bridge action.
  957. * \since 12.0.0
  958. *
  959. * \param bridge_channel Which channel to call the callback on.
  960. * \param data Action frame data to call the callback.
  961. *
  962. * \return Nothing
  963. */
  964. static void bridge_channel_do_callback(struct ast_bridge_channel *bridge_channel, struct bridge_custom_callback *data)
  965. {
  966. if (ast_test_flag(data, AST_BRIDGE_CHANNEL_CB_OPTION_MEDIA)) {
  967. bridge_channel_suspend(bridge_channel);
  968. ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
  969. }
  970. data->callback(bridge_channel, data->payload_exists ? data->payload : NULL, data->payload_size);
  971. if (ast_test_flag(data, AST_BRIDGE_CHANNEL_CB_OPTION_MEDIA)) {
  972. ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
  973. bridge_channel_unsuspend(bridge_channel);
  974. }
  975. }
  976. /*!
  977. * \internal
  978. * \brief Marshal a custom callback function to be called on a bridge_channel
  979. */
  980. static int payload_helper_cb(ast_bridge_channel_post_action_data post_it,
  981. struct ast_bridge_channel *bridge_channel,
  982. enum ast_bridge_channel_custom_callback_option flags,
  983. ast_bridge_custom_callback_fn callback, const void *payload, size_t payload_size)
  984. {
  985. struct bridge_custom_callback *cb_data;
  986. size_t len_data = sizeof(*cb_data) + (payload ? payload_size : 0);
  987. /* Sanity check. */
  988. if (!callback) {
  989. ast_assert(0);
  990. return -1;
  991. }
  992. /* Fill in custom callback frame data. */
  993. cb_data = alloca(len_data);
  994. cb_data->callback = callback;
  995. cb_data->payload_size = payload_size;
  996. cb_data->flags = flags;
  997. cb_data->payload_exists = payload && payload_size;
  998. if (cb_data->payload_exists) {
  999. memcpy(cb_data->payload, payload, payload_size);/* Safe */
  1000. }
  1001. return post_it(bridge_channel, BRIDGE_CHANNEL_ACTION_CALLBACK, cb_data, len_data);
  1002. }
  1003. int ast_bridge_channel_write_callback(struct ast_bridge_channel *bridge_channel,
  1004. enum ast_bridge_channel_custom_callback_option flags,
  1005. ast_bridge_custom_callback_fn callback, const void *payload, size_t payload_size)
  1006. {
  1007. return payload_helper_cb(bridge_channel_write_action_data,
  1008. bridge_channel, flags, callback, payload, payload_size);
  1009. }
  1010. int ast_bridge_channel_queue_callback(struct ast_bridge_channel *bridge_channel,
  1011. enum ast_bridge_channel_custom_callback_option flags,
  1012. ast_bridge_custom_callback_fn callback, const void *payload, size_t payload_size)
  1013. {
  1014. return payload_helper_cb(bridge_channel_queue_action_data,
  1015. bridge_channel, flags, callback, payload, payload_size);
  1016. }
  1017. struct bridge_park {
  1018. int parker_uuid_offset;
  1019. int app_data_offset;
  1020. /* buffer used for holding those strings */
  1021. char parkee_uuid[0];
  1022. };
  1023. /*!
  1024. * \internal
  1025. * \brief Park a bridge_cahnnel
  1026. */
  1027. static void bridge_channel_park(struct ast_bridge_channel *bridge_channel, struct bridge_park *payload)
  1028. {
  1029. if (!ast_parking_provider_registered()) {
  1030. ast_log(AST_LOG_WARNING, "Unable to park %s: No parking provider loaded!\n",
  1031. ast_channel_name(bridge_channel->chan));
  1032. return;
  1033. }
  1034. if (ast_parking_park_bridge_channel(bridge_channel, payload->parkee_uuid,
  1035. &payload->parkee_uuid[payload->parker_uuid_offset],
  1036. payload->app_data_offset ? &payload->parkee_uuid[payload->app_data_offset] : NULL)) {
  1037. ast_log(AST_LOG_WARNING, "Error occurred while parking %s\n",
  1038. ast_channel_name(bridge_channel->chan));
  1039. }
  1040. }
  1041. /*!
  1042. * \internal
  1043. * \brief Marshal a park action onto a bridge_channel
  1044. */
  1045. static int payload_helper_park(ast_bridge_channel_post_action_data post_it,
  1046. struct ast_bridge_channel *bridge_channel,
  1047. const char *parkee_uuid,
  1048. const char *parker_uuid,
  1049. const char *app_data)
  1050. {
  1051. struct bridge_park *payload;
  1052. size_t len_parkee_uuid = strlen(parkee_uuid) + 1;
  1053. size_t len_parker_uuid = strlen(parker_uuid) + 1;
  1054. size_t len_app_data = !app_data ? 0 : strlen(app_data) + 1;
  1055. size_t len_payload = sizeof(*payload) + len_parker_uuid + len_parkee_uuid + len_app_data;
  1056. payload = alloca(len_payload);
  1057. payload->app_data_offset = len_app_data ? len_parkee_uuid + len_parker_uuid : 0;
  1058. payload->parker_uuid_offset = len_parkee_uuid;
  1059. strcpy(payload->parkee_uuid, parkee_uuid);
  1060. strcpy(&payload->parkee_uuid[payload->parker_uuid_offset], parker_uuid);
  1061. if (app_data) {
  1062. strcpy(&payload->parkee_uuid[payload->app_data_offset], app_data);
  1063. }
  1064. return post_it(bridge_channel, BRIDGE_CHANNEL_ACTION_PARK, payload, len_payload);
  1065. }
  1066. int ast_bridge_channel_write_park(struct ast_bridge_channel *bridge_channel, const char *parkee_uuid, const char *parker_uuid, const char *app_data)
  1067. {
  1068. return payload_helper_park(bridge_channel_write_action_data,
  1069. bridge_channel, parkee_uuid, parker_uuid, app_data);
  1070. }
  1071. /*!
  1072. * \internal
  1073. * \brief Handle bridge channel interval expiration.
  1074. * \since 12.0.0
  1075. *
  1076. * \param bridge_channel Channel to run expired intervals on.
  1077. *
  1078. * \return Nothing
  1079. */
  1080. static void bridge_channel_handle_interval(struct ast_bridge_channel *bridge_channel)
  1081. {
  1082. struct ast_heap *interval_hooks;
  1083. struct ast_bridge_hook_timer *hook;
  1084. struct timeval start;
  1085. int chan_suspended = 0;
  1086. interval_hooks = bridge_channel->features->interval_hooks;
  1087. ast_heap_wrlock(interval_hooks);
  1088. start = ast_tvnow();
  1089. while ((hook = ast_heap_peek(interval_hooks, 1))) {
  1090. int interval;
  1091. unsigned int execution_time;
  1092. if (ast_tvdiff_ms(hook->timer.trip_time, start) > 0) {
  1093. ast_debug(1, "Hook %p on %p(%s) wants to happen in the future, stopping our traversal\n",
  1094. hook, bridge_channel, ast_channel_name(bridge_channel->chan));
  1095. break;
  1096. }
  1097. ao2_ref(hook, +1);
  1098. ast_heap_unlock(interval_hooks);
  1099. if (!chan_suspended
  1100. && ast_test_flag(&hook->timer, AST_BRIDGE_HOOK_TIMER_OPTION_MEDIA)) {
  1101. chan_suspended = 1;
  1102. bridge_channel_suspend(bridge_channel);
  1103. ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
  1104. }
  1105. ast_debug(1, "Executing hook %p on %p(%s)\n",
  1106. hook, bridge_channel, ast_channel_name(bridge_channel->chan));
  1107. interval = hook->generic.callback(bridge_channel, hook->generic.hook_pvt);
  1108. ast_heap_wrlock(interval_hooks);
  1109. if (ast_heap_peek(interval_hooks, hook->timer.heap_index) != hook
  1110. || !ast_heap_remove(interval_hooks, hook)) {
  1111. /* Interval hook is already removed from the bridge_channel. */
  1112. ao2_ref(hook, -1);
  1113. continue;
  1114. }
  1115. ao2_ref(hook, -1);
  1116. if (interval < 0) {
  1117. ast_debug(1, "Removed interval hook %p from %p(%s)\n",
  1118. hook, bridge_channel, ast_channel_name(bridge_channel->chan));
  1119. ao2_ref(hook, -1);
  1120. continue;
  1121. }
  1122. if (interval) {
  1123. /* Set new interval for the hook. */
  1124. hook->timer.interval = interval;
  1125. }
  1126. ast_debug(1, "Updating interval hook %p with interval %u on %p(%s)\n",
  1127. hook, hook->timer.interval, bridge_channel,
  1128. ast_channel_name(bridge_channel->chan));
  1129. /* resetting start */
  1130. start = ast_tvnow();
  1131. /*
  1132. * Resetup the interval hook for the next interval. We may need
  1133. * to skip over any missed intervals because the hook was
  1134. * delayed or took too long.
  1135. */
  1136. execution_time = ast_tvdiff_ms(start, hook->timer.trip_time);
  1137. while (hook->timer.interval < execution_time) {
  1138. execution_time -= hook->timer.interval;
  1139. }
  1140. hook->timer.trip_time = ast_tvadd(start, ast_samp2tv(hook->timer.interval - execution_time, 1000));
  1141. hook->timer.seqno = ast_atomic_fetchadd_int((int *) &bridge_channel->features->interval_sequence, +1);
  1142. if (ast_heap_push(interval_hooks, hook)) {
  1143. /* Could not push the hook back onto the heap. */
  1144. ao2_ref(hook, -1);
  1145. }
  1146. }
  1147. ast_heap_unlock(interval_hooks);
  1148. if (chan_suspended) {
  1149. ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
  1150. bridge_channel_unsuspend(bridge_channel);
  1151. }
  1152. }
  1153. /*!
  1154. * \internal
  1155. * \brief Write a DTMF stream out to a channel
  1156. */
  1157. static int bridge_channel_write_dtmf_stream(struct ast_bridge_channel *bridge_channel, const char *dtmf)
  1158. {
  1159. return bridge_channel_write_action_data(bridge_channel,
  1160. BRIDGE_CHANNEL_ACTION_DTMF_STREAM, dtmf, strlen(dtmf) + 1);
  1161. }
  1162. /*!
  1163. * \internal
  1164. * \brief Indicate to the testsuite a feature was successfully detected.
  1165. *
  1166. * Currently, this function only will relay built-in features to the testsuite,
  1167. * but it could be modified to detect applicationmap items should the need arise.
  1168. *
  1169. * \param chan The channel that activated the feature
  1170. * \param dtmf The DTMF sequence entered to activate the feature
  1171. */
  1172. static void testsuite_notify_feature_success(struct ast_channel *chan, const char *dtmf)
  1173. {
  1174. #ifdef TEST_FRAMEWORK
  1175. char *feature = "unknown";
  1176. struct ast_featuremap_config *featuremap = ast_get_chan_featuremap_config(chan);
  1177. struct ast_features_xfer_config *xfer = ast_get_chan_features_xfer_config(chan);
  1178. if (featuremap) {
  1179. if (!strcmp(dtmf, featuremap->blindxfer)) {
  1180. feature = "blindxfer";
  1181. } else if (!strcmp(dtmf, featuremap->atxfer)) {
  1182. feature = "atxfer";
  1183. } else if (!strcmp(dtmf, featuremap->disconnect)) {
  1184. feature = "disconnect";
  1185. } else if (!strcmp(dtmf, featuremap->automon)) {
  1186. feature = "automon";
  1187. } else if (!strcmp(dtmf, featuremap->automixmon)) {
  1188. feature = "automixmon";
  1189. } else if (!strcmp(dtmf, featuremap->parkcall)) {
  1190. feature = "parkcall";
  1191. } else if (!strcmp(dtmf, xfer->atxferthreeway)) {
  1192. feature = "atxferthreeway";
  1193. }
  1194. }
  1195. ast_test_suite_event_notify("FEATURE_DETECTION",
  1196. "Result: success\r\n"
  1197. "Feature: %s", feature);
  1198. #endif /* TEST_FRAMEWORK */
  1199. }
  1200. /*!
  1201. * \internal
  1202. * \brief Internal function that executes a feature on a bridge channel
  1203. * \note Neither the bridge nor the bridge_channel locks should be held when entering
  1204. * this function.
  1205. */
  1206. static void bridge_channel_feature(struct ast_bridge_channel *bridge_channel, const char *starting_dtmf)
  1207. {
  1208. struct ast_bridge_features *features = bridge_channel->features;
  1209. struct ast_bridge_hook_dtmf *hook = NULL;
  1210. char dtmf[MAXIMUM_DTMF_FEATURE_STRING];
  1211. size_t dtmf_len;
  1212. unsigned int digit_timeout;
  1213. RAII_VAR(struct ast_features_general_config *, gen_cfg, NULL, ao2_cleanup);
  1214. ast_channel_lock(bridge_channel->chan);
  1215. gen_cfg = ast_get_chan_features_general_config(bridge_channel->chan);
  1216. if (!gen_cfg) {
  1217. ast_log(LOG_ERROR, "Unable to retrieve features configuration.\n");
  1218. ast_channel_unlock(bridge_channel->chan);
  1219. return;
  1220. }
  1221. digit_timeout = gen_cfg->featuredigittimeout;
  1222. ast_channel_unlock(bridge_channel->chan);
  1223. if (ast_strlen_zero(starting_dtmf)) {
  1224. dtmf[0] = '\0';
  1225. dtmf_len = 0;
  1226. } else {
  1227. ast_copy_string(dtmf, starting_dtmf, sizeof(dtmf));
  1228. dtmf_len = strlen(dtmf);
  1229. }
  1230. /*
  1231. * Check if any feature DTMF hooks match or could match and
  1232. * try to collect more DTMF digits.
  1233. */
  1234. for (;;) {
  1235. int res;
  1236. if (dtmf_len) {
  1237. ast_debug(1, "DTMF feature string on %p(%s) is now '%s'\n",
  1238. bridge_channel, ast_channel_name(bridge_channel->chan), dtmf);
  1239. /* See if a DTMF feature hook matches or can match */
  1240. hook = ao2_find(features->dtmf_hooks, dtmf, OBJ_PARTIAL_KEY);
  1241. if (!hook) {
  1242. ast_debug(1, "No DTMF feature hooks on %p(%s) match '%s'\n",
  1243. bridge_channel, ast_channel_name(bridge_channel->chan), dtmf);
  1244. break;
  1245. }
  1246. if (strlen(hook->dtmf.code) == dtmf_len) {
  1247. ast_debug(1, "DTMF feature hook %p matched DTMF string '%s' on %p(%s)\n",
  1248. hook, dtmf, bridge_channel, ast_channel_name(bridge_channel->chan));
  1249. break;
  1250. }
  1251. ao2_ref(hook, -1);
  1252. hook = NULL;
  1253. if (ARRAY_LEN(dtmf) - 1 <= dtmf_len) {
  1254. /* We have reached the maximum length of a DTMF feature string. */
  1255. break;
  1256. }
  1257. }
  1258. res = ast_waitfordigit(bridge_channel->chan, digit_timeout);
  1259. if (!res) {
  1260. ast_debug(1, "DTMF feature string collection on %p(%s) timed out\n",
  1261. bridge_channel, ast_channel_name(bridge_channel->chan));
  1262. break;
  1263. }
  1264. if (res < 0) {
  1265. ast_debug(1, "DTMF feature string collection failed on %p(%s) for some reason\n",
  1266. bridge_channel, ast_channel_name(bridge_channel->chan));
  1267. break;
  1268. }
  1269. /* Add the new DTMF into the DTMF string so we can do our matching */
  1270. dtmf[dtmf_len] = res;
  1271. dtmf[++dtmf_len] = '\0';
  1272. }
  1273. if (hook) {
  1274. int remove_me;
  1275. /* Execute the matched hook on this channel. */
  1276. remove_me = hook->generic.callback(bridge_channel, hook->generic.hook_pvt);
  1277. if (remove_me) {
  1278. ast_debug(1, "DTMF hook %p is being removed from %p(%s)\n",
  1279. hook, bridge_channel, ast_channel_name(bridge_channel->chan));
  1280. ao2_unlink(features->dtmf_hooks, hook);
  1281. }
  1282. testsuite_notify_feature_success(bridge_channel->chan, hook->dtmf.code);
  1283. ao2_ref(hook, -1);
  1284. /*
  1285. * If we are handing the channel off to an external hook for
  1286. * ownership, we are not guaranteed what kind of state it will
  1287. * come back in. If the channel hungup, we need to detect that
  1288. * here if the hook did not already change the state.
  1289. */
  1290. if (bridge_channel->chan && ast_check_hangup_locked(bridge_channel->chan)) {
  1291. ast_bridge_channel_kick(bridge_channel, 0);
  1292. }
  1293. } else {
  1294. if (features->dtmf_passthrough) {
  1295. /* Stream any collected DTMF to the other channels. */
  1296. bridge_channel_write_dtmf_stream(bridge_channel, dtmf);
  1297. }
  1298. ast_test_suite_event_notify("FEATURE_DETECTION", "Result: fail");
  1299. }
  1300. }
  1301. /*!
  1302. * \internal
  1303. * \brief Indicate that a bridge_channel is talking
  1304. */
  1305. static void bridge_channel_talking(struct ast_bridge_channel *bridge_channel, int talking)
  1306. {
  1307. struct ast_bridge_features *features = bridge_channel->features;
  1308. struct ast_bridge_hook *hook;
  1309. struct ao2_iterator iter;
  1310. /* Run any talk detection hooks. */
  1311. iter = ao2_iterator_init(features->other_hooks, 0);
  1312. for (; (hook = ao2_iterator_next(&iter)); ao2_ref(hook, -1)) {
  1313. int remove_me;
  1314. ast_bridge_talking_indicate_callback talk_cb;
  1315. if (hook->type != AST_BRIDGE_HOOK_TYPE_TALK) {
  1316. continue;
  1317. }
  1318. talk_cb = (ast_bridge_talking_indicate_callback) hook->callback;
  1319. remove_me = talk_cb(bridge_channel, hook->hook_pvt, talking);
  1320. if (remove_me) {
  1321. ast_debug(1, "Talk detection hook %p is being removed from %p(%s)\n",
  1322. hook, bridge_channel, ast_channel_name(bridge_channel->chan));
  1323. ao2_unlink(features->other_hooks, hook);
  1324. }
  1325. }
  1326. ao2_iterator_destroy(&iter);
  1327. }
  1328. /*! \brief Internal function that plays back DTMF on a bridge channel */
  1329. static void bridge_channel_dtmf_stream(struct ast_bridge_channel *bridge_channel, const char *dtmf)
  1330. {
  1331. ast_debug(1, "Playing DTMF stream '%s' out to %p(%s)\n",
  1332. dtmf, bridge_channel, ast_channel_name(bridge_channel->chan));
  1333. ast_dtmf_stream(bridge_channel->chan, NULL, dtmf, 0, 0);
  1334. }
  1335. /*! \brief Data specifying where a blind transfer is going to */
  1336. struct blind_transfer_data {
  1337. char exten[AST_MAX_EXTENSION];
  1338. char context[AST_MAX_CONTEXT];
  1339. };
  1340. /*!
  1341. * \internal
  1342. * \brief Execute after bridge actions on a channel when it leaves a bridge
  1343. */
  1344. static void after_bridge_move_channel(struct ast_channel *chan_bridged, void *data)
  1345. {
  1346. RAII_VAR(struct ast_channel *, chan_target, data, ao2_cleanup);
  1347. struct ast_party_connected_line connected_target;
  1348. unsigned char connected_line_data[1024];
  1349. int payload_size;
  1350. ast_party_connected_line_init(&connected_target);
  1351. ast_channel_lock(chan_target);
  1352. ast_party_connected_line_copy(&connected_target, ast_channel_connected(chan_target));
  1353. ast_channel_unlock(chan_target);
  1354. ast_party_id_reset(&connected_target.priv);
  1355. if (ast_channel_move(chan_target, chan_bridged)) {
  1356. ast_softhangup(chan_target, AST_SOFTHANGUP_DEV);
  1357. ast_party_connected_line_free(&connected_target);
  1358. return;
  1359. }
  1360. if ((payload_size = ast_connected_line_build_data(connected_line_data,
  1361. sizeof(connected_line_data), &connected_target, NULL)) != -1) {
  1362. struct ast_control_read_action_payload *frame_payload;
  1363. int frame_size;
  1364. frame_size = payload_size + sizeof(*frame_payload);
  1365. frame_payload = ast_alloca(frame_size);
  1366. frame_payload->action = AST_FRAME_READ_ACTION_CONNECTED_LINE_MACRO;
  1367. frame_payload->payload_size = payload_size;
  1368. memcpy(frame_payload->payload, connected_line_data, payload_size);
  1369. ast_queue_control_data(chan_target, AST_CONTROL_READ_ACTION, frame_payload, frame_size);
  1370. }
  1371. ast_party_connected_line_free(&connected_target);
  1372. }
  1373. /*!
  1374. * \internal
  1375. * \brief Execute logic to cleanup when after bridge fails
  1376. */
  1377. static void after_bridge_move_channel_fail(enum ast_bridge_after_cb_reason reason, void *data)
  1378. {
  1379. RAII_VAR(struct ast_channel *, chan_target, data, ao2_cleanup);
  1380. ast_log(LOG_WARNING, "Unable to complete transfer: %s\n",
  1381. ast_bridge_after_cb_reason_string(reason));
  1382. ast_softhangup(chan_target, AST_SOFTHANGUP_DEV);
  1383. }
  1384. /*!
  1385. * \internal
  1386. * \brief Perform a blind transfer on a channel in a bridge
  1387. */
  1388. static void bridge_channel_blind_transfer(struct ast_bridge_channel *bridge_channel,
  1389. struct blind_transfer_data *blind_data)
  1390. {
  1391. ast_async_goto(bridge_channel->chan, blind_data->context, blind_data->exten, 1);
  1392. ast_bridge_channel_kick(bridge_channel, AST_CAUSE_NORMAL_CLEARING);
  1393. }
  1394. /*!
  1395. * \internal
  1396. * \brief Perform an attended transfer on a channel in a bridge
  1397. */
  1398. static void bridge_channel_attended_transfer(struct ast_bridge_channel *bridge_channel,
  1399. const char *target_chan_name)
  1400. {
  1401. RAII_VAR(struct ast_channel *, chan_target, NULL, ao2_cleanup);
  1402. RAII_VAR(struct ast_channel *, chan_bridged, NULL, ao2_cleanup);
  1403. chan_target = ast_channel_get_by_name(target_chan_name);
  1404. if (!chan_target) {
  1405. /* Dang, it disappeared somehow */
  1406. ast_bridge_channel_kick(bridge_channel, AST_CAUSE_NORMAL_CLEARING);
  1407. return;
  1408. }
  1409. ast_bridge_channel_lock(bridge_channel);
  1410. chan_bridged = bridge_channel->chan;
  1411. ast_assert(chan_bridged != NULL);
  1412. ao2_ref(chan_bridged, +1);
  1413. ast_bridge_channel_unlock(bridge_channel);
  1414. if (ast_bridge_set_after_callback(chan_bridged, after_bridge_move_channel,
  1415. after_bridge_move_channel_fail, ast_channel_ref(chan_target))) {
  1416. ast_softhangup(chan_target, AST_SOFTHANGUP_DEV);
  1417. /* Release the ref we tried to pass to ast_bridge_set_after_callback(). */
  1418. ast_channel_unref(chan_target);
  1419. }
  1420. ast_bridge_channel_kick(bridge_channel, AST_CAUSE_NORMAL_CLEARING);
  1421. }
  1422. /*!
  1423. * \internal
  1424. * \brief Handle bridge channel bridge action frame.
  1425. * \since 12.0.0
  1426. *
  1427. * \param bridge_channel Channel to execute the action on.
  1428. * \param action What to do.
  1429. * \param data data from the action.
  1430. *
  1431. * \return Nothing
  1432. */
  1433. static void bridge_channel_handle_action(struct ast_bridge_channel *bridge_channel,
  1434. enum bridge_channel_action_type action, void *data)
  1435. {
  1436. switch (action) {
  1437. case BRIDGE_CHANNEL_ACTION_DTMF_STREAM:
  1438. bridge_channel_suspend(bridge_channel);
  1439. ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
  1440. bridge_channel_dtmf_stream(bridge_channel, data);
  1441. ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
  1442. bridge_channel_unsuspend(bridge_channel);
  1443. break;
  1444. case BRIDGE_CHANNEL_ACTION_TALKING_START:
  1445. case BRIDGE_CHANNEL_ACTION_TALKING_STOP:
  1446. bridge_channel_talking(bridge_channel,
  1447. action == BRIDGE_CHANNEL_ACTION_TALKING_START);
  1448. break;
  1449. case BRIDGE_CHANNEL_ACTION_PLAY_FILE:
  1450. bridge_channel_suspend(bridge_channel);
  1451. ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
  1452. bridge_channel_playfile(bridge_channel, data);
  1453. ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
  1454. bridge_channel_unsuspend(bridge_channel);
  1455. break;
  1456. case BRIDGE_CHANNEL_ACTION_RUN_APP:
  1457. bridge_channel_suspend(bridge_channel);
  1458. ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
  1459. bridge_channel_run_app(bridge_channel, data);
  1460. ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
  1461. bridge_channel_unsuspend(bridge_channel);
  1462. break;
  1463. case BRIDGE_CHANNEL_ACTION_CALLBACK:
  1464. bridge_channel_do_callback(bridge_channel, data);
  1465. break;
  1466. case BRIDGE_CHANNEL_ACTION_PARK:
  1467. bridge_channel_suspend(bridge_channel);
  1468. ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
  1469. bridge_channel_park(bridge_channel, data);
  1470. ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
  1471. bridge_channel_unsuspend(bridge_channel);
  1472. break;
  1473. case BRIDGE_CHANNEL_ACTION_BLIND_TRANSFER:
  1474. bridge_channel_blind_transfer(bridge_channel, data);
  1475. break;
  1476. case BRIDGE_CHANNEL_ACTION_ATTENDED_TRANSFER:
  1477. bridge_channel_attended_transfer(bridge_channel, data);
  1478. break;
  1479. default:
  1480. break;
  1481. }
  1482. }
  1483. /*!
  1484. * \internal
  1485. * \brief Check if a bridge should dissolve and do it.
  1486. * \since 12.0.0
  1487. *
  1488. * \param bridge_channel Channel causing the check.
  1489. *
  1490. * \note On entry, bridge_channel->bridge is already locked.
  1491. *
  1492. * \return Nothing
  1493. */
  1494. static void bridge_channel_dissolve_check(struct ast_bridge_channel *bridge_channel)
  1495. {
  1496. struct ast_bridge *bridge = bridge_channel->bridge;
  1497. if (bridge->dissolved) {
  1498. return;
  1499. }
  1500. if (!bridge->num_channels
  1501. && ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_DISSOLVE_EMPTY)) {
  1502. /* Last channel leaving the bridge turns off the lights. */
  1503. bridge_dissolve(bridge, ast_channel_hangupcause(bridge_channel->chan));
  1504. return;
  1505. }
  1506. switch (bridge_channel->state) {
  1507. case BRIDGE_CHANNEL_STATE_END:
  1508. /* Do we need to dissolve the bridge because this channel hung up? */
  1509. if (ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_DISSOLVE_HANGUP)
  1510. || (bridge_channel->features->usable
  1511. && ast_test_flag(&bridge_channel->features->feature_flags,
  1512. AST_BRIDGE_CHANNEL_FLAG_DISSOLVE_HANGUP))) {
  1513. bridge_dissolve(bridge, ast_channel_hangupcause(bridge_channel->chan));
  1514. return;
  1515. }
  1516. break;
  1517. default:
  1518. break;
  1519. }
  1520. if (bridge->num_lonely && bridge->num_lonely == bridge->num_channels) {
  1521. /*
  1522. * This will start a chain reaction where each channel leaving
  1523. * enters this function and causes the next to leave as long as
  1524. * there aren't non-lonely channels in the bridge.
  1525. */
  1526. ast_bridge_channel_leave_bridge(AST_LIST_FIRST(&bridge->channels),
  1527. BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE,
  1528. ast_channel_hangupcause(bridge_channel->chan));
  1529. }
  1530. }
  1531. void bridge_channel_internal_pull(struct ast_bridge_channel *bridge_channel)
  1532. {
  1533. struct ast_bridge *bridge = bridge_channel->bridge;
  1534. if (!bridge_channel->in_bridge) {
  1535. return;
  1536. }
  1537. bridge_channel->in_bridge = 0;
  1538. ast_debug(1, "Bridge %s: pulling %p(%s)\n",
  1539. bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan));
  1540. ast_verb(3, "Channel %s left '%s' %s-bridge <%s>\n",
  1541. ast_channel_name(bridge_channel->chan),
  1542. bridge->technology->name,
  1543. bridge->v_table->name,
  1544. bridge->uniqueid);
  1545. if (!bridge_channel->just_joined) {
  1546. /* Tell the bridge technology we are leaving so they tear us down */
  1547. ast_debug(1, "Bridge %s: %p(%s) is leaving %s technology\n",
  1548. bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan),
  1549. bridge->technology->name);
  1550. if (bridge->technology->leave) {
  1551. bridge->technology->leave(bridge, bridge_channel);
  1552. }
  1553. }
  1554. /* Remove channel from the bridge */
  1555. if (!bridge_channel->suspended) {
  1556. --bridge->num_active;
  1557. }
  1558. if (ast_test_flag(&bridge_channel->features->feature_flags, AST_BRIDGE_CHANNEL_FLAG_LONELY)) {
  1559. --bridge->num_lonely;
  1560. }
  1561. --bridge->num_channels;
  1562. AST_LIST_REMOVE(&bridge->channels, bridge_channel, entry);
  1563. bridge->v_table->pull(bridge, bridge_channel);
  1564. ast_bridge_channel_clear_roles(bridge_channel);
  1565. /* If we are not going to be hung up after leaving a bridge, and we were an
  1566. * outgoing channel, clear the outgoing flag.
  1567. */
  1568. if (ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_OUTGOING)
  1569. && (ast_channel_softhangup_internal_flag(bridge_channel->chan) & (AST_SOFTHANGUP_ASYNCGOTO | AST_SOFTHANGUP_UNBRIDGE)
  1570. || bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT)) {
  1571. ast_debug(2, "Channel %s will survive this bridge; clearing outgoing (dialed) flag\n", ast_channel_name(bridge_channel->chan));
  1572. ast_clear_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_OUTGOING);
  1573. }
  1574. bridge_channel_dissolve_check(bridge_channel);
  1575. bridge->reconfigured = 1;
  1576. ast_bridge_publish_leave(bridge, bridge_channel->chan);
  1577. }
  1578. int bridge_channel_internal_push(struct ast_bridge_channel *bridge_channel)
  1579. {
  1580. struct ast_bridge *bridge = bridge_channel->bridge;
  1581. struct ast_bridge_channel *swap;
  1582. ast_assert(!bridge_channel->in_bridge);
  1583. swap = bridge_find_channel(bridge, bridge_channel->swap);
  1584. bridge_channel->swap = NULL;
  1585. if (swap) {
  1586. ast_debug(1, "Bridge %s: pushing %p(%s) by swapping with %p(%s)\n",
  1587. bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan),
  1588. swap, ast_channel_name(swap->chan));
  1589. } else {
  1590. ast_debug(1, "Bridge %s: pushing %p(%s)\n",
  1591. bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan));
  1592. }
  1593. /* Add channel to the bridge */
  1594. if (bridge->dissolved
  1595. || bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT
  1596. || (swap && swap->state != BRIDGE_CHANNEL_STATE_WAIT)
  1597. || bridge->v_table->push(bridge, bridge_channel, swap)
  1598. || ast_bridge_channel_establish_roles(bridge_channel)) {
  1599. ast_debug(1, "Bridge %s: pushing %p(%s) into bridge failed\n",
  1600. bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan));
  1601. return -1;
  1602. }
  1603. bridge_channel->in_bridge = 1;
  1604. bridge_channel->just_joined = 1;
  1605. AST_LIST_INSERT_TAIL(&bridge->channels, bridge_channel, entry);
  1606. ++bridge->num_channels;
  1607. if (ast_test_flag(&bridge_channel->features->feature_flags, AST_BRIDGE_CHANNEL_FLAG_LONELY)) {
  1608. ++bridge->num_lonely;
  1609. }
  1610. if (!bridge_channel->suspended) {
  1611. ++bridge->num_active;
  1612. }
  1613. ast_verb(3, "Channel %s %s%s%s '%s' %s-bridge <%s>\n",
  1614. ast_channel_name(bridge_channel->chan),
  1615. swap ? "swapped with " : "joined",
  1616. swap ? ast_channel_name(swap->chan) : "",
  1617. swap ? " into" : "",
  1618. bridge->technology->name,
  1619. bridge->v_table->name,
  1620. bridge->uniqueid);
  1621. ast_bridge_publish_enter(bridge, bridge_channel->chan, swap ? swap->chan : NULL);
  1622. if (swap) {
  1623. ast_bridge_channel_leave_bridge(swap, BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, 0);
  1624. bridge_channel_internal_pull(swap);
  1625. }
  1626. /* Clear any BLINDTRANSFER and ATTENDEDTRANSFER since the transfer has completed. */
  1627. pbx_builtin_setvar_helper(bridge_channel->chan, "BLINDTRANSFER", NULL);
  1628. pbx_builtin_setvar_helper(bridge_channel->chan, "ATTENDEDTRANSFER", NULL);
  1629. /* Wake up the bridge channel thread to reevaluate any interval timers. */
  1630. ast_queue_frame(bridge_channel->chan, &ast_null_frame);
  1631. bridge->reconfigured = 1;
  1632. return 0;
  1633. }
  1634. /*!
  1635. * \internal
  1636. * \brief Handle bridge channel control frame action.
  1637. * \since 12.0.0
  1638. *
  1639. * \param bridge_channel Channel to execute the control frame action on.
  1640. * \param fr Control frame to handle.
  1641. *
  1642. * \return Nothing
  1643. */
  1644. static void bridge_channel_handle_control(struct ast_bridge_channel *bridge_channel, struct ast_frame *fr)
  1645. {
  1646. struct ast_channel *chan;
  1647. struct ast_option_header *aoh;
  1648. int is_caller;
  1649. chan = bridge_channel->chan;
  1650. switch (fr->subclass.integer) {
  1651. case AST_CONTROL_REDIRECTING:
  1652. is_caller = !ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING);
  1653. if (ast_channel_redirecting_sub(NULL, chan, fr, 1) &&
  1654. ast_channel_redirecting_macro(NULL, chan, fr, is_caller, 1)) {
  1655. ast_indicate_data(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
  1656. }
  1657. break;
  1658. case AST_CONTROL_CONNECTED_LINE:
  1659. is_caller = !ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING);
  1660. if (ast_channel_connected_line_sub(NULL, chan, fr, 1) &&
  1661. ast_channel_connected_line_macro(NULL, chan, fr, is_caller, 1)) {
  1662. ast_indicate_data(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
  1663. }
  1664. break;
  1665. case AST_CONTROL_OPTION:
  1666. /*
  1667. * Forward option Requests, but only ones we know are safe These
  1668. * are ONLY sent by chan_iax2 and I'm not convinced that they
  1669. * are useful. I haven't deleted them entirely because I just am
  1670. * not sure of the ramifications of removing them.
  1671. */
  1672. aoh = fr->data.ptr;
  1673. if (aoh && aoh->flag == AST_OPTION_FLAG_REQUEST) {
  1674. switch (ntohs(aoh->option)) {
  1675. case AST_OPTION_TONE_VERIFY:
  1676. case AST_OPTION_TDD:
  1677. case AST_OPTION_RELAXDTMF:
  1678. case AST_OPTION_AUDIO_MODE:
  1679. case AST_OPTION_DIGIT_DETECT:
  1680. case AST_OPTION_FAX_DETECT:
  1681. ast_channel_setoption(chan, ntohs(aoh->option), aoh->data,
  1682. fr->datalen - sizeof(*aoh), 0);
  1683. break;
  1684. default:
  1685. break;
  1686. }
  1687. }
  1688. break;
  1689. case AST_CONTROL_ANSWER:
  1690. if (ast_channel_state(chan) != AST_STATE_UP) {
  1691. ast_answer(chan);
  1692. } else {
  1693. ast_indicate(chan, -1);
  1694. }
  1695. break;
  1696. default:
  1697. ast_indicate_data(chan, fr->subclass.integer, fr->data.ptr, fr->datalen);
  1698. break;
  1699. }
  1700. }
  1701. /*!
  1702. * \internal
  1703. * \brief Handle bridge channel write frame to channel.
  1704. * \since 12.0.0
  1705. *
  1706. * \param bridge_channel Channel to write outgoing frame.
  1707. *
  1708. * \return Nothing
  1709. */
  1710. static void bridge_channel_handle_write(struct ast_bridge_channel *bridge_channel)
  1711. {
  1712. struct ast_frame *fr;
  1713. char nudge;
  1714. struct sync_payload *sync_payload;
  1715. ast_bridge_channel_lock(bridge_channel);
  1716. if (read(bridge_channel->alert_pipe[0], &nudge, sizeof(nudge)) < 0) {
  1717. if (errno != EINTR && errno != EAGAIN) {
  1718. ast_log(LOG_WARNING, "read() failed for alert pipe on %p(%s): %s\n",
  1719. bridge_channel, ast_channel_name(bridge_channel->chan), strerror(errno));
  1720. }
  1721. }
  1722. fr = AST_LIST_REMOVE_HEAD(&bridge_channel->wr_queue, frame_list);
  1723. ast_bridge_channel_unlock(bridge_channel);
  1724. if (!fr) {
  1725. return;
  1726. }
  1727. switch (fr->frametype) {
  1728. case AST_FRAME_BRIDGE_ACTION:
  1729. bridge_channel_handle_action(bridge_channel, fr->subclass.integer, fr->data.ptr);
  1730. break;
  1731. case AST_FRAME_BRIDGE_ACTION_SYNC:
  1732. sync_payload = fr->data.ptr;
  1733. bridge_channel_handle_action(bridge_channel, fr->subclass.integer, sync_payload->data);
  1734. break;
  1735. case AST_FRAME_CONTROL:
  1736. bridge_channel_handle_control(bridge_channel, fr);
  1737. break;
  1738. case AST_FRAME_NULL:
  1739. break;
  1740. default:
  1741. /* Write the frame to the channel. */
  1742. bridge_channel->activity = BRIDGE_CHANNEL_THREAD_SIMPLE;
  1743. ast_write(bridge_channel->chan, fr);
  1744. break;
  1745. }
  1746. bridge_frame_free(fr);
  1747. }
  1748. /*! \brief Internal function to handle DTMF from a channel */
  1749. static struct ast_frame *bridge_handle_dtmf(struct ast_bridge_channel *bridge_channel, struct ast_frame *frame)
  1750. {
  1751. struct ast_bridge_features *features = bridge_channel->features;
  1752. struct ast_bridge_hook_dtmf *hook;
  1753. char dtmf[2];
  1754. /* See if this DTMF matches the beginning of any feature hooks. */
  1755. dtmf[0] = frame->subclass.integer;
  1756. dtmf[1] = '\0';
  1757. hook = ao2_find(features->dtmf_hooks, dtmf, OBJ_PARTIAL_KEY);
  1758. if (hook) {
  1759. enum ast_frame_type frametype = frame->frametype;
  1760. bridge_frame_free(frame);
  1761. frame = NULL;
  1762. ao2_ref(hook, -1);
  1763. /* Collect any more needed DTMF to execute a hook. */
  1764. bridge_channel_suspend(bridge_channel);
  1765. ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
  1766. switch (frametype) {
  1767. case AST_FRAME_DTMF_BEGIN:
  1768. bridge_channel_feature(bridge_channel, NULL);
  1769. break;
  1770. case AST_FRAME_DTMF_END:
  1771. bridge_channel_feature(bridge_channel, dtmf);
  1772. break;
  1773. default:
  1774. /* Unexpected frame type. */
  1775. ast_assert(0);
  1776. break;
  1777. }
  1778. ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
  1779. bridge_channel_unsuspend(bridge_channel);
  1780. #ifdef TEST_FRAMEWORK
  1781. } else if (frame->frametype == AST_FRAME_DTMF_END) {
  1782. /* Only transmit this event on DTMF end or else every DTMF
  1783. * press will result in the event being broadcast twice
  1784. */
  1785. ast_test_suite_event_notify("FEATURE_DETECTION", "Result: fail");
  1786. #endif
  1787. }
  1788. return frame;
  1789. }
  1790. /*!
  1791. * \internal
  1792. * \brief Feed notification that a frame is waiting on a channel into the bridging core
  1793. *
  1794. * \param bridge_channel Bridge channel the notification was received on
  1795. */
  1796. static void bridge_handle_trip(struct ast_bridge_channel *bridge_channel)
  1797. {
  1798. struct ast_frame *frame;
  1799. if (bridge_channel->features->mute) {
  1800. frame = ast_read_noaudio(bridge_channel->chan);
  1801. } else {
  1802. frame = ast_read(bridge_channel->chan);
  1803. }
  1804. if (!frame) {
  1805. ast_bridge_channel_kick(bridge_channel, 0);
  1806. return;
  1807. }
  1808. switch (frame->frametype) {
  1809. case AST_FRAME_CONTROL:
  1810. switch (frame->subclass.integer) {
  1811. case AST_CONTROL_HANGUP:
  1812. ast_bridge_channel_kick(bridge_channel, 0);
  1813. bridge_frame_free(frame);
  1814. return;
  1815. default:
  1816. break;
  1817. }
  1818. break;
  1819. case AST_FRAME_DTMF_BEGIN:
  1820. case AST_FRAME_DTMF_END:
  1821. frame = bridge_handle_dtmf(bridge_channel, frame);
  1822. if (!frame) {
  1823. return;
  1824. }
  1825. if (!bridge_channel->features->dtmf_passthrough) {
  1826. bridge_frame_free(frame);
  1827. return;
  1828. }
  1829. break;
  1830. default:
  1831. break;
  1832. }
  1833. /* Simply write the frame out to the bridge technology. */
  1834. bridge_channel_write_frame(bridge_channel, frame);
  1835. bridge_frame_free(frame);
  1836. }
  1837. /*!
  1838. * \internal
  1839. * \brief Determine how long till the next timer interval.
  1840. * \since 12.0.0
  1841. *
  1842. * \param bridge_channel Channel to determine how long can wait.
  1843. *
  1844. * \retval ms Number of milliseconds to wait.
  1845. * \retval -1 to wait forever.
  1846. */
  1847. static int bridge_channel_next_interval(struct ast_bridge_channel *bridge_channel)
  1848. {
  1849. struct ast_heap *interval_hooks = bridge_channel->features->interval_hooks;
  1850. struct ast_bridge_hook_timer *hook;
  1851. int ms;
  1852. ast_heap_wrlock(interval_hooks);
  1853. hook = ast_heap_peek(interval_hooks, 1);
  1854. if (hook) {
  1855. ms = ast_tvdiff_ms(hook->timer.trip_time, ast_tvnow());
  1856. if (ms < 0) {
  1857. /* Expire immediately. An interval hook is ready to run. */
  1858. ms = 0;
  1859. }
  1860. } else {
  1861. /* No hook so wait forever. */
  1862. ms = -1;
  1863. }
  1864. ast_heap_unlock(interval_hooks);
  1865. return ms;
  1866. }
  1867. /*!
  1868. * \internal
  1869. * \brief Wait for something to happen on the bridge channel and handle it.
  1870. * \since 12.0.0
  1871. *
  1872. * \param bridge_channel Channel to wait.
  1873. *
  1874. * \note Each channel does writing/reading in their own thread.
  1875. *
  1876. * \return Nothing
  1877. */
  1878. static void bridge_channel_wait(struct ast_bridge_channel *bridge_channel)
  1879. {
  1880. int ms;
  1881. int outfd;
  1882. struct ast_channel *chan;
  1883. /* Wait for data to either come from the channel or us to be signaled */
  1884. ast_bridge_channel_lock(bridge_channel);
  1885. if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
  1886. } else if (bridge_channel->suspended) {
  1887. /* XXX ASTERISK-21271 the external party use of suspended will go away as will these references because this is the bridge channel thread */
  1888. ast_debug(1, "Bridge %s: %p(%s) is going into a signal wait\n",
  1889. bridge_channel->bridge->uniqueid, bridge_channel,
  1890. ast_channel_name(bridge_channel->chan));
  1891. ast_cond_wait(&bridge_channel->cond, ao2_object_get_lockaddr(bridge_channel));
  1892. } else {
  1893. ast_bridge_channel_unlock(bridge_channel);
  1894. outfd = -1;
  1895. ms = bridge_channel_next_interval(bridge_channel);
  1896. chan = ast_waitfor_nandfds(&bridge_channel->chan, 1,
  1897. &bridge_channel->alert_pipe[0], 1, NULL, &outfd, &ms);
  1898. if (ast_channel_softhangup_internal_flag(bridge_channel->chan) & AST_SOFTHANGUP_UNBRIDGE) {
  1899. ast_channel_clear_softhangup(bridge_channel->chan, AST_SOFTHANGUP_UNBRIDGE);
  1900. ast_bridge_channel_lock_bridge(bridge_channel);
  1901. bridge_channel->bridge->reconfigured = 1;
  1902. bridge_reconfigured(bridge_channel->bridge, 0);
  1903. ast_bridge_unlock(bridge_channel->bridge);
  1904. }
  1905. ast_bridge_channel_lock(bridge_channel);
  1906. bridge_channel->activity = BRIDGE_CHANNEL_THREAD_FRAME;
  1907. ast_bridge_channel_unlock(bridge_channel);
  1908. if (!bridge_channel->suspended
  1909. && bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
  1910. if (chan) {
  1911. bridge_handle_trip(bridge_channel);
  1912. } else if (-1 < outfd) {
  1913. bridge_channel_handle_write(bridge_channel);
  1914. } else if (ms == 0) {
  1915. /* An interval expired. */
  1916. bridge_channel_handle_interval(bridge_channel);
  1917. }
  1918. }
  1919. bridge_channel->activity = BRIDGE_CHANNEL_THREAD_IDLE;
  1920. return;
  1921. }
  1922. ast_bridge_channel_unlock(bridge_channel);
  1923. }
  1924. /*!
  1925. * \internal
  1926. * \brief Handle bridge channel join/leave event.
  1927. * \since 12.0.0
  1928. *
  1929. * \param bridge_channel Which channel is involved.
  1930. * \param type Specified join/leave event.
  1931. *
  1932. * \return Nothing
  1933. */
  1934. static void bridge_channel_event_join_leave(struct ast_bridge_channel *bridge_channel, enum ast_bridge_hook_type type)
  1935. {
  1936. struct ast_bridge_features *features = bridge_channel->features;
  1937. struct ast_bridge_hook *hook;
  1938. struct ao2_iterator iter;
  1939. /* Run the specified hooks. */
  1940. iter = ao2_iterator_init(features->other_hooks, 0);
  1941. for (; (hook = ao2_iterator_next(&iter)); ao2_ref(hook, -1)) {
  1942. if (hook->type == type) {
  1943. break;
  1944. }
  1945. }
  1946. if (hook) {
  1947. /* Found the first specified hook to run. */
  1948. bridge_channel_suspend(bridge_channel);
  1949. ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
  1950. do {
  1951. if (hook->type == type) {
  1952. hook->callback(bridge_channel, hook->hook_pvt);
  1953. ao2_unlink(features->other_hooks, hook);
  1954. }
  1955. ao2_ref(hook, -1);
  1956. } while ((hook = ao2_iterator_next(&iter)));
  1957. ast_indicate(bridge_channel->chan, AST_CONTROL_SRCUPDATE);
  1958. bridge_channel_unsuspend(bridge_channel);
  1959. }
  1960. ao2_iterator_destroy(&iter);
  1961. }
  1962. /*! \brief Join a channel to a bridge and handle anything the bridge may want us to do */
  1963. int bridge_channel_internal_join(struct ast_bridge_channel *bridge_channel)
  1964. {
  1965. int res = 0;
  1966. ast_format_copy(&bridge_channel->read_format, ast_channel_readformat(bridge_channel->chan));
  1967. ast_format_copy(&bridge_channel->write_format, ast_channel_writeformat(bridge_channel->chan));
  1968. ast_debug(1, "Bridge %s: %p(%s) is joining\n",
  1969. bridge_channel->bridge->uniqueid,
  1970. bridge_channel, ast_channel_name(bridge_channel->chan));
  1971. /*
  1972. * Directly locking the bridge is safe here because nobody else
  1973. * knows about this bridge_channel yet.
  1974. */
  1975. ast_bridge_lock(bridge_channel->bridge);
  1976. /* Make sure we're still good to be put into a bridge */
  1977. ast_channel_lock(bridge_channel->chan);
  1978. if (ast_channel_internal_bridge(bridge_channel->chan)
  1979. || ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_ZOMBIE)) {
  1980. ast_channel_unlock(bridge_channel->chan);
  1981. ast_bridge_unlock(bridge_channel->bridge);
  1982. ast_debug(1, "Bridge %s: %p(%s) failed to join Bridge\n",
  1983. bridge_channel->bridge->uniqueid,
  1984. bridge_channel,
  1985. ast_channel_name(bridge_channel->chan));
  1986. return -1;
  1987. }
  1988. ast_channel_internal_bridge_set(bridge_channel->chan, bridge_channel->bridge);
  1989. ast_channel_unlock(bridge_channel->chan);
  1990. /* Add the jitterbuffer if the channel requires it */
  1991. ast_jb_enable_for_channel(bridge_channel->chan);
  1992. if (!bridge_channel->bridge->callid) {
  1993. bridge_channel->bridge->callid = ast_read_threadstorage_callid();
  1994. }
  1995. if (bridge_channel_internal_push(bridge_channel)) {
  1996. int cause = bridge_channel->bridge->cause;
  1997. ast_bridge_unlock(bridge_channel->bridge);
  1998. ast_bridge_channel_kick(bridge_channel, cause);
  1999. ast_bridge_channel_lock_bridge(bridge_channel);
  2000. ast_bridge_features_remove(bridge_channel->features,
  2001. AST_BRIDGE_HOOK_REMOVE_ON_PULL);
  2002. bridge_channel_dissolve_check(bridge_channel);
  2003. res = -1;
  2004. }
  2005. bridge_reconfigured(bridge_channel->bridge, !bridge_channel->inhibit_colp);
  2006. if (bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
  2007. /*
  2008. * Indicate a source change since this channel is entering the
  2009. * bridge system only if the bridge technology is not MULTIMIX
  2010. * capable. The MULTIMIX technology has already done it.
  2011. */
  2012. if (!(bridge_channel->bridge->technology->capabilities
  2013. & AST_BRIDGE_CAPABILITY_MULTIMIX)) {
  2014. ast_indicate(bridge_channel->chan, AST_CONTROL_SRCCHANGE);
  2015. }
  2016. ast_bridge_unlock(bridge_channel->bridge);
  2017. bridge_channel_event_join_leave(bridge_channel, AST_BRIDGE_HOOK_TYPE_JOIN);
  2018. while (bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
  2019. /* Wait for something to do. */
  2020. bridge_channel_wait(bridge_channel);
  2021. }
  2022. bridge_channel_event_join_leave(bridge_channel, AST_BRIDGE_HOOK_TYPE_LEAVE);
  2023. ast_bridge_channel_lock_bridge(bridge_channel);
  2024. }
  2025. bridge_channel_internal_pull(bridge_channel);
  2026. bridge_channel_settle_owed_events(bridge_channel->bridge, bridge_channel);
  2027. bridge_reconfigured(bridge_channel->bridge, 1);
  2028. ast_bridge_unlock(bridge_channel->bridge);
  2029. /* Complete any active hold before exiting the bridge. */
  2030. if (ast_channel_hold_state(bridge_channel->chan) == AST_CONTROL_HOLD) {
  2031. ast_debug(1, "Channel %s simulating UNHOLD for bridge end.\n",
  2032. ast_channel_name(bridge_channel->chan));
  2033. ast_indicate(bridge_channel->chan, AST_CONTROL_UNHOLD);
  2034. }
  2035. /* Complete any partial DTMF digit before exiting the bridge. */
  2036. if (ast_channel_sending_dtmf_digit(bridge_channel->chan)) {
  2037. ast_channel_end_dtmf(bridge_channel->chan,
  2038. ast_channel_sending_dtmf_digit(bridge_channel->chan),
  2039. ast_channel_sending_dtmf_tv(bridge_channel->chan), "bridge end");
  2040. }
  2041. /* Indicate a source change since this channel is leaving the bridge system. */
  2042. ast_indicate(bridge_channel->chan, AST_CONTROL_SRCCHANGE);
  2043. /*
  2044. * Wait for any dual redirect to complete.
  2045. *
  2046. * Must be done while "still in the bridge" for ast_async_goto()
  2047. * to work right.
  2048. */
  2049. while (ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_BRIDGE_DUAL_REDIRECT_WAIT)) {
  2050. sched_yield();
  2051. }
  2052. ast_channel_lock(bridge_channel->chan);
  2053. ast_channel_internal_bridge_set(bridge_channel->chan, NULL);
  2054. ast_channel_unlock(bridge_channel->chan);
  2055. ast_bridge_channel_restore_formats(bridge_channel);
  2056. return res;
  2057. }
  2058. int bridge_channel_internal_queue_blind_transfer(struct ast_channel *transferee,
  2059. const char *exten, const char *context,
  2060. transfer_channel_cb new_channel_cb, void *user_data)
  2061. {
  2062. RAII_VAR(struct ast_bridge_channel *, transferee_bridge_channel, NULL, ao2_cleanup);
  2063. struct blind_transfer_data blind_data;
  2064. ast_channel_lock(transferee);
  2065. transferee_bridge_channel = ast_channel_get_bridge_channel(transferee);
  2066. ast_channel_unlock(transferee);
  2067. if (!transferee_bridge_channel) {
  2068. return -1;
  2069. }
  2070. if (new_channel_cb) {
  2071. new_channel_cb(transferee, user_data, AST_BRIDGE_TRANSFER_SINGLE_PARTY);
  2072. }
  2073. ast_copy_string(blind_data.exten, exten, sizeof(blind_data.exten));
  2074. ast_copy_string(blind_data.context, context, sizeof(blind_data.context));
  2075. return bridge_channel_queue_action_data(transferee_bridge_channel,
  2076. BRIDGE_CHANNEL_ACTION_BLIND_TRANSFER, &blind_data, sizeof(blind_data));
  2077. }
  2078. int bridge_channel_internal_queue_attended_transfer(struct ast_channel *transferee,
  2079. struct ast_channel *unbridged_chan)
  2080. {
  2081. RAII_VAR(struct ast_bridge_channel *, transferee_bridge_channel, NULL, ao2_cleanup);
  2082. char unbridged_chan_name[AST_CHANNEL_NAME];
  2083. ast_channel_lock(transferee);
  2084. transferee_bridge_channel = ast_channel_get_bridge_channel(transferee);
  2085. ast_channel_unlock(transferee);
  2086. if (!transferee_bridge_channel) {
  2087. return -1;
  2088. }
  2089. ast_copy_string(unbridged_chan_name, ast_channel_name(unbridged_chan),
  2090. sizeof(unbridged_chan_name));
  2091. return bridge_channel_queue_action_data(transferee_bridge_channel,
  2092. BRIDGE_CHANNEL_ACTION_ATTENDED_TRANSFER, unbridged_chan_name,
  2093. sizeof(unbridged_chan_name));
  2094. }
  2095. int bridge_channel_internal_allows_optimization(struct ast_bridge_channel *bridge_channel)
  2096. {
  2097. return bridge_channel->in_bridge
  2098. && AST_LIST_EMPTY(&bridge_channel->wr_queue);
  2099. }
  2100. /*!
  2101. * \internal
  2102. * \brief Close a pipe.
  2103. * \since 12.0.0
  2104. *
  2105. * \param my_pipe What to close.
  2106. *
  2107. * \return Nothing
  2108. */
  2109. static void pipe_close(int *my_pipe)
  2110. {
  2111. if (my_pipe[0] > -1) {
  2112. close(my_pipe[0]);
  2113. my_pipe[0] = -1;
  2114. }
  2115. if (my_pipe[1] > -1) {
  2116. close(my_pipe[1]);
  2117. my_pipe[1] = -1;
  2118. }
  2119. }
  2120. /*!
  2121. * \internal
  2122. * \brief Initialize a pipe as non-blocking.
  2123. * \since 12.0.0
  2124. *
  2125. * \param my_pipe What to initialize.
  2126. *
  2127. * \retval 0 on success.
  2128. * \retval -1 on error.
  2129. */
  2130. static int pipe_init_nonblock(int *my_pipe)
  2131. {
  2132. int flags;
  2133. my_pipe[0] = -1;
  2134. my_pipe[1] = -1;
  2135. if (pipe(my_pipe)) {
  2136. ast_log(LOG_WARNING, "Can't create pipe! Try increasing max file descriptors with ulimit -n\n");
  2137. return -1;
  2138. }
  2139. flags = fcntl(my_pipe[0], F_GETFL);
  2140. if (fcntl(my_pipe[0], F_SETFL, flags | O_NONBLOCK) < 0) {
  2141. ast_log(LOG_WARNING, "Unable to set read pipe nonblocking! (%d: %s)\n",
  2142. errno, strerror(errno));
  2143. return -1;
  2144. }
  2145. flags = fcntl(my_pipe[1], F_GETFL);
  2146. if (fcntl(my_pipe[1], F_SETFL, flags | O_NONBLOCK) < 0) {
  2147. ast_log(LOG_WARNING, "Unable to set write pipe nonblocking! (%d: %s)\n",
  2148. errno, strerror(errno));
  2149. return -1;
  2150. }
  2151. return 0;
  2152. }
  2153. /* Destroy elements of the bridge channel structure and the bridge channel structure itself */
  2154. static void bridge_channel_destroy(void *obj)
  2155. {
  2156. struct ast_bridge_channel *bridge_channel = obj;
  2157. struct ast_frame *fr;
  2158. if (bridge_channel->callid) {
  2159. bridge_channel->callid = ast_callid_unref(bridge_channel->callid);
  2160. }
  2161. if (bridge_channel->bridge) {
  2162. ao2_ref(bridge_channel->bridge, -1);
  2163. bridge_channel->bridge = NULL;
  2164. }
  2165. /* Flush any unhandled wr_queue frames. */
  2166. while ((fr = AST_LIST_REMOVE_HEAD(&bridge_channel->wr_queue, frame_list))) {
  2167. bridge_frame_free(fr);
  2168. }
  2169. pipe_close(bridge_channel->alert_pipe);
  2170. ast_cond_destroy(&bridge_channel->cond);
  2171. }
  2172. struct ast_bridge_channel *bridge_channel_internal_alloc(struct ast_bridge *bridge)
  2173. {
  2174. struct ast_bridge_channel *bridge_channel;
  2175. bridge_channel = ao2_alloc(sizeof(struct ast_bridge_channel), bridge_channel_destroy);
  2176. if (!bridge_channel) {
  2177. return NULL;
  2178. }
  2179. ast_cond_init(&bridge_channel->cond, NULL);
  2180. if (pipe_init_nonblock(bridge_channel->alert_pipe)) {
  2181. ao2_ref(bridge_channel, -1);
  2182. return NULL;
  2183. }
  2184. if (bridge) {
  2185. bridge_channel->bridge = bridge;
  2186. ao2_ref(bridge_channel->bridge, +1);
  2187. }
  2188. return bridge_channel;
  2189. }