Redirect call through dialling local channel

Hello, I need to redirect calls, but cannot do this directly, only by means of the local channel (music on hold not smooth when dialing multiple times). Occasionally I get the Exceptionally long voice queue length queuing message. Also randomly, talks start with choppy sound for several seconds, and this is most annoying. How can I correct this? What’s wrong with my dial plan?

[call-client]
exten => s,1,NoOp(Call client)
same => n,Set(__call_protocol=PJSIP)
same => n,Set(__call_trunk=XXXXX) ; trunk name
same => n,Set(__redir_num=XXXXXXXXXXX) ; should be some number
same => n,Set(dnid=${CUT(CALLERID(dnid),;,1)})

same => n,Set(__orig_num={IF([{LEN({CALLERID(RDNIS)})}>0]?{CALLERID(RDNIS)}:{orig_num})})
same => n,Set(__orig_num={IF([{LEN({REDIRECTING(orig-num)})}>0]?{REDIRECTING(orig-num)}:{orig_num})})
same => n,Set(__orig_num={IF([{LEN({orig_num})}=0&{redir_num}!={dnid}]?{dnid}:{orig_num})})

same => n,Set(DEFAULT_MUSIC_ON_HOLD=XXX); default music class
same => n,Set(DEFAULT_BUSY_FILE=busy_default)
same => n,Set(DEFAULT_NOANSWER_FILE=noanswer_default)
same => n,Set(__phones={ARG1}) ; phones, arg2 same => n,Set(timeout={IF([{ISNULL({ARG2})}=1]?180:{ARG2})}) ; total call time, arg2
same => n,Set(__trial_timeout={IF([{ISNULL({ARG3})}=1]?30:{ARG3})}) ; trial time, arg3 same => n,Set(play_moh={IF([{ISNULL({ARG4})}=1]?0:{ARG4})}); play moh, arg 4
same => n,Set(moh={IF([{ISNULL({ARG5})}=1]?{DEFAULT_MUSIC_ON_HOLD}:{ARG5})}) ; moh file, arg5
same => n,Set(__say_busy={IF([{ISNULL({ARG6})}=1]?0:{ARG6})}); - play busy message, arg6 same => n,Set(busy_file={IF([{ISNULL({ARG7})}=1]?busy/{DEFAULT_BUSY_FILE}:busy/{ARG7})}) ; busy message file, arg7 same => n,Set(return_back={IF([{ISNULL({ARG8})}=1]?0:{ARG8})}) ; return to previous context, arg8
same => n,Set(say_noanswer={IF([{ISNULL({ARG9})}=1]?0:{ARG9})}) ; play noanswer file, arg9 same => n,Set(noanswer_file={IF([{ISNULL({ARG10})}=1]?{DEFAULT_NOANSWER_FILE}:${ARG10})}) ; noanswer file, arg10

same => n,Set(dial_options=)
same => n,ExecIf([{play_moh}=1]?Set(dial_options=[{dial_options}m({moh})]):Set(dial_options=[{dial_options}r]))

same => n,Set(__phones_busy={IF([{say_busy}=1]?1:0)}) same => n,Set(__phones_problem=1) same => n,Set(__call_end_time=[{EPOCH}+{timeout}])

same => n(dial),Dial(Local/s@call-client-phones/n,{timeout},{dial_options})
same => n(afterdial),GoToIf([{phones_busy}=1]?busy:noanswer)
same => n(busy),Wait(0.5)
same => n,Playback(${busy_file})
same => n,Wait(0.5)
same => n,Set(__phones_busy=0)
same => n,GoTo(dial)

same => n(noanswer),NoOp(Cancelling call)
same => n,Wait(0.5)
same => n,ExecIf([{say_noanswer}=1]?Playback(noanswer/{noanswer_file})) same => n,ExecIf([${return_back}=1]?Return():HangUp())

[call-client-phones]
exten => s,1,Set(cphones={phones}) same => n(whl),While(["{SET(cl_i={SHIFT(cphones,+)})}" !=""])
same => n,Set(cl_ph=)
same => n,While(["{SET(cl_phi={SHIFT(cl_i,&)})}" !=""]) same => n,Set(cl_ph={IF([{ISNULL({cl_ph})}=1]?{call_protocol}/{cl_phi}@{call_trunk}:{cl_ph}&{call_protocol}/{cl_phi}@{call_trunk})})
same => n,EndWhile()
same => n,Dial({cl_ph},{trial_timeout},b(add-diversion-header^s^1({redir_num},{orig_num})))
same => n,Set(phones_busy={IF([{DIALSTATUS}=BUSY&{phones_busy}=1]?1:0)})
same => n,EndWhile()
same => n,Set(cphones={phones}) same => n,ExecIf([${phones_busy}=0]?GoTo(phones_free))
same => n,Hangup()

same => n(phones_free),Wait(2)
same => n,GoToIf([{DIALSTATUS}=BUSY|${DIALSTATUS}=NOANSWER]?whl)

[add-diversion-header]
exten => s,1,NoOp(Diversion)
same => n,Set(phoneb={CUT(ARG1,\;,1)}) same => n,Set(origphone={CUT(ARG2,;,1)})
same => n,Set(counter={IF([{LEN({origphone})}=0]?[{REDIRECTING(count)}+1]:{REDIRECTING(count)})}) same => n,Set(counter={IF([{counter}<1]?1:{counter})}) same => n,Set(PJSIP_HEADER(add,Diversion)="{phoneb}" sip:${phoneb}@XXX.XXX.XXX.XXX:5060;reason=unconditional;privacy=off;screen=no;counter={counter}) same => n,GoToIf([{LEN({origphone})}=0]?return)
same => n,Set(PJSIP_HEADER(add,Diversion)="{origphone}" <sip:{origphone}@XXX.XXX.XXX.XXX:5060>;reason=unconditional;privacy=off;screen=no;counter=1)
exten => s,n(return),Return()

Choppy audio is either resource overload (e.g. network overload) or a problem with the device at hte destination of the media. Some devices don’t respond well to source changes.

I would start by making the call from something like X-Lite, which doesn’t appear to have the second problem.

I temporarily turned off the firewall. Today the sound is good, the swap file is not used. Yesterday, when the problems occured, one megabyte of it was used and there was much memory cached.

Cached memory is generally a good thing. What is bad is having anything in the swap file, although it may only indicate a historic memory shortage, Memory manages wll generally discard cached pages before using the swap file.

So, maybe at some time in the past the system had memory shortage, and it subsequently influenced performance. Thank you, I will watch how it will run during the next couple of days.