Use new intervalpli interceptor in examples

Instead of manually spawning a goroutine this demonstrates how an
interceptor can be useful.

Co-authored-by: Antoine Baché <antoine@tenten.app>
This commit is contained in:
Sean DuBois
2023-04-25 22:10:07 -04:00
parent 98860dda8c
commit 03c83a178c
7 changed files with 79 additions and 76 deletions

View File

@@ -8,10 +8,9 @@ import (
"fmt"
"os"
"strings"
"time"
"github.com/pion/interceptor"
"github.com/pion/rtcp"
"github.com/pion/interceptor/pkg/intervalpli"
"github.com/pion/webrtc/v3"
"github.com/pion/webrtc/v3/examples/internal/signal"
"github.com/pion/webrtc/v3/pkg/media"
@@ -65,8 +64,18 @@ func main() {
// for each PeerConnection.
i := &interceptor.Registry{}
// Register a intervalpli factory
// This interceptor sends a PLI every 3 seconds. A PLI causes a video keyframe to be generated by the sender.
// This makes our video seekable and more error resilent, but at a cost of lower picture quality and higher bitrates
// A real world application should process incoming RTCP packets from viewers and forward them to senders
intervalPliFactory, err := intervalpli.NewReceiverInterceptor()
if err != nil {
panic(err)
}
i.Add(intervalPliFactory)
// Use the default set of Interceptors
if err := webrtc.RegisterDefaultInterceptors(m, i); err != nil {
if err = webrtc.RegisterDefaultInterceptors(m, i); err != nil {
panic(err)
}
@@ -108,17 +117,6 @@ func main() {
// an ivf file, since we could have multiple video tracks we provide a counter.
// In your application this is where you would handle/process video
peerConnection.OnTrack(func(track *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) {
// Send a PLI on an interval so that the publisher is pushing a keyframe every rtcpPLIInterval
go func() {
ticker := time.NewTicker(time.Second * 3)
for range ticker.C {
errSend := peerConnection.WriteRTCP([]rtcp.Packet{&rtcp.PictureLossIndication{MediaSSRC: uint32(track.SSRC())}})
if errSend != nil {
fmt.Println(errSend)
}
}
}()
codec := track.Codec()
if strings.EqualFold(codec.MimeType, webrtc.MimeTypeOpus) {
fmt.Println("Got Opus track, saving to disk as output.opus (48 kHz, 2 channels)")