bridge.c 161 KB

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