sip icon indicating copy to clipboard operation
sip copied to clipboard

SIP Trunk with Asterisk

Open KTarun003 opened this issue 1 year ago • 50 comments

Hi,

I would like my agent to accept inbound calls or make outbound calls through asterisk server. I have searched the entire documentation, but I could only find the steps Twilio or Telnyx. Any help will be much appreciated.

KTarun003 avatar Nov 25 '24 06:11 KTarun003

Hi @KTarun003!

The steps to accept inbound or make outbound calls for Asterisk is the same as with Twilio or Telnyx. We do not provide documentation for it, because Asterisk can be configured in many different ways and we obviously cannot cover them all.

Is there any specific step of the quickstart that doesn't work with your Asterisk setup? Also, please consider joining our Slack - we usually answer general support questions there.

dennwc avatar Nov 25 '24 11:11 dennwc

I am facing issue when I want to create a trunk, I dont know which URI to point the trunk. And I am using examples/voice-pipeline-agent/function_calling_weather.py example. Are there any changes to this code when using SIP. I am trying to self-host. if that helps.

KTarun003 avatar Nov 25 '24 11:11 KTarun003

I have a similar question. I just asked it on the #sip slack channel, and I referenced this issue too.

arthurblake avatar Nov 25 '24 20:11 arthurblake

I’m trying to connect it with an Asterisk system, but for now, I’m starting with a softphone like Zoiper to test the integration, and I haven’t been able to get it working correctly. I’m using the LiveKit API to create rules similar to these: SIP Trunk Creation: lk sip inbound create inbound-trunk.json

  "trunk": {
    "name": "Softphone inbound trunk",
    "numbers": ["+9999999999"], 
    "auth_username": "sip-user",
    "auth_password": "sip-pass",
    "allowed_addresses": ["0.0.0.0/0"]
  }
}

Dispatch Rule Creation: lk sip dispatch create dispatch-rule.json

{
  "name": "test-1 dispatch rule softphone",
  "trunk_ids": ["<id>"],
  "rule": {
    "dispatchRuleIndividual": {
      "roomPrefix": "call"
    }
  }
}

The issue is that I can’t register the softphone with LiveKit. I’ve followed the official documentation and successfully set up an example with Twilio by associating a phone number, but now I’d like to test it with Zoiper to eventually integrate the system with Asterisk.

I’m registering the softphone with the following details:

Username: sip-user Password: sip-pass Sip uri: 6o3m8e2088l.sip.livekit.cloud

Do you have any advice or additional steps to make this configuration work correctly? Any help would be greatly appreciated!

rodrigoGA avatar Nov 26 '24 15:11 rodrigoGA

Hi, In my case, I successfully completed the integration with Asterisk with the help of fellow developers and by using LiveKit documentation. The process involves creating a dispatch rule and a room to obtain the SIP URI and registering the number within LiveKit.

You should not register the trunk outside of LiveKit for outgoing calls. For incoming calls, you only need to send the call to the SIP URI you get from LiveKit.

On the server where you have Asterisk and currently receive the DID from your provider to a standard Asterisk extension (this is to verify that everything is working fine on your provider's side and with your Asterisk server), once validated, you should route the call from your Dialplan like this: same => n,Dial(SIP/${EXTEN}@dilivekitetcetc.sip.livekit.cloud)

As mentioned, the call must negotiate using the ulaw-alaw codecs, as I encountered issues when only G729 was being sent in my configuration.

For outgoing calls, you need to configure LiveKit so that when the server launches, it handles the call to the destination number (this is the "participant" according to LiveKit documentation). You will also need to create a trunk in Asterisk and register it in LiveKit since it will generate the call to your SIP provider connected to the PSTN. Therefore, you must configure Asterisk to manage this process (create the context where the call will land and send it to your SIP provider).

charly17 avatar Nov 28 '24 22:11 charly17

Hi, I try to get livekit also running with asterisk, but I have a complete local setup with an ATA to connect an analog phone, livekit and livekit-sip locally running in a docker container. I managed it that when I call a defined number, the phone enters the room (I use "dispatchRuleDirect"). But the phone continues to ring - the room is not answering the phone.

Any help would be appreciated - especially @charly17 - do you maybe have an idea whats the problem on my side? Thanks in advance :)

dydimos avatar Dec 16 '24 22:12 dydimos

I think one way to resolve the issues here would be if someone can provide an example of Asterisk configuration inside a Docker container.

We can then combine it with our existing Docker compose example for self-hosting and properly document integration with Asterisk in our official docs.

Unfortunately I don't have spare cycles right now to do it, and I'm not very familiar with Asterisk. Can someone help by making a repo/PR with an example config? That would be really helpful!

