Files
home-sensors/cmd/watcher/watcher.go
T

319 lines
8.7 KiB
Go
Raw Normal View History

2023-09-20 22:09:15 -07:00
//go:build !js
// +build !js
package main
import (
"context"
2023-09-28 20:35:50 -07:00
"flag"
2023-09-20 22:09:15 -07:00
"fmt"
"net/http"
"time"
"connectrpc.com/connect"
pb "github.com/chathaway-codes/home-sensors/v2/gen"
servicepb "github.com/chathaway-codes/home-sensors/v2/gen/genconnect"
2023-10-01 22:02:30 -07:00
"github.com/chathaway-codes/home-sensors/v2/internal/sensors"
2023-10-01 20:38:38 -07:00
"github.com/chathaway-codes/home-sensors/v2/internal/video"
2023-10-03 16:17:34 -07:00
"github.com/chathaway-codes/home-sensors/v2/internal/watcher/config"
2023-09-20 22:09:15 -07:00
"github.com/pion/webrtc/v3"
"github.com/pion/webrtc/v3/pkg/media"
2023-10-01 20:38:38 -07:00
"github.com/rs/zerolog/log"
2023-09-20 22:09:15 -07:00
"google.golang.org/protobuf/proto"
)
2023-10-03 16:17:34 -07:00
var (
signalerServer = flag.String("signaler_address", "home.chathaway.codes", "address of the signaler")
)
func withAuth[T any](token string, v *T) *connect.Request[T] {
req := connect.NewRequest[T](v)
2023-09-28 20:35:50 -07:00
req.Header().Add("Authorization", "Bearer "+token)
return req
}
2023-10-01 20:38:38 -07:00
func main() {
2023-09-28 20:35:50 -07:00
flag.Parse()
2023-09-20 22:09:15 -07:00
ctx := context.Background()
2023-09-28 20:35:50 -07:00
2023-10-03 16:17:34 -07:00
cfg, err := config.Default.Get()
2023-09-28 20:35:50 -07:00
if err != nil {
2023-10-03 16:17:34 -07:00
log.Fatal().Err(err).Msg("failed to get config")
2023-09-28 20:35:50 -07:00
}
2023-10-01 22:02:30 -07:00
2023-10-03 16:17:34 -07:00
client := servicepb.NewSignalerServiceClient(
http.DefaultClient,
fmt.Sprintf("https://%s/", *signalerServer),
connect.WithGRPC(),
)
authToken, err := client.CreateAuthToken(ctx, connect.NewRequest(&pb.CreateAuthTokenRequest{
2023-10-03 16:17:34 -07:00
Home: cfg.HomeName,
Type: &pb.CreateAuthTokenRequest_Camera_{
Camera: &pb.CreateAuthTokenRequest_Camera{
2023-10-03 16:17:34 -07:00
Id: cfg.CameraName,
},
},
}))
if err != nil {
2023-10-01 20:38:38 -07:00
log.Fatal().Err(err).Msg("failed to get auth token")
}
token := authToken.Msg.GetToken()
2023-10-01 20:38:38 -07:00
2023-10-03 16:17:34 -07:00
vid, err := video.Default.Get()
if err != nil {
log.Fatal().Err(err).Msg("failed to get default video")
}
sensors, err := sensors.Default.Get()
if err != nil {
log.Fatal().Err(err).Msg("failed to get default sensor")
}
2023-10-01 20:38:38 -07:00
go vid.Run()
defer vid.Done()
2023-10-01 22:02:30 -07:00
go sensors.Run()
defer sensors.Done()
sensorCh, sensorDone := sensors.Join()
defer sensorDone()
go handleSensor(ctx, client, token, sensorCh)
2023-09-28 20:35:50 -07:00
// Create a new RTCPeerConnection
2023-10-01 20:38:38 -07:00
log.Info().Msg("waiting for connections")
2023-09-20 22:09:15 -07:00
2023-09-28 20:35:50 -07:00
for {
// Wait for a session request
session, err := client.PopSession(ctx, withAuth(token, &pb.PopSessionRequest{}))
if err != nil {
2023-10-03 16:17:34 -07:00
log.Error().Err(err).Msg("error creating session")
continue
2023-09-28 20:35:50 -07:00
}
go handleSession(ctx, client, token, session, vid)
}
}
2023-10-01 22:02:30 -07:00
func handleSensor(ctx context.Context, client servicepb.SignalerServiceClient, token string, ch <-chan *pb.Sample) {
for {
var sample *pb.Sample
select {
case sample = <-ch:
// proceed
case <-ctx.Done():
return
}
if _, err := client.CreateSample(ctx, withAuth(token, &pb.CreateSampleRequest{
Sample: sample,
})); err != nil {
log.Error().Err(err).Msg("failed to create sample")
}
}
}
2023-10-01 20:38:38 -07:00
func handleSession(ctx context.Context, client servicepb.SignalerServiceClient, token string, session *connect.Response[pb.Session], vid *video.Video) {
2023-09-28 20:35:50 -07:00
var err error
2023-10-01 20:38:38 -07:00
log.Debug().Msg("new session")
2023-09-28 20:35:50 -07:00
2023-09-20 22:09:15 -07:00
peerConnection, err := webrtc.NewPeerConnection(webrtc.Configuration{
ICEServers: []webrtc.ICEServer{
{
URLs: []string{"stun:stun.l.google.com:19302"},
},
},
})
2023-09-28 20:35:50 -07:00
// We use the cancel func to signal that the stream is ready
iceConnectedCtx, iceConnectedCtxCancel := context.WithCancel(context.Background())
2023-09-20 22:09:15 -07:00
defer func() {
2023-09-28 20:35:50 -07:00
if err := peerConnection.Close(); err != nil {
2023-10-01 20:38:38 -07:00
log.Debug().Err(err).Msg("cannot close peerConnection")
2023-09-20 22:09:15 -07:00
}
}()
2023-09-28 20:35:50 -07:00
// connect to the video stream; the cleanup is done in the goroutine which
// consumes the framess
ch, trackCodec, cleanUp := vid.Join()
// Create a video track
videoTrack, videoTrackErr := webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{MimeType: trackCodec}, "video", "pion")
if videoTrackErr != nil {
2023-10-01 20:38:38 -07:00
log.Info().Err(err).Msg("Failed to create video track")
2023-09-28 20:35:50 -07:00
}
2023-09-20 22:09:15 -07:00
2023-09-28 20:35:50 -07:00
rtpSender, err := peerConnection.AddTrack(videoTrack)
if err != nil {
2023-10-01 20:38:38 -07:00
log.Info().Err(err).Msg("Failed to add track to connection")
2023-09-28 20:35:50 -07:00
}
2023-09-20 22:09:15 -07:00
2023-09-28 20:35:50 -07:00
// Read incoming RTCP packets
// Before these packets are returned they are processed by interceptors. For things
// like NACK this needs to be called.
go func() {
rtcpBuf := make([]byte, 1500)
for {
if _, _, err := rtpSender.Read(rtcpBuf); err != nil {
return
2023-09-20 22:09:15 -07:00
}
2023-09-28 20:35:50 -07:00
}
}()
2023-09-20 22:09:15 -07:00
2023-09-28 20:35:50 -07:00
go func() {
defer cleanUp()
readyToSend := false
for frame := range ch {
select {
case <-iceConnectedCtx.Done():
readyToSend = true
default:
// do nothing
2023-09-20 22:09:15 -07:00
}
2023-09-28 20:35:50 -07:00
if !readyToSend {
continue
2023-09-20 22:09:15 -07:00
}
2023-09-28 20:35:50 -07:00
if err := videoTrack.WriteSample(media.Sample{Data: frame, Duration: time.Second}); err != nil {
panic(err)
2023-09-20 22:09:15 -07:00
}
2023-09-28 20:35:50 -07:00
}
}()
2023-09-20 22:09:15 -07:00
// Set the handler for ICE connection state
// This will notify you when the peer has connected/disconnected
peerConnection.OnICEConnectionStateChange(func(connectionState webrtc.ICEConnectionState) {
2023-10-01 20:38:38 -07:00
log.Debug().Msgf("Connection State has changed %s \n", connectionState.String())
2023-09-20 22:09:15 -07:00
if connectionState == webrtc.ICEConnectionStateConnected {
iceConnectedCtxCancel()
}
})
// Set the handler for Peer connection state
// This will notify you when the peer has connected/disconnected
2023-10-03 16:17:34 -07:00
exitCh := make(chan struct{})
2023-09-20 22:09:15 -07:00
peerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
2023-10-01 20:38:38 -07:00
log.Debug().Msgf("Peer Connection State has changed: %s\n", s.String())
2023-09-20 22:09:15 -07:00
if s == webrtc.PeerConnectionStateFailed {
// Wait until PeerConnection has had no network activity for 30 seconds or another failure. It may be reconnected using an ICE Restart.
// Use webrtc.PeerConnectionStateDisconnected if you are interested in detecting faster timeout.
// Note that the PeerConnection may come back from PeerConnectionStateDisconnected.
2023-10-03 16:17:34 -07:00
close(exitCh)
2023-09-28 20:35:50 -07:00
return
2023-09-20 22:09:15 -07:00
}
2023-10-03 16:17:34 -07:00
if s == webrtc.PeerConnectionStateDisconnected {
close(exitCh)
}
2023-09-20 22:09:15 -07:00
})
peerConnection.OnICECandidate(func(i *webrtc.ICECandidate) {
if i == nil {
2023-09-28 20:35:50 -07:00
if _, err := client.CreateIceMessage(ctx, withAuth(token, &pb.CreateIceMessageRequest{
SessionIdentifier: session.Msg.GetId(),
IceMessage: &pb.IceMessage{
Type: &pb.IceMessage_NoMoreCandidates{},
},
})); err != nil {
2023-10-01 20:38:38 -07:00
log.Warn().Err(err).Msg("error sending done w/ candidates")
2023-09-28 20:35:50 -07:00
}
2023-09-20 22:09:15 -07:00
return
}
c := i.ToJSON()
2023-09-28 20:35:50 -07:00
var usernameFragment *string
if c.UsernameFragment != nil {
usernameFragment = proto.String(*c.UsernameFragment)
}
client.CreateIceMessage(ctx, withAuth(token, &pb.CreateIceMessageRequest{
SessionIdentifier: session.Msg.GetId(),
IceMessage: &pb.IceMessage{
Type: &pb.IceMessage_Candidate{
Candidate: &pb.IceCandidate{
Candidate: c.Candidate,
SdpMid: c.SDPMid,
SdpLineIndex: proto.Int32(int32(*c.SDPMLineIndex)),
2023-09-28 20:35:50 -07:00
UsernameFragment: usernameFragment,
},
},
},
}))
2023-09-20 22:09:15 -07:00
})
2023-10-01 20:38:38 -07:00
log.Info().Msg("Spawning helper")
2023-09-28 20:35:50 -07:00
// helper which sends answers, waits for
2023-09-20 22:09:15 -07:00
// Add ICE candidates from remote
2023-09-28 20:35:50 -07:00
for {
2023-10-03 16:17:34 -07:00
select {
case <-exitCh:
return
default:
// check for another message
}
2023-09-28 20:35:50 -07:00
msg, err := client.PopIceMessage(ctx, withAuth(token, &pb.PopIceMessageRequest{
SessionIdentifier: session.Msg.GetId(),
}))
if err != nil {
2023-10-01 20:38:38 -07:00
log.Info().Err(err).Msg("failed to pop ice message")
continue
2023-09-28 20:35:50 -07:00
}
switch msg.Msg.Type.(type) {
case *pb.IceMessage_Candidate:
candidate := msg.Msg.GetCandidate()
var sdpMLine *uint16
if candidate.SdpLineIndex != nil {
t := uint16(candidate.GetSdpLineIndex())
sdpMLine = &t
}
if err := peerConnection.AddICECandidate(webrtc.ICECandidateInit{
Candidate: candidate.GetCandidate(),
SDPMid: candidate.SdpMid,
SDPMLineIndex: sdpMLine,
}); err != nil {
2023-10-01 20:38:38 -07:00
log.Warn().Err(err).Msg("failed to add ice candidate")
2023-09-28 20:35:50 -07:00
}
// Send back an answer
answer, err := peerConnection.CreateAnswer(nil)
if err != nil {
2023-10-01 20:38:38 -07:00
log.Debug().Msg("Candidate failed")
2023-09-28 20:35:50 -07:00
continue
}
if err := peerConnection.SetLocalDescription(answer); err != nil {
2023-10-01 20:38:38 -07:00
log.Info().Err(err).Msg("Failed to set local description")
2023-09-28 20:35:50 -07:00
}
_, err = client.CreateIceMessage(ctx, withAuth(token, &pb.CreateIceMessageRequest{
SessionIdentifier: session.Msg.GetId(),
2023-09-28 20:35:50 -07:00
IceMessage: &pb.IceMessage{
Type: &pb.IceMessage_Session{
Session: &pb.IceSessionDescription{
SdpType: int64(answer.Type),
Sdp: answer.SDP,
},
},
},
}))
if err != nil {
2023-10-01 20:38:38 -07:00
log.Info().Err(err).Msg("Failed to send answer")
}
2023-09-28 20:35:50 -07:00
case *pb.IceMessage_Session:
iceSession := msg.Msg.GetSession()
2023-09-20 22:09:15 -07:00
2023-09-28 20:35:50 -07:00
switch iceSession.SdpType {
case int64(webrtc.SDPTypeOffer):
offer := webrtc.SessionDescription{
2023-09-28 20:35:50 -07:00
Type: webrtc.SDPType(iceSession.SdpType),
SDP: iceSession.Sdp,
}
2023-09-20 22:09:15 -07:00
2023-09-28 20:35:50 -07:00
if err := peerConnection.SetRemoteDescription(offer); err != nil {
2023-10-01 20:38:38 -07:00
log.Warn().Err(err).Msg("failed to set remote description")
}
2023-09-28 20:35:50 -07:00
default:
2023-10-01 20:38:38 -07:00
log.Info().Msgf("unexpected sdp type: %v", webrtc.SDPType(iceSession.SdpType).String())
}
2023-10-01 20:38:38 -07:00
log.Info().Msg("Accepted promise!")
2023-09-28 20:35:50 -07:00
case *pb.IceMessage_NoMoreCandidates:
// do nothing
}
2023-09-28 20:35:50 -07:00
}
2023-09-20 22:09:15 -07:00
}