add: basic webrtc is working
This commit is contained in:
+24
-15
@@ -5,6 +5,7 @@ import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -51,22 +52,25 @@ func New() *Server {
|
||||
func (s *Server) CreateAuthToken(ctx context.Context, request *connect.Request[pb.CreateAuthTokenRequest]) (*connect.Response[pb.AuthToken], error) {
|
||||
req := request.Msg
|
||||
|
||||
log.Printf("Creating auth token")
|
||||
defer log.Printf("Done creating auth token")
|
||||
var id string
|
||||
switch req.Type.(type) {
|
||||
case *pb.CreateAuthTokenRequest_Camera_:
|
||||
id = req.GetCamera().GetId()
|
||||
s.mu.Lock()
|
||||
thisCamera := &camera{
|
||||
id: id,
|
||||
}
|
||||
home := req.GetHome()
|
||||
s.mu.Lock()
|
||||
if _, ok := s.camerasByHome[home]; !ok {
|
||||
s.camerasByHome[home] = make(map[string]*camera)
|
||||
}
|
||||
s.camerasByHome[home][id] = thisCamera
|
||||
if _, ok := s.sessionsByCamera[id]; !ok {
|
||||
s.sessionsByCamera[id] = make(chan *session, 100)
|
||||
if _, ok := s.sessionsByCamera[id]; ok {
|
||||
close(s.sessionsByCamera[id])
|
||||
}
|
||||
s.sessionsByCamera[id] = make(chan *session, 100)
|
||||
s.mu.Unlock()
|
||||
case *pb.CreateAuthTokenRequest_Client_:
|
||||
myUUID, err := uuid.NewV4()
|
||||
@@ -113,6 +117,9 @@ func (s *Server) ListCameras(ctx context.Context, request *connect.Request[pb.Li
|
||||
// Optionally, wait_for_update can be set to prevent returning until the Camera has seen the
|
||||
// session request, populated candidates, and returned a session offer.
|
||||
func (s *Server) CreateSession(ctx context.Context, request *connect.Request[pb.CreateSessionRequest]) (*connect.Response[pb.Session], error) {
|
||||
|
||||
log.Printf("Creating session")
|
||||
defer log.Printf("Done session")
|
||||
thisSession := request.Msg.Session
|
||||
if thisSession == nil {
|
||||
return nil, status.Errorf(codes.InvalidArgument, "nil session")
|
||||
@@ -125,8 +132,6 @@ func (s *Server) CreateSession(ctx context.Context, request *connect.Request[pb.
|
||||
id := myUUID.String()
|
||||
thisSession.Id = &pb.Session_Identifier{Id: id}
|
||||
cameraID := thisSession.GetCamera().GetId()
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
sess := &session{
|
||||
id: id,
|
||||
@@ -135,8 +140,13 @@ func (s *Server) CreateSession(ctx context.Context, request *connect.Request[pb.
|
||||
toCamera: make(chan *pb.IceMessage, 100),
|
||||
toClient: make(chan *pb.IceMessage, 100),
|
||||
}
|
||||
s.sessionsByCamera[cameraID] <- sess
|
||||
|
||||
s.mu.Lock()
|
||||
ch := s.sessionsByCamera[cameraID]
|
||||
s.sessionsById[id] = sess
|
||||
s.mu.Unlock()
|
||||
|
||||
ch <- sess
|
||||
|
||||
return connect.NewResponse(thisSession), nil
|
||||
}
|
||||
@@ -161,6 +171,10 @@ func (s *Server) PopSession(ctx context.Context, request *connect.Request[pb.Pop
|
||||
|
||||
sess := <-ch
|
||||
|
||||
if sess == nil {
|
||||
return nil, status.Errorf(codes.DataLoss, "someone else stole the session")
|
||||
}
|
||||
|
||||
return connect.NewResponse(&pb.Session{
|
||||
Id: &pb.Session_Identifier{
|
||||
Id: sess.id,
|
||||
@@ -228,8 +242,10 @@ func (s *Server) cleanup() {
|
||||
ticker := time.NewTicker(time.Minute * 5)
|
||||
for t := range ticker.C {
|
||||
func() {
|
||||
log.Printf("Starting cleanup")
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
log.Printf("Cleanup locked")
|
||||
|
||||
// Look for any stale sessions
|
||||
staleSessionsByCamera := make(map[string]*session)
|
||||
@@ -245,16 +261,9 @@ func (s *Server) cleanup() {
|
||||
}
|
||||
}
|
||||
}
|
||||
log.Printf("Removing stale sessions")
|
||||
|
||||
for camera, lastSession := range staleSessionsByCamera {
|
||||
// Consume from the chanel until we remove all stale sessions
|
||||
for sess := range s.sessionsByCamera[camera] {
|
||||
if sess == lastSession {
|
||||
// We consumed all stale channels; break
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
// TODO: how do we prevent sessions from accumlating if cameras don't pick up on the request?
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user