More abstractions (#316)

This commit is contained in:
Raja Subramanian
2022-01-07 01:46:15 +05:30
committed by GitHub
parent dd612f966d
commit ecf9590d56
12 changed files with 326 additions and 143 deletions
+19 -37
View File
@@ -49,7 +49,7 @@ type MediaTrackSubscriptionsParams struct {
Telemetry telemetry.TelemetryService
Logger *logger.Logger
Logger logger.Logger
}
func NewMediaTrackSubscriptions(params MediaTrackSubscriptionsParams) *MediaTrackSubscriptions {
@@ -177,19 +177,13 @@ func (t *MediaTrackSubscriptions) AddSubscriber(sub types.Participant, codec web
go t.sendDownTrackBindingReports(sub)
})
downTrack.OnPacketSent(func(_ *sfu.DownTrack, size int) {
if t.params.Telemetry != nil {
t.params.Telemetry.OnDownstreamPacket(subscriberID, t.params.MediaTrack.ID(), size)
}
t.params.Telemetry.OnDownstreamPacket(subscriberID, t.params.MediaTrack.ID(), size)
})
downTrack.OnPaddingSent(func(_ *sfu.DownTrack, size int) {
if t.params.Telemetry != nil {
t.params.Telemetry.OnDownstreamPacket(subscriberID, t.params.MediaTrack.ID(), size)
}
t.params.Telemetry.OnDownstreamPacket(subscriberID, t.params.MediaTrack.ID(), size)
})
downTrack.OnRTCP(func(pkts []rtcp.Packet) {
if t.params.Telemetry != nil {
t.params.Telemetry.HandleRTCP(livekit.StreamType_DOWNSTREAM, subscriberID, t.params.MediaTrack.ID(), pkts)
}
t.params.Telemetry.HandleRTCP(livekit.StreamType_DOWNSTREAM, subscriberID, t.params.MediaTrack.ID(), pkts)
})
downTrack.OnCloseHandler(func() {
@@ -199,9 +193,7 @@ func (t *MediaTrackSubscriptions) AddSubscriber(sub types.Participant, codec web
t.subscribedTracksMu.Unlock()
t.maybeNotifyNoSubscribers()
if t.params.Telemetry != nil {
t.params.Telemetry.TrackUnsubscribed(context.Background(), subscriberID, t.params.MediaTrack.ToProto())
}
t.params.Telemetry.TrackUnsubscribed(context.Background(), subscriberID, t.params.MediaTrack.ToProto())
// ignore if the subscribing sub is not connected
if sub.SubscriberPC().ConnectionState() == webrtc.PeerConnectionStateClosed {
@@ -213,14 +205,12 @@ func (t *MediaTrackSubscriptions) AddSubscriber(sub types.Participant, codec web
if sender == nil {
return
}
if t.params.Logger != nil {
t.params.Logger.Debugw("removing peerconnection track",
"track", t.params.MediaTrack.ID(),
"subscriber", sub.Identity(),
"subscriberID", subscriberID,
"kind", t.params.MediaTrack.Kind(),
)
}
t.params.Logger.Debugw("removing peerconnection track",
"track", t.params.MediaTrack.ID(),
"subscriber", sub.Identity(),
"subscriberID", subscriberID,
"kind", t.params.MediaTrack.Kind(),
)
if err := sub.SubscriberPC().RemoveTrack(sender); err != nil {
if err == webrtc.ErrConnectionClosed {
// sub closing, can skip removing subscribedtracks
@@ -229,13 +219,11 @@ func (t *MediaTrackSubscriptions) AddSubscriber(sub types.Participant, codec web
if _, ok := err.(*rtcerr.InvalidStateError); !ok {
// most of these are safe to ignore, since the track state might have already
// been set to Inactive
if t.params.Logger != nil {
t.params.Logger.Debugw("could not remove remoteTrack from forwarder",
"error", err,
"subscriber", sub.Identity(),
"subscriberID", subscriberID,
)
}
t.params.Logger.Debugw("could not remove remoteTrack from forwarder",
"error", err,
"subscriber", sub.Identity(),
"subscriberID", subscriberID,
)
}
}
@@ -255,9 +243,7 @@ func (t *MediaTrackSubscriptions) AddSubscriber(sub types.Participant, codec web
sub.Negotiate()
}()
if t.params.Telemetry != nil {
t.params.Telemetry.TrackSubscribed(context.Background(), subscriberID, t.params.MediaTrack.ToProto())
}
t.params.Telemetry.TrackSubscribed(context.Background(), subscriberID, t.params.MediaTrack.ToProto())
return downTrack, nil
}
@@ -271,9 +257,7 @@ func (t *MediaTrackSubscriptions) RemoveSubscriber(participantID livekit.Partici
}
func (t *MediaTrackSubscriptions) RemoveAllSubscribers() {
if t.params.Logger != nil {
t.params.Logger.Debugw("removing all subscribers", "track", t.params.MediaTrack.ID())
}
t.params.Logger.Debugw("removing all subscribers", "track", t.params.MediaTrack.ID())
t.subscribedTracksMu.RLock()
subscribedTracks := t.subscribedTracks
@@ -353,9 +337,7 @@ func (t *MediaTrackSubscriptions) sendDownTrackBindingReports(sub types.Particip
i := 0
for {
if err := sub.SubscriberPC().WriteRTCP(batch); err != nil {
if t.params.Logger != nil {
t.params.Logger.Errorw("could not write RTCP", err)
}
t.params.Logger.Errorw("could not write RTCP", err)
return
}
if i > 5 {