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:
@@ -0,0 +1,92 @@
|
||||
// Copyright 2023 LiveKit, Inc.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package rtc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
"github.com/livekit/protocol/rpc"
|
||||
"github.com/livekit/psrpc"
|
||||
)
|
||||
|
||||
const (
|
||||
RoomAgentTopic = "room"
|
||||
PublisherAgentTopic = "publisher"
|
||||
)
|
||||
|
||||
type AgentClient interface {
|
||||
CheckEnabled(ctx context.Context, req *rpc.CheckEnabledRequest) *rpc.CheckEnabledResponse
|
||||
JobRequest(ctx context.Context, job *livekit.Job)
|
||||
}
|
||||
|
||||
type agentClient struct {
|
||||
client rpc.AgentInternalClient
|
||||
}
|
||||
|
||||
func NewAgentClient(bus psrpc.MessageBus) (AgentClient, error) {
|
||||
client, err := rpc.NewAgentInternalClient(bus)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &agentClient{client: client}, nil
|
||||
}
|
||||
|
||||
func (c *agentClient) CheckEnabled(ctx context.Context, req *rpc.CheckEnabledRequest) *rpc.CheckEnabledResponse {
|
||||
res := &rpc.CheckEnabledResponse{}
|
||||
resChan, err := c.client.CheckEnabled(ctx, req, psrpc.WithRequestTimeout(time.Second))
|
||||
if err != nil {
|
||||
return res
|
||||
}
|
||||
|
||||
for r := range resChan {
|
||||
if r.Err != nil {
|
||||
continue
|
||||
}
|
||||
if r.Result.RoomEnabled {
|
||||
res.RoomEnabled = true
|
||||
if res.PublisherEnabled {
|
||||
return res
|
||||
}
|
||||
}
|
||||
if r.Result.PublisherEnabled {
|
||||
res.PublisherEnabled = true
|
||||
if res.RoomEnabled {
|
||||
return res
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func (c *agentClient) JobRequest(ctx context.Context, job *livekit.Job) {
|
||||
var topic string
|
||||
var logError bool
|
||||
switch job.Type {
|
||||
case livekit.JobType_JT_ROOM:
|
||||
topic = RoomAgentTopic
|
||||
case livekit.JobType_JT_PUBLISHER:
|
||||
topic = PublisherAgentTopic
|
||||
logError = true
|
||||
}
|
||||
|
||||
_, err := c.client.JobRequest(ctx, topic, job)
|
||||
if err != nil && logError {
|
||||
logger.Warnw("agent job request failed", err)
|
||||
}
|
||||
}
|
||||
@@ -27,10 +27,6 @@ import (
|
||||
"github.com/livekit/protocol/webhook"
|
||||
)
|
||||
|
||||
type AgentClient interface {
|
||||
JobRequest(ctx context.Context, job *livekit.Job)
|
||||
}
|
||||
|
||||
type EgressLauncher interface {
|
||||
StartEgress(context.Context, *rpc.StartEgressRequest) (*livekit.EgressInfo, error)
|
||||
StartEgressWithClusterId(ctx context.Context, clusterId string, req *rpc.StartEgressRequest) (*livekit.EgressInfo, error)
|
||||
@@ -1034,6 +1034,13 @@ func (p *ParticipantImpl) IsRecorder() bool {
|
||||
return p.grants.Video.Recorder
|
||||
}
|
||||
|
||||
func (p *ParticipantImpl) IsAgent() bool {
|
||||
p.lock.RLock()
|
||||
defer p.lock.RUnlock()
|
||||
|
||||
return p.grants.Video.Agent
|
||||
}
|
||||
|
||||
func (p *ParticipantImpl) VerifySubscribeParticipantInfo(pID livekit.ParticipantID, version uint32) {
|
||||
if !p.IsReady() {
|
||||
// we have not sent a JoinResponse yet. metadata would be covered in JoinResponse
|
||||
|
||||
+47
-13
@@ -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,
|
||||
|
||||
@@ -264,6 +264,7 @@ type Participant interface {
|
||||
// permissions
|
||||
Hidden() bool
|
||||
IsRecorder() bool
|
||||
IsAgent() bool
|
||||
|
||||
Start()
|
||||
Close(sendLeave bool, reason ParticipantCloseReason, isExpectedToResume bool) error
|
||||
|
||||
@@ -408,6 +408,16 @@ type FakeLocalParticipant struct {
|
||||
identityReturnsOnCall map[int]struct {
|
||||
result1 livekit.ParticipantIdentity
|
||||
}
|
||||
IsAgentStub func() bool
|
||||
isAgentMutex sync.RWMutex
|
||||
isAgentArgsForCall []struct {
|
||||
}
|
||||
isAgentReturns struct {
|
||||
result1 bool
|
||||
}
|
||||
isAgentReturnsOnCall map[int]struct {
|
||||
result1 bool
|
||||
}
|
||||
IsClosedStub func() bool
|
||||
isClosedMutex sync.RWMutex
|
||||
isClosedArgsForCall []struct {
|
||||
@@ -2986,6 +2996,59 @@ func (fake *FakeLocalParticipant) IdentityReturnsOnCall(i int, result1 livekit.P
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeLocalParticipant) IsAgent() bool {
|
||||
fake.isAgentMutex.Lock()
|
||||
ret, specificReturn := fake.isAgentReturnsOnCall[len(fake.isAgentArgsForCall)]
|
||||
fake.isAgentArgsForCall = append(fake.isAgentArgsForCall, struct {
|
||||
}{})
|
||||
stub := fake.IsAgentStub
|
||||
fakeReturns := fake.isAgentReturns
|
||||
fake.recordInvocation("IsAgent", []interface{}{})
|
||||
fake.isAgentMutex.Unlock()
|
||||
if stub != nil {
|
||||
return stub()
|
||||
}
|
||||
if specificReturn {
|
||||
return ret.result1
|
||||
}
|
||||
return fakeReturns.result1
|
||||
}
|
||||
|
||||
func (fake *FakeLocalParticipant) IsAgentCallCount() int {
|
||||
fake.isAgentMutex.RLock()
|
||||
defer fake.isAgentMutex.RUnlock()
|
||||
return len(fake.isAgentArgsForCall)
|
||||
}
|
||||
|
||||
func (fake *FakeLocalParticipant) IsAgentCalls(stub func() bool) {
|
||||
fake.isAgentMutex.Lock()
|
||||
defer fake.isAgentMutex.Unlock()
|
||||
fake.IsAgentStub = stub
|
||||
}
|
||||
|
||||
func (fake *FakeLocalParticipant) IsAgentReturns(result1 bool) {
|
||||
fake.isAgentMutex.Lock()
|
||||
defer fake.isAgentMutex.Unlock()
|
||||
fake.IsAgentStub = nil
|
||||
fake.isAgentReturns = struct {
|
||||
result1 bool
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeLocalParticipant) IsAgentReturnsOnCall(i int, result1 bool) {
|
||||
fake.isAgentMutex.Lock()
|
||||
defer fake.isAgentMutex.Unlock()
|
||||
fake.IsAgentStub = nil
|
||||
if fake.isAgentReturnsOnCall == nil {
|
||||
fake.isAgentReturnsOnCall = make(map[int]struct {
|
||||
result1 bool
|
||||
})
|
||||
}
|
||||
fake.isAgentReturnsOnCall[i] = struct {
|
||||
result1 bool
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeLocalParticipant) IsClosed() bool {
|
||||
fake.isClosedMutex.Lock()
|
||||
ret, specificReturn := fake.isClosedReturnsOnCall[len(fake.isClosedArgsForCall)]
|
||||
@@ -6031,6 +6094,8 @@ func (fake *FakeLocalParticipant) Invocations() map[string][][]interface{} {
|
||||
defer fake.iDMutex.RUnlock()
|
||||
fake.identityMutex.RLock()
|
||||
defer fake.identityMutex.RUnlock()
|
||||
fake.isAgentMutex.RLock()
|
||||
defer fake.isAgentMutex.RUnlock()
|
||||
fake.isClosedMutex.RLock()
|
||||
defer fake.isClosedMutex.RUnlock()
|
||||
fake.isDisconnectedMutex.RLock()
|
||||
|
||||
@@ -118,6 +118,16 @@ type FakeParticipant struct {
|
||||
identityReturnsOnCall map[int]struct {
|
||||
result1 livekit.ParticipantIdentity
|
||||
}
|
||||
IsAgentStub func() bool
|
||||
isAgentMutex sync.RWMutex
|
||||
isAgentArgsForCall []struct {
|
||||
}
|
||||
isAgentReturns struct {
|
||||
result1 bool
|
||||
}
|
||||
isAgentReturnsOnCall map[int]struct {
|
||||
result1 bool
|
||||
}
|
||||
IsPublisherStub func() bool
|
||||
isPublisherMutex sync.RWMutex
|
||||
isPublisherArgsForCall []struct {
|
||||
@@ -780,6 +790,59 @@ func (fake *FakeParticipant) IdentityReturnsOnCall(i int, result1 livekit.Partic
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) IsAgent() bool {
|
||||
fake.isAgentMutex.Lock()
|
||||
ret, specificReturn := fake.isAgentReturnsOnCall[len(fake.isAgentArgsForCall)]
|
||||
fake.isAgentArgsForCall = append(fake.isAgentArgsForCall, struct {
|
||||
}{})
|
||||
stub := fake.IsAgentStub
|
||||
fakeReturns := fake.isAgentReturns
|
||||
fake.recordInvocation("IsAgent", []interface{}{})
|
||||
fake.isAgentMutex.Unlock()
|
||||
if stub != nil {
|
||||
return stub()
|
||||
}
|
||||
if specificReturn {
|
||||
return ret.result1
|
||||
}
|
||||
return fakeReturns.result1
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) IsAgentCallCount() int {
|
||||
fake.isAgentMutex.RLock()
|
||||
defer fake.isAgentMutex.RUnlock()
|
||||
return len(fake.isAgentArgsForCall)
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) IsAgentCalls(stub func() bool) {
|
||||
fake.isAgentMutex.Lock()
|
||||
defer fake.isAgentMutex.Unlock()
|
||||
fake.IsAgentStub = stub
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) IsAgentReturns(result1 bool) {
|
||||
fake.isAgentMutex.Lock()
|
||||
defer fake.isAgentMutex.Unlock()
|
||||
fake.IsAgentStub = nil
|
||||
fake.isAgentReturns = struct {
|
||||
result1 bool
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) IsAgentReturnsOnCall(i int, result1 bool) {
|
||||
fake.isAgentMutex.Lock()
|
||||
defer fake.isAgentMutex.Unlock()
|
||||
fake.IsAgentStub = nil
|
||||
if fake.isAgentReturnsOnCall == nil {
|
||||
fake.isAgentReturnsOnCall = make(map[int]struct {
|
||||
result1 bool
|
||||
})
|
||||
}
|
||||
fake.isAgentReturnsOnCall[i] = struct {
|
||||
result1 bool
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) IsPublisher() bool {
|
||||
fake.isPublisherMutex.Lock()
|
||||
ret, specificReturn := fake.isPublisherReturnsOnCall[len(fake.isPublisherArgsForCall)]
|
||||
@@ -1318,6 +1381,8 @@ func (fake *FakeParticipant) Invocations() map[string][][]interface{} {
|
||||
defer fake.iDMutex.RUnlock()
|
||||
fake.identityMutex.RLock()
|
||||
defer fake.identityMutex.RUnlock()
|
||||
fake.isAgentMutex.RLock()
|
||||
defer fake.isAgentMutex.RUnlock()
|
||||
fake.isPublisherMutex.RLock()
|
||||
defer fake.isPublisherMutex.RUnlock()
|
||||
fake.isRecorderMutex.RLock()
|
||||
|
||||
Reference in New Issue
Block a user