diff --git a/include/asterisk/channel.h b/include/asterisk/channel.h index a76e606f9e..c9e537fc32 100644 --- a/include/asterisk/channel.h +++ b/include/asterisk/channel.h @@ -966,6 +966,16 @@ enum { * The channel is executing a subroutine or macro */ AST_FLAG_SUBROUTINE_EXEC = (1 << 27), + /*! + * The channel is currently in an operation where + * frames should be deferred. + */ + AST_FLAG_DEFER_FRAMES = (1 << 28), + /*! + * The channel is currently deferring hangup frames + * in addition to other frame types. + */ + AST_FLAG_DEFER_HANGUP_FRAMES = (1 << 29), }; /*! \brief ast_bridge_config flags */ @@ -4681,4 +4691,43 @@ enum ast_channel_error { */ enum ast_channel_error ast_channel_errno(void); +/*! + * \brief Retrieve the deferred read queue. + */ +struct ast_readq_list *ast_channel_deferred_readq(struct ast_channel *chan); + +/*! + * \brief Start deferring deferrable frames on this channel + * + * Sometimes, a channel gets entered into a mode where a "main" application + * is tasked with servicing frames on the channel, but that application does + * not need to act on those frames. However, it would be imprudent to simply + * drop important frames. This function can be called so that important frames + * will be deferred, rather than placed in the channel frame queue as normal. + * + * Hangups are an interesting frame type. Hangups will always be detectable by + * a reader when a channel is deferring frames. If the defer_hangups parameter + * is non-zero, then the hangup frame will also be duplicated and deferred, so + * that the next reader of the channel will get the hangup frame, too. + * + * \pre chan MUST be locked before calling + * + * \param chan The channel on which frames should be deferred + * \param defer_hangups Defer hangups in addition to other deferrable frames + */ +void ast_channel_start_defer_frames(struct ast_channel *chan, int defer_hangups); + +/*! + * \brief Stop deferring deferrable frames on this channel + * + * When it is time to stop deferring frames on the channel, all deferred frames + * will be queued onto the channel's read queue so that the next servicer of + * the channel can handle those frames as necessary. + * + * \pre chan MUST be locked before calling + * + * \param chan The channel on which to stop deferring frames. + */ +void ast_channel_stop_defer_frames(struct ast_channel *chan); + #endif /* _ASTERISK_CHANNEL_H */ diff --git a/main/autoservice.c b/main/autoservice.c index 305ab23b10..e6eb1ef55e 100644 --- a/main/autoservice.c +++ b/main/autoservice.c @@ -220,6 +220,7 @@ int ast_autoservice_start(struct ast_channel *chan) as->orig_end_dtmf_flag = ast_test_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY) ? 1 : 0; if (!as->orig_end_dtmf_flag) ast_set_flag(ast_channel_flags(chan), AST_FLAG_END_DTMF_ONLY); + ast_channel_start_defer_frames(chan, 1); ast_channel_unlock(chan); AST_LIST_LOCK(&aslist); diff --git a/main/channel.c b/main/channel.c index 278104cc98..0a7865df12 100644 --- a/main/channel.c +++ b/main/channel.c @@ -1061,6 +1061,26 @@ struct ast_channel *ast_dummy_channel_alloc(void) return tmp; } +void ast_channel_start_defer_frames(struct ast_channel *chan, int defer_hangups) +{ + ast_set_flag(ast_channel_flags(chan), AST_FLAG_DEFER_FRAMES); + ast_set2_flag(ast_channel_flags(chan), defer_hangups, AST_FLAG_DEFER_HANGUP_FRAMES); +} + +void ast_channel_stop_defer_frames(struct ast_channel *chan) +{ + ast_clear_flag(ast_channel_flags(chan), AST_FLAG_DEFER_FRAMES); + + /* Move the deferred frames onto the channel read queue, ahead of other queued frames */ + ast_queue_frame_head(chan, AST_LIST_FIRST(ast_channel_deferred_readq(chan))); + /* ast_frfree will mosey down the list and free them all */ + if (!AST_LIST_EMPTY(ast_channel_deferred_readq(chan))) { + ast_frfree(AST_LIST_FIRST(ast_channel_deferred_readq(chan))); + } + /* Reset the list to be empty */ + AST_LIST_HEAD_INIT_NOLOCK(ast_channel_deferred_readq(chan)); +} + static int __ast_queue_frame(struct ast_channel *chan, struct ast_frame *fin, int head, struct ast_frame *after) { struct ast_frame *f; @@ -1533,6 +1553,10 @@ int ast_safe_sleep_conditional(struct ast_channel *chan, int timeout_ms, int (*c silgen = ast_channel_start_silence_generator(chan); } + ast_channel_lock(chan); + ast_channel_start_defer_frames(chan, 0); + ast_channel_unlock(chan); + start = ast_tvnow(); while ((ms = ast_remaining_ms(start, timeout_ms))) { struct ast_frame *dup_f = NULL; @@ -3885,6 +3909,36 @@ static struct ast_frame *__ast_read(struct ast_channel *chan, int dropaudio) if (!AST_LIST_EMPTY(ast_channel_readq(chan))) { int skip_dtmf = should_skip_dtmf(chan); + if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_FRAMES)) { + AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_readq(chan), f, frame_list) { + if (ast_is_deferrable_frame(f)) { + if(f->frametype == AST_FRAME_CONTROL && + (f->subclass.integer == AST_CONTROL_HANGUP || + f->subclass.integer == AST_CONTROL_END_OF_Q)) { + /* Hangup is a special case. We want to defer the frame, but we also do not + * want to remove it from the frame queue. So rather than just moving the frame + * over, we duplicate it and move the copy to the deferred readq. + * + * The reason for this? This way, whoever calls ast_read() will get a NULL return + * immediately and can tell the channel has hung up and do what it needs to. Also, + * when frame deferral finishes, then whoever calls ast_read() next will also get + * the hangup. + */ + if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_DEFER_HANGUP_FRAMES)) { + struct ast_frame *dup; + + dup = ast_frdup(f); + AST_LIST_INSERT_TAIL(ast_channel_deferred_readq(chan), dup, frame_list); + } + } else { + AST_LIST_INSERT_TAIL(ast_channel_deferred_readq(chan), f, frame_list); + AST_LIST_REMOVE_CURRENT(frame_list); + } + } + } + AST_LIST_TRAVERSE_SAFE_END; + } + AST_LIST_TRAVERSE_SAFE_BEGIN(ast_channel_readq(chan), f, frame_list) { /* We have to be picky about which frame we pull off of the readq because * there are cases where we want to leave DTMF frames on the queue until @@ -10216,6 +10270,7 @@ int ast_channel_connected_line_macro(struct ast_channel *autoservice_chan, struc ast_party_connected_line_copy(ast_channel_connected(macro_chan), connected); } + ast_channel_start_defer_frames(macro_chan, 0); ast_channel_unlock(macro_chan); retval = ast_app_run_macro(autoservice_chan, macro_chan, macro, macro_args); @@ -10266,6 +10321,7 @@ int ast_channel_redirecting_macro(struct ast_channel *autoservice_chan, struct a ast_party_redirecting_copy(ast_channel_redirecting(macro_chan), redirecting); } + ast_channel_start_defer_frames(macro_chan, 0); ast_channel_unlock(macro_chan); retval = ast_app_run_macro(autoservice_chan, macro_chan, macro, macro_args); @@ -10309,6 +10365,7 @@ int ast_channel_connected_line_sub(struct ast_channel *autoservice_chan, struct ast_party_connected_line_copy(ast_channel_connected(sub_chan), connected); } + ast_channel_start_defer_frames(sub_chan, 0); ast_channel_unlock(sub_chan); retval = ast_app_run_sub(autoservice_chan, sub_chan, sub, sub_args, 0); @@ -10352,6 +10409,7 @@ int ast_channel_redirecting_sub(struct ast_channel *autoservice_chan, struct ast ast_party_redirecting_copy(ast_channel_redirecting(sub_chan), redirecting); } + ast_channel_start_defer_frames(sub_chan, 0); ast_channel_unlock(sub_chan); retval = ast_app_run_sub(autoservice_chan, sub_chan, sub, sub_args, 0);