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 #include "asterisk/_private.h"
38 #include "asterisk/paths.h" /* use ast_config_AST_SYSTEM_NAME */
40 #include "asterisk/pbx.h"
41 #include "asterisk/frame.h"
42 #include "asterisk/mod_format.h"
43 #include "asterisk/sched.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/musiconhold.h"
46 #include "asterisk/say.h"
47 #include "asterisk/file.h"
48 #include "asterisk/cli.h"
49 #include "asterisk/translate.h"
50 #include "asterisk/manager.h"
51 #include "asterisk/chanvars.h"
52 #include "asterisk/linkedlists.h"
53 #include "asterisk/indications.h"
54 #include "asterisk/causes.h"
55 #include "asterisk/callerid.h"
56 #include "asterisk/utils.h"
57 #include "asterisk/lock.h"
58 #include "asterisk/app.h"
59 #include "asterisk/transcap.h"
60 #include "asterisk/devicestate.h"
61 #include "asterisk/threadstorage.h"
62 #include "asterisk/slinfactory.h"
63 #include "asterisk/audiohook.h"
64 #include "asterisk/framehook.h"
65 #include "asterisk/timing.h"
66 #include "asterisk/autochan.h"
67 #include "asterisk/stringfields.h"
68 #include "asterisk/global_datastores.h"
69 #include "asterisk/data.h"
70 #include "asterisk/channel_internal.h"
71 #include "asterisk/features.h"
72 #include "asterisk/bridge.h"
73 #include "asterisk/test.h"
74 #include "asterisk/stasis_channels.h"
75 #include "asterisk/max_forwards.h"
81 #include <sys/epoll.h>
84 #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
87 #endif /* defined(HAVE_PRI) */
88 #endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
90 struct ast_epoll_data {
91 struct ast_channel *chan;
95 /* uncomment if you have problems with 'monitoring' synchronized files */
97 #define MONITOR_CONSTANT_DELAY
98 #define MONITOR_DELAY 150 * 8 /*!< 150 ms of MONITORING DELAY */
101 static int chancount;
103 unsigned long global_fin, global_fout;
105 AST_THREADSTORAGE(state2str_threadbuf);
106 #define STATE2STR_BUFSIZE 32
108 /*! Default amount of time to use when emulating a digit as a begin and end
110 #define AST_DEFAULT_EMULATE_DTMF_DURATION 100
112 #define DEFAULT_AMA_FLAGS AST_AMA_DOCUMENTATION
114 /*! Minimum amount of time between the end of the last digit and the beginning
115 * of a new one - 45ms */
116 #define AST_MIN_DTMF_GAP 45
118 /*! \brief List of channel drivers */
120 const struct ast_channel_tech *tech;
121 AST_LIST_ENTRY(chanlist) list;
124 /*! \brief the list of registered channel types */
125 static AST_RWLIST_HEAD_STATIC(backends, chanlist);
128 #define NUM_CHANNEL_BUCKETS 61
130 #define NUM_CHANNEL_BUCKETS 1567
133 /*! \brief All active channels on the system */
134 static struct ao2_container *channels;
136 /*! \brief map AST_CAUSE's to readable string representations
146 static const struct causes_map causes[] = {
147 { AST_CAUSE_UNALLOCATED, "UNALLOCATED", "Unallocated (unassigned) number" },
148 { AST_CAUSE_NO_ROUTE_TRANSIT_NET, "NO_ROUTE_TRANSIT_NET", "No route to specified transmit network" },
149 { AST_CAUSE_NO_ROUTE_DESTINATION, "NO_ROUTE_DESTINATION", "No route to destination" },
150 { AST_CAUSE_MISDIALLED_TRUNK_PREFIX, "MISDIALLED_TRUNK_PREFIX", "Misdialed trunk prefix" },
151 { AST_CAUSE_CHANNEL_UNACCEPTABLE, "CHANNEL_UNACCEPTABLE", "Channel unacceptable" },
152 { AST_CAUSE_CALL_AWARDED_DELIVERED, "CALL_AWARDED_DELIVERED", "Call awarded and being delivered in an established channel" },
153 { AST_CAUSE_PRE_EMPTED, "PRE_EMPTED", "Pre-empted" },
154 { AST_CAUSE_NUMBER_PORTED_NOT_HERE, "NUMBER_PORTED_NOT_HERE", "Number ported elsewhere" },
155 { AST_CAUSE_NORMAL_CLEARING, "NORMAL_CLEARING", "Normal Clearing" },
156 { AST_CAUSE_USER_BUSY, "USER_BUSY", "User busy" },
157 { AST_CAUSE_NO_USER_RESPONSE, "NO_USER_RESPONSE", "No user responding" },
158 { AST_CAUSE_NO_ANSWER, "NO_ANSWER", "User alerting, no answer" },
159 { AST_CAUSE_SUBSCRIBER_ABSENT, "SUBSCRIBER_ABSENT", "Subscriber absent" },
160 { AST_CAUSE_CALL_REJECTED, "CALL_REJECTED", "Call Rejected" },
161 { AST_CAUSE_NUMBER_CHANGED, "NUMBER_CHANGED", "Number changed" },
162 { AST_CAUSE_REDIRECTED_TO_NEW_DESTINATION, "REDIRECTED_TO_NEW_DESTINATION", "Redirected to new destination" },
163 { AST_CAUSE_ANSWERED_ELSEWHERE, "ANSWERED_ELSEWHERE", "Answered elsewhere" },
164 { AST_CAUSE_DESTINATION_OUT_OF_ORDER, "DESTINATION_OUT_OF_ORDER", "Destination out of order" },
165 { AST_CAUSE_INVALID_NUMBER_FORMAT, "INVALID_NUMBER_FORMAT", "Invalid number format" },
166 { AST_CAUSE_FACILITY_REJECTED, "FACILITY_REJECTED", "Facility rejected" },
167 { AST_CAUSE_RESPONSE_TO_STATUS_ENQUIRY, "RESPONSE_TO_STATUS_ENQUIRY", "Response to STATus ENQuiry" },
168 { AST_CAUSE_NORMAL_UNSPECIFIED, "NORMAL_UNSPECIFIED", "Normal, unspecified" },
169 { AST_CAUSE_NORMAL_CIRCUIT_CONGESTION, "NORMAL_CIRCUIT_CONGESTION", "Circuit/channel congestion" },
170 { AST_CAUSE_NETWORK_OUT_OF_ORDER, "NETWORK_OUT_OF_ORDER", "Network out of order" },
171 { AST_CAUSE_NORMAL_TEMPORARY_FAILURE, "NORMAL_TEMPORARY_FAILURE", "Temporary failure" },
172 { AST_CAUSE_SWITCH_CONGESTION, "SWITCH_CONGESTION", "Switching equipment congestion" },
173 { AST_CAUSE_ACCESS_INFO_DISCARDED, "ACCESS_INFO_DISCARDED", "Access information discarded" },
174 { AST_CAUSE_REQUESTED_CHAN_UNAVAIL, "REQUESTED_CHAN_UNAVAIL", "Requested channel not available" },
175 { AST_CAUSE_FACILITY_NOT_SUBSCRIBED, "FACILITY_NOT_SUBSCRIBED", "Facility not subscribed" },
176 { AST_CAUSE_OUTGOING_CALL_BARRED, "OUTGOING_CALL_BARRED", "Outgoing call barred" },
177 { AST_CAUSE_INCOMING_CALL_BARRED, "INCOMING_CALL_BARRED", "Incoming call barred" },
178 { AST_CAUSE_BEARERCAPABILITY_NOTAUTH, "BEARERCAPABILITY_NOTAUTH", "Bearer capability not authorized" },
179 { AST_CAUSE_BEARERCAPABILITY_NOTAVAIL, "BEARERCAPABILITY_NOTAVAIL", "Bearer capability not available" },
180 { AST_CAUSE_BEARERCAPABILITY_NOTIMPL, "BEARERCAPABILITY_NOTIMPL", "Bearer capability not implemented" },
181 { AST_CAUSE_CHAN_NOT_IMPLEMENTED, "CHAN_NOT_IMPLEMENTED", "Channel not implemented" },
182 { AST_CAUSE_FACILITY_NOT_IMPLEMENTED, "FACILITY_NOT_IMPLEMENTED", "Facility not implemented" },
183 { AST_CAUSE_INVALID_CALL_REFERENCE, "INVALID_CALL_REFERENCE", "Invalid call reference value" },
184 { AST_CAUSE_INCOMPATIBLE_DESTINATION, "INCOMPATIBLE_DESTINATION", "Incompatible destination" },
185 { AST_CAUSE_INVALID_MSG_UNSPECIFIED, "INVALID_MSG_UNSPECIFIED", "Invalid message unspecified" },
186 { AST_CAUSE_MANDATORY_IE_MISSING, "MANDATORY_IE_MISSING", "Mandatory information element is missing" },
187 { AST_CAUSE_MESSAGE_TYPE_NONEXIST, "MESSAGE_TYPE_NONEXIST", "Message type nonexist." },
188 { AST_CAUSE_WRONG_MESSAGE, "WRONG_MESSAGE", "Wrong message" },
189 { AST_CAUSE_IE_NONEXIST, "IE_NONEXIST", "Info. element nonexist or not implemented" },
190 { AST_CAUSE_INVALID_IE_CONTENTS, "INVALID_IE_CONTENTS", "Invalid information element contents" },
191 { AST_CAUSE_WRONG_CALL_STATE, "WRONG_CALL_STATE", "Message not compatible with call state" },
192 { AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE, "RECOVERY_ON_TIMER_EXPIRE", "Recover on timer expiry" },
193 { AST_CAUSE_MANDATORY_IE_LENGTH_ERROR, "MANDATORY_IE_LENGTH_ERROR", "Mandatory IE length error" },
194 { AST_CAUSE_PROTOCOL_ERROR, "PROTOCOL_ERROR", "Protocol error, unspecified" },
195 { AST_CAUSE_INTERWORKING, "INTERWORKING", "Interworking, unspecified" },
198 struct ast_variable *ast_channeltype_list(void)
201 struct ast_variable *var = NULL, *prev = NULL;
203 AST_RWLIST_RDLOCK(&backends);
204 AST_RWLIST_TRAVERSE(&backends, cl, list) {
206 if ((prev->next = ast_variable_new(cl->tech->type, cl->tech->description, "")))
209 var = ast_variable_new(cl->tech->type, cl->tech->description, "");
213 AST_RWLIST_UNLOCK(&backends);
218 #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
219 static const char *party_number_ton2str(int ton)
221 #if defined(HAVE_PRI)
222 switch ((ton >> 4) & 0x07) {
223 case PRI_TON_INTERNATIONAL:
224 return "International";
225 case PRI_TON_NATIONAL:
227 case PRI_TON_NET_SPECIFIC:
228 return "Network Specific";
229 case PRI_TON_SUBSCRIBER:
231 case PRI_TON_ABBREVIATED:
232 return "Abbreviated";
233 case PRI_TON_RESERVED:
235 case PRI_TON_UNKNOWN:
239 #endif /* defined(HAVE_PRI) */
242 #endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
244 #if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
245 static const char *party_number_plan2str(int plan)
247 #if defined(HAVE_PRI)
248 switch (plan & 0x0F) {
250 case PRI_NPI_UNKNOWN:
252 case PRI_NPI_E163_E164:
253 return "Public (E.163/E.164)";
255 return "Data (X.121)";
257 return "Telex (F.69)";
258 case PRI_NPI_NATIONAL:
259 return "National Standard";
260 case PRI_NPI_PRIVATE:
262 case PRI_NPI_RESERVED:
265 #endif /* defined(HAVE_PRI) */
268 #endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
270 /*! \brief Show channel types - CLI command */
271 static char *handle_cli_core_show_channeltypes(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
273 #define FORMAT "%-15.15s %-40.40s %-13.13s %-13.13s %-13.13s %-13.13s\n"
279 e->command = "core show channeltypes";
281 "Usage: core show channeltypes\n"
282 " Lists available channel types registered in your\n"
283 " Asterisk server.\n";
290 return CLI_SHOWUSAGE;
292 ast_cli(a->fd, FORMAT, "Type", "Description", "Devicestate", "Presencestate", "Indications", "Transfer");
293 ast_cli(a->fd, FORMAT, "-------------", "-------------", "-------------", "-------------", "-------------", "-------------");
295 AST_RWLIST_RDLOCK(&backends);
296 AST_RWLIST_TRAVERSE(&backends, cl, list) {
297 ast_cli(a->fd, FORMAT, cl->tech->type, cl->tech->description,
298 (cl->tech->devicestate) ? "yes" : "no",
299 (cl->tech->presencestate) ? "yes" : "no",
300 (cl->tech->indicate) ? "yes" : "no",
301 (cl->tech->transfer) ? "yes" : "no");
304 AST_RWLIST_UNLOCK(&backends);
306 ast_cli(a->fd, "----------\n%d channel drivers registered.\n", count_chan);
313 static char *complete_channeltypes(struct ast_cli_args *a)
323 wordlen = strlen(a->word);
325 AST_RWLIST_RDLOCK(&backends);
326 AST_RWLIST_TRAVERSE(&backends, cl, list) {
327 if (!strncasecmp(a->word, cl->tech->type, wordlen) && ++which > a->n) {
328 ret = ast_strdup(cl->tech->type);
332 AST_RWLIST_UNLOCK(&backends);
337 /*! \brief Show details about a channel driver - CLI command */
338 static char *handle_cli_core_show_channeltype(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
340 struct chanlist *cl = NULL;
341 struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
345 e->command = "core show channeltype";
347 "Usage: core show channeltype <name>\n"
348 " Show details about the specified channel type, <name>.\n";
351 return complete_channeltypes(a);
355 return CLI_SHOWUSAGE;
357 AST_RWLIST_RDLOCK(&backends);
359 AST_RWLIST_TRAVERSE(&backends, cl, list) {
360 if (!strncasecmp(cl->tech->type, a->argv[3], strlen(cl->tech->type)))
366 ast_cli(a->fd, "\n%s is not a registered channel driver.\n", a->argv[3]);
367 AST_RWLIST_UNLOCK(&backends);
372 "-- Info about channel driver: %s --\n"
373 " Device State: %s\n"
374 "Presence State: %s\n"
377 " Capabilities: %s\n"
381 " Image Support: %s\n"
382 " Text Support: %s\n",
384 (cl->tech->devicestate) ? "yes" : "no",
385 (cl->tech->presencestate) ? "yes" : "no",
386 (cl->tech->indicate) ? "yes" : "no",
387 (cl->tech->transfer) ? "yes" : "no",
388 ast_format_cap_get_names(cl->tech->capabilities, &codec_buf),
389 (cl->tech->send_digit_begin) ? "yes" : "no",
390 (cl->tech->send_digit_end) ? "yes" : "no",
391 (cl->tech->send_html) ? "yes" : "no",
392 (cl->tech->send_image) ? "yes" : "no",
393 (cl->tech->send_text) ? "yes" : "no"
397 AST_RWLIST_UNLOCK(&backends);
402 static struct ast_cli_entry cli_channel[] = {
403 AST_CLI_DEFINE(handle_cli_core_show_channeltypes, "List available channel types"),
404 AST_CLI_DEFINE(handle_cli_core_show_channeltype, "Give more details on that channel type")
407 static struct ast_frame *kill_read(struct ast_channel *chan)
409 /* Hangup channel. */
413 static struct ast_frame *kill_exception(struct ast_channel *chan)
415 /* Hangup channel. */
419 static int kill_write(struct ast_channel *chan, struct ast_frame *frame)
421 /* Hangup channel. */
425 static int kill_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
427 /* No problem fixing up the channel. */
431 static int kill_hangup(struct ast_channel *chan)
433 ast_channel_tech_pvt_set(chan, NULL);
438 * \brief Kill the channel channel driver technology descriptor.
441 * The purpose of this channel technology is to encourage the
442 * channel to hangup as quickly as possible.
444 * \note Used by DTMF atxfer and zombie channels.
446 const struct ast_channel_tech ast_kill_tech = {
448 .description = "Kill channel (should not see this)",
450 .exception = kill_exception,
453 .hangup = kill_hangup,
456 /*! \brief Checks to see if a channel is needing hang up */
457 int ast_check_hangup(struct ast_channel *chan)
459 if (ast_channel_softhangup_internal_flag(chan)) /* yes if soft hangup flag set */
461 if (ast_tvzero(*ast_channel_whentohangup(chan))) /* no if no hangup scheduled */
463 if (ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow()) > 0) /* no if hangup time has not come yet. */
465 ast_debug(4, "Hangup time has come: %" PRIi64 "\n", ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow()));
466 ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(chan));
467 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_TIMEOUT); /* record event */
471 int ast_check_hangup_locked(struct ast_channel *chan)
474 ast_channel_lock(chan);
475 res = ast_check_hangup(chan);
476 ast_channel_unlock(chan);
480 void ast_channel_softhangup_withcause_locked(struct ast_channel *chan, int causecode)
482 ast_channel_lock(chan);
485 ast_debug(1, "Setting hangupcause of channel %s to %d (is %d now)\n",
486 ast_channel_name(chan), causecode, ast_channel_hangupcause(chan));
488 ast_channel_hangupcause_set(chan, causecode);
491 ast_softhangup_nolock(chan, AST_SOFTHANGUP_EXPLICIT);
493 ast_channel_unlock(chan);
496 static int ast_channel_softhangup_cb(void *obj, void *arg, int flags)
498 struct ast_channel *chan = obj;
500 ast_softhangup(chan, AST_SOFTHANGUP_SHUTDOWN);
505 void ast_softhangup_all(void)
507 ao2_callback(channels, OBJ_NODATA | OBJ_MULTIPLE, ast_channel_softhangup_cb, NULL);
510 /*! \brief returns number of active/allocated channels */
511 int ast_active_channels(void)
513 return channels ? ao2_container_count(channels) : 0;
516 int ast_undestroyed_channels(void)
518 return ast_atomic_fetchadd_int(&chancount, 0);
521 /*! \brief Set when to hangup channel */
522 void ast_channel_setwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
524 if (ast_tvzero(offset)) {
525 ast_channel_whentohangup_set(chan, &offset);
527 struct timeval tv = ast_tvadd(offset, ast_tvnow());
528 ast_channel_whentohangup_set(chan, &tv);
530 ast_queue_frame(chan, &ast_null_frame);
534 void ast_channel_setwhentohangup(struct ast_channel *chan, time_t offset)
536 struct timeval when = { offset, };
537 ast_channel_setwhentohangup_tv(chan, when);
540 /*! \brief Compare a offset with when to hangup channel */
541 int ast_channel_cmpwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
543 struct timeval whentohangup;
545 if (ast_tvzero(*ast_channel_whentohangup(chan)))
546 return ast_tvzero(offset) ? 0 : -1;
548 if (ast_tvzero(offset))
551 whentohangup = ast_tvadd(offset, ast_tvnow());
553 return ast_tvdiff_ms(whentohangup, *ast_channel_whentohangup(chan));
556 int ast_channel_cmpwhentohangup(struct ast_channel *chan, time_t offset)
558 struct timeval when = { offset, };
559 return ast_channel_cmpwhentohangup_tv(chan, when);
562 /*! \brief Register a new telephony channel in Asterisk */
563 int ast_channel_register(const struct ast_channel_tech *tech)
565 struct chanlist *chan;
567 AST_RWLIST_WRLOCK(&backends);
569 AST_RWLIST_TRAVERSE(&backends, chan, list) {
570 if (!strcasecmp(tech->type, chan->tech->type)) {
571 ast_log(LOG_WARNING, "Already have a handler for type '%s'\n", tech->type);
572 AST_RWLIST_UNLOCK(&backends);
577 if (!(chan = ast_calloc(1, sizeof(*chan)))) {
578 AST_RWLIST_UNLOCK(&backends);
582 AST_RWLIST_INSERT_HEAD(&backends, chan, list);
584 ast_debug(1, "Registered handler for '%s' (%s)\n", chan->tech->type, chan->tech->description);
586 ast_verb(2, "Registered channel type '%s' (%s)\n", chan->tech->type, chan->tech->description);
588 AST_RWLIST_UNLOCK(&backends);
593 /*! \brief Unregister channel driver */
594 void ast_channel_unregister(const struct ast_channel_tech *tech)
596 struct chanlist *chan;
598 ast_debug(1, "Unregistering channel type '%s'\n", tech->type);
600 AST_RWLIST_WRLOCK(&backends);
602 AST_RWLIST_TRAVERSE_SAFE_BEGIN(&backends, chan, list) {
603 if (chan->tech == tech) {
604 AST_LIST_REMOVE_CURRENT(list);
606 ast_verb(2, "Unregistered channel type '%s'\n", tech->type);
610 AST_LIST_TRAVERSE_SAFE_END;
612 AST_RWLIST_UNLOCK(&backends);
615 /*! \brief Get handle to channel driver based on name */
616 const struct ast_channel_tech *ast_get_channel_tech(const char *name)
618 struct chanlist *chanls;
619 const struct ast_channel_tech *ret = NULL;
621 AST_RWLIST_RDLOCK(&backends);
623 AST_RWLIST_TRAVERSE(&backends, chanls, list) {
624 if (!strcasecmp(name, chanls->tech->type)) {
630 AST_RWLIST_UNLOCK(&backends);
635 /*! \brief Gives the string form of a given hangup cause */
636 const char *ast_cause2str(int cause)
640 for (x = 0; x < ARRAY_LEN(causes); x++) {
641 if (causes[x].cause == cause)
642 return causes[x].desc;
648 /*! \brief Convert a symbolic hangup cause to number */
649 int ast_str2cause(const char *name)
653 for (x = 0; x < ARRAY_LEN(causes); x++)
654 if (!strncasecmp(causes[x].name, name, strlen(causes[x].name)))
655 return causes[x].cause;
660 static struct stasis_message *create_channel_snapshot_message(struct ast_channel *channel)
662 RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
664 if (!ast_channel_snapshot_type()) {
668 ast_channel_lock(channel);
669 snapshot = ast_channel_snapshot_create(channel);
670 ast_channel_unlock(channel);
675 return stasis_message_create(ast_channel_snapshot_type(), snapshot);
678 static void publish_cache_clear(struct ast_channel *chan)
680 RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
681 RAII_VAR(struct stasis_message *, clear_msg, NULL, ao2_cleanup);
683 clear_msg = create_channel_snapshot_message(chan);
688 message = stasis_cache_clear_create(clear_msg);
689 stasis_publish(ast_channel_topic(chan), message);
692 /*! \brief Gives the string form of a given channel state.
694 * \note This function is not reentrant.
698 const char *ast_state2str(enum ast_channel_state state)
705 case AST_STATE_RESERVED:
707 case AST_STATE_OFFHOOK:
709 case AST_STATE_DIALING:
713 case AST_STATE_RINGING:
719 case AST_STATE_DIALING_OFFHOOK:
720 return "Dialing Offhook";
721 case AST_STATE_PRERING:
726 if (!(buf = ast_threadstorage_get(&state2str_threadbuf, STATE2STR_BUFSIZE)))
728 snprintf(buf, STATE2STR_BUFSIZE, "Unknown (%u)", state);
733 /*! \brief Gives the string form of a given transfer capability */
734 char *ast_transfercapability2str(int transfercapability)
736 switch (transfercapability) {
737 case AST_TRANS_CAP_SPEECH:
739 case AST_TRANS_CAP_DIGITAL:
741 case AST_TRANS_CAP_RESTRICTED_DIGITAL:
742 return "RESTRICTED_DIGITAL";
743 case AST_TRANS_CAP_3_1K_AUDIO:
745 case AST_TRANS_CAP_DIGITAL_W_TONES:
746 return "DIGITAL_W_TONES";
747 case AST_TRANS_CAP_VIDEO:
754 /*! \brief Channel technology used to extract a channel from a running application. The
755 * channel created with this technology will be immediately hung up - most external
756 * applications won't ever want to see this.
758 static const struct ast_channel_tech surrogate_tech = {
760 .description = "Surrogate channel used to pull channel from an application",
761 .properties = AST_CHAN_TP_INTERNAL,
764 static const struct ast_channel_tech null_tech = {
766 .description = "Null channel (should not see this)",
769 static void ast_channel_destructor(void *obj);
770 static void ast_dummy_channel_destructor(void *obj);
771 static int ast_channel_by_uniqueid_cb(void *obj, void *arg, void *data, int flags);
773 static int does_id_conflict(const char *uniqueid)
775 struct ast_channel *conflict;
778 if (ast_strlen_zero(uniqueid)) {
782 conflict = ast_channel_callback(ast_channel_by_uniqueid_cb, (char *) uniqueid, &length, OBJ_NOLOCK);
784 ast_log(LOG_ERROR, "Channel Unique ID '%s' already in use by channel %s(%p)\n",
785 uniqueid, ast_channel_name(conflict), conflict);
786 ast_channel_unref(conflict);
793 /*! \brief Create a new channel structure */
794 static struct ast_channel * attribute_malloc __attribute__((format(printf, 15, 0)))
795 __ast_channel_alloc_ap(int needqueue, int state, const char *cid_num, const char *cid_name,
796 const char *acctcode, const char *exten, const char *context, const struct ast_assigned_ids *assignedids,
797 const struct ast_channel *requestor, enum ama_flags amaflag, struct ast_endpoint *endpoint,
798 const char *file, int line,
799 const char *function, const char *name_fmt, va_list ap)
801 struct ast_channel *tmp;
802 struct varshead *headp;
803 char *tech = "", *tech2 = NULL;
804 struct ast_format_cap *nativeformats;
805 struct ast_sched_context *schedctx;
806 struct ast_timer *timer;
808 const struct ast_channel_tech *channel_tech;
810 /* If shutting down, don't allocate any new channels */
811 if (ast_shutting_down()) {
812 ast_log(LOG_WARNING, "Channel allocation failed: Refusing due to active shutdown\n");
816 tmp = __ast_channel_internal_alloc(ast_channel_destructor, assignedids, requestor,
817 file, line, function);
819 /* Channel structure allocation failure. */
823 ast_channel_stage_snapshot(tmp);
825 if (!(nativeformats = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
826 /* format capabilities structure allocation failure */
827 return ast_channel_unref(tmp);
829 ast_format_cap_append(nativeformats, ast_format_none, 0);
830 ast_channel_nativeformats_set(tmp, nativeformats);
831 ao2_ref(nativeformats, -1);
833 ast_channel_set_rawwriteformat(tmp, ast_format_none);
834 ast_channel_set_rawreadformat(tmp, ast_format_none);
835 ast_channel_set_writeformat(tmp, ast_format_none);
836 ast_channel_set_readformat(tmp, ast_format_none);
839 * Init file descriptors to unopened state so
840 * the destructor can know not to close them.
842 ast_channel_timingfd_set(tmp, -1);
843 ast_channel_internal_alertpipe_clear(tmp);
844 ast_channel_internal_fd_clear_all(tmp);
847 ast_channel_epfd_set(tmp, epoll_create(25));
850 if (!(schedctx = ast_sched_context_create())) {
851 ast_log(LOG_WARNING, "Channel allocation failed: Unable to create schedule context\n");
852 return ast_channel_unref(tmp);
854 ast_channel_sched_set(tmp, schedctx);
856 ast_party_dialed_init(ast_channel_dialed(tmp));
857 ast_party_caller_init(ast_channel_caller(tmp));
858 ast_party_connected_line_init(ast_channel_connected(tmp));
859 ast_party_connected_line_init(ast_channel_connected_indicated(tmp));
860 ast_party_redirecting_init(ast_channel_redirecting(tmp));
863 ast_channel_caller(tmp)->id.name.valid = 1;
864 ast_channel_caller(tmp)->id.name.str = ast_strdup(cid_name);
865 if (!ast_channel_caller(tmp)->id.name.str) {
866 return ast_channel_unref(tmp);
870 ast_channel_caller(tmp)->id.number.valid = 1;
871 ast_channel_caller(tmp)->id.number.str = ast_strdup(cid_num);
872 if (!ast_channel_caller(tmp)->id.number.str) {
873 return ast_channel_unref(tmp);
877 if ((timer = ast_timer_open())) {
878 ast_channel_timer_set(tmp, timer);
879 if (strcmp(ast_timer_get_name(ast_channel_timer(tmp)), "timerfd")) {
882 ast_channel_timingfd_set(tmp, ast_timer_fd(ast_channel_timer(tmp)));
885 if (needqueue && ast_channel_internal_alertpipe_init(tmp)) {
886 return ast_channel_unref(tmp);
889 /* Always watch the alertpipe */
890 ast_channel_set_fd(tmp, AST_ALERT_FD, ast_channel_internal_alert_readfd(tmp));
891 /* And timing pipe */
892 ast_channel_set_fd(tmp, AST_TIMING_FD, ast_channel_timingfd(tmp));
895 ast_channel_state_set(tmp, state);
896 ast_channel_hold_state_set(tmp, AST_CONTROL_UNHOLD);
898 ast_channel_streamid_set(tmp, -1);
899 ast_channel_vstreamid_set(tmp, -1);
901 ast_channel_fin_set(tmp, global_fin);
902 ast_channel_fout_set(tmp, global_fout);
905 ast_channel_creationtime_set(tmp, &now);
907 ast_channel_internal_setup_topics(tmp);
909 if (!ast_strlen_zero(name_fmt)) {
910 char *slash, *slash2;
911 /* Almost every channel is calling this function, and setting the name via the ast_string_field_build() call.
912 * And they all use slightly different formats for their name string.
913 * This means, to set the name here, we have to accept variable args, and call the string_field_build from here.
914 * This means, that the stringfields must have a routine that takes the va_lists directly, and
915 * uses them to build the string, instead of forming the va_lists internally from the vararg ... list.
916 * This new function was written so this can be accomplished.
918 ast_channel_name_build_va(tmp, name_fmt, ap);
919 tech = ast_strdupa(ast_channel_name(tmp));
920 if ((slash = strchr(tech, '/'))) {
921 if ((slash2 = strchr(slash + 1, '/'))) {
929 * Start the string with '-' so it becomes an empty string
932 ast_channel_name_set(tmp, "-**Unknown**");
935 if (amaflag != AST_AMA_NONE) {
936 ast_channel_amaflags_set(tmp, amaflag);
938 ast_channel_amaflags_set(tmp, DEFAULT_AMA_FLAGS);
941 if (!ast_strlen_zero(acctcode)) {
942 ast_channel_accountcode_set(tmp, acctcode);
944 ast_channel_language_set(tmp, ast_defaultlanguage);
946 ast_channel_context_set(tmp, S_OR(context, "default"));
947 ast_channel_exten_set(tmp, S_OR(exten, "s"));
948 ast_channel_priority_set(tmp, 1);
950 headp = ast_channel_varshead(tmp);
951 AST_LIST_HEAD_INIT_NOLOCK(headp);
953 ast_pbx_hangup_handler_init(tmp);
954 AST_LIST_HEAD_INIT_NOLOCK(ast_channel_datastores(tmp));
955 AST_LIST_HEAD_INIT_NOLOCK(ast_channel_autochans(tmp));
957 channel_tech = ast_get_channel_tech(tech);
958 if (!channel_tech && !ast_strlen_zero(tech2)) {
959 channel_tech = ast_get_channel_tech(tech2);
962 ast_channel_tech_set(tmp, channel_tech);
964 ast_channel_tech_set(tmp, &null_tech);
967 /* You might scream "locking inversion" at seeing this but it is actually perfectly fine.
968 * Since the channel was just created nothing can know about it yet or even acquire it.
970 ast_channel_lock(tmp);
974 if (assignedids && (does_id_conflict(assignedids->uniqueid) || does_id_conflict(assignedids->uniqueid2))) {
975 ast_channel_internal_errno_set(AST_CHANNEL_ERROR_ID_EXISTS);
976 ao2_unlock(channels);
977 /* This is a bit unorthodox, but we can't just call ast_channel_stage_snapshot_done()
978 * because that will result in attempting to publish the channel snapshot. That causes
979 * badness in some places, such as CDRs. So we need to manually clear the flag on the
980 * channel that says that a snapshot is being cleared.
982 ast_clear_flag(ast_channel_flags(tmp), AST_FLAG_SNAPSHOT_STAGE);
983 ast_channel_unlock(tmp);
984 return ast_channel_unref(tmp);
987 ast_channel_internal_finalize(tmp);
989 ast_atomic_fetchadd_int(&chancount, +1);
991 ao2_link_flags(channels, tmp, OBJ_NOLOCK);
993 ao2_unlock(channels);
996 ast_endpoint_add_channel(endpoint, tmp);
1000 * And now, since the channel structure is built, and has its name, let
1001 * the world know of its existance
1003 ast_channel_stage_snapshot_done(tmp);
1007 struct ast_channel *__ast_channel_alloc(int needqueue, int state, const char *cid_num,
1008 const char *cid_name, const char *acctcode,
1009 const char *exten, const char *context, const struct ast_assigned_ids *assignedids,
1010 const struct ast_channel *requestor, enum ama_flags amaflag,
1011 struct ast_endpoint *endpoint,
1012 const char *file, int line, const char *function,
1013 const char *name_fmt, ...)
1016 struct ast_channel *result;
1018 va_start(ap, name_fmt);
1019 result = __ast_channel_alloc_ap(needqueue, state, cid_num, cid_name, acctcode, exten, context,
1020 assignedids, requestor, amaflag, endpoint, file, line, function, name_fmt, ap);
1026 /* only do the minimum amount of work needed here to make a channel
1027 * structure that can be used to expand channel vars */
1028 struct ast_channel *__ast_dummy_channel_alloc(const char *file, int line, const char *function)
1030 struct ast_channel *tmp;
1031 struct varshead *headp;
1033 tmp = __ast_channel_internal_alloc(ast_dummy_channel_destructor, NULL, NULL,
1034 file, line, function);
1036 /* Dummy channel structure allocation failure. */
1040 ast_pbx_hangup_handler_init(tmp);
1041 AST_LIST_HEAD_INIT_NOLOCK(ast_channel_datastores(tmp));
1044 * Init file descriptors to unopened state just in case
1045 * autoservice is called on the channel or something tries to
1046 * read a frame from it.
1048 ast_channel_timingfd_set(tmp, -1);
1049 ast_channel_internal_alertpipe_clear(tmp);
1050 ast_channel_internal_fd_clear_all(tmp);
1052 ast_channel_epfd_set(tmp, -1);
1055 ast_channel_hold_state_set(tmp, AST_CONTROL_UNHOLD);
1057 ast_channel_internal_setup_topics(tmp);
1059 headp = ast_channel_varshead(tmp);
1060 AST_LIST_HEAD_INIT_NOLOCK(headp);
1065 void ast_channel_start_defer_frames(struct ast_channel *chan)
1067 ast_set_flag(ast_channel_flags(chan), AST_FLAG_DEFER_FRAMES);
1070 void ast_channel_stop_defer_frames(struct ast_channel *chan)
1072 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_DEFER_FRAMES);
1074 /* Move the deferred frames onto the channel read queue, ahead of other queued frames */
1075 ast_queue_frame_head(chan, AST_LIST_FIRST(ast_channel_deferred_readq(chan)));
1076 /* ast_frfree will mosey down the list and free them all */
1077 if (!AST_LIST_EMPTY(ast_channel_deferred_readq(chan))) {
1078 ast_frfree(AST_LIST_FIRST(ast_channel_deferred_readq(chan)));
1080 /* Reset the list to be empty */
1081 AST_LIST_HEAD_INIT_NOLOCK(ast_channel_deferred_readq(chan));
1084 static int __ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin, int head, struct ast_frame *after)
1086 struct ast_frame *f;
1087 struct ast_frame *cur;
1088 unsigned int new_frames = 0;
1089 unsigned int new_voice_frames = 0;
1090 unsigned int queued_frames = 0;
1091 unsigned int queued_voice_frames = 0;
1092 AST_LIST_HEAD_NOLOCK(,ast_frame) frames;
1094 ast_channel_lock(chan);
1097 * Check the last frame on the queue if we are queuing the new
1100 cur = AST_LIST_LAST(ast_channel_readq(chan));
1101 if (cur && cur->frametype == AST_FRAME_CONTROL && !head && (!after || after == cur)) {
1102 switch (cur->subclass.integer) {
1103 case AST_CONTROL_END_OF_Q:
1104 if (fin->frametype == AST_FRAME_CONTROL
1105 && fin->subclass.integer == AST_CONTROL_HANGUP) {
1107 * Destroy the end-of-Q marker frame so we can queue the hangup
1108 * frame in its place.
1110 AST_LIST_REMOVE(ast_channel_readq(chan), cur, frame_list);
1114 * This has degenerated to a normal queue append anyway. Since
1115 * we just destroyed the last frame in the queue we must make
1116 * sure that "after" is NULL or bad things will happen.
1122 case AST_CONTROL_HANGUP:
1123 /* Don't queue anything. */
1124 ast_channel_unlock(chan);
1131 /* Build copies of all the new frames and count them */
1132 AST_LIST_HEAD_INIT_NOLOCK(&frames);
1133 for (cur = fin; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
1134 if (!(f = ast_frdup(cur))) {
1135 if (AST_LIST_FIRST(&frames)) {
1136 ast_frfree(AST_LIST_FIRST(&frames));
1138 ast_channel_unlock(chan);
1142 AST_LIST_INSERT_TAIL(&frames, f, frame_list);
1144 if (f->frametype == AST_FRAME_VOICE) {
1149 /* Count how many frames exist on the queue */
1150 AST_LIST_TRAVERSE(ast_channel_readq(chan), cur, frame_list) {
1152 if (cur->frametype == AST_FRAME_VOICE) {
1153 queued_voice_frames++;
1157 if ((queued_frames + new_frames > 128 || queued_voice_frames + new_voice_frames > 96)) {
1159 ast_log(LOG_WARNING, "Exceptionally long %squeue length queuing to %s\n", queued_frames + new_frames > 128 ? "" : "voice ", ast_channel_name(chan));
1160 AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_readq(chan), cur, frame_list) {
1161 /* Save the most recent frame */
1162 if (!AST_LIST_NEXT(cur, frame_list)) {
1164 } else if (cur->frametype == AST_FRAME_VOICE || cur->frametype == AST_FRAME_VIDEO || cur->frametype == AST_FRAME_NULL) {
1168 AST_LIST_REMOVE_CURRENT(frame_list);
1172 AST_LIST_TRAVERSE_SAFE_END;
1176 AST_LIST_INSERT_LIST_AFTER(ast_channel_readq(chan), &frames, after, frame_list);
1179 AST_LIST_APPEND_LIST(&frames, ast_channel_readq(chan), frame_list);
1180 AST_LIST_HEAD_INIT_NOLOCK(ast_channel_readq(chan));
1182 AST_LIST_APPEND_LIST(ast_channel_readq(chan), &frames, frame_list);
1185 if (ast_channel_alert_writable(chan)) {
1186 if (ast_channel_alert_write(chan)) {
1187 ast_log(LOG_WARNING, "Unable to write to alert pipe on %s (qlen = %u): %s!\n",
1188 ast_channel_name(chan), queued_frames, strerror(errno));
1190 } else if (ast_channel_timingfd(chan) > -1) {
1191 ast_timer_enable_continuous(ast_channel_timer(chan));
1192 } else if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING)) {
1193 pthread_kill(ast_channel_blocker(chan), SIGURG);
1196 ast_channel_unlock(chan);
1201 int ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin)
1203 return __ast_queue_frame(chan, fin, 0, NULL);
1206 int ast_queue_frame_head(struct ast_channel *chan, struct ast_frame *fin)
1208 return __ast_queue_frame(chan, fin, 1, NULL);
1211 /*! \brief Queue a hangup frame for channel */
1212 int ast_queue_hangup(struct ast_channel *chan)
1214 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
1217 /* Yeah, let's not change a lock-critical value without locking */
1218 ast_channel_lock(chan);
1219 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
1220 ast_channel_publish_blob(chan, ast_channel_hangup_request_type(), NULL);
1222 res = ast_queue_frame(chan, &f);
1223 ast_channel_unlock(chan);
1227 /*! \brief Queue a hangup frame for channel */
1228 int ast_queue_hangup_with_cause(struct ast_channel *chan, int cause)
1230 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
1231 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
1235 f.data.uint32 = cause;
1238 /* Yeah, let's not change a lock-critical value without locking */
1239 ast_channel_lock(chan);
1240 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_DEV);
1242 f.data.uint32 = ast_channel_hangupcause(chan);
1244 blob = ast_json_pack("{s: i}",
1246 ast_channel_publish_blob(chan, ast_channel_hangup_request_type(), blob);
1248 res = ast_queue_frame(chan, &f);
1249 ast_channel_unlock(chan);
1253 int ast_queue_hold(struct ast_channel *chan, const char *musicclass)
1255 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HOLD };
1256 struct ast_json *blob = NULL;
1259 if (!ast_strlen_zero(musicclass)) {
1260 f.data.ptr = (void *) musicclass;
1261 f.datalen = strlen(musicclass) + 1;
1263 blob = ast_json_pack("{s: s}",
1264 "musicclass", musicclass);
1267 ast_channel_publish_cached_blob(chan, ast_channel_hold_type(), blob);
1269 res = ast_queue_frame(chan, &f);
1271 ast_json_unref(blob);
1276 int ast_queue_unhold(struct ast_channel *chan)
1278 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_UNHOLD };
1281 ast_channel_publish_cached_blob(chan, ast_channel_unhold_type(), NULL);
1283 res = ast_queue_frame(chan, &f);
1288 /*! \brief Queue a control frame */
1289 int ast_queue_control(struct ast_channel *chan, enum ast_control_frame_type control)
1291 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control };
1292 return ast_queue_frame(chan, &f);
1295 /*! \brief Queue a control frame with payload */
1296 int ast_queue_control_data(struct ast_channel *chan, enum ast_control_frame_type control,
1297 const void *data, size_t datalen)
1299 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control, .data.ptr = (void *) data, .datalen = datalen };
1300 return ast_queue_frame(chan, &f);
1303 /*! \brief Set defer DTMF flag on channel */
1304 int ast_channel_defer_dtmf(struct ast_channel *chan)
1309 pre = ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF);
1310 ast_set_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF);
1315 /*! \brief Unset defer DTMF flag on channel */
1316 void ast_channel_undefer_dtmf(struct ast_channel *chan)
1319 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF);
1322 struct ast_channel *ast_channel_callback(ao2_callback_data_fn *cb_fn, void *arg,
1323 void *data, int ao2_flags)
1325 return ao2_callback_data(channels, ao2_flags, cb_fn, arg, data);
1328 static int ast_channel_by_name_cb(void *obj, void *arg, void *data, int flags)
1330 struct ast_channel *chan = obj;
1331 const char *name = arg;
1332 size_t name_len = *(size_t *) data;
1333 int ret = CMP_MATCH;
1335 if (ast_strlen_zero(name)) {
1336 ast_log(LOG_ERROR, "BUG! Must supply a channel name or partial name to match!\n");
1340 ast_channel_lock(chan);
1341 if ((!name_len && strcasecmp(ast_channel_name(chan), name))
1342 || (name_len && strncasecmp(ast_channel_name(chan), name, name_len))) {
1343 ret = 0; /* name match failed, keep looking */
1345 ast_channel_unlock(chan);
1350 static int ast_channel_by_exten_cb(void *obj, void *arg, void *data, int flags)
1352 struct ast_channel *chan = obj;
1353 char *context = arg;
1355 int ret = CMP_MATCH;
1357 if (ast_strlen_zero(exten) || ast_strlen_zero(context)) {
1358 ast_log(LOG_ERROR, "BUG! Must have a context and extension to match!\n");
1362 ast_channel_lock(chan);
1363 if (strcasecmp(ast_channel_context(chan), context) && strcasecmp(ast_channel_macrocontext(chan), context)) {
1364 ret = 0; /* Context match failed, continue */
1365 } else if (strcasecmp(ast_channel_exten(chan), exten) && strcasecmp(ast_channel_macroexten(chan), exten)) {
1366 ret = 0; /* Extension match failed, continue */
1368 ast_channel_unlock(chan);
1373 static int ast_channel_by_uniqueid_cb(void *obj, void *arg, void *data, int flags)
1375 struct ast_channel *chan = obj;
1376 char *uniqueid = arg;
1377 size_t id_len = *(size_t *) data;
1378 int ret = CMP_MATCH;
1380 if (ast_strlen_zero(uniqueid)) {
1381 ast_log(LOG_ERROR, "BUG! Must supply a uniqueid or partial uniqueid to match!\n");
1385 ast_channel_lock(chan);
1386 if ((!id_len && strcasecmp(ast_channel_uniqueid(chan), uniqueid))
1387 || (id_len && strncasecmp(ast_channel_uniqueid(chan), uniqueid, id_len))) {
1388 ret = 0; /* uniqueid match failed, keep looking */
1390 ast_channel_unlock(chan);
1395 struct ast_channel_iterator {
1396 /* storage for non-dynamically allocated iterator */
1397 struct ao2_iterator simple_iterator;
1398 /* pointer to the actual iterator (simple_iterator or a dynamically
1399 * allocated iterator)
1401 struct ao2_iterator *active_iterator;
1404 struct ast_channel_iterator *ast_channel_iterator_destroy(struct ast_channel_iterator *i)
1406 ao2_iterator_destroy(i->active_iterator);
1412 struct ast_channel_iterator *ast_channel_iterator_by_exten_new(const char *exten, const char *context)
1414 struct ast_channel_iterator *i;
1415 char *l_exten = (char *) exten;
1416 char *l_context = (char *) context;
1418 if (!(i = ast_calloc(1, sizeof(*i)))) {
1422 i->active_iterator = (void *) ast_channel_callback(ast_channel_by_exten_cb,
1423 l_context, l_exten, OBJ_MULTIPLE);
1424 if (!i->active_iterator) {
1432 struct ast_channel_iterator *ast_channel_iterator_by_name_new(const char *name, size_t name_len)
1434 struct ast_channel_iterator *i;
1435 char *l_name = (char *) name;
1437 if (!(i = ast_calloc(1, sizeof(*i)))) {
1441 i->active_iterator = (void *) ast_channel_callback(ast_channel_by_name_cb,
1443 OBJ_MULTIPLE | (name_len == 0 /* match the whole word, so optimize */ ? OBJ_KEY : 0));
1444 if (!i->active_iterator) {
1452 struct ast_channel_iterator *ast_channel_iterator_all_new(void)
1454 struct ast_channel_iterator *i;
1456 if (!(i = ast_calloc(1, sizeof(*i)))) {
1460 i->simple_iterator = ao2_iterator_init(channels, 0);
1461 i->active_iterator = &i->simple_iterator;
1466 struct ast_channel *ast_channel_iterator_next(struct ast_channel_iterator *i)
1468 return ao2_iterator_next(i->active_iterator);
1471 /* Legacy function, not currently used for lookups, but we need a cmp_fn */
1472 static int ast_channel_cmp_cb(void *obj, void *arg, int flags)
1474 ast_log(LOG_ERROR, "BUG! Should never be called!\n");
1478 struct ast_channel *ast_channel_get_by_name_prefix(const char *name, size_t name_len)
1480 struct ast_channel *chan;
1481 char *l_name = (char *) name;
1483 chan = ast_channel_callback(ast_channel_by_name_cb, l_name, &name_len,
1484 (name_len == 0) /* optimize if it is a complete name match */ ? OBJ_KEY : 0);
1489 if (ast_strlen_zero(l_name)) {
1490 /* We didn't have a name to search for so quit. */
1494 /* Now try a search for uniqueid. */
1495 return ast_channel_callback(ast_channel_by_uniqueid_cb, l_name, &name_len, 0);
1498 struct ast_channel *ast_channel_get_by_name(const char *name)
1500 return ast_channel_get_by_name_prefix(name, 0);
1503 struct ast_channel *ast_channel_get_by_exten(const char *exten, const char *context)
1505 char *l_exten = (char *) exten;
1506 char *l_context = (char *) context;
1508 return ast_channel_callback(ast_channel_by_exten_cb, l_context, l_exten, 0);
1511 int ast_is_deferrable_frame(const struct ast_frame *frame)
1513 /* Do not add a default entry in this switch statement. Each new
1514 * frame type should be addressed directly as to whether it should
1515 * be queued up or not.
1517 switch (frame->frametype) {
1518 case AST_FRAME_BRIDGE_ACTION:
1519 case AST_FRAME_BRIDGE_ACTION_SYNC:
1520 case AST_FRAME_CONTROL:
1521 case AST_FRAME_TEXT:
1522 case AST_FRAME_IMAGE:
1523 case AST_FRAME_HTML:
1526 case AST_FRAME_DTMF_END:
1527 case AST_FRAME_DTMF_BEGIN:
1528 case AST_FRAME_VOICE:
1529 case AST_FRAME_VIDEO:
1530 case AST_FRAME_NULL:
1533 case AST_FRAME_MODEM:
1539 /*! \brief Wait, look for hangups and condition arg */
1540 int ast_safe_sleep_conditional(struct ast_channel *chan, int timeout_ms, int (*cond)(void*), void *data)
1542 struct ast_frame *f;
1543 struct ast_silence_generator *silgen = NULL;
1545 struct timeval start;
1547 AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_frames;
1549 AST_LIST_HEAD_INIT_NOLOCK(&deferred_frames);
1551 /* If no other generator is present, start silencegen while waiting */
1552 if (ast_opt_transmit_silence && !ast_channel_generatordata(chan)) {
1553 silgen = ast_channel_start_silence_generator(chan);
1556 start = ast_tvnow();
1557 while ((ms = ast_remaining_ms(start, timeout_ms))) {
1558 struct ast_frame *dup_f = NULL;
1560 if (cond && ((*cond)(data) == 0)) {
1563 ms = ast_waitfor(chan, ms);
1575 if (!ast_is_deferrable_frame(f)) {
1580 if ((dup_f = ast_frisolate(f))) {
1584 AST_LIST_INSERT_HEAD(&deferred_frames, dup_f, frame_list);
1589 /* stop silgen if present */
1591 ast_channel_stop_silence_generator(chan, silgen);
1594 /* We need to free all the deferred frames, but we only need to
1595 * queue the deferred frames if there was no error and no
1596 * hangup was received
1598 ast_channel_lock(chan);
1599 while ((f = AST_LIST_REMOVE_HEAD(&deferred_frames, frame_list))) {
1601 ast_queue_frame_head(chan, f);
1605 ast_channel_unlock(chan);
1610 /*! \brief Wait, look for hangups */
1611 int ast_safe_sleep(struct ast_channel *chan, int ms)
1613 return ast_safe_sleep_conditional(chan, ms, NULL, NULL);
1616 struct ast_channel *ast_channel_release(struct ast_channel *chan)
1618 /* Safe, even if already unlinked. */
1619 ao2_unlink(channels, chan);
1620 return ast_channel_unref(chan);
1623 void ast_party_name_init(struct ast_party_name *init)
1626 init->char_set = AST_PARTY_CHAR_SET_ISO8859_1;
1627 init->presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
1631 void ast_party_name_copy(struct ast_party_name *dest, const struct ast_party_name *src)
1634 /* Don't copy to self */
1638 ast_free(dest->str);
1639 dest->str = ast_strdup(src->str);
1640 dest->char_set = src->char_set;
1641 dest->presentation = src->presentation;
1642 dest->valid = src->valid;
1645 void ast_party_name_set_init(struct ast_party_name *init, const struct ast_party_name *guide)
1648 init->char_set = guide->char_set;
1649 init->presentation = guide->presentation;
1650 init->valid = guide->valid;
1653 void ast_party_name_set(struct ast_party_name *dest, const struct ast_party_name *src)
1656 /* Don't set to self */
1660 if (src->str && src->str != dest->str) {
1661 ast_free(dest->str);
1662 dest->str = ast_strdup(src->str);
1665 dest->char_set = src->char_set;
1666 dest->presentation = src->presentation;
1667 dest->valid = src->valid;
1670 void ast_party_name_free(struct ast_party_name *doomed)
1672 ast_free(doomed->str);
1676 void ast_party_number_init(struct ast_party_number *init)
1679 init->plan = 0;/* Unknown */
1680 init->presentation = AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED;
1684 void ast_party_number_copy(struct ast_party_number *dest, const struct ast_party_number *src)
1687 /* Don't copy to self */
1691 ast_free(dest->str);
1692 dest->str = ast_strdup(src->str);
1693 dest->plan = src->plan;
1694 dest->presentation = src->presentation;
1695 dest->valid = src->valid;
1698 void ast_party_number_set_init(struct ast_party_number *init, const struct ast_party_number *guide)
1701 init->plan = guide->plan;
1702 init->presentation = guide->presentation;
1703 init->valid = guide->valid;
1706 void ast_party_number_set(struct ast_party_number *dest, const struct ast_party_number *src)
1709 /* Don't set to self */
1713 if (src->str && src->str != dest->str) {
1714 ast_free(dest->str);
1715 dest->str = ast_strdup(src->str);
1718 dest->plan = src->plan;
1719 dest->presentation = src->presentation;
1720 dest->valid = src->valid;
1723 void ast_party_number_free(struct ast_party_number *doomed)
1725 ast_free(doomed->str);
1729 void ast_party_subaddress_init(struct ast_party_subaddress *init)
1733 init->odd_even_indicator = 0;
1737 void ast_party_subaddress_copy(struct ast_party_subaddress *dest, const struct ast_party_subaddress *src)
1740 /* Don't copy to self */
1744 ast_free(dest->str);
1745 dest->str = ast_strdup(src->str);
1746 dest->type = src->type;
1747 dest->odd_even_indicator = src->odd_even_indicator;
1748 dest->valid = src->valid;
1751 void ast_party_subaddress_set_init(struct ast_party_subaddress *init, const struct ast_party_subaddress *guide)
1754 init->type = guide->type;
1755 init->odd_even_indicator = guide->odd_even_indicator;
1756 init->valid = guide->valid;
1759 void ast_party_subaddress_set(struct ast_party_subaddress *dest, const struct ast_party_subaddress *src)
1762 /* Don't set to self */
1766 if (src->str && src->str != dest->str) {
1767 ast_free(dest->str);
1768 dest->str = ast_strdup(src->str);
1771 dest->type = src->type;
1772 dest->odd_even_indicator = src->odd_even_indicator;
1773 dest->valid = src->valid;
1776 void ast_party_subaddress_free(struct ast_party_subaddress *doomed)
1778 ast_free(doomed->str);
1782 void ast_set_party_id_all(struct ast_set_party_id *update_id)
1784 update_id->name = 1;
1785 update_id->number = 1;
1786 update_id->subaddress = 1;
1789 void ast_party_id_init(struct ast_party_id *init)
1791 ast_party_name_init(&init->name);
1792 ast_party_number_init(&init->number);
1793 ast_party_subaddress_init(&init->subaddress);
1797 void ast_party_id_copy(struct ast_party_id *dest, const struct ast_party_id *src)
1800 /* Don't copy to self */
1804 ast_party_name_copy(&dest->name, &src->name);
1805 ast_party_number_copy(&dest->number, &src->number);
1806 ast_party_subaddress_copy(&dest->subaddress, &src->subaddress);
1808 ast_free(dest->tag);
1809 dest->tag = ast_strdup(src->tag);
1812 void ast_party_id_set_init(struct ast_party_id *init, const struct ast_party_id *guide)
1814 ast_party_name_set_init(&init->name, &guide->name);
1815 ast_party_number_set_init(&init->number, &guide->number);
1816 ast_party_subaddress_set_init(&init->subaddress, &guide->subaddress);
1820 void ast_party_id_set(struct ast_party_id *dest, const struct ast_party_id *src, const struct ast_set_party_id *update)
1823 /* Don't set to self */
1827 if (!update || update->name) {
1828 ast_party_name_set(&dest->name, &src->name);
1830 if (!update || update->number) {
1831 ast_party_number_set(&dest->number, &src->number);
1833 if (!update || update->subaddress) {
1834 ast_party_subaddress_set(&dest->subaddress, &src->subaddress);
1837 if (src->tag && src->tag != dest->tag) {
1838 ast_free(dest->tag);
1839 dest->tag = ast_strdup(src->tag);
1843 void ast_party_id_free(struct ast_party_id *doomed)
1845 ast_party_name_free(&doomed->name);
1846 ast_party_number_free(&doomed->number);
1847 ast_party_subaddress_free(&doomed->subaddress);
1849 ast_free(doomed->tag);
1853 int ast_party_id_presentation(const struct ast_party_id *id)
1855 int number_priority;
1857 int number_screening;
1861 /* Determine name presentation priority. */
1862 if (!id->name.valid) {
1863 name_value = AST_PRES_UNAVAILABLE;
1866 name_value = id->name.presentation & AST_PRES_RESTRICTION;
1867 switch (name_value) {
1868 case AST_PRES_RESTRICTED:
1871 case AST_PRES_ALLOWED:
1874 case AST_PRES_UNAVAILABLE:
1878 name_value = AST_PRES_UNAVAILABLE;
1884 /* Determine number presentation priority. */
1885 if (!id->number.valid) {
1886 number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
1887 number_value = AST_PRES_UNAVAILABLE;
1888 number_priority = 3;
1890 number_screening = id->number.presentation & AST_PRES_NUMBER_TYPE;
1891 number_value = id->number.presentation & AST_PRES_RESTRICTION;
1892 switch (number_value) {
1893 case AST_PRES_RESTRICTED:
1894 number_priority = 0;
1896 case AST_PRES_ALLOWED:
1897 number_priority = 1;
1899 case AST_PRES_UNAVAILABLE:
1900 number_priority = 2;
1903 number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
1904 number_value = AST_PRES_UNAVAILABLE;
1905 number_priority = 3;
1910 /* Select the wining presentation value. */
1911 if (name_priority < number_priority) {
1912 number_value = name_value;
1914 if (number_value == AST_PRES_UNAVAILABLE) {
1915 return AST_PRES_NUMBER_NOT_AVAILABLE;
1918 return number_value | number_screening;
1921 void ast_party_id_invalidate(struct ast_party_id *id)
1924 id->number.valid = 0;
1925 id->subaddress.valid = 0;
1928 void ast_party_id_reset(struct ast_party_id *id)
1930 ast_party_id_free(id);
1931 ast_party_id_init(id);
1934 struct ast_party_id ast_party_id_merge(struct ast_party_id *base, struct ast_party_id *overlay)
1936 struct ast_party_id merged;
1939 if (overlay->name.valid) {
1940 merged.name = overlay->name;
1942 if (overlay->number.valid) {
1943 merged.number = overlay->number;
1945 if (overlay->subaddress.valid) {
1946 merged.subaddress = overlay->subaddress;
1948 /* Note the actual structure is returned and not a pointer to it! */
1952 void ast_party_id_merge_copy(struct ast_party_id *dest, struct ast_party_id *base, struct ast_party_id *overlay)
1954 struct ast_party_id merged;
1956 merged = ast_party_id_merge(base, overlay);
1957 ast_party_id_copy(dest, &merged);
1960 void ast_party_dialed_init(struct ast_party_dialed *init)
1962 init->number.str = NULL;
1963 init->number.plan = 0;/* Unknown */
1964 ast_party_subaddress_init(&init->subaddress);
1965 init->transit_network_select = 0;
1968 void ast_party_dialed_copy(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
1971 /* Don't copy to self */
1975 ast_free(dest->number.str);
1976 dest->number.str = ast_strdup(src->number.str);
1977 dest->number.plan = src->number.plan;
1978 ast_party_subaddress_copy(&dest->subaddress, &src->subaddress);
1979 dest->transit_network_select = src->transit_network_select;
1982 void ast_party_dialed_set_init(struct ast_party_dialed *init, const struct ast_party_dialed *guide)
1984 init->number.str = NULL;
1985 init->number.plan = guide->number.plan;
1986 ast_party_subaddress_set_init(&init->subaddress, &guide->subaddress);
1987 init->transit_network_select = guide->transit_network_select;
1990 void ast_party_dialed_set(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
1992 if (src->number.str && src->number.str != dest->number.str) {
1993 ast_free(dest->number.str);
1994 dest->number.str = ast_strdup(src->number.str);
1996 dest->number.plan = src->number.plan;
1998 ast_party_subaddress_set(&dest->subaddress, &src->subaddress);
2000 dest->transit_network_select = src->transit_network_select;
2003 void ast_party_dialed_free(struct ast_party_dialed *doomed)
2005 ast_free(doomed->number.str);
2006 doomed->number.str = NULL;
2007 ast_party_subaddress_free(&doomed->subaddress);
2010 void ast_party_caller_init(struct ast_party_caller *init)
2012 ast_party_id_init(&init->id);
2013 ast_party_id_init(&init->ani);
2014 ast_party_id_init(&init->priv);
2018 void ast_party_caller_copy(struct ast_party_caller *dest, const struct ast_party_caller *src)
2021 /* Don't copy to self */
2025 ast_party_id_copy(&dest->id, &src->id);
2026 ast_party_id_copy(&dest->ani, &src->ani);
2027 ast_party_id_copy(&dest->priv, &src->priv);
2028 dest->ani2 = src->ani2;
2031 void ast_party_caller_set_init(struct ast_party_caller *init, const struct ast_party_caller *guide)
2033 ast_party_id_set_init(&init->id, &guide->id);
2034 ast_party_id_set_init(&init->ani, &guide->ani);
2035 ast_party_id_set_init(&init->priv, &guide->priv);
2036 init->ani2 = guide->ani2;
2039 void ast_party_caller_set(struct ast_party_caller *dest, const struct ast_party_caller *src, const struct ast_set_party_caller *update)
2041 ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
2042 ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
2043 ast_party_id_set(&dest->priv, &src->priv, update ? &update->priv : NULL);
2044 dest->ani2 = src->ani2;
2047 void ast_party_caller_free(struct ast_party_caller *doomed)
2049 ast_party_id_free(&doomed->id);
2050 ast_party_id_free(&doomed->ani);
2051 ast_party_id_free(&doomed->priv);
2054 void ast_party_connected_line_init(struct ast_party_connected_line *init)
2056 ast_party_id_init(&init->id);
2057 ast_party_id_init(&init->ani);
2058 ast_party_id_init(&init->priv);
2060 init->source = AST_CONNECTED_LINE_UPDATE_SOURCE_UNKNOWN;
2063 void ast_party_connected_line_copy(struct ast_party_connected_line *dest, const struct ast_party_connected_line *src)
2066 /* Don't copy to self */
2070 ast_party_id_copy(&dest->id, &src->id);
2071 ast_party_id_copy(&dest->ani, &src->ani);
2072 ast_party_id_copy(&dest->priv, &src->priv);
2073 dest->ani2 = src->ani2;
2074 dest->source = src->source;
2077 void ast_party_connected_line_set_init(struct ast_party_connected_line *init, const struct ast_party_connected_line *guide)
2079 ast_party_id_set_init(&init->id, &guide->id);
2080 ast_party_id_set_init(&init->ani, &guide->ani);
2081 ast_party_id_set_init(&init->priv, &guide->priv);
2082 init->ani2 = guide->ani2;
2083 init->source = guide->source;
2086 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)
2088 ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
2089 ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
2090 ast_party_id_set(&dest->priv, &src->priv, update ? &update->priv : NULL);
2091 dest->ani2 = src->ani2;
2092 dest->source = src->source;
2095 void ast_party_connected_line_collect_caller(struct ast_party_connected_line *connected, struct ast_party_caller *caller)
2097 connected->id = caller->id;
2098 connected->ani = caller->ani;
2099 connected->priv = caller->priv;
2100 connected->ani2 = caller->ani2;
2101 connected->source = AST_CONNECTED_LINE_UPDATE_SOURCE_UNKNOWN;
2104 void ast_party_connected_line_free(struct ast_party_connected_line *doomed)
2106 ast_party_id_free(&doomed->id);
2107 ast_party_id_free(&doomed->ani);
2108 ast_party_id_free(&doomed->priv);
2111 void ast_party_redirecting_reason_init(struct ast_party_redirecting_reason *init)
2114 init->code = AST_REDIRECTING_REASON_UNKNOWN;
2117 void ast_party_redirecting_reason_copy(struct ast_party_redirecting_reason *dest, const struct ast_party_redirecting_reason *src)
2123 ast_free(dest->str);
2124 dest->str = ast_strdup(src->str);
2125 dest->code = src->code;
2128 void ast_party_redirecting_reason_set_init(struct ast_party_redirecting_reason *init, const struct ast_party_redirecting_reason *guide)
2131 init->code = guide->code;
2134 void ast_party_redirecting_reason_set(struct ast_party_redirecting_reason *dest, const struct ast_party_redirecting_reason *src)
2140 if (src->str && src->str != dest->str) {
2141 ast_free(dest->str);
2142 dest->str = ast_strdup(src->str);
2145 dest->code = src->code;
2148 void ast_party_redirecting_reason_free(struct ast_party_redirecting_reason *doomed)
2150 ast_free(doomed->str);
2154 void ast_party_redirecting_init(struct ast_party_redirecting *init)
2156 ast_party_id_init(&init->orig);
2157 ast_party_id_init(&init->from);
2158 ast_party_id_init(&init->to);
2159 ast_party_id_init(&init->priv_orig);
2160 ast_party_id_init(&init->priv_from);
2161 ast_party_id_init(&init->priv_to);
2162 ast_party_redirecting_reason_init(&init->reason);
2163 ast_party_redirecting_reason_init(&init->orig_reason);
2167 void ast_party_redirecting_copy(struct ast_party_redirecting *dest, const struct ast_party_redirecting *src)
2170 /* Don't copy to self */
2174 ast_party_id_copy(&dest->orig, &src->orig);
2175 ast_party_id_copy(&dest->from, &src->from);
2176 ast_party_id_copy(&dest->to, &src->to);
2177 ast_party_id_copy(&dest->priv_orig, &src->priv_orig);
2178 ast_party_id_copy(&dest->priv_from, &src->priv_from);
2179 ast_party_id_copy(&dest->priv_to, &src->priv_to);
2180 ast_party_redirecting_reason_copy(&dest->reason, &src->reason);
2181 ast_party_redirecting_reason_copy(&dest->orig_reason, &src->orig_reason);
2182 dest->count = src->count;
2185 void ast_party_redirecting_set_init(struct ast_party_redirecting *init, const struct ast_party_redirecting *guide)
2187 ast_party_id_set_init(&init->orig, &guide->orig);
2188 ast_party_id_set_init(&init->from, &guide->from);
2189 ast_party_id_set_init(&init->to, &guide->to);
2190 ast_party_id_set_init(&init->priv_orig, &guide->priv_orig);
2191 ast_party_id_set_init(&init->priv_from, &guide->priv_from);
2192 ast_party_id_set_init(&init->priv_to, &guide->priv_to);
2193 ast_party_redirecting_reason_set_init(&init->reason, &guide->reason);
2194 ast_party_redirecting_reason_set_init(&init->orig_reason, &guide->orig_reason);
2195 init->count = guide->count;
2198 void ast_party_redirecting_set(struct ast_party_redirecting *dest, const struct ast_party_redirecting *src, const struct ast_set_party_redirecting *update)
2200 ast_party_id_set(&dest->orig, &src->orig, update ? &update->orig : NULL);
2201 ast_party_id_set(&dest->from, &src->from, update ? &update->from : NULL);
2202 ast_party_id_set(&dest->to, &src->to, update ? &update->to : NULL);
2203 ast_party_id_set(&dest->priv_orig, &src->priv_orig, update ? &update->priv_orig : NULL);
2204 ast_party_id_set(&dest->priv_from, &src->priv_from, update ? &update->priv_from : NULL);
2205 ast_party_id_set(&dest->priv_to, &src->priv_to, update ? &update->priv_to : NULL);
2206 ast_party_redirecting_reason_set(&dest->reason, &src->reason);
2207 ast_party_redirecting_reason_set(&dest->orig_reason, &src->orig_reason);
2208 dest->count = src->count;
2211 void ast_party_redirecting_free(struct ast_party_redirecting *doomed)
2213 ast_party_id_free(&doomed->orig);
2214 ast_party_id_free(&doomed->from);
2215 ast_party_id_free(&doomed->to);
2216 ast_party_id_free(&doomed->priv_orig);
2217 ast_party_id_free(&doomed->priv_from);
2218 ast_party_id_free(&doomed->priv_to);
2219 ast_party_redirecting_reason_free(&doomed->reason);
2220 ast_party_redirecting_reason_free(&doomed->orig_reason);
2223 /*! \brief Free a channel structure */
2224 static void ast_channel_destructor(void *obj)
2226 struct ast_channel *chan = obj;
2230 struct ast_var_t *vardata;
2231 struct ast_frame *f;
2232 struct varshead *headp;
2233 struct ast_datastore *datastore;
2234 char device_name[AST_CHANNEL_NAME];
2237 /* Stop monitoring */
2238 if (ast_channel_monitor(chan)) {
2239 ast_channel_monitor(chan)->stop(chan, 0);
2242 /* If there is native format music-on-hold state, free it */
2243 if (ast_channel_music_state(chan)) {
2244 ast_moh_cleanup(chan);
2247 ast_pbx_hangup_handler_destroy(chan);
2249 /* Things that may possibly raise Stasis messages shouldn't occur after this point */
2250 ast_set_flag(ast_channel_flags(chan), AST_FLAG_DEAD);
2252 if (ast_channel_internal_is_finalized(chan)) {
2253 /* A channel snapshot should not be in the process of being staged now. */
2254 ast_assert(!ast_test_flag(ast_channel_flags(chan), AST_FLAG_SNAPSHOT_STAGE));
2256 ast_channel_lock(chan);
2257 ast_channel_publish_snapshot(chan);
2258 ast_channel_unlock(chan);
2259 publish_cache_clear(chan);
2262 ast_channel_lock(chan);
2264 /* Get rid of each of the data stores on the channel */
2265 while ((datastore = AST_LIST_REMOVE_HEAD(ast_channel_datastores(chan), entry)))
2266 /* Free the data store */
2267 ast_datastore_free(datastore);
2269 /* While the channel is locked, take the reference to its callid while we tear down the call. */
2270 callid = ast_channel_callid(chan);
2271 ast_channel_callid_cleanup(chan);
2273 ast_channel_unlock(chan);
2275 /* Lock and unlock the channel just to be sure nobody has it locked still
2276 due to a reference that was stored in a datastore. (i.e. app_chanspy) */
2277 ast_channel_lock(chan);
2278 ast_channel_unlock(chan);
2280 if (ast_channel_tech_pvt(chan)) {
2281 ast_log_callid(LOG_WARNING, callid, "Channel '%s' may not have been hung up properly\n", ast_channel_name(chan));
2282 ast_free(ast_channel_tech_pvt(chan));
2285 if (ast_channel_sched(chan)) {
2286 ast_sched_context_destroy(ast_channel_sched(chan));
2289 if (ast_channel_internal_is_finalized(chan)) {
2292 ast_copy_string(device_name, ast_channel_name(chan), sizeof(device_name));
2293 if ((dashptr = strrchr(device_name, '-'))) {
2297 device_name[0] = '\0';
2300 /* Free translators */
2301 if (ast_channel_readtrans(chan))
2302 ast_translator_free_path(ast_channel_readtrans(chan));
2303 if (ast_channel_writetrans(chan))
2304 ast_translator_free_path(ast_channel_writetrans(chan));
2305 if (ast_channel_pbx(chan))
2306 ast_log_callid(LOG_WARNING, callid, "PBX may not have been terminated properly on '%s'\n", ast_channel_name(chan));
2309 ast_channel_set_oldwriteformat(chan, NULL);
2310 ast_channel_set_rawreadformat(chan, NULL);
2311 ast_channel_set_rawwriteformat(chan, NULL);
2312 ast_channel_set_readformat(chan, NULL);
2313 ast_channel_set_writeformat(chan, NULL);
2315 ast_party_dialed_free(ast_channel_dialed(chan));
2316 ast_party_caller_free(ast_channel_caller(chan));
2317 ast_party_connected_line_free(ast_channel_connected(chan));
2318 ast_party_connected_line_free(ast_channel_connected_indicated(chan));
2319 ast_party_redirecting_free(ast_channel_redirecting(chan));
2321 /* Close pipes if appropriate */
2322 ast_channel_internal_alertpipe_close(chan);
2323 if (ast_channel_timer(chan)) {
2324 ast_timer_close(ast_channel_timer(chan));
2325 ast_channel_timer_set(chan, NULL);
2328 for (i = 0; i < AST_MAX_FDS; i++) {
2329 if (ast_channel_internal_epfd_data(chan, i)) {
2330 ast_free(ast_channel_internal_epfd_data(chan, i));
2333 close(ast_channel_epfd(chan));
2335 while ((f = AST_LIST_REMOVE_HEAD(ast_channel_readq(chan), frame_list)))
2338 /* loop over the variables list, freeing all data and deleting list items */
2339 /* no need to lock the list, as the channel is already locked */
2340 headp = ast_channel_varshead(chan);
2341 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
2342 ast_var_delete(vardata);
2344 ast_app_group_discard(chan);
2346 /* Destroy the jitterbuffer */
2347 ast_jb_destroy(chan);
2349 if (ast_channel_cdr(chan)) {
2350 ast_cdr_free(ast_channel_cdr(chan));
2351 ast_channel_cdr_set(chan, NULL);
2354 if (ast_channel_zone(chan)) {
2355 ast_channel_zone_set(chan, ast_tone_zone_unref(ast_channel_zone(chan)));
2358 ast_channel_internal_cleanup(chan);
2360 if (device_name[0]) {
2362 * We have a device name to notify of a new state.
2364 * Queue an unknown state, because, while we know that this particular
2365 * instance is dead, we don't know the state of all other possible
2368 ast_devstate_changed_literal(AST_DEVICE_UNKNOWN, (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DISABLE_DEVSTATE_CACHE) ? AST_DEVSTATE_NOT_CACHABLE : AST_DEVSTATE_CACHABLE), device_name);
2371 ast_channel_nativeformats_set(chan, NULL);
2373 ast_channel_named_callgroups_set(chan, NULL);
2374 ast_channel_named_pickupgroups_set(chan, NULL);
2376 ast_atomic_fetchadd_int(&chancount, -1);
2379 /*! \brief Free a dummy channel structure */
2380 static void ast_dummy_channel_destructor(void *obj)
2382 struct ast_channel *chan = obj;
2383 struct ast_datastore *datastore;
2384 struct ast_var_t *vardata;
2385 struct varshead *headp;
2387 ast_pbx_hangup_handler_destroy(chan);
2389 /* Get rid of each of the data stores on the channel */
2390 while ((datastore = AST_LIST_REMOVE_HEAD(ast_channel_datastores(chan), entry))) {
2391 /* Free the data store */
2392 ast_datastore_free(datastore);
2395 ast_party_dialed_free(ast_channel_dialed(chan));
2396 ast_party_caller_free(ast_channel_caller(chan));
2397 ast_party_connected_line_free(ast_channel_connected(chan));
2398 ast_party_connected_line_free(ast_channel_connected_indicated(chan));
2399 ast_party_redirecting_free(ast_channel_redirecting(chan));
2401 /* loop over the variables list, freeing all data and deleting list items */
2402 /* no need to lock the list, as the channel is already locked */
2403 headp = ast_channel_varshead(chan);
2404 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
2405 ast_var_delete(vardata);
2407 if (ast_channel_cdr(chan)) {
2408 ast_cdr_free(ast_channel_cdr(chan));
2409 ast_channel_cdr_set(chan, NULL);
2412 ast_channel_internal_cleanup(chan);
2415 struct ast_datastore *ast_channel_datastore_alloc(const struct ast_datastore_info *info, const char *uid)
2417 return ast_datastore_alloc(info, uid);
2420 int ast_channel_datastore_free(struct ast_datastore *datastore)
2422 return ast_datastore_free(datastore);
2425 int ast_channel_datastore_inherit(struct ast_channel *from, struct ast_channel *to)
2427 struct ast_datastore *datastore = NULL, *datastore2;
2429 AST_LIST_TRAVERSE(ast_channel_datastores(from), datastore, entry) {
2430 if (datastore->inheritance > 0) {
2431 datastore2 = ast_datastore_alloc(datastore->info, datastore->uid);
2433 datastore2->data = datastore->info->duplicate ? datastore->info->duplicate(datastore->data) : NULL;
2434 datastore2->inheritance = datastore->inheritance == DATASTORE_INHERIT_FOREVER ? DATASTORE_INHERIT_FOREVER : datastore->inheritance - 1;
2435 AST_LIST_INSERT_TAIL(ast_channel_datastores(to), datastore2, entry);
2442 int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
2446 AST_LIST_INSERT_HEAD(ast_channel_datastores(chan), datastore, entry);
2451 int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
2453 return AST_LIST_REMOVE(ast_channel_datastores(chan), datastore, entry) ? 0 : -1;
2456 struct ast_datastore *ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
2458 struct ast_datastore *datastore = NULL;
2463 AST_LIST_TRAVERSE(ast_channel_datastores(chan), datastore, entry) {
2464 if (datastore->info != info) {
2469 /* matched by type only */
2473 if ((datastore->uid != NULL) && !strcasecmp(uid, datastore->uid)) {
2474 /* Matched by type AND uid */
2482 /*! Set the file descriptor on the channel */
2483 void ast_channel_set_fd(struct ast_channel *chan, int which, int fd)
2486 struct epoll_event ev;
2487 struct ast_epoll_data *aed = NULL;
2489 if (ast_channel_fd_isset(chan, which)) {
2490 epoll_ctl(ast_channel_epfd(chan), EPOLL_CTL_DEL, ast_channel_fd(chan, which), &ev);
2491 aed = ast_channel_internal_epfd_data(chan, which);
2494 /* If this new fd is valid, add it to the epoll */
2496 if (!aed && (!(aed = ast_calloc(1, sizeof(*aed)))))
2499 ast_channel_internal_epfd_data_set(chan, which, aed);
2503 ev.events = EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP;
2505 epoll_ctl(ast_channel_epfd(chan), EPOLL_CTL_ADD, fd, &ev);
2507 /* We don't have to keep around this epoll data structure now */
2509 ast_channel_epfd_data_set(chan, which, NULL);
2512 ast_channel_internal_fd_set(chan, which, fd);
2516 /*! Add a channel to an optimized waitfor */
2517 void ast_poll_channel_add(struct ast_channel *chan0, struct ast_channel *chan1)
2520 struct epoll_event ev;
2523 if (ast_channel_epfd(chan0) == -1)
2526 /* Iterate through the file descriptors on chan1, adding them to chan0 */
2527 for (i = 0; i < AST_MAX_FDS; i++) {
2528 if (!ast_channel_fd_isset(chan1, i)) {
2531 ev.events = EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP;
2532 ev.data.ptr = ast_channel_internal_epfd_data(chan1, i);
2533 epoll_ctl(ast_channel_epfd(chan0), EPOLL_CTL_ADD, ast_channel_fd(chan1, i), &ev);
2540 /*! Delete a channel from an optimized waitfor */
2541 void ast_poll_channel_del(struct ast_channel *chan0, struct ast_channel *chan1)
2544 struct epoll_event ev;
2547 if (ast_channel_epfd(chan0) == -1)
2550 for (i = 0; i < AST_MAX_FDS; i++) {
2551 if (!ast_channel_fd_isset(chan1, i)) {
2554 epoll_ctl(ast_channel_epfd(chan0), EPOLL_CTL_DEL, ast_channel_fd(chan1, i), &ev);
2561 void ast_channel_clear_softhangup(struct ast_channel *chan, int flag)
2563 ast_channel_lock(chan);
2565 ast_channel_softhangup_internal_flag_clear(chan, flag);
2567 if (!ast_channel_softhangup_internal_flag(chan)) {
2568 struct ast_frame *fr;
2570 /* If we have completely cleared the softhangup flag,
2571 * then we need to fully abort the hangup process. This requires
2572 * pulling the END_OF_Q frame out of the channel frame queue if it
2573 * still happens to be there. */
2575 fr = AST_LIST_LAST(ast_channel_readq(chan));
2576 if (fr && fr->frametype == AST_FRAME_CONTROL &&
2577 fr->subclass.integer == AST_CONTROL_END_OF_Q) {
2578 AST_LIST_REMOVE(ast_channel_readq(chan), fr, frame_list);
2583 ast_channel_unlock(chan);
2586 /*! \brief Softly hangup a channel, don't lock */
2587 int ast_softhangup_nolock(struct ast_channel *chan, int cause)
2589 ast_debug(1, "Soft-Hanging (%#04x) up channel '%s'\n", (unsigned)cause, ast_channel_name(chan));
2590 /* Inform channel driver that we need to be hung up, if it cares */
2591 ast_channel_softhangup_internal_flag_add(chan, cause);
2592 ast_queue_frame(chan, &ast_null_frame);
2593 /* Interrupt any poll call or such */
2594 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING))
2595 pthread_kill(ast_channel_blocker(chan), SIGURG);
2599 /*! \brief Softly hangup a channel, lock */
2600 int ast_softhangup(struct ast_channel *chan, int cause)
2602 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
2605 ast_channel_lock(chan);
2606 res = ast_softhangup_nolock(chan, cause);
2607 blob = ast_json_pack("{s: i, s: b}",
2610 ast_channel_publish_blob(chan, ast_channel_hangup_request_type(), blob);
2611 ast_channel_unlock(chan);
2616 static void free_translation(struct ast_channel *clonechan)
2618 if (ast_channel_writetrans(clonechan)) {
2619 ast_translator_free_path(ast_channel_writetrans(clonechan));
2621 if (ast_channel_readtrans(clonechan)) {
2622 ast_translator_free_path(ast_channel_readtrans(clonechan));
2624 ast_channel_writetrans_set(clonechan, NULL);
2625 ast_channel_readtrans_set(clonechan, NULL);
2628 void ast_set_hangupsource(struct ast_channel *chan, const char *source, int force)
2630 RAII_VAR(struct ast_channel *, bridge, ast_channel_bridge_peer(chan), ast_channel_cleanup);
2632 ast_channel_lock(chan);
2633 if (force || ast_strlen_zero(ast_channel_hangupsource(chan))) {
2634 ast_channel_hangupsource_set(chan, source);
2636 ast_channel_unlock(chan);
2639 ast_channel_lock(bridge);
2640 if (force || ast_strlen_zero(ast_channel_hangupsource(bridge))) {
2641 ast_channel_hangupsource_set(bridge, source);
2643 ast_channel_unlock(bridge);
2647 int ast_channel_has_audio_frame_or_monitor(struct ast_channel *chan)
2649 return ast_channel_monitor(chan)
2650 || !ast_audiohook_write_list_empty(ast_channel_audiohooks(chan))
2651 || !ast_framehook_list_contains_no_active(ast_channel_framehooks(chan));
2654 int ast_channel_has_hook_requiring_audio(struct ast_channel *chan)
2656 return ast_channel_monitor(chan)
2657 || !ast_audiohook_write_list_empty(ast_channel_audiohooks(chan))
2658 || !ast_framehook_list_contains_no_active_of_type(ast_channel_framehooks(chan), AST_FRAME_VOICE);
2661 static void destroy_hooks(struct ast_channel *chan)
2663 if (ast_channel_audiohooks(chan)) {
2664 ast_audiohook_detach_list(ast_channel_audiohooks(chan));
2665 ast_channel_audiohooks_set(chan, NULL);
2668 ast_framehook_list_destroy(chan);
2671 /*! \brief Hangup a channel */
2672 void ast_hangup(struct ast_channel *chan)
2674 /* Be NULL safe for RAII_VAR() usage. */
2679 ast_autoservice_stop(chan);
2681 ast_channel_lock(chan);
2683 while (ast_channel_masq(chan) || ast_channel_masqr(chan)) {
2684 CHANNEL_DEADLOCK_AVOIDANCE(chan);
2687 /* Mark as a zombie so a masquerade cannot be setup on this channel. */
2688 ast_set_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE);
2690 ast_channel_unlock(chan);
2693 * XXX if running the hangup handlers here causes problems
2694 * because the handlers take too long to execute, we could move
2695 * the meat of this function into another thread. A thread
2696 * where channels go to die.
2698 * If this is done, ast_autoservice_chan_hangup_peer() will no
2701 ast_pbx_hangup_handler_run(chan);
2702 ao2_unlink(channels, chan);
2703 ast_channel_lock(chan);
2705 destroy_hooks(chan);
2707 free_translation(chan);
2708 /* Close audio stream */
2709 if (ast_channel_stream(chan)) {
2710 ast_closestream(ast_channel_stream(chan));
2711 ast_channel_stream_set(chan, NULL);
2713 /* Close video stream */
2714 if (ast_channel_vstream(chan)) {
2715 ast_closestream(ast_channel_vstream(chan));
2716 ast_channel_vstream_set(chan, NULL);
2718 if (ast_channel_sched(chan)) {
2719 ast_sched_context_destroy(ast_channel_sched(chan));
2720 ast_channel_sched_set(chan, NULL);
2723 if (ast_channel_generatordata(chan)) { /* Clear any tone stuff remaining */
2724 if (ast_channel_generator(chan) && ast_channel_generator(chan)->release) {
2725 ast_channel_generator(chan)->release(chan, ast_channel_generatordata(chan));
2728 ast_channel_generatordata_set(chan, NULL);
2729 ast_channel_generator_set(chan, NULL);
2731 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING)) {
2732 ast_log(LOG_WARNING, "Hard hangup called by thread %ld on %s, while fd "
2733 "is blocked by thread %ld in procedure %s! Expect a failure\n",
2734 (long) pthread_self(), ast_channel_name(chan), (long)ast_channel_blocker(chan), ast_channel_blockproc(chan));
2735 ast_assert(ast_test_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING) == 0);
2738 ast_debug(1, "Hanging up channel '%s'\n", ast_channel_name(chan));
2739 if (ast_channel_tech(chan)->hangup) {
2740 ast_channel_tech(chan)->hangup(chan);
2743 ast_channel_unlock(chan);
2747 ast_channel_unref(chan);
2752 * \brief Set channel answered time if not already set.
2755 * \param chan Channel to set answered time.
2759 static void set_channel_answer_time(struct ast_channel *chan)
2761 if (ast_tvzero(ast_channel_answertime(chan))) {
2762 struct timeval answertime;
2764 answertime = ast_tvnow();
2765 ast_channel_answertime_set(chan, &answertime);
2769 int ast_raw_answer(struct ast_channel *chan)
2773 ast_channel_lock(chan);
2775 /* You can't answer an outbound call */
2776 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_OUTGOING)) {
2777 ast_channel_unlock(chan);
2781 /* Stop if we're a zombie or need a soft hangup */
2782 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
2783 ast_channel_unlock(chan);
2788 * Mark when incoming channel answered so we can know how
2789 * long the channel has been up.
2791 set_channel_answer_time(chan);
2793 ast_channel_unlock(chan);
2795 switch (ast_channel_state(chan)) {
2796 case AST_STATE_RINGING:
2797 case AST_STATE_RING:
2798 ast_channel_lock(chan);
2799 if (ast_channel_tech(chan)->answer) {
2800 res = ast_channel_tech(chan)->answer(chan);
2802 ast_setstate(chan, AST_STATE_UP);
2803 ast_channel_unlock(chan);
2811 ast_indicate(chan, -1);
2816 int __ast_answer(struct ast_channel *chan, unsigned int delay)
2819 enum ast_channel_state old_state;
2821 old_state = ast_channel_state(chan);
2822 if ((res = ast_raw_answer(chan))) {
2826 switch (old_state) {
2827 case AST_STATE_RINGING:
2828 case AST_STATE_RING:
2829 /* wait for media to start flowing, but don't wait any longer
2830 * than 'delay' or 500 milliseconds, whichever is longer
2833 AST_LIST_HEAD_NOLOCK(, ast_frame) frames;
2834 struct ast_frame *cur;
2835 struct ast_frame *new_frame;
2836 int timeout_ms = MAX(delay, 500);
2837 unsigned int done = 0;
2838 struct timeval start;
2840 AST_LIST_HEAD_INIT_NOLOCK(&frames);
2842 start = ast_tvnow();
2844 int ms = ast_remaining_ms(start, timeout_ms);
2845 ms = ast_waitfor(chan, ms);
2847 ast_log(LOG_WARNING, "Error condition occurred when polling channel %s for a voice frame: %s\n", ast_channel_name(chan), strerror(errno));
2852 ast_debug(2, "Didn't receive a media frame from %s within %u ms of answering. Continuing anyway\n", ast_channel_name(chan), MAX(delay, 500));
2855 cur = ast_read(chan);
2856 if (!cur || ((cur->frametype == AST_FRAME_CONTROL) &&
2857 (cur->subclass.integer == AST_CONTROL_HANGUP))) {
2862 ast_debug(2, "Hangup of channel %s detected in answer routine\n", ast_channel_name(chan));
2866 if ((new_frame = ast_frisolate(cur)) != cur) {
2870 AST_LIST_INSERT_HEAD(&frames, new_frame, frame_list);
2872 /* if a specific delay period was requested, continue
2873 * until that delay has passed. don't stop just because
2874 * incoming media has arrived.
2880 switch (new_frame->frametype) {
2881 /* all of these frametypes qualify as 'media' */
2882 case AST_FRAME_VOICE:
2883 case AST_FRAME_VIDEO:
2884 case AST_FRAME_TEXT:
2885 case AST_FRAME_DTMF_BEGIN:
2886 case AST_FRAME_DTMF_END:
2887 case AST_FRAME_IMAGE:
2888 case AST_FRAME_HTML:
2889 case AST_FRAME_MODEM:
2892 case AST_FRAME_CONTROL:
2894 case AST_FRAME_BRIDGE_ACTION:
2895 case AST_FRAME_BRIDGE_ACTION_SYNC:
2896 case AST_FRAME_NULL:
2906 ast_channel_lock(chan);
2907 while ((cur = AST_LIST_REMOVE_HEAD(&frames, frame_list))) {
2909 ast_queue_frame_head(chan, cur);
2913 ast_channel_unlock(chan);
2923 int ast_answer(struct ast_channel *chan)
2925 return __ast_answer(chan, 0);
2928 inline int ast_auto_answer(struct ast_channel *chan)
2930 if (ast_channel_state(chan) == AST_STATE_UP) {
2931 /* Already answered */
2934 return ast_answer(chan);
2937 int ast_channel_get_duration(struct ast_channel *chan)
2939 ast_assert(NULL != chan);
2941 if (ast_tvzero(ast_channel_creationtime(chan))) {
2944 return (ast_tvdiff_ms(ast_tvnow(), ast_channel_creationtime(chan)) / 1000);
2947 int ast_channel_get_up_time(struct ast_channel *chan)
2949 ast_assert(NULL != chan);
2951 if (ast_tvzero(ast_channel_answertime(chan))) {
2954 return (ast_tvdiff_ms(ast_tvnow(), ast_channel_answertime(chan)) / 1000);
2957 static void deactivate_generator_nolock(struct ast_channel *chan)
2959 if (ast_channel_generatordata(chan)) {
2960 struct ast_generator *generator = ast_channel_generator(chan);
2962 if (generator && generator->release) {
2963 generator->release(chan, ast_channel_generatordata(chan));
2965 ast_channel_generatordata_set(chan, NULL);
2966 ast_channel_generator_set(chan, NULL);
2967 ast_channel_set_fd(chan, AST_GENERATOR_FD, -1);
2968 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_WRITE_INT);
2969 ast_settimeout(chan, 0, NULL, NULL);
2973 void ast_deactivate_generator(struct ast_channel *chan)
2975 ast_channel_lock(chan);
2976 deactivate_generator_nolock(chan);
2977 ast_channel_unlock(chan);
2980 static void generator_write_format_change(struct ast_channel *chan)
2982 struct ast_generator *generator;
2984 ast_channel_lock(chan);
2985 generator = ast_channel_generator(chan);
2986 if (generator && generator->write_format_change) {
2987 generator->write_format_change(chan, ast_channel_generatordata(chan));
2989 ast_channel_unlock(chan);
2992 static int generator_force(const void *data)
2994 /* Called if generator doesn't have data */
2997 int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples) = NULL;
2998 struct ast_channel *chan = (struct ast_channel *)data;
3000 ast_channel_lock(chan);
3001 tmp = ast_channel_generatordata(chan);
3002 ast_channel_generatordata_set(chan, NULL);
3003 if (ast_channel_generator(chan))
3004 generate = ast_channel_generator(chan)->generate;
3005 ast_channel_unlock(chan);
3007 if (!tmp || !generate) {
3011 res = generate(chan, tmp, 0, ast_format_get_sample_rate(ast_channel_writeformat(chan)) / 50);
3013 ast_channel_lock(chan);
3014 if (ast_channel_generator(chan) && generate == ast_channel_generator(chan)->generate) {
3015 ast_channel_generatordata_set(chan, tmp);
3017 ast_channel_unlock(chan);
3020 ast_debug(1, "Auto-deactivating generator\n");
3021 ast_deactivate_generator(chan);
3027 int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen, void *params)
3030 void *generatordata = NULL;
3032 ast_channel_lock(chan);
3033 if (ast_channel_generatordata(chan)) {
3034 struct ast_generator *generator_old = ast_channel_generator(chan);
3036 if (generator_old && generator_old->release) {
3037 generator_old->release(chan, ast_channel_generatordata(chan));
3040 if (gen->alloc && !(generatordata = gen->alloc(chan, params))) {
3043 ast_channel_generatordata_set(chan, generatordata);
3045 ast_settimeout(chan, 50, generator_force, chan);
3046 ast_channel_generator_set(chan, gen);
3048 ast_channel_unlock(chan);
3055 /*! \brief Wait for x amount of time on a file descriptor to have input. */
3056 int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception)
3059 ast_waitfor_nandfds(NULL, 0, fds, n, exception, &winner, ms);
3063 /*! \brief Wait for x amount of time on a file descriptor to have input. */
3065 static struct ast_channel *ast_waitfor_nandfds_classic(struct ast_channel **c, int n, int *fds, int nfds,
3066 int *exception, int *outfd, int *ms)
3068 struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
3069 int *exception, int *outfd, int *ms)
3072 struct timeval start = { 0 , 0 };
3073 struct pollfd *pfds = NULL;
3078 struct timeval now = { 0, 0 };
3079 struct timeval whentohangup = { 0, 0 }, diff;
3080 struct ast_channel *winner = NULL;
3093 if ((sz = n * AST_MAX_FDS + nfds)) {
3094 pfds = ast_alloca(sizeof(*pfds) * sz);
3095 fdmap = ast_alloca(sizeof(*fdmap) * sz);
3097 /* nothing to allocate and no FDs to check */
3101 for (x = 0; x < n; x++) {
3102 ast_channel_lock(c[x]);
3103 if (!ast_tvzero(*ast_channel_whentohangup(c[x]))) {
3104 if (ast_tvzero(whentohangup))
3106 diff = ast_tvsub(*ast_channel_whentohangup(c[x]), now);
3107 if (diff.tv_sec < 0 || ast_tvzero(diff)) {
3108 ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(c[x]));
3109 /* Should already be hungup */
3110 ast_channel_softhangup_internal_flag_add(c[x], AST_SOFTHANGUP_TIMEOUT);
3111 ast_channel_unlock(c[x]);
3114 if (ast_tvzero(whentohangup) || ast_tvcmp(diff, whentohangup) < 0)
3115 whentohangup = diff;
3117 ast_channel_unlock(c[x]);
3119 /* Wait full interval */
3121 /* INT_MAX, not LONG_MAX, because it matters on 64-bit */
3122 if (!ast_tvzero(whentohangup) && whentohangup.tv_sec < INT_MAX / 1000) {
3123 rms = whentohangup.tv_sec * 1000 + whentohangup.tv_usec / 1000; /* timeout in milliseconds */
3124 if (*ms >= 0 && *ms < rms) { /* original *ms still smaller */
3127 } else if (!ast_tvzero(whentohangup) && rms < 0) {
3128 /* Tiny corner case... call would need to last >24 days */
3132 * Build the pollfd array, putting the channels' fds first,
3133 * followed by individual fds. Order is important because
3134 * individual fd's must have priority over channel fds.
3137 for (x = 0; x < n; x++) {
3138 for (y = 0; y < AST_MAX_FDS; y++) {
3139 fdmap[max].fdno = y; /* fd y is linked to this pfds */
3140 fdmap[max].chan = x; /* channel x is linked to this pfds */
3141 max += ast_add_fd(&pfds[max], ast_channel_fd(c[x], y));
3143 CHECK_BLOCKING(c[x]);
3145 /* Add the individual fds */
3146 for (x = 0; x < nfds; x++) {
3147 fdmap[max].chan = -1;
3148 max += ast_add_fd(&pfds[max], fds[x]);
3152 start = ast_tvnow();
3155 if (sizeof(int) == 4) { /* XXX fix timeout > 600000 on linux x86-32 */
3158 if (kbrms > 600000) {
3161 res = ast_poll(pfds, max, kbrms);
3165 } while (!res && (rms > 0));
3167 res = ast_poll(pfds, max, rms);
3169 for (x = 0; x < n; x++) {
3170 ast_clear_flag(ast_channel_flags(c[x]), AST_FLAG_BLOCKING);
3172 if (res < 0) { /* Simulate a timeout if we were interrupted */
3173 if (errno != EINTR) {
3178 if (!ast_tvzero(whentohangup)) { /* if we have a timeout, check who expired */
3180 for (x = 0; x < n; x++) {
3181 if (!ast_tvzero(*ast_channel_whentohangup(c[x])) && ast_tvcmp(*ast_channel_whentohangup(c[x]), now) <= 0) {
3182 ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(c[x]));
3183 ast_channel_softhangup_internal_flag_add(c[x], AST_SOFTHANGUP_TIMEOUT);
3184 if (winner == NULL) {
3190 if (res == 0) { /* no fd ready, reset timeout and done */
3191 *ms = 0; /* XXX use 0 since we may not have an exact timeout. */
3195 * Then check if any channel or fd has a pending event.
3196 * Remember to check channels first and fds last, as they
3197 * must have priority on setting 'winner'
3199 for (x = 0; x < max; x++) {
3200 res = pfds[x].revents;
3204 if (fdmap[x].chan >= 0) { /* this is a channel */
3205 winner = c[fdmap[x].chan]; /* override previous winners */
3206 if (res & POLLPRI) {
3207 ast_set_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3209 ast_clear_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3211 ast_channel_fdno_set(winner, fdmap[x].fdno);
3212 } else { /* this is an fd */
3214 *outfd = pfds[x].fd;
3217 *exception = (res & POLLPRI) ? -1 : 0;
3223 *ms -= ast_tvdiff_ms(ast_tvnow(), start);
3232 static struct ast_channel *ast_waitfor_nandfds_simple(struct ast_channel *chan, int *ms)
3234 struct timeval start = { 0 , 0 };
3236 struct epoll_event ev[1];
3237 long diff, rms = *ms;
3238 struct ast_channel *winner = NULL;
3239 struct ast_epoll_data *aed = NULL;
3241 ast_channel_lock(chan);
3242 /* Figure out their timeout */
3243 if (!ast_tvzero(*ast_channel_whentohangup(chan))) {
3244 if ((diff = ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow())) < 0) {
3245 /* They should already be hungup! */
3246 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_TIMEOUT);
3247 ast_channel_unlock(chan);
3250 /* If this value is smaller then the current one... make it priority */
3256 ast_channel_unlock(chan);
3258 /* Time to make this channel block... */
3259 CHECK_BLOCKING(chan);
3262 start = ast_tvnow();
3265 /* We don't have to add any file descriptors... they are already added, we just have to wait! */
3266 res = epoll_wait(ast_channel_epfd(chan), ev, 1, rms);
3269 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_BLOCKING);
3271 /* Simulate a timeout if we were interrupted */
3273 if (errno != EINTR) {
3279 /* If this channel has a timeout see if it expired */
3280 if (!ast_tvzero(*ast_channel_whentohangup(chan))) {
3281 if (ast_tvdiff_ms(ast_tvnow(), *ast_channel_whentohangup(chan)) >= 0) {
3282 ast_channel_softhangup_internal_flag_add(chan, AST_SOFTHANGUP_TIMEOUT);
3287 /* No fd ready, reset timeout and be done for now */
3293 /* See what events are pending */
3294 aed = ev[0].data.ptr;
3295 ast_channel_fdno_set(chan, aed->which);
3296 if (ev[0].events & EPOLLPRI) {
3297 ast_set_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3299 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3303 *ms -= ast_tvdiff_ms(ast_tvnow(), start);
3312 static struct ast_channel *ast_waitfor_nandfds_complex(struct ast_channel **c, int n, int *ms)
3314 struct timeval start = { 0 , 0 };
3316 struct epoll_event ev[25] = { { 0, } };
3317 struct timeval now = { 0, 0 };
3318 long whentohangup = 0, diff = 0, rms = *ms;
3319 struct ast_channel *winner = NULL;
3321 for (i = 0; i < n; i++) {
3322 ast_channel_lock(c[i]);
3323 if (!ast_tvzero(*ast_channel_whentohangup(c[i]))) {
3324 if (whentohangup == 0) {
3327 if ((diff = ast_tvdiff_ms(*ast_channel_whentohangup(c[i]), now)) < 0) {
3328 ast_channel_softhangup_internal_flag_add(c[i], AST_SOFTHANGUP_TIMEOUT);
3329 ast_channel_unlock(c[i]);
3332 if (!whentohangup || whentohangup > diff) {
3333 whentohangup = diff;
3336 ast_channel_unlock(c[i]);
3337 CHECK_BLOCKING(c[i]);
3343 if (*ms >= 0 && *ms < rms) {
3349 start = ast_tvnow();
3352 res = epoll_wait(ast_channel_epfd(c[0]), ev, 25, rms);
3354 for (i = 0; i < n; i++) {
3355 ast_clear_flag(ast_channel_flags(c[i]), AST_FLAG_BLOCKING);
3359 if (errno != EINTR) {
3367 for (i = 0; i < n; i++) {
3368 if (!ast_tvzero(*ast_channel_whentohangup(c[i])) && ast_tvdiff_ms(now, *ast_channel_whentohangup(c[i])) >= 0) {
3369 ast_channel_softhangup_internal_flag_add(c[i], AST_SOFTHANGUP_TIMEOUT);
3382 for (i = 0; i < res; i++) {
3383 struct ast_epoll_data *aed = ev[i].data.ptr;
3385 if (!ev[i].events || !aed) {
3390 if (ev[i].events & EPOLLPRI) {
3391 ast_set_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3393 ast_clear_flag(ast_channel_flags(winner), AST_FLAG_EXCEPTION);
3395 ast_channel_fdno_set(winner, aed->which);
3399 *ms -= ast_tvdiff_ms(ast_tvnow(), start);
3408 struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
3409 int *exception, int *outfd, int *ms)
3411 /* Clear all provided values in one place. */
3419 /* If no epoll file descriptor is available resort to classic nandfds */
3420 if (!n || nfds || ast_channel_epfd(c[0]) == -1) {
3421 return ast_waitfor_nandfds_classic(c, n, fds, nfds, exception, outfd, ms);
3422 } else if (!nfds && n == 1) {
3423 return ast_waitfor_nandfds_simple(c[0], ms);
3425 return ast_waitfor_nandfds_complex(c, n, ms);
3430 struct ast_channel *ast_waitfor_n(struct ast_channel **c, int n, int *ms)
3432 return ast_waitfor_nandfds(c, n, NULL, 0, NULL, NULL, ms);
3435 int ast_waitfor(struct ast_channel *c, int ms)
3440 ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
3443 ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
3448 int ast_waitfordigit(struct ast_channel *c, int ms)
3450 return ast_waitfordigit_full(c, ms, -1, -1);
3453 int ast_settimeout(struct ast_channel *c, unsigned int rate, int (*func)(const void *data), void *data)
3455 return ast_settimeout_full(c, rate, func, data, 0);
3458 int ast_settimeout_full(struct ast_channel *c, unsigned int rate, int (*func)(const void *data), void *data, unsigned int is_ao2_obj)
3461 unsigned int real_rate = rate, max_rate;
3463 ast_channel_lock(c);
3465 if (ast_channel_timingfd(c) == -1) {
3466 ast_channel_unlock(c);
3475 if (rate && rate > (max_rate = ast_timer_get_max_rate(ast_channel_timer(c)))) {
3476 real_rate = max_rate;
3479 ast_debug(1, "Scheduling timer at (%u requested / %u actual) timer ticks per second\n", rate, real_rate);
3481 res = ast_timer_set_rate(ast_channel_timer(c), real_rate);
3483 if (ast_channel_timingdata(c) && ast_test_flag(ast_channel_flags(c), AST_FLAG_TIMINGDATA_IS_AO2_OBJ)) {
3484 ao2_ref(ast_channel_timingdata(c), -1);
3487 ast_channel_timingfunc_set(c, func);
3488 ast_channel_timingdata_set(c, data);
3490 if (data && is_ao2_obj) {
3492 ast_set_flag(ast_channel_flags(c), AST_FLAG_TIMINGDATA_IS_AO2_OBJ);
3494 ast_clear_flag(ast_channel_flags(c), AST_FLAG_TIMINGDATA_IS_AO2_OBJ);
3497 if (func == NULL && rate == 0 && ast_channel_fdno(c) == AST_TIMING_FD) {
3498 /* Clearing the timing func and setting the rate to 0
3499 * means that we don't want to be reading from the timingfd
3500 * any more. Setting c->fdno to -1 means we won't have any
3501 * errant reads from the timingfd, meaning we won't potentially
3502 * miss any important frames.
3504 ast_channel_fdno_set(c, -1);
3507 ast_channel_unlock(c);
3512 int ast_waitfordigit_full(struct ast_channel *c, int timeout_ms, int audiofd, int cmdfd)
3514 struct timeval start = ast_tvnow();
3517 /* Stop if we're a zombie or need a soft hangup */
3518 if (ast_test_flag(ast_channel_flags(c), AST_FLAG_ZOMBIE) || ast_check_hangup(c))
3521 /* Only look for the end of DTMF, don't bother with the beginning and don't emulate things */
3522 ast_set_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3524 /* Wait for a digit, no more than timeout_ms milliseconds total.
3525 * Or, wait indefinitely if timeout_ms is <0.
3527 while ((ms = ast_remaining_ms(start, timeout_ms))) {
3528 struct ast_channel *rchan;
3532 /* While ast_waitfor_nandfds tries to help by reducing the timeout by how much was waited,
3533 * it is unhelpful if it waited less than a millisecond.
3535 rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
3537 if (!rchan && outfd < 0 && ms) {
3538 if (errno == 0 || errno == EINTR)
3540 ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
3541 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3543 } else if (outfd > -1) {
3544 /* The FD we were watching has something waiting */
3545 ast_log(LOG_WARNING, "The FD we were waiting for has something waiting. Waitfordigit returning numeric 1\n");
3546 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3550 struct ast_frame *f = ast_read(c);
3554 switch (f->frametype) {
3555 case AST_FRAME_DTMF_BEGIN:
3557 case AST_FRAME_DTMF_END:
3558 res = f->subclass.integer;
3560 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3562 case AST_FRAME_CONTROL:
3563 switch (f->subclass.integer) {
3564 case AST_CONTROL_HANGUP:
3566 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3568 case AST_CONTROL_STREAM_STOP:
3569 case AST_CONTROL_STREAM_SUSPEND:
3570 case AST_CONTROL_STREAM_RESTART:
3571 case AST_CONTROL_STREAM_REVERSE:
3572 case AST_CONTROL_STREAM_FORWARD:
3573 /* Fall-through and treat as if it were a DTMF signal. Items
3574 * that perform stream control will handle this. */
3575 res = f->subclass.integer;
3577 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3579 case AST_CONTROL_PVT_CAUSE_CODE:
3580 case AST_CONTROL_RINGING:
3581 case AST_CONTROL_ANSWER:
3582 case AST_CONTROL_SRCUPDATE:
3583 case AST_CONTROL_SRCCHANGE:
3584 case AST_CONTROL_CONNECTED_LINE:
3585 case AST_CONTROL_REDIRECTING:
3586 case AST_CONTROL_UPDATE_RTP_PEER:
3587 case AST_CONTROL_HOLD:
3588 case AST_CONTROL_UNHOLD:
3593 ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", f->subclass.integer);
3597 case AST_FRAME_VOICE:
3598 /* Write audio if appropriate */
3600 if (write(audiofd, f->data.ptr, f->datalen) < 0) {
3601 ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
3612 ast_clear_flag(ast_channel_flags(c), AST_FLAG_END_DTMF_ONLY);
3614 return 0; /* Time is up */
3617 enum DtmfDirection {
3622 static const char *dtmf_direction_to_string(enum DtmfDirection direction)
3624 switch (direction) {
3634 static void send_dtmf_begin_event(struct ast_channel *chan,
3635 enum DtmfDirection direction, const char digit)
3637 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
3638 char digit_str[] = { digit, '\0' };
3640 blob = ast_json_pack("{ s: s, s: s }",
3642 "direction", dtmf_direction_to_string(direction));
3647 ast_channel_publish_cached_blob(chan, ast_channel_dtmf_begin_type(), blob);
3650 static void send_dtmf_end_event(struct ast_channel *chan,
3651 enum DtmfDirection direction, const char digit, long duration_ms)
3653 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
3654 char digit_str[] = { digit, '\0' };
3656 blob = ast_json_pack("{ s: s, s: s, s: i }",
3658 "direction", dtmf_direction_to_string(direction),
3659 "duration_ms", duration_ms);
3664 ast_channel_publish_cached_blob(chan, ast_channel_dtmf_end_type(), blob);
3667 static void ast_read_generator_actions(struct ast_channel *chan, struct ast_frame *f)
3669 struct ast_generator *generator;
3674 generator = ast_channel_generator(chan);
3676 || !generator->generate
3677 || f->frametype != AST_FRAME_VOICE
3678 || !ast_channel_generatordata(chan)
3679 || ast_channel_timingfunc(chan)) {
3684 * We must generate frames in phase locked mode since
3685 * we have no internal timer available.
3687 if (ast_format_cmp(f->subclass.format, ast_channel_writeformat(chan)) == AST_FORMAT_CMP_NOT_EQUAL) {
3689 factor = ((float) ast_format_get_sample_rate(ast_channel_writeformat(chan))) / ((float) ast_format_get_sample_rate(f->subclass.format));
3690 samples = (int) (((float) f->samples) * factor);
3692 samples = f->samples;
3695 gendata = ast_channel_generatordata(chan);
3696 ast_channel_generatordata_set(chan, NULL); /* reset, to let writes go through */
3699 * This unlock is here based on two assumptions that hold true at
3700 * this point in the code. 1) this function is only called from
3701 * within __ast_read() and 2) all generators call ast_write() in
3702 * their generate callback.
3704 * The reason this is added is so that when ast_write is called,
3705 * the lock that occurs there will not recursively lock the
3706 * channel. Doing this will allow deadlock avoidance to work in
3709 ast_channel_unlock(chan);
3710 res = generator->generate(chan, gendata, f->datalen, samples);
3711 ast_channel_lock(chan);
3712 if (generator == ast_channel_generator(chan)) {
3713 ast_channel_generatordata_set(chan, gendata);
3715 ast_debug(1, "Auto-deactivating generator\n");
3716 ast_deactivate_generator(chan);
3721 static inline void queue_dtmf_readq(struct ast_channel *chan, struct ast_frame *f)
3723 struct ast_frame *fr = ast_channel_dtmff(chan);
3725 fr->frametype = AST_FRAME_DTMF_END;
3726 fr->subclass.integer = f->subclass.integer;
3729 /* The only time this function will be called is for a frame that just came
3730 * out of the channel driver. So, we want to stick it on the tail of the
3733 ast_queue_frame(chan, fr);
3737 * \brief Determine whether or not we should ignore DTMF in the readq
3739 static inline int should_skip_dtmf(struct ast_channel *chan)
3741 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_DTMF | AST_FLAG_EMULATE_DTMF)) {
3742 /* We're in the middle of emulating a digit, or DTMF has been
3743 * explicitly deferred. Skip this digit, then. */
3747 if (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
3748 ast_tvdiff_ms(ast_tvnow(), *ast_channel_dtmf_tv(chan)) < AST_MIN_DTMF_GAP) {
3749 /* We're not in the middle of a digit, but it hasn't been long enough
3750 * since the last digit, so we'll have to skip DTMF for now. */
3758 * \brief calculates the number of samples to jump forward with in a monitor stream.
3760 * \note When using ast_seekstream() with the read and write streams of a monitor,
3761 * the number of samples to seek forward must be of the same sample rate as the stream
3762 * or else the jump will not be calculated correctly.
3764 * \retval number of samples to seek forward after rate conversion.
3766 static inline int calc_monitor_jump(int samples, int sample_rate, int seek_rate)
3768 int diff = sample_rate - seek_rate;
3771 samples = samples / (float) (sample_rate / seek_rate);
3772 } else if (diff < 0) {
3773 samples = samples * (float) (seek_rate / sample_rate);
3779 static struct ast_frame *__ast_read(struct ast_channel *chan, int dropaudio)
3781 struct ast_frame *f = NULL; /* the return value */
3785 /* this function is very long so make sure there is only one return
3786 * point at the end (there are only two exceptions to this).
3788 ast_channel_lock(chan);
3790 /* Stop if we're a zombie or need a soft hangup */
3791 if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE) || ast_check_hangup(chan)) {
3792 if (ast_channel_generator(chan))
3793 ast_deactivate_generator(chan);
3796 * It is possible for chan->_softhangup to be set and there
3797 * still be control frames that need to be read. Instead of
3798 * just going to 'done' in the case of ast_check_hangup(), we
3799 * need to queue the end-of-Q frame so that it can mark the end
3800 * of the read queue. If there are frames to be read,
3801 * ast_queue_control() will be called repeatedly, but will only
3802 * queue the first end-of-Q frame.
3804 if (ast_channel_softhangup_internal_flag(chan)) {
3805 ast_queue_control(chan, AST_CONTROL_END_OF_Q);
3812 * The ast_waitfor() code records which of the channel's file
3813 * descriptors reported that data is available. In theory,
3814 * ast_read() should only be called after ast_waitfor() reports
3815 * that a channel has data available for reading. However,
3816 * there still may be some edge cases throughout the code where
3817 * ast_read() is called improperly. This can potentially cause
3818 * problems, so if this is a developer build, make a lot of
3819 * noise if this happens so that it can be addressed.
3821 * One of the potential problems is blocking on a dead channel.
3823 if (ast_channel_fdno(chan) == -1) {
3825 "ast_read() on chan '%s' called with no recorded file descriptor.\n",
3826 ast_channel_name(chan));
3831 prestate = ast_channel_state(chan);
3833 if (ast_channel_timingfd(chan) > -1 && ast_channel_fdno(chan) == AST_TIMING_FD) {
3834 enum ast_timer_event res;
3836 ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
3838 res = ast_timer_get_event(ast_channel_timer(chan));
3841 case AST_TIMING_EVENT_EXPIRED:
3842 if (ast_timer_ack(ast_channel_timer(chan), 1) < 0) {
3843 ast_log(LOG_ERROR, "Failed to acknoweldge timer in ast_read\n");
3847 if (ast_channel_timingfunc(chan)) {
3848 /* save a copy of func/data before unlocking the channel */
3849 ast_timing_func_t func = ast_channel_timingfunc(chan);
3850 void *data = ast_channel_timingdata(chan);
3852 if (data && ast_test_flag(ast_channel_flags(chan), AST_FLAG_TIMINGDATA_IS_AO2_OBJ)) {
3856 ast_channel_fdno_set(chan, -1);
3857 ast_channel_unlock(chan);
3863 ast_timer_set_rate(ast_channel_timer(chan), 0);
3864 ast_channel_fdno_set(chan, -1);
3865 ast_channel_unlock(chan);
3868 /* cannot 'goto done' because the channel is already unlocked */
3869 return &ast_null_frame;
3871 case AST_TIMING_EVENT_CONTINUOUS:
3872 if (AST_LIST_EMPTY(ast_channel_readq(chan)) ||
3873 !AST_LIST_NEXT(AST_LIST_FIRST(ast_channel_readq(chan)), frame_list)) {
3874 ast_timer_disable_continuous(ast_channel_timer(chan));
3879 } else if (ast_channel_fd_isset(chan, AST_GENERATOR_FD) && ast_channel_fdno(chan) == AST_GENERATOR_FD) {
3880 /* if the AST_GENERATOR_FD is set, call the generator with args
3881 * set to -1 so it can do whatever it needs to.
3883 void *tmp = ast_channel_generatordata(chan);
3884 ast_channel_generatordata_set(chan, NULL); /* reset to let ast_write get through */
3885 ast_channel_generator(chan)->generate(chan, tmp, -1, -1);
3886 ast_channel_generatordata_set(chan, tmp);
3887 f = &ast_null_frame;
3888 ast_channel_fdno_set(chan, -1);
3890 } else if (ast_channel_fd_isset(chan, AST_JITTERBUFFER_FD) && ast_channel_fdno(chan) == AST_JITTERBUFFER_FD) {