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"
62 #include "asterisk/res_pjsip.h"
63 #include "asterisk/res_pjsip_session.h"
65 #include "pjsip/include/chan_pjsip.h"
66 #include "pjsip/include/dialplan_functions.h"
68 AST_THREADSTORAGE(uniqueid_threadbuf);
69 #define UNIQUEID_BUFSIZE 256
71 static const char desc[] = "PJSIP Channel";
72 static const char channel_type[] = "PJSIP";
74 static unsigned int chan_idx;
76 static void chan_pjsip_pvt_dtor(void *obj)
78 struct chan_pjsip_pvt *pvt = obj;
81 for (i = 0; i < SIP_MEDIA_SIZE; ++i) {
82 ao2_cleanup(pvt->media[i]);
87 /* \brief Asterisk core interaction functions */
88 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);
89 static int chan_pjsip_sendtext(struct ast_channel *ast, const char *text);
90 static int chan_pjsip_digit_begin(struct ast_channel *ast, char digit);
91 static int chan_pjsip_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
92 static int chan_pjsip_call(struct ast_channel *ast, const char *dest, int timeout);
93 static int chan_pjsip_hangup(struct ast_channel *ast);
94 static int chan_pjsip_answer(struct ast_channel *ast);
95 static struct ast_frame *chan_pjsip_read(struct ast_channel *ast);
96 static int chan_pjsip_write(struct ast_channel *ast, struct ast_frame *f);
97 static int chan_pjsip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
98 static int chan_pjsip_transfer(struct ast_channel *ast, const char *target);
99 static int chan_pjsip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
100 static int chan_pjsip_devicestate(const char *data);
101 static int chan_pjsip_queryoption(struct ast_channel *ast, int option, void *data, int *datalen);
102 static const char *chan_pjsip_get_uniqueid(struct ast_channel *ast);
104 /*! \brief PBX interface structure for channel registration */
105 struct ast_channel_tech chan_pjsip_tech = {
106 .type = channel_type,
107 .description = "PJSIP Channel Driver",
108 .requester = chan_pjsip_request,
109 .send_text = chan_pjsip_sendtext,
110 .send_digit_begin = chan_pjsip_digit_begin,
111 .send_digit_end = chan_pjsip_digit_end,
112 .call = chan_pjsip_call,
113 .hangup = chan_pjsip_hangup,
114 .answer = chan_pjsip_answer,
115 .read = chan_pjsip_read,
116 .write = chan_pjsip_write,
117 .write_video = chan_pjsip_write,
118 .exception = chan_pjsip_read,
119 .indicate = chan_pjsip_indicate,
120 .transfer = chan_pjsip_transfer,
121 .fixup = chan_pjsip_fixup,
122 .devicestate = chan_pjsip_devicestate,
123 .queryoption = chan_pjsip_queryoption,
124 .func_channel_read = pjsip_acf_channel_read,
125 .get_pvt_uniqueid = chan_pjsip_get_uniqueid,
126 .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER
129 /*! \brief SIP session interaction functions */
130 static void chan_pjsip_session_begin(struct ast_sip_session *session);
131 static void chan_pjsip_session_end(struct ast_sip_session *session);
132 static int chan_pjsip_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
133 static void chan_pjsip_incoming_response(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
135 /*! \brief SIP session supplement structure */
136 static struct ast_sip_session_supplement chan_pjsip_supplement = {
138 .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL,
139 .session_begin = chan_pjsip_session_begin,
140 .session_end = chan_pjsip_session_end,
141 .incoming_request = chan_pjsip_incoming_request,
142 .incoming_response = chan_pjsip_incoming_response,
145 static int chan_pjsip_incoming_ack(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
147 static struct ast_sip_session_supplement chan_pjsip_ack_supplement = {
149 .priority = AST_SIP_SUPPLEMENT_PRIORITY_CHANNEL,
150 .incoming_request = chan_pjsip_incoming_ack,
153 /*! \brief Function called by RTP engine to get local audio RTP peer */
154 static enum ast_rtp_glue_result chan_pjsip_get_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
156 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
157 struct chan_pjsip_pvt *pvt = channel->pvt;
158 struct ast_sip_endpoint *endpoint;
160 if (!pvt || !channel->session || !pvt->media[SIP_MEDIA_AUDIO]->rtp) {
161 return AST_RTP_GLUE_RESULT_FORBID;
164 endpoint = channel->session->endpoint;
166 *instance = pvt->media[SIP_MEDIA_AUDIO]->rtp;
167 ao2_ref(*instance, +1);
169 ast_assert(endpoint != NULL);
170 if (endpoint->media.rtp.encryption != AST_SIP_MEDIA_ENCRYPT_NONE) {
171 return AST_RTP_GLUE_RESULT_FORBID;
174 if (endpoint->media.direct_media.enabled) {
175 return AST_RTP_GLUE_RESULT_REMOTE;
178 return AST_RTP_GLUE_RESULT_LOCAL;
181 /*! \brief Function called by RTP engine to get local video RTP peer */
182 static enum ast_rtp_glue_result chan_pjsip_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
184 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
185 struct chan_pjsip_pvt *pvt = channel->pvt;
186 struct ast_sip_endpoint *endpoint;
188 if (!pvt || !channel->session || !pvt->media[SIP_MEDIA_VIDEO]->rtp) {
189 return AST_RTP_GLUE_RESULT_FORBID;
192 endpoint = channel->session->endpoint;
194 *instance = pvt->media[SIP_MEDIA_VIDEO]->rtp;
195 ao2_ref(*instance, +1);
197 ast_assert(endpoint != NULL);
198 if (endpoint->media.rtp.encryption != AST_SIP_MEDIA_ENCRYPT_NONE) {
199 return AST_RTP_GLUE_RESULT_FORBID;
202 return AST_RTP_GLUE_RESULT_LOCAL;
205 /*! \brief Function called by RTP engine to get peer capabilities */
206 static void chan_pjsip_get_codec(struct ast_channel *chan, struct ast_format_cap *result)
208 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
210 ast_format_cap_copy(result, channel->session->endpoint->media.codecs);
213 static int send_direct_media_request(void *data)
215 RAII_VAR(struct ast_sip_session *, session, data, ao2_cleanup);
217 return ast_sip_session_refresh(session, NULL, NULL, NULL,
218 session->endpoint->media.direct_media.method, 1);
221 /*! \brief Destructor function for \ref transport_info_data */
222 static void transport_info_destroy(void *obj)
224 struct transport_info_data *data = obj;
228 /*! \brief Datastore used to store local/remote addresses for the
229 * INVITE request that created the PJSIP channel */
230 static struct ast_datastore_info transport_info = {
231 .type = "chan_pjsip_transport_info",
232 .destroy = transport_info_destroy,
235 static struct ast_datastore_info direct_media_mitigation_info = { };
237 static int direct_media_mitigate_glare(struct ast_sip_session *session)
239 RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
241 if (session->endpoint->media.direct_media.glare_mitigation ==
242 AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_NONE) {
246 datastore = ast_sip_session_get_datastore(session, "direct_media_glare_mitigation");
251 /* Removing the datastore ensures we won't try to mitigate glare on subsequent reinvites */
252 ast_sip_session_remove_datastore(session, "direct_media_glare_mitigation");
254 if ((session->endpoint->media.direct_media.glare_mitigation ==
255 AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_OUTGOING &&
256 session->inv_session->role == PJSIP_ROLE_UAC) ||
257 (session->endpoint->media.direct_media.glare_mitigation ==
258 AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_INCOMING &&
259 session->inv_session->role == PJSIP_ROLE_UAS)) {
266 static int check_for_rtp_changes(struct ast_channel *chan, struct ast_rtp_instance *rtp,
267 struct ast_sip_session_media *media, int rtcp_fd)
272 changed = ast_rtp_instance_get_and_cmp_remote_address(rtp, &media->direct_media_addr);
274 ast_channel_set_fd(chan, rtcp_fd, -1);
275 ast_rtp_instance_set_prop(media->rtp, AST_RTP_PROPERTY_RTCP, 0);
277 } else if (!ast_sockaddr_isnull(&media->direct_media_addr)){
278 ast_sockaddr_setnull(&media->direct_media_addr);
281 ast_rtp_instance_set_prop(media->rtp, AST_RTP_PROPERTY_RTCP, 1);
282 ast_channel_set_fd(chan, rtcp_fd, ast_rtp_instance_fd(media->rtp, 1));
289 /*! \brief Function called by RTP engine to change where the remote party should send media */
290 static int chan_pjsip_set_rtp_peer(struct ast_channel *chan,
291 struct ast_rtp_instance *rtp,
292 struct ast_rtp_instance *vrtp,
293 struct ast_rtp_instance *tpeer,
294 const struct ast_format_cap *cap,
297 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
298 struct chan_pjsip_pvt *pvt = channel->pvt;
299 struct ast_sip_session *session = channel->session;
302 /* Don't try to do any direct media shenanigans on early bridges */
303 if ((rtp || vrtp || tpeer) && !ast_channel_is_bridged(chan)) {
307 if (nat_active && session->endpoint->media.direct_media.disable_on_nat) {
311 if (pvt->media[SIP_MEDIA_AUDIO]) {
312 changed |= check_for_rtp_changes(chan, rtp, pvt->media[SIP_MEDIA_AUDIO], 1);
314 if (pvt->media[SIP_MEDIA_VIDEO]) {
315 changed |= check_for_rtp_changes(chan, vrtp, pvt->media[SIP_MEDIA_VIDEO], 3);
318 if (direct_media_mitigate_glare(session)) {
322 if (cap && !ast_format_cap_is_empty(cap) && !ast_format_cap_identical(session->direct_media_cap, cap)) {
323 ast_format_cap_copy(session->direct_media_cap, cap);
328 ao2_ref(session, +1);
331 if (ast_sip_push_task(session->serializer, send_direct_media_request, session)) {
332 ao2_cleanup(session);
339 /*! \brief Local glue for interacting with the RTP engine core */
340 static struct ast_rtp_glue chan_pjsip_rtp_glue = {
342 .get_rtp_info = chan_pjsip_get_rtp_peer,
343 .get_vrtp_info = chan_pjsip_get_vrtp_peer,
344 .get_codec = chan_pjsip_get_codec,
345 .update_peer = chan_pjsip_set_rtp_peer,
348 /*! \brief Function called to create a new PJSIP Asterisk channel */
349 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)
351 struct ast_channel *chan;
352 struct ast_format fmt;
353 RAII_VAR(struct chan_pjsip_pvt *, pvt, NULL, ao2_cleanup);
354 struct ast_sip_channel_pvt *channel;
355 struct ast_variable *var;
357 if (!(pvt = ao2_alloc(sizeof(*pvt), chan_pjsip_pvt_dtor))) {
361 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),
362 ast_atomic_fetchadd_int((int *)&chan_idx, +1)))) {
366 ast_channel_tech_set(chan, &chan_pjsip_tech);
368 if (!(channel = ast_sip_channel_pvt_alloc(pvt, session))) {
369 ast_channel_unlock(chan);
374 for (var = session->endpoint->channel_vars; var; var = var->next) {
376 pbx_builtin_setvar_helper(chan, var->name, ast_get_encoded_str(
377 var->value, buf, sizeof(buf)));
380 ast_channel_stage_snapshot(chan);
382 ast_channel_tech_pvt_set(chan, channel);
384 if (ast_format_cap_is_empty(session->req_caps) || !ast_format_cap_has_joint(session->req_caps, session->endpoint->media.codecs)) {
385 ast_format_cap_copy(ast_channel_nativeformats(chan), session->endpoint->media.codecs);
387 ast_format_cap_copy(ast_channel_nativeformats(chan), session->req_caps);
390 ast_codec_choose(&session->endpoint->media.prefs, ast_channel_nativeformats(chan), 1, &fmt);
391 ast_format_copy(ast_channel_writeformat(chan), &fmt);
392 ast_format_copy(ast_channel_rawwriteformat(chan), &fmt);
393 ast_format_copy(ast_channel_readformat(chan), &fmt);
394 ast_format_copy(ast_channel_rawreadformat(chan), &fmt);
396 if (state == AST_STATE_RING) {
397 ast_channel_rings_set(chan, 1);
400 ast_channel_adsicpe_set(chan, AST_ADSI_UNAVAILABLE);
402 ast_channel_context_set(chan, session->endpoint->context);
403 ast_channel_exten_set(chan, S_OR(exten, "s"));
404 ast_channel_priority_set(chan, 1);
406 ast_channel_callgroup_set(chan, session->endpoint->pickup.callgroup);
407 ast_channel_pickupgroup_set(chan, session->endpoint->pickup.pickupgroup);
409 ast_channel_named_callgroups_set(chan, session->endpoint->pickup.named_callgroups);
410 ast_channel_named_pickupgroups_set(chan, session->endpoint->pickup.named_pickupgroups);
412 if (!ast_strlen_zero(session->endpoint->language)) {
413 ast_channel_language_set(chan, session->endpoint->language);
416 if (!ast_strlen_zero(session->endpoint->zone)) {
417 struct ast_tone_zone *zone = ast_get_indication_zone(session->endpoint->zone);
419 ast_log(LOG_ERROR, "Unknown country code '%s' for tonezone. Check indications.conf for available country codes.\n", session->endpoint->zone);
421 ast_channel_zone_set(chan, zone);
424 ast_channel_stage_snapshot_done(chan);
425 ast_channel_unlock(chan);
427 /* If res_pjsip_session is ever updated to create/destroy ast_sip_session_media
428 * during a call such as if multiple same-type stream support is introduced,
429 * these will need to be recaptured as well */
430 pvt->media[SIP_MEDIA_AUDIO] = ao2_find(session->media, "audio", OBJ_KEY);
431 pvt->media[SIP_MEDIA_VIDEO] = ao2_find(session->media, "video", OBJ_KEY);
432 if (pvt->media[SIP_MEDIA_AUDIO] && pvt->media[SIP_MEDIA_AUDIO]->rtp) {
433 ast_rtp_instance_set_channel_id(pvt->media[SIP_MEDIA_AUDIO]->rtp, ast_channel_uniqueid(chan));
435 if (pvt->media[SIP_MEDIA_VIDEO] && pvt->media[SIP_MEDIA_VIDEO]->rtp) {
436 ast_rtp_instance_set_channel_id(pvt->media[SIP_MEDIA_VIDEO]->rtp, ast_channel_uniqueid(chan));
439 ast_endpoint_add_channel(session->endpoint->persistent, chan);
444 static int answer(void *data)
446 pj_status_t status = PJ_SUCCESS;
447 pjsip_tx_data *packet = NULL;
448 struct ast_sip_session *session = data;
450 pjsip_dlg_inc_lock(session->inv_session->dlg);
451 if (session->inv_session->invite_tsx) {
452 status = pjsip_inv_answer(session->inv_session, 200, NULL, NULL, &packet);
454 ast_log(LOG_ERROR,"Cannot answer '%s' because there is no associated SIP transaction\n",
455 ast_channel_name(session->channel));
457 pjsip_dlg_dec_lock(session->inv_session->dlg);
459 if (status == PJ_SUCCESS && packet) {
460 ast_sip_session_send_response(session, packet);
463 ao2_ref(session, -1);
465 return (status == PJ_SUCCESS) ? 0 : -1;
468 /*! \brief Function called by core when we should answer a PJSIP session */
469 static int chan_pjsip_answer(struct ast_channel *ast)
471 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
473 if (ast_channel_state(ast) == AST_STATE_UP) {
477 ast_setstate(ast, AST_STATE_UP);
479 ao2_ref(channel->session, +1);
480 if (ast_sip_push_task(channel->session->serializer, answer, channel->session)) {
481 ast_log(LOG_WARNING, "Unable to push answer task to the threadpool. Cannot answer call\n");
482 ao2_cleanup(channel->session);
489 /*! \brief Internal helper function called when CNG tone is detected */
490 static struct ast_frame *chan_pjsip_cng_tone_detected(struct ast_sip_session *session, struct ast_frame *f)
492 const char *target_context;
495 /* If we only needed this DSP for fax detection purposes we can just drop it now */
496 if (session->endpoint->dtmf == AST_SIP_DTMF_INBAND) {
497 ast_dsp_set_features(session->dsp, DSP_FEATURE_DIGIT_DETECT);
499 ast_dsp_free(session->dsp);
503 /* If already executing in the fax extension don't do anything */
504 if (!strcmp(ast_channel_exten(session->channel), "fax")) {
508 target_context = S_OR(ast_channel_macrocontext(session->channel), ast_channel_context(session->channel));
510 /* We need to unlock the channel here because ast_exists_extension has the
511 * potential to start and stop an autoservice on the channel. Such action
512 * is prone to deadlock if the channel is locked.
514 ast_channel_unlock(session->channel);
515 exists = ast_exists_extension(session->channel, target_context, "fax", 1,
516 S_COR(ast_channel_caller(session->channel)->id.number.valid,
517 ast_channel_caller(session->channel)->id.number.str, NULL));
518 ast_channel_lock(session->channel);
521 ast_verb(2, "Redirecting '%s' to fax extension due to CNG detection\n",
522 ast_channel_name(session->channel));
523 pbx_builtin_setvar_helper(session->channel, "FAXEXTEN", ast_channel_exten(session->channel));
524 if (ast_async_goto(session->channel, target_context, "fax", 1)) {
525 ast_log(LOG_ERROR, "Failed to async goto '%s' into fax extension in '%s'\n",
526 ast_channel_name(session->channel), target_context);
531 ast_log(LOG_NOTICE, "FAX CNG detected on '%s' but no fax extension in '%s'\n",
532 ast_channel_name(session->channel), target_context);
538 /*! \brief Function called by core to read any waiting frames */
539 static struct ast_frame *chan_pjsip_read(struct ast_channel *ast)
541 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
542 struct chan_pjsip_pvt *pvt = channel->pvt;
544 struct ast_sip_session_media *media = NULL;
546 int fdno = ast_channel_fdno(ast);
550 media = pvt->media[SIP_MEDIA_AUDIO];
553 media = pvt->media[SIP_MEDIA_AUDIO];
557 media = pvt->media[SIP_MEDIA_VIDEO];
560 media = pvt->media[SIP_MEDIA_VIDEO];
565 if (!media || !media->rtp) {
566 return &ast_null_frame;
569 if (!(f = ast_rtp_instance_read(media->rtp, rtcp))) {
573 if (f->frametype != AST_FRAME_VOICE) {
577 if (!(ast_format_cap_iscompatible(ast_channel_nativeformats(ast), &f->subclass.format))) {
578 ast_debug(1, "Oooh, format changed to %s\n", ast_getformatname(&f->subclass.format));
579 ast_format_cap_set(ast_channel_nativeformats(ast), &f->subclass.format);
580 ast_set_read_format(ast, ast_channel_readformat(ast));
581 ast_set_write_format(ast, ast_channel_writeformat(ast));
584 if (channel->session->dsp) {
585 f = ast_dsp_process(ast, channel->session->dsp, f);
587 if (f && (f->frametype == AST_FRAME_DTMF)) {
588 if (f->subclass.integer == 'f') {
589 ast_debug(3, "Fax CNG detected on %s\n", ast_channel_name(ast));
590 f = chan_pjsip_cng_tone_detected(channel->session, f);
592 ast_debug(3, "* Detected inband DTMF '%c' on '%s'\n", f->subclass.integer,
593 ast_channel_name(ast));
601 /*! \brief Function called by core to write frames */
602 static int chan_pjsip_write(struct ast_channel *ast, struct ast_frame *frame)
604 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
605 struct chan_pjsip_pvt *pvt = channel->pvt;
606 struct ast_sip_session_media *media;
609 switch (frame->frametype) {
610 case AST_FRAME_VOICE:
611 media = pvt->media[SIP_MEDIA_AUDIO];
616 if (!(ast_format_cap_iscompatible(ast_channel_nativeformats(ast), &frame->subclass.format))) {
620 "Asked to transmit frame type %s, while native formats is %s (read/write = %s/%s)\n",
621 ast_getformatname(&frame->subclass.format),
622 ast_getformatname_multiple(buf, sizeof(buf), ast_channel_nativeformats(ast)),
623 ast_getformatname(ast_channel_readformat(ast)),
624 ast_getformatname(ast_channel_writeformat(ast)));
628 res = ast_rtp_instance_write(media->rtp, frame);
631 case AST_FRAME_VIDEO:
632 if ((media = pvt->media[SIP_MEDIA_VIDEO]) && media->rtp) {
633 res = ast_rtp_instance_write(media->rtp, frame);
636 case AST_FRAME_MODEM:
639 ast_log(LOG_WARNING, "Can't send %d type frames with PJSIP\n", frame->frametype);
647 struct ast_sip_session *session;
648 struct ast_channel *chan;
651 static int fixup(void *data)
653 struct fixup_data *fix_data = data;
654 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(fix_data->chan);
655 struct chan_pjsip_pvt *pvt = channel->pvt;
657 channel->session->channel = fix_data->chan;
658 if (pvt->media[SIP_MEDIA_AUDIO] && pvt->media[SIP_MEDIA_AUDIO]->rtp) {
659 ast_rtp_instance_set_channel_id(pvt->media[SIP_MEDIA_AUDIO]->rtp, ast_channel_uniqueid(fix_data->chan));
661 if (pvt->media[SIP_MEDIA_VIDEO] && pvt->media[SIP_MEDIA_VIDEO]->rtp) {
662 ast_rtp_instance_set_channel_id(pvt->media[SIP_MEDIA_VIDEO]->rtp, ast_channel_uniqueid(fix_data->chan));
668 /*! \brief Function called by core to change the underlying owner channel */
669 static int chan_pjsip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
671 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(newchan);
672 struct fixup_data fix_data;
674 fix_data.session = channel->session;
675 fix_data.chan = newchan;
677 if (channel->session->channel != oldchan) {
681 if (ast_sip_push_task_synchronous(channel->session->serializer, fixup, &fix_data)) {
682 ast_log(LOG_WARNING, "Unable to perform channel fixup\n");
689 /*! AO2 hash function for on hold UIDs */
690 static int uid_hold_hash_fn(const void *obj, const int flags)
692 const char *key = obj;
694 switch (flags & OBJ_SEARCH_MASK) {
697 case OBJ_SEARCH_OBJECT:
700 /* Hash can only work on something with a full key. */
704 return ast_str_hash(key);
707 /*! AO2 sort function for on hold UIDs */
708 static int uid_hold_sort_fn(const void *obj_left, const void *obj_right, const int flags)
710 const char *left = obj_left;
711 const char *right = obj_right;
714 switch (flags & OBJ_SEARCH_MASK) {
715 case OBJ_SEARCH_OBJECT:
717 cmp = strcmp(left, right);
719 case OBJ_SEARCH_PARTIAL_KEY:
720 cmp = strncmp(left, right, strlen(right));
723 /* Sort can only work on something with a full or partial key. */
731 static struct ao2_container *pjsip_uids_onhold;
734 * \brief Add a channel ID to the list of PJSIP channels on hold
736 * \param chan_uid - Unique ID of the channel being put into the hold list
738 * \retval 0 Channel has been added to or was already in the hold list
739 * \retval -1 Failed to add channel to the hold list
741 static int chan_pjsip_add_hold(const char *chan_uid)
743 RAII_VAR(char *, hold_uid, NULL, ao2_cleanup);
745 hold_uid = ao2_find(pjsip_uids_onhold, chan_uid, OBJ_SEARCH_KEY);
747 /* Device is already on hold. Nothing to do. */
751 /* Device wasn't in hold list already. Create a new one. */
752 hold_uid = ao2_alloc_options(strlen(chan_uid) + 1, NULL,
753 AO2_ALLOC_OPT_LOCK_NOLOCK);
758 ast_copy_string(hold_uid, chan_uid, strlen(chan_uid) + 1);
760 if (ao2_link(pjsip_uids_onhold, hold_uid) == 0) {
768 * \brief Remove a channel ID from the list of PJSIP channels on hold
770 * \param chan_uid - Unique ID of the channel being taken out of the hold list
772 static void chan_pjsip_remove_hold(const char *chan_uid)
774 ao2_find(pjsip_uids_onhold, chan_uid, OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA);
778 * \brief Determine whether a channel ID is in the list of PJSIP channels on hold
780 * \param chan_uid - Channel being checked
782 * \retval 0 The channel is not in the hold list
783 * \retval 1 The channel is in the hold list
785 static int chan_pjsip_get_hold(const char *chan_uid)
787 RAII_VAR(char *, hold_uid, NULL, ao2_cleanup);
789 hold_uid = ao2_find(pjsip_uids_onhold, chan_uid, OBJ_SEARCH_KEY);
797 /*! \brief Function called to get the device state of an endpoint */
798 static int chan_pjsip_devicestate(const char *data)
800 RAII_VAR(struct ast_sip_endpoint *, endpoint, ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", data), ao2_cleanup);
801 enum ast_device_state state = AST_DEVICE_UNKNOWN;
802 RAII_VAR(struct ast_endpoint_snapshot *, endpoint_snapshot, NULL, ao2_cleanup);
803 RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
804 struct ast_devstate_aggregate aggregate;
808 return AST_DEVICE_INVALID;
811 endpoint_snapshot = ast_endpoint_latest_snapshot(ast_endpoint_get_tech(endpoint->persistent),
812 ast_endpoint_get_resource(endpoint->persistent));
814 if (!endpoint_snapshot) {
815 return AST_DEVICE_INVALID;
818 if (endpoint_snapshot->state == AST_ENDPOINT_OFFLINE) {
819 state = AST_DEVICE_UNAVAILABLE;
820 } else if (endpoint_snapshot->state == AST_ENDPOINT_ONLINE) {
821 state = AST_DEVICE_NOT_INUSE;
824 if (!endpoint_snapshot->num_channels || !(cache = ast_channel_cache())) {
828 ast_devstate_aggregate_init(&aggregate);
832 for (num = 0; num < endpoint_snapshot->num_channels; num++) {
833 RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
834 struct ast_channel_snapshot *snapshot;
836 msg = stasis_cache_get(cache, ast_channel_snapshot_type(),
837 endpoint_snapshot->channel_ids[num]);
843 snapshot = stasis_message_data(msg);
845 if (snapshot->state == AST_STATE_DOWN) {
846 ast_devstate_aggregate_add(&aggregate, AST_DEVICE_NOT_INUSE);
847 } else if (snapshot->state == AST_STATE_RINGING) {
848 ast_devstate_aggregate_add(&aggregate, AST_DEVICE_RINGING);
849 } else if ((snapshot->state == AST_STATE_UP) || (snapshot->state == AST_STATE_RING) ||
850 (snapshot->state == AST_STATE_BUSY)) {
851 if (chan_pjsip_get_hold(snapshot->uniqueid)) {
852 ast_devstate_aggregate_add(&aggregate, AST_DEVICE_ONHOLD);
854 ast_devstate_aggregate_add(&aggregate, AST_DEVICE_INUSE);
860 if (endpoint->devicestate_busy_at && (inuse == endpoint->devicestate_busy_at)) {
861 state = AST_DEVICE_BUSY;
862 } else if (ast_devstate_aggregate_result(&aggregate) != AST_DEVICE_INVALID) {
863 state = ast_devstate_aggregate_result(&aggregate);
869 /*! \brief Function called to query options on a channel */
870 static int chan_pjsip_queryoption(struct ast_channel *ast, int option, void *data, int *datalen)
872 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
873 struct ast_sip_session *session = channel->session;
875 enum ast_sip_session_t38state state = T38_STATE_UNAVAILABLE;
878 case AST_OPTION_T38_STATE:
879 if (session->endpoint->media.t38.enabled) {
880 switch (session->t38state) {
881 case T38_LOCAL_REINVITE:
882 case T38_PEER_REINVITE:
883 state = T38_STATE_NEGOTIATING;
886 state = T38_STATE_NEGOTIATED;
889 state = T38_STATE_REJECTED;
892 state = T38_STATE_UNKNOWN;
897 *((enum ast_t38_state *) data) = state;
908 struct uniqueid_data {
909 struct ast_sip_session *session;
913 static int get_uniqueid(void *data)
915 struct uniqueid_data *uid_data = data;
917 ast_copy_pj_str(uid_data->uniqueid, &uid_data->session->inv_session->dlg->call_id->id, UNIQUEID_BUFSIZE);
922 static const char *chan_pjsip_get_uniqueid(struct ast_channel *ast)
924 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
925 struct uniqueid_data uid_data = {
926 .session = channel->session,
927 .uniqueid = ast_threadstorage_get(&uniqueid_threadbuf, UNIQUEID_BUFSIZE),
930 if (!uid_data.uniqueid ||
931 ast_sip_push_task_synchronous(channel->session->serializer, get_uniqueid, &uid_data)) {
935 return uid_data.uniqueid;
938 struct indicate_data {
939 struct ast_sip_session *session;
946 static void indicate_data_destroy(void *obj)
948 struct indicate_data *ind_data = obj;
950 ast_free(ind_data->frame_data);
951 ao2_ref(ind_data->session, -1);
954 static struct indicate_data *indicate_data_alloc(struct ast_sip_session *session,
955 int condition, int response_code, const void *frame_data, size_t datalen)
957 struct indicate_data *ind_data = ao2_alloc(sizeof(*ind_data), indicate_data_destroy);
963 ind_data->frame_data = ast_malloc(datalen);
964 if (!ind_data->frame_data) {
965 ao2_ref(ind_data, -1);
969 memcpy(ind_data->frame_data, frame_data, datalen);
970 ind_data->datalen = datalen;
971 ind_data->condition = condition;
972 ind_data->response_code = response_code;
973 ao2_ref(session, +1);
974 ind_data->session = session;
979 static int indicate(void *data)
981 pjsip_tx_data *packet = NULL;
982 struct indicate_data *ind_data = data;
983 struct ast_sip_session *session = ind_data->session;
984 int response_code = ind_data->response_code;
986 if (pjsip_inv_answer(session->inv_session, response_code, NULL, NULL, &packet) == PJ_SUCCESS) {
987 ast_sip_session_send_response(session, packet);
990 ao2_ref(ind_data, -1);
995 /*! \brief Send SIP INFO with video update request */
996 static int transmit_info_with_vidupdate(void *data)
999 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n"
1000 " <media_control>\r\n"
1001 " <vc_primitive>\r\n"
1003 " <picture_fast_update/>\r\n"
1004 " </to_encoder>\r\n"
1005 " </vc_primitive>\r\n"
1006 " </media_control>\r\n";
1008 const struct ast_sip_body body = {
1009 .type = "application",
1010 .subtype = "media_control+xml",
1014 RAII_VAR(struct ast_sip_session *, session, data, ao2_cleanup);
1015 struct pjsip_tx_data *tdata;
1017 if (ast_sip_create_request("INFO", session->inv_session->dlg, session->endpoint, NULL, NULL, &tdata)) {
1018 ast_log(LOG_ERROR, "Could not create text video update INFO request\n");
1021 if (ast_sip_add_body(tdata, &body)) {
1022 ast_log(LOG_ERROR, "Could not add body to text video update INFO request\n");
1025 ast_sip_session_send_request(session, tdata);
1030 /*! \brief Update connected line information */
1031 static int update_connected_line_information(void *data)
1033 RAII_VAR(struct ast_sip_session *, session, data, ao2_cleanup);
1034 struct ast_party_id connected_id;
1036 if ((ast_channel_state(session->channel) != AST_STATE_UP) && (session->inv_session->role == PJSIP_UAS_ROLE)) {
1037 int response_code = 0;
1039 if (ast_channel_state(session->channel) == AST_STATE_RING) {
1040 response_code = !session->endpoint->inband_progress ? 180 : 183;
1041 } else if (ast_channel_state(session->channel) == AST_STATE_RINGING) {
1042 response_code = 183;
1045 if (response_code) {
1046 struct pjsip_tx_data *packet = NULL;
1048 if (pjsip_inv_answer(session->inv_session, response_code, NULL, NULL, &packet) == PJ_SUCCESS) {
1049 ast_sip_session_send_response(session, packet);
1053 enum ast_sip_session_refresh_method method = session->endpoint->id.refresh_method;
1055 if (session->inv_session->invite_tsx && (session->inv_session->options & PJSIP_INV_SUPPORT_UPDATE)) {
1056 method = AST_SIP_SESSION_REFRESH_METHOD_UPDATE;
1059 connected_id = ast_channel_connected_effective_id(session->channel);
1060 if ((session->endpoint->id.send_pai || session->endpoint->id.send_rpid) &&
1061 (session->endpoint->id.trust_outbound ||
1062 ((connected_id.name.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED &&
1063 (connected_id.number.presentation & AST_PRES_RESTRICTION) == AST_PRES_ALLOWED))) {
1064 ast_sip_session_refresh(session, NULL, NULL, NULL, method, 1);
1071 /*! \brief Function called by core to ask the channel to indicate some sort of condition */
1072 static int chan_pjsip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
1074 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
1075 struct chan_pjsip_pvt *pvt = channel->pvt;
1076 struct ast_sip_session_media *media;
1077 int response_code = 0;
1080 size_t device_buf_size;
1082 switch (condition) {
1083 case AST_CONTROL_RINGING:
1084 if (ast_channel_state(ast) == AST_STATE_RING) {
1085 if (channel->session->endpoint->inband_progress) {
1086 response_code = 183;
1089 response_code = 180;
1094 ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, "PJSIP/%s", ast_sorcery_object_get_id(channel->session->endpoint));
1096 case AST_CONTROL_BUSY:
1097 if (ast_channel_state(ast) != AST_STATE_UP) {
1098 response_code = 486;
1103 case AST_CONTROL_CONGESTION:
1104 if (ast_channel_state(ast) != AST_STATE_UP) {
1105 response_code = 503;
1110 case AST_CONTROL_INCOMPLETE:
1111 if (ast_channel_state(ast) != AST_STATE_UP) {
1112 response_code = 484;
1117 case AST_CONTROL_PROCEEDING:
1118 if (ast_channel_state(ast) != AST_STATE_UP) {
1119 response_code = 100;
1124 case AST_CONTROL_PROGRESS:
1125 if (ast_channel_state(ast) != AST_STATE_UP) {
1126 response_code = 183;
1131 case AST_CONTROL_VIDUPDATE:
1132 media = pvt->media[SIP_MEDIA_VIDEO];
1133 if (media && media->rtp) {
1134 /* FIXME: Only use this for VP8. Additional work would have to be done to
1135 * fully support other video codecs */
1136 struct ast_format_cap *fcap = ast_channel_nativeformats(ast);
1137 struct ast_format vp8;
1138 ast_format_set(&vp8, AST_FORMAT_VP8, 0);
1139 if (ast_format_cap_iscompatible(fcap, &vp8)) {
1140 /* FIXME Fake RTP write, this will be sent as an RTCP packet. Ideally the
1141 * RTP engine would provide a way to externally write/schedule RTCP
1143 struct ast_frame fr;
1144 fr.frametype = AST_FRAME_CONTROL;
1145 fr.subclass.integer = AST_CONTROL_VIDUPDATE;
1146 res = ast_rtp_instance_write(media->rtp, &fr);
1148 ao2_ref(channel->session, +1);
1150 if (ast_sip_push_task(channel->session->serializer, transmit_info_with_vidupdate, channel->session)) {
1151 ao2_cleanup(channel->session);
1158 case AST_CONTROL_CONNECTED_LINE:
1159 ao2_ref(channel->session, +1);
1160 if (ast_sip_push_task(channel->session->serializer, update_connected_line_information, channel->session)) {
1161 ao2_cleanup(channel->session);
1164 case AST_CONTROL_UPDATE_RTP_PEER:
1166 case AST_CONTROL_PVT_CAUSE_CODE:
1169 case AST_CONTROL_HOLD:
1170 chan_pjsip_add_hold(ast_channel_uniqueid(ast));
1171 device_buf_size = strlen(ast_channel_name(ast)) + 1;
1172 device_buf = alloca(device_buf_size);
1173 ast_channel_get_device_name(ast, device_buf, device_buf_size);
1174 ast_devstate_changed_literal(AST_DEVICE_ONHOLD, 1, device_buf);
1175 ast_moh_start(ast, data, NULL);
1177 case AST_CONTROL_UNHOLD:
1178 chan_pjsip_remove_hold(ast_channel_uniqueid(ast));
1179 device_buf_size = strlen(ast_channel_name(ast)) + 1;
1180 device_buf = alloca(device_buf_size);
1181 ast_channel_get_device_name(ast, device_buf, device_buf_size);
1182 ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, 1, device_buf);
1185 case AST_CONTROL_SRCUPDATE:
1187 case AST_CONTROL_SRCCHANGE:
1189 case AST_CONTROL_REDIRECTING:
1190 if (ast_channel_state(ast) != AST_STATE_UP) {
1191 response_code = 181;
1196 case AST_CONTROL_T38_PARAMETERS:
1199 if (channel->session->t38state == T38_PEER_REINVITE) {
1200 const struct ast_control_t38_parameters *parameters = data;
1202 if (parameters->request_response == AST_T38_REQUEST_PARMS) {
1203 res = AST_T38_REQUEST_PARMS;
1212 ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", condition);
1217 if (response_code) {
1218 struct indicate_data *ind_data = indicate_data_alloc(channel->session, condition, response_code, data, datalen);
1219 if (!ind_data || ast_sip_push_task(channel->session->serializer, indicate, ind_data)) {
1220 ast_log(LOG_NOTICE, "Cannot send response code %d to endpoint %s. Could not queue task properly\n",
1221 response_code, ast_sorcery_object_get_id(channel->session->endpoint));
1222 ao2_cleanup(ind_data);
1230 struct transfer_data {
1231 struct ast_sip_session *session;
1235 static void transfer_data_destroy(void *obj)
1237 struct transfer_data *trnf_data = obj;
1239 ast_free(trnf_data->target);
1240 ao2_cleanup(trnf_data->session);
1243 static struct transfer_data *transfer_data_alloc(struct ast_sip_session *session, const char *target)
1245 struct transfer_data *trnf_data = ao2_alloc(sizeof(*trnf_data), transfer_data_destroy);
1251 if (!(trnf_data->target = ast_strdup(target))) {
1252 ao2_ref(trnf_data, -1);
1256 ao2_ref(session, +1);
1257 trnf_data->session = session;
1262 static void transfer_redirect(struct ast_sip_session *session, const char *target)
1264 pjsip_tx_data *packet;
1265 enum ast_control_transfer message = AST_TRANSFER_SUCCESS;
1266 pjsip_contact_hdr *contact;
1269 if (pjsip_inv_end_session(session->inv_session, 302, NULL, &packet) != PJ_SUCCESS) {
1270 message = AST_TRANSFER_FAILED;
1271 ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
1276 if (!(contact = pjsip_msg_find_hdr(packet->msg, PJSIP_H_CONTACT, NULL))) {
1277 contact = pjsip_contact_hdr_create(packet->pool);
1280 pj_strdup2_with_null(packet->pool, &tmp, target);
1281 if (!(contact->uri = pjsip_parse_uri(packet->pool, tmp.ptr, tmp.slen, PJSIP_PARSE_URI_AS_NAMEADDR))) {
1282 message = AST_TRANSFER_FAILED;
1283 ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
1284 pjsip_tx_data_dec_ref(packet);
1288 pjsip_msg_add_hdr(packet->msg, (pjsip_hdr *) contact);
1290 ast_sip_session_send_response(session, packet);
1291 ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
1294 static void transfer_refer(struct ast_sip_session *session, const char *target)
1297 enum ast_control_transfer message = AST_TRANSFER_SUCCESS;
1299 pjsip_tx_data *packet;
1301 if (pjsip_xfer_create_uac(session->inv_session->dlg, NULL, &sub) != PJ_SUCCESS) {
1302 message = AST_TRANSFER_FAILED;
1303 ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
1308 if (pjsip_xfer_initiate(sub, pj_cstr(&tmp, target), &packet) != PJ_SUCCESS) {
1309 message = AST_TRANSFER_FAILED;
1310 ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
1311 pjsip_evsub_terminate(sub, PJ_FALSE);
1316 pjsip_xfer_send_request(sub, packet);
1317 ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
1320 static int transfer(void *data)
1322 struct transfer_data *trnf_data = data;
1324 if (ast_channel_state(trnf_data->session->channel) == AST_STATE_RING) {
1325 transfer_redirect(trnf_data->session, trnf_data->target);
1327 transfer_refer(trnf_data->session, trnf_data->target);
1330 ao2_ref(trnf_data, -1);
1334 /*! \brief Function called by core for Asterisk initiated transfer */
1335 static int chan_pjsip_transfer(struct ast_channel *chan, const char *target)
1337 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
1338 struct transfer_data *trnf_data = transfer_data_alloc(channel->session, target);
1344 if (ast_sip_push_task(channel->session->serializer, transfer, trnf_data)) {
1345 ast_log(LOG_WARNING, "Error requesting transfer\n");
1346 ao2_cleanup(trnf_data);
1353 /*! \brief Function called by core to start a DTMF digit */
1354 static int chan_pjsip_digit_begin(struct ast_channel *chan, char digit)
1356 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
1357 struct chan_pjsip_pvt *pvt = channel->pvt;
1358 struct ast_sip_session_media *media = pvt->media[SIP_MEDIA_AUDIO];
1361 switch (channel->session->endpoint->dtmf) {
1362 case AST_SIP_DTMF_RFC_4733:
1363 if (!media || !media->rtp) {
1367 ast_rtp_instance_dtmf_begin(media->rtp, digit);
1368 case AST_SIP_DTMF_NONE:
1370 case AST_SIP_DTMF_INBAND:
1380 struct info_dtmf_data {
1381 struct ast_sip_session *session;
1383 unsigned int duration;
1386 static void info_dtmf_data_destroy(void *obj)
1388 struct info_dtmf_data *dtmf_data = obj;
1389 ao2_ref(dtmf_data->session, -1);
1392 static struct info_dtmf_data *info_dtmf_data_alloc(struct ast_sip_session *session, char digit, unsigned int duration)
1394 struct info_dtmf_data *dtmf_data = ao2_alloc(sizeof(*dtmf_data), info_dtmf_data_destroy);
1398 ao2_ref(session, +1);
1399 dtmf_data->session = session;
1400 dtmf_data->digit = digit;
1401 dtmf_data->duration = duration;
1405 static int transmit_info_dtmf(void *data)
1407 RAII_VAR(struct info_dtmf_data *, dtmf_data, data, ao2_cleanup);
1409 struct ast_sip_session *session = dtmf_data->session;
1410 struct pjsip_tx_data *tdata;
1412 RAII_VAR(struct ast_str *, body_text, NULL, ast_free_ptr);
1414 struct ast_sip_body body = {
1415 .type = "application",
1416 .subtype = "dtmf-relay",
1419 if (!(body_text = ast_str_create(32))) {
1420 ast_log(LOG_ERROR, "Could not allocate buffer for INFO DTMF.\n");
1423 ast_str_set(&body_text, 0, "Signal=%c\r\nDuration=%u\r\n", dtmf_data->digit, dtmf_data->duration);
1425 body.body_text = ast_str_buffer(body_text);
1427 if (ast_sip_create_request("INFO", session->inv_session->dlg, session->endpoint, NULL, NULL, &tdata)) {
1428 ast_log(LOG_ERROR, "Could not create DTMF INFO request\n");
1431 if (ast_sip_add_body(tdata, &body)) {
1432 ast_log(LOG_ERROR, "Could not add body to DTMF INFO request\n");
1433 pjsip_tx_data_dec_ref(tdata);
1436 ast_sip_session_send_request(session, tdata);
1441 /*! \brief Function called by core to stop a DTMF digit */
1442 static int chan_pjsip_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
1444 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
1445 struct chan_pjsip_pvt *pvt = channel->pvt;
1446 struct ast_sip_session_media *media = pvt->media[SIP_MEDIA_AUDIO];
1449 switch (channel->session->endpoint->dtmf) {
1450 case AST_SIP_DTMF_INFO:
1452 struct info_dtmf_data *dtmf_data = info_dtmf_data_alloc(channel->session, digit, duration);
1458 if (ast_sip_push_task(channel->session->serializer, transmit_info_dtmf, dtmf_data)) {
1459 ast_log(LOG_WARNING, "Error sending DTMF via INFO.\n");
1460 ao2_cleanup(dtmf_data);
1465 case AST_SIP_DTMF_RFC_4733:
1466 if (!media || !media->rtp) {
1470 ast_rtp_instance_dtmf_end_with_duration(media->rtp, digit, duration);
1471 case AST_SIP_DTMF_NONE:
1473 case AST_SIP_DTMF_INBAND:
1481 static int call(void *data)
1483 struct ast_sip_session *session = data;
1484 pjsip_tx_data *tdata;
1486 int res = ast_sip_session_create_invite(session, &tdata);
1489 ast_queue_hangup(session->channel);
1491 ast_sip_session_send_request(session, tdata);
1493 ao2_ref(session, -1);
1497 /*! \brief Function called by core to actually start calling a remote party */
1498 static int chan_pjsip_call(struct ast_channel *ast, const char *dest, int timeout)
1500 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
1502 ao2_ref(channel->session, +1);
1503 if (ast_sip_push_task(channel->session->serializer, call, channel->session)) {
1504 ast_log(LOG_WARNING, "Error attempting to place outbound call to call '%s'\n", dest);
1505 ao2_cleanup(channel->session);
1512 /*! \brief Internal function which translates from Asterisk cause codes to SIP response codes */
1513 static int hangup_cause2sip(int cause)
1516 case AST_CAUSE_UNALLOCATED: /* 1 */
1517 case AST_CAUSE_NO_ROUTE_DESTINATION: /* 3 IAX2: Can't find extension in context */
1518 case AST_CAUSE_NO_ROUTE_TRANSIT_NET: /* 2 */
1520 case AST_CAUSE_CONGESTION: /* 34 */
1521 case AST_CAUSE_SWITCH_CONGESTION: /* 42 */
1523 case AST_CAUSE_NO_USER_RESPONSE: /* 18 */
1525 case AST_CAUSE_NO_ANSWER: /* 19 */
1526 case AST_CAUSE_UNREGISTERED: /* 20 */
1528 case AST_CAUSE_CALL_REJECTED: /* 21 */
1530 case AST_CAUSE_NUMBER_CHANGED: /* 22 */
1532 case AST_CAUSE_NORMAL_UNSPECIFIED: /* 31 */
1534 case AST_CAUSE_INVALID_NUMBER_FORMAT:
1536 case AST_CAUSE_USER_BUSY:
1538 case AST_CAUSE_FAILURE:
1540 case AST_CAUSE_FACILITY_REJECTED: /* 29 */
1542 case AST_CAUSE_CHAN_NOT_IMPLEMENTED:
1544 case AST_CAUSE_DESTINATION_OUT_OF_ORDER:
1546 case AST_CAUSE_BEARERCAPABILITY_NOTAVAIL: /* Can't find codec to connect to host */
1548 case AST_CAUSE_INTERWORKING: /* Unspecified Interworking issues */
1550 case AST_CAUSE_NOTDEFINED:
1552 ast_debug(1, "AST hangup cause %d (no match found in PJSIP)\n", cause);
1560 struct hangup_data {
1562 struct ast_channel *chan;
1565 static void hangup_data_destroy(void *obj)
1567 struct hangup_data *h_data = obj;
1569 h_data->chan = ast_channel_unref(h_data->chan);
1572 static struct hangup_data *hangup_data_alloc(int cause, struct ast_channel *chan)
1574 struct hangup_data *h_data = ao2_alloc(sizeof(*h_data), hangup_data_destroy);
1580 h_data->cause = cause;
1581 h_data->chan = ast_channel_ref(chan);
1586 /*! \brief Clear a channel from a session along with its PVT */
1587 static void clear_session_and_channel(struct ast_sip_session *session, struct ast_channel *ast, struct chan_pjsip_pvt *pvt)
1589 session->channel = NULL;
1590 if (pvt->media[SIP_MEDIA_AUDIO] && pvt->media[SIP_MEDIA_AUDIO]->rtp) {
1591 ast_rtp_instance_set_channel_id(pvt->media[SIP_MEDIA_AUDIO]->rtp, "");
1593 if (pvt->media[SIP_MEDIA_VIDEO] && pvt->media[SIP_MEDIA_VIDEO]->rtp) {
1594 ast_rtp_instance_set_channel_id(pvt->media[SIP_MEDIA_VIDEO]->rtp, "");
1596 ast_channel_tech_pvt_set(ast, NULL);
1599 static int hangup(void *data)
1601 struct hangup_data *h_data = data;
1602 struct ast_channel *ast = h_data->chan;
1603 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
1604 struct chan_pjsip_pvt *pvt = channel->pvt;
1605 struct ast_sip_session *session = channel->session;
1606 int cause = h_data->cause;
1608 if (!session->defer_terminate) {
1610 pjsip_tx_data *packet = NULL;
1612 if (session->inv_session->state == PJSIP_INV_STATE_NULL) {
1613 pjsip_inv_terminate(session->inv_session, cause ? cause : 603, PJ_TRUE);
1614 } else if (((status = pjsip_inv_end_session(session->inv_session, cause ? cause : 603, NULL, &packet)) == PJ_SUCCESS)
1616 if (packet->msg->type == PJSIP_RESPONSE_MSG) {
1617 ast_sip_session_send_response(session, packet);
1619 ast_sip_session_send_request(session, packet);
1624 clear_session_and_channel(session, ast, pvt);
1625 ao2_cleanup(channel);
1626 ao2_cleanup(h_data);
1631 /*! \brief Function called by core to hang up a PJSIP session */
1632 static int chan_pjsip_hangup(struct ast_channel *ast)
1634 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
1635 struct chan_pjsip_pvt *pvt = channel->pvt;
1636 int cause = hangup_cause2sip(ast_channel_hangupcause(channel->session->channel));
1637 struct hangup_data *h_data = hangup_data_alloc(cause, ast);
1643 if (ast_sip_push_task(channel->session->serializer, hangup, h_data)) {
1644 ast_log(LOG_WARNING, "Unable to push hangup task to the threadpool. Expect bad things\n");
1651 /* Go ahead and do our cleanup of the session and channel even if we're not going
1652 * to be able to send our SIP request/response
1654 clear_session_and_channel(channel->session, ast, pvt);
1655 ao2_cleanup(channel);
1656 ao2_cleanup(h_data);
1661 struct request_data {
1662 struct ast_sip_session *session;
1663 struct ast_format_cap *caps;
1668 static int request(void *obj)
1670 struct request_data *req_data = obj;
1671 struct ast_sip_session *session = NULL;
1672 char *tmp = ast_strdupa(req_data->dest), *endpoint_name = NULL, *request_user = NULL;
1673 RAII_VAR(struct ast_sip_endpoint *, endpoint, NULL, ao2_cleanup);
1675 AST_DECLARE_APP_ARGS(args,
1676 AST_APP_ARG(endpoint);
1680 if (ast_strlen_zero(tmp)) {
1681 ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty destination\n");
1682 req_data->cause = AST_CAUSE_CHANNEL_UNACCEPTABLE;
1686 AST_NONSTANDARD_APP_ARGS(args, tmp, '/');
1688 /* If a request user has been specified extract it from the endpoint name portion */
1689 if ((endpoint_name = strchr(args.endpoint, '@'))) {
1690 request_user = args.endpoint;
1691 *endpoint_name++ = '\0';
1693 endpoint_name = args.endpoint;
1696 if (ast_strlen_zero(endpoint_name)) {
1697 ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty endpoint name\n");
1698 req_data->cause = AST_CAUSE_CHANNEL_UNACCEPTABLE;
1699 } else if (!(endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", endpoint_name))) {
1700 ast_log(LOG_ERROR, "Unable to create PJSIP channel - endpoint '%s' was not found\n", endpoint_name);
1701 req_data->cause = AST_CAUSE_NO_ROUTE_DESTINATION;
1705 if (!(session = ast_sip_session_create_outgoing(endpoint, NULL, args.aor, request_user, req_data->caps))) {
1706 req_data->cause = AST_CAUSE_NO_ROUTE_DESTINATION;
1710 req_data->session = session;
1715 /*! \brief Function called by core to create a new outgoing PJSIP session */
1716 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)
1718 struct request_data req_data;
1719 RAII_VAR(struct ast_sip_session *, session, NULL, ao2_cleanup);
1721 req_data.caps = cap;
1722 req_data.dest = data;
1724 if (ast_sip_push_task_synchronous(NULL, request, &req_data)) {
1725 *cause = req_data.cause;
1729 session = req_data.session;
1731 if (!(session->channel = chan_pjsip_new(session, AST_STATE_DOWN, NULL, NULL, assignedids, requestor, NULL))) {
1732 /* Session needs to be terminated prematurely */
1736 return session->channel;
1739 struct sendtext_data {
1740 struct ast_sip_session *session;
1744 static void sendtext_data_destroy(void *obj)
1746 struct sendtext_data *data = obj;
1747 ao2_ref(data->session, -1);
1750 static struct sendtext_data* sendtext_data_create(struct ast_sip_session *session, const char *text)
1752 int size = strlen(text) + 1;
1753 struct sendtext_data *data = ao2_alloc(sizeof(*data)+size, sendtext_data_destroy);
1759 data->session = session;
1760 ao2_ref(data->session, +1);
1761 ast_copy_string(data->text, text, size);
1765 static int sendtext(void *obj)
1767 RAII_VAR(struct sendtext_data *, data, obj, ao2_cleanup);
1768 pjsip_tx_data *tdata;
1770 const struct ast_sip_body body = {
1773 .body_text = data->text
1776 /* NOT ast_strlen_zero, because a zero-length message is specifically
1777 * allowed by RFC 3428 (See section 10, Examples) */
1782 ast_debug(3, "Sending in dialog SIP message\n");
1784 ast_sip_create_request("MESSAGE", data->session->inv_session->dlg, data->session->endpoint, NULL, NULL, &tdata);
1785 ast_sip_add_body(tdata, &body);
1786 ast_sip_send_request(tdata, data->session->inv_session->dlg, data->session->endpoint, NULL, NULL);
1791 /*! \brief Function called by core to send text on PJSIP session */
1792 static int chan_pjsip_sendtext(struct ast_channel *ast, const char *text)
1794 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
1795 struct sendtext_data *data = sendtext_data_create(channel->session, text);
1797 if (!data || ast_sip_push_task(channel->session->serializer, sendtext, data)) {
1804 /*! \brief Convert SIP hangup causes to Asterisk hangup causes */
1805 static int hangup_sip2cause(int cause)
1807 /* Possible values taken from causes.h */
1810 case 401: /* Unauthorized */
1811 return AST_CAUSE_CALL_REJECTED;
1812 case 403: /* Not found */
1813 return AST_CAUSE_CALL_REJECTED;
1814 case 404: /* Not found */
1815 return AST_CAUSE_UNALLOCATED;
1816 case 405: /* Method not allowed */
1817 return AST_CAUSE_INTERWORKING;
1818 case 407: /* Proxy authentication required */
1819 return AST_CAUSE_CALL_REJECTED;
1820 case 408: /* No reaction */
1821 return AST_CAUSE_NO_USER_RESPONSE;
1822 case 409: /* Conflict */
1823 return AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
1824 case 410: /* Gone */
1825 return AST_CAUSE_NUMBER_CHANGED;
1826 case 411: /* Length required */
1827 return AST_CAUSE_INTERWORKING;
1828 case 413: /* Request entity too large */
1829 return AST_CAUSE_INTERWORKING;
1830 case 414: /* Request URI too large */
1831 return AST_CAUSE_INTERWORKING;
1832 case 415: /* Unsupported media type */
1833 return AST_CAUSE_INTERWORKING;
1834 case 420: /* Bad extension */
1835 return AST_CAUSE_NO_ROUTE_DESTINATION;
1836 case 480: /* No answer */
1837 return AST_CAUSE_NO_ANSWER;
1838 case 481: /* No answer */
1839 return AST_CAUSE_INTERWORKING;
1840 case 482: /* Loop detected */
1841 return AST_CAUSE_INTERWORKING;
1842 case 483: /* Too many hops */
1843 return AST_CAUSE_NO_ANSWER;
1844 case 484: /* Address incomplete */
1845 return AST_CAUSE_INVALID_NUMBER_FORMAT;
1846 case 485: /* Ambiguous */
1847 return AST_CAUSE_UNALLOCATED;
1848 case 486: /* Busy everywhere */
1849 return AST_CAUSE_BUSY;
1850 case 487: /* Request terminated */
1851 return AST_CAUSE_INTERWORKING;
1852 case 488: /* No codecs approved */
1853 return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
1854 case 491: /* Request pending */
1855 return AST_CAUSE_INTERWORKING;
1856 case 493: /* Undecipherable */
1857 return AST_CAUSE_INTERWORKING;
1858 case 500: /* Server internal failure */
1859 return AST_CAUSE_FAILURE;
1860 case 501: /* Call rejected */
1861 return AST_CAUSE_FACILITY_REJECTED;
1863 return AST_CAUSE_DESTINATION_OUT_OF_ORDER;
1864 case 503: /* Service unavailable */
1865 return AST_CAUSE_CONGESTION;
1866 case 504: /* Gateway timeout */
1867 return AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
1868 case 505: /* SIP version not supported */
1869 return AST_CAUSE_INTERWORKING;
1870 case 600: /* Busy everywhere */
1871 return AST_CAUSE_USER_BUSY;
1872 case 603: /* Decline */
1873 return AST_CAUSE_CALL_REJECTED;
1874 case 604: /* Does not exist anywhere */
1875 return AST_CAUSE_UNALLOCATED;
1876 case 606: /* Not acceptable */
1877 return AST_CAUSE_BEARERCAPABILITY_NOTAVAIL;
1879 if (cause < 500 && cause >= 400) {
1880 /* 4xx class error that is unknown - someting wrong with our request */
1881 return AST_CAUSE_INTERWORKING;
1882 } else if (cause < 600 && cause >= 500) {
1883 /* 5xx class error - problem in the remote end */
1884 return AST_CAUSE_CONGESTION;
1885 } else if (cause < 700 && cause >= 600) {
1886 /* 6xx - global errors in the 4xx class */
1887 return AST_CAUSE_INTERWORKING;
1889 return AST_CAUSE_NORMAL;
1895 static void chan_pjsip_session_begin(struct ast_sip_session *session)
1897 RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
1899 if (session->endpoint->media.direct_media.glare_mitigation ==
1900 AST_SIP_DIRECT_MEDIA_GLARE_MITIGATION_NONE) {
1904 datastore = ast_sip_session_alloc_datastore(&direct_media_mitigation_info,
1905 "direct_media_glare_mitigation");
1911 ast_sip_session_add_datastore(session, datastore);
1914 /*! \brief Function called when the session ends */
1915 static void chan_pjsip_session_end(struct ast_sip_session *session)
1917 if (!session->channel) {
1921 chan_pjsip_remove_hold(ast_channel_uniqueid(session->channel));
1923 if (!ast_channel_hangupcause(session->channel) && session->inv_session) {
1924 int cause = hangup_sip2cause(session->inv_session->cause);
1926 ast_queue_hangup_with_cause(session->channel, cause);
1928 ast_queue_hangup(session->channel);
1932 /*! \brief Function called when a request is received on the session */
1933 static int chan_pjsip_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
1935 RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
1936 struct transport_info_data *transport_data;
1937 pjsip_tx_data *packet = NULL;
1939 if (session->channel) {
1943 datastore = ast_sip_session_alloc_datastore(&transport_info, "transport_info");
1948 transport_data = ast_calloc(1, sizeof(*transport_data));
1949 if (!transport_data) {
1952 pj_sockaddr_cp(&transport_data->local_addr, &rdata->tp_info.transport->local_addr);
1953 pj_sockaddr_cp(&transport_data->remote_addr, &rdata->pkt_info.src_addr);
1954 datastore->data = transport_data;
1955 ast_sip_session_add_datastore(session, datastore);
1957 if (!(session->channel = chan_pjsip_new(session, AST_STATE_RING, session->exten, NULL, NULL, NULL, NULL))) {
1958 if (pjsip_inv_end_session(session->inv_session, 503, NULL, &packet) == PJ_SUCCESS) {
1959 ast_sip_session_send_response(session, packet);
1962 ast_log(LOG_ERROR, "Failed to allocate new PJSIP channel on incoming SIP INVITE\n");
1965 /* channel gets created on incoming request, but we wait to call start
1966 so other supplements have a chance to run */
1970 static int pbx_start_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
1974 res = ast_pbx_start(session->channel);
1977 case AST_PBX_FAILED:
1978 ast_log(LOG_WARNING, "Failed to start PBX ;(\n");
1979 ast_channel_hangupcause_set(session->channel, AST_CAUSE_SWITCH_CONGESTION);
1980 ast_hangup(session->channel);
1982 case AST_PBX_CALL_LIMIT:
1983 ast_log(LOG_WARNING, "Failed to start PBX (call limit reached) \n");
1984 ast_channel_hangupcause_set(session->channel, AST_CAUSE_SWITCH_CONGESTION);
1985 ast_hangup(session->channel);
1987 case AST_PBX_SUCCESS:
1992 ast_debug(3, "Started PBX on new PJSIP channel %s\n", ast_channel_name(session->channel));
1994 return (res == AST_PBX_SUCCESS) ? 0 : -1;
1997 static struct ast_sip_session_supplement pbx_start_supplement = {
1999 .priority = AST_SIP_SUPPLEMENT_PRIORITY_LAST,
2000 .incoming_request = pbx_start_incoming_request,
2003 /*! \brief Function called when a response is received on the session */
2004 static void chan_pjsip_incoming_response(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
2006 struct pjsip_status_line status = rdata->msg_info.msg->line.status;
2008 if (!session->channel) {
2012 switch (status.code) {
2014 ast_queue_control(session->channel, AST_CONTROL_RINGING);
2015 ast_channel_lock(session->channel);
2016 if (ast_channel_state(session->channel) != AST_STATE_UP) {
2017 ast_setstate(session->channel, AST_STATE_RINGING);
2019 ast_channel_unlock(session->channel);
2022 ast_queue_control(session->channel, AST_CONTROL_PROGRESS);
2025 ast_queue_control(session->channel, AST_CONTROL_ANSWER);
2032 static int chan_pjsip_incoming_ack(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
2034 if (rdata->msg_info.msg->line.req.method.id == PJSIP_ACK_METHOD) {
2035 if (session->endpoint->media.direct_media.enabled && session->channel) {
2036 ast_queue_control(session->channel, AST_CONTROL_SRCCHANGE);
2042 static int update_devstate(void *obj, void *arg, int flags)
2044 ast_devstate_changed(AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE,
2045 "PJSIP/%s", ast_sorcery_object_get_id(obj));
2049 static struct ast_custom_function chan_pjsip_dial_contacts_function = {
2050 .name = "PJSIP_DIAL_CONTACTS",
2051 .read = pjsip_acf_dial_contacts_read,
2054 static struct ast_custom_function media_offer_function = {
2055 .name = "PJSIP_MEDIA_OFFER",
2056 .read = pjsip_acf_media_offer_read,
2057 .write = pjsip_acf_media_offer_write
2061 * \brief Load the module
2063 * Module loading including tests for configuration or dependencies.
2064 * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
2065 * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
2066 * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
2067 * configuration file or other non-critical problem return
2068 * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
2070 static int load_module(void)
2072 struct ao2_container *endpoints;
2074 if (!(chan_pjsip_tech.capabilities = ast_format_cap_alloc(0))) {
2075 return AST_MODULE_LOAD_DECLINE;
2078 ast_format_cap_add_all_by_type(chan_pjsip_tech.capabilities, AST_FORMAT_TYPE_AUDIO);
2080 ast_rtp_glue_register(&chan_pjsip_rtp_glue);
2082 if (ast_channel_register(&chan_pjsip_tech)) {
2083 ast_log(LOG_ERROR, "Unable to register channel class %s\n", channel_type);
2087 if (ast_custom_function_register(&chan_pjsip_dial_contacts_function)) {
2088 ast_log(LOG_ERROR, "Unable to register PJSIP_DIAL_CONTACTS dialplan function\n");
2092 if (ast_custom_function_register(&media_offer_function)) {
2093 ast_log(LOG_WARNING, "Unable to register PJSIP_MEDIA_OFFER dialplan function\n");
2097 if (ast_sip_session_register_supplement(&chan_pjsip_supplement)) {
2098 ast_log(LOG_ERROR, "Unable to register PJSIP supplement\n");
2102 if (!(pjsip_uids_onhold = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_RWLOCK,
2103 AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT, 37, uid_hold_hash_fn,
2104 uid_hold_sort_fn, NULL))) {
2105 ast_log(LOG_ERROR, "Unable to create held channels container\n");
2109 if (ast_sip_session_register_supplement(&pbx_start_supplement)) {
2110 ast_log(LOG_ERROR, "Unable to register PJSIP pbx start supplement\n");
2111 ast_sip_session_unregister_supplement(&chan_pjsip_supplement);
2115 if (ast_sip_session_register_supplement(&chan_pjsip_ack_supplement)) {
2116 ast_log(LOG_ERROR, "Unable to register PJSIP ACK supplement\n");
2117 ast_sip_session_unregister_supplement(&pbx_start_supplement);
2118 ast_sip_session_unregister_supplement(&chan_pjsip_supplement);
2122 /* since endpoints are loaded before the channel driver their device
2123 states get set to 'invalid', so they need to be updated */
2124 if ((endpoints = ast_sip_get_endpoints())) {
2125 ao2_callback(endpoints, OBJ_NODATA, update_devstate, NULL);
2126 ao2_ref(endpoints, -1);
2132 ao2_cleanup(pjsip_uids_onhold);
2133 pjsip_uids_onhold = NULL;
2134 ast_custom_function_unregister(&media_offer_function);
2135 ast_custom_function_unregister(&chan_pjsip_dial_contacts_function);
2136 ast_channel_unregister(&chan_pjsip_tech);
2137 ast_rtp_glue_unregister(&chan_pjsip_rtp_glue);
2139 return AST_MODULE_LOAD_FAILURE;
2142 /*! \brief Reload module */
2143 static int reload(void)
2148 /*! \brief Unload the PJSIP channel from Asterisk */
2149 static int unload_module(void)
2151 ao2_cleanup(pjsip_uids_onhold);
2152 pjsip_uids_onhold = NULL;
2154 ast_sip_session_unregister_supplement(&chan_pjsip_supplement);
2155 ast_sip_session_unregister_supplement(&pbx_start_supplement);
2156 ast_sip_session_unregister_supplement(&chan_pjsip_ack_supplement);
2158 ast_custom_function_unregister(&media_offer_function);
2159 ast_custom_function_unregister(&chan_pjsip_dial_contacts_function);
2161 ast_channel_unregister(&chan_pjsip_tech);
2162 ast_rtp_glue_unregister(&chan_pjsip_rtp_glue);
2167 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "PJSIP Channel Driver",
2168 .load = load_module,
2169 .unload = unload_module,
2171 .load_pri = AST_MODPRI_CHANNEL_DRIVER,