Improve thread safety with participant callbacks (#728)

This commit is contained in:
David Zhao
2022-05-27 12:26:07 -07:00
committed by GitHub
parent cb9f0d37c2
commit a4882b9866
+49 -19
View File
@@ -329,17 +329,19 @@ func (p *ParticipantImpl) SetMetadata(metadata string) {
p.lock.Lock() p.lock.Lock()
changed := p.grants.Metadata != metadata changed := p.grants.Metadata != metadata
p.grants.Metadata = metadata p.grants.Metadata = metadata
onParticipantUpdate := p.onParticipantUpdate
onClaimsChanged := p.onClaimsChanged
p.lock.Unlock() p.lock.Unlock()
if !changed { if !changed {
return return
} }
if p.onParticipantUpdate != nil { if onParticipantUpdate != nil {
p.onParticipantUpdate(p) onParticipantUpdate(p)
} }
if p.onClaimsChanged != nil { if onClaimsChanged != nil {
p.onClaimsChanged(p) onClaimsChanged(p)
} }
} }
@@ -374,6 +376,8 @@ func (p *ParticipantImpl) SetPermission(permission *livekit.ParticipantPermissio
video.Recorder = permission.Recorder video.Recorder = permission.Recorder
canPublish := video.GetCanPublish() canPublish := video.GetCanPublish()
onParticipantUpdate := p.onParticipantUpdate
onClaimsChanged := p.onClaimsChanged
p.lock.Unlock() p.lock.Unlock()
// publish permission has been revoked then remove all published tracks // publish permission has been revoked then remove all published tracks
@@ -391,11 +395,11 @@ func (p *ParticipantImpl) SetPermission(permission *livekit.ParticipantPermissio
// update isPublisher attribute // update isPublisher attribute
p.isPublisher.Store(canPublish && p.publisher.IsEstablished()) p.isPublisher.Store(canPublish && p.publisher.IsEstablished())
if p.onParticipantUpdate != nil { if onParticipantUpdate != nil {
p.onParticipantUpdate(p) onParticipantUpdate(p)
} }
if p.onClaimsChanged != nil { if onClaimsChanged != nil {
p.onClaimsChanged(p) onClaimsChanged(p)
} }
return true return true
} }
@@ -427,33 +431,45 @@ func (p *ParticipantImpl) SubscriberMediaEngine() *webrtc.MediaEngine {
// callbacks for clients // callbacks for clients
func (p *ParticipantImpl) OnTrackPublished(callback func(types.LocalParticipant, types.MediaTrack)) { func (p *ParticipantImpl) OnTrackPublished(callback func(types.LocalParticipant, types.MediaTrack)) {
p.lock.Lock()
p.onTrackPublished = callback p.onTrackPublished = callback
p.lock.Unlock()
} }
func (p *ParticipantImpl) OnStateChange(callback func(p types.LocalParticipant, oldState livekit.ParticipantInfo_State)) { func (p *ParticipantImpl) OnStateChange(callback func(p types.LocalParticipant, oldState livekit.ParticipantInfo_State)) {
p.lock.Lock() p.lock.Lock()
defer p.lock.Unlock()
p.onStateChange = callback p.onStateChange = callback
p.lock.Unlock()
} }
func (p *ParticipantImpl) OnTrackUpdated(callback func(types.LocalParticipant, types.MediaTrack)) { func (p *ParticipantImpl) OnTrackUpdated(callback func(types.LocalParticipant, types.MediaTrack)) {
p.lock.Lock()
p.onTrackUpdated = callback p.onTrackUpdated = callback
p.lock.Unlock()
} }
func (p *ParticipantImpl) OnParticipantUpdate(callback func(types.LocalParticipant)) { func (p *ParticipantImpl) OnParticipantUpdate(callback func(types.LocalParticipant)) {
p.lock.Lock()
p.onParticipantUpdate = callback p.onParticipantUpdate = callback
p.lock.Unlock()
} }
func (p *ParticipantImpl) OnDataPacket(callback func(types.LocalParticipant, *livekit.DataPacket)) { func (p *ParticipantImpl) OnDataPacket(callback func(types.LocalParticipant, *livekit.DataPacket)) {
p.lock.Lock()
p.onDataPacket = callback p.onDataPacket = callback
p.lock.Unlock()
} }
func (p *ParticipantImpl) OnSubscribedTo(callback func(types.LocalParticipant, livekit.ParticipantID)) { func (p *ParticipantImpl) OnSubscribedTo(callback func(types.LocalParticipant, livekit.ParticipantID)) {
p.lock.Lock()
p.onSubscribedTo = callback p.onSubscribedTo = callback
p.lock.Unlock()
} }
func (p *ParticipantImpl) OnClose(callback func(types.LocalParticipant, map[livekit.TrackID]livekit.ParticipantID)) { func (p *ParticipantImpl) OnClose(callback func(types.LocalParticipant, map[livekit.TrackID]livekit.ParticipantID)) {
p.lock.Lock()
p.onClose = callback p.onClose = callback
p.lock.Unlock()
} }
func (p *ParticipantImpl) OnClaimsChanged(callback func(types.LocalParticipant)) { func (p *ParticipantImpl) OnClaimsChanged(callback func(types.LocalParticipant)) {
@@ -468,6 +484,7 @@ func (p *ParticipantImpl) HandleOffer(sdp webrtc.SessionDescription) (answer web
p.lock.Unlock() p.lock.Unlock()
return return
} }
onParticipantUpdate := p.onParticipantUpdate
p.lock.Unlock() p.lock.Unlock()
p.params.Logger.Debugw("answering pub offer", p.params.Logger.Debugw("answering pub offer",
"state", p.State().String(), "state", p.State().String(),
@@ -509,8 +526,8 @@ func (p *ParticipantImpl) HandleOffer(sdp webrtc.SessionDescription) (answer web
if p.isPublisher.Load() != p.CanPublish() { if p.isPublisher.Load() != p.CanPublish() {
p.isPublisher.Store(p.CanPublish()) p.isPublisher.Store(p.CanPublish())
// trigger update as well if participant is already fully connected // trigger update as well if participant is already fully connected
if p.State() == livekit.ParticipantInfo_ACTIVE && p.onParticipantUpdate != nil { if p.State() == livekit.ParticipantInfo_ACTIVE && onParticipantUpdate != nil {
p.onParticipantUpdate(p) onParticipantUpdate(p)
} }
} }
@@ -834,6 +851,7 @@ func (p *ParticipantImpl) AddSubscribedTrack(subTrack types.SubscribedTrack) {
"publisherIdentity", subTrack.PublisherIdentity(), "publisherIdentity", subTrack.PublisherIdentity(),
"trackID", subTrack.ID()) "trackID", subTrack.ID())
p.lock.Lock() p.lock.Lock()
onSubscribedTo := p.onSubscribedTo
p.subscribedTracks[subTrack.ID()] = subTrack p.subscribedTracks[subTrack.ID()] = subTrack
settings := p.subscribedTracksSettings[subTrack.ID()] settings := p.subscribedTracksSettings[subTrack.ID()]
p.lock.Unlock() p.lock.Unlock()
@@ -849,8 +867,8 @@ func (p *ParticipantImpl) AddSubscribedTrack(subTrack types.SubscribedTrack) {
publisherID := subTrack.PublisherID() publisherID := subTrack.PublisherID()
isAlreadySubscribed := p.isSubscribedTo(publisherID) isAlreadySubscribed := p.isSubscribedTo(publisherID)
p.subscribedTo.Store(publisherID, struct{}{}) p.subscribedTo.Store(publisherID, struct{}{})
if !isAlreadySubscribed && p.onSubscribedTo != nil { if !isAlreadySubscribed && onSubscribedTo != nil {
p.onSubscribedTo(p, publisherID) onSubscribedTo(p, publisherID)
} }
} }
@@ -958,8 +976,11 @@ func (p *ParticipantImpl) setupUpTrackManager() {
return return
} }
if p.onTrackUpdated != nil { p.lock.RLock()
p.onTrackUpdated(p, track) onTrackUpdated := p.onTrackUpdated
p.lock.RUnlock()
if onTrackUpdated != nil {
onTrackUpdated(p, track)
} }
}) })
@@ -1031,8 +1052,14 @@ func (p *ParticipantImpl) onMediaTrack(track *webrtc.TrackRemote, rtpReceiver *w
"rid", track.RID(), "rid", track.RID(),
"SSRC", track.SSRC()) "SSRC", track.SSRC())
} }
if !isNewTrack && publishedTrack != nil && !publishedTrack.HasPendingCodec() && p.IsReady() && p.onTrackUpdated != nil {
p.onTrackUpdated(p, publishedTrack) if !isNewTrack && publishedTrack != nil && !publishedTrack.HasPendingCodec() && p.IsReady() {
p.lock.RLock()
onTrackUpdated := p.onTrackUpdated
p.lock.RUnlock()
if onTrackUpdated != nil {
onTrackUpdated(p, publishedTrack)
}
} }
} }
@@ -1073,9 +1100,12 @@ func (p *ParticipantImpl) handleDataMessage(kind livekit.DataPacket_Kind, data [
// only forward on user payloads // only forward on user payloads
switch payload := dp.Value.(type) { switch payload := dp.Value.(type) {
case *livekit.DataPacket_User: case *livekit.DataPacket_User:
if p.onDataPacket != nil { p.lock.RLock()
onDataPacket := p.onDataPacket
p.lock.RUnlock()
if onDataPacket != nil {
payload.User.ParticipantSid = string(p.params.SID) payload.User.ParticipantSid = string(p.params.SID)
p.onDataPacket(p, &dp) onDataPacket(p, &dp)
} }
default: default:
p.params.Logger.Warnw("received unsupported data packet", nil, "payload", payload) p.params.Logger.Warnw("received unsupported data packet", nil, "payload", payload)