Asterisk - The Open Source Telephony Project GIT-master-55f4e6d
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 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
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, file, line, function, name_fmt, ap);
968 va_end(ap);
969
970 return result;
971}
972
973/* only do the minimum amount of work needed here to make a channel
974 * structure that can be used to expand channel vars */
975struct ast_channel *__ast_dummy_channel_alloc(const char *file, int line, const char *function)
976{
977 struct ast_channel *tmp;
978 struct varshead *headp;
979
981 file, line, function);
982 if (!tmp) {
983 /* Dummy channel structure allocation failure. */
984 return NULL;
985 }
986
989
990 /*
991 * Init file descriptors to unopened state just in case
992 * autoservice is called on the channel or something tries to
993 * read a frame from it.
994 */
998
1000
1002
1003 headp = ast_channel_varshead(tmp);
1005
1006 return tmp;
1007}
1008
1009static int __ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin, int head, struct ast_frame *after)
1010{
1011 struct ast_frame *f;
1012 struct ast_frame *cur;
1013 unsigned int new_frames = 0;
1014 unsigned int new_voice_frames = 0;
1015 unsigned int queued_frames = 0;
1016 unsigned int queued_voice_frames = 0;
1018
1019 ast_channel_lock(chan);
1020
1021 /*
1022 * Check the last frame on the queue if we are queuing the new
1023 * frames after it.
1024 */
1025 cur = AST_LIST_LAST(ast_channel_readq(chan));
1026 if (cur && cur->frametype == AST_FRAME_CONTROL && !head && (!after || after == cur)) {
1027 switch (cur->subclass.integer) {
1029 if (fin->frametype == AST_FRAME_CONTROL
1030 && fin->subclass.integer == AST_CONTROL_HANGUP) {
1031 /*
1032 * Destroy the end-of-Q marker frame so we can queue the hangup
1033 * frame in its place.
1034 */
1036 ast_frfree(cur);
1037
1038 /*
1039 * This has degenerated to a normal queue append anyway. Since
1040 * we just destroyed the last frame in the queue we must make
1041 * sure that "after" is NULL or bad things will happen.
1042 */
1043 after = NULL;
1044 break;
1045 }
1046 /* Fall through */
1047 case AST_CONTROL_HANGUP:
1048 /* Don't queue anything. */
1049 ast_channel_unlock(chan);
1050 return 0;
1051 default:
1052 break;
1053 }
1054 }
1055
1056 /* Build copies of all the new frames and count them */
1058 for (cur = fin; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
1059 if (!(f = ast_frdup(cur))) {
1060 if (AST_LIST_FIRST(&frames)) {
1062 }
1063 ast_channel_unlock(chan);
1064 return -1;
1065 }
1066
1068 new_frames++;
1069 if (f->frametype == AST_FRAME_VOICE) {
1070 new_voice_frames++;
1071 }
1072 }
1073
1074 /* Count how many frames exist on the queue */
1076 queued_frames++;
1077 if (cur->frametype == AST_FRAME_VOICE) {
1078 queued_voice_frames++;
1079 }
1080 }
1081
1082 if ((queued_frames + new_frames > 128 || queued_voice_frames + new_voice_frames > 96)) {
1083 int total_queued = queued_frames + new_frames;
1084 int total_voice = queued_voice_frames + new_voice_frames;
1085 int count = 0;
1086 ast_log(LOG_WARNING, "Exceptionally long %squeue length (%d voice / %d total) queuing to %s\n",
1087 queued_frames + new_frames > 128 ? "" : "voice ", total_voice, total_queued, ast_channel_name(chan));
1089 /* Save the most recent frame */
1090 if (!AST_LIST_NEXT(cur, frame_list)) {
1091 break;
1092 } else if (cur->frametype == AST_FRAME_VOICE || cur->frametype == AST_FRAME_VIDEO || cur->frametype == AST_FRAME_NULL) {
1093 if (++count > 64) {
1094 break;
1095 }
1097 ast_frfree(cur);
1098
1099 /* Read from the alert pipe for each flushed frame. */
1101 }
1102 }
1104 if (count) {
1105 ast_debug(4, "Discarded %d frame%s due to queue overload on %s\n", count, ESS(count), ast_channel_name(chan));
1106 }
1107 }
1108
1109 if (after) {
1111 } else {
1112 if (head) {
1115 }
1117 }
1118
1119 if (ast_channel_alert_writable(chan)) {
1120 /* Write to the alert pipe for each added frame */
1121 while (new_frames--) {
1122 if (ast_channel_alert_write(chan)) {
1123 ast_log(LOG_WARNING, "Unable to write to alert pipe on %s (qlen = %u): %s!\n",
1124 ast_channel_name(chan), queued_frames, strerror(errno));
1125 break;
1126 }
1127 }
1128 } else if (ast_channel_timingfd(chan) > -1) {
1131 pthread_kill(ast_channel_blocker(chan), SIGURG);
1132 }
1133
1134 ast_channel_unlock(chan);
1135
1136 return 0;
1137}
1138
1139int ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin)
1140{
1141 return __ast_queue_frame(chan, fin, 0, NULL);
1142}
1143
1144int ast_queue_frame_head(struct ast_channel *chan, struct ast_frame *fin)
1145{
1146 return __ast_queue_frame(chan, fin, 1, NULL);
1147}
1148
1149/*! \brief Queue a hangup frame for channel */
1151{
1152 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
1153 int res;
1154
1155 /* Yeah, let's not change a lock-critical value without locking */
1156 ast_channel_lock(chan);
1159
1160 res = ast_queue_frame(chan, &f);
1161 ast_channel_unlock(chan);
1162 return res;
1163}
1164
1165/*! \brief Queue a hangup frame for channel */
1166int ast_queue_hangup_with_cause(struct ast_channel *chan, int cause)
1167{
1168 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
1169 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HANGUP };
1170 int res;
1171
1172 if (cause >= 0) {
1173 f.data.uint32 = cause;
1174 }
1175
1176 /* Yeah, let's not change a lock-critical value without locking */
1177 ast_channel_lock(chan);
1179 if (cause < 0) {
1181 }
1182 blob = ast_json_pack("{s: i}",
1183 "cause", cause);
1185
1186 res = ast_queue_frame(chan, &f);
1187 ast_channel_unlock(chan);
1188 return res;
1189}
1190
1191int ast_queue_hold(struct ast_channel *chan, const char *musicclass)
1192{
1193 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_HOLD };
1194 struct ast_json *blob = NULL;
1195 int res;
1196
1197 if (!ast_strlen_zero(musicclass)) {
1198 f.data.ptr = (void *) musicclass;
1199 f.datalen = strlen(musicclass) + 1;
1200
1201 blob = ast_json_pack("{s: s}",
1202 "musicclass", musicclass);
1203 }
1204
1205 ast_channel_lock(chan);
1207 ast_channel_unlock(chan);
1208
1209 res = ast_queue_frame(chan, &f);
1210
1211 ast_json_unref(blob);
1212
1213 return res;
1214}
1215
1217{
1218 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = AST_CONTROL_UNHOLD };
1219 int res;
1220
1221 ast_channel_lock(chan);
1223 ast_channel_unlock(chan);
1224
1225 res = ast_queue_frame(chan, &f);
1226
1227 return res;
1228}
1229
1230/*! \brief Queue a control frame */
1232{
1233 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control };
1234 return ast_queue_frame(chan, &f);
1235}
1236
1237/*! \brief Queue a control frame with payload */
1239 const void *data, size_t datalen)
1240{
1241 struct ast_frame f = { AST_FRAME_CONTROL, .subclass.integer = control, .data.ptr = (void *) data, .datalen = datalen };
1242 return ast_queue_frame(chan, &f);
1243}
1244
1245/*! \brief Queue an ANSWER control frame with topology */
1246int ast_queue_answer(struct ast_channel *chan, const struct ast_stream_topology *topology)
1247{
1248 struct ast_frame f = {
1250 .subclass.integer = AST_CONTROL_ANSWER,
1251 .subclass.topology = (struct ast_stream_topology *)topology,
1252 };
1253 return ast_queue_frame(chan, &f);
1254}
1255
1256/*! \brief Set defer DTMF flag on channel */
1258{
1259 int pre = 0;
1260
1261 if (chan) {
1262 ast_channel_lock(chan);
1265 ast_channel_unlock(chan);
1266 }
1267 return pre;
1268}
1269
1270/*! \brief Unset defer DTMF flag on channel */
1272{
1273 if (chan) {
1275 }
1276}
1277
1279 void *data, int ao2_flags)
1280{
1281 return ao2_callback_data(channels, ao2_flags, cb_fn, arg, data);
1282}
1283
1284static int ast_channel_by_name_cb(void *obj, void *arg, void *data, int flags)
1285{
1286 struct ast_channel *chan = obj;
1287 const char *name = arg;
1288 size_t name_len = *(size_t *) data;
1289 int ret = CMP_MATCH;
1290
1291 if (ast_strlen_zero(name)) {
1292 ast_log(LOG_ERROR, "BUG! Must supply a channel name or partial name to match!\n");
1293 return CMP_STOP;
1294 }
1295
1296 ast_channel_lock(chan);
1297 if ((!name_len && strcasecmp(ast_channel_name(chan), name))
1298 || (name_len && strncasecmp(ast_channel_name(chan), name, name_len))) {
1299 ret = 0; /* name match failed, keep looking */
1300 }
1301 ast_channel_unlock(chan);
1302
1303 return ret;
1304}
1305
1306static int ast_channel_by_exten_cb(void *obj, void *arg, void *data, int flags)
1307{
1308 struct ast_channel *chan = obj;
1309 char *context = arg;
1310 char *exten = data;
1311 int ret = CMP_MATCH;
1312
1314 ast_log(LOG_ERROR, "BUG! Must have a context and extension to match!\n");
1315 return CMP_STOP;
1316 }
1317
1318 ast_channel_lock(chan);
1319 if (strcasecmp(ast_channel_context(chan), context)) {
1320 ret = 0; /* Context match failed, continue */
1321 } else if (strcasecmp(ast_channel_exten(chan), exten)) {
1322 ret = 0; /* Extension match failed, continue */
1323 }
1324 ast_channel_unlock(chan);
1325
1326 return ret;
1327}
1328
1329static int ast_channel_by_uniqueid_cb(void *obj, void *arg, void *data, int flags)
1330{
1331 struct ast_channel *chan = obj;
1332 char *uniqueid = arg;
1333 size_t id_len = *(size_t *) data;
1334 int ret = CMP_MATCH;
1335
1337 ast_log(LOG_ERROR, "BUG! Must supply a uniqueid or partial uniqueid to match!\n");
1338 return CMP_STOP;
1339 }
1340
1341 ast_channel_lock(chan);
1342 if ((!id_len && strcasecmp(ast_channel_uniqueid(chan), uniqueid))
1343 || (id_len && strncasecmp(ast_channel_uniqueid(chan), uniqueid, id_len))) {
1344 ret = 0; /* uniqueid match failed, keep looking */
1345 }
1346 ast_channel_unlock(chan);
1347
1348 return ret;
1349}
1350
1352 /* storage for non-dynamically allocated iterator */
1354 /* pointer to the actual iterator (simple_iterator or a dynamically
1355 * allocated iterator)
1356 */
1358};
1359
1361{
1363 ast_free(i);
1364
1365 return NULL;
1366}
1367
1369{
1370 struct ast_channel_iterator *i;
1371 char *l_exten = (char *) exten;
1372 char *l_context = (char *) context;
1373
1374 if (!(i = ast_calloc(1, sizeof(*i)))) {
1375 return NULL;
1376 }
1377
1379 l_context, l_exten, OBJ_MULTIPLE);
1380 if (!i->active_iterator) {
1381 ast_free(i);
1382 return NULL;
1383 }
1384
1385 return i;
1386}
1387
1389{
1390 struct ast_channel_iterator *i;
1391 char *l_name = (char *) name;
1392
1393 if (!(i = ast_calloc(1, sizeof(*i)))) {
1394 return NULL;
1395 }
1396
1398 l_name, &name_len,
1399 OBJ_MULTIPLE | (name_len == 0 /* match the whole word, so optimize */ ? OBJ_KEY : 0));
1400 if (!i->active_iterator) {
1401 ast_free(i);
1402 return NULL;
1403 }
1404
1405 return i;
1406}
1407
1409{
1410 struct ast_channel_iterator *i;
1411
1412 if (!(i = ast_calloc(1, sizeof(*i)))) {
1413 return NULL;
1414 }
1415
1418
1419 return i;
1420}
1421
1423{
1425}
1426
1427/* Legacy function, not currently used for lookups, but we need a cmp_fn */
1428static int ast_channel_cmp_cb(void *obj, void *arg, int flags)
1429{
1430 ast_log(LOG_ERROR, "BUG! Should never be called!\n");
1431 return CMP_STOP;
1432}
1433
1434struct ast_channel *ast_channel_get_by_name_prefix(const char *name, size_t name_len)
1435{
1436 struct ast_channel *chan;
1437 char *l_name = (char *) name;
1438
1439 if (ast_strlen_zero(l_name)) {
1440 /* We didn't have a name to search for so quit. */
1441 return NULL;
1442 }
1443
1444 chan = ast_channel_callback(ast_channel_by_name_cb, l_name, &name_len,
1445 (name_len == 0) /* optimize if it is a complete name match */ ? OBJ_KEY : 0);
1446 if (chan) {
1447 return chan;
1448 }
1449
1450 /* Now try a search for uniqueid. */
1451 return ast_channel_callback(ast_channel_by_uniqueid_cb, l_name, &name_len, 0);
1452}
1453
1455{
1457}
1458
1459struct ast_channel *ast_channel_get_by_exten(const char *exten, const char *context)
1460{
1461 char *l_exten = (char *) exten;
1462 char *l_context = (char *) context;
1463
1464 return ast_channel_callback(ast_channel_by_exten_cb, l_context, l_exten, 0);
1465}
1466
1467int ast_is_deferrable_frame(const struct ast_frame *frame)
1468{
1469 /* Do not add a default entry in this switch statement. Each new
1470 * frame type should be addressed directly as to whether it should
1471 * be queued up or not.
1472 */
1473 switch (frame->frametype) {
1476 case AST_FRAME_CONTROL:
1477 case AST_FRAME_TEXT:
1479 case AST_FRAME_IMAGE:
1480 case AST_FRAME_HTML:
1481 return 1;
1482
1483 case AST_FRAME_DTMF_END:
1485 case AST_FRAME_VOICE:
1486 case AST_FRAME_VIDEO:
1487 case AST_FRAME_NULL:
1488 case AST_FRAME_IAX:
1489 case AST_FRAME_CNG:
1490 case AST_FRAME_MODEM:
1491 case AST_FRAME_RTCP:
1492 return 0;
1493 }
1494 return 0;
1495}
1496
1497/*! \brief Wait, look for hangups and condition arg */
1498static int safe_sleep_conditional(struct ast_channel *chan, int timeout_ms, int (*cond)(void*), void *data, unsigned int generate_silence)
1499{
1500 struct ast_frame *f;
1501 struct ast_silence_generator *silgen = NULL;
1502 int res = 0;
1503 struct timeval start;
1504 int ms;
1505 AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_frames;
1506
1507 AST_LIST_HEAD_INIT_NOLOCK(&deferred_frames);
1508
1509 /* If no other generator is present, start silencegen while waiting */
1510 if (generate_silence && ast_opt_transmit_silence && !ast_channel_generatordata(chan)) {
1512 }
1513
1514 start = ast_tvnow();
1515 while ((ms = ast_remaining_ms(start, timeout_ms))) {
1516 struct ast_frame *dup_f = NULL;
1517
1518 if (cond && ((*cond)(data) == 0)) {
1519 break;
1520 }
1521 ms = ast_waitfor(chan, ms);
1522 if (ms < 0) {
1523 res = -1;
1524 break;
1525 }
1526 if (ms > 0) {
1527 f = ast_read(chan);
1528 if (!f) {
1529 res = -1;
1530 break;
1531 }
1532
1533 if (!ast_is_deferrable_frame(f)) {
1534 ast_frfree(f);
1535 continue;
1536 }
1537
1538 if ((dup_f = ast_frisolate(f))) {
1539 if (dup_f != f) {
1540 ast_frfree(f);
1541 }
1542 AST_LIST_INSERT_HEAD(&deferred_frames, dup_f, frame_list);
1543 }
1544 }
1545 }
1546
1547 /* stop silgen if present */
1548 if (silgen) {
1550 }
1551
1552 /* We need to free all the deferred frames, but we only need to
1553 * queue the deferred frames if there was no error and no
1554 * hangup was received
1555 */
1556 ast_channel_lock(chan);
1557 while ((f = AST_LIST_REMOVE_HEAD(&deferred_frames, frame_list))) {
1558 if (!res) {
1559 ast_queue_frame_head(chan, f);
1560 }
1561 ast_frfree(f);
1562 }
1563 ast_channel_unlock(chan);
1564
1565 return res;
1566}
1567
1568int ast_safe_sleep_conditional(struct ast_channel *chan, int timeout_ms, int (*cond)(void*), void *data)
1569{
1570 return safe_sleep_conditional(chan, timeout_ms, cond, data, 1);
1571}
1572
1573/*! \brief Wait, look for hangups */
1574int ast_safe_sleep(struct ast_channel *chan, int ms)
1575{
1576 return safe_sleep_conditional(chan, ms, NULL, NULL, 1);
1577}
1578
1580{
1581 return safe_sleep_conditional(chan, ms, NULL, NULL, 0);
1582}
1583
1585{
1586 /* Safe, even if already unlinked. */
1587 ao2_unlink(channels, chan);
1588 return ast_channel_unref(chan);
1589}
1590
1592{
1593 init->str = NULL;
1596 init->valid = 0;
1597}
1598
1599void ast_party_name_copy(struct ast_party_name *dest, const struct ast_party_name *src)
1600{
1601 if (dest == src) {
1602 /* Don't copy to self */
1603 return;
1604 }
1605
1606 ast_free(dest->str);
1607 dest->str = ast_strdup(src->str);
1608 dest->char_set = src->char_set;
1609 dest->presentation = src->presentation;
1610 dest->valid = src->valid;
1611}
1612
1613void ast_party_name_set_init(struct ast_party_name *init, const struct ast_party_name *guide)
1614{
1615 init->str = NULL;
1616 init->char_set = guide->char_set;
1617 init->presentation = guide->presentation;
1618 init->valid = guide->valid;
1619}
1620
1621void ast_party_name_set(struct ast_party_name *dest, const struct ast_party_name *src)
1622{
1623 if (dest == src) {
1624 /* Don't set to self */
1625 return;
1626 }
1627
1628 if (src->str && src->str != dest->str) {
1629 ast_free(dest->str);
1630 dest->str = ast_strdup(src->str);
1631 }
1632
1633 dest->char_set = src->char_set;
1634 dest->presentation = src->presentation;
1635 dest->valid = src->valid;
1636}
1637
1639{
1640 ast_free(doomed->str);
1641 doomed->str = NULL;
1642}
1643
1645{
1646 init->str = NULL;
1647 init->plan = 0;/* Unknown */
1649 init->valid = 0;
1650}
1651
1652void ast_party_number_copy(struct ast_party_number *dest, const struct ast_party_number *src)
1653{
1654 if (dest == src) {
1655 /* Don't copy to self */
1656 return;
1657 }
1658
1659 ast_free(dest->str);
1660 dest->str = ast_strdup(src->str);
1661 dest->plan = src->plan;
1662 dest->presentation = src->presentation;
1663 dest->valid = src->valid;
1664}
1665
1666void ast_party_number_set_init(struct ast_party_number *init, const struct ast_party_number *guide)
1667{
1668 init->str = NULL;
1669 init->plan = guide->plan;
1670 init->presentation = guide->presentation;
1671 init->valid = guide->valid;
1672}
1673
1674void ast_party_number_set(struct ast_party_number *dest, const struct ast_party_number *src)
1675{
1676 if (dest == src) {
1677 /* Don't set to self */
1678 return;
1679 }
1680
1681 if (src->str && src->str != dest->str) {
1682 ast_free(dest->str);
1683 dest->str = ast_strdup(src->str);
1684 }
1685
1686 dest->plan = src->plan;
1687 dest->presentation = src->presentation;
1688 dest->valid = src->valid;
1689}
1690
1692{
1693 ast_free(doomed->str);
1694 doomed->str = NULL;
1695}
1696
1698{
1699 init->str = NULL;
1700 init->type = 0;
1701 init->odd_even_indicator = 0;
1702 init->valid = 0;
1703}
1704
1706{
1707 if (dest == src) {
1708 /* Don't copy to self */
1709 return;
1710 }
1711
1712 ast_free(dest->str);
1713 dest->str = ast_strdup(src->str);
1714 dest->type = src->type;
1716 dest->valid = src->valid;
1717}
1718
1720{
1721 init->str = NULL;
1722 init->type = guide->type;
1724 init->valid = guide->valid;
1725}
1726
1728{
1729 if (dest == src) {
1730 /* Don't set to self */
1731 return;
1732 }
1733
1734 if (src->str && src->str != dest->str) {
1735 ast_free(dest->str);
1736 dest->str = ast_strdup(src->str);
1737 }
1738
1739 dest->type = src->type;
1741 dest->valid = src->valid;
1742}
1743
1745{
1746 ast_free(doomed->str);
1747 doomed->str = NULL;
1748}
1749
1751{
1752 update_id->name = 1;
1753 update_id->number = 1;
1754 update_id->subaddress = 1;
1755}
1756
1758{
1759 ast_party_name_init(&init->name);
1762 init->tag = NULL;
1763}
1764
1765void ast_party_id_copy(struct ast_party_id *dest, const struct ast_party_id *src)
1766{
1767 if (dest == src) {
1768 /* Don't copy to self */
1769 return;
1770 }
1771
1772 ast_party_name_copy(&dest->name, &src->name);
1773 ast_party_number_copy(&dest->number, &src->number);
1775
1776 ast_free(dest->tag);
1777 dest->tag = ast_strdup(src->tag);
1778}
1779
1780void ast_party_id_set_init(struct ast_party_id *init, const struct ast_party_id *guide)
1781{
1782 ast_party_name_set_init(&init->name, &guide->name);
1783 ast_party_number_set_init(&init->number, &guide->number);
1785 init->tag = NULL;
1786}
1787
1788void ast_party_id_set(struct ast_party_id *dest, const struct ast_party_id *src, const struct ast_set_party_id *update)
1789{
1790 if (dest == src) {
1791 /* Don't set to self */
1792 return;
1793 }
1794
1795 if (!update || update->name) {
1796 ast_party_name_set(&dest->name, &src->name);
1797 }
1798 if (!update || update->number) {
1799 ast_party_number_set(&dest->number, &src->number);
1800 }
1801 if (!update || update->subaddress) {
1803 }
1804
1805 if (src->tag && src->tag != dest->tag) {
1806 ast_free(dest->tag);
1807 dest->tag = ast_strdup(src->tag);
1808 }
1809}
1810
1812{
1813 ast_party_name_free(&doomed->name);
1814 ast_party_number_free(&doomed->number);
1816
1817 ast_free(doomed->tag);
1818 doomed->tag = NULL;
1819}
1820
1822{
1823 int number_priority;
1824 int number_value;
1825 int number_screening;
1826 int name_priority;
1827 int name_value;
1828
1829 /* Determine name presentation priority. */
1830 if (!id->name.valid) {
1831 name_value = AST_PRES_UNAVAILABLE;
1832 name_priority = 3;
1833 } else {
1834 name_value = id->name.presentation & AST_PRES_RESTRICTION;
1835 switch (name_value) {
1837 name_priority = 0;
1838 break;
1839 case AST_PRES_ALLOWED:
1840 name_priority = 1;
1841 break;
1843 name_priority = 2;
1844 break;
1845 default:
1846 name_value = AST_PRES_UNAVAILABLE;
1847 name_priority = 3;
1848 break;
1849 }
1850 }
1851
1852 /* Determine number presentation priority. */
1853 if (!id->number.valid) {
1854 number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
1855 number_value = AST_PRES_UNAVAILABLE;
1856 number_priority = 3;
1857 } else {
1858 number_screening = id->number.presentation & AST_PRES_NUMBER_TYPE;
1859 number_value = id->number.presentation & AST_PRES_RESTRICTION;
1860 switch (number_value) {
1862 number_priority = 0;
1863 break;
1864 case AST_PRES_ALLOWED:
1865 number_priority = 1;
1866 break;
1868 number_priority = 2;
1869 break;
1870 default:
1871 number_screening = AST_PRES_USER_NUMBER_UNSCREENED;
1872 number_value = AST_PRES_UNAVAILABLE;
1873 number_priority = 3;
1874 break;
1875 }
1876 }
1877
1878 /* Select the wining presentation value. */
1879 if (name_priority < number_priority) {
1880 number_value = name_value;
1881 }
1882 if (number_value == AST_PRES_UNAVAILABLE) {
1884 }
1885
1886 return number_value | number_screening;
1887}
1888
1890{
1891 id->name.valid = 0;
1892 id->number.valid = 0;
1893 id->subaddress.valid = 0;
1894}
1895
1897{
1900}
1901
1902struct ast_party_id ast_party_id_merge(struct ast_party_id *base, struct ast_party_id *overlay)
1903{
1904 struct ast_party_id merged;
1905
1906 merged = *base;
1907 if (overlay->name.valid) {
1908 merged.name = overlay->name;
1909 }
1910 if (overlay->number.valid) {
1911 merged.number = overlay->number;
1912 }
1913 if (overlay->subaddress.valid) {
1914 merged.subaddress = overlay->subaddress;
1915 }
1916 /* Note the actual structure is returned and not a pointer to it! */
1917 return merged;
1918}
1919
1920void ast_party_id_merge_copy(struct ast_party_id *dest, struct ast_party_id *base, struct ast_party_id *overlay)
1921{
1922 struct ast_party_id merged;
1923
1924 merged = ast_party_id_merge(base, overlay);
1925 ast_party_id_copy(dest, &merged);
1926}
1927
1929{
1930 init->number.str = NULL;
1931 init->number.plan = 0;/* Unknown */
1933 init->transit_network_select = 0;
1934}
1935
1936void ast_party_dialed_copy(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
1937{
1938 if (dest == src) {
1939 /* Don't copy to self */
1940 return;
1941 }
1942
1943 ast_free(dest->number.str);
1944 dest->number.str = ast_strdup(src->number.str);
1945 dest->number.plan = src->number.plan;
1948}
1949
1950void ast_party_dialed_set_init(struct ast_party_dialed *init, const struct ast_party_dialed *guide)
1951{
1952 init->number.str = NULL;
1953 init->number.plan = guide->number.plan;
1956}
1957
1958void ast_party_dialed_set(struct ast_party_dialed *dest, const struct ast_party_dialed *src)
1959{
1960 if (src->number.str && src->number.str != dest->number.str) {
1961 ast_free(dest->number.str);
1962 dest->number.str = ast_strdup(src->number.str);
1963 }
1964 dest->number.plan = src->number.plan;
1965
1967
1969}
1970
1972{
1973 ast_free(doomed->number.str);
1974 doomed->number.str = NULL;
1976}
1977
1979{
1980 ast_party_id_init(&init->id);
1981 ast_party_id_init(&init->ani);
1982 ast_party_id_init(&init->priv);
1983 init->ani2 = 0;
1984}
1985
1986void ast_party_caller_copy(struct ast_party_caller *dest, const struct ast_party_caller *src)
1987{
1988 if (dest == src) {
1989 /* Don't copy to self */
1990 return;
1991 }
1992
1993 ast_party_id_copy(&dest->id, &src->id);
1994 ast_party_id_copy(&dest->ani, &src->ani);
1995 ast_party_id_copy(&dest->priv, &src->priv);
1996 dest->ani2 = src->ani2;
1997}
1998
1999void ast_party_caller_set_init(struct ast_party_caller *init, const struct ast_party_caller *guide)
2000{
2001 ast_party_id_set_init(&init->id, &guide->id);
2002 ast_party_id_set_init(&init->ani, &guide->ani);
2003 ast_party_id_set_init(&init->priv, &guide->priv);
2004 init->ani2 = guide->ani2;
2005}
2006
2007void ast_party_caller_set(struct ast_party_caller *dest, const struct ast_party_caller *src, const struct ast_set_party_caller *update)
2008{
2009 ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
2010 ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
2011 ast_party_id_set(&dest->priv, &src->priv, update ? &update->priv : NULL);
2012 dest->ani2 = src->ani2;
2013}
2014
2016{
2017 ast_party_id_free(&doomed->id);
2018 ast_party_id_free(&doomed->ani);
2019 ast_party_id_free(&doomed->priv);
2020}
2021
2023{
2024 ast_party_id_init(&init->id);
2025 ast_party_id_init(&init->ani);
2026 ast_party_id_init(&init->priv);
2027 init->ani2 = 0;
2029}
2030
2032{
2033 if (dest == src) {
2034 /* Don't copy to self */
2035 return;
2036 }
2037
2038 ast_party_id_copy(&dest->id, &src->id);
2039 ast_party_id_copy(&dest->ani, &src->ani);
2040 ast_party_id_copy(&dest->priv, &src->priv);
2041 dest->ani2 = src->ani2;
2042 dest->source = src->source;
2043}
2044
2046{
2047 ast_party_id_set_init(&init->id, &guide->id);
2048 ast_party_id_set_init(&init->ani, &guide->ani);
2049 ast_party_id_set_init(&init->priv, &guide->priv);
2050 init->ani2 = guide->ani2;
2051 init->source = guide->source;
2052}
2053
2055{
2056 ast_party_id_set(&dest->id, &src->id, update ? &update->id : NULL);
2057 ast_party_id_set(&dest->ani, &src->ani, update ? &update->ani : NULL);
2058 ast_party_id_set(&dest->priv, &src->priv, update ? &update->priv : NULL);
2059 dest->ani2 = src->ani2;
2060 dest->source = src->source;
2061}
2062
2064{
2065 connected->id = caller->id;
2066 connected->ani = caller->ani;
2067 connected->priv = caller->priv;
2068 connected->ani2 = caller->ani2;
2070}
2071
2073{
2074 ast_party_id_free(&doomed->id);
2075 ast_party_id_free(&doomed->ani);
2076 ast_party_id_free(&doomed->priv);
2077}
2078
2080{
2081 init->str = NULL;
2083}
2084
2086{
2087 if (dest == src) {
2088 return;
2089 }
2090
2091 ast_free(dest->str);
2092 dest->str = ast_strdup(src->str);
2093 dest->code = src->code;
2094}
2095
2097{
2098 init->str = NULL;
2099 init->code = guide->code;
2100}
2101
2103{
2104 if (dest == src) {
2105 return;
2106 }
2107
2108 if (src->str && src->str != dest->str) {
2109 ast_free(dest->str);
2110 dest->str = ast_strdup(src->str);
2111 }
2112
2113 dest->code = src->code;
2114}
2115
2117{
2118 ast_free(doomed->str);
2119}
2120
2121
2123{
2124 ast_party_id_init(&init->orig);
2125 ast_party_id_init(&init->from);
2126 ast_party_id_init(&init->to);
2129 ast_party_id_init(&init->priv_to);
2132 init->count = 0;
2133}
2134
2136{
2137 if (dest == src) {
2138 /* Don't copy to self */
2139 return;
2140 }
2141
2142 ast_party_id_copy(&dest->orig, &src->orig);
2143 ast_party_id_copy(&dest->from, &src->from);
2144 ast_party_id_copy(&dest->to, &src->to);
2145 ast_party_id_copy(&dest->priv_orig, &src->priv_orig);
2146 ast_party_id_copy(&dest->priv_from, &src->priv_from);
2147 ast_party_id_copy(&dest->priv_to, &src->priv_to);
2150 dest->count = src->count;
2151}
2152
2154{
2155 ast_party_id_set_init(&init->orig, &guide->orig);
2156 ast_party_id_set_init(&init->from, &guide->from);
2157 ast_party_id_set_init(&init->to, &guide->to);
2158 ast_party_id_set_init(&init->priv_orig, &guide->priv_orig);
2159 ast_party_id_set_init(&init->priv_from, &guide->priv_from);
2160 ast_party_id_set_init(&init->priv_to, &guide->priv_to);
2163 init->count = guide->count;
2164}
2165
2167{
2168 ast_party_id_set(&dest->orig, &src->orig, update ? &update->orig : NULL);
2169 ast_party_id_set(&dest->from, &src->from, update ? &update->from : NULL);
2170 ast_party_id_set(&dest->to, &src->to, update ? &update->to : NULL);
2171 ast_party_id_set(&dest->priv_orig, &src->priv_orig, update ? &update->priv_orig : NULL);
2172 ast_party_id_set(&dest->priv_from, &src->priv_from, update ? &update->priv_from : NULL);
2173 ast_party_id_set(&dest->priv_to, &src->priv_to, update ? &update->priv_to : NULL);
2176 dest->count = src->count;
2177}
2178
2180{
2181 ast_party_id_free(&doomed->orig);
2182 ast_party_id_free(&doomed->from);
2183 ast_party_id_free(&doomed->to);
2184 ast_party_id_free(&doomed->priv_orig);
2185 ast_party_id_free(&doomed->priv_from);
2186 ast_party_id_free(&doomed->priv_to);
2189}
2190
2191/*! \brief Free a channel structure */
2192static void ast_channel_destructor(void *obj)
2193{
2194 struct ast_channel *chan = obj;
2195 struct ast_var_t *vardata;
2196 struct ast_frame *f;
2197 struct varshead *headp;
2198 struct ast_datastore *datastore;
2199 char device_name[AST_CHANNEL_NAME];
2200 ast_callid callid;
2201
2202 ast_debug(1, "Channel %p '%s' destroying\n", chan, ast_channel_name(chan));
2203
2204 /* If there is native format music-on-hold state, free it */
2205 if (ast_channel_music_state(chan)) {
2206 ast_moh_cleanup(chan);
2207 }
2208
2210
2211 /* Things that may possibly raise Stasis messages shouldn't occur after this point */
2213
2215 /* A channel snapshot should not be in the process of being staged now. */
2217
2218 ast_channel_lock(chan);
2220 ast_channel_unlock(chan);
2221 }
2222
2223 ast_channel_lock(chan);
2224
2225 /* Get rid of each of the data stores on the channel */
2226 while ((datastore = AST_LIST_REMOVE_HEAD(ast_channel_datastores(chan), entry)))
2227 /* Free the data store */
2228 ast_datastore_free(datastore);
2229
2230 /* While the channel is locked, take the reference to its callid while we tear down the call. */
2231 callid = ast_channel_callid(chan);
2233
2234 ast_channel_unlock(chan);
2235
2236 /* Lock and unlock the channel just to be sure nobody has it locked still
2237 due to a reference that was stored in a datastore. (i.e. app_chanspy) */
2238 ast_channel_lock(chan);
2239 ast_channel_unlock(chan);
2240
2241 if (ast_channel_tech_pvt(chan)) {
2242 ast_log_callid(LOG_WARNING, callid, "Channel '%s' may not have been hung up properly\n", ast_channel_name(chan));
2244 }
2245
2246 if (ast_channel_sched(chan)) {
2248 }
2249
2251 char *dashptr;
2252
2253 ast_copy_string(device_name, ast_channel_name(chan), sizeof(device_name));
2254 if ((dashptr = strrchr(device_name, '-'))) {
2255 *dashptr = '\0';
2256 }
2257 } else {
2258 device_name[0] = '\0';
2259 }
2260
2261 /* Free translators */
2262 if (ast_channel_readtrans(chan))
2264 if (ast_channel_writetrans(chan))
2266 if (ast_channel_pbx(chan))
2267 ast_log_callid(LOG_WARNING, callid, "PBX may not have been terminated properly on '%s'\n", ast_channel_name(chan));
2268
2269 /* Free formats */
2275
2281
2282 /* Close pipes if appropriate */
2284 if (ast_channel_timer(chan)) {
2287 }
2289 ast_frfree(f);
2290
2291 /* loop over the variables list, freeing all data and deleting list items */
2292 /* no need to lock the list, as the channel is already locked */
2293 headp = ast_channel_varshead(chan);
2294 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
2295 ast_var_delete(vardata);
2296
2298
2299 /* Destroy the jitterbuffer */
2300 ast_jb_destroy(chan);
2301
2302 if (ast_channel_cdr(chan)) {
2305 }
2306
2307 if (ast_channel_zone(chan)) {
2309 }
2310
2312
2313 if (device_name[0]) {
2314 /*
2315 * We have a device name to notify of a new state.
2316 *
2317 * Queue an unknown state, because, while we know that this particular
2318 * instance is dead, we don't know the state of all other possible
2319 * instances.
2320 */
2322 }
2323
2325
2328
2330}
2331
2332/*! \brief Free a dummy channel structure */
2333static void ast_dummy_channel_destructor(void *obj)
2334{
2335 struct ast_channel *chan = obj;
2336 struct ast_datastore *datastore;
2337 struct ast_var_t *vardata;
2338 struct varshead *headp;
2339
2341
2342 /* Get rid of each of the data stores on the channel */
2343 while ((datastore = AST_LIST_REMOVE_HEAD(ast_channel_datastores(chan), entry))) {
2344 /* Free the data store */
2345 ast_datastore_free(datastore);
2346 }
2347
2353
2354 /* loop over the variables list, freeing all data and deleting list items */
2355 /* no need to lock the list, as the channel is already locked */
2356 headp = ast_channel_varshead(chan);
2357 while ((vardata = AST_LIST_REMOVE_HEAD(headp, entries)))
2358 ast_var_delete(vardata);
2359
2360 if (ast_channel_cdr(chan)) {
2363 }
2364
2366}
2367
2369{
2370 struct ast_datastore *datastore = NULL, *datastore2;
2371
2373 if (datastore->inheritance > 0) {
2374 datastore2 = ast_datastore_alloc(datastore->info, datastore->uid);
2375 if (datastore2) {
2376 datastore2->data = datastore->info->duplicate ? datastore->info->duplicate(datastore->data) : NULL;
2377 datastore2->inheritance = datastore->inheritance == DATASTORE_INHERIT_FOREVER ? DATASTORE_INHERIT_FOREVER : datastore->inheritance - 1;
2379 }
2380 }
2381 }
2382 return 0;
2383}
2384
2385int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
2386{
2387 int res = 0;
2388
2390
2391 return res;
2392}
2393
2394int ast_channel_datastore_remove(struct ast_channel *chan, struct ast_datastore *datastore)
2395{
2396 return AST_LIST_REMOVE(ast_channel_datastores(chan), datastore, entry) ? 0 : -1;
2397}
2398
2399struct ast_datastore *ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
2400{
2401 struct ast_datastore *datastore = NULL;
2402
2403 if (info == NULL)
2404 return NULL;
2405
2407 if (datastore->info != info) {
2408 continue;
2409 }
2410
2411 if (uid == NULL) {
2412 /* matched by type only */
2413 break;
2414 }
2415
2416 if ((datastore->uid != NULL) && !strcasecmp(uid, datastore->uid)) {
2417 /* Matched by type AND uid */
2418 break;
2419 }
2420 }
2421
2422 return datastore;
2423}
2424
2425/*! Set the file descriptor on the channel */
2426void ast_channel_set_fd(struct ast_channel *chan, int which, int fd)
2427{
2428 ast_channel_internal_fd_set(chan, which, fd);
2429 return;
2430}
2431
2433{
2434 ast_channel_lock(chan);
2435
2437
2439 struct ast_frame *fr;
2440
2441 /* If we have completely cleared the softhangup flag,
2442 * then we need to fully abort the hangup process. This requires
2443 * pulling the END_OF_Q frame out of the channel frame queue if it
2444 * still happens to be there. */
2445
2446 fr = AST_LIST_LAST(ast_channel_readq(chan));
2447 if (fr && fr->frametype == AST_FRAME_CONTROL &&
2450 ast_frfree(fr);
2451 }
2452 }
2453
2454 ast_channel_unlock(chan);
2455}
2456
2457/*! \brief Softly hangup a channel, don't lock */
2458int ast_softhangup_nolock(struct ast_channel *chan, int cause)
2459{
2460 ast_debug(1, "Soft-Hanging (%#04x) up channel '%s'\n", (unsigned)cause, ast_channel_name(chan));
2461 /* Inform channel driver that we need to be hung up, if it cares */
2464 /* Interrupt any poll call or such */
2466 pthread_kill(ast_channel_blocker(chan), SIGURG);
2467 return 0;
2468}
2469
2470/*! \brief Softly hangup a channel, lock */
2471int ast_softhangup(struct ast_channel *chan, int cause)
2472{
2473 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
2474 int res;
2475
2476 ast_channel_lock(chan);
2477 res = ast_softhangup_nolock(chan, cause);
2478 blob = ast_json_pack("{s: i, s: b}",
2479 "cause", cause,
2480 "soft", 1);
2482 ast_channel_unlock(chan);
2483
2484 return res;
2485}
2486
2487static void free_translation(struct ast_channel *clonechan)
2488{
2489 if (ast_channel_writetrans(clonechan)) {
2491 }
2492 if (ast_channel_readtrans(clonechan)) {
2494 }
2495 ast_channel_writetrans_set(clonechan, NULL);
2496 ast_channel_readtrans_set(clonechan, NULL);
2497}
2498
2499void ast_set_hangupsource(struct ast_channel *chan, const char *source, int force)
2500{
2502
2503 ast_channel_lock(chan);
2504 if (force || ast_strlen_zero(ast_channel_hangupsource(chan))) {
2505 ast_channel_hangupsource_set(chan, source);
2506 }
2507 ast_channel_unlock(chan);
2508
2509 if (bridge) {
2510 ast_channel_lock(bridge);
2511 if (force || ast_strlen_zero(ast_channel_hangupsource(bridge))) {
2512 ast_channel_hangupsource_set(bridge, source);
2513 }
2514 ast_channel_unlock(bridge);
2515 }
2516}
2517
2519{
2522}
2523
2525{
2528}
2529
2530static void destroy_hooks(struct ast_channel *chan)
2531{
2532 if (ast_channel_audiohooks(chan)) {
2535 }
2536
2538}
2539
2540/*! \brief Hangup a channel */
2541void ast_hangup(struct ast_channel *chan)
2542{
2543 /* Be NULL safe for RAII_VAR() usage. */
2544 if (!chan) {
2545 return;
2546 }
2547
2548 ast_debug(1, "Channel %p '%s' hanging up. Refs: %d\n", chan, ast_channel_name(chan),
2549 ao2_ref(chan, 0));
2550
2552
2553 ast_channel_lock(chan);
2554
2555 while (ast_channel_masq(chan) || ast_channel_masqr(chan)) {
2557 }
2558
2559 /* Mark as a zombie so a masquerade cannot be setup on this channel. */
2561
2562 ast_channel_unlock(chan);
2563
2564 /*
2565 * XXX if running the hangup handlers here causes problems
2566 * because the handlers take too long to execute, we could move
2567 * the meat of this function into another thread. A thread
2568 * where channels go to die.
2569 *
2570 * If this is done, ast_autoservice_chan_hangup_peer() will no
2571 * longer be needed.
2572 */
2574 ao2_unlink(channels, chan);
2575 ast_channel_lock(chan);
2576
2577 destroy_hooks(chan);
2578
2579 free_translation(chan);
2580 /* Close audio stream */
2581 if (ast_channel_stream(chan)) {
2584 }
2585 /* Close video stream */
2586 if (ast_channel_vstream(chan)) {
2589 }
2590 if (ast_channel_sched(chan)) {
2593 }
2594
2595 if (ast_channel_generatordata(chan)) { /* Clear any tone stuff remaining */
2596 if (ast_channel_generator(chan) && ast_channel_generator(chan)->release) {
2598 }
2599 }
2602
2604 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",
2606 ast_channel_blockproc(chan));
2607 ast_assert(0);
2608 }
2609
2610 if (ast_channel_tech(chan)->hangup) {
2611 ast_channel_tech(chan)->hangup(chan);
2612 }
2613
2614 ast_channel_unlock(chan);
2615
2616 ast_cc_offer(chan);
2617
2618 ast_channel_unref(chan);
2619}
2620
2621/*!
2622 * \internal
2623 * \brief Set channel answered time if not already set.
2624 * \since 13.11.0
2625 *
2626 * \param chan Channel to set answered time.
2627 */
2628static void set_channel_answer_time(struct ast_channel *chan)
2629{
2631 struct timeval answertime;
2632
2633 answertime = ast_tvnow();
2634 ast_channel_answertime_set(chan, &answertime);
2635 }
2636}
2637
2638
2640{
2641 int res = 0;
2642 SCOPE_TRACE(1, "%s\n", ast_channel_name(chan));
2643
2644 ast_channel_lock(chan);
2645
2646 /* You can't answer an outbound call */
2648 ast_channel_unlock(chan);
2649 return 0;
2650 }
2651
2652 /* Stop if we're a zombie or need a soft hangup */
2654 ast_channel_unlock(chan);
2655 return -1;
2656 }
2657
2658 /*
2659 * Mark when incoming channel answered so we can know how
2660 * long the channel has been up.
2661 */
2663
2664 ast_channel_unlock(chan);
2665
2666 switch (ast_channel_state(chan)) {
2667 case AST_STATE_RINGING:
2668 case AST_STATE_RING:
2669 ast_channel_lock(chan);
2670 if (ast_channel_tech(chan)->answer_with_stream_topology) {
2671 res = ast_channel_tech(chan)->answer_with_stream_topology(chan, topology);
2672
2673 } else if (ast_channel_tech(chan)->answer) {
2674 res = ast_channel_tech(chan)->answer(chan);
2675 }
2677 ast_channel_unlock(chan);
2678 break;
2679 case AST_STATE_UP:
2680 break;
2681 default:
2682 break;
2683 }
2684
2685 ast_indicate(chan, -1);
2686
2687 return res;
2688}
2689
2691{
2693}
2694
2695int __ast_answer(struct ast_channel *chan, unsigned int delay)
2696{
2697 int res = 0;
2698 enum ast_channel_state old_state;
2699 SCOPE_TRACE(1, "%s\n", ast_channel_name(chan));
2700
2701 old_state = ast_channel_state(chan);
2702 if ((res = ast_raw_answer(chan))) {
2703 return res;
2704 }
2705
2706 switch (old_state) {
2707 case AST_STATE_RINGING:
2708 case AST_STATE_RING:
2709 /* wait for media to start flowing, but don't wait any longer
2710 * than 'delay' or 500 milliseconds, whichever is longer
2711 */
2712 do {
2714 struct ast_frame *cur;
2715 struct ast_frame *new_frame;
2716 int timeout_ms = MAX(delay, 500);
2717 unsigned int done = 0;
2718 struct timeval start;
2719
2721
2722 start = ast_tvnow();
2723 for (;;) {
2724 int ms = ast_remaining_ms(start, timeout_ms);
2725 ms = ast_waitfor(chan, ms);
2726 if (ms < 0) {
2727 ast_log(LOG_WARNING, "Error condition occurred when polling channel %s for a voice frame: %s\n", ast_channel_name(chan), strerror(errno));
2728 res = -1;
2729 break;
2730 }
2731 if (ms == 0) {
2732 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));
2733 break;
2734 }
2735 cur = ast_read(chan);
2736 if (!cur || ((cur->frametype == AST_FRAME_CONTROL) &&
2737 (cur->subclass.integer == AST_CONTROL_HANGUP))) {
2738 if (cur) {
2739 ast_frfree(cur);
2740 }
2741 res = -1;
2742 ast_debug(2, "Hangup of channel %s detected in answer routine\n", ast_channel_name(chan));
2743 break;
2744 }
2745
2746 if ((new_frame = ast_frisolate(cur)) != cur) {
2747 ast_frfree(cur);
2748 }
2749
2751
2752 /* if a specific delay period was requested, continue
2753 * until that delay has passed. don't stop just because
2754 * incoming media has arrived.
2755 */
2756 if (delay) {
2757 continue;
2758 }
2759
2760 switch (new_frame->frametype) {
2761 /* all of these frametypes qualify as 'media' */
2762 case AST_FRAME_VOICE:
2763 case AST_FRAME_VIDEO:
2764 case AST_FRAME_TEXT:
2767 case AST_FRAME_DTMF_END:
2768 case AST_FRAME_IMAGE:
2769 case AST_FRAME_HTML:
2770 case AST_FRAME_MODEM:
2771 case AST_FRAME_RTCP:
2772 done = 1;
2773 break;
2774 case AST_FRAME_CONTROL:
2775 case AST_FRAME_IAX:
2778 case AST_FRAME_NULL:
2779 case AST_FRAME_CNG:
2780 break;
2781 }
2782
2783 if (done) {
2784 break;
2785 }
2786 }
2787
2788 ast_channel_lock(chan);
2789 while ((cur = AST_LIST_REMOVE_HEAD(&frames, frame_list))) {
2790 if (res == 0) {
2791 ast_queue_frame_head(chan, cur);
2792 }
2793 ast_frfree(cur);
2794 }
2795 ast_channel_unlock(chan);
2796 } while (0);
2797 break;
2798 default:
2799 break;
2800 }
2801
2802 return res;
2803}
2804
2805int ast_answer(struct ast_channel *chan)
2806{
2807 SCOPE_TRACE(1, "%s\n", ast_channel_name(chan));
2808 return __ast_answer(chan, 0);
2809}
2810
2811inline int ast_auto_answer(struct ast_channel *chan)
2812{
2813 if (ast_channel_state(chan) == AST_STATE_UP) {
2814 /* Already answered */
2815 return 0;
2816 }
2817 return ast_answer(chan);
2818}
2819
2821{
2822 ast_assert(NULL != chan);
2823
2825 return 0;
2826 }
2828}
2829
2831{
2832 return (ast_channel_get_duration_ms(chan) / 1000);
2833}
2834
2836{
2837 ast_assert(NULL != chan);
2838
2840 return 0;
2841 }
2843}
2844
2846{
2847 return (ast_channel_get_up_time_ms(chan) / 1000);
2848}
2849
2850/*!
2851 * \brief Determine whether or not we have to trigger dtmf emulating using 50 fps timer events
2852 * especially when no voice frames are received during dtmf processing (direct media or muted
2853 * sender case using SIP INFO)
2854 */
2855static inline int should_trigger_dtmf_emulating(struct ast_channel *chan)
2856{
2858 /* We're in the middle of emulating a digit, or DTMF has been
2859 * explicitly deferred. Trigger dtmf with periodic 50 pfs timer events, then. */
2860 return 1;
2861 }
2862
2863 if (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
2865 /*
2866 * We're not in the middle of a digit, but it hasn't been long enough
2867 * since the last digit, so we'll have to trigger DTMF further on.
2868 * Using 2 times AST_MIN_DTMF_GAP to trigger readq reading for possible
2869 * buffered next dtmf event
2870 */
2871 return 1;
2872 }
2873
2874 return 0;
2875}
2876
2878{
2879 if (ast_channel_generatordata(chan)) {
2880 struct ast_generator *generator = ast_channel_generator(chan);
2881
2882 if (generator && generator->release) {
2883 generator->release(chan, ast_channel_generatordata(chan));
2884 }
2889 ast_settimeout(chan, 0, NULL, NULL);
2890 }
2891}
2892
2894{
2895 ast_channel_lock(chan);
2898 /* if in the middle of dtmf emulation keep 50 tick per sec timer on rolling */
2900 }
2901 ast_channel_unlock(chan);
2902}
2903
2905{
2906 struct ast_generator *generator;
2907
2908 ast_channel_lock(chan);
2909 generator = ast_channel_generator(chan);
2910 if (generator && generator->write_format_change) {
2911 generator->write_format_change(chan, ast_channel_generatordata(chan));
2912 }
2913 ast_channel_unlock(chan);
2914}
2915
2916static int generator_force(const void *data)
2917{
2918 /* Called if generator doesn't have data */
2919 void *tmp;
2920 int res;
2921 int (*generate)(struct ast_channel *chan, void *tmp, int datalen, int samples) = NULL;
2922 struct ast_channel *chan = (struct ast_channel *)data;
2923
2924 ast_channel_lock(chan);
2927 if (ast_channel_generator(chan))
2928 generate = ast_channel_generator(chan)->generate;
2929 ast_channel_unlock(chan);
2930
2931 if (!tmp || !generate) {
2932 return 0;
2933 }
2934
2935 res = generate(chan, tmp, 0, ast_format_get_sample_rate(ast_channel_writeformat(chan)) / 50);
2936
2937 ast_channel_lock(chan);
2938 if (ast_channel_generator(chan) && generate == ast_channel_generator(chan)->generate) {
2940 }
2941 ast_channel_unlock(chan);
2942
2943 if (res) {
2944 ast_debug(1, "Auto-deactivating generator\n");
2946 }
2947
2948 return 0;
2949}
2950
2951int ast_activate_generator(struct ast_channel *chan, struct ast_generator *gen, void *params)
2952{
2953 int res = 0;
2954 void *generatordata = NULL;
2955
2956 ast_channel_lock(chan);
2957 if (ast_channel_generatordata(chan)) {
2958 struct ast_generator *generator_old = ast_channel_generator(chan);
2959
2960 if (generator_old && generator_old->release) {
2961 generator_old->release(chan, ast_channel_generatordata(chan));
2962 }
2963 }
2964 if (gen->alloc && !(generatordata = gen->alloc(chan, params))) {
2965 res = -1;
2966 }
2967 ast_channel_generatordata_set(chan, generatordata);
2968 if (!res) {
2969 ast_settimeout(chan, 50, generator_force, chan);
2971 }
2972 ast_channel_unlock(chan);
2973
2974 ast_prod(chan);
2975
2976 return res;
2977}
2978
2979/*! \brief Wait for x amount of time on a file descriptor to have input. */
2980int ast_waitfor_n_fd(int *fds, int n, int *ms, int *exception)
2981{
2982 int winner = -1;
2983 ast_waitfor_nandfds(NULL, 0, fds, n, exception, &winner, ms);
2984 return winner;
2985}
2986
2987/*! \brief Wait for x amount of time on a file descriptor to have input. */
2988struct ast_channel *ast_waitfor_nandfds(struct ast_channel **c, int n, int *fds, int nfds,
2989 int *exception, int *outfd, int *ms)
2990{
2991 struct timeval start = { 0 , 0 };
2992 struct pollfd *pfds = NULL;
2993 int res;
2994 long rms;
2995 int x, y, max;
2996 int sz = nfds;
2997 struct timeval now = { 0, 0 };
2998 struct timeval whentohangup = { 0, 0 }, diff;
2999 struct ast_channel *winner = NULL;
3000 struct fdmap {
3001 int chan;
3002 int fdno;
3003 } *fdmap = NULL;
3004
3005 if (outfd) {
3006 *outfd = -99999;
3007 }
3008 if (exception) {
3009 *exception = 0;
3010 }
3011
3012 for (x = 0; x < n; x++) {
3013 ast_channel_lock(c[x]);
3015 if (ast_tvzero(whentohangup))
3016 now = ast_tvnow();
3017 diff = ast_tvsub(*ast_channel_whentohangup(c[x]), now);
3018 if (diff.tv_sec < 0 || ast_tvzero(diff)) {
3019 ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(c[x]));
3020 /* Should already be hungup */
3023 return c[x];
3024 }
3025 if (ast_tvzero(whentohangup) || ast_tvcmp(diff, whentohangup) < 0)
3026 whentohangup = diff;
3027 }
3028 sz += ast_channel_fd_count(c[x]);
3030 }
3031
3032 if (!sz) {
3033 return NULL;
3034 }
3035
3036 pfds = ast_alloca(sizeof(*pfds) * sz);
3037 fdmap = ast_alloca(sizeof(*fdmap) * sz);
3038
3039 /* Wait full interval */
3040 rms = *ms;
3041 /* INT_MAX, not LONG_MAX, because it matters on 64-bit */
3042 if (!ast_tvzero(whentohangup) && whentohangup.tv_sec < INT_MAX / 1000) {
3043 rms = whentohangup.tv_sec * 1000 + whentohangup.tv_usec / 1000; /* timeout in milliseconds */
3044 if (*ms >= 0 && *ms < rms) { /* original *ms still smaller */
3045 rms = *ms;
3046 }
3047 } else if (!ast_tvzero(whentohangup) && rms < 0) {
3048 /* Tiny corner case... call would need to last >24 days */
3049 rms = INT_MAX;
3050 }
3051 /*
3052 * Build the pollfd array, putting the channels' fds first,
3053 * followed by individual fds. Order is important because
3054 * individual fd's must have priority over channel fds.
3055 */
3056 max = 0;
3057 for (x = 0; x < n; x++) {
3058 ast_channel_lock(c[x]);
3059 for (y = 0; y < ast_channel_fd_count(c[x]); y++) {
3060 fdmap[max].fdno = y; /* fd y is linked to this pfds */
3061 fdmap[max].chan = x; /* channel x is linked to this pfds */
3062 max += ast_add_fd(&pfds[max], ast_channel_fd(c[x], y));
3063 }
3064 CHECK_BLOCKING(c[x]);
3066 }
3067 /* Add the individual fds */
3068 for (x = 0; x < nfds; x++) {
3069 fdmap[max].chan = -1;
3070 max += ast_add_fd(&pfds[max], fds[x]);
3071 }
3072
3073 if (*ms > 0) {
3074 start = ast_tvnow();
3075 }
3076
3077 if (sizeof(int) == 4) { /* XXX fix timeout > 600000 on linux x86-32 */
3078 do {
3079 int kbrms = rms;
3080 if (kbrms > 600000) {
3081 kbrms = 600000;
3082 }
3083 res = ast_poll(pfds, max, kbrms);
3084 if (!res) {
3085 rms -= kbrms;
3086 }
3087 } while (!res && (rms > 0));
3088 } else {
3089 res = ast_poll(pfds, max, rms);
3090 }
3091 for (x = 0; x < n; x++) {
3092 ast_channel_lock(c[x]);
3095 }
3096 if (res < 0) { /* Simulate a timeout if we were interrupted */
3097 if (errno != EINTR) {
3098 *ms = -1;
3099 }
3100 return NULL;
3101 }
3102 if (!ast_tvzero(whentohangup)) { /* if we have a timeout, check who expired */
3103 now = ast_tvnow();
3104 for (x = 0; x < n; x++) {
3106 ast_test_suite_event_notify("HANGUP_TIME", "Channel: %s", ast_channel_name(c[x]));
3108 if (winner == NULL) {
3109 winner = c[x];
3110 }
3111 }
3112 }
3113 }
3114 if (res == 0) { /* no fd ready, reset timeout and done */
3115 *ms = 0; /* XXX use 0 since we may not have an exact timeout. */
3116 return winner;
3117 }
3118 /*
3119 * Then check if any channel or fd has a pending event.
3120 * Remember to check channels first and fds last, as they
3121 * must have priority on setting 'winner'
3122 */
3123 for (x = 0; x < max; x++) {
3124 res = pfds[x].revents;
3125 if (res == 0) {
3126 continue;
3127 }
3128 if (fdmap[x].chan >= 0) { /* this is a channel */
3129 winner = c[fdmap[x].chan]; /* override previous winners */
3130 ast_channel_lock(winner);
3131 if (res & POLLPRI) {
3133 } else {
3135 }
3136 ast_channel_fdno_set(winner, fdmap[x].fdno);
3137 ast_channel_unlock(winner);
3138 } else { /* this is an fd */
3139 if (outfd) {
3140 *outfd = pfds[x].fd;
3141 }
3142 if (exception) {
3143 *exception = (res & POLLPRI) ? -1 : 0;
3144 }
3145 winner = NULL;
3146 }
3147 }
3148 if (*ms > 0) {
3149 *ms -= ast_tvdiff_ms(ast_tvnow(), start);
3150 if (*ms < 0) {
3151 *ms = 0;
3152 }
3153 }
3154 return winner;
3155}
3156
3157struct ast_channel *ast_waitfor_n(struct ast_channel **c, int n, int *ms)
3158{
3159 return ast_waitfor_nandfds(c, n, NULL, 0, NULL, NULL, ms);
3160}
3161
3162int ast_waitfor(struct ast_channel *c, int ms)
3163{
3164 if (ms < 0) {
3165 do {
3166 ms = 100000;
3167 ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
3168 } while (!ms);
3169 } else {
3170 ast_waitfor_nandfds(&c, 1, NULL, 0, NULL, NULL, &ms);
3171 }
3172 return ms;
3173}
3174
3175int ast_waitfordigit(struct ast_channel *c, int ms)
3176{
3177 return ast_waitfordigit_full(c, ms, NULL, -1, -1);
3178}
3179
3180int ast_settimeout(struct ast_channel *c, unsigned int rate, int (*func)(const void *data), void *data)
3181{
3182 return ast_settimeout_full(c, rate, func, data, 0);
3183}
3184
3185int ast_settimeout_full(struct ast_channel *c, unsigned int rate, int (*func)(const void *data), void *data, unsigned int is_ao2_obj)
3186{
3187 int res;
3188 unsigned int real_rate = rate, max_rate;
3189
3191
3192 if (ast_channel_timingfd(c) == -1) {
3194 return -1;
3195 }
3196
3197 if (!func) {
3198 rate = 0;
3199 data = NULL;
3200 }
3201
3202 if (rate && rate > (max_rate = ast_timer_get_max_rate(ast_channel_timer(c)))) {
3203 real_rate = max_rate;
3204 }
3205
3206 ast_debug(3, "Scheduling timer at (%u requested / %u actual) timer ticks per second\n", rate, real_rate);
3207
3208 res = ast_timer_set_rate(ast_channel_timer(c), real_rate);
3209
3212 }
3213
3216
3217 if (data && is_ao2_obj) {
3218 ao2_ref(data, 1);
3220 } else {
3222 }
3223
3224 if (func == NULL && rate == 0 && ast_channel_fdno(c) == AST_TIMING_FD) {
3225 /* Clearing the timing func and setting the rate to 0
3226 * means that we don't want to be reading from the timingfd
3227 * any more. Setting c->fdno to -1 means we won't have any
3228 * errant reads from the timingfd, meaning we won't potentially
3229 * miss any important frames.
3230 */
3232 }
3233
3235
3236 return res;
3237}
3238
3239int ast_waitfordigit_full(struct ast_channel *c, int timeout_ms, const char *breakon, int audiofd, int cmdfd)
3240{
3241 struct timeval start = ast_tvnow();
3242 int ms;
3243
3244 /* Stop if we're a zombie or need a soft hangup */
3246 return -1;
3247
3248 /* Only look for the end of DTMF, don't bother with the beginning and don't emulate things */
3250
3251 /* Wait for a digit, no more than timeout_ms milliseconds total.
3252 * Or, wait indefinitely if timeout_ms is <0.
3253 */
3254 while ((ms = ast_remaining_ms(start, timeout_ms))) {
3255 struct ast_channel *rchan;
3256 int outfd = -1;
3257
3258 errno = 0;
3259 /* While ast_waitfor_nandfds tries to help by reducing the timeout by how much was waited,
3260 * it is unhelpful if it waited less than a millisecond.
3261 */
3262 rchan = ast_waitfor_nandfds(&c, 1, &cmdfd, (cmdfd > -1) ? 1 : 0, NULL, &outfd, &ms);
3263
3264 if (!rchan && outfd < 0 && ms) {
3265 if (errno == 0 || errno == EINTR)
3266 continue;
3267 ast_log(LOG_WARNING, "Wait failed (%s)\n", strerror(errno));
3269 return -1;
3270 } else if (outfd > -1) {
3271 /* The FD we were watching has something waiting */
3272 ast_log(LOG_WARNING, "The FD we were waiting for has something waiting. Waitfordigit returning numeric 1\n");
3274 return 1;
3275 } else if (rchan) {
3276 int res;
3277 struct ast_frame *f = ast_read(c);
3278
3279 if (!f) {
3281
3282 return -1;
3283 }
3284
3285 switch (f->frametype) {
3287 break;
3288 case AST_FRAME_DTMF_END:
3289 res = f->subclass.integer;
3290 if (!breakon || strchr(breakon, res)) {
3291 ast_frfree(f);
3293 return res;
3294 }
3295 break;
3296 case AST_FRAME_CONTROL:
3297 switch (f->subclass.integer) {
3298 case AST_CONTROL_HANGUP:
3299 ast_frfree(f);
3301 return -1;
3307 /* Fall-through and treat as if it were a DTMF signal. Items
3308 * that perform stream control will handle this. */
3309 res = f->subclass.integer;
3310 ast_frfree(f);
3312 return res;
3316 case AST_CONTROL_ANSWER:
3322 case AST_CONTROL_HOLD:
3323 case AST_CONTROL_UNHOLD:
3324 case AST_CONTROL_FLASH:
3325 case -1:
3326 /* Unimportant */
3327 break;
3328 default:
3329 ast_log(LOG_WARNING, "Unexpected control subclass '%d'\n", f->subclass.integer);
3330 break;
3331 }
3332 break;
3333 case AST_FRAME_VOICE:
3334 /* Write audio if appropriate */
3335 if (audiofd > -1) {
3336 if (write(audiofd, f->data.ptr, f->datalen) < 0) {
3337 ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno));
3338 }
3339 }
3340 default:
3341 /* Ignore */
3342 break;
3343 }
3344 ast_frfree(f);
3345 }
3346 }
3347
3349
3350 return 0; /* Time is up */
3351}
3352
3355 DTMF_SENT
3357
3359{
3360 switch (direction) {
3361 case DTMF_RECEIVED:
3362 return "Received";
3363 case DTMF_SENT:
3364 return "Sent";
3365 }
3366
3367 return "?";
3368}
3369
3370static void send_dtmf_begin_event(struct ast_channel *chan,
3371 enum DtmfDirection direction, const char digit)
3372{
3373 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
3374 char digit_str[] = { digit, '\0' };
3375
3376 blob = ast_json_pack("{ s: s, s: s }",
3377 "digit", digit_str,
3378 "direction", dtmf_direction_to_string(direction));
3379 if (!blob) {
3380 return;
3381 }
3382
3384}
3385
3386static void send_dtmf_end_event(struct ast_channel *chan,
3387 enum DtmfDirection direction, const char digit, long duration_ms)
3388{
3389 RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
3390 char digit_str[] = { digit, '\0' };
3391
3392 blob = ast_json_pack("{ s: s, s: s, s: I }",
3393 "digit", digit_str,
3394 "direction", dtmf_direction_to_string(direction),
3395 "duration_ms", (ast_json_int_t)duration_ms);
3396 if (!blob) {
3397 return;
3398 }
3399
3401}
3402
3403static void send_flash_event(struct ast_channel *chan)
3404{
3406}
3407
3408static void send_wink_event(struct ast_channel *chan)
3409{
3411}
3412
3413static void ast_read_generator_actions(struct ast_channel *chan, struct ast_frame *f)
3414{
3415 struct ast_generator *generator;
3416 void *gendata;
3417 int res;
3418 int samples;
3419
3420 generator = ast_channel_generator(chan);
3421 if (!generator
3422 || !generator->generate
3423 || f->frametype != AST_FRAME_VOICE
3425 || ast_channel_timingfunc(chan)) {
3426 return;
3427 }
3428
3429 /*
3430 * We must generate frames in phase locked mode since
3431 * we have no internal timer available.
3432 */
3434 float factor;
3436 samples = (int) (((float) f->samples) * factor);
3437 } else {
3438 samples = f->samples;
3439 }
3440
3441 gendata = ast_channel_generatordata(chan);
3442 ast_channel_generatordata_set(chan, NULL); /* reset, to let writes go through */
3443
3444 /*
3445 * This unlock is here based on two assumptions that hold true at
3446 * this point in the code. 1) this function is only called from
3447 * within __ast_read() and 2) all generators call ast_write() in
3448 * their generate callback.
3449 *
3450 * The reason this is added is so that when ast_write is called,
3451 * the lock that occurs there will not recursively lock the
3452 * channel. Doing this will allow deadlock avoidance to work in
3453 * deeper functions.
3454 */
3455 ast_channel_unlock(chan);
3456 res = generator->generate(chan, gendata, f->datalen, samples);
3457 ast_channel_lock(chan);
3458 if (generator == ast_channel_generator(chan)) {
3459 ast_channel_generatordata_set(chan, gendata);
3460 if (res) {
3461 ast_debug(1, "Auto-deactivating generator\n");
3463 }
3464 }
3465}
3466
3467static inline void queue_dtmf_readq(struct ast_channel *chan, struct ast_frame *f)
3468{
3469 struct ast_frame *fr = ast_channel_dtmff(chan);
3470
3473 fr->len = f->len;
3474
3475 /* The only time this function will be called is for a frame that just came
3476 * out of the channel driver. So, we want to stick it on the tail of the
3477 * readq. */
3478
3479 ast_queue_frame(chan, fr);
3480}
3481
3482/*!
3483 * \brief Determine whether or not we should ignore DTMF in the readq
3484 */
3485static inline int should_skip_dtmf(struct ast_channel *chan)
3486{
3488 /* We're in the middle of emulating a digit, or DTMF has been
3489 * explicitly deferred. Skip this digit, then. */
3490 return 1;
3491 }
3492
3493 if (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
3495 /* We're not in the middle of a digit, but it hasn't been long enough
3496 * since the last digit, so we'll have to skip DTMF for now. */
3497 return 1;
3498 }
3499
3500 return 0;
3501}
3502
3503/*!
3504 * \brief calculates the number of samples to jump forward with in a monitor stream.
3505
3506 * \note When using ast_seekstream() with the read and write streams of a monitor,
3507 * the number of samples to seek forward must be of the same sample rate as the stream
3508 * or else the jump will not be calculated correctly.
3509 *
3510 * \retval number of samples to seek forward after rate conversion.
3511 */
3512static inline int calc_monitor_jump(int samples, int sample_rate, int seek_rate)
3513{
3514 int diff = sample_rate - seek_rate;
3515
3516 if (diff > 0) {
3517 samples = samples / (float) (sample_rate / seek_rate);
3518 } else if (diff < 0) {
3519 samples = samples * (float) (seek_rate / sample_rate);
3520 }
3521
3522 return samples;
3523}
3524
3525static struct ast_frame *__ast_read(struct ast_channel *chan, int dropaudio, int dropnondefault)
3526{
3527 struct ast_frame *f = NULL; /* the return value */
3528 int prestate;
3529 int cause = 0;
3530 struct ast_stream *stream = NULL, *default_stream = NULL;
3531
3532 /* this function is very long so make sure there is only one return
3533 * point at the end (there are only two exceptions to this).
3534 */
3535 ast_channel_lock(chan);
3536
3537 /* Stop if we're a zombie or need a soft hangup */
3539 if (ast_channel_generator(chan))
3541
3542 /*
3543 * It is possible for chan->_softhangup to be set and there
3544 * still be control frames that need to be read. Instead of
3545 * just going to 'done' in the case of ast_check_hangup(), we
3546 * need to queue the end-of-Q frame so that it can mark the end
3547 * of the read queue. If there are frames to be read,
3548 * ast_queue_control() will be called repeatedly, but will only
3549 * queue the first end-of-Q frame.
3550 */
3553 } else {
3554 goto done;
3555 }
3556 } else {
3557#ifdef AST_DEVMODE
3558 /*
3559 * The ast_waitfor() code records which of the channel's file
3560 * descriptors reported that data is available. In theory,
3561 * ast_read() should only be called after ast_waitfor() reports
3562 * that a channel has data available for reading. However,
3563 * there still may be some edge cases throughout the code where
3564 * ast_read() is called improperly. This can potentially cause
3565 * problems, so if this is a developer build, make a lot of
3566 * noise if this happens so that it can be addressed.
3567 *
3568 * One of the potential problems is blocking on a dead channel.
3569 */
3570 if (ast_channel_fdno(chan) == -1) {
3572 "ast_read() on chan '%s' called with no recorded file descriptor.\n",
3573 ast_channel_name(chan));
3574 }
3575#endif
3576 }
3577
3578 prestate = ast_channel_state(chan);
3579
3580 if (ast_channel_timingfd(chan) > -1 && ast_channel_fdno(chan) == AST_TIMING_FD) {
3581 enum ast_timer_event res;
3582 int trigger_dtmf_emulating = should_trigger_dtmf_emulating(chan);
3583
3585
3587
3588 switch (res) {
3590 if (ast_timer_ack(ast_channel_timer(chan), 1) < 0) {
3591 ast_log(LOG_ERROR, "Failed to acknowledge timer in ast_read\n");
3592 goto done;
3593 }
3594
3595 if (ast_channel_timingfunc(chan)) {
3596 /* save a copy of func/data before unlocking the channel */
3598 void *data = ast_channel_timingdata(chan);
3599 int got_ref = 0;
3601 ao2_ref(data, 1);
3602 got_ref = 1;
3603 }
3604 ast_channel_fdno_set(chan, -1);
3605 ast_channel_unlock(chan);
3606 func(data);
3607 if (got_ref) {
3608 ao2_ref(data, -1);
3609 }
3610
3611 if (trigger_dtmf_emulating) {
3612 /*
3613 * Since we're breaking out of this switch block and not
3614 * returning, we need to re-lock the channel.
3615 */
3616 ast_channel_lock(chan);
3617 /* generate null frame to trigger dtmf emulating */
3618 f = &ast_null_frame;
3619 break;
3620 }
3621 } else if (trigger_dtmf_emulating) {
3622 /* generate null frame to trigger dtmf emulating */
3623 f = &ast_null_frame;
3624 break;
3625 } else {
3627 /* generate very last null frame to trigger dtmf emulating */
3628 f = &ast_null_frame;
3629 break;
3630 }
3631
3632 /* cannot 'goto done' because the channel is already unlocked */
3633 return &ast_null_frame;
3634
3636 if (AST_LIST_EMPTY(ast_channel_readq(chan)) ||
3639 }
3640 break;
3641 }
3642
3644 /* if the AST_GENERATOR_FD is set, call the generator with args
3645 * set to -1 so it can do whatever it needs to.
3646 */
3647 void *tmp = ast_channel_generatordata(chan);
3648 ast_channel_generatordata_set(chan, NULL); /* reset to let ast_write get through */
3649 ast_channel_generator(chan)->generate(chan, tmp, -1, -1);
3651 f = &ast_null_frame;
3652 ast_channel_fdno_set(chan, -1);
3653 goto done;
3656 }
3657
3658 /* Read and ignore anything on the alertpipe, but read only
3659 one sizeof(blah) per frame that we send from it */
3661 f = &ast_null_frame;
3662 goto done;
3663 }
3664
3665 /* Check for pending read queue */
3666 if (!AST_LIST_EMPTY(ast_channel_readq(chan))) {
3667 int skipped_dtmf_frame = 0;
3668 int skip_dtmf = should_skip_dtmf(chan);
3669
3671 /* We have to be picky about which frame we pull off of the readq because
3672 * there are cases where we want to leave DTMF frames on the queue until
3673 * some later time. */
3674
3675 if ( (f->frametype == AST_FRAME_DTMF_BEGIN || f->frametype == AST_FRAME_DTMF_END) && skip_dtmf) {
3676 skipped_dtmf_frame = 1;
3677 continue;
3678 }
3679
3681 break;
3682 }
3684
3685 if (!f) {
3686 /* There were no acceptable frames on the readq. */
3687 f = &ast_null_frame;
3688 if (!skipped_dtmf_frame) {
3689 /*
3690 * Do not trigger alert pipe if only buffered dtmf begin or end frames
3691 * are left in the readq.
3692 */
3694 } else {
3695 /*
3696 * Safely disable continous timer events if only buffered dtmf begin or end
3697 * frames are left in the readq.
3698 */
3700 }
3701 }
3702
3703 /* Interpret hangup and end-of-Q frames to return NULL */
3704 /* XXX why not the same for frames from the channel ? */
3705 if (f->frametype == AST_FRAME_CONTROL) {
3706 switch (f->subclass.integer) {
3707 case AST_CONTROL_HANGUP:
3709 cause = f->data.uint32;
3710 /* Fall through */
3712 ast_frfree(f);
3713 f = NULL;
3714 break;
3715 default:
3716 break;
3717 }
3718 } else if (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO) {
3719 if (ast_channel_tech(chan) && ast_channel_tech(chan)->read_stream) {
3722 } else {
3723 /* Since this channel driver does not support multistream determine the default stream this frame
3724 * originated from and update the frame to include it.
3725 */
3726 stream = default_stream = ast_channel_get_default_stream(chan, ast_format_get_type(f->subclass.format));
3727 /* In order to allow media to be passed up the underlying media type has to have a format negotiated on
3728 * the channel itself. In cases where this hasn't happened the channel driver is incorrectly passing up
3729 * a frame for a format that has not been negotiated. If this occurs just drop the frame as we have no
3730 * stream that it came from.
3731 */
3732 if (!stream) {
3733 ast_frfree(f);
3734 f = &ast_null_frame;
3735 } else {
3737 }
3738 }
3739 }
3740 } else {
3742 if (ast_channel_tech(chan)->exception)
3743 f = ast_channel_tech(chan)->exception(chan);
3744 else {
3745 ast_log(LOG_WARNING, "Exception flag set on '%s', but no exception handler\n", ast_channel_name(chan));
3746 f = &ast_null_frame;
3747 }
3748 /* Clear the exception flag */
3750 } else if (ast_channel_tech(chan) && ast_channel_tech(chan)->read_stream) {
3751 f = ast_channel_tech(chan)->read_stream(chan);
3752
3753 /* This channel driver supports multistream so the stream_num on the frame is valid, the only
3754 * thing different is that we need to find the default stream so we know whether to invoke the
3755 * default stream logic or not (such as transcoding).
3756 */
3757 if (f && (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO)) {
3760 }
3761 } else if (ast_channel_tech(chan) && ast_channel_tech(chan)->read) {
3762 f = ast_channel_tech(chan)->read(chan);
3763
3764 /* Since this channel driver does not support multistream determine the default stream this frame
3765 * originated from and update the frame to include it.
3766 */
3767 if (f && (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO)) {
3768 stream = default_stream = ast_channel_get_default_stream(chan, ast_format_get_type(f->subclass.format));
3769 if (!stream) {
3770 ast_frfree(f);
3771 f = &ast_null_frame;
3772 } else {
3774 }
3775 }
3776 }
3777 else
3778 ast_log(LOG_WARNING, "No read routine on channel %s\n", ast_channel_name(chan));
3779 }
3780
3781 if (stream == default_stream) {
3782 /* Perform the framehook read event here. After the frame enters the framehook list
3783 * there is no telling what will happen, <insert mad scientist laugh here>!!! */
3785 }
3786
3787 /*
3788 * Reset the recorded file descriptor that triggered this read so that we can
3789 * easily detect when ast_read() is called without properly using ast_waitfor().
3790 */
3791 ast_channel_fdno_set(chan, -1);
3792
3793 if (f) {
3794 struct ast_frame *readq_tail = AST_LIST_LAST(ast_channel_readq(chan));
3795 struct ast_control_read_action_payload *read_action_payload;
3797 int hooked = 0;
3798
3799 /* if the channel driver returned more than one frame, stuff the excess
3800 into the readq for the next ast_read call
3801 */
3802 if (AST_LIST_NEXT(f, frame_list)) {
3806 }
3807
3808 if (dropnondefault && stream != default_stream) {
3809 /* If the frame originates from a non-default stream and the caller can not handle other streams
3810 * absorb the frame and replace it with a null one instead.
3811 */
3812 ast_frfree(f);
3813 f = &ast_null_frame;
3814 }
3815
3816 switch (f->frametype) {
3817 case AST_FRAME_CONTROL:
3819 if (prestate == AST_STATE_UP && ast_channel_is_bridged(chan)) {
3820 ast_debug(1, "Dropping duplicate answer!\n");
3821 ast_frfree(f);
3822 f = &ast_null_frame;
3823 } else {
3824 /*
3825 * Mark when outgoing channel answered so we can know how
3826 * long the channel has been up.
3827 */
3829
3831 }
3832 } else if (f->subclass.integer == AST_CONTROL_READ_ACTION) {
3833 read_action_payload = f->data.ptr;
3834 switch (read_action_payload->action) {
3838 if (ast_connected_line_parse_data(read_action_payload->payload,
3839 read_action_payload->payload_size, &connected)) {
3841 break;
3842 }
3843 ast_channel_unlock(chan);
3846 read_action_payload->payload,
3847 read_action_payload->payload_size);
3848 }
3850 ast_channel_lock(chan);
3851 break;
3853 ast_channel_unlock(chan);
3854 ast_sendtext(chan, (const char *)read_action_payload->payload);
3855 ast_channel_lock(chan);
3856 break;
3858 ast_channel_unlock(chan);
3859 ast_sendtext_data(chan, (struct ast_msg_data *)read_action_payload->payload);
3860 ast_channel_lock(chan);
3861 break;
3862 }
3863 ast_frfree(f);
3864 f = &ast_null_frame;
3865 } else if (f->subclass.integer == AST_CONTROL_STREAM_TOPOLOGY_REQUEST_CHANGE && dropnondefault) {
3866 /* The caller of this function is incapable of handling streams so we don't accept the change request
3867 * and stick to the streams currently on the channel.
3868 */
3870 ast_frfree(f);
3871 f = &ast_null_frame;
3872 } else if (f->subclass.integer == AST_CONTROL_STREAM_TOPOLOGY_CHANGED && dropnondefault) {
3873 /* The caller of this function is incapable of handling streams so we absorb the notification that the
3874 * stream topology has changed.
3875 */
3876 ast_frfree(f);
3877 f = &ast_null_frame;
3878 } else if (f->subclass.integer == AST_CONTROL_FLASH) {
3879 send_flash_event(chan);
3880 } else if (f->subclass.integer == AST_CONTROL_WINK) {
3881 send_wink_event(chan);
3882 }
3883 break;
3884 case AST_FRAME_DTMF_END:
3886 ast_log(LOG_DTMF, "DTMF end '%c' received on %s, duration %ld ms\n", f->subclass.integer, ast_channel_name(chan), f->len);
3887 /* Queue it up if DTMF is deferred, or if DTMF emulation is forced. */
3889 queue_dtmf_readq(chan, f);
3890 ast_frfree(f);
3891 f = &ast_null_frame;
3893 if (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
3895 /* If it hasn't been long enough, defer this digit */
3896 queue_dtmf_readq(chan, f);
3897 ast_frfree(f);
3898 f = &ast_null_frame;
3899 } else {
3900 /* There was no begin, turn this into a begin and send the end later */
3901 struct timeval tv = ast_tvnow();
3905 ast_channel_dtmf_tv_set(chan, &tv);
3906 if (f->len) {
3907 if (f->len > option_dtmfminduration)
3909 else
3911 } else
3913 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));
3914
3915 /*
3916 * Start generating 50 fps timer events (null frames) for dtmf emulating
3917 * independently from any existing incoming voice frames.
3918 * If channel generator is already activated in regular mode use these
3919 * timer events to generate null frames.
3920 */
3921 if (!ast_channel_generator(chan)) {
3923 }
3924 }
3925 if (ast_channel_audiohooks(chan)) {
3926 struct ast_frame *old_frame = f;
3927 /*!
3928 * \todo XXX It is possible to write a digit to the audiohook twice
3929 * if the digit was originally read while the channel was in autoservice. */
3931 if (old_frame != f)
3932 ast_frfree(old_frame);
3933 }
3934 } else {
3935 struct timeval now = ast_tvnow();
3937 ast_log(LOG_DTMF, "DTMF end accepted with begin '%c' on %s\n", f->subclass.integer, ast_channel_name(chan));
3939 if (!f->len)
3940 f->len = ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan));
3941
3942 /* detect tones that were received on
3943 * the wire with durations shorter than
3944 * option_dtmfminduration and set f->len
3945 * to the actual duration of the DTMF
3946 * frames on the wire. This will cause
3947 * dtmf emulation to be triggered later
3948 * on.
3949 */
3951 f->len = ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan));
3952 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));
3953 }
3954 } else if (!f->len) {
3955 ast_log(LOG_DTMF, "DTMF end accepted without begin '%c' on %s\n", f->subclass.integer, ast_channel_name(chan));
3957 }
3959 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));
3963 ast_frfree(f);
3964 f = &ast_null_frame;
3965
3966 /* Start generating 50 fps timer events (null frames) for dtmf emulating
3967 * independently from any existing incoming voice frames.
3968 * If channel generator is already activated in regular mode use these
3969 * timer events to generate null frames.
3970 */
3971 if (!ast_channel_generator(chan)) {
3973 }
3974 } else {
3975 ast_log(LOG_DTMF, "DTMF end passthrough '%c' on %s\n", f->subclass.integer, ast_channel_name(chan));
3976 if (f->len < option_dtmfminduration) {
3978 }
3979 ast_channel_dtmf_tv_set(chan, &now);
3980
3981 /* Start generating 50 fps timer events (null frames) for dtmf emulating
3982 * independently from any existing incoming voice frames.
3983 * If channel generator is already activated in regular mode use these
3984 * timer events to generate null frames.
3985 */
3986 if (!ast_channel_generator(chan)) {
3988 }
3989 }
3990 if (ast_channel_audiohooks(chan)) {
3991 struct ast_frame *old_frame = f;
3993 if (old_frame != f)
3994 ast_frfree(old_frame);
3995 }
3996 }
3997 break;
4000 ast_log(LOG_DTMF, "DTMF begin '%c' received on %s\n", f->subclass.integer, ast_channel_name(chan));
4002 (!ast_tvzero(*ast_channel_dtmf_tv(chan)) &&
4004 ast_log(LOG_DTMF, "DTMF begin ignored '%c' on %s\n", f->subclass.integer, ast_channel_name(chan));
4005 ast_frfree(f);
4006 f = &ast_null_frame;
4007 } else {
4008 struct timeval now = ast_tvnow();
4010 ast_channel_dtmf_tv_set(chan, &now);
4011 ast_log(LOG_DTMF, "DTMF begin passthrough '%c' on %s\n", f->subclass.integer, ast_channel_name(chan));
4012 }
4013 break;
4014 case AST_FRAME_NULL:
4015 /* The EMULATE_DTMF flag must be cleared here as opposed to when the duration
4016 * is reached , because we want to make sure we pass at least one
4017 * voice frame through before starting the next digit, to ensure a gap
4018 * between DTMF digits. */
4020 struct timeval now = ast_tvnow();
4024 } else if (ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan)) >= ast_channel_emulate_dtmf_duration(chan)) {
4026 ast_frfree(f);
4027 f = ast_channel_dtmff(chan);
4030 f->len = ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan));
4031 ast_channel_dtmf_tv_set(chan, &now);
4034 ast_log(LOG_DTMF, "DTMF end emulation of '%c' queued on %s\n", f->subclass.integer, ast_channel_name(chan));
4035 if (ast_channel_audiohooks(chan)) {
4036 struct ast_frame *old_frame = f;
4038 if (old_frame != f) {
4039 ast_frfree(old_frame);
4040 }
4041 }
4042
4043 /* Start generating 50 fps timer events (null frames) for dtmf emulating
4044 * independently from any existing incoming voice frames.
4045 * If channel generator is already activated in regular mode use these
4046 * timer events to generate null frames.
4047 */
4048 if (!ast_channel_generator(chan)) {
4050 }
4051 }
4052 }
4053 break;
4054 case AST_FRAME_VOICE:
4055 /* If media was received from a non-default stream don't perform any actions, let it just go through */
4056 if (stream != default_stream) {
4057 break;
4058 }
4059
4060 /* The EMULATE_DTMF flag must be cleared here as opposed to when the duration
4061 * is reached , because we want to make sure we pass at least one
4062 * voice frame through before starting the next digit, to ensure a gap
4063 * between DTMF digits. */
4067 }
4068
4069 if (dropaudio || ast_test_flag(ast_channel_flags(chan), AST_FLAG_IN_DTMF)) {
4070 if (dropaudio)
4072 ast_frfree(f);
4073 f = &ast_null_frame;
4074 }
4075
4077 struct timeval now = ast_tvnow();
4080 ast_frfree(f);
4081 f = ast_channel_dtmff(chan);
4084 f->len = ast_tvdiff_ms(now, *ast_channel_dtmf_tv(chan));
4085 ast_channel_dtmf_tv_set(chan, &now);
4086 if (ast_channel_audiohooks(chan)) {
4087 struct ast_frame *old_frame = f;
4089 if (old_frame != f)
4090 ast_frfree(old_frame);
4091 }
4092 ast_log(LOG_DTMF, "DTMF end emulation of '%c' queued on %s\n", f->subclass.integer, ast_channel_name(chan));
4093 } else {
4094 /* Drop voice frames while we're still in the middle of the digit */
4095 ast_frfree(f);
4096 f = &ast_null_frame;
4097 }
4098 break;
4099 }
4100 if (f->frametype != AST_FRAME_VOICE) {
4101 break;
4102 }
4105 struct ast_format *core_format;
4106
4107 /*
4108 * Note: This frame may not be one of the current native
4109 * formats. We may have gotten it out of the read queue from
4110 * a previous multi-frame translation, from a framehook
4111 * injected frame, or the device we're talking to isn't
4112 * respecting negotiated formats. Regardless we will accept
4113 * all frames.
4114 *
4115 * Update the read translation path to handle the new format
4116 * that just came in. If the core wants slinear we need to
4117 * setup a new translation path because the core is usually
4118 * doing something with the audio itself and may not handle
4119 * any other format. e.g., Softmix bridge, holding bridge
4120 * announcer channel, recording, AMD... Otherwise, we'll
4121 * setup to pass the frame as is to the core. In this case
4122 * the core doesn't care. The channel is likely in
4123 * autoservice, safesleep, or the channel is in a bridge.
4124 * Let the bridge technology deal with format compatibility
4125 * between the channels in the bridge.
4126 *
4127 * Beware of the transcode_via_slin and genericplc options as
4128 * they force any transcoding to go through slin on a bridge.
4129 * Unfortunately transcode_via_slin is enabled by default and
4130 * genericplc is enabled in the codecs.conf.sample file.
4131 *
4132 * XXX Only updating translation to slinear frames has some
4133 * corner cases if slinear is one of the native formats and
4134 * there are different sample rates involved. We might wind
4135 * up with conflicting translation paths between channels
4136 * where the read translation path on this channel reduces
4137 * the sample rate followed by a write translation path on
4138 * the peer channel that increases the sample rate.
4139 */
4140 core_format = ast_channel_readformat(chan);
4141 if (!ast_format_cache_is_slinear(core_format)) {
4142 core_format = f->subclass.format;
4143 }
4144 if (ast_set_read_format_path(chan, f->subclass.format, core_format)) {
4145 /* Drop frame. We couldn't make it compatible with the core. */
4146 ast_frfree(f);
4147 f = &ast_null_frame;
4148 break;
4149 }
4150 }
4151 /*
4152 * Send frame to audiohooks if present, if frametype is linear, to preserve
4153 * functional compatibility with previous behavior. If not linear, hold off
4154 * until transcoding is done where we are more likely to have a linear frame
4155 */
4157 /* Place hooked after declaration */
4158 struct ast_frame *old_frame = f;
4159 hooked = 1;
4160
4162 if (old_frame != f) {
4163 ast_frfree(old_frame);
4164 }
4165 }
4166
4167 if (ast_channel_readtrans(chan)
4169 f = ast_translate(ast_channel_readtrans(chan), f, 1);
4170 if (!f) {
4171 f = &ast_null_frame;
4172 }
4173 }
4174
4175 /* Second chance at hooking a linear frame, also the last chance */
4176 if (ast_channel_audiohooks(chan) && !hooked) {
4177 struct ast_frame *old_frame = f;
4178
4180 if (old_frame != f) {
4181 ast_frfree(old_frame);
4182 }
4183 }
4184
4185 /*
4186 * It is possible for the translation process on the channel to have
4187 * produced multiple frames from the single input frame we passed it; if
4188 * this happens, queue the additional frames *before* the frames we may
4189 * have queued earlier. if the readq was empty, put them at the head of
4190 * the queue, and if it was not, put them just after the frame that was
4191 * at the end of the queue.
4192 */
4193 if (AST_LIST_NEXT(f, frame_list)) {
4194 struct ast_frame *cur, *multi_frame = AST_LIST_NEXT(f, frame_list);
4195
4196 /* Mark these frames as being re-queued */
4197 for (cur = multi_frame; cur; cur = AST_LIST_NEXT(cur, frame_list)) {
4199 }
4200
4201 if (!readq_tail) {
4202 ast_queue_frame_head(chan, multi_frame);
4203 } else {
4204 __ast_queue_frame(chan, multi_frame, 0, readq_tail);
4205 }
4206 ast_frfree(multi_frame);
4208 }
4209
4210 /*
4211 * Run generator sitting on the line if timing device not available
4212 * and synchronous generation of outgoing frames is necessary
4213 */
4215 break;
4216 case AST_FRAME_RTCP:
4217 /* Incoming RTCP feedback needs to get to the translator for
4218 * outgoing media, which means we treat it as an ast_write */
4219 if (ast_channel_writetrans(chan)) {
4221 }
4222 break;
4223 default:
4224 /* Just pass it on! */
4225 break;
4226 }
4227 } else {
4228 /* Make sure we always return NULL in the future */
4231 }
4232 if (cause)
4233 ast_channel_hangupcause_set(chan, cause);
4234 if (ast_channel_generator(chan))
4236 /* We no longer End the CDR here */
4237 }
4238
4239 /* High bit prints debugging */
4240 if (ast_channel_fin(chan) & DEBUGCHAN_FLAG)
4241 ast_frame_dump(ast_channel_name(chan), f, "<<");
4243
4244done:
4247
4249 /* The list gets recreated if audiohooks are added again later */
4252 }
4253 ast_channel_unlock(chan);
4254 return f;
4255}
4256
4257struct ast_frame *ast_read(struct ast_channel *chan)
4258{
4259 return __ast_read(chan, 0, 1);
4260}
4261
4263{
4264 return __ast_read(chan, 0, 0);
4265}
4266
4268{
4269 return __ast_read(chan, 1, 1);
4270}
4271
4273{
4274 return __ast_read(chan, 1, 0);
4275}
4276
4277int ast_indicate(struct ast_channel *chan, int condition)
4278{
4279 return ast_indicate_data(chan, condition, NULL, 0);
4280}
4281
4283{
4284 /* Don't include a default case here so that we get compiler warnings
4285 * when a new type is added. */
4286
4287 switch (condition) {
4295 case AST_CONTROL_OPTION:
4296 case AST_CONTROL_WINK:
4297 case AST_CONTROL_FLASH:
4300 case AST_CONTROL_ANSWER:
4301 case AST_CONTROL_HANGUP:
4307 case AST_CONTROL_CC:
4309 case AST_CONTROL_AOC:
4311 case AST_CONTROL_MCID:
4324 case