dennwc avatar Dec 17 '24 11:12 dennwc

Hi, I try to get livekit also running with asterisk, but I have a complete local setup with an ATA to connect an analog phone, livekit and livekit-sip locally running in a docker container. I managed it that when I call a defined number, the phone enters the room (I use "dispatchRuleDirect"). But the phone continues to ring - the room is not answering the phone.

Any help would be appreciated - especially @charly17 - do you maybe have an idea whats the problem on my side? Thanks in advance :)

I know it is not an answer, but I am having exactly the same problem using FreeSwitch.

It sends the Invite, gets a Processing(100) response and then a Ringing (180) response repeated every second until the call times out with no answer.

My node application detects the call and logs the connecting to agent which suggests the dispatch and numbers all line up. We just never get the call answered (Status 200) by the room as you say.

My current line of thought is if the response is coming from a different IP address so is being blocked by the firewall...

It is similar to : https://github.com/livekit/sip/issues/208

Update: I added a pin to the dispatch rule. This stopped the agent from doing anything but the call did the same thing. This suggests LiveKit cloud thinks the call is being answered but the return SIP messaging is going to the wrong place.

Update 2: It works where the SIP messaging is TCP but not when it is UDP. To set it up on FreeSwitch is really easy.

Create a Destination setting the action as a bridge to the SIP endpoint like:

<extension name="TestAgent" continue="false" uuid="4f38bc1b-fa5d-44ef-988c-c6191432ab19">
	<condition field="destination_number" expression="111">
		<action application="bridge" data="sofia/external/[email protected];transport=tcp"/>
	</condition>
</extension>

You don't need to do anything else. No need to create a trunk.

dtcgroup avatar Dec 17 '24 16:12 dtcgroup

If you get continuous 180 responses, this means LiveKit SIP is waiting for any media from an Agent.

Due to the way SFU works currently, publishing tracks is not enough. You might need to send at least one media packet (could be empty/silence). This should unblock SIP and it will start bridging the call.

Are you seeing Waiting for track subscription(s) each time the call gets stuck? If so, this is it.

dennwc avatar Dec 17 '24 21:12 dennwc

Somehow I was able to solve it, now the call is transmitted to the room and I can hear myself speak. I used your docker-compose as a reference.

If everything works for me, I will document the setup and can post it here if there is any interest.

dydimos avatar Dec 18 '24 23:12 dydimos

Hello @dydimos, I'm trying to achieve the same.

I have a complete local setup with an ATA (Grandstream H802) to connect an analog phone, livekit, and livekit-sip locally running.

The ATA modem is simply not registering/communicating with the local livekit-sip server.

I confirmed that all my servers, ATA modem, livekit, livekit-sip, and Redis, are running and accessible on the local network.

Any help would be appreciated.

Thanks in advance.

umairmehar avatar Dec 26 '24 12:12 umairmehar

Estou tentando conectá-lo a um sistema Asterisk, mas, por enquanto, estou começando com um softphone como o Zoiper para testar a integração, e não consegui fazê-lo funcionar corretamente. Estou usando a API do LiveKit para criar regras semelhantes a estas: Criação de tronco SIP: lk sip inbound create inbound-trunk.json

  "trunk": {
    "name": "Softphone inbound trunk",
    "numbers": ["+9999999999"], 
    "auth_username": "sip-user",
    "auth_password": "sip-pass",
    "allowed_addresses": ["0.0.0.0/0"]
  }
}

Criação de regra de despacho: lk sip dispatch create dispatch-rule.json

{
  "name": "test-1 dispatch rule softphone",
  "trunk_ids": ["<id>"],
  "rule": {
    "dispatchRuleIndividual": {
      "roomPrefix": "call"
    }
  }
}

O problema é que não consigo registrar o softphone com o LiveKit. Eu segui a documentação oficial e configurei com sucesso um exemplo com o Twilio associando um número de telefone, mas agora eu gostaria de testá-lo com o Zoiper para eventualmente integrar o sistema com o Asterisk.

Estou registrando o softphone com os seguintes detalhes:

Nome de usuário: sip-user Senha: sip-pass Sip uri: 6o3m8e2088l.sip.livekit.cloud

Você tem algum conselho ou etapas adicionais para fazer essa configuração funcionar corretamente? Qualquer ajuda seria muito apreciada!

Alguma solução?

duzaq avatar Jan 06 '25 13:01 duzaq

Somehow I was able to solve it, now the call is transmitted to the room and I can hear myself speak. I used your docker-compose as a reference.

If everything works for me, I will document the setup and can post it here if there is any interest.

Of course, I'm interested in seeing your setup. Do you use livekit.cloud, or are you running a local LiveKit server?

UlfWil avatar Jan 19 '25 21:01 UlfWil

