Implement Agents protocol addition (#2786)
This commit is contained in:
@@ -56,11 +56,13 @@ type AgentHandler struct {
|
||||
workers map[string]*agent.Worker
|
||||
keyProvider auth.KeyProvider
|
||||
|
||||
// TODO remove once deprecated CheckEnabled is removed
|
||||
namespaces map[string]*namespaceInfo
|
||||
publisherEnabled bool
|
||||
roomEnabled bool
|
||||
roomTopic string
|
||||
publisherTopic string
|
||||
|
||||
roomTopic string
|
||||
publisherTopic string
|
||||
}
|
||||
|
||||
type namespaceInfo struct {
|
||||
|
||||
@@ -19,10 +19,13 @@ import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
"github.com/livekit/protocol/utils"
|
||||
"github.com/livekit/protocol/utils/guid"
|
||||
"github.com/livekit/psrpc"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/config"
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
@@ -78,6 +81,11 @@ func (r *StandardRoomAllocator) CreateRoom(ctx context.Context, req *livekit.Cre
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
req, err = r.applyNamedRoomConfiguration(req)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
if req.EmptyTimeout > 0 {
|
||||
rm.EmptyTimeout = req.EmptyTimeout
|
||||
}
|
||||
@@ -98,6 +106,24 @@ func (r *StandardRoomAllocator) CreateRoom(ctx context.Context, req *livekit.Cre
|
||||
internal.TrackEgress = req.Egress.Tracks
|
||||
}
|
||||
}
|
||||
if req.Agent == nil {
|
||||
// Backward compatibility: by default, start any agent in the empty namespace
|
||||
req.Agent = &livekit.RoomAgent{
|
||||
Agents: []*livekit.CreateAgentJobDefinitionRequest{
|
||||
&livekit.CreateAgentJobDefinitionRequest{
|
||||
Type: livekit.JobType_JT_ROOM,
|
||||
Room: req.Name,
|
||||
Namespace: "default",
|
||||
},
|
||||
&livekit.CreateAgentJobDefinitionRequest{
|
||||
Type: livekit.JobType_JT_PUBLISHER,
|
||||
Room: req.Name,
|
||||
Namespace: "default",
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
internal.Agents = req.Agent.Agents
|
||||
if req.MinPlayoutDelay > 0 || req.MaxPlayoutDelay > 0 {
|
||||
internal.PlayoutDelay = &livekit.PlayoutDelay{
|
||||
Enabled: true,
|
||||
@@ -182,3 +208,44 @@ func applyDefaultRoomConfig(room *livekit.Room, internal *livekit.RoomInternal,
|
||||
}
|
||||
internal.SyncStreams = conf.SyncStreams
|
||||
}
|
||||
|
||||
func (r *StandardRoomAllocator) applyNamedRoomConfiguration(req *livekit.CreateRoomRequest) (*livekit.CreateRoomRequest, error) {
|
||||
if req.ConfigName == "" {
|
||||
return req, nil
|
||||
}
|
||||
|
||||
conf, ok := r.config.Room.RoomConfigurations[req.ConfigName]
|
||||
if !ok {
|
||||
return nil, psrpc.NewErrorf(psrpc.InvalidArgument, "unknown roomc confguration in create room request")
|
||||
}
|
||||
|
||||
clone := proto.Clone(req).(*livekit.CreateRoomRequest)
|
||||
|
||||
// Request overwrites conf
|
||||
if clone.EmptyTimeout == 0 {
|
||||
clone.EmptyTimeout = conf.EmptyTimeout
|
||||
}
|
||||
if clone.DepartureTimeout == 0 {
|
||||
clone.DepartureTimeout = req.DepartureTimeout
|
||||
}
|
||||
if clone.MaxParticipants == 0 {
|
||||
clone.MaxParticipants = conf.MaxParticipants
|
||||
}
|
||||
if clone.Egress == nil {
|
||||
clone.Egress = proto.Clone(conf.Egress).(*livekit.RoomEgress)
|
||||
}
|
||||
if clone.Agent == nil {
|
||||
clone.Agent = proto.Clone(conf.Agent).(*livekit.RoomAgent)
|
||||
}
|
||||
if clone.MinPlayoutDelay == 0 {
|
||||
clone.MinPlayoutDelay = conf.MinPlayoutDelay
|
||||
}
|
||||
if clone.MaxPlayoutDelay == 0 {
|
||||
clone.MaxPlayoutDelay = conf.MaxPlayoutDelay
|
||||
}
|
||||
if !clone.SyncStreams {
|
||||
clone.SyncStreams = conf.SyncStreams
|
||||
}
|
||||
|
||||
return clone, nil
|
||||
}
|
||||
|
||||
@@ -103,10 +103,14 @@ func (s *RoomService) CreateRoom(ctx context.Context, req *livekit.CreateRoomReq
|
||||
defer done()
|
||||
|
||||
if created {
|
||||
go s.agentClient.LaunchJob(context.WithoutCancel(ctx), &agent.JobDescription{
|
||||
JobType: livekit.JobType_JT_ROOM,
|
||||
Room: rm,
|
||||
})
|
||||
_, internal, err := s.roomStore.LoadRoom(ctx, livekit.RoomName(req.Name), true)
|
||||
|
||||
if internal.Agents != nil {
|
||||
err = s.launchAgents(ctx, rm, internal.Agents)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if req.Egress != nil && req.Egress.Room != nil {
|
||||
// ensure room name matches
|
||||
@@ -126,6 +130,23 @@ func (s *RoomService) CreateRoom(ctx context.Context, req *livekit.CreateRoomReq
|
||||
return rm, nil
|
||||
}
|
||||
|
||||
func (s *RoomService) launchAgents(ctx context.Context, rm *livekit.Room, agents []*livekit.CreateAgentJobDefinitionRequest) error {
|
||||
for _, ag := range agents {
|
||||
if ag.Type != livekit.JobType_JT_ROOM {
|
||||
continue
|
||||
}
|
||||
|
||||
go s.agentClient.LaunchJob(ctx, &agent.JobRequest{
|
||||
JobType: ag.Type,
|
||||
Room: rm,
|
||||
Metadata: ag.Metadata,
|
||||
Namespace: ag.Namespace,
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *RoomService) ListRooms(ctx context.Context, req *livekit.ListRoomsRequest) (*livekit.ListRoomsResponse, error) {
|
||||
AppendLogFields(ctx, "room", req.Names)
|
||||
err := EnsureListPermission(ctx)
|
||||
@@ -288,7 +309,7 @@ func (s *RoomService) UpdateRoomMetadata(ctx context.Context, req *livekit.Updat
|
||||
return nil, twirpAuthError(err)
|
||||
}
|
||||
|
||||
room, _, err := s.roomStore.LoadRoom(ctx, livekit.RoomName(req.Room), false)
|
||||
room, internal, err := s.roomStore.LoadRoom(ctx, livekit.RoomName(req.Room), false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -323,10 +344,10 @@ func (s *RoomService) UpdateRoomMetadata(ctx context.Context, req *livekit.Updat
|
||||
}
|
||||
|
||||
if created {
|
||||
go s.agentClient.LaunchJob(context.WithoutCancel(ctx), &agent.JobDescription{
|
||||
JobType: livekit.JobType_JT_ROOM,
|
||||
Room: room,
|
||||
})
|
||||
err = s.launchAgents(ctx, room, internal.Agents)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return room, nil
|
||||
|
||||
@@ -533,10 +533,24 @@ func (s *RTCService) startConnection(
|
||||
}
|
||||
|
||||
if created && s.agentClient != nil {
|
||||
go s.agentClient.LaunchJob(ctx, &agent.JobDescription{
|
||||
JobType: livekit.JobType_JT_ROOM,
|
||||
Room: cr.Room,
|
||||
})
|
||||
// TODO Have CreateRoom return the RoomInternal object?
|
||||
_, internal, err := s.store.LoadRoom(ctx, livekit.RoomName(roomName), true)
|
||||
if err != nil {
|
||||
return connectionResult{}, nil, err
|
||||
}
|
||||
|
||||
for _, ag := range internal.Agents {
|
||||
if ag.Type != livekit.JobType_JT_ROOM {
|
||||
continue
|
||||
}
|
||||
|
||||
go s.agentClient.LaunchJob(ctx, &agent.JobRequest{
|
||||
JobType: ag.Type,
|
||||
Room: cr.Room,
|
||||
Metadata: ag.Metadata,
|
||||
Namespace: ag.Namespace,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// this needs to be started first *before* using router functions on this node
|
||||
|
||||
Reference in New Issue
Block a user