FreeSWITCH: Limiting the number of concurrent calls on multiple SIP accounts

The user has several SIP accounts on a vPBX, and he wants that maximum one call is possible at a time.

The limit application in FreeSWITCH allows to control the number of concurrent calls, but one should be careful with when this limit should be applied. The switch decrements the limit counter automatically when a channel is terminated. But if the limit is executed on a-leg, and b-leg is transferred, the limit counter decreases only when the a-leg finishes the call. As a result, the user may receive a call, transfer it to a new destination and hang up, but the new calls are not coming in because the limit counter is reset when the original call ends.

In order to reset the limit counter after the b-leg is transferred, the limit application needs to be executed on b-leg only. This is possible by exporting the execute_on_answer variable with nolocal modifier.

The example also shows how to retrieve user variables from the XML directory in the calls toward the user.

  <context name="moretti">

    <extension name="common_variables" continue="true">
      <condition>
        <action inline="true" application="set" data="availability_username=moretti"/>
      </condition>
    </extension>

    <extension name="pstn_out">      
      <condition field="destination_number" expression="^[01]" break="on-false">
        <!-- For outbound calls, we only set the limit counters,
             but do not limit the call -->
        <action application="limit" data="hash ${domain_name} ${availability_username} -1"/>
        <action application="set" data="hangup_after_bridge=true"/>
        <action application="set" data="continue_on_fail=false"/>
      </condition>

      <condition>
        <action application="bridge" data="${outgw}/${destination_number}"/>
      </condition>
    </extension> 

    <extension name="inbound_73x">
      <condition field="destination_number" expression="^73(d)$" break="on-false">
        <!-- retrieve variables from the user entry in the directory -->
        <action application="set" data="directory_userid=70$1@${domain_name}"/>
        <action application="set" data="call_timeout=${user_data(${directory_userid} var ring_timeout)}"/>
      </condition>

      <!-- check the limit -->
      <condition field="${cond(${limit_usage(hash ${domain} ${availability_username})} > 0 ? true:false)}"
                 expression="^true$" break="on-true">
        <action application="hangup"/>
      </condition>

      <!-- group call to the SIP user and a mobile phone -->
      <condition>
        <action application="export" data="nolocal:execute_on_answer=limit hash ${domain} ${availability_username} -1"/>

        <action application="set" data="ignore_early_media=true"/>
        <action application="set" data="transfer_ringback=$${hold_music}"/>
        <action application="set" data="hangup_after_bridge=true"/>
        <action application="set" data="continue_on_fail=false"/>
        <action application="bridge" data="user/${directory_userid},[leg_delay_start=10]${outgw}/0123456789"/>
      </condition>
    </extension>    

  </context>

, , ,

  1. #1 by chrisbware on November 21, 2017 - 10:07 am

    Stanislav, thank you so much! I’ve spent a day trying to fix this issue. Keep up the good work!

    • #2 by txlab on November 21, 2017 - 10:08 am

      🙂

Leave a comment