1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950 |
- /*
- * Asterisk -- An open source telephony toolkit.
- *
- * Copyright (C) 2013, Digium, Inc.
- *
- * Joshua Colp <jcolp@digium.com>
- *
- * See http://www.asterisk.org for more information about
- * the Asterisk project. Please do not directly contact
- * any of the maintainers of this project for assistance;
- * the project provides a web site, mailing lists and IRC
- * channels for your use.
- *
- * This program is free software, distributed under the terms of
- * the GNU General Public License Version 2. See the LICENSE file
- * at the top of the source tree.
- */
- /*! \file
- *
- * \author Joshua Colp <jcolp@digium.com>
- *
- * \brief PSJIP SIP Channel Driver
- *
- * \ingroup channel_drivers
- */
- /*** MODULEINFO
- <depend>pjproject</depend>
- <depend>res_pjsip</depend>
- <depend>res_pjsip_session</depend>
- <support_level>core</support_level>
- ***/
- #include "asterisk.h"
- #include <pjsip.h>
- #include <pjsip_ua.h>
- #include <pjlib.h>
- ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
- #include "asterisk/lock.h"
- #include "asterisk/channel.h"
- #include "asterisk/module.h"
- #include "asterisk/pbx.h"
- #include "asterisk/rtp_engine.h"
- #include "asterisk/acl.h"
- #include "asterisk/callerid.h"
- #include "asterisk/file.h"
- #include "asterisk/cli.h"
- #include "asterisk/app.h"
- #include "asterisk/musiconhold.h"
- #include "asterisk/causes.h"
- #include "asterisk/taskprocessor.h"
- #include "asterisk/dsp.h"
- #include "asterisk/stasis_endpoints.h"
- #include "asterisk/stasis_channels.h"
- #include "asterisk/indications.h"
- #include "asterisk/format_cache.h"
- #include "asterisk/translate.h"
- #include "asterisk/threadstorage.h"
- #include "asterisk/features_config.h"
- #include "asterisk/pickup.h"
- #include "asterisk/test.h"
- #include "asterisk/message.h"
- #include "asterisk/res_pjsip.h"
- #include "asterisk/res_pjsip_session.h"
- #include "pjsip/include/chan_pjsip.h"
- #include "pjsip/include/dialplan_functions.h"
- #include "pjsip/include/cli_functions.h"
- AST_THREADSTORAGE(uniqueid_threadbuf);
- #define UNIQUEID_BUFSIZE 256
- static const char channel_type[] = "PJSIP";
- static unsigned int chan_idx;
- static void chan_pjsip_pvt_dtor(void *obj)
- {
- struct chan_pjsip_pvt *pvt = obj;
- int i;
- for (i = 0; i < SIP_MEDIA_SIZE; ++i) {
- ao2_cleanup(pvt->media[i]);
- pvt->media[i] = NULL;
- }
- }
- /* \brief Asterisk core interaction functions */
- static struct ast_channel *chan_pjsip_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause);
- static int chan_pjsip_sendtext_data(struct ast_channel *ast, struct ast_msg_data *msg);
- static int chan_pjsip_sendtext(struct ast_channel *ast, const char *text);
- static int chan_pjsip_digit_begin(struct ast_channel *ast, char digit);
- static int chan_pjsip_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
- static int chan_pjsip_call(struct ast_channel *ast, const char *dest, int timeout);
- static int chan_pjsip_hangup(struct ast_channel *ast);
- static int chan_pjsip_answer(struct ast_channel *ast);
- static struct ast_frame *chan_pjsip_read(struct ast_channel *ast);
- static int chan_pjsip_write(struct ast_channel *ast, struct ast_frame *f);
- static int chan_pjsip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
- static int chan_pjsip_transfer(struct ast_channel *ast, const char *target);
- static int chan_pjsip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
- static int chan_pjsip_devicestate(const char *data);
- static int chan_pjsip_queryoption(struct ast_channel *ast, int option, void *data, int *datalen);
- static const char *chan_pjsip_get_uniqueid(struct ast_channel *ast);
- /*! \brief PBX interface structure for channel registration */
- struct ast_channel_tech chan_pjsip_tech = {
- .type = channel_type,
- .description = "PJSIP Channel Driver",
- .requester = chan_pjsip_request,
- .send_text = chan_pjsip_sendtext,
- .send_text_data = chan_pjsip_sendtext_data,
- .send_digit_begin = chan_pjsip_digit_begin,
- .send_digit_end = chan_pjsip_digit_end,
- .call = chan_pjsip_call,
- .hangup = chan_pjsip_hangup,
- .answer = chan_pjsip_answer,
- .read = chan_pjsip_read,
- .write = chan_pjsip_write,
- .write_video = chan_pjsip_write,
- .exception = chan_pjsip_read,
- .indicate = chan_pjsip_indicate,
- .transfer = chan_pjsip_transfer,
- .fixup = chan_pjsip_fixup,
- .devicestate = chan_pjsip_devicestate,
- .queryoption = chan_pjsip_queryoption,
- .func_channel_read = pjsip_acf_channel_read,
- .get_pvt_uniqueid = chan_pjsip_get_uniqueid,
- .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER | AST_CHAN_TP_SEND_TEXT_DATA
- };
- /*! \brief SIP session interaction functions */
- static void chan_pjsip_session_begin(struct ast_sip_session *session);
- static void chan_pjsip_session_end(struct ast_sip_session *session);
- static int chan_pjsip_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
- static void chan_pjsip_incoming_response(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
- /*! \brief SIP session supplement structure */
- static struct ast_sip_session_supplement chan_pjsip_supplement = {
- .method = "INVITE",
- .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL,
- .session_begin = chan_pjsip_session_begin,
- .session_end = chan_pjsip_session_end,
- .incoming_request = chan_pjsip_incoming_request,
- /* It is important that this supplement runs after media has been negotiated */
- .response_priority = AST_SIP_SESSION_AFTER_MEDIA,
- };
- /*! \brief SIP session supplement structure just for responses */
- static struct ast_sip_session_supplement chan_pjsip_supplement_response = {
- .method = "INVITE",
- .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL,
- .incoming_response = chan_pjsip_incoming_response,
- .response_priority = AST_SIP_SESSION_BEFORE_MEDIA | AST_SIP_SESSION_AFTER_MEDIA,
- };
- static int chan_pjsip_incoming_ack(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
- static struct ast_sip_session_supplement chan_pjsip_ack_supplement = {
- .method = "ACK",
- .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL,
- .incoming_request = chan_pjsip_incoming_ack,
- };
- /*! \brief Function called by RTP engine to get local audio RTP peer */
- static enum ast_rtp_glue_result chan_pjsip_get_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
- {
- struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
- struct chan_pjsip_pvt *pvt;
- struct ast_sip_endpoint *endpoint;
- struct ast_datastore *datastore;
- if (!channel || !channel->session || !(pvt = channel->pvt) || !pvt->media[SIP_MEDIA_AUDIO]->rtp) {
- return AST_RTP_GLUE_RESULT_FORBID;
- }
- datastore = ast_sip_session_get_datastore(channel->session, "t38");
- if (datastore) {
- ao2_ref(datastore, -1);
- return AST_RTP_GLUE_RESULT_FORBID;
- }
- endpoint = channel->session->endpoint;
- *instance = pvt->media[SIP_MEDIA_AUDIO]->rtp;
- ao2_ref(*instance, +1);
- ast_assert(endpoint != NULL);
- if (endpoint->media.rtp.encryption != AST_SIP_MEDIA_ENCRYPT_NONE) {
- return AST_RTP_GLUE_RESULT_FORBID;
- }
- if (endpoint->media.direct_media.enabled) {
- return AST_RTP_GLUE_RESULT_REMOTE;
- }
- return AST_RTP_GLUE_RESULT_LOCAL;
- }
- /*! \brief Function called by RTP engine to get local video RTP peer */
- static enum ast_rtp_glue_result chan_pjsip_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
- {
- struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
- struct chan_pjsip_pvt *pvt = channel->pvt;
- struct ast_sip_endpoint *endpoint;
- if (!pvt || !channel->session || !pvt->media[SIP_MEDIA_VIDEO]->rtp) {
- return AST_RTP_GLUE_RESULT_FORBID;
- }
- endpoint = channel->session->endpoint;
- *instance = pvt->media[SIP_MEDIA_VIDEO]->rtp;
- ao2_ref(*instance, +1);
- ast_assert(endpoint != NULL);
- if (endpoint->media.rtp.encryption != AST_SIP_MEDIA_ENCRYPT_NONE) {
- return AST_RTP_GLUE_RESULT_FORBID;
- }
- return AST_RTP_GLUE_RESULT_LOCAL;
- }
- /*! \brief Function called by RTP engine to get peer capabilities */
- static void chan_pjsip_get_codec(struct ast_channel *chan, struct ast_format_cap *result)
- {
- ast_format_cap_append_from_cap(result, ast_channel_nativeformats(chan), AST_MEDIA_TYPE_UNKNOWN);
- }
- /*! \brief Destructor function for \ref transport_info_data */
- static void transport_info_destroy(void *obj)
- {
- struct transport_info_data *data = obj;
- ast_free(data);
- }
- /*! \brief Datastore used to store local/remote addresses for the
- * INVITE request that created the PJSIP channel */
- static struct ast_datastore_info transport_info = {
- .type = "chan_pjsip_transport_info",
- .destroy = transport_info_destroy,
- };
- static struct ast_datastore_info direct_media_mitigation_info = { };
- static int direct_media_mitigate_glare(struct ast_sip_session *session)
- {
- RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
- if (session->endpoint->media.direct_media.glare_mitigation ==
- AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_NONE) {
- return 0;
- }
- datastore = ast_sip_session_get_datastore(session, "direct_media_glare_mitigation");
- if (!datastore) {
- return 0;
- }
- /* Removing the datastore ensures we won't try to mitigate glare on subsequent reinvites */
- ast_sip_session_remove_datastore(session, "direct_media_glare_mitigation");
- if ((session->endpoint->media.direct_media.glare_mitigation ==
- AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_OUTGOING &&
- session->inv_session->role == PJSIP_ROLE_UAC) ||
- (session->endpoint->media.direct_media.glare_mitigation ==
- AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_INCOMING &&
- session->inv_session->role == PJSIP_ROLE_UAS)) {
- return 1;
- }
- return 0;
- }
- /*!
- * \pre chan is locked
- */
- static int check_for_rtp_changes(struct ast_channel *chan, struct ast_rtp_instance *rtp,
- struct ast_sip_session_media *media, int rtcp_fd)
- {
- int changed = 0;
- if (rtp) {
- changed = ast_rtp_instance_get_and_cmp_remote_address(rtp, &media->direct_media_addr);
- if (media->rtp) {
- ast_channel_set_fd(chan, rtcp_fd, -1);
- ast_rtp_instance_set_prop(media->rtp, AST_RTP_PROPERTY_RTCP, 0);
- }
- } else if (!ast_sockaddr_isnull(&media->direct_media_addr)){
- ast_sockaddr_setnull(&media->direct_media_addr);
- changed = 1;
- if (media->rtp) {
- ast_rtp_instance_set_prop(media->rtp, AST_RTP_PROPERTY_RTCP, 1);
- ast_channel_set_fd(chan, rtcp_fd, ast_rtp_instance_fd(media->rtp, 1));
- }
- }
- return changed;
- }
- struct rtp_direct_media_data {
- struct ast_channel *chan;
- struct ast_rtp_instance *rtp;
- struct ast_rtp_instance *vrtp;
- struct ast_format_cap *cap;
- struct ast_sip_session *session;
- };
- static void rtp_direct_media_data_destroy(void *data)
- {
- struct rtp_direct_media_data *cdata = data;
- ao2_cleanup(cdata->session);
- ao2_cleanup(cdata->cap);
- ao2_cleanup(cdata->vrtp);
- ao2_cleanup(cdata->rtp);
- ao2_cleanup(cdata->chan);
- }
- static struct rtp_direct_media_data *rtp_direct_media_data_create(
- struct ast_channel *chan, struct ast_rtp_instance *rtp, struct ast_rtp_instance *vrtp,
- const struct ast_format_cap *cap, struct ast_sip_session *session)
- {
- struct rtp_direct_media_data *cdata = ao2_alloc(sizeof(*cdata), rtp_direct_media_data_destroy);
- if (!cdata) {
- return NULL;
- }
- cdata->chan = ao2_bump(chan);
- cdata->rtp = ao2_bump(rtp);
- cdata->vrtp = ao2_bump(vrtp);
- cdata->cap = ao2_bump((struct ast_format_cap *)cap);
- cdata->session = ao2_bump(session);
- return cdata;
- }
- static int send_direct_media_request(void *data)
- {
- struct rtp_direct_media_data *cdata = data;
- struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(cdata->chan);
- struct chan_pjsip_pvt *pvt = channel->pvt;
- int changed = 0;
- int res = 0;
- /* The channel needs to be locked when checking for RTP changes.
- * Otherwise, we could end up destroying an underlying RTCP structure
- * at the same time that the channel thread is attempting to read RTCP
- */
- ast_channel_lock(cdata->chan);
- if (pvt->media[SIP_MEDIA_AUDIO]) {
- changed |= check_for_rtp_changes(
- cdata->chan, cdata->rtp, pvt->media[SIP_MEDIA_AUDIO], 1);
- }
- if (pvt->media[SIP_MEDIA_VIDEO]) {
- changed |= check_for_rtp_changes(
- cdata->chan, cdata->vrtp, pvt->media[SIP_MEDIA_VIDEO], 3);
- }
- ast_channel_unlock(cdata->chan);
- if (direct_media_mitigate_glare(cdata->session)) {
- ast_debug(4, "Disregarding setting RTP on %s: mitigating re-INVITE glare\n", ast_channel_name(cdata->chan));
- ao2_ref(cdata, -1);
- return 0;
- }
- if (cdata->cap && ast_format_cap_count(cdata->cap) &&
- !ast_format_cap_identical(cdata->session->direct_media_cap, cdata->cap)) {
- ast_format_cap_remove_by_type(cdata->session->direct_media_cap, AST_MEDIA_TYPE_UNKNOWN);
- ast_format_cap_append_from_cap(cdata->session->direct_media_cap, cdata->cap, AST_MEDIA_TYPE_UNKNOWN);
- changed = 1;
- }
- if (changed) {
- ast_debug(4, "RTP changed on %s; initiating direct media update\n", ast_channel_name(cdata->chan));
- res = ast_sip_session_refresh(cdata->session, NULL, NULL, NULL,
- cdata->session->endpoint->media.direct_media.method, 1);
- }
- ao2_ref(cdata, -1);
- return res;
- }
- /*! \brief Function called by RTP engine to change where the remote party should send media */
- static int chan_pjsip_set_rtp_peer(struct ast_channel *chan,
- struct ast_rtp_instance *rtp,
- struct ast_rtp_instance *vrtp,
- struct ast_rtp_instance *tpeer,
- const struct ast_format_cap *cap,
- int nat_active)
- {
- struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
- struct ast_sip_session *session = channel->session;
- struct rtp_direct_media_data *cdata;
- /* Don't try to do any direct media shenanigans on early bridges */
- if ((rtp || vrtp || tpeer) && !ast_channel_is_bridged(chan)) {
- ast_debug(4, "Disregarding setting RTP on %s: channel is not bridged\n", ast_channel_name(chan));
- return 0;
- }
- if (nat_active && session->endpoint->media.direct_media.disable_on_nat) {
- ast_debug(4, "Disregarding setting RTP on %s: NAT is active\n", ast_channel_name(chan));
- return 0;
- }
- cdata = rtp_direct_media_data_create(chan, rtp, vrtp, cap, session);
- if (!cdata) {
- return 0;
- }
- if (ast_sip_push_task(session->serializer, send_direct_media_request, cdata)) {
- ast_log(LOG_ERROR, "Unable to send direct media request for channel %s\n", ast_channel_name(chan));
- ao2_ref(cdata, -1);
- }
- return 0;
- }
- /*! \brief Local glue for interacting with the RTP engine core */
- static struct ast_rtp_glue chan_pjsip_rtp_glue = {
- .type = "PJSIP",
- .get_rtp_info = chan_pjsip_get_rtp_peer,
- .get_vrtp_info = chan_pjsip_get_vrtp_peer,
- .get_codec = chan_pjsip_get_codec,
- .update_peer = chan_pjsip_set_rtp_peer,
- };
- static void set_channel_on_rtp_instance(struct chan_pjsip_pvt *pvt, const char *channel_id)
- {
- if (pvt->media[SIP_MEDIA_AUDIO] && pvt->media[SIP_MEDIA_AUDIO]->rtp) {
- ast_rtp_instance_set_channel_id(pvt->media[SIP_MEDIA_AUDIO]->rtp, channel_id);
- }
- if (pvt->media[SIP_MEDIA_VIDEO] && pvt->media[SIP_MEDIA_VIDEO]->rtp) {
- ast_rtp_instance_set_channel_id(pvt->media[SIP_MEDIA_VIDEO]->rtp, channel_id);
- }
- }
- /*! \brief Function called to create a new PJSIP Asterisk channel */
- static struct ast_channel *chan_pjsip_new(struct ast_sip_session *session, int state, const char *exten, const char *title, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *cid_name)
- {
- struct ast_channel *chan;
- struct ast_format_cap *caps;
- RAII_VAR(struct chan_pjsip_pvt *, pvt, NULL, ao2_cleanup);
- struct ast_sip_channel_pvt *channel;
- struct ast_variable *var;
- if (!(pvt = ao2_alloc(sizeof(*pvt), chan_pjsip_pvt_dtor))) {
- return NULL;
- }
- caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- if (!caps) {
- return NULL;
- }
- chan = ast_channel_alloc_with_endpoint(1, state,
- S_COR(session->id.number.valid, session->id.number.str, ""),
- S_COR(session->id.name.valid, session->id.name.str, ""),
- session->endpoint->accountcode,
- exten, session->endpoint->context,
- assignedids, requestor, 0,
- session->endpoint->persistent, "PJSIP/%s-%08x",
- ast_sorcery_object_get_id(session->endpoint),
- (unsigned) ast_atomic_fetchadd_int((int *) &chan_idx, +1));
- if (!chan) {
- ao2_ref(caps, -1);
- return NULL;
- }
- ast_channel_tech_set(chan, &chan_pjsip_tech);
- if (!(channel = ast_sip_channel_pvt_alloc(pvt, session))) {
- ao2_ref(caps, -1);
- ast_channel_unlock(chan);
- ast_hangup(chan);
- return NULL;
- }
- ast_channel_stage_snapshot(chan);
- ast_channel_tech_pvt_set(chan, channel);
- if (!ast_format_cap_count(session->req_caps) ||
- !ast_format_cap_iscompatible(session->req_caps, session->endpoint->media.codecs)) {
- ast_format_cap_append_from_cap(caps, session->endpoint->media.codecs, AST_MEDIA_TYPE_UNKNOWN);
- } else {
- ast_format_cap_append_from_cap(caps, session->req_caps, AST_MEDIA_TYPE_UNKNOWN);
- }
- ast_channel_nativeformats_set(chan, caps);
- if (!ast_format_cap_empty(caps)) {
- struct ast_format *fmt;
- fmt = ast_format_cap_get_best_by_type(caps, AST_MEDIA_TYPE_AUDIO);
- if (!fmt) {
- /* Since our capabilities aren't empty, this will succeed */
- fmt = ast_format_cap_get_format(caps, 0);
- }
- ast_channel_set_writeformat(chan, fmt);
- ast_channel_set_rawwriteformat(chan, fmt);
- ast_channel_set_readformat(chan, fmt);
- ast_channel_set_rawreadformat(chan, fmt);
- ao2_ref(fmt, -1);
- }
- ao2_ref(caps, -1);
- if (state == AST_STATE_RING) {
- ast_channel_rings_set(chan, 1);
- }
- ast_channel_adsicpe_set(chan, AST_ADSI_UNAVAILABLE);
- ast_party_id_copy(&ast_channel_caller(chan)->id, &session->id);
- ast_party_id_copy(&ast_channel_caller(chan)->ani, &session->id);
- if (!ast_strlen_zero(exten)) {
- /* Set provided DNID on the new channel. */
- ast_channel_dialed(chan)->number.str = ast_strdup(exten);
- }
- ast_channel_priority_set(chan, 1);
- ast_channel_callgroup_set(chan, session->endpoint->pickup.callgroup);
- ast_channel_pickupgroup_set(chan, session->endpoint->pickup.pickupgroup);
- ast_channel_named_callgroups_set(chan, session->endpoint->pickup.named_callgroups);
- ast_channel_named_pickupgroups_set(chan, session->endpoint->pickup.named_pickupgroups);
- if (!ast_strlen_zero(session->endpoint->language)) {
- ast_channel_language_set(chan, session->endpoint->language);
- }
- if (!ast_strlen_zero(session->endpoint->zone)) {
- struct ast_tone_zone *zone = ast_get_indication_zone(session->endpoint->zone);
- if (!zone) {
- ast_log(LOG_ERROR, "Unknown country code '%s' for tonezone. Check indications.conf for available country codes.\n", session->endpoint->zone);
- }
- ast_channel_zone_set(chan, zone);
- }
- for (var = session->endpoint->channel_vars; var; var = var->next) {
- char buf[512];
- pbx_builtin_setvar_helper(chan, var->name, ast_get_encoded_str(
- var->value, buf, sizeof(buf)));
- }
- ast_channel_stage_snapshot_done(chan);
- ast_channel_unlock(chan);
- /* If res_pjsip_session is ever updated to create/destroy ast_sip_session_media
- * during a call such as if multiple same-type stream support is introduced,
- * these will need to be recaptured as well */
- pvt->media[SIP_MEDIA_AUDIO] = ao2_find(session->media, "audio", OBJ_KEY);
- pvt->media[SIP_MEDIA_VIDEO] = ao2_find(session->media, "video", OBJ_KEY);
- set_channel_on_rtp_instance(pvt, ast_channel_uniqueid(chan));
- return chan;
- }
- static int answer(void *data)
- {
- pj_status_t status = PJ_SUCCESS;
- pjsip_tx_data *packet = NULL;
- struct ast_sip_session *session = data;
- if (session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
- ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
- session->inv_session->cause,
- pjsip_get_status_text(session->inv_session->cause)->ptr);
- #ifdef HAVE_PJSIP_INV_SESSION_REF
- pjsip_inv_dec_ref(session->inv_session);
- #endif
- return 0;
- }
- pjsip_dlg_inc_lock(session->inv_session->dlg);
- if (session->inv_session->invite_tsx) {
- status = pjsip_inv_answer(session->inv_session, 200, NULL, NULL, &packet);
- } else {
- ast_log(LOG_ERROR,"Cannot answer '%s' because there is no associated SIP transaction\n",
- ast_channel_name(session->channel));
- }
- pjsip_dlg_dec_lock(session->inv_session->dlg);
- if (status == PJ_SUCCESS && packet) {
- ast_sip_session_send_response(session, packet);
- }
- #ifdef HAVE_PJSIP_INV_SESSION_REF
- pjsip_inv_dec_ref(session->inv_session);
- #endif
- if (status != PJ_SUCCESS) {
- char err[PJ_ERR_MSG_SIZE];
- pj_strerror(status, err, sizeof(err));
- ast_log(LOG_WARNING,"Cannot answer '%s': %s\n",
- ast_channel_name(session->channel), err);
- /*
- * Return this value so we can distinguish between this
- * failure and the threadpool synchronous push failing.
- */
- return -2;
- }
- return 0;
- }
- /*! \brief Function called by core when we should answer a PJSIP session */
- static int chan_pjsip_answer(struct ast_channel *ast)
- {
- struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
- struct ast_sip_session *session;
- int res;
- if (ast_channel_state(ast) == AST_STATE_UP) {
- return 0;
- }
- ast_setstate(ast, AST_STATE_UP);
- session = ao2_bump(channel->session);
- #ifdef HAVE_PJSIP_INV_SESSION_REF
- if (pjsip_inv_add_ref(session->inv_session) != PJ_SUCCESS) {
- ast_log(LOG_ERROR, "Can't increase the session reference counter\n");
- ao2_ref(session, -1);
- return -1;
- }
- #endif
- /* the answer task needs to be pushed synchronously otherwise a race condition
- can occur between this thread and bridging (specifically when native bridging
- attempts to do direct media) */
- ast_channel_unlock(ast);
- res = ast_sip_push_task_wait_serializer(session->serializer, answer, session);
- if (res) {
- if (res == -1) {
- ast_log(LOG_ERROR,"Cannot answer '%s': Unable to push answer task to the threadpool.\n",
- ast_channel_name(session->channel));
- #ifdef HAVE_PJSIP_INV_SESSION_REF
- pjsip_inv_dec_ref(session->inv_session);
- #endif
- }
- ao2_ref(session, -1);
- ast_channel_lock(ast);
- return -1;
- }
- ao2_ref(session, -1);
- ast_channel_lock(ast);
- return 0;
- }
- /*! \brief Internal helper function called when CNG tone is detected */
- static struct ast_frame *chan_pjsip_cng_tone_detected(struct ast_sip_session *session, struct ast_frame *f)
- {
- const char *target_context;
- int exists;
- int dsp_features;
- dsp_features = ast_dsp_get_features(session->dsp);
- dsp_features &= ~DSP_FEATURE_FAX_DETECT;
- if (dsp_features) {
- ast_dsp_set_features(session->dsp, dsp_features);
- } else {
- ast_dsp_free(session->dsp);
- session->dsp = NULL;
- }
- /* If already executing in the fax extension don't do anything */
- if (!strcmp(ast_channel_exten(session->channel), "fax")) {
- return f;
- }
- target_context = S_OR(ast_channel_macrocontext(session->channel), ast_channel_context(session->channel));
- /*
- * We need to unlock the channel here because ast_exists_extension has the
- * potential to start and stop an autoservice on the channel. Such action
- * is prone to deadlock if the channel is locked.
- *
- * ast_async_goto() has its own restriction on not holding the channel lock.
- */
- ast_channel_unlock(session->channel);
- ast_frfree(f);
- f = &ast_null_frame;
- exists = ast_exists_extension(session->channel, target_context, "fax", 1,
- S_COR(ast_channel_caller(session->channel)->id.number.valid,
- ast_channel_caller(session->channel)->id.number.str, NULL));
- if (exists) {
- ast_verb(2, "Redirecting '%s' to fax extension due to CNG detection\n",
- ast_channel_name(session->channel));
- pbx_builtin_setvar_helper(session->channel, "FAXEXTEN", ast_channel_exten(session->channel));
- if (ast_async_goto(session->channel, target_context, "fax", 1)) {
- ast_log(LOG_ERROR, "Failed to async goto '%s' into fax extension in '%s'\n",
- ast_channel_name(session->channel), target_context);
- }
- } else {
- ast_log(LOG_NOTICE, "FAX CNG detected on '%s' but no fax extension in '%s'\n",
- ast_channel_name(session->channel), target_context);
- }
- ast_channel_lock(session->channel);
- return f;
- }
- /*!
- * \brief Function called by core to read any waiting frames
- *
- * \note The channel is already locked.
- */
- static struct ast_frame *chan_pjsip_read(struct ast_channel *ast)
- {
- struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
- struct ast_sip_session *session;
- struct chan_pjsip_pvt *pvt = channel->pvt;
- struct ast_frame *f;
- struct ast_sip_session_media *media = NULL;
- int rtcp = 0;
- int fdno = ast_channel_fdno(ast);
- switch (fdno) {
- case 0:
- media = pvt->media[SIP_MEDIA_AUDIO];
- break;
- case 1:
- media = pvt->media[SIP_MEDIA_AUDIO];
- rtcp = 1;
- break;
- case 2:
- media = pvt->media[SIP_MEDIA_VIDEO];
- break;
- case 3:
- media = pvt->media[SIP_MEDIA_VIDEO];
- rtcp = 1;
- break;
- }
- if (!media || !media->rtp) {
- return &ast_null_frame;
- }
- if (!(f = ast_rtp_instance_read(media->rtp, rtcp))) {
- return f;
- }
- ast_rtp_instance_set_last_rx(media->rtp, time(NULL));
- if (f->frametype != AST_FRAME_VOICE) {
- return f;
- }
- session = channel->session;
- /*
- * Asymmetric RTP only has one native format set at a time.
- * Therefore we need to update the native format to the current
- * raw read format BEFORE the native format check
- */
- if (!session->endpoint->asymmetric_rtp_codec &&
- ast_format_cmp(ast_channel_rawwriteformat(ast), f->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
- struct ast_format_cap *caps;
- /* For maximum compatibility we ensure that the formats match that of the received media */
- ast_debug(1, "Oooh, got a frame with format of %s on channel '%s' when we're sending '%s', switching to match\n",
- ast_format_get_name(f->subclass.format), ast_channel_name(ast),
- ast_format_get_name(ast_channel_rawwriteformat(ast)));
- caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- if (caps) {
- ast_format_cap_append_from_cap(caps, ast_channel_nativeformats(ast), AST_MEDIA_TYPE_UNKNOWN);
- ast_format_cap_remove_by_type(caps, AST_MEDIA_TYPE_AUDIO);
- ast_format_cap_append(caps, f->subclass.format, 0);
- ast_channel_nativeformats_set(ast, caps);
- ao2_ref(caps, -1);
- }
- ast_set_write_format_path(ast, ast_channel_writeformat(ast), f->subclass.format);
- ast_set_read_format_path(ast, ast_channel_readformat(ast), f->subclass.format);
- if (ast_channel_is_bridged(ast)) {
- ast_channel_set_unbridged_nolock(ast, 1);
- }
- }
- if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), f->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
- ast_debug(1, "Oooh, got a frame with format of %s on channel '%s' when it has not been negotiated\n",
- ast_format_get_name(f->subclass.format), ast_channel_name(ast));
- ast_frfree(f);
- return &ast_null_frame;
- }
- if (session->dsp) {
- int dsp_features;
- dsp_features = ast_dsp_get_features(session->dsp);
- if ((dsp_features & DSP_FEATURE_FAX_DETECT)
- && session->endpoint->faxdetect_timeout
- && session->endpoint->faxdetect_timeout <= ast_channel_get_up_time(ast)) {
- dsp_features &= ~DSP_FEATURE_FAX_DETECT;
- if (dsp_features) {
- ast_dsp_set_features(session->dsp, dsp_features);
- } else {
- ast_dsp_free(session->dsp);
- session->dsp = NULL;
- }
- ast_debug(3, "Channel driver fax CNG detection timeout on %s\n",
- ast_channel_name(ast));
- }
- }
- if (session->dsp) {
- f = ast_dsp_process(ast, session->dsp, f);
- if (f && (f->frametype == AST_FRAME_DTMF)) {
- if (f->subclass.integer == 'f') {
- ast_debug(3, "Channel driver fax CNG detected on %s\n",
- ast_channel_name(ast));
- f = chan_pjsip_cng_tone_detected(session, f);
- } else {
- ast_debug(3, "* Detected inband DTMF '%c' on '%s'\n", f->subclass.integer,
- ast_channel_name(ast));
- }
- }
- }
- return f;
- }
- /*! \brief Function called by core to write frames */
- static int chan_pjsip_write(struct ast_channel *ast, struct ast_frame *frame)
- {
- struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
- struct chan_pjsip_pvt *pvt = channel->pvt;
- struct ast_sip_session_media *media;
- int res = 0;
- switch (frame->frametype) {
- case AST_FRAME_VOICE:
- media = pvt->media[SIP_MEDIA_AUDIO];
- if (!media) {
- return 0;
- }
- if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), frame->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
- struct ast_str *cap_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
- struct ast_str *write_transpath = ast_str_alloca(256);
- struct ast_str *read_transpath = ast_str_alloca(256);
- ast_log(LOG_WARNING,
- "Channel %s asked to send %s frame when native formats are %s (rd:%s->%s;%s wr:%s->%s;%s)\n",
- ast_channel_name(ast),
- ast_format_get_name(frame->subclass.format),
- ast_format_cap_get_names(ast_channel_nativeformats(ast), &cap_buf),
- ast_format_get_name(ast_channel_rawreadformat(ast)),
- ast_format_get_name(ast_channel_readformat(ast)),
- ast_translate_path_to_str(ast_channel_readtrans(ast), &read_transpath),
- ast_format_get_name(ast_channel_writeformat(ast)),
- ast_format_get_name(ast_channel_rawwriteformat(ast)),
- ast_translate_path_to_str(ast_channel_writetrans(ast), &write_transpath));
- return 0;
- }
- if (media->rtp) {
- res = ast_rtp_instance_write(media->rtp, frame);
- }
- break;
- case AST_FRAME_VIDEO:
- if ((media = pvt->media[SIP_MEDIA_VIDEO]) && media->rtp) {
- res = ast_rtp_instance_write(media->rtp, frame);
- }
- break;
- case AST_FRAME_MODEM:
- break;
- case AST_FRAME_CNG:
- break;
- default:
- ast_log(LOG_WARNING, "Can't send %u type frames with PJSIP\n", frame->frametype);
- break;
- }
- return res;
- }
- /*! \brief Function called by core to change the underlying owner channel */
- static int chan_pjsip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
- {
- struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(newchan);
- struct chan_pjsip_pvt *pvt = channel->pvt;
- if (channel->session->channel != oldchan) {
- return -1;
- }
- /*
- * The masquerade has suspended the channel's session
- * serializer so we can safely change it outside of
- * the serializer thread.
- */
- channel->session->channel = newchan;
- set_channel_on_rtp_instance(pvt, ast_channel_uniqueid(newchan));
- return 0;
- }
- /*! AO2 hash function for on hold UIDs */
- static int uid_hold_hash_fn(const void *obj, const int flags)
- {
- const char *key = obj;
- switch (flags & OBJ_SEARCH_MASK) {
- case OBJ_SEARCH_KEY:
- break;
- case OBJ_SEARCH_OBJECT:
- break;
- default:
- /* Hash can only work on something with a full key. */
- ast_assert(0);
- return 0;
- }
- return ast_str_hash(key);
- }
- /*! AO2 sort function for on hold UIDs */
- static int uid_hold_sort_fn(const void *obj_left, const void *obj_right, const int flags)
- {
- const char *left = obj_left;
- const char *right = obj_right;
- int cmp;
- switch (flags & OBJ_SEARCH_MASK) {
- case OBJ_SEARCH_OBJECT:
- case OBJ_SEARCH_KEY:
- cmp = strcmp(left, right);
- break;
- case OBJ_SEARCH_PARTIAL_KEY:
- cmp = strncmp(left, right, strlen(right));
- break;
- default:
- /* Sort can only work on something with a full or partial key. */
- ast_assert(0);
- cmp = 0;
- break;
- }
- return cmp;
- }
- static struct ao2_container *pjsip_uids_onhold;
- /*!
- * \brief Add a channel ID to the list of PJSIP channels on hold
- *
- * \param chan_uid - Unique ID of the channel being put into the hold list
- *
- * \retval 0 Channel has been added to or was already in the hold list
- * \retval -1 Failed to add channel to the hold list
- */
- static int chan_pjsip_add_hold(const char *chan_uid)
- {
- RAII_VAR(char *, hold_uid, NULL, ao2_cleanup);
- hold_uid = ao2_find(pjsip_uids_onhold, chan_uid, OBJ_SEARCH_KEY);
- if (hold_uid) {
- /* Device is already on hold. Nothing to do. */
- return 0;
- }
- /* Device wasn't in hold list already. Create a new one. */
- hold_uid = ao2_alloc_options(strlen(chan_uid) + 1, NULL,
- AO2_ALLOC_OPT_LOCK_NOLOCK);
- if (!hold_uid) {
- return -1;
- }
- ast_copy_string(hold_uid, chan_uid, strlen(chan_uid) + 1);
- if (ao2_link(pjsip_uids_onhold, hold_uid) == 0) {
- return -1;
- }
- return 0;
- }
- /*!
- * \brief Remove a channel ID from the list of PJSIP channels on hold
- *
- * \param chan_uid - Unique ID of the channel being taken out of the hold list
- */
- static void chan_pjsip_remove_hold(const char *chan_uid)
- {
- ao2_find(pjsip_uids_onhold, chan_uid, OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA);
- }
- /*!
- * \brief Determine whether a channel ID is in the list of PJSIP channels on hold
- *
- * \param chan_uid - Channel being checked
- *
- * \retval 0 The channel is not in the hold list
- * \retval 1 The channel is in the hold list
- */
- static int chan_pjsip_get_hold(const char *chan_uid)
- {
- RAII_VAR(char *, hold_uid, NULL, ao2_cleanup);
- hold_uid = ao2_find(pjsip_uids_onhold, chan_uid, OBJ_SEARCH_KEY);
- if (!hold_uid) {
- return 0;
- }
- return 1;
- }
- /*! \brief Function called to get the device state of an endpoint */
- static int chan_pjsip_devicestate(const char *data)
- {
- RAII_VAR(struct ast_sip_endpoint *, endpoint, ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", data), ao2_cleanup);
- enum ast_device_state state = AST_DEVICE_UNKNOWN;
- RAII_VAR(struct ast_endpoint_snapshot *, endpoint_snapshot, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
- struct ast_devstate_aggregate aggregate;
- int num, inuse = 0;
- if (!endpoint) {
- return AST_DEVICE_INVALID;
- }
- endpoint_snapshot = ast_endpoint_latest_snapshot(ast_endpoint_get_tech(endpoint->persistent),
- ast_endpoint_get_resource(endpoint->persistent));
- if (!endpoint_snapshot) {
- return AST_DEVICE_INVALID;
- }
- if (endpoint_snapshot->state == AST_ENDPOINT_OFFLINE) {
- state = AST_DEVICE_UNAVAILABLE;
- } else if (endpoint_snapshot->state == AST_ENDPOINT_ONLINE) {
- state = AST_DEVICE_NOT_INUSE;
- }
- if (!endpoint_snapshot->num_channels || !(cache = ast_channel_cache())) {
- return state;
- }
- ast_devstate_aggregate_init(&aggregate);
- ao2_ref(cache, +1);
- for (num = 0; num < endpoint_snapshot->num_channels; num++) {
- RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
- struct ast_channel_snapshot *snapshot;
- msg = stasis_cache_get(cache, ast_channel_snapshot_type(),
- endpoint_snapshot->channel_ids[num]);
- if (!msg) {
- continue;
- }
- snapshot = stasis_message_data(msg);
- if (chan_pjsip_get_hold(snapshot->uniqueid)) {
- ast_devstate_aggregate_add(&aggregate, AST_DEVICE_ONHOLD);
- } else {
- ast_devstate_aggregate_add(&aggregate, ast_state_chan2dev(snapshot->state));
- }
- if ((snapshot->state == AST_STATE_UP) || (snapshot->state == AST_STATE_RING) ||
- (snapshot->state == AST_STATE_BUSY)) {
- inuse++;
- }
- }
- if (endpoint->devicestate_busy_at && (inuse == endpoint->devicestate_busy_at)) {
- state = AST_DEVICE_BUSY;
- } else if (ast_devstate_aggregate_result(&aggregate) != AST_DEVICE_INVALID) {
- state = ast_devstate_aggregate_result(&aggregate);
- }
- return state;
- }
- /*! \brief Function called to query options on a channel */
- static int chan_pjsip_queryoption(struct ast_channel *ast, int option, void *data, int *datalen)
- {
- struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
- struct ast_sip_session *session = channel->session;
- int res = -1;
- enum ast_t38_state state = T38_STATE_UNAVAILABLE;
- switch (option) {
- case AST_OPTION_T38_STATE:
- if (session->endpoint->media.t38.enabled) {
- switch (session->t38state) {
- case T38_LOCAL_REINVITE:
- case T38_PEER_REINVITE:
- state = T38_STATE_NEGOTIATING;
- break;
- case T38_ENABLED:
- state = T38_STATE_NEGOTIATED;
- break;
- case T38_REJECTED:
- state = T38_STATE_REJECTED;
- break;
- default:
- state = T38_STATE_UNKNOWN;
- break;
- }
- }
- *((enum ast_t38_state *) data) = state;
- res = 0;
- break;
- default:
- break;
- }
- return res;
- }
- static const char *chan_pjsip_get_uniqueid(struct ast_channel *ast)
- {
- struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
- char *uniqueid = ast_threadstorage_get(&uniqueid_threadbuf, UNIQUEID_BUFSIZE);
- if (!uniqueid) {
- return "";
- }
- ast_copy_pj_str(uniqueid, &channel->session->inv_session->dlg->call_id->id, UNIQUEID_BUFSIZE);
- return uniqueid;
- }
- struct indicate_data {
- struct ast_sip_session *session;
- int condition;
- int response_code;
- void *frame_data;
- size_t datalen;
- };
- static void indicate_data_destroy(void *obj)
- {
- struct indicate_data *ind_data = obj;
- ast_free(ind_data->frame_data);
- ao2_ref(ind_data->session, -1);
- }
- static struct indicate_data *indicate_data_alloc(struct ast_sip_session *session,
- int condition, int response_code, const void *frame_data, size_t datalen)
- {
- struct indicate_data *ind_data = ao2_alloc(sizeof(*ind_data), indicate_data_destroy);
- if (!ind_data) {
- return NULL;
- }
- ind_data->frame_data = ast_malloc(datalen);
- if (!ind_data->frame_data) {
- ao2_ref(ind_data, -1);
- return NULL;
- }
- memcpy(ind_data->frame_data, frame_data, datalen);
- ind_data->datalen = datalen;
- ind_data->condition = condition;
- ind_data->response_code = response_code;
- ao2_ref(session, +1);
- ind_data->session = session;
- return ind_data;
- }
- static int indicate(void *data)
- {
- pjsip_tx_data *packet = NULL;
- struct indicate_data *ind_data = data;
- struct ast_sip_session *session = ind_data->session;
- int response_code = ind_data->response_code;
- if ((session->inv_session->state != PJSIP_INV_STATE_DISCONNECTED) &&
- (pjsip_inv_answer(session->inv_session, response_code, NULL, NULL, &packet) == PJ_SUCCESS)) {
- ast_sip_session_send_response(session, packet);
- }
- #ifdef HAVE_PJSIP_INV_SESSION_REF
- pjsip_inv_dec_ref(session->inv_session);
- #endif
- ao2_ref(ind_data, -1);
- return 0;
- }
- /*! \brief Send SIP INFO with video update request */
- static int transmit_info_with_vidupdate(void *data)
- {
- const char * xml =
- "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n"
- " <media_control>\r\n"
- " <vc_primitive>\r\n"
- " <to_encoder>\r\n"
- " <picture_fast_update/>\r\n"
- " </to_encoder>\r\n"
- " </vc_primitive>\r\n"
- " </media_control>\r\n";
- const struct ast_sip_body body = {
- .type = "application",
- .subtype = "media_control+xml",
- .body_text = xml
- };
- RAII_VAR(struct ast_sip_session *, session, data, ao2_cleanup);
- struct pjsip_tx_data *tdata;
- if (session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
- ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
- session->inv_session->cause,
- pjsip_get_status_text(session->inv_session->cause)->ptr);
- goto failure;
- }
- if (ast_sip_create_request("INFO", session->inv_session->dlg, session->endpoint, NULL, NULL, &tdata)) {
- ast_log(LOG_ERROR, "Could not create text video update INFO request\n");
- goto failure;
- }
- if (ast_sip_add_body(tdata, &body)) {
- ast_log(LOG_ERROR, "Could not add body to text video update INFO request\n");
- goto failure;
- }
- ast_sip_session_send_request(session, tdata);
- #ifdef HAVE_PJSIP_INV_SESSION_REF
- pjsip_inv_dec_ref(session->inv_session);
- #endif
- return 0;
- failure:
- #ifdef HAVE_PJSIP_INV_SESSION_REF
- pjsip_inv_dec_ref(session->inv_session);
- #endif
- return -1;
- }
- /*!
- * \internal
- * \brief TRUE if a COLP update can be sent to the peer.
- * \since 13.3.0
- *
- * \param session The session to see if the COLP update is allowed.
- *
- * \retval 0 Update is not allowed.
- * \retval 1 Update is allowed.
- */
- static int is_colp_update_allowed(struct ast_sip_session *session)
- {
- struct ast_party_id connected_id;
- int update_allowed = 0;
- if (!session->endpoint->send_connected_line
- || (!session->endpoint->id.send_pai && !session->endpoint->id.send_rpid)) {
- return 0;
- }
- /*
- * Check if privacy allows the update. Check while the channel
- * is locked so we can work with the shallow connected_id copy.
- */
- ast_channel_lock(session->channel);
- connected_id = ast_channel_connected_effective_id(session->channel);
- if (connected_id.number.valid
- && (session->endpoint->id.trust_outbound
- || (ast_party_id_presentation(&connected_id) & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED)) {
- update_allowed = 1;
- }
- ast_channel_unlock(session->channel);
- return update_allowed;
- }
- /*! \brief Update connected line information */
- static int update_connected_line_information(void *data)
- {
- struct ast_sip_session *session = data;
- if (session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
- ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
- session->inv_session->cause,
- pjsip_get_status_text(session->inv_session->cause)->ptr);
- #ifdef HAVE_PJSIP_INV_SESSION_REF
- pjsip_inv_dec_ref(session->inv_session);
- #endif
- ao2_ref(session, -1);
- return -1;
- }
- if (ast_channel_state(session->channel) == AST_STATE_UP
- || session->inv_session->role == PJSIP_ROLE_UAC) {
- if (is_colp_update_allowed(session)) {
- enum ast_sip_session_refresh_method method;
- int generate_new_sdp;
- method = session->endpoint->id.refresh_method;
- if (session->inv_session->options & PJSIP_INV_SUPPORT_UPDATE) {
- method = AST_SIP_SESSION_REFRESH_METHOD_UPDATE;
- }
- /* Only the INVITE method actually needs SDP, UPDATE can do without */
- generate_new_sdp = (method == AST_SIP_SESSION_REFRESH_METHOD_INVITE);
- ast_sip_session_refresh(session, NULL, NULL, NULL, method, generate_new_sdp);
- }
- } else if (session->endpoint->rpid_immediate
- && session->inv_session->state != PJSIP_INV_STATE_DISCONNECTED
- && is_colp_update_allowed(session)) {
- int response_code = 0;
- if (ast_channel_state(session->channel) == AST_STATE_RING) {
- response_code = !session->endpoint->inband_progress ? 180 : 183;
- } else if (ast_channel_state(session->channel) == AST_STATE_RINGING) {
- response_code = 183;
- }
- if (response_code) {
- struct pjsip_tx_data *packet = NULL;
- if (pjsip_inv_answer(session->inv_session, response_code, NULL, NULL, &packet) == PJ_SUCCESS) {
- ast_sip_session_send_response(session, packet);
- }
- }
- }
- #ifdef HAVE_PJSIP_INV_SESSION_REF
- pjsip_inv_dec_ref(session->inv_session);
- #endif
- ao2_ref(session, -1);
- return 0;
- }
- /*! \brief Function called by core to ask the channel to indicate some sort of condition */
- static int chan_pjsip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
- {
- struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
- struct chan_pjsip_pvt *pvt = channel->pvt;
- struct ast_sip_session_media *media;
- int response_code = 0;
- int res = 0;
- char *device_buf;
- size_t device_buf_size;
- switch (condition) {
- case AST_CONTROL_RINGING:
- if (ast_channel_state(ast) == AST_STATE_RING) {
- if (channel->session->endpoint->inband_progress) {
- response_code = 183;
- res = -1;
- } else {
- response_code = 180;
- }
- } else {
- res = -1;
- }
- ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, "PJSIP/%s", ast_sorcery_object_get_id(channel->session->endpoint));
- break;
- case AST_CONTROL_BUSY:
- if (ast_channel_state(ast) != AST_STATE_UP) {
- response_code = 486;
- } else {
- res = -1;
- }
- break;
- case AST_CONTROL_CONGESTION:
- if (ast_channel_state(ast) != AST_STATE_UP) {
- response_code = 503;
- } else {
- res = -1;
- }
- break;
- case AST_CONTROL_INCOMPLETE:
- if (ast_channel_state(ast) != AST_STATE_UP) {
- response_code = 484;
- } else {
- res = -1;
- }
- break;
- case AST_CONTROL_PROCEEDING:
- if (ast_channel_state(ast) != AST_STATE_UP) {
- response_code = 100;
- } else {
- res = -1;
- }
- break;
- case AST_CONTROL_PROGRESS:
- if (ast_channel_state(ast) != AST_STATE_UP) {
- response_code = 183;
- } else {
- res = -1;
- }
- ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, "PJSIP/%s", ast_sorcery_object_get_id(channel->session->endpoint));
- break;
- case AST_CONTROL_VIDUPDATE:
- media = pvt->media[SIP_MEDIA_VIDEO];
- if (media && media->rtp) {
- /* FIXME: Only use this for VP8. Additional work would have to be done to
- * fully support other video codecs */
- if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), ast_format_vp8) != AST_FORMAT_CMP_NOT_EQUAL ||
- ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), ast_format_vp9) != AST_FORMAT_CMP_NOT_EQUAL) {
- /* FIXME Fake RTP write, this will be sent as an RTCP packet. Ideally the
- * RTP engine would provide a way to externally write/schedule RTCP
- * packets */
- struct ast_frame fr;
- fr.frametype = AST_FRAME_CONTROL;
- fr.subclass.integer = AST_CONTROL_VIDUPDATE;
- res = ast_rtp_instance_write(media->rtp, &fr);
- } else {
- ao2_ref(channel->session, +1);
- #ifdef HAVE_PJSIP_INV_SESSION_REF
- if (pjsip_inv_add_ref(channel->session->inv_session) != PJ_SUCCESS) {
- ast_log(LOG_ERROR, "Can't increase the session reference counter\n");
- ao2_cleanup(channel->session);
- } else {
- #endif
- if (ast_sip_push_task(channel->session->serializer, transmit_info_with_vidupdate, channel->session)) {
- ao2_cleanup(channel->session);
- }
- #ifdef HAVE_PJSIP_INV_SESSION_REF
- }
- #endif
- }
- ast_test_suite_event_notify("AST_CONTROL_VIDUPDATE", "Result: Success");
- } else {
- ast_test_suite_event_notify("AST_CONTROL_VIDUPDATE", "Result: Failure");
- res = -1;
- }
- break;
- case AST_CONTROL_CONNECTED_LINE:
- ao2_ref(channel->session, +1);
- #ifdef HAVE_PJSIP_INV_SESSION_REF
- if (pjsip_inv_add_ref(channel->session->inv_session) != PJ_SUCCESS) {
- ast_log(LOG_ERROR, "Can't increase the session reference counter\n");
- ao2_cleanup(channel->session);
- return -1;
- }
- #endif
- if (ast_sip_push_task(channel->session->serializer, update_connected_line_information, channel->session)) {
- #ifdef HAVE_PJSIP_INV_SESSION_REF
- pjsip_inv_dec_ref(channel->session->inv_session);
- #endif
- ao2_cleanup(channel->session);
- }
- break;
- case AST_CONTROL_UPDATE_RTP_PEER:
- break;
- case AST_CONTROL_PVT_CAUSE_CODE:
- res = -1;
- break;
- case AST_CONTROL_MASQUERADE_NOTIFY:
- ast_assert(datalen == sizeof(int));
- if (*(int *) data) {
- /*
- * Masquerade is beginning:
- * Wait for session serializer to get suspended.
- */
- ast_channel_unlock(ast);
- ast_sip_session_suspend(channel->session);
- ast_channel_lock(ast);
- } else {
- /*
- * Masquerade is complete:
- * Unsuspend the session serializer.
- */
- ast_sip_session_unsuspend(channel->session);
- }
- break;
- case AST_CONTROL_HOLD:
- chan_pjsip_add_hold(ast_channel_uniqueid(ast));
- device_buf_size = strlen(ast_channel_name(ast)) + 1;
- device_buf = alloca(device_buf_size);
- ast_channel_get_device_name(ast, device_buf, device_buf_size);
- ast_devstate_changed_literal(AST_DEVICE_ONHOLD, 1, device_buf);
- ast_moh_start(ast, data, NULL);
- break;
- case AST_CONTROL_UNHOLD:
- chan_pjsip_remove_hold(ast_channel_uniqueid(ast));
- device_buf_size = strlen(ast_channel_name(ast)) + 1;
- device_buf = alloca(device_buf_size);
- ast_channel_get_device_name(ast, device_buf, device_buf_size);
- ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, 1, device_buf);
- ast_moh_stop(ast);
- break;
- case AST_CONTROL_SRCUPDATE:
- break;
- case AST_CONTROL_SRCCHANGE:
- break;
- case AST_CONTROL_REDIRECTING:
- if (ast_channel_state(ast) != AST_STATE_UP) {
- response_code = 181;
- } else {
- res = -1;
- }
- break;
- case AST_CONTROL_T38_PARAMETERS:
- res = 0;
- if (channel->session->t38state == T38_PEER_REINVITE) {
- const struct ast_control_t38_parameters *parameters = data;
- if (parameters->request_response == AST_T38_REQUEST_PARMS) {
- res = AST_T38_REQUEST_PARMS;
- }
- }
- break;
- case -1:
- res = -1;
- break;
- default:
- ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", condition);
- res = -1;
- break;
- }
- if (response_code) {
- struct indicate_data *ind_data = indicate_data_alloc(channel->session, condition, response_code, data, datalen);
- if (!ind_data) {
- return -1;
- }
- #ifdef HAVE_PJSIP_INV_SESSION_REF
- if (pjsip_inv_add_ref(ind_data->session->inv_session) != PJ_SUCCESS) {
- ast_log(LOG_ERROR, "Can't increase the session reference counter\n");
- ao2_cleanup(ind_data);
- return -1;
- }
- #endif
- if (ast_sip_push_task(channel->session->serializer, indicate, ind_data)) {
- ast_log(LOG_NOTICE, "Cannot send response code %d to endpoint %s. Could not queue task properly\n",
- response_code, ast_sorcery_object_get_id(channel->session->endpoint));
- #ifdef HAVE_PJSIP_INV_SESSION_REF
- pjsip_inv_dec_ref(ind_data->session->inv_session);
- #endif
- ao2_cleanup(ind_data);
- res = -1;
- }
- }
- return res;
- }
- struct transfer_data {
- struct ast_sip_session *session;
- char *target;
- };
- static void transfer_data_destroy(void *obj)
- {
- struct transfer_data *trnf_data = obj;
- ast_free(trnf_data->target);
- ao2_cleanup(trnf_data->session);
- }
- static struct transfer_data *transfer_data_alloc(struct ast_sip_session *session, const char *target)
- {
- struct transfer_data *trnf_data = ao2_alloc(sizeof(*trnf_data), transfer_data_destroy);
- if (!trnf_data) {
- return NULL;
- }
- if (!(trnf_data->target = ast_strdup(target))) {
- ao2_ref(trnf_data, -1);
- return NULL;
- }
- ao2_ref(session, +1);
- trnf_data->session = session;
- return trnf_data;
- }
- static void transfer_redirect(struct ast_sip_session *session, const char *target)
- {
- pjsip_tx_data *packet;
- enum ast_control_transfer message = AST_TRANSFER_SUCCESS;
- pjsip_contact_hdr *contact;
- pj_str_t tmp;
- if (pjsip_inv_end_session(session->inv_session, 302, NULL, &packet) != PJ_SUCCESS
- || !packet) {
- ast_log(LOG_WARNING, "Failed to redirect PJSIP session for channel %s\n",
- ast_channel_name(session->channel));
- message = AST_TRANSFER_FAILED;
- ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
- return;
- }
- if (!(contact = pjsip_msg_find_hdr(packet->msg, PJSIP_H_CONTACT, NULL))) {
- contact = pjsip_contact_hdr_create(packet->pool);
- }
- pj_strdup2_with_null(packet->pool, &tmp, target);
- if (!(contact->uri = pjsip_parse_uri(packet->pool, tmp.ptr, tmp.slen, PJSIP_PARSE_URI_AS_NAMEADDR))) {
- ast_log(LOG_WARNING, "Failed to parse destination URI '%s' for channel %s\n",
- target, ast_channel_name(session->channel));
- message = AST_TRANSFER_FAILED;
- ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
- pjsip_tx_data_dec_ref(packet);
- return;
- }
- pjsip_msg_add_hdr(packet->msg, (pjsip_hdr *) contact);
- ast_sip_session_send_response(session, packet);
- ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
- }
- static void transfer_refer(struct ast_sip_session *session, const char *target)
- {
- pjsip_evsub *sub;
- enum ast_control_transfer message = AST_TRANSFER_SUCCESS;
- pj_str_t tmp;
- pjsip_tx_data *packet;
- const char *ref_by_val;
- char local_info[pj_strlen(&session->inv_session->dlg->local.info_str) + 1];
- if (pjsip_xfer_create_uac(session->inv_session->dlg, NULL, &sub) != PJ_SUCCESS) {
- message = AST_TRANSFER_FAILED;
- ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
- return;
- }
- if (pjsip_xfer_initiate(sub, pj_cstr(&tmp, target), &packet) != PJ_SUCCESS) {
- message = AST_TRANSFER_FAILED;
- ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
- pjsip_evsub_terminate(sub, PJ_FALSE);
- return;
- }
- ref_by_val = pbx_builtin_getvar_helper(session->channel, "SIPREFERREDBYHDR");
- if (!ast_strlen_zero(ref_by_val)) {
- ast_sip_add_header(packet, "Referred-By", ref_by_val);
- } else {
- ast_copy_pj_str(local_info, &session->inv_session->dlg->local.info_str, sizeof(local_info));
- ast_sip_add_header(packet, "Referred-By", local_info);
- }
- pjsip_xfer_send_request(sub, packet);
- ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
- }
- static int transfer(void *data)
- {
- struct transfer_data *trnf_data = data;
- struct ast_sip_endpoint *endpoint = NULL;
- struct ast_sip_contact *contact = NULL;
- const char *target = trnf_data->target;
- if (trnf_data->session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
- ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
- trnf_data->session->inv_session->cause,
- pjsip_get_status_text(trnf_data->session->inv_session->cause)->ptr);
- } else {
- /* See if we have an endpoint; if so, use its contact */
- endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", target);
- if (endpoint) {
- contact = ast_sip_location_retrieve_contact_from_aor_list(endpoint->aors);
- if (contact && !ast_strlen_zero(contact->uri)) {
- target = contact->uri;
- }
- }
- if (ast_channel_state(trnf_data->session->channel) == AST_STATE_RING) {
- transfer_redirect(trnf_data->session, target);
- } else {
- transfer_refer(trnf_data->session, target);
- }
- }
- #ifdef HAVE_PJSIP_INV_SESSION_REF
- pjsip_inv_dec_ref(trnf_data->session->inv_session);
- #endif
- ao2_ref(trnf_data, -1);
- ao2_cleanup(endpoint);
- ao2_cleanup(contact);
- return 0;
- }
- /*! \brief Function called by core for Asterisk initiated transfer */
- static int chan_pjsip_transfer(struct ast_channel *chan, const char *target)
- {
- struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
- struct transfer_data *trnf_data = transfer_data_alloc(channel->session, target);
- if (!trnf_data) {
- return -1;
- }
- #ifdef HAVE_PJSIP_INV_SESSION_REF
- if (pjsip_inv_add_ref(trnf_data->session->inv_session) != PJ_SUCCESS) {
- ast_log(LOG_ERROR, "Can't increase the session reference counter\n");
- ao2_cleanup(trnf_data);
- return -1;
- }
- #endif
- if (ast_sip_push_task(channel->session->serializer, transfer, trnf_data)) {
- ast_log(LOG_WARNING, "Error requesting transfer\n");
- #ifdef HAVE_PJSIP_INV_SESSION_REF
- pjsip_inv_dec_ref(trnf_data->session->inv_session);
- #endif
- ao2_cleanup(trnf_data);
- return -1;
- }
- return 0;
- }
- /*! \brief Function called by core to start a DTMF digit */
- static int chan_pjsip_digit_begin(struct ast_channel *chan, char digit)
- {
- struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
- struct chan_pjsip_pvt *pvt = channel->pvt;
- struct ast_sip_session_media *media = pvt->media[SIP_MEDIA_AUDIO];
- int res = 0;
- switch (channel->session->dtmf) {
- case AST_SIP_DTMF_RFC_4733:
- if (!media || !media->rtp) {
- return -1;
- }
- ast_rtp_instance_dtmf_begin(media->rtp, digit);
- break;
- case AST_SIP_DTMF_AUTO:
- if (!media || !media->rtp || (ast_rtp_instance_dtmf_mode_get(media->rtp) == AST_RTP_DTMF_MODE_INBAND)) {
- return -1;
- }
- ast_rtp_instance_dtmf_begin(media->rtp, digit);
- break;
- case AST_SIP_DTMF_AUTO_INFO:
- if (!media || !media->rtp || (ast_rtp_instance_dtmf_mode_get(media->rtp) == AST_RTP_DTMF_MODE_NONE)) {
- return -1;
- }
- ast_rtp_instance_dtmf_begin(media->rtp, digit);
- break;
- case AST_SIP_DTMF_NONE:
- break;
- case AST_SIP_DTMF_INBAND:
- res = -1;
- break;
- default:
- break;
- }
- return res;
- }
- struct info_dtmf_data {
- struct ast_sip_session *session;
- char digit;
- unsigned int duration;
- };
- static void info_dtmf_data_destroy(void *obj)
- {
- struct info_dtmf_data *dtmf_data = obj;
- ao2_ref(dtmf_data->session, -1);
- }
- static struct info_dtmf_data *info_dtmf_data_alloc(struct ast_sip_session *session, char digit, unsigned int duration)
- {
- struct info_dtmf_data *dtmf_data = ao2_alloc(sizeof(*dtmf_data), info_dtmf_data_destroy);
- if (!dtmf_data) {
- return NULL;
- }
- ao2_ref(session, +1);
- dtmf_data->session = session;
- dtmf_data->digit = digit;
- dtmf_data->duration = duration;
- return dtmf_data;
- }
- static int transmit_info_dtmf(void *data)
- {
- RAII_VAR(struct info_dtmf_data *, dtmf_data, data, ao2_cleanup);
- struct ast_sip_session *session = dtmf_data->session;
- struct pjsip_tx_data *tdata;
- RAII_VAR(struct ast_str *, body_text, NULL, ast_free_ptr);
- struct ast_sip_body body = {
- .type = "application",
- .subtype = "dtmf-relay",
- };
- if (session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
- ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
- session->inv_session->cause,
- pjsip_get_status_text(session->inv_session->cause)->ptr);
- goto failure;
- }
- if (!(body_text = ast_str_create(32))) {
- ast_log(LOG_ERROR, "Could not allocate buffer for INFO DTMF.\n");
- goto failure;
- }
- ast_str_set(&body_text, 0, "Signal=%c\r\nDuration=%u\r\n", dtmf_data->digit, dtmf_data->duration);
- body.body_text = ast_str_buffer(body_text);
- if (ast_sip_create_request("INFO", session->inv_session->dlg, session->endpoint, NULL, NULL, &tdata)) {
- ast_log(LOG_ERROR, "Could not create DTMF INFO request\n");
- goto failure;
- }
- if (ast_sip_add_body(tdata, &body)) {
- ast_log(LOG_ERROR, "Could not add body to DTMF INFO request\n");
- pjsip_tx_data_dec_ref(tdata);
- goto failure;
- }
- ast_sip_session_send_request(session, tdata);
- #ifdef HAVE_PJSIP_INV_SESSION_REF
- pjsip_inv_dec_ref(session->inv_session);
- #endif
- return 0;
- failure:
- #ifdef HAVE_PJSIP_INV_SESSION_REF
- pjsip_inv_dec_ref(session->inv_session);
- #endif
- return -1;
- }
- /*! \brief Function called by core to stop a DTMF digit */
- static int chan_pjsip_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
- {
- struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
- struct chan_pjsip_pvt *pvt = channel->pvt;
- struct ast_sip_session_media *media = pvt->media[SIP_MEDIA_AUDIO];
- int res = 0;
- switch (channel->session->dtmf) {
- case AST_SIP_DTMF_AUTO_INFO:
- {
- if (!media || !media->rtp) {
- return -1;
- }
- if (ast_rtp_instance_dtmf_mode_get(media->rtp) != AST_RTP_DTMF_MODE_NONE) {
- ast_debug(3, "Told to send end of digit on Auto-Info channel %s RFC4733 negotiated so using it.\n", ast_channel_name(ast));
- ast_rtp_instance_dtmf_end_with_duration(media->rtp, digit, duration);
- break;
- }
- /* If RFC_4733 was not negotiated, fail through to the DTMF_INFO processing */
- ast_debug(3, "Told to send end of digit on Auto-Info channel %s RFC4733 NOT negotiated using INFO instead.\n", ast_channel_name(ast));
- }
- case AST_SIP_DTMF_INFO:
- {
- struct info_dtmf_data *dtmf_data = info_dtmf_data_alloc(channel->session, digit, duration);
- if (!dtmf_data) {
- return -1;
- }
- #ifdef HAVE_PJSIP_INV_SESSION_REF
- if (pjsip_inv_add_ref(dtmf_data->session->inv_session) != PJ_SUCCESS) {
- ast_log(LOG_ERROR, "Can't increase the session reference counter\n");
- ao2_cleanup(dtmf_data);
- return -1;
- }
- #endif
- if (ast_sip_push_task(channel->session->serializer, transmit_info_dtmf, dtmf_data)) {
- ast_log(LOG_WARNING, "Error sending DTMF via INFO.\n");
- #ifdef HAVE_PJSIP_INV_SESSION_REF
- pjsip_inv_dec_ref(dtmf_data->session->inv_session);
- #endif
- ao2_cleanup(dtmf_data);
- return -1;
- }
- break;
- }
- case AST_SIP_DTMF_RFC_4733:
- if (!media || !media->rtp) {
- return -1;
- }
- ast_rtp_instance_dtmf_end_with_duration(media->rtp, digit, duration);
- break;
- case AST_SIP_DTMF_AUTO:
- if (!media || !media->rtp || (ast_rtp_instance_dtmf_mode_get(media->rtp) == AST_RTP_DTMF_MODE_INBAND)) {
- return -1;
- }
- ast_rtp_instance_dtmf_end_with_duration(media->rtp, digit, duration);
- break;
- case AST_SIP_DTMF_NONE:
- break;
- case AST_SIP_DTMF_INBAND:
- res = -1;
- break;
- }
- return res;
- }
- static void update_initial_connected_line(struct ast_sip_session *session)
- {
- struct ast_party_connected_line connected;
- /*
- * Use the channel CALLERID() as the initial connected line data.
- * The core or a predial handler may have supplied missing values
- * from the session->endpoint->id.self about who we are calling.
- */
- ast_channel_lock(session->channel);
- ast_party_id_copy(&session->id, &ast_channel_caller(session->channel)->id);
- ast_channel_unlock(session->channel);
- /* Supply initial connected line information if available. */
- if (!session->id.number.valid && !session->id.name.valid) {
- return;
- }
- ast_party_connected_line_init(&connected);
- connected.id = session->id;
- connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
- ast_channel_queue_connected_line_update(session->channel, &connected, NULL);
- }
- static int call(void *data)
- {
- struct ast_sip_channel_pvt *channel = data;
- struct ast_sip_session *session = channel->session;
- struct chan_pjsip_pvt *pvt = channel->pvt;
- pjsip_tx_data *tdata;
- int res = ast_sip_session_create_invite(session, &tdata);
- if (res) {
- ast_set_hangupsource(session->channel, ast_channel_name(session->channel), 0);
- ast_queue_hangup(session->channel);
- } else {
- set_channel_on_rtp_instance(pvt, ast_channel_uniqueid(session->channel));
- update_initial_connected_line(session);
- ast_sip_session_send_request(session, tdata);
- }
- ao2_ref(channel, -1);
- return res;
- }
- /*! \brief Function called by core to actually start calling a remote party */
- static int chan_pjsip_call(struct ast_channel *ast, const char *dest, int timeout)
- {
- struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
- ao2_ref(channel, +1);
- if (ast_sip_push_task(channel->session->serializer, call, channel)) {
- ast_log(LOG_WARNING, "Error attempting to place outbound call to '%s'\n", dest);
- ao2_cleanup(channel);
- return -1;
- }
- return 0;
- }
- /*! \brief Internal function which translates from Asterisk cause codes to SIP response codes */
- static int hangup_cause2sip(int cause)
- {
- switch (cause) {
- case AST_CAUSE_UNALLOCATED: /* 1 */
- case AST_CAUSE_NO_ROUTE_DESTINATION: /* 3 IAX2: Can't find extension in context */
- case AST_CAUSE_NO_ROUTE_TRANSIT_NET: /* 2 */
- return 404;
- case AST_CAUSE_CONGESTION: /* 34 */
- case AST_CAUSE_SWITCH_CONGESTION: /* 42 */
- return 503;
- case AST_CAUSE_NO_USER_RESPONSE: /* 18 */
- return 408;
- case AST_CAUSE_NO_ANSWER: /* 19 */
- case AST_CAUSE_UNREGISTERED: /* 20 */
- return 480;
- case AST_CAUSE_CALL_REJECTED: /* 21 */
- return 403;
- case AST_CAUSE_NUMBER_CHANGED: /* 22 */
- return 410;
- case AST_CAUSE_NORMAL_UNSPECIFIED: /* 31 */
- return 480;
- case AST_CAUSE_INVALID_NUMBER_FORMAT:
- return 484;
- case AST_CAUSE_USER_BUSY:
- return 486;
- case AST_CAUSE_FAILURE:
- return 500;
- case AST_CAUSE_FACILITY_REJECTED: /* 29 */
- return 501;
- case AST_CAUSE_CHAN_NOT_IMPLEMENTED:
- return 503;
- case AST_CAUSE_DESTINATION_OUT_OF_ORDER:
- return 502;
- case AST_CAUSE_BEARERCAPABILITY_NOTAVAIL: /* Can't find codec to connect to host */
- return 488;
- case AST_CAUSE_INTERWORKING: /* Unspecified Interworking issues */
- return 500;
- case AST_CAUSE_NOTDEFINED:
- default:
- ast_debug(1, "AST hangup cause %d (no match found in PJSIP)\n", cause);
- return 0;
- }
- /* Never reached */
- return 0;
- }
- struct hangup_data {
- int cause;
- struct ast_channel *chan;
- };
- static void hangup_data_destroy(void *obj)
- {
- struct hangup_data *h_data = obj;
- h_data->chan = ast_channel_unref(h_data->chan);
- }
- static struct hangup_data *hangup_data_alloc(int cause, struct ast_channel *chan)
- {
- struct hangup_data *h_data = ao2_alloc(sizeof(*h_data), hangup_data_destroy);
- if (!h_data) {
- return NULL;
- }
- h_data->cause = cause;
- h_data->chan = ast_channel_ref(chan);
- return h_data;
- }
- /*! \brief Clear a channel from a session along with its PVT */
- static void clear_session_and_channel(struct ast_sip_session *session, struct ast_channel *ast, struct chan_pjsip_pvt *pvt)
- {
- session->channel = NULL;
- set_channel_on_rtp_instance(pvt, "");
- ast_channel_tech_pvt_set(ast, NULL);
- }
- static int hangup(void *data)
- {
- struct hangup_data *h_data = data;
- struct ast_channel *ast = h_data->chan;
- struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
- struct chan_pjsip_pvt *pvt = channel->pvt;
- struct ast_sip_session *session = channel->session;
- int cause = h_data->cause;
- /*
- * It's possible that session_terminate might cause the session to be destroyed
- * immediately so we need to keep a reference to it so we can NULL session->channel
- * afterwards.
- */
- ast_sip_session_terminate(ao2_bump(session), cause);
- clear_session_and_channel(session, ast, pvt);
- ao2_cleanup(session);
- ao2_cleanup(channel);
- ao2_cleanup(h_data);
- return 0;
- }
- /*! \brief Function called by core to hang up a PJSIP session */
- static int chan_pjsip_hangup(struct ast_channel *ast)
- {
- struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
- struct chan_pjsip_pvt *pvt;
- int cause;
- struct hangup_data *h_data;
- if (!channel || !channel->session) {
- return -1;
- }
- pvt = channel->pvt;
- cause = hangup_cause2sip(ast_channel_hangupcause(channel->session->channel));
- h_data = hangup_data_alloc(cause, ast);
- if (!h_data) {
- goto failure;
- }
- if (ast_sip_push_task(channel->session->serializer, hangup, h_data)) {
- ast_log(LOG_WARNING, "Unable to push hangup task to the threadpool. Expect bad things\n");
- goto failure;
- }
- return 0;
- failure:
- /* Go ahead and do our cleanup of the session and channel even if we're not going
- * to be able to send our SIP request/response
- */
- clear_session_and_channel(channel->session, ast, pvt);
- ao2_cleanup(channel);
- ao2_cleanup(h_data);
- return -1;
- }
- struct request_data {
- struct ast_sip_session *session;
- struct ast_format_cap *caps;
- const char *dest;
- int cause;
- };
- static int request(void *obj)
- {
- struct request_data *req_data = obj;
- struct ast_sip_session *session = NULL;
- char *tmp = ast_strdupa(req_data->dest), *endpoint_name = NULL, *request_user = NULL;
- struct ast_sip_endpoint *endpoint;
- AST_DECLARE_APP_ARGS(args,
- AST_APP_ARG(endpoint);
- AST_APP_ARG(aor);
- );
- if (ast_strlen_zero(tmp)) {
- ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty destination\n");
- req_data->cause = AST_CAUSE_CHANNEL_UNACCEPTABLE;
- return -1;
- }
- AST_NONSTANDARD_APP_ARGS(args, tmp, '/');
- if (ast_sip_get_disable_multi_domain()) {
- /* If a request user has been specified extract it from the endpoint name portion */
- if ((endpoint_name = strchr(args.endpoint, '@'))) {
- request_user = args.endpoint;
- *endpoint_name++ = '\0';
- } else {
- endpoint_name = args.endpoint;
- }
- if (ast_strlen_zero(endpoint_name)) {
- if (request_user) {
- ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty endpoint name: %s@<endpoint-name>\n",
- request_user);
- } else {
- ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty endpoint name\n");
- }
- req_data->cause = AST_CAUSE_CHANNEL_UNACCEPTABLE;
- return -1;
- }
- endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint",
- endpoint_name);
- if (!endpoint) {
- ast_log(LOG_ERROR, "Unable to create PJSIP channel - endpoint '%s' was not found\n", endpoint_name);
- req_data->cause = AST_CAUSE_NO_ROUTE_DESTINATION;
- return -1;
- }
- } else {
- /* First try to find an exact endpoint match, for single (user) or multi-domain (user@domain) */
- endpoint_name = args.endpoint;
- if (ast_strlen_zero(endpoint_name)) {
- ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty endpoint name\n");
- req_data->cause = AST_CAUSE_CHANNEL_UNACCEPTABLE;
- return -1;
- }
- endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint",
- endpoint_name);
- if (!endpoint) {
- /* It seems it's not a multi-domain endpoint or single endpoint exact match,
- * it's possible that it's a SIP trunk with a specified user (user@trunkname),
- * so extract the user before @ sign.
- */
- endpoint_name = strchr(args.endpoint, '@');
- if (!endpoint_name) {
- /*
- * Couldn't find an '@' so it had to be an endpoint
- * name that doesn't exist.
- */
- ast_log(LOG_ERROR, "Unable to create PJSIP channel - endpoint '%s' was not found\n",
- args.endpoint);
- req_data->cause = AST_CAUSE_NO_ROUTE_DESTINATION;
- return -1;
- }
- request_user = args.endpoint;
- *endpoint_name++ = '\0';
- if (ast_strlen_zero(endpoint_name)) {
- ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty endpoint name: %s@<endpoint-name>\n",
- request_user);
- req_data->cause = AST_CAUSE_CHANNEL_UNACCEPTABLE;
- return -1;
- }
- endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint",
- endpoint_name);
- if (!endpoint) {
- ast_log(LOG_ERROR, "Unable to create PJSIP channel - endpoint '%s' was not found\n", endpoint_name);
- req_data->cause = AST_CAUSE_NO_ROUTE_DESTINATION;
- return -1;
- }
- }
- }
- session = ast_sip_session_create_outgoing(endpoint, NULL, args.aor, request_user,
- req_data->caps);
- ao2_ref(endpoint, -1);
- if (!session) {
- ast_log(LOG_ERROR, "Failed to create outgoing session to endpoint '%s'\n", endpoint_name);
- req_data->cause = AST_CAUSE_NO_ROUTE_DESTINATION;
- return -1;
- }
- req_data->session = session;
- return 0;
- }
- /*! \brief Function called by core to create a new outgoing PJSIP session */
- static struct ast_channel *chan_pjsip_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
- {
- struct request_data req_data;
- RAII_VAR(struct ast_sip_session *, session, NULL, ao2_cleanup);
- req_data.caps = cap;
- req_data.dest = data;
- /* Default failure value in case ast_sip_push_task_wait_servant() itself fails. */
- req_data.cause = AST_CAUSE_FAILURE;
- if (ast_sip_push_task_wait_servant(NULL, request, &req_data)) {
- *cause = req_data.cause;
- return NULL;
- }
- session = req_data.session;
- if (!(session->channel = chan_pjsip_new(session, AST_STATE_DOWN, NULL, NULL, assignedids, requestor, NULL))) {
- /* Session needs to be terminated prematurely */
- return NULL;
- }
- return session->channel;
- }
- struct sendtext_data {
- struct ast_sip_session *session;
- struct ast_msg_data *msg;
- };
- static void sendtext_data_destroy(void *obj)
- {
- struct sendtext_data *data = obj;
- ao2_cleanup(data->session);
- ast_free(data->msg);
- }
- static struct sendtext_data* sendtext_data_create(struct ast_channel *chan,
- struct ast_msg_data *msg)
- {
- struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
- struct sendtext_data *data = ao2_alloc(sizeof(*data), sendtext_data_destroy);
- if (!data) {
- return NULL;
- }
- data->msg = ast_msg_data_dup(msg);
- if (!data->msg) {
- ao2_cleanup(data);
- return NULL;
- }
- data->session = channel->session;
- ao2_ref(data->session, +1);
- return data;
- }
- static int sendtext(void *obj)
- {
- struct sendtext_data *data = obj;
- pjsip_tx_data *tdata;
- const char *body_text = ast_msg_data_get_attribute(data->msg, AST_MSG_DATA_ATTR_BODY);
- const char *content_type = ast_msg_data_get_attribute(data->msg, AST_MSG_DATA_ATTR_CONTENT_TYPE);
- char *sep;
- struct ast_sip_body body = {
- .type = "text",
- .subtype = "plain",
- .body_text = body_text,
- };
- if (!ast_strlen_zero(content_type)) {
- sep = strchr(content_type, '/');
- if (sep) {
- *sep = '\0';
- body.type = content_type;
- body.subtype = ++sep;
- }
- }
- if (data->session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
- ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
- data->session->inv_session->cause,
- pjsip_get_status_text(data->session->inv_session->cause)->ptr);
- } else {
- pjsip_from_hdr *hdr;
- pjsip_name_addr *name_addr;
- const char *from = ast_msg_data_get_attribute(data->msg, AST_MSG_DATA_ATTR_FROM);
- const char *to = ast_msg_data_get_attribute(data->msg, AST_MSG_DATA_ATTR_TO);
- int invalidate_tdata = 0;
- ast_sip_create_request("MESSAGE", data->session->inv_session->dlg, data->session->endpoint, NULL, NULL, &tdata);
- ast_sip_add_body(tdata, &body);
- /*
- * If we have a 'from' in the msg, set the display name in the From
- * header to it.
- */
- if (!ast_strlen_zero(from)) {
- hdr = PJSIP_MSG_FROM_HDR(tdata->msg);
- name_addr = (pjsip_name_addr *) hdr->uri;
- pj_strdup2(tdata->pool, &name_addr->display, from);
- invalidate_tdata = 1;
- }
- /*
- * If we have a 'to' in the msg, set the display name in the To
- * header to it.
- */
- if (!ast_strlen_zero(to)) {
- hdr = PJSIP_MSG_TO_HDR(tdata->msg);
- name_addr = (pjsip_name_addr *) hdr->uri;
- pj_strdup2(tdata->pool, &name_addr->display, to);
- invalidate_tdata = 1;
- }
- if (invalidate_tdata) {
- pjsip_tx_data_invalidate_msg(tdata);
- }
- ast_sip_send_request(tdata, data->session->inv_session->dlg, data->session->endpoint, NULL, NULL);
- }
- #ifdef HAVE_PJSIP_INV_SESSION_REF
- pjsip_inv_dec_ref(data->session->inv_session);
- #endif
- ao2_cleanup(data);
- return 0;
- }
- /*! \brief Function called by core to send text on PJSIP session */
- static int chan_pjsip_sendtext_data(struct ast_channel *ast, struct ast_msg_data *msg)
- {
- struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
- struct sendtext_data *data = sendtext_data_create(ast, msg);
- ast_debug(1, "Sending MESSAGE from '%s' to '%s:%s': %s\n",
- ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_FROM),
- ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_TO),
- ast_channel_name(ast),
- ast_msg_data_get_attribute(msg, AST_MSG_DATA_ATTR_BODY));
- if (!data) {
- return -1;
- }
- #ifdef HAVE_PJSIP_INV_SESSION_REF
- if (pjsip_inv_add_ref(data->session->inv_session) != PJ_SUCCESS) {
- ast_log(LOG_ERROR, "Can't increase the session reference counter\n");
- ao2_ref(data, -1);
- return -1;
- }
- #endif
- if (ast_sip_push_task(channel->session->serializer, sendtext, data)) {
- #ifdef HAVE_PJSIP_INV_SESSION_REF
- pjsip_inv_dec_ref(data->session->inv_session);
- #endif
- ao2_ref(data, -1);
- return -1;
- }
- return 0;
- }
- static int chan_pjsip_sendtext(struct ast_channel *ast, const char *text)
- {
- struct ast_msg_data *msg;
- int rc;
- struct ast_msg_data_attribute attrs[] =
- {
- {
- .type = AST_MSG_DATA_ATTR_BODY,
- .value = (char *)text,
- }
- };
- msg = ast_msg_data_alloc(AST_MSG_DATA_SOURCE_TYPE_UNKNOWN, attrs, ARRAY_LEN(attrs));
- if (!msg) {
- return -1;
- }
- rc = chan_pjsip_sendtext_data(ast, msg);
- ast_free(msg);
- return rc;
- }
- /*! \brief Convert SIP hangup causes to Asterisk hangup causes */
- static int hangup_sip2cause(int cause)
- {
- /* Possible values taken from causes.h */
- switch(cause) {
- case 401: /* Unauthorized */
- return AST_CAUSE_CALL_REJECTED;
- case 403: /* Not found */
- return AST_CAUSE_CALL_REJECTED;
- case 404: /* Not found */
- return AST_CAUSE_UNALLOCATED;
- case 405: /* Method not allowed */
- return AST_CAUSE_INTERWORKING;
- case 407: /* Proxy authentication required */
- return AST_CAUSE_CALL_REJECTED;
- case 408: /* No reaction */
- return AST_CAUSE_NO_USER_RESPONSE;
- case 409: /* Conflict */
- return AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
- case 410: /* Gone */
- return AST_CAUSE_NUMBER_CHANGED;
- case 411: /* Length required */
- return AST_CAUSE_INTERWORKING;
- case 413: /* Request entity too large */
- return AST_CAUSE_INTERWORKING;
- case 414: /* Request URI too large */
- return AST_CAUSE_INTERWORKING;
- case 415: /* Unsupported media type */
- return AST_CAUSE_INTERWORKING;
- case 420: /* Bad extension */
- return AST_CAUSE_NO_ROUTE_DESTINATION;
- case 480: /* No answer */
- return AST_CAUSE_NO_ANSWER;
- case 481: /* No answer */
- return AST_CAUSE_INTERWORKING;
- case 482: /* Loop detected */
- return AST_CAUSE_INTERWORKING;
- case 483: /* Too many hops */
- return AST_CAUSE_NO_ANSWER;
- case 484: /* Address incomplete */
- return AST_CAUSE_INVALID_NUMBER_FORMAT;
- case 485: /* Ambiguous */
- return AST_CAUSE_UNALLOCATED;
- case 486: /* Busy everywhere */
- return AST_CAUSE_BUSY;
- case 487: /* Request terminated */
- return AST_CAUSE_INTERWORKING;
- case 488: /* No codecs approved */
- return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
- case 491: /* Request pending */
- return AST_CAUSE_INTERWORKING;
- case 493: /* Undecipherable */
- return AST_CAUSE_INTERWORKING;
- case 500: /* Server internal failure */
- return AST_CAUSE_FAILURE;
- case 501: /* Call rejected */
- return AST_CAUSE_FACILITY_REJECTED;
- case 502:
- return AST_CAUSE_DESTINATION_OUT_OF_ORDER;
- case 503: /* Service unavailable */
- return AST_CAUSE_CONGESTION;
- case 504: /* Gateway timeout */
- return AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
- case 505: /* SIP version not supported */
- return AST_CAUSE_INTERWORKING;
- case 600: /* Busy everywhere */
- return AST_CAUSE_USER_BUSY;
- case 603: /* Decline */
- return AST_CAUSE_CALL_REJECTED;
- case 604: /* Does not exist anywhere */
- return AST_CAUSE_UNALLOCATED;
- case 606: /* Not acceptable */
- return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
- default:
- if (cause < 500 && cause >= 400) {
- /* 4xx class error that is unknown - someting wrong with our request */
- return AST_CAUSE_INTERWORKING;
- } else if (cause < 600 && cause >= 500) {
- /* 5xx class error - problem in the remote end */
- return AST_CAUSE_CONGESTION;
- } else if (cause < 700 && cause >= 600) {
- /* 6xx - global errors in the 4xx class */
- return AST_CAUSE_INTERWORKING;
- }
- return AST_CAUSE_NORMAL;
- }
- /* Never reached */
- return 0;
- }
- static void chan_pjsip_session_begin(struct ast_sip_session *session)
- {
- RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
- if (session->endpoint->media.direct_media.glare_mitigation ==
- AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_NONE) {
- return;
- }
- datastore = ast_sip_session_alloc_datastore(&direct_media_mitigation_info,
- "direct_media_glare_mitigation");
- if (!datastore) {
- return;
- }
- ast_sip_session_add_datastore(session, datastore);
- }
- /*! \brief Function called when the session ends */
- static void chan_pjsip_session_end(struct ast_sip_session *session)
- {
- if (!session->channel) {
- return;
- }
- chan_pjsip_remove_hold(ast_channel_uniqueid(session->channel));
- ast_set_hangupsource(session->channel, ast_channel_name(session->channel), 0);
- if (!ast_channel_hangupcause(session->channel) && session->inv_session) {
- int cause = hangup_sip2cause(session->inv_session->cause);
- ast_queue_hangup_with_cause(session->channel, cause);
- } else {
- ast_queue_hangup(session->channel);
- }
- }
- /*! \brief Function called when a request is received on the session */
- static int chan_pjsip_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
- {
- RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
- struct transport_info_data *transport_data;
- pjsip_tx_data *packet = NULL;
- if (session->channel) {
- return 0;
- }
- /* Check for a to-tag to determine if this is a reinvite */
- if (rdata->msg_info.to->tag.slen) {
- /* Weird case. We've received a reinvite but we don't have a channel. The most
- * typical case for this happening is that a blind transfer fails, and so the
- * transferer attempts to reinvite himself back into the call. We already got
- * rid of that channel, and the other side of the call is unrecoverable.
- *
- * We treat this as a failure, so our best bet is to just hang this call
- * up and not create a new channel. Clearing defer_terminate here ensures that
- * calling ast_sip_session_terminate() can result in a BYE being sent ASAP.
- */
- session->defer_terminate = 0;
- ast_sip_session_terminate(session, 400);
- return -1;
- }
- datastore = ast_sip_session_alloc_datastore(&transport_info, "transport_info");
- if (!datastore) {
- return -1;
- }
- transport_data = ast_calloc(1, sizeof(*transport_data));
- if (!transport_data) {
- return -1;
- }
- pj_sockaddr_cp(&transport_data->local_addr, &rdata->tp_info.transport->local_addr);
- pj_sockaddr_cp(&transport_data->remote_addr, &rdata->pkt_info.src_addr);
- datastore->data = transport_data;
- ast_sip_session_add_datastore(session, datastore);
- if (!(session->channel = chan_pjsip_new(session, AST_STATE_RING, session->exten, NULL, NULL, NULL, NULL))) {
- if (pjsip_inv_end_session(session->inv_session, 503, NULL, &packet) == PJ_SUCCESS
- && packet) {
- ast_sip_session_send_response(session, packet);
- }
- ast_log(LOG_ERROR, "Failed to allocate new PJSIP channel on incoming SIP INVITE\n");
- return -1;
- }
- /* channel gets created on incoming request, but we wait to call start
- so other supplements have a chance to run */
- return 0;
- }
- static int call_pickup_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
- {
- struct ast_features_pickup_config *pickup_cfg;
- struct ast_channel *chan;
- /* Check for a to-tag to determine if this is a reinvite */
- if (rdata->msg_info.to->tag.slen) {
- /* We don't care about reinvites */
- return 0;
- }
- pickup_cfg = ast_get_chan_features_pickup_config(session->channel);
- if (!pickup_cfg) {
- ast_log(LOG_ERROR, "Unable to retrieve pickup configuration options. Unable to detect call pickup extension.\n");
- return 0;
- }
- if (strcmp(session->exten, pickup_cfg->pickupexten)) {
- ao2_ref(pickup_cfg, -1);
- return 0;
- }
- ao2_ref(pickup_cfg, -1);
- /* We can't directly use session->channel because the pickup operation will cause a masquerade to occur,
- * changing the channel pointer in session to a different channel. To ensure we work on the right channel
- * we store a pointer locally before we begin and keep a reference so it remains valid no matter what.
- */
- chan = ast_channel_ref(session->channel);
- if (ast_pickup_call(chan)) {
- ast_channel_hangupcause_set(chan, AST_CAUSE_CALL_REJECTED);
- } else {
- ast_channel_hangupcause_set(chan, AST_CAUSE_NORMAL_CLEARING);
- }
- /* A hangup always occurs because the pickup operation will have either failed resulting in the call
- * needing to be hung up OR the pickup operation was a success and the channel we now have is actually
- * the channel that was replaced, which should be hung up since it is literally in limbo not connected
- * to anything at all.
- */
- ast_hangup(chan);
- ast_channel_unref(chan);
- return 1;
- }
- static struct ast_sip_session_supplement call_pickup_supplement = {
- .method = "INVITE",
- .priority = AST_SIP_SUPPLEMENT_PRIORITY_LAST - 1,
- .incoming_request = call_pickup_incoming_request,
- };
- static int pbx_start_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
- {
- int res;
- /* Check for a to-tag to determine if this is a reinvite */
- if (rdata->msg_info.to->tag.slen) {
- /* We don't care about reinvites */
- return 0;
- }
- res = ast_pbx_start(session->channel);
- switch (res) {
- case AST_PBX_FAILED:
- ast_log(LOG_WARNING, "Failed to start PBX ;(\n");
- ast_channel_hangupcause_set(session->channel, AST_CAUSE_SWITCH_CONGESTION);
- ast_hangup(session->channel);
- break;
- case AST_PBX_CALL_LIMIT:
- ast_log(LOG_WARNING, "Failed to start PBX (call limit reached) \n");
- ast_channel_hangupcause_set(session->channel, AST_CAUSE_SWITCH_CONGESTION);
- ast_hangup(session->channel);
- break;
- case AST_PBX_SUCCESS:
- default:
- break;
- }
- ast_debug(3, "Started PBX on new PJSIP channel %s\n", ast_channel_name(session->channel));
- return (res == AST_PBX_SUCCESS) ? 0 : -1;
- }
- static struct ast_sip_session_supplement pbx_start_supplement = {
- .method = "INVITE",
- .priority = AST_SIP_SUPPLEMENT_PRIORITY_LAST,
- .incoming_request = pbx_start_incoming_request,
- };
- /*! \brief Function called when a response is received on the session */
- static void chan_pjsip_incoming_response(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
- {
- struct pjsip_status_line status = rdata->msg_info.msg->line.status;
- struct ast_control_pvt_cause_code *cause_code;
- int data_size = sizeof(*cause_code);
- if (!session->channel) {
- return;
- }
- /* Build and send the tech-specific cause information */
- /* size of the string making up the cause code is "SIP " number + " " + reason length */
- data_size += 4 + 4 + pj_strlen(&status.reason);
- cause_code = ast_alloca(data_size);
- memset(cause_code, 0, data_size);
- ast_copy_string(cause_code->chan_name, ast_channel_name(session->channel), AST_CHANNEL_NAME);
- snprintf(cause_code->code, data_size - sizeof(*cause_code) + 1, "SIP %d %.*s", status.code,
- (int) pj_strlen(&status.reason), pj_strbuf(&status.reason));
- cause_code->ast_cause = hangup_sip2cause(status.code);
- ast_queue_control_data(session->channel, AST_CONTROL_PVT_CAUSE_CODE, cause_code, data_size);
- ast_channel_hangupcause_hash_set(session->channel, cause_code, data_size);
- switch (status.code) {
- case 180:
- ast_queue_control(session->channel, AST_CONTROL_RINGING);
- ast_channel_lock(session->channel);
- if (ast_channel_state(session->channel) != AST_STATE_UP) {
- ast_setstate(session->channel, AST_STATE_RINGING);
- }
- ast_channel_unlock(session->channel);
- break;
- case 183:
- ast_queue_control(session->channel, AST_CONTROL_PROGRESS);
- break;
- case 200:
- ast_queue_control(session->channel, AST_CONTROL_ANSWER);
- break;
- default:
- break;
- }
- }
- static int chan_pjsip_incoming_ack(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
- {
- if (rdata->msg_info.msg->line.req.method.id == PJSIP_ACK_METHOD) {
- if (session->endpoint->media.direct_media.enabled && session->channel) {
- ast_queue_control(session->channel, AST_CONTROL_SRCCHANGE);
- }
- }
- return 0;
- }
- static int update_devstate(void *obj, void *arg, int flags)
- {
- ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE,
- "PJSIP/%s", ast_sorcery_object_get_id(obj));
- return 0;
- }
- static struct ast_custom_function chan_pjsip_dial_contacts_function = {
- .name = "PJSIP_DIAL_CONTACTS",
- .read = pjsip_acf_dial_contacts_read,
- };
- static struct ast_custom_function chan_pjsip_parse_uri_function = {
- .name = "PJSIP_PARSE_URI",
- .read = pjsip_acf_parse_uri_read,
- };
- static struct ast_custom_function media_offer_function = {
- .name = "PJSIP_MEDIA_OFFER",
- .read = pjsip_acf_media_offer_read,
- .write = pjsip_acf_media_offer_write
- };
- static struct ast_custom_function dtmf_mode_function = {
- .name = "PJSIP_DTMF_MODE",
- .read = pjsip_acf_dtmf_mode_read,
- .write = pjsip_acf_dtmf_mode_write
- };
- static struct ast_custom_function session_refresh_function = {
- .name = "PJSIP_SEND_SESSION_REFRESH",
- .write = pjsip_acf_session_refresh_write,
- };
- /*!
- * \brief Load the module
- *
- * Module loading including tests for configuration or dependencies.
- * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
- * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
- * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
- * configuration file or other non-critical problem return
- * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
- */
- static int load_module(void)
- {
- struct ao2_container *endpoints;
- CHECK_PJSIP_SESSION_MODULE_LOADED();
- if (!(chan_pjsip_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
- return AST_MODULE_LOAD_DECLINE;
- }
- ast_format_cap_append_by_type(chan_pjsip_tech.capabilities, AST_MEDIA_TYPE_AUDIO);
- ast_rtp_glue_register(&chan_pjsip_rtp_glue);
- if (ast_channel_register(&chan_pjsip_tech)) {
- ast_log(LOG_ERROR, "Unable to register channel class %s\n", channel_type);
- goto end;
- }
- if (ast_custom_function_register(&chan_pjsip_dial_contacts_function)) {
- ast_log(LOG_ERROR, "Unable to register PJSIP_DIAL_CONTACTS dialplan function\n");
- goto end;
- }
- if (ast_custom_function_register(&chan_pjsip_parse_uri_function)) {
- ast_log(LOG_ERROR, "Unable to register PJSIP_PARSE_URI dialplan function\n");
- goto end;
- }
- if (ast_custom_function_register(&media_offer_function)) {
- ast_log(LOG_WARNING, "Unable to register PJSIP_MEDIA_OFFER dialplan function\n");
- goto end;
- }
- if (ast_custom_function_register(&dtmf_mode_function)) {
- ast_log(LOG_WARNING, "Unable to register PJSIP_DTMF_MODE dialplan function\n");
- goto end;
- }
- if (ast_custom_function_register(&session_refresh_function)) {
- ast_log(LOG_WARNING, "Unable to register PJSIP_SEND_SESSION_REFRESH dialplan function\n");
- goto end;
- }
- if (ast_sip_session_register_supplement(&chan_pjsip_supplement)) {
- ast_log(LOG_ERROR, "Unable to register PJSIP supplement\n");
- goto end;
- }
- if (ast_sip_session_register_supplement(&chan_pjsip_supplement_response)) {
- ast_log(LOG_ERROR, "Unable to register PJSIP supplement\n");
- goto end;
- }
- if (!(pjsip_uids_onhold = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_RWLOCK,
- AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT, 37, uid_hold_hash_fn,
- uid_hold_sort_fn, NULL))) {
- ast_log(LOG_ERROR, "Unable to create held channels container\n");
- goto end;
- }
- if (ast_sip_session_register_supplement(&call_pickup_supplement)) {
- ast_log(LOG_ERROR, "Unable to register PJSIP call pickup supplement\n");
- goto end;
- }
- if (ast_sip_session_register_supplement(&pbx_start_supplement)) {
- ast_log(LOG_ERROR, "Unable to register PJSIP pbx start supplement\n");
- goto end;
- }
- if (ast_sip_session_register_supplement(&chan_pjsip_ack_supplement)) {
- ast_log(LOG_ERROR, "Unable to register PJSIP ACK supplement\n");
- goto end;
- }
- if (pjsip_channel_cli_register()) {
- ast_log(LOG_ERROR, "Unable to register PJSIP Channel CLI\n");
- goto end;
- }
- /* since endpoints are loaded before the channel driver their device
- states get set to 'invalid', so they need to be updated */
- if ((endpoints = ast_sip_get_endpoints())) {
- ao2_callback(endpoints, OBJ_NODATA, update_devstate, NULL);
- ao2_ref(endpoints, -1);
- }
- return 0;
- end:
- ao2_cleanup(pjsip_uids_onhold);
- pjsip_uids_onhold = NULL;
- ast_sip_session_unregister_supplement(&chan_pjsip_ack_supplement);
- ast_sip_session_unregister_supplement(&pbx_start_supplement);
- ast_sip_session_unregister_supplement(&chan_pjsip_supplement_response);
- ast_sip_session_unregister_supplement(&chan_pjsip_supplement);
- ast_sip_session_unregister_supplement(&call_pickup_supplement);
- ast_custom_function_unregister(&dtmf_mode_function);
- ast_custom_function_unregister(&media_offer_function);
- ast_custom_function_unregister(&chan_pjsip_dial_contacts_function);
- ast_custom_function_unregister(&chan_pjsip_parse_uri_function);
- ast_custom_function_unregister(&session_refresh_function);
- ast_channel_unregister(&chan_pjsip_tech);
- ast_rtp_glue_unregister(&chan_pjsip_rtp_glue);
- return AST_MODULE_LOAD_DECLINE;
- }
- /*! \brief Unload the PJSIP channel from Asterisk */
- static int unload_module(void)
- {
- ao2_cleanup(pjsip_uids_onhold);
- pjsip_uids_onhold = NULL;
- pjsip_channel_cli_unregister();
- ast_sip_session_unregister_supplement(&chan_pjsip_supplement_response);
- ast_sip_session_unregister_supplement(&chan_pjsip_supplement);
- ast_sip_session_unregister_supplement(&pbx_start_supplement);
- ast_sip_session_unregister_supplement(&chan_pjsip_ack_supplement);
- ast_sip_session_unregister_supplement(&call_pickup_supplement);
- ast_custom_function_unregister(&dtmf_mode_function);
- ast_custom_function_unregister(&media_offer_function);
- ast_custom_function_unregister(&chan_pjsip_dial_contacts_function);
- ast_custom_function_unregister(&chan_pjsip_parse_uri_function);
- ast_custom_function_unregister(&session_refresh_function);
- ast_channel_unregister(&chan_pjsip_tech);
- ao2_ref(chan_pjsip_tech.capabilities, -1);
- ast_rtp_glue_unregister(&chan_pjsip_rtp_glue);
- return 0;
- }
- AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Channel Driver",
- .support_level = AST_MODULE_SUPPORT_CORE,
- .load = load_module,
- .unload = unload_module,
- .load_pri = AST_MODPRI_CHANNEL_DRIVER,
- );
|