2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Channel Management
23 * \author Mark Spencer <markster@digium.com>
27 <support_level>core</support_level>
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include "asterisk/_private.h"
40 #include "asterisk/paths.h" /* use ast_config_AST_SYSTEM_NAME */
42 #include "asterisk/pbx.h"
43 #include "asterisk/frame.h"
44 #include "asterisk/mod_format.h"
45 #include "asterisk/sched.h"
46 #include "asterisk/channel.h"
47 #include "asterisk/musiconhold.h"
48 #include "asterisk/say.h"
49 #include "asterisk/file.h"
50 #include "asterisk/cli.h"
51 #include "asterisk/translate.h"
52 #include "asterisk/manager.h"
53 #include "asterisk/cel.h"
54 #include "asterisk/chanvars.h"
55 #include "asterisk/linkedlists.h"
56 #include "asterisk/indications.h"
57 #include "asterisk/monitor.h"
58 #include "asterisk/causes.h"
59 #include "asterisk/callerid.h"
60 #include "asterisk/utils.h"
61 #include "asterisk/lock.h"
62 #include "asterisk/app.h"
63 #include "asterisk/transcap.h"
64 #include "asterisk/devicestate.h"
65 #include "asterisk/threadstorage.h"
66 #include "asterisk/slinfactory.h"
67 #include "asterisk/audiohook.h"
68 #include "asterisk/framehook.h"
69 #include "asterisk/timing.h"
70 #include "asterisk/autochan.h"
71 #include "asterisk/stringfields.h"
72 #include "asterisk/global_datastores.h"
73 #include "asterisk/data.h"
74 #include "asterisk/channel_internal.h"
80 #include <sys/epoll.h>
83 #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
86 #endif /* defined(HAVE_PRI) */
87 #endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
89 struct ast_epoll_data {
90 struct ast_channel *chan;
94 /* uncomment if you have problems with 'monitoring' synchronized files */
96 #define MONITOR_CONSTANT_DELAY
97 #define MONITOR_DELAY 150 * 8 /*!< 150 ms of MONITORING DELAY */
100 /*! \brief Prevent new channel allocation if shutting down. */
101 static int shutting_down;
103 static int uniqueint;
104 static int chancount;
106 unsigned long global_fin, global_fout;
108 AST_THREADSTORAGE(state2str_threadbuf);
109 #define STATE2STR_BUFSIZE 32
111 /*! Default amount of time to use when emulating a digit as a begin and end
113 #define AST_DEFAULT_EMULATE_DTMF_DURATION 100
115 /*! Minimum amount of time between the end of the last digit and the beginning
116 * of a new one - 45ms */
117 #define AST_MIN_DTMF_GAP 45
119 /*! \brief List of channel drivers */
121 const struct ast_channel_tech *tech;
122 AST_LIST_ENTRY(chanlist) list;
126 /*! \brief Structure to hold channel context backtrace data */
127 struct ast_chan_trace_data {
129 AST_LIST_HEAD_NOLOCK(, ast_chan_trace) trace;
132 /*! \brief Structure to save contexts where an ast_chan has been into */
133 struct ast_chan_trace {
134 char context[AST_MAX_CONTEXT];
135 char exten[AST_MAX_EXTENSION];
137 AST_LIST_ENTRY(ast_chan_trace) entry;
141 /*! \brief the list of registered channel types */
142 static AST_RWLIST_HEAD_STATIC(backends, chanlist);
145 #define NUM_CHANNEL_BUCKETS 61
147 #define NUM_CHANNEL_BUCKETS 1567
150 /*! \brief All active channels on the system */
151 static struct ao2_container *channels;
153 /*! \brief map AST_CAUSE's to readable string representations
157 static const struct {
162 { AST_CAUSE_UNALLOCATED, "UNALLOCATED", "Unallocated (unassigned) number" },
163 { AST_CAUSE_NO_ROUTE_TRANSIT_NET, "NO_ROUTE_TRANSIT_NET", "No route to specified transmit network" },
164 { AST_CAUSE_NO_ROUTE_DESTINATION, "NO_ROUTE_DESTINATION", "No route to destination" },
165 { AST_CAUSE_MISDIALLED_TRUNK_PREFIX, "MISDIALLED_TRUNK_PREFIX", "Misdialed trunk prefix" },
166 { AST_CAUSE_CHANNEL_UNACCEPTABLE, "CHANNEL_UNACCEPTABLE", "Channel unacceptable" },
167 { AST_CAUSE_CALL_AWARDED_DELIVERED, "CALL_AWARDED_DELIVERED", "Call awarded and being delivered in an established channel" },
168 { AST_CAUSE_PRE_EMPTED, "PRE_EMPTED", "Pre-empted" },
169 { AST_CAUSE_NUMBER_PORTED_NOT_HERE, "NUMBER_PORTED_NOT_HERE", "Number ported elsewhere" },
170 { AST_CAUSE_NORMAL_CLEARING, "NORMAL_CLEARING", "Normal Clearing" },
171 { AST_CAUSE_USER_BUSY, "USER_BUSY", "User busy" },
172 { AST_CAUSE_NO_USER_RESPONSE, "NO_USER_RESPONSE", "No user responding" },
173 { AST_CAUSE_NO_ANSWER, "NO_ANSWER", "User alerting, no answer" },
174 { AST_CAUSE_SUBSCRIBER_ABSENT, "SUBSCRIBER_ABSENT", "Subscriber absent" },
175 { AST_CAUSE_CALL_REJECTED, "CALL_REJECTED", "Call Rejected" },
176 { AST_CAUSE_NUMBER_CHANGED, "NUMBER_CHANGED", "Number changed" },
177 { AST_CAUSE_REDIRECTED_TO_NEW_DESTINATION, "REDIRECTED_TO_NEW_DESTINATION", "Redirected to new destination" },
178 { AST_CAUSE_ANSWERED_ELSEWHERE, "ANSWERED_ELSEWHERE", "Answered elsewhere" },
179 { AST_CAUSE_DESTINATION_OUT_OF_ORDER, "DESTINATION_OUT_OF_ORDER", "Destination out of order" },
180 { AST_CAUSE_INVALID_NUMBER_FORMAT, "INVALID_NUMBER_FORMAT", "Invalid number format" },
181 { AST_CAUSE_FACILITY_REJECTED, "FACILITY_REJECTED", "Facility rejected" },
182 { AST_CAUSE_RESPONSE_TO_STATUS_ENQUIRY, "RESPONSE_TO_STATUS_ENQUIRY", "Response to STATus ENQuiry" },
183 { AST_CAUSE_NORMAL_UNSPECIFIED, "NORMAL_UNSPECIFIED", "Normal, unspecified" },
184 { AST_CAUSE_NORMAL_CIRCUIT_CONGESTION, "NORMAL_CIRCUIT_CONGESTION", "Circuit/channel congestion" },
185 { AST_CAUSE_NETWORK_OUT_OF_ORDER, "NETWORK_OUT_OF_ORDER", "Network out of order" },
186 { AST_CAUSE_NORMAL_TEMPORARY_FAILURE, "NORMAL_TEMPORARY_FAILURE", "Temporary failure" },
187 { AST_CAUSE_SWITCH_CONGESTION, "SWITCH_CONGESTION", "Switching equipment congestion" },
188 { AST_CAUSE_ACCESS_INFO_DISCARDED, "ACCESS_INFO_DISCARDED", "Access information discarded" },
189 { AST_CAUSE_REQUESTED_CHAN_UNAVAIL, "REQUESTED_CHAN_UNAVAIL", "Requested channel not available" },
190 { AST_CAUSE_FACILITY_NOT_SUBSCRIBED, "FACILITY_NOT_SUBSCRIBED", "Facility not subscribed" },
191 { AST_CAUSE_OUTGOING_CALL_BARRED, "OUTGOING_CALL_BARRED", "Outgoing call barred" },
192 { AST_CAUSE_INCOMING_CALL_BARRED, "INCOMING_CALL_BARRED", "Incoming call barred" },
193 { AST_CAUSE_BEARERCAPABILITY_NOTAUTH, "BEARERCAPABILITY_NOTAUTH", "Bearer capability not authorized" },
194 { AST_CAUSE_BEARERCAPABILITY_NOTAVAIL, "BEARERCAPABILITY_NOTAVAIL", "Bearer capability not available" },
195 { AST_CAUSE_BEARERCAPABILITY_NOTIMPL, "BEARERCAPABILITY_NOTIMPL", "Bearer capability not implemented" },
196 { AST_CAUSE_CHAN_NOT_IMPLEMENTED, "CHAN_NOT_IMPLEMENTED", "Channel not implemented" },
197 { AST_CAUSE_FACILITY_NOT_IMPLEMENTED, "FACILITY_NOT_IMPLEMENTED", "Facility not implemented" },
198 { AST_CAUSE_INVALID_CALL_REFERENCE, "INVALID_CALL_REFERENCE", "Invalid call reference value" },
199 { AST_CAUSE_INCOMPATIBLE_DESTINATION, "INCOMPATIBLE_DESTINATION", "Incompatible destination" },
200 { AST_CAUSE_INVALID_MSG_UNSPECIFIED, "INVALID_MSG_UNSPECIFIED", "Invalid message unspecified" },
201 { AST_CAUSE_MANDATORY_IE_MISSING, "MANDATORY_IE_MISSING", "Mandatory information element is missing" },
202 { AST_CAUSE_MESSAGE_TYPE_NONEXIST, "MESSAGE_TYPE_NONEXIST", "Message type nonexist." },
203 { AST_CAUSE_WRONG_MESSAGE, "WRONG_MESSAGE", "Wrong message" },
204 { AST_CAUSE_IE_NONEXIST, "IE_NONEXIST", "Info. element nonexist or not implemented" },
205 { AST_CAUSE_INVALID_IE_CONTENTS, "INVALID_IE_CONTENTS", "Invalid information element contents" },
206 { AST_CAUSE_WRONG_CALL_STATE, "WRONG_CALL_STATE", "Message not compatible with call state" },
207 { AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE, "RECOVERY_ON_TIMER_EXPIRE", "Recover on timer expiry" },
208 { AST_CAUSE_MANDATORY_IE_LENGTH_ERROR, "MANDATORY_IE_LENGTH_ERROR", "Mandatory IE length error" },
209 { AST_CAUSE_PROTOCOL_ERROR, "PROTOCOL_ERROR", "Protocol error, unspecified" },
210 { AST_CAUSE_INTERWORKING, "INTERWORKING", "Interworking, unspecified" },
213 struct ast_variable *ast_channeltype_list(void)
216 struct ast_variable *var = NULL, *prev = NULL;
218 AST_RWLIST_RDLOCK(&backends);
219 AST_RWLIST_TRAVERSE(&backends, cl, list) {
221 if ((prev->next = ast_variable_new(cl->tech->type, cl->tech->description, "")))
224 var = ast_variable_new(cl->tech->type, cl->tech->description, "");
228 AST_RWLIST_UNLOCK(&backends);
233 #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
234 static const char *party_number_ton2str(int ton)
236 #if defined(HAVE_PRI)
237 switch ((ton >> 4) & 0x07) {
238 case PRI_TON_INTERNATIONAL:
239 return "International";
240 case PRI_TON_NATIONAL:
242 case PRI_TON_NET_SPECIFIC:
243 return "Network Specific";
244 case PRI_TON_SUBSCRIBER:
246 case PRI_TON_ABBREVIATED:
247 return "Abbreviated";
248 case PRI_TON_RESERVED:
250 case PRI_TON_UNKNOWN:
254 #endif /* defined(HAVE_PRI) */
257 #endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
259 #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
260 static const char *party_number_plan2str(int plan)
262 #if defined(HAVE_PRI)
263 switch (plan & 0x0F) {
265 case PRI_NPI_UNKNOWN:
267 case PRI_NPI_E163_E164:
268 return "Public (E.163/E.164)";
270 return "Data (X.121)";
272 return "Telex (F.69)";
273 case PRI_NPI_NATIONAL:
274 return "National Standard";
275 case PRI_NPI_PRIVATE:
277 case PRI_NPI_RESERVED:
280 #endif /* defined(HAVE_PRI) */
283 #endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
285 /*! \brief Show channel types - CLI command */
286 static char *handle_cli_core_show_channeltypes(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
288 #define FORMAT "%-10.10s %-40.40s %-12.12s %-12.12s %-12.12s\n"
294 e->command = "core show channeltypes";
296 "Usage: core show channeltypes\n"
297 " Lists available channel types registered in your\n"
298 " Asterisk server.\n";
305 return CLI_SHOWUSAGE;
307 ast_cli(a->fd, FORMAT, "Type", "Description", "Devicestate", "Indications", "Transfer");
308 ast_cli(a->fd, FORMAT, "----------", "-----------", "-----------", "-----------", "--------");
310 AST_RWLIST_RDLOCK(&backends);
311 AST_RWLIST_TRAVERSE(&backends, cl, list) {
312 ast_cli(a->fd, FORMAT, cl->tech->type, cl->tech->description,
313 (cl->tech->devicestate) ? "yes" : "no",
314 (cl->tech->indicate) ? "yes" : "no",
315 (cl->tech->transfer) ? "yes" : "no");
318 AST_RWLIST_UNLOCK(&backends);
320 ast_cli(a->fd, "----------\n%d channel drivers registered.\n", count_chan);
327 static char *complete_channeltypes(struct ast_cli_args *a)
337 wordlen = strlen(a->word);
339 AST_RWLIST_RDLOCK(&backends);
340 AST_RWLIST_TRAVERSE(&backends, cl, list) {
341 if (!strncasecmp(a->word, cl->tech->type, wordlen) && ++which > a->n) {
342 ret = ast_strdup(cl->tech->type);
346 AST_RWLIST_UNLOCK(&backends);
351 /*! \brief Show details about a channel driver - CLI command */
352 static char *handle_cli_core_show_channeltype(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
354 struct chanlist *cl = NULL;
359 e->command = "core show channeltype";
361 "Usage: core show channeltype <name>\n"
362 " Show details about the specified channel type, <name>.\n";
365 return complete_channeltypes(a);
369 return CLI_SHOWUSAGE;
371 AST_RWLIST_RDLOCK(&backends);
373 AST_RWLIST_TRAVERSE(&backends, cl, list) {
374 if (!strncasecmp(cl->tech->type, a->argv[3], strlen(cl->tech->type)))
380 ast_cli(a->fd, "\n%s is not a registered channel driver.\n", a->argv[3]);
381 AST_RWLIST_UNLOCK(&backends);
386 "-- Info about channel driver: %s --\n"
387 " Device State: %s\n"
390 " Capabilities: %s\n"
394 " Image Support: %s\n"
395 " Text Support: %s\n",
397 (cl->tech->devicestate) ? "yes" : "no",
398 (cl->tech->indicate) ? "yes" : "no",
399 (cl->tech->transfer) ? "yes" : "no",
400 ast_getformatname_multiple(buf, sizeof(buf), cl->tech->capabilities),
401 (cl->tech->send_digit_begin) ? "yes" : "no",
402 (cl->tech->send_digit_end) ? "yes" : "no",
403 (cl->tech->send_html) ? "yes" : "no",
404 (cl->tech->send_image) ? "yes" : "no",
405 (cl->tech->send_text) ? "yes" : "no"
409 AST_RWLIST_UNLOCK(&backends);
414 static struct ast_cli_entry cli_channel[] = {
415 AST_CLI_DEFINE(handle_cli_core_show_channeltypes, "List available channel types"),
416 AST_CLI_DEFINE(handle_cli_core_show_channeltype, "Give more details on that channel type")
419 static struct ast_frame *kill_read(struct ast_channel *chan)
421 /* Hangup channel. */
425 static struct ast_frame *kill_exception(struct ast_channel *chan)
427 /* Hangup channel. */
431 static int kill_write(struct ast_channel *chan, struct ast_frame *frame)
433 /* Hangup channel. */
437 static int kill_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
439 /* No problem fixing up the channel. */
443 static int kill_hangup(struct ast_channel *chan)
445 ast_channel_tech_pvt_set(chan, NULL);
450 * \brief Kill the channel channel driver technology descriptor.
453 * The purpose of this channel technology is to encourage the
454 * channel to hangup as quickly as possible.
456 * \note Used by DTMF atxfer and zombie channels.
458 const struct ast_channel_tech ast_kill_tech = {
460 .description = "Kill channel (should not see this)",
462 .exception = kill_exception,
465 .hangup = kill_hangup,
469 /*! \brief Destructor for the channel trace datastore */
470 static void ast_chan_trace_destroy_cb(void *data)
472 struct ast_chan_trace *trace;
473 struct ast_chan_trace_data *traced = data;
474 while ((trace = AST_LIST_REMOVE_HEAD(&traced->trace, entry))) {
480 /*! \brief Datastore to put the linked list of ast_chan_trace and trace status */
481 static const struct ast_datastore_info ast_chan_trace_datastore_info = {
483 .destroy = ast_chan_trace_destroy_cb
486 /*! \brief Put the channel backtrace in a string */
487 int ast_channel_trace_serialize(struct ast_channel *chan, struct ast_str **buf)
490 struct ast_chan_trace *trace;
491 struct ast_chan_trace_data *traced;
492 struct ast_datastore *store;
494 ast_channel_lock(chan);
495 store = ast_channel_datastore_find(chan, &ast_chan_trace_datastore_info, NULL);
497 ast_channel_unlock(chan);
500 traced = store->data;
502 AST_LIST_TRAVERSE(&traced->trace, trace, entry) {
503 if (ast_str_append(buf, 0, "[%d] => %s, %s, %d\n", total, trace->context, trace->exten, trace->priority) < 0) {
504 ast_log(LOG_ERROR, "Data Buffer Size Exceeded!\n");
510 ast_channel_unlock(chan);
514 /* !\brief Whether or not context tracing is enabled */
515 int ast_channel_trace_is_enabled(struct ast_channel *chan)
517 struct ast_datastore *store = ast_channel_datastore_find(chan, &ast_chan_trace_datastore_info, NULL);
520 return ((struct ast_chan_trace_data *)store->data)->enabled;
523 /*! \brief Update the context backtrace data if tracing is enabled */
524 static int ast_channel_trace_data_update(struct ast_channel *chan, struct ast_chan_trace_data *traced)
526 struct ast_chan_trace *trace;
527 if (!traced->enabled)
529 /* If the last saved context does not match the current one
530 OR we have not saved any context so far, then save the current context */
531 if ((!AST_LIST_EMPTY(&traced->trace) && strcasecmp(AST_LIST_FIRST(&traced->trace)->context, ast_channel_context(chan))) ||
532 (AST_LIST_EMPTY(&traced->trace))) {
533 /* Just do some debug logging */
534 if (AST_LIST_EMPTY(&traced->trace))
535 ast_debug(1, "Setting initial trace context to %s\n", ast_channel_context(chan));
537 ast_debug(1, "Changing trace context from %s to %s\n", AST_LIST_FIRST(&traced->trace)->context, ast_channel_context(chan));
538 /* alloc or bail out */
539 trace = ast_malloc(sizeof(*trace));
542 /* save the current location and store it in the trace list */
543 ast_copy_string(trace->context, ast_channel_context(chan), sizeof(trace->context));
544 ast_copy_string(trace->exten, ast_channel_exten(chan), sizeof(trace->exten));
545 trace->priority = ast_channel_priority(chan);
546 AST_LIST_INSERT_HEAD(&traced->trace, trace, entry);
551 /*! \brief Update the context backtrace if tracing is enabled */
552 int ast_channel_trace_update(struct ast_channel *chan)
554 struct ast_datastore *store = ast_channel_datastore_find(chan, &ast_chan_trace_datastore_info, NULL);
557 return ast_channel_trace_data_update(chan, store->data);
560 /*! \brief Enable context tracing in the channel */
561 int ast_channel_trace_enable(struct ast_channel *chan)
563 struct ast_datastore *store = ast_channel_datastore_find(chan, &ast_chan_trace_datastore_info, NULL);
564 struct ast_chan_trace_data *traced;
566 store = ast_datastore_alloc(&ast_chan_trace_datastore_info, "ChanTrace");
569 traced = ast_calloc(1, sizeof(*traced));
571 ast_datastore_free(store);
574 store->data = traced;
575 AST_LIST_HEAD_INIT_NOLOCK(&traced->trace);
576 ast_channel_datastore_add(chan, store);
578 ((struct ast_chan_trace_data *)store->data)->enabled = 1;
579 ast_channel_trace_data_update(chan, store->data);
583 /*! \brief Disable context tracing in the channel */
584 int ast_channel_trace_disable(struct ast_channel *chan)
586 struct ast_datastore *store = ast_channel_datastore_find(chan, &ast_chan_trace_datastore_info, NULL);
589 ((struct ast_chan_trace_data *)store->data)->enabled = 0;
592 #endif /* CHANNEL_TRACE */
594 /*! \brief Checks to see if a channel is needing hang up */
595 int ast_check_hangup(struct ast_channel *chan)
597 if (ast_channel_softhangup_internal_flag(chan)) /* yes if soft hangup flag set */
599 if (ast_tvzero(*ast_channel_whentohangup(chan))) /* no if no hangup scheduled */
601 if (ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow()) > 0) /* no if hangup time has not come yet. */
603 ast_debug(4, "Hangup time has come: %" PRIi64 "\n", ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow()));
604 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_TIMEOUT); /* record event */
608 int ast_check_hangup_locked(struct ast_channel *chan)
611 ast_channel_lock(chan);
612 res = ast_check_hangup(chan);
613 ast_channel_unlock(chan);
617 void ast_channel_softhangup_withcause_locked(struct ast_channel *chan, int causecode)
619 ast_channel_lock(chan);
622 ast_debug(1, "Setting hangupcause of channel %s to %d (is %d now)\n",
623 ast_channel_name(chan), causecode, ast_channel_hangupcause(chan));
625 ast_channel_hangupcause_set(chan, causecode);
628 ast_softhangup_nolock(chan, AST_SOFTHANGUP_EXPLICIT);
630 ast_channel_unlock(chan);
633 static int ast_channel_softhangup_cb(void *obj, void *arg, int flags)
635 struct ast_channel *chan = obj;
637 ast_softhangup(chan, AST_SOFTHANGUP_SHUTDOWN);
642 void ast_begin_shutdown(int hangup)
647 ao2_callback(channels, OBJ_NODATA | OBJ_MULTIPLE, ast_channel_softhangup_cb, NULL);
651 /*! \brief returns number of active/allocated channels */
652 int ast_active_channels(void)
654 return channels ? ao2_container_count(channels) : 0;
657 int ast_undestroyed_channels(void)
659 return ast_atomic_fetchadd_int(&chancount, 0);
662 /*! \brief Cancel a shutdown in progress */
663 void ast_cancel_shutdown(void)
668 /*! \brief Returns non-zero if Asterisk is being shut down */
669 int ast_shutting_down(void)
671 return shutting_down;
674 /*! \brief Set when to hangup channel */
675 void ast_channel_setwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
677 if (ast_tvzero(offset)) {
678 ast_channel_whentohangup_set(chan, &offset);
680 struct timeval tv = ast_tvadd(offset, ast_tvnow());
681 ast_channel_whentohangup_set(chan, &tv);
683 ast_queue_frame(chan, &ast_null_frame);
687 void ast_channel_setwhentohangup(struct ast_channel *chan, time_t offset)
689 struct timeval when = { offset, };
690 ast_channel_setwhentohangup_tv(chan, when);
693 /*! \brief Compare a offset with when to hangup channel */
694 int ast_channel_cmpwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
696 struct timeval whentohangup;
698 if (ast_tvzero(*ast_channel_whentohangup(chan)))
699 return ast_tvzero(offset) ? 0 : -1;
701 if (ast_tvzero(offset))
704 whentohangup = ast_tvadd(offset, ast_tvnow());
706 return ast_tvdiff_ms(whentohangup, *ast_channel_whentohangup(chan));
709 int ast_channel_cmpwhentohangup(struct ast_channel *chan, time_t offset)
711 struct timeval when = { offset, };
712 return ast_channel_cmpwhentohangup_tv(chan, when);
715 /*! \brief Register a new telephony channel in Asterisk */
716 int ast_channel_register(const struct ast_channel_tech *tech)
718 struct chanlist *chan;
720 AST_RWLIST_WRLOCK(&backends);
722 AST_RWLIST_TRAVERSE(&backends, chan, list) {
723 if (!strcasecmp(tech->type, chan->tech->type)) {
724 ast_log(LOG_WARNING, "Already have a handler for type '%s'\n", tech->type);
725 AST_RWLIST_UNLOCK(&backends);
730 if (!(chan = ast_calloc(1, sizeof(*chan)))) {
731 AST_RWLIST_UNLOCK(&backends);
735 AST_RWLIST_INSERT_HEAD(&backends, chan, list);
737 ast_debug(1, "Registered handler for '%s' (%s)\n", chan->tech->type, chan->tech->description);
739 ast_verb(2, "Registered channel type '%s' (%s)\n", chan->tech->type, chan->tech->description);
741 AST_RWLIST_UNLOCK(&backends);
746 /*! \brief Unregister channel driver */
747 void ast_channel_unregister(const struct ast_channel_tech *tech)
749 struct chanlist *chan;
751 ast_debug(1, "Unregistering channel type '%s'\n", tech->type);
753 AST_RWLIST_WRLOCK(&backends);
755 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&backends, chan, list) {
756 if (chan->tech == tech) {
757 AST_LIST_REMOVE_CURRENT(list);
759 ast_verb(2, "Unregistered channel type '%s'\n", tech->type);
763 AST_LIST_TRAVERSE_SAFE_END;
765 AST_RWLIST_UNLOCK(&backends);
768 /*! \brief Get handle to channel driver based on name */
769 const struct ast_channel_tech *ast_get_channel_tech(const char *name)
771 struct chanlist *chanls;
772 const struct ast_channel_tech *ret = NULL;
774 AST_RWLIST_RDLOCK(&backends);
776 AST_RWLIST_TRAVERSE(&backends, chanls, list) {
777 if (!strcasecmp(name, chanls->tech->type)) {
783 AST_RWLIST_UNLOCK(&backends);
788 /*! \brief Gives the string form of a given hangup cause */
789 const char *ast_cause2str(int cause)
793 for (x = 0; x < ARRAY_LEN(causes); x++) {
794 if (causes[x].cause == cause)
795 return causes[x].desc;
801 /*! \brief Convert a symbolic hangup cause to number */
802 int ast_str2cause(const char *name)
806 for (x = 0; x < ARRAY_LEN(causes); x++)
807 if (!strncasecmp(causes[x].name, name, strlen(causes[x].name)))
808 return causes[x].cause;
813 /*! \brief Gives the string form of a given channel state.
815 * \note This function is not reentrant.
819 const char *ast_state2str(enum ast_channel_state state)
826 case AST_STATE_RESERVED:
828 case AST_STATE_OFFHOOK:
830 case AST_STATE_DIALING:
834 case AST_STATE_RINGING:
840 case AST_STATE_DIALING_OFFHOOK:
841 return "Dialing Offhook";
842 case AST_STATE_PRERING:
845 if (!(buf = ast_threadstorage_get(&state2str_threadbuf, STATE2STR_BUFSIZE)))
847 snprintf(buf, STATE2STR_BUFSIZE, "Unknown (%d)", state);
852 /*! \brief Gives the string form of a given transfer capability */
853 char *ast_transfercapability2str(int transfercapability)
855 switch (transfercapability) {
856 case AST_TRANS_CAP_SPEECH:
858 case AST_TRANS_CAP_DIGITAL:
860 case AST_TRANS_CAP_RESTRICTED_DIGITAL:
861 return "RESTRICTED_DIGITAL";
862 case AST_TRANS_CAP_3_1K_AUDIO:
864 case AST_TRANS_CAP_DIGITAL_W_TONES:
865 return "DIGITAL_W_TONES";
866 case AST_TRANS_CAP_VIDEO:
873 /*! \brief Pick the best audio codec */
874 struct ast_format *ast_best_codec(struct ast_format_cap *cap, struct ast_format *result)
876 /* This just our opinion, expressed in code. We are asked to choose
877 the best codec to use, given no information */
878 static const enum ast_format_id prefs[] =
880 /*! Okay, ulaw is used by all telephony equipment, so start with it */
882 /*! Unless of course, you're a silly European, so then prefer ALAW */
888 /*! G.722 is better then all below, but not as common as the above... so give ulaw and alaw priority */
890 /*! Okay, well, signed linear is easy to translate into other stuff */
891 AST_FORMAT_SLINEAR192,
892 AST_FORMAT_SLINEAR96,
893 AST_FORMAT_SLINEAR48,
894 AST_FORMAT_SLINEAR44,
895 AST_FORMAT_SLINEAR32,
896 AST_FORMAT_SLINEAR24,
897 AST_FORMAT_SLINEAR16,
898 AST_FORMAT_SLINEAR12,
900 /*! G.726 is standard ADPCM, in RFC3551 packing order */
902 /*! G.726 is standard ADPCM, in AAL2 packing order */
903 AST_FORMAT_G726_AAL2,
904 /*! ADPCM has great sound quality and is still pretty easy to translate */
906 /*! Okay, we're down to vocoders now, so pick GSM because it's small and easier to
907 translate and sounds pretty good */
909 /*! iLBC is not too bad */
911 /*! Speex is free, but computationally more expensive than GSM */
915 /*! SILK is pretty awesome. */
917 /*! CELT supports crazy high sample rates */
919 /*! Ick, LPC10 sounds terrible, but at least we have code for it, if you're tacky enough
922 /*! G.729a is faster than 723 and slightly less expensive */
924 /*! Down to G.723.1 which is proprietary but at least designed for voice */
930 /* Find the first preferred codec in the format given */
931 for (x = 0; x < ARRAY_LEN(prefs); x++) {
932 if (ast_format_cap_best_byid(cap, prefs[x], result)) {
937 ast_format_clear(result);
938 ast_log(LOG_WARNING, "Don't know any of %s formats\n", ast_getformatname_multiple(buf, sizeof(buf), cap));
943 static const struct ast_channel_tech null_tech = {
945 .description = "Null channel (should not see this)",
948 static void ast_channel_destructor(void *obj);
949 static void ast_dummy_channel_destructor(void *obj);
951 /*! \brief Create a new channel structure */
952 static struct ast_channel * attribute_malloc __attribute__((format(printf, 13, 0)))
953 __ast_channel_alloc_ap(int needqueue, int state, const char *cid_num, const char *cid_name,
954 const char *acctcode, const char *exten, const char *context,
955 const char *linkedid, const int amaflag, const char *file, int line,
956 const char *function, const char *name_fmt, va_list ap)
958 struct ast_channel *tmp;
959 struct varshead *headp;
960 char *tech = "", *tech2 = NULL;
961 struct ast_format_cap *nativeformats;
962 struct ast_sched_context *schedctx;
963 struct ast_timer *timer;
966 /* If shutting down, don't allocate any new channels */
967 if (ast_shutting_down()) {
968 ast_log(LOG_WARNING, "Channel allocation failed: Refusing due to active shutdown\n");
972 if (!(tmp = ast_channel_internal_alloc(ast_channel_destructor))) {
973 /* Channel structure allocation failure. */
976 if (!(nativeformats = ast_format_cap_alloc())) {
978 /* format capabilities structure allocation failure */
981 ast_channel_nativeformats_set(tmp, nativeformats);
984 * Init file descriptors to unopened state so
985 * the destructor can know not to close them.
987 ast_channel_timingfd_set(tmp, -1);
988 ast_channel_internal_alertpipe_clear(tmp);
989 ast_channel_internal_fd_clear_all(tmp);
992 ast_channel_epfd_set(tmp, epoll_create(25));
995 if (!(schedctx = ast_sched_context_create())) {
996 ast_log(LOG_WARNING, "Channel allocation failed: Unable to create schedule context\n");
997 return ast_channel_unref(tmp);
999 ast_channel_sched_set(tmp, schedctx);
1001 ast_party_dialed_init(ast_channel_dialed(tmp));
1002 ast_party_caller_init(ast_channel_caller(tmp));
1003 ast_party_connected_line_init(ast_channel_connected(tmp));
1004 ast_party_redirecting_init(ast_channel_redirecting(tmp));
1007 ast_channel_caller(tmp)->id.name.valid = 1;
1008 ast_channel_caller(tmp)->id.name.str = ast_strdup(cid_name);
1009 if (!ast_channel_caller(tmp)->id.name.str) {
1010 return ast_channel_unref(tmp);
1014 ast_channel_caller(tmp)->id.number.valid = 1;
1015 ast_channel_caller(tmp)->id.number.str = ast_strdup(cid_num);
1016 if (!ast_channel_caller(tmp)->id.number.str) {
1017 return ast_channel_unref(tmp);
1021 if ((timer = ast_timer_open())) {
1022 ast_channel_timer_set(tmp, timer);
1023 if (strcmp(ast_timer_get_name(ast_channel_timer(tmp)), "timerfd")) {
1026 ast_channel_timingfd_set(tmp, ast_timer_fd(ast_channel_timer(tmp)));
1030 * This is the last place the channel constructor can fail.
1032 * The destructor takes advantage of this fact to ensure that the
1033 * AST_CEL_CHANNEL_END is not posted if we have not posted the
1034 * AST_CEL_CHANNEL_START yet.
1037 if (needqueue && ast_channel_internal_alertpipe_init(tmp)) {
1038 return ast_channel_unref(tmp);
1041 /* Always watch the alertpipe */
1042 ast_channel_set_fd(tmp, AST_ALERT_FD, ast_channel_internal_alert_readfd(tmp));
1043 /* And timing pipe */
1044 ast_channel_set_fd(tmp, AST_TIMING_FD, ast_channel_timingfd(tmp));
1047 ast_channel_state_set(tmp, state);
1049 ast_channel_streamid_set(tmp, -1);
1051 ast_channel_fin_set(tmp, global_fin);
1052 ast_channel_fout_set(tmp, global_fout);
1055 ast_channel_creationtime_set(tmp, &now);
1057 if (ast_strlen_zero(ast_config_AST_SYSTEM_NAME)) {
1058 ast_channel_uniqueid_build(tmp, "%li.%d", (long) time(NULL),
1059 ast_atomic_fetchadd_int(&uniqueint, 1));
1061 ast_channel_uniqueid_build(tmp, "%s-%li.%d", ast_config_AST_SYSTEM_NAME,
1062 (long) time(NULL), ast_atomic_fetchadd_int(&uniqueint, 1));
1065 if (!ast_strlen_zero(linkedid)) {
1066 ast_channel_linkedid_set(tmp, linkedid);
1068 ast_channel_linkedid_set(tmp, ast_channel_uniqueid(tmp));
1071 if (!ast_strlen_zero(name_fmt)) {
1072 char *slash, *slash2;
1073 /* Almost every channel is calling this function, and setting the name via the ast_string_field_build() call.
1074 * And they all use slightly different formats for their name string.
1075 * This means, to set the name here, we have to accept variable args, and call the string_field_build from here.
1076 * This means, that the stringfields must have a routine that takes the va_lists directly, and
1077 * uses them to build the string, instead of forming the va_lists internally from the vararg ... list.
1078 * This new function was written so this can be accomplished.
1080 ast_channel_name_build_va(tmp, name_fmt, ap);
1081 tech = ast_strdupa(ast_channel_name(tmp));
1082 if ((slash = strchr(tech, '/'))) {
1083 if ((slash2 = strchr(slash + 1, '/'))) {
1091 * Start the string with '-' so it becomes an empty string
1092 * in the destructor.
1094 ast_channel_name_set(tmp, "-**Unknown**");
1097 /* Reminder for the future: under what conditions do we NOT want to track cdrs on channels? */
1099 /* These 4 variables need to be set up for the cdr_init() to work right */
1101 ast_channel_amaflags_set(tmp, amaflag);
1103 ast_channel_amaflags_set(tmp, ast_default_amaflags);
1106 if (!ast_strlen_zero(acctcode))
1107 ast_channel_accountcode_set(tmp, acctcode);
1109 ast_channel_accountcode_set(tmp, ast_default_accountcode);
1111 ast_channel_context_set(tmp, S_OR(context, "default"));
1112 ast_channel_exten_set(tmp, S_OR(exten, "s"));
1113 ast_channel_priority_set(tmp, 1);
1115 ast_channel_cdr_set(tmp, ast_cdr_alloc());
1116 ast_cdr_init(ast_channel_cdr(tmp), tmp);
1117 ast_cdr_start(ast_channel_cdr(tmp));
1119 ast_atomic_fetchadd_int(&chancount, +1);
1120 ast_cel_report_event(tmp, AST_CEL_CHANNEL_START, NULL, NULL, NULL);
1122 headp = ast_channel_varshead(tmp);
1123 AST_LIST_HEAD_INIT_NOLOCK(headp);
1125 ast_pbx_hangup_handler_init(tmp);
1126 AST_LIST_HEAD_INIT_NOLOCK(ast_channel_datastores(tmp));
1128 AST_LIST_HEAD_INIT_NOLOCK(ast_channel_autochans(tmp));
1130 ast_channel_language_set(tmp, defaultlanguage);
1132 ast_channel_tech_set(tmp, &null_tech);
1134 ao2_link(channels, tmp);
1137 * And now, since the channel structure is built, and has its name, let's
1138 * call the manager event generator with this Newchannel event. This is the
1139 * proper and correct place to make this call, but you sure do have to pass
1140 * a lot of data into this func to do it here!
1142 if (ast_get_channel_tech(tech) || (tech2 && ast_get_channel_tech(tech2))) {
1144 <managerEventInstance>
1145 <synopsis>Raised when a new channel is created.</synopsis>
1147 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newstate']/managerEventInstance/syntax/parameter[@name='ChannelState'])" />
1148 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newstate']/managerEventInstance/syntax/parameter[@name='ChannelStateDesc'])" />
1150 </managerEventInstance>
1152 ast_manager_event(tmp, EVENT_FLAG_CALL, "Newchannel",
1154 "ChannelState: %d\r\n"
1155 "ChannelStateDesc: %s\r\n"
1156 "CallerIDNum: %s\r\n"
1157 "CallerIDName: %s\r\n"
1158 "AccountCode: %s\r\n"
1162 ast_channel_name(tmp),
1164 ast_state2str(state),
1167 ast_channel_accountcode(tmp),
1170 ast_channel_uniqueid(tmp));
1173 ast_channel_internal_finalize(tmp);
1177 struct ast_channel *__ast_channel_alloc(int needqueue, int state, const char *cid_num,
1178 const char *cid_name, const char *acctcode,
1179 const char *exten, const char *context,
1180 const char *linkedid, const int amaflag,
1181 const char *file, int line, const char *function,
1182 const char *name_fmt, ...)
1185 struct ast_channel *result;
1187 va_start(ap, name_fmt);
1188 result = __ast_channel_alloc_ap(needqueue, state, cid_num, cid_name, acctcode, exten, context,
1189 linkedid, amaflag, file, line, function, name_fmt, ap);
1195 /* only do the minimum amount of work needed here to make a channel
1196 * structure that can be used to expand channel vars */
1197 #if defined(REF_DEBUG) || defined(__AST_DEBUG_MALLOC)
1198 struct ast_channel *__ast_dummy_channel_alloc(const char *file, int line, const char *function)
1200 struct ast_channel *ast_dummy_channel_alloc(void)
1203 struct ast_channel *tmp;
1204 struct varshead *headp;
1206 if (!(tmp = ast_channel_internal_alloc(ast_dummy_channel_destructor))) {
1207 /* Dummy channel structure allocation failure. */
1211 ast_pbx_hangup_handler_init(tmp);
1212 AST_LIST_HEAD_INIT_NOLOCK(ast_channel_datastores(tmp));
1215 * Init file descriptors to unopened state just in case
1216 * autoservice is called on the channel or something tries to
1217 * read a frame from it.
1219 ast_channel_timingfd_set(tmp, -1);
1220 ast_channel_internal_alertpipe_clear(tmp);
1221 ast_channel_internal_fd_clear_all(tmp);
1223 ast_channel_epfd_set(tmp, -1);
1226 headp = ast_channel_varshead(tmp);
1227 AST_LIST_HEAD_INIT_NOLOCK(headp);
1232 static int __ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin, int head, struct ast_frame *after)
1234 struct ast_frame *f;
1235 struct ast_frame *cur;
1236 unsigned int new_frames = 0;
1237 unsigned int new_voice_frames = 0;
1238 unsigned int queued_frames = 0;
1239 unsigned int queued_voice_frames = 0;
1240 AST_LIST_HEAD_NOLOCK(,ast_frame) frames;
1242 ast_channel_lock(chan);
1245 * Check the last frame on the queue if we are queuing the new
1248 cur = AST_LIST_LAST(ast_channel_readq(chan));
1249 if (cur && cur->frametype == AST_FRAME_CONTROL && !head && (!after || after == cur)) {
1250 switch (cur->subclass.integer) {
1251 case AST_CONTROL_END_OF_Q:
1252 if (fin->frametype == AST_FRAME_CONTROL
1253 && fin->subclass.integer == AST_CONTROL_HANGUP) {
1255 * Destroy the end-of-Q marker frame so we can queue the hangup
1256 * frame in its place.
1258 AST_LIST_REMOVE(ast_channel_readq(chan), cur, frame_list);
1262 * This has degenerated to a normal queue append anyway. Since
1263 * we just destroyed the last frame in the queue we must make
1264 * sure that "after" is NULL or bad things will happen.
1270 case AST_CONTROL_HANGUP:
1271 /* Don't queue anything. */
1272 ast_channel_unlock(chan);
1279 /* Build copies of all the new frames and count them */
1280 AST_LIST_HEAD_INIT_NOLOCK(&frames);
1281 for (cur = fin; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
1282 if (!(f = ast_frdup(cur))) {
1283 if (AST_LIST_FIRST(&frames)) {
1284 ast_frfree(AST_LIST_FIRST(&frames));
1286 ast_channel_unlock(chan);
1290 AST_LIST_INSERT_TAIL(&frames, f, frame_list);
1292 if (f->frametype == AST_FRAME_VOICE) {
1297 /* Count how many frames exist on the queue */
1298 AST_LIST_TRAVERSE(ast_channel_readq(chan), cur, frame_list) {
1300 if (cur->frametype == AST_FRAME_VOICE) {
1301 queued_voice_frames++;
1305 if ((queued_frames + new_frames > 128 || queued_voice_frames + new_voice_frames > 96)) {
1307 ast_log(LOG_WARNING, "Exceptionally long %squeue length queuing to %s\n", queued_frames + new_frames > 128 ? "" : "voice ", ast_channel_name(chan));
1308 AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_readq(chan), cur, frame_list) {
1309 /* Save the most recent frame */
1310 if (!AST_LIST_NEXT(cur, frame_list)) {
1312 } else if (cur->frametype == AST_FRAME_VOICE || cur->frametype == AST_FRAME_VIDEO || cur->frametype == AST_FRAME_NULL) {
1316 AST_LIST_REMOVE_CURRENT(frame_list);
1320 AST_LIST_TRAVERSE_SAFE_END;
1324 AST_LIST_INSERT_LIST_AFTER(ast_channel_readq(chan), &frames, after, frame_list);
1327 AST_LIST_APPEND_LIST(&frames, ast_channel_readq(chan), frame_list);
1328 AST_LIST_HEAD_INIT_NOLOCK(ast_channel_readq(chan));
1330 AST_LIST_APPEND_LIST(ast_channel_readq(chan), &frames, frame_list);
1333 if (ast_channel_alert_writable(chan)) {
1334 if (ast_channel_alert_write(chan)) {
1335 ast_log(LOG_WARNING, "Unable to write to alert pipe on %s (qlen = %d): %s!\n",
1336 ast_channel_name(chan), queued_frames, strerror(errno));
1338 } else if (ast_channel_timingfd(chan) > -1) {
1339 ast_timer_enable_continuous(ast_channel_timer(chan));
1340 } else if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING)) {
1341 pthread_kill(ast_channel_blocker(chan), SIGURG);
1344 ast_channel_unlock(chan);
1349 int ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin)
1351 return __ast_queue_frame(chan, fin, 0, NULL);
1354 int ast_queue_frame_head(struct ast_channel *chan, struct ast_frame *fin)
1356 return __ast_queue_frame(chan, fin, 1, NULL);
1359 /*! \brief Queue a hangup frame for channel */
1360 int ast_queue_hangup(struct ast_channel *chan)
1362 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
1365 /* Yeah, let's not change a lock-critical value without locking */
1366 ast_channel_lock(chan);
1367 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
1369 <managerEventInstance>
1370 <synopsis>Raised when a hangup is requested with no set cause.</synopsis>
1371 </managerEventInstance>
1373 manager_event(EVENT_FLAG_CALL, "HangupRequest",
1376 ast_channel_name(chan),
1377 ast_channel_uniqueid(chan));
1379 res = ast_queue_frame(chan, &f);
1380 ast_channel_unlock(chan);
1384 /*! \brief Queue a hangup frame for channel */
1385 int ast_queue_hangup_with_cause(struct ast_channel *chan, int cause)
1387 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
1391 f.data.uint32 = cause;
1394 /* Yeah, let's not change a lock-critical value without locking */
1395 ast_channel_lock(chan);
1396 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
1398 f.data.uint32 = ast_channel_hangupcause(chan);
1401 <managerEventInstance>
1402 <synopsis>Raised when a hangup is requested with a specific cause code.</synopsis>
1404 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Hangup']/managerEventInstance/syntax/parameter[@name='Cause'])" />
1406 </managerEventInstance>
1408 manager_event(EVENT_FLAG_CALL, "HangupRequest",
1412 ast_channel_name(chan),
1413 ast_channel_uniqueid(chan),
1416 res = ast_queue_frame(chan, &f);
1417 ast_channel_unlock(chan);
1421 /*! \brief Queue a control frame */
1422 int ast_queue_control(struct ast_channel *chan, enum ast_control_frame_type control)
1424 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control };
1425 return ast_queue_frame(chan, &f);
1428 /*! \brief Queue a control frame with payload */
1429 int ast_queue_control_data(struct ast_channel *chan, enum ast_control_frame_type control,
1430 const void *data, size_t datalen)
1432 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control, .data.ptr = (void *) data, .datalen = datalen };
1433 return ast_queue_frame(chan, &f);
1436 /*! \brief Set defer DTMF flag on channel */
1437 int ast_channel_defer_dtmf(struct ast_channel *chan)
1442 pre = ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF);
1443 ast_set_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF);
1448 /*! \brief Unset defer DTMF flag on channel */
1449 void ast_channel_undefer_dtmf(struct ast_channel *chan)
1452 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF);
1455 struct ast_channel *ast_channel_callback(ao2_callback_data_fn *cb_fn, void *arg,
1456 void *data, int ao2_flags)
1458 return ao2_callback_data(channels, ao2_flags, cb_fn, arg, data);
1461 static int ast_channel_by_name_cb(void *obj, void *arg, void *data, int flags)
1463 struct ast_channel *chan = obj;
1464 const char *name = arg;
1465 size_t name_len = *(size_t *) data;
1466 int ret = CMP_MATCH;
1468 if (ast_strlen_zero(name)) {
1469 ast_log(LOG_ERROR, "BUG! Must supply a channel name or partial name to match!\n");
1473 ast_channel_lock(chan);
1474 if ((!name_len && strcasecmp(ast_channel_name(chan), name))
1475 || (name_len && strncasecmp(ast_channel_name(chan), name, name_len))) {
1476 ret = 0; /* name match failed, keep looking */
1478 ast_channel_unlock(chan);
1483 static int ast_channel_by_exten_cb(void *obj, void *arg, void *data, int flags)
1485 struct ast_channel *chan = obj;
1486 char *context = arg;
1488 int ret = CMP_MATCH;
1490 if (ast_strlen_zero(exten) || ast_strlen_zero(context)) {
1491 ast_log(LOG_ERROR, "BUG! Must have a context and extension to match!\n");
1495 ast_channel_lock(chan);
1496 if (strcasecmp(ast_channel_context(chan), context) && strcasecmp(ast_channel_macrocontext(chan), context)) {
1497 ret = 0; /* Context match failed, continue */
1498 } else if (strcasecmp(ast_channel_exten(chan), exten) && strcasecmp(ast_channel_macroexten(chan), exten)) {
1499 ret = 0; /* Extension match failed, continue */
1501 ast_channel_unlock(chan);
1506 static int ast_channel_by_uniqueid_cb(void *obj, void *arg, void *data, int flags)
1508 struct ast_channel *chan = obj;
1509 char *uniqueid = arg;
1510 size_t id_len = *(size_t *) data;
1511 int ret = CMP_MATCH;
1513 if (ast_strlen_zero(uniqueid)) {
1514 ast_log(LOG_ERROR, "BUG! Must supply a uniqueid or partial uniqueid to match!\n");
1518 ast_channel_lock(chan);
1519 if ((!id_len && strcasecmp(ast_channel_uniqueid(chan), uniqueid))
1520 || (id_len && strncasecmp(ast_channel_uniqueid(chan), uniqueid, id_len))) {
1521 ret = 0; /* uniqueid match failed, keep looking */
1523 ast_channel_unlock(chan);
1528 struct ast_channel_iterator {
1529 /* storage for non-dynamically allocated iterator */
1530 struct ao2_iterator simple_iterator;
1531 /* pointer to the actual iterator (simple_iterator or a dynamically
1532 * allocated iterator)
1534 struct ao2_iterator *active_iterator;
1537 struct ast_channel_iterator *ast_channel_iterator_destroy(struct ast_channel_iterator *i)
1539 ao2_iterator_destroy(i->active_iterator);
1545 struct ast_channel_iterator *ast_channel_iterator_by_exten_new(const char *exten, const char *context)
1547 struct ast_channel_iterator *i;
1548 char *l_exten = (char *) exten;
1549 char *l_context = (char *) context;
1551 if (!(i = ast_calloc(1, sizeof(*i)))) {
1555 i->active_iterator = (void *) ast_channel_callback(ast_channel_by_exten_cb,
1556 l_context, l_exten, OBJ_MULTIPLE);
1557 if (!i->active_iterator) {
1565 struct ast_channel_iterator *ast_channel_iterator_by_name_new(const char *name, size_t name_len)
1567 struct ast_channel_iterator *i;
1568 char *l_name = (char *) name;
1570 if (!(i = ast_calloc(1, sizeof(*i)))) {
1574 i->active_iterator = (void *) ast_channel_callback(ast_channel_by_name_cb,
1576 OBJ_MULTIPLE | (name_len == 0 /* match the whole word, so optimize */ ? OBJ_KEY : 0));
1577 if (!i->active_iterator) {
1585 struct ast_channel_iterator *ast_channel_iterator_all_new(void)
1587 struct ast_channel_iterator *i;
1589 if (!(i = ast_calloc(1, sizeof(*i)))) {
1593 i->simple_iterator = ao2_iterator_init(channels, 0);
1594 i->active_iterator = &i->simple_iterator;
1599 struct ast_channel *ast_channel_iterator_next(struct ast_channel_iterator *i)
1601 return ao2_iterator_next(i->active_iterator);
1604 /* Legacy function, not currently used for lookups, but we need a cmp_fn */
1605 static int ast_channel_cmp_cb(void *obj, void *arg, int flags)
1607 ast_log(LOG_ERROR, "BUG! Should never be called!\n");
1611 struct ast_channel *ast_channel_get_by_name_prefix(const char *name, size_t name_len)
1613 struct ast_channel *chan;
1614 char *l_name = (char *) name;
1616 chan = ast_channel_callback(ast_channel_by_name_cb, l_name, &name_len,
1617 (name_len == 0) /* optimize if it is a complete name match */ ? OBJ_KEY : 0);
1622 if (ast_strlen_zero(l_name)) {
1623 /* We didn't have a name to search for so quit. */
1627 /* Now try a search for uniqueid. */
1628 return ast_channel_callback(ast_channel_by_uniqueid_cb, l_name, &name_len, 0);
1631 struct ast_channel *ast_channel_get_by_name(const char *name)
1633 return ast_channel_get_by_name_prefix(name, 0);
1636 struct ast_channel *ast_channel_get_by_exten(const char *exten, const char *context)
1638 char *l_exten = (char *) exten;
1639 char *l_context = (char *) context;
1641 return ast_channel_callback(ast_channel_by_exten_cb, l_context, l_exten, 0);
1644 int ast_is_deferrable_frame(const struct ast_frame *frame)
1646 /* Do not add a default entry in this switch statement. Each new
1647 * frame type should be addressed directly as to whether it should
1648 * be queued up or not.
1650 switch (frame->frametype) {
1651 case AST_FRAME_CONTROL:
1652 case AST_FRAME_TEXT:
1653 case AST_FRAME_IMAGE:
1654 case AST_FRAME_HTML:
1657 case AST_FRAME_DTMF_END:
1658 case AST_FRAME_DTMF_BEGIN:
1659 case AST_FRAME_VOICE:
1660 case AST_FRAME_VIDEO:
1661 case AST_FRAME_NULL:
1664 case AST_FRAME_MODEM:
1670 /*! \brief Wait, look for hangups and condition arg */
1671 int ast_safe_sleep_conditional(struct ast_channel *chan, int ms, int (*cond)(void*), void *data)
1673 struct ast_frame *f;
1674 struct ast_silence_generator *silgen = NULL;
1676 AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_frames;
1678 AST_LIST_HEAD_INIT_NOLOCK(&deferred_frames);
1680 /* If no other generator is present, start silencegen while waiting */
1681 if (ast_opt_transmit_silence && !ast_channel_generatordata(chan)) {
1682 silgen = ast_channel_start_silence_generator(chan);
1686 struct ast_frame *dup_f = NULL;
1687 if (cond && ((*cond)(data) == 0)) {
1690 ms = ast_waitfor(chan, ms);
1702 if (!ast_is_deferrable_frame(f)) {
1707 if ((dup_f = ast_frisolate(f))) {
1711 AST_LIST_INSERT_HEAD(&deferred_frames, dup_f, frame_list);
1716 /* stop silgen if present */
1718 ast_channel_stop_silence_generator(chan, silgen);
1721 /* We need to free all the deferred frames, but we only need to
1722 * queue the deferred frames if there was no error and no
1723 * hangup was received
1725 ast_channel_lock(chan);
1726 while ((f = AST_LIST_REMOVE_HEAD(&deferred_frames, frame_list))) {
1728 ast_queue_frame_head(chan, f);
1732 ast_channel_unlock(chan);
1737 /*! \brief Wait, look for hangups */
1738 int ast_safe_sleep(struct ast_channel *chan, int ms)
1740 return ast_safe_sleep_conditional(chan, ms, NULL, NULL);
1743 struct ast_channel *ast_channel_release(struct ast_channel *chan)
1745 /* Safe, even if already unlinked. */
1746 ao2_unlink(channels, chan);
1747 return ast_channel_unref(chan);
1750 void ast_party_name_init(struct ast_party_name *init)
1753 init->char_set = AST_PARTY_CHAR_SET_ISO8859_1;
1754 init->presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
1758 void ast_party_name_copy(struct ast_party_name *dest, const struct ast_party_name *src)
1761 /* Don't copy to self */
1765 ast_free(dest->str);
1766 dest->str = ast_strdup(src->str);
1767 dest->char_set = src->char_set;
1768 dest->presentation = src->presentation;
1769 dest->valid = src->valid;
1772 void ast_party_name_set_init(struct ast_party_name *init, const struct ast_party_name *guide)
1775 init->char_set = guide->char_set;
1776 init->presentation = guide->presentation;
1777 init->valid = guide->valid;
1780 void ast_party_name_set(struct ast_party_name *dest, const struct ast_party_name *src)
1783 /* Don't set to self */
1787 if (src->str && src->str != dest->str) {
1788 ast_free(dest->str);
1789 dest->str = ast_strdup(src->str);
1792 dest->char_set = src->char_set;
1793 dest->presentation = src->presentation;
1794 dest->valid = src->valid;
1797 void ast_party_name_free(struct ast_party_name *doomed)
1799 ast_free(doomed->str);
1803 void ast_party_number_init(struct ast_party_number *init)
1806 init->plan = 0;/* Unknown */
1807 init->presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
1811 void ast_party_number_copy(struct ast_party_number *dest, const struct ast_party_number *src)
1814 /* Don't copy to self */
1818 ast_free(dest->str);
1819 dest->str = ast_strdup(src->str);
1820 dest->plan = src->plan;
1821 dest->presentation = src->presentation;
1822 dest->valid = src->valid;
1825 void ast_party_number_set_init(struct ast_party_number *init, const struct ast_party_number *guide)
1828 init->plan = guide->plan;
1829 init->presentation = guide->presentation;
1830 init->valid = guide->valid;
1833 void ast_party_number_set(struct ast_party_number *dest, const struct ast_party_number *src)
1836 /* Don't set to self */
1840 if (src->str && src->str != dest->str) {
1841 ast_free(dest->str);
1842 dest->str = ast_strdup(src->str);
1845 dest->plan = src->plan;
1846 dest->presentation = src->presentation;
1847 dest->valid = src->valid;
1850 void ast_party_number_free(struct ast_party_number *doomed)
1852 ast_free(doomed->str);
1856 void ast_party_subaddress_init(struct ast_party_subaddress *init)
1860 init->odd_even_indicator = 0;
1864 void ast_party_subaddress_copy(struct ast_party_subaddress *dest, const struct ast_party_subaddress *src)
1867 /* Don't copy to self */
1871 ast_free(dest->str);
1872 dest->str = ast_strdup(src->str);
1873 dest->type = src->type;
1874 dest->odd_even_indicator = src->odd_even_indicator;
1875 dest->valid = src->valid;
1878 void ast_party_subaddress_set_init(struct ast_party_subaddress *init, const struct ast_party_subaddress *guide)
1881 init->type = guide->type;
1882 init->odd_even_indicator = guide->odd_even_indicator;
1883 init->valid = guide->valid;
1886 void ast_party_subaddress_set(struct ast_party_subaddress *dest, const struct ast_party_subaddress *src)
1889 /* Don't set to self */
1893 if (src->str && src->str != dest->str) {
1894 ast_free(dest->str);
1895 dest->str = ast_strdup(src->str);
1898 dest->type = src->type;
1899 dest->odd_even_indicator = src->odd_even_indicator;
1900 dest->valid = src->valid;
1903 void ast_party_subaddress_free(struct ast_party_subaddress *doomed)
1905 ast_free(doomed->str);
1909 void ast_set_party_id_all(struct ast_set_party_id *update_id)
1911 update_id->name = 1;
1912 update_id->number = 1;
1913 update_id->subaddress = 1;
1916 void ast_party_id_init(struct ast_party_id *init)
1918 ast_party_name_init(&init->name);
1919 ast_party_number_init(&init->number);
1920 ast_party_subaddress_init(&init->subaddress);
1924 void ast_party_id_copy(struct ast_party_id *dest, const struct ast_party_id *src)
1927 /* Don't copy to self */
1931 ast_party_name_copy(&dest->name, &src->name);
1932 ast_party_number_copy(&dest->number, &src->number);
1933 ast_party_subaddress_copy(&dest->subaddress, &src->subaddress);
1935 ast_free(dest->tag);
1936 dest->tag = ast_strdup(src->tag);
1939 void ast_party_id_set_init(struct ast_party_id *init, const struct ast_party_id *guide)
1941 ast_party_name_set_init(&init->name, &guide->name);
1942 ast_party_number_set_init(&init->number, &guide->number);
1943 ast_party_subaddress_set_init(&init->subaddress, &guide->subaddress);
1947 void ast_party_id_set(struct ast_party_id *dest, const struct ast_party_id *src, const struct ast_set_party_id *update)
1950 /* Don't set to self */
1954 if (!update || update->name) {
1955 ast_party_name_set(&dest->name, &src->name);
1957 if (!update || update->number) {
1958 ast_party_number_set(&dest->number, &src->number);
1960 if (!update || update->subaddress) {
1961 ast_party_subaddress_set(&dest->subaddress, &src->subaddress);
1964 if (src->tag && src->tag != dest->tag) {
1965 ast_free(dest->tag);
1966 dest->tag = ast_strdup(src->tag);
1970 void ast_party_id_free(struct ast_party_id *doomed)
1972 ast_party_name_free(&doomed->name);
1973 ast_party_number_free(&doomed->number);
1974 ast_party_subaddress_free(&doomed->subaddress);
1976 ast_free(doomed->tag);
1980 int ast_party_id_presentation(const struct ast_party_id *id)
1982 int number_priority;
1984 int number_screening;
1988 /* Determine name presentation priority. */
1989 if (!id->name.valid) {
1990 name_value = AST_PRES_UNAVAILABLE;
1993 name_value = id->name.presentation & AST_PRES_RESTRICTION;
1994 switch (name_value) {
1995 case AST_PRES_RESTRICTED:
1998 case AST_PRES_ALLOWED:
2001 case AST_PRES_UNAVAILABLE:
2005 name_value = AST_PRES_UNAVAILABLE;
2011 /* Determine number presentation priority. */
2012 if (!id->number.valid) {
2013 number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
2014 number_value = AST_PRES_UNAVAILABLE;
2015 number_priority = 3;
2017 number_screening = id->number.presentation & AST_PRES_NUMBER_TYPE;
2018 number_value = id->number.presentation & AST_PRES_RESTRICTION;
2019 switch (number_value) {
2020 case AST_PRES_RESTRICTED:
2021 number_priority = 0;
2023 case AST_PRES_ALLOWED:
2024 number_priority = 1;
2026 case AST_PRES_UNAVAILABLE:
2027 number_priority = 2;
2030 number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
2031 number_value = AST_PRES_UNAVAILABLE;
2032 number_priority = 3;
2037 /* Select the wining presentation value. */
2038 if (name_priority < number_priority) {
2039 number_value = name_value;
2041 if (number_value == AST_PRES_UNAVAILABLE) {
2042 return AST_PRES_NUMBER_NOT_AVAILABLE;
2045 return number_value | number_screening;
2048 void ast_party_id_invalidate(struct ast_party_id *id)
2051 id->number.valid = 0;
2052 id->subaddress.valid = 0;
2055 void ast_party_id_reset(struct ast_party_id *id)
2057 ast_party_id_free(id);
2058 ast_party_id_init(id);
2061 struct ast_party_id ast_party_id_merge(struct ast_party_id *base, struct ast_party_id *overlay)
2063 struct ast_party_id merged;
2066 if (overlay->name.valid) {
2067 merged.name = overlay->name;
2069 if (overlay->number.valid) {
2070 merged.number = overlay->number;
2072 if (overlay->subaddress.valid) {
2073 merged.subaddress = overlay->subaddress;
2075 /* Note the actual structure is returned and not a pointer to it! */
2079 void ast_party_id_merge_copy(struct ast_party_id *dest, struct ast_party_id *base, struct ast_party_id *overlay)
2081 struct ast_party_id merged;
2083 merged = ast_party_id_merge(base, overlay);
2084 ast_party_id_copy(dest, &merged);
2087 void ast_party_dialed_init(struct ast_party_dialed *init)
2089 init->number.str = NULL;
2090 init->number.plan = 0;/* Unknown */
2091 ast_party_subaddress_init(&init->subaddress);
2092 init->transit_network_select = 0;
2095 void ast_party_dialed_copy(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
2098 /* Don't copy to self */
2102 ast_free(dest->number.str);
2103 dest->number.str = ast_strdup(src->number.str);
2104 dest->number.plan = src->number.plan;
2105 ast_party_subaddress_copy(&dest->subaddress, &src->subaddress);
2106 dest->transit_network_select = src->transit_network_select;
2109 void ast_party_dialed_set_init(struct ast_party_dialed *init, const struct ast_party_dialed *guide)
2111 init->number.str = NULL;
2112 init->number.plan = guide->number.plan;
2113 ast_party_subaddress_set_init(&init->subaddress, &guide->subaddress);
2114 init->transit_network_select = guide->transit_network_select;
2117 void ast_party_dialed_set(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
2119 if (src->number.str && src->number.str != dest->number.str) {
2120 ast_free(dest->number.str);
2121 dest->number.str = ast_strdup(src->number.str);
2123 dest->number.plan = src->number.plan;
2125 ast_party_subaddress_set(&dest->subaddress, &src->subaddress);
2127 dest->transit_network_select = src->transit_network_select;
2130 void ast_party_dialed_free(struct ast_party_dialed *doomed)
2132 ast_free(doomed->number.str);
2133 doomed->number.str = NULL;
2134 ast_party_subaddress_free(&doomed->subaddress);
2137 void ast_party_caller_init(struct ast_party_caller *init)
2139 ast_party_id_init(&init->id);
2140 ast_party_id_init(&init->ani);
2141 ast_party_id_init(&init->priv);
2145 void ast_party_caller_copy(struct ast_party_caller *dest, const struct ast_party_caller *src)
2148 /* Don't copy to self */
2152 ast_party_id_copy(&dest->id, &src->id);
2153 ast_party_id_copy(&dest->ani, &src->ani);
2154 ast_party_id_copy(&dest->priv, &src->priv);
2155 dest->ani2 = src->ani2;
2158 void ast_party_caller_set_init(struct ast_party_caller *init, const struct ast_party_caller *guide)
2160 ast_party_id_set_init(&init->id, &guide->id);
2161 ast_party_id_set_init(&init->ani, &guide->ani);
2162 ast_party_id_set_init(&init->priv, &guide->priv);
2163 init->ani2 = guide->ani2;
2166 void ast_party_caller_set(struct ast_party_caller *dest, const struct ast_party_caller *src, const struct ast_set_party_caller *update)
2168 ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
2169 ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
2170 ast_party_id_set(&dest->priv, &src->priv, update ? &update->priv : NULL);
2171 dest->ani2 = src->ani2;
2174 void ast_party_caller_free(struct ast_party_caller *doomed)
2176 ast_party_id_free(&doomed->id);
2177 ast_party_id_free(&doomed->ani);
2178 ast_party_id_free(&doomed->priv);
2181 void ast_party_connected_line_init(struct ast_party_connected_line *init)
2183 ast_party_id_init(&init->id);
2184 ast_party_id_init(&init->ani);
2185 ast_party_id_init(&init->priv);
2187 init->source = AST_CONNECTED_LINE_UPDATE_SOURCE_UNKNOWN;
2190 void ast_party_connected_line_copy(struct ast_party_connected_line *dest, const struct ast_party_connected_line *src)
2193 /* Don't copy to self */
2197 ast_party_id_copy(&dest->id, &src->id);
2198 ast_party_id_copy(&dest->ani, &src->ani);
2199 ast_party_id_copy(&dest->priv, &src->priv);
2200 dest->ani2 = src->ani2;
2201 dest->source = src->source;
2204 void ast_party_connected_line_set_init(struct ast_party_connected_line *init, const struct ast_party_connected_line *guide)
2206 ast_party_id_set_init(&init->id, &guide->id);
2207 ast_party_id_set_init(&init->ani, &guide->ani);
2208 ast_party_id_set_init(&init->priv, &guide->priv);
2209 init->ani2 = guide->ani2;
2210 init->source = guide->source;
2213 void ast_party_connected_line_set(struct ast_party_connected_line *dest, const struct ast_party_connected_line *src, const struct ast_set_party_connected_line *update)
2215 ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
2216 ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
2217 ast_party_id_set(&dest->priv, &src->priv, update ? &update->priv : NULL);
2218 dest->ani2 = src->ani2;
2219 dest->source = src->source;
2222 void ast_party_connected_line_collect_caller(struct ast_party_connected_line *connected, struct ast_party_caller *caller)
2224 connected->id = caller->id;
2225 connected->ani = caller->ani;
2226 connected->priv = caller->priv;
2227 connected->ani2 = caller->ani2;
2228 connected->source = AST_CONNECTED_LINE_UPDATE_SOURCE_UNKNOWN;
2231 void ast_party_connected_line_free(struct ast_party_connected_line *doomed)
2233 ast_party_id_free(&doomed->id);
2234 ast_party_id_free(&doomed->ani);
2235 ast_party_id_free(&doomed->priv);
2238 void ast_party_redirecting_reason_init(struct ast_party_redirecting_reason *init)
2241 init->code = AST_REDIRECTING_REASON_UNKNOWN;
2244 void ast_party_redirecting_reason_copy(struct ast_party_redirecting_reason *dest, const struct ast_party_redirecting_reason *src)
2250 ast_free(dest->str);
2251 dest->str = ast_strdup(src->str);
2252 dest->code = src->code;
2255 void ast_party_redirecting_reason_set_init(struct ast_party_redirecting_reason *init, const struct ast_party_redirecting_reason *guide)
2258 init->code = guide->code;
2261 void ast_party_redirecting_reason_set(struct ast_party_redirecting_reason *dest, const struct ast_party_redirecting_reason *src)
2267 if (src->str && src->str != dest->str) {
2268 ast_free(dest->str);
2269 dest->str = ast_strdup(src->str);
2272 dest->code = src->code;
2275 void ast_party_redirecting_reason_free(struct ast_party_redirecting_reason *doomed)
2277 ast_free(doomed->str);
2281 void ast_party_redirecting_init(struct ast_party_redirecting *init)
2283 ast_party_id_init(&init->orig);
2284 ast_party_id_init(&init->from);
2285 ast_party_id_init(&init->to);
2286 ast_party_id_init(&init->priv_orig);
2287 ast_party_id_init(&init->priv_from);
2288 ast_party_id_init(&init->priv_to);
2289 ast_party_redirecting_reason_init(&init->reason);
2290 ast_party_redirecting_reason_init(&init->orig_reason);
2294 void ast_party_redirecting_copy(struct ast_party_redirecting *dest, const struct ast_party_redirecting *src)
2297 /* Don't copy to self */
2301 ast_party_id_copy(&dest->orig, &src->orig);
2302 ast_party_id_copy(&dest->from, &src->from);
2303 ast_party_id_copy(&dest->to, &src->to);
2304 ast_party_id_copy(&dest->priv_orig, &src->priv_orig);
2305 ast_party_id_copy(&dest->priv_from, &src->priv_from);
2306 ast_party_id_copy(&dest->priv_to, &src->priv_to);
2307 ast_party_redirecting_reason_copy(&dest->reason, &src->reason);
2308 ast_party_redirecting_reason_copy(&dest->orig_reason, &src->orig_reason);
2309 dest->count = src->count;
2312 void ast_party_redirecting_set_init(struct ast_party_redirecting *init, const struct ast_party_redirecting *guide)
2314 ast_party_id_set_init(&init->orig, &guide->orig);
2315 ast_party_id_set_init(&init->from, &guide->from);
2316 ast_party_id_set_init(&init->to, &guide->to);
2317 ast_party_id_set_init(&init->priv_orig, &guide->priv_orig);
2318 ast_party_id_set_init(&init->priv_from, &guide->priv_from);
2319 ast_party_id_set_init(&init->priv_to, &guide->priv_to);
2320 ast_party_redirecting_reason_set_init(&init->reason, &guide->reason);
2321 ast_party_redirecting_reason_set_init(&init->orig_reason, &guide->orig_reason);
2322 init->count = guide->count;
2325 void ast_party_redirecting_set(struct ast_party_redirecting *dest, const struct ast_party_redirecting *src, const struct ast_set_party_redirecting *update)
2327 ast_party_id_set(&dest->orig, &src->orig, update ? &update->orig : NULL);
2328 ast_party_id_set(&dest->from, &src->from, update ? &update->from : NULL);
2329 ast_party_id_set(&dest->to, &src->to, update ? &update->to : NULL);
2330 ast_party_id_set(&dest->priv_orig, &src->priv_orig, update ? &update->priv_orig : NULL);
2331 ast_party_id_set(&dest->priv_from, &src->priv_from, update ? &update->priv_from : NULL);
2332 ast_party_id_set(&dest->priv_to, &src->priv_to, update ? &update->priv_to : NULL);
2333 ast_party_redirecting_reason_set(&dest->reason, &src->reason);
2334 ast_party_redirecting_reason_set(&dest->orig_reason, &src->orig_reason);
2335 dest->count = src->count;
2338 void ast_party_redirecting_free(struct ast_party_redirecting *doomed)
2340 ast_party_id_free(&doomed->orig);
2341 ast_party_id_free(&doomed->from);
2342 ast_party_id_free(&doomed->to);
2343 ast_party_id_free(&doomed->priv_orig);
2344 ast_party_id_free(&doomed->priv_from);
2345 ast_party_id_free(&doomed->priv_to);
2346 ast_party_redirecting_reason_free(&doomed->reason);
2347 ast_party_redirecting_reason_free(&doomed->orig_reason);
2350 /*! \brief Free a channel structure */
2351 static void ast_channel_destructor(void *obj)
2353 struct ast_channel *chan = obj;
2357 struct ast_var_t *vardata;
2358 struct ast_frame *f;
2359 struct varshead *headp;
2360 struct ast_datastore *datastore;
2361 char device_name[AST_CHANNEL_NAME];
2362 struct ast_callid *callid;
2364 if (ast_channel_internal_is_finalized(chan)) {
2365 ast_cel_report_event(chan, AST_CEL_CHANNEL_END, NULL, NULL, NULL);
2366 ast_cel_check_retire_linkedid(chan);
2369 ast_pbx_hangup_handler_destroy(chan);
2371 ast_channel_lock(chan);
2373 /* Get rid of each of the data stores on the channel */
2374 while ((datastore = AST_LIST_REMOVE_HEAD(ast_channel_datastores(chan), entry)))
2375 /* Free the data store */
2376 ast_datastore_free(datastore);
2378 /* While the channel is locked, take the reference to its callid while we tear down the call. */
2379 callid = ast_channel_callid(chan);
2380 ast_channel_callid_cleanup(chan);
2382 ast_channel_unlock(chan);
2384 /* Lock and unlock the channel just to be sure nobody has it locked still
2385 due to a reference that was stored in a datastore. (i.e. app_chanspy) */
2386 ast_channel_lock(chan);
2387 ast_channel_unlock(chan);
2389 if (ast_channel_tech_pvt(chan)) {
2390 ast_log_callid(LOG_WARNING, callid, "Channel '%s' may not have been hung up properly\n", ast_channel_name(chan));
2391 ast_free(ast_channel_tech_pvt(chan));
2394 if (ast_channel_sched(chan)) {
2395 ast_sched_context_destroy(ast_channel_sched(chan));
2398 if (ast_channel_internal_is_finalized(chan)) {
2401 ast_copy_string(device_name, ast_channel_name(chan), sizeof(device_name));
2402 if ((dashptr = strrchr(device_name, '-'))) {
2406 device_name[0] = '\0';
2409 /* Stop monitoring */
2410 if (ast_channel_monitor(chan))
2411 ast_channel_monitor(chan)->stop( chan, 0 );
2413 /* If there is native format music-on-hold state, free it */
2414 if (ast_channel_music_state(chan))
2415 ast_moh_cleanup(chan);
2417 /* Free translators */
2418 if (ast_channel_readtrans(chan))
2419 ast_translator_free_path(ast_channel_readtrans(chan));
2420 if (ast_channel_writetrans(chan))
2421 ast_translator_free_path(ast_channel_writetrans(chan));
2422 if (ast_channel_pbx(chan))
2423 ast_log_callid(LOG_WARNING, callid, "PBX may not have been terminated properly on '%s'\n", ast_channel_name(chan));
2425 ast_party_dialed_free(ast_channel_dialed(chan));
2426 ast_party_caller_free(ast_channel_caller(chan));
2427 ast_party_connected_line_free(ast_channel_connected(chan));
2428 ast_party_redirecting_free(ast_channel_redirecting(chan));
2430 /* Close pipes if appropriate */
2431 ast_channel_internal_alertpipe_close(chan);
2432 if (ast_channel_timer(chan)) {
2433 ast_timer_close(ast_channel_timer(chan));
2434 ast_channel_timer_set(chan, NULL);
2437 for (i = 0; i < AST_MAX_FDS; i++) {
2438 if (ast_channel_internal_epfd_data(chan, i)) {
2439 ast_free(ast_channel_internal_epfd_data(chan, i));
2442 close(ast_channel_epfd(chan));
2444 while ((f = AST_LIST_REMOVE_HEAD(ast_channel_readq(chan), frame_list)))
2447 /* loop over the variables list, freeing all data and deleting list items */
2448 /* no need to lock the list, as the channel is already locked */
2449 headp = ast_channel_varshead(chan);
2450 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
2451 ast_var_delete(vardata);
2453 ast_app_group_discard(chan);
2455 /* Destroy the jitterbuffer */
2456 ast_jb_destroy(chan);
2458 if (ast_channel_cdr(chan)) {
2459 ast_cdr_discard(ast_channel_cdr(chan));
2460 ast_channel_cdr_set(chan, NULL);
2463 if (ast_channel_zone(chan)) {
2464 ast_channel_zone_set(chan, ast_tone_zone_unref(ast_channel_zone(chan)));
2467 ast_channel_internal_cleanup(chan);
2469 if (device_name[0]) {
2471 * We have a device name to notify of a new state.
2473 * Queue an unknown state, because, while we know that this particular
2474 * instance is dead, we don't know the state of all other possible
2477 ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, device_name);
2480 ast_channel_nativeformats_set(chan, ast_format_cap_destroy(ast_channel_nativeformats(chan)));
2482 ast_callid_unref(callid);
2485 ast_channel_named_callgroups_set(chan, NULL);
2486 ast_channel_named_pickupgroups_set(chan, NULL);
2488 ast_atomic_fetchadd_int(&chancount, -1);
2491 /*! \brief Free a dummy channel structure */
2492 static void ast_dummy_channel_destructor(void *obj)
2494 struct ast_channel *chan = obj;
2495 struct ast_datastore *datastore;
2496 struct ast_var_t *vardata;
2497 struct varshead *headp;
2499 ast_pbx_hangup_handler_destroy(chan);
2501 /* Get rid of each of the data stores on the channel */
2502 while ((datastore = AST_LIST_REMOVE_HEAD(ast_channel_datastores(chan), entry))) {
2503 /* Free the data store */
2504 ast_datastore_free(datastore);
2507 ast_party_dialed_free(ast_channel_dialed(chan));
2508 ast_party_caller_free(ast_channel_caller(chan));
2509 ast_party_connected_line_free(ast_channel_connected(chan));
2510 ast_party_redirecting_free(ast_channel_redirecting(chan));
2512 /* loop over the variables list, freeing all data and deleting list items */
2513 /* no need to lock the list, as the channel is already locked */
2514 headp = ast_channel_varshead(chan);
2515 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
2516 ast_var_delete(vardata);
2518 if (ast_channel_cdr(chan)) {
2519 ast_cdr_discard(ast_channel_cdr(chan));
2520 ast_channel_cdr_set(chan, NULL);
2523 ast_channel_internal_cleanup(chan);
2526 struct ast_datastore *ast_channel_datastore_alloc(const struct ast_datastore_info *info, const char *uid)
2528 return ast_datastore_alloc(info, uid);
2531 int ast_channel_datastore_free(struct ast_datastore *datastore)
2533 return ast_datastore_free(datastore);
2536 int ast_channel_datastore_inherit(struct ast_channel *from, struct ast_channel *to)
2538 struct ast_datastore *datastore = NULL, *datastore2;
2540 AST_LIST_TRAVERSE(ast_channel_datastores(from), datastore, entry) {
2541 if (datastore->inheritance > 0) {
2542 datastore2 = ast_datastore_alloc(datastore->info, datastore->uid);
2544 datastore2->data = datastore->info->duplicate ? datastore->info->duplicate(datastore->data) : NULL;
2545 datastore2->inheritance = datastore->inheritance == DATASTORE_INHERIT_FOREVER ? DATASTORE_INHERIT_FOREVER : datastore->inheritance - 1;
2546 AST_LIST_INSERT_TAIL(ast_channel_datastores(to), datastore2, entry);
2553 int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
2557 AST_LIST_INSERT_HEAD(ast_channel_datastores(chan), datastore, entry);
2562 int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
2564 return AST_LIST_REMOVE(ast_channel_datastores(chan), datastore, entry) ? 0 : -1;
2567 struct ast_datastore *ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
2569 struct ast_datastore *datastore = NULL;
2574 AST_LIST_TRAVERSE(ast_channel_datastores(chan), datastore, entry) {
2575 if (datastore->info != info) {
2580 /* matched by type only */
2584 if ((datastore->uid != NULL) && !strcasecmp(uid, datastore->uid)) {
2585 /* Matched by type AND uid */
2593 /*! Set the file descriptor on the channel */
2594 void ast_channel_set_fd(struct ast_channel *chan, int which, int fd)
2597 struct epoll_event ev;
2598 struct ast_epoll_data *aed = NULL;
2600 if (ast_channel_fd_isset(chan, which)) {
2601 epoll_ctl(ast_channel_epfd(chan), EPOLL_CTL_DEL, ast_channel_fd(chan, which), &ev);
2602 aed = ast_channel_internal_epfd_data(chan, which);
2605 /* If this new fd is valid, add it to the epoll */
2607 if (!aed && (!(aed = ast_calloc(1, sizeof(*aed)))))
2610 ast_channel_internal_epfd_data_set(chan, which, aed);
2614 ev.events = EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP;
2616 epoll_ctl(ast_channel_epfd(chan), EPOLL_CTL_ADD, fd, &ev);
2618 /* We don't have to keep around this epoll data structure now */
2620 ast_channel_epfd_data_set(chan, which, NULL);
2623 ast_channel_internal_fd_set(chan, which, fd);
2627 /*! Add a channel to an optimized waitfor */
2628 void ast_poll_channel_add(struct ast_channel *chan0, struct ast_channel *chan1)
2631 struct epoll_event ev;
2634 if (ast_channel_epfd(chan0) == -1)
2637 /* Iterate through the file descriptors on chan1, adding them to chan0 */
2638 for (i = 0; i < AST_MAX_FDS; i++) {
2639 if (!ast_channel_fd_isset(chan1, i)) {
2642 ev.events = EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP;
2643 ev.data.ptr = ast_channel_internal_epfd_data(chan1, i);
2644 epoll_ctl(ast_channel_epfd(chan0), EPOLL_CTL_ADD, ast_channel_fd(chan1, i), &ev);
2651 /*! Delete a channel from an optimized waitfor */
2652 void ast_poll_channel_del(struct ast_channel *chan0, struct ast_channel *chan1)
2655 struct epoll_event ev;
2658 if (ast_channel_epfd(chan0) == -1)
2661 for (i = 0; i < AST_MAX_FDS; i++) {
2662 if (!ast_channel_fd_isset(chan1, i)) {
2665 epoll_ctl(ast_channel_epfd(chan0), EPOLL_CTL_DEL, ast_channel_fd(chan1, i), &ev);
2672 void ast_channel_clear_softhangup(struct ast_channel *chan, int flag)
2674 ast_channel_lock(chan);
2676 ast_channel_softhangup_internal_flag_clear(chan, flag);
2678 if (!ast_channel_softhangup_internal_flag(chan)) {
2679 struct ast_frame *fr;
2681 /* If we have completely cleared the softhangup flag,
2682 * then we need to fully abort the hangup process. This requires
2683 * pulling the END_OF_Q frame out of the channel frame queue if it
2684 * still happens to be there. */
2686 fr = AST_LIST_LAST(ast_channel_readq(chan));
2687 if (fr && fr->frametype == AST_FRAME_CONTROL &&
2688 fr->subclass.integer == AST_CONTROL_END_OF_Q) {
2689 AST_LIST_REMOVE(ast_channel_readq(chan), fr, frame_list);
2694 ast_channel_unlock(chan);
2697 /*! \brief Softly hangup a channel, don't lock */
2698 int ast_softhangup_nolock(struct ast_channel *chan, int cause)
2700 ast_debug(1, "Soft-Hanging up channel '%s'\n", ast_channel_name(chan));
2701 /* Inform channel driver that we need to be hung up, if it cares */
2702 ast_channel_softhangup_internal_flag_add(chan, cause);
2703 ast_queue_frame(chan, &ast_null_frame);
2704 /* Interrupt any poll call or such */
2705 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING))
2706 pthread_kill(ast_channel_blocker(chan), SIGURG);
2710 /*! \brief Softly hangup a channel, lock */
2711 int ast_softhangup(struct ast_channel *chan, int cause)
2715 ast_channel_lock(chan);
2716 res = ast_softhangup_nolock(chan, cause);
2718 <managerEventInstance>
2719 <synopsis>Raised when a soft hangup is requested with a specific cause code.</synopsis>
2721 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Hangup']/managerEventInstance/syntax/parameter[@name='Cause'])" />
2723 </managerEventInstance>
2725 manager_event(EVENT_FLAG_CALL, "SoftHangupRequest",
2729 ast_channel_name(chan),
2730 ast_channel_uniqueid(chan),
2732 ast_channel_unlock(chan);
2737 static void free_translation(struct ast_channel *clonechan)
2739 if (ast_channel_writetrans(clonechan))
2740 ast_translator_free_path(ast_channel_writetrans(clonechan));
2741 if (ast_channel_readtrans(clonechan))
2742 ast_translator_free_path(ast_channel_readtrans(clonechan));
2743 ast_channel_writetrans_set(clonechan, NULL);
2744 ast_channel_readtrans_set(clonechan, NULL);
2745 if (ast_format_cap_is_empty(ast_channel_nativeformats(clonechan))) {
2746 ast_format_clear(ast_channel_rawwriteformat(clonechan));
2747 ast_format_clear(ast_channel_rawreadformat(clonechan));
2749 struct ast_format tmpfmt;
2750 ast_best_codec(ast_channel_nativeformats(clonechan), &tmpfmt);
2751 ast_format_copy(ast_channel_rawwriteformat(clonechan), &tmpfmt);
2752 ast_format_copy(ast_channel_rawreadformat(clonechan), &tmpfmt);
2756 void ast_set_hangupsource(struct ast_channel *chan, const char *source, int force)
2758 struct ast_channel *bridge;
2760 ast_channel_lock(chan);
2761 if (force || ast_strlen_zero(ast_channel_hangupsource(chan))) {
2762 ast_channel_hangupsource_set(chan, source);
2764 bridge = ast_bridged_channel(chan);
2766 ast_channel_ref(bridge);
2768 ast_channel_unlock(chan);
2771 ast_channel_lock(bridge);
2772 if (force || ast_strlen_zero(ast_channel_hangupsource(bridge))) {
2773 ast_channel_hangupsource_set(bridge, source);
2775 ast_channel_unlock(bridge);
2776 ast_channel_unref(bridge);
2780 static void destroy_hooks(struct ast_channel *chan)
2782 if (ast_channel_audiohooks(chan)) {
2783 ast_audiohook_detach_list(ast_channel_audiohooks(chan));
2784 ast_channel_audiohooks_set(chan, NULL);
2787 ast_framehook_list_destroy(chan);
2790 /*! \brief Hangup a channel */
2791 int ast_hangup(struct ast_channel *chan)
2793 char extra_str[64]; /* used for cel logging below */
2795 ast_autoservice_stop(chan);
2797 ast_channel_lock(chan);
2800 * Do the masquerade if someone is setup to masquerade into us.
2802 * NOTE: We must hold the channel lock after testing for a
2803 * pending masquerade and setting the channel as a zombie to
2804 * prevent __ast_channel_masquerade() from setting up a
2805 * masquerade with a dead channel.
2807 while (ast_channel_masq(chan)) {
2808 ast_channel_unlock(chan);
2809 ast_do_masquerade(chan);
2810 ast_channel_lock(chan);
2813 if (ast_channel_masqr(chan)) {
2815 * This channel is one which will be masqueraded into something.
2816 * Mark it as a zombie already so ast_do_masquerade() will know
2819 ast_set_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE);
2820 destroy_hooks(chan);
2821 ast_channel_unlock(chan);
2825 /* Mark as a zombie so a masquerade cannot be setup on this channel. */
2826 ast_set_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE);
2828 ast_channel_unlock(chan);
2831 * XXX if running the hangup handlers here causes problems
2832 * because the handlers take too long to execute, we could move
2833 * the meat of this function into another thread. A thread
2834 * where channels go to die.
2836 * If this is done, ast_autoservice_chan_hangup_peer() will no
2839 ast_pbx_hangup_handler_run(chan);
2840 ao2_unlink(channels, chan);
2841 ast_channel_lock(chan);
2843 destroy_hooks(chan);
2845 free_translation(chan);
2846 /* Close audio stream */
2847 if (ast_channel_stream(chan)) {
2848 ast_closestream(ast_channel_stream(chan));
2849 ast_channel_stream_set(chan, NULL);
2851 /* Close video stream */
2852 if (ast_channel_vstream(chan)) {
2853 ast_closestream(ast_channel_vstream(chan));
2854 ast_channel_vstream_set(chan, NULL);
2856 if (ast_channel_sched(chan)) {
2857 ast_sched_context_destroy(ast_channel_sched(chan));
2858 ast_channel_sched_set(chan, NULL);
2861 if (ast_channel_generatordata(chan)) { /* Clear any tone stuff remaining */
2862 if (ast_channel_generator(chan) && ast_channel_generator(chan)->release) {
2863 ast_channel_generator(chan)->release(chan, ast_channel_generatordata(chan));
2866 ast_channel_generatordata_set(chan, NULL);
2867 ast_channel_generator_set(chan, NULL);
2869 snprintf(extra_str, sizeof(extra_str), "%d,%s,%s", ast_channel_hangupcause(chan), ast_channel_hangupsource(chan), S_OR(pbx_builtin_getvar_helper(chan, "DIALSTATUS"), ""));
2870 ast_cel_report_event(chan, AST_CEL_HANGUP, NULL, extra_str, NULL);
2872 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING)) {
2873 ast_log(LOG_WARNING, "Hard hangup called by thread %ld on %s, while fd "
2874 "is blocked by thread %ld in procedure %s! Expect a failure\n",
2875 (long) pthread_self(), ast_channel_name(chan), (long)ast_channel_blocker(chan), ast_channel_blockproc(chan));
2876 ast_assert(ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING) == 0);
2879 ast_debug(1, "Hanging up channel '%s'\n", ast_channel_name(chan));
2880 if (ast_channel_tech(chan)->hangup) {
2881 ast_channel_tech(chan)->hangup(chan);
2884 ast_channel_unlock(chan);
2888 <managerEventInstance>
2889 <synopsis>Raised when a channel is hung up.</synopsis>
2891 <parameter name="Cause">
2892 <para>A numeric cause code for why the channel was hung up.</para>
2894 <parameter name="Cause-txt">
2895 <para>A description of why the channel was hung up.</para>
2898 </managerEventInstance>
2900 ast_manager_event(chan, EVENT_FLAG_CALL, "Hangup",
2903 "CallerIDNum: %s\r\n"
2904 "CallerIDName: %s\r\n"
2905 "ConnectedLineNum: %s\r\n"
2906 "ConnectedLineName: %s\r\n"
2907 "AccountCode: %s\r\n"
2909 "Cause-txt: %s\r\n",
2910 ast_channel_name(chan),
2911 ast_channel_uniqueid(chan),
2912 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, "<unknown>"),
2913 S_COR(ast_channel_caller(chan)->id.name.valid, ast_channel_caller(chan)->id.name.str, "<unknown>"),
2914 S_COR(ast_channel_connected(chan)->id.number.valid, ast_channel_connected(chan)->id.number.str, "<unknown>"),
2915 S_COR(ast_channel_connected(chan)->id.name.valid, ast_channel_connected(chan)->id.name.str, "<unknown>"),
2916 ast_channel_accountcode(chan),
2917 ast_channel_hangupcause(chan),
2918 ast_cause2str(ast_channel_hangupcause(chan))
2921 if (ast_channel_cdr(chan) && !ast_test_flag(ast_channel_cdr(chan), AST_CDR_FLAG_BRIDGED) &&
2922 !ast_test_flag(ast_channel_cdr(chan), AST_CDR_FLAG_POST_DISABLED) &&
2923 (ast_channel_cdr(chan)->disposition != AST_CDR_NULL || ast_test_flag(ast_channel_cdr(chan), AST_CDR_FLAG_DIALED))) {
2924 ast_channel_lock(chan);
2925 ast_cdr_end(ast_channel_cdr(chan));
2926 ast_cdr_detach(ast_channel_cdr(chan));
2927 ast_channel_cdr_set(chan, NULL);
2928 ast_channel_unlock(chan);
2931 ast_channel_unref(chan);
2936 int ast_raw_answer(struct ast_channel *chan, int cdr_answer)
2940 ast_channel_lock(chan);
2942 /* You can't answer an outbound call */
2943 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING)) {
2944 ast_channel_unlock(chan);
2948 /* Stop if we're a zombie or need a soft hangup */
2949 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
2950 ast_channel_unlock(chan);
2954 ast_channel_unlock(chan);
2956 switch (ast_channel_state(chan)) {
2957 case AST_STATE_RINGING:
2958 case AST_STATE_RING:
2959 ast_channel_lock(chan);
2960 if (ast_channel_tech(chan)->answer) {
2961 res = ast_channel_tech(chan)->answer(chan);
2963 ast_setstate(chan, AST_STATE_UP);
2965 ast_cdr_answer(ast_channel_cdr(chan));
2967 ast_cel_report_event(chan, AST_CEL_ANSWER, NULL, NULL, NULL);
2968 ast_channel_unlock(chan);
2971 ast_cel_report_event(chan, AST_CEL_ANSWER, NULL, NULL, NULL);
2972 /* Calling ast_cdr_answer when it it has previously been called
2973 * is essentially a no-op, so it is safe.
2976 ast_cdr_answer(ast_channel_cdr(chan));
2983 ast_indicate(chan, -1);
2988 int __ast_answer(struct ast_channel *chan, unsigned int delay, int cdr_answer)
2991 enum ast_channel_state old_state;
2993 old_state = ast_channel_state(chan);
2994 if ((res = ast_raw_answer(chan, cdr_answer))) {
2998 switch (old_state) {
2999 case AST_STATE_RINGING:
3000 case AST_STATE_RING:
3001 /* wait for media to start flowing, but don't wait any longer
3002 * than 'delay' or 500 milliseconds, whichever is longer
3005 AST_LIST_HEAD_NOLOCK(, ast_frame) frames;
3006 struct ast_frame *cur, *new;
3007 int ms = MAX(delay, 500);
3008 unsigned int done = 0;
3010 AST_LIST_HEAD_INIT_NOLOCK(&frames);
3013 ms = ast_waitfor(chan, ms);
3015 ast_log(LOG_WARNING, "Error condition occurred when polling channel %s for a voice frame: %s\n", ast_channel_name(chan), strerror(errno));
3020 ast_debug(2, "Didn't receive a media frame from %s within %d ms of answering. Continuing anyway\n", ast_channel_name(chan), MAX(delay, 500));
3023 cur = ast_read(chan);
3024 if (!cur || ((cur->frametype == AST_FRAME_CONTROL) &&
3025 (cur->subclass.integer == AST_CONTROL_HANGUP))) {
3030 ast_debug(2, "Hangup of channel %s detected in answer routine\n", ast_channel_name(chan));
3034 if ((new = ast_frisolate(cur)) != cur) {
3038 AST_LIST_INSERT_HEAD(&frames, new, frame_list);
3040 /* if a specific delay period was requested, continue
3041 * until that delay has passed. don't stop just because
3042 * incoming media has arrived.
3048 switch (new->frametype) {
3049 /* all of these frametypes qualify as 'media' */
3050 case AST_FRAME_VOICE:
3051 case AST_FRAME_VIDEO:
3052 case AST_FRAME_TEXT:
3053 case AST_FRAME_DTMF_BEGIN:
3054 case AST_FRAME_DTMF_END:
3055 case AST_FRAME_IMAGE:
3056 case AST_FRAME_HTML:
3057 case AST_FRAME_MODEM:
3060 case AST_FRAME_CONTROL:
3062 case AST_FRAME_NULL:
3073 ast_channel_lock(chan);
3074 while ((cur = AST_LIST_REMOVE_HEAD(&frames, frame_list))) {
3075 ast_queue_frame_head(chan, cur);
3078 ast_channel_unlock(chan);
3089 int ast_answer(struct ast_channel *chan)
3091 return __ast_answer(chan, 0, 1);
3094 void ast_deactivate_generator(struct ast_channel *chan)
3096 ast_channel_lock(chan);
3097 if (ast_channel_generatordata(chan)) {
3098 if (ast_channel_generator(chan) && ast_channel_generator(chan)->release) {
3099 ast_channel_generator(chan)->release(chan, ast_channel_generatordata(chan));
3101 ast_channel_generatordata_set(chan, NULL);
3102 ast_channel_generator_set(chan, NULL);
3103 ast_channel_set_fd(chan, AST_GENERATOR_FD, -1);
3104 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_WRITE_INT);
3105 ast_settimeout(chan, 0, NULL, NULL);
3107 ast_channel_unlock(chan);
3110 static void generator_write_format_change(struct ast_channel *chan)
3112 ast_channel_lock(chan);
3113 if (ast_channel_generator(chan) && ast_channel_generator(chan)->write_format_change) {
3114 ast_channel_generator(chan)->write_format_change(chan, ast_channel_generatordata(chan));
3116 ast_channel_unlock(chan);
3119 static int generator_force(const void *data)
3121 /* Called if generator doesn't have data */
3124 int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples) = NULL;
3125 struct ast_channel *chan = (struct ast_channel *)data;
3127 ast_channel_lock(chan);
3128 tmp = ast_channel_generatordata(chan);
3129 ast_channel_generatordata_set(chan, NULL);
3130 if (ast_channel_generator(chan))
3131 generate = ast_channel_generator(chan)->generate;
3132 ast_channel_unlock(chan);
3134 if (!tmp || !generate)
3137 res = generate(chan, tmp, 0, ast_format_rate(ast_channel_writeformat(chan)) / 50);
3139 ast_channel_generatordata_set(chan, tmp);
3142 ast_debug(1, "Auto-deactivating generator\n");
3143 ast_deactivate_generator(chan);
3149 int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen, void *params)
3152 void *generatordata = NULL;
3154 ast_channel_lock(chan);
3155 if (ast_channel_generatordata(chan)) {
3156 if (ast_channel_generator(chan) && ast_channel_generator(chan)->release) {
3157 ast_channel_generator(chan)->release(chan, ast_channel_generatordata(chan));
3160 if (gen->alloc && !(generatordata = gen->alloc(chan, params))) {
3163 ast_channel_generatordata_set(chan, generatordata);
3165 ast_settimeout(chan, 50, generator_force, chan);
3166 ast_channel_generator_set(chan, gen);
3168 ast_channel_unlock(chan);
3175 /*! \brief Wait for x amount of time on a file descriptor to have input. */
3176 int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception)
3179 ast_waitfor_nandfds(NULL, 0, fds, n, exception, &winner, ms);
3183 /*! \brief Wait for x amount of time on a file descriptor to have input. */
3185 static struct ast_channel *ast_waitfor_nandfds_classic(struct ast_channel **c, int n, int *fds, int nfds,
3186 int *exception, int *outfd, int *ms)
3188 struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
3189 int *exception, int *outfd, int *ms)
3192 struct timeval start = { 0 , 0 };
3193 struct pollfd *pfds = NULL;
3198 struct timeval now = { 0, 0 };
3199 struct timeval whentohangup = { 0, 0 }, diff;
3200 struct ast_channel *winner = NULL;
3213 if ((sz = n * AST_MAX_FDS + nfds)) {
3214 pfds = ast_alloca(sizeof(*pfds) * sz);
3215 fdmap = ast_alloca(sizeof(*fdmap) * sz);
3217 /* nothing to allocate and no FDs to check */
3221 /* Perform any pending masquerades */
3222 for (x = 0; x < n; x++) {
3223 while (ast_channel_masq(c[x])) {
3224 ast_do_masquerade(c[x]);
3227 ast_channel_lock(c[x]);
3228 if (!ast_tvzero(*ast_channel_whentohangup(c[x]))) {
3229 if (ast_tvzero(whentohangup))
3231 diff = ast_tvsub(*ast_channel_whentohangup(c[x]), now);
3232 if (diff.tv_sec < 0 || ast_tvzero(diff)) {
3233 /* Should already be hungup */
3234 ast_channel_softhangup_internal_flag_add(c[x], AST_SOFTHANGUP_TIMEOUT);
3235 ast_channel_unlock(c[x]);
3238 if (ast_tvzero(whentohangup) || ast_tvcmp(diff, whentohangup) < 0)
3239 whentohangup = diff;
3241 ast_channel_unlock(c[x]);
3243 /* Wait full interval */
3245 /* INT_MAX, not LONG_MAX, because it matters on 64-bit */
3246 if (!ast_tvzero(whentohangup) && whentohangup.tv_sec < INT_MAX / 1000) {
3247 rms = whentohangup.tv_sec * 1000 + whentohangup.tv_usec / 1000; /* timeout in milliseconds */
3248 if (*ms >= 0 && *ms < rms) { /* original *ms still smaller */
3251 } else if (!ast_tvzero(whentohangup) && rms < 0) {
3252 /* Tiny corner case... call would need to last >24 days */
3256 * Build the pollfd array, putting the channels' fds first,
3257 * followed by individual fds. Order is important because
3258 * individual fd's must have priority over channel fds.
3261 for (x = 0; x < n; x++) {
3262 for (y = 0; y < AST_MAX_FDS; y++) {
3263 fdmap[max].fdno = y; /* fd y is linked to this pfds */
3264 fdmap[max].chan = x; /* channel x is linked to this pfds */
3265 max += ast_add_fd(&pfds[max], ast_channel_fd(c[x], y));
3267 CHECK_BLOCKING(c[x]);
3269 /* Add the individual fds */
3270 for (x = 0; x < nfds; x++) {
3271 fdmap[max].chan = -1;
3272 max += ast_add_fd(&pfds[max], fds[x]);
3276 start = ast_tvnow();
3279 if (sizeof(int) == 4) { /* XXX fix timeout > 600000 on linux x86-32 */
3282 if (kbrms > 600000) {
3285 res = ast_poll(pfds, max, kbrms);
3289 } while (!res && (rms > 0));
3291 res = ast_poll(pfds, max, rms);
3293 for (x = 0; x < n; x++) {
3294 ast_clear_flag(ast_channel_flags(c[x]), AST_FLAG_BLOCKING);
3296 if (res < 0) { /* Simulate a timeout if we were interrupted */
3297 if (errno != EINTR) {
3302 if (!ast_tvzero(whentohangup)) { /* if we have a timeout, check who expired */
3304 for (x = 0; x < n; x++) {
3305 if (!ast_tvzero(*ast_channel_whentohangup(c[x])) && ast_tvcmp(*ast_channel_whentohangup(c[x]), now) <= 0) {
3306 ast_channel_softhangup_internal_flag_add(c[x], AST_SOFTHANGUP_TIMEOUT);
3307 if (winner == NULL) {
3313 if (res == 0) { /* no fd ready, reset timeout and done */
3314 *ms = 0; /* XXX use 0 since we may not have an exact timeout. */
3318 * Then check if any channel or fd has a pending event.
3319 * Remember to check channels first and fds last, as they
3320 * must have priority on setting 'winner'
3322 for (x = 0; x < max; x++) {
3323 res = pfds[x].revents;
3327 if (fdmap[x].chan >= 0) { /* this is a channel */
3328 winner = c[fdmap[x].chan]; /* override previous winners */
3329 if (res & POLLPRI) {
3330 ast_set_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3332 ast_clear_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3334 ast_channel_fdno_set(winner, fdmap[x].fdno);
3335 } else { /* this is an fd */
3337 *outfd = pfds[x].fd;
3340 *exception = (res & POLLPRI) ? -1 : 0;
3346 *ms -= ast_tvdiff_ms(ast_tvnow(), start);
3355 static struct ast_channel *ast_waitfor_nandfds_simple(struct ast_channel *chan, int *ms)
3357 struct timeval start = { 0 , 0 };
3359 struct epoll_event ev[1];
3360 long diff, rms = *ms;
3361 struct ast_channel *winner = NULL;
3362 struct ast_epoll_data *aed = NULL;
3365 /* See if this channel needs to be masqueraded */
3366 while (ast_channel_masq(chan)) {
3367 ast_do_masquerade(chan);
3370 ast_channel_lock(chan);
3371 /* Figure out their timeout */
3372 if (!ast_tvzero(*ast_channel_whentohangup(chan))) {
3373 if ((diff = ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow())) < 0) {
3374 /* They should already be hungup! */
3375 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_TIMEOUT);
3376 ast_channel_unlock(chan);
3379 /* If this value is smaller then the current one... make it priority */
3385 ast_channel_unlock(chan);
3387 /* Time to make this channel block... */
3388 CHECK_BLOCKING(chan);
3391 start = ast_tvnow();
3394 /* We don't have to add any file descriptors... they are already added, we just have to wait! */
3395 res = epoll_wait(ast_channel_epfd(chan), ev, 1, rms);
3398 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING);
3400 /* Simulate a timeout if we were interrupted */
3402 if (errno != EINTR) {
3408 /* If this channel has a timeout see if it expired */
3409 if (!ast_tvzero(*ast_channel_whentohangup(chan))) {
3410 if (ast_tvdiff_ms(ast_tvnow(), *ast_channel_whentohangup(chan)) >= 0) {
3411 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_TIMEOUT);
3416 /* No fd ready, reset timeout and be done for now */
3422 /* See what events are pending */
3423 aed = ev[0].data.ptr;
3424 ast_channel_fdno_set(chan, aed->which);
3425 if (ev[0].events & EPOLLPRI) {
3426 ast_set_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3428 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3432 *ms -= ast_tvdiff_ms(ast_tvnow(), start);
3441 static struct ast_channel *ast_waitfor_nandfds_complex(struct ast_channel **c, int n, int *ms)
3443 struct timeval start = { 0 , 0 };
3445 struct epoll_event ev[25] = { { 0, } };
3446 struct timeval now = { 0, 0 };
3447 long whentohangup = 0, diff = 0, rms = *ms;
3448 struct ast_channel *winner = NULL;
3450 for (i = 0; i < n; i++) {
3451 while (ast_channel_masq(c[i])) {
3452 ast_do_masquerade(c[i]);
3455 ast_channel_lock(c[i]);
3456 if (!ast_tvzero(*ast_channel_whentohangup(c[i]))) {
3457 if (whentohangup == 0) {
3460 if ((diff = ast_tvdiff_ms(*ast_channel_whentohangup(c[i]), now)) < 0) {
3461 ast_channel_softhangup_internal_flag_add(c[i], AST_SOFTHANGUP_TIMEOUT);
3462 ast_channel_unlock(c[i]);
3465 if (!whentohangup || whentohangup > diff) {
3466 whentohangup = diff;
3469 ast_channel_unlock(c[i]);
3470 CHECK_BLOCKING(c[i]);
3476 if (*ms >= 0 && *ms < rms) {
3482 start = ast_tvnow();
3485 res = epoll_wait(ast_channel_epfd(c[0]), ev, 25, rms);
3487 for (i = 0; i < n; i++) {
3488 ast_clear_flag(ast_channel_flags(c[i]), AST_FLAG_BLOCKING);
3492 if (errno != EINTR) {
3500 for (i = 0; i < n; i++) {
3501 if (!ast_tvzero(*ast_channel_whentohangup(c[i])) && ast_tvdiff_ms(now, *ast_channel_whentohangup(c[i])) >= 0) {
3502 ast_channel_softhangup_internal_flag_add(c[i], AST_SOFTHANGUP_TIMEOUT);
3515 for (i = 0; i < res; i++) {
3516 struct ast_epoll_data *aed = ev[i].data.ptr;
3518 if (!ev[i].events || !aed) {
3523 if (ev[i].events & EPOLLPRI) {
3524 ast_set_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3526 ast_clear_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3528 ast_channel_fdno_set(winner, aed->which);
3532 *ms -= ast_tvdiff_ms(ast_tvnow(), start);
3541 struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
3542 int *exception, int *outfd, int *ms)
3544 /* Clear all provided values in one place. */
3552 /* If no epoll file descriptor is available resort to classic nandfds */
3553 if (!n || nfds || ast_channel_epfd(c[0]) == -1) {
3554 return ast_waitfor_nandfds_classic(c, n, fds, nfds, exception, outfd, ms);
3555 } else if (!nfds && n == 1) {
3556 return ast_waitfor_nandfds_simple(c[0], ms);
3558 return ast_waitfor_nandfds_complex(c, n, ms);
3563 struct ast_channel *ast_waitfor_n(struct ast_channel **c, int n, int *ms)
3565 return ast_waitfor_nandfds(c, n, NULL, 0, NULL, NULL, ms);
3568 int ast_waitfor(struct ast_channel *c, int ms)
3570 int oldms = ms; /* -1 if no timeout */
3572 ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
3573 if ((ms < 0) && (oldms < 0)) {
3579 int ast_waitfordigit(struct ast_channel *c, int ms)
3581 return ast_waitfordigit_full(c, ms, -1, -1);
3584 int ast_settimeout(struct ast_channel *c, unsigned int rate, int (*func)(const void *data), void *data)
3587 unsigned int real_rate = rate, max_rate;
3589 ast_channel_lock(c);
3591 if (ast_channel_timingfd(c) == -1) {
3592 ast_channel_unlock(c);
3601 if (rate && rate > (max_rate = ast_timer_get_max_rate(ast_channel_timer(c)))) {
3602 real_rate = max_rate;
3605 ast_debug(1, "Scheduling timer at (%u requested / %u actual) timer ticks per second\n", rate, real_rate);
3607 res = ast_timer_set_rate(ast_channel_timer(c), real_rate);
3609 ast_channel_timingfunc_set(c, func);
3610 ast_channel_timingdata_set(c, data);
3612 if (func == NULL && rate == 0 && ast_channel_fdno(c) == AST_TIMING_FD) {
3613 /* Clearing the timing func and setting the rate to 0
3614 * means that we don't want to be reading from the timingfd
3615 * any more. Setting c->fdno to -1 means we won't have any
3616 * errant reads from the timingfd, meaning we won't potentially
3617 * miss any important frames.
3619 ast_channel_fdno_set(c, -1);
3622 ast_channel_unlock(c);
3627 int ast_waitfordigit_full(struct ast_channel *c, int timeout_ms, int audiofd, int cmdfd)
3629 struct timeval start = ast_tvnow();
3631 /* Stop if we're a zombie or need a soft hangup */
3632 if (ast_test_flag(ast_channel_flags(c), AST_FLAG_ZOMBIE) || ast_check_hangup(c))
3635 /* Only look for the end of DTMF, don't bother with the beginning and don't emulate things */
3636 ast_set_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3638 /* Wait for a digit, no more than timeout_ms milliseconds total.
3639 * Or, wait indefinitely if timeout_ms is <0.
3641 while (ast_tvdiff_ms(ast_tvnow(), start) < timeout_ms || timeout_ms < 0) {
3642 struct ast_channel *rchan;
3646 if (timeout_ms < 0) {
3649 ms = timeout_ms - ast_tvdiff_ms(ast_tvnow(), start);
3656 /* While ast_waitfor_nandfds tries to help by reducing the timeout by how much was waited,
3657 * it is unhelpful if it waited less than a millisecond.
3659 rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
3661 if (!rchan && outfd < 0 && ms) {
3662 if (errno == 0 || errno == EINTR)
3664 ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
3665 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3667 } else if (outfd > -1) {
3668 /* The FD we were watching has something waiting */
3669 ast_log(LOG_WARNING, "The FD we were waiting for has something waiting. Waitfordigit returning numeric 1\n");
3670 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3674 struct ast_frame *f = ast_read(c);
3678 switch (f->frametype) {
3679 case AST_FRAME_DTMF_BEGIN:
3681 case AST_FRAME_DTMF_END:
3682 res = f->subclass.integer;
3684 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3686 case AST_FRAME_CONTROL:
3687 switch (f->subclass.integer) {
3688 case AST_CONTROL_HANGUP:
3690 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3692 case AST_CONTROL_PVT_CAUSE_CODE:
3693 case AST_CONTROL_RINGING:
3694 case AST_CONTROL_ANSWER:
3695 case AST_CONTROL_SRCUPDATE:
3696 case AST_CONTROL_SRCCHANGE:
3697 case AST_CONTROL_CONNECTED_LINE:
3698 case AST_CONTROL_REDIRECTING:
3699 case AST_CONTROL_UPDATE_RTP_PEER:
3700 case AST_CONTROL_HOLD:
3701 case AST_CONTROL_UNHOLD:
3706 ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", f->subclass.integer);
3710 case AST_FRAME_VOICE:
3711 /* Write audio if appropriate */
3713 if (write(audiofd, f->data.ptr, f->datalen) < 0) {
3714 ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
3725 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3727 return 0; /* Time is up */
3730 static void send_dtmf_event(struct ast_channel *chan, const char *direction, const char digit, const char *begin, const char *end)
3733 <managerEventInstance>
3734 <synopsis>Raised when a DTMF digit has started or ended on a channel.</synopsis>
3736 <parameter name="Direction">
3738 <enum name="Received"/>
3742 <parameter name="Begin">
3748 <parameter name="End">
3755 </managerEventInstance>
3757 ast_manager_event(chan, EVENT_FLAG_DTMF,
3765 ast_channel_name(chan), ast_channel_uniqueid(chan), digit, direction, begin, end);
3768 static void ast_read_generator_actions(struct ast_channel *chan, struct ast_frame *f)
3770 if (ast_channel_generator(chan) && ast_channel_generator(chan)->generate && ast_channel_generatordata(chan) && !ast_internal_timing_enabled(chan)) {
3771 void *tmp = ast_channel_generatordata(chan);
3772 int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples) = ast_channel_generator(chan)->generate;
3776 if (ast_channel_timingfunc(chan)) {
3777 ast_debug(1, "Generator got voice, switching to phase locked mode\n");
3778 ast_settimeout(chan, 0, NULL, NULL);
3781 ast_channel_generatordata_set(chan, NULL); /* reset, to let writes go through */
3783 if (ast_format_cmp(&f->subclass.format, ast_channel_writeformat(chan)) == AST_FORMAT_CMP_NOT_EQUAL) {
3785 factor = ((float) ast_format_rate(ast_channel_writeformat(chan))) / ((float) ast_format_rate(&f->subclass.format));
3786 samples = (int) ( ((float) f->samples) * factor );
3788 samples = f->samples;
3791 /* This unlock is here based on two assumptions that hold true at this point in the
3792 * code. 1) this function is only called from within __ast_read() and 2) all generators
3793 * call ast_write() in their generate callback.
3795 * The reason this is added is so that when ast_write is called, the lock that occurs
3796 * there will not recursively lock the channel. Doing this will cause intended deadlock
3797 * avoidance not to work in deeper functions
3799 ast_channel_unlock(chan);
3800 res = generate(chan, tmp, f->datalen, samples);
3801 ast_channel_lock(chan);
3802 ast_channel_generatordata_set(chan, tmp);
3804 ast_debug(1, "Auto-deactivating generator\n");
3805 ast_deactivate_generator(chan);
3808 } else if (f->frametype == AST_FRAME_CNG) {
3809 if (ast_channel_generator(chan) && !ast_channel_timingfunc(chan) && (ast_channel_timingfd(chan) > -1)) {
3810 ast_debug(1, "Generator got CNG, switching to timed mode\n");
3811 ast_settimeout(chan, 50, generator_force, chan);
3816 static inline void queue_dtmf_readq(struct ast_channel *chan, struct ast_frame *f)
3818 struct ast_frame *fr = ast_channel_dtmff(chan);
3820 fr->frametype = AST_FRAME_DTMF_END;
3821 fr->subclass.integer = f->subclass.integer;
3824 /* The only time this function will be called is for a frame that just came
3825 * out of the channel driver. So, we want to stick it on the tail of the
3828 ast_queue_frame(chan, fr);
3832 * \brief Determine whether or not we should ignore DTMF in the readq
3834 static inline int should_skip_dtmf(struct ast_channel *chan)
3836 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF | AST_FLAG_EMULATE_DTMF)) {
3837 /* We're in the middle of emulating a digit, or DTMF has been
3838 * explicitly deferred. Skip this digit, then. */
3842 if (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
3843 ast_tvdiff_ms(ast_tvnow(), *ast_channel_dtmf_tv(chan)) < AST_MIN_DTMF_GAP) {
3844 /* We're not in the middle of a digit, but it hasn't been long enough
3845 * since the last digit, so we'll have to skip DTMF for now. */
3853 * \brief calculates the number of samples to jump forward with in a monitor stream.
3855 * \note When using ast_seekstream() with the read and write streams of a monitor,
3856 * the number of samples to seek forward must be of the same sample rate as the stream