From 571ebf37324c125a20107027fad5e27efb85c625 Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Mon, 13 Jan 2025 15:39:44 +0100 Subject: [PATCH] Use the new TWCC API in example Simplify the bandwidth-estimation-from-disk example to use the simplified callback-less API. --- .../bandwidth-estimation-from-disk/main.go | 45 +++++++++---------- go.mod | 2 + 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/examples/bandwidth-estimation-from-disk/main.go b/examples/bandwidth-estimation-from-disk/main.go index 39aa35bc720..024a1c8b401 100644 --- a/examples/bandwidth-estimation-from-disk/main.go +++ b/examples/bandwidth-estimation-from-disk/main.go @@ -64,40 +64,40 @@ func main() { panic(err) } - // Create a Congestion Controller. This analyzes inbound and outbound data and provides - // suggestions on how much we should be sending. - // - // Passing `nil` means we use the default Estimation Algorithm which is Google Congestion Control. - // You can use the other ones that Pion provides, or write your own! - congestionController, err := cc.NewInterceptor(func() (cc.BandwidthEstimator, error) { - return gcc.NewSendSideBWE(gcc.SendSideBWEInitialBitrate(lowBitrate)) - }) - if err != nil { + if err := webrtc.ConfigureTWCCHeaderExtensionSender(m, i); err != nil { panic(err) } - estimatorChan := make(chan cc.BandwidthEstimator, 1) - congestionController.OnNewPeerConnection(func(id string, estimator cc.BandwidthEstimator) { //nolint: revive - estimatorChan <- estimator - }) - - i.Add(congestionController) - if err = webrtc.ConfigureTWCCHeaderExtensionSender(m, i); err != nil { + if err := webrtc.RegisterDefaultInterceptors(m, i); err != nil { panic(err) } - if err = webrtc.RegisterDefaultInterceptors(m, i); err != nil { + api := webrtc.NewAPI( + webrtc.WithInterceptorRegistry(i), webrtc.WithMediaEngine(m), + ) + + estimator, err := gcc.NewSendSideBWE( + gcc.SendSideBWEInitialBitrate(lowBitrate), + ) + if err != nil { panic(err) } - - // Create a new RTCPeerConnection - peerConnection, err := webrtc.NewAPI(webrtc.WithInterceptorRegistry(i), webrtc.WithMediaEngine(m)).NewPeerConnection(webrtc.Configuration{ + interceptor, err := cc.NewSingleInterceptor(estimator) + if err != nil { + panic(err) + } + configuration := webrtc.Configuration{ ICEServers: []webrtc.ICEServer{ { URLs: []string{"stun:stun.l.google.com:19302"}, }, }, - }) + } + + peerConnection, err := api.NewPeerConnection( + configuration, + webrtc.WithInterceptor(interceptor), + ) if err != nil { panic(err) } @@ -107,9 +107,6 @@ func main() { } }() - // Wait until our Bandwidth Estimator has been created - estimator := <-estimatorChan - // Create a video track videoTrack, err := webrtc.NewTrackLocalStaticSample(webrtc.RTPCodecCapability{MimeType: webrtc.MimeTypeVP8}, "video", "pion") if err != nil { diff --git a/go.mod b/go.mod index 8fc8e074b07..05d50a78bfd 100644 --- a/go.mod +++ b/go.mod @@ -34,3 +34,5 @@ require ( golang.org/x/sys v0.28.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) + +replace github.com/pion/interceptor => ../interceptor