Multi VoIP Provider Aggregation and Asterisk Local Channels
It’s been a while since I’ve first wanted to try the Asterisk local channels to offer the ability to dial into an aggregation of several VoIP providers so that, from a user perspective, the actual provider set is hidden behind the local context. Technically, I couldn’t see why it wouldn’t work out but I just did a small experiment today with interesting results.
Obviously, the idea behind provider aggregation is to scale: leveraging on multiple providers to offer a maximum number of concurrent channels. This technique offers a second benefit: the ability to initiate calls from several distinct POPs without much hassle.
From the Asterisk documentation:
“[...] a pseudo-channel [which] loops calls back into the dialplan in a different context.”
Hence, calling a local channel allows one to start the call execution directly within a specific execution context. Leveraging on this, provider aggregation is basically hidden within such context, where the actual SIP dialing is performed, depending on a mix of variables and conditions.
First, we obviously need to set our providers within the sip.conf configuration. Nothing special compared to offering direct SIP dialing.
As one can see, while your millage may vary, nothing unexpected. We are just setting the table.
Then within the dial plan, we first need to define a few global variables to be shared among the channels.
In this specific case, we have define 2 providers, hence, we are setting the appropriate global variables for those, that is, the maximum number of concurrent calls.
Then comes the special sauce.
We define our local context (multi-provider) within which we define 2 extensions handlers, one for the actual dialed extension, and the other one for handling the hangup. For each provider, we set a condition, comparing the actual count with the limit, then we set the PROVIDER_CONTEXT channel variable. This variable will not only be used to construct the actual Dial command, but also within the h handler to decrease the count. An else condition has been used to return with congestion if all provider channels are currently in used. Finally, we increment the number of channel in use for the selected provider, and perform the actual dial on the provider sip channel.
Now, to use that setup, simply issue the following:
Areas of Improvements
- The conditional section is a bit cumbersome (when you have a bunch of providers to setup) and could be improved using a macro.
- We could probably use some sort of index variable instead of using hard coded variables such as provider_2, provider_2_count, provider_2_limit.
- Some edge could still be missing at this stage and I might revisit some.
- Instead of filling the first provider, then the second and so on, we could have perform either a round robin or random selection logic as well.