chan_pjsip.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \author Joshua Colp <jcolp@digium.com>
  21. *
  22. * \brief PSJIP SIP Channel Driver
  23. *
  24. * \ingroup channel_drivers
  25. */
  26. /*** MODULEINFO
  27. <depend>pjproject</depend>
  28. <depend>res_pjsip</depend>
  29. <depend>res_pjsip_session</depend>
  30. <support_level>core</support_level>
  31. ***/
  32. #include "asterisk.h"
  33. #include <pjsip.h>
  34. #include <pjsip_ua.h>
  35. #include <pjlib.h>
  36. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  37. #include "asterisk/lock.h"
  38. #include "asterisk/channel.h"
  39. #include "asterisk/module.h"
  40. #include "asterisk/pbx.h"
  41. #include "asterisk/rtp_engine.h"
  42. #include "asterisk/acl.h"
  43. #include "asterisk/callerid.h"
  44. #include "asterisk/file.h"
  45. #include "asterisk/cli.h"
  46. #include "asterisk/app.h"
  47. #include "asterisk/musiconhold.h"
  48. #include "asterisk/causes.h"
  49. #include "asterisk/taskprocessor.h"
  50. #include "asterisk/dsp.h"
  51. #include "asterisk/stasis_endpoints.h"
  52. #include "asterisk/stasis_channels.h"
  53. #include "asterisk/indications.h"
  54. #include "asterisk/format_cache.h"
  55. #include "asterisk/translate.h"
  56. #include "asterisk/threadstorage.h"
  57. #include "asterisk/features_config.h"
  58. #include "asterisk/pickup.h"
  59. #include "asterisk/test.h"
  60. #include "asterisk/res_pjsip.h"
  61. #include "asterisk/res_pjsip_session.h"
  62. #include "pjsip/include/chan_pjsip.h"
  63. #include "pjsip/include/dialplan_functions.h"
  64. AST_THREADSTORAGE(uniqueid_threadbuf);
  65. #define UNIQUEID_BUFSIZE 256
  66. static const char channel_type[] = "PJSIP";
  67. static unsigned int chan_idx;
  68. static void chan_pjsip_pvt_dtor(void *obj)
  69. {
  70. struct chan_pjsip_pvt *pvt = obj;
  71. int i;
  72. for (i = 0; i < SIP_MEDIA_SIZE; ++i) {
  73. ao2_cleanup(pvt->media[i]);
  74. pvt->media[i] = NULL;
  75. }
  76. }
  77. /* \brief Asterisk core interaction functions */
  78. 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);
  79. static int chan_pjsip_sendtext(struct ast_channel *ast, const char *text);
  80. static int chan_pjsip_digit_begin(struct ast_channel *ast, char digit);
  81. static int chan_pjsip_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
  82. static int chan_pjsip_call(struct ast_channel *ast, const char *dest, int timeout);
  83. static int chan_pjsip_hangup(struct ast_channel *ast);
  84. static int chan_pjsip_answer(struct ast_channel *ast);
  85. static struct ast_frame *chan_pjsip_read(struct ast_channel *ast);
  86. static int chan_pjsip_write(struct ast_channel *ast, struct ast_frame *f);
  87. static int chan_pjsip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
  88. static int chan_pjsip_transfer(struct ast_channel *ast, const char *target);
  89. static int chan_pjsip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
  90. static int chan_pjsip_devicestate(const char *data);
  91. static int chan_pjsip_queryoption(struct ast_channel *ast, int option, void *data, int *datalen);
  92. static const char *chan_pjsip_get_uniqueid(struct ast_channel *ast);
  93. /*! \brief PBX interface structure for channel registration */
  94. struct ast_channel_tech chan_pjsip_tech = {
  95. .type = channel_type,
  96. .description = "PJSIP Channel Driver",
  97. .requester = chan_pjsip_request,
  98. .send_text = chan_pjsip_sendtext,
  99. .send_digit_begin = chan_pjsip_digit_begin,
  100. .send_digit_end = chan_pjsip_digit_end,
  101. .call = chan_pjsip_call,
  102. .hangup = chan_pjsip_hangup,
  103. .answer = chan_pjsip_answer,
  104. .read = chan_pjsip_read,
  105. .write = chan_pjsip_write,
  106. .write_video = chan_pjsip_write,
  107. .exception = chan_pjsip_read,
  108. .indicate = chan_pjsip_indicate,
  109. .transfer = chan_pjsip_transfer,
  110. .fixup = chan_pjsip_fixup,
  111. .devicestate = chan_pjsip_devicestate,
  112. .queryoption = chan_pjsip_queryoption,
  113. .func_channel_read = pjsip_acf_channel_read,
  114. .get_pvt_uniqueid = chan_pjsip_get_uniqueid,
  115. .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER
  116. };
  117. /*! \brief SIP session interaction functions */
  118. static void chan_pjsip_session_begin(struct ast_sip_session *session);
  119. static void chan_pjsip_session_end(struct ast_sip_session *session);
  120. static int chan_pjsip_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
  121. static void chan_pjsip_incoming_response(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
  122. /*! \brief SIP session supplement structure */
  123. static struct ast_sip_session_supplement chan_pjsip_supplement = {
  124. .method = "INVITE",
  125. .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL,
  126. .session_begin = chan_pjsip_session_begin,
  127. .session_end = chan_pjsip_session_end,
  128. .incoming_request = chan_pjsip_incoming_request,
  129. .incoming_response = chan_pjsip_incoming_response,
  130. /* It is important that this supplement runs after media has been negotiated */
  131. .response_priority = AST_SIP_SESSION_AFTER_MEDIA,
  132. };
  133. static int chan_pjsip_incoming_ack(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
  134. static struct ast_sip_session_supplement chan_pjsip_ack_supplement = {
  135. .method = "ACK",
  136. .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL,
  137. .incoming_request = chan_pjsip_incoming_ack,
  138. };
  139. /*! \brief Function called by RTP engine to get local audio RTP peer */
  140. static enum ast_rtp_glue_result chan_pjsip_get_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
  141. {
  142. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
  143. struct chan_pjsip_pvt *pvt = channel->pvt;
  144. struct ast_sip_endpoint *endpoint;
  145. if (!pvt || !channel->session || !pvt->media[SIP_MEDIA_AUDIO]->rtp) {
  146. return AST_RTP_GLUE_RESULT_FORBID;
  147. }
  148. endpoint = channel->session->endpoint;
  149. *instance = pvt->media[SIP_MEDIA_AUDIO]->rtp;
  150. ao2_ref(*instance, +1);
  151. ast_assert(endpoint != NULL);
  152. if (endpoint->media.rtp.encryption != AST_SIP_MEDIA_ENCRYPT_NONE) {
  153. return AST_RTP_GLUE_RESULT_FORBID;
  154. }
  155. if (endpoint->media.direct_media.enabled) {
  156. return AST_RTP_GLUE_RESULT_REMOTE;
  157. }
  158. return AST_RTP_GLUE_RESULT_LOCAL;
  159. }
  160. /*! \brief Function called by RTP engine to get local video RTP peer */
  161. static enum ast_rtp_glue_result chan_pjsip_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
  162. {
  163. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
  164. struct chan_pjsip_pvt *pvt = channel->pvt;
  165. struct ast_sip_endpoint *endpoint;
  166. if (!pvt || !channel->session || !pvt->media[SIP_MEDIA_VIDEO]->rtp) {
  167. return AST_RTP_GLUE_RESULT_FORBID;
  168. }
  169. endpoint = channel->session->endpoint;
  170. *instance = pvt->media[SIP_MEDIA_VIDEO]->rtp;
  171. ao2_ref(*instance, +1);
  172. ast_assert(endpoint != NULL);
  173. if (endpoint->media.rtp.encryption != AST_SIP_MEDIA_ENCRYPT_NONE) {
  174. return AST_RTP_GLUE_RESULT_FORBID;
  175. }
  176. return AST_RTP_GLUE_RESULT_LOCAL;
  177. }
  178. /*! \brief Function called by RTP engine to get peer capabilities */
  179. static void chan_pjsip_get_codec(struct ast_channel *chan, struct ast_format_cap *result)
  180. {
  181. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
  182. ast_format_cap_append_from_cap(result, channel->session->endpoint->media.codecs, AST_MEDIA_TYPE_UNKNOWN);
  183. }
  184. static int send_direct_media_request(void *data)
  185. {
  186. struct ast_sip_session *session = data;
  187. int res;
  188. res = ast_sip_session_refresh(session, NULL, NULL, NULL,
  189. session->endpoint->media.direct_media.method, 1);
  190. ao2_ref(session, -1);
  191. return res;
  192. }
  193. /*! \brief Destructor function for \ref transport_info_data */
  194. static void transport_info_destroy(void *obj)
  195. {
  196. struct transport_info_data *data = obj;
  197. ast_free(data);
  198. }
  199. /*! \brief Datastore used to store local/remote addresses for the
  200. * INVITE request that created the PJSIP channel */
  201. static struct ast_datastore_info transport_info = {
  202. .type = "chan_pjsip_transport_info",
  203. .destroy = transport_info_destroy,
  204. };
  205. static struct ast_datastore_info direct_media_mitigation_info = { };
  206. static int direct_media_mitigate_glare(struct ast_sip_session *session)
  207. {
  208. RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
  209. if (session->endpoint->media.direct_media.glare_mitigation ==
  210. AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_NONE) {
  211. return 0;
  212. }
  213. datastore = ast_sip_session_get_datastore(session, "direct_media_glare_mitigation");
  214. if (!datastore) {
  215. return 0;
  216. }
  217. /* Removing the datastore ensures we won't try to mitigate glare on subsequent reinvites */
  218. ast_sip_session_remove_datastore(session, "direct_media_glare_mitigation");
  219. if ((session->endpoint->media.direct_media.glare_mitigation ==
  220. AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_OUTGOING &&
  221. session->inv_session->role == PJSIP_ROLE_UAC) ||
  222. (session->endpoint->media.direct_media.glare_mitigation ==
  223. AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_INCOMING &&
  224. session->inv_session->role == PJSIP_ROLE_UAS)) {
  225. return 1;
  226. }
  227. return 0;
  228. }
  229. static int check_for_rtp_changes(struct ast_channel *chan, struct ast_rtp_instance *rtp,
  230. struct ast_sip_session_media *media, int rtcp_fd)
  231. {
  232. int changed = 0;
  233. if (rtp) {
  234. changed = ast_rtp_instance_get_and_cmp_remote_address(rtp, &media->direct_media_addr);
  235. if (media->rtp) {
  236. ast_channel_set_fd(chan, rtcp_fd, -1);
  237. ast_rtp_instance_set_prop(media->rtp, AST_RTP_PROPERTY_RTCP, 0);
  238. }
  239. } else if (!ast_sockaddr_isnull(&media->direct_media_addr)){
  240. ast_sockaddr_setnull(&media->direct_media_addr);
  241. changed = 1;
  242. if (media->rtp) {
  243. ast_rtp_instance_set_prop(media->rtp, AST_RTP_PROPERTY_RTCP, 1);
  244. ast_channel_set_fd(chan, rtcp_fd, ast_rtp_instance_fd(media->rtp, 1));
  245. }
  246. }
  247. return changed;
  248. }
  249. /*! \brief Function called by RTP engine to change where the remote party should send media */
  250. static int chan_pjsip_set_rtp_peer(struct ast_channel *chan,
  251. struct ast_rtp_instance *rtp,
  252. struct ast_rtp_instance *vrtp,
  253. struct ast_rtp_instance *tpeer,
  254. const struct ast_format_cap *cap,
  255. int nat_active)
  256. {
  257. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
  258. struct chan_pjsip_pvt *pvt = channel->pvt;
  259. struct ast_sip_session *session = channel->session;
  260. int changed = 0;
  261. /* Don't try to do any direct media shenanigans on early bridges */
  262. if ((rtp || vrtp || tpeer) && !ast_channel_is_bridged(chan)) {
  263. ast_debug(4, "Disregarding setting RTP on %s: channel is not bridged\n", ast_channel_name(chan));
  264. return 0;
  265. }
  266. if (nat_active && session->endpoint->media.direct_media.disable_on_nat) {
  267. ast_debug(4, "Disregarding setting RTP on %s: NAT is active\n", ast_channel_name(chan));
  268. return 0;
  269. }
  270. if (pvt->media[SIP_MEDIA_AUDIO]) {
  271. changed |= check_for_rtp_changes(chan, rtp, pvt->media[SIP_MEDIA_AUDIO], 1);
  272. }
  273. if (pvt->media[SIP_MEDIA_VIDEO]) {
  274. changed |= check_for_rtp_changes(chan, vrtp, pvt->media[SIP_MEDIA_VIDEO], 3);
  275. }
  276. if (direct_media_mitigate_glare(session)) {
  277. ast_debug(4, "Disregarding setting RTP on %s: mitigating re-INVITE glare\n", ast_channel_name(chan));
  278. return 0;
  279. }
  280. if (cap && ast_format_cap_count(cap) && !ast_format_cap_identical(session->direct_media_cap, cap)) {
  281. ast_format_cap_remove_by_type(session->direct_media_cap, AST_MEDIA_TYPE_UNKNOWN);
  282. ast_format_cap_append_from_cap(session->direct_media_cap, cap, AST_MEDIA_TYPE_UNKNOWN);
  283. changed = 1;
  284. }
  285. if (changed) {
  286. ao2_ref(session, +1);
  287. ast_debug(4, "RTP changed on %s; initiating direct media update\n", ast_channel_name(chan));
  288. if (ast_sip_push_task(session->serializer, send_direct_media_request, session)) {
  289. ao2_cleanup(session);
  290. }
  291. }
  292. return 0;
  293. }
  294. /*! \brief Local glue for interacting with the RTP engine core */
  295. static struct ast_rtp_glue chan_pjsip_rtp_glue = {
  296. .type = "PJSIP",
  297. .get_rtp_info = chan_pjsip_get_rtp_peer,
  298. .get_vrtp_info = chan_pjsip_get_vrtp_peer,
  299. .get_codec = chan_pjsip_get_codec,
  300. .update_peer = chan_pjsip_set_rtp_peer,
  301. };
  302. static void set_channel_on_rtp_instance(struct chan_pjsip_pvt *pvt, const char *channel_id)
  303. {
  304. if (pvt->media[SIP_MEDIA_AUDIO] && pvt->media[SIP_MEDIA_AUDIO]->rtp) {
  305. ast_rtp_instance_set_channel_id(pvt->media[SIP_MEDIA_AUDIO]->rtp, channel_id);
  306. }
  307. if (pvt->media[SIP_MEDIA_VIDEO] && pvt->media[SIP_MEDIA_VIDEO]->rtp) {
  308. ast_rtp_instance_set_channel_id(pvt->media[SIP_MEDIA_VIDEO]->rtp, channel_id);
  309. }
  310. }
  311. /*! \brief Function called to create a new PJSIP Asterisk channel */
  312. 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)
  313. {
  314. struct ast_channel *chan;
  315. struct ast_format_cap *caps;
  316. RAII_VAR(struct chan_pjsip_pvt *, pvt, NULL, ao2_cleanup);
  317. struct ast_sip_channel_pvt *channel;
  318. struct ast_variable *var;
  319. if (!(pvt = ao2_alloc(sizeof(*pvt), chan_pjsip_pvt_dtor))) {
  320. return NULL;
  321. }
  322. caps = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  323. if (!caps) {
  324. return NULL;
  325. }
  326. chan = ast_channel_alloc_with_endpoint(1, state,
  327. S_COR(session->id.number.valid, session->id.number.str, ""),
  328. S_COR(session->id.name.valid, session->id.name.str, ""),
  329. session->endpoint->accountcode, "", "", assignedids, requestor, 0,
  330. session->endpoint->persistent, "PJSIP/%s-%08x",
  331. ast_sorcery_object_get_id(session->endpoint),
  332. (unsigned) ast_atomic_fetchadd_int((int *) &chan_idx, +1));
  333. if (!chan) {
  334. ao2_ref(caps, -1);
  335. return NULL;
  336. }
  337. ast_channel_tech_set(chan, &chan_pjsip_tech);
  338. if (!(channel = ast_sip_channel_pvt_alloc(pvt, session))) {
  339. ao2_ref(caps, -1);
  340. ast_channel_unlock(chan);
  341. ast_hangup(chan);
  342. return NULL;
  343. }
  344. ast_channel_stage_snapshot(chan);
  345. ast_channel_tech_pvt_set(chan, channel);
  346. if (!ast_format_cap_count(session->req_caps) ||
  347. !ast_format_cap_iscompatible(session->req_caps, session->endpoint->media.codecs)) {
  348. ast_format_cap_append_from_cap(caps, session->endpoint->media.codecs, AST_MEDIA_TYPE_UNKNOWN);
  349. } else {
  350. ast_format_cap_append_from_cap(caps, session->req_caps, AST_MEDIA_TYPE_UNKNOWN);
  351. }
  352. ast_channel_nativeformats_set(chan, caps);
  353. if (!ast_format_cap_empty(caps)) {
  354. struct ast_format *fmt;
  355. fmt = ast_format_cap_get_best_by_type(caps, AST_MEDIA_TYPE_AUDIO);
  356. if (!fmt) {
  357. /* Since our capabilities aren't empty, this will succeed */
  358. fmt = ast_format_cap_get_format(caps, 0);
  359. }
  360. ast_channel_set_writeformat(chan, fmt);
  361. ast_channel_set_rawwriteformat(chan, fmt);
  362. ast_channel_set_readformat(chan, fmt);
  363. ast_channel_set_rawreadformat(chan, fmt);
  364. ao2_ref(fmt, -1);
  365. }
  366. ao2_ref(caps, -1);
  367. if (state == AST_STATE_RING) {
  368. ast_channel_rings_set(chan, 1);
  369. }
  370. ast_channel_adsicpe_set(chan, AST_ADSI_UNAVAILABLE);
  371. ast_party_id_copy(&ast_channel_caller(chan)->id, &session->id);
  372. ast_party_id_copy(&ast_channel_caller(chan)->ani, &session->id);
  373. ast_channel_context_set(chan, session->endpoint->context);
  374. ast_channel_exten_set(chan, S_OR(exten, "s"));
  375. ast_channel_priority_set(chan, 1);
  376. ast_channel_callgroup_set(chan, session->endpoint->pickup.callgroup);
  377. ast_channel_pickupgroup_set(chan, session->endpoint->pickup.pickupgroup);
  378. ast_channel_named_callgroups_set(chan, session->endpoint->pickup.named_callgroups);
  379. ast_channel_named_pickupgroups_set(chan, session->endpoint->pickup.named_pickupgroups);
  380. if (!ast_strlen_zero(session->endpoint->language)) {
  381. ast_channel_language_set(chan, session->endpoint->language);
  382. }
  383. if (!ast_strlen_zero(session->endpoint->zone)) {
  384. struct ast_tone_zone *zone = ast_get_indication_zone(session->endpoint->zone);
  385. if (!zone) {
  386. ast_log(LOG_ERROR, "Unknown country code '%s' for tonezone. Check indications.conf for available country codes.\n", session->endpoint->zone);
  387. }
  388. ast_channel_zone_set(chan, zone);
  389. }
  390. for (var = session->endpoint->channel_vars; var; var = var->next) {
  391. char buf[512];
  392. pbx_builtin_setvar_helper(chan, var->name, ast_get_encoded_str(
  393. var->value, buf, sizeof(buf)));
  394. }
  395. ast_channel_stage_snapshot_done(chan);
  396. ast_channel_unlock(chan);
  397. /* If res_pjsip_session is ever updated to create/destroy ast_sip_session_media
  398. * during a call such as if multiple same-type stream support is introduced,
  399. * these will need to be recaptured as well */
  400. pvt->media[SIP_MEDIA_AUDIO] = ao2_find(session->media, "audio", OBJ_KEY);
  401. pvt->media[SIP_MEDIA_VIDEO] = ao2_find(session->media, "video", OBJ_KEY);
  402. set_channel_on_rtp_instance(pvt, ast_channel_uniqueid(chan));
  403. return chan;
  404. }
  405. static int answer(void *data)
  406. {
  407. pj_status_t status = PJ_SUCCESS;
  408. pjsip_tx_data *packet = NULL;
  409. struct ast_sip_session *session = data;
  410. if (session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
  411. return 0;
  412. }
  413. pjsip_dlg_inc_lock(session->inv_session->dlg);
  414. if (session->inv_session->invite_tsx) {
  415. status = pjsip_inv_answer(session->inv_session, 200, NULL, NULL, &packet);
  416. } else {
  417. ast_log(LOG_ERROR,"Cannot answer '%s' because there is no associated SIP transaction\n",
  418. ast_channel_name(session->channel));
  419. }
  420. pjsip_dlg_dec_lock(session->inv_session->dlg);
  421. if (status == PJ_SUCCESS && packet) {
  422. ast_sip_session_send_response(session, packet);
  423. }
  424. return (status == PJ_SUCCESS) ? 0 : -1;
  425. }
  426. /*! \brief Function called by core when we should answer a PJSIP session */
  427. static int chan_pjsip_answer(struct ast_channel *ast)
  428. {
  429. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
  430. struct ast_sip_session *session;
  431. if (ast_channel_state(ast) == AST_STATE_UP) {
  432. return 0;
  433. }
  434. ast_setstate(ast, AST_STATE_UP);
  435. session = ao2_bump(channel->session);
  436. /* the answer task needs to be pushed synchronously otherwise a race condition
  437. can occur between this thread and bridging (specifically when native bridging
  438. attempts to do direct media) */
  439. ast_channel_unlock(ast);
  440. if (ast_sip_push_task_synchronous(session->serializer, answer, session)) {
  441. ast_log(LOG_WARNING, "Unable to push answer task to the threadpool. Cannot answer call\n");
  442. ao2_ref(session, -1);
  443. ast_channel_lock(ast);
  444. return -1;
  445. }
  446. ao2_ref(session, -1);
  447. ast_channel_lock(ast);
  448. return 0;
  449. }
  450. /*! \brief Internal helper function called when CNG tone is detected */
  451. static struct ast_frame *chan_pjsip_cng_tone_detected(struct ast_sip_session *session, struct ast_frame *f)
  452. {
  453. const char *target_context;
  454. int exists;
  455. /* If we only needed this DSP for fax detection purposes we can just drop it now */
  456. if (session->endpoint->dtmf == AST_SIP_DTMF_INBAND || session->endpoint->dtmf == AST_SIP_DTMF_AUTO) {
  457. ast_dsp_set_features(session->dsp, DSP_FEATURE_DIGIT_DETECT);
  458. } else {
  459. ast_dsp_free(session->dsp);
  460. session->dsp = NULL;
  461. }
  462. /* If already executing in the fax extension don't do anything */
  463. if (!strcmp(ast_channel_exten(session->channel), "fax")) {
  464. return f;
  465. }
  466. target_context = S_OR(ast_channel_macrocontext(session->channel), ast_channel_context(session->channel));
  467. /* We need to unlock the channel here because ast_exists_extension has the
  468. * potential to start and stop an autoservice on the channel. Such action
  469. * is prone to deadlock if the channel is locked.
  470. */
  471. ast_channel_unlock(session->channel);
  472. exists = ast_exists_extension(session->channel, target_context, "fax", 1,
  473. S_COR(ast_channel_caller(session->channel)->id.number.valid,
  474. ast_channel_caller(session->channel)->id.number.str, NULL));
  475. ast_channel_lock(session->channel);
  476. if (exists) {
  477. ast_verb(2, "Redirecting '%s' to fax extension due to CNG detection\n",
  478. ast_channel_name(session->channel));
  479. pbx_builtin_setvar_helper(session->channel, "FAXEXTEN", ast_channel_exten(session->channel));
  480. if (ast_async_goto(session->channel, target_context, "fax", 1)) {
  481. ast_log(LOG_ERROR, "Failed to async goto '%s' into fax extension in '%s'\n",
  482. ast_channel_name(session->channel), target_context);
  483. }
  484. ast_frfree(f);
  485. f = &ast_null_frame;
  486. } else {
  487. ast_log(LOG_NOTICE, "FAX CNG detected on '%s' but no fax extension in '%s'\n",
  488. ast_channel_name(session->channel), target_context);
  489. }
  490. return f;
  491. }
  492. /*! \brief Function called by core to read any waiting frames */
  493. static struct ast_frame *chan_pjsip_read(struct ast_channel *ast)
  494. {
  495. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
  496. struct chan_pjsip_pvt *pvt = channel->pvt;
  497. struct ast_frame *f;
  498. struct ast_sip_session_media *media = NULL;
  499. int rtcp = 0;
  500. int fdno = ast_channel_fdno(ast);
  501. switch (fdno) {
  502. case 0:
  503. media = pvt->media[SIP_MEDIA_AUDIO];
  504. break;
  505. case 1:
  506. media = pvt->media[SIP_MEDIA_AUDIO];
  507. rtcp = 1;
  508. break;
  509. case 2:
  510. media = pvt->media[SIP_MEDIA_VIDEO];
  511. break;
  512. case 3:
  513. media = pvt->media[SIP_MEDIA_VIDEO];
  514. rtcp = 1;
  515. break;
  516. }
  517. if (!media || !media->rtp) {
  518. return &ast_null_frame;
  519. }
  520. if (!(f = ast_rtp_instance_read(media->rtp, rtcp))) {
  521. return f;
  522. }
  523. if (f->frametype != AST_FRAME_VOICE) {
  524. return f;
  525. }
  526. if (channel->session->dsp) {
  527. f = ast_dsp_process(ast, channel->session->dsp, f);
  528. if (f && (f->frametype == AST_FRAME_DTMF)) {
  529. if (f->subclass.integer == 'f') {
  530. ast_debug(3, "Fax CNG detected on %s\n", ast_channel_name(ast));
  531. f = chan_pjsip_cng_tone_detected(channel->session, f);
  532. } else {
  533. ast_debug(3, "* Detected inband DTMF '%c' on '%s'\n", f->subclass.integer,
  534. ast_channel_name(ast));
  535. }
  536. }
  537. }
  538. return f;
  539. }
  540. /*! \brief Function called by core to write frames */
  541. static int chan_pjsip_write(struct ast_channel *ast, struct ast_frame *frame)
  542. {
  543. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
  544. struct chan_pjsip_pvt *pvt = channel->pvt;
  545. struct ast_sip_session_media *media;
  546. int res = 0;
  547. switch (frame->frametype) {
  548. case AST_FRAME_VOICE:
  549. media = pvt->media[SIP_MEDIA_AUDIO];
  550. if (!media) {
  551. return 0;
  552. }
  553. if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), frame->subclass.format) == AST_FORMAT_CMP_NOT_EQUAL) {
  554. struct ast_str *cap_buf = ast_str_alloca(128);
  555. struct ast_str *write_transpath = ast_str_alloca(256);
  556. struct ast_str *read_transpath = ast_str_alloca(256);
  557. ast_log(LOG_WARNING,
  558. "Channel %s asked to send %s frame when native formats are %s (rd:%s->%s;%s wr:%s->%s;%s)\n",
  559. ast_channel_name(ast),
  560. ast_format_get_name(frame->subclass.format),
  561. ast_format_cap_get_names(ast_channel_nativeformats(ast), &cap_buf),
  562. ast_format_get_name(ast_channel_rawreadformat(ast)),
  563. ast_format_get_name(ast_channel_readformat(ast)),
  564. ast_translate_path_to_str(ast_channel_readtrans(ast), &read_transpath),
  565. ast_format_get_name(ast_channel_writeformat(ast)),
  566. ast_format_get_name(ast_channel_rawwriteformat(ast)),
  567. ast_translate_path_to_str(ast_channel_writetrans(ast), &write_transpath));
  568. return 0;
  569. }
  570. if (media->rtp) {
  571. res = ast_rtp_instance_write(media->rtp, frame);
  572. }
  573. break;
  574. case AST_FRAME_VIDEO:
  575. if ((media = pvt->media[SIP_MEDIA_VIDEO]) && media->rtp) {
  576. res = ast_rtp_instance_write(media->rtp, frame);
  577. }
  578. break;
  579. case AST_FRAME_MODEM:
  580. break;
  581. default:
  582. ast_log(LOG_WARNING, "Can't send %u type frames with PJSIP\n", frame->frametype);
  583. break;
  584. }
  585. return res;
  586. }
  587. /*! \brief Function called by core to change the underlying owner channel */
  588. static int chan_pjsip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  589. {
  590. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(newchan);
  591. struct chan_pjsip_pvt *pvt = channel->pvt;
  592. if (channel->session->channel != oldchan) {
  593. return -1;
  594. }
  595. /*
  596. * The masquerade has suspended the channel's session
  597. * serializer so we can safely change it outside of
  598. * the serializer thread.
  599. */
  600. channel->session->channel = newchan;
  601. set_channel_on_rtp_instance(pvt, ast_channel_uniqueid(newchan));
  602. return 0;
  603. }
  604. /*! AO2 hash function for on hold UIDs */
  605. static int uid_hold_hash_fn(const void *obj, const int flags)
  606. {
  607. const char *key = obj;
  608. switch (flags & OBJ_SEARCH_MASK) {
  609. case OBJ_SEARCH_KEY:
  610. break;
  611. case OBJ_SEARCH_OBJECT:
  612. break;
  613. default:
  614. /* Hash can only work on something with a full key. */
  615. ast_assert(0);
  616. return 0;
  617. }
  618. return ast_str_hash(key);
  619. }
  620. /*! AO2 sort function for on hold UIDs */
  621. static int uid_hold_sort_fn(const void *obj_left, const void *obj_right, const int flags)
  622. {
  623. const char *left = obj_left;
  624. const char *right = obj_right;
  625. int cmp;
  626. switch (flags & OBJ_SEARCH_MASK) {
  627. case OBJ_SEARCH_OBJECT:
  628. case OBJ_SEARCH_KEY:
  629. cmp = strcmp(left, right);
  630. break;
  631. case OBJ_SEARCH_PARTIAL_KEY:
  632. cmp = strncmp(left, right, strlen(right));
  633. break;
  634. default:
  635. /* Sort can only work on something with a full or partial key. */
  636. ast_assert(0);
  637. cmp = 0;
  638. break;
  639. }
  640. return cmp;
  641. }
  642. static struct ao2_container *pjsip_uids_onhold;
  643. /*!
  644. * \brief Add a channel ID to the list of PJSIP channels on hold
  645. *
  646. * \param chan_uid - Unique ID of the channel being put into the hold list
  647. *
  648. * \retval 0 Channel has been added to or was already in the hold list
  649. * \retval -1 Failed to add channel to the hold list
  650. */
  651. static int chan_pjsip_add_hold(const char *chan_uid)
  652. {
  653. RAII_VAR(char *, hold_uid, NULL, ao2_cleanup);
  654. hold_uid = ao2_find(pjsip_uids_onhold, chan_uid, OBJ_SEARCH_KEY);
  655. if (hold_uid) {
  656. /* Device is already on hold. Nothing to do. */
  657. return 0;
  658. }
  659. /* Device wasn't in hold list already. Create a new one. */
  660. hold_uid = ao2_alloc_options(strlen(chan_uid) + 1, NULL,
  661. AO2_ALLOC_OPT_LOCK_NOLOCK);
  662. if (!hold_uid) {
  663. return -1;
  664. }
  665. ast_copy_string(hold_uid, chan_uid, strlen(chan_uid) + 1);
  666. if (ao2_link(pjsip_uids_onhold, hold_uid) == 0) {
  667. return -1;
  668. }
  669. return 0;
  670. }
  671. /*!
  672. * \brief Remove a channel ID from the list of PJSIP channels on hold
  673. *
  674. * \param chan_uid - Unique ID of the channel being taken out of the hold list
  675. */
  676. static void chan_pjsip_remove_hold(const char *chan_uid)
  677. {
  678. ao2_find(pjsip_uids_onhold, chan_uid, OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA);
  679. }
  680. /*!
  681. * \brief Determine whether a channel ID is in the list of PJSIP channels on hold
  682. *
  683. * \param chan_uid - Channel being checked
  684. *
  685. * \retval 0 The channel is not in the hold list
  686. * \retval 1 The channel is in the hold list
  687. */
  688. static int chan_pjsip_get_hold(const char *chan_uid)
  689. {
  690. RAII_VAR(char *, hold_uid, NULL, ao2_cleanup);
  691. hold_uid = ao2_find(pjsip_uids_onhold, chan_uid, OBJ_SEARCH_KEY);
  692. if (!hold_uid) {
  693. return 0;
  694. }
  695. return 1;
  696. }
  697. /*! \brief Function called to get the device state of an endpoint */
  698. static int chan_pjsip_devicestate(const char *data)
  699. {
  700. RAII_VAR(struct ast_sip_endpoint *, endpoint, ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", data), ao2_cleanup);
  701. enum ast_device_state state = AST_DEVICE_UNKNOWN;
  702. RAII_VAR(struct ast_endpoint_snapshot *, endpoint_snapshot, NULL, ao2_cleanup);
  703. RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
  704. struct ast_devstate_aggregate aggregate;
  705. int num, inuse = 0;
  706. if (!endpoint) {
  707. return AST_DEVICE_INVALID;
  708. }
  709. endpoint_snapshot = ast_endpoint_latest_snapshot(ast_endpoint_get_tech(endpoint->persistent),
  710. ast_endpoint_get_resource(endpoint->persistent));
  711. if (!endpoint_snapshot) {
  712. return AST_DEVICE_INVALID;
  713. }
  714. if (endpoint_snapshot->state == AST_ENDPOINT_OFFLINE) {
  715. state = AST_DEVICE_UNAVAILABLE;
  716. } else if (endpoint_snapshot->state == AST_ENDPOINT_ONLINE) {
  717. state = AST_DEVICE_NOT_INUSE;
  718. }
  719. if (!endpoint_snapshot->num_channels || !(cache = ast_channel_cache())) {
  720. return state;
  721. }
  722. ast_devstate_aggregate_init(&aggregate);
  723. ao2_ref(cache, +1);
  724. for (num = 0; num < endpoint_snapshot->num_channels; num++) {
  725. RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
  726. struct ast_channel_snapshot *snapshot;
  727. msg = stasis_cache_get(cache, ast_channel_snapshot_type(),
  728. endpoint_snapshot->channel_ids[num]);
  729. if (!msg) {
  730. continue;
  731. }
  732. snapshot = stasis_message_data(msg);
  733. if (chan_pjsip_get_hold(snapshot->uniqueid)) {
  734. ast_devstate_aggregate_add(&aggregate, AST_DEVICE_ONHOLD);
  735. } else {
  736. ast_devstate_aggregate_add(&aggregate, ast_state_chan2dev(snapshot->state));
  737. }
  738. if ((snapshot->state == AST_STATE_UP) || (snapshot->state == AST_STATE_RING) ||
  739. (snapshot->state == AST_STATE_BUSY)) {
  740. inuse++;
  741. }
  742. }
  743. if (endpoint->devicestate_busy_at && (inuse == endpoint->devicestate_busy_at)) {
  744. state = AST_DEVICE_BUSY;
  745. } else if (ast_devstate_aggregate_result(&aggregate) != AST_DEVICE_INVALID) {
  746. state = ast_devstate_aggregate_result(&aggregate);
  747. }
  748. return state;
  749. }
  750. /*! \brief Function called to query options on a channel */
  751. static int chan_pjsip_queryoption(struct ast_channel *ast, int option, void *data, int *datalen)
  752. {
  753. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
  754. struct ast_sip_session *session = channel->session;
  755. int res = -1;
  756. enum ast_t38_state state = T38_STATE_UNAVAILABLE;
  757. switch (option) {
  758. case AST_OPTION_T38_STATE:
  759. if (session->endpoint->media.t38.enabled) {
  760. switch (session->t38state) {
  761. case T38_LOCAL_REINVITE:
  762. case T38_PEER_REINVITE:
  763. state = T38_STATE_NEGOTIATING;
  764. break;
  765. case T38_ENABLED:
  766. state = T38_STATE_NEGOTIATED;
  767. break;
  768. case T38_REJECTED:
  769. state = T38_STATE_REJECTED;
  770. break;
  771. default:
  772. state = T38_STATE_UNKNOWN;
  773. break;
  774. }
  775. }
  776. *((enum ast_t38_state *) data) = state;
  777. res = 0;
  778. break;
  779. default:
  780. break;
  781. }
  782. return res;
  783. }
  784. static const char *chan_pjsip_get_uniqueid(struct ast_channel *ast)
  785. {
  786. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
  787. char *uniqueid = ast_threadstorage_get(&uniqueid_threadbuf, UNIQUEID_BUFSIZE);
  788. if (!uniqueid) {
  789. return "";
  790. }
  791. ast_copy_pj_str(uniqueid, &channel->session->inv_session->dlg->call_id->id, UNIQUEID_BUFSIZE);
  792. return uniqueid;
  793. }
  794. struct indicate_data {
  795. struct ast_sip_session *session;
  796. int condition;
  797. int response_code;
  798. void *frame_data;
  799. size_t datalen;
  800. };
  801. static void indicate_data_destroy(void *obj)
  802. {
  803. struct indicate_data *ind_data = obj;
  804. ast_free(ind_data->frame_data);
  805. ao2_ref(ind_data->session, -1);
  806. }
  807. static struct indicate_data *indicate_data_alloc(struct ast_sip_session *session,
  808. int condition, int response_code, const void *frame_data, size_t datalen)
  809. {
  810. struct indicate_data *ind_data = ao2_alloc(sizeof(*ind_data), indicate_data_destroy);
  811. if (!ind_data) {
  812. return NULL;
  813. }
  814. ind_data->frame_data = ast_malloc(datalen);
  815. if (!ind_data->frame_data) {
  816. ao2_ref(ind_data, -1);
  817. return NULL;
  818. }
  819. memcpy(ind_data->frame_data, frame_data, datalen);
  820. ind_data->datalen = datalen;
  821. ind_data->condition = condition;
  822. ind_data->response_code = response_code;
  823. ao2_ref(session, +1);
  824. ind_data->session = session;
  825. return ind_data;
  826. }
  827. static int indicate(void *data)
  828. {
  829. pjsip_tx_data *packet = NULL;
  830. struct indicate_data *ind_data = data;
  831. struct ast_sip_session *session = ind_data->session;
  832. int response_code = ind_data->response_code;
  833. if ((session->inv_session->state != PJSIP_INV_STATE_DISCONNECTED) &&
  834. (pjsip_inv_answer(session->inv_session, response_code, NULL, NULL, &packet) == PJ_SUCCESS)) {
  835. ast_sip_session_send_response(session, packet);
  836. }
  837. ao2_ref(ind_data, -1);
  838. return 0;
  839. }
  840. /*! \brief Send SIP INFO with video update request */
  841. static int transmit_info_with_vidupdate(void *data)
  842. {
  843. const char * xml =
  844. "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n"
  845. " <media_control>\r\n"
  846. " <vc_primitive>\r\n"
  847. " <to_encoder>\r\n"
  848. " <picture_fast_update/>\r\n"
  849. " </to_encoder>\r\n"
  850. " </vc_primitive>\r\n"
  851. " </media_control>\r\n";
  852. const struct ast_sip_body body = {
  853. .type = "application",
  854. .subtype = "media_control+xml",
  855. .body_text = xml
  856. };
  857. RAII_VAR(struct ast_sip_session *, session, data, ao2_cleanup);
  858. struct pjsip_tx_data *tdata;
  859. if (ast_sip_create_request("INFO", session->inv_session->dlg, session->endpoint, NULL, NULL, &tdata)) {
  860. ast_log(LOG_ERROR, "Could not create text video update INFO request\n");
  861. return -1;
  862. }
  863. if (ast_sip_add_body(tdata, &body)) {
  864. ast_log(LOG_ERROR, "Could not add body to text video update INFO request\n");
  865. return -1;
  866. }
  867. ast_sip_session_send_request(session, tdata);
  868. return 0;
  869. }
  870. /*!
  871. * \internal
  872. * \brief TRUE if a COLP update can be sent to the peer.
  873. * \since 13.3.0
  874. *
  875. * \param session The session to see if the COLP update is allowed.
  876. *
  877. * \retval 0 Update is not allowed.
  878. * \retval 1 Update is allowed.
  879. */
  880. static int is_colp_update_allowed(struct ast_sip_session *session)
  881. {
  882. struct ast_party_id connected_id;
  883. int update_allowed = 0;
  884. if (!session->endpoint->id.send_pai && !session->endpoint->id.send_rpid) {
  885. return 0;
  886. }
  887. /*
  888. * Check if privacy allows the update. Check while the channel
  889. * is locked so we can work with the shallow connected_id copy.
  890. */
  891. ast_channel_lock(session->channel);
  892. connected_id = ast_channel_connected_effective_id(session->channel);
  893. if (connected_id.number.valid
  894. && (session->endpoint->id.trust_outbound
  895. || (ast_party_id_presentation(&connected_id) & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED)) {
  896. update_allowed = 1;
  897. }
  898. ast_channel_unlock(session->channel);
  899. return update_allowed;
  900. }
  901. /*! \brief Update connected line information */
  902. static int update_connected_line_information(void *data)
  903. {
  904. struct ast_sip_session *session = data;
  905. if (ast_channel_state(session->channel) == AST_STATE_UP
  906. || session->inv_session->role == PJSIP_ROLE_UAC) {
  907. if (is_colp_update_allowed(session)) {
  908. enum ast_sip_session_refresh_method method;
  909. int generate_new_sdp;
  910. method = session->endpoint->id.refresh_method;
  911. if (session->inv_session->invite_tsx
  912. && (session->inv_session->options & PJSIP_INV_SUPPORT_UPDATE)) {
  913. method = AST_SIP_SESSION_REFRESH_METHOD_UPDATE;
  914. }
  915. /* Only the INVITE method actually needs SDP, UPDATE can do without */
  916. generate_new_sdp = (method == AST_SIP_SESSION_REFRESH_METHOD_INVITE);
  917. ast_sip_session_refresh(session, NULL, NULL, NULL, method, generate_new_sdp);
  918. }
  919. } else if (session->endpoint->rpid_immediate
  920. && session->inv_session->state != PJSIP_INV_STATE_DISCONNECTED
  921. && is_colp_update_allowed(session)) {
  922. int response_code = 0;
  923. if (ast_channel_state(session->channel) == AST_STATE_RING) {
  924. response_code = !session->endpoint->inband_progress ? 180 : 183;
  925. } else if (ast_channel_state(session->channel) == AST_STATE_RINGING) {
  926. response_code = 183;
  927. }
  928. if (response_code) {
  929. struct pjsip_tx_data *packet = NULL;
  930. if (pjsip_inv_answer(session->inv_session, response_code, NULL, NULL, &packet) == PJ_SUCCESS) {
  931. ast_sip_session_send_response(session, packet);
  932. }
  933. }
  934. }
  935. ao2_ref(session, -1);
  936. return 0;
  937. }
  938. /*! \brief Function called by core to ask the channel to indicate some sort of condition */
  939. static int chan_pjsip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
  940. {
  941. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
  942. struct chan_pjsip_pvt *pvt = channel->pvt;
  943. struct ast_sip_session_media *media;
  944. int response_code = 0;
  945. int res = 0;
  946. char *device_buf;
  947. size_t device_buf_size;
  948. switch (condition) {
  949. case AST_CONTROL_RINGING:
  950. if (ast_channel_state(ast) == AST_STATE_RING) {
  951. if (channel->session->endpoint->inband_progress) {
  952. response_code = 183;
  953. res = -1;
  954. } else {
  955. response_code = 180;
  956. }
  957. } else {
  958. res = -1;
  959. }
  960. ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, "PJSIP/%s", ast_sorcery_object_get_id(channel->session->endpoint));
  961. break;
  962. case AST_CONTROL_BUSY:
  963. if (ast_channel_state(ast) != AST_STATE_UP) {
  964. response_code = 486;
  965. } else {
  966. res = -1;
  967. }
  968. break;
  969. case AST_CONTROL_CONGESTION:
  970. if (ast_channel_state(ast) != AST_STATE_UP) {
  971. response_code = 503;
  972. } else {
  973. res = -1;
  974. }
  975. break;
  976. case AST_CONTROL_INCOMPLETE:
  977. if (ast_channel_state(ast) != AST_STATE_UP) {
  978. response_code = 484;
  979. } else {
  980. res = -1;
  981. }
  982. break;
  983. case AST_CONTROL_PROCEEDING:
  984. if (ast_channel_state(ast) != AST_STATE_UP) {
  985. response_code = 100;
  986. } else {
  987. res = -1;
  988. }
  989. break;
  990. case AST_CONTROL_PROGRESS:
  991. if (ast_channel_state(ast) != AST_STATE_UP) {
  992. response_code = 183;
  993. } else {
  994. res = -1;
  995. }
  996. break;
  997. case AST_CONTROL_VIDUPDATE:
  998. media = pvt->media[SIP_MEDIA_VIDEO];
  999. if (media && media->rtp) {
  1000. /* FIXME: Only use this for VP8. Additional work would have to be done to
  1001. * fully support other video codecs */
  1002. if (ast_format_cap_iscompatible_format(ast_channel_nativeformats(ast), ast_format_vp8) != AST_FORMAT_CMP_NOT_EQUAL) {
  1003. /* FIXME Fake RTP write, this will be sent as an RTCP packet. Ideally the
  1004. * RTP engine would provide a way to externally write/schedule RTCP
  1005. * packets */
  1006. struct ast_frame fr;
  1007. fr.frametype = AST_FRAME_CONTROL;
  1008. fr.subclass.integer = AST_CONTROL_VIDUPDATE;
  1009. res = ast_rtp_instance_write(media->rtp, &fr);
  1010. } else {
  1011. ao2_ref(channel->session, +1);
  1012. if (ast_sip_push_task(channel->session->serializer, transmit_info_with_vidupdate, channel->session)) {
  1013. ao2_cleanup(channel->session);
  1014. }
  1015. }
  1016. ast_test_suite_event_notify("AST_CONTROL_VIDUPDATE", "Result: Success");
  1017. } else {
  1018. ast_test_suite_event_notify("AST_CONTROL_VIDUPDATE", "Result: Failure");
  1019. res = -1;
  1020. }
  1021. break;
  1022. case AST_CONTROL_CONNECTED_LINE:
  1023. ao2_ref(channel->session, +1);
  1024. if (ast_sip_push_task(channel->session->serializer, update_connected_line_information, channel->session)) {
  1025. ao2_cleanup(channel->session);
  1026. }
  1027. break;
  1028. case AST_CONTROL_UPDATE_RTP_PEER:
  1029. break;
  1030. case AST_CONTROL_PVT_CAUSE_CODE:
  1031. res = -1;
  1032. break;
  1033. case AST_CONTROL_MASQUERADE_NOTIFY:
  1034. ast_assert(datalen == sizeof(int));
  1035. if (*(int *) data) {
  1036. /*
  1037. * Masquerade is beginning:
  1038. * Wait for session serializer to get suspended.
  1039. */
  1040. ast_channel_unlock(ast);
  1041. ast_sip_session_suspend(channel->session);
  1042. ast_channel_lock(ast);
  1043. } else {
  1044. /*
  1045. * Masquerade is complete:
  1046. * Unsuspend the session serializer.
  1047. */
  1048. ast_sip_session_unsuspend(channel->session);
  1049. }
  1050. break;
  1051. case AST_CONTROL_HOLD:
  1052. chan_pjsip_add_hold(ast_channel_uniqueid(ast));
  1053. device_buf_size = strlen(ast_channel_name(ast)) + 1;
  1054. device_buf = alloca(device_buf_size);
  1055. ast_channel_get_device_name(ast, device_buf, device_buf_size);
  1056. ast_devstate_changed_literal(AST_DEVICE_ONHOLD, 1, device_buf);
  1057. ast_moh_start(ast, data, NULL);
  1058. break;
  1059. case AST_CONTROL_UNHOLD:
  1060. chan_pjsip_remove_hold(ast_channel_uniqueid(ast));
  1061. device_buf_size = strlen(ast_channel_name(ast)) + 1;
  1062. device_buf = alloca(device_buf_size);
  1063. ast_channel_get_device_name(ast, device_buf, device_buf_size);
  1064. ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, 1, device_buf);
  1065. ast_moh_stop(ast);
  1066. break;
  1067. case AST_CONTROL_SRCUPDATE:
  1068. break;
  1069. case AST_CONTROL_SRCCHANGE:
  1070. break;
  1071. case AST_CONTROL_REDIRECTING:
  1072. if (ast_channel_state(ast) != AST_STATE_UP) {
  1073. response_code = 181;
  1074. } else {
  1075. res = -1;
  1076. }
  1077. break;
  1078. case AST_CONTROL_T38_PARAMETERS:
  1079. res = 0;
  1080. if (channel->session->t38state == T38_PEER_REINVITE) {
  1081. const struct ast_control_t38_parameters *parameters = data;
  1082. if (parameters->request_response == AST_T38_REQUEST_PARMS) {
  1083. res = AST_T38_REQUEST_PARMS;
  1084. }
  1085. }
  1086. break;
  1087. case -1:
  1088. res = -1;
  1089. break;
  1090. default:
  1091. ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", condition);
  1092. res = -1;
  1093. break;
  1094. }
  1095. if (response_code) {
  1096. struct indicate_data *ind_data = indicate_data_alloc(channel->session, condition, response_code, data, datalen);
  1097. if (!ind_data || ast_sip_push_task(channel->session->serializer, indicate, ind_data)) {
  1098. ast_log(LOG_NOTICE, "Cannot send response code %d to endpoint %s. Could not queue task properly\n",
  1099. response_code, ast_sorcery_object_get_id(channel->session->endpoint));
  1100. ao2_cleanup(ind_data);
  1101. res = -1;
  1102. }
  1103. }
  1104. return res;
  1105. }
  1106. struct transfer_data {
  1107. struct ast_sip_session *session;
  1108. char *target;
  1109. };
  1110. static void transfer_data_destroy(void *obj)
  1111. {
  1112. struct transfer_data *trnf_data = obj;
  1113. ast_free(trnf_data->target);
  1114. ao2_cleanup(trnf_data->session);
  1115. }
  1116. static struct transfer_data *transfer_data_alloc(struct ast_sip_session *session, const char *target)
  1117. {
  1118. struct transfer_data *trnf_data = ao2_alloc(sizeof(*trnf_data), transfer_data_destroy);
  1119. if (!trnf_data) {
  1120. return NULL;
  1121. }
  1122. if (!(trnf_data->target = ast_strdup(target))) {
  1123. ao2_ref(trnf_data, -1);
  1124. return NULL;
  1125. }
  1126. ao2_ref(session, +1);
  1127. trnf_data->session = session;
  1128. return trnf_data;
  1129. }
  1130. static void transfer_redirect(struct ast_sip_session *session, const char *target)
  1131. {
  1132. pjsip_tx_data *packet;
  1133. enum ast_control_transfer message = AST_TRANSFER_SUCCESS;
  1134. pjsip_contact_hdr *contact;
  1135. pj_str_t tmp;
  1136. if (pjsip_inv_end_session(session->inv_session, 302, NULL, &packet) != PJ_SUCCESS) {
  1137. ast_log(LOG_WARNING, "Failed to redirect PJSIP session for channel %s\n",
  1138. ast_channel_name(session->channel));
  1139. message = AST_TRANSFER_FAILED;
  1140. ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
  1141. return;
  1142. }
  1143. if (!(contact = pjsip_msg_find_hdr(packet->msg, PJSIP_H_CONTACT, NULL))) {
  1144. contact = pjsip_contact_hdr_create(packet->pool);
  1145. }
  1146. pj_strdup2_with_null(packet->pool, &tmp, target);
  1147. if (!(contact->uri = pjsip_parse_uri(packet->pool, tmp.ptr, tmp.slen, PJSIP_PARSE_URI_AS_NAMEADDR))) {
  1148. ast_log(LOG_WARNING, "Failed to parse destination URI '%s' for channel %s\n",
  1149. target, ast_channel_name(session->channel));
  1150. message = AST_TRANSFER_FAILED;
  1151. ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
  1152. pjsip_tx_data_dec_ref(packet);
  1153. return;
  1154. }
  1155. pjsip_msg_add_hdr(packet->msg, (pjsip_hdr *) contact);
  1156. ast_sip_session_send_response(session, packet);
  1157. ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
  1158. }
  1159. static void transfer_refer(struct ast_sip_session *session, const char *target)
  1160. {
  1161. pjsip_evsub *sub;
  1162. enum ast_control_transfer message = AST_TRANSFER_SUCCESS;
  1163. pj_str_t tmp;
  1164. pjsip_tx_data *packet;
  1165. if (pjsip_xfer_create_uac(session->inv_session->dlg, NULL, &sub) != PJ_SUCCESS) {
  1166. message = AST_TRANSFER_FAILED;
  1167. ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
  1168. return;
  1169. }
  1170. if (pjsip_xfer_initiate(sub, pj_cstr(&tmp, target), &packet) != PJ_SUCCESS) {
  1171. message = AST_TRANSFER_FAILED;
  1172. ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
  1173. pjsip_evsub_terminate(sub, PJ_FALSE);
  1174. return;
  1175. }
  1176. pjsip_xfer_send_request(sub, packet);
  1177. ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
  1178. }
  1179. static int transfer(void *data)
  1180. {
  1181. struct transfer_data *trnf_data = data;
  1182. struct ast_sip_endpoint *endpoint = NULL;
  1183. struct ast_sip_contact *contact = NULL;
  1184. const char *target = trnf_data->target;
  1185. /* See if we have an endpoint; if so, use its contact */
  1186. endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", target);
  1187. if (endpoint) {
  1188. contact = ast_sip_location_retrieve_contact_from_aor_list(endpoint->aors);
  1189. if (contact && !ast_strlen_zero(contact->uri)) {
  1190. target = contact->uri;
  1191. }
  1192. }
  1193. if (ast_channel_state(trnf_data->session->channel) == AST_STATE_RING) {
  1194. transfer_redirect(trnf_data->session, target);
  1195. } else {
  1196. transfer_refer(trnf_data->session, target);
  1197. }
  1198. ao2_ref(trnf_data, -1);
  1199. ao2_cleanup(endpoint);
  1200. ao2_cleanup(contact);
  1201. return 0;
  1202. }
  1203. /*! \brief Function called by core for Asterisk initiated transfer */
  1204. static int chan_pjsip_transfer(struct ast_channel *chan, const char *target)
  1205. {
  1206. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
  1207. struct transfer_data *trnf_data = transfer_data_alloc(channel->session, target);
  1208. if (!trnf_data) {
  1209. return -1;
  1210. }
  1211. if (ast_sip_push_task(channel->session->serializer, transfer, trnf_data)) {
  1212. ast_log(LOG_WARNING, "Error requesting transfer\n");
  1213. ao2_cleanup(trnf_data);
  1214. return -1;
  1215. }
  1216. return 0;
  1217. }
  1218. /*! \brief Function called by core to start a DTMF digit */
  1219. static int chan_pjsip_digit_begin(struct ast_channel *chan, char digit)
  1220. {
  1221. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
  1222. struct chan_pjsip_pvt *pvt = channel->pvt;
  1223. struct ast_sip_session_media *media = pvt->media[SIP_MEDIA_AUDIO];
  1224. int res = 0;
  1225. switch (channel->session->endpoint->dtmf) {
  1226. case AST_SIP_DTMF_RFC_4733:
  1227. if (!media || !media->rtp) {
  1228. return -1;
  1229. }
  1230. ast_rtp_instance_dtmf_begin(media->rtp, digit);
  1231. break;
  1232. case AST_SIP_DTMF_AUTO:
  1233. if (!media || !media->rtp || (ast_rtp_instance_dtmf_mode_get(media->rtp) == AST_RTP_DTMF_MODE_INBAND)) {
  1234. return -1;
  1235. }
  1236. ast_rtp_instance_dtmf_begin(media->rtp, digit);
  1237. break;
  1238. case AST_SIP_DTMF_NONE:
  1239. break;
  1240. case AST_SIP_DTMF_INBAND:
  1241. res = -1;
  1242. break;
  1243. default:
  1244. break;
  1245. }
  1246. return res;
  1247. }
  1248. struct info_dtmf_data {
  1249. struct ast_sip_session *session;
  1250. char digit;
  1251. unsigned int duration;
  1252. };
  1253. static void info_dtmf_data_destroy(void *obj)
  1254. {
  1255. struct info_dtmf_data *dtmf_data = obj;
  1256. ao2_ref(dtmf_data->session, -1);
  1257. }
  1258. static struct info_dtmf_data *info_dtmf_data_alloc(struct ast_sip_session *session, char digit, unsigned int duration)
  1259. {
  1260. struct info_dtmf_data *dtmf_data = ao2_alloc(sizeof(*dtmf_data), info_dtmf_data_destroy);
  1261. if (!dtmf_data) {
  1262. return NULL;
  1263. }
  1264. ao2_ref(session, +1);
  1265. dtmf_data->session = session;
  1266. dtmf_data->digit = digit;
  1267. dtmf_data->duration = duration;
  1268. return dtmf_data;
  1269. }
  1270. static int transmit_info_dtmf(void *data)
  1271. {
  1272. RAII_VAR(struct info_dtmf_data *, dtmf_data, data, ao2_cleanup);
  1273. struct ast_sip_session *session = dtmf_data->session;
  1274. struct pjsip_tx_data *tdata;
  1275. RAII_VAR(struct ast_str *, body_text, NULL, ast_free_ptr);
  1276. struct ast_sip_body body = {
  1277. .type = "application",
  1278. .subtype = "dtmf-relay",
  1279. };
  1280. if (!(body_text = ast_str_create(32))) {
  1281. ast_log(LOG_ERROR, "Could not allocate buffer for INFO DTMF.\n");
  1282. return -1;
  1283. }
  1284. ast_str_set(&body_text, 0, "Signal=%c\r\nDuration=%u\r\n", dtmf_data->digit, dtmf_data->duration);
  1285. body.body_text = ast_str_buffer(body_text);
  1286. if (ast_sip_create_request("INFO", session->inv_session->dlg, session->endpoint, NULL, NULL, &tdata)) {
  1287. ast_log(LOG_ERROR, "Could not create DTMF INFO request\n");
  1288. return -1;
  1289. }
  1290. if (ast_sip_add_body(tdata, &body)) {
  1291. ast_log(LOG_ERROR, "Could not add body to DTMF INFO request\n");
  1292. pjsip_tx_data_dec_ref(tdata);
  1293. return -1;
  1294. }
  1295. ast_sip_session_send_request(session, tdata);
  1296. return 0;
  1297. }
  1298. /*! \brief Function called by core to stop a DTMF digit */
  1299. static int chan_pjsip_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
  1300. {
  1301. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
  1302. struct chan_pjsip_pvt *pvt = channel->pvt;
  1303. struct ast_sip_session_media *media = pvt->media[SIP_MEDIA_AUDIO];
  1304. int res = 0;
  1305. switch (channel->session->endpoint->dtmf) {
  1306. case AST_SIP_DTMF_INFO:
  1307. {
  1308. struct info_dtmf_data *dtmf_data = info_dtmf_data_alloc(channel->session, digit, duration);
  1309. if (!dtmf_data) {
  1310. return -1;
  1311. }
  1312. if (ast_sip_push_task(channel->session->serializer, transmit_info_dtmf, dtmf_data)) {
  1313. ast_log(LOG_WARNING, "Error sending DTMF via INFO.\n");
  1314. ao2_cleanup(dtmf_data);
  1315. return -1;
  1316. }
  1317. break;
  1318. }
  1319. case AST_SIP_DTMF_RFC_4733:
  1320. if (!media || !media->rtp) {
  1321. return -1;
  1322. }
  1323. ast_rtp_instance_dtmf_end_with_duration(media->rtp, digit, duration);
  1324. break;
  1325. case AST_SIP_DTMF_AUTO:
  1326. if (!media || !media->rtp || (ast_rtp_instance_dtmf_mode_get(media->rtp) == AST_RTP_DTMF_MODE_INBAND)) {
  1327. return -1;
  1328. }
  1329. ast_rtp_instance_dtmf_end_with_duration(media->rtp, digit, duration);
  1330. break;
  1331. case AST_SIP_DTMF_NONE:
  1332. break;
  1333. case AST_SIP_DTMF_INBAND:
  1334. res = -1;
  1335. break;
  1336. }
  1337. return res;
  1338. }
  1339. static void update_initial_connected_line(struct ast_sip_session *session)
  1340. {
  1341. struct ast_party_connected_line connected;
  1342. /*
  1343. * Use the channel CALLERID() as the initial connected line data.
  1344. * The core or a predial handler may have supplied missing values
  1345. * from the session->endpoint->id.self about who we are calling.
  1346. */
  1347. ast_channel_lock(session->channel);
  1348. ast_party_id_copy(&session->id, &ast_channel_caller(session->channel)->id);
  1349. ast_channel_unlock(session->channel);
  1350. /* Supply initial connected line information if available. */
  1351. if (!session->id.number.valid && !session->id.name.valid) {
  1352. return;
  1353. }
  1354. ast_party_connected_line_init(&connected);
  1355. connected.id = session->id;
  1356. connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
  1357. ast_channel_queue_connected_line_update(session->channel, &connected, NULL);
  1358. }
  1359. static int call(void *data)
  1360. {
  1361. struct ast_sip_channel_pvt *channel = data;
  1362. struct ast_sip_session *session = channel->session;
  1363. struct chan_pjsip_pvt *pvt = channel->pvt;
  1364. pjsip_tx_data *tdata;
  1365. int res = ast_sip_session_create_invite(session, &tdata);
  1366. if (res) {
  1367. ast_set_hangupsource(session->channel, ast_channel_name(session->channel), 0);
  1368. ast_queue_hangup(session->channel);
  1369. } else {
  1370. set_channel_on_rtp_instance(pvt, ast_channel_uniqueid(session->channel));
  1371. update_initial_connected_line(session);
  1372. ast_sip_session_send_request(session, tdata);
  1373. }
  1374. ao2_ref(channel, -1);
  1375. return res;
  1376. }
  1377. /*! \brief Function called by core to actually start calling a remote party */
  1378. static int chan_pjsip_call(struct ast_channel *ast, const char *dest, int timeout)
  1379. {
  1380. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
  1381. ao2_ref(channel, +1);
  1382. if (ast_sip_push_task(channel->session->serializer, call, channel)) {
  1383. ast_log(LOG_WARNING, "Error attempting to place outbound call to '%s'\n", dest);
  1384. ao2_cleanup(channel);
  1385. return -1;
  1386. }
  1387. return 0;
  1388. }
  1389. /*! \brief Internal function which translates from Asterisk cause codes to SIP response codes */
  1390. static int hangup_cause2sip(int cause)
  1391. {
  1392. switch (cause) {
  1393. case AST_CAUSE_UNALLOCATED: /* 1 */
  1394. case AST_CAUSE_NO_ROUTE_DESTINATION: /* 3 IAX2: Can't find extension in context */
  1395. case AST_CAUSE_NO_ROUTE_TRANSIT_NET: /* 2 */
  1396. return 404;
  1397. case AST_CAUSE_CONGESTION: /* 34 */
  1398. case AST_CAUSE_SWITCH_CONGESTION: /* 42 */
  1399. return 503;
  1400. case AST_CAUSE_NO_USER_RESPONSE: /* 18 */
  1401. return 408;
  1402. case AST_CAUSE_NO_ANSWER: /* 19 */
  1403. case AST_CAUSE_UNREGISTERED: /* 20 */
  1404. return 480;
  1405. case AST_CAUSE_CALL_REJECTED: /* 21 */
  1406. return 403;
  1407. case AST_CAUSE_NUMBER_CHANGED: /* 22 */
  1408. return 410;
  1409. case AST_CAUSE_NORMAL_UNSPECIFIED: /* 31 */
  1410. return 480;
  1411. case AST_CAUSE_INVALID_NUMBER_FORMAT:
  1412. return 484;
  1413. case AST_CAUSE_USER_BUSY:
  1414. return 486;
  1415. case AST_CAUSE_FAILURE:
  1416. return 500;
  1417. case AST_CAUSE_FACILITY_REJECTED: /* 29 */
  1418. return 501;
  1419. case AST_CAUSE_CHAN_NOT_IMPLEMENTED:
  1420. return 503;
  1421. case AST_CAUSE_DESTINATION_OUT_OF_ORDER:
  1422. return 502;
  1423. case AST_CAUSE_BEARERCAPABILITY_NOTAVAIL: /* Can't find codec to connect to host */
  1424. return 488;
  1425. case AST_CAUSE_INTERWORKING: /* Unspecified Interworking issues */
  1426. return 500;
  1427. case AST_CAUSE_NOTDEFINED:
  1428. default:
  1429. ast_debug(1, "AST hangup cause %d (no match found in PJSIP)\n", cause);
  1430. return 0;
  1431. }
  1432. /* Never reached */
  1433. return 0;
  1434. }
  1435. struct hangup_data {
  1436. int cause;
  1437. struct ast_channel *chan;
  1438. };
  1439. static void hangup_data_destroy(void *obj)
  1440. {
  1441. struct hangup_data *h_data = obj;
  1442. h_data->chan = ast_channel_unref(h_data->chan);
  1443. }
  1444. static struct hangup_data *hangup_data_alloc(int cause, struct ast_channel *chan)
  1445. {
  1446. struct hangup_data *h_data = ao2_alloc(sizeof(*h_data), hangup_data_destroy);
  1447. if (!h_data) {
  1448. return NULL;
  1449. }
  1450. h_data->cause = cause;
  1451. h_data->chan = ast_channel_ref(chan);
  1452. return h_data;
  1453. }
  1454. /*! \brief Clear a channel from a session along with its PVT */
  1455. static void clear_session_and_channel(struct ast_sip_session *session, struct ast_channel *ast, struct chan_pjsip_pvt *pvt)
  1456. {
  1457. session->channel = NULL;
  1458. set_channel_on_rtp_instance(pvt, "");
  1459. ast_channel_tech_pvt_set(ast, NULL);
  1460. }
  1461. static int hangup(void *data)
  1462. {
  1463. struct hangup_data *h_data = data;
  1464. struct ast_channel *ast = h_data->chan;
  1465. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
  1466. struct chan_pjsip_pvt *pvt = channel->pvt;
  1467. struct ast_sip_session *session = channel->session;
  1468. int cause = h_data->cause;
  1469. ast_sip_session_terminate(session, cause);
  1470. clear_session_and_channel(session, ast, pvt);
  1471. ao2_cleanup(channel);
  1472. ao2_cleanup(h_data);
  1473. return 0;
  1474. }
  1475. /*! \brief Function called by core to hang up a PJSIP session */
  1476. static int chan_pjsip_hangup(struct ast_channel *ast)
  1477. {
  1478. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
  1479. struct chan_pjsip_pvt *pvt = channel->pvt;
  1480. int cause = hangup_cause2sip(ast_channel_hangupcause(channel->session->channel));
  1481. struct hangup_data *h_data = hangup_data_alloc(cause, ast);
  1482. if (!h_data) {
  1483. goto failure;
  1484. }
  1485. if (ast_sip_push_task(channel->session->serializer, hangup, h_data)) {
  1486. ast_log(LOG_WARNING, "Unable to push hangup task to the threadpool. Expect bad things\n");
  1487. goto failure;
  1488. }
  1489. return 0;
  1490. failure:
  1491. /* Go ahead and do our cleanup of the session and channel even if we're not going
  1492. * to be able to send our SIP request/response
  1493. */
  1494. clear_session_and_channel(channel->session, ast, pvt);
  1495. ao2_cleanup(channel);
  1496. ao2_cleanup(h_data);
  1497. return -1;
  1498. }
  1499. struct request_data {
  1500. struct ast_sip_session *session;
  1501. struct ast_format_cap *caps;
  1502. const char *dest;
  1503. int cause;
  1504. };
  1505. static int request(void *obj)
  1506. {
  1507. struct request_data *req_data = obj;
  1508. struct ast_sip_session *session = NULL;
  1509. char *tmp = ast_strdupa(req_data->dest), *endpoint_name = NULL, *request_user = NULL;
  1510. RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
  1511. AST_DECLARE_APP_ARGS(args,
  1512. AST_APP_ARG(endpoint);
  1513. AST_APP_ARG(aor);
  1514. );
  1515. if (ast_strlen_zero(tmp)) {
  1516. ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty destination\n");
  1517. req_data->cause = AST_CAUSE_CHANNEL_UNACCEPTABLE;
  1518. return -1;
  1519. }
  1520. AST_NONSTANDARD_APP_ARGS(args, tmp, '/');
  1521. /* If a request user has been specified extract it from the endpoint name portion */
  1522. if ((endpoint_name = strchr(args.endpoint, '@'))) {
  1523. request_user = args.endpoint;
  1524. *endpoint_name++ = '\0';
  1525. } else {
  1526. endpoint_name = args.endpoint;
  1527. }
  1528. if (ast_strlen_zero(endpoint_name)) {
  1529. ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty endpoint name\n");
  1530. req_data->cause = AST_CAUSE_CHANNEL_UNACCEPTABLE;
  1531. } else if (!(endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", endpoint_name))) {
  1532. ast_log(LOG_ERROR, "Unable to create PJSIP channel - endpoint '%s' was not found\n", endpoint_name);
  1533. req_data->cause = AST_CAUSE_NO_ROUTE_DESTINATION;
  1534. return -1;
  1535. }
  1536. if (!(session = ast_sip_session_create_outgoing(endpoint, NULL, args.aor, request_user, req_data->caps))) {
  1537. ast_log(LOG_ERROR, "Failed to create outgoing session to endpoint '%s'\n", endpoint_name);
  1538. req_data->cause = AST_CAUSE_NO_ROUTE_DESTINATION;
  1539. return -1;
  1540. }
  1541. req_data->session = session;
  1542. return 0;
  1543. }
  1544. /*! \brief Function called by core to create a new outgoing PJSIP session */
  1545. 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)
  1546. {
  1547. struct request_data req_data;
  1548. RAII_VAR(struct ast_sip_session *, session, NULL, ao2_cleanup);
  1549. req_data.caps = cap;
  1550. req_data.dest = data;
  1551. if (ast_sip_push_task_synchronous(NULL, request, &req_data)) {
  1552. *cause = req_data.cause;
  1553. return NULL;
  1554. }
  1555. session = req_data.session;
  1556. if (!(session->channel = chan_pjsip_new(session, AST_STATE_DOWN, NULL, NULL, assignedids, requestor, NULL))) {
  1557. /* Session needs to be terminated prematurely */
  1558. return NULL;
  1559. }
  1560. return session->channel;
  1561. }
  1562. struct sendtext_data {
  1563. struct ast_sip_session *session;
  1564. char text[0];
  1565. };
  1566. static void sendtext_data_destroy(void *obj)
  1567. {
  1568. struct sendtext_data *data = obj;
  1569. ao2_ref(data->session, -1);
  1570. }
  1571. static struct sendtext_data* sendtext_data_create(struct ast_sip_session *session, const char *text)
  1572. {
  1573. int size = strlen(text) + 1;
  1574. struct sendtext_data *data = ao2_alloc(sizeof(*data)+size, sendtext_data_destroy);
  1575. if (!data) {
  1576. return NULL;
  1577. }
  1578. data->session = session;
  1579. ao2_ref(data->session, +1);
  1580. ast_copy_string(data->text, text, size);
  1581. return data;
  1582. }
  1583. static int sendtext(void *obj)
  1584. {
  1585. RAII_VAR(struct sendtext_data *, data, obj, ao2_cleanup);
  1586. pjsip_tx_data *tdata;
  1587. const struct ast_sip_body body = {
  1588. .type = "text",
  1589. .subtype = "plain",
  1590. .body_text = data->text
  1591. };
  1592. ast_debug(3, "Sending in dialog SIP message\n");
  1593. ast_sip_create_request("MESSAGE", data->session->inv_session->dlg, data->session->endpoint, NULL, NULL, &tdata);
  1594. ast_sip_add_body(tdata, &body);
  1595. ast_sip_send_request(tdata, data->session->inv_session->dlg, data->session->endpoint, NULL, NULL);
  1596. return 0;
  1597. }
  1598. /*! \brief Function called by core to send text on PJSIP session */
  1599. static int chan_pjsip_sendtext(struct ast_channel *ast, const char *text)
  1600. {
  1601. struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
  1602. struct sendtext_data *data = sendtext_data_create(channel->session, text);
  1603. if (!data || ast_sip_push_task(channel->session->serializer, sendtext, data)) {
  1604. ao2_ref(data, -1);
  1605. return -1;
  1606. }
  1607. return 0;
  1608. }
  1609. /*! \brief Convert SIP hangup causes to Asterisk hangup causes */
  1610. static int hangup_sip2cause(int cause)
  1611. {
  1612. /* Possible values taken from causes.h */
  1613. switch(cause) {
  1614. case 401: /* Unauthorized */
  1615. return AST_CAUSE_CALL_REJECTED;
  1616. case 403: /* Not found */
  1617. return AST_CAUSE_CALL_REJECTED;
  1618. case 404: /* Not found */
  1619. return AST_CAUSE_UNALLOCATED;
  1620. case 405: /* Method not allowed */
  1621. return AST_CAUSE_INTERWORKING;
  1622. case 407: /* Proxy authentication required */
  1623. return AST_CAUSE_CALL_REJECTED;
  1624. case 408: /* No reaction */
  1625. return AST_CAUSE_NO_USER_RESPONSE;
  1626. case 409: /* Conflict */
  1627. return AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
  1628. case 410: /* Gone */
  1629. return AST_CAUSE_NUMBER_CHANGED;
  1630. case 411: /* Length required */
  1631. return AST_CAUSE_INTERWORKING;
  1632. case 413: /* Request entity too large */
  1633. return AST_CAUSE_INTERWORKING;
  1634. case 414: /* Request URI too large */
  1635. return AST_CAUSE_INTERWORKING;
  1636. case 415: /* Unsupported media type */
  1637. return AST_CAUSE_INTERWORKING;
  1638. case 420: /* Bad extension */
  1639. return AST_CAUSE_NO_ROUTE_DESTINATION;
  1640. case 480: /* No answer */
  1641. return AST_CAUSE_NO_ANSWER;
  1642. case 481: /* No answer */
  1643. return AST_CAUSE_INTERWORKING;
  1644. case 482: /* Loop detected */
  1645. return AST_CAUSE_INTERWORKING;
  1646. case 483: /* Too many hops */
  1647. return AST_CAUSE_NO_ANSWER;
  1648. case 484: /* Address incomplete */
  1649. return AST_CAUSE_INVALID_NUMBER_FORMAT;
  1650. case 485: /* Ambiguous */
  1651. return AST_CAUSE_UNALLOCATED;
  1652. case 486: /* Busy everywhere */
  1653. return AST_CAUSE_BUSY;
  1654. case 487: /* Request terminated */
  1655. return AST_CAUSE_INTERWORKING;
  1656. case 488: /* No codecs approved */
  1657. return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
  1658. case 491: /* Request pending */
  1659. return AST_CAUSE_INTERWORKING;
  1660. case 493: /* Undecipherable */
  1661. return AST_CAUSE_INTERWORKING;
  1662. case 500: /* Server internal failure */
  1663. return AST_CAUSE_FAILURE;
  1664. case 501: /* Call rejected */
  1665. return AST_CAUSE_FACILITY_REJECTED;
  1666. case 502:
  1667. return AST_CAUSE_DESTINATION_OUT_OF_ORDER;
  1668. case 503: /* Service unavailable */
  1669. return AST_CAUSE_CONGESTION;
  1670. case 504: /* Gateway timeout */
  1671. return AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
  1672. case 505: /* SIP version not supported */
  1673. return AST_CAUSE_INTERWORKING;
  1674. case 600: /* Busy everywhere */
  1675. return AST_CAUSE_USER_BUSY;
  1676. case 603: /* Decline */
  1677. return AST_CAUSE_CALL_REJECTED;
  1678. case 604: /* Does not exist anywhere */
  1679. return AST_CAUSE_UNALLOCATED;
  1680. case 606: /* Not acceptable */
  1681. return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
  1682. default:
  1683. if (cause < 500 && cause >= 400) {
  1684. /* 4xx class error that is unknown - someting wrong with our request */
  1685. return AST_CAUSE_INTERWORKING;
  1686. } else if (cause < 600 && cause >= 500) {
  1687. /* 5xx class error - problem in the remote end */
  1688. return AST_CAUSE_CONGESTION;
  1689. } else if (cause < 700 && cause >= 600) {
  1690. /* 6xx - global errors in the 4xx class */
  1691. return AST_CAUSE_INTERWORKING;
  1692. }
  1693. return AST_CAUSE_NORMAL;
  1694. }
  1695. /* Never reached */
  1696. return 0;
  1697. }
  1698. static void chan_pjsip_session_begin(struct ast_sip_session *session)
  1699. {
  1700. RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
  1701. if (session->endpoint->media.direct_media.glare_mitigation ==
  1702. AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_NONE) {
  1703. return;
  1704. }
  1705. datastore = ast_sip_session_alloc_datastore(&direct_media_mitigation_info,
  1706. "direct_media_glare_mitigation");
  1707. if (!datastore) {
  1708. return;
  1709. }
  1710. ast_sip_session_add_datastore(session, datastore);
  1711. }
  1712. /*! \brief Function called when the session ends */
  1713. static void chan_pjsip_session_end(struct ast_sip_session *session)
  1714. {
  1715. if (!session->channel) {
  1716. return;
  1717. }
  1718. chan_pjsip_remove_hold(ast_channel_uniqueid(session->channel));
  1719. ast_set_hangupsource(session->channel, ast_channel_name(session->channel), 0);
  1720. if (!ast_channel_hangupcause(session->channel) && session->inv_session) {
  1721. int cause = hangup_sip2cause(session->inv_session->cause);
  1722. ast_queue_hangup_with_cause(session->channel, cause);
  1723. } else {
  1724. ast_queue_hangup(session->channel);
  1725. }
  1726. }
  1727. /*! \brief Function called when a request is received on the session */
  1728. static int chan_pjsip_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
  1729. {
  1730. RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
  1731. struct transport_info_data *transport_data;
  1732. pjsip_tx_data *packet = NULL;
  1733. if (session->channel) {
  1734. return 0;
  1735. }
  1736. if (session->inv_session->state >= PJSIP_INV_STATE_CONFIRMED) {
  1737. /* Weird case. We've received a reinvite but we don't have a channel. The most
  1738. * typical case for this happening is that a blind transfer fails, and so the
  1739. * transferer attempts to reinvite himself back into the call. We already got
  1740. * rid of that channel, and the other side of the call is unrecoverable.
  1741. *
  1742. * We treat this as a failure, so our best bet is to just hang this call
  1743. * up and not create a new channel. Clearing defer_terminate here ensures that
  1744. * calling ast_sip_session_terminate() can result in a BYE being sent ASAP.
  1745. */
  1746. session->defer_terminate = 0;
  1747. ast_sip_session_terminate(session, 400);
  1748. return -1;
  1749. }
  1750. datastore = ast_sip_session_alloc_datastore(&transport_info, "transport_info");
  1751. if (!datastore) {
  1752. return -1;
  1753. }
  1754. transport_data = ast_calloc(1, sizeof(*transport_data));
  1755. if (!transport_data) {
  1756. return -1;
  1757. }
  1758. pj_sockaddr_cp(&transport_data->local_addr, &rdata->tp_info.transport->local_addr);
  1759. pj_sockaddr_cp(&transport_data->remote_addr, &rdata->pkt_info.src_addr);
  1760. datastore->data = transport_data;
  1761. ast_sip_session_add_datastore(session, datastore);
  1762. if (!(session->channel = chan_pjsip_new(session, AST_STATE_RING, session->exten, NULL, NULL, NULL, NULL))) {
  1763. if (pjsip_inv_end_session(session->inv_session, 503, NULL, &packet) == PJ_SUCCESS) {
  1764. ast_sip_session_send_response(session, packet);
  1765. }
  1766. ast_log(LOG_ERROR, "Failed to allocate new PJSIP channel on incoming SIP INVITE\n");
  1767. return -1;
  1768. }
  1769. /* channel gets created on incoming request, but we wait to call start
  1770. so other supplements have a chance to run */
  1771. return 0;
  1772. }
  1773. static int call_pickup_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
  1774. {
  1775. struct ast_features_pickup_config *pickup_cfg;
  1776. struct ast_channel *chan;
  1777. /* We don't care about reinvites */
  1778. if (session->inv_session->state >= PJSIP_INV_STATE_CONFIRMED) {
  1779. return 0;
  1780. }
  1781. pickup_cfg = ast_get_chan_features_pickup_config(session->channel);
  1782. if (!pickup_cfg) {
  1783. ast_log(LOG_ERROR, "Unable to retrieve pickup configuration options. Unable to detect call pickup extension.\n");
  1784. return 0;
  1785. }
  1786. if (strcmp(session->exten, pickup_cfg->pickupexten)) {
  1787. ao2_ref(pickup_cfg, -1);
  1788. return 0;
  1789. }
  1790. ao2_ref(pickup_cfg, -1);
  1791. /* We can't directly use session->channel because the pickup operation will cause a masquerade to occur,
  1792. * changing the channel pointer in session to a different channel. To ensure we work on the right channel
  1793. * we store a pointer locally before we begin and keep a reference so it remains valid no matter what.
  1794. */
  1795. chan = ast_channel_ref(session->channel);
  1796. if (ast_pickup_call(chan)) {
  1797. ast_channel_hangupcause_set(chan, AST_CAUSE_CALL_REJECTED);
  1798. } else {
  1799. ast_channel_hangupcause_set(chan, AST_CAUSE_NORMAL_CLEARING);
  1800. }
  1801. /* A hangup always occurs because the pickup operation will have either failed resulting in the call
  1802. * needing to be hung up OR the pickup operation was a success and the channel we now have is actually
  1803. * the channel that was replaced, which should be hung up since it is literally in limbo not connected
  1804. * to anything at all.
  1805. */
  1806. ast_hangup(chan);
  1807. ast_channel_unref(chan);
  1808. return 1;
  1809. }
  1810. static struct ast_sip_session_supplement call_pickup_supplement = {
  1811. .method = "INVITE",
  1812. .priority = AST_SIP_SUPPLEMENT_PRIORITY_LAST - 1,
  1813. .incoming_request = call_pickup_incoming_request,
  1814. };
  1815. static int pbx_start_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
  1816. {
  1817. int res;
  1818. /* We don't care about reinvites */
  1819. if (session->inv_session->state >= PJSIP_INV_STATE_CONFIRMED) {
  1820. return 0;
  1821. }
  1822. res = ast_pbx_start(session->channel);
  1823. switch (res) {
  1824. case AST_PBX_FAILED:
  1825. ast_log(LOG_WARNING, "Failed to start PBX ;(\n");
  1826. ast_channel_hangupcause_set(session->channel, AST_CAUSE_SWITCH_CONGESTION);
  1827. ast_hangup(session->channel);
  1828. break;
  1829. case AST_PBX_CALL_LIMIT:
  1830. ast_log(LOG_WARNING, "Failed to start PBX (call limit reached) \n");
  1831. ast_channel_hangupcause_set(session->channel, AST_CAUSE_SWITCH_CONGESTION);
  1832. ast_hangup(session->channel);
  1833. break;
  1834. case AST_PBX_SUCCESS:
  1835. default:
  1836. break;
  1837. }
  1838. ast_debug(3, "Started PBX on new PJSIP channel %s\n", ast_channel_name(session->channel));
  1839. return (res == AST_PBX_SUCCESS) ? 0 : -1;
  1840. }
  1841. static struct ast_sip_session_supplement pbx_start_supplement = {
  1842. .method = "INVITE",
  1843. .priority = AST_SIP_SUPPLEMENT_PRIORITY_LAST,
  1844. .incoming_request = pbx_start_incoming_request,
  1845. };
  1846. /*! \brief Function called when a response is received on the session */
  1847. static void chan_pjsip_incoming_response(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
  1848. {
  1849. struct pjsip_status_line status = rdata->msg_info.msg->line.status;
  1850. struct ast_control_pvt_cause_code *cause_code;
  1851. int data_size = sizeof(*cause_code);
  1852. if (!session->channel) {
  1853. return;
  1854. }
  1855. switch (status.code) {
  1856. case 180:
  1857. ast_queue_control(session->channel, AST_CONTROL_RINGING);
  1858. ast_channel_lock(session->channel);
  1859. if (ast_channel_state(session->channel) != AST_STATE_UP) {
  1860. ast_setstate(session->channel, AST_STATE_RINGING);
  1861. }
  1862. ast_channel_unlock(session->channel);
  1863. break;
  1864. case 183:
  1865. ast_queue_control(session->channel, AST_CONTROL_PROGRESS);
  1866. break;
  1867. case 200:
  1868. ast_queue_control(session->channel, AST_CONTROL_ANSWER);
  1869. break;
  1870. default:
  1871. break;
  1872. }
  1873. /* Build and send the tech-specific cause information */
  1874. /* size of the string making up the cause code is "SIP " number + " " + reason length */
  1875. data_size += 4 + 4 + pj_strlen(&status.reason);
  1876. cause_code = ast_alloca(data_size);
  1877. memset(cause_code, 0, data_size);
  1878. ast_copy_string(cause_code->chan_name, ast_channel_name(session->channel), AST_CHANNEL_NAME);
  1879. snprintf(cause_code->code, data_size - sizeof(*cause_code) + 1, "SIP %d %.*s", status.code,
  1880. (int) pj_strlen(&status.reason), pj_strbuf(&status.reason));
  1881. cause_code->ast_cause = hangup_sip2cause(status.code);
  1882. ast_queue_control_data(session->channel, AST_CONTROL_PVT_CAUSE_CODE, cause_code, data_size);
  1883. ast_channel_hangupcause_hash_set(session->channel, cause_code, data_size);
  1884. }
  1885. static int chan_pjsip_incoming_ack(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
  1886. {
  1887. if (rdata->msg_info.msg->line.req.method.id == PJSIP_ACK_METHOD) {
  1888. if (session->endpoint->media.direct_media.enabled && session->channel) {
  1889. ast_queue_control(session->channel, AST_CONTROL_SRCCHANGE);
  1890. }
  1891. }
  1892. return 0;
  1893. }
  1894. static int update_devstate(void *obj, void *arg, int flags)
  1895. {
  1896. ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE,
  1897. "PJSIP/%s", ast_sorcery_object_get_id(obj));
  1898. return 0;
  1899. }
  1900. static struct ast_custom_function chan_pjsip_dial_contacts_function = {
  1901. .name = "PJSIP_DIAL_CONTACTS",
  1902. .read = pjsip_acf_dial_contacts_read,
  1903. };
  1904. static struct ast_custom_function media_offer_function = {
  1905. .name = "PJSIP_MEDIA_OFFER",
  1906. .read = pjsip_acf_media_offer_read,
  1907. .write = pjsip_acf_media_offer_write
  1908. };
  1909. /*!
  1910. * \brief Load the module
  1911. *
  1912. * Module loading including tests for configuration or dependencies.
  1913. * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
  1914. * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
  1915. * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
  1916. * configuration file or other non-critical problem return
  1917. * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
  1918. */
  1919. static int load_module(void)
  1920. {
  1921. struct ao2_container *endpoints;
  1922. CHECK_PJSIP_SESSION_MODULE_LOADED();
  1923. if (!(chan_pjsip_tech.capabilities = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
  1924. return AST_MODULE_LOAD_DECLINE;
  1925. }
  1926. ast_format_cap_append_by_type(chan_pjsip_tech.capabilities, AST_MEDIA_TYPE_AUDIO);
  1927. ast_rtp_glue_register(&chan_pjsip_rtp_glue);
  1928. if (ast_channel_register(&chan_pjsip_tech)) {
  1929. ast_log(LOG_ERROR, "Unable to register channel class %s\n", channel_type);
  1930. goto end;
  1931. }
  1932. if (ast_custom_function_register(&chan_pjsip_dial_contacts_function)) {
  1933. ast_log(LOG_ERROR, "Unable to register PJSIP_DIAL_CONTACTS dialplan function\n");
  1934. goto end;
  1935. }
  1936. if (ast_custom_function_register(&media_offer_function)) {
  1937. ast_log(LOG_WARNING, "Unable to register PJSIP_MEDIA_OFFER dialplan function\n");
  1938. goto end;
  1939. }
  1940. if (ast_sip_session_register_supplement(&chan_pjsip_supplement)) {
  1941. ast_log(LOG_ERROR, "Unable to register PJSIP supplement\n");
  1942. goto end;
  1943. }
  1944. if (!(pjsip_uids_onhold = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_RWLOCK,
  1945. AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT, 37, uid_hold_hash_fn,
  1946. uid_hold_sort_fn, NULL))) {
  1947. ast_log(LOG_ERROR, "Unable to create held channels container\n");
  1948. goto end;
  1949. }
  1950. if (ast_sip_session_register_supplement(&call_pickup_supplement)) {
  1951. ast_log(LOG_ERROR, "Unable to register PJSIP call pickup supplement\n");
  1952. ast_sip_session_unregister_supplement(&chan_pjsip_supplement);
  1953. goto end;
  1954. }
  1955. if (ast_sip_session_register_supplement(&pbx_start_supplement)) {
  1956. ast_log(LOG_ERROR, "Unable to register PJSIP pbx start supplement\n");
  1957. ast_sip_session_unregister_supplement(&chan_pjsip_supplement);
  1958. ast_sip_session_unregister_supplement(&call_pickup_supplement);
  1959. goto end;
  1960. }
  1961. if (ast_sip_session_register_supplement(&chan_pjsip_ack_supplement)) {
  1962. ast_log(LOG_ERROR, "Unable to register PJSIP ACK supplement\n");
  1963. ast_sip_session_unregister_supplement(&pbx_start_supplement);
  1964. ast_sip_session_unregister_supplement(&chan_pjsip_supplement);
  1965. ast_sip_session_unregister_supplement(&call_pickup_supplement);
  1966. goto end;
  1967. }
  1968. /* since endpoints are loaded before the channel driver their device
  1969. states get set to 'invalid', so they need to be updated */
  1970. if ((endpoints = ast_sip_get_endpoints())) {
  1971. ao2_callback(endpoints, OBJ_NODATA, update_devstate, NULL);
  1972. ao2_ref(endpoints, -1);
  1973. }
  1974. return 0;
  1975. end:
  1976. ao2_cleanup(pjsip_uids_onhold);
  1977. pjsip_uids_onhold = NULL;
  1978. ast_custom_function_unregister(&media_offer_function);
  1979. ast_custom_function_unregister(&chan_pjsip_dial_contacts_function);
  1980. ast_channel_unregister(&chan_pjsip_tech);
  1981. ast_rtp_glue_unregister(&chan_pjsip_rtp_glue);
  1982. return AST_MODULE_LOAD_FAILURE;
  1983. }
  1984. /*! \brief Unload the PJSIP channel from Asterisk */
  1985. static int unload_module(void)
  1986. {
  1987. ao2_cleanup(pjsip_uids_onhold);
  1988. pjsip_uids_onhold = NULL;
  1989. ast_sip_session_unregister_supplement(&chan_pjsip_supplement);
  1990. ast_sip_session_unregister_supplement(&pbx_start_supplement);
  1991. ast_sip_session_unregister_supplement(&chan_pjsip_ack_supplement);
  1992. ast_sip_session_unregister_supplement(&call_pickup_supplement);
  1993. ast_custom_function_unregister(&media_offer_function);
  1994. ast_custom_function_unregister(&chan_pjsip_dial_contacts_function);
  1995. ast_channel_unregister(&chan_pjsip_tech);
  1996. ao2_ref(chan_pjsip_tech.capabilities, -1);
  1997. ast_rtp_glue_unregister(&chan_pjsip_rtp_glue);
  1998. return 0;
  1999. }
  2000. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Channel Driver",
  2001. .support_level = AST_MODULE_SUPPORT_CORE,
  2002. .load = load_module,
  2003. .unload = unload_module,
  2004. .load_pri = AST_MODPRI_CHANNEL_DRIVER,
  2005. );