move to ion-SFU's receiver and downtrack
This commit is contained in:
@@ -80,6 +80,10 @@ func (t *DataTrack) IsMuted() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (t *DataTrack) SetMuted(muted bool) {
|
||||
|
||||
}
|
||||
|
||||
func (t *DataTrack) OnClose(f func()) {
|
||||
t.onClose = f
|
||||
}
|
||||
|
||||
+123
-182
@@ -1,20 +1,18 @@
|
||||
package rtc
|
||||
|
||||
import (
|
||||
"io"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gammazero/workerpool"
|
||||
"github.com/pion/ion-sfu/pkg/buffer"
|
||||
"github.com/pion/ion-sfu/pkg/sfu"
|
||||
"github.com/pion/ion-sfu/pkg/twcc"
|
||||
"github.com/pion/rtcp"
|
||||
"github.com/pion/transport/packetio"
|
||||
"github.com/pion/webrtc/v3"
|
||||
"github.com/pion/webrtc/v3/pkg/rtcerr"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/logger"
|
||||
"github.com/livekit/livekit-server/pkg/rtc/types"
|
||||
"github.com/livekit/livekit-server/pkg/sfu"
|
||||
"github.com/livekit/livekit-server/pkg/utils"
|
||||
"github.com/livekit/livekit-server/proto/livekit"
|
||||
)
|
||||
@@ -40,21 +38,21 @@ type MediaTrack struct {
|
||||
streamID string
|
||||
kind livekit.TrackType
|
||||
codec webrtc.RTPCodecParameters
|
||||
conf ReceiverConfig
|
||||
onClose func()
|
||||
|
||||
// channel to send RTCP packets to the source
|
||||
rtcpCh *utils.CalmChannel
|
||||
rtcpCh chan []rtcp.Packet
|
||||
lock sync.RWMutex
|
||||
once sync.Once
|
||||
// map of target participantId -> DownTrack
|
||||
downtracks map[string]types.DownTrack
|
||||
receiver types.Receiver
|
||||
nackWorker *workerpool.WorkerPool
|
||||
downtracks map[string]*sfu.DownTrack
|
||||
twcc *twcc.Responder
|
||||
receiver sfu.Receiver
|
||||
//lastNack int64
|
||||
lastPLI time.Time
|
||||
}
|
||||
|
||||
func NewMediaTrack(trackId string, pId string, rtcpCh *utils.CalmChannel, track *webrtc.TrackRemote, receiver types.Receiver) *MediaTrack {
|
||||
func NewMediaTrack(trackId string, pId string, rtcpCh chan []rtcp.Packet, conf ReceiverConfig, track *webrtc.TrackRemote) *MediaTrack {
|
||||
t := &MediaTrack{
|
||||
id: trackId,
|
||||
participantId: pId,
|
||||
@@ -62,22 +60,16 @@ func NewMediaTrack(trackId string, pId string, rtcpCh *utils.CalmChannel, track
|
||||
streamID: track.StreamID(),
|
||||
kind: ToProtoTrackKind(track.Kind()),
|
||||
codec: track.Codec(),
|
||||
conf: conf,
|
||||
rtcpCh: rtcpCh,
|
||||
lock: sync.RWMutex{},
|
||||
once: sync.Once{},
|
||||
downtracks: make(map[string]types.DownTrack),
|
||||
receiver: receiver,
|
||||
nackWorker: workerpool.New(1),
|
||||
downtracks: make(map[string]*sfu.DownTrack),
|
||||
}
|
||||
|
||||
return t
|
||||
}
|
||||
|
||||
func (t *MediaTrack) Start() {
|
||||
t.once.Do(func() {
|
||||
// start worker
|
||||
go t.forwardRTPWorker()
|
||||
})
|
||||
}
|
||||
|
||||
func (t *MediaTrack) ID() string {
|
||||
@@ -96,6 +88,18 @@ func (t *MediaTrack) IsMuted() bool {
|
||||
return t.muted.Get()
|
||||
}
|
||||
|
||||
func (t *MediaTrack) SetMuted(muted bool) {
|
||||
if !t.muted.TrySet(muted) {
|
||||
return
|
||||
}
|
||||
// mute all of the downtracks
|
||||
t.lock.RLock()
|
||||
for _, dt := range t.downtracks {
|
||||
dt.Mute(muted)
|
||||
}
|
||||
t.lock.RUnlock()
|
||||
}
|
||||
|
||||
func (t *MediaTrack) OnClose(f func()) {
|
||||
t.onClose = f
|
||||
}
|
||||
@@ -116,80 +120,142 @@ func (t *MediaTrack) AddSubscriber(participant types.Participant) error {
|
||||
}
|
||||
|
||||
codec := t.codec
|
||||
// pack ID to identify all publishedTracks
|
||||
packedId := PackTrackId(t.participantId, t.id)
|
||||
|
||||
// using DownTrack from ion-sfu
|
||||
outTrack, err := sfu.NewDownTrack(webrtc.RTPCodecCapability{
|
||||
downTrack, err := sfu.NewDownTrack(webrtc.RTPCodecCapability{
|
||||
MimeType: codec.MimeType,
|
||||
ClockRate: codec.ClockRate,
|
||||
Channels: codec.Channels,
|
||||
SDPFmtpLine: codec.SDPFmtpLine,
|
||||
RTCPFeedback: feedbackTypes,
|
||||
}, packedId, t.streamID)
|
||||
}, t.receiver, t.participantId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
transceiver, err := participant.PeerConnection().AddTransceiverFromTrack(outTrack, webrtc.RTPTransceiverInit{
|
||||
transceiver, err := participant.PeerConnection().AddTransceiverFromTrack(downTrack, webrtc.RTPTransceiverInit{
|
||||
Direction: webrtc.RTPTransceiverDirectionSendrecv,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
outTrack.SetTransceiver(transceiver)
|
||||
downTrack.SetTransceiver(transceiver)
|
||||
// when outtrack is bound, start loop to send reports
|
||||
outTrack.OnBind(func() {
|
||||
if rr := bufferFactory.GetOrNew(packetio.RTCPBufferPacket, outTrack.SSRC()).(*buffer.RTCPReader); rr != nil {
|
||||
rr.OnPacket(func(pkt []byte) {
|
||||
t.handleRTCP(outTrack, participant.Identity(), pkt)
|
||||
})
|
||||
}
|
||||
downTrack.OnBind(func() {
|
||||
t.sendDownTrackBindingReports(participant.ID(), participant.RTCPChan())
|
||||
})
|
||||
outTrack.OnCloseHandler(func() {
|
||||
downTrack.OnCloseHandler(func() {
|
||||
t.lock.Lock()
|
||||
delete(t.downtracks, participant.ID())
|
||||
t.lock.Unlock()
|
||||
|
||||
// ignore if the subscribing participant is not connected
|
||||
if participant.PeerConnection().ConnectionState() != webrtc.PeerConnectionStateConnected {
|
||||
if participant.PeerConnection().ConnectionState() == webrtc.PeerConnectionStateClosed {
|
||||
return
|
||||
}
|
||||
|
||||
// if the source has been terminated, we'll need to terminate all of the downtracks
|
||||
// however, if the dest participant has disconnected, then we can skip
|
||||
sender := transceiver.Sender()
|
||||
if sender != nil {
|
||||
logger.Debugw("removing peerconnection track",
|
||||
"track", t.id,
|
||||
"participantId", t.participantId,
|
||||
"destParticipant", participant.Identity())
|
||||
if err := participant.PeerConnection().RemoveTrack(sender); err != nil {
|
||||
if err == webrtc.ErrConnectionClosed {
|
||||
// participant closing, can skip removing downtracks
|
||||
return
|
||||
}
|
||||
if _, ok := err.(*rtcerr.InvalidStateError); !ok {
|
||||
logger.Warnw("could not remove remoteTrack from forwarder",
|
||||
"participant", participant.Identity(),
|
||||
"err", err)
|
||||
}
|
||||
if sender == nil {
|
||||
return
|
||||
}
|
||||
logger.Debugw("removing peerconnection track",
|
||||
"track", t.id,
|
||||
"participantId", t.participantId,
|
||||
"destParticipant", participant.Identity())
|
||||
if err := participant.PeerConnection().RemoveTrack(sender); err != nil {
|
||||
if err == webrtc.ErrConnectionClosed {
|
||||
// participant closing, can skip removing downtracks
|
||||
return
|
||||
}
|
||||
if _, ok := err.(*rtcerr.InvalidStateError); !ok {
|
||||
logger.Warnw("could not remove remoteTrack from forwarder",
|
||||
"participant", participant.Identity(),
|
||||
"err", err)
|
||||
}
|
||||
}
|
||||
|
||||
participant.RemoveDownTrack(t.streamID, outTrack)
|
||||
participant.RemoveDownTrack(t.streamID, downTrack)
|
||||
})
|
||||
participant.AddDownTrack(t.streamID, outTrack)
|
||||
|
||||
t.lock.Lock()
|
||||
defer t.lock.Unlock()
|
||||
t.downtracks[participant.ID()] = outTrack
|
||||
downTrack.Mute(t.muted.Get())
|
||||
t.downtracks[participant.ID()] = downTrack
|
||||
t.lock.Unlock()
|
||||
|
||||
participant.AddDownTrack(t.streamID, downTrack)
|
||||
t.receiver.AddDownTrack(downTrack, true)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// adds a new RTP receiver to the track, returns true if this is a new track
|
||||
func (t *MediaTrack) AddReceiver(receiver *webrtc.RTPReceiver, track *webrtc.TrackRemote) {
|
||||
//rid := track.RID()
|
||||
|
||||
buff, rtcpReader := bufferFactory.GetBufferPair(uint32(track.SSRC()))
|
||||
buff.OnFeedback(func(fb []rtcp.Packet) {
|
||||
t.rtcpCh <- fb
|
||||
})
|
||||
|
||||
if t.Kind() == livekit.TrackType_AUDIO {
|
||||
// TODO: audio level stuff
|
||||
} else if t.Kind() == livekit.TrackType_VIDEO {
|
||||
if t.twcc == nil {
|
||||
t.twcc = twcc.NewTransportWideCCResponder(uint32(track.SSRC()))
|
||||
t.twcc.OnFeedback(func(p rtcp.RawPacket) {
|
||||
t.rtcpCh <- []rtcp.Packet{&p}
|
||||
})
|
||||
}
|
||||
buff.OnTransportWideCC(func(sn uint16, timeNS int64, marker bool) {
|
||||
t.twcc.Push(sn, timeNS, marker)
|
||||
})
|
||||
}
|
||||
|
||||
rtcpReader.OnPacket(func(bytes []byte) {
|
||||
pkts, err := rtcp.Unmarshal(bytes)
|
||||
if err != nil {
|
||||
logger.Errorw("could not unmarshal RTCP", "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, pkt := range pkts {
|
||||
switch pkt := pkt.(type) {
|
||||
case *rtcp.SourceDescription:
|
||||
// do nothing for now
|
||||
case *rtcp.SenderReport:
|
||||
buff.SetSenderReportData(pkt.RTPTime, pkt.NTPTime)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
if t.receiver == nil {
|
||||
// pack ID to identify all publishedTracks
|
||||
packedId := PackTrackId(t.participantId, track.ID())
|
||||
t.lock.Lock()
|
||||
t.receiver = NewWrappedReceiver(sfu.NewWebRTCReceiver(receiver, track, t.participantId), packedId)
|
||||
t.lock.Unlock()
|
||||
t.receiver.SetRTCPCh(t.rtcpCh)
|
||||
t.receiver.OnCloseHandler(func() {
|
||||
// source track closed
|
||||
if t.Kind() == livekit.TrackType_AUDIO {
|
||||
// TODO: remove audio level observer
|
||||
}
|
||||
t.lock.Lock()
|
||||
t.receiver = nil
|
||||
t.lock.Unlock()
|
||||
})
|
||||
}
|
||||
t.receiver.AddUpTrack(track, buff)
|
||||
|
||||
buff.Bind(receiver.GetParameters(), buffer.Options{
|
||||
BufferTime: t.conf.maxBufferTime,
|
||||
MaxBitRate: t.conf.maxBitrate,
|
||||
})
|
||||
}
|
||||
|
||||
// removes peer from subscription
|
||||
// stop all forwarders to the peer
|
||||
func (t *MediaTrack) RemoveSubscriber(participantId string) {
|
||||
@@ -208,19 +274,20 @@ func (t *MediaTrack) RemoveAllSubscribers() {
|
||||
for _, dt := range t.downtracks {
|
||||
go dt.Close()
|
||||
}
|
||||
t.downtracks = make(map[string]types.DownTrack)
|
||||
t.downtracks = make(map[string]*sfu.DownTrack)
|
||||
}
|
||||
|
||||
func (t *MediaTrack) sendDownTrackBindingReports(participantId string, rtcpCh *utils.CalmChannel) {
|
||||
func (t *MediaTrack) sendDownTrackBindingReports(participantId string, rtcpCh chan []rtcp.Packet) {
|
||||
var sd []rtcp.SourceDescriptionChunk
|
||||
|
||||
t.lock.RLock()
|
||||
dt := t.downtracks[participantId]
|
||||
t.lock.RUnlock()
|
||||
if !dt.IsBound() {
|
||||
|
||||
chunks := dt.CreateSourceDescriptionChunks()
|
||||
if chunks == nil {
|
||||
return
|
||||
}
|
||||
chunks := dt.CreateSourceDescriptionChunks()
|
||||
if chunks != nil {
|
||||
sd = append(sd, chunks...)
|
||||
}
|
||||
@@ -234,7 +301,7 @@ func (t *MediaTrack) sendDownTrackBindingReports(participantId string, rtcpCh *u
|
||||
batch := pkts
|
||||
i := 0
|
||||
for {
|
||||
rtcpCh.Write(batch)
|
||||
rtcpCh <- batch
|
||||
if i > 5 {
|
||||
return
|
||||
}
|
||||
@@ -243,129 +310,3 @@ func (t *MediaTrack) sendDownTrackBindingReports(participantId string, rtcpCh *u
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// b reads from the receiver and writes to each sender
|
||||
func (t *MediaTrack) forwardRTPWorker() {
|
||||
defer Recover()
|
||||
defer func() {
|
||||
t.RemoveAllSubscribers()
|
||||
// TODO: send unpublished events?
|
||||
t.nackWorker.Stop()
|
||||
|
||||
if t.onClose != nil {
|
||||
t.onClose()
|
||||
}
|
||||
}()
|
||||
|
||||
for pkt := range t.receiver.RTPChan() {
|
||||
//logger.Debugw("read packet from remoteTrack",
|
||||
// "participant", t.participantId,
|
||||
// "track", t.ID())
|
||||
// when track is muted, it's "disabled" on the client side, and will still be sending black frames
|
||||
// when our metadata is updated as such, we shortcircuit forwarding of black frames
|
||||
if t.muted.Get() {
|
||||
continue
|
||||
}
|
||||
|
||||
t.lock.RLock()
|
||||
for dstId, dt := range t.downtracks {
|
||||
//logger.Debugw("read packet from remoteTrack",
|
||||
// "srcParticipant", t.participantId,
|
||||
// "destParticipant", dstId,
|
||||
// "track", t.ID())
|
||||
err := dt.WriteRTP(pkt.Packet)
|
||||
if IsEOF(err) {
|
||||
// this participant unsubscribed, remove it
|
||||
t.RemoveSubscriber(dstId)
|
||||
continue
|
||||
}
|
||||
|
||||
if err == sfu.ErrRequiresKeyFrame {
|
||||
delta := time.Now().Sub(t.lastPLI)
|
||||
if delta < maxPLIFrequency {
|
||||
continue
|
||||
}
|
||||
logger.Infow("keyframe required, sending PLI",
|
||||
"srcParticipant", t.participantId)
|
||||
rtcpPkts := []rtcp.Packet{
|
||||
&rtcp.PictureLossIndication{SenderSSRC: uint32(t.ssrc), MediaSSRC: pkt.Packet.SSRC},
|
||||
}
|
||||
t.rtcpCh.Write(rtcpPkts)
|
||||
t.lastPLI = time.Now()
|
||||
} else if err != nil {
|
||||
logger.Warnw("could not forward packet to participant",
|
||||
"src", t.participantId,
|
||||
"dest", dstId,
|
||||
"remoteTrack", t.id,
|
||||
"err", err)
|
||||
}
|
||||
}
|
||||
t.lock.RUnlock()
|
||||
}
|
||||
}
|
||||
|
||||
func (t *MediaTrack) handleRTCP(dt *sfu.DownTrack, identity string, rtcpBuf []byte) {
|
||||
defer Recover()
|
||||
pkts, err := rtcp.Unmarshal(rtcpBuf)
|
||||
if err != nil {
|
||||
logger.Warnw("could not decode RTCP packet", "err", err)
|
||||
}
|
||||
|
||||
var fwdPkts []rtcp.Packet
|
||||
// sufficient to send this only once
|
||||
pliOnce := true
|
||||
firOnce := true
|
||||
for _, pkt := range pkts {
|
||||
switch p := pkt.(type) {
|
||||
case *rtcp.PictureLossIndication:
|
||||
if pliOnce {
|
||||
p.MediaSSRC = dt.LastSSRC()
|
||||
p.SenderSSRC = dt.SSRC()
|
||||
fwdPkts = append(fwdPkts, p)
|
||||
pliOnce = false
|
||||
}
|
||||
case *rtcp.FullIntraRequest:
|
||||
if firOnce {
|
||||
p.MediaSSRC = dt.LastSSRC()
|
||||
p.SenderSSRC = dt.SSRC()
|
||||
fwdPkts = append(fwdPkts, p)
|
||||
firOnce = false
|
||||
}
|
||||
case *rtcp.ReceiverReport:
|
||||
if len(p.Reports) > 0 && p.Reports[0].FractionLost > 25 {
|
||||
//log.Tracef("Slow link for sender %s, fraction packet lost %.2f", f.track.peerID, float64(p.Reports[0].FractionLost)/256)
|
||||
}
|
||||
case *rtcp.TransportLayerNack:
|
||||
logger.Debugw("forwarder got nack",
|
||||
"participant", identity,
|
||||
"packet", p)
|
||||
var nackedPackets []uint16
|
||||
for _, pair := range p.Nacks {
|
||||
nackedPackets = append(nackedPackets, dt.GetNACKSeqNo(pair.PacketList())...)
|
||||
}
|
||||
|
||||
if t.nackWorker.Stopped() {
|
||||
return
|
||||
}
|
||||
t.nackWorker.Submit(func() {
|
||||
pktBuf := packetFactory.Get().([]byte)
|
||||
for _, sn := range nackedPackets {
|
||||
pkt, err := t.receiver.GetBufferedPacket(pktBuf, sn, dt.SnOffset())
|
||||
if err == io.EOF {
|
||||
break
|
||||
} else if err != nil {
|
||||
logger.Warnw("error getting buffered packet", "error", err)
|
||||
continue
|
||||
}
|
||||
// what about handling write errors such as no packet found
|
||||
dt.WriteRTP(pkt)
|
||||
}
|
||||
packetFactory.Put(pktBuf)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if len(fwdPkts) > 0 {
|
||||
t.rtcpCh.Write(fwdPkts)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,112 +0,0 @@
|
||||
package rtc
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gammazero/workerpool"
|
||||
"github.com/pion/ion-sfu/pkg/buffer"
|
||||
"github.com/pion/rtcp"
|
||||
"github.com/pion/webrtc/v3"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/rtc/types"
|
||||
"github.com/livekit/livekit-server/pkg/rtc/types/typesfakes"
|
||||
"github.com/livekit/livekit-server/pkg/sfu"
|
||||
"github.com/livekit/livekit-server/pkg/utils"
|
||||
"github.com/livekit/livekit-server/proto/livekit"
|
||||
)
|
||||
|
||||
const (
|
||||
testWaitDuration = 10 * time.Millisecond
|
||||
)
|
||||
|
||||
func TestForwardRTP(t *testing.T) {
|
||||
t.Run("ensure that forwarders are getting packets", func(t *testing.T) {
|
||||
mt := newMediaTrackWithReceiver()
|
||||
receiver := mt.receiver.(*typesfakes.FakeReceiver)
|
||||
packet := buffer.ExtPacket{}
|
||||
receiver.RTPChanReturns(packetGenerator(packet))
|
||||
|
||||
dt := &typesfakes.FakeDownTrack{}
|
||||
mt.downtracks["test"] = dt
|
||||
|
||||
mt.Start()
|
||||
time.Sleep(testWaitDuration)
|
||||
|
||||
assert.Equal(t, 1, dt.WriteRTPCallCount(), "WriteRTP wasn't called on Forwarder")
|
||||
assert.EqualValues(t, packet.Packet, dt.WriteRTPArgsForCall(0))
|
||||
})
|
||||
|
||||
t.Run("muted tracks do not forward data", func(t *testing.T) {
|
||||
mt := newMediaTrackWithReceiver()
|
||||
mt.muted.TrySet(true)
|
||||
|
||||
dt := &typesfakes.FakeDownTrack{}
|
||||
mt.downtracks["test"] = dt
|
||||
|
||||
mt.Start()
|
||||
time.Sleep(testWaitDuration)
|
||||
assert.Zero(t, dt.WriteRTPCallCount())
|
||||
})
|
||||
}
|
||||
|
||||
func TestMissingKeyFrames(t *testing.T) {
|
||||
t.Run("PLI packet is sent when forwarder misses keyframe", func(t *testing.T) {
|
||||
mt := newMediaTrackWithReceiver()
|
||||
|
||||
dt := &typesfakes.FakeDownTrack{}
|
||||
mt.downtracks["test"] = dt
|
||||
dt.WriteRTPReturns(sfu.ErrRequiresKeyFrame)
|
||||
|
||||
mt.Start()
|
||||
time.Sleep(testWaitDuration)
|
||||
|
||||
select {
|
||||
case o := <-mt.rtcpCh.ReadChan():
|
||||
if o == nil {
|
||||
return
|
||||
}
|
||||
pkts, ok := o.([]rtcp.Packet)
|
||||
assert.True(t, ok)
|
||||
assert.Len(t, pkts, 1, "a single RTCP packet should be returned")
|
||||
assert.IsType(t, &rtcp.PictureLossIndication{}, pkts[0])
|
||||
default:
|
||||
t.Fatalf("did not receive RTCP packets")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// returns a receiver that reads a packet then returns EOF
|
||||
func newMediaTrackWithReceiver() *MediaTrack {
|
||||
packet := buffer.ExtPacket{}
|
||||
receiver := &typesfakes.FakeReceiver{
|
||||
RTPChanStub: func() <-chan buffer.ExtPacket {
|
||||
return packetGenerator(packet)
|
||||
},
|
||||
}
|
||||
return &MediaTrack{
|
||||
id: utils.NewGuid(utils.TrackPrefix),
|
||||
participantId: "PAtest",
|
||||
kind: livekit.TrackType_VIDEO,
|
||||
codec: webrtc.RTPCodecParameters{},
|
||||
rtcpCh: utils.NewCalmChannel(5),
|
||||
lock: sync.RWMutex{},
|
||||
once: sync.Once{},
|
||||
downtracks: map[string]types.DownTrack{},
|
||||
receiver: receiver,
|
||||
nackWorker: workerpool.New(1),
|
||||
}
|
||||
}
|
||||
|
||||
func packetGenerator(packets ...buffer.ExtPacket) <-chan buffer.ExtPacket {
|
||||
pc := make(chan buffer.ExtPacket)
|
||||
go func() {
|
||||
defer close(pc)
|
||||
for _, p := range packets {
|
||||
pc <- p
|
||||
}
|
||||
}()
|
||||
return pc
|
||||
}
|
||||
+32
-20
@@ -7,6 +7,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/bep/debounce"
|
||||
"github.com/pion/ion-sfu/pkg/sfu"
|
||||
"github.com/pion/rtcp"
|
||||
"github.com/pion/webrtc/v3"
|
||||
"github.com/pkg/errors"
|
||||
@@ -15,7 +16,6 @@ import (
|
||||
"github.com/livekit/livekit-server/pkg/logger"
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
"github.com/livekit/livekit-server/pkg/rtc/types"
|
||||
"github.com/livekit/livekit-server/pkg/sfu"
|
||||
"github.com/livekit/livekit-server/pkg/utils"
|
||||
"github.com/livekit/livekit-server/proto/livekit"
|
||||
)
|
||||
@@ -41,7 +41,7 @@ type ParticipantImpl struct {
|
||||
mediaEngine *webrtc.MediaEngine
|
||||
identity string
|
||||
state atomic.Value // livekit.ParticipantInfo_State
|
||||
rtcpCh *utils.CalmChannel
|
||||
rtcpCh chan []rtcp.Packet
|
||||
subscribedTracks map[string][]*sfu.DownTrack
|
||||
// publishedTracks that participant is publishing
|
||||
publishedTracks map[string]types.PublishedTrack
|
||||
@@ -85,7 +85,7 @@ func NewParticipant(identity string, pc types.PeerConnection, rs routing.Message
|
||||
peerConn: pc,
|
||||
responseSink: rs,
|
||||
receiverConfig: receiverConfig,
|
||||
rtcpCh: utils.NewCalmChannel(50),
|
||||
rtcpCh: make(chan []rtcp.Packet, 50),
|
||||
subscribedTracks: make(map[string][]*sfu.DownTrack),
|
||||
lock: sync.RWMutex{},
|
||||
negotiationCond: sync.NewCond(&sync.Mutex{}),
|
||||
@@ -162,7 +162,7 @@ func (p *ParticipantImpl) IsReady() bool {
|
||||
return state == livekit.ParticipantInfo_JOINED || state == livekit.ParticipantInfo_ACTIVE
|
||||
}
|
||||
|
||||
func (p *ParticipantImpl) RTCPChan() *utils.CalmChannel {
|
||||
func (p *ParticipantImpl) RTCPChan() chan []rtcp.Packet {
|
||||
return p.rtcpCh
|
||||
}
|
||||
|
||||
@@ -361,7 +361,7 @@ func (p *ParticipantImpl) Close() error {
|
||||
p.onClose(p)
|
||||
}
|
||||
p.peerConn.Close()
|
||||
p.rtcpCh.Close()
|
||||
close(p.rtcpCh)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -538,11 +538,28 @@ func (p *ParticipantImpl) onMediaTrack(track *webrtc.TrackRemote, rtpReceiver *w
|
||||
}
|
||||
|
||||
// create ReceiverImpl
|
||||
receiver := NewReceiver(p.rtcpCh, rtpReceiver, track, p.receiverConfig)
|
||||
mt := NewMediaTrack(ti.Sid, p.id, p.rtcpCh, track, receiver)
|
||||
mt.name = ti.Name
|
||||
//receiver := NewReceiver(p.rtcpCh, rtpReceiver, track, p.receiverConfig)
|
||||
|
||||
p.handleTrackPublished(mt)
|
||||
// use existing mediatrack to handle simulcast
|
||||
p.lock.RLock()
|
||||
ptrack := p.publishedTracks[ti.Sid]
|
||||
p.lock.RUnlock()
|
||||
|
||||
var mt *MediaTrack
|
||||
var newTrack bool
|
||||
if trk, ok := ptrack.(*MediaTrack); ok {
|
||||
mt = trk
|
||||
} else {
|
||||
mt = NewMediaTrack(ti.Sid, p.id, p.rtcpCh, p.receiverConfig, track)
|
||||
mt.name = ti.Name
|
||||
newTrack = true
|
||||
}
|
||||
|
||||
mt.AddReceiver(rtpReceiver, track)
|
||||
|
||||
if newTrack {
|
||||
p.handleTrackPublished(mt)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ParticipantImpl) onDataChannel(dc *webrtc.DataChannel) {
|
||||
@@ -625,14 +642,13 @@ func (p *ParticipantImpl) downTracksRTCPWorker() {
|
||||
p.lock.RLock()
|
||||
for _, dts := range p.subscribedTracks {
|
||||
for _, dt := range dts {
|
||||
if !dt.IsBound() {
|
||||
sr := dt.CreateSenderReport()
|
||||
chunks := dt.CreateSourceDescriptionChunks()
|
||||
if sr == nil || chunks == nil {
|
||||
continue
|
||||
}
|
||||
pkts = append(pkts, dt.CreateSenderReport())
|
||||
chunks := dt.CreateSourceDescriptionChunks()
|
||||
if chunks != nil {
|
||||
sd = append(sd, chunks...)
|
||||
}
|
||||
sd = append(sd, chunks...)
|
||||
}
|
||||
}
|
||||
p.lock.RUnlock()
|
||||
@@ -664,14 +680,10 @@ func (p *ParticipantImpl) downTracksRTCPWorker() {
|
||||
func (p *ParticipantImpl) rtcpSendWorker() {
|
||||
defer Recover()
|
||||
// read from rtcpChan
|
||||
for o := range p.rtcpCh.ReadChan() {
|
||||
if o == nil {
|
||||
for pkts := range p.rtcpCh {
|
||||
if pkts == nil {
|
||||
return
|
||||
}
|
||||
pkts, ok := o.([]rtcp.Packet)
|
||||
if !ok {
|
||||
logger.Errorw("unexpected obj from rtcpCh", "object", o)
|
||||
}
|
||||
//for _, pkt := range pkts {
|
||||
// logger.Debugw("writing RTCP", "packet", pkt)
|
||||
//}
|
||||
|
||||
@@ -4,13 +4,12 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/pion/ion-sfu/pkg/buffer"
|
||||
"github.com/pion/ion-sfu/pkg/sfu"
|
||||
"github.com/pion/rtcp"
|
||||
"github.com/pion/rtp"
|
||||
"github.com/pion/webrtc/v3"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
"github.com/livekit/livekit-server/pkg/sfu"
|
||||
"github.com/livekit/livekit-server/pkg/utils"
|
||||
"github.com/livekit/livekit-server/proto/livekit"
|
||||
)
|
||||
|
||||
@@ -54,7 +53,7 @@ type Participant interface {
|
||||
State() livekit.ParticipantInfo_State
|
||||
IsReady() bool
|
||||
ToProto() *livekit.ParticipantInfo
|
||||
RTCPChan() *utils.CalmChannel
|
||||
RTCPChan() chan []rtcp.Packet
|
||||
GetResponseSink() routing.MessageSink
|
||||
SetResponseSink(sink routing.MessageSink)
|
||||
|
||||
@@ -97,6 +96,7 @@ type PublishedTrack interface {
|
||||
Kind() livekit.TrackType
|
||||
Name() string
|
||||
IsMuted() bool
|
||||
SetMuted(muted bool)
|
||||
AddSubscriber(participant Participant) error
|
||||
RemoveSubscriber(participantId string)
|
||||
RemoveAllSubscribers()
|
||||
|
||||
@@ -6,9 +6,9 @@ import (
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
"github.com/livekit/livekit-server/pkg/rtc/types"
|
||||
"github.com/livekit/livekit-server/pkg/sfu"
|
||||
"github.com/livekit/livekit-server/pkg/utils"
|
||||
"github.com/livekit/livekit-server/proto/livekit"
|
||||
"github.com/pion/ion-sfu/pkg/sfu"
|
||||
"github.com/pion/rtcp"
|
||||
webrtc "github.com/pion/webrtc/v3"
|
||||
)
|
||||
|
||||
@@ -161,15 +161,15 @@ type FakeParticipant struct {
|
||||
peerConnectionReturnsOnCall map[int]struct {
|
||||
result1 types.PeerConnection
|
||||
}
|
||||
RTCPChanStub func() *utils.CalmChannel
|
||||
RTCPChanStub func() chan []rtcp.Packet
|
||||
rTCPChanMutex sync.RWMutex
|
||||
rTCPChanArgsForCall []struct {
|
||||
}
|
||||
rTCPChanReturns struct {
|
||||
result1 *utils.CalmChannel
|
||||
result1 chan []rtcp.Packet
|
||||
}
|
||||
rTCPChanReturnsOnCall map[int]struct {
|
||||
result1 *utils.CalmChannel
|
||||
result1 chan []rtcp.Packet
|
||||
}
|
||||
RemoveDownTrackStub func(string, *sfu.DownTrack)
|
||||
removeDownTrackMutex sync.RWMutex
|
||||
@@ -1060,7 +1060,7 @@ func (fake *FakeParticipant) PeerConnectionReturnsOnCall(i int, result1 types.Pe
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) RTCPChan() *utils.CalmChannel {
|
||||
func (fake *FakeParticipant) RTCPChan() chan []rtcp.Packet {
|
||||
fake.rTCPChanMutex.Lock()
|
||||
ret, specificReturn := fake.rTCPChanReturnsOnCall[len(fake.rTCPChanArgsForCall)]
|
||||
fake.rTCPChanArgsForCall = append(fake.rTCPChanArgsForCall, struct {
|
||||
@@ -1084,32 +1084,32 @@ func (fake *FakeParticipant) RTCPChanCallCount() int {
|
||||
return len(fake.rTCPChanArgsForCall)
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) RTCPChanCalls(stub func() *utils.CalmChannel) {
|
||||
func (fake *FakeParticipant) RTCPChanCalls(stub func() chan []rtcp.Packet) {
|
||||
fake.rTCPChanMutex.Lock()
|
||||
defer fake.rTCPChanMutex.Unlock()
|
||||
fake.RTCPChanStub = stub
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) RTCPChanReturns(result1 *utils.CalmChannel) {
|
||||
func (fake *FakeParticipant) RTCPChanReturns(result1 chan []rtcp.Packet) {
|
||||
fake.rTCPChanMutex.Lock()
|
||||
defer fake.rTCPChanMutex.Unlock()
|
||||
fake.RTCPChanStub = nil
|
||||
fake.rTCPChanReturns = struct {
|
||||
result1 *utils.CalmChannel
|
||||
result1 chan []rtcp.Packet
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeParticipant) RTCPChanReturnsOnCall(i int, result1 *utils.CalmChannel) {
|
||||
func (fake *FakeParticipant) RTCPChanReturnsOnCall(i int, result1 chan []rtcp.Packet) {
|
||||
fake.rTCPChanMutex.Lock()
|
||||
defer fake.rTCPChanMutex.Unlock()
|
||||
fake.RTCPChanStub = nil
|
||||
if fake.rTCPChanReturnsOnCall == nil {
|
||||
fake.rTCPChanReturnsOnCall = make(map[int]struct {
|
||||
result1 *utils.CalmChannel
|
||||
result1 chan []rtcp.Packet
|
||||
})
|
||||
}
|
||||
fake.rTCPChanReturnsOnCall[i] = struct {
|
||||
result1 *utils.CalmChannel
|
||||
result1 chan []rtcp.Packet
|
||||
}{result1}
|
||||
}
|
||||
|
||||
|
||||
@@ -74,6 +74,11 @@ type FakePublishedTrack struct {
|
||||
removeSubscriberArgsForCall []struct {
|
||||
arg1 string
|
||||
}
|
||||
SetMutedStub func(bool)
|
||||
setMutedMutex sync.RWMutex
|
||||
setMutedArgsForCall []struct {
|
||||
arg1 bool
|
||||
}
|
||||
StartStub func()
|
||||
startMutex sync.RWMutex
|
||||
startArgsForCall []struct {
|
||||
@@ -443,6 +448,38 @@ func (fake *FakePublishedTrack) RemoveSubscriberArgsForCall(i int) string {
|
||||
return argsForCall.arg1
|
||||
}
|
||||
|
||||
func (fake *FakePublishedTrack) SetMuted(arg1 bool) {
|
||||
fake.setMutedMutex.Lock()
|
||||
fake.setMutedArgsForCall = append(fake.setMutedArgsForCall, struct {
|
||||
arg1 bool
|
||||
}{arg1})
|
||||
stub := fake.SetMutedStub
|
||||
fake.recordInvocation("SetMuted", []interface{}{arg1})
|
||||
fake.setMutedMutex.Unlock()
|
||||
if stub != nil {
|
||||
fake.SetMutedStub(arg1)
|
||||
}
|
||||
}
|
||||
|
||||
func (fake *FakePublishedTrack) SetMutedCallCount() int {
|
||||
fake.setMutedMutex.RLock()
|
||||
defer fake.setMutedMutex.RUnlock()
|
||||
return len(fake.setMutedArgsForCall)
|
||||
}
|
||||
|
||||
func (fake *FakePublishedTrack) SetMutedCalls(stub func(bool)) {
|
||||
fake.setMutedMutex.Lock()
|
||||
defer fake.setMutedMutex.Unlock()
|
||||
fake.SetMutedStub = stub
|
||||
}
|
||||
|
||||
func (fake *FakePublishedTrack) SetMutedArgsForCall(i int) bool {
|
||||
fake.setMutedMutex.RLock()
|
||||
defer fake.setMutedMutex.RUnlock()
|
||||
argsForCall := fake.setMutedArgsForCall[i]
|
||||
return argsForCall.arg1
|
||||
}
|
||||
|
||||
func (fake *FakePublishedTrack) Start() {
|
||||
fake.startMutex.Lock()
|
||||
fake.startArgsForCall = append(fake.startArgsForCall, struct {
|
||||
@@ -486,6 +523,8 @@ func (fake *FakePublishedTrack) Invocations() map[string][][]interface{} {
|
||||
defer fake.removeAllSubscribersMutex.RUnlock()
|
||||
fake.removeSubscriberMutex.RLock()
|
||||
defer fake.removeSubscriberMutex.RUnlock()
|
||||
fake.setMutedMutex.RLock()
|
||||
defer fake.setMutedMutex.RUnlock()
|
||||
fake.startMutex.RLock()
|
||||
defer fake.startMutex.RUnlock()
|
||||
copiedInvocations := map[string][][]interface{}{}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package rtc
|
||||
|
||||
import (
|
||||
"github.com/pion/ion-sfu/pkg/sfu"
|
||||
)
|
||||
|
||||
// wrapper around WebRTC receiver, overriding its ID
|
||||
|
||||
type WrappedReceiver struct {
|
||||
sfu.Receiver
|
||||
trackId string
|
||||
}
|
||||
|
||||
func NewWrappedReceiver(receiver sfu.Receiver, trackId string) WrappedReceiver {
|
||||
return WrappedReceiver{
|
||||
Receiver: receiver,
|
||||
trackId: trackId,
|
||||
}
|
||||
}
|
||||
|
||||
func (r WrappedReceiver) TrackID() string {
|
||||
return r.trackId
|
||||
}
|
||||
Reference in New Issue
Block a user