Skip to content

Commit d131859

Browse files
committed
Feat: Add bandwidth estimation and TWCC header extension support
* Add GCC (Google Congestion Control) bandwidth estimator for sender transports * Add GetBandwidthEstimator() method to PCTransport for accessing bandwidth estimates * Configure TWCC header extension sender with MediaEngine * Add congestion control interceptor with 500kbps initial bitrate * Maintain full backward compatibility
1 parent dd3b698 commit d131859

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

transport.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"github.com/bep/debounce"
2525
"github.com/pion/dtls/v3"
2626
"github.com/pion/interceptor"
27+
"github.com/pion/interceptor/pkg/cc"
28+
"github.com/pion/interceptor/pkg/gcc"
2729
"github.com/pion/interceptor/pkg/nack"
2830
"github.com/pion/interceptor/pkg/twcc"
2931
"github.com/pion/sdp/v3"
@@ -65,6 +67,10 @@ type PCTransport struct {
6567
onRTTUpdate func(rtt uint32)
6668

6769
OnOffer func(description webrtc.SessionDescription)
70+
71+
// Bandwidth estimation
72+
estimator cc.BandwidthEstimator
73+
estimatorChan chan cc.BandwidthEstimator
6874
}
6975

7076
type PCTransportParams struct {
@@ -109,6 +115,13 @@ func (t *PCTransport) registerDefaultInterceptors(params PCTransportParams, i *i
109115
}
110116
i.Add(twccGenerator)
111117

118+
// twcc header extension interceptor (must be before LimitSizeInterceptor)
119+
twccHeaderExt, err := twcc.NewHeaderExtensionInterceptor()
120+
if err != nil {
121+
return err
122+
}
123+
i.Add(twccHeaderExt)
124+
112125
i.Add(sdkinterceptor.NewLimitSizeInterceptorFactory())
113126

114127
if params.OnRTTUpdate != nil {
@@ -127,6 +140,23 @@ func (t *PCTransport) registerDefaultInterceptors(params PCTransportParams, i *i
127140
}
128141
i.Add(lkinterceptor.NewRTTFromXRFactory(onXRRtt))
129142

143+
// Add GCC congestion control (only for sender transports)
144+
if params.IsSender {
145+
congestionController, err := cc.NewInterceptor(func() (cc.BandwidthEstimator, error) {
146+
return gcc.NewSendSideBWE(gcc.SendSideBWEInitialBitrate(500_000))
147+
})
148+
if err != nil {
149+
return err
150+
}
151+
152+
estimatorChan := make(chan cc.BandwidthEstimator, 1)
153+
congestionController.OnNewPeerConnection(func(id string, estimator cc.BandwidthEstimator) {
154+
estimatorChan <- estimator
155+
})
156+
i.Add(congestionController)
157+
t.estimatorChan = estimatorChan
158+
}
159+
130160
return nil
131161
}
132162

@@ -449,3 +479,21 @@ func (t *PCTransport) createAndSendOffer(options *webrtc.OfferOptions) error {
449479
func (t *PCTransport) SetConfiguration(config webrtc.Configuration) error {
450480
return t.pc.SetConfiguration(config)
451481
}
482+
483+
// GetBandwidthEstimator returns the current bandwidth estimator
484+
func (t *PCTransport) GetBandwidthEstimator() cc.BandwidthEstimator {
485+
// If we already have an estimator, return it
486+
if t.estimator != nil {
487+
return t.estimator
488+
}
489+
490+
// Otherwise, try to get it from the channel (non-blocking)
491+
select {
492+
case estimator := <-t.estimatorChan:
493+
t.estimator = estimator
494+
return estimator
495+
default:
496+
// No estimator available yet
497+
return nil
498+
}
499+
}

0 commit comments

Comments
 (0)