Hi everyone,

I'm attempting to integrate my Asterisk server with LiveKit SIP (Also mentioned this in slack community), and I'm running into an issue where calls to the SIP URI are failing with a 503 "Try again later" response. Below are the details of what I've done so far:

Setup Overview:

  • SIP URI: sip:2vbic7bb4fd.sip.livekit.cloud
  • Asterisk is configured to forward calls to this URI when the DID is dialed.
  • The DID is confirmed to be working correctly and is properly routed to the context in Asterisk.

Asterisk Dialplan:

Here’s the relevant part of my dialplan:

exten => 9999,1,Dial(SIP/2vbic7bb4fd.sip.livekit.cloud)
exten => 9999,n,GotoIf($["${DIALSTATUS}"="CONGESTION"]?retry:hangup)
exten => 9999,n(retry),Wait(5)
exten => 9999,n,Dial(SIP/2vbic7bb4fd.sip.livekit.cloud)
exten => 9999,n(hangup),Hangup()

Issue:

When I call the DID, Asterisk attempts to route the call to LiveKit, but I receive the following logs:

-- Called SIP/2vbic7bb4fd.sip.livekit.cloud
-- Got SIP response 503 "Try again later" back from 84.235.238.224:5060
-- SIP/2vbic7bb4fd.sip.livekit.cloud-0000002d is circuit-busy
== Everyone is busy/congested at this time (1:0/1/0)

This results in a CONGESTION status and retries as per the dialplan logic. However, the issue persists even after retries.

Steps Taken to Troubleshoot:

  1. Codec Compatibility: I ensured that only ulaw and alaw codecs are allowed in both Asterisk and LiveKit.
  2. SIP URI Verification: Confirmed the SIP URI is correct and matches the one provided by LiveKit.
  3. Firewall: Verified that there are no firewall restrictions blocking traffic to/from LiveKit (84.235.238.224:5060).
  4. LiveKit Dispatch Rules: Checked that the dispatch rule routes calls to the correct room in LiveKit.
  5. Connectivity: Tested connectivity to the LiveKit server using ping and telnet for SIP port 5060.

Logs from Asterisk:

Here’s an excerpt from the logs:

Got SIP response 503 "Try again later" back from 84.235.238.224:5060
SIP/2vbic7bb4fd.sip.livekit.cloud-0000002d is circuit-busy

Request for Help:

  • Is the 503 "Try again later" response indicative of an issue on LiveKit's side, or could it be related to my configuration?
  • Are there additional debugging steps I can take to identify the cause of this issue?
  • Could this be related to resource availability or misconfigured dispatch rules in LiveKit?

Any guidance or suggestions would be greatly appreciated. Thank you in advance for your time and help!

Best regards.

UmairShah7677 avatar Jan 20 '25 17:01 UmairShah7677

Hi @UmairShah7677,

I don't see that Asterisk is sending the extension number when Dial. You can try this: Dial(SIP/[email protected]) where EXT_NUMBER is the number that you configured in Livekit Cloud when created the Inbound Trunk.

Remember that you need configure two things in Livekit: an Inbound Trunk, and a Dispatch Rule. As documentation says: https://docs.livekit.io/sip/trunk-inbound/

MefhigosetH avatar Jan 20 '25 17:01 MefhigosetH

Hi everyone!

We are having a very similar issue.

We are using the cloud sip server, and first we connected our agent using sip-trunk and dispatch rules to a Twilio Number and it works just fine.

But we need to integrate with Kazoo our VoIP, so we created an extension on kazoo to LiveKit, created the SIP Trunk and dispatch rules. But in this case when we make the call, its received by LiveKit, the agent is actually dispatched to the room and we can see even the agents starts the speech

2025-01-20 14:14:05,947 - DEBUG livekit.agents.pipeline - speech playout started {"speech_id": "01fa05decd9a"}

but on the user ends theres still ringing, and we just keep getting a 180 code and never de 200 status

Image

Any guidance is helpful

ccameroVzy avatar Jan 20 '25 20:01 ccameroVzy

Hi @ccameroVzy,

I've read that Livekit Cloud, only works with TCP packets. Not UDP. Try with TCP communication.

MefhigosetH avatar Jan 20 '25 20:01 MefhigosetH

@MefhigosetH yes, that was the issue.

Now is working properly thanks!

ccameroVzy avatar Jan 20 '25 21:01 ccameroVzy

Hey @MefhigosetH Thank you for help, it is connected but now it is keep ringing. Can you please tell me @ccameroVzy that how you fixed as after this Dial(SIP/[email protected]) the 503 error is solved, but its keep on ringing, Also i can see session on the livekit cloud.

Image

In sngrep it is giving response of "200" but i can't hear any agent.

Image

UmairShah7677 avatar Jan 21 '25 08:01 UmairShah7677

@UmairShah7677 , I think that your Asterisk needs to communicate with Livekit Cloud via TCP. To do so, follow this article: https://support.digium.com/s/article/How-to-use-SIP-over-TCP

Note that Asterisk 1.8 and above support SIP over TCP.

If upgrade is not possible, you can try a hosted Livekit instance using Docker Compose. This schema support standard SIP via UDP. I have been test with Asterisk 11 and everything works fine.

MefhigosetH avatar Jan 21 '25 20:01 MefhigosetH

Thank you @MefhigosetH working fine.

UmairShah7677 avatar Jan 22 '25 10:01 UmairShah7677

We enabled the TCP as well, but it keeps ringing while livekit agent is publishing track on the room properly as per livekit agents logs

PatroxGaurab avatar Mar 27 '25 14:03 PatroxGaurab

Me está sucediendo igual. Se realiza el registro de la llamada, pero no hay una respuesta. No se si estoy haciendo algo mal

Image

Paso a paso de lo que me ha sucedido:

  • Paso 1: Me aparecía el siguiente error:
Got SIP response 503 "Try again later" back from 84.235.238.224:5060
SIP/2vbic7bb4fd.sip.livekit.cloud-0000002d is circuit-busy
Everyone is busy/congested at this time (1:0/1/0)

Lo pude solucionar agregando lo siguiente en el archivo de Asterisk:

Dial(SIP/{EXT_NUMBER}@2vbic7bb4fd.sip.livekit.cloud)

  • Paso 2: Ahora al realizar la llamada se queda sonando, hasta colgarse por sí sola y es ahi donde aparece registro de la llamada en el dashboard de LiveKit

Image

Agradezco cualquier ayuda... Gracias!!!

stiven77nj avatar Mar 27 '25 14:03 stiven77nj

Hi.

I think a VoIP traffic trace would be helpful. Maybe a sngrep trace?

I understand that the call will keep ringing until the first DSP (handshake) packet arrives 🤷🏻‍♂️

MefhigosetH avatar Mar 27 '25 15:03 MefhigosetH

¿Esta configuración es correcta?

host=#####.sip.livekit.cloud
port=5060
context=from-trunk-custom
insecure=port,invite
nat=yes
qualify=yes
dtmfmode=rfc2833
disallow=all
allow=opus
canreinvite=no
transport=tcp

¡¡¡Agradezco cualquier ayuda... Gracias!!!

stiven77nj avatar Mar 27 '25 19:03 stiven77nj

Hi @stiven77nj 👋🏻

The config looks ok for me.

Please, take into account that SDP media packets may came from a host with an IP distinct that the SIP signalling host (TCP/5060) talking with your Asterisk. Make sure that any device or policy in your network is blocking/filtering this traffic.

See this comment https://github.com/livekit/sip/issues/231#issuecomment-2603906205 for an example conversation between hosts.

MefhigosetH avatar Mar 27 '25 19:03 MefhigosetH

Hola, estuve revisando lo que me dices, pero no vi nada claro. Sin embargo, hizo un montaje en local con docker como la siguiendo la documentación. Estoy relizando unas llamadas y los logs de mis contenedores aparece lo siguiente:

  • servidor liveKit:

Image

  • servidor sip:

Image

Alguna idea??... Ayuda por favor

stiven77nj avatar Mar 28 '25 22:03 stiven77nj

Hola @stiven77nj 👋🏻

Por el mensaje del servidor/contenedor livekit, pareciera que no existe un inbound trunk que coincida con la llamada entrante que le estás enviando. También debes crear una Dispatch Rule.

Sería el paso 3 de este instructivo: https://docs.livekit.io/agents/quickstarts/inbound-calls/

MefhigosetH avatar Mar 29 '25 01:03 MefhigosetH

Hola @MefhigosetH !

La verdad, no entiendo. Sigue sin funcionar.

Este es el paso a paso de lo hice:

  • .env.local

Image

  • inbound-trunk.json

Image

  • dispatch-rule.json

Image

Los IDs de cada archivo los genero de la siguiente manera:

lk sip inbound create inbound-trunk.json
lk sip dispatch create dispatch-rule.json

No se si exista una guia o alguien que haya podido realizar la conexion de manera exitosa, me pueda ayudar!.

Gracias!!

stiven77nj avatar Mar 31 '25 13:03 stiven77nj

Hi, is there any solution for ringing, As when the call is routing towards self hosted sip server, it keeps on ringing, i turned off the sounds on my own asterisk server, but this ringing is coming from the livekit sip server, how can i turn the ring off.

UmairShah7677 avatar Jul 07 '25 16:07 UmairShah7677