agent: release JobTerminate handler when job ends before registration (#4908)

* agent: release JobTerminate handler when job ends before registration

JobRequest registered the job's JobTerminate handler after AssignJob
returned, so an ended UpdateJobStatus or worker disconnect handled in
between deregistered nothing and the handler leaked. AssignJob also sent
the assignment before recording the job as running, so an immediate
ended update was dropped.

Record the job before sending the assignment, and after registering the
handler recheck under h.mu that the job is still running and its worker
still registered, deregistering otherwise.

Fixes #4901

* replace utils.CloneProto with proto.CloneOf

* agent: wait on worker readiness and poll for released handlers in test
This commit is contained in:
Paul Wells
2026-09-25 00:58:22 -07:00
committed by GitHub
parent f52b0e2bd5
commit 1ba2cadccd
28 changed files with 237 additions and 109 deletions
+110
View File
@@ -2,6 +2,7 @@ package agent_test
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"sync" "sync"
"testing" "testing"
@@ -256,3 +257,112 @@ func TestDrainConnectionsDoesNotDeadlock(t *testing.T) {
}) })
} }
} }
func TestJobTerminateHandlerReleased(t *testing.T) {
const jobCount = 50
requestJobs := func(t *testing.T, client rpc.AgentInternalClient, agentName func(i int) string) []string {
jobIDs := make([]string, jobCount)
errs := make([]error, jobCount)
var wg sync.WaitGroup
for i := range jobCount {
job := &livekit.Job{
Id: guid.New(guid.AgentJobPrefix),
DispatchId: guid.New(guid.AgentDispatchPrefix),
Type: livekit.JobType_JT_ROOM,
Room: &livekit.Room{},
AgentName: agentName(i),
}
jobIDs[i] = job.Id
wg.Add(1)
go func() {
defer wg.Done()
_, errs[i] = client.JobRequest(context.Background(), job.AgentName, agent.RoomAgentTopic, job)
}()
}
wg.Wait()
for _, err := range errs {
require.NoError(t, err)
}
return jobIDs
}
// the server reads the ping only after registering the worker's job request topic.
waitRegistered := func(t *testing.T, w *testutils.AgentWorker, agentName string) {
pongs := w.WorkerPongs.Observe()
defer pongs.Stop()
w.Register(agentName, livekit.JobType_JT_ROOM)
w.SendPing(&livekit.WorkerPing{Timestamp: time.Now().UnixMilli()})
select {
case <-pongs.Events():
case <-time.After(5 * time.Second):
require.Fail(t, "registration timeout")
}
}
// any response means a JobTerminate handler is still registered.
countAnswered := func(client rpc.AgentInternalClient, jobIDs []string) int32 {
var answered atomic.Int32
var wg sync.WaitGroup
for _, id := range jobIDs {
wg.Add(1)
go func() {
defer wg.Done()
_, err := client.JobTerminate(context.Background(), id, &rpc.JobTerminateRequest{JobId: id}, psrpc.WithRequestTimeout(200*time.Millisecond))
if !errors.Is(err, psrpc.ErrRequestTimedOut) && !errors.Is(err, psrpc.ErrNoResponse) {
answered.Inc()
}
}()
}
wg.Wait()
return answered.Load()
}
// handlers are released as the server processes the worker's messages; a leaked one never is.
requireHandlersReleased := func(t *testing.T, client rpc.AgentInternalClient, jobIDs []string) {
var answered int32
require.Eventually(t, func() bool {
answered = countAnswered(client, jobIDs)
return answered == 0
}, 10*time.Second, 100*time.Millisecond, "jobs with leaked JobTerminate handlers: %d", answered)
}
t.Run("job fails on assignment", func(t *testing.T) {
bus := psrpc.NewLocalMessageBus()
client := must.Get(rpc.NewAgentInternalClient(bus))
server := testutils.NewTestServer(bus)
t.Cleanup(server.Close)
var worker *testutils.AgentWorker
worker = server.SimulateAgentWorker(testutils.WithJobAssignmentHandler(func(j *livekit.Job) testutils.JobLoad {
worker.SendUpdateJob(&livekit.UpdateJobStatus{JobId: j.Id, Status: livekit.JobStatus_JS_FAILED})
return testutils.NewStableJobLoad(0)
}))
waitRegistered(t, worker, "fail_agent")
jobIDs := requestJobs(t, client, func(int) string { return "fail_agent" })
requireHandlersReleased(t, client, jobIDs)
})
t.Run("worker disconnects on assignment", func(t *testing.T) {
bus := psrpc.NewLocalMessageBus()
client := must.Get(rpc.NewAgentInternalClient(bus))
server := testutils.NewTestServer(bus)
t.Cleanup(server.Close)
agentName := func(i int) string { return fmt.Sprintf("disconnect_agent_%d", i) }
for i := range jobCount {
var worker *testutils.AgentWorker
worker = server.SimulateAgentWorker(testutils.WithJobAssignmentHandler(func(j *livekit.Job) testutils.JobLoad {
worker.Close()
return nil
}))
waitRegistered(t, worker, agentName(i))
}
jobIDs := requestJobs(t, client, agentName)
requireHandlersReleased(t, client, jobIDs)
})
}
+12 -9
View File
@@ -29,7 +29,6 @@ import (
"github.com/livekit/protocol/livekit" "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger" "github.com/livekit/protocol/logger"
"github.com/livekit/protocol/rpc" "github.com/livekit/protocol/rpc"
"github.com/livekit/protocol/utils"
"github.com/livekit/protocol/utils/guid" "github.com/livekit/protocol/utils/guid"
"github.com/livekit/psrpc" "github.com/livekit/psrpc"
) )
@@ -333,12 +332,12 @@ func (w *Worker) GetJobState(jobID livekit.JobID) (*livekit.JobState, error) {
if !ok { if !ok {
return nil, ErrJobNotFound return nil, ErrJobNotFound
} }
return utils.CloneProto(j.State), nil return proto.CloneOf(j.State), nil
} }
func (w *Worker) AssignJob(ctx context.Context, job *livekit.Job, hook AssignmentHook) (*livekit.JobState, error) { func (w *Worker) AssignJob(ctx context.Context, job *livekit.Job, hook AssignmentHook) (*livekit.JobState, error) {
availCh := make(chan *livekit.AvailabilityResponse, 1) availCh := make(chan *livekit.AvailabilityResponse, 1)
job = utils.CloneProto(job) job = proto.CloneOf(job)
jobID := livekit.JobID(job.Id) jobID := livekit.JobID(job.Id)
w.mu.Lock() w.mu.Lock()
@@ -420,16 +419,20 @@ func (w *Worker) AssignJob(ctx context.Context, job *livekit.Job, hook Assignmen
if hook != nil { if hook != nil {
send = hook(send) send = hook(send)
} }
state := proto.CloneOf(job.State)
// must be in runningJobs before the assignment is sent; stored as a copy because send reads job without w.mu.
w.mu.Lock()
w.runningJobs[jobID] = proto.CloneOf(job)
w.mu.Unlock()
if err := send(&livekit.JobAssignment{Job: job, Token: token}); err != nil { if err := send(&livekit.JobAssignment{Job: job, Token: token}); err != nil {
w.mu.Lock()
delete(w.runningJobs, jobID)
w.mu.Unlock()
return nil, err return nil, err
} }
state := utils.CloneProto(job.State)
w.mu.Lock()
w.runningJobs[jobID] = job
w.mu.Unlock()
// TODO sweep jobs that are never started. We can't do this until all SDKs actually update the the JOB state // TODO sweep jobs that are never started. We can't do this until all SDKs actually update the the JOB state
return state, nil return state, nil
@@ -20,7 +20,6 @@ import (
"github.com/livekit/livekit-server/pkg/utils" "github.com/livekit/livekit-server/pkg/utils"
"github.com/livekit/protocol/livekit" "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger" "github.com/livekit/protocol/logger"
protoutils "github.com/livekit/protocol/utils"
) )
type ConfigurationItem struct { type ConfigurationItem struct {
@@ -59,7 +58,7 @@ func (s *StaticClientConfigurationManager) GetConfiguration(clientInfo *livekit.
var conf *livekit.ClientConfiguration var conf *livekit.ClientConfiguration
for k, v := range matchedConf { for k, v := range matchedConf {
if k == 0 { if k == 0 {
conf = protoutils.CloneProto(matchedConf[0]) conf = proto.CloneOf(matchedConf[0])
} else { } else {
// TODO : there is a problem use protobuf merge, we don't have flag to indicate 'no value', // TODO : there is a problem use protobuf merge, we don't have flag to indicate 'no value',
// don't override default behavior or other configuration's field. So a bool value = false or // don't override default behavior or other configuration's field. So a bool value = false or
+5 -3
View File
@@ -19,6 +19,8 @@ import (
"sync" "sync"
"time" "time"
"google.golang.org/protobuf/proto"
"github.com/livekit/protocol/livekit" "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/utils" "github.com/livekit/protocol/utils"
"github.com/livekit/protocol/utils/guid" "github.com/livekit/protocol/utils/guid"
@@ -76,14 +78,14 @@ func NewLocalNode(conf *config.Config) (*LocalNodeImpl, error) {
} }
func NewLocalNodeFromNodeProto(node *livekit.Node) (*LocalNodeImpl, error) { func NewLocalNodeFromNodeProto(node *livekit.Node) (*LocalNodeImpl, error) {
return &LocalNodeImpl{node: utils.CloneProto(node)}, nil return &LocalNodeImpl{node: proto.CloneOf(node)}, nil
} }
func (l *LocalNodeImpl) Clone() *livekit.Node { func (l *LocalNodeImpl) Clone() *livekit.Node {
l.lock.RLock() l.lock.RLock()
defer l.lock.RUnlock() defer l.lock.RUnlock()
return utils.CloneProto(l.node) return proto.CloneOf(l.node)
} }
// for testing only // for testing only
@@ -134,7 +136,7 @@ func (l *LocalNodeImpl) SetStats(stats *livekit.NodeStats) {
l.lock.Lock() l.lock.Lock()
defer l.lock.Unlock() defer l.lock.Unlock()
l.node.Stats = utils.CloneProto(stats) l.node.Stats = proto.CloneOf(stats)
} }
func (l *LocalNodeImpl) UpdateNodeStats() bool { func (l *LocalNodeImpl) UpdateNodeStats() bool {
+3 -2
View File
@@ -18,9 +18,10 @@ import (
"sync" "sync"
"time" "time"
"google.golang.org/protobuf/proto"
"github.com/livekit/protocol/livekit" "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger" "github.com/livekit/protocol/logger"
"github.com/livekit/protocol/utils"
"github.com/livekit/livekit-server/pkg/config" "github.com/livekit/livekit-server/pkg/config"
"github.com/livekit/livekit-server/pkg/telemetry/prometheus" "github.com/livekit/livekit-server/pkg/telemetry/prometheus"
@@ -87,5 +88,5 @@ func (n *NodeStats) GetLatestNodeStats() *livekit.NodeStats {
defer n.lock.RUnlock() defer n.lock.RUnlock()
latestPtr := (n.statsHistoryWritePtr - 1 + len(n.statsHistory)) % len(n.statsHistory) latestPtr := (n.statsHistoryWritePtr - 1 + len(n.statsHistory)) % len(n.statsHistory)
return utils.CloneProto(n.statsHistory[latestPtr]) return proto.CloneOf(n.statsHistory[latestPtr])
} }
+2 -2
View File
@@ -20,13 +20,13 @@ import (
"sync" "sync"
"github.com/frostbyte73/core" "github.com/frostbyte73/core"
"google.golang.org/protobuf/proto"
"github.com/livekit/livekit-server/pkg/rtc/types" "github.com/livekit/livekit-server/pkg/rtc/types"
sfuutils "github.com/livekit/livekit-server/pkg/sfu/utils" sfuutils "github.com/livekit/livekit-server/pkg/sfu/utils"
"github.com/livekit/protocol/datatrack" "github.com/livekit/protocol/datatrack"
"github.com/livekit/protocol/livekit" "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger" "github.com/livekit/protocol/logger"
"github.com/livekit/protocol/utils"
) )
var ( var (
@@ -98,7 +98,7 @@ func (d *DataTrack) PublisherIdentity() livekit.ParticipantIdentity {
} }
func (d *DataTrack) ToProto() *livekit.DataTrackInfo { func (d *DataTrack) ToProto() *livekit.DataTrackInfo {
return utils.CloneProto(d.dti) return proto.CloneOf(d.dti)
} }
func (d *DataTrack) PubHandle() uint16 { func (d *DataTrack) PubHandle() uint16 {
+7 -8
View File
@@ -30,7 +30,6 @@ import (
"github.com/livekit/protocol/codecs/mime" "github.com/livekit/protocol/codecs/mime"
"github.com/livekit/protocol/livekit" "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger" "github.com/livekit/protocol/logger"
"github.com/livekit/protocol/utils"
"github.com/livekit/livekit-server/pkg/rtc/types" "github.com/livekit/livekit-server/pkg/rtc/types"
"github.com/livekit/livekit-server/pkg/sfu" "github.com/livekit/livekit-server/pkg/sfu"
@@ -154,7 +153,7 @@ func NewMediaTrackReceiver(params MediaTrackReceiverParams, ti *livekit.TrackInf
params: params, params: params,
state: mediaTrackReceiverStateOpen, state: mediaTrackReceiverStateOpen,
} }
t.trackInfo.Store(utils.CloneProto(ti)) t.trackInfo.Store(proto.CloneOf(ti))
t.MediaTrackSubscriptions = NewMediaTrackSubscriptions(MediaTrackSubscriptionsParams{ t.MediaTrackSubscriptions = NewMediaTrackSubscriptions(MediaTrackSubscriptionsParams{
MediaTrack: params.MediaTrack, MediaTrack: params.MediaTrack,
@@ -812,7 +811,7 @@ func (t *MediaTrackReceiver) UpdateCodecInfo(codecs []*livekit.SimulcastCodec) {
if len(c.Layers) != 0 { if len(c.Layers) != 0 {
clonedLayers := make([]*livekit.VideoLayer, 0, len(c.Layers)) clonedLayers := make([]*livekit.VideoLayer, 0, len(c.Layers))
for _, l := range c.Layers { for _, l := range c.Layers {
clonedLayers = append(clonedLayers, utils.CloneProto(l)) clonedLayers = append(clonedLayers, proto.CloneOf(l))
} }
origin.Layers = clonedLayers origin.Layers = clonedLayers
@@ -871,7 +870,7 @@ func (t *MediaTrackReceiver) UpdateCodecRids(mimeType mime.MimeType, rids buffer
} }
func (t *MediaTrackReceiver) UpdateTrackInfo(ti *livekit.TrackInfo) { func (t *MediaTrackReceiver) UpdateTrackInfo(ti *livekit.TrackInfo) {
clonedInfo := utils.CloneProto(ti) clonedInfo := proto.CloneOf(ti)
t.lock.Lock() t.lock.Lock()
trackInfo := t.TrackInfo() trackInfo := t.TrackInfo()
@@ -924,7 +923,7 @@ func (t *MediaTrackReceiver) UpdateAudioTrack(update *livekit.UpdateLocalAudioTr
t.lock.Lock() t.lock.Lock()
trackInfo := t.TrackInfo() trackInfo := t.TrackInfo()
clonedInfo := utils.CloneProto(trackInfo) clonedInfo := proto.CloneOf(trackInfo)
clonedInfo.AudioFeatures = sutils.DedupeSlice(update.Features) clonedInfo.AudioFeatures = sutils.DedupeSlice(update.Features)
@@ -960,7 +959,7 @@ func (t *MediaTrackReceiver) UpdateVideoTrack(update *livekit.UpdateLocalVideoTr
t.lock.Lock() t.lock.Lock()
trackInfo := t.TrackInfo() trackInfo := t.TrackInfo()
clonedInfo := utils.CloneProto(trackInfo) clonedInfo := proto.CloneOf(trackInfo)
clonedInfo.Width = update.Width clonedInfo.Width = update.Width
clonedInfo.Height = update.Height clonedInfo.Height = update.Height
if proto.Equal(trackInfo, clonedInfo) { if proto.Equal(trackInfo, clonedInfo) {
@@ -981,7 +980,7 @@ func (t *MediaTrackReceiver) UpdateVideoSize(mimeType mime.MimeType, sizes []cod
var changed bool var changed bool
t.lock.Lock() t.lock.Lock()
trackInfo := t.TrackInfo() trackInfo := t.TrackInfo()
clonedInfo := utils.CloneProto(trackInfo) clonedInfo := proto.CloneOf(trackInfo)
var maxWidth, maxHeight uint32 var maxWidth, maxHeight uint32
for _, size := range sizes { for _, size := range sizes {
if size.Width > maxWidth { if size.Width > maxWidth {
@@ -1028,7 +1027,7 @@ func (t *MediaTrackReceiver) TrackInfo() *livekit.TrackInfo {
} }
func (t *MediaTrackReceiver) TrackInfoClone() *livekit.TrackInfo { func (t *MediaTrackReceiver) TrackInfoClone() *livekit.TrackInfo {
return utils.CloneProto(t.TrackInfo()) return proto.CloneOf(t.TrackInfo())
} }
func (t *MediaTrackReceiver) NotifyMaxLayerChange(mimeType mime.MimeType, maxLayer int32) { func (t *MediaTrackReceiver) NotifyMaxLayerChange(mimeType mime.MimeType, maxLayer int32) {
+18 -18
View File
@@ -667,7 +667,7 @@ func (p *ParticipantImpl) GetClientInfo() *livekit.ClientInfo {
func (p *ParticipantImpl) GetClientConfiguration() *livekit.ClientConfiguration { func (p *ParticipantImpl) GetClientConfiguration() *livekit.ClientConfiguration {
p.lock.RLock() p.lock.RLock()
defer p.lock.RUnlock() defer p.lock.RUnlock()
return utils.CloneProto(p.params.ClientConf) return proto.CloneOf(p.params.ClientConf)
} }
func (p *ParticipantImpl) GetBufferFactory() *buffer.Factory { func (p *ParticipantImpl) GetBufferFactory() *buffer.Factory {
@@ -709,7 +709,7 @@ func (p *ParticipantImpl) UpdateMetadata(update *livekit.UpdateParticipantMetada
sendRequestResponse := func() error { sendRequestResponse := func() error {
if !fromAdmin || (update.RequestId != 0 || err != nil) { if !fromAdmin || (update.RequestId != 0 || err != nil) {
requestResponse.Request = &livekit.RequestResponse_UpdateMetadata{ requestResponse.Request = &livekit.RequestResponse_UpdateMetadata{
UpdateMetadata: utils.CloneProto(update), UpdateMetadata: proto.CloneOf(update),
} }
p.sendRequestResponse(requestResponse) p.sendRequestResponse(requestResponse)
} }
@@ -985,7 +985,7 @@ func (p *ParticipantImpl) ToProtoWithVersion() (*livekit.ParticipantInfo, utils.
} }
if !found { if !found {
pi.Tracks = append(pi.Tracks, utils.CloneProto(pti.trackInfos[0])) pi.Tracks = append(pi.Tracks, proto.CloneOf(pti.trackInfos[0]))
} }
} }
@@ -1195,7 +1195,7 @@ func (p *ParticipantImpl) HandleICETrickle(trickleRequest *livekit.TrickleReques
Reason: livekit.RequestResponse_UNCLASSIFIED_ERROR, Reason: livekit.RequestResponse_UNCLASSIFIED_ERROR,
Message: err.Error(), Message: err.Error(),
Request: &livekit.RequestResponse_Trickle{ Request: &livekit.RequestResponse_Trickle{
Trickle: utils.CloneProto(trickleRequest), Trickle: proto.CloneOf(trickleRequest),
}, },
}) })
return return
@@ -1361,7 +1361,7 @@ func (p *ParticipantImpl) AddTrack(req *livekit.AddTrackRequest) {
p.sendRequestResponse(&livekit.RequestResponse{ p.sendRequestResponse(&livekit.RequestResponse{
Reason: livekit.RequestResponse_NOT_ALLOWED, Reason: livekit.RequestResponse_NOT_ALLOWED,
Request: &livekit.RequestResponse_AddTrack{ Request: &livekit.RequestResponse_AddTrack{
AddTrack: utils.CloneProto(req), AddTrack: proto.CloneOf(req),
}, },
}) })
return return
@@ -1372,7 +1372,7 @@ func (p *ParticipantImpl) AddTrack(req *livekit.AddTrackRequest) {
p.sendRequestResponse(&livekit.RequestResponse{ p.sendRequestResponse(&livekit.RequestResponse{
Reason: livekit.RequestResponse_UNSUPPORTED_TYPE, Reason: livekit.RequestResponse_UNSUPPORTED_TYPE,
Request: &livekit.RequestResponse_AddTrack{ Request: &livekit.RequestResponse_AddTrack{
AddTrack: utils.CloneProto(req), AddTrack: proto.CloneOf(req),
}, },
}) })
return return
@@ -2948,7 +2948,7 @@ func (p *ParticipantImpl) addPendingTrack(req *livekit.AddTrackRequest) *livekit
p.sendRequestResponse(&livekit.RequestResponse{ p.sendRequestResponse(&livekit.RequestResponse{
Reason: livekit.RequestResponse_NOT_FOUND, Reason: livekit.RequestResponse_NOT_FOUND,
Request: &livekit.RequestResponse_AddTrack{ Request: &livekit.RequestResponse_AddTrack{
AddTrack: utils.CloneProto(req), AddTrack: proto.CloneOf(req),
}, },
}) })
p.pendingTracksLock.Unlock() p.pendingTracksLock.Unlock()
@@ -2967,7 +2967,7 @@ func (p *ParticipantImpl) addPendingTrack(req *livekit.AddTrackRequest) *livekit
Reason: livekit.RequestResponse_LIMIT_EXCEEDED, Reason: livekit.RequestResponse_LIMIT_EXCEEDED,
Message: fmt.Sprintf("too many pending tracks, rejecting new track, numPendingTracks: %d", len(p.pendingTracks)), Message: fmt.Sprintf("too many pending tracks, rejecting new track, numPendingTracks: %d", len(p.pendingTracks)),
Request: &livekit.RequestResponse_AddTrack{ Request: &livekit.RequestResponse_AddTrack{
AddTrack: utils.CloneProto(req), AddTrack: proto.CloneOf(req),
}, },
}) })
p.pendingTracksLock.Unlock() p.pendingTracksLock.Unlock()
@@ -2989,7 +2989,7 @@ func (p *ParticipantImpl) addPendingTrack(req *livekit.AddTrackRequest) *livekit
clonedLayers := make([]*livekit.VideoLayer, 0, len(layers)) clonedLayers := make([]*livekit.VideoLayer, 0, len(layers))
for _, l := range layers { for _, l := range layers {
clonedLayers = append(clonedLayers, utils.CloneProto(l)) clonedLayers = append(clonedLayers, proto.CloneOf(l))
} }
slices.SortFunc(clonedLayers, func(i, j *livekit.VideoLayer) int { slices.SortFunc(clonedLayers, func(i, j *livekit.VideoLayer) int {
return int(i.Quality) - int(j.Quality) return int(i.Quality) - int(j.Quality)
@@ -3157,7 +3157,7 @@ func (p *ParticipantImpl) addPendingTrack(req *livekit.AddTrackRequest) *livekit
Reason: livekit.RequestResponse_LIMIT_EXCEEDED, Reason: livekit.RequestResponse_LIMIT_EXCEEDED,
Message: fmt.Sprintf("too many pending queued tracks, rejecting new track, numPendingTracksQueued: %d", len(p.pendingTracks[req.Cid].trackInfos)), Message: fmt.Sprintf("too many pending queued tracks, rejecting new track, numPendingTracksQueued: %d", len(p.pendingTracks[req.Cid].trackInfos)),
Request: &livekit.RequestResponse_AddTrack{ Request: &livekit.RequestResponse_AddTrack{
AddTrack: utils.CloneProto(req), AddTrack: proto.CloneOf(req),
}, },
}) })
p.pendingTracksLock.Unlock() p.pendingTracksLock.Unlock()
@@ -3175,7 +3175,7 @@ func (p *ParticipantImpl) addPendingTrack(req *livekit.AddTrackRequest) *livekit
p.sendRequestResponse(&livekit.RequestResponse{ p.sendRequestResponse(&livekit.RequestResponse{
Reason: livekit.RequestResponse_QUEUED, Reason: livekit.RequestResponse_QUEUED,
Request: &livekit.RequestResponse_AddTrack{ Request: &livekit.RequestResponse_AddTrack{
AddTrack: utils.CloneProto(req), AddTrack: proto.CloneOf(req),
}, },
}) })
@@ -3185,7 +3185,7 @@ func (p *ParticipantImpl) addPendingTrack(req *livekit.AddTrackRequest) *livekit
} }
p.pendingTracksLock.Unlock() p.pendingTracksLock.Unlock()
p.GetTelemetryListener().OnTrackPublishRequested(p.ID(), p.Identity(), utils.CloneProto(ti), true) p.GetTelemetryListener().OnTrackPublishRequested(p.ID(), p.Identity(), proto.CloneOf(ti), true)
return nil return nil
} }
@@ -3210,7 +3210,7 @@ func (p *ParticipantImpl) addPendingTrack(req *livekit.AddTrackRequest) *livekit
} }
p.pendingTracksLock.Unlock() p.pendingTracksLock.Unlock()
p.GetTelemetryListener().OnTrackPublishRequested(p.ID(), p.Identity(), utils.CloneProto(ti), true) p.GetTelemetryListener().OnTrackPublishRequested(p.ID(), p.Identity(), proto.CloneOf(ti), true)
return ti return ti
} }
@@ -3262,7 +3262,7 @@ func (p *ParticipantImpl) setTrackMuted(mute *livekit.MuteTrackRequest, fromAdmi
for _, pti := range p.pendingTracks { for _, pti := range p.pendingTracks {
for i, ti := range pti.trackInfos { for i, ti := range pti.trackInfos {
if livekit.TrackID(ti.Sid) == trackID { if livekit.TrackID(ti.Sid) == trackID {
ti = utils.CloneProto(ti) ti = proto.CloneOf(ti)
changed = changed || ti.Muted != mute.Muted changed = changed || ti.Muted != mute.Muted
ti.Muted = mute.Muted ti.Muted = mute.Muted
pti.trackInfos[i] = ti pti.trackInfos[i] = ti
@@ -3286,7 +3286,7 @@ func (p *ParticipantImpl) setTrackMuted(mute *livekit.MuteTrackRequest, fromAdmi
p.sendRequestResponse(&livekit.RequestResponse{ p.sendRequestResponse(&livekit.RequestResponse{
Reason: livekit.RequestResponse_NOT_FOUND, Reason: livekit.RequestResponse_NOT_FOUND,
Request: &livekit.RequestResponse_Mute{ Request: &livekit.RequestResponse_Mute{
Mute: utils.CloneProto(mute), Mute: proto.CloneOf(mute),
}, },
}) })
} }
@@ -3714,7 +3714,7 @@ func (p *ParticipantImpl) getPendingTrack(clientId string, kind livekit.TrackTyp
return signalCid, nil, buffer.VideoLayersRid{}, false, time.Time{} return signalCid, nil, buffer.VideoLayersRid{}, false, time.Time{}
} }
return signalCid, utils.CloneProto(pendingInfo.trackInfos[0]), pendingInfo.sdpRids, pendingInfo.migrated, pendingInfo.createdAt return signalCid, proto.CloneOf(pendingInfo.trackInfos[0]), pendingInfo.sdpRids, pendingInfo.migrated, pendingInfo.createdAt
} }
func (p *ParticipantImpl) getPendingTrackPrimaryBySdpCid(sdpCid string) *pendingTrackInfo { func (p *ParticipantImpl) getPendingTrackPrimaryBySdpCid(sdpCid string) *pendingTrackInfo {
@@ -4231,7 +4231,7 @@ func (p *ParticipantImpl) UpdateAudioTrack(update *livekit.UpdateLocalAudioTrack
p.sendRequestResponse(&livekit.RequestResponse{ p.sendRequestResponse(&livekit.RequestResponse{
Reason: livekit.RequestResponse_NOT_FOUND, Reason: livekit.RequestResponse_NOT_FOUND,
Request: &livekit.RequestResponse_UpdateAudioTrack{ Request: &livekit.RequestResponse_UpdateAudioTrack{
UpdateAudioTrack: utils.CloneProto(update), UpdateAudioTrack: proto.CloneOf(update),
}, },
}) })
return errors.New("could not find track") return errors.New("could not find track")
@@ -4263,7 +4263,7 @@ func (p *ParticipantImpl) UpdateVideoTrack(update *livekit.UpdateLocalVideoTrack
p.sendRequestResponse(&livekit.RequestResponse{ p.sendRequestResponse(&livekit.RequestResponse{
Reason: livekit.RequestResponse_NOT_FOUND, Reason: livekit.RequestResponse_NOT_FOUND,
Request: &livekit.RequestResponse_UpdateVideoTrack{ Request: &livekit.RequestResponse_UpdateVideoTrack{
UpdateVideoTrack: utils.CloneProto(update), UpdateVideoTrack: proto.CloneOf(update),
}, },
}) })
return errors.New("could not find track") return errors.New("could not find track")
+10 -8
View File
@@ -15,6 +15,8 @@
package rtc package rtc
import ( import (
"google.golang.org/protobuf/proto"
"github.com/livekit/livekit-server/pkg/rtc/types" "github.com/livekit/livekit-server/pkg/rtc/types"
"github.com/livekit/protocol/datatrack" "github.com/livekit/protocol/datatrack"
"github.com/livekit/protocol/livekit" "github.com/livekit/protocol/livekit"
@@ -30,7 +32,7 @@ func (p *ParticipantImpl) HandlePublishDataTrackRequest(req *livekit.PublishData
Reason: livekit.RequestResponse_NOT_ALLOWED, Reason: livekit.RequestResponse_NOT_ALLOWED,
Message: "does not have permission to publish data", Message: "does not have permission to publish data",
Request: &livekit.RequestResponse_PublishDataTrack{ Request: &livekit.RequestResponse_PublishDataTrack{
PublishDataTrack: utils.CloneProto(req), PublishDataTrack: proto.CloneOf(req),
}, },
}) })
return return
@@ -42,7 +44,7 @@ func (p *ParticipantImpl) HandlePublishDataTrackRequest(req *livekit.PublishData
Reason: livekit.RequestResponse_INVALID_HANDLE, Reason: livekit.RequestResponse_INVALID_HANDLE,
Message: "handle should be > 0 AND < 65536", Message: "handle should be > 0 AND < 65536",
Request: &livekit.RequestResponse_PublishDataTrack{ Request: &livekit.RequestResponse_PublishDataTrack{
PublishDataTrack: utils.CloneProto(req), PublishDataTrack: proto.CloneOf(req),
}, },
}) })
return return
@@ -54,7 +56,7 @@ func (p *ParticipantImpl) HandlePublishDataTrackRequest(req *livekit.PublishData
Reason: livekit.RequestResponse_INVALID_NAME, Reason: livekit.RequestResponse_INVALID_NAME,
Message: "name should not be empty and should not exceed 256 characters", Message: "name should not be empty and should not exceed 256 characters",
Request: &livekit.RequestResponse_PublishDataTrack{ Request: &livekit.RequestResponse_PublishDataTrack{
PublishDataTrack: utils.CloneProto(req), PublishDataTrack: proto.CloneOf(req),
}, },
}) })
return return
@@ -67,7 +69,7 @@ func (p *ParticipantImpl) HandlePublishDataTrackRequest(req *livekit.PublishData
Reason: livekit.RequestResponse_INVALID_REQUEST, Reason: livekit.RequestResponse_INVALID_REQUEST,
Message: "encoding identifier is empty or exceeds the maximum length", Message: "encoding identifier is empty or exceeds the maximum length",
Request: &livekit.RequestResponse_PublishDataTrack{ Request: &livekit.RequestResponse_PublishDataTrack{
PublishDataTrack: utils.CloneProto(req), PublishDataTrack: proto.CloneOf(req),
}, },
}) })
return return
@@ -95,7 +97,7 @@ func (p *ParticipantImpl) HandlePublishDataTrackRequest(req *livekit.PublishData
Reason: reason, Reason: reason,
Message: message, Message: message,
Request: &livekit.RequestResponse_PublishDataTrack{ Request: &livekit.RequestResponse_PublishDataTrack{
PublishDataTrack: utils.CloneProto(req), PublishDataTrack: proto.CloneOf(req),
}, },
}) })
return return
@@ -108,8 +110,8 @@ func (p *ParticipantImpl) HandlePublishDataTrackRequest(req *livekit.PublishData
Name: req.Name, Name: req.Name,
Encryption: req.Encryption, Encryption: req.Encryption,
} }
dti.FrameEncoding = utils.CloneProto(req.GetFrameEncoding()) dti.FrameEncoding = proto.CloneOf(req.GetFrameEncoding())
dti.Schema = utils.CloneProto(req.GetSchema()) dti.Schema = proto.CloneOf(req.GetSchema())
dt := NewDataTrack( dt := NewDataTrack(
DataTrackParams{ DataTrackParams{
Logger: p.params.Logger.WithValues("trackID", dti.Sid), Logger: p.params.Logger.WithValues("trackID", dti.Sid),
@@ -143,7 +145,7 @@ func (p *ParticipantImpl) HandleUnpublishDataTrackRequest(req *livekit.Unpublish
p.sendRequestResponse(&livekit.RequestResponse{ p.sendRequestResponse(&livekit.RequestResponse{
Reason: livekit.RequestResponse_NOT_FOUND, Reason: livekit.RequestResponse_NOT_FOUND,
Request: &livekit.RequestResponse_UnpublishDataTrack{ Request: &livekit.RequestResponse_UnpublishDataTrack{
UnpublishDataTrack: utils.CloneProto(req), UnpublishDataTrack: proto.CloneOf(req),
}, },
}) })
return return
+1 -1
View File
@@ -560,7 +560,7 @@ func TestPreferMediaCodecForPublisher(t *testing.T) {
for i := 0; i < 2; i++ { for i := 0; i < 2; i++ {
// publish preferred track without client using setCodecPreferences() // publish preferred track without client using setCodecPreferences()
trackCid := fmt.Sprintf("%s-%d", tc.trackBaseCid, i) trackCid := fmt.Sprintf("%s-%d", tc.trackBaseCid, i)
req := utils.CloneProto(tc.addTrack) req := proto.CloneOf(tc.addTrack)
req.SimulcastCodecs = []*livekit.SimulcastCodec{ req.SimulcastCodecs = []*livekit.SimulcastCodec{
{ {
Codec: tc.preferredCodec, Codec: tc.preferredCodec,
+2 -2
View File
@@ -22,13 +22,13 @@ import (
"github.com/pion/sdp/v3" "github.com/pion/sdp/v3"
"github.com/pion/webrtc/v4" "github.com/pion/webrtc/v4"
"google.golang.org/protobuf/proto"
"github.com/livekit/livekit-server/pkg/rtc/types" "github.com/livekit/livekit-server/pkg/rtc/types"
"github.com/livekit/protocol/codecs/mime" "github.com/livekit/protocol/codecs/mime"
"github.com/livekit/protocol/livekit" "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger" "github.com/livekit/protocol/logger"
lksdp "github.com/livekit/protocol/sdp" lksdp "github.com/livekit/protocol/sdp"
"github.com/livekit/protocol/utils"
) )
func (p *ParticipantImpl) populateSdpCid(parsedOffer *sdp.SessionDescription) ([]*sdp.MediaDescription, []*sdp.MediaDescription) { func (p *ParticipantImpl) populateSdpCid(parsedOffer *sdp.SessionDescription) ([]*sdp.MediaDescription, []*sdp.MediaDescription) {
@@ -143,7 +143,7 @@ func (p *ParticipantImpl) populateSdpCid(parsedOffer *sdp.SessionDescription) ([
} }
if updated { if updated {
p.pendingTracks[signalCid].trackInfos[0] = utils.CloneProto(info) p.pendingTracks[signalCid].trackInfos[0] = proto.CloneOf(info)
p.pubLogger.Debugw( p.pubLogger.Debugw(
"pending track SDP cid updated", "pending track SDP cid updated",
"signalCid", signalCid, "signalCid", signalCid,
+4 -4
View File
@@ -254,7 +254,7 @@ func NewRoom(
egressLauncher EgressLauncher, egressLauncher EgressLauncher,
) *Room { ) *Room {
r := &Room{ r := &Room{
protoRoom: utils.CloneProto(room), protoRoom: proto.CloneOf(room),
internal: internal, internal: internal,
logger: LoggerWithRoom( logger: LoggerWithRoom(
logger.GetLogger().WithComponent(sutils.ComponentRoom), logger.GetLogger().WithComponent(sutils.ComponentRoom),
@@ -874,7 +874,7 @@ func (r *Room) GetAgentDispatches(dispatchID string) ([]*livekit.AgentDispatch,
for _, ad := range r.agentDispatches { for _, ad := range r.agentDispatches {
if dispatchID == "" || ad.Id == dispatchID { if dispatchID == "" || ad.Id == dispatchID {
ret = append(ret, utils.CloneProto(ad.AgentDispatch)) ret = append(ret, proto.CloneOf(ad.AgentDispatch))
} }
} }
@@ -1587,7 +1587,7 @@ func (r *Room) sendSpeakerChanges(speakers []*livekit.SpeakerInfo) {
func (r *Room) updateProto() *livekit.Room { func (r *Room) updateProto() *livekit.Room {
r.lock.RLock() r.lock.RLock()
room := utils.CloneProto(r.protoRoom) room := proto.CloneOf(r.protoRoom)
r.lock.RUnlock() r.lock.RUnlock()
room.NumPublishers = 0 room.NumPublishers = 0
@@ -1658,7 +1658,7 @@ func (r *Room) audioUpdateWorker() {
// changedSpeakers need to include previous speakers that are no longer speaking // changedSpeakers need to include previous speakers that are no longer speaking
for sid, speaker := range lastActiveMap { for sid, speaker := range lastActiveMap {
if nextActiveMap[sid] == nil { if nextActiveMap[sid] == nil {
inactiveSpeaker := utils.CloneProto(speaker) inactiveSpeaker := proto.CloneOf(speaker)
inactiveSpeaker.Level = 0 inactiveSpeaker.Level = 0
inactiveSpeaker.Active = false inactiveSpeaker.Active = false
changedSpeakers = append(changedSpeakers, inactiveSpeaker) changedSpeakers = append(changedSpeakers, inactiveSpeaker)
+7 -8
View File
@@ -24,7 +24,6 @@ import (
"github.com/livekit/protocol/auth/authfakes" "github.com/livekit/protocol/auth/authfakes"
"github.com/livekit/protocol/livekit" "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/utils"
"github.com/livekit/protocol/webhook" "github.com/livekit/protocol/webhook"
"github.com/livekit/livekit-server/version" "github.com/livekit/livekit-server/version"
@@ -291,7 +290,7 @@ func TestPushAndDequeueUpdates(t *testing.T) {
{ {
name: "last version is enqueued", name: "last version is enqueued",
pi: subscriber1v2, pi: subscriber1v2,
existing: &ParticipantUpdate{ParticipantInfo: utils.CloneProto(subscriber1v1)}, // clone the existing value since it can be modified when setting to disconnected existing: &ParticipantUpdate{ParticipantInfo: proto.CloneOf(subscriber1v1)}, // clone the existing value since it can be modified when setting to disconnected
validate: func(t *testing.T, rm *Room, _ []*ParticipantUpdate) { validate: func(t *testing.T, rm *Room, _ []*ParticipantUpdate) {
queued := rm.batchedUpdates[livekit.ParticipantIdentity(identity)] queued := rm.batchedUpdates[livekit.ParticipantIdentity(identity)]
require.NotNil(t, queued) require.NotNil(t, queued)
@@ -301,7 +300,7 @@ func TestPushAndDequeueUpdates(t *testing.T) {
{ {
name: "latest version when immediate", name: "latest version when immediate",
pi: subscriber1v2, pi: subscriber1v2,
existing: &ParticipantUpdate{ParticipantInfo: utils.CloneProto(subscriber1v1)}, existing: &ParticipantUpdate{ParticipantInfo: proto.CloneOf(subscriber1v1)},
immediate: true, immediate: true,
expected: []*ParticipantUpdate{{ParticipantInfo: subscriber1v2}}, expected: []*ParticipantUpdate{{ParticipantInfo: subscriber1v2}},
validate: func(t *testing.T, rm *Room, _ []*ParticipantUpdate) { validate: func(t *testing.T, rm *Room, _ []*ParticipantUpdate) {
@@ -312,7 +311,7 @@ func TestPushAndDequeueUpdates(t *testing.T) {
{ {
name: "out of order updates are rejected", name: "out of order updates are rejected",
pi: subscriber1v1, pi: subscriber1v1,
existing: &ParticipantUpdate{ParticipantInfo: utils.CloneProto(subscriber1v2)}, existing: &ParticipantUpdate{ParticipantInfo: proto.CloneOf(subscriber1v2)},
validate: func(t *testing.T, rm *Room, updates []*ParticipantUpdate) { validate: func(t *testing.T, rm *Room, updates []*ParticipantUpdate) {
queued := rm.batchedUpdates[livekit.ParticipantIdentity(identity)] queued := rm.batchedUpdates[livekit.ParticipantIdentity(identity)]
requirePIEquals(t, subscriber1v2, queued.ParticipantInfo) requirePIEquals(t, subscriber1v2, queued.ParticipantInfo)
@@ -322,7 +321,7 @@ func TestPushAndDequeueUpdates(t *testing.T) {
name: "sid change is broadcasted immediately with synthsized disconnect", name: "sid change is broadcasted immediately with synthsized disconnect",
pi: publisher2, pi: publisher2,
closeReason: types.ParticipantCloseReasonServiceRequestRemoveParticipant, // just to test if update contain the close reason closeReason: types.ParticipantCloseReasonServiceRequestRemoveParticipant, // just to test if update contain the close reason
existing: &ParticipantUpdate{ParticipantInfo: utils.CloneProto(subscriber1v2), CloseReason: types.ParticipantCloseReasonStale}, existing: &ParticipantUpdate{ParticipantInfo: proto.CloneOf(subscriber1v2), CloseReason: types.ParticipantCloseReasonStale},
expected: []*ParticipantUpdate{ expected: []*ParticipantUpdate{
{ {
ParticipantInfo: &livekit.ParticipantInfo{ ParticipantInfo: &livekit.ParticipantInfo{
@@ -340,7 +339,7 @@ func TestPushAndDequeueUpdates(t *testing.T) {
{ {
name: "when switching to publisher, queue is cleared", name: "when switching to publisher, queue is cleared",
pi: publisher1v2, pi: publisher1v2,
existing: &ParticipantUpdate{ParticipantInfo: utils.CloneProto(subscriber1v1)}, existing: &ParticipantUpdate{ParticipantInfo: proto.CloneOf(subscriber1v1)},
expected: []*ParticipantUpdate{{ParticipantInfo: publisher1v2}}, expected: []*ParticipantUpdate{{ParticipantInfo: publisher1v2}},
validate: func(t *testing.T, rm *Room, updates []*ParticipantUpdate) { validate: func(t *testing.T, rm *Room, updates []*ParticipantUpdate) {
require.Empty(t, rm.batchedUpdates) require.Empty(t, rm.batchedUpdates)
@@ -651,7 +650,7 @@ func TestDataChannel(t *testing.T) {
} }
setSource(mode, packet, p) setSource(mode, packet, p)
packetExp := utils.CloneProto(packet) packetExp := proto.CloneOf(packet)
if mode != legacySID { if mode != legacySID {
packetExp.ParticipantIdentity = string(p.Identity()) packetExp.ParticipantIdentity = string(p.Identity())
packetExp.GetUser().ParticipantIdentity = string(p.Identity()) packetExp.GetUser().ParticipantIdentity = string(p.Identity())
@@ -699,7 +698,7 @@ func TestDataChannel(t *testing.T) {
setSource(mode, packet, p) setSource(mode, packet, p)
setDest(mode, packet, p1) setDest(mode, packet, p1)
packetExp := utils.CloneProto(packet) packetExp := proto.CloneOf(packet)
if mode != legacySID { if mode != legacySID {
packetExp.ParticipantIdentity = string(p.Identity()) packetExp.ParticipantIdentity = string(p.Identity())
packetExp.GetUser().ParticipantIdentity = string(p.Identity()) packetExp.GetUser().ParticipantIdentity = string(p.Identity())
+1 -1
View File
@@ -323,7 +323,7 @@ func (t *SubscribedTrack) UpdateSubscriberSettings(settings *livekit.UpdateTrack
} }
isImmediate = isImmediate || (!settings.Disabled && settings.Disabled != t.isMutedLocked()) isImmediate = isImmediate || (!settings.Disabled && settings.Disabled != t.isMutedLocked())
t.settings = utils.CloneProto(settings) t.settings = proto.CloneOf(settings)
t.logger.Debugw("saving subscriber track settings", "settings", logger.Proto(t.settings)) t.logger.Debugw("saving subscriber track settings", "settings", logger.Proto(t.settings))
t.settingsLock.Unlock() t.settingsLock.Unlock()
+1 -1
View File
@@ -675,7 +675,7 @@ func (t *TransportManager) GetICEConfig() *livekit.ICEConfig {
if t.iceConfig == nil { if t.iceConfig == nil {
return nil return nil
} }
return utils.CloneProto(t.iceConfig) return proto.CloneOf(t.iceConfig)
} }
func (t *TransportManager) resetTransportConfigureLocked(reconfigured bool) { func (t *TransportManager) resetTransportConfigureLocked(reconfigured bool) {
+3 -2
View File
@@ -18,13 +18,14 @@ import (
"context" "context"
"fmt" "fmt"
"google.golang.org/protobuf/proto"
"github.com/livekit/livekit-server/pkg/config" "github.com/livekit/livekit-server/pkg/config"
"github.com/livekit/livekit-server/pkg/routing" "github.com/livekit/livekit-server/pkg/routing"
"github.com/livekit/protocol/agent" "github.com/livekit/protocol/agent"
"github.com/livekit/protocol/livekit" "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger" "github.com/livekit/protocol/logger"
"github.com/livekit/protocol/rpc" "github.com/livekit/protocol/rpc"
"github.com/livekit/protocol/utils"
"github.com/livekit/protocol/utils/guid" "github.com/livekit/protocol/utils/guid"
"github.com/livekit/psrpc" "github.com/livekit/psrpc"
) )
@@ -120,7 +121,7 @@ func redactCreateAgentDispatchRequest(req *livekit.CreateAgentDispatchRequest) *
return req return req
} }
clone := utils.CloneProto(req) clone := proto.CloneOf(req)
// replace with size of metadata to provide visibility on request size // replace with size of metadata to provide visibility on request size
if clone.Metadata != "" { if clone.Metadata != "" {
+11 -6
View File
@@ -423,14 +423,19 @@ func (h *AgentHandler) JobRequest(ctx context.Context, job *livekit.Job) (*rpc.J
switch state.GetStatus() { switch state.GetStatus() {
case livekit.JobStatus_JS_RUNNING: case livekit.JobStatus_JS_RUNNING:
logger.Infow("assigned job to worker", "apiKey", selected.APIKey()) logger.Infow("assigned job to worker", "apiKey", selected.APIKey())
h.mu.Lock() jobID := livekit.JobID(job.Id)
h.jobToWorker[livekit.JobID(job.Id)] = selected if err := h.agentServer.RegisterJobTerminateTopic(job.Id); err != nil {
h.mu.Unlock()
err = h.agentServer.RegisterJobTerminateTopic(job.Id)
if err != nil {
logger.Errorw("failed to register JobTerminate handler", err) logger.Errorw("failed to register JobTerminate handler", err)
} }
// checked under h.mu after registering so that either this or the job/worker cleanup, whichever runs second, releases the handler.
h.mu.Lock()
if _, err := selected.GetJobState(jobID); err == nil && h.workers[selected.ID] == selected {
h.jobToWorker[jobID] = selected
} else {
h.deregisterJob(jobID)
}
h.mu.Unlock()
fallthrough fallthrough
case livekit.JobStatus_JS_SUCCESS: case livekit.JobStatus_JS_SUCCESS:
return &rpc.JobRequestResponse{ return &rpc.JobRequestResponse{
+6 -5
View File
@@ -20,6 +20,7 @@ import (
"time" "time"
"github.com/thoas/go-funk" "github.com/thoas/go-funk"
"google.golang.org/protobuf/proto"
"github.com/livekit/protocol/livekit" "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/utils" "github.com/livekit/protocol/utils"
@@ -203,7 +204,7 @@ func (s *LocalStore) StoreAgentDispatch(ctx context.Context, dispatch *livekit.A
s.lock.Lock() s.lock.Lock()
defer s.lock.Unlock() defer s.lock.Unlock()
clone := utils.CloneProto(dispatch) clone := proto.CloneOf(dispatch)
if clone.State != nil { if clone.State != nil {
clone.State.Jobs = nil clone.State.Jobs = nil
} }
@@ -242,13 +243,13 @@ func (s *LocalStore) ListAgentDispatches(ctx context.Context, roomName livekit.R
var js []*livekit.Job var js []*livekit.Job
for _, j := range agentJobs { for _, j := range agentJobs {
js = append(js, utils.CloneProto(j)) js = append(js, proto.CloneOf(j))
} }
var ds []*livekit.AgentDispatch var ds []*livekit.AgentDispatch
m := make(map[string]*livekit.AgentDispatch) m := make(map[string]*livekit.AgentDispatch)
for _, d := range agentDispatches { for _, d := range agentDispatches {
clone := utils.CloneProto(d) clone := proto.CloneOf(d)
m[d.Id] = clone m[d.Id] = clone
ds = append(ds, clone) ds = append(ds, clone)
} }
@@ -256,7 +257,7 @@ func (s *LocalStore) ListAgentDispatches(ctx context.Context, roomName livekit.R
for _, j := range js { for _, j := range js {
d := m[j.DispatchId] d := m[j.DispatchId]
if d != nil { if d != nil {
d.State.Jobs = append(d.State.Jobs, utils.CloneProto(j)) d.State.Jobs = append(d.State.Jobs, proto.CloneOf(j))
} }
} }
@@ -267,7 +268,7 @@ func (s *LocalStore) StoreAgentJob(ctx context.Context, job *livekit.Job) error
s.lock.Lock() s.lock.Lock()
defer s.lock.Unlock() defer s.lock.Unlock()
clone := utils.CloneProto(job) clone := proto.CloneOf(job)
clone.Room = nil clone.Room = nil
if clone.Participant != nil { if clone.Participant != nil {
clone.Participant = &livekit.ParticipantInfo{ clone.Participant = &livekit.ParticipantInfo{
+3 -3
View File
@@ -548,7 +548,7 @@ func (s *RedisStore) storeIngress(_ context.Context, info *livekit.IngressInfo)
} }
// ignore state // ignore state
infoCopy := utils.CloneProto(info) infoCopy := proto.CloneOf(info)
infoCopy.State = nil infoCopy.State = nil
data, err := proto.Marshal(infoCopy) data, err := proto.Marshal(infoCopy)
@@ -851,7 +851,7 @@ func (s *RedisStore) DeleteIngress(_ context.Context, info *livekit.IngressInfo)
} }
func (s *RedisStore) StoreAgentDispatch(_ context.Context, dispatch *livekit.AgentDispatch) error { func (s *RedisStore) StoreAgentDispatch(_ context.Context, dispatch *livekit.AgentDispatch) error {
di := utils.CloneProto(dispatch) di := proto.CloneOf(dispatch)
// Do not store jobs with the dispatch // Do not store jobs with the dispatch
if di.State != nil { if di.State != nil {
@@ -914,7 +914,7 @@ func (s *RedisStore) StoreAgentJob(_ context.Context, job *livekit.Job) error {
key := AgentJobPrefix + string(job.Room.Name) key := AgentJobPrefix + string(job.Room.Name)
jb := utils.CloneProto(job) jb := proto.CloneOf(job)
// Do not store room with the job // Do not store room with the job
jb.Room = nil jb.Room = nil
+3 -3
View File
@@ -347,7 +347,7 @@ func TestAgentStore(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 1, len(rd)) require.Equal(t, 1, len(rd))
expected := utils.CloneProto(ad) expected := proto.CloneOf(ad)
expected.State.Jobs = nil expected.State.Jobs = nil
require.True(t, proto.Equal(expected, rd[0])) require.True(t, proto.Equal(expected, rd[0]))
@@ -358,7 +358,7 @@ func TestAgentStore(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 1, len(rd)) require.Equal(t, 1, len(rd))
expected = utils.CloneProto(ad) expected = proto.CloneOf(ad)
expected.State.Jobs[0].Room = nil expected.State.Jobs[0].Room = nil
expected.State.Jobs[0].Participant = &livekit.ParticipantInfo{ expected.State.Jobs[0].Participant = &livekit.ParticipantInfo{
Identity: "identity", Identity: "identity",
@@ -372,7 +372,7 @@ func TestAgentStore(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 1, len(rd)) require.Equal(t, 1, len(rd))
expected = utils.CloneProto(ad) expected = proto.CloneOf(ad)
expected.State.Jobs = nil expected.State.Jobs = nil
require.True(t, proto.Equal(expected, rd[0])) require.True(t, proto.Equal(expected, rd[0]))
+5 -3
View File
@@ -19,6 +19,8 @@ import (
"errors" "errors"
"time" "time"
"google.golang.org/protobuf/proto"
"github.com/livekit/protocol/livekit" "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger" "github.com/livekit/protocol/logger"
"github.com/livekit/protocol/utils" "github.com/livekit/protocol/utils"
@@ -211,7 +213,7 @@ func (r *StandardRoomAllocator) applyNamedRoomConfiguration(req *livekit.CreateR
return req, psrpc.NewErrorf(psrpc.InvalidArgument, "unknown room configuration in create room request") return req, psrpc.NewErrorf(psrpc.InvalidArgument, "unknown room configuration in create room request")
} }
clone := utils.CloneProto(req) clone := proto.CloneOf(req)
if clone.EmptyTimeout == 0 { if clone.EmptyTimeout == 0 {
clone.EmptyTimeout = conf.EmptyTimeout clone.EmptyTimeout = conf.EmptyTimeout
@@ -223,12 +225,12 @@ func (r *StandardRoomAllocator) applyNamedRoomConfiguration(req *livekit.CreateR
clone.MaxParticipants = conf.MaxParticipants clone.MaxParticipants = conf.MaxParticipants
} }
if clone.Egress == nil { if clone.Egress == nil {
clone.Egress = utils.CloneProto(conf.Egress) clone.Egress = proto.CloneOf(conf.Egress)
} }
if clone.Agents == nil { if clone.Agents == nil {
clone.Agents = make([]*livekit.RoomAgentDispatch, 0, len(conf.Agents)) clone.Agents = make([]*livekit.RoomAgentDispatch, 0, len(conf.Agents))
for _, agent := range conf.Agents { for _, agent := range conf.Agents {
clone.Agents = append(clone.Agents, utils.CloneProto(agent)) clone.Agents = append(clone.Agents, proto.CloneOf(agent))
} }
} }
if clone.MinPlayoutDelay == 0 { if clone.MinPlayoutDelay == 0 {
+3 -3
View File
@@ -26,12 +26,12 @@ import (
"github.com/pion/rtp/codecs" "github.com/pion/rtp/codecs"
"github.com/pion/webrtc/v4" "github.com/pion/webrtc/v4"
"go.uber.org/zap/zapcore" "go.uber.org/zap/zapcore"
"google.golang.org/protobuf/proto"
"github.com/livekit/mediatransportutil" "github.com/livekit/mediatransportutil"
"github.com/livekit/protocol/codecs/mime" "github.com/livekit/protocol/codecs/mime"
"github.com/livekit/protocol/livekit" "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger" "github.com/livekit/protocol/logger"
"github.com/livekit/protocol/utils"
"github.com/livekit/protocol/utils/mono" "github.com/livekit/protocol/utils/mono"
"github.com/livekit/livekit-server/pkg/sfu/buffer" "github.com/livekit/livekit-server/pkg/sfu/buffer"
@@ -491,7 +491,7 @@ func (f *Forwarder) GetState() *livekit.RTPForwarderState {
state.SenderReportState = make([]*livekit.RTCPSenderReportState, len(f.refInfos)) state.SenderReportState = make([]*livekit.RTCPSenderReportState, len(f.refInfos))
for layer, refInfo := range f.refInfos { for layer, refInfo := range f.refInfos {
state.SenderReportState[layer] = utils.CloneProto(refInfo.senderReport) state.SenderReportState[layer] = proto.CloneOf(refInfo.senderReport)
} }
return state return state
} }
@@ -506,7 +506,7 @@ func (f *Forwarder) SeedState(state *livekit.RTPForwarderState) {
for layer, rtcpSenderReportState := range state.SenderReportState { for layer, rtcpSenderReportState := range state.SenderReportState {
f.refInfos[layer] = refInfo{} f.refInfos[layer] = refInfo{}
if senderReport := utils.CloneProto(rtcpSenderReportState); senderReport != nil && senderReport.NtpTimestamp != 0 { if senderReport := proto.CloneOf(rtcpSenderReportState); senderReport != nil && senderReport.NtpTimestamp != 0 {
f.refInfos[layer].senderReport = senderReport f.refInfos[layer].senderReport = senderReport
} }
} }
+5 -4
View File
@@ -25,6 +25,7 @@ import (
"github.com/pion/webrtc/v4" "github.com/pion/webrtc/v4"
"go.uber.org/atomic" "go.uber.org/atomic"
"google.golang.org/protobuf/proto"
"github.com/livekit/mediatransportutil/pkg/bucket" "github.com/livekit/mediatransportutil/pkg/bucket"
"github.com/livekit/mediatransportutil/pkg/codec" "github.com/livekit/mediatransportutil/pkg/codec"
@@ -164,7 +165,7 @@ func NewReceiverBase(params ReceiverBaseParams, trackInfo *livekit.TrackInfo, co
params: params, params: params,
codecState: codecState, codecState: codecState,
isRED: mime.IsMimeTypeStringRED(params.Codec.MimeType), isRED: mime.IsMimeTypeStringRED(params.Codec.MimeType),
trackInfo: utils.CloneProto(trackInfo), trackInfo: proto.CloneOf(trackInfo),
videoLayerMode: buffer.GetVideoLayerModeForMimeType(mime.NormalizeMimeType(params.Codec.MimeType), trackInfo), videoLayerMode: buffer.GetVideoLayerModeForMimeType(mime.NormalizeMimeType(params.Codec.MimeType), trackInfo),
} }
@@ -256,7 +257,7 @@ func (r *ReceiverBase) TrackInfo() *livekit.TrackInfo {
r.bufferMu.RLock() r.bufferMu.RLock()
defer r.bufferMu.RUnlock() defer r.bufferMu.RUnlock()
return utils.CloneProto(r.trackInfo) return proto.CloneOf(r.trackInfo)
} }
func (r *ReceiverBase) UpdateTrackInfo(ti *livekit.TrackInfo) { func (r *ReceiverBase) UpdateTrackInfo(ti *livekit.TrackInfo) {
@@ -282,7 +283,7 @@ func (r *ReceiverBase) UpdateTrackInfo(ti *livekit.TrackInfo) {
"shouldResync", shouldResync, "shouldResync", shouldResync,
) )
} }
r.trackInfo = utils.CloneProto(ti) r.trackInfo = proto.CloneOf(ti)
paused := r.trackInfo.GetMuted() paused := r.trackInfo.GetMuted()
for _, buff := range r.buffers { for _, buff := range r.buffers {
@@ -665,7 +666,7 @@ func (r *ReceiverBase) GetOrCreateBuffer(layer int32) buffer.BufferProvider {
} }
r.bufferPromises[layer] = bp r.bufferPromises[layer] = bp
ti := utils.CloneProto(r.trackInfo) ti := proto.CloneOf(r.trackInfo)
r.bufferMu.Unlock() r.bufferMu.Unlock()
defer close(bp.ready) defer close(bp.ready)
+3 -2
View File
@@ -19,6 +19,7 @@ import (
"time" "time"
"go.uber.org/zap/zapcore" "go.uber.org/zap/zapcore"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb" "google.golang.org/protobuf/types/known/timestamppb"
"github.com/livekit/mediatransportutil" "github.com/livekit/mediatransportutil"
@@ -298,8 +299,8 @@ func (r *rtpStatsBase) seed(from *rtpStatsBase) bool {
r.rtt = from.rtt r.rtt = from.rtt
r.maxRtt = from.maxRtt r.maxRtt = from.maxRtt
r.srFirst = utils.CloneProto(from.srFirst) r.srFirst = proto.CloneOf(from.srFirst)
r.srNewest = utils.CloneProto(from.srNewest) r.srNewest = proto.CloneOf(from.srNewest)
r.nextSnapshotID = from.nextSnapshotID r.nextSnapshotID = from.nextSnapshotID
r.snapshots = make([]snapshot, cap(from.snapshots)) r.snapshots = make([]snapshot, cap(from.snapshots))
+3 -2
View File
@@ -22,6 +22,7 @@ import (
"github.com/pion/rtcp" "github.com/pion/rtcp"
"go.uber.org/zap" "go.uber.org/zap"
"go.uber.org/zap/zapcore" "go.uber.org/zap/zapcore"
"google.golang.org/protobuf/proto"
"github.com/livekit/mediatransportutil" "github.com/livekit/mediatransportutil"
"github.com/livekit/mediatransportutil/pkg/latency" "github.com/livekit/mediatransportutil/pkg/latency"
@@ -654,7 +655,7 @@ func (r *RTPStatsReceiver) getExtendedSenderReport(srData *livekit.RTCPSenderRep
} }
} }
srDataExt := protoutils.CloneProto(srData) srDataExt := proto.CloneOf(srData)
srDataExt.RtpTimestampExt = uint64(srDataExt.RtpTimestamp) + tsCycles srDataExt.RtpTimestampExt = uint64(srDataExt.RtpTimestamp) + tsCycles
return srDataExt return srDataExt
} }
@@ -816,7 +817,7 @@ func (r *RTPStatsReceiver) GetRtcpSenderReportData() *livekit.RTCPSenderReportSt
r.lock.RLock() r.lock.RLock()
defer r.lock.RUnlock() defer r.lock.RUnlock()
return protoutils.CloneProto(r.srNewest) return proto.CloneOf(r.srNewest)
} }
func (r *RTPStatsReceiver) LastSenderReportTime() time.Time { func (r *RTPStatsReceiver) LastSenderReportTime() time.Time {
+3 -3
View File
@@ -20,11 +20,11 @@ import (
"time" "time"
"github.com/frostbyte73/core" "github.com/frostbyte73/core"
"google.golang.org/protobuf/proto"
"github.com/livekit/protocol/codecs/mime" "github.com/livekit/protocol/codecs/mime"
"github.com/livekit/protocol/livekit" "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger" "github.com/livekit/protocol/logger"
"github.com/livekit/protocol/utils"
"github.com/livekit/livekit-server/pkg/sfu/buffer" "github.com/livekit/livekit-server/pkg/sfu/buffer"
"github.com/livekit/livekit-server/pkg/sfu/streamtracker" "github.com/livekit/livekit-server/pkg/sfu/streamtracker"
@@ -144,7 +144,7 @@ func NewStreamTrackerManager(
logger: logger, logger: logger,
mimeType: mimeType, mimeType: mimeType,
videoLayerMode: buffer.GetVideoLayerModeForMimeType(mimeType, trackInfo), videoLayerMode: buffer.GetVideoLayerModeForMimeType(mimeType, trackInfo),
trackInfo: utils.CloneProto(trackInfo), trackInfo: proto.CloneOf(trackInfo),
maxPublishedLayer: buffer.InvalidLayerSpatial, maxPublishedLayer: buffer.InvalidLayerSpatial,
maxTemporalLayerSeen: buffer.InvalidLayerTemporal, maxTemporalLayerSeen: buffer.InvalidLayerTemporal,
clockRate: clockRate, clockRate: clockRate,
@@ -374,7 +374,7 @@ func (s *StreamTrackerManager) setPaused(paused bool) {
func (s *StreamTrackerManager) UpdateTrackInfo(ti *livekit.TrackInfo) { func (s *StreamTrackerManager) UpdateTrackInfo(ti *livekit.TrackInfo) {
s.lock.Lock() s.lock.Lock()
s.trackInfo = utils.CloneProto(ti) s.trackInfo = proto.CloneOf(ti)
s.maxExpectedLayerFromTrackInfoLocked(false) s.maxExpectedLayerFromTrackInfoLocked(false)
paused := s.trackInfo.GetMuted() paused := s.trackInfo.GetMuted()
+2 -2
View File
@@ -21,12 +21,12 @@ import (
"unsafe" "unsafe"
"go.uber.org/zap/zapcore" "go.uber.org/zap/zapcore"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb" "google.golang.org/protobuf/types/known/timestamppb"
"github.com/livekit/livekit-server/pkg/utils" "github.com/livekit/livekit-server/pkg/utils"
"github.com/livekit/protocol/livekit" "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger" "github.com/livekit/protocol/logger"
protoutils "github.com/livekit/protocol/utils"
) )
type ReferenceGuard struct { type ReferenceGuard struct {
@@ -419,7 +419,7 @@ func coalesce(stats []*livekit.AnalyticsStat) *livekit.AnalyticsStat {
for _, videoLayer := range analyticsStream.VideoLayers { for _, videoLayer := range analyticsStream.VideoLayers {
coalescedVideoLayer := coalescedVideoLayers[videoLayer.Layer] coalescedVideoLayer := coalescedVideoLayers[videoLayer.Layer]
if coalescedVideoLayer == nil { if coalescedVideoLayer == nil {
coalescedVideoLayer = protoutils.CloneProto(videoLayer) coalescedVideoLayer = proto.CloneOf(videoLayer)
coalescedVideoLayers[videoLayer.Layer] = coalescedVideoLayer coalescedVideoLayers[videoLayer.Layer] = coalescedVideoLayer
} else { } else {
coalescedVideoLayer.Packets += videoLayer.Packets coalescedVideoLayer.Packets += videoLayer.Packets
+3 -2
View File
@@ -17,15 +17,16 @@
package utils package utils
import ( import (
"google.golang.org/protobuf/proto"
"github.com/livekit/protocol/livekit" "github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/utils"
) )
func ClientInfoWithoutAddress(c *livekit.ClientInfo) *livekit.ClientInfo { func ClientInfoWithoutAddress(c *livekit.ClientInfo) *livekit.ClientInfo {
if c == nil { if c == nil {
return nil return nil
} }
clone := utils.CloneProto(c) clone := proto.CloneOf(c)
clone.Address = "" clone.Address = ""
return clone return clone
} }