Index: apps/app_chanspy.c =================================================================== --- apps/app_chanspy.c (revision 184672) +++ apps/app_chanspy.c (working copy) @@ -215,6 +215,8 @@ ast_log(LOG_NOTICE, "Attaching %s to %s\n", spychan_name, chan->name); + ast_set_flag(audiohook, AST_AUDIOHOOK_TRIGGER_SYNC | AST_AUDIOHOOK_SMALL_QUEUE); + res = ast_audiohook_attach(chan, audiohook); if (!res && ast_test_flag(chan, AST_FLAG_NBRIDGE) && (peer = ast_bridged_channel(chan))) { Index: include/asterisk/audiohook.h =================================================================== --- include/asterisk/audiohook.h (revision 184672) +++ include/asterisk/audiohook.h (working copy) @@ -54,6 +54,10 @@ AST_AUDIOHOOK_TRIGGER_WRITE = (2 << 0), /*!< Audiohook wants to be triggered when writing audio out */ AST_AUDIOHOOK_WANTS_DTMF = (1 << 1), /*!< Audiohook also wants to receive DTMF frames */ AST_AUDIOHOOK_TRIGGER_SYNC = (1 << 2), /*!< Audiohook wants to be triggered when both sides have combined audio available */ + /*! Audiohooks with this flag set will not allow for a large amount of samples to build up on its + * slinfactories. We will flush the factories if they contain too many samples. + */ + AST_AUDIOHOOK_SMALL_QUEUE = (1 << 3), }; #define AST_AUDIOHOOK_SYNC_TOLERANCE 100 /*< Tolerance in milliseconds for audiohooks synchronization */ Index: main/audiohook.c =================================================================== --- main/audiohook.c (revision 184672) +++ main/audiohook.c (working copy) @@ -130,6 +130,7 @@ struct ast_slinfactory *factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_factory : &audiohook->write_factory); struct ast_slinfactory *other_factory = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->write_factory : &audiohook->read_factory); struct timeval *time = (direction == AST_AUDIOHOOK_DIRECTION_READ ? &audiohook->read_time : &audiohook->write_time), previous_time = *time; + int our_factory_samples; int our_factory_ms; int other_factory_samples; int other_factory_ms; @@ -137,7 +138,8 @@ /* Update last feeding time to be current */ *time = ast_tvnow(); - our_factory_ms = ast_tvdiff_ms(*time, previous_time) + (ast_slinfactory_available(factory) / 8); + our_factory_samples = ast_slinfactory_available(factory); + our_factory_ms = ast_tvdiff_ms(*time, previous_time) + (our_factory_samples / 8); other_factory_samples = ast_slinfactory_available(other_factory); other_factory_ms = other_factory_samples / 8; @@ -149,6 +151,14 @@ ast_slinfactory_flush(other_factory); } + if (ast_test_flag(audiohook, AST_AUDIOHOOK_SMALL_QUEUE) && (our_factory_samples > 640 || other_factory_samples > 640)) { + if (option_debug) { + ast_log(LOG_DEBUG, "Audiohook %p has stale audio in its factories. Flushing them both\n", audiohook); + } + ast_slinfactory_flush(factory); + ast_slinfactory_flush(other_factory); + } + /* Write frame out to respective factory */ ast_slinfactory_feed(factory, frame);