Agents enabled check (#2227)

* agents enabled check

* participant -> publisher

* nil check client

* add NumConnections

* add lock around agent check

* do not launch agents against other agents

* regen

* don't need atomic anymore

* update protocol
This commit is contained in:
David Colburn
2023-11-07 19:19:07 -08:00
committed by GitHub
parent 302facc60d
commit 57643a42ed
15 changed files with 385 additions and 91 deletions
+47 -13
View File
@@ -30,6 +30,7 @@ import (
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
"github.com/livekit/protocol/rpc"
"github.com/livekit/protocol/utils"
"github.com/livekit/livekit-server/pkg/config"
@@ -74,15 +75,18 @@ type Room struct {
audioConfig *config.AudioConfig
serverInfo *livekit.ServerInfo
telemetry telemetry.TelemetryService
agentClient AgentClient
egressLauncher EgressLauncher
trackManager *RoomTrackManager
// agents
agentClient AgentClient
publisherAgentsEnabled bool
// map of identity -> Participant
participants map[livekit.ParticipantIdentity]types.LocalParticipant
participantOpts map[livekit.ParticipantIdentity]*ParticipantOptions
participantRequestSources map[livekit.ParticipantIdentity]routing.MessageSource
hasPublished sync.Map // map of identity -> bool
hasPublished map[livekit.ParticipantIdentity]bool
bufferFactory *buffer.FactoryOfBufferFactory
// batch update participant info for non-publishers
@@ -135,11 +139,13 @@ func NewRoom(
participants: make(map[livekit.ParticipantIdentity]types.LocalParticipant),
participantOpts: make(map[livekit.ParticipantIdentity]*ParticipantOptions),
participantRequestSources: make(map[livekit.ParticipantIdentity]routing.MessageSource),
hasPublished: make(map[livekit.ParticipantIdentity]bool),
bufferFactory: buffer.NewFactoryOfBufferFactory(config.Receiver.PacketBufferSize),
batchedUpdates: make(map[livekit.ParticipantIdentity]*livekit.ParticipantInfo),
closed: make(chan struct{}),
trailer: []byte(utils.RandomSecret()),
}
r.protoProxy = utils.NewProtoProxy[*livekit.Room](roomUpdateInterval, r.updateProto)
if r.protoRoom.EmptyTimeout == 0 {
r.protoRoom.EmptyTimeout = DefaultEmptyTimeout
@@ -148,6 +154,21 @@ func NewRoom(
r.protoRoom.CreationTime = time.Now().Unix()
}
if agentClient != nil {
go func() {
res := r.agentClient.CheckEnabled(context.Background(), &rpc.CheckEnabledRequest{})
if res.PublisherEnabled {
r.lock.Lock()
r.publisherAgentsEnabled = true
// if there are already published tracks, start the agents
for identity := range r.hasPublished {
r.launchPublisherAgent(r.participants[identity])
}
r.lock.Unlock()
}
}()
}
go r.audioUpdateWorker()
go r.connectionQualityWorker()
go r.changeUpdateWorker()
@@ -477,6 +498,7 @@ func (r *Room) RemoveParticipant(identity livekit.ParticipantIdentity, pID livek
delete(r.participants, identity)
delete(r.participantOpts, identity)
delete(r.participantRequestSources, identity)
delete(r.hasPublished, identity)
if !p.Hidden() {
r.protoRoom.NumParticipants--
}
@@ -512,7 +534,6 @@ func (r *Room) RemoveParticipant(identity livekit.ParticipantIdentity, pID livek
for _, t := range p.GetPublishedTracks() {
r.trackManager.RemoveTrack(t)
}
r.hasPublished.Delete(p.Identity())
p.OnTrackUpdated(nil)
p.OnTrackPublished(nil)
@@ -902,17 +923,15 @@ func (r *Room) onTrackPublished(participant types.LocalParticipant, track types.
r.trackManager.AddTrack(track, participant.Identity(), participant.ID())
// launch jobs
_, hasPublished := r.hasPublished.Swap(participant.Identity(), true)
r.lock.Lock()
hasPublished := r.hasPublished[participant.Identity()]
r.hasPublished[participant.Identity()] = true
publisherAgentsEnabled := r.publisherAgentsEnabled
r.lock.Unlock()
if !hasPublished {
if r.agentClient != nil {
go func() {
r.agentClient.JobRequest(context.Background(), &livekit.Job{
Id: utils.NewGuid("JP_"),
Type: livekit.JobType_JT_PUBLISHER,
Room: r.protoRoom,
Participant: participant.ToProto(),
})
}()
if publisherAgentsEnabled {
r.launchPublisherAgent(participant)
}
if r.internal != nil && r.internal.ParticipantEgress != nil {
go func() {
@@ -1302,6 +1321,21 @@ func (r *Room) connectionQualityWorker() {
}
}
func (r *Room) launchPublisherAgent(p types.Participant) {
if p == nil || p.IsRecorder() || p.IsAgent() {
return
}
go func() {
r.agentClient.JobRequest(context.Background(), &livekit.Job{
Id: utils.NewGuid("JP_"),
Type: livekit.JobType_JT_PUBLISHER,
Room: r.ToProto(),
Participant: p.ToProto(),
})
}()
}
func (r *Room) DebugInfo() map[string]interface{} {
info := map[string]interface{}{
"Name": r.protoRoom.Name,