Skip to content

Commit 080a3dc

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 080a3dc

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

transport.go

Lines changed: 46 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 {
@@ -127,6 +133,23 @@ func (t *PCTransport) registerDefaultInterceptors(params PCTransportParams, i *i
127133
}
128134
i.Add(lkinterceptor.NewRTTFromXRFactory(onXRRtt))
129135

136+
// Add GCC congestion control (only for sender transports)
137+
if params.IsSender {
138+
congestionController, err := cc.NewInterceptor(func() (cc.BandwidthEstimator, error) {
139+
return gcc.NewSendSideBWE(gcc.SendSideBWEInitialBitrate(500_000))
140+
})
141+
if err != nil {
142+
return err
143+
}
144+
145+
estimatorChan := make(chan cc.BandwidthEstimator, 1)
146+
congestionController.OnNewPeerConnection(func(id string, estimator cc.BandwidthEstimator) {
147+
estimatorChan <- estimator
148+
})
149+
i.Add(congestionController)
150+
t.estimatorChan = estimatorChan
151+
}
152+
130153
return nil
131154
}
132155

@@ -184,6 +207,11 @@ func NewPCTransport(params PCTransportParams) (*PCTransport, error) {
184207
se.SetDTLSRetransmissionInterval(dtlsRetransmissionInterval)
185208
se.SetICETimeouts(iceDisconnectedTimeout, iceFailedTimeout, iceKeepaliveInterval)
186209

210+
// Configure TWCC header extension sender
211+
if err := webrtc.ConfigureTWCCHeaderExtensionSender(m, i); err != nil {
212+
return nil, err
213+
}
214+
187215
api := webrtc.NewAPI(webrtc.WithMediaEngine(m), webrtc.WithSettingEngine(se), webrtc.WithInterceptorRegistry(i))
188216
pc, err := api.NewPeerConnection(params.Configuration)
189217
if err != nil {
@@ -449,3 +477,21 @@ func (t *PCTransport) createAndSendOffer(options *webrtc.OfferOptions) error {
449477
func (t *PCTransport) SetConfiguration(config webrtc.Configuration) error {
450478
return t.pc.SetConfiguration(config)
451479
}
480+
481+
// GetBandwidthEstimator returns the current bandwidth estimator
482+
func (t *PCTransport) GetBandwidthEstimator() cc.BandwidthEstimator {
483+
// If we already have an estimator, return it
484+
if t.estimator != nil {
485+
return t.estimator
486+
}
487+
488+
// Otherwise, try to get it from the channel (non-blocking)
489+
select {
490+
case estimator := <-t.estimatorChan:
491+
t.estimator = estimator
492+
return estimator
493+
default:
494+
// No estimator available yet
495+
return nil
496+
}
497+
}

0 commit comments

Comments
 (0)