Publisher controlled flex permissions (#284)

* WIP commit

* Add some tests

* allowedSubscribers uses participant sid

* correct variable name

* correct another variable name

* Add ParticipantSid to SubscriptionPermissionUpdate message

* protocol v0.11.2

* WIP commit

* WIP commit

* fix tests

* Remove unused code

* Close uptrack manager

* Remove duplicate close

* move comment to the correct line where the loop could be long

* Fix disallowed list revocation, thank you Jie

* Remove unneeded interface method

* RemoveSubscriber in Participant

* Clean up disallowed subscriptions and handle permissions on new track addition

* add test for track addition after permission set

* Remove unnecessary check
This commit is contained in:
Raja Subramanian
2021-12-24 14:14:40 +05:30
committed by GitHub
parent eae6eff6a3
commit ee99a323d2
15 changed files with 1524 additions and 569 deletions
+57 -24
View File
@@ -50,10 +50,10 @@ type MediaTrack struct {
simulcasted utils.AtomicFlag
buffer *buffer.Buffer
// channel to send RTCP packets to the source
lock sync.RWMutex
// map of target participantId -> types.SubscribedTrack
subscribedTracks sync.Map
subscribedTracks sync.Map // participantSid => types.SubscribedTrack
twcc *twcc.Responder
audioLevel *AudioLevel
receiver sfu.Receiver
@@ -70,6 +70,7 @@ type MediaTrack struct {
connectionStats *connectionquality.ConnectionStats
done chan struct{}
// quality level enable/disable
maxQualityLock sync.Mutex
maxSubscriberQuality map[string]livekit.VideoQuality
@@ -86,12 +87,13 @@ type MediaTrackParams struct {
SdpCid string
ParticipantID string
ParticipantIdentity string
RTCPChan chan []rtcp.Packet
BufferFactory *buffer.Factory
ReceiverConfig ReceiverConfig
AudioConfig config.AudioConfig
Telemetry telemetry.TelemetryService
Logger logger.Logger
// channel to send RTCP packets to the source
RTCPChan chan []rtcp.Packet
BufferFactory *buffer.Factory
ReceiverConfig ReceiverConfig
AudioConfig config.AudioConfig
Telemetry telemetry.TelemetryService
Logger logger.Logger
}
func NewMediaTrack(track *webrtc.TrackRemote, params MediaTrackParams) *MediaTrack {
@@ -196,8 +198,10 @@ func (t *MediaTrack) AddSubscriber(sub types.Participant) error {
t.lock.Lock()
defer t.lock.Unlock()
subscriberID := sub.ID()
// don't subscribe to the same track multiple times
if _, ok := t.subscribedTracks.Load(sub.ID()); ok {
if _, ok := t.subscribedTracks.Load(subscriberID); ok {
return nil
}
@@ -221,14 +225,14 @@ func (t *MediaTrack) AddSubscriber(sub types.Participant) error {
Channels: codec.Channels,
SDPFmtpLine: codec.SDPFmtpLine,
RTCPFeedback: FeedbackTypes,
}, receiver, t.params.BufferFactory, sub.ID(), t.params.ReceiverConfig.PacketBufferSize)
}, receiver, t.params.BufferFactory, subscriberID, t.params.ReceiverConfig.PacketBufferSize)
if err != nil {
return err
}
subTrack := NewSubscribedTrack(SubscribedTrackParams{
PublisherID: t.params.ParticipantID,
PublisherIdentity: t.params.ParticipantIdentity,
SubscriberID: sub.ID(),
SubscriberID: subscriberID,
MediaTrack: t,
DownTrack: downTrack,
})
@@ -282,19 +286,19 @@ func (t *MediaTrack) AddSubscriber(sub types.Participant) error {
go t.sendDownTrackBindingReports(sub)
})
downTrack.OnPacketSent(func(_ *sfu.DownTrack, size int) {
t.params.Telemetry.OnDownstreamPacket(sub.ID(), size)
t.params.Telemetry.OnDownstreamPacket(subscriberID, size)
})
downTrack.OnPaddingSent(func(_ *sfu.DownTrack, size int) {
t.params.Telemetry.OnDownstreamPacket(sub.ID(), size)
t.params.Telemetry.OnDownstreamPacket(subscriberID, size)
})
downTrack.OnRTCP(func(pkts []rtcp.Packet) {
t.params.Telemetry.HandleRTCP(livekit.StreamType_DOWNSTREAM, sub.ID(), pkts)
t.params.Telemetry.HandleRTCP(livekit.StreamType_DOWNSTREAM, subscriberID, pkts)
})
downTrack.OnCloseHandler(func() {
go func() {
t.subscribedTracks.Delete(sub.ID())
t.params.Telemetry.TrackUnsubscribed(context.Background(), sub.ID(), t.ToProto())
t.subscribedTracks.Delete(subscriberID)
t.params.Telemetry.TrackUnsubscribed(context.Background(), subscriberID, t.ToProto())
// ignore if the subscribing sub is not connected
if sub.SubscriberPC().ConnectionState() == webrtc.PeerConnectionStateClosed {
@@ -309,7 +313,7 @@ func (t *MediaTrack) AddSubscriber(sub types.Participant) error {
t.params.Logger.Debugw("removing peerconnection track",
"track", t.ID(),
"subscriber", sub.Identity(),
"subscriberID", sub.ID(),
"subscriberID", subscriberID,
"kind", t.Kind(),
)
if err := sub.SubscriberPC().RemoveTrack(sender); err != nil {
@@ -323,12 +327,12 @@ func (t *MediaTrack) AddSubscriber(sub types.Participant) error {
t.params.Logger.Debugw("could not remove remoteTrack from forwarder",
"error", err,
"subscriber", sub.Identity(),
"subscriberID", sub.ID(),
"subscriberID", subscriberID,
)
}
}
t.NotifySubscriberMute(sub.ID())
t.NotifySubscriberMute(subscriberID)
sub.RemoveSubscribedTrack(subTrack)
sub.Negotiate()
}()
@@ -337,18 +341,18 @@ func (t *MediaTrack) AddSubscriber(sub types.Participant) error {
downTrack.AddReceiverReportListener(t.handleMaxLossFeedback)
}
t.subscribedTracks.Store(sub.ID(), subTrack)
t.subscribedTracks.Store(subscriberID, subTrack)
subTrack.SetPublisherMuted(t.IsMuted())
t.receiver.AddDownTrack(downTrack)
// since sub will lock, run it in a goroutine to avoid deadlocks
go func() {
t.NotifySubscriberMaxQuality(sub.ID(), livekit.VideoQuality_HIGH) // start with HIGH, let subscription change it later
t.NotifySubscriberMaxQuality(subscriberID, livekit.VideoQuality_HIGH) // start with HIGH, let subscription change it later
sub.AddSubscribedTrack(subTrack)
sub.Negotiate()
}()
t.params.Telemetry.TrackSubscribed(context.Background(), sub.ID(), t.ToProto())
t.params.Telemetry.TrackSubscribed(context.Background(), subscriberID, t.ToProto())
return nil
}
@@ -469,6 +473,35 @@ func (t *MediaTrack) RemoveAllSubscribers() {
t.subscribedTracks = sync.Map{}
}
func (t *MediaTrack) RevokeDisallowedSubscribers(allowedSubscriberIDs []string) []string {
t.lock.Lock()
defer t.lock.Unlock()
var revokedSubscriberIDs []string
// LK-TODO: large number of subscribers needs to be solved for this loop
t.subscribedTracks.Range(func(key interface{}, val interface{}) bool {
if subID, ok := key.(string); ok {
found := false
for _, allowedID := range allowedSubscriberIDs {
if subID == allowedID {
found = true
break
}
}
if !found {
if subTrack, ok := val.(types.SubscribedTrack); ok {
go subTrack.DownTrack().Close()
revokedSubscriberIDs = append(revokedSubscriberIDs, subID)
}
}
}
return true
})
return revokedSubscriberIDs
}
func (t *MediaTrack) ToProto() *livekit.TrackInfo {
info := t.params.TrackInfo
info.Muted = t.IsMuted()
@@ -543,8 +576,8 @@ func (t *MediaTrack) GetQualityForDimension(width, height uint32) livekit.VideoQ
return quality
}
func (t *MediaTrack) getSubscribedTrack(id string) types.SubscribedTrack {
if val, ok := t.subscribedTracks.Load(id); ok {
func (t *MediaTrack) getSubscribedTrack(subscriberID string) types.SubscribedTrack {
if val, ok := t.subscribedTracks.Load(subscriberID); ok {
if st, ok := val.(types.SubscribedTrack); ok {
return st
}