Skip to content

Commit c9b25c7

Browse files
committed
Simplify RTCP forwarding
Signed-off-by: Šimon Brandner <[email protected]>
1 parent ba10bd7 commit c9b25c7

File tree

2 files changed

+23
-37
lines changed

2 files changed

+23
-37
lines changed

src/publisher.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -127,31 +127,40 @@ func (p *Publisher) Matches(trackDescription event.SFUTrackDescription) bool {
127127

128128
func (p *Publisher) WriteRTCP(packets []rtcp.Packet) {
129129
packetsToSend := []rtcp.Packet{}
130+
readSSRC := uint32(p.Track.SSRC())
130131

131132
for _, packet := range packets {
132-
// Since we sometimes spam the sender with PLIs, make sure we don't send
133-
// them way too often
134-
if _, ok := packet.(*rtcp.PictureLossIndication); ok {
133+
switch typedPacket := packet.(type) {
134+
// We mung the packets here, so that the SSRCs match what the
135+
// receiver expects:
136+
// The media SSRC is the SSRC of the media about which the packet is
137+
// reporting; therefore, we mung it to be the SSRC of the publishing
138+
// participant's track. Without this, it would be SSRC of the SFU's
139+
// track which isn't right
140+
case *rtcp.PictureLossIndication:
141+
// Since we sometimes spam the sender with PLIs, make sure we don't send
142+
// them way too often
135143
if time.Now().UnixNano()-p.lastPLI.Load() < minimalPLIInterval.Nanoseconds() {
136144
continue
137145
}
138-
139146
p.lastPLI.Store(time.Now().UnixNano())
147+
148+
typedPacket.MediaSSRC = readSSRC
149+
packetsToSend = append(packetsToSend, typedPacket)
150+
case *rtcp.FullIntraRequest:
151+
typedPacket.MediaSSRC = readSSRC
152+
packetsToSend = append(packetsToSend, typedPacket)
140153
}
141154

142155
packetsToSend = append(packetsToSend, packet)
143156
}
144157

145-
if len(packetsToSend) < 1 {
146-
return
147-
}
148-
149-
if err := p.Call.PeerConnection.WriteRTCP(packetsToSend); err != nil {
150-
if errors.Is(err, io.ErrClosedPipe) {
151-
return
158+
if len(packetsToSend) != 1 {
159+
if err := p.Call.PeerConnection.WriteRTCP(packetsToSend); err != nil {
160+
if !errors.Is(err, io.ErrClosedPipe) {
161+
p.logger.WithError(err).Warn("failed to write RTCP on track")
162+
}
152163
}
153-
154-
p.logger.WithError(err).Warn("failed to write RTCP on track")
155164
}
156165
}
157166

src/subscriber.go

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"io"
2222
"sync"
2323

24-
"github.com/pion/rtcp"
2524
"github.com/pion/webrtc/v3"
2625
"github.com/sirupsen/logrus"
2726
)
@@ -125,28 +124,6 @@ func (s *Subscriber) forwardRTCP() {
125124
s.logger.WithError(err).Warn("failed to read RTCP on track")
126125
}
127126

128-
packetsToForward := []rtcp.Packet{}
129-
readSSRC := uint32(s.publisher.Track.SSRC())
130-
131-
for _, packet := range packets {
132-
switch typedPacket := packet.(type) {
133-
// We mung the packets here, so that the SSRCs match what the
134-
// receiver expects:
135-
// The media SSRC is the SSRC of the media about which the packet is
136-
// reporting; therefore, we mung it to be the SSRC of the publishing
137-
// participant's track. Without this, it would be SSRC of the SFU's
138-
// track which isn't right
139-
case *rtcp.PictureLossIndication:
140-
typedPacket.MediaSSRC = readSSRC
141-
packetsToForward = append(packetsToForward, typedPacket)
142-
case *rtcp.FullIntraRequest:
143-
typedPacket.MediaSSRC = readSSRC
144-
packetsToForward = append(packetsToForward, typedPacket)
145-
}
146-
}
147-
148-
if len(packetsToForward) != 0 {
149-
s.publisher.WriteRTCP(packetsToForward)
150-
}
127+
s.publisher.WriteRTCP(packets)
151128
}
152129
}

0 commit comments

Comments
 (0)