bridge.c 164 KB

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