add: in production
This commit is contained in:
+41
-18
@@ -8,7 +8,6 @@ import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"connectrpc.com/connect"
|
||||
@@ -16,13 +15,17 @@ import (
|
||||
servicepb "github.com/chathaway-codes/home-sensors/v2/gen/genconnect"
|
||||
"github.com/chathaway-codes/home-sensors/v2/internal/sensors"
|
||||
"github.com/chathaway-codes/home-sensors/v2/internal/video"
|
||||
"github.com/chathaway-codes/home-sensors/v2/internal/watcher/config"
|
||||
"github.com/pion/webrtc/v3"
|
||||
"github.com/pion/webrtc/v3/pkg/media"
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/rs/zerolog/log"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
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)
|
||||
req.Header().Add("Authorization", "Bearer "+token)
|
||||
@@ -30,10 +33,32 @@ func withAuth[T any](token string, v *T) *connect.Request[T] {
|
||||
}
|
||||
|
||||
func main() {
|
||||
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
||||
flag.Parse()
|
||||
ctx := context.Background()
|
||||
|
||||
cfg, err := config.Default.Get()
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("failed to get config")
|
||||
}
|
||||
|
||||
client := servicepb.NewSignalerServiceClient(
|
||||
http.DefaultClient,
|
||||
fmt.Sprintf("https://%s/", *signalerServer),
|
||||
connect.WithGRPC(),
|
||||
)
|
||||
authToken, err := client.CreateAuthToken(ctx, connect.NewRequest(&pb.CreateAuthTokenRequest{
|
||||
Home: cfg.HomeName,
|
||||
Type: &pb.CreateAuthTokenRequest_Camera_{
|
||||
Camera: &pb.CreateAuthTokenRequest_Camera{
|
||||
Id: cfg.CameraName,
|
||||
},
|
||||
},
|
||||
}))
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("failed to get auth token")
|
||||
}
|
||||
token := authToken.Msg.GetToken()
|
||||
|
||||
vid, err := video.Default.Get()
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("failed to get default video")
|
||||
@@ -43,19 +68,6 @@ func main() {
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("failed to get default sensor")
|
||||
}
|
||||
client := servicepb.NewSignalerServiceClient(http.DefaultClient, "http://192.168.0.65:8080/")
|
||||
authToken, err := client.CreateAuthToken(ctx, connect.NewRequest(&pb.CreateAuthTokenRequest{
|
||||
Home: "home1234",
|
||||
Type: &pb.CreateAuthTokenRequest_Camera_{
|
||||
Camera: &pb.CreateAuthTokenRequest_Camera{
|
||||
Id: "movie",
|
||||
},
|
||||
},
|
||||
}))
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("failed to get auth token")
|
||||
}
|
||||
token := authToken.Msg.GetToken()
|
||||
|
||||
go vid.Run()
|
||||
defer vid.Done()
|
||||
@@ -75,7 +87,8 @@ func main() {
|
||||
// Wait for a session request
|
||||
session, err := client.PopSession(ctx, withAuth(token, &pb.PopSessionRequest{}))
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("error creating session")
|
||||
log.Error().Err(err).Msg("error creating session")
|
||||
continue
|
||||
}
|
||||
go handleSession(ctx, client, token, session, vid)
|
||||
}
|
||||
@@ -174,6 +187,7 @@ func handleSession(ctx context.Context, client servicepb.SignalerServiceClient,
|
||||
|
||||
// Set the handler for Peer connection state
|
||||
// This will notify you when the peer has connected/disconnected
|
||||
exitCh := make(chan struct{})
|
||||
peerConnection.OnConnectionStateChange(func(s webrtc.PeerConnectionState) {
|
||||
log.Debug().Msgf("Peer Connection State has changed: %s\n", s.String())
|
||||
|
||||
@@ -181,9 +195,12 @@ func handleSession(ctx context.Context, client servicepb.SignalerServiceClient,
|
||||
// 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.
|
||||
fmt.Println("Peer Connection has gone to failed exiting")
|
||||
close(exitCh)
|
||||
return
|
||||
}
|
||||
if s == webrtc.PeerConnectionStateDisconnected {
|
||||
close(exitCh)
|
||||
}
|
||||
})
|
||||
|
||||
peerConnection.OnICECandidate(func(i *webrtc.ICECandidate) {
|
||||
@@ -223,6 +240,12 @@ func handleSession(ctx context.Context, client servicepb.SignalerServiceClient,
|
||||
|
||||
// Add ICE candidates from remote
|
||||
for {
|
||||
select {
|
||||
case <-exitCh:
|
||||
return
|
||||
default:
|
||||
// check for another message
|
||||
}
|
||||
msg, err := client.PopIceMessage(ctx, withAuth(token, &pb.PopIceMessageRequest{
|
||||
SessionIdentifier: session.Msg.GetId(),
|
||||
}))
|
||||
|
||||
Reference in New Issue
Block a user