Asterisk - The Open Source Telephony Project  GIT-master-0190e70
chan_pjsip.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Joshua Colp <jcolp@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*! \file
20  *
21  * \author Joshua Colp <jcolp@digium.com>
22  *
23  * \brief PSJIP SIP Channel Driver
24  *
25  * \ingroup channel_drivers
26  */
27 
28 /*** MODULEINFO
29  <depend>pjproject</depend>
30  <depend>res_pjsip</depend>
31  <depend>res_pjsip_pubsub</depend>
32  <depend>res_pjsip_session</depend>
33  <support_level>core</support_level>
34  ***/
35 
36 #include "asterisk.h"
37 
38 #include <pjsip.h>
39 #include <pjsip_ua.h>
40 #include <pjlib.h>
41 
42 #include "asterisk/lock.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/module.h"
45 #include "asterisk/pbx.h"
46 #include "asterisk/rtp_engine.h"
47 #include "asterisk/acl.h"
48 #include "asterisk/callerid.h"
49 #include "asterisk/file.h"
50 #include "asterisk/cli.h"
51 #include "asterisk/app.h"
52 #include "asterisk/musiconhold.h"
53 #include "asterisk/causes.h"
54 #include "asterisk/taskprocessor.h"
55 #include "asterisk/dsp.h"
58 #include "asterisk/indications.h"
59 #include "asterisk/format_cache.h"
60 #include "asterisk/translate.h"
61 #include "asterisk/threadstorage.h"
63 #include "asterisk/pickup.h"
64 #include "asterisk/test.h"
65 #include "asterisk/message.h"
66 
67 #include "asterisk/res_pjsip.h"
69 #include "asterisk/stream.h"
70 
74 
76 #define UNIQUEID_BUFSIZE 256
77 
78 static const char channel_type[] = "PJSIP";
79 
80 static unsigned int chan_idx;
81 
82 static void chan_pjsip_pvt_dtor(void *obj)
83 {
84 }
85 
86 /* \brief Asterisk core interaction functions */
87 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  struct ast_stream_topology *topology, const struct ast_assigned_ids *assignedids,
90  const struct ast_channel *requestor, const char *data, int *cause);
91 static int chan_pjsip_sendtext_data(struct ast_channel *ast, struct ast_msg_data *msg);
92 static int chan_pjsip_sendtext(struct ast_channel *ast, const char *text);
93 static int chan_pjsip_digit_begin(struct ast_channel *ast, char digit);
94 static int chan_pjsip_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
95 static int chan_pjsip_call(struct ast_channel *ast, const char *dest, int timeout);
96 static int chan_pjsip_hangup(struct ast_channel *ast);
97 static int chan_pjsip_answer(struct ast_channel *ast);
98 static struct ast_frame *chan_pjsip_read_stream(struct ast_channel *ast);
99 static int chan_pjsip_write(struct ast_channel *ast, struct ast_frame *f);
100 static int chan_pjsip_write_stream(struct ast_channel *ast, int stream_num, struct ast_frame *f);
101 static int chan_pjsip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
102 static int chan_pjsip_transfer(struct ast_channel *ast, const char *target);
103 static int chan_pjsip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
104 static int chan_pjsip_devicestate(const char *data);
105 static int chan_pjsip_queryoption(struct ast_channel *ast, int option, void *data, int *datalen);
106 static const char *chan_pjsip_get_uniqueid(struct ast_channel *ast);
107 
108 /*! \brief PBX interface structure for channel registration */
110  .type = channel_type,
111  .description = "PJSIP Channel Driver",
112  .requester = chan_pjsip_request,
113  .requester_with_stream_topology = chan_pjsip_request_with_stream_topology,
114  .send_text = chan_pjsip_sendtext,
115  .send_text_data = chan_pjsip_sendtext_data,
116  .send_digit_begin = chan_pjsip_digit_begin,
117  .send_digit_end = chan_pjsip_digit_end,
118  .call = chan_pjsip_call,
119  .hangup = chan_pjsip_hangup,
120  .answer = chan_pjsip_answer,
121  .read_stream = chan_pjsip_read_stream,
122  .write = chan_pjsip_write,
123  .write_stream = chan_pjsip_write_stream,
124  .exception = chan_pjsip_read_stream,
125  .indicate = chan_pjsip_indicate,
126  .transfer = chan_pjsip_transfer,
127  .fixup = chan_pjsip_fixup,
128  .devicestate = chan_pjsip_devicestate,
129  .queryoption = chan_pjsip_queryoption,
130  .func_channel_read = pjsip_acf_channel_read,
131  .get_pvt_uniqueid = chan_pjsip_get_uniqueid,
133 };
134 
135 /*! \brief SIP session interaction functions */
137 static void chan_pjsip_session_end(struct ast_sip_session *session);
138 static int chan_pjsip_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
139 static void chan_pjsip_incoming_response(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
140 
141 /*! \brief SIP session supplement structure */
143  .method = "INVITE",
145  .session_begin = chan_pjsip_session_begin,
146  .session_end = chan_pjsip_session_end,
147  .incoming_request = chan_pjsip_incoming_request,
148  /* It is important that this supplement runs after media has been negotiated */
149  .response_priority = AST_SIP_SESSION_AFTER_MEDIA,
150 };
151 
152 /*! \brief SIP session supplement structure just for responses */
154  .method = "INVITE",
156  .incoming_response = chan_pjsip_incoming_response,
158 };
159 
160 static int chan_pjsip_incoming_ack(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
161 
163  .method = "ACK",
165  .incoming_request = chan_pjsip_incoming_ack,
166 };
167 
168 /*! \brief Function called by RTP engine to get local audio RTP peer */
169 static enum ast_rtp_glue_result chan_pjsip_get_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
170 {
172  struct ast_sip_endpoint *endpoint;
173  struct ast_datastore *datastore;
174  struct ast_sip_session_media *media;
175 
176  if (!channel || !channel->session) {
178  }
179 
180  /* XXX Getting the first RTP instance for direct media related stuff seems just
181  * absolutely wrong. But the native RTP bridge knows no other method than single-stream
182  * for direct media. So this is the best we can do.
183  */
185  if (!media || !media->rtp) {
187  }
188 
189  datastore = ast_sip_session_get_datastore(channel->session, "t38");
190  if (datastore) {
191  ao2_ref(datastore, -1);
193  }
194 
195  endpoint = channel->session->endpoint;
196 
197  *instance = media->rtp;
198  ao2_ref(*instance, +1);
199 
200  ast_assert(endpoint != NULL);
201  if (endpoint->media.rtp.encryption != AST_SIP_MEDIA_ENCRYPT_NONE) {
203  }
204 
205  if (endpoint->media.direct_media.enabled) {
207  }
208 
210 }
211 
212 /*! \brief Function called by RTP engine to get local video RTP peer */
213 static enum ast_rtp_glue_result chan_pjsip_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
214 {
216  struct ast_sip_endpoint *endpoint;
217  struct ast_sip_session_media *media;
218 
219  if (!channel || !channel->session) {
221  }
222 
224  if (!media || !media->rtp) {
226  }
227 
228  endpoint = channel->session->endpoint;
229 
230  *instance = media->rtp;
231  ao2_ref(*instance, +1);
232 
233  ast_assert(endpoint != NULL);
234  if (endpoint->media.rtp.encryption != AST_SIP_MEDIA_ENCRYPT_NONE) {
236  }
237 
239 }
240 
241 /*! \brief Function called by RTP engine to get peer capabilities */
242 static void chan_pjsip_get_codec(struct ast_channel *chan, struct ast_format_cap *result)
243 {
244  SCOPE_ENTER(1, "%s Native formats %s\n", ast_channel_name(chan),
247  SCOPE_EXIT_RTN();
248 }
249 
250 /*! \brief Destructor function for \ref transport_info_data */
251 static void transport_info_destroy(void *obj)
252 {
253  struct transport_info_data *data = obj;
254  ast_free(data);
255 }
256 
257 /*! \brief Datastore used to store local/remote addresses for the
258  * INVITE request that created the PJSIP channel */
260  .type = "chan_pjsip_transport_info",
261  .destroy = transport_info_destroy,
262 };
263 
265 
266 static int direct_media_mitigate_glare(struct ast_sip_session *session)
267 {
268  RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
269 
272  return 0;
273  }
274 
275  datastore = ast_sip_session_get_datastore(session, "direct_media_glare_mitigation");
276  if (!datastore) {
277  return 0;
278  }
279 
280  /* Removing the datastore ensures we won't try to mitigate glare on subsequent reinvites */
281  ast_sip_session_remove_datastore(session, "direct_media_glare_mitigation");
282 
283  if ((session->endpoint->media.direct_media.glare_mitigation ==
285  session->inv_session->role == PJSIP_ROLE_UAC) ||
288  session->inv_session->role == PJSIP_ROLE_UAS)) {
289  return 1;
290  }
291 
292  return 0;
293 }
294 
295 /*! \brief Helper function to find the position for RTCP */
296 static int rtp_find_rtcp_fd_position(struct ast_sip_session *session, struct ast_rtp_instance *rtp)
297 {
298  int index;
299 
300  for (index = 0; index < AST_VECTOR_SIZE(&session->active_media_state->read_callbacks); ++index) {
301  struct ast_sip_session_media_read_callback_state *callback_state =
302  AST_VECTOR_GET_ADDR(&session->active_media_state->read_callbacks, index);
303 
304  if (callback_state->fd != ast_rtp_instance_fd(rtp, 1)) {
305  continue;
306  }
307 
308  return index;
309  }
310 
311  return -1;
312 }
313 
314 /*!
315  * \pre chan is locked
316  */
317 static int check_for_rtp_changes(struct ast_channel *chan, struct ast_rtp_instance *rtp,
318  struct ast_sip_session_media *media, struct ast_sip_session *session)
319 {
320  int changed = 0, position = -1;
321 
322  if (media->rtp) {
323  position = rtp_find_rtcp_fd_position(session, media->rtp);
324  }
325 
326  if (rtp) {
328  if (media->rtp) {
329  if (position != -1) {
330  ast_channel_set_fd(chan, position + AST_EXTENDED_FDS, -1);
331  }
333  }
334  } else if (!ast_sockaddr_isnull(&media->direct_media_addr)){
336  changed = 1;
337  if (media->rtp) {
338  /* Direct media has ended - reset time of last received RTP packet
339  * to avoid premature RTP timeout. Synchronisation between the
340  * modification of direct_mdedia_addr+last_rx here and reading the
341  * values in res_pjsip_sdp_rtp.c:rtp_check_timeout() is provided
342  * by the channel's lock (which is held while this function is
343  * executed).
344  */
345  ast_rtp_instance_set_last_rx(media->rtp, time(NULL));
347  if (position != -1) {
348  ast_channel_set_fd(chan, position + AST_EXTENDED_FDS, ast_rtp_instance_fd(media->rtp, 1));
349  }
350  }
351  }
352 
353  return changed;
354 }
355 
357  struct ast_channel *chan;
362 };
363 
364 static void rtp_direct_media_data_destroy(void *data)
365 {
366  struct rtp_direct_media_data *cdata = data;
367 
368  ao2_cleanup(cdata->session);
369  ao2_cleanup(cdata->cap);
370  ao2_cleanup(cdata->vrtp);
371  ao2_cleanup(cdata->rtp);
372  ao2_cleanup(cdata->chan);
373 }
374 
376  struct ast_channel *chan, struct ast_rtp_instance *rtp, struct ast_rtp_instance *vrtp,
377  const struct ast_format_cap *cap, struct ast_sip_session *session)
378 {
380 
381  if (!cdata) {
382  return NULL;
383  }
384 
385  cdata->chan = ao2_bump(chan);
386  cdata->rtp = ao2_bump(rtp);
387  cdata->vrtp = ao2_bump(vrtp);
388  cdata->cap = ao2_bump((struct ast_format_cap *)cap);
389  cdata->session = ao2_bump(session);
390 
391  return cdata;
392 }
393 
394 static int send_direct_media_request(void *data)
395 {
396  struct rtp_direct_media_data *cdata = data;
398  struct ast_sip_session *session;
399  int changed = 0;
400  int res = 0;
401 
402  /* XXX In an ideal world each media stream would be direct, but for now preserve behavior
403  * and connect only the default media sessions for audio and video.
404  */
405 
406  /* The channel needs to be locked when checking for RTP changes.
407  * Otherwise, we could end up destroying an underlying RTCP structure
408  * at the same time that the channel thread is attempting to read RTCP
409  */
410  ast_channel_lock(cdata->chan);
411  session = channel->session;
413  changed |= check_for_rtp_changes(
414  cdata->chan, cdata->rtp, session->active_media_state->default_session[AST_MEDIA_TYPE_AUDIO], session);
415  }
417  changed |= check_for_rtp_changes(
418  cdata->chan, cdata->vrtp, session->active_media_state->default_session[AST_MEDIA_TYPE_VIDEO], session);
419  }
420  ast_channel_unlock(cdata->chan);
421 
422  if (direct_media_mitigate_glare(cdata->session)) {
423  ast_debug(4, "Disregarding setting RTP on %s: mitigating re-INVITE glare\n", ast_channel_name(cdata->chan));
424  ao2_ref(cdata, -1);
425  return 0;
426  }
427 
428  if (cdata->cap && ast_format_cap_count(cdata->cap) &&
432  changed = 1;
433  }
434 
435  if (changed) {
436  ast_debug(4, "RTP changed on %s; initiating direct media update\n", ast_channel_name(cdata->chan));
437  res = ast_sip_session_refresh(cdata->session, NULL, NULL, NULL,
439  }
440 
441  ao2_ref(cdata, -1);
442  return res;
443 }
444 
445 /*! \brief Function called by RTP engine to change where the remote party should send media */
447  struct ast_rtp_instance *rtp,
448  struct ast_rtp_instance *vrtp,
449  struct ast_rtp_instance *tpeer,
450  const struct ast_format_cap *cap,
451  int nat_active)
452 {
454  struct ast_sip_session *session = channel->session;
456  SCOPE_ENTER(1, "%s %s\n", ast_channel_name(chan),
458 
459  /* Don't try to do any direct media shenanigans on early bridges */
460  if ((rtp || vrtp || tpeer) && !ast_channel_is_bridged(chan)) {
461  ast_debug(4, "Disregarding setting RTP on %s: channel is not bridged\n", ast_channel_name(chan));
462  SCOPE_EXIT_RTN_VALUE(0, "Channel not bridged\n");
463  }
464 
465  if (nat_active && session->endpoint->media.direct_media.disable_on_nat) {
466  ast_debug(4, "Disregarding setting RTP on %s: NAT is active\n", ast_channel_name(chan));
467  SCOPE_EXIT_RTN_VALUE(0, "NAT is active\n");
468  }
469 
470  cdata = rtp_direct_media_data_create(chan, rtp, vrtp, cap, session);
471  if (!cdata) {
473  }
474 
476  ast_log(LOG_ERROR, "Unable to send direct media request for channel %s\n", ast_channel_name(chan));
477  ao2_ref(cdata, -1);
478  }
479 
481 }
482 
483 /*! \brief Local glue for interacting with the RTP engine core */
485  .type = "PJSIP",
486  .get_rtp_info = chan_pjsip_get_rtp_peer,
487  .get_vrtp_info = chan_pjsip_get_vrtp_peer,
488  .get_codec = chan_pjsip_get_codec,
489  .update_peer = chan_pjsip_set_rtp_peer,
490 };
491 
492 static void set_channel_on_rtp_instance(const struct ast_sip_session *session,
493  const char *channel_id)
494 {
495  int i;
496 
497  for (i = 0; i < AST_VECTOR_SIZE(&session->active_media_state->sessions); ++i) {
498  struct ast_sip_session_media *session_media;
499 
500  session_media = AST_VECTOR_GET(&session->active_media_state->sessions, i);
501  if (!session_media || !session_media->rtp) {
502  continue;
503  }
504 
505  ast_rtp_instance_set_channel_id(session_media->rtp, channel_id);
506  }
507 }
508 
509 /*!
510  * \brief Determine if a topology is compatible with format capabilities
511  *
512  * This will return true if ANY formats in the topology are compatible with the format
513  * capabilities.
514  *
515  * XXX When supporting true multistream, we will need to be sure to mark which streams from
516  * top1 are compatible with which streams from top2. Then the ones that are not compatible
517  * will need to be marked as "removed" so that they are negotiated as expected.
518  *
519  * \param top Topology
520  * \param cap Format capabilities
521  * \retval 1 The topology has at least one compatible format
522  * \retval 0 The topology has no compatible formats or an error occurred.
523  */
524 static int compatible_formats_exist(struct ast_stream_topology *top, struct ast_format_cap *cap)
525 {
526  struct ast_format_cap *cap_from_top;
527  int res;
528  SCOPE_ENTER(1, "Topology: %s Formats: %s\n",
531 
532  cap_from_top = ast_stream_topology_get_formats(top);
533 
534  if (!cap_from_top) {
535  SCOPE_EXIT_RTN_VALUE(0, "Topology had no formats\n");
536  }
537 
538  res = ast_format_cap_iscompatible(cap_from_top, cap);
539  ao2_ref(cap_from_top, -1);
540 
541  SCOPE_EXIT_RTN_VALUE(res, "Compatible? %s\n", res ? "yes" : "no");
542 }
543 
544 /*! \brief Function called to create a new PJSIP Asterisk channel */
545 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)
546 {
547  struct ast_channel *chan;
548  struct ast_format_cap *caps;
549  RAII_VAR(struct chan_pjsip_pvt *, pvt, NULL, ao2_cleanup);
551  struct ast_variable *var;
552  struct ast_stream_topology *topology;
553  SCOPE_ENTER(1, "%s\n", ast_sip_session_get_name(session));
554 
555  if (!(pvt = ao2_alloc_options(sizeof(*pvt), chan_pjsip_pvt_dtor, AO2_ALLOC_OPT_LOCK_NOLOCK))) {
556  SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't create pvt\n");
557  }
558 
559  chan = ast_channel_alloc_with_endpoint(1, state,
560  S_COR(session->id.number.valid, session->id.number.str, ""),
561  S_COR(session->id.name.valid, session->id.name.str, ""),
562  session->endpoint->accountcode,
563  exten, session->endpoint->context,
564  assignedids, requestor, 0,
565  session->endpoint->persistent, "PJSIP/%s-%08x",
567  (unsigned) ast_atomic_fetchadd_int((int *) &chan_idx, +1));
568  if (!chan) {
569  SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't create channel\n");
570  }
571 
572  ast_channel_tech_set(chan, &chan_pjsip_tech);
573 
574  if (!(channel = ast_sip_channel_pvt_alloc(pvt, session))) {
575  ast_channel_unlock(chan);
576  ast_hangup(chan);
577  SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't create pvt channel\n");
578  }
579 
580  ast_channel_tech_pvt_set(chan, channel);
581 
585  if (!caps) {
586  ast_channel_unlock(chan);
587  ast_hangup(chan);
588  SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't create caps\n");
589  }
591  topology = ast_stream_topology_clone(session->endpoint->media.topology);
592  } else {
595  }
596 
597  if (!topology || !caps) {
598  ao2_cleanup(caps);
599  ast_stream_topology_free(topology);
600  ast_channel_unlock(chan);
601  ast_hangup(chan);
602  SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't get caps or clone topology\n");
603  }
604 
606 
607  ast_channel_nativeformats_set(chan, caps);
608  ast_channel_set_stream_topology(chan, topology);
609 
610  if (!ast_format_cap_empty(caps)) {
611  struct ast_format *fmt;
612 
614  if (!fmt) {
615  /* Since our capabilities aren't empty, this will succeed */
616  fmt = ast_format_cap_get_format(caps, 0);
617  }
618  ast_channel_set_writeformat(chan, fmt);
620  ast_channel_set_readformat(chan, fmt);
622  ao2_ref(fmt, -1);
623  }
624 
625  ao2_ref(caps, -1);
626 
627  if (state == AST_STATE_RING) {
628  ast_channel_rings_set(chan, 1);
629  }
630 
632 
633  ast_party_id_copy(&ast_channel_caller(chan)->id, &session->id);
634  ast_party_id_copy(&ast_channel_caller(chan)->ani, &session->id);
635 
636  if (!ast_strlen_zero(exten)) {
637  /* Set provided DNID on the new channel. */
638  ast_channel_dialed(chan)->number.str = ast_strdup(exten);
639  }
640 
641  ast_channel_priority_set(chan, 1);
642 
645 
648 
649  if (!ast_strlen_zero(session->endpoint->language)) {
650  ast_channel_language_set(chan, session->endpoint->language);
651  }
652 
653  if (!ast_strlen_zero(session->endpoint->zone)) {
654  struct ast_tone_zone *zone = ast_get_indication_zone(session->endpoint->zone);
655  if (!zone) {
656  ast_log(LOG_ERROR, "Unknown country code '%s' for tonezone. Check indications.conf for available country codes.\n", session->endpoint->zone);
657  }
658  ast_channel_zone_set(chan, zone);
659  }
660 
661  for (var = session->endpoint->channel_vars; var; var = var->next) {
662  char buf[512];
664  var->value, buf, sizeof(buf)));
665  }
666 
668  ast_channel_unlock(chan);
669 
671 
672  SCOPE_EXIT_RTN_VALUE(chan);
673 }
674 
675 struct answer_data {
677  unsigned long indent;
678 };
679 
680 static int answer(void *data)
681 {
682  struct answer_data *ans_data = data;
683  pj_status_t status = PJ_SUCCESS;
684  pjsip_tx_data *packet = NULL;
685  struct ast_sip_session *session = ans_data->session;
686  SCOPE_ENTER_TASK(1, ans_data->indent, "%s\n", ast_sip_session_get_name(session));
687 
688  if (session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
689  ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
690  session->inv_session->cause,
691  pjsip_get_status_text(session->inv_session->cause)->ptr);
692 #ifdef HAVE_PJSIP_INV_SESSION_REF
693  pjsip_inv_dec_ref(session->inv_session);
694 #endif
695  SCOPE_EXIT_RTN_VALUE(0, "Disconnected\n");
696  }
697 
698  pjsip_dlg_inc_lock(session->inv_session->dlg);
699  if (session->inv_session->invite_tsx) {
700  status = pjsip_inv_answer(session->inv_session, 200, NULL, NULL, &packet);
701  } else {
702  ast_log(LOG_ERROR,"Cannot answer '%s' because there is no associated SIP transaction\n",
703  ast_channel_name(session->channel));
704  }
705  pjsip_dlg_dec_lock(session->inv_session->dlg);
706 
707  if (status == PJ_SUCCESS && packet) {
708  ast_sip_session_send_response(session, packet);
709  }
710 
711 #ifdef HAVE_PJSIP_INV_SESSION_REF
712  pjsip_inv_dec_ref(session->inv_session);
713 #endif
714 
715  if (status != PJ_SUCCESS) {
716  char err[PJ_ERR_MSG_SIZE];
717 
718  pj_strerror(status, err, sizeof(err));
719  ast_log(LOG_WARNING,"Cannot answer '%s': %s\n",
720  ast_channel_name(session->channel), err);
721  /*
722  * Return this value so we can distinguish between this
723  * failure and the threadpool synchronous push failing.
724  */
725  SCOPE_EXIT_RTN_VALUE(-2, "pjproject failure\n");
726  }
728 }
729 
730 /*! \brief Function called by core when we should answer a PJSIP session */
731 static int chan_pjsip_answer(struct ast_channel *ast)
732 {
734  struct ast_sip_session *session;
735  struct answer_data ans_data = { 0, };
736  int res;
737  SCOPE_ENTER(1, "%s\n", ast_channel_name(ast));
738 
739  if (ast_channel_state(ast) == AST_STATE_UP) {
740  SCOPE_EXIT_RTN_VALUE(0, "Already up\n");
741  return 0;
742  }
743 
745  session = ao2_bump(channel->session);
746 
747 #ifdef HAVE_PJSIP_INV_SESSION_REF
748  if (pjsip_inv_add_ref(session->inv_session) != PJ_SUCCESS) {
749  ast_log(LOG_ERROR, "Couldn't increase the session reference counter\n");
750  ao2_ref(session, -1);
751  SCOPE_EXIT_RTN_VALUE(-1, "Couldn't increase the session reference counter\n");
752  }
753 #endif
754 
755  /* the answer task needs to be pushed synchronously otherwise a race condition
756  can occur between this thread and bridging (specifically when native bridging
757  attempts to do direct media) */
758  ast_channel_unlock(ast);
759  ans_data.session = session;
760  ans_data.indent = ast_trace_get_indent();
761  res = ast_sip_push_task_wait_serializer(session->serializer, answer, &ans_data);
762  if (res) {
763  if (res == -1) {
764  ast_log(LOG_ERROR,"Cannot answer '%s': Unable to push answer task to the threadpool.\n",
765  ast_channel_name(session->channel));
766 #ifdef HAVE_PJSIP_INV_SESSION_REF
767  pjsip_inv_dec_ref(session->inv_session);
768 #endif
769  }
770  ao2_ref(session, -1);
771  ast_channel_lock(ast);
772  SCOPE_EXIT_RTN_VALUE(-1, "Couldn't push task\n");
773  }
774  ao2_ref(session, -1);
775  ast_channel_lock(ast);
776 
778 }
779 
780 /*! \brief Internal helper function called when CNG tone is detected */
781 static struct ast_frame *chan_pjsip_cng_tone_detected(struct ast_channel *ast, struct ast_sip_session *session,
782  struct ast_frame *f)
783 {
784  const char *target_context;
785  int exists;
786  int dsp_features;
787 
788  dsp_features = ast_dsp_get_features(session->dsp);
789  dsp_features &= ~DSP_FEATURE_FAX_DETECT;
790  if (dsp_features) {
791  ast_dsp_set_features(session->dsp, dsp_features);
792  } else {
793  ast_dsp_free(session->dsp);
794  session->dsp = NULL;
795  }
796 
797  /* If already executing in the fax extension don't do anything */
798  if (!strcmp(ast_channel_exten(ast), "fax")) {
799  return f;
800  }
801 
802  target_context = S_OR(ast_channel_macrocontext(ast), ast_channel_context(ast));
803 
804  /*
805  * We need to unlock the channel here because ast_exists_extension has the
806  * potential to start and stop an autoservice on the channel. Such action
807  * is prone to deadlock if the channel is locked.
808  *
809  * ast_async_goto() has its own restriction on not holding the channel lock.
810  */
811  ast_channel_unlock(ast);
812  ast_frfree(f);
813  f = &ast_null_frame;
814  exists = ast_exists_extension(ast, target_context, "fax", 1,
815  S_COR(ast_channel_caller(ast)->id.number.valid,
816  ast_channel_caller(ast)->id.number.str, NULL));
817  if (exists) {
818  ast_verb(2, "Redirecting '%s' to fax extension due to CNG detection\n",
819  ast_channel_name(ast));
820  pbx_builtin_setvar_helper(ast, "FAXEXTEN", ast_channel_exten(ast));
821  if (ast_async_goto(ast, target_context, "fax", 1)) {
822  ast_log(LOG_ERROR, "Failed to async goto '%s' into fax extension in '%s'\n",
823  ast_channel_name(ast), target_context);
824  }
825  } else {
826  ast_log(LOG_NOTICE, "FAX CNG detected on '%s' but no fax extension in '%s'\n",
827  ast_channel_name(ast), target_context);
828  }
829 
830  /* It's possible for a masquerade to have occurred when doing the ast_async_goto resulting in
831  * the channel on the session having changed. Since we need to return with the original channel
832  * locked we lock the channel that was passed in and not session->channel.
833  */
834  ast_channel_lock(ast);
835 
836  return f;
837 }
838 
839 /*! \brief Determine if the given frame is in a format we've negotiated */
840 static int is_compatible_format(struct ast_sip_session *session, struct ast_frame *f)
841 {
842  struct ast_stream_topology *topology = session->active_media_state->topology;
843  struct ast_stream *stream = ast_stream_topology_get_stream(topology, f->stream_num);
844  const struct ast_format_cap *cap = ast_stream_get_formats(stream);
845 
847 }
848 
849 /*!
850  * \brief Function called by core to read any waiting frames
851  *
852  * \note The channel is already locked.
853  */
854 static struct ast_frame *chan_pjsip_read_stream(struct ast_channel *ast)
855 {
857  struct ast_sip_session *session = channel->session;
858  struct ast_sip_session_media_read_callback_state *callback_state;
859  struct ast_frame *f;
860  int fdno = ast_channel_fdno(ast) - AST_EXTENDED_FDS;
861  struct ast_frame *cur;
862 
863  if (fdno >= AST_VECTOR_SIZE(&session->active_media_state->read_callbacks)) {
864  return &ast_null_frame;
865  }
866 
867  callback_state = AST_VECTOR_GET_ADDR(&session->active_media_state->read_callbacks, fdno);
868  f = callback_state->read_callback(session, callback_state->session);
869 
870  if (!f) {
871  return f;
872  }
873 
874  for (cur = f; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
875  if (cur->frametype == AST_FRAME_VOICE) {
876  break;
877  }
878  }
879 
880  if (!cur || callback_state->session != session->active_media_state->default_session[callback_state->session->type]) {
881  return f;
882  }
883 
884  session = channel->session;
885 
886  /*
887  * Asymmetric RTP only has one native format set at a time.
888  * Therefore we need to update the native format to the current
889  * raw read format BEFORE the native format check
890  */
891  if (!session->endpoint->asymmetric_rtp_codec &&
893  is_compatible_format(session, cur)) {
894  struct ast_format_cap *caps;
895 
896  /* For maximum compatibility we ensure that the formats match that of the received media */
897  ast_debug(1, "Oooh, got a frame with format of %s on channel '%s' when we're sending '%s', switching to match\n",
900 
902  if (caps) {
905  ast_format_cap_append(caps, cur->subclass.format, 0);
907  ao2_ref(caps, -1);
908  }
909 
912 
913  if (ast_channel_is_bridged(ast)) {
915  }
916  }
917 
920  ast_debug(1, "Oooh, got a frame with format of %s on channel '%s' when it has not been negotiated\n",
922  ast_frfree(f);
923  return &ast_null_frame;
924  }
925 
926  if (session->dsp) {
927  int dsp_features;
928 
929  dsp_features = ast_dsp_get_features(session->dsp);
930  if ((dsp_features & DSP_FEATURE_FAX_DETECT)
931  && session->endpoint->faxdetect_timeout
932  && session->endpoint->faxdetect_timeout <= ast_channel_get_up_time(ast)) {
933  dsp_features &= ~DSP_FEATURE_FAX_DETECT;
934  if (dsp_features) {
935  ast_dsp_set_features(session->dsp, dsp_features);
936  } else {
937  ast_dsp_free(session->dsp);
938  session->dsp = NULL;
939  }
940  ast_debug(3, "Channel driver fax CNG detection timeout on %s\n",
941  ast_channel_name(ast));
942  }
943  }
944  if (session->dsp) {
945  f = ast_dsp_process(ast, session->dsp, f);
946  if (f && (f->frametype == AST_FRAME_DTMF)) {
947  if (f->subclass.integer == 'f') {
948  ast_debug(3, "Channel driver fax CNG detected on %s\n",
949  ast_channel_name(ast));
950  f = chan_pjsip_cng_tone_detected(ast, session, f);
951  /* When chan_pjsip_cng_tone_detected returns it is possible for the
952  * channel pointed to by ast and by session->channel to differ due to a
953  * masquerade. It's best not to touch things after this.
954  */
955  } else {
956  ast_debug(3, "* Detected inband DTMF '%c' on '%s'\n", f->subclass.integer,
957  ast_channel_name(ast));
958  }
959  }
960  }
961 
962  return f;
963 }
964 
965 static int chan_pjsip_write_stream(struct ast_channel *ast, int stream_num, struct ast_frame *frame)
966 {
968  struct ast_sip_session *session = channel->session;
969  struct ast_sip_session_media *media = NULL;
970  int res = 0;
971 
972  /* The core provides a guarantee that the stream will exist when we are called if stream_num is provided */
973  if (stream_num >= 0) {
974  /* What is not guaranteed is that a media session will exist */
975  if (stream_num < AST_VECTOR_SIZE(&channel->session->active_media_state->sessions)) {
976  media = AST_VECTOR_GET(&channel->session->active_media_state->sessions, stream_num);
977  }
978  }
979 
980  switch (frame->frametype) {
981  case AST_FRAME_VOICE:
982  if (!media) {
983  return 0;
984  } else if (media->type != AST_MEDIA_TYPE_AUDIO) {
985  ast_debug(3, "Channel %s stream %d is of type '%s', not audio!\n",
986  ast_channel_name(ast), stream_num, ast_codec_media_type2str(media->type));
987  return 0;
988  } else if (media == channel->session->active_media_state->default_session[AST_MEDIA_TYPE_AUDIO] &&
991  struct ast_str *write_transpath = ast_str_alloca(256);
992  struct ast_str *read_transpath = ast_str_alloca(256);
993 
995  "Channel %s asked to send %s frame when native formats are %s (rd:%s->%s;%s wr:%s->%s;%s)\n",
996  ast_channel_name(ast),
1001  ast_translate_path_to_str(ast_channel_readtrans(ast), &read_transpath),
1004  ast_translate_path_to_str(ast_channel_writetrans(ast), &write_transpath));
1005  return 0;
1006  } else if (media->write_callback) {
1007  res = media->write_callback(session, media, frame);
1008 
1009  }
1010  break;
1011  case AST_FRAME_VIDEO:
1012  if (!media) {
1013  return 0;
1014  } else if (media->type != AST_MEDIA_TYPE_VIDEO) {
1015  ast_debug(3, "Channel %s stream %d is of type '%s', not video!\n",
1016  ast_channel_name(ast), stream_num, ast_codec_media_type2str(media->type));
1017  return 0;
1018  } else if (media->write_callback) {
1019  res = media->write_callback(session, media, frame);
1020  }
1021  break;
1022  case AST_FRAME_MODEM:
1023  if (!media) {
1024  return 0;
1025  } else if (media->type != AST_MEDIA_TYPE_IMAGE) {
1026  ast_debug(3, "Channel %s stream %d is of type '%s', not image!\n",
1027  ast_channel_name(ast), stream_num, ast_codec_media_type2str(media->type));
1028  return 0;
1029  } else if (media->write_callback) {
1030  res = media->write_callback(session, media, frame);
1031  }
1032  break;
1033  case AST_FRAME_CNG:
1034  break;
1035  case AST_FRAME_RTCP:
1036  /* We only support writing out feedback */
1037  if (frame->subclass.integer != AST_RTP_RTCP_PSFB || !media) {
1038  return 0;
1039  } else if (media->type != AST_MEDIA_TYPE_VIDEO) {
1040  ast_debug(3, "Channel %s stream %d is of type '%s', not video! Unable to write RTCP feedback.\n",
1041  ast_channel_name(ast), stream_num, ast_codec_media_type2str(media->type));
1042  return 0;
1043  } else if (media->write_callback) {
1044  res = media->write_callback(session, media, frame);
1045  }
1046  break;
1047  default:
1048  ast_log(LOG_WARNING, "Can't send %u type frames with PJSIP\n", frame->frametype);
1049  break;
1050  }
1051 
1052  return res;
1053 }
1054 
1055 static int chan_pjsip_write(struct ast_channel *ast, struct ast_frame *frame)
1056 {
1057  return chan_pjsip_write_stream(ast, -1, frame);
1058 }
1059 
1060 /*! \brief Function called by core to change the underlying owner channel */
1061 static int chan_pjsip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
1062 {
1064 
1065  if (channel->session->channel != oldchan) {
1066  return -1;
1067  }
1068 
1069  /*
1070  * The masquerade has suspended the channel's session
1071  * serializer so we can safely change it outside of
1072  * the serializer thread.
1073  */
1074  channel->session->channel = newchan;
1075 
1077 
1078  return 0;
1079 }
1080 
1081 /*! AO2 hash function for on hold UIDs */
1082 static int uid_hold_hash_fn(const void *obj, const int flags)
1083 {
1084  const char *key = obj;
1085 
1086  switch (flags & OBJ_SEARCH_MASK) {
1087  case OBJ_SEARCH_KEY:
1088  break;
1089  case OBJ_SEARCH_OBJECT:
1090  break;
1091  default:
1092  /* Hash can only work on something with a full key. */
1093  ast_assert(0);
1094  return 0;
1095  }
1096  return ast_str_hash(key);
1097 }
1098 
1099 /*! AO2 sort function for on hold UIDs */
1100 static int uid_hold_sort_fn(const void *obj_left, const void *obj_right, const int flags)
1101 {
1102  const char *left = obj_left;
1103  const char *right = obj_right;
1104  int cmp;
1105 
1106  switch (flags & OBJ_SEARCH_MASK) {
1107  case OBJ_SEARCH_OBJECT:
1108  case OBJ_SEARCH_KEY:
1109  cmp = strcmp(left, right);
1110  break;
1112  cmp = strncmp(left, right, strlen(right));
1113  break;
1114  default:
1115  /* Sort can only work on something with a full or partial key. */
1116  ast_assert(0);
1117  cmp = 0;
1118  break;
1119  }
1120  return cmp;
1121 }
1122 
1124 
1125 /*!
1126  * \brief Add a channel ID to the list of PJSIP channels on hold
1127  *
1128  * \param chan_uid - Unique ID of the channel being put into the hold list
1129  *
1130  * \retval 0 Channel has been added to or was already in the hold list
1131  * \retval -1 Failed to add channel to the hold list
1132  */
1133 static int chan_pjsip_add_hold(const char *chan_uid)
1134 {
1135  RAII_VAR(char *, hold_uid, NULL, ao2_cleanup);
1136 
1137  hold_uid = ao2_find(pjsip_uids_onhold, chan_uid, OBJ_SEARCH_KEY);
1138  if (hold_uid) {
1139  /* Device is already on hold. Nothing to do. */
1140  return 0;
1141  }
1142 
1143  /* Device wasn't in hold list already. Create a new one. */
1144  hold_uid = ao2_alloc_options(strlen(chan_uid) + 1, NULL,
1146  if (!hold_uid) {
1147  return -1;
1148  }
1149 
1150  ast_copy_string(hold_uid, chan_uid, strlen(chan_uid) + 1);
1151 
1152  if (ao2_link(pjsip_uids_onhold, hold_uid) == 0) {
1153  return -1;
1154  }
1155 
1156  return 0;
1157 }
1158 
1159 /*!
1160  * \brief Remove a channel ID from the list of PJSIP channels on hold
1161  *
1162  * \param chan_uid - Unique ID of the channel being taken out of the hold list
1163  */
1164 static void chan_pjsip_remove_hold(const char *chan_uid)
1165 {
1166  ao2_find(pjsip_uids_onhold, chan_uid, OBJ_SEARCH_KEY | OBJ_UNLINK | OBJ_NODATA);
1167 }
1168 
1169 /*!
1170  * \brief Determine whether a channel ID is in the list of PJSIP channels on hold
1171  *
1172  * \param chan_uid - Channel being checked
1173  *
1174  * \retval 0 The channel is not in the hold list
1175  * \retval 1 The channel is in the hold list
1176  */
1177 static int chan_pjsip_get_hold(const char *chan_uid)
1178 {
1179  RAII_VAR(char *, hold_uid, NULL, ao2_cleanup);
1180 
1181  hold_uid = ao2_find(pjsip_uids_onhold, chan_uid, OBJ_SEARCH_KEY);
1182  if (!hold_uid) {
1183  return 0;
1184  }
1185 
1186  return 1;
1187 }
1188 
1189 /*! \brief Function called to get the device state of an endpoint */
1190 static int chan_pjsip_devicestate(const char *data)
1191 {
1192  RAII_VAR(struct ast_sip_endpoint *, endpoint, ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", data), ao2_cleanup);
1194  RAII_VAR(struct ast_endpoint_snapshot *, endpoint_snapshot, NULL, ao2_cleanup);
1195  struct ast_devstate_aggregate aggregate;
1196  int num, inuse = 0;
1197 
1198  if (!endpoint) {
1199  return AST_DEVICE_INVALID;
1200  }
1201 
1202  endpoint_snapshot = ast_endpoint_latest_snapshot(ast_endpoint_get_tech(endpoint->persistent),
1203  ast_endpoint_get_resource(endpoint->persistent));
1204 
1205  if (!endpoint_snapshot) {
1206  return AST_DEVICE_INVALID;
1207  }
1208 
1209  if (endpoint_snapshot->state == AST_ENDPOINT_OFFLINE) {
1210  state = AST_DEVICE_UNAVAILABLE;
1211  } else if (endpoint_snapshot->state == AST_ENDPOINT_ONLINE) {
1212  state = AST_DEVICE_NOT_INUSE;
1213  }
1214 
1215  if (!endpoint_snapshot->num_channels) {
1216  return state;
1217  }
1218 
1219  ast_devstate_aggregate_init(&aggregate);
1220 
1221  for (num = 0; num < endpoint_snapshot->num_channels; num++) {
1222  struct ast_channel_snapshot *snapshot;
1223 
1224  snapshot = ast_channel_snapshot_get_latest(endpoint_snapshot->channel_ids[num]);
1225  if (!snapshot) {
1226  continue;
1227  }
1228 
1229  if (chan_pjsip_get_hold(snapshot->base->uniqueid)) {
1231  } else {
1232  ast_devstate_aggregate_add(&aggregate, ast_state_chan2dev(snapshot->state));
1233  }
1234 
1235  if ((snapshot->state == AST_STATE_UP) || (snapshot->state == AST_STATE_RING) ||
1236  (snapshot->state == AST_STATE_BUSY)) {
1237  inuse++;
1238  }
1239 
1240  ao2_ref(snapshot, -1);
1241  }
1242 
1243  if (endpoint->devicestate_busy_at && (inuse == endpoint->devicestate_busy_at)) {
1244  state = AST_DEVICE_BUSY;
1245  } else if (ast_devstate_aggregate_result(&aggregate) != AST_DEVICE_INVALID) {
1246  state = ast_devstate_aggregate_result(&aggregate);
1247  }
1248 
1249  return state;
1250 }
1251 
1252 /*! \brief Function called to query options on a channel */
1253 static int chan_pjsip_queryoption(struct ast_channel *ast, int option, void *data, int *datalen)
1254 {
1256  int res = -1;
1258 
1259  if (!channel) {
1260  return -1;
1261  }
1262 
1263  switch (option) {
1264  case AST_OPTION_T38_STATE:
1265  if (channel->session->endpoint->media.t38.enabled) {
1266  switch (channel->session->t38state) {
1267  case T38_LOCAL_REINVITE:
1268  case T38_PEER_REINVITE:
1269  state = T38_STATE_NEGOTIATING;
1270  break;
1271  case T38_ENABLED:
1272  state = T38_STATE_NEGOTIATED;
1273  break;
1274  case T38_REJECTED:
1275  state = T38_STATE_REJECTED;
1276  break;
1277  default:
1278  state = T38_STATE_UNKNOWN;
1279  break;
1280  }
1281  }
1282 
1283  *((enum ast_t38_state *) data) = state;
1284  res = 0;
1285 
1286  break;
1287  default:
1288  break;
1289  }
1290 
1291  return res;
1292 }
1293 
1294 static const char *chan_pjsip_get_uniqueid(struct ast_channel *ast)
1295 {
1298 
1299  if (!uniqueid) {
1300  return "";
1301  }
1302 
1303  ast_copy_pj_str(uniqueid, &channel->session->inv_session->dlg->call_id->id, UNIQUEID_BUFSIZE);
1304 
1305  return uniqueid;
1306 }
1307 
1312  void *frame_data;
1313  size_t datalen;
1314 };
1315 
1316 static void indicate_data_destroy(void *obj)
1317 {
1318  struct indicate_data *ind_data = obj;
1319 
1320  ast_free(ind_data->frame_data);
1321  ao2_ref(ind_data->session, -1);
1322 }
1323 
1324 static struct indicate_data *indicate_data_alloc(struct ast_sip_session *session,
1325  int condition, int response_code, const void *frame_data, size_t datalen)
1326 {
1327  struct indicate_data *ind_data = ao2_alloc(sizeof(*ind_data), indicate_data_destroy);
1328 
1329  if (!ind_data) {
1330  return NULL;
1331  }
1332 
1333  ind_data->frame_data = ast_malloc(datalen);
1334  if (!ind_data->frame_data) {
1335  ao2_ref(ind_data, -1);
1336  return NULL;
1337  }
1338 
1339  memcpy(ind_data->frame_data, frame_data, datalen);
1340  ind_data->datalen = datalen;
1341  ind_data->condition = condition;
1342  ind_data->response_code = response_code;
1343  ao2_ref(session, +1);
1344  ind_data->session = session;
1345 
1346  return ind_data;
1347 }
1348 
1349 static int indicate(void *data)
1350 {
1351  pjsip_tx_data *packet = NULL;
1352  struct indicate_data *ind_data = data;
1353  struct ast_sip_session *session = ind_data->session;
1354  int response_code = ind_data->response_code;
1355 
1356  if ((session->inv_session->state != PJSIP_INV_STATE_DISCONNECTED) &&
1357  (pjsip_inv_answer(session->inv_session, response_code, NULL, NULL, &packet) == PJ_SUCCESS)) {
1358  ast_sip_session_send_response(session, packet);
1359  }
1360 
1361 #ifdef HAVE_PJSIP_INV_SESSION_REF
1362  pjsip_inv_dec_ref(session->inv_session);
1363 #endif
1364  ao2_ref(ind_data, -1);
1365 
1366  return 0;
1367 }
1368 
1369 /*! \brief Send SIP INFO with video update request */
1370 static int transmit_info_with_vidupdate(void *data)
1371 {
1372  const char * xml =
1373  "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n"
1374  " <media_control>\r\n"
1375  " <vc_primitive>\r\n"
1376  " <to_encoder>\r\n"
1377  " <picture_fast_update/>\r\n"
1378  " </to_encoder>\r\n"
1379  " </vc_primitive>\r\n"
1380  " </media_control>\r\n";
1381 
1382  const struct ast_sip_body body = {
1383  .type = "application",
1384  .subtype = "media_control+xml",
1385  .body_text = xml
1386  };
1387 
1388  RAII_VAR(struct ast_sip_session *, session, data, ao2_cleanup);
1389  struct pjsip_tx_data *tdata;
1390 
1391  if (session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
1392  ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
1393  session->inv_session->cause,
1394  pjsip_get_status_text(session->inv_session->cause)->ptr);
1395  goto failure;
1396  }
1397 
1398  if (ast_sip_create_request("INFO", session->inv_session->dlg, session->endpoint, NULL, NULL, &tdata)) {
1399  ast_log(LOG_ERROR, "Could not create text video update INFO request\n");
1400  goto failure;
1401  }
1402  if (ast_sip_add_body(tdata, &body)) {
1403  ast_log(LOG_ERROR, "Could not add body to text video update INFO request\n");
1404  goto failure;
1405  }
1406  ast_sip_session_send_request(session, tdata);
1407 
1408 #ifdef HAVE_PJSIP_INV_SESSION_REF
1409  pjsip_inv_dec_ref(session->inv_session);
1410 #endif
1411 
1412  return 0;
1413 
1414 failure:
1415 #ifdef HAVE_PJSIP_INV_SESSION_REF
1416  pjsip_inv_dec_ref(session->inv_session);
1417 #endif
1418  return -1;
1419 
1420 }
1421 
1422 /*!
1423  * \internal
1424  * \brief TRUE if a COLP update can be sent to the peer.
1425  * \since 13.3.0
1426  *
1427  * \param session The session to see if the COLP update is allowed.
1428  *
1429  * \retval 0 Update is not allowed.
1430  * \retval 1 Update is allowed.
1431  */
1432 static int is_colp_update_allowed(struct ast_sip_session *session)
1433 {
1434  struct ast_party_id connected_id;
1435  int update_allowed = 0;
1436 
1437  if (!session->endpoint->id.send_connected_line
1438  || (!session->endpoint->id.send_pai && !session->endpoint->id.send_rpid)) {
1439  return 0;
1440  }
1441 
1442  /*
1443  * Check if privacy allows the update. Check while the channel
1444  * is locked so we can work with the shallow connected_id copy.
1445  */
1446  ast_channel_lock(session->channel);
1447  connected_id = ast_channel_connected_effective_id(session->channel);
1448  if (connected_id.number.valid
1449  && (session->endpoint->id.trust_outbound
1451  update_allowed = 1;
1452  }
1453  ast_channel_unlock(session->channel);
1454 
1455  return update_allowed;
1456 }
1457 
1458 /*! \brief Update connected line information */
1460 {
1461  struct ast_sip_session *session = data;
1462 
1463  if (session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
1464  ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
1465  session->inv_session->cause,
1466  pjsip_get_status_text(session->inv_session->cause)->ptr);
1467 #ifdef HAVE_PJSIP_INV_SESSION_REF
1468  pjsip_inv_dec_ref(session->inv_session);
1469 #endif
1470  ao2_ref(session, -1);
1471  return -1;
1472  }
1473 
1474  if (ast_channel_state(session->channel) == AST_STATE_UP
1475  || session->inv_session->role == PJSIP_ROLE_UAC) {
1476  if (is_colp_update_allowed(session)) {
1478  int generate_new_sdp;
1479 
1480  method = session->endpoint->id.refresh_method;
1481  if (session->inv_session->options & PJSIP_INV_SUPPORT_UPDATE) {
1483  }
1484 
1485  /* Only the INVITE method actually needs SDP, UPDATE can do without */
1486  generate_new_sdp = (method == AST_SIP_SESSION_REFRESH_METHOD_INVITE);
1487 
1488  ast_sip_session_refresh(session, NULL, NULL, NULL, method, generate_new_sdp, NULL);
1489  }
1490  } else if (session->endpoint->id.rpid_immediate
1491  && session->inv_session->state != PJSIP_INV_STATE_DISCONNECTED
1492  && is_colp_update_allowed(session)) {
1493  int response_code = 0;
1494 
1495  if (ast_channel_state(session->channel) == AST_STATE_RING) {
1496  response_code = !session->endpoint->inband_progress ? 180 : 183;
1497  } else if (ast_channel_state(session->channel) == AST_STATE_RINGING) {
1498  response_code = 183;
1499  }
1500 
1501  if (response_code) {
1502  struct pjsip_tx_data *packet = NULL;
1503 
1504  if (pjsip_inv_answer(session->inv_session, response_code, NULL, NULL, &packet) == PJ_SUCCESS) {
1505  ast_sip_session_send_response(session, packet);
1506  }
1507  }
1508  }
1509 
1510 #ifdef HAVE_PJSIP_INV_SESSION_REF
1511  pjsip_inv_dec_ref(session->inv_session);
1512 #endif
1513 
1514  ao2_ref(session, -1);
1515  return 0;
1516 }
1517 
1518 /*! \brief Callback which changes the value of locally held on the media stream */
1519 static void local_hold_set_state(struct ast_sip_session_media *session_media, unsigned int held)
1520 {
1521  if (session_media) {
1522  session_media->locally_held = held;
1523  }
1524 }
1525 
1526 /*! \brief Update local hold state and send a re-INVITE with the new SDP */
1527 static int remote_send_hold_refresh(struct ast_sip_session *session, unsigned int held)
1528 {
1531  ao2_ref(session, -1);
1532 
1533  return 0;
1534 }
1535 
1536 /*! \brief Update local hold state to be held */
1537 static int remote_send_hold(void *data)
1538 {
1539  return remote_send_hold_refresh(data, 1);
1540 }
1541 
1542 /*! \brief Update local hold state to be unheld */
1543 static int remote_send_unhold(void *data)
1544 {
1545  return remote_send_hold_refresh(data, 0);
1546 }
1547 
1551 };
1552 
1554 {
1555  ao2_cleanup(refresh_data->session);
1556 
1558  ast_free(refresh_data);
1559 }
1560 
1562  struct ast_sip_session *session, const struct ast_stream_topology *topology)
1563 {
1565 
1566  refresh_data = ast_calloc(1, sizeof(*refresh_data));
1567  if (!refresh_data) {
1568  return NULL;
1569  }
1570 
1571  refresh_data->session = ao2_bump(session);
1573  if (!refresh_data->media_state) {
1574  topology_change_refresh_data_free(refresh_data);
1575  return NULL;
1576  }
1577  refresh_data->media_state->topology = ast_stream_topology_clone(topology);
1578  if (!refresh_data->media_state->topology) {
1579  topology_change_refresh_data_free(refresh_data);
1580  return NULL;
1581  }
1582 
1583  return refresh_data;
1584 }
1585 
1586 static int on_topology_change_response(struct ast_sip_session *session, pjsip_rx_data *rdata)
1587 {
1588  SCOPE_ENTER(3, "%s: Received response code %d. PT: %s AT: %s\n", ast_sip_session_get_name(session),
1589  rdata->msg_info.msg->line.status.code,
1592 
1593 
1594  if (PJSIP_IS_STATUS_IN_CLASS(rdata->msg_info.msg->line.status.code, 200)) {
1595  /* The topology was changed to something new so give notice to what requested
1596  * it so it queries the channel and updates accordingly.
1597  */
1598  if (session->channel) {
1600  SCOPE_EXIT_RTN_VALUE(0, "%s: Queued topology change frame\n", ast_sip_session_get_name(session));
1601  }
1602  SCOPE_EXIT_RTN_VALUE(0, "%s: No channel? Can't queue topology change frame\n", ast_sip_session_get_name(session));
1603  } else if (300 <= rdata->msg_info.msg->line.status.code) {
1604  /* The topology change failed, so drop the current pending media state */
1606  SCOPE_EXIT_RTN_VALUE(0, "%s: response code > 300. Resetting pending media state\n", ast_sip_session_get_name(session));
1607  }
1608 
1609  SCOPE_EXIT_RTN_VALUE(0, "%s: Nothing to do\n", ast_sip_session_get_name(session));
1610 }
1611 
1612 static int send_topology_change_refresh(void *data)
1613 {
1615  struct ast_sip_session *session = refresh_data->session;
1616  int ret;
1617  SCOPE_ENTER(3, "%s: %s\n", ast_sip_session_get_name(session),
1618  ast_str_tmp(256, ast_stream_topology_to_str(refresh_data->media_state->topology, &STR_TMP)));
1619 
1620 
1623  refresh_data->media_state = NULL;
1624  topology_change_refresh_data_free(refresh_data);
1625 
1626  SCOPE_EXIT_RTN_VALUE(ret, "%s\n", ast_sip_session_get_name(session));
1627 }
1628 
1630  const struct ast_stream_topology *proposed)
1631 {
1633  int res;
1634  SCOPE_ENTER(1);
1635 
1636  refresh_data = topology_change_refresh_data_alloc(session, proposed);
1637  if (!refresh_data) {
1638  SCOPE_EXIT_RTN_VALUE(-1, "Couldn't create refresh_data\n");
1639  }
1640 
1641  res = ast_sip_push_task(session->serializer, send_topology_change_refresh, refresh_data);
1642  if (res) {
1643  topology_change_refresh_data_free(refresh_data);
1644  }
1645  SCOPE_EXIT_RTN_VALUE(res, "RC: %d\n", res);
1646 }
1647 
1648 /*! \brief Function called by core to ask the channel to indicate some sort of condition */
1649 static int chan_pjsip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
1650 {
1652  struct ast_sip_session_media *media;
1653  int response_code = 0;
1654  int res = 0;
1655  char *device_buf;
1656  size_t device_buf_size;
1657  int i;
1658  const struct ast_stream_topology *topology;
1659  struct ast_frame f = {
1661  .subclass = {
1662  .integer = condition
1663  }
1664  };
1665  char condition_name[256];
1666  SCOPE_ENTER(3, "%s: Indicated %s\n", ast_channel_name(ast),
1667  ast_frame_subclass2str(&f, condition_name, sizeof(condition_name), NULL, 0));
1668 
1669  switch (condition) {
1670  case AST_CONTROL_RINGING:
1671  if (ast_channel_state(ast) == AST_STATE_RING) {
1672  if (channel->session->endpoint->inband_progress ||
1673  (channel->session->inv_session && channel->session->inv_session->neg &&
1674  pjmedia_sdp_neg_get_state(channel->session->inv_session->neg) == PJMEDIA_SDP_NEG_STATE_DONE)) {
1675  response_code = 183;
1676  res = -1;
1677  } else {
1678  response_code = 180;
1679  }
1680  } else {
1681  res = -1;
1682  }
1684  break;
1685  case AST_CONTROL_BUSY:
1686  if (ast_channel_state(ast) != AST_STATE_UP) {
1687  response_code = 486;
1688  } else {
1689  res = -1;
1690  }
1691  break;
1693  if (ast_channel_state(ast) != AST_STATE_UP) {
1694  response_code = 503;
1695  } else {
1696  res = -1;
1697  }
1698  break;
1700  if (ast_channel_state(ast) != AST_STATE_UP) {
1701  response_code = 484;
1702  } else {
1703  res = -1;
1704  }
1705  break;
1707  if (ast_channel_state(ast) != AST_STATE_UP) {
1708  response_code = 100;
1709  } else {
1710  res = -1;
1711  }
1712  break;
1713  case AST_CONTROL_PROGRESS:
1714  if (ast_channel_state(ast) != AST_STATE_UP) {
1715  response_code = 183;
1716  } else {
1717  res = -1;
1718  }
1720  break;
1721  case AST_CONTROL_VIDUPDATE:
1722  for (i = 0; i < AST_VECTOR_SIZE(&channel->session->active_media_state->sessions); ++i) {
1723  media = AST_VECTOR_GET(&channel->session->active_media_state->sessions, i);
1724  if (!media || media->type != AST_MEDIA_TYPE_VIDEO) {
1725  continue;
1726  }
1727  if (media->rtp) {
1728  /* FIXME: Only use this for VP8. Additional work would have to be done to
1729  * fully support other video codecs */
1730 
1734  (channel->session->endpoint->media.webrtc &&
1736  /* FIXME Fake RTP write, this will be sent as an RTCP packet. Ideally the
1737  * RTP engine would provide a way to externally write/schedule RTCP
1738  * packets */
1739  struct ast_frame fr;
1742  res = ast_rtp_instance_write(media->rtp, &fr);
1743  } else {
1744  ao2_ref(channel->session, +1);
1745 #ifdef HAVE_PJSIP_INV_SESSION_REF
1746  if (pjsip_inv_add_ref(channel->session->inv_session) != PJ_SUCCESS) {
1747  ast_log(LOG_ERROR, "Can't increase the session reference counter\n");
1748  ao2_cleanup(channel->session);
1749  } else {
1750 #endif
1752  ao2_cleanup(channel->session);
1753  }
1754 #ifdef HAVE_PJSIP_INV_SESSION_REF
1755  }
1756 #endif
1757  }
1758  ast_test_suite_event_notify("AST_CONTROL_VIDUPDATE", "Result: Success");
1759  } else {
1760  ast_test_suite_event_notify("AST_CONTROL_VIDUPDATE", "Result: Failure");
1761  res = -1;
1762  }
1763  }
1764  /* XXX If there were no video streams, then this should set
1765  * res to -1
1766  */
1767  break;
1769  ao2_ref(channel->session, +1);
1770 #ifdef HAVE_PJSIP_INV_SESSION_REF
1771  if (pjsip_inv_add_ref(channel->session->inv_session) != PJ_SUCCESS) {
1772  ao2_cleanup(channel->session);
1773  SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: Couldn't increase the session reference counter\n",
1774  ast_channel_name(ast));
1775  }
1776 #endif
1778 #ifdef HAVE_PJSIP_INV_SESSION_REF
1779  pjsip_inv_dec_ref(channel->session->inv_session);
1780 #endif
1781  ao2_cleanup(channel->session);
1782  }
1783  break;
1785  break;
1787  res = -1;
1788  break;
1790  ast_assert(datalen == sizeof(int));
1791  if (*(int *) data) {
1792  /*
1793  * Masquerade is beginning:
1794  * Wait for session serializer to get suspended.
1795  */
1796  ast_channel_unlock(ast);
1797  ast_sip_session_suspend(channel->session);
1798  ast_channel_lock(ast);
1799  } else {
1800  /*
1801  * Masquerade is complete:
1802  * Unsuspend the session serializer.
1803  */
1805  }
1806  break;
1807  case AST_CONTROL_HOLD:
1809  device_buf_size = strlen(ast_channel_name(ast)) + 1;
1810  device_buf = alloca(device_buf_size);
1811  ast_channel_get_device_name(ast, device_buf, device_buf_size);
1813  if (!channel->session->moh_passthrough) {
1814  ast_moh_start(ast, data, NULL);
1815  } else {
1817  ast_log(LOG_WARNING, "Could not queue task to remotely put session '%s' on hold with endpoint '%s'\n",
1819  ao2_ref(channel->session, -1);
1820  }
1821  }
1822  break;
1823  case AST_CONTROL_UNHOLD:
1825  device_buf_size = strlen(ast_channel_name(ast)) + 1;
1826  device_buf = alloca(device_buf_size);
1827  ast_channel_get_device_name(ast, device_buf, device_buf_size);
1829  if (!channel->session->moh_passthrough) {
1830  ast_moh_stop(ast);
1831  } else {
1833  ast_log(LOG_WARNING, "Could not queue task to remotely take session '%s' off hold with endpoint '%s'\n",
1835  ao2_ref(channel->session, -1);
1836  }
1837  }
1838  break;
1839  case AST_CONTROL_SRCUPDATE:
1840  break;
1841  case AST_CONTROL_SRCCHANGE:
1842  break;
1844  if (ast_channel_state(ast) != AST_STATE_UP) {
1845  response_code = 181;
1846  } else {
1847  res = -1;
1848  }
1849  break;
1851  res = 0;
1852 
1853  if (channel->session->t38state == T38_PEER_REINVITE) {
1854  const struct ast_control_t38_parameters *parameters = data;
1855 
1856  if (parameters->request_response == AST_T38_REQUEST_PARMS) {
1857  res = AST_T38_REQUEST_PARMS;
1858  }
1859  }
1860 
1861  break;
1863  topology = data;
1864  ast_trace(-1, "%s: New topology: %s\n", ast_channel_name(ast),
1865  ast_str_tmp(256, ast_stream_topology_to_str(topology, &STR_TMP)));
1866  res = handle_topology_request_change(channel->session, topology);
1867  break;
1869  break;
1871  break;
1872  case -1:
1873  res = -1;
1874  break;
1875  default:
1876  ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", condition);
1877  res = -1;
1878  break;
1879  }
1880 
1881  if (response_code) {
1882  struct indicate_data *ind_data = indicate_data_alloc(channel->session, condition, response_code, data, datalen);
1883 
1884  if (!ind_data) {
1885  SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: Couldn't alloc indicate data\n", ast_channel_name(ast));
1886  }
1887 #ifdef HAVE_PJSIP_INV_SESSION_REF
1888  if (pjsip_inv_add_ref(ind_data->session->inv_session) != PJ_SUCCESS) {
1889  ao2_cleanup(ind_data);
1890  SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: Couldn't increase the session reference counter\n", ast_channel_name(ast));
1891  }
1892 #endif
1893  if (ast_sip_push_task(channel->session->serializer, indicate, ind_data)) {
1894  ast_log(LOG_ERROR, "%s: Cannot send response code %d to endpoint %s. Could not queue task properly\n",
1895  ast_channel_name(ast), response_code, ast_sorcery_object_get_id(channel->session->endpoint));
1896 #ifdef HAVE_PJSIP_INV_SESSION_REF
1897  pjsip_inv_dec_ref(ind_data->session->inv_session);
1898 #endif
1899  ao2_cleanup(ind_data);
1900  res = -1;
1901  }
1902  }
1903 
1904  SCOPE_EXIT_RTN_VALUE(res, "%s\n", ast_channel_name(ast));
1905 }
1906 
1909  char *target;
1910 };
1911 
1912 static void transfer_data_destroy(void *obj)
1913 {
1914  struct transfer_data *trnf_data = obj;
1915 
1916  ast_free(trnf_data->target);
1917  ao2_cleanup(trnf_data->session);
1918 }
1919 
1920 static struct transfer_data *transfer_data_alloc(struct ast_sip_session *session, const char *target)
1921 {
1922  struct transfer_data *trnf_data = ao2_alloc(sizeof(*trnf_data), transfer_data_destroy);
1923 
1924  if (!trnf_data) {
1925  return NULL;
1926  }
1927 
1928  if (!(trnf_data->target = ast_strdup(target))) {
1929  ao2_ref(trnf_data, -1);
1930  return NULL;
1931  }
1932 
1933  ao2_ref(session, +1);
1934  trnf_data->session = session;
1935 
1936  return trnf_data;
1937 }
1938 
1939 static void transfer_redirect(struct ast_sip_session *session, const char *target)
1940 {
1941  pjsip_tx_data *packet;
1943  pjsip_contact_hdr *contact;
1944  pj_str_t tmp;
1945 
1946  if (pjsip_inv_end_session(session->inv_session, 302, NULL, &packet) != PJ_SUCCESS
1947  || !packet) {
1948  ast_log(LOG_WARNING, "Failed to redirect PJSIP session for channel %s\n",
1949  ast_channel_name(session->channel));
1950  message = AST_TRANSFER_FAILED;
1951  ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
1952 
1953  return;
1954  }
1955 
1956  if (!(contact = pjsip_msg_find_hdr(packet->msg, PJSIP_H_CONTACT, NULL))) {
1957  contact = pjsip_contact_hdr_create(packet->pool);
1958  }
1959 
1960  pj_strdup2_with_null(packet->pool, &tmp, target);
1961  if (!(contact->uri = pjsip_parse_uri(packet->pool, tmp.ptr, tmp.slen, PJSIP_PARSE_URI_AS_NAMEADDR))) {
1962  ast_log(LOG_WARNING, "Failed to parse destination URI '%s' for channel %s\n",
1963  target, ast_channel_name(session->channel));
1964  message = AST_TRANSFER_FAILED;
1965  ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
1966  pjsip_tx_data_dec_ref(packet);
1967 
1968  return;
1969  }
1970  pjsip_msg_add_hdr(packet->msg, (pjsip_hdr *) contact);
1971 
1972  ast_sip_session_send_response(session, packet);
1973  ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
1974 }
1975 
1976 /*! \brief REFER Callback module, used to attach session data structure to subscription */
1977 static pjsip_module refer_callback_module = {
1978  .name = { "REFER Callback", 14 },
1979  .id = -1,
1980 };
1981 
1982 /*!
1983  * \brief Callback function to report status of implicit REFER-NOTIFY subscription.
1984  *
1985  * This function will be called on any state change in the REFER-NOTIFY subscription.
1986  * Its primary purpose is to report SUCCESS/FAILURE of a transfer initiated via
1987  * \ref transfer_refer as well as to terminate the subscription, if necessary.
1988  */
1989 static void xfer_client_on_evsub_state(pjsip_evsub *sub, pjsip_event *event)
1990 {
1991  struct ast_sip_session *session;
1992  struct ast_channel *chan = NULL;
1994  int res = 0;
1995 
1996  if (!event) {
1997  return;
1998  }
1999 
2000  session = pjsip_evsub_get_mod_data(sub, refer_callback_module.id);
2001  if (!session) {
2002  return;
2003  }
2004 
2005  chan = session->channel;
2006  if (!chan) {
2007  return;
2008  }
2009 
2010  if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_ACCEPTED) {
2011  /* Check if subscription is suppressed and terminate and send completion code, if so. */
2012  pjsip_rx_data *rdata;
2013  pjsip_generic_string_hdr *refer_sub;
2014  const pj_str_t REFER_SUB = { "Refer-Sub", 9 };
2015 
2016  ast_debug(3, "Transfer accepted on channel %s\n", ast_channel_name(chan));
2017 
2018  /* Check if response message */
2019  if (event->type == PJSIP_EVENT_TSX_STATE && event->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
2020  rdata = event->body.tsx_state.src.rdata;
2021 
2022  /* Find Refer-Sub header */
2023  refer_sub = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &REFER_SUB, NULL);
2024 
2025  /* Check if subscription is suppressed. If it is, the far end will not terminate it,
2026  * and the subscription will remain active until it times out. Terminating it here
2027  * eliminates the unnecessary timeout.
2028  */
2029  if (refer_sub && !pj_stricmp2(&refer_sub->hvalue, "false")) {
2030  /* Since no subscription is desired, assume that call has been transferred successfully. */
2031  /* Terminate subscription. */
2032  pjsip_evsub_terminate(sub, PJ_TRUE);
2033  res = -1;
2034  }
2035  }
2036  } else if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_ACTIVE ||
2037  pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) {
2038  /* Check for NOTIFY complete or error. */
2039  pjsip_msg *msg;
2040  pjsip_msg_body *body;
2041  pjsip_status_line status_line = { .code = 0 };
2042  pj_bool_t is_last;
2043  pj_status_t status;
2044 
2045  if (event->type == PJSIP_EVENT_TSX_STATE && event->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
2046  pjsip_rx_data *rdata;
2047 
2048  rdata = event->body.tsx_state.src.rdata;
2049  msg = rdata->msg_info.msg;
2050 
2051  if (!pjsip_method_cmp(&msg->line.req.method, pjsip_get_notify_method())) {
2052  body = msg->body;
2053  if (body && !pj_stricmp2(&body->content_type.type, "message")
2054  && !pj_stricmp2(&body->content_type.subtype, "sipfrag")) {
2055  pjsip_parse_status_line((char *)body->data, body->len, &status_line);
2056  }
2057  }
2058  } else {
2059  status_line.code = 500;
2060  status_line.reason = *pjsip_get_status_text(500);
2061  }
2062 
2063  is_last = (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED);
2064  /* If the status code is >= 200, the subscription is finished. */
2065  if (status_line.code >= 200 || is_last) {
2066  res = -1;
2067 
2068  /* If the subscription has terminated, return AST_TRANSFER_SUCCESS for 2XX.
2069  * Any other status code returns AST_TRANSFER_FAILED.
2070  * The subscription should not terminate for any code < 200,
2071  * but if it does, that constitutes a failure. */
2072  if (status_line.code < 200 || status_line.code >= 300) {
2073  message = AST_TRANSFER_FAILED;
2074  }
2075  /* If subscription not terminated and subscription is finished (status code >= 200)
2076  * terminate it */
2077  if (!is_last) {
2078  pjsip_tx_data *tdata;
2079 
2080  status = pjsip_evsub_initiate(sub, pjsip_get_subscribe_method(), 0, &tdata);
2081  if (status == PJ_SUCCESS) {
2082  pjsip_evsub_send_request(sub, tdata);
2083  }
2084  }
2085  /* Finished. Remove session from subscription */
2086  pjsip_evsub_set_mod_data(sub, refer_callback_module.id, NULL);
2087  ast_debug(3, "Transfer channel %s completed: %d %.*s (%s)\n",
2088  ast_channel_name(chan),
2089  status_line.code,
2090  (int)status_line.reason.slen, status_line.reason.ptr,
2091  (message == AST_TRANSFER_SUCCESS) ? "Success" : "Failure");
2092  }
2093  }
2094 
2095  if (res) {
2096  ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
2097  }
2098 }
2099 
2100 static void transfer_refer(struct ast_sip_session *session, const char *target)
2101 {
2102  pjsip_evsub *sub;
2104  pj_str_t tmp;
2105  pjsip_tx_data *packet;
2106  const char *ref_by_val;
2107  char local_info[pj_strlen(&session->inv_session->dlg->local.info_str) + 1];
2108  struct pjsip_evsub_user xfer_cb;
2109 
2110  pj_bzero(&xfer_cb, sizeof(xfer_cb));
2111  xfer_cb.on_evsub_state = &xfer_client_on_evsub_state;
2112 
2113  if (pjsip_xfer_create_uac(session->inv_session->dlg, &xfer_cb, &sub) != PJ_SUCCESS) {
2114  message = AST_TRANSFER_FAILED;
2115  ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
2116 
2117  return;
2118  }
2119 
2120  pjsip_evsub_set_mod_data(sub, refer_callback_module.id, session);
2121 
2122  if (pjsip_xfer_initiate(sub, pj_cstr(&tmp, target), &packet) != PJ_SUCCESS) {
2123  message = AST_TRANSFER_FAILED;
2124  ast_queue_control_data(session->channel, AST_CONTROL_TRANSFER, &message, sizeof(message));
2125  pjsip_evsub_terminate(sub, PJ_FALSE);
2126 
2127  return;
2128  }
2129 
2130  ref_by_val = pbx_builtin_getvar_helper(session->channel, "SIPREFERREDBYHDR");
2131  if (!ast_strlen_zero(ref_by_val)) {
2132  ast_sip_add_header(packet, "Referred-By", ref_by_val);
2133  } else {
2134  ast_copy_pj_str(local_info, &session->inv_session->dlg->local.info_str, sizeof(local_info));
2135  ast_sip_add_header(packet, "Referred-By", local_info);
2136  }
2137 
2138  pjsip_xfer_send_request(sub, packet);
2139 }
2140 
2141 static int transfer(void *data)
2142 {
2143  struct transfer_data *trnf_data = data;
2144  struct ast_sip_endpoint *endpoint = NULL;
2145  struct ast_sip_contact *contact = NULL;
2146  const char *target = trnf_data->target;
2147 
2148  if (trnf_data->session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
2149  ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
2150  trnf_data->session->inv_session->cause,
2151  pjsip_get_status_text(trnf_data->session->inv_session->cause)->ptr);
2152  } else {
2153  /* See if we have an endpoint; if so, use its contact */
2154  endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", target);
2155  if (endpoint) {
2157  if (contact && !ast_strlen_zero(contact->uri)) {
2158  target = contact->uri;
2159  }
2160  }
2161 
2162  if (ast_channel_state(trnf_data->session->channel) == AST_STATE_RING) {
2163  transfer_redirect(trnf_data->session, target);
2164  } else {
2165  transfer_refer(trnf_data->session, target);
2166  }
2167  }
2168 
2169 #ifdef HAVE_PJSIP_INV_SESSION_REF
2170  pjsip_inv_dec_ref(trnf_data->session->inv_session);
2171 #endif
2172 
2173  ao2_ref(trnf_data, -1);
2174  ao2_cleanup(endpoint);
2175  ao2_cleanup(contact);
2176  return 0;
2177 }
2178 
2179 /*! \brief Function called by core for Asterisk initiated transfer */
2180 static int chan_pjsip_transfer(struct ast_channel *chan, const char *target)
2181 {
2183  struct transfer_data *trnf_data = transfer_data_alloc(channel->session, target);
2184 
2185  if (!trnf_data) {
2186  return -1;
2187  }
2188 
2189 #ifdef HAVE_PJSIP_INV_SESSION_REF
2190  if (pjsip_inv_add_ref(trnf_data->session->inv_session) != PJ_SUCCESS) {
2191  ast_log(LOG_ERROR, "Can't increase the session reference counter\n");
2192  ao2_cleanup(trnf_data);
2193  return -1;
2194  }
2195 #endif
2196 
2197  if (ast_sip_push_task(channel->session->serializer, transfer, trnf_data)) {
2198  ast_log(LOG_WARNING, "Error requesting transfer\n");
2199 #ifdef HAVE_PJSIP_INV_SESSION_REF
2200  pjsip_inv_dec_ref(trnf_data->session->inv_session);
2201 #endif
2202  ao2_cleanup(trnf_data);
2203  return -1;
2204  }
2205 
2206  return 0;
2207 }
2208 
2209 /*! \brief Function called by core to start a DTMF digit */
2211 {
2213  struct ast_sip_session_media *media;
2214 
2216 
2217  switch (channel->session->dtmf) {
2218  case AST_SIP_DTMF_RFC_4733:
2219  if (!media || !media->rtp) {
2220  return 0;
2221  }
2222 
2223  ast_rtp_instance_dtmf_begin(media->rtp, digit);
2224  break;
2225  case AST_SIP_DTMF_AUTO:
2226  if (!media || !media->rtp) {
2227  return 0;
2228  }
2229 
2231  return -1;
2232  }
2233 
2234  ast_rtp_instance_dtmf_begin(media->rtp, digit);
2235  break;
2237  if (!media || !media->rtp || (ast_rtp_instance_dtmf_mode_get(media->rtp) == AST_RTP_DTMF_MODE_NONE)) {
2238  return 0;
2239  }
2240  ast_rtp_instance_dtmf_begin(media->rtp, digit);
2241  break;
2242  case AST_SIP_DTMF_NONE:
2243  break;
2244  case AST_SIP_DTMF_INBAND:
2245  return -1;
2246  default:
2247  break;
2248  }
2249 
2250  return 0;
2251 }
2252 
2255  char digit;
2256  unsigned int duration;
2257 };
2258 
2259 static void info_dtmf_data_destroy(void *obj)
2260 {
2261  struct info_dtmf_data *dtmf_data = obj;
2262  ao2_ref(dtmf_data->session, -1);
2263 }
2264 
2265 static struct info_dtmf_data *info_dtmf_data_alloc(struct ast_sip_session *session, char digit, unsigned int duration)
2266 {
2267  struct info_dtmf_data *dtmf_data = ao2_alloc(sizeof(*dtmf_data), info_dtmf_data_destroy);
2268  if (!dtmf_data) {
2269  return NULL;
2270  }
2271  ao2_ref(session, +1);
2272  dtmf_data->session = session;
2273  dtmf_data->digit = digit;
2274  dtmf_data->duration = duration;
2275  return dtmf_data;
2276 }
2277 
2278 static int transmit_info_dtmf(void *data)
2279 {
2280  RAII_VAR(struct info_dtmf_data *, dtmf_data, data, ao2_cleanup);
2281 
2282  struct ast_sip_session *session = dtmf_data->session;
2283  struct pjsip_tx_data *tdata;
2284 
2285  RAII_VAR(struct ast_str *, body_text, NULL, ast_free_ptr);
2286 
2287  struct ast_sip_body body = {
2288  .type = "application",
2289  .subtype = "dtmf-relay",
2290  };
2291 
2292  if (session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
2293  ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
2294  session->inv_session->cause,
2295  pjsip_get_status_text(session->inv_session->cause)->ptr);
2296  goto failure;
2297  }
2298 
2299  if (!(body_text = ast_str_create(32))) {
2300  ast_log(LOG_ERROR, "Could not allocate buffer for INFO DTMF.\n");
2301  goto failure;
2302  }
2303  ast_str_set(&body_text, 0, "Signal=%c\r\nDuration=%u\r\n", dtmf_data->digit, dtmf_data->duration);
2304 
2306 
2307  if (ast_sip_create_request("INFO", session->inv_session->dlg, session->endpoint, NULL, NULL, &tdata)) {
2308  ast_log(LOG_ERROR, "Could not create DTMF INFO request\n");
2309  goto failure;
2310  }
2311  if (ast_sip_add_body(tdata, &body)) {
2312  ast_log(LOG_ERROR, "Could not add body to DTMF INFO request\n");
2313  pjsip_tx_data_dec_ref(tdata);
2314  goto failure;
2315  }
2316  ast_sip_session_send_request(session, tdata);
2317 
2318 #ifdef HAVE_PJSIP_INV_SESSION_REF
2319  pjsip_inv_dec_ref(session->inv_session);
2320 #endif
2321 
2322  return 0;
2323 
2324 failure:
2325 #ifdef HAVE_PJSIP_INV_SESSION_REF
2326  pjsip_inv_dec_ref(session->inv_session);
2327 #endif
2328  return -1;
2329 
2330 }
2331 
2332 /*! \brief Function called by core to stop a DTMF digit */
2333 static int chan_pjsip_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
2334 {
2336  struct ast_sip_session_media *media;
2337 
2338  if (!channel || !channel->session) {
2339  /* This happens when the channel is hungup while a DTMF digit is playing. See ASTERISK-28086 */
2340  ast_debug(3, "Channel %s disappeared while calling digit_end\n", ast_channel_name(ast));
2341  return -1;
2342  }
2343 
2345 
2346  switch (channel->session->dtmf) {
2348  {
2349  if (!media || !media->rtp) {
2350  return 0;
2351  }
2352 
2354  ast_debug(3, "Told to send end of digit on Auto-Info channel %s RFC4733 negotiated so using it.\n", ast_channel_name(ast));
2355  ast_rtp_instance_dtmf_end_with_duration(media->rtp, digit, duration);
2356  break;
2357  }
2358  /* If RFC_4733 was not negotiated, fail through to the DTMF_INFO processing */
2359  ast_debug(3, "Told to send end of digit on Auto-Info channel %s RFC4733 NOT negotiated using INFO instead.\n", ast_channel_name(ast));
2360  }
2361 
2362  case AST_SIP_DTMF_INFO:
2363  {
2364  struct info_dtmf_data *dtmf_data = info_dtmf_data_alloc(channel->session, digit, duration);
2365 
2366  if (!dtmf_data) {
2367  return -1;
2368  }
2369 
2370 #ifdef HAVE_PJSIP_INV_SESSION_REF
2371  if (pjsip_inv_add_ref(dtmf_data->session->inv_session) != PJ_SUCCESS) {
2372  ast_log(LOG_ERROR, "Can't increase the session reference counter\n");
2373  ao2_cleanup(dtmf_data);
2374  return -1;
2375  }
2376 #endif
2377 
2378  if (ast_sip_push_task(channel->session->serializer, transmit_info_dtmf, dtmf_data)) {
2379  ast_log(LOG_WARNING, "Error sending DTMF via INFO.\n");
2380 #ifdef HAVE_PJSIP_INV_SESSION_REF
2381  pjsip_inv_dec_ref(dtmf_data->session->inv_session);
2382 #endif
2383  ao2_cleanup(dtmf_data);
2384  return -1;
2385  }
2386  break;
2387  }
2388  case AST_SIP_DTMF_RFC_4733:
2389  if (!media || !media->rtp) {
2390  return 0;
2391  }
2392 
2393  ast_rtp_instance_dtmf_end_with_duration(media->rtp, digit, duration);
2394  break;
2395  case AST_SIP_DTMF_AUTO:
2396  if (!media || !media->rtp) {
2397  return 0;
2398  }
2399 
2401  return -1;
2402  }
2403 
2404  ast_rtp_instance_dtmf_end_with_duration(media->rtp, digit, duration);
2405  break;
2406  case AST_SIP_DTMF_NONE:
2407  break;
2408  case AST_SIP_DTMF_INBAND:
2409  return -1;
2410  }
2411 
2412  return 0;
2413 }
2414 
2416 {
2417  struct ast_party_connected_line connected;
2418 
2419  /*
2420  * Use the channel CALLERID() as the initial connected line data.
2421  * The core or a predial handler may have supplied missing values
2422  * from the session->endpoint->id.self about who we are calling.
2423  */
2424  ast_channel_lock(session->channel);
2425  ast_party_id_copy(&session->id, &ast_channel_caller(session->channel)->id);
2426  ast_channel_unlock(session->channel);
2427 
2428  /* Supply initial connected line information if available. */
2429  if (!session->id.number.valid && !session->id.name.valid) {
2430  return;
2431  }
2432 
2433  ast_party_connected_line_init(&connected);
2434  connected.id = session->id;
2436 
2437  ast_channel_queue_connected_line_update(session->channel, &connected, NULL);
2438 }
2439 
2440 static int call(void *data)
2441 {
2442  struct ast_sip_channel_pvt *channel = data;
2443  struct ast_sip_session *session = channel->session;
2444  pjsip_tx_data *tdata;
2445  int res = 0;
2446  SCOPE_ENTER(1, "%s Topology: %s\n",
2447  ast_sip_session_get_name(session),
2449  );
2450 
2451 
2452  res = ast_sip_session_create_invite(session, &tdata);
2453 
2454  if (res) {
2455  ast_set_hangupsource(session->channel, ast_channel_name(session->channel), 0);
2456  ast_queue_hangup(session->channel);
2457  } else {
2460  ast_sip_session_send_request(session, tdata);
2461  }
2462  ao2_ref(channel, -1);
2463  SCOPE_EXIT_RTN_VALUE(res, "RC: %d\n", res);
2464 }
2465 
2466 /*! \brief Function called by core to actually start calling a remote party */
2467 static int chan_pjsip_call(struct ast_channel *ast, const char *dest, int timeout)
2468 {
2470  SCOPE_ENTER(1, "%s Topology: %s\n", ast_sip_session_get_name(channel->session),
2472 
2473  ao2_ref(channel, +1);
2474  if (ast_sip_push_task(channel->session->serializer, call, channel)) {
2475  ast_log(LOG_WARNING, "Error attempting to place outbound call to '%s'\n", dest);
2476  ao2_cleanup(channel);
2477  SCOPE_EXIT_RTN_VALUE(-1, "Couldn't push task\n");
2478  }
2479 
2480  SCOPE_EXIT_RTN_VALUE(0, "'call' task pushed\n");
2481 }
2482 
2483 /*! \brief Internal function which translates from Asterisk cause codes to SIP response codes */
2484 static int hangup_cause2sip(int cause)
2485 {
2486  switch (cause) {
2487  case AST_CAUSE_UNALLOCATED: /* 1 */
2488  case AST_CAUSE_NO_ROUTE_DESTINATION: /* 3 IAX2: Can't find extension in context */
2489  case AST_CAUSE_NO_ROUTE_TRANSIT_NET: /* 2 */
2490  return 404;
2491  case AST_CAUSE_CONGESTION: /* 34 */
2492  case AST_CAUSE_SWITCH_CONGESTION: /* 42 */
2493  return 503;
2494  case AST_CAUSE_NO_USER_RESPONSE: /* 18 */
2495  return 408;
2496  case AST_CAUSE_NO_ANSWER: /* 19 */
2497  case AST_CAUSE_UNREGISTERED: /* 20 */
2498  return 480;
2499  case AST_CAUSE_CALL_REJECTED: /* 21 */
2500  return 403;
2501  case AST_CAUSE_NUMBER_CHANGED: /* 22 */
2502  return 410;
2503  case AST_CAUSE_NORMAL_UNSPECIFIED: /* 31 */
2504  return 480;
2506  return 484;
2507  case AST_CAUSE_USER_BUSY:
2508  return 486;
2509  case AST_CAUSE_FAILURE:
2510  return 500;
2511  case AST_CAUSE_FACILITY_REJECTED: /* 29 */
2512  return 501;
2514  return 503;
2516  return 502;
2517  case AST_CAUSE_BEARERCAPABILITY_NOTAVAIL: /* Can't find codec to connect to host */
2518  return 488;
2519  case AST_CAUSE_INTERWORKING: /* Unspecified Interworking issues */
2520  return 500;
2521  case AST_CAUSE_NOTDEFINED:
2522  default:
2523  ast_debug(1, "AST hangup cause %d (no match found in PJSIP)\n", cause);
2524  return 0;
2525  }
2526 
2527  /* Never reached */
2528  return 0;
2529 }
2530 
2531 struct hangup_data {
2532  int cause;
2534 };
2535 
2536 static void hangup_data_destroy(void *obj)
2537 {
2538  struct hangup_data *h_data = obj;
2539 
2540  h_data->chan = ast_channel_unref(h_data->chan);
2541 }
2542 
2544 {
2545  struct hangup_data *h_data = ao2_alloc(sizeof(*h_data), hangup_data_destroy);
2546 
2547  if (!h_data) {
2548  return NULL;
2549  }
2550 
2551  h_data->cause = cause;
2552  h_data->chan = ast_channel_ref(chan);
2553 
2554  return h_data;
2555 }
2556 
2557 /*! \brief Clear a channel from a session along with its PVT */
2558 static void clear_session_and_channel(struct ast_sip_session *session, struct ast_channel *ast)
2559 {
2560  session->channel = NULL;
2561  set_channel_on_rtp_instance(session, "");
2563 }
2564 
2565 static int hangup(void *data)
2566 {
2567  struct hangup_data *h_data = data;
2568  struct ast_channel *ast = h_data->chan;
2570  SCOPE_ENTER(1, "%s\n", ast_channel_name(ast));
2571 
2572  /*
2573  * Before cleaning we have to ensure that channel or its session is not NULL
2574  * we have seen rare case when taskprocessor calls hangup but channel is NULL
2575  * due to SIP session timeout and answer happening at the same time
2576  */
2577  if (channel) {
2578  struct ast_sip_session *session = channel->session;
2579  if (session) {
2580  int cause = h_data->cause;
2581 
2582  /*
2583  * It's possible that session_terminate might cause the session to be destroyed
2584  * immediately so we need to keep a reference to it so we can NULL session->channel
2585  * afterwards.
2586  */
2587  ast_sip_session_terminate(ao2_bump(session), cause);
2588  clear_session_and_channel(session, ast);
2589  ao2_cleanup(session);
2590  }
2591  ao2_cleanup(channel);
2592  }
2593  ao2_cleanup(h_data);
2595 }
2596 
2597 /*! \brief Function called by core to hang up a PJSIP session */
2598 static int chan_pjsip_hangup(struct ast_channel *ast)
2599 {
2601  int cause;
2602  struct hangup_data *h_data;
2603  SCOPE_ENTER(1, "%s\n", ast_channel_name(ast));
2604 
2605  if (!channel || !channel->session) {
2606  SCOPE_EXIT_RTN_VALUE(-1, "No channel or session\n");
2607  }
2608 
2610  h_data = hangup_data_alloc(cause, ast);
2611 
2612  if (!h_data) {
2613  goto failure;
2614  }
2615 
2616  if (ast_sip_push_task(channel->session->serializer, hangup, h_data)) {
2617  ast_log(LOG_WARNING, "Unable to push hangup task to the threadpool. Expect bad things\n");
2618  goto failure;
2619  }
2620 
2621  SCOPE_EXIT_RTN_VALUE(0, "Cause: %d\n", cause);
2622 
2623 failure:
2624  /* Go ahead and do our cleanup of the session and channel even if we're not going
2625  * to be able to send our SIP request/response
2626  */
2627  clear_session_and_channel(channel->session, ast);
2628  ao2_cleanup(channel);
2629  ao2_cleanup(h_data);
2630 
2631  SCOPE_EXIT_RTN_VALUE(-1, "Cause: %d\n", cause);
2632 }
2633 
2637  const char *dest;
2638  int cause;
2639 };
2640 
2641 static int request(void *obj)
2642 {
2643  struct request_data *req_data = obj;
2644  struct ast_sip_session *session = NULL;
2645  char *tmp = ast_strdupa(req_data->dest), *endpoint_name = NULL, *request_user = NULL;
2646  struct ast_sip_endpoint *endpoint;
2647 
2649  AST_APP_ARG(endpoint);
2650  AST_APP_ARG(aor);
2651  );
2652  SCOPE_ENTER(1, "%s\n",tmp);
2653 
2654  if (ast_strlen_zero(tmp)) {
2655  ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty destination\n");
2657  SCOPE_EXIT_RTN_VALUE(-1, "Empty destination\n");
2658  }
2659 
2660  AST_NONSTANDARD_APP_ARGS(args, tmp, '/');
2661 
2663  /* If a request user has been specified extract it from the endpoint name portion */
2664  if ((endpoint_name = strchr(args.endpoint, '@'))) {
2665  request_user = args.endpoint;
2666  *endpoint_name++ = '\0';
2667  } else {
2668  endpoint_name = args.endpoint;
2669  }
2670 
2671  if (ast_strlen_zero(endpoint_name)) {
2672  if (request_user) {
2673  ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty endpoint name: %s@<endpoint-name>\n",
2674  request_user);
2675  } else {
2676  ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty endpoint name\n");
2677  }
2679  SCOPE_EXIT_RTN_VALUE(-1, "Empty endpoint name\n");
2680  }
2681  endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint",
2682  endpoint_name);
2683  if (!endpoint) {
2684  ast_log(LOG_ERROR, "Unable to create PJSIP channel - endpoint '%s' was not found\n", endpoint_name);
2686  SCOPE_EXIT_RTN_VALUE(-1, "Endpoint not found\n");
2687  }
2688  } else {
2689  /* First try to find an exact endpoint match, for single (user) or multi-domain (user@domain) */
2690  endpoint_name = args.endpoint;
2691  if (ast_strlen_zero(endpoint_name)) {
2692  ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty endpoint name\n");
2694  SCOPE_EXIT_RTN_VALUE(-1, "Empty endpoint name\n");
2695  }
2696  endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint",
2697  endpoint_name);
2698  if (!endpoint) {
2699  /* It seems it's not a multi-domain endpoint or single endpoint exact match,
2700  * it's possible that it's a SIP trunk with a specified user (user@trunkname),
2701  * so extract the user before @ sign.
2702  */
2703  endpoint_name = strchr(args.endpoint, '@');
2704  if (!endpoint_name) {
2705  /*
2706  * Couldn't find an '@' so it had to be an endpoint
2707  * name that doesn't exist.
2708  */
2709  ast_log(LOG_ERROR, "Unable to create PJSIP channel - endpoint '%s' was not found\n",
2710  args.endpoint);
2712  SCOPE_EXIT_RTN_VALUE(-1, "Endpoint not found\n");
2713  }
2714  request_user = args.endpoint;
2715  *endpoint_name++ = '\0';
2716 
2717  if (ast_strlen_zero(endpoint_name)) {
2718  ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty endpoint name: %s@<endpoint-name>\n",
2719  request_user);
2721  SCOPE_EXIT_RTN_VALUE(-1, "Empty endpoint name\n");
2722  }
2723 
2724  endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint",
2725  endpoint_name);
2726  if (!endpoint) {
2727  ast_log(LOG_ERROR, "Unable to create PJSIP channel - endpoint '%s' was not found\n", endpoint_name);
2729  SCOPE_EXIT_RTN_VALUE(-1, "Endpoint not found\n");
2730  }
2731  }
2732  }
2733 
2734  session = ast_sip_session_create_outgoing(endpoint, NULL, args.aor, request_user,
2735  req_data->topology);
2736  ao2_ref(endpoint, -1);
2737  if (!session) {
2738  ast_log(LOG_ERROR, "Failed to create outgoing session to endpoint '%s'\n", endpoint_name);
2740  SCOPE_EXIT_RTN_VALUE(-1, "Couldn't create session\n");
2741  }
2742 
2743  req_data->session = session;
2744 
2746 }
2747 
2748 /*! \brief Function called by core to create a new outgoing PJSIP session */
2749 static struct ast_channel *chan_pjsip_request_with_stream_topology(const char *type, struct ast_stream_topology *topology, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
2750 {
2751  struct request_data req_data;
2752  RAII_VAR(struct ast_sip_session *, session, NULL, ao2_cleanup);
2753  SCOPE_ENTER(1, "%s Topology: %s\n", data,
2754  ast_str_tmp(256, ast_stream_topology_to_str(topology, &STR_TMP)));
2755 
2756  req_data.topology = topology;
2757  req_data.dest = data;
2758  /* Default failure value in case ast_sip_push_task_wait_servant() itself fails. */
2759  req_data.cause = AST_CAUSE_FAILURE;
2760 
2761  if (ast_sip_push_task_wait_servant(NULL, request, &req_data)) {
2762  *cause = req_data.cause;
2763  SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't push task\n");
2764  }
2765 
2766  session = req_data.session;
2767 
2768  if (!(session->channel = chan_pjsip_new(session, AST_STATE_DOWN, NULL, NULL, assignedids, requestor, NULL))) {
2769  /* Session needs to be terminated prematurely */
2770  SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't create channel\n");
2771  }
2772 
2773  SCOPE_EXIT_RTN_VALUE(session->channel, "Channel: %s\n", ast_channel_name(session->channel));
2774 }
2775 
2776 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)
2777 {
2778  struct ast_stream_topology *topology;
2779  struct ast_channel *chan;
2780 
2782  if (!topology) {
2783  return NULL;
2784  }
2785 
2786  chan = chan_pjsip_request_with_stream_topology(type, topology, assignedids, requestor, data, cause);
2787 
2788  ast_stream_topology_free(topology);
2789 
2790  return chan;
2791 }
2792 
2796 };
2797 
2798 static void sendtext_data_destroy(void *obj)
2799 {
2800  struct sendtext_data *data = obj;
2801  ao2_cleanup(data->session);
2802  ast_free(data->msg);
2803 }
2804 
2806  struct ast_msg_data *msg)
2807 {
2809  struct sendtext_data *data = ao2_alloc(sizeof(*data), sendtext_data_destroy);
2810 
2811  if (!data) {
2812  return NULL;
2813  }
2814 
2815  data->msg = ast_msg_data_dup(msg);
2816  if (!data->msg) {
2817  ao2_cleanup(data);
2818  return NULL;
2819  }
2820  data->session = channel->session;
2821  ao2_ref(data->session, +1);
2822 
2823  return data;
2824 }
2825 
2826 static int sendtext(void *obj)
2827 {
2828  struct sendtext_data *data = obj;
2829  pjsip_tx_data *tdata;
2830  const char *body_text = ast_msg_data_get_attribute(data->msg, AST_MSG_DATA_ATTR_BODY);
2831  const char *content_type = ast_msg_data_get_attribute(data->msg, AST_MSG_DATA_ATTR_CONTENT_TYPE);
2832  char *sep;
2833  struct ast_sip_body body = {
2834  .type = "text",
2835  .subtype = "plain",
2836  .body_text = body_text,
2837  };
2838 
2839  if (!ast_strlen_zero(content_type)) {
2840  sep = strchr(content_type, '/');
2841  if (sep) {
2842  *sep = '\0';
2843  body.type = content_type;
2844  body.subtype = ++sep;
2845  }
2846  }
2847 
2848  if (data->session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
2849  ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
2850  data->session->inv_session->cause,
2851  pjsip_get_status_text(data->session->inv_session->cause)->ptr);
2852  } else {
2853  pjsip_from_hdr *hdr;
2854  pjsip_name_addr *name_addr;
2855  const char *from = ast_msg_data_get_attribute(data->msg, AST_MSG_DATA_ATTR_FROM);
2856  const char *to = ast_msg_data_get_attribute(data->msg, AST_MSG_DATA_ATTR_TO);
2857  int invalidate_tdata = 0;
2858 
2859  ast_sip_create_request("MESSAGE", data->session->inv_session->dlg, data->session->endpoint, NULL, NULL, &tdata);
2860  ast_sip_add_body(tdata, &body);
2861 
2862  /*
2863  * If we have a 'from' in the msg, set the display name in the From
2864  * header to it.
2865  */
2866  if (!ast_strlen_zero(from)) {
2867  hdr = PJSIP_MSG_FROM_HDR(tdata->msg);
2868  name_addr = (pjsip_name_addr *) hdr->uri;
2869  pj_strdup2(tdata->pool, &name_addr->display, from);
2870  invalidate_tdata = 1;
2871  }
2872 
2873  /*
2874  * If we have a 'to' in the msg, set the display name in the To
2875  * header to it.
2876  */
2877  if (!ast_strlen_zero(to)) {
2878  hdr = PJSIP_MSG_TO_HDR(tdata->msg);
2879  name_addr = (pjsip_name_addr *) hdr->uri;
2880  pj_strdup2(tdata->pool, &name_addr->display, to);
2881  invalidate_tdata = 1;
2882  }
2883 
2884  if (invalidate_tdata) {
2885  pjsip_tx_data_invalidate_msg(tdata);
2886  }
2887 
2888  ast_sip_send_request(tdata, data->session->inv_session->dlg, data->session->endpoint, NULL, NULL);
2889  }
2890 
2891 #ifdef HAVE_PJSIP_INV_SESSION_REF
2892  pjsip_inv_dec_ref(data->session->inv_session);
2893 #endif
2894 
2895  ao2_cleanup(data);
2896 
2897  return 0;
2898 }
2899 
2900 /*! \brief Function called by core to send text on PJSIP session */
2901 static int chan_pjsip_sendtext_data(struct ast_channel *ast, struct ast_msg_data *msg)
2902 {
2904  struct sendtext_data *data = sendtext_data_create(ast, msg);
2905 
2906  ast_debug(1, "Sending MESSAGE from '%s' to '%s:%s': %s\n",
2909  ast_channel_name(ast),
2911 
2912  if (!data) {
2913  return -1;
2914  }
2915 
2916 #ifdef HAVE_PJSIP_INV_SESSION_REF
2917  if (pjsip_inv_add_ref(data->session->inv_session) != PJ_SUCCESS) {
2918  ast_log(LOG_ERROR, "Can't increase the session reference counter\n");
2919  ao2_ref(data, -1);
2920  return -1;
2921  }
2922 #endif
2923 
2924  if (ast_sip_push_task(channel->session->serializer, sendtext, data)) {
2925 #ifdef HAVE_PJSIP_INV_SESSION_REF
2926  pjsip_inv_dec_ref(data->session->inv_session);
2927 #endif
2928  ao2_ref(data, -1);
2929  return -1;
2930  }
2931  return 0;
2932 }
2933 
2934 static int chan_pjsip_sendtext(struct ast_channel *ast, const char *text)
2935 {
2936  struct ast_msg_data *msg;
2937  int rc;
2938  struct ast_msg_data_attribute attrs[] =
2939  {
2940  {
2942  .value = (char *)text,
2943  }
2944  };
2945 
2947  if (!msg) {
2948  return -1;
2949  }
2950  rc = chan_pjsip_sendtext_data(ast, msg);
2951  ast_free(msg);
2952 
2953  return rc;
2954 }
2955 
2956 /*! \brief Convert SIP hangup causes to Asterisk hangup causes */
2957 static int hangup_sip2cause(int cause)
2958 {
2959  /* Possible values taken from causes.h */
2960 
2961  switch(cause) {
2962  case 401: /* Unauthorized */
2963  return AST_CAUSE_CALL_REJECTED;
2964  case 403: /* Not found */
2965  return AST_CAUSE_CALL_REJECTED;
2966  case 404: /* Not found */
2967  return AST_CAUSE_UNALLOCATED;
2968  case 405: /* Method not allowed */
2969  return AST_CAUSE_INTERWORKING;
2970  case 407: /* Proxy authentication required */
2971  return AST_CAUSE_CALL_REJECTED;
2972  case 408: /* No reaction */
2974  case 409: /* Conflict */
2976  case 410: /* Gone */
2977  return AST_CAUSE_NUMBER_CHANGED;
2978  case 411: /* Length required */
2979  return AST_CAUSE_INTERWORKING;
2980  case 413: /* Request entity too large */
2981  return AST_CAUSE_INTERWORKING;
2982  case 414: /* Request URI too large */
2983  return AST_CAUSE_INTERWORKING;
2984  case 415: /* Unsupported media type */
2985  return AST_CAUSE_INTERWORKING;
2986  case 420: /* Bad extension */
2988  case 480: /* No answer */
2989  return AST_CAUSE_NO_ANSWER;
2990  case 481: /* No answer */
2991  return AST_CAUSE_INTERWORKING;
2992  case 482: /* Loop detected */
2993  return AST_CAUSE_INTERWORKING;
2994  case 483: /* Too many hops */
2995  return AST_CAUSE_NO_ANSWER;
2996  case 484: /* Address incomplete */
2998  case 485: /* Ambiguous */
2999  return AST_CAUSE_UNALLOCATED;
3000  case 486: /* Busy everywhere */
3001  return AST_CAUSE_BUSY;
3002  case 487: /* Request terminated */
3003  return AST_CAUSE_INTERWORKING;
3004  case 488: /* No codecs approved */
3006  case 491: /* Request pending */
3007  return AST_CAUSE_INTERWORKING;
3008  case 493: /* Undecipherable */
3009  return AST_CAUSE_INTERWORKING;
3010  case 500: /* Server internal failure */
3011  return AST_CAUSE_FAILURE;
3012  case 501: /* Call rejected */
3014  case 502:
3016  case 503: /* Service unavailable */
3017  return AST_CAUSE_CONGESTION;
3018  case 504: /* Gateway timeout */
3020  case 505: /* SIP version not supported */
3021  return AST_CAUSE_INTERWORKING;
3022  case 600: /* Busy everywhere */
3023  return AST_CAUSE_USER_BUSY;
3024  case 603: /* Decline */
3025  return AST_CAUSE_CALL_REJECTED;
3026  case 604: /* Does not exist anywhere */
3027  return AST_CAUSE_UNALLOCATED;
3028  case 606: /* Not acceptable */
3030  default:
3031  if (cause < 500 && cause >= 400) {
3032  /* 4xx class error that is unknown - someting wrong with our request */
3033  return AST_CAUSE_INTERWORKING;
3034  } else if (cause < 600 && cause >= 500) {
3035  /* 5xx class error - problem in the remote end */
3036  return AST_CAUSE_CONGESTION;
3037  } else if (cause < 700 && cause >= 600) {
3038  /* 6xx - global errors in the 4xx class */
3039  return AST_CAUSE_INTERWORKING;
3040  }
3041  return AST_CAUSE_NORMAL;
3042  }
3043  /* Never reached */
3044  return 0;
3045 }
3046 
3047 static void chan_pjsip_session_begin(struct ast_sip_session *session)
3048 {
3049  RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
3050  SCOPE_ENTER(1, "%s\n", ast_sip_session_get_name(session));
3051 
3052  if (session->endpoint->media.direct_media.glare_mitigation ==
3054  SCOPE_EXIT_RTN("Direct media no glare mitigation\n");
3055  }
3056 
3057  datastore = ast_sip_session_alloc_datastore(&direct_media_mitigation_info,
3058  "direct_media_glare_mitigation");
3059 
3060  if (!datastore) {
3061  SCOPE_EXIT_RTN("Couldn't create datastore\n");
3062  }
3063 
3064  ast_sip_session_add_datastore(session, datastore);
3065  SCOPE_EXIT_RTN();
3066 }
3067 
3068 /*! \brief Function called when the session ends */
3069 static void chan_pjsip_session_end(struct ast_sip_session *session)
3070 {
3071  SCOPE_ENTER(1, "%s\n", ast_sip_session_get_name(session));
3072 
3073  if (!session->channel) {
3074  SCOPE_EXIT_RTN("No channel\n");
3075  }
3076 
3078 
3079  ast_set_hangupsource(session->channel, ast_channel_name(session->channel), 0);
3080  if (!ast_channel_hangupcause(session->channel) && session->inv_session) {
3081  int cause = hangup_sip2cause(session->inv_session->cause);
3082 
3083  ast_queue_hangup_with_cause(session->channel, cause);
3084  } else {
3085  ast_queue_hangup(session->channel);
3086  }
3087 
3088  SCOPE_EXIT_RTN();
3089 }
3090 
3091 /*! \brief Function called when a request is received on the session */
3092 static int chan_pjsip_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
3093 {
3094  RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
3095  struct transport_info_data *transport_data;
3096  pjsip_tx_data *packet = NULL;
3097  SCOPE_ENTER(3, "%s\n", ast_sip_session_get_name(session));
3098 
3099  if (session->channel) {
3100  SCOPE_EXIT_RTN_VALUE(0, "%s: No channel\n", ast_sip_session_get_name(session));
3101  }
3102 
3103  /* Check for a to-tag to determine if this is a reinvite */
3104  if (rdata->msg_info.to->tag.slen) {
3105  /* Weird case. We've received a reinvite but we don't have a channel. The most
3106  * typical case for this happening is that a blind transfer fails, and so the
3107  * transferer attempts to reinvite himself back into the call. We already got
3108  * rid of that channel, and the other side of the call is unrecoverable.
3109  *
3110  * We treat this as a failure, so our best bet is to just hang this call
3111  * up and not create a new channel. Clearing defer_terminate here ensures that
3112  * calling ast_sip_session_terminate() can result in a BYE being sent ASAP.
3113  */
3114  session->defer_terminate = 0;
3115  ast_sip_session_terminate(session, 400);
3116  SCOPE_EXIT_RTN_VALUE(-1, "%s: We have a To tag but no channel. Terminating session\n", ast_sip_session_get_name(session));
3117  }
3118 
3119  datastore = ast_sip_session_alloc_datastore(&transport_info, "transport_info");
3120  if (!datastore) {
3121  SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: Couldn't alloc transport_info datastore\n", ast_sip_session_get_name(session));
3122  }
3123 
3124  transport_data = ast_calloc(1, sizeof(*transport_data));
3125  if (!transport_data) {
3126  SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: Couldn't alloc transport_info\n", ast_sip_session_get_name(session));
3127  }
3128  pj_sockaddr_cp(&transport_data->local_addr, &rdata->tp_info.transport->local_addr);
3129  pj_sockaddr_cp(&transport_data->remote_addr, &rdata->pkt_info.src_addr);
3130  datastore->data = transport_data;
3131  ast_sip_session_add_datastore(session, datastore);
3132 
3133  if (!(session->channel = chan_pjsip_new(session, AST_STATE_RING, session->exten, NULL, NULL, NULL, NULL))) {
3134  if (pjsip_inv_end_session(session->inv_session, 503, NULL, &packet) == PJ_SUCCESS
3135  && packet) {
3136  ast_sip_session_send_response(session, packet);
3137  }
3138 
3139  SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: Failed to allocate new PJSIP channel on incoming SIP INVITE\n",
3140  ast_sip_session_get_name(session));
3141  }
3142  /* channel gets created on incoming request, but we wait to call start
3143  so other supplements have a chance to run */
3144  SCOPE_EXIT_RTN_VALUE(0, "%s\n", ast_sip_session_get_name(session));
3145 }
3146 
3147 static int call_pickup_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
3148 {
3149  struct ast_features_pickup_config *pickup_cfg;
3150  struct ast_channel *chan;
3151 
3152  /* Check for a to-tag to determine if this is a reinvite */
3153  if (rdata->msg_info.to->tag.slen) {
3154  /* We don't care about reinvites */
3155  return 0;
3156  }
3157 
3158  pickup_cfg = ast_get_chan_features_pickup_config(session->channel);
3159  if (!pickup_cfg) {
3160  ast_log(LOG_ERROR, "Unable to retrieve pickup configuration options. Unable to detect call pickup extension.\n");
3161  return 0;
3162  }
3163 
3164  if (strcmp(session->exten, pickup_cfg->pickupexten)) {
3165  ao2_ref(pickup_cfg, -1);
3166  return 0;
3167  }
3168  ao2_ref(pickup_cfg, -1);
3169 
3170  /* We can't directly use session->channel because the pickup operation will cause a masquerade to occur,
3171  * changing the channel pointer in session to a different channel. To ensure we work on the right channel
3172  * we store a pointer locally before we begin and keep a reference so it remains valid no matter what.
3173  */
3174  chan = ast_channel_ref(session->channel);
3175  if (ast_pickup_call(chan)) {
3177  } else {
3179  }
3180  /* A hangup always occurs because the pickup operation will have either failed resulting in the call
3181  * needing to be hung up OR the pickup operation was a success and the channel we now have is actually
3182  * the channel that was replaced, which should be hung up since it is literally in limbo not connected
3183  * to anything at all.
3184  */
3185  ast_hangup(chan);
3186  ast_channel_unref(chan);
3187 
3188  return 1;
3189 }
3190 
3192  .method = "INVITE",
3193  .priority = AST_SIP_SUPPLEMENT_PRIORITY_LAST - 1,
3194  .incoming_request = call_pickup_incoming_request,
3195 };
3196 
3197 static int pbx_start_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
3198 {
3199  int res;
3200  SCOPE_ENTER(1, "%s\n", ast_sip_session_get_name(session));
3201 
3202  /* Check for a to-tag to determine if this is a reinvite */
3203  if (rdata->msg_info.to->tag.slen) {
3204  /* We don't care about reinvites */
3205  SCOPE_EXIT_RTN_VALUE(0, "Reinvite\n");
3206  }
3207 
3208  res = ast_pbx_start(session->channel);
3209 
3210  switch (res) {
3211  case AST_PBX_FAILED:
3212  ast_log(LOG_WARNING, "Failed to start PBX ;(\n");
3214  ast_hangup(session->channel);
3215  break;
3216  case AST_PBX_CALL_LIMIT:
3217  ast_log(LOG_WARNING, "Failed to start PBX (call limit reached) \n");
3219  ast_hangup(session->channel);
3220  break;
3221  case AST_PBX_SUCCESS:
3222  default:
3223  break;
3224  }
3225 
3226  ast_debug(3, "Started PBX on new PJSIP channel %s\n", ast_channel_name(session->channel));
3227 
3228  SCOPE_EXIT_RTN_VALUE((res == AST_PBX_SUCCESS) ? 0 : -1, "RC: %d\n", res);
3229 }
3230 
3232  .method = "INVITE",
3234  .incoming_request = pbx_start_incoming_request,
3235 };
3236 
3237 /*! \brief Function called when a response is received on the session */
3238 static void chan_pjsip_incoming_response(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
3239 {
3240  struct pjsip_status_line status = rdata->msg_info.msg->line.status;
3241  struct ast_control_pvt_cause_code *cause_code;
3242  int data_size = sizeof(*cause_code);
3243  SCOPE_ENTER(3, "%s: Status: %d\n", ast_sip_session_get_name(session), status.code);
3244 
3245  if (!session->channel) {
3246  SCOPE_EXIT_RTN("%s: No channel\n", ast_sip_session_get_name(session));
3247  }
3248 
3249  /* Build and send the tech-specific cause information */
3250  /* size of the string making up the cause code is "SIP " number + " " + reason length */
3251  data_size += 4 + 4 + pj_strlen(&status.reason);
3252  cause_code = ast_alloca(data_size);
3253  memset(cause_code, 0, data_size);
3254 
3256 
3257  snprintf(cause_code->code, data_size - sizeof(*cause_code) + 1, "SIP %d %.*s", status.code,
3258  (int) pj_strlen(&status.reason), pj_strbuf(&status.reason));
3259 
3260  cause_code->ast_cause = hangup_sip2cause(status.code);
3261  ast_queue_control_data(session->channel, AST_CONTROL_PVT_CAUSE_CODE, cause_code, data_size);
3262  ast_channel_hangupcause_hash_set(session->channel, cause_code, data_size);
3263 
3264  switch (status.code) {
3265  case 180:
3266  ast_trace(-1, "%s: Queueing RINGING\n", ast_sip_session_get_name(session));
3268  ast_channel_lock(session->channel);
3269  if (ast_channel_state(session->channel) != AST_STATE_UP) {
3271  }
3272  ast_channel_unlock(session->channel);
3273  break;
3274  case 183:
3275  ast_trace(-1, "%s: Queueing PROGRESS\n", ast_sip_session_get_name(session));
3276  if (session->endpoint->ignore_183_without_sdp) {
3277  pjsip_rdata_sdp_info *sdp = pjsip_rdata_get_sdp_info(rdata);
3278  if (sdp && sdp->body.ptr) {
3279  ast_trace(1, "%s Method: %.*s Status: %d Queueing PROGRESS with SDP\n", ast_sip_session_get_name(session),
3280  (int)rdata->msg_info.cseq->method.name.slen, rdata->msg_info.cseq->method.name.ptr, status.code);
3282  }
3283  } else {
3284  ast_trace(1, "%s Method: %.*s Status: %d Queueing PROGRESS without SDP\n", ast_sip_session_get_name(session),
3285  (int)rdata->msg_info.cseq->method.name.slen, rdata->msg_info.cseq->method.name.ptr, status.code);
3287  }
3288  break;
3289  case 200:
3290  ast_trace(-1, "%s: Queueing ANSWER\n", ast_sip_session_get_name(session));
3292  break;
3293  default:
3294  ast_trace(-1, "%s: Not queueing anything\n", ast_sip_session_get_name(session));
3295  break;
3296  }
3297 
3298  SCOPE_EXIT_RTN("%s\n", ast_sip_session_get_name(session));
3299 }
3300 
3301 static int chan_pjsip_incoming_ack(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
3302 {
3303  SCOPE_ENTER(3, "%s\n", ast_sip_session_get_name(session));
3304 
3305  if (rdata->msg_info.msg->line.req.method.id == PJSIP_ACK_METHOD) {
3306  if (session->endpoint->media.direct_media.enabled && session->channel) {
3307  ast_trace(-1, "%s: Queueing SRCCHANGE\n", ast_sip_session_get_name(session));
3309  }
3310  }
3311  SCOPE_EXIT_RTN_VALUE(0, "%s\n", ast_sip_session_get_name(session));
3312 }
3313 
3314 static int update_devstate(void *obj, void *arg, int flags)
3315 {
3317  "PJSIP/%s", ast_sorcery_object_get_id(obj));
3318  return 0;
3319 }
3320 
3322  .name = "PJSIP_DIAL_CONTACTS",
3324 };
3325 
3327  .name = "PJSIP_PARSE_URI",
3328  .read = pjsip_acf_parse_uri_read,
3329 };
3330 
3332  .name = "PJSIP_MEDIA_OFFER",
3335 };
3336 
3338  .name = "PJSIP_DTMF_MODE",
3339  .read = pjsip_acf_dtmf_mode_read,
3340  .write = pjsip_acf_dtmf_mode_write
3341 };
3342 
3344  .name = "PJSIP_MOH_PASSTHROUGH",
3347 };
3348 
3350  .name = "PJSIP_SEND_SESSION_REFRESH",
3352 };
3353 
3354 /*!
3355  * \brief Load the module
3356  *
3357  * Module loading including tests for configuration or dependencies.
3358  * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
3359  * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
3360  * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
3361  * configuration file or other non-critical problem return
3362  * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
3363  */
3364 static int load_module(void)
3365 {
3366  struct ao2_container *endpoints;
3367 
3369  return AST_MODULE_LOAD_DECLINE;
3370  }
3371 
3373 
3374  ast_rtp_glue_register(&chan_pjsip_rtp_glue);
3375 
3376  if (ast_channel_register(&chan_pjsip_tech)) {
3377  ast_log(LOG_ERROR, "Unable to register channel class %s\n", channel_type);
3378  goto end;
3379  }
3380 
3381  if (ast_custom_function_register(&chan_pjsip_dial_contacts_function)) {
3382  ast_log(LOG_ERROR, "Unable to register PJSIP_DIAL_CONTACTS dialplan function\n");
3383  goto end;
3384  }
3385 
3386  if (ast_custom_function_register(&chan_pjsip_parse_uri_function)) {
3387  ast_log(LOG_ERROR, "Unable to register PJSIP_PARSE_URI dialplan function\n");
3388  goto end;
3389  }
3390 
3391  if (ast_custom_function_register(&media_offer_function)) {
3392  ast_log(LOG_WARNING, "Unable to register PJSIP_MEDIA_OFFER dialplan function\n");
3393  goto end;
3394  }
3395 
3396  if (ast_custom_function_register(&dtmf_mode_function)) {
3397  ast_log(LOG_WARNING, "Unable to register PJSIP_DTMF_MODE dialplan function\n");
3398  goto end;
3399  }
3400 
3401  if (ast_custom_function_register(&moh_passthrough_function)) {
3402  ast_log(LOG_WARNING, "Unable to register PJSIP_MOH_PASSTHROUGH dialplan function\n");
3403  goto end;
3404  }
3405 
3406  if (ast_custom_function_register(&session_refresh_function)) {
3407  ast_log(LOG_WARNING, "Unable to register PJSIP_SEND_SESSION_REFRESH dialplan function\n");
3408  goto end;
3409  }
3410 
3412 
3413  ast_sip_session_register_supplement(&chan_pjsip_supplement);
3414  ast_sip_session_register_supplement(&chan_pjsip_supplement_response);
3415 
3416  if (!(pjsip_uids_onhold = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_RWLOCK,
3418  uid_hold_sort_fn, NULL))) {
3419  ast_log(LOG_ERROR, "Unable to create held channels container\n");
3420  goto end;
3421  }
3422 
3423  ast_sip_session_register_supplement(&call_pickup_supplement);
3424  ast_sip_session_register_supplement(&pbx_start_supplement);
3425  ast_sip_session_register_supplement(&chan_pjsip_ack_supplement);
3426 
3428  ast_log(LOG_ERROR, "Unable to register PJSIP Channel CLI\n");
3429  goto end;
3430  }
3431 
3432  /* since endpoints are loaded before the channel driver their device
3433  states get set to 'invalid', so they need to be updated */
3434  if ((endpoints = ast_sip_get_endpoints())) {
3436  ao2_ref(endpoints, -1);
3437  }
3438 
3439  return 0;
3440 
3441 end:
3442  ao2_cleanup(pjsip_uids_onhold);
3443  pjsip_uids_onhold = NULL;
3444  ast_sip_session_unregister_supplement(&chan_pjsip_ack_supplement);
3445  ast_sip_session_unregister_supplement(&pbx_start_supplement);
3446  ast_sip_session_unregister_supplement(&chan_pjsip_supplement_response);
3447  ast_sip_session_unregister_supplement(&chan_pjsip_supplement);
3448  ast_sip_session_unregister_supplement(&call_pickup_supplement);
3450  ast_custom_function_unregister(&dtmf_mode_function);
3451  ast_custom_function_unregister(&moh_passthrough_function);
3452  ast_custom_function_unregister(&media_offer_function);
3453  ast_custom_function_unregister(&chan_pjsip_dial_contacts_function);
3454  ast_custom_function_unregister(&chan_pjsip_parse_uri_function);
3455  ast_custom_function_unregister(&session_refresh_function);
3456  ast_channel_unregister(&chan_pjsip_tech);
3457  ast_rtp_glue_unregister(&chan_pjsip_rtp_glue);
3458 
3459  return AST_MODULE_LOAD_DECLINE;
3460 }
3461 
3462 /*! \brief Unload the PJSIP channel from Asterisk */
3463 static int unload_module(void)
3464 {
3465  ao2_cleanup(pjsip_uids_onhold);
3466  pjsip_uids_onhold = NULL;
3467 
3469 
3470  ast_sip_session_unregister_supplement(&chan_pjsip_supplement_response);
3471  ast_sip_session_unregister_supplement(&chan_pjsip_supplement);
3472  ast_sip_session_unregister_supplement(&pbx_start_supplement);
3473  ast_sip_session_unregister_supplement(&chan_pjsip_ack_supplement);
3474  ast_sip_session_unregister_supplement(&call_pickup_supplement);
3475 
3477 
3478  ast_custom_function_unregister(&dtmf_mode_function);
3479  ast_custom_function_unregister(&moh_passthrough_function);
3480  ast_custom_function_unregister(&media_offer_function);
3481  ast_custom_function_unregister(&chan_pjsip_dial_contacts_function);
3482  ast_custom_function_unregister(&chan_pjsip_parse_uri_function);
3483  ast_custom_function_unregister(&session_refresh_function);
3484 
3485  ast_channel_unregister(&chan_pjsip_tech);
3486  ao2_ref(chan_pjsip_tech.capabilities, -1);
3487  ast_rtp_glue_unregister(&chan_pjsip_rtp_glue);
3488 
3489  return 0;
3490 }
3491 
3493  .support_level = AST_MODULE_SUPPORT_CORE,
3494  .load = load_module,
3495  .unload = unload_module,
3496  .load_pri = AST_MODPRI_CHANNEL_DRIVER,
3497  .requires = "res_pjsip,res_pjsip_session,res_pjsip_pubsub",
3498 );
const char * name
Definition: pbx.h:119
static int remote_send_hold(void *data)
Update local hold state to be held.
Definition: chan_pjsip.c:1537
struct ast_party_caller * ast_channel_caller(struct ast_channel *chan)
const char * type
Definition: datastore.h:32
struct ast_sip_endpoint_pickup_configuration pickup
Definition: res_pjsip.h:848
struct ast_variable * next
struct ast_format * ast_format_vp8
Built-in cached vp8 format.
Definition: format_cache.c:196
static int remote_send_hold_refresh(struct ast_sip_session *session, unsigned int held)
Update local hold state and send a re-INVITE with the new SDP.
Definition: chan_pjsip.c:1527
#define AST_THREADSTORAGE(name)
Define a thread storage variable.
Definition: threadstorage.h:84
static int direct_media_mitigate_glare(struct ast_sip_session *session)
Definition: chan_pjsip.c:266
int pjsip_acf_media_offer_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
PJSIP_MEDIA_OFFER function read callback.
int ast_queue_hangup(struct ast_channel *chan)
Queue a hangup frame.
Definition: channel.c:1146
static struct sendtext_data * sendtext_data_create(struct ast_channel *chan, struct ast_msg_data *msg)
Definition: chan_pjsip.c:2805
#define SCOPE_EXIT_LOG_RTN_VALUE(__value, __log_level,...)
Definition: logger.h:927
struct ast_sip_session * session
Definition: chan_pjsip.c:1908
static struct ast_custom_function session_refresh_function
Definition: chan_pjsip.c:3349
static const char type[]
Definition: chan_ooh323.c:109
void ast_sip_session_send_request(struct ast_sip_session *session, pjsip_tx_data *tdata)
Send a SIP request.
Information needed to identify an endpoint in a call.
Definition: channel.h:339
enum sip_cc_notify_state state
Definition: chan_sip.c:957
Tone Indication Support.
const char * body_text
Definition: res_pjsip.h:1971
static int chan_pjsip_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
Function called when a request is received on the session.
Definition: chan_pjsip.c:3092
void ast_party_connected_line_init(struct ast_party_connected_line *init)
Initialize the given connected line structure.
Definition: channel.c:2008
static unsigned int chan_idx
Definition: chan_pjsip.c:80
void ast_channel_pickupgroup_set(struct ast_channel *chan, ast_group_t value)
enum ast_sip_session_t38state t38state
char digit
#define ast_channel_lock(chan)
Definition: channel.h:2890
#define T38_ENABLED
Definition: chan_ooh323.c:102
static int indicate(void *data)
Definition: chan_pjsip.c:1349
struct ast_frame * ast_dsp_process(struct ast_channel *chan, struct ast_dsp *dsp, struct ast_frame *inf)
Return AST_FRAME_NULL frames when there is silence, AST_FRAME_BUSY on busies, and call progress...
Definition: dsp.c:1484
static char exten[AST_MAX_EXTENSION]
Definition: chan_alsa.c:118
Main Channel structure associated with a channel.
struct ast_sip_endpoint * endpoint
Music on hold handling.
pj_sockaddr local_addr
Our address that received the request.
Definition: chan_pjsip.h:34
struct ast_sip_session_media_state * ast_sip_session_media_state_alloc(void)
Allocate a session media state structure.
ast_device_state
Device States.
Definition: devicestate.h:52
static int chan_pjsip_queryoption(struct ast_channel *ast, int option, void *data, int *datalen)
Function called to query options on a channel.
Definition: chan_pjsip.c:1253
char * str
Subscriber phone number (Malloced)
Definition: channel.h:292
void ast_sip_session_media_state_free(struct ast_sip_session_media_state *media_state)
Free a session media state structure.
const char *const type
Definition: channel.h:630
struct ast_channel_snapshot_base * base
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
Structure which contains media state information (streams, sessions)
static int is_colp_update_allowed(struct ast_sip_session *session)
Definition: chan_pjsip.c:1432
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
struct ast_features_pickup_config * ast_get_chan_features_pickup_config(struct ast_channel *chan)
Get the pickup configuration options for a channel.
char * str
Subscriber phone number (Malloced)
Definition: channel.h:387
char chan_name[AST_CHANNEL_NAME]
int ast_queue_control(struct ast_channel *chan, enum ast_control_frame_type control)
Queue a control frame without payload.
Definition: channel.c:1227
unsigned int locally_held
Stream is on hold by local side.
static int is_compatible_format(struct ast_sip_session *session, struct ast_frame *f)
Determine if the given frame is in a format we&#39;ve negotiated.
Definition: chan_pjsip.c:840
void * ast_threadstorage_get(struct ast_threadstorage *ts, size_t init_size)
Retrieve thread storage.
static int transfer(void *data)
Definition: chan_pjsip.c:2141
static void chan_pjsip_pvt_dtor(void *obj)
Definition: chan_pjsip.c:82
void ast_rtp_instance_set_channel_id(struct ast_rtp_instance *instance, const char *uniqueid)
Set the channel that owns this RTP instance.
Definition: rtp_engine.c:553
struct ast_sip_session_media_state * pending_media_state
#define ast_channel_alloc_with_endpoint(needqueue, state, cid_num, cid_name, acctcode, exten, context, assignedids, requestor, amaflag, endpoint,...)
Definition: channel.h:1263
struct ast_sockaddr direct_media_addr
Direct media address.
struct ast_rtp_instance * rtp
Definition: chan_pjsip.c:358
#define SCOPE_ENTER_TASK(level, indent,...)
Definition: logger.h:889
char * ast_get_encoded_str(const char *stream, char *result, size_t result_len)
Decode a stream of encoded control or extended ASCII characters.
Definition: main/app.c:2925
static struct ast_custom_function media_offer_function
Definition: chan_pjsip.c:3331
enum ast_msg_data_attribute_type type
Definition: message.h:463
static int transmit_info_with_vidupdate(void *data)
Send SIP INFO with video update request.
Definition: chan_pjsip.c:1370
CallerID (and other GR30) management and generation Includes code and algorithms from the Zapata libr...
void ast_channel_set_writeformat(struct ast_channel *chan, struct ast_format *format)
struct ast_msg_data * ast_msg_data_dup(struct ast_msg_data *msg)
Clone an ast_msg_data structure.
Definition: message.c:1458
void ast_sip_session_unsuspend(struct ast_sip_session *session)
Request the session serializer be unsuspended.
static int remote_send_unhold(void *data)
Update local hold state to be unheld.
Definition: chan_pjsip.c:1543
struct ast_party_id id
Connected party ID.
Definition: channel.h:459
struct ast_format_cap * ast_stream_topology_get_formats(struct ast_stream_topology *topology)
Create a format capabilities structure representing the topology.
Definition: stream.c:930
void * ast_channel_tech_pvt(const struct ast_channel *chan)
#define AST_CAUSE_NORMAL_TEMPORARY_FAILURE
Definition: causes.h:121
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2926
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1105
static struct topology_change_refresh_data * topology_change_refresh_data_alloc(struct ast_sip_session *session, const struct ast_stream_topology *topology)
Definition: chan_pjsip.c:1561
int ast_sip_session_create_invite(struct ast_sip_session *session, pjsip_tx_data **tdata)
Creates an INVITE request.
#define AST_CAUSE_SWITCH_CONGESTION
Definition: causes.h:122
static int exists(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_logic.c:124
static void local_hold_set_state(struct ast_sip_session_media *session_media, unsigned int held)
Callback which changes the value of locally held on the media stream.
Definition: chan_pjsip.c:1519
Support for translation of data formats. translate.c.
void ast_sip_session_terminate(struct ast_sip_session *session, int response)
Terminate a session and, if possible, send the provided response code.
static void hangup_data_destroy(void *obj)
Definition: chan_pjsip.c:2536
struct ast_party_name name
Subscriber name.
Definition: channel.h:341
void ast_channel_unregister(const struct ast_channel_tech *tech)
Unregister a channel technology.
Definition: channel.c:566
#define UNIQUEID_BUFSIZE
Definition: chan_pjsip.c:76
void ast_dsp_free(struct ast_dsp *dsp)
Definition: dsp.c:1744
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
#define AST_CAUSE_UNALLOCATED
Definition: causes.h:97
void ast_channel_hangupcause_set(struct ast_channel *chan, int value)
void ast_sip_session_suspend(struct ast_sip_session *session)
Request and wait for the session serializer to be suspended.
static struct ast_custom_function dtmf_mode_function
Definition: chan_pjsip.c:3337
unsigned int defer_terminate
static struct ast_sip_session_supplement chan_pjsip_supplement_response
SIP session supplement structure just for responses.
Definition: chan_pjsip.c:153
Convenient Signal Processing routines.
static int chan_pjsip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
Function called by core to ask the channel to indicate some sort of condition.
Definition: chan_pjsip.c:1649
const char * ast_endpoint_get_tech(const struct ast_endpoint *endpoint)
Gets the technology of the given endpoint.
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)
Function called to create a new PJSIP Asterisk channel.
Definition: chan_pjsip.c:545
void ast_channel_set_rawwriteformat(struct ast_channel *chan, struct ast_format *format)
Call Pickup API.
static int chan_pjsip_write(struct ast_channel *ast, struct ast_frame *f)
Definition: chan_pjsip.c:1055
#define LOG_WARNING
Definition: logger.h:274
enum ast_pbx_result ast_pbx_start(struct ast_channel *c)
Create a new thread and start the PBX.
Definition: pbx.c:4712
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:714
#define AST_FORMAT_CAP_NAMES_LEN
Definition: format_cap.h:326
struct ast_sip_session * ast_sip_session_create_outgoing(struct ast_sip_endpoint *endpoint, struct ast_sip_contact *contact, const char *location, const char *request_user, struct ast_stream_topology *req_topology)
Create a new outgoing SIP session.
union ast_frame::@257 data
int ast_party_id_presentation(const struct ast_party_id *id)
Determine the overall presentation value for the given party.
Definition: channel.c:1807
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:1716
struct ast_endpoint_snapshot * ast_endpoint_latest_snapshot(const char *tech, const char *resource)
Retrieve the most recent snapshot for the endpoint with the given name.
static int chan_pjsip_call(struct ast_channel *ast, const char *dest, int timeout)
Function called by core to actually start calling a remote party.
Definition: chan_pjsip.c:2467
struct ast_sip_session * session
Definition: chan_pjsip.c:2794
static int timeout
Definition: cdr_mysql.c:86
static int tmp()
Definition: bt_open.c:389
static int unload_module(void)
Unload the PJSIP channel from Asterisk.
Definition: chan_pjsip.c:3463
static int chan_pjsip_add_hold(const char *chan_uid)
Add a channel ID to the list of PJSIP channels on hold.
Definition: chan_pjsip.c:1133
#define AST_CAUSE_FACILITY_REJECTED
Definition: causes.h:116
const char * ast_codec_media_type2str(enum ast_media_type type)
Conversion function to take a media type and turn it into a string.
Definition: codec.c:347
#define AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE
Definition: causes.h:142
static struct transfer_data * transfer_data_alloc(struct ast_sip_session *session, const char *target)
Definition: chan_pjsip.c:1920
unsigned int ignore_183_without_sdp
Definition: res_pjsip.h:898
void ast_party_id_copy(struct ast_party_id *dest, const struct ast_party_id *src)
Copy the source party id information to the destination party id.
Definition: channel.c:1751
Structure for variables, used for configurations and for channel variables.
ast_control_transfer
#define var
Definition: ast_expr2f.c:614
Structure representing a snapshot of channel state.
int ast_rtp_instance_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
Send a frame out over RTP.
Definition: rtp_engine.c:568
static int hangup_cause2sip(int cause)
Internal function which translates from Asterisk cause codes to SIP response codes.
Definition: chan_pjsip.c:2484
#define ast_rtp_glue_register(glue)
Definition: rtp_engine.h:847
unsigned int asymmetric_rtp_codec
Definition: res_pjsip.h:888
#define AST_LIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
Definition: linkedlists.h:438
struct ast_format_cap * codecs
Definition: res_pjsip.h:767
Test Framework API.
static struct indicate_data * indicate_data_alloc(struct ast_sip_session *session, int condition, int response_code, const void *frame_data, size_t datalen)
Definition: chan_pjsip.c:1324
static pjsip_module refer_callback_module
REFER Callback module, used to attach session data structure to subscription.
Definition: chan_pjsip.c:1977
const ast_string_field context
Definition: res_pjsip.h:812
struct ast_sip_session_media * default_session[AST_MEDIA_TYPE_END]
Default media sessions for each type.
enum ast_control_t38 request_response
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)
Definition: chan_pjsip.c:2776
static int chan_pjsip_write_stream(struct ast_channel *ast, int stream_num, struct ast_frame *f)
Definition: chan_pjsip.c:965
Structure to pass both assignedid values to channel drivers.
Definition: channel.h:605
int pjsip_acf_moh_passthrough_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
PJSIP_MOH_PASSTHROUGH function write callback.
Structure for a data store type.
Definition: datastore.h:31
void ast_channel_callgroup_set(struct ast_channel *chan, ast_group_t value)
Structure used to transport a message through the frame core.
Definition: message.c:1369
#define ast_trace(level,...)
Definition: logger.h:876
ast_channel_state
ast_channel states
Definition: channelstate.h:35
ast_sip_session_media_write_cb write_callback
The write callback when writing frames.
char * str
Subscriber name (Malloced)
Definition: channel.h:265
A structure which contains a channel implementation and session.
enum ast_sip_session_refresh_method method
Definition: res_pjsip.h:730
Definition: astman.c:222
struct ast_sip_session * session
Pointer to session.
static struct ast_datastore_info transport_info
Datastore used to store local/remote addresses for the INVITE request that created the PJSIP channel...
Definition: chan_pjsip.c:259
Definition of a media format.
Definition: format.c:43
ast_t38_state
Possible T38 states on channels.
Definition: channel.h:879
static int chan_pjsip_digit_begin(struct ast_channel *ast, char digit)
Function called by core to start a DTMF digit.
Definition: chan_pjsip.c:2210
const ast_string_field uniqueid
unsigned int duration
Definition: chan_pjsip.c:2256
struct ast_sip_session * session
Definition: chan_pjsip.c:1309
void ast_channel_named_pickupgroups_set(struct ast_channel *chan, struct ast_namedgroups *value)
int ast_format_cap_append_by_type(struct ast_format_cap *cap, enum ast_media_type type)
Add all codecs Asterisk knows about for a specific type to the capabilities structure.
Definition: format_cap.c:216
static int check_for_rtp_changes(struct ast_channel *chan, struct ast_rtp_instance *rtp, struct ast_sip_session_media *media, struct ast_sip_session *session)
Definition: chan_pjsip.c:317
static void indicate_data_destroy(void *obj)
Definition: chan_pjsip.c:1316
size_t ast_format_cap_count(const struct ast_format_cap *cap)
Get the number of formats present within the capabilities structure.
Definition: format_cap.c:395
struct ast_stream_topology * ast_stream_topology_create_from_format_cap(struct ast_format_cap *cap)
A helper function that, given a format capabilities structure, creates a topology and separates the m...
Definition: stream.c:848
int ast_channel_get_up_time(struct ast_channel *chan)
Obtain how long it has been since the channel was answered.
Definition: channel.c:2840
struct ast_format * ast_format_cap_get_best_by_type(const struct ast_format_cap *cap, enum ast_media_type type)
Get the most preferred format for a particular media type.
Definition: format_cap.c:417
#define ao2_alloc_options(data_size, destructor_fn, options)
Definition: astobj2.h:406
#define ast_assert(a)
Definition: utils.h:650
struct ast_msg_data * ast_msg_data_alloc(enum ast_msg_data_source_type source, struct ast_msg_data_attribute attributes[], size_t count)
Allocates an ast_msg_data structure.
Definition: message.c:1381
int ast_channel_register(const struct ast_channel_tech *tech)
Register a channel technology (a new channel driver) Called by a channel module to register the kind ...
Definition: channel.c:535
Definition: muted.c:95
char * text
Definition: app_queue.c:1511
#define ast_str_alloca(init_len)
Definition: strings.h:800
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
struct ast_party_dialed::@240 number
Dialed/Called number.
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:243
static int call(void *data)
Definition: chan_pjsip.c:2440
Structure for a data store object.
Definition: datastore.h:68
void ast_copy_pj_str(char *dest, const pj_str_t *src, size_t size)
Copy a pj_str_t into a standard character buffer.
Definition: res_pjsip.c:5093
static int hangup(void *data)
Definition: chan_pjsip.c:2565
Generic File Format Support. Should be included by clients of the file handling routines. File service providers should instead include mod_format.h.
static int pbx_start_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
Definition: chan_pjsip.c:3197
void ast_sip_session_unregister_supplement(struct ast_sip_session_supplement *supplement)
Unregister a an supplement to SIP session processing.
Definition: pjsip_session.c:63
char exten[AST_MAX_EXTENSION]
int pjsip_acf_channel_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
CHANNEL function read callback.
const char * args
struct ast_stream * ast_stream_topology_get_stream(const struct ast_stream_topology *topology, unsigned int position)
Get a specific stream from the topology.
Definition: stream.c:788
#define NULL
Definition: resample.c:96
const char * data
#define AST_CAUSE_NO_USER_RESPONSE
Definition: causes.h:107
Definitions to aid in the use of thread local storage.
char * end
Definition: eagi_proxy.c:73
static struct ast_datastore_info direct_media_mitigation_info
Definition: chan_pjsip.c:264
Out-of-call text message support.
void ast_channel_zone_set(struct ast_channel *chan, struct ast_tone_zone *value)
#define AST_FRAME_DTMF
struct pjsip_inv_session * inv_session
int ast_sip_push_task_wait_servant(struct ast_taskprocessor *serializer, int(*sip_task)(void *), void *task_data)
Push a task to SIP servants and wait for it to complete.
Definition: res_pjsip.c:5057
Transport information stored in transport_info datastore.
Definition: chan_pjsip.h:30
static void transfer_data_destroy(void *obj)
Definition: chan_pjsip.c:1912
#define AST_CAUSE_INVALID_NUMBER_FORMAT
Definition: causes.h:115
static struct ast_custom_function chan_pjsip_dial_contacts_function
Definition: chan_pjsip.c:3321
void ast_moh_stop(struct ast_channel *chan)
Turn off music on hold on a given channel.
Definition: channel.c:7739
void ast_free_ptr(void *ptr)
free() wrapper
Definition: astmm.c:1771
static int uid_hold_hash_fn(const void *obj, const int flags)
Definition: chan_pjsip.c:1082
struct ast_sip_direct_media_configuration direct_media
Definition: res_pjsip.h:763
static struct ast_sip_session_supplement chan_pjsip_ack_supplement
Definition: chan_pjsip.c:162
#define ast_verb(level,...)
Definition: logger.h:455
#define ast_trace_get_indent()
Definition: logger.h:879
A structure describing a SIP session.
struct ast_trans_pvt * ast_channel_readtrans(const struct ast_channel *chan)
const char * type
Definition: rtp_engine.h:722
const char * ast_endpoint_get_resource(const struct ast_endpoint *endpoint)
Gets the resource name of the given endpoint.
static void chan_pjsip_session_end(struct ast_sip_session *session)
Function called when the session ends.
Definition: chan_pjsip.c:3069
int ast_atomic_fetchadd_int(volatile int *p, int v)
Atomically add v to *p and return the previous value of *p.
Definition: lock.h:755
int ast_sip_send_request(pjsip_tx_data *tdata, struct pjsip_dialog *dlg, struct ast_sip_endpoint *endpoint, void *token, void(*callback)(void *token, pjsip_event *e))
General purpose method for sending a SIP request.
Definition: res_pjsip.c:4887
int ast_custom_function_unregister(struct ast_custom_function *acf)
Unregister a custom function.
int ast_sip_add_header(pjsip_tx_data *tdata, const char *name, const char *value)
Add a header to an outbound SIP message.
Definition: res_pjsip.c:4916
static int chan_pjsip_incoming_ack(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
Definition: chan_pjsip.c:3301
const char * pbx_builtin_getvar_helper(struct ast_channel *chan, const char *name)
Return a pointer to the value of the corresponding channel variable.
struct ast_format_cap * direct_media_cap
struct ast_frame_subclass subclass
enum ast_device_state ast_devstate_aggregate_result(struct ast_devstate_aggregate *agg)
Get the aggregate device state result.
Definition: devicestate.c:663
Media Stream API.
unsigned int inband_progress
Definition: res_pjsip.h:860
static void chan_pjsip_remove_hold(const char *chan_uid)
Remove a channel ID from the list of PJSIP channels on hold.
Definition: chan_pjsip.c:1164
static void ast_sockaddr_setnull(struct ast_sockaddr *addr)
Sets address addr to null.
Definition: netsock2.h:140
int ast_queue_hangup_with_cause(struct ast_channel *chan, int cause)
Queue a hangup frame with hangupcause set.
Definition: channel.c:1162
static struct ast_frame * chan_pjsip_cng_tone_detected(struct ast_channel *ast, struct ast_sip_session *session, struct ast_frame *f)
Internal helper function called when CNG tone is detected.
Definition: chan_pjsip.c:781
static int update_connected_line_information(void *data)
Update connected line information.
Definition: chan_pjsip.c:1459
int ast_set_read_format_path(struct ast_channel *chan, struct ast_format *raw_format, struct ast_format *core_format)
Set specific read path on channel.
Definition: channel.c:5474
struct ast_sip_session_media_state * active_media_state
void * ast_sorcery_retrieve_by_id(const struct ast_sorcery *sorcery, const char *type, const char *id)
Retrieve an object using its unique identifier.
Definition: sorcery.c:1850
static int hangup_sip2cause(int cause)
Convert SIP hangup causes to Asterisk hangup causes.
Definition: chan_pjsip.c:2957
struct ast_format * ast_channel_readformat(struct ast_channel *chan)
static void sendtext_data_destroy(void *obj)
Definition: chan_pjsip.c:2798
static int on_topology_change_response(struct ast_sip_session *session, pjsip_rx_data *rdata)
Definition: chan_pjsip.c:1586
enum ast_format_cmp_res ast_format_cap_iscompatible_format(const struct ast_format_cap *cap, const struct ast_format *format)
Find if ast_format is within the capabilities of the ast_format_cap object.
Definition: format_cap.c:583
void ast_channel_tech_set(struct ast_channel *chan, const struct ast_channel_tech *value)
struct ast_sip_endpoint_media_configuration media
Definition: res_pjsip.h:838
Number structure.
Definition: app_followme.c:154
static int ast_sockaddr_isnull(const struct ast_sockaddr *addr)
Checks if the ast_sockaddr is null. "null" in this sense essentially means uninitialized, or having a 0 length.
Definition: netsock2.h:127
void ast_sip_session_remove_datastore(struct ast_sip_session *session, const char *name)
Remove a session datastore from the session.
#define ao2_bump(obj)
Definition: astobj2.h:491
struct ast_dsp * dsp
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1065
struct ast_party_id id
Caller party ID.
Definition: channel.h:421
static int chan_pjsip_transfer(struct ast_channel *ast, const char *target)
Function called by core for Asterisk initiated transfer.
Definition: chan_pjsip.c:2180
#define ast_str_tmp(init_len, __expr)
Definition: strings.h:1136
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:444
#define ast_log
Definition: astobj2.c:42
void ast_channel_set_rawreadformat(struct ast_channel *chan, struct ast_format *format)
#define AST_CAUSE_NO_ROUTE_TRANSIT_NET
Definition: causes.h:98
static void set_channel_on_rtp_instance(const struct ast_sip_session *session, const char *channel_id)
Definition: chan_pjsip.c:492
int ast_dsp_get_features(struct ast_dsp *dsp)
Get features.
Definition: dsp.c:1738
The arg parameter is a partial search key similar to OBJ_SEARCH_KEY.
Definition: astobj2.h:1120
unsigned long indent
Definition: chan_pjsip.c:677
const char * ast_stream_topology_to_str(const struct ast_stream_topology *topology, struct ast_str **buf)
Get a string representing the topology for debugging/display purposes.
Definition: stream.c:936
enum ast_format_cmp_res ast_format_cmp(const struct ast_format *format1, const struct ast_format *format2)
Compare two formats.
Definition: format.c:201
int ast_devstate_changed(enum ast_device_state state, enum ast_devstate_cache cachable, const char *fmt,...)
Tells Asterisk the State for Device is changed.
Definition: devicestate.c:510
static int chan_pjsip_sendtext_data(struct ast_channel *ast, struct ast_msg_data *msg)
Function called by core to send text on PJSIP session.
Definition: chan_pjsip.c:2901
struct ast_channel * chan
Definition: chan_pjsip.c:357
void ast_channel_queue_connected_line_update(struct ast_channel *chan, const struct ast_party_connected_line *connected, const struct ast_set_party_connected_line *update)
Queue a connected line update frame on a channel.
Definition: channel.c:9059
struct ast_msg_data * msg
Definition: chan_pjsip.c:2795
struct ast_namedgroups * named_pickupgroups
Definition: res_pjsip.h:660
General Asterisk PBX channel definitions.
static void transfer_refer(struct ast_sip_session *session, const char *target)
Definition: chan_pjsip.c:2100
#define AST_VECTOR_GET_ADDR(vec, idx)
Get an address of element in a vector.
Definition: vector.h:670
void ast_channel_rings_set(struct ast_channel *chan, int value)
static void chan_pjsip_incoming_response(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
Function called when a response is received on the session.
Definition: chan_pjsip.c:3238
static int chan_pjsip_hangup(struct ast_channel *ast)
Function called by core to hang up a PJSIP session.
Definition: chan_pjsip.c:2598
struct ast_datastore * ast_sip_session_alloc_datastore(const struct ast_datastore_info *info, const char *uid)
Alternative for ast_datastore_alloc()
static int compatible_formats_exist(struct ast_stream_topology *top, struct ast_format_cap *cap)
Determine if a topology is compatible with format capabilities.
Definition: chan_pjsip.c:524
void ast_set_hangupsource(struct ast_channel *chan, const char *source, int force)
Set the source of the hangup in this channel and it&#39;s bridge.
Definition: channel.c:2490
struct ast_datastore * ast_sip_session_get_datastore(struct ast_sip_session *session, const char *name)
Retrieve a session datastore.
int ast_sip_add_body(pjsip_tx_data *tdata, const struct ast_sip_body *body)
Add a body to an outbound SIP message.
Definition: res_pjsip.c:4944
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:851
A set of tones for a given locale.
Definition: indications.h:74
void ast_channel_nativeformats_set(struct ast_channel *chan, struct ast_format_cap *value)
struct ast_trans_pvt * ast_channel_writetrans(const struct ast_channel *chan)
void ast_channel_stage_snapshot_done(struct ast_channel *chan)
Clear flag to indicate channel snapshot is being staged, and publish snapshot.
const char * type
Definition: res_pjsip.h:1967
static int update_devstate(void *obj, void *arg, int flags)
Definition: chan_pjsip.c:3314
PJSIP dialplan functions header file.
static struct ast_mansession session
const ast_string_field language
Definition: res_pjsip.h:824
Channels have this property if they can create jitter; i.e. most VoIP channels.
Definition: channel.h:966
int ast_sip_push_task_wait_serializer(struct ast_taskprocessor *serializer, int(*sip_task)(void *), void *task_data)
Push a task to the serializer and wait for it to complete.
Definition: res_pjsip.c:5071
int pjsip_acf_dtmf_mode_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
PJSIP_DTMF_MODE function read callback.
Data structure associated with a custom dialplan function.
Definition: pbx.h:118
Access Control of various sorts.
struct ast_sip_channel_pvt * ast_sip_channel_pvt_alloc(void *pvt, struct ast_sip_session *session)
Allocate a new SIP channel pvt structure.
int ast_sip_session_refresh(struct ast_sip_session *session, ast_sip_session_request_creation_cb on_request_creation, ast_sip_session_sdp_creation_cb on_sdp_creation, ast_sip_session_response_cb on_response, enum ast_sip_session_refresh_method method, int generate_new_sdp, struct ast_sip_session_media_state *media_state)
Send a reinvite or UPDATE on a session.
void * frame_data
Definition: chan_pjsip.c:1312
void pjsip_channel_cli_unregister(void)
Unregisters the channel cli commands.
Definition: cli_commands.c:484
#define AST_CAUSE_NORMAL_CLEARING
Definition: causes.h:105
#define AST_CAUSE_CHAN_NOT_IMPLEMENTED
Definition: causes.h:131
static void chan_pjsip_session_begin(struct ast_sip_session *session)
SIP session interaction functions.
Definition: chan_pjsip.c:3047
size_t datalen
Definition: chan_pjsip.c:1313
struct ast_sip_session * session
Definition: chan_pjsip.c:2635
struct ast_sip_media_rtp_configuration rtp
Definition: res_pjsip.h:761
static struct ao2_container * endpoints
#define ao2_ref(o, delta)
Definition: astobj2.h:464
void ast_channel_set_readformat(struct ast_channel *chan, struct ast_format *format)
#define S_COR(a, b, c)
returns the equivalent of logic or for strings, with an additional boolean check: second one if not e...
Definition: strings.h:85
const ast_string_field accountcode
Definition: res_pjsip.h:832
char * ast_frame_subclass2str(struct ast_frame *f, char *subclass, size_t slen, char *moreinfo, size_t mlen)
Copy the discription of a frame&#39;s subclass into the provided string.
Definition: main/frame.c:406
#define DSP_FEATURE_FAX_DETECT
Definition: dsp.h:29
ast_sip_session_media_read_cb read_callback
The callback to invoke.
struct ast_sip_session_media * session
The media session.
static struct ast_custom_function moh_passthrough_function
Definition: chan_pjsip.c:3343
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
const char * ast_sorcery_object_get_id(const void *object)
Get the unique identifier of a sorcery object.
Definition: sorcery.c:2309
struct ast_channel * channel
struct ast_namedgroups * named_callgroups
Definition: res_pjsip.h:658
#define ast_format_cap_append(cap, format, framing)
Definition: format_cap.h:103
const char * method
Definition: res_pjsip.c:4186
static int call_pickup_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
Definition: chan_pjsip.c:3147
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:193
enum ast_device_state ast_state_chan2dev(enum ast_channel_state chanstate)
Convert channel state to devicestate.
Definition: devicestate.c:242
static int transmit_info_dtmf(void *data)
Definition: chan_pjsip.c:2278
void ast_channel_set_unbridged_nolock(struct ast_channel *chan, int value)
Variant of ast_channel_set_unbridged. Use this if the channel is already locked prior to calling...
#define AST_CAUSE_NO_ANSWER
Definition: causes.h:108
struct ast_format_cap * cap
Definition: chan_pjsip.c:360
static int send_direct_media_request(void *data)
Definition: chan_pjsip.c:394
Channels have this property if they can accept input with jitter; i.e. most VoIP channels.
Definition: channel.h:961
const struct ast_format_cap * ast_stream_get_formats(const struct ast_stream *stream)
Get the current negotiated formats of a stream.
Definition: stream.c:330
unsigned int ast_sip_get_disable_multi_domain(void)
Retrieve the system setting &#39;disable multi domain&#39;.
Structure which contains read callback information.
An entity with which Asterisk communicates.
Definition: res_pjsip.h:809
int pjsip_acf_media_offer_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
PJSIP_MEDIA_OFFER function write callback.
int ast_exists_extension(struct ast_channel *c, const char *context, const char *exten, int priority, const char *callerid)
Determine whether an extension exists.
Definition: pbx.c:4179
static int answer(void *data)
Definition: chan_pjsip.c:680
ast_rtp_glue_result
Definition: rtp_engine.h:158
enum ast_sip_session_refresh_method refresh_method
Definition: res_pjsip.h:644
void ast_sip_session_send_response(struct ast_sip_session *session, pjsip_tx_data *tdata)
Send a SIP response.
#define ast_format_cap_alloc(flags)
Definition: format_cap.h:52
int ast_sip_register_service(pjsip_module *module)
Register a SIP service in Asterisk.
Definition: res_pjsip.c:3217
static int send_topology_change_refresh(void *data)
Definition: chan_pjsip.c:1612
struct ast_sip_session * session
Definition: chan_pjsip.c:361
#define AST_CAUSE_NOTDEFINED
Definition: causes.h:154
Structure to describe a channel "technology", ie a channel driver See for examples: ...
Definition: channel.h:629
void ast_channel_adsicpe_set(struct ast_channel *chan, enum ast_channel_adsicpe value)
const char * ast_channel_exten(const struct ast_channel *chan)
Core PBX routines and definitions.
static int chan_pjsip_sendtext(struct ast_channel *ast, const char *text)
Definition: chan_pjsip.c:2934
struct ast_sip_contact * ast_sip_location_retrieve_contact_from_aor_list(const char *aor_list)
Retrieve the first bound contact from a list of AORs.
Definition: location.c:304
void ast_format_cap_remove_by_type(struct ast_format_cap *cap, enum ast_media_type type)
Remove all formats matching a specific format type.
Definition: format_cap.c:525
A snapshot of an endpoint&#39;s state.
struct ast_taskprocessor * serializer
#define AST_CAUSE_DESTINATION_OUT_OF_ORDER
Definition: causes.h:114
const ast_string_field zone
Definition: res_pjsip.h:822
#define ast_test_suite_event_notify(s, f,...)
Definition: test.h:196
int pjsip_acf_parse_uri_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
PJSIP_PARSE_URI function read callback.
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:290
int ast_channel_fdno(const struct ast_channel *chan)
const char * ast_channel_uniqueid(const struct ast_channel *chan)
struct ast_sip_endpoint_id_configuration id
Definition: res_pjsip.h:844
void ast_devstate_aggregate_add(struct ast_devstate_aggregate *agg, enum ast_device_state state)
Add a device state to the aggregate device state.
Definition: devicestate.c:636
static int chan_pjsip_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
Function called by core to stop a DTMF digit.
Definition: chan_pjsip.c:2333
#define AST_CAUSE_NORMAL
Definition: causes.h:150
enum ast_sip_session_media_encryption encryption
Definition: res_pjsip.h:708
#define AST_CAUSE_FAILURE
Definition: causes.h:149
int fd
The file descriptor itself.
void ast_channel_stage_snapshot(struct ast_channel *chan)
Set flag to indicate channel snapshot is being staged.
pj_sockaddr remote_addr
The address that sent the request.
Definition: chan_pjsip.h:32
static void transport_info_destroy(void *obj)
Destructor function for transport_info_data.
Definition: chan_pjsip.c:251
struct ast_format * ast_channel_rawreadformat(struct ast_channel *chan)
static int chan_pjsip_get_hold(const char *chan_uid)
Determine whether a channel ID is in the list of PJSIP channels on hold.
Definition: chan_pjsip.c:1177
#define AST_EXTENDED_FDS
Definition: channel.h:196
#define LOG_ERROR
Definition: logger.h:285
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Definition: astobj2.h:1310
struct ast_stream_topology * ast_channel_set_stream_topology(struct ast_channel *chan, struct ast_stream_topology *topology)
Set the topology of streams on a channel.
int ast_pickup_call(struct ast_channel *chan)
Pickup a call.
Definition: pickup.c:200
const char * ast_sip_session_get_name(const struct ast_sip_session *session)
Get the channel or endpoint name associated with the session.
struct ast_channel * chan
Definition: chan_pjsip.c:2533
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:584
Format capabilities structure, holds formats + preference order + etc.
Definition: format_cap.c:54
static int chan_pjsip_devicestate(const char *data)
Function called to get the device state of an endpoint.
Definition: chan_pjsip.c:1190
static struct ast_sip_session_supplement chan_pjsip_supplement
SIP session supplement structure.
Definition: chan_pjsip.c:142
struct ast_party_id ast_channel_connected_effective_id(struct ast_channel *chan)
static int chan_pjsip_answer(struct ast_channel *ast)
Function called by core when we should answer a PJSIP session.
Definition: chan_pjsip.c:731
int ast_sip_push_task(struct ast_taskprocessor *serializer, int(*sip_task)(void *), void *task_data)
Pushes a task to SIP servants.
Definition: res_pjsip.c:4991
#define AST_CAUSE_NORMAL_UNSPECIFIED
Definition: causes.h:118
void ast_channel_named_callgroups_set(struct ast_channel *chan, struct ast_namedgroups *value)
static const char channel_type[]
Definition: chan_pjsip.c:78
#define AST_CAUSE_CHANNEL_UNACCEPTABLE
Definition: causes.h:101
int pjsip_acf_dtmf_mode_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
PJSIP_DTMF_MODE function write callback.
static struct ast_channel * chan_pjsip_request_with_stream_topology(const char *type, struct ast_stream_topology *topology, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
Function called by core to create a new outgoing PJSIP session.
Definition: chan_pjsip.c:2749
#define AST_NONSTANDARD_APP_ARGS(args, parse, sep)
Performs the &#39;nonstandard&#39; argument separation process for an application.
struct ast_tone_zone * ast_get_indication_zone(const char *country)
locate ast_tone_zone
Definition: indications.c:433
static void rtp_direct_media_data_destroy(void *data)
Definition: chan_pjsip.c:364
static void info_dtmf_data_destroy(void *obj)
Definition: chan_pjsip.c:2259
static void xfer_client_on_evsub_state(pjsip_evsub *sub, pjsip_event *event)
Callback function to report status of implicit REFER-NOTIFY subscription.
Definition: chan_pjsip.c:1989
enum ast_sip_dtmf_mode dtmf
const char * ast_msg_data_get_attribute(struct ast_msg_data *msg, enum ast_msg_data_attribute_type attribute_type)
Get attribute from ast_msg_data.
Definition: message.c:1496
The PJSIP channel driver pvt, stored in the ast_sip_channel_pvt data structure.
Definition: chan_pjsip.h:42
Contact associated with an address of record.
Definition: res_pjsip.h:278
const char * subtype
Definition: res_pjsip.h:1969
Connected Line/Party information.
Definition: channel.h:457
int ast_rtp_instance_dtmf_begin(struct ast_rtp_instance *instance, char digit)
Begin sending a DTMF digit.
Definition: rtp_engine.c:2081
struct ast_party_dialed * ast_channel_dialed(struct ast_channel *chan)
struct ast_format * ast_format_vp9
Built-in cached vp9 format.
Definition: format_cache.c:201
int ast_moh_start(struct ast_channel *chan, const char *mclass, const char *interpclass)
Turn on music on hold on a given channel.
Definition: channel.c:7729
static struct info_dtmf_data * info_dtmf_data_alloc(struct ast_sip_session *session, char digit, unsigned int duration)
Definition: chan_pjsip.c:2265
ast_sip_session_refresh_method
Definition: res_pjsip.h:481
struct ast_format * ast_format_h264
Built-in cached h264 format.
Definition: format_cache.c:181
static struct hangup_data * hangup_data_alloc(int cause, struct ast_channel *chan)
Definition: chan_pjsip.c:2543
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:411
struct ast_sip_t38_configuration t38
Definition: res_pjsip.h:765
#define LOG_NOTICE
Definition: logger.h:263
const char * ast_format_cap_get_names(const struct ast_format_cap *cap, struct ast_str **buf)
Get the names of codecs of a set of formats.
Definition: format_cap.c:736
unsigned int faxdetect_timeout
Definition: res_pjsip.h:882
struct ast_format_cap * capabilities
Definition: channel.h:633
#define SCOPE_EXIT_RTN(...)
Definition: logger.h:900
int ast_channel_is_bridged(const struct ast_channel *chan)
Determine if a channel is in a bridge.
Definition: channel.c:10603
int ast_set_write_format_path(struct ast_channel *chan, struct ast_format *core_format, struct ast_format *raw_format)
Set specific write path on channel.
Definition: channel.c:5510
#define ast_strlen_zero(a)
Definition: muted.c:73
struct ast_stream_topology * topology
Definition: chan_pjsip.c:2636
#define ast_channel_unlock(chan)
Definition: channel.h:2891
static int chan_pjsip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
Function called by core to change the underlying owner channel.
Definition: chan_pjsip.c:1061
void ast_dsp_set_features(struct ast_dsp *dsp, int features)
Select feature set.
Definition: dsp.c:1729
#define AST_CAUSE_UNREGISTERED
Definition: causes.h:153
int source
Information about the source of an update.
Definition: channel.h:483
#define ast_free(a)
Definition: astmm.h:182
PJSIP Channel Driver shared data structures.
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
void ast_devstate_aggregate_init(struct ast_devstate_aggregate *agg)
Initialize aggregate device state.
Definition: devicestate.c:630
#define AST_CHANNEL_NAME
Definition: channel.h:172
enum ast_sip_direct_media_glare_mitigation glare_mitigation
Definition: res_pjsip.h:732
int ast_format_cap_identical(const struct ast_format_cap *cap1, const struct ast_format_cap *cap2)
Determine if two capabilities structures are identical.
Definition: format_cap.c:689
const char * dest
Definition: chan_pjsip.c:2637
void ast_hangup(struct ast_channel *chan)
Hang up a channel.
Definition: channel.c:2534
#define AST_CAUSE_NUMBER_CHANGED
Definition: causes.h:111
#define SCOPE_ENTER(level,...)
Definition: logger.h:885
static struct ao2_container * pjsip_uids_onhold
Definition: chan_pjsip.c:1123
int ast_rtp_instance_fd(struct ast_rtp_instance *instance, int rtcp)
Get the file descriptor for an RTP session (or RTCP)
Definition: rtp_engine.c:2192
static void chan_pjsip_get_codec(struct ast_channel *chan, struct ast_format_cap *result)
Function called by RTP engine to get peer capabilities.
Definition: chan_pjsip.c:242
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
enum ast_channel_state state
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756
An API for managing task processing threads that can be shared across modules.
static int request(void *obj)
Definition: chan_pjsip.c:2641
#define AST_CAUSE_INTERWORKING
Definition: causes.h:145
int ast_stream_topology_get_count(const struct ast_stream_topology *topology)
Get the number of streams in a topology.
Definition: stream.c:765
A structure containing SIP session media information.
void ast_channel_set_fd(struct ast_channel *chan, int which, int fd)
Definition: channel.c:2417
static int cdata(void *userdata, int state, const char *cdata, size_t len)
static struct ast_frame * chan_pjsip_read_stream(struct ast_channel *ast)
Function called by core to read any waiting frames.
Definition: chan_pjsip.c:854
static char cid_name[AST_MAX_EXTENSION]
Definition: chan_mgcp.c:165
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
struct ast_format_cap * ast_channel_nativeformats(const struct ast_channel *chan)
A supplement to SIP message processing.
int pbx_builtin_setvar_helper(struct ast_channel *chan, const char *name, const char *value)
Add a variable to the channel variable stack, removing the most recently set value for the same name...
struct ast_stream_topology * ast_stream_topology_clone(const struct ast_stream_topology *topology)
Create a deep clone of an existing stream topology.
Definition: stream.c:667
#define AST_PRES_RESTRICTION
Definition: callerid.h:323
struct ast_frame ast_null_frame
Definition: main/frame.c:79
#define SCOPE_EXIT_RTN_VALUE(__return_value,...)
Definition: logger.h:904
struct ast_format * ast_format_h265
Built-in cached h265 format.
Definition: format_cache.c:186
int pjsip_channel_cli_register(void)
Registers the channel cli commands.
Definition: cli_commands.c:448
struct ast_endpoint * persistent
Definition: res_pjsip.h:862
struct ast_sip_session_media_state * media_state
Definition: chan_pjsip.c:1550
The arg parameter is an object of the same type.
Definition: astobj2.h:1091
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition: vector.h:682
struct ast_sip_session * session
Definition: chan_pjsip.c:2254
static struct ast_custom_function chan_pjsip_parse_uri_function
Definition: chan_pjsip.c:3326
enum ast_media_type type
Media type of this session media.
static struct rtp_direct_media_data * rtp_direct_media_data_create(struct ast_channel *chan, struct ast_rtp_instance *rtp, struct ast_rtp_instance *vrtp, const struct ast_format_cap *cap, struct ast_sip_session *session)
Definition: chan_pjsip.c:375
#define AST_CAUSE_NO_ROUTE_DESTINATION
Definition: causes.h:99
const char * ast_translate_path_to_str(struct ast_trans_pvt *t, struct ast_str **str)
Puts a string representation of the translation path into outbuf.
Definition: translate.c:922
#define ast_channel_ref(c)
Increase channel reference count.
Definition: channel.h:2915
static int load_module(void)
Load the module.
Definition: chan_pjsip.c:3364
struct ast_stream_topology * topology
Definition: res_pjsip.h:769
struct ast_channel_snapshot * ast_channel_snapshot_get_latest(const char *uniqueid)
Obtain the latest ast_channel_snapshot from the Stasis Message Bus API cache. This is an ao2 object...
struct ast_sorcery * ast_sip_get_sorcery(void)
Get a pointer to the SIP sorcery structure.
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
Standard Command Line Interface.
Channels have this property if they implement send_text_data.
Definition: channel.h:976
static struct ast_sip_session_supplement call_pickup_supplement
Definition: chan_pjsip.c:3191
int ast_channel_hangupcause(const struct ast_channel *chan)
struct ast_stream_topology * topology
The media stream topology.
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
static struct ast_threadstorage uniqueid_threadbuf
Definition: chan_pjsip.c:75
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:79
struct ast_rtp_instance * rtp
RTP instance itself.
const char * ast_channel_name(const struct ast_channel *chan)
#define AST_CAUSE_USER_BUSY
Definition: causes.h:106
void ast_sip_unregister_service(pjsip_module *module)
Definition: res_pjsip.c:3233
int ast_setstate(struct ast_channel *chan, enum ast_channel_state)
Change the state of a channel.
Definition: channel.c:7349
int ast_rtp_glue_unregister(struct ast_rtp_glue *glue)
Unregister RTP glue.
Definition: rtp_engine.c:408