publish new codec when subscriber needed (#762)

* publish new codec as needed
This commit is contained in:
cnderrauber
2022-06-16 18:16:34 +08:00
committed by GitHub
parent 83b71fce50
commit d15725c662
9 changed files with 285 additions and 30 deletions
+33 -3
View File
@@ -8,6 +8,7 @@ import (
"github.com/pion/rtcp"
"github.com/pion/webrtc/v3"
"go.uber.org/atomic"
"google.golang.org/protobuf/proto"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
@@ -96,8 +97,7 @@ func (t *MediaTrack) HasSdpCid(cid string) bool {
return true
}
info := t.MediaTrackReceiver.TrackInfo(false)
t.params.Logger.Debugw("MediaTrack.HasSdpCid", "cid", cid, "trackInfo", info.String())
info := t.params.TrackInfo
for _, c := range info.Codecs {
if c.Cid == cid {
return true
@@ -113,8 +113,22 @@ func (t *MediaTrack) ToProto() *livekit.TrackInfo {
return info
}
func (t *MediaTrack) SetPendingCodecSid(codecs []*livekit.SimulcastCodec) {
ti := proto.Clone(t.params.TrackInfo).(*livekit.TrackInfo)
for _, c := range codecs {
for _, origin := range ti.Codecs {
if strings.Contains(origin.MimeType, c.Codec) {
origin.Cid = c.Cid
break
}
}
}
t.params.TrackInfo = ti
}
// AddReceiver adds a new RTP receiver to the track, returns true when receiver represents a new codec
func (t *MediaTrack) AddReceiver(receiver *webrtc.RTPReceiver, track *webrtc.TrackRemote, twcc *twcc.Responder, mid string) bool {
var newCodec bool
buff, rtcpReader := t.params.BufferFactory.GetBufferPair(uint32(track.SSRC()))
if buff == nil || rtcpReader == nil {
@@ -180,6 +194,22 @@ func (t *MediaTrack) AddReceiver(receiver *webrtc.RTPReceiver, track *webrtc.Tra
t.params.Telemetry.TrackStats(livekit.StreamType_UPSTREAM, t.PublisherID(), t.ID(), stat)
})
if t.PrimaryReceiver() == nil {
// primary codec published, set potential codecs
potentialCodecs := make([]webrtc.RTPCodecParameters, 0, len(t.params.TrackInfo.Codecs))
parameters := receiver.GetParameters()
for _, c := range t.params.TrackInfo.Codecs {
for _, nc := range parameters.Codecs {
if strings.EqualFold(nc.MimeType, c.MimeType) {
potentialCodecs = append(potentialCodecs, nc)
break
}
}
}
t.params.Logger.Debugw("primary codec published, set potential codecs", "potential", potentialCodecs)
if len(potentialCodecs) > 0 {
t.MediaTrackReceiver.SetPotentialCodecs(potentialCodecs, parameters.HeaderExtensions)
}
t.params.Telemetry.TrackPublished(
context.Background(),
t.PublisherID(),
@@ -234,7 +264,7 @@ func (t *MediaTrack) SetRTT(rtt uint32) {
}
func (t *MediaTrack) HasPendingCodec() bool {
return len(t.params.TrackInfo.Codecs) > len(t.Receivers())
return t.MediaTrackReceiver.PrimaryReceiver() == nil
}
func (t *MediaTrack) OnMaxLayerChange(maxLayer int32) {