bridge.c 158 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385
  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 API
  21. *
  22. * \author Joshua Colp <jcolp@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. /*** DOCUMENTATION
  28. <manager name="BridgeTechnologyList" language="en_US">
  29. <synopsis>
  30. List available bridging technologies and their statuses.
  31. </synopsis>
  32. <syntax>
  33. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  34. </syntax>
  35. <description>
  36. <para>Returns detailed information about the available bridging technologies.</para>
  37. </description>
  38. </manager>
  39. <manager name="BridgeTechnologySuspend" language="en_US">
  40. <synopsis>
  41. Suspend a bridging technology.
  42. </synopsis>
  43. <syntax>
  44. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  45. <parameter name="BridgeTechnology" required="true">
  46. <para>The name of the bridging technology to suspend.</para>
  47. </parameter>
  48. </syntax>
  49. <description>
  50. <para>Marks a bridging technology as suspended, which prevents subsequently created bridges from using it.</para>
  51. </description>
  52. </manager>
  53. <manager name="BridgeTechnologyUnsuspend" language="en_US">
  54. <synopsis>
  55. Unsuspend a bridging technology.
  56. </synopsis>
  57. <syntax>
  58. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  59. <parameter name="BridgeTechnology" required="true">
  60. <para>The name of the bridging technology to unsuspend.</para>
  61. </parameter>
  62. </syntax>
  63. <description>
  64. <para>Clears a previously suspended bridging technology, which allows subsequently created bridges to use it.</para>
  65. </description>
  66. </manager>
  67. ***/
  68. #include "asterisk.h"
  69. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  70. #include "asterisk/logger.h"
  71. #include "asterisk/channel.h"
  72. #include "asterisk/options.h"
  73. #include "asterisk/utils.h"
  74. #include "asterisk/lock.h"
  75. #include "asterisk/linkedlists.h"
  76. #include "asterisk/bridge.h"
  77. #include "asterisk/bridge_internal.h"
  78. #include "asterisk/bridge_channel_internal.h"
  79. #include "asterisk/bridge_features.h"
  80. #include "asterisk/bridge_basic.h"
  81. #include "asterisk/bridge_technology.h"
  82. #include "asterisk/bridge_channel.h"
  83. #include "asterisk/bridge_after.h"
  84. #include "asterisk/stasis_bridges.h"
  85. #include "asterisk/stasis_channels.h"
  86. #include "asterisk/stasis_cache_pattern.h"
  87. #include "asterisk/app.h"
  88. #include "asterisk/file.h"
  89. #include "asterisk/module.h"
  90. #include "asterisk/astobj2.h"
  91. #include "asterisk/pbx.h"
  92. #include "asterisk/test.h"
  93. #include "asterisk/_private.h"
  94. #include "asterisk/heap.h"
  95. #include "asterisk/say.h"
  96. #include "asterisk/timing.h"
  97. #include "asterisk/stringfields.h"
  98. #include "asterisk/musiconhold.h"
  99. #include "asterisk/features.h"
  100. #include "asterisk/cli.h"
  101. #include "asterisk/parking.h"
  102. #include "asterisk/core_local.h"
  103. #include "asterisk/core_unreal.h"
  104. #include "asterisk/causes.h"
  105. /*! All bridges container. */
  106. static struct ao2_container *bridges;
  107. static AST_RWLIST_HEAD_STATIC(bridge_technologies, ast_bridge_technology);
  108. static unsigned int optimization_id;
  109. /* Initial starting point for the bridge array of channels */
  110. #define BRIDGE_ARRAY_START 128
  111. /* Grow rate of bridge array of channels */
  112. #define BRIDGE_ARRAY_GROW 32
  113. /* Variable name - stores peer information about the most recent blind transfer */
  114. #define BLINDTRANSFER "BLINDTRANSFER"
  115. /* Variable name - stores peer information about the most recent attended transfer */
  116. #define ATTENDEDTRANSFER "ATTENDEDTRANSFER"
  117. static void cleanup_video_mode(struct ast_bridge *bridge);
  118. static int bridge_make_compatible(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel);
  119. /*! Default DTMF keys for built in features */
  120. static char builtin_features_dtmf[AST_BRIDGE_BUILTIN_END][MAXIMUM_DTMF_FEATURE_STRING];
  121. /*! Function handlers for the built in features */
  122. static ast_bridge_hook_callback builtin_features_handlers[AST_BRIDGE_BUILTIN_END];
  123. /*! Function handlers for built in interval features */
  124. static ast_bridge_builtin_set_limits_fn builtin_interval_handlers[AST_BRIDGE_BUILTIN_INTERVAL_END];
  125. /*! Bridge manager service request */
  126. struct bridge_manager_request {
  127. /*! List of bridge service requests. */
  128. AST_LIST_ENTRY(bridge_manager_request) node;
  129. /*! Refed bridge requesting service. */
  130. struct ast_bridge *bridge;
  131. };
  132. struct bridge_manager_controller {
  133. /*! Condition, used to wake up the bridge manager thread. */
  134. ast_cond_t cond;
  135. /*! Queue of bridge service requests. */
  136. AST_LIST_HEAD_NOLOCK(, bridge_manager_request) service_requests;
  137. /*! Manager thread */
  138. pthread_t thread;
  139. /*! TRUE if the manager needs to stop. */
  140. unsigned int stop:1;
  141. };
  142. /*! Bridge manager controller. */
  143. static struct bridge_manager_controller *bridge_manager;
  144. /*!
  145. * \internal
  146. * \brief Request service for a bridge from the bridge manager.
  147. * \since 12.0.0
  148. *
  149. * \param bridge Requesting service.
  150. *
  151. * \return Nothing
  152. */
  153. static void bridge_manager_service_req(struct ast_bridge *bridge)
  154. {
  155. struct bridge_manager_request *request;
  156. ao2_lock(bridge_manager);
  157. if (bridge_manager->stop) {
  158. ao2_unlock(bridge_manager);
  159. return;
  160. }
  161. /* Create the service request. */
  162. request = ast_calloc(1, sizeof(*request));
  163. if (!request) {
  164. /* Well. This isn't good. */
  165. ao2_unlock(bridge_manager);
  166. return;
  167. }
  168. ao2_ref(bridge, +1);
  169. request->bridge = bridge;
  170. /* Put request into the queue and wake the bridge manager. */
  171. AST_LIST_INSERT_TAIL(&bridge_manager->service_requests, request, node);
  172. ast_cond_signal(&bridge_manager->cond);
  173. ao2_unlock(bridge_manager);
  174. }
  175. int __ast_bridge_technology_register(struct ast_bridge_technology *technology, struct ast_module *module)
  176. {
  177. struct ast_bridge_technology *current;
  178. /* Perform a sanity check to make sure the bridge technology conforms to our needed requirements */
  179. if (ast_strlen_zero(technology->name)
  180. || !technology->capabilities
  181. || !technology->write) {
  182. ast_log(LOG_WARNING, "Bridge technology %s failed registration sanity check.\n",
  183. technology->name);
  184. return -1;
  185. }
  186. AST_RWLIST_WRLOCK(&bridge_technologies);
  187. /* Look for duplicate bridge technology already using this name, or already registered */
  188. AST_RWLIST_TRAVERSE(&bridge_technologies, current, entry) {
  189. if ((!strcasecmp(current->name, technology->name)) || (current == technology)) {
  190. ast_log(LOG_WARNING, "A bridge technology of %s already claims to exist in our world.\n",
  191. technology->name);
  192. AST_RWLIST_UNLOCK(&bridge_technologies);
  193. return -1;
  194. }
  195. }
  196. /* Copy module pointer so reference counting can keep the module from unloading */
  197. technology->mod = module;
  198. /* Insert our new bridge technology into the list and print out a pretty message */
  199. AST_RWLIST_INSERT_TAIL(&bridge_technologies, technology, entry);
  200. AST_RWLIST_UNLOCK(&bridge_technologies);
  201. ast_verb(2, "Registered bridge technology %s\n", technology->name);
  202. return 0;
  203. }
  204. int ast_bridge_technology_unregister(struct ast_bridge_technology *technology)
  205. {
  206. struct ast_bridge_technology *current;
  207. AST_RWLIST_WRLOCK(&bridge_technologies);
  208. /* Ensure the bridge technology is registered before removing it */
  209. AST_RWLIST_TRAVERSE_SAFE_BEGIN(&bridge_technologies, current, entry) {
  210. if (current == technology) {
  211. AST_RWLIST_REMOVE_CURRENT(entry);
  212. ast_verb(2, "Unregistered bridge technology %s\n", technology->name);
  213. break;
  214. }
  215. }
  216. AST_RWLIST_TRAVERSE_SAFE_END;
  217. AST_RWLIST_UNLOCK(&bridge_technologies);
  218. return current ? 0 : -1;
  219. }
  220. /*!
  221. * \internal
  222. * \brief Put an action onto the specified bridge. Don't dup the action frame.
  223. * \since 12.0.0
  224. *
  225. * \param bridge What to queue the action on.
  226. * \param action What to do.
  227. *
  228. * \return Nothing
  229. */
  230. static void bridge_queue_action_nodup(struct ast_bridge *bridge, struct ast_frame *action)
  231. {
  232. ast_debug(1, "Bridge %s: queueing action type:%u sub:%d\n",
  233. bridge->uniqueid, action->frametype, action->subclass.integer);
  234. ast_bridge_lock(bridge);
  235. AST_LIST_INSERT_TAIL(&bridge->action_queue, action, frame_list);
  236. ast_bridge_unlock(bridge);
  237. bridge_manager_service_req(bridge);
  238. }
  239. int ast_bridge_queue_action(struct ast_bridge *bridge, struct ast_frame *action)
  240. {
  241. struct ast_frame *dup;
  242. dup = ast_frdup(action);
  243. if (!dup) {
  244. return -1;
  245. }
  246. bridge_queue_action_nodup(bridge, dup);
  247. return 0;
  248. }
  249. void bridge_dissolve(struct ast_bridge *bridge, int cause)
  250. {
  251. struct ast_bridge_channel *bridge_channel;
  252. struct ast_frame action = {
  253. .frametype = AST_FRAME_BRIDGE_ACTION,
  254. .subclass.integer = BRIDGE_CHANNEL_ACTION_DEFERRED_DISSOLVING,
  255. };
  256. if (bridge->dissolved) {
  257. return;
  258. }
  259. bridge->dissolved = 1;
  260. if (cause <= 0) {
  261. cause = AST_CAUSE_NORMAL_CLEARING;
  262. }
  263. bridge->cause = cause;
  264. ast_debug(1, "Bridge %s: dissolving bridge with cause %d(%s)\n",
  265. bridge->uniqueid, cause, ast_cause2str(cause));
  266. AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
  267. ast_bridge_channel_leave_bridge(bridge_channel,
  268. BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, cause);
  269. }
  270. /* Must defer dissolving bridge because it is already locked. */
  271. ast_bridge_queue_action(bridge, &action);
  272. }
  273. /*!
  274. * \internal
  275. * \brief Check if a bridge should dissolve because of a stolen channel and do it.
  276. * \since 12.0.0
  277. *
  278. * \param bridge Bridge to check.
  279. * \param bridge_channel Stolen channel causing the check. It is not in the bridge to check and may be in another bridge.
  280. *
  281. * \note On entry, bridge and bridge_channel->bridge are already locked.
  282. *
  283. * \return Nothing
  284. */
  285. static void bridge_dissolve_check_stolen(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
  286. {
  287. if (bridge->dissolved) {
  288. return;
  289. }
  290. if (bridge_channel->features->usable
  291. && ast_test_flag(&bridge_channel->features->feature_flags,
  292. AST_BRIDGE_CHANNEL_FLAG_DISSOLVE_HANGUP)) {
  293. /* The stolen channel controlled the bridge it was stolen from. */
  294. bridge_dissolve(bridge, 0);
  295. return;
  296. }
  297. if (bridge->num_channels < 2
  298. && ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_DISSOLVE_HANGUP)) {
  299. /*
  300. * The stolen channel has not left enough channels to keep the
  301. * bridge alive. Assume the stolen channel hung up.
  302. */
  303. bridge_dissolve(bridge, 0);
  304. return;
  305. }
  306. }
  307. /*!
  308. * \internal
  309. * \brief Update connected line information after a bridge has been reconfigured.
  310. *
  311. * \param bridge The bridge itself.
  312. *
  313. * \return Nothing
  314. */
  315. static void bridge_reconfigured_connected_line_update(struct ast_bridge *bridge)
  316. {
  317. struct ast_party_connected_line connected;
  318. struct ast_bridge_channel *bridge_channel = AST_LIST_FIRST(&bridge->channels), *peer;
  319. unsigned char data[1024];
  320. size_t datalen;
  321. if (!bridge_channel ||
  322. !(bridge->technology->capabilities & (AST_BRIDGE_CAPABILITY_1TO1MIX | AST_BRIDGE_CAPABILITY_NATIVE)) ||
  323. !(peer = ast_bridge_channel_peer(bridge_channel)) ||
  324. ast_test_flag(ast_channel_flags(bridge_channel->chan), AST_FLAG_ZOMBIE) ||
  325. ast_test_flag(ast_channel_flags(peer->chan), AST_FLAG_ZOMBIE) ||
  326. ast_check_hangup_locked(bridge_channel->chan) ||
  327. ast_check_hangup_locked(peer->chan)) {
  328. return;
  329. }
  330. ast_party_connected_line_init(&connected);
  331. ast_channel_lock(bridge_channel->chan);
  332. ast_connected_line_copy_from_caller(&connected, ast_channel_caller(bridge_channel->chan));
  333. ast_channel_unlock(bridge_channel->chan);
  334. if ((datalen = ast_connected_line_build_data(data, sizeof(data), &connected, NULL)) != (size_t) -1) {
  335. ast_bridge_channel_queue_control_data(peer, AST_CONTROL_CONNECTED_LINE, data, datalen);
  336. }
  337. ast_channel_lock(peer->chan);
  338. ast_connected_line_copy_from_caller(&connected, ast_channel_caller(peer->chan));
  339. ast_channel_unlock(peer->chan);
  340. if ((datalen = ast_connected_line_build_data(data, sizeof(data), &connected, NULL)) != (size_t) -1) {
  341. ast_bridge_channel_queue_control_data(bridge_channel, AST_CONTROL_CONNECTED_LINE, data, datalen);
  342. }
  343. ast_party_connected_line_free(&connected);
  344. }
  345. /*!
  346. * \internal
  347. * \brief Complete joining a channel to the bridge.
  348. * \since 12.0.0
  349. *
  350. * \param bridge What to operate upon.
  351. * \param bridge_channel What is joining the bridge technology.
  352. *
  353. * \note On entry, bridge is already locked.
  354. *
  355. * \return Nothing
  356. */
  357. static void bridge_channel_complete_join(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
  358. {
  359. /* Make the channel compatible with the bridge */
  360. bridge_make_compatible(bridge, bridge_channel);
  361. /* Tell the bridge technology we are joining so they set us up */
  362. ast_debug(1, "Bridge %s: %p(%s) is joining %s technology\n",
  363. bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan),
  364. bridge->technology->name);
  365. if (bridge->technology->join
  366. && bridge->technology->join(bridge, bridge_channel)) {
  367. ast_debug(1, "Bridge %s: %p(%s) failed to join %s technology\n",
  368. bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan),
  369. bridge->technology->name);
  370. bridge_channel->just_joined = 1;
  371. return;
  372. }
  373. bridge_channel->just_joined = 0;
  374. }
  375. /*!
  376. * \internal
  377. * \brief Complete joining new channels to the bridge.
  378. * \since 12.0.0
  379. *
  380. * \param bridge Check for new channels on this bridge.
  381. *
  382. * \note On entry, bridge is already locked.
  383. *
  384. * \return Nothing
  385. */
  386. static void bridge_complete_join(struct ast_bridge *bridge)
  387. {
  388. struct ast_bridge_channel *bridge_channel;
  389. if (bridge->dissolved) {
  390. /*
  391. * No sense in completing the join on channels for a dissolved
  392. * bridge. They are just going to be removed soon anyway.
  393. * However, we do have reason to abort here because the bridge
  394. * technology may not be able to handle the number of channels
  395. * still in the bridge.
  396. */
  397. return;
  398. }
  399. AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
  400. if (!bridge_channel->just_joined) {
  401. continue;
  402. }
  403. bridge_channel_complete_join(bridge, bridge_channel);
  404. }
  405. }
  406. /*! \brief Helper function used to find the "best" bridge technology given specified capabilities */
  407. static struct ast_bridge_technology *find_best_technology(uint32_t capabilities, struct ast_bridge *bridge)
  408. {
  409. struct ast_bridge_technology *current;
  410. struct ast_bridge_technology *best = NULL;
  411. AST_RWLIST_RDLOCK(&bridge_technologies);
  412. AST_RWLIST_TRAVERSE(&bridge_technologies, current, entry) {
  413. if (current->suspended) {
  414. ast_debug(1, "Bridge technology %s is suspended. Skipping.\n",
  415. current->name);
  416. continue;
  417. }
  418. if (!(current->capabilities & capabilities)) {
  419. ast_debug(1, "Bridge technology %s does not have any capabilities we want.\n",
  420. current->name);
  421. continue;
  422. }
  423. if (best && current->preference <= best->preference) {
  424. ast_debug(1, "Bridge technology %s has less preference than %s (%u <= %u). Skipping.\n",
  425. current->name, best->name, current->preference, best->preference);
  426. continue;
  427. }
  428. if (current->compatible && !current->compatible(bridge)) {
  429. ast_debug(1, "Bridge technology %s is not compatible with properties of existing bridge.\n",
  430. current->name);
  431. continue;
  432. }
  433. best = current;
  434. }
  435. if (best) {
  436. /* Increment it's module reference count if present so it does not get unloaded while in use */
  437. ast_module_ref(best->mod);
  438. ast_debug(1, "Chose bridge technology %s\n", best->name);
  439. }
  440. AST_RWLIST_UNLOCK(&bridge_technologies);
  441. return best;
  442. }
  443. struct tech_deferred_destroy {
  444. struct ast_bridge_technology *tech;
  445. void *tech_pvt;
  446. };
  447. /*!
  448. * \internal
  449. * \brief Deferred destruction of bridge tech private structure.
  450. * \since 12.0.0
  451. *
  452. * \param bridge What to execute the action on.
  453. * \param action Deferred bridge tech destruction.
  454. *
  455. * \note On entry, bridge must not be locked.
  456. *
  457. * \return Nothing
  458. */
  459. static void bridge_tech_deferred_destroy(struct ast_bridge *bridge, struct ast_frame *action)
  460. {
  461. struct tech_deferred_destroy *deferred = action->data.ptr;
  462. struct ast_bridge dummy_bridge = {
  463. .technology = deferred->tech,
  464. .tech_pvt = deferred->tech_pvt,
  465. .creator = bridge->creator,
  466. .name = bridge->name,
  467. .uniqueid = bridge->uniqueid,
  468. };
  469. ast_debug(1, "Bridge %s: calling %s technology destructor (deferred, dummy)\n",
  470. dummy_bridge.uniqueid, dummy_bridge.technology->name);
  471. dummy_bridge.technology->destroy(&dummy_bridge);
  472. ast_module_unref(dummy_bridge.technology->mod);
  473. }
  474. /*!
  475. * \internal
  476. * \brief Handle bridge action frame.
  477. * \since 12.0.0
  478. *
  479. * \param bridge What to execute the action on.
  480. * \param action What to do.
  481. *
  482. * \note On entry, bridge is already locked.
  483. * \note Can be called by the bridge destructor.
  484. *
  485. * \return Nothing
  486. */
  487. static void bridge_action_bridge(struct ast_bridge *bridge, struct ast_frame *action)
  488. {
  489. #if 0 /* In case we need to know when the destructor is calling us. */
  490. int in_destructor = !ao2_ref(bridge, 0);
  491. #endif
  492. switch (action->subclass.integer) {
  493. case BRIDGE_CHANNEL_ACTION_DEFERRED_TECH_DESTROY:
  494. ast_bridge_unlock(bridge);
  495. bridge_tech_deferred_destroy(bridge, action);
  496. ast_bridge_lock(bridge);
  497. break;
  498. case BRIDGE_CHANNEL_ACTION_DEFERRED_DISSOLVING:
  499. ast_bridge_unlock(bridge);
  500. bridge->v_table->dissolving(bridge);
  501. ast_bridge_lock(bridge);
  502. break;
  503. default:
  504. /* Unexpected deferred action type. Should never happen. */
  505. ast_assert(0);
  506. break;
  507. }
  508. }
  509. /*!
  510. * \internal
  511. * \brief Do any pending bridge actions.
  512. * \since 12.0.0
  513. *
  514. * \param bridge What to do actions on.
  515. *
  516. * \note On entry, bridge is already locked.
  517. * \note Can be called by the bridge destructor.
  518. *
  519. * \return Nothing
  520. */
  521. static void bridge_handle_actions(struct ast_bridge *bridge)
  522. {
  523. struct ast_frame *action;
  524. while ((action = AST_LIST_REMOVE_HEAD(&bridge->action_queue, frame_list))) {
  525. switch (action->frametype) {
  526. case AST_FRAME_BRIDGE_ACTION:
  527. bridge_action_bridge(bridge, action);
  528. break;
  529. default:
  530. /* Unexpected deferred frame type. Should never happen. */
  531. ast_assert(0);
  532. break;
  533. }
  534. ast_frfree(action);
  535. }
  536. }
  537. static struct stasis_message *create_bridge_snapshot_message(struct ast_bridge *bridge)
  538. {
  539. RAII_VAR(struct ast_bridge_snapshot *, snapshot, NULL, ao2_cleanup);
  540. if (!ast_bridge_snapshot_type()) {
  541. return NULL;
  542. }
  543. ast_bridge_lock(bridge);
  544. snapshot = ast_bridge_snapshot_create(bridge);
  545. ast_bridge_unlock(bridge);
  546. if (!snapshot) {
  547. return NULL;
  548. }
  549. return stasis_message_create(ast_bridge_snapshot_type(), snapshot);
  550. }
  551. static void destroy_bridge(void *obj)
  552. {
  553. struct ast_bridge *bridge = obj;
  554. ast_debug(1, "Bridge %s: actually destroying %s bridge, nobody wants it anymore\n",
  555. bridge->uniqueid, bridge->v_table->name);
  556. if (bridge->construction_completed) {
  557. RAII_VAR(struct stasis_message *, clear_msg, NULL, ao2_cleanup);
  558. clear_msg = create_bridge_snapshot_message(bridge);
  559. if (clear_msg) {
  560. RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
  561. msg = stasis_cache_clear_create(clear_msg);
  562. if (msg) {
  563. stasis_publish(ast_bridge_topic(bridge), msg);
  564. }
  565. }
  566. }
  567. /* Do any pending actions in the context of destruction. */
  568. ast_bridge_lock(bridge);
  569. bridge_handle_actions(bridge);
  570. ast_bridge_unlock(bridge);
  571. /* There should not be any channels left in the bridge. */
  572. ast_assert(AST_LIST_EMPTY(&bridge->channels));
  573. ast_debug(1, "Bridge %s: calling %s bridge destructor\n",
  574. bridge->uniqueid, bridge->v_table->name);
  575. bridge->v_table->destroy(bridge);
  576. /* Pass off the bridge to the technology to destroy if needed */
  577. if (bridge->technology) {
  578. ast_debug(1, "Bridge %s: calling %s technology stop\n",
  579. bridge->uniqueid, bridge->technology->name);
  580. if (bridge->technology->stop) {
  581. ast_bridge_lock(bridge);
  582. bridge->technology->stop(bridge);
  583. ast_bridge_unlock(bridge);
  584. }
  585. ast_debug(1, "Bridge %s: calling %s technology destructor\n",
  586. bridge->uniqueid, bridge->technology->name);
  587. if (bridge->technology->destroy) {
  588. bridge->technology->destroy(bridge);
  589. }
  590. ast_module_unref(bridge->technology->mod);
  591. bridge->technology = NULL;
  592. }
  593. if (bridge->callid) {
  594. bridge->callid = ast_callid_unref(bridge->callid);
  595. }
  596. cleanup_video_mode(bridge);
  597. stasis_cp_single_unsubscribe(bridge->topics);
  598. ast_string_field_free_memory(bridge);
  599. }
  600. struct ast_bridge *bridge_register(struct ast_bridge *bridge)
  601. {
  602. if (bridge) {
  603. bridge->construction_completed = 1;
  604. ast_bridge_lock(bridge);
  605. ast_bridge_publish_state(bridge);
  606. ast_bridge_unlock(bridge);
  607. if (!ao2_link(bridges, bridge)) {
  608. ast_bridge_destroy(bridge, 0);
  609. bridge = NULL;
  610. }
  611. }
  612. return bridge;
  613. }
  614. struct ast_bridge *bridge_alloc(size_t size, const struct ast_bridge_methods *v_table)
  615. {
  616. struct ast_bridge *bridge;
  617. /* Check v_table that all methods are present. */
  618. if (!v_table
  619. || !v_table->name
  620. || !v_table->destroy
  621. || !v_table->dissolving
  622. || !v_table->push
  623. || !v_table->pull
  624. || !v_table->notify_masquerade
  625. || !v_table->get_merge_priority) {
  626. ast_log(LOG_ERROR, "Virtual method table for bridge class %s not complete.\n",
  627. v_table && v_table->name ? v_table->name : "<unknown>");
  628. ast_assert(0);
  629. return NULL;
  630. }
  631. bridge = ao2_alloc(size, destroy_bridge);
  632. if (!bridge) {
  633. return NULL;
  634. }
  635. if (ast_string_field_init(bridge, 80)) {
  636. ao2_cleanup(bridge);
  637. return NULL;
  638. }
  639. bridge->v_table = v_table;
  640. return bridge;
  641. }
  642. struct ast_bridge *bridge_base_init(struct ast_bridge *self, uint32_t capabilities, unsigned int flags, const char *creator, const char *name, const char *id)
  643. {
  644. char uuid_hold[AST_UUID_STR_LEN];
  645. if (!self) {
  646. return NULL;
  647. }
  648. if (!ast_strlen_zero(id)) {
  649. ast_string_field_set(self, uniqueid, id);
  650. } else {
  651. ast_uuid_generate_str(uuid_hold, AST_UUID_STR_LEN);
  652. ast_string_field_set(self, uniqueid, uuid_hold);
  653. }
  654. ast_string_field_set(self, creator, creator);
  655. if (!ast_strlen_zero(creator)) {
  656. ast_string_field_set(self, name, name);
  657. }
  658. ast_set_flag(&self->feature_flags, flags);
  659. self->allowed_capabilities = capabilities;
  660. if (bridge_topics_init(self) != 0) {
  661. ast_log(LOG_WARNING, "Bridge %s: Could not initialize topics\n",
  662. self->uniqueid);
  663. ao2_ref(self, -1);
  664. return NULL;
  665. }
  666. /* Use our helper function to find the "best" bridge technology. */
  667. self->technology = find_best_technology(capabilities, self);
  668. if (!self->technology) {
  669. ast_log(LOG_WARNING, "Bridge %s: Could not create class %s. No technology to support it.\n",
  670. self->uniqueid, self->v_table->name);
  671. ao2_ref(self, -1);
  672. return NULL;
  673. }
  674. /* Pass off the bridge to the technology to manipulate if needed */
  675. ast_debug(1, "Bridge %s: calling %s technology constructor\n",
  676. self->uniqueid, self->technology->name);
  677. if (self->technology->create && self->technology->create(self)) {
  678. ast_log(LOG_WARNING, "Bridge %s: failed to setup bridge technology %s\n",
  679. self->uniqueid, self->technology->name);
  680. ao2_ref(self, -1);
  681. return NULL;
  682. }
  683. ast_debug(1, "Bridge %s: calling %s technology start\n",
  684. self->uniqueid, self->technology->name);
  685. if (self->technology->start && self->technology->start(self)) {
  686. ast_log(LOG_WARNING, "Bridge %s: failed to start bridge technology %s\n",
  687. self->uniqueid, self->technology->name);
  688. ao2_ref(self, -1);
  689. return NULL;
  690. }
  691. if (!ast_bridge_topic(self)) {
  692. ao2_ref(self, -1);
  693. return NULL;
  694. }
  695. return self;
  696. }
  697. /*!
  698. * \internal
  699. * \brief ast_bridge base class destructor.
  700. * \since 12.0.0
  701. *
  702. * \param self Bridge to operate upon.
  703. *
  704. * \note Stub because of nothing to do.
  705. *
  706. * \return Nothing
  707. */
  708. static void bridge_base_destroy(struct ast_bridge *self)
  709. {
  710. }
  711. /*!
  712. * \internal
  713. * \brief The bridge is being dissolved.
  714. * \since 12.0.0
  715. *
  716. * \param self Bridge to operate upon.
  717. *
  718. * \return Nothing
  719. */
  720. static void bridge_base_dissolving(struct ast_bridge *self)
  721. {
  722. ao2_unlink(bridges, self);
  723. }
  724. /*!
  725. * \internal
  726. * \brief ast_bridge base push method.
  727. * \since 12.0.0
  728. *
  729. * \param self Bridge to operate upon.
  730. * \param bridge_channel Bridge channel to push.
  731. * \param swap Bridge channel to swap places with if not NULL.
  732. *
  733. * \note On entry, self is already locked.
  734. * \note Stub because of nothing to do.
  735. *
  736. * \retval 0 on success
  737. * \retval -1 on failure
  738. */
  739. static int bridge_base_push(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
  740. {
  741. return 0;
  742. }
  743. /*!
  744. * \internal
  745. * \brief ast_bridge base pull method.
  746. * \since 12.0.0
  747. *
  748. * \param self Bridge to operate upon.
  749. * \param bridge_channel Bridge channel to pull.
  750. *
  751. * \note On entry, self is already locked.
  752. *
  753. * \return Nothing
  754. */
  755. static void bridge_base_pull(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
  756. {
  757. ast_bridge_features_remove(bridge_channel->features, AST_BRIDGE_HOOK_REMOVE_ON_PULL);
  758. }
  759. /*!
  760. * \internal
  761. * \brief ast_bridge base notify_masquerade method.
  762. * \since 12.0.0
  763. *
  764. * \param self Bridge to operate upon.
  765. * \param bridge_channel Bridge channel that was masqueraded.
  766. *
  767. * \note On entry, self is already locked.
  768. *
  769. * \return Nothing
  770. */
  771. static void bridge_base_notify_masquerade(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel)
  772. {
  773. self->reconfigured = 1;
  774. }
  775. /*!
  776. * \internal
  777. * \brief Get the merge priority of this bridge.
  778. * \since 12.0.0
  779. *
  780. * \param self Bridge to operate upon.
  781. *
  782. * \note On entry, self is already locked.
  783. *
  784. * \return Merge priority
  785. */
  786. static int bridge_base_get_merge_priority(struct ast_bridge *self)
  787. {
  788. return 0;
  789. }
  790. /*!
  791. * \internal
  792. * \brief ast_bridge base push_peek method.
  793. * \since 13.2.0
  794. *
  795. * \param self Bridge to operate upon.
  796. * \param bridge_channel Bridge channel to push.
  797. * \param swap Bridge channel to swap places with if not NULL.
  798. *
  799. * \note On entry, self is already locked.
  800. * \note Stub because of nothing to do.
  801. *
  802. * \retval 0 on success
  803. * \retval -1 on failure
  804. */
  805. static int bridge_base_push_peek(struct ast_bridge *self, struct ast_bridge_channel *bridge_channel, struct ast_bridge_channel *swap)
  806. {
  807. return 0;
  808. }
  809. struct ast_bridge_methods ast_bridge_base_v_table = {
  810. .name = "base",
  811. .destroy = bridge_base_destroy,
  812. .dissolving = bridge_base_dissolving,
  813. .push = bridge_base_push,
  814. .pull = bridge_base_pull,
  815. .notify_masquerade = bridge_base_notify_masquerade,
  816. .get_merge_priority = bridge_base_get_merge_priority,
  817. .push_peek = bridge_base_push_peek,
  818. };
  819. struct ast_bridge *ast_bridge_base_new(uint32_t capabilities, unsigned int flags, const char *creator, const char *name, const char *id)
  820. {
  821. void *bridge;
  822. bridge = bridge_alloc(sizeof(struct ast_bridge), &ast_bridge_base_v_table);
  823. bridge = bridge_base_init(bridge, capabilities, flags, creator, name, id);
  824. bridge = bridge_register(bridge);
  825. return bridge;
  826. }
  827. int ast_bridge_destroy(struct ast_bridge *bridge, int cause)
  828. {
  829. ast_debug(1, "Bridge %s: telling all channels to leave the party\n", bridge->uniqueid);
  830. ast_bridge_lock(bridge);
  831. bridge_dissolve(bridge, cause);
  832. ast_bridge_unlock(bridge);
  833. ao2_ref(bridge, -1);
  834. return 0;
  835. }
  836. static int bridge_make_compatible(struct ast_bridge *bridge, struct ast_bridge_channel *bridge_channel)
  837. {
  838. struct ast_str *codec_buf = ast_str_alloca(64);
  839. struct ast_format *best_format;
  840. RAII_VAR(struct ast_format *, read_format, NULL, ao2_cleanup);
  841. RAII_VAR(struct ast_format *, write_format, NULL, ao2_cleanup);
  842. ast_channel_lock(bridge_channel->chan);
  843. read_format = ao2_bump(ast_channel_readformat(bridge_channel->chan));
  844. write_format = ao2_bump(ast_channel_writeformat(bridge_channel->chan));
  845. ast_channel_unlock(bridge_channel->chan);
  846. /* Are the formats currently in use something this bridge can handle? */
  847. if (ast_format_cap_iscompatible_format(bridge->technology->format_capabilities, read_format) == AST_FORMAT_CMP_NOT_EQUAL) {
  848. best_format = ast_format_cap_get_format(bridge->technology->format_capabilities, 0);
  849. /* Read format is a no go... */
  850. ast_debug(1, "Bridge technology %s wants to read any of formats %s but channel has %s\n",
  851. bridge->technology->name,
  852. ast_format_cap_get_names(bridge->technology->format_capabilities, &codec_buf),
  853. ast_format_get_name(read_format));
  854. /* Switch read format to the best one chosen */
  855. if (ast_set_read_format(bridge_channel->chan, best_format)) {
  856. ast_log(LOG_WARNING, "Failed to set channel %s to read format %s\n",
  857. ast_channel_name(bridge_channel->chan), ast_format_get_name(best_format));
  858. ao2_cleanup(best_format);
  859. return -1;
  860. }
  861. ast_debug(1, "Bridge %s put channel %s into read format %s\n",
  862. bridge->uniqueid, ast_channel_name(bridge_channel->chan),
  863. ast_format_get_name(best_format));
  864. ao2_cleanup(best_format);
  865. } else {
  866. ast_debug(1, "Bridge %s is happy that channel %s already has read format %s\n",
  867. bridge->uniqueid, ast_channel_name(bridge_channel->chan),
  868. ast_format_get_name(read_format));
  869. }
  870. if (ast_format_cap_iscompatible_format(bridge->technology->format_capabilities, write_format) == AST_FORMAT_CMP_NOT_EQUAL) {
  871. best_format = ast_format_cap_get_format(bridge->technology->format_capabilities, 0);
  872. /* Write format is a no go... */
  873. ast_debug(1, "Bridge technology %s wants to write any of formats %s but channel has %s\n",
  874. bridge->technology->name,
  875. ast_format_cap_get_names(bridge->technology->format_capabilities, &codec_buf),
  876. ast_format_get_name(write_format));
  877. /* Switch write format to the best one chosen */
  878. if (ast_set_write_format(bridge_channel->chan, best_format)) {
  879. ast_log(LOG_WARNING, "Failed to set channel %s to write format %s\n",
  880. ast_channel_name(bridge_channel->chan), ast_format_get_name(best_format));
  881. ao2_cleanup(best_format);
  882. return -1;
  883. }
  884. ast_debug(1, "Bridge %s put channel %s into write format %s\n",
  885. bridge->uniqueid, ast_channel_name(bridge_channel->chan),
  886. ast_format_get_name(best_format));
  887. ao2_cleanup(best_format);
  888. } else {
  889. ast_debug(1, "Bridge %s is happy that channel %s already has write format %s\n",
  890. bridge->uniqueid, ast_channel_name(bridge_channel->chan),
  891. ast_format_get_name(write_format));
  892. }
  893. return 0;
  894. }
  895. /*!
  896. * \internal
  897. * \brief Perform the smart bridge operation.
  898. * \since 12.0.0
  899. *
  900. * \param bridge Work on this bridge.
  901. *
  902. * \details
  903. * Basically see if a new bridge technology should be used instead
  904. * of the current one.
  905. *
  906. * \note On entry, bridge is already locked.
  907. *
  908. * \retval 0 on success.
  909. * \retval -1 on error.
  910. */
  911. static int smart_bridge_operation(struct ast_bridge *bridge)
  912. {
  913. uint32_t new_capabilities;
  914. struct ast_bridge_technology *new_technology;
  915. struct ast_bridge_technology *old_technology = bridge->technology;
  916. struct ast_bridge_channel *bridge_channel;
  917. struct ast_frame *deferred_action;
  918. struct ast_bridge dummy_bridge = {
  919. .technology = bridge->technology,
  920. .tech_pvt = bridge->tech_pvt,
  921. .creator = bridge->creator,
  922. .name = bridge->name,
  923. .uniqueid = bridge->uniqueid,
  924. };
  925. if (bridge->dissolved) {
  926. ast_debug(1, "Bridge %s is dissolved, not performing smart bridge operation.\n",
  927. bridge->uniqueid);
  928. return 0;
  929. }
  930. /* Determine new bridge technology capabilities needed. */
  931. if (2 < bridge->num_channels) {
  932. new_capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX;
  933. new_capabilities &= bridge->allowed_capabilities;
  934. } else {
  935. new_capabilities = AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_1TO1MIX;
  936. new_capabilities &= bridge->allowed_capabilities;
  937. if (!new_capabilities
  938. && (bridge->allowed_capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX)) {
  939. /* Allow switching between different multimix bridge technologies. */
  940. new_capabilities = AST_BRIDGE_CAPABILITY_MULTIMIX;
  941. }
  942. }
  943. /* Find a bridge technology to satisfy the new capabilities. */
  944. new_technology = find_best_technology(new_capabilities, bridge);
  945. if (!new_technology) {
  946. int is_compatible = 0;
  947. if (old_technology->compatible) {
  948. is_compatible = old_technology->compatible(bridge);
  949. } else if (old_technology->capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX) {
  950. is_compatible = 1;
  951. } else if (bridge->num_channels <= 2
  952. && (old_technology->capabilities & AST_BRIDGE_CAPABILITY_1TO1MIX)) {
  953. is_compatible = 1;
  954. }
  955. if (is_compatible) {
  956. ast_debug(1, "Bridge %s could not get a new technology, staying with old technology.\n",
  957. bridge->uniqueid);
  958. return 0;
  959. }
  960. ast_log(LOG_WARNING, "Bridge %s has no technology available to support it.\n",
  961. bridge->uniqueid);
  962. return -1;
  963. }
  964. if (new_technology == old_technology) {
  965. ast_debug(1, "Bridge %s is already using the new technology.\n",
  966. bridge->uniqueid);
  967. ast_module_unref(old_technology->mod);
  968. return 0;
  969. }
  970. if (old_technology->destroy) {
  971. struct tech_deferred_destroy deferred_tech_destroy = {
  972. .tech = dummy_bridge.technology,
  973. .tech_pvt = dummy_bridge.tech_pvt,
  974. };
  975. struct ast_frame action = {
  976. .frametype = AST_FRAME_BRIDGE_ACTION,
  977. .subclass.integer = BRIDGE_CHANNEL_ACTION_DEFERRED_TECH_DESTROY,
  978. .data.ptr = &deferred_tech_destroy,
  979. .datalen = sizeof(deferred_tech_destroy),
  980. };
  981. /*
  982. * We need to defer the bridge technology destroy callback
  983. * because we have the bridge locked.
  984. */
  985. deferred_action = ast_frdup(&action);
  986. if (!deferred_action) {
  987. ast_module_unref(new_technology->mod);
  988. return -1;
  989. }
  990. } else {
  991. deferred_action = NULL;
  992. }
  993. /*
  994. * We are now committed to changing the bridge technology. We
  995. * must not release the bridge lock until we have installed the
  996. * new bridge technology.
  997. */
  998. ast_verb(4, "Bridge %s: switching from %s technology to %s\n",
  999. bridge->uniqueid, old_technology->name, new_technology->name);
  1000. /*
  1001. * Since we are soon going to pass this bridge to a new
  1002. * technology we need to NULL out the tech_pvt pointer but
  1003. * don't worry as it still exists in dummy_bridge, ditto for the
  1004. * old technology.
  1005. */
  1006. bridge->tech_pvt = NULL;
  1007. bridge->technology = new_technology;
  1008. /* Setup the new bridge technology. */
  1009. ast_debug(1, "Bridge %s: calling %s technology constructor\n",
  1010. bridge->uniqueid, new_technology->name);
  1011. if (new_technology->create && new_technology->create(bridge)) {
  1012. ast_log(LOG_WARNING, "Bridge %s: failed to setup bridge technology %s\n",
  1013. bridge->uniqueid, new_technology->name);
  1014. bridge->tech_pvt = dummy_bridge.tech_pvt;
  1015. bridge->technology = dummy_bridge.technology;
  1016. ast_module_unref(new_technology->mod);
  1017. return -1;
  1018. }
  1019. /* To ensure that things are sane for the old technology move the channels it
  1020. * expects to the dummy bridge
  1021. */
  1022. AST_LIST_TRAVERSE_SAFE_BEGIN(&bridge->channels, bridge_channel, entry) {
  1023. if (bridge_channel->just_joined) {
  1024. continue;
  1025. }
  1026. ast_debug(1, "Bridge %s: moving %p(%s) to dummy bridge temporarily\n",
  1027. bridge->uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan));
  1028. AST_LIST_REMOVE_CURRENT(entry);
  1029. AST_LIST_INSERT_TAIL(&dummy_bridge.channels, bridge_channel, entry);
  1030. dummy_bridge.num_channels++;
  1031. if (ast_test_flag(&bridge_channel->features->feature_flags, AST_BRIDGE_CHANNEL_FLAG_LONELY)) {
  1032. dummy_bridge.num_lonely++;
  1033. }
  1034. if (!bridge_channel->suspended) {
  1035. dummy_bridge.num_active++;
  1036. }
  1037. }
  1038. AST_LIST_TRAVERSE_SAFE_END;
  1039. /* Take all the channels out of the old technology */
  1040. AST_LIST_TRAVERSE_SAFE_BEGIN(&dummy_bridge.channels, bridge_channel, entry) {
  1041. ast_debug(1, "Bridge %s: %p(%s) is leaving %s technology (dummy)\n",
  1042. dummy_bridge.uniqueid, bridge_channel, ast_channel_name(bridge_channel->chan),
  1043. old_technology->name);
  1044. if (old_technology->leave) {
  1045. old_technology->leave(&dummy_bridge, bridge_channel);
  1046. }
  1047. AST_LIST_REMOVE_CURRENT(entry);
  1048. AST_LIST_INSERT_TAIL(&bridge->channels, bridge_channel, entry);
  1049. dummy_bridge.num_channels--;
  1050. if (ast_test_flag(&bridge_channel->features->feature_flags, AST_BRIDGE_CHANNEL_FLAG_LONELY)) {
  1051. dummy_bridge.num_lonely--;
  1052. }
  1053. if (!bridge_channel->suspended) {
  1054. dummy_bridge.num_active--;
  1055. }
  1056. }
  1057. AST_LIST_TRAVERSE_SAFE_END;
  1058. ast_debug(1, "Bridge %s: calling %s technology stop\n",
  1059. dummy_bridge.uniqueid, old_technology->name);
  1060. if (old_technology->stop) {
  1061. old_technology->stop(&dummy_bridge);
  1062. }
  1063. /* Add any new channels or re-add existing channels to the bridge. */
  1064. AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
  1065. bridge_channel_complete_join(bridge, bridge_channel);
  1066. }
  1067. ast_debug(1, "Bridge %s: calling %s technology start\n",
  1068. bridge->uniqueid, new_technology->name);
  1069. if (new_technology->start && new_technology->start(bridge)) {
  1070. ast_log(LOG_WARNING, "Bridge %s: failed to start bridge technology %s\n",
  1071. bridge->uniqueid, new_technology->name);
  1072. }
  1073. /*
  1074. * Now that all the channels have been moved over we need to get
  1075. * rid of all the information the old technology may have left
  1076. * around.
  1077. */
  1078. if (old_technology->destroy) {
  1079. ast_debug(1, "Bridge %s: deferring %s technology destructor\n",
  1080. dummy_bridge.uniqueid, old_technology->name);
  1081. bridge_queue_action_nodup(bridge, deferred_action);
  1082. } else {
  1083. ast_debug(1, "Bridge %s: calling %s technology destructor\n",
  1084. dummy_bridge.uniqueid, old_technology->name);
  1085. ast_module_unref(old_technology->mod);
  1086. }
  1087. return 0;
  1088. }
  1089. /*!
  1090. * \internal
  1091. * \brief Bridge channel to check if a BRIDGE_PLAY_SOUND needs to be played.
  1092. * \since 12.0.0
  1093. *
  1094. * \param bridge_channel What to check.
  1095. *
  1096. * \return Nothing
  1097. */
  1098. static void check_bridge_play_sound(struct ast_bridge_channel *bridge_channel)
  1099. {
  1100. const char *play_file;
  1101. ast_channel_lock(bridge_channel->chan);
  1102. play_file = pbx_builtin_getvar_helper(bridge_channel->chan, "BRIDGE_PLAY_SOUND");
  1103. if (!ast_strlen_zero(play_file)) {
  1104. play_file = ast_strdupa(play_file);
  1105. pbx_builtin_setvar_helper(bridge_channel->chan, "BRIDGE_PLAY_SOUND", NULL);
  1106. } else {
  1107. play_file = NULL;
  1108. }
  1109. ast_channel_unlock(bridge_channel->chan);
  1110. if (play_file) {
  1111. ast_bridge_channel_queue_playfile(bridge_channel, NULL, play_file, NULL);
  1112. }
  1113. }
  1114. /*!
  1115. * \internal
  1116. * \brief Check for any BRIDGE_PLAY_SOUND channel variables in the bridge.
  1117. * \since 12.0.0
  1118. *
  1119. * \param bridge What to operate on.
  1120. *
  1121. * \note On entry, the bridge is already locked.
  1122. *
  1123. * \return Nothing
  1124. */
  1125. static void check_bridge_play_sounds(struct ast_bridge *bridge)
  1126. {
  1127. struct ast_bridge_channel *bridge_channel;
  1128. AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
  1129. check_bridge_play_sound(bridge_channel);
  1130. }
  1131. }
  1132. static void update_bridge_vars_set(struct ast_channel *chan, const char *name, const char *pvtid)
  1133. {
  1134. ast_channel_stage_snapshot(chan);
  1135. pbx_builtin_setvar_helper(chan, "BRIDGEPEER", name);
  1136. pbx_builtin_setvar_helper(chan, "BRIDGEPVTCALLID", pvtid);
  1137. ast_channel_stage_snapshot_done(chan);
  1138. }
  1139. /*!
  1140. * \internal
  1141. * \brief Set BRIDGEPEER and BRIDGEPVTCALLID channel variables in a 2 party bridge.
  1142. * \since 12.0.0
  1143. *
  1144. * \param c0 Party of the first part.
  1145. * \param c1 Party of the second part.
  1146. *
  1147. * \note On entry, the bridge is already locked.
  1148. * \note The bridge is expected to have exactly two parties.
  1149. *
  1150. * \return Nothing
  1151. */
  1152. static void set_bridge_peer_vars_2party(struct ast_channel *c0, struct ast_channel *c1)
  1153. {
  1154. const char *c0_name;
  1155. const char *c1_name;
  1156. const char *c0_pvtid = NULL;
  1157. const char *c1_pvtid = NULL;
  1158. #define UPDATE_BRIDGE_VARS_GET(chan, name, pvtid) \
  1159. do { \
  1160. name = ast_strdupa(ast_channel_name(chan)); \
  1161. if (ast_channel_tech(chan)->get_pvt_uniqueid) { \
  1162. pvtid = ast_strdupa(ast_channel_tech(chan)->get_pvt_uniqueid(chan)); \
  1163. } \
  1164. } while (0)
  1165. ast_channel_lock(c1);
  1166. UPDATE_BRIDGE_VARS_GET(c1, c1_name, c1_pvtid);
  1167. ast_channel_unlock(c1);
  1168. ast_channel_lock(c0);
  1169. update_bridge_vars_set(c0, c1_name, c1_pvtid);
  1170. UPDATE_BRIDGE_VARS_GET(c0, c0_name, c0_pvtid);
  1171. ast_channel_unlock(c0);
  1172. ast_channel_lock(c1);
  1173. update_bridge_vars_set(c1, c0_name, c0_pvtid);
  1174. ast_channel_unlock(c1);
  1175. }
  1176. /*!
  1177. * \internal
  1178. * \brief Fill the BRIDGEPEER value buffer with a comma separated list of channel names.
  1179. * \since 12.0.0
  1180. *
  1181. * \param buf Buffer to fill. The caller must guarantee the buffer is large enough.
  1182. * \param cur_idx Which index into names[] to skip.
  1183. * \param names Channel names to put in the buffer.
  1184. * \param num_names Number of names in the array.
  1185. *
  1186. * \return Nothing
  1187. */
  1188. static void fill_bridgepeer_buf(char *buf, unsigned int cur_idx, const char *names[], unsigned int num_names)
  1189. {
  1190. int need_separator = 0;
  1191. unsigned int idx;
  1192. const char *src;
  1193. char *pos;
  1194. pos = buf;
  1195. for (idx = 0; idx < num_names; ++idx) {
  1196. if (idx == cur_idx) {
  1197. continue;
  1198. }
  1199. if (need_separator) {
  1200. *pos++ = ',';
  1201. }
  1202. need_separator = 1;
  1203. /* Copy name into buffer. */
  1204. src = names[idx];
  1205. while (*src) {
  1206. *pos++ = *src++;
  1207. }
  1208. }
  1209. *pos = '\0';
  1210. }
  1211. /*!
  1212. * \internal
  1213. * \brief Set BRIDGEPEER and BRIDGEPVTCALLID channel variables in a multi-party bridge.
  1214. * \since 12.0.0
  1215. *
  1216. * \param bridge What to operate on.
  1217. *
  1218. * \note On entry, the bridge is already locked.
  1219. * \note The bridge is expected to have more than two parties.
  1220. *
  1221. * \return Nothing
  1222. */
  1223. static void set_bridge_peer_vars_multiparty(struct ast_bridge *bridge)
  1224. {
  1225. /*
  1226. * Set a maximum number of channel names for the BRIDGEPEER
  1227. * list. The plus one is for the current channel which is not
  1228. * put in the list.
  1229. */
  1230. #define MAX_BRIDGEPEER_CHANS (10 + 1)
  1231. unsigned int idx;
  1232. unsigned int num_names;
  1233. unsigned int len;
  1234. const char **names;
  1235. char *buf;
  1236. struct ast_bridge_channel *bridge_channel;
  1237. /* Get first MAX_BRIDGEPEER_CHANS channel names. */
  1238. num_names = MIN(bridge->num_channels, MAX_BRIDGEPEER_CHANS);
  1239. names = ast_alloca(num_names * sizeof(*names));
  1240. idx = 0;
  1241. AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
  1242. if (num_names <= idx) {
  1243. break;
  1244. }
  1245. ast_channel_lock(bridge_channel->chan);
  1246. names[idx++] = ast_strdupa(ast_channel_name(bridge_channel->chan));
  1247. ast_channel_unlock(bridge_channel->chan);
  1248. }
  1249. /* Determine maximum buf size needed. */
  1250. len = num_names;
  1251. for (idx = 0; idx < num_names; ++idx) {
  1252. len += strlen(names[idx]);
  1253. }
  1254. buf = ast_alloca(len);
  1255. /* Set the bridge channel variables. */
  1256. idx = 0;
  1257. buf[0] = '\0';
  1258. AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
  1259. if (idx < num_names) {
  1260. fill_bridgepeer_buf(buf, idx, names, num_names);
  1261. }
  1262. ++idx;
  1263. ast_channel_lock(bridge_channel->chan);
  1264. update_bridge_vars_set(bridge_channel->chan, buf, NULL);
  1265. ast_channel_unlock(bridge_channel->chan);
  1266. }
  1267. }
  1268. /*!
  1269. * \internal
  1270. * \brief Set BRIDGEPEER and BRIDGEPVTCALLID channel variables in a holding bridge.
  1271. * \since 12.0.0
  1272. *
  1273. * \param bridge What to operate on.
  1274. *
  1275. * \note On entry, the bridge is already locked.
  1276. *
  1277. * \return Nothing
  1278. */
  1279. static void set_bridge_peer_vars_holding(struct ast_bridge *bridge)
  1280. {
  1281. struct ast_bridge_channel *bridge_channel;
  1282. AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
  1283. ast_channel_lock(bridge_channel->chan);
  1284. update_bridge_vars_set(bridge_channel->chan, NULL, NULL);
  1285. ast_channel_unlock(bridge_channel->chan);
  1286. }
  1287. }
  1288. /*!
  1289. * \internal
  1290. * \brief Set BRIDGEPEER and BRIDGEPVTCALLID channel variables in the bridge.
  1291. * \since 12.0.0
  1292. *
  1293. * \param bridge What to operate on.
  1294. *
  1295. * \note On entry, the bridge is already locked.
  1296. *
  1297. * \return Nothing
  1298. */
  1299. static void set_bridge_peer_vars(struct ast_bridge *bridge)
  1300. {
  1301. if (bridge->technology->capabilities & AST_BRIDGE_CAPABILITY_HOLDING) {
  1302. set_bridge_peer_vars_holding(bridge);
  1303. return;
  1304. }
  1305. if (bridge->num_channels < 2) {
  1306. return;
  1307. }
  1308. if (bridge->num_channels == 2) {
  1309. set_bridge_peer_vars_2party(AST_LIST_FIRST(&bridge->channels)->chan,
  1310. AST_LIST_LAST(&bridge->channels)->chan);
  1311. } else {
  1312. set_bridge_peer_vars_multiparty(bridge);
  1313. }
  1314. }
  1315. void bridge_reconfigured(struct ast_bridge *bridge, unsigned int colp_update)
  1316. {
  1317. if (!bridge->reconfigured) {
  1318. return;
  1319. }
  1320. bridge->reconfigured = 0;
  1321. if (ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_SMART)
  1322. && smart_bridge_operation(bridge)) {
  1323. /* Smart bridge failed. */
  1324. bridge_dissolve(bridge, 0);
  1325. return;
  1326. }
  1327. bridge_complete_join(bridge);
  1328. if (bridge->dissolved) {
  1329. return;
  1330. }
  1331. check_bridge_play_sounds(bridge);
  1332. set_bridge_peer_vars(bridge);
  1333. ast_bridge_publish_state(bridge);
  1334. if (colp_update) {
  1335. bridge_reconfigured_connected_line_update(bridge);
  1336. }
  1337. }
  1338. struct ast_bridge_channel *bridge_find_channel(struct ast_bridge *bridge, struct ast_channel *chan)
  1339. {
  1340. struct ast_bridge_channel *bridge_channel;
  1341. AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
  1342. if (bridge_channel->chan == chan) {
  1343. break;
  1344. }
  1345. }
  1346. return bridge_channel;
  1347. }
  1348. void ast_bridge_notify_masquerade(struct ast_channel *chan)
  1349. {
  1350. struct ast_bridge_channel *bridge_channel;
  1351. struct ast_bridge *bridge;
  1352. /* Safely get the bridge_channel pointer for the chan. */
  1353. ast_channel_lock(chan);
  1354. bridge_channel = ast_channel_get_bridge_channel(chan);
  1355. ast_channel_unlock(chan);
  1356. if (!bridge_channel) {
  1357. /* Not in a bridge */
  1358. return;
  1359. }
  1360. ast_bridge_channel_lock_bridge(bridge_channel);
  1361. bridge = bridge_channel->bridge;
  1362. if (bridge_channel == bridge_find_channel(bridge, chan)) {
  1363. /*
  1364. * XXX ASTERISK-22366 this needs more work. The channels need
  1365. * to be made compatible again if the formats change. The
  1366. * bridge_channel thread needs to monitor for this case.
  1367. */
  1368. /* The channel we want to notify is still in a bridge. */
  1369. bridge->v_table->notify_masquerade(bridge, bridge_channel);
  1370. bridge_reconfigured(bridge, 1);
  1371. }
  1372. ast_bridge_unlock(bridge);
  1373. ao2_ref(bridge_channel, -1);
  1374. }
  1375. /*
  1376. * XXX ASTERISK-21271 make ast_bridge_join() require features to be allocated just like ast_bridge_impart() and not expect the struct back.
  1377. *
  1378. * This change is really going to break ConfBridge. All other
  1379. * users are easily changed. However, it is needed so the
  1380. * bridging code can manipulate features on all channels
  1381. * consistently no matter how they joined.
  1382. *
  1383. * Need to update the features parameter doxygen when this
  1384. * change is made to be like ast_bridge_impart().
  1385. */
  1386. int ast_bridge_join(struct ast_bridge *bridge,
  1387. struct ast_channel *chan,
  1388. struct ast_channel *swap,
  1389. struct ast_bridge_features *features,
  1390. struct ast_bridge_tech_optimizations *tech_args,
  1391. enum ast_bridge_join_flags flags)
  1392. {
  1393. struct ast_bridge_channel *bridge_channel;
  1394. int res = 0;
  1395. bridge_channel = bridge_channel_internal_alloc(bridge);
  1396. if (flags & AST_BRIDGE_JOIN_PASS_REFERENCE) {
  1397. ao2_ref(bridge, -1);
  1398. }
  1399. if (!bridge_channel) {
  1400. ao2_t_cleanup(swap, "Error exit: bridge_channel alloc failed");
  1401. res = -1;
  1402. goto join_exit;
  1403. }
  1404. /* XXX ASTERISK-21271 features cannot be NULL when passed in. When it is changed to allocated we can do like ast_bridge_impart() and allocate one. */
  1405. ast_assert(features != NULL);
  1406. if (!features) {
  1407. ao2_ref(bridge_channel, -1);
  1408. ao2_t_cleanup(swap, "Error exit: features is NULL");
  1409. res = -1;
  1410. goto join_exit;
  1411. }
  1412. if (tech_args) {
  1413. bridge_channel->tech_args = *tech_args;
  1414. }
  1415. ast_channel_lock(chan);
  1416. if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE)) {
  1417. res = -1;
  1418. } else {
  1419. ast_channel_internal_bridge_channel_set(chan, bridge_channel);
  1420. }
  1421. ast_channel_unlock(chan);
  1422. bridge_channel->thread = pthread_self();
  1423. bridge_channel->chan = chan;
  1424. bridge_channel->swap = swap;
  1425. bridge_channel->features = features;
  1426. bridge_channel->inhibit_colp = !!(flags & AST_BRIDGE_JOIN_INHIBIT_JOIN_COLP);
  1427. /* allow subclass to peek at upcoming push operation */
  1428. if (bridge->v_table->push_peek && !res) {
  1429. struct ast_bridge_channel *bcswap = NULL;
  1430. ast_bridge_lock(bridge);
  1431. if (bridge_channel->swap) {
  1432. bcswap = bridge_find_channel(bridge, bridge_channel->swap);
  1433. }
  1434. res = bridge->v_table->push_peek(bridge, bridge_channel, bcswap);
  1435. ast_bridge_unlock(bridge);
  1436. }
  1437. if (!res) {
  1438. res = bridge_channel_internal_join(bridge_channel);
  1439. }
  1440. /* Cleanup all the data in the bridge channel after it leaves the bridge. */
  1441. ast_channel_lock(chan);
  1442. ast_channel_internal_bridge_channel_set(chan, NULL);
  1443. ast_channel_unlock(chan);
  1444. bridge_channel->chan = NULL;
  1445. /* If bridge_channel->swap is not NULL then the join failed. */
  1446. ao2_t_cleanup(bridge_channel->swap, "Bridge complete: join failed");
  1447. bridge_channel->swap = NULL;
  1448. bridge_channel->features = NULL;
  1449. ao2_ref(bridge_channel, -1);
  1450. join_exit:;
  1451. ast_bridge_run_after_callback(chan);
  1452. if (!(ast_channel_softhangup_internal_flag(chan) & AST_SOFTHANGUP_ASYNCGOTO)
  1453. && !ast_bridge_setup_after_goto(chan)) {
  1454. /* Claim the after bridge goto is an async goto destination. */
  1455. ast_channel_lock(chan);
  1456. ast_softhangup_nolock(chan, AST_SOFTHANGUP_ASYNCGOTO);
  1457. ast_channel_unlock(chan);
  1458. }
  1459. return res;
  1460. }
  1461. /*! \brief Thread responsible for imparted bridged channels to be departed */
  1462. static void *bridge_channel_depart_thread(void *data)
  1463. {
  1464. struct ast_bridge_channel *bridge_channel = data;
  1465. if (bridge_channel->callid) {
  1466. ast_callid_threadassoc_add(bridge_channel->callid);
  1467. }
  1468. bridge_channel_internal_join(bridge_channel);
  1469. /*
  1470. * cleanup
  1471. *
  1472. * If bridge_channel->swap is not NULL then the join failed.
  1473. */
  1474. ao2_t_cleanup(bridge_channel->swap, "Bridge complete: Departable impart join failed");
  1475. bridge_channel->swap = NULL;
  1476. ast_bridge_features_destroy(bridge_channel->features);
  1477. bridge_channel->features = NULL;
  1478. ast_bridge_discard_after_callback(bridge_channel->chan, AST_BRIDGE_AFTER_CB_REASON_DEPART);
  1479. ast_bridge_discard_after_goto(bridge_channel->chan);
  1480. return NULL;
  1481. }
  1482. /*! \brief Thread responsible for independent imparted bridged channels */
  1483. static void *bridge_channel_ind_thread(void *data)
  1484. {
  1485. struct ast_bridge_channel *bridge_channel = data;
  1486. struct ast_channel *chan;
  1487. if (bridge_channel->callid) {
  1488. ast_callid_threadassoc_add(bridge_channel->callid);
  1489. }
  1490. bridge_channel_internal_join(bridge_channel);
  1491. chan = bridge_channel->chan;
  1492. /* cleanup */
  1493. ast_channel_lock(chan);
  1494. ast_channel_internal_bridge_channel_set(chan, NULL);
  1495. ast_channel_unlock(chan);
  1496. bridge_channel->chan = NULL;
  1497. /* If bridge_channel->swap is not NULL then the join failed. */
  1498. ao2_t_cleanup(bridge_channel->swap, "Bridge complete: Independent impart join failed");
  1499. bridge_channel->swap = NULL;
  1500. ast_bridge_features_destroy(bridge_channel->features);
  1501. bridge_channel->features = NULL;
  1502. ao2_ref(bridge_channel, -1);
  1503. ast_bridge_run_after_callback(chan);
  1504. ast_bridge_run_after_goto(chan);
  1505. return NULL;
  1506. }
  1507. int ast_bridge_impart(struct ast_bridge *bridge,
  1508. struct ast_channel *chan,
  1509. struct ast_channel *swap,
  1510. struct ast_bridge_features *features,
  1511. enum ast_bridge_impart_flags flags)
  1512. {
  1513. int res = 0;
  1514. struct ast_bridge_channel *bridge_channel;
  1515. /* Imparted channels cannot have a PBX. */
  1516. if (ast_channel_pbx(chan)) {
  1517. ast_log(AST_LOG_WARNING, "Channel %s has a PBX thread and cannot be imparted into bridge %s\n",
  1518. ast_channel_name(chan), bridge->uniqueid);
  1519. ast_bridge_features_destroy(features);
  1520. return -1;
  1521. }
  1522. /* Supply an empty features structure if the caller did not. */
  1523. if (!features) {
  1524. features = ast_bridge_features_new();
  1525. if (!features) {
  1526. return -1;
  1527. }
  1528. }
  1529. /* Try to allocate a structure for the bridge channel */
  1530. bridge_channel = bridge_channel_internal_alloc(bridge);
  1531. if (!bridge_channel) {
  1532. ast_bridge_features_destroy(features);
  1533. return -1;
  1534. }
  1535. ast_channel_lock(chan);
  1536. if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE)) {
  1537. ast_log(AST_LOG_NOTICE, "Channel %s is a zombie and cannot be imparted into bridge %s\n",
  1538. ast_channel_name(chan), bridge->uniqueid);
  1539. res = -1;
  1540. } else {
  1541. ast_channel_internal_bridge_channel_set(chan, bridge_channel);
  1542. }
  1543. ast_channel_unlock(chan);
  1544. bridge_channel->chan = chan;
  1545. bridge_channel->swap = ao2_t_bump(swap, "Setting up bridge impart");
  1546. bridge_channel->features = features;
  1547. bridge_channel->inhibit_colp = !!(flags & AST_BRIDGE_IMPART_INHIBIT_JOIN_COLP);
  1548. bridge_channel->depart_wait =
  1549. (flags & AST_BRIDGE_IMPART_CHAN_MASK) == AST_BRIDGE_IMPART_CHAN_DEPARTABLE;
  1550. bridge_channel->callid = ast_read_threadstorage_callid();
  1551. /* allow subclass to peek at swap channel before it can hangup */
  1552. if (bridge->v_table->push_peek && !res) {
  1553. struct ast_bridge_channel *bcswap = NULL;
  1554. ast_bridge_lock(bridge);
  1555. if (bridge_channel->swap) {
  1556. bcswap = bridge_find_channel(bridge, bridge_channel->swap);
  1557. }
  1558. res = bridge->v_table->push_peek(bridge, bridge_channel, bcswap);
  1559. ast_bridge_unlock(bridge);
  1560. }
  1561. /* Actually create the thread that will handle the channel */
  1562. if (!res) {
  1563. if ((flags & AST_BRIDGE_IMPART_CHAN_MASK) == AST_BRIDGE_IMPART_CHAN_INDEPENDENT) {
  1564. res = ast_pthread_create_detached(&bridge_channel->thread, NULL,
  1565. bridge_channel_ind_thread, bridge_channel);
  1566. } else {
  1567. res = ast_pthread_create(&bridge_channel->thread, NULL,
  1568. bridge_channel_depart_thread, bridge_channel);
  1569. }
  1570. }
  1571. if (res) {
  1572. /* cleanup */
  1573. ast_channel_lock(chan);
  1574. ast_channel_internal_bridge_channel_set(chan, NULL);
  1575. ast_channel_unlock(chan);
  1576. bridge_channel->chan = NULL;
  1577. ao2_t_cleanup(bridge_channel->swap, "Bridge complete: Impart failed");
  1578. bridge_channel->swap = NULL;
  1579. ast_bridge_features_destroy(bridge_channel->features);
  1580. bridge_channel->features = NULL;
  1581. ao2_ref(bridge_channel, -1);
  1582. return -1;
  1583. }
  1584. return 0;
  1585. }
  1586. int ast_bridge_depart(struct ast_channel *chan)
  1587. {
  1588. struct ast_bridge_channel *bridge_channel;
  1589. int departable;
  1590. ast_channel_lock(chan);
  1591. bridge_channel = ast_channel_internal_bridge_channel(chan);
  1592. departable = bridge_channel && bridge_channel->depart_wait;
  1593. ast_channel_unlock(chan);
  1594. if (!departable) {
  1595. ast_log(LOG_ERROR, "Channel %s cannot be departed.\n",
  1596. ast_channel_name(chan));
  1597. /*
  1598. * Should never happen. It likely means that
  1599. * ast_bridge_depart() is called by two threads for the same
  1600. * channel, the channel was never imparted to be departed, or it
  1601. * has already been departed.
  1602. */
  1603. ast_assert(0);
  1604. return -1;
  1605. }
  1606. /*
  1607. * We are claiming the bridge_channel reference held by
  1608. * bridge_channel_depart_thread().
  1609. */
  1610. ast_bridge_channel_leave_bridge(bridge_channel,
  1611. BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, AST_CAUSE_NORMAL_CLEARING);
  1612. /* Wait for the depart thread to die */
  1613. ast_debug(1, "Waiting for %p(%s) bridge thread to die.\n",
  1614. bridge_channel, ast_channel_name(bridge_channel->chan));
  1615. pthread_join(bridge_channel->thread, NULL);
  1616. ast_channel_lock(chan);
  1617. ast_channel_internal_bridge_channel_set(chan, NULL);
  1618. ast_channel_unlock(chan);
  1619. /* We can get rid of the bridge_channel after the depart thread has died. */
  1620. ao2_ref(bridge_channel, -1);
  1621. return 0;
  1622. }
  1623. int ast_bridge_remove(struct ast_bridge *bridge, struct ast_channel *chan)
  1624. {
  1625. struct ast_bridge_channel *bridge_channel;
  1626. ast_bridge_lock(bridge);
  1627. /* Try to find the channel that we want to remove */
  1628. if (!(bridge_channel = bridge_find_channel(bridge, chan))) {
  1629. ast_bridge_unlock(bridge);
  1630. return -1;
  1631. }
  1632. ast_bridge_channel_leave_bridge(bridge_channel,
  1633. BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, AST_CAUSE_NORMAL_CLEARING);
  1634. ast_bridge_unlock(bridge);
  1635. return 0;
  1636. }
  1637. static void kick_it(struct ast_bridge_channel *bridge_channel, const void *payload, size_t payload_size)
  1638. {
  1639. ast_bridge_channel_kick(bridge_channel, AST_CAUSE_NORMAL_CLEARING);
  1640. }
  1641. int ast_bridge_kick(struct ast_bridge *bridge, struct ast_channel *chan)
  1642. {
  1643. struct ast_bridge_channel *bridge_channel;
  1644. int res;
  1645. ast_bridge_lock(bridge);
  1646. /* Try to find the channel that we want to kick. */
  1647. if (!(bridge_channel = bridge_find_channel(bridge, chan))) {
  1648. ast_bridge_unlock(bridge);
  1649. return -1;
  1650. }
  1651. res = ast_bridge_channel_queue_callback(bridge_channel, 0, kick_it, NULL, 0);
  1652. ast_bridge_unlock(bridge);
  1653. return res;
  1654. }
  1655. /*!
  1656. * \internal
  1657. * \brief Point the bridge_channel to a new bridge.
  1658. * \since 12.0.0
  1659. *
  1660. * \param bridge_channel What is to point to a new bridge.
  1661. * \param new_bridge Where the bridge channel should point.
  1662. *
  1663. * \return Nothing
  1664. */
  1665. static void bridge_channel_change_bridge(struct ast_bridge_channel *bridge_channel, struct ast_bridge *new_bridge)
  1666. {
  1667. struct ast_bridge *old_bridge;
  1668. ao2_ref(new_bridge, +1);
  1669. ast_bridge_channel_lock(bridge_channel);
  1670. ast_channel_lock(bridge_channel->chan);
  1671. old_bridge = bridge_channel->bridge;
  1672. bridge_channel->bridge = new_bridge;
  1673. ast_channel_internal_bridge_set(bridge_channel->chan, new_bridge);
  1674. ast_channel_unlock(bridge_channel->chan);
  1675. ast_bridge_channel_unlock(bridge_channel);
  1676. ao2_ref(old_bridge, -1);
  1677. }
  1678. static void bridge_channel_moving(struct ast_bridge_channel *bridge_channel, struct ast_bridge *src, struct ast_bridge *dst)
  1679. {
  1680. struct ast_bridge_features *features = bridge_channel->features;
  1681. struct ast_bridge_hook *hook;
  1682. struct ao2_iterator iter;
  1683. /* Run any moving hooks. */
  1684. iter = ao2_iterator_init(features->other_hooks, 0);
  1685. for (; (hook = ao2_iterator_next(&iter)); ao2_ref(hook, -1)) {
  1686. int remove_me;
  1687. ast_bridge_move_indicate_callback move_cb;
  1688. if (hook->type != AST_BRIDGE_HOOK_TYPE_MOVE) {
  1689. continue;
  1690. }
  1691. move_cb = (ast_bridge_move_indicate_callback) hook->callback;
  1692. remove_me = move_cb(bridge_channel, hook->hook_pvt, src, dst);
  1693. if (remove_me) {
  1694. ast_debug(1, "Move detection hook %p is being removed from %p(%s)\n",
  1695. hook, bridge_channel, ast_channel_name(bridge_channel->chan));
  1696. ao2_unlink(features->other_hooks, hook);
  1697. }
  1698. }
  1699. ao2_iterator_destroy(&iter);
  1700. }
  1701. void bridge_do_merge(struct ast_bridge *dst_bridge, struct ast_bridge *src_bridge, struct ast_bridge_channel **kick_me, unsigned int num_kick,
  1702. unsigned int optimized)
  1703. {
  1704. struct ast_bridge_channel *bridge_channel;
  1705. unsigned int idx;
  1706. ast_debug(1, "Merging bridge %s into bridge %s\n",
  1707. src_bridge->uniqueid, dst_bridge->uniqueid);
  1708. ast_bridge_publish_merge(dst_bridge, src_bridge);
  1709. /*
  1710. * Move channels from src_bridge over to dst_bridge.
  1711. *
  1712. * We must use AST_LIST_TRAVERSE_SAFE_BEGIN() because
  1713. * bridge_channel_internal_pull() alters the list we are traversing.
  1714. */
  1715. AST_LIST_TRAVERSE_SAFE_BEGIN(&src_bridge->channels, bridge_channel, entry) {
  1716. if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
  1717. /*
  1718. * The channel is already leaving let it leave normally because
  1719. * pulling it may delete hooks that should run for this channel.
  1720. */
  1721. continue;
  1722. }
  1723. if (ast_test_flag(&bridge_channel->features->feature_flags,
  1724. AST_BRIDGE_CHANNEL_FLAG_IMMOVABLE)) {
  1725. continue;
  1726. }
  1727. if (kick_me) {
  1728. for (idx = 0; idx < num_kick; ++idx) {
  1729. if (bridge_channel == kick_me[idx]) {
  1730. ast_bridge_channel_leave_bridge(bridge_channel,
  1731. BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, AST_CAUSE_NORMAL_CLEARING);
  1732. break;
  1733. }
  1734. }
  1735. }
  1736. bridge_channel_internal_pull(bridge_channel);
  1737. if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
  1738. /*
  1739. * The channel died as a result of being pulled or it was
  1740. * kicked. Leave it pointing to the original bridge.
  1741. */
  1742. continue;
  1743. }
  1744. bridge_channel_moving(bridge_channel, bridge_channel->bridge, dst_bridge);
  1745. /* Point to new bridge.*/
  1746. bridge_channel_change_bridge(bridge_channel, dst_bridge);
  1747. if (bridge_channel_internal_push(bridge_channel)) {
  1748. ast_bridge_features_remove(bridge_channel->features,
  1749. AST_BRIDGE_HOOK_REMOVE_ON_PULL);
  1750. ast_bridge_channel_leave_bridge(bridge_channel,
  1751. BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, bridge_channel->bridge->cause);
  1752. }
  1753. }
  1754. AST_LIST_TRAVERSE_SAFE_END;
  1755. if (kick_me) {
  1756. /*
  1757. * Now we can kick any channels in the dst_bridge without
  1758. * potentially dissolving the bridge.
  1759. */
  1760. for (idx = 0; idx < num_kick; ++idx) {
  1761. bridge_channel = kick_me[idx];
  1762. ast_bridge_channel_lock(bridge_channel);
  1763. if (bridge_channel->state == BRIDGE_CHANNEL_STATE_WAIT) {
  1764. ast_bridge_channel_leave_bridge_nolock(bridge_channel,
  1765. BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, AST_CAUSE_NORMAL_CLEARING);
  1766. bridge_channel_internal_pull(bridge_channel);
  1767. }
  1768. ast_bridge_channel_unlock(bridge_channel);
  1769. }
  1770. }
  1771. bridge_reconfigured(dst_bridge, !optimized);
  1772. bridge_reconfigured(src_bridge, !optimized);
  1773. ast_debug(1, "Merged bridge %s into bridge %s\n",
  1774. src_bridge->uniqueid, dst_bridge->uniqueid);
  1775. }
  1776. struct merge_direction {
  1777. /*! Destination merge bridge. */
  1778. struct ast_bridge *dest;
  1779. /*! Source merge bridge. */
  1780. struct ast_bridge *src;
  1781. };
  1782. /*!
  1783. * \internal
  1784. * \brief Determine which bridge should merge into the other.
  1785. * \since 12.0.0
  1786. *
  1787. * \param bridge1 A bridge for merging
  1788. * \param bridge2 A bridge for merging
  1789. *
  1790. * \note The two bridges are assumed already locked.
  1791. *
  1792. * \return Which bridge merges into which or NULL bridges if cannot merge.
  1793. */
  1794. static struct merge_direction bridge_merge_determine_direction(struct ast_bridge *bridge1, struct ast_bridge *bridge2)
  1795. {
  1796. struct merge_direction merge = { NULL, NULL };
  1797. int bridge1_priority;
  1798. int bridge2_priority;
  1799. if (!ast_test_flag(&bridge1->feature_flags,
  1800. AST_BRIDGE_FLAG_MERGE_INHIBIT_TO | AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM)
  1801. && !ast_test_flag(&bridge2->feature_flags,
  1802. AST_BRIDGE_FLAG_MERGE_INHIBIT_TO | AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM)) {
  1803. /*
  1804. * Can merge either way. Merge to the higher priority merge
  1805. * bridge. Otherwise merge to the larger bridge.
  1806. */
  1807. bridge1_priority = bridge1->v_table->get_merge_priority(bridge1);
  1808. bridge2_priority = bridge2->v_table->get_merge_priority(bridge2);
  1809. if (bridge2_priority < bridge1_priority) {
  1810. merge.dest = bridge1;
  1811. merge.src = bridge2;
  1812. } else if (bridge1_priority < bridge2_priority) {
  1813. merge.dest = bridge2;
  1814. merge.src = bridge1;
  1815. } else {
  1816. /* Merge to the larger bridge. */
  1817. if (bridge2->num_channels <= bridge1->num_channels) {
  1818. merge.dest = bridge1;
  1819. merge.src = bridge2;
  1820. } else {
  1821. merge.dest = bridge2;
  1822. merge.src = bridge1;
  1823. }
  1824. }
  1825. } else if (!ast_test_flag(&bridge1->feature_flags, AST_BRIDGE_FLAG_MERGE_INHIBIT_TO)
  1826. && !ast_test_flag(&bridge2->feature_flags, AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM)) {
  1827. /* Can merge only one way. */
  1828. merge.dest = bridge1;
  1829. merge.src = bridge2;
  1830. } else if (!ast_test_flag(&bridge2->feature_flags, AST_BRIDGE_FLAG_MERGE_INHIBIT_TO)
  1831. && !ast_test_flag(&bridge1->feature_flags, AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM)) {
  1832. /* Can merge only one way. */
  1833. merge.dest = bridge2;
  1834. merge.src = bridge1;
  1835. }
  1836. return merge;
  1837. }
  1838. /*!
  1839. * \internal
  1840. * \brief Merge two bridges together
  1841. * \since 12.0.0
  1842. *
  1843. * \param dst_bridge Destination bridge of merge.
  1844. * \param src_bridge Source bridge of merge.
  1845. * \param merge_best_direction TRUE if don't care about which bridge merges into the other.
  1846. * \param kick_me Array of channels to kick from the bridges.
  1847. * \param num_kick Number of channels in the kick_me array.
  1848. *
  1849. * \note The dst_bridge and src_bridge are assumed already locked.
  1850. *
  1851. * \retval 0 on success
  1852. * \retval -1 on failure
  1853. */
  1854. static int bridge_merge_locked(struct ast_bridge *dst_bridge, struct ast_bridge *src_bridge, int merge_best_direction, struct ast_channel **kick_me, unsigned int num_kick)
  1855. {
  1856. struct merge_direction merge;
  1857. struct ast_bridge_channel **kick_them = NULL;
  1858. /* Sanity check. */
  1859. ast_assert(dst_bridge && src_bridge && dst_bridge != src_bridge && (!num_kick || kick_me));
  1860. if (dst_bridge->dissolved || src_bridge->dissolved) {
  1861. ast_debug(1, "Can't merge bridges %s and %s, at least one bridge is dissolved.\n",
  1862. src_bridge->uniqueid, dst_bridge->uniqueid);
  1863. return -1;
  1864. }
  1865. if (ast_test_flag(&dst_bridge->feature_flags, AST_BRIDGE_FLAG_MASQUERADE_ONLY)
  1866. || ast_test_flag(&src_bridge->feature_flags, AST_BRIDGE_FLAG_MASQUERADE_ONLY)) {
  1867. ast_debug(1, "Can't merge bridges %s and %s, masquerade only.\n",
  1868. src_bridge->uniqueid, dst_bridge->uniqueid);
  1869. return -1;
  1870. }
  1871. if (dst_bridge->inhibit_merge || src_bridge->inhibit_merge) {
  1872. ast_debug(1, "Can't merge bridges %s and %s, merging temporarily inhibited.\n",
  1873. src_bridge->uniqueid, dst_bridge->uniqueid);
  1874. return -1;
  1875. }
  1876. if (merge_best_direction) {
  1877. merge = bridge_merge_determine_direction(dst_bridge, src_bridge);
  1878. } else {
  1879. merge.dest = dst_bridge;
  1880. merge.src = src_bridge;
  1881. }
  1882. if (!merge.dest
  1883. || ast_test_flag(&merge.dest->feature_flags, AST_BRIDGE_FLAG_MERGE_INHIBIT_TO)
  1884. || ast_test_flag(&merge.src->feature_flags, AST_BRIDGE_FLAG_MERGE_INHIBIT_FROM)) {
  1885. ast_debug(1, "Can't merge bridges %s and %s, merging inhibited.\n",
  1886. src_bridge->uniqueid, dst_bridge->uniqueid);
  1887. return -1;
  1888. }
  1889. if (merge.src->num_channels < 2) {
  1890. /*
  1891. * For a two party bridge, a channel may be temporarily removed
  1892. * from the source bridge or the initial bridge members have not
  1893. * joined yet.
  1894. */
  1895. ast_debug(1, "Can't merge bridge %s into bridge %s, not enough channels in source bridge.\n",
  1896. merge.src->uniqueid, merge.dest->uniqueid);
  1897. return -1;
  1898. }
  1899. if (2 + num_kick < merge.dest->num_channels + merge.src->num_channels
  1900. && !(merge.dest->technology->capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX)
  1901. && (!ast_test_flag(&merge.dest->feature_flags, AST_BRIDGE_FLAG_SMART)
  1902. || !(merge.dest->allowed_capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX))) {
  1903. ast_debug(1, "Can't merge bridge %s into bridge %s, multimix is needed and it cannot be acquired.\n",
  1904. merge.src->uniqueid, merge.dest->uniqueid);
  1905. return -1;
  1906. }
  1907. if (num_kick) {
  1908. unsigned int num_to_kick = 0;
  1909. unsigned int idx;
  1910. kick_them = ast_alloca(num_kick * sizeof(*kick_them));
  1911. for (idx = 0; idx < num_kick; ++idx) {
  1912. kick_them[num_to_kick] = bridge_find_channel(merge.src, kick_me[idx]);
  1913. if (!kick_them[num_to_kick]) {
  1914. kick_them[num_to_kick] = bridge_find_channel(merge.dest, kick_me[idx]);
  1915. }
  1916. if (kick_them[num_to_kick]) {
  1917. ++num_to_kick;
  1918. }
  1919. }
  1920. if (num_to_kick != num_kick) {
  1921. ast_debug(1, "Can't merge bridge %s into bridge %s, at least one kicked channel is not in either bridge.\n",
  1922. merge.src->uniqueid, merge.dest->uniqueid);
  1923. return -1;
  1924. }
  1925. }
  1926. bridge_do_merge(merge.dest, merge.src, kick_them, num_kick, 0);
  1927. return 0;
  1928. }
  1929. int ast_bridge_merge(struct ast_bridge *dst_bridge, struct ast_bridge *src_bridge, int merge_best_direction, struct ast_channel **kick_me, unsigned int num_kick)
  1930. {
  1931. int res;
  1932. /* Sanity check. */
  1933. ast_assert(dst_bridge && src_bridge);
  1934. ast_bridge_lock_both(dst_bridge, src_bridge);
  1935. res = bridge_merge_locked(dst_bridge, src_bridge, merge_best_direction, kick_me, num_kick);
  1936. ast_bridge_unlock(src_bridge);
  1937. ast_bridge_unlock(dst_bridge);
  1938. return res;
  1939. }
  1940. int bridge_do_move(struct ast_bridge *dst_bridge, struct ast_bridge_channel *bridge_channel, int attempt_recovery,
  1941. unsigned int optimized)
  1942. {
  1943. struct ast_bridge *orig_bridge;
  1944. int was_in_bridge;
  1945. int res = 0;
  1946. if (bridge_channel->swap) {
  1947. ast_debug(1, "Moving %p(%s) into bridge %s swapping with %s\n",
  1948. bridge_channel, ast_channel_name(bridge_channel->chan), dst_bridge->uniqueid,
  1949. ast_channel_name(bridge_channel->swap));
  1950. } else {
  1951. ast_debug(1, "Moving %p(%s) into bridge %s\n",
  1952. bridge_channel, ast_channel_name(bridge_channel->chan), dst_bridge->uniqueid);
  1953. }
  1954. orig_bridge = bridge_channel->bridge;
  1955. was_in_bridge = bridge_channel->in_bridge;
  1956. bridge_channel_internal_pull(bridge_channel);
  1957. if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
  1958. /*
  1959. * The channel died as a result of being pulled. Leave it
  1960. * pointing to the original bridge.
  1961. *
  1962. * Clear out the swap channel pointer. A ref is not held
  1963. * by bridge_channel->swap at this point.
  1964. */
  1965. bridge_channel->swap = NULL;
  1966. bridge_reconfigured(orig_bridge, 0);
  1967. return -1;
  1968. }
  1969. /* Point to new bridge.*/
  1970. ao2_ref(orig_bridge, +1);/* Keep a ref in case the push fails. */
  1971. bridge_channel_change_bridge(bridge_channel, dst_bridge);
  1972. bridge_channel_moving(bridge_channel, orig_bridge, dst_bridge);
  1973. if (bridge_channel_internal_push(bridge_channel)) {
  1974. /* Try to put the channel back into the original bridge. */
  1975. ast_bridge_features_remove(bridge_channel->features,
  1976. AST_BRIDGE_HOOK_REMOVE_ON_PULL);
  1977. if (attempt_recovery && was_in_bridge) {
  1978. /* Point back to original bridge. */
  1979. bridge_channel_change_bridge(bridge_channel, orig_bridge);
  1980. if (bridge_channel_internal_push(bridge_channel)) {
  1981. ast_bridge_features_remove(bridge_channel->features,
  1982. AST_BRIDGE_HOOK_REMOVE_ON_PULL);
  1983. ast_bridge_channel_leave_bridge(bridge_channel,
  1984. BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, bridge_channel->bridge->cause);
  1985. bridge_channel_settle_owed_events(orig_bridge, bridge_channel);
  1986. }
  1987. } else {
  1988. ast_bridge_channel_leave_bridge(bridge_channel,
  1989. BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, bridge_channel->bridge->cause);
  1990. bridge_channel_settle_owed_events(orig_bridge, bridge_channel);
  1991. }
  1992. res = -1;
  1993. } else {
  1994. bridge_channel_settle_owed_events(orig_bridge, bridge_channel);
  1995. }
  1996. bridge_reconfigured(dst_bridge, !optimized);
  1997. bridge_reconfigured(orig_bridge, !optimized);
  1998. ao2_ref(orig_bridge, -1);
  1999. return res;
  2000. }
  2001. /*!
  2002. * \internal
  2003. * \brief Move a channel from one bridge to another.
  2004. * \since 12.0.0
  2005. *
  2006. * \param dst_bridge Destination bridge of bridge channel move.
  2007. * \param src_bridge Source bridge of bridge channel move.
  2008. * \param chan Channel to move.
  2009. * \param swap Channel to replace in dst_bridge.
  2010. * \param attempt_recovery TRUE if failure attempts to push channel back into original bridge.
  2011. *
  2012. * \note The dst_bridge and src_bridge are assumed already locked.
  2013. *
  2014. * \retval 0 on success.
  2015. * \retval -1 on failure.
  2016. */
  2017. static int bridge_move_locked(struct ast_bridge *dst_bridge, struct ast_bridge *src_bridge, struct ast_channel *chan, struct ast_channel *swap, int attempt_recovery)
  2018. {
  2019. struct ast_bridge_channel *bridge_channel;
  2020. if (dst_bridge->dissolved || src_bridge->dissolved) {
  2021. ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, at least one bridge is dissolved.\n",
  2022. ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid);
  2023. return -1;
  2024. }
  2025. if (ast_test_flag(&dst_bridge->feature_flags, AST_BRIDGE_FLAG_MASQUERADE_ONLY)
  2026. || ast_test_flag(&src_bridge->feature_flags, AST_BRIDGE_FLAG_MASQUERADE_ONLY)) {
  2027. ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, masquerade only.\n",
  2028. ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid);
  2029. return -1;
  2030. }
  2031. if (dst_bridge->inhibit_merge || src_bridge->inhibit_merge) {
  2032. ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, temporarily inhibited.\n",
  2033. ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid);
  2034. return -1;
  2035. }
  2036. bridge_channel = bridge_find_channel(src_bridge, chan);
  2037. if (!bridge_channel) {
  2038. ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, channel not in bridge.\n",
  2039. ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid);
  2040. return -1;
  2041. }
  2042. if (bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT) {
  2043. ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, channel leaving bridge.\n",
  2044. ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid);
  2045. return -1;
  2046. }
  2047. if (ast_test_flag(&bridge_channel->features->feature_flags,
  2048. AST_BRIDGE_CHANNEL_FLAG_IMMOVABLE)) {
  2049. ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, channel immovable.\n",
  2050. ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid);
  2051. return -1;
  2052. }
  2053. if (swap) {
  2054. struct ast_bridge_channel *bridge_channel_swap;
  2055. bridge_channel_swap = bridge_find_channel(dst_bridge, swap);
  2056. if (!bridge_channel_swap) {
  2057. ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, swap channel %s not in bridge.\n",
  2058. ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid,
  2059. ast_channel_name(swap));
  2060. return -1;
  2061. }
  2062. if (bridge_channel_swap->state != BRIDGE_CHANNEL_STATE_WAIT) {
  2063. ast_debug(1, "Can't move channel %s from bridge %s into bridge %s, swap channel %s leaving bridge.\n",
  2064. ast_channel_name(chan), src_bridge->uniqueid, dst_bridge->uniqueid,
  2065. ast_channel_name(swap));
  2066. return -1;
  2067. }
  2068. }
  2069. bridge_channel->swap = swap;
  2070. return bridge_do_move(dst_bridge, bridge_channel, attempt_recovery, 0);
  2071. }
  2072. int ast_bridge_move(struct ast_bridge *dst_bridge, struct ast_bridge *src_bridge, struct ast_channel *chan, struct ast_channel *swap, int attempt_recovery)
  2073. {
  2074. int res;
  2075. ast_bridge_lock_both(dst_bridge, src_bridge);
  2076. res = bridge_move_locked(dst_bridge, src_bridge, chan, swap, attempt_recovery);
  2077. ast_bridge_unlock(src_bridge);
  2078. ast_bridge_unlock(dst_bridge);
  2079. return res;
  2080. }
  2081. int ast_bridge_add_channel(struct ast_bridge *bridge, struct ast_channel *chan,
  2082. struct ast_bridge_features *features, int play_tone, const char *xfersound)
  2083. {
  2084. RAII_VAR(struct ast_bridge *, chan_bridge, NULL, ao2_cleanup);
  2085. RAII_VAR(struct ast_channel *, yanked_chan, NULL, ao2_cleanup);
  2086. ast_channel_lock(chan);
  2087. chan_bridge = ast_channel_get_bridge(chan);
  2088. ast_channel_unlock(chan);
  2089. if (chan_bridge) {
  2090. struct ast_bridge_channel *bridge_channel;
  2091. ast_bridge_lock_both(bridge, chan_bridge);
  2092. bridge_channel = bridge_find_channel(chan_bridge, chan);
  2093. if (bridge_move_locked(bridge, chan_bridge, chan, NULL, 1)) {
  2094. ast_bridge_unlock(chan_bridge);
  2095. ast_bridge_unlock(bridge);
  2096. return -1;
  2097. }
  2098. /*
  2099. * bridge_move_locked() will implicitly ensure that
  2100. * bridge_channel is not NULL.
  2101. */
  2102. ast_assert(bridge_channel != NULL);
  2103. /*
  2104. * Additional checks if the channel we just stole dissolves the
  2105. * original bridge.
  2106. */
  2107. bridge_dissolve_check_stolen(chan_bridge, bridge_channel);
  2108. ast_bridge_unlock(chan_bridge);
  2109. ast_bridge_unlock(bridge);
  2110. /* The channel was in a bridge so it is not getting any new features. */
  2111. ast_bridge_features_destroy(features);
  2112. } else {
  2113. /* Slightly less easy case. We need to yank channel A from
  2114. * where he currently is and impart him into our bridge.
  2115. */
  2116. yanked_chan = ast_channel_yank(chan);
  2117. if (!yanked_chan) {
  2118. ast_log(LOG_WARNING, "Could not gain control of channel %s\n", ast_channel_name(chan));
  2119. return -1;
  2120. }
  2121. if (ast_channel_state(yanked_chan) != AST_STATE_UP) {
  2122. ast_answer(yanked_chan);
  2123. }
  2124. ast_channel_ref(yanked_chan);
  2125. if (ast_bridge_impart(bridge, yanked_chan, NULL, features,
  2126. AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
  2127. /* It is possible for us to yank a channel and have some other
  2128. * thread start a PBX on the channl after we yanked it. In particular,
  2129. * this can theoretically happen on the ;2 of a Local channel if we
  2130. * yank it prior to the ;1 being answered. Make sure that it isn't
  2131. * executing a PBX before hanging it up.
  2132. */
  2133. if (ast_channel_pbx(yanked_chan)) {
  2134. ast_channel_unref(yanked_chan);
  2135. } else {
  2136. ast_hangup(yanked_chan);
  2137. }
  2138. return -1;
  2139. }
  2140. }
  2141. if (play_tone && !ast_strlen_zero(xfersound)) {
  2142. struct ast_channel *play_chan = yanked_chan ?: chan;
  2143. RAII_VAR(struct ast_bridge_channel *, play_bridge_channel, NULL, ao2_cleanup);
  2144. ast_channel_lock(play_chan);
  2145. play_bridge_channel = ast_channel_get_bridge_channel(play_chan);
  2146. ast_channel_unlock(play_chan);
  2147. if (!play_bridge_channel) {
  2148. ast_log(LOG_WARNING, "Unable to play tone for channel %s. No longer in a bridge.\n",
  2149. ast_channel_name(play_chan));
  2150. } else {
  2151. ast_bridge_channel_queue_playfile(play_bridge_channel, NULL, xfersound, NULL);
  2152. }
  2153. }
  2154. return 0;
  2155. }
  2156. static int bridge_allows_optimization(struct ast_bridge *bridge)
  2157. {
  2158. return !(bridge->inhibit_merge
  2159. || bridge->dissolved
  2160. || ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_MASQUERADE_ONLY));
  2161. }
  2162. /*!
  2163. * \internal
  2164. * \brief Lock the unreal channel stack for chan and prequalify it.
  2165. * \since 12.0.0
  2166. *
  2167. * \param chan Unreal channel writing a frame into the channel driver.
  2168. *
  2169. * \note It is assumed that chan is already locked.
  2170. *
  2171. * \retval bridge on success with bridge and bridge_channel locked.
  2172. * \retval NULL if cannot do optimization now.
  2173. */
  2174. static struct ast_bridge *optimize_lock_chan_stack(struct ast_channel *chan)
  2175. {
  2176. struct ast_bridge *bridge;
  2177. struct ast_bridge_channel *bridge_channel;
  2178. if (!AST_LIST_EMPTY(ast_channel_readq(chan))) {
  2179. return NULL;
  2180. }
  2181. if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_EMULATE_DTMF)) {
  2182. return NULL;
  2183. }
  2184. if (ast_channel_has_audio_frame_or_monitor(chan)) {
  2185. /* Channel has an active monitor, audiohook, or framehook. */
  2186. return NULL;
  2187. }
  2188. bridge_channel = ast_channel_internal_bridge_channel(chan);
  2189. if (!bridge_channel || ast_bridge_channel_trylock(bridge_channel)) {
  2190. return NULL;
  2191. }
  2192. bridge = bridge_channel->bridge;
  2193. if (bridge_channel->activity != BRIDGE_CHANNEL_THREAD_SIMPLE
  2194. || bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT
  2195. || ast_bridge_trylock(bridge)) {
  2196. ast_bridge_channel_unlock(bridge_channel);
  2197. return NULL;
  2198. }
  2199. if (!bridge_channel_internal_allows_optimization(bridge_channel) ||
  2200. !bridge_allows_optimization(bridge)) {
  2201. ast_bridge_unlock(bridge);
  2202. ast_bridge_channel_unlock(bridge_channel);
  2203. return NULL;
  2204. }
  2205. return bridge;
  2206. }
  2207. /*!
  2208. * \internal
  2209. * \brief Lock the unreal channel stack for peer and prequalify it.
  2210. * \since 12.0.0
  2211. *
  2212. * \param peer Other unreal channel in the pair.
  2213. *
  2214. * \retval bridge on success with bridge, bridge_channel, and peer locked.
  2215. * \retval NULL if cannot do optimization now.
  2216. */
  2217. static struct ast_bridge *optimize_lock_peer_stack(struct ast_channel *peer)
  2218. {
  2219. struct ast_bridge *bridge;
  2220. struct ast_bridge_channel *bridge_channel;
  2221. if (ast_channel_trylock(peer)) {
  2222. return NULL;
  2223. }
  2224. if (!AST_LIST_EMPTY(ast_channel_readq(peer))) {
  2225. ast_channel_unlock(peer);
  2226. return NULL;
  2227. }
  2228. if (ast_test_flag(ast_channel_flags(peer), AST_FLAG_EMULATE_DTMF)) {
  2229. ast_channel_unlock(peer);
  2230. return NULL;
  2231. }
  2232. if (ast_channel_has_audio_frame_or_monitor(peer)) {
  2233. /* Peer has an active monitor, audiohook, or framehook. */
  2234. ast_channel_unlock(peer);
  2235. return NULL;
  2236. }
  2237. bridge_channel = ast_channel_internal_bridge_channel(peer);
  2238. if (!bridge_channel || ast_bridge_channel_trylock(bridge_channel)) {
  2239. ast_channel_unlock(peer);
  2240. return NULL;
  2241. }
  2242. bridge = bridge_channel->bridge;
  2243. if (bridge_channel->activity != BRIDGE_CHANNEL_THREAD_IDLE
  2244. || bridge_channel->state != BRIDGE_CHANNEL_STATE_WAIT
  2245. || ast_bridge_trylock(bridge)) {
  2246. ast_bridge_channel_unlock(bridge_channel);
  2247. ast_channel_unlock(peer);
  2248. return NULL;
  2249. }
  2250. if (!bridge_allows_optimization(bridge) ||
  2251. !bridge_channel_internal_allows_optimization(bridge_channel)) {
  2252. ast_bridge_unlock(bridge);
  2253. ast_bridge_channel_unlock(bridge_channel);
  2254. ast_channel_unlock(peer);
  2255. return NULL;
  2256. }
  2257. return bridge;
  2258. }
  2259. /*!
  2260. * \internal
  2261. * \brief Indicates allowability of a swap optimization
  2262. */
  2263. enum bridge_allow_swap {
  2264. /*! Bridges cannot allow for a swap optimization to occur */
  2265. SWAP_PROHIBITED,
  2266. /*! Bridge swap optimization can occur into the chan_bridge */
  2267. SWAP_TO_CHAN_BRIDGE,
  2268. /*! Bridge swap optimization can occur into the peer_bridge */
  2269. SWAP_TO_PEER_BRIDGE,
  2270. };
  2271. /*!
  2272. * \internal
  2273. * \brief Determine if two bridges allow for swap optimization to occur
  2274. *
  2275. * \param chan_bridge First bridge being tested
  2276. * \param peer_bridge Second bridge being tested
  2277. * \return Allowability of swap optimization
  2278. */
  2279. static enum bridge_allow_swap bridges_allow_swap_optimization(struct ast_bridge *chan_bridge,
  2280. struct ast_bridge *peer_bridge)
  2281. {
  2282. int chan_priority;
  2283. int peer_priority;
  2284. if (!ast_test_flag(&chan_bridge->feature_flags,
  2285. AST_BRIDGE_FLAG_SWAP_INHIBIT_TO | AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM |
  2286. AST_BRIDGE_FLAG_TRANSFER_BRIDGE_ONLY)
  2287. && !ast_test_flag(&peer_bridge->feature_flags,
  2288. AST_BRIDGE_FLAG_SWAP_INHIBIT_TO | AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM |
  2289. AST_BRIDGE_FLAG_TRANSFER_BRIDGE_ONLY)) {
  2290. /*
  2291. * Can swap either way. Swap to the higher priority merge
  2292. * bridge.
  2293. */
  2294. chan_priority = chan_bridge->v_table->get_merge_priority(chan_bridge);
  2295. peer_priority = peer_bridge->v_table->get_merge_priority(peer_bridge);
  2296. if (chan_bridge->num_channels == 2
  2297. && chan_priority <= peer_priority) {
  2298. return SWAP_TO_PEER_BRIDGE;
  2299. } else if (peer_bridge->num_channels == 2
  2300. && peer_priority <= chan_priority) {
  2301. return SWAP_TO_CHAN_BRIDGE;
  2302. }
  2303. } else if (chan_bridge->num_channels == 2
  2304. && !ast_test_flag(&chan_bridge->feature_flags, AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM | AST_BRIDGE_FLAG_TRANSFER_BRIDGE_ONLY)
  2305. && !ast_test_flag(&peer_bridge->feature_flags, AST_BRIDGE_FLAG_SWAP_INHIBIT_TO)) {
  2306. /* Can swap optimize only one way. */
  2307. return SWAP_TO_PEER_BRIDGE;
  2308. } else if (peer_bridge->num_channels == 2
  2309. && !ast_test_flag(&peer_bridge->feature_flags, AST_BRIDGE_FLAG_SWAP_INHIBIT_FROM | AST_BRIDGE_FLAG_TRANSFER_BRIDGE_ONLY)
  2310. && !ast_test_flag(&chan_bridge->feature_flags, AST_BRIDGE_FLAG_SWAP_INHIBIT_TO)) {
  2311. /* Can swap optimize only one way. */
  2312. return SWAP_TO_CHAN_BRIDGE;
  2313. }
  2314. return SWAP_PROHIBITED;
  2315. }
  2316. /*!
  2317. * \internal
  2318. * \brief Check and attempt to swap optimize out the unreal channels.
  2319. * \since 12.0.0
  2320. *
  2321. * \param chan_bridge
  2322. * \param chan_bridge_channel
  2323. * \param peer_bridge
  2324. * \param peer_bridge_channel
  2325. * \param pvt Unreal data containing callbacks to call if the optimization actually
  2326. * happens
  2327. *
  2328. * \retval 1 if unreal channels failed to optimize out.
  2329. * \retval 0 if unreal channels were not optimized out.
  2330. * \retval -1 if unreal channels were optimized out.
  2331. */
  2332. static int try_swap_optimize_out(struct ast_bridge *chan_bridge,
  2333. struct ast_bridge_channel *chan_bridge_channel, struct ast_bridge *peer_bridge,
  2334. struct ast_bridge_channel *peer_bridge_channel,
  2335. struct ast_unreal_pvt *pvt)
  2336. {
  2337. struct ast_bridge *dst_bridge;
  2338. struct ast_bridge_channel *dst_bridge_channel;
  2339. struct ast_bridge_channel *src_bridge_channel;
  2340. struct ast_bridge_channel *other;
  2341. int res = 1;
  2342. switch (bridges_allow_swap_optimization(chan_bridge, peer_bridge)) {
  2343. case SWAP_TO_CHAN_BRIDGE:
  2344. dst_bridge = chan_bridge;
  2345. dst_bridge_channel = chan_bridge_channel;
  2346. src_bridge_channel = peer_bridge_channel;
  2347. break;
  2348. case SWAP_TO_PEER_BRIDGE:
  2349. dst_bridge = peer_bridge;
  2350. dst_bridge_channel = peer_bridge_channel;
  2351. src_bridge_channel = chan_bridge_channel;
  2352. break;
  2353. case SWAP_PROHIBITED:
  2354. default:
  2355. return 0;
  2356. }
  2357. other = ast_bridge_channel_peer(src_bridge_channel);
  2358. if (other && other->state == BRIDGE_CHANNEL_STATE_WAIT) {
  2359. unsigned int id;
  2360. if (ast_channel_trylock(other->chan)) {
  2361. return 1;
  2362. }
  2363. id = ast_atomic_fetchadd_int((int *) &optimization_id, +1);
  2364. ast_verb(4, "Move-swap optimizing %s <-- %s.\n",
  2365. ast_channel_name(dst_bridge_channel->chan),
  2366. ast_channel_name(other->chan));
  2367. if (pvt && !ast_test_flag(pvt, AST_UNREAL_OPTIMIZE_BEGUN) && pvt->callbacks
  2368. && pvt->callbacks->optimization_started) {
  2369. pvt->callbacks->optimization_started(pvt, other->chan,
  2370. dst_bridge_channel->chan == pvt->owner ? AST_UNREAL_OWNER : AST_UNREAL_CHAN,
  2371. id);
  2372. ast_set_flag(pvt, AST_UNREAL_OPTIMIZE_BEGUN);
  2373. }
  2374. other->swap = dst_bridge_channel->chan;
  2375. if (!bridge_do_move(dst_bridge, other, 1, 1)) {
  2376. ast_bridge_channel_leave_bridge(src_bridge_channel,
  2377. BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, AST_CAUSE_NORMAL_CLEARING);
  2378. res = -1;
  2379. }
  2380. if (pvt && pvt->callbacks && pvt->callbacks->optimization_finished) {
  2381. pvt->callbacks->optimization_finished(pvt, res == 1, id);
  2382. }
  2383. ast_channel_unlock(other->chan);
  2384. }
  2385. return res;
  2386. }
  2387. /*!
  2388. * \internal
  2389. * \brief Indicates allowability of a merge optimization
  2390. */
  2391. enum bridge_allow_merge {
  2392. /*! Bridge properties prohibit merge optimization */
  2393. MERGE_PROHIBITED,
  2394. /*! Merge optimization cannot occur because the source bridge has too few channels */
  2395. MERGE_NOT_ENOUGH_CHANNELS,
  2396. /*! Merge optimization cannot occur because multimix capability could not be requested */
  2397. MERGE_NO_MULTIMIX,
  2398. /*! Merge optimization allowed between bridges */
  2399. MERGE_ALLOWED,
  2400. };
  2401. /*!
  2402. * \internal
  2403. * \brief Determines allowability of a merge optimization
  2404. *
  2405. * \note The merge output parameter is undefined if MERGE_PROHIBITED is returned. For success
  2406. * and other failure returns, a merge direction was determined, and the parameter is safe to
  2407. * access.
  2408. *
  2409. * \param chan_bridge First bridge being tested
  2410. * \param peer_bridge Second bridge being tested
  2411. * \param num_kick_channels The number of channels to remove from the bridges during merging
  2412. * \param[out] merge Indicates the recommended direction for the bridge merge
  2413. */
  2414. static enum bridge_allow_merge bridges_allow_merge_optimization(struct ast_bridge *chan_bridge,
  2415. struct ast_bridge *peer_bridge, int num_kick_channels, struct merge_direction *merge)
  2416. {
  2417. *merge = bridge_merge_determine_direction(chan_bridge, peer_bridge);
  2418. if (!merge->dest) {
  2419. return MERGE_PROHIBITED;
  2420. }
  2421. if (merge->src->num_channels < 2) {
  2422. return MERGE_NOT_ENOUGH_CHANNELS;
  2423. } else if ((2 + num_kick_channels) < merge->dest->num_channels + merge->src->num_channels
  2424. && !(merge->dest->technology->capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX)
  2425. && (!ast_test_flag(&merge->dest->feature_flags, AST_BRIDGE_FLAG_SMART)
  2426. || !(merge->dest->allowed_capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX))) {
  2427. return MERGE_NO_MULTIMIX;
  2428. }
  2429. return MERGE_ALLOWED;
  2430. }
  2431. /*!
  2432. * \internal
  2433. * \brief Check and attempt to merge optimize out the unreal channels.
  2434. * \since 12.0.0
  2435. *
  2436. * \param chan_bridge
  2437. * \param chan_bridge_channel
  2438. * \param peer_bridge
  2439. * \param peer_bridge_channel
  2440. * \param pvt Unreal data containing callbacks to call if the optimization actually
  2441. * happens
  2442. *
  2443. * \retval 0 if unreal channels were not optimized out.
  2444. * \retval -1 if unreal channels were optimized out.
  2445. */
  2446. static int try_merge_optimize_out(struct ast_bridge *chan_bridge,
  2447. struct ast_bridge_channel *chan_bridge_channel, struct ast_bridge *peer_bridge,
  2448. struct ast_bridge_channel *peer_bridge_channel,
  2449. struct ast_unreal_pvt *pvt)
  2450. {
  2451. struct merge_direction merge;
  2452. struct ast_bridge_channel *kick_me[] = {
  2453. chan_bridge_channel,
  2454. peer_bridge_channel,
  2455. };
  2456. unsigned int id;
  2457. switch (bridges_allow_merge_optimization(chan_bridge, peer_bridge, ARRAY_LEN(kick_me), &merge)) {
  2458. case MERGE_ALLOWED:
  2459. break;
  2460. case MERGE_PROHIBITED:
  2461. return 0;
  2462. case MERGE_NOT_ENOUGH_CHANNELS:
  2463. ast_debug(4, "Can't optimize %s -- %s out, not enough channels in bridge %s.\n",
  2464. ast_channel_name(chan_bridge_channel->chan),
  2465. ast_channel_name(peer_bridge_channel->chan),
  2466. merge.src->uniqueid);
  2467. return 0;
  2468. case MERGE_NO_MULTIMIX:
  2469. ast_debug(4, "Can't optimize %s -- %s out, multimix is needed and it cannot be acquired.\n",
  2470. ast_channel_name(chan_bridge_channel->chan),
  2471. ast_channel_name(peer_bridge_channel->chan));
  2472. return 0;
  2473. }
  2474. ast_verb(4, "Merge optimizing %s -- %s out.\n",
  2475. ast_channel_name(chan_bridge_channel->chan),
  2476. ast_channel_name(peer_bridge_channel->chan));
  2477. id = ast_atomic_fetchadd_int((int *) &optimization_id, +1);
  2478. if (pvt && !ast_test_flag(pvt, AST_UNREAL_OPTIMIZE_BEGUN) && pvt->callbacks
  2479. && pvt->callbacks->optimization_started) {
  2480. pvt->callbacks->optimization_started(pvt, NULL,
  2481. merge.dest == ast_channel_internal_bridge(pvt->owner) ? AST_UNREAL_OWNER : AST_UNREAL_CHAN,
  2482. id);
  2483. ast_set_flag(pvt, AST_UNREAL_OPTIMIZE_BEGUN);
  2484. }
  2485. bridge_do_merge(merge.dest, merge.src, kick_me, ARRAY_LEN(kick_me), 1);
  2486. if (pvt && pvt->callbacks && pvt->callbacks->optimization_finished) {
  2487. pvt->callbacks->optimization_finished(pvt, 1, id);
  2488. }
  2489. return -1;
  2490. }
  2491. int ast_bridge_unreal_optimize_out(struct ast_channel *chan, struct ast_channel *peer, struct ast_unreal_pvt *pvt)
  2492. {
  2493. struct ast_bridge *chan_bridge;
  2494. struct ast_bridge *peer_bridge;
  2495. struct ast_bridge_channel *chan_bridge_channel;
  2496. struct ast_bridge_channel *peer_bridge_channel;
  2497. int res = 0;
  2498. chan_bridge = optimize_lock_chan_stack(chan);
  2499. if (!chan_bridge) {
  2500. return res;
  2501. }
  2502. chan_bridge_channel = ast_channel_internal_bridge_channel(chan);
  2503. peer_bridge = optimize_lock_peer_stack(peer);
  2504. if (peer_bridge) {
  2505. peer_bridge_channel = ast_channel_internal_bridge_channel(peer);
  2506. res = try_swap_optimize_out(chan_bridge, chan_bridge_channel,
  2507. peer_bridge, peer_bridge_channel, pvt);
  2508. if (!res) {
  2509. res = try_merge_optimize_out(chan_bridge, chan_bridge_channel,
  2510. peer_bridge, peer_bridge_channel, pvt);
  2511. } else if (0 < res) {
  2512. res = 0;
  2513. }
  2514. /* Release peer locks. */
  2515. ast_bridge_unlock(peer_bridge);
  2516. ast_bridge_channel_unlock(peer_bridge_channel);
  2517. ast_channel_unlock(peer);
  2518. }
  2519. /* Release chan locks. */
  2520. ast_bridge_unlock(chan_bridge);
  2521. ast_bridge_channel_unlock(chan_bridge_channel);
  2522. return res;
  2523. }
  2524. enum ast_bridge_optimization ast_bridges_allow_optimization(struct ast_bridge *chan_bridge,
  2525. struct ast_bridge *peer_bridge)
  2526. {
  2527. struct merge_direction merge;
  2528. if (!bridge_allows_optimization(chan_bridge) || !bridge_allows_optimization(peer_bridge)) {
  2529. return AST_BRIDGE_OPTIMIZE_PROHIBITED;
  2530. }
  2531. switch (bridges_allow_swap_optimization(chan_bridge, peer_bridge)) {
  2532. case SWAP_TO_CHAN_BRIDGE:
  2533. return AST_BRIDGE_OPTIMIZE_SWAP_TO_CHAN_BRIDGE;
  2534. case SWAP_TO_PEER_BRIDGE:
  2535. return AST_BRIDGE_OPTIMIZE_SWAP_TO_PEER_BRIDGE;
  2536. case SWAP_PROHIBITED:
  2537. default:
  2538. break;
  2539. }
  2540. /* Two channels will be kicked from the bridges, the unreal;1 and unreal;2 channels */
  2541. if (bridges_allow_merge_optimization(chan_bridge, peer_bridge, 2, &merge) != MERGE_ALLOWED) {
  2542. return AST_BRIDGE_OPTIMIZE_PROHIBITED;
  2543. }
  2544. if (merge.dest == chan_bridge) {
  2545. return AST_BRIDGE_OPTIMIZE_MERGE_TO_CHAN_BRIDGE;
  2546. } else {
  2547. return AST_BRIDGE_OPTIMIZE_MERGE_TO_PEER_BRIDGE;
  2548. }
  2549. }
  2550. /*!
  2551. * \internal
  2552. * \brief Adjust the bridge merge inhibit request count.
  2553. * \since 12.0.0
  2554. *
  2555. * \param bridge What to operate on.
  2556. * \param request Inhibit request increment.
  2557. * (Positive to add requests. Negative to remove requests.)
  2558. *
  2559. * \note This function assumes bridge is locked.
  2560. *
  2561. * \return Nothing
  2562. */
  2563. void bridge_merge_inhibit_nolock(struct ast_bridge *bridge, int request)
  2564. {
  2565. int new_request;
  2566. new_request = bridge->inhibit_merge + request;
  2567. ast_assert(0 <= new_request);
  2568. bridge->inhibit_merge = new_request;
  2569. }
  2570. void ast_bridge_merge_inhibit(struct ast_bridge *bridge, int request)
  2571. {
  2572. ast_bridge_lock(bridge);
  2573. bridge_merge_inhibit_nolock(bridge, request);
  2574. ast_bridge_unlock(bridge);
  2575. }
  2576. int ast_bridge_suspend(struct ast_bridge *bridge, struct ast_channel *chan)
  2577. {
  2578. struct ast_bridge_channel *bridge_channel;
  2579. /* XXX ASTERISK-21271 the case of a disolved bridge while channel is suspended is not handled. */
  2580. /* XXX ASTERISK-21271 suspend/unsuspend needs to be rethought. The caller must block until it has successfully suspended the channel for temporary control. */
  2581. /* XXX ASTERISK-21271 external suspend/unsuspend needs to be eliminated. The channel may be playing a file at the time and stealing it then is not good. */
  2582. ast_bridge_lock(bridge);
  2583. if (!(bridge_channel = bridge_find_channel(bridge, chan))) {
  2584. ast_bridge_unlock(bridge);
  2585. return -1;
  2586. }
  2587. bridge_channel_internal_suspend_nolock(bridge_channel);
  2588. ast_bridge_unlock(bridge);
  2589. return 0;
  2590. }
  2591. int ast_bridge_unsuspend(struct ast_bridge *bridge, struct ast_channel *chan)
  2592. {
  2593. struct ast_bridge_channel *bridge_channel;
  2594. /* XXX ASTERISK-21271 the case of a disolved bridge while channel is suspended is not handled. */
  2595. ast_bridge_lock(bridge);
  2596. if (!(bridge_channel = bridge_find_channel(bridge, chan))) {
  2597. ast_bridge_unlock(bridge);
  2598. return -1;
  2599. }
  2600. bridge_channel_internal_unsuspend_nolock(bridge_channel);
  2601. ast_bridge_unlock(bridge);
  2602. return 0;
  2603. }
  2604. void ast_bridge_technology_suspend(struct ast_bridge_technology *technology)
  2605. {
  2606. technology->suspended = 1;
  2607. }
  2608. void ast_bridge_technology_unsuspend(struct ast_bridge_technology *technology)
  2609. {
  2610. /*
  2611. * XXX We may want the act of unsuspending a bridge technology
  2612. * to prod all existing bridges to see if they should start
  2613. * using it.
  2614. */
  2615. technology->suspended = 0;
  2616. }
  2617. int ast_bridge_features_register(enum ast_bridge_builtin_feature feature, ast_bridge_hook_callback callback, const char *dtmf)
  2618. {
  2619. if (ARRAY_LEN(builtin_features_handlers) <= feature
  2620. || builtin_features_handlers[feature]) {
  2621. return -1;
  2622. }
  2623. if (!ast_strlen_zero(dtmf)) {
  2624. ast_copy_string(builtin_features_dtmf[feature], dtmf, sizeof(builtin_features_dtmf[feature]));
  2625. }
  2626. builtin_features_handlers[feature] = callback;
  2627. return 0;
  2628. }
  2629. int ast_bridge_features_unregister(enum ast_bridge_builtin_feature feature)
  2630. {
  2631. if (ARRAY_LEN(builtin_features_handlers) <= feature
  2632. || !builtin_features_handlers[feature]) {
  2633. return -1;
  2634. }
  2635. builtin_features_handlers[feature] = NULL;
  2636. return 0;
  2637. }
  2638. int ast_bridge_features_do(enum ast_bridge_builtin_feature feature, struct ast_bridge_channel *bridge_channel, void *hook_pvt)
  2639. {
  2640. ast_bridge_hook_callback callback;
  2641. if (ARRAY_LEN(builtin_features_handlers) <= feature) {
  2642. return -1;
  2643. }
  2644. callback = builtin_features_handlers[feature];
  2645. if (!callback) {
  2646. return -1;
  2647. }
  2648. callback(bridge_channel, hook_pvt);
  2649. return 0;
  2650. }
  2651. int ast_bridge_interval_register(enum ast_bridge_builtin_interval interval, ast_bridge_builtin_set_limits_fn callback)
  2652. {
  2653. if (ARRAY_LEN(builtin_interval_handlers) <= interval
  2654. || builtin_interval_handlers[interval]) {
  2655. return -1;
  2656. }
  2657. builtin_interval_handlers[interval] = callback;
  2658. return 0;
  2659. }
  2660. int ast_bridge_interval_unregister(enum ast_bridge_builtin_interval interval)
  2661. {
  2662. if (ARRAY_LEN(builtin_interval_handlers) <= interval
  2663. || !builtin_interval_handlers[interval]) {
  2664. return -1;
  2665. }
  2666. builtin_interval_handlers[interval] = NULL;
  2667. return 0;
  2668. }
  2669. /*!
  2670. * \internal
  2671. * \brief Bridge hook destructor.
  2672. * \since 12.0.0
  2673. *
  2674. * \param vhook Object to destroy.
  2675. *
  2676. * \return Nothing
  2677. */
  2678. static void bridge_hook_destroy(void *vhook)
  2679. {
  2680. struct ast_bridge_hook *hook = vhook;
  2681. if (hook->destructor) {
  2682. hook->destructor(hook->hook_pvt);
  2683. }
  2684. }
  2685. /*!
  2686. * \internal
  2687. * \brief Allocate and setup a generic bridge hook.
  2688. * \since 12.0.0
  2689. *
  2690. * \param size How big an object to allocate.
  2691. * \param callback Function to execute upon activation
  2692. * \param hook_pvt Unique data
  2693. * \param destructor Optional destructor callback for hook_pvt data
  2694. * \param remove_flags Dictates what situations the hook should be removed.
  2695. *
  2696. * \retval hook on success.
  2697. * \retval NULL on error.
  2698. */
  2699. static struct ast_bridge_hook *bridge_hook_generic(size_t size,
  2700. ast_bridge_hook_callback callback,
  2701. void *hook_pvt,
  2702. ast_bridge_hook_pvt_destructor destructor,
  2703. enum ast_bridge_hook_remove_flags remove_flags)
  2704. {
  2705. struct ast_bridge_hook *hook;
  2706. /* Allocate new hook and setup it's basic variables */
  2707. hook = ao2_alloc_options(size, bridge_hook_destroy, AO2_ALLOC_OPT_LOCK_NOLOCK);
  2708. if (hook) {
  2709. hook->callback = callback;
  2710. hook->destructor = destructor;
  2711. hook->hook_pvt = hook_pvt;
  2712. ast_set_flag(&hook->remove_flags, remove_flags);
  2713. }
  2714. return hook;
  2715. }
  2716. int ast_bridge_dtmf_hook(struct ast_bridge_features *features,
  2717. const char *dtmf,
  2718. ast_bridge_hook_callback callback,
  2719. void *hook_pvt,
  2720. ast_bridge_hook_pvt_destructor destructor,
  2721. enum ast_bridge_hook_remove_flags remove_flags)
  2722. {
  2723. struct ast_bridge_hook_dtmf *hook;
  2724. int res;
  2725. /* Allocate new hook and setup it's various variables */
  2726. hook = (struct ast_bridge_hook_dtmf *) bridge_hook_generic(sizeof(*hook), callback,
  2727. hook_pvt, destructor, remove_flags);
  2728. if (!hook) {
  2729. return -1;
  2730. }
  2731. hook->generic.type = AST_BRIDGE_HOOK_TYPE_DTMF;
  2732. ast_copy_string(hook->dtmf.code, dtmf, sizeof(hook->dtmf.code));
  2733. /* Once done we put it in the container. */
  2734. res = ao2_link(features->dtmf_hooks, hook) ? 0 : -1;
  2735. if (res) {
  2736. /*
  2737. * Could not link the hook into the container.
  2738. *
  2739. * Remove the hook_pvt destructor call from the hook since we
  2740. * are returning failure to install the hook.
  2741. */
  2742. hook->generic.destructor = NULL;
  2743. }
  2744. ao2_ref(hook, -1);
  2745. return res;
  2746. }
  2747. /*!
  2748. * \internal
  2749. * \brief Attach an other hook to a bridge features structure
  2750. *
  2751. * \param features Bridge features structure
  2752. * \param callback Function to execute upon activation
  2753. * \param hook_pvt Unique data
  2754. * \param destructor Optional destructor callback for hook_pvt data
  2755. * \param remove_flags Dictates what situations the hook should be removed.
  2756. * \param type What type of hook is being attached.
  2757. *
  2758. * \retval 0 on success
  2759. * \retval -1 on failure (The caller must cleanup any hook_pvt resources.)
  2760. */
  2761. static int bridge_other_hook(struct ast_bridge_features *features,
  2762. ast_bridge_hook_callback callback,
  2763. void *hook_pvt,
  2764. ast_bridge_hook_pvt_destructor destructor,
  2765. enum ast_bridge_hook_remove_flags remove_flags,
  2766. enum ast_bridge_hook_type type)
  2767. {
  2768. struct ast_bridge_hook *hook;
  2769. int res;
  2770. /* Allocate new hook and setup it's various variables */
  2771. hook = bridge_hook_generic(sizeof(*hook), callback, hook_pvt, destructor,
  2772. remove_flags);
  2773. if (!hook) {
  2774. return -1;
  2775. }
  2776. hook->type = type;
  2777. /* Once done we put it in the container. */
  2778. res = ao2_link(features->other_hooks, hook) ? 0 : -1;
  2779. if (res) {
  2780. /*
  2781. * Could not link the hook into the container.
  2782. *
  2783. * Remove the hook_pvt destructor call from the hook since we
  2784. * are returning failure to install the hook.
  2785. */
  2786. hook->destructor = NULL;
  2787. }
  2788. ao2_ref(hook, -1);
  2789. return res;
  2790. }
  2791. int ast_bridge_hangup_hook(struct ast_bridge_features *features,
  2792. ast_bridge_hook_callback callback,
  2793. void *hook_pvt,
  2794. ast_bridge_hook_pvt_destructor destructor,
  2795. enum ast_bridge_hook_remove_flags remove_flags)
  2796. {
  2797. return bridge_other_hook(features, callback, hook_pvt, destructor, remove_flags,
  2798. AST_BRIDGE_HOOK_TYPE_HANGUP);
  2799. }
  2800. int ast_bridge_join_hook(struct ast_bridge_features *features,
  2801. ast_bridge_hook_callback callback,
  2802. void *hook_pvt,
  2803. ast_bridge_hook_pvt_destructor destructor,
  2804. enum ast_bridge_hook_remove_flags remove_flags)
  2805. {
  2806. return bridge_other_hook(features, callback, hook_pvt, destructor, remove_flags,
  2807. AST_BRIDGE_HOOK_TYPE_JOIN);
  2808. }
  2809. int ast_bridge_leave_hook(struct ast_bridge_features *features,
  2810. ast_bridge_hook_callback callback,
  2811. void *hook_pvt,
  2812. ast_bridge_hook_pvt_destructor destructor,
  2813. enum ast_bridge_hook_remove_flags remove_flags)
  2814. {
  2815. return bridge_other_hook(features, callback, hook_pvt, destructor, remove_flags,
  2816. AST_BRIDGE_HOOK_TYPE_LEAVE);
  2817. }
  2818. int ast_bridge_talk_detector_hook(struct ast_bridge_features *features,
  2819. ast_bridge_talking_indicate_callback callback,
  2820. void *hook_pvt,
  2821. ast_bridge_hook_pvt_destructor destructor,
  2822. enum ast_bridge_hook_remove_flags remove_flags)
  2823. {
  2824. ast_bridge_hook_callback hook_cb = (ast_bridge_hook_callback) callback;
  2825. return bridge_other_hook(features, hook_cb, hook_pvt, destructor, remove_flags,
  2826. AST_BRIDGE_HOOK_TYPE_TALK);
  2827. }
  2828. int ast_bridge_move_hook(struct ast_bridge_features *features,
  2829. ast_bridge_move_indicate_callback callback,
  2830. void *hook_pvt,
  2831. ast_bridge_hook_pvt_destructor destructor,
  2832. enum ast_bridge_hook_remove_flags remove_flags)
  2833. {
  2834. ast_bridge_hook_callback hook_cb = (ast_bridge_hook_callback) callback;
  2835. return bridge_other_hook(features, hook_cb, hook_pvt, destructor, remove_flags,
  2836. AST_BRIDGE_HOOK_TYPE_MOVE);
  2837. }
  2838. int ast_bridge_interval_hook(struct ast_bridge_features *features,
  2839. enum ast_bridge_hook_timer_option flags,
  2840. unsigned int interval,
  2841. ast_bridge_hook_callback callback,
  2842. void *hook_pvt,
  2843. ast_bridge_hook_pvt_destructor destructor,
  2844. enum ast_bridge_hook_remove_flags remove_flags)
  2845. {
  2846. struct ast_bridge_hook_timer *hook;
  2847. int res;
  2848. if (!features ||!interval || !callback) {
  2849. return -1;
  2850. }
  2851. /* Allocate new hook and setup it's various variables */
  2852. hook = (struct ast_bridge_hook_timer *) bridge_hook_generic(sizeof(*hook), callback,
  2853. hook_pvt, destructor, remove_flags);
  2854. if (!hook) {
  2855. return -1;
  2856. }
  2857. hook->generic.type = AST_BRIDGE_HOOK_TYPE_TIMER;
  2858. hook->timer.interval = interval;
  2859. hook->timer.trip_time = ast_tvadd(ast_tvnow(), ast_samp2tv(interval, 1000));
  2860. hook->timer.seqno = ast_atomic_fetchadd_int((int *) &features->interval_sequence, +1);
  2861. hook->timer.flags = flags;
  2862. ast_debug(1, "Putting interval hook %p with interval %u in the heap on features %p\n",
  2863. hook, hook->timer.interval, features);
  2864. ast_heap_wrlock(features->interval_hooks);
  2865. res = ast_heap_push(features->interval_hooks, hook);
  2866. ast_heap_unlock(features->interval_hooks);
  2867. if (res) {
  2868. /*
  2869. * Could not push the hook into the heap
  2870. *
  2871. * Remove the hook_pvt destructor call from the hook since we
  2872. * are returning failure to install the hook.
  2873. */
  2874. hook->generic.destructor = NULL;
  2875. ao2_ref(hook, -1);
  2876. }
  2877. return res ? -1 : 0;
  2878. }
  2879. int ast_bridge_features_enable(struct ast_bridge_features *features,
  2880. enum ast_bridge_builtin_feature feature,
  2881. const char *dtmf,
  2882. void *config,
  2883. ast_bridge_hook_pvt_destructor destructor,
  2884. enum ast_bridge_hook_remove_flags remove_flags)
  2885. {
  2886. if (ARRAY_LEN(builtin_features_handlers) <= feature
  2887. || !builtin_features_handlers[feature]) {
  2888. return -1;
  2889. }
  2890. /* If no alternate DTMF stream was provided use the default one */
  2891. if (ast_strlen_zero(dtmf)) {
  2892. dtmf = builtin_features_dtmf[feature];
  2893. /* If no DTMF is still available (ie: it has been disabled) then error out now */
  2894. if (ast_strlen_zero(dtmf)) {
  2895. ast_debug(1, "Failed to enable built in feature %u on %p, no DTMF string is available for it.\n",
  2896. feature, features);
  2897. return -1;
  2898. }
  2899. }
  2900. /*
  2901. * The rest is basically pretty easy. We create another hook
  2902. * using the built in feature's DTMF callback. Easy as pie.
  2903. */
  2904. return ast_bridge_dtmf_hook(features, dtmf, builtin_features_handlers[feature],
  2905. config, destructor, remove_flags);
  2906. }
  2907. int ast_bridge_features_limits_construct(struct ast_bridge_features_limits *limits)
  2908. {
  2909. memset(limits, 0, sizeof(*limits));
  2910. if (ast_string_field_init(limits, 256)) {
  2911. return -1;
  2912. }
  2913. return 0;
  2914. }
  2915. void ast_bridge_features_limits_destroy(struct ast_bridge_features_limits *limits)
  2916. {
  2917. ast_string_field_free_memory(limits);
  2918. }
  2919. int ast_bridge_features_set_limits(struct ast_bridge_features *features,
  2920. struct ast_bridge_features_limits *limits,
  2921. enum ast_bridge_hook_remove_flags remove_flags)
  2922. {
  2923. if (builtin_interval_handlers[AST_BRIDGE_BUILTIN_INTERVAL_LIMITS]) {
  2924. ast_bridge_builtin_set_limits_fn callback;
  2925. callback = builtin_interval_handlers[AST_BRIDGE_BUILTIN_INTERVAL_LIMITS];
  2926. return callback(features, limits, remove_flags);
  2927. }
  2928. ast_log(LOG_ERROR, "Attempted to set limits without an AST_BRIDGE_BUILTIN_INTERVAL_LIMITS callback registered.\n");
  2929. return -1;
  2930. }
  2931. void ast_bridge_features_set_flag(struct ast_bridge_features *features, unsigned int flag)
  2932. {
  2933. ast_set_flag(&features->feature_flags, flag);
  2934. features->usable = 1;
  2935. }
  2936. /*!
  2937. * \internal
  2938. * \brief ao2 object match hooks with appropriate remove_flags.
  2939. * \since 12.0.0
  2940. *
  2941. * \param obj Feature hook object.
  2942. * \param arg Removal flags
  2943. * \param flags Not used
  2944. *
  2945. * \retval CMP_MATCH if hook's remove_flags match the removal flags set.
  2946. * \retval 0 if not match.
  2947. */
  2948. static int hook_remove_match(void *obj, void *arg, int flags)
  2949. {
  2950. struct ast_bridge_hook *hook = obj;
  2951. enum ast_bridge_hook_remove_flags *remove_flags = arg;
  2952. if (ast_test_flag(&hook->remove_flags, *remove_flags)) {
  2953. return CMP_MATCH;
  2954. } else {
  2955. return 0;
  2956. }
  2957. }
  2958. /*!
  2959. * \internal
  2960. * \brief Remove all hooks with appropriate remove_flags in the container.
  2961. * \since 12.0.0
  2962. *
  2963. * \param hooks Hooks container to work on.
  2964. * \param remove_flags Determinator for whether hook is removed
  2965. *
  2966. * \return Nothing
  2967. */
  2968. static void hooks_remove_container(struct ao2_container *hooks, enum ast_bridge_hook_remove_flags remove_flags)
  2969. {
  2970. ao2_callback(hooks, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE,
  2971. hook_remove_match, &remove_flags);
  2972. }
  2973. /*!
  2974. * \internal
  2975. * \brief Remove all hooks in the heap with appropriate remove_flags set.
  2976. * \since 12.0.0
  2977. *
  2978. * \param hooks Hooks heap to work on.
  2979. * \param remove_flags Determinator for whether hook is removed
  2980. *
  2981. * \return Nothing
  2982. */
  2983. static void hooks_remove_heap(struct ast_heap *hooks, enum ast_bridge_hook_remove_flags remove_flags)
  2984. {
  2985. struct ast_bridge_hook *hook;
  2986. int changed;
  2987. ast_heap_wrlock(hooks);
  2988. do {
  2989. int idx;
  2990. changed = 0;
  2991. for (idx = ast_heap_size(hooks); idx; --idx) {
  2992. hook = ast_heap_peek(hooks, idx);
  2993. if (ast_test_flag(&hook->remove_flags, remove_flags)) {
  2994. ast_heap_remove(hooks, hook);
  2995. ao2_ref(hook, -1);
  2996. changed = 1;
  2997. }
  2998. }
  2999. } while (changed);
  3000. ast_heap_unlock(hooks);
  3001. }
  3002. void ast_bridge_features_remove(struct ast_bridge_features *features, enum ast_bridge_hook_remove_flags remove_flags)
  3003. {
  3004. hooks_remove_container(features->dtmf_hooks, remove_flags);
  3005. hooks_remove_container(features->other_hooks, remove_flags);
  3006. hooks_remove_heap(features->interval_hooks, remove_flags);
  3007. }
  3008. static int interval_hook_time_cmp(void *a, void *b)
  3009. {
  3010. struct ast_bridge_hook_timer *hook_a = a;
  3011. struct ast_bridge_hook_timer *hook_b = b;
  3012. int cmp;
  3013. cmp = ast_tvcmp(hook_b->timer.trip_time, hook_a->timer.trip_time);
  3014. if (cmp) {
  3015. return cmp;
  3016. }
  3017. cmp = hook_b->timer.seqno - hook_a->timer.seqno;
  3018. return cmp;
  3019. }
  3020. /*!
  3021. * \internal
  3022. * \brief DTMF hook container sort comparison function.
  3023. * \since 12.0.0
  3024. *
  3025. * \param obj_left pointer to the (user-defined part) of an object.
  3026. * \param obj_right pointer to the (user-defined part) of an object.
  3027. * \param flags flags from ao2_callback()
  3028. * OBJ_POINTER - if set, 'obj_right', is an object.
  3029. * OBJ_KEY - if set, 'obj_right', is a search key item that is not an object.
  3030. * OBJ_PARTIAL_KEY - if set, 'obj_right', is a partial search key item that is not an object.
  3031. *
  3032. * \retval <0 if obj_left < obj_right
  3033. * \retval =0 if obj_left == obj_right
  3034. * \retval >0 if obj_left > obj_right
  3035. */
  3036. static int bridge_dtmf_hook_sort(const void *obj_left, const void *obj_right, int flags)
  3037. {
  3038. const struct ast_bridge_hook_dtmf *hook_left = obj_left;
  3039. const struct ast_bridge_hook_dtmf *hook_right = obj_right;
  3040. const char *right_key = obj_right;
  3041. int cmp;
  3042. switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
  3043. default:
  3044. case OBJ_POINTER:
  3045. right_key = hook_right->dtmf.code;
  3046. /* Fall through */
  3047. case OBJ_KEY:
  3048. cmp = strcasecmp(hook_left->dtmf.code, right_key);
  3049. break;
  3050. case OBJ_PARTIAL_KEY:
  3051. cmp = strncasecmp(hook_left->dtmf.code, right_key, strlen(right_key));
  3052. break;
  3053. }
  3054. return cmp;
  3055. }
  3056. /*! \brief Callback for merging hook ao2_containers */
  3057. static int merge_container_cb(void *obj, void *data, int flags)
  3058. {
  3059. ao2_link(data, obj);
  3060. return 0;
  3061. }
  3062. /*! \brief Wrapper for interval hooks that calls into the wrapped hook */
  3063. static int interval_wrapper_cb(struct ast_bridge_channel *bridge_channel, void *obj)
  3064. {
  3065. struct ast_bridge_hook_timer *hook = obj;
  3066. return hook->generic.callback(bridge_channel, hook->generic.hook_pvt);
  3067. }
  3068. /*! \brief Destructor for the hook wrapper */
  3069. static void interval_wrapper_pvt_dtor(void *obj)
  3070. {
  3071. ao2_cleanup(obj);
  3072. }
  3073. /*! \brief Wrap the provided interval hook and add it to features */
  3074. static void wrap_hook(struct ast_bridge_features *features, struct ast_bridge_hook_timer *hook)
  3075. {
  3076. /* Break out of the current wrapper if it exists to avoid multiple layers */
  3077. if (hook->generic.callback == interval_wrapper_cb) {
  3078. hook = hook->generic.hook_pvt;
  3079. }
  3080. ast_bridge_interval_hook(features, hook->timer.flags, hook->timer.interval,
  3081. interval_wrapper_cb, ao2_bump(hook), interval_wrapper_pvt_dtor,
  3082. hook->generic.remove_flags.flags);
  3083. }
  3084. void ast_bridge_features_merge(struct ast_bridge_features *into, const struct ast_bridge_features *from)
  3085. {
  3086. struct ast_bridge_hook_timer *hook;
  3087. int idx;
  3088. /* Merge hook containers */
  3089. ao2_callback(from->dtmf_hooks, 0, merge_container_cb, into->dtmf_hooks);
  3090. ao2_callback(from->other_hooks, 0, merge_container_cb, into->other_hooks);
  3091. /* Merge hook heaps */
  3092. ast_heap_wrlock(from->interval_hooks);
  3093. for (idx = 1; (hook = ast_heap_peek(from->interval_hooks, idx)); idx++) {
  3094. wrap_hook(into, hook);
  3095. }
  3096. ast_heap_unlock(from->interval_hooks);
  3097. /* Merge feature flags */
  3098. into->feature_flags.flags |= from->feature_flags.flags;
  3099. into->usable |= from->usable;
  3100. into->mute |= from->mute;
  3101. into->dtmf_passthrough |= from->dtmf_passthrough;
  3102. }
  3103. /* XXX ASTERISK-21271 make ast_bridge_features_init() static when make ast_bridge_join() requires features to be allocated. */
  3104. int ast_bridge_features_init(struct ast_bridge_features *features)
  3105. {
  3106. /* Zero out the structure */
  3107. memset(features, 0, sizeof(*features));
  3108. /* Initialize the DTMF hooks container */
  3109. features->dtmf_hooks = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX,
  3110. AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE, bridge_dtmf_hook_sort, NULL);
  3111. if (!features->dtmf_hooks) {
  3112. return -1;
  3113. }
  3114. /* Initialize the miscellaneous other hooks container */
  3115. features->other_hooks = ao2_container_alloc_list(AO2_ALLOC_OPT_LOCK_MUTEX, 0, NULL,
  3116. NULL);
  3117. if (!features->other_hooks) {
  3118. return -1;
  3119. }
  3120. /* Initialize the interval hooks heap */
  3121. features->interval_hooks = ast_heap_create(8, interval_hook_time_cmp,
  3122. offsetof(struct ast_bridge_hook_timer, timer.heap_index));
  3123. if (!features->interval_hooks) {
  3124. return -1;
  3125. }
  3126. features->dtmf_passthrough = 1;
  3127. return 0;
  3128. }
  3129. /* XXX ASTERISK-21271 make ast_bridge_features_cleanup() static when make ast_bridge_join() requires features to be allocated. */
  3130. void ast_bridge_features_cleanup(struct ast_bridge_features *features)
  3131. {
  3132. struct ast_bridge_hook_timer *hook;
  3133. /* Destroy the interval hooks heap. */
  3134. if (features->interval_hooks) {
  3135. while ((hook = ast_heap_pop(features->interval_hooks))) {
  3136. ao2_ref(hook, -1);
  3137. }
  3138. features->interval_hooks = ast_heap_destroy(features->interval_hooks);
  3139. }
  3140. /* Destroy the miscellaneous other hooks container. */
  3141. ao2_cleanup(features->other_hooks);
  3142. features->other_hooks = NULL;
  3143. /* Destroy the DTMF hooks container. */
  3144. ao2_cleanup(features->dtmf_hooks);
  3145. features->dtmf_hooks = NULL;
  3146. }
  3147. void ast_bridge_features_destroy(struct ast_bridge_features *features)
  3148. {
  3149. if (!features) {
  3150. return;
  3151. }
  3152. ast_bridge_features_cleanup(features);
  3153. ast_free(features);
  3154. }
  3155. struct ast_bridge_features *ast_bridge_features_new(void)
  3156. {
  3157. struct ast_bridge_features *features;
  3158. features = ast_malloc(sizeof(*features));
  3159. if (features) {
  3160. if (ast_bridge_features_init(features)) {
  3161. ast_bridge_features_destroy(features);
  3162. features = NULL;
  3163. }
  3164. }
  3165. return features;
  3166. }
  3167. void ast_bridge_set_mixing_interval(struct ast_bridge *bridge, unsigned int mixing_interval)
  3168. {
  3169. ast_bridge_lock(bridge);
  3170. bridge->softmix.internal_mixing_interval = mixing_interval;
  3171. ast_bridge_unlock(bridge);
  3172. }
  3173. void ast_bridge_set_internal_sample_rate(struct ast_bridge *bridge, unsigned int sample_rate)
  3174. {
  3175. ast_bridge_lock(bridge);
  3176. bridge->softmix.internal_sample_rate = sample_rate;
  3177. ast_bridge_unlock(bridge);
  3178. }
  3179. static void cleanup_video_mode(struct ast_bridge *bridge)
  3180. {
  3181. switch (bridge->softmix.video_mode.mode) {
  3182. case AST_BRIDGE_VIDEO_MODE_NONE:
  3183. break;
  3184. case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
  3185. if (bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc) {
  3186. ast_channel_unref(bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc);
  3187. }
  3188. break;
  3189. case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
  3190. if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc) {
  3191. ast_channel_unref(bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc);
  3192. }
  3193. if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_old_vsrc) {
  3194. ast_channel_unref(bridge->softmix.video_mode.mode_data.talker_src_data.chan_old_vsrc);
  3195. }
  3196. }
  3197. memset(&bridge->softmix.video_mode, 0, sizeof(bridge->softmix.video_mode));
  3198. }
  3199. void ast_bridge_set_single_src_video_mode(struct ast_bridge *bridge, struct ast_channel *video_src_chan)
  3200. {
  3201. ast_bridge_lock(bridge);
  3202. cleanup_video_mode(bridge);
  3203. bridge->softmix.video_mode.mode = AST_BRIDGE_VIDEO_MODE_SINGLE_SRC;
  3204. bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc = ast_channel_ref(video_src_chan);
  3205. ast_test_suite_event_notify("BRIDGE_VIDEO_MODE", "Message: video mode set to single source\r\nVideo Mode: %u\r\nVideo Channel: %s",
  3206. bridge->softmix.video_mode.mode, ast_channel_name(video_src_chan));
  3207. ast_indicate(video_src_chan, AST_CONTROL_VIDUPDATE);
  3208. ast_bridge_unlock(bridge);
  3209. }
  3210. void ast_bridge_set_talker_src_video_mode(struct ast_bridge *bridge)
  3211. {
  3212. ast_bridge_lock(bridge);
  3213. cleanup_video_mode(bridge);
  3214. bridge->softmix.video_mode.mode = AST_BRIDGE_VIDEO_MODE_TALKER_SRC;
  3215. ast_test_suite_event_notify("BRIDGE_VIDEO_MODE", "Message: video mode set to talker source\r\nVideo Mode: %u",
  3216. bridge->softmix.video_mode.mode);
  3217. ast_bridge_unlock(bridge);
  3218. }
  3219. void ast_bridge_update_talker_src_video_mode(struct ast_bridge *bridge, struct ast_channel *chan, int talker_energy, int is_keyframe)
  3220. {
  3221. struct ast_bridge_video_talker_src_data *data;
  3222. /* If the channel doesn't support video, we don't care about it */
  3223. if (!ast_format_cap_has_type(ast_channel_nativeformats(chan), AST_MEDIA_TYPE_VIDEO)) {
  3224. return;
  3225. }
  3226. ast_bridge_lock(bridge);
  3227. data = &bridge->softmix.video_mode.mode_data.talker_src_data;
  3228. if (data->chan_vsrc == chan) {
  3229. data->average_talking_energy = talker_energy;
  3230. } else if ((data->average_talking_energy < talker_energy) && is_keyframe) {
  3231. if (data->chan_old_vsrc) {
  3232. ast_channel_unref(data->chan_old_vsrc);
  3233. }
  3234. if (data->chan_vsrc) {
  3235. data->chan_old_vsrc = data->chan_vsrc;
  3236. ast_indicate(data->chan_old_vsrc, AST_CONTROL_VIDUPDATE);
  3237. }
  3238. data->chan_vsrc = ast_channel_ref(chan);
  3239. data->average_talking_energy = talker_energy;
  3240. ast_test_suite_event_notify("BRIDGE_VIDEO_SRC", "Message: video source updated\r\nVideo Channel: %s", ast_channel_name(data->chan_vsrc));
  3241. ast_indicate(data->chan_vsrc, AST_CONTROL_VIDUPDATE);
  3242. } else if ((data->average_talking_energy < talker_energy) && !is_keyframe) {
  3243. ast_indicate(chan, AST_CONTROL_VIDUPDATE);
  3244. } else if (!data->chan_vsrc && is_keyframe) {
  3245. data->chan_vsrc = ast_channel_ref(chan);
  3246. data->average_talking_energy = talker_energy;
  3247. ast_test_suite_event_notify("BRIDGE_VIDEO_SRC", "Message: video source updated\r\nVideo Channel: %s", ast_channel_name(data->chan_vsrc));
  3248. ast_indicate(chan, AST_CONTROL_VIDUPDATE);
  3249. } else if (!data->chan_old_vsrc && is_keyframe) {
  3250. data->chan_old_vsrc = ast_channel_ref(chan);
  3251. ast_indicate(chan, AST_CONTROL_VIDUPDATE);
  3252. }
  3253. ast_bridge_unlock(bridge);
  3254. }
  3255. int ast_bridge_number_video_src(struct ast_bridge *bridge)
  3256. {
  3257. int res = 0;
  3258. ast_bridge_lock(bridge);
  3259. switch (bridge->softmix.video_mode.mode) {
  3260. case AST_BRIDGE_VIDEO_MODE_NONE:
  3261. break;
  3262. case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
  3263. if (bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc) {
  3264. res = 1;
  3265. }
  3266. break;
  3267. case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
  3268. if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc) {
  3269. res++;
  3270. }
  3271. if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_old_vsrc) {
  3272. res++;
  3273. }
  3274. }
  3275. ast_bridge_unlock(bridge);
  3276. return res;
  3277. }
  3278. int ast_bridge_is_video_src(struct ast_bridge *bridge, struct ast_channel *chan)
  3279. {
  3280. int res = 0;
  3281. ast_bridge_lock(bridge);
  3282. switch (bridge->softmix.video_mode.mode) {
  3283. case AST_BRIDGE_VIDEO_MODE_NONE:
  3284. break;
  3285. case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
  3286. if (bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc == chan) {
  3287. res = 1;
  3288. }
  3289. break;
  3290. case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
  3291. if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc == chan) {
  3292. res = 1;
  3293. } else if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_old_vsrc == chan) {
  3294. res = 2;
  3295. }
  3296. }
  3297. ast_bridge_unlock(bridge);
  3298. return res;
  3299. }
  3300. void ast_bridge_remove_video_src(struct ast_bridge *bridge, struct ast_channel *chan)
  3301. {
  3302. ast_bridge_lock(bridge);
  3303. switch (bridge->softmix.video_mode.mode) {
  3304. case AST_BRIDGE_VIDEO_MODE_NONE:
  3305. break;
  3306. case AST_BRIDGE_VIDEO_MODE_SINGLE_SRC:
  3307. if (bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc == chan) {
  3308. if (bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc) {
  3309. ast_channel_unref(bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc);
  3310. }
  3311. bridge->softmix.video_mode.mode_data.single_src_data.chan_vsrc = NULL;
  3312. }
  3313. break;
  3314. case AST_BRIDGE_VIDEO_MODE_TALKER_SRC:
  3315. if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc == chan) {
  3316. if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc) {
  3317. ast_channel_unref(bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc);
  3318. }
  3319. bridge->softmix.video_mode.mode_data.talker_src_data.chan_vsrc = NULL;
  3320. bridge->softmix.video_mode.mode_data.talker_src_data.average_talking_energy = 0;
  3321. }
  3322. if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_old_vsrc == chan) {
  3323. if (bridge->softmix.video_mode.mode_data.talker_src_data.chan_old_vsrc) {
  3324. ast_channel_unref(bridge->softmix.video_mode.mode_data.talker_src_data.chan_old_vsrc);
  3325. }
  3326. bridge->softmix.video_mode.mode_data.talker_src_data.chan_old_vsrc = NULL;
  3327. }
  3328. }
  3329. ast_bridge_unlock(bridge);
  3330. }
  3331. static int channel_hash(const void *obj, int flags)
  3332. {
  3333. const struct ast_channel *chan = obj;
  3334. const char *name = obj;
  3335. int hash;
  3336. switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
  3337. default:
  3338. case OBJ_POINTER:
  3339. name = ast_channel_name(chan);
  3340. /* Fall through */
  3341. case OBJ_KEY:
  3342. hash = ast_str_hash(name);
  3343. break;
  3344. case OBJ_PARTIAL_KEY:
  3345. /* Should never happen in hash callback. */
  3346. ast_assert(0);
  3347. hash = 0;
  3348. break;
  3349. }
  3350. return hash;
  3351. }
  3352. static int channel_cmp(void *obj, void *arg, int flags)
  3353. {
  3354. const struct ast_channel *left = obj;
  3355. const struct ast_channel *right = arg;
  3356. const char *right_name = arg;
  3357. int cmp;
  3358. switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
  3359. default:
  3360. case OBJ_POINTER:
  3361. right_name = ast_channel_name(right);
  3362. /* Fall through */
  3363. case OBJ_KEY:
  3364. cmp = strcmp(ast_channel_name(left), right_name);
  3365. break;
  3366. case OBJ_PARTIAL_KEY:
  3367. cmp = strncmp(ast_channel_name(left), right_name, strlen(right_name));
  3368. break;
  3369. }
  3370. return cmp ? 0 : CMP_MATCH;
  3371. }
  3372. struct ao2_container *ast_bridge_peers_nolock(struct ast_bridge *bridge)
  3373. {
  3374. struct ao2_container *channels;
  3375. struct ast_bridge_channel *iter;
  3376. channels = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK,
  3377. 13, channel_hash, channel_cmp);
  3378. if (!channels) {
  3379. return NULL;
  3380. }
  3381. AST_LIST_TRAVERSE(&bridge->channels, iter, entry) {
  3382. ao2_link(channels, iter->chan);
  3383. }
  3384. return channels;
  3385. }
  3386. struct ao2_container *ast_bridge_peers(struct ast_bridge *bridge)
  3387. {
  3388. struct ao2_container *channels;
  3389. ast_bridge_lock(bridge);
  3390. channels = ast_bridge_peers_nolock(bridge);
  3391. ast_bridge_unlock(bridge);
  3392. return channels;
  3393. }
  3394. struct ast_channel *ast_bridge_peer_nolock(struct ast_bridge *bridge, struct ast_channel *chan)
  3395. {
  3396. struct ast_channel *peer = NULL;
  3397. struct ast_bridge_channel *iter;
  3398. /* Asking for the peer channel only makes sense on a two-party bridge. */
  3399. if (bridge->num_channels == 2
  3400. && bridge->technology->capabilities
  3401. & (AST_BRIDGE_CAPABILITY_NATIVE | AST_BRIDGE_CAPABILITY_1TO1MIX)) {
  3402. int in_bridge = 0;
  3403. AST_LIST_TRAVERSE(&bridge->channels, iter, entry) {
  3404. if (iter->chan != chan) {
  3405. peer = iter->chan;
  3406. } else {
  3407. in_bridge = 1;
  3408. }
  3409. }
  3410. if (in_bridge && peer) {
  3411. ast_channel_ref(peer);
  3412. } else {
  3413. peer = NULL;
  3414. }
  3415. }
  3416. return peer;
  3417. }
  3418. struct ast_channel *ast_bridge_peer(struct ast_bridge *bridge, struct ast_channel *chan)
  3419. {
  3420. struct ast_channel *peer;
  3421. ast_bridge_lock(bridge);
  3422. peer = ast_bridge_peer_nolock(bridge, chan);
  3423. ast_bridge_unlock(bridge);
  3424. return peer;
  3425. }
  3426. /*!
  3427. * \internal
  3428. * \brief Transfer an entire bridge to a specific destination.
  3429. *
  3430. * This creates a local channel to dial out and swaps the called local channel
  3431. * with the transferer channel. By doing so, all participants in the bridge are
  3432. * connected to the specified destination.
  3433. *
  3434. * While this means of transferring would work for both two-party and multi-party
  3435. * bridges, this method is only used for multi-party bridges since this method would
  3436. * be less efficient for two-party bridges.
  3437. *
  3438. * \param is_external Whether the transfer is externally initiated
  3439. * \param transferer The channel performing a transfer
  3440. * \param bridge The bridge where the transfer is being performed
  3441. * \param exten The destination extension for the blind transfer
  3442. * \param context The destination context for the blind transfer
  3443. * \param transferee The party being transferred if there is only one
  3444. * \param new_channel_cb Callback to call on channel that is created to
  3445. * facilitate the blind transfer.
  3446. * \param user_data_wrapper User-provided data needed in new_channel_cb
  3447. * \param transfer_message The Stasis publication for this transfer.
  3448. *
  3449. * \return The success or failure of the operation
  3450. */
  3451. static enum ast_transfer_result blind_transfer_bridge(int is_external,
  3452. struct ast_channel *transferer, struct ast_bridge *bridge,
  3453. const char *exten, const char *context, struct ast_channel *transferee,
  3454. transfer_channel_cb new_channel_cb,
  3455. struct transfer_channel_data *user_data_wrapper,
  3456. struct ast_blind_transfer_message *transfer_message)
  3457. {
  3458. struct ast_channel *local;
  3459. char chan_name[AST_MAX_EXTENSION + AST_MAX_CONTEXT + 2];
  3460. int cause;
  3461. snprintf(chan_name, sizeof(chan_name), "%s@%s", exten, context);
  3462. local = ast_request("Local", ast_channel_nativeformats(transferer), NULL, transferer,
  3463. chan_name, &cause);
  3464. if (!local) {
  3465. return AST_BRIDGE_TRANSFER_FAIL;
  3466. }
  3467. ast_channel_lock_both(local, transferer);
  3468. ast_channel_req_accountcodes(local, transferer, AST_CHANNEL_REQUESTOR_REPLACEMENT);
  3469. transfer_message->replace_channel = ast_channel_snapshot_get_latest(ast_channel_uniqueid(local));
  3470. if (!transfer_message->replace_channel) {
  3471. ast_hangup(local);
  3472. return AST_BRIDGE_TRANSFER_FAIL;
  3473. }
  3474. pbx_builtin_setvar_helper(local, BLINDTRANSFER, ast_channel_name(transferer));
  3475. ast_channel_unlock(local);
  3476. ast_channel_unlock(transferer);
  3477. if (new_channel_cb) {
  3478. new_channel_cb(local, user_data_wrapper, AST_BRIDGE_TRANSFER_MULTI_PARTY);
  3479. }
  3480. if (ast_call(local, chan_name, 0)) {
  3481. ast_hangup(local);
  3482. return AST_BRIDGE_TRANSFER_FAIL;
  3483. }
  3484. if (ast_bridge_impart(bridge, local, transferer, NULL,
  3485. AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
  3486. ast_hangup(local);
  3487. return AST_BRIDGE_TRANSFER_FAIL;
  3488. }
  3489. return AST_BRIDGE_TRANSFER_SUCCESS;
  3490. }
  3491. /*!
  3492. * \internal
  3493. * \brief Get the transferee channel
  3494. *
  3495. * This is only applicable to cases where a transfer is occurring on a
  3496. * two-party bridge. The channels container passed in is expected to only
  3497. * contain two channels, the transferer and the transferee. The transferer
  3498. * channel is passed in as a parameter to ensure we don't return it as
  3499. * the transferee channel.
  3500. *
  3501. * \param channels A two-channel container containing the transferer and transferee
  3502. * \param transferer The party that is transfering the call
  3503. * \return The party that is being transferred
  3504. */
  3505. static struct ast_channel *get_transferee(struct ao2_container *channels, struct ast_channel *transferer)
  3506. {
  3507. struct ao2_iterator channel_iter;
  3508. struct ast_channel *transferee;
  3509. for (channel_iter = ao2_iterator_init(channels, 0);
  3510. (transferee = ao2_iterator_next(&channel_iter));
  3511. ao2_cleanup(transferee)) {
  3512. if (transferee != transferer) {
  3513. break;
  3514. }
  3515. }
  3516. ao2_iterator_destroy(&channel_iter);
  3517. return transferee;
  3518. }
  3519. /*!
  3520. * \brief Perform an attended transfer of a bridge
  3521. *
  3522. * This performs an attended transfer of an entire bridge to a target.
  3523. * The target varies, depending on what bridges exist during the transfer
  3524. * attempt.
  3525. *
  3526. * If two bridges exist, then a local channel is created to link the two
  3527. * bridges together.
  3528. *
  3529. * If only one bridge exists, then a local channel is created with one end
  3530. * placed into the existing bridge and the other end masquerading into
  3531. * the unbridged channel.
  3532. *
  3533. * \param chan1 Transferer channel. Guaranteed to be bridged.
  3534. * \param chan2 Other transferer channel. May or may not be bridged.
  3535. * \param bridge1 Bridge that chan1 is in. Guaranteed to be non-NULL.
  3536. * \param bridge2 Bridge that chan2 is in. If NULL, then chan2 is not bridged.
  3537. * \param publication Data to publish for a stasis attended transfer message.
  3538. * \retval AST_BRIDGE_TRANSFER_FAIL Internal error occurred
  3539. * \retval AST_BRIDGE_TRANSFER_SUCCESS Succesfully transferred the bridge
  3540. */
  3541. static enum ast_transfer_result attended_transfer_bridge(struct ast_channel *chan1,
  3542. struct ast_channel *chan2, struct ast_bridge *bridge1, struct ast_bridge *bridge2,
  3543. struct ast_attended_transfer_message *transfer_msg)
  3544. {
  3545. static const char *dest = "_attended@transfer/m";
  3546. struct ast_channel *local_chan;
  3547. int cause;
  3548. int res;
  3549. const char *app = NULL;
  3550. local_chan = ast_request("Local", ast_channel_nativeformats(chan1), NULL, chan1,
  3551. dest, &cause);
  3552. if (!local_chan) {
  3553. return AST_BRIDGE_TRANSFER_FAIL;
  3554. }
  3555. ast_channel_lock_both(local_chan, chan1);
  3556. ast_channel_req_accountcodes(local_chan, chan1, AST_CHANNEL_REQUESTOR_REPLACEMENT);
  3557. pbx_builtin_setvar_helper(local_chan, ATTENDEDTRANSFER, ast_channel_name(chan1));
  3558. ast_channel_unlock(local_chan);
  3559. ast_channel_unlock(chan1);
  3560. if (bridge2) {
  3561. res = ast_local_setup_bridge(local_chan, bridge2, chan2, NULL);
  3562. } else {
  3563. app = ast_strdupa(ast_channel_appl(chan2));
  3564. res = ast_local_setup_masquerade(local_chan, chan2);
  3565. }
  3566. if (res) {
  3567. ast_hangup(local_chan);
  3568. return AST_BRIDGE_TRANSFER_FAIL;
  3569. }
  3570. if (ast_call(local_chan, dest, 0)) {
  3571. ast_hangup(local_chan);
  3572. return AST_BRIDGE_TRANSFER_FAIL;
  3573. }
  3574. /* Get a ref for use later since this one is being stolen */
  3575. ao2_ref(local_chan, +1);
  3576. if (ast_bridge_impart(bridge1, local_chan, chan1, NULL,
  3577. AST_BRIDGE_IMPART_CHAN_INDEPENDENT)) {
  3578. ast_hangup(local_chan);
  3579. ao2_cleanup(local_chan);
  3580. return AST_BRIDGE_TRANSFER_FAIL;
  3581. }
  3582. if (bridge2) {
  3583. RAII_VAR(struct ast_channel *, local_chan2, NULL, ao2_cleanup);
  3584. struct ast_channel *locals[2];
  3585. ast_channel_lock(local_chan);
  3586. local_chan2 = ast_local_get_peer(local_chan);
  3587. ast_channel_unlock(local_chan);
  3588. ast_assert(local_chan2 != NULL);
  3589. locals[0] = local_chan;
  3590. locals[1] = local_chan2;
  3591. ast_attended_transfer_message_add_link(transfer_msg, locals);
  3592. } else {
  3593. ast_attended_transfer_message_add_app(transfer_msg, app, local_chan);
  3594. }
  3595. ao2_cleanup(local_chan);
  3596. return AST_BRIDGE_TRANSFER_SUCCESS;
  3597. }
  3598. static enum ast_transfer_result try_parking(struct ast_channel *transferer,
  3599. const char *context, const char *exten, transfer_channel_cb new_channel_cb,
  3600. struct transfer_channel_data *user_data_wrapper)
  3601. {
  3602. RAII_VAR(struct ast_bridge_channel *, transferer_bridge_channel, NULL, ao2_cleanup);
  3603. if (!ast_parking_provider_registered()) {
  3604. return AST_BRIDGE_TRANSFER_FAIL;
  3605. }
  3606. ast_channel_lock(transferer);
  3607. transferer_bridge_channel = ast_channel_get_bridge_channel(transferer);
  3608. ast_channel_unlock(transferer);
  3609. if (!transferer_bridge_channel) {
  3610. return AST_BRIDGE_TRANSFER_FAIL;
  3611. }
  3612. if (ast_parking_blind_transfer_park(transferer_bridge_channel,
  3613. context, exten, new_channel_cb, user_data_wrapper)) {
  3614. return AST_BRIDGE_TRANSFER_FAIL;
  3615. }
  3616. return AST_BRIDGE_TRANSFER_SUCCESS;
  3617. }
  3618. void ast_bridge_set_transfer_variables(struct ast_channel *chan, const char *value, int attended)
  3619. {
  3620. char *writevar;
  3621. char *erasevar;
  3622. if (attended) {
  3623. writevar = ATTENDEDTRANSFER;
  3624. erasevar = BLINDTRANSFER;
  3625. } else {
  3626. writevar = BLINDTRANSFER;
  3627. erasevar = ATTENDEDTRANSFER;
  3628. }
  3629. pbx_builtin_setvar_helper(chan, writevar, value);
  3630. pbx_builtin_setvar_helper(chan, erasevar, NULL);
  3631. }
  3632. /*!
  3633. * \internal
  3634. * \brief Set the transfer variable as appropriate on channels involved in the transfer
  3635. *
  3636. * The transferer channel will have its variable set the same as its BRIDGEPEER
  3637. * variable. This will account for all channels that it is bridged to. The other channels
  3638. * involved in the transfer will have their variable set to the transferer
  3639. * channel's name.
  3640. *
  3641. * \param transferer The channel performing the transfer
  3642. * \param channels The channels belonging to the bridge
  3643. * \param is_attended false set BLINDTRANSFER and unset ATTENDEDTRANSFER
  3644. * true set ATTENDEDTRANSFER and unset BLINDTRANSFER
  3645. */
  3646. static void set_transfer_variables_all(struct ast_channel *transferer, struct ao2_container *channels, int is_attended)
  3647. {
  3648. struct ao2_iterator iter;
  3649. struct ast_channel *chan;
  3650. const char *transferer_name;
  3651. const char *transferer_bridgepeer;
  3652. ast_channel_lock(transferer);
  3653. transferer_name = ast_strdupa(ast_channel_name(transferer));
  3654. transferer_bridgepeer = ast_strdupa(S_OR(pbx_builtin_getvar_helper(transferer, "BRIDGEPEER"), ""));
  3655. ast_channel_unlock(transferer);
  3656. for (iter = ao2_iterator_init(channels, 0);
  3657. (chan = ao2_iterator_next(&iter));
  3658. ao2_cleanup(chan)) {
  3659. if (chan == transferer) {
  3660. ast_bridge_set_transfer_variables(chan, transferer_bridgepeer, is_attended);
  3661. } else {
  3662. ast_bridge_set_transfer_variables(chan, transferer_name, is_attended);
  3663. }
  3664. }
  3665. ao2_iterator_destroy(&iter);
  3666. }
  3667. static struct ast_bridge *acquire_bridge(struct ast_channel *chan)
  3668. {
  3669. struct ast_bridge *bridge;
  3670. ast_channel_lock(chan);
  3671. bridge = ast_channel_get_bridge(chan);
  3672. ast_channel_unlock(chan);
  3673. if (bridge
  3674. && ast_test_flag(&bridge->feature_flags, AST_BRIDGE_FLAG_MASQUERADE_ONLY)) {
  3675. ao2_ref(bridge, -1);
  3676. bridge = NULL;
  3677. }
  3678. return bridge;
  3679. }
  3680. enum ast_transfer_result ast_bridge_transfer_blind(int is_external,
  3681. struct ast_channel *transferer, const char *exten, const char *context,
  3682. transfer_channel_cb new_channel_cb, void *user_data)
  3683. {
  3684. RAII_VAR(struct ast_bridge *, bridge, NULL, ao2_cleanup);
  3685. RAII_VAR(struct ast_bridge_channel *, bridge_channel, NULL, ao2_cleanup);
  3686. RAII_VAR(struct ao2_container *, channels, NULL, ao2_cleanup);
  3687. RAII_VAR(struct ast_channel *, transferee, NULL, ast_channel_cleanup);
  3688. RAII_VAR(struct transfer_channel_data *, user_data_wrapper, NULL, ao2_cleanup);
  3689. RAII_VAR(struct ast_blind_transfer_message *, transfer_message, NULL, ao2_cleanup);
  3690. int do_bridge_transfer;
  3691. int transfer_prohibited;
  3692. enum ast_transfer_result transfer_result;
  3693. transfer_message = ast_blind_transfer_message_create(is_external, transferer, exten, context);
  3694. if (!transfer_message) {
  3695. /* Out of memory. Not even possible to publish a Stasis message about the
  3696. * failure
  3697. */
  3698. ast_log(LOG_ERROR, "Unable to allocate memory for blind transfer publication from %s\n",
  3699. ast_channel_name(transferer));
  3700. return AST_BRIDGE_TRANSFER_FAIL;
  3701. }
  3702. bridge = acquire_bridge(transferer);
  3703. if (!bridge) {
  3704. transfer_result = AST_BRIDGE_TRANSFER_INVALID;
  3705. goto publish;
  3706. }
  3707. ast_bridge_lock(bridge);
  3708. transfer_message->bridge = ast_bridge_snapshot_create(bridge);
  3709. ast_bridge_unlock(bridge);
  3710. if (!transfer_message->bridge) {
  3711. transfer_result = AST_BRIDGE_TRANSFER_FAIL;
  3712. goto publish;
  3713. }
  3714. transferee = ast_bridge_peer(bridge, transferer);
  3715. if (transferee) {
  3716. transfer_message->transferee = ast_channel_snapshot_get_latest(ast_channel_uniqueid(transferee));
  3717. if (!transfer_message->transferee) {
  3718. transfer_result = AST_BRIDGE_TRANSFER_FAIL;
  3719. goto publish;
  3720. }
  3721. }
  3722. ast_channel_lock(transferer);
  3723. bridge_channel = ast_channel_get_bridge_channel(transferer);
  3724. ast_channel_unlock(transferer);
  3725. if (!bridge_channel) {
  3726. transfer_result = AST_BRIDGE_TRANSFER_INVALID;
  3727. goto publish;
  3728. }
  3729. user_data_wrapper = ao2_alloc(sizeof(*user_data_wrapper), NULL);
  3730. if (!user_data_wrapper) {
  3731. transfer_result = AST_BRIDGE_TRANSFER_FAIL;
  3732. goto publish;
  3733. }
  3734. user_data_wrapper->data = user_data;
  3735. /* Take off hold if they are on hold. */
  3736. ast_bridge_channel_write_unhold(bridge_channel);
  3737. transfer_result = try_parking(transferer, context, exten, new_channel_cb, user_data_wrapper);
  3738. if (transfer_result == AST_BRIDGE_TRANSFER_SUCCESS) {
  3739. goto publish;
  3740. }
  3741. /* Since parking didn't take control of the user_data_wrapper, we are just going to raise the completed flag now. */
  3742. user_data_wrapper->completed = 1;
  3743. {
  3744. SCOPED_LOCK(lock, bridge, ast_bridge_lock, ast_bridge_unlock);
  3745. channels = ast_bridge_peers_nolock(bridge);
  3746. if (!channels) {
  3747. transfer_result = AST_BRIDGE_TRANSFER_FAIL;
  3748. goto publish;
  3749. }
  3750. if (ao2_container_count(channels) <= 1) {
  3751. transfer_result = AST_BRIDGE_TRANSFER_INVALID;
  3752. goto publish;
  3753. }
  3754. transfer_prohibited = ast_test_flag(&bridge->feature_flags,
  3755. AST_BRIDGE_FLAG_TRANSFER_PROHIBITED);
  3756. do_bridge_transfer = ast_test_flag(&bridge->feature_flags,
  3757. AST_BRIDGE_FLAG_TRANSFER_BRIDGE_ONLY) ||
  3758. ao2_container_count(channels) > 2;
  3759. }
  3760. if (transfer_prohibited) {
  3761. transfer_result = AST_BRIDGE_TRANSFER_NOT_PERMITTED;
  3762. goto publish;
  3763. }
  3764. set_transfer_variables_all(transferer, channels, 0);
  3765. if (do_bridge_transfer) {
  3766. transfer_result = blind_transfer_bridge(is_external, transferer, bridge,
  3767. exten, context, transferee, new_channel_cb, user_data_wrapper, transfer_message);
  3768. goto publish;
  3769. }
  3770. /* Reaching this portion means that we're dealing with a two-party bridge */
  3771. if (!transferee) {
  3772. transfer_result = AST_BRIDGE_TRANSFER_FAIL;
  3773. goto publish;
  3774. }
  3775. if (bridge_channel_internal_queue_blind_transfer(transferee, exten, context,
  3776. new_channel_cb, user_data_wrapper)) {
  3777. transfer_result = AST_BRIDGE_TRANSFER_FAIL;
  3778. goto publish;
  3779. }
  3780. ast_bridge_remove(bridge, transferer);
  3781. transfer_result = AST_BRIDGE_TRANSFER_SUCCESS;
  3782. publish:
  3783. transfer_message->result = transfer_result;
  3784. ast_bridge_publish_blind_transfer(transfer_message);
  3785. return transfer_result;
  3786. }
  3787. /*!
  3788. * \internal
  3789. * \brief Performs an attended transfer by moving a channel from one bridge to another
  3790. *
  3791. * The channel that is bridged to the source_channel is moved into the dest_bridge from
  3792. * the source_bridge_channel's bridge. The swap_channel is swapped out of the dest_bridge and placed in
  3793. * the source_bridge_channel's bridge.
  3794. *
  3795. * \note dest_bridge and source_bridge_channel's bridge MUST be locked before calling this function.
  3796. *
  3797. * \param dest_bridge The final bridge for the attended transfer
  3798. * \param source_channel Channel who is bridged to the channel that will move
  3799. * \param swap_channel Channel to be swapped out of the dest_bridge
  3800. * \return The success or failure of the swap attempt
  3801. */
  3802. static enum ast_transfer_result bridge_swap_attended_transfer(struct ast_bridge *dest_bridge,
  3803. struct ast_bridge_channel *source_bridge_channel, struct ast_channel *swap_channel)
  3804. {
  3805. struct ast_bridge_channel *bridged_to_source;
  3806. bridged_to_source = ast_bridge_channel_peer(source_bridge_channel);
  3807. if (bridged_to_source
  3808. && bridged_to_source->state == BRIDGE_CHANNEL_STATE_WAIT
  3809. && !ast_test_flag(&bridged_to_source->features->feature_flags,
  3810. AST_BRIDGE_CHANNEL_FLAG_IMMOVABLE)) {
  3811. bridged_to_source->swap = swap_channel;
  3812. if (bridge_do_move(dest_bridge, bridged_to_source, 1, 0)) {
  3813. return AST_BRIDGE_TRANSFER_FAIL;
  3814. }
  3815. /* Must kick the source channel out of its bridge. */
  3816. ast_bridge_channel_leave_bridge(source_bridge_channel,
  3817. BRIDGE_CHANNEL_STATE_END_NO_DISSOLVE, AST_CAUSE_NORMAL_CLEARING);
  3818. return AST_BRIDGE_TRANSFER_SUCCESS;
  3819. } else {
  3820. return AST_BRIDGE_TRANSFER_INVALID;
  3821. }
  3822. }
  3823. /*!
  3824. * \internal
  3825. * \brief Function that performs an attended transfer when both transferer channels are bridged
  3826. *
  3827. * The method by which the transfer is performed is dependent on whether the bridges allow for
  3828. * optimization to occur between them. If no optimization is permitted, then an unreal channel
  3829. * is placed as a link between the two bridges. If optimization is permitted, then that means
  3830. * we are free to perform move or merge operations in order to perform the transfer.
  3831. *
  3832. * \note to_transferee_bridge and to_target_bridge MUST be locked before calling this function
  3833. *
  3834. * \param to_transferee The channel that is bridged to the transferee
  3835. * \param to_transferee_bridge_channel to_transferee's bridge_channel
  3836. * \param to_transfer_target The channel that is bridged to the transfer target
  3837. * \param to_target_bridge_channel to_transfer_target's bridge_channel
  3838. * \param to_transferee_bridge The bridge between to_transferee and the transferee
  3839. * \param to_target_bridge The bridge between to_transfer_target and the transfer_target
  3840. * \param publication Data to publish for a stasis attended transfer message
  3841. * \return The success or failure of the attended transfer
  3842. */
  3843. static enum ast_transfer_result two_bridge_attended_transfer(struct ast_channel *to_transferee,
  3844. struct ast_bridge_channel *to_transferee_bridge_channel,
  3845. struct ast_channel *to_transfer_target,
  3846. struct ast_bridge_channel *to_target_bridge_channel,
  3847. struct ast_bridge *to_transferee_bridge, struct ast_bridge *to_target_bridge,
  3848. struct ast_attended_transfer_message *transfer_msg)
  3849. {
  3850. struct ast_bridge_channel *kick_me[] = {
  3851. to_transferee_bridge_channel,
  3852. to_target_bridge_channel,
  3853. };
  3854. enum ast_transfer_result res;
  3855. struct ast_bridge *final_bridge = NULL;
  3856. RAII_VAR(struct ao2_container *, channels, NULL, ao2_cleanup);
  3857. channels = ast_bridge_peers_nolock(to_transferee_bridge);
  3858. if (!channels) {
  3859. res = AST_BRIDGE_TRANSFER_FAIL;
  3860. goto end;
  3861. }
  3862. set_transfer_variables_all(to_transferee, channels, 1);
  3863. switch (ast_bridges_allow_optimization(to_transferee_bridge, to_target_bridge)) {
  3864. case AST_BRIDGE_OPTIMIZE_SWAP_TO_CHAN_BRIDGE:
  3865. final_bridge = to_transferee_bridge;
  3866. res = bridge_swap_attended_transfer(to_transferee_bridge, to_target_bridge_channel, to_transferee);
  3867. goto end;
  3868. case AST_BRIDGE_OPTIMIZE_SWAP_TO_PEER_BRIDGE:
  3869. final_bridge = to_target_bridge;
  3870. res = bridge_swap_attended_transfer(to_target_bridge, to_transferee_bridge_channel, to_transfer_target);
  3871. goto end;
  3872. case AST_BRIDGE_OPTIMIZE_MERGE_TO_CHAN_BRIDGE:
  3873. final_bridge = to_transferee_bridge;
  3874. bridge_do_merge(to_transferee_bridge, to_target_bridge, kick_me, ARRAY_LEN(kick_me), 0);
  3875. res = AST_BRIDGE_TRANSFER_SUCCESS;
  3876. goto end;
  3877. case AST_BRIDGE_OPTIMIZE_MERGE_TO_PEER_BRIDGE:
  3878. final_bridge = to_target_bridge;
  3879. bridge_do_merge(to_target_bridge, to_transferee_bridge, kick_me, ARRAY_LEN(kick_me), 0);
  3880. res = AST_BRIDGE_TRANSFER_SUCCESS;
  3881. goto end;
  3882. case AST_BRIDGE_OPTIMIZE_PROHIBITED:
  3883. default:
  3884. /* Just because optimization wasn't doable doesn't necessarily mean
  3885. * that we can actually perform the transfer. Some reasons for non-optimization
  3886. * indicate bridge invalidity, so let's check those before proceeding.
  3887. */
  3888. if (to_transferee_bridge->inhibit_merge || to_transferee_bridge->dissolved ||
  3889. to_target_bridge->inhibit_merge || to_target_bridge->dissolved) {
  3890. return AST_BRIDGE_TRANSFER_INVALID;
  3891. }
  3892. return attended_transfer_bridge(to_transferee, to_transfer_target,
  3893. to_transferee_bridge, to_target_bridge, transfer_msg);
  3894. }
  3895. end:
  3896. if (res == AST_BRIDGE_TRANSFER_SUCCESS) {
  3897. ast_attended_transfer_message_add_merge(transfer_msg, final_bridge);
  3898. }
  3899. return res;
  3900. }
  3901. enum ast_transfer_result ast_bridge_transfer_attended(struct ast_channel *to_transferee,
  3902. struct ast_channel *to_transfer_target)
  3903. {
  3904. RAII_VAR(struct ast_bridge *, to_transferee_bridge, NULL, ao2_cleanup);
  3905. RAII_VAR(struct ast_bridge *, to_target_bridge, NULL, ao2_cleanup);
  3906. RAII_VAR(struct ast_bridge_channel *, to_transferee_bridge_channel, NULL, ao2_cleanup);
  3907. RAII_VAR(struct ast_bridge_channel *, to_target_bridge_channel, NULL, ao2_cleanup);
  3908. RAII_VAR(struct ao2_container *, channels, NULL, ao2_cleanup);
  3909. RAII_VAR(struct ast_channel *, transferee, NULL, ao2_cleanup);
  3910. RAII_VAR(struct ast_attended_transfer_message *, transfer_msg, NULL, ao2_cleanup);
  3911. struct ast_bridge *the_bridge = NULL;
  3912. struct ast_channel *chan_bridged;
  3913. struct ast_channel *chan_unbridged;
  3914. int transfer_prohibited;
  3915. int do_bridge_transfer;
  3916. enum ast_transfer_result res;
  3917. const char *app = NULL;
  3918. to_transferee_bridge = acquire_bridge(to_transferee);
  3919. to_target_bridge = acquire_bridge(to_transfer_target);
  3920. transfer_msg = ast_attended_transfer_message_create(1, to_transferee, to_transferee_bridge,
  3921. to_transfer_target, to_target_bridge, NULL, NULL);
  3922. if (!transfer_msg) {
  3923. ast_log(LOG_ERROR, "Unable to create Stasis publication for attended transfer from %s\n",
  3924. ast_channel_name(to_transferee));
  3925. return AST_BRIDGE_TRANSFER_FAIL;
  3926. }
  3927. /* They can't both be unbridged, you silly goose! */
  3928. if (!to_transferee_bridge && !to_target_bridge) {
  3929. res = AST_BRIDGE_TRANSFER_INVALID;
  3930. goto end;
  3931. }
  3932. ast_channel_lock(to_transferee);
  3933. to_transferee_bridge_channel = ast_channel_get_bridge_channel(to_transferee);
  3934. ast_channel_unlock(to_transferee);
  3935. ast_channel_lock(to_transfer_target);
  3936. to_target_bridge_channel = ast_channel_get_bridge_channel(to_transfer_target);
  3937. ast_channel_unlock(to_transfer_target);
  3938. if (to_transferee_bridge_channel) {
  3939. /* Take off hold if they are on hold. */
  3940. ast_bridge_channel_write_unhold(to_transferee_bridge_channel);
  3941. }
  3942. if (to_target_bridge_channel) {
  3943. const char *target_complete_sound;
  3944. /* Take off hold if they are on hold. */
  3945. ast_bridge_channel_write_unhold(to_target_bridge_channel);
  3946. /* Is there a courtesy sound to play to the target? */
  3947. ast_channel_lock(to_transfer_target);
  3948. target_complete_sound = pbx_builtin_getvar_helper(to_transfer_target,
  3949. "ATTENDED_TRANSFER_COMPLETE_SOUND");
  3950. if (!ast_strlen_zero(target_complete_sound)) {
  3951. target_complete_sound = ast_strdupa(target_complete_sound);
  3952. } else {
  3953. target_complete_sound = NULL;
  3954. }
  3955. ast_channel_unlock(to_transfer_target);
  3956. if (!target_complete_sound) {
  3957. ast_channel_lock(to_transferee);
  3958. target_complete_sound = pbx_builtin_getvar_helper(to_transferee,
  3959. "ATTENDED_TRANSFER_COMPLETE_SOUND");
  3960. if (!ast_strlen_zero(target_complete_sound)) {
  3961. target_complete_sound = ast_strdupa(target_complete_sound);
  3962. } else {
  3963. target_complete_sound = NULL;
  3964. }
  3965. ast_channel_unlock(to_transferee);
  3966. }
  3967. if (target_complete_sound) {
  3968. ast_bridge_channel_write_playfile(to_target_bridge_channel, NULL,
  3969. target_complete_sound, NULL);
  3970. }
  3971. }
  3972. /* Let's get the easy one out of the way first */
  3973. if (to_transferee_bridge && to_target_bridge) {
  3974. if (!to_transferee_bridge_channel || !to_target_bridge_channel) {
  3975. res = AST_BRIDGE_TRANSFER_INVALID;
  3976. goto end;
  3977. }
  3978. ast_bridge_lock_both(to_transferee_bridge, to_target_bridge);
  3979. res = two_bridge_attended_transfer(to_transferee, to_transferee_bridge_channel,
  3980. to_transfer_target, to_target_bridge_channel,
  3981. to_transferee_bridge, to_target_bridge, transfer_msg);
  3982. ast_bridge_unlock(to_transferee_bridge);
  3983. ast_bridge_unlock(to_target_bridge);
  3984. goto end;
  3985. }
  3986. the_bridge = to_transferee_bridge ?: to_target_bridge;
  3987. chan_bridged = to_transferee_bridge ? to_transferee : to_transfer_target;
  3988. chan_unbridged = to_transferee_bridge ? to_transfer_target : to_transferee;
  3989. {
  3990. int chan_count;
  3991. SCOPED_LOCK(lock, the_bridge, ast_bridge_lock, ast_bridge_unlock);
  3992. channels = ast_bridge_peers_nolock(the_bridge);
  3993. if (!channels) {
  3994. res = AST_BRIDGE_TRANSFER_FAIL;
  3995. goto end;
  3996. }
  3997. chan_count = ao2_container_count(channels);
  3998. if (chan_count <= 1) {
  3999. res = AST_BRIDGE_TRANSFER_INVALID;
  4000. goto end;
  4001. }
  4002. transfer_prohibited = ast_test_flag(&the_bridge->feature_flags,
  4003. AST_BRIDGE_FLAG_TRANSFER_PROHIBITED);
  4004. do_bridge_transfer = ast_test_flag(&the_bridge->feature_flags,
  4005. AST_BRIDGE_FLAG_TRANSFER_BRIDGE_ONLY) ||
  4006. chan_count > 2;
  4007. }
  4008. if (transfer_prohibited) {
  4009. res = AST_BRIDGE_TRANSFER_NOT_PERMITTED;
  4010. goto end;
  4011. }
  4012. set_transfer_variables_all(to_transferee, channels, 1);
  4013. if (do_bridge_transfer) {
  4014. ast_bridge_lock(the_bridge);
  4015. res = attended_transfer_bridge(chan_bridged, chan_unbridged, the_bridge, NULL, transfer_msg);
  4016. ast_bridge_unlock(the_bridge);
  4017. goto end;
  4018. }
  4019. transferee = get_transferee(channels, chan_bridged);
  4020. if (!transferee) {
  4021. res = AST_BRIDGE_TRANSFER_FAIL;
  4022. goto end;
  4023. }
  4024. app = ast_strdupa(ast_channel_appl(chan_unbridged));
  4025. if (bridge_channel_internal_queue_attended_transfer(transferee, chan_unbridged)) {
  4026. res = AST_BRIDGE_TRANSFER_FAIL;
  4027. goto end;
  4028. }
  4029. ast_bridge_remove(the_bridge, chan_bridged);
  4030. ast_attended_transfer_message_add_app(transfer_msg, app, NULL);
  4031. res = AST_BRIDGE_TRANSFER_SUCCESS;
  4032. end:
  4033. transfer_msg->result = res;
  4034. ast_bridge_publish_attended_transfer(transfer_msg);
  4035. return res;
  4036. }
  4037. /*!
  4038. * \internal
  4039. * \brief Service the bridge manager request.
  4040. * \since 12.0.0
  4041. *
  4042. * \param bridge requesting service.
  4043. *
  4044. * \return Nothing
  4045. */
  4046. static void bridge_manager_service(struct ast_bridge *bridge)
  4047. {
  4048. ast_bridge_lock(bridge);
  4049. if (bridge->callid) {
  4050. ast_callid_threadassoc_change(bridge->callid);
  4051. }
  4052. /* Do any pending bridge actions. */
  4053. bridge_handle_actions(bridge);
  4054. ast_bridge_unlock(bridge);
  4055. }
  4056. /*!
  4057. * \internal
  4058. * \brief Bridge manager service thread.
  4059. * \since 12.0.0
  4060. *
  4061. * \return Nothing
  4062. */
  4063. static void *bridge_manager_thread(void *data)
  4064. {
  4065. struct bridge_manager_controller *manager = data;
  4066. struct bridge_manager_request *request;
  4067. ao2_lock(manager);
  4068. while (!manager->stop) {
  4069. request = AST_LIST_REMOVE_HEAD(&manager->service_requests, node);
  4070. if (!request) {
  4071. ast_cond_wait(&manager->cond, ao2_object_get_lockaddr(manager));
  4072. continue;
  4073. }
  4074. ao2_unlock(manager);
  4075. /* Service the bridge. */
  4076. bridge_manager_service(request->bridge);
  4077. ao2_ref(request->bridge, -1);
  4078. ast_free(request);
  4079. ao2_lock(manager);
  4080. }
  4081. ao2_unlock(manager);
  4082. return NULL;
  4083. }
  4084. /*!
  4085. * \internal
  4086. * \brief Destroy the bridge manager controller.
  4087. * \since 12.0.0
  4088. *
  4089. * \param obj Bridge manager to destroy.
  4090. *
  4091. * \return Nothing
  4092. */
  4093. static void bridge_manager_destroy(void *obj)
  4094. {
  4095. struct bridge_manager_controller *manager = obj;
  4096. struct bridge_manager_request *request;
  4097. if (manager->thread != AST_PTHREADT_NULL) {
  4098. /* Stop the manager thread. */
  4099. ao2_lock(manager);
  4100. manager->stop = 1;
  4101. ast_cond_signal(&manager->cond);
  4102. ao2_unlock(manager);
  4103. ast_debug(1, "Waiting for bridge manager thread to die.\n");
  4104. pthread_join(manager->thread, NULL);
  4105. }
  4106. /* Destroy the service request queue. */
  4107. while ((request = AST_LIST_REMOVE_HEAD(&manager->service_requests, node))) {
  4108. ao2_ref(request->bridge, -1);
  4109. ast_free(request);
  4110. }
  4111. ast_cond_destroy(&manager->cond);
  4112. }
  4113. /*!
  4114. * \internal
  4115. * \brief Create the bridge manager controller.
  4116. * \since 12.0.0
  4117. *
  4118. * \retval manager on success.
  4119. * \retval NULL on error.
  4120. */
  4121. static struct bridge_manager_controller *bridge_manager_create(void)
  4122. {
  4123. struct bridge_manager_controller *manager;
  4124. manager = ao2_alloc(sizeof(*manager), bridge_manager_destroy);
  4125. if (!manager) {
  4126. /* Well. This isn't good. */
  4127. return NULL;
  4128. }
  4129. ast_cond_init(&manager->cond, NULL);
  4130. AST_LIST_HEAD_INIT_NOLOCK(&manager->service_requests);
  4131. /* Create the bridge manager thread. */
  4132. if (ast_pthread_create(&manager->thread, NULL, bridge_manager_thread, manager)) {
  4133. /* Well. This isn't good either. */
  4134. manager->thread = AST_PTHREADT_NULL;
  4135. ao2_ref(manager, -1);
  4136. manager = NULL;
  4137. }
  4138. return manager;
  4139. }
  4140. /*!
  4141. * \internal
  4142. * \brief Bridge ao2 container sort function.
  4143. * \since 12.0.0
  4144. *
  4145. * \param obj_left pointer to the (user-defined part) of an object.
  4146. * \param obj_right pointer to the (user-defined part) of an object.
  4147. * \param flags flags from ao2_callback()
  4148. * OBJ_POINTER - if set, 'obj_right', is an object.
  4149. * OBJ_KEY - if set, 'obj_right', is a search key item that is not an object.
  4150. * OBJ_PARTIAL_KEY - if set, 'obj_right', is a partial search key item that is not an object.
  4151. *
  4152. * \retval <0 if obj_left < obj_right
  4153. * \retval =0 if obj_left == obj_right
  4154. * \retval >0 if obj_left > obj_right
  4155. */
  4156. static int bridge_sort_cmp(const void *obj_left, const void *obj_right, int flags)
  4157. {
  4158. const struct ast_bridge *bridge_left = obj_left;
  4159. const struct ast_bridge *bridge_right = obj_right;
  4160. const char *right_key = obj_right;
  4161. int cmp;
  4162. switch (flags & (OBJ_POINTER | OBJ_KEY | OBJ_PARTIAL_KEY)) {
  4163. default:
  4164. case OBJ_POINTER:
  4165. right_key = bridge_right->uniqueid;
  4166. /* Fall through */
  4167. case OBJ_KEY:
  4168. cmp = strcmp(bridge_left->uniqueid, right_key);
  4169. break;
  4170. case OBJ_PARTIAL_KEY:
  4171. cmp = strncmp(bridge_left->uniqueid, right_key, strlen(right_key));
  4172. break;
  4173. }
  4174. return cmp;
  4175. }
  4176. struct ast_bridge *ast_bridge_find_by_id(const char *bridge_id)
  4177. {
  4178. return ao2_find(bridges, bridge_id, OBJ_SEARCH_KEY);
  4179. }
  4180. struct bridge_complete {
  4181. /*! Nth match to return. */
  4182. int state;
  4183. /*! Which match currently on. */
  4184. int which;
  4185. };
  4186. static int complete_bridge_live_search(void *obj, void *arg, void *data, int flags)
  4187. {
  4188. struct bridge_complete *search = data;
  4189. if (++search->which > search->state) {
  4190. return CMP_MATCH;
  4191. }
  4192. return 0;
  4193. }
  4194. static char *complete_bridge_live(const char *word, int state)
  4195. {
  4196. char *ret;
  4197. struct ast_bridge *bridge;
  4198. struct bridge_complete search = {
  4199. .state = state,
  4200. };
  4201. bridge = ao2_callback_data(bridges, ast_strlen_zero(word) ? 0 : OBJ_PARTIAL_KEY,
  4202. complete_bridge_live_search, (char *) word, &search);
  4203. if (!bridge) {
  4204. return NULL;
  4205. }
  4206. ret = ast_strdup(bridge->uniqueid);
  4207. ao2_ref(bridge, -1);
  4208. return ret;
  4209. }
  4210. static char *complete_bridge_stasis(const char *word, int state)
  4211. {
  4212. char *ret = NULL;
  4213. int wordlen = strlen(word), which = 0;
  4214. RAII_VAR(struct ao2_container *, cached_bridges, NULL, ao2_cleanup);
  4215. struct ao2_iterator iter;
  4216. struct stasis_message *msg;
  4217. cached_bridges = stasis_cache_dump(ast_bridge_cache(), ast_bridge_snapshot_type());
  4218. if (!cached_bridges) {
  4219. return NULL;
  4220. }
  4221. iter = ao2_iterator_init(cached_bridges, 0);
  4222. for (; (msg = ao2_iterator_next(&iter)); ao2_ref(msg, -1)) {
  4223. struct ast_bridge_snapshot *snapshot = stasis_message_data(msg);
  4224. if (!strncasecmp(word, snapshot->uniqueid, wordlen) && (++which > state)) {
  4225. ret = ast_strdup(snapshot->uniqueid);
  4226. ao2_ref(msg, -1);
  4227. break;
  4228. }
  4229. }
  4230. ao2_iterator_destroy(&iter);
  4231. return ret;
  4232. }
  4233. static char *handle_bridge_show_all(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4234. {
  4235. #define FORMAT_HDR "%-36s %5s %-15s %s\n"
  4236. #define FORMAT_ROW "%-36s %5u %-15s %s\n"
  4237. RAII_VAR(struct ao2_container *, cached_bridges, NULL, ao2_cleanup);
  4238. struct ao2_iterator iter;
  4239. struct stasis_message *msg;
  4240. switch (cmd) {
  4241. case CLI_INIT:
  4242. e->command = "bridge show all";
  4243. e->usage =
  4244. "Usage: bridge show all\n"
  4245. " List all bridges\n";
  4246. return NULL;
  4247. case CLI_GENERATE:
  4248. return NULL;
  4249. }
  4250. cached_bridges = stasis_cache_dump(ast_bridge_cache(), ast_bridge_snapshot_type());
  4251. if (!cached_bridges) {
  4252. ast_cli(a->fd, "Failed to retrieve cached bridges\n");
  4253. return CLI_SUCCESS;
  4254. }
  4255. ast_cli(a->fd, FORMAT_HDR, "Bridge-ID", "Chans", "Type", "Technology");
  4256. iter = ao2_iterator_init(cached_bridges, 0);
  4257. for (; (msg = ao2_iterator_next(&iter)); ao2_ref(msg, -1)) {
  4258. struct ast_bridge_snapshot *snapshot = stasis_message_data(msg);
  4259. ast_cli(a->fd, FORMAT_ROW,
  4260. snapshot->uniqueid,
  4261. snapshot->num_channels,
  4262. S_OR(snapshot->subclass, "<unknown>"),
  4263. S_OR(snapshot->technology, "<unknown>"));
  4264. }
  4265. ao2_iterator_destroy(&iter);
  4266. return CLI_SUCCESS;
  4267. #undef FORMAT_HDR
  4268. #undef FORMAT_ROW
  4269. }
  4270. /*! \brief Internal callback function for sending channels in a bridge to the CLI */
  4271. static int bridge_show_specific_print_channel(void *obj, void *arg, int flags)
  4272. {
  4273. const char *uniqueid = obj;
  4274. struct ast_cli_args *a = arg;
  4275. RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
  4276. struct ast_channel_snapshot *snapshot;
  4277. msg = stasis_cache_get(ast_channel_cache(), ast_channel_snapshot_type(), uniqueid);
  4278. if (!msg) {
  4279. return 0;
  4280. }
  4281. snapshot = stasis_message_data(msg);
  4282. ast_cli(a->fd, "Channel: %s\n", snapshot->name);
  4283. return 0;
  4284. }
  4285. static char *handle_bridge_show_specific(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4286. {
  4287. RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
  4288. struct ast_bridge_snapshot *snapshot;
  4289. switch (cmd) {
  4290. case CLI_INIT:
  4291. e->command = "bridge show";
  4292. e->usage =
  4293. "Usage: bridge show <bridge-id>\n"
  4294. " Show information about the <bridge-id> bridge\n";
  4295. return NULL;
  4296. case CLI_GENERATE:
  4297. if (a->pos == 2) {
  4298. return complete_bridge_stasis(a->word, a->n);
  4299. }
  4300. return NULL;
  4301. }
  4302. if (a->argc != 3) {
  4303. return CLI_SHOWUSAGE;
  4304. }
  4305. msg = stasis_cache_get(ast_bridge_cache(), ast_bridge_snapshot_type(), a->argv[2]);
  4306. if (!msg) {
  4307. ast_cli(a->fd, "Bridge '%s' not found\n", a->argv[2]);
  4308. return CLI_SUCCESS;
  4309. }
  4310. snapshot = stasis_message_data(msg);
  4311. ast_cli(a->fd, "Id: %s\n", snapshot->uniqueid);
  4312. ast_cli(a->fd, "Type: %s\n", S_OR(snapshot->subclass, "<unknown>"));
  4313. ast_cli(a->fd, "Technology: %s\n", S_OR(snapshot->technology, "<unknown>"));
  4314. ast_cli(a->fd, "Num-Channels: %u\n", snapshot->num_channels);
  4315. ao2_callback(snapshot->channels, OBJ_NODATA, bridge_show_specific_print_channel, a);
  4316. return CLI_SUCCESS;
  4317. }
  4318. #ifdef AST_DEVMODE
  4319. static char *handle_bridge_destroy_specific(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4320. {
  4321. struct ast_bridge *bridge;
  4322. switch (cmd) {
  4323. case CLI_INIT:
  4324. e->command = "bridge destroy";
  4325. e->usage =
  4326. "Usage: bridge destroy <bridge-id>\n"
  4327. " Destroy the <bridge-id> bridge\n";
  4328. return NULL;
  4329. case CLI_GENERATE:
  4330. if (a->pos == 2) {
  4331. return complete_bridge_live(a->word, a->n);
  4332. }
  4333. return NULL;
  4334. }
  4335. if (a->argc != 3) {
  4336. return CLI_SHOWUSAGE;
  4337. }
  4338. bridge = ast_bridge_find_by_id(a->argv[2]);
  4339. if (!bridge) {
  4340. ast_cli(a->fd, "Bridge '%s' not found\n", a->argv[2]);
  4341. return CLI_SUCCESS;
  4342. }
  4343. ast_cli(a->fd, "Destroying bridge '%s'\n", a->argv[2]);
  4344. ast_bridge_destroy(bridge, 0);
  4345. return CLI_SUCCESS;
  4346. }
  4347. #endif
  4348. static char *complete_bridge_participant(const char *bridge_name, const char *line, const char *word, int pos, int state)
  4349. {
  4350. struct ast_bridge *bridge;
  4351. struct ast_bridge_channel *bridge_channel;
  4352. int which;
  4353. int wordlen;
  4354. bridge = ast_bridge_find_by_id(bridge_name);
  4355. if (!bridge) {
  4356. return NULL;
  4357. }
  4358. if (!state) {
  4359. ao2_ref(bridge, -1);
  4360. return ast_strdup("all");
  4361. }
  4362. state--;
  4363. {
  4364. SCOPED_LOCK(bridge_lock, bridge, ast_bridge_lock, ast_bridge_unlock);
  4365. which = 0;
  4366. wordlen = strlen(word);
  4367. AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
  4368. if (!strncasecmp(ast_channel_name(bridge_channel->chan), word, wordlen)
  4369. && ++which > state) {
  4370. ao2_ref(bridge, -1);
  4371. return ast_strdup(ast_channel_name(bridge_channel->chan));
  4372. }
  4373. }
  4374. }
  4375. ao2_ref(bridge, -1);
  4376. return NULL;
  4377. }
  4378. static char *handle_bridge_kick_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4379. {
  4380. struct ast_bridge *bridge;
  4381. switch (cmd) {
  4382. case CLI_INIT:
  4383. e->command = "bridge kick";
  4384. e->usage =
  4385. "Usage: bridge kick <bridge-id> <channel-name | all>\n"
  4386. " Kick the <channel-name> channel out of the <bridge-id> bridge\n"
  4387. " If all is specified as the channel name then all channels will be\n"
  4388. " kicked out of the bridge.\n";
  4389. return NULL;
  4390. case CLI_GENERATE:
  4391. if (a->pos == 2) {
  4392. return complete_bridge_live(a->word, a->n);
  4393. }
  4394. if (a->pos == 3) {
  4395. return complete_bridge_participant(a->argv[2], a->line, a->word, a->pos, a->n);
  4396. }
  4397. return NULL;
  4398. }
  4399. if (a->argc != 4) {
  4400. return CLI_SHOWUSAGE;
  4401. }
  4402. bridge = ast_bridge_find_by_id(a->argv[2]);
  4403. if (!bridge) {
  4404. ast_cli(a->fd, "Bridge '%s' not found\n", a->argv[2]);
  4405. return CLI_SUCCESS;
  4406. }
  4407. if (!strcasecmp(a->argv[3], "all")) {
  4408. struct ast_bridge_channel *bridge_channel;
  4409. ast_cli(a->fd, "Kicking all channels from bridge '%s'\n", a->argv[2]);
  4410. ast_bridge_lock(bridge);
  4411. AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
  4412. ast_bridge_channel_queue_callback(bridge_channel, 0, kick_it, NULL, 0);
  4413. }
  4414. ast_bridge_unlock(bridge);
  4415. } else {
  4416. struct ast_channel *chan;
  4417. chan = ast_channel_get_by_name_prefix(a->argv[3], strlen(a->argv[3]));
  4418. if (!chan) {
  4419. ast_cli(a->fd, "Channel '%s' not found\n", a->argv[3]);
  4420. ao2_ref(bridge, -1);
  4421. return CLI_SUCCESS;
  4422. }
  4423. ast_cli(a->fd, "Kicking channel '%s' from bridge '%s'\n",
  4424. ast_channel_name(chan), a->argv[2]);
  4425. ast_bridge_kick(bridge, chan);
  4426. ast_channel_unref(chan);
  4427. }
  4428. ao2_ref(bridge, -1);
  4429. return CLI_SUCCESS;
  4430. }
  4431. /*! Bridge technology capabilities to string. */
  4432. static const char *tech_capability2str(uint32_t capabilities)
  4433. {
  4434. const char *type;
  4435. if (capabilities & AST_BRIDGE_CAPABILITY_HOLDING) {
  4436. type = "Holding";
  4437. } else if (capabilities & AST_BRIDGE_CAPABILITY_EARLY) {
  4438. type = "Early";
  4439. } else if (capabilities & AST_BRIDGE_CAPABILITY_NATIVE) {
  4440. type = "Native";
  4441. } else if (capabilities & AST_BRIDGE_CAPABILITY_1TO1MIX) {
  4442. type = "1to1Mix";
  4443. } else if (capabilities & AST_BRIDGE_CAPABILITY_MULTIMIX) {
  4444. type = "MultiMix";
  4445. } else {
  4446. type = "<Unknown>";
  4447. }
  4448. return type;
  4449. }
  4450. static char *handle_bridge_technology_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4451. {
  4452. #define FORMAT_HDR "%-20s %-20s %8s %s\n"
  4453. #define FORMAT_ROW "%-20s %-20s %8u %s\n"
  4454. struct ast_bridge_technology *cur;
  4455. switch (cmd) {
  4456. case CLI_INIT:
  4457. e->command = "bridge technology show";
  4458. e->usage =
  4459. "Usage: bridge technology show\n"
  4460. " List registered bridge technologies\n";
  4461. return NULL;
  4462. case CLI_GENERATE:
  4463. return NULL;
  4464. }
  4465. ast_cli(a->fd, FORMAT_HDR, "Name", "Type", "Priority", "Suspended");
  4466. AST_RWLIST_RDLOCK(&bridge_technologies);
  4467. AST_RWLIST_TRAVERSE(&bridge_technologies, cur, entry) {
  4468. const char *type;
  4469. /* Decode type for display */
  4470. type = tech_capability2str(cur->capabilities);
  4471. ast_cli(a->fd, FORMAT_ROW, cur->name, type, cur->preference,
  4472. AST_CLI_YESNO(cur->suspended));
  4473. }
  4474. AST_RWLIST_UNLOCK(&bridge_technologies);
  4475. return CLI_SUCCESS;
  4476. #undef FORMAT
  4477. }
  4478. static char *complete_bridge_technology(const char *word, int state)
  4479. {
  4480. struct ast_bridge_technology *cur;
  4481. char *res;
  4482. int which;
  4483. int wordlen;
  4484. which = 0;
  4485. wordlen = strlen(word);
  4486. AST_RWLIST_RDLOCK(&bridge_technologies);
  4487. AST_RWLIST_TRAVERSE(&bridge_technologies, cur, entry) {
  4488. if (!strncasecmp(cur->name, word, wordlen) && ++which > state) {
  4489. res = ast_strdup(cur->name);
  4490. AST_RWLIST_UNLOCK(&bridge_technologies);
  4491. return res;
  4492. }
  4493. }
  4494. AST_RWLIST_UNLOCK(&bridge_technologies);
  4495. return NULL;
  4496. }
  4497. static char *handle_bridge_technology_suspend(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  4498. {
  4499. struct ast_bridge_technology *cur;
  4500. int suspend;
  4501. int successful;
  4502. switch (cmd) {
  4503. case CLI_INIT:
  4504. e->command = "bridge technology {suspend|unsuspend}";
  4505. e->usage =
  4506. "Usage: bridge technology {suspend|unsuspend} <technology-name>\n"
  4507. " Suspend or unsuspend a bridge technology.\n";
  4508. return NULL;
  4509. case CLI_GENERATE:
  4510. if (a->pos == 3) {
  4511. return complete_bridge_technology(a->word, a->n);
  4512. }
  4513. return NULL;
  4514. }
  4515. if (a->argc != 4) {
  4516. return CLI_SHOWUSAGE;
  4517. }
  4518. suspend = !strcasecmp(a->argv[2], "suspend");
  4519. successful = 0;
  4520. AST_RWLIST_WRLOCK(&bridge_technologies);
  4521. AST_RWLIST_TRAVERSE(&bridge_technologies, cur, entry) {
  4522. if (!strcasecmp(cur->name, a->argv[3])) {
  4523. successful = 1;
  4524. if (suspend) {
  4525. ast_bridge_technology_suspend(cur);
  4526. } else {
  4527. ast_bridge_technology_unsuspend(cur);
  4528. }
  4529. break;
  4530. }
  4531. }
  4532. AST_RWLIST_UNLOCK(&bridge_technologies);
  4533. if (successful) {
  4534. if (suspend) {
  4535. ast_cli(a->fd, "Suspended bridge technology '%s'\n", a->argv[3]);
  4536. } else {
  4537. ast_cli(a->fd, "Unsuspended bridge technology '%s'\n", a->argv[3]);
  4538. }
  4539. } else {
  4540. ast_cli(a->fd, "Bridge technology '%s' not found\n", a->argv[3]);
  4541. }
  4542. return CLI_SUCCESS;
  4543. }
  4544. static struct ast_cli_entry bridge_cli[] = {
  4545. AST_CLI_DEFINE(handle_bridge_show_all, "List all bridges"),
  4546. AST_CLI_DEFINE(handle_bridge_show_specific, "Show information about a bridge"),
  4547. #ifdef AST_DEVMODE
  4548. AST_CLI_DEFINE(handle_bridge_destroy_specific, "Destroy a bridge"),
  4549. #endif
  4550. AST_CLI_DEFINE(handle_bridge_kick_channel, "Kick a channel from a bridge"),
  4551. AST_CLI_DEFINE(handle_bridge_technology_show, "List registered bridge technologies"),
  4552. AST_CLI_DEFINE(handle_bridge_technology_suspend, "Suspend/unsuspend a bridge technology"),
  4553. };
  4554. static int handle_manager_bridge_tech_suspend(struct mansession *s, const struct message *m, int suspend)
  4555. {
  4556. const char *name = astman_get_header(m, "BridgeTechnology");
  4557. struct ast_bridge_technology *cur;
  4558. int successful = 0;
  4559. if (ast_strlen_zero(name)) {
  4560. astman_send_error(s, m, "BridgeTechnology must be provided");
  4561. return 0;
  4562. }
  4563. AST_RWLIST_RDLOCK(&bridge_technologies);
  4564. AST_RWLIST_TRAVERSE(&bridge_technologies, cur, entry) {
  4565. if (!strcasecmp(cur->name, name)) {
  4566. successful = 1;
  4567. if (suspend) {
  4568. ast_bridge_technology_suspend(cur);
  4569. } else {
  4570. ast_bridge_technology_unsuspend(cur);
  4571. }
  4572. break;
  4573. }
  4574. }
  4575. AST_RWLIST_UNLOCK(&bridge_technologies);
  4576. if (!successful) {
  4577. astman_send_error(s, m, "BridgeTechnology not found");
  4578. return 0;
  4579. }
  4580. astman_send_ack(s, m, (suspend ? "Suspended bridge technology" : "Unsuspended bridge technology"));
  4581. return 0;
  4582. }
  4583. static int manager_bridge_tech_suspend(struct mansession *s, const struct message *m)
  4584. {
  4585. return handle_manager_bridge_tech_suspend(s, m, 1);
  4586. }
  4587. static int manager_bridge_tech_unsuspend(struct mansession *s, const struct message *m)
  4588. {
  4589. return handle_manager_bridge_tech_suspend(s, m, 0);
  4590. }
  4591. static int manager_bridge_tech_list(struct mansession *s, const struct message *m)
  4592. {
  4593. const char *id = astman_get_header(m, "ActionID");
  4594. RAII_VAR(struct ast_str *, id_text, ast_str_create(128), ast_free);
  4595. struct ast_bridge_technology *cur;
  4596. int num_items = 0;
  4597. if (!id_text) {
  4598. astman_send_error(s, m, "Internal error");
  4599. return -1;
  4600. }
  4601. if (!ast_strlen_zero(id)) {
  4602. ast_str_set(&id_text, 0, "ActionID: %s\r\n", id);
  4603. }
  4604. astman_send_listack(s, m, "Bridge technology listing will follow", "start");
  4605. AST_RWLIST_RDLOCK(&bridge_technologies);
  4606. AST_RWLIST_TRAVERSE(&bridge_technologies, cur, entry) {
  4607. const char *type;
  4608. type = tech_capability2str(cur->capabilities);
  4609. astman_append(s,
  4610. "Event: BridgeTechnologyListItem\r\n"
  4611. "BridgeTechnology: %s\r\n"
  4612. "BridgeType: %s\r\n"
  4613. "BridgePriority: %u\r\n"
  4614. "BridgeSuspended: %s\r\n"
  4615. "%s"
  4616. "\r\n",
  4617. cur->name, type, cur->preference, AST_YESNO(cur->suspended),
  4618. ast_str_buffer(id_text));
  4619. ++num_items;
  4620. }
  4621. AST_RWLIST_UNLOCK(&bridge_technologies);
  4622. astman_send_list_complete_start(s, m, "BridgeTechnologyListComplete", num_items);
  4623. astman_send_list_complete_end(s);
  4624. return 0;
  4625. }
  4626. /*!
  4627. * \internal
  4628. * \brief Print bridge object key (name).
  4629. * \since 12.0.0
  4630. *
  4631. * \param v_obj A pointer to the object we want the key printed.
  4632. * \param where User data needed by prnt to determine where to put output.
  4633. * \param prnt Print output callback function to use.
  4634. *
  4635. * \return Nothing
  4636. */
  4637. static void bridge_prnt_obj(void *v_obj, void *where, ao2_prnt_fn *prnt)
  4638. {
  4639. struct ast_bridge *bridge = v_obj;
  4640. if (!bridge) {
  4641. return;
  4642. }
  4643. prnt(where, "%s %s chans:%u",
  4644. bridge->uniqueid, bridge->v_table->name, bridge->num_channels);
  4645. }
  4646. /*!
  4647. * \internal
  4648. * \brief Shutdown the bridging system. Stuff to always do.
  4649. * \since 12.0.0
  4650. *
  4651. * \return Nothing
  4652. */
  4653. static void bridge_shutdown(void)
  4654. {
  4655. ast_manager_unregister("BridgeTechnologyList");
  4656. ast_manager_unregister("BridgeTechnologySuspend");
  4657. ast_manager_unregister("BridgeTechnologyUnsuspend");
  4658. ast_cli_unregister_multiple(bridge_cli, ARRAY_LEN(bridge_cli));
  4659. ao2_container_unregister("bridges");
  4660. }
  4661. /*!
  4662. * \internal
  4663. * \brief Shutdown the bridging system. More stuff to do on graceful shutdown.
  4664. * \since 13.3.0
  4665. *
  4666. * \return Nothing
  4667. */
  4668. static void bridge_cleanup(void)
  4669. {
  4670. ao2_cleanup(bridges);
  4671. bridges = NULL;
  4672. ao2_cleanup(bridge_manager);
  4673. bridge_manager = NULL;
  4674. }
  4675. int ast_bridging_init(void)
  4676. {
  4677. ast_register_cleanup(bridge_cleanup);
  4678. ast_register_atexit(bridge_shutdown);
  4679. if (ast_stasis_bridging_init()) {
  4680. return -1;
  4681. }
  4682. bridge_manager = bridge_manager_create();
  4683. if (!bridge_manager) {
  4684. return -1;
  4685. }
  4686. bridges = ao2_container_alloc_rbtree(AO2_ALLOC_OPT_LOCK_MUTEX,
  4687. AO2_CONTAINER_ALLOC_OPT_DUPS_REPLACE, bridge_sort_cmp, NULL);
  4688. if (!bridges) {
  4689. return -1;
  4690. }
  4691. ao2_container_register("bridges", bridges, bridge_prnt_obj);
  4692. ast_bridging_init_basic();
  4693. ast_cli_register_multiple(bridge_cli, ARRAY_LEN(bridge_cli));
  4694. ast_manager_register_xml_core("BridgeTechnologyList", 0, manager_bridge_tech_list);
  4695. ast_manager_register_xml_core("BridgeTechnologySuspend", 0, manager_bridge_tech_suspend);
  4696. ast_manager_register_xml_core("BridgeTechnologyUnsuspend", 0, manager_bridge_tech_unsuspend);
  4697. return 0;
  4698. }