Asterisk - The Open Source Telephony Project GIT-master-55f4e6d
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"
53#include "asterisk/causes.h"
55#include "asterisk/dsp.h"
60#include "asterisk/translate.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
78static const char channel_type[] = "PJSIP";
79
80static unsigned int chan_idx;
81
82static void chan_pjsip_pvt_dtor(void *obj)
83{
84}
85
86/*! \brief Asterisk core interaction functions */
87static 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);
91static int chan_pjsip_sendtext_data(struct ast_channel *ast, struct ast_msg_data *msg);
92static int chan_pjsip_sendtext(struct ast_channel *ast, const char *text);
93static int chan_pjsip_digit_begin(struct ast_channel *ast, char digit);
94static int chan_pjsip_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
95static int chan_pjsip_call(struct ast_channel *ast, const char *dest, int timeout);
96static int chan_pjsip_hangup(struct ast_channel *ast);
97static int chan_pjsip_answer(struct ast_channel *ast);
98static struct ast_frame *chan_pjsip_read_stream(struct ast_channel *ast);
99static int chan_pjsip_write(struct ast_channel *ast, struct ast_frame *f);
100static int chan_pjsip_write_stream(struct ast_channel *ast, int stream_num, struct ast_frame *f);
101static int chan_pjsip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
102static int chan_pjsip_transfer(struct ast_channel *ast, const char *target);
103static int chan_pjsip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
104static int chan_pjsip_devicestate(const char *data);
105static int chan_pjsip_queryoption(struct ast_channel *ast, int option, void *data, int *datalen);
106static const char *chan_pjsip_get_uniqueid(struct ast_channel *ast);
107
108/*! \brief PBX interface structure for channel registration */
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 */
138static int chan_pjsip_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
139static void chan_pjsip_incoming_response(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
140static void chan_pjsip_incoming_response_update_cause(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
141
142/*! \brief SIP session supplement structure */
144 .method = "INVITE",
146 .session_begin = chan_pjsip_session_begin,
147 .session_end = chan_pjsip_session_end,
148 .incoming_request = chan_pjsip_incoming_request,
149 .incoming_response = chan_pjsip_incoming_response,
150 /* It is important that this supplement runs after media has been negotiated */
151 .response_priority = AST_SIP_SESSION_AFTER_MEDIA,
152};
153
154/*! \brief SIP session supplement structure just for responses */
156 .method = "INVITE",
160};
161
162static int chan_pjsip_incoming_ack(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
163
165 .method = "ACK",
167 .incoming_request = chan_pjsip_incoming_ack,
168};
169
170static int chan_pjsip_incoming_prack(struct ast_sip_session *session, struct pjsip_rx_data *rdata);
171
173 .method = "PRACK",
175 .incoming_request = chan_pjsip_incoming_prack,
176};
177
178/*! \brief Function called by RTP engine to get local audio RTP peer */
180{
181 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
182 struct ast_sip_endpoint *endpoint;
183 struct ast_datastore *datastore;
184 struct ast_sip_session_media *media;
185
186 if (!channel || !channel->session) {
188 }
189
190 /* XXX Getting the first RTP instance for direct media related stuff seems just
191 * absolutely wrong. But the native RTP bridge knows no other method than single-stream
192 * for direct media. So this is the best we can do.
193 */
195 if (!media || !media->rtp) {
197 }
198
199 datastore = ast_sip_session_get_datastore(channel->session, "t38");
200 if (datastore) {
201 ao2_ref(datastore, -1);
203 }
204
205 endpoint = channel->session->endpoint;
206
207 *instance = media->rtp;
208 ao2_ref(*instance, +1);
209
210 ast_assert(endpoint != NULL);
213 }
214
215 if (endpoint->media.direct_media.enabled) {
217 }
218
220}
221
222/*! \brief Function called by RTP engine to get local video RTP peer */
224{
225 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
226 struct ast_sip_endpoint *endpoint;
227 struct ast_sip_session_media *media;
228
229 if (!channel || !channel->session) {
231 }
232
234 if (!media || !media->rtp) {
236 }
237
238 endpoint = channel->session->endpoint;
239
240 *instance = media->rtp;
241 ao2_ref(*instance, +1);
242
243 ast_assert(endpoint != NULL);
246 }
247
249}
250
251/*! \brief Function called by RTP engine to get peer capabilities */
252static void chan_pjsip_get_codec(struct ast_channel *chan, struct ast_format_cap *result)
253{
254 SCOPE_ENTER(1, "%s Native formats %s\n", ast_channel_name(chan),
258}
259
260/*! \brief Destructor function for \ref transport_info_data */
261static void transport_info_destroy(void *obj)
262{
263 struct transport_info_data *data = obj;
264 ast_free(data);
265}
266
267/*! \brief Datastore used to store local/remote addresses for the
268 * INVITE request that created the PJSIP channel */
270 .type = "chan_pjsip_transport_info",
271 .destroy = transport_info_destroy,
272};
273
275
277{
278 RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
279
280 if (session->endpoint->media.direct_media.glare_mitigation ==
282 return 0;
283 }
284
285 datastore = ast_sip_session_get_datastore(session, "direct_media_glare_mitigation");
286 if (!datastore) {
287 return 0;
288 }
289
290 /* Removing the datastore ensures we won't try to mitigate glare on subsequent reinvites */
291 ast_sip_session_remove_datastore(session, "direct_media_glare_mitigation");
292
293 if ((session->endpoint->media.direct_media.glare_mitigation ==
295 session->inv_session->role == PJSIP_ROLE_UAC) ||
296 (session->endpoint->media.direct_media.glare_mitigation ==
298 session->inv_session->role == PJSIP_ROLE_UAS)) {
299 return 1;
300 }
301
302 return 0;
303}
304
305/*! \brief Helper function to find the position for RTCP */
307{
308 int index;
309
310 for (index = 0; index < AST_VECTOR_SIZE(&session->active_media_state->read_callbacks); ++index) {
311 struct ast_sip_session_media_read_callback_state *callback_state =
312 AST_VECTOR_GET_ADDR(&session->active_media_state->read_callbacks, index);
313
314 if (callback_state->fd != ast_rtp_instance_fd(rtp, 1)) {
315 continue;
316 }
317
318 return index;
319 }
320
321 return -1;
322}
323
324/*!
325 * \pre chan is locked
326 */
327static int check_for_rtp_changes(struct ast_channel *chan, struct ast_rtp_instance *rtp,
328 struct ast_sip_session_media *media, struct ast_sip_session *session)
329{
330 int changed = 0, position = -1;
331
332 if (media->rtp) {
333 position = rtp_find_rtcp_fd_position(session, media->rtp);
334 }
335
336 if (rtp) {
338 if (media->rtp) {
339 if (position != -1) {
340 ast_channel_set_fd(chan, position + AST_EXTENDED_FDS, -1);
341 }
343 }
344 } else if (!ast_sockaddr_isnull(&media->direct_media_addr)){
346 changed = 1;
347 if (media->rtp) {
349 if (position != -1) {
350 ast_channel_set_fd(chan, position + AST_EXTENDED_FDS, ast_rtp_instance_fd(media->rtp, 1));
351 }
352 }
353 }
354
355 return changed;
356}
357
364};
365
366static void rtp_direct_media_data_destroy(void *data)
367{
368 struct rtp_direct_media_data *cdata = data;
369
370 ao2_cleanup(cdata->session);
371 ao2_cleanup(cdata->cap);
372 ao2_cleanup(cdata->vrtp);
373 ao2_cleanup(cdata->rtp);
374 ao2_cleanup(cdata->chan);
375}
376
378 struct ast_channel *chan, struct ast_rtp_instance *rtp, struct ast_rtp_instance *vrtp,
379 const struct ast_format_cap *cap, struct ast_sip_session *session)
380{
382
383 if (!cdata) {
384 return NULL;
385 }
386
387 cdata->chan = ao2_bump(chan);
388 cdata->rtp = ao2_bump(rtp);
389 cdata->vrtp = ao2_bump(vrtp);
390 cdata->cap = ao2_bump((struct ast_format_cap *)cap);
391 cdata->session = ao2_bump(session);
392
393 return cdata;
394}
395
396static int send_direct_media_request(void *data)
397{
398 struct rtp_direct_media_data *cdata = data;
399 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(cdata->chan);
400 struct ast_sip_session *session;
401 int changed = 0;
402 int res = 0;
403
404 /* XXX In an ideal world each media stream would be direct, but for now preserve behavior
405 * and connect only the default media sessions for audio and video.
406 */
407
408 /* The channel needs to be locked when checking for RTP changes.
409 * Otherwise, we could end up destroying an underlying RTCP structure
410 * at the same time that the channel thread is attempting to read RTCP
411 */
412 ast_channel_lock(cdata->chan);
413 session = channel->session;
414 if (session->active_media_state->default_session[AST_MEDIA_TYPE_AUDIO]) {
415 changed |= check_for_rtp_changes(
416 cdata->chan, cdata->rtp, session->active_media_state->default_session[AST_MEDIA_TYPE_AUDIO], session);
417 }
418 if (session->active_media_state->default_session[AST_MEDIA_TYPE_VIDEO]) {
419 changed |= check_for_rtp_changes(
420 cdata->chan, cdata->vrtp, session->active_media_state->default_session[AST_MEDIA_TYPE_VIDEO], session);
421 }
423
424 if (direct_media_mitigate_glare(cdata->session)) {
425 ast_debug(4, "Disregarding setting RTP on %s: mitigating re-INVITE glare\n", ast_channel_name(cdata->chan));
426 ao2_ref(cdata, -1);
427 return 0;
428 }
429
430 if (cdata->cap && ast_format_cap_count(cdata->cap) &&
431 !ast_format_cap_identical(cdata->session->direct_media_cap, cdata->cap)) {
433 ast_format_cap_append_from_cap(cdata->session->direct_media_cap, cdata->cap, AST_MEDIA_TYPE_UNKNOWN);
434 changed = 1;
435 }
436
437 if (changed) {
438 ast_debug(4, "RTP changed on %s; initiating direct media update\n", ast_channel_name(cdata->chan));
439 res = ast_sip_session_refresh(cdata->session, NULL, NULL, NULL,
440 cdata->session->endpoint->media.direct_media.method, 1, NULL);
441 }
442
443 ao2_ref(cdata, -1);
444 return res;
445}
446
447/*! \brief Function called by RTP engine to change where the remote party should send media */
448static int chan_pjsip_set_rtp_peer(struct ast_channel *chan,
449 struct ast_rtp_instance *rtp,
450 struct ast_rtp_instance *vrtp,
451 struct ast_rtp_instance *tpeer,
452 const struct ast_format_cap *cap,
453 int nat_active)
454{
455 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
456 struct ast_sip_session *session = channel->session;
458 SCOPE_ENTER(1, "%s %s\n", ast_channel_name(chan),
460
461 /* Don't try to do any direct media shenanigans on early bridges */
462 if ((rtp || vrtp || tpeer) && !ast_channel_is_bridged(chan)) {
463 ast_debug(4, "Disregarding setting RTP on %s: channel is not bridged\n", ast_channel_name(chan));
464 SCOPE_EXIT_RTN_VALUE(0, "Channel not bridged\n");
465 }
466
467 if (nat_active && session->endpoint->media.direct_media.disable_on_nat) {
468 ast_debug(4, "Disregarding setting RTP on %s: NAT is active\n", ast_channel_name(chan));
469 SCOPE_EXIT_RTN_VALUE(0, "NAT is active\n");
470 }
471
473 if (!cdata) {
475 }
476
478 ast_log(LOG_ERROR, "Unable to send direct media request for channel %s\n", ast_channel_name(chan));
479 ao2_ref(cdata, -1);
480 }
481
483}
484
485/*! \brief Local glue for interacting with the RTP engine core */
487 .type = "PJSIP",
488 .get_rtp_info = chan_pjsip_get_rtp_peer,
489 .get_vrtp_info = chan_pjsip_get_vrtp_peer,
490 .get_codec = chan_pjsip_get_codec,
491 .update_peer = chan_pjsip_set_rtp_peer,
492};
493
495 const char *channel_id)
496{
497 int i;
498
499 for (i = 0; i < AST_VECTOR_SIZE(&session->active_media_state->sessions); ++i) {
500 struct ast_sip_session_media *session_media;
501
502 session_media = AST_VECTOR_GET(&session->active_media_state->sessions, i);
503 if (!session_media || !session_media->rtp) {
504 continue;
505 }
506
507 ast_rtp_instance_set_channel_id(session_media->rtp, channel_id);
508 }
509}
510
511/*!
512 * \brief Determine if a topology is compatible with format capabilities
513 *
514 * This will return true if ANY formats in the topology are compatible with the format
515 * capabilities.
516 *
517 * XXX When supporting true multistream, we will need to be sure to mark which streams from
518 * top1 are compatible with which streams from top2. Then the ones that are not compatible
519 * will need to be marked as "removed" so that they are negotiated as expected.
520 *
521 * \param top Topology
522 * \param cap Format capabilities
523 * \retval 1 The topology has at least one compatible format
524 * \retval 0 The topology has no compatible formats or an error occurred.
525 */
527{
528 struct ast_format_cap *cap_from_top;
529 int res;
530 SCOPE_ENTER(1, "Topology: %s Formats: %s\n",
533
534 cap_from_top = ast_stream_topology_get_formats(top);
535
536 if (!cap_from_top) {
537 SCOPE_EXIT_RTN_VALUE(0, "Topology had no formats\n");
538 }
539
540 res = ast_format_cap_iscompatible(cap_from_top, cap);
541 ao2_ref(cap_from_top, -1);
542
543 SCOPE_EXIT_RTN_VALUE(res, "Compatible? %s\n", res ? "yes" : "no");
544}
545
546/*! \brief Function called to create a new PJSIP Asterisk channel */
547static 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)
548{
549 struct ast_channel *chan;
550 struct ast_format_cap *caps;
551 RAII_VAR(struct chan_pjsip_pvt *, pvt, NULL, ao2_cleanup);
552 struct ast_sip_channel_pvt *channel;
553 struct ast_variable *var;
554 struct ast_stream_topology *topology;
556
558 SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't create pvt\n");
559 }
560
562 S_COR(session->id.number.valid, session->id.number.str, ""),
563 S_COR(session->id.name.valid, session->id.name.str, ""),
564 session->endpoint->accountcode,
565 exten, session->endpoint->context,
566 assignedids, requestor, 0,
567 session->endpoint->persistent, "PJSIP/%s-%08x",
569 (unsigned) ast_atomic_fetchadd_int((int *) &chan_idx, +1));
570 if (!chan) {
571 SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't create channel\n");
572 }
573
575
576 if (!(channel = ast_sip_channel_pvt_alloc(pvt, session))) {
577 ast_channel_unlock(chan);
578 ast_hangup(chan);
579 SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't create pvt channel\n");
580 }
581
582 ast_channel_tech_pvt_set(chan, channel);
583
584 if (!ast_stream_topology_get_count(session->pending_media_state->topology) ||
585 !compatible_formats_exist(session->pending_media_state->topology, session->endpoint->media.codecs)) {
587 if (!caps) {
588 ast_channel_unlock(chan);
589 ast_hangup(chan);
590 SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't create caps\n");
591 }
592 ast_format_cap_append_from_cap(caps, session->endpoint->media.codecs, AST_MEDIA_TYPE_UNKNOWN);
593 topology = ast_stream_topology_clone(session->endpoint->media.topology);
594 } else {
595 caps = ast_stream_topology_get_formats(session->pending_media_state->topology);
596 topology = ast_stream_topology_clone(session->pending_media_state->topology);
597 }
598
599 if (!topology || !caps) {
600 ao2_cleanup(caps);
601 ast_stream_topology_free(topology);
602 ast_channel_unlock(chan);
603 ast_hangup(chan);
604 SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't get caps or clone topology\n");
605 }
606
608
610 ast_channel_set_stream_topology(chan, topology);
611
612 if (!ast_format_cap_empty(caps)) {
613 struct ast_format *fmt;
614
616 if (!fmt) {
617 /* Since our capabilities aren't empty, this will succeed */
618 fmt = ast_format_cap_get_format(caps, 0);
619 }
624 ao2_ref(fmt, -1);
625 }
626
627 ao2_ref(caps, -1);
628
629 if (state == AST_STATE_RING) {
630 ast_channel_rings_set(chan, 1);
631 }
632
634
637 ast_channel_caller(chan)->ani2 = session->ani2;
638
639 if (!ast_strlen_zero(exten)) {
640 /* Set provided DNID on the new channel. */
641 ast_channel_dialed(chan)->number.str = ast_strdup(exten);
642 }
643
645
646 ast_channel_callgroup_set(chan, session->endpoint->pickup.callgroup);
647 ast_channel_pickupgroup_set(chan, session->endpoint->pickup.pickupgroup);
648
649 ast_channel_named_callgroups_set(chan, session->endpoint->pickup.named_callgroups);
650 ast_channel_named_pickupgroups_set(chan, session->endpoint->pickup.named_pickupgroups);
651
652 if (!ast_strlen_zero(session->endpoint->language)) {
653 ast_channel_language_set(chan, session->endpoint->language);
654 }
655
656 if (!ast_strlen_zero(session->endpoint->zone)) {
657 struct ast_tone_zone *zone = ast_get_indication_zone(session->endpoint->zone);
658 if (!zone) {
659 ast_log(LOG_ERROR, "Unknown country code '%s' for tonezone. Check indications.conf for available country codes.\n", session->endpoint->zone);
660 }
661 ast_channel_zone_set(chan, zone);
662 }
663
664 for (var = session->endpoint->channel_vars; var; var = var->next) {
665 char buf[512];
667 var->value, buf, sizeof(buf)));
668 }
669
671 ast_channel_unlock(chan);
672
674
676}
677
680 unsigned long indent;
681};
682
683static int answer(void *data)
684{
685 struct answer_data *ans_data = data;
686 pj_status_t status = PJ_SUCCESS;
687 pjsip_tx_data *packet = NULL;
688 struct ast_sip_session *session = ans_data->session;
690
691 if (session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
692 ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
693 session->inv_session->cause,
694 pjsip_get_status_text(session->inv_session->cause)->ptr);
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) {
709 }
710
711 if (status != PJ_SUCCESS) {
712 char err[PJ_ERR_MSG_SIZE];
713
714 pj_strerror(status, err, sizeof(err));
715 ast_log(LOG_WARNING,"Cannot answer '%s': %s\n",
716 ast_channel_name(session->channel), err);
717 /*
718 * Return this value so we can distinguish between this
719 * failure and the threadpool synchronous push failing.
720 */
721 SCOPE_EXIT_RTN_VALUE(-2, "pjproject failure\n");
722 }
724}
725
726/*! \brief Function called by core when we should answer a PJSIP session */
727static int chan_pjsip_answer(struct ast_channel *ast)
728{
729 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
730 struct ast_sip_session *session;
731 struct answer_data ans_data = { 0, };
732 int res;
733 SCOPE_ENTER(1, "%s\n", ast_channel_name(ast));
734
735 if (ast_channel_state(ast) == AST_STATE_UP) {
736 SCOPE_EXIT_RTN_VALUE(0, "Already up\n");
737 return 0;
738 }
739
741 session = ao2_bump(channel->session);
742
743 /* the answer task needs to be pushed synchronously otherwise a race condition
744 can occur between this thread and bridging (specifically when native bridging
745 attempts to do direct media) */
747 ans_data.session = session;
748 ans_data.indent = ast_trace_get_indent();
749 res = ast_sip_push_task_wait_serializer(session->serializer, answer, &ans_data);
750 if (res) {
751 if (res == -1) {
752 ast_log(LOG_ERROR,"Cannot answer '%s': Unable to push answer task to the threadpool.\n",
753 ast_channel_name(session->channel));
754 }
755 ao2_ref(session, -1);
756 ast_channel_lock(ast);
757 SCOPE_EXIT_RTN_VALUE(-1, "Couldn't push task\n");
758 }
759 ao2_ref(session, -1);
760 ast_channel_lock(ast);
761
763}
764
765/*! \brief Internal helper function called when CNG tone is detected */
767 struct ast_frame *f)
768{
769 const char *target_context;
770 int exists;
771 int dsp_features;
772
773 dsp_features = ast_dsp_get_features(session->dsp);
774 dsp_features &= ~DSP_FEATURE_FAX_DETECT;
775 if (dsp_features) {
776 ast_dsp_set_features(session->dsp, dsp_features);
777 } else {
778 ast_dsp_free(session->dsp);
779 session->dsp = NULL;
780 }
781
782 /* If already executing in the fax extension don't do anything */
783 if (!strcmp(ast_channel_exten(ast), "fax")) {
784 return f;
785 }
786
787 target_context = ast_channel_context(ast);
788
789 /*
790 * We need to unlock the channel here because ast_exists_extension has the
791 * potential to start and stop an autoservice on the channel. Such action
792 * is prone to deadlock if the channel is locked.
793 *
794 * ast_async_goto() has its own restriction on not holding the channel lock.
795 */
797 ast_frfree(f);
798 f = &ast_null_frame;
799 exists = ast_exists_extension(ast, target_context, "fax", 1,
800 S_COR(ast_channel_caller(ast)->id.number.valid,
801 ast_channel_caller(ast)->id.number.str, NULL));
802 if (exists) {
803 ast_verb(2, "Redirecting '%s' to fax extension due to CNG detection\n",
804 ast_channel_name(ast));
805 pbx_builtin_setvar_helper(ast, "FAXEXTEN", ast_channel_exten(ast));
806 if (ast_async_goto(ast, target_context, "fax", 1)) {
807 ast_log(LOG_ERROR, "Failed to async goto '%s' into fax extension in '%s'\n",
808 ast_channel_name(ast), target_context);
809 }
810 } else {
811 ast_log(LOG_NOTICE, "FAX CNG detected on '%s' but no fax extension in '%s'\n",
812 ast_channel_name(ast), target_context);
813 }
814
815 /* It's possible for a masquerade to have occurred when doing the ast_async_goto resulting in
816 * the channel on the session having changed. Since we need to return with the original channel
817 * locked we lock the channel that was passed in and not session->channel.
818 */
819 ast_channel_lock(ast);
820
821 return f;
822}
823
824/*! \brief Determine if the given frame is in a format we've negotiated */
826{
827 struct ast_stream_topology *topology = session->active_media_state->topology;
828 struct ast_stream *stream = ast_stream_topology_get_stream(topology, f->stream_num);
829 const struct ast_format_cap *cap = ast_stream_get_formats(stream);
830
832}
833
834/*!
835 * \brief Function called by core to read any waiting frames
836 *
837 * \note The channel is already locked.
838 */
840{
841 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
842 struct ast_sip_session *session = channel->session;
843 struct ast_sip_session_media_read_callback_state *callback_state;
844 struct ast_frame *f;
845 int fdno = ast_channel_fdno(ast) - AST_EXTENDED_FDS;
846 struct ast_frame *cur;
847
848 if (fdno >= AST_VECTOR_SIZE(&session->active_media_state->read_callbacks)) {
849 return &ast_null_frame;
850 }
851
852 callback_state = AST_VECTOR_GET_ADDR(&session->active_media_state->read_callbacks, fdno);
853 f = callback_state->read_callback(session, callback_state->session);
854
855 if (!f) {
856 return f;
857 }
858
859 for (cur = f; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
860 if (cur->frametype == AST_FRAME_VOICE) {
861 break;
862 }
863 }
864
865 if (!cur || callback_state->session != session->active_media_state->default_session[callback_state->session->type]) {
866 return f;
867 }
868
869 session = channel->session;
870
871 /*
872 * Asymmetric RTP only has one native format set at a time.
873 * Therefore we need to update the native format to the current
874 * raw read format BEFORE the native format check
875 */
876 if (!session->endpoint->asymmetric_rtp_codec &&
879 struct ast_format_cap *caps;
880
881 /* For maximum compatibility we ensure that the formats match that of the received media */
882 ast_debug(1, "Oooh, got a frame with format of %s on channel '%s' when we're sending '%s', switching to match\n",
885
887 if (caps) {
892 ao2_ref(caps, -1);
893 }
894
897
898 if (ast_channel_is_bridged(ast)) {
900 }
901 }
902
905 ast_debug(1, "Oooh, got a frame with format of %s on channel '%s' when it has not been negotiated\n",
907 ast_frfree(f);
908 return &ast_null_frame;
909 }
910
911 if (session->dsp) {
912 int dsp_features;
913
914 dsp_features = ast_dsp_get_features(session->dsp);
915 if ((dsp_features & DSP_FEATURE_FAX_DETECT)
916 && session->endpoint->faxdetect_timeout
917 && session->endpoint->faxdetect_timeout <= ast_channel_get_up_time(ast)) {
918 dsp_features &= ~DSP_FEATURE_FAX_DETECT;
919 if (dsp_features) {
920 ast_dsp_set_features(session->dsp, dsp_features);
921 } else {
922 ast_dsp_free(session->dsp);
923 session->dsp = NULL;
924 }
925 ast_debug(3, "Channel driver fax CNG detection timeout on %s\n",
926 ast_channel_name(ast));
927 }
928 }
929 if (session->dsp) {
930 f = ast_dsp_process(ast, session->dsp, f);
931 if (f && (f->frametype == AST_FRAME_DTMF)) {
932 if (f->subclass.integer == 'f') {
933 ast_debug(3, "Channel driver fax CNG detected on %s\n",
934 ast_channel_name(ast));
936 /* When chan_pjsip_cng_tone_detected returns it is possible for the
937 * channel pointed to by ast and by session->channel to differ due to a
938 * masquerade. It's best not to touch things after this.
939 */
940 } else {
941 ast_debug(3, "* Detected inband DTMF '%c' on '%s'\n", f->subclass.integer,
942 ast_channel_name(ast));
943 }
944 }
945 }
946
947 return f;
948}
949
950static int chan_pjsip_write_stream(struct ast_channel *ast, int stream_num, struct ast_frame *frame)
951{
952 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
953 struct ast_sip_session *session = channel->session;
954 struct ast_sip_session_media *media = NULL;
955 int res = 0;
956
957 /* The core provides a guarantee that the stream will exist when we are called if stream_num is provided */
958 if (stream_num >= 0) {
959 /* What is not guaranteed is that a media session will exist */
962 }
963 }
964
965 switch (frame->frametype) {
966 case AST_FRAME_VOICE:
967 if (!media) {
968 return 0;
969 } else if (media->type != AST_MEDIA_TYPE_AUDIO) {
970 ast_debug(3, "Channel %s stream %d is of type '%s', not audio!\n",
972 return 0;
973 } else if (media == channel->session->active_media_state->default_session[AST_MEDIA_TYPE_AUDIO] &&
976 struct ast_str *write_transpath = ast_str_alloca(256);
977 struct ast_str *read_transpath = ast_str_alloca(256);
978
980 "Channel %s asked to send %s frame when native formats are %s (rd:%s->%s;%s wr:%s->%s;%s)\n",
981 ast_channel_name(ast),
989 ast_translate_path_to_str(ast_channel_writetrans(ast), &write_transpath));
990 return 0;
991 } else if (media->write_callback) {
992 res = media->write_callback(session, media, frame);
993
994 }
995 break;
996 case AST_FRAME_VIDEO:
997 if (!media) {
998 return 0;
999 } else if (media->type != AST_MEDIA_TYPE_VIDEO) {
1000 ast_debug(3, "Channel %s stream %d is of type '%s', not video!\n",
1001 ast_channel_name(ast), stream_num, ast_codec_media_type2str(media->type));
1002 return 0;
1003 } else if (media->write_callback) {
1004 res = media->write_callback(session, media, frame);
1005 }
1006 break;
1007 case AST_FRAME_MODEM:
1008 if (!media) {
1009 return 0;
1010 } else if (media->type != AST_MEDIA_TYPE_IMAGE) {
1011 ast_debug(3, "Channel %s stream %d is of type '%s', not image!\n",
1012 ast_channel_name(ast), stream_num, ast_codec_media_type2str(media->type));
1013 return 0;
1014 } else if (media->write_callback) {
1015 res = media->write_callback(session, media, frame);
1016 }
1017 break;
1018 case AST_FRAME_CNG:
1019 break;
1020 case AST_FRAME_RTCP:
1021 /* We only support writing out feedback */
1022 if (frame->subclass.integer != AST_RTP_RTCP_PSFB || !media) {
1023 return 0;
1024 } else if (media->type != AST_MEDIA_TYPE_VIDEO) {
1025 ast_debug(3, "Channel %s stream %d is of type '%s', not video! Unable to write RTCP feedback.\n",
1026 ast_channel_name(ast), stream_num, ast_codec_media_type2str(media->type));
1027 return 0;
1028 } else if (media->write_callback) {
1029 res = media->write_callback(session, media, frame);
1030 }
1031 break;
1032 default:
1033 ast_log(LOG_WARNING, "Can't send %u type frames with PJSIP\n", frame->frametype);
1034 break;
1035 }
1036
1037 return res;
1038}
1039
1040static int chan_pjsip_write(struct ast_channel *ast, struct ast_frame *frame)
1041{
1042 return chan_pjsip_write_stream(ast, -1, frame);
1043}
1044
1045/*! \brief Function called by core to change the underlying owner channel */
1046static int chan_pjsip_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
1047{
1048 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(newchan);
1049
1050 if (channel->session->channel != oldchan) {
1051 return -1;
1052 }
1053
1054 /*
1055 * The masquerade has suspended the channel's session
1056 * serializer so we can safely change it outside of
1057 * the serializer thread.
1058 */
1059 channel->session->channel = newchan;
1060
1062
1063 return 0;
1064}
1065
1066/*! AO2 hash function for on hold UIDs */
1067static int uid_hold_hash_fn(const void *obj, const int flags)
1068{
1069 const char *key = obj;
1070
1071 switch (flags & OBJ_SEARCH_MASK) {
1072 case OBJ_SEARCH_KEY:
1073 break;
1074 case OBJ_SEARCH_OBJECT:
1075 break;
1076 default:
1077 /* Hash can only work on something with a full key. */
1078 ast_assert(0);
1079 return 0;
1080 }
1081 return ast_str_hash(key);
1082}
1083
1084/*! AO2 sort function for on hold UIDs */
1085static int uid_hold_sort_fn(const void *obj_left, const void *obj_right, const int flags)
1086{
1087 const char *left = obj_left;
1088 const char *right = obj_right;
1089 int cmp;
1090
1091 switch (flags & OBJ_SEARCH_MASK) {
1092 case OBJ_SEARCH_OBJECT:
1093 case OBJ_SEARCH_KEY:
1094 cmp = strcmp(left, right);
1095 break;
1097 cmp = strncmp(left, right, strlen(right));
1098 break;
1099 default:
1100 /* Sort can only work on something with a full or partial key. */
1101 ast_assert(0);
1102 cmp = 0;
1103 break;
1104 }
1105 return cmp;
1106}
1107
1109
1110/*!
1111 * \brief Add a channel ID to the list of PJSIP channels on hold
1112 *
1113 * \param chan_uid - Unique ID of the channel being put into the hold list
1114 *
1115 * \retval 0 Channel has been added to or was already in the hold list
1116 * \retval -1 Failed to add channel to the hold list
1117 */
1118static int chan_pjsip_add_hold(const char *chan_uid)
1119{
1120 RAII_VAR(char *, hold_uid, NULL, ao2_cleanup);
1121
1122 hold_uid = ao2_find(pjsip_uids_onhold, chan_uid, OBJ_SEARCH_KEY);
1123 if (hold_uid) {
1124 /* Device is already on hold. Nothing to do. */
1125 return 0;
1126 }
1127
1128 /* Device wasn't in hold list already. Create a new one. */
1129 hold_uid = ao2_alloc_options(strlen(chan_uid) + 1, NULL,
1131 if (!hold_uid) {
1132 return -1;
1133 }
1134
1135 ast_copy_string(hold_uid, chan_uid, strlen(chan_uid) + 1);
1136
1137 if (ao2_link(pjsip_uids_onhold, hold_uid) == 0) {
1138 return -1;
1139 }
1140
1141 return 0;
1142}
1143
1144/*!
1145 * \brief Remove a channel ID from the list of PJSIP channels on hold
1146 *
1147 * \param chan_uid - Unique ID of the channel being taken out of the hold list
1148 */
1149static void chan_pjsip_remove_hold(const char *chan_uid)
1150{
1152}
1153
1154/*!
1155 * \brief Determine whether a channel ID is in the list of PJSIP channels on hold
1156 *
1157 * \param chan_uid - Channel being checked
1158 *
1159 * \retval 0 The channel is not in the hold list
1160 * \retval 1 The channel is in the hold list
1161 */
1162static int chan_pjsip_get_hold(const char *chan_uid)
1163{
1164 RAII_VAR(char *, hold_uid, NULL, ao2_cleanup);
1165
1166 hold_uid = ao2_find(pjsip_uids_onhold, chan_uid, OBJ_SEARCH_KEY);
1167 if (!hold_uid) {
1168 return 0;
1169 }
1170
1171 return 1;
1172}
1173
1174/*! \brief Function called to get the device state of an endpoint */
1175static int chan_pjsip_devicestate(const char *data)
1176{
1177 RAII_VAR(struct ast_sip_endpoint *, endpoint, ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint", data), ao2_cleanup);
1179 RAII_VAR(struct ast_endpoint_snapshot *, endpoint_snapshot, NULL, ao2_cleanup);
1180 struct ast_devstate_aggregate aggregate;
1181 int num, inuse = 0;
1182
1183 if (!endpoint) {
1184 return AST_DEVICE_INVALID;
1185 }
1186
1187 endpoint_snapshot = ast_endpoint_latest_snapshot(ast_endpoint_get_tech(endpoint->persistent),
1188 ast_endpoint_get_resource(endpoint->persistent));
1189
1190 if (!endpoint_snapshot) {
1191 return AST_DEVICE_INVALID;
1192 }
1193
1194 if (endpoint_snapshot->state == AST_ENDPOINT_OFFLINE) {
1196 } else if (endpoint_snapshot->state == AST_ENDPOINT_ONLINE) {
1198 }
1199
1200 if (!endpoint_snapshot->num_channels) {
1201 return state;
1202 }
1203
1204 ast_devstate_aggregate_init(&aggregate);
1205
1206 for (num = 0; num < endpoint_snapshot->num_channels; num++) {
1207 struct ast_channel_snapshot *snapshot;
1208
1209 snapshot = ast_channel_snapshot_get_latest(endpoint_snapshot->channel_ids[num]);
1210 if (!snapshot) {
1211 continue;
1212 }
1213
1214 if (chan_pjsip_get_hold(snapshot->base->uniqueid)) {
1216 } else {
1217 ast_devstate_aggregate_add(&aggregate, ast_state_chan2dev(snapshot->state));
1218 }
1219
1220 if ((snapshot->state == AST_STATE_UP) || (snapshot->state == AST_STATE_RING) ||
1221 (snapshot->state == AST_STATE_BUSY)) {
1222 inuse++;
1223 }
1224
1225 ao2_ref(snapshot, -1);
1226 }
1227
1228 if (endpoint->devicestate_busy_at && (inuse == endpoint->devicestate_busy_at)) {
1230 } else if (ast_devstate_aggregate_result(&aggregate) != AST_DEVICE_INVALID) {
1232 }
1233
1234 return state;
1235}
1236
1237/*! \brief Function called to query options on a channel */
1238static int chan_pjsip_queryoption(struct ast_channel *ast, int option, void *data, int *datalen)
1239{
1240 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
1241 int res = -1;
1243
1244 if (!channel) {
1245 return -1;
1246 }
1247
1248 switch (option) {
1250 if (channel->session->endpoint->media.t38.enabled) {
1251 switch (channel->session->t38state) {
1252 case T38_LOCAL_REINVITE:
1253 case T38_PEER_REINVITE:
1255 break;
1256 case T38_ENABLED:
1258 break;
1259 case T38_REJECTED:
1261 break;
1262 default:
1264 break;
1265 }
1266 }
1267
1268 *((enum ast_t38_state *) data) = state;
1269 res = 0;
1270
1271 break;
1272 default:
1273 break;
1274 }
1275
1276 return res;
1277}
1278
1279static const char *chan_pjsip_get_uniqueid(struct ast_channel *ast)
1280{
1281 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
1283
1284 if (!channel || !uniqueid) {
1285 return "";
1286 }
1287
1288 ast_copy_pj_str(uniqueid, &channel->session->inv_session->dlg->call_id->id, UNIQUEID_BUFSIZE);
1289
1290 return uniqueid;
1291}
1292
1298 size_t datalen;
1299};
1300
1301static void indicate_data_destroy(void *obj)
1302{
1303 struct indicate_data *ind_data = obj;
1304
1305 ast_free(ind_data->frame_data);
1306 ao2_ref(ind_data->session, -1);
1307}
1308
1310 int condition, int response_code, const void *frame_data, size_t datalen)
1311{
1312 struct indicate_data *ind_data = ao2_alloc(sizeof(*ind_data), indicate_data_destroy);
1313
1314 if (!ind_data) {
1315 return NULL;
1316 }
1317
1318 ind_data->frame_data = ast_malloc(datalen);
1319 if (!ind_data->frame_data) {
1320 ao2_ref(ind_data, -1);
1321 return NULL;
1322 }
1323
1324 memcpy(ind_data->frame_data, frame_data, datalen);
1325 ind_data->datalen = datalen;
1326 ind_data->condition = condition;
1327 ind_data->response_code = response_code;
1328 ao2_ref(session, +1);
1329 ind_data->session = session;
1330
1331 return ind_data;
1332}
1333
1334static int indicate(void *data)
1335{
1336 pjsip_tx_data *packet = NULL;
1337 struct indicate_data *ind_data = data;
1338 struct ast_sip_session *session = ind_data->session;
1339 int response_code = ind_data->response_code;
1340
1341 if ((session->inv_session->state != PJSIP_INV_STATE_DISCONNECTED) &&
1342 (pjsip_inv_answer(session->inv_session, response_code, NULL, NULL, &packet) == PJ_SUCCESS)) {
1344 }
1345
1346 ao2_ref(ind_data, -1);
1347
1348 return 0;
1349}
1350
1351/*! \brief Send SIP INFO with video update request */
1352static int transmit_info_with_vidupdate(void *data)
1353{
1354 const char * xml =
1355 "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n"
1356 " <media_control>\r\n"
1357 " <vc_primitive>\r\n"
1358 " <to_encoder>\r\n"
1359 " <picture_fast_update/>\r\n"
1360 " </to_encoder>\r\n"
1361 " </vc_primitive>\r\n"
1362 " </media_control>\r\n";
1363
1364 const struct ast_sip_body body = {
1365 .type = "application",
1366 .subtype = "media_control+xml",
1367 .body_text = xml
1368 };
1369
1370 RAII_VAR(struct ast_sip_session *, session, data, ao2_cleanup);
1371 struct pjsip_tx_data *tdata;
1372
1373 if (session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
1374 ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
1375 session->inv_session->cause,
1376 pjsip_get_status_text(session->inv_session->cause)->ptr);
1377 return -1;
1378 }
1379
1380 if (ast_sip_create_request("INFO", session->inv_session->dlg, session->endpoint, NULL, NULL, &tdata)) {
1381 ast_log(LOG_ERROR, "Could not create text video update INFO request\n");
1382 return -1;
1383 }
1384 if (ast_sip_add_body(tdata, &body)) {
1385 ast_log(LOG_ERROR, "Could not add body to text video update INFO request\n");
1386 return -1;
1387 }
1389
1390 return 0;
1391}
1392
1393/*!
1394 * \internal
1395 * \brief TRUE if a COLP update can be sent to the peer.
1396 * \since 13.3.0
1397 *
1398 * \param session The session to see if the COLP update is allowed.
1399 *
1400 * \retval 0 Update is not allowed.
1401 * \retval 1 Update is allowed.
1402 */
1404{
1405 struct ast_party_id connected_id;
1406 int update_allowed = 0;
1407
1408 if (!session->endpoint->id.send_connected_line
1409 || (!session->endpoint->id.send_pai && !session->endpoint->id.send_rpid)) {
1410 return 0;
1411 }
1412
1413 /*
1414 * Check if privacy allows the update. Check while the channel
1415 * is locked so we can work with the shallow connected_id copy.
1416 */
1417 ast_channel_lock(session->channel);
1418 connected_id = ast_channel_connected_effective_id(session->channel);
1419 if (connected_id.number.valid
1420 && (session->endpoint->id.trust_outbound
1422 update_allowed = 1;
1423 }
1424 ast_channel_unlock(session->channel);
1425
1426 return update_allowed;
1427}
1428
1429/*! \brief Update connected line information */
1431{
1432 struct ast_sip_session *session = data;
1433
1434 if (session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
1435 ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
1436 session->inv_session->cause,
1437 pjsip_get_status_text(session->inv_session->cause)->ptr);
1438 ao2_ref(session, -1);
1439 return -1;
1440 }
1441
1442 if (ast_channel_state(session->channel) == AST_STATE_UP
1443 || session->inv_session->role == PJSIP_ROLE_UAC) {
1446 int generate_new_sdp;
1447
1448 method = session->endpoint->id.refresh_method;
1449 if (session->inv_session->options & PJSIP_INV_SUPPORT_UPDATE) {
1451 }
1452
1453 /* Only the INVITE method actually needs SDP, UPDATE can do without */
1454 generate_new_sdp = (method == AST_SIP_SESSION_REFRESH_METHOD_INVITE);
1455
1456 ast_sip_session_refresh(session, NULL, NULL, NULL, method, generate_new_sdp, NULL);
1457 }
1458 } else if (session->endpoint->id.rpid_immediate
1459 && session->inv_session->state != PJSIP_INV_STATE_DISCONNECTED
1461 int response_code = 0;
1462
1463 if (ast_channel_state(session->channel) == AST_STATE_RING) {
1464 response_code = !session->endpoint->inband_progress ? 180 : 183;
1465 } else if (ast_channel_state(session->channel) == AST_STATE_RINGING) {
1466 response_code = 183;
1467 }
1468
1469 if (response_code) {
1470 struct pjsip_tx_data *packet = NULL;
1471
1472 if (pjsip_inv_answer(session->inv_session, response_code, NULL, NULL, &packet) == PJ_SUCCESS) {
1474 }
1475 }
1476 }
1477
1478 ao2_ref(session, -1);
1479 return 0;
1480}
1481
1482/*! \brief Update local hold state and send a re-INVITE with the new SDP */
1483static int remote_send_hold_refresh(struct ast_sip_session *session, unsigned int held)
1484{
1485 struct ast_sip_session_media *session_media = session->active_media_state->default_session[AST_MEDIA_TYPE_AUDIO];
1486 if (session_media) {
1487 session_media->locally_held = held;
1488 }
1490 ao2_ref(session, -1);
1491
1492 return 0;
1493}
1494
1495/*! \brief Update local hold state to be held */
1496static int remote_send_hold(void *data)
1497{
1498 return remote_send_hold_refresh(data, 1);
1499}
1500
1501/*! \brief Update local hold state to be unheld */
1502static int remote_send_unhold(void *data)
1503{
1504 return remote_send_hold_refresh(data, 0);
1505}
1506
1510};
1511
1513{
1515
1518}
1519
1521 struct ast_sip_session *session, const struct ast_stream_topology *topology)
1522{
1524
1525 refresh_data = ast_calloc(1, sizeof(*refresh_data));
1526 if (!refresh_data) {
1527 return NULL;
1528 }
1529
1532 if (!refresh_data->media_state) {
1534 return NULL;
1535 }
1536 refresh_data->media_state->topology = ast_stream_topology_clone(topology);
1537 if (!refresh_data->media_state->topology) {
1539 return NULL;
1540 }
1541
1542 return refresh_data;
1543}
1544
1545static int on_topology_change_response(struct ast_sip_session *session, pjsip_rx_data *rdata)
1546{
1547 SCOPE_ENTER(3, "%s: Received response code %d. PT: %s AT: %s\n", ast_sip_session_get_name(session),
1548 rdata->msg_info.msg->line.status.code,
1549 ast_str_tmp(256, ast_stream_topology_to_str(session->pending_media_state->topology, &STR_TMP)),
1550 ast_str_tmp(256, ast_stream_topology_to_str(session->active_media_state->topology, &STR_TMP)));
1551
1552
1553 if (PJSIP_IS_STATUS_IN_CLASS(rdata->msg_info.msg->line.status.code, 200)) {
1554 /* The topology was changed to something new so give notice to what requested
1555 * it so it queries the channel and updates accordingly.
1556 */
1557 if (session->channel) {
1559 SCOPE_EXIT_RTN_VALUE(0, "%s: Queued topology change frame\n", ast_sip_session_get_name(session));
1560 }
1561 SCOPE_EXIT_RTN_VALUE(0, "%s: No channel? Can't queue topology change frame\n", ast_sip_session_get_name(session));
1562 } else if (300 <= rdata->msg_info.msg->line.status.code) {
1563 /* The topology change failed, so drop the current pending media state */
1564 ast_sip_session_media_state_reset(session->pending_media_state);
1565 SCOPE_EXIT_RTN_VALUE(0, "%s: response code > 300. Resetting pending media state\n", ast_sip_session_get_name(session));
1566 }
1567
1568 SCOPE_EXIT_RTN_VALUE(0, "%s: Nothing to do\n", ast_sip_session_get_name(session));
1569}
1570
1571static int send_topology_change_refresh(void *data)
1572{
1577 int ret;
1579 ast_str_tmp(256, ast_stream_topology_to_str(refresh_data->media_state->topology, &STR_TMP)));
1580
1581 /* See RFC 6337, especially section 3.2: If the early media SDP was sent reliably, we are allowed
1582 * to send UPDATEs. Only relevant for AST_STATE_RINGING and AST_STATE_RING - if the channel is UP,
1583 * re-INVITES can be sent.
1584 */
1585 if (session->early_confirmed && (state == AST_STATE_RINGING || state == AST_STATE_RING)) {
1587 }
1588
1590 method, 1, refresh_data->media_state);
1591 refresh_data->media_state = NULL;
1593
1595}
1596
1598 const struct ast_stream_topology *proposed)
1599{
1601 int res;
1602 SCOPE_ENTER(1);
1603
1605 if (!refresh_data) {
1606 SCOPE_EXIT_RTN_VALUE(-1, "Couldn't create refresh_data\n");
1607 }
1608
1610 if (res) {
1612 }
1613 SCOPE_EXIT_RTN_VALUE(res, "RC: %d\n", res);
1614}
1615
1616/* Forward declarations */
1617static int transmit_info_dtmf(void *data);
1618static struct info_dtmf_data *info_dtmf_data_alloc(struct ast_sip_session *session, char digit, unsigned int duration);
1619
1620/*! \brief Function called by core to ask the channel to indicate some sort of condition */
1621static int chan_pjsip_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
1622{
1623 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
1624 struct ast_sip_session_media *media;
1625 int response_code = 0;
1626 int res = 0;
1627 char *device_buf;
1628 size_t device_buf_size;
1629 int i;
1630 const struct ast_stream_topology *topology;
1631 struct ast_frame f = {
1633 .subclass = {
1634 .integer = condition
1635 },
1636 .datalen = datalen,
1637 .data.ptr = (void *)data,
1638 };
1639 char condition_name[256];
1640 unsigned int duration;
1641 char digit;
1642 struct info_dtmf_data *dtmf_data;
1643
1644 SCOPE_ENTER(3, "%s: Indicated %s\n", ast_channel_name(ast),
1645 ast_frame_subclass2str(&f, condition_name, sizeof(condition_name), NULL, 0));
1646
1647 switch (condition) {
1649 if (ast_channel_state(ast) == AST_STATE_RING) {
1650 if (channel->session->endpoint->inband_progress ||
1651 (channel->session->inv_session && channel->session->inv_session->neg &&
1652 pjmedia_sdp_neg_get_state(channel->session->inv_session->neg) == PJMEDIA_SDP_NEG_STATE_DONE)) {
1653 res = -1;
1655 response_code = 180;
1656 } else {
1657 response_code = 183;
1658 }
1659 } else {
1660 response_code = 180;
1661 }
1662 } else {
1663 res = -1;
1664 }
1666 break;
1667 case AST_CONTROL_BUSY:
1668 if (ast_channel_state(ast) != AST_STATE_UP) {
1669 response_code = 486;
1670 } else {
1671 res = -1;
1672 }
1673 break;
1675 if (ast_channel_state(ast) != AST_STATE_UP) {
1676 response_code = 503;
1677 } else {
1678 res = -1;
1679 }
1680 break;
1682 if (ast_channel_state(ast) != AST_STATE_UP) {
1683 response_code = 484;
1684 } else {
1685 res = -1;
1686 }
1687 break;
1689 if (ast_channel_state(ast) != AST_STATE_UP) {
1690 response_code = 100;
1691 } else {
1692 res = -1;
1693 }
1694 break;
1696 if (ast_channel_state(ast) != AST_STATE_UP) {
1697 response_code = 183;
1698 } else {
1699 res = -1;
1700 }
1702 break;
1703 case AST_CONTROL_FLASH:
1704 duration = 300;
1705 digit = '!';
1706 dtmf_data = info_dtmf_data_alloc(channel->session, digit, duration);
1707
1708 if (!dtmf_data) {
1709 res = -1;
1710 break;
1711 }
1712
1713 if (ast_sip_push_task(channel->session->serializer, transmit_info_dtmf, dtmf_data)) {
1714 ast_log(LOG_WARNING, "Error sending FLASH via INFO on channel %s\n", ast_channel_name(ast));
1715 ao2_ref(dtmf_data, -1); /* dtmf_data can't be null here */
1716 res = -1;
1717 }
1718 break;
1720 for (i = 0; i < AST_VECTOR_SIZE(&channel->session->active_media_state->sessions); ++i) {
1721 media = AST_VECTOR_GET(&channel->session->active_media_state->sessions, i);
1722 if (!media || media->type != AST_MEDIA_TYPE_VIDEO) {
1723 continue;
1724 }
1725 if (media->rtp) {
1726 /* FIXME: Only use this for VP8. Additional work would have to be done to
1727 * fully support other video codecs */
1728
1732 (channel->session->endpoint->media.webrtc &&
1734 /* FIXME Fake RTP write, this will be sent as an RTCP packet. Ideally the
1735 * RTP engine would provide a way to externally write/schedule RTCP
1736 * packets */
1737 struct ast_frame fr;
1740 res = ast_rtp_instance_write(media->rtp, &fr);
1741 } else {
1742 ao2_ref(channel->session, +1);
1744 ao2_cleanup(channel->session);
1745 }
1746 }
1747 ast_test_suite_event_notify("AST_CONTROL_VIDUPDATE", "Result: Success");
1748 } else {
1749 ast_test_suite_event_notify("AST_CONTROL_VIDUPDATE", "Result: Failure");
1750 res = -1;
1751 }
1752 }
1753 /* XXX If there were no video streams, then this should set
1754 * res to -1
1755 */
1756 break;
1758 ao2_ref(channel->session, +1);
1760 ao2_cleanup(channel->session);
1761 }
1762 break;
1764 break;
1766 res = -1;
1767 break;
1769 ast_assert(datalen == sizeof(int));
1770 if (*(int *) data) {
1771 /*
1772 * Masquerade is beginning:
1773 * Wait for session serializer to get suspended.
1774 */
1775 ast_channel_unlock(ast);
1777 ast_channel_lock(ast);
1778 } else {
1779 /*
1780 * Masquerade is complete:
1781 * Unsuspend the session serializer.
1782 */
1784 }
1785 break;
1786 case AST_CONTROL_HOLD:
1788 device_buf_size = strlen(ast_channel_name(ast)) + 1;
1789 device_buf = alloca(device_buf_size);
1790 ast_channel_get_device_name(ast, device_buf, device_buf_size);
1792 if (!channel->session->moh_passthrough) {
1793 ast_moh_start(ast, data, NULL);
1794 } else {
1796 ast_log(LOG_WARNING, "Could not queue task to remotely put session '%s' on hold with endpoint '%s'\n",
1798 ao2_ref(channel->session, -1);
1799 }
1800 }
1801 break;
1802 case AST_CONTROL_UNHOLD:
1804 device_buf_size = strlen(ast_channel_name(ast)) + 1;
1805 device_buf = alloca(device_buf_size);
1806 ast_channel_get_device_name(ast, device_buf, device_buf_size);
1808 if (!channel->session->moh_passthrough) {
1809 ast_moh_stop(ast);
1810 } else {
1812 ast_log(LOG_WARNING, "Could not queue task to remotely take session '%s' off hold with endpoint '%s'\n",
1814 ao2_ref(channel->session, -1);
1815 }
1816 }
1817 break;
1819 break;
1821 break;
1823 if (ast_channel_state(ast) != AST_STATE_UP) {
1824 response_code = 181;
1825 } else {
1826 res = -1;
1827 }
1828 break;
1830 res = 0;
1831
1832 if (channel->session->t38state == T38_PEER_REINVITE) {
1833 const struct ast_control_t38_parameters *parameters = data;
1834
1835 if (parameters->request_response == AST_T38_REQUEST_PARMS) {
1837 }
1838 }
1839
1840 break;
1842 topology = data;
1843 ast_trace(-1, "%s: New topology: %s\n", ast_channel_name(ast),
1844 ast_str_tmp(256, ast_stream_topology_to_str(topology, &STR_TMP)));
1845 res = handle_topology_request_change(channel->session, topology);
1846 break;
1848 break;
1850 break;
1851 case -1:
1852 res = -1;
1853 break;
1854 default:
1855 ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", condition);
1856 res = -1;
1857 break;
1858 }
1859
1860 if (response_code) {
1861 struct indicate_data *ind_data = indicate_data_alloc(channel->session, condition, response_code, data, datalen);
1862
1863 if (!ind_data) {
1864 SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: Couldn't alloc indicate data\n", ast_channel_name(ast));
1865 }
1866
1867 if (ast_sip_push_task(channel->session->serializer, indicate, ind_data)) {
1868 ast_log(LOG_ERROR, "%s: Cannot send response code %d to endpoint %s. Could not queue task properly\n",
1870 ao2_cleanup(ind_data);
1871 res = -1;
1872 }
1873 }
1874
1875 SCOPE_EXIT_RTN_VALUE(res, "%s\n", ast_channel_name(ast));
1876}
1877
1880 char *target;
1881};
1882
1883static void transfer_data_destroy(void *obj)
1884{
1885 struct transfer_data *trnf_data = obj;
1886
1887 ast_free(trnf_data->target);
1888 ao2_cleanup(trnf_data->session);
1889}
1890
1892{
1893 struct transfer_data *trnf_data = ao2_alloc(sizeof(*trnf_data), transfer_data_destroy);
1894
1895 if (!trnf_data) {
1896 return NULL;
1897 }
1898
1899 if (!(trnf_data->target = ast_strdup(target))) {
1900 ao2_ref(trnf_data, -1);
1901 return NULL;
1902 }
1903
1904 ao2_ref(session, +1);
1905 trnf_data->session = session;
1906
1907 return trnf_data;
1908}
1909
1910static void transfer_redirect(struct ast_sip_session *session, const char *target)
1911{
1912 pjsip_tx_data *packet;
1914 pjsip_contact_hdr *contact;
1915 pj_str_t tmp;
1916
1917 if (pjsip_inv_end_session(session->inv_session, 302, NULL, &packet) != PJ_SUCCESS
1918 || !packet) {
1919 ast_log(LOG_WARNING, "Failed to redirect PJSIP session for channel %s\n",
1920 ast_channel_name(session->channel));
1923
1924 return;
1925 }
1926
1927 if (!(contact = pjsip_msg_find_hdr(packet->msg, PJSIP_H_CONTACT, NULL))) {
1928 contact = pjsip_contact_hdr_create(packet->pool);
1929 }
1930
1931 pj_strdup2_with_null(packet->pool, &tmp, target);
1932 if (!(contact->uri = pjsip_parse_uri(packet->pool, tmp.ptr, tmp.slen, PJSIP_PARSE_URI_AS_NAMEADDR))) {
1933 ast_log(LOG_WARNING, "Failed to parse destination URI '%s' for channel %s\n",
1934 target, ast_channel_name(session->channel));
1937 pjsip_tx_data_dec_ref(packet);
1938
1939 return;
1940 }
1941 pjsip_msg_add_hdr(packet->msg, (pjsip_hdr *) contact);
1942
1945}
1946
1947/*! \brief REFER Callback module, used to attach session data structure to subscription */
1948static pjsip_module refer_callback_module = {
1949 .name = { "REFER Callback", 14 },
1950 .id = -1,
1951};
1952
1953/*!
1954 * \brief Callback function to report status of implicit REFER-NOTIFY subscription.
1955 *
1956 * This function will be called on any state change in the REFER-NOTIFY subscription.
1957 * Its primary purpose is to report SUCCESS/FAILURE of a transfer initiated via
1958 * \ref transfer_refer as well as to terminate the subscription, if necessary.
1959 */
1960static void xfer_client_on_evsub_state(pjsip_evsub *sub, pjsip_event *event)
1961{
1962 struct ast_channel *chan;
1964 int res = 0;
1965
1966 if (!event) {
1967 return;
1968 }
1969
1970 chan = pjsip_evsub_get_mod_data(sub, refer_callback_module.id);
1971 if (!chan) {
1972 return;
1973 }
1974
1975 if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_ACCEPTED) {
1976 /* Check if subscription is suppressed and terminate and send completion code, if so. */
1977 pjsip_rx_data *rdata;
1978 pjsip_generic_string_hdr *refer_sub;
1979 const pj_str_t REFER_SUB = { "Refer-Sub", 9 };
1980
1981 ast_debug(3, "Transfer accepted on channel %s\n", ast_channel_name(chan));
1982
1983 /* Check if response message */
1984 if (event->type == PJSIP_EVENT_TSX_STATE && event->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
1985 rdata = event->body.tsx_state.src.rdata;
1986
1987 /* Find Refer-Sub header */
1988 refer_sub = pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &REFER_SUB, NULL);
1989
1990 /* Check if subscription is suppressed. If it is, the far end will not terminate it,
1991 * and the subscription will remain active until it times out. Terminating it here
1992 * eliminates the unnecessary timeout.
1993 */
1994 if (refer_sub && !pj_stricmp2(&refer_sub->hvalue, "false")) {
1995 /* Since no subscription is desired, assume that call has been transferred successfully. */
1996 /* Channel reference will be released at end of function */
1997 /* Terminate subscription. */
1998 pjsip_evsub_set_mod_data(sub, refer_callback_module.id, NULL);
1999 pjsip_evsub_terminate(sub, PJ_TRUE);
2000 res = -1;
2001 }
2002 }
2003 } else if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_ACTIVE ||
2004 pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) {
2005 /* Check for NOTIFY complete or error. */
2006 pjsip_msg *msg;
2007 pjsip_msg_body *body;
2008 pjsip_status_line status_line = { .code = 0 };
2009 pj_bool_t is_last;
2010 pj_status_t status;
2011
2012 if (event->type == PJSIP_EVENT_TSX_STATE && event->body.tsx_state.type == PJSIP_EVENT_RX_MSG) {
2013 pjsip_rx_data *rdata;
2014
2015 rdata = event->body.tsx_state.src.rdata;
2016 msg = rdata->msg_info.msg;
2017
2018 if (msg->type == PJSIP_REQUEST_MSG) {
2019 if (!pjsip_method_cmp(&msg->line.req.method, pjsip_get_notify_method())) {
2020 body = msg->body;
2021 if (body && !pj_stricmp2(&body->content_type.type, "message")
2022 && !pj_stricmp2(&body->content_type.subtype, "sipfrag")) {
2023 pjsip_parse_status_line((char *)body->data, body->len, &status_line);
2024 }
2025 }
2026 } else {
2027 status_line.code = msg->line.status.code;
2028 status_line.reason = msg->line.status.reason;
2029 }
2030 } else {
2031 status_line.code = 500;
2032 status_line.reason = *pjsip_get_status_text(500);
2033 }
2034
2035 is_last = (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED);
2036 /* If the status code is >= 200, the subscription is finished. */
2037 if (status_line.code >= 200 || is_last) {
2038 res = -1;
2039
2040 /* If the subscription has terminated, return AST_TRANSFER_SUCCESS for 2XX.
2041 * Return AST_TRANSFER_FAILED for any code < 200.
2042 * Otherwise, return the status code.
2043 * The subscription should not terminate for any code < 200,
2044 * but if it does, that constitutes a failure. */
2045 if (status_line.code < 200) {
2047 } else if (status_line.code >= 300) {
2048 message = status_line.code;
2049 }
2050
2051 /* If subscription not terminated and subscription is finished (status code >= 200)
2052 * terminate it */
2053 if (!is_last) {
2054 pjsip_tx_data *tdata;
2055
2056 status = pjsip_evsub_initiate(sub, pjsip_get_subscribe_method(), 0, &tdata);
2057 if (status == PJ_SUCCESS) {
2058 pjsip_evsub_send_request(sub, tdata);
2059 }
2060 }
2061 /* Finished. Remove session from subscription */
2062 pjsip_evsub_set_mod_data(sub, refer_callback_module.id, NULL);
2063 ast_debug(3, "Transfer channel %s completed: %d %.*s (%s)\n",
2064 ast_channel_name(chan),
2065 status_line.code,
2066 (int)status_line.reason.slen, status_line.reason.ptr,
2067 (message == AST_TRANSFER_SUCCESS) ? "Success" : "Failure");
2068 }
2069 }
2070
2071 if (res) {
2073 ao2_ref(chan, -1);
2074 }
2075}
2076
2077static void transfer_refer(struct ast_sip_session *session, const char *target)
2078{
2079 pjsip_evsub *sub;
2081 pj_str_t tmp;
2082 pjsip_tx_data *packet;
2083 const char *ref_by_val;
2084 char local_info[pj_strlen(&session->inv_session->dlg->local.info_str) + 1];
2085 struct pjsip_evsub_user xfer_cb;
2086 struct ast_channel *chan = session->channel;
2087
2088 pj_bzero(&xfer_cb, sizeof(xfer_cb));
2089 xfer_cb.on_evsub_state = &xfer_client_on_evsub_state;
2090
2091 if (pjsip_xfer_create_uac(session->inv_session->dlg, &xfer_cb, &sub) != PJ_SUCCESS) {
2094
2095 return;
2096 }
2097
2098 /* refer_callback_module requires a reference to chan
2099 * which will be released in xfer_client_on_evsub_state()
2100 * when the implicit REFER subscription terminates */
2101 pjsip_evsub_set_mod_data(sub, refer_callback_module.id, chan);
2102 ao2_ref(chan, +1);
2103
2104 if (pjsip_xfer_initiate(sub, pj_cstr(&tmp, target), &packet) != PJ_SUCCESS) {
2105 goto failure;
2106 }
2107
2108 ref_by_val = pbx_builtin_getvar_helper(chan, "SIPREFERREDBYHDR");
2109 if (!ast_strlen_zero(ref_by_val)) {
2110 ast_sip_add_header(packet, "Referred-By", ref_by_val);
2111 } else {
2112 ast_copy_pj_str(local_info, &session->inv_session->dlg->local.info_str, sizeof(local_info));
2113 ast_sip_add_header(packet, "Referred-By", local_info);
2114 }
2115
2116 if (pjsip_xfer_send_request(sub, packet) == PJ_SUCCESS) {
2117 return;
2118 }
2119
2120failure:
2123 pjsip_evsub_set_mod_data(sub, refer_callback_module.id, NULL);
2124 pjsip_evsub_terminate(sub, PJ_FALSE);
2125
2126 ao2_ref(chan, -1);
2127}
2128
2129static int transfer(void *data)
2130{
2131 struct transfer_data *trnf_data = data;
2132 struct ast_sip_endpoint *endpoint = NULL;
2133 struct ast_sip_contact *contact = NULL;
2134 const char *target = trnf_data->target;
2135
2136 if (trnf_data->session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
2137 ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
2138 trnf_data->session->inv_session->cause,
2139 pjsip_get_status_text(trnf_data->session->inv_session->cause)->ptr);
2140 } else {
2141 /* See if we have an endpoint; if so, use its contact */
2143 if (endpoint) {
2145 if (contact && !ast_strlen_zero(contact->uri)) {
2146 target = contact->uri;
2147 }
2148 }
2149
2150 if (ast_channel_state(trnf_data->session->channel) == AST_STATE_RING) {
2151 transfer_redirect(trnf_data->session, target);
2152 } else {
2153 transfer_refer(trnf_data->session, target);
2154 }
2155 }
2156
2157 ao2_ref(trnf_data, -1);
2159 ao2_cleanup(contact);
2160 return 0;
2161}
2162
2163/*! \brief Function called by core for Asterisk initiated transfer */
2164static int chan_pjsip_transfer(struct ast_channel *chan, const char *target)
2165{
2166 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
2167 struct transfer_data *trnf_data = transfer_data_alloc(channel->session, target);
2168
2169 if (!trnf_data) {
2170 return -1;
2171 }
2172
2173 if (ast_sip_push_task(channel->session->serializer, transfer, trnf_data)) {
2174 ast_log(LOG_WARNING, "Error requesting transfer\n");
2175 ao2_cleanup(trnf_data);
2176 return -1;
2177 }
2178
2179 return 0;
2180}
2181
2182/*! \brief Function called by core to start a DTMF digit */
2183static int chan_pjsip_digit_begin(struct ast_channel *chan, char digit)
2184{
2185 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
2186 struct ast_sip_session_media *media;
2187
2189
2190 switch (channel->session->dtmf) {
2192 if (!media || !media->rtp) {
2193 return 0;
2194 }
2195
2197 break;
2198 case AST_SIP_DTMF_AUTO:
2199 if (!media || !media->rtp) {
2200 return 0;
2201 }
2202
2204 return -1;
2205 }
2206
2208 break;
2210 if (!media || !media->rtp || (ast_rtp_instance_dtmf_mode_get(media->rtp) == AST_RTP_DTMF_MODE_NONE)) {
2211 return 0;
2212 }
2214 break;
2215 case AST_SIP_DTMF_NONE:
2216 break;
2218 return -1;
2219 default:
2220 break;
2221 }
2222
2223 return 0;
2224}
2225
2228 char digit;
2229 unsigned int duration;
2230};
2231
2232static void info_dtmf_data_destroy(void *obj)
2233{
2234 struct info_dtmf_data *dtmf_data = obj;
2235 ao2_ref(dtmf_data->session, -1);
2236}
2237
2239{
2240 struct info_dtmf_data *dtmf_data = ao2_alloc(sizeof(*dtmf_data), info_dtmf_data_destroy);
2241 if (!dtmf_data) {
2242 return NULL;
2243 }
2244 ao2_ref(session, +1);
2245 dtmf_data->session = session;
2246 dtmf_data->digit = digit;
2247 dtmf_data->duration = duration;
2248 return dtmf_data;
2249}
2250
2251static int transmit_info_dtmf(void *data)
2252{
2253 RAII_VAR(struct info_dtmf_data *, dtmf_data, data, ao2_cleanup);
2254
2255 struct ast_sip_session *session = dtmf_data->session;
2256 struct pjsip_tx_data *tdata;
2257
2258 RAII_VAR(struct ast_str *, body_text, NULL, ast_free_ptr);
2259
2260 struct ast_sip_body body = {
2261 .type = "application",
2262 .subtype = "dtmf-relay",
2263 };
2264
2265 if (session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
2266 ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
2267 session->inv_session->cause,
2268 pjsip_get_status_text(session->inv_session->cause)->ptr);
2269 return -1;
2270 }
2271
2272 if (!(body_text = ast_str_create(32))) {
2273 ast_log(LOG_ERROR, "Could not allocate buffer for INFO DTMF.\n");
2274 return -1;
2275 }
2276 ast_str_set(&body_text, 0, "Signal=%c\r\nDuration=%u\r\n", dtmf_data->digit, dtmf_data->duration);
2277
2279
2280 if (ast_sip_create_request("INFO", session->inv_session->dlg, session->endpoint, NULL, NULL, &tdata)) {
2281 ast_log(LOG_ERROR, "Could not create DTMF INFO request\n");
2282 return -1;
2283 }
2284 if (ast_sip_add_body(tdata, &body)) {
2285 ast_log(LOG_ERROR, "Could not add body to DTMF INFO request\n");
2286 pjsip_tx_data_dec_ref(tdata);
2287 return -1;
2288 }
2290
2291 return 0;
2292}
2293
2294/*! \brief Function called by core to stop a DTMF digit */
2295static int chan_pjsip_digit_end(struct ast_channel *ast, char digit, unsigned int duration)
2296{
2297 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
2298 struct ast_sip_session_media *media;
2299
2300 if (!channel || !channel->session) {
2301 /* This happens when the channel is hungup while a DTMF digit is playing. See ASTERISK-28086 */
2302 ast_debug(3, "Channel %s disappeared while calling digit_end\n", ast_channel_name(ast));
2303 return -1;
2304 }
2305
2307
2308 switch (channel->session->dtmf) {
2310 {
2311 if (!media || !media->rtp) {
2312 return 0;
2313 }
2314
2316 ast_debug(3, "Told to send end of digit on Auto-Info channel %s RFC4733 negotiated so using it.\n", ast_channel_name(ast));
2318 break;
2319 }
2320 /* If RFC_4733 was not negotiated, fail through to the DTMF_INFO processing */
2321 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));
2322 }
2323
2324 case AST_SIP_DTMF_INFO:
2325 {
2326 struct info_dtmf_data *dtmf_data = info_dtmf_data_alloc(channel->session, digit, duration);
2327
2328 if (!dtmf_data) {
2329 return -1;
2330 }
2331
2332 if (ast_sip_push_task(channel->session->serializer, transmit_info_dtmf, dtmf_data)) {
2333 ast_log(LOG_WARNING, "Error sending DTMF via INFO.\n");
2334 ao2_cleanup(dtmf_data);
2335 return -1;
2336 }
2337 break;
2338 }
2340 if (!media || !media->rtp) {
2341 return 0;
2342 }
2343
2345 break;
2346 case AST_SIP_DTMF_AUTO:
2347 if (!media || !media->rtp) {
2348 return 0;
2349 }
2350
2352 return -1;
2353 }
2354
2356 break;
2357 case AST_SIP_DTMF_NONE:
2358 break;
2360 return -1;
2361 }
2362
2363 return 0;
2364}
2365
2367{
2369
2370 /*
2371 * Use the channel CALLERID() as the initial connected line data.
2372 * The core or a predial handler may have supplied missing values
2373 * from the session->endpoint->id.self about who we are calling.
2374 */
2375 ast_channel_lock(session->channel);
2377 ast_channel_unlock(session->channel);
2378
2379 /* Supply initial connected line information if available. */
2380 if (!session->id.number.valid && !session->id.name.valid) {
2381 return;
2382 }
2383
2385 connected.id = session->id;
2387
2389}
2390
2391static int call(void *data)
2392{
2393 struct ast_sip_channel_pvt *channel = data;
2394 struct ast_sip_session *session = channel->session;
2395 pjsip_tx_data *tdata;
2396 int res = 0;
2397 SCOPE_ENTER(1, "%s Topology: %s\n",
2399 ast_str_tmp(256, ast_stream_topology_to_str(channel->session->pending_media_state->topology, &STR_TMP))
2400 );
2401
2402
2404
2405 if (res) {
2406 ast_set_hangupsource(session->channel, ast_channel_name(session->channel), 0);
2407 ast_queue_hangup(session->channel);
2408 } else {
2412 }
2413 ao2_ref(channel, -1);
2414 SCOPE_EXIT_RTN_VALUE(res, "RC: %d\n", res);
2415}
2416
2417/*! \brief Function called by core to actually start calling a remote party */
2418static int chan_pjsip_call(struct ast_channel *ast, const char *dest, int timeout)
2419{
2420 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
2421 SCOPE_ENTER(1, "%s Topology: %s\n", ast_sip_session_get_name(channel->session),
2423
2424 ao2_ref(channel, +1);
2425 if (ast_sip_push_task(channel->session->serializer, call, channel)) {
2426 ast_log(LOG_WARNING, "Error attempting to place outbound call to '%s'\n", dest);
2427 ao2_cleanup(channel);
2428 SCOPE_EXIT_RTN_VALUE(-1, "Couldn't push task\n");
2429 }
2430
2431 SCOPE_EXIT_RTN_VALUE(0, "'call' task pushed\n");
2432}
2433
2434/*! \brief Internal function which translates from Asterisk cause codes to SIP response codes */
2435static int hangup_cause2sip(int cause)
2436{
2437 switch (cause) {
2438 case AST_CAUSE_UNALLOCATED: /* 1 */
2439 case AST_CAUSE_NO_ROUTE_DESTINATION: /* 3 IAX2: Can't find extension in context */
2440 case AST_CAUSE_NO_ROUTE_TRANSIT_NET: /* 2 */
2441 return 404;
2442 case AST_CAUSE_CONGESTION: /* 34 */
2443 case AST_CAUSE_SWITCH_CONGESTION: /* 42 */
2444 return 503;
2445 case AST_CAUSE_NO_USER_RESPONSE: /* 18 */
2446 return 408;
2447 case AST_CAUSE_NO_ANSWER: /* 19 */
2448 case AST_CAUSE_UNREGISTERED: /* 20 */
2449 return 480;
2450 case AST_CAUSE_CALL_REJECTED: /* 21 */
2451 return 403;
2452 case AST_CAUSE_NUMBER_CHANGED: /* 22 */
2453 return 410;
2454 case AST_CAUSE_NORMAL_UNSPECIFIED: /* 31 */
2455 return 480;
2457 return 484;
2459 return 486;
2460 case AST_CAUSE_FAILURE:
2461 return 500;
2462 case AST_CAUSE_FACILITY_REJECTED: /* 29 */
2463 return 501;
2465 return 503;
2467 return 502;
2468 case AST_CAUSE_BEARERCAPABILITY_NOTAVAIL: /* Can't find codec to connect to host */
2469 return 488;
2470 case AST_CAUSE_INTERWORKING: /* Unspecified Interworking issues */
2471 return 500;
2473 default:
2474 ast_debug(1, "AST hangup cause %d (no match found in PJSIP)\n", cause);
2475 return 0;
2476 }
2477
2478 /* Never reached */
2479 return 0;
2480}
2481
2482struct hangup_data {
2485};
2486
2487static void hangup_data_destroy(void *obj)
2488{
2489 struct hangup_data *h_data = obj;
2490
2491 h_data->chan = ast_channel_unref(h_data->chan);
2492}
2493
2495{
2496 struct hangup_data *h_data = ao2_alloc(sizeof(*h_data), hangup_data_destroy);
2497
2498 if (!h_data) {
2499 return NULL;
2500 }
2501
2502 h_data->cause = cause;
2503 h_data->chan = ast_channel_ref(chan);
2504
2505 return h_data;
2506}
2507
2508/*! \brief Clear a channel from a session along with its PVT */
2510{
2511 session->channel = NULL;
2514}
2515
2516static int hangup(void *data)
2517{
2518 struct hangup_data *h_data = data;
2519 struct ast_channel *ast = h_data->chan;
2520 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
2521 SCOPE_ENTER(1, "%s\n", ast_channel_name(ast));
2522
2523 /*
2524 * Before cleaning we have to ensure that channel or its session is not NULL
2525 * we have seen rare case when taskprocessor calls hangup but channel is NULL
2526 * due to SIP session timeout and answer happening at the same time
2527 */
2528 if (channel) {
2529 struct ast_sip_session *session = channel->session;
2530 if (session) {
2531 int cause = h_data->cause;
2532
2533 if (channel->session->active_media_state &&
2534 channel->session->active_media_state->default_session[AST_MEDIA_TYPE_AUDIO]) {
2535 struct ast_sip_session_media *media =
2537 if (media->rtp) {
2539 }
2540 }
2541
2542 /*
2543 * It's possible that session_terminate might cause the session to be destroyed
2544 * immediately so we need to keep a reference to it so we can NULL session->channel
2545 * afterwards.
2546 */
2550 }
2551 ao2_cleanup(channel);
2552 }
2553 ao2_cleanup(h_data);
2555}
2556
2557/*! \brief Function called by core to hang up a PJSIP session */
2558static int chan_pjsip_hangup(struct ast_channel *ast)
2559{
2560 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
2561 int cause;
2562 struct hangup_data *h_data;
2563 SCOPE_ENTER(1, "%s\n", ast_channel_name(ast));
2564
2565 if (!channel || !channel->session) {
2566 SCOPE_EXIT_RTN_VALUE(-1, "No channel or session\n");
2567 }
2568
2570 h_data = hangup_data_alloc(cause, ast);
2571
2572 if (!h_data) {
2573 goto failure;
2574 }
2575
2576 if (ast_sip_push_task(channel->session->serializer, hangup, h_data)) {
2577 ast_log(LOG_WARNING, "Unable to push hangup task to the threadpool. Expect bad things\n");
2578 goto failure;
2579 }
2580
2581 SCOPE_EXIT_RTN_VALUE(0, "Cause: %d\n", cause);
2582
2583failure:
2584 /* Go ahead and do our cleanup of the session and channel even if we're not going
2585 * to be able to send our SIP request/response
2586 */
2587 clear_session_and_channel(channel->session, ast);
2588 ao2_cleanup(channel);
2589 ao2_cleanup(h_data);
2590
2591 SCOPE_EXIT_RTN_VALUE(-1, "Cause: %d\n", cause);
2592}
2593
2597 const char *dest;
2599};
2600
2601static int request(void *obj)
2602{
2603 struct request_data *req_data = obj;
2604 struct ast_sip_session *session = NULL;
2605 char *tmp = ast_strdupa(req_data->dest), *endpoint_name = NULL, *request_user = NULL;
2606 struct ast_sip_endpoint *endpoint;
2607
2609 AST_APP_ARG(endpoint);
2610 AST_APP_ARG(aor);
2611 );
2612 SCOPE_ENTER(1, "%s\n",tmp);
2613
2614 if (ast_strlen_zero(tmp)) {
2615 ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty destination\n");
2617 SCOPE_EXIT_RTN_VALUE(-1, "Empty destination\n");
2618 }
2619
2621
2623 /* If a request user has been specified extract it from the endpoint name portion */
2624 if ((endpoint_name = strchr(args.endpoint, '@'))) {
2625 request_user = args.endpoint;
2626 *endpoint_name++ = '\0';
2627 } else {
2628 endpoint_name = args.endpoint;
2629 }
2630
2631 if (ast_strlen_zero(endpoint_name)) {
2632 if (request_user) {
2633 ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty endpoint name: %s@<endpoint-name>\n",
2634 request_user);
2635 } else {
2636 ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty endpoint name\n");
2637 }
2639 SCOPE_EXIT_RTN_VALUE(-1, "Empty endpoint name\n");
2640 }
2641 endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint",
2642 endpoint_name);
2643 if (!endpoint) {
2644 ast_log(LOG_ERROR, "Unable to create PJSIP channel - endpoint '%s' was not found\n", endpoint_name);
2646 SCOPE_EXIT_RTN_VALUE(-1, "Endpoint not found\n");
2647 }
2648 } else {
2649 /* First try to find an exact endpoint match, for single (user) or multi-domain (user@domain) */
2650 endpoint_name = args.endpoint;
2651 if (ast_strlen_zero(endpoint_name)) {
2652 ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty endpoint name\n");
2654 SCOPE_EXIT_RTN_VALUE(-1, "Empty endpoint name\n");
2655 }
2656 endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint",
2657 endpoint_name);
2658 if (!endpoint) {
2659 /* It seems it's not a multi-domain endpoint or single endpoint exact match,
2660 * it's possible that it's a SIP trunk with a specified user (user@trunkname),
2661 * so extract the user before @ sign.
2662 */
2663 endpoint_name = strchr(args.endpoint, '@');
2664 if (!endpoint_name) {
2665 /*
2666 * Couldn't find an '@' so it had to be an endpoint
2667 * name that doesn't exist.
2668 */
2669 ast_log(LOG_ERROR, "Unable to create PJSIP channel - endpoint '%s' was not found\n",
2670 args.endpoint);
2672 SCOPE_EXIT_RTN_VALUE(-1, "Endpoint not found\n");
2673 }
2674 request_user = args.endpoint;
2675 *endpoint_name++ = '\0';
2676
2677 if (ast_strlen_zero(endpoint_name)) {
2678 ast_log(LOG_ERROR, "Unable to create PJSIP channel with empty endpoint name: %s@<endpoint-name>\n",
2679 request_user);
2681 SCOPE_EXIT_RTN_VALUE(-1, "Empty endpoint name\n");
2682 }
2683
2684 endpoint = ast_sorcery_retrieve_by_id(ast_sip_get_sorcery(), "endpoint",
2685 endpoint_name);
2686 if (!endpoint) {
2687 ast_log(LOG_ERROR, "Unable to create PJSIP channel - endpoint '%s' was not found\n", endpoint_name);
2689 SCOPE_EXIT_RTN_VALUE(-1, "Endpoint not found\n");
2690 }
2691 }
2692 }
2693
2694 session = ast_sip_session_create_outgoing(endpoint, NULL, args.aor, request_user,
2695 req_data->topology);
2696 ao2_ref(endpoint, -1);
2697 if (!session) {
2698 ast_log(LOG_ERROR, "Failed to create outgoing session to endpoint '%s'\n", endpoint_name);
2700 SCOPE_EXIT_RTN_VALUE(-1, "Couldn't create session\n");
2701 }
2702
2703 req_data->session = session;
2704
2706}
2707
2708/*! \brief Function called by core to create a new outgoing PJSIP session */
2709static 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)
2710{
2711 struct request_data req_data;
2713 SCOPE_ENTER(1, "%s Topology: %s\n", data,
2715
2716 req_data.topology = topology;
2717 req_data.dest = data;
2718 /* Default failure value in case ast_sip_push_task_wait_servant() itself fails. */
2719 req_data.cause = AST_CAUSE_FAILURE;
2720
2721 if (ast_sip_push_task_wait_servant(NULL, request, &req_data)) {
2722 *cause = req_data.cause;
2723 SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't push task\n");
2724 }
2725
2726 session = req_data.session;
2727
2728 if (!(session->channel = chan_pjsip_new(session, AST_STATE_DOWN, NULL, NULL, assignedids, requestor, NULL))) {
2729 /* Session needs to be terminated prematurely */
2730 SCOPE_EXIT_RTN_VALUE(NULL, "Couldn't create channel\n");
2731 }
2732
2733 SCOPE_EXIT_RTN_VALUE(session->channel, "Channel: %s\n", ast_channel_name(session->channel));
2734}
2735
2736static 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)
2737{
2738 struct ast_stream_topology *topology;
2739 struct ast_channel *chan;
2740
2742 if (!topology) {
2743 return NULL;
2744 }
2745
2746 chan = chan_pjsip_request_with_stream_topology(type, topology, assignedids, requestor, data, cause);
2747
2748 ast_stream_topology_free(topology);
2749
2750 return chan;
2751}
2752
2756};
2757
2758static void sendtext_data_destroy(void *obj)
2759{
2760 struct sendtext_data *data = obj;
2761 ao2_cleanup(data->session);
2762 ast_free(data->msg);
2763}
2764
2766 struct ast_msg_data *msg)
2767{
2768 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(chan);
2769 struct sendtext_data *data = ao2_alloc(sizeof(*data), sendtext_data_destroy);
2770
2771 if (!data) {
2772 return NULL;
2773 }
2774
2775 data->msg = ast_msg_data_dup(msg);
2776 if (!data->msg) {
2777 ao2_cleanup(data);
2778 return NULL;
2779 }
2780 data->session = channel->session;
2781 ao2_ref(data->session, +1);
2782
2783 return data;
2784}
2785
2786static int sendtext(void *obj)
2787{
2788 struct sendtext_data *data = obj;
2789 pjsip_tx_data *tdata;
2790 const char *body_text = ast_msg_data_get_attribute(data->msg, AST_MSG_DATA_ATTR_BODY);
2791 const char *content_type = ast_msg_data_get_attribute(data->msg, AST_MSG_DATA_ATTR_CONTENT_TYPE);
2792 char *sep;
2793 struct ast_sip_body body = {
2794 .type = "text",
2795 .subtype = "plain",
2796 .body_text = body_text,
2797 };
2798
2799 if (!ast_strlen_zero(content_type)) {
2800 sep = strchr(content_type, '/');
2801 if (sep) {
2802 *sep = '\0';
2803 body.type = content_type;
2804 body.subtype = ++sep;
2805 }
2806 }
2807
2808 if (data->session->inv_session->state == PJSIP_INV_STATE_DISCONNECTED) {
2809 ast_log(LOG_ERROR, "Session already DISCONNECTED [reason=%d (%s)]\n",
2810 data->session->inv_session->cause,
2811 pjsip_get_status_text(data->session->inv_session->cause)->ptr);
2812 } else {
2813 pjsip_from_hdr *hdr;
2814 pjsip_name_addr *name_addr;
2815 const char *from = ast_msg_data_get_attribute(data->msg, AST_MSG_DATA_ATTR_FROM);
2816 const char *to = ast_msg_data_get_attribute(data->msg, AST_MSG_DATA_ATTR_TO);
2817 int invalidate_tdata = 0;
2818
2819 ast_sip_create_request("MESSAGE", data->session->inv_session->dlg, data->session->endpoint, NULL, NULL, &tdata);
2820 ast_sip_add_body(tdata, &body);
2821
2822 /*
2823 * If we have a 'from' in the msg, set the display name in the From
2824 * header to it.
2825 */
2826 if (!ast_strlen_zero(from)) {
2827 hdr = PJSIP_MSG_FROM_HDR(tdata->msg);
2828 name_addr = (pjsip_name_addr *) hdr->uri;
2829 pj_strdup2(tdata->pool, &name_addr->display, from);
2830 invalidate_tdata = 1;
2831 }
2832
2833 /*
2834 * If we have a 'to' in the msg, set the display name in the To
2835 * header to it.
2836 */
2837 if (!ast_strlen_zero(to)) {
2838 hdr = PJSIP_MSG_TO_HDR(tdata->msg);
2839 name_addr = (pjsip_name_addr *) hdr->uri;
2840 pj_strdup2(tdata->pool, &name_addr->display, to);
2841 invalidate_tdata = 1;
2842 }
2843
2844 if (invalidate_tdata) {
2845 pjsip_tx_data_invalidate_msg(tdata);
2846 }
2847
2848 ast_sip_send_request(tdata, data->session->inv_session->dlg, data->session->endpoint, NULL, NULL);
2849 }
2850
2851 ao2_cleanup(data);
2852
2853 return 0;
2854}
2855
2856/*! \brief Function called by core to send text on PJSIP session */
2857static int chan_pjsip_sendtext_data(struct ast_channel *ast, struct ast_msg_data *msg)
2858{
2859 struct ast_sip_channel_pvt *channel = ast_channel_tech_pvt(ast);
2860 struct sendtext_data *data = sendtext_data_create(ast, msg);
2861
2862 ast_debug(1, "Sending MESSAGE from '%s' to '%s:%s': %s\n",
2865 ast_channel_name(ast),
2867
2868 if (!data) {
2869 return -1;
2870 }
2871
2872 if (ast_sip_push_task(channel->session->serializer, sendtext, data)) {
2873 ao2_ref(data, -1);
2874 return -1;
2875 }
2876 return 0;
2877}
2878
2879static int chan_pjsip_sendtext(struct ast_channel *ast, const char *text)
2880{
2881 struct ast_msg_data *msg;
2882 int rc;
2883 struct ast_msg_data_attribute attrs[] =
2884 {
2885 {
2887 .value = (char *)text,
2888 }
2889 };
2890
2892 if (!msg) {
2893 return -1;
2894 }
2895 rc = chan_pjsip_sendtext_data(ast, msg);
2896 ast_free(msg);
2897
2898 return rc;
2899}
2900
2902{
2903 RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
2905
2906 if (session->endpoint->media.direct_media.glare_mitigation ==
2908 SCOPE_EXIT_RTN("Direct media no glare mitigation\n");
2909 }
2910
2912 "direct_media_glare_mitigation");
2913
2914 if (!datastore) {
2915 SCOPE_EXIT_RTN("Couldn't create datastore\n");
2916 }
2917
2920}
2921
2922/*! \brief Function called when the session ends */
2924{
2926
2927 if (!session->channel) {
2928 SCOPE_EXIT_RTN("No channel\n");
2929 }
2930
2931
2932 if (session->active_media_state &&
2933 session->active_media_state->default_session[AST_MEDIA_TYPE_AUDIO]) {
2934 struct ast_sip_session_media *media =
2935 session->active_media_state->default_session[AST_MEDIA_TYPE_AUDIO];
2936 if (media->rtp) {
2938 }
2939 }
2940
2942
2943 ast_set_hangupsource(session->channel, ast_channel_name(session->channel), 0);
2944 if (!ast_channel_hangupcause(session->channel) && session->inv_session) {
2945 int cause = ast_sip_hangup_sip2cause(session->inv_session->cause);
2946
2947 ast_queue_hangup_with_cause(session->channel, cause);
2948 } else {
2949 ast_queue_hangup(session->channel);
2950 }
2951
2953}
2954
2956{
2957 const pj_str_t *host = ast_sip_pjsip_uri_get_hostname(session->request_uri);
2958 size_t size = pj_strlen(host) + 1;
2959 char *domain = ast_alloca(size);
2960
2961 ast_copy_pj_str(domain, host, size);
2962
2963 pbx_builtin_setvar_helper(session->channel, "SIPDOMAIN", domain);
2964 return;
2965}
2966
2967/*! \brief Function called when a request is received on the session */
2968static int chan_pjsip_incoming_request(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
2969{
2970 RAII_VAR(struct ast_datastore *, datastore, NULL, ao2_cleanup);
2971 struct transport_info_data *transport_data;
2972 pjsip_tx_data *packet = NULL;
2974
2975 if (session->channel) {
2976 SCOPE_EXIT_RTN_VALUE(0, "%s: No channel\n", ast_sip_session_get_name(session));
2977 }
2978
2979 /* Check for a to-tag to determine if this is a reinvite */
2980 if (rdata->msg_info.to->tag.slen) {
2981 /* Weird case. We've received a reinvite but we don't have a channel. The most
2982 * typical case for this happening is that a blind transfer fails, and so the
2983 * transferer attempts to reinvite himself back into the call. We already got
2984 * rid of that channel, and the other side of the call is unrecoverable.
2985 *
2986 * We treat this as a failure, so our best bet is to just hang this call
2987 * up and not create a new channel. Clearing defer_terminate here ensures that
2988 * calling ast_sip_session_terminate() can result in a BYE being sent ASAP.
2989 */
2990 session->defer_terminate = 0;
2992 SCOPE_EXIT_RTN_VALUE(-1, "%s: We have a To tag but no channel. Terminating session\n", ast_sip_session_get_name(session));
2993 }
2994
2995 datastore = ast_sip_session_alloc_datastore(&transport_info, "transport_info");
2996 if (!datastore) {
2997 SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: Couldn't alloc transport_info datastore\n", ast_sip_session_get_name(session));
2998 }
2999
3000 transport_data = ast_calloc(1, sizeof(*transport_data));
3001 if (!transport_data) {
3002 SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: Couldn't alloc transport_info\n", ast_sip_session_get_name(session));
3003 }
3004 pj_sockaddr_cp(&transport_data->local_addr, &rdata->tp_info.transport->local_addr);
3005 pj_sockaddr_cp(&transport_data->remote_addr, &rdata->pkt_info.src_addr);
3006 datastore->data = transport_data;
3008
3009 if (!(session->channel = chan_pjsip_new(session, AST_STATE_RING, session->exten, NULL, NULL, NULL, NULL))) {
3010 if (pjsip_inv_end_session(session->inv_session, 503, NULL, &packet) == PJ_SUCCESS
3011 && packet) {
3013 }
3014
3015 SCOPE_EXIT_LOG_RTN_VALUE(-1, LOG_ERROR, "%s: Failed to allocate new PJSIP channel on incoming SIP INVITE\n",
3017 }
3018
3020
3021 /* channel gets created on incoming request, but we wait to call start
3022 so other supplements have a chance to run */
3024}
3025
3026static int call_pickup_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
3027{
3028 struct ast_features_pickup_config *pickup_cfg;
3029 struct ast_channel *chan;
3030
3031 /* Check for a to-tag to determine if this is a reinvite */
3032 if (rdata->msg_info.to->tag.slen) {
3033 /* We don't care about reinvites */
3034 return 0;
3035 }
3036
3037 pickup_cfg = ast_get_chan_features_pickup_config(session->channel);
3038 if (!pickup_cfg) {
3039 ast_log(LOG_ERROR, "Unable to retrieve pickup configuration options. Unable to detect call pickup extension.\n");
3040 return 0;
3041 }
3042
3043 if (strcmp(session->exten, pickup_cfg->pickupexten)) {
3044 ao2_ref(pickup_cfg, -1);
3045 return 0;
3046 }
3047 ao2_ref(pickup_cfg, -1);
3048
3049 /* We can't directly use session->channel because the pickup operation will cause a masquerade to occur,
3050 * changing the channel pointer in session to a different channel. To ensure we work on the right channel
3051 * we store a pointer locally before we begin and keep a reference so it remains valid no matter what.
3052 */
3053 chan = ast_channel_ref(session->channel);
3054 if (ast_pickup_call(chan)) {
3056 } else {
3058 }
3059 /* A hangup always occurs because the pickup operation will have either failed resulting in the call
3060 * needing to be hung up OR the pickup operation was a success and the channel we now have is actually
3061 * the channel that was replaced, which should be hung up since it is literally in limbo not connected
3062 * to anything at all.
3063 */
3064 ast_hangup(chan);
3065 ast_channel_unref(chan);
3066
3067 return 1;
3068}
3069
3071 .method = "INVITE",
3072 .priority = AST_SIP_SUPPLEMENT_PRIORITY_LAST - 1,
3073 .incoming_request = call_pickup_incoming_request,
3074};
3075
3076static int pbx_start_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
3077{
3078 int res;
3080
3081 /* Check for a to-tag to determine if this is a reinvite */
3082 if (rdata->msg_info.to->tag.slen) {
3083 /* We don't care about reinvites */
3084 SCOPE_EXIT_RTN_VALUE(0, "Reinvite\n");
3085 }
3086
3087 res = ast_pbx_start(session->channel);
3088
3089 switch (res) {
3090 case AST_PBX_FAILED:
3091 ast_log(LOG_WARNING, "Failed to start PBX ;(\n");
3093 ast_hangup(session->channel);
3094 break;
3095 case AST_PBX_CALL_LIMIT:
3096 ast_log(LOG_WARNING, "Failed to start PBX (call limit reached) \n");
3098 ast_hangup(session->channel);
3099 break;
3100 case AST_PBX_SUCCESS:
3101 default:
3102 break;
3103 }
3104
3105 ast_debug(3, "Started PBX on new PJSIP channel %s\n", ast_channel_name(session->channel));
3106
3107 SCOPE_EXIT_RTN_VALUE((res == AST_PBX_SUCCESS) ? 0 : -1, "RC: %d\n", res);
3108}
3109
3111 .method = "INVITE",
3113 .incoming_request = pbx_start_incoming_request,
3114};
3115
3116/*! \brief Function called when a response is received on the session */
3117static void chan_pjsip_incoming_response_update_cause(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
3118{
3119 struct pjsip_status_line status = rdata->msg_info.msg->line.status;
3120 struct ast_control_pvt_cause_code *cause_code;
3121 int data_size = sizeof(*cause_code);
3122 SCOPE_ENTER(3, "%s: Status: %d\n", ast_sip_session_get_name(session), status.code);
3123
3124 if (!session->channel) {
3125 SCOPE_EXIT_RTN("%s: No channel\n", ast_sip_session_get_name(session));
3126 }
3127
3128 /* Build and send the tech-specific cause information */
3129 /* size of the string making up the cause code is "SIP " number + " " + reason length */
3130 data_size += 4 + 4 + pj_strlen(&status.reason);
3131 cause_code = ast_alloca(data_size);
3132 memset(cause_code, 0, data_size);
3133
3135
3136 snprintf(cause_code->code, data_size - sizeof(*cause_code) + 1, "SIP %d %.*s", status.code,
3137 (int) pj_strlen(&status.reason), pj_strbuf(&status.reason));
3138
3139 cause_code->ast_cause = ast_sip_hangup_sip2cause(status.code);
3140 ast_queue_control_data(session->channel, AST_CONTROL_PVT_CAUSE_CODE, cause_code, data_size);
3141 ast_channel_hangupcause_hash_set(session->channel, cause_code, data_size);
3142
3144}
3145
3146/*! \brief Function called when a response is received on the session */
3147static void chan_pjsip_incoming_response(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
3148{
3149 struct pjsip_status_line status = rdata->msg_info.msg->line.status;
3150 SCOPE_ENTER(3, "%s: Status: %d\n", ast_sip_session_get_name(session), status.code);
3151
3152 if (!session->channel) {
3153 SCOPE_EXIT_RTN("%s: No channel\n", ast_sip_session_get_name(session));
3154 }
3155
3156 switch (status.code) {
3157 case 180: {
3158 pjsip_rdata_sdp_info *sdp = pjsip_rdata_get_sdp_info(rdata);
3159 if (sdp && sdp->body.ptr) {
3160 ast_trace(-1, "%s: Queueing PROGRESS\n", ast_sip_session_get_name(session));
3161 session->early_confirmed = pjsip_100rel_is_reliable(rdata) == PJ_TRUE;
3163 } else {
3164 ast_trace(-1, "%s: Queueing RINGING\n", ast_sip_session_get_name(session));
3166 }
3167
3168 ast_channel_lock(session->channel);
3169 if (ast_channel_state(session->channel) != AST_STATE_UP) {
3171 }
3172 ast_channel_unlock(session->channel);
3173 break;
3174 }
3175 case 183:
3176 if (session->endpoint->ignore_183_without_sdp) {
3177 pjsip_rdata_sdp_info *sdp = pjsip_rdata_get_sdp_info(rdata);
3178 if (sdp && sdp->body.ptr) {
3179 ast_trace(-1, "%s: Queueing PROGRESS\n", ast_sip_session_get_name(session));
3180 ast_trace(1, "%s Method: %.*s Status: %d Queueing PROGRESS with SDP\n", ast_sip_session_get_name(session),
3181 (int)rdata->msg_info.cseq->method.name.slen, rdata->msg_info.cseq->method.name.ptr, status.code);
3182 session->early_confirmed = pjsip_100rel_is_reliable(rdata) == PJ_TRUE;
3184 }
3185 } else {
3186 ast_trace(-1, "%s: Queueing PROGRESS\n", ast_sip_session_get_name(session));
3187 ast_trace(1, "%s Method: %.*s Status: %d Queueing PROGRESS without SDP\n", ast_sip_session_get_name(session),
3188 (int)rdata->msg_info.cseq->method.name.slen, rdata->msg_info.cseq->method.name.ptr, status.code);
3190 }
3191 break;
3192 case 200:
3193 ast_trace(-1, "%s: Queueing ANSWER\n", ast_sip_session_get_name(session));
3195 break;
3196 default:
3197 ast_trace(-1, "%s: Not queueing anything\n", ast_sip_session_get_name(session));
3198 break;
3199 }
3200
3202}
3203
3204static int chan_pjsip_incoming_ack(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
3205{
3207
3208 if (rdata->msg_info.msg->line.req.method.id == PJSIP_ACK_METHOD) {
3209 if (session->endpoint->media.direct_media.enabled && session->channel) {
3210 ast_trace(-1, "%s: Queueing SRCCHANGE\n", ast_sip_session_get_name(session));
3212 }
3213 }
3215}
3216
3217static int chan_pjsip_incoming_prack(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
3218{
3220
3221 if (pj_strcmp2(&rdata->msg_info.msg->line.req.method.name, "PRACK") == 0 &&
3222 pjmedia_sdp_neg_get_state(session->inv_session->neg) == PJMEDIA_SDP_NEG_STATE_DONE) {
3223
3224 session->early_confirmed = 1;
3225 }
3227}
3228
3229static int update_devstate(void *obj, void *arg, int flags)
3230{
3232 "PJSIP/%s", ast_sorcery_object_get_id(obj));
3233 return 0;
3234}
3235
3237 .name = "PJSIP_DIAL_CONTACTS",
3239};
3240
3242 .name = "PJSIP_PARSE_URI",
3244};
3245
3247 .name = "PJSIP_PARSE_URI_FROM",
3249};
3250
3252 .name = "PJSIP_MEDIA_OFFER",
3255};
3256
3258 .name = "PJSIP_DTMF_MODE",
3261};
3262
3264 .name = "PJSIP_MOH_PASSTHROUGH",
3267};
3268
3270 .name = "PJSIP_SEND_SESSION_REFRESH",
3272};
3273
3274static char *app_pjsip_hangup = "PJSIPHangup";
3275
3276/*!
3277 * \brief Load the module
3278 *
3279 * Module loading including tests for configuration or dependencies.
3280 * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
3281 * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
3282 * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
3283 * configuration file or other non-critical problem return
3284 * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
3285 */
3286static int load_module(void)
3287{
3288 struct ao2_container *endpoints;
3289
3292 }
3293
3295
3297
3299 ast_log(LOG_ERROR, "Unable to register channel class %s\n", channel_type);
3300 goto end;
3301 }
3302
3304 ast_log(LOG_ERROR, "Unable to register PJSIP_DIAL_CONTACTS dialplan function\n");
3305 goto end;
3306 }
3307
3309 ast_log(LOG_ERROR, "Unable to register PJSIP_PARSE_URI dialplan function\n");
3310 goto end;
3311 }
3312
3314 ast_log(LOG_ERROR, "Unable to register PJSIP_PARSE_URI_FROM dialplan function\n");
3315 goto end;
3316 }
3317
3319 ast_log(LOG_WARNING, "Unable to register PJSIP_MEDIA_OFFER dialplan function\n");
3320 goto end;
3321 }
3322
3324 ast_log(LOG_WARNING, "Unable to register PJSIP_DTMF_MODE dialplan function\n");
3325 goto end;
3326 }
3327
3329 ast_log(LOG_WARNING, "Unable to register PJSIP_MOH_PASSTHROUGH dialplan function\n");
3330 goto end;
3331 }
3332
3334 ast_log(LOG_WARNING, "Unable to register PJSIP_SEND_SESSION_REFRESH dialplan function\n");
3335 goto end;
3336 }
3337
3339 ast_log(LOG_WARNING, "Unable to register PJSIPHangup dialplan application\n");
3340 goto end;
3341 }
3343
3344
3346
3349
3353 ast_log(LOG_ERROR, "Unable to create held channels container\n");
3354 goto end;
3355 }
3356
3361
3363 ast_log(LOG_ERROR, "Unable to register PJSIP Channel CLI\n");
3364 goto end;
3365 }
3366
3367 /* since endpoints are loaded before the channel driver their device
3368 states get set to 'invalid', so they need to be updated */
3369 if ((endpoints = ast_sip_get_endpoints())) {
3371 ao2_ref(endpoints, -1);
3372 }
3373
3374 return 0;
3375
3376end:
3395
3398
3400}
3401
3402/*! \brief Unload the PJSIP channel from Asterisk */
3403static int unload_module(void)
3404{
3407
3409
3416
3418
3428
3432
3433 return 0;
3434}
3435
3437 .support_level = AST_MODULE_SUPPORT_CORE,
3438 .load = load_module,
3439 .unload = unload_module,
3440 .load_pri = AST_MODPRI_CHANNEL_DRIVER,
3441 .requires = "res_pjsip,res_pjsip_session,res_pjsip_pubsub",
Access Control of various sorts.
char digit
jack_status_t status
Definition: app_jack.c:146
char * text
Definition: app_queue.c:1639
#define var
Definition: ast_expr2f.c:605
Asterisk main include file. File version handling, generic pbx functions.
static struct ast_mansession session
#define ast_alloca(size)
call __builtin_alloca to ensure we get gcc builtin semantics
Definition: astmm.h:288
#define ast_free(a)
Definition: astmm.h:180
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:298
void ast_free_ptr(void *ptr)
free() wrapper
Definition: astmm.c:1739
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:191
#define ast_log
Definition: astobj2.c:42
#define ao2_link(container, obj)
Add an object to a container.
Definition: astobj2.h:1532
@ AO2_ALLOC_OPT_LOCK_NOLOCK
Definition: astobj2.h:367
@ AO2_ALLOC_OPT_LOCK_RWLOCK
Definition: astobj2.h:365
#define ao2_callback(c, flags, cb_fn, arg)
ao2_callback() is a generic function that applies cb_fn() to all objects in a container,...
Definition: astobj2.h:1693
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1736
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
#define ao2_alloc_options(data_size, destructor_fn, options)
Definition: astobj2.h:404
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition: astobj2.h:480
@ OBJ_SEARCH_PARTIAL_KEY
The arg parameter is a partial search key similar to OBJ_SEARCH_KEY.
Definition: astobj2.h:1116
@ OBJ_SEARCH_OBJECT
The arg parameter is an object of the same type.
Definition: astobj2.h:1087
@ OBJ_NODATA
Definition: astobj2.h:1044
@ OBJ_SEARCH_MASK
Search option field mask.
Definition: astobj2.h:1072
@ OBJ_UNLINK
Definition: astobj2.h:1039
@ OBJ_SEARCH_KEY
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1101
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:409
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Allocate and initialize a hash container with the desired number of buckets.
Definition: astobj2.h:1303
@ AO2_CONTAINER_ALLOC_OPT_DUPS_REJECT
Reject objects with duplicate keys in container.
Definition: astobj2.h:1188
static int tmp()
Definition: bt_open.c:389
CallerID (and other GR30) management and generation Includes code and algorithms from the Zapata libr...
#define AST_PRES_ALLOWED
Definition: callerid.h:432
@ AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER
Definition: callerid.h:554
#define AST_PRES_RESTRICTION
Definition: callerid.h:431
Internal Asterisk hangup causes.
#define AST_CAUSE_SWITCH_CONGESTION
Definition: causes.h:123
#define AST_CAUSE_CONGESTION
Definition: causes.h:153
#define AST_CAUSE_UNALLOCATED
Definition: causes.h:98
#define AST_CAUSE_INTERWORKING
Definition: causes.h:146
#define AST_CAUSE_NUMBER_CHANGED
Definition: causes.h:112
#define AST_CAUSE_BEARERCAPABILITY_NOTAVAIL
Definition: causes.h:130
#define AST_CAUSE_INVALID_NUMBER_FORMAT
Definition: causes.h:116
#define AST_CAUSE_CHAN_NOT_IMPLEMENTED
Definition: causes.h:132
#define AST_CAUSE_FAILURE
Definition: causes.h:150
#define AST_CAUSE_DESTINATION_OUT_OF_ORDER
Definition: causes.h:115
#define AST_CAUSE_NO_USER_RESPONSE
Definition: causes.h:108
#define AST_CAUSE_CHANNEL_UNACCEPTABLE
Definition: causes.h:102
#define AST_CAUSE_NOTDEFINED
Definition: causes.h:155
#define AST_CAUSE_CALL_REJECTED
Definition: causes.h:111
#define AST_CAUSE_FACILITY_REJECTED
Definition: causes.h:117
#define AST_CAUSE_NORMAL_UNSPECIFIED
Definition: causes.h:119
#define AST_CAUSE_NO_ROUTE_TRANSIT_NET
Definition: causes.h:99
#define AST_CAUSE_NO_ROUTE_DESTINATION
Definition: causes.h:100
#define AST_CAUSE_UNREGISTERED
Definition: causes.h:154
#define AST_CAUSE_NO_ANSWER
Definition: causes.h:109
#define AST_CAUSE_NORMAL_CLEARING
Definition: causes.h:106
#define AST_CAUSE_USER_BUSY
Definition: causes.h:107
enum cc_state state
Definition: ccss.c:393
static PGresult * result
Definition: cel_pgsql.c:84
static const char type[]
Definition: chan_ooh323.c:109
static void transfer_data_destroy(void *obj)
Definition: chan_pjsip.c:1883
static int chan_pjsip_incoming_ack(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
Definition: chan_pjsip.c:3204
static struct ast_datastore_info direct_media_mitigation_info
Definition: chan_pjsip.c:274
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:252
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:269
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:1960
static int hangup_cause2sip(int cause)
Internal function which translates from Asterisk cause codes to SIP response codes.
Definition: chan_pjsip.c:2435
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:1483
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:327
static int chan_pjsip_devicestate(const char *data)
Function called to get the device state of an endpoint.
Definition: chan_pjsip.c:1175
static int uid_hold_sort_fn(const void *obj_left, const void *obj_right, const int flags)
Definition: chan_pjsip.c:1085
static int chan_pjsip_transfer(struct ast_channel *ast, const char *target)
Function called by core for Asterisk initiated transfer.
Definition: chan_pjsip.c:2164
static int update_connected_line_information(void *data)
Update connected line information.
Definition: chan_pjsip.c:1430
static void transfer_redirect(struct ast_sip_session *session, const char *target)
Definition: chan_pjsip.c:1910
static void clear_session_and_channel(struct ast_sip_session *session, struct ast_channel *ast)
Clear a channel from a session along with its PVT.
Definition: chan_pjsip.c:2509
static char * app_pjsip_hangup
Definition: chan_pjsip.c:3274
static void chan_pjsip_session_end(struct ast_sip_session *session)
Function called when the session ends.
Definition: chan_pjsip.c:2923
static int sendtext(void *obj)
Definition: chan_pjsip.c:2786
static void update_initial_connected_line(struct ast_sip_session *session)
Definition: chan_pjsip.c:2366
static int update_devstate(void *obj, void *arg, int flags)
Definition: chan_pjsip.c:3229
static struct ast_sip_session_supplement chan_pjsip_supplement
SIP session supplement structure.
Definition: chan_pjsip.c:143
static int remote_send_unhold(void *data)
Update local hold state to be unheld.
Definition: chan_pjsip.c:1502
static void rtp_direct_media_data_destroy(void *data)
Definition: chan_pjsip.c:366
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:2418
static int direct_media_mitigate_glare(struct ast_sip_session *session)
Definition: chan_pjsip.c:276
static int chan_pjsip_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance *rtp, struct ast_rtp_instance *vrtp, struct ast_rtp_instance *tpeer, const struct ast_format_cap *cap, int nat_active)
Function called by RTP engine to change where the remote party should send media.
Definition: chan_pjsip.c:448
static int request(void *obj)
Definition: chan_pjsip.c:2601
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:1621
static int chan_pjsip_hangup(struct ast_channel *ast)
Function called by core to hang up a PJSIP session.
Definition: chan_pjsip.c:2558
static void chan_pjsip_pvt_dtor(void *obj)
Definition: chan_pjsip.c:82
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:1520
static int handle_topology_request_change(struct ast_sip_session *session, const struct ast_stream_topology *proposed)
Definition: chan_pjsip.c:1597
static enum ast_rtp_glue_result chan_pjsip_get_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
Function called by RTP engine to get local audio RTP peer.
Definition: chan_pjsip.c:179
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)
Asterisk core interaction functions.
Definition: chan_pjsip.c:2736
static struct ast_custom_function chan_pjsip_dial_contacts_function
Definition: chan_pjsip.c:3236
static int send_topology_change_refresh(void *data)
Definition: chan_pjsip.c:1571
static int indicate(void *data)
Definition: chan_pjsip.c:1334
static int remote_send_hold(void *data)
Update local hold state to be held.
Definition: chan_pjsip.c:1496
static int on_topology_change_response(struct ast_sip_session *session, pjsip_rx_data *rdata)
Definition: chan_pjsip.c:1545
static struct ao2_container * pjsip_uids_onhold
Definition: chan_pjsip.c:1108
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:1238
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:2968
struct ast_channel_tech chan_pjsip_tech
PBX interface structure for channel registration.
Definition: chan_pjsip.c:109
static void set_channel_on_rtp_instance(const struct ast_sip_session *session, const char *channel_id)
Definition: chan_pjsip.c:494
static int chan_pjsip_incoming_prack(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
Definition: chan_pjsip.c:3217
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:2709
static int uid_hold_hash_fn(const void *obj, const int flags)
Definition: chan_pjsip.c:1067
static struct ast_sip_session_supplement pbx_start_supplement
Definition: chan_pjsip.c:3110
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:2183
static struct ast_custom_function chan_pjsip_parse_uri_from_function
Definition: chan_pjsip.c:3246
static struct ast_sip_session_supplement chan_pjsip_prack_supplement
Definition: chan_pjsip.c:172
static void chan_pjsip_session_begin(struct ast_sip_session *session)
SIP session interaction functions.
Definition: chan_pjsip.c:2901
static void topology_change_refresh_data_free(struct topology_change_refresh_data *refresh_data)
Definition: chan_pjsip.c:1512
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:1309
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:377
static struct sendtext_data * sendtext_data_create(struct ast_channel *chan, struct ast_msg_data *msg)
Definition: chan_pjsip.c:2765
static void transport_info_destroy(void *obj)
Destructor function for transport_info_data.
Definition: chan_pjsip.c:261
static int send_direct_media_request(void *data)
Definition: chan_pjsip.c:396
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:2857
static struct ast_rtp_glue chan_pjsip_rtp_glue
Local glue for interacting with the RTP engine core.
Definition: chan_pjsip.c:486
static pjsip_module refer_callback_module
REFER Callback module, used to attach session data structure to subscription.
Definition: chan_pjsip.c:1948
static int call_pickup_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
Definition: chan_pjsip.c:3026
static enum ast_rtp_glue_result chan_pjsip_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
Function called by RTP engine to get local video RTP peer.
Definition: chan_pjsip.c:223
static int rtp_find_rtcp_fd_position(struct ast_sip_session *session, struct ast_rtp_instance *rtp)
Helper function to find the position for RTCP.
Definition: chan_pjsip.c:306
static int transfer(void *data)
Definition: chan_pjsip.c:2129
static const char channel_type[]
Definition: chan_pjsip.c:78
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:1149
static int hangup(void *data)
Definition: chan_pjsip.c:2516
static int transmit_info_with_vidupdate(void *data)
Send SIP INFO with video update request.
Definition: chan_pjsip.c:1352
static void sendtext_data_destroy(void *obj)
Definition: chan_pjsip.c:2758
static struct ast_custom_function moh_passthrough_function
Definition: chan_pjsip.c:3263
static void chan_pjsip_incoming_response_update_cause(struct ast_sip_session *session, struct pjsip_rx_data *rdata)
Function called when a response is received on the session.
Definition: chan_pjsip.c:3117
static struct hangup_data * hangup_data_alloc(int cause, struct ast_channel *chan)
Definition: chan_pjsip.c:2494
static int chan_pjsip_write_stream(struct ast_channel *ast, int stream_num, struct ast_frame *f)
Definition: chan_pjsip.c:950
static int answer(void *data)
Definition: chan_pjsip.c:683
static int transmit_info_dtmf(void *data)
Definition: chan_pjsip.c:2251
static struct ast_custom_function media_offer_function
Definition: chan_pjsip.c:3251
static int chan_pjsip_write(struct ast_channel *ast, struct ast_frame *f)
Definition: chan_pjsip.c:1040
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:526
static void hangup_data_destroy(void *obj)
Definition: chan_pjsip.c:2487
static int load_module(void)
Load the module.
Definition: chan_pjsip.c:3286
static struct ast_sip_session_supplement chan_pjsip_ack_supplement
Definition: chan_pjsip.c:164
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:1162
#define UNIQUEID_BUFSIZE
Definition: chan_pjsip.c:76
static int call(void *data)
Definition: chan_pjsip.c:2391
static int chan_pjsip_sendtext(struct ast_channel *ast, const char *text)
Definition: chan_pjsip.c:2879
static struct ast_custom_function chan_pjsip_parse_uri_function
Definition: chan_pjsip.c:3241
static void transfer_refer(struct ast_sip_session *session, const char *target)
Definition: chan_pjsip.c:2077
static struct ast_threadstorage uniqueid_threadbuf
Definition: chan_pjsip.c:75
static int unload_module(void)
Unload the PJSIP channel from Asterisk.
Definition: chan_pjsip.c:3403
static int pbx_start_incoming_request(struct ast_sip_session *session, pjsip_rx_data *rdata)
Definition: chan_pjsip.c:3076
static int is_compatible_format(struct ast_sip_session *session, struct ast_frame *f)
Determine if the given frame is in a format we've negotiated.
Definition: chan_pjsip.c:825
static unsigned int chan_idx
Definition: chan_pjsip.c:80
static void indicate_data_destroy(void *obj)
Definition: chan_pjsip.c:1301
static struct ast_custom_function session_refresh_function
Definition: chan_pjsip.c:3269
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:766
static const char * chan_pjsip_get_uniqueid(struct ast_channel *ast)
Definition: chan_pjsip.c:1279
static struct transfer_data * transfer_data_alloc(struct ast_sip_session *session, const char *target)
Definition: chan_pjsip.c:1891
static int chan_pjsip_answer(struct ast_channel *ast)
Function called by core when we should answer a PJSIP session.
Definition: chan_pjsip.c:727
static void info_dtmf_data_destroy(void *obj)
Definition: chan_pjsip.c:2232
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:1118
static struct info_dtmf_data * info_dtmf_data_alloc(struct ast_sip_session *session, char digit, unsigned int duration)
Definition: chan_pjsip.c:2238
static int is_colp_update_allowed(struct ast_sip_session *session)
Definition: chan_pjsip.c:1403
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:2295
static struct ast_sip_session_supplement chan_pjsip_supplement_response
SIP session supplement structure just for responses.
Definition: chan_pjsip.c:155
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:839
static void set_sipdomain_variable(struct ast_sip_session *session)
Definition: chan_pjsip.c:2955
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:1046
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:547
static struct ast_sip_session_supplement call_pickup_supplement
Definition: chan_pjsip.c:3070
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:3147
static struct ast_custom_function dtmf_mode_function
Definition: chan_pjsip.c:3257
PJSIP Channel Driver shared data structures.
General Asterisk PBX channel definitions.
const char * ast_channel_name(const struct ast_channel *chan)
void ast_channel_rings_set(struct ast_channel *chan, int value)
void ast_channel_named_pickupgroups_set(struct ast_channel *chan, struct ast_namedgroups *value)
#define AST_EXTENDED_FDS
Definition: channel.h:195
int ast_channel_get_device_name(struct ast_channel *chan, char *device_name, size_t name_buffer_length)
Get a device name given its channel structure.
Definition: channel.c:10496
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.
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.
@ AST_CHAN_TP_SEND_TEXT_DATA
Channels have this property if they implement send_text_data.
Definition: channel.h:975
@ AST_CHAN_TP_WANTSJITTER
Channels have this property if they can accept input with jitter; i.e. most VoIP channels.
Definition: channel.h:960
@ AST_CHAN_TP_CREATESJITTER
Channels have this property if they can create jitter; i.e. most VoIP channels.
Definition: channel.h:965
#define ast_channel_alloc_with_endpoint(needqueue, state, cid_num, cid_name, acctcode, exten, context, assignedids, requestor, amaflag, endpoint,...)
Definition: channel.h:1262
void * ast_channel_tech_pvt(const struct ast_channel *chan)
struct ast_format * ast_channel_rawreadformat(struct ast_channel *chan)
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:5488
void ast_hangup(struct ast_channel *chan)
Hang up a channel.
Definition: channel.c:2541
int ast_queue_hangup(struct ast_channel *chan)
Queue a hangup frame.
Definition: channel.c:1150
int ast_party_id_presentation(const struct ast_party_id *id)
Determine the overall presentation value for the given party.
Definition: channel.c:1821
void ast_channel_nativeformats_set(struct ast_channel *chan, struct ast_format_cap *value)
int ast_channel_fdno(const struct ast_channel *chan)
#define ast_channel_lock(chan)
Definition: channel.h:2922
struct ast_trans_pvt * ast_channel_readtrans(const struct ast_channel *chan)
struct ast_format_cap * ast_channel_nativeformats(const struct ast_channel *chan)
ast_t38_state
Possible T38 states on channels.
Definition: channel.h:878
@ T38_STATE_UNAVAILABLE
Definition: channel.h:879
@ T38_STATE_UNKNOWN
Definition: channel.h:880
@ T38_STATE_REJECTED
Definition: channel.h:882
@ T38_STATE_NEGOTIATED
Definition: channel.h:883
@ T38_STATE_NEGOTIATING
Definition: channel.h:881
void ast_channel_unregister(const struct ast_channel_tech *tech)
Unregister a channel technology.
Definition: channel.c:570
struct ast_trans_pvt * ast_channel_writetrans(const struct ast_channel *chan)
int ast_queue_control(struct ast_channel *chan, enum ast_control_frame_type control)
Queue a control frame without payload.
Definition: channel.c:1231
#define ast_channel_ref(c)
Increase channel reference count.
Definition: channel.h:2947
const char * ast_channel_uniqueid(const struct ast_channel *chan)
const char * ast_channel_context(const struct ast_channel *chan)
int ast_queue_control_data(struct ast_channel *chan, enum ast_control_frame_type control, const void *data, size_t datalen)
Queue a control frame with payload.
Definition: channel.c:1238
@ AST_ADSI_UNAVAILABLE
Definition: channel.h:871
int ast_queue_hangup_with_cause(struct ast_channel *chan, int cause)
Queue a hangup frame with hangupcause set.
Definition: channel.c:1166
void ast_channel_set_rawreadformat(struct ast_channel *chan, struct ast_format *format)
void ast_channel_tech_pvt_set(struct ast_channel *chan, void *value)
void ast_channel_callgroup_set(struct ast_channel *chan, ast_group_t value)
struct ast_format * ast_channel_rawwriteformat(struct ast_channel *chan)
int ast_channel_hangupcause(const struct ast_channel *chan)
int ast_channel_is_bridged(const struct ast_channel *chan)
Determine if a channel is in a bridge.
Definition: channel.c:10545
void ast_channel_set_rawwriteformat(struct ast_channel *chan, struct ast_format *format)
struct ast_party_dialed * ast_channel_dialed(struct ast_channel *chan)
void ast_set_hangupsource(struct ast_channel *chan, const char *source, int force)
Set the source of the hangup in this channel and it's bridge.
Definition: channel.c:2499
void ast_channel_named_callgroups_set(struct ast_channel *chan, struct ast_namedgroups *value)
void ast_channel_hangupcause_hash_set(struct ast_channel *chan, const struct ast_control_pvt_cause_code *cause_code, int datalen)
Sets the HANGUPCAUSE hash and optionally the SIP_CAUSE hash on the given channel.
Definition: channel.c:4346
void ast_channel_set_readformat(struct ast_channel *chan, struct ast_format *format)
#define AST_CHANNEL_NAME
Definition: channel.h:171
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:539
#define ast_channel_unref(c)
Decrease channel reference count.
Definition: channel.h:2958
struct ast_format * ast_channel_writeformat(struct ast_channel *chan)
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:1765
struct ast_party_id ast_channel_connected_effective_id(struct ast_channel *chan)
void ast_party_connected_line_init(struct ast_party_connected_line *init)
Initialize the given connected line structure.
Definition: channel.c:2022
int ast_channel_get_up_time(struct ast_channel *chan)
Obtain how long it has been since the channel was answered.
Definition: channel.c:2845
void ast_channel_set_fd(struct ast_channel *chan, int which, int fd)
Definition: channel.c:2426
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:5524
void ast_channel_zone_set(struct ast_channel *chan, struct ast_tone_zone *value)
struct ast_party_caller * ast_channel_caller(struct ast_channel *chan)
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:9106
void ast_channel_priority_set(struct ast_channel *chan, int value)
void ast_channel_hangupcause_set(struct ast_channel *chan, int value)
void ast_channel_pickupgroup_set(struct ast_channel *chan, ast_group_t value)
void ast_channel_adsicpe_set(struct ast_channel *chan, enum ast_channel_adsicpe value)
void ast_channel_tech_set(struct ast_channel *chan, const struct ast_channel_tech *value)
const char * ast_channel_exten(const struct ast_channel *chan)
#define ast_channel_unlock(chan)
Definition: channel.h:2923
void ast_channel_set_writeformat(struct ast_channel *chan, struct ast_format *format)
struct ast_format * ast_channel_readformat(struct ast_channel *chan)
ast_channel_state
ast_channel states
Definition: channelstate.h:35
@ AST_STATE_RING
Definition: channelstate.h:40
@ AST_STATE_RINGING
Definition: channelstate.h:41
@ AST_STATE_DOWN
Definition: channelstate.h:36
@ AST_STATE_BUSY
Definition: channelstate.h:43
@ AST_STATE_UP
Definition: channelstate.h:42
int ast_setstate(struct ast_channel *chan, enum ast_channel_state)
Change the state of a channel.
Definition: channel.c:7386
Standard Command Line Interface.
int pjsip_channel_cli_register(void)
Registers the channel cli commands.
Definition: cli_commands.c:462
void pjsip_channel_cli_unregister(void)
Unregisters the channel cli commands.
Definition: cli_commands.c:498
PJSIP CLI functions header file.
@ AST_MEDIA_TYPE_AUDIO
Definition: codec.h:32
@ AST_MEDIA_TYPE_UNKNOWN
Definition: codec.h:31
@ AST_MEDIA_TYPE_VIDEO
Definition: codec.h:33
@ AST_MEDIA_TYPE_IMAGE
Definition: codec.h:34
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:348
@ AST_DEVSTATE_CACHABLE
Definition: devicestate.h:70
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
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
void ast_devstate_aggregate_init(struct ast_devstate_aggregate *agg)
Initialize aggregate device state.
Definition: devicestate.c:630
enum ast_device_state ast_devstate_aggregate_result(struct ast_devstate_aggregate *agg)
Get the aggregate device state result.
Definition: devicestate.c:663
enum ast_device_state ast_state_chan2dev(enum ast_channel_state chanstate)
Convert channel state to devicestate.
Definition: devicestate.c:242
int ast_devstate_changed_literal(enum ast_device_state state, enum ast_devstate_cache cachable, const char *device)
Tells Asterisk the State for Device is changed.
Definition: devicestate.c:471
ast_device_state
Device States.
Definition: devicestate.h:52
@ AST_DEVICE_UNKNOWN
Definition: devicestate.h:53
@ AST_DEVICE_ONHOLD
Definition: devicestate.h:61
@ AST_DEVICE_INVALID
Definition: devicestate.h:57
@ AST_DEVICE_BUSY
Definition: devicestate.h:56
@ AST_DEVICE_NOT_INUSE
Definition: devicestate.h:54
@ AST_DEVICE_UNAVAILABLE
Definition: devicestate.h:58
int pjsip_acf_moh_passthrough_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
PJSIP_MOH_PASSTHROUGH function read callback.
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 pjsip_acf_media_offer_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
PJSIP_MEDIA_OFFER function write callback.
int pjsip_acf_dtmf_mode_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
PJSIP_DTMF_MODE function write callback.
int pjsip_acf_session_refresh_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
PJSIP_SEND_SESSION_REFRESH function write callback.
int pjsip_app_hangup(struct ast_channel *chan, const char *data)
PJSIPHangup Dialplan App.
int pjsip_acf_moh_passthrough_write(struct ast_channel *chan, const char *cmd, char *data, const char *value)
PJSIP_MOH_PASSTHROUGH function write callback.
int pjsip_action_hangup(struct mansession *s, const struct message *m)
PJSIPHangup Manager Action.
int pjsip_acf_channel_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
CHANNEL function read callback.
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.
int pjsip_acf_dial_contacts_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
PJSIP_DIAL_CONTACTS function read callback.
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.
PJSIP dialplan functions header file.
Convenient Signal Processing routines.
void ast_dsp_free(struct ast_dsp *dsp)
Definition: dsp.c:1783
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:1499
#define DSP_FEATURE_FAX_DETECT
Definition: dsp.h:29
int ast_dsp_get_features(struct ast_dsp *dsp)
Get features.
Definition: dsp.c:1777
void ast_dsp_set_features(struct ast_dsp *dsp, int features)
Select feature set.
Definition: dsp.c:1768
char connected
Definition: eagi_proxy.c:82
char * end
Definition: eagi_proxy.c:73
char buf[BUFSIZE]
Definition: eagi_proxy.c:66
@ AST_ENDPOINT_OFFLINE
Definition: endpoints.h:55
@ AST_ENDPOINT_ONLINE
Definition: endpoints.h:57
const char * ast_endpoint_get_tech(const struct ast_endpoint *endpoint)
Gets the technology of the given endpoint.
const char * ast_endpoint_get_resource(const struct ast_endpoint *endpoint)
Gets the resource name of the given endpoint.
Generic File Format Support. Should be included by clients of the file handling routines....
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
@ AST_FORMAT_CMP_NOT_EQUAL
Definition: format.h:38
const char * ast_format_get_name(const struct ast_format *format)
Get the name associated with a format.
Definition: format.c:334
Media Format Cache API.
struct ast_format * ast_format_h264
Built-in cached h264 format.
Definition: format_cache.c:176
struct ast_format * ast_format_h265
Built-in cached h265 format.
Definition: format_cache.c:181
struct ast_format * ast_format_vp9
Built-in cached vp9 format.
Definition: format_cache.c:196
struct ast_format * ast_format_vp8
Built-in cached vp8 format.
Definition: format_cache.c:191
#define AST_FORMAT_CAP_NAMES_LEN
Definition: format_cap.h:324
int ast_format_cap_empty(const struct ast_format_cap *cap)
Determine if a format cap has no formats in it.
Definition: format_cap.c:744
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
struct ast_format * ast_format_cap_get_format(const struct ast_format_cap *cap, int position)
Get the format at a specific index.
Definition: format_cap.c:400
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
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:523
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:581
@ AST_FORMAT_CAP_FLAG_DEFAULT
Definition: format_cap.h:38
int ast_format_cap_append_from_cap(struct ast_format_cap *dst, const struct ast_format_cap *src, enum ast_media_type type)
Append the formats of provided type in src to dst.
Definition: format_cap.c:269
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:734
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:687
int ast_format_cap_iscompatible(const struct ast_format_cap *cap1, const struct ast_format_cap *cap2)
Determine if any joint capabilities exist between two capabilities structures.
Definition: format_cap.c:653
#define ast_format_cap_append(cap, format, framing)
Add format capability to capabilities structure.
Definition: format_cap.h:99
#define ast_format_cap_alloc(flags)
Allocate a new ast_format_cap structure.
Definition: format_cap.h:49
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
static int exists(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len)
Definition: func_logic.c:157
int ast_manager_unregister(const char *action)
Unregister a registered manager command.
Definition: manager.c:8057
#define SCOPE_EXIT_RTN(...)
#define SCOPE_EXIT_RTN_VALUE(__return_value,...)
#define SCOPE_EXIT_LOG_RTN_VALUE(__value, __log_level,...)
#define SCOPE_ENTER(level,...)
#define SCOPE_ENTER_TASK(level, indent,...)
#define ast_trace(level,...)
#define ast_trace_get_indent()
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,...
void ast_channel_stage_snapshot_done(struct ast_channel *chan)
Clear flag to indicate channel snapshot is being staged, and publish snapshot.
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.
void ast_channel_stage_snapshot(struct ast_channel *chan)
Set flag to indicate channel snapshot is being staged.
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.
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.
struct ast_msg_data * ast_msg_data_dup(struct ast_msg_data *msg)
Clone an ast_msg_data structure.
@ AST_MSG_DATA_ATTR_BODY
Definition: message.h:458
@ AST_MSG_DATA_ATTR_TO
Definition: message.h:455
@ AST_MSG_DATA_ATTR_FROM
Definition: message.h:456
@ AST_MSG_DATA_ATTR_CONTENT_TYPE
Definition: message.h:457
@ AST_MSG_DATA_SOURCE_TYPE_UNKNOWN
Definition: message.h:447
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:2099
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:2165
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:2179
Application convenience functions, designed to give consistent look and feel to Asterisk apps.
#define AST_APP_ARG(name)
Define an application argument.
#define AST_DECLARE_APP_ARGS(name, arglist)
Declare a structure to hold an application's arguments.
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:3162
#define AST_NONSTANDARD_APP_ARGS(args, parse, sep)
Performs the 'nonstandard' argument separation process for an application.
struct ast_features_pickup_config * ast_get_chan_features_pickup_config(struct ast_channel *chan)
Get the pickup configuration options for a channel.
@ AST_T38_REQUEST_PARMS
#define AST_FRAME_DTMF
ast_control_transfer
@ AST_TRANSFER_FAILED
@ AST_TRANSFER_SUCCESS
#define ast_frfree(fr)
char * ast_frame_subclass2str(struct ast_frame *f, char *subclass, size_t slen, char *moreinfo, size_t mlen)
Copy the discription of a frame's subclass into the provided string.
Definition: main/frame.c:406
#define AST_OPTION_T38_STATE
@ AST_FRAME_VIDEO
@ AST_FRAME_VOICE
@ AST_FRAME_RTCP
@ AST_FRAME_MODEM
@ AST_FRAME_CONTROL
@ AST_CONTROL_SRCUPDATE
@ AST_CONTROL_PROGRESS
@ AST_CONTROL_STREAM_TOPOLOGY_SOURCE_CHANGED
@ AST_CONTROL_BUSY
@ AST_CONTROL_UNHOLD
@ AST_CONTROL_VIDUPDATE
@ AST_CONTROL_STREAM_TOPOLOGY_REQUEST_CHANGE
@ AST_CONTROL_PROCEEDING
@ AST_CONTROL_REDIRECTING
@ AST_CONTROL_T38_PARAMETERS
@ AST_CONTROL_CONGESTION
@ AST_CONTROL_ANSWER
@ AST_CONTROL_RINGING
@ AST_CONTROL_HOLD
@ AST_CONTROL_STREAM_TOPOLOGY_CHANGED
@ AST_CONTROL_CONNECTED_LINE
@ AST_CONTROL_TRANSFER
@ AST_CONTROL_FLASH
@ AST_CONTROL_SRCCHANGE
@ AST_CONTROL_INCOMPLETE
@ AST_CONTROL_MASQUERADE_NOTIFY
@ AST_CONTROL_PVT_CAUSE_CODE
@ AST_CONTROL_UPDATE_RTP_PEER
struct ast_frame ast_null_frame
Definition: main/frame.c:79
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_ERROR
#define ast_verb(level,...)
#define LOG_NOTICE
#define LOG_WARNING
Tone Indication Support.
struct ast_tone_zone * ast_get_indication_zone(const char *country)
locate ast_tone_zone
Definition: indications.c:439
#define AST_LIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
Definition: linkedlists.h:439
Asterisk locking-related definitions:
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:757
static struct ao2_container * endpoints
#define EVENT_FLAG_SYSTEM
Definition: manager.h:75
#define ast_manager_register_xml(action, authority, func)
Register a manager callback using XML documentation to describe the manager.
Definition: manager.h:191
#define EVENT_FLAG_CALL
Definition: manager.h:76
Out-of-call text message support.
Asterisk module definitions.
@ AST_MODFLAG_LOAD_ORDER
Definition: module.h:331
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:557
@ AST_MODPRI_CHANNEL_DRIVER
Definition: module.h:341