2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * Joshua Colp <jcolp@digium.com>
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.
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.
21 * \author Joshua Colp <jcolp@digium.com>
23 * \brief PSJIP SIP Channel Driver
25 * \ingroup channel_drivers
29 <depend>pjproject</depend>
30 <depend>res_pjsip</depend>
31 <depend>res_pjsip_session</depend>
32 <support_level>core</support_level>
41 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
43 #include "asterisk/lock.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/module.h"
46 #include "asterisk/pbx.h"
47 #include "asterisk/rtp_engine.h"
48 #include "asterisk/acl.h"
49 #include "asterisk/callerid.h"
50 #include "asterisk/file.h"
51 #include "asterisk/cli.h"
52 #include "asterisk/app.h"
53 #include "asterisk/musiconhold.h"
54 #include "asterisk/causes.h"
55 #include "asterisk/taskprocessor.h"
56 #include "asterisk/dsp.h"
57 #include "asterisk/stasis_endpoints.h"
58 #include "asterisk/stasis_channels.h"
59 #include "asterisk/indications.h"
60 #include "asterisk/threadstorage.h"
61 #include "asterisk/features_config.h"
62 #include "asterisk/pickup.h"
64 #include "asterisk/res_pjsip.h"
65 #include "asterisk/res_pjsip_session.h"
67 #include "pjsip/include/chan_pjsip.h"
68 #include "pjsip/include/dialplan_functions.h"
70 AST_THREADSTORAGE(uniqueid_threadbuf);
71 #define UNIQUEID_BUFSIZE 256
73 static const char desc[] = "PJSIP Channel";
74 static const char channel_type[] = "PJSIP";
76 static unsigned int chan_idx;
78 static void chan_pjsip_pvt_dtor(void *obj)
80 struct chan_pjsip_pvt *pvt = obj;
83 for (i = 0; i < SIP_MEDIA_SIZE; ++i) {
84 ao2_cleanup(pvt->media[i]);
89 /* \brief Asterisk core interaction functions */
90 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);
91 static int chan_pjsip_sendtext(struct ast_channel *ast, const char *text);
92 static int chan_pjsip_digit_begin(struct ast_channel *ast, char digit);
93 static int chan_pjsip_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
94 static int chan_pjsip_call(struct ast_channel *ast, const char *dest, int timeout);
95 static int chan_pjsip_hangup(struct ast_channel *ast);
96 static int chan_pjsip_answer(struct ast_channel *ast);
97 static struct ast_frame *chan_pjsip_read(struct ast_channel *ast);
98 static int chan_pjsip_write(struct ast_channel *ast, struct ast_frame *f);
99 static int chan_pjsip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
100 static int chan_pjsip_transfer(struct ast_channel *ast, const char *target);
101 static int chan_pjsip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
102 static int chan_pjsip_devicestate(const char *data);
103 static int chan_pjsip_queryoption(struct ast_channel *ast, int option, void *data, int *datalen);
104 static const char *chan_pjsip_get_uniqueid(struct ast_channel *ast);
106 /*! \brief PBX interface structure for channel registration */
107 struct ast_channel_tech chan_pjsip_tech = {
108 .type = channel_type,
109 .description = "PJSIP Channel Driver",
110 .requester = chan_pjsip_request,
111 .send_text = chan_pjsip_sendtext,
112 .send_digit_begin = chan_pjsip_digit_begin,
113 .send_digit_end = chan_pjsip_digit_end,
114 .call = chan_pjsip_call,
115 .hangup = chan_pjsip_hangup,
116 .answer = chan_pjsip_answer,
117 .read = chan_pjsip_read,
118 .write = chan_pjsip_write,
119 .write_video = chan_pjsip_write,
120 .exception = chan_pjsip_read,
121 .indicate = chan_pjsip_indicate,
122 .transfer = chan_pjsip_transfer,
123 .fixup = chan_pjsip_fixup,
124 .devicestate = chan_pjsip_devicestate,
125 .queryoption = chan_pjsip_queryoption,
126 .func_channel_read = pjsip_acf_channel_read,
127 .get_pvt_uniqueid = chan_pjsip_get_uniqueid,
128 .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER
131 /*! \brief SIP session interaction functions */
132 static void chan_pjsip_session_begin(struct ast_sip_session *session);
133 static void chan_pjsip_session_end(struct ast_sip_session *session);
134 static int chan_pjsip_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
135 static void chan_pjsip_incoming_response(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
137 /*! \brief SIP session supplement structure */
138 static struct ast_sip_session_supplement chan_pjsip_supplement = {
140 .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL,
141 .session_begin = chan_pjsip_session_begin,
142 .session_end = chan_pjsip_session_end,
143 .incoming_request = chan_pjsip_incoming_request,
144 .incoming_response = chan_pjsip_incoming_response,
147 static int chan_pjsip_incoming_ack(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
149 static struct ast_sip_session_supplement chan_pjsip_ack_supplement = {
151 .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL,
152 .incoming_request = chan_pjsip_incoming_ack,
155 /*! \brief Function called by RTP engine to get local audio RTP peer */
156 static enum ast_rtp_glue_result chan_pjsip_get_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
158 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
159 struct chan_pjsip_pvt *pvt = channel->pvt;
160 struct ast_sip_endpoint *endpoint;
162 if (!pvt || !channel->session || !pvt->media[SIP_MEDIA_AUDIO]->rtp) {
163 return AST_RTP_GLUE_RESULT_FORBID;
166 endpoint = channel->session->endpoint;
168 *instance = pvt->media[SIP_MEDIA_AUDIO]->rtp;
169 ao2_ref(*instance, +1);
171 ast_assert(endpoint != NULL);
172 if (endpoint->media.rtp.encryption != AST_SIP_MEDIA_ENCRYPT_NONE) {
173 return AST_RTP_GLUE_RESULT_FORBID;
176 if (endpoint->media.direct_media.enabled) {
177 return AST_RTP_GLUE_RESULT_REMOTE;
180 return AST_RTP_GLUE_RESULT_LOCAL;
183 /*! \brief Function called by RTP engine to get local video RTP peer */
184 static enum ast_rtp_glue_result chan_pjsip_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
186 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
187 struct chan_pjsip_pvt *pvt = channel->pvt;
188 struct ast_sip_endpoint *endpoint;
190 if (!pvt || !channel->session || !pvt->media[SIP_MEDIA_VIDEO]->rtp) {
191 return AST_RTP_GLUE_RESULT_FORBID;
194 endpoint = channel->session->endpoint;
196 *instance = pvt->media[SIP_MEDIA_VIDEO]->rtp;
197 ao2_ref(*instance, +1);
199 ast_assert(endpoint != NULL);
200 if (endpoint->media.rtp.encryption != AST_SIP_MEDIA_ENCRYPT_NONE) {
201 return AST_RTP_GLUE_RESULT_FORBID;
204 return AST_RTP_GLUE_RESULT_LOCAL;
207 /*! \brief Function called by RTP engine to get peer capabilities */
208 static void chan_pjsip_get_codec(struct ast_channel *chan, struct ast_format_cap *result)
210 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
212 ast_format_cap_copy(result, channel->session->endpoint->media.codecs);
215 static int send_direct_media_request(void *data)
217 RAII_VAR(struct ast_sip_session *, session, data, ao2_cleanup);
219 return ast_sip_session_refresh(session, NULL, NULL, NULL,
220 session->endpoint->media.direct_media.method, 1);
223 /*! \brief Destructor function for \ref transport_info_data */
224 static void transport_info_destroy(void *obj)
226 struct transport_info_data *data = obj;
230 /*! \brief Datastore used to store local/remote addresses for the
231 * INVITE request that created the PJSIP channel */
232 static struct ast_datastore_info transport_info = {
233 .type = "chan_pjsip_transport_info",
234 .destroy = transport_info_destroy,
237 static struct ast_datastore_info direct_media_mitigation_info = { };
239 static int direct_media_mitigate_glare(struct ast_sip_session *session)
241 RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
243 if (session->endpoint->media.direct_media.glare_mitigation ==
244 AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_NONE) {
248 datastore = ast_sip_session_get_datastore(session, "direct_media_glare_mitigation");
253 /* Removing the datastore ensures we won't try to mitigate glare on subsequent reinvites */
254 ast_sip_session_remove_datastore(session, "direct_media_glare_mitigation");
256 if ((session->endpoint->media.direct_media.glare_mitigation ==
257 AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_OUTGOING &&
258 session->inv_session->role == PJSIP_ROLE_UAC) ||
259 (session->endpoint->media.direct_media.glare_mitigation ==
260 AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_INCOMING &&
261 session->inv_session->role == PJSIP_ROLE_UAS)) {
268 static int check_for_rtp_changes(struct ast_channel *chan, struct ast_rtp_instance *rtp,
269 struct ast_sip_session_media *media, int rtcp_fd)
274 changed = ast_rtp_instance_get_and_cmp_remote_address(rtp, &media->direct_media_addr);
276 ast_channel_set_fd(chan, rtcp_fd, -1);
277 ast_rtp_instance_set_prop(media->rtp, AST_RTP_PROPERTY_RTCP, 0);
279 } else if (!ast_sockaddr_isnull(&media->direct_media_addr)){
280 ast_sockaddr_setnull(&media->direct_media_addr);
283 ast_rtp_instance_set_prop(media->rtp, AST_RTP_PROPERTY_RTCP, 1);
284 ast_channel_set_fd(chan, rtcp_fd, ast_rtp_instance_fd(media->rtp, 1));
291 /*! \brief Function called by RTP engine to change where the remote party should send media */
292 static int chan_pjsip_set_rtp_peer(struct ast_channel *chan,
293 struct ast_rtp_instance *rtp,
294 struct ast_rtp_instance *vrtp,
295 struct ast_rtp_instance *tpeer,
296 const struct ast_format_cap *cap,
299 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
300 struct chan_pjsip_pvt *pvt = channel->pvt;
301 struct ast_sip_session *session = channel->session;
304 /* Don't try to do any direct media shenanigans on early bridges */
305 if ((rtp || vrtp || tpeer) && !ast_channel_is_bridged(chan)) {
306 ast_debug(4, "Disregarding setting RTP on %s: channel is not bridged\n", ast_channel_name(chan));
310 if (nat_active && session->endpoint->media.direct_media.disable_on_nat) {
311 ast_debug(4, "Disregarding setting RTP on %s: NAT is active\n", ast_channel_name(chan));
315 if (pvt->media[SIP_MEDIA_AUDIO]) {
316 changed |= check_for_rtp_changes(chan, rtp, pvt->media[SIP_MEDIA_AUDIO], 1);
318 if (pvt->media[SIP_MEDIA_VIDEO]) {
319 changed |= check_for_rtp_changes(chan, vrtp, pvt->media[SIP_MEDIA_VIDEO], 3);
322 if (direct_media_mitigate_glare(session)) {
323 ast_debug(4, "Disregarding setting RTP on %s: mitigating re-INVITE glare\n", ast_channel_name(chan));
327 if (cap && !ast_format_cap_is_empty(cap) && !ast_format_cap_identical(session->direct_media_cap, cap)) {
328 ast_format_cap_copy(session->direct_media_cap, cap);
333 ao2_ref(session, +1);
335 ast_debug(4, "RTP changed on %s; initiating direct media update\n", ast_channel_name(chan));
336 if (ast_sip_push_task(session->serializer, send_direct_media_request, session)) {
337 ao2_cleanup(session);
344 /*! \brief Local glue for interacting with the RTP engine core */
345 static struct ast_rtp_glue chan_pjsip_rtp_glue = {
347 .get_rtp_info = chan_pjsip_get_rtp_peer,
348 .get_vrtp_info = chan_pjsip_get_vrtp_peer,
349 .get_codec = chan_pjsip_get_codec,
350 .update_peer = chan_pjsip_set_rtp_peer,
353 /*! \brief Function called to create a new PJSIP Asterisk channel */
354 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)
356 struct ast_channel *chan;
357 struct ast_format fmt;
358 RAII_VAR(struct chan_pjsip_pvt *, pvt, NULL, ao2_cleanup);
359 struct ast_sip_channel_pvt *channel;
360 struct ast_variable *var;
362 if (!(pvt = ao2_alloc(sizeof(*pvt), chan_pjsip_pvt_dtor))) {
366 if (!(chan = ast_channel_alloc(1, state, S_OR(session->id.number.str, ""), S_OR(session->id.name.str, ""), "", "", "", assignedids, requestor, 0, "PJSIP/%s-%08x", ast_sorcery_object_get_id(session->endpoint),
367 (unsigned)ast_atomic_fetchadd_int((int *)&chan_idx, +1)))) {
371 ast_channel_tech_set(chan, &chan_pjsip_tech);
373 if (!(channel = ast_sip_channel_pvt_alloc(pvt, session))) {
374 ast_channel_unlock(chan);
379 for (var = session->endpoint->channel_vars; var; var = var->next) {
381 pbx_builtin_setvar_helper(chan, var->name, ast_get_encoded_str(
382 var->value, buf, sizeof(buf)));
385 ast_channel_stage_snapshot(chan);
387 ast_channel_tech_pvt_set(chan, channel);
389 if (ast_format_cap_is_empty(session->req_caps) || !ast_format_cap_has_joint(session->req_caps, session->endpoint->media.codecs)) {
390 ast_format_cap_copy(ast_channel_nativeformats(chan), session->endpoint->media.codecs);
392 ast_format_cap_copy(ast_channel_nativeformats(chan), session->req_caps);
395 ast_codec_choose(&session->endpoint->media.prefs, ast_channel_nativeformats(chan), 1, &fmt);
396 ast_format_copy(ast_channel_writeformat(chan), &fmt);
397 ast_format_copy(ast_channel_rawwriteformat(chan), &fmt);
398 ast_format_copy(ast_channel_readformat(chan), &fmt);
399 ast_format_copy(ast_channel_rawreadformat(chan), &fmt);
401 if (state == AST_STATE_RING) {
402 ast_channel_rings_set(chan, 1);
405 ast_channel_adsicpe_set(chan, AST_ADSI_UNAVAILABLE);
407 ast_channel_context_set(chan, session->endpoint->context);
408 ast_channel_exten_set(chan, S_OR(exten, "s"));
409 ast_channel_priority_set(chan, 1);
411 ast_channel_callgroup_set(chan, session->endpoint->pickup.callgroup);
412 ast_channel_pickupgroup_set(chan, session->endpoint->pickup.pickupgroup);
414 ast_channel_named_callgroups_set(chan, session->endpoint->pickup.named_callgroups);
415 ast_channel_named_pickupgroups_set(chan, session->endpoint->pickup.named_pickupgroups);
417 if (!ast_strlen_zero(session->endpoint->language)) {
418 ast_channel_language_set(chan, session->endpoint->language);
421 if (!ast_strlen_zero(session->endpoint->zone)) {
422 struct ast_tone_zone *zone = ast_get_indication_zone(session->endpoint->zone);
424 ast_log(LOG_ERROR, "Unknown country code '%s' for tonezone. Check indications.conf for available country codes.\n", session->endpoint->zone);
426 ast_channel_zone_set(chan, zone);
429 ast_channel_stage_snapshot_done(chan);
430 ast_channel_unlock(chan);
432 /* If res_pjsip_session is ever updated to create/destroy ast_sip_session_media
433 * during a call such as if multiple same-type stream support is introduced,
434 * these will need to be recaptured as well */
435 pvt->media[SIP_MEDIA_AUDIO] = ao2_find(session->media, "audio", OBJ_KEY);
436 pvt->media[SIP_MEDIA_VIDEO] = ao2_find(session->media, "video", OBJ_KEY);
437 if (pvt->media[SIP_MEDIA_AUDIO] && pvt->media[SIP_MEDIA_AUDIO]->rtp) {
438 ast_rtp_instance_set_channel_id(pvt->media[SIP_MEDIA_AUDIO]->rtp, ast_channel_uniqueid(chan));
440 if (pvt->media[SIP_MEDIA_VIDEO] && pvt->media[SIP_MEDIA_VIDEO]->rtp) {
441 ast_rtp_instance_set_channel_id(pvt->media[SIP_MEDIA_VIDEO]->rtp, ast_channel_uniqueid(chan));
444 ast_endpoint_add_channel(session->endpoint->persistent, chan);
449 static int answer(void *data)
451 pj_status_t status = PJ_SUCCESS;
452 pjsip_tx_data *packet = NULL;
453 struct ast_sip_session *session = data;
455 pjsip_dlg_inc_lock(session->inv_session->dlg);
456 if (session->inv_session->invite_tsx) {
457 status = pjsip_inv_answer(session->inv_session, 200, NULL, NULL, &packet);
459 ast_log(LOG_ERROR,"Cannot answer '%s' because there is no associated SIP transaction\n",
460 ast_channel_name(session->channel));
462 pjsip_dlg_dec_lock(session->inv_session->dlg);
464 if (status == PJ_SUCCESS && packet) {
465 ast_sip_session_send_response(session, packet);
468 ao2_ref(session, -1);
470 return (status == PJ_SUCCESS) ? 0 : -1;
473 /*! \brief Function called by core when we should answer a PJSIP session */
474 static int chan_pjsip_answer(struct ast_channel *ast)
476 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
478 if (ast_channel_state(ast) == AST_STATE_UP) {
482 ast_setstate(ast, AST_STATE_UP);
484 ao2_ref(channel->session, +1);
485 if (ast_sip_push_task(channel->session->serializer, answer, channel->session)) {
486 ast_log(LOG_WARNING, "Unable to push answer task to the threadpool. Cannot answer call\n");
487 ao2_cleanup(channel->session);
494 /*! \brief Internal helper function called when CNG tone is detected */
495 static struct ast_frame *chan_pjsip_cng_tone_detected(struct ast_sip_session *session, struct ast_frame *f)
497 const char *target_context;
500 /* If we only needed this DSP for fax detection purposes we can just drop it now */
501 if (session->endpoint->dtmf == AST_SIP_DTMF_INBAND) {
502 ast_dsp_set_features(session->dsp, DSP_FEATURE_DIGIT_DETECT);
504 ast_dsp_free(session->dsp);
508 /* If already executing in the fax extension don't do anything */
509 if (!strcmp(ast_channel_exten(session->channel), "fax")) {
513 target_context = S_OR(ast_channel_macrocontext(session->channel), ast_channel_context(session->channel));
515 /* We need to unlock the channel here because ast_exists_extension has the
516 * potential to start and stop an autoservice on the channel. Such action
517 * is prone to deadlock if the channel is locked.
519 ast_channel_unlock(session->channel);
520 exists = ast_exists_extension(session->channel, target_context, "fax", 1,
521 S_COR(ast_channel_caller(session->channel)->id.number.valid,
522 ast_channel_caller(session->channel)->id.number.str, NULL));
523 ast_channel_lock(session->channel);
526 ast_verb(2, "Redirecting '%s' to fax extension due to CNG detection\n",
527 ast_channel_name(session->channel));
528 pbx_builtin_setvar_helper(session->channel, "FAXEXTEN", ast_channel_exten(session->channel));
529 if (ast_async_goto(session->channel, target_context, "fax", 1)) {
530 ast_log(LOG_ERROR, "Failed to async goto '%s' into fax extension in '%s'\n",
531 ast_channel_name(session->channel), target_context);
536 ast_log(LOG_NOTICE, "FAX CNG detected on '%s' but no fax extension in '%s'\n",
537 ast_channel_name(session->channel), target_context);
543 /*! \brief Function called by core to read any waiting frames */
544 static struct ast_frame *chan_pjsip_read(struct ast_channel *ast)
546 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
547 struct chan_pjsip_pvt *pvt = channel->pvt;
549 struct ast_sip_session_media *media = NULL;
551 int fdno = ast_channel_fdno(ast);
555 media = pvt->media[SIP_MEDIA_AUDIO];
558 media = pvt->media[SIP_MEDIA_AUDIO];
562 media = pvt->media[SIP_MEDIA_VIDEO];
565 media = pvt->media[SIP_MEDIA_VIDEO];
570 if (!media || !media->rtp) {
571 return &ast_null_frame;
574 if (!(f = ast_rtp_instance_read(media->rtp, rtcp))) {
578 if (f->frametype != AST_FRAME_VOICE) {
582 if (!(ast_format_cap_iscompatible(ast_channel_nativeformats(ast), &f->subclass.format))) {
583 ast_debug(1, "Oooh, format changed to %s\n", ast_getformatname(&f->subclass.format));
584 ast_format_cap_set(ast_channel_nativeformats(ast), &f->subclass.format);
585 ast_set_read_format(ast, ast_channel_readformat(ast));
586 ast_set_write_format(ast, ast_channel_writeformat(ast));
589 if (channel->session->dsp) {
590 f = ast_dsp_process(ast, channel->session->dsp, f);
592 if (f && (f->frametype == AST_FRAME_DTMF)) {
593 if (f->subclass.integer == 'f') {
594 ast_debug(3, "Fax CNG detected on %s\n", ast_channel_name(ast));
595 f = chan_pjsip_cng_tone_detected(channel->session, f);
597 ast_debug(3, "* Detected inband DTMF '%c' on '%s'\n", f->subclass.integer,
598 ast_channel_name(ast));
606 /*! \brief Function called by core to write frames */
607 static int chan_pjsip_write(struct ast_channel *ast, struct ast_frame *frame)
609 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
610 struct chan_pjsip_pvt *pvt = channel->pvt;
611 struct ast_sip_session_media *media;
614 switch (frame->frametype) {
615 case AST_FRAME_VOICE:
616 media = pvt->media[SIP_MEDIA_AUDIO];
621 if (!(ast_format_cap_iscompatible(ast_channel_nativeformats(ast), &frame->subclass.format))) {
625 "Asked to transmit frame type %s, while native formats is %s (read/write = %s/%s)\n",
626 ast_getformatname(&frame->subclass.format),
627 ast_getformatname_multiple(buf, sizeof(buf), ast_channel_nativeformats(ast)),
628 ast_getformatname(ast_channel_readformat(ast)),
629 ast_getformatname(ast_channel_writeformat(ast)));
633 res = ast_rtp_instance_write(media->rtp, frame);
636 case AST_FRAME_VIDEO:
637 if ((media = pvt->media[SIP_MEDIA_VIDEO]) && media->rtp) {
638 res = ast_rtp_instance_write(media->rtp, frame);
641 case AST_FRAME_MODEM:
644 ast_log(LOG_WARNING, "Can't send %u type frames with PJSIP\n", frame->frametype);
652 struct ast_sip_session *session;
653 struct ast_channel *chan;
656 static int fixup(void *data)
658 struct fixup_data *fix_data = data;
659 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(fix_data->chan);
660 struct chan_pjsip_pvt *pvt = channel->pvt;
662 channel->session->channel = fix_data->chan;
663 if (pvt->media[SIP_MEDIA_AUDIO] && pvt->media[SIP_MEDIA_AUDIO]->rtp) {
664 ast_rtp_instance_set_channel_id(pvt->media[SIP_MEDIA_AUDIO]->rtp, ast_channel_uniqueid(fix_data->chan));
666 if (pvt->media[SIP_MEDIA_VIDEO] && pvt->media[SIP_MEDIA_VIDEO]->rtp) {
667 ast_rtp_instance_set_channel_id(pvt->media[SIP_MEDIA_VIDEO]->rtp, ast_channel_uniqueid(fix_data->chan));
673 /*! \brief Function called by core to change the underlying owner channel */
674 static int chan_pjsip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
676 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(newchan);
677 struct fixup_data fix_data;
679 fix_data.session = channel->session;
680 fix_data.chan = newchan;
682 if (channel->session->channel != oldchan) {
686 if (ast_sip_push_task_synchronous(channel->session->serializer, fixup, &fix_data)) {
687 ast_log(LOG_WARNING, "Unable to perform channel fixup\n");
694 /*! AO2 hash function for on hold UIDs */
695 static int uid_hold_hash_fn(const void *obj, const int flags)
697 const char *key = obj;
699 switch (flags & OBJ_SEARCH_MASK) {
702 case OBJ_SEARCH_OBJECT:
705 /* Hash can only work on something with a full key. */
709 return ast_str_hash(key);
712 /*! AO2 sort function for on hold UIDs */
713 static int uid_hold_sort_fn(const void *obj_left, const void *obj_right, const int flags)
715 const char *left = obj_left;
716 const char *right = obj_right;
719 switch (flags & OBJ_SEARCH_MASK) {
720 case OBJ_SEARCH_OBJECT:
722 cmp = strcmp(left, right);
724 case OBJ_SEARCH_PARTIAL_KEY:
725 cmp = strncmp(left, right, strlen(right));
728 /* Sort can only work on something with a full or partial key. */
736 static struct ao2_container *pjsip_uids_onhold;
739 * \brief Add a channel ID to the list of PJSIP channels on hold
741 * \param chan_uid - Unique ID of the channel being put into the hold list
743 * \retval 0 Channel has been added to or was already in the hold list
744 * \retval -1 Failed to add channel to the hold list
746 static int chan_pjsip_add_hold(const char *chan_uid)
748 RAII_VAR(char *, hold_uid, NULL, ao2_cleanup);
750 hold_uid = ao2_find(pjsip_uids_onhold, chan_uid, OBJ_SEARCH_KEY);
752 /* Device is already on hold. Nothing to do. */
756 /* Device wasn't in hold list already. Create a new one. */
757 hold_uid = ao2_alloc_options(strlen(chan_uid) + 1, NULL,
758 AO2_ALLOC_OPT_LOCK_NOLOCK);
763 ast_copy_string(hold_uid, chan_uid, strlen(chan_uid) + 1);
765 if (ao2_link(pjsip_uids_onhold, hold_uid) == 0) {
773 * \brief Remove a channel ID from the list of PJSIP channels on hold
775 * \param chan_uid - Unique ID of the channel being taken out of the hold list
777 static void chan_pjsip_remove_hold(const char *chan_uid)
779 ao2_find(pjsip_uids_onhold, chan_uid, OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA);
783 * \brief Determine whether a channel ID is in the list of PJSIP channels on hold
785 * \param chan_uid - Channel being checked
787 * \retval 0 The channel is not in the hold list
788 * \retval 1 The channel is in the hold list
790 static int chan_pjsip_get_hold(const char *chan_uid)
792 RAII_VAR(char *, hold_uid, NULL, ao2_cleanup);
794 hold_uid = ao2_find(pjsip_uids_onhold, chan_uid, OBJ_SEARCH_KEY);
802 /*! \brief Function called to get the device state of an endpoint */
803 static int chan_pjsip_devicestate(const char *data)
805 RAII_VAR(struct ast_sip_endpoint *, endpoint, ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", data), ao2_cleanup);
806 enum ast_device_state state = AST_DEVICE_UNKNOWN;
807 RAII_VAR(struct ast_endpoint_snapshot *, endpoint_snapshot, NULL, ao2_cleanup);
808 RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
809 struct ast_devstate_aggregate aggregate;
813 return AST_DEVICE_INVALID;
816 endpoint_snapshot = ast_endpoint_latest_snapshot(ast_endpoint_get_tech(endpoint->persistent),
817 ast_endpoint_get_resource(endpoint->persistent));
819 if (!endpoint_snapshot) {
820 return AST_DEVICE_INVALID;
823 if (endpoint_snapshot->state == AST_ENDPOINT_OFFLINE) {
824 state = AST_DEVICE_UNAVAILABLE;
825 } else if (endpoint_snapshot->state == AST_ENDPOINT_ONLINE) {
826 state = AST_DEVICE_NOT_INUSE;
829 if (!endpoint_snapshot->num_channels || !(cache = ast_channel_cache())) {
833 ast_devstate_aggregate_init(&aggregate);
837 for (num = 0; num < endpoint_snapshot->num_channels; num++) {
838 RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
839 struct ast_channel_snapshot *snapshot;
841 msg = stasis_cache_get(cache, ast_channel_snapshot_type(),
842 endpoint_snapshot->channel_ids[num]);
848 snapshot = stasis_message_data(msg);
850 if (snapshot->state == AST_STATE_DOWN) {
851 ast_devstate_aggregate_add(&aggregate, AST_DEVICE_NOT_INUSE);
852 } else if (snapshot->state == AST_STATE_RINGING) {
853 ast_devstate_aggregate_add(&aggregate, AST_DEVICE_RINGING);
854 } else if ((snapshot->state == AST_STATE_UP) || (snapshot->state == AST_STATE_RING) ||
855 (snapshot->state == AST_STATE_BUSY)) {
856 if (chan_pjsip_get_hold(snapshot->uniqueid)) {
857 ast_devstate_aggregate_add(&aggregate, AST_DEVICE_ONHOLD);
859 ast_devstate_aggregate_add(&aggregate, AST_DEVICE_INUSE);
865 if (endpoint->devicestate_busy_at && (inuse == endpoint->devicestate_busy_at)) {
866 state = AST_DEVICE_BUSY;
867 } else if (ast_devstate_aggregate_result(&aggregate) != AST_DEVICE_INVALID) {
868 state = ast_devstate_aggregate_result(&aggregate);
874 /*! \brief Function called to query options on a channel */
875 static int chan_pjsip_queryoption(struct ast_channel *ast, int option, void *data, int *datalen)
877 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
878 struct ast_sip_session *session = channel->session;
880 enum ast_sip_session_t38state state = T38_STATE_UNAVAILABLE;
883 case AST_OPTION_T38_STATE:
884 if (session->endpoint->media.t38.enabled) {
885 switch (session->t38state) {
886 case T38_LOCAL_REINVITE:
887 case T38_PEER_REINVITE:
888 state = T38_STATE_NEGOTIATING;
891 state = T38_STATE_NEGOTIATED;
894 state = T38_STATE_REJECTED;
897 state = T38_STATE_UNKNOWN;
902 *((enum ast_t38_state *) data) = state;
913 static const char *chan_pjsip_get_uniqueid(struct ast_channel *ast)
915 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
916 char *uniqueid = ast_threadstorage_get(&uniqueid_threadbuf, UNIQUEID_BUFSIZE);
922 ast_copy_pj_str(uniqueid, &channel->session->inv_session->dlg->call_id->id, UNIQUEID_BUFSIZE);
927 struct indicate_data {
928 struct ast_sip_session *session;
935 static void indicate_data_destroy(void *obj)
937 struct indicate_data *ind_data = obj;
939 ast_free(ind_data->frame_data);
940 ao2_ref(ind_data->session, -1);
943 static struct indicate_data *indicate_data_alloc(struct ast_sip_session *session,
944 int condition, int response_code, const void *frame_data, size_t datalen)
946 struct indicate_data *ind_data = ao2_alloc(sizeof(*ind_data), indicate_data_destroy);
952 ind_data->frame_data = ast_malloc(datalen);
953 if (!ind_data->frame_data) {
954 ao2_ref(ind_data, -1);
958 memcpy(ind_data->frame_data, frame_data, datalen);
959 ind_data->datalen = datalen;
960 ind_data->condition = condition;
961 ind_data->response_code = response_code;
962 ao2_ref(session, +1);
963 ind_data->session = session;
968 static int indicate(void *data)
970 pjsip_tx_data *packet = NULL;
971 struct indicate_data *ind_data = data;
972 struct ast_sip_session *session = ind_data->session;
973 int response_code = ind_data->response_code;
975 if (pjsip_inv_answer(session->inv_session, response_code, NULL, NULL, &packet) == PJ_SUCCESS) {
976 ast_sip_session_send_response(session, packet);
979 ao2_ref(ind_data, -1);
984 /*! \brief Send SIP INFO with video update request */
985 static int transmit_info_with_vidupdate(void *data)
988 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n"
989 " <media_control>\r\n"
990 " <vc_primitive>\r\n"
992 " <picture_fast_update/>\r\n"
994 " </vc_primitive>\r\n"
995 " </media_control>\r\n";
997 const struct ast_sip_body body = {
998 .type = "application",
999 .subtype = "media_control+xml",
1003 RAII_VAR(struct ast_sip_session *, session, data, ao2_cleanup);
1004 struct pjsip_tx_data *tdata;
1006 if (ast_sip_create_request("INFO", session->inv_session->dlg, session->endpoint, NULL, NULL, &tdata)) {
1007 ast_log(LOG_ERROR, "Could not create text video update INFO request\n");
1010 if (ast_sip_add_body(tdata, &body)) {
1011 ast_log(LOG_ERROR, "Could not add body to text video update INFO request\n");
1014 ast_sip_session_send_request(session, tdata);
1019 /*! \brief Update connected line information */
1020 static int update_connected_line_information(void *data)
1022 RAII_VAR(struct ast_sip_session *, session, data, ao2_cleanup);
1023 struct ast_party_id connected_id;
1025 if ((ast_channel_state(session->channel) != AST_STATE_UP) && (session->inv_session->role == PJSIP_UAS_ROLE)) {
1026 int response_code = 0;
1028 if (ast_channel_state(session->channel) == AST_STATE_RING) {
1029 response_code = !session->endpoint->inband_progress ? 180 : 183;
1030 } else if (ast_channel_state(session->channel) == AST_STATE_RINGING) {
1031 response_code = 183;
1034 if (response_code) {
1035 struct pjsip_tx_data *packet = NULL;
1037 if (pjsip_inv_answer(session->inv_session, response_code, NULL, NULL, &packet) == PJ_SUCCESS) {
1038 ast_sip_session_send_response(session, packet);
1042 enum ast_sip_session_refresh_method method = session->endpoint->id.refresh_method;
1044 if (session->inv_session->invite_tsx && (session->inv_session->options & PJSIP_INV_SUPPORT_UPDATE)) {
1045 method = AST_SIP_SESSION_REFRESH_METHOD_UPDATE;
1048 connected_id = ast_channel_connected_effective_id(session->channel);
1049 if ((session->endpoint->id.send_pai || session->endpoint->id.send_rpid) &&
1050 (session->endpoint->id.trust_outbound ||
1051 ((connected_id.name.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED &&
1052 (connected_id.number.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED))) {
1053 ast_sip_session_refresh(session, NULL, NULL, NULL, method, 1);
1060 /*! \brief Function called by core to ask the channel to indicate some sort of condition */
1061 static int chan_pjsip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
1063 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
1064 struct chan_pjsip_pvt *pvt = channel->pvt;
1065 struct ast_sip_session_media *media;
1066 int response_code = 0;
1069 size_t device_buf_size;
1071 switch (condition) {
1072 case AST_CONTROL_RINGING:
1073 if (ast_channel_state(ast) == AST_STATE_RING) {
1074 if (channel->session->endpoint->inband_progress) {
1075 response_code = 183;
1078 response_code = 180;
1083 ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, "PJSIP/%s", ast_sorcery_object_get_id(channel->session->endpoint));
1085 case AST_CONTROL_BUSY:
1086 if (ast_channel_state(ast) != AST_STATE_UP) {
1087 response_code = 486;
1092 case AST_CONTROL_CONGESTION:
1093 if (ast_channel_state(ast) != AST_STATE_UP) {
1094 response_code = 503;
1099 case AST_CONTROL_INCOMPLETE:
1100 if (ast_channel_state(ast) != AST_STATE_UP) {
1101 response_code = 484;
1106 case AST_CONTROL_PROCEEDING:
1107 if (ast_channel_state(ast) != AST_STATE_UP) {
1108 response_code = 100;
1113 case AST_CONTROL_PROGRESS:
1114 if (ast_channel_state(ast) != AST_STATE_UP) {
1115 response_code = 183;
1120 case AST_CONTROL_VIDUPDATE:
1121 media = pvt->media[SIP_MEDIA_VIDEO];
1122 if (media && media->rtp) {
1123 /* FIXME: Only use this for VP8. Additional work would have to be done to
1124 * fully support other video codecs */
1125 struct ast_format_cap *fcap = ast_channel_nativeformats(ast);
1126 struct ast_format vp8;
1127 ast_format_set(&vp8, AST_FORMAT_VP8, 0);
1128 if (ast_format_cap_iscompatible(fcap, &vp8)) {
1129 /* FIXME Fake RTP write, this will be sent as an RTCP packet. Ideally the
1130 * RTP engine would provide a way to externally write/schedule RTCP
1132 struct ast_frame fr;
1133 fr.frametype = AST_FRAME_CONTROL;
1134 fr.subclass.integer = AST_CONTROL_VIDUPDATE;
1135 res = ast_rtp_instance_write(media->rtp, &fr);
1137 ao2_ref(channel->session, +1);
1139 if (ast_sip_push_task(channel->session->serializer, transmit_info_with_vidupdate, channel->session)) {
1140 ao2_cleanup(channel->session);
1147 case AST_CONTROL_CONNECTED_LINE:
1148 ao2_ref(channel->session, +1);
1149 if (ast_sip_push_task(channel->session->serializer, update_connected_line_information, channel->session)) {
1150 ao2_cleanup(channel->session);
1153 case AST_CONTROL_UPDATE_RTP_PEER:
1155 case AST_CONTROL_PVT_CAUSE_CODE:
1158 case AST_CONTROL_HOLD:
1159 chan_pjsip_add_hold(ast_channel_uniqueid(ast));
1160 device_buf_size = strlen(ast_channel_name(ast)) + 1;
1161 device_buf = alloca(device_buf_size);
1162 ast_channel_get_device_name(ast, device_buf, device_buf_size);
1163 ast_devstate_changed_literal(AST_DEVICE_ONHOLD, 1, device_buf);
1164 ast_moh_start(ast, data, NULL);
1166 case AST_CONTROL_UNHOLD:
1167 chan_pjsip_remove_hold(ast_channel_uniqueid(ast));
1168 device_buf_size = strlen(ast_channel_name(ast)) + 1;
1169 device_buf = alloca(device_buf_size);
1170 ast_channel_get_device_name(ast, device_buf, device_buf_size);
1171 ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, 1, device_buf);
1174 case AST_CONTROL_SRCUPDATE:
1176 case AST_CONTROL_SRCCHANGE:
1178 case AST_CONTROL_REDIRECTING:
1179 if (ast_channel_state(ast) != AST_STATE_UP) {
1180 response_code = 181;
1185 case AST_CONTROL_T38_PARAMETERS:
1188 if (channel->session->t38state == T38_PEER_REINVITE) {
1189 const struct ast_control_t38_parameters *parameters = data;
1191 if (parameters->request_response == AST_T38_REQUEST_PARMS) {
1192 res = AST_T38_REQUEST_PARMS;
1201 ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", condition);
1206 if (response_code) {
1207 struct indicate_data *ind_data = indicate_data_alloc(channel->session, condition, response_code, data, datalen);
1208 if (!ind_data || ast_sip_push_task(channel->session->serializer, indicate, ind_data)) {
1209 ast_log(LOG_NOTICE, "Cannot send response code %d to endpoint %s. Could not queue task properly\n",
1210 response_code, ast_sorcery_object_get_id(channel->session->endpoint));
1211 ao2_cleanup(ind_data);
1219 struct transfer_data {
1220 struct ast_sip_session *session;
1224 static void transfer_data_destroy(void *obj)
1226 struct transfer_data *trnf_data = obj;
1228 ast_free(trnf_data->target);
1229 ao2_cleanup(trnf_data->session);
1232 static struct transfer_data *transfer_data_alloc(struct ast_sip_session *session, const char *target)
1234 struct transfer_data *trnf_data = ao2_alloc(sizeof(*trnf_data), transfer_data_destroy);
1240 if (!(trnf_data->target = ast_strdup(target))) {
1241 ao2_ref(trnf_data, -1);
1245 ao2_ref(session, +1);
1246 trnf_data->session = session;
1251 static void transfer_redirect(struct ast_sip_session *session, const char *target)
1253 pjsip_tx_data *packet;
1254 enum ast_control_transfer message = AST_TRANSFER_SUCCESS;
1255 pjsip_contact_hdr *contact;
1258 if (pjsip_inv_end_session(session->inv_session, 302, NULL, &packet) != PJ_SUCCESS) {
1259 message = AST_TRANSFER_FAILED;
1260 ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
1265 if (!(contact = pjsip_msg_find_hdr(packet->msg, PJSIP_H_CONTACT, NULL))) {
1266 contact = pjsip_contact_hdr_create(packet->pool);
1269 pj_strdup2_with_null(packet->pool, &tmp, target);
1270 if (!(contact->uri = pjsip_parse_uri(packet->pool, tmp.ptr, tmp.slen, PJSIP_PARSE_URI_AS_NAMEADDR))) {
1271 message = AST_TRANSFER_FAILED;
1272 ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
1273 pjsip_tx_data_dec_ref(packet);
1277 pjsip_msg_add_hdr(packet->msg, (pjsip_hdr *) contact);
1279 ast_sip_session_send_response(session, packet);
1280 ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
1283 static void transfer_refer(struct ast_sip_session *session, const char *target)
1286 enum ast_control_transfer message = AST_TRANSFER_SUCCESS;
1288 pjsip_tx_data *packet;
1290 if (pjsip_xfer_create_uac(session->inv_session->dlg, NULL, &sub) != PJ_SUCCESS) {
1291 message = AST_TRANSFER_FAILED;
1292 ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
1297 if (pjsip_xfer_initiate(sub, pj_cstr(&tmp, target), &packet) != PJ_SUCCESS) {
1298 message = AST_TRANSFER_FAILED;
1299 ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
1300 pjsip_evsub_terminate(sub, PJ_FALSE);
1305 pjsip_xfer_send_request(sub, packet);
1306 ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
1309 static int transfer(void *data)
1311 struct transfer_data *trnf_data = data;
1313 if (ast_channel_state(trnf_data->session->channel) == AST_STATE_RING) {
1314 transfer_redirect(trnf_data->session, trnf_data->target);
1316 transfer_refer(trnf_data->session, trnf_data->target);
1319 ao2_ref(trnf_data, -1);
1323 /*! \brief Function called by core for Asterisk initiated transfer */
1324 static int chan_pjsip_transfer(struct ast_channel *chan, const char *target)
1326 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
1327 struct transfer_data *trnf_data = transfer_data_alloc(channel->session, target);
1333 if (ast_sip_push_task(channel->session->serializer, transfer, trnf_data)) {
1334 ast_log(LOG_WARNING, "Error requesting transfer\n");
1335 ao2_cleanup(trnf_data);
1342 /*! \brief Function called by core to start a DTMF digit */
1343 static int chan_pjsip_digit_begin(struct ast_channel *chan, char digit)
1345 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
1346 struct chan_pjsip_pvt *pvt = channel->pvt;
1347 struct ast_sip_session_media *media = pvt->media[SIP_MEDIA_AUDIO];
1350 switch (channel->session->endpoint->dtmf) {
1351 case AST_SIP_DTMF_RFC_4733:
1352 if (!media || !media->rtp) {
1356 ast_rtp_instance_dtmf_begin(media->rtp, digit);
1357 case AST_SIP_DTMF_NONE:
1359 case AST_SIP_DTMF_INBAND:
1369 struct info_dtmf_data {
1370 struct ast_sip_session *session;
1372 unsigned int duration;
1375 static void info_dtmf_data_destroy(void *obj)
1377 struct info_dtmf_data *dtmf_data = obj;
1378 ao2_ref(dtmf_data->session, -1);
1381 static struct info_dtmf_data *info_dtmf_data_alloc(struct ast_sip_session *session, char digit, unsigned int duration)
1383 struct info_dtmf_data *dtmf_data = ao2_alloc(sizeof(*dtmf_data), info_dtmf_data_destroy);
1387 ao2_ref(session, +1);
1388 dtmf_data->session = session;
1389 dtmf_data->digit = digit;
1390 dtmf_data->duration = duration;
1394 static int transmit_info_dtmf(void *data)
1396 RAII_VAR(struct info_dtmf_data *, dtmf_data, data, ao2_cleanup);
1398 struct ast_sip_session *session = dtmf_data->session;
1399 struct pjsip_tx_data *tdata;
1401 RAII_VAR(struct ast_str *, body_text, NULL, ast_free_ptr);
1403 struct ast_sip_body body = {
1404 .type = "application",
1405 .subtype = "dtmf-relay",
1408 if (!(body_text = ast_str_create(32))) {
1409 ast_log(LOG_ERROR, "Could not allocate buffer for INFO DTMF.\n");
1412 ast_str_set(&body_text, 0, "Signal=%c\r\nDuration=%u\r\n", dtmf_data->digit, dtmf_data->duration);
1414 body.body_text = ast_str_buffer(body_text);
1416 if (ast_sip_create_request("INFO", session->inv_session->dlg, session->endpoint, NULL, NULL, &tdata)) {
1417 ast_log(LOG_ERROR, "Could not create DTMF INFO request\n");
1420 if (ast_sip_add_body(tdata, &body)) {
1421 ast_log(LOG_ERROR, "Could not add body to DTMF INFO request\n");
1422 pjsip_tx_data_dec_ref(tdata);
1425 ast_sip_session_send_request(session, tdata);
1430 /*! \brief Function called by core to stop a DTMF digit */
1431 static int chan_pjsip_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
1433 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
1434 struct chan_pjsip_pvt *pvt = channel->pvt;
1435 struct ast_sip_session_media *media = pvt->media[SIP_MEDIA_AUDIO];
1438 switch (channel->session->endpoint->dtmf) {
1439 case AST_SIP_DTMF_INFO:
1441 struct info_dtmf_data *dtmf_data = info_dtmf_data_alloc(channel->session, digit, duration);
1447 if (ast_sip_push_task(channel->session->serializer, transmit_info_dtmf, dtmf_data)) {
1448 ast_log(LOG_WARNING, "Error sending DTMF via INFO.\n");
1449 ao2_cleanup(dtmf_data);
1454 case AST_SIP_DTMF_RFC_4733:
1455 if (!media || !media->rtp) {
1459 ast_rtp_instance_dtmf_end_with_duration(media->rtp, digit, duration);
1460 case AST_SIP_DTMF_NONE:
1462 case AST_SIP_DTMF_INBAND:
1470 static void update_initial_connected_line(struct ast_sip_session *session)
1472 struct ast_party_connected_line connected;
1473 struct ast_set_party_connected_line update_connected;
1474 struct ast_sip_endpoint_id_configuration *id = &session->endpoint->id;
1476 if (!id->self.number.valid && !id->self.name.valid) {
1480 /* Supply initial connected line information if available. */
1481 memset(&update_connected, 0, sizeof(update_connected));
1482 ast_party_connected_line_init(&connected);
1483 connected.id.number = id->self.number;
1484 connected.id.name = id->self.name;
1485 connected.id.tag = id->self.tag;
1486 connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
1488 if (connected.id.number.valid) {
1489 update_connected.id.number = 1;
1492 if (connected.id.name.valid) {
1493 update_connected.id.name = 1;
1496 /* Invalidate any earlier private connected id representation */
1497 ast_set_party_id_all(&update_connected.priv);
1499 ast_channel_queue_connected_line_update(session->channel, &connected, &update_connected);
1502 static int call(void *data)
1504 struct ast_sip_session *session = data;
1505 pjsip_tx_data *tdata;
1507 int res = ast_sip_session_create_invite(session, &tdata);
1510 ast_queue_hangup(session->channel);
1512 update_initial_connected_line(session);
1513 ast_sip_session_send_request(session, tdata);
1515 ao2_ref(session, -1);
1519 /*! \brief Function called by core to actually start calling a remote party */
1520 static int chan_pjsip_call(struct ast_channel *ast, const char *dest, int timeout)
1522 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
1524 ao2_ref(channel->session, +1);
1525 if (ast_sip_push_task(channel->session->serializer, call, channel->session)) {
1526 ast_log(LOG_WARNING, "Error attempting to place outbound call to call '%s'\n", dest);
1527 ao2_cleanup(channel->session);
1534 /*! \brief Internal function which translates from Asterisk cause codes to SIP response codes */
1535 static int hangup_cause2sip(int cause)
1538 case AST_CAUSE_UNALLOCATED: /* 1 */
1539 case AST_CAUSE_NO_ROUTE_DESTINATION: /* 3 IAX2: Can't find extension in context */
1540 case AST_CAUSE_NO_ROUTE_TRANSIT_NET: /* 2 */
1542 case AST_CAUSE_CONGESTION: /* 34 */
1543 case AST_CAUSE_SWITCH_CONGESTION: /* 42 */
1545 case AST_CAUSE_NO_USER_RESPONSE: /* 18 */
1547 case AST_CAUSE_NO_ANSWER: /* 19 */
1548 case AST_CAUSE_UNREGISTERED: /* 20 */
1550 case AST_CAUSE_CALL_REJECTED: /* 21 */
1552 case AST_CAUSE_NUMBER_CHANGED: /* 22 */
1554 case AST_CAUSE_NORMAL_UNSPECIFIED: /* 31 */
1556 case AST_CAUSE_INVALID_NUMBER_FORMAT:
1558 case AST_CAUSE_USER_BUSY:
1560 case AST_CAUSE_FAILURE:
1562 case AST_CAUSE_FACILITY_REJECTED: /* 29 */
1564 case AST_CAUSE_CHAN_NOT_IMPLEMENTED:
1566 case AST_CAUSE_DESTINATION_OUT_OF_ORDER:
1568 case AST_CAUSE_BEARERCAPABILITY_NOTAVAIL: /* Can't find codec to connect to host */
1570 case AST_CAUSE_INTERWORKING: /* Unspecified Interworking issues */
1572 case AST_CAUSE_NOTDEFINED:
1574 ast_debug(1, "AST hangup cause %d (no match found in PJSIP)\n", cause);
1582 struct hangup_data {
1584 struct ast_channel *chan;
1587 static void hangup_data_destroy(void *obj)
1589 struct hangup_data *h_data = obj;
1591 h_data->chan = ast_channel_unref(h_data->chan);
1594 static struct hangup_data *hangup_data_alloc(int cause, struct ast_channel *chan)
1596 struct hangup_data *h_data = ao2_alloc(sizeof(*h_data), hangup_data_destroy);
1602 h_data->cause = cause;
1603 h_data->chan = ast_channel_ref(chan);
1608 /*! \brief Clear a channel from a session along with its PVT */
1609 static void clear_session_and_channel(struct ast_sip_session *session, struct ast_channel *ast, struct chan_pjsip_pvt *pvt)
1611 session->channel = NULL;
1612 if (pvt->media[SIP_MEDIA_AUDIO] && pvt->media[SIP_MEDIA_AUDIO]->rtp) {
1613 ast_rtp_instance_set_channel_id(pvt->media[SIP_MEDIA_AUDIO]->rtp, "");
1615 if (pvt->media[SIP_MEDIA_VIDEO] && pvt->media[SIP_MEDIA_VIDEO]->rtp) {
1616 ast_rtp_instance_set_channel_id(pvt->media[SIP_MEDIA_VIDEO]->rtp, "");
1618 ast_channel_tech_pvt_set(ast, NULL);
1621 static int hangup(void *data)
1623 struct hangup_data *h_data = data;
1624 struct ast_channel *ast = h_data->chan;
1625 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
1626 struct chan_pjsip_pvt *pvt = channel->pvt;
1627 struct ast_sip_session *session = channel->session;
1628 int cause = h_data->cause;
1630 if (!session->defer_terminate) {
1632 pjsip_tx_data *packet = NULL;
1634 if (session->inv_session->state == PJSIP_INV_STATE_NULL) {
1635 pjsip_inv_terminate(session->inv_session, cause ? cause : 603, PJ_TRUE);
1636 } else if (((status = pjsip_inv_end_session(session->inv_session, cause ? cause : 603, NULL, &packet)) == PJ_SUCCESS)
1638 if (packet->msg->type == PJSIP_RESPONSE_MSG) {
1639 ast_sip_session_send_response(session, packet);
1641 ast_sip_session_send_request(session, packet);
1646 clear_session_and_channel(session, ast, pvt);
1647 ao2_cleanup(channel);
1648 ao2_cleanup(h_data);
1653 /*! \brief Function called by core to hang up a PJSIP session */
1654 static int chan_pjsip_hangup(struct ast_channel *ast)
1656 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
1657 struct chan_pjsip_pvt *pvt = channel->pvt;
1658 int cause = hangup_cause2sip(ast_channel_hangupcause(channel->session->channel));
1659 struct hangup_data *h_data = hangup_data_alloc(cause, ast);
1665 if (ast_sip_push_task(channel->session->serializer, hangup, h_data)) {
1666 ast_log(LOG_WARNING, "Unable to push hangup task to the threadpool. Expect bad things\n");
1673 /* Go ahead and do our cleanup of the session and channel even if we're not going
1674 * to be able to send our SIP request/response
1676 clear_session_and_channel(channel->session, ast, pvt);
1677 ao2_cleanup(channel);
1678 ao2_cleanup(h_data);
1683 struct request_data {
1684 struct ast_sip_session *session;
1685 struct ast_format_cap *caps;
1690 static int request(void *obj)
1692 struct request_data *req_data = obj;
1693 struct ast_sip_session *session = NULL;
1694 char *tmp = ast_strdupa(req_data->dest), *endpoint_name = NULL, *request_user = NULL;
1695 RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
1697 AST_DECLARE_APP_ARGS(args,
1698 AST_APP_ARG(endpoint);
1702 if (ast_strlen_zero(tmp)) {
1703 ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty destination\n");
1704 req_data->cause = AST_CAUSE_CHANNEL_UNACCEPTABLE;
1708 AST_NONSTANDARD_APP_ARGS(args, tmp, '/');
1710 /* If a request user has been specified extract it from the endpoint name portion */
1711 if ((endpoint_name = strchr(args.endpoint, '@'))) {
1712 request_user = args.endpoint;
1713 *endpoint_name++ = '\0';
1715 endpoint_name = args.endpoint;
1718 if (ast_strlen_zero(endpoint_name)) {
1719 ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty endpoint name\n");
1720 req_data->cause = AST_CAUSE_CHANNEL_UNACCEPTABLE;
1721 } else if (!(endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", endpoint_name))) {
1722 ast_log(LOG_ERROR, "Unable to create PJSIP channel - endpoint '%s' was not found\n", endpoint_name);
1723 req_data->cause = AST_CAUSE_NO_ROUTE_DESTINATION;
1727 if (!(session = ast_sip_session_create_outgoing(endpoint, NULL, args.aor, request_user, req_data->caps))) {
1728 req_data->cause = AST_CAUSE_NO_ROUTE_DESTINATION;
1732 req_data->session = session;
1737 /*! \brief Function called by core to create a new outgoing PJSIP session */
1738 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)
1740 struct request_data req_data;
1741 RAII_VAR(struct ast_sip_session *, session, NULL, ao2_cleanup);
1743 req_data.caps = cap;
1744 req_data.dest = data;
1746 if (ast_sip_push_task_synchronous(NULL, request, &req_data)) {
1747 *cause = req_data.cause;
1751 session = req_data.session;
1753 if (!(session->channel = chan_pjsip_new(session, AST_STATE_DOWN, NULL, NULL, assignedids, requestor, NULL))) {
1754 /* Session needs to be terminated prematurely */
1758 return session->channel;
1761 struct sendtext_data {
1762 struct ast_sip_session *session;
1766 static void sendtext_data_destroy(void *obj)
1768 struct sendtext_data *data = obj;
1769 ao2_ref(data->session, -1);
1772 static struct sendtext_data* sendtext_data_create(struct ast_sip_session *session, const char *text)
1774 int size = strlen(text) + 1;
1775 struct sendtext_data *data = ao2_alloc(sizeof(*data)+size, sendtext_data_destroy);
1781 data->session = session;
1782 ao2_ref(data->session, +1);
1783 ast_copy_string(data->text, text, size);
1787 static int sendtext(void *obj)
1789 RAII_VAR(struct sendtext_data *, data, obj, ao2_cleanup);
1790 pjsip_tx_data *tdata;
1792 const struct ast_sip_body body = {
1795 .body_text = data->text
1798 /* NOT ast_strlen_zero, because a zero-length message is specifically
1799 * allowed by RFC 3428 (See section 10, Examples) */
1804 ast_debug(3, "Sending in dialog SIP message\n");
1806 ast_sip_create_request("MESSAGE", data->session->inv_session->dlg, data->session->endpoint, NULL, NULL, &tdata);
1807 ast_sip_add_body(tdata, &body);
1808 ast_sip_send_request(tdata, data->session->inv_session->dlg, data->session->endpoint, NULL, NULL);
1813 /*! \brief Function called by core to send text on PJSIP session */
1814 static int chan_pjsip_sendtext(struct ast_channel *ast, const char *text)
1816 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
1817 struct sendtext_data *data = sendtext_data_create(channel->session, text);
1819 if (!data || ast_sip_push_task(channel->session->serializer, sendtext, data)) {
1826 /*! \brief Convert SIP hangup causes to Asterisk hangup causes */
1827 static int hangup_sip2cause(int cause)
1829 /* Possible values taken from causes.h */
1832 case 401: /* Unauthorized */
1833 return AST_CAUSE_CALL_REJECTED;
1834 case 403: /* Not found */
1835 return AST_CAUSE_CALL_REJECTED;
1836 case 404: /* Not found */
1837 return AST_CAUSE_UNALLOCATED;
1838 case 405: /* Method not allowed */
1839 return AST_CAUSE_INTERWORKING;
1840 case 407: /* Proxy authentication required */
1841 return AST_CAUSE_CALL_REJECTED;
1842 case 408: /* No reaction */
1843 return AST_CAUSE_NO_USER_RESPONSE;
1844 case 409: /* Conflict */
1845 return AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
1846 case 410: /* Gone */
1847 return AST_CAUSE_NUMBER_CHANGED;
1848 case 411: /* Length required */
1849 return AST_CAUSE_INTERWORKING;
1850 case 413: /* Request entity too large */
1851 return AST_CAUSE_INTERWORKING;
1852 case 414: /* Request URI too large */
1853 return AST_CAUSE_INTERWORKING;
1854 case 415: /* Unsupported media type */
1855 return AST_CAUSE_INTERWORKING;
1856 case 420: /* Bad extension */
1857 return AST_CAUSE_NO_ROUTE_DESTINATION;
1858 case 480: /* No answer */
1859 return AST_CAUSE_NO_ANSWER;
1860 case 481: /* No answer */
1861 return AST_CAUSE_INTERWORKING;
1862 case 482: /* Loop detected */
1863 return AST_CAUSE_INTERWORKING;
1864 case 483: /* Too many hops */
1865 return AST_CAUSE_NO_ANSWER;
1866 case 484: /* Address incomplete */
1867 return AST_CAUSE_INVALID_NUMBER_FORMAT;
1868 case 485: /* Ambiguous */
1869 return AST_CAUSE_UNALLOCATED;
1870 case 486: /* Busy everywhere */
1871 return AST_CAUSE_BUSY;
1872 case 487: /* Request terminated */
1873 return AST_CAUSE_INTERWORKING;
1874 case 488: /* No codecs approved */
1875 return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
1876 case 491: /* Request pending */
1877 return AST_CAUSE_INTERWORKING;
1878 case 493: /* Undecipherable */
1879 return AST_CAUSE_INTERWORKING;
1880 case 500: /* Server internal failure */
1881 return AST_CAUSE_FAILURE;
1882 case 501: /* Call rejected */
1883 return AST_CAUSE_FACILITY_REJECTED;
1885 return AST_CAUSE_DESTINATION_OUT_OF_ORDER;
1886 case 503: /* Service unavailable */
1887 return AST_CAUSE_CONGESTION;
1888 case 504: /* Gateway timeout */
1889 return AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
1890 case 505: /* SIP version not supported */
1891 return AST_CAUSE_INTERWORKING;
1892 case 600: /* Busy everywhere */
1893 return AST_CAUSE_USER_BUSY;
1894 case 603: /* Decline */
1895 return AST_CAUSE_CALL_REJECTED;
1896 case 604: /* Does not exist anywhere */
1897 return AST_CAUSE_UNALLOCATED;
1898 case 606: /* Not acceptable */
1899 return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
1901 if (cause < 500 && cause >= 400) {
1902 /* 4xx class error that is unknown - someting wrong with our request */
1903 return AST_CAUSE_INTERWORKING;
1904 } else if (cause < 600 && cause >= 500) {
1905 /* 5xx class error - problem in the remote end */
1906 return AST_CAUSE_CONGESTION;
1907 } else if (cause < 700 && cause >= 600) {
1908 /* 6xx - global errors in the 4xx class */
1909 return AST_CAUSE_INTERWORKING;
1911 return AST_CAUSE_NORMAL;
1917 static void chan_pjsip_session_begin(struct ast_sip_session *session)
1919 RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
1921 if (session->endpoint->media.direct_media.glare_mitigation ==
1922 AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_NONE) {
1926 datastore = ast_sip_session_alloc_datastore(&direct_media_mitigation_info,
1927 "direct_media_glare_mitigation");
1933 ast_sip_session_add_datastore(session, datastore);
1936 /*! \brief Function called when the session ends */
1937 static void chan_pjsip_session_end(struct ast_sip_session *session)
1939 if (!session->channel) {
1943 chan_pjsip_remove_hold(ast_channel_uniqueid(session->channel));
1945 if (!ast_channel_hangupcause(session->channel) && session->inv_session) {
1946 int cause = hangup_sip2cause(session->inv_session->cause);
1948 ast_queue_hangup_with_cause(session->channel, cause);
1950 ast_queue_hangup(session->channel);
1954 /*! \brief Function called when a request is received on the session */
1955 static int chan_pjsip_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
1957 RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
1958 struct transport_info_data *transport_data;
1959 pjsip_tx_data *packet = NULL;
1961 if (session->channel) {
1965 datastore = ast_sip_session_alloc_datastore(&transport_info, "transport_info");
1970 transport_data = ast_calloc(1, sizeof(*transport_data));
1971 if (!transport_data) {
1974 pj_sockaddr_cp(&transport_data->local_addr, &rdata->tp_info.transport->local_addr);
1975 pj_sockaddr_cp(&transport_data->remote_addr, &rdata->pkt_info.src_addr);
1976 datastore->data = transport_data;
1977 ast_sip_session_add_datastore(session, datastore);
1979 if (!(session->channel = chan_pjsip_new(session, AST_STATE_RING, session->exten, NULL, NULL, NULL, NULL))) {
1980 if (pjsip_inv_end_session(session->inv_session, 503, NULL, &packet) == PJ_SUCCESS) {
1981 ast_sip_session_send_response(session, packet);
1984 ast_log(LOG_ERROR, "Failed to allocate new PJSIP channel on incoming SIP INVITE\n");
1987 /* channel gets created on incoming request, but we wait to call start
1988 so other supplements have a chance to run */
1992 static int call_pickup_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
1994 struct ast_features_pickup_config *pickup_cfg = ast_get_chan_features_pickup_config(session->channel);
1995 struct ast_channel *chan;
1998 ast_log(LOG_ERROR, "Unable to retrieve pickup configuration options. Unable to detect call pickup extension.\n");
2002 if (strcmp(session->exten, pickup_cfg->pickupexten)) {
2003 ao2_ref(pickup_cfg, -1);
2006 ao2_ref(pickup_cfg, -1);
2008 /* We can't directly use session->channel because the pickup operation will cause a masquerade to occur,
2009 * changing the channel pointer in session to a different channel. To ensure we work on the right channel
2010 * we store a pointer locally before we begin and keep a reference so it remains valid no matter what.
2012 chan = ast_channel_ref(session->channel);
2013 if (ast_pickup_call(chan)) {
2014 ast_channel_hangupcause_set(chan, AST_CAUSE_CALL_REJECTED);
2016 ast_channel_hangupcause_set(chan, AST_CAUSE_NORMAL_CLEARING);
2018 /* A hangup always occurs because the pickup operation will have either failed resulting in the call
2019 * needing to be hung up OR the pickup operation was a success and the channel we now have is actually
2020 * the channel that was replaced, which should be hung up since it is literally in limbo not connected
2021 * to anything at all.
2024 ast_channel_unref(chan);
2029 static struct ast_sip_session_supplement call_pickup_supplement = {
2031 .priority = AST_SIP_SUPPLEMENT_PRIORITY_LAST - 1,
2032 .incoming_request = call_pickup_incoming_request,
2035 static int pbx_start_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
2039 res = ast_pbx_start(session->channel);
2042 case AST_PBX_FAILED:
2043 ast_log(LOG_WARNING, "Failed to start PBX ;(\n");
2044 ast_channel_hangupcause_set(session->channel, AST_CAUSE_SWITCH_CONGESTION);
2045 ast_hangup(session->channel);
2047 case AST_PBX_CALL_LIMIT:
2048 ast_log(LOG_WARNING, "Failed to start PBX (call limit reached) \n");
2049 ast_channel_hangupcause_set(session->channel, AST_CAUSE_SWITCH_CONGESTION);
2050 ast_hangup(session->channel);
2052 case AST_PBX_SUCCESS:
2057 ast_debug(3, "Started PBX on new PJSIP channel %s\n", ast_channel_name(session->channel));
2059 return (res == AST_PBX_SUCCESS) ? 0 : -1;
2062 static struct ast_sip_session_supplement pbx_start_supplement = {
2064 .priority = AST_SIP_SUPPLEMENT_PRIORITY_LAST,
2065 .incoming_request = pbx_start_incoming_request,
2068 /*! \brief Function called when a response is received on the session */
2069 static void chan_pjsip_incoming_response(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
2071 struct pjsip_status_line status = rdata->msg_info.msg->line.status;
2073 if (!session->channel) {
2077 switch (status.code) {
2079 ast_queue_control(session->channel, AST_CONTROL_RINGING);
2080 ast_channel_lock(session->channel);
2081 if (ast_channel_state(session->channel) != AST_STATE_UP) {
2082 ast_setstate(session->channel, AST_STATE_RINGING);
2084 ast_channel_unlock(session->channel);
2087 ast_queue_control(session->channel, AST_CONTROL_PROGRESS);
2090 ast_queue_control(session->channel, AST_CONTROL_ANSWER);
2097 static int chan_pjsip_incoming_ack(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
2099 if (rdata->msg_info.msg->line.req.method.id == PJSIP_ACK_METHOD) {
2100 if (session->endpoint->media.direct_media.enabled && session->channel) {
2101 ast_queue_control(session->channel, AST_CONTROL_SRCCHANGE);
2107 static int update_devstate(void *obj, void *arg, int flags)
2109 ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE,
2110 "PJSIP/%s", ast_sorcery_object_get_id(obj));
2114 static struct ast_custom_function chan_pjsip_dial_contacts_function = {
2115 .name = "PJSIP_DIAL_CONTACTS",
2116 .read = pjsip_acf_dial_contacts_read,
2119 static struct ast_custom_function media_offer_function = {
2120 .name = "PJSIP_MEDIA_OFFER",
2121 .read = pjsip_acf_media_offer_read,
2122 .write = pjsip_acf_media_offer_write
2126 * \brief Load the module
2128 * Module loading including tests for configuration or dependencies.
2129 * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
2130 * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
2131 * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
2132 * configuration file or other non-critical problem return
2133 * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
2135 static int load_module(void)
2137 struct ao2_container *endpoints;
2139 if (!(chan_pjsip_tech.capabilities = ast_format_cap_alloc(0))) {
2140 return AST_MODULE_LOAD_DECLINE;
2143 ast_format_cap_add_all_by_type(chan_pjsip_tech.capabilities, AST_FORMAT_TYPE_AUDIO);
2145 ast_rtp_glue_register(&chan_pjsip_rtp_glue);
2147 if (ast_channel_register(&chan_pjsip_tech)) {
2148 ast_log(LOG_ERROR, "Unable to register channel class %s\n", channel_type);
2152 if (ast_custom_function_register(&chan_pjsip_dial_contacts_function)) {
2153 ast_log(LOG_ERROR, "Unable to register PJSIP_DIAL_CONTACTS dialplan function\n");
2157 if (ast_custom_function_register(&media_offer_function)) {
2158 ast_log(LOG_WARNING, "Unable to register PJSIP_MEDIA_OFFER dialplan function\n");
2162 if (ast_sip_session_register_supplement(&chan_pjsip_supplement)) {
2163 ast_log(LOG_ERROR, "Unable to register PJSIP supplement\n");
2167 if (!(pjsip_uids_onhold = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_RWLOCK,
2168 AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT, 37, uid_hold_hash_fn,
2169 uid_hold_sort_fn, NULL))) {
2170 ast_log(LOG_ERROR, "Unable to create held channels container\n");
2174 if (ast_sip_session_register_supplement(&call_pickup_supplement)) {
2175 ast_log(LOG_ERROR, "Unable to register PJSIP call pickup supplement\n");
2176 ast_sip_session_unregister_supplement(&chan_pjsip_supplement);
2180 if (ast_sip_session_register_supplement(&pbx_start_supplement)) {
2181 ast_log(LOG_ERROR, "Unable to register PJSIP pbx start supplement\n");
2182 ast_sip_session_unregister_supplement(&chan_pjsip_supplement);
2183 ast_sip_session_unregister_supplement(&call_pickup_supplement);
2187 if (ast_sip_session_register_supplement(&chan_pjsip_ack_supplement)) {
2188 ast_log(LOG_ERROR, "Unable to register PJSIP ACK supplement\n");
2189 ast_sip_session_unregister_supplement(&pbx_start_supplement);
2190 ast_sip_session_unregister_supplement(&chan_pjsip_supplement);
2191 ast_sip_session_unregister_supplement(&call_pickup_supplement);
2195 /* since endpoints are loaded before the channel driver their device
2196 states get set to 'invalid', so they need to be updated */
2197 if ((endpoints = ast_sip_get_endpoints())) {
2198 ao2_callback(endpoints, OBJ_NODATA, update_devstate, NULL);
2199 ao2_ref(endpoints, -1);
2205 ao2_cleanup(pjsip_uids_onhold);
2206 pjsip_uids_onhold = NULL;
2207 ast_custom_function_unregister(&media_offer_function);
2208 ast_custom_function_unregister(&chan_pjsip_dial_contacts_function);
2209 ast_channel_unregister(&chan_pjsip_tech);
2210 ast_rtp_glue_unregister(&chan_pjsip_rtp_glue);
2212 return AST_MODULE_LOAD_FAILURE;
2215 /*! \brief Reload module */
2216 static int reload(void)
2221 /*! \brief Unload the PJSIP channel from Asterisk */
2222 static int unload_module(void)
2224 ao2_cleanup(pjsip_uids_onhold);
2225 pjsip_uids_onhold = NULL;
2227 ast_sip_session_unregister_supplement(&chan_pjsip_supplement);
2228 ast_sip_session_unregister_supplement(&pbx_start_supplement);
2229 ast_sip_session_unregister_supplement(&chan_pjsip_ack_supplement);
2230 ast_sip_session_unregister_supplement(&call_pickup_supplement);
2232 ast_custom_function_unregister(&media_offer_function);
2233 ast_custom_function_unregister(&chan_pjsip_dial_contacts_function);
2235 ast_channel_unregister(&chan_pjsip_tech);
2236 ast_rtp_glue_unregister(&chan_pjsip_rtp_glue);
2241 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Channel Driver",
2242 .load = load_module,
2243 .unload = unload_module,
2245 .load_pri = AST_MODPRI_CHANNEL_DRIVER,