Skip to content

Commit

Permalink
Forward RTCP packets from WHIP connections to WHEP sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
esonderegger committed Apr 25, 2024
1 parent b751fb8 commit d8a7422
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
6 changes: 4 additions & 2 deletions internal/webrtc/whep.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
type (
whepSession struct {
videoTrack *trackMultiCodec
peerConnection *webrtc.PeerConnection
currentLayer atomic.Value
sequenceNumber uint16
timestamp uint32
Expand Down Expand Up @@ -147,8 +148,9 @@ func WHEP(offer, streamKey string) (string, string, error) {
defer stream.whepSessionsLock.Unlock()

stream.whepSessions[whepSessionId] = &whepSession{
videoTrack: videoTrack,
timestamp: 50000,
peerConnection: peerConnection,
videoTrack: videoTrack,
timestamp: 50000,
}
stream.whepSessions[whepSessionId].currentLayer.Store("")
return peerConnection.LocalDescription().SDP, whepSessionId, nil
Expand Down
15 changes: 15 additions & 0 deletions internal/webrtc/whip.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,20 @@ func videoWriter(remoteTrack *webrtc.TrackRemote, stream *stream, peerConnection
}
}

func forwardRtcpToWhepSessions(rtpReceiver *webrtc.RTPReceiver, stream *stream) {
for {
rtcpPackets, _, rtcpErr := rtpReceiver.ReadRTCP()
if rtcpErr != nil {
return
}
for _, sess := range stream.whepSessions {
if sess.peerConnection.ConnectionState() == webrtc.PeerConnectionStateConnected {
sess.peerConnection.WriteRTCP(rtcpPackets)

Check failure on line 132 in internal/webrtc/whip.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `sess.peerConnection.WriteRTCP` is not checked (errcheck)
}
}
}
}

func WHIP(offer, streamKey string) (string, error) {
peerConnection, err := newPeerConnection(apiWhip)
if err != nil {
Expand All @@ -135,6 +149,7 @@ func WHIP(offer, streamKey string) (string, error) {
}

peerConnection.OnTrack(func(remoteTrack *webrtc.TrackRemote, rtpReceiver *webrtc.RTPReceiver) {
go forwardRtcpToWhepSessions(rtpReceiver, stream)
if strings.HasPrefix(remoteTrack.Codec().RTPCodecCapability.MimeType, "audio") {
audioWriter(remoteTrack, stream)
} else {
Expand Down

0 comments on commit d8a7422

Please sign in to comment.