Skip to content

Commit 9e17414

Browse files
tmatthSean-Der
authored andcommitted
Only send PLIs for video tracks
The existing examples were sending them for audio and video.
1 parent c3ff511 commit 9e17414

File tree

5 files changed

+55
-45
lines changed

5 files changed

+55
-45
lines changed

gocv-receive/main.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,18 @@ func createWebRTCConn(ffmpegIn io.Writer) {
153153
// Set a handler for when a new remote track starts, this handler copies inbound RTP packets,
154154
// replaces the SSRC and sends them back
155155
peerConnection.OnTrack(func(track *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
156-
// Send a PLI on an interval so that the publisher is pushing a keyframe every rtcpPLIInterval
157-
go func() {
158-
ticker := time.NewTicker(time.Second * 3)
159-
for range ticker.C {
160-
errSend := peerConnection.WriteRTCP([]rtcp.Packet{&rtcp.PictureLossIndication{MediaSSRC: uint32(track.SSRC())}})
161-
if errSend != nil {
162-
fmt.Println(errSend)
156+
if track.Kind() == webrtc.RTPCodecTypeVideo {
157+
// Send a PLI on an interval so that the publisher is pushing a keyframe every rtcpPLIInterval
158+
go func() {
159+
ticker := time.NewTicker(time.Second * 3)
160+
for range ticker.C {
161+
errSend := peerConnection.WriteRTCP([]rtcp.Packet{&rtcp.PictureLossIndication{MediaSSRC: uint32(track.SSRC())}})
162+
if errSend != nil {
163+
fmt.Println(errSend)
164+
}
163165
}
164-
}
165-
}()
166+
}()
167+
}
166168

167169
fmt.Printf("Track has started, of type %d: %s \n", track.PayloadType(), track.Codec().RTPCodecCapability.MimeType)
168170
for {

gstreamer-receive/main.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,18 @@ func main() {
4343
// Set a handler for when a new remote track starts, this handler creates a gstreamer pipeline
4444
// for the given codec
4545
peerConnection.OnTrack(func(track *webrtc.TrackRemote, _ *webrtc.RTPReceiver) {
46-
// Send a PLI on an interval so that the publisher is pushing a keyframe every rtcpPLIInterval
47-
go func() {
48-
ticker := time.NewTicker(time.Second * 3)
49-
for range ticker.C {
50-
rtcpSendErr := peerConnection.WriteRTCP([]rtcp.Packet{&rtcp.PictureLossIndication{MediaSSRC: uint32(track.SSRC())}})
51-
if rtcpSendErr != nil {
52-
fmt.Println(rtcpSendErr)
46+
if track.Kind() == webrtc.RTPCodecTypeVideo {
47+
// Send a PLI on an interval so that the publisher is pushing a keyframe every rtcpPLIInterval
48+
go func() {
49+
ticker := time.NewTicker(time.Second * 3)
50+
for range ticker.C {
51+
rtcpSendErr := peerConnection.WriteRTCP([]rtcp.Packet{&rtcp.PictureLossIndication{MediaSSRC: uint32(track.SSRC())}})
52+
if rtcpSendErr != nil {
53+
fmt.Println(rtcpSendErr)
54+
}
5355
}
54-
}
55-
}()
56+
}()
57+
}
5658

5759
codecName := strings.Split(track.Codec().RTPCodecCapability.MimeType, "/")[1]
5860
fmt.Printf("Track has started, of type %d: %s \n", track.PayloadType(), codecName)

save-to-webm/main.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,16 +194,18 @@ func createWebRTCConn(saver *webmSaver) *webrtc.PeerConnection {
194194
// Set a handler for when a new remote track starts, this handler copies inbound RTP packets,
195195
// replaces the SSRC and sends them back
196196
peerConnection.OnTrack(func(track *webrtc.TrackRemote, _ *webrtc.RTPReceiver) {
197-
// Send a PLI on an interval so that the publisher is pushing a keyframe every rtcpPLIInterval
198-
go func() {
199-
ticker := time.NewTicker(time.Second * 3)
200-
for range ticker.C {
201-
errSend := peerConnection.WriteRTCP([]rtcp.Packet{&rtcp.PictureLossIndication{MediaSSRC: uint32(track.SSRC())}})
202-
if errSend != nil {
203-
fmt.Println(errSend)
197+
if track.Kind() == webrtc.RTPCodecTypeVideo {
198+
// Send a PLI on an interval so that the publisher is pushing a keyframe every rtcpPLIInterval
199+
go func() {
200+
ticker := time.NewTicker(time.Second * 3)
201+
for range ticker.C {
202+
errSend := peerConnection.WriteRTCP([]rtcp.Packet{&rtcp.PictureLossIndication{MediaSSRC: uint32(track.SSRC())}})
203+
if errSend != nil {
204+
fmt.Println(errSend)
205+
}
204206
}
205-
}
206-
}()
207+
}()
208+
}
207209

208210
fmt.Printf("Track has started, of type %d: %s \n", track.PayloadType(), track.Codec().RTPCodecCapability.MimeType)
209211
for {

snapshot/main.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,18 @@ func signaling(w http.ResponseWriter, r *http.Request) {
4444
// Set a handler for when a new remote track starts, this handler saves buffers to SampleBuilder
4545
// so we can generate a snapshot
4646
peerConnection.OnTrack(func(track *webrtc.TrackRemote, _ *webrtc.RTPReceiver) {
47-
// Send a PLI on an interval so that the publisher is pushing a keyframe every rtcpPLIInterval
48-
go func() {
49-
ticker := time.NewTicker(time.Second * 3)
50-
for range ticker.C {
51-
errSend := peerConnection.WriteRTCP([]rtcp.Packet{&rtcp.PictureLossIndication{MediaSSRC: uint32(track.SSRC())}})
52-
if errSend != nil {
53-
fmt.Println(errSend)
47+
if track.Kind() == webrtc.RTPCodecTypeVideo {
48+
// Send a PLI on an interval so that the publisher is pushing a keyframe every rtcpPLIInterval
49+
go func() {
50+
ticker := time.NewTicker(time.Second * 3)
51+
for range ticker.C {
52+
errSend := peerConnection.WriteRTCP([]rtcp.Packet{&rtcp.PictureLossIndication{MediaSSRC: uint32(track.SSRC())}})
53+
if errSend != nil {
54+
fmt.Println(errSend)
55+
}
5456
}
55-
}
56-
}()
57+
}()
58+
}
5759

5860
for {
5961
// Read RTP Packets in a loop

twitch/main.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,18 @@ func main() {
8080
}
8181

8282
peerConnection.OnTrack(func(track *webrtc.TrackRemote, _ *webrtc.RTPReceiver) {
83-
// Send a PLI on an interval so that the publisher is pushing a keyframe every rtcpPLIInterval
84-
go func() {
85-
ticker := time.NewTicker(time.Second * 3)
86-
for range ticker.C {
87-
rtcpSendErr := peerConnection.WriteRTCP([]rtcp.Packet{&rtcp.PictureLossIndication{MediaSSRC: uint32(track.SSRC())}})
88-
if rtcpSendErr != nil {
89-
fmt.Println(rtcpSendErr)
83+
if track.Kind() == webrtc.RTPCodecTypeVideo {
84+
// Send a PLI on an interval so that the publisher is pushing a keyframe every rtcpPLIInterval
85+
go func() {
86+
ticker := time.NewTicker(time.Second * 3)
87+
for range ticker.C {
88+
rtcpSendErr := peerConnection.WriteRTCP([]rtcp.Packet{&rtcp.PictureLossIndication{MediaSSRC: uint32(track.SSRC())}})
89+
if rtcpSendErr != nil {
90+
fmt.Println(rtcpSendErr)
91+
}
9092
}
91-
}
92-
}()
93+
}()
94+
}
9395

9496
fmt.Printf("Track has started, of type %d: %s \n", track.PayloadType(), track.Codec().RTPCodecCapability.MimeType)
9597
for {

0 commit comments

Comments
 (0)