Skip to content

Commit

Permalink
fix(sfu): remove audio tracks from twcc (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrlandoCo authored Dec 10, 2020
1 parent fa1d1e0 commit 1ff724b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 33 deletions.
2 changes: 1 addition & 1 deletion pkg/buffer/interceptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (i *Interceptor) BindRTCPReader(reader interceptor.RTCPReader) interceptor.
case *rtcp.SenderReport:
buffer := i.getBuffer(pkt.SSRC)
if buffer == nil {
return pkts, attributes, nil
continue
}
buffer.setSenderReportData(pkt.RTPTime, pkt.NTPTime)
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/sfu/downtrack.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ type DownTrack struct {
}

// NewDownTrack returns a DownTrack.
func NewDownTrack(c webrtc.RTPCodecCapability, r Receiver, peerID, id, streamID string) (*DownTrack, error) {
func NewDownTrack(c webrtc.RTPCodecCapability, r Receiver, peerID string) (*DownTrack, error) {
return &DownTrack{
id: id,
id: r.TrackID(),
peerID: peerID,
streamID: r.StreamID(),
nList: newNACKList(),
codec: c,
receiver: r,
streamID: streamID,
codec: c,
}, nil
}

Expand Down Expand Up @@ -216,9 +216,10 @@ func (d *DownTrack) writeSimpleRTP(pkt rtp.Packet) error {
atomic.AddUint32(&d.octetCount, uint32(len(pkt.Payload)))
atomic.AddUint32(&d.packetCount, 1)

d.lastSSRC = pkt.SSRC
newSN := pkt.SequenceNumber - d.snOffset
newTS := pkt.Timestamp - d.tsOffset
if (newSN-d.lastSN)&0x8000 == 0 {
if (newSN-d.lastSN)&0x8000 == 0 || d.lastSN == 0 {
d.lastSN = newSN
atomic.StoreInt64(&d.lastPacketMs, time.Now().UnixNano()/1e6)
atomic.StoreUint32(&d.lastTS, newTS)
Expand Down
13 changes: 5 additions & 8 deletions pkg/sfu/mediaengine.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,13 @@ func getPublisherMediaEngine() (*webrtc.MediaEngine, error) {
sdp.SDESRTPStreamIDURI,
sdp.TransportCCURI,
} {
if err := me.RegisterHeaderExtension(webrtc.RTPHeaderExtensionCapability{URI: extension}, webrtc.RTPCodecTypeAudio); err != nil {
if err := me.RegisterHeaderExtension(webrtc.RTPHeaderExtensionCapability{URI: extension}, webrtc.RTPCodecTypeVideo); err != nil {
return nil, err
}
if err := me.RegisterHeaderExtension(webrtc.RTPHeaderExtensionCapability{URI: extension}, webrtc.RTPCodecTypeVideo); err != nil {
if extension == sdp.TransportCCURI {
continue
}
if err := me.RegisterHeaderExtension(webrtc.RTPHeaderExtensionCapability{URI: extension}, webrtc.RTPCodecTypeAudio); err != nil {
return nil, err
}
}
Expand All @@ -79,11 +82,5 @@ func getPublisherMediaEngine() (*webrtc.MediaEngine, error) {

func getSubscriberMediaEngine() (*webrtc.MediaEngine, error) {
me := &webrtc.MediaEngine{}
if err := me.RegisterHeaderExtension(webrtc.RTPHeaderExtensionCapability{URI: sdp.SDESMidURI}, webrtc.RTPCodecTypeVideo); err != nil {
return nil, err
}
if err := me.RegisterHeaderExtension(webrtc.RTPHeaderExtensionCapability{URI: sdp.SDESMidURI}, webrtc.RTPCodecTypeAudio); err != nil {
return nil, err
}
return me, nil
}
2 changes: 1 addition & 1 deletion pkg/sfu/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (r *router) addDownTrack(sub *Subscriber, recv Receiver) error {
Channels: codec.Channels,
SDPFmtpLine: codec.SDPFmtpLine,
RTCPFeedback: []webrtc.RTCPFeedback{{"goog-remb", ""}, {"nack", ""}, {"nack", "pli"}},
}, recv, sub.id, recv.TrackID(), recv.StreamID())
}, recv, sub.id)
if err != nil {
return err
}
Expand Down
44 changes: 26 additions & 18 deletions pkg/sfu/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ func (s *Subscriber) downTracksReports() {
Type: rtcp.SDESCNAME,
Text: dt.streamID,
}},
}, rtcp.SourceDescriptionChunk{
Source: dt.ssrc,
Items: []rtcp.SourceDescriptionItem{{
Type: rtcp.SDESType(15),
Text: dt.transceiver.Mid(),
}},
})
}
}
Expand All @@ -226,32 +232,34 @@ func (s *Subscriber) sendStreamDownTracksReports(streamID string) {
if !dt.bound.get() {
continue
}
now := time.Now().UnixNano()
nowNTP := timeToNtp(now)
lastPktMs := atomic.LoadInt64(&dt.lastPacketMs)
maxPktTs := atomic.LoadUint32(&dt.lastTS)
diffTs := uint32((now/1e6)-lastPktMs) * dt.codec.ClockRate / 1000
octets, packets := dt.getSRStats()
r = append(r, &rtcp.SenderReport{
SSRC: dt.ssrc,
NTPTime: nowNTP,
RTPTime: maxPktTs + diffTs,
PacketCount: packets,
OctetCount: octets,
})
sd = append(sd, rtcp.SourceDescriptionChunk{
Source: dt.ssrc,
Items: []rtcp.SourceDescriptionItem{{
Type: rtcp.SDESCNAME,
Text: dt.streamID,
}},
}, rtcp.SourceDescriptionChunk{
Source: dt.ssrc,
Items: []rtcp.SourceDescriptionItem{{
Type: rtcp.SDESType(15),
Text: dt.transceiver.Mid(),
}},
})
}
s.RUnlock()
if len(r) > 0 {
r = append(r, &rtcp.SourceDescription{Chunks: sd})
if err := s.pc.WriteRTCP(r); err != nil {
log.Errorf("Sending track binding reports err:%v", err)
r = append(r, &rtcp.SourceDescription{Chunks: sd})
go func() {
r := r
i := 0
for {
if err := s.pc.WriteRTCP(r); err != nil {
log.Errorf("Sending track binding reports err:%v", err)
}
if i > 5 {
return
}
i++
time.Sleep(20 * time.Millisecond)
}
}
}()
}

0 comments on commit 1ff724b

Please sign in to comment.