Skip to content

Commit

Permalink
Add support doorbird source #1060
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Nov 24, 2024
1 parent 25145f7 commit 194d1da
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 0 deletions.
36 changes: 36 additions & 0 deletions internal/doorbird/doorbird.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package doorbird

import (
"net/url"

"github.com/AlexxIT/go2rtc/internal/streams"
"github.com/AlexxIT/go2rtc/pkg/core"
"github.com/AlexxIT/go2rtc/pkg/doorbird"
)

func Init() {
streams.RedirectFunc("doorbird", func(rawURL string) (string, error) {
u, err := url.Parse(rawURL)
if err != nil {
return "", err
}

// https://www.doorbird.com/downloads/api_lan.pdf
switch u.Query().Get("media") {
case "video":
u.Path = "/bha-api/video.cgi"
case "audio":
u.Path = "/bha-api/audio-receive.cgi"
default:
return "", nil
}

u.Scheme = "http"

return u.String(), nil
})

streams.HandleFunc("doorbird", func(source string) (core.Producer, error) {
return doorbird.Dial(source)
})
}
4 changes: 4 additions & 0 deletions internal/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/AlexxIT/go2rtc/pkg/image"
"github.com/AlexxIT/go2rtc/pkg/magic"
"github.com/AlexxIT/go2rtc/pkg/mpjpeg"
"github.com/AlexxIT/go2rtc/pkg/pcm"
"github.com/AlexxIT/go2rtc/pkg/tcp"
)

Expand Down Expand Up @@ -87,6 +88,9 @@ func do(req *http.Request) (core.Producer, error) {
return image.Open(res)
case ct == "multipart/x-mixed-replace":
return mpjpeg.Open(res.Body)
//https://www.iana.org/assignments/media-types/audio/basic
case ct == "audio/basic":
return pcm.Open(res.Body)
}

return magic.Open(res.Body)
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/AlexxIT/go2rtc/internal/app"
"github.com/AlexxIT/go2rtc/internal/bubble"
"github.com/AlexxIT/go2rtc/internal/debug"
"github.com/AlexxIT/go2rtc/internal/doorbird"
"github.com/AlexxIT/go2rtc/internal/dvrip"
"github.com/AlexxIT/go2rtc/internal/echo"
"github.com/AlexxIT/go2rtc/internal/exec"
Expand Down Expand Up @@ -82,6 +83,7 @@ func main() {
bubble.Init() // bubble source
expr.Init() // expr source
gopro.Init() // gopro source
doorbird.Init() // doorbird source

// 6. Helper modules

Expand Down
100 changes: 100 additions & 0 deletions pkg/doorbird/backchannel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package doorbird

import (
"fmt"
"net"
"net/http"
"net/url"
"time"

"github.com/AlexxIT/go2rtc/pkg/core"
"github.com/pion/rtp"
)

type Client struct {
core.Connection
conn net.Conn
}

func Dial(rawURL string) (*Client, error) {
u, err := url.Parse(rawURL)
if err != nil {
return nil, err
}

user := u.User.Username()
pass, _ := u.User.Password()

rawURL = fmt.Sprintf("http://%s/bha-api/audio-transmit.cgi?http-user=%s&&http-password=%s", u.Host, user, pass)

req, err := http.NewRequest("POST", rawURL, nil)
if err != nil {
return nil, err
}
req.Header = http.Header{
"Content-Type": []string{"audio/basic"},
"Content-Length": []string{"9999999"},
"Connection": []string{"Keep-Alive"},
"Cache-Control": []string{"no-cache"},
}

if u.Port() == "" {
u.Host += ":80"
}

conn, err := net.DialTimeout("tcp", u.Host, core.ConnDialTimeout)
if err != nil {
return nil, err
}

_ = conn.SetWriteDeadline(time.Now().Add(core.ConnDeadline))
if err = req.Write(conn); err != nil {
return nil, err
}

medias := []*core.Media{
{
Kind: core.KindAudio,
Direction: core.DirectionSendonly,
Codecs: []*core.Codec{
{Name: core.CodecPCMU, ClockRate: 8000},
},
},
}

return &Client{
core.Connection{
ID: core.NewID(),
FormatName: "doorbird",
Protocol: "http",
URL: rawURL,
Medias: medias,
Transport: conn,
},
conn,
}, nil
}

func (c *Client) GetTrack(media *core.Media, codec *core.Codec) (*core.Receiver, error) {
return nil, core.ErrCantGetTrack
}

func (c *Client) AddTrack(media *core.Media, codec *core.Codec, track *core.Receiver) error {
sender := core.NewSender(media, track.Codec)

sender.Handler = func(pkt *rtp.Packet) {
_ = c.conn.SetWriteDeadline(time.Now().Add(core.ConnDeadline))
if n, err := c.conn.Write(pkt.Payload); err == nil {
c.Send += n
}
}

sender.HandleRTP(track)
c.Senders = append(c.Senders, sender)
return nil
}

func (c *Client) Start() (err error) {
_, err = c.conn.Read(nil)
return
}
55 changes: 55 additions & 0 deletions pkg/pcm/producer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package pcm

import (
"io"

"github.com/AlexxIT/go2rtc/pkg/core"
"github.com/pion/rtp"
)

type Producer struct {
core.Connection
rd io.Reader
}

func Open(rd io.Reader) (*Producer, error) {
medias := []*core.Media{
{
Kind: core.KindAudio,
Direction: core.DirectionRecvonly,
Codecs: []*core.Codec{
{Name: core.CodecPCMU, ClockRate: 8000},
},
},
}
return &Producer{
core.Connection{
ID: core.NewID(),
FormatName: "pcm",
Medias: medias,
Transport: rd,
},
rd,
}, nil
}

func (c *Producer) Start() error {
for {
payload := make([]byte, 1024)
if _, err := io.ReadFull(c.rd, payload); err != nil {
return err
}

c.Recv += 1024

if len(c.Receivers) == 0 {
continue
}

pkt := &rtp.Packet{
Header: rtp.Header{Timestamp: core.Now90000()},
Payload: payload,
}
c.Receivers[0].WriteRTP(pkt)
}
}

0 comments on commit 194d1da

Please sign in to comment.