go2rtc icon indicating copy to clipboard operation
go2rtc copied to clipboard

Basic SIP server for doorbells

Open felipecrs opened this issue 2 years ago • 21 comments

This project never stops surprising me. It is like the always missing glue for cameras and smart homes. And I have another suggestion.

An embedded and very basic SIP server that doorbells like Dahua VTO or Hikvision can connect to.

go2rtc has 2-way audio support for doorbells, which is awesome. But that's not enough for a fully functional doorbell setup. In other words, a SIP server is still required even though go2rtc can do 2-way audio.

That's because the doorbell still needs to signal and receive signals to react on certain events like:

  • Doorbell is calling
  • Call has been accepted
  • Call has been ended

In my understanding, if go2rtc could expose a basic SIP server in which the doorbell could connect to, no other project like the Asterisk add-on would be required (Asterisk is VERY hard to manage, and overkill for the purpose).

go2rtc as a SIP server would:

  • Allow to receive calls from the doorbell (i.e. it would let the doorbell call it and ring)
    • When the VTO doesn't have a SIP server configured to accept a call from it, it basically doesn't work.
    • Users can then create Home Assistant cards and automations to notify when the doorbell is ringing
  • Send the call accepted signal to the doorbell as soon as some 2-way audio connection is established
  • Send the call ended signal to the doorbell as soon as that 2-way audio connection is dropped

Luckily, there seems to be some good SIP servers written in golang out there:

  • https://github.com/emiago/sipgo
  • https://github.com/ghettovoice/gosip
  • https://github.com/jart/gosip

So, what do you think?

Refs #49

felipecrs avatar May 04 '23 14:05 felipecrs

I knows nothing about SIP technologies and didn't have any hardware that using it.

AlexxIT avatar May 05 '23 03:05 AlexxIT

That's sad, but thank you.

I know very little of Golang, but nevertheless I'll try to build something.

felipecrs avatar May 05 '23 03:05 felipecrs

It does not look very difficult according to the examples of https://github.com/emiago/sipgo. However, my doorbell is in a very inaccessible place which makes it very hard for me to work with it.

But I will try to build something when I get a chance.

felipecrs avatar May 10 '23 03:05 felipecrs

Can your doorbell works with this integration? https://www.home-assistant.io/integrations/voip/

AlexxIT avatar May 10 '23 03:05 AlexxIT

I don't think so. That does not even look like SIP also.

I think that integration only works with the Grandstream conversor for old analog phones.

I don't think that integration talks to the conversor over SIP either. Maybe I'm wrong.


Off topic, SIP itself could be used as a 2-way audio source provider. In fact, even as a video source.

But what I'm suggesting is way simpler than that, which is to make go2rtc to expose a SIP server on port like 5060.

Then I would configure my doorbell to use go2rtc as the SIP server.

On the go2rtc side, apart from exposing the SIP TCP server on port 5060, it would:

  • Accept the registration of the doorbell (checks the username and password authentication)
  • Allow the doorbell to ring (it should send the "call in progress" signal back to the doorbell)
  • When someone connects to the 2-way audio stream with microphone, send the call accepted signal.
  • Maybe send the hangup signal on some other occasion.

I currently use the Asterisk add-on https://github.com/TECH7Fox/asterisk-hass-addons with the SIP card to be able to call with my doorbell from Home Assistant https://github.com/TECH7Fox/sip-hass-card.

Cc @pergolafabio @nanosonde which are the most experienced people I know about SIP.

(Sorry to ping you both, if you don't care about this please unsubscribe)

Any opinion is appreciated. I don't know if the approach I suggest actually makes sense or is feasible. Maybe I'm just crazy.

felipecrs avatar May 10 '23 03:05 felipecrs

Oh wait! The VoIP integration for Home Assistant seems to be SIP indeed. I'll do some testing with it.

felipecrs avatar May 10 '23 03:05 felipecrs

Yes, the voip is indeed a sip, but be careful, it only works with opus codec, and remember to change port to something else then 5060 if you have asterisk running...

But anyway, I don't see the point for calling to HA to speak to assist anyway :-)

pergolafabio avatar May 10 '23 04:05 pergolafabio

But I can easily read code of voip integration and reproduce it with go2rtc. I saw how they accept connection on 5060 port and answer to it.

You right. They using OPUS/48000/2. I can change it to PCM/16000/1

AlexxIT avatar May 10 '23 05:05 AlexxIT

As realized in https://github.com/rroller/dahua/issues/359#issuecomment-2139719570, the Dahua VTO is useless when NOT CONNECTED to a SIP server. Even the most basic function of button press event will not work when it's not connected to a SIP server.

I detailed my VTO setup on Home Assistant (https://github.com/felipecrs/dahua-vto-on-home-assistant) and I use go2rtc for 2-way audio communication, but I still need to run the full Asterisk add-on just to have a SIP server for my VTO to connect to and therefore allow me to capture the button press event to run automations on.

After these realizations, just to reiterate, if go2rtc could expose a simple SIP server for the doorbell to connect to, I could get rid of Asterisk in my setup which would make things a lot easier.

felipecrs avatar May 30 '24 14:05 felipecrs

Can your doorbell works with this integration? https://www.home-assistant.io/integrations/voip/

By the way, I tried making it work with that integration and I could not. No matter what, the doorbell would not connect to it. I don't have much details though.

felipecrs avatar May 30 '24 14:05 felipecrs

I will try making some tests with:

https://github.com/emiago/sipgo/tree/main/example/register

felipecrs avatar May 30 '24 14:05 felipecrs

OMG! I can't believe, it worked in the first attempt!

~\repos\sipgo\example\register main ❯ go run ./server -u "8001:somepass" -ip 0.0.0.0:5060
May 30 11:53:33.797517 INF Listening on addr=0.0.0.0:5060
May 30 11:53:42.670412 INF New client registered source=192.168.1.40:5060 username=8001
May 30 11:57:34.866194 WRN Server > SIP request handler not found
May 30 11:57:34.888231 WRN Server > SIP request handler not found
May 30 11:57:34.976455 WRN Server > SIP request handler not found

192.168.1.40 is my VTO. It registered in gosip immediatelly with no issue.

image

And everything worked as expected. The button press event is emitted and I don't need SIP for anything else.

@AlexxIT, it would be awesome if you could bundle this in go2rtc. What do you think?

felipecrs avatar May 30 '24 15:05 felipecrs

I'll try when have time for my two doorbells

AlexxIT avatar May 30 '24 17:05 AlexxIT

hi, I join in here. I have a Loxone smarthome and use Hikvision DS-K1T342MFWX Door Stations. Using go2rtc I was able to grab the video of the door stations and transcode them to the mjpeg stream loxone needs. I wrote a small udp listener (container with python script parsing the udp sip frame) waiting for the sip call of the door station and trigger the doorbell/loxone app with it. this works fine.

Unfortunately the Door Station is not listening for SIP-Invites - the only possibility to talk to visitors is using ISAPI/Two-Way-Audio. On the other hand, the only possibility for Loxone to connect to Door Stations is doing an outbound SIP-Call.

So if I read this issue right go2rtc might be able to be the missing glue.

Would it somehow be possible to connect to a go2rtc SIP-Listener which connects audio to the two-way audio stream? Or am I thinking to complicated here and there would be a more easy way?

If someone is interested in the python script I can clean it up a bit and share it.

wiking-at avatar Oct 26 '24 14:10 wiking-at

im' interested

i also use the two way audio for my hikvisvion device i have indoor stations , so i use the hikvision addon to first send the callsignal to abort the ringing, then i start two way audio

pergolafabio avatar Oct 26 '24 14:10 pergolafabio

@pergolafabio Here you go: https://gist.github.com/wiking-at/fc30228cee8bff3cefca975b48119b28

wiking-at avatar Oct 26 '24 18:10 wiking-at

Ty, will have a look after

pergolafabio avatar Oct 26 '24 18:10 pergolafabio

That will only work probably without indoor station? Don't think you can call callcenter and indoor center at same time

pergolafabio avatar Oct 26 '24 18:10 pergolafabio

if you have an indoor station it should be easier afaik - as you can use it to set up sip. - and maybe configure the indoor station to forward the call to the udp listener for the mqtt processing.

I don't have an indoor station and would prefer to just use a tablet with Loxone App (or home assistant app if Loxone doesn't work out).

wiking-at avatar Oct 26 '24 18:10 wiking-at

Yeah, but I want to avoid SIP, I have an sip script to use a script to register as extension, so I can forward the call from indoor... Don't think it's possible to call indoor and callcenter at same time, without sip

pergolafabio avatar Oct 26 '24 19:10 pergolafabio

i see.. as the outdoor station can always only call one ip this is hard. possible solutions - proxy from helper to indoor station or mirror and inspect traffic to indoor station - both solutions not really viable. your current solution seems much better.

wiking-at avatar Oct 26 '24 20:10 wiking-at