add red codec for opus (#938)

* opus/red codec

* panic

* forward red track to nonred subscriber

* config

* clean code

* solve comments
This commit is contained in:
cnderrauber
2022-08-22 12:32:27 +08:00
committed by GitHub
parent a600dfc9e3
commit a118d21af0
10 changed files with 300 additions and 13 deletions
+60
View File
@@ -79,6 +79,7 @@ type ParticipantParams struct {
Grants *auth.ClaimGrants
InitialVersion uint32
ClientConf *livekit.ClientConfiguration
ClientInfo ClientInfo
Region string
Migration bool
AdaptiveStream bool
@@ -405,6 +406,64 @@ func (p *ParticipantImpl) HandleOffer(offer webrtc.SessionDescription) error {
}
func (p *ParticipantImpl) setCodecPreferencesForPublisher(offer webrtc.SessionDescription) webrtc.SessionDescription {
offer = p.setCodecPreferencesOpusRedForPublisher(offer)
offer = p.setCodecPreferencesVideoForPublisher(offer)
return offer
}
func (p *ParticipantImpl) setCodecPreferencesOpusRedForPublisher(offer webrtc.SessionDescription) webrtc.SessionDescription {
parsed, lastAudio, err := p.TransportManager.GetLastUnmatchedMediaForOffer(offer, "audio")
if err != nil || lastAudio == nil {
return offer
}
codecs, err := codecsFromMediaDescription(lastAudio)
if err != nil {
return offer
}
var opusPayload uint8
for _, codec := range codecs {
if strings.EqualFold(codec.Name, "opus") {
opusPayload = codec.PayloadType
break
}
}
if opusPayload == 0 {
return offer
}
var preferredCodecs, leftCodecs []string
for _, codec := range codecs {
// codec contain opus/red
if strings.EqualFold(codec.Name, "red") && strings.Contains(codec.Fmtp, strconv.FormatInt(int64(opusPayload), 10)) {
preferredCodecs = append(preferredCodecs, strconv.FormatInt(int64(codec.PayloadType), 10))
} else {
leftCodecs = append(leftCodecs, strconv.FormatInt(int64(codec.PayloadType), 10))
}
}
// no opus/red found
if len(preferredCodecs) == 0 {
return offer
}
lastAudio.MediaName.Formats = append(lastAudio.MediaName.Formats[:0], preferredCodecs...)
lastAudio.MediaName.Formats = append(lastAudio.MediaName.Formats, leftCodecs...)
bytes, err := parsed.Marshal()
if err != nil {
p.params.Logger.Errorw("failed to marshal offer", err)
return offer
}
return webrtc.SessionDescription{
Type: offer.Type,
SDP: string(bytes),
}
}
func (p *ParticipantImpl) setCodecPreferencesVideoForPublisher(offer webrtc.SessionDescription) webrtc.SessionDescription {
parsed, lastVideo, err := p.TransportManager.GetLastUnmatchedMediaForOffer(offer, "video")
if err != nil || lastVideo == nil {
return offer
@@ -977,6 +1036,7 @@ func (p *ParticipantImpl) setupTransportManager() error {
EnabledCodecs: p.params.EnabledCodecs,
SimTracks: p.params.SimTracks,
ClientConf: p.params.ClientConf,
ClientInfo: p.params.ClientInfo,
Migration: p.params.Migration,
AllowTCPFallback: p.params.AllowTCPFallback,
Logger: p.params.Logger,