Skip to content

Commit 5d666eb

Browse files
committed
Handle missing track
Signed-off-by: Šimon Brandner <[email protected]>
1 parent 7f96ae6 commit 5d666eb

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

pkg/peer/peer.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/pion/rtcp"
1111
"github.com/pion/webrtc/v3"
1212
"github.com/sirupsen/logrus"
13+
"golang.org/x/exp/slices"
1314
"maunium.net/go/mautrix/event"
1415
)
1516

@@ -22,6 +23,7 @@ var (
2223
ErrDataChannelNotAvailable = errors.New("data channel is not available")
2324
ErrDataChannelNotReady = errors.New("data channel is not ready")
2425
ErrCantSubscribeToTrack = errors.New("can't subscribe to track")
26+
ErrCantWriteRTCP = errors.New("can't write RTCP")
2527
)
2628

2729
// A wrapped representation of the peer connection (single peer in the call).
@@ -117,16 +119,21 @@ func (p *Peer[ID]) SubscribeTo(track *webrtc.TrackLocalStaticRTP) error {
117119
return nil
118120
}
119121

120-
func (p *Peer[ID]) WriteRTCP(packets []rtcp.Packet, streamID string, trackID string, lastPLITimestamp int64) {
122+
func (p *Peer[ID]) WriteRTCP(packets []rtcp.Packet, streamID string, trackID string, lastPLITimestamp int64) error {
121123
const minimalPLIInterval = time.Millisecond * 500
122124

123125
packetsToSend := []rtcp.Packet{}
124126
var mediaSSRC uint32
125-
for _, receiver := range p.peerConnection.GetReceivers() {
126-
if receiver.Track().ID() == trackID && receiver.Track().StreamID() == streamID {
127-
mediaSSRC = uint32(receiver.Track().SSRC())
128-
break
129-
}
127+
receivers := p.peerConnection.GetReceivers()
128+
receiverIndex := slices.IndexFunc(receivers, func(receiver *webrtc.RTPReceiver) bool {
129+
return receiver.Track().ID() == trackID && receiver.Track().StreamID() == streamID
130+
})
131+
132+
if receiverIndex == -1 {
133+
p.logger.Error("failed to find track to write RTCP on")
134+
return ErrCantWriteRTCP
135+
} else {
136+
mediaSSRC = uint32(receivers[receiverIndex].Track().SSRC())
130137
}
131138

132139
for _, packet := range packets {
@@ -159,10 +166,13 @@ func (p *Peer[ID]) WriteRTCP(packets []rtcp.Packet, streamID string, trackID str
159166
if len(packetsToSend) != 0 {
160167
if err := p.peerConnection.WriteRTCP(packetsToSend); err != nil {
161168
if !errors.Is(err, io.ErrClosedPipe) {
162-
p.logger.WithError(err).Warn("failed to write RTCP on track")
169+
p.logger.WithError(err).Error("failed to write RTCP on track")
170+
return err
163171
}
164172
}
165173
}
174+
175+
return nil
166176
}
167177

168178
// Unsubscribes from the given list of tracks.

0 commit comments

Comments
 (0)