Add support HomeKit server

This commit is contained in:
Alexey Khit
2023-09-02 06:35:04 +03:00
parent a101387b26
commit f00e646612
18 changed files with 1988 additions and 53 deletions
+11 -4
View File
@@ -3,6 +3,7 @@ package homekit
import (
"encoding/json"
"errors"
"fmt"
"math/rand"
"net"
"net/url"
@@ -82,6 +83,8 @@ func (c *Client) GetMedias() []*core.Media {
return nil
}
c.SDP = fmt.Sprintf("%+v\n%+v", c.videoConfig, c.audioConfig)
c.Medias = []*core.Media{
videoToMedia(c.videoConfig.Codecs),
audioToMedia(c.audioConfig.Codecs),
@@ -135,6 +138,10 @@ func (c *Client) Start() error {
}
}
if c.audioSession.OnReadRTP != nil {
c.audioSession.OnReadRTP = timekeeper(c.audioSession.OnReadRTP)
}
<-deadline.C
return nil
@@ -151,9 +158,9 @@ func (c *Client) Stop() error {
func (c *Client) MarshalJSON() ([]byte, error) {
info := &core.Info{
Type: "HomeKit active producer",
URL: c.hap.URL(),
//SDP: fmt.Sprintf("%+v", *c.config),
Type: "HomeKit active producer",
URL: c.hap.URL(),
SDP: fmt.Sprintf("%+v\n%+v", c.videoConfig, c.audioConfig),
Medias: c.Medias,
Receivers: c.Receivers,
Recv: c.videoSession.Recv + c.audioSession.Recv,
@@ -197,7 +204,7 @@ func (c *Client) srtpEndpoint() *srtp.Endpoint {
}
}
func limitter(handler core.HandlerFunc) core.HandlerFunc {
func timekeeper(handler core.HandlerFunc) core.HandlerFunc {
const sampleRate = 16000
const sampleSize = 480
+193
View File
@@ -0,0 +1,193 @@
package homekit
import (
"fmt"
"io"
"math/rand"
"net"
"time"
"github.com/AlexxIT/go2rtc/pkg/core"
"github.com/AlexxIT/go2rtc/pkg/h264"
"github.com/AlexxIT/go2rtc/pkg/hap/camera"
"github.com/AlexxIT/go2rtc/pkg/srtp"
"github.com/pion/rtp"
)
type Consumer struct {
core.SuperConsumer
conn net.Conn
srtp *srtp.Server
deadline *time.Timer
sessionID string
videoSession *srtp.Session
audioSession *srtp.Session
}
func NewConsumer(conn net.Conn, server *srtp.Server) *Consumer {
return &Consumer{
SuperConsumer: core.SuperConsumer{
Type: "HomeKit passive consumer",
RemoteAddr: conn.RemoteAddr().String(),
Medias: []*core.Media{
{
Kind: core.KindVideo,
Direction: core.DirectionSendonly,
Codecs: []*core.Codec{
{Name: core.CodecH264},
},
},
{
Kind: core.KindAudio,
Direction: core.DirectionSendonly,
Codecs: []*core.Codec{
{Name: core.CodecOpus},
},
},
},
},
conn: conn,
srtp: server,
}
}
func (c *Consumer) SetOffer(offer *camera.SetupEndpoints) {
c.sessionID = offer.SessionID
c.videoSession = &srtp.Session{
Remote: &srtp.Endpoint{
Addr: offer.Address.IPAddr,
Port: offer.Address.VideoRTPPort,
MasterKey: []byte(offer.VideoCrypto.MasterKey),
MasterSalt: []byte(offer.VideoCrypto.MasterSalt),
},
}
c.audioSession = &srtp.Session{
Remote: &srtp.Endpoint{
Addr: offer.Address.IPAddr,
Port: offer.Address.AudioRTPPort,
MasterKey: []byte(offer.AudioCrypto.MasterKey),
MasterSalt: []byte(offer.AudioCrypto.MasterSalt),
},
}
}
func (c *Consumer) GetAnswer() *camera.SetupEndpoints {
c.videoSession.Local = c.srtpEndpoint()
c.audioSession.Local = c.srtpEndpoint()
return &camera.SetupEndpoints{
SessionID: c.sessionID,
Status: []byte{0},
Address: camera.Addr{
IPAddr: c.videoSession.Local.Addr,
VideoRTPPort: c.videoSession.Local.Port,
AudioRTPPort: c.audioSession.Local.Port,
},
VideoCrypto: camera.CryptoSuite{
MasterKey: string(c.videoSession.Local.MasterKey),
MasterSalt: string(c.videoSession.Local.MasterSalt),
},
AudioCrypto: camera.CryptoSuite{
MasterKey: string(c.audioSession.Local.MasterKey),
MasterSalt: string(c.audioSession.Local.MasterSalt),
},
VideoSSRC: []uint32{c.videoSession.Local.SSRC},
AudioSSRC: []uint32{c.audioSession.Local.SSRC},
}
}
func (c *Consumer) SetConfig(conf *camera.SelectedStreamConfig) bool {
if c.sessionID != conf.Control.SessionID {
return false
}
c.SDP = fmt.Sprintf("%+v\n%+v", conf.VideoCodec, conf.AudioCodec)
c.videoSession.Remote.SSRC = conf.VideoCodec.RTPParams[0].SSRC
c.videoSession.PayloadType = conf.VideoCodec.RTPParams[0].PayloadType
c.videoSession.RTCPInterval = toDuration(conf.VideoCodec.RTPParams[0].RTCPInterval)
c.audioSession.Remote.SSRC = conf.AudioCodec.RTPParams[0].SSRC
c.audioSession.PayloadType = conf.AudioCodec.RTPParams[0].PayloadType
c.audioSession.RTCPInterval = toDuration(conf.AudioCodec.RTPParams[0].RTCPInterval)
c.srtp.AddSession(c.videoSession)
c.srtp.AddSession(c.audioSession)
return true
}
func (c *Consumer) AddTrack(media *core.Media, codec *core.Codec, track *core.Receiver) error {
var session *srtp.Session
if codec.Kind() == core.KindVideo {
session = c.videoSession
} else {
session = c.audioSession
}
sender := core.NewSender(media, track.Codec)
if c.deadline == nil {
c.deadline = time.NewTimer(time.Second * 30)
sender.Handler = func(packet *rtp.Packet) {
c.deadline.Reset(core.ConnDeadline)
if n, err := session.WriteRTP(packet); err == nil {
c.Send += n
}
}
} else {
sender.Handler = func(packet *rtp.Packet) {
if n, err := session.WriteRTP(packet); err == nil {
c.Send += n
}
}
}
switch codec.Name {
case core.CodecH264:
sender.Handler = h264.RTPPay(1378, sender.Handler)
if track.Codec.IsRTP() {
sender.Handler = h264.RTPDepay(track.Codec, sender.Handler)
} else {
sender.Handler = h264.RepairAVCC(track.Codec, sender.Handler)
}
}
sender.HandleRTP(track)
c.Senders = append(c.Senders, sender)
return nil
}
func (c *Consumer) WriteTo(io.Writer) (int64, error) {
if c.deadline != nil {
<-c.deadline.C
}
return 0, nil
}
func (c *Consumer) Stop() error {
_ = c.SuperConsumer.Close()
if c.deadline != nil {
c.deadline.Reset(0)
}
return c.SuperConsumer.Close()
}
func (c *Consumer) srtpEndpoint() *srtp.Endpoint {
addr := c.conn.LocalAddr().(*net.TCPAddr)
return &srtp.Endpoint{
Addr: addr.IP.To4().String(),
Port: uint16(c.srtp.Port()),
MasterKey: []byte(core.RandString(16, 0)),
MasterSalt: []byte(core.RandString(14, 0)),
SSRC: rand.Uint32(),
}
}
func toDuration(seconds float32) time.Duration {
return time.Duration(seconds * float32(time.Second))
}
+70
View File
@@ -0,0 +1,70 @@
package homekit
import (
"bufio"
"bytes"
"net"
"net/http"
"github.com/AlexxIT/go2rtc/pkg/hap"
)
func ProxyHandler(pair ServerPair, dial func() (net.Conn, error)) hap.HandlerFunc {
return func(controller net.Conn) error {
accessory, err := dial()
if err != nil {
return err
}
// accessory (ex. Camera) => controller (ex. iPhone)
go proxy(accessory, controller, nil)
// controller => accessory
return proxy(controller, accessory, pair)
}
}
func proxy(r, w net.Conn, pair ServerPair) error {
b := make([]byte, 64*1024)
for {
n, err := r.Read(b)
if err != nil {
break
}
if pair != nil && bytes.HasPrefix(b[:n], []byte("POST /pairings HTTP/1.1")) {
buf := bytes.NewBuffer(b[:n])
req, err := http.ReadRequest(bufio.NewReader(buf))
if err != nil {
return err
}
res, err := handlePairings(r, req, pair)
if err != nil {
return err
}
buf.Reset()
if err = res.Write(buf); err != nil {
return err
}
if _, err = buf.WriteTo(r); err != nil {
return err
}
continue
}
//if n > 512 {
// log.Printf("[hap] %d bytes => %s\n%s...", n, w.RemoteAddr(), b[:512])
//} else {
// log.Printf("[hap] %d bytes => %s\n%s", n, w.RemoteAddr(), b[:n])
//}
if _, err = w.Write(b[:n]); err != nil {
break
}
}
_ = r.Close()
_ = w.Close()
return nil
}
+229
View File
@@ -0,0 +1,229 @@
package homekit
import (
"bufio"
"bytes"
"encoding/json"
"errors"
"io"
"net"
"net/http"
"strconv"
"strings"
"github.com/AlexxIT/go2rtc/pkg/hap"
"github.com/AlexxIT/go2rtc/pkg/hap/tlv8"
)
type Server interface {
ServerPair
ServerAccessory
}
type ServerPair interface {
GetPair(conn net.Conn, id string) []byte
AddPair(conn net.Conn, id string, public []byte, permissions byte)
DelPair(conn net.Conn, id string)
}
type ServerAccessory interface {
GetAccessories(conn net.Conn) []*hap.Accessory
GetCharacteristic(conn net.Conn, aid uint8, iid uint64) any
SetCharacteristic(conn net.Conn, aid uint8, iid uint64, value any)
GetImage(conn net.Conn, width, height int) []byte
}
func ServerHandler(server Server) hap.HandlerFunc {
return handleRequest(func(conn net.Conn, req *http.Request) (*http.Response, error) {
switch req.URL.Path {
case hap.PathPairings:
return handlePairings(conn, req, server)
case hap.PathAccessories:
body := hap.JSONAccessories{Value: server.GetAccessories(conn)}
return makeResponse(hap.MimeJSON, body)
case hap.PathCharacteristics:
switch req.Method {
case "GET":
var v hap.JSONCharacters
id := req.URL.Query().Get("id")
for _, id = range strings.Split(id, ",") {
s1, s2, _ := strings.Cut(id, ".")
aid, _ := strconv.Atoi(s1)
iid, _ := strconv.ParseUint(s2, 10, 64)
val := server.GetCharacteristic(conn, uint8(aid), iid)
v.Value = append(v.Value, hap.JSONCharacter{AID: uint8(aid), IID: iid, Value: val})
}
return makeResponse(hap.MimeJSON, v)
case "PUT":
var v struct {
Value []struct {
AID uint8 `json:"aid"`
IID uint64 `json:"iid"`
Value any `json:"value"`
} `json:"characteristics"`
}
if err := json.NewDecoder(req.Body).Decode(&v); err != nil {
return nil, err
}
for _, char := range v.Value {
server.SetCharacteristic(conn, char.AID, char.IID, char.Value)
}
res := &http.Response{
StatusCode: http.StatusNoContent,
Proto: "HTTP",
ProtoMajor: 1,
ProtoMinor: 1,
}
return res, nil
}
case hap.PathResource:
var v struct {
Width int `json:"image-width"`
Height int `json:"image-height"`
Type string `json:"resource-type"`
}
if err := json.NewDecoder(req.Body).Decode(&v); err != nil {
return nil, err
}
body := server.GetImage(conn, v.Width, v.Height)
return makeResponse("image/jpeg", body)
}
return nil, errors.New("hap: unsupported path: " + req.RequestURI)
})
}
func handleRequest(handle func(conn net.Conn, req *http.Request) (*http.Response, error)) hap.HandlerFunc {
return func(conn net.Conn) error {
rw := bufio.NewReaderSize(conn, 16*1024)
wr := bufio.NewWriterSize(conn, 16*1024)
for {
req, err := http.ReadRequest(rw)
//debug(req)
if err != nil {
return err
}
res, err := handle(conn, req)
//debug(res)
if err != nil {
return err
}
if err = res.Write(wr); err != nil {
return err
}
if err = wr.Flush(); err != nil {
return err
}
}
}
}
func handlePairings(conn net.Conn, req *http.Request, pair ServerPair) (*http.Response, error) {
cmd := struct {
Method byte `tlv8:"0"`
Identifier string `tlv8:"1"`
PublicKey string `tlv8:"3"`
State byte `tlv8:"6"`
Permissions byte `tlv8:"11"`
}{}
if err := tlv8.UnmarshalReader(req.Body, &cmd); err != nil {
return nil, err
}
switch cmd.Method {
case 3: // add
pair.AddPair(conn, cmd.Identifier, []byte(cmd.PublicKey), cmd.Permissions)
case 4: // delete
pair.DelPair(conn, cmd.Identifier)
}
body := struct {
State byte `tlv8:"6"`
}{
State: hap.StateM2,
}
return makeResponse(hap.MimeTLV8, body)
}
func makeResponse(mime string, v any) (*http.Response, error) {
var body []byte
var err error
switch mime {
case hap.MimeJSON:
body, err = json.Marshal(v)
case hap.MimeTLV8:
body, err = tlv8.Marshal(v)
case "image/jpeg":
body = v.([]byte)
}
if err != nil {
return nil, err
}
res := &http.Response{
StatusCode: http.StatusOK,
Proto: "HTTP",
ProtoMajor: 1,
ProtoMinor: 1,
Header: http.Header{
"Content-Type": []string{mime},
"Content-Length": []string{strconv.Itoa(len(body))},
},
ContentLength: int64(len(body)),
Body: io.NopCloser(bytes.NewReader(body)),
}
return res, nil
}
//func debug(v any) {
// switch v := v.(type) {
// case *http.Request:
// if v == nil {
// return
// }
// if v.ContentLength != 0 {
// b, err := io.ReadAll(v.Body)
// if err != nil {
// panic(err)
// }
// v.Body = io.NopCloser(bytes.NewReader(b))
// log.Printf("[homekit] request: %s %s\n%s", v.Method, v.RequestURI, b)
// } else {
// log.Printf("[homekit] request: %s %s <nobody>", v.Method, v.RequestURI)
// }
// case *http.Response:
// if v == nil {
// return
// }
// if v.Header.Get("Content-Type") == "image/jpeg" {
// log.Printf("[homekit] response: %d <jpeg>", v.StatusCode)
// return
// }
// if v.ContentLength != 0 {
// b, err := io.ReadAll(v.Body)
// if err != nil {
// panic(err)
// }
// v.Body = io.NopCloser(bytes.NewReader(b))
// log.Printf("[homekit] response: %d\n%s", v.StatusCode, b)
// } else {
// log.Printf("[homekit] response: %d <nobody>", v.StatusCode)
// }
// }
//}