Asterisk - The Open Source Telephony Project GIT-master-5782b03
channel.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2006, Digium, Inc.
5 *
6 * Mark Spencer <markster@digium.com>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19/*! \file
20 *
21 * \brief Channel Management
22 *
23 * \author Mark Spencer <markster@digium.com>
24 */
25
26/*** MODULEINFO
27 <support_level>core</support_level>
28 ***/
29
30#include "asterisk.h"
31
32#include "asterisk/_private.h"
33
34#include <sys/time.h>
35#include <signal.h>
36#include <math.h>
37
38#include "asterisk/paths.h" /* use ast_config_AST_SYSTEM_NAME */
39
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"
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"
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"
63#include "asterisk/audiohook.h"
64#include "asterisk/framehook.h"
65#include "asterisk/timing.h"
66#include "asterisk/autochan.h"
70#include "asterisk/features.h"
71#include "asterisk/bridge.h"
72#include "asterisk/test.h"
75#include "asterisk/stream.h"
76#include "asterisk/message.h"
77
78/*** DOCUMENTATION
79 ***/
80
81#if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
82#if defined(HAVE_PRI)
83#include "libpri.h"
84#endif /* defined(HAVE_PRI) */
85#endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
86
87/* uncomment if you have problems with 'monitoring' synchronized files */
88#if 0
89#define MONITOR_CONSTANT_DELAY
90#define MONITOR_DELAY 150 * 8 /*!< 150 ms of MONITORING DELAY */
91#endif
92
93static int chancount;
94
95unsigned long global_fin, global_fout;
96
98#define STATE2STR_BUFSIZE 32
99
100/*! Default amount of time to use when emulating a DTMF digit as a begin and end
101 * 100ms */
102#define AST_DEFAULT_EMULATE_DTMF_DURATION 100
103
104/*! Default amount of time to use when emulating an MF digit as a begin and end
105 * 55ms */
106#define DEFAULT_EMULATE_MF_DURATION 55
107
108#define DEFAULT_AMA_FLAGS AST_AMA_DOCUMENTATION
109
110/*! Minimum amount of time between the end of the last digit and the beginning
111 * of a new one - 45ms */
112#define AST_MIN_DTMF_GAP 45
113
114/*! \brief List of channel drivers */
115struct chanlist {
116 const struct ast_channel_tech *tech;
118};
119
120/*! \brief the list of registered channel types */
122
123/*! \brief All active channels on the system */
124static struct ao2_container *channels;
125
126/*! \brief map AST_CAUSE's to readable string representations
127 *
128 * \ref causes.h
129*/
131 int cause;
132 const char *name;
133 const char *desc;
134};
135
136static const struct causes_map causes[] = {
137 { AST_CAUSE_UNALLOCATED, "UNALLOCATED", "Unallocated (unassigned) number" },
138 { AST_CAUSE_NO_ROUTE_TRANSIT_NET, "NO_ROUTE_TRANSIT_NET", "No route to specified transmit network" },
139 { AST_CAUSE_NO_ROUTE_DESTINATION, "NO_ROUTE_DESTINATION", "No route to destination" },
140 { AST_CAUSE_MISDIALLED_TRUNK_PREFIX, "MISDIALLED_TRUNK_PREFIX", "Misdialed trunk prefix" },
141 { AST_CAUSE_CHANNEL_UNACCEPTABLE, "CHANNEL_UNACCEPTABLE", "Channel unacceptable" },
142 { AST_CAUSE_CALL_AWARDED_DELIVERED, "CALL_AWARDED_DELIVERED", "Call awarded and being delivered in an established channel" },
143 { AST_CAUSE_PRE_EMPTED, "PRE_EMPTED", "Pre-empted" },
144 { AST_CAUSE_NUMBER_PORTED_NOT_HERE, "NUMBER_PORTED_NOT_HERE", "Number ported elsewhere" },
145 { AST_CAUSE_NORMAL_CLEARING, "NORMAL_CLEARING", "Normal Clearing" },
146 { AST_CAUSE_USER_BUSY, "USER_BUSY", "User busy" },
147 { AST_CAUSE_NO_USER_RESPONSE, "NO_USER_RESPONSE", "No user responding" },
148 { AST_CAUSE_NO_ANSWER, "NO_ANSWER", "User alerting, no answer" },
149 { AST_CAUSE_SUBSCRIBER_ABSENT, "SUBSCRIBER_ABSENT", "Subscriber absent" },
150 { AST_CAUSE_CALL_REJECTED, "CALL_REJECTED", "Call Rejected" },
151 { AST_CAUSE_NUMBER_CHANGED, "NUMBER_CHANGED", "Number changed" },
152 { AST_CAUSE_REDIRECTED_TO_NEW_DESTINATION, "REDIRECTED_TO_NEW_DESTINATION", "Redirected to new destination" },
153 { AST_CAUSE_ANSWERED_ELSEWHERE, "ANSWERED_ELSEWHERE", "Answered elsewhere" },
154 { AST_CAUSE_DESTINATION_OUT_OF_ORDER, "DESTINATION_OUT_OF_ORDER", "Destination out of order" },
155 { AST_CAUSE_INVALID_NUMBER_FORMAT, "INVALID_NUMBER_FORMAT", "Invalid number format" },
156 { AST_CAUSE_FACILITY_REJECTED, "FACILITY_REJECTED", "Facility rejected" },
157 { AST_CAUSE_RESPONSE_TO_STATUS_ENQUIRY, "RESPONSE_TO_STATUS_ENQUIRY", "Response to STATus ENQuiry" },
158 { AST_CAUSE_NORMAL_UNSPECIFIED, "NORMAL_UNSPECIFIED", "Normal, unspecified" },
159 { AST_CAUSE_NORMAL_CIRCUIT_CONGESTION, "NORMAL_CIRCUIT_CONGESTION", "Circuit/channel congestion" },
160 { AST_CAUSE_NETWORK_OUT_OF_ORDER, "NETWORK_OUT_OF_ORDER", "Network out of order" },
161 { AST_CAUSE_NORMAL_TEMPORARY_FAILURE, "NORMAL_TEMPORARY_FAILURE", "Temporary failure" },
162 { AST_CAUSE_SWITCH_CONGESTION, "SWITCH_CONGESTION", "Switching equipment congestion" },
163 { AST_CAUSE_ACCESS_INFO_DISCARDED, "ACCESS_INFO_DISCARDED", "Access information discarded" },
164 { AST_CAUSE_REQUESTED_CHAN_UNAVAIL, "REQUESTED_CHAN_UNAVAIL", "Requested channel not available" },
165 { AST_CAUSE_FACILITY_NOT_SUBSCRIBED, "FACILITY_NOT_SUBSCRIBED", "Facility not subscribed" },
166 { AST_CAUSE_OUTGOING_CALL_BARRED, "OUTGOING_CALL_BARRED", "Outgoing call barred" },
167 { AST_CAUSE_INCOMING_CALL_BARRED, "INCOMING_CALL_BARRED", "Incoming call barred" },
168 { AST_CAUSE_BEARERCAPABILITY_NOTAUTH, "BEARERCAPABILITY_NOTAUTH", "Bearer capability not authorized" },
169 { AST_CAUSE_BEARERCAPABILITY_NOTAVAIL, "BEARERCAPABILITY_NOTAVAIL", "Bearer capability not available" },
170 { AST_CAUSE_BEARERCAPABILITY_NOTIMPL, "BEARERCAPABILITY_NOTIMPL", "Bearer capability not implemented" },
171 { AST_CAUSE_CHAN_NOT_IMPLEMENTED, "CHAN_NOT_IMPLEMENTED", "Channel not implemented" },
172 { AST_CAUSE_FACILITY_NOT_IMPLEMENTED, "FACILITY_NOT_IMPLEMENTED", "Facility not implemented" },
173 { AST_CAUSE_INVALID_CALL_REFERENCE, "INVALID_CALL_REFERENCE", "Invalid call reference value" },
174 { AST_CAUSE_INCOMPATIBLE_DESTINATION, "INCOMPATIBLE_DESTINATION", "Incompatible destination" },
175 { AST_CAUSE_INVALID_MSG_UNSPECIFIED, "INVALID_MSG_UNSPECIFIED", "Invalid message unspecified" },
176 { AST_CAUSE_MANDATORY_IE_MISSING, "MANDATORY_IE_MISSING", "Mandatory information element is missing" },
177 { AST_CAUSE_MESSAGE_TYPE_NONEXIST, "MESSAGE_TYPE_NONEXIST", "Message type nonexist." },
178 { AST_CAUSE_WRONG_MESSAGE, "WRONG_MESSAGE", "Wrong message" },
179 { AST_CAUSE_IE_NONEXIST, "IE_NONEXIST", "Info. element nonexist or not implemented" },
180 { AST_CAUSE_INVALID_IE_CONTENTS, "INVALID_IE_CONTENTS", "Invalid information element contents" },
181 { AST_CAUSE_WRONG_CALL_STATE, "WRONG_CALL_STATE", "Message not compatible with call state" },
182 { AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE, "RECOVERY_ON_TIMER_EXPIRE", "Recover on timer expiry" },
183 { AST_CAUSE_MANDATORY_IE_LENGTH_ERROR, "MANDATORY_IE_LENGTH_ERROR", "Mandatory IE length error" },
184 { AST_CAUSE_PROTOCOL_ERROR, "PROTOCOL_ERROR", "Protocol error, unspecified" },
185 { AST_CAUSE_INTERWORKING, "INTERWORKING", "Interworking, unspecified" },
186};
187
189{
190 struct chanlist *cl;
191 struct ast_variable *var = NULL, *prev = NULL;
192
194 AST_RWLIST_TRAVERSE(&backends, cl, list) {
195 if (prev) {
196 if ((prev->next = ast_variable_new(cl->tech->type, cl->tech->description, "")))
197 prev = prev->next;
198 } else {
199 var = ast_variable_new(cl->tech->type, cl->tech->description, "");
200 prev = var;
201 }
202 }
204
205 return var;
206}
207
208#if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
209static const char *party_number_ton2str(int ton)
210{
211#if defined(HAVE_PRI)
212 switch ((ton >> 4) & 0x07) {
213 case PRI_TON_INTERNATIONAL:
214 return "International";
215 case PRI_TON_NATIONAL:
216 return "National";
217 case PRI_TON_NET_SPECIFIC:
218 return "Network Specific";
219 case PRI_TON_SUBSCRIBER:
220 return "Subscriber";
221 case PRI_TON_ABBREVIATED:
222 return "Abbreviated";
223 case PRI_TON_RESERVED:
224 return "Reserved";
225 case PRI_TON_UNKNOWN:
226 default:
227 break;
228 }
229#endif /* defined(HAVE_PRI) */
230 return "Unknown";
231}
232#endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
233
234#if defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED)
235static const char *party_number_plan2str(int plan)
236{
237#if defined(HAVE_PRI)
238 switch (plan & 0x0F) {
239 default:
240 case PRI_NPI_UNKNOWN:
241 break;
242 case PRI_NPI_E163_E164:
243 return "Public (E.163/E.164)";
244 case PRI_NPI_X121:
245 return "Data (X.121)";
246 case PRI_NPI_F69:
247 return "Telex (F.69)";
248 case PRI_NPI_NATIONAL:
249 return "National Standard";
250 case PRI_NPI_PRIVATE:
251 return "Private";
252 case PRI_NPI_RESERVED:
253 return "Reserved";
254 }
255#endif /* defined(HAVE_PRI) */
256 return "Unknown";
257}
258#endif /* defined(KEEP_TILL_CHANNEL_PARTY_NUMBER_INFO_NEEDED) */
259
260/*! \brief Show channel types - CLI command */
261static char *handle_cli_core_show_channeltypes(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
262{
263#define FORMAT "%-15.15s %-40.40s %-13.13s %-13.13s %-13.13s %-13.13s\n"
264 struct chanlist *cl;
265 int count_chan = 0;
266
267 switch (cmd) {
268 case CLI_INIT:
269 e->command = "core show channeltypes";
270 e->usage =
271 "Usage: core show channeltypes\n"
272 " Lists available channel types registered in your\n"
273 " Asterisk server.\n";
274 return NULL;
275 case CLI_GENERATE:
276 return NULL;
277 }
278
279 if (a->argc != 3)
280 return CLI_SHOWUSAGE;
281
282 ast_cli(a->fd, FORMAT, "Type", "Description", "Devicestate", "Presencestate", "Indications", "Transfer");
283 ast_cli(a->fd, FORMAT, "-------------", "-------------", "-------------", "-------------", "-------------", "-------------");
284
287 ast_cli(a->fd, FORMAT, cl->tech->type, cl->tech->description,
288 (cl->tech->devicestate) ? "yes" : "no",
289 (cl->tech->presencestate) ? "yes" : "no",
290 (cl->tech->indicate) ? "yes" : "no",
291 (cl->tech->transfer) ? "yes" : "no");
292 count_chan++;
293 }
295
296 ast_cli(a->fd, "----------\n%d channel drivers registered.\n", count_chan);
297
298 return CLI_SUCCESS;
299
300#undef FORMAT
301}
302
304{
305 struct chanlist *cl;
306 int wordlen;
307
308 if (a->pos != 3) {
309 return NULL;
310 }
311
312 wordlen = strlen(a->word);
313
316 if (!strncasecmp(a->word, cl->tech->type, wordlen)) {
318 }
319 }
321
322 return NULL;
323}
324
325/*! \brief Show details about a channel driver - CLI command */
326static char *handle_cli_core_show_channeltype(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
327{
328 struct chanlist *cl = NULL;
330
331 switch (cmd) {
332 case CLI_INIT:
333 e->command = "core show channeltype";
334 e->usage =
335 "Usage: core show channeltype <name>\n"
336 " Show details about the specified channel type, <name>.\n";
337 return NULL;
338 case CLI_GENERATE:
339 return complete_channeltypes(a);
340 }
341
342 if (a->argc != 4)
343 return CLI_SHOWUSAGE;
344
346
347 AST_RWLIST_TRAVERSE(&backends, cl, list) {
348 if (!strncasecmp(cl->tech->type, a->argv[3], strlen(cl->tech->type)))
349 break;
350 }
351
352
353 if (!cl) {
354 ast_cli(a->fd, "\n%s is not a registered channel driver.\n", a->argv[3]);
356 return CLI_FAILURE;
357 }
358
359 ast_cli(a->fd,
360 "-- Info about channel driver: %s --\n"
361 " Device State: %s\n"
362 "Presence State: %s\n"
363 " Indication: %s\n"
364 " Transfer : %s\n"
365 " Capabilities: %s\n"
366 " Digit Begin: %s\n"
367 " Digit End: %s\n"
368 " Send HTML : %s\n"
369 " Image Support: %s\n"
370 " Text Support: %s\n",
371 cl->tech->type,
372 (cl->tech->devicestate) ? "yes" : "no",
373 (cl->tech->presencestate) ? "yes" : "no",
374 (cl->tech->indicate) ? "yes" : "no",
375 (cl->tech->transfer) ? "yes" : "no",
376 ast_format_cap_get_names(cl->tech->capabilities, &codec_buf),
377 (cl->tech->send_digit_begin) ? "yes" : "no",
378 (cl->tech->send_digit_end) ? "yes" : "no",
379 (cl->tech->send_html) ? "yes" : "no",
380 (cl->tech->send_image) ? "yes" : "no",
381 (cl->tech->send_text) ? "yes" : "no"
382
383 );
384
386
387 return CLI_SUCCESS;
388}
389
390static struct ast_cli_entry cli_channel[] = {
391 AST_CLI_DEFINE(handle_cli_core_show_channeltypes, "List available channel types"),
392 AST_CLI_DEFINE(handle_cli_core_show_channeltype, "Give more details on that channel type")
393};
394
395static struct ast_frame *kill_read(struct ast_channel *chan)
396{
397 /* Hangup channel. */
398 return NULL;
399}
400
401static struct ast_frame *kill_exception(struct ast_channel *chan)
402{
403 /* Hangup channel. */
404 return NULL;
405}
406
407static int kill_write(struct ast_channel *chan, struct ast_frame *frame)
408{
409 /* Hangup channel. */
410 return -1;
411}
412
413static int kill_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
414{
415 /* No problem fixing up the channel. */
416 return 0;
417}
418
419static int kill_hangup(struct ast_channel *chan)
420{
422 return 0;
423}
424
425/*!
426 * \brief Kill the channel channel driver technology descriptor.
427 *
428 * \details
429 * The purpose of this channel technology is to encourage the
430 * channel to hangup as quickly as possible.
431 *
432 * \note Used by DTMF atxfer and zombie channels.
433 */
435 .type = "Kill",
436 .description = "Kill channel (should not see this)",
437 .read = kill_read,
438 .exception = kill_exception,
439 .write = kill_write,
440 .fixup = kill_fixup,
441 .hangup = kill_hangup,
442};
443
444/*! \brief Checks to see if a channel is needing hang up */
446{
447 if (ast_channel_softhangup_internal_flag(chan)) /* yes if soft hangup flag set */
448 return 1;
449 if (ast_tvzero(*ast_channel_whentohangup(chan))) /* no if no hangup scheduled */
450 return 0;
451 if (ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow()) > 0) /* no if hangup time has not come yet. */
452 return 0;
453 ast_debug(4, "Hangup time has come: %" PRIi64 "\n", ast_tvdiff_ms(*ast_channel_whentohangup(chan), ast_tvnow()));
454 ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(chan));
456 return 1;
457}
458
460{
461 int res;
462 ast_channel_lock(chan);
463 res = ast_check_hangup(chan);
464 ast_channel_unlock(chan);
465 return res;
466}
467
469{
470 ast_channel_lock(chan);
471
472 if (causecode > 0) {
473 ast_debug(1, "Setting hangupcause of channel %s to %d (is %d now)\n",
474 ast_channel_name(chan), causecode, ast_channel_hangupcause(chan));
475
476 ast_channel_hangupcause_set(chan, causecode);
477 }
478
480
481 ast_channel_unlock(chan);
482}
483
484static int ast_channel_softhangup_cb(void *obj, void *arg, int flags)
485{
486 struct ast_channel *chan = obj;
487
489
490 return 0;
491}
492
494{
496}
497
498/*! \brief returns number of active/allocated channels */
500{
502}
503
505{
507}
508
509/*! \brief Set when to hangup channel */
510void ast_channel_setwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
511{
512 if (ast_tvzero(offset)) {
513 ast_channel_whentohangup_set(chan, &offset);
514 } else {
515 struct timeval tv = ast_tvadd(offset, ast_tvnow());
517 }
519 return;
520}
521
522/*! \brief Compare a offset with when to hangup channel */
523int ast_channel_cmpwhentohangup_tv(struct ast_channel *chan, struct timeval offset)
524{
525 struct timeval whentohangup;
526
528 return ast_tvzero(offset) ? 0 : -1;
529
530 if (ast_tvzero(offset))
531 return 1;
532
533 whentohangup = ast_tvadd(offset, ast_tvnow());
534
535 return ast_tvdiff_ms(whentohangup, *ast_channel_whentohangup(chan));
536}
537
538/*! \brief Register a new telephony channel in Asterisk */
540{
541 struct chanlist *chan;
542
544
546 if (!strcasecmp(tech->type, chan->tech->type)) {
547 ast_log(LOG_WARNING, "Already have a handler for type '%s'\n", tech->type);
549 return -1;
550 }
551 }
552
553 if (!(chan = ast_calloc(1, sizeof(*chan)))) {
555 return -1;
556 }
557 chan->tech = tech;
559
560 ast_debug(5, "Registered handler for '%s' (%s)\n", chan->tech->type, chan->tech->description);
561
562 ast_verb(5, "Registered channel type '%s' (%s)\n", chan->tech->type, chan->tech->description);
563
565
566 return 0;
567}
568
569/*! \brief Unregister channel driver */
571{
572 struct chanlist *chan;
573
574 ast_debug(5, "Unregistering channel type '%s'\n", tech->type);
575
577
579 if (chan->tech == tech) {
581 ast_free(chan);
582 ast_verb(5, "Unregistered channel type '%s'\n", tech->type);
583 break;
584 }
585 }
587
589}
590
591/*! \brief Get handle to channel driver based on name */
593{
594 struct chanlist *chanls;
595 const struct ast_channel_tech *ret = NULL;
596
598
599 AST_RWLIST_TRAVERSE(&backends, chanls, list) {
600 if (!strcasecmp(name, chanls->tech->type)) {
601 ret = chanls->tech;
602 break;
603 }
604 }
605
607
608 return ret;
609}
610
611/*! \brief Gives the string form of a given hangup cause */
612const char *ast_cause2str(int cause)
613{
614 int x;
615
616 for (x = 0; x < ARRAY_LEN(causes); x++) {
617 if (causes[x].cause == cause)
618 return causes[x].desc;
619 }
620
621 return "Unknown";
622}
623
624/*! \brief Convert a symbolic hangup cause to number */
625int ast_str2cause(const char *name)
626{
627 int x;
628
629 for (x = 0; x < ARRAY_LEN(causes); x++)
630 if (!strncasecmp(causes[x].name, name, strlen(causes[x].name)))
631 return causes[x].cause;
632
633 return -1;
634}
635
637{
638 char *buf;
639
640 switch (state) {
641 case AST_STATE_DOWN:
642 return "Down";
644 return "Rsrvd";
646 return "OffHook";
648 return "Dialing";
649 case AST_STATE_RING:
650 return "Ring";
652 return "Ringing";
653 case AST_STATE_UP:
654 return "Up";
655 case AST_STATE_BUSY:
656 return "Busy";
658 return "Dialing Offhook";
660 return "Pre-ring";
661 case AST_STATE_MUTE:
662 return "Mute";
663 default:
665 return "Unknown";
666 snprintf(buf, STATE2STR_BUFSIZE, "Unknown (%u)", state);
667 return buf;
668 }
669}
670
671/*! \brief Gives the string form of a given transfer capability */
672char *ast_transfercapability2str(int transfercapability)
673{
674 switch (transfercapability) {
676 return "SPEECH";
678 return "DIGITAL";
680 return "RESTRICTED_DIGITAL";
682 return "3K1AUDIO";
684 return "DIGITAL_W_TONES";
686 return "VIDEO";
687 default:
688 return "UNKNOWN";
689 }
690}
691
692/*! \brief Channel technology used to extract a channel from a running application. The
693 * channel created with this technology will be immediately hung up - most external
694 * applications won't ever want to see this.
695 */
696static const struct ast_channel_tech surrogate_tech = {
697 .type = "Surrogate",
698 .description = "Surrogate channel used to pull channel from an application",
699 .properties = AST_CHAN_TP_INTERNAL,
700};
701
702static const struct ast_channel_tech null_tech = {
703 .type = "NULL",
704 .description = "Null channel (should not see this)",
705};
706
707static void ast_channel_destructor(void *obj);
708static void ast_dummy_channel_destructor(void *obj);
709static int ast_channel_by_uniqueid_cb(void *obj, void *arg, void *data, int flags);
710
711static int does_id_conflict(const char *uniqueid)
712{
713 struct ast_channel *conflict;
714 size_t length = 0;
715
717 return 0;
718 }
719
721 if (conflict) {
722 ast_log(LOG_ERROR, "Channel Unique ID '%s' already in use by channel %s(%p)\n",
723 uniqueid, ast_channel_name(conflict), conflict);
724 ast_channel_unref(conflict);
725 return 1;
726 }
727
728 return 0;
729}
730
731/*! \brief Create a new channel structure */
732static struct ast_channel *__attribute__((format(printf, 15, 0)))
733__ast_channel_alloc_ap(int needqueue, int state, const char *cid_num, const char *cid_name,
734 const char *acctcode, const char *exten, const char *context, const struct ast_assigned_ids *assignedids,
735 const struct ast_channel *requestor, enum ama_flags amaflag, struct ast_endpoint *endpoint,
736 struct ast_channel_initializers *initializers, const char *file, int line,
737 const char *function, const char *name_fmt, va_list ap)
738{
739 struct ast_channel *tmp;
740 struct varshead *headp;
741 char *tech = "", *tech2 = NULL;
742 struct ast_format_cap *nativeformats;
743 struct ast_sched_context *schedctx;
744 struct ast_timer *timer;
745 struct timeval now;
746 const struct ast_channel_tech *channel_tech;
747 struct ast_stream_topology *topology;
748
749 /* If shutting down, don't allocate any new channels */
750 if (ast_shutting_down()) {
751 ast_log(LOG_WARNING, "Channel allocation failed: Refusing due to active shutdown\n");
752 return NULL;
753 }
754
755 tmp = __ast_channel_internal_alloc_with_initializers(ast_channel_destructor, assignedids, requestor, initializers,
756 file, line, function);
757 if (!tmp) {
758 /* Channel structure allocation failure. */
759 return NULL;
760 }
761
763
764 /*
765 * Init file descriptors to unopened state so
766 * the destructor can know not to close them.
767 */
771
773 if (!nativeformats) {
774 /*
775 * Aborting the channel creation. We do not need to complete staging
776 * the channel snapshot because the channel has not been finalized or
777 * linked into the channels container yet. Nobody else knows about
778 * this channel nor will anybody ever know about it.
779 */
780 return ast_channel_unref(tmp);
781 }
782 ast_format_cap_append(nativeformats, ast_format_none, 0);
783 ast_channel_nativeformats_set(tmp, nativeformats);
784 ao2_ref(nativeformats, -1);
785
790
791 if (!(schedctx = ast_sched_context_create())) {
792 ast_log(LOG_WARNING, "Channel allocation failed: Unable to create schedule context\n");
793 /* See earlier channel creation abort comment above. */
794 return ast_channel_unref(tmp);
795 }
796 ast_channel_sched_set(tmp, schedctx);
797
803
804 if (cid_name) {
807 if (!ast_channel_caller(tmp)->id.name.str) {
808 /* See earlier channel creation abort comment above. */
809 return ast_channel_unref(tmp);
810 }
811 }
812 if (cid_num) {
815 if (!ast_channel_caller(tmp)->id.number.str) {
816 /* See earlier channel creation abort comment above. */
817 return ast_channel_unref(tmp);
818 }
819 }
820
821 if ((timer = ast_timer_open())) {
823 if (strcmp(ast_timer_get_name(ast_channel_timer(tmp)), "timerfd")) {
824 needqueue = 0;
825 }
827 }
828
829 if (needqueue && ast_channel_internal_alertpipe_init(tmp)) {
830 /* See earlier channel creation abort comment above. */
831 return ast_channel_unref(tmp);
832 }
833
834 if (!(topology = ast_stream_topology_alloc())) {
835 return ast_channel_unref(tmp);
836 }
838
839 /* Always watch the alertpipe */
841 /* And timing pipe */
843
844 /* Initial state */
847
850
853
854 now = ast_tvnow();
856
858
859 if (!ast_strlen_zero(name_fmt)) {
860 char *slash, *slash2;
861 /* Almost every channel is calling this function, and setting the name via the ast_string_field_build() call.
862 * And they all use slightly different formats for their name string.
863 * This means, to set the name here, we have to accept variable args, and call the string_field_build from here.
864 * This means, that the stringfields must have a routine that takes the va_lists directly, and
865 * uses them to build the string, instead of forming the va_lists internally from the vararg ... list.
866 * This new function was written so this can be accomplished.
867 */
868 ast_channel_name_build_va(tmp, name_fmt, ap);
870 if ((slash = strchr(tech, '/'))) {
871 if ((slash2 = strchr(slash + 1, '/'))) {
872 tech2 = slash + 1;
873 *slash2 = '\0';
874 }
875 *slash = '\0';
876 }
877 } else {
878 /*
879 * Start the string with '-' so it becomes an empty string
880 * in the destructor.
881 */
882 ast_channel_name_set(tmp, "-**Unknown**");
883 }
884
885 if (amaflag != AST_AMA_NONE) {
887 } else {
889 }
890
891 if (!ast_strlen_zero(acctcode)) {
892 ast_channel_accountcode_set(tmp, acctcode);
893 }
894 ast_channel_language_set(tmp, ast_defaultlanguage);
895
897 ast_channel_exten_set(tmp, S_OR(exten, "s"));
899
900 headp = ast_channel_varshead(tmp);
902
906
907 channel_tech = ast_get_channel_tech(tech);
908 if (!channel_tech && !ast_strlen_zero(tech2)) {
909 channel_tech = ast_get_channel_tech(tech2);
910 }
911 if (channel_tech) {
912 ast_channel_tech_set(tmp, channel_tech);
913 } else {
915 }
916
917 /* You might scream "locking inversion" at seeing this but it is actually perfectly fine.
918 * Since the channel was just created nothing can know about it yet or even acquire it.
919 */
921
923
924 if (assignedids && (does_id_conflict(assignedids->uniqueid) || does_id_conflict(assignedids->uniqueid2))) {
928 /* See earlier channel creation abort comment above. */
929 return ast_channel_unref(tmp);
930 }
931
932 /* Finalize and link into the channels container. */
936
938
939 if (endpoint) {
940 ast_endpoint_add_channel(endpoint, tmp);
941 }
942
943 /*
944 * And now, since the channel structure is built, and has its name, let
945 * the world know of its existance
946 */
948
949 ast_debug(1, "Channel %p '%s' allocated\n", tmp, ast_channel_name(tmp));
950
951 return tmp;
952}
953
954struct ast_channel *__ast_channel_alloc(int needqueue, int state, const char *cid_num,
955 const char *cid_name, const char *acctcode,
956 const char *exten, const char *context, const struct ast_assigned_ids *assignedids,
957 const struct ast_channel *requestor, enum ama_flags amaflag,
958 struct ast_endpoint *endpoint,
959 const char *file, int line, const char *function,
960 const char *name_fmt, ...)
961{
962 va_list ap;
963 struct ast_channel *result;
964
965 va_start(ap, name_fmt);
966 result = __ast_channel_alloc_ap(needqueue, state, cid_num, cid_name, acctcode, exten, context,
967 assignedids, requestor, amaflag, endpoint, NULL, file, line, function, name_fmt, ap);
968 va_end(ap);
969
970 return result;
971}
972
973struct ast_channel *__ast_channel_alloc_with_initializers(int needqueue, int state, const char *cid_num,
974 const char *cid_name, const char *acctcode,
975 const char *exten, const char *context, const struct ast_assigned_ids *assignedids,
976 const struct ast_channel *requestor, enum ama_flags amaflag,
977 struct ast_endpoint *endpoint, struct ast_channel_initializers *initializers,
978 const char *file, int line, const char *function,
979 const char *name_fmt, ...)
980{
981 va_list ap;
982 struct ast_channel *result;
983
984 va_start(ap, name_fmt);
985 result = __ast_channel_alloc_ap(needqueue, state, cid_num, cid_name, acctcode, exten, context,
986 assignedids, requestor, amaflag, endpoint, initializers, file, line, function, name_fmt, ap);
987 va_end(ap);
988
989 return result;
990}
991
992/* only do the minimum amount of work needed here to make a channel
993 * structure that can be used to expand channel vars */
994struct ast_channel *__ast_dummy_channel_alloc(const char *file, int line, const char *function)
995{
996 struct ast_channel *tmp;
997 struct varshead *headp;
998
1000 file, line, function);
1001 if (!tmp) {
1002 /* Dummy channel structure allocation failure. */
1003 return NULL;
1004 }
1005
1008
1009 /*
1010 * Init file descriptors to unopened state just in case
1011 * autoservice is called on the channel or something tries to
1012 * read a frame from it.
1013 */
1017
1019
1021
1022 headp = ast_channel_varshead(tmp);
1024
1025 return tmp;
1026}
1027
1028static int __ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin, int head, struct ast_frame *after)
1029{
1030 struct ast_frame *f;
1031 struct ast_frame *cur;
1032 unsigned int new_frames = 0;
1033 unsigned int new_voice_frames = 0;
1034 unsigned int queued_frames = 0;
1035 unsigned int queued_voice_frames = 0;
1037
1038 ast_channel_lock(chan);
1039
1040 /*
1041 * Check the last frame on the queue if we are queuing the new
1042 * frames after it.
1043 */
1044 cur = AST_LIST_LAST(ast_channel_readq(chan));
1045 if (cur && cur->frametype == AST_FRAME_CONTROL && !head && (!after || after == cur)) {
1046 switch (cur->subclass.integer) {
1048 if (fin->frametype == AST_FRAME_CONTROL
1049 && fin->subclass.integer == AST_CONTROL_HANGUP) {
1050 /*
1051 * Destroy the end-of-Q marker frame so we can queue the hangup
1052 * frame in its place.
1053 */
1055 ast_frfree(cur);
1056
1057 /*
1058 * This has degenerated to a normal queue append anyway. Since
1059 * we just destroyed the last frame in the queue we must make
1060 * sure that "after" is NULL or bad things will happen.
1061 */
1062 after = NULL;
1063 break;
1064 }
1065 /* Fall through */
1066 case AST_CONTROL_HANGUP:
1067 /* Don't queue anything. */
1068 ast_channel_unlock(chan);
1069 return 0;
1070 default:
1071 break;
1072 }
1073 }
1074
1075 /* Build copies of all the new frames and count them */
1077 for (cur = fin; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
1078 if (!(f = ast_frdup(cur))) {
1079 if (AST_LIST_FIRST(&frames)) {
1081 }
1082 ast_channel_unlock(chan);
1083 return -1;
1084 }
1085
1087 new_frames++;
1088 if (f->frametype == AST_FRAME_VOICE) {
1089 new_voice_frames++;
1090 }
1091 }
1092
1093 /* Count how many frames exist on the queue */
1095 queued_frames++;
1096 if (cur->frametype == AST_FRAME_VOICE) {
1097 queued_voice_frames++;
1098 }
1099 }
1100
1101 if ((queued_frames + new_frames > 128 || queued_voice_frames + new_voice_frames > 96)) {
1102 int total_queued = queued_frames + new_frames;
1103 int total_voice = queued_voice_frames + new_voice_frames;
1104 int count = 0;
1105 ast_log(LOG_WARNING, "Exceptionally long %squeue length (%d voice / %d total) queuing to %s\n",
1106 queued_frames + new_frames > 128 ? "" : "voice ", total_voice, total_queued, ast_channel_name(chan));
1108 /* Save the most recent frame */
1109 if (!AST_LIST_NEXT(cur, frame_list)) {
1110 break;
1111 } else if (cur->frametype == AST_FRAME_VOICE || cur->frametype == AST_FRAME_VIDEO || cur->frametype == AST_FRAME_NULL) {
1112 if (++count > 64) {
1113 break;
1114 }
1116 ast_frfree(cur);
1117
1118 /* Read from the alert pipe for each flushed frame. */
1120 }
1121 }
1123 if (count) {
1124 ast_debug(4, "Discarded %d frame%s due to queue overload on %s\n", count, ESS(count), ast_channel_name(chan));
1125 }
1126 }
1127
1128 if (after) {
1130 } else {
1131 if (head) {
1134 }
1136 }
1137
1138 if (ast_channel_alert_writable(chan)) {
1139 /* Write to the alert pipe for each added frame */
1140 while (new_frames--) {
1141 if (ast_channel_alert_write(chan)) {
1142 ast_log(LOG_WARNING, "Unable to write to alert pipe on %s (qlen = %u): %s!\n",
1143 ast_channel_name(chan), queued_frames, strerror(errno));
1144 break;
1145 }
1146 }
1147 } else if (ast_channel_timingfd(chan) > -1) {
1150 pthread_kill(ast_channel_blocker(chan), SIGURG);
1151 }
1152
1153 ast_channel_unlock(chan);
1154
1155 return 0;
1156}
1157
1158int ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin)
1159{
1160 return __ast_queue_frame(chan, fin, 0, NULL);
1161}
1162
1163int ast_queue_frame_head(struct ast_channel *chan, struct ast_frame *fin)
1164{
1165 return __ast_queue_frame(chan, fin, 1, NULL);
1166}
1167
1168/*! \brief Queue a hangup frame for channel */
1170{
1171 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
1172 int res;
1173
1174 /* Yeah, let's not change a lock-critical value without locking */
1175 ast_channel_lock(chan);
1178
1179 res = ast_queue_frame(chan, &f);
1180 ast_channel_unlock(chan);
1181 return res;
1182}
1183
1184/*! \brief Queue a hangup frame for channel */
1185int ast_queue_hangup_with_cause(struct ast_channel *chan, int cause)
1186{
1187 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
1188 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
1189 int res;
1190
1191 if (cause >= 0) {
1192 f.data.uint32 = cause;
1193 }
1194
1195 /* Yeah, let's not change a lock-critical value without locking */
1196 ast_channel_lock(chan);
1198 if (cause < 0) {
1200 }
1201 blob = ast_json_pack("{s: i}",
1202 "cause", cause);
1204
1205 res = ast_queue_frame(chan, &f);
1206 ast_channel_unlock(chan);
1207 return res;
1208}
1209
1210int ast_queue_hold(struct ast_channel *chan, const char *musicclass)
1211{
1212 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HOLD };
1213 struct ast_json *blob = NULL;
1214 int res;
1215
1216 if (!ast_strlen_zero(musicclass)) {
1217 f.data.ptr = (void *) musicclass;
1218 f.datalen = strlen(musicclass) + 1;
1219
1220 blob = ast_json_pack("{s: s}",
1221 "musicclass", musicclass);
1222 }
1223
1224 ast_channel_lock(chan);
1226 ast_channel_unlock(chan);
1227
1228 res = ast_queue_frame(chan, &f);
1229
1230 ast_json_unref(blob);
1231
1232 return res;
1233}
1234
1236{
1237 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_UNHOLD };
1238 int res;
1239
1240 ast_channel_lock(chan);
1242 ast_channel_unlock(chan);
1243
1244 res = ast_queue_frame(chan, &f);
1245
1246 return res;
1247}
1248
1249/*! \brief Queue a control frame */
1251{
1252 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control };
1253 return ast_queue_frame(chan, &f);
1254}
1255
1256/*! \brief Queue a control frame with payload */
1258 const void *data, size_t datalen)
1259{
1260 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control, .data.ptr = (void *) data, .datalen = datalen };
1261 return ast_queue_frame(chan, &f);
1262}
1263
1264/*! \brief Queue an ANSWER control frame with topology */
1265int ast_queue_answer(struct ast_channel *chan, const struct ast_stream_topology *topology)
1266{
1267 struct ast_frame f = {
1269 .subclass.integer = AST_CONTROL_ANSWER,
1270 .subclass.topology = (struct ast_stream_topology *)topology,
1271 };
1272 return ast_queue_frame(chan, &f);
1273}
1274
1275/*! \brief Set defer DTMF flag on channel */
1277{
1278 int pre = 0;
1279
1280 if (chan) {
1281 ast_channel_lock(chan);
1284 ast_channel_unlock(chan);
1285 }
1286 return pre;
1287}
1288
1289/*! \brief Unset defer DTMF flag on channel */
1291{
1292 if (chan) {
1294 }
1295}
1296
1298 void *data, int ao2_flags)
1299{
1300 return ao2_callback_data(channels, ao2_flags, cb_fn, arg, data);
1301}
1302
1303static int ast_channel_by_name_cb(void *obj, void *arg, void *data, int flags)
1304{
1305 struct ast_channel *chan = obj;
1306 const char *name = arg;
1307 size_t name_len = *(size_t *) data;
1308 int ret = CMP_MATCH;
1309
1310 if (ast_strlen_zero(name)) {
1311 ast_log(LOG_ERROR, "BUG! Must supply a channel name or partial name to match!\n");
1312 return CMP_STOP;
1313 }
1314
1315 ast_channel_lock(chan);
1316 if ((!name_len && strcasecmp(ast_channel_name(chan), name))
1317 || (name_len && strncasecmp(ast_channel_name(chan), name, name_len))) {
1318 ret = 0; /* name match failed, keep looking */
1319 }
1320 ast_channel_unlock(chan);
1321
1322 return ret;
1323}
1324
1325static int ast_channel_by_exten_cb(void *obj, void *arg, void *data, int flags)
1326{
1327 struct ast_channel *chan = obj;
1328 char *context = arg;
1329 char *exten = data;
1330 int ret = CMP_MATCH;
1331
1333 ast_log(LOG_ERROR, "BUG! Must have a context and extension to match!\n");
1334 return CMP_STOP;
1335 }
1336
1337 ast_channel_lock(chan);
1338 if (strcasecmp(ast_channel_context(chan), context)) {
1339 ret = 0; /* Context match failed, continue */
1340 } else if (strcasecmp(ast_channel_exten(chan), exten)) {
1341 ret = 0; /* Extension match failed, continue */
1342 }
1343 ast_channel_unlock(chan);
1344
1345 return ret;
1346}
1347
1348static int ast_channel_by_uniqueid_cb(void *obj, void *arg, void *data, int flags)
1349{
1350 struct ast_channel *chan = obj;
1351 char *uniqueid = arg;
1352 size_t id_len = *(size_t *) data;
1353 int ret = CMP_MATCH;
1354
1356 ast_log(LOG_ERROR, "BUG! Must supply a uniqueid or partial uniqueid to match!\n");
1357 return CMP_STOP;
1358 }
1359
1360 ast_channel_lock(chan);
1361 if ((!id_len && strcasecmp(ast_channel_uniqueid(chan), uniqueid))
1362 || (id_len && strncasecmp(ast_channel_uniqueid(chan), uniqueid, id_len))) {
1363 ret = 0; /* uniqueid match failed, keep looking */
1364 }
1365 ast_channel_unlock(chan);
1366
1367 return ret;
1368}
1369
1371 /* storage for non-dynamically allocated iterator */
1373 /* pointer to the actual iterator (simple_iterator or a dynamically
1374 * allocated iterator)
1375 */
1377};
1378
1380{
1382 ast_free(i);
1383
1384 return NULL;
1385}
1386
1388{
1389 struct ast_channel_iterator *i;
1390 char *l_exten = (char *) exten;
1391 char *l_context = (char *) context;
1392
1393 if (!(i = ast_calloc(1, sizeof(*i)))) {
1394 return NULL;
1395 }
1396
1398 l_context, l_exten, OBJ_MULTIPLE);
1399 if (!i->active_iterator) {
1400 ast_free(i);
1401 return NULL;
1402 }
1403
1404 return i;
1405}
1406
1408{
1409 struct ast_channel_iterator *i;
1410 char *l_name = (char *) name;
1411
1412 if (!(i = ast_calloc(1, sizeof(*i)))) {
1413 return NULL;
1414 }
1415
1417 l_name, &name_len,
1418 OBJ_MULTIPLE | (name_len == 0 /* match the whole word, so optimize */ ? OBJ_KEY : 0));
1419 if (!i->active_iterator) {
1420 ast_free(i);
1421 return NULL;
1422 }
1423
1424 return i;
1425}
1426
1428{
1429 struct ast_channel_iterator *i;
1430
1431 if (!(i = ast_calloc(1, sizeof(*i)))) {
1432 return NULL;
1433 }
1434
1437
1438 return i;
1439}
1440
1442{
1444}
1445
1446/* Legacy function, not currently used for lookups, but we need a cmp_fn */
1447static int ast_channel_cmp_cb(void *obj, void *arg, int flags)
1448{
1449 ast_log(LOG_ERROR, "BUG! Should never be called!\n");
1450 return CMP_STOP;
1451}
1452
1453struct ast_channel *ast_channel_get_by_name_prefix(const char *name, size_t name_len)
1454{
1455 struct ast_channel *chan;
1456 char *l_name = (char *) name;
1457
1458 if (ast_strlen_zero(l_name)) {
1459 /* We didn't have a name to search for so quit. */
1460 return NULL;
1461 }
1462
1463 chan = ast_channel_callback(ast_channel_by_name_cb, l_name, &name_len,
1464 (name_len == 0) /* optimize if it is a complete name match */ ? OBJ_KEY : 0);
1465 if (chan) {
1466 return chan;
1467 }
1468
1469 /* Now try a search for uniqueid. */
1470 return ast_channel_callback(ast_channel_by_uniqueid_cb, l_name, &name_len, 0);
1471}
1472
1474{
1476}
1477
1478struct ast_channel *ast_channel_get_by_exten(const char *exten, const char *context)
1479{
1480 char *l_exten = (char *) exten;
1481 char *l_context = (char *) context;
1482
1483 return ast_channel_callback(ast_channel_by_exten_cb, l_context, l_exten, 0);
1484}
1485
1486int ast_is_deferrable_frame(const struct ast_frame *frame)
1487{
1488 /* Do not add a default entry in this switch statement. Each new
1489 * frame type should be addressed directly as to whether it should
1490 * be queued up or not.
1491 */
1492 switch (frame->frametype) {
1495 case AST_FRAME_CONTROL:
1496 case AST_FRAME_TEXT:
1498 case AST_FRAME_IMAGE:
1499 case AST_FRAME_HTML:
1500 return 1;
1501
1502 case AST_FRAME_DTMF_END:
1504 case AST_FRAME_VOICE:
1505 case AST_FRAME_VIDEO:
1506 case AST_FRAME_NULL:
1507 case AST_FRAME_IAX:
1508 case AST_FRAME_CNG:
1509 case AST_FRAME_MODEM:
1510 case AST_FRAME_RTCP:
1511 return 0;
1512 }
1513 return 0;
1514}
1515
1516/*! \brief Wait, look for hangups and condition arg */
1517static int safe_sleep_conditional(struct ast_channel *chan, int timeout_ms, int (*cond)(void*), void *data, unsigned int generate_silence)
1518{
1519 struct ast_frame *f;
1520 struct ast_silence_generator *silgen = NULL;
1521 int res = 0;
1522 struct timeval start;
1523 int ms;
1524 AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_frames;
1525
1526 AST_LIST_HEAD_INIT_NOLOCK(&deferred_frames);
1527
1528 /* If no other generator is present, start silencegen while waiting */
1529 if (generate_silence && ast_opt_transmit_silence && !ast_channel_generatordata(chan)) {
1531 }
1532
1533 start = ast_tvnow();
1534 while ((ms = ast_remaining_ms(start, timeout_ms))) {
1535 struct ast_frame *dup_f = NULL;
1536
1537 if (cond && ((*cond)(data) == 0)) {
1538 break;
1539 }
1540 ms = ast_waitfor(chan, ms);
1541 if (ms < 0) {
1542 res = -1;
1543 break;
1544 }
1545 if (ms > 0) {
1546 f = ast_read(chan);
1547 if (!f) {
1548 res = -1;
1549 break;
1550 }
1551
1552 if (!ast_is_deferrable_frame(f)) {
1553 ast_frfree(f);
1554 continue;
1555 }
1556
1557 if ((dup_f = ast_frisolate(f))) {
1558 if (dup_f != f) {
1559 ast_frfree(f);
1560 }
1561 AST_LIST_INSERT_HEAD(&deferred_frames, dup_f, frame_list);
1562 }
1563 }
1564 }
1565
1566 /* stop silgen if present */
1567 if (silgen) {
1569 }
1570
1571 /* We need to free all the deferred frames, but we only need to
1572 * queue the deferred frames if there was no error and no
1573 * hangup was received
1574 */
1575 ast_channel_lock(chan);
1576 while ((f = AST_LIST_REMOVE_HEAD(&deferred_frames, frame_list))) {
1577 if (!res) {
1578 ast_queue_frame_head(chan, f);
1579 }
1580 ast_frfree(f);
1581 }
1582 ast_channel_unlock(chan);
1583
1584 return res;
1585}
1586
1587int ast_safe_sleep_conditional(struct ast_channel *chan, int timeout_ms, int (*cond)(void*), void *data)
1588{
1589 return safe_sleep_conditional(chan, timeout_ms, cond, data, 1);
1590}
1591
1592/*! \brief Wait, look for hangups */
1593int ast_safe_sleep(struct ast_channel *chan, int ms)
1594{
1595 return safe_sleep_conditional(chan, ms, NULL, NULL, 1);
1596}
1597
1599{
1600 return safe_sleep_conditional(chan, ms, NULL, NULL, 0);
1601}
1602
1604{
1605 /* Safe, even if already unlinked. */
1606 ao2_unlink(channels, chan);
1607 return ast_channel_unref(chan);
1608}
1609
1611{
1612 init->str = NULL;
1615 init->valid = 0;
1616}
1617
1618void ast_party_name_copy(struct ast_party_name *dest, const struct ast_party_name *src)
1619{
1620 if (dest == src) {
1621 /* Don't copy to self */
1622 return;
1623 }
1624
1625 ast_free(dest->str);
1626 dest->str = ast_strdup(src->str);
1627 dest->char_set = src->char_set;
1628 dest->presentation = src->presentation;
1629 dest->valid = src->valid;
1630}
1631
1632void ast_party_name_set_init(struct ast_party_name *init, const struct ast_party_name *guide)
1633{
1634 init->str = NULL;
1635 init->char_set = guide->char_set;
1636 init->presentation = guide->presentation;
1637 init->valid = guide->valid;
1638}
1639
1640void ast_party_name_set(struct ast_party_name *dest, const struct ast_party_name *src)
1641{
1642 if (dest == src) {
1643 /* Don't set to self */
1644 return;
1645 }
1646
1647 if (src->str && src->str != dest->str) {
1648 ast_free(dest->str);
1649 dest->str = ast_strdup(src->str);
1650 }
1651
1652 dest->char_set = src->char_set;
1653 dest->presentation = src->presentation;
1654 dest->valid = src->valid;
1655}
1656
1658{
1659 ast_free(doomed->str);
1660 doomed->str = NULL;
1661}
1662
1664{
1665 init->str = NULL;
1666 init->plan = 0;/* Unknown */
1668 init->valid = 0;
1669}
1670
1671void ast_party_number_copy(struct ast_party_number *dest, const struct ast_party_number *src)
1672{
1673 if (dest == src) {
1674 /* Don't copy to self */
1675 return;
1676 }
1677
1678 ast_free(dest->str);
1679 dest->str = ast_strdup(src->str);
1680 dest->plan = src->plan;
1681 dest->presentation = src->presentation;
1682 dest->valid = src->valid;
1683}
1684
1685void ast_party_number_set_init(struct ast_party_number *init, const struct ast_party_number *guide)
1686{
1687 init->str = NULL;
1688 init->plan = guide->plan;
1689 init->presentation = guide->presentation;
1690 init->valid = guide->valid;
1691}
1692
1693void ast_party_number_set(struct ast_party_number *dest, const struct ast_party_number *src)
1694{
1695 if (dest == src) {
1696 /* Don't set to self */
1697 return;
1698 }
1699
1700 if (src->str && src->str != dest->str) {
1701 ast_free(dest->str);
1702 dest->str = ast_strdup(src->str);
1703 }
1704
1705 dest->plan = src->plan;
1706 dest->presentation = src->presentation;
1707 dest->valid = src->valid;
1708}
1709
1711{
1712 ast_free(doomed->str);
1713 doomed->str = NULL;
1714}
1715
1717{
1718 init->str = NULL;
1719 init->type = 0;
1720 init->odd_even_indicator = 0;
1721 init->valid = 0;
1722}
1723
1725{
1726 if (dest == src) {
1727 /* Don't copy to self */
1728 return;
1729 }
1730
1731 ast_free(dest->str);
1732 dest->str = ast_strdup(src->str);
1733 dest->type = src->type;
1735 dest->valid = src->valid;
1736}
1737
1739{
1740 init->str = NULL;
1741 init->type = guide->type;
1743 init->valid = guide->valid;
1744}
1745
1747{
1748 if (dest == src) {
1749 /* Don't set to self */
1750 return;
1751 }
1752
1753 if (src->str && src->str != dest->str) {
1754 ast_free(dest->str);
1755 dest->str = ast_strdup(src->str);
1756 }
1757
1758 dest->type = src->type;
1760 dest->valid = src->valid;
1761}
1762
1764{
1765 ast_free(doomed->str);
1766 doomed->str = NULL;
1767}
1768
1770{
1771 update_id->name = 1;
1772 update_id->number = 1;
1773 update_id->subaddress = 1;
1774}
1775
1777{
1778 ast_party_name_init(&init->name);
1781 init->tag = NULL;
1782}
1783
1784void ast_party_id_copy(struct ast_party_id *dest, const struct ast_party_id *src)
1785{
1786 if (dest == src) {
1787 /* Don't copy to self */
1788 return;
1789 }
1790
1791 ast_party_name_copy(&dest->name, &src->name);
1792 ast_party_number_copy(&dest->number, &src->number);
1794
1795 ast_free(dest->tag);
1796 dest->tag = ast_strdup(src->tag);
1797}
1798
1799void ast_party_id_set_init(struct ast_party_id *init, const struct ast_party_id *guide)
1800{
1801 ast_party_name_set_init(&init->name, &guide->name);
1802 ast_party_number_set_init(&init->number, &guide->number);
1804 init->tag = NULL;
1805}
1806
1807void ast_party_id_set(struct ast_party_id *dest, const struct ast_party_id *src, const struct ast_set_party_id *update)
1808{
1809 if (dest == src) {
1810 /* Don't set to self */
1811 return;
1812 }
1813
1814 if (!update || update->name) {
1815 ast_party_name_set(&dest->name, &src->name);
1816 }
1817 if (!update || update->number) {
1818 ast_party_number_set(&dest->number, &src->number);
1819 }
1820 if (!update || update->subaddress) {
1822 }
1823
1824 if (src->tag && src->tag != dest->tag) {
1825 ast_free(dest->tag);
1826 dest->tag = ast_strdup(src->tag);
1827 }
1828}
1829
1831{
1832 ast_party_name_free(&doomed->name);
1833 ast_party_number_free(&doomed->number);
1835
1836 ast_free(doomed->tag);
1837 doomed->tag = NULL;
1838}
1839
1841{
1842 int number_priority;
1843 int number_value;
1844 int number_screening;
1845 int name_priority;
1846 int name_value;
1847
1848 /* Determine name presentation priority. */
1849 if (!id->name.valid) {
1850 name_value = AST_PRES_UNAVAILABLE;
1851 name_priority = 3;
1852 } else {
1853 name_value = id->name.presentation & AST_PRES_RESTRICTION;
1854 switch (name_value) {
1856 name_priority = 0;
1857 break;
1858 case AST_PRES_ALLOWED:
1859 name_priority = 1;
1860 break;
1862 name_priority = 2;
1863 break;
1864 default:
1865 name_value = AST_PRES_UNAVAILABLE;
1866 name_priority = 3;
1867 break;
1868 }
1869 }
1870
1871 /* Determine number presentation priority. */
1872 if (!id->number.valid) {
1873 number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
1874 number_value = AST_PRES_UNAVAILABLE;
1875 number_priority = 3;
1876 } else {
1877 number_screening = id->number.presentation & AST_PRES_NUMBER_TYPE;
1878 number_value = id->number.presentation & AST_PRES_RESTRICTION;
1879 switch (number_value) {
1881 number_priority = 0;
1882 break;
1883 case AST_PRES_ALLOWED:
1884 number_priority = 1;
1885 break;
1887 number_priority = 2;
1888 break;
1889 default:
1890 number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
1891 number_value = AST_PRES_UNAVAILABLE;
1892 number_priority = 3;
1893 break;
1894 }
1895 }
1896
1897 /* Select the wining presentation value. */
1898 if (name_priority < number_priority) {
1899 number_value = name_value;
1900 }
1901 if (number_value == AST_PRES_UNAVAILABLE) {
1903 }
1904
1905 return number_value | number_screening;
1906}
1907
1909{
1910 id->name.valid = 0;
1911 id->number.valid = 0;
1912 id->subaddress.valid = 0;
1913}
1914
1916{
1919}
1920
1921struct ast_party_id ast_party_id_merge(struct ast_party_id *base, struct ast_party_id *overlay)
1922{
1923 struct ast_party_id merged;
1924
1925 merged = *base;
1926 if (overlay->name.valid) {
1927 merged.name = overlay->name;
1928 }
1929 if (overlay->number.valid) {
1930 merged.number = overlay->number;
1931 }
1932 if (overlay->subaddress.valid) {
1933 merged.subaddress = overlay->subaddress;
1934 }
1935 /* Note the actual structure is returned and not a pointer to it! */
1936 return merged;
1937}
1938
1939void ast_party_id_merge_copy(struct ast_party_id *dest, struct ast_party_id *base, struct ast_party_id *overlay)
1940{
1941 struct ast_party_id merged;
1942
1943 merged = ast_party_id_merge(base, overlay);
1944 ast_party_id_copy(dest, &merged);
1945}
1946
1948{
1949 init->number.str = NULL;
1950 init->number.plan = 0;/* Unknown */
1952 init->transit_network_select = 0;
1953}
1954
1955void ast_party_dialed_copy(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
1956{
1957 if (dest == src) {
1958 /* Don't copy to self */
1959 return;
1960 }
1961
1962 ast_free(dest->number.str);
1963 dest->number.str = ast_strdup(src->number.str);
1964 dest->number.plan = src->number.plan;
1967}
1968
1969void ast_party_dialed_set_init(struct ast_party_dialed *init, const struct ast_party_dialed *guide)
1970{
1971 init->number.str = NULL;
1972 init->number.plan = guide->number.plan;
1975}
1976
1977void ast_party_dialed_set(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
1978{
1979 if (src->number.str && src->number.str != dest->number.str) {
1980 ast_free(dest->number.str);
1981 dest->number.str = ast_strdup(src->number.str);
1982 }
1983 dest->number.plan = src->number.plan;
1984
1986
1988}
1989
1991{
1992 ast_free(doomed->number.str);
1993 doomed->number.str = NULL;
1995}
1996
1998{
1999 ast_party_id_init(&init->id);
2000 ast_party_id_init(&init->ani);
2001 ast_party_id_init(&init->priv);
2002 init->ani2 = 0;
2003}
2004
2005void ast_party_caller_copy(struct ast_party_caller *dest, const struct ast_party_caller *src)
2006{
2007 if (dest == src) {
2008 /* Don't copy to self */
2009 return;
2010 }
2011
2012 ast_party_id_copy(&dest->id, &src->id);
2013 ast_party_id_copy(&dest->ani, &src->ani);
2014 ast_party_id_copy(&dest->priv, &src->priv);
2015 dest->ani2 = src->ani2;
2016}
2017
2018void ast_party_caller_set_init(struct ast_party_caller *init, const struct ast_party_caller *guide)
2019{
2020 ast_party_id_set_init(&init->id, &guide->id);
2021 ast_party_id_set_init(&init->ani, &guide->ani);
2022 ast_party_id_set_init(&init->priv, &guide->priv);
2023 init->ani2 = guide->ani2;
2024}
2025
2026void ast_party_caller_set(struct ast_party_caller *dest, const struct ast_party_caller *src, const struct ast_set_party_caller *update)
2027{
2028 ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
2029 ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
2030 ast_party_id_set(&dest->priv, &src->priv, update ? &update->priv : NULL);
2031 dest->ani2 = src->ani2;
2032}
2033
2035{
2036 ast_party_id_free(&doomed->id);
2037 ast_party_id_free(&doomed->ani);
2038 ast_party_id_free(&doomed->priv);
2039}
2040
2042{
2043 ast_party_id_init(&init->id);
2044 ast_party_id_init(&init->ani);
2045 ast_party_id_init(&init->priv);
2046 init->ani2 = 0;
2048}
2049
2051{
2052 if (dest == src) {
2053 /* Don't copy to self */
2054 return;
2055 }
2056
2057 ast_party_id_copy(&dest->id, &src->id);
2058 ast_party_id_copy(&dest->ani, &src->ani);
2059 ast_party_id_copy(&dest->priv, &src->priv);
2060 dest->ani2 = src->ani2;
2061 dest->source = src->source;
2062}
2063
2065{
2066 ast_party_id_set_init(&init->id, &guide->id);
2067 ast_party_id_set_init(&init->ani, &guide->ani);
2068 ast_party_id_set_init(&init->priv, &guide->priv);
2069 init->ani2 = guide->ani2;
2070 init->source = guide->source;
2071}
2072
2074{
2075 ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
2076 ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
2077 ast_party_id_set(&dest->priv, &src->priv, update ? &update->priv : NULL);
2078 dest->ani2 = src->ani2;
2079 dest->source = src->source;
2080}
2081
2083{
2084 connected->id = caller->id;
2085 connected->ani = caller->ani;
2086 connected->priv = caller->priv;
2087 connected->ani2 = caller->ani2;
2089}
2090
2092{
2093 ast_party_id_free(&doomed->id);
2094 ast_party_id_free(&doomed->ani);
2095 ast_party_id_free(&doomed->priv);
2096}
2097
2099{
2100 init->str = NULL;
2102}
2103
2105{
2106 if (dest == src) {
2107 return;
2108 }
2109
2110 ast_free(dest->str);
2111 dest->str = ast_strdup(src->str);
2112 dest->code = src->code;
2113}
2114
2116{
2117 init->str = NULL;
2118 init->code = guide->code;
2119}
2120
2122{
2123 if (dest == src) {
2124 return;
2125 }
2126
2127 if (src->str && src->str != dest->str) {
2128 ast_free(dest->str);
2129 dest->str = ast_strdup(src->str);
2130 }
2131
2132 dest->code = src->code;
2133}
2134
2136{
2137 ast_free(doomed->str);
2138}
2139
2140
2142{
2143 ast_party_id_init(&init->orig);
2144 ast_party_id_init(&init->from);
2145 ast_party_id_init(&init->to);
2148 ast_party_id_init(&init->priv_to);
2151 init->count = 0;
2152}
2153
2155{
2156 if (dest == src) {
2157 /* Don't copy to self */
2158 return;
2159 }
2160
2161 ast_party_id_copy(&dest->orig, &src->orig);
2162 ast_party_id_copy(&dest->from, &src->from);
2163 ast_party_id_copy(&dest->to, &src->to);
2164 ast_party_id_copy(&dest->priv_orig, &src->priv_orig);
2165 ast_party_id_copy(&dest->priv_from, &src->priv_from);
2166 ast_party_id_copy(&dest->priv_to, &src->priv_to);
2169 dest->count = src->count;
2170}
2171
2173{
2174 ast_party_id_set_init(&init->orig, &guide->orig);
2175 ast_party_id_set_init(&init->from, &guide->from);
2176 ast_party_id_set_init(&init->to, &guide->to);
2177 ast_party_id_set_init(&init->priv_orig, &guide->priv_orig);
2178 ast_party_id_set_init(&init->priv_from, &guide->priv_from);
2179 ast_party_id_set_init(&init->priv_to, &guide->priv_to);
2182 init->count = guide->count;
2183}
2184
2186{
2187 ast_party_id_set(&dest->orig, &src->orig, update ? &update->orig : NULL);
2188 ast_party_id_set(&dest->from, &src->from, update ? &update->from : NULL);
2189 ast_party_id_set(&dest->to, &src->to, update ? &update->to : NULL);
2190 ast_party_id_set(&dest->priv_orig, &src->priv_orig, update ? &update->priv_orig : NULL);
2191 ast_party_id_set(&dest->priv_from, &src->priv_from, update ? &update->priv_from : NULL);
2192 ast_party_id_set(&dest->priv_to, &src->priv_to, update ? &update->priv_to : NULL);
2195 dest->count = src->count;
2196}
2197
2199{
2200 ast_party_id_free(&doomed->orig);
2201 ast_party_id_free(&doomed->from);
2202 ast_party_id_free(&doomed->to);
2203 ast_party_id_free(&doomed->priv_orig);
2204 ast_party_id_free(&doomed->priv_from);
2205 ast_party_id_free(&doomed->priv_to);
2208}
2209
2210/*! \brief Free a channel structure */
2211static void ast_channel_destructor(void *obj)
2212{
2213 struct ast_channel *chan = obj;
2214 struct ast_var_t *vardata;
2215 struct ast_frame *f;
2216 struct varshead *headp;
2217 struct ast_datastore *datastore;
2218 char device_name[AST_CHANNEL_NAME];
2219 ast_callid callid;
2220
2221 ast_debug(1, "Channel %p '%s' destroying\n", chan, ast_channel_name(chan));
2222
2223 /* If there is native format music-on-hold state, free it */
2224 if (ast_channel_music_state(chan)) {
2225 ast_moh_cleanup(chan);
2226 }
2227
2229
2230 /* Things that may possibly raise Stasis messages shouldn't occur after this point */
2232
2234 /* A channel snapshot should not be in the process of being staged now. */
2236
2237 ast_channel_lock(chan);
2239 ast_channel_unlock(chan);
2240 }
2241
2242 ast_channel_lock(chan);
2243
2244 /* Get rid of each of the data stores on the channel */
2245 while ((datastore = AST_LIST_REMOVE_HEAD(ast_channel_datastores(chan), entry)))
2246 /* Free the data store */
2247 ast_datastore_free(datastore);
2248
2249 /* While the channel is locked, take the reference to its callid while we tear down the call. */
2250 callid = ast_channel_callid(chan);
2252
2253 ast_channel_unlock(chan);
2254
2255 /* Lock and unlock the channel just to be sure nobody has it locked still
2256 due to a reference that was stored in a datastore. (i.e. app_chanspy) */
2257 ast_channel_lock(chan);
2258 ast_channel_unlock(chan);
2259
2260 if (ast_channel_tech_pvt(chan)) {
2261 ast_log_callid(LOG_WARNING, callid, "Channel '%s' may not have been hung up properly\n", ast_channel_name(chan));
2263 }
2264
2265 if (ast_channel_sched(chan)) {
2267 }
2268
2270 char *dashptr;
2271
2272 ast_copy_string(device_name, ast_channel_name(chan), sizeof(device_name));
2273 if ((dashptr = strrchr(device_name, '-'))) {
2274 *dashptr = '\0';
2275 }
2276 } else {
2277 device_name[0] = '\0';
2278 }
2279
2280 /* Free translators */
2281 if (ast_channel_readtrans(chan))
2283 if (ast_channel_writetrans(chan))
2285 if (ast_channel_pbx(chan))
2286 ast_log_callid(LOG_WARNING, callid, "PBX may not have been terminated properly on '%s'\n", ast_channel_name(chan));
2287
2288 /* Free formats */
2294
2300
2301 /* Close pipes if appropriate */
2303 if (ast_channel_timer(chan)) {
2306 }
2308 ast_frfree(f);
2309
2310 /* loop over the variables list, freeing all data and deleting list items */
2311 /* no need to lock the list, as the channel is already locked */
2312 headp = ast_channel_varshead(chan);
2313 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
2314 ast_var_delete(vardata);
2315
2317
2318 /* Destroy the jitterbuffer */
2319 ast_jb_destroy(chan);
2320
2321 if (ast_channel_cdr(chan)) {
2324 }
2325
2326 if (ast_channel_zone(chan)) {
2328 }
2329
2331
2332 if (device_name[0]) {
2333 /*
2334 * We have a device name to notify of a new state.
2335 *
2336 * Queue an unknown state, because, while we know that this particular
2337 * instance is dead, we don't know the state of all other possible
2338 * instances.
2339 */
2341 }
2342
2344
2347
2349}
2350
2351/*! \brief Free a dummy channel structure */
2352static void ast_dummy_channel_destructor(void *obj)
2353{
2354 struct ast_channel *chan = obj;
2355 struct ast_datastore *datastore;
2356 struct ast_var_t *vardata;
2357 struct varshead *headp;
2358
2360
2361 /* Get rid of each of the data stores on the channel */
2362 while ((datastore = AST_LIST_REMOVE_HEAD(ast_channel_datastores(chan), entry))) {
2363 /* Free the data store */
2364 ast_datastore_free(datastore);
2365 }
2366
2372
2373 /* loop over the variables list, freeing all data and deleting list items */
2374 /* no need to lock the list, as the channel is already locked */
2375 headp = ast_channel_varshead(chan);
2376 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
2377 ast_var_delete(vardata);
2378
2379 if (ast_channel_cdr(chan)) {
2382 }
2383
2385}
2386
2388{
2389 struct ast_datastore *datastore = NULL, *datastore2;
2390
2392 if (datastore->inheritance > 0) {
2393 datastore2 = ast_datastore_alloc(datastore->info, datastore->uid);
2394 if (datastore2) {
2395 datastore2->data = datastore->info->duplicate ? datastore->info->duplicate(datastore->data) : NULL;
2396 datastore2->inheritance = datastore->inheritance == DATASTORE_INHERIT_FOREVER ? DATASTORE_INHERIT_FOREVER : datastore->inheritance - 1;
2398 }
2399 }
2400 }
2401 return 0;
2402}
2403
2404int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
2405{
2406 int res = 0;
2407
2409
2410 return res;
2411}
2412
2413int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
2414{
2415 return AST_LIST_REMOVE(ast_channel_datastores(chan), datastore, entry) ? 0 : -1;
2416}
2417
2418struct ast_datastore *ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
2419{
2420 struct ast_datastore *datastore = NULL;
2421
2422 if (info == NULL)
2423 return NULL;
2424
2426 if (datastore->info != info) {
2427 continue;
2428 }
2429
2430 if (uid == NULL) {
2431 /* matched by type only */
2432 break;
2433 }
2434
2435 if ((datastore->uid != NULL) && !strcasecmp(uid, datastore->uid)) {
2436 /* Matched by type AND uid */
2437 break;
2438 }
2439 }
2440
2441 return datastore;
2442}
2443
2444/*! Set the file descriptor on the channel */
2445void ast_channel_set_fd(struct ast_channel *chan, int which, int fd)
2446{
2447 ast_channel_internal_fd_set(chan, which, fd);
2448 return;
2449}
2450
2452{
2453 ast_channel_lock(chan);
2454
2456
2458 struct ast_frame *fr;
2459
2460 /* If we have completely cleared the softhangup flag,
2461 * then we need to fully abort the hangup process. This requires
2462 * pulling the END_OF_Q frame out of the channel frame queue if it
2463 * still happens to be there. */
2464
2465 fr = AST_LIST_LAST(ast_channel_readq(chan));
2466 if (fr && fr->frametype == AST_FRAME_CONTROL &&
2469 ast_frfree(fr);
2470 }
2471 }
2472
2473 ast_channel_unlock(chan);
2474}
2475
2476/*! \brief Softly hangup a channel, don't lock */
2477int ast_softhangup_nolock(struct ast_channel *chan, int cause)
2478{
2479 ast_debug(1, "Soft-Hanging (%#04x) up channel '%s'\n", (unsigned)cause, ast_channel_name(chan));
2480 /* Inform channel driver that we need to be hung up, if it cares */
2483 /* Interrupt any poll call or such */
2485 pthread_kill(ast_channel_blocker(chan), SIGURG);
2486 return 0;
2487}
2488
2489/*! \brief Softly hangup a channel, lock */
2490int ast_softhangup(struct ast_channel *chan, int cause)
2491{
2492 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
2493 int res;
2494
2495 ast_channel_lock(chan);
2496 res = ast_softhangup_nolock(chan, cause);
2497 blob = ast_json_pack("{s: i, s: b}",
2498 "cause", cause,
2499 "soft", 1);
2501 ast_channel_unlock(chan);
2502
2503 return res;
2504}
2505
2506static void free_translation(struct ast_channel *clonechan)
2507{
2508 if (ast_channel_writetrans(clonechan)) {
2510 }
2511 if (ast_channel_readtrans(clonechan)) {
2513 }
2514 ast_channel_writetrans_set(clonechan, NULL);
2515 ast_channel_readtrans_set(clonechan, NULL);
2516}
2517
2518void ast_set_hangupsource(struct ast_channel *chan, const char *source, int force)
2519{
2521
2522 ast_channel_lock(chan);
2523 if (force || ast_strlen_zero(ast_channel_hangupsource(chan))) {
2524 ast_channel_hangupsource_set(chan, source);
2525 }
2526 ast_channel_unlock(chan);
2527
2528 if (bridge) {
2529 ast_channel_lock(bridge);
2530 if (force || ast_strlen_zero(ast_channel_hangupsource(bridge))) {
2531 ast_channel_hangupsource_set(bridge, source);
2532 }
2533 ast_channel_unlock(bridge);
2534 }
2535}
2536
2538{
2541}
2542
2544{
2547}
2548
2549static void destroy_hooks(struct ast_channel *chan)
2550{
2551 if (ast_channel_audiohooks(chan)) {
2554 }
2555
2557}
2558
2559/*! \brief Hangup a channel */
2560void ast_hangup(struct ast_channel *chan)
2561{
2562 /* Be NULL safe for RAII_VAR() usage. */
2563 if (!chan) {
2564 return;
2565 }
2566
2567 ast_debug(1, "Channel %p '%s' hanging up. Refs: %d\n", chan, ast_channel_name(chan),
2568 ao2_ref(chan, 0));
2569
2571
2572 ast_channel_lock(chan);
2573
2574 while (ast_channel_masq(chan) || ast_channel_masqr(chan)) {
2576 }
2577
2578 /* Mark as a zombie so a masquerade cannot be setup on this channel. */
2580
2581 ast_channel_unlock(chan);
2582
2583 /*
2584 * XXX if running the hangup handlers here causes problems
2585 * because the handlers take too long to execute, we could move
2586 * the meat of this function into another thread. A thread
2587 * where channels go to die.
2588 *
2589 * If this is done, ast_autoservice_chan_hangup_peer() will no
2590 * longer be needed.
2591 */
2593 ao2_unlink(channels, chan);
2594 ast_channel_lock(chan);
2595
2596 destroy_hooks(chan);
2597
2598 free_translation(chan);
2599 /* Close audio stream */
2600 if (ast_channel_stream(chan)) {
2603 }
2604 /* Close video stream */
2605 if (ast_channel_vstream(chan)) {
2608 }
2609 if (ast_channel_sched(chan)) {
2612 }
2613
2614 if (ast_channel_generatordata(chan)) { /* Clear any tone stuff remaining */
2615 if (ast_channel_generator(chan) && ast_channel_generator(chan)->release) {
2617 }
2618 }
2621
2623 ast_log(LOG_WARNING, "Hard hangup called by thread LWP %d on %s, while blocked by thread LWP %d in procedure %s! Expect a failure\n",
2625 ast_channel_blockproc(chan));
2626 ast_assert(0);
2627 }
2628
2629 if (ast_channel_tech(chan)->hangup) {
2630 ast_channel_tech(chan)->hangup(chan);
2631 }
2632
2633 ast_channel_unlock(chan);
2634
2635 ast_cc_offer(chan);
2636
2637 ast_channel_unref(chan);
2638}
2639
2640/*!
2641 * \internal
2642 * \brief Set channel answered time if not already set.
2643 * \since 13.11.0
2644 *
2645 * \param chan Channel to set answered time.
2646 */
2647static void set_channel_answer_time(struct ast_channel *chan)
2648{
2650 struct timeval answertime;
2651
2652 answertime = ast_tvnow();
2653 ast_channel_answertime_set(chan, &answertime);
2654 }
2655}
2656
2657
2659{
2660 int res = 0;
2661 SCOPE_TRACE(1, "%s\n", ast_channel_name(chan));
2662
2663 ast_channel_lock(chan);
2664
2665 /* You can't answer an outbound call */
2667 ast_channel_unlock(chan);
2668 return 0;
2669 }
2670
2671 /* Stop if we're a zombie or need a soft hangup */
2673 ast_channel_unlock(chan);
2674 return -1;
2675 }
2676
2677 /*
2678 * Mark when incoming channel answered so we can know how
2679 * long the channel has been up.
2680 */
2682
2683 ast_channel_unlock(chan);
2684
2685 switch (ast_channel_state(chan)) {
2686 case AST_STATE_RINGING:
2687 case AST_STATE_RING:
2688 ast_channel_lock(chan);
2689 if (ast_channel_tech(chan)->answer_with_stream_topology) {
2690 res = ast_channel_tech(chan)->answer_with_stream_topology(chan, topology);
2691
2692 } else if (ast_channel_tech(chan)->answer) {
2693 res = ast_channel_tech(chan)->answer(chan);
2694 }
2696 ast_channel_unlock(chan);
2697 break;
2698 case AST_STATE_UP:
2699 break;
2700 default:
2701 break;
2702 }
2703
2704 ast_indicate(chan, -1);
2705
2706 return res;
2707}
2708
2710{
2712}
2713
2714int __ast_answer(struct ast_channel *chan, unsigned int delay)
2715{
2716 int res = 0;
2717 enum ast_channel_state old_state;
2718 SCOPE_TRACE(1, "%s\n", ast_channel_name(chan));
2719
2720 old_state = ast_channel_state(chan);
2721 if ((res = ast_raw_answer(chan))) {
2722 return res;
2723 }
2724
2725 switch (old_state) {
2726 case AST_STATE_RINGING:
2727 case AST_STATE_RING:
2728 /* wait for media to start flowing, but don't wait any longer
2729 * than 'delay' or 500 milliseconds, whichever is longer
2730 */
2731 do {
2733 struct ast_frame *cur;
2734 struct ast_frame *new_frame;
2735 int timeout_ms = MAX(delay, 500);
2736 unsigned int done = 0;
2737 struct timeval start;
2738
2740
2741 start = ast_tvnow();
2742 for (;;) {
2743 int ms = ast_remaining_ms(start, timeout_ms);
2744 ms = ast_waitfor(chan, ms);
2745 if (ms < 0) {
2746 ast_log(LOG_WARNING, "Error condition occurred when polling channel %s for a voice frame: %s\n", ast_channel_name(chan), strerror(errno));
2747 res = -1;
2748 break;
2749 }
2750 if (ms == 0) {
2751 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));
2752 break;
2753 }
2754 cur = ast_read(chan);
2755 if (!cur || ((cur->frametype == AST_FRAME_CONTROL) &&
2756 (cur->subclass.integer == AST_CONTROL_HANGUP))) {
2757 if (cur) {
2758 ast_frfree(cur);
2759 }
2760 res = -1;
2761 ast_debug(2, "Hangup of channel %s detected in answer routine\n", ast_channel_name(chan));
2762 break;
2763 }
2764
2765 if ((new_frame = ast_frisolate(cur)) != cur) {
2766 ast_frfree(cur);
2767 }
2768
2770
2771 /* if a specific delay period was requested, continue
2772 * until that delay has passed. don't stop just because
2773 * incoming media has arrived.
2774 */
2775 if (delay) {
2776 continue;
2777 }
2778
2779 switch (new_frame->frametype) {
2780 /* all of these frametypes qualify as 'media' */
2781 case AST_FRAME_VOICE:
2782 case AST_FRAME_VIDEO:
2783 case AST_FRAME_TEXT:
2786 case AST_FRAME_DTMF_END:
2787 case AST_FRAME_IMAGE:
2788 case AST_FRAME_HTML:
2789 case AST_FRAME_MODEM:
2790 case AST_FRAME_RTCP:
2791 done = 1;
2792 break;
2793 case AST_FRAME_CONTROL:
2794 case AST_FRAME_IAX:
2797 case AST_FRAME_NULL:
2798 case AST_FRAME_CNG:
2799 break;
2800 }
2801
2802 if (done) {
2803 break;
2804 }
2805 }
2806
2807 ast_channel_lock(chan);
2808 while ((cur = AST_LIST_REMOVE_HEAD(&frames, frame_list))) {
2809 if (res == 0) {
2810 ast_queue_frame_head(chan, cur);
2811 }
2812 ast_frfree(cur);
2813 }
2814 ast_channel_unlock(chan);
2815 } while (0);
2816 break;
2817 default:
2818 break;
2819 }
2820
2821 return res;
2822}
2823
2824int ast_answer(struct ast_channel *chan)
2825{
2826 SCOPE_TRACE(1, "%s\n", ast_channel_name(chan));
2827 return __ast_answer(chan, 0);
2828}
2829
2830inline int ast_auto_answer(struct ast_channel *chan)
2831{
2832 if (ast_channel_state(chan) == AST_STATE_UP) {
2833 /* Already answered */
2834 return 0;
2835 }
2836 return ast_answer(chan);
2837}
2838
2840{
2841 ast_assert(NULL != chan);
2842
2844 return 0;
2845 }
2847}
2848
2850{
2851 return (ast_channel_get_duration_ms(chan) / 1000);
2852}
2853
2855{
2856 ast_assert(NULL != chan);
2857
2859 return 0;
2860 }
2862}
2863
2865{
2866 return (ast_channel_get_up_time_ms(chan) / 1000);
2867}
2868
2869/*!
2870 * \brief Determine whether or not we have to trigger dtmf emulating using 50 fps timer events
2871 * especially when no voice frames are received during dtmf processing (direct media or muted
2872 * sender case using SIP INFO)
2873 */
2874static inline int should_trigger_dtmf_emulating(struct ast_channel *chan)
2875{
2877 /* We're in the middle of emulating a digit, or DTMF has been
2878 * explicitly deferred. Trigger dtmf with periodic 50 pfs timer events, then. */
2879 return 1;
2880 }
2881
2882 if (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
2884 /*
2885 * We're not in the middle of a digit, but it hasn't been long enough
2886 * since the last digit, so we'll have to trigger DTMF further on.
2887 * Using 2 times AST_MIN_DTMF_GAP to trigger readq reading for possible
2888 * buffered next dtmf event
2889 */
2890 return 1;
2891 }
2892
2893 return 0;
2894}
2895
2897{
2898 if (ast_channel_generatordata(chan)) {
2899 struct ast_generator *generator = ast_channel_generator(chan);
2900
2901 if (generator && generator->release) {
2902 generator->release(chan, ast_channel_generatordata(chan));
2903 }
2908 ast_settimeout(chan, 0, NULL, NULL);
2909 }
2910}
2911
2913{
2914 ast_channel_lock(chan);
2917 /* if in the middle of dtmf emulation keep 50 tick per sec timer on rolling */
2919 }
2920 ast_channel_unlock(chan);
2921}
2922
2924{
2925 struct ast_generator *generator;
2926
2927 ast_channel_lock(chan);
2928 generator = ast_channel_generator(chan);
2929 if (generator && generator->write_format_change) {
2930 generator->write_format_change(chan, ast_channel_generatordata(chan));
2931 }
2932 ast_channel_unlock(chan);
2933}
2934
2935static int generator_force(const void *data)
2936{
2937 /* Called if generator doesn't have data */
2938 void *tmp;
2939 int res;
2940 int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples) = NULL;
2941 struct ast_channel *chan = (struct ast_channel *)data;
2942
2943 ast_channel_lock(chan);
2946 if (ast_channel_generator(chan))
2947 generate = ast_channel_generator(chan)->generate;
2948 ast_channel_unlock(chan);
2949
2950 if (!tmp || !generate) {
2951 return 0;
2952 }
2953
2954 res = generate(chan, tmp, 0, ast_format_get_sample_rate(ast_channel_writeformat(chan)) / 50);
2955
2956 ast_channel_lock(chan);
2957 if (ast_channel_generator(chan) && generate == ast_channel_generator(chan)->generate) {
2959 }
2960 ast_channel_unlock(chan);
2961
2962 if (res) {
2963 ast_debug(1, "Auto-deactivating generator\n");
2965 }
2966
2967 return 0;
2968}
2969
2970int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen, void *params)
2971{
2972 int res = 0;
2973 void *generatordata = NULL;
2974
2975 ast_channel_lock(chan);
2976 if (ast_channel_generatordata(chan)) {
2977 struct ast_generator *generator_old = ast_channel_generator(chan);
2978
2979 if (generator_old && generator_old->release) {
2980 generator_old->release(chan, ast_channel_generatordata(chan));
2981 }
2982 }
2983 if (gen->alloc && !(generatordata = gen->alloc(chan, params))) {
2984 res = -1;
2985 }
2986 ast_channel_generatordata_set(chan, generatordata);
2987 if (!res) {
2988 ast_settimeout(chan, 50, generator_force, chan);
2990 }
2991 ast_channel_unlock(chan);
2992
2993 ast_prod(chan);
2994
2995 return res;
2996}
2997
2998/*! \brief Wait for x amount of time on a file descriptor to have input. */
2999int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception)
3000{
3001 int winner = -1;
3002 ast_waitfor_nandfds(NULL, 0, fds, n, exception, &winner, ms);
3003 return winner;
3004}
3005
3006/*! \brief Wait for x amount of time on a file descriptor to have input. */
3007struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
3008 int *exception, int *outfd, int *ms)
3009{
3010 struct timeval start = { 0 , 0 };
3011 struct pollfd *pfds = NULL;
3012 int res;
3013 long rms;
3014 int x, y, max;
3015 int sz = nfds;
3016 struct timeval now = { 0, 0 };
3017 struct timeval whentohangup = { 0, 0 }, diff;
3018 struct ast_channel *winner = NULL;
3019 struct fdmap {
3020 int chan;
3021 int fdno;
3022 } *fdmap = NULL;
3023
3024 if (outfd) {
3025 *outfd = -99999;
3026 }
3027 if (exception) {
3028 *exception = 0;
3029 }
3030
3031 for (x = 0; x < n; x++) {
3032 ast_channel_lock(c[x]);
3034 if (ast_tvzero(whentohangup))
3035 now = ast_tvnow();
3036 diff = ast_tvsub(*ast_channel_whentohangup(c[x]), now);
3037 if (diff.tv_sec < 0 || ast_tvzero(diff)) {
3038 ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(c[x]));
3039 /* Should already be hungup */
3042 return c[x];
3043 }
3044 if (ast_tvzero(whentohangup) || ast_tvcmp(diff, whentohangup) < 0)
3045 whentohangup = diff;
3046 }
3047 sz += ast_channel_fd_count(c[x]);
3049 }
3050
3051 if (!sz) {
3052 return NULL;
3053 }
3054
3055 pfds = ast_alloca(sizeof(*pfds) * sz);
3056 fdmap = ast_alloca(sizeof(*fdmap) * sz);
3057
3058 /* Wait full interval */
3059 rms = *ms;
3060 /* INT_MAX, not LONG_MAX, because it matters on 64-bit */
3061 if (!ast_tvzero(whentohangup) && whentohangup.tv_sec < INT_MAX / 1000) {
3062 rms = whentohangup.tv_sec * 1000 + whentohangup.tv_usec / 1000; /* timeout in milliseconds */
3063 if (*ms >= 0 && *ms < rms) { /* original *ms still smaller */
3064 rms = *ms;
3065 }
3066 } else if (!ast_tvzero(whentohangup) && rms < 0) {
3067 /* Tiny corner case... call would need to last >24 days */
3068 rms = INT_MAX;
3069 }
3070 /*
3071 * Build the pollfd array, putting the channels' fds first,
3072 * followed by individual fds. Order is important because
3073 * individual fd's must have priority over channel fds.
3074 */
3075 max = 0;
3076 for (x = 0; x < n; x++) {
3077 ast_channel_lock(c[x]);
3078 for (y = 0; y < ast_channel_fd_count(c[x]); y++) {
3079 fdmap[max].fdno = y; /* fd y is linked to this pfds */
3080 fdmap[max].chan = x; /* channel x is linked to this pfds */
3081 max += ast_add_fd(&pfds[max], ast_channel_fd(c[x], y));
3082 }
3083 CHECK_BLOCKING(c[x]);
3085 }
3086 /* Add the individual fds */
3087 for (x = 0; x < nfds; x++) {
3088 fdmap[max].chan = -1;
3089 max += ast_add_fd(&pfds[max], fds[x]);
3090 }
3091
3092 if (*ms > 0) {
3093 start = ast_tvnow();
3094 }
3095
3096 if (sizeof(int) == 4) { /* XXX fix timeout > 600000 on linux x86-32 */
3097 do {
3098 int kbrms = rms;
3099 if (kbrms > 600000) {
3100 kbrms = 600000;
3101 }
3102 res = ast_poll(pfds, max, kbrms);
3103 if (!res) {
3104 rms -= kbrms;
3105 }
3106 } while (!res && (rms > 0));
3107 } else {
3108 res = ast_poll(pfds, max, rms);
3109 }
3110 for (x = 0; x < n; x++) {
3111 ast_channel_lock(c[x]);
3114 }
3115 if (res < 0) { /* Simulate a timeout if we were interrupted */
3116 if (errno != EINTR) {
3117 *ms = -1;
3118 }
3119 return NULL;
3120 }
3121 if (!ast_tvzero(whentohangup)) { /* if we have a timeout, check who expired */
3122 now = ast_tvnow();
3123 for (x = 0; x < n; x++) {
3125 ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(c[x]));
3127 if (winner == NULL) {
3128 winner = c[x];
3129 }
3130 }
3131 }
3132 }
3133 if (res == 0) { /* no fd ready, reset timeout and done */
3134 *ms = 0; /* XXX use 0 since we may not have an exact timeout. */
3135 return winner;
3136 }
3137 /*
3138 * Then check if any channel or fd has a pending event.
3139 * Remember to check channels first and fds last, as they
3140 * must have priority on setting 'winner'
3141 */
3142 for (x = 0; x < max; x++) {
3143 res = pfds[x].revents;
3144 if (res == 0) {
3145 continue;
3146 }
3147 if (fdmap[x].chan >= 0) { /* this is a channel */
3148 winner = c[fdmap[x].chan]; /* override previous winners */
3149 ast_channel_lock(winner);
3150 if (res & POLLPRI) {
3152 } else {
3154 }
3155 ast_channel_fdno_set(winner, fdmap[x].fdno);
3156 ast_channel_unlock(winner);
3157 } else { /* this is an fd */
3158 if (outfd) {
3159 *outfd = pfds[x].fd;
3160 }
3161 if (exception) {
3162 *exception = (res & POLLPRI) ? -1 : 0;
3163 }
3164 winner = NULL;
3165 }
3166 }
3167 if (*ms > 0) {
3168 *ms -= ast_tvdiff_ms(ast_tvnow(), start);
3169 if (*ms < 0) {
3170 *ms = 0;
3171 }
3172 }
3173 return winner;
3174}
3175
3176struct ast_channel *ast_waitfor_n(struct ast_channel **c, int n, int *ms)
3177{
3178 return ast_waitfor_nandfds(c, n, NULL, 0, NULL, NULL, ms);
3179}
3180
3181int ast_waitfor(struct ast_channel *c, int ms)
3182{
3183 if (ms < 0) {
3184 do {
3185 ms = 100000;
3186 ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
3187 } while (!ms);
3188 } else {
3189 ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
3190 }
3191 return ms;
3192}
3193
3194int ast_waitfordigit(struct ast_channel *c, int ms)
3195{
3196 return ast_waitfordigit_full(c, ms, NULL, -1, -1);
3197}
3198
3199int ast_settimeout(struct ast_channel *c, unsigned int rate, int (*func)(const void *data), void *data)
3200{
3201 return ast_settimeout_full(c, rate, func, data, 0);
3202}
3203
3204int ast_settimeout_full(struct ast_channel *c, unsigned int rate, int (*func)(const void *data), void *data, unsigned int is_ao2_obj)
3205{
3206 int res;
3207 unsigned int real_rate = rate, max_rate;
3208
3210
3211 if (ast_channel_timingfd(c) == -1) {
3213 return -1;
3214 }
3215
3216 if (!func) {
3217 rate = 0;
3218 data = NULL;
3219 }
3220
3221 if (rate && rate > (max_rate = ast_timer_get_max_rate(ast_channel_timer(c)))) {
3222 real_rate = max_rate;
3223 }
3224
3225 ast_debug(3, "Scheduling timer at (%u requested / %u actual) timer ticks per second\n", rate, real_rate);
3226
3227 res = ast_timer_set_rate(ast_channel_timer(c), real_rate);
3228
3231 }
3232
3235
3236 if (data && is_ao2_obj) {
3237 ao2_ref(data, 1);
3239 } else {
3241 }
3242
3243 if (func == NULL && rate == 0 && ast_channel_fdno(c) == AST_TIMING_FD) {
3244 /* Clearing the timing func and setting the rate to 0
3245 * means that we don't want to be reading from the timingfd
3246 * any more. Setting c->fdno to -1 means we won't have any
3247 * errant reads from the timingfd, meaning we won't potentially
3248 * miss any important frames.
3249 */
3251 }
3252
3254
3255 return res;
3256}
3257
3258int ast_waitfordigit_full(struct ast_channel *c, int timeout_ms, const char *breakon, int audiofd, int cmdfd)
3259{
3260 struct timeval start = ast_tvnow();
3261 int ms;
3262
3263 /* Stop if we're a zombie or need a soft hangup */
3265 return -1;
3266
3267 /* Only look for the end of DTMF, don't bother with the beginning and don't emulate things */
3269
3270 /* Wait for a digit, no more than timeout_ms milliseconds total.
3271 * Or, wait indefinitely if timeout_ms is <0.
3272 */
3273 while ((ms = ast_remaining_ms(start, timeout_ms))) {
3274 struct ast_channel *rchan;
3275 int outfd = -1;
3276
3277 errno = 0;
3278 /* While ast_waitfor_nandfds tries to help by reducing the timeout by how much was waited,
3279 * it is unhelpful if it waited less than a millisecond.
3280 */
3281 rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
3282
3283 if (!rchan && outfd < 0 && ms) {
3284 if (errno == 0 || errno == EINTR)
3285 continue;
3286 ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
3288 return -1;
3289 } else if (outfd > -1) {
3290 /* The FD we were watching has something waiting */
3291 ast_log(LOG_WARNING, "The FD we were waiting for has something waiting. Waitfordigit returning numeric 1\n");
3293 return 1;
3294 } else if (rchan) {
3295 int res;
3296 struct ast_frame *f = ast_read(c);
3297
3298 if (!f) {
3300
3301 return -1;
3302 }
3303
3304 switch (f->frametype) {
3306 break;
3307 case AST_FRAME_DTMF_END:
3308 res = f->subclass.integer;
3309 if (!breakon || strchr(breakon, res)) {
3310 ast_frfree(f);
3312 return res;
3313 }
3314 break;
3315 case AST_FRAME_CONTROL:
3316 switch (f->subclass.integer) {
3317 case AST_CONTROL_HANGUP:
3318 ast_frfree(f);
3320 return -1;
3326 /* Fall-through and treat as if it were a DTMF signal. Items
3327 * that perform stream control will handle this. */
3328 res = f->subclass.integer;
3329 ast_frfree(f);
3331 return res;
3335 case AST_CONTROL_ANSWER:
3341 case AST_CONTROL_HOLD:
3342 case AST_CONTROL_UNHOLD:
3343 case AST_CONTROL_FLASH:
3344 case -1:
3345 /* Unimportant */
3346 break;
3347 default:
3348 ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", f->subclass.integer);
3349 break;
3350 }
3351 break;
3352 case AST_FRAME_VOICE:
3353 /* Write audio if appropriate */
3354 if (audiofd > -1) {
3355 if (write(audiofd, f->data.ptr, f->datalen) < 0) {
3356 ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
3357 }
3358 }
3359 default:
3360 /* Ignore */
3361 break;
3362 }
3363 ast_frfree(f);
3364 }
3365 }
3366
3368
3369 return 0; /* Time is up */
3370}
3371
3374 DTMF_SENT
3376
3378{
3379 switch (direction) {
3380 case DTMF_RECEIVED:
3381 return "Received";
3382 case DTMF_SENT:
3383 return "Sent";
3384 }
3385
3386 return "?";
3387}
3388
3389static void send_dtmf_begin_event(struct ast_channel *chan,
3390 enum DtmfDirection direction, const char digit)
3391{
3392 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
3393 char digit_str[] = { digit, '\0' };
3394
3395 blob = ast_json_pack("{ s: s, s: s }",
3396 "digit", digit_str,
3397 "direction", dtmf_direction_to_string(direction));
3398 if (!blob) {
3399 return;
3400 }
3401
3403}
3404
3405static void send_dtmf_end_event(struct ast_channel *chan,
3406 enum DtmfDirection direction, const char digit, long duration_ms)
3407{
3408 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
3409 char digit_str[] = { digit, '\0' };
3410
3411 blob = ast_json_pack("{ s: s, s: s, s: I }",
3412 "digit", digit_str,
3413 "direction", dtmf_direction_to_string(direction),
3414 "duration_ms", (ast_json_int_t)duration_ms);
3415 if (!blob) {
3416 return;
3417 }
3418
3420}
3421
3422static void send_flash_event(struct ast_channel *chan)
3423{
3425}
3426
3427static void send_wink_event(struct ast_channel *chan)
3428{
3430}
3431
3432static void ast_read_generator_actions(struct ast_channel *chan, struct ast_frame *f)
3433{
3434 struct ast_generator *generator;
3435 void *gendata;
3436 int res;
3437 int samples;
3438
3439 generator = ast_channel_generator(chan);
3440 if (!generator
3441 || !generator->generate
3442 || f->frametype != AST_FRAME_VOICE
3444 || ast_channel_timingfunc(chan)) {
3445 return;
3446 }
3447
3448 /*
3449 * We must generate frames in phase locked mode since
3450 * we have no internal timer available.
3451 */
3453 float factor;
3455 samples = (int) (((float) f->samples) * factor);
3456 } else {
3457 samples = f->samples;
3458 }
3459
3460 gendata = ast_channel_generatordata(chan);
3461 ast_channel_generatordata_set(chan, NULL); /* reset, to let writes go through */
3462
3463 /*
3464 * This unlock is here based on two assumptions that hold true at
3465 * this point in the code. 1) this function is only called from
3466 * within __ast_read() and 2) all generators call ast_write() in
3467 * their generate callback.
3468 *
3469 * The reason this is added is so that when ast_write is called,
3470 * the lock that occurs there will not recursively lock the
3471 * channel. Doing this will allow deadlock avoidance to work in
3472 * deeper functions.
3473 */
3474 ast_channel_unlock(chan);
3475 res = generator->generate(chan, gendata, f->datalen, samples);
3476 ast_channel_lock(chan);
3477 if (generator == ast_channel_generator(chan)) {
3478 ast_channel_generatordata_set(chan, gendata);
3479 if (res) {
3480 ast_debug(1, "Auto-deactivating generator\n");
3482 }
3483 }
3484}
3485
3486static inline void queue_dtmf_readq(struct ast_channel *chan, struct ast_frame *f)
3487{
3488 struct ast_frame *fr = ast_channel_dtmff(chan);
3489
3492 fr->len = f->len;
3493
3494 /* The only time this function will be called is for a frame that just came
3495 * out of the channel driver. So, we want to stick it on the tail of the
3496 * readq. */
3497
3498 ast_queue_frame(chan, fr);
3499}
3500
3501/*!
3502 * \brief Determine whether or not we should ignore DTMF in the readq
3503 */
3504static inline int should_skip_dtmf(struct ast_channel *chan)
3505{
3507 /* We're in the middle of emulating a digit, or DTMF has been
3508 * explicitly deferred. Skip this digit, then. */
3509 return 1;
3510 }
3511
3512 if (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
3514 /* We're not in the middle of a digit, but it hasn't been long enough
3515 * since the last digit, so we'll have to skip DTMF for now. */
3516 return 1;
3517 }
3518
3519 return 0;
3520}
3521
3522/*!
3523 * \brief calculates the number of samples to jump forward with in a monitor stream.
3524
3525 * \note When using ast_seekstream() with the read and write streams of a monitor,
3526 * the number of samples to seek forward must be of the same sample rate as the stream
3527 * or else the jump will not be calculated correctly.
3528 *
3529 * \retval number of samples to seek forward after rate conversion.
3530 */
3531static inline int calc_monitor_jump(int samples, int sample_rate, int seek_rate)
3532{
3533 int diff = sample_rate - seek_rate;
3534
3535 if (diff > 0) {
3536 samples = samples / (float) (sample_rate / seek_rate);
3537 } else if (diff < 0) {
3538 samples = samples * (float) (seek_rate / sample_rate);
3539 }
3540
3541 return samples;
3542}
3543
3544static struct ast_frame *__ast_read(struct ast_channel *chan, int dropaudio, int dropnondefault)
3545{
3546 struct ast_frame *f = NULL; /* the return value */
3547 int prestate;
3548 int cause = 0;
3549 struct ast_stream *stream = NULL, *default_stream = NULL;
3550
3551 /* this function is very long so make sure there is only one return
3552 * point at the end (there are only two exceptions to this).
3553 */
3554 ast_channel_lock(chan);
3555
3556 /* Stop if we're a zombie or need a soft hangup */
3558 if (ast_channel_generator(chan))
3560
3561 /*
3562 * It is possible for chan->_softhangup to be set and there
3563 * still be control frames that need to be read. Instead of
3564 * just going to 'done' in the case of ast_check_hangup(), we
3565 * need to queue the end-of-Q frame so that it can mark the end
3566 * of the read queue. If there are frames to be read,
3567 * ast_queue_control() will be called repeatedly, but will only
3568 * queue the first end-of-Q frame.
3569 */
3572 } else {
3573 goto done;
3574 }
3575 } else {
3576#ifdef AST_DEVMODE
3577 /*
3578 * The ast_waitfor() code records which of the channel's file
3579 * descriptors reported that data is available. In theory,
3580 * ast_read() should only be called after ast_waitfor() reports
3581 * that a channel has data available for reading. However,
3582 * there still may be some edge cases throughout the code where
3583 * ast_read() is called improperly. This can potentially cause
3584 * problems, so if this is a developer build, make a lot of
3585 * noise if this happens so that it can be addressed.
3586 *
3587 * One of the potential problems is blocking on a dead channel.
3588 */
3589 if (ast_channel_fdno(chan) == -1) {
3591 "ast_read() on chan '%s' called with no recorded file descriptor.\n",
3592 ast_channel_name(chan));
3593 }
3594#endif
3595 }
3596
3597 prestate = ast_channel_state(chan);
3598
3599 if (ast_channel_timingfd(chan) > -1 && ast_channel_fdno(chan) == AST_TIMING_FD) {
3600 enum ast_timer_event res;
3601 int trigger_dtmf_emulating = should_trigger_dtmf_emulating(chan);
3602
3604
3606
3607 switch (res) {
3609 if (ast_timer_ack(ast_channel_timer(chan), 1) < 0) {
3610 ast_log(LOG_ERROR, "Failed to acknowledge timer in ast_read\n");
3611 goto done;
3612 }
3613
3614 if (ast_channel_timingfunc(chan)) {
3615 /* save a copy of func/data before unlocking the channel */
3617 void *data = ast_channel_timingdata(chan);
3618 int got_ref = 0;
3620 ao2_ref(data, 1);
3621 got_ref = 1;
3622 }
3623 ast_channel_fdno_set(chan, -1);
3624 ast_channel_unlock(chan);
3625 func(data);
3626 if (got_ref) {
3627 ao2_ref(data, -1);
3628 }
3629
3630 if (trigger_dtmf_emulating) {
3631 /*
3632 * Since we're breaking out of this switch block and not
3633 * returning, we need to re-lock the channel.
3634 */
3635 ast_channel_lock(chan);
3636 /* generate null frame to trigger dtmf emulating */
3637 f = &ast_null_frame;
3638 break;
3639 }
3640 } else if (trigger_dtmf_emulating) {
3641 /* generate null frame to trigger dtmf emulating */
3642 f = &ast_null_frame;
3643 break;
3644 } else {
3646 /* generate very last null frame to trigger dtmf emulating */
3647 f = &ast_null_frame;
3648 break;
3649 }
3650
3651 /* cannot 'goto done' because the channel is already unlocked */
3652 return &ast_null_frame;
3653
3655 if (AST_LIST_EMPTY(ast_channel_readq(chan)) ||
3658 }
3659 break;
3660 }
3661
3663 /* if the AST_GENERATOR_FD is set, call the generator with args
3664 * set to -1 so it can do whatever it needs to.
3665 */
3666 void *tmp = ast_channel_generatordata(chan);
3667 ast_channel_generatordata_set(chan, NULL); /* reset to let ast_write get through */
3668 ast_channel_generator(chan)->generate(chan, tmp, -1, -1);
3670 f = &ast_null_frame;
3671 ast_channel_fdno_set(chan, -1);
3672 goto done;
3675 }
3676
3677 /* Read and ignore anything on the alertpipe, but read only
3678 one sizeof(blah) per frame that we send from it */
3680 f = &ast_null_frame;
3681 goto done;
3682 }
3683
3684 /* Check for pending read queue */
3685 if (!AST_LIST_EMPTY(ast_channel_readq(chan))) {
3686 int skipped_dtmf_frame = 0;
3687 int skip_dtmf = should_skip_dtmf(chan);
3688
3690 /* We have to be picky about which frame we pull off of the readq because
3691 * there are cases where we want to leave DTMF frames on the queue until
3692 * some later time. */
3693
3694 if ( (f->frametype == AST_FRAME_DTMF_BEGIN || f->frametype == AST_FRAME_DTMF_END) && skip_dtmf) {
3695 skipped_dtmf_frame = 1;
3696 continue;
3697 }
3698
3700 break;
3701 }
3703
3704 if (!f) {
3705 /* There were no acceptable frames on the readq. */
3706 f = &ast_null_frame;
3707 if (!skipped_dtmf_frame) {
3708 /*
3709 * Do not trigger alert pipe if only buffered dtmf begin or end frames
3710 * are left in the readq.
3711 */
3713 } else {
3714 /*
3715 * Safely disable continous timer events if only buffered dtmf begin or end
3716 * frames are left in the readq.
3717 */
3719 }
3720 }
3721
3722 /* Interpret hangup and end-of-Q frames to return NULL */
3723 /* XXX why not the same for frames from the channel ? */
3724 if (f->frametype == AST_FRAME_CONTROL) {
3725 switch (f->subclass.integer) {
3726 case AST_CONTROL_HANGUP:
3728 cause = f->data.uint32;
3729 /* Fall through */
3731 ast_frfree(f);
3732 f = NULL;
3733 break;
3734 default:
3735 break;
3736 }
3737 } else if (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO) {
3738 if (ast_channel_tech(chan) && ast_channel_tech(chan)->read_stream) {
3741 } else {
3742 /* Since this channel driver does not support multistream determine the default stream this frame
3743 * originated from and update the frame to include it.
3744 */
3745 stream = default_stream = ast_channel_get_default_stream(chan, ast_format_get_type(f->subclass.format));
3746 /* In order to allow media to be passed up the underlying media type has to have a format negotiated on
3747 * the channel itself. In cases where this hasn't happened the channel driver is incorrectly passing up
3748 * a frame for a format that has not been negotiated. If this occurs just drop the frame as we have no
3749 * stream that it came from.
3750 */
3751 if (!stream) {
3752 ast_frfree(f);
3753 f = &ast_null_frame;
3754 } else {
3756 }
3757 }
3758 }
3759 } else {
3761 if (ast_channel_tech(chan)->exception)
3762 f = ast_channel_tech(chan)->exception(chan);
3763 else {
3764 ast_log(LOG_WARNING, "Exception flag set on '%s', but no exception handler\n", ast_channel_name(chan));
3765 f = &ast_null_frame;
3766 }
3767 /* Clear the exception flag */
3769 } else if (ast_channel_tech(chan) && ast_channel_tech(chan)->read_stream) {
3770 f = ast_channel_tech(chan)->read_stream(chan);
3771
3772 /* This channel driver supports multistream so the stream_num on the frame is valid, the only
3773 * thing different is that we need to find the default stream so we know whether to invoke the
3774 * default stream logic or not (such as transcoding).
3775 */
3776 if (f && (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO)) {
3779 }
3780 } else if (ast_channel_tech(chan) && ast_channel_tech(chan)->read) {
3781 f = ast_channel_tech(chan)->read(chan);
3782
3783 /* Since this channel driver does not support multistream determine the default stream this frame
3784 * originated from and update the frame to include it.
3785 */
3786 if (f && (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO)) {
3787 stream = default_stream = ast_channel_get_default_stream(chan, ast_format_get_type(f->subclass.format));
3788 if (!stream) {
3789 ast_frfree(f);
3790 f = &ast_null_frame;
3791 } else {
3793 }
3794 }
3795 }
3796 else
3797 ast_log(LOG_WARNING, "No read routine on channel %s\n", ast_channel_name(chan));
3798 }
3799
3800 if (stream == default_stream) {
3801 /* Perform the framehook read event here. After the frame enters the framehook list
3802 * there is no telling what will happen, <insert mad scientist laugh here>!!! */
3804 }
3805
3806 /*
3807 * Reset the recorded file descriptor that triggered this read so that we can
3808 * easily detect when ast_read() is called without properly using ast_waitfor().
3809 */
3810 ast_channel_fdno_set(chan, -1);
3811
3812 if (f) {
3813 struct ast_frame *readq_tail = AST_LIST_LAST(ast_channel_readq(chan));
3814 struct ast_control_read_action_payload *read_action_payload;
3816 int hooked = 0;
3817
3818 /* if the channel driver returned more than one frame, stuff the excess
3819 into the readq for the next ast_read call
3820 */
3821 if (AST_LIST_NEXT(f, frame_list)) {
3825 }
3826
3827 if (dropnondefault && stream != default_stream) {
3828 /* If the frame originates from a non-default stream and the caller can not handle other streams
3829 * absorb the frame and replace it with a null one instead.
3830 */
3831 ast_frfree(f);
3832 f = &ast_null_frame;
3833 }
3834
3835 switch (f->frametype) {
3836 case AST_FRAME_CONTROL:
3838 if (prestate == AST_STATE_UP && ast_channel_is_bridged(chan)) {
3839 ast_debug(1, "Dropping duplicate answer!\n");
3840 ast_frfree(f);
3841 f = &ast_null_frame;
3842 } else {
3843 /*
3844 * Mark when outgoing channel answered so we can know how
3845 * long the channel has been up.
3846 */
3848
3850 }
3851 } else if (f->subclass.integer == AST_CONTROL_READ_ACTION) {
3852 read_action_payload = f->data.ptr;
3853 switch (read_action_payload->action) {
3857 if (ast_connected_line_parse_data(read_action_payload->payload,
3858 read_action_payload->payload_size, &connected)) {
3860 break;
3861 }
3862 ast_channel_unlock(chan);
3865 read_action_payload->payload,
3866 read_action_payload->payload_size);
3867 }
3869 ast_channel_lock(chan);
3870 break;
3872 ast_channel_unlock(chan);
3873 ast_sendtext(chan, (const char *)read_action_payload->payload);
3874 ast_channel_lock(chan);
3875 break;
3877 ast_channel_unlock(chan);
3878 ast_sendtext_data(chan, (struct ast_msg_data *)read_action_payload->payload);
3879 ast_channel_lock(chan);
3880 break;
3881 }
3882 ast_frfree(f);
3883 f = &ast_null_frame;
3884 } else if (f->subclass.integer == AST_CONTROL_STREAM_TOPOLOGY_REQUEST_CHANGE && dropnondefault) {
3885 /* The caller of this function is incapable of handling streams so we don't accept the change request
3886 * and stick to the streams currently on the channel.
3887 */
3889 ast_frfree(f);
3890 f = &ast_null_frame;
3891 } else if (f->subclass.integer == AST_CONTROL_STREAM_TOPOLOGY_CHANGED && dropnondefault) {
3892 /* The caller of this function is incapable of handling streams so we absorb the notification that the
3893 * stream topology has changed.
3894 */
3895 ast_frfree(f);
3896 f = &ast_null_frame;
3897 } else if (f->subclass.integer == AST_CONTROL_FLASH) {
3898 send_flash_event(chan);
3899 } else if (f->subclass.integer == AST_CONTROL_WINK) {
3900 send_wink_event(chan);
3901 }
3902 break;
3903 case AST_FRAME_DTMF_END:
3905 ast_log(LOG_DTMF, "DTMF end '%c' received on %s, duration %ld ms\n", f->subclass.integer, ast_channel_name(chan), f->len);
3906 /* Queue it up if DTMF is deferred, or if DTMF emulation is forced. */
3908 queue_dtmf_readq(chan, f);
3909 ast_frfree(f);
3910 f = &ast_null_frame;
3912 if (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
3914 /* If it hasn't been long enough, defer this digit */
3915 queue_dtmf_readq(chan, f);
3916 ast_frfree(f);
3917 f = &ast_null_frame;
3918 } else {
3919 /* There was no begin, turn this into a begin and send the end later */
3920 struct timeval tv = ast_tvnow();
3924 ast_channel_dtmf_tv_set(chan, &tv);
3925 if (f->len) {
3926 if (f->len > option_dtmfminduration)
3928 else
3930 } else
3932 ast_log(LOG_DTMF, "DTMF begin emulation of '%c' with duration %u queued on %s\n", f->subclass.integer, ast_channel_emulate_dtmf_duration(chan), ast_channel_name(chan));
3933
3934 /*
3935 * Start generating 50 fps timer events (null frames) for dtmf emulating
3936 * independently from any existing incoming voice frames.
3937 * If channel generator is already activated in regular mode use these
3938 * timer events to generate null frames.
3939 */
3940 if (!ast_channel_generator(chan)) {
3942 }
3943 }
3944 if (ast_channel_audiohooks(chan)) {
3945 struct ast_frame *old_frame = f;
3946 /*!
3947 * \todo XXX It is possible to write a digit to the audiohook twice
3948 * if the digit was originally read while the channel was in autoservice. */
3950 if (old_frame != f)
3951 ast_frfree(old_frame);
3952 }
3953 } else {
3954 struct timeval now = ast_tvnow();
3956 ast_log(LOG_DTMF, "DTMF end accepted with begin '%c' on %s\n", f->subclass.integer, ast_channel_name(chan));
3958 if (!f->len)
3959 f->len = ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan));
3960
3961 /* detect tones that were received on
3962 * the wire with durations shorter than
3963 * option_dtmfminduration and set f->len
3964 * to the actual duration of the DTMF
3965 * frames on the wire. This will cause
3966 * dtmf emulation to be triggered later
3967 * on.
3968 */
3970 f->len = ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan));
3971 ast_log(LOG_DTMF, "DTMF end '%c' detected to have actual duration %ld on the wire, emulation will be triggered on %s\n", f->subclass.integer, f->len, ast_channel_name(chan));
3972 }
3973 } else if (!f->len) {
3974 ast_log(LOG_DTMF, "DTMF end accepted without begin '%c' on %s\n", f->subclass.integer, ast_channel_name(chan));
3976 }
3978 ast_log(LOG_DTMF, "DTMF end '%c' has duration %ld but want minimum %u, emulating on %s\n", f->subclass.integer, f->len, option_dtmfminduration, ast_channel_name(chan));
3982 ast_frfree(f);
3983 f = &ast_null_frame;
3984
3985 /* Start generating 50 fps timer events (null frames) for dtmf emulating
3986 * independently from any existing incoming voice frames.
3987 * If channel generator is already activated in regular mode use these
3988 * timer events to generate null frames.
3989 */
3990 if (!ast_channel_generator(chan)) {
3992 }
3993 } else {
3994 ast_log(LOG_DTMF, "DTMF end passthrough '%c' on %s\n", f->subclass.integer, ast_channel_name(chan));
3995 if (f->len < option_dtmfminduration) {
3997 }
3998 ast_channel_dtmf_tv_set(chan, &now);
3999
4000 /* Start generating 50 fps timer events (null frames) for dtmf emulating
4001 * independently from any existing incoming voice frames.
4002 * If channel generator is already activated in regular mode use these
4003 * timer events to generate null frames.
4004 */
4005 if (!ast_channel_generator(chan)) {
4007 }
4008 }
4009 if (ast_channel_audiohooks(chan)) {
4010 struct ast_frame *old_frame = f;
4012 if (old_frame != f)
4013 ast_frfree(old_frame);
4014 }
4015 }
4016 break;
4019 ast_log(LOG_DTMF, "DTMF begin '%c' received on %s\n", f->subclass.integer, ast_channel_name(chan));
4021 (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
4023 ast_log(LOG_DTMF, "DTMF begin ignored '%c' on %s\n", f->subclass.integer, ast_channel_name(chan));
4024 ast_frfree(f);
4025 f = &ast_null_frame;
4026 } else {
4027 struct timeval now = ast_tvnow();
4029 ast_channel_dtmf_tv_set(chan, &now);
4030 ast_log(LOG_DTMF, "DTMF begin passthrough '%c' on %s\n", f->subclass.integer, ast_channel_name(chan));
4031 }
4032 break;
4033 case AST_FRAME_NULL:
4034 /* The EMULATE_DTMF flag must be cleared here as opposed to when the duration
4035 * is reached , because we want to make sure we pass at least one
4036 * voice frame through before starting the next digit, to ensure a gap
4037 * between DTMF digits. */
4039 struct timeval now = ast_tvnow();
4043 } else if (ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan)) >= ast_channel_emulate_dtmf_duration(chan)) {
4045 ast_frfree(f);
4046 f = ast_channel_dtmff(chan);
4049 f->len = ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan));
4050 ast_channel_dtmf_tv_set(chan, &now);
4053 ast_log(LOG_DTMF, "DTMF end emulation of '%c' queued on %s\n", f->subclass.integer, ast_channel_name(chan));
4054 if (ast_channel_audiohooks(chan)) {
4055 struct ast_frame *old_frame = f;
4057 if (old_frame != f) {
4058 ast_frfree(old_frame);
4059 }
4060 }
4061
4062 /* Start generating 50 fps timer events (null frames) for dtmf emulating
4063 * independently from any existing incoming voice frames.
4064 * If channel generator is already activated in regular mode use these
4065 * timer events to generate null frames.
4066 */
4067 if (!ast_channel_generator(chan)) {
4069 }
4070 }
4071 }
4072 break;
4073 case AST_FRAME_VOICE:
4074 /* If media was received from a non-default stream don't perform any actions, let it just go through */
4075 if (stream != default_stream) {
4076 break;
4077 }
4078
4079 /* The EMULATE_DTMF flag must be cleared here as opposed to when the duration
4080 * is reached , because we want to make sure we pass at least one
4081 * voice frame through before starting the next digit, to ensure a gap
4082 * between DTMF digits. */
4086 }
4087
4088 if (dropaudio || ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_DTMF)) {
4089 if (dropaudio)
4091 ast_frfree(f);
4092 f = &ast_null_frame;
4093 }
4094
4096 struct timeval now = ast_tvnow();
4099 ast_frfree(f);
4100 f = ast_channel_dtmff(chan);
4103 f->len = ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan));
4104 ast_channel_dtmf_tv_set(chan, &now);
4105 if (ast_channel_audiohooks(chan)) {
4106 struct ast_frame *old_frame = f;
4108 if (old_frame != f)
4109 ast_frfree(old_frame);
4110 }
4111 ast_log(LOG_DTMF, "DTMF end emulation of '%c' queued on %s\n", f->subclass.integer, ast_channel_name(chan));
4112 } else {
4113 /* Drop voice frames while we're still in the middle of the digit */
4114 ast_frfree(f);
4115 f = &ast_null_frame;
4116 }
4117 break;
4118 }
4119 if (f->frametype != AST_FRAME_VOICE) {
4120 break;
4121 }
4124 struct ast_format *core_format;
4125
4126 /*
4127 * Note: This frame may not be one of the current native
4128 * formats. We may have gotten it out of the read queue from
4129 * a previous multi-frame translation, from a framehook
4130 * injected frame, or the device we're talking to isn't
4131 * respecting negotiated formats. Regardless we will accept
4132 * all frames.
4133 *
4134 * Update the read translation path to handle the new format
4135 * that just came in. If the core wants slinear we need to
4136 * setup a new translation path because the core is usually
4137 * doing something with the audio itself and may not handle
4138 * any other format. e.g., Softmix bridge, holding bridge
4139 * announcer channel, recording, AMD... Otherwise, we'll
4140 * setup to pass the frame as is to the core. In this case
4141 * the core doesn't care. The channel is likely in
4142 * autoservice, safesleep, or the channel is in a bridge.
4143 * Let the bridge technology deal with format compatibility
4144 * between the channels in the bridge.
4145 *
4146 * Beware of the transcode_via_slin and genericplc options as
4147 * they force any transcoding to go through slin on a bridge.
4148 * Unfortunately transcode_via_slin is enabled by default and
4149 * genericplc is enabled in the codecs.conf.sample file.
4150 *
4151 * XXX Only updating translation to slinear frames has some
4152 * corner cases if slinear is one of the native formats and
4153 * there are different sample rates involved. We might wind
4154 * up with conflicting translation paths between channels
4155 * where the read translation path on this channel reduces
4156 * the sample rate followed by a write translation path on
4157 * the peer channel that increases the sample rate.
4158 */
4159 core_format = ast_channel_readformat(chan);
4160 if (!ast_format_cache_is_slinear(core_format)) {
4161 core_format = f->subclass.format;
4162 }
4163 if (ast_set_read_format_path(chan, f->subclass.format, core_format)) {
4164 /* Drop frame. We couldn't make it compatible with the core. */
4165 ast_frfree(f);
4166 f = &ast_null_frame;
4167 break;
4168 }
4169 }
4170 /*
4171 * Send frame to audiohooks if present, if frametype is linear, to preserve
4172 * functional compatibility with previous behavior. If not linear, hold off
4173 * until transcoding is done where we are more likely to have a linear frame
4174 */
4176 /* Place hooked after declaration */
4177 struct ast_frame *old_frame = f;
4178 hooked = 1;
4179
4181 if (old_frame != f) {
4182 ast_frfree(old_frame);
4183 }
4184 }
4185
4186 if (ast_channel_readtrans(chan)
4188 f = ast_translate(ast_channel_readtrans(chan), f, 1);
4189 if (!f) {
4190 f = &ast_null_frame;
4191 }
4192 }
4193
4194 /* Second chance at hooking a linear frame, also the last chance */
4195 if (ast_channel_audiohooks(chan) && !hooked) {
4196 struct ast_frame *old_frame = f;
4197
4199 if (old_frame != f) {
4200 ast_frfree(old_frame);
4201 }
4202 }
4203
4204 /*
4205 * It is possible for the translation process on the channel to have
4206 * produced multiple frames from the single input frame we passed it; if
4207 * this happens, queue the additional frames *before* the frames we may
4208 * have queued earlier. if the readq was empty, put them at the head of
4209 * the queue, and if it was not, put them just after the frame that was
4210 * at the end of the queue.
4211 */
4212 if (AST_LIST_NEXT(f, frame_list)) {
4213 struct ast_frame *cur, *multi_frame = AST_LIST_NEXT(f, frame_list);
4214
4215 /* Mark these frames as being re-queued */
4216 for (cur = multi_frame; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
4218 }
4219
4220 if (!readq_tail) {
4221 ast_queue_frame_head(chan, multi_frame);
4222 } else {
4223 __ast_queue_frame(chan, multi_frame, 0, readq_tail);
4224 }
4225 ast_frfree(multi_frame);
4227 }
4228
4229 /*
4230 * Run generator sitting on the line if timing device not available
4231 * and synchronous generation of outgoing frames is necessary
4232 */
4234 break;
4235 case AST_FRAME_RTCP:
4236 /* Incoming RTCP feedback needs to get to the translator for
4237 * outgoing media, which means we treat it as an ast_write */
4238 if (ast_channel_writetrans(chan)) {
4240 }
4241 break;
4242 default:
4243 /* Just pass it on! */
4244 break;
4245 }
4246 } else {
4247 /* Make sure we always return NULL in the future */
4250 }
4251 if (cause)
4252 ast_channel_hangupcause_set(chan, cause);
4253 if (ast_channel_generator(chan))
4255 /* We no longer End the CDR here */
4256 }
4257
4258 /* High bit prints debugging */
4259 if (ast_channel_fin(chan) & DEBUGCHAN_FLAG)
4260 ast_frame_dump(ast_channel_name(chan), f, "<<");
4262
4263done:
4266
4268 /* The list gets recreated if audiohooks are added again later */
4271 }
4272 ast_channel_unlock(chan);
4273 return f;
4274}
4275
4276struct ast_frame *ast_read(struct ast_channel *chan)
4277{
4278 return __ast_read(chan, 0, 1);
4279}
4280
4282{
4283 return __ast_read(chan, 0, 0);
4284}
4285
4287{
4288 return __ast_read(chan, 1, 1);
4289}
4290
4292{
4293 return __ast_read(chan, 1, 0);
4294}
4295
4296int ast_indicate(struct ast_channel *chan, int condition)
4297{
4298 return ast_indicate_data(chan, condition, NULL, 0);
4299}
4300
4302{
4303 /* Don't include a default case here so that we get compiler warnings
4304 * when a new type is added. */
4305
4306 switch (condition) {
4314 case AST_CONTROL_OPTION:
4315 case AST_CONTROL_WINK:
4316 case AST_CONTROL_FLASH:
4319 case AST_CONTROL_ANSWER:
4320 case AST_CONTROL_HANGUP: