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:
+33
-5
@@ -55,6 +55,9 @@ type TrackReceiver interface {
|
||||
|
||||
GetLayerDimension(layer int32) (uint32, uint32)
|
||||
TrackInfo() *livekit.TrackInfo
|
||||
|
||||
// Get primary receiver if this receiver represents a RED codec; otherwise it will return itself
|
||||
GetPrimaryReceiverForRed() TrackReceiver
|
||||
}
|
||||
|
||||
// WebRTCReceiver receives a media track
|
||||
@@ -71,6 +74,7 @@ type WebRTCReceiver struct {
|
||||
codec webrtc.RTPCodecParameters
|
||||
isSimulcast bool
|
||||
isSVC bool
|
||||
isRED bool
|
||||
onCloseHandler func()
|
||||
closeOnce sync.Once
|
||||
closed atomic.Bool
|
||||
@@ -101,6 +105,8 @@ type WebRTCReceiver struct {
|
||||
|
||||
// update layer info
|
||||
onMaxLayerChange func(maxLayer int32)
|
||||
|
||||
primaryReceiver atomic.Value // *RedPrimaryReceiver
|
||||
}
|
||||
|
||||
func RidToLayer(rid string) int32 {
|
||||
@@ -126,6 +132,10 @@ func IsSvcCodec(mime string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func IsRedCodec(mime string) bool {
|
||||
return strings.HasSuffix(strings.ToLower(mime), "red")
|
||||
}
|
||||
|
||||
type ReceiverOpts func(w *WebRTCReceiver) *WebRTCReceiver
|
||||
|
||||
// WithPliThrottleConfig indicates minimum time(ms) between sending PLIs
|
||||
@@ -186,6 +196,7 @@ func NewWebRTCReceiver(
|
||||
streamTrackerManager: NewStreamTrackerManager(logger, trackInfo),
|
||||
trackInfo: trackInfo,
|
||||
isSVC: IsSvcCodec(track.Codec().MimeType),
|
||||
isRED: IsRedCodec(track.Codec().MimeType),
|
||||
}
|
||||
|
||||
w.streamTrackerManager.OnMaxLayerChanged(w.onMaxLayerChange)
|
||||
@@ -376,7 +387,7 @@ func (w *WebRTCReceiver) AddDownTrack(track TrackSender) error {
|
||||
}
|
||||
}
|
||||
|
||||
w.storeDownTrack(track)
|
||||
w.downTrackSpreader.Store(track)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -533,6 +544,9 @@ func (w *WebRTCReceiver) forwardRTP(layer int32) {
|
||||
w.closeOnce.Do(func() {
|
||||
w.closed.Store(true)
|
||||
w.closeTracks()
|
||||
if pr := w.primaryReceiver.Load(); pr != nil {
|
||||
pr.(*RedPrimaryReceiver).Close()
|
||||
}
|
||||
})
|
||||
|
||||
w.streamTrackerManager.RemoveTracker(layer)
|
||||
@@ -565,6 +579,9 @@ func (w *WebRTCReceiver) forwardRTP(layer int32) {
|
||||
w.downTrackSpreader.Broadcast(func(dt TrackSender) {
|
||||
_ = dt.WriteRTP(pkt, spatialLayer)
|
||||
})
|
||||
if pr := w.primaryReceiver.Load(); pr != nil {
|
||||
pr.(*RedPrimaryReceiver).ForwardRTP(pkt, spatialLayer)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -581,10 +598,6 @@ func (w *WebRTCReceiver) closeTracks() {
|
||||
}
|
||||
}
|
||||
|
||||
func (w *WebRTCReceiver) storeDownTrack(track TrackSender) {
|
||||
w.downTrackSpreader.Store(track)
|
||||
}
|
||||
|
||||
func (w *WebRTCReceiver) DebugInfo() map[string]interface{} {
|
||||
info := map[string]interface{}{
|
||||
"Simulcast": w.isSimulcast,
|
||||
@@ -607,3 +620,18 @@ func (w *WebRTCReceiver) DebugInfo() map[string]interface{} {
|
||||
|
||||
return info
|
||||
}
|
||||
|
||||
func (w *WebRTCReceiver) GetPrimaryReceiverForRed() TrackReceiver {
|
||||
if !w.isRED || w.closed.Load() {
|
||||
return w
|
||||
}
|
||||
|
||||
if w.primaryReceiver.Load() == nil {
|
||||
pr := NewRedPrimaryReceiver(w, DownTrackSpreaderParams{
|
||||
Threshold: w.lbThreshold,
|
||||
Logger: w.logger,
|
||||
})
|
||||
w.primaryReceiver.CompareAndSwap(nil, pr)
|
||||
}
|
||||
return w.primaryReceiver.Load().(*RedPrimaryReceiver)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
package sfu
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
|
||||
"go.uber.org/atomic"
|
||||
|
||||
"github.com/pion/rtp"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/sfu/buffer"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"github.com/livekit/protocol/logger"
|
||||
)
|
||||
|
||||
const (
|
||||
MimeTypeAudioRed = "audio/red"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrIncompleteRedHeader = errors.New("incomplete red block header")
|
||||
ErrIncompleteRedBlock = errors.New("incomplete red block payload")
|
||||
)
|
||||
|
||||
type RedPrimaryReceiver struct {
|
||||
TrackReceiver
|
||||
downTrackSpreader *DownTrackSpreader
|
||||
logger logger.Logger
|
||||
closed atomic.Bool
|
||||
}
|
||||
|
||||
func NewRedPrimaryReceiver(receiver TrackReceiver, dsp DownTrackSpreaderParams) *RedPrimaryReceiver {
|
||||
return &RedPrimaryReceiver{
|
||||
TrackReceiver: receiver,
|
||||
downTrackSpreader: NewDownTrackSpreader(dsp),
|
||||
logger: dsp.Logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RedPrimaryReceiver) ForwardRTP(pkt *buffer.ExtPacket, spatialLayer int32) {
|
||||
// extract primary payload from RED and forward to downtracks
|
||||
if r.downTrackSpreader.DownTrackCount() == 0 {
|
||||
return
|
||||
}
|
||||
payload, err := ExtractPrimaryEncodingForRED(pkt.Packet.Payload)
|
||||
if err != nil {
|
||||
r.logger.Errorw("get primary encoding for red failed", err, "payloadtype", pkt.Packet.PayloadType)
|
||||
return
|
||||
}
|
||||
|
||||
pPkt := *pkt
|
||||
primaryRtpPacket := *pkt.Packet
|
||||
primaryRtpPacket.Payload = payload
|
||||
pPkt.Packet = &primaryRtpPacket
|
||||
|
||||
// not modify the ExtPacket.RawPacket here for performance since it is not used by the DownTrack,
|
||||
// otherwise it should be set to the correct value (marshal the primary rtp packet)
|
||||
|
||||
r.downTrackSpreader.Broadcast(func(dt TrackSender) {
|
||||
_ = dt.WriteRTP(&pPkt, spatialLayer)
|
||||
})
|
||||
|
||||
// TODO : detect rtp packet lost, recover it from the redundant payload then send to downstreams.
|
||||
}
|
||||
|
||||
func (r *RedPrimaryReceiver) AddDownTrack(track TrackSender) error {
|
||||
if r.closed.Load() {
|
||||
return ErrReceiverClosed
|
||||
}
|
||||
|
||||
if r.downTrackSpreader.HasDownTrack(track.SubscriberID()) {
|
||||
r.logger.Infow("subscriberID already exists, replace the downtrack", "subscriberID", track.SubscriberID())
|
||||
}
|
||||
|
||||
r.downTrackSpreader.Store(track)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *RedPrimaryReceiver) DeleteDownTrack(subscriberID livekit.ParticipantID) {
|
||||
if r.closed.Load() {
|
||||
return
|
||||
}
|
||||
|
||||
r.downTrackSpreader.Free(subscriberID)
|
||||
}
|
||||
|
||||
func (r *RedPrimaryReceiver) Close() {
|
||||
r.closed.Store(true)
|
||||
for _, dt := range r.downTrackSpreader.ResetAndGetDownTracks() {
|
||||
dt.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RedPrimaryReceiver) ReadRTP(buf []byte, layer uint8, sn uint16) (int, error) {
|
||||
n, err := r.TrackReceiver.ReadRTP(buf, layer, sn)
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
|
||||
var pkt rtp.Packet
|
||||
pkt.Unmarshal(buf[:n])
|
||||
payload, err := ExtractPrimaryEncodingForRED(pkt.Payload)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
pkt.Payload = payload
|
||||
|
||||
return pkt.MarshalTo(buf)
|
||||
}
|
||||
|
||||
func ExtractPrimaryEncodingForRED(payload []byte) ([]byte, error) {
|
||||
|
||||
/* RED payload https://datatracker.ietf.org/doc/html/rfc2198#section-3
|
||||
0 1 2 3
|
||||
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
|F| block PT | timestamp offset | block length |
|
||||
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
F: 1 bit First bit in header indicates whether another header block
|
||||
follows. If 1 further header blocks follow, if 0 this is the
|
||||
last header block.
|
||||
*/
|
||||
|
||||
var blockLength int
|
||||
for {
|
||||
if payload[0]&0x80 == 0 {
|
||||
// last block is primary encoding data
|
||||
payload = payload[1:]
|
||||
break
|
||||
} else {
|
||||
if len(payload) < 4 {
|
||||
// illegal data
|
||||
return nil, ErrIncompleteRedHeader
|
||||
}
|
||||
blockLength += int(binary.BigEndian.Uint16(payload[2:]) & 0x03FF)
|
||||
payload = payload[4:]
|
||||
}
|
||||
}
|
||||
|
||||
if len(payload) < blockLength {
|
||||
return nil, ErrIncompleteRedBlock
|
||||
}
|
||||
|
||||
return payload[blockLength:], nil
|
||||
}
|
||||
Reference in New Issue
Block a user