bridge_channel.c 90 KB

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