Support rtx for publisher (#2452)
* Support rtx for publisher * remote log * solve comment
This commit is contained in:
+84
-6
@@ -17,6 +17,7 @@ package rtc
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -37,11 +38,14 @@ import (
|
||||
"github.com/livekit/livekit-server/pkg/config"
|
||||
"github.com/livekit/livekit-server/pkg/rtc/transport"
|
||||
"github.com/livekit/livekit-server/pkg/rtc/types"
|
||||
lkinterceptor "github.com/livekit/livekit-server/pkg/sfu/interceptor"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/pacer"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/rtpextension"
|
||||
"github.com/livekit/livekit-server/pkg/sfu/streamallocator"
|
||||
sfuutils "github.com/livekit/livekit-server/pkg/sfu/utils"
|
||||
"github.com/livekit/livekit-server/pkg/telemetry/prometheus"
|
||||
sutils "github.com/livekit/livekit-server/pkg/utils"
|
||||
lktwcc "github.com/livekit/mediatransportutil/pkg/twcc"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
"github.com/livekit/protocol/logger/pionlogger"
|
||||
@@ -199,6 +203,7 @@ type TransportParams struct {
|
||||
ParticipantIdentity livekit.ParticipantIdentity
|
||||
ProtocolVersion types.ProtocolVersion
|
||||
Config *WebRTCConfig
|
||||
Twcc *lktwcc.Responder
|
||||
DirectionConfig DirectionConfig
|
||||
CongestionControlConfig config.CongestionControlConfig
|
||||
EnabledCodecs []*livekit.Codec
|
||||
@@ -326,6 +331,40 @@ func newPeerConnection(params TransportParams, onBandwidthEstimator func(estimat
|
||||
ir.Add(f)
|
||||
}
|
||||
}
|
||||
|
||||
setTWCCForVideo := func(info *interceptor.StreamInfo) {
|
||||
if !strings.HasPrefix(info.MimeType, "video") {
|
||||
return
|
||||
}
|
||||
// rtx stream don't have rtcp feedback, always set twcc for rtx stream
|
||||
twccFb := strings.HasSuffix(info.MimeType, "rtx")
|
||||
if !twccFb {
|
||||
for _, fb := range info.RTCPFeedback {
|
||||
if fb.Type == webrtc.TypeRTCPFBTransportCC {
|
||||
twccFb = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if !twccFb {
|
||||
return
|
||||
}
|
||||
|
||||
twccExtID := sfuutils.GetHeaderExtensionID(info.RTPHeaderExtensions, webrtc.RTPHeaderExtensionCapability{URI: sdp.TransportCCURI})
|
||||
if twccExtID != 0 {
|
||||
if buffer := params.Config.BufferFactory.GetBuffer(info.SSRC); buffer != nil {
|
||||
params.Logger.Debugw("set rtx twcc and ext id", "ssrc", info.SSRC, "twccExtID", twccExtID)
|
||||
buffer.SetTWCCAndExtID(params.Twcc, uint8(twccExtID))
|
||||
} else {
|
||||
params.Logger.Errorw("failed to get buffer for rtx stream", nil, "ssrc", info.SSRC)
|
||||
}
|
||||
}
|
||||
}
|
||||
// put rtx interceptor behind unhandle simulcast interceptor so it can get the correct mid & rid
|
||||
ir.Add(lkinterceptor.NewRTXInfoExtractorFactory(setTWCCForVideo, func(repair, base uint32) {
|
||||
params.Logger.Debugw("rtx pair found from extension", "repair", repair, "base", base)
|
||||
params.Config.BufferFactory.SetRTXPair(repair, base)
|
||||
}, params.Logger))
|
||||
api := webrtc.NewAPI(
|
||||
webrtc.WithMediaEngine(me),
|
||||
webrtc.WithSettingEngine(se),
|
||||
@@ -1532,11 +1571,7 @@ func (t *PCTransport) handleRemoteDescriptionReceived(e *event) error {
|
||||
}
|
||||
}
|
||||
|
||||
func (t *PCTransport) isRemoteOfferRestartICE(sd *webrtc.SessionDescription) (string, bool, error) {
|
||||
parsed, err := sd.Unmarshal()
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
}
|
||||
func (t *PCTransport) isRemoteOfferRestartICE(parsed *sdp.SessionDescription) (string, bool, error) {
|
||||
user, pwd, err := lksdp.ExtractICECredential(parsed)
|
||||
if err != nil {
|
||||
return "", false, err
|
||||
@@ -1633,7 +1668,11 @@ func (t *PCTransport) createAndSendAnswer() error {
|
||||
}
|
||||
|
||||
func (t *PCTransport) handleRemoteOfferReceived(sd *webrtc.SessionDescription) error {
|
||||
iceCredential, offerRestartICE, err := t.isRemoteOfferRestartICE(sd)
|
||||
parsed, err := sd.Unmarshal()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
iceCredential, offerRestartICE, err := t.isRemoteOfferRestartICE(parsed)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "check remote offer restart ice failed")
|
||||
}
|
||||
@@ -1655,6 +1694,13 @@ func (t *PCTransport) handleRemoteOfferReceived(sd *webrtc.SessionDescription) e
|
||||
if err := t.setRemoteDescription(*sd); err != nil {
|
||||
return err
|
||||
}
|
||||
rtxRepairs := rtxRepairsFromSDP(parsed, t.params.Logger)
|
||||
if len(rtxRepairs) > 0 {
|
||||
t.params.Logger.Debugw("rtx pairs found from sdp", "ssrcs", rtxRepairs)
|
||||
for repair, base := range rtxRepairs {
|
||||
t.params.Config.BufferFactory.SetRTXPair(repair, base)
|
||||
}
|
||||
}
|
||||
|
||||
if t.currentOfferIceCredential == "" || offerRestartICE {
|
||||
t.currentOfferIceCredential = iceCredential
|
||||
@@ -1778,3 +1824,35 @@ func configureAudioTransceiver(tr *webrtc.RTPTransceiver, stereo bool, nack bool
|
||||
|
||||
tr.SetCodecPreferences(configCodecs)
|
||||
}
|
||||
|
||||
func rtxRepairsFromSDP(s *sdp.SessionDescription, logger logger.Logger) map[uint32]uint32 {
|
||||
rtxRepairFlows := map[uint32]uint32{}
|
||||
for _, media := range s.MediaDescriptions {
|
||||
for _, attr := range media.Attributes {
|
||||
switch attr.Key {
|
||||
case sdp.AttrKeySSRCGroup:
|
||||
split := strings.Split(attr.Value, " ")
|
||||
if split[0] == sdp.SemanticTokenFlowIdentification {
|
||||
// Essentially lines like `a=ssrc-group:FID 2231627014 632943048` are processed by this section
|
||||
// as this declares that the second SSRC (632943048) is a rtx repair flow (RFC4588) for the first
|
||||
// (2231627014) as specified in RFC5576
|
||||
if len(split) == 3 {
|
||||
baseSsrc, err := strconv.ParseUint(split[1], 10, 32)
|
||||
if err != nil {
|
||||
logger.Warnw("Failed to parse SSRC", err, "ssrc", split[1])
|
||||
continue
|
||||
}
|
||||
rtxRepairFlow, err := strconv.ParseUint(split[2], 10, 32)
|
||||
if err != nil {
|
||||
logger.Warnw("Failed to parse SSRC", err, "ssrc", split[2])
|
||||
continue
|
||||
}
|
||||
rtxRepairFlows[uint32(rtxRepairFlow)] = uint32(baseSsrc)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return rtxRepairFlows
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user