2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Channel Management
23 * \author Mark Spencer <markster@digium.com>
28 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
40 #include <sys/ioctl.h>
42 #include <linux/zaptel.h>
45 #endif /* __linux__ */
47 #error "You need newer zaptel! Please cvs update zaptel"
51 #include "asterisk/pbx.h"
52 #include "asterisk/frame.h"
53 #include "asterisk/sched.h"
54 #include "asterisk/options.h"
55 #include "asterisk/channel.h"
56 #include "asterisk/chanspy.h"
57 #include "asterisk/musiconhold.h"
58 #include "asterisk/logger.h"
59 #include "asterisk/say.h"
60 #include "asterisk/file.h"
61 #include "asterisk/cli.h"
62 #include "asterisk/translate.h"
63 #include "asterisk/manager.h"
64 #include "asterisk/chanvars.h"
65 #include "asterisk/linkedlists.h"
66 #include "asterisk/indications.h"
67 #include "asterisk/monitor.h"
68 #include "asterisk/causes.h"
69 #include "asterisk/callerid.h"
70 #include "asterisk/utils.h"
71 #include "asterisk/lock.h"
72 #include "asterisk/app.h"
73 #include "asterisk/transcap.h"
74 #include "asterisk/devicestate.h"
75 #include "asterisk/sha1.h"
77 struct channel_spy_trans
{
79 struct ast_trans_pvt
*path
;
82 struct ast_channel_spy_list
{
83 struct channel_spy_trans read_translator
;
84 struct channel_spy_trans write_translator
;
85 AST_LIST_HEAD_NOLOCK(, ast_channel_spy
) list
;
88 /* uncomment if you have problems with 'monitoring' synchronized files */
90 #define MONITOR_CONSTANT_DELAY
91 #define MONITOR_DELAY 150 * 8 /* 150 ms of MONITORING DELAY */
94 /*! Prevent new channel allocation if shutting down. */
95 static int shutting_down
= 0;
97 AST_MUTEX_DEFINE_STATIC(uniquelock
);
98 static int uniqueint
= 0;
100 unsigned long global_fin
= 0, global_fout
= 0;
102 /* XXX Lock appropriately in more functions XXX */
105 const struct ast_channel_tech
*tech
;
106 AST_LIST_ENTRY(chanlist
) list
;
109 /*! the list of registered channel types */
110 static AST_LIST_HEAD_NOLOCK_STATIC(backends
, chanlist
);
112 /*! the list of channels we have. Note that the lock for this list is used for
113 both the channels list and the backends list. */
114 static AST_LIST_HEAD_STATIC(channels
, ast_channel
);
116 /*! map AST_CAUSE's to readable string representations */
117 const struct ast_cause
{
122 { AST_CAUSE_UNALLOCATED
, "UNALLOCATED", "Unallocated (unassigned) number" },
123 { AST_CAUSE_NO_ROUTE_TRANSIT_NET
, "NO_ROUTE_TRANSIT_NET", "No route to specified transmit network" },
124 { AST_CAUSE_NO_ROUTE_DESTINATION
, "NO_ROUTE_DESTINATION", "No route to destination" },
125 { AST_CAUSE_CHANNEL_UNACCEPTABLE
, "CHANNEL_UNACCEPTABLE", "Channel unacceptable" },
126 { AST_CAUSE_CALL_AWARDED_DELIVERED
, "CALL_AWARDED_DELIVERED", "Call awarded and being delivered in an established channel" },
127 { AST_CAUSE_NORMAL_CLEARING
, "NORMAL_CLEARING", "Normal Clearing" },
128 { AST_CAUSE_USER_BUSY
, "USER_BUSY", "User busy" },
129 { AST_CAUSE_NO_USER_RESPONSE
, "NO_USER_RESPONSE", "No user responding" },
130 { AST_CAUSE_NO_ANSWER
, "NO_ANSWER", "User alerting, no answer" },
131 { AST_CAUSE_CALL_REJECTED
, "CALL_REJECTED", "Call Rejected" },
132 { AST_CAUSE_NUMBER_CHANGED
, "NUMBER_CHANGED", "Number changed" },
133 { AST_CAUSE_DESTINATION_OUT_OF_ORDER
, "DESTINATION_OUT_OF_ORDER", "Destination out of order" },
134 { AST_CAUSE_INVALID_NUMBER_FORMAT
, "INVALID_NUMBER_FORMAT", "Invalid number format" },
135 { AST_CAUSE_FACILITY_REJECTED
, "FACILITY_REJECTED", "Facility rejected" },
136 { AST_CAUSE_RESPONSE_TO_STATUS_ENQUIRY
, "RESPONSE_TO_STATUS_ENQUIRY", "Response to STATus ENQuiry" },
137 { AST_CAUSE_NORMAL_UNSPECIFIED
, "NORMAL_UNSPECIFIED", "Normal, unspecified" },
138 { AST_CAUSE_NORMAL_CIRCUIT_CONGESTION
, "NORMAL_CIRCUIT_CONGESTION", "Circuit/channel congestion" },
139 { AST_CAUSE_NETWORK_OUT_OF_ORDER
, "NETWORK_OUT_OF_ORDER", "Network out of order" },
140 { AST_CAUSE_NORMAL_TEMPORARY_FAILURE
, "NORMAL_TEMPORARY_FAILURE", "Temporary failure" },
141 { AST_CAUSE_SWITCH_CONGESTION
, "SWITCH_CONGESTION", "Switching equipment congestion" },
142 { AST_CAUSE_ACCESS_INFO_DISCARDED
, "ACCESS_INFO_DISCARDED", "Access information discarded" },
143 { AST_CAUSE_REQUESTED_CHAN_UNAVAIL
, "REQUESTED_CHAN_UNAVAIL", "Requested channel not available" },
144 { AST_CAUSE_PRE_EMPTED
, "PRE_EMPTED", "Pre-empted" },
145 { AST_CAUSE_FACILITY_NOT_SUBSCRIBED
, "FACILITY_NOT_SUBSCRIBED", "Facility not subscribed" },
146 { AST_CAUSE_OUTGOING_CALL_BARRED
, "OUTGOING_CALL_BARRED", "Outgoing call barred" },
147 { AST_CAUSE_INCOMING_CALL_BARRED
, "INCOMING_CALL_BARRED", "Incoming call barred" },
148 { AST_CAUSE_BEARERCAPABILITY_NOTAUTH
, "BEARERCAPABILITY_NOTAUTH", "Bearer capability not authorized" },
149 { AST_CAUSE_BEARERCAPABILITY_NOTAVAIL
, "BEARERCAPABILITY_NOTAVAIL", "Bearer capability not available" },
150 { AST_CAUSE_BEARERCAPABILITY_NOTIMPL
, "BEARERCAPABILITY_NOTIMPL", "Bearer capability not implemented" },
151 { AST_CAUSE_CHAN_NOT_IMPLEMENTED
, "CHAN_NOT_IMPLEMENTED", "Channel not implemented" },
152 { AST_CAUSE_FACILITY_NOT_IMPLEMENTED
, "FACILITY_NOT_IMPLEMENTED", "Facility not implemented" },
153 { AST_CAUSE_INVALID_CALL_REFERENCE
, "INVALID_CALL_REFERENCE", "Invalid call reference value" },
154 { AST_CAUSE_INCOMPATIBLE_DESTINATION
, "INCOMPATIBLE_DESTINATION", "Incompatible destination" },
155 { AST_CAUSE_INVALID_MSG_UNSPECIFIED
, "INVALID_MSG_UNSPECIFIED", "Invalid message unspecified" },
156 { AST_CAUSE_MANDATORY_IE_MISSING
, "MANDATORY_IE_MISSING", "Mandatory information element is missing" },
157 { AST_CAUSE_MESSAGE_TYPE_NONEXIST
, "MESSAGE_TYPE_NONEXIST", "Message type nonexist." },
158 { AST_CAUSE_WRONG_MESSAGE
, "WRONG_MESSAGE", "Wrong message" },
159 { AST_CAUSE_IE_NONEXIST
, "IE_NONEXIST", "Info. element nonexist or not implemented" },
160 { AST_CAUSE_INVALID_IE_CONTENTS
, "INVALID_IE_CONTENTS", "Invalid information element contents" },
161 { AST_CAUSE_WRONG_CALL_STATE
, "WRONG_CALL_STATE", "Message not compatible with call state" },
162 { AST_CAUSE_RECOVERY_ON_TIMER_EXPIRE
, "RECOVERY_ON_TIMER_EXPIRE", "Recover on timer expiry" },
163 { AST_CAUSE_MANDATORY_IE_LENGTH_ERROR
, "MANDATORY_IE_LENGTH_ERROR", "Mandatory IE length error" },
164 { AST_CAUSE_PROTOCOL_ERROR
, "PROTOCOL_ERROR", "Protocol error, unspecified" },
165 { AST_CAUSE_INTERWORKING
, "INTERWORKING", "Interworking, unspecified" },
169 struct ast_variable
*ast_channeltype_list(void)
172 struct ast_variable
*var
=NULL
, *prev
= NULL
;
173 AST_LIST_TRAVERSE(&backends
, cl
, list
) {
175 if ((prev
->next
= ast_variable_new(cl
->tech
->type
, cl
->tech
->description
)))
178 var
= ast_variable_new(cl
->tech
->type
, cl
->tech
->description
);
185 static int show_channeltypes(int fd
, int argc
, char *argv
[])
187 #define FORMAT "%-10.10s %-40.40s %-12.12s %-12.12s %-12.12s\n"
191 ast_cli(fd
, FORMAT
, "Type", "Description", "Devicestate", "Indications", "Transfer");
192 ast_cli(fd
, FORMAT
, "----------", "-----------", "-----------", "-----------", "--------");
193 if (AST_LIST_LOCK(&channels
)) {
194 ast_log(LOG_WARNING
, "Unable to lock channel list\n");
197 AST_LIST_TRAVERSE(&backends
, cl
, list
) {
198 ast_cli(fd
, FORMAT
, cl
->tech
->type
, cl
->tech
->description
,
199 (cl
->tech
->devicestate
) ? "yes" : "no",
200 (cl
->tech
->indicate
) ? "yes" : "no",
201 (cl
->tech
->transfer
) ? "yes" : "no");
204 AST_LIST_UNLOCK(&channels
);
205 ast_cli(fd
, "----------\n%d channel drivers registered.\n", count_chan
);
206 return RESULT_SUCCESS
;
212 static int show_channeltype(int fd
, int argc
, char *argv
[])
214 struct chanlist
*cl
= NULL
;
217 return RESULT_SHOWUSAGE
;
219 if (AST_LIST_LOCK(&channels
)) {
220 ast_log(LOG_WARNING
, "Unable to lock channel list\n");
221 return RESULT_FAILURE
;
224 AST_LIST_TRAVERSE(&backends
, cl
, list
) {
225 if (!strncasecmp(cl
->tech
->type
, argv
[2], strlen(cl
->tech
->type
))) {
232 ast_cli(fd
, "\n%s is not a registered channel driver.\n", argv
[2]);
233 AST_LIST_UNLOCK(&channels
);
234 return RESULT_FAILURE
;
238 "-- Info about channel driver: %s --\n"
239 " Device State: %s\n"
242 " Capabilities: %d\n"
245 " Image Support: %s\n"
246 " Text Support: %s\n",
248 (cl
->tech
->devicestate
) ? "yes" : "no",
249 (cl
->tech
->indicate
) ? "yes" : "no",
250 (cl
->tech
->transfer
) ? "yes" : "no",
251 (cl
->tech
->capabilities
) ? cl
->tech
->capabilities
: -1,
252 (cl
->tech
->send_digit
) ? "yes" : "no",
253 (cl
->tech
->send_html
) ? "yes" : "no",
254 (cl
->tech
->send_image
) ? "yes" : "no",
255 (cl
->tech
->send_text
) ? "yes" : "no"
259 AST_LIST_UNLOCK(&channels
);
260 return RESULT_SUCCESS
;
263 static char *complete_channeltypes(const char *line
, const char *word
, int pos
, int state
)
273 wordlen
= strlen(word
);
275 AST_LIST_TRAVERSE(&backends
, cl
, list
) {
276 if (!strncasecmp(word
, cl
->tech
->type
, wordlen
) && ++which
> state
) {
277 ret
= strdup(cl
->tech
->type
);
285 static char show_channeltypes_usage
[] =
286 "Usage: show channeltypes\n"
287 " Shows available channel types registered in your Asterisk server.\n";
289 static char show_channeltype_usage
[] =
290 "Usage: show channeltype <name>\n"
291 " Show details about the specified channel type, <name>.\n";
293 static struct ast_cli_entry cli_show_channeltypes
=
294 { { "show", "channeltypes", NULL
}, show_channeltypes
, "Show available channel types", show_channeltypes_usage
};
296 static struct ast_cli_entry cli_show_channeltype
=
297 { { "show", "channeltype", NULL
}, show_channeltype
, "Give more details on that channel type", show_channeltype_usage
, complete_channeltypes
};
299 /*! \brief Checks to see if a channel is needing hang up */
300 int ast_check_hangup(struct ast_channel
*chan
)
302 if (chan
->_softhangup
) /* yes if soft hangup flag set */
304 if (!chan
->tech_pvt
) /* yes if no technology private data */
306 if (!chan
->whentohangup
) /* no if no hangup scheduled */
308 if (chan
->whentohangup
> time(NULL
)) /* no if hangup time has not come yet. */
310 chan
->_softhangup
|= AST_SOFTHANGUP_TIMEOUT
; /* record event */
314 static int ast_check_hangup_locked(struct ast_channel
*chan
)
317 ast_channel_lock(chan
);
318 res
= ast_check_hangup(chan
);
319 ast_channel_unlock(chan
);
323 /*! \brief Initiate system shutdown */
324 void ast_begin_shutdown(int hangup
)
326 struct ast_channel
*c
;
329 AST_LIST_LOCK(&channels
);
330 AST_LIST_TRAVERSE(&channels
, c
, chan_list
)
331 ast_softhangup(c
, AST_SOFTHANGUP_SHUTDOWN
);
332 AST_LIST_UNLOCK(&channels
);
336 /*! \brief returns number of active/allocated channels */
337 int ast_active_channels(void)
339 struct ast_channel
*c
;
341 AST_LIST_LOCK(&channels
);
342 AST_LIST_TRAVERSE(&channels
, c
, chan_list
)
344 AST_LIST_UNLOCK(&channels
);
348 /*! \brief Cancel a shutdown in progress */
349 void ast_cancel_shutdown(void)
354 /*! \brief Returns non-zero if Asterisk is being shut down */
355 int ast_shutting_down(void)
357 return shutting_down
;
360 /*! \brief Set when to hangup channel */
361 void ast_channel_setwhentohangup(struct ast_channel
*chan
, time_t offset
)
363 chan
->whentohangup
= offset
? time(NULL
) + offset
: 0;
364 ast_queue_frame(chan
, &ast_null_frame
);
368 /*! \brief Compare a offset with when to hangup channel */
369 int ast_channel_cmpwhentohangup(struct ast_channel
*chan
, time_t offset
)
373 if (chan
->whentohangup
== 0) {
374 return (offset
== 0) ? 0 : -1;
376 if (offset
== 0) /* XXX why is this special ? */
379 whentohangup
= offset
+ time (NULL
);
380 if (chan
->whentohangup
< whentohangup
)
382 else if (chan
->whentohangup
== whentohangup
)
390 /*! \brief Register a new telephony channel in Asterisk */
391 int ast_channel_register(const struct ast_channel_tech
*tech
)
393 struct chanlist
*chan
;
395 AST_LIST_LOCK(&channels
);
397 AST_LIST_TRAVERSE(&backends
, chan
, list
) {
398 if (!strcasecmp(tech
->type
, chan
->tech
->type
)) {
399 ast_log(LOG_WARNING
, "Already have a handler for type '%s'\n", tech
->type
);
400 AST_LIST_UNLOCK(&channels
);
405 if (!(chan
= ast_calloc(1, sizeof(*chan
)))) {
406 AST_LIST_UNLOCK(&channels
);
410 AST_LIST_INSERT_HEAD(&backends
, chan
, list
);
413 ast_log(LOG_DEBUG
, "Registered handler for '%s' (%s)\n", chan
->tech
->type
, chan
->tech
->description
);
415 if (option_verbose
> 1)
416 ast_verbose(VERBOSE_PREFIX_2
"Registered channel type '%s' (%s)\n", chan
->tech
->type
,
417 chan
->tech
->description
);
419 AST_LIST_UNLOCK(&channels
);
423 void ast_channel_unregister(const struct ast_channel_tech
*tech
)
425 struct chanlist
*chan
;
428 ast_log(LOG_DEBUG
, "Unregistering channel type '%s'\n", tech
->type
);
430 AST_LIST_LOCK(&channels
);
432 AST_LIST_TRAVERSE_SAFE_BEGIN(&backends
, chan
, list
) {
433 if (chan
->tech
== tech
) {
434 AST_LIST_REMOVE_CURRENT(&backends
, list
);
436 if (option_verbose
> 1)
437 ast_verbose(VERBOSE_PREFIX_2
"Unregistered channel type '%s'\n", tech
->type
);
441 AST_LIST_TRAVERSE_SAFE_END
443 AST_LIST_UNLOCK(&channels
);
446 const struct ast_channel_tech
*ast_get_channel_tech(const char *name
)
448 struct chanlist
*chanls
;
449 const struct ast_channel_tech
*ret
= NULL
;
451 if (AST_LIST_LOCK(&channels
)) {
452 ast_log(LOG_WARNING
, "Unable to lock channel tech list\n");
456 AST_LIST_TRAVERSE(&backends
, chanls
, list
) {
457 if (!strcasecmp(name
, chanls
->tech
->type
)) {
463 AST_LIST_UNLOCK(&channels
);
468 /*! \brief Gives the string form of a given hangup cause */
469 const char *ast_cause2str(int cause
)
473 for (x
=0; x
< sizeof(causes
) / sizeof(causes
[0]); x
++) {
474 if (causes
[x
].cause
== cause
)
475 return causes
[x
].desc
;
481 /*! \brief Convert a symbolic hangup cause to number */
482 int ast_str2cause(const char *name
)
486 for (x
= 0; x
< sizeof(causes
) / sizeof(causes
[0]); x
++)
487 if (strncasecmp(causes
[x
].name
, name
, strlen(causes
[x
].name
)) == 0)
488 return causes
[x
].cause
;
493 /*! \brief Gives the string form of a given channel state */
494 char *ast_state2str(int state
)
496 /* XXX Not reentrant XXX */
497 static char localtmp
[256];
501 case AST_STATE_RESERVED
:
503 case AST_STATE_OFFHOOK
:
505 case AST_STATE_DIALING
:
509 case AST_STATE_RINGING
:
516 snprintf(localtmp
, sizeof(localtmp
), "Unknown (%d)\n", state
);
521 /*! \brief Gives the string form of a given transfer capability */
522 char *ast_transfercapability2str(int transfercapability
)
524 switch(transfercapability
) {
525 case AST_TRANS_CAP_SPEECH
:
527 case AST_TRANS_CAP_DIGITAL
:
529 case AST_TRANS_CAP_RESTRICTED_DIGITAL
:
530 return "RESTRICTED_DIGITAL";
531 case AST_TRANS_CAP_3_1K_AUDIO
:
533 case AST_TRANS_CAP_DIGITAL_W_TONES
:
534 return "DIGITAL_W_TONES";
535 case AST_TRANS_CAP_VIDEO
:
542 /*! \brief Pick the best audio codec */
543 int ast_best_codec(int fmts
)
545 /* This just our opinion, expressed in code. We are asked to choose
546 the best codec to use, given no information */
550 /*! Okay, ulaw is used by all telephony equipment, so start with it */
552 /*! Unless of course, you're a silly European, so then prefer ALAW */
554 /*! Okay, well, signed linear is easy to translate into other stuff */
556 /*! G.726 is standard ADPCM */
558 /*! ADPCM has great sound quality and is still pretty easy to translate */
560 /*! Okay, we're down to vocoders now, so pick GSM because it's small and easier to
561 translate and sounds pretty good */
563 /*! iLBC is not too bad */
565 /*! Speex is free, but computationally more expensive than GSM */
567 /*! Ick, LPC10 sounds terrible, but at least we have code for it, if you're tacky enough
570 /*! G.729a is faster than 723 and slightly less expensive */
572 /*! Down to G.723.1 which is proprietary but at least designed for voice */
576 /* Strip out video */
577 fmts
&= AST_FORMAT_AUDIO_MASK
;
579 /* Find the first preferred codec in the format given */
580 for (x
=0; x
< (sizeof(prefs
) / sizeof(prefs
[0]) ); x
++)
583 ast_log(LOG_WARNING
, "Don't know any of 0x%x formats\n", fmts
);
587 static const struct ast_channel_tech null_tech
= {
589 .description
= "Null channel (should not see this)",
592 /*! \brief Create a new channel structure */
593 struct ast_channel
*ast_channel_alloc(int needqueue
)
595 struct ast_channel
*tmp
;
598 struct varshead
*headp
;
600 /* If shutting down, don't allocate any new channels */
602 ast_log(LOG_WARNING
, "Channel allocation failed: Refusing due to active shutdown\n");
606 if (!(tmp
= ast_calloc(1, sizeof(*tmp
))))
609 if (!(tmp
->sched
= sched_context_create())) {
610 ast_log(LOG_WARNING
, "Channel allocation failed: Unable to create schedule context\n");
615 ast_string_field_init(tmp
, 128);
617 /* Don't bother initializing the last two FD here, because they
618 will *always* be set just a few lines down (AST_TIMING_FD,
620 for (x
=0; x
<AST_MAX_FDS
- 2; x
++)
624 tmp
->timingfd
= open("/dev/zap/timer", O_RDWR
);
625 if (tmp
->timingfd
> -1) {
626 /* Check if timing interface supports new
629 if (!ioctl(tmp
->timingfd
, ZT_TIMERPONG
, &flags
))
637 if (pipe(tmp
->alertpipe
)) {
638 ast_log(LOG_WARNING
, "Channel allocation failed: Can't create alert pipe!\n");
642 flags
= fcntl(tmp
->alertpipe
[0], F_GETFL
);
643 fcntl(tmp
->alertpipe
[0], F_SETFL
, flags
| O_NONBLOCK
);
644 flags
= fcntl(tmp
->alertpipe
[1], F_GETFL
);
645 fcntl(tmp
->alertpipe
[1], F_SETFL
, flags
| O_NONBLOCK
);
647 } else /* Make sure we've got it done right if they don't */
648 tmp
->alertpipe
[0] = tmp
->alertpipe
[1] = -1;
650 /* Always watch the alertpipe */
651 tmp
->fds
[AST_ALERT_FD
] = tmp
->alertpipe
[0];
652 /* And timing pipe */
653 tmp
->fds
[AST_TIMING_FD
] = tmp
->timingfd
;
654 ast_string_field_set(tmp
, name
, "**Unknown**");
656 tmp
->_state
= AST_STATE_DOWN
;
660 tmp
->fin
= global_fin
;
661 tmp
->fout
= global_fout
;
662 ast_mutex_lock(&uniquelock
);
663 if (ast_strlen_zero(ast_config_AST_SYSTEM_NAME
))
664 ast_string_field_build(tmp
, uniqueid
, "%li.%d", (long) time(NULL
), uniqueint
++);
666 ast_string_field_build(tmp
, uniqueid
, "%s-%li.%d", ast_config_AST_SYSTEM_NAME
, (long) time(NULL
), uniqueint
++);
667 ast_mutex_unlock(&uniquelock
);
668 headp
= &tmp
->varshead
;
669 ast_mutex_init(&tmp
->lock
);
670 AST_LIST_HEAD_INIT_NOLOCK(headp
);
671 AST_LIST_HEAD_INIT_NOLOCK(&tmp
->datastores
);
672 strcpy(tmp
->context
, "default");
673 ast_string_field_set(tmp
, language
, defaultlanguage
);
674 strcpy(tmp
->exten
, "s");
676 tmp
->amaflags
= ast_default_amaflags
;
677 ast_string_field_set(tmp
, accountcode
, ast_default_accountcode
);
679 tmp
->tech
= &null_tech
;
681 AST_LIST_LOCK(&channels
);
682 AST_LIST_INSERT_HEAD(&channels
, tmp
, chan_list
);
683 AST_LIST_UNLOCK(&channels
);
687 /*! \brief Queue an outgoing media frame */
688 int ast_queue_frame(struct ast_channel
*chan
, struct ast_frame
*fin
)
691 struct ast_frame
*prev
, *cur
;
695 /* Build us a copy and free the original one */
696 if (!(f
= ast_frdup(fin
))) {
697 ast_log(LOG_WARNING
, "Unable to duplicate frame\n");
700 ast_channel_lock(chan
);
702 for (cur
= chan
->readq
; cur
; cur
= cur
->next
) {
703 if ((cur
->frametype
== AST_FRAME_CONTROL
) && (cur
->subclass
== AST_CONTROL_HANGUP
)) {
704 /* Don't bother actually queueing anything after a hangup */
706 ast_channel_unlock(chan
);
712 /* Allow up to 96 voice frames outstanding, and up to 128 total frames */
713 if (((fin
->frametype
== AST_FRAME_VOICE
) && (qlen
> 96)) || (qlen
> 128)) {
714 if (fin
->frametype
!= AST_FRAME_VOICE
) {
715 ast_log(LOG_WARNING
, "Exceptionally long queue length queuing to %s\n", chan
->name
);
718 ast_log(LOG_DEBUG
, "Dropping voice to exceptionally long queue on %s\n", chan
->name
);
720 ast_channel_unlock(chan
);
728 if (chan
->alertpipe
[1] > -1) {
729 if (write(chan
->alertpipe
[1], &blah
, sizeof(blah
)) != sizeof(blah
))
730 ast_log(LOG_WARNING
, "Unable to write to alert pipe on %s, frametype/subclass %d/%d (qlen = %d): %s!\n",
731 chan
->name
, f
->frametype
, f
->subclass
, qlen
, strerror(errno
));
733 } else if (chan
->timingfd
> -1) {
734 ioctl(chan
->timingfd
, ZT_TIMERPING
, &blah
);
736 } else if (ast_test_flag(chan
, AST_FLAG_BLOCKING
)) {
737 pthread_kill(chan
->blocker
, SIGURG
);
739 ast_channel_unlock(chan
);
743 /*! \brief Queue a hangup frame for channel */
744 int ast_queue_hangup(struct ast_channel
*chan
)
746 struct ast_frame f
= { AST_FRAME_CONTROL
, AST_CONTROL_HANGUP
};
747 /* Yeah, let's not change a lock-critical value without locking */
748 if (!ast_channel_trylock(chan
)) {
749 chan
->_softhangup
|= AST_SOFTHANGUP_DEV
;
750 ast_channel_unlock(chan
);
752 return ast_queue_frame(chan
, &f
);
755 /*! \brief Queue a control frame */
756 int ast_queue_control(struct ast_channel
*chan
, enum ast_control_frame_type control
)
758 struct ast_frame f
= { AST_FRAME_CONTROL
, };
760 f
.subclass
= control
;
762 return ast_queue_frame(chan
, &f
);
765 /*! \brief Queue a control frame with payload */
766 int ast_queue_control_data(struct ast_channel
*chan
, enum ast_control_frame_type control
,
767 const void *data
, size_t datalen
)
769 struct ast_frame f
= { AST_FRAME_CONTROL
, };
771 f
.subclass
= control
;
772 f
.data
= (void *) data
;
775 return ast_queue_frame(chan
, &f
);
778 /*! \brief Set defer DTMF flag on channel */
779 int ast_channel_defer_dtmf(struct ast_channel
*chan
)
784 pre
= ast_test_flag(chan
, AST_FLAG_DEFER_DTMF
);
785 ast_set_flag(chan
, AST_FLAG_DEFER_DTMF
);
790 /*! \brief Unset defer DTMF flag on channel */
791 void ast_channel_undefer_dtmf(struct ast_channel
*chan
)
794 ast_clear_flag(chan
, AST_FLAG_DEFER_DTMF
);
798 * \brief Helper function to find channels.
800 * It supports these modes:
802 * prev != NULL : get channel next in list after prev
803 * name != NULL : get channel with matching name
804 * name != NULL && namelen != 0 : get channel whose name starts with prefix
805 * exten != NULL : get channel whose exten or macroexten matches
806 * context != NULL && exten != NULL : get channel whose context or macrocontext
808 * It returns with the channel's lock held. If getting the individual lock fails,
809 * unlock and retry quickly up to 10 times, then give up.
811 * \note XXX Note that this code has cost O(N) because of the need to verify
812 * that the object is still on the global list.
814 * \note XXX also note that accessing fields (e.g. c->name in ast_log())
815 * can only be done with the lock held or someone could delete the
816 * object while we work on it. This causes some ugliness in the code.
817 * Note that removing the first ast_log() may be harmful, as it would
818 * shorten the retry period and possibly cause failures.
819 * We should definitely go for a better scheme that is deadlock-free.
821 static struct ast_channel
*channel_find_locked(const struct ast_channel
*prev
,
822 const char *name
, const int namelen
,
823 const char *context
, const char *exten
)
825 const char *msg
= prev
? "deadlock" : "initial deadlock";
827 struct ast_channel
*c
;
829 for (retries
= 0; retries
< 10; retries
++) {
831 AST_LIST_LOCK(&channels
);
832 AST_LIST_TRAVERSE(&channels
, c
, chan_list
) {
833 if (prev
) { /* look for next item */
834 if (c
!= prev
) /* not this one */
836 /* found, prepare to return c->next */
837 c
= AST_LIST_NEXT(c
, chan_list
);
838 } else if (name
) { /* want match by name */
839 if ( (!namelen
&& strcasecmp(c
->name
, name
)) ||
840 (namelen
&& strncasecmp(c
->name
, name
, namelen
)) )
841 continue; /* name match failed */
843 if (context
&& strcasecmp(c
->context
, context
) &&
844 strcasecmp(c
->macrocontext
, context
))
845 continue; /* context match failed */
846 if (strcasecmp(c
->exten
, exten
) &&
847 strcasecmp(c
->macroexten
, exten
))
848 continue; /* exten match failed */
850 /* if we get here, c points to the desired record */
853 /* exit if chan not found or mutex acquired successfully */
854 /* this is slightly unsafe, as we _should_ hold the lock to access c->name */
855 done
= c
== NULL
|| ast_channel_trylock(c
) == 0;
857 ast_log(LOG_DEBUG
, "Avoiding %s for channel '%p'\n", msg
, c
);
858 AST_LIST_UNLOCK(&channels
);
861 usleep(1); /* give other threads a chance before retrying */
864 * c is surely not null, but we don't have the lock so cannot
867 ast_log(LOG_DEBUG
, "Failure, could not lock '%p' after %d retries!\n",
873 /*! \brief Browse channels in use */
874 struct ast_channel
*ast_channel_walk_locked(const struct ast_channel
*prev
)
876 return channel_find_locked(prev
, NULL
, 0, NULL
, NULL
);
879 /*! \brief Get channel by name and lock it */
880 struct ast_channel
*ast_get_channel_by_name_locked(const char *name
)
882 return channel_find_locked(NULL
, name
, 0, NULL
, NULL
);
885 /*! \brief Get channel by name prefix and lock it */
886 struct ast_channel
*ast_get_channel_by_name_prefix_locked(const char *name
, const int namelen
)
888 return channel_find_locked(NULL
, name
, namelen
, NULL
, NULL
);
891 /*! \brief Get next channel by name prefix and lock it */
892 struct ast_channel
*ast_walk_channel_by_name_prefix_locked(struct ast_channel
*chan
, const char *name
, const int namelen
)
894 return channel_find_locked(chan
, name
, namelen
, NULL
, NULL
);
897 /*! \brief Get channel by exten (and optionally context) and lock it */
898 struct ast_channel
*ast_get_channel_by_exten_locked(const char *exten
, const char *context
)
900 return channel_find_locked(NULL
, NULL
, 0, context
, exten
);
903 /*! \brief Wait, look for hangups and condition arg */
904 int ast_safe_sleep_conditional(struct ast_channel
*chan
, int ms
, int (*cond
)(void*), void *data
)
909 if (cond
&& ((*cond
)(data
) == 0))
911 ms
= ast_waitfor(chan
, ms
);
924 /*! \brief Wait, look for hangups */
925 int ast_safe_sleep(struct ast_channel
*chan
, int ms
)
927 return ast_safe_sleep_conditional(chan
, ms
, NULL
, NULL
);
930 static void free_cid(struct ast_callerid
*cid
)
941 free(cid
->cid_rdnis
);
944 /*! \brief Free a channel structure */
945 void ast_channel_free(struct ast_channel
*chan
)
948 struct ast_var_t
*vardata
;
949 struct ast_frame
*f
, *fp
;
950 struct varshead
*headp
;
951 struct ast_datastore
*datastore
= NULL
;
952 char name
[AST_CHANNEL_NAME
];
954 headp
=&chan
->varshead
;
956 AST_LIST_LOCK(&channels
);
957 AST_LIST_REMOVE(&channels
, chan
, chan_list
);
958 /* Lock and unlock the channel just to be sure nobody
959 has it locked still */
960 ast_channel_lock(chan
);
961 ast_channel_unlock(chan
);
962 if (chan
->tech_pvt
) {
963 ast_log(LOG_WARNING
, "Channel '%s' may not have been hung up properly\n", chan
->name
);
964 free(chan
->tech_pvt
);
968 sched_context_destroy(chan
->sched
);
970 ast_copy_string(name
, chan
->name
, sizeof(name
));
972 /* Stop monitoring */
974 chan
->monitor
->stop( chan
, 0 );
977 /* If there is native format music-on-hold state, free it */
978 if(chan
->music_state
)
979 ast_moh_cleanup(chan
);
981 /* Free translators */
983 ast_translator_free_path(chan
->readtrans
);
984 if (chan
->writetrans
)
985 ast_translator_free_path(chan
->writetrans
);
987 ast_log(LOG_WARNING
, "PBX may not have been terminated properly on '%s'\n", chan
->name
);
988 free_cid(&chan
->cid
);
989 ast_mutex_destroy(&chan
->lock
);
990 /* Close pipes if appropriate */
991 if ((fd
= chan
->alertpipe
[0]) > -1)
993 if ((fd
= chan
->alertpipe
[1]) > -1)
995 if ((fd
= chan
->timingfd
) > -1)
1005 /* Get rid of each of the data stores on the channel */
1006 while ((datastore
= AST_LIST_REMOVE_HEAD(&chan
->datastores
, entry
)))
1007 /* Free the data store */
1008 ast_channel_datastore_free(datastore
);
1009 AST_LIST_HEAD_INIT_NOLOCK(&chan
->datastores
);
1011 /* loop over the variables list, freeing all data and deleting list items */
1012 /* no need to lock the list, as the channel is already locked */
1014 while ((vardata
= AST_LIST_REMOVE_HEAD(headp
, entries
)))
1015 ast_var_delete(vardata
);
1017 /* Destroy the jitterbuffer */
1018 ast_jb_destroy(chan
);
1020 ast_string_field_free_all(chan
);
1022 AST_LIST_UNLOCK(&channels
);
1024 ast_device_state_changed_literal(name
);
1027 struct ast_datastore
*ast_channel_datastore_alloc(const struct ast_datastore_info
*info
, char *uid
)
1029 struct ast_datastore
*datastore
= NULL
;
1031 /* Make sure we at least have type so we can identify this */
1036 /* Allocate memory for datastore and clear it */
1037 datastore
= ast_calloc(1, sizeof(*datastore
));
1038 if (datastore
== NULL
) {
1042 datastore
->info
= info
;
1044 datastore
->uid
= ast_strdup(uid
);
1049 int ast_channel_datastore_free(struct ast_datastore
*datastore
)
1053 /* Using the destroy function (if present) destroy the data */
1054 if (datastore
->info
->destroy
!= NULL
&& datastore
->data
!= NULL
) {
1055 datastore
->info
->destroy(datastore
->data
);
1056 datastore
->data
= NULL
;
1059 /* Free allocated UID memory */
1060 if (datastore
->uid
!= NULL
) {
1061 free(datastore
->uid
);
1062 datastore
->uid
= NULL
;
1065 /* Finally free memory used by ourselves */
1071 int ast_channel_datastore_add(struct ast_channel
*chan
, struct ast_datastore
*datastore
)
1075 AST_LIST_INSERT_HEAD(&chan
->datastores
, datastore
, entry
);
1080 int ast_channel_datastore_remove(struct ast_channel
*chan
, struct ast_datastore
*datastore
)
1082 struct ast_datastore
*datastore2
= NULL
;
1085 /* Find our position and remove ourselves */
1086 AST_LIST_TRAVERSE_SAFE_BEGIN(&chan
->datastores
, datastore2
, entry
) {
1087 if (datastore2
== datastore
) {
1088 AST_LIST_REMOVE_CURRENT(&chan
->datastores
, entry
);
1093 AST_LIST_TRAVERSE_SAFE_END
1098 struct ast_datastore
*ast_channel_datastore_find(struct ast_channel
*chan
, const struct ast_datastore_info
*info
, char *uid
)
1100 struct ast_datastore
*datastore
= NULL
;
1105 AST_LIST_TRAVERSE_SAFE_BEGIN(&chan
->datastores
, datastore
, entry
) {
1106 if (datastore
->info
== info
) {
1107 if (uid
!= NULL
&& datastore
->uid
!= NULL
) {
1108 if (!strcasecmp(uid
, datastore
->uid
)) {
1109 /* Matched by type AND uid */
1113 /* Matched by type at least */
1118 AST_LIST_TRAVERSE_SAFE_END
1123 int ast_channel_spy_add(struct ast_channel
*chan
, struct ast_channel_spy
*spy
)
1125 /* Link the owner channel to the spy */
1128 if (!ast_test_flag(spy
, CHANSPY_FORMAT_AUDIO
)) {
1129 ast_log(LOG_WARNING
, "Could not add channel spy '%s' to channel '%s', only audio format spies are supported.\n",
1130 spy
->type
, chan
->name
);
1134 if (ast_test_flag(spy
, CHANSPY_READ_VOLADJUST
) && (spy
->read_queue
.format
!= AST_FORMAT_SLINEAR
)) {
1135 ast_log(LOG_WARNING
, "Cannot provide volume adjustment on '%s' format spies\n",
1136 ast_getformatname(spy
->read_queue
.format
));
1140 if (ast_test_flag(spy
, CHANSPY_WRITE_VOLADJUST
) && (spy
->write_queue
.format
!= AST_FORMAT_SLINEAR
)) {
1141 ast_log(LOG_WARNING
, "Cannot provide volume adjustment on '%s' format spies\n",
1142 ast_getformatname(spy
->write_queue
.format
));
1146 if (ast_test_flag(spy
, CHANSPY_MIXAUDIO
) &&
1147 ((spy
->read_queue
.format
!= AST_FORMAT_SLINEAR
) ||
1148 (spy
->write_queue
.format
!= AST_FORMAT_SLINEAR
))) {
1149 ast_log(LOG_WARNING
, "Cannot provide audio mixing on '%s'-'%s' format spies\n",
1150 ast_getformatname(spy
->read_queue
.format
), ast_getformatname(spy
->write_queue
.format
));
1155 if (!(chan
->spies
= ast_calloc(1, sizeof(*chan
->spies
)))) {
1159 AST_LIST_HEAD_INIT_NOLOCK(&chan
->spies
->list
);
1160 AST_LIST_INSERT_HEAD(&chan
->spies
->list
, spy
, list
);
1162 AST_LIST_INSERT_TAIL(&chan
->spies
->list
, spy
, list
);
1165 if (ast_test_flag(spy
, CHANSPY_TRIGGER_MODE
) != CHANSPY_TRIGGER_NONE
) {
1166 ast_cond_init(&spy
->trigger
, NULL
);
1167 ast_set_flag(spy
, CHANSPY_TRIGGER_READ
);
1168 ast_clear_flag(spy
, CHANSPY_TRIGGER_WRITE
);
1171 ast_log(LOG_DEBUG
, "Spy %s added to channel %s\n",
1172 spy
->type
, chan
->name
);
1177 void ast_channel_spy_stop_by_type(struct ast_channel
*chan
, const char *type
)
1179 struct ast_channel_spy
*spy
;
1184 AST_LIST_TRAVERSE(&chan
->spies
->list
, spy
, list
) {
1185 ast_mutex_lock(&spy
->lock
);
1186 if ((spy
->type
== type
) && (spy
->status
== CHANSPY_RUNNING
)) {
1187 spy
->status
= CHANSPY_STOP
;
1188 if (ast_test_flag(spy
, CHANSPY_TRIGGER_MODE
) != CHANSPY_TRIGGER_NONE
)
1189 ast_cond_signal(&spy
->trigger
);
1191 ast_mutex_unlock(&spy
->lock
);
1195 void ast_channel_spy_trigger_wait(struct ast_channel_spy
*spy
)
1200 tv
= ast_tvadd(ast_tvnow(), ast_samp2tv(50000, 1000));
1201 ts
.tv_sec
= tv
.tv_sec
;
1202 ts
.tv_nsec
= tv
.tv_usec
* 1000;
1204 ast_cond_timedwait(&spy
->trigger
, &spy
->lock
, &ts
);
1207 void ast_channel_spy_remove(struct ast_channel
*chan
, struct ast_channel_spy
*spy
)
1209 struct ast_frame
*f
;
1214 AST_LIST_REMOVE(&chan
->spies
->list
, spy
, list
);
1216 ast_mutex_lock(&spy
->lock
);
1220 for (f
= spy
->read_queue
.head
; f
; f
= spy
->read_queue
.head
) {
1221 spy
->read_queue
.head
= f
->next
;
1224 for (f
= spy
->write_queue
.head
; f
; f
= spy
->write_queue
.head
) {
1225 spy
->write_queue
.head
= f
->next
;
1229 if (ast_test_flag(spy
, CHANSPY_TRIGGER_MODE
) != CHANSPY_TRIGGER_NONE
)
1230 ast_cond_destroy(&spy
->trigger
);
1232 ast_mutex_unlock(&spy
->lock
);
1234 ast_log(LOG_DEBUG
, "Spy %s removed from channel %s\n",
1235 spy
->type
, chan
->name
);
1237 if (AST_LIST_EMPTY(&chan
->spies
->list
)) {
1238 if (chan
->spies
->read_translator
.path
)
1239 ast_translator_free_path(chan
->spies
->read_translator
.path
);
1240 if (chan
->spies
->write_translator
.path
)
1241 ast_translator_free_path(chan
->spies
->write_translator
.path
);
1247 static void detach_spies(struct ast_channel
*chan
)
1249 struct ast_channel_spy
*spy
;
1254 /* Marking the spies as done is sufficient. Chanspy or spy users will get the picture. */
1255 AST_LIST_TRAVERSE(&chan
->spies
->list
, spy
, list
) {
1256 ast_mutex_lock(&spy
->lock
);
1258 if (spy
->status
== CHANSPY_RUNNING
)
1259 spy
->status
= CHANSPY_DONE
;
1260 if (ast_test_flag(spy
, CHANSPY_TRIGGER_MODE
) != CHANSPY_TRIGGER_NONE
)
1261 ast_cond_signal(&spy
->trigger
);
1262 ast_mutex_unlock(&spy
->lock
);
1265 AST_LIST_TRAVERSE_SAFE_BEGIN(&chan
->spies
->list
, spy
, list
)
1266 ast_channel_spy_remove(chan
, spy
);
1267 AST_LIST_TRAVERSE_SAFE_END
;
1270 /*! \brief Softly hangup a channel, don't lock */
1271 int ast_softhangup_nolock(struct ast_channel
*chan
, int cause
)
1274 ast_log(LOG_DEBUG
, "Soft-Hanging up channel '%s'\n", chan
->name
);
1275 /* Inform channel driver that we need to be hung up, if it cares */
1276 chan
->_softhangup
|= cause
;
1277 ast_queue_frame(chan
, &ast_null_frame
);
1278 /* Interrupt any poll call or such */
1279 if (ast_test_flag(chan
, AST_FLAG_BLOCKING
))
1280 pthread_kill(chan
->blocker
, SIGURG
);
1284 /*! \brief Softly hangup a channel, lock */
1285 int ast_softhangup(struct ast_channel
*chan
, int cause
)
1288 ast_channel_lock(chan
);
1289 res
= ast_softhangup_nolock(chan
, cause
);
1290 ast_channel_unlock(chan
);
1294 enum spy_direction
{
1299 #define SPY_QUEUE_SAMPLE_LIMIT 4000 /* half of one second */
1301 static void queue_frame_to_spies(struct ast_channel
*chan
, struct ast_frame
*f
, enum spy_direction dir
)
1303 struct ast_frame
*translated_frame
= NULL
;
1304 struct ast_channel_spy
*spy
;
1305 struct channel_spy_trans
*trans
;
1307 trans
= (dir
== SPY_READ
) ? &chan
->spies
->read_translator
: &chan
->spies
->write_translator
;
1309 AST_LIST_TRAVERSE(&chan
->spies
->list
, spy
, list
) {
1310 struct ast_frame
*last
;
1311 struct ast_frame
*f1
; /* the frame to append */
1312 struct ast_channel_spy_queue
*queue
;
1314 ast_mutex_lock(&spy
->lock
);
1316 queue
= (dir
== SPY_READ
) ? &spy
->read_queue
: &spy
->write_queue
;
1318 if ((queue
->format
== AST_FORMAT_SLINEAR
) && (f
->subclass
!= AST_FORMAT_SLINEAR
)) {
1319 if (!translated_frame
) {
1320 if (trans
->path
&& (trans
->last_format
!= f
->subclass
)) {
1321 ast_translator_free_path(trans
->path
);
1325 ast_log(LOG_DEBUG
, "Building translator from %s to SLINEAR for spies on channel %s\n",
1326 ast_getformatname(f
->subclass
), chan
->name
);
1327 if ((trans
->path
= ast_translator_build_path(AST_FORMAT_SLINEAR
, f
->subclass
)) == NULL
) {
1328 ast_log(LOG_WARNING
, "Cannot build a path from %s to %s\n",
1329 ast_getformatname(f
->subclass
), ast_getformatname(AST_FORMAT_SLINEAR
));
1330 ast_mutex_unlock(&spy
->lock
);
1333 trans
->last_format
= f
->subclass
;
1336 if (!(translated_frame
= ast_translate(trans
->path
, f
, 0))) {
1337 ast_log(LOG_ERROR
, "Translation to %s failed, dropping frame for spies\n",
1338 ast_getformatname(AST_FORMAT_SLINEAR
));
1339 ast_mutex_unlock(&spy
->lock
);
1343 f1
= translated_frame
;
1345 if (f
->subclass
!= queue
->format
) {
1346 ast_log(LOG_WARNING
, "Spy '%s' on channel '%s' wants format '%s', but frame is '%s', dropping\n",
1347 spy
->type
, chan
->name
,
1348 ast_getformatname(queue
->format
), ast_getformatname(f
->subclass
));
1349 ast_mutex_unlock(&spy
->lock
);
1354 /* duplicate and append f1 to the tail */
1357 for (last
= queue
->head
; last
&& last
->next
; last
= last
->next
)
1364 queue
->samples
+= f
->samples
;
1366 if (queue
->samples
> SPY_QUEUE_SAMPLE_LIMIT
) {
1367 if (ast_test_flag(spy
, CHANSPY_TRIGGER_MODE
) != CHANSPY_TRIGGER_NONE
) {
1368 switch (ast_test_flag(spy
, CHANSPY_TRIGGER_MODE
)) {
1369 case CHANSPY_TRIGGER_READ
:
1370 if (dir
== SPY_WRITE
) {
1371 ast_set_flag(spy
, CHANSPY_TRIGGER_WRITE
);
1372 ast_clear_flag(spy
, CHANSPY_TRIGGER_READ
);
1374 ast_log(LOG_DEBUG
, "Switching spy '%s' on '%s' to write-trigger mode\n",
1375 spy
->type
, chan
->name
);
1378 case CHANSPY_TRIGGER_WRITE
:
1379 if (dir
== SPY_READ
) {
1380 ast_set_flag(spy
, CHANSPY_TRIGGER_READ
);
1381 ast_clear_flag(spy
, CHANSPY_TRIGGER_WRITE
);
1383 ast_log(LOG_DEBUG
, "Switching spy '%s' on '%s' to read-trigger mode\n",
1384 spy
->type
, chan
->name
);
1389 ast_log(LOG_DEBUG
, "Triggering queue flush for spy '%s' on '%s'\n",
1390 spy
->type
, chan
->name
);
1391 ast_set_flag(spy
, CHANSPY_TRIGGER_FLUSH
);
1392 ast_cond_signal(&spy
->trigger
);
1395 ast_log(LOG_DEBUG
, "Spy '%s' on channel '%s' %s queue too long, dropping frames\n",
1396 spy
->type
, chan
->name
, (dir
== SPY_READ
) ? "read" : "write");
1397 while (queue
->samples
> SPY_QUEUE_SAMPLE_LIMIT
) {
1398 struct ast_frame
*drop
= queue
->head
;
1400 queue
->samples
-= drop
->samples
;
1401 queue
->head
= drop
->next
;
1406 switch (ast_test_flag(spy
, CHANSPY_TRIGGER_MODE
)) {
1407 case CHANSPY_TRIGGER_READ
:
1408 if (dir
== SPY_READ
)
1409 ast_cond_signal(&spy
->trigger
);
1411 case CHANSPY_TRIGGER_WRITE
:
1412 if (dir
== SPY_WRITE
)
1413 ast_cond_signal(&spy
->trigger
);
1418 ast_mutex_unlock(&spy
->lock
);
1421 if (translated_frame
)
1422 ast_frfree(translated_frame
);
1425 static void free_translation(struct ast_channel
*clone
)
1427 if (clone
->writetrans
)
1428 ast_translator_free_path(clone
->writetrans
);
1429 if (clone
->readtrans
)
1430 ast_translator_free_path(clone
->readtrans
);
1431 clone
->writetrans
= NULL
;
1432 clone
->readtrans
= NULL
;
1433 clone
->rawwriteformat
= clone
->nativeformats
;
1434 clone
->rawreadformat
= clone
->nativeformats
;
1437 /*! \brief Hangup a channel */
1438 int ast_hangup(struct ast_channel
*chan
)
1442 /* Don't actually hang up a channel that will masquerade as someone else, or
1443 if someone is going to masquerade as us */
1444 ast_channel_lock(chan
);
1446 detach_spies(chan
); /* get rid of spies */
1449 if (ast_do_masquerade(chan
))
1450 ast_log(LOG_WARNING
, "Failed to perform masquerade\n");
1454 ast_log(LOG_WARNING
, "%s getting hung up, but someone is trying to masq into us?!?\n", chan
->name
);
1455 ast_channel_unlock(chan
);
1458 /* If this channel is one which will be masqueraded into something,
1459 mark it as a zombie already, so we know to free it later */
1461 ast_set_flag(chan
, AST_FLAG_ZOMBIE
);
1462 ast_channel_unlock(chan
);
1465 free_translation(chan
);
1466 if (chan
->stream
) /* Close audio stream */
1467 ast_closestream(chan
->stream
);
1468 if (chan
->vstream
) /* Close video stream */
1469 ast_closestream(chan
->vstream
);
1471 sched_context_destroy(chan
->sched
);
1475 if (chan
->generatordata
) /* Clear any tone stuff remaining */
1476 chan
->generator
->release(chan
, chan
->generatordata
);
1477 chan
->generatordata
= NULL
;
1478 chan
->generator
= NULL
;
1479 if (chan
->cdr
) { /* End the CDR if it hasn't already */
1480 ast_cdr_end(chan
->cdr
);
1481 ast_cdr_detach(chan
->cdr
); /* Post and Free the CDR */
1484 if (ast_test_flag(chan
, AST_FLAG_BLOCKING
)) {
1485 ast_log(LOG_WARNING
, "Hard hangup called by thread %ld on %s, while fd "
1486 "is blocked by thread %ld in procedure %s! Expect a failure\n",
1487 (long)pthread_self(), chan
->name
, (long)chan
->blocker
, chan
->blockproc
);
1490 if (!ast_test_flag(chan
, AST_FLAG_ZOMBIE
)) {
1492 ast_log(LOG_DEBUG
, "Hanging up channel '%s'\n", chan
->name
);
1493 if (chan
->tech
->hangup
)
1494 res
= chan
->tech
->hangup(chan
);
1497 ast_log(LOG_DEBUG
, "Hanging up zombie '%s'\n", chan
->name
);
1500 ast_channel_unlock(chan
);
1501 manager_event(EVENT_FLAG_CALL
, "Hangup",
1505 "Cause-txt: %s\r\n",
1509 ast_cause2str(chan
->hangupcause
)
1511 ast_channel_free(chan
);
1515 int ast_answer(struct ast_channel
*chan
)
1518 ast_channel_lock(chan
);
1519 /* You can't answer an outbound call */
1520 if (ast_test_flag(chan
, AST_FLAG_OUTGOING
)) {
1521 ast_channel_unlock(chan
);
1524 /* Stop if we're a zombie or need a soft hangup */
1525 if (ast_test_flag(chan
, AST_FLAG_ZOMBIE
) || ast_check_hangup(chan
)) {
1526 ast_channel_unlock(chan
);
1529 switch(chan
->_state
) {
1530 case AST_STATE_RINGING
:
1531 case AST_STATE_RING
:
1532 if (chan
->tech
->answer
)
1533 res
= chan
->tech
->answer(chan
);
1534 ast_setstate(chan
, AST_STATE_UP
);
1535 ast_cdr_answer(chan
->cdr
);
1538 ast_cdr_answer(chan
->cdr
);
1541 ast_channel_unlock(chan
);
1545 void ast_deactivate_generator(struct ast_channel
*chan
)
1547 ast_channel_lock(chan
);
1548 if (chan
->generatordata
) {
1549 if (chan
->generator
&& chan
->generator
->release
)
1550 chan
->generator
->release(chan
, chan
->generatordata
);
1551 chan
->generatordata
= NULL
;
1552 chan
->generator
= NULL
;
1553 chan
->fds
[AST_GENERATOR_FD
] = -1;
1554 ast_clear_flag(chan
, AST_FLAG_WRITE_INT
);
1555 ast_settimeout(chan
, 0, NULL
, NULL
);
1557 ast_channel_unlock(chan
);
1560 static int generator_force(void *data
)
1562 /* Called if generator doesn't have data */
1565 int (*generate
)(struct ast_channel
*chan
, void *tmp
, int datalen
, int samples
);
1566 struct ast_channel
*chan
= data
;
1567 tmp
= chan
->generatordata
;
1568 chan
->generatordata
= NULL
;
1569 generate
= chan
->generator
->generate
;
1570 res
= generate(chan
, tmp
, 0, 160);
1571 chan
->generatordata
= tmp
;
1573 ast_log(LOG_DEBUG
, "Auto-deactivating generator\n");
1574 ast_deactivate_generator(chan
);
1579 int ast_activate_generator(struct ast_channel
*chan
, struct ast_generator
*gen
, void *params
)
1583 ast_channel_lock(chan
);
1585 if (chan
->generatordata
) {
1586 if (chan
->generator
&& chan
->generator
->release
)
1587 chan
->generator
->release(chan
, chan
->generatordata
);
1588 chan
->generatordata
= NULL
;
1592 if (gen
->alloc
&& !(chan
->generatordata
= gen
->alloc(chan
, params
))) {
1597 ast_settimeout(chan
, 160, generator_force
, chan
);
1598 chan
->generator
= gen
;
1601 ast_channel_unlock(chan
);
1606 /*! \brief Wait for x amount of time on a file descriptor to have input. */
1607 int ast_waitfor_n_fd(int *fds
, int n
, int *ms
, int *exception
)
1610 ast_waitfor_nandfds(NULL
, 0, fds
, n
, exception
, &winner
, ms
);
1614 /*! \brief Wait for x amount of time on a file descriptor to have input. */
1615 struct ast_channel
*ast_waitfor_nandfds(struct ast_channel
**c
, int n
, int *fds
, int nfds
,
1616 int *exception
, int *outfd
, int *ms
)
1618 struct timeval start
= { 0 , 0 };
1619 struct pollfd
*pfds
;
1625 long whentohangup
= 0, diff
;
1626 struct ast_channel
*winner
= NULL
;
1632 sz
= n
* AST_MAX_FDS
+ nfds
;
1633 pfds
= alloca(sizeof(*pfds
) * sz
);
1634 fdmap
= alloca(sizeof(*fdmap
) * sz
);
1641 /* Perform any pending masquerades */
1642 for (x
=0; x
< n
; x
++) {
1643 ast_channel_lock(c
[x
]);
1645 if (ast_do_masquerade(c
[x
])) {
1646 ast_log(LOG_WARNING
, "Masquerade failed\n");
1648 ast_channel_unlock(c
[x
]);
1652 if (c
[x
]->whentohangup
) {
1655 diff
= c
[x
]->whentohangup
- now
;
1657 /* Should already be hungup */
1658 c
[x
]->_softhangup
|= AST_SOFTHANGUP_TIMEOUT
;
1659 ast_channel_unlock(c
[x
]);
1662 if (!whentohangup
|| (diff
< whentohangup
))
1663 whentohangup
= diff
;
1665 ast_channel_unlock(c
[x
]);
1667 /* Wait full interval */
1670 rms
= (whentohangup
- now
) * 1000; /* timeout in milliseconds */
1671 if (*ms
>= 0 && *ms
< rms
) /* original *ms still smaller */
1675 * Build the pollfd array, putting the channels' fds first,
1676 * followed by individual fds. Order is important because
1677 * individual fd's must have priority over channel fds.
1680 for (x
=0; x
<n
; x
++) {
1681 for (y
=0; y
<AST_MAX_FDS
; y
++) {
1682 fdmap
[max
].fdno
= y
; /* fd y is linked to this pfds */
1683 fdmap
[max
].chan
= x
; /* channel x is linked to this pfds */
1684 max
+= ast_add_fd(&pfds
[max
], c
[x
]->fds
[y
]);
1686 CHECK_BLOCKING(c
[x
]);
1688 /* Add the individual fds */
1689 for (x
=0; x
<nfds
; x
++) {
1690 fdmap
[max
].chan
= -1;
1691 max
+= ast_add_fd(&pfds
[max
], fds
[x
]);
1695 start
= ast_tvnow();
1697 if (sizeof(int) == 4) { /* XXX fix timeout > 600000 on linux x86-32 */
1702 res
= poll(pfds
, max
, kbrms
);
1705 } while (!res
&& (rms
> 0));
1707 res
= poll(pfds
, max
, rms
);
1710 ast_clear_flag(c
[x
], AST_FLAG_BLOCKING
);
1711 if (res
< 0) { /* Simulate a timeout if we were interrupted */
1716 if (whentohangup
) { /* if we have a timeout, check who expired */
1718 for (x
=0; x
<n
; x
++) {
1719 if (c
[x
]->whentohangup
&& now
>= c
[x
]->whentohangup
) {
1720 c
[x
]->_softhangup
|= AST_SOFTHANGUP_TIMEOUT
;
1726 if (res
== 0) { /* no fd ready, reset timeout and done */
1727 *ms
= 0; /* XXX use 0 since we may not have an exact timeout. */
1731 * Then check if any channel or fd has a pending event.
1732 * Remember to check channels first and fds last, as they
1733 * must have priority on setting 'winner'
1735 for (x
= 0; x
< max
; x
++) {
1736 res
= pfds
[x
].revents
;
1739 if (fdmap
[x
].chan
>= 0) { /* this is a channel */
1740 winner
= c
[fdmap
[x
].chan
]; /* override previous winners */
1742 ast_set_flag(winner
, AST_FLAG_EXCEPTION
);
1744 ast_clear_flag(winner
, AST_FLAG_EXCEPTION
);
1745 winner
->fdno
= fdmap
[x
].fdno
;
1746 } else { /* this is an fd */
1748 *outfd
= pfds
[x
].fd
;
1750 *exception
= (res
& POLLPRI
) ? -1 : 0;
1755 *ms
-= ast_tvdiff_ms(ast_tvnow(), start
);
1762 struct ast_channel
*ast_waitfor_n(struct ast_channel
**c
, int n
, int *ms
)
1764 return ast_waitfor_nandfds(c
, n
, NULL
, 0, NULL
, NULL
, ms
);
1767 int ast_waitfor(struct ast_channel
*c
, int ms
)
1769 int oldms
= ms
; /* -1 if no timeout */
1771 ast_waitfor_nandfds(&c
, 1, NULL
, 0, NULL
, NULL
, &ms
);
1772 if ((ms
< 0) && (oldms
< 0))
1777 /* XXX never to be called with ms = -1 */
1778 int ast_waitfordigit(struct ast_channel
*c
, int ms
)
1780 return ast_waitfordigit_full(c
, ms
, -1, -1);
1783 int ast_settimeout(struct ast_channel
*c
, int samples
, int (*func
)(void *data
), void *data
)
1787 if (c
->timingfd
> -1) {
1792 ast_log(LOG_DEBUG
, "Scheduling timer at %d sample intervals\n", samples
);
1793 res
= ioctl(c
->timingfd
, ZT_TIMERCONFIG
, &samples
);
1794 c
->timingfunc
= func
;
1795 c
->timingdata
= data
;
1801 int ast_waitfordigit_full(struct ast_channel
*c
, int ms
, int audiofd
, int cmdfd
)
1804 /* Stop if we're a zombie or need a soft hangup */
1805 if (ast_test_flag(c
, AST_FLAG_ZOMBIE
) || ast_check_hangup(c
))
1807 /* Wait for a digit, no more than ms milliseconds total. */
1809 struct ast_channel
*rchan
;
1813 rchan
= ast_waitfor_nandfds(&c
, 1, &cmdfd
, (cmdfd
> -1) ? 1 : 0, NULL
, &outfd
, &ms
);
1814 if (!rchan
&& outfd
< 0 && ms
) {
1815 if (errno
== 0 || errno
== EINTR
)
1817 ast_log(LOG_WARNING
, "Wait failed (%s)\n", strerror(errno
));
1819 } else if (outfd
> -1) {
1820 /* The FD we were watching has something waiting */
1824 struct ast_frame
*f
= ast_read(c
);
1828 switch(f
->frametype
) {
1829 case AST_FRAME_DTMF
:
1833 case AST_FRAME_CONTROL
:
1834 switch(f
->subclass
) {
1835 case AST_CONTROL_HANGUP
:
1838 case AST_CONTROL_RINGING
:
1839 case AST_CONTROL_ANSWER
:
1843 ast_log(LOG_WARNING
, "Unexpected control subclass '%d'\n", f
->subclass
);
1845 case AST_FRAME_VOICE
:
1846 /* Write audio if appropriate */
1848 write(audiofd
, f
->data
, f
->datalen
);
1854 return 0; /* Time is up */
1857 static struct ast_frame
*__ast_read(struct ast_channel
*chan
, int dropaudio
)
1859 struct ast_frame
*f
= NULL
; /* the return value */
1863 /* this function is very long so make sure there is only one return
1864 * point at the end (there is only one exception to this).
1866 ast_channel_lock(chan
);
1868 if (ast_do_masquerade(chan
)) {
1869 ast_log(LOG_WARNING
, "Failed to perform masquerade\n");
1871 f
= &ast_null_frame
;
1876 /* Stop if we're a zombie or need a soft hangup */
1877 if (ast_test_flag(chan
, AST_FLAG_ZOMBIE
) || ast_check_hangup(chan
)) {
1878 if (chan
->generator
)
1879 ast_deactivate_generator(chan
);
1882 prestate
= chan
->_state
;
1884 if (!ast_test_flag(chan
, AST_FLAG_DEFER_DTMF
) && !ast_strlen_zero(chan
->dtmfq
)) {
1885 /* We have DTMF that has been deferred. Return it now */
1886 chan
->dtmff
.frametype
= AST_FRAME_DTMF
;
1887 chan
->dtmff
.subclass
= chan
->dtmfq
[0];
1888 /* Drop first digit from the buffer */
1889 memmove(chan
->dtmfq
, chan
->dtmfq
+ 1, sizeof(chan
->dtmfq
) - 1);
1894 /* Read and ignore anything on the alertpipe, but read only
1895 one sizeof(blah) per frame that we send from it */
1896 if (chan
->alertpipe
[0] > -1)
1897 read(chan
->alertpipe
[0], &blah
, sizeof(blah
));
1900 if (chan
->timingfd
> -1 && chan
->fdno
== AST_TIMING_FD
&& ast_test_flag(chan
, AST_FLAG_EXCEPTION
)) {
1903 ast_clear_flag(chan
, AST_FLAG_EXCEPTION
);
1905 /* IF we can't get event, assume it's an expired as-per the old interface */
1906 res
= ioctl(chan
->timingfd
, ZT_GETEVENT
, &blah
);
1908 blah
= ZT_EVENT_TIMER_EXPIRED
;
1910 if (blah
== ZT_EVENT_TIMER_PING
) {
1911 if (!chan
->readq
|| !chan
->readq
->next
) {
1912 /* Acknowledge PONG unless we need it again */
1913 if (ioctl(chan
->timingfd
, ZT_TIMERPONG
, &blah
)) {
1914 ast_log(LOG_WARNING
, "Failed to pong timer on '%s': %s\n", chan
->name
, strerror(errno
));
1917 } else if (blah
== ZT_EVENT_TIMER_EXPIRED
) {
1918 ioctl(chan
->timingfd
, ZT_TIMERACK
, &blah
);
1919 if (chan
->timingfunc
) {
1920 /* save a copy of func/data before unlocking the channel */
1921 int (*func
)(void *) = chan
->timingfunc
;
1922 void *data
= chan
->timingdata
;
1923 ast_channel_unlock(chan
);
1927 ioctl(chan
->timingfd
, ZT_TIMERCONFIG
, &blah
);
1928 chan
->timingdata
= NULL
;
1929 ast_channel_unlock(chan
);
1931 /* cannot 'goto done' because the channel is already unlocked */
1932 return &ast_null_frame
;
1934 ast_log(LOG_NOTICE
, "No/unknown event '%d' on timer for '%s'?\n", blah
, chan
->name
);
1937 if (chan
->fds
[AST_GENERATOR_FD
] > -1 && chan
->fdno
== AST_GENERATOR_FD
) {
1938 /* if the AST_GENERATOR_FD is set, call the generator with args
1939 * set to -1 so it can do whatever it needs to.
1941 void *tmp
= chan
->generatordata
;
1942 chan
->generatordata
= NULL
; /* reset to let ast_write get through */
1943 chan
->generator
->generate(chan
, tmp
, -1, -1);
1944 chan
->generatordata
= tmp
;
1945 f
= &ast_null_frame
;
1949 /* Check for pending read queue */
1952 chan
->readq
= f
->next
;
1954 /* Interpret hangup and return NULL */
1955 /* XXX why not the same for frames from the channel ? */
1956 if (f
->frametype
== AST_FRAME_CONTROL
&& f
->subclass
== AST_CONTROL_HANGUP
) {
1961 chan
->blocker
= pthread_self();
1962 if (ast_test_flag(chan
, AST_FLAG_EXCEPTION
)) {
1963 if (chan
->tech
->exception
)
1964 f
= chan
->tech
->exception(chan
);
1966 ast_log(LOG_WARNING
, "Exception flag set on '%s', but no exception handler\n", chan
->name
);
1967 f
= &ast_null_frame
;
1969 /* Clear the exception flag */
1970 ast_clear_flag(chan
, AST_FLAG_EXCEPTION
);
1971 } else if (chan
->tech
->read
)
1972 f
= chan
->tech
->read(chan
);
1974 ast_log(LOG_WARNING
, "No read routine on channel %s\n", chan
->name
);
1978 /* if the channel driver returned more than one frame, stuff the excess
1979 into the readq for the next ast_read call
1982 chan
->readq
= f
->next
;
1986 switch (f
->frametype
) {
1987 case AST_FRAME_CONTROL
:
1988 if (f
->subclass
== AST_CONTROL_ANSWER
) {
1989 if (!ast_test_flag(chan
, AST_FLAG_OUTGOING
)) {
1990 ast_log(LOG_DEBUG
, "Ignoring answer on an inbound call!\n");
1992 f
= &ast_null_frame
;
1993 } else if (prestate
== AST_STATE_UP
) {
1994 ast_log(LOG_DEBUG
, "Dropping duplicate answer!\n");
1996 f
= &ast_null_frame
;
1998 /* Answer the CDR */
1999 ast_setstate(chan
, AST_STATE_UP
);
2000 ast_cdr_answer(chan
->cdr
);
2004 case AST_FRAME_DTMF
:
2005 ast_log(LOG_DTMF
, "DTMF '%c' received on %s\n", f
->subclass
, chan
->name
);
2006 if (ast_test_flag(chan
, AST_FLAG_DEFER_DTMF
)) {
2007 if (strlen(chan
->dtmfq
) < sizeof(chan
->dtmfq
) - 2)
2008 chan
->dtmfq
[strlen(chan
->dtmfq
)] = f
->subclass
;
2010 ast_log(LOG_WARNING
, "Dropping deferred DTMF digits on %s\n", chan
->name
);
2012 f
= &ast_null_frame
;
2015 case AST_FRAME_DTMF_BEGIN
:
2016 ast_log(LOG_DTMF
, "DTMF begin '%c' received on %s\n", f
->subclass
, chan
->name
);
2018 case AST_FRAME_DTMF_END
:
2019 ast_log(LOG_DTMF
, "DTMF end '%c' received on %s\n", f
->subclass
, chan
->name
);
2021 case AST_FRAME_VOICE
:
2024 f
= &ast_null_frame
;
2025 } else if (!(f
->subclass
& chan
->nativeformats
)) {
2026 /* This frame can't be from the current native formats -- drop it on the
2028 ast_log(LOG_NOTICE
, "Dropping incompatible voice frame on %s of format %s since our native format has changed to %s\n",
2029 chan
->name
, ast_getformatname(f
->subclass
), ast_getformatname(chan
->nativeformats
));
2031 f
= &ast_null_frame
;
2034 queue_frame_to_spies(chan
, f
, SPY_READ
);
2036 if (chan
->monitor
&& chan
->monitor
->read_stream
) {
2037 /* XXX what does this do ? */
2038 #ifndef MONITOR_CONSTANT_DELAY
2039 int jump
= chan
->outsmpl
- chan
->insmpl
- 4 * f
->samples
;
2041 if (ast_seekstream(chan
->monitor
->read_stream
, jump
+ f
->samples
, SEEK_FORCECUR
) == -1)
2042 ast_log(LOG_WARNING
, "Failed to perform seek in monitoring read stream, synchronization between the files may be broken\n");
2043 chan
->insmpl
+= jump
+ 4 * f
->samples
;
2045 chan
->insmpl
+= f
->samples
;
2047 int jump
= chan
->outsmpl
- chan
->insmpl
;
2048 if (jump
- MONITOR_DELAY
>= 0) {
2049 if (ast_seekstream(chan
->monitor
->read_stream
, jump
- f
->samples
, SEEK_FORCECUR
) == -1)
2050 ast_log(LOG_WARNING
, "Failed to perform seek in monitoring read stream, synchronization between the files may be broken\n");
2051 chan
->insmpl
+= jump
;
2053 chan
->insmpl
+= f
->samples
;
2055 if (chan
->monitor
->state
== AST_MONITOR_RUNNING
) {
2056 if (ast_writestream(chan
->monitor
->read_stream
, f
) < 0)
2057 ast_log(LOG_WARNING
, "Failed to write data to channel monitor read stream\n");
2061 if (chan
->readtrans
&& (f
= ast_translate(chan
->readtrans
, f
, 1)) == NULL
)
2062 f
= &ast_null_frame
;
2064 /* Run generator sitting on the line if timing device not available
2065 * and synchronous generation of outgoing frames is necessary */
2066 if (chan
->generatordata
&& !ast_internal_timing_enabled(chan
)) {
2067 void *tmp
= chan
->generatordata
;
2070 if (chan
->timingfunc
) {
2071 if (option_debug
> 1)
2072 ast_log(LOG_DEBUG
, "Generator got voice, switching to phase locked mode\n");
2073 ast_settimeout(chan
, 0, NULL
, NULL
);
2076 chan
->generatordata
= NULL
; /* reset, to let writes go through */
2077 res
= chan
->generator
->generate(chan
, tmp
, f
->datalen
, f
->samples
);
2078 chan
->generatordata
= tmp
;
2080 if (option_debug
> 1)
2081 ast_log(LOG_DEBUG
, "Auto-deactivating generator\n");
2082 ast_deactivate_generator(chan
);
2085 } else if (f
->frametype
== AST_FRAME_CNG
) {
2086 if (chan
->generator
&& !chan
->timingfunc
&& (chan
->timingfd
> -1)) {
2087 if (option_debug
> 1)
2088 ast_log(LOG_DEBUG
, "Generator got CNG, switching to timed mode\n");
2089 ast_settimeout(chan
, 160, generator_force
, chan
);
2095 /* Make sure we always return NULL in the future */
2096 chan
->_softhangup
|= AST_SOFTHANGUP_DEV
;
2097 if (chan
->generator
)
2098 ast_deactivate_generator(chan
);
2099 /* End the CDR if appropriate */
2101 ast_cdr_end(chan
->cdr
);
2104 /* High bit prints debugging */
2105 if (chan
->fin
& DEBUGCHAN_FLAG
)
2106 ast_frame_dump(chan
->name
, f
, "<<");
2107 chan
->fin
= FRAMECOUNT_INC(chan
->fin
);
2110 ast_channel_unlock(chan
);
2114 int ast_internal_timing_enabled(struct ast_channel
*chan
)
2116 int ret
= ast_opt_internal_timing
&& chan
->timingfd
> -1;
2117 if (option_debug
> 4)
2118 ast_log(LOG_DEBUG
, "Internal timing is %s (option_internal_timing=%d chan->timingfd=%d)\n", ret
? "enabled": "disabled", ast_opt_internal_timing
, chan
->timingfd
);
2122 struct ast_frame
*ast_read(struct ast_channel
*chan
)
2124 return __ast_read(chan
, 0);
2127 struct ast_frame
*ast_read_noaudio(struct ast_channel
*chan
)
2129 return __ast_read(chan
, 1);
2132 int ast_indicate(struct ast_channel
*chan
, int condition
)
2134 return ast_indicate_data(chan
, condition
, NULL
, 0);
2137 int ast_indicate_data(struct ast_channel
*chan
, int condition
, const void *data
, size_t datalen
)
2141 ast_channel_lock(chan
);
2142 /* Stop if we're a zombie or need a soft hangup */
2143 if (ast_test_flag(chan
, AST_FLAG_ZOMBIE
) || ast_check_hangup(chan
)) {
2144 ast_channel_unlock(chan
);
2147 if (chan
->tech
->indicate
)
2148 res
= chan
->tech
->indicate(chan
, condition
, data
, datalen
);
2149 ast_channel_unlock(chan
);
2150 if (!chan
->tech
->indicate
|| res
) {
2152 * Device does not support (that) indication, lets fake
2153 * it by doing our own tone generation. (PM2002)
2156 ast_playtones_stop(chan
);
2158 const struct tone_zone_sound
*ts
= NULL
;
2159 switch (condition
) {
2160 case AST_CONTROL_RINGING
:
2161 ts
= ast_get_indication_tone(chan
->zone
, "ring");
2163 case AST_CONTROL_BUSY
:
2164 ts
= ast_get_indication_tone(chan
->zone
, "busy");
2166 case AST_CONTROL_CONGESTION
:
2167 ts
= ast_get_indication_tone(chan
->zone
, "congestion");
2170 if (ts
&& ts
->data
[0]) {
2171 ast_log(LOG_DEBUG
, "Driver for channel '%s' does not support indication %d, emulating it\n", chan
->name
, condition
);
2172 ast_playtones_start(chan
,0,ts
->data
, 1);
2174 } else if (condition
== AST_CONTROL_PROGRESS
) {
2175 /* ast_playtones_stop(chan); */
2176 } else if (condition
== AST_CONTROL_PROCEEDING
) {
2177 /* Do nothing, really */
2178 } else if (condition
== AST_CONTROL_HOLD
) {
2179 /* Do nothing.... */
2180 } else if (condition
== AST_CONTROL_UNHOLD
) {
2181 /* Do nothing.... */
2182 } else if (condition
== AST_CONTROL_VIDUPDATE
) {
2183 /* Do nothing.... */
2186 ast_log(LOG_WARNING
, "Unable to handle indication %d for '%s'\n", condition
, chan
->name
);
2194 int ast_recvchar(struct ast_channel
*chan
, int timeout
)
2197 char *buf
= ast_recvtext(chan
, timeout
);
2199 return -1; /* error or timeout */
2200 c
= *(unsigned char *)buf
;
2205 char *ast_recvtext(struct ast_channel
*chan
, int timeout
)
2211 struct ast_frame
*f
;
2212 if (ast_check_hangup(chan
))
2214 res
= ast_waitfor(chan
, timeout
);
2215 if (res
<= 0) /* timeout or error */
2217 timeout
= res
; /* update timeout */
2220 break; /* no frame */
2221 if (f
->frametype
== AST_FRAME_CONTROL
&& f
->subclass
== AST_CONTROL_HANGUP
)
2222 done
= 1; /* force a break */
2223 else if (f
->frametype
== AST_FRAME_TEXT
) { /* what we want */
2224 buf
= ast_strndup((char *) f
->data
, f
->datalen
); /* dup and break */
2232 int ast_sendtext(struct ast_channel
*chan
, const char *text
)
2235 /* Stop if we're a zombie or need a soft hangup */
2236 if (ast_test_flag(chan
, AST_FLAG_ZOMBIE
) || ast_check_hangup(chan
))
2238 CHECK_BLOCKING(chan
);
2239 if (chan
->tech
->send_text
)
2240 res
= chan
->tech
->send_text(chan
, text
);
2241 ast_clear_flag(chan
, AST_FLAG_BLOCKING
);
2245 static int do_senddigit(struct ast_channel
*chan
, char digit
)
2249 if (chan
->tech
->send_digit
)
2250 res
= chan
->tech
->send_digit(chan
, digit
);
2253 * Device does not support DTMF tones, lets fake
2254 * it by doing our own generation. (PM2002)
2256 static const char* dtmf_tones
[] = {
2257 "!941+1336/100,!0/100", /* 0 */
2258 "!697+1209/100,!0/100", /* 1 */
2259 "!697+1336/100,!0/100", /* 2 */
2260 "!697+1477/100,!0/100", /* 3 */
2261 "!770+1209/100,!0/100", /* 4 */
2262 "!770+1336/100,!0/100", /* 5 */
2263 "!770+1477/100,!0/100", /* 6 */
2264 "!852+1209/100,!0/100", /* 7 */
2265 "!852+1336/100,!0/100", /* 8 */
2266 "!852+1477/100,!0/100", /* 9 */
2267 "!697+1633/100,!0/100", /* A */
2268 "!770+1633/100,!0/100", /* B */
2269 "!852+1633/100,!0/100", /* C */
2270 "!941+1633/100,!0/100", /* D */
2271 "!941+1209/100,!0/100", /* * */
2272 "!941+1477/100,!0/100" }; /* # */
2273 if (digit
>= '0' && digit
<='9')
2274 ast_playtones_start(chan
, 0, dtmf_tones
[digit
-'0'], 0);
2275 else if (digit
>= 'A' && digit
<= 'D')
2276 ast_playtones_start(chan
, 0, dtmf_tones
[digit
-'A'+10], 0);
2277 else if (digit
== '*')
2278 ast_playtones_start(chan
, 0, dtmf_tones
[14], 0);
2279 else if (digit
== '#')
2280 ast_playtones_start(chan
, 0, dtmf_tones
[15], 0);
2283 ast_log(LOG_DEBUG
, "Unable to generate DTMF tone '%c' for '%s'\n", digit
, chan
->name
);
2289 int ast_senddigit(struct ast_channel
*chan
, char digit
)
2291 return do_senddigit(chan
, digit
);
2294 int ast_prod(struct ast_channel
*chan
)
2296 struct ast_frame a
= { AST_FRAME_VOICE
};
2299 /* Send an empty audio frame to get things moving */
2300 if (chan
->_state
!= AST_STATE_UP
) {
2301 ast_log(LOG_DEBUG
, "Prodding channel '%s'\n", chan
->name
);
2302 a
.subclass
= chan
->rawwriteformat
;
2303 a
.data
= nothing
+ AST_FRIENDLY_OFFSET
;
2305 if (ast_write(chan
, &a
))
2306 ast_log(LOG_WARNING
, "Prodding channel '%s' failed\n", chan
->name
);
2311 int ast_write_video(struct ast_channel
*chan
, struct ast_frame
*fr
)
2314 if (!chan
->tech
->write_video
)
2316 res
= ast_write(chan
, fr
);
2322 int ast_write(struct ast_channel
*chan
, struct ast_frame
*fr
)
2325 struct ast_frame
*f
= NULL
;
2327 /* Stop if we're a zombie or need a soft hangup */
2328 ast_channel_lock(chan
);
2329 if (ast_test_flag(chan
, AST_FLAG_ZOMBIE
) || ast_check_hangup(chan
))
2332 /* Handle any pending masquerades */
2333 if (chan
->masq
&& ast_do_masquerade(chan
)) {
2334 ast_log(LOG_WARNING
, "Failed to perform masquerade\n");
2338 res
= 0; /* XXX explain, why 0 ? */
2341 if (chan
->generatordata
) {
2342 if (ast_test_flag(chan
, AST_FLAG_WRITE_INT
))
2343 ast_deactivate_generator(chan
);
2345 res
= 0; /* XXX explain, why 0 ? */
2349 /* High bit prints debugging */
2350 if (chan
->fout
& DEBUGCHAN_FLAG
)
2351 ast_frame_dump(chan
->name
, fr
, ">>");
2352 CHECK_BLOCKING(chan
);
2353 switch(fr
->frametype
) {
2354 case AST_FRAME_CONTROL
:
2355 /* XXX Interpret control frames XXX */
2356 ast_log(LOG_WARNING
, "Don't know how to handle control frames yet\n");
2358 case AST_FRAME_DTMF_BEGIN
:
2359 res
= (chan
->tech
->send_digit_begin
== NULL
) ? 0 :
2360 chan
->tech
->send_digit_begin(chan
, fr
->subclass
);
2362 case AST_FRAME_DTMF_END
:
2363 res
= (chan
->tech
->send_digit_end
== NULL
) ? 0 :
2364 chan
->tech
->send_digit_end(chan
);
2366 case AST_FRAME_DTMF
:
2367 ast_clear_flag(chan
, AST_FLAG_BLOCKING
);
2368 ast_channel_unlock(chan
);
2369 res
= do_senddigit(chan
,fr
->subclass
);
2370 ast_channel_lock(chan
);
2371 CHECK_BLOCKING(chan
);
2373 case AST_FRAME_TEXT
:
2374 res
= (chan
->tech
->send_text
== NULL
) ? 0 :
2375 chan
->tech
->send_text(chan
, (char *) fr
->data
);
2377 case AST_FRAME_HTML
:
2378 res
= (chan
->tech
->send_html
== NULL
) ? 0 :
2379 chan
->tech
->send_html(chan
, fr
->subclass
, (char *) fr
->data
, fr
->datalen
);
2381 case AST_FRAME_VIDEO
:
2382 /* XXX Handle translation of video codecs one day XXX */
2383 res
= (chan
->tech
->write_video
== NULL
) ? 0 :
2384 chan
->tech
->write_video(chan
, fr
);
2386 case AST_FRAME_MODEM
:
2387 res
= (chan
->tech
->write
== NULL
) ? 0 :
2388 chan
->tech
->write(chan
, fr
);
2390 case AST_FRAME_VOICE
:
2391 if (chan
->tech
->write
== NULL
)
2392 break; /*! \todo XXX should return 0 maybe ? */
2394 /* Bypass translator if we're writing format in the raw write format. This
2395 allows mixing of native / non-native formats */
2396 if (fr
->subclass
== chan
->rawwriteformat
)
2399 f
= (chan
->writetrans
) ? ast_translate(chan
->writetrans
, fr
, 0) : fr
;
2404 queue_frame_to_spies(chan
, f
, SPY_WRITE
);
2406 if (chan
->monitor
&& chan
->monitor
->write_stream
) {
2407 /* XXX must explain this code */
2408 #ifndef MONITOR_CONSTANT_DELAY
2409 int jump
= chan
->insmpl
- chan
->outsmpl
- 4 * f
->samples
;
2411 if (ast_seekstream(chan
->monitor
->write_stream
, jump
+ f
->samples
, SEEK_FORCECUR
) == -1)
2412 ast_log(LOG_WARNING
, "Failed to perform seek in monitoring write stream, synchronization between the files may be broken\n");
2413 chan
->outsmpl
+= jump
+ 4 * f
->samples
;
2415 chan
->outsmpl
+= f
->samples
;
2417 int jump
= chan
->insmpl
- chan
->outsmpl
;
2418 if (jump
- MONITOR_DELAY
>= 0) {
2419 if (ast_seekstream(chan
->monitor
->write_stream
, jump
- f
->samples
, SEEK_FORCECUR
) == -1)
2420 ast_log(LOG_WARNING
, "Failed to perform seek in monitoring write stream, synchronization between the files may be broken\n");
2421 chan
->outsmpl
+= jump
;
2423 chan
->outsmpl
+= f
->samples
;
2425 if (chan
->monitor
->state
== AST_MONITOR_RUNNING
) {
2426 if (ast_writestream(chan
->monitor
->write_stream
, f
) < 0)
2427 ast_log(LOG_WARNING
, "Failed to write data to channel monitor write stream\n");
2431 res
= chan
->tech
->write(chan
, f
);
2438 ast_clear_flag(chan
, AST_FLAG_BLOCKING
);
2439 /* Consider a write failure to force a soft hangup */
2441 chan
->_softhangup
|= AST_SOFTHANGUP_DEV
;
2443 chan
->fout
= FRAMECOUNT_INC(chan
->fout
);
2446 ast_channel_unlock(chan
);
2450 static int set_format(struct ast_channel
*chan
, int fmt
, int *rawformat
, int *format
,
2451 struct ast_trans_pvt
**trans
, const int direction
)
2456 /* Make sure we only consider audio */
2457 fmt
&= AST_FORMAT_AUDIO_MASK
;
2459 native
= chan
->nativeformats
;
2460 /* Find a translation path from the native format to one of the desired formats */
2463 res
= ast_translator_best_choice(&fmt
, &native
);
2466 res
= ast_translator_best_choice(&native
, &fmt
);
2469 ast_log(LOG_WARNING
, "Unable to find a codec translation path from %s to %s\n",
2470 ast_getformatname(native
), ast_getformatname(fmt
));
2474 /* Now we have a good choice for both. */
2475 ast_channel_lock(chan
);
2476 *rawformat
= native
;
2477 /* User perspective is fmt */
2479 /* Free any read translation we have right now */
2481 ast_translator_free_path(*trans
);
2482 /* Build a translation path from the raw format to the desired format */
2485 *trans
= ast_translator_build_path(*format
, *rawformat
);
2488 *trans
= ast_translator_build_path(*rawformat
, *format
);
2489 ast_channel_unlock(chan
);
2491 ast_log(LOG_DEBUG
, "Set channel %s to %s format %s\n", chan
->name
,
2492 direction
? "write" : "read", ast_getformatname(fmt
));
2496 int ast_set_read_format(struct ast_channel
*chan
, int fmt
)
2498 return set_format(chan
, fmt
, &chan
->rawreadformat
, &chan
->readformat
,
2499 &chan
->readtrans
, 0);
2502 int ast_set_write_format(struct ast_channel
*chan
, int fmt
)
2504 return set_format(chan
, fmt
, &chan
->rawwriteformat
, &chan
->writeformat
,
2505 &chan
->writetrans
, 1);
2508 struct ast_channel
*__ast_request_and_dial(const char *type
, int format
, void *data
, int timeout
, int *outstate
, const char *cid_num
, const char *cid_name
, struct outgoing_helper
*oh
)
2512 struct ast_channel
*chan
;
2518 outstate
= &dummy_outstate
; /* make outstate always a valid pointer */
2520 chan
= ast_request(type
, format
, data
, &cause
);
2522 ast_log(LOG_NOTICE
, "Unable to request channel %s/%s\n", type
, (char *)data
);
2523 /* compute error and return */
2524 if (cause
== AST_CAUSE_BUSY
)
2525 *outstate
= AST_CONTROL_BUSY
;
2526 else if (cause
== AST_CAUSE_CONGESTION
)
2527 *outstate
= AST_CONTROL_CONGESTION
;
2533 ast_set_variables(chan
, oh
->vars
);
2534 /* XXX why is this necessary, for the parent_channel perhaps ? */
2535 if (!ast_strlen_zero(oh
->cid_num
) && !ast_strlen_zero(oh
->cid_name
))
2536 ast_set_callerid(chan
, oh
->cid_num
, oh
->cid_name
, oh
->cid_num
);
2537 if (oh
->parent_channel
)
2538 ast_channel_inherit_variables(oh
->parent_channel
, chan
);
2540 ast_cdr_setaccount(chan
, oh
->account
);
2542 ast_set_callerid(chan
, cid_num
, cid_name
, cid_num
);
2544 if (ast_call(chan
, data
, 0)) { /* ast_call failed... */
2545 ast_log(LOG_NOTICE
, "Unable to call channel %s/%s\n", type
, (char *)data
);
2547 res
= 1; /* mark success in case chan->_state is already AST_STATE_UP */
2548 while (timeout
&& chan
->_state
!= AST_STATE_UP
) {
2549 struct ast_frame
*f
;
2550 res
= ast_waitfor(chan
, timeout
);
2551 if (res
<= 0) /* error, timeout, or done */
2557 *outstate
= AST_CONTROL_HANGUP
;
2561 if (f
->frametype
== AST_FRAME_CONTROL
) {
2562 switch (f
->subclass
) {
2563 case AST_CONTROL_RINGING
: /* record but keep going */
2564 *outstate
= f
->subclass
;
2567 case AST_CONTROL_BUSY
:
2568 case AST_CONTROL_CONGESTION
:
2569 case AST_CONTROL_ANSWER
:
2570 *outstate
= f
->subclass
;
2571 timeout
= 0; /* trick to force exit from the while() */
2575 case AST_CONTROL_PROGRESS
:
2576 case AST_CONTROL_PROCEEDING
:
2577 case AST_CONTROL_HOLD
:
2578 case AST_CONTROL_UNHOLD
:
2579 case AST_CONTROL_VIDUPDATE
:
2580 case -1: /* Ignore -- just stopping indications */
2584 ast_log(LOG_NOTICE
, "Don't know what to do with control frame %d\n", f
->subclass
);
2593 if (!ast_strlen_zero(oh
->context
))
2594 ast_copy_string(chan
->context
, oh
->context
, sizeof(chan
->context
));
2595 if (!ast_strlen_zero(oh
->exten
))
2596 ast_copy_string(chan
->exten
, oh
->exten
, sizeof(chan
->exten
));
2598 chan
->priority
= oh
->priority
;
2600 if (chan
->_state
== AST_STATE_UP
)
2601 *outstate
= AST_CONTROL_ANSWER
;
2604 if (!chan
->cdr
&& (chan
->cdr
= ast_cdr_alloc()))
2605 ast_cdr_init(chan
->cdr
, chan
);
2608 snprintf(tmp
, sizeof(tmp
), "%s/%s", type
, (char *)data
);
2609 ast_cdr_setapp(chan
->cdr
,"Dial",tmp
);
2610 ast_cdr_update(chan
);
2611 ast_cdr_start(chan
->cdr
);
2612 ast_cdr_end(chan
->cdr
);
2613 /* If the cause wasn't handled properly */
2614 if (ast_cdr_disposition(chan
->cdr
,chan
->hangupcause
))
2615 ast_cdr_failed(chan
->cdr
);
2623 struct ast_channel
*ast_request_and_dial(const char *type
, int format
, void *data
, int timeout
, int *outstate
, const char *cidnum
, const char *cidname
)
2625 return __ast_request_and_dial(type
, format
, data
, timeout
, outstate
, cidnum
, cidname
, NULL
);
2628 struct ast_channel
*ast_request(const char *type
, int format
, void *data
, int *cause
)
2630 struct chanlist
*chan
;
2631 struct ast_channel
*c
;
2636 int videoformat
= format
& AST_FORMAT_VIDEO_MASK
;
2640 *cause
= AST_CAUSE_NOTDEFINED
;
2642 if (AST_LIST_LOCK(&channels
)) {
2643 ast_log(LOG_WARNING
, "Unable to lock channel list\n");
2647 AST_LIST_TRAVERSE(&backends
, chan
, list
) {
2648 if (strcasecmp(type
, chan
->tech
->type
))
2651 capabilities
= chan
->tech
->capabilities
;
2652 fmt
= format
& AST_FORMAT_AUDIO_MASK
;
2653 res
= ast_translator_best_choice(&fmt
, &capabilities
);
2655 ast_log(LOG_WARNING
, "No translator path exists for channel type %s (native %d) to %d\n", type
, chan
->tech
->capabilities
, format
);
2656 AST_LIST_UNLOCK(&channels
);
2659 AST_LIST_UNLOCK(&channels
);
2660 if (!chan
->tech
->requester
)
2663 if (!(c
= chan
->tech
->requester(type
, capabilities
| videoformat
, data
, cause
)))
2666 if (c
->_state
== AST_STATE_DOWN
) {
2667 manager_event(EVENT_FLAG_CALL
, "Newchannel",
2671 "CallerIDName: %s\r\n"
2673 c
->name
, ast_state2str(c
->_state
),
2674 S_OR(c
->cid
.cid_num
, "<unknown>"),
2675 S_OR(c
->cid
.cid_name
, "<unknown>"),
2681 ast_log(LOG_WARNING
, "No channel type registered for '%s'\n", type
);
2682 *cause
= AST_CAUSE_NOSUCHDRIVER
;
2683 AST_LIST_UNLOCK(&channels
);
2688 int ast_call(struct ast_channel
*chan
, char *addr
, int timeout
)
2690 /* Place an outgoing call, but don't wait any longer than timeout ms before returning.
2691 If the remote end does not answer within the timeout, then do NOT hang up, but
2694 /* Stop if we're a zombie or need a soft hangup */
2695 ast_channel_lock(chan
);
2696 if (!ast_test_flag(chan
, AST_FLAG_ZOMBIE
) && !ast_check_hangup(chan
)) {
2697 if (chan
->tech
->call
)
2698 res
= chan
->tech
->call(chan
, addr
, timeout
);
2699 ast_set_flag(chan
, AST_FLAG_OUTGOING
);
2701 ast_channel_unlock(chan
);
2706 \brief Transfer a call to dest, if the channel supports transfer
2710 \arg the manager interface
2712 int ast_transfer(struct ast_channel
*chan
, char *dest
)
2716 /* Stop if we're a zombie or need a soft hangup */
2717 ast_channel_lock(chan
);
2718 if (!ast_test_flag(chan
, AST_FLAG_ZOMBIE
) && !ast_check_hangup(chan
)) {
2719 if (chan
->tech
->transfer
) {
2720 res
= chan
->tech
->transfer(chan
, dest
);
2726 ast_channel_unlock(chan
);
2730 int ast_readstring(struct ast_channel
*c
, char *s
, int len
, int timeout
, int ftimeout
, char *enders
)
2732 return ast_readstring_full(c
, s
, len
, timeout
, ftimeout
, enders
, -1, -1);
2735 int ast_readstring_full(struct ast_channel
*c
, char *s
, int len
, int timeout
, int ftimeout
, char *enders
, int audiofd
, int ctrlfd
)
2737 int pos
= 0; /* index in the buffer where we accumulate digits */
2740 /* Stop if we're a zombie or need a soft hangup */
2741 if (ast_test_flag(c
, AST_FLAG_ZOMBIE
) || ast_check_hangup(c
))
2748 d
= ast_waitstream_full(c
, AST_DIGIT_ANY
, audiofd
, ctrlfd
);
2752 d
= ast_waitfordigit_full(c
, to
, audiofd
, ctrlfd
);
2754 d
= ast_waitfordigit_full(c
, to
, audiofd
, ctrlfd
);
2766 if (!strchr(enders
, d
))
2768 if (strchr(enders
, d
) || (pos
>= len
)) {
2778 int ast_channel_supports_html(struct ast_channel
*chan
)
2780 return (chan
->tech
->send_html
) ? 1 : 0;
2783 int ast_channel_sendhtml(struct ast_channel
*chan
, int subclass
, const char *data
, int datalen
)
2785 if (chan
->tech
->send_html
)
2786 return chan
->tech
->send_html(chan
, subclass
, data
, datalen
);
2790 int ast_channel_sendurl(struct ast_channel
*chan
, const char *url
)
2792 return ast_channel_sendhtml(chan
, AST_HTML_URL
, url
, strlen(url
) + 1);
2795 int ast_channel_make_compatible(struct ast_channel
*chan
, struct ast_channel
*peer
)
2800 /* Set up translation from the chan to the peer */
2801 src
= chan
->nativeformats
;
2802 dst
= peer
->nativeformats
;
2803 if (ast_translator_best_choice(&dst
, &src
) < 0) {
2804 ast_log(LOG_WARNING
, "No path to translate from %s(%d) to %s(%d)\n", chan
->name
, src
, peer
->name
, dst
);
2808 /* if the best path is not 'pass through', then
2809 transcoding is needed; if desired, force transcode path
2810 to use SLINEAR between channels, but only if there is
2811 no direct conversion available */
2812 if ((src
!= dst
) && ast_opt_transcode_via_slin
&&
2813 (ast_translate_path_steps(dst
, src
) != 1))
2814 dst
= AST_FORMAT_SLINEAR
;
2815 if (ast_set_read_format(chan
, dst
) < 0) {
2816 ast_log(LOG_WARNING
, "Unable to set read format on channel %s to %d\n", chan
->name
, dst
);
2819 if (ast_set_write_format(peer
, dst
) < 0) {
2820 ast_log(LOG_WARNING
, "Unable to set write format on channel %s to %d\n", peer
->name
, dst
);
2824 /* Set up translation from the peer to the chan */
2825 src
= peer
->nativeformats
;
2826 dst
= chan
->nativeformats
;
2827 if (ast_translator_best_choice(&dst
, &src
) < 0) {
2828 ast_log(LOG_WARNING
, "No path to translate from %s(%d) to %s(%d)\n", peer
->name
, src
, chan
->name
, dst
);
2832 /* if the best path is not 'pass through', then
2833 transcoding is needed; if desired, force transcode path
2834 to use SLINEAR between channels, but only if there is
2835 no direct conversion available */
2836 if ((src
!= dst
) && ast_opt_transcode_via_slin
&&
2837 (ast_translate_path_steps(dst
, src
) != 1))
2838 dst
= AST_FORMAT_SLINEAR
;
2839 if (ast_set_read_format(peer
, dst
) < 0) {
2840 ast_log(LOG_WARNING
, "Unable to set read format on channel %s to %d\n", peer
->name
, dst
);
2843 if (ast_set_write_format(chan
, dst
) < 0) {
2844 ast_log(LOG_WARNING
, "Unable to set write format on channel %s to %d\n", chan
->name
, dst
);
2850 int ast_channel_masquerade(struct ast_channel
*original
, struct ast_channel
*clone
)
2854 /* each of these channels may be sitting behind a channel proxy (i.e. chan_agent)
2855 and if so, we don't really want to masquerade it, but its proxy */
2856 if (original
->_bridge
&& (original
->_bridge
!= ast_bridged_channel(original
)))
2857 original
= original
->_bridge
;
2859 if (clone
->_bridge
&& (clone
->_bridge
!= ast_bridged_channel(clone
)))
2860 clone
= clone
->_bridge
;
2862 if (original
== clone
) {
2863 ast_log(LOG_WARNING
, "Can't masquerade channel '%s' into itself!\n", original
->name
);
2866 ast_channel_lock(original
);
2867 while(ast_channel_trylock(clone
)) {
2868 ast_channel_unlock(original
);
2870 ast_channel_lock(original
);
2872 ast_log(LOG_DEBUG
, "Planning to masquerade channel %s into the structure of %s\n",
2873 clone
->name
, original
->name
);
2874 if (original
->masq
) {
2875 ast_log(LOG_WARNING
, "%s is already going to masquerade as %s\n",
2876 original
->masq
->name
, original
->name
);
2877 } else if (clone
->masqr
) {
2878 ast_log(LOG_WARNING
, "%s is already going to masquerade as %s\n",
2879 clone
->name
, clone
->masqr
->name
);
2881 original
->masq
= clone
;
2882 clone
->masqr
= original
;
2883 ast_queue_frame(original
, &ast_null_frame
);
2884 ast_queue_frame(clone
, &ast_null_frame
);
2885 ast_log(LOG_DEBUG
, "Done planning to masquerade channel %s into the structure of %s\n", clone
->name
, original
->name
);
2888 ast_channel_unlock(clone
);
2889 ast_channel_unlock(original
);
2893 void ast_change_name(struct ast_channel
*chan
, char *newname
)
2895 manager_event(EVENT_FLAG_CALL
, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", chan
->name
, newname
, chan
->uniqueid
);
2896 ast_string_field_set(chan
, name
, newname
);
2899 void ast_channel_inherit_variables(const struct ast_channel
*parent
, struct ast_channel
*child
)
2901 struct ast_var_t
*current
, *newvar
;
2902 const char *varname
;
2904 AST_LIST_TRAVERSE(&parent
->varshead
, current
, entries
) {
2907 varname
= ast_var_full_name(current
);
2911 if (varname
[0] == '_') {
2913 if (varname
[1] == '_')
2919 newvar
= ast_var_assign(&varname
[1], ast_var_value(current
));
2921 AST_LIST_INSERT_TAIL(&child
->varshead
, newvar
, entries
);
2923 ast_log(LOG_DEBUG
, "Copying soft-transferable variable %s.\n", ast_var_name(newvar
));
2927 newvar
= ast_var_assign(ast_var_full_name(current
), ast_var_value(current
));
2929 AST_LIST_INSERT_TAIL(&child
->varshead
, newvar
, entries
);
2931 ast_log(LOG_DEBUG
, "Copying hard-transferable variable %s.\n", ast_var_name(newvar
));
2936 ast_log(LOG_DEBUG
, "Not copying variable %s.\n", ast_var_name(current
));
2943 \brief Clone channel variables from 'clone' channel into 'original' channel
2945 All variables except those related to app_groupcount are cloned.
2946 Variables are actually _removed_ from 'clone' channel, presumably
2947 because it will subsequently be destroyed.
2949 \note Assumes locks will be in place on both channels when called.
2951 static void clone_variables(struct ast_channel
*original
, struct ast_channel
*clone
)
2953 struct ast_var_t
*varptr
;
2955 /* we need to remove all app_groupcount related variables from the original
2956 channel before merging in the clone's variables; any groups assigned to the
2957 original channel should be released, only those assigned to the clone
2961 AST_LIST_TRAVERSE_SAFE_BEGIN(&original
->varshead
, varptr
, entries
) {
2962 if (!strncmp(ast_var_name(varptr
), GROUP_CATEGORY_PREFIX
, strlen(GROUP_CATEGORY_PREFIX
))) {
2963 AST_LIST_REMOVE(&original
->varshead
, varptr
, entries
);
2964 ast_var_delete(varptr
);
2967 AST_LIST_TRAVERSE_SAFE_END
;
2969 /* Append variables from clone channel into original channel */
2970 /* XXX Is this always correct? We have to in order to keep MACROS working XXX */
2971 if (AST_LIST_FIRST(&clone
->varshead
))
2972 AST_LIST_INSERT_TAIL(&original
->varshead
, AST_LIST_FIRST(&clone
->varshead
), entries
);
2976 \brief Masquerade a channel
2978 \note Assumes channel will be locked when called
2980 int ast_do_masquerade(struct ast_channel
*original
)
2985 struct ast_frame
*cur
, *prev
;
2986 const struct ast_channel_tech
*t
;
2988 struct ast_callerid tmpcid
;
2989 struct ast_channel
*clone
= original
->masq
;
2990 int rformat
= original
->readformat
;
2991 int wformat
= original
->writeformat
;
2997 if (option_debug
> 3)
2998 ast_log(LOG_DEBUG
, "Actually Masquerading %s(%d) into the structure of %s(%d)\n",
2999 clone
->name
, clone
->_state
, original
->name
, original
->_state
);
3001 /* XXX This is a seriously wacked out operation. We're essentially putting the guts of
3002 the clone channel into the original channel. Start by killing off the original
3003 channel's backend. I'm not sure we're going to keep this function, because
3004 while the features are nice, the cost is very high in terms of pure nastiness. XXX */
3006 /* We need the clone's lock, too */
3007 ast_channel_lock(clone
);
3009 if (option_debug
> 1)
3010 ast_log(LOG_DEBUG
, "Got clone lock for masquerade on '%s' at %p\n", clone
->name
, &clone
->lock
);
3012 /* Having remembered the original read/write formats, we turn off any translation on either
3014 free_translation(clone
);
3015 free_translation(original
);
3018 /* Unlink the masquerade */
3019 original
->masq
= NULL
;
3020 clone
->masqr
= NULL
;
3022 /* Save the original name */
3023 ast_copy_string(orig
, original
->name
, sizeof(orig
));
3024 /* Save the new name */
3025 ast_copy_string(newn
, clone
->name
, sizeof(newn
));
3026 /* Create the masq name */
3027 snprintf(masqn
, sizeof(masqn
), "%s<MASQ>", newn
);
3029 /* Copy the name from the clone channel */
3030 ast_string_field_set(original
, name
, newn
);
3032 /* Mangle the name of the clone channel */
3033 ast_string_field_set(clone
, name
, masqn
);
3035 /* Notify any managers of the change, first the masq then the other */
3036 manager_event(EVENT_FLAG_CALL
, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", newn
, masqn
, clone
->uniqueid
);
3037 manager_event(EVENT_FLAG_CALL
, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", orig
, newn
, original
->uniqueid
);
3039 /* Swap the technologies */
3041 original
->tech
= clone
->tech
;
3044 t_pvt
= original
->tech_pvt
;
3045 original
->tech_pvt
= clone
->tech_pvt
;
3046 clone
->tech_pvt
= t_pvt
;
3048 /* Swap the readq's */
3049 cur
= original
->readq
;
3050 original
->readq
= clone
->readq
;
3053 /* Swap the alertpipes */
3054 for (i
= 0; i
< 2; i
++) {
3055 x
= original
->alertpipe
[i
];
3056 original
->alertpipe
[i
] = clone
->alertpipe
[i
];
3057 clone
->alertpipe
[i
] = x
;
3060 /* Swap the raw formats */
3061 x
= original
->rawreadformat
;
3062 original
->rawreadformat
= clone
->rawreadformat
;
3063 clone
->rawreadformat
= x
;
3064 x
= original
->rawwriteformat
;
3065 original
->rawwriteformat
= clone
->rawwriteformat
;
3066 clone
->rawwriteformat
= x
;
3068 /* Save any pending frames on both sides. Start by counting
3069 * how many we're going to need... */
3072 for (cur
= clone
->readq
; cur
; cur
= cur
->next
) {
3076 /* If we had any, prepend them to the ones already in the queue, and
3077 * load up the alertpipe */
3079 prev
->next
= original
->readq
;
3080 original
->readq
= clone
->readq
;
3081 clone
->readq
= NULL
;
3082 if (original
->alertpipe
[1] > -1) {
3083 for (i
= 0; i
< x
; i
++)
3084 write(original
->alertpipe
[1], &x
, sizeof(x
));
3087 clone
->_softhangup
= AST_SOFTHANGUP_DEV
;
3090 /* And of course, so does our current state. Note we need not
3091 call ast_setstate since the event manager doesn't really consider
3092 these separate. We do this early so that the clone has the proper
3093 state of the original channel. */
3094 origstate
= original
->_state
;
3095 original
->_state
= clone
->_state
;
3096 clone
->_state
= origstate
;
3098 if (clone
->tech
->fixup
){
3099 res
= clone
->tech
->fixup(original
, clone
);
3101 ast_log(LOG_WARNING
, "Fixup failed on channel %s, strange things may happen.\n", clone
->name
);
3104 /* Start by disconnecting the original's physical side */
3105 if (clone
->tech
->hangup
)
3106 res
= clone
->tech
->hangup(clone
);
3108 ast_log(LOG_WARNING
, "Hangup failed! Strange things may happen!\n");
3109 ast_channel_unlock(clone
);
3113 snprintf(zombn
, sizeof(zombn
), "%s<ZOMBIE>", orig
);
3114 /* Mangle the name of the clone channel */
3115 ast_string_field_set(clone
, name
, zombn
);
3116 manager_event(EVENT_FLAG_CALL
, "Rename", "Oldname: %s\r\nNewname: %s\r\nUniqueid: %s\r\n", masqn
, zombn
, clone
->uniqueid
);
3118 /* Update the type. */
3119 t_pvt
= original
->monitor
;
3120 original
->monitor
= clone
->monitor
;
3121 clone
->monitor
= t_pvt
;
3123 /* Keep the same language. */
3124 ast_string_field_set(original
, language
, clone
->language
);
3125 /* Copy the FD's other than the generator fd */
3126 for (x
= 0; x
< AST_MAX_FDS
; x
++) {
3127 if (x
!= AST_GENERATOR_FD
)
3128 original
->fds
[x
] = clone
->fds
[x
];
3130 /* Move data stores over */
3131 if (AST_LIST_FIRST(&clone
->datastores
))
3132 AST_LIST_INSERT_TAIL(&original
->datastores
, AST_LIST_FIRST(&clone
->datastores
), entry
);
3133 AST_LIST_HEAD_INIT_NOLOCK(&clone
->datastores
);
3135 clone_variables(original
, clone
);
3136 AST_LIST_HEAD_INIT_NOLOCK(&clone
->varshead
);
3137 /* Presense of ADSI capable CPE follows clone */
3138 original
->adsicpe
= clone
->adsicpe
;
3139 /* Bridge remains the same */
3140 /* CDR fields remain the same */
3141 /* XXX What about blocking, softhangup, blocker, and lock and blockproc? XXX */
3142 /* Application and data remain the same */
3143 /* Clone exception becomes real one, as with fdno */
3144 ast_copy_flags(original
, clone
, AST_FLAG_EXCEPTION
);
3145 original
->fdno
= clone
->fdno
;
3146 /* Schedule context remains the same */
3147 /* Stream stuff stays the same */
3148 /* Keep the original state. The fixup code will need to work with it most likely */
3150 /* Just swap the whole structures, nevermind the allocations, they'll work themselves
3152 tmpcid
= original
->cid
;
3153 original
->cid
= clone
->cid
;
3154 clone
->cid
= tmpcid
;
3156 /* Restore original timing file descriptor */
3157 original
->fds
[AST_TIMING_FD
] = original
->timingfd
;
3159 /* Our native formats are different now */
3160 original
->nativeformats
= clone
->nativeformats
;
3162 /* Context, extension, priority, app data, jump table, remain the same */
3163 /* pvt switches. pbx stays the same, as does next */
3165 /* Set the write format */
3166 ast_set_write_format(original
, wformat
);
3168 /* Set the read format */
3169 ast_set_read_format(original
, rformat
);
3171 /* Copy the music class */
3172 ast_string_field_set(original
, musicclass
, clone
->musicclass
);
3175 ast_log(LOG_DEBUG
, "Putting channel %s in %d/%d formats\n", original
->name
, wformat
, rformat
);
3177 /* Okay. Last thing is to let the channel driver know about all this mess, so he
3178 can fix up everything as best as possible */
3179 if (original
->tech
->fixup
) {
3180 res
= original
->tech
->fixup(clone
, original
);
3182 ast_log(LOG_WARNING
, "Channel for type '%s' could not fixup channel %s\n",
3183 original
->tech
->type
, original
->name
);
3184 ast_channel_unlock(clone
);
3188 ast_log(LOG_WARNING
, "Channel type '%s' does not have a fixup routine (for %s)! Bad things may happen.\n",
3189 original
->tech
->type
, original
->name
);
3191 /* Now, at this point, the "clone" channel is totally F'd up. We mark it as
3192 a zombie so nothing tries to touch it. If it's already been marked as a
3193 zombie, then free it now (since it already is considered invalid). */
3194 if (ast_test_flag(clone
, AST_FLAG_ZOMBIE
)) {
3196 ast_log(LOG_DEBUG
, "Destroying channel clone '%s'\n", clone
->name
);
3197 ast_channel_unlock(clone
);
3198 manager_event(EVENT_FLAG_CALL
, "Hangup",
3202 "Cause-txt: %s\r\n",
3206 ast_cause2str(clone
->hangupcause
)
3208 ast_channel_free(clone
);
3210 ast_log(LOG_DEBUG
, "Released clone lock on '%s'\n", clone
->name
);
3211 ast_set_flag(clone
, AST_FLAG_ZOMBIE
);
3212 ast_queue_frame(clone
, &ast_null_frame
);
3213 ast_channel_unlock(clone
);
3216 /* Signal any blocker */
3217 if (ast_test_flag(original
, AST_FLAG_BLOCKING
))
3218 pthread_kill(original
->blocker
, SIGURG
);
3220 ast_log(LOG_DEBUG
, "Done Masquerading %s (%d)\n", original
->name
, original
->_state
);
3224 void ast_set_callerid(struct ast_channel
*chan
, const char *callerid
, const char *calleridname
, const char *ani
)
3227 if (chan
->cid
.cid_num
)
3228 free(chan
->cid
.cid_num
);
3229 chan
->cid
.cid_num
= ast_strdup(callerid
);
3232 if (chan
->cid
.cid_name
)
3233 free(chan
->cid
.cid_name
);
3234 chan
->cid
.cid_name
= ast_strdup(calleridname
);
3237 if (chan
->cid
.cid_ani
)
3238 free(chan
->cid
.cid_ani
);
3239 chan
->cid
.cid_ani
= ast_strdup(ani
);
3242 ast_cdr_setcid(chan
->cdr
, chan
);
3243 manager_event(EVENT_FLAG_CALL
, "Newcallerid",
3246 "CallerIDName: %s\r\n"
3248 "CID-CallingPres: %d (%s)\r\n",
3250 S_OR(chan
->cid
.cid_num
, "<Unknown>"),
3251 S_OR(chan
->cid
.cid_name
, "<Unknown>"),
3254 ast_describe_caller_presentation(chan
->cid
.cid_pres
)
3258 int ast_setstate(struct ast_channel
*chan
, int state
)
3260 int oldstate
= chan
->_state
;
3262 if (oldstate
== state
)
3265 chan
->_state
= state
;
3266 ast_device_state_changed_literal(chan
->name
);
3267 manager_event(EVENT_FLAG_CALL
,
3268 (oldstate
== AST_STATE_DOWN
) ? "Newchannel" : "Newstate",
3272 "CallerIDName: %s\r\n"
3274 chan
->name
, ast_state2str(chan
->_state
),
3275 S_OR(chan
->cid
.cid_num
, "<unknown>"),
3276 S_OR(chan
->cid
.cid_name
, "<unknown>"),
3282 /*! \brief Find bridged channel */
3283 struct ast_channel
*ast_bridged_channel(struct ast_channel
*chan
)
3285 struct ast_channel
*bridged
;
3286 bridged
= chan
->_bridge
;
3287 if (bridged
&& bridged
->tech
->bridged_channel
)
3288 bridged
= bridged
->tech
->bridged_channel(chan
, bridged
);
3292 static void bridge_playfile(struct ast_channel
*chan
, struct ast_channel
*peer
, const char *sound
, int remain
)
3294 int min
= 0, sec
= 0, check
;
3296 check
= ast_autoservice_start(peer
);
3301 if (remain
/ 60 > 1) {
3309 if (!strcmp(sound
,"timeleft")) { /* Queue support */
3310 ast_stream_and_wait(chan
, "vm-youhave", chan
->language
, "");
3312 ast_say_number(chan
, min
, AST_DIGIT_ANY
, chan
->language
, NULL
);
3313 ast_stream_and_wait(chan
, "queue-minutes", chan
->language
, "");
3316 ast_say_number(chan
, sec
, AST_DIGIT_ANY
, chan
->language
, NULL
);
3317 ast_stream_and_wait(chan
, "queue-seconds", chan
->language
, "");
3320 ast_stream_and_wait(chan
, sound
, chan
->language
, "");
3323 ast_autoservice_stop(peer
);
3326 static enum ast_bridge_result
ast_generic_bridge(struct ast_channel
*c0
, struct ast_channel
*c1
,
3327 struct ast_bridge_config
*config
, struct ast_frame
**fo
,
3328 struct ast_channel
**rc
, struct timeval bridge_end
)
3330 /* Copy voice back and forth between the two channels. */
3331 struct ast_channel
*cs
[3];
3332 struct ast_frame
*f
;
3333 enum ast_bridge_result res
= AST_BRIDGE_COMPLETE
;
3334 int o0nativeformats
;
3335 int o1nativeformats
;
3339 /* Indicates whether a frame was queued into a jitterbuffer */
3340 int frame_put_in_jb
= 0;
3346 pvt0
= c0
->tech_pvt
;
3347 pvt1
= c1
->tech_pvt
;
3348 o0nativeformats
= c0
->nativeformats
;
3349 o1nativeformats
= c1
->nativeformats
;
3350 watch_c0_dtmf
= config
->flags
& AST_BRIDGE_DTMF_CHANNEL_0
;
3351 watch_c1_dtmf
= config
->flags
& AST_BRIDGE_DTMF_CHANNEL_1
;
3353 /* Check the need of a jitterbuffer for each channel */
3354 jb_in_use
= ast_jb_do_usecheck(c0
, c1
);
3357 struct ast_channel
*who
, *other
;
3359 if ((c0
->tech_pvt
!= pvt0
) || (c1
->tech_pvt
!= pvt1
) ||
3360 (o0nativeformats
!= c0
->nativeformats
) ||
3361 (o1nativeformats
!= c1
->nativeformats
)) {
3362 /* Check for Masquerade, codec changes, etc */
3363 res
= AST_BRIDGE_RETRY
;
3366 if (bridge_end
.tv_sec
) {
3367 to
= ast_tvdiff_ms(bridge_end
, ast_tvnow());
3369 res
= AST_BRIDGE_RETRY
;
3374 /* Calculate the appropriate max sleep interval - in general, this is the time,
3375 left to the closest jb delivery moment */
3377 to
= ast_jb_get_when_to_wakeup(c0
, c1
, to
);
3378 who
= ast_waitfor_n(cs
, 2, &to
);
3380 /* No frame received within the specified timeout - check if we have to deliver now */
3382 ast_jb_get_and_deliver(c0
, c1
);
3383 if (c0
->_softhangup
== AST_SOFTHANGUP_UNBRIDGE
|| c1
->_softhangup
== AST_SOFTHANGUP_UNBRIDGE
) {
3384 if (c0
->_softhangup
== AST_SOFTHANGUP_UNBRIDGE
)
3385 c0
->_softhangup
= 0;
3386 if (c1
->_softhangup
== AST_SOFTHANGUP_UNBRIDGE
)
3387 c1
->_softhangup
= 0;
3397 ast_log(LOG_DEBUG
, "Didn't get a frame from channel: %s\n",who
->name
);
3401 other
= (who
== c0
) ? c1
: c0
; /* the 'other' channel */
3402 /* Try add the frame info the who's bridged channel jitterbuff */
3404 frame_put_in_jb
= !ast_jb_put(other
, f
);
3406 if ((f
->frametype
== AST_FRAME_CONTROL
) && !(config
->flags
& AST_BRIDGE_IGNORE_SIGS
)) {
3407 int bridge_exit
= 0;
3409 switch (f
->subclass
) {
3410 case AST_CONTROL_HOLD
:
3411 case AST_CONTROL_UNHOLD
:
3412 case AST_CONTROL_VIDUPDATE
:
3413 ast_indicate_data(other
, f
->subclass
, f
->data
, f
->datalen
);
3419 ast_log(LOG_DEBUG
, "Got a FRAME_CONTROL (%d) frame on channel %s\n", f
->subclass
, who
->name
);
3425 if ((f
->frametype
== AST_FRAME_VOICE
) ||
3426 (f
->frametype
== AST_FRAME_DTMF
) ||
3427 (f
->frametype
== AST_FRAME_VIDEO
) ||
3428 (f
->frametype
== AST_FRAME_IMAGE
) ||
3429 (f
->frametype
== AST_FRAME_HTML
) ||
3430 (f
->frametype
== AST_FRAME_MODEM
) ||
3431 (f
->frametype
== AST_FRAME_TEXT
)) {
3432 /* monitored dtmf causes exit from bridge */
3433 int monitored_source
= (who
== c0
) ? watch_c0_dtmf
: watch_c1_dtmf
;
3435 if (f
->frametype
== AST_FRAME_DTMF
&& monitored_source
) {
3438 ast_log(LOG_DEBUG
, "Got DTMF on channel (%s)\n", who
->name
);
3441 /* Write immediately frames, not passed through jb */
3442 if (!frame_put_in_jb
)
3443 ast_write(other
, f
);
3445 /* Check if we have to deliver now */
3447 ast_jb_get_and_deliver(c0
, c1
);
3449 /* XXX do we want to pass on also frames not matched above ? */
3452 /* Swap who gets priority */
3460 /*! \brief Bridge two channels together */
3461 enum ast_bridge_result
ast_channel_bridge(struct ast_channel
*c0
, struct ast_channel
*c1
,
3462 struct ast_bridge_config
*config
, struct ast_frame
**fo
, struct ast_channel
**rc
)
3464 struct ast_channel
*who
= NULL
;
3465 enum ast_bridge_result res
= AST_BRIDGE_COMPLETE
;
3468 int o0nativeformats
;
3469 int o1nativeformats
;
3470 long time_left_ms
=0;
3471 struct timeval nexteventts
= { 0, };
3472 char caller_warning
= 0;
3473 char callee_warning
= 0;
3477 ast_log(LOG_WARNING
, "%s is already in a bridge with %s\n",
3478 c0
->name
, c0
->_bridge
->name
);
3482 ast_log(LOG_WARNING
, "%s is already in a bridge with %s\n",
3483 c1
->name
, c1
->_bridge
->name
);
3487 /* Stop if we're a zombie or need a soft hangup */
3488 if (ast_test_flag(c0
, AST_FLAG_ZOMBIE
) || ast_check_hangup_locked(c0
) ||
3489 ast_test_flag(c1
, AST_FLAG_ZOMBIE
) || ast_check_hangup_locked(c1
))
3493 firstpass
= config
->firstpass
;
3494 config
->firstpass
= 0;
3496 if (ast_tvzero(config
->start_time
))
3497 config
->start_time
= ast_tvnow();
3498 time_left_ms
= config
->timelimit
;
3500 caller_warning
= ast_test_flag(&config
->features_caller
, AST_FEATURE_PLAY_WARNING
);
3501 callee_warning
= ast_test_flag(&config
->features_callee
, AST_FEATURE_PLAY_WARNING
);
3503 if (config
->start_sound
&& firstpass
) {
3505 bridge_playfile(c0
, c1
, config
->start_sound
, time_left_ms
/ 1000);
3507 bridge_playfile(c1
, c0
, config
->start_sound
, time_left_ms
/ 1000);
3510 /* Keep track of bridge */
3514 /* \todo XXX here should check that cid_num is not NULL */
3515 manager_event(EVENT_FLAG_CALL
, "Link",
3521 "CallerID2: %s\r\n",
3522 c0
->name
, c1
->name
, c0
->uniqueid
, c1
->uniqueid
, c0
->cid
.cid_num
, c1
->cid
.cid_num
);
3524 o0nativeformats
= c0
->nativeformats
;
3525 o1nativeformats
= c1
->nativeformats
;
3527 if (config
->timelimit
) {
3528 nexteventts
= ast_tvadd(config
->start_time
, ast_samp2tv(config
->timelimit
, 1000));
3529 if (caller_warning
|| callee_warning
)
3530 nexteventts
= ast_tvsub(nexteventts
, ast_samp2tv(config
->play_warning
, 1000));
3533 for (/* ever */;;) {
3535 if (config
->timelimit
) {
3538 to
= ast_tvdiff_ms(nexteventts
, now
);
3541 time_left_ms
= config
->timelimit
- ast_tvdiff_ms(now
, config
->start_time
);
3542 if (time_left_ms
< to
)
3545 if (time_left_ms
<= 0) {
3546 if (caller_warning
&& config
->end_sound
)
3547 bridge_playfile(c0
, c1
, config
->end_sound
, 0);
3548 if (callee_warning
&& config
->end_sound
)
3549 bridge_playfile(c1
, c0
, config
->end_sound
, 0);
3558 if (time_left_ms
>= 5000 && config
->warning_sound
&& config
->play_warning
) {
3559 int t
= (time_left_ms
+ 500) / 1000; /* round to nearest second */
3561 bridge_playfile(c0
, c1
, config
->warning_sound
, t
);
3563 bridge_playfile(c1
, c0
, config
->warning_sound
, t
);
3565 if (config
->warning_freq
) {
3566 nexteventts
= ast_tvadd(nexteventts
, ast_samp2tv(config
->warning_freq
, 1000));
3568 nexteventts
= ast_tvadd(config
->start_time
, ast_samp2tv(config
->timelimit
, 1000));
3572 if (c0
->_softhangup
== AST_SOFTHANGUP_UNBRIDGE
|| c1
->_softhangup
== AST_SOFTHANGUP_UNBRIDGE
) {
3573 if (c0
->_softhangup
== AST_SOFTHANGUP_UNBRIDGE
)
3574 c0
->_softhangup
= 0;
3575 if (c1
->_softhangup
== AST_SOFTHANGUP_UNBRIDGE
)
3576 c1
->_softhangup
= 0;
3579 ast_log(LOG_DEBUG
, "Unbridge signal received. Ending native bridge.\n");
3583 /* Stop if we're a zombie or need a soft hangup */
3584 if (ast_test_flag(c0
, AST_FLAG_ZOMBIE
) || ast_check_hangup_locked(c0
) ||
3585 ast_test_flag(c1
, AST_FLAG_ZOMBIE
) || ast_check_hangup_locked(c1
)) {
3590 ast_log(LOG_DEBUG
, "Bridge stops because we're zombie or need a soft hangup: c0=%s, c1=%s, flags: %s,%s,%s,%s\n",
3592 ast_test_flag(c0
, AST_FLAG_ZOMBIE
) ? "Yes" : "No",
3593 ast_check_hangup(c0
) ? "Yes" : "No",
3594 ast_test_flag(c1
, AST_FLAG_ZOMBIE
) ? "Yes" : "No",
3595 ast_check_hangup(c1
) ? "Yes" : "No");
3599 if (c0
->tech
->bridge
&&
3600 (config
->timelimit
== 0) &&
3601 (c0
->tech
->bridge
== c1
->tech
->bridge
) &&
3602 !nativefailed
&& !c0
->monitor
&& !c1
->monitor
&&
3603 !c0
->spies
&& !c1
->spies
) {
3604 /* Looks like they share a bridge method and nothing else is in the way */
3605 ast_set_flag(c0
, AST_FLAG_NBRIDGE
);
3606 ast_set_flag(c1
, AST_FLAG_NBRIDGE
);
3607 if ((res
= c0
->tech
->bridge(c0
, c1
, config
->flags
, fo
, rc
, to
)) == AST_BRIDGE_COMPLETE
) {
3608 /* \todo XXX here should check that cid_num is not NULL */
3609 manager_event(EVENT_FLAG_CALL
, "Unlink",
3615 "CallerID2: %s\r\n",
3616 c0
->name
, c1
->name
, c0
->uniqueid
, c1
->uniqueid
, c0
->cid
.cid_num
, c1
->cid
.cid_num
);
3617 ast_log(LOG_DEBUG
, "Returning from native bridge, channels: %s, %s\n", c0
->name
, c1
->name
);
3619 ast_clear_flag(c0
, AST_FLAG_NBRIDGE
);
3620 ast_clear_flag(c1
, AST_FLAG_NBRIDGE
);
3622 if (c0
->_softhangup
== AST_SOFTHANGUP_UNBRIDGE
|| c1
->_softhangup
== AST_SOFTHANGUP_UNBRIDGE
)
3630 ast_clear_flag(c0
, AST_FLAG_NBRIDGE
);
3631 ast_clear_flag(c1
, AST_FLAG_NBRIDGE
);
3634 case AST_BRIDGE_RETRY
:
3637 if (option_verbose
> 2)
3638 ast_verbose(VERBOSE_PREFIX_3
"Native bridging %s and %s ended\n",
3639 c0
->name
, c1
->name
);
3641 case AST_BRIDGE_FAILED_NOWARN
:
3647 if (((c0
->writeformat
!= c1
->readformat
) || (c0
->readformat
!= c1
->writeformat
) ||
3648 (c0
->nativeformats
!= o0nativeformats
) || (c1
->nativeformats
!= o1nativeformats
)) &&
3649 !(c0
->generator
|| c1
->generator
)) {
3650 if (ast_channel_make_compatible(c0
, c1
)) {
3651 ast_log(LOG_WARNING
, "Can't make %s and %s compatible\n", c0
->name
, c1
->name
);
3652 /* \todo XXX here should check that cid_num is not NULL */
3653 manager_event(EVENT_FLAG_CALL
, "Unlink",
3659 "CallerID2: %s\r\n",
3660 c0
->name
, c1
->name
, c0
->uniqueid
, c1
->uniqueid
, c0
->cid
.cid_num
, c1
->cid
.cid_num
);
3661 return AST_BRIDGE_FAILED
;
3663 o0nativeformats
= c0
->nativeformats
;
3664 o1nativeformats
= c1
->nativeformats
;
3666 res
= ast_generic_bridge(c0
, c1
, config
, fo
, rc
, nexteventts
);
3667 if (res
!= AST_BRIDGE_RETRY
)
3674 /* \todo XXX here should check that cid_num is not NULL */
3675 manager_event(EVENT_FLAG_CALL
, "Unlink",
3681 "CallerID2: %s\r\n",
3682 c0
->name
, c1
->name
, c0
->uniqueid
, c1
->uniqueid
, c0
->cid
.cid_num
, c1
->cid
.cid_num
);
3683 ast_log(LOG_DEBUG
, "Bridge stops bridging channels %s and %s\n", c0
->name
, c1
->name
);
3688 /*! \brief Sets an option on a channel */
3689 int ast_channel_setoption(struct ast_channel
*chan
, int option
, void *data
, int datalen
, int block
)
3693 if (chan
->tech
->setoption
) {
3694 res
= chan
->tech
->setoption(chan
, option
, data
, datalen
);
3702 /* XXX Implement blocking -- just wait for our option frame reply, discarding
3703 intermediate packets. XXX */
3704 ast_log(LOG_ERROR
, "XXX Blocking not implemented yet XXX\n");
3710 struct tonepair_def
{
3717 struct tonepair_state
{
3725 unsigned char offset
[AST_FRIENDLY_OFFSET
];
3729 static void tonepair_release(struct ast_channel
*chan
, void *params
)
3731 struct tonepair_state
*ts
= params
;
3734 ast_set_write_format(chan
, ts
->origwfmt
);
3738 static void *tonepair_alloc(struct ast_channel
*chan
, void *params
)
3740 struct tonepair_state
*ts
;
3741 struct tonepair_def
*td
= params
;
3743 if (!(ts
= ast_calloc(1, sizeof(*ts
))))
3745 ts
->origwfmt
= chan
->writeformat
;
3746 if (ast_set_write_format(chan
, AST_FORMAT_SLINEAR
)) {
3747 ast_log(LOG_WARNING
, "Unable to set '%s' to signed linear format (write)\n", chan
->name
);
3748 tonepair_release(NULL
, ts
);
3751 ts
->freq1
= td
->freq1
;
3752 ts
->freq2
= td
->freq2
;
3753 ts
->duration
= td
->duration
;
3756 /* Let interrupts interrupt :) */
3757 ast_set_flag(chan
, AST_FLAG_WRITE_INT
);
3761 static int tonepair_generator(struct ast_channel
*chan
, void *data
, int len
, int samples
)
3763 struct tonepair_state
*ts
= data
;
3766 /* we need to prepare a frame with 16 * timelen samples as we're
3767 * generating SLIN audio
3771 if (len
> sizeof(ts
->data
) / 2 - 1) {
3772 ast_log(LOG_WARNING
, "Can't generate that much data!\n");
3775 memset(&ts
->f
, 0, sizeof(ts
->f
));
3776 for (x
= 0; x
< (len
/ 2); x
++) {
3777 ts
->data
[x
] = ts
->vol
* (
3778 sin((ts
->freq1
* 2.0 * M_PI
/ 8000.0) * (ts
->pos
+ x
)) +
3779 sin((ts
->freq2
* 2.0 * M_PI
/ 8000.0) * (ts
->pos
+ x
))
3782 ts
->f
.frametype
= AST_FRAME_VOICE
;
3783 ts
->f
.subclass
= AST_FORMAT_SLINEAR
;
3784 ts
->f
.datalen
= len
;
3785 ts
->f
.samples
= samples
;
3786 ts
->f
.offset
= AST_FRIENDLY_OFFSET
;
3787 ts
->f
.data
= ts
->data
;
3788 ast_write(chan
, &ts
->f
);
3790 if (ts
->duration
> 0) {
3791 if (ts
->pos
>= ts
->duration
* 8)
3797 static struct ast_generator tonepair
= {
3798 alloc
: tonepair_alloc
,
3799 release
: tonepair_release
,
3800 generate
: tonepair_generator
,
3803 int ast_tonepair_start(struct ast_channel
*chan
, int freq1
, int freq2
, int duration
, int vol
)
3805 struct tonepair_def d
= { 0, };
3809 d
.duration
= duration
;
3810 d
.vol
= (vol
< 1) ? 8192 : vol
; /* force invalid to 8192 */
3811 if (ast_activate_generator(chan
, &tonepair
, &d
))
3816 void ast_tonepair_stop(struct ast_channel
*chan
)
3818 ast_deactivate_generator(chan
);
3821 int ast_tonepair(struct ast_channel
*chan
, int freq1
, int freq2
, int duration
, int vol
)
3825 if ((res
= ast_tonepair_start(chan
, freq1
, freq2
, duration
, vol
)))
3828 /* Give us some wiggle room */
3829 while (chan
->generatordata
&& ast_waitfor(chan
, 100) >= 0) {
3830 struct ast_frame
*f
= ast_read(chan
);
3839 ast_group_t
ast_get_group(char *s
)
3843 int start
=0, finish
=0, x
;
3844 ast_group_t group
= 0;
3848 while ((piece
= strsep(&c
, ","))) {
3849 if (sscanf(piece
, "%d-%d", &start
, &finish
) == 2) {
3851 } else if (sscanf(piece
, "%d", &start
)) {
3855 ast_log(LOG_ERROR
, "Syntax error parsing group configuration '%s' at '%s'. Ignoring.\n", s
, piece
);
3858 for (x
= start
; x
<= finish
; x
++) {
3859 if ((x
> 63) || (x
< 0)) {
3860 ast_log(LOG_WARNING
, "Ignoring invalid group %d (maximum group is 63)\n", x
);
3862 group
|= ((ast_group_t
) 1 << x
);
3868 static int (*ast_moh_start_ptr
)(struct ast_channel
*, const char *) = NULL
;
3869 static void (*ast_moh_stop_ptr
)(struct ast_channel
*) = NULL
;
3870 static void (*ast_moh_cleanup_ptr
)(struct ast_channel
*) = NULL
;
3872 void ast_install_music_functions(int (*start_ptr
)(struct ast_channel
*, const char *),
3873 void (*stop_ptr
)(struct ast_channel
*),
3874 void (*cleanup_ptr
)(struct ast_channel
*))
3876 ast_moh_start_ptr
= start_ptr
;
3877 ast_moh_stop_ptr
= stop_ptr
;
3878 ast_moh_cleanup_ptr
= cleanup_ptr
;
3881 void ast_uninstall_music_functions(void)
3883 ast_moh_start_ptr
= NULL
;
3884 ast_moh_stop_ptr
= NULL
;
3885 ast_moh_cleanup_ptr
= NULL
;
3888 /*! \brief Turn on music on hold on a given channel */
3889 int ast_moh_start(struct ast_channel
*chan
, const char *mclass
)
3891 if (ast_moh_start_ptr
)
3892 return ast_moh_start_ptr(chan
, mclass
);
3894 if (option_verbose
> 2)
3895 ast_verbose(VERBOSE_PREFIX_3
"Music class %s requested but no musiconhold loaded.\n", mclass
? mclass
: "default");
3900 /*! \brief Turn off music on hold on a given channel */
3901 void ast_moh_stop(struct ast_channel
*chan
)
3903 if (ast_moh_stop_ptr
)
3904 ast_moh_stop_ptr(chan
);
3907 void ast_moh_cleanup(struct ast_channel
*chan
)
3909 if (ast_moh_cleanup_ptr
)
3910 ast_moh_cleanup_ptr(chan
);
3913 void ast_channels_init(void)
3915 ast_cli_register(&cli_show_channeltypes
);
3916 ast_cli_register(&cli_show_channeltype
);
3919 /*! \brief Print call group and pickup group ---*/
3920 char *ast_print_group(char *buf
, int buflen
, ast_group_t group
)
3928 if (!group
) /* Return empty string if no group */
3931 for (i
= 0; i
<= 63; i
++) { /* Max group is 63 */
3932 if (group
& ((ast_group_t
) 1 << i
)) {
3934 strncat(buf
, ", ", buflen
);
3938 snprintf(num
, sizeof(num
), "%u", i
);
3939 strncat(buf
, num
, buflen
);
3945 void ast_set_variables(struct ast_channel
*chan
, struct ast_variable
*vars
)
3947 struct ast_variable
*cur
;
3949 for (cur
= vars
; cur
; cur
= cur
->next
)
3950 pbx_builtin_setvar_helper(chan
, cur
->name
, cur
->value
);
3953 static void copy_data_from_queue(struct ast_channel_spy_queue
*queue
, short *buf
, unsigned int samples
)
3955 struct ast_frame
*f
;
3963 ast_log(LOG_ERROR
, "Ran out of frames before buffer filled!\n");
3967 tocopy
= (f
->samples
> samples
) ? samples
: f
->samples
;
3968 bytestocopy
= ast_codec_get_len(queue
->format
, tocopy
);
3969 memcpy(buf
, f
->data
, bytestocopy
);
3972 f
->samples
-= tocopy
;
3973 f
->data
+= bytestocopy
;
3974 f
->datalen
-= bytestocopy
;
3975 f
->offset
+= bytestocopy
;
3976 queue
->samples
-= tocopy
;
3978 queue
->head
= f
->next
;
3984 struct ast_frame
*ast_channel_spy_read_frame(struct ast_channel_spy
*spy
, unsigned int samples
)
3986 struct ast_frame
*result
;
3987 /* buffers are allocated to hold SLINEAR, which is the largest format */
3988 short read_buf
[samples
];
3989 short write_buf
[samples
];
3990 struct ast_frame
*read_frame
;
3991 struct ast_frame
*write_frame
;
3993 struct ast_frame stack_read_frame
= { .frametype
= AST_FRAME_VOICE
,
3994 .subclass
= spy
->read_queue
.format
,
3997 .datalen
= ast_codec_get_len(spy
->read_queue
.format
, samples
),
3999 struct ast_frame stack_write_frame
= { .frametype
= AST_FRAME_VOICE
,
4000 .subclass
= spy
->write_queue
.format
,
4003 .datalen
= ast_codec_get_len(spy
->write_queue
.format
, samples
),
4006 /* if a flush has been requested, dump everything in whichever queue is larger */
4007 if (ast_test_flag(spy
, CHANSPY_TRIGGER_FLUSH
)) {
4008 if (spy
->read_queue
.samples
> spy
->write_queue
.samples
) {
4009 if (ast_test_flag(spy
, CHANSPY_READ_VOLADJUST
)) {
4010 for (result
= spy
->read_queue
.head
; result
; result
= result
->next
)
4011 ast_frame_adjust_volume(result
, spy
->read_vol_adjustment
);
4013 result
= spy
->read_queue
.head
;
4014 spy
->read_queue
.head
= NULL
;
4015 spy
->read_queue
.samples
= 0;
4017 if (ast_test_flag(spy
, CHANSPY_WRITE_VOLADJUST
)) {
4018 for (result
= spy
->write_queue
.head
; result
; result
= result
->next
)
4019 ast_frame_adjust_volume(result
, spy
->write_vol_adjustment
);
4021 result
= spy
->write_queue
.head
;
4022 spy
->write_queue
.head
= NULL
;
4023 spy
->write_queue
.samples
= 0;
4025 ast_clear_flag(spy
, CHANSPY_TRIGGER_FLUSH
);
4029 if ((spy
->read_queue
.samples
< samples
) || (spy
->write_queue
.samples
< samples
))
4032 /* short-circuit if both head frames have exactly what we want */
4033 if ((spy
->read_queue
.head
->samples
== samples
) &&
4034 (spy
->write_queue
.head
->samples
== samples
)) {
4035 read_frame
= spy
->read_queue
.head
;
4036 spy
->read_queue
.head
= read_frame
->next
;
4037 read_frame
->next
= NULL
;
4039 write_frame
= spy
->write_queue
.head
;
4040 spy
->write_queue
.head
= write_frame
->next
;
4041 write_frame
->next
= NULL
;
4043 spy
->read_queue
.samples
-= samples
;
4044 spy
->write_queue
.samples
-= samples
;
4048 copy_data_from_queue(&spy
->read_queue
, read_buf
, samples
);
4049 copy_data_from_queue(&spy
->write_queue
, write_buf
, samples
);
4051 read_frame
= &stack_read_frame
;
4052 write_frame
= &stack_write_frame
;
4056 if (ast_test_flag(spy
, CHANSPY_READ_VOLADJUST
))
4057 ast_frame_adjust_volume(read_frame
, spy
->read_vol_adjustment
);
4059 if (ast_test_flag(spy
, CHANSPY_WRITE_VOLADJUST
))
4060 ast_frame_adjust_volume(write_frame
, spy
->write_vol_adjustment
);
4062 if (ast_test_flag(spy
, CHANSPY_MIXAUDIO
)) {
4063 ast_frame_slinear_sum(read_frame
, write_frame
);
4066 result
= ast_frdup(read_frame
);
4068 result
= read_frame
;
4069 ast_frfree(write_frame
);
4073 result
= ast_frdup(read_frame
);
4074 result
->next
= ast_frdup(write_frame
);
4076 result
= read_frame
;
4077 result
->next
= write_frame
;
4084 static void *silence_generator_alloc(struct ast_channel
*chan
, void *data
)
4086 /* just store the data pointer in the channel structure */
4090 static void silence_generator_release(struct ast_channel
*chan
, void *data
)
4095 static int silence_generator_generate(struct ast_channel
*chan
, void *data
, int len
, int samples
)
4098 struct ast_frame frame
= {
4099 .frametype
= AST_FRAME_VOICE
,
4100 .subclass
= AST_FORMAT_SLINEAR
,
4103 .datalen
= sizeof(buf
),
4105 memset(buf
, 0, sizeof(buf
));
4106 if (ast_write(chan
, &frame
))
4111 static struct ast_generator silence_generator
= {
4112 .alloc
= silence_generator_alloc
,
4113 .release
= silence_generator_release
,
4114 .generate
= silence_generator_generate
,
4117 struct ast_silence_generator
{
4118 int old_write_format
;
4121 struct ast_silence_generator
*ast_channel_start_silence_generator(struct ast_channel
*chan
)
4123 struct ast_silence_generator
*state
;
4125 if (!(state
= ast_calloc(1, sizeof(*state
)))) {
4129 state
->old_write_format
= chan
->writeformat
;
4131 if (ast_set_write_format(chan
, AST_FORMAT_SLINEAR
) < 0) {
4132 ast_log(LOG_ERROR
, "Could not set write format to SLINEAR\n");
4137 ast_activate_generator(chan
, &silence_generator
, state
);
4140 ast_log(LOG_DEBUG
, "Started silence generator on '%s'\n", chan
->name
);
4145 void ast_channel_stop_silence_generator(struct ast_channel
*chan
, struct ast_silence_generator
*state
)
4150 ast_deactivate_generator(chan
);
4153 ast_log(LOG_DEBUG
, "Stopped silence generator on '%s'\n", chan
->name
);
4155 if (ast_set_write_format(chan
, state
->old_write_format
) < 0)
4156 ast_log(LOG_ERROR
, "Could not return write format to its original state\n");
4162 /*! \ brief Convert channel reloadreason (ENUM) to text string for manager event */
4163 const char *channelreloadreason2txt(enum channelreloadreason reason
)
4166 case CHANNEL_MODULE_LOAD
:
4167 return "LOAD (Channel module load)";
4169 case CHANNEL_MODULE_RELOAD
:
4170 return "RELOAD (Channel module reload)";
4172 case CHANNEL_CLI_RELOAD
:
4173 return "CLIRELOAD (Channel module reload by CLI command)";
4176 return "MANAGERRELOAD (Channel module reload by manager)";
4180 #ifdef DEBUG_CHANNEL_LOCKS
4182 /*! \brief Unlock AST channel (and print debugging output)
4183 \note You need to enable DEBUG_CHANNEL_LOCKS for this function
4185 int ast_channel_unlock(struct ast_channel
*chan
)
4188 if (option_debug
> 2)
4189 ast_log(LOG_DEBUG
, "::::==== Unlocking AST channel %s\n", chan
->name
);
4192 ast_log(LOG_DEBUG
, "::::==== Unlocking non-existing channel \n");
4196 res
= ast_mutex_unlock(&chan
->lock
);
4198 if (option_debug
> 2) {
4199 /* Try to find counter if possible on your platform
4200 I've only found out how to do this on Linux
4201 DEBUG_THREADS changes the lock structure
4205 #ifdef DEBUG_THREADS
4206 if ((count
= chan
->lock
.mutex
.__m_count
))
4208 if ((count
= chan
->lock
.__m_count
))
4210 ast_log(LOG_DEBUG
, ":::=== Still have %d locks (recursive)\n", count
);
4213 ast_log(LOG_DEBUG
, "::::==== Channel %s was unlocked\n", chan
->name
);
4214 if (res
== EINVAL
) {
4215 ast_log(LOG_DEBUG
, "::::==== Channel %s had no lock by this thread. Failed unlocking\n", chan
->name
);
4219 /* We had no lock, so okay any way*/
4220 if (option_debug
> 3)
4221 ast_log(LOG_DEBUG
, "::::==== Channel %s was not locked at all \n", chan
->name
);
4227 /*! \brief Lock AST channel (and print debugging output)
4228 \note You need to enable DEBUG_CHANNEL_LOCKS for this function */
4229 int ast_channel_lock(struct ast_channel
*chan
)
4233 if (option_debug
> 3)
4234 ast_log(LOG_DEBUG
, "====:::: Locking AST channel %s\n", chan
->name
);
4236 res
= ast_mutex_lock(&chan
->lock
);
4238 if (option_debug
> 3) {
4241 #ifdef DEBUG_THREADS
4242 if ((count
= chan
->lock
.mutex
.__m_count
))
4244 if ((count
= chan
->lock
.__m_count
))
4246 ast_log(LOG_DEBUG
, ":::=== Now have %d locks (recursive)\n", count
);
4249 ast_log(LOG_DEBUG
, "::::==== Channel %s was locked\n", chan
->name
);
4250 if (res
== EDEADLK
) {
4251 /* We had no lock, so okey any way */
4252 if (option_debug
> 3)
4253 ast_log(LOG_DEBUG
, "::::==== Channel %s was not locked by us. Lock would cause deadlock.\n", chan
->name
);
4255 if (res
== EINVAL
) {
4256 if (option_debug
> 3)
4257 ast_log(LOG_DEBUG
, "::::==== Channel %s lock failed. No mutex.\n", chan
->name
);
4263 /*! \brief Lock AST channel (and print debugging output)
4264 \note You need to enable DEBUG_CHANNEL_LOCKS for this function */
4265 int ast_channel_trylock(struct ast_channel
*chan
)
4269 if (option_debug
> 2)
4270 ast_log(LOG_DEBUG
, "====:::: Trying to lock AST channel %s\n", chan
->name
);
4272 res
= ast_mutex_trylock(&chan
->lock
);
4274 if (option_debug
> 2) {
4277 #ifdef DEBUG_THREADS
4278 if ((count
= chan
->lock
.mutex
.__m_count
))
4280 if ((count
= chan
->lock
.__m_count
))
4282 ast_log(LOG_DEBUG
, ":::=== Now have %d locks (recursive)\n", count
);
4285 ast_log(LOG_DEBUG
, "::::==== Channel %s was locked\n", chan
->name
);
4287 /* We failed to lock */
4288 if (option_debug
> 2)
4289 ast_log(LOG_DEBUG
, "::::==== Channel %s failed to lock. Not waiting around...\n", chan
->name
);
4291 if (res
== EDEADLK
) {
4292 /* We had no lock, so okey any way*/
4293 if (option_debug
> 2)
4294 ast_log(LOG_DEBUG
, "::::==== Channel %s was not locked. Lock would cause deadlock.\n", chan
->name
);
4296 if (res
== EINVAL
&& option_debug
> 2)
4297 ast_log(LOG_DEBUG
, "::::==== Channel %s lock failed. No mutex.\n", chan
->name
);
4305 * Wrappers for various ast_say_*() functions that call the full version
4306 * of the same functions.
4307 * The proper place would be say.c, but that file is optional and one
4308 * must be able to build asterisk even without it (using a loadable 'say'
4309 * implementation that only supplies the 'full' version of the functions.
4312 int ast_say_number(struct ast_channel
*chan
, int num
,
4313 const char *ints
, const char *language
, const char *options
)
4315 return ast_say_number_full(chan
, num
, ints
, language
, options
, -1, -1);
4318 int ast_say_enumeration(struct ast_channel
*chan
, int num
,
4319 const char *ints
, const char *language
, const char *options
)
4321 return ast_say_enumeration_full(chan
, num
, ints
, language
, options
, -1, -1);
4324 int ast_say_digits(struct ast_channel
*chan
, int num
,
4325 const char *ints
, const char *lang
)
4327 return ast_say_digits_full(chan
, num
, ints
, lang
, -1, -1);
4330 int ast_say_digit_str(struct ast_channel
*chan
, const char *str
,
4331 const char *ints
, const char *lang
)
4333 return ast_say_digit_str_full(chan
, str
, ints
, lang
, -1, -1);
4336 int ast_say_character_str(struct ast_channel
*chan
, const char *str
,
4337 const char *ints
, const char *lang
)
4339 return ast_say_character_str_full(chan
, str
, ints
, lang
, -1, -1);
4342 int ast_say_phonetic_str(struct ast_channel
*chan
, const char *str
,
4343 const char *ints
, const char *lang
)
4345 return ast_say_phonetic_str_full(chan
, str
, ints
, lang
, -1, -1);
4348 int ast_say_digits_full(struct ast_channel
*chan
, int num
,
4349 const char *ints
, const char *lang
, int audiofd
, int ctrlfd
)
4353 snprintf(buf
, sizeof(buf
), "%d", num
);
4354 return ast_say_digit_str_full(chan
, buf
, ints
, lang
, audiofd
, ctrlfd